zfs_mod.c revision 3c112a2b34403220c06c3e2fcac403358cfba168
015f8fff605f2fbd5fd0072e555576297804d57bhiremath/*
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * CDDL HEADER START
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * The contents of this file are subject to the terms of the
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * Common Development and Distribution License (the "License").
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * You may not use this file except in compliance with the License.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * or http://www.opensolaris.org/os/licensing.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * See the License for the specific language governing permissions
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * and limitations under the License.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * When distributing Covered Code, include this CDDL HEADER in each
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * If applicable, add the following below this CDDL HEADER, with the
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * fields enclosed by brackets "[]" replaced with your own identifying
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * information: Portions Copyright [yyyy] [name of copyright owner]
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * CDDL HEADER END
015f8fff605f2fbd5fd0072e555576297804d57bhiremath */
015f8fff605f2fbd5fd0072e555576297804d57bhiremath/*
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath */
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath/*
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * ZFS syseventd module.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * The purpose of this module is to identify when devices are added to the
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * system, and appropriately online or replace the affected vdevs.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * When a device is added to the system:
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * 1. Search for any vdevs whose devid matches that of the newly added
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * device.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * 2. If no vdevs are found, then search for any vdevs whose devfs path
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * matches that of the new device.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * 3. If no vdevs match by either method, then ignore the event.
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala *
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala * 4. Attempt to online the device with a flag to indicate that it should
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * be unspared when resilvering completes. If this succeeds, then the
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * same device was inserted and we should continue normally.
bd670b35a010421b6e1a5536c34453a827007c81Erik Nordmark *
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * 5. If the pool does not have the 'autoreplace' property set, attempt to
bd670b35a010421b6e1a5536c34453a827007c81Erik Nordmark * online the device again without the unspare flag, which will
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * generate a FMA fault.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * 6. If the pool has the 'autoreplace' property set, and the matching vdev
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * is a whole disk, then label the new disk and attempt a 'zpool
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * replace'.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * The module responds to EC_DEV_ADD events for both disks and lofi devices,
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * with the latter used for testing. The special ESC_ZFS_VDEV_CHECK event
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * indicates that a device failed to open during pool load, but the autoreplace
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * property was set. In this case, we deferred the associated FMA fault until
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * our module had a chance to process the autoreplace logic. If the device
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * could not be replaced, then the second online attempt will trigger the FMA
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * fault that we skipped earlier.
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath */
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <alloca.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <devid.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <fcntl.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <libnvpair.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <libsysevent.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <libzfs.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <limits.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <stdlib.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <string.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <syslog.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <sys/list.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <sys/sunddi.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <sys/sysevent/eventdefs.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <sys/sysevent/dev.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <thread_pool.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include <unistd.h>
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath#include "syseventd.h"
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath#if defined(__i386) || defined(__amd64)
015f8fff605f2fbd5fd0072e555576297804d57bhiremath#define PHYS_PATH ":q"
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath#define RAW_SLICE "p0"
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath#elif defined(__sparc)
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath#define PHYS_PATH ":c"
015f8fff605f2fbd5fd0072e555576297804d57bhiremath#define RAW_SLICE "s2"
015f8fff605f2fbd5fd0072e555576297804d57bhiremath#else
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath#error Unknown architecture
bbd6719318c24a8a2364080d8a139444e9944311hiremath#endif
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremathtypedef void (*zfs_process_func_t)(zpool_handle_t *, nvlist_t *, boolean_t);
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremathlibzfs_handle_t *g_zfshdl;
015f8fff605f2fbd5fd0072e555576297804d57bhiremathlist_t g_pool_list;
015f8fff605f2fbd5fd0072e555576297804d57bhiremathtpool_t *g_tpool;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremathtypedef struct unavailpool {
015f8fff605f2fbd5fd0072e555576297804d57bhiremath zpool_handle_t *uap_zhp;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath list_node_t uap_node;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath} unavailpool_t;
bbd6719318c24a8a2364080d8a139444e9944311hiremath
bbd6719318c24a8a2364080d8a139444e9944311hiremathint
bbd6719318c24a8a2364080d8a139444e9944311hiremathzfs_toplevel_state(zpool_handle_t *zhp)
bbd6719318c24a8a2364080d8a139444e9944311hiremath{
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath nvlist_t *nvroot;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath vdev_stat_t *vs;
bd670b35a010421b6e1a5536c34453a827007c81Erik Nordmark unsigned int c;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
015f8fff605f2fbd5fd0072e555576297804d57bhiremath ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
bbd6719318c24a8a2364080d8a139444e9944311hiremath verify(nvlist_lookup_uint64_array(nvroot, ZPOOL_CONFIG_VDEV_STATS,
bbd6719318c24a8a2364080d8a139444e9944311hiremath (uint64_t **)&vs, &c) == 0);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath return (vs->vs_state);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath}
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
bbd6719318c24a8a2364080d8a139444e9944311hiremathstatic int
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremathzfs_unavail_pool(zpool_handle_t *zhp, void *data)
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath{
015f8fff605f2fbd5fd0072e555576297804d57bhiremath if (zfs_toplevel_state(zhp) < VDEV_STATE_DEGRADED) {
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath unavailpool_t *uap;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath uap = malloc(sizeof (unavailpool_t));
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath uap->uap_zhp = zhp;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath list_insert_tail((list_t *)data, uap);
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath } else {
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath zpool_close(zhp);
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath }
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath return (0);
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath}
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath/*
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * The device associated with the given vdev (either by devid or physical path)
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * has been added to the system. If 'isdisk' is set, then we only attempt a
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * replacement if it's a whole disk. This also implies that we should label the
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * disk first.
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath *
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * First, we attempt to online the device (making sure to undo any spare
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * operation when finished). If this succeeds, then we're done. If it fails,
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * and the new state is VDEV_CANT_OPEN, it indicates that the device was opened,
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * but that the label was not what we expected. If the 'autoreplace' property
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * is not set, then we relabel the disk (if specified), and attempt a 'zpool
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * replace'. If the online is successful, but the new state is something else
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * (REMOVED or FAULTED), it indicates that we're out of sync or in some sort of
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * race, and we should avoid attempting to relabel the disk.
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath */
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremathstatic void
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremathzfs_process_add(zpool_handle_t *zhp, nvlist_t *vdev, boolean_t isdisk)
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath{
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath char *path;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath vdev_state_t newstate;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath nvlist_t *nvroot, *newvd;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath uint64_t wholedisk = 0ULL;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath char *physpath = NULL;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath char rawpath[PATH_MAX], fullpath[PATH_MAX];
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath size_t len;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath if (nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path) != 0)
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath return;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath (void) nvlist_lookup_string(vdev, ZPOOL_CONFIG_PHYS_PATH, &physpath);
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath (void) nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK, &wholedisk);
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath /*
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * We should have a way to online a device by guid. With the current
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * interface, we are forced to chop off the 's0' for whole disks.
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath */
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath (void) strlcpy(fullpath, path, sizeof (fullpath));
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath if (wholedisk)
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath fullpath[strlen(fullpath) - 2] = '\0';
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath /*
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * Attempt to online the device. It would be nice to online this by
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath * GUID, but the current interface only supports lookup by path.
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath */
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath if (zpool_vdev_online(zhp, fullpath,
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath ZFS_ONLINE_CHECKREMOVE | ZFS_ONLINE_UNSPARE, &newstate) == 0 &&
015f8fff605f2fbd5fd0072e555576297804d57bhiremath (newstate == VDEV_STATE_HEALTHY || newstate == VDEV_STATE_DEGRADED))
015f8fff605f2fbd5fd0072e555576297804d57bhiremath return;
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath /*
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * If the pool doesn't have the autoreplace property set, then attempt a
bd670b35a010421b6e1a5536c34453a827007c81Erik Nordmark * true online (without the unspare flag), which will trigger a FMA
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath * fault.
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath */
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath if (!zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOREPLACE, NULL) ||
bd670b35a010421b6e1a5536c34453a827007c81Erik Nordmark (isdisk && !wholedisk)) {
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath (void) zpool_vdev_online(zhp, fullpath, ZFS_ONLINE_FORCEFAULT,
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath &newstate);
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath return;
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath }
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
261906274d77b4a1c6d61c75d170ab5a8e85a6a7Shantkumar Hiremath if (isdisk) {
23a9f846c20554daf41a34c0f67d0184f9bb792fShantkumar Hiremath /*
bbd6719318c24a8a2364080d8a139444e9944311hiremath * If this is a request to label a whole disk, then attempt to
bbd6719318c24a8a2364080d8a139444e9944311hiremath * write out the label. Before we can label the disk, we need
bbd6719318c24a8a2364080d8a139444e9944311hiremath * access to a raw node. Ideally, we'd like to walk the devinfo
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * tree and find a raw node from the corresponding parent node.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * This is overly complicated, and since we know how we labeled
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * this device in the first place, we know it's save to switch
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * from /dev/dsk to /dev/rdsk and append the backup slice.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath *
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * If any part of this process fails, then do a force online to
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * trigger a ZFS fault for the device (and any hot spare
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * replacement).
015f8fff605f2fbd5fd0072e555576297804d57bhiremath */
015f8fff605f2fbd5fd0072e555576297804d57bhiremath if (strncmp(path, "/dev/dsk/", 9) != 0) {
015f8fff605f2fbd5fd0072e555576297804d57bhiremath (void) zpool_vdev_online(zhp, fullpath,
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala ZFS_ONLINE_FORCEFAULT, &newstate);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala return;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala (void) strlcpy(rawpath, path + 9, sizeof (rawpath));
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala len = strlen(rawpath);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala rawpath[len - 2] = '\0';
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala if (zpool_label_disk(g_zfshdl, zhp, rawpath) != 0) {
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala (void) zpool_vdev_online(zhp, fullpath,
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala ZFS_ONLINE_FORCEFAULT, &newstate);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath return;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath /*
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * Cosntruct the root vdev to pass to zpool_vdev_attach(). While adding
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala * the entire vdev structure is harmless, we construct a reduced set of
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala * path/physpath/wholedisk to keep it simple.
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala */
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala if (nvlist_alloc(&nvroot, NV_UNIQUE_NAME, 0) != 0)
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala return;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala if (nvlist_alloc(&newvd, NV_UNIQUE_NAME, 0) != 0) {
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_free(nvroot);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala return;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala if (nvlist_add_string(newvd, ZPOOL_CONFIG_TYPE, VDEV_TYPE_DISK) != 0 ||
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_add_string(newvd, ZPOOL_CONFIG_PATH, path) != 0 ||
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala (physpath != NULL && nvlist_add_string(newvd,
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala ZPOOL_CONFIG_PHYS_PATH, physpath) != 0) ||
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_add_uint64(newvd, ZPOOL_CONFIG_WHOLE_DISK, wholedisk) != 0 ||
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT) != 0 ||
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &newvd,
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala 1) != 0) {
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_free(newvd);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_free(nvroot);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala return;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_free(newvd);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala (void) zpool_vdev_attach(zhp, fullpath, path, nvroot, B_TRUE);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_free(nvroot);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath}
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
015f8fff605f2fbd5fd0072e555576297804d57bhiremath/*
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * Utility functions to find a vdev matching given criteria.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath */
e11c3f44f531fdff80941ce57c065d2ae861cefcmeemtypedef struct dev_data {
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem const char *dd_compare;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath const char *dd_prop;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath zfs_process_func_t dd_func;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem boolean_t dd_found;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath boolean_t dd_isdisk;
af4c679f647cf088543c762e33d41a3ac52cfa14Sean McEnroe uint64_t dd_pool_guid;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem uint64_t dd_vdev_guid;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem} dev_data_t;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem
015f8fff605f2fbd5fd0072e555576297804d57bhiremathstatic void
af4c679f647cf088543c762e33d41a3ac52cfa14Sean McEnroezfs_iter_vdev(zpool_handle_t *zhp, nvlist_t *nvl, void *data)
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem{
015f8fff605f2fbd5fd0072e555576297804d57bhiremath dev_data_t *dp = data;
af4c679f647cf088543c762e33d41a3ac52cfa14Sean McEnroe char *path;
af4c679f647cf088543c762e33d41a3ac52cfa14Sean McEnroe uint_t c, children;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem nvlist_t **child;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem size_t len;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath uint64_t guid;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem /*
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * First iterate over any children.
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem */
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem if (nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN,
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem &child, &children) == 0) {
af4c679f647cf088543c762e33d41a3ac52cfa14Sean McEnroe for (c = 0; c < children; c++)
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem zfs_iter_vdev(zhp, child[c], data);
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem return;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath }
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem if (dp->dd_vdev_guid != 0) {
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem if (nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_GUID,
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem &guid) != 0 || guid != dp->dd_vdev_guid)
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem return;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath } else {
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem len = strlen(dp->dd_compare);
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem if (nvlist_lookup_string(nvl, dp->dd_prop, &path) != 0 ||
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem strncmp(dp->dd_compare, path, len) != 0)
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem return;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem /*
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * Normally, we want to have an exact match for the comparison
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * string. However, we allow substring matches in the following
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * cases:
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem *
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * <path>: This is a devpath, and the target is one
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * of its children.
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem *
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * <path/> This is a devid for a whole disk, and
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * the target is one of its children.
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem */
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem if (path[len] != '\0' && path[len] != ':' &&
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem path[len - 1] != '/')
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem return;
03494a9880d80f834bec10a1e8f0a2f8f7c97bf4Bill Taylor }
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem (dp->dd_func)(zhp, nvl, dp->dd_isdisk);
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem}
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem
e11c3f44f531fdff80941ce57c065d2ae861cefcmeemvoid
e11c3f44f531fdff80941ce57c065d2ae861cefcmeemzfs_enable_ds(void *arg)
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem{
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem unavailpool_t *pool = (unavailpool_t *)arg;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem (void) zpool_enable_datasets(pool->uap_zhp, NULL, 0);
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem zpool_close(pool->uap_zhp);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath free(pool);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala}
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummalastatic int
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummalazfs_iter_pool(zpool_handle_t *zhp, void *data)
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala{
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala nvlist_t *config, *nvl;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala dev_data_t *dp = data;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala uint64_t pool_guid;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala unavailpool_t *pool;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala if ((config = zpool_get_config(zhp, NULL)) != NULL) {
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala if (dp->dd_pool_guid == 0 ||
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala &pool_guid) == 0 && pool_guid == dp->dd_pool_guid)) {
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala (void) nvlist_lookup_nvlist(config,
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala ZPOOL_CONFIG_VDEV_TREE, &nvl);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala zfs_iter_vdev(zhp, nvl, data);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala for (pool = list_head(&g_pool_list); pool != NULL;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala pool = list_next(&g_pool_list, pool)) {
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala if (strcmp(zpool_get_name(zhp),
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala zpool_get_name(pool->uap_zhp)))
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala continue;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala if (zfs_toplevel_state(zhp) >= VDEV_STATE_DEGRADED) {
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala list_remove(&g_pool_list, pool);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala (void) tpool_dispatch(g_tpool, zfs_enable_ds, pool);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala break;
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala }
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem zpool_close(zhp);
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem return (0);
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem}
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath/*
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * Given a physical device path, iterate over all (pool, vdev) pairs which
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * correspond to the given path.
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem */
e11c3f44f531fdff80941ce57c065d2ae861cefcmeemstatic boolean_t
e11c3f44f531fdff80941ce57c065d2ae861cefcmeemdevpath_iter(const char *devpath, zfs_process_func_t func, boolean_t wholedisk)
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem{
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem dev_data_t data = { 0 };
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem data.dd_compare = devpath;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem data.dd_func = func;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem data.dd_prop = ZPOOL_CONFIG_PHYS_PATH;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath data.dd_found = B_FALSE;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath data.dd_isdisk = wholedisk;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem return (data.dd_found);
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem}
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala/*
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala * Given a /devices path, lookup the corresponding devid for each minor node,
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala * and find any vdevs with matching devids. Doing this straight up would be
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * rather inefficient, O(minor nodes * vdevs in system), so we take advantage of
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * the fact that each devid ends with "/<minornode>". Once we find any valid
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * minor node, we chop off the portion after the last slash, and then search for
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * matching vdevs, which is O(vdevs in system).
015f8fff605f2fbd5fd0072e555576297804d57bhiremath */
e11c3f44f531fdff80941ce57c065d2ae861cefcmeemstatic boolean_t
e11c3f44f531fdff80941ce57c065d2ae861cefcmeemdevid_iter(const char *devpath, zfs_process_func_t func, boolean_t wholedisk)
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem{
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem size_t len = strlen(devpath) + sizeof ("/devices") +
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem sizeof (PHYS_PATH) - 1;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem char *fullpath;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem int fd;
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem ddi_devid_t devid;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath char *devidstr, *fulldevid;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath dev_data_t data = { 0 };
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem /*
e11c3f44f531fdff80941ce57c065d2ae861cefcmeem * Try to open a known minor node.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath */
015f8fff605f2fbd5fd0072e555576297804d57bhiremath fullpath = alloca(len);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath (void) snprintf(fullpath, len, "/devices%s%s", devpath, PHYS_PATH);
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath if ((fd = open(fullpath, O_RDONLY)) < 0)
015f8fff605f2fbd5fd0072e555576297804d57bhiremath return (B_FALSE);
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath /*
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * Determine the devid as a string, with no trailing slash for the minor
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * node.
015f8fff605f2fbd5fd0072e555576297804d57bhiremath */
015f8fff605f2fbd5fd0072e555576297804d57bhiremath if (devid_get(fd, &devid) != 0) {
015f8fff605f2fbd5fd0072e555576297804d57bhiremath (void) close(fd);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath return (B_FALSE);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath }
015f8fff605f2fbd5fd0072e555576297804d57bhiremath (void) close(fd);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath if ((devidstr = devid_str_encode(devid, NULL)) == NULL) {
015f8fff605f2fbd5fd0072e555576297804d57bhiremath devid_free(devid);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath return (B_FALSE);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath }
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath len = strlen(devidstr) + 2;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath fulldevid = alloca(len);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala (void) snprintf(fulldevid, len, "%s/", devidstr);
015f8fff605f2fbd5fd0072e555576297804d57bhiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath data.dd_compare = fulldevid;
015f8fff605f2fbd5fd0072e555576297804d57bhiremath data.dd_func = func;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath data.dd_prop = ZPOOL_CONFIG_DEVID;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath data.dd_found = B_FALSE;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath data.dd_isdisk = wholedisk;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath (void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala devid_str_free(devidstr);
1cfa752f4e24c34133009b0f6c139127a5c461deRamaswamy Tummala devid_free(devid);
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath return (data.dd_found);
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath}
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath/*
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * This function is called when we receive a devfs add event. This can be
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * either a disk event or a lofi event, and the behavior is slightly different
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath * depending on which it is.
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath */
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremathstatic int
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremathzfs_deliver_add(nvlist_t *nvl, boolean_t is_lofi)
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath{
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath char *devpath, *devname;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath char path[PATH_MAX], realpath[PATH_MAX];
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath char *colon, *raw;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath int ret;
d3a82192edbbe93c6027629b50fd93fed5d0e1abShantkumar Hiremath
015f8fff605f2fbd5fd0072e555576297804d57bhiremath /*
015f8fff605f2fbd5fd0072e555576297804d57bhiremath * The main unit of operation is the physical device path. For disks,
* this is the device node, as all minor nodes are affected. For lofi
* devices, this includes the minor path. Unfortunately, this isn't
* represented in the DEV_PHYS_PATH for various reasons.
*/
if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devpath) != 0)
return (-1);
/*
* If this is a lofi device, then also get the minor instance name.
* Unfortunately, the current payload doesn't include an easy way to get
* this information. So we cheat by resolving the 'dev_name' (which
* refers to the raw device) and taking the portion between ':(*),raw'.
*/
(void) strlcpy(realpath, devpath, sizeof (realpath));
if (is_lofi) {
if (nvlist_lookup_string(nvl, DEV_NAME,
&devname) == 0 &&
(ret = resolvepath(devname, path,
sizeof (path))) > 0) {
path[ret] = '\0';
colon = strchr(path, ':');
if (colon != NULL)
raw = strstr(colon + 1, ",raw");
if (colon != NULL && raw != NULL) {
*raw = '\0';
(void) snprintf(realpath,
sizeof (realpath), "%s%s",
devpath, colon);
*raw = ',';
}
}
}
/*
* Iterate over all vdevs with a matching devid, and then those with a
* matching /devices path. For disks, we only want to pay attention to
* vdevs marked as whole disks. For lofi, we don't care (because we're
* matching an exact minor name).
*/
if (!devid_iter(realpath, zfs_process_add, !is_lofi))
(void) devpath_iter(realpath, zfs_process_add, !is_lofi);
return (0);
}
/*
* Called when we receive a VDEV_CHECK event, which indicates a device could not
* be opened during initial pool open, but the autoreplace property was set on
* the pool. In this case, we treat it as if it were an add event.
*/
static int
zfs_deliver_check(nvlist_t *nvl)
{
dev_data_t data = { 0 };
if (nvlist_lookup_uint64(nvl, ZFS_EV_POOL_GUID,
&data.dd_pool_guid) != 0 ||
nvlist_lookup_uint64(nvl, ZFS_EV_VDEV_GUID,
&data.dd_vdev_guid) != 0)
return (0);
data.dd_isdisk = B_TRUE;
data.dd_func = zfs_process_add;
(void) zpool_iter(g_zfshdl, zfs_iter_pool, &data);
return (0);
}
#define DEVICE_PREFIX "/devices"
static int
zfsdle_vdev_online(zpool_handle_t *zhp, void *data)
{
char *devname = data;
boolean_t avail_spare, l2cache;
vdev_state_t newstate;
nvlist_t *tgt;
syseventd_print(9, "zfsdle_vdev_online: searching for %s in pool %s\n",
devname, zpool_get_name(zhp));
if ((tgt = zpool_find_vdev_by_physpath(zhp, devname,
&avail_spare, &l2cache, NULL)) != NULL) {
char *path, fullpath[MAXPATHLEN];
uint64_t wholedisk = 0ULL;
verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
&path) == 0);
verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
&wholedisk) == 0);
(void) strlcpy(fullpath, path, sizeof (fullpath));
if (wholedisk)
fullpath[strlen(fullpath) - 2] = '\0';
if (zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
syseventd_print(9, "zfsdle_vdev_online: setting device"
" device %s to ONLINE state in pool %s.\n",
fullpath, zpool_get_name(zhp));
if (zpool_get_state(zhp) != POOL_STATE_UNAVAIL)
(void) zpool_vdev_online(zhp, fullpath, 0,
&newstate);
}
zpool_close(zhp);
return (1);
}
zpool_close(zhp);
return (0);
}
int
zfs_deliver_dle(nvlist_t *nvl)
{
char *devname;
if (nvlist_lookup_string(nvl, DEV_PHYS_PATH, &devname) != 0) {
syseventd_print(9, "zfs_deliver_event: no physpath\n");
return (-1);
}
if (strncmp(devname, DEVICE_PREFIX, strlen(DEVICE_PREFIX)) != 0) {
syseventd_print(9, "zfs_deliver_event: invalid "
"device '%s'", devname);
return (-1);
}
/*
* We try to find the device using the physical
* path that has been supplied. We need to strip off
* the /devices prefix before starting our search.
*/
devname += strlen(DEVICE_PREFIX);
if (zpool_iter(g_zfshdl, zfsdle_vdev_online, devname) != 1) {
syseventd_print(9, "zfs_deliver_event: device '%s' not"
" found\n", devname);
return (1);
}
return (0);
}
/*ARGSUSED*/
static int
zfs_deliver_event(sysevent_t *ev, int unused)
{
const char *class = sysevent_get_class_name(ev);
const char *subclass = sysevent_get_subclass_name(ev);
nvlist_t *nvl;
int ret;
boolean_t is_lofi, is_check, is_dle = B_FALSE;
if (strcmp(class, EC_DEV_ADD) == 0) {
/*
* We're mainly interested in disk additions, but we also listen
* for new lofi devices, to allow for simplified testing.
*/
if (strcmp(subclass, ESC_DISK) == 0)
is_lofi = B_FALSE;
else if (strcmp(subclass, ESC_LOFI) == 0)
is_lofi = B_TRUE;
else
return (0);
is_check = B_FALSE;
} else if (strcmp(class, EC_ZFS) == 0 &&
strcmp(subclass, ESC_ZFS_VDEV_CHECK) == 0) {
/*
* This event signifies that a device failed to open during pool
* load, but the 'autoreplace' property was set, so we should
* pretend it's just been added.
*/
is_check = B_TRUE;
} else if (strcmp(class, EC_DEV_STATUS) == 0 &&
strcmp(subclass, ESC_DEV_DLE) == 0) {
is_dle = B_TRUE;
} else {
return (0);
}
if (sysevent_get_attr_list(ev, &nvl) != 0)
return (-1);
if (is_dle)
ret = zfs_deliver_dle(nvl);
else if (is_check)
ret = zfs_deliver_check(nvl);
else
ret = zfs_deliver_add(nvl, is_lofi);
nvlist_free(nvl);
return (ret);
}
static struct slm_mod_ops zfs_mod_ops = {
SE_MAJOR_VERSION, SE_MINOR_VERSION, 10, zfs_deliver_event
};
struct slm_mod_ops *
slm_init()
{
if ((g_zfshdl = libzfs_init()) == NULL)
return (NULL);
/* collect a list of unavailable pools */
list_create(&g_pool_list, sizeof (struct unavailpool),
offsetof(struct unavailpool, uap_node));
(void) zpool_iter(g_zfshdl, zfs_unavail_pool, (void *)&g_pool_list);
if (!list_is_empty(&g_pool_list))
g_tpool = tpool_create(1, sysconf(_SC_NPROCESSORS_ONLN),
0, NULL);
return (&zfs_mod_ops);
}
void
slm_fini()
{
unavailpool_t *pool;
if (g_tpool) {
tpool_wait(g_tpool);
tpool_destroy(g_tpool);
}
while ((pool = (list_head(&g_pool_list))) != NULL) {
list_remove(&g_pool_list, pool);
zpool_close(pool->uap_zhp);
free(pool);
}
list_destroy(&g_pool_list);
libzfs_fini(g_zfshdl);
}