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*69fb9702Smike_s  * 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 <sys/sunddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
397c478bd9Sstevel@tonic-gate #include "disks_private.h"
407c478bd9Sstevel@tonic-gate #include "partition.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate extern dm_desc_type_t drive_assoc_types[];
447c478bd9Sstevel@tonic-gate extern dm_desc_type_t bus_assoc_types[];
457c478bd9Sstevel@tonic-gate extern dm_desc_type_t controller_assoc_types[];
467c478bd9Sstevel@tonic-gate extern dm_desc_type_t media_assoc_types[];
477c478bd9Sstevel@tonic-gate extern dm_desc_type_t slice_assoc_types[];
487c478bd9Sstevel@tonic-gate extern dm_desc_type_t partition_assoc_types[];
497c478bd9Sstevel@tonic-gate extern dm_desc_type_t path_assoc_types[];
507c478bd9Sstevel@tonic-gate extern dm_desc_type_t alias_assoc_types[];
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static dm_descriptor_t *ptr_array_to_desc_array(descriptor_t **ptrs, int *errp);
537c478bd9Sstevel@tonic-gate static descriptor_t **desc_array_to_ptr_array(dm_descriptor_t *da, int *errp);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate void
567c478bd9Sstevel@tonic-gate dm_free_descriptor(dm_descriptor_t desc)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	descriptor_t	*dp;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (desc == NULL) {
617c478bd9Sstevel@tonic-gate 	    return;
627c478bd9Sstevel@tonic-gate 	}
63*69fb9702Smike_s 	dp = (descriptor_t *)(uintptr_t)desc;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	cache_wlock();
667c478bd9Sstevel@tonic-gate 	cache_free_descriptor(dp);
677c478bd9Sstevel@tonic-gate 	cache_unlock();
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate void
717c478bd9Sstevel@tonic-gate dm_free_descriptors(dm_descriptor_t *desc_list)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	descriptor_t	**dp;
747c478bd9Sstevel@tonic-gate 	int		error;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (desc_list == NULL) {
777c478bd9Sstevel@tonic-gate 	    return;
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	dp = desc_array_to_ptr_array(desc_list, &error);
807c478bd9Sstevel@tonic-gate 	if (error != 0) {
817c478bd9Sstevel@tonic-gate 	    free(desc_list);
827c478bd9Sstevel@tonic-gate 	    return;
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	cache_wlock();
867c478bd9Sstevel@tonic-gate 	cache_free_descriptors(dp);
877c478bd9Sstevel@tonic-gate 	cache_unlock();
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
917c478bd9Sstevel@tonic-gate void
927c478bd9Sstevel@tonic-gate dm_free_name(char *name)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	free(name);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate dm_descriptor_t *
987c478bd9Sstevel@tonic-gate dm_get_associated_descriptors(dm_descriptor_t desc, dm_desc_type_t type,
997c478bd9Sstevel@tonic-gate     int *errp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	descriptor_t **descs = NULL;
1027c478bd9Sstevel@tonic-gate 	descriptor_t  *dp;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 
105*69fb9702Smike_s 	dp = (descriptor_t *)(uintptr_t)desc;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	cache_wlock();
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (!cache_is_valid_desc(dp)) {
1107c478bd9Sstevel@tonic-gate 	    cache_unlock();
1117c478bd9Sstevel@tonic-gate 	    *errp = EBADF;
1127c478bd9Sstevel@tonic-gate 	    return (NULL);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/* verify that the descriptor is still valid */
1167c478bd9Sstevel@tonic-gate 	if (dp->p.generic == NULL) {
1177c478bd9Sstevel@tonic-gate 	    cache_unlock();
1187c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
1197c478bd9Sstevel@tonic-gate 	    return (NULL);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	switch (dp->type) {
1237c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
1247c478bd9Sstevel@tonic-gate 	    descs = drive_get_assoc_descriptors(dp, type, errp);
1257c478bd9Sstevel@tonic-gate 	    break;
1267c478bd9Sstevel@tonic-gate 	case DM_BUS:
1277c478bd9Sstevel@tonic-gate 	    descs = bus_get_assoc_descriptors(dp, type, errp);
1287c478bd9Sstevel@tonic-gate 	    break;
1297c478bd9Sstevel@tonic-gate 	case DM_CONTROLLER:
1307c478bd9Sstevel@tonic-gate 	    descs = controller_get_assoc_descriptors(dp, type, errp);
1317c478bd9Sstevel@tonic-gate 	    break;
1327c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
1337c478bd9Sstevel@tonic-gate 	    descs = media_get_assoc_descriptors(dp, type, errp);
1347c478bd9Sstevel@tonic-gate 	    break;
1357c478bd9Sstevel@tonic-gate 	case DM_SLICE:
1367c478bd9Sstevel@tonic-gate 	    descs = slice_get_assoc_descriptors(dp, type, errp);
1377c478bd9Sstevel@tonic-gate 	    break;
1387c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
1397c478bd9Sstevel@tonic-gate 	    descs = partition_get_assoc_descriptors(dp, type, errp);
1407c478bd9Sstevel@tonic-gate 	    break;
1417c478bd9Sstevel@tonic-gate 	case DM_PATH:
1427c478bd9Sstevel@tonic-gate 	    descs = path_get_assoc_descriptors(dp, type, errp);
1437c478bd9Sstevel@tonic-gate 	    break;
1447c478bd9Sstevel@tonic-gate 	case DM_ALIAS:
1457c478bd9Sstevel@tonic-gate 	    descs = alias_get_assoc_descriptors(dp, type, errp);
1467c478bd9Sstevel@tonic-gate 	    break;
1477c478bd9Sstevel@tonic-gate 	default:
1487c478bd9Sstevel@tonic-gate 	    *errp = EINVAL;
1497c478bd9Sstevel@tonic-gate 	    break;
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	cache_unlock();
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	return (ptr_array_to_desc_array(descs, errp));
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate dm_desc_type_t *
1587c478bd9Sstevel@tonic-gate dm_get_associated_types(dm_desc_type_t type)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	switch (type) {
1617c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
1627c478bd9Sstevel@tonic-gate 	    return (drive_assoc_types);
1637c478bd9Sstevel@tonic-gate 	case DM_BUS:
1647c478bd9Sstevel@tonic-gate 	    return (bus_assoc_types);
1657c478bd9Sstevel@tonic-gate 	case DM_CONTROLLER:
1667c478bd9Sstevel@tonic-gate 	    return (controller_assoc_types);
1677c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
1687c478bd9Sstevel@tonic-gate 	    return (media_assoc_types);
1697c478bd9Sstevel@tonic-gate 	case DM_SLICE:
1707c478bd9Sstevel@tonic-gate 	    return (slice_assoc_types);
1717c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
1727c478bd9Sstevel@tonic-gate 	    return (partition_assoc_types);
1737c478bd9Sstevel@tonic-gate 	case DM_PATH:
1747c478bd9Sstevel@tonic-gate 	    return (path_assoc_types);
1757c478bd9Sstevel@tonic-gate 	case DM_ALIAS:
1767c478bd9Sstevel@tonic-gate 	    return (alias_assoc_types);
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	return (NULL);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate nvlist_t *
1837c478bd9Sstevel@tonic-gate dm_get_attributes(dm_descriptor_t desc, int *errp)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	descriptor_t	*dp;
1867c478bd9Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
189*69fb9702Smike_s 	dp = (descriptor_t *)(uintptr_t)desc;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	cache_rlock();
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (!cache_is_valid_desc(dp)) {
1947c478bd9Sstevel@tonic-gate 	    cache_unlock();
1957c478bd9Sstevel@tonic-gate 	    *errp = EBADF;
1967c478bd9Sstevel@tonic-gate 	    return (NULL);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/* verify that the descriptor is still valid */
2007c478bd9Sstevel@tonic-gate 	if (dp->p.generic == NULL) {
2017c478bd9Sstevel@tonic-gate 	    cache_unlock();
2027c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
2037c478bd9Sstevel@tonic-gate 	    return (NULL);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	switch (dp->type) {
2077c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
2087c478bd9Sstevel@tonic-gate 	    attrs = drive_get_attributes(dp, errp);
2097c478bd9Sstevel@tonic-gate 	    break;
2107c478bd9Sstevel@tonic-gate 	case DM_BUS:
2117c478bd9Sstevel@tonic-gate 	    attrs = bus_get_attributes(dp, errp);
2127c478bd9Sstevel@tonic-gate 	    break;
2137c478bd9Sstevel@tonic-gate 	case DM_CONTROLLER:
2147c478bd9Sstevel@tonic-gate 	    attrs = controller_get_attributes(dp, errp);
2157c478bd9Sstevel@tonic-gate 	    break;
2167c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
2177c478bd9Sstevel@tonic-gate 	    attrs = media_get_attributes(dp, errp);
2187c478bd9Sstevel@tonic-gate 	    break;
2197c478bd9Sstevel@tonic-gate 	case DM_SLICE:
2207c478bd9Sstevel@tonic-gate 	    attrs = slice_get_attributes(dp, errp);
2217c478bd9Sstevel@tonic-gate 	    break;
2227c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
2237c478bd9Sstevel@tonic-gate 	    attrs = partition_get_attributes(dp, errp);
2247c478bd9Sstevel@tonic-gate 	    break;
2257c478bd9Sstevel@tonic-gate 	case DM_PATH:
2267c478bd9Sstevel@tonic-gate 	    attrs = path_get_attributes(dp, errp);
2277c478bd9Sstevel@tonic-gate 	    break;
2287c478bd9Sstevel@tonic-gate 	case DM_ALIAS:
2297c478bd9Sstevel@tonic-gate 	    attrs = alias_get_attributes(dp, errp);
2307c478bd9Sstevel@tonic-gate 	    break;
2317c478bd9Sstevel@tonic-gate 	default:
2327c478bd9Sstevel@tonic-gate 	    *errp = EINVAL;
2337c478bd9Sstevel@tonic-gate 	    break;
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	cache_unlock();
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (attrs);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate dm_descriptor_t
2427c478bd9Sstevel@tonic-gate dm_get_descriptor_by_name(dm_desc_type_t desc_type, char *name, int *errp)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	dm_descriptor_t desc = NULL;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	cache_wlock();
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	switch (desc_type) {
2507c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
2517c478bd9Sstevel@tonic-gate 	    desc = (uintptr_t)drive_get_descriptor_by_name(name, errp);
2527c478bd9Sstevel@tonic-gate 	    break;
2537c478bd9Sstevel@tonic-gate 	case DM_BUS:
2547c478bd9Sstevel@tonic-gate 	    desc = (uintptr_t)bus_get_descriptor_by_name(name, errp);
2557c478bd9Sstevel@tonic-gate 	    break;
2567c478bd9Sstevel@tonic-gate 	case DM_CONTROLLER:
2577c478bd9Sstevel@tonic-gate 	    desc = (uintptr_t)controller_get_descriptor_by_name(name,
2587c478bd9Sstevel@tonic-gate 		errp);
2597c478bd9Sstevel@tonic-gate 	    break;
2607c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
2617c478bd9Sstevel@tonic-gate 	    desc = (uintptr_t)media_get_descriptor_by_name(name, errp);
2627c478bd9Sstevel@tonic-gate 	    break;
2637c478bd9Sstevel@tonic-gate 	case DM_SLICE:
2647c478bd9Sstevel@tonic-gate 	    desc = (uintptr_t)slice_get_descriptor_by_name(name, errp);
2657c478bd9Sstevel@tonic-gate 	    break;
2667c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
2677c478bd9Sstevel@tonic-gate 	    desc = (uintptr_t)partition_get_descriptor_by_name(name,
2687c478bd9Sstevel@tonic-gate 		errp);
2697c478bd9Sstevel@tonic-gate 	    break;
2707c478bd9Sstevel@tonic-gate 	case DM_PATH:
2717c478bd9Sstevel@tonic-gate 	    desc = (uintptr_t)path_get_descriptor_by_name(name, errp);
2727c478bd9Sstevel@tonic-gate 	    break;
2737c478bd9Sstevel@tonic-gate 	case DM_ALIAS:
2747c478bd9Sstevel@tonic-gate 	    desc = (uintptr_t)alias_get_descriptor_by_name(name, errp);
2757c478bd9Sstevel@tonic-gate 	    break;
2767c478bd9Sstevel@tonic-gate 	default:
2777c478bd9Sstevel@tonic-gate 	    *errp = EINVAL;
2787c478bd9Sstevel@tonic-gate 	    break;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	cache_unlock();
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	return (desc);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate dm_descriptor_t *
2877c478bd9Sstevel@tonic-gate dm_get_descriptors(dm_desc_type_t type, int filter[], int *errp)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	descriptor_t **descs = NULL;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	cache_wlock();
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	switch (type) {
2957c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
2967c478bd9Sstevel@tonic-gate 	    descs = drive_get_descriptors(filter, errp);
2977c478bd9Sstevel@tonic-gate 	    break;
2987c478bd9Sstevel@tonic-gate 	case DM_BUS:
2997c478bd9Sstevel@tonic-gate 	    descs = bus_get_descriptors(filter, errp);
3007c478bd9Sstevel@tonic-gate 	    break;
3017c478bd9Sstevel@tonic-gate 	case DM_CONTROLLER:
3027c478bd9Sstevel@tonic-gate 	    descs = controller_get_descriptors(filter, errp);
3037c478bd9Sstevel@tonic-gate 	    break;
3047c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
3057c478bd9Sstevel@tonic-gate 	    descs = media_get_descriptors(filter, errp);
3067c478bd9Sstevel@tonic-gate 	    break;
3077c478bd9Sstevel@tonic-gate 	case DM_SLICE:
3087c478bd9Sstevel@tonic-gate 	    descs = slice_get_descriptors(filter, errp);
3097c478bd9Sstevel@tonic-gate 	    break;
3107c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
3117c478bd9Sstevel@tonic-gate 	    descs = partition_get_descriptors(filter, errp);
3127c478bd9Sstevel@tonic-gate 	    break;
3137c478bd9Sstevel@tonic-gate 	case DM_PATH:
3147c478bd9Sstevel@tonic-gate 	    descs = path_get_descriptors(filter, errp);
3157c478bd9Sstevel@tonic-gate 	    break;
3167c478bd9Sstevel@tonic-gate 	case DM_ALIAS:
3177c478bd9Sstevel@tonic-gate 	    descs = alias_get_descriptors(filter, errp);
3187c478bd9Sstevel@tonic-gate 	    break;
3197c478bd9Sstevel@tonic-gate 	default:
3207c478bd9Sstevel@tonic-gate 	    *errp = EINVAL;
3217c478bd9Sstevel@tonic-gate 	    break;
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	cache_unlock();
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	return (ptr_array_to_desc_array(descs, errp));
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate char *
3307c478bd9Sstevel@tonic-gate dm_get_name(dm_descriptor_t desc, int *errp)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	descriptor_t	*dp;
3337c478bd9Sstevel@tonic-gate 	char		*nm = NULL;
3347c478bd9Sstevel@tonic-gate 	char		*name = NULL;
3357c478bd9Sstevel@tonic-gate 
336*69fb9702Smike_s 	dp = (descriptor_t *)(uintptr_t)desc;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	cache_rlock();
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	if (!cache_is_valid_desc(dp)) {
3417c478bd9Sstevel@tonic-gate 	    cache_unlock();
3427c478bd9Sstevel@tonic-gate 	    *errp = EBADF;
3437c478bd9Sstevel@tonic-gate 	    return (NULL);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/* verify that the descriptor is still valid */
3477c478bd9Sstevel@tonic-gate 	if (dp->p.generic == NULL) {
3487c478bd9Sstevel@tonic-gate 	    cache_unlock();
3497c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
3507c478bd9Sstevel@tonic-gate 	    return (NULL);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	switch (dp->type) {
3547c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
3557c478bd9Sstevel@tonic-gate 	    nm = (drive_get_name(dp));
3567c478bd9Sstevel@tonic-gate 	    break;
3577c478bd9Sstevel@tonic-gate 	case DM_BUS:
3587c478bd9Sstevel@tonic-gate 	    nm = (bus_get_name(dp));
3597c478bd9Sstevel@tonic-gate 	    break;
3607c478bd9Sstevel@tonic-gate 	case DM_CONTROLLER:
3617c478bd9Sstevel@tonic-gate 	    nm = (controller_get_name(dp));
3627c478bd9Sstevel@tonic-gate 	    break;
3637c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
3647c478bd9Sstevel@tonic-gate 	    nm = (media_get_name(dp));
3657c478bd9Sstevel@tonic-gate 	    break;
3667c478bd9Sstevel@tonic-gate 	case DM_SLICE:
3677c478bd9Sstevel@tonic-gate 	    nm = (slice_get_name(dp));
3687c478bd9Sstevel@tonic-gate 	    break;
3697c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
3707c478bd9Sstevel@tonic-gate 	    nm = (partition_get_name(dp));
3717c478bd9Sstevel@tonic-gate 	    break;
3727c478bd9Sstevel@tonic-gate 	case DM_PATH:
3737c478bd9Sstevel@tonic-gate 	    nm = (path_get_name(dp));
3747c478bd9Sstevel@tonic-gate 	    break;
3757c478bd9Sstevel@tonic-gate 	case DM_ALIAS:
3767c478bd9Sstevel@tonic-gate 	    nm = (alias_get_name(dp));
3777c478bd9Sstevel@tonic-gate 	    break;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	cache_unlock();
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	*errp = 0;
3837c478bd9Sstevel@tonic-gate 	if (nm != NULL) {
3847c478bd9Sstevel@tonic-gate 	    name = strdup(nm);
3857c478bd9Sstevel@tonic-gate 	    if (name == NULL) {
3867c478bd9Sstevel@tonic-gate 		*errp = ENOMEM;
3877c478bd9Sstevel@tonic-gate 		return (NULL);
3887c478bd9Sstevel@tonic-gate 	    }
3897c478bd9Sstevel@tonic-gate 	    return (name);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 	return (NULL);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate nvlist_t *
3957c478bd9Sstevel@tonic-gate dm_get_stats(dm_descriptor_t desc, int stat_type, int *errp)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	descriptor_t  *dp;
3987c478bd9Sstevel@tonic-gate 	nvlist_t	*stats = NULL;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 
401*69fb9702Smike_s 	dp = (descriptor_t *)(uintptr_t)desc;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	cache_rlock();
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (!cache_is_valid_desc(dp)) {
4067c478bd9Sstevel@tonic-gate 	    cache_unlock();
4077c478bd9Sstevel@tonic-gate 	    *errp = EBADF;
4087c478bd9Sstevel@tonic-gate 	    return (NULL);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/* verify that the descriptor is still valid */
4127c478bd9Sstevel@tonic-gate 	if (dp->p.generic == NULL) {
4137c478bd9Sstevel@tonic-gate 	    cache_unlock();
4147c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
4157c478bd9Sstevel@tonic-gate 	    return (NULL);
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	switch (dp->type) {
4197c478bd9Sstevel@tonic-gate 	case DM_DRIVE:
4207c478bd9Sstevel@tonic-gate 	    stats = drive_get_stats(dp, stat_type, errp);
4217c478bd9Sstevel@tonic-gate 	    break;
4227c478bd9Sstevel@tonic-gate 	case DM_BUS:
4237c478bd9Sstevel@tonic-gate 	    stats = bus_get_stats(dp, stat_type, errp);
4247c478bd9Sstevel@tonic-gate 	    break;
4257c478bd9Sstevel@tonic-gate 	case DM_CONTROLLER:
4267c478bd9Sstevel@tonic-gate 	    stats = controller_get_stats(dp, stat_type, errp);
4277c478bd9Sstevel@tonic-gate 	    break;
4287c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
4297c478bd9Sstevel@tonic-gate 	    stats = media_get_stats(dp, stat_type, errp);
4307c478bd9Sstevel@tonic-gate 	    break;
4317c478bd9Sstevel@tonic-gate 	case DM_SLICE:
4327c478bd9Sstevel@tonic-gate 	    stats = slice_get_stats(dp, stat_type, errp);
4337c478bd9Sstevel@tonic-gate 	    break;
4347c478bd9Sstevel@tonic-gate 	case DM_PARTITION:
4357c478bd9Sstevel@tonic-gate 	    stats = partition_get_stats(dp, stat_type, errp);
4367c478bd9Sstevel@tonic-gate 	    break;
4377c478bd9Sstevel@tonic-gate 	case DM_PATH:
4387c478bd9Sstevel@tonic-gate 	    stats = path_get_stats(dp, stat_type, errp);
4397c478bd9Sstevel@tonic-gate 	    break;
4407c478bd9Sstevel@tonic-gate 	case DM_ALIAS:
4417c478bd9Sstevel@tonic-gate 	    stats = alias_get_stats(dp, stat_type, errp);
4427c478bd9Sstevel@tonic-gate 	    break;
4437c478bd9Sstevel@tonic-gate 	default:
4447c478bd9Sstevel@tonic-gate 	    *errp = EINVAL;
4457c478bd9Sstevel@tonic-gate 	    break;
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	cache_unlock();
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	return (stats);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate dm_desc_type_t
4547c478bd9Sstevel@tonic-gate dm_get_type(dm_descriptor_t desc)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	descriptor_t  *dp;
4577c478bd9Sstevel@tonic-gate 
458*69fb9702Smike_s 	dp = (descriptor_t *)(uintptr_t)desc;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	cache_rlock();
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (!cache_is_valid_desc(dp)) {
4637c478bd9Sstevel@tonic-gate 	    cache_unlock();
4647c478bd9Sstevel@tonic-gate 	    return (-1);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	cache_unlock();
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	return (dp->type);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate void
4737c478bd9Sstevel@tonic-gate libdiskmgt_add_str(nvlist_t *attrs, char *name, char *val, int *errp)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	if (*errp == 0) {
4767c478bd9Sstevel@tonic-gate 		*errp = nvlist_add_string(attrs, name, val);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate descriptor_t **
4817c478bd9Sstevel@tonic-gate libdiskmgt_empty_desc_array(int *errp)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	descriptor_t	**empty;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	empty = (descriptor_t **)calloc(1, sizeof (descriptor_t *));
4867c478bd9Sstevel@tonic-gate 	if (empty == NULL) {
4877c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
4887c478bd9Sstevel@tonic-gate 	    return (NULL);
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 	empty[0] = NULL;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	*errp = 0;
4937c478bd9Sstevel@tonic-gate 	return (empty);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate void
4977c478bd9Sstevel@tonic-gate libdiskmgt_init_debug()
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	char	*valp;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if ((valp = getenv(DM_DEBUG)) != NULL) {
5027c478bd9Sstevel@tonic-gate 	    dm_debug = atoi(valp);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate int
5077c478bd9Sstevel@tonic-gate libdiskmgt_str_eq(char *nm1, char *nm2)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	if (nm1 == NULL) {
5107c478bd9Sstevel@tonic-gate 	    if (dm_debug) {
5117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "WARNING: str_eq nm1 NULL\n");
5127c478bd9Sstevel@tonic-gate 	    }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	    if (nm2 == NULL) {
5157c478bd9Sstevel@tonic-gate 		return (1);
5167c478bd9Sstevel@tonic-gate 	    } else {
5177c478bd9Sstevel@tonic-gate 		return (0);
5187c478bd9Sstevel@tonic-gate 	    }
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* nm1 != NULL */
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (nm2 == NULL) {
5247c478bd9Sstevel@tonic-gate 	    if (dm_debug) {
5257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "WARNING: str_eq nm2 NULL\n");
5267c478bd9Sstevel@tonic-gate 	    }
5277c478bd9Sstevel@tonic-gate 	    return (0);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (strcmp(nm1, nm2) == 0) {
5317c478bd9Sstevel@tonic-gate 	    return (1);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	return (0);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5387c478bd9Sstevel@tonic-gate static descriptor_t **
5397c478bd9Sstevel@tonic-gate desc_array_to_ptr_array(dm_descriptor_t *descs, int *errp)
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate #ifdef _LP64
5427c478bd9Sstevel@tonic-gate 	return ((descriptor_t **)descs);
5437c478bd9Sstevel@tonic-gate #else
5447c478bd9Sstevel@tonic-gate 	/* convert the 64 bit descriptors to 32 bit ptrs */
5457c478bd9Sstevel@tonic-gate 	int	cnt;
5467c478bd9Sstevel@tonic-gate 	int	i;
5477c478bd9Sstevel@tonic-gate 	descriptor_t **da;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	for (cnt = 0; descs[cnt]; cnt++);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	da = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
5527c478bd9Sstevel@tonic-gate 	if (da == NULL) {
5537c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
5547c478bd9Sstevel@tonic-gate 	    return (NULL);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	for (i = 0; descs[i]; i++) {
558*69fb9702Smike_s 	    da[i] = (descriptor_t *)(uintptr_t)descs[i];
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	*errp = 0;
5617c478bd9Sstevel@tonic-gate 	free(descs);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	return (da);
5647c478bd9Sstevel@tonic-gate #endif
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5687c478bd9Sstevel@tonic-gate static dm_descriptor_t *
5697c478bd9Sstevel@tonic-gate ptr_array_to_desc_array(descriptor_t **ptrs, int *errp)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate #ifdef _LP64
5727c478bd9Sstevel@tonic-gate 	return ((dm_descriptor_t *)ptrs);
5737c478bd9Sstevel@tonic-gate #else
5747c478bd9Sstevel@tonic-gate 	/* convert the 32 bit ptrs to the 64 bit descriptors */
5757c478bd9Sstevel@tonic-gate 	int	cnt;
5767c478bd9Sstevel@tonic-gate 	int	i;
5777c478bd9Sstevel@tonic-gate 	dm_descriptor_t *da;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (*errp != 0 || ptrs == NULL) {
5807c478bd9Sstevel@tonic-gate 	    return (NULL);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	for (cnt = 0; ptrs[cnt]; cnt++);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	da = (dm_descriptor_t *)calloc(cnt + 1, sizeof (dm_descriptor_t));
5867c478bd9Sstevel@tonic-gate 	if (da == NULL) {
5877c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
5887c478bd9Sstevel@tonic-gate 	    return (NULL);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	for (i = 0; ptrs[i]; i++) {
5927c478bd9Sstevel@tonic-gate 	    da[i] = (uintptr_t)ptrs[i];
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 	*errp = 0;
5957c478bd9Sstevel@tonic-gate 	free(ptrs);
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	return (da);
5987c478bd9Sstevel@tonic-gate #endif
5997c478bd9Sstevel@tonic-gate }
600