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