]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
0fdd8d64 | 22 | * Copyright 2015 Nexenta Systems, Inc. All rights reserved. |
572e2857 | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
3bc7e0fb | 24 | * Copyright (c) 2012 by Delphix. All rights reserved. |
ee42b3d6 | 25 | * Copyright 2015 RackTop Systems. |
39fc0cb5 | 26 | * Copyright (c) 2016, Intel Corporation. |
34dc7c2f BB |
27 | */ |
28 | ||
34dc7c2f BB |
29 | /* |
30 | * Pool import support functions. | |
31 | * | |
32 | * To import a pool, we rely on reading the configuration information from the | |
33 | * ZFS label of each device. If we successfully read the label, then we | |
34 | * organize the configuration information in the following hierarchy: | |
35 | * | |
36 | * pool guid -> toplevel vdev guid -> label txg | |
37 | * | |
38 | * Duplicate entries matching this same tuple will be discarded. Once we have | |
39 | * examined every device, we pick the best label txg config for each toplevel | |
40 | * vdev. We then arrange these toplevel vdevs into a complete pool config, and | |
41 | * update any paths that have changed. Finally, we attempt to import the pool | |
42 | * using our derived config, and record the results. | |
43 | */ | |
44 | ||
428870ff | 45 | #include <ctype.h> |
34dc7c2f BB |
46 | #include <devid.h> |
47 | #include <dirent.h> | |
48 | #include <errno.h> | |
49 | #include <libintl.h> | |
39fc0cb5 DB |
50 | #ifdef HAVE_LIBUDEV |
51 | #include <libudev.h> | |
52 | #include <sched.h> | |
53 | #endif | |
428870ff | 54 | #include <stddef.h> |
34dc7c2f BB |
55 | #include <stdlib.h> |
56 | #include <string.h> | |
57 | #include <sys/stat.h> | |
58 | #include <unistd.h> | |
59 | #include <fcntl.h> | |
428870ff BB |
60 | #include <sys/vtoc.h> |
61 | #include <sys/dktp/fdisk.h> | |
62 | #include <sys/efi_partition.h> | |
34dc7c2f | 63 | #include <sys/vdev_impl.h> |
d603ed6c | 64 | #include <blkid/blkid.h> |
34dc7c2f BB |
65 | #include "libzfs.h" |
66 | #include "libzfs_impl.h" | |
67 | ||
68 | /* | |
69 | * Intermediate structures used to gather configuration information. | |
70 | */ | |
71 | typedef struct config_entry { | |
72 | uint64_t ce_txg; | |
73 | nvlist_t *ce_config; | |
74 | struct config_entry *ce_next; | |
75 | } config_entry_t; | |
76 | ||
77 | typedef struct vdev_entry { | |
78 | uint64_t ve_guid; | |
79 | config_entry_t *ve_configs; | |
80 | struct vdev_entry *ve_next; | |
81 | } vdev_entry_t; | |
82 | ||
83 | typedef struct pool_entry { | |
84 | uint64_t pe_guid; | |
85 | vdev_entry_t *pe_vdevs; | |
86 | struct pool_entry *pe_next; | |
87 | } pool_entry_t; | |
88 | ||
89 | typedef struct name_entry { | |
90 | char *ne_name; | |
91 | uint64_t ne_guid; | |
44867b6d | 92 | uint64_t ne_order; |
7d90f569 | 93 | uint64_t ne_num_labels; |
34dc7c2f BB |
94 | struct name_entry *ne_next; |
95 | } name_entry_t; | |
96 | ||
97 | typedef struct pool_list { | |
98 | pool_entry_t *pools; | |
99 | name_entry_t *names; | |
100 | } pool_list_t; | |
101 | ||
325414e4 BB |
102 | #define DEV_BYID_PATH "/dev/disk/by-id/" |
103 | ||
39fc0cb5 DB |
104 | /* |
105 | * Linux persistent device strings for vdev labels | |
106 | * | |
107 | * based on libudev for consistency with libudev disk add/remove events | |
108 | */ | |
109 | #ifdef HAVE_LIBUDEV | |
110 | ||
39fc0cb5 DB |
111 | typedef struct vdev_dev_strs { |
112 | char vds_devid[128]; | |
113 | char vds_devphys[128]; | |
114 | } vdev_dev_strs_t; | |
115 | ||
116 | /* | |
117 | * Obtain the persistent device id string (describes what) | |
118 | * | |
119 | * used by ZED auto-{online,expand,replace} | |
120 | */ | |
121 | static int | |
122 | udev_device_get_devid(struct udev_device *dev, char *bufptr, size_t buflen) | |
123 | { | |
124 | struct udev_list_entry *entry; | |
125 | const char *bus; | |
126 | char devbyid[MAXPATHLEN]; | |
127 | ||
128 | /* The bus based by-id path is preferred */ | |
129 | bus = udev_device_get_property_value(dev, "ID_BUS"); | |
130 | ||
131 | if (bus == NULL) { | |
132 | const char *dm_uuid; | |
133 | ||
134 | /* | |
135 | * For multipath nodes use the persistent uuid based identifier | |
136 | * | |
137 | * Example: /dev/disk/by-id/dm-uuid-mpath-35000c5006304de3f | |
138 | */ | |
139 | dm_uuid = udev_device_get_property_value(dev, "DM_UUID"); | |
140 | if (dm_uuid != NULL) { | |
141 | (void) snprintf(bufptr, buflen, "dm-uuid-%s", dm_uuid); | |
142 | return (0); | |
143 | } | |
144 | return (ENODATA); | |
145 | } | |
146 | ||
147 | /* | |
148 | * locate the bus specific by-id link | |
149 | */ | |
150 | (void) snprintf(devbyid, sizeof (devbyid), "%s%s-", DEV_BYID_PATH, bus); | |
151 | entry = udev_device_get_devlinks_list_entry(dev); | |
152 | while (entry != NULL) { | |
153 | const char *name; | |
154 | ||
155 | name = udev_list_entry_get_name(entry); | |
156 | if (strncmp(name, devbyid, strlen(devbyid)) == 0) { | |
157 | name += strlen(DEV_BYID_PATH); | |
158 | (void) strlcpy(bufptr, name, buflen); | |
159 | return (0); | |
160 | } | |
161 | entry = udev_list_entry_get_next(entry); | |
162 | } | |
163 | ||
164 | return (ENODATA); | |
165 | } | |
166 | ||
167 | /* | |
168 | * Obtain the persistent physical location string (describes where) | |
169 | * | |
170 | * used by ZED auto-{online,expand,replace} | |
171 | */ | |
172 | static int | |
173 | udev_device_get_physical(struct udev_device *dev, char *bufptr, size_t buflen) | |
174 | { | |
175 | const char *physpath, *value; | |
176 | ||
177 | /* | |
178 | * Skip indirect multipath device nodes | |
179 | */ | |
180 | value = udev_device_get_property_value(dev, "DM_MULTIPATH_DEVICE_PATH"); | |
181 | if (value != NULL && strcmp(value, "1") == 0) | |
182 | return (ENODATA); /* skip physical for multipath nodes */ | |
183 | ||
184 | physpath = udev_device_get_property_value(dev, "ID_PATH"); | |
185 | if (physpath != NULL && physpath[0] != '\0') { | |
186 | (void) strlcpy(bufptr, physpath, buflen); | |
187 | return (0); | |
188 | } | |
189 | ||
190 | return (ENODATA); | |
191 | } | |
192 | ||
193 | /* | |
194 | * A disk is considered a multipath whole disk when: | |
195 | * DEVNAME key value has "dm-" | |
196 | * DM_NAME key value has "mpath" prefix | |
197 | * DM_UUID key exists | |
198 | * ID_PART_TABLE_TYPE key does not exist or is not gpt | |
199 | */ | |
200 | static boolean_t | |
201 | udev_mpath_whole_disk(struct udev_device *dev) | |
202 | { | |
203 | const char *devname, *mapname, *type, *uuid; | |
204 | ||
205 | devname = udev_device_get_property_value(dev, "DEVNAME"); | |
206 | mapname = udev_device_get_property_value(dev, "DM_NAME"); | |
207 | type = udev_device_get_property_value(dev, "ID_PART_TABLE_TYPE"); | |
208 | uuid = udev_device_get_property_value(dev, "DM_UUID"); | |
209 | ||
210 | if ((devname != NULL && strncmp(devname, "/dev/dm-", 8) == 0) && | |
211 | (mapname != NULL && strncmp(mapname, "mpath", 5) == 0) && | |
212 | ((type == NULL) || (strcmp(type, "gpt") != 0)) && | |
213 | (uuid != NULL)) { | |
214 | return (B_TRUE); | |
215 | } | |
216 | ||
217 | return (B_FALSE); | |
218 | } | |
219 | ||
220 | /* | |
221 | * Check if a disk is effectively a multipath whole disk | |
222 | */ | |
223 | boolean_t | |
224 | is_mpath_whole_disk(const char *path) | |
225 | { | |
226 | struct udev *udev; | |
227 | struct udev_device *dev = NULL; | |
228 | char nodepath[MAXPATHLEN]; | |
229 | char *sysname; | |
230 | boolean_t wholedisk = B_FALSE; | |
231 | ||
232 | if (realpath(path, nodepath) == NULL) | |
233 | return (B_FALSE); | |
234 | sysname = strrchr(nodepath, '/') + 1; | |
235 | if (strncmp(sysname, "dm-", 3) != 0) | |
236 | return (B_FALSE); | |
237 | if ((udev = udev_new()) == NULL) | |
238 | return (B_FALSE); | |
239 | if ((dev = udev_device_new_from_subsystem_sysname(udev, "block", | |
240 | sysname)) == NULL) { | |
241 | udev_device_unref(dev); | |
242 | return (B_FALSE); | |
243 | } | |
244 | ||
245 | wholedisk = udev_mpath_whole_disk(dev); | |
246 | ||
247 | udev_device_unref(dev); | |
248 | return (wholedisk); | |
249 | } | |
250 | ||
251 | static int | |
252 | udev_device_is_ready(struct udev_device *dev) | |
253 | { | |
254 | #ifdef HAVE_LIBUDEV_UDEV_DEVICE_GET_IS_INITIALIZED | |
255 | return (udev_device_get_is_initialized(dev)); | |
256 | #else | |
257 | /* wait for DEVLINKS property to be initialized */ | |
258 | return (udev_device_get_property_value(dev, "DEVLINKS") != NULL); | |
259 | #endif | |
260 | } | |
261 | ||
2d82ea8b BB |
262 | /* |
263 | * Wait up to timeout_ms for udev to set up the device node. The device is | |
264 | * considered ready when libudev determines it has been initialized, all of | |
265 | * the device links have been verified to exist, and it has been allowed to | |
266 | * settle. At this point the device the device can be accessed reliably. | |
267 | * Depending on the complexity of the udev rules this process could take | |
268 | * several seconds. | |
269 | */ | |
270 | int | |
271 | zpool_label_disk_wait(char *path, int timeout_ms) | |
272 | { | |
273 | struct udev *udev; | |
274 | struct udev_device *dev = NULL; | |
275 | char nodepath[MAXPATHLEN]; | |
276 | char *sysname = NULL; | |
277 | int ret = ENODEV; | |
278 | int settle_ms = 50; | |
279 | long sleep_ms = 10; | |
280 | hrtime_t start, settle; | |
281 | ||
282 | if ((udev = udev_new()) == NULL) | |
283 | return (ENXIO); | |
284 | ||
285 | start = gethrtime(); | |
286 | settle = 0; | |
287 | ||
288 | do { | |
289 | if (sysname == NULL) { | |
290 | if (realpath(path, nodepath) != NULL) { | |
291 | sysname = strrchr(nodepath, '/') + 1; | |
292 | } else { | |
293 | (void) usleep(sleep_ms * MILLISEC); | |
294 | continue; | |
295 | } | |
296 | } | |
297 | ||
298 | dev = udev_device_new_from_subsystem_sysname(udev, | |
299 | "block", sysname); | |
300 | if ((dev != NULL) && udev_device_is_ready(dev)) { | |
301 | struct udev_list_entry *links, *link; | |
302 | ||
303 | ret = 0; | |
304 | links = udev_device_get_devlinks_list_entry(dev); | |
305 | ||
306 | udev_list_entry_foreach(link, links) { | |
307 | struct stat64 statbuf; | |
308 | const char *name; | |
309 | ||
310 | name = udev_list_entry_get_name(link); | |
311 | errno = 0; | |
312 | if (stat64(name, &statbuf) == 0 && errno == 0) | |
313 | continue; | |
314 | ||
315 | settle = 0; | |
316 | ret = ENODEV; | |
317 | break; | |
318 | } | |
319 | ||
320 | if (ret == 0) { | |
321 | if (settle == 0) { | |
322 | settle = gethrtime(); | |
323 | } else if (NSEC2MSEC(gethrtime() - settle) >= | |
324 | settle_ms) { | |
325 | udev_device_unref(dev); | |
326 | break; | |
327 | } | |
328 | } | |
329 | } | |
330 | ||
331 | udev_device_unref(dev); | |
332 | (void) usleep(sleep_ms * MILLISEC); | |
333 | ||
334 | } while (NSEC2MSEC(gethrtime() - start) < timeout_ms); | |
335 | ||
336 | udev_unref(udev); | |
337 | ||
338 | return (ret); | |
339 | } | |
340 | ||
341 | ||
39fc0cb5 DB |
342 | /* |
343 | * Encode the persistent devices strings | |
344 | * used for the vdev disk label | |
345 | */ | |
346 | static int | |
347 | encode_device_strings(const char *path, vdev_dev_strs_t *ds, | |
348 | boolean_t wholedisk) | |
34dc7c2f | 349 | { |
39fc0cb5 DB |
350 | struct udev *udev; |
351 | struct udev_device *dev = NULL; | |
352 | char nodepath[MAXPATHLEN]; | |
353 | char *sysname; | |
354 | int ret = ENODEV; | |
355 | hrtime_t start; | |
356 | ||
357 | if ((udev = udev_new()) == NULL) | |
358 | return (ENXIO); | |
359 | ||
360 | /* resolve path to a runtime device node instance */ | |
361 | if (realpath(path, nodepath) == NULL) | |
362 | goto no_dev; | |
363 | ||
364 | sysname = strrchr(nodepath, '/') + 1; | |
365 | ||
366 | /* | |
367 | * Wait up to 3 seconds for udev to set up the device node context | |
368 | */ | |
369 | start = gethrtime(); | |
370 | do { | |
371 | dev = udev_device_new_from_subsystem_sysname(udev, "block", | |
372 | sysname); | |
373 | if (dev == NULL) | |
374 | goto no_dev; | |
375 | if (udev_device_is_ready(dev)) | |
376 | break; /* udev ready */ | |
377 | ||
378 | udev_device_unref(dev); | |
379 | dev = NULL; | |
380 | ||
381 | if (NSEC2MSEC(gethrtime() - start) < 10) | |
382 | (void) sched_yield(); /* yield/busy wait up to 10ms */ | |
383 | else | |
384 | (void) usleep(10 * MILLISEC); | |
385 | ||
386 | } while (NSEC2MSEC(gethrtime() - start) < (3 * MILLISEC)); | |
387 | ||
388 | if (dev == NULL) | |
389 | goto no_dev; | |
390 | ||
391 | /* | |
392 | * Only whole disks require extra device strings | |
393 | */ | |
394 | if (!wholedisk && !udev_mpath_whole_disk(dev)) | |
395 | goto no_dev; | |
396 | ||
397 | ret = udev_device_get_devid(dev, ds->vds_devid, sizeof (ds->vds_devid)); | |
398 | if (ret != 0) | |
399 | goto no_dev_ref; | |
400 | ||
401 | /* physical location string (optional) */ | |
402 | if (udev_device_get_physical(dev, ds->vds_devphys, | |
403 | sizeof (ds->vds_devphys)) != 0) { | |
404 | ds->vds_devphys[0] = '\0'; /* empty string --> not available */ | |
34dc7c2f | 405 | } |
39fc0cb5 DB |
406 | |
407 | no_dev_ref: | |
408 | udev_device_unref(dev); | |
409 | no_dev: | |
410 | udev_unref(udev); | |
34dc7c2f BB |
411 | |
412 | return (ret); | |
413 | } | |
414 | ||
39fc0cb5 DB |
415 | /* |
416 | * Update a leaf vdev's persistent device strings (Linux only) | |
417 | * | |
418 | * - only applies for a dedicated leaf vdev (aka whole disk) | |
419 | * - updated during pool create|add|attach|import | |
420 | * - used for matching device matching during auto-{online,expand,replace} | |
421 | * - stored in a leaf disk config label (i.e. alongside 'path' NVP) | |
422 | * - these strings are currently not used in kernel (i.e. for vdev_disk_open) | |
423 | * | |
424 | * single device node example: | |
425 | * devid: 'scsi-MG03SCA300_350000494a8cb3d67-part1' | |
426 | * phys_path: 'pci-0000:04:00.0-sas-0x50000394a8cb3d67-lun-0' | |
427 | * | |
428 | * multipath device node example: | |
429 | * devid: 'dm-uuid-mpath-35000c5006304de3f' | |
430 | */ | |
431 | void | |
432 | update_vdev_config_dev_strs(nvlist_t *nv) | |
433 | { | |
434 | vdev_dev_strs_t vds; | |
435 | char *env, *type, *path; | |
436 | uint64_t wholedisk = 0; | |
437 | ||
438 | /* | |
439 | * For the benefit of legacy ZFS implementations, allow | |
440 | * for opting out of devid strings in the vdev label. | |
441 | * | |
442 | * example use: | |
443 | * env ZFS_VDEV_DEVID_OPT_OUT=YES zpool import dozer | |
444 | * | |
445 | * explanation: | |
446 | * Older ZFS on Linux implementations had issues when attempting to | |
447 | * display pool config VDEV names if a "devid" NVP value is present | |
448 | * in the pool's config. | |
449 | * | |
450 | * For example, a pool that originated on illumos platform would | |
451 | * have a devid value in the config and "zpool status" would fail | |
452 | * when listing the config. | |
453 | * | |
454 | * A pool can be stripped of any "devid" values on import or | |
455 | * prevented from adding them on zpool create|add by setting | |
456 | * ZFS_VDEV_DEVID_OPT_OUT. | |
457 | */ | |
458 | env = getenv("ZFS_VDEV_DEVID_OPT_OUT"); | |
459 | if (env && (strtoul(env, NULL, 0) > 0 || | |
460 | !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2))) { | |
461 | (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID); | |
462 | (void) nvlist_remove_all(nv, ZPOOL_CONFIG_PHYS_PATH); | |
463 | return; | |
464 | } | |
465 | ||
466 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0 || | |
467 | strcmp(type, VDEV_TYPE_DISK) != 0) { | |
468 | return; | |
469 | } | |
470 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) | |
471 | return; | |
472 | (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &wholedisk); | |
473 | ||
474 | /* | |
475 | * Update device string values in config nvlist | |
476 | */ | |
477 | if (encode_device_strings(path, &vds, (boolean_t)wholedisk) == 0) { | |
478 | (void) nvlist_add_string(nv, ZPOOL_CONFIG_DEVID, vds.vds_devid); | |
479 | if (vds.vds_devphys[0] != '\0') { | |
480 | (void) nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH, | |
481 | vds.vds_devphys); | |
482 | } | |
483 | } else { | |
484 | /* clear out any stale entries */ | |
485 | (void) nvlist_remove_all(nv, ZPOOL_CONFIG_DEVID); | |
486 | (void) nvlist_remove_all(nv, ZPOOL_CONFIG_PHYS_PATH); | |
487 | } | |
488 | } | |
489 | #else | |
490 | ||
491 | boolean_t | |
492 | is_mpath_whole_disk(const char *path) | |
493 | { | |
494 | return (B_FALSE); | |
495 | } | |
496 | ||
2d82ea8b BB |
497 | /* |
498 | * Wait up to timeout_ms for udev to set up the device node. The device is | |
499 | * considered ready when the provided path have been verified to exist and | |
500 | * it has been allowed to settle. At this point the device the device can | |
501 | * be accessed reliably. Depending on the complexity of the udev rules thisi | |
502 | * process could take several seconds. | |
503 | */ | |
504 | int | |
505 | zpool_label_disk_wait(char *path, int timeout_ms) | |
506 | { | |
507 | int settle_ms = 50; | |
508 | long sleep_ms = 10; | |
509 | hrtime_t start, settle; | |
510 | struct stat64 statbuf; | |
511 | ||
512 | start = gethrtime(); | |
513 | settle = 0; | |
514 | ||
515 | do { | |
516 | errno = 0; | |
517 | if ((stat64(path, &statbuf) == 0) && (errno == 0)) { | |
518 | if (settle == 0) | |
519 | settle = gethrtime(); | |
520 | else if (NSEC2MSEC(gethrtime() - settle) >= settle_ms) | |
521 | return (0); | |
522 | } else if (errno != ENOENT) { | |
523 | return (errno); | |
524 | } | |
525 | ||
526 | usleep(sleep_ms * MILLISEC); | |
527 | } while (NSEC2MSEC(gethrtime() - start) < timeout_ms); | |
528 | ||
529 | return (ENODEV); | |
530 | } | |
531 | ||
39fc0cb5 DB |
532 | void |
533 | update_vdev_config_dev_strs(nvlist_t *nv) | |
534 | { | |
535 | } | |
536 | ||
537 | #endif /* HAVE_LIBUDEV */ | |
538 | ||
34dc7c2f BB |
539 | /* |
540 | * Go through and fix up any path and/or devid information for the given vdev | |
541 | * configuration. | |
542 | */ | |
543 | static int | |
544 | fix_paths(nvlist_t *nv, name_entry_t *names) | |
545 | { | |
546 | nvlist_t **child; | |
547 | uint_t c, children; | |
548 | uint64_t guid; | |
549 | name_entry_t *ne, *best; | |
39fc0cb5 | 550 | char *path; |
34dc7c2f BB |
551 | |
552 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
553 | &child, &children) == 0) { | |
554 | for (c = 0; c < children; c++) | |
555 | if (fix_paths(child[c], names) != 0) | |
556 | return (-1); | |
557 | return (0); | |
558 | } | |
559 | ||
560 | /* | |
561 | * This is a leaf (file or disk) vdev. In either case, go through | |
562 | * the name list and see if we find a matching guid. If so, replace | |
563 | * the path and see if we can calculate a new devid. | |
564 | * | |
565 | * There may be multiple names associated with a particular guid, in | |
44867b6d BB |
566 | * which case we have overlapping partitions or multiple paths to the |
567 | * same disk. In this case we prefer to use the path name which | |
568 | * matches the ZPOOL_CONFIG_PATH. If no matching entry is found we | |
569 | * use the lowest order device which corresponds to the first match | |
570 | * while traversing the ZPOOL_IMPORT_PATH search path. | |
34dc7c2f BB |
571 | */ |
572 | verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) == 0); | |
573 | if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) != 0) | |
574 | path = NULL; | |
575 | ||
34dc7c2f BB |
576 | best = NULL; |
577 | for (ne = names; ne != NULL; ne = ne->ne_next) { | |
578 | if (ne->ne_guid == guid) { | |
34dc7c2f BB |
579 | if (path == NULL) { |
580 | best = ne; | |
581 | break; | |
582 | } | |
583 | ||
44867b6d | 584 | if ((strlen(path) == strlen(ne->ne_name)) && |
d1d7e268 | 585 | strncmp(path, ne->ne_name, strlen(path)) == 0) { |
34dc7c2f | 586 | best = ne; |
44867b6d | 587 | break; |
34dc7c2f | 588 | } |
44867b6d | 589 | |
7d90f569 | 590 | if (best == NULL) { |
44867b6d | 591 | best = ne; |
7d90f569 BB |
592 | continue; |
593 | } | |
594 | ||
595 | /* Prefer paths with move vdev labels. */ | |
596 | if (ne->ne_num_labels > best->ne_num_labels) { | |
597 | best = ne; | |
598 | continue; | |
599 | } | |
600 | ||
601 | /* Prefer paths earlier in the search order. */ | |
20c901dc | 602 | if (ne->ne_num_labels == best->ne_num_labels && |
7d90f569 BB |
603 | ne->ne_order < best->ne_order) { |
604 | best = ne; | |
605 | continue; | |
606 | } | |
34dc7c2f BB |
607 | } |
608 | } | |
609 | ||
610 | if (best == NULL) | |
611 | return (0); | |
612 | ||
613 | if (nvlist_add_string(nv, ZPOOL_CONFIG_PATH, best->ne_name) != 0) | |
614 | return (-1); | |
615 | ||
39fc0cb5 DB |
616 | /* Linux only - update ZPOOL_CONFIG_DEVID and ZPOOL_CONFIG_PHYS_PATH */ |
617 | update_vdev_config_dev_strs(nv); | |
34dc7c2f BB |
618 | |
619 | return (0); | |
620 | } | |
621 | ||
622 | /* | |
623 | * Add the given configuration to the list of known devices. | |
624 | */ | |
625 | static int | |
626 | add_config(libzfs_handle_t *hdl, pool_list_t *pl, const char *path, | |
7d90f569 | 627 | int order, int num_labels, nvlist_t *config) |
34dc7c2f BB |
628 | { |
629 | uint64_t pool_guid, vdev_guid, top_guid, txg, state; | |
630 | pool_entry_t *pe; | |
631 | vdev_entry_t *ve; | |
632 | config_entry_t *ce; | |
633 | name_entry_t *ne; | |
634 | ||
635 | /* | |
636 | * If this is a hot spare not currently in use or level 2 cache | |
637 | * device, add it to the list of names to translate, but don't do | |
638 | * anything else. | |
639 | */ | |
640 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
641 | &state) == 0 && | |
642 | (state == POOL_STATE_SPARE || state == POOL_STATE_L2CACHE) && | |
643 | nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid) == 0) { | |
644 | if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) | |
645 | return (-1); | |
646 | ||
647 | if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) { | |
648 | free(ne); | |
649 | return (-1); | |
650 | } | |
651 | ne->ne_guid = vdev_guid; | |
44867b6d | 652 | ne->ne_order = order; |
7d90f569 | 653 | ne->ne_num_labels = num_labels; |
34dc7c2f BB |
654 | ne->ne_next = pl->names; |
655 | pl->names = ne; | |
656 | return (0); | |
657 | } | |
658 | ||
659 | /* | |
660 | * If we have a valid config but cannot read any of these fields, then | |
661 | * it means we have a half-initialized label. In vdev_label_init() | |
662 | * we write a label with txg == 0 so that we can identify the device | |
663 | * in case the user refers to the same disk later on. If we fail to | |
664 | * create the pool, we'll be left with a label in this state | |
665 | * which should not be considered part of a valid pool. | |
666 | */ | |
667 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
668 | &pool_guid) != 0 || | |
669 | nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, | |
670 | &vdev_guid) != 0 || | |
671 | nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, | |
672 | &top_guid) != 0 || | |
673 | nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
674 | &txg) != 0 || txg == 0) { | |
675 | nvlist_free(config); | |
676 | return (0); | |
677 | } | |
678 | ||
679 | /* | |
680 | * First, see if we know about this pool. If not, then add it to the | |
681 | * list of known pools. | |
682 | */ | |
683 | for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { | |
684 | if (pe->pe_guid == pool_guid) | |
685 | break; | |
686 | } | |
687 | ||
688 | if (pe == NULL) { | |
689 | if ((pe = zfs_alloc(hdl, sizeof (pool_entry_t))) == NULL) { | |
690 | nvlist_free(config); | |
691 | return (-1); | |
692 | } | |
693 | pe->pe_guid = pool_guid; | |
694 | pe->pe_next = pl->pools; | |
695 | pl->pools = pe; | |
696 | } | |
697 | ||
698 | /* | |
699 | * Second, see if we know about this toplevel vdev. Add it if its | |
700 | * missing. | |
701 | */ | |
702 | for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { | |
703 | if (ve->ve_guid == top_guid) | |
704 | break; | |
705 | } | |
706 | ||
707 | if (ve == NULL) { | |
708 | if ((ve = zfs_alloc(hdl, sizeof (vdev_entry_t))) == NULL) { | |
709 | nvlist_free(config); | |
710 | return (-1); | |
711 | } | |
712 | ve->ve_guid = top_guid; | |
713 | ve->ve_next = pe->pe_vdevs; | |
714 | pe->pe_vdevs = ve; | |
715 | } | |
716 | ||
717 | /* | |
718 | * Third, see if we have a config with a matching transaction group. If | |
719 | * so, then we do nothing. Otherwise, add it to the list of known | |
720 | * configs. | |
721 | */ | |
722 | for (ce = ve->ve_configs; ce != NULL; ce = ce->ce_next) { | |
723 | if (ce->ce_txg == txg) | |
724 | break; | |
725 | } | |
726 | ||
727 | if (ce == NULL) { | |
728 | if ((ce = zfs_alloc(hdl, sizeof (config_entry_t))) == NULL) { | |
729 | nvlist_free(config); | |
730 | return (-1); | |
731 | } | |
732 | ce->ce_txg = txg; | |
733 | ce->ce_config = config; | |
734 | ce->ce_next = ve->ve_configs; | |
735 | ve->ve_configs = ce; | |
736 | } else { | |
737 | nvlist_free(config); | |
738 | } | |
739 | ||
740 | /* | |
741 | * At this point we've successfully added our config to the list of | |
742 | * known configs. The last thing to do is add the vdev guid -> path | |
743 | * mappings so that we can fix up the configuration as necessary before | |
744 | * doing the import. | |
745 | */ | |
746 | if ((ne = zfs_alloc(hdl, sizeof (name_entry_t))) == NULL) | |
747 | return (-1); | |
748 | ||
749 | if ((ne->ne_name = zfs_strdup(hdl, path)) == NULL) { | |
750 | free(ne); | |
751 | return (-1); | |
752 | } | |
753 | ||
754 | ne->ne_guid = vdev_guid; | |
44867b6d | 755 | ne->ne_order = order; |
7d90f569 | 756 | ne->ne_num_labels = num_labels; |
34dc7c2f BB |
757 | ne->ne_next = pl->names; |
758 | pl->names = ne; | |
759 | ||
760 | return (0); | |
761 | } | |
762 | ||
763 | /* | |
764 | * Returns true if the named pool matches the given GUID. | |
765 | */ | |
766 | static int | |
767 | pool_active(libzfs_handle_t *hdl, const char *name, uint64_t guid, | |
768 | boolean_t *isactive) | |
769 | { | |
770 | zpool_handle_t *zhp; | |
771 | uint64_t theguid; | |
772 | ||
773 | if (zpool_open_silent(hdl, name, &zhp) != 0) | |
774 | return (-1); | |
775 | ||
776 | if (zhp == NULL) { | |
777 | *isactive = B_FALSE; | |
778 | return (0); | |
779 | } | |
780 | ||
781 | verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_POOL_GUID, | |
782 | &theguid) == 0); | |
783 | ||
784 | zpool_close(zhp); | |
785 | ||
786 | *isactive = (theguid == guid); | |
787 | return (0); | |
788 | } | |
789 | ||
790 | static nvlist_t * | |
791 | refresh_config(libzfs_handle_t *hdl, nvlist_t *config) | |
792 | { | |
793 | nvlist_t *nvl; | |
13fe0198 | 794 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
795 | int err; |
796 | ||
797 | if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) | |
798 | return (NULL); | |
799 | ||
800 | if (zcmd_alloc_dst_nvlist(hdl, &zc, | |
801 | zc.zc_nvlist_conf_size * 2) != 0) { | |
802 | zcmd_free_nvlists(&zc); | |
803 | return (NULL); | |
804 | } | |
805 | ||
806 | while ((err = ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_TRYIMPORT, | |
807 | &zc)) != 0 && errno == ENOMEM) { | |
808 | if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { | |
809 | zcmd_free_nvlists(&zc); | |
810 | return (NULL); | |
811 | } | |
812 | } | |
813 | ||
814 | if (err) { | |
34dc7c2f BB |
815 | zcmd_free_nvlists(&zc); |
816 | return (NULL); | |
817 | } | |
818 | ||
819 | if (zcmd_read_dst_nvlist(hdl, &zc, &nvl) != 0) { | |
820 | zcmd_free_nvlists(&zc); | |
821 | return (NULL); | |
822 | } | |
823 | ||
824 | zcmd_free_nvlists(&zc); | |
825 | return (nvl); | |
826 | } | |
827 | ||
428870ff BB |
828 | /* |
829 | * Determine if the vdev id is a hole in the namespace. | |
830 | */ | |
831 | boolean_t | |
832 | vdev_is_hole(uint64_t *hole_array, uint_t holes, uint_t id) | |
833 | { | |
d6320ddb BB |
834 | int c; |
835 | ||
836 | for (c = 0; c < holes; c++) { | |
428870ff BB |
837 | |
838 | /* Top-level is a hole */ | |
839 | if (hole_array[c] == id) | |
840 | return (B_TRUE); | |
841 | } | |
842 | return (B_FALSE); | |
843 | } | |
844 | ||
34dc7c2f BB |
845 | /* |
846 | * Convert our list of pools into the definitive set of configurations. We | |
847 | * start by picking the best config for each toplevel vdev. Once that's done, | |
848 | * we assemble the toplevel vdevs into a full config for the pool. We make a | |
849 | * pass to fix up any incorrect paths, and then add it to the main list to | |
850 | * return to the user. | |
851 | */ | |
852 | static nvlist_t * | |
853 | get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok) | |
854 | { | |
855 | pool_entry_t *pe; | |
856 | vdev_entry_t *ve; | |
857 | config_entry_t *ce; | |
d4ed6673 | 858 | nvlist_t *ret = NULL, *config = NULL, *tmp = NULL, *nvtop, *nvroot; |
34dc7c2f BB |
859 | nvlist_t **spares, **l2cache; |
860 | uint_t i, nspares, nl2cache; | |
861 | boolean_t config_seen; | |
862 | uint64_t best_txg; | |
3bc7e0fb GW |
863 | char *name, *hostname = NULL; |
864 | uint64_t guid; | |
34dc7c2f BB |
865 | uint_t children = 0; |
866 | nvlist_t **child = NULL; | |
428870ff BB |
867 | uint_t holes; |
868 | uint64_t *hole_array, max_id; | |
34dc7c2f BB |
869 | uint_t c; |
870 | boolean_t isactive; | |
871 | uint64_t hostid; | |
872 | nvlist_t *nvl; | |
428870ff | 873 | boolean_t valid_top_config = B_FALSE; |
34dc7c2f BB |
874 | |
875 | if (nvlist_alloc(&ret, 0, 0) != 0) | |
876 | goto nomem; | |
877 | ||
878 | for (pe = pl->pools; pe != NULL; pe = pe->pe_next) { | |
428870ff | 879 | uint64_t id, max_txg = 0; |
34dc7c2f BB |
880 | |
881 | if (nvlist_alloc(&config, NV_UNIQUE_NAME, 0) != 0) | |
882 | goto nomem; | |
883 | config_seen = B_FALSE; | |
884 | ||
885 | /* | |
886 | * Iterate over all toplevel vdevs. Grab the pool configuration | |
887 | * from the first one we find, and then go through the rest and | |
888 | * add them as necessary to the 'vdevs' member of the config. | |
889 | */ | |
890 | for (ve = pe->pe_vdevs; ve != NULL; ve = ve->ve_next) { | |
891 | ||
892 | /* | |
893 | * Determine the best configuration for this vdev by | |
894 | * selecting the config with the latest transaction | |
895 | * group. | |
896 | */ | |
897 | best_txg = 0; | |
898 | for (ce = ve->ve_configs; ce != NULL; | |
899 | ce = ce->ce_next) { | |
900 | ||
901 | if (ce->ce_txg > best_txg) { | |
902 | tmp = ce->ce_config; | |
903 | best_txg = ce->ce_txg; | |
904 | } | |
905 | } | |
906 | ||
428870ff BB |
907 | /* |
908 | * We rely on the fact that the max txg for the | |
909 | * pool will contain the most up-to-date information | |
910 | * about the valid top-levels in the vdev namespace. | |
911 | */ | |
912 | if (best_txg > max_txg) { | |
913 | (void) nvlist_remove(config, | |
914 | ZPOOL_CONFIG_VDEV_CHILDREN, | |
915 | DATA_TYPE_UINT64); | |
916 | (void) nvlist_remove(config, | |
917 | ZPOOL_CONFIG_HOLE_ARRAY, | |
918 | DATA_TYPE_UINT64_ARRAY); | |
919 | ||
920 | max_txg = best_txg; | |
921 | hole_array = NULL; | |
922 | holes = 0; | |
923 | max_id = 0; | |
924 | valid_top_config = B_FALSE; | |
925 | ||
926 | if (nvlist_lookup_uint64(tmp, | |
927 | ZPOOL_CONFIG_VDEV_CHILDREN, &max_id) == 0) { | |
928 | verify(nvlist_add_uint64(config, | |
929 | ZPOOL_CONFIG_VDEV_CHILDREN, | |
930 | max_id) == 0); | |
931 | valid_top_config = B_TRUE; | |
932 | } | |
933 | ||
934 | if (nvlist_lookup_uint64_array(tmp, | |
935 | ZPOOL_CONFIG_HOLE_ARRAY, &hole_array, | |
936 | &holes) == 0) { | |
937 | verify(nvlist_add_uint64_array(config, | |
938 | ZPOOL_CONFIG_HOLE_ARRAY, | |
939 | hole_array, holes) == 0); | |
940 | } | |
941 | } | |
942 | ||
34dc7c2f BB |
943 | if (!config_seen) { |
944 | /* | |
945 | * Copy the relevant pieces of data to the pool | |
946 | * configuration: | |
947 | * | |
948 | * version | |
3bc7e0fb GW |
949 | * pool guid |
950 | * name | |
d96eb2b1 | 951 | * comment (if available) |
3bc7e0fb | 952 | * pool state |
34dc7c2f BB |
953 | * hostid (if available) |
954 | * hostname (if available) | |
955 | */ | |
295304be | 956 | uint64_t state, version; |
3bc7e0fb GW |
957 | char *comment = NULL; |
958 | ||
959 | version = fnvlist_lookup_uint64(tmp, | |
960 | ZPOOL_CONFIG_VERSION); | |
961 | fnvlist_add_uint64(config, | |
962 | ZPOOL_CONFIG_VERSION, version); | |
963 | guid = fnvlist_lookup_uint64(tmp, | |
964 | ZPOOL_CONFIG_POOL_GUID); | |
965 | fnvlist_add_uint64(config, | |
966 | ZPOOL_CONFIG_POOL_GUID, guid); | |
967 | name = fnvlist_lookup_string(tmp, | |
968 | ZPOOL_CONFIG_POOL_NAME); | |
969 | fnvlist_add_string(config, | |
970 | ZPOOL_CONFIG_POOL_NAME, name); | |
34dc7c2f | 971 | |
d96eb2b1 | 972 | if (nvlist_lookup_string(tmp, |
3bc7e0fb GW |
973 | ZPOOL_CONFIG_COMMENT, &comment) == 0) |
974 | fnvlist_add_string(config, | |
975 | ZPOOL_CONFIG_COMMENT, comment); | |
d96eb2b1 | 976 | |
3bc7e0fb GW |
977 | state = fnvlist_lookup_uint64(tmp, |
978 | ZPOOL_CONFIG_POOL_STATE); | |
979 | fnvlist_add_uint64(config, | |
980 | ZPOOL_CONFIG_POOL_STATE, state); | |
d96eb2b1 | 981 | |
34dc7c2f BB |
982 | hostid = 0; |
983 | if (nvlist_lookup_uint64(tmp, | |
984 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { | |
3bc7e0fb GW |
985 | fnvlist_add_uint64(config, |
986 | ZPOOL_CONFIG_HOSTID, hostid); | |
987 | hostname = fnvlist_lookup_string(tmp, | |
988 | ZPOOL_CONFIG_HOSTNAME); | |
989 | fnvlist_add_string(config, | |
990 | ZPOOL_CONFIG_HOSTNAME, hostname); | |
34dc7c2f BB |
991 | } |
992 | ||
993 | config_seen = B_TRUE; | |
994 | } | |
995 | ||
996 | /* | |
997 | * Add this top-level vdev to the child array. | |
998 | */ | |
999 | verify(nvlist_lookup_nvlist(tmp, | |
1000 | ZPOOL_CONFIG_VDEV_TREE, &nvtop) == 0); | |
1001 | verify(nvlist_lookup_uint64(nvtop, ZPOOL_CONFIG_ID, | |
1002 | &id) == 0); | |
428870ff | 1003 | |
34dc7c2f BB |
1004 | if (id >= children) { |
1005 | nvlist_t **newchild; | |
1006 | ||
1007 | newchild = zfs_alloc(hdl, (id + 1) * | |
1008 | sizeof (nvlist_t *)); | |
1009 | if (newchild == NULL) | |
1010 | goto nomem; | |
1011 | ||
1012 | for (c = 0; c < children; c++) | |
1013 | newchild[c] = child[c]; | |
1014 | ||
1015 | free(child); | |
1016 | child = newchild; | |
1017 | children = id + 1; | |
1018 | } | |
1019 | if (nvlist_dup(nvtop, &child[id], 0) != 0) | |
1020 | goto nomem; | |
1021 | ||
1022 | } | |
1023 | ||
428870ff BB |
1024 | /* |
1025 | * If we have information about all the top-levels then | |
1026 | * clean up the nvlist which we've constructed. This | |
1027 | * means removing any extraneous devices that are | |
1028 | * beyond the valid range or adding devices to the end | |
1029 | * of our array which appear to be missing. | |
1030 | */ | |
1031 | if (valid_top_config) { | |
1032 | if (max_id < children) { | |
1033 | for (c = max_id; c < children; c++) | |
1034 | nvlist_free(child[c]); | |
1035 | children = max_id; | |
1036 | } else if (max_id > children) { | |
1037 | nvlist_t **newchild; | |
1038 | ||
1039 | newchild = zfs_alloc(hdl, (max_id) * | |
1040 | sizeof (nvlist_t *)); | |
1041 | if (newchild == NULL) | |
1042 | goto nomem; | |
1043 | ||
1044 | for (c = 0; c < children; c++) | |
1045 | newchild[c] = child[c]; | |
1046 | ||
1047 | free(child); | |
1048 | child = newchild; | |
1049 | children = max_id; | |
1050 | } | |
1051 | } | |
1052 | ||
34dc7c2f BB |
1053 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, |
1054 | &guid) == 0); | |
1055 | ||
428870ff BB |
1056 | /* |
1057 | * The vdev namespace may contain holes as a result of | |
1058 | * device removal. We must add them back into the vdev | |
1059 | * tree before we process any missing devices. | |
1060 | */ | |
1061 | if (holes > 0) { | |
1062 | ASSERT(valid_top_config); | |
1063 | ||
1064 | for (c = 0; c < children; c++) { | |
1065 | nvlist_t *holey; | |
1066 | ||
1067 | if (child[c] != NULL || | |
1068 | !vdev_is_hole(hole_array, holes, c)) | |
1069 | continue; | |
1070 | ||
1071 | if (nvlist_alloc(&holey, NV_UNIQUE_NAME, | |
1072 | 0) != 0) | |
1073 | goto nomem; | |
1074 | ||
1075 | /* | |
1076 | * Holes in the namespace are treated as | |
1077 | * "hole" top-level vdevs and have a | |
1078 | * special flag set on them. | |
1079 | */ | |
1080 | if (nvlist_add_string(holey, | |
1081 | ZPOOL_CONFIG_TYPE, | |
1082 | VDEV_TYPE_HOLE) != 0 || | |
1083 | nvlist_add_uint64(holey, | |
1084 | ZPOOL_CONFIG_ID, c) != 0 || | |
1085 | nvlist_add_uint64(holey, | |
0fdd8d64 MT |
1086 | ZPOOL_CONFIG_GUID, 0ULL) != 0) { |
1087 | nvlist_free(holey); | |
428870ff | 1088 | goto nomem; |
0fdd8d64 | 1089 | } |
428870ff BB |
1090 | child[c] = holey; |
1091 | } | |
1092 | } | |
1093 | ||
34dc7c2f BB |
1094 | /* |
1095 | * Look for any missing top-level vdevs. If this is the case, | |
1096 | * create a faked up 'missing' vdev as a placeholder. We cannot | |
1097 | * simply compress the child array, because the kernel performs | |
1098 | * certain checks to make sure the vdev IDs match their location | |
1099 | * in the configuration. | |
1100 | */ | |
428870ff | 1101 | for (c = 0; c < children; c++) { |
34dc7c2f BB |
1102 | if (child[c] == NULL) { |
1103 | nvlist_t *missing; | |
1104 | if (nvlist_alloc(&missing, NV_UNIQUE_NAME, | |
1105 | 0) != 0) | |
1106 | goto nomem; | |
1107 | if (nvlist_add_string(missing, | |
1108 | ZPOOL_CONFIG_TYPE, | |
1109 | VDEV_TYPE_MISSING) != 0 || | |
1110 | nvlist_add_uint64(missing, | |
1111 | ZPOOL_CONFIG_ID, c) != 0 || | |
1112 | nvlist_add_uint64(missing, | |
1113 | ZPOOL_CONFIG_GUID, 0ULL) != 0) { | |
1114 | nvlist_free(missing); | |
1115 | goto nomem; | |
1116 | } | |
1117 | child[c] = missing; | |
1118 | } | |
428870ff | 1119 | } |
34dc7c2f BB |
1120 | |
1121 | /* | |
1122 | * Put all of this pool's top-level vdevs into a root vdev. | |
1123 | */ | |
1124 | if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) | |
1125 | goto nomem; | |
1126 | if (nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, | |
1127 | VDEV_TYPE_ROOT) != 0 || | |
1128 | nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) != 0 || | |
1129 | nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, guid) != 0 || | |
1130 | nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
1131 | child, children) != 0) { | |
1132 | nvlist_free(nvroot); | |
1133 | goto nomem; | |
1134 | } | |
1135 | ||
1136 | for (c = 0; c < children; c++) | |
1137 | nvlist_free(child[c]); | |
1138 | free(child); | |
1139 | children = 0; | |
1140 | child = NULL; | |
1141 | ||
1142 | /* | |
1143 | * Go through and fix up any paths and/or devids based on our | |
1144 | * known list of vdev GUID -> path mappings. | |
1145 | */ | |
1146 | if (fix_paths(nvroot, pl->names) != 0) { | |
1147 | nvlist_free(nvroot); | |
1148 | goto nomem; | |
1149 | } | |
1150 | ||
1151 | /* | |
1152 | * Add the root vdev to this pool's configuration. | |
1153 | */ | |
1154 | if (nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
1155 | nvroot) != 0) { | |
1156 | nvlist_free(nvroot); | |
1157 | goto nomem; | |
1158 | } | |
1159 | nvlist_free(nvroot); | |
1160 | ||
1161 | /* | |
1162 | * zdb uses this path to report on active pools that were | |
1163 | * imported or created using -R. | |
1164 | */ | |
1165 | if (active_ok) | |
1166 | goto add_pool; | |
1167 | ||
1168 | /* | |
1169 | * Determine if this pool is currently active, in which case we | |
1170 | * can't actually import it. | |
1171 | */ | |
1172 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
1173 | &name) == 0); | |
1174 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
1175 | &guid) == 0); | |
1176 | ||
1177 | if (pool_active(hdl, name, guid, &isactive) != 0) | |
1178 | goto error; | |
1179 | ||
1180 | if (isactive) { | |
1181 | nvlist_free(config); | |
1182 | config = NULL; | |
1183 | continue; | |
1184 | } | |
1185 | ||
428870ff BB |
1186 | if ((nvl = refresh_config(hdl, config)) == NULL) { |
1187 | nvlist_free(config); | |
1188 | config = NULL; | |
1189 | continue; | |
1190 | } | |
34dc7c2f BB |
1191 | |
1192 | nvlist_free(config); | |
1193 | config = nvl; | |
1194 | ||
1195 | /* | |
1196 | * Go through and update the paths for spares, now that we have | |
1197 | * them. | |
1198 | */ | |
1199 | verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
1200 | &nvroot) == 0); | |
1201 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
1202 | &spares, &nspares) == 0) { | |
1203 | for (i = 0; i < nspares; i++) { | |
1204 | if (fix_paths(spares[i], pl->names) != 0) | |
1205 | goto nomem; | |
1206 | } | |
1207 | } | |
1208 | ||
1209 | /* | |
1210 | * Update the paths for l2cache devices. | |
1211 | */ | |
1212 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
1213 | &l2cache, &nl2cache) == 0) { | |
1214 | for (i = 0; i < nl2cache; i++) { | |
1215 | if (fix_paths(l2cache[i], pl->names) != 0) | |
1216 | goto nomem; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * Restore the original information read from the actual label. | |
1222 | */ | |
1223 | (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTID, | |
1224 | DATA_TYPE_UINT64); | |
1225 | (void) nvlist_remove(config, ZPOOL_CONFIG_HOSTNAME, | |
1226 | DATA_TYPE_STRING); | |
1227 | if (hostid != 0) { | |
1228 | verify(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, | |
1229 | hostid) == 0); | |
1230 | verify(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, | |
1231 | hostname) == 0); | |
1232 | } | |
1233 | ||
1234 | add_pool: | |
1235 | /* | |
1236 | * Add this pool to the list of configs. | |
1237 | */ | |
1238 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
1239 | &name) == 0); | |
1240 | if (nvlist_add_nvlist(ret, name, config) != 0) | |
1241 | goto nomem; | |
1242 | ||
1243 | nvlist_free(config); | |
1244 | config = NULL; | |
1245 | } | |
1246 | ||
1247 | return (ret); | |
1248 | ||
1249 | nomem: | |
1250 | (void) no_memory(hdl); | |
1251 | error: | |
1252 | nvlist_free(config); | |
1253 | nvlist_free(ret); | |
1254 | for (c = 0; c < children; c++) | |
1255 | nvlist_free(child[c]); | |
1256 | free(child); | |
1257 | ||
1258 | return (NULL); | |
1259 | } | |
1260 | ||
1261 | /* | |
1262 | * Return the offset of the given label. | |
1263 | */ | |
1264 | static uint64_t | |
1265 | label_offset(uint64_t size, int l) | |
1266 | { | |
1267 | ASSERT(P2PHASE_TYPED(size, sizeof (vdev_label_t), uint64_t) == 0); | |
1268 | return (l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ? | |
1269 | 0 : size - VDEV_LABELS * sizeof (vdev_label_t))); | |
1270 | } | |
1271 | ||
1272 | /* | |
1273 | * Given a file descriptor, read the label information and return an nvlist | |
7d90f569 BB |
1274 | * describing the configuration, if there is one. The number of valid |
1275 | * labels found will be returned in num_labels when non-NULL. | |
34dc7c2f BB |
1276 | */ |
1277 | int | |
7d90f569 | 1278 | zpool_read_label(int fd, nvlist_t **config, int *num_labels) |
34dc7c2f BB |
1279 | { |
1280 | struct stat64 statbuf; | |
7d90f569 | 1281 | int l, count = 0; |
34dc7c2f | 1282 | vdev_label_t *label; |
7d90f569 BB |
1283 | nvlist_t *expected_config = NULL; |
1284 | uint64_t expected_guid = 0, size; | |
34dc7c2f BB |
1285 | |
1286 | *config = NULL; | |
1287 | ||
ff3510c1 | 1288 | if (fstat64_blk(fd, &statbuf) == -1) |
34dc7c2f BB |
1289 | return (0); |
1290 | size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); | |
1291 | ||
1292 | if ((label = malloc(sizeof (vdev_label_t))) == NULL) | |
1293 | return (-1); | |
1294 | ||
1295 | for (l = 0; l < VDEV_LABELS; l++) { | |
7d90f569 BB |
1296 | uint64_t state, guid, txg; |
1297 | ||
b128c09f | 1298 | if (pread64(fd, label, sizeof (vdev_label_t), |
34dc7c2f BB |
1299 | label_offset(size, l)) != sizeof (vdev_label_t)) |
1300 | continue; | |
1301 | ||
1302 | if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, | |
1303 | sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) | |
1304 | continue; | |
1305 | ||
7d90f569 BB |
1306 | if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_GUID, |
1307 | &guid) != 0 || guid == 0) { | |
1308 | nvlist_free(*config); | |
1309 | continue; | |
1310 | } | |
1311 | ||
34dc7c2f BB |
1312 | if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, |
1313 | &state) != 0 || state > POOL_STATE_L2CACHE) { | |
1314 | nvlist_free(*config); | |
1315 | continue; | |
1316 | } | |
1317 | ||
1318 | if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE && | |
1319 | (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, | |
1320 | &txg) != 0 || txg == 0)) { | |
1321 | nvlist_free(*config); | |
1322 | continue; | |
1323 | } | |
1324 | ||
7d90f569 BB |
1325 | if (expected_guid) { |
1326 | if (expected_guid == guid) | |
1327 | count++; | |
1328 | ||
1329 | nvlist_free(*config); | |
1330 | } else { | |
1331 | expected_config = *config; | |
1332 | expected_guid = guid; | |
1333 | count++; | |
1334 | } | |
34dc7c2f BB |
1335 | } |
1336 | ||
7d90f569 BB |
1337 | if (num_labels != NULL) |
1338 | *num_labels = count; | |
1339 | ||
34dc7c2f | 1340 | free(label); |
7d90f569 BB |
1341 | *config = expected_config; |
1342 | ||
34dc7c2f BB |
1343 | return (0); |
1344 | } | |
1345 | ||
519129ff | 1346 | typedef struct rdsk_node { |
8a39abaa BB |
1347 | char *rn_name; /* Full path to device */ |
1348 | int rn_order; /* Preferred order (low to high) */ | |
1349 | int rn_num_labels; /* Number of valid labels */ | |
519129ff | 1350 | libzfs_handle_t *rn_hdl; |
8a39abaa | 1351 | nvlist_t *rn_config; /* Label config */ |
519129ff BB |
1352 | avl_tree_t *rn_avl; |
1353 | avl_node_t rn_node; | |
8a39abaa BB |
1354 | kmutex_t *rn_lock; |
1355 | boolean_t rn_labelpaths; | |
519129ff BB |
1356 | } rdsk_node_t; |
1357 | ||
1358 | static int | |
1359 | slice_cache_compare(const void *arg1, const void *arg2) | |
1360 | { | |
1361 | const char *nm1 = ((rdsk_node_t *)arg1)->rn_name; | |
1362 | const char *nm2 = ((rdsk_node_t *)arg2)->rn_name; | |
1363 | char *nm1slice, *nm2slice; | |
1364 | int rv; | |
1365 | ||
1366 | /* | |
1367 | * partitions one and three (slices zero and two) are the most | |
1368 | * likely to provide results, so put those first | |
1369 | */ | |
1370 | nm1slice = strstr(nm1, "part1"); | |
1371 | nm2slice = strstr(nm2, "part1"); | |
1372 | if (nm1slice && !nm2slice) { | |
1373 | return (-1); | |
1374 | } | |
1375 | if (!nm1slice && nm2slice) { | |
1376 | return (1); | |
1377 | } | |
1378 | nm1slice = strstr(nm1, "part3"); | |
1379 | nm2slice = strstr(nm2, "part3"); | |
1380 | if (nm1slice && !nm2slice) { | |
1381 | return (-1); | |
1382 | } | |
1383 | if (!nm1slice && nm2slice) { | |
1384 | return (1); | |
1385 | } | |
1386 | ||
1387 | rv = strcmp(nm1, nm2); | |
1388 | if (rv == 0) | |
1389 | return (0); | |
1390 | return (rv > 0 ? 1 : -1); | |
1391 | } | |
1392 | ||
8fc5674c NB |
1393 | static boolean_t |
1394 | is_watchdog_dev(char *dev) | |
1395 | { | |
1396 | /* For 'watchdog' dev */ | |
1397 | if (strcmp(dev, "watchdog") == 0) | |
1398 | return (B_TRUE); | |
1399 | ||
1400 | /* For 'watchdog<digit><whatever> */ | |
1401 | if (strstr(dev, "watchdog") == dev && isdigit(dev[8])) | |
1402 | return (B_TRUE); | |
1403 | ||
1404 | return (B_FALSE); | |
1405 | } | |
1406 | ||
8a39abaa BB |
1407 | static int |
1408 | label_paths_impl(libzfs_handle_t *hdl, nvlist_t *nvroot, uint64_t pool_guid, | |
1409 | uint64_t vdev_guid, char **path, char **devid) | |
1410 | { | |
1411 | nvlist_t **child; | |
1412 | uint_t c, children; | |
1413 | uint64_t guid; | |
1414 | char *val; | |
1415 | int error; | |
1416 | ||
1417 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
1418 | &child, &children) == 0) { | |
1419 | for (c = 0; c < children; c++) { | |
1420 | error = label_paths_impl(hdl, child[c], | |
1421 | pool_guid, vdev_guid, path, devid); | |
1422 | if (error) | |
1423 | return (error); | |
1424 | } | |
1425 | return (0); | |
1426 | } | |
1427 | ||
1428 | if (nvroot == NULL) | |
1429 | return (0); | |
1430 | ||
1431 | error = nvlist_lookup_uint64(nvroot, ZPOOL_CONFIG_GUID, &guid); | |
1432 | if ((error != 0) || (guid != vdev_guid)) | |
1433 | return (0); | |
1434 | ||
1435 | error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_PATH, &val); | |
1436 | if (error == 0) | |
1437 | *path = val; | |
1438 | ||
1439 | error = nvlist_lookup_string(nvroot, ZPOOL_CONFIG_DEVID, &val); | |
1440 | if (error == 0) | |
1441 | *devid = val; | |
1442 | ||
1443 | return (0); | |
1444 | } | |
1445 | ||
1446 | /* | |
1447 | * Given a disk label fetch the ZPOOL_CONFIG_PATH and ZPOOL_CONFIG_DEVID | |
1448 | * and store these strings as config_path and devid_path respectively. | |
1449 | * The returned pointers are only valid as long as label remains valid. | |
1450 | */ | |
1451 | static int | |
1452 | label_paths(libzfs_handle_t *hdl, nvlist_t *label, char **path, char **devid) | |
1453 | { | |
1454 | nvlist_t *nvroot; | |
1455 | uint64_t pool_guid; | |
1456 | uint64_t vdev_guid; | |
1457 | ||
1458 | *path = NULL; | |
1459 | *devid = NULL; | |
1460 | ||
1461 | if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvroot) || | |
1462 | nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &pool_guid) || | |
1463 | nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &vdev_guid)) | |
1464 | return (ENOENT); | |
1465 | ||
1466 | return (label_paths_impl(hdl, nvroot, pool_guid, vdev_guid, path, | |
1467 | devid)); | |
1468 | } | |
1469 | ||
519129ff BB |
1470 | static void |
1471 | zpool_open_func(void *arg) | |
1472 | { | |
1473 | rdsk_node_t *rn = arg; | |
8a39abaa | 1474 | libzfs_handle_t *hdl = rn->rn_hdl; |
519129ff BB |
1475 | struct stat64 statbuf; |
1476 | nvlist_t *config; | |
8a39abaa BB |
1477 | char *bname, *dupname; |
1478 | int error; | |
519129ff BB |
1479 | int num_labels; |
1480 | int fd; | |
1481 | ||
519129ff BB |
1482 | /* |
1483 | * Skip devices with well known prefixes there can be side effects | |
1484 | * when opening devices which need to be avoided. | |
1485 | * | |
519129ff | 1486 | * hpet - High Precision Event Timer |
519129ff BB |
1487 | * watchdog - Watchdog must be closed in a special way. |
1488 | */ | |
8a39abaa BB |
1489 | dupname = zfs_strdup(hdl, rn->rn_name); |
1490 | bname = basename(dupname); | |
1491 | error = ((strcmp(bname, "hpet") == 0) || is_watchdog_dev(bname)); | |
1492 | free(dupname); | |
1493 | if (error) | |
519129ff BB |
1494 | return; |
1495 | ||
1496 | /* | |
1497 | * Ignore failed stats. We only want regular files and block devices. | |
1498 | */ | |
8a39abaa | 1499 | if (stat64(rn->rn_name, &statbuf) != 0 || |
519129ff BB |
1500 | (!S_ISREG(statbuf.st_mode) && !S_ISBLK(statbuf.st_mode))) |
1501 | return; | |
1502 | ||
8a39abaa | 1503 | if ((fd = open(rn->rn_name, O_RDONLY)) < 0) |
519129ff | 1504 | return; |
8a39abaa | 1505 | |
519129ff | 1506 | /* |
8a39abaa | 1507 | * This file is too small to hold a zpool |
519129ff | 1508 | */ |
519129ff BB |
1509 | if (S_ISREG(statbuf.st_mode) && |
1510 | statbuf.st_size < SPA_MINDEVSIZE) { | |
1511 | (void) close(fd); | |
1512 | return; | |
519129ff BB |
1513 | } |
1514 | ||
1515 | if ((zpool_read_label(fd, &config, &num_labels)) != 0) { | |
1516 | (void) close(fd); | |
519129ff BB |
1517 | return; |
1518 | } | |
1519 | ||
1520 | if (num_labels == 0) { | |
1521 | (void) close(fd); | |
1522 | nvlist_free(config); | |
1523 | return; | |
1524 | } | |
1525 | ||
1526 | (void) close(fd); | |
1527 | ||
1528 | rn->rn_config = config; | |
1529 | rn->rn_num_labels = num_labels; | |
8a39abaa BB |
1530 | |
1531 | /* | |
1532 | * Add additional entries for paths described by this label. | |
1533 | */ | |
1534 | if (rn->rn_labelpaths) { | |
1535 | char *path = NULL; | |
1536 | char *devid = NULL; | |
1537 | rdsk_node_t *slice; | |
1538 | avl_index_t where; | |
1539 | int error; | |
1540 | ||
1541 | if (label_paths(rn->rn_hdl, rn->rn_config, &path, &devid)) | |
1542 | return; | |
1543 | ||
1544 | /* | |
1545 | * Allow devlinks to stabilize so all paths are available. | |
1546 | */ | |
1547 | zpool_label_disk_wait(rn->rn_name, DISK_LABEL_WAIT); | |
1548 | ||
1549 | if (path != NULL) { | |
1550 | slice = zfs_alloc(hdl, sizeof (rdsk_node_t)); | |
1551 | slice->rn_name = zfs_strdup(hdl, path); | |
1552 | slice->rn_avl = rn->rn_avl; | |
1553 | slice->rn_hdl = hdl; | |
1554 | slice->rn_order = 1; | |
1555 | slice->rn_labelpaths = B_FALSE; | |
1556 | mutex_enter(rn->rn_lock); | |
1557 | if (avl_find(rn->rn_avl, slice, &where)) { | |
1558 | mutex_exit(rn->rn_lock); | |
1559 | free(slice->rn_name); | |
1560 | free(slice); | |
1561 | } else { | |
1562 | avl_insert(rn->rn_avl, slice, where); | |
1563 | mutex_exit(rn->rn_lock); | |
1564 | zpool_open_func(slice); | |
1565 | } | |
1566 | } | |
1567 | ||
1568 | if (devid != NULL) { | |
1569 | slice = zfs_alloc(hdl, sizeof (rdsk_node_t)); | |
1570 | error = asprintf(&slice->rn_name, "%s%s", | |
1571 | DEV_BYID_PATH, devid); | |
1572 | if (error == -1) { | |
1573 | free(slice); | |
1574 | return; | |
1575 | } | |
1576 | ||
1577 | slice->rn_avl = rn->rn_avl; | |
1578 | slice->rn_hdl = hdl; | |
1579 | slice->rn_order = 2; | |
1580 | slice->rn_labelpaths = B_FALSE; | |
1581 | mutex_enter(rn->rn_lock); | |
1582 | if (avl_find(rn->rn_avl, slice, &where)) { | |
1583 | mutex_exit(rn->rn_lock); | |
1584 | free(slice->rn_name); | |
1585 | free(slice); | |
1586 | } else { | |
1587 | avl_insert(rn->rn_avl, slice, where); | |
1588 | mutex_exit(rn->rn_lock); | |
1589 | zpool_open_func(slice); | |
1590 | } | |
1591 | } | |
1592 | } | |
519129ff BB |
1593 | } |
1594 | ||
51a3ae72 DK |
1595 | /* |
1596 | * Given a file descriptor, clear (zero) the label information. This function | |
131cc95c DK |
1597 | * is used in the appliance stack as part of the ZFS sysevent module and |
1598 | * to implement the "zpool labelclear" command. | |
51a3ae72 DK |
1599 | */ |
1600 | int | |
1601 | zpool_clear_label(int fd) | |
1602 | { | |
1603 | struct stat64 statbuf; | |
1604 | int l; | |
1605 | vdev_label_t *label; | |
1606 | uint64_t size; | |
1607 | ||
1608 | if (fstat64_blk(fd, &statbuf) == -1) | |
1609 | return (0); | |
1610 | size = P2ALIGN_TYPED(statbuf.st_size, sizeof (vdev_label_t), uint64_t); | |
1611 | ||
1612 | if ((label = calloc(sizeof (vdev_label_t), 1)) == NULL) | |
1613 | return (-1); | |
1614 | ||
1615 | for (l = 0; l < VDEV_LABELS; l++) { | |
1616 | if (pwrite64(fd, label, sizeof (vdev_label_t), | |
4def05f8 RY |
1617 | label_offset(size, l)) != sizeof (vdev_label_t)) { |
1618 | free(label); | |
51a3ae72 | 1619 | return (-1); |
4def05f8 | 1620 | } |
51a3ae72 DK |
1621 | } |
1622 | ||
1623 | free(label); | |
1624 | return (0); | |
1625 | } | |
1626 | ||
d603ed6c | 1627 | /* |
8a39abaa BB |
1628 | * Scan a list of directories for zfs devices. |
1629 | */ | |
1630 | static int | |
1631 | zpool_find_import_scan(libzfs_handle_t *hdl, kmutex_t *lock, | |
1632 | avl_tree_t **slice_cache, char **dir, int dirs) | |
1633 | { | |
1634 | avl_tree_t *cache; | |
1635 | rdsk_node_t *slice; | |
1636 | void *cookie; | |
1637 | int i, error; | |
1638 | ||
1639 | *slice_cache = NULL; | |
1640 | cache = zfs_alloc(hdl, sizeof (avl_tree_t)); | |
1641 | avl_create(cache, slice_cache_compare, sizeof (rdsk_node_t), | |
1642 | offsetof(rdsk_node_t, rn_node)); | |
1643 | ||
1644 | for (i = 0; i < dirs; i++) { | |
1645 | char path[MAXPATHLEN]; | |
1646 | struct dirent64 *dp; | |
1647 | DIR *dirp; | |
1648 | ||
1649 | if (realpath(dir[i], path) == NULL) { | |
1650 | error = errno; | |
1651 | if (error == ENOENT) | |
1652 | continue; | |
1653 | ||
1654 | zfs_error_aux(hdl, strerror(error)); | |
1655 | (void) zfs_error_fmt(hdl, EZFS_BADPATH, dgettext( | |
1656 | TEXT_DOMAIN, "cannot resolve path '%s'"), dir[i]); | |
1657 | goto error; | |
1658 | } | |
1659 | ||
1660 | dirp = opendir(path); | |
1661 | if (dirp == NULL) { | |
1662 | error = errno; | |
1663 | zfs_error_aux(hdl, strerror(error)); | |
1664 | (void) zfs_error_fmt(hdl, EZFS_BADPATH, | |
1665 | dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); | |
1666 | goto error; | |
1667 | } | |
1668 | ||
1669 | while ((dp = readdir64(dirp)) != NULL) { | |
1670 | const char *name = dp->d_name; | |
1671 | if (name[0] == '.' && | |
1672 | (name[1] == 0 || (name[1] == '.' && name[2] == 0))) | |
1673 | continue; | |
1674 | ||
1675 | slice = zfs_alloc(hdl, sizeof (rdsk_node_t)); | |
1676 | error = asprintf(&slice->rn_name, "%s/%s", path, name); | |
1677 | if (error == -1) { | |
1678 | free(slice); | |
1679 | continue; | |
1680 | } | |
1681 | slice->rn_lock = lock; | |
1682 | slice->rn_avl = cache; | |
1683 | slice->rn_hdl = hdl; | |
1684 | slice->rn_order = i+1; | |
1685 | slice->rn_labelpaths = B_FALSE; | |
1686 | mutex_enter(lock); | |
1687 | avl_add(cache, slice); | |
1688 | mutex_exit(lock); | |
1689 | } | |
1690 | ||
1691 | (void) closedir(dirp); | |
1692 | } | |
1693 | ||
1694 | *slice_cache = cache; | |
1695 | return (0); | |
1696 | ||
1697 | error: | |
1698 | while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { | |
1699 | free(slice->rn_name); | |
1700 | free(slice); | |
1701 | } | |
1702 | free(cache); | |
1703 | ||
1704 | return (error); | |
1705 | } | |
1706 | ||
1707 | /* | |
1708 | * Use libblkid to quickly enumerate all known zfs devices. | |
d603ed6c | 1709 | */ |
428870ff | 1710 | static int |
8a39abaa BB |
1711 | zpool_find_import_blkid(libzfs_handle_t *hdl, kmutex_t *lock, |
1712 | avl_tree_t **slice_cache) | |
428870ff | 1713 | { |
8a39abaa | 1714 | rdsk_node_t *slice; |
d603ed6c BB |
1715 | blkid_cache cache; |
1716 | blkid_dev_iterate iter; | |
1717 | blkid_dev dev; | |
8a39abaa | 1718 | int error; |
428870ff | 1719 | |
8a39abaa BB |
1720 | *slice_cache = NULL; |
1721 | ||
1722 | error = blkid_get_cache(&cache, NULL); | |
1723 | if (error != 0) | |
1724 | return (error); | |
428870ff | 1725 | |
8a39abaa BB |
1726 | error = blkid_probe_all_new(cache); |
1727 | if (error != 0) { | |
1728 | blkid_put_cache(cache); | |
1729 | return (error); | |
428870ff | 1730 | } |
428870ff | 1731 | |
d603ed6c BB |
1732 | iter = blkid_dev_iterate_begin(cache); |
1733 | if (iter == NULL) { | |
8a39abaa BB |
1734 | blkid_put_cache(cache); |
1735 | return (EINVAL); | |
d603ed6c | 1736 | } |
428870ff | 1737 | |
8a39abaa BB |
1738 | error = blkid_dev_set_search(iter, "TYPE", "zfs_member"); |
1739 | if (error != 0) { | |
1740 | blkid_dev_iterate_end(iter); | |
1741 | blkid_put_cache(cache); | |
1742 | return (error); | |
428870ff | 1743 | } |
428870ff | 1744 | |
8a39abaa BB |
1745 | *slice_cache = zfs_alloc(hdl, sizeof (avl_tree_t)); |
1746 | avl_create(*slice_cache, slice_cache_compare, sizeof (rdsk_node_t), | |
1747 | offsetof(rdsk_node_t, rn_node)); | |
428870ff | 1748 | |
8a39abaa BB |
1749 | while (blkid_dev_next(iter, &dev) == 0) { |
1750 | slice = zfs_alloc(hdl, sizeof (rdsk_node_t)); | |
1751 | slice->rn_name = zfs_strdup(hdl, blkid_dev_devname(dev)); | |
1752 | slice->rn_lock = lock; | |
1753 | slice->rn_avl = *slice_cache; | |
1754 | slice->rn_hdl = hdl; | |
1755 | slice->rn_order = 100; | |
1756 | slice->rn_labelpaths = B_TRUE; | |
1757 | mutex_enter(lock); | |
1758 | avl_add(*slice_cache, slice); | |
1759 | mutex_exit(lock); | |
428870ff BB |
1760 | } |
1761 | ||
d603ed6c | 1762 | blkid_dev_iterate_end(iter); |
d603ed6c | 1763 | blkid_put_cache(cache); |
8a39abaa BB |
1764 | |
1765 | return (0); | |
428870ff BB |
1766 | } |
1767 | ||
eac47204 | 1768 | char * |
44867b6d BB |
1769 | zpool_default_import_path[DEFAULT_IMPORT_PATH_SIZE] = { |
1770 | "/dev/disk/by-vdev", /* Custom rules, use first if they exist */ | |
44867b6d | 1771 | "/dev/mapper", /* Use multipath devices before components */ |
95003f70 TC |
1772 | "/dev/disk/by-partlabel", /* Single unique entry set by user */ |
1773 | "/dev/disk/by-partuuid", /* Generated partition uuid */ | |
1774 | "/dev/disk/by-label", /* Custom persistent labels */ | |
44867b6d BB |
1775 | "/dev/disk/by-uuid", /* Single unique entry and persistent */ |
1776 | "/dev/disk/by-id", /* May be multiple entries and persistent */ | |
1777 | "/dev/disk/by-path", /* Encodes physical location and persistent */ | |
44867b6d BB |
1778 | "/dev" /* UNSAFE device names will change */ |
1779 | }; | |
1780 | ||
34dc7c2f BB |
1781 | /* |
1782 | * Given a list of directories to search, find all pools stored on disk. This | |
1783 | * includes partial pools which are not available to import. If no args are | |
1784 | * given (argc is 0), then the default directory (/dev/dsk) is searched. | |
b128c09f BB |
1785 | * poolname or guid (but not both) are provided by the caller when trying |
1786 | * to import a specific pool. | |
34dc7c2f | 1787 | */ |
b128c09f | 1788 | static nvlist_t * |
428870ff | 1789 | zpool_find_import_impl(libzfs_handle_t *hdl, importargs_t *iarg) |
34dc7c2f | 1790 | { |
519129ff | 1791 | nvlist_t *ret = NULL; |
34dc7c2f BB |
1792 | pool_list_t pools = { 0 }; |
1793 | pool_entry_t *pe, *penext; | |
1794 | vdev_entry_t *ve, *venext; | |
1795 | config_entry_t *ce, *cenext; | |
1796 | name_entry_t *ne, *nenext; | |
8a39abaa BB |
1797 | kmutex_t lock; |
1798 | avl_tree_t *cache; | |
519129ff BB |
1799 | rdsk_node_t *slice; |
1800 | void *cookie; | |
8a39abaa | 1801 | taskq_t *t; |
d603ed6c BB |
1802 | |
1803 | verify(iarg->poolname == NULL || iarg->guid == 0); | |
8a39abaa | 1804 | mutex_init(&lock, NULL, MUTEX_DEFAULT, NULL); |
34dc7c2f | 1805 | |
7d11e37e | 1806 | /* |
8a39abaa BB |
1807 | * Locate pool member vdevs using libblkid or by directory scanning. |
1808 | * On success a newly allocated AVL tree which is populated with an | |
1809 | * entry for each discovered vdev will be returned as the cache. | |
1810 | * It's the callers responsibility to consume and destroy this tree. | |
7d11e37e | 1811 | */ |
8a39abaa BB |
1812 | if (iarg->scan || iarg->paths != 0) { |
1813 | int dirs = iarg->paths; | |
1814 | char **dir = iarg->path; | |
d603ed6c | 1815 | |
8a39abaa BB |
1816 | if (dirs == 0) { |
1817 | dir = zpool_default_import_path; | |
1818 | dirs = DEFAULT_IMPORT_PATH_SIZE; | |
1819 | } | |
1820 | ||
1821 | if (zpool_find_import_scan(hdl, &lock, &cache, dir, dirs) != 0) | |
1822 | return (NULL); | |
1823 | } else { | |
1824 | if (zpool_find_import_blkid(hdl, &lock, &cache) != 0) | |
1825 | return (NULL); | |
34dc7c2f BB |
1826 | } |
1827 | ||
1828 | /* | |
8a39abaa BB |
1829 | * Create a thread pool to parallelize the process of reading and |
1830 | * validating labels, a large number of threads can be used due to | |
1831 | * minimal contention. | |
34dc7c2f | 1832 | */ |
8a39abaa BB |
1833 | t = taskq_create("z_import", 2 * boot_ncpus, defclsyspri, |
1834 | 2 * boot_ncpus, INT_MAX, TASKQ_PREPOPULATE); | |
44867b6d | 1835 | |
8a39abaa BB |
1836 | for (slice = avl_first(cache); slice; |
1837 | (slice = avl_walk(cache, slice, AVL_AFTER))) | |
1838 | (void) taskq_dispatch(t, zpool_open_func, slice, TQ_SLEEP); | |
44867b6d | 1839 | |
8a39abaa BB |
1840 | taskq_wait(t); |
1841 | taskq_destroy(t); | |
34dc7c2f | 1842 | |
8a39abaa BB |
1843 | /* |
1844 | * Process the cache filtering out any entries which are not | |
1845 | * for the specificed pool then adding matching label configs. | |
1846 | */ | |
1847 | cookie = NULL; | |
1848 | while ((slice = avl_destroy_nodes(cache, &cookie)) != NULL) { | |
1849 | if (slice->rn_config != NULL) { | |
1850 | nvlist_t *config = slice->rn_config; | |
1851 | boolean_t matched = B_TRUE; | |
1852 | ||
1853 | if (iarg->poolname != NULL) { | |
1854 | char *pname; | |
1855 | ||
1856 | matched = nvlist_lookup_string(config, | |
1857 | ZPOOL_CONFIG_POOL_NAME, &pname) == 0 && | |
1858 | strcmp(iarg->poolname, pname) == 0; | |
1859 | } else if (iarg->guid != 0) { | |
1860 | uint64_t this_guid; | |
1861 | ||
1862 | matched = nvlist_lookup_uint64(config, | |
1863 | ZPOOL_CONFIG_POOL_GUID, &this_guid) == 0 && | |
1864 | iarg->guid == this_guid; | |
1865 | } | |
1866 | if (!matched) { | |
1867 | nvlist_free(config); | |
1868 | } else { | |
1869 | add_config(hdl, &pools, | |
1870 | slice->rn_name, slice->rn_order, | |
1871 | slice->rn_num_labels, config); | |
34dc7c2f BB |
1872 | } |
1873 | } | |
8a39abaa BB |
1874 | free(slice->rn_name); |
1875 | free(slice); | |
34dc7c2f | 1876 | } |
8a39abaa BB |
1877 | avl_destroy(cache); |
1878 | free(cache); | |
1879 | mutex_destroy(&lock); | |
34dc7c2f | 1880 | |
428870ff | 1881 | ret = get_configs(hdl, &pools, iarg->can_be_active); |
34dc7c2f | 1882 | |
34dc7c2f BB |
1883 | for (pe = pools.pools; pe != NULL; pe = penext) { |
1884 | penext = pe->pe_next; | |
1885 | for (ve = pe->pe_vdevs; ve != NULL; ve = venext) { | |
1886 | venext = ve->ve_next; | |
1887 | for (ce = ve->ve_configs; ce != NULL; ce = cenext) { | |
1888 | cenext = ce->ce_next; | |
8a5fc748 | 1889 | nvlist_free(ce->ce_config); |
34dc7c2f BB |
1890 | free(ce); |
1891 | } | |
1892 | free(ve); | |
1893 | } | |
1894 | free(pe); | |
1895 | } | |
1896 | ||
1897 | for (ne = pools.names; ne != NULL; ne = nenext) { | |
1898 | nenext = ne->ne_next; | |
0fdd8d64 | 1899 | free(ne->ne_name); |
34dc7c2f BB |
1900 | free(ne); |
1901 | } | |
1902 | ||
34dc7c2f BB |
1903 | return (ret); |
1904 | } | |
1905 | ||
b128c09f BB |
1906 | nvlist_t * |
1907 | zpool_find_import(libzfs_handle_t *hdl, int argc, char **argv) | |
1908 | { | |
428870ff | 1909 | importargs_t iarg = { 0 }; |
b128c09f | 1910 | |
428870ff BB |
1911 | iarg.paths = argc; |
1912 | iarg.path = argv; | |
b128c09f | 1913 | |
428870ff | 1914 | return (zpool_find_import_impl(hdl, &iarg)); |
b128c09f BB |
1915 | } |
1916 | ||
34dc7c2f BB |
1917 | /* |
1918 | * Given a cache file, return the contents as a list of importable pools. | |
b128c09f BB |
1919 | * poolname or guid (but not both) are provided by the caller when trying |
1920 | * to import a specific pool. | |
34dc7c2f BB |
1921 | */ |
1922 | nvlist_t * | |
1923 | zpool_find_import_cached(libzfs_handle_t *hdl, const char *cachefile, | |
b128c09f | 1924 | char *poolname, uint64_t guid) |
34dc7c2f BB |
1925 | { |
1926 | char *buf; | |
1927 | int fd; | |
1928 | struct stat64 statbuf; | |
1929 | nvlist_t *raw, *src, *dst; | |
1930 | nvlist_t *pools; | |
1931 | nvpair_t *elem; | |
1932 | char *name; | |
b128c09f | 1933 | uint64_t this_guid; |
34dc7c2f BB |
1934 | boolean_t active; |
1935 | ||
b128c09f BB |
1936 | verify(poolname == NULL || guid == 0); |
1937 | ||
34dc7c2f BB |
1938 | if ((fd = open(cachefile, O_RDONLY)) < 0) { |
1939 | zfs_error_aux(hdl, "%s", strerror(errno)); | |
1940 | (void) zfs_error(hdl, EZFS_BADCACHE, | |
1941 | dgettext(TEXT_DOMAIN, "failed to open cache file")); | |
1942 | return (NULL); | |
1943 | } | |
1944 | ||
1945 | if (fstat64(fd, &statbuf) != 0) { | |
1946 | zfs_error_aux(hdl, "%s", strerror(errno)); | |
1947 | (void) close(fd); | |
1948 | (void) zfs_error(hdl, EZFS_BADCACHE, | |
1949 | dgettext(TEXT_DOMAIN, "failed to get size of cache file")); | |
1950 | return (NULL); | |
1951 | } | |
1952 | ||
1953 | if ((buf = zfs_alloc(hdl, statbuf.st_size)) == NULL) { | |
1954 | (void) close(fd); | |
1955 | return (NULL); | |
1956 | } | |
1957 | ||
1958 | if (read(fd, buf, statbuf.st_size) != statbuf.st_size) { | |
1959 | (void) close(fd); | |
1960 | free(buf); | |
1961 | (void) zfs_error(hdl, EZFS_BADCACHE, | |
1962 | dgettext(TEXT_DOMAIN, | |
1963 | "failed to read cache file contents")); | |
1964 | return (NULL); | |
1965 | } | |
1966 | ||
1967 | (void) close(fd); | |
1968 | ||
1969 | if (nvlist_unpack(buf, statbuf.st_size, &raw, 0) != 0) { | |
1970 | free(buf); | |
1971 | (void) zfs_error(hdl, EZFS_BADCACHE, | |
1972 | dgettext(TEXT_DOMAIN, | |
1973 | "invalid or corrupt cache file contents")); | |
1974 | return (NULL); | |
1975 | } | |
1976 | ||
1977 | free(buf); | |
1978 | ||
1979 | /* | |
1980 | * Go through and get the current state of the pools and refresh their | |
1981 | * state. | |
1982 | */ | |
1983 | if (nvlist_alloc(&pools, 0, 0) != 0) { | |
1984 | (void) no_memory(hdl); | |
1985 | nvlist_free(raw); | |
1986 | return (NULL); | |
1987 | } | |
1988 | ||
1989 | elem = NULL; | |
1990 | while ((elem = nvlist_next_nvpair(raw, elem)) != NULL) { | |
ab2894e6 | 1991 | src = fnvpair_value_nvlist(elem); |
34dc7c2f | 1992 | |
ab2894e6 | 1993 | name = fnvlist_lookup_string(src, ZPOOL_CONFIG_POOL_NAME); |
b128c09f BB |
1994 | if (poolname != NULL && strcmp(poolname, name) != 0) |
1995 | continue; | |
1996 | ||
ab2894e6 MA |
1997 | this_guid = fnvlist_lookup_uint64(src, ZPOOL_CONFIG_POOL_GUID); |
1998 | if (guid != 0 && guid != this_guid) | |
1999 | continue; | |
34dc7c2f | 2000 | |
b128c09f BB |
2001 | if (pool_active(hdl, name, this_guid, &active) != 0) { |
2002 | nvlist_free(raw); | |
2003 | nvlist_free(pools); | |
2004 | return (NULL); | |
2005 | } | |
34dc7c2f | 2006 | |
b128c09f BB |
2007 | if (active) |
2008 | continue; | |
34dc7c2f | 2009 | |
b128c09f BB |
2010 | if ((dst = refresh_config(hdl, src)) == NULL) { |
2011 | nvlist_free(raw); | |
2012 | nvlist_free(pools); | |
2013 | return (NULL); | |
2014 | } | |
34dc7c2f | 2015 | |
b128c09f BB |
2016 | if (nvlist_add_nvlist(pools, nvpair_name(elem), dst) != 0) { |
2017 | (void) no_memory(hdl); | |
34dc7c2f | 2018 | nvlist_free(dst); |
b128c09f BB |
2019 | nvlist_free(raw); |
2020 | nvlist_free(pools); | |
2021 | return (NULL); | |
34dc7c2f | 2022 | } |
b128c09f | 2023 | nvlist_free(dst); |
34dc7c2f BB |
2024 | } |
2025 | ||
2026 | nvlist_free(raw); | |
2027 | return (pools); | |
2028 | } | |
2029 | ||
428870ff BB |
2030 | static int |
2031 | name_or_guid_exists(zpool_handle_t *zhp, void *data) | |
2032 | { | |
2033 | importargs_t *import = data; | |
2034 | int found = 0; | |
2035 | ||
2036 | if (import->poolname != NULL) { | |
2037 | char *pool_name; | |
2038 | ||
2039 | verify(nvlist_lookup_string(zhp->zpool_config, | |
2040 | ZPOOL_CONFIG_POOL_NAME, &pool_name) == 0); | |
2041 | if (strcmp(pool_name, import->poolname) == 0) | |
2042 | found = 1; | |
2043 | } else { | |
2044 | uint64_t pool_guid; | |
2045 | ||
2046 | verify(nvlist_lookup_uint64(zhp->zpool_config, | |
2047 | ZPOOL_CONFIG_POOL_GUID, &pool_guid) == 0); | |
2048 | if (pool_guid == import->guid) | |
2049 | found = 1; | |
2050 | } | |
2051 | ||
2052 | zpool_close(zhp); | |
2053 | return (found); | |
2054 | } | |
2055 | ||
2056 | nvlist_t * | |
2057 | zpool_search_import(libzfs_handle_t *hdl, importargs_t *import) | |
2058 | { | |
2059 | verify(import->poolname == NULL || import->guid == 0); | |
2060 | ||
2061 | if (import->unique) | |
2062 | import->exists = zpool_iter(hdl, name_or_guid_exists, import); | |
2063 | ||
2064 | if (import->cachefile != NULL) | |
2065 | return (zpool_find_import_cached(hdl, import->cachefile, | |
2066 | import->poolname, import->guid)); | |
2067 | ||
2068 | return (zpool_find_import_impl(hdl, import)); | |
2069 | } | |
34dc7c2f BB |
2070 | |
2071 | boolean_t | |
2072 | find_guid(nvlist_t *nv, uint64_t guid) | |
2073 | { | |
2074 | uint64_t tmp; | |
2075 | nvlist_t **child; | |
2076 | uint_t c, children; | |
2077 | ||
2078 | verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &tmp) == 0); | |
2079 | if (tmp == guid) | |
2080 | return (B_TRUE); | |
2081 | ||
2082 | if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, | |
2083 | &child, &children) == 0) { | |
2084 | for (c = 0; c < children; c++) | |
2085 | if (find_guid(child[c], guid)) | |
2086 | return (B_TRUE); | |
2087 | } | |
2088 | ||
2089 | return (B_FALSE); | |
2090 | } | |
2091 | ||
2092 | typedef struct aux_cbdata { | |
2093 | const char *cb_type; | |
2094 | uint64_t cb_guid; | |
2095 | zpool_handle_t *cb_zhp; | |
2096 | } aux_cbdata_t; | |
2097 | ||
2098 | static int | |
2099 | find_aux(zpool_handle_t *zhp, void *data) | |
2100 | { | |
2101 | aux_cbdata_t *cbp = data; | |
2102 | nvlist_t **list; | |
2103 | uint_t i, count; | |
2104 | uint64_t guid; | |
2105 | nvlist_t *nvroot; | |
2106 | ||
2107 | verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE, | |
2108 | &nvroot) == 0); | |
2109 | ||
2110 | if (nvlist_lookup_nvlist_array(nvroot, cbp->cb_type, | |
2111 | &list, &count) == 0) { | |
2112 | for (i = 0; i < count; i++) { | |
2113 | verify(nvlist_lookup_uint64(list[i], | |
2114 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
2115 | if (guid == cbp->cb_guid) { | |
2116 | cbp->cb_zhp = zhp; | |
2117 | return (1); | |
2118 | } | |
2119 | } | |
2120 | } | |
2121 | ||
2122 | zpool_close(zhp); | |
2123 | return (0); | |
2124 | } | |
2125 | ||
2126 | /* | |
2127 | * Determines if the pool is in use. If so, it returns true and the state of | |
2128 | * the pool as well as the name of the pool. Both strings are allocated and | |
2129 | * must be freed by the caller. | |
2130 | */ | |
2131 | int | |
2132 | zpool_in_use(libzfs_handle_t *hdl, int fd, pool_state_t *state, char **namestr, | |
2133 | boolean_t *inuse) | |
2134 | { | |
2135 | nvlist_t *config; | |
2136 | char *name; | |
2137 | boolean_t ret; | |
2138 | uint64_t guid, vdev_guid; | |
2139 | zpool_handle_t *zhp; | |
2140 | nvlist_t *pool_config; | |
2141 | uint64_t stateval, isspare; | |
2142 | aux_cbdata_t cb = { 0 }; | |
2143 | boolean_t isactive; | |
2144 | ||
2145 | *inuse = B_FALSE; | |
2146 | ||
7d90f569 | 2147 | if (zpool_read_label(fd, &config, NULL) != 0) { |
34dc7c2f BB |
2148 | (void) no_memory(hdl); |
2149 | return (-1); | |
2150 | } | |
2151 | ||
2152 | if (config == NULL) | |
2153 | return (0); | |
2154 | ||
2155 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
2156 | &stateval) == 0); | |
2157 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, | |
2158 | &vdev_guid) == 0); | |
2159 | ||
2160 | if (stateval != POOL_STATE_SPARE && stateval != POOL_STATE_L2CACHE) { | |
2161 | verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
2162 | &name) == 0); | |
2163 | verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
2164 | &guid) == 0); | |
2165 | } | |
2166 | ||
2167 | switch (stateval) { | |
2168 | case POOL_STATE_EXPORTED: | |
572e2857 BB |
2169 | /* |
2170 | * A pool with an exported state may in fact be imported | |
2171 | * read-only, so check the in-core state to see if it's | |
2172 | * active and imported read-only. If it is, set | |
2173 | * its state to active. | |
2174 | */ | |
2175 | if (pool_active(hdl, name, guid, &isactive) == 0 && isactive && | |
02f8fe42 JJS |
2176 | (zhp = zpool_open_canfail(hdl, name)) != NULL) { |
2177 | if (zpool_get_prop_int(zhp, ZPOOL_PROP_READONLY, NULL)) | |
2178 | stateval = POOL_STATE_ACTIVE; | |
2179 | ||
2180 | /* | |
2181 | * All we needed the zpool handle for is the | |
2182 | * readonly prop check. | |
2183 | */ | |
2184 | zpool_close(zhp); | |
2185 | } | |
572e2857 | 2186 | |
34dc7c2f BB |
2187 | ret = B_TRUE; |
2188 | break; | |
2189 | ||
2190 | case POOL_STATE_ACTIVE: | |
2191 | /* | |
2192 | * For an active pool, we have to determine if it's really part | |
2193 | * of a currently active pool (in which case the pool will exist | |
2194 | * and the guid will be the same), or whether it's part of an | |
2195 | * active pool that was disconnected without being explicitly | |
2196 | * exported. | |
2197 | */ | |
2198 | if (pool_active(hdl, name, guid, &isactive) != 0) { | |
2199 | nvlist_free(config); | |
2200 | return (-1); | |
2201 | } | |
2202 | ||
2203 | if (isactive) { | |
2204 | /* | |
2205 | * Because the device may have been removed while | |
2206 | * offlined, we only report it as active if the vdev is | |
2207 | * still present in the config. Otherwise, pretend like | |
2208 | * it's not in use. | |
2209 | */ | |
2210 | if ((zhp = zpool_open_canfail(hdl, name)) != NULL && | |
2211 | (pool_config = zpool_get_config(zhp, NULL)) | |
2212 | != NULL) { | |
2213 | nvlist_t *nvroot; | |
2214 | ||
2215 | verify(nvlist_lookup_nvlist(pool_config, | |
2216 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
2217 | ret = find_guid(nvroot, vdev_guid); | |
2218 | } else { | |
2219 | ret = B_FALSE; | |
2220 | } | |
2221 | ||
2222 | /* | |
2223 | * If this is an active spare within another pool, we | |
2224 | * treat it like an unused hot spare. This allows the | |
2225 | * user to create a pool with a hot spare that currently | |
2226 | * in use within another pool. Since we return B_TRUE, | |
2227 | * libdiskmgt will continue to prevent generic consumers | |
2228 | * from using the device. | |
2229 | */ | |
2230 | if (ret && nvlist_lookup_uint64(config, | |
2231 | ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare) | |
2232 | stateval = POOL_STATE_SPARE; | |
2233 | ||
2234 | if (zhp != NULL) | |
2235 | zpool_close(zhp); | |
2236 | } else { | |
2237 | stateval = POOL_STATE_POTENTIALLY_ACTIVE; | |
2238 | ret = B_TRUE; | |
2239 | } | |
2240 | break; | |
2241 | ||
2242 | case POOL_STATE_SPARE: | |
2243 | /* | |
2244 | * For a hot spare, it can be either definitively in use, or | |
2245 | * potentially active. To determine if it's in use, we iterate | |
2246 | * over all pools in the system and search for one with a spare | |
2247 | * with a matching guid. | |
2248 | * | |
2249 | * Due to the shared nature of spares, we don't actually report | |
2250 | * the potentially active case as in use. This means the user | |
2251 | * can freely create pools on the hot spares of exported pools, | |
2252 | * but to do otherwise makes the resulting code complicated, and | |
2253 | * we end up having to deal with this case anyway. | |
2254 | */ | |
2255 | cb.cb_zhp = NULL; | |
2256 | cb.cb_guid = vdev_guid; | |
2257 | cb.cb_type = ZPOOL_CONFIG_SPARES; | |
2258 | if (zpool_iter(hdl, find_aux, &cb) == 1) { | |
2259 | name = (char *)zpool_get_name(cb.cb_zhp); | |
0fdd8d64 | 2260 | ret = B_TRUE; |
34dc7c2f | 2261 | } else { |
0fdd8d64 | 2262 | ret = B_FALSE; |
34dc7c2f BB |
2263 | } |
2264 | break; | |
2265 | ||
2266 | case POOL_STATE_L2CACHE: | |
2267 | ||
2268 | /* | |
2269 | * Check if any pool is currently using this l2cache device. | |
2270 | */ | |
2271 | cb.cb_zhp = NULL; | |
2272 | cb.cb_guid = vdev_guid; | |
2273 | cb.cb_type = ZPOOL_CONFIG_L2CACHE; | |
2274 | if (zpool_iter(hdl, find_aux, &cb) == 1) { | |
2275 | name = (char *)zpool_get_name(cb.cb_zhp); | |
0fdd8d64 | 2276 | ret = B_TRUE; |
34dc7c2f | 2277 | } else { |
0fdd8d64 | 2278 | ret = B_FALSE; |
34dc7c2f BB |
2279 | } |
2280 | break; | |
2281 | ||
2282 | default: | |
2283 | ret = B_FALSE; | |
2284 | } | |
2285 | ||
2286 | ||
2287 | if (ret) { | |
2288 | if ((*namestr = zfs_strdup(hdl, name)) == NULL) { | |
2289 | if (cb.cb_zhp) | |
2290 | zpool_close(cb.cb_zhp); | |
2291 | nvlist_free(config); | |
2292 | return (-1); | |
2293 | } | |
2294 | *state = (pool_state_t)stateval; | |
2295 | } | |
2296 | ||
2297 | if (cb.cb_zhp) | |
2298 | zpool_close(cb.cb_zhp); | |
2299 | ||
2300 | nvlist_free(config); | |
2301 | *inuse = ret; | |
2302 | return (0); | |
2303 | } |