xref: /titanic_52/usr/src/lib/libdiskmgt/common/media.c (revision ac9c73bd0c7b819708a53a48719ffd52f3ab06c3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
518c2aff7Sartem  * Common Development and Distribution License (the "License").
618c2aff7Sartem  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ac9c73bdSsjelinek  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <stropts.h>
357c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
407c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
437c478bd9Sstevel@tonic-gate #include "disks_private.h"
447c478bd9Sstevel@tonic-gate #include "partition.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	IOCTLRETRIES		2
477c478bd9Sstevel@tonic-gate #define	IOCTLRETRYINTERVAL	1
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static descriptor_t	**apply_filter(descriptor_t **media, int filter[],
507c478bd9Sstevel@tonic-gate 			    int *errp);
517c478bd9Sstevel@tonic-gate static int		get_attrs(disk_t *dp, int fd, nvlist_t *attrs);
5218c2aff7Sartem static int		get_rmm_name(disk_t *dp, char *mname, int size);
537c478bd9Sstevel@tonic-gate static int		get_media_type(uint_t media_type);
547c478bd9Sstevel@tonic-gate static int		desc_ok(descriptor_t *dp);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * This function gets the descriptors we are associated with.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate descriptor_t **
607c478bd9Sstevel@tonic-gate media_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type,
617c478bd9Sstevel@tonic-gate     int *errp)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	if (!desc_ok(desc)) {
647c478bd9Sstevel@tonic-gate 		*errp = ENODEV;
657c478bd9Sstevel@tonic-gate 		return (NULL);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	switch (type) {
697c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
707c478bd9Sstevel@tonic-gate 		return (drive_get_assocs(desc, errp));
717c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
727c478bd9Sstevel@tonic-gate 		return (partition_get_assocs(desc, errp));
737c478bd9Sstevel@tonic-gate 	case DM_SLICE:
747c478bd9Sstevel@tonic-gate 		return (slice_get_assocs(desc, errp));
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
787c478bd9Sstevel@tonic-gate 	return (NULL);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Get the media descriptors for the given drive/partition/slice.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate descriptor_t **
857c478bd9Sstevel@tonic-gate media_get_assocs(descriptor_t *dp, int *errp)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	descriptor_t	**media;
887c478bd9Sstevel@tonic-gate 	char		mname[MAXPATHLEN];
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
91*ac9c73bdSsjelinek 		/*
92*ac9c73bdSsjelinek 		 * For drives, this means no media but slice/part.
93*ac9c73bdSsjelinek 		 * require media.
94*ac9c73bdSsjelinek 		 */
957c478bd9Sstevel@tonic-gate 		if (dp->type == DM_DRIVE) {
967c478bd9Sstevel@tonic-gate 			return (libdiskmgt_empty_desc_array(errp));
977c478bd9Sstevel@tonic-gate 		} else {
987c478bd9Sstevel@tonic-gate 			*errp = ENODEV;
997c478bd9Sstevel@tonic-gate 			return (NULL);
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/* make the snapshot */
1047c478bd9Sstevel@tonic-gate 	media = (descriptor_t **)calloc(2, sizeof (descriptor_t *));
1057c478bd9Sstevel@tonic-gate 	if (media == NULL) {
1067c478bd9Sstevel@tonic-gate 		*errp = ENOMEM;
1077c478bd9Sstevel@tonic-gate 		return (NULL);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	media[0] = cache_get_desc(DM_MEDIA, dp->p.disk, mname, NULL, errp);
1117c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
1127c478bd9Sstevel@tonic-gate 		free(media);
1137c478bd9Sstevel@tonic-gate 		return (NULL);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	media[1] = NULL;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	*errp = 0;
1187c478bd9Sstevel@tonic-gate 	return (media);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate nvlist_t *
1227c478bd9Sstevel@tonic-gate media_get_attributes(descriptor_t *dp, int *errp)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
1257c478bd9Sstevel@tonic-gate 	int		fd;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (!desc_ok(dp)) {
1287c478bd9Sstevel@tonic-gate 		*errp = ENODEV;
1297c478bd9Sstevel@tonic-gate 		return (NULL);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
1337c478bd9Sstevel@tonic-gate 		*errp = ENOMEM;
1347c478bd9Sstevel@tonic-gate 		return (NULL);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	fd = drive_open_disk(dp->p.disk, NULL, 0);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if ((*errp = get_attrs(dp->p.disk, fd, attrs)) != 0) {
1407c478bd9Sstevel@tonic-gate 		nvlist_free(attrs);
1417c478bd9Sstevel@tonic-gate 		attrs = NULL;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
1457c478bd9Sstevel@tonic-gate 		(void) close(fd);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	return (attrs);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate descriptor_t *
1527c478bd9Sstevel@tonic-gate media_get_descriptor_by_name(char *name, int *errp)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	descriptor_t	**media;
1557c478bd9Sstevel@tonic-gate 	int		i;
1567c478bd9Sstevel@tonic-gate 	descriptor_t	*medium = NULL;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	media = cache_get_descriptors(DM_MEDIA, errp);
1597c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
1607c478bd9Sstevel@tonic-gate 		return (NULL);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	for (i = 0; media[i]; i++) {
1647c478bd9Sstevel@tonic-gate 		if (libdiskmgt_str_eq(name, media[i]->name)) {
1657c478bd9Sstevel@tonic-gate 			medium = media[i];
1667c478bd9Sstevel@tonic-gate 		} else {
1677c478bd9Sstevel@tonic-gate 			/* clean up the unused descriptors */
1687c478bd9Sstevel@tonic-gate 			cache_free_descriptor(media[i]);
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	free(media);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (medium == NULL) {
1747c478bd9Sstevel@tonic-gate 		*errp = ENODEV;
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return (medium);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate descriptor_t **
1817c478bd9Sstevel@tonic-gate media_get_descriptors(int filter[], int *errp)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	descriptor_t	**media;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	media = cache_get_descriptors(DM_MEDIA, errp);
1867c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
1877c478bd9Sstevel@tonic-gate 		return (NULL);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (filter != NULL && filter[0] != DM_FILTER_END) {
1917c478bd9Sstevel@tonic-gate 		descriptor_t	**found;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		found = apply_filter(media, filter, errp);
1947c478bd9Sstevel@tonic-gate 		if (*errp != 0) {
1957c478bd9Sstevel@tonic-gate 			media = NULL;
1967c478bd9Sstevel@tonic-gate 		} else {
1977c478bd9Sstevel@tonic-gate 			media = found;
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	return (media);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate char *
2057c478bd9Sstevel@tonic-gate media_get_name(descriptor_t *desc)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	return (desc->name);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /* ARGSUSED */
2117c478bd9Sstevel@tonic-gate nvlist_t *
2127c478bd9Sstevel@tonic-gate media_get_stats(descriptor_t *dp, int stat_type, int *errp)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	/* There are no stat types defined for media */
2157c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
2167c478bd9Sstevel@tonic-gate 	return (NULL);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate int
2207c478bd9Sstevel@tonic-gate media_make_descriptors()
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	int		error;
2237c478bd9Sstevel@tonic-gate 	disk_t		*dp;
2247c478bd9Sstevel@tonic-gate 	char		mname[MAXPATHLEN];
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	dp = cache_get_disklist();
2277c478bd9Sstevel@tonic-gate 	while (dp != NULL) {
2287c478bd9Sstevel@tonic-gate 		if (media_read_name(dp, mname, sizeof (mname))) {
2297c478bd9Sstevel@tonic-gate 			cache_load_desc(DM_MEDIA, dp, mname, NULL, &error);
2307c478bd9Sstevel@tonic-gate 			if (error != 0) {
2317c478bd9Sstevel@tonic-gate 				return (error);
2327c478bd9Sstevel@tonic-gate 			}
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		dp = dp->next;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (0);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * Read the media information.
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate int
2457c478bd9Sstevel@tonic-gate media_read_info(int fd, struct dk_minfo *minfo)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate 	int	status;
2487c478bd9Sstevel@tonic-gate 	int	tries = 0;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	minfo->dki_media_type = 0;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * This ioctl can fail if the media is not loaded or spun up.
2547c478bd9Sstevel@tonic-gate 	 * Retrying can sometimes succeed since the first ioctl will have
2557c478bd9Sstevel@tonic-gate 	 * started the media before the ioctl timed out so the media may be
2567c478bd9Sstevel@tonic-gate 	 * spun up on the subsequent attempt.
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	while ((status = ioctl(fd, DKIOCGMEDIAINFO, minfo)) < 0) {
2597c478bd9Sstevel@tonic-gate 		tries++;
2607c478bd9Sstevel@tonic-gate 		if (tries >= IOCTLRETRIES) {
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		(void) sleep(IOCTLRETRYINTERVAL);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (status < 0) {
2677c478bd9Sstevel@tonic-gate 		return (0);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	return (1);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /* return 1 if there is media, 0 if not. */
2747c478bd9Sstevel@tonic-gate int
2757c478bd9Sstevel@tonic-gate media_read_name(disk_t *dp, char *mname, int size)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	mname[0] = 0;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (!dp->removable) {
2807c478bd9Sstevel@tonic-gate 		/* not removable, so media name is devid */
2817c478bd9Sstevel@tonic-gate 		if (dp->device_id != NULL) {
2827c478bd9Sstevel@tonic-gate 			(void) strlcpy(mname, dp->device_id, size);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		return (1);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/* This is a removable media drive. */
28818c2aff7Sartem 	return (get_rmm_name(dp, mname, size));
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate static descriptor_t **
2927c478bd9Sstevel@tonic-gate apply_filter(descriptor_t **media, int filter[], int *errp)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate 	descriptor_t	**found;
2957c478bd9Sstevel@tonic-gate 	int		i;
2967c478bd9Sstevel@tonic-gate 	int		cnt = 0;
2977c478bd9Sstevel@tonic-gate 	int		pos;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/* count the number of media in the snapshot */
3007c478bd9Sstevel@tonic-gate 	for (i = 0; media[i]; i++) {
3017c478bd9Sstevel@tonic-gate 		cnt++;
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	found = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
3057c478bd9Sstevel@tonic-gate 	if (found == NULL) {
3067c478bd9Sstevel@tonic-gate 		*errp = ENOMEM;
3077c478bd9Sstevel@tonic-gate 		cache_free_descriptors(media);
3087c478bd9Sstevel@tonic-gate 		return (NULL);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	pos = 0;
3127c478bd9Sstevel@tonic-gate 	for (i = 0; media[i]; i++) {
3137c478bd9Sstevel@tonic-gate 		int	fd;
3147c478bd9Sstevel@tonic-gate 		struct	dk_minfo minfo;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		if ((fd = drive_open_disk(media[i]->p.disk, NULL, 0)) < 0) {
3177c478bd9Sstevel@tonic-gate 			continue;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		if (media_read_info(fd, &minfo)) {
3217c478bd9Sstevel@tonic-gate 			int	mtype;
3227c478bd9Sstevel@tonic-gate 			int	j;
3237c478bd9Sstevel@tonic-gate 			int	match;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 			mtype = get_media_type(minfo.dki_media_type);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			match = 0;
3287c478bd9Sstevel@tonic-gate 			for (j = 0; filter[j] != DM_FILTER_END; j++) {
3297c478bd9Sstevel@tonic-gate 				if (mtype == filter[j]) {
3307c478bd9Sstevel@tonic-gate 					found[pos++] = media[i];
3317c478bd9Sstevel@tonic-gate 					match = 1;
3327c478bd9Sstevel@tonic-gate 					break;
3337c478bd9Sstevel@tonic-gate 				}
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 			if (!match) {
3377c478bd9Sstevel@tonic-gate 				cache_free_descriptor(media[i]);
3387c478bd9Sstevel@tonic-gate 			}
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		(void) close(fd);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	found[pos] = NULL;
3437c478bd9Sstevel@tonic-gate 	free(media);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	*errp = 0;
3467c478bd9Sstevel@tonic-gate 	return (found);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /* return 1 if the media descriptor is still valid, 0 if not. */
3507c478bd9Sstevel@tonic-gate static int
3517c478bd9Sstevel@tonic-gate desc_ok(descriptor_t *dp)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	/* First verify the media name for removable media */
3547c478bd9Sstevel@tonic-gate 	if (dp->p.disk->removable) {
3557c478bd9Sstevel@tonic-gate 		char	mname[MAXPATHLEN];
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
3587c478bd9Sstevel@tonic-gate 			return (0);
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		if (mname[0] == 0) {
3627c478bd9Sstevel@tonic-gate 			return (libdiskmgt_str_eq(dp->name, NULL));
3637c478bd9Sstevel@tonic-gate 		} else {
3647c478bd9Sstevel@tonic-gate 			return (libdiskmgt_str_eq(dp->name, mname));
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	return (1);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static int
3727c478bd9Sstevel@tonic-gate get_attrs(disk_t *dp, int fd, nvlist_t *attrs)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	struct	dk_minfo minfo;
3757c478bd9Sstevel@tonic-gate 	struct	dk_geom	geometry;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (fd < 0) {
3787c478bd9Sstevel@tonic-gate 		return (ENODEV);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	bzero(&minfo, sizeof (struct dk_minfo));
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	/* The first thing to do is read the media */
3847c478bd9Sstevel@tonic-gate 	if (!media_read_info(fd, &minfo)) {
3857c478bd9Sstevel@tonic-gate 		return (ENODEV);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (partition_has_fdisk(dp, fd)) {
3897c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_FDISK) != 0) {
3907c478bd9Sstevel@tonic-gate 			return (ENOMEM);
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (dp->removable) {
3957c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_REMOVABLE) != 0) {
3967c478bd9Sstevel@tonic-gate 			return (ENOMEM);
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_LOADED) != 0) {
4007c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint64(attrs, DM_SIZE, minfo.dki_capacity) != 0) {
4057c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BLOCKSIZE, minfo.dki_lbsize) != 0) {
4097c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_MTYPE,
4137c478bd9Sstevel@tonic-gate 	    get_media_type(minfo.dki_media_type)) != 0) {
4147c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
417*ac9c73bdSsjelinek 	/* only for disks < 1TB  and x86 */
418*ac9c73bdSsjelinek #if defined(i386) || defined(__amd64)
419*ac9c73bdSsjelinek 	if (ioctl(fd, DKIOCG_PHYGEOM, &geometry) >= 0) {
420*ac9c73bdSsjelinek #else
421*ac9c73bdSsjelinek 	/* sparc call */
4227c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGGEOM, &geometry) >= 0) {
423*ac9c73bdSsjelinek #endif
4247c478bd9Sstevel@tonic-gate 		struct vtoc	vtoc;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint64(attrs, DM_START, 0) != 0) {
4277c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint64(attrs, DM_NACCESSIBLE,
4307c478bd9Sstevel@tonic-gate 		    geometry.dkg_ncyl * geometry.dkg_nhead * geometry.dkg_nsect)
4317c478bd9Sstevel@tonic-gate 		    != 0) {
4327c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint32(attrs, DM_NCYLINDERS, geometry.dkg_ncyl)
4357c478bd9Sstevel@tonic-gate 		    != 0) {
4367c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4377c478bd9Sstevel@tonic-gate 		}
438*ac9c73bdSsjelinek 		if (nvlist_add_uint32(attrs, DM_NPHYSCYLINDERS,
439*ac9c73bdSsjelinek 		    geometry.dkg_pcyl) != 0) {
4407c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4417c478bd9Sstevel@tonic-gate 		}
442*ac9c73bdSsjelinek 		if (nvlist_add_uint32(attrs, DM_NALTCYLINDERS,
443*ac9c73bdSsjelinek 		    geometry.dkg_acyl) != 0) {
4447c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4457c478bd9Sstevel@tonic-gate 		}
446*ac9c73bdSsjelinek 		if (nvlist_add_uint32(attrs, DM_NHEADS,
447*ac9c73bdSsjelinek 		    geometry.dkg_nhead) != 0) {
4487c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint32(attrs, DM_NSECTORS, geometry.dkg_nsect)
4517c478bd9Sstevel@tonic-gate 		    != 0) {
4527c478bd9Sstevel@tonic-gate 			return (ENOMEM);
4537c478bd9Sstevel@tonic-gate 		}
454*ac9c73bdSsjelinek 		if (nvlist_add_uint32(attrs, DM_NACTUALCYLINDERS,
455*ac9c73bdSsjelinek 		    geometry.dkg_ncyl) != 0) {
456*ac9c73bdSsjelinek 			return (ENOMEM);
457*ac9c73bdSsjelinek 		}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		if (read_vtoc(fd, &vtoc) >= 0 && vtoc.v_volume[0] != 0) {
4607c478bd9Sstevel@tonic-gate 			char	label[LEN_DKL_VVOL + 1];
4617c478bd9Sstevel@tonic-gate 
462*ac9c73bdSsjelinek 			(void) snprintf(label, sizeof (label), "%.*s",
463*ac9c73bdSsjelinek 			    LEN_DKL_VVOL, vtoc.v_volume);
4647c478bd9Sstevel@tonic-gate 			if (nvlist_add_string(attrs, DM_LABEL, label) != 0) {
4657c478bd9Sstevel@tonic-gate 				return (ENOMEM);
4667c478bd9Sstevel@tonic-gate 			}
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	} else {
4707c478bd9Sstevel@tonic-gate 		/* check for disks > 1TB for accessible size */
4717c478bd9Sstevel@tonic-gate 		struct dk_gpt	*efip;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		if (efi_alloc_and_read(fd, &efip) >= 0) {
4747c478bd9Sstevel@tonic-gate 			diskaddr_t	p8size = 0;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 			if (nvlist_add_boolean(attrs, DM_EFI) != 0) {
4777c478bd9Sstevel@tonic-gate 				return (ENOMEM);
4787c478bd9Sstevel@tonic-gate 			}
479*ac9c73bdSsjelinek 			if (nvlist_add_uint64(attrs, DM_START,
480*ac9c73bdSsjelinek 			    efip->efi_first_u_lba) != 0) {
4817c478bd9Sstevel@tonic-gate 				return (ENOMEM);
4827c478bd9Sstevel@tonic-gate 			}
4837c478bd9Sstevel@tonic-gate 			/* partition 8 is reserved on EFI labels */
4847c478bd9Sstevel@tonic-gate 			if (efip->efi_nparts >= 9) {
4857c478bd9Sstevel@tonic-gate 				p8size = efip->efi_parts[8].p_size;
4867c478bd9Sstevel@tonic-gate 			}
4877c478bd9Sstevel@tonic-gate 			if (nvlist_add_uint64(attrs, DM_NACCESSIBLE,
488*ac9c73bdSsjelinek 			    (efip->efi_last_u_lba - p8size) -
489*ac9c73bdSsjelinek 			    efip->efi_first_u_lba) != 0) {
4907c478bd9Sstevel@tonic-gate 				efi_free(efip);
4917c478bd9Sstevel@tonic-gate 				return (ENOMEM);
4927c478bd9Sstevel@tonic-gate 			}
4937c478bd9Sstevel@tonic-gate 			efi_free(efip);
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 	return (0);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate static int
5007c478bd9Sstevel@tonic-gate get_media_type(uint_t media_type)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate 	switch (media_type) {
5037c478bd9Sstevel@tonic-gate 	case DK_UNKNOWN:
5047c478bd9Sstevel@tonic-gate 		return (DM_MT_UNKNOWN);
5057c478bd9Sstevel@tonic-gate 	case DK_MO_ERASABLE:
5067c478bd9Sstevel@tonic-gate 		return (DM_MT_MO_ERASABLE);
5077c478bd9Sstevel@tonic-gate 	case DK_MO_WRITEONCE:
5087c478bd9Sstevel@tonic-gate 		return (DM_MT_MO_WRITEONCE);
5097c478bd9Sstevel@tonic-gate 	case DK_AS_MO:
5107c478bd9Sstevel@tonic-gate 		return (DM_MT_AS_MO);
5117c478bd9Sstevel@tonic-gate 	case DK_CDROM:
5127c478bd9Sstevel@tonic-gate 		return (DM_MT_CDROM);
5137c478bd9Sstevel@tonic-gate 	case DK_CDR:
5147c478bd9Sstevel@tonic-gate 		return (DM_MT_CDR);
5157c478bd9Sstevel@tonic-gate 	case DK_CDRW:
5167c478bd9Sstevel@tonic-gate 		return (DM_MT_CDRW);
5177c478bd9Sstevel@tonic-gate 	case DK_DVDROM:
5187c478bd9Sstevel@tonic-gate 		return (DM_MT_DVDROM);
5197c478bd9Sstevel@tonic-gate 	case DK_DVDR:
5207c478bd9Sstevel@tonic-gate 		return (DM_MT_DVDR);
5217c478bd9Sstevel@tonic-gate 	case DK_DVDRAM:
5227c478bd9Sstevel@tonic-gate 		return (DM_MT_DVDRAM);
5237c478bd9Sstevel@tonic-gate 	case DK_FIXED_DISK:
5247c478bd9Sstevel@tonic-gate 		return (DM_MT_FIXED);
5257c478bd9Sstevel@tonic-gate 	case DK_FLOPPY:
5267c478bd9Sstevel@tonic-gate 		return (DM_MT_FLOPPY);
5277c478bd9Sstevel@tonic-gate 	case DK_ZIP:
5287c478bd9Sstevel@tonic-gate 		return (DM_MT_ZIP);
5297c478bd9Sstevel@tonic-gate 	case DK_JAZ:
5307c478bd9Sstevel@tonic-gate 		return (DM_MT_JAZ);
5317c478bd9Sstevel@tonic-gate 	default:
5327c478bd9Sstevel@tonic-gate 		return (DM_MT_UNKNOWN);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate /*
53718c2aff7Sartem  * This function handles removable media.
5387c478bd9Sstevel@tonic-gate  */
5397c478bd9Sstevel@tonic-gate static int
54018c2aff7Sartem get_rmm_name(disk_t *dp, char *mname, int size)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	int		loaded;
5437c478bd9Sstevel@tonic-gate 	int		fd;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	loaded = 0;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if ((fd = drive_open_disk(dp, NULL, 0)) >= 0) {
5487c478bd9Sstevel@tonic-gate 		struct dk_minfo minfo;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		if ((loaded = media_read_info(fd, &minfo))) {
5517c478bd9Sstevel@tonic-gate 			struct vtoc vtoc;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 			if (read_vtoc(fd, &vtoc) >= 0) {
5547c478bd9Sstevel@tonic-gate 				if (vtoc.v_volume[0] != NULL) {
5557c478bd9Sstevel@tonic-gate 					if (LEN_DKL_VVOL < size) {
556*ac9c73bdSsjelinek 						(void) strlcpy(mname,
557*ac9c73bdSsjelinek 						    vtoc.v_volume,
558*ac9c73bdSsjelinek 						    LEN_DKL_VVOL);
5597c478bd9Sstevel@tonic-gate 					} else {
560*ac9c73bdSsjelinek 						(void) strlcpy(mname,
561*ac9c73bdSsjelinek 						    vtoc.v_volume, size);
5627c478bd9Sstevel@tonic-gate 					}
5637c478bd9Sstevel@tonic-gate 				}
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		(void) close(fd);
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	return (loaded);
5717c478bd9Sstevel@tonic-gate }
572