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.
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.
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]
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.
30 * ZFS syseventd module.
32 * file origin: openzfs/usr/src/cmd/syseventd/modules/zfs_mod/zfs_mod.c
34 * The purpose of this module is to identify when devices are added to the
35 * system, and appropriately online or replace the affected vdevs.
37 * When a device is added to the system:
39 * 1. Search for any vdevs whose devid matches that of the newly added
42 * 2. If no vdevs are found, then search for any vdevs whose udev path
43 * matches that of the new device.
45 * 3. If no vdevs match by either method, then ignore the event.
47 * 4. Attempt to online the device with a flag to indicate that it should
48 * be unspared when resilvering completes. If this succeeds, then the
49 * same device was inserted and we should continue normally.
51 * 5. If the pool does not have the 'autoreplace' property set, attempt to
52 * online the device again without the unspare flag, which will
53 * generate a FMA fault.
55 * 6. If the pool has the 'autoreplace' property set, and the matching vdev
56 * is a whole disk, then label the new disk and attempt a 'zpool
59 * The module responds to EC_DEV_ADD events. The special ESC_ZFS_VDEV_CHECK
60 * event indicates that a device failed to open during pool load, but the
61 * autoreplace property was set. In this case, we deferred the associated
62 * FMA fault until our module had a chance to process the autoreplace logic.
63 * If the device could not be replaced, then the second online attempt will
64 * trigger the FMA fault that we skipped earlier.
66 * On Linux udev provides a disk insert for both the disk and the partition.
71 #include <libnvpair.h>
80 #include <sys/sunddi.h>
81 #include <sys/sysevent/eventdefs.h>
82 #include <sys/sysevent/dev.h>
83 #include <thread_pool.h>
87 #include "zfs_agents.h"
88 #include "../zed_log.h"
90 #define DEV_BYID_PATH "/dev/disk/by-id/"
91 #define DEV_BYPATH_PATH "/dev/disk/by-path/"
92 #define DEV_BYVDEV_PATH "/dev/disk/by-vdev/"
94 typedef void (*zfs_process_func_t)(zpool_handle_t *, nvlist_t *, boolean_t);
96 libzfs_handle_t *g_zfshdl;
97 list_t g_pool_list; /* list of unavailable pools at initialization */
98 list_t g_device_list; /* list of disks with asynchronous label request */
100 boolean_t g_enumeration_done;
101 pthread_t g_zfs_tid; /* zfs_enum_pools() thread */
103 typedef struct unavailpool {
104 zpool_handle_t *uap_zhp;
105 list_node_t uap_node;
108 typedef struct pendingdev {
109 char pd_physpath[128];
114 zfs_toplevel_state(zpool_handle_t *zhp)
120 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
121 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
122 verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
123 (uint64_t **)&vs, &c) == 0);
124 return (vs->vs_state);
128 zfs_unavail_pool(zpool_handle_t *zhp, void *data)
130 zed_log_msg(LOG_INFO, "zfs_unavail_pool: examining '%s' (state %d)",
131 zpool_get_name(zhp), (int)zfs_toplevel_state(zhp));
133 if (zfs_toplevel_state(zhp) < VDEV_STATE_DEGRADED) {
135 uap = malloc(sizeof (unavailpool_t));
137 list_insert_tail((list_t *)data, uap);
145 * Two stage replace on Linux
146 * since we get disk notifications
147 * we can wait for partitioned disk slice to show up!
149 * First stage tags the disk, initiates async partitioning, and returns
150 * Second stage finds the tag and proceeds to ZFS labeling/replace
152 * disk-add --> label-disk + tag-disk --> partition-add --> zpool_vdev_attach
154 * 1. physical match with no fs, no partition
155 * tag it top, partition disk
157 * 2. physical match again, see partition and tag
162 * The device associated with the given vdev (either by devid or physical path)
163 * has been added to the system. If 'isdisk' is set, then we only attempt a
164 * replacement if it's a whole disk. This also implies that we should label the
167 * First, we attempt to online the device (making sure to undo any spare
168 * operation when finished). If this succeeds, then we're done. If it fails,
169 * and the new state is VDEV_CANT_OPEN, it indicates that the device was opened,
170 * but that the label was not what we expected. If the 'autoreplace' property
171 * is enabled, then we relabel the disk (if specified), and attempt a 'zpool
172 * replace'. If the online is successful, but the new state is something else
173 * (REMOVED or FAULTED), it indicates that we're out of sync or in some sort of
174 * race, and we should avoid attempting to relabel the disk.
176 * Also can arrive here from a ESC_ZFS_VDEV_CHECK event
179 zfs_process_add(zpool_handle_t *zhp, nvlist_t *vdev, boolean_t labeled)
182 vdev_state_t newstate;
183 nvlist_t *nvroot, *newvd;
184 pendingdev_t *device;
185 uint64_t wholedisk = 0ULL;
186 uint64_t offline = 0ULL;
187 uint64_t guid = 0ULL;
188 char *physpath = NULL, *new_devid = NULL, *enc_sysfs_path = NULL;
189 char rawpath[PATH_MAX], fullpath[PATH_MAX];
190 char devpath[PATH_MAX];
192 boolean_t is_dm = B_FALSE;
193 boolean_t is_sd = B_FALSE;
197 if (nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path) != 0)
200 /* Skip healthy disks */
201 verify(nvlist_lookup_uint64_array(vdev, ZPOOL_CONFIG_VDEV_STATS,
202 (uint64_t **)&vs, &c) == 0);
203 if (vs->vs_state == VDEV_STATE_HEALTHY) {
204 zed_log_msg(LOG_INFO, "%s: %s is already healthy, skip it.",
209 (void) nvlist_lookup_string(vdev, ZPOOL_CONFIG_PHYS_PATH, &physpath);
210 (void) nvlist_lookup_string(vdev, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
212 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK, &wholedisk);
213 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_OFFLINE, &offline);
214 (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_GUID, &guid);
217 return; /* don't intervene if it was taken offline */
219 is_dm = zfs_dev_is_dm(path);
220 zed_log_msg(LOG_INFO, "zfs_process_add: pool '%s' vdev '%s', phys '%s'"
221 " wholedisk %d, %s dm (guid %llu)", zpool_get_name(zhp), path,
222 physpath ? physpath : "NULL", wholedisk, is_dm ? "is" : "not",
223 (long long unsigned int)guid);
226 * The VDEV guid is preferred for identification (gets passed in path)
229 (void) snprintf(fullpath, sizeof (fullpath), "%llu",
230 (long long unsigned int)guid);
233 * otherwise use path sans partition suffix for whole disks
235 (void) strlcpy(fullpath, path, sizeof (fullpath));
237 char *spath = zfs_strip_partition(fullpath);
239 zed_log_msg(LOG_INFO, "%s: Can't alloc",
244 (void) strlcpy(fullpath, spath, sizeof (fullpath));
250 * Attempt to online the device.
252 if (zpool_vdev_online(zhp, fullpath,
253 ZFS_ONLINE_CHECKREMOVE | ZFS_ONLINE_UNSPARE, &newstate) == 0 &&
254 (newstate == VDEV_STATE_HEALTHY ||
255 newstate == VDEV_STATE_DEGRADED)) {
256 zed_log_msg(LOG_INFO, " zpool_vdev_online: vdev %s is %s",
257 fullpath, (newstate == VDEV_STATE_HEALTHY) ?
258 "HEALTHY" : "DEGRADED");
263 * vdev_id alias rule for using scsi_debug devices (FMA automated
266 if (physpath != NULL && strcmp("scsidebug", physpath) == 0)
270 * If the pool doesn't have the autoreplace property set, then use
271 * vdev online to trigger a FMA fault by posting an ereport.
273 if (!zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOREPLACE, NULL) ||
274 !(wholedisk || is_dm) || (physpath == NULL)) {
275 (void) zpool_vdev_online(zhp, fullpath, ZFS_ONLINE_FORCEFAULT,
277 zed_log_msg(LOG_INFO, "Pool's autoreplace is not enabled or "
278 "not a whole disk for '%s'", fullpath);
283 * Convert physical path into its current device node. Rawpath
284 * needs to be /dev/disk/by-vdev for a scsi_debug device since
285 * /dev/disk/by-path will not be present.
287 (void) snprintf(rawpath, sizeof (rawpath), "%s%s",
288 is_sd ? DEV_BYVDEV_PATH : DEV_BYPATH_PATH, physpath);
290 if (realpath(rawpath, devpath) == NULL && !is_dm) {
291 zed_log_msg(LOG_INFO, " realpath: %s failed (%s)",
292 rawpath, strerror(errno));
294 (void) zpool_vdev_online(zhp, fullpath, ZFS_ONLINE_FORCEFAULT,
297 zed_log_msg(LOG_INFO, " zpool_vdev_online: %s FORCEFAULT (%s)",
298 fullpath, libzfs_error_description(g_zfshdl));
302 /* Only autoreplace bad disks */
303 if ((vs->vs_state != VDEV_STATE_DEGRADED) &&
304 (vs->vs_state != VDEV_STATE_FAULTED) &&
305 (vs->vs_state != VDEV_STATE_CANT_OPEN)) {
309 nvlist_lookup_string(vdev, "new_devid", &new_devid);
312 /* Don't label device mapper or multipath disks. */
313 } else if (!labeled) {
315 * we're auto-replacing a raw disk, so label it first
320 * If this is a request to label a whole disk, then attempt to
321 * write out the label. Before we can label the disk, we need
322 * to map the physical string that was matched on to the under
325 * If any part of this process fails, then do a force online
326 * to trigger a ZFS fault for the device (and any hot spare
329 leafname = strrchr(devpath, '/') + 1;
332 * If this is a request to label a whole disk, then attempt to
333 * write out the label.
335 if (zpool_label_disk(g_zfshdl, zhp, leafname) != 0) {
336 zed_log_msg(LOG_INFO, " zpool_label_disk: could not "
337 "label '%s' (%s)", leafname,
338 libzfs_error_description(g_zfshdl));
340 (void) zpool_vdev_online(zhp, fullpath,
341 ZFS_ONLINE_FORCEFAULT, &newstate);
346 * The disk labeling is asynchronous on Linux. Just record
347 * this label request and return as there will be another
348 * disk add event for the partition after the labeling is
351 device = malloc(sizeof (pendingdev_t));
352 (void) strlcpy(device->pd_physpath, physpath,
353 sizeof (device->pd_physpath));
354 list_insert_tail(&g_device_list, device);
356 zed_log_msg(LOG_INFO, " zpool_label_disk: async '%s' (%llu)",
357 leafname, (u_longlong_t)guid);
359 return; /* resumes at EC_DEV_ADD.ESC_DISK for partition */
361 } else /* labeled */ {
362 boolean_t found = B_FALSE;
364 * match up with request above to label the disk
366 for (device = list_head(&g_device_list); device != NULL;
367 device = list_next(&g_device_list, device)) {
368 if (strcmp(physpath, device->pd_physpath) == 0) {
369 list_remove(&g_device_list, device);
374 zed_log_msg(LOG_INFO, "zpool_label_disk: %s != %s",
375 physpath, device->pd_physpath);
378 /* unexpected partition slice encountered */
379 zed_log_msg(LOG_INFO, "labeled disk %s unexpected here",
381 (void) zpool_vdev_online(zhp, fullpath,
382 ZFS_ONLINE_FORCEFAULT, &newstate);
386 zed_log_msg(LOG_INFO, " zpool_label_disk: resume '%s' (%llu)",
387 physpath, (u_longlong_t)guid);
389 (void) snprintf(devpath, sizeof (devpath), "%s%s",
390 DEV_BYID_PATH, new_devid);
394 * Construct the root vdev to pass to zpool_vdev_attach(). While adding
395 * the entire vdev structure is harmless, we construct a reduced set of
396 * path/physpath/wholedisk to keep it simple.
398 if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0) {
399 zed_log_msg(LOG_WARNING, "zfs_mod: nvlist_alloc out of memory");
402 if (nvlist_alloc(&newvd, NV_UNIQUE_NAME, 0) != 0) {
403 zed_log_msg(LOG_WARNING, "zfs_mod: nvlist_alloc out of memory");
408 if (nvlist_add_string(newvd, ZPOOL_CONFIG_TYPE, VDEV_TYPE_DISK) != 0 ||
409 nvlist_add_string(newvd, ZPOOL_CONFIG_PATH, path) != 0 ||
410 nvlist_add_string(newvd, ZPOOL_CONFIG_DEVID, new_devid) != 0 ||
411 (physpath != NULL && nvlist_add_string(newvd,
412 ZPOOL_CONFIG_PHYS_PATH, physpath) != 0) ||
413 (enc_sysfs_path != NULL && nvlist_add_string(newvd,
414 ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH, enc_sysfs_path) != 0) ||
415 nvlist_add_uint64(newvd, ZPOOL_CONFIG_WHOLE_DISK, wholedisk) != 0 ||
416 nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) != 0 ||
417 nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &newvd,
419 zed_log_msg(LOG_WARNING, "zfs_mod: unable to add nvlist pairs");
428 * Wait for udev to verify the links exist, then auto-replace
429 * the leaf disk at same physical location.
431 if (zpool_label_disk_wait(path, 3000) != 0) {
432 zed_log_msg(LOG_WARNING, "zfs_mod: expected replacement "
433 "disk %s is missing", path);
438 ret = zpool_vdev_attach(zhp, fullpath, path, nvroot, B_TRUE, B_FALSE);
440 zed_log_msg(LOG_INFO, " zpool_vdev_replace: %s with %s (%s)",
441 fullpath, path, (ret == 0) ? "no errors" :
442 libzfs_error_description(g_zfshdl));
448 * Utility functions to find a vdev matching given criteria.
450 typedef struct dev_data {
451 const char *dd_compare;
453 zfs_process_func_t dd_func;
455 boolean_t dd_islabeled;
456 uint64_t dd_pool_guid;
457 uint64_t dd_vdev_guid;
458 const char *dd_new_devid;
462 zfs_iter_vdev(zpool_handle_t *zhp, nvlist_t *nvl, void *data)
464 dev_data_t *dp = data;
470 * First iterate over any children.
472 if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN,
473 &child, &children) == 0) {
474 for (c = 0; c < children; c++)
475 zfs_iter_vdev(zhp, child[c], data);
479 * Iterate over any spares and cache devices
481 if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_SPARES,
482 &child, &children) == 0) {
483 for (c = 0; c < children; c++)
484 zfs_iter_vdev(zhp, child[c], data);
486 if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_L2CACHE,
487 &child, &children) == 0) {
488 for (c = 0; c < children; c++)
489 zfs_iter_vdev(zhp, child[c], data);
492 /* once a vdev was matched and processed there is nothing left to do */
497 * Match by GUID if available otherwise fallback to devid or physical
499 if (dp->dd_vdev_guid != 0) {
502 if (nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_GUID,
503 &guid) != 0 || guid != dp->dd_vdev_guid) {
506 zed_log_msg(LOG_INFO, " zfs_iter_vdev: matched on %llu", guid);
507 dp->dd_found = B_TRUE;
509 } else if (dp->dd_compare != NULL) {
511 * NOTE: On Linux there is an event for partition, so unlike
512 * illumos, substring matching is not required to accommodate
513 * the partition suffix. An exact match will be present in
514 * the dp->dd_compare value.
516 if (nvlist_lookup_string(nvl, dp->dd_prop, &path) != 0 ||
517 strcmp(dp->dd_compare, path) != 0)
520 zed_log_msg(LOG_INFO, " zfs_iter_vdev: matched %s on %s",
522 dp->dd_found = B_TRUE;
524 /* pass the new devid for use by replacing code */
525 if (dp->dd_new_devid != NULL) {
526 (void) nvlist_add_string(nvl, "new_devid",
531 (dp->dd_func)(zhp, nvl, dp->dd_islabeled);
535 zfs_enable_ds(void *arg)
537 unavailpool_t *pool = (unavailpool_t *)arg;
539 (void) zpool_enable_datasets(pool->uap_zhp, NULL, 0);
540 zpool_close(pool->uap_zhp);
545 zfs_iter_pool(zpool_handle_t *zhp, void *data)
547 nvlist_t *config, *nvl;
548 dev_data_t *dp = data;
552 zed_log_msg(LOG_INFO, "zfs_iter_pool: evaluating vdevs on %s (by %s)",
553 zpool_get_name(zhp), dp->dd_vdev_guid ? "GUID" : dp->dd_prop);
556 * For each vdev in this pool, look for a match to apply dd_func
558 if ((config = zpool_get_config(zhp, NULL)) != NULL) {
559 if (dp->dd_pool_guid == 0 ||
560 (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
561 &pool_guid) == 0 && pool_guid == dp->dd_pool_guid)) {
562 (void) nvlist_lookup_nvlist(config,
563 ZPOOL_CONFIG_VDEV_TREE, &nvl);
564 zfs_iter_vdev(zhp, nvl, data);
569 * if this pool was originally unavailable,
570 * then enable its datasets asynchronously
572 if (g_enumeration_done) {
573 for (pool = list_head(&g_pool_list); pool != NULL;
574 pool = list_next(&g_pool_list, pool)) {
576 if (strcmp(zpool_get_name(zhp),
577 zpool_get_name(pool->uap_zhp)))
579 if (zfs_toplevel_state(zhp) >= VDEV_STATE_DEGRADED) {
580 list_remove(&g_pool_list, pool);
581 (void) tpool_dispatch(g_tpool, zfs_enable_ds,
589 return (dp->dd_found); /* cease iteration after a match */
593 * Given a physical device location, iterate over all
594 * (pool, vdev) pairs which correspond to that location.
597 devphys_iter(const char *physical, const char *devid, zfs_process_func_t func,
600 dev_data_t data = { 0 };
602 data.dd_compare = physical;
604 data.dd_prop = ZPOOL_CONFIG_PHYS_PATH;
605 data.dd_found = B_FALSE;
606 data.dd_islabeled = is_slice;
607 data.dd_new_devid = devid; /* used by auto replace code */
609 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
611 return (data.dd_found);
615 * Given a device identifier, find any vdevs with a matching devid.
616 * On Linux we can match devid directly which is always a whole disk.
619 devid_iter(const char *devid, zfs_process_func_t func, boolean_t is_slice)
621 dev_data_t data = { 0 };
623 data.dd_compare = devid;
625 data.dd_prop = ZPOOL_CONFIG_DEVID;
626 data.dd_found = B_FALSE;
627 data.dd_islabeled = is_slice;
628 data.dd_new_devid = devid;
630 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
632 return (data.dd_found);
636 * Handle a EC_DEV_ADD.ESC_DISK event.
639 * Expects: DEV_PHYS_PATH string in schema
640 * Matches: vdev's ZPOOL_CONFIG_PHYS_PATH or ZPOOL_CONFIG_DEVID
642 * path: '/dev/dsk/c0t1d0s0' (persistent)
643 * devid: 'id1,sd@SATA_____Hitachi_HDS72101______JP2940HZ3H74MC/a'
644 * phys_path: '/pci@0,0/pci103c,1609@11/disk@1,0:a'
647 * provides: DEV_PHYS_PATH and DEV_IDENTIFIER strings in schema
648 * Matches: vdev's ZPOOL_CONFIG_PHYS_PATH or ZPOOL_CONFIG_DEVID
650 * path: '/dev/sdc1' (not persistent)
651 * devid: 'ata-SAMSUNG_HD204UI_S2HGJD2Z805891-part1'
652 * phys_path: 'pci-0000:04:00.0-sas-0x4433221106000000-lun-0'
655 zfs_deliver_add(nvlist_t *nvl, boolean_t is_lofi)
657 char *devpath = NULL, *devid;
661 * Expecting a devid string and an optional physical location
663 if (nvlist_lookup_string(nvl, DEV_IDENTIFIER, &devid) != 0)
666 (void) nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devpath);
668 is_slice = (nvlist_lookup_boolean(nvl, DEV_IS_PART) == 0);
670 zed_log_msg(LOG_INFO, "zfs_deliver_add: adding %s (%s) (is_slice %d)",
671 devid, devpath ? devpath : "NULL", is_slice);
674 * Iterate over all vdevs looking for a match in the following order:
675 * 1. ZPOOL_CONFIG_DEVID (identifies the unique disk)
676 * 2. ZPOOL_CONFIG_PHYS_PATH (identifies disk physical location).
678 * For disks, we only want to pay attention to vdevs marked as whole
679 * disks or are a multipath device.
681 if (!devid_iter(devid, zfs_process_add, is_slice) && devpath != NULL)
682 (void) devphys_iter(devpath, devid, zfs_process_add, is_slice);
688 * Called when we receive a VDEV_CHECK event, which indicates a device could not
689 * be opened during initial pool open, but the autoreplace property was set on
690 * the pool. In this case, we treat it as if it were an add event.
693 zfs_deliver_check(nvlist_t *nvl)
695 dev_data_t data = { 0 };
697 if (nvlist_lookup_uint64(nvl, ZFS_EV_POOL_GUID,
698 &data.dd_pool_guid) != 0 ||
699 nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID,
700 &data.dd_vdev_guid) != 0 ||
701 data.dd_vdev_guid == 0)
704 zed_log_msg(LOG_INFO, "zfs_deliver_check: pool '%llu', vdev %llu",
705 data.dd_pool_guid, data.dd_vdev_guid);
707 data.dd_func = zfs_process_add;
709 (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
715 zfsdle_vdev_online(zpool_handle_t *zhp, void *data)
717 char *devname = data;
718 boolean_t avail_spare, l2cache;
722 zed_log_msg(LOG_INFO, "zfsdle_vdev_online: searching for '%s' in '%s'",
723 devname, zpool_get_name(zhp));
725 if ((tgt = zpool_find_vdev_by_physpath(zhp, devname,
726 &avail_spare, &l2cache, NULL)) != NULL) {
727 char *path, fullpath[MAXPATHLEN];
730 error = nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &path);
736 error = nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
742 path = strrchr(path, '/');
744 path = zfs_strip_partition(path + 1);
754 (void) strlcpy(fullpath, path, sizeof (fullpath));
758 * We need to reopen the pool associated with this
759 * device so that the kernel can update the size of
760 * the expanded device. When expanding there is no
761 * need to restart the scrub from the beginning.
763 boolean_t scrub_restart = B_FALSE;
764 (void) zpool_reopen_one(zhp, &scrub_restart);
766 (void) strlcpy(fullpath, path, sizeof (fullpath));
769 if (zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
770 vdev_state_t newstate;
772 if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL) {
773 error = zpool_vdev_online(zhp, fullpath, 0,
775 zed_log_msg(LOG_INFO, "zfsdle_vdev_online: "
776 "setting device '%s' to ONLINE state "
777 "in pool '%s': %d", fullpath,
778 zpool_get_name(zhp), error);
789 * This function handles the ESC_DEV_DLE device change event. Use the
790 * provided vdev guid when looking up a disk or partition, when the guid
791 * is not present assume the entire disk is owned by ZFS and append the
792 * expected -part1 partition information then lookup by physical path.
795 zfs_deliver_dle(nvlist_t *nvl)
797 char *devname, name[MAXPATHLEN];
800 if (nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID, &guid) == 0) {
801 sprintf(name, "%llu", (u_longlong_t)guid);
802 } else if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devname) == 0) {
803 strlcpy(name, devname, MAXPATHLEN);
804 zfs_append_partition(name, MAXPATHLEN);
806 zed_log_msg(LOG_INFO, "zfs_deliver_dle: no guid or physpath");
809 if (zpool_iter(g_zfshdl, zfsdle_vdev_online, name) != 1) {
810 zed_log_msg(LOG_INFO, "zfs_deliver_dle: device '%s' not "
819 * syseventd daemon module event handler
821 * Handles syseventd daemon zfs device related events:
823 * EC_DEV_ADD.ESC_DISK
824 * EC_DEV_STATUS.ESC_DEV_DLE
825 * EC_ZFS.ESC_ZFS_VDEV_CHECK
827 * Note: assumes only one thread active at a time (not thread safe)
830 zfs_slm_deliver_event(const char *class, const char *subclass, nvlist_t *nvl)
833 boolean_t is_lofi = B_FALSE, is_check = B_FALSE, is_dle = B_FALSE;
835 if (strcmp(class, EC_DEV_ADD) == 0) {
837 * We're mainly interested in disk additions, but we also listen
838 * for new loop devices, to allow for simplified testing.
840 if (strcmp(subclass, ESC_DISK) == 0)
842 else if (strcmp(subclass, ESC_LOFI) == 0)
848 } else if (strcmp(class, EC_ZFS) == 0 &&
849 strcmp(subclass, ESC_ZFS_VDEV_CHECK) == 0) {
851 * This event signifies that a device failed to open
852 * during pool load, but the 'autoreplace' property was
853 * set, so we should pretend it's just been added.
856 } else if (strcmp(class, EC_DEV_STATUS) == 0 &&
857 strcmp(subclass, ESC_DEV_DLE) == 0) {
864 ret = zfs_deliver_dle(nvl);
866 ret = zfs_deliver_check(nvl);
868 ret = zfs_deliver_add(nvl, is_lofi);
875 zfs_enum_pools(void *arg)
877 (void) zpool_iter(g_zfshdl, zfs_unavail_pool, (void *)&g_pool_list);
879 * Linux - instead of using a thread pool, each list entry
880 * will spawn a thread when an unavailable pool transitions
881 * to available. zfs_slm_fini will wait for these threads.
883 g_enumeration_done = B_TRUE;
888 * called from zed daemon at startup
890 * sent messages from zevents or udev monitor
892 * For now, each agent has its own libzfs instance
897 if ((g_zfshdl = libzfs_init()) == NULL)
901 * collect a list of unavailable pools (asynchronously,
902 * since this can take a while)
904 list_create(&g_pool_list, sizeof (struct unavailpool),
905 offsetof(struct unavailpool, uap_node));
907 if (pthread_create(&g_zfs_tid, NULL, zfs_enum_pools, NULL) != 0) {
908 list_destroy(&g_pool_list);
909 libzfs_fini(g_zfshdl);
913 list_create(&g_device_list, sizeof (struct pendingdev),
914 offsetof(struct pendingdev, pd_node));
923 pendingdev_t *device;
925 /* wait for zfs_enum_pools thread to complete */
926 (void) pthread_join(g_zfs_tid, NULL);
927 /* destroy the thread pool */
928 if (g_tpool != NULL) {
930 tpool_destroy(g_tpool);
933 while ((pool = (list_head(&g_pool_list))) != NULL) {
934 list_remove(&g_pool_list, pool);
935 zpool_close(pool->uap_zhp);
938 list_destroy(&g_pool_list);
940 while ((device = (list_head(&g_device_list))) != NULL) {
941 list_remove(&g_device_list, device);
944 list_destroy(&g_device_list);
946 libzfs_fini(g_zfshdl);
950 zfs_slm_event(const char *class, const char *subclass, nvlist_t *nvl)
952 zed_log_msg(LOG_INFO, "zfs_slm_event: %s.%s", class, subclass);
953 (void) zfs_slm_deliver_event(class, subclass, nvl);