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