10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51896Sjkennedy * Common Development and Distribution License (the "License"). 61896Sjkennedy * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*7464SAndrew.Balfour@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <dlfcn.h> 270Sstevel@tonic-gate #include <meta.h> 280Sstevel@tonic-gate #include <metadyn.h> 290Sstevel@tonic-gate #include <ctype.h> 300Sstevel@tonic-gate #include <dirent.h> 310Sstevel@tonic-gate #include <devid.h> 320Sstevel@tonic-gate #include <sys/param.h> 330Sstevel@tonic-gate #include <sys/scsi/impl/uscsi.h> 340Sstevel@tonic-gate #include <sys/scsi/generic/commands.h> 350Sstevel@tonic-gate #include <sys/scsi/generic/inquiry.h> 360Sstevel@tonic-gate #include <sys/efi_partition.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate typedef struct ctlr_cache { 390Sstevel@tonic-gate char *ctlr_nm; 400Sstevel@tonic-gate int ctlr_ty; 410Sstevel@tonic-gate struct ctlr_cache *ctlr_nx; 420Sstevel@tonic-gate } ctlr_cache_t; 430Sstevel@tonic-gate 440Sstevel@tonic-gate static ctlr_cache_t *ctlr_cache = NULL; 450Sstevel@tonic-gate 460Sstevel@tonic-gate 470Sstevel@tonic-gate /* 480Sstevel@tonic-gate * return set for a device 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate mdsetname_t * 510Sstevel@tonic-gate metagetset( 520Sstevel@tonic-gate mdname_t *np, 530Sstevel@tonic-gate int bypass_daemon, 540Sstevel@tonic-gate md_error_t *ep 550Sstevel@tonic-gate ) 560Sstevel@tonic-gate { 570Sstevel@tonic-gate mdsetname_t *sp; 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* metadevice */ 600Sstevel@tonic-gate if (metaismeta(np)) 610Sstevel@tonic-gate return (metasetnosetname(MD_MIN2SET(meta_getminor(np->dev)), 62*7464SAndrew.Balfour@Sun.COM ep)); 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* regular device */ 650Sstevel@tonic-gate if (meta_is_drive_in_anyset(np->drivenamep, &sp, bypass_daemon, 660Sstevel@tonic-gate ep) != 0) 670Sstevel@tonic-gate return (NULL); 680Sstevel@tonic-gate 690Sstevel@tonic-gate if (sp != NULL) 700Sstevel@tonic-gate return (sp); 710Sstevel@tonic-gate 720Sstevel@tonic-gate return (metasetnosetname(MD_LOCAL_SET, ep)); 730Sstevel@tonic-gate } 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * convert system to md types 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate static void 790Sstevel@tonic-gate meta_geom_to_md( 800Sstevel@tonic-gate struct dk_geom *gp, 810Sstevel@tonic-gate mdgeom_t *mdgp 820Sstevel@tonic-gate ) 830Sstevel@tonic-gate { 840Sstevel@tonic-gate (void) memset(mdgp, '\0', sizeof (*mdgp)); 850Sstevel@tonic-gate mdgp->ncyl = gp->dkg_ncyl; 860Sstevel@tonic-gate mdgp->nhead = gp->dkg_nhead; 870Sstevel@tonic-gate mdgp->nsect = gp->dkg_nsect; 880Sstevel@tonic-gate mdgp->rpm = gp->dkg_rpm; 890Sstevel@tonic-gate mdgp->write_reinstruct = gp->dkg_write_reinstruct; 900Sstevel@tonic-gate mdgp->read_reinstruct = gp->dkg_read_reinstruct; 910Sstevel@tonic-gate mdgp->blk_sz = DEV_BSIZE; 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* 950Sstevel@tonic-gate * convert efi to md types 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate static void 980Sstevel@tonic-gate meta_efi_to_mdgeom(struct dk_gpt *gpt, mdgeom_t *mdgp) 990Sstevel@tonic-gate { 1000Sstevel@tonic-gate (void) memset(mdgp, '\0', sizeof (*mdgp)); 1010Sstevel@tonic-gate mdgp->ncyl = (gpt->efi_last_u_lba - gpt->efi_first_u_lba) / 102*7464SAndrew.Balfour@Sun.COM (MD_EFI_FG_HEADS * MD_EFI_FG_SECTORS); 1030Sstevel@tonic-gate mdgp->nhead = MD_EFI_FG_HEADS; 1040Sstevel@tonic-gate mdgp->nsect = MD_EFI_FG_SECTORS; 1050Sstevel@tonic-gate mdgp->rpm = MD_EFI_FG_RPM; 1060Sstevel@tonic-gate mdgp->write_reinstruct = MD_EFI_FG_WRI; 1070Sstevel@tonic-gate mdgp->read_reinstruct = MD_EFI_FG_RRI; 1080Sstevel@tonic-gate mdgp->blk_sz = DEV_BSIZE; 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate static void 1120Sstevel@tonic-gate meta_efi_to_mdvtoc(struct dk_gpt *gpt, mdvtoc_t *mdvp) 1130Sstevel@tonic-gate { 1140Sstevel@tonic-gate char typename[EFI_PART_NAME_LEN]; 1150Sstevel@tonic-gate uint_t i; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate (void) memset(mdvp, '\0', sizeof (*mdvp)); 1180Sstevel@tonic-gate mdvp->nparts = gpt->efi_nparts; 1190Sstevel@tonic-gate if (mdvp->nparts > MD_MAX_PARTS) 1200Sstevel@tonic-gate return; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate mdvp->first_lba = gpt->efi_first_u_lba; 1230Sstevel@tonic-gate mdvp->last_lba = gpt->efi_last_u_lba; 1240Sstevel@tonic-gate mdvp->lbasize = gpt->efi_lbasize; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate for (i = 0; (i < gpt->efi_nparts); ++i) { 1270Sstevel@tonic-gate mdvp->parts[i].start = gpt->efi_parts[i].p_start; 1280Sstevel@tonic-gate mdvp->parts[i].size = gpt->efi_parts[i].p_size; 1290Sstevel@tonic-gate mdvp->parts[i].tag = gpt->efi_parts[i].p_tag; 1300Sstevel@tonic-gate mdvp->parts[i].flag = gpt->efi_parts[i].p_flag; 1310Sstevel@tonic-gate /* 1321896Sjkennedy * It is possible to present an efi label but be using vtoc 1331896Sjkennedy * disks to create a > 1 TB metadevice. In case the first 1341896Sjkennedy * disk in the underlying metadevice is a vtoc disk and starts 1351896Sjkennedy * at the beginning of the disk it is necessary to convey this 1361896Sjkennedy * information to the user. 1371896Sjkennedy */ 1381896Sjkennedy if (mdvp->parts[i].size > 0 && 1391896Sjkennedy mdvp->parts[i].start != 0 && mdvp->nparts == 1) { 1401896Sjkennedy mdvp->parts[i].label = btodb(DK_LABEL_SIZE); 1411896Sjkennedy mdvp->parts[i].start = 0; 1421896Sjkennedy } 1431896Sjkennedy 1441896Sjkennedy /* 1450Sstevel@tonic-gate * Due to the lack of a label for the entire partition table, 1460Sstevel@tonic-gate * we use p_name of the reserved partition 1470Sstevel@tonic-gate */ 1480Sstevel@tonic-gate if ((gpt->efi_parts[i].p_tag == V_RESERVED) && 1490Sstevel@tonic-gate (gpt->efi_parts[i].p_name != NULL)) { 1500Sstevel@tonic-gate (void) strlcpy(typename, gpt->efi_parts[i].p_name, 151*7464SAndrew.Balfour@Sun.COM EFI_PART_NAME_LEN); 1520Sstevel@tonic-gate /* Stop at first (if any) space or tab */ 1530Sstevel@tonic-gate (void) strtok(typename, " \t"); 1540Sstevel@tonic-gate mdvp->typename = Strdup(typename); 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static void 1600Sstevel@tonic-gate meta_mdvtoc_to_efi(mdvtoc_t *mdvp, struct dk_gpt **gpt) 1610Sstevel@tonic-gate { 1620Sstevel@tonic-gate char typename[EFI_PART_NAME_LEN]; 1630Sstevel@tonic-gate uint_t i; 1640Sstevel@tonic-gate uint_t lastpart; 1650Sstevel@tonic-gate size_t size; 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* first we count how many partitions we have to send */ 1680Sstevel@tonic-gate for (i = 0; i < MD_MAX_PARTS; i++) { 1690Sstevel@tonic-gate if ((mdvp->parts[i].start == 0) && 1700Sstevel@tonic-gate (mdvp->parts[i].size == 0) && 1710Sstevel@tonic-gate (mdvp->parts[i].tag != V_RESERVED)) { 1720Sstevel@tonic-gate continue; 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate /* if we are here, we know the partition is really used */ 1750Sstevel@tonic-gate lastpart = i; 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate size = sizeof (struct dk_gpt) + (sizeof (struct dk_part) * lastpart); 1780Sstevel@tonic-gate *gpt = calloc(size, sizeof (char)); 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate (*gpt)->efi_nparts = lastpart + 1; 1810Sstevel@tonic-gate (*gpt)->efi_first_u_lba = mdvp->first_lba; 1820Sstevel@tonic-gate (*gpt)->efi_last_u_lba = mdvp->last_lba; 1830Sstevel@tonic-gate (*gpt)->efi_lbasize = mdvp->lbasize; 1840Sstevel@tonic-gate for (i = 0; (i < (*gpt)->efi_nparts); ++i) { 1850Sstevel@tonic-gate (*gpt)->efi_parts[i].p_start = mdvp->parts[i].start; 1860Sstevel@tonic-gate (*gpt)->efi_parts[i].p_size = mdvp->parts[i].size; 1870Sstevel@tonic-gate (*gpt)->efi_parts[i].p_tag = mdvp->parts[i].tag; 1880Sstevel@tonic-gate (*gpt)->efi_parts[i].p_flag = mdvp->parts[i].flag; 1890Sstevel@tonic-gate /* 1900Sstevel@tonic-gate * Due to the lack of a label for the entire partition table, 1910Sstevel@tonic-gate * we use p_name of the reserved partition 1920Sstevel@tonic-gate */ 1930Sstevel@tonic-gate if (((*gpt)->efi_parts[i].p_tag == V_RESERVED) && 194*7464SAndrew.Balfour@Sun.COM (mdvp->typename != NULL)) { 1950Sstevel@tonic-gate (void) strlcpy((*gpt)->efi_parts[i].p_name, typename, 196*7464SAndrew.Balfour@Sun.COM EFI_PART_NAME_LEN); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate } 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate void 2030Sstevel@tonic-gate ctlr_cache_add(char *nm, int ty) 2040Sstevel@tonic-gate { 2050Sstevel@tonic-gate ctlr_cache_t **ccpp; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate for (ccpp = &ctlr_cache; *ccpp != NULL; ccpp = &(*ccpp)->ctlr_nx) 2080Sstevel@tonic-gate if (strcmp((*ccpp)->ctlr_nm, nm) == 0) 2090Sstevel@tonic-gate return; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate *ccpp = Zalloc(sizeof (ctlr_cache_t)); 2120Sstevel@tonic-gate (*ccpp)->ctlr_nm = Strdup(nm); 2130Sstevel@tonic-gate (*ccpp)->ctlr_ty = ty; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate int 2170Sstevel@tonic-gate ctlr_cache_look(char *nm) 2180Sstevel@tonic-gate { 2190Sstevel@tonic-gate ctlr_cache_t *tcp; 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate for (tcp = ctlr_cache; tcp != NULL; tcp = tcp->ctlr_nx) 2220Sstevel@tonic-gate if (strcmp(tcp->ctlr_nm, nm) == 0) 2230Sstevel@tonic-gate return (tcp->ctlr_ty); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate return (-1); 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate void 2300Sstevel@tonic-gate metaflushctlrcache(void) 2310Sstevel@tonic-gate { 2320Sstevel@tonic-gate ctlr_cache_t *cp, *np; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate for (cp = ctlr_cache, np = NULL; cp != NULL; cp = np) { 2350Sstevel@tonic-gate np = cp->ctlr_nx; 2360Sstevel@tonic-gate Free(cp->ctlr_nm); 2370Sstevel@tonic-gate Free(cp); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate ctlr_cache = NULL; 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * getdrvnode -- return the driver name based on mdname_t->bname 2440Sstevel@tonic-gate * Need to free pointer when finished. 2450Sstevel@tonic-gate */ 2460Sstevel@tonic-gate char * 2470Sstevel@tonic-gate getdrvnode(mdname_t *np, md_error_t *ep) 2480Sstevel@tonic-gate { 2490Sstevel@tonic-gate char *devicespath, 250*7464SAndrew.Balfour@Sun.COM *drvnode, 251*7464SAndrew.Balfour@Sun.COM *cp; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate if ((devicespath = metagetdevicesname(np, ep)) == NULL) 2540Sstevel@tonic-gate return (NULL); 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /* 2570Sstevel@tonic-gate * At this point devicespath should be like the following 2580Sstevel@tonic-gate * "/devices/<unknow_and_dont_care>/xxxx@vvvv" 2590Sstevel@tonic-gate * 2600Sstevel@tonic-gate * There's a couple of 'if' statements below which could 2610Sstevel@tonic-gate * return an error condition, but I've decide to allow 2620Sstevel@tonic-gate * a more open approach regarding the mapping so as to 2630Sstevel@tonic-gate * not restrict possible future projects. 2640Sstevel@tonic-gate */ 2650Sstevel@tonic-gate if (drvnode = strrchr(devicespath, '/')) 2660Sstevel@tonic-gate /* 2670Sstevel@tonic-gate * drvnode now just "xxxx@vvvv" 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate drvnode++; 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate if (cp = strrchr(drvnode, '@')) 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * Now drvnode is just the driver name "xxxx" 2740Sstevel@tonic-gate */ 2750Sstevel@tonic-gate *cp = '\0'; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate cp = Strdup(drvnode); 2780Sstevel@tonic-gate Free(devicespath); 2790Sstevel@tonic-gate np->devicesname = NULL; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate return (cp); 2820Sstevel@tonic-gate } 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate /* 2850Sstevel@tonic-gate * meta_load_dl -- open dynamic library using LDLIBRARYPATH, a debug 2860Sstevel@tonic-gate * environment variable METALDPATH, or the default location. 2870Sstevel@tonic-gate */ 2880Sstevel@tonic-gate static void * 2890Sstevel@tonic-gate meta_load_dl(mdname_t *np, md_error_t *ep) 2900Sstevel@tonic-gate { 2910Sstevel@tonic-gate char *drvnode, 292*7464SAndrew.Balfour@Sun.COM newpath[MAXPATHLEN], 293*7464SAndrew.Balfour@Sun.COM *p; 2940Sstevel@tonic-gate void *cookie; 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate if ((drvnode = getdrvnode(np, ep)) != NULL) { 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * Library seach algorithm: 3000Sstevel@tonic-gate * 1) Use LDLIBRARYPATH which is implied when a non-absolute 3010Sstevel@tonic-gate * path name is passed to dlopen() 3020Sstevel@tonic-gate * 2) Use the value of METALDPATH as the directory. Mainly 3030Sstevel@tonic-gate * used for debugging 3040Sstevel@tonic-gate * 3) Last search the default location of "/usr/lib" 3050Sstevel@tonic-gate */ 3060Sstevel@tonic-gate (void) snprintf(newpath, sizeof (newpath), "lib%s.so.1", 3070Sstevel@tonic-gate drvnode); 3080Sstevel@tonic-gate if ((cookie = dlopen(newpath, RTLD_LAZY)) == NULL) { 3090Sstevel@tonic-gate if ((p = getenv("METALDPATH")) == NULL) 3100Sstevel@tonic-gate p = METALDPATH_DEFAULT; 3110Sstevel@tonic-gate (void) snprintf(newpath, sizeof (newpath), 3120Sstevel@tonic-gate "%s/lib%s.so.1", p, drvnode); 3130Sstevel@tonic-gate Free(drvnode); 3140Sstevel@tonic-gate if ((cookie = dlopen(newpath, RTLD_LAZY)) != NULL) { 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * Common failure here would be failing to 3170Sstevel@tonic-gate * find a libXX.so.1 such as libsd.so.1 3180Sstevel@tonic-gate * Some controllers will not have a library 3190Sstevel@tonic-gate * because there's no enclosure or name 3200Sstevel@tonic-gate * translation required. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate return (cookie); 3230Sstevel@tonic-gate } 3240Sstevel@tonic-gate } else { 3250Sstevel@tonic-gate Free(drvnode); 3260Sstevel@tonic-gate return (cookie); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate return (NULL); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate /* 3330Sstevel@tonic-gate * meta_match_names -- possibly convert the driver names returned by CINFO 3340Sstevel@tonic-gate */ 3350Sstevel@tonic-gate static void 3360Sstevel@tonic-gate meta_match_names(mdname_t *np, struct dk_cinfo *cp, mdcinfo_t *mdcp, 3370Sstevel@tonic-gate md_error_t *ep) 3380Sstevel@tonic-gate { 3390Sstevel@tonic-gate void *cookie; 3400Sstevel@tonic-gate meta_convert_e ((*fptr)(mdname_t *, struct dk_cinfo *, mdcinfo_t *, 341*7464SAndrew.Balfour@Sun.COM md_error_t *)); 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate if ((cookie = meta_load_dl(np, ep)) != NULL) { 3440Sstevel@tonic-gate fptr = (meta_convert_e (*)(mdname_t *, struct dk_cinfo *, 3450Sstevel@tonic-gate mdcinfo_t *, md_error_t *))dlsym(cookie, "convert_path"); 3460Sstevel@tonic-gate if (fptr != NULL) 3470Sstevel@tonic-gate (void) (*fptr)(np, cp, mdcp, ep); 3480Sstevel@tonic-gate (void) dlclose(cookie); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate /* 3530Sstevel@tonic-gate * meta_match_enclosure -- return any enclosure info if found 3540Sstevel@tonic-gate */ 3550Sstevel@tonic-gate int 3560Sstevel@tonic-gate meta_match_enclosure(mdname_t *np, mdcinfo_t *mdcp, md_error_t *ep) 3570Sstevel@tonic-gate { 3580Sstevel@tonic-gate meta_enclosure_e e, 359*7464SAndrew.Balfour@Sun.COM ((*fptr)(mdname_t *, mdcinfo_t *, 360*7464SAndrew.Balfour@Sun.COM md_error_t *)); 3610Sstevel@tonic-gate void *cookie; 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate if ((cookie = meta_load_dl(np, ep)) != NULL) { 3640Sstevel@tonic-gate fptr = (meta_enclosure_e (*)(mdname_t *, mdcinfo_t *, 3650Sstevel@tonic-gate md_error_t *))dlsym(cookie, "get_enclosure"); 3660Sstevel@tonic-gate if (fptr != NULL) { 3670Sstevel@tonic-gate e = (*fptr)(np, mdcp, ep); 3680Sstevel@tonic-gate switch (e) { 3690Sstevel@tonic-gate case Enclosure_Error: 3700Sstevel@tonic-gate /* 3710Sstevel@tonic-gate * Looks like this library wanted to handle 3720Sstevel@tonic-gate * our device and had an internal error. 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate return (1); 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate case Enclosure_Okay: 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * Found a library to handle the request so 3790Sstevel@tonic-gate * just return with data provided. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate return (0); 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate case Enclosure_Noop: 3840Sstevel@tonic-gate /* 3850Sstevel@tonic-gate * Need to continue the search 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate break; 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate (void) dlclose(cookie); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate return (0); 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate 3950Sstevel@tonic-gate static int 3960Sstevel@tonic-gate meta_cinfo_to_md(mdname_t *np, struct dk_cinfo *cp, mdcinfo_t *mdcp, 3970Sstevel@tonic-gate md_error_t *ep) 3980Sstevel@tonic-gate { 3990Sstevel@tonic-gate /* default */ 4000Sstevel@tonic-gate (void) memset(mdcp, '\0', sizeof (*mdcp)); 4010Sstevel@tonic-gate (void) strncpy(mdcp->cname, cp->dki_cname, 4020Sstevel@tonic-gate min((sizeof (mdcp->cname) - 1), sizeof (cp->dki_cname))); 4030Sstevel@tonic-gate mdcp->ctype = MHD_CTLR_GENERIC; 4040Sstevel@tonic-gate mdcp->cnum = cp->dki_cnum; 4050Sstevel@tonic-gate (void) strncpy(mdcp->dname, cp->dki_dname, 4060Sstevel@tonic-gate min((sizeof (mdcp->dname) - 1), sizeof (cp->dki_dname))); 4070Sstevel@tonic-gate mdcp->unit = cp->dki_unit; 4080Sstevel@tonic-gate mdcp->maxtransfer = cp->dki_maxtransfer; 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* 4110Sstevel@tonic-gate * See if the driver name returned from DKIOCINFO 4120Sstevel@tonic-gate * is valid or not. In somecases, such as the ap_dmd 4130Sstevel@tonic-gate * driver, we need to modify the name that's return 4140Sstevel@tonic-gate * for everything to work. 4150Sstevel@tonic-gate */ 4160Sstevel@tonic-gate meta_match_names(np, cp, mdcp, ep); 4170Sstevel@tonic-gate 4180Sstevel@tonic-gate if (meta_match_enclosure(np, mdcp, ep)) 4190Sstevel@tonic-gate return (-1); 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate /* return success */ 4220Sstevel@tonic-gate return (0); 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate static void 4260Sstevel@tonic-gate meta_vtoc_to_md( 4270Sstevel@tonic-gate struct vtoc *vp, 4280Sstevel@tonic-gate mdvtoc_t *mdvp 4290Sstevel@tonic-gate ) 4300Sstevel@tonic-gate { 4310Sstevel@tonic-gate char typename[sizeof (vp->v_asciilabel) + 1]; 4320Sstevel@tonic-gate uint_t i; 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate (void) memset(mdvp, '\0', sizeof (*mdvp)); 4350Sstevel@tonic-gate (void) strncpy(typename, vp->v_asciilabel, 4360Sstevel@tonic-gate sizeof (vp->v_asciilabel)); 4370Sstevel@tonic-gate typename[sizeof (typename) - 1] = '\0'; 4380Sstevel@tonic-gate for (i = 0; ((i < sizeof (typename)) && (typename[i] != '\0')); ++i) { 4390Sstevel@tonic-gate if ((typename[i] == ' ') || (typename[i] == '\t')) { 4400Sstevel@tonic-gate typename[i] = '\0'; 4410Sstevel@tonic-gate break; 4420Sstevel@tonic-gate } 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate mdvp->typename = Strdup(typename); 4450Sstevel@tonic-gate mdvp->nparts = vp->v_nparts; 4460Sstevel@tonic-gate for (i = 0; (i < vp->v_nparts); ++i) { 4470Sstevel@tonic-gate mdvp->parts[i].start = vp->v_part[i].p_start; 4480Sstevel@tonic-gate mdvp->parts[i].size = vp->v_part[i].p_size; 4490Sstevel@tonic-gate mdvp->parts[i].tag = vp->v_part[i].p_tag; 4500Sstevel@tonic-gate mdvp->parts[i].flag = vp->v_part[i].p_flag; 4510Sstevel@tonic-gate if (vp->v_part[i].p_start == 0 && vp->v_part[i].p_size > 0) 4520Sstevel@tonic-gate mdvp->parts[i].label = btodb(DK_LABEL_SIZE); 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate } 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate /* 4570Sstevel@tonic-gate * free allocations in vtoc 4580Sstevel@tonic-gate */ 4590Sstevel@tonic-gate void 4600Sstevel@tonic-gate metafreevtoc( 4610Sstevel@tonic-gate mdvtoc_t *vtocp 4620Sstevel@tonic-gate ) 4630Sstevel@tonic-gate { 4640Sstevel@tonic-gate if (vtocp->typename != NULL) 4650Sstevel@tonic-gate Free(vtocp->typename); 4660Sstevel@tonic-gate (void) memset(vtocp, 0, sizeof (*vtocp)); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate 4690Sstevel@tonic-gate /* 4700Sstevel@tonic-gate * return md types 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate mdvtoc_t * 4730Sstevel@tonic-gate metagetvtoc( 4740Sstevel@tonic-gate mdname_t *np, /* only rname, drivenamep, are setup */ 4750Sstevel@tonic-gate int nocache, 4760Sstevel@tonic-gate uint_t *partnop, 4770Sstevel@tonic-gate md_error_t *ep 4780Sstevel@tonic-gate ) 4790Sstevel@tonic-gate { 4800Sstevel@tonic-gate mddrivename_t *dnp = np->drivenamep; 4810Sstevel@tonic-gate struct dk_geom geom; 4820Sstevel@tonic-gate char *minor_name = NULL; 4830Sstevel@tonic-gate char *rname = np->rname; 4840Sstevel@tonic-gate int fd; 4850Sstevel@tonic-gate int partno; 4860Sstevel@tonic-gate int err = 0; /* saves errno from ioctl */ 4870Sstevel@tonic-gate ddi_devid_t devid; 4880Sstevel@tonic-gate char *p; 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate /* short circuit */ 4910Sstevel@tonic-gate if ((! nocache) && (dnp->vtoc.nparts != 0)) { 4920Sstevel@tonic-gate if (partnop != NULL) { 4930Sstevel@tonic-gate /* 4940Sstevel@tonic-gate * the following assigment works because the 4950Sstevel@tonic-gate * mdname_t structs are always created as part 4960Sstevel@tonic-gate * of the drivenamep struct. When a user 4970Sstevel@tonic-gate * creates an mdname_t struct it either 4980Sstevel@tonic-gate * uses an existing drivenamep struct or creates 4990Sstevel@tonic-gate * a new one and then adds the mdname_t struct 5000Sstevel@tonic-gate * as part of its parts_val array. So what is 5010Sstevel@tonic-gate * being computed below is the slice offset in 5020Sstevel@tonic-gate * the parts_val array. 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate *partnop = np - np->drivenamep->parts.parts_val; 5050Sstevel@tonic-gate assert(*partnop < dnp->parts.parts_len); 5060Sstevel@tonic-gate } 5070Sstevel@tonic-gate return (&dnp->vtoc); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* can't get vtoc */ 5110Sstevel@tonic-gate if (! nocache) { 5120Sstevel@tonic-gate switch (dnp->type) { 5130Sstevel@tonic-gate case MDT_ACCES: 5140Sstevel@tonic-gate case MDT_UNKNOWN: 5150Sstevel@tonic-gate (void) mdsyserror(ep, dnp->errnum, rname); 5160Sstevel@tonic-gate return (NULL); 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate /* get all the info */ 5210Sstevel@tonic-gate if ((fd = open(rname, (O_RDONLY|O_NDELAY), 0)) < 0) { 5220Sstevel@tonic-gate (void) mdsyserror(ep, errno, rname); 5230Sstevel@tonic-gate return (NULL); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * The disk is open so this is a good point to get the devid 5280Sstevel@tonic-gate * otherwise it will need to be done at another time which 5290Sstevel@tonic-gate * means reopening it. 5300Sstevel@tonic-gate */ 5310Sstevel@tonic-gate if (devid_get(fd, &devid) != 0) { 5320Sstevel@tonic-gate /* there is no devid for the disk */ 5330Sstevel@tonic-gate if (((p = getenv("MD_DEBUG")) != NULL) && 5340Sstevel@tonic-gate (strstr(p, "DEVID") != NULL)) { 5350Sstevel@tonic-gate (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 5360Sstevel@tonic-gate "%s has no device id\n"), np->rname); 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate np->minor_name = (char *)NULL; 5390Sstevel@tonic-gate dnp->devid = NULL; 5400Sstevel@tonic-gate } else { 5410Sstevel@tonic-gate (void) devid_get_minor_name(fd, &minor_name); 5420Sstevel@tonic-gate /* 5430Sstevel@tonic-gate * The minor name could be NULL if the underlying 5440Sstevel@tonic-gate * device driver does not support 'minor names'. 5450Sstevel@tonic-gate * This means we do not use devid's for this device. 5460Sstevel@tonic-gate * SunCluster did driver does not support minor names. 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate if (minor_name != NULL) { 5490Sstevel@tonic-gate np->minor_name = Strdup(minor_name); 5500Sstevel@tonic-gate devid_str_free(minor_name); 5510Sstevel@tonic-gate dnp->devid = devid_str_encode(devid, NULL); 5520Sstevel@tonic-gate } else { 5530Sstevel@tonic-gate np->minor_name = (char *)NULL; 5540Sstevel@tonic-gate dnp->devid = NULL; 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate if (((p = getenv("MD_DEBUG")) != NULL) && 5570Sstevel@tonic-gate (strstr(p, "DEVID") != NULL)) { 5580Sstevel@tonic-gate (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 5590Sstevel@tonic-gate "%s no minor name (no devid)\n"), 5600Sstevel@tonic-gate np->rname); 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate devid_free(devid); 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate 5660Sstevel@tonic-gate /* 5670Sstevel@tonic-gate * if our drivenamep points to a device not supporting DKIOCGGEOM, 5680Sstevel@tonic-gate * it's likely to have an EFI label. 5690Sstevel@tonic-gate */ 5700Sstevel@tonic-gate (void) memset(&geom, 0, sizeof (geom)); 5710Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &geom) != 0) { 5720Sstevel@tonic-gate err = errno; 5730Sstevel@tonic-gate if (err == ENOTTY) { 5740Sstevel@tonic-gate (void) mddeverror(ep, MDE_NOT_DISK, NODEV, rname); 5750Sstevel@tonic-gate (void) close(fd); 5760Sstevel@tonic-gate return (NULL); 5770Sstevel@tonic-gate } else if (err != ENOTSUP) { 5780Sstevel@tonic-gate (void) mdsyserror(ep, err, rname); 5790Sstevel@tonic-gate (void) close(fd); 5800Sstevel@tonic-gate return (NULL); 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate /* 5850Sstevel@tonic-gate * If we are here, there was either no failure on DKIOCGGEOM or 5860Sstevel@tonic-gate * the failure was ENOTSUP 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate if (err == ENOTSUP) { 5890Sstevel@tonic-gate /* DKIOCGGEOM yielded ENOTSUP => try efi_alloc_and_read */ 5900Sstevel@tonic-gate struct dk_gpt *gpt; 5910Sstevel@tonic-gate int save_errno; 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate /* this also sets errno */ 5940Sstevel@tonic-gate partno = efi_alloc_and_read(fd, &gpt); 5950Sstevel@tonic-gate save_errno = errno; 5960Sstevel@tonic-gate (void) close(fd); 5970Sstevel@tonic-gate if (partno < 0) { 5980Sstevel@tonic-gate efi_free(gpt); 5990Sstevel@tonic-gate (void) mdsyserror(ep, save_errno, rname); 6000Sstevel@tonic-gate return (NULL); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate if (partno >= gpt->efi_nparts) { 6030Sstevel@tonic-gate efi_free(gpt); 6040Sstevel@tonic-gate (void) mddeverror(ep, MDE_INVALID_PART, NODEV64, 605*7464SAndrew.Balfour@Sun.COM rname); 6060Sstevel@tonic-gate return (NULL); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /* convert to our format */ 6100Sstevel@tonic-gate metafreevtoc(&dnp->vtoc); 6110Sstevel@tonic-gate meta_efi_to_mdvtoc(gpt, &dnp->vtoc); 6120Sstevel@tonic-gate if (dnp->vtoc.nparts > MD_MAX_PARTS) { 6130Sstevel@tonic-gate (void) mddeverror(ep, MDE_TOO_MANY_PARTS, NODEV64, 6140Sstevel@tonic-gate rname); 6150Sstevel@tonic-gate return (NULL); 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate /* 6180Sstevel@tonic-gate * libmeta needs at least V_NUMPAR partitions. 6190Sstevel@tonic-gate * If we have an EFI partition with less than V_NUMPAR slices, 6200Sstevel@tonic-gate * we nevertheless reserve space for V_NUMPAR 6210Sstevel@tonic-gate */ 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate if (dnp->vtoc.nparts < V_NUMPAR) { 6240Sstevel@tonic-gate dnp->vtoc.nparts = V_NUMPAR; 6250Sstevel@tonic-gate } 6260Sstevel@tonic-gate meta_efi_to_mdgeom(gpt, &dnp->geom); 6270Sstevel@tonic-gate efi_free(gpt); 6280Sstevel@tonic-gate } else { 6290Sstevel@tonic-gate /* no error on DKIOCGGEOM, try meta_getvtoc */ 6300Sstevel@tonic-gate struct vtoc vtoc; 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate if (meta_getvtoc(fd, np->cname, &vtoc, &partno, ep) < 0) { 6330Sstevel@tonic-gate (void) close(fd); 6340Sstevel@tonic-gate return (NULL); 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate (void) close(fd); 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate /* convert to our format */ 6390Sstevel@tonic-gate meta_geom_to_md(&geom, &dnp->geom); 6400Sstevel@tonic-gate metafreevtoc(&dnp->vtoc); 6410Sstevel@tonic-gate meta_vtoc_to_md(&vtoc, &dnp->vtoc); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* fix up any drives which are now accessible */ 6450Sstevel@tonic-gate if ((nocache) && (dnp->type == MDT_ACCES) && 6460Sstevel@tonic-gate (dnp->vtoc.nparts == dnp->parts.parts_len)) { 6470Sstevel@tonic-gate dnp->type = MDT_COMP; 6480Sstevel@tonic-gate dnp->errnum = 0; 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate /* save partno */ 6520Sstevel@tonic-gate assert(partno < dnp->vtoc.nparts); 6530Sstevel@tonic-gate if (partnop != NULL) 6540Sstevel@tonic-gate *partnop = partno; 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate /* return info */ 6570Sstevel@tonic-gate return (&dnp->vtoc); 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate static void 6610Sstevel@tonic-gate meta_mdvtoc_to_vtoc( 6620Sstevel@tonic-gate mdvtoc_t *mdvp, 6630Sstevel@tonic-gate struct vtoc *vp 6640Sstevel@tonic-gate ) 6650Sstevel@tonic-gate { 6660Sstevel@tonic-gate uint_t i; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate (void) memset(&vp->v_part, '\0', sizeof (vp->v_part)); 6690Sstevel@tonic-gate vp->v_nparts = (ushort_t)mdvp->nparts; 6700Sstevel@tonic-gate for (i = 0; (i < mdvp->nparts); ++i) { 6710Sstevel@tonic-gate vp->v_part[i].p_start = (daddr32_t)mdvp->parts[i].start; 6720Sstevel@tonic-gate vp->v_part[i].p_size = (daddr32_t)mdvp->parts[i].size; 6730Sstevel@tonic-gate vp->v_part[i].p_tag = mdvp->parts[i].tag; 6740Sstevel@tonic-gate vp->v_part[i].p_flag = mdvp->parts[i].flag; 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * Set the vtoc, but use the cached copy to get the info from. 6800Sstevel@tonic-gate * We write np->drivenamep->vtoc to disk. 6810Sstevel@tonic-gate * Before we can do this we read the vtoc in. 6820Sstevel@tonic-gate * if we're dealing with a metadevice and this metadevice is a 64 bit device 6830Sstevel@tonic-gate * we can use meta_getmdvtoc/meta_setmdvtoc 6840Sstevel@tonic-gate * else 6850Sstevel@tonic-gate * we use meta_getvtoc/meta_setvtoc but than we first have to convert 6860Sstevel@tonic-gate * dnp->vtoc (actually being a mdvtoc_t) into a vtoc_t 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate int 6890Sstevel@tonic-gate metasetvtoc( 6900Sstevel@tonic-gate mdname_t *np, 6910Sstevel@tonic-gate md_error_t *ep 6920Sstevel@tonic-gate ) 6930Sstevel@tonic-gate { 6940Sstevel@tonic-gate char *rname = np->rname; 6950Sstevel@tonic-gate mddrivename_t *dnp = np->drivenamep; 6960Sstevel@tonic-gate int fd; 6970Sstevel@tonic-gate int err; 6980Sstevel@tonic-gate int save_errno; 6990Sstevel@tonic-gate struct dk_geom geom; 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate if ((fd = open(rname, (O_RDONLY | O_NDELAY), 0)) < 0) 7020Sstevel@tonic-gate return (mdsyserror(ep, errno, rname)); 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate err = ioctl(fd, DKIOCGGEOM, &geom); 7050Sstevel@tonic-gate save_errno = errno; 7060Sstevel@tonic-gate if (err == 0) { 7070Sstevel@tonic-gate struct vtoc vtoc; 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate if (meta_getvtoc(fd, np->cname, &vtoc, NULL, ep) < 0) { 7100Sstevel@tonic-gate (void) close(fd); 7110Sstevel@tonic-gate return (-1); 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate meta_mdvtoc_to_vtoc(&dnp->vtoc, &vtoc); 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate if (meta_setvtoc(fd, np->cname, &vtoc, ep) < 0) { 7170Sstevel@tonic-gate (void) close(fd); 7180Sstevel@tonic-gate return (-1); 7190Sstevel@tonic-gate } 7200Sstevel@tonic-gate } else if (save_errno == ENOTSUP) { 7210Sstevel@tonic-gate struct dk_gpt *gpt; 7220Sstevel@tonic-gate int ret; 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate /* allocation of gpt is done in meta_mdvtoc_to_efi */ 7250Sstevel@tonic-gate meta_mdvtoc_to_efi(&dnp->vtoc, &gpt); 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate ret = efi_write(fd, gpt); 7280Sstevel@tonic-gate save_errno = errno; 7290Sstevel@tonic-gate free(gpt); 7300Sstevel@tonic-gate if (ret != 0) { 7310Sstevel@tonic-gate (void) close(fd); 7320Sstevel@tonic-gate return (mdsyserror(ep, save_errno, rname)); 7330Sstevel@tonic-gate } else { 7340Sstevel@tonic-gate (void) close(fd); 7350Sstevel@tonic-gate return (0); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate } else { 7390Sstevel@tonic-gate (void) close(fd); 7400Sstevel@tonic-gate return (mdsyserror(ep, save_errno, rname)); 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate (void) close(fd); 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate return (0); 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate mdgeom_t * 7490Sstevel@tonic-gate metagetgeom( 7500Sstevel@tonic-gate mdname_t *np, /* only rname, drivenamep, are setup */ 7510Sstevel@tonic-gate md_error_t *ep 7520Sstevel@tonic-gate ) 7530Sstevel@tonic-gate { 7540Sstevel@tonic-gate if (metagetvtoc(np, FALSE, NULL, ep) == NULL) 7550Sstevel@tonic-gate return (NULL); 7560Sstevel@tonic-gate return (&np->drivenamep->geom); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate mdcinfo_t * 7600Sstevel@tonic-gate metagetcinfo( 7610Sstevel@tonic-gate mdname_t *np, /* only rname, drivenamep, are setup */ 7620Sstevel@tonic-gate md_error_t *ep 7630Sstevel@tonic-gate ) 7640Sstevel@tonic-gate { 7650Sstevel@tonic-gate char *rname = np->rname; 7660Sstevel@tonic-gate mddrivename_t *dnp = np->drivenamep; 7670Sstevel@tonic-gate int fd; 7680Sstevel@tonic-gate struct dk_cinfo cinfo; 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate /* short circuit */ 7710Sstevel@tonic-gate if (dnp->cinfo.cname[0] != '\0') 7720Sstevel@tonic-gate return (&dnp->cinfo); 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate /* get controller info */ 7750Sstevel@tonic-gate if ((fd = open(rname, (O_RDONLY|O_NDELAY), 0)) < 0) { 7760Sstevel@tonic-gate (void) mdsyserror(ep, errno, rname); 7770Sstevel@tonic-gate return (NULL); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate if (ioctl(fd, DKIOCINFO, &cinfo) != 0) { 7800Sstevel@tonic-gate int save = errno; 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate (void) close(fd); 7830Sstevel@tonic-gate if (save == ENOTTY) { 7840Sstevel@tonic-gate (void) mddeverror(ep, MDE_NOT_DISK, NODEV64, rname); 7850Sstevel@tonic-gate } else { 7860Sstevel@tonic-gate (void) mdsyserror(ep, save, rname); 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate return (NULL); 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate (void) close(fd); /* sd/ssd bug */ 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate /* convert to our format */ 7930Sstevel@tonic-gate if (meta_cinfo_to_md(np, &cinfo, &dnp->cinfo, ep) != 0) 7940Sstevel@tonic-gate return (NULL); 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate /* return info */ 7970Sstevel@tonic-gate return (&dnp->cinfo); 7980Sstevel@tonic-gate } 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate /* 8010Sstevel@tonic-gate * get partition number 8020Sstevel@tonic-gate */ 8030Sstevel@tonic-gate int 8040Sstevel@tonic-gate metagetpartno( 8050Sstevel@tonic-gate mdname_t *np, 8060Sstevel@tonic-gate md_error_t *ep 8070Sstevel@tonic-gate ) 8080Sstevel@tonic-gate { 8090Sstevel@tonic-gate mdvtoc_t *vtocp; 8100Sstevel@tonic-gate uint_t partno; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate if ((vtocp = metagetvtoc(np, FALSE, &partno, ep)) == NULL) 8130Sstevel@tonic-gate return (-1); 8140Sstevel@tonic-gate assert(partno < vtocp->nparts); 8150Sstevel@tonic-gate return (partno); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * get size of device 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate diskaddr_t 8220Sstevel@tonic-gate metagetsize( 8230Sstevel@tonic-gate mdname_t *np, 8240Sstevel@tonic-gate md_error_t *ep 8250Sstevel@tonic-gate ) 8260Sstevel@tonic-gate { 8270Sstevel@tonic-gate mdvtoc_t *vtocp; 8280Sstevel@tonic-gate uint_t partno; 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate if ((vtocp = metagetvtoc(np, FALSE, &partno, ep)) == NULL) 8310Sstevel@tonic-gate return (MD_DISKADDR_ERROR); 8320Sstevel@tonic-gate assert(partno < vtocp->nparts); 8330Sstevel@tonic-gate return (vtocp->parts[partno].size); 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate /* 8370Sstevel@tonic-gate * get label of device 8380Sstevel@tonic-gate */ 8390Sstevel@tonic-gate diskaddr_t 8400Sstevel@tonic-gate metagetlabel( 8410Sstevel@tonic-gate mdname_t *np, 8420Sstevel@tonic-gate md_error_t *ep 8430Sstevel@tonic-gate ) 8440Sstevel@tonic-gate { 8450Sstevel@tonic-gate mdvtoc_t *vtocp; 8460Sstevel@tonic-gate uint_t partno; 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate if ((vtocp = metagetvtoc(np, FALSE, &partno, ep)) == NULL) 8490Sstevel@tonic-gate return (MD_DISKADDR_ERROR); 8500Sstevel@tonic-gate assert(partno < vtocp->nparts); 8510Sstevel@tonic-gate return (vtocp->parts[partno].label); 8520Sstevel@tonic-gate } 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate /* 8550Sstevel@tonic-gate * find out where database replicas end 8560Sstevel@tonic-gate */ 8570Sstevel@tonic-gate static int 8580Sstevel@tonic-gate mddb_getendblk( 8590Sstevel@tonic-gate mdsetname_t *sp, 8600Sstevel@tonic-gate mdname_t *np, 8610Sstevel@tonic-gate diskaddr_t *endblkp, 8620Sstevel@tonic-gate md_error_t *ep 8630Sstevel@tonic-gate ) 8640Sstevel@tonic-gate { 8650Sstevel@tonic-gate md_replicalist_t *rlp = NULL; 8660Sstevel@tonic-gate md_replicalist_t *rl; 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate /* make sure we have a component */ 8690Sstevel@tonic-gate *endblkp = 0; 8700Sstevel@tonic-gate if (metaismeta(np)) 8710Sstevel@tonic-gate return (0); 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate /* get replicas, quit if none */ 8740Sstevel@tonic-gate if (metareplicalist(sp, MD_BASICNAME_OK | PRINT_FAST, &rlp, ep) < 0) { 8750Sstevel@tonic-gate if (! mdismddberror(ep, MDE_DB_NODB)) 8760Sstevel@tonic-gate return (-1); 8770Sstevel@tonic-gate mdclrerror(ep); 8780Sstevel@tonic-gate return (0); 8790Sstevel@tonic-gate } else if (rlp == NULL) 8800Sstevel@tonic-gate return (0); 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate /* go through all the replicas */ 8830Sstevel@tonic-gate for (rl = rlp; (rl != NULL); rl = rl->rl_next) { 8840Sstevel@tonic-gate md_replica_t *rp = rl->rl_repp; 8850Sstevel@tonic-gate mdname_t *repnamep = rp->r_namep; 8860Sstevel@tonic-gate diskaddr_t dbend; 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate if (np->dev != repnamep->dev) 8890Sstevel@tonic-gate continue; 8900Sstevel@tonic-gate dbend = rp->r_blkno + rp->r_nblk - 1; 8910Sstevel@tonic-gate if (dbend > *endblkp) 8920Sstevel@tonic-gate *endblkp = dbend; 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate /* cleanup, return success */ 8960Sstevel@tonic-gate metafreereplicalist(rlp); 8970Sstevel@tonic-gate return (0); 8980Sstevel@tonic-gate } 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate /* 9010Sstevel@tonic-gate * return cached start block 9020Sstevel@tonic-gate */ 9030Sstevel@tonic-gate static diskaddr_t 9040Sstevel@tonic-gate metagetend( 9050Sstevel@tonic-gate mdsetname_t *sp, 9060Sstevel@tonic-gate mdname_t *np, 9070Sstevel@tonic-gate md_error_t *ep 9080Sstevel@tonic-gate ) 9090Sstevel@tonic-gate { 9100Sstevel@tonic-gate diskaddr_t end_blk = MD_DISKADDR_ERROR; 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate /* short circuit */ 9130Sstevel@tonic-gate if (np->end_blk != MD_DISKADDR_ERROR) 9140Sstevel@tonic-gate return (np->end_blk); 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate /* look for database locations */ 9170Sstevel@tonic-gate if (mddb_getendblk(sp, np, &end_blk, ep) != 0) 9180Sstevel@tonic-gate return (MD_DISKADDR_ERROR); 9190Sstevel@tonic-gate 9200Sstevel@tonic-gate /* success */ 9210Sstevel@tonic-gate np->end_blk = end_blk; 9220Sstevel@tonic-gate return (end_blk); 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate /* 9260Sstevel@tonic-gate * does device have a metadb 9270Sstevel@tonic-gate */ 9280Sstevel@tonic-gate int 9290Sstevel@tonic-gate metahasmddb( 9300Sstevel@tonic-gate mdsetname_t *sp, 9310Sstevel@tonic-gate mdname_t *np, 9320Sstevel@tonic-gate md_error_t *ep 9330Sstevel@tonic-gate ) 9340Sstevel@tonic-gate { 9350Sstevel@tonic-gate if (metagetend(sp, np, ep) == MD_DISKADDR_ERROR) 9360Sstevel@tonic-gate return (-1); 9370Sstevel@tonic-gate else if (np->end_blk > 0) 9380Sstevel@tonic-gate return (1); 9390Sstevel@tonic-gate else 9400Sstevel@tonic-gate return (0); 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate /* 9440Sstevel@tonic-gate * return cached start block 9450Sstevel@tonic-gate */ 9460Sstevel@tonic-gate diskaddr_t 9470Sstevel@tonic-gate metagetstart( 9480Sstevel@tonic-gate mdsetname_t *sp, 9490Sstevel@tonic-gate mdname_t *np, 9500Sstevel@tonic-gate md_error_t *ep 9510Sstevel@tonic-gate ) 9520Sstevel@tonic-gate { 9530Sstevel@tonic-gate diskaddr_t start_blk = MD_DISKADDR_ERROR; 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate /* short circuit */ 9560Sstevel@tonic-gate if (np->start_blk != MD_DISKADDR_ERROR) 9570Sstevel@tonic-gate return (np->start_blk); 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate /* look for database locations */ 9600Sstevel@tonic-gate if ((start_blk = metagetend(sp, np, ep)) == MD_DISKADDR_ERROR) 9610Sstevel@tonic-gate return (MD_DISKADDR_ERROR); 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate /* check for label */ 9640Sstevel@tonic-gate if (start_blk == 0) { 9650Sstevel@tonic-gate start_blk = metagetlabel(np, ep); 9660Sstevel@tonic-gate if (start_blk == MD_DISKADDR_ERROR) { 9670Sstevel@tonic-gate return (MD_DISKADDR_ERROR); 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate } 9700Sstevel@tonic-gate 9710Sstevel@tonic-gate /* roundup to next cylinder */ 9720Sstevel@tonic-gate if (start_blk != 0) { 9730Sstevel@tonic-gate mdgeom_t *geomp; 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate if ((geomp = metagetgeom(np, ep)) == NULL) 9760Sstevel@tonic-gate return (MD_DISKADDR_ERROR); 9770Sstevel@tonic-gate start_blk = roundup(start_blk, (geomp->nhead * geomp->nsect)); 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* success */ 9810Sstevel@tonic-gate np->start_blk = start_blk; 9820Sstevel@tonic-gate return (start_blk); 9830Sstevel@tonic-gate } 9840Sstevel@tonic-gate 9850Sstevel@tonic-gate /* 9860Sstevel@tonic-gate * return cached devices name 9870Sstevel@tonic-gate */ 9880Sstevel@tonic-gate char * 9890Sstevel@tonic-gate metagetdevicesname( 9900Sstevel@tonic-gate mdname_t *np, 9910Sstevel@tonic-gate md_error_t *ep 9920Sstevel@tonic-gate ) 9930Sstevel@tonic-gate { 9940Sstevel@tonic-gate char path[MAXPATHLEN + 1]; 9950Sstevel@tonic-gate int len; 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate /* short circuit */ 9980Sstevel@tonic-gate if (np->devicesname != NULL) 9990Sstevel@tonic-gate return (np->devicesname); 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate /* follow symlink */ 10020Sstevel@tonic-gate if ((len = readlink(np->bname, path, (sizeof (path) - 1))) < 0) { 10030Sstevel@tonic-gate (void) mdsyserror(ep, errno, np->bname); 10040Sstevel@tonic-gate return (NULL); 10050Sstevel@tonic-gate } else if (len >= sizeof (path)) { 10060Sstevel@tonic-gate (void) mdsyserror(ep, ENAMETOOLONG, np->bname); 10070Sstevel@tonic-gate return (NULL); 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate path[len] = '\0'; 10100Sstevel@tonic-gate if ((len = strfind(path, "/devices/")) < 0) { 10110Sstevel@tonic-gate (void) mddeverror(ep, MDE_DEVICES_NAME, np->dev, np->bname); 10120Sstevel@tonic-gate return (NULL); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate /* return name */ 10160Sstevel@tonic-gate np->devicesname = Strdup(path + len + strlen("/devices")); 10170Sstevel@tonic-gate return (np->devicesname); 10180Sstevel@tonic-gate } 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate /* 10210Sstevel@tonic-gate * get metadevice misc name 10220Sstevel@tonic-gate */ 10230Sstevel@tonic-gate char * 10240Sstevel@tonic-gate metagetmiscname( 10250Sstevel@tonic-gate mdname_t *np, 10260Sstevel@tonic-gate md_error_t *ep 10270Sstevel@tonic-gate ) 10280Sstevel@tonic-gate { 10290Sstevel@tonic-gate mddrivename_t *dnp = np->drivenamep; 10300Sstevel@tonic-gate md_i_driverinfo_t mid; 10310Sstevel@tonic-gate 10320Sstevel@tonic-gate /* short circuit */ 10330Sstevel@tonic-gate if (dnp->miscname != NULL) 10340Sstevel@tonic-gate return (dnp->miscname); 10350Sstevel@tonic-gate if (metachkmeta(np, ep) != 0) 10360Sstevel@tonic-gate return (NULL); 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate /* get misc module from driver */ 10390Sstevel@tonic-gate (void) memset(&mid, 0, sizeof (mid)); 10400Sstevel@tonic-gate mid.mnum = meta_getminor(np->dev); 10410Sstevel@tonic-gate if (metaioctl(MD_IOCGET_DRVNM, &mid, &mid.mde, np->cname) != 0) { 10420Sstevel@tonic-gate (void) mdstealerror(ep, &mid.mde); 10430Sstevel@tonic-gate return (NULL); 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate 10460Sstevel@tonic-gate /* return miscname */ 10470Sstevel@tonic-gate dnp->miscname = Strdup(MD_PNTDRIVERNAME(&mid)); 10480Sstevel@tonic-gate return (dnp->miscname); 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate /* 10520Sstevel@tonic-gate * get unit structure from driver 10530Sstevel@tonic-gate */ 10540Sstevel@tonic-gate md_unit_t * 10550Sstevel@tonic-gate meta_get_mdunit( 10560Sstevel@tonic-gate mdsetname_t *sp, 10570Sstevel@tonic-gate mdname_t *np, 10580Sstevel@tonic-gate md_error_t *ep 10590Sstevel@tonic-gate ) 10600Sstevel@tonic-gate { 10610Sstevel@tonic-gate md_i_get_t mig; 10620Sstevel@tonic-gate char *miscname = NULL; 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate /* should have a set */ 10650Sstevel@tonic-gate assert(sp != NULL); 10660Sstevel@tonic-gate assert(sp->setno == MD_MIN2SET(meta_getminor(np->dev))); 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate /* get size of unit structure */ 10690Sstevel@tonic-gate if (metachkmeta(np, ep) != 0) 10700Sstevel@tonic-gate return (NULL); 10710Sstevel@tonic-gate if ((miscname = metagetmiscname(np, ep)) == NULL) 10720Sstevel@tonic-gate return (NULL); 10730Sstevel@tonic-gate (void) memset(&mig, '\0', sizeof (mig)); 10740Sstevel@tonic-gate MD_SETDRIVERNAME(&mig, miscname, sp->setno); 10750Sstevel@tonic-gate mig.id = meta_getminor(np->dev); 10760Sstevel@tonic-gate if (metaioctl(MD_IOCGET, &mig, &mig.mde, np->cname) != 0) { 10770Sstevel@tonic-gate (void) mdstealerror(ep, &mig.mde); 10780Sstevel@tonic-gate return (NULL); 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate /* get actual unit structure */ 10820Sstevel@tonic-gate assert(mig.size > 0); 10830Sstevel@tonic-gate mig.mdp = (uintptr_t)Zalloc(mig.size); 10840Sstevel@tonic-gate if (metaioctl(MD_IOCGET, &mig, &mig.mde, np->cname) != 0) { 10850Sstevel@tonic-gate (void) mdstealerror(ep, &mig.mde); 108662Sjeanm Free((void *)(uintptr_t)mig.mdp); 10870Sstevel@tonic-gate return (NULL); 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate 109062Sjeanm return ((md_unit_t *)(uintptr_t)mig.mdp); 10910Sstevel@tonic-gate } 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate /* 10940Sstevel@tonic-gate * free metadevice unit 10950Sstevel@tonic-gate */ 10960Sstevel@tonic-gate void 10970Sstevel@tonic-gate meta_free_unit( 10980Sstevel@tonic-gate mddrivename_t *dnp 10990Sstevel@tonic-gate ) 11000Sstevel@tonic-gate { 11010Sstevel@tonic-gate if (dnp->unitp != NULL) { 11020Sstevel@tonic-gate switch (dnp->unitp->type) { 11030Sstevel@tonic-gate case MD_DEVICE: 11040Sstevel@tonic-gate meta_free_stripe((md_stripe_t *)dnp->unitp); 11050Sstevel@tonic-gate break; 11060Sstevel@tonic-gate case MD_METAMIRROR: 11070Sstevel@tonic-gate meta_free_mirror((md_mirror_t *)dnp->unitp); 11080Sstevel@tonic-gate break; 11090Sstevel@tonic-gate case MD_METATRANS: 11100Sstevel@tonic-gate meta_free_trans((md_trans_t *)dnp->unitp); 11110Sstevel@tonic-gate break; 11120Sstevel@tonic-gate case MD_METARAID: 11130Sstevel@tonic-gate meta_free_raid((md_raid_t *)dnp->unitp); 11140Sstevel@tonic-gate break; 11150Sstevel@tonic-gate case MD_METASP: 11160Sstevel@tonic-gate meta_free_sp((md_sp_t *)dnp->unitp); 11170Sstevel@tonic-gate break; 11180Sstevel@tonic-gate default: 11190Sstevel@tonic-gate assert(0); 11200Sstevel@tonic-gate break; 11210Sstevel@tonic-gate } 11220Sstevel@tonic-gate dnp->unitp = NULL; 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate 11260Sstevel@tonic-gate /* 11270Sstevel@tonic-gate * free metadevice name info 11280Sstevel@tonic-gate */ 11290Sstevel@tonic-gate void 11300Sstevel@tonic-gate meta_invalidate_name( 11310Sstevel@tonic-gate mdname_t *namep 11320Sstevel@tonic-gate ) 11330Sstevel@tonic-gate { 11340Sstevel@tonic-gate mddrivename_t *dnp = namep->drivenamep; 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate /* get rid of cached name info */ 11370Sstevel@tonic-gate if (namep->devicesname != NULL) { 11380Sstevel@tonic-gate Free(namep->devicesname); 11390Sstevel@tonic-gate namep->devicesname = NULL; 11400Sstevel@tonic-gate } 11410Sstevel@tonic-gate namep->key = MD_KEYBAD; 11420Sstevel@tonic-gate namep->start_blk = -1; 11430Sstevel@tonic-gate namep->end_blk = -1; 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate /* get rid of cached drivename info */ 11460Sstevel@tonic-gate (void) memset(&dnp->geom, 0, sizeof (dnp->geom)); 11470Sstevel@tonic-gate (void) memset(&dnp->cinfo, 0, sizeof (dnp->cinfo)); 11480Sstevel@tonic-gate metafreevtoc(&dnp->vtoc); 11490Sstevel@tonic-gate metaflushsidenames(dnp); 11500Sstevel@tonic-gate dnp->side_names_key = MD_KEYBAD; 11510Sstevel@tonic-gate if (dnp->miscname != NULL) { 11520Sstevel@tonic-gate Free(dnp->miscname); 11530Sstevel@tonic-gate dnp->miscname = NULL; 11540Sstevel@tonic-gate } 11550Sstevel@tonic-gate meta_free_unit(dnp); 11560Sstevel@tonic-gate } 11570Sstevel@tonic-gate 11580Sstevel@tonic-gate /* 11590Sstevel@tonic-gate * get metadevice unit 11600Sstevel@tonic-gate */ 11610Sstevel@tonic-gate md_common_t * 11620Sstevel@tonic-gate meta_get_unit( 11630Sstevel@tonic-gate mdsetname_t *sp, 11640Sstevel@tonic-gate mdname_t *np, 11650Sstevel@tonic-gate md_error_t *ep 11660Sstevel@tonic-gate ) 11670Sstevel@tonic-gate { 11680Sstevel@tonic-gate char *miscname; 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate /* short circuit */ 11710Sstevel@tonic-gate if (np->drivenamep->unitp != NULL) 11720Sstevel@tonic-gate return (np->drivenamep->unitp); 11730Sstevel@tonic-gate if (metachkmeta(np, ep) != 0) 11740Sstevel@tonic-gate return (NULL); 11750Sstevel@tonic-gate 11760Sstevel@tonic-gate /* dispatch */ 11770Sstevel@tonic-gate if ((miscname = metagetmiscname(np, ep)) == NULL) 11780Sstevel@tonic-gate return (NULL); 11790Sstevel@tonic-gate else if (strcmp(miscname, MD_STRIPE) == 0) 11800Sstevel@tonic-gate return ((md_common_t *)meta_get_stripe(sp, np, ep)); 11810Sstevel@tonic-gate else if (strcmp(miscname, MD_MIRROR) == 0) 11820Sstevel@tonic-gate return ((md_common_t *)meta_get_mirror(sp, np, ep)); 11830Sstevel@tonic-gate else if (strcmp(miscname, MD_TRANS) == 0) 11840Sstevel@tonic-gate return ((md_common_t *)meta_get_trans(sp, np, ep)); 11850Sstevel@tonic-gate else if (strcmp(miscname, MD_RAID) == 0) 11860Sstevel@tonic-gate return ((md_common_t *)meta_get_raid(sp, np, ep)); 11870Sstevel@tonic-gate else if (strcmp(miscname, MD_SP) == 0) 11880Sstevel@tonic-gate return ((md_common_t *)meta_get_sp(sp, np, ep)); 11890Sstevel@tonic-gate else { 11900Sstevel@tonic-gate (void) mdmderror(ep, MDE_UNKNOWN_TYPE, meta_getminor(np->dev), 11910Sstevel@tonic-gate np->cname); 11920Sstevel@tonic-gate return (NULL); 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate } 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate 11970Sstevel@tonic-gate int 11980Sstevel@tonic-gate meta_isopen( 11990Sstevel@tonic-gate mdsetname_t *sp, 12000Sstevel@tonic-gate mdname_t *np, 12010Sstevel@tonic-gate md_error_t *ep, 12020Sstevel@tonic-gate mdcmdopts_t options 12030Sstevel@tonic-gate ) 12040Sstevel@tonic-gate { 12050Sstevel@tonic-gate md_isopen_t d; 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate if (metachkmeta(np, ep) != 0) 12080Sstevel@tonic-gate return (-1); 12090Sstevel@tonic-gate 12100Sstevel@tonic-gate (void) memset(&d, '\0', sizeof (d)); 12110Sstevel@tonic-gate d.dev = np->dev; 12120Sstevel@tonic-gate if (metaioctl(MD_IOCISOPEN, &d, &d.mde, np->cname) != 0) 12130Sstevel@tonic-gate return (mdstealerror(ep, &d.mde)); 12140Sstevel@tonic-gate 12150Sstevel@tonic-gate /* 12160Sstevel@tonic-gate * shortcut: if the device is open, no need to check on other nodes, 12170Sstevel@tonic-gate * even in case of a mn metadevice 12180Sstevel@tonic-gate * Also return in case we're told not to check on other nodes. 12190Sstevel@tonic-gate */ 12200Sstevel@tonic-gate if ((d.isopen != 0) || ((options & MDCMD_MN_OPEN_CHECK) == 0)) { 12210Sstevel@tonic-gate return (d.isopen); 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate 12240Sstevel@tonic-gate /* 12250Sstevel@tonic-gate * If the device is closed locally, but it's a mn device, 12260Sstevel@tonic-gate * check on all other nodes, too 12270Sstevel@tonic-gate */ 12280Sstevel@tonic-gate if (sp->setno != MD_LOCAL_SET) { 12290Sstevel@tonic-gate (void) metaget_setdesc(sp, ep); /* not supposed to fail */ 12300Sstevel@tonic-gate if (sp->setdesc->sd_flags & MD_SR_MN) { 12310Sstevel@tonic-gate int err = 0; 12320Sstevel@tonic-gate md_mn_result_t *resp; 12330Sstevel@tonic-gate /* 12340Sstevel@tonic-gate * This message is never directly issued. 12350Sstevel@tonic-gate * So we launch it with a suspend override flag. 12360Sstevel@tonic-gate * If the commd is suspended, and this message comes 12370Sstevel@tonic-gate * along it must be sent due to replaying a metainit or 12380Sstevel@tonic-gate * similar. In that case we don't want this message to 12390Sstevel@tonic-gate * be blocked. 12400Sstevel@tonic-gate * If the commd is not suspended, the flag does no harm. 12410Sstevel@tonic-gate * Additionally we don't want the result of the message 12420Sstevel@tonic-gate * cached in the MCT, because we want uptodate results, 12430Sstevel@tonic-gate * and the message doesn't need being logged either. 12440Sstevel@tonic-gate * Hence NO_LOG and NO_MCT 12450Sstevel@tonic-gate */ 12460Sstevel@tonic-gate err = mdmn_send_message( 1247*7464SAndrew.Balfour@Sun.COM sp->setno, 1248*7464SAndrew.Balfour@Sun.COM MD_MN_MSG_CLU_CHECK, 1249*7464SAndrew.Balfour@Sun.COM MD_MSGF_NO_MCT | MD_MSGF_STOP_ON_ERROR | 1250*7464SAndrew.Balfour@Sun.COM MD_MSGF_NO_LOG | MD_MSGF_OVERRIDE_SUSPEND, 1251*7464SAndrew.Balfour@Sun.COM (char *)&d, sizeof (md_isopen_t), 1252*7464SAndrew.Balfour@Sun.COM &resp, ep); 12530Sstevel@tonic-gate if (err == 0) { 12540Sstevel@tonic-gate d.isopen = resp->mmr_exitval; 12550Sstevel@tonic-gate } else { 12560Sstevel@tonic-gate /* 12570Sstevel@tonic-gate * in case some error occurred, 12580Sstevel@tonic-gate * we better say the device is open 12590Sstevel@tonic-gate */ 12600Sstevel@tonic-gate d.isopen = 1; 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate if (resp != (md_mn_result_t *)NULL) { 12630Sstevel@tonic-gate free_result(resp); 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate } 12680Sstevel@tonic-gate 12690Sstevel@tonic-gate return (d.isopen); 12700Sstevel@tonic-gate } 1271