xref: /titanic_51/usr/src/lib/libdiskmgt/common/media.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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
5*18c2aff7Sartem  * Common Development and Distribution License (the "License").
6*18c2aff7Sartem  * 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*18c2aff7Sartem  * Copyright 2006 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);
52*18c2aff7Sartem 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))) {
917c478bd9Sstevel@tonic-gate 	    /* For drives, this means no media but slice/part. require media. */
927c478bd9Sstevel@tonic-gate 	    if (dp->type == DM_DRIVE) {
937c478bd9Sstevel@tonic-gate 		return (libdiskmgt_empty_desc_array(errp));
947c478bd9Sstevel@tonic-gate 	    } else {
957c478bd9Sstevel@tonic-gate 		*errp = ENODEV;
967c478bd9Sstevel@tonic-gate 		return (NULL);
977c478bd9Sstevel@tonic-gate 	    }
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/* make the snapshot */
1017c478bd9Sstevel@tonic-gate 	media = (descriptor_t **)calloc(2, sizeof (descriptor_t *));
1027c478bd9Sstevel@tonic-gate 	if (media == NULL) {
1037c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
1047c478bd9Sstevel@tonic-gate 	    return (NULL);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	media[0] = cache_get_desc(DM_MEDIA, dp->p.disk, mname, NULL, errp);
1087c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
1097c478bd9Sstevel@tonic-gate 	    free(media);
1107c478bd9Sstevel@tonic-gate 	    return (NULL);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	media[1] = NULL;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	*errp = 0;
1157c478bd9Sstevel@tonic-gate 	return (media);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate nvlist_t *
1197c478bd9Sstevel@tonic-gate media_get_attributes(descriptor_t *dp, int *errp)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
1227c478bd9Sstevel@tonic-gate 	int		fd;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (!desc_ok(dp)) {
1257c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
1267c478bd9Sstevel@tonic-gate 	    return (NULL);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
1307c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
1317c478bd9Sstevel@tonic-gate 	    return (NULL);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	fd = drive_open_disk(dp->p.disk, NULL, 0);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if ((*errp = get_attrs(dp->p.disk, fd, attrs)) != 0) {
1377c478bd9Sstevel@tonic-gate 	    nvlist_free(attrs);
1387c478bd9Sstevel@tonic-gate 	    attrs = NULL;
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
1427c478bd9Sstevel@tonic-gate 	    (void) close(fd);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	return (attrs);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate descriptor_t *
1497c478bd9Sstevel@tonic-gate media_get_descriptor_by_name(char *name, int *errp)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	descriptor_t	**media;
1527c478bd9Sstevel@tonic-gate 	int		i;
1537c478bd9Sstevel@tonic-gate 	descriptor_t	*medium = NULL;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	media = cache_get_descriptors(DM_MEDIA, errp);
1567c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
1577c478bd9Sstevel@tonic-gate 	    return (NULL);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	for (i = 0; media[i]; i++) {
1617c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(name, media[i]->name)) {
1627c478bd9Sstevel@tonic-gate 		medium = media[i];
1637c478bd9Sstevel@tonic-gate 	    } else {
1647c478bd9Sstevel@tonic-gate 		/* clean up the unused descriptors */
1657c478bd9Sstevel@tonic-gate 		cache_free_descriptor(media[i]);
1667c478bd9Sstevel@tonic-gate 	    }
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	free(media);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (medium == NULL) {
1717c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	return (medium);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate descriptor_t **
1787c478bd9Sstevel@tonic-gate media_get_descriptors(int filter[], int *errp)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	descriptor_t	**media;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	media = cache_get_descriptors(DM_MEDIA, errp);
1837c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
1847c478bd9Sstevel@tonic-gate 	    return (NULL);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (filter != NULL && filter[0] != DM_FILTER_END) {
1887c478bd9Sstevel@tonic-gate 	    descriptor_t	**found;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	    found = apply_filter(media, filter, errp);
1917c478bd9Sstevel@tonic-gate 	    if (*errp != 0) {
1927c478bd9Sstevel@tonic-gate 		media = NULL;
1937c478bd9Sstevel@tonic-gate 	    } else {
1947c478bd9Sstevel@tonic-gate 		media = found;
1957c478bd9Sstevel@tonic-gate 	    }
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return (media);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate char *
2027c478bd9Sstevel@tonic-gate media_get_name(descriptor_t *desc)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	return (desc->name);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /* ARGSUSED */
2087c478bd9Sstevel@tonic-gate nvlist_t *
2097c478bd9Sstevel@tonic-gate media_get_stats(descriptor_t *dp, int stat_type, int *errp)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	/* There are no stat types defined for media */
2127c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
2137c478bd9Sstevel@tonic-gate 	return (NULL);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate int
2177c478bd9Sstevel@tonic-gate media_make_descriptors()
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	int		error;
2207c478bd9Sstevel@tonic-gate 	disk_t		*dp;
2217c478bd9Sstevel@tonic-gate 	char		mname[MAXPATHLEN];
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	dp = cache_get_disklist();
2247c478bd9Sstevel@tonic-gate 	while (dp != NULL) {
2257c478bd9Sstevel@tonic-gate 	    if (media_read_name(dp, mname, sizeof (mname))) {
2267c478bd9Sstevel@tonic-gate 		cache_load_desc(DM_MEDIA, dp, mname, NULL, &error);
2277c478bd9Sstevel@tonic-gate 		if (error != 0) {
2287c478bd9Sstevel@tonic-gate 		    return (error);
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 	    }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	    dp = dp->next;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	return (0);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Read the media information.
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate int
2427c478bd9Sstevel@tonic-gate media_read_info(int fd, struct dk_minfo *minfo)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	int	status;
2457c478bd9Sstevel@tonic-gate 	int	tries = 0;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	minfo->dki_media_type = 0;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * This ioctl can fail if the media is not loaded or spun up.
2517c478bd9Sstevel@tonic-gate 	 * Retrying can sometimes succeed since the first ioctl will have
2527c478bd9Sstevel@tonic-gate 	 * started the media before the ioctl timed out so the media may be
2537c478bd9Sstevel@tonic-gate 	 * spun up on the subsequent attempt.
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 	while ((status = ioctl(fd, DKIOCGMEDIAINFO, minfo)) < 0) {
2567c478bd9Sstevel@tonic-gate 	    tries++;
2577c478bd9Sstevel@tonic-gate 	    if (tries >= IOCTLRETRIES) {
2587c478bd9Sstevel@tonic-gate 		break;
2597c478bd9Sstevel@tonic-gate 	    }
2607c478bd9Sstevel@tonic-gate 	    (void) sleep(IOCTLRETRYINTERVAL);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (status < 0) {
2647c478bd9Sstevel@tonic-gate 	    return (0);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	return (1);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /* return 1 if there is media, 0 if not. */
2717c478bd9Sstevel@tonic-gate int
2727c478bd9Sstevel@tonic-gate media_read_name(disk_t *dp, char *mname, int size)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	mname[0] = 0;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (!dp->removable) {
2777c478bd9Sstevel@tonic-gate 	    /* not removable, so media name is devid */
2787c478bd9Sstevel@tonic-gate 	    if (dp->device_id != NULL) {
2797c478bd9Sstevel@tonic-gate 		(void) strlcpy(mname, dp->device_id, size);
2807c478bd9Sstevel@tonic-gate 	    }
2817c478bd9Sstevel@tonic-gate 	    return (1);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/* This is a removable media drive. */
285*18c2aff7Sartem 	return (get_rmm_name(dp, mname, size));
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static descriptor_t **
2897c478bd9Sstevel@tonic-gate apply_filter(descriptor_t **media, int filter[], int *errp)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	descriptor_t	**found;
2927c478bd9Sstevel@tonic-gate 	int		i;
2937c478bd9Sstevel@tonic-gate 	int		cnt = 0;
2947c478bd9Sstevel@tonic-gate 	int		pos;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/* count the number of media in the snapshot */
2977c478bd9Sstevel@tonic-gate 	for (i = 0; media[i]; i++) {
2987c478bd9Sstevel@tonic-gate 	    cnt++;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	found = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
3027c478bd9Sstevel@tonic-gate 	if (found == NULL) {
3037c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
3047c478bd9Sstevel@tonic-gate 	    cache_free_descriptors(media);
3057c478bd9Sstevel@tonic-gate 	    return (NULL);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	pos = 0;
3097c478bd9Sstevel@tonic-gate 	for (i = 0; media[i]; i++) {
3107c478bd9Sstevel@tonic-gate 	    int			fd;
3117c478bd9Sstevel@tonic-gate 	    struct dk_minfo	minfo;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	    if ((fd = drive_open_disk(media[i]->p.disk, NULL, 0)) < 0) {
3147c478bd9Sstevel@tonic-gate 		continue;
3157c478bd9Sstevel@tonic-gate 	    }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	    if (media_read_info(fd, &minfo)) {
3187c478bd9Sstevel@tonic-gate 		int	mtype;
3197c478bd9Sstevel@tonic-gate 		int	j;
3207c478bd9Sstevel@tonic-gate 		int	match;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		mtype = get_media_type(minfo.dki_media_type);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		match = 0;
3257c478bd9Sstevel@tonic-gate 		for (j = 0; filter[j] != DM_FILTER_END; j++) {
3267c478bd9Sstevel@tonic-gate 		    if (mtype == filter[j]) {
3277c478bd9Sstevel@tonic-gate 			found[pos++] = media[i];
3287c478bd9Sstevel@tonic-gate 			match = 1;
3297c478bd9Sstevel@tonic-gate 			break;
3307c478bd9Sstevel@tonic-gate 		    }
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 		if (!match) {
3347c478bd9Sstevel@tonic-gate 		    cache_free_descriptor(media[i]);
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 	    }
3377c478bd9Sstevel@tonic-gate 	    (void) close(fd);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	found[pos] = NULL;
3407c478bd9Sstevel@tonic-gate 	free(media);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	*errp = 0;
3437c478bd9Sstevel@tonic-gate 	return (found);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /* return 1 if the media descriptor is still valid, 0 if not. */
3477c478bd9Sstevel@tonic-gate static int
3487c478bd9Sstevel@tonic-gate desc_ok(descriptor_t *dp)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	/* First verify the media name for removable media */
3517c478bd9Sstevel@tonic-gate 	if (dp->p.disk->removable) {
3527c478bd9Sstevel@tonic-gate 	    char	mname[MAXPATHLEN];
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	    if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
3557c478bd9Sstevel@tonic-gate 		return (0);
3567c478bd9Sstevel@tonic-gate 	    }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	    if (mname[0] == 0) {
3597c478bd9Sstevel@tonic-gate 		return (libdiskmgt_str_eq(dp->name, NULL));
3607c478bd9Sstevel@tonic-gate 	    } else {
3617c478bd9Sstevel@tonic-gate 		return (libdiskmgt_str_eq(dp->name, mname));
3627c478bd9Sstevel@tonic-gate 	    }
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	return (1);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate static int
3697c478bd9Sstevel@tonic-gate get_attrs(disk_t *dp, int fd, nvlist_t *attrs)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	struct dk_minfo	minfo;
3727c478bd9Sstevel@tonic-gate 	struct dk_geom	geometry;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (fd < 0) {
3757c478bd9Sstevel@tonic-gate 	    return (ENODEV);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	bzero(&minfo, sizeof (struct dk_minfo));
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/* The first thing to do is read the media */
3817c478bd9Sstevel@tonic-gate 	if (!media_read_info(fd, &minfo)) {
3827c478bd9Sstevel@tonic-gate 	    return (ENODEV);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (partition_has_fdisk(dp, fd)) {
3867c478bd9Sstevel@tonic-gate 	    if (nvlist_add_boolean(attrs, DM_FDISK) != 0) {
3877c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3887c478bd9Sstevel@tonic-gate 	    }
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (dp->removable) {
3927c478bd9Sstevel@tonic-gate 	    if (nvlist_add_boolean(attrs, DM_REMOVABLE) != 0) {
3937c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3947c478bd9Sstevel@tonic-gate 	    }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	    if (nvlist_add_boolean(attrs, DM_LOADED) != 0) {
3977c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3987c478bd9Sstevel@tonic-gate 	    }
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint64(attrs, DM_SIZE, minfo.dki_capacity) != 0) {
4027c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BLOCKSIZE, minfo.dki_lbsize) != 0) {
4067c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_MTYPE,
4107c478bd9Sstevel@tonic-gate 	    get_media_type(minfo.dki_media_type)) != 0) {
4117c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/* only for disks < 1TB */
4157c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCGGEOM, &geometry) >= 0) {
4167c478bd9Sstevel@tonic-gate 	    struct vtoc	vtoc;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint64(attrs, DM_START, 0) != 0) {
4197c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4207c478bd9Sstevel@tonic-gate 	    }
4217c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint64(attrs, DM_NACCESSIBLE,
4227c478bd9Sstevel@tonic-gate 		geometry.dkg_ncyl * geometry.dkg_nhead * geometry.dkg_nsect)
4237c478bd9Sstevel@tonic-gate 		!= 0) {
4247c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4257c478bd9Sstevel@tonic-gate 	    }
4267c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_NCYLINDERS, geometry.dkg_ncyl)
4277c478bd9Sstevel@tonic-gate 		!= 0) {
4287c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4297c478bd9Sstevel@tonic-gate 	    }
4307c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_NPHYSCYLINDERS, geometry.dkg_pcyl)
4317c478bd9Sstevel@tonic-gate 		!= 0) {
4327c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4337c478bd9Sstevel@tonic-gate 	    }
4347c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_NALTCYLINDERS, geometry.dkg_acyl)
4357c478bd9Sstevel@tonic-gate 		!= 0) {
4367c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4377c478bd9Sstevel@tonic-gate 	    }
4387c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_NHEADS, geometry.dkg_nhead) != 0) {
4397c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4407c478bd9Sstevel@tonic-gate 	    }
4417c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_NSECTORS, geometry.dkg_nsect)
4427c478bd9Sstevel@tonic-gate 		!= 0) {
4437c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4447c478bd9Sstevel@tonic-gate 	    }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	    if (read_vtoc(fd, &vtoc) >= 0 && vtoc.v_volume[0] != 0) {
4477c478bd9Sstevel@tonic-gate 		char	label[LEN_DKL_VVOL + 1];
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 		(void) snprintf(label, sizeof (label), "%.*s", LEN_DKL_VVOL,
4507c478bd9Sstevel@tonic-gate 		    vtoc.v_volume);
4517c478bd9Sstevel@tonic-gate 		if (nvlist_add_string(attrs, DM_LABEL, label) != 0) {
4527c478bd9Sstevel@tonic-gate 		    return (ENOMEM);
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 	    }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	} else {
4577c478bd9Sstevel@tonic-gate 	    /* check for disks > 1TB for accessible size */
4587c478bd9Sstevel@tonic-gate 	    struct dk_gpt	*efip;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	    if (efi_alloc_and_read(fd, &efip) >= 0) {
4617c478bd9Sstevel@tonic-gate 		diskaddr_t	p8size = 0;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(attrs, DM_EFI) != 0) {
4647c478bd9Sstevel@tonic-gate 		    return (ENOMEM);
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint64(attrs, DM_START, efip->efi_first_u_lba)
4677c478bd9Sstevel@tonic-gate 		    != 0) {
4687c478bd9Sstevel@tonic-gate 		    return (ENOMEM);
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 		/* partition 8 is reserved on EFI labels */
4717c478bd9Sstevel@tonic-gate 		if (efip->efi_nparts >= 9) {
4727c478bd9Sstevel@tonic-gate 		    p8size = efip->efi_parts[8].p_size;
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 		if (nvlist_add_uint64(attrs, DM_NACCESSIBLE,
4757c478bd9Sstevel@tonic-gate 		    (efip->efi_last_u_lba - p8size) - efip->efi_first_u_lba)
4767c478bd9Sstevel@tonic-gate 		    != 0) {
4777c478bd9Sstevel@tonic-gate 		    efi_free(efip);
4787c478bd9Sstevel@tonic-gate 		    return (ENOMEM);
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 		efi_free(efip);
4817c478bd9Sstevel@tonic-gate 	    }
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* This ioctl seems to be mainly for intel-based drives. */
4857c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCG_PHYGEOM, &geometry) >= 0) {
4867c478bd9Sstevel@tonic-gate 	    if (nvlist_add_uint32(attrs, DM_NACTUALCYLINDERS, geometry.dkg_ncyl)
4877c478bd9Sstevel@tonic-gate 		!= 0) {
4887c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4897c478bd9Sstevel@tonic-gate 	    }
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	return (0);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate static int
4967c478bd9Sstevel@tonic-gate get_media_type(uint_t media_type)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	switch (media_type) {
4997c478bd9Sstevel@tonic-gate 	case DK_UNKNOWN:
5007c478bd9Sstevel@tonic-gate 	    return (DM_MT_UNKNOWN);
5017c478bd9Sstevel@tonic-gate 	case DK_MO_ERASABLE:
5027c478bd9Sstevel@tonic-gate 	    return (DM_MT_MO_ERASABLE);
5037c478bd9Sstevel@tonic-gate 	case DK_MO_WRITEONCE:
5047c478bd9Sstevel@tonic-gate 	    return (DM_MT_MO_WRITEONCE);
5057c478bd9Sstevel@tonic-gate 	case DK_AS_MO:
5067c478bd9Sstevel@tonic-gate 	    return (DM_MT_AS_MO);
5077c478bd9Sstevel@tonic-gate 	case DK_CDROM:
5087c478bd9Sstevel@tonic-gate 	    return (DM_MT_CDROM);
5097c478bd9Sstevel@tonic-gate 	case DK_CDR:
5107c478bd9Sstevel@tonic-gate 	    return (DM_MT_CDR);
5117c478bd9Sstevel@tonic-gate 	case DK_CDRW:
5127c478bd9Sstevel@tonic-gate 	    return (DM_MT_CDRW);
5137c478bd9Sstevel@tonic-gate 	case DK_DVDROM:
5147c478bd9Sstevel@tonic-gate 	    return (DM_MT_DVDROM);
5157c478bd9Sstevel@tonic-gate 	case DK_DVDR:
5167c478bd9Sstevel@tonic-gate 	    return (DM_MT_DVDR);
5177c478bd9Sstevel@tonic-gate 	case DK_DVDRAM:
5187c478bd9Sstevel@tonic-gate 	    return (DM_MT_DVDRAM);
5197c478bd9Sstevel@tonic-gate 	case DK_FIXED_DISK:
5207c478bd9Sstevel@tonic-gate 	    return (DM_MT_FIXED);
5217c478bd9Sstevel@tonic-gate 	case DK_FLOPPY:
5227c478bd9Sstevel@tonic-gate 	    return (DM_MT_FLOPPY);
5237c478bd9Sstevel@tonic-gate 	case DK_ZIP:
5247c478bd9Sstevel@tonic-gate 	    return (DM_MT_ZIP);
5257c478bd9Sstevel@tonic-gate 	case DK_JAZ:
5267c478bd9Sstevel@tonic-gate 	    return (DM_MT_JAZ);
5277c478bd9Sstevel@tonic-gate 	default:
5287c478bd9Sstevel@tonic-gate 	    return (DM_MT_UNKNOWN);
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate }
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate /*
533*18c2aff7Sartem  * This function handles removable media.
5347c478bd9Sstevel@tonic-gate  */
5357c478bd9Sstevel@tonic-gate static int
536*18c2aff7Sartem get_rmm_name(disk_t *dp, char *mname, int size)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	int		loaded;
5397c478bd9Sstevel@tonic-gate 	int		fd;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	loaded = 0;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if ((fd = drive_open_disk(dp, NULL, 0)) >= 0) {
5447c478bd9Sstevel@tonic-gate 	    struct dk_minfo minfo;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	    if ((loaded = media_read_info(fd, &minfo))) {
5477c478bd9Sstevel@tonic-gate 		struct vtoc vtoc;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		if (read_vtoc(fd, &vtoc) >= 0) {
5507c478bd9Sstevel@tonic-gate 		    if (vtoc.v_volume[0] != NULL) {
5517c478bd9Sstevel@tonic-gate 			if (LEN_DKL_VVOL < size) {
5527c478bd9Sstevel@tonic-gate 			    (void) strlcpy(mname, vtoc.v_volume, LEN_DKL_VVOL);
5537c478bd9Sstevel@tonic-gate 			} else {
5547c478bd9Sstevel@tonic-gate 			    (void) strlcpy(mname, vtoc.v_volume, size);
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 		    }
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 	    }
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	    (void) close(fd);
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	return (loaded);
5647c478bd9Sstevel@tonic-gate }
565