17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
518c2aff7Sartem  * Common Development and Distribution License (the "License").
618c2aff7Sartem  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21342440ecSPrasad Singamsetty 
227c478bd9Sstevel@tonic-gate /*
23342440ecSPrasad Singamsetty  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27c470f575SYuri Pankov /*
28c470f575SYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
29c470f575SYuri Pankov  */
30c470f575SYuri Pankov 
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <dirent.h>
377c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <devid.h>
447c478bd9Sstevel@tonic-gate #include <dirent.h>
457c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
467c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
497c478bd9Sstevel@tonic-gate #include "disks_private.h"
507c478bd9Sstevel@tonic-gate #include "partition.h"
517c478bd9Sstevel@tonic-gate #ifndef VT_ENOTSUP
527c478bd9Sstevel@tonic-gate #define	VT_ENOTSUP	(-5)
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	FMT_UNKNOWN	0
567c478bd9Sstevel@tonic-gate #define	FMT_VTOC	1
577c478bd9Sstevel@tonic-gate #define	FMT_EFI		2
587c478bd9Sstevel@tonic-gate 
5946a2abf2Seschrock typedef int (*detectorp)(char *, nvlist_t *, int *);
6046a2abf2Seschrock 
6146a2abf2Seschrock static detectorp detectors[] = {
6246a2abf2Seschrock 	inuse_mnt,
6346a2abf2Seschrock 	inuse_active_zpool,
6446a2abf2Seschrock 	inuse_lu,
6546a2abf2Seschrock 	inuse_dump,
6646a2abf2Seschrock 	inuse_vxvm,
6746a2abf2Seschrock 	inuse_exported_zpool,
6846a2abf2Seschrock 	inuse_fs,  /* fs should always be last */
6946a2abf2Seschrock 	NULL
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static int	add_inuse(char *name, nvlist_t *attrs);
737c478bd9Sstevel@tonic-gate static int	desc_ok(descriptor_t *dp);
747c478bd9Sstevel@tonic-gate static void	dsk2rdsk(char *dsk, char *rdsk, int size);
757c478bd9Sstevel@tonic-gate static int	get_attrs(descriptor_t *dp, int fd,  nvlist_t *attrs);
767c478bd9Sstevel@tonic-gate static descriptor_t **get_fixed_assocs(descriptor_t *desc, int *errp);
777c478bd9Sstevel@tonic-gate static int	get_slice_num(slice_t *devp);
787c478bd9Sstevel@tonic-gate static int	match_fixed_name(disk_t *dp, char *name, int *errp);
797c478bd9Sstevel@tonic-gate static int	make_fixed_descriptors(disk_t *dp);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate descriptor_t **
slice_get_assoc_descriptors(descriptor_t * desc,dm_desc_type_t type,int * errp)827c478bd9Sstevel@tonic-gate slice_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type,
837c478bd9Sstevel@tonic-gate     int *errp)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	if (!desc_ok(desc)) {
86*fe12dc75SToomas Soome 		*errp = ENODEV;
87*fe12dc75SToomas Soome 		return (NULL);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	switch (type) {
917c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
92*fe12dc75SToomas Soome 		return (media_get_assocs(desc, errp));
937c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
94*fe12dc75SToomas Soome 		return (partition_get_assocs(desc, errp));
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
987c478bd9Sstevel@tonic-gate 	return (NULL);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * This is called by media/partition to get the slice descriptors for the given
1037c478bd9Sstevel@tonic-gate  * media/partition descriptor.
1047c478bd9Sstevel@tonic-gate  * For media, just get the slices, but for a partition, it must be a solaris
1057c478bd9Sstevel@tonic-gate  * partition and if there are active partitions, it must be the active one.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate descriptor_t **
slice_get_assocs(descriptor_t * desc,int * errp)1087c478bd9Sstevel@tonic-gate slice_get_assocs(descriptor_t *desc, int *errp)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	/* Just check the first drive name. */
1117c478bd9Sstevel@tonic-gate 	if (desc->p.disk->aliases == NULL) {
112*fe12dc75SToomas Soome 		*errp = 0;
113*fe12dc75SToomas Soome 		return (libdiskmgt_empty_desc_array(errp));
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
11618c2aff7Sartem 	return (get_fixed_assocs(desc, errp));
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate nvlist_t *
slice_get_attributes(descriptor_t * dp,int * errp)1207c478bd9Sstevel@tonic-gate slice_get_attributes(descriptor_t *dp, int *errp)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
1237c478bd9Sstevel@tonic-gate 	int		fd;
1247c478bd9Sstevel@tonic-gate 	char		devpath[MAXPATHLEN];
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (!desc_ok(dp)) {
127*fe12dc75SToomas Soome 		*errp = ENODEV;
128*fe12dc75SToomas Soome 		return (NULL);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
132*fe12dc75SToomas Soome 		*errp = ENOMEM;
133*fe12dc75SToomas Soome 		return (NULL);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/* dp->name is /dev/dsk, need to convert back to /dev/rdsk */
1377c478bd9Sstevel@tonic-gate 	dsk2rdsk(dp->name, devpath, sizeof (devpath));
1387c478bd9Sstevel@tonic-gate 	fd = open(devpath, O_RDONLY|O_NDELAY);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((*errp = get_attrs(dp, fd, attrs)) != 0) {
141*fe12dc75SToomas Soome 		nvlist_free(attrs);
142*fe12dc75SToomas Soome 		attrs = NULL;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
146*fe12dc75SToomas Soome 		(void) close(fd);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	return (attrs);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Look for the slice by the slice devpath.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate descriptor_t *
slice_get_descriptor_by_name(char * name,int * errp)1567c478bd9Sstevel@tonic-gate slice_get_descriptor_by_name(char *name, int *errp)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	int		found = 0;
1597c478bd9Sstevel@tonic-gate 	disk_t		*dp;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	for (dp = cache_get_disklist(); dp != NULL; dp = dp->next) {
1627c478bd9Sstevel@tonic-gate 		found = match_fixed_name(dp, name, errp);
1637c478bd9Sstevel@tonic-gate 
16418c2aff7Sartem 		if (found) {
16518c2aff7Sartem 			char	mname[MAXPATHLEN];
1667c478bd9Sstevel@tonic-gate 
16718c2aff7Sartem 			if (*errp != 0) {
168*fe12dc75SToomas Soome 				return (NULL);
16918c2aff7Sartem 			}
1707c478bd9Sstevel@tonic-gate 
17118c2aff7Sartem 			mname[0] = 0;
17218c2aff7Sartem 			(void) media_read_name(dp, mname, sizeof (mname));
1737c478bd9Sstevel@tonic-gate 
17418c2aff7Sartem 			return (cache_get_desc(DM_SLICE, dp, name, mname,
17518c2aff7Sartem 			    errp));
17618c2aff7Sartem 		}
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	*errp = ENODEV;
1807c478bd9Sstevel@tonic-gate 	return (NULL);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /* ARGSUSED */
1847c478bd9Sstevel@tonic-gate descriptor_t **
slice_get_descriptors(int filter[],int * errp)1857c478bd9Sstevel@tonic-gate slice_get_descriptors(int filter[], int *errp)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	return (cache_get_descriptors(DM_SLICE, errp));
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate char *
slice_get_name(descriptor_t * desc)1917c478bd9Sstevel@tonic-gate slice_get_name(descriptor_t *desc)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	return (desc->name);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate nvlist_t *
slice_get_stats(descriptor_t * dp,int stat_type,int * errp)1977c478bd9Sstevel@tonic-gate slice_get_stats(descriptor_t *dp, int stat_type, int *errp)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	nvlist_t	*stats;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (stat_type != DM_SLICE_STAT_USE) {
202*fe12dc75SToomas Soome 		*errp = EINVAL;
203*fe12dc75SToomas Soome 		return (NULL);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	*errp = 0;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&stats, NVATTRS_STAT, 0) != 0) {
209*fe12dc75SToomas Soome 		*errp = ENOMEM;
210*fe12dc75SToomas Soome 		return (NULL);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if ((*errp = add_inuse(dp->name, stats)) != 0) {
21483e86541SYuri Pankov 		nvlist_free(stats);
21583e86541SYuri Pankov 		return (NULL);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	return (stats);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * A slice descriptor points to a disk, the name is the devpath and the
2237c478bd9Sstevel@tonic-gate  * secondary name is the media name.
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate int
slice_make_descriptors()2267c478bd9Sstevel@tonic-gate slice_make_descriptors()
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	disk_t		*dp;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	dp = cache_get_disklist();
2317c478bd9Sstevel@tonic-gate 	while (dp != NULL) {
232*fe12dc75SToomas Soome 		int	error;
2337c478bd9Sstevel@tonic-gate 
234*fe12dc75SToomas Soome 		error = make_fixed_descriptors(dp);
235*fe12dc75SToomas Soome 		if (error != 0) {
236*fe12dc75SToomas Soome 			return (error);
237*fe12dc75SToomas Soome 		}
2387c478bd9Sstevel@tonic-gate 
239*fe12dc75SToomas Soome 		dp = dp->next;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	return (0);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /* convert rdsk paths to dsk paths */
2467c478bd9Sstevel@tonic-gate void
slice_rdsk2dsk(char * rdsk,char * dsk,int size)2477c478bd9Sstevel@tonic-gate slice_rdsk2dsk(char *rdsk, char *dsk, int size)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	char	*strp;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	(void) strlcpy(dsk, rdsk, size);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if ((strp = strstr(dsk, "/rdsk/")) == NULL) {
254*fe12dc75SToomas Soome 		/* not rdsk, check for floppy */
255*fe12dc75SToomas Soome 		strp = strstr(dsk, "/rdiskette");
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (strp != NULL) {
259*fe12dc75SToomas Soome 		strp++;	/* move ptr to the r in rdsk or rdiskette */
2607c478bd9Sstevel@tonic-gate 
261*fe12dc75SToomas Soome 		/* move the succeeding chars over by one */
262*fe12dc75SToomas Soome 		do {
263*fe12dc75SToomas Soome 			*strp = *(strp + 1);
264*fe12dc75SToomas Soome 			strp++;
265*fe12dc75SToomas Soome 		} while (*strp);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * Check if/how the slice is used.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate static int
add_inuse(char * name,nvlist_t * attrs)2737c478bd9Sstevel@tonic-gate add_inuse(char *name, nvlist_t *attrs)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	int	i;
2767c478bd9Sstevel@tonic-gate 	int	error;
2777c478bd9Sstevel@tonic-gate 
27846a2abf2Seschrock 	for (i = 0; detectors[i] != NULL; i ++) {
279*fe12dc75SToomas Soome 		if (detectors[i](name, attrs, &error) || error != 0) {
280*fe12dc75SToomas Soome 			if (error != 0) {
281*fe12dc75SToomas Soome 				return (error);
282*fe12dc75SToomas Soome 			}
283*fe12dc75SToomas Soome 			break;
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	return (0);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate /* return 1 if the slice descriptor is still valid, 0 if not. */
2917c478bd9Sstevel@tonic-gate static int
desc_ok(descriptor_t * dp)2927c478bd9Sstevel@tonic-gate desc_ok(descriptor_t *dp)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	/* First verify the media name for removable media */
2957c478bd9Sstevel@tonic-gate 	if (dp->p.disk->removable) {
296*fe12dc75SToomas Soome 		char	mname[MAXPATHLEN];
2977c478bd9Sstevel@tonic-gate 
298*fe12dc75SToomas Soome 		if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
299*fe12dc75SToomas Soome 			return (0);
300*fe12dc75SToomas Soome 		}
3017c478bd9Sstevel@tonic-gate 
302*fe12dc75SToomas Soome 		if (mname[0] == 0) {
303*fe12dc75SToomas Soome 			return (libdiskmgt_str_eq(dp->secondary_name, NULL));
304*fe12dc75SToomas Soome 		} else {
305*fe12dc75SToomas Soome 			return (libdiskmgt_str_eq(dp->secondary_name, mname));
306*fe12dc75SToomas Soome 		}
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/*
3107c478bd9Sstevel@tonic-gate 	 * We could verify the slice is still there, but other code down the
3117c478bd9Sstevel@tonic-gate 	 * line already does these checks (e.g. see get_attrs).
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	return (1);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /* convert dsk paths to rdsk paths */
3187c478bd9Sstevel@tonic-gate static void
dsk2rdsk(char * dsk,char * rdsk,int size)3197c478bd9Sstevel@tonic-gate dsk2rdsk(char *dsk, char *rdsk, int size)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	char	*slashp;
3227c478bd9Sstevel@tonic-gate 	size_t	len;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	(void) strlcpy(rdsk, dsk, size);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* make sure there is enough room to add the r to dsk */
3277c478bd9Sstevel@tonic-gate 	len = strlen(dsk);
3287c478bd9Sstevel@tonic-gate 	if (len + 2 > size) {
329*fe12dc75SToomas Soome 		return;
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if ((slashp = strstr(rdsk, "/dsk/")) == NULL) {
333*fe12dc75SToomas Soome 		/* not dsk, check for floppy */
334*fe12dc75SToomas Soome 		slashp = strstr(rdsk, "/diskette");
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (slashp != NULL) {
338*fe12dc75SToomas Soome 		char	*endp;
3397c478bd9Sstevel@tonic-gate 
340*fe12dc75SToomas Soome 		endp = rdsk + len;	/* point to terminating 0 */
341*fe12dc75SToomas Soome 		/* move the succeeding chars over by one */
342*fe12dc75SToomas Soome 		do {
343*fe12dc75SToomas Soome 			*(endp + 1) = *endp;
344*fe12dc75SToomas Soome 			endp--;
345*fe12dc75SToomas Soome 		} while (endp != slashp);
3467c478bd9Sstevel@tonic-gate 
347*fe12dc75SToomas Soome 		*(endp + 1) = 'r';
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static int
get_attrs(descriptor_t * dp,int fd,nvlist_t * attrs)3527c478bd9Sstevel@tonic-gate get_attrs(descriptor_t *dp, int fd,  nvlist_t *attrs)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	struct dk_minfo	minfo;
3557c478bd9Sstevel@tonic-gate 	int		status;
3567c478bd9Sstevel@tonic-gate 	int		data_format = FMT_UNKNOWN;
3577c478bd9Sstevel@tonic-gate 	int		snum = -1;
3587c478bd9Sstevel@tonic-gate 	int		error;
359342440ecSPrasad Singamsetty 	struct extvtoc	vtoc;
3607c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efip;
3617c478bd9Sstevel@tonic-gate 	struct dk_cinfo	dkinfo;
3627c478bd9Sstevel@tonic-gate 	int		cooked_fd;
3637c478bd9Sstevel@tonic-gate 	struct stat	buf;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (fd < 0) {
366*fe12dc75SToomas Soome 		return (ENODEV);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/* First make sure media is inserted and spun up. */
3707c478bd9Sstevel@tonic-gate 	if (!media_read_info(fd, &minfo)) {
371*fe12dc75SToomas Soome 		return (ENODEV);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
374342440ecSPrasad Singamsetty 	if ((status = read_extvtoc(fd, &vtoc)) >= 0) {
375*fe12dc75SToomas Soome 		data_format = FMT_VTOC;
3767c478bd9Sstevel@tonic-gate 	} else if (status == VT_ENOTSUP && efi_alloc_and_read(fd, &efip) >= 0) {
377*fe12dc75SToomas Soome 		data_format = FMT_EFI;
378*fe12dc75SToomas Soome 		if (nvlist_add_boolean(attrs, DM_EFI) != 0) {
379*fe12dc75SToomas Soome 			efi_free(efip);
380*fe12dc75SToomas Soome 			return (ENOMEM);
381*fe12dc75SToomas Soome 		}
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (data_format == FMT_UNKNOWN) {
385*fe12dc75SToomas Soome 		return (ENODEV);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, &dkinfo) >= 0) {
389*fe12dc75SToomas Soome 		snum = dkinfo.dki_partition;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/* check the slice */
3937c478bd9Sstevel@tonic-gate 	if (data_format == FMT_VTOC) {
394*fe12dc75SToomas Soome 		if (snum < 0 || snum >= vtoc.v_nparts ||
395*fe12dc75SToomas Soome 		    vtoc.v_part[snum].p_size == 0) {
396*fe12dc75SToomas Soome 			return (ENODEV);
397*fe12dc75SToomas Soome 		}
3987c478bd9Sstevel@tonic-gate 	} else { /* data_format == FMT_EFI */
399*fe12dc75SToomas Soome 		if (snum < 0 || snum >= efip->efi_nparts ||
400*fe12dc75SToomas Soome 		    efip->efi_parts[snum].p_size == 0) {
401*fe12dc75SToomas Soome 			efi_free(efip);
402*fe12dc75SToomas Soome 			return (ENODEV);
403*fe12dc75SToomas Soome 		}
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/* the slice exists */
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_INDEX, snum) != 0) {
409*fe12dc75SToomas Soome 		if (data_format == FMT_EFI) {
410*fe12dc75SToomas Soome 			efi_free(efip);
411*fe12dc75SToomas Soome 		}
412*fe12dc75SToomas Soome 		return (ENOMEM);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (data_format == FMT_VTOC) {
416*fe12dc75SToomas Soome 		if (nvlist_add_uint64(attrs, DM_START,
417*fe12dc75SToomas Soome 		    vtoc.v_part[snum].p_start) != 0) {
418*fe12dc75SToomas Soome 			return (ENOMEM);
419*fe12dc75SToomas Soome 		}
4207c478bd9Sstevel@tonic-gate 
421*fe12dc75SToomas Soome 		if (nvlist_add_uint64(attrs, DM_SIZE,
422*fe12dc75SToomas Soome 		    vtoc.v_part[snum].p_size) != 0) {
423*fe12dc75SToomas Soome 			return (ENOMEM);
424*fe12dc75SToomas Soome 		}
4257c478bd9Sstevel@tonic-gate 
426*fe12dc75SToomas Soome 		if (nvlist_add_uint32(attrs, DM_TAG,
427*fe12dc75SToomas Soome 		    vtoc.v_part[snum].p_tag) != 0) {
428*fe12dc75SToomas Soome 			return (ENOMEM);
429*fe12dc75SToomas Soome 		}
4307c478bd9Sstevel@tonic-gate 
431*fe12dc75SToomas Soome 		if (nvlist_add_uint32(attrs, DM_FLAG,
432*fe12dc75SToomas Soome 		    vtoc.v_part[snum].p_flag) != 0) {
433*fe12dc75SToomas Soome 			return (ENOMEM);
434*fe12dc75SToomas Soome 		}
4357c478bd9Sstevel@tonic-gate 	} else { /* data_format == FMT_EFI */
436*fe12dc75SToomas Soome 		if (nvlist_add_uint64(attrs, DM_START,
437*fe12dc75SToomas Soome 		    efip->efi_parts[snum].p_start) != 0) {
438*fe12dc75SToomas Soome 			efi_free(efip);
439*fe12dc75SToomas Soome 			return (ENOMEM);
440*fe12dc75SToomas Soome 		}
4417c478bd9Sstevel@tonic-gate 
442*fe12dc75SToomas Soome 		if (nvlist_add_uint64(attrs, DM_SIZE,
443*fe12dc75SToomas Soome 		    efip->efi_parts[snum].p_size) != 0) {
444*fe12dc75SToomas Soome 			efi_free(efip);
445*fe12dc75SToomas Soome 			return (ENOMEM);
446*fe12dc75SToomas Soome 		}
4477c478bd9Sstevel@tonic-gate 
448*fe12dc75SToomas Soome 		if (efip->efi_parts[snum].p_name[0] != 0) {
449*fe12dc75SToomas Soome 			char	label[EFI_PART_NAME_LEN + 1];
4507c478bd9Sstevel@tonic-gate 
451*fe12dc75SToomas Soome 			(void) snprintf(label, sizeof (label), "%.*s",
452*fe12dc75SToomas Soome 			    EFI_PART_NAME_LEN, efip->efi_parts[snum].p_name);
453*fe12dc75SToomas Soome 			if (nvlist_add_string(attrs, DM_EFI_NAME, label) != 0) {
454*fe12dc75SToomas Soome 				efi_free(efip);
455*fe12dc75SToomas Soome 				return (ENOMEM);
456*fe12dc75SToomas Soome 			}
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if (data_format == FMT_EFI) {
461*fe12dc75SToomas Soome 		efi_free(efip);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (inuse_mnt(dp->name, attrs, &error)) {
465*fe12dc75SToomas Soome 		if (error != 0)
466*fe12dc75SToomas Soome 			return (error);
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (fstat(fd, &buf) != -1) {
470*fe12dc75SToomas Soome 		if (nvlist_add_uint64(attrs, DM_DEVT, buf.st_rdev) != 0) {
471*fe12dc75SToomas Soome 			return (ENOMEM);
472*fe12dc75SToomas Soome 		}
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * We need to open the cooked slice (not the raw one) to get the
477c470f575SYuri Pankov 	 * correct devid.
4787c478bd9Sstevel@tonic-gate 	 */
479c470f575SYuri Pankov 	cooked_fd = open(dp->name, O_RDONLY|O_NDELAY);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (cooked_fd >= 0) {
482*fe12dc75SToomas Soome 		int		no_mem = 0;
483*fe12dc75SToomas Soome 		ddi_devid_t	devid;
4847c478bd9Sstevel@tonic-gate 
485*fe12dc75SToomas Soome 		if (devid_get(cooked_fd, &devid) == 0) {
486*fe12dc75SToomas Soome 			char	*minor;
4877c478bd9Sstevel@tonic-gate 
488*fe12dc75SToomas Soome 			if (devid_get_minor_name(cooked_fd, &minor) == 0) {
489*fe12dc75SToomas Soome 				char	*devidstr;
4907c478bd9Sstevel@tonic-gate 
491*fe12dc75SToomas Soome 				devidstr = devid_str_encode(devid, minor);
492*fe12dc75SToomas Soome 				if (devidstr != NULL) {
4937c478bd9Sstevel@tonic-gate 
494*fe12dc75SToomas Soome 					if (nvlist_add_string(attrs,
495*fe12dc75SToomas Soome 					    DM_DEVICEID, devidstr) != 0) {
496*fe12dc75SToomas Soome 						no_mem = 1;
497*fe12dc75SToomas Soome 					}
4987c478bd9Sstevel@tonic-gate 
499*fe12dc75SToomas Soome 					devid_str_free(devidstr);
500*fe12dc75SToomas Soome 				}
501*fe12dc75SToomas Soome 				devid_str_free(minor);
502*fe12dc75SToomas Soome 			}
503*fe12dc75SToomas Soome 			devid_free(devid);
5047c478bd9Sstevel@tonic-gate 		}
505*fe12dc75SToomas Soome 		(void) close(cooked_fd);
5067c478bd9Sstevel@tonic-gate 
507*fe12dc75SToomas Soome 		if (no_mem) {
508*fe12dc75SToomas Soome 			return (ENOMEM);
509*fe12dc75SToomas Soome 		}
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	return (0);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static descriptor_t **
get_fixed_assocs(descriptor_t * desc,int * errp)5167c478bd9Sstevel@tonic-gate get_fixed_assocs(descriptor_t *desc, int *errp)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	int		fd;
5197c478bd9Sstevel@tonic-gate 	int		status;
5207c478bd9Sstevel@tonic-gate 	int		data_format = FMT_UNKNOWN;
5217c478bd9Sstevel@tonic-gate 	int		cnt;
522342440ecSPrasad Singamsetty 	struct extvtoc	vtoc;
5237c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efip;
5247c478bd9Sstevel@tonic-gate 	int		pos;
5257c478bd9Sstevel@tonic-gate 	char		*media_name = NULL;
5267c478bd9Sstevel@tonic-gate 	slice_t		*devp;
5277c478bd9Sstevel@tonic-gate 	descriptor_t	**slices;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if ((fd = drive_open_disk(desc->p.disk, NULL, 0)) < 0) {
530*fe12dc75SToomas Soome 		*errp = ENODEV;
531*fe12dc75SToomas Soome 		return (NULL);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
534342440ecSPrasad Singamsetty 	if ((status = read_extvtoc(fd, &vtoc)) >= 0) {
535*fe12dc75SToomas Soome 		data_format = FMT_VTOC;
5367c478bd9Sstevel@tonic-gate 	} else if (status == VT_ENOTSUP && efi_alloc_and_read(fd, &efip) >= 0) {
537*fe12dc75SToomas Soome 		data_format = FMT_EFI;
5387c478bd9Sstevel@tonic-gate 	} else {
539*fe12dc75SToomas Soome 		(void) close(fd);
540*fe12dc75SToomas Soome 		*errp = 0;
541*fe12dc75SToomas Soome 		return (libdiskmgt_empty_desc_array(errp));
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	(void) close(fd);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/* count the number of slices */
546*fe12dc75SToomas Soome 	devp = desc->p.disk->aliases->devpaths;
547*fe12dc75SToomas Soome 	for (cnt = 0; devp != NULL; devp = devp->next)
548*fe12dc75SToomas Soome 		cnt++;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/* allocate the array for the descriptors */
5517c478bd9Sstevel@tonic-gate 	slices = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
5527c478bd9Sstevel@tonic-gate 	if (slices == NULL) {
553*fe12dc75SToomas Soome 		if (data_format == FMT_EFI) {
554*fe12dc75SToomas Soome 			efi_free(efip);
555*fe12dc75SToomas Soome 		}
556*fe12dc75SToomas Soome 		*errp = ENOMEM;
557*fe12dc75SToomas Soome 		return (NULL);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/* get the media name from the descriptor */
5617c478bd9Sstevel@tonic-gate 	if (desc->type == DM_MEDIA) {
562*fe12dc75SToomas Soome 		media_name = desc->name;
5637c478bd9Sstevel@tonic-gate 	} else {
564*fe12dc75SToomas Soome 		/* must be a DM_PARTITION */
565*fe12dc75SToomas Soome 		media_name = desc->secondary_name;
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	pos = 0;
5697c478bd9Sstevel@tonic-gate 	for (devp = desc->p.disk->aliases->devpaths; devp != NULL;
5707c478bd9Sstevel@tonic-gate 	    devp = devp->next) {
5717c478bd9Sstevel@tonic-gate 
572*fe12dc75SToomas Soome 		int		slice_num;
573*fe12dc75SToomas Soome 		char	devpath[MAXPATHLEN];
5747c478bd9Sstevel@tonic-gate 
575*fe12dc75SToomas Soome 		slice_num = get_slice_num(devp);
576*fe12dc75SToomas Soome 		/* can't get slicenum, so no need to keep trying the drive */
577*fe12dc75SToomas Soome 		if (slice_num == -1) {
578*fe12dc75SToomas Soome 			break;
5797c478bd9Sstevel@tonic-gate 		}
580*fe12dc75SToomas Soome 
581*fe12dc75SToomas Soome 		if (data_format == FMT_VTOC) {
582*fe12dc75SToomas Soome 			if (slice_num >= vtoc.v_nparts ||
583*fe12dc75SToomas Soome 			    vtoc.v_part[slice_num].p_size == 0) {
584*fe12dc75SToomas Soome 				continue;
585*fe12dc75SToomas Soome 			}
586*fe12dc75SToomas Soome 		} else { /* data_format == FMT_EFI */
587*fe12dc75SToomas Soome 			if (slice_num >= efip->efi_nparts ||
588*fe12dc75SToomas Soome 			    efip->efi_parts[slice_num].p_size == 0) {
589*fe12dc75SToomas Soome 				continue;
590*fe12dc75SToomas Soome 			}
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 
593*fe12dc75SToomas Soome 		slice_rdsk2dsk(devp->devpath, devpath, sizeof (devpath));
594*fe12dc75SToomas Soome 		slices[pos] = cache_get_desc(DM_SLICE, desc->p.disk, devpath,
595*fe12dc75SToomas Soome 		    media_name, errp);
596*fe12dc75SToomas Soome 		if (*errp != 0) {
597*fe12dc75SToomas Soome 			cache_free_descriptors(slices);
598*fe12dc75SToomas Soome 			if (data_format == FMT_EFI) {
599*fe12dc75SToomas Soome 				efi_free(efip);
600*fe12dc75SToomas Soome 			}
601*fe12dc75SToomas Soome 			return (NULL);
6027c478bd9Sstevel@tonic-gate 		}
603*fe12dc75SToomas Soome 		pos++;
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 	slices[pos] = NULL;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (data_format == FMT_EFI) {
608*fe12dc75SToomas Soome 		efi_free(efip);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	*errp = 0;
6127c478bd9Sstevel@tonic-gate 	return (slices);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate static int
get_slice_num(slice_t * devp)6167c478bd9Sstevel@tonic-gate get_slice_num(slice_t *devp)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	/* check if we already determined the devpath slice number */
6197c478bd9Sstevel@tonic-gate 	if (devp->slice_num == -1) {
620*fe12dc75SToomas Soome 		int		fd;
6217c478bd9Sstevel@tonic-gate 
622*fe12dc75SToomas Soome 		if ((fd = open(devp->devpath, O_RDONLY|O_NDELAY)) >= 0) {
623*fe12dc75SToomas Soome 			struct dk_cinfo	dkinfo;
624*fe12dc75SToomas Soome 			if (ioctl(fd, DKIOCINFO, &dkinfo) >= 0) {
625*fe12dc75SToomas Soome 				devp->slice_num = dkinfo.dki_partition;
626*fe12dc75SToomas Soome 			}
627*fe12dc75SToomas Soome 			(void) close(fd);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	return (devp->slice_num);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate static int
make_fixed_descriptors(disk_t * dp)6357c478bd9Sstevel@tonic-gate make_fixed_descriptors(disk_t *dp)
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	int		error = 0;
6387c478bd9Sstevel@tonic-gate 	alias_t		*ap;
6397c478bd9Sstevel@tonic-gate 	slice_t		*devp;
6407c478bd9Sstevel@tonic-gate 	char		mname[MAXPATHLEN];
6417c478bd9Sstevel@tonic-gate 	int		data_format = FMT_UNKNOWN;
642342440ecSPrasad Singamsetty 	struct extvtoc	vtoc;
6437c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efip;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/* Just check the first drive name. */
6467c478bd9Sstevel@tonic-gate 	if ((ap = dp->aliases) == NULL) {
647*fe12dc75SToomas Soome 		return (0);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	mname[0] = 0;
6517c478bd9Sstevel@tonic-gate 	(void) media_read_name(dp, mname, sizeof (mname));
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	for (devp = ap->devpaths; devp != NULL; devp = devp->next) {
654*fe12dc75SToomas Soome 		int		slice_num;
655*fe12dc75SToomas Soome 		char	devpath[MAXPATHLEN];
656*fe12dc75SToomas Soome 
657*fe12dc75SToomas Soome 		slice_num = get_slice_num(devp);
658*fe12dc75SToomas Soome 		/* can't get slicenum, so no need to keep trying the drive */
659*fe12dc75SToomas Soome 		if (slice_num == -1) {
660*fe12dc75SToomas Soome 			break;
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 
663*fe12dc75SToomas Soome 		if (data_format == FMT_UNKNOWN) {
664*fe12dc75SToomas Soome 			int	fd;
665*fe12dc75SToomas Soome 			int	status;
666*fe12dc75SToomas Soome 
667*fe12dc75SToomas Soome 			if ((fd = drive_open_disk(dp, NULL, 0)) >= 0) {
668*fe12dc75SToomas Soome 				if ((status = read_extvtoc(fd, &vtoc)) >= 0) {
669*fe12dc75SToomas Soome 					data_format = FMT_VTOC;
670*fe12dc75SToomas Soome 				} else if (status == VT_ENOTSUP &&
671*fe12dc75SToomas Soome 				    efi_alloc_and_read(fd, &efip) >= 0) {
672*fe12dc75SToomas Soome 					data_format = FMT_EFI;
673*fe12dc75SToomas Soome 				}
674*fe12dc75SToomas Soome 				(void) close(fd);
675*fe12dc75SToomas Soome 			}
676*fe12dc75SToomas Soome 		}
6777c478bd9Sstevel@tonic-gate 
678*fe12dc75SToomas Soome 		/* can't get slice data, so no need to keep trying the drive */
679*fe12dc75SToomas Soome 		if (data_format == FMT_UNKNOWN) {
680*fe12dc75SToomas Soome 			break;
6817c478bd9Sstevel@tonic-gate 		}
682*fe12dc75SToomas Soome 
683*fe12dc75SToomas Soome 		if (data_format == FMT_VTOC) {
684*fe12dc75SToomas Soome 			if (slice_num >= vtoc.v_nparts ||
685*fe12dc75SToomas Soome 			    vtoc.v_part[slice_num].p_size == 0) {
686*fe12dc75SToomas Soome 				continue;
687*fe12dc75SToomas Soome 			}
688*fe12dc75SToomas Soome 		} else { /* data_format == FMT_EFI */
689*fe12dc75SToomas Soome 			if (slice_num >= efip->efi_nparts ||
690*fe12dc75SToomas Soome 			    efip->efi_parts[slice_num].p_size == 0) {
691*fe12dc75SToomas Soome 				continue;
692*fe12dc75SToomas Soome 			}
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 
695*fe12dc75SToomas Soome 		slice_rdsk2dsk(devp->devpath, devpath, sizeof (devpath));
696*fe12dc75SToomas Soome 		cache_load_desc(DM_SLICE, dp, devpath, mname, &error);
697*fe12dc75SToomas Soome 		if (error != 0) {
698*fe12dc75SToomas Soome 			break;
699*fe12dc75SToomas Soome 		}
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (data_format == FMT_EFI) {
703*fe12dc75SToomas Soome 		efi_free(efip);
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	return (error);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate  * Just look for the name on the devpaths we have cached. Return 1 if we
7117c478bd9Sstevel@tonic-gate  * find the name and the size of that slice is non-zero.
7127c478bd9Sstevel@tonic-gate  */
7137c478bd9Sstevel@tonic-gate static int
match_fixed_name(disk_t * diskp,char * name,int * errp)7147c478bd9Sstevel@tonic-gate match_fixed_name(disk_t *diskp, char *name, int *errp)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	slice_t		*dp = NULL;
7177c478bd9Sstevel@tonic-gate 	alias_t		*ap;
7187c478bd9Sstevel@tonic-gate 	int		slice_num;
7197c478bd9Sstevel@tonic-gate 	int		fd;
7207c478bd9Sstevel@tonic-gate 	int		status;
7217c478bd9Sstevel@tonic-gate 	int		data_format = FMT_UNKNOWN;
722342440ecSPrasad Singamsetty 	struct extvtoc	vtoc;
7237c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efip;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	ap = diskp->aliases;
7267c478bd9Sstevel@tonic-gate 	while (ap != NULL) {
727*fe12dc75SToomas Soome 		slice_t	*devp;
7287c478bd9Sstevel@tonic-gate 
729*fe12dc75SToomas Soome 		devp = ap->devpaths;
730*fe12dc75SToomas Soome 		while (devp != NULL) {
731*fe12dc75SToomas Soome 			char	path[MAXPATHLEN];
7327c478bd9Sstevel@tonic-gate 
733*fe12dc75SToomas Soome 			slice_rdsk2dsk(devp->devpath, path, sizeof (path));
734*fe12dc75SToomas Soome 			if (libdiskmgt_str_eq(path, name)) {
735*fe12dc75SToomas Soome 				/* found it */
736*fe12dc75SToomas Soome 				dp = devp;
737*fe12dc75SToomas Soome 				break;
738*fe12dc75SToomas Soome 			}
7397c478bd9Sstevel@tonic-gate 
740*fe12dc75SToomas Soome 			devp = devp->next;
741*fe12dc75SToomas Soome 		}
7427c478bd9Sstevel@tonic-gate 
743*fe12dc75SToomas Soome 		if (dp != NULL) {
744*fe12dc75SToomas Soome 			break;
745*fe12dc75SToomas Soome 		}
7467c478bd9Sstevel@tonic-gate 
747*fe12dc75SToomas Soome 		ap = ap->next;
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
751*fe12dc75SToomas Soome 		*errp = 0;
752*fe12dc75SToomas Soome 		return (0);
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	/*
7567c478bd9Sstevel@tonic-gate 	 * If we found a match on the name we now have to check that this
7577c478bd9Sstevel@tonic-gate 	 * slice really exists (non-0 size).
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	slice_num = get_slice_num(dp);
7617c478bd9Sstevel@tonic-gate 	/* can't get slicenum, so no slice */
7627c478bd9Sstevel@tonic-gate 	if (slice_num == -1) {
763*fe12dc75SToomas Soome 		*errp = ENODEV;
764