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