1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 30*7c478bd9Sstevel@tonic-gate #include <libdevinfo.h> 31*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #include "libdiskmgt.h" 36*7c478bd9Sstevel@tonic-gate #include "disks_private.h" 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate static int add_path_state(descriptor_t *dp, nvlist_t *attrs); 39*7c478bd9Sstevel@tonic-gate static int add_wwn(descriptor_t *dp, nvlist_t *attrs); 40*7c478bd9Sstevel@tonic-gate static descriptor_t **get_assoc_drives(descriptor_t *desc, int *errp); 41*7c478bd9Sstevel@tonic-gate static descriptor_t **get_assoc_controllers(descriptor_t *desc, int *errp); 42*7c478bd9Sstevel@tonic-gate static char *path_state_name(di_path_state_t st); 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate descriptor_t ** 45*7c478bd9Sstevel@tonic-gate path_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type, int *errp) 46*7c478bd9Sstevel@tonic-gate { 47*7c478bd9Sstevel@tonic-gate switch (type) { 48*7c478bd9Sstevel@tonic-gate case DM_DRIVE: 49*7c478bd9Sstevel@tonic-gate return (get_assoc_drives(desc, errp)); 50*7c478bd9Sstevel@tonic-gate case DM_CONTROLLER: 51*7c478bd9Sstevel@tonic-gate return (get_assoc_controllers(desc, errp)); 52*7c478bd9Sstevel@tonic-gate } 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate *errp = EINVAL; 55*7c478bd9Sstevel@tonic-gate return (NULL); 56*7c478bd9Sstevel@tonic-gate } 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate nvlist_t * 59*7c478bd9Sstevel@tonic-gate path_get_attributes(descriptor_t *dp, int *errp) 60*7c478bd9Sstevel@tonic-gate { 61*7c478bd9Sstevel@tonic-gate path_t *pp; 62*7c478bd9Sstevel@tonic-gate nvlist_t *attrs = NULL; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate pp = dp->p.path; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) { 67*7c478bd9Sstevel@tonic-gate *errp = ENOMEM; 68*7c478bd9Sstevel@tonic-gate return (NULL); 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate if (nvlist_add_string(attrs, DM_CTYPE, pp->ctype) != 0) { 72*7c478bd9Sstevel@tonic-gate nvlist_free(attrs); 73*7c478bd9Sstevel@tonic-gate *errp = ENOMEM; 74*7c478bd9Sstevel@tonic-gate return (NULL); 75*7c478bd9Sstevel@tonic-gate } 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate /* 78*7c478bd9Sstevel@tonic-gate * We add the path state and wwn attributes only for descriptors that 79*7c478bd9Sstevel@tonic-gate * we got via their association to a specific drive (since these 80*7c478bd9Sstevel@tonic-gate * attributes are drive specific). 81*7c478bd9Sstevel@tonic-gate */ 82*7c478bd9Sstevel@tonic-gate if (dp->name != NULL) { 83*7c478bd9Sstevel@tonic-gate if (add_path_state(dp, attrs) != 0) { 84*7c478bd9Sstevel@tonic-gate nvlist_free(attrs); 85*7c478bd9Sstevel@tonic-gate *errp = ENOMEM; 86*7c478bd9Sstevel@tonic-gate return (NULL); 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate if (add_wwn(dp, attrs) != 0) { 89*7c478bd9Sstevel@tonic-gate nvlist_free(attrs); 90*7c478bd9Sstevel@tonic-gate *errp = ENOMEM; 91*7c478bd9Sstevel@tonic-gate return (NULL); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate } 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate *errp = 0; 96*7c478bd9Sstevel@tonic-gate return (attrs); 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate descriptor_t * 100*7c478bd9Sstevel@tonic-gate path_get_descriptor_by_name(char *name, int *errp) 101*7c478bd9Sstevel@tonic-gate { 102*7c478bd9Sstevel@tonic-gate descriptor_t **paths; 103*7c478bd9Sstevel@tonic-gate int i; 104*7c478bd9Sstevel@tonic-gate descriptor_t *path = NULL; 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate paths = cache_get_descriptors(DM_PATH, errp); 107*7c478bd9Sstevel@tonic-gate if (*errp != 0) { 108*7c478bd9Sstevel@tonic-gate return (NULL); 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate for (i = 0; paths[i]; i++) { 112*7c478bd9Sstevel@tonic-gate if (libdiskmgt_str_eq(name, paths[i]->p.path->name)) { 113*7c478bd9Sstevel@tonic-gate path = paths[i]; 114*7c478bd9Sstevel@tonic-gate } else { 115*7c478bd9Sstevel@tonic-gate /* clean up the unused descriptors */ 116*7c478bd9Sstevel@tonic-gate cache_free_descriptor(paths[i]); 117*7c478bd9Sstevel@tonic-gate } 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate free(paths); 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate if (path == NULL) { 122*7c478bd9Sstevel@tonic-gate *errp = ENODEV; 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate return (NULL); 126*7c478bd9Sstevel@tonic-gate } 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 129*7c478bd9Sstevel@tonic-gate descriptor_t ** 130*7c478bd9Sstevel@tonic-gate path_get_descriptors(int filter[], int *errp) 131*7c478bd9Sstevel@tonic-gate { 132*7c478bd9Sstevel@tonic-gate return (cache_get_descriptors(DM_PATH, errp)); 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate char * 136*7c478bd9Sstevel@tonic-gate path_get_name(descriptor_t *desc) 137*7c478bd9Sstevel@tonic-gate { 138*7c478bd9Sstevel@tonic-gate return (desc->p.path->name); 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 142*7c478bd9Sstevel@tonic-gate nvlist_t * 143*7c478bd9Sstevel@tonic-gate path_get_stats(descriptor_t *dp, int stat_type, int *errp) 144*7c478bd9Sstevel@tonic-gate { 145*7c478bd9Sstevel@tonic-gate /* There are no stat types defined for paths */ 146*7c478bd9Sstevel@tonic-gate *errp = EINVAL; 147*7c478bd9Sstevel@tonic-gate return (NULL); 148*7c478bd9Sstevel@tonic-gate } 149*7c478bd9Sstevel@tonic-gate 150*7c478bd9Sstevel@tonic-gate int 151*7c478bd9Sstevel@tonic-gate path_make_descriptors() 152*7c478bd9Sstevel@tonic-gate { 153*7c478bd9Sstevel@tonic-gate int error; 154*7c478bd9Sstevel@tonic-gate controller_t *cp; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate cp = cache_get_controllerlist(); 157*7c478bd9Sstevel@tonic-gate while (cp != NULL) { 158*7c478bd9Sstevel@tonic-gate if (cp->paths != NULL) { 159*7c478bd9Sstevel@tonic-gate int i; 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate for (i = 0; cp->paths[i]; i++) { 162*7c478bd9Sstevel@tonic-gate cache_load_desc(DM_PATH, cp->paths[i], NULL, NULL, &error); 163*7c478bd9Sstevel@tonic-gate if (error != 0) { 164*7c478bd9Sstevel@tonic-gate return (error); 165*7c478bd9Sstevel@tonic-gate } 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate cp = cp->next; 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate 171*7c478bd9Sstevel@tonic-gate return (0); 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate /* 175*7c478bd9Sstevel@tonic-gate * This is called when we have a name in the descriptor name field. That 176*7c478bd9Sstevel@tonic-gate * only will be the case when the descriptor was created by getting the 177*7c478bd9Sstevel@tonic-gate * association from a drive to the path. Since we filled in the name with 178*7c478bd9Sstevel@tonic-gate * the drive device id in that case, we can use the device id to look up the 179*7c478bd9Sstevel@tonic-gate * drive-path state. 180*7c478bd9Sstevel@tonic-gate */ 181*7c478bd9Sstevel@tonic-gate static int 182*7c478bd9Sstevel@tonic-gate add_path_state(descriptor_t *dp, nvlist_t *attrs) 183*7c478bd9Sstevel@tonic-gate { 184*7c478bd9Sstevel@tonic-gate ddi_devid_t devid; 185*7c478bd9Sstevel@tonic-gate path_t *pp; 186*7c478bd9Sstevel@tonic-gate int i; 187*7c478bd9Sstevel@tonic-gate int status = 0; 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate if (devid_str_decode(dp->name, &devid, NULL) != 0) { 190*7c478bd9Sstevel@tonic-gate return (0); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate /* find the index of the correct drive assoc. */ 194*7c478bd9Sstevel@tonic-gate pp = dp->p.path; 195*7c478bd9Sstevel@tonic-gate for (i = 0; pp->disks[i] && pp->states[i] != -1; i++) { 196*7c478bd9Sstevel@tonic-gate if (pp->disks[i]->devid != NULL && 197*7c478bd9Sstevel@tonic-gate devid_compare(pp->disks[i]->devid, devid) == 0) { 198*7c478bd9Sstevel@tonic-gate 199*7c478bd9Sstevel@tonic-gate /* add the corresponding state */ 200*7c478bd9Sstevel@tonic-gate if (nvlist_add_string(attrs, DM_PATH_STATE, 201*7c478bd9Sstevel@tonic-gate path_state_name(pp->states[i])) != 0) { 202*7c478bd9Sstevel@tonic-gate status = ENOMEM; 203*7c478bd9Sstevel@tonic-gate } 204*7c478bd9Sstevel@tonic-gate break; 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate devid_free(devid); 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate return (status); 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate /* 213*7c478bd9Sstevel@tonic-gate * This is called when we have a name in the descriptor name field. That 214*7c478bd9Sstevel@tonic-gate * only will be the case when the descriptor was created by getting the 215*7c478bd9Sstevel@tonic-gate * association from a drive to the path. Since we filled in the name with 216*7c478bd9Sstevel@tonic-gate * the drive device id in that case, we can use the device id to look up the 217*7c478bd9Sstevel@tonic-gate * drive wwn. 218*7c478bd9Sstevel@tonic-gate */ 219*7c478bd9Sstevel@tonic-gate static int 220*7c478bd9Sstevel@tonic-gate add_wwn(descriptor_t *dp, nvlist_t *attrs) 221*7c478bd9Sstevel@tonic-gate { 222*7c478bd9Sstevel@tonic-gate ddi_devid_t devid; 223*7c478bd9Sstevel@tonic-gate path_t *pp; 224*7c478bd9Sstevel@tonic-gate int i; 225*7c478bd9Sstevel@tonic-gate int status = 0; 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate if (devid_str_decode(dp->name, &devid, NULL) != 0) { 228*7c478bd9Sstevel@tonic-gate return (0); 229*7c478bd9Sstevel@tonic-gate } 230*7c478bd9Sstevel@tonic-gate 231*7c478bd9Sstevel@tonic-gate /* find the index of the correct drive assoc. */ 232*7c478bd9Sstevel@tonic-gate pp = dp->p.path; 233*7c478bd9Sstevel@tonic-gate for (i = 0; pp->disks[i] && pp->states[i] != -1; i++) { 234*7c478bd9Sstevel@tonic-gate if (pp->disks[i]->devid != NULL && 235*7c478bd9Sstevel@tonic-gate devid_compare(pp->disks[i]->devid, devid) == 0) { 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate /* add the corresponding state */ 238*7c478bd9Sstevel@tonic-gate if (nvlist_add_string(attrs, DM_WWN, pp->wwns[i]) != 0) { 239*7c478bd9Sstevel@tonic-gate status = ENOMEM; 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate break; 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate } 244*7c478bd9Sstevel@tonic-gate devid_free(devid); 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate return (status); 247*7c478bd9Sstevel@tonic-gate } 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate static descriptor_t ** 250*7c478bd9Sstevel@tonic-gate get_assoc_controllers(descriptor_t *desc, int *errp) 251*7c478bd9Sstevel@tonic-gate { 252*7c478bd9Sstevel@tonic-gate path_t *pp; 253*7c478bd9Sstevel@tonic-gate descriptor_t **controllers; 254*7c478bd9Sstevel@tonic-gate int i; 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate pp = desc->p.path; 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate /* a path can have just one controller */ 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate controllers = (descriptor_t **)calloc(2, sizeof (descriptor_t *)); 261*7c478bd9Sstevel@tonic-gate if (controllers == NULL) { 262*7c478bd9Sstevel@tonic-gate *errp = ENOMEM; 263*7c478bd9Sstevel@tonic-gate return (NULL); 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate i = 0; 267*7c478bd9Sstevel@tonic-gate if (pp->controller != NULL) { 268*7c478bd9Sstevel@tonic-gate controllers[i++] = cache_get_desc(DM_CONTROLLER, 269*7c478bd9Sstevel@tonic-gate pp->controller, NULL, NULL, errp); 270*7c478bd9Sstevel@tonic-gate if (*errp != 0) { 271*7c478bd9Sstevel@tonic-gate cache_free_descriptors(controllers); 272*7c478bd9Sstevel@tonic-gate return (NULL); 273*7c478bd9Sstevel@tonic-gate } 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate 276*7c478bd9Sstevel@tonic-gate controllers[i] = NULL; 277*7c478bd9Sstevel@tonic-gate 278*7c478bd9Sstevel@tonic-gate *errp = 0; 279*7c478bd9Sstevel@tonic-gate return (controllers); 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate 282*7c478bd9Sstevel@tonic-gate static descriptor_t ** 283*7c478bd9Sstevel@tonic-gate get_assoc_drives(descriptor_t *desc, int *errp) 284*7c478bd9Sstevel@tonic-gate { 285*7c478bd9Sstevel@tonic-gate path_t *pp; 286*7c478bd9Sstevel@tonic-gate descriptor_t **drives; 287*7c478bd9Sstevel@tonic-gate int cnt; 288*7c478bd9Sstevel@tonic-gate int i; 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate pp = desc->p.path; 291*7c478bd9Sstevel@tonic-gate 292*7c478bd9Sstevel@tonic-gate /* Count how many we have. */ 293*7c478bd9Sstevel@tonic-gate for (cnt = 0; pp->disks[cnt]; cnt++); 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate /* make the snapshot */ 296*7c478bd9Sstevel@tonic-gate drives = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *)); 297*7c478bd9Sstevel@tonic-gate if (drives == NULL) { 298*7c478bd9Sstevel@tonic-gate *errp = ENOMEM; 299*7c478bd9Sstevel@tonic-gate return (NULL); 300*7c478bd9Sstevel@tonic-gate } 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate for (i = 0; pp->disks[i]; i++) { 303*7c478bd9Sstevel@tonic-gate drives[i] = cache_get_desc(DM_DRIVE, pp->disks[i], NULL, NULL, 304*7c478bd9Sstevel@tonic-gate errp); 305*7c478bd9Sstevel@tonic-gate if (*errp != 0) { 306*7c478bd9Sstevel@tonic-gate cache_free_descriptors(drives); 307*7c478bd9Sstevel@tonic-gate return (NULL); 308*7c478bd9Sstevel@tonic-gate } 309*7c478bd9Sstevel@tonic-gate } 310*7c478bd9Sstevel@tonic-gate drives[i] = NULL; 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate *errp = 0; 313*7c478bd9Sstevel@tonic-gate return (drives); 314*7c478bd9Sstevel@tonic-gate } 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate static char * 317*7c478bd9Sstevel@tonic-gate path_state_name(di_path_state_t st) 318*7c478bd9Sstevel@tonic-gate { 319*7c478bd9Sstevel@tonic-gate switch (st) { 320*7c478bd9Sstevel@tonic-gate case DI_PATH_STATE_ONLINE: 321*7c478bd9Sstevel@tonic-gate return ("online"); 322*7c478bd9Sstevel@tonic-gate case DI_PATH_STATE_STANDBY: 323*7c478bd9Sstevel@tonic-gate return ("standby"); 324*7c478bd9Sstevel@tonic-gate case DI_PATH_STATE_OFFLINE: 325*7c478bd9Sstevel@tonic-gate return ("offline"); 326*7c478bd9Sstevel@tonic-gate case DI_PATH_STATE_FAULT: 327*7c478bd9Sstevel@tonic-gate return ("faulted"); 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate return ("unknown"); 330*7c478bd9Sstevel@tonic-gate } 331