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 https://opensource.org/licenses/CDDL-1.0. 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 /* 22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012 by Delphix. All rights reserved. 24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 25 * Copyright (c) 2016, 2017, Intel Corporation. 26 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved. 27 * Copyright (c) 2023, Klara Inc. 28 */ 29 30 /* 31 * ZFS syseventd module. 32 * 33 * file origin: openzfs/usr/src/cmd/syseventd/modules/zfs_mod/zfs_mod.c 34 * 35 * The purpose of this module is to identify when devices are added to the 36 * system, and appropriately online or replace the affected vdevs. 37 * 38 * When a device is added to the system: 39 * 40 * 1. Search for any vdevs whose devid matches that of the newly added 41 * device. 42 * 43 * 2. If no vdevs are found, then search for any vdevs whose udev path 44 * matches that of the new device. 45 * 46 * 3. If no vdevs match by either method, then ignore the event. 47 * 48 * 4. Attempt to online the device with a flag to indicate that it should 49 * be unspared when resilvering completes. If this succeeds, then the 50 * same device was inserted and we should continue normally. 51 * 52 * 5. If the pool does not have the 'autoreplace' property set, attempt to 53 * online the device again without the unspare flag, which will 54 * generate a FMA fault. 55 * 56 * 6. If the pool has the 'autoreplace' property set, and the matching vdev 57 * is a whole disk, then label the new disk and attempt a 'zpool 58 * replace'. 59 * 60 * The module responds to EC_DEV_ADD events. The special ESC_ZFS_VDEV_CHECK 61 * event indicates that a device failed to open during pool load, but the 62 * autoreplace property was set. In this case, we deferred the associated 63 * FMA fault until our module had a chance to process the autoreplace logic. 64 * If the device could not be replaced, then the second online attempt will 65 * trigger the FMA fault that we skipped earlier. 66 * 67 * On Linux udev provides a disk insert for both the disk and the partition. 68 */ 69 70 #include <ctype.h> 71 #include <fcntl.h> 72 #include <libnvpair.h> 73 #include <libzfs.h> 74 #include <libzutil.h> 75 #include <limits.h> 76 #include <stddef.h> 77 #include <stdlib.h> 78 #include <string.h> 79 #include <syslog.h> 80 #include <sys/list.h> 81 #include <sys/sunddi.h> 82 #include <sys/sysevent/eventdefs.h> 83 #include <sys/sysevent/dev.h> 84 #include <thread_pool.h> 85 #include <pthread.h> 86 #include <unistd.h> 87 #include <errno.h> 88 #include "zfs_agents.h" 89 #include "../zed_log.h" 90 91 #define DEV_BYID_PATH "/dev/disk/by-id/" 92 #define DEV_BYPATH_PATH "/dev/disk/by-path/" 93 #define DEV_BYVDEV_PATH "/dev/disk/by-vdev/" 94 95 typedef void (*zfs_process_func_t)(zpool_handle_t *, nvlist_t *, boolean_t); 96 97 libzfs_handle_t *g_zfshdl; 98 list_t g_pool_list; /* list of unavailable pools at initialization */ 99 list_t g_device_list; /* list of disks with asynchronous label request */ 100 tpool_t *g_tpool; 101 boolean_t g_enumeration_done; 102 pthread_t g_zfs_tid; /* zfs_enum_pools() thread */ 103 104 typedef struct unavailpool { 105 zpool_handle_t *uap_zhp; 106 list_node_t uap_node; 107 } unavailpool_t; 108 109 typedef struct pendingdev { 110 char pd_physpath[128]; 111 list_node_t pd_node; 112 } pendingdev_t; 113 114 static int 115 zfs_toplevel_state(zpool_handle_t *zhp) 116 { 117 nvlist_t *nvroot; 118 vdev_stat_t *vs; 119 unsigned int c; 120 121 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL), 122 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); 123 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS, 124 (uint64_t **)&vs, &c) == 0); 125 return (vs->vs_state); 126 } 127 128 static int 129 zfs_unavail_pool(zpool_handle_t *zhp, void *data) 130 { 131 zed_log_msg(LOG_INFO, "zfs_unavail_pool: examining '%s' (state %d)", 132 zpool_get_name(zhp), (int)zfs_toplevel_state(zhp)); 133 134 if (zfs_toplevel_state(zhp) < VDEV_STATE_DEGRADED) { 135 unavailpool_t *uap; 136 uap = malloc(sizeof (unavailpool_t)); 137 if (uap == NULL) { 138 perror("malloc"); 139 exit(EXIT_FAILURE); 140 } 141 142 uap->uap_zhp = zhp; 143 list_insert_tail((list_t *)data, uap); 144 } else { 145 zpool_close(zhp); 146 } 147 return (0); 148 } 149 150 /* 151 * Write an array of strings to the zed log 152 */ 153 static void lines_to_zed_log_msg(char **lines, int lines_cnt) 154 { 155 int i; 156 for (i = 0; i < lines_cnt; i++) { 157 zed_log_msg(LOG_INFO, "%s", lines[i]); 158 } 159 } 160 161 /* 162 * Two stage replace on Linux 163 * since we get disk notifications 164 * we can wait for partitioned disk slice to show up! 165 * 166 * First stage tags the disk, initiates async partitioning, and returns 167 * Second stage finds the tag and proceeds to ZFS labeling/replace 168 * 169 * disk-add --> label-disk + tag-disk --> partition-add --> zpool_vdev_attach 170 * 171 * 1. physical match with no fs, no partition 172 * tag it top, partition disk 173 * 174 * 2. physical match again, see partition and tag 175 * 176 */ 177 178 /* 179 * The device associated with the given vdev (either by devid or physical path) 180 * has been added to the system. If 'isdisk' is set, then we only attempt a 181 * replacement if it's a whole disk. This also implies that we should label the 182 * disk first. 183 * 184 * First, we attempt to online the device (making sure to undo any spare 185 * operation when finished). If this succeeds, then we're done. If it fails, 186 * and the new state is VDEV_CANT_OPEN, it indicates that the device was opened, 187 * but that the label was not what we expected. If the 'autoreplace' property 188 * is enabled, then we relabel the disk (if specified), and attempt a 'zpool 189 * replace'. If the online is successful, but the new state is something else 190 * (REMOVED or FAULTED), it indicates that we're out of sync or in some sort of 191 * race, and we should avoid attempting to relabel the disk. 192 * 193 * Also can arrive here from a ESC_ZFS_VDEV_CHECK event 194 */ 195 static void 196 zfs_process_add(zpool_handle_t *zhp, nvlist_t *vdev, boolean_t labeled) 197 { 198 const char *path; 199 vdev_state_t newstate; 200 nvlist_t *nvroot, *newvd; 201 pendingdev_t *device; 202 uint64_t wholedisk = 0ULL; 203 uint64_t offline = 0ULL, faulted = 0ULL; 204 uint64_t guid = 0ULL; 205 uint64_t is_spare = 0; 206 const char *physpath = NULL, *new_devid = NULL, *enc_sysfs_path = NULL; 207 char rawpath[PATH_MAX], fullpath[PATH_MAX]; 208 char pathbuf[PATH_MAX]; 209 int ret; 210 int online_flag = ZFS_ONLINE_CHECKREMOVE | ZFS_ONLINE_UNSPARE; 211 boolean_t is_sd = B_FALSE; 212 boolean_t is_mpath_wholedisk = B_FALSE; 213 uint_t c; 214 vdev_stat_t *vs; 215 char **lines = NULL; 216 int lines_cnt = 0; 217 218 /* 219 * Get the persistent path, typically under the '/dev/disk/by-id' or 220 * '/dev/disk/by-vdev' directories. Note that this path can change 221 * when a vdev is replaced with a new disk. 222 */ 223 if (nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path) != 0) 224 return; 225 226 /* Skip healthy disks */ 227 verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_VDEV_STATS, 228 (uint64_t **)&vs, &c) == 0); 229 if (vs->vs_state == VDEV_STATE_HEALTHY) { 230 zed_log_msg(LOG_INFO, "%s: %s is already healthy, skip it.", 231 __func__, path); 232 return; 233 } 234 235 (void) nvlist_lookup_string(vdev, ZPOOL_CONFIG_PHYS_PATH, &physpath); 236 (void) nvlist_lookup_string(vdev, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH, 237 &enc_sysfs_path); 238 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK, &wholedisk); 239 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_OFFLINE, &offline); 240 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_FAULTED, &faulted); 241 242 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_GUID, &guid); 243 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_IS_SPARE, &is_spare); 244 245 /* 246 * Special case: 247 * 248 * We've seen times where a disk won't have a ZPOOL_CONFIG_PHYS_PATH 249 * entry in their config. For example, on this force-faulted disk: 250 * 251 * children[0]: 252 * type: 'disk' 253 * id: 0 254 * guid: 14309659774640089719 255 * path: '/dev/disk/by-vdev/L28' 256 * whole_disk: 0 257 * DTL: 654 258 * create_txg: 4 259 * com.delphix:vdev_zap_leaf: 1161 260 * faulted: 1 261 * aux_state: 'external' 262 * children[1]: 263 * type: 'disk' 264 * id: 1 265 * guid: 16002508084177980912 266 * path: '/dev/disk/by-vdev/L29' 267 * devid: 'dm-uuid-mpath-35000c500a61d68a3' 268 * phys_path: 'L29' 269 * vdev_enc_sysfs_path: '/sys/class/enclosure/0:0:1:0/SLOT 30 32' 270 * whole_disk: 0 271 * DTL: 1028 272 * create_txg: 4 273 * com.delphix:vdev_zap_leaf: 131 274 * 275 * If the disk's path is a /dev/disk/by-vdev/ path, then we can infer 276 * the ZPOOL_CONFIG_PHYS_PATH from the by-vdev disk name. 277 */ 278 if (physpath == NULL && path != NULL) { 279 /* If path begins with "/dev/disk/by-vdev/" ... */ 280 if (strncmp(path, DEV_BYVDEV_PATH, 281 strlen(DEV_BYVDEV_PATH)) == 0) { 282 /* Set physpath to the char after "/dev/disk/by-vdev" */ 283 physpath = &path[strlen(DEV_BYVDEV_PATH)]; 284 } 285 } 286 287 /* 288 * We don't want to autoreplace offlined disks. However, we do want to 289 * replace force-faulted disks (`zpool offline -f`). Force-faulted 290 * disks have both offline=1 and faulted=1 in the nvlist. 291 */ 292 if (offline && !faulted) { 293 zed_log_msg(LOG_INFO, "%s: %s is offline, skip autoreplace", 294 __func__, path); 295 return; 296 } 297 298 is_mpath_wholedisk = is_mpath_whole_disk(path); 299 zed_log_msg(LOG_INFO, "zfs_process_add: pool '%s' vdev '%s', phys '%s'" 300 " %s blank disk, %s mpath blank disk, %s labeled, enc sysfs '%s', " 301 "(guid %llu)", 302 zpool_get_name(zhp), path, 303 physpath ? physpath : "NULL", 304 wholedisk ? "is" : "not", 305 is_mpath_wholedisk? "is" : "not", 306 labeled ? "is" : "not", 307 enc_sysfs_path, 308 (long long unsigned int)guid); 309 310 /* 311 * The VDEV guid is preferred for identification (gets passed in path) 312 */ 313 if (guid != 0) { 314 (void) snprintf(fullpath, sizeof (fullpath), "%llu", 315 (long long unsigned int)guid); 316 } else { 317 /* 318 * otherwise use path sans partition suffix for whole disks 319 */ 320 (void) strlcpy(fullpath, path, sizeof (fullpath)); 321 if (wholedisk) { 322 char *spath = zfs_strip_partition(fullpath); 323 if (!spath) { 324 zed_log_msg(LOG_INFO, "%s: Can't alloc", 325 __func__); 326 return; 327 } 328 329 (void) strlcpy(fullpath, spath, sizeof (fullpath)); 330 free(spath); 331 } 332 } 333 334 if (is_spare) 335 online_flag |= ZFS_ONLINE_SPARE; 336 337 /* 338 * Attempt to online the device. 339 */ 340 if (zpool_vdev_online(zhp, fullpath, online_flag, &newstate) == 0 && 341 (newstate == VDEV_STATE_HEALTHY || 342 newstate == VDEV_STATE_DEGRADED)) { 343 zed_log_msg(LOG_INFO, 344 " zpool_vdev_online: vdev '%s' ('%s') is " 345 "%s", fullpath, physpath, (newstate == VDEV_STATE_HEALTHY) ? 346 "HEALTHY" : "DEGRADED"); 347 return; 348 } 349 350 /* 351 * vdev_id alias rule for using scsi_debug devices (FMA automated 352 * testing) 353 */ 354 if (physpath != NULL && strcmp("scsidebug", physpath) == 0) 355 is_sd = B_TRUE; 356 357 /* 358 * If the pool doesn't have the autoreplace property set, then use 359 * vdev online to trigger a FMA fault by posting an ereport. 360 */ 361 if (!zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOREPLACE, NULL) || 362 !(wholedisk || is_mpath_wholedisk) || (physpath == NULL)) { 363 (void) zpool_vdev_online(zhp, fullpath, ZFS_ONLINE_FORCEFAULT, 364 &newstate); 365 zed_log_msg(LOG_INFO, "Pool's autoreplace is not enabled or " 366 "not a blank disk for '%s' ('%s')", fullpath, 367 physpath); 368 return; 369 } 370 371 /* 372 * Convert physical path into its current device node. Rawpath 373 * needs to be /dev/disk/by-vdev for a scsi_debug device since 374 * /dev/disk/by-path will not be present. 375 */ 376 (void) snprintf(rawpath, sizeof (rawpath), "%s%s", 377 is_sd ? DEV_BYVDEV_PATH : DEV_BYPATH_PATH, physpath); 378 379 if (realpath(rawpath, pathbuf) == NULL && !is_mpath_wholedisk) { 380 zed_log_msg(LOG_INFO, " realpath: %s failed (%s)", 381 rawpath, strerror(errno)); 382 383 int err = zpool_vdev_online(zhp, fullpath, 384 ZFS_ONLINE_FORCEFAULT, &newstate); 385 386 zed_log_msg(LOG_INFO, " zpool_vdev_online: %s FORCEFAULT (%s) " 387 "err %d, new state %d", 388 fullpath, libzfs_error_description(g_zfshdl), err, 389 err ? (int)newstate : 0); 390 return; 391 } 392 393 /* Only autoreplace bad disks */ 394 if ((vs->vs_state != VDEV_STATE_DEGRADED) && 395 (vs->vs_state != VDEV_STATE_FAULTED) && 396 (vs->vs_state != VDEV_STATE_REMOVED) && 397 (vs->vs_state != VDEV_STATE_CANT_OPEN)) { 398 zed_log_msg(LOG_INFO, " not autoreplacing since disk isn't in " 399 "a bad state (currently %llu)", vs->vs_state); 400 return; 401 } 402 403 nvlist_lookup_string(vdev, "new_devid", &new_devid); 404 405 if (is_mpath_wholedisk) { 406 /* Don't label device mapper or multipath disks. */ 407 zed_log_msg(LOG_INFO, 408 " it's a multipath wholedisk, don't label"); 409 if (zpool_prepare_disk(zhp, vdev, "autoreplace", &lines, 410 &lines_cnt) != 0) { 411 zed_log_msg(LOG_INFO, 412 " zpool_prepare_disk: could not " 413 "prepare '%s' (%s)", fullpath, 414 libzfs_error_description(g_zfshdl)); 415 if (lines_cnt > 0) { 416 zed_log_msg(LOG_INFO, 417 " zfs_prepare_disk output:"); 418 lines_to_zed_log_msg(lines, lines_cnt); 419 } 420 libzfs_free_str_array(lines, lines_cnt); 421 return; 422 } 423 } else if (!labeled) { 424 /* 425 * we're auto-replacing a raw disk, so label it first 426 */ 427 char *leafname; 428 429 /* 430 * If this is a request to label a whole disk, then attempt to 431 * write out the label. Before we can label the disk, we need 432 * to map the physical string that was matched on to the under 433 * lying device node. 434 * 435 * If any part of this process fails, then do a force online 436 * to trigger a ZFS fault for the device (and any hot spare 437 * replacement). 438 */ 439 leafname = strrchr(pathbuf, '/') + 1; 440 441 /* 442 * If this is a request to label a whole disk, then attempt to 443 * write out the label. 444 */ 445 if (zpool_prepare_and_label_disk(g_zfshdl, zhp, leafname, 446 vdev, "autoreplace", &lines, &lines_cnt) != 0) { 447 zed_log_msg(LOG_WARNING, 448 " zpool_prepare_and_label_disk: could not " 449 "label '%s' (%s)", leafname, 450 libzfs_error_description(g_zfshdl)); 451 if (lines_cnt > 0) { 452 zed_log_msg(LOG_INFO, 453 " zfs_prepare_disk output:"); 454 lines_to_zed_log_msg(lines, lines_cnt); 455 } 456 libzfs_free_str_array(lines, lines_cnt); 457 458 (void) zpool_vdev_online(zhp, fullpath, 459 ZFS_ONLINE_FORCEFAULT, &newstate); 460 return; 461 } 462 463 /* 464 * The disk labeling is asynchronous on Linux. Just record 465 * this label request and return as there will be another 466 * disk add event for the partition after the labeling is 467 * completed. 468 */ 469 device = malloc(sizeof (pendingdev_t)); 470 if (device == NULL) { 471 perror("malloc"); 472 exit(EXIT_FAILURE); 473 } 474 475 (void) strlcpy(device->pd_physpath, physpath, 476 sizeof (device->pd_physpath)); 477 list_insert_tail(&g_device_list, device); 478 479 zed_log_msg(LOG_NOTICE, " zpool_label_disk: async '%s' (%llu)", 480 leafname, (u_longlong_t)guid); 481 482 return; /* resumes at EC_DEV_ADD.ESC_DISK for partition */ 483 484 } else /* labeled */ { 485 boolean_t found = B_FALSE; 486 /* 487 * match up with request above to label the disk 488 */ 489 for (device = list_head(&g_device_list); device != NULL; 490 device = list_next(&g_device_list, device)) { 491 if (strcmp(physpath, device->pd_physpath) == 0) { 492 list_remove(&g_device_list, device); 493 free(device); 494 found = B_TRUE; 495 break; 496 } 497 zed_log_msg(LOG_INFO, "zpool_label_disk: %s != %s", 498 physpath, device->pd_physpath); 499 } 500 if (!found) { 501 /* unexpected partition slice encountered */ 502 zed_log_msg(LOG_WARNING, "labeled disk %s was " 503 "unexpected here", fullpath); 504 (void) zpool_vdev_online(zhp, fullpath, 505 ZFS_ONLINE_FORCEFAULT, &newstate); 506 return; 507 } 508 509 zed_log_msg(LOG_INFO, " zpool_label_disk: resume '%s' (%llu)", 510 physpath, (u_longlong_t)guid); 511 512 /* 513 * Paths that begin with '/dev/disk/by-id/' will change and so 514 * they must be updated before calling zpool_vdev_attach(). 515 */ 516 if (strncmp(path, DEV_BYID_PATH, strlen(DEV_BYID_PATH)) == 0) { 517 (void) snprintf(pathbuf, sizeof (pathbuf), "%s%s", 518 DEV_BYID_PATH, new_devid); 519 zed_log_msg(LOG_INFO, " zpool_label_disk: path '%s' " 520 "replaced by '%s'", path, pathbuf); 521 path = pathbuf; 522 } 523 } 524 525 libzfs_free_str_array(lines, lines_cnt); 526 527 /* 528 * Construct the root vdev to pass to zpool_vdev_attach(). While adding 529 * the entire vdev structure is harmless, we construct a reduced set of 530 * path/physpath/wholedisk to keep it simple. 531 */ 532 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) { 533 zed_log_msg(LOG_WARNING, "zfs_mod: nvlist_alloc out of memory"); 534 return; 535 } 536 if (nvlist_alloc(&newvd, NV_UNIQUE_NAME, 0) != 0) { 537 zed_log_msg(LOG_WARNING, "zfs_mod: nvlist_alloc out of memory"); 538 nvlist_free(nvroot); 539 return; 540 } 541 542 if (nvlist_add_string(newvd, ZPOOL_CONFIG_TYPE, VDEV_TYPE_DISK) != 0 || 543 nvlist_add_string(newvd, ZPOOL_CONFIG_PATH, path) != 0 || 544 nvlist_add_string(newvd, ZPOOL_CONFIG_DEVID, new_devid) != 0 || 545 (physpath != NULL && nvlist_add_string(newvd, 546 ZPOOL_CONFIG_PHYS_PATH, physpath) != 0) || 547 (enc_sysfs_path != NULL && nvlist_add_string(newvd, 548 ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH, enc_sysfs_path) != 0) || 549 nvlist_add_uint64(newvd, ZPOOL_CONFIG_WHOLE_DISK, wholedisk) != 0 || 550 nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) != 0 || 551 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 552 (const nvlist_t **)&newvd, 1) != 0) { 553 zed_log_msg(LOG_WARNING, "zfs_mod: unable to add nvlist pairs"); 554 nvlist_free(newvd); 555 nvlist_free(nvroot); 556 return; 557 } 558 559 nvlist_free(newvd); 560 561 /* 562 * Wait for udev to verify the links exist, then auto-replace 563 * the leaf disk at same physical location. 564 */ 565 if (zpool_label_disk_wait(path, DISK_LABEL_WAIT) != 0) { 566 zed_log_msg(LOG_WARNING, "zfs_mod: pool '%s', after labeling " 567 "replacement disk, the expected disk partition link '%s' " 568 "is missing after waiting %u ms", 569 zpool_get_name(zhp), path, DISK_LABEL_WAIT); 570 nvlist_free(nvroot); 571 return; 572 } 573 574 /* 575 * Prefer sequential resilvering when supported (mirrors and dRAID), 576 * otherwise fallback to a traditional healing resilver. 577 */ 578 ret = zpool_vdev_attach(zhp, fullpath, path, nvroot, B_TRUE, B_TRUE); 579 if (ret != 0) { 580 ret = zpool_vdev_attach(zhp, fullpath, path, nvroot, 581 B_TRUE, B_FALSE); 582 } 583 584 zed_log_msg(LOG_WARNING, " zpool_vdev_replace: %s with %s (%s)", 585 fullpath, path, (ret == 0) ? "no errors" : 586 libzfs_error_description(g_zfshdl)); 587 588 nvlist_free(nvroot); 589 } 590 591 /* 592 * Utility functions to find a vdev matching given criteria. 593 */ 594 typedef struct dev_data { 595 const char *dd_compare; 596 const char *dd_prop; 597 zfs_process_func_t dd_func; 598 boolean_t dd_found; 599 boolean_t dd_islabeled; 600 uint64_t dd_pool_guid; 601 uint64_t dd_vdev_guid; 602 uint64_t dd_new_vdev_guid; 603 const char *dd_new_devid; 604 uint64_t dd_num_spares; 605 } dev_data_t; 606 607 static void 608 zfs_iter_vdev(zpool_handle_t *zhp, nvlist_t *nvl, void *data) 609 { 610 dev_data_t *dp = data; 611 const char *path = NULL; 612 uint_t c, children; 613 nvlist_t **child; 614 uint64_t guid = 0; 615 uint64_t isspare = 0; 616 617 /* 618 * First iterate over any children. 619 */ 620 if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, 621 &child, &children) == 0) { 622 for (c = 0; c < children; c++) 623 zfs_iter_vdev(zhp, child[c], data); 624 } 625 626 /* 627 * Iterate over any spares and cache devices 628 */ 629 if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_SPARES, 630 &child, &children) == 0) { 631 for (c = 0; c < children; c++) 632 zfs_iter_vdev(zhp, child[c], data); 633 } 634 if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_L2CACHE, 635 &child, &children) == 0) { 636 for (c = 0; c < children; c++) 637 zfs_iter_vdev(zhp, child[c], data); 638 } 639 640 /* once a vdev was matched and processed there is nothing left to do */ 641 if (dp->dd_found && dp->dd_num_spares == 0) 642 return; 643 (void) nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_GUID, &guid); 644 645 /* 646 * Match by GUID if available otherwise fallback to devid or physical 647 */ 648 if (dp->dd_vdev_guid != 0) { 649 if (guid != dp->dd_vdev_guid) 650 return; 651 zed_log_msg(LOG_INFO, " zfs_iter_vdev: matched on %llu", guid); 652 dp->dd_found = B_TRUE; 653 654 } else if (dp->dd_compare != NULL) { 655 /* 656 * NOTE: On Linux there is an event for partition, so unlike 657 * illumos, substring matching is not required to accommodate 658 * the partition suffix. An exact match will be present in 659 * the dp->dd_compare value. 660 * If the attached disk already contains a vdev GUID, it means 661 * the disk is not clean. In such a scenario, the physical path 662 * would be a match that makes the disk faulted when trying to 663 * online it. So, we would only want to proceed if either GUID 664 * matches with the last attached disk or the disk is in clean 665 * state. 666 */ 667 if (nvlist_lookup_string(nvl, dp->dd_prop, &path) != 0 || 668 strcmp(dp->dd_compare, path) != 0) { 669 return; 670 } 671 if (dp->dd_new_vdev_guid != 0 && dp->dd_new_vdev_guid != guid) { 672 zed_log_msg(LOG_INFO, " %s: no match (GUID:%llu" 673 " != vdev GUID:%llu)", __func__, 674 dp->dd_new_vdev_guid, guid); 675 return; 676 } 677 678 zed_log_msg(LOG_INFO, " zfs_iter_vdev: matched %s on %s", 679 dp->dd_prop, path); 680 dp->dd_found = B_TRUE; 681 682 /* pass the new devid for use by auto-replacing code */ 683 if (dp->dd_new_devid != NULL) { 684 (void) nvlist_add_string(nvl, "new_devid", 685 dp->dd_new_devid); 686 } 687 } 688 689 if (dp->dd_found == B_TRUE && nvlist_lookup_uint64(nvl, 690 ZPOOL_CONFIG_IS_SPARE, &isspare) == 0 && isspare) 691 dp->dd_num_spares++; 692 693 (dp->dd_func)(zhp, nvl, dp->dd_islabeled); 694 } 695 696 static void 697 zfs_enable_ds(void *arg) 698 { 699 unavailpool_t *pool = (unavailpool_t *)arg; 700 701 (void) zpool_enable_datasets(pool->uap_zhp, NULL, 0); 702 zpool_close(pool->uap_zhp); 703 free(pool); 704 } 705 706 static int 707 zfs_iter_pool(zpool_handle_t *zhp, void *data) 708 { 709 nvlist_t *config, *nvl; 710 dev_data_t *dp = data; 711 uint64_t pool_guid; 712 unavailpool_t *pool; 713 714 zed_log_msg(LOG_INFO, "zfs_iter_pool: evaluating vdevs on %s (by %s)", 715 zpool_get_name(zhp), dp->dd_vdev_guid ? "GUID" : dp->dd_prop); 716 717 /* 718 * For each vdev in this pool, look for a match to apply dd_func 719 */ 720 if ((config = zpool_get_config(zhp, NULL)) != NULL) { 721 if (dp->dd_pool_guid == 0 || 722 (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, 723 &pool_guid) == 0 && pool_guid == dp->dd_pool_guid)) { 724 (void) nvlist_lookup_nvlist(config, 725 ZPOOL_CONFIG_VDEV_TREE, &nvl); 726 zfs_iter_vdev(zhp, nvl, data); 727 } 728 } else { 729 zed_log_msg(LOG_INFO, "%s: no config\n", __func__); 730 } 731 732 /* 733 * if this pool was originally unavailable, 734 * then enable its datasets asynchronously 735 */ 736 if (g_enumeration_done) { 737 for (pool = list_head(&g_pool_list); pool != NULL; 738 pool = list_next(&g_pool_list, pool)) { 739 740 if (strcmp(zpool_get_name(zhp), 741 zpool_get_name(pool->uap_zhp))) 742 continue; 743 if (zfs_toplevel_state(zhp) >= VDEV_STATE_DEGRADED) { 744 list_remove(&g_pool_list, pool); 745 (void) tpool_dispatch(g_tpool, zfs_enable_ds, 746 pool); 747 break; 748 } 749 } 750 } 751 752 zpool_close(zhp); 753 754 /* cease iteration after a match */ 755 return (dp->dd_found && dp->dd_num_spares == 0); 756 } 757 758 /* 759 * Given a physical device location, iterate over all 760 * (pool, vdev) pairs which correspond to that location. 761 */ 762 static boolean_t 763 devphys_iter(const char *physical, const char *devid, zfs_process_func_t func, 764 boolean_t is_slice, uint64_t new_vdev_guid) 765 { 766 dev_data_t data = { 0 }; 767 768 data.dd_compare = physical; 769 data.dd_func = func; 770 data.dd_prop = ZPOOL_CONFIG_PHYS_PATH; 771 data.dd_found = B_FALSE; 772 data.dd_islabeled = is_slice; 773 data.dd_new_devid = devid; /* used by auto replace code */ 774 data.dd_new_vdev_guid = new_vdev_guid; 775 776 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data); 777 778 return (data.dd_found); 779 } 780 781 /* 782 * Given a device identifier, find any vdevs with a matching by-vdev 783 * path. Normally we shouldn't need this as the comparison would be 784 * made earlier in the devphys_iter(). For example, if we were replacing 785 * /dev/disk/by-vdev/L28, normally devphys_iter() would match the 786 * ZPOOL_CONFIG_PHYS_PATH of "L28" from the old disk config to "L28" 787 * of the new disk config. However, we've seen cases where 788 * ZPOOL_CONFIG_PHYS_PATH was not in the config for the old disk. Here's 789 * an example of a real 2-disk mirror pool where one disk was force 790 * faulted: 791 * 792 * com.delphix:vdev_zap_top: 129 793 * children[0]: 794 * type: 'disk' 795 * id: 0 796 * guid: 14309659774640089719 797 * path: '/dev/disk/by-vdev/L28' 798 * whole_disk: 0 799 * DTL: 654 800 * create_txg: 4 801 * com.delphix:vdev_zap_leaf: 1161 802 * faulted: 1 803 * aux_state: 'external' 804 * children[1]: 805 * type: 'disk' 806 * id: 1 807 * guid: 16002508084177980912 808 * path: '/dev/disk/by-vdev/L29' 809 * devid: 'dm-uuid-mpath-35000c500a61d68a3' 810 * phys_path: 'L29' 811 * vdev_enc_sysfs_path: '/sys/class/enclosure/0:0:1:0/SLOT 30 32' 812 * whole_disk: 0 813 * DTL: 1028 814 * create_txg: 4 815 * com.delphix:vdev_zap_leaf: 131 816 * 817 * So in the case above, the only thing we could compare is the path. 818 * 819 * We can do this because we assume by-vdev paths are authoritative as physical 820 * paths. We could not assume this for normal paths like /dev/sda since the 821 * physical location /dev/sda points to could change over time. 822 */ 823 static boolean_t 824 by_vdev_path_iter(const char *by_vdev_path, const char *devid, 825 zfs_process_func_t func, boolean_t is_slice) 826 { 827 dev_data_t data = { 0 }; 828 829 data.dd_compare = by_vdev_path; 830 data.dd_func = func; 831 data.dd_prop = ZPOOL_CONFIG_PATH; 832 data.dd_found = B_FALSE; 833 data.dd_islabeled = is_slice; 834 data.dd_new_devid = devid; 835 836 if (strncmp(by_vdev_path, DEV_BYVDEV_PATH, 837 strlen(DEV_BYVDEV_PATH)) != 0) { 838 /* by_vdev_path doesn't start with "/dev/disk/by-vdev/" */ 839 return (B_FALSE); 840 } 841 842 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data); 843 844 return (data.dd_found); 845 } 846 847 /* 848 * Given a device identifier, find any vdevs with a matching devid. 849 * On Linux we can match devid directly which is always a whole disk. 850 */ 851 static boolean_t 852 devid_iter(const char *devid, zfs_process_func_t func, boolean_t is_slice) 853 { 854 dev_data_t data = { 0 }; 855 856 data.dd_compare = devid; 857 data.dd_func = func; 858 data.dd_prop = ZPOOL_CONFIG_DEVID; 859 data.dd_found = B_FALSE; 860 data.dd_islabeled = is_slice; 861 data.dd_new_devid = devid; 862 863 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data); 864 865 return (data.dd_found); 866 } 867 868 /* 869 * Given a device guid, find any vdevs with a matching guid. 870 */ 871 static boolean_t 872 guid_iter(uint64_t pool_guid, uint64_t vdev_guid, const char *devid, 873 zfs_process_func_t func, boolean_t is_slice) 874 { 875 dev_data_t data = { 0 }; 876 877 data.dd_func = func; 878 data.dd_found = B_FALSE; 879 data.dd_pool_guid = pool_guid; 880 data.dd_vdev_guid = vdev_guid; 881 data.dd_islabeled = is_slice; 882 data.dd_new_devid = devid; 883 884 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data); 885 886 return (data.dd_found); 887 } 888 889 /* 890 * Handle a EC_DEV_ADD.ESC_DISK event. 891 * 892 * illumos 893 * Expects: DEV_PHYS_PATH string in schema 894 * Matches: vdev's ZPOOL_CONFIG_PHYS_PATH or ZPOOL_CONFIG_DEVID 895 * 896 * path: '/dev/dsk/c0t1d0s0' (persistent) 897 * devid: 'id1,sd@SATA_____Hitachi_HDS72101______JP2940HZ3H74MC/a' 898 * phys_path: '/pci@0,0/pci103c,1609@11/disk@1,0:a' 899 * 900 * linux 901 * provides: DEV_PHYS_PATH and DEV_IDENTIFIER strings in schema 902 * Matches: vdev's ZPOOL_CONFIG_PHYS_PATH or ZPOOL_CONFIG_DEVID 903 * 904 * path: '/dev/sdc1' (not persistent) 905 * devid: 'ata-SAMSUNG_HD204UI_S2HGJD2Z805891-part1' 906 * phys_path: 'pci-0000:04:00.0-sas-0x4433221106000000-lun-0' 907 */ 908 static int 909 zfs_deliver_add(nvlist_t *nvl) 910 { 911 const char *devpath = NULL, *devid = NULL; 912 uint64_t pool_guid = 0, vdev_guid = 0; 913 boolean_t is_slice; 914 915 /* 916 * Expecting a devid string and an optional physical location and guid 917 */ 918 if (nvlist_lookup_string(nvl, DEV_IDENTIFIER, &devid) != 0) { 919 zed_log_msg(LOG_INFO, "%s: no dev identifier\n", __func__); 920 return (-1); 921 } 922 923 (void) nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devpath); 924 (void) nvlist_lookup_uint64(nvl, ZFS_EV_POOL_GUID, &pool_guid); 925 (void) nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID, &vdev_guid); 926 927 is_slice = (nvlist_lookup_boolean(nvl, DEV_IS_PART) == 0); 928 929 zed_log_msg(LOG_INFO, "zfs_deliver_add: adding %s (%s) (is_slice %d)", 930 devid, devpath ? devpath : "NULL", is_slice); 931 932 /* 933 * Iterate over all vdevs looking for a match in the following order: 934 * 1. ZPOOL_CONFIG_DEVID (identifies the unique disk) 935 * 2. ZPOOL_CONFIG_PHYS_PATH (identifies disk physical location). 936 * 3. ZPOOL_CONFIG_GUID (identifies unique vdev). 937 * 4. ZPOOL_CONFIG_PATH for /dev/disk/by-vdev devices only (since 938 * by-vdev paths represent physical paths). 939 */ 940 if (devid_iter(devid, zfs_process_add, is_slice)) 941 return (0); 942 if (devpath != NULL && devphys_iter(devpath, devid, zfs_process_add, 943 is_slice, vdev_guid)) 944 return (0); 945 if (vdev_guid != 0) 946 (void) guid_iter(pool_guid, vdev_guid, devid, zfs_process_add, 947 is_slice); 948 949 if (devpath != NULL) { 950 /* Can we match a /dev/disk/by-vdev/ path? */ 951 char by_vdev_path[MAXPATHLEN]; 952 snprintf(by_vdev_path, sizeof (by_vdev_path), 953 "/dev/disk/by-vdev/%s", devpath); 954 if (by_vdev_path_iter(by_vdev_path, devid, zfs_process_add, 955 is_slice)) 956 return (0); 957 } 958 959 return (0); 960 } 961 962 /* 963 * Called when we receive a VDEV_CHECK event, which indicates a device could not 964 * be opened during initial pool open, but the autoreplace property was set on 965 * the pool. In this case, we treat it as if it were an add event. 966 */ 967 static int 968 zfs_deliver_check(nvlist_t *nvl) 969 { 970 dev_data_t data = { 0 }; 971 972 if (nvlist_lookup_uint64(nvl, ZFS_EV_POOL_GUID, 973 &data.dd_pool_guid) != 0 || 974 nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID, 975 &data.dd_vdev_guid) != 0 || 976 data.dd_vdev_guid == 0) 977 return (0); 978 979 zed_log_msg(LOG_INFO, "zfs_deliver_check: pool '%llu', vdev %llu", 980 data.dd_pool_guid, data.dd_vdev_guid); 981 982 data.dd_func = zfs_process_add; 983 984 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data); 985 986 return (0); 987 } 988 989 /* 990 * Given a path to a vdev, lookup the vdev's physical size from its 991 * config nvlist. 992 * 993 * Returns the vdev's physical size in bytes on success, 0 on error. 994 */ 995 static uint64_t 996 vdev_size_from_config(zpool_handle_t *zhp, const char *vdev_path) 997 { 998 nvlist_t *nvl = NULL; 999 boolean_t avail_spare, l2cache, log; 1000 vdev_stat_t *vs = NULL; 1001 uint_t c; 1002 1003 nvl = zpool_find_vdev(zhp, vdev_path, &avail_spare, &l2cache, &log); 1004 if (!nvl) 1005 return (0); 1006 1007 verify(nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_VDEV_STATS, 1008 (uint64_t **)&vs, &c) == 0); 1009 if (!vs) { 1010 zed_log_msg(LOG_INFO, "%s: no nvlist for '%s'", __func__, 1011 vdev_path); 1012 return (0); 1013 } 1014 1015 return (vs->vs_pspace); 1016 } 1017 1018 /* 1019 * Given a path to a vdev, lookup if the vdev is a "whole disk" in the 1020 * config nvlist. "whole disk" means that ZFS was passed a whole disk 1021 * at pool creation time, which it partitioned up and has full control over. 1022 * Thus a partition with wholedisk=1 set tells us that zfs created the 1023 * partition at creation time. A partition without whole disk set would have 1024 * been created by externally (like with fdisk) and passed to ZFS. 1025 * 1026 * Returns the whole disk value (either 0 or 1). 1027 */ 1028 static uint64_t 1029 vdev_whole_disk_from_config(zpool_handle_t *zhp, const char *vdev_path) 1030 { 1031 nvlist_t *nvl = NULL; 1032 boolean_t avail_spare, l2cache, log; 1033 uint64_t wholedisk = 0; 1034 1035 nvl = zpool_find_vdev(zhp, vdev_path, &avail_spare, &l2cache, &log); 1036 if (!nvl) 1037 return (0); 1038 1039 (void) nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_WHOLE_DISK, &wholedisk); 1040 1041 return (wholedisk); 1042 } 1043 1044 /* 1045 * If the device size grew more than 1% then return true. 1046 */ 1047 #define DEVICE_GREW(oldsize, newsize) \ 1048 ((newsize > oldsize) && \ 1049 ((newsize / (newsize - oldsize)) <= 100)) 1050 1051 static int 1052 zfsdle_vdev_online(zpool_handle_t *zhp, void *data) 1053 { 1054 boolean_t avail_spare, l2cache; 1055 nvlist_t *udev_nvl = data; 1056 nvlist_t *tgt; 1057 int error; 1058 1059 const char *tmp_devname; 1060 char devname[MAXPATHLEN] = ""; 1061 uint64_t guid; 1062 1063 if (nvlist_lookup_uint64(udev_nvl, ZFS_EV_VDEV_GUID, &guid) == 0) { 1064 sprintf(devname, "%llu", (u_longlong_t)guid); 1065 } else if (nvlist_lookup_string(udev_nvl, DEV_PHYS_PATH, 1066 &tmp_devname) == 0) { 1067 strlcpy(devname, tmp_devname, MAXPATHLEN); 1068 zfs_append_partition(devname, MAXPATHLEN); 1069 } else { 1070 zed_log_msg(LOG_INFO, "%s: no guid or physpath", __func__); 1071 } 1072 1073 zed_log_msg(LOG_INFO, "zfsdle_vdev_online: searching for '%s' in '%s'", 1074 devname, zpool_get_name(zhp)); 1075 1076 if ((tgt = zpool_find_vdev_by_physpath(zhp, devname, 1077 &avail_spare, &l2cache, NULL)) != NULL) { 1078 const char *path; 1079 char fullpath[MAXPATHLEN]; 1080 uint64_t wholedisk = 0; 1081 1082 error = nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &path); 1083 if (error) { 1084 zpool_close(zhp); 1085 return (0); 1086 } 1087 1088 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK, 1089 &wholedisk); 1090 1091 if (wholedisk) { 1092 char *tmp; 1093 path = strrchr(path, '/'); 1094 if (path != NULL) { 1095 tmp = zfs_strip_partition(path + 1); 1096 if (tmp == NULL) { 1097 zpool_close(zhp); 1098 return (0); 1099 } 1100 } else { 1101 zpool_close(zhp); 1102 return (0); 1103 } 1104 1105 (void) strlcpy(fullpath, tmp, sizeof (fullpath)); 1106 free(tmp); 1107 1108 /* 1109 * We need to reopen the pool associated with this 1110 * device so that the kernel can update the size of 1111 * the expanded device. When expanding there is no 1112 * need to restart the scrub from the beginning. 1113 */ 1114 boolean_t scrub_restart = B_FALSE; 1115 (void) zpool_reopen_one(zhp, &scrub_restart); 1116 } else { 1117 (void) strlcpy(fullpath, path, sizeof (fullpath)); 1118 } 1119 1120 if (zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) { 1121 vdev_state_t newstate; 1122 1123 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) { 1124 /* 1125 * If this disk size has not changed, then 1126 * there's no need to do an autoexpand. To 1127 * check we look at the disk's size in its 1128 * config, and compare it to the disk size 1129 * that udev is reporting. 1130 */ 1131 uint64_t udev_size = 0, conf_size = 0, 1132 wholedisk = 0, udev_parent_size = 0; 1133 1134 /* 1135 * Get the size of our disk that udev is 1136 * reporting. 1137 */ 1138 if (nvlist_lookup_uint64(udev_nvl, DEV_SIZE, 1139 &udev_size) != 0) { 1140 udev_size = 0; 1141 } 1142 1143 /* 1144 * Get the size of our disk's parent device 1145 * from udev (where sda1's parent is sda). 1146 */ 1147 if (nvlist_lookup_uint64(udev_nvl, 1148 DEV_PARENT_SIZE, &udev_parent_size) != 0) { 1149 udev_parent_size = 0; 1150 } 1151 1152 conf_size = vdev_size_from_config(zhp, 1153 fullpath); 1154 1155 wholedisk = vdev_whole_disk_from_config(zhp, 1156 fullpath); 1157 1158 /* 1159 * Only attempt an autoexpand if the vdev size 1160 * changed. There are two different cases 1161 * to consider. 1162 * 1163 * 1. wholedisk=1 1164 * If you do a 'zpool create' on a whole disk 1165 * (like /dev/sda), then zfs will create 1166 * partitions on the disk (like /dev/sda1). In 1167 * that case, wholedisk=1 will be set in the 1168 * partition's nvlist config. So zed will need 1169 * to see if your parent device (/dev/sda) 1170 * expanded in size, and if so, then attempt 1171 * the autoexpand. 1172 * 1173 * 2. wholedisk=0 1174 * If you do a 'zpool create' on an existing 1175 * partition, or a device that doesn't allow 1176 * partitions, then wholedisk=0, and you will 1177 * simply need to check if the device itself 1178 * expanded in size. 1179 */ 1180 if (DEVICE_GREW(conf_size, udev_size) || 1181 (wholedisk && DEVICE_GREW(conf_size, 1182 udev_parent_size))) { 1183 error = zpool_vdev_online(zhp, fullpath, 1184 0, &newstate); 1185 1186 zed_log_msg(LOG_INFO, 1187 "%s: autoexpanding '%s' from %llu" 1188 " to %llu bytes in pool '%s': %d", 1189 __func__, fullpath, conf_size, 1190 MAX(udev_size, udev_parent_size), 1191 zpool_get_name(zhp), error); 1192 } 1193 } 1194 } 1195 zpool_close(zhp); 1196 return (1); 1197 } 1198 zpool_close(zhp); 1199 return (0); 1200 } 1201 1202 /* 1203 * This function handles the ESC_DEV_DLE device change event. Use the 1204 * provided vdev guid when looking up a disk or partition, when the guid 1205 * is not present assume the entire disk is owned by ZFS and append the 1206 * expected -part1 partition information then lookup by physical path. 1207 */ 1208 static int 1209 zfs_deliver_dle(nvlist_t *nvl) 1210 { 1211 const char *devname; 1212 char name[MAXPATHLEN]; 1213 uint64_t guid; 1214 1215 if (nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID, &guid) == 0) { 1216 sprintf(name, "%llu", (u_longlong_t)guid); 1217 } else if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devname) == 0) { 1218 strlcpy(name, devname, MAXPATHLEN); 1219 zfs_append_partition(name, MAXPATHLEN); 1220 } else { 1221 sprintf(name, "unknown"); 1222 zed_log_msg(LOG_INFO, "zfs_deliver_dle: no guid or physpath"); 1223 } 1224 1225 if (zpool_iter(g_zfshdl, zfsdle_vdev_online, nvl) != 1) { 1226 zed_log_msg(LOG_INFO, "zfs_deliver_dle: device '%s' not " 1227 "found", name); 1228 return (1); 1229 } 1230 1231 return (0); 1232 } 1233 1234 /* 1235 * syseventd daemon module event handler 1236 * 1237 * Handles syseventd daemon zfs device related events: 1238 * 1239 * EC_DEV_ADD.ESC_DISK 1240 * EC_DEV_STATUS.ESC_DEV_DLE 1241 * EC_ZFS.ESC_ZFS_VDEV_CHECK 1242 * 1243 * Note: assumes only one thread active at a time (not thread safe) 1244 */ 1245 static int 1246 zfs_slm_deliver_event(const char *class, const char *subclass, nvlist_t *nvl) 1247 { 1248 int ret; 1249 boolean_t is_check = B_FALSE, is_dle = B_FALSE; 1250 1251 if (strcmp(class, EC_DEV_ADD) == 0) { 1252 /* 1253 * We're mainly interested in disk additions, but we also listen 1254 * for new loop devices, to allow for simplified testing. 1255 */ 1256 if (strcmp(subclass, ESC_DISK) != 0 && 1257 strcmp(subclass, ESC_LOFI) != 0) 1258 return (0); 1259 1260 is_check = B_FALSE; 1261 } else if (strcmp(class, EC_ZFS) == 0 && 1262 strcmp(subclass, ESC_ZFS_VDEV_CHECK) == 0) { 1263 /* 1264 * This event signifies that a device failed to open 1265 * during pool load, but the 'autoreplace' property was 1266 * set, so we should pretend it's just been added. 1267 */ 1268 is_check = B_TRUE; 1269 } else if (strcmp(class, EC_DEV_STATUS) == 0 && 1270 strcmp(subclass, ESC_DEV_DLE) == 0) { 1271 is_dle = B_TRUE; 1272 } else { 1273 return (0); 1274 } 1275 1276 if (is_dle) 1277 ret = zfs_deliver_dle(nvl); 1278 else if (is_check) 1279 ret = zfs_deliver_check(nvl); 1280 else 1281 ret = zfs_deliver_add(nvl); 1282 1283 return (ret); 1284 } 1285 1286 static void * 1287 zfs_enum_pools(void *arg) 1288 { 1289 (void) arg; 1290 1291 (void) zpool_iter(g_zfshdl, zfs_unavail_pool, (void *)&g_pool_list); 1292 /* 1293 * Linux - instead of using a thread pool, each list entry 1294 * will spawn a thread when an unavailable pool transitions 1295 * to available. zfs_slm_fini will wait for these threads. 1296 */ 1297 g_enumeration_done = B_TRUE; 1298 return (NULL); 1299 } 1300 1301 /* 1302 * called from zed daemon at startup 1303 * 1304 * sent messages from zevents or udev monitor 1305 * 1306 * For now, each agent has its own libzfs instance 1307 */ 1308 int 1309 zfs_slm_init(void) 1310 { 1311 if ((g_zfshdl = libzfs_init()) == NULL) 1312 return (-1); 1313 1314 /* 1315 * collect a list of unavailable pools (asynchronously, 1316 * since this can take a while) 1317 */ 1318 list_create(&g_pool_list, sizeof (struct unavailpool), 1319 offsetof(struct unavailpool, uap_node)); 1320 1321 if (pthread_create(&g_zfs_tid, NULL, zfs_enum_pools, NULL) != 0) { 1322 list_destroy(&g_pool_list); 1323 libzfs_fini(g_zfshdl); 1324 return (-1); 1325 } 1326 1327 pthread_setname_np(g_zfs_tid, "enum-pools"); 1328 list_create(&g_device_list, sizeof (struct pendingdev), 1329 offsetof(struct pendingdev, pd_node)); 1330 1331 return (0); 1332 } 1333 1334 void 1335 zfs_slm_fini(void) 1336 { 1337 unavailpool_t *pool; 1338 pendingdev_t *device; 1339 1340 /* wait for zfs_enum_pools thread to complete */ 1341 (void) pthread_join(g_zfs_tid, NULL); 1342 /* destroy the thread pool */ 1343 if (g_tpool != NULL) { 1344 tpool_wait(g_tpool); 1345 tpool_destroy(g_tpool); 1346 } 1347 1348 while ((pool = list_remove_head(&g_pool_list)) != NULL) { 1349 zpool_close(pool->uap_zhp); 1350 free(pool); 1351 } 1352 list_destroy(&g_pool_list); 1353 1354 while ((device = list_remove_head(&g_device_list)) != NULL) 1355 free(device); 1356 list_destroy(&g_device_list); 1357 1358 libzfs_fini(g_zfshdl); 1359 } 1360 1361 void 1362 zfs_slm_event(const char *class, const char *subclass, nvlist_t *nvl) 1363 { 1364 zed_log_msg(LOG_INFO, "zfs_slm_event: %s.%s", class, subclass); 1365 (void) zfs_slm_deliver_event(class, subclass, nvl); 1366 } 1367