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