Print this page
zpool import is braindead
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libzfs/common/libzfs_import.c
+++ new/usr/src/lib/libzfs/common/libzfs_import.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.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 + * Copyright 2014 RackTop Systems.
25 26 */
26 27
27 28 /*
28 29 * Pool import support functions.
29 30 *
30 31 * To import a pool, we rely on reading the configuration information from the
31 32 * ZFS label of each device. If we successfully read the label, then we
32 33 * organize the configuration information in the following hierarchy:
33 34 *
34 35 * pool guid -> toplevel vdev guid -> label txg
35 36 *
36 37 * Duplicate entries matching this same tuple will be discarded. Once we have
37 38 * examined every device, we pick the best label txg config for each toplevel
38 39 * vdev. We then arrange these toplevel vdevs into a complete pool config, and
39 40 * update any paths that have changed. Finally, we attempt to import the pool
40 41 * using our derived config, and record the results.
41 42 */
42 43
43 44 #include <ctype.h>
44 45 #include <devid.h>
45 46 #include <dirent.h>
46 47 #include <errno.h>
47 48 #include <libintl.h>
48 49 #include <stddef.h>
49 50 #include <stdlib.h>
50 51 #include <string.h>
51 52 #include <sys/stat.h>
52 53 #include <unistd.h>
53 54 #include <fcntl.h>
54 55 #include <sys/vtoc.h>
55 56 #include <sys/dktp/fdisk.h>
56 57 #include <sys/efi_partition.h>
57 58 #include <thread_pool.h>
58 59
59 60 #include <sys/vdev_impl.h>
60 61
61 62 #include "libzfs.h"
62 63 #include "libzfs_impl.h"
63 64
64 65 /*
65 66 * Intermediate structures used to gather configuration information.
66 67 */
67 68 typedef struct config_entry {
68 69 uint64_t ce_txg;
69 70 nvlist_t *ce_config;
70 71 struct config_entry *ce_next;
71 72 } config_entry_t;
72 73
73 74 typedef struct vdev_entry {
74 75 uint64_t ve_guid;
75 76 config_entry_t *ve_configs;
76 77 struct vdev_entry *ve_next;
77 78 } vdev_entry_t;
78 79
79 80 typedef struct pool_entry {
80 81 uint64_t pe_guid;
81 82 vdev_entry_t *pe_vdevs;
82 83 struct pool_entry *pe_next;
83 84 } pool_entry_t;
84 85
85 86 typedef struct name_entry {
86 87 char *ne_name;
87 88 uint64_t ne_guid;
88 89 struct name_entry *ne_next;
89 90 } name_entry_t;
90 91
91 92 typedef struct pool_list {
92 93 pool_entry_t *pools;
93 94 name_entry_t *names;
94 95 } pool_list_t;
95 96
96 97 static char *
97 98 get_devid(const char *path)
98 99 {
99 100 int fd;
100 101 ddi_devid_t devid;
101 102 char *minor, *ret;
102 103
103 104 if ((fd = open(path, O_RDONLY)) < 0)
104 105 return (NULL);
105 106
106 107 minor = NULL;
107 108 ret = NULL;
108 109 if (devid_get(fd, &devid) == 0) {
109 110 if (devid_get_minor_name(fd, &minor) == 0)
110 111 ret = devid_str_encode(devid, minor);
111 112 if (minor != NULL)
112 113 devid_str_free(minor);
113 114 devid_free(devid);
114 115 }
115 116 (void) close(fd);
116 117
117 118 return (ret);
118 119 }
119 120
120 121
121 122 /*
122 123 * Go through and fix up any path and/or devid information for the given vdev
123 124 * configuration.
124 125 */
125 126 static int
126 127 fix_paths(nvlist_t *nv, name_entry_t *names)
127 128 {
128 129 nvlist_t **child;
129 130 uint_t c, children;
130 131 uint64_t guid;
131 132 name_entry_t *ne, *best;
132 133 char *path, *devid;
133 134 int matched;
134 135
135 136 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
136 137 &child, &children) == 0) {
137 138 for (c = 0; c < children; c++)
138 139 if (fix_paths(child[c], names) != 0)
139 140 return (-1);
140 141 return (0);
141 142 }
142 143
143 144 /*
144 145 * This is a leaf (file or disk) vdev. In either case, go through
145 146 * the name list and see if we find a matching guid. If so, replace
146 147 * the path and see if we can calculate a new devid.
147 148 *
148 149 * There may be multiple names associated with a particular guid, in
149 150 * which case we have overlapping slices or multiple paths to the same
150 151 * disk. If this is the case, then we want to pick the path that is
151 152 * the most similar to the original, where "most similar" is the number
152 153 * of matching characters starting from the end of the path. This will
153 154 * preserve slice numbers even if the disks have been reorganized, and
154 155 * will also catch preferred disk names if multiple paths exist.
155 156 */
156 157 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0);
157 158 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0)
158 159 path = NULL;
159 160
160 161 matched = 0;
161 162 best = NULL;
162 163 for (ne = names; ne != NULL; ne = ne->ne_next) {
163 164 if (ne->ne_guid == guid) {
164 165 const char *src, *dst;
165 166 int count;
166 167
167 168 if (path == NULL) {
168 169 best = ne;
169 170 break;
170 171 }
171 172
172 173 src = ne->ne_name + strlen(ne->ne_name) - 1;
173 174 dst = path + strlen(path) - 1;
174 175 for (count = 0; src >= ne->ne_name && dst >= path;
175 176 src--, dst--, count++)
176 177 if (*src != *dst)
177 178 break;
178 179
179 180 /*
180 181 * At this point, 'count' is the number of characters
181 182 * matched from the end.
182 183 */
183 184 if (count > matched || best == NULL) {
184 185 best = ne;
185 186 matched = count;
186 187 }
187 188 }
188 189 }
189 190
190 191 if (best == NULL)
191 192 return (0);
192 193
193 194 if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0)
194 195 return (-1);
195 196
196 197 if ((devid = get_devid(best->ne_name)) == NULL) {
197 198 (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID);
198 199 } else {
199 200 if (nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, devid) != 0)
200 201 return (-1);
201 202 devid_str_free(devid);
202 203 }
203 204
204 205 return (0);
205 206 }
206 207
207 208 /*
208 209 * Add the given configuration to the list of known devices.
209 210 */
210 211 static int
211 212 add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path,
212 213 nvlist_t *config)
213 214 {
214 215 uint64_t pool_guid, vdev_guid, top_guid, txg, state;
215 216 pool_entry_t *pe;
216 217 vdev_entry_t *ve;
217 218 config_entry_t *ce;
218 219 name_entry_t *ne;
219 220
220 221 /*
221 222 * If this is a hot spare not currently in use or level 2 cache
222 223 * device, add it to the list of names to translate, but don't do
223 224 * anything else.
224 225 */
225 226 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
226 227 &state) == 0 &&
227 228 (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) &&
228 229 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) {
229 230 if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
230 231 return (-1);
231 232
232 233 if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
233 234 free(ne);
234 235 return (-1);
235 236 }
236 237 ne->ne_guid = vdev_guid;
237 238 ne->ne_next = pl->names;
238 239 pl->names = ne;
239 240 return (0);
240 241 }
241 242
242 243 /*
243 244 * If we have a valid config but cannot read any of these fields, then
244 245 * it means we have a half-initialized label. In vdev_label_init()
245 246 * we write a label with txg == 0 so that we can identify the device
246 247 * in case the user refers to the same disk later on. If we fail to
247 248 * create the pool, we'll be left with a label in this state
248 249 * which should not be considered part of a valid pool.
249 250 */
250 251 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
251 252 &pool_guid) != 0 ||
252 253 nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
253 254 &vdev_guid) != 0 ||
254 255 nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID,
255 256 &top_guid) != 0 ||
256 257 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
257 258 &txg) != 0 || txg == 0) {
258 259 nvlist_free(config);
259 260 return (0);
260 261 }
261 262
262 263 /*
263 264 * First, see if we know about this pool. If not, then add it to the
264 265 * list of known pools.
265 266 */
266 267 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
267 268 if (pe->pe_guid == pool_guid)
268 269 break;
269 270 }
270 271
271 272 if (pe == NULL) {
272 273 if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) {
273 274 nvlist_free(config);
274 275 return (-1);
275 276 }
276 277 pe->pe_guid = pool_guid;
277 278 pe->pe_next = pl->pools;
278 279 pl->pools = pe;
279 280 }
280 281
281 282 /*
282 283 * Second, see if we know about this toplevel vdev. Add it if its
283 284 * missing.
284 285 */
285 286 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
286 287 if (ve->ve_guid == top_guid)
287 288 break;
288 289 }
289 290
290 291 if (ve == NULL) {
291 292 if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) {
292 293 nvlist_free(config);
293 294 return (-1);
294 295 }
295 296 ve->ve_guid = top_guid;
296 297 ve->ve_next = pe->pe_vdevs;
297 298 pe->pe_vdevs = ve;
298 299 }
299 300
300 301 /*
301 302 * Third, see if we have a config with a matching transaction group. If
302 303 * so, then we do nothing. Otherwise, add it to the list of known
303 304 * configs.
304 305 */
305 306 for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) {
306 307 if (ce->ce_txg == txg)
307 308 break;
308 309 }
309 310
310 311 if (ce == NULL) {
311 312 if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) {
312 313 nvlist_free(config);
313 314 return (-1);
314 315 }
315 316 ce->ce_txg = txg;
316 317 ce->ce_config = config;
317 318 ce->ce_next = ve->ve_configs;
318 319 ve->ve_configs = ce;
319 320 } else {
320 321 nvlist_free(config);
321 322 }
322 323
323 324 /*
324 325 * At this point we've successfully added our config to the list of
325 326 * known configs. The last thing to do is add the vdev guid -> path
326 327 * mappings so that we can fix up the configuration as necessary before
327 328 * doing the import.
328 329 */
329 330 if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL)
330 331 return (-1);
331 332
332 333 if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) {
333 334 free(ne);
334 335 return (-1);
335 336 }
336 337
337 338 ne->ne_guid = vdev_guid;
338 339 ne->ne_next = pl->names;
339 340 pl->names = ne;
340 341
341 342 return (0);
342 343 }
343 344
344 345 /*
345 346 * Returns true if the named pool matches the given GUID.
346 347 */
347 348 static int
348 349 pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid,
349 350 boolean_t *isactive)
350 351 {
351 352 zpool_handle_t *zhp;
352 353 uint64_t theguid;
353 354
354 355 if (zpool_open_silent(hdl, name, &zhp) != 0)
355 356 return (-1);
356 357
357 358 if (zhp == NULL) {
358 359 *isactive = B_FALSE;
359 360 return (0);
360 361 }
361 362
362 363 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID,
363 364 &theguid) == 0);
364 365
365 366 zpool_close(zhp);
366 367
367 368 *isactive = (theguid == guid);
368 369 return (0);
369 370 }
370 371
371 372 static nvlist_t *
372 373 refresh_config(libzfs_handle_t *hdl, nvlist_t *config)
373 374 {
374 375 nvlist_t *nvl;
375 376 zfs_cmd_t zc = { 0 };
376 377 int err;
377 378
378 379 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0)
379 380 return (NULL);
380 381
381 382 if (zcmd_alloc_dst_nvlist(hdl, &zc,
382 383 zc.zc_nvlist_conf_size * 2) != 0) {
383 384 zcmd_free_nvlists(&zc);
384 385 return (NULL);
385 386 }
386 387
387 388 while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT,
388 389 &zc)) != 0 && errno == ENOMEM) {
389 390 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
390 391 zcmd_free_nvlists(&zc);
391 392 return (NULL);
392 393 }
393 394 }
394 395
395 396 if (err) {
396 397 zcmd_free_nvlists(&zc);
397 398 return (NULL);
398 399 }
399 400
400 401 if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) {
401 402 zcmd_free_nvlists(&zc);
402 403 return (NULL);
403 404 }
404 405
405 406 zcmd_free_nvlists(&zc);
406 407 return (nvl);
407 408 }
408 409
409 410 /*
410 411 * Determine if the vdev id is a hole in the namespace.
411 412 */
412 413 boolean_t
413 414 vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id)
414 415 {
415 416 for (int c = 0; c < holes; c++) {
416 417
417 418 /* Top-level is a hole */
418 419 if (hole_array[c] == id)
419 420 return (B_TRUE);
420 421 }
421 422 return (B_FALSE);
422 423 }
423 424
424 425 /*
425 426 * Convert our list of pools into the definitive set of configurations. We
426 427 * start by picking the best config for each toplevel vdev. Once that's done,
↓ open down ↓ |
392 lines elided |
↑ open up ↑ |
427 428 * we assemble the toplevel vdevs into a full config for the pool. We make a
428 429 * pass to fix up any incorrect paths, and then add it to the main list to
429 430 * return to the user.
430 431 */
431 432 static nvlist_t *
432 433 get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
433 434 {
434 435 pool_entry_t *pe;
435 436 vdev_entry_t *ve;
436 437 config_entry_t *ce;
437 - nvlist_t *ret = NULL, *config = NULL, *tmp, *nvtop, *nvroot;
438 + nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot;
438 439 nvlist_t **spares, **l2cache;
439 440 uint_t i, nspares, nl2cache;
440 441 boolean_t config_seen;
441 442 uint64_t best_txg;
442 - char *name, *hostname;
443 + char *name, *hostname = NULL;
443 444 uint64_t guid;
444 445 uint_t children = 0;
445 446 nvlist_t **child = NULL;
446 447 uint_t holes;
447 448 uint64_t *hole_array, max_id;
448 449 uint_t c;
449 450 boolean_t isactive;
450 451 uint64_t hostid;
451 452 nvlist_t *nvl;
452 453 boolean_t found_one = B_FALSE;
453 454 boolean_t valid_top_config = B_FALSE;
454 455
455 456 if (nvlist_alloc(&ret, 0, 0) != 0)
456 457 goto nomem;
457 458
458 459 for (pe = pl->pools; pe != NULL; pe = pe->pe_next) {
459 460 uint64_t id, max_txg = 0;
460 461
461 462 if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0)
462 463 goto nomem;
463 464 config_seen = B_FALSE;
464 465
465 466 /*
466 467 * Iterate over all toplevel vdevs. Grab the pool configuration
467 468 * from the first one we find, and then go through the rest and
468 469 * add them as necessary to the 'vdevs' member of the config.
469 470 */
470 471 for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) {
471 472
472 473 /*
473 474 * Determine the best configuration for this vdev by
474 475 * selecting the config with the latest transaction
475 476 * group.
476 477 */
477 478 best_txg = 0;
478 479 for (ce = ve->ve_configs; ce != NULL;
479 480 ce = ce->ce_next) {
480 481
481 482 if (ce->ce_txg > best_txg) {
482 483 tmp = ce->ce_config;
483 484 best_txg = ce->ce_txg;
484 485 }
485 486 }
486 487
487 488 /*
488 489 * We rely on the fact that the max txg for the
489 490 * pool will contain the most up-to-date information
490 491 * about the valid top-levels in the vdev namespace.
491 492 */
492 493 if (best_txg > max_txg) {
493 494 (void) nvlist_remove(config,
494 495 ZPOOL_CONFIG_VDEV_CHILDREN,
495 496 DATA_TYPE_UINT64);
496 497 (void) nvlist_remove(config,
497 498 ZPOOL_CONFIG_HOLE_ARRAY,
498 499 DATA_TYPE_UINT64_ARRAY);
499 500
500 501 max_txg = best_txg;
501 502 hole_array = NULL;
502 503 holes = 0;
503 504 max_id = 0;
504 505 valid_top_config = B_FALSE;
505 506
506 507 if (nvlist_lookup_uint64(tmp,
507 508 ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) {
508 509 verify(nvlist_add_uint64(config,
509 510 ZPOOL_CONFIG_VDEV_CHILDREN,
510 511 max_id) == 0);
511 512 valid_top_config = B_TRUE;
512 513 }
513 514
514 515 if (nvlist_lookup_uint64_array(tmp,
515 516 ZPOOL_CONFIG_HOLE_ARRAY, &hole_array,
516 517 &holes) == 0) {
517 518 verify(nvlist_add_uint64_array(config,
518 519 ZPOOL_CONFIG_HOLE_ARRAY,
519 520 hole_array, holes) == 0);
520 521 }
521 522 }
522 523
523 524 if (!config_seen) {
524 525 /*
525 526 * Copy the relevant pieces of data to the pool
526 527 * configuration:
527 528 *
528 529 * version
529 530 * pool guid
530 531 * name
531 532 * comment (if available)
532 533 * pool state
533 534 * hostid (if available)
534 535 * hostname (if available)
535 536 */
536 537 uint64_t state, version;
537 538 char *comment = NULL;
538 539
539 540 version = fnvlist_lookup_uint64(tmp,
540 541 ZPOOL_CONFIG_VERSION);
541 542 fnvlist_add_uint64(config,
542 543 ZPOOL_CONFIG_VERSION, version);
543 544 guid = fnvlist_lookup_uint64(tmp,
544 545 ZPOOL_CONFIG_POOL_GUID);
545 546 fnvlist_add_uint64(config,
546 547 ZPOOL_CONFIG_POOL_GUID, guid);
547 548 name = fnvlist_lookup_string(tmp,
548 549 ZPOOL_CONFIG_POOL_NAME);
549 550 fnvlist_add_string(config,
550 551 ZPOOL_CONFIG_POOL_NAME, name);
551 552
552 553 if (nvlist_lookup_string(tmp,
553 554 ZPOOL_CONFIG_COMMENT, &comment) == 0)
554 555 fnvlist_add_string(config,
555 556 ZPOOL_CONFIG_COMMENT, comment);
556 557
557 558 state = fnvlist_lookup_uint64(tmp,
558 559 ZPOOL_CONFIG_POOL_STATE);
559 560 fnvlist_add_uint64(config,
560 561 ZPOOL_CONFIG_POOL_STATE, state);
561 562
562 563 hostid = 0;
563 564 if (nvlist_lookup_uint64(tmp,
564 565 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
565 566 fnvlist_add_uint64(config,
566 567 ZPOOL_CONFIG_HOSTID, hostid);
567 568 hostname = fnvlist_lookup_string(tmp,
568 569 ZPOOL_CONFIG_HOSTNAME);
569 570 fnvlist_add_string(config,
570 571 ZPOOL_CONFIG_HOSTNAME, hostname);
571 572 }
572 573
573 574 config_seen = B_TRUE;
574 575 }
575 576
576 577 /*
577 578 * Add this top-level vdev to the child array.
578 579 */
579 580 verify(nvlist_lookup_nvlist(tmp,
580 581 ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0);
581 582 verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID,
582 583 &id) == 0);
583 584
584 585 if (id >= children) {
585 586 nvlist_t **newchild;
586 587
587 588 newchild = zfs_alloc(hdl, (id + 1) *
588 589 sizeof (nvlist_t *));
589 590 if (newchild == NULL)
590 591 goto nomem;
591 592
592 593 for (c = 0; c < children; c++)
593 594 newchild[c] = child[c];
594 595
595 596 free(child);
596 597 child = newchild;
597 598 children = id + 1;
598 599 }
599 600 if (nvlist_dup(nvtop, &child[id], 0) != 0)
600 601 goto nomem;
601 602
602 603 }
603 604
604 605 /*
605 606 * If we have information about all the top-levels then
606 607 * clean up the nvlist which we've constructed. This
607 608 * means removing any extraneous devices that are
608 609 * beyond the valid range or adding devices to the end
609 610 * of our array which appear to be missing.
610 611 */
611 612 if (valid_top_config) {
612 613 if (max_id < children) {
613 614 for (c = max_id; c < children; c++)
614 615 nvlist_free(child[c]);
615 616 children = max_id;
616 617 } else if (max_id > children) {
617 618 nvlist_t **newchild;
618 619
619 620 newchild = zfs_alloc(hdl, (max_id) *
620 621 sizeof (nvlist_t *));
621 622 if (newchild == NULL)
622 623 goto nomem;
623 624
624 625 for (c = 0; c < children; c++)
625 626 newchild[c] = child[c];
626 627
627 628 free(child);
628 629 child = newchild;
629 630 children = max_id;
630 631 }
631 632 }
632 633
633 634 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
634 635 &guid) == 0);
635 636
636 637 /*
637 638 * The vdev namespace may contain holes as a result of
638 639 * device removal. We must add them back into the vdev
639 640 * tree before we process any missing devices.
640 641 */
641 642 if (holes > 0) {
642 643 ASSERT(valid_top_config);
643 644
644 645 for (c = 0; c < children; c++) {
645 646 nvlist_t *holey;
646 647
647 648 if (child[c] != NULL ||
648 649 !vdev_is_hole(hole_array, holes, c))
649 650 continue;
650 651
651 652 if (nvlist_alloc(&holey, NV_UNIQUE_NAME,
652 653 0) != 0)
653 654 goto nomem;
654 655
655 656 /*
656 657 * Holes in the namespace are treated as
657 658 * "hole" top-level vdevs and have a
658 659 * special flag set on them.
659 660 */
660 661 if (nvlist_add_string(holey,
661 662 ZPOOL_CONFIG_TYPE,
662 663 VDEV_TYPE_HOLE) != 0 ||
663 664 nvlist_add_uint64(holey,
664 665 ZPOOL_CONFIG_ID, c) != 0 ||
665 666 nvlist_add_uint64(holey,
666 667 ZPOOL_CONFIG_GUID, 0ULL) != 0)
667 668 goto nomem;
668 669 child[c] = holey;
669 670 }
670 671 }
671 672
672 673 /*
673 674 * Look for any missing top-level vdevs. If this is the case,
674 675 * create a faked up 'missing' vdev as a placeholder. We cannot
675 676 * simply compress the child array, because the kernel performs
676 677 * certain checks to make sure the vdev IDs match their location
677 678 * in the configuration.
678 679 */
679 680 for (c = 0; c < children; c++) {
680 681 if (child[c] == NULL) {
681 682 nvlist_t *missing;
682 683 if (nvlist_alloc(&missing, NV_UNIQUE_NAME,
683 684 0) != 0)
684 685 goto nomem;
685 686 if (nvlist_add_string(missing,
686 687 ZPOOL_CONFIG_TYPE,
687 688 VDEV_TYPE_MISSING) != 0 ||
688 689 nvlist_add_uint64(missing,
689 690 ZPOOL_CONFIG_ID, c) != 0 ||
690 691 nvlist_add_uint64(missing,
691 692 ZPOOL_CONFIG_GUID, 0ULL) != 0) {
692 693 nvlist_free(missing);
693 694 goto nomem;
694 695 }
695 696 child[c] = missing;
696 697 }
697 698 }
698 699
699 700 /*
700 701 * Put all of this pool's top-level vdevs into a root vdev.
701 702 */
702 703 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
703 704 goto nomem;
704 705 if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
705 706 VDEV_TYPE_ROOT) != 0 ||
706 707 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 ||
707 708 nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 ||
708 709 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
709 710 child, children) != 0) {
710 711 nvlist_free(nvroot);
711 712 goto nomem;
712 713 }
713 714
714 715 for (c = 0; c < children; c++)
715 716 nvlist_free(child[c]);
716 717 free(child);
717 718 children = 0;
718 719 child = NULL;
719 720
720 721 /*
721 722 * Go through and fix up any paths and/or devids based on our
722 723 * known list of vdev GUID -> path mappings.
723 724 */
724 725 if (fix_paths(nvroot, pl->names) != 0) {
725 726 nvlist_free(nvroot);
726 727 goto nomem;
727 728 }
728 729
729 730 /*
730 731 * Add the root vdev to this pool's configuration.
731 732 */
732 733 if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
733 734 nvroot) != 0) {
734 735 nvlist_free(nvroot);
735 736 goto nomem;
736 737 }
737 738 nvlist_free(nvroot);
738 739
739 740 /*
740 741 * zdb uses this path to report on active pools that were
741 742 * imported or created using -R.
742 743 */
743 744 if (active_ok)
744 745 goto add_pool;
745 746
746 747 /*
747 748 * Determine if this pool is currently active, in which case we
748 749 * can't actually import it.
749 750 */
750 751 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
751 752 &name) == 0);
752 753 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
753 754 &guid) == 0);
754 755
755 756 if (pool_active(hdl, name, guid, &isactive) != 0)
756 757 goto error;
757 758
758 759 if (isactive) {
759 760 nvlist_free(config);
760 761 config = NULL;
761 762 continue;
762 763 }
763 764
764 765 if ((nvl = refresh_config(hdl, config)) == NULL) {
765 766 nvlist_free(config);
766 767 config = NULL;
767 768 continue;
768 769 }
769 770
770 771 nvlist_free(config);
771 772 config = nvl;
772 773
773 774 /*
774 775 * Go through and update the paths for spares, now that we have
775 776 * them.
776 777 */
777 778 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
778 779 &nvroot) == 0);
779 780 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
780 781 &spares, &nspares) == 0) {
781 782 for (i = 0; i < nspares; i++) {
782 783 if (fix_paths(spares[i], pl->names) != 0)
783 784 goto nomem;
784 785 }
785 786 }
786 787
787 788 /*
788 789 * Update the paths for l2cache devices.
789 790 */
790 791 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
791 792 &l2cache, &nl2cache) == 0) {
792 793 for (i = 0; i < nl2cache; i++) {
793 794 if (fix_paths(l2cache[i], pl->names) != 0)
794 795 goto nomem;
795 796 }
796 797 }
797 798
798 799 /*
799 800 * Restore the original information read from the actual label.
800 801 */
801 802 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID,
802 803 DATA_TYPE_UINT64);
803 804 (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME,
804 805 DATA_TYPE_STRING);
805 806 if (hostid != 0) {
806 807 verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
807 808 hostid) == 0);
808 809 verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
809 810 hostname) == 0);
810 811 }
811 812
812 813 add_pool:
813 814 /*
814 815 * Add this pool to the list of configs.
815 816 */
816 817 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
817 818 &name) == 0);
818 819 if (nvlist_add_nvlist(ret, name, config) != 0)
819 820 goto nomem;
820 821
821 822 found_one = B_TRUE;
822 823 nvlist_free(config);
823 824 config = NULL;
824 825 }
825 826
826 827 if (!found_one) {
827 828 nvlist_free(ret);
828 829 ret = NULL;
829 830 }
830 831
831 832 return (ret);
832 833
833 834 nomem:
834 835 (void) no_memory(hdl);
835 836 error:
836 837 nvlist_free(config);
837 838 nvlist_free(ret);
838 839 for (c = 0; c < children; c++)
839 840 nvlist_free(child[c]);
840 841 free(child);
841 842
842 843 return (NULL);
843 844 }
844 845
845 846 /*
846 847 * Return the offset of the given label.
847 848 */
848 849 static uint64_t
849 850 label_offset(uint64_t size, int l)
850 851 {
851 852 ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0);
852 853 return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
853 854 0 : size - VDEV_LABELS * sizeof (vdev_label_t)));
854 855 }
855 856
856 857 /*
857 858 * Given a file descriptor, read the label information and return an nvlist
858 859 * describing the configuration, if there is one.
859 860 */
860 861 int
861 862 zpool_read_label(int fd, nvlist_t **config)
862 863 {
863 864 struct stat64 statbuf;
864 865 int l;
865 866 vdev_label_t *label;
866 867 uint64_t state, txg, size;
867 868
868 869 *config = NULL;
869 870
870 871 if (fstat64(fd, &statbuf) == -1)
871 872 return (0);
872 873 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
873 874
874 875 if ((label = malloc(sizeof (vdev_label_t))) == NULL)
875 876 return (-1);
876 877
877 878 for (l = 0; l < VDEV_LABELS; l++) {
878 879 if (pread64(fd, label, sizeof (vdev_label_t),
879 880 label_offset(size, l)) != sizeof (vdev_label_t))
880 881 continue;
881 882
882 883 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
883 884 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0)
884 885 continue;
885 886
886 887 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
887 888 &state) != 0 || state > POOL_STATE_L2CACHE) {
888 889 nvlist_free(*config);
889 890 continue;
890 891 }
891 892
892 893 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
893 894 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
894 895 &txg) != 0 || txg == 0)) {
895 896 nvlist_free(*config);
896 897 continue;
897 898 }
↓ open down ↓ |
445 lines elided |
↑ open up ↑ |
898 899
899 900 free(label);
900 901 return (0);
901 902 }
902 903
903 904 free(label);
904 905 *config = NULL;
905 906 return (0);
906 907 }
907 908
908 -typedef struct rdsk_node {
909 - char *rn_name;
910 - int rn_dfd;
911 - libzfs_handle_t *rn_hdl;
912 - nvlist_t *rn_config;
913 - avl_tree_t *rn_avl;
914 - avl_node_t rn_node;
915 - boolean_t rn_nozpool;
916 -} rdsk_node_t;
909 +typedef struct slice_node {
910 + char *sn_name;
911 + nvlist_t *sn_config;
912 + boolean_t sn_nozpool;
913 + int sn_partno;
914 + struct disk_node *sn_disk;
915 + struct slice_node *sn_next;
916 +} slice_node_t;
917 +
918 +typedef struct disk_node {
919 + char *dn_name;
920 + int dn_dfd;
921 + libzfs_handle_t *dn_hdl;
922 + nvlist_t *dn_config;
923 + struct slice_node *dn_slices;
924 + struct disk_node *dn_next;
925 +} disk_node_t;
926 +
927 +#ifdef sparc
928 +#define WHOLE_DISK "s2"
929 +#else
930 +#define WHOLE_DISK "p0"
931 +#endif
917 932
918 -static int
919 -slice_cache_compare(const void *arg1, const void *arg2)
933 +/*
934 + * This function splits the slice from the device name. Currently it supports
935 + * VTOC slices (s[0-16]) and DOS/FDISK partitions (p[0-4]). If this function
936 + * is updated to support other slice types then the check_slices function will
937 + * also need to be updated.
938 + */
939 +static boolean_t
940 +get_disk_slice(libzfs_handle_t *hdl, char *disk, char **slice, int *partno)
920 941 {
921 - const char *nm1 = ((rdsk_node_t *)arg1)->rn_name;
922 - const char *nm2 = ((rdsk_node_t *)arg2)->rn_name;
923 - char *nm1slice, *nm2slice;
924 - int rv;
942 + char *p;
925 943
926 - /*
927 - * slices zero and two are the most likely to provide results,
928 - * so put those first
929 - */
930 - nm1slice = strstr(nm1, "s0");
931 - nm2slice = strstr(nm2, "s0");
932 - if (nm1slice && !nm2slice) {
933 - return (-1);
934 - }
935 - if (!nm1slice && nm2slice) {
936 - return (1);
937 - }
938 - nm1slice = strstr(nm1, "s2");
939 - nm2slice = strstr(nm2, "s2");
940 - if (nm1slice && !nm2slice) {
941 - return (-1);
942 - }
943 - if (!nm1slice && nm2slice) {
944 - return (1);
945 - }
944 + if ((p = strrchr(disk, 's')) == NULL &&
945 + (p = strrchr(disk, 'p')) == NULL)
946 + return (B_FALSE);
946 947
947 - rv = strcmp(nm1, nm2);
948 - if (rv == 0)
949 - return (0);
950 - return (rv > 0 ? 1 : -1);
948 + if (!isdigit(p[1]))
949 + return (B_FALSE);
950 +
951 + *slice = zfs_strdup(hdl, p);
952 + *partno = atoi(p + 1);
953 +
954 + p = '\0';
955 + return (B_TRUE);
951 956 }
952 957
953 958 static void
954 -check_one_slice(avl_tree_t *r, char *diskname, uint_t partno,
955 - diskaddr_t size, uint_t blksz)
959 +check_one_slice(slice_node_t *slice, diskaddr_t size, uint_t blksz)
956 960 {
957 - rdsk_node_t tmpnode;
958 - rdsk_node_t *node;
959 - char sname[MAXNAMELEN];
960 -
961 - tmpnode.rn_name = &sname[0];
962 - (void) snprintf(tmpnode.rn_name, MAXNAMELEN, "%s%u",
963 - diskname, partno);
964 961 /*
965 962 * protect against division by zero for disk labels that
966 963 * contain a bogus sector size
967 964 */
968 965 if (blksz == 0)
969 966 blksz = DEV_BSIZE;
970 967 /* too small to contain a zpool? */
971 - if ((size < (SPA_MINDEVSIZE / blksz)) &&
972 - (node = avl_find(r, &tmpnode, NULL)))
973 - node->rn_nozpool = B_TRUE;
974 -}
975 -
976 -static void
977 -nozpool_all_slices(avl_tree_t *r, const char *sname)
978 -{
979 - char diskname[MAXNAMELEN];
980 - char *ptr;
981 - int i;
982 -
983 - (void) strncpy(diskname, sname, MAXNAMELEN);
984 - if (((ptr = strrchr(diskname, 's')) == NULL) &&
985 - ((ptr = strrchr(diskname, 'p')) == NULL))
986 - return;
987 - ptr[0] = 's';
988 - ptr[1] = '\0';
989 - for (i = 0; i < NDKMAP; i++)
990 - check_one_slice(r, diskname, i, 0, 1);
991 - ptr[0] = 'p';
992 - for (i = 0; i <= FD_NUMPART; i++)
993 - check_one_slice(r, diskname, i, 0, 1);
968 + if (size < (SPA_MINDEVSIZE / blksz))
969 + slice->sn_nozpool = B_TRUE;
994 970 }
995 971
996 972 static void
997 -check_slices(avl_tree_t *r, int fd, const char *sname)
973 +check_slices(slice_node_t *slices, int fd)
998 974 {
999 975 struct extvtoc vtoc;
1000 976 struct dk_gpt *gpt;
1001 - char diskname[MAXNAMELEN];
1002 - char *ptr;
1003 - int i;
1004 -
1005 - (void) strncpy(diskname, sname, MAXNAMELEN);
1006 - if ((ptr = strrchr(diskname, 's')) == NULL || !isdigit(ptr[1]))
1007 - return;
1008 - ptr[1] = '\0';
977 + slice_node_t *slice;
978 + diskaddr_t size;
1009 979
1010 980 if (read_extvtoc(fd, &vtoc) >= 0) {
1011 - for (i = 0; i < NDKMAP; i++)
1012 - check_one_slice(r, diskname, i,
1013 - vtoc.v_part[i].p_size, vtoc.v_sectorsz);
981 + for (slice = slices; slice; slice = slice->sn_next) {
982 + if (slice->sn_name[0] == 'p')
983 + continue;
984 + size = vtoc.v_part[slice->sn_partno].p_size;
985 + check_one_slice(slice, size, vtoc.v_sectorsz);
986 + }
1014 987 } else if (efi_alloc_and_read(fd, &gpt) >= 0) {
1015 - /*
1016 - * on x86 we'll still have leftover links that point
1017 - * to slices s[9-15], so use NDKMAP instead
1018 - */
1019 - for (i = 0; i < NDKMAP; i++)
1020 - check_one_slice(r, diskname, i,
1021 - gpt->efi_parts[i].p_size, gpt->efi_lbasize);
1022 - /* nodes p[1-4] are never used with EFI labels */
1023 - ptr[0] = 'p';
1024 - for (i = 1; i <= FD_NUMPART; i++)
1025 - check_one_slice(r, diskname, i, 0, 1);
988 + for (slice = slices; slice; slice = slice->sn_next) {
989 + /* nodes p[1-4] are never used with EFI labels */
990 + if (slice->sn_name[0] == 'p') {
991 + if (slice->sn_partno > 0)
992 + slice->sn_nozpool = B_TRUE;
993 + continue;
994 + }
995 + size = gpt->efi_parts[slice->sn_partno].p_size;
996 + check_one_slice(slice, size, gpt->efi_lbasize);
997 + }
1026 998 efi_free(gpt);
1027 999 }
1028 1000 }
1029 1001
1030 1002 static void
1031 1003 zpool_open_func(void *arg)
1032 1004 {
1033 - rdsk_node_t *rn = arg;
1005 + disk_node_t *disk = arg;
1034 1006 struct stat64 statbuf;
1007 + slice_node_t *slice;
1035 1008 nvlist_t *config;
1009 + char *devname;
1036 1010 int fd;
1037 1011
1038 - if (rn->rn_nozpool)
1012 + /*
1013 + * If the disk has no slices we open it directly, otherwise we try
1014 + * to open the whole disk slice.
1015 + */
1016 + if (disk->dn_slices == NULL)
1017 + devname = strdup(disk->dn_name);
1018 + else
1019 + (void) asprintf(&devname, "%s" WHOLE_DISK, disk->dn_name);
1020 +
1021 + if (devname == NULL) {
1022 + (void) no_memory(disk->dn_hdl);
1039 1023 return;
1040 - if ((fd = openat64(rn->rn_dfd, rn->rn_name, O_RDONLY)) < 0) {
1041 - /* symlink to a device that's no longer there */
1042 - if (errno == ENOENT)
1043 - nozpool_all_slices(rn->rn_avl, rn->rn_name);
1024 + }
1025 +
1026 + if ((fd = openat64(disk->dn_dfd, devname, O_RDONLY)) < 0) {
1027 + free(devname);
1044 1028 return;
1045 1029 }
1046 1030 /*
1047 1031 * Ignore failed stats. We only want regular
1048 1032 * files, character devs and block devs.
1049 1033 */
1050 1034 if (fstat64(fd, &statbuf) != 0 ||
1051 1035 (!S_ISREG(statbuf.st_mode) &&
1052 1036 !S_ISCHR(statbuf.st_mode) &&
1053 1037 !S_ISBLK(statbuf.st_mode))) {
1054 1038 (void) close(fd);
1039 + free(devname);
1055 1040 return;
1056 1041 }
1057 1042 /* this file is too small to hold a zpool */
1058 - if (S_ISREG(statbuf.st_mode) &&
1059 - statbuf.st_size < SPA_MINDEVSIZE) {
1043 + if (S_ISREG(statbuf.st_mode) && statbuf.st_size < SPA_MINDEVSIZE) {
1060 1044 (void) close(fd);
1045 + free(devname);
1061 1046 return;
1062 - } else if (!S_ISREG(statbuf.st_mode)) {
1047 + } else if (!S_ISREG(statbuf.st_mode) && disk->dn_slices != NULL) {
1063 1048 /*
1064 1049 * Try to read the disk label first so we don't have to
1065 1050 * open a bunch of minor nodes that can't have a zpool.
1066 1051 */
1067 - check_slices(rn->rn_avl, fd, rn->rn_name);
1052 + check_slices(disk->dn_slices, fd);
1068 1053 }
1069 1054
1070 - if ((zpool_read_label(fd, &config)) != 0) {
1055 + /*
1056 + * If we're working with the device directly (it has no slices)
1057 + * then we can just read the config and we're done.
1058 + */
1059 + if (disk->dn_slices == NULL) {
1060 + if (zpool_read_label(fd, &config) != 0) {
1061 + (void) no_memory(disk->dn_hdl);
1062 + (void) close(fd);
1063 + free(devname);
1064 + return;
1065 + }
1066 + disk->dn_config = config;
1071 1067 (void) close(fd);
1072 - (void) no_memory(rn->rn_hdl);
1068 + free(devname);
1073 1069 return;
1074 1070 }
1071 +
1075 1072 (void) close(fd);
1073 + free(devname);
1076 1074
1075 + /*
1076 + * Go through and read the label off each slice. The check_slices
1077 + * function has already performed some basic checks and set the
1078 + * sn_nozpool flag on any slices which just can't contain a zpool.
1079 + */
1080 + for (slice = disk->dn_slices; slice; slice = slice->sn_next) {
1081 + if (slice->sn_nozpool == B_TRUE)
1082 + continue;
1077 1083
1078 - rn->rn_config = config;
1079 - if (config != NULL) {
1080 - assert(rn->rn_nozpool == B_FALSE);
1084 + (void) asprintf(&devname, "%s%s", disk->dn_name,
1085 + slice->sn_name);
1086 +
1087 + if (devname == NULL) {
1088 + (void) no_memory(disk->dn_hdl);
1089 + free(devname);
1090 + return;
1091 + }
1092 +
1093 + if ((fd = openat64(disk->dn_dfd, devname, O_RDONLY)) < 0) {
1094 + free(devname);
1095 + continue;
1096 + }
1097 +
1098 + if ((zpool_read_label(fd, &config)) != 0) {
1099 + (void) no_memory(disk->dn_hdl);
1100 + (void) close(fd);
1101 + free(devname);
1102 + return;
1103 + }
1104 +
1105 + slice->sn_config = config;
1106 + (void) close(fd);
1107 + free(devname);
1081 1108 }
1082 1109 }
1083 1110
1084 1111 /*
1085 1112 * Given a file descriptor, clear (zero) the label information. This function
1086 1113 * is currently only used in the appliance stack as part of the ZFS sysevent
1087 1114 * module.
1088 1115 */
1089 1116 int
1090 1117 zpool_clear_label(int fd)
1091 1118 {
1092 1119 struct stat64 statbuf;
1093 1120 int l;
1094 1121 vdev_label_t *label;
1095 1122 uint64_t size;
1096 1123
1097 1124 if (fstat64(fd, &statbuf) == -1)
1098 1125 return (0);
1099 1126 size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t);
1100 1127
1101 1128 if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL)
1102 1129 return (-1);
1103 1130
1104 1131 for (l = 0; l < VDEV_LABELS; l++) {
1105 1132 if (pwrite64(fd, label, sizeof (vdev_label_t),
1106 1133 label_offset(size, l)) != sizeof (vdev_label_t))
1107 1134 return (-1);
1108 1135 }
1109 1136
1110 1137 free(label);
1111 1138 return (0);
1112 1139 }
1113 1140
1114 1141 /*
1115 1142 * Given a list of directories to search, find all pools stored on disk. This
1116 1143 * includes partial pools which are not available to import. If no args are
1117 1144 * given (argc is 0), then the default directory (/dev/dsk) is searched.
1118 1145 * poolname or guid (but not both) are provided by the caller when trying
1119 1146 * to import a specific pool.
1120 1147 */
1121 1148 static nvlist_t *
1122 1149 zpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg)
1123 1150 {
1124 1151 int i, dirs = iarg->paths;
1125 1152 DIR *dirp = NULL;
1126 1153 struct dirent64 *dp;
↓ open down ↓ |
36 lines elided |
↑ open up ↑ |
1127 1154 char path[MAXPATHLEN];
1128 1155 char *end, **dir = iarg->path;
1129 1156 size_t pathleft;
1130 1157 nvlist_t *ret = NULL;
1131 1158 static char *default_dir = "/dev/dsk";
1132 1159 pool_list_t pools = { 0 };
1133 1160 pool_entry_t *pe, *penext;
1134 1161 vdev_entry_t *ve, *venext;
1135 1162 config_entry_t *ce, *cenext;
1136 1163 name_entry_t *ne, *nenext;
1137 - avl_tree_t slice_cache;
1138 - rdsk_node_t *slice;
1139 1164 void *cookie;
1140 1165
1141 1166 if (dirs == 0) {
1142 1167 dirs = 1;
1143 1168 dir = &default_dir;
1144 1169 }
1145 1170
1146 1171 /*
1147 1172 * Go through and read the label configuration information from every
1148 1173 * possible device, organizing the information according to pool GUID
1149 1174 * and toplevel GUID.
1150 1175 */
1151 1176 for (i = 0; i < dirs; i++) {
1152 1177 tpool_t *t;
1153 1178 char *rdsk;
1154 1179 int dfd;
1180 + disk_node_t *disks = NULL, *curdisk = NULL;
1181 + slice_node_t *curslice = NULL;
1155 1182
1156 1183 /* use realpath to normalize the path */
1157 1184 if (realpath(dir[i], path) == 0) {
1158 1185 (void) zfs_error_fmt(hdl, EZFS_BADPATH,
1159 1186 dgettext(TEXT_DOMAIN, "cannot open '%s'"), dir[i]);
1160 1187 goto error;
1161 1188 }
1162 1189 end = &path[strlen(path)];
1163 1190 *end++ = '/';
1164 1191 *end = 0;
1165 1192 pathleft = &path[sizeof (path)] - end;
1166 1193
1167 1194 /*
1168 1195 * Using raw devices instead of block devices when we're
1169 1196 * reading the labels skips a bunch of slow operations during
1170 1197 * close(2) processing, so we replace /dev/dsk with /dev/rdsk.
1171 1198 */
1172 1199 if (strcmp(path, "/dev/dsk/") == 0)
1173 1200 rdsk = "/dev/rdsk/";
1174 1201 else
1175 1202 rdsk = path;
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
1176 1203
1177 1204 if ((dfd = open64(rdsk, O_RDONLY)) < 0 ||
1178 1205 (dirp = fdopendir(dfd)) == NULL) {
1179 1206 zfs_error_aux(hdl, strerror(errno));
1180 1207 (void) zfs_error_fmt(hdl, EZFS_BADPATH,
1181 1208 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1182 1209 rdsk);
1183 1210 goto error;
1184 1211 }
1185 1212
1186 - avl_create(&slice_cache, slice_cache_compare,
1187 - sizeof (rdsk_node_t), offsetof(rdsk_node_t, rn_node));
1188 1213 /*
1189 1214 * This is not MT-safe, but we have no MT consumers of libzfs
1190 1215 */
1191 1216 while ((dp = readdir64(dirp)) != NULL) {
1192 - const char *name = dp->d_name;
1193 - if (name[0] == '.' &&
1194 - (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
1217 + boolean_t isslice;
1218 + char *name, *sname;
1219 + int partno;
1220 +
1221 + if (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' ||
1222 + (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
1195 1223 continue;
1196 1224
1197 - slice = zfs_alloc(hdl, sizeof (rdsk_node_t));
1198 - slice->rn_name = zfs_strdup(hdl, name);
1199 - slice->rn_avl = &slice_cache;
1200 - slice->rn_dfd = dfd;
1201 - slice->rn_hdl = hdl;
1202 - slice->rn_nozpool = B_FALSE;
1203 - avl_add(&slice_cache, slice);
1225 + name = zfs_strdup(hdl, dp->d_name);
1226 +
1227 + /*
1228 + * We create a new disk node every time we encounter
1229 + * a disk with no slices or the disk name changes.
1230 + */
1231 + isslice = get_disk_slice(hdl, name, &sname, &partno);
1232 + if (isslice == B_FALSE || curdisk == NULL ||
1233 + strcmp(curdisk->dn_name, name) != 0) {
1234 + disk_node_t *newdisk;
1235 +
1236 + newdisk = zfs_alloc(hdl, sizeof (disk_node_t));
1237 + newdisk->dn_name = name;
1238 + newdisk->dn_dfd = dfd;
1239 + newdisk->dn_hdl = hdl;
1240 +
1241 + if (curdisk != NULL)
1242 + curdisk->dn_next = newdisk;
1243 + else
1244 + disks = newdisk;
1245 +
1246 + curdisk = newdisk;
1247 + curslice = NULL;
1248 + }
1249 +
1250 + assert(curdisk != NULL);
1251 +
1252 + /*
1253 + * Add a new slice node to the current disk node.
1254 + * We do this for all slices including zero slices.
1255 + */
1256 + if (isslice == B_TRUE) {
1257 + slice_node_t *newslice;
1258 +
1259 + newslice = zfs_alloc(hdl,
1260 + sizeof (slice_node_t));
1261 + newslice->sn_name = sname;
1262 + newslice->sn_partno = partno;
1263 + newslice->sn_disk = curdisk;
1264 +
1265 + if (curslice != NULL)
1266 + curslice->sn_next = newslice;
1267 + else
1268 + curdisk->dn_slices = newslice;
1269 +
1270 + curslice = newslice;
1271 + }
1204 1272 }
1205 1273 /*
1206 1274 * create a thread pool to do all of this in parallel;
1207 - * rn_nozpool is not protected, so this is racy in that
1208 - * multiple tasks could decide that the same slice can
1209 - * not hold a zpool, which is benign. Also choose
1210 - * double the number of processors; we hold a lot of
1211 - * locks in the kernel, so going beyond this doesn't
1212 - * buy us much.
1275 + * choose double the number of processors; we hold a lot
1276 + * of locks in the kernel, so going beyond this doesn't
1277 + * buy us much. Each disk (and any slices it might have)
1278 + * is handled inside a single thread.
1213 1279 */
1214 1280 t = tpool_create(1, 2 * sysconf(_SC_NPROCESSORS_ONLN),
1215 1281 0, NULL);
1216 - for (slice = avl_first(&slice_cache); slice;
1217 - (slice = avl_walk(&slice_cache, slice,
1218 - AVL_AFTER)))
1219 - (void) tpool_dispatch(t, zpool_open_func, slice);
1282 + for (curdisk = disks; curdisk; curdisk = curdisk->dn_next)
1283 + (void) tpool_dispatch(t, zpool_open_func, curdisk);
1220 1284 tpool_wait(t);
1221 1285 tpool_destroy(t);
1222 1286
1223 - cookie = NULL;
1224 - while ((slice = avl_destroy_nodes(&slice_cache,
1225 - &cookie)) != NULL) {
1226 - if (slice->rn_config != NULL) {
1227 - nvlist_t *config = slice->rn_config;
1287 + curdisk = disks;
1288 + while (curdisk != NULL) {
1289 + nvlist_t *config;
1290 + disk_node_t *prevdisk;
1291 +
1292 + /*
1293 + * If the device has slices we examine the config on
1294 + * each of those. If not we use the config directly
1295 + * from the device instead.
1296 + */
1297 + curslice = curdisk->dn_slices;
1298 +
1299 + if (curslice != NULL)
1300 + config = curslice->sn_config;
1301 + else
1302 + config = curdisk->dn_config;
1303 +
1304 + do {
1228 1305 boolean_t matched = B_TRUE;
1229 1306
1307 + if (config == NULL)
1308 + goto next;
1309 +
1230 1310 if (iarg->poolname != NULL) {
1231 1311 char *pname;
1232 1312
1233 1313 matched = nvlist_lookup_string(config,
1234 1314 ZPOOL_CONFIG_POOL_NAME,
1235 1315 &pname) == 0 &&
1236 1316 strcmp(iarg->poolname, pname) == 0;
1237 1317 } else if (iarg->guid != 0) {
1238 1318 uint64_t this_guid;
1239 1319
1240 1320 matched = nvlist_lookup_uint64(config,
1241 1321 ZPOOL_CONFIG_POOL_GUID,
1242 1322 &this_guid) == 0 &&
1243 1323 iarg->guid == this_guid;
1244 1324 }
1325 +
1245 1326 if (!matched) {
1246 1327 nvlist_free(config);
1247 - config = NULL;
1248 - continue;
1328 + goto next;
1249 1329 }
1330 +
1250 1331 /* use the non-raw path for the config */
1251 - (void) strlcpy(end, slice->rn_name, pathleft);
1332 + if (curslice != NULL)
1333 + (void) snprintf(end, pathleft, "%s%s",
1334 + curdisk->dn_name,
1335 + curslice->sn_name);
1336 + else
1337 + (void) strlcpy(end, curdisk->dn_name,
1338 + pathleft);
1252 1339 if (add_config(hdl, &pools, path, config) != 0)
1253 1340 goto error;
1254 - }
1255 - free(slice->rn_name);
1256 - free(slice);
1341 +
1342 +next:
1343 + /*
1344 + * If we're looking at slices free this one
1345 + * and go move onto the next.
1346 + */
1347 + if (curslice != NULL) {
1348 + slice_node_t *prevslice;
1349 +
1350 + prevslice = curslice;
1351 + curslice = curslice->sn_next;
1352 +
1353 + free(prevslice->sn_name);
1354 + free(prevslice);
1355 +
1356 + if (curslice != NULL) {
1357 + config = curslice->sn_config;
1358 + }
1359 + }
1360 + } while (curslice != NULL);
1361 +
1362 + /*
1363 + * Free this disk and move onto the next one.
1364 + */
1365 + prevdisk = curdisk;
1366 + curdisk = curdisk->dn_next;
1367 +
1368 + free(prevdisk->dn_name);
1369 + free(prevdisk);
1257 1370 }
1258 - avl_destroy(&slice_cache);
1259 1371
1260 1372 (void) closedir(dirp);
1261 1373 dirp = NULL;
1262 1374 }
1263 1375
1264 1376 ret = get_configs(hdl, &pools, iarg->can_be_active);
1265 1377
1266 1378 error:
1267 1379 for (pe = pools.pools; pe != NULL; pe = penext) {
1268 1380 penext = pe->pe_next;
1269 1381 for (ve = pe->pe_vdevs; ve != NULL; ve = venext) {
1270 1382 venext = ve->ve_next;
1271 1383 for (ce = ve->ve_configs; ce != NULL; ce = cenext) {
1272 1384 cenext = ce->ce_next;
1273 1385 if (ce->ce_config)
1274 1386 nvlist_free(ce->ce_config);
1275 1387 free(ce);
1276 1388 }
1277 1389 free(ve);
1278 1390 }
1279 1391 free(pe);
1280 1392 }
1281 1393
1282 1394 for (ne = pools.names; ne != NULL; ne = nenext) {
1283 1395 nenext = ne->ne_next;
1284 1396 if (ne->ne_name)
1285 1397 free(ne->ne_name);
1286 1398 free(ne);
1287 1399 }
1288 1400
1289 1401 if (dirp)
1290 1402 (void) closedir(dirp);
1291 1403
1292 1404 return (ret);
1293 1405 }
1294 1406
1295 1407 nvlist_t *
1296 1408 zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv)
1297 1409 {
1298 1410 importargs_t iarg = { 0 };
1299 1411
1300 1412 iarg.paths = argc;
1301 1413 iarg.path = argv;
1302 1414
1303 1415 return (zpool_find_import_impl(hdl, &iarg));
1304 1416 }
1305 1417
1306 1418 /*
1307 1419 * Given a cache file, return the contents as a list of importable pools.
1308 1420 * poolname or guid (but not both) are provided by the caller when trying
1309 1421 * to import a specific pool.
1310 1422 */
1311 1423 nvlist_t *
1312 1424 zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile,
1313 1425 char *poolname, uint64_t guid)
1314 1426 {
1315 1427 char *buf;
1316 1428 int fd;
1317 1429 struct stat64 statbuf;
1318 1430 nvlist_t *raw, *src, *dst;
1319 1431 nvlist_t *pools;
1320 1432 nvpair_t *elem;
1321 1433 char *name;
1322 1434 uint64_t this_guid;
1323 1435 boolean_t active;
1324 1436
1325 1437 verify(poolname == NULL || guid == 0);
1326 1438
1327 1439 if ((fd = open(cachefile, O_RDONLY)) < 0) {
1328 1440 zfs_error_aux(hdl, "%s", strerror(errno));
1329 1441 (void) zfs_error(hdl, EZFS_BADCACHE,
1330 1442 dgettext(TEXT_DOMAIN, "failed to open cache file"));
1331 1443 return (NULL);
1332 1444 }
1333 1445
1334 1446 if (fstat64(fd, &statbuf) != 0) {
1335 1447 zfs_error_aux(hdl, "%s", strerror(errno));
1336 1448 (void) close(fd);
1337 1449 (void) zfs_error(hdl, EZFS_BADCACHE,
1338 1450 dgettext(TEXT_DOMAIN, "failed to get size of cache file"));
1339 1451 return (NULL);
1340 1452 }
1341 1453
1342 1454 if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) {
1343 1455 (void) close(fd);
1344 1456 return (NULL);
1345 1457 }
1346 1458
1347 1459 if (read(fd, buf, statbuf.st_size) != statbuf.st_size) {
1348 1460 (void) close(fd);
1349 1461 free(buf);
1350 1462 (void) zfs_error(hdl, EZFS_BADCACHE,
1351 1463 dgettext(TEXT_DOMAIN,
1352 1464 "failed to read cache file contents"));
1353 1465 return (NULL);
1354 1466 }
1355 1467
1356 1468 (void) close(fd);
1357 1469
1358 1470 if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) {
1359 1471 free(buf);
1360 1472 (void) zfs_error(hdl, EZFS_BADCACHE,
1361 1473 dgettext(TEXT_DOMAIN,
1362 1474 "invalid or corrupt cache file contents"));
1363 1475 return (NULL);
1364 1476 }
1365 1477
1366 1478 free(buf);
1367 1479
1368 1480 /*
1369 1481 * Go through and get the current state of the pools and refresh their
1370 1482 * state.
1371 1483 */
1372 1484 if (nvlist_alloc(&pools, 0, 0) != 0) {
1373 1485 (void) no_memory(hdl);
1374 1486 nvlist_free(raw);
1375 1487 return (NULL);
1376 1488 }
1377 1489
1378 1490 elem = NULL;
1379 1491 while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) {
1380 1492 verify(nvpair_value_nvlist(elem, &src) == 0);
1381 1493
1382 1494 verify(nvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME,
1383 1495 &name) == 0);
1384 1496 if (poolname != NULL && strcmp(poolname, name) != 0)
1385 1497 continue;
1386 1498
1387 1499 verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID,
1388 1500 &this_guid) == 0);
1389 1501 if (guid != 0) {
1390 1502 verify(nvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID,
1391 1503 &this_guid) == 0);
1392 1504 if (guid != this_guid)
1393 1505 continue;
1394 1506 }
1395 1507
1396 1508 if (pool_active(hdl, name, this_guid, &active) != 0) {
1397 1509 nvlist_free(raw);
1398 1510 nvlist_free(pools);
1399 1511 return (NULL);
1400 1512 }
1401 1513
1402 1514 if (active)
1403 1515 continue;
1404 1516
1405 1517 if ((dst = refresh_config(hdl, src)) == NULL) {
1406 1518 nvlist_free(raw);
1407 1519 nvlist_free(pools);
1408 1520 return (NULL);
1409 1521 }
1410 1522
1411 1523 if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) {
1412 1524 (void) no_memory(hdl);
1413 1525 nvlist_free(dst);
1414 1526 nvlist_free(raw);
1415 1527 nvlist_free(pools);
1416 1528 return (NULL);
1417 1529 }
1418 1530 nvlist_free(dst);
1419 1531 }
1420 1532
1421 1533 nvlist_free(raw);
1422 1534 return (pools);
1423 1535 }
1424 1536
1425 1537 static int
1426 1538 name_or_guid_exists(zpool_handle_t *zhp, void *data)
1427 1539 {
1428 1540 importargs_t *import = data;
1429 1541 int found = 0;
1430 1542
1431 1543 if (import->poolname != NULL) {
1432 1544 char *pool_name;
1433 1545
1434 1546 verify(nvlist_lookup_string(zhp->zpool_config,
1435 1547 ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0);
1436 1548 if (strcmp(pool_name, import->poolname) == 0)
1437 1549 found = 1;
1438 1550 } else {
1439 1551 uint64_t pool_guid;
1440 1552
1441 1553 verify(nvlist_lookup_uint64(zhp->zpool_config,
1442 1554 ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0);
1443 1555 if (pool_guid == import->guid)
1444 1556 found = 1;
1445 1557 }
1446 1558
1447 1559 zpool_close(zhp);
1448 1560 return (found);
1449 1561 }
1450 1562
1451 1563 nvlist_t *
1452 1564 zpool_search_import(libzfs_handle_t *hdl, importargs_t *import)
1453 1565 {
1454 1566 verify(import->poolname == NULL || import->guid == 0);
1455 1567
1456 1568 if (import->unique)
1457 1569 import->exists = zpool_iter(hdl, name_or_guid_exists, import);
1458 1570
1459 1571 if (import->cachefile != NULL)
1460 1572 return (zpool_find_import_cached(hdl, import->cachefile,
1461 1573 import->poolname, import->guid));
1462 1574
1463 1575 return (zpool_find_import_impl(hdl, import));
1464 1576 }
1465 1577
1466 1578 boolean_t
1467 1579 find_guid(nvlist_t *nv, uint64_t guid)
1468 1580 {
1469 1581 uint64_t tmp;
1470 1582 nvlist_t **child;
1471 1583 uint_t c, children;
1472 1584
1473 1585 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0);
1474 1586 if (tmp == guid)
1475 1587 return (B_TRUE);
1476 1588
1477 1589 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1478 1590 &child, &children) == 0) {
1479 1591 for (c = 0; c < children; c++)
1480 1592 if (find_guid(child[c], guid))
1481 1593 return (B_TRUE);
1482 1594 }
1483 1595
1484 1596 return (B_FALSE);
1485 1597 }
1486 1598
1487 1599 typedef struct aux_cbdata {
1488 1600 const char *cb_type;
1489 1601 uint64_t cb_guid;
1490 1602 zpool_handle_t *cb_zhp;
1491 1603 } aux_cbdata_t;
1492 1604
1493 1605 static int
1494 1606 find_aux(zpool_handle_t *zhp, void *data)
1495 1607 {
1496 1608 aux_cbdata_t *cbp = data;
1497 1609 nvlist_t **list;
1498 1610 uint_t i, count;
1499 1611 uint64_t guid;
1500 1612 nvlist_t *nvroot;
1501 1613
1502 1614 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1503 1615 &nvroot) == 0);
1504 1616
1505 1617 if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type,
1506 1618 &list, &count) == 0) {
1507 1619 for (i = 0; i < count; i++) {
1508 1620 verify(nvlist_lookup_uint64(list[i],
1509 1621 ZPOOL_CONFIG_GUID, &guid) == 0);
1510 1622 if (guid == cbp->cb_guid) {
1511 1623 cbp->cb_zhp = zhp;
1512 1624 return (1);
1513 1625 }
1514 1626 }
1515 1627 }
1516 1628
1517 1629 zpool_close(zhp);
1518 1630 return (0);
1519 1631 }
1520 1632
1521 1633 /*
1522 1634 * Determines if the pool is in use. If so, it returns true and the state of
1523 1635 * the pool as well as the name of the pool. Both strings are allocated and
1524 1636 * must be freed by the caller.
1525 1637 */
1526 1638 int
1527 1639 zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr,
1528 1640 boolean_t *inuse)
1529 1641 {
1530 1642 nvlist_t *config;
1531 1643 char *name;
1532 1644 boolean_t ret;
1533 1645 uint64_t guid, vdev_guid;
1534 1646 zpool_handle_t *zhp;
1535 1647 nvlist_t *pool_config;
1536 1648 uint64_t stateval, isspare;
1537 1649 aux_cbdata_t cb = { 0 };
1538 1650 boolean_t isactive;
1539 1651
1540 1652 *inuse = B_FALSE;
1541 1653
1542 1654 if (zpool_read_label(fd, &config) != 0) {
1543 1655 (void) no_memory(hdl);
1544 1656 return (-1);
1545 1657 }
1546 1658
1547 1659 if (config == NULL)
1548 1660 return (0);
1549 1661
1550 1662 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
1551 1663 &stateval) == 0);
1552 1664 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID,
1553 1665 &vdev_guid) == 0);
1554 1666
1555 1667 if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) {
1556 1668 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1557 1669 &name) == 0);
1558 1670 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1559 1671 &guid) == 0);
1560 1672 }
1561 1673
1562 1674 switch (stateval) {
1563 1675 case POOL_STATE_EXPORTED:
1564 1676 /*
1565 1677 * A pool with an exported state may in fact be imported
1566 1678 * read-only, so check the in-core state to see if it's
1567 1679 * active and imported read-only. If it is, set
1568 1680 * its state to active.
1569 1681 */
1570 1682 if (pool_active(hdl, name, guid, &isactive) == 0 && isactive &&
1571 1683 (zhp = zpool_open_canfail(hdl, name)) != NULL) {
1572 1684 if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL))
1573 1685 stateval = POOL_STATE_ACTIVE;
1574 1686
1575 1687 /*
1576 1688 * All we needed the zpool handle for is the
1577 1689 * readonly prop check.
1578 1690 */
1579 1691 zpool_close(zhp);
1580 1692 }
1581 1693
1582 1694 ret = B_TRUE;
1583 1695 break;
1584 1696
1585 1697 case POOL_STATE_ACTIVE:
1586 1698 /*
1587 1699 * For an active pool, we have to determine if it's really part
1588 1700 * of a currently active pool (in which case the pool will exist
1589 1701 * and the guid will be the same), or whether it's part of an
1590 1702 * active pool that was disconnected without being explicitly
1591 1703 * exported.
1592 1704 */
1593 1705 if (pool_active(hdl, name, guid, &isactive) != 0) {
1594 1706 nvlist_free(config);
1595 1707 return (-1);
1596 1708 }
1597 1709
1598 1710 if (isactive) {
1599 1711 /*
1600 1712 * Because the device may have been removed while
1601 1713 * offlined, we only report it as active if the vdev is
1602 1714 * still present in the config. Otherwise, pretend like
1603 1715 * it's not in use.
1604 1716 */
1605 1717 if ((zhp = zpool_open_canfail(hdl, name)) != NULL &&
1606 1718 (pool_config = zpool_get_config(zhp, NULL))
1607 1719 != NULL) {
1608 1720 nvlist_t *nvroot;
1609 1721
1610 1722 verify(nvlist_lookup_nvlist(pool_config,
1611 1723 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1612 1724 ret = find_guid(nvroot, vdev_guid);
1613 1725 } else {
1614 1726 ret = B_FALSE;
1615 1727 }
1616 1728
1617 1729 /*
1618 1730 * If this is an active spare within another pool, we
1619 1731 * treat it like an unused hot spare. This allows the
1620 1732 * user to create a pool with a hot spare that currently
1621 1733 * in use within another pool. Since we return B_TRUE,
1622 1734 * libdiskmgt will continue to prevent generic consumers
1623 1735 * from using the device.
1624 1736 */
1625 1737 if (ret && nvlist_lookup_uint64(config,
1626 1738 ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare)
1627 1739 stateval = POOL_STATE_SPARE;
1628 1740
1629 1741 if (zhp != NULL)
1630 1742 zpool_close(zhp);
1631 1743 } else {
1632 1744 stateval = POOL_STATE_POTENTIALLY_ACTIVE;
1633 1745 ret = B_TRUE;
1634 1746 }
1635 1747 break;
1636 1748
1637 1749 case POOL_STATE_SPARE:
1638 1750 /*
1639 1751 * For a hot spare, it can be either definitively in use, or
1640 1752 * potentially active. To determine if it's in use, we iterate
1641 1753 * over all pools in the system and search for one with a spare
1642 1754 * with a matching guid.
1643 1755 *
1644 1756 * Due to the shared nature of spares, we don't actually report
1645 1757 * the potentially active case as in use. This means the user
1646 1758 * can freely create pools on the hot spares of exported pools,
1647 1759 * but to do otherwise makes the resulting code complicated, and
1648 1760 * we end up having to deal with this case anyway.
1649 1761 */
1650 1762 cb.cb_zhp = NULL;
1651 1763 cb.cb_guid = vdev_guid;
1652 1764 cb.cb_type = ZPOOL_CONFIG_SPARES;
1653 1765 if (zpool_iter(hdl, find_aux, &cb) == 1) {
1654 1766 name = (char *)zpool_get_name(cb.cb_zhp);
1655 1767 ret = TRUE;
1656 1768 } else {
1657 1769 ret = FALSE;
1658 1770 }
1659 1771 break;
1660 1772
1661 1773 case POOL_STATE_L2CACHE:
1662 1774
1663 1775 /*
1664 1776 * Check if any pool is currently using this l2cache device.
1665 1777 */
1666 1778 cb.cb_zhp = NULL;
1667 1779 cb.cb_guid = vdev_guid;
1668 1780 cb.cb_type = ZPOOL_CONFIG_L2CACHE;
1669 1781 if (zpool_iter(hdl, find_aux, &cb) == 1) {
1670 1782 name = (char *)zpool_get_name(cb.cb_zhp);
1671 1783 ret = TRUE;
1672 1784 } else {
1673 1785 ret = FALSE;
1674 1786 }
1675 1787 break;
1676 1788
1677 1789 default:
1678 1790 ret = B_FALSE;
1679 1791 }
1680 1792
1681 1793
1682 1794 if (ret) {
1683 1795 if ((*namestr = zfs_strdup(hdl, name)) == NULL) {
1684 1796 if (cb.cb_zhp)
1685 1797 zpool_close(cb.cb_zhp);
1686 1798 nvlist_free(config);
1687 1799 return (-1);
1688 1800 }
1689 1801 *state = (pool_state_t)stateval;
1690 1802 }
1691 1803
1692 1804 if (cb.cb_zhp)
1693 1805 zpool_close(cb.cb_zhp);
1694 1806
1695 1807 nvlist_free(config);
1696 1808 *inuse = ret;
1697 1809 return (0);
1698 1810 }
↓ open down ↓ |
430 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX