xref: /onnv-gate/usr/src/lib/libdiskmgt/common/alias.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <fcntl.h>
30*0Sstevel@tonic-gate #include <libdevinfo.h>
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate #include <stdlib.h>
33*0Sstevel@tonic-gate #include <sys/dkio.h>
34*0Sstevel@tonic-gate #include <sys/sunddi.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "libdiskmgt.h"
39*0Sstevel@tonic-gate #include "disks_private.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate static int		get_status(disk_t *diskp, int fd, nvlist_t *attrs);
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate descriptor_t **
alias_get_assoc_descriptors(descriptor_t * desc,dm_desc_type_t type,int * errp)44*0Sstevel@tonic-gate alias_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type,
45*0Sstevel@tonic-gate     int *errp)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	switch (type) {
48*0Sstevel@tonic-gate 	case DM_DRIVE:
49*0Sstevel@tonic-gate 	    return (drive_get_assocs(desc, errp));
50*0Sstevel@tonic-gate 	}
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	*errp = EINVAL;
53*0Sstevel@tonic-gate 	return (NULL);
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate nvlist_t *
alias_get_attributes(descriptor_t * dp,int * errp)57*0Sstevel@tonic-gate alias_get_attributes(descriptor_t *dp, int *errp)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	alias_t		*ap;
60*0Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	/* Find the alias for this descriptor */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	*errp = ENODEV;
65*0Sstevel@tonic-gate 	for (ap = dp->p.disk->aliases; ap != NULL; ap = ap->next) {
66*0Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(dp->name, ap->alias)) {
67*0Sstevel@tonic-gate 		/* we found the alias for this descriptor */
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 		if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
70*0Sstevel@tonic-gate 		    *errp = ENOMEM;
71*0Sstevel@tonic-gate 		    return (NULL);
72*0Sstevel@tonic-gate 		}
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 		if (ap->target >= 0) {
75*0Sstevel@tonic-gate 		    if (nvlist_add_uint32(attrs, DM_LUN, ap->lun) != 0) {
76*0Sstevel@tonic-gate 			nvlist_free(attrs);
77*0Sstevel@tonic-gate 			*errp = ENOMEM;
78*0Sstevel@tonic-gate 			return (NULL);
79*0Sstevel@tonic-gate 		    }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 		    if (nvlist_add_uint32(attrs, DM_TARGET, ap->target) != 0) {
82*0Sstevel@tonic-gate 			nvlist_free(attrs);
83*0Sstevel@tonic-gate 			*errp = ENOMEM;
84*0Sstevel@tonic-gate 			return (NULL);
85*0Sstevel@tonic-gate 		    }
86*0Sstevel@tonic-gate 		}
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 		if (ap->wwn != NULL) {
89*0Sstevel@tonic-gate 		    if (nvlist_add_string(attrs, DM_WWN, ap->wwn) != 0) {
90*0Sstevel@tonic-gate 			nvlist_free(attrs);
91*0Sstevel@tonic-gate 			*errp = ENOMEM;
92*0Sstevel@tonic-gate 			return (NULL);
93*0Sstevel@tonic-gate 		    }
94*0Sstevel@tonic-gate 		}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 		if (ap->devpaths != NULL) {
97*0Sstevel@tonic-gate 		    /* get the status for this alias */
98*0Sstevel@tonic-gate 		    int		fd;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 		    fd = open(ap->devpaths->devpath, O_RDONLY|O_NDELAY);
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 		    if ((*errp = get_status(dp->p.disk, fd, attrs)) != 0) {
103*0Sstevel@tonic-gate 			nvlist_free(attrs);
104*0Sstevel@tonic-gate 			attrs = NULL;
105*0Sstevel@tonic-gate 		    }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 		    if (fd >= 0) {
108*0Sstevel@tonic-gate 			(void) close(fd);
109*0Sstevel@tonic-gate 		    }
110*0Sstevel@tonic-gate 		}
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 		*errp = 0;
113*0Sstevel@tonic-gate 		break;
114*0Sstevel@tonic-gate 	    }
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	return (attrs);
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate descriptor_t *
alias_get_descriptor_by_name(char * name,int * errp)121*0Sstevel@tonic-gate alias_get_descriptor_by_name(char *name, int *errp)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	descriptor_t	**aliases;
124*0Sstevel@tonic-gate 	int		i;
125*0Sstevel@tonic-gate 	descriptor_t	*alias = NULL;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	aliases = cache_get_descriptors(DM_ALIAS, errp);
128*0Sstevel@tonic-gate 	if (*errp != 0) {
129*0Sstevel@tonic-gate 	    return (NULL);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	for (i = 0; aliases[i]; i++) {
133*0Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(name, aliases[i]->name)) {
134*0Sstevel@tonic-gate 		alias = aliases[i];
135*0Sstevel@tonic-gate 	    } else {
136*0Sstevel@tonic-gate 		/* clean up the unused descriptors */
137*0Sstevel@tonic-gate 		cache_free_descriptor(aliases[i]);
138*0Sstevel@tonic-gate 	    }
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 	free(aliases);
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	if (alias == NULL) {
143*0Sstevel@tonic-gate 	    *errp = ENODEV;
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	return (alias);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /* ARGSUSED */
150*0Sstevel@tonic-gate descriptor_t **
alias_get_descriptors(int filter[],int * errp)151*0Sstevel@tonic-gate alias_get_descriptors(int filter[], int *errp)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	return (cache_get_descriptors(DM_ALIAS, errp));
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate char *
alias_get_name(descriptor_t * desc)157*0Sstevel@tonic-gate alias_get_name(descriptor_t *desc)
158*0Sstevel@tonic-gate {
159*0Sstevel@tonic-gate 	return (desc->name);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /* ARGSUSED */
163*0Sstevel@tonic-gate nvlist_t *
alias_get_stats(descriptor_t * dp,int stat_type,int * errp)164*0Sstevel@tonic-gate alias_get_stats(descriptor_t *dp, int stat_type, int *errp)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	/* There are no stat types defined for aliases */
167*0Sstevel@tonic-gate 	*errp = EINVAL;
168*0Sstevel@tonic-gate 	return (NULL);
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate int
alias_make_descriptors()172*0Sstevel@tonic-gate alias_make_descriptors()
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate 	int		error;
175*0Sstevel@tonic-gate 	disk_t		*dp;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	dp = cache_get_disklist();
178*0Sstevel@tonic-gate 	while (dp != NULL) {
179*0Sstevel@tonic-gate 	    alias_t *ap;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	    ap = dp->aliases;
182*0Sstevel@tonic-gate 	    while (ap != NULL) {
183*0Sstevel@tonic-gate 		if (ap->alias != NULL) {
184*0Sstevel@tonic-gate 		    cache_load_desc(DM_ALIAS, dp, ap->alias, NULL, &error);
185*0Sstevel@tonic-gate 		    if (error != 0) {
186*0Sstevel@tonic-gate 			return (error);
187*0Sstevel@tonic-gate 		    }
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 		ap = ap->next;
190*0Sstevel@tonic-gate 	    }
191*0Sstevel@tonic-gate 	    dp = dp->next;
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	return (0);
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate static int
get_status(disk_t * diskp,int fd,nvlist_t * attrs)198*0Sstevel@tonic-gate get_status(disk_t *diskp, int fd, nvlist_t *attrs)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate 	struct dk_minfo	minfo;
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	/* Make sure media is inserted and spun up. */
203*0Sstevel@tonic-gate 	if (fd >= 0 && media_read_info(fd, &minfo)) {
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_UP) != 0) {
206*0Sstevel@tonic-gate 		return (ENOMEM);
207*0Sstevel@tonic-gate 	    }
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	} else {
210*0Sstevel@tonic-gate 	    /* Not ready, so either no media or dead. */
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	    if (diskp->removable) {
213*0Sstevel@tonic-gate 		/* This is a removable drive with no media. */
214*0Sstevel@tonic-gate 		if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_UP) != 0) {
215*0Sstevel@tonic-gate 		    return (ENOMEM);
216*0Sstevel@tonic-gate 		}
217*0Sstevel@tonic-gate 	    } else {
218*0Sstevel@tonic-gate 		/* not removable, so must be dead */
219*0Sstevel@tonic-gate 		if (nvlist_add_uint32(attrs, DM_STATUS, DM_DISK_DOWN) != 0) {
220*0Sstevel@tonic-gate 		    return (ENOMEM);
221*0Sstevel@tonic-gate 		}
222*0Sstevel@tonic-gate 	    }
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	return (0);
226*0Sstevel@tonic-gate }
227