xref: /titanic_53/usr/src/lib/libdiskmgt/common/partition.c (revision 3e1bd7a2aaeb6188caef90679b98088cfef1edc6)
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*3e1bd7a2Ssjelinek  * Copyright 2005 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 <sys/sunddi.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <dirent.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
427c478bd9Sstevel@tonic-gate #include "disks_private.h"
437c478bd9Sstevel@tonic-gate #include "partition.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifdef sparc
467c478bd9Sstevel@tonic-gate #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
477c478bd9Sstevel@tonic-gate #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
487c478bd9Sstevel@tonic-gate 			    (les((unsigned)((val)&0xffff0000)>>16)))
497c478bd9Sstevel@tonic-gate #else
507c478bd9Sstevel@tonic-gate #define	les(val)	(val)
517c478bd9Sstevel@tonic-gate #define	lel(val)	(val)
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	ISIZE		FD_NUMPART * sizeof (struct ipart)
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int	desc_ok(descriptor_t *dp);
577c478bd9Sstevel@tonic-gate static int	get_attrs(descriptor_t *dp, struct ipart *iparts,
587c478bd9Sstevel@tonic-gate 		    nvlist_t *attrs);
597c478bd9Sstevel@tonic-gate static int	get_parts(disk_t *disk, struct ipart *iparts, char *opath,
607c478bd9Sstevel@tonic-gate 		    int opath_len);
617c478bd9Sstevel@tonic-gate static int	open_disk(disk_t *diskp, char *opath, int len);
627c478bd9Sstevel@tonic-gate static int	has_slices(descriptor_t *desc, int *errp);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate descriptor_t **
657c478bd9Sstevel@tonic-gate partition_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type,
667c478bd9Sstevel@tonic-gate     int *errp)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	if (!desc_ok(desc)) {
697c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
707c478bd9Sstevel@tonic-gate 	    return (NULL);
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	switch (type) {
747c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
757c478bd9Sstevel@tonic-gate 	    return (media_get_assocs(desc, errp));
767c478bd9Sstevel@tonic-gate 	case DM_SLICE:
777c478bd9Sstevel@tonic-gate 	    if (!has_slices(desc, errp)) {
787c478bd9Sstevel@tonic-gate 		if (*errp != 0) {
797c478bd9Sstevel@tonic-gate 		    return (NULL);
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 		return (libdiskmgt_empty_desc_array(errp));
827c478bd9Sstevel@tonic-gate 	    }
837c478bd9Sstevel@tonic-gate 	    return (slice_get_assocs(desc, errp));
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
877c478bd9Sstevel@tonic-gate 	return (NULL);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * This is called by media/slice to get the associated partitions.
927c478bd9Sstevel@tonic-gate  * For a media desc. we just get all the partitions, but for a slice desc.
937c478bd9Sstevel@tonic-gate  * we just get the active solaris partition.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate descriptor_t **
967c478bd9Sstevel@tonic-gate partition_get_assocs(descriptor_t *desc, int *errp)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	descriptor_t	**partitions;
997c478bd9Sstevel@tonic-gate 	int		pos;
1007c478bd9Sstevel@tonic-gate 	int		i;
1017c478bd9Sstevel@tonic-gate 	struct ipart	iparts[FD_NUMPART];
1027c478bd9Sstevel@tonic-gate 	char		pname[MAXPATHLEN];
1037c478bd9Sstevel@tonic-gate 	int		conv_flag = 0;
104*3e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64)
105*3e1bd7a2Ssjelinek 	int		len;
106*3e1bd7a2Ssjelinek #endif
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (get_parts(desc->p.disk, iparts, pname, sizeof (pname)) != 0) {
1097c478bd9Sstevel@tonic-gate 	    return (libdiskmgt_empty_desc_array(errp));
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/* allocate the array for the descriptors */
1137c478bd9Sstevel@tonic-gate 	partitions = (descriptor_t **)calloc(FD_NUMPART + 1,
1147c478bd9Sstevel@tonic-gate 	    sizeof (descriptor_t *));
1157c478bd9Sstevel@tonic-gate 	if (partitions == NULL) {
1167c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
1177c478bd9Sstevel@tonic-gate 	    return (NULL);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
120*3e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64)
1217c478bd9Sstevel@tonic-gate 	    /* convert part. name (e.g. c0d0p0) */
1227c478bd9Sstevel@tonic-gate 	    len = strlen(pname);
1237c478bd9Sstevel@tonic-gate 	    if (len > 1 && *(pname + (len - 2)) == 'p') {
1247c478bd9Sstevel@tonic-gate 		conv_flag = 1;
1257c478bd9Sstevel@tonic-gate 		*(pname + (len - 1)) = 0;
1267c478bd9Sstevel@tonic-gate 	    }
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * If this is a slice desc. we need the first active solaris partition
1317c478bd9Sstevel@tonic-gate 	 * and if there isn't one then we need the first solaris partition.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	if (desc->type == DM_SLICE) {
1347c478bd9Sstevel@tonic-gate 	    for (i = 0; i < FD_NUMPART; i++) {
1357c478bd9Sstevel@tonic-gate 		if (iparts[i].bootid == ACTIVE &&
1367c478bd9Sstevel@tonic-gate 		    (iparts[i].systid == SUNIXOS ||
1377c478bd9Sstevel@tonic-gate 		    iparts[i].systid == SUNIXOS2)) {
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 	    }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	    /* no active solaris part., try to get the first solaris part. */
1437c478bd9Sstevel@tonic-gate 	    if (i >= FD_NUMPART) {
1447c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
1457c478bd9Sstevel@tonic-gate 		    if (iparts[i].systid == SUNIXOS ||
1467c478bd9Sstevel@tonic-gate 			iparts[i].systid == SUNIXOS2) {
1477c478bd9Sstevel@tonic-gate 			    break;
1487c478bd9Sstevel@tonic-gate 		    }
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 	    }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	    if (i < FD_NUMPART) {
1537c478bd9Sstevel@tonic-gate 		/* we found a solaris partition to use */
1547c478bd9Sstevel@tonic-gate 		char	part_name[MAXPATHLEN];
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		if (conv_flag) {
1577c478bd9Sstevel@tonic-gate 		    /* convert part. name (e.g. c0d0p0) */
1587c478bd9Sstevel@tonic-gate 		    (void) snprintf(part_name, sizeof (part_name), "%s%d",
1597c478bd9Sstevel@tonic-gate 			pname, i);
1607c478bd9Sstevel@tonic-gate 		} else {
1617c478bd9Sstevel@tonic-gate 		    (void) snprintf(part_name, sizeof (part_name), "%d", i);
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		/* the media name comes from the slice desc. */
1657c478bd9Sstevel@tonic-gate 		partitions[0] = cache_get_desc(DM_PARTITION, desc->p.disk,
1667c478bd9Sstevel@tonic-gate 		    part_name, desc->secondary_name, errp);
1677c478bd9Sstevel@tonic-gate 		if (*errp != 0) {
1687c478bd9Sstevel@tonic-gate 		    cache_free_descriptors(partitions);
1697c478bd9Sstevel@tonic-gate 		    return (NULL);
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 		partitions[1] = NULL;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		return (partitions);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	    }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	    return (libdiskmgt_empty_desc_array(errp));
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/* Must be for media, so get all the parts. */
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	pos = 0;
1837c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
1847c478bd9Sstevel@tonic-gate 	    if (iparts[i].systid != 0) {
1857c478bd9Sstevel@tonic-gate 		char	part_name[MAXPATHLEN];
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		if (conv_flag) {
1887c478bd9Sstevel@tonic-gate 		    /* convert part. name (e.g. c0d0p0) */
1897c478bd9Sstevel@tonic-gate 		    (void) snprintf(part_name, sizeof (part_name), "%s%d",
1907c478bd9Sstevel@tonic-gate 			pname, i);
1917c478bd9Sstevel@tonic-gate 		} else {
1927c478bd9Sstevel@tonic-gate 		    (void) snprintf(part_name, sizeof (part_name), "%d", i);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		/* the media name comes from the media desc. */
1967c478bd9Sstevel@tonic-gate 		partitions[pos] = cache_get_desc(DM_PARTITION, desc->p.disk,
1977c478bd9Sstevel@tonic-gate 		    part_name, desc->name, errp);
1987c478bd9Sstevel@tonic-gate 		if (*errp != 0) {
1997c478bd9Sstevel@tonic-gate 		    cache_free_descriptors(partitions);
2007c478bd9Sstevel@tonic-gate 		    return (NULL);
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		pos++;
2047c478bd9Sstevel@tonic-gate 	    }
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	partitions[pos] = NULL;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	*errp = 0;
2097c478bd9Sstevel@tonic-gate 	return (partitions);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate nvlist_t *
2137c478bd9Sstevel@tonic-gate partition_get_attributes(descriptor_t *dp, int *errp)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
2167c478bd9Sstevel@tonic-gate 	struct ipart	iparts[FD_NUMPART];
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (!desc_ok(dp)) {
2197c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
2207c478bd9Sstevel@tonic-gate 	    return (NULL);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if ((*errp = get_parts(dp->p.disk, iparts, NULL, 0)) != 0) {
2247c478bd9Sstevel@tonic-gate 	    return (NULL);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
2287c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
2297c478bd9Sstevel@tonic-gate 	    return (NULL);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if ((*errp = get_attrs(dp, iparts, attrs)) != 0) {
2337c478bd9Sstevel@tonic-gate 	    nvlist_free(attrs);
2347c478bd9Sstevel@tonic-gate 	    attrs = NULL;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	return (attrs);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * Look for the partition by the partition number (which is not too useful).
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate descriptor_t *
2447c478bd9Sstevel@tonic-gate partition_get_descriptor_by_name(char *name, int *errp)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	descriptor_t	**partitions;
2477c478bd9Sstevel@tonic-gate 	int		i;
2487c478bd9Sstevel@tonic-gate 	descriptor_t	*partition = NULL;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	partitions = cache_get_descriptors(DM_PARTITION, errp);
2517c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
2527c478bd9Sstevel@tonic-gate 	    return (NULL);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	for (i = 0; partitions[i]; i++) {
2567c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(name, partitions[i]->name)) {
2577c478bd9Sstevel@tonic-gate 		partition = partitions[i];
2587c478bd9Sstevel@tonic-gate 	    } else {
2597c478bd9Sstevel@tonic-gate 		/* clean up the unused descriptors */
2607c478bd9Sstevel@tonic-gate 		cache_free_descriptor(partitions[i]);
2617c478bd9Sstevel@tonic-gate 	    }
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 	free(partitions);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (partition == NULL) {
2667c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	return (partition);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /* ARGSUSED */
2737c478bd9Sstevel@tonic-gate descriptor_t **
2747c478bd9Sstevel@tonic-gate partition_get_descriptors(int filter[], int *errp)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	return (cache_get_descriptors(DM_PARTITION, errp));
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate char *
2807c478bd9Sstevel@tonic-gate partition_get_name(descriptor_t *desc)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	return (desc->name);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /* ARGSUSED */
2867c478bd9Sstevel@tonic-gate nvlist_t *
2877c478bd9Sstevel@tonic-gate partition_get_stats(descriptor_t *dp, int stat_type, int *errp)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	/* There are no stat types defined for partitions */
2907c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
2917c478bd9Sstevel@tonic-gate 	return (NULL);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /* ARGSUSED */
2957c478bd9Sstevel@tonic-gate int
2967c478bd9Sstevel@tonic-gate partition_has_fdisk(disk_t *dp, int fd)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	char		bootsect[512 * 3]; /* 3 sectors to be safe */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate #ifdef sparc
3017c478bd9Sstevel@tonic-gate 	if (dp->drv_type == DM_DT_FIXED) {
3027c478bd9Sstevel@tonic-gate 	    /* on sparc, only removable media can have fdisk parts. */
3037c478bd9Sstevel@tonic-gate 	    return (0);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * We assume the caller already made sure media was inserted and
3097c478bd9Sstevel@tonic-gate 	 * spun up.
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	if ((ioctl(fd, DKIOCGMBOOT, bootsect) < 0) && (errno != ENOTTY)) {
3137c478bd9Sstevel@tonic-gate 	    return (0);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	return (1);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * A partition descriptor points to a disk, the name is the partition number
3217c478bd9Sstevel@tonic-gate  * and the secondary name is the media name.
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate int
3247c478bd9Sstevel@tonic-gate partition_make_descriptors()
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	int		error;
3277c478bd9Sstevel@tonic-gate 	disk_t		*dp;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	dp = cache_get_disklist();
3307c478bd9Sstevel@tonic-gate 	while (dp != NULL) {
3317c478bd9Sstevel@tonic-gate 	    struct ipart	iparts[FD_NUMPART];
3327c478bd9Sstevel@tonic-gate 	    char		pname[MAXPATHLEN];
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	    if (get_parts(dp, iparts, pname, sizeof (pname)) == 0) {
3357c478bd9Sstevel@tonic-gate 		int	i;
3367c478bd9Sstevel@tonic-gate 		char	mname[MAXPATHLEN];
3377c478bd9Sstevel@tonic-gate 		int	conv_flag = 0;
338*3e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64)
3397c478bd9Sstevel@tonic-gate 		/* convert part. name (e.g. c0d0p0) */
3407c478bd9Sstevel@tonic-gate 		int	len;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		len = strlen(pname);
3437c478bd9Sstevel@tonic-gate 		if (len > 1 && *(pname + (len - 2)) == 'p') {
3447c478bd9Sstevel@tonic-gate 		    conv_flag = 1;
3457c478bd9Sstevel@tonic-gate 		    *(pname + (len - 1)) = 0;
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate #endif
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		mname[0] = 0;
3507c478bd9Sstevel@tonic-gate 		(void) media_read_name(dp, mname, sizeof (mname));
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
3537c478bd9Sstevel@tonic-gate 		    if (iparts[i].systid != 0) {
3547c478bd9Sstevel@tonic-gate 			char	part_name[MAXPATHLEN];
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 			if (conv_flag) {
3577c478bd9Sstevel@tonic-gate 			    /* convert part. name (e.g. c0d0p0) */
3587c478bd9Sstevel@tonic-gate 			    (void) snprintf(part_name, sizeof (part_name),
3597c478bd9Sstevel@tonic-gate 				"%s%d", pname, i);
3607c478bd9Sstevel@tonic-gate 			} else {
3617c478bd9Sstevel@tonic-gate 			    (void) snprintf(part_name, sizeof (part_name),
3627c478bd9Sstevel@tonic-gate 				"%d", i);
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			cache_load_desc(DM_PARTITION, dp, part_name, mname,
3667c478bd9Sstevel@tonic-gate 			    &error);
3677c478bd9Sstevel@tonic-gate 			if (error != 0) {
3687c478bd9Sstevel@tonic-gate 			    return (error);
3697c478bd9Sstevel@tonic-gate 			}
3707c478bd9Sstevel@tonic-gate 		    }
3717c478bd9Sstevel@tonic-gate 		}
3727c478bd9Sstevel@tonic-gate 	    }
3737c478bd9Sstevel@tonic-gate 	    dp = dp->next;
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	return (0);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static int
3807c478bd9Sstevel@tonic-gate get_attrs(descriptor_t *dp, struct ipart *iparts, nvlist_t *attrs)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	char		*p;
3837c478bd9Sstevel@tonic-gate 	int		part_num;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * We already made sure the media was loaded and ready in the
3877c478bd9Sstevel@tonic-gate 	 * get_parts call within partition_get_attributes.
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	p = strrchr(dp->name, 'p');
3917c478bd9Sstevel@tonic-gate 	if (p == NULL) {
3927c478bd9Sstevel@tonic-gate 	    p = dp->name;
3937c478bd9Sstevel@tonic-gate 	} else {
3947c478bd9Sstevel@tonic-gate 	    p++;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 	part_num = atoi(p);
3977c478bd9Sstevel@tonic-gate 	if (part_num >= FD_NUMPART || iparts[part_num].systid == 0) {
3987c478bd9Sstevel@tonic-gate 	    return (ENODEV);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/* we found the partition */
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BOOTID,
4047c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].bootid) != 0) {
4057c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_PTYPE,
4097c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].systid) != 0) {
4107c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BHEAD,
4147c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].beghead) != 0) {
4157c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BSECT,
4197c478bd9Sstevel@tonic-gate 	    (unsigned int)((iparts[part_num].begsect) & 0x3f)) != 0) {
4207c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BCYL, (unsigned int)
4247c478bd9Sstevel@tonic-gate 	    ((iparts[part_num].begcyl & 0xff) |
4257c478bd9Sstevel@tonic-gate 	    ((iparts[part_num].begsect & 0xc0) << 2))) != 0) {
4267c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_EHEAD,
4307c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].endhead) != 0) {
4317c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_ESECT,
4357c478bd9Sstevel@tonic-gate 	    (unsigned int)((iparts[part_num].endsect) & 0x3f)) != 0) {
4367c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_ECYL, (unsigned int)
4407c478bd9Sstevel@tonic-gate 	    ((iparts[part_num].endcyl & 0xff) |
4417c478bd9Sstevel@tonic-gate 	    ((iparts[part_num].endsect & 0xc0) << 2))) != 0) {
4427c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_RELSECT,
4467c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].relsect) != 0) {
4477c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_NSECTORS,
4517c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].numsect) != 0) {
4527c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	return (0);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate static int
4597c478bd9Sstevel@tonic-gate get_parts(disk_t *disk, struct ipart *iparts, char *opath, int opath_len)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	int		fd;
4627c478bd9Sstevel@tonic-gate 	struct dk_minfo	minfo;
4637c478bd9Sstevel@tonic-gate 	struct mboot	bootblk;
4647c478bd9Sstevel@tonic-gate 	char		bootsect[512];
4657c478bd9Sstevel@tonic-gate 	int		i;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/* Can't use drive_open_disk since we need the partition dev name. */
4687c478bd9Sstevel@tonic-gate 	if ((fd = open_disk(disk, opath, opath_len)) < 0) {
4697c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/* First make sure media is inserted and spun up. */
4737c478bd9Sstevel@tonic-gate 	if (!media_read_info(fd, &minfo)) {
4747c478bd9Sstevel@tonic-gate 	    (void) close(fd);
4757c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (!partition_has_fdisk(disk, fd)) {
4797c478bd9Sstevel@tonic-gate 	    (void) close(fd);
4807c478bd9Sstevel@tonic-gate 	    return (ENOTTY);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (lseek(fd, 0, 0) == -1) {
4847c478bd9Sstevel@tonic-gate 	    (void) close(fd);
4857c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (read(fd, bootsect, 512) != 512) {
4897c478bd9Sstevel@tonic-gate 	    (void) close(fd);
4907c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	(void) close(fd);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	(void) memcpy(&bootblk, bootsect, sizeof (bootblk));
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if (les(bootblk.signature) != MBB_MAGIC)  {
4977c478bd9Sstevel@tonic-gate 	    return (ENOTTY);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	(void) memcpy(iparts, bootblk.parts, ISIZE);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
5037c478bd9Sstevel@tonic-gate 	    if (iparts[i].systid != 0) {
5047c478bd9Sstevel@tonic-gate 		iparts[i].relsect = lel(iparts[i].relsect);
5057c478bd9Sstevel@tonic-gate 		iparts[i].numsect = lel(iparts[i].numsect);
5067c478bd9Sstevel@tonic-gate 	    }
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	return (0);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate /* return 1 if the partition descriptor is still valid, 0 if not. */
5127c478bd9Sstevel@tonic-gate static int
5137c478bd9Sstevel@tonic-gate desc_ok(descriptor_t *dp)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	/* First verify the media name for removable media */
5167c478bd9Sstevel@tonic-gate 	if (dp->p.disk->removable) {
5177c478bd9Sstevel@tonic-gate 	    char	mname[MAXPATHLEN];
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	    if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
5207c478bd9Sstevel@tonic-gate 		return (0);
5217c478bd9Sstevel@tonic-gate 	    }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	    if (mname[0] == 0) {
5247c478bd9Sstevel@tonic-gate 		return (libdiskmgt_str_eq(dp->secondary_name, NULL));
5257c478bd9Sstevel@tonic-gate 	    } else {
5267c478bd9Sstevel@tonic-gate 		return (libdiskmgt_str_eq(dp->secondary_name, mname));
5277c478bd9Sstevel@tonic-gate 	    }
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/*
5317c478bd9Sstevel@tonic-gate 	 * We could verify the partition is still there but this is kind of
5327c478bd9Sstevel@tonic-gate 	 * expensive and other code down the line will do that (e.g. see
5337c478bd9Sstevel@tonic-gate 	 * get_attrs).
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	return (1);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * Return 1 if partition has slices, 0 if not.
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate static int
5437c478bd9Sstevel@tonic-gate has_slices(descriptor_t *desc, int *errp)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	int		pnum;
5467c478bd9Sstevel@tonic-gate 	int		i;
5477c478bd9Sstevel@tonic-gate 	char		*p;
5487c478bd9Sstevel@tonic-gate 	struct ipart	iparts[FD_NUMPART];
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (get_parts(desc->p.disk, iparts, NULL, 0) != 0) {
5517c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
5527c478bd9Sstevel@tonic-gate 	    return (0);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	p = strrchr(desc->name, 'p');
5567c478bd9Sstevel@tonic-gate 	if (p == NULL) {
5577c478bd9Sstevel@tonic-gate 	    p = desc->name;
5587c478bd9Sstevel@tonic-gate 	} else {
5597c478bd9Sstevel@tonic-gate 	    p++;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	pnum = atoi(p);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/*
5647c478bd9Sstevel@tonic-gate 	 * Slices are associated with the active solaris partition or if there
5657c478bd9Sstevel@tonic-gate 	 * is no active solaris partition, then the first solaris partition.
5667c478bd9Sstevel@tonic-gate 	 */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	*errp = 0;
5697c478bd9Sstevel@tonic-gate 	if (iparts[pnum].bootid == ACTIVE &&
5707c478bd9Sstevel@tonic-gate 	    (iparts[pnum].systid == SUNIXOS ||
5717c478bd9Sstevel@tonic-gate 	    iparts[pnum].systid == SUNIXOS2)) {
5727c478bd9Sstevel@tonic-gate 		return (1);
5737c478bd9Sstevel@tonic-gate 	} else {
5747c478bd9Sstevel@tonic-gate 	    int	active = 0;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	    /* Check if there are no active solaris partitions. */
5777c478bd9Sstevel@tonic-gate 	    for (i = 0; i < FD_NUMPART; i++) {
5787c478bd9Sstevel@tonic-gate 		if (iparts[i].bootid == ACTIVE &&
5797c478bd9Sstevel@tonic-gate 		    (iparts[i].systid == SUNIXOS ||
5807c478bd9Sstevel@tonic-gate 		    iparts[i].systid == SUNIXOS2)) {
5817c478bd9Sstevel@tonic-gate 			active = 1;
5827c478bd9Sstevel@tonic-gate 			break;
5837c478bd9Sstevel@tonic-gate 		}
5847c478bd9Sstevel@tonic-gate 	    }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	    if (!active) {
5877c478bd9Sstevel@tonic-gate 		/* Check if this is the first solaris partition. */
5887c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
5897c478bd9Sstevel@tonic-gate 		    if (iparts[i].systid == SUNIXOS ||
5907c478bd9Sstevel@tonic-gate 			iparts[i].systid == SUNIXOS2) {
5917c478bd9Sstevel@tonic-gate 			    break;
5927c478bd9Sstevel@tonic-gate 		    }
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		if (i < FD_NUMPART && i == pnum) {
5967c478bd9Sstevel@tonic-gate 		    return (1);
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 	    }
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	return (0);
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static int
6057c478bd9Sstevel@tonic-gate open_disk(disk_t *diskp, char *opath, int len)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	char	rmmedia_devpath[MAXPATHLEN];
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (diskp->removable && media_get_volm_path(diskp, rmmedia_devpath,
6107c478bd9Sstevel@tonic-gate 	    sizeof (rmmedia_devpath))) {
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	    int		fd;
6137c478bd9Sstevel@tonic-gate 	    struct stat	buf;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	    if (rmmedia_devpath[0] == 0) {
6167c478bd9Sstevel@tonic-gate 		/* removable but no media */
6177c478bd9Sstevel@tonic-gate 		return (-1);
6187c478bd9Sstevel@tonic-gate 	    }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	    if ((fd = open(rmmedia_devpath, O_RDONLY|O_NDELAY)) < 0) {
6217c478bd9Sstevel@tonic-gate 		return (-1);
6227c478bd9Sstevel@tonic-gate 	    }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	    if (fstat(fd, &buf) != 0) {
6257c478bd9Sstevel@tonic-gate 		(void) close(fd);
6267c478bd9Sstevel@tonic-gate 		return (-1);
6277c478bd9Sstevel@tonic-gate 	    }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	    if (buf.st_mode & S_IFCHR) {
6307c478bd9Sstevel@tonic-gate 		/* opened, is device, so done */
6317c478bd9Sstevel@tonic-gate 		if (opath != NULL) {
6327c478bd9Sstevel@tonic-gate 		    (void) strlcpy(opath, rmmedia_devpath, len);
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 		return (fd);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	    } else if (buf.st_mode & S_IFDIR) {
6377c478bd9Sstevel@tonic-gate 		/* disk w/ slices so handle the directory */
6387c478bd9Sstevel@tonic-gate 		DIR		*dirp;
6397c478bd9Sstevel@tonic-gate 		struct dirent	*dentp;
6407c478bd9Sstevel@tonic-gate 		int		dfd;
6417c478bd9Sstevel@tonic-gate #ifdef _LP64
6427c478bd9Sstevel@tonic-gate 		struct dirent	*result;
6437c478bd9Sstevel@tonic-gate #endif
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		/* each device file in the dir represents a slice */
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		if ((dirp = fdopendir(fd)) == NULL) {
6487c478bd9Sstevel@tonic-gate 		    (void) close(fd);
6497c478bd9Sstevel@tonic-gate 		    return (-1);
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		if ((dentp = (struct dirent *)malloc(sizeof (struct dirent) +
653*3e1bd7a2Ssjelinek 		    PATH_MAX + 1)) == NULL) {
6547c478bd9Sstevel@tonic-gate 		    /* out of memory */
6557c478bd9Sstevel@tonic-gate 		    (void) close(fd);
6567c478bd9Sstevel@tonic-gate 		    return (-1);
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate #ifdef _LP64
6597c478bd9Sstevel@tonic-gate 		while (readdir_r(dirp, dentp, &result) != NULL) {
6607c478bd9Sstevel@tonic-gate #else
6617c478bd9Sstevel@tonic-gate 		while (readdir_r(dirp, dentp) != NULL) {
6627c478bd9Sstevel@tonic-gate #endif
6637c478bd9Sstevel@tonic-gate 		    char	slice_path[MAXPATHLEN];
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		    if (libdiskmgt_str_eq(".", dentp->d_name) ||
6667c478bd9Sstevel@tonic-gate 			libdiskmgt_str_eq("..", dentp->d_name)) {
6677c478bd9Sstevel@tonic-gate 			continue;
6687c478bd9Sstevel@tonic-gate 		    }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		    (void) snprintf(slice_path, sizeof (slice_path), "%s/%s",
6717c478bd9Sstevel@tonic-gate 			rmmedia_devpath, dentp->d_name);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		    if ((dfd = open(slice_path, O_RDONLY|O_NDELAY)) < 0) {
6747c478bd9Sstevel@tonic-gate 			continue;
6757c478bd9Sstevel@tonic-gate 		    }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		    if (fstat(dfd, &buf) == 0 && (buf.st_mode & S_IFCHR)) {
6787c478bd9Sstevel@tonic-gate 			/* opened, is device, so done */
6797c478bd9Sstevel@tonic-gate 			free(dentp);
6807c478bd9Sstevel@tonic-gate 			(void) close(fd);
6817c478bd9Sstevel@tonic-gate 			if (opath != NULL) {
6827c478bd9Sstevel@tonic-gate 			    (void) strlcpy(opath, slice_path, len);
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 			return (dfd);
6857c478bd9Sstevel@tonic-gate 		    }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 		    /* not a device, keep looking */
6887c478bd9Sstevel@tonic-gate 		    (void) close(dfd);
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		/* did not find a device under the rmmedia_path */
6927c478bd9Sstevel@tonic-gate 		free(dentp);
6937c478bd9Sstevel@tonic-gate 		(void) close(fd);
6947c478bd9Sstevel@tonic-gate 	    }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	    /* didn't find a device under volume management control */
6977c478bd9Sstevel@tonic-gate 	    return (-1);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * Not removable media under volume management control so just open the
7027c478bd9Sstevel@tonic-gate 	 * first devpath.
7037c478bd9Sstevel@tonic-gate 	 */
7047c478bd9Sstevel@tonic-gate 	if (diskp->aliases != NULL && diskp->aliases->devpaths != NULL) {
7057c478bd9Sstevel@tonic-gate #ifdef sparc
7067c478bd9Sstevel@tonic-gate 	    if (opath != NULL) {
7077c478bd9Sstevel@tonic-gate 		(void) strlcpy(opath, diskp->aliases->devpaths->devpath, len);
7087c478bd9Sstevel@tonic-gate 	    }
7097c478bd9Sstevel@tonic-gate 	    return (open(diskp->aliases->devpaths->devpath, O_RDONLY|O_NDELAY));
7107c478bd9Sstevel@tonic-gate #else
7117c478bd9Sstevel@tonic-gate 	    /* On intel we need to open partition device (e.g. c0d0p0). */
7127c478bd9Sstevel@tonic-gate 	    char	part_dev[MAXPATHLEN];
7137c478bd9Sstevel@tonic-gate 	    char	*p;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	    (void) strlcpy(part_dev, diskp->aliases->devpaths->devpath,
7167c478bd9Sstevel@tonic-gate 		sizeof (part_dev));
7177c478bd9Sstevel@tonic-gate 	    p = strrchr(part_dev, '/');
7187c478bd9Sstevel@tonic-gate 	    if (p == NULL) {
7197c478bd9Sstevel@tonic-gate 		p = strrchr(part_dev, 's');
7207c478bd9Sstevel@tonic-gate 		if (p != NULL) {
7217c478bd9Sstevel@tonic-gate 		    *p = 'p';
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 	    } else {
7247c478bd9Sstevel@tonic-gate 		char *ps;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		*p = 0;
7277c478bd9Sstevel@tonic-gate 		ps = strrchr((p + 1), 's');
7287c478bd9Sstevel@tonic-gate 		if (ps != NULL) {
7297c478bd9Sstevel@tonic-gate 		    *ps = 'p';
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 		*p = '/';
7327c478bd9Sstevel@tonic-gate 	    }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	    if (opath != NULL) {
7357c478bd9Sstevel@tonic-gate 		(void) strlcpy(opath, part_dev, len);
7367c478bd9Sstevel@tonic-gate 	    }
7377c478bd9Sstevel@tonic-gate 	    return (open(part_dev, O_RDONLY|O_NDELAY));
7387c478bd9Sstevel@tonic-gate #endif
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	return (-1);
7427c478bd9Sstevel@tonic-gate }
743