Print this page
NEX-15328 libdiskmgt: memleak in findevs()
Reviewed by: Alexander Eremin <alexander.eremin@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-15328 libdiskmgt: memleak in findevs()
Reviewed by: Alexander Eremin <alexander.eremin@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-14951 teach libdiskmgt about nvme, sata and xen
Reviewed by: Dan Fields <dan.fields@nexenta.com>
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libdiskmgt/common/findevs.c
+++ new/usr/src/lib/libdiskmgt/common/findevs.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 28 * Copyright (c) 2011 by Delphix. All rights reserved.
29 29 * Copyright 2017 Nexenta Systems, Inc.
30 30 */
31 31
32 32 #include <fcntl.h>
33 33 #include <libdevinfo.h>
34 34 #include <stdio.h>
35 35 #include <stdlib.h>
36 36 #include <string.h>
37 37 #include <sys/stat.h>
38 38 #include <sys/sunddi.h>
39 39 #include <sys/types.h>
40 40 #include <sys/mkdev.h>
41 41 #include <ctype.h>
42 42 #include <libgen.h>
43 43 #include <unistd.h>
44 44 #include <devid.h>
45 45 #include <sys/fs/zfs.h>
46 46
47 47 #include "libdiskmgt.h"
48 48 #include "disks_private.h"
49 49
50 50 /* specify which disk links to use in the /dev directory */
51 51 #define DEVLINK_REGEX "rdsk/.*"
52 52 #define DEVLINK_FLOPPY_REGEX "rdiskette[0-9]"
53 53
54 54 #define FLOPPY_NAME "rdiskette"
55 55
56 56 #define MAXPROPLEN 1024
57 57 #define DEVICE_ID_PROP "devid"
58 58 #define PROD_ID_PROP "inquiry-product-id"
59 59 #define PROD_ID_USB_PROP "usb-product-name"
60 60 #define REMOVABLE_PROP "removable-media"
61 61 #define HOTPLUGGABLE_PROP "hotpluggable"
62 62 #define SCSI_OPTIONS_PROP "scsi-options"
63 63 #define VENDOR_ID_PROP "inquiry-vendor-id"
64 64 #define VENDOR_ID_USB_PROP "usb-vendor-name"
65 65 #define WWN_PROP "node-wwn"
66 66
67 67 static char *ctrltypes[] = {
68 68 DDI_NT_FC_ATTACHMENT_POINT,
69 69 DDI_NT_NVME_ATTACHMENT_POINT,
70 70 DDI_NT_SATA_ATTACHMENT_POINT,
71 71 DDI_NT_SATA_NEXUS,
72 72 DDI_NT_SCSI_ATTACHMENT_POINT,
73 73 DDI_NT_SCSI_NEXUS,
74 74 NULL
75 75 };
76 76
77 77 static char *bustypes[] = {
78 78 "sbus",
79 79 "pci",
80 80 "usb",
81 81 NULL
82 82 };
83 83
84 84 static bus_t *add_bus(struct search_args *args, di_node_t node,
85 85 di_minor_t minor, controller_t *cp);
86 86 static controller_t *add_controller(struct search_args *args,
87 87 di_node_t node, di_minor_t minor);
88 88 static int add_devpath(di_devlink_t devlink, void *arg);
89 89 static int add_devs(di_node_t node, di_minor_t minor, void *arg);
90 90 static int add_disk2controller(disk_t *diskp,
91 91 struct search_args *args);
92 92 static int add_disk2path(disk_t *dp, path_t *pp,
93 93 di_path_state_t st, char *wwn);
94 94 static int add_int2array(int p, int **parray);
95 95 static int add_ptr2array(void *p, void ***parray);
96 96 static char *bus_type(di_node_t node, di_minor_t minor,
97 97 di_prom_handle_t ph);
98 98 static void remove_controller(controller_t *cp,
99 99 controller_t *currp);
100 100 static void clean_paths(struct search_args *args);
101 101 static disk_t *create_disk(char *deviceid, char *kernel_name,
102 102 struct search_args *args);
103 103 static char *ctype(di_node_t node, di_minor_t minor);
104 104 static boolean_t disk_is_cdrom(const char *type);
105 105 static alias_t *find_alias(disk_t *diskp, char *kernel_name);
106 106 static bus_t *find_bus(struct search_args *args, char *name);
107 107 static controller_t *find_controller(struct search_args *args, char *name);
108 108 static disk_t *get_disk_by_deviceid(disk_t *listp, char *devid);
109 109 static void get_disk_name_from_path(char *path, char *name,
110 110 int size);
111 111 static char *get_byte_prop(char *prop_name, di_node_t node);
112 112 static di_node_t get_parent_bus(di_node_t node,
113 113 struct search_args *args);
114 114 static int get_prom_int(char *prop_name, di_node_t node,
115 115 di_prom_handle_t ph);
116 116 static char *get_prom_str(char *prop_name, di_node_t node,
117 117 di_prom_handle_t ph);
118 118 static int get_prop(char *prop_name, di_node_t node);
119 119 static char *get_str_prop(char *prop_name, di_node_t node);
120 120 static int have_disk(struct search_args *args, char *devid,
121 121 char *kernel_name, disk_t **diskp);
122 122 static int is_ctds(char *name);
123 123 static int is_drive(di_minor_t minor);
124 124 static int is_zvol(di_node_t node, di_minor_t minor);
125 125 static int is_ctrl(di_node_t node, di_minor_t minor);
126 126 static int new_alias(disk_t *diskp, char *kernel_path,
127 127 char *devlink_path, struct search_args *args);
128 128 static int new_devpath(alias_t *ap, char *devpath);
129 129 static path_t *new_path(controller_t *cp, disk_t *diskp,
130 130 di_node_t node, di_path_state_t st, char *wwn);
131 131 static void remove_invalid_controller(char *name,
132 132 controller_t *currp, struct search_args *args);
133 133
134 134 /*
135 135 * The functions in this file do a dev tree walk to build up a model of the
136 136 * disks, controllers and paths on the system. This model is returned in the
137 137 * args->disk_listp and args->controller_listp members of the args param.
138 138 * There is no global data for this file so it is thread safe. It is up to
139 139 * the caller to merge the resulting model with any existing model that is
140 140 * cached. The caller must also free the memory for this model when it is
141 141 * no longer needed.
142 142 */
143 143 void
|
↓ open down ↓ |
143 lines elided |
↑ open up ↑ |
144 144 findevs(struct search_args *args)
145 145 {
146 146 di_node_t di_root;
147 147
148 148 args->bus_listp = NULL;
149 149 args->controller_listp = NULL;
150 150 args->disk_listp = NULL;
151 151
152 152 args->dev_walk_status = 0;
153 153 args->handle = di_devlink_init(NULL, 0);
154 + args->ph = di_prom_init();
154 155
155 156 /*
156 157 * Have to make several passes at this with the new devfs caching.
157 158 * First, we find non-mpxio devices. Then we find mpxio/multipath
158 159 * devices.
159 160 */
160 161 di_root = di_init("/", DINFOCACHE);
161 - args->ph = di_prom_init();
162 162 (void) di_walk_minor(di_root, NULL, 0, args, add_devs);
163 163 di_fini(di_root);
164 164
165 165 di_root = di_init("/", DINFOCPYALL|DINFOPATH);
166 166 (void) di_walk_minor(di_root, NULL, 0, args, add_devs);
167 167 di_fini(di_root);
168 168
169 + if (args->ph != DI_PROM_HANDLE_NIL)
170 + di_prom_fini(args->ph);
169 171 (void) di_devlink_fini(&(args->handle));
170 172
171 173 clean_paths(args);
172 174 }
173 175
174 176 /*
175 177 * Definitions of private functions
176 178 */
177 179
178 180 static bus_t *
179 181 add_bus(struct search_args *args, di_node_t node, di_minor_t minor,
180 182 controller_t *cp)
181 183 {
182 184 char *btype;
183 185 char *devpath;
184 186 bus_t *bp;
185 187 char kstat_name[MAXPATHLEN];
186 188 di_node_t pnode;
187 189
188 190 if (node == DI_NODE_NIL) {
189 191 return (NULL);
190 192 }
191 193
192 194 if ((btype = bus_type(node, minor, args->ph)) == NULL) {
193 195 return (add_bus(args, di_parent_node(node),
194 196 di_minor_next(di_parent_node(node), NULL), cp));
195 197 }
196 198
197 199 devpath = di_devfs_path(node);
198 200
199 201 if ((bp = find_bus(args, devpath)) != NULL) {
200 202 di_devfs_path_free((void *) devpath);
201 203
202 204 if (cp != NULL) {
203 205 if (add_ptr2array(cp,
204 206 (void ***)&bp->controllers) != 0) {
205 207 args->dev_walk_status = ENOMEM;
206 208 return (NULL);
207 209 }
208 210 }
209 211 return (bp);
210 212 }
211 213
212 214 /* Special handling for root node. */
213 215 if (strcmp(devpath, "/") == 0) {
214 216 di_devfs_path_free((void *) devpath);
215 217 return (NULL);
216 218 }
217 219
218 220 if (dm_debug) {
219 221 (void) fprintf(stderr, "INFO: add_bus %s\n", devpath);
220 222 }
221 223
222 224 bp = (bus_t *)calloc(1, sizeof (bus_t));
223 225 if (bp == NULL) {
224 226 return (NULL);
225 227 }
226 228
227 229 bp->name = strdup(devpath);
228 230 di_devfs_path_free((void *) devpath);
229 231 if (bp->name == NULL) {
230 232 args->dev_walk_status = ENOMEM;
231 233 cache_free_bus(bp);
232 234 return (NULL);
233 235 }
234 236
235 237 bp->btype = strdup(btype);
236 238 if (bp->btype == NULL) {
237 239 args->dev_walk_status = ENOMEM;
238 240 cache_free_bus(bp);
239 241 return (NULL);
240 242 }
241 243
242 244 (void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
243 245 di_node_name(node), di_instance(node));
244 246
245 247 if ((bp->kstat_name = strdup(kstat_name)) == NULL) {
246 248 args->dev_walk_status = ENOMEM;
247 249 cache_free_bus(bp);
248 250 return (NULL);
249 251 }
250 252
251 253 /* if parent node is a bus, get its name */
252 254 if ((pnode = get_parent_bus(node, args)) != NULL) {
253 255 devpath = di_devfs_path(pnode);
254 256 bp->pname = strdup(devpath);
255 257 di_devfs_path_free((void *) devpath);
256 258 if (bp->pname == NULL) {
257 259 args->dev_walk_status = ENOMEM;
258 260 cache_free_bus(bp);
259 261 return (NULL);
260 262 }
261 263
262 264 } else {
263 265 bp->pname = NULL;
264 266 }
265 267
266 268 bp->freq = get_prom_int("clock-frequency", node, args->ph);
267 269
268 270 bp->controllers = (controller_t **)calloc(1, sizeof (controller_t *));
269 271 if (bp->controllers == NULL) {
270 272 args->dev_walk_status = ENOMEM;
271 273 cache_free_bus(bp);
272 274 return (NULL);
273 275 }
274 276 bp->controllers[0] = NULL;
275 277
276 278 if (cp != NULL) {
277 279 if (add_ptr2array(cp, (void ***)&bp->controllers) != 0) {
278 280 args->dev_walk_status = ENOMEM;
279 281 return (NULL);
280 282 }
281 283 }
282 284
283 285 bp->next = args->bus_listp;
284 286 args->bus_listp = bp;
285 287
286 288 return (bp);
287 289 }
288 290
289 291 static controller_t *
290 292 add_controller(struct search_args *args, di_node_t node, di_minor_t minor)
291 293 {
292 294 char *devpath;
293 295 controller_t *cp;
294 296 char kstat_name[MAXPATHLEN];
295 297 char *c_type = DM_CTYPE_UNKNOWN;
296 298
297 299 devpath = di_devfs_path(node);
298 300
299 301 if ((cp = find_controller(args, devpath)) != NULL) {
300 302 di_devfs_path_free((void *) devpath);
301 303 return (cp);
302 304 }
303 305
304 306 /* Special handling for fp attachment node. */
305 307 if (strcmp(di_node_name(node), "fp") == 0) {
306 308 di_node_t pnode;
307 309
308 310 pnode = di_parent_node(node);
309 311 if (pnode != DI_NODE_NIL) {
310 312 di_devfs_path_free((void *) devpath);
311 313 devpath = di_devfs_path(pnode);
312 314
313 315 if ((cp = find_controller(args, devpath)) != NULL) {
314 316 di_devfs_path_free((void *) devpath);
315 317 return (cp);
316 318 }
317 319
318 320 /* not in the list, create it */
319 321 node = pnode;
320 322 c_type = DM_CTYPE_FIBRE;
321 323 }
322 324 }
323 325
324 326 if (dm_debug) {
325 327 (void) fprintf(stderr, "INFO: add_controller %s\n", devpath);
326 328 }
327 329
328 330 cp = (controller_t *)calloc(1, sizeof (controller_t));
329 331 if (cp == NULL) {
330 332 return (NULL);
331 333 }
332 334
333 335 cp->name = strdup(devpath);
334 336 di_devfs_path_free((void *) devpath);
335 337 if (cp->name == NULL) {
336 338 cache_free_controller(cp);
337 339 return (NULL);
338 340 }
339 341
340 342 if (strcmp(c_type, DM_CTYPE_UNKNOWN) == 0) {
341 343 c_type = ctype(node, minor);
342 344 }
343 345 cp->ctype = c_type;
344 346
345 347 (void) snprintf(kstat_name, sizeof (kstat_name), "%s%d",
346 348 di_node_name(node), di_instance(node));
347 349
348 350 if ((cp->kstat_name = strdup(kstat_name)) == NULL) {
349 351 cache_free_controller(cp);
350 352 return (NULL);
351 353 }
352 354
353 355 if (libdiskmgt_str_eq(cp->ctype, "scsi")) {
354 356 cp->scsi_options = get_prop(SCSI_OPTIONS_PROP, node);
355 357 }
356 358
357 359 if (libdiskmgt_str_eq(di_node_name(node), "scsi_vhci")) {
358 360 cp->multiplex = 1;
359 361 } else {
360 362 cp->multiplex = 0;
361 363 }
362 364
363 365 cp->freq = get_prom_int("clock-frequency", node, args->ph);
364 366
365 367 cp->disks = (disk_t **)calloc(1, sizeof (disk_t *));
366 368 if (cp->disks == NULL) {
367 369 cache_free_controller(cp);
368 370 return (NULL);
369 371 }
370 372 cp->disks[0] = NULL;
371 373
372 374 cp->next = args->controller_listp;
373 375 args->controller_listp = cp;
374 376
375 377 cp->bus = add_bus(args, di_parent_node(node),
376 378 di_minor_next(di_parent_node(node), NULL), cp);
377 379
378 380 return (cp);
379 381 }
380 382
381 383 static int
382 384 add_devpath(di_devlink_t devlink, void *arg)
383 385 {
384 386 struct search_args *args;
385 387 char *devidstr;
386 388 disk_t *diskp;
387 389 char kernel_name[MAXPATHLEN];
388 390
389 391 args = (struct search_args *)arg;
390 392
391 393 /*
392 394 * Get the diskp value from calling have_disk. Can either be found
393 395 * by kernel name or devid.
394 396 */
395 397
396 398 diskp = NULL;
397 399 devidstr = get_str_prop(DEVICE_ID_PROP, args->node);
398 400 (void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
399 401 di_node_name(args->node), di_instance(args->node));
400 402
401 403 (void) have_disk(args, devidstr, kernel_name, &diskp);
402 404
403 405 /*
404 406 * The devlink_path is usually of the form /dev/rdsk/c0t0d0s0.
405 407 * For diskettes it is /dev/rdiskette*.
406 408 * On Intel we would also get each fdisk partition as well
407 409 * (e.g. /dev/rdsk/c0t0d0p0).
408 410 */
409 411 if (diskp != NULL) {
410 412 alias_t *ap;
411 413 char *devlink_path;
412 414
413 415 if (diskp->drv_type != DM_DT_FLOPPY) {
414 416 /*
415 417 * Add other controllers for multipath disks.
416 418 * This will have no effect if the controller
417 419 * relationship is already set up.
418 420 */
419 421 if (add_disk2controller(diskp, args) != 0) {
420 422 args->dev_walk_status = ENOMEM;
421 423 }
422 424 }
423 425
424 426 (void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
425 427 di_node_name(args->node), di_instance(args->node));
426 428 devlink_path = (char *)di_devlink_path(devlink);
427 429
428 430 if (dm_debug > 1) {
429 431 (void) fprintf(stderr,
430 432 "INFO: devpath %s\n", devlink_path);
431 433 }
432 434
433 435 if ((ap = find_alias(diskp, kernel_name)) == NULL) {
434 436 if (new_alias(diskp, kernel_name, devlink_path,
435 437 args) != 0) {
436 438 args->dev_walk_status = ENOMEM;
437 439 }
438 440 } else {
439 441 /*
440 442 * It is possible that we have already added this
441 443 * devpath. Do not add it again. new_devpath will
442 444 * return a 0 if found, and not add the path.
443 445 */
444 446 if (new_devpath(ap, devlink_path) != 0) {
445 447 args->dev_walk_status = ENOMEM;
446 448 }
447 449 }
448 450 }
449 451
450 452 return (DI_WALK_CONTINUE);
451 453 }
452 454
453 455 static int
454 456 add_devs(di_node_t node, di_minor_t minor, void *arg)
455 457 {
456 458 struct search_args *args;
457 459 int result = DI_WALK_CONTINUE;
458 460
459 461 args = (struct search_args *)arg;
460 462
461 463 if (dm_debug > 1) {
462 464 /* This is all just debugging code */
463 465 char *devpath;
464 466 char dev_name[MAXPATHLEN];
465 467
466 468 devpath = di_devfs_path(node);
467 469 (void) snprintf(dev_name, sizeof (dev_name), "%s:%s", devpath,
468 470 di_minor_name(minor));
469 471 di_devfs_path_free((void *) devpath);
470 472
471 473 (void) fprintf(stderr,
472 474 "INFO: dev: %s, node: %s%d, minor: 0x%x, type: %s\n",
473 475 dev_name, di_node_name(node), di_instance(node),
474 476 di_minor_spectype(minor),
475 477 (di_minor_nodetype(minor) != NULL ?
476 478 di_minor_nodetype(minor) : "NULL"));
477 479 }
478 480
479 481 if (bus_type(node, minor, args->ph) != NULL) {
480 482 if (add_bus(args, node, minor, NULL) == NULL) {
481 483 args->dev_walk_status = ENOMEM;
482 484 result = DI_WALK_TERMINATE;
483 485 }
484 486
485 487 } else if (is_ctrl(node, minor)) {
486 488 if (add_controller(args, node, minor) == NULL) {
487 489 args->dev_walk_status = ENOMEM;
488 490 result = DI_WALK_TERMINATE;
489 491 }
490 492
491 493 } else if (di_minor_spectype(minor) == S_IFCHR &&
492 494 (is_drive(minor) || is_zvol(node, minor))) {
493 495 char *devidstr;
494 496 char kernel_name[MAXPATHLEN];
495 497 disk_t *diskp;
496 498
497 499 (void) snprintf(kernel_name, sizeof (kernel_name), "%s%d",
498 500 di_node_name(node), di_instance(node));
499 501 devidstr = get_str_prop(DEVICE_ID_PROP, node);
500 502
501 503 args->node = node;
502 504 args->minor = minor;
503 505 /*
504 506 * Check if we already got this disk and
505 507 * this is another slice.
506 508 */
507 509 if (!have_disk(args, devidstr, kernel_name, &diskp)) {
508 510 args->dev_walk_status = 0;
509 511 /*
510 512 * This is a newly found disk, create the
511 513 * disk structure.
512 514 */
513 515 diskp = create_disk(devidstr, kernel_name, args);
514 516 if (diskp == NULL) {
515 517 args->dev_walk_status = ENOMEM;
516 518 }
517 519
518 520 if (diskp->drv_type != DM_DT_FLOPPY) {
519 521 /* add the controller relationship */
520 522 if (args->dev_walk_status == 0) {
521 523 if (add_disk2controller(diskp,
522 524 args) != 0) {
523 525 args->dev_walk_status = ENOMEM;
524 526 }
525 527 }
526 528 }
527 529 }
528 530 if (is_zvol(node, minor)) {
529 531 char zvdsk[MAXNAMELEN];
530 532 char *str;
531 533 alias_t *ap;
532 534
533 535 if (di_prop_lookup_strings(di_minor_devt(minor),
534 536 node, "name", &str) == -1)
535 537 return (DI_WALK_CONTINUE);
536 538 (void) snprintf(zvdsk, MAXNAMELEN, "/dev/zvol/rdsk/%s",
537 539 str);
538 540 if ((ap = find_alias(diskp, kernel_name)) == NULL) {
539 541 if (new_alias(diskp, kernel_name,
540 542 zvdsk, args) != 0) {
541 543 args->dev_walk_status = ENOMEM;
542 544 }
543 545 } else {
544 546 /*
545 547 * It is possible that we have already added
546 548 * this devpath.
547 549 * Do not add it again. new_devpath will
548 550 * return a 0 if found, and not add the path.
549 551 */
550 552 if (new_devpath(ap, zvdsk) != 0) {
551 553 args->dev_walk_status = ENOMEM;
552 554 }
553 555 }
554 556 }
555 557
556 558 /* Add the devpaths for the drive. */
557 559 if (args->dev_walk_status == 0) {
558 560 char *devpath;
559 561 char slice_path[MAXPATHLEN];
560 562 char *pattern;
561 563
562 564 /*
563 565 * We will come through here once for each of
564 566 * the raw slice device names.
565 567 */
566 568 devpath = di_devfs_path(node);
567 569 (void) snprintf(slice_path,
568 570 sizeof (slice_path), "%s:%s",
569 571 devpath, di_minor_name(minor));
570 572 di_devfs_path_free((void *) devpath);
571 573
572 574 if (libdiskmgt_str_eq(di_minor_nodetype(minor),
573 575 DDI_NT_FD)) {
574 576 pattern = DEVLINK_FLOPPY_REGEX;
575 577 } else {
576 578 pattern = DEVLINK_REGEX;
577 579 }
578 580
579 581 /* Walk the /dev tree to get the devlinks. */
580 582 (void) di_devlink_walk(args->handle, pattern,
581 583 slice_path, DI_PRIMARY_LINK, arg, add_devpath);
582 584 }
583 585
584 586 if (args->dev_walk_status != 0) {
585 587 result = DI_WALK_TERMINATE;
586 588 }
587 589 }
588 590
589 591 return (result);
590 592 }
591 593
592 594 static int
593 595 add_disk2controller(disk_t *diskp, struct search_args *args)
594 596 {
595 597 di_node_t pnode;
596 598 controller_t *cp;
597 599 di_minor_t minor;
598 600 di_node_t node;
599 601 int i;
600 602
601 603 node = args->node;
602 604
603 605 pnode = di_parent_node(node);
604 606 if (pnode == DI_NODE_NIL) {
605 607 return (0);
606 608 }
607 609
608 610 minor = di_minor_next(pnode, NULL);
609 611 if (minor == NULL) {
610 612 return (0);
611 613 }
612 614
613 615 if ((cp = add_controller(args, pnode, minor)) == NULL) {
614 616 return (ENOMEM);
615 617 }
616 618
617 619 /* check if the disk <-> ctrl assoc is already there */
618 620 for (i = 0; diskp->controllers[i]; i++) {
619 621 if (cp == diskp->controllers[i]) {
620 622 return (0);
621 623 }
622 624 }
623 625
624 626 /* this is a new controller for this disk */
625 627
626 628 /* add the disk to the controller */
627 629 if (add_ptr2array(diskp, (void ***)&cp->disks) != 0) {
628 630 return (ENOMEM);
629 631 }
630 632
631 633 /* add the controller to the disk */
632 634 if (add_ptr2array(cp, (void ***)&diskp->controllers) != 0) {
633 635 return (ENOMEM);
634 636 }
635 637
636 638 /*
637 639 * Set up paths for mpxio controlled drives.
638 640 */
639 641 if (libdiskmgt_str_eq(di_node_name(pnode), "scsi_vhci")) {
640 642 /* note: mpxio di_path stuff is all consolidation private */
641 643 di_path_t pi = DI_PATH_NIL;
642 644
643 645 while (
644 646 (pi = di_path_client_next_path(node, pi)) != DI_PATH_NIL) {
645 647 int cnt;
646 648 uchar_t *bytes;
647 649 char str[MAXPATHLEN];
648 650 char *wwn;
649 651
650 652 di_node_t phci_node = di_path_phci_node(pi);
651 653
652 654 /* get the node wwn */
653 655 cnt = di_path_prop_lookup_bytes(pi, WWN_PROP, &bytes);
654 656 wwn = NULL;
655 657 if (cnt > 0) {
656 658 int i;
657 659 str[0] = 0;
658 660
659 661 for (i = 0; i < cnt; i++) {
660 662 /*
661 663 * A byte is only 2 hex chars + null.
662 664 */
663 665 char bstr[8];
664 666
665 667 (void) snprintf(bstr,
666 668 sizeof (bstr), "%.2x", bytes[i]);
667 669 (void) strlcat(str, bstr, sizeof (str));
668 670 }
669 671 wwn = str;
670 672 }
671 673
672 674 if (new_path(cp, diskp, phci_node,
673 675 di_path_state(pi), wwn) == NULL) {
674 676 return (ENOMEM);
675 677 }
676 678 }
677 679 }
678 680
679 681 return (0);
680 682 }
681 683
682 684 static int
683 685 add_disk2path(disk_t *dp, path_t *pp, di_path_state_t st, char *wwn)
684 686 {
685 687 /* add the disk to the path */
686 688 if (add_ptr2array(dp, (void ***)&pp->disks) != 0) {
687 689 cache_free_path(pp);
688 690 return (0);
689 691 }
690 692
691 693 /* add the path to the disk */
692 694 if (add_ptr2array(pp, (void ***)&dp->paths) != 0) {
693 695 cache_free_path(pp);
694 696 return (0);
695 697 }
696 698
697 699 /* add the path state for this disk */
698 700 if (add_int2array(st, &pp->states) != 0) {
699 701 cache_free_path(pp);
700 702 return (0);
701 703 }
702 704
703 705 /* add the path state for this disk */
704 706 if (wwn != NULL) {
705 707 char *wp;
706 708
707 709 if ((wp = strdup(wwn)) != NULL) {
708 710 if (add_ptr2array(wp, (void ***)(&pp->wwns)) != 0) {
709 711 cache_free_path(pp);
710 712 return (0);
711 713 }
712 714 }
713 715 }
714 716
715 717 return (1);
716 718 }
717 719
718 720 static int
719 721 add_int2array(int p, int **parray)
720 722 {
721 723 int i;
722 724 int cnt;
723 725 int *pa;
724 726 int *new_array;
725 727
726 728 pa = *parray;
727 729
728 730 cnt = 0;
729 731 if (pa != NULL) {
730 732 for (; pa[cnt] != -1; cnt++)
731 733 ;
732 734 }
733 735
734 736 new_array = (int *)calloc(cnt + 2, sizeof (int *));
735 737 if (new_array == NULL) {
736 738 return (ENOMEM);
737 739 }
738 740
739 741 /* copy the existing array */
740 742 for (i = 0; i < cnt; i++) {
741 743 new_array[i] = pa[i];
742 744 }
743 745
744 746 new_array[i] = p;
745 747 new_array[i + 1] = -1;
746 748
747 749 free(pa);
748 750 *parray = new_array;
749 751
750 752 return (0);
751 753 }
752 754
753 755 static int
754 756 add_ptr2array(void *p, void ***parray)
755 757 {
756 758 int i;
757 759 int cnt;
758 760 void **pa;
759 761 void **new_array;
760 762
761 763 pa = *parray;
762 764
763 765 cnt = 0;
764 766 if (pa != NULL) {
765 767 for (; pa[cnt]; cnt++)
766 768 ;
767 769 }
768 770
769 771 new_array = (void **)calloc(cnt + 2, sizeof (void *));
770 772 if (new_array == NULL) {
771 773 return (ENOMEM);
772 774 }
773 775
774 776 /* copy the existing array */
775 777 for (i = 0; i < cnt; i++) {
776 778 new_array[i] = pa[i];
777 779 }
778 780
779 781 new_array[i] = p;
780 782 new_array[i + 1] = NULL;
781 783
782 784 free(pa);
783 785 *parray = new_array;
784 786
785 787 return (0);
786 788 }
787 789
788 790 /*
789 791 * This function checks to see if a controller has other associations
790 792 * that may be valid. If we are calling this function, we have found that
791 793 * a controller for an mpxio device is showing up independently of the
792 794 * mpxio controller, noted as /scsi_vhci. This can happen with some FC
793 795 * cards that have inbound management devices that show up as well, with
794 796 * the real controller data associated. We do not want to display these
795 797 * 'devices' as real devices in libdiskmgt.
796 798 */
797 799 static void
798 800 remove_controller(controller_t *cp, controller_t *currp)
799 801 {
800 802 int i;
801 803
802 804 if (cp == currp) {
803 805 if (dm_debug) {
804 806 (void) fprintf(stderr, "ERROR: removing current"
805 807 " controller\n");
806 808 }
807 809 return;
808 810 }
809 811
810 812 if (cp->disks != NULL && cp->disks[0] != NULL) {
811 813 if (dm_debug) {
812 814 (void) fprintf(stderr,
813 815 "INFO: removing inbound management controller"
814 816 " with disk ptrs.\n");
815 817 }
816 818 /*
817 819 * loop through the disks and remove the reference to the
818 820 * controller for this disk structure. The disk itself
819 821 * is still a valid device, the controller being removed
820 822 * is a 'path' so any disk that has a reference to it
821 823 * as a controller needs to have this reference removed.
822 824 */
823 825 for (i = 0; cp->disks[i]; i++) {
824 826 disk_t *dp = cp->disks[i];
825 827 int j;
826 828
827 829 for (j = 0; dp->controllers[j]; j++) {
828 830 int k;
829 831
830 832 if (libdiskmgt_str_eq(dp->controllers[j]->name,
831 833 cp->name)) {
832 834
833 835 if (dm_debug) {
834 836 (void) fprintf(stderr,
835 837 "INFO: REMOVING disk %s on "
836 838 "controller %s\n",
837 839 dp->kernel_name, cp->name);
838 840 }
839 841 for (k = j; dp->controllers[k]; k++) {
840 842 dp->controllers[k] =
841 843 dp->controllers[k + 1];
842 844 }
843 845 }
844 846 }
845 847 }
846 848 }
847 849 /*
848 850 * Paths are removed with the call to cache_free_controller()
849 851 * below.
850 852 */
851 853
852 854 if (cp->paths != NULL && cp->paths[0] != NULL) {
853 855 if (dm_debug) {
854 856 (void) fprintf(stderr,
855 857 "INFO: removing inbound management controller"
856 858 " with path ptrs. \n");
857 859 }
858 860 }
859 861 cache_free_controller(cp);
860 862 }
861 863
862 864 /*
863 865 * If we have a controller in the list that is really a path then we need to
864 866 * take that controller out of the list since nodes that are paths are not
865 867 * considered to be controllers.
866 868 */
867 869 static void
868 870 clean_paths(struct search_args *args)
869 871 {
870 872 controller_t *cp;
871 873
872 874 cp = args->controller_listp;
873 875 while (cp != NULL) {
874 876 path_t **pp;
875 877
876 878 pp = cp->paths;
877 879 if (pp != NULL) {
878 880 int i;
879 881
880 882 for (i = 0; pp[i]; i++) {
881 883 remove_invalid_controller(pp[i]->name, cp,
882 884 args);
883 885 }
884 886 }
885 887 cp = cp->next;
886 888 }
887 889 }
888 890
889 891 static disk_t *
890 892 create_disk(char *deviceid, char *kernel_name, struct search_args *args)
891 893 {
892 894 disk_t *diskp;
893 895 char *type;
894 896 char *prod_id;
895 897 char *vendor_id;
896 898
897 899 if (dm_debug) {
898 900 (void) fprintf(stderr, "INFO: create_disk %s\n", kernel_name);
899 901 }
900 902
901 903 diskp = calloc(1, sizeof (disk_t));
902 904 if (diskp == NULL) {
903 905 return (NULL);
904 906 }
905 907
906 908 diskp->controllers = (controller_t **)
907 909 calloc(1, sizeof (controller_t *));
908 910 if (diskp->controllers == NULL) {
909 911 cache_free_disk(diskp);
910 912 return (NULL);
911 913 }
912 914 diskp->controllers[0] = NULL;
913 915
914 916 diskp->devid = NULL;
915 917 if (deviceid != NULL) {
916 918 if ((diskp->device_id = strdup(deviceid)) == NULL) {
917 919 cache_free_disk(diskp);
918 920 return (NULL);
919 921 }
920 922 (void) devid_str_decode(deviceid, &(diskp->devid), NULL);
921 923 }
922 924
923 925 if (kernel_name != NULL) {
924 926 diskp->kernel_name = strdup(kernel_name);
925 927 if (diskp->kernel_name == NULL) {
926 928 cache_free_disk(diskp);
927 929 return (NULL);
928 930 }
929 931 }
930 932
931 933 diskp->paths = NULL;
932 934 diskp->aliases = NULL;
933 935
934 936 diskp->cd_rom = 0;
935 937 diskp->rpm = 0;
936 938 diskp->solid_state = -1;
937 939 type = di_minor_nodetype(args->minor);
938 940
939 941 prod_id = get_str_prop(PROD_ID_PROP, args->node);
940 942 if (prod_id != NULL) {
941 943 if ((diskp->product_id = strdup(prod_id)) == NULL) {
942 944 cache_free_disk(diskp);
943 945 return (NULL);
944 946 }
945 947 } else {
946 948 prod_id = get_str_prop(PROD_ID_USB_PROP, args->node);
947 949 if (prod_id != NULL) {
948 950 if ((diskp->product_id = strdup(prod_id)) == NULL) {
949 951 cache_free_disk(diskp);
950 952 return (NULL);
951 953 }
952 954 }
953 955 }
954 956
955 957 vendor_id = get_str_prop(VENDOR_ID_PROP, args->node);
956 958 if (vendor_id != NULL) {
957 959 if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
958 960 cache_free_disk(diskp);
959 961 return (NULL);
960 962 }
961 963 } else {
962 964 vendor_id = get_str_prop(VENDOR_ID_USB_PROP, args->node);
963 965 if (vendor_id != NULL) {
964 966 if ((diskp->vendor_id = strdup(vendor_id)) == NULL) {
965 967 cache_free_disk(diskp);
966 968 return (NULL);
967 969 }
968 970 }
969 971 }
970 972
971 973 /*
972 974 * DVD, CD-ROM, CD-RW, MO, etc. are all reported as CD-ROMS.
973 975 * We try to use uscsi later to determine the real type.
974 976 * The cd_rom flag tells us that the kernel categorized the drive
975 977 * as a CD-ROM. We leave the drv_type as UNKNOWN for now.
976 978 * The combination of the cd_rom flag being set with the drv_type of
977 979 * unknown is what triggers the uscsi probe in drive.c.
978 980 */
979 981 if (disk_is_cdrom(type)) {
980 982 diskp->drv_type = DM_DT_UNKNOWN;
981 983 diskp->cd_rom = 1;
982 984 diskp->removable = 1;
983 985 } else if (libdiskmgt_str_eq(type, DDI_NT_FD)) {
984 986 diskp->drv_type = DM_DT_FLOPPY;
985 987 diskp->removable = 1;
986 988 } else {
987 989 /* not a CD-ROM or Floppy */
988 990 diskp->removable = get_prop(REMOVABLE_PROP, args->node);
989 991
990 992 if (diskp->removable == -1) {
991 993 diskp->removable = 0;
992 994 diskp->drv_type = DM_DT_FIXED;
993 995 }
994 996 }
995 997
996 998 diskp->next = args->disk_listp;
997 999 args->disk_listp = diskp;
998 1000
999 1001 return (diskp);
1000 1002 }
1001 1003
1002 1004 static char *
1003 1005 ctype(di_node_t node, di_minor_t minor)
1004 1006 {
1005 1007 char *type;
1006 1008 char *name;
1007 1009
1008 1010 type = di_minor_nodetype(minor);
1009 1011 name = di_node_name(node);
1010 1012
1011 1013 /* IDE disks use SCSI nexus as the type, so handle this special case */
1012 1014 if ((libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
1013 1015 libdiskmgt_str_eq(type, DDI_PSEUDO)) &&
1014 1016 libdiskmgt_str_eq(name, "ide"))
1015 1017 return (DM_CTYPE_ATA);
1016 1018
1017 1019 if (libdiskmgt_str_eq(type, DDI_NT_FC_ATTACHMENT_POINT) ||
1018 1020 (libdiskmgt_str_eq(type, DDI_NT_NEXUS) &&
1019 1021 libdiskmgt_str_eq(name, "fp")))
1020 1022 return (DM_CTYPE_FIBRE);
1021 1023
1022 1024 if (libdiskmgt_str_eq(type, DDI_NT_NVME_ATTACHMENT_POINT))
1023 1025 return (DM_CTYPE_NVME);
1024 1026
1025 1027 if (libdiskmgt_str_eq(type, DDI_NT_SATA_NEXUS) ||
1026 1028 libdiskmgt_str_eq(type, DDI_NT_SATA_ATTACHMENT_POINT))
1027 1029 return (DM_CTYPE_SATA);
1028 1030
1029 1031 if (libdiskmgt_str_eq(type, DDI_NT_SCSI_NEXUS) ||
1030 1032 libdiskmgt_str_eq(type, DDI_NT_SCSI_ATTACHMENT_POINT))
1031 1033 return (DM_CTYPE_SCSI);
1032 1034
1033 1035 if (libdiskmgt_str_eq(di_minor_name(minor), "scsa2usb"))
1034 1036 return (DM_CTYPE_USB);
1035 1037
1036 1038 if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1037 1039 libdiskmgt_str_eq(name, "xpvd"))
1038 1040 return (DM_CTYPE_XEN);
1039 1041
1040 1042 if (dm_debug) {
1041 1043 (void) fprintf(stderr,
1042 1044 "INFO: unknown controller type=%s name=%s\n", type, name);
1043 1045 }
1044 1046
1045 1047 return (DM_CTYPE_UNKNOWN);
1046 1048 }
1047 1049
1048 1050 static boolean_t
1049 1051 disk_is_cdrom(const char *type)
1050 1052 {
1051 1053 return (strncmp(type, DDI_NT_CD, strlen(DDI_NT_CD)) == 0);
1052 1054 }
1053 1055
1054 1056 static alias_t *
1055 1057 find_alias(disk_t *diskp, char *kernel_name)
1056 1058 {
1057 1059 alias_t *ap;
1058 1060
1059 1061 ap = diskp->aliases;
1060 1062 while (ap != NULL) {
1061 1063 if (libdiskmgt_str_eq(ap->kstat_name, kernel_name)) {
1062 1064 return (ap);
1063 1065 }
1064 1066 ap = ap->next;
1065 1067 }
1066 1068
1067 1069 return (NULL);
1068 1070 }
1069 1071
1070 1072 static bus_t *
1071 1073 find_bus(struct search_args *args, char *name)
1072 1074 {
1073 1075 bus_t *listp;
1074 1076
1075 1077 listp = args->bus_listp;
1076 1078 while (listp != NULL) {
1077 1079 if (libdiskmgt_str_eq(listp->name, name)) {
1078 1080 return (listp);
1079 1081 }
1080 1082 listp = listp->next;
1081 1083 }
1082 1084
1083 1085 return (NULL);
1084 1086 }
1085 1087
1086 1088 static controller_t *
1087 1089 find_controller(struct search_args *args, char *name)
1088 1090 {
1089 1091 controller_t *listp;
1090 1092
1091 1093 listp = args->controller_listp;
1092 1094 while (listp != NULL) {
1093 1095 if (libdiskmgt_str_eq(listp->name, name)) {
1094 1096 return (listp);
1095 1097 }
1096 1098 listp = listp->next;
1097 1099 }
1098 1100
1099 1101 return (NULL);
1100 1102 }
1101 1103
1102 1104 /*
1103 1105 * Check if we have the drive in our list, based upon the device id.
1104 1106 * We got the device id from the dev tree walk. This is encoded
1105 1107 * using devid_str_encode(3DEVID). In order to check the device ids we need
1106 1108 * to use the devid_compare(3DEVID) function, so we need to decode the
1107 1109 * string representation of the device id.
1108 1110 */
1109 1111 static disk_t *
1110 1112 get_disk_by_deviceid(disk_t *listp, char *devidstr)
1111 1113 {
1112 1114 ddi_devid_t devid;
1113 1115
1114 1116 if (devidstr == NULL || devid_str_decode(devidstr, &devid, NULL) != 0) {
1115 1117 return (NULL);
1116 1118 }
1117 1119
1118 1120 while (listp != NULL) {
1119 1121 if (listp->devid != NULL &&
1120 1122 devid_compare(listp->devid, devid) == 0) {
1121 1123 break;
1122 1124 }
1123 1125 listp = listp->next;
1124 1126 }
1125 1127
1126 1128 devid_free(devid);
1127 1129 return (listp);
1128 1130 }
1129 1131
1130 1132 /*
1131 1133 * Get the base disk name with no path prefix and no slice (if there is one).
1132 1134 * The name parameter should be big enough to hold the name.
1133 1135 * This handles diskette names ok (/dev/rdiskette0) since there is no slice,
1134 1136 * and converts the raw diskette name.
1135 1137 * But, we don't know how to strip off the slice from third party drive
1136 1138 * names. That just means that their drive name will include a slice on
1137 1139 * it.
1138 1140 */
1139 1141 static void
1140 1142 get_disk_name_from_path(char *path, char *name, int size)
1141 1143 {
1142 1144 char *basep;
1143 1145 int cnt = 0;
1144 1146
1145 1147 basep = strrchr(path, '/');
1146 1148 if (basep == NULL) {
1147 1149 basep = path;
1148 1150 } else {
1149 1151 basep++;
1150 1152 }
1151 1153
1152 1154 size = size - 1; /* leave room for terminating 0 */
1153 1155
1154 1156 if (is_ctds(basep)) {
1155 1157 while (*basep != 0 && *basep != 's' && cnt < size) {
1156 1158 *name++ = *basep++;
1157 1159 cnt++;
1158 1160 }
1159 1161 *name = 0;
1160 1162 } else {
1161 1163 if (strncmp(basep, FLOPPY_NAME,
1162 1164 sizeof (FLOPPY_NAME) - 1) == 0) {
1163 1165 /*
1164 1166 * a floppy, convert rdiskette name to diskette name,
1165 1167 * by skipping over the 'r' for raw diskette
1166 1168 */
1167 1169 basep++;
1168 1170 }
1169 1171
1170 1172 /* not a ctds name, just copy it */
1171 1173 (void) strlcpy(name, basep, size);
1172 1174 }
1173 1175 }
1174 1176
1175 1177 static char *
1176 1178 get_byte_prop(char *prop_name, di_node_t node)
1177 1179 {
1178 1180 int cnt;
1179 1181 uchar_t *bytes;
1180 1182 int i;
1181 1183 char str[MAXPATHLEN];
1182 1184
1183 1185 cnt = di_prop_lookup_bytes(DDI_DEV_T_ANY, node, prop_name, &bytes);
1184 1186 if (cnt < 1) {
1185 1187 return (NULL);
1186 1188 }
1187 1189
1188 1190 str[0] = 0;
1189 1191 for (i = 0; i < cnt; i++) {
1190 1192 char bstr[8]; /* a byte is only 2 hex chars + null */
1191 1193
1192 1194 (void) snprintf(bstr, sizeof (bstr), "%.2x", bytes[i]);
1193 1195 (void) strlcat(str, bstr, sizeof (str));
1194 1196 }
1195 1197 return (strdup(str));
1196 1198 }
1197 1199
1198 1200 static di_node_t
1199 1201 get_parent_bus(di_node_t node, struct search_args *args)
1200 1202 {
1201 1203 di_node_t pnode;
1202 1204
1203 1205 pnode = di_parent_node(node);
1204 1206 if (pnode == DI_NODE_NIL) {
1205 1207 return (NULL);
1206 1208 }
1207 1209
1208 1210 if (bus_type(pnode, di_minor_next(pnode, NULL), args->ph) != NULL) {
1209 1211 return (pnode);
1210 1212 }
1211 1213
1212 1214 return (get_parent_bus(pnode, args));
1213 1215 }
1214 1216
1215 1217 static int
1216 1218 get_prom_int(char *prop_name, di_node_t node, di_prom_handle_t ph)
1217 1219 {
1218 1220 int *n;
1219 1221
1220 1222 if (di_prom_prop_lookup_ints(ph, node, prop_name, &n) == 1) {
1221 1223 return (*n);
1222 1224 }
1223 1225
1224 1226 return (0);
1225 1227 }
1226 1228
1227 1229 static char *
1228 1230 get_prom_str(char *prop_name, di_node_t node, di_prom_handle_t ph)
1229 1231 {
1230 1232 char *str;
1231 1233
1232 1234 if (di_prom_prop_lookup_strings(ph, node, prop_name, &str) == 1) {
1233 1235 return (str);
1234 1236 }
1235 1237
1236 1238 return (NULL);
1237 1239 }
1238 1240
1239 1241 /*
1240 1242 * Get one of the positive int or boolean properties.
1241 1243 */
1242 1244 static int
1243 1245 get_prop(char *prop_name, di_node_t node)
1244 1246 {
1245 1247 int num;
1246 1248 int *ip;
1247 1249
1248 1250 if ((num = di_prop_lookup_ints(DDI_DEV_T_ANY, node, prop_name, &ip))
1249 1251 >= 0) {
1250 1252 if (num == 0) {
1251 1253 /* boolean */
1252 1254 return (1);
1253 1255 } else if (num == 1) {
1254 1256 /* single int */
1255 1257 return (*ip);
1256 1258 }
1257 1259 }
1258 1260 return (-1);
1259 1261 }
1260 1262
1261 1263 static char *
1262 1264 get_str_prop(char *prop_name, di_node_t node)
1263 1265 {
1264 1266 char *str;
1265 1267
1266 1268 if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, prop_name, &str) == 1) {
1267 1269 return (str);
1268 1270 }
1269 1271
1270 1272 return (NULL);
1271 1273 }
1272 1274
1273 1275 /*
1274 1276 * Check if we have the drive in our list, based upon the device id, if the
1275 1277 * drive has a device id, or the kernel name, if it doesn't have a device id.
1276 1278 */
1277 1279 static int
1278 1280 have_disk(struct search_args *args, char *devidstr, char *kernel_name,
1279 1281 disk_t **diskp)
1280 1282 {
1281 1283 disk_t *listp;
1282 1284
1283 1285 *diskp = NULL;
1284 1286 listp = args->disk_listp;
1285 1287 if (devidstr != NULL) {
1286 1288 if ((*diskp = get_disk_by_deviceid(listp, devidstr)) != NULL) {
1287 1289 return (1);
1288 1290 }
1289 1291
1290 1292 } else {
1291 1293 /* no devid, try matching the kernel names on the drives */
1292 1294 while (listp != NULL) {
1293 1295 if (libdiskmgt_str_eq(kernel_name,
1294 1296 listp->kernel_name)) {
1295 1297 *diskp = listp;
1296 1298 return (1);
1297 1299 }
1298 1300 listp = listp->next;
1299 1301 }
1300 1302 }
1301 1303 return (0);
1302 1304 }
1303 1305
1304 1306 static char *
1305 1307 bus_type(di_node_t node, di_minor_t minor, di_prom_handle_t ph)
1306 1308 {
1307 1309 char *type;
1308 1310 int i;
1309 1311
1310 1312 type = get_prom_str("device_type", node, ph);
1311 1313 if (type == NULL) {
1312 1314 type = di_node_name(node);
1313 1315 }
1314 1316
1315 1317 for (i = 0; bustypes[i]; i++) {
1316 1318 if (libdiskmgt_str_eq(type, bustypes[i])) {
1317 1319 return (type);
1318 1320 }
1319 1321 }
1320 1322
1321 1323 if (minor != NULL && strcmp(di_minor_nodetype(minor),
1322 1324 DDI_NT_USB_ATTACHMENT_POINT) == 0) {
1323 1325 return ("usb");
1324 1326 }
1325 1327
1326 1328 return (NULL);
1327 1329 }
1328 1330
1329 1331 /*
1330 1332 * If the input name is in c[t]ds format then return 1, otherwise return 0.
1331 1333 */
1332 1334 static int
1333 1335 is_ctds(char *name)
1334 1336 {
1335 1337 char *p;
1336 1338
1337 1339 p = name;
1338 1340
1339 1341 if (*p++ != 'c') {
1340 1342 return (0);
1341 1343 }
1342 1344 /* skip controller digits */
1343 1345 while (isdigit(*p)) {
1344 1346 p++;
1345 1347 }
1346 1348
1347 1349 /* handle optional target */
1348 1350 if (*p == 't') {
1349 1351 p++;
1350 1352 /* skip over target */
1351 1353 while (isdigit(*p) || isupper(*p)) {
1352 1354 p++;
1353 1355 }
1354 1356 }
1355 1357
1356 1358 if (*p++ != 'd') {
1357 1359 return (0);
1358 1360 }
1359 1361 while (isdigit(*p)) {
1360 1362 p++;
1361 1363 }
1362 1364
1363 1365 if (*p++ != 's') {
1364 1366 return (0);
1365 1367 }
1366 1368
1367 1369 /* check the slice number */
1368 1370 while (isdigit(*p)) {
1369 1371 p++;
1370 1372 }
1371 1373
1372 1374 if (*p != 0) {
1373 1375 return (0);
1374 1376 }
1375 1377
1376 1378 return (1);
1377 1379 }
1378 1380
1379 1381 static int
1380 1382 is_drive(di_minor_t minor)
1381 1383 {
1382 1384 return (strncmp(di_minor_nodetype(minor), DDI_NT_BLOCK,
1383 1385 strlen(DDI_NT_BLOCK)) == 0);
1384 1386 }
1385 1387
1386 1388 static int
1387 1389 is_zvol(di_node_t node, di_minor_t minor)
1388 1390 {
1389 1391 if ((strncmp(di_node_name(node), ZFS_DRIVER, 3) == 0) &&
1390 1392 minor(di_minor_devt(minor)))
1391 1393 return (1);
1392 1394 return (0);
1393 1395 }
1394 1396
1395 1397 static int
1396 1398 is_ctrl(di_node_t node, di_minor_t minor)
1397 1399 {
1398 1400 char *type;
1399 1401 char *name;
1400 1402 int type_index;
1401 1403
1402 1404 type = di_minor_nodetype(minor);
1403 1405 type_index = 0;
1404 1406
1405 1407 while (ctrltypes[type_index] != NULL) {
1406 1408 if (libdiskmgt_str_eq(type, ctrltypes[type_index])) {
1407 1409 return (1);
1408 1410 }
1409 1411 type_index++;
1410 1412 }
1411 1413
1412 1414 name = di_node_name(node);
1413 1415 if (libdiskmgt_str_eq(type, DDI_PSEUDO) &&
1414 1416 (libdiskmgt_str_eq(name, "ide") ||
1415 1417 libdiskmgt_str_eq(name, "xpvd")))
1416 1418 return (1);
1417 1419
1418 1420 return (0);
1419 1421 }
1420 1422
1421 1423 static int
1422 1424 new_alias(disk_t *diskp, char *kernel_name, char *devlink_path,
1423 1425 struct search_args *args)
1424 1426 {
1425 1427 alias_t *aliasp;
1426 1428 char alias[MAXPATHLEN];
1427 1429 di_node_t pnode;
1428 1430
1429 1431 aliasp = malloc(sizeof (alias_t));
1430 1432 if (aliasp == NULL) {
1431 1433 return (ENOMEM);
1432 1434 }
1433 1435
1434 1436 aliasp->alias = NULL;
1435 1437 aliasp->kstat_name = NULL;
1436 1438 aliasp->wwn = NULL;
1437 1439 aliasp->devpaths = NULL;
1438 1440 aliasp->orig_paths = NULL;
1439 1441
1440 1442 get_disk_name_from_path(devlink_path, alias, sizeof (alias));
1441 1443
1442 1444 aliasp->alias = strdup(alias);
1443 1445 if (aliasp->alias == NULL) {
1444 1446 cache_free_alias(aliasp);
1445 1447 return (ENOMEM);
1446 1448 }
1447 1449
1448 1450 if (kernel_name != NULL) {
1449 1451 aliasp->kstat_name = strdup(kernel_name);
1450 1452 if (aliasp->kstat_name == NULL) {
1451 1453 cache_free_alias(aliasp);
1452 1454 return (ENOMEM);
1453 1455 }
1454 1456 } else {
1455 1457 aliasp->kstat_name = NULL;
1456 1458 }
1457 1459
1458 1460 aliasp->lun = get_prop(DM_LUN, args->node);
1459 1461 aliasp->target = get_prop(DM_TARGET, args->node);
1460 1462 aliasp->wwn = get_byte_prop(WWN_PROP, args->node);
1461 1463
1462 1464 pnode = di_parent_node(args->node);
1463 1465 if (pnode != DI_NODE_NIL) {
1464 1466 char prop_name[MAXPROPLEN];
1465 1467
1466 1468 (void) snprintf(prop_name, sizeof (prop_name),
1467 1469 "target%d-sync-speed", aliasp->target);
1468 1470 diskp->sync_speed = get_prop(prop_name, pnode);
1469 1471 (void) snprintf(prop_name, sizeof (prop_name), "target%d-wide",
1470 1472 aliasp->target);
1471 1473 diskp->wide = get_prop(prop_name, pnode);
1472 1474 }
1473 1475
1474 1476 if (new_devpath(aliasp, devlink_path) != 0) {
1475 1477 cache_free_alias(aliasp);
1476 1478 return (ENOMEM);
1477 1479 }
1478 1480
1479 1481 aliasp->next = diskp->aliases;
1480 1482 diskp->aliases = aliasp;
1481 1483
1482 1484 return (0);
1483 1485 }
1484 1486
1485 1487 /*
1486 1488 * Append the new devpath to the end of the devpath list. This is important
1487 1489 * since we may want to use the order of the devpaths to match up the vtoc
1488 1490 * entries.
1489 1491 */
1490 1492 static int
1491 1493 new_devpath(alias_t *ap, char *devpath)
1492 1494 {
1493 1495 slice_t *newdp;
1494 1496 slice_t *alistp;
1495 1497
1496 1498 /*
1497 1499 * First, search the alias list to be sure that this devpath is
1498 1500 * not already there.
1499 1501 */
1500 1502
1501 1503 for (alistp = ap->devpaths; alistp != NULL; alistp = alistp->next) {
1502 1504 if (libdiskmgt_str_eq(alistp->devpath, devpath)) {
1503 1505 return (0);
1504 1506 }
1505 1507 }
1506 1508
1507 1509 /*
1508 1510 * Otherwise, not found so add this new devpath to the list.
1509 1511 */
1510 1512
1511 1513 newdp = malloc(sizeof (slice_t));
1512 1514 if (newdp == NULL) {
1513 1515 return (ENOMEM);
1514 1516 }
1515 1517
1516 1518 newdp->devpath = strdup(devpath);
1517 1519 if (newdp->devpath == NULL) {
1518 1520 free(newdp);
1519 1521 return (ENOMEM);
1520 1522 }
1521 1523 newdp->slice_num = -1;
1522 1524 newdp->next = NULL;
1523 1525
1524 1526 if (ap->devpaths == NULL) {
1525 1527 ap->devpaths = newdp;
1526 1528 } else {
1527 1529 /* append the devpath to the end of the list */
1528 1530 slice_t *dp;
1529 1531
1530 1532 dp = ap->devpaths;
1531 1533 while (dp->next != NULL) {
1532 1534 dp = dp->next;
1533 1535 }
1534 1536
1535 1537 dp->next = newdp;
1536 1538 }
1537 1539
1538 1540 return (0);
1539 1541 }
1540 1542
1541 1543 static path_t *
1542 1544 new_path(controller_t *cp, disk_t *dp, di_node_t node, di_path_state_t st,
1543 1545 char *wwn)
1544 1546 {
1545 1547 char *devpath;
1546 1548 path_t *pp;
1547 1549 di_minor_t minor;
1548 1550
1549 1551 /* Special handling for fp attachment node. */
1550 1552 if (strcmp(di_node_name(node), "fp") == 0) {
1551 1553 di_node_t pnode;
1552 1554
1553 1555 pnode = di_parent_node(node);
1554 1556 if (pnode != DI_NODE_NIL) {
1555 1557 node = pnode;
1556 1558 }
1557 1559 }
1558 1560
1559 1561 devpath = di_devfs_path(node);
1560 1562
1561 1563 /* check if the path is already there */
1562 1564 pp = NULL;
1563 1565 if (cp->paths != NULL) {
1564 1566 int i;
1565 1567
1566 1568 for (i = 0; cp->paths[i]; i++) {
1567 1569 if (libdiskmgt_str_eq(devpath, cp->paths[i]->name)) {
1568 1570 pp = cp->paths[i];
1569 1571 break;
1570 1572 }
1571 1573 }
1572 1574 }
1573 1575
1574 1576 if (pp != NULL) {
1575 1577 /* the path exists, add this disk to it */
1576 1578
1577 1579 di_devfs_path_free((void *) devpath);
1578 1580 if (!add_disk2path(dp, pp, st, wwn)) {
1579 1581 return (NULL);
1580 1582 }
1581 1583 return (pp);
1582 1584 }
1583 1585
1584 1586 /* create a new path */
1585 1587
1586 1588 pp = calloc(1, sizeof (path_t));
1587 1589 if (pp == NULL) {
1588 1590 di_devfs_path_free((void *) devpath);
1589 1591 return (NULL);
1590 1592 }
1591 1593
1592 1594 pp->name = strdup(devpath);
1593 1595 di_devfs_path_free((void *) devpath);
1594 1596 if (pp->name == NULL) {
1595 1597 cache_free_path(pp);
1596 1598 return (NULL);
1597 1599 }
1598 1600
1599 1601 /* add the disk to the path */
1600 1602 if (!add_disk2path(dp, pp, st, wwn)) {
1601 1603 return (NULL);
1602 1604 }
1603 1605
1604 1606 /* add the path to the controller */
1605 1607 if (add_ptr2array(pp, (void ***)&cp->paths) != 0) {
1606 1608 cache_free_path(pp);
1607 1609 return (NULL);
1608 1610 }
1609 1611
1610 1612 /* add the controller to the path */
1611 1613 pp->controller = cp;
1612 1614
1613 1615 minor = di_minor_next(node, NULL);
1614 1616 if (minor != NULL) {
1615 1617 pp->ctype = ctype(node, minor);
1616 1618 } else {
1617 1619 pp->ctype = DM_CTYPE_UNKNOWN;
1618 1620 }
1619 1621
1620 1622 return (pp);
1621 1623 }
1622 1624
1623 1625 /*
1624 1626 * We pass in the current controller pointer (currp) so we can double check
1625 1627 * that we aren't corrupting the list by removing the element we are on. This
1626 1628 * should never happen, but it doesn't hurt to double check.
1627 1629 */
1628 1630 static void
1629 1631 remove_invalid_controller(char *name, controller_t *currp,
1630 1632 struct search_args *args)
1631 1633 {
1632 1634 controller_t *cp;
1633 1635 bus_t *bp;
1634 1636 controller_t *prevp;
1635 1637
1636 1638 bp = args->bus_listp;
1637 1639 while (bp != NULL) {
1638 1640 int i;
1639 1641
1640 1642 for (i = 0; bp->controllers[i]; i++) {
1641 1643 if (libdiskmgt_str_eq(bp->controllers[i]->name, name)) {
1642 1644 int j;
1643 1645 /*
1644 1646 * remove pointer to invalid controller.
1645 1647 * (it is a path)
1646 1648 */
1647 1649 for (j = i; bp->controllers[j]; j++) {
1648 1650 bp->controllers[j] =
1649 1651 bp->controllers[j + 1];
1650 1652 }
1651 1653 }
1652 1654 }
1653 1655 bp = bp->next;
1654 1656 }
1655 1657
1656 1658 if (args->controller_listp == NULL) {
1657 1659 return;
1658 1660 }
1659 1661
1660 1662 cp = args->controller_listp;
1661 1663 if (libdiskmgt_str_eq(cp->name, name)) {
1662 1664 args->controller_listp = cp->next;
1663 1665 if (dm_debug) {
1664 1666 (void) fprintf(stderr,
1665 1667 "INFO: Removed controller %s from list\n",
1666 1668 cp->name);
1667 1669 }
1668 1670 remove_controller(cp, currp);
1669 1671 return;
1670 1672 }
1671 1673
1672 1674 prevp = cp;
1673 1675 cp = cp->next;
1674 1676 while (cp != NULL) {
1675 1677 if (libdiskmgt_str_eq(cp->name, name)) {
1676 1678 if (dm_debug) {
1677 1679 (void) fprintf(stderr,
1678 1680 "INFO: Removed controller %s from list\n",
1679 1681 cp->name);
1680 1682 }
1681 1683 prevp->next = cp->next;
1682 1684 remove_controller(cp, currp);
1683 1685 return;
1684 1686 }
1685 1687 prevp = cp;
1686 1688 cp = cp->next;
1687 1689 }
1688 1690 }
|
↓ open down ↓ |
1510 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX