xref: /titanic_54/usr/src/lib/libdiskmgt/common/slice.c (revision 46a2abf27af40eda17a3f97e79eda1aef4e3c3c8)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*46a2abf2Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <dirent.h>
357c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <devid.h>
427c478bd9Sstevel@tonic-gate #include <dirent.h>
437c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
447c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
477c478bd9Sstevel@tonic-gate #include "disks_private.h"
487c478bd9Sstevel@tonic-gate #include "partition.h"
497c478bd9Sstevel@tonic-gate #ifndef VT_ENOTSUP
507c478bd9Sstevel@tonic-gate #define	VT_ENOTSUP	(-5)
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	FMT_UNKNOWN	0
547c478bd9Sstevel@tonic-gate #define	FMT_VTOC	1
557c478bd9Sstevel@tonic-gate #define	FMT_EFI		2
567c478bd9Sstevel@tonic-gate 
57*46a2abf2Seschrock typedef int (*detectorp)(char *, nvlist_t *, int *);
58*46a2abf2Seschrock 
59*46a2abf2Seschrock static detectorp detectors[] = {
60*46a2abf2Seschrock 	inuse_mnt,
61*46a2abf2Seschrock 	inuse_svm,
62*46a2abf2Seschrock 	inuse_active_zpool,
63*46a2abf2Seschrock 	inuse_lu,
64*46a2abf2Seschrock 	inuse_dump,
65*46a2abf2Seschrock 	inuse_vxvm,
66*46a2abf2Seschrock 	inuse_exported_zpool,
67*46a2abf2Seschrock 	inuse_fs,  /* fs should always be last */
68*46a2abf2Seschrock 	NULL
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static int	add_inuse(char *name, nvlist_t *attrs);
727c478bd9Sstevel@tonic-gate static int	desc_ok(descriptor_t *dp);
737c478bd9Sstevel@tonic-gate static void	dsk2rdsk(char *dsk, char *rdsk, int size);
747c478bd9Sstevel@tonic-gate static int	get_attrs(descriptor_t *dp, int fd,  nvlist_t *attrs);
757c478bd9Sstevel@tonic-gate static descriptor_t **get_fixed_assocs(descriptor_t *desc, int *errp);
767c478bd9Sstevel@tonic-gate static descriptor_t **get_removable_assocs(descriptor_t *desc, char *volm_path,
777c478bd9Sstevel@tonic-gate 		    int *errp);
787c478bd9Sstevel@tonic-gate static int	get_slice_num(slice_t *devp);
797c478bd9Sstevel@tonic-gate static int	match_fixed_name(disk_t *dp, char *name, int *errp);
807c478bd9Sstevel@tonic-gate static int	match_removable_name(disk_t *dp, char *name, int *errp);
817c478bd9Sstevel@tonic-gate static int	make_fixed_descriptors(disk_t *dp);
827c478bd9Sstevel@tonic-gate static int	make_removable_descriptors(disk_t *dp);
837c478bd9Sstevel@tonic-gate static int	make_volm_dir_descriptors(disk_t *dp, int fd,
847c478bd9Sstevel@tonic-gate 		    char *volm_path);
857c478bd9Sstevel@tonic-gate static int	num_removable_slices(int fd, struct stat *bufp,
867c478bd9Sstevel@tonic-gate 		    char *volm_path);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate descriptor_t **
897c478bd9Sstevel@tonic-gate slice_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type,
907c478bd9Sstevel@tonic-gate     int *errp)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	if (!desc_ok(desc)) {
937c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
947c478bd9Sstevel@tonic-gate 	    return (NULL);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	switch (type) {
987c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
997c478bd9Sstevel@tonic-gate 	    return (media_get_assocs(desc, errp));
1007c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
1017c478bd9Sstevel@tonic-gate 	    return (partition_get_assocs(desc, errp));
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
1057c478bd9Sstevel@tonic-gate 	return (NULL);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * This is called by media/partition to get the slice descriptors for the given
1107c478bd9Sstevel@tonic-gate  * media/partition descriptor.
1117c478bd9Sstevel@tonic-gate  * For media, just get the slices, but for a partition, it must be a solaris
1127c478bd9Sstevel@tonic-gate  * partition and if there are active partitions, it must be the active one.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate descriptor_t **
1157c478bd9Sstevel@tonic-gate slice_get_assocs(descriptor_t *desc, int *errp)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	int		under_volm = 0;
1187c478bd9Sstevel@tonic-gate 	char		volm_path[MAXPATHLEN];
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/* Just check the first drive name. */
1217c478bd9Sstevel@tonic-gate 	if (desc->p.disk->aliases == NULL) {
1227c478bd9Sstevel@tonic-gate 	    *errp = 0;
1237c478bd9Sstevel@tonic-gate 	    return (libdiskmgt_empty_desc_array(errp));
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (desc->p.disk->removable) {
1277c478bd9Sstevel@tonic-gate 	    if ((under_volm = media_get_volm_path(desc->p.disk, volm_path,
1287c478bd9Sstevel@tonic-gate 		sizeof (volm_path)))) {
1297c478bd9Sstevel@tonic-gate 		if (volm_path[0] == 0) {
1307c478bd9Sstevel@tonic-gate 		    /* no media */
1317c478bd9Sstevel@tonic-gate 		    *errp = 0;
1327c478bd9Sstevel@tonic-gate 		    return (libdiskmgt_empty_desc_array(errp));
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	    }
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (desc->p.disk->removable) {
1387c478bd9Sstevel@tonic-gate 	    if (under_volm) {
1397c478bd9Sstevel@tonic-gate 		return (get_removable_assocs(desc, volm_path, errp));
1407c478bd9Sstevel@tonic-gate 	    } else {
1417c478bd9Sstevel@tonic-gate 		return (get_fixed_assocs(desc, errp));
1427c478bd9Sstevel@tonic-gate 	    }
1437c478bd9Sstevel@tonic-gate 	} else {
1447c478bd9Sstevel@tonic-gate 	    return (get_fixed_assocs(desc, errp));
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate nvlist_t *
1497c478bd9Sstevel@tonic-gate slice_get_attributes(descriptor_t *dp, int *errp)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
1527c478bd9Sstevel@tonic-gate 	int		fd;
1537c478bd9Sstevel@tonic-gate 	char		devpath[MAXPATHLEN];
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (!desc_ok(dp)) {
1567c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
1577c478bd9Sstevel@tonic-gate 	    return (NULL);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
1617c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
1627c478bd9Sstevel@tonic-gate 	    return (NULL);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/* dp->name is /dev/dsk, need to convert back to /dev/rdsk */
1667c478bd9Sstevel@tonic-gate 	dsk2rdsk(dp->name, devpath, sizeof (devpath));
1677c478bd9Sstevel@tonic-gate 	fd = open(devpath, O_RDONLY|O_NDELAY);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if ((*errp = get_attrs(dp, fd, attrs)) != 0) {
1707c478bd9Sstevel@tonic-gate 	    nvlist_free(attrs);
1717c478bd9Sstevel@tonic-gate 	    attrs = NULL;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
1757c478bd9Sstevel@tonic-gate 	    (void) close(fd);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	return (attrs);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Look for the slice by the slice devpath.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate descriptor_t *
1857c478bd9Sstevel@tonic-gate slice_get_descriptor_by_name(char *name, int *errp)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	int		found = 0;
1887c478bd9Sstevel@tonic-gate 	disk_t		*dp;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	for (dp = cache_get_disklist(); dp != NULL; dp = dp->next) {
1917c478bd9Sstevel@tonic-gate 	    if (dp->removable) {
1927c478bd9Sstevel@tonic-gate 		found = match_removable_name(dp, name, errp);
1937c478bd9Sstevel@tonic-gate 	    } else {
1947c478bd9Sstevel@tonic-gate 		found = match_fixed_name(dp, name, errp);
1957c478bd9Sstevel@tonic-gate 	    }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	    if (found) {
1987c478bd9Sstevel@tonic-gate 		char	mname[MAXPATHLEN];
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		if (*errp != 0) {
2017c478bd9Sstevel@tonic-gate 		    return (NULL);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		mname[0] = 0;
2057c478bd9Sstevel@tonic-gate 		(void) media_read_name(dp, mname, sizeof (mname));
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		return (cache_get_desc(DM_SLICE, dp, name, mname, errp));
2087c478bd9Sstevel@tonic-gate 	    }
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	*errp = ENODEV;
2127c478bd9Sstevel@tonic-gate 	return (NULL);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /* ARGSUSED */
2167c478bd9Sstevel@tonic-gate descriptor_t **
2177c478bd9Sstevel@tonic-gate slice_get_descriptors(int filter[], int *errp)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	return (cache_get_descriptors(DM_SLICE, errp));
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate char *
2237c478bd9Sstevel@tonic-gate slice_get_name(descriptor_t *desc)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	return (desc->name);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate nvlist_t *
2297c478bd9Sstevel@tonic-gate slice_get_stats(descriptor_t *dp, int stat_type, int *errp)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate 	nvlist_t	*stats;
2327c478bd9Sstevel@tonic-gate 	char		*str;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (stat_type != DM_SLICE_STAT_USE) {
2357c478bd9Sstevel@tonic-gate 	    *errp = EINVAL;
2367c478bd9Sstevel@tonic-gate 	    return (NULL);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	*errp = 0;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&stats, NVATTRS_STAT, 0) != 0) {
2427c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
2437c478bd9Sstevel@tonic-gate 	    return (NULL);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if ((*errp = add_inuse(dp->name, stats)) != 0) {
2477c478bd9Sstevel@tonic-gate 	    return (NULL);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* if no cluster use, check for a use of the local name */
2517c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(stats, DM_USED_BY, &str) != 0) {
2527c478bd9Sstevel@tonic-gate 	    disk_t	*diskp;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	    diskp = dp->p.disk;
2557c478bd9Sstevel@tonic-gate 	    if (diskp->aliases != NULL && diskp->aliases->cluster) {
2567c478bd9Sstevel@tonic-gate 		slice_t		*sp;
2577c478bd9Sstevel@tonic-gate 		int		snum = -1;
2587c478bd9Sstevel@tonic-gate 		struct dk_minfo	minfo;
2597c478bd9Sstevel@tonic-gate 		struct dk_cinfo	dkinfo;
2607c478bd9Sstevel@tonic-gate 		char		devpath[MAXPATHLEN];
2617c478bd9Sstevel@tonic-gate 		int		fd;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		/* dp->name is /dev/dsk, need to convert back to /dev/rdsk */
2647c478bd9Sstevel@tonic-gate 		dsk2rdsk(dp->name, devpath, sizeof (devpath));
2657c478bd9Sstevel@tonic-gate 		fd = open(devpath, O_RDONLY|O_NDELAY);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		if (fd >= 0 && media_read_info(fd, &minfo) &&
2687c478bd9Sstevel@tonic-gate 		    ioctl(fd, DKIOCINFO, &dkinfo) >= 0) {
2697c478bd9Sstevel@tonic-gate 		    snum = dkinfo.dki_partition;
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		if (fd >= 0) {
2737c478bd9Sstevel@tonic-gate 		    (void) close(fd);
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		if (snum >= 0) {
2777c478bd9Sstevel@tonic-gate 		    for (sp = diskp->aliases->orig_paths; sp != NULL;
2787c478bd9Sstevel@tonic-gate 			sp = sp->next) {
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 			if (sp->slice_num == snum) {
2817c478bd9Sstevel@tonic-gate 			    char	localpath[MAXPATHLEN];
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 			    slice_rdsk2dsk(sp->devpath, localpath,
2847c478bd9Sstevel@tonic-gate 				sizeof (localpath));
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 			    if ((*errp = add_inuse(localpath, stats)) != 0) {
2877c478bd9Sstevel@tonic-gate 				return (NULL);
2887c478bd9Sstevel@tonic-gate 			    }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 			    break;
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 		    }
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 	    }
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	return (stats);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * A slice descriptor points to a disk, the name is the devpath and the
3027c478bd9Sstevel@tonic-gate  * secondary name is the media name.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate int
3057c478bd9Sstevel@tonic-gate slice_make_descriptors()
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	disk_t		*dp;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	dp = cache_get_disklist();
3107c478bd9Sstevel@tonic-gate 	while (dp != NULL) {
3117c478bd9Sstevel@tonic-gate 	    int	error;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	    if (dp->removable) {
3147c478bd9Sstevel@tonic-gate 		error = make_removable_descriptors(dp);
3157c478bd9Sstevel@tonic-gate 	    } else {
3167c478bd9Sstevel@tonic-gate 		error = make_fixed_descriptors(dp);
3177c478bd9Sstevel@tonic-gate 	    }
3187c478bd9Sstevel@tonic-gate 	    if (error != 0) {
3197c478bd9Sstevel@tonic-gate 		return (error);
3207c478bd9Sstevel@tonic-gate 	    }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	    dp = dp->next;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /* convert rdsk paths to dsk paths */
3297c478bd9Sstevel@tonic-gate void
3307c478bd9Sstevel@tonic-gate slice_rdsk2dsk(char *rdsk, char *dsk, int size)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	char	*strp;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	(void) strlcpy(dsk, rdsk, size);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if ((strp = strstr(dsk, "/rdsk/")) == NULL) {
3377c478bd9Sstevel@tonic-gate 	    /* not rdsk, check for floppy */
3387c478bd9Sstevel@tonic-gate 	    strp = strstr(dsk, "/rdiskette");
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (strp != NULL) {
3427c478bd9Sstevel@tonic-gate 	    strp++;	/* move ptr to the r in rdsk or rdiskette */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	    /* move the succeeding chars over by one */
3457c478bd9Sstevel@tonic-gate 	    do {
3467c478bd9Sstevel@tonic-gate 		*strp = *(strp + 1);
3477c478bd9Sstevel@tonic-gate 		strp++;
3487c478bd9Sstevel@tonic-gate 	    } while (*strp);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * Check if/how the slice is used.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static int
3567c478bd9Sstevel@tonic-gate add_inuse(char *name, nvlist_t *attrs)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	int	i;
3597c478bd9Sstevel@tonic-gate 	int	error;
3607c478bd9Sstevel@tonic-gate 
361*46a2abf2Seschrock 	for (i = 0; detectors[i] != NULL; i ++) {
362*46a2abf2Seschrock 	    if (detectors[i](name, attrs, &error) || error != 0) {
3637c478bd9Sstevel@tonic-gate 		if (error != 0) {
3647c478bd9Sstevel@tonic-gate 		    return (error);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		break;
3677c478bd9Sstevel@tonic-gate 	    }
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	return (0);
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /* return 1 if the slice descriptor is still valid, 0 if not. */
3747c478bd9Sstevel@tonic-gate static int
3757c478bd9Sstevel@tonic-gate desc_ok(descriptor_t *dp)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	/* First verify the media name for removable media */
3787c478bd9Sstevel@tonic-gate 	if (dp->p.disk->removable) {
3797c478bd9Sstevel@tonic-gate 	    char	mname[MAXPATHLEN];
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	    if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
3827c478bd9Sstevel@tonic-gate 		return (0);
3837c478bd9Sstevel@tonic-gate 	    }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	    if (mname[0] == 0) {
3867c478bd9Sstevel@tonic-gate 		return (libdiskmgt_str_eq(dp->secondary_name, NULL));
3877c478bd9Sstevel@tonic-gate 	    } else {
3887c478bd9Sstevel@tonic-gate 		return (libdiskmgt_str_eq(dp->secondary_name, mname));
3897c478bd9Sstevel@tonic-gate 	    }
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/*
3937c478bd9Sstevel@tonic-gate 	 * We could verify the slice is still there, but other code down the
3947c478bd9Sstevel@tonic-gate 	 * line already does these checks (e.g. see get_attrs).
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (1);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /* convert dsk paths to rdsk paths */
4017c478bd9Sstevel@tonic-gate static void
4027c478bd9Sstevel@tonic-gate dsk2rdsk(char *dsk, char *rdsk, int size)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	char	*slashp;
4057c478bd9Sstevel@tonic-gate 	size_t	len;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	(void) strlcpy(rdsk, dsk, size);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	/* make sure there is enough room to add the r to dsk */
4107c478bd9Sstevel@tonic-gate 	len = strlen(dsk);
4117c478bd9Sstevel@tonic-gate 	if (len + 2 > size) {
4127c478bd9Sstevel@tonic-gate 	    return;
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if ((slashp = strstr(rdsk, "/dsk/")) == NULL) {
4167c478bd9Sstevel@tonic-gate 	    /* not dsk, check for floppy */
4177c478bd9Sstevel@tonic-gate 	    slashp = strstr(rdsk, "/diskette");
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (slashp != NULL) {
4217c478bd9Sstevel@tonic-gate 	    char	*endp;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	    endp = rdsk + len;	/* point to terminating 0 */
4247c478bd9Sstevel@tonic-gate 	    /* move the succeeding chars over by one */
4257c478bd9Sstevel@tonic-gate 	    do {
4267c478bd9Sstevel@tonic-gate 		*(endp + 1) = *endp;
4277c478bd9Sstevel@tonic-gate 		endp--;
4287c478bd9Sstevel@tonic-gate 	    } while (endp != slashp);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	    *(endp + 1) = 'r';
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate static int
4357c478bd9Sstevel@tonic-gate get_attrs(descriptor_t *dp, int fd,  nvlist_t *attrs)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	struct dk_minfo	minfo;
4387c478bd9Sstevel@tonic-gate 	int		status;
4397c478bd9Sstevel@tonic-gate 	int		data_format = FMT_UNKNOWN;
4407c478bd9Sstevel@tonic-gate 	int		snum = -1;
4417c478bd9Sstevel@tonic-gate 	int		error;
4427c478bd9Sstevel@tonic-gate 	struct vtoc	vtoc;
4437c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efip;
4447c478bd9Sstevel@tonic-gate 	struct dk_cinfo	dkinfo;
4457c478bd9Sstevel@tonic-gate 	disk_t		*diskp;
4467c478bd9Sstevel@tonic-gate 	char		localpath[MAXPATHLEN];
4477c478bd9Sstevel@tonic-gate 	int		cooked_fd;
4487c478bd9Sstevel@tonic-gate 	struct stat	buf;
4497c478bd9Sstevel@tonic-gate 	int		mntpnt = 0;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (fd < 0) {
4527c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/* First make sure media is inserted and spun up. */
4567c478bd9Sstevel@tonic-gate 	if (!media_read_info(fd, &minfo)) {
4577c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	if ((status = read_vtoc(fd, &vtoc)) >= 0) {
4617c478bd9Sstevel@tonic-gate 	    data_format = FMT_VTOC;
4627c478bd9Sstevel@tonic-gate 	} else if (status == VT_ENOTSUP && efi_alloc_and_read(fd, &efip) >= 0) {
4637c478bd9Sstevel@tonic-gate 	    data_format = FMT_EFI;
4647c478bd9Sstevel@tonic-gate 	    if (nvlist_add_boolean(attrs, DM_EFI) != 0) {
4657c478bd9Sstevel@tonic-gate 		efi_free(efip);
4667c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4677c478bd9Sstevel@tonic-gate 	    }
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (data_format == FMT_UNKNOWN) {
4717c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCINFO, &dkinfo) >= 0) {
4757c478bd9Sstevel@tonic-gate 	    snum = dkinfo.dki_partition;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/* check the slice */
4797c478bd9Sstevel@tonic-gate 	if (data_format == FMT_VTOC) {
4807c478bd9Sstevel@tonic-gate 	    if (snum < 0 || snum >= vtoc.v_nparts ||
4817c478bd9Sstevel@tonic-gate 		vtoc.v_part[snum].p_size == 0) {
4827c478bd9Sstevel@tonic-gate 		return (ENODEV);
4837c478bd9Sstevel@tonic-gate 	    }
4847c478bd9Sstevel@tonic-gate 	} else { /* data_format == FMT_EFI */
4857c478bd9Sstevel@tonic-gate 	    if (snum < 0 || snum >= efip->efi_nparts ||
4867c478bd9Sstevel@tonic-gate 		efip->efi_parts[snum].p_size == 0) {
4877c478bd9Sstevel@tonic-gate 		efi_free(efip);
4887c478bd9Sstevel@tonic-gate 		return (ENODEV);
4897c478bd9Sstevel@tonic-gate 	    }
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/* the slice exists */
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_INDEX, snum) != 0) {
4957c478bd9Sstevel@tonic-gate 	    if (data_format == FMT_EFI) {
4967c478bd9Sstevel@tonic-gate 		efi_free(efip);
4977c478bd9Sstevel@tonic-gate 	    }
4987c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (data_format == FMT_VTOC) {
5027c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint64(attrs, DM_START, vtoc.v_part[snum].p_start)
5037c478bd9Sstevel@tonic-gate 		!= 0) {
5047c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5057c478bd9Sstevel@tonic-gate 	    }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint64(attrs, DM_SIZE, vtoc.v_part[snum].p_size)
5087c478bd9Sstevel@tonic-gate 		!= 0) {
5097c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5107c478bd9Sstevel@tonic-gate 	    }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_TAG, vtoc.v_part[snum].p_tag)
5137c478bd9Sstevel@tonic-gate 		!= 0) {
5147c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5157c478bd9Sstevel@tonic-gate 	    }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_FLAG, vtoc.v_part[snum].p_flag)
5187c478bd9Sstevel@tonic-gate 		!= 0) {
5197c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5207c478bd9Sstevel@tonic-gate 	    }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	} else { /* data_format == FMT_EFI */
5237c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint64(attrs, DM_START,
5247c478bd9Sstevel@tonic-gate 		efip->efi_parts[snum].p_start) != 0) {
5257c478bd9Sstevel@tonic-gate 		efi_free(efip);
5267c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5277c478bd9Sstevel@tonic-gate 	    }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint64(attrs, DM_SIZE, efip->efi_parts[snum].p_size)
5307c478bd9Sstevel@tonic-gate 		!= 0) {
5317c478bd9Sstevel@tonic-gate 		efi_free(efip);
5327c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5337c478bd9Sstevel@tonic-gate 	    }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	    if (efip->efi_parts[snum].p_name[0] != 0) {
5367c478bd9Sstevel@tonic-gate 		char	label[EFI_PART_NAME_LEN + 1];
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		(void) snprintf(label, sizeof (label), "%.*s",
5397c478bd9Sstevel@tonic-gate 		    EFI_PART_NAME_LEN, efip->efi_parts[snum].p_name);
5407c478bd9Sstevel@tonic-gate 		if (nvlist_add_string(attrs, DM_EFI_NAME, label) != 0) {
5417c478bd9Sstevel@tonic-gate 		    efi_free(efip);
5427c478bd9Sstevel@tonic-gate 		    return (ENOMEM);
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 	    }
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (data_format == FMT_EFI) {
5487c478bd9Sstevel@tonic-gate 	    efi_free(efip);
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	if (inuse_mnt(dp->name, attrs, &error)) {
5527c478bd9Sstevel@tonic-gate 	    if (error != 0) {
5537c478bd9Sstevel@tonic-gate 		return (error);
5547c478bd9Sstevel@tonic-gate 	    }
5557c478bd9Sstevel@tonic-gate 	    mntpnt = 1;
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * Some extra attrs for cluster slices.
5607c478bd9Sstevel@tonic-gate 	 *
5617c478bd9Sstevel@tonic-gate 	 * get localname and possible mnt point for localpath
5627c478bd9Sstevel@tonic-gate 	 */
5637c478bd9Sstevel@tonic-gate 	localpath[0] = 0;
5647c478bd9Sstevel@tonic-gate 	diskp = dp->p.disk;
5657c478bd9Sstevel@tonic-gate 	if (diskp->aliases != NULL && diskp->aliases->cluster) {
5667c478bd9Sstevel@tonic-gate 	    slice_t *sp;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	    for (sp = diskp->aliases->orig_paths; sp != NULL; sp = sp->next) {
5697c478bd9Sstevel@tonic-gate 		if (sp->slice_num == -1) {
5707c478bd9Sstevel@tonic-gate 		    /* determine the slice number for this path */
5717c478bd9Sstevel@tonic-gate 		    int			sfd;
5727c478bd9Sstevel@tonic-gate 		    struct dk_cinfo	dkinfo;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		    if ((sfd = open(sp->devpath, O_RDONLY|O_NDELAY)) >= 0) {
5757c478bd9Sstevel@tonic-gate 			if (ioctl(sfd, DKIOCINFO, &dkinfo) >= 0) {
5767c478bd9Sstevel@tonic-gate 			    sp->slice_num = dkinfo.dki_partition;
5777c478bd9Sstevel@tonic-gate 			}
5787c478bd9Sstevel@tonic-gate 			(void) close(sfd);
5797c478bd9Sstevel@tonic-gate 		    }
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		if (sp->slice_num == snum) {
5837c478bd9Sstevel@tonic-gate 		    slice_rdsk2dsk(sp->devpath, localpath, sizeof (localpath));
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		    if (nvlist_add_string(attrs, DM_LOCALNAME, localpath)
5867c478bd9Sstevel@tonic-gate 			!= 0) {
5877c478bd9Sstevel@tonic-gate 			return (ENOMEM);
5887c478bd9Sstevel@tonic-gate 		    }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		    if (mntpnt == 0) {
5917c478bd9Sstevel@tonic-gate 			if (inuse_mnt(localpath, attrs, &error)) {
5927c478bd9Sstevel@tonic-gate 			    if (error != 0) {
5937c478bd9Sstevel@tonic-gate 				return (error);
5947c478bd9Sstevel@tonic-gate 			    }
5957c478bd9Sstevel@tonic-gate 			}
5967c478bd9Sstevel@tonic-gate 		    }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		    break;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 	    }
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if (fstat(fd, &buf) != -1) {
6047c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint64(attrs, DM_DEVT, buf.st_rdev) != 0) {
6057c478bd9Sstevel@tonic-gate 		return (ENOMEM);
6067c478bd9Sstevel@tonic-gate 	    }
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * We need to open the cooked slice (not the raw one) to get the
6117c478bd9Sstevel@tonic-gate 	 * correct devid.  Also see if we need to read the localpath for the
6127c478bd9Sstevel@tonic-gate 	 * cluster disk, since the minor name is unavailable for the did pseudo
6137c478bd9Sstevel@tonic-gate 	 * device.
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	if (localpath[0] != 0) {
6167c478bd9Sstevel@tonic-gate 	    cooked_fd = open(localpath, O_RDONLY|O_NDELAY);
6177c478bd9Sstevel@tonic-gate 	} else {
6187c478bd9Sstevel@tonic-gate 	    cooked_fd = open(dp->name, O_RDONLY|O_NDELAY);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (cooked_fd >= 0) {
6227c478bd9Sstevel@tonic-gate 	    int		no_mem = 0;
6237c478bd9Sstevel@tonic-gate 	    ddi_devid_t	devid;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	    if (devid_get(cooked_fd, &devid) == 0) {
6267c478bd9Sstevel@tonic-gate 		char	*minor;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		if (devid_get_minor_name(cooked_fd, &minor) == 0) {
6297c478bd9Sstevel@tonic-gate 		    char	*devidstr;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 		    if ((devidstr = devid_str_encode(devid, minor)) != 0) {
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 			if (nvlist_add_string(attrs, DM_DEVICEID, devidstr)
6347c478bd9Sstevel@tonic-gate 			    != 0) {
6357c478bd9Sstevel@tonic-gate 			    no_mem = 1;
6367c478bd9Sstevel@tonic-gate 			}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			devid_str_free(devidstr);
6397c478bd9Sstevel@tonic-gate 		    }
6407c478bd9Sstevel@tonic-gate 		    devid_str_free(minor);
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 		devid_free(devid);
6437c478bd9Sstevel@tonic-gate 	    }
6447c478bd9Sstevel@tonic-gate 	    (void) close(cooked_fd);
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	    if (no_mem) {
6477c478bd9Sstevel@tonic-gate 		return (ENOMEM);
6487c478bd9Sstevel@tonic-gate 	    }
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	return (0);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate static descriptor_t **
6557c478bd9Sstevel@tonic-gate get_fixed_assocs(descriptor_t *desc, int *errp)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	int		fd;
6587c478bd9Sstevel@tonic-gate 	int		status;
6597c478bd9Sstevel@tonic-gate 	int		data_format = FMT_UNKNOWN;
6607c478bd9Sstevel@tonic-gate 	int		cnt;
6617c478bd9Sstevel@tonic-gate 	struct vtoc	vtoc;
6627c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efip;
6637c478bd9Sstevel@tonic-gate 	int		pos;
6647c478bd9Sstevel@tonic-gate 	char		*media_name = NULL;
6657c478bd9Sstevel@tonic-gate 	slice_t		*devp;
6667c478bd9Sstevel@tonic-gate 	descriptor_t	**slices;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	if ((fd = drive_open_disk(desc->p.disk, NULL, 0)) < 0) {
6697c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
6707c478bd9Sstevel@tonic-gate 	    return (NULL);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if ((status = read_vtoc(fd, &vtoc)) >= 0) {
6747c478bd9Sstevel@tonic-gate 	    data_format = FMT_VTOC;
6757c478bd9Sstevel@tonic-gate 	} else if (status == VT_ENOTSUP && efi_alloc_and_read(fd, &efip) >= 0) {
6767c478bd9Sstevel@tonic-gate 	    data_format = FMT_EFI;
6777c478bd9Sstevel@tonic-gate 	} else {
6787c478bd9Sstevel@tonic-gate 	    (void) close(fd);
6797c478bd9Sstevel@tonic-gate 	    *errp = 0;
6807c478bd9Sstevel@tonic-gate 	    return (libdiskmgt_empty_desc_array(errp));
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 	(void) close(fd);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/* count the number of slices */
6857c478bd9Sstevel@tonic-gate 	for (cnt = 0, devp = desc->p.disk->aliases->devpaths; devp != NULL;
6867c478bd9Sstevel@tonic-gate 	    devp = devp->next, cnt++);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/* allocate the array for the descriptors */
6897c478bd9Sstevel@tonic-gate 	slices = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
6907c478bd9Sstevel@tonic-gate 	if (slices == NULL) {
6917c478bd9Sstevel@tonic-gate 	    if (data_format == FMT_EFI) {
6927c478bd9Sstevel@tonic-gate 		efi_free(efip);
6937c478bd9Sstevel@tonic-gate 	    }
6947c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
6957c478bd9Sstevel@tonic-gate 	    return (NULL);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/* get the media name from the descriptor */
6997c478bd9Sstevel@tonic-gate 	if (desc->type == DM_MEDIA) {
7007c478bd9Sstevel@tonic-gate 	    media_name = desc->name;
7017c478bd9Sstevel@tonic-gate 	} else {
7027c478bd9Sstevel@tonic-gate 	    /* must be a DM_PARTITION */
7037c478bd9Sstevel@tonic-gate 	    media_name = desc->secondary_name;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	pos = 0;
7077c478bd9Sstevel@tonic-gate 	for (devp = desc->p.disk->aliases->devpaths; devp != NULL;
7087c478bd9Sstevel@tonic-gate 	    devp = devp->next) {
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	    int		slice_num;
7117c478bd9Sstevel@tonic-gate 	    char	devpath[MAXPATHLEN];
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	    slice_num = get_slice_num(devp);
7147c478bd9Sstevel@tonic-gate 	    /* can't get slicenum, so no need to keep trying the drive */
7157c478bd9Sstevel@tonic-gate 	    if (slice_num == -1) {
7167c478bd9Sstevel@tonic-gate 		break;
7177c478bd9Sstevel@tonic-gate 	    }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	    if (data_format == FMT_VTOC) {
7207c478bd9Sstevel@tonic-gate 		if (slice_num >= vtoc.v_nparts ||
7217c478bd9Sstevel@tonic-gate 		    vtoc.v_part[slice_num].p_size == 0) {
7227c478bd9Sstevel@tonic-gate 		    continue;
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 	    } else { /* data_format == FMT_EFI */
7257c478bd9Sstevel@tonic-gate 		if (slice_num >= efip->efi_nparts ||
7267c478bd9Sstevel@tonic-gate 		    efip->efi_parts[slice_num].p_size == 0) {
7277c478bd9Sstevel@tonic-gate 		    continue;
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 	    }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	    slice_rdsk2dsk(devp->devpath, devpath, sizeof (devpath));
7327c478bd9Sstevel@tonic-gate 	    slices[pos] = cache_get_desc(DM_SLICE, desc->p.disk, devpath,
7337c478bd9Sstevel@tonic-gate 		media_name, errp);
7347c478bd9Sstevel@tonic-gate 	    if (*errp != 0) {
7357c478bd9Sstevel@tonic-gate 		cache_free_descriptors(slices);
7367c478bd9Sstevel@tonic-gate 		if (data_format == FMT_EFI) {
7377c478bd9Sstevel@tonic-gate 		    efi_free(efip);
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 		return (NULL);
7407c478bd9Sstevel@tonic-gate 	    }
7417c478bd9Sstevel@tonic-gate 	    pos++;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 	slices[pos] = NULL;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	if (data_format == FMT_EFI) {
7467c478bd9Sstevel@tonic-gate 	    efi_free(efip);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	*errp = 0;
7507c478bd9Sstevel@tonic-gate 	return (slices);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate /*
7547c478bd9Sstevel@tonic-gate  * Called for loaded removable media under volume management control.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate static descriptor_t **
7577c478bd9Sstevel@tonic-gate get_removable_assocs(descriptor_t *desc, char *volm_path, int *errp)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	int		pos;
7607c478bd9Sstevel@tonic-gate 	int		fd;
7617c478bd9Sstevel@tonic-gate 	int		cnt;
7627c478bd9Sstevel@tonic-gate 	struct stat	buf;
7637c478bd9Sstevel@tonic-gate 	descriptor_t	**slices;
7647c478bd9Sstevel@tonic-gate 	char		*media_name = NULL;
7657c478bd9Sstevel@tonic-gate 	char		devpath[MAXPATHLEN];
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/* get the media name from the descriptor */
7687c478bd9Sstevel@tonic-gate 	if (desc->type == DM_MEDIA) {
7697c478bd9Sstevel@tonic-gate 		media_name = desc->name;
7707c478bd9Sstevel@tonic-gate 	} else {
7717c478bd9Sstevel@tonic-gate 		/* must be a DM_PARTITION */
7727c478bd9Sstevel@tonic-gate 		media_name = desc->secondary_name;
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * For removable media under volm control the volm_path will
7777c478bd9Sstevel@tonic-gate 	 * either be a device (if the media is made up of a single slice) or
7787c478bd9Sstevel@tonic-gate 	 * a directory (if the media has multiple slices) with the slices
7797c478bd9Sstevel@tonic-gate 	 * as devices contained in the directory.
7807c478bd9Sstevel@tonic-gate 	 */
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if ((fd = open(volm_path, O_RDONLY|O_NDELAY)) < 0 ||
7837c478bd9Sstevel@tonic-gate 	    fstat(fd, &buf) != 0) {
7847c478bd9Sstevel@tonic-gate 		*errp = ENODEV;
7857c478bd9Sstevel@tonic-gate 		return (NULL);
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	cnt = num_removable_slices(fd, &buf, volm_path);
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	/* allocate the array for the descriptors */
7914bc0a2efScasper 	slices = calloc(cnt + 1, sizeof (descriptor_t *));
7927c478bd9Sstevel@tonic-gate 	if (slices == NULL) {
7937c478bd9Sstevel@tonic-gate 		*errp = ENOMEM;
7947c478bd9Sstevel@tonic-gate 		return (NULL);
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	slice_rdsk2dsk(volm_path, devpath, sizeof (devpath));
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	pos = 0;
8007c478bd9Sstevel@tonic-gate 	*errp = 0;
8014bc0a2efScasper 	if (S_ISCHR(buf.st_mode)) {
8027c478bd9Sstevel@tonic-gate 		struct dk_minfo	minfo;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		/* Make sure media has readable label */
8057c478bd9Sstevel@tonic-gate 		if (media_read_info(fd, &minfo)) {
8067c478bd9Sstevel@tonic-gate 			int		status;
8077c478bd9Sstevel@tonic-gate 			int		data_format = FMT_UNKNOWN;
8087c478bd9Sstevel@tonic-gate 			struct vtoc	vtoc;
8097c478bd9Sstevel@tonic-gate 			struct dk_gpt	*efip;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 			if ((status = read_vtoc(fd, &vtoc)) >= 0) {
8127c478bd9Sstevel@tonic-gate 				data_format = FMT_VTOC;
8137c478bd9Sstevel@tonic-gate 			} else if (status == VT_ENOTSUP &&
8147c478bd9Sstevel@tonic-gate 			    efi_alloc_and_read(fd, &efip) >= 0) {
8157c478bd9Sstevel@tonic-gate 				data_format = FMT_EFI;
8167c478bd9Sstevel@tonic-gate 			}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 			if (data_format != FMT_UNKNOWN) {
8197c478bd9Sstevel@tonic-gate 			    /* has a readable label */
8204bc0a2efScasper 				slices[pos++] =
8214bc0a2efScasper 				    cache_get_desc(DM_SLICE, desc->p.disk,
8227c478bd9Sstevel@tonic-gate 				    devpath, media_name, errp);
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 		}
8254bc0a2efScasper 		(void) close(fd);
8264bc0a2efScasper 	} else if (S_ISDIR(buf.st_mode)) {
8277c478bd9Sstevel@tonic-gate 		DIR		*dirp;
8284bc0a2efScasper 		struct dirent	*dentp;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		/* rewind, num_removable_slices already traversed */
8317c478bd9Sstevel@tonic-gate 		(void) lseek(fd, 0, SEEK_SET);
8327c478bd9Sstevel@tonic-gate 
8334bc0a2efScasper 		if ((dirp = fdopendir(fd)) == NULL) {
8344bc0a2efScasper 			*errp = errno;
8354bc0a2efScasper 			(void) close(fd);
8364bc0a2efScasper 			return (NULL);
8374bc0a2efScasper 		}
8387c478bd9Sstevel@tonic-gate 
8394bc0a2efScasper 		while ((dentp = readdir(dirp)) != NULL) {
8407c478bd9Sstevel@tonic-gate 			int	dfd;
8417c478bd9Sstevel@tonic-gate 			int	is_dev = 0;
8427c478bd9Sstevel@tonic-gate 			char	slice_path[MAXPATHLEN];
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 			if (libdiskmgt_str_eq(".", dentp->d_name) ||
8457c478bd9Sstevel@tonic-gate 			    libdiskmgt_str_eq("..", dentp->d_name)) {
8467c478bd9Sstevel@tonic-gate 				continue;
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 			(void) snprintf(slice_path, sizeof (slice_path),
8507c478bd9Sstevel@tonic-gate 			    "%s/%s", devpath, dentp->d_name);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 			if ((dfd = open(slice_path, O_RDONLY|O_NDELAY)) >= 0) {
8537c478bd9Sstevel@tonic-gate 				struct stat	buf;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 				if (fstat(dfd, &buf) == 0 &&
8564bc0a2efScasper 				    S_ISCHR(buf.st_mode)) {
8577c478bd9Sstevel@tonic-gate 					is_dev = 1;
8587c478bd9Sstevel@tonic-gate 				}
8597c478bd9Sstevel@tonic-gate 				(void) close(dfd);
8607c478bd9Sstevel@tonic-gate 			}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 			if (!is_dev) {
8637c478bd9Sstevel@tonic-gate 				continue;
8647c478bd9Sstevel@tonic-gate 			}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 			slices[pos++] = cache_get_desc(DM_SLICE, desc->p.disk,
8677c478bd9Sstevel@tonic-gate 			    slice_path, media_name, errp);
8687c478bd9Sstevel@tonic-gate 			if (*errp != 0) {
8697c478bd9Sstevel@tonic-gate 				break;
8707c478bd9Sstevel@tonic-gate 			}
8717c478bd9Sstevel@tonic-gate 		}
8724bc0a2efScasper 		(void) closedir(dirp);
8734bc0a2efScasper 	} else {
8747c478bd9Sstevel@tonic-gate 		(void) close(fd);
8754bc0a2efScasper 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	slices[pos] = NULL;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
8807c478bd9Sstevel@tonic-gate 		cache_free_descriptors(slices);
8817c478bd9Sstevel@tonic-gate 		return (NULL);
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	return (slices);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate static int
8887c478bd9Sstevel@tonic-gate get_slice_num(slice_t *devp)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	/* check if we already determined the devpath slice number */
8917c478bd9Sstevel@tonic-gate 	if (devp->slice_num == -1) {
8927c478bd9Sstevel@tonic-gate 	    int		fd;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	    if ((fd = open(devp->devpath, O_RDONLY|O_NDELAY)) >= 0) {
8957c478bd9Sstevel@tonic-gate 		struct dk_cinfo	dkinfo;
8967c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCINFO, &dkinfo) >= 0) {
8977c478bd9Sstevel@tonic-gate 		    devp->slice_num = dkinfo.dki_partition;
8987c478bd9Sstevel@tonic-gate 		}
8997c478bd9Sstevel@tonic-gate 		(void) close(fd);
9007c478bd9Sstevel@tonic-gate 	    }
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	return (devp->slice_num);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate static int
9077c478bd9Sstevel@tonic-gate make_fixed_descriptors(disk_t *dp)
9087c478bd9Sstevel@tonic-gate {
9097c478bd9Sstevel@tonic-gate 	int		error = 0;
9107c478bd9Sstevel@tonic-gate 	alias_t		*ap;
9117c478bd9Sstevel@tonic-gate 	slice_t		*devp;
9127c478bd9Sstevel@tonic-gate 	char		mname[MAXPATHLEN];
9137c478bd9Sstevel@tonic-gate 	int		data_format = FMT_UNKNOWN;
9147c478bd9Sstevel@tonic-gate 	struct vtoc	vtoc;
9157c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efip;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/* Just check the first drive name. */
9187c478bd9Sstevel@tonic-gate 	if ((ap = dp->aliases) == NULL) {
9197c478bd9Sstevel@tonic-gate 	    return (0);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	mname[0] = 0;
9237c478bd9Sstevel@tonic-gate 	(void) media_read_name(dp, mname, sizeof (mname));
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	for (devp = ap->devpaths; devp != NULL; devp = devp->next) {
9267c478bd9Sstevel@tonic-gate 	    int		slice_num;
9277c478bd9Sstevel@tonic-gate 	    char	devpath[MAXPATHLEN];
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	    slice_num = get_slice_num(devp);
9307c478bd9Sstevel@tonic-gate 	    /* can't get slicenum, so no need to keep trying the drive */
9317c478bd9Sstevel@tonic-gate 	    if (slice_num == -1) {
9327c478bd9Sstevel@tonic-gate 		break;
9337c478bd9Sstevel@tonic-gate 	    }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	    if (data_format == FMT_UNKNOWN) {
9367c478bd9Sstevel@tonic-gate 		int	fd;
9377c478bd9Sstevel@tonic-gate 		int	status;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		if ((fd = drive_open_disk(dp, NULL, 0)) >= 0) {
9407c478bd9Sstevel@tonic-gate 		    if ((status = read_vtoc(fd, &vtoc)) >= 0) {
9417c478bd9Sstevel@tonic-gate 			data_format = FMT_VTOC;
9427c478bd9Sstevel@tonic-gate 		    } else if (status == VT_ENOTSUP &&
9437c478bd9Sstevel@tonic-gate 			efi_alloc_and_read(fd, &efip) >= 0) {
9447c478bd9Sstevel@tonic-gate 			data_format = FMT_EFI;
9457c478bd9Sstevel@tonic-gate 		    }
9467c478bd9Sstevel@tonic-gate 		    (void) close(fd);
9477c478bd9Sstevel@tonic-gate 		}
9487c478bd9Sstevel@tonic-gate 	    }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	    /* can't get slice data, so no need to keep trying the drive */
9517c478bd9Sstevel@tonic-gate 	    if (data_format == FMT_UNKNOWN) {
9527c478bd9Sstevel@tonic-gate 		break;
9537c478bd9Sstevel@tonic-gate 	    }
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	    if (data_format == FMT_VTOC) {
9567c478bd9Sstevel@tonic-gate 		if (slice_num >= vtoc.v_nparts ||
9577c478bd9Sstevel@tonic-gate 		    vtoc.v_part[slice_num].p_size == 0) {
9587c478bd9Sstevel@tonic-gate 		    continue;
9597c478bd9Sstevel@tonic-gate 		}
9607c478bd9Sstevel@tonic-gate 	    } else { /* data_format == FMT_EFI */
9617c478bd9Sstevel@tonic-gate 		if (slice_num >= efip->efi_nparts ||
9627c478bd9Sstevel@tonic-gate 		    efip->efi_parts[slice_num].p_size == 0) {
9637c478bd9Sstevel@tonic-gate 		    continue;
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 	    }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	    slice_rdsk2dsk(devp->devpath, devpath, sizeof (devpath));
9687c478bd9Sstevel@tonic-gate 	    cache_load_desc(DM_SLICE, dp, devpath, mname, &error);
9697c478bd9Sstevel@tonic-gate 	    if (error != 0) {
9707c478bd9Sstevel@tonic-gate 		break;
9717c478bd9Sstevel@tonic-gate 	    }
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	if (data_format == FMT_EFI) {
9757c478bd9Sstevel@tonic-gate 	    efi_free(efip);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	return (error);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate /*
9827c478bd9Sstevel@tonic-gate  * For removable media under volm control we have to do some special handling.
9837c478bd9Sstevel@tonic-gate  * We don't use the vtoc and /dev/dsk devpaths, since the slices are named
9847c478bd9Sstevel@tonic-gate  * under the /vol fs.
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate static int
9877c478bd9Sstevel@tonic-gate make_removable_descriptors(disk_t *dp)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	char		volm_path[MAXPATHLEN];
9907c478bd9Sstevel@tonic-gate 	int		error;
9917c478bd9Sstevel@tonic-gate 	int		fd;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	/*
9947c478bd9Sstevel@tonic-gate 	 * If this removable drive is not under volm control, just use
9957c478bd9Sstevel@tonic-gate 	 * normal handling.
9967c478bd9Sstevel@tonic-gate 	 */
9977c478bd9Sstevel@tonic-gate 	if (!media_get_volm_path(dp, volm_path, sizeof (volm_path))) {
9987c478bd9Sstevel@tonic-gate 	    return (make_fixed_descriptors(dp));
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (volm_path[0] == 0) {
10027c478bd9Sstevel@tonic-gate 	    /* no media */
10037c478bd9Sstevel@tonic-gate 	    return (0);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	/*
10077c478bd9Sstevel@tonic-gate 	 * For removable media under volm control the rmmedia_devapth will
10087c478bd9Sstevel@tonic-gate 	 * either be a device (if the media is made up of a single slice) or
10097c478bd9Sstevel@tonic-gate 	 * a directory (if the media has multiple slices) with the slices
10107c478bd9Sstevel@tonic-gate 	 * as devices contained in the directory.
10117c478bd9Sstevel@tonic-gate 	 */
10127c478bd9Sstevel@tonic-gate 	error = 0;
10137c478bd9Sstevel@tonic-gate 	if ((fd = open(volm_path, O_RDONLY|O_NDELAY)) >= 0) {
10147c478bd9Sstevel@tonic-gate 	    struct stat	buf;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	    if (fstat(fd, &buf) == 0) {
10174bc0a2efScasper 		if (S_ISCHR(buf.st_mode)) {
10187c478bd9Sstevel@tonic-gate 		    int			status;
10197c478bd9Sstevel@tonic-gate 		    int			data_format = FMT_UNKNOWN;
10207c478bd9Sstevel@tonic-gate 		    struct dk_minfo	minfo;
10217c478bd9Sstevel@tonic-gate 		    int			error;
10227c478bd9Sstevel@tonic-gate 		    struct vtoc		vtoc;
10237c478bd9Sstevel@tonic-gate 		    struct dk_gpt	*efip;
10247c478bd9Sstevel@tonic-gate 		    char		devpath[MAXPATHLEN];
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 		    /* Make sure media has readable label */
10277c478bd9Sstevel@tonic-gate 		    if (!media_read_info(fd, &minfo)) {
10287c478bd9Sstevel@tonic-gate 			/* no media */
10297c478bd9Sstevel@tonic-gate 			return (0);
10307c478bd9Sstevel@tonic-gate 		    }
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		    if ((status = read_vtoc(fd, &vtoc)) >= 0) {
10337c478bd9Sstevel@tonic-gate 			data_format = FMT_VTOC;
10347c478bd9Sstevel@tonic-gate 		    } else if (status == VT_ENOTSUP &&
10357c478bd9Sstevel@tonic-gate 			efi_alloc_and_read(fd, &efip) >= 0) {
10367c478bd9Sstevel@tonic-gate 			data_format = FMT_EFI;
10377c478bd9Sstevel@tonic-gate 		    }
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		    if (data_format == FMT_UNKNOWN) {
10407c478bd9Sstevel@tonic-gate 			/* no readable label */
10417c478bd9Sstevel@tonic-gate 			return (0);
10427c478bd9Sstevel@tonic-gate 		    }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		    slice_rdsk2dsk(volm_path, devpath, sizeof (devpath));
10457c478bd9Sstevel@tonic-gate 		    /* The media name is the volm_path in this case. */
10467c478bd9Sstevel@tonic-gate 		    cache_load_desc(DM_SLICE, dp, devpath, volm_path, &error);
10477c478bd9Sstevel@tonic-gate 
10484bc0a2efScasper 		} else if (S_ISDIR(buf.st_mode)) {
10497c478bd9Sstevel@tonic-gate 		    /* each device file in the dir represents a slice */
10507c478bd9Sstevel@tonic-gate 		    error = make_volm_dir_descriptors(dp, fd, volm_path);
10517c478bd9Sstevel@tonic-gate 		}
10527c478bd9Sstevel@tonic-gate 	    }
10537c478bd9Sstevel@tonic-gate 	    (void) close(fd);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	return (error);
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate /*
10607c478bd9Sstevel@tonic-gate  * This handles removable media with slices under volume management control.
10617c478bd9Sstevel@tonic-gate  * In this case we have a dir which is the media name and each slice on the
10627c478bd9Sstevel@tonic-gate  * media is a device file in this dir.
10637c478bd9Sstevel@tonic-gate  */
10647c478bd9Sstevel@tonic-gate static int
10657c478bd9Sstevel@tonic-gate make_volm_dir_descriptors(disk_t *dp, int dirfd, char *volm_path)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	int		error;
10687c478bd9Sstevel@tonic-gate 	DIR		*dirp;
10697c478bd9Sstevel@tonic-gate 	struct dirent	*dentp;
10707c478bd9Sstevel@tonic-gate 	char		devpath[MAXPATHLEN];
10717c478bd9Sstevel@tonic-gate 
10724bc0a2efScasper 	dirfd = dup(dirfd);
10734bc0a2efScasper 	if (dirfd < 0)
10744bc0a2efScasper 		return (0);
10757c478bd9Sstevel@tonic-gate 	if ((dirp = fdopendir(dirfd)) == NULL) {
10764bc0a2efScasper 		(void) close(dirfd);
10777c478bd9Sstevel@tonic-gate 		return (0);
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	slice_rdsk2dsk(volm_path, devpath, sizeof (devpath));
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	error = 0;
10834bc0a2efScasper 	while ((dentp = readdir(dirp)) != NULL) {
10847c478bd9Sstevel@tonic-gate 		int	fd;
10857c478bd9Sstevel@tonic-gate 		char	slice_path[MAXPATHLEN];
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(".", dentp->d_name) ||
10887c478bd9Sstevel@tonic-gate 		    libdiskmgt_str_eq("..", dentp->d_name)) {
10897c478bd9Sstevel@tonic-gate 			continue;
10907c478bd9Sstevel@tonic-gate 		}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		(void) snprintf(slice_path, sizeof (slice_path), "%s/%s",
10937c478bd9Sstevel@tonic-gate 		    devpath, dentp->d_name);
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		if ((fd = open(slice_path, O_RDONLY|O_NDELAY)) >= 0) {
10967c478bd9Sstevel@tonic-gate 			struct stat	buf;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 			/* The media name is the volm_path in this case. */
10994bc0a2efScasper 			if (fstat(fd, &buf) == 0 && S_ISCHR(buf.st_mode)) {
11004bc0a2efScasper 				cache_load_desc(DM_SLICE, dp, slice_path,
11014bc0a2efScasper 				    volm_path, &error);
11027c478bd9Sstevel@tonic-gate 				if (error != 0) {
11037c478bd9Sstevel@tonic-gate 					(void) close(fd);
11047c478bd9Sstevel@tonic-gate 					break;
11057c478bd9Sstevel@tonic-gate 				}
11067c478bd9Sstevel@tonic-gate 			}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 			(void) close(fd);
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 	}
11114bc0a2efScasper 	(void) closedir(dirp);
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	return (error);
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate /*
11177c478bd9Sstevel@tonic-gate  * Just look for the name on the devpaths we have cached. Return 1 if we
11187c478bd9Sstevel@tonic-gate  * find the name and the size of that slice is non-zero.
11197c478bd9Sstevel@tonic-gate  */
11207c478bd9Sstevel@tonic-gate static int
11217c478bd9Sstevel@tonic-gate match_fixed_name(disk_t *diskp, char *name, int *errp)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	slice_t		*dp = NULL;
11247c478bd9Sstevel@tonic-gate 	alias_t		*ap;
11257c478bd9Sstevel@tonic-gate 	int		slice_num;
11267c478bd9Sstevel@tonic-gate 	int		fd;
11277c478bd9Sstevel@tonic-gate 	int		status;
11287c478bd9Sstevel@tonic-gate 	int		data_format = FMT_UNKNOWN;
11297c478bd9Sstevel@tonic-gate 	struct vtoc	vtoc;
11307c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efip;
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	ap = diskp->aliases;
11337c478bd9Sstevel@tonic-gate 	while (ap != NULL) {
11347c478bd9Sstevel@tonic-gate 	    slice_t	*devp;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	    devp = ap->devpaths;
11377c478bd9Sstevel@tonic-gate 	    while (devp != NULL) {
11387c478bd9Sstevel@tonic-gate 		char	path[MAXPATHLEN];
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 		slice_rdsk2dsk(devp->devpath, path, sizeof (path));
11417c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(path, name)) {
11427c478bd9Sstevel@tonic-gate 		    /* found it */
11437c478bd9Sstevel@tonic-gate 		    dp = devp;
11447c478bd9Sstevel@tonic-gate 		    break;
11457c478bd9Sstevel@tonic-gate 		}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 		devp = devp->next;
11487c478bd9Sstevel@tonic-gate 	    }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	    if (dp != NULL) {
11517c478bd9Sstevel@tonic-gate 		break;
11527c478bd9Sstevel@tonic-gate 	    }
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	    ap = ap->next;
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
11587c478bd9Sstevel@tonic-gate 	    *errp = 0;
11597c478bd9Sstevel@tonic-gate 	    return (0);
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	/*
11637c478bd9Sstevel@tonic-gate 	 * If we found a match on the name we now have to check that this
11647c478bd9Sstevel@tonic-gate 	 * slice really exists (non-0 size).
11657c478bd9Sstevel@tonic-gate 	 */
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	slice_num = get_slice_num(dp);
11687c478bd9Sstevel@tonic-gate 	/* can't get slicenum, so no slice */
11697c478bd9Sstevel@tonic-gate 	if (slice_num == -1) {
11707c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
11717c478bd9Sstevel@tonic-gate 	    return (1);
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	if ((fd = drive_open_disk(diskp, NULL, 0)) < 0) {
11757c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
11767c478bd9Sstevel@tonic-gate 	    return (1);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if ((status = read_vtoc(fd, &vtoc)) >= 0) {
11807c478bd9Sstevel@tonic-gate 	    data_format = FMT_VTOC;
11817c478bd9Sstevel@tonic-gate 	} else if (status == VT_ENOTSUP && efi_alloc_and_read(fd, &efip) >= 0) {
11827c478bd9Sstevel@tonic-gate 	    data_format = FMT_EFI;
11837c478bd9Sstevel@tonic-gate 	} else {
11847c478bd9Sstevel@tonic-gate 	    (void) close(fd);
11857c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
11867c478bd9Sstevel@tonic-gate 	    return (1);
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 	(void) close(fd);
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	if (data_format == FMT_VTOC) {
11917c478bd9Sstevel@tonic-gate 	    if (slice_num < vtoc.v_nparts &&
11927c478bd9Sstevel@tonic-gate 		vtoc.v_part[slice_num].p_size > 0) {
11937c478bd9Sstevel@tonic-gate 		*errp = 0;
11947c478bd9Sstevel@tonic-gate 		return (1);
11957c478bd9Sstevel@tonic-gate 	    }
11967c478bd9Sstevel@tonic-gate 	} else { /* data_format == FMT_EFI */
11977c478bd9Sstevel@tonic-gate 	    if (slice_num < efip->efi_nparts &&
11987c478bd9Sstevel@tonic-gate 		efip->efi_parts[slice_num].p_size > 0) {
11997c478bd9Sstevel@tonic-gate 		efi_free(efip);
12007c478bd9Sstevel@tonic-gate 		*errp = 0;
12017c478bd9Sstevel@tonic-gate 		return (1);
12027c478bd9Sstevel@tonic-gate 	    }
12037c478bd9Sstevel@tonic-gate 	    efi_free(efip);
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	*errp = ENODEV;
12077c478bd9Sstevel@tonic-gate 	return (1);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate static int
12117c478bd9Sstevel@tonic-gate match_removable_name(disk_t *diskp, char *name, int *errp)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate 	char		volm_path[MAXPATHLEN];
12147c478bd9Sstevel@tonic-gate 	int		found;
12157c478bd9Sstevel@tonic-gate 	int		fd;
12167c478bd9Sstevel@tonic-gate 	struct stat	buf;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	/*
12197c478bd9Sstevel@tonic-gate 	 * If this removable drive is not under volm control, just use
12207c478bd9Sstevel@tonic-gate 	 * normal handling.
12217c478bd9Sstevel@tonic-gate 	 */
12227c478bd9Sstevel@tonic-gate 	if (!media_get_volm_path(diskp, volm_path, sizeof (volm_path))) {
12237c478bd9Sstevel@tonic-gate 	    return (match_fixed_name(diskp, name, errp));
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	if (volm_path[0] == 0) {
12277c478bd9Sstevel@tonic-gate 	    /* no media */
12287c478bd9Sstevel@tonic-gate 	    *errp = 0;
12297c478bd9Sstevel@tonic-gate 	    return (0);
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	/*
12337c478bd9Sstevel@tonic-gate 	 * For removable media under volm control the rmmedia_devapth will
12347c478bd9Sstevel@tonic-gate 	 * either be a device (if the media is made up of a single slice) or
12357c478bd9Sstevel@tonic-gate 	 * a directory (if the media has multiple slices) with the slices
12367c478bd9Sstevel@tonic-gate 	 * as devices contained in the directory.
12377c478bd9Sstevel@tonic-gate 	 */
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	*errp = 0;
12407c478bd9Sstevel@tonic-gate 
12414bc0a2efScasper 	if ((fd = open(volm_path, O_RDONLY|O_NDELAY)) == -1 ||
12424bc0a2efScasper 	    fstat(fd, &buf) != 0) {
12434bc0a2efScasper 		return (0);
12444bc0a2efScasper 	}
12457c478bd9Sstevel@tonic-gate 
12464bc0a2efScasper 	found = 0;
12474bc0a2efScasper 
12484bc0a2efScasper 	if (S_ISCHR(buf.st_mode)) {
12497c478bd9Sstevel@tonic-gate 		char	devpath[MAXPATHLEN];
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 		slice_rdsk2dsk(volm_path, devpath, sizeof (devpath));
12527c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(name, devpath)) {
12537c478bd9Sstevel@tonic-gate 			found = 1;
12547c478bd9Sstevel@tonic-gate 		}
12554bc0a2efScasper 		(void) close(fd);
12564bc0a2efScasper 		return (found);
12574bc0a2efScasper 	} else if (S_ISDIR(buf.st_mode)) {
12587c478bd9Sstevel@tonic-gate 		/* each device file in the dir represents a slice */
12597c478bd9Sstevel@tonic-gate 		DIR		*dirp;
12607c478bd9Sstevel@tonic-gate 		struct dirent	*dentp;
12617c478bd9Sstevel@tonic-gate 		char		devpath[MAXPATHLEN];
12627c478bd9Sstevel@tonic-gate 
12634bc0a2efScasper 		if ((dirp = fdopendir(fd)) == NULL) {
12644bc0a2efScasper 			(void) close(fd);
12654bc0a2efScasper 			return (0);
12664bc0a2efScasper 		}
12674bc0a2efScasper 
12687c478bd9Sstevel@tonic-gate 		slice_rdsk2dsk(volm_path, devpath, sizeof (devpath));
12697c478bd9Sstevel@tonic-gate 
12704bc0a2efScasper 		while ((dentp = readdir(dirp)) != NULL) {
12717c478bd9Sstevel@tonic-gate 			char	slice_path[MAXPATHLEN];
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 			if (libdiskmgt_str_eq(".", dentp->d_name) ||
12747c478bd9Sstevel@tonic-gate 			    libdiskmgt_str_eq("..", dentp->d_name)) {
12757c478bd9Sstevel@tonic-gate 				continue;
12767c478bd9Sstevel@tonic-gate 			}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 			(void) snprintf(slice_path, sizeof (slice_path),
12797c478bd9Sstevel@tonic-gate 			    "%s/%s", devpath, dentp->d_name);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 			if (libdiskmgt_str_eq(name, slice_path)) {
12827c478bd9Sstevel@tonic-gate 				/* found name, check device */
12837c478bd9Sstevel@tonic-gate 				int	dfd;
12847c478bd9Sstevel@tonic-gate 				int	is_dev = 0;
12857c478bd9Sstevel@tonic-gate 
12864bc0a2efScasper 				dfd = open(slice_path, O_RDONLY|O_NDELAY);
12874bc0a2efScasper 				if (dfd >= 0) {
12887c478bd9Sstevel@tonic-gate 					struct stat	buf;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 					if (fstat(dfd, &buf) == 0 &&
12914bc0a2efScasper 					    S_ISCHR(buf.st_mode)) {
12927c478bd9Sstevel@tonic-gate 						is_dev = 1;
12937c478bd9Sstevel@tonic-gate 					}
12947c478bd9Sstevel@tonic-gate 					(void) close(dfd);
12957c478bd9Sstevel@tonic-gate 				}
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 				/* we found the name */
12987c478bd9Sstevel@tonic-gate 				found = 1;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 				if (!is_dev) {
13017c478bd9Sstevel@tonic-gate 					*errp = ENODEV;
13027c478bd9Sstevel@tonic-gate 				}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 				break;
13057c478bd9Sstevel@tonic-gate 			}
13067c478bd9Sstevel@tonic-gate 		}
13074bc0a2efScasper 		(void) closedir(dirp);
13084bc0a2efScasper 	} else {
13097c478bd9Sstevel@tonic-gate 		(void) close(fd);
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	return (found);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate static int
13167c478bd9Sstevel@tonic-gate num_removable_slices(int fd, struct stat *bufp, char *volm_path)
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate 	int cnt = 0;
13197c478bd9Sstevel@tonic-gate 
13204bc0a2efScasper 	if (S_ISCHR(bufp->st_mode))
13214bc0a2efScasper 		return (1);
13227c478bd9Sstevel@tonic-gate 
13234bc0a2efScasper 	if (S_ISDIR(bufp->st_mode)) {
13247c478bd9Sstevel@tonic-gate 		/* each device file in the dir represents a slice */
13257c478bd9Sstevel@tonic-gate 		DIR		*dirp;
13267c478bd9Sstevel@tonic-gate 		struct dirent	*dentp;
13277c478bd9Sstevel@tonic-gate 		char		devpath[MAXPATHLEN];
13287c478bd9Sstevel@tonic-gate 
13294bc0a2efScasper 		fd = dup(fd);
13304bc0a2efScasper 
13314bc0a2efScasper 		if (fd < 0)
13324bc0a2efScasper 			return (0);
13334bc0a2efScasper 
13344bc0a2efScasper 		if ((dirp = fdopendir(fd)) == NULL) {
13354bc0a2efScasper 			(void) close(fd);
13364bc0a2efScasper 			return (0);
13374bc0a2efScasper 		}
13384bc0a2efScasper 
13397c478bd9Sstevel@tonic-gate 		slice_rdsk2dsk(volm_path, devpath, sizeof (devpath));
13407c478bd9Sstevel@tonic-gate 
13414bc0a2efScasper 		while ((dentp = readdir(dirp)) != NULL) {
13427c478bd9Sstevel@tonic-gate 			int	dfd;
13437c478bd9Sstevel@tonic-gate 			char	slice_path[MAXPATHLEN];
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 			if (libdiskmgt_str_eq(".", dentp->d_name) ||
13467c478bd9Sstevel@tonic-gate 			    libdiskmgt_str_eq("..", dentp->d_name)) {
13477c478bd9Sstevel@tonic-gate 				continue;
13487c478bd9Sstevel@tonic-gate 			}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 			(void) snprintf(slice_path, sizeof (slice_path),
13517c478bd9Sstevel@tonic-gate 			    "%s/%s", devpath, dentp->d_name);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 			if ((dfd = open(slice_path, O_RDONLY|O_NDELAY)) >= 0) {
13547c478bd9Sstevel@tonic-gate 				struct stat	buf;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 				if (fstat(dfd, &buf) == 0 &&
13574bc0a2efScasper 				    S_ISCHR(buf.st_mode)) {
13587c478bd9Sstevel@tonic-gate 					cnt++;
13597c478bd9Sstevel@tonic-gate 				}
13607c478bd9Sstevel@tonic-gate 				(void) close(dfd);
13617c478bd9Sstevel@tonic-gate 			}
13627c478bd9Sstevel@tonic-gate 		}
13634bc0a2efScasper 		(void) closedir(dirp);
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 	return (cnt);
13667c478bd9Sstevel@tonic-gate }
1367