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