1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2021 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2016 Toomas Soome <tsoome@me.com>
28 * Copyright 2017 Joyent, Inc.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2019, Datto Inc. All rights reserved.
31 * Copyright (c) 2021, Klara Inc.
32 * Copyright (c) 2021, 2023 Hewlett Packard Enterprise Development LP.
33 */
34
35 #include <sys/zfs_context.h>
36 #include <sys/fm/fs/zfs.h>
37 #include <sys/spa.h>
38 #include <sys/spa_impl.h>
39 #include <sys/bpobj.h>
40 #include <sys/dmu.h>
41 #include <sys/dmu_tx.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/vdev_rebuild.h>
45 #include <sys/vdev_draid.h>
46 #include <sys/uberblock_impl.h>
47 #include <sys/metaslab.h>
48 #include <sys/metaslab_impl.h>
49 #include <sys/space_map.h>
50 #include <sys/space_reftree.h>
51 #include <sys/zio.h>
52 #include <sys/zap.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/arc.h>
55 #include <sys/zil.h>
56 #include <sys/dsl_scan.h>
57 #include <sys/vdev_raidz.h>
58 #include <sys/abd.h>
59 #include <sys/vdev_initialize.h>
60 #include <sys/vdev_trim.h>
61 #include <sys/vdev_raidz.h>
62 #include <sys/zvol.h>
63 #include <sys/zfs_ratelimit.h>
64 #include "zfs_prop.h"
65
66 /*
67 * One metaslab from each (normal-class) vdev is used by the ZIL. These are
68 * called "embedded slog metaslabs", are referenced by vdev_log_mg, and are
69 * part of the spa_embedded_log_class. The metaslab with the most free space
70 * in each vdev is selected for this purpose when the pool is opened (or a
71 * vdev is added). See vdev_metaslab_init().
72 *
73 * Log blocks can be allocated from the following locations. Each one is tried
74 * in order until the allocation succeeds:
75 * 1. dedicated log vdevs, aka "slog" (spa_log_class)
76 * 2. embedded slog metaslabs (spa_embedded_log_class)
77 * 3. other metaslabs in normal vdevs (spa_normal_class)
78 *
79 * zfs_embedded_slog_min_ms disables the embedded slog if there are fewer
80 * than this number of metaslabs in the vdev. This ensures that we don't set
81 * aside an unreasonable amount of space for the ZIL. If set to less than
82 * 1 << (spa_slop_shift + 1), on small pools the usable space may be reduced
83 * (by more than 1<<spa_slop_shift) due to the embedded slog metaslab.
84 */
85 static uint_t zfs_embedded_slog_min_ms = 64;
86
87 /* default target for number of metaslabs per top-level vdev */
88 static uint_t zfs_vdev_default_ms_count = 200;
89
90 /* minimum number of metaslabs per top-level vdev */
91 static uint_t zfs_vdev_min_ms_count = 16;
92
93 /* practical upper limit of total metaslabs per top-level vdev */
94 static uint_t zfs_vdev_ms_count_limit = 1ULL << 17;
95
96 /* lower limit for metaslab size (512M) */
97 static uint_t zfs_vdev_default_ms_shift = 29;
98
99 /* upper limit for metaslab size (16G) */
100 static uint_t zfs_vdev_max_ms_shift = 34;
101
102 int vdev_validate_skip = B_FALSE;
103
104 /*
105 * Since the DTL space map of a vdev is not expected to have a lot of
106 * entries, we default its block size to 4K.
107 */
108 int zfs_vdev_dtl_sm_blksz = (1 << 12);
109
110 /*
111 * Rate limit slow IO (delay) events to this many per second.
112 */
113 static unsigned int zfs_slow_io_events_per_second = 20;
114
115 /*
116 * Rate limit deadman "hung IO" events to this many per second.
117 */
118 static unsigned int zfs_deadman_events_per_second = 1;
119
120 /*
121 * Rate limit direct write IO verify failures to this many per scond.
122 */
123 static unsigned int zfs_dio_write_verify_events_per_second = 20;
124
125 /*
126 * Rate limit checksum events after this many checksum errors per second.
127 */
128 static unsigned int zfs_checksum_events_per_second = 20;
129
130 /*
131 * Ignore errors during scrub/resilver. Allows to work around resilver
132 * upon import when there are pool errors.
133 */
134 static int zfs_scan_ignore_errors = 0;
135
136 /*
137 * vdev-wide space maps that have lots of entries written to them at
138 * the end of each transaction can benefit from a higher I/O bandwidth
139 * (e.g. vdev_obsolete_sm), thus we default their block size to 128K.
140 */
141 int zfs_vdev_standard_sm_blksz = (1 << 17);
142
143 /*
144 * Tunable parameter for debugging or performance analysis. Setting this
145 * will cause pool corruption on power loss if a volatile out-of-order
146 * write cache is enabled.
147 */
148 int zfs_nocacheflush = 0;
149
150 /*
151 * Maximum and minimum ashift values that can be automatically set based on
152 * vdev's physical ashift (disk's physical sector size). While ASHIFT_MAX
153 * is higher than the maximum value, it is intentionally limited here to not
154 * excessively impact pool space efficiency. Higher ashift values may still
155 * be forced by vdev logical ashift or by user via ashift property, but won't
156 * be set automatically as a performance optimization.
157 */
158 uint_t zfs_vdev_max_auto_ashift = 14;
159 uint_t zfs_vdev_min_auto_ashift = ASHIFT_MIN;
160
161 /*
162 * VDEV checksum verification for Direct I/O writes. This is neccessary for
163 * Linux, because anonymous pages can not be placed under write protection
164 * during Direct I/O writes.
165 */
166 #if !defined(__FreeBSD__)
167 uint_t zfs_vdev_direct_write_verify = 1;
168 #else
169 uint_t zfs_vdev_direct_write_verify = 0;
170 #endif
171
172 void
vdev_dbgmsg(vdev_t * vd,const char * fmt,...)173 vdev_dbgmsg(vdev_t *vd, const char *fmt, ...)
174 {
175 va_list adx;
176 char buf[256];
177
178 va_start(adx, fmt);
179 (void) vsnprintf(buf, sizeof (buf), fmt, adx);
180 va_end(adx);
181
182 if (vd->vdev_path != NULL) {
183 zfs_dbgmsg("%s vdev '%s': %s", vd->vdev_ops->vdev_op_type,
184 vd->vdev_path, buf);
185 } else {
186 zfs_dbgmsg("%s-%llu vdev (guid %llu): %s",
187 vd->vdev_ops->vdev_op_type,
188 (u_longlong_t)vd->vdev_id,
189 (u_longlong_t)vd->vdev_guid, buf);
190 }
191 }
192
193 void
vdev_dbgmsg_print_tree(vdev_t * vd,int indent)194 vdev_dbgmsg_print_tree(vdev_t *vd, int indent)
195 {
196 char state[20];
197
198 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops) {
199 zfs_dbgmsg("%*svdev %llu: %s", indent, "",
200 (u_longlong_t)vd->vdev_id,
201 vd->vdev_ops->vdev_op_type);
202 return;
203 }
204
205 switch (vd->vdev_state) {
206 case VDEV_STATE_UNKNOWN:
207 (void) snprintf(state, sizeof (state), "unknown");
208 break;
209 case VDEV_STATE_CLOSED:
210 (void) snprintf(state, sizeof (state), "closed");
211 break;
212 case VDEV_STATE_OFFLINE:
213 (void) snprintf(state, sizeof (state), "offline");
214 break;
215 case VDEV_STATE_REMOVED:
216 (void) snprintf(state, sizeof (state), "removed");
217 break;
218 case VDEV_STATE_CANT_OPEN:
219 (void) snprintf(state, sizeof (state), "can't open");
220 break;
221 case VDEV_STATE_FAULTED:
222 (void) snprintf(state, sizeof (state), "faulted");
223 break;
224 case VDEV_STATE_DEGRADED:
225 (void) snprintf(state, sizeof (state), "degraded");
226 break;
227 case VDEV_STATE_HEALTHY:
228 (void) snprintf(state, sizeof (state), "healthy");
229 break;
230 default:
231 (void) snprintf(state, sizeof (state), "<state %u>",
232 (uint_t)vd->vdev_state);
233 }
234
235 zfs_dbgmsg("%*svdev %u: %s%s, guid: %llu, path: %s, %s", indent,
236 "", (int)vd->vdev_id, vd->vdev_ops->vdev_op_type,
237 vd->vdev_islog ? " (log)" : "",
238 (u_longlong_t)vd->vdev_guid,
239 vd->vdev_path ? vd->vdev_path : "N/A", state);
240
241 for (uint64_t i = 0; i < vd->vdev_children; i++)
242 vdev_dbgmsg_print_tree(vd->vdev_child[i], indent + 2);
243 }
244
245 /*
246 * Virtual device management.
247 */
248
249 static vdev_ops_t *const vdev_ops_table[] = {
250 &vdev_root_ops,
251 &vdev_raidz_ops,
252 &vdev_draid_ops,
253 &vdev_draid_spare_ops,
254 &vdev_mirror_ops,
255 &vdev_replacing_ops,
256 &vdev_spare_ops,
257 &vdev_disk_ops,
258 &vdev_file_ops,
259 &vdev_missing_ops,
260 &vdev_hole_ops,
261 &vdev_indirect_ops,
262 NULL
263 };
264
265 /*
266 * Given a vdev type, return the appropriate ops vector.
267 */
268 static vdev_ops_t *
vdev_getops(const char * type)269 vdev_getops(const char *type)
270 {
271 vdev_ops_t *ops, *const *opspp;
272
273 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
274 if (strcmp(ops->vdev_op_type, type) == 0)
275 break;
276
277 return (ops);
278 }
279
280 /*
281 * Given a vdev and a metaslab class, find which metaslab group we're
282 * interested in. All vdevs may belong to two different metaslab classes.
283 * Dedicated slog devices use only the primary metaslab group, rather than a
284 * separate log group. For embedded slogs, the vdev_log_mg will be non-NULL.
285 */
286 metaslab_group_t *
vdev_get_mg(vdev_t * vd,metaslab_class_t * mc)287 vdev_get_mg(vdev_t *vd, metaslab_class_t *mc)
288 {
289 if (mc == spa_embedded_log_class(vd->vdev_spa) &&
290 vd->vdev_log_mg != NULL)
291 return (vd->vdev_log_mg);
292 else
293 return (vd->vdev_mg);
294 }
295
296 void
vdev_default_xlate(vdev_t * vd,const zfs_range_seg64_t * logical_rs,zfs_range_seg64_t * physical_rs,zfs_range_seg64_t * remain_rs)297 vdev_default_xlate(vdev_t *vd, const zfs_range_seg64_t *logical_rs,
298 zfs_range_seg64_t *physical_rs, zfs_range_seg64_t *remain_rs)
299 {
300 (void) vd, (void) remain_rs;
301
302 physical_rs->rs_start = logical_rs->rs_start;
303 physical_rs->rs_end = logical_rs->rs_end;
304 }
305
306 /*
307 * Derive the enumerated allocation bias from string input.
308 * String origin is either the per-vdev zap or zpool(8).
309 */
310 static vdev_alloc_bias_t
vdev_derive_alloc_bias(const char * bias)311 vdev_derive_alloc_bias(const char *bias)
312 {
313 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
314
315 if (strcmp(bias, VDEV_ALLOC_BIAS_LOG) == 0)
316 alloc_bias = VDEV_BIAS_LOG;
317 else if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
318 alloc_bias = VDEV_BIAS_SPECIAL;
319 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
320 alloc_bias = VDEV_BIAS_DEDUP;
321
322 return (alloc_bias);
323 }
324
325 /*
326 * Default asize function: return the MAX of psize with the asize of
327 * all children. This is what's used by anything other than RAID-Z.
328 */
329 uint64_t
vdev_default_asize(vdev_t * vd,uint64_t psize,uint64_t txg)330 vdev_default_asize(vdev_t *vd, uint64_t psize, uint64_t txg)
331 {
332 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
333 uint64_t csize;
334
335 for (int c = 0; c < vd->vdev_children; c++) {
336 csize = vdev_psize_to_asize_txg(vd->vdev_child[c], psize, txg);
337 asize = MAX(asize, csize);
338 }
339
340 return (asize);
341 }
342
343 uint64_t
vdev_default_min_asize(vdev_t * vd)344 vdev_default_min_asize(vdev_t *vd)
345 {
346 return (vd->vdev_min_asize);
347 }
348
349 /*
350 * Get the minimum allocatable size. We define the allocatable size as
351 * the vdev's asize rounded to the nearest metaslab. This allows us to
352 * replace or attach devices which don't have the same physical size but
353 * can still satisfy the same number of allocations.
354 */
355 uint64_t
vdev_get_min_asize(vdev_t * vd)356 vdev_get_min_asize(vdev_t *vd)
357 {
358 vdev_t *pvd = vd->vdev_parent;
359
360 /*
361 * If our parent is NULL (inactive spare or cache) or is the root,
362 * just return our own asize.
363 */
364 if (pvd == NULL)
365 return (vd->vdev_asize);
366
367 /*
368 * The top-level vdev just returns the allocatable size rounded
369 * to the nearest metaslab.
370 */
371 if (vd == vd->vdev_top)
372 return (P2ALIGN_TYPED(vd->vdev_asize, 1ULL << vd->vdev_ms_shift,
373 uint64_t));
374
375 return (pvd->vdev_ops->vdev_op_min_asize(pvd));
376 }
377
378 void
vdev_set_min_asize(vdev_t * vd)379 vdev_set_min_asize(vdev_t *vd)
380 {
381 vd->vdev_min_asize = vdev_get_min_asize(vd);
382
383 for (int c = 0; c < vd->vdev_children; c++)
384 vdev_set_min_asize(vd->vdev_child[c]);
385 }
386
387 /*
388 * Get the minimal allocation size for the top-level vdev.
389 */
390 uint64_t
vdev_get_min_alloc(vdev_t * vd)391 vdev_get_min_alloc(vdev_t *vd)
392 {
393 uint64_t min_alloc = 1ULL << vd->vdev_ashift;
394
395 if (vd->vdev_ops->vdev_op_min_alloc != NULL)
396 min_alloc = vd->vdev_ops->vdev_op_min_alloc(vd);
397
398 return (min_alloc);
399 }
400
401 /*
402 * Get the parity level for a top-level vdev.
403 */
404 uint64_t
vdev_get_nparity(vdev_t * vd)405 vdev_get_nparity(vdev_t *vd)
406 {
407 uint64_t nparity = 0;
408
409 if (vd->vdev_ops->vdev_op_nparity != NULL)
410 nparity = vd->vdev_ops->vdev_op_nparity(vd);
411
412 return (nparity);
413 }
414
415 static int
vdev_prop_get_int(vdev_t * vd,vdev_prop_t prop,uint64_t * value)416 vdev_prop_get_int(vdev_t *vd, vdev_prop_t prop, uint64_t *value)
417 {
418 spa_t *spa = vd->vdev_spa;
419 objset_t *mos = spa->spa_meta_objset;
420 uint64_t objid;
421 int err;
422
423 if (vd->vdev_root_zap != 0) {
424 objid = vd->vdev_root_zap;
425 } else if (vd->vdev_top_zap != 0) {
426 objid = vd->vdev_top_zap;
427 } else if (vd->vdev_leaf_zap != 0) {
428 objid = vd->vdev_leaf_zap;
429 } else {
430 return (EINVAL);
431 }
432
433 err = zap_lookup(mos, objid, vdev_prop_to_name(prop),
434 sizeof (uint64_t), 1, value);
435
436 if (err == ENOENT)
437 *value = vdev_prop_default_numeric(prop);
438
439 return (err);
440 }
441
442 /*
443 * Get the number of data disks for a top-level vdev.
444 */
445 uint64_t
vdev_get_ndisks(vdev_t * vd)446 vdev_get_ndisks(vdev_t *vd)
447 {
448 uint64_t ndisks = 1;
449
450 if (vd->vdev_ops->vdev_op_ndisks != NULL)
451 ndisks = vd->vdev_ops->vdev_op_ndisks(vd);
452
453 return (ndisks);
454 }
455
456 vdev_t *
vdev_lookup_top(spa_t * spa,uint64_t vdev)457 vdev_lookup_top(spa_t *spa, uint64_t vdev)
458 {
459 vdev_t *rvd = spa->spa_root_vdev;
460
461 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
462
463 if (vdev < rvd->vdev_children) {
464 ASSERT(rvd->vdev_child[vdev] != NULL);
465 return (rvd->vdev_child[vdev]);
466 }
467
468 return (NULL);
469 }
470
471 vdev_t *
vdev_lookup_by_guid(vdev_t * vd,uint64_t guid)472 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
473 {
474 vdev_t *mvd;
475
476 if (vd->vdev_guid == guid)
477 return (vd);
478
479 for (int c = 0; c < vd->vdev_children; c++)
480 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
481 NULL)
482 return (mvd);
483
484 return (NULL);
485 }
486
487 static int
vdev_count_leaves_impl(vdev_t * vd)488 vdev_count_leaves_impl(vdev_t *vd)
489 {
490 int n = 0;
491
492 if (vd->vdev_ops->vdev_op_leaf)
493 return (1);
494
495 for (int c = 0; c < vd->vdev_children; c++)
496 n += vdev_count_leaves_impl(vd->vdev_child[c]);
497
498 return (n);
499 }
500
501 int
vdev_count_leaves(spa_t * spa)502 vdev_count_leaves(spa_t *spa)
503 {
504 int rc;
505
506 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
507 rc = vdev_count_leaves_impl(spa->spa_root_vdev);
508 spa_config_exit(spa, SCL_VDEV, FTAG);
509
510 return (rc);
511 }
512
513 void
vdev_add_child(vdev_t * pvd,vdev_t * cvd)514 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
515 {
516 size_t oldsize, newsize;
517 uint64_t id = cvd->vdev_id;
518 vdev_t **newchild;
519
520 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
521 ASSERT(cvd->vdev_parent == NULL);
522
523 cvd->vdev_parent = pvd;
524
525 if (pvd == NULL)
526 return;
527
528 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
529
530 oldsize = pvd->vdev_children * sizeof (vdev_t *);
531 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
532 newsize = pvd->vdev_children * sizeof (vdev_t *);
533
534 newchild = kmem_alloc(newsize, KM_SLEEP);
535 if (pvd->vdev_child != NULL) {
536 memcpy(newchild, pvd->vdev_child, oldsize);
537 kmem_free(pvd->vdev_child, oldsize);
538 }
539
540 pvd->vdev_child = newchild;
541 pvd->vdev_child[id] = cvd;
542
543 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
544 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
545
546 /*
547 * Walk up all ancestors to update guid sum.
548 */
549 for (; pvd != NULL; pvd = pvd->vdev_parent)
550 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
551
552 if (cvd->vdev_ops->vdev_op_leaf) {
553 list_insert_head(&cvd->vdev_spa->spa_leaf_list, cvd);
554 cvd->vdev_spa->spa_leaf_list_gen++;
555 }
556 }
557
558 void
vdev_remove_child(vdev_t * pvd,vdev_t * cvd)559 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
560 {
561 int c;
562 uint_t id = cvd->vdev_id;
563
564 ASSERT(cvd->vdev_parent == pvd);
565
566 if (pvd == NULL)
567 return;
568
569 ASSERT(id < pvd->vdev_children);
570 ASSERT(pvd->vdev_child[id] == cvd);
571
572 pvd->vdev_child[id] = NULL;
573 cvd->vdev_parent = NULL;
574
575 for (c = 0; c < pvd->vdev_children; c++)
576 if (pvd->vdev_child[c])
577 break;
578
579 if (c == pvd->vdev_children) {
580 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
581 pvd->vdev_child = NULL;
582 pvd->vdev_children = 0;
583 }
584
585 if (cvd->vdev_ops->vdev_op_leaf) {
586 spa_t *spa = cvd->vdev_spa;
587 list_remove(&spa->spa_leaf_list, cvd);
588 spa->spa_leaf_list_gen++;
589 }
590
591 /*
592 * Walk up all ancestors to update guid sum.
593 */
594 for (; pvd != NULL; pvd = pvd->vdev_parent)
595 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
596 }
597
598 /*
599 * Remove any holes in the child array.
600 */
601 void
vdev_compact_children(vdev_t * pvd)602 vdev_compact_children(vdev_t *pvd)
603 {
604 vdev_t **newchild, *cvd;
605 int oldc = pvd->vdev_children;
606 int newc;
607
608 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
609
610 if (oldc == 0)
611 return;
612
613 for (int c = newc = 0; c < oldc; c++)
614 if (pvd->vdev_child[c])
615 newc++;
616
617 if (newc > 0) {
618 newchild = kmem_zalloc(newc * sizeof (vdev_t *), KM_SLEEP);
619
620 for (int c = newc = 0; c < oldc; c++) {
621 if ((cvd = pvd->vdev_child[c]) != NULL) {
622 newchild[newc] = cvd;
623 cvd->vdev_id = newc++;
624 }
625 }
626 } else {
627 newchild = NULL;
628 }
629
630 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
631 pvd->vdev_child = newchild;
632 pvd->vdev_children = newc;
633 }
634
635 /*
636 * Allocate and minimally initialize a vdev_t.
637 */
638 vdev_t *
vdev_alloc_common(spa_t * spa,uint_t id,uint64_t guid,vdev_ops_t * ops)639 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
640 {
641 vdev_t *vd;
642 vdev_indirect_config_t *vic;
643
644 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
645 vic = &vd->vdev_indirect_config;
646
647 if (spa->spa_root_vdev == NULL) {
648 ASSERT(ops == &vdev_root_ops);
649 spa->spa_root_vdev = vd;
650 spa->spa_load_guid = spa_generate_load_guid();
651 }
652
653 if (guid == 0 && ops != &vdev_hole_ops) {
654 if (spa->spa_root_vdev == vd) {
655 /*
656 * The root vdev's guid will also be the pool guid,
657 * which must be unique among all pools.
658 */
659 guid = spa_generate_guid(NULL);
660 } else {
661 /*
662 * Any other vdev's guid must be unique within the pool.
663 */
664 guid = spa_generate_guid(spa);
665 }
666 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
667 }
668
669 vd->vdev_spa = spa;
670 vd->vdev_id = id;
671 vd->vdev_guid = guid;
672 vd->vdev_guid_sum = guid;
673 vd->vdev_ops = ops;
674 vd->vdev_state = VDEV_STATE_CLOSED;
675 vd->vdev_ishole = (ops == &vdev_hole_ops);
676 vic->vic_prev_indirect_vdev = UINT64_MAX;
677
678 rw_init(&vd->vdev_indirect_rwlock, NULL, RW_DEFAULT, NULL);
679 mutex_init(&vd->vdev_obsolete_lock, NULL, MUTEX_DEFAULT, NULL);
680 vd->vdev_obsolete_segments = zfs_range_tree_create(NULL,
681 ZFS_RANGE_SEG64, NULL, 0, 0);
682
683 /*
684 * Initialize rate limit structs for events. We rate limit ZIO delay
685 * and checksum events so that we don't overwhelm ZED with thousands
686 * of events when a disk is acting up.
687 */
688 zfs_ratelimit_init(&vd->vdev_delay_rl, &zfs_slow_io_events_per_second,
689 1);
690 zfs_ratelimit_init(&vd->vdev_deadman_rl, &zfs_deadman_events_per_second,
691 1);
692 zfs_ratelimit_init(&vd->vdev_dio_verify_rl,
693 &zfs_dio_write_verify_events_per_second, 1);
694 zfs_ratelimit_init(&vd->vdev_checksum_rl,
695 &zfs_checksum_events_per_second, 1);
696
697 /*
698 * Default Thresholds for tuning ZED
699 */
700 vd->vdev_checksum_n = vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_N);
701 vd->vdev_checksum_t = vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_T);
702 vd->vdev_io_n = vdev_prop_default_numeric(VDEV_PROP_IO_N);
703 vd->vdev_io_t = vdev_prop_default_numeric(VDEV_PROP_IO_T);
704 vd->vdev_slow_io_n = vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_N);
705 vd->vdev_slow_io_t = vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_T);
706
707 list_link_init(&vd->vdev_config_dirty_node);
708 list_link_init(&vd->vdev_state_dirty_node);
709 list_link_init(&vd->vdev_initialize_node);
710 list_link_init(&vd->vdev_leaf_node);
711 list_link_init(&vd->vdev_trim_node);
712
713 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_NOLOCKDEP, NULL);
714 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
715 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
716 mutex_init(&vd->vdev_scan_io_queue_lock, NULL, MUTEX_DEFAULT, NULL);
717
718 mutex_init(&vd->vdev_initialize_lock, NULL, MUTEX_DEFAULT, NULL);
719 mutex_init(&vd->vdev_initialize_io_lock, NULL, MUTEX_DEFAULT, NULL);
720 cv_init(&vd->vdev_initialize_cv, NULL, CV_DEFAULT, NULL);
721 cv_init(&vd->vdev_initialize_io_cv, NULL, CV_DEFAULT, NULL);
722
723 mutex_init(&vd->vdev_trim_lock, NULL, MUTEX_DEFAULT, NULL);
724 mutex_init(&vd->vdev_autotrim_lock, NULL, MUTEX_DEFAULT, NULL);
725 mutex_init(&vd->vdev_trim_io_lock, NULL, MUTEX_DEFAULT, NULL);
726 cv_init(&vd->vdev_trim_cv, NULL, CV_DEFAULT, NULL);
727 cv_init(&vd->vdev_autotrim_cv, NULL, CV_DEFAULT, NULL);
728 cv_init(&vd->vdev_autotrim_kick_cv, NULL, CV_DEFAULT, NULL);
729 cv_init(&vd->vdev_trim_io_cv, NULL, CV_DEFAULT, NULL);
730
731 mutex_init(&vd->vdev_rebuild_lock, NULL, MUTEX_DEFAULT, NULL);
732 cv_init(&vd->vdev_rebuild_cv, NULL, CV_DEFAULT, NULL);
733
734 for (int t = 0; t < DTL_TYPES; t++) {
735 vd->vdev_dtl[t] = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64,
736 NULL, 0, 0);
737 }
738
739 txg_list_create(&vd->vdev_ms_list, spa,
740 offsetof(struct metaslab, ms_txg_node));
741 txg_list_create(&vd->vdev_dtl_list, spa,
742 offsetof(struct vdev, vdev_dtl_node));
743 vd->vdev_stat.vs_timestamp = gethrtime();
744 vdev_queue_init(vd);
745
746 return (vd);
747 }
748
749 /*
750 * Allocate a new vdev. The 'alloctype' is used to control whether we are
751 * creating a new vdev or loading an existing one - the behavior is slightly
752 * different for each case.
753 */
754 int
vdev_alloc(spa_t * spa,vdev_t ** vdp,nvlist_t * nv,vdev_t * parent,uint_t id,int alloctype)755 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
756 int alloctype)
757 {
758 vdev_ops_t *ops;
759 const char *type;
760 uint64_t guid = 0, islog;
761 vdev_t *vd;
762 vdev_indirect_config_t *vic;
763 const char *tmp = NULL;
764 int rc;
765 vdev_alloc_bias_t alloc_bias = VDEV_BIAS_NONE;
766 boolean_t top_level = (parent && !parent->vdev_parent);
767
768 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
769
770 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
771 return (SET_ERROR(EINVAL));
772
773 if ((ops = vdev_getops(type)) == NULL)
774 return (SET_ERROR(EINVAL));
775
776 /*
777 * If this is a load, get the vdev guid from the nvlist.
778 * Otherwise, vdev_alloc_common() will generate one for us.
779 */
780 if (alloctype == VDEV_ALLOC_LOAD) {
781 uint64_t label_id;
782
783 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
784 label_id != id)
785 return (SET_ERROR(EINVAL));
786
787 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
788 return (SET_ERROR(EINVAL));
789 } else if (alloctype == VDEV_ALLOC_SPARE) {
790 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
791 return (SET_ERROR(EINVAL));
792 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
793 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
794 return (SET_ERROR(EINVAL));
795 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
796 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
797 return (SET_ERROR(EINVAL));
798 }
799
800 /*
801 * The first allocated vdev must be of type 'root'.
802 */
803 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
804 return (SET_ERROR(EINVAL));
805
806 /*
807 * Determine whether we're a log vdev.
808 */
809 islog = 0;
810 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
811 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
812 return (SET_ERROR(ENOTSUP));
813
814 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
815 return (SET_ERROR(ENOTSUP));
816
817 if (top_level && alloctype == VDEV_ALLOC_ADD) {
818 const char *bias;
819
820 /*
821 * If creating a top-level vdev, check for allocation
822 * classes input.
823 */
824 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
825 &bias) == 0) {
826 alloc_bias = vdev_derive_alloc_bias(bias);
827
828 /* spa_vdev_add() expects feature to be enabled */
829 if (spa->spa_load_state != SPA_LOAD_CREATE &&
830 !spa_feature_is_enabled(spa,
831 SPA_FEATURE_ALLOCATION_CLASSES)) {
832 return (SET_ERROR(ENOTSUP));
833 }
834 }
835
836 /* spa_vdev_add() expects feature to be enabled */
837 if (ops == &vdev_draid_ops &&
838 spa->spa_load_state != SPA_LOAD_CREATE &&
839 !spa_feature_is_enabled(spa, SPA_FEATURE_DRAID)) {
840 return (SET_ERROR(ENOTSUP));
841 }
842 }
843
844 /*
845 * Initialize the vdev specific data. This is done before calling
846 * vdev_alloc_common() since it may fail and this simplifies the
847 * error reporting and cleanup code paths.
848 */
849 void *tsd = NULL;
850 if (ops->vdev_op_init != NULL) {
851 rc = ops->vdev_op_init(spa, nv, &tsd);
852 if (rc != 0) {
853 return (rc);
854 }
855 }
856
857 vd = vdev_alloc_common(spa, id, guid, ops);
858 vd->vdev_tsd = tsd;
859 vd->vdev_islog = islog;
860
861 if (top_level && alloc_bias != VDEV_BIAS_NONE)
862 vd->vdev_alloc_bias = alloc_bias;
863
864 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &tmp) == 0)
865 vd->vdev_path = spa_strdup(tmp);
866
867 /*
868 * ZPOOL_CONFIG_AUX_STATE = "external" means we previously forced a
869 * fault on a vdev and want it to persist across imports (like with
870 * zpool offline -f).
871 */
872 rc = nvlist_lookup_string(nv, ZPOOL_CONFIG_AUX_STATE, &tmp);
873 if (rc == 0 && tmp != NULL && strcmp(tmp, "external") == 0) {
874 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
875 vd->vdev_faulted = 1;
876 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
877 }
878
879 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &tmp) == 0)
880 vd->vdev_devid = spa_strdup(tmp);
881 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH, &tmp) == 0)
882 vd->vdev_physpath = spa_strdup(tmp);
883
884 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_VDEV_ENC_SYSFS_PATH,
885 &tmp) == 0)
886 vd->vdev_enc_sysfs_path = spa_strdup(tmp);
887
888 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &tmp) == 0)
889 vd->vdev_fru = spa_strdup(tmp);
890
891 /*
892 * Set the whole_disk property. If it's not specified, leave the value
893 * as -1.
894 */
895 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
896 &vd->vdev_wholedisk) != 0)
897 vd->vdev_wholedisk = -1ULL;
898
899 vic = &vd->vdev_indirect_config;
900
901 ASSERT0(vic->vic_mapping_object);
902 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_OBJECT,
903 &vic->vic_mapping_object);
904 ASSERT0(vic->vic_births_object);
905 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_INDIRECT_BIRTHS,
906 &vic->vic_births_object);
907 ASSERT3U(vic->vic_prev_indirect_vdev, ==, UINT64_MAX);
908 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_PREV_INDIRECT_VDEV,
909 &vic->vic_prev_indirect_vdev);
910
911 /*
912 * Look for the 'not present' flag. This will only be set if the device
913 * was not present at the time of import.
914 */
915 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
916 &vd->vdev_not_present);
917
918 /*
919 * Get the alignment requirement. Ignore pool ashift for vdev
920 * attach case.
921 */
922 if (alloctype != VDEV_ALLOC_ATTACH) {
923 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT,
924 &vd->vdev_ashift);
925 } else {
926 vd->vdev_attaching = B_TRUE;
927 }
928
929 /*
930 * Retrieve the vdev creation time.
931 */
932 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
933 &vd->vdev_crtxg);
934
935 if (vd->vdev_ops == &vdev_root_ops &&
936 (alloctype == VDEV_ALLOC_LOAD ||
937 alloctype == VDEV_ALLOC_SPLIT ||
938 alloctype == VDEV_ALLOC_ROOTPOOL)) {
939 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_ROOT_ZAP,
940 &vd->vdev_root_zap);
941 }
942
943 /*
944 * If we're a top-level vdev, try to load the allocation parameters.
945 */
946 if (top_level &&
947 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
948 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
949 &vd->vdev_ms_array);
950 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
951 &vd->vdev_ms_shift);
952 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
953 &vd->vdev_asize);
954 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NONALLOCATING,
955 &vd->vdev_noalloc);
956 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
957 &vd->vdev_removing);
958 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
959 &vd->vdev_top_zap);
960 vd->vdev_rz_expanding = nvlist_exists(nv,
961 ZPOOL_CONFIG_RAIDZ_EXPANDING);
962 } else {
963 ASSERT0(vd->vdev_top_zap);
964 }
965
966 if (top_level && alloctype != VDEV_ALLOC_ATTACH) {
967 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
968 alloctype == VDEV_ALLOC_ADD ||
969 alloctype == VDEV_ALLOC_SPLIT ||
970 alloctype == VDEV_ALLOC_ROOTPOOL);
971 /* Note: metaslab_group_create() is now deferred */
972 }
973
974 if (vd->vdev_ops->vdev_op_leaf &&
975 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
976 (void) nvlist_lookup_uint64(nv,
977 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
978 } else {
979 ASSERT0(vd->vdev_leaf_zap);
980 }
981
982 /*
983 * If we're a leaf vdev, try to load the DTL object and other state.
984 */
985
986 if (vd->vdev_ops->vdev_op_leaf &&
987 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
988 alloctype == VDEV_ALLOC_ROOTPOOL)) {
989 if (alloctype == VDEV_ALLOC_LOAD) {
990 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
991 &vd->vdev_dtl_object);
992 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
993 &vd->vdev_unspare);
994 }
995
996 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
997 uint64_t spare = 0;
998
999 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
1000 &spare) == 0 && spare)
1001 spa_spare_add(vd);
1002 }
1003
1004 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
1005 &vd->vdev_offline);
1006
1007 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
1008 &vd->vdev_resilver_txg);
1009
1010 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REBUILD_TXG,
1011 &vd->vdev_rebuild_txg);
1012
1013 if (nvlist_exists(nv, ZPOOL_CONFIG_RESILVER_DEFER))
1014 vdev_defer_resilver(vd);
1015
1016 /*
1017 * In general, when importing a pool we want to ignore the
1018 * persistent fault state, as the diagnosis made on another
1019 * system may not be valid in the current context. The only
1020 * exception is if we forced a vdev to a persistently faulted
1021 * state with 'zpool offline -f'. The persistent fault will
1022 * remain across imports until cleared.
1023 *
1024 * Local vdevs will remain in the faulted state.
1025 */
1026 if (spa_load_state(spa) == SPA_LOAD_OPEN ||
1027 spa_load_state(spa) == SPA_LOAD_IMPORT) {
1028 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
1029 &vd->vdev_faulted);
1030 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
1031 &vd->vdev_degraded);
1032 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
1033 &vd->vdev_removed);
1034
1035 if (vd->vdev_faulted || vd->vdev_degraded) {
1036 const char *aux;
1037
1038 vd->vdev_label_aux =
1039 VDEV_AUX_ERR_EXCEEDED;
1040 if (nvlist_lookup_string(nv,
1041 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
1042 strcmp(aux, "external") == 0)
1043 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
1044 else
1045 vd->vdev_faulted = 0ULL;
1046 }
1047 }
1048 }
1049
1050 /*
1051 * Add ourselves to the parent's list of children.
1052 */
1053 vdev_add_child(parent, vd);
1054
1055 *vdp = vd;
1056
1057 return (0);
1058 }
1059
1060 void
vdev_free(vdev_t * vd)1061 vdev_free(vdev_t *vd)
1062 {
1063 spa_t *spa = vd->vdev_spa;
1064
1065 ASSERT3P(vd->vdev_initialize_thread, ==, NULL);
1066 ASSERT3P(vd->vdev_trim_thread, ==, NULL);
1067 ASSERT3P(vd->vdev_autotrim_thread, ==, NULL);
1068 ASSERT3P(vd->vdev_rebuild_thread, ==, NULL);
1069
1070 /*
1071 * Scan queues are normally destroyed at the end of a scan. If the
1072 * queue exists here, that implies the vdev is being removed while
1073 * the scan is still running.
1074 */
1075 if (vd->vdev_scan_io_queue != NULL) {
1076 mutex_enter(&vd->vdev_scan_io_queue_lock);
1077 dsl_scan_io_queue_destroy(vd->vdev_scan_io_queue);
1078 vd->vdev_scan_io_queue = NULL;
1079 mutex_exit(&vd->vdev_scan_io_queue_lock);
1080 }
1081
1082 /*
1083 * vdev_free() implies closing the vdev first. This is simpler than
1084 * trying to ensure complicated semantics for all callers.
1085 */
1086 vdev_close(vd);
1087
1088 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
1089 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
1090
1091 /*
1092 * Free all children.
1093 */
1094 for (int c = 0; c < vd->vdev_children; c++)
1095 vdev_free(vd->vdev_child[c]);
1096
1097 ASSERT(vd->vdev_child == NULL);
1098 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
1099
1100 if (vd->vdev_ops->vdev_op_fini != NULL)
1101 vd->vdev_ops->vdev_op_fini(vd);
1102
1103 /*
1104 * Discard allocation state.
1105 */
1106 if (vd->vdev_mg != NULL) {
1107 vdev_metaslab_fini(vd);
1108 metaslab_group_destroy(vd->vdev_mg);
1109 vd->vdev_mg = NULL;
1110 }
1111 if (vd->vdev_log_mg != NULL) {
1112 ASSERT0(vd->vdev_ms_count);
1113 metaslab_group_destroy(vd->vdev_log_mg);
1114 vd->vdev_log_mg = NULL;
1115 }
1116
1117 ASSERT0(vd->vdev_stat.vs_space);
1118 ASSERT0(vd->vdev_stat.vs_dspace);
1119 ASSERT0(vd->vdev_stat.vs_alloc);
1120
1121 /*
1122 * Remove this vdev from its parent's child list.
1123 */
1124 vdev_remove_child(vd->vdev_parent, vd);
1125
1126 ASSERT(vd->vdev_parent == NULL);
1127 ASSERT(!list_link_active(&vd->vdev_leaf_node));
1128
1129 /*
1130 * Clean up vdev structure.
1131 */
1132 vdev_queue_fini(vd);
1133
1134 if (vd->vdev_path)
1135 spa_strfree(vd->vdev_path);
1136 if (vd->vdev_devid)
1137 spa_strfree(vd->vdev_devid);
1138 if (vd->vdev_physpath)
1139 spa_strfree(vd->vdev_physpath);
1140
1141 if (vd->vdev_enc_sysfs_path)
1142 spa_strfree(vd->vdev_enc_sysfs_path);
1143
1144 if (vd->vdev_fru)
1145 spa_strfree(vd->vdev_fru);
1146
1147 if (vd->vdev_isspare)
1148 spa_spare_remove(vd);
1149 if (vd->vdev_isl2cache)
1150 spa_l2cache_remove(vd);
1151
1152 txg_list_destroy(&vd->vdev_ms_list);
1153 txg_list_destroy(&vd->vdev_dtl_list);
1154
1155 mutex_enter(&vd->vdev_dtl_lock);
1156 space_map_close(vd->vdev_dtl_sm);
1157 for (int t = 0; t < DTL_TYPES; t++) {
1158 zfs_range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
1159 zfs_range_tree_destroy(vd->vdev_dtl[t]);
1160 }
1161 mutex_exit(&vd->vdev_dtl_lock);
1162
1163 EQUIV(vd->vdev_indirect_births != NULL,
1164 vd->vdev_indirect_mapping != NULL);
1165 if (vd->vdev_indirect_births != NULL) {
1166 vdev_indirect_mapping_close(vd->vdev_indirect_mapping);
1167 vdev_indirect_births_close(vd->vdev_indirect_births);
1168 }
1169
1170 if (vd->vdev_obsolete_sm != NULL) {
1171 ASSERT(vd->vdev_removing ||
1172 vd->vdev_ops == &vdev_indirect_ops);
1173 space_map_close(vd->vdev_obsolete_sm);
1174 vd->vdev_obsolete_sm = NULL;
1175 }
1176 zfs_range_tree_destroy(vd->vdev_obsolete_segments);
1177 rw_destroy(&vd->vdev_indirect_rwlock);
1178 mutex_destroy(&vd->vdev_obsolete_lock);
1179
1180 mutex_destroy(&vd->vdev_dtl_lock);
1181 mutex_destroy(&vd->vdev_stat_lock);
1182 mutex_destroy(&vd->vdev_probe_lock);
1183 mutex_destroy(&vd->vdev_scan_io_queue_lock);
1184
1185 mutex_destroy(&vd->vdev_initialize_lock);
1186 mutex_destroy(&vd->vdev_initialize_io_lock);
1187 cv_destroy(&vd->vdev_initialize_io_cv);
1188 cv_destroy(&vd->vdev_initialize_cv);
1189
1190 mutex_destroy(&vd->vdev_trim_lock);
1191 mutex_destroy(&vd->vdev_autotrim_lock);
1192 mutex_destroy(&vd->vdev_trim_io_lock);
1193 cv_destroy(&vd->vdev_trim_cv);
1194 cv_destroy(&vd->vdev_autotrim_cv);
1195 cv_destroy(&vd->vdev_autotrim_kick_cv);
1196 cv_destroy(&vd->vdev_trim_io_cv);
1197
1198 mutex_destroy(&vd->vdev_rebuild_lock);
1199 cv_destroy(&vd->vdev_rebuild_cv);
1200
1201 zfs_ratelimit_fini(&vd->vdev_delay_rl);
1202 zfs_ratelimit_fini(&vd->vdev_deadman_rl);
1203 zfs_ratelimit_fini(&vd->vdev_dio_verify_rl);
1204 zfs_ratelimit_fini(&vd->vdev_checksum_rl);
1205
1206 if (vd == spa->spa_root_vdev)
1207 spa->spa_root_vdev = NULL;
1208
1209 kmem_free(vd, sizeof (vdev_t));
1210 }
1211
1212 /*
1213 * Transfer top-level vdev state from svd to tvd.
1214 */
1215 static void
vdev_top_transfer(vdev_t * svd,vdev_t * tvd)1216 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
1217 {
1218 spa_t *spa = svd->vdev_spa;
1219 metaslab_t *msp;
1220 vdev_t *vd;
1221 int t;
1222
1223 ASSERT(tvd == tvd->vdev_top);
1224
1225 tvd->vdev_ms_array = svd->vdev_ms_array;
1226 tvd->vdev_ms_shift = svd->vdev_ms_shift;
1227 tvd->vdev_ms_count = svd->vdev_ms_count;
1228 tvd->vdev_top_zap = svd->vdev_top_zap;
1229
1230 svd->vdev_ms_array = 0;
1231 svd->vdev_ms_shift = 0;
1232 svd->vdev_ms_count = 0;
1233 svd->vdev_top_zap = 0;
1234
1235 if (tvd->vdev_mg)
1236 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
1237 if (tvd->vdev_log_mg)
1238 ASSERT3P(tvd->vdev_log_mg, ==, svd->vdev_log_mg);
1239 tvd->vdev_mg = svd->vdev_mg;
1240 tvd->vdev_log_mg = svd->vdev_log_mg;
1241 tvd->vdev_ms = svd->vdev_ms;
1242
1243 svd->vdev_mg = NULL;
1244 svd->vdev_log_mg = NULL;
1245 svd->vdev_ms = NULL;
1246
1247 if (tvd->vdev_mg != NULL)
1248 tvd->vdev_mg->mg_vd = tvd;
1249 if (tvd->vdev_log_mg != NULL)
1250 tvd->vdev_log_mg->mg_vd = tvd;
1251
1252 tvd->vdev_checkpoint_sm = svd->vdev_checkpoint_sm;
1253 svd->vdev_checkpoint_sm = NULL;
1254
1255 tvd->vdev_alloc_bias = svd->vdev_alloc_bias;
1256 svd->vdev_alloc_bias = VDEV_BIAS_NONE;
1257
1258 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
1259 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
1260 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
1261
1262 svd->vdev_stat.vs_alloc = 0;
1263 svd->vdev_stat.vs_space = 0;
1264 svd->vdev_stat.vs_dspace = 0;
1265
1266 /*
1267 * State which may be set on a top-level vdev that's in the
1268 * process of being removed.
1269 */
1270 ASSERT0(tvd->vdev_indirect_config.vic_births_object);
1271 ASSERT0(tvd->vdev_indirect_config.vic_mapping_object);
1272 ASSERT3U(tvd->vdev_indirect_config.vic_prev_indirect_vdev, ==, -1ULL);
1273 ASSERT3P(tvd->vdev_indirect_mapping, ==, NULL);
1274 ASSERT3P(tvd->vdev_indirect_births, ==, NULL);
1275 ASSERT3P(tvd->vdev_obsolete_sm, ==, NULL);
1276 ASSERT0(tvd->vdev_noalloc);
1277 ASSERT0(tvd->vdev_removing);
1278 ASSERT0(tvd->vdev_rebuilding);
1279 tvd->vdev_noalloc = svd->vdev_noalloc;
1280 tvd->vdev_removing = svd->vdev_removing;
1281 tvd->vdev_rebuilding = svd->vdev_rebuilding;
1282 tvd->vdev_rebuild_config = svd->vdev_rebuild_config;
1283 tvd->vdev_indirect_config = svd->vdev_indirect_config;
1284 tvd->vdev_indirect_mapping = svd->vdev_indirect_mapping;
1285 tvd->vdev_indirect_births = svd->vdev_indirect_births;
1286 zfs_range_tree_swap(&svd->vdev_obsolete_segments,
1287 &tvd->vdev_obsolete_segments);
1288 tvd->vdev_obsolete_sm = svd->vdev_obsolete_sm;
1289 svd->vdev_indirect_config.vic_mapping_object = 0;
1290 svd->vdev_indirect_config.vic_births_object = 0;
1291 svd->vdev_indirect_config.vic_prev_indirect_vdev = -1ULL;
1292 svd->vdev_indirect_mapping = NULL;
1293 svd->vdev_indirect_births = NULL;
1294 svd->vdev_obsolete_sm = NULL;
1295 svd->vdev_noalloc = 0;
1296 svd->vdev_removing = 0;
1297 svd->vdev_rebuilding = 0;
1298
1299 for (t = 0; t < TXG_SIZE; t++) {
1300 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
1301 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
1302 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
1303 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
1304 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
1305 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
1306 }
1307
1308 if (list_link_active(&svd->vdev_config_dirty_node)) {
1309 vdev_config_clean(svd);
1310 vdev_config_dirty(tvd);
1311 }
1312
1313 if (list_link_active(&svd->vdev_state_dirty_node)) {
1314 vdev_state_clean(svd);
1315 vdev_state_dirty(tvd);
1316 }
1317
1318 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
1319 svd->vdev_deflate_ratio = 0;
1320
1321 tvd->vdev_islog = svd->vdev_islog;
1322 svd->vdev_islog = 0;
1323
1324 dsl_scan_io_queue_vdev_xfer(svd, tvd);
1325 }
1326
1327 static void
vdev_top_update(vdev_t * tvd,vdev_t * vd)1328 vdev_top_update(vdev_t *tvd, vdev_t *vd)
1329 {
1330 if (vd == NULL)
1331 return;
1332
1333 vd->vdev_top = tvd;
1334
1335 for (int c = 0; c < vd->vdev_children; c++)
1336 vdev_top_update(tvd, vd->vdev_child[c]);
1337 }
1338
1339 /*
1340 * Add a mirror/replacing vdev above an existing vdev. There is no need to
1341 * call .vdev_op_init() since mirror/replacing vdevs do not have private state.
1342 */
1343 vdev_t *
vdev_add_parent(vdev_t * cvd,vdev_ops_t * ops)1344 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
1345 {
1346 spa_t *spa = cvd->vdev_spa;
1347 vdev_t *pvd = cvd->vdev_parent;
1348 vdev_t *mvd;
1349
1350 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1351
1352 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
1353
1354 mvd->vdev_asize = cvd->vdev_asize;
1355 mvd->vdev_min_asize = cvd->vdev_min_asize;
1356 mvd->vdev_max_asize = cvd->vdev_max_asize;
1357 mvd->vdev_psize = cvd->vdev_psize;
1358 mvd->vdev_ashift = cvd->vdev_ashift;
1359 mvd->vdev_logical_ashift = cvd->vdev_logical_ashift;
1360 mvd->vdev_physical_ashift = cvd->vdev_physical_ashift;
1361 mvd->vdev_state = cvd->vdev_state;
1362 mvd->vdev_crtxg = cvd->vdev_crtxg;
1363
1364 vdev_remove_child(pvd, cvd);
1365 vdev_add_child(pvd, mvd);
1366 cvd->vdev_id = mvd->vdev_children;
1367 vdev_add_child(mvd, cvd);
1368 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1369
1370 if (mvd == mvd->vdev_top)
1371 vdev_top_transfer(cvd, mvd);
1372
1373 return (mvd);
1374 }
1375
1376 /*
1377 * Remove a 1-way mirror/replacing vdev from the tree.
1378 */
1379 void
vdev_remove_parent(vdev_t * cvd)1380 vdev_remove_parent(vdev_t *cvd)
1381 {
1382 vdev_t *mvd = cvd->vdev_parent;
1383 vdev_t *pvd = mvd->vdev_parent;
1384
1385 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1386
1387 ASSERT(mvd->vdev_children == 1);
1388 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
1389 mvd->vdev_ops == &vdev_replacing_ops ||
1390 mvd->vdev_ops == &vdev_spare_ops);
1391 cvd->vdev_ashift = mvd->vdev_ashift;
1392 cvd->vdev_logical_ashift = mvd->vdev_logical_ashift;
1393 cvd->vdev_physical_ashift = mvd->vdev_physical_ashift;
1394 vdev_remove_child(mvd, cvd);
1395 vdev_remove_child(pvd, mvd);
1396
1397 /*
1398 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
1399 * Otherwise, we could have detached an offline device, and when we
1400 * go to import the pool we'll think we have two top-level vdevs,
1401 * instead of a different version of the same top-level vdev.
1402 */
1403 if (mvd->vdev_top == mvd) {
1404 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
1405 cvd->vdev_orig_guid = cvd->vdev_guid;
1406 cvd->vdev_guid += guid_delta;
1407 cvd->vdev_guid_sum += guid_delta;
1408
1409 /*
1410 * If pool not set for autoexpand, we need to also preserve
1411 * mvd's asize to prevent automatic expansion of cvd.
1412 * Otherwise if we are adjusting the mirror by attaching and
1413 * detaching children of non-uniform sizes, the mirror could
1414 * autoexpand, unexpectedly requiring larger devices to
1415 * re-establish the mirror.
1416 */
1417 if (!cvd->vdev_spa->spa_autoexpand)
1418 cvd->vdev_asize = mvd->vdev_asize;
1419 }
1420 cvd->vdev_id = mvd->vdev_id;
1421 vdev_add_child(pvd, cvd);
1422 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
1423
1424 if (cvd == cvd->vdev_top)
1425 vdev_top_transfer(mvd, cvd);
1426
1427 ASSERT(mvd->vdev_children == 0);
1428 vdev_free(mvd);
1429 }
1430
1431 /*
1432 * Choose GCD for spa_gcd_alloc.
1433 */
1434 static uint64_t
vdev_gcd(uint64_t a,uint64_t b)1435 vdev_gcd(uint64_t a, uint64_t b)
1436 {
1437 while (b != 0) {
1438 uint64_t t = b;
1439 b = a % b;
1440 a = t;
1441 }
1442 return (a);
1443 }
1444
1445 /*
1446 * Set spa_min_alloc and spa_gcd_alloc.
1447 */
1448 static void
vdev_spa_set_alloc(spa_t * spa,uint64_t min_alloc)1449 vdev_spa_set_alloc(spa_t *spa, uint64_t min_alloc)
1450 {
1451 if (min_alloc < spa->spa_min_alloc)
1452 spa->spa_min_alloc = min_alloc;
1453 if (spa->spa_gcd_alloc == INT_MAX) {
1454 spa->spa_gcd_alloc = min_alloc;
1455 } else {
1456 spa->spa_gcd_alloc = vdev_gcd(min_alloc,
1457 spa->spa_gcd_alloc);
1458 }
1459 }
1460
1461 void
vdev_metaslab_group_create(vdev_t * vd)1462 vdev_metaslab_group_create(vdev_t *vd)
1463 {
1464 spa_t *spa = vd->vdev_spa;
1465
1466 /*
1467 * metaslab_group_create was delayed until allocation bias was available
1468 */
1469 if (vd->vdev_mg == NULL) {
1470 metaslab_class_t *mc;
1471
1472 if (vd->vdev_islog && vd->vdev_alloc_bias == VDEV_BIAS_NONE)
1473 vd->vdev_alloc_bias = VDEV_BIAS_LOG;
1474
1475 ASSERT3U(vd->vdev_islog, ==,
1476 (vd->vdev_alloc_bias == VDEV_BIAS_LOG));
1477
1478 switch (vd->vdev_alloc_bias) {
1479 case VDEV_BIAS_LOG:
1480 mc = spa_log_class(spa);
1481 break;
1482 case VDEV_BIAS_SPECIAL:
1483 mc = spa_special_class(spa);
1484 break;
1485 case VDEV_BIAS_DEDUP:
1486 mc = spa_dedup_class(spa);
1487 break;
1488 default:
1489 mc = spa_normal_class(spa);
1490 }
1491
1492 vd->vdev_mg = metaslab_group_create(mc, vd,
1493 spa->spa_alloc_count);
1494
1495 if (!vd->vdev_islog) {
1496 vd->vdev_log_mg = metaslab_group_create(
1497 spa_embedded_log_class(spa), vd, 1);
1498 }
1499
1500 /*
1501 * The spa ashift min/max only apply for the normal metaslab
1502 * class. Class destination is late binding so ashift boundary
1503 * setting had to wait until now.
1504 */
1505 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1506 mc == spa_normal_class(spa) && vd->vdev_aux == NULL) {
1507 if (vd->vdev_ashift > spa->spa_max_ashift)
1508 spa->spa_max_ashift = vd->vdev_ashift;
1509 if (vd->vdev_ashift < spa->spa_min_ashift)
1510 spa->spa_min_ashift = vd->vdev_ashift;
1511
1512 uint64_t min_alloc = vdev_get_min_alloc(vd);
1513 vdev_spa_set_alloc(spa, min_alloc);
1514 }
1515 }
1516 }
1517
1518 int
vdev_metaslab_init(vdev_t * vd,uint64_t txg)1519 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
1520 {
1521 spa_t *spa = vd->vdev_spa;
1522 uint64_t oldc = vd->vdev_ms_count;
1523 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
1524 metaslab_t **mspp;
1525 int error;
1526 boolean_t expanding = (oldc != 0);
1527
1528 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1529
1530 /*
1531 * This vdev is not being allocated from yet or is a hole.
1532 */
1533 if (vd->vdev_ms_shift == 0)
1534 return (0);
1535
1536 ASSERT(!vd->vdev_ishole);
1537
1538 ASSERT(oldc <= newc);
1539
1540 mspp = vmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
1541
1542 if (expanding) {
1543 memcpy(mspp, vd->vdev_ms, oldc * sizeof (*mspp));
1544 vmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
1545 }
1546
1547 vd->vdev_ms = mspp;
1548 vd->vdev_ms_count = newc;
1549
1550 for (uint64_t m = oldc; m < newc; m++) {
1551 uint64_t object = 0;
1552 /*
1553 * vdev_ms_array may be 0 if we are creating the "fake"
1554 * metaslabs for an indirect vdev for zdb's leak detection.
1555 * See zdb_leak_init().
1556 */
1557 if (txg == 0 && vd->vdev_ms_array != 0) {
1558 error = dmu_read(spa->spa_meta_objset,
1559 vd->vdev_ms_array,
1560 m * sizeof (uint64_t), sizeof (uint64_t), &object,
1561 DMU_READ_PREFETCH);
1562 if (error != 0) {
1563 vdev_dbgmsg(vd, "unable to read the metaslab "
1564 "array [error=%d]", error);
1565 return (error);
1566 }
1567 }
1568
1569 error = metaslab_init(vd->vdev_mg, m, object, txg,
1570 &(vd->vdev_ms[m]));
1571 if (error != 0) {
1572 vdev_dbgmsg(vd, "metaslab_init failed [error=%d]",
1573 error);
1574 return (error);
1575 }
1576 }
1577
1578 /*
1579 * Find the emptiest metaslab on the vdev and mark it for use for
1580 * embedded slog by moving it from the regular to the log metaslab
1581 * group.
1582 */
1583 if (vd->vdev_mg->mg_class == spa_normal_class(spa) &&
1584 vd->vdev_ms_count > zfs_embedded_slog_min_ms &&
1585 avl_is_empty(&vd->vdev_log_mg->mg_metaslab_tree)) {
1586 uint64_t slog_msid = 0;
1587 uint64_t smallest = UINT64_MAX;
1588
1589 /*
1590 * Note, we only search the new metaslabs, because the old
1591 * (pre-existing) ones may be active (e.g. have non-empty
1592 * range_tree's), and we don't move them to the new
1593 * metaslab_t.
1594 */
1595 for (uint64_t m = oldc; m < newc; m++) {
1596 uint64_t alloc =
1597 space_map_allocated(vd->vdev_ms[m]->ms_sm);
1598 if (alloc < smallest) {
1599 slog_msid = m;
1600 smallest = alloc;
1601 }
1602 }
1603 metaslab_t *slog_ms = vd->vdev_ms[slog_msid];
1604 /*
1605 * The metaslab was marked as dirty at the end of
1606 * metaslab_init(). Remove it from the dirty list so that we
1607 * can uninitialize and reinitialize it to the new class.
1608 */
1609 if (txg != 0) {
1610 (void) txg_list_remove_this(&vd->vdev_ms_list,
1611 slog_ms, txg);
1612 }
1613 uint64_t sm_obj = space_map_object(slog_ms->ms_sm);
1614 metaslab_fini(slog_ms);
1615 VERIFY0(metaslab_init(vd->vdev_log_mg, slog_msid, sm_obj, txg,
1616 &vd->vdev_ms[slog_msid]));
1617 }
1618
1619 if (txg == 0)
1620 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
1621
1622 /*
1623 * If the vdev is marked as non-allocating then don't
1624 * activate the metaslabs since we want to ensure that
1625 * no allocations are performed on this device.
1626 */
1627 if (vd->vdev_noalloc) {
1628 /* track non-allocating vdev space */
1629 spa->spa_nonallocating_dspace += spa_deflate(spa) ?
1630 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space;
1631 } else if (!expanding) {
1632 metaslab_group_activate(vd->vdev_mg);
1633 if (vd->vdev_log_mg != NULL)
1634 metaslab_group_activate(vd->vdev_log_mg);
1635 }
1636
1637 if (txg == 0)
1638 spa_config_exit(spa, SCL_ALLOC, FTAG);
1639
1640 return (0);
1641 }
1642
1643 void
vdev_metaslab_fini(vdev_t * vd)1644 vdev_metaslab_fini(vdev_t *vd)
1645 {
1646 if (vd->vdev_checkpoint_sm != NULL) {
1647 ASSERT(spa_feature_is_active(vd->vdev_spa,
1648 SPA_FEATURE_POOL_CHECKPOINT));
1649 space_map_close(vd->vdev_checkpoint_sm);
1650 /*
1651 * Even though we close the space map, we need to set its
1652 * pointer to NULL. The reason is that vdev_metaslab_fini()
1653 * may be called multiple times for certain operations
1654 * (i.e. when destroying a pool) so we need to ensure that
1655 * this clause never executes twice. This logic is similar
1656 * to the one used for the vdev_ms clause below.
1657 */
1658 vd->vdev_checkpoint_sm = NULL;
1659 }
1660
1661 if (vd->vdev_ms != NULL) {
1662 metaslab_group_t *mg = vd->vdev_mg;
1663
1664 metaslab_group_passivate(mg);
1665 if (vd->vdev_log_mg != NULL) {
1666 ASSERT(!vd->vdev_islog);
1667 metaslab_group_passivate(vd->vdev_log_mg);
1668 }
1669
1670 uint64_t count = vd->vdev_ms_count;
1671 for (uint64_t m = 0; m < count; m++) {
1672 metaslab_t *msp = vd->vdev_ms[m];
1673 if (msp != NULL)
1674 metaslab_fini(msp);
1675 }
1676 vmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
1677 vd->vdev_ms = NULL;
1678 vd->vdev_ms_count = 0;
1679
1680 for (int i = 0; i < ZFS_RANGE_TREE_HISTOGRAM_SIZE; i++) {
1681 ASSERT0(mg->mg_histogram[i]);
1682 if (vd->vdev_log_mg != NULL)
1683 ASSERT0(vd->vdev_log_mg->mg_histogram[i]);
1684 }
1685 }
1686 ASSERT0(vd->vdev_ms_count);
1687 }
1688
1689 typedef struct vdev_probe_stats {
1690 boolean_t vps_readable;
1691 boolean_t vps_writeable;
1692 boolean_t vps_zio_done_probe;
1693 int vps_flags;
1694 } vdev_probe_stats_t;
1695
1696 static void
vdev_probe_done(zio_t * zio)1697 vdev_probe_done(zio_t *zio)
1698 {
1699 spa_t *spa = zio->io_spa;
1700 vdev_t *vd = zio->io_vd;
1701 vdev_probe_stats_t *vps = zio->io_private;
1702
1703 ASSERT(vd->vdev_probe_zio != NULL);
1704
1705 if (zio->io_type == ZIO_TYPE_READ) {
1706 if (zio->io_error == 0)
1707 vps->vps_readable = 1;
1708 if (zio->io_error == 0 && spa_writeable(spa)) {
1709 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
1710 zio->io_offset, zio->io_size, zio->io_abd,
1711 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1712 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
1713 } else {
1714 abd_free(zio->io_abd);
1715 }
1716 } else if (zio->io_type == ZIO_TYPE_WRITE) {
1717 if (zio->io_error == 0)
1718 vps->vps_writeable = 1;
1719 abd_free(zio->io_abd);
1720 } else if (zio->io_type == ZIO_TYPE_NULL) {
1721 zio_t *pio;
1722 zio_link_t *zl;
1723
1724 vd->vdev_cant_read |= !vps->vps_readable;
1725 vd->vdev_cant_write |= !vps->vps_writeable;
1726 vdev_dbgmsg(vd, "probe done, cant_read=%u cant_write=%u",
1727 vd->vdev_cant_read, vd->vdev_cant_write);
1728
1729 if (vdev_readable(vd) &&
1730 (vdev_writeable(vd) || !spa_writeable(spa))) {
1731 zio->io_error = 0;
1732 } else {
1733 ASSERT(zio->io_error != 0);
1734 vdev_dbgmsg(vd, "failed probe");
1735 (void) zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
1736 spa, vd, NULL, NULL, 0);
1737 zio->io_error = SET_ERROR(ENXIO);
1738
1739 /*
1740 * If this probe was initiated from zio pipeline, then
1741 * change the state in a spa_async_request. Probes that
1742 * were initiated from a vdev_open can change the state
1743 * as part of the open call.
1744 */
1745 if (vps->vps_zio_done_probe) {
1746 vd->vdev_fault_wanted = B_TRUE;
1747 spa_async_request(spa, SPA_ASYNC_FAULT_VDEV);
1748 }
1749 }
1750
1751 mutex_enter(&vd->vdev_probe_lock);
1752 ASSERT(vd->vdev_probe_zio == zio);
1753 vd->vdev_probe_zio = NULL;
1754 mutex_exit(&vd->vdev_probe_lock);
1755
1756 zl = NULL;
1757 while ((pio = zio_walk_parents(zio, &zl)) != NULL)
1758 if (!vdev_accessible(vd, pio))
1759 pio->io_error = SET_ERROR(ENXIO);
1760
1761 kmem_free(vps, sizeof (*vps));
1762 }
1763 }
1764
1765 /*
1766 * Determine whether this device is accessible.
1767 *
1768 * Read and write to several known locations: the pad regions of each
1769 * vdev label but the first, which we leave alone in case it contains
1770 * a VTOC.
1771 */
1772 zio_t *
vdev_probe(vdev_t * vd,zio_t * zio)1773 vdev_probe(vdev_t *vd, zio_t *zio)
1774 {
1775 spa_t *spa = vd->vdev_spa;
1776 vdev_probe_stats_t *vps = NULL;
1777 zio_t *pio;
1778
1779 ASSERT(vd->vdev_ops->vdev_op_leaf);
1780
1781 /*
1782 * Don't probe the probe.
1783 */
1784 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1785 return (NULL);
1786
1787 /*
1788 * To prevent 'probe storms' when a device fails, we create
1789 * just one probe i/o at a time. All zios that want to probe
1790 * this vdev will become parents of the probe io.
1791 */
1792 mutex_enter(&vd->vdev_probe_lock);
1793
1794 if ((pio = vd->vdev_probe_zio) == NULL) {
1795 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1796
1797 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1798 ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_TRYHARD;
1799 vps->vps_zio_done_probe = (zio != NULL);
1800
1801 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1802 /*
1803 * vdev_cant_read and vdev_cant_write can only
1804 * transition from TRUE to FALSE when we have the
1805 * SCL_ZIO lock as writer; otherwise they can only
1806 * transition from FALSE to TRUE. This ensures that
1807 * any zio looking at these values can assume that
1808 * failures persist for the life of the I/O. That's
1809 * important because when a device has intermittent
1810 * connectivity problems, we want to ensure that
1811 * they're ascribed to the device (ENXIO) and not
1812 * the zio (EIO).
1813 *
1814 * Since we hold SCL_ZIO as writer here, clear both
1815 * values so the probe can reevaluate from first
1816 * principles.
1817 */
1818 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1819 vd->vdev_cant_read = B_FALSE;
1820 vd->vdev_cant_write = B_FALSE;
1821 }
1822
1823 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1824 vdev_probe_done, vps,
1825 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1826 }
1827
1828 if (zio != NULL)
1829 zio_add_child(zio, pio);
1830
1831 mutex_exit(&vd->vdev_probe_lock);
1832
1833 if (vps == NULL) {
1834 ASSERT(zio != NULL);
1835 return (NULL);
1836 }
1837
1838 for (int l = 1; l < VDEV_LABELS; l++) {
1839 zio_nowait(zio_read_phys(pio, vd,
1840 vdev_label_offset(vd->vdev_psize, l,
1841 offsetof(vdev_label_t, vl_be)), VDEV_PAD_SIZE,
1842 abd_alloc_for_io(VDEV_PAD_SIZE, B_TRUE),
1843 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1844 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1845 }
1846
1847 if (zio == NULL)
1848 return (pio);
1849
1850 zio_nowait(pio);
1851 return (NULL);
1852 }
1853
1854 static void
vdev_load_child(void * arg)1855 vdev_load_child(void *arg)
1856 {
1857 vdev_t *vd = arg;
1858
1859 vd->vdev_load_error = vdev_load(vd);
1860 }
1861
1862 static void
vdev_open_child(void * arg)1863 vdev_open_child(void *arg)
1864 {
1865 vdev_t *vd = arg;
1866
1867 vd->vdev_open_thread = curthread;
1868 vd->vdev_open_error = vdev_open(vd);
1869 vd->vdev_open_thread = NULL;
1870 }
1871
1872 static boolean_t
vdev_uses_zvols(vdev_t * vd)1873 vdev_uses_zvols(vdev_t *vd)
1874 {
1875 #ifdef _KERNEL
1876 if (zvol_is_zvol(vd->vdev_path))
1877 return (B_TRUE);
1878 #endif
1879
1880 for (int c = 0; c < vd->vdev_children; c++)
1881 if (vdev_uses_zvols(vd->vdev_child[c]))
1882 return (B_TRUE);
1883
1884 return (B_FALSE);
1885 }
1886
1887 /*
1888 * Returns B_TRUE if the passed child should be opened.
1889 */
1890 static boolean_t
vdev_default_open_children_func(vdev_t * vd)1891 vdev_default_open_children_func(vdev_t *vd)
1892 {
1893 (void) vd;
1894 return (B_TRUE);
1895 }
1896
1897 /*
1898 * Open the requested child vdevs. If any of the leaf vdevs are using
1899 * a ZFS volume then do the opens in a single thread. This avoids a
1900 * deadlock when the current thread is holding the spa_namespace_lock.
1901 */
1902 static void
vdev_open_children_impl(vdev_t * vd,vdev_open_children_func_t * open_func)1903 vdev_open_children_impl(vdev_t *vd, vdev_open_children_func_t *open_func)
1904 {
1905 int children = vd->vdev_children;
1906
1907 taskq_t *tq = taskq_create("vdev_open", children, minclsyspri,
1908 children, children, TASKQ_PREPOPULATE);
1909 vd->vdev_nonrot = B_TRUE;
1910
1911 for (int c = 0; c < children; c++) {
1912 vdev_t *cvd = vd->vdev_child[c];
1913
1914 if (open_func(cvd) == B_FALSE)
1915 continue;
1916
1917 if (tq == NULL || vdev_uses_zvols(vd)) {
1918 cvd->vdev_open_error = vdev_open(cvd);
1919 } else {
1920 VERIFY(taskq_dispatch(tq, vdev_open_child,
1921 cvd, TQ_SLEEP) != TASKQID_INVALID);
1922 }
1923
1924 vd->vdev_nonrot &= cvd->vdev_nonrot;
1925 }
1926
1927 if (tq != NULL) {
1928 taskq_wait(tq);
1929 taskq_destroy(tq);
1930 }
1931 }
1932
1933 /*
1934 * Open all child vdevs.
1935 */
1936 void
vdev_open_children(vdev_t * vd)1937 vdev_open_children(vdev_t *vd)
1938 {
1939 vdev_open_children_impl(vd, vdev_default_open_children_func);
1940 }
1941
1942 /*
1943 * Conditionally open a subset of child vdevs.
1944 */
1945 void
vdev_open_children_subset(vdev_t * vd,vdev_open_children_func_t * open_func)1946 vdev_open_children_subset(vdev_t *vd, vdev_open_children_func_t *open_func)
1947 {
1948 vdev_open_children_impl(vd, open_func);
1949 }
1950
1951 /*
1952 * Compute the raidz-deflation ratio. Note, we hard-code 128k (1 << 17)
1953 * because it is the "typical" blocksize. Even though SPA_MAXBLOCKSIZE
1954 * changed, this algorithm can not change, otherwise it would inconsistently
1955 * account for existing bp's. We also hard-code txg 0 for the same reason
1956 * since expanded RAIDZ vdevs can use a different asize for different birth
1957 * txg's.
1958 */
1959 static void
vdev_set_deflate_ratio(vdev_t * vd)1960 vdev_set_deflate_ratio(vdev_t *vd)
1961 {
1962 if (vd == vd->vdev_top && !vd->vdev_ishole && vd->vdev_ashift != 0) {
1963 vd->vdev_deflate_ratio = (1 << 17) /
1964 (vdev_psize_to_asize_txg(vd, 1 << 17, 0) >>
1965 SPA_MINBLOCKSHIFT);
1966 }
1967 }
1968
1969 /*
1970 * Choose the best of two ashifts, preferring one between logical ashift
1971 * (absolute minimum) and administrator defined maximum, otherwise take
1972 * the biggest of the two.
1973 */
1974 uint64_t
vdev_best_ashift(uint64_t logical,uint64_t a,uint64_t b)1975 vdev_best_ashift(uint64_t logical, uint64_t a, uint64_t b)
1976 {
1977 if (a > logical && a <= zfs_vdev_max_auto_ashift) {
1978 if (b <= logical || b > zfs_vdev_max_auto_ashift)
1979 return (a);
1980 else
1981 return (MAX(a, b));
1982 } else if (b <= logical || b > zfs_vdev_max_auto_ashift)
1983 return (MAX(a, b));
1984 return (b);
1985 }
1986
1987 /*
1988 * Maximize performance by inflating the configured ashift for top level
1989 * vdevs to be as close to the physical ashift as possible while maintaining
1990 * administrator defined limits and ensuring it doesn't go below the
1991 * logical ashift.
1992 */
1993 static void
vdev_ashift_optimize(vdev_t * vd)1994 vdev_ashift_optimize(vdev_t *vd)
1995 {
1996 ASSERT(vd == vd->vdev_top);
1997
1998 if (vd->vdev_ashift < vd->vdev_physical_ashift &&
1999 vd->vdev_physical_ashift <= zfs_vdev_max_auto_ashift) {
2000 vd->vdev_ashift = MIN(
2001 MAX(zfs_vdev_max_auto_ashift, vd->vdev_ashift),
2002 MAX(zfs_vdev_min_auto_ashift,
2003 vd->vdev_physical_ashift));
2004 } else {
2005 /*
2006 * If the logical and physical ashifts are the same, then
2007 * we ensure that the top-level vdev's ashift is not smaller
2008 * than our minimum ashift value. For the unusual case
2009 * where logical ashift > physical ashift, we can't cap
2010 * the calculated ashift based on max ashift as that
2011 * would cause failures.
2012 * We still check if we need to increase it to match
2013 * the min ashift.
2014 */
2015 vd->vdev_ashift = MAX(zfs_vdev_min_auto_ashift,
2016 vd->vdev_ashift);
2017 }
2018 }
2019
2020 /*
2021 * Prepare a virtual device for access.
2022 */
2023 int
vdev_open(vdev_t * vd)2024 vdev_open(vdev_t *vd)
2025 {
2026 spa_t *spa = vd->vdev_spa;
2027 int error;
2028 uint64_t osize = 0;
2029 uint64_t max_osize = 0;
2030 uint64_t asize, max_asize, psize;
2031 uint64_t logical_ashift = 0;
2032 uint64_t physical_ashift = 0;
2033
2034 ASSERT(vd->vdev_open_thread == curthread ||
2035 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2036 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
2037 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
2038 vd->vdev_state == VDEV_STATE_OFFLINE);
2039
2040 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2041 vd->vdev_cant_read = B_FALSE;
2042 vd->vdev_cant_write = B_FALSE;
2043 vd->vdev_fault_wanted = B_FALSE;
2044 vd->vdev_remove_wanted = B_FALSE;
2045 vd->vdev_min_asize = vdev_get_min_asize(vd);
2046
2047 /*
2048 * If this vdev is not removed, check its fault status. If it's
2049 * faulted, bail out of the open.
2050 */
2051 if (!vd->vdev_removed && vd->vdev_faulted) {
2052 ASSERT(vd->vdev_children == 0);
2053 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
2054 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
2055 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2056 vd->vdev_label_aux);
2057 return (SET_ERROR(ENXIO));
2058 } else if (vd->vdev_offline) {
2059 ASSERT(vd->vdev_children == 0);
2060 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
2061 return (SET_ERROR(ENXIO));
2062 }
2063
2064 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize,
2065 &logical_ashift, &physical_ashift);
2066
2067 /* Keep the device in removed state if unplugged */
2068 if (error == ENOENT && vd->vdev_removed) {
2069 vdev_set_state(vd, B_TRUE, VDEV_STATE_REMOVED,
2070 VDEV_AUX_NONE);
2071 return (error);
2072 }
2073
2074 /*
2075 * Physical volume size should never be larger than its max size, unless
2076 * the disk has shrunk while we were reading it or the device is buggy
2077 * or damaged: either way it's not safe for use, bail out of the open.
2078 */
2079 if (osize > max_osize) {
2080 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2081 VDEV_AUX_OPEN_FAILED);
2082 return (SET_ERROR(ENXIO));
2083 }
2084
2085 /*
2086 * Reset the vdev_reopening flag so that we actually close
2087 * the vdev on error.
2088 */
2089 vd->vdev_reopening = B_FALSE;
2090 if (zio_injection_enabled && error == 0)
2091 error = zio_handle_device_injection(vd, NULL, SET_ERROR(ENXIO));
2092
2093 if (error) {
2094 if (vd->vdev_removed &&
2095 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
2096 vd->vdev_removed = B_FALSE;
2097
2098 if (vd->vdev_stat.vs_aux == VDEV_AUX_CHILDREN_OFFLINE) {
2099 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE,
2100 vd->vdev_stat.vs_aux);
2101 } else {
2102 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2103 vd->vdev_stat.vs_aux);
2104 }
2105 return (error);
2106 }
2107
2108 vd->vdev_removed = B_FALSE;
2109
2110 /*
2111 * Recheck the faulted flag now that we have confirmed that
2112 * the vdev is accessible. If we're faulted, bail.
2113 */
2114 if (vd->vdev_faulted) {
2115 ASSERT(vd->vdev_children == 0);
2116 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
2117 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
2118 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2119 vd->vdev_label_aux);
2120 return (SET_ERROR(ENXIO));
2121 }
2122
2123 if (vd->vdev_degraded) {
2124 ASSERT(vd->vdev_children == 0);
2125 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
2126 VDEV_AUX_ERR_EXCEEDED);
2127 } else {
2128 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
2129 }
2130
2131 /*
2132 * For hole or missing vdevs we just return success.
2133 */
2134 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
2135 return (0);
2136
2137 for (int c = 0; c < vd->vdev_children; c++) {
2138 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
2139 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
2140 VDEV_AUX_NONE);
2141 break;
2142 }
2143 }
2144
2145 osize = P2ALIGN_TYPED(osize, sizeof (vdev_label_t), uint64_t);
2146 max_osize = P2ALIGN_TYPED(max_osize, sizeof (vdev_label_t), uint64_t);
2147
2148 if (vd->vdev_children == 0) {
2149 if (osize < SPA_MINDEVSIZE) {
2150 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2151 VDEV_AUX_TOO_SMALL);
2152 return (SET_ERROR(EOVERFLOW));
2153 }
2154 psize = osize;
2155 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
2156 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
2157 VDEV_LABEL_END_SIZE);
2158 } else {
2159 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
2160 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
2161 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2162 VDEV_AUX_TOO_SMALL);
2163 return (SET_ERROR(EOVERFLOW));
2164 }
2165 psize = 0;
2166 asize = osize;
2167 max_asize = max_osize;
2168 }
2169
2170 /*
2171 * If the vdev was expanded, record this so that we can re-create the
2172 * uberblock rings in labels {2,3}, during the next sync.
2173 */
2174 if ((psize > vd->vdev_psize) && (vd->vdev_psize != 0))
2175 vd->vdev_copy_uberblocks = B_TRUE;
2176
2177 vd->vdev_psize = psize;
2178
2179 /*
2180 * Make sure the allocatable size hasn't shrunk too much.
2181 */
2182 if (asize < vd->vdev_min_asize) {
2183 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2184 VDEV_AUX_BAD_LABEL);
2185 return (SET_ERROR(EINVAL));
2186 }
2187
2188 /*
2189 * We can always set the logical/physical ashift members since
2190 * their values are only used to calculate the vdev_ashift when
2191 * the device is first added to the config. These values should
2192 * not be used for anything else since they may change whenever
2193 * the device is reopened and we don't store them in the label.
2194 */
2195 vd->vdev_physical_ashift =
2196 MAX(physical_ashift, vd->vdev_physical_ashift);
2197 vd->vdev_logical_ashift = MAX(logical_ashift,
2198 vd->vdev_logical_ashift);
2199
2200 if (vd->vdev_asize == 0) {
2201 /*
2202 * This is the first-ever open, so use the computed values.
2203 * For compatibility, a different ashift can be requested.
2204 */
2205 vd->vdev_asize = asize;
2206 vd->vdev_max_asize = max_asize;
2207
2208 /*
2209 * If the vdev_ashift was not overridden at creation time
2210 * (0) or the override value is impossible for the device,
2211 * then set it the logical ashift and optimize the ashift.
2212 */
2213 if (vd->vdev_ashift < vd->vdev_logical_ashift) {
2214 vd->vdev_ashift = vd->vdev_logical_ashift;
2215
2216 if (vd->vdev_logical_ashift > ASHIFT_MAX) {
2217 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2218 VDEV_AUX_ASHIFT_TOO_BIG);
2219 return (SET_ERROR(EDOM));
2220 }
2221
2222 if (vd->vdev_top == vd && vd->vdev_attaching == B_FALSE)
2223 vdev_ashift_optimize(vd);
2224 vd->vdev_attaching = B_FALSE;
2225 }
2226 if (vd->vdev_ashift != 0 && (vd->vdev_ashift < ASHIFT_MIN ||
2227 vd->vdev_ashift > ASHIFT_MAX)) {
2228 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2229 VDEV_AUX_BAD_ASHIFT);
2230 return (SET_ERROR(EDOM));
2231 }
2232 } else {
2233 /*
2234 * Make sure the alignment required hasn't increased.
2235 */
2236 if (vd->vdev_ashift > vd->vdev_top->vdev_ashift &&
2237 vd->vdev_ops->vdev_op_leaf) {
2238 (void) zfs_ereport_post(
2239 FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT,
2240 spa, vd, NULL, NULL, 0);
2241 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2242 VDEV_AUX_BAD_LABEL);
2243 return (SET_ERROR(EDOM));
2244 }
2245 vd->vdev_max_asize = max_asize;
2246 }
2247
2248 /*
2249 * If all children are healthy we update asize if either:
2250 * The asize has increased, due to a device expansion caused by dynamic
2251 * LUN growth or vdev replacement, and automatic expansion is enabled;
2252 * making the additional space available.
2253 *
2254 * The asize has decreased, due to a device shrink usually caused by a
2255 * vdev replace with a smaller device. This ensures that calculations
2256 * based of max_asize and asize e.g. esize are always valid. It's safe
2257 * to do this as we've already validated that asize is greater than
2258 * vdev_min_asize.
2259 */
2260 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
2261 ((asize > vd->vdev_asize &&
2262 (vd->vdev_expanding || spa->spa_autoexpand)) ||
2263 (asize < vd->vdev_asize)))
2264 vd->vdev_asize = asize;
2265
2266 vdev_set_min_asize(vd);
2267
2268 /*
2269 * Ensure we can issue some IO before declaring the
2270 * vdev open for business.
2271 */
2272 if (vd->vdev_ops->vdev_op_leaf &&
2273 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
2274 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
2275 VDEV_AUX_ERR_EXCEEDED);
2276 return (error);
2277 }
2278
2279 /*
2280 * Track the minimum allocation size.
2281 */
2282 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
2283 vd->vdev_islog == 0 && vd->vdev_aux == NULL) {
2284 uint64_t min_alloc = vdev_get_min_alloc(vd);
2285 vdev_spa_set_alloc(spa, min_alloc);
2286 }
2287
2288 /*
2289 * If this is a leaf vdev, assess whether a resilver is needed.
2290 * But don't do this if we are doing a reopen for a scrub, since
2291 * this would just restart the scrub we are already doing.
2292 */
2293 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen)
2294 dsl_scan_assess_vdev(spa->spa_dsl_pool, vd);
2295
2296 return (0);
2297 }
2298
2299 static void
vdev_validate_child(void * arg)2300 vdev_validate_child(void *arg)
2301 {
2302 vdev_t *vd = arg;
2303
2304 vd->vdev_validate_thread = curthread;
2305 vd->vdev_validate_error = vdev_validate(vd);
2306 vd->vdev_validate_thread = NULL;
2307 }
2308
2309 /*
2310 * Called once the vdevs are all opened, this routine validates the label
2311 * contents. This needs to be done before vdev_load() so that we don't
2312 * inadvertently do repair I/Os to the wrong device.
2313 *
2314 * This function will only return failure if one of the vdevs indicates that it
2315 * has since been destroyed or exported. This is only possible if
2316 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
2317 * will be updated but the function will return 0.
2318 */
2319 int
vdev_validate(vdev_t * vd)2320 vdev_validate(vdev_t *vd)
2321 {
2322 spa_t *spa = vd->vdev_spa;
2323 taskq_t *tq = NULL;
2324 nvlist_t *label;
2325 uint64_t guid = 0, aux_guid = 0, top_guid;
2326 uint64_t state;
2327 nvlist_t *nvl;
2328 uint64_t txg;
2329 int children = vd->vdev_children;
2330
2331 if (vdev_validate_skip)
2332 return (0);
2333
2334 if (children > 0) {
2335 tq = taskq_create("vdev_validate", children, minclsyspri,
2336 children, children, TASKQ_PREPOPULATE);
2337 }
2338
2339 for (uint64_t c = 0; c < children; c++) {
2340 vdev_t *cvd = vd->vdev_child[c];
2341
2342 if (tq == NULL || vdev_uses_zvols(cvd)) {
2343 vdev_validate_child(cvd);
2344 } else {
2345 VERIFY(taskq_dispatch(tq, vdev_validate_child, cvd,
2346 TQ_SLEEP) != TASKQID_INVALID);
2347 }
2348 }
2349 if (tq != NULL) {
2350 taskq_wait(tq);
2351 taskq_destroy(tq);
2352 }
2353 for (int c = 0; c < children; c++) {
2354 int error = vd->vdev_child[c]->vdev_validate_error;
2355
2356 if (error != 0)
2357 return (SET_ERROR(EBADF));
2358 }
2359
2360
2361 /*
2362 * If the device has already failed, or was marked offline, don't do
2363 * any further validation. Otherwise, label I/O will fail and we will
2364 * overwrite the previous state.
2365 */
2366 if (!vd->vdev_ops->vdev_op_leaf || !vdev_readable(vd))
2367 return (0);
2368
2369 /*
2370 * If we are performing an extreme rewind, we allow for a label that
2371 * was modified at a point after the current txg.
2372 * If config lock is not held do not check for the txg. spa_sync could
2373 * be updating the vdev's label before updating spa_last_synced_txg.
2374 */
2375 if (spa->spa_extreme_rewind || spa_last_synced_txg(spa) == 0 ||
2376 spa_config_held(spa, SCL_CONFIG, RW_WRITER) != SCL_CONFIG)
2377 txg = UINT64_MAX;
2378 else
2379 txg = spa_last_synced_txg(spa);
2380
2381 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
2382 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2383 VDEV_AUX_BAD_LABEL);
2384 vdev_dbgmsg(vd, "vdev_validate: failed reading config for "
2385 "txg %llu", (u_longlong_t)txg);
2386 return (0);
2387 }
2388
2389 /*
2390 * Determine if this vdev has been split off into another
2391 * pool. If so, then refuse to open it.
2392 */
2393 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
2394 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
2395 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2396 VDEV_AUX_SPLIT_POOL);
2397 nvlist_free(label);
2398 vdev_dbgmsg(vd, "vdev_validate: vdev split into other pool");
2399 return (0);
2400 }
2401
2402 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID, &guid) != 0) {
2403 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2404 VDEV_AUX_CORRUPT_DATA);
2405 nvlist_free(label);
2406 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2407 ZPOOL_CONFIG_POOL_GUID);
2408 return (0);
2409 }
2410
2411 /*
2412 * If config is not trusted then ignore the spa guid check. This is
2413 * necessary because if the machine crashed during a re-guid the new
2414 * guid might have been written to all of the vdev labels, but not the
2415 * cached config. The check will be performed again once we have the
2416 * trusted config from the MOS.
2417 */
2418 if (spa->spa_trust_config && guid != spa_guid(spa)) {
2419 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2420 VDEV_AUX_CORRUPT_DATA);
2421 nvlist_free(label);
2422 vdev_dbgmsg(vd, "vdev_validate: vdev label pool_guid doesn't "
2423 "match config (%llu != %llu)", (u_longlong_t)guid,
2424 (u_longlong_t)spa_guid(spa));
2425 return (0);
2426 }
2427
2428 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
2429 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
2430 &aux_guid) != 0)
2431 aux_guid = 0;
2432
2433 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0) {
2434 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2435 VDEV_AUX_CORRUPT_DATA);
2436 nvlist_free(label);
2437 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2438 ZPOOL_CONFIG_GUID);
2439 return (0);
2440 }
2441
2442 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID, &top_guid)
2443 != 0) {
2444 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2445 VDEV_AUX_CORRUPT_DATA);
2446 nvlist_free(label);
2447 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2448 ZPOOL_CONFIG_TOP_GUID);
2449 return (0);
2450 }
2451
2452 /*
2453 * If this vdev just became a top-level vdev because its sibling was
2454 * detached, it will have adopted the parent's vdev guid -- but the
2455 * label may or may not be on disk yet. Fortunately, either version
2456 * of the label will have the same top guid, so if we're a top-level
2457 * vdev, we can safely compare to that instead.
2458 * However, if the config comes from a cachefile that failed to update
2459 * after the detach, a top-level vdev will appear as a non top-level
2460 * vdev in the config. Also relax the constraints if we perform an
2461 * extreme rewind.
2462 *
2463 * If we split this vdev off instead, then we also check the
2464 * original pool's guid. We don't want to consider the vdev
2465 * corrupt if it is partway through a split operation.
2466 */
2467 if (vd->vdev_guid != guid && vd->vdev_guid != aux_guid) {
2468 boolean_t mismatch = B_FALSE;
2469 if (spa->spa_trust_config && !spa->spa_extreme_rewind) {
2470 if (vd != vd->vdev_top || vd->vdev_guid != top_guid)
2471 mismatch = B_TRUE;
2472 } else {
2473 if (vd->vdev_guid != top_guid &&
2474 vd->vdev_top->vdev_guid != guid)
2475 mismatch = B_TRUE;
2476 }
2477
2478 if (mismatch) {
2479 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2480 VDEV_AUX_CORRUPT_DATA);
2481 nvlist_free(label);
2482 vdev_dbgmsg(vd, "vdev_validate: config guid "
2483 "doesn't match label guid");
2484 vdev_dbgmsg(vd, "CONFIG: guid %llu, top_guid %llu",
2485 (u_longlong_t)vd->vdev_guid,
2486 (u_longlong_t)vd->vdev_top->vdev_guid);
2487 vdev_dbgmsg(vd, "LABEL: guid %llu, top_guid %llu, "
2488 "aux_guid %llu", (u_longlong_t)guid,
2489 (u_longlong_t)top_guid, (u_longlong_t)aux_guid);
2490 return (0);
2491 }
2492 }
2493
2494 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
2495 &state) != 0) {
2496 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2497 VDEV_AUX_CORRUPT_DATA);
2498 nvlist_free(label);
2499 vdev_dbgmsg(vd, "vdev_validate: '%s' missing from label",
2500 ZPOOL_CONFIG_POOL_STATE);
2501 return (0);
2502 }
2503
2504 nvlist_free(label);
2505
2506 /*
2507 * If this is a verbatim import, no need to check the
2508 * state of the pool.
2509 */
2510 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
2511 spa_load_state(spa) == SPA_LOAD_OPEN &&
2512 state != POOL_STATE_ACTIVE) {
2513 vdev_dbgmsg(vd, "vdev_validate: invalid pool state (%llu) "
2514 "for spa %s", (u_longlong_t)state, spa->spa_name);
2515 return (SET_ERROR(EBADF));
2516 }
2517
2518 /*
2519 * If we were able to open and validate a vdev that was
2520 * previously marked permanently unavailable, clear that state
2521 * now.
2522 */
2523 if (vd->vdev_not_present)
2524 vd->vdev_not_present = 0;
2525
2526 return (0);
2527 }
2528
2529 static void
vdev_update_path(const char * prefix,char * svd,char ** dvd,uint64_t guid)2530 vdev_update_path(const char *prefix, char *svd, char **dvd, uint64_t guid)
2531 {
2532 if (svd != NULL && *dvd != NULL) {
2533 if (strcmp(svd, *dvd) != 0) {
2534 zfs_dbgmsg("vdev_copy_path: vdev %llu: %s changed "
2535 "from '%s' to '%s'", (u_longlong_t)guid, prefix,
2536 *dvd, svd);
2537 spa_strfree(*dvd);
2538 *dvd = spa_strdup(svd);
2539 }
2540 } else if (svd != NULL) {
2541 *dvd = spa_strdup(svd);
2542 zfs_dbgmsg("vdev_copy_path: vdev %llu: path set to '%s'",
2543 (u_longlong_t)guid, *dvd);
2544 }
2545 }
2546
2547 static void
vdev_copy_path_impl(vdev_t * svd,vdev_t * dvd)2548 vdev_copy_path_impl(vdev_t *svd, vdev_t *dvd)
2549 {
2550 char *old, *new;
2551
2552 vdev_update_path("vdev_path", svd->vdev_path, &dvd->vdev_path,
2553 dvd->vdev_guid);
2554
2555 vdev_update_path("vdev_devid", svd->vdev_devid, &dvd->vdev_devid,
2556 dvd->vdev_guid);
2557
2558 vdev_update_path("vdev_physpath", svd->vdev_physpath,
2559 &dvd->vdev_physpath, dvd->vdev_guid);
2560
2561 /*
2562 * Our enclosure sysfs path may have changed between imports
2563 */
2564 old = dvd->vdev_enc_sysfs_path;
2565 new = svd->vdev_enc_sysfs_path;
2566 if ((old != NULL && new == NULL) ||
2567 (old == NULL && new != NULL) ||
2568 ((old != NULL && new != NULL) && strcmp(new, old) != 0)) {
2569 zfs_dbgmsg("vdev_copy_path: vdev %llu: vdev_enc_sysfs_path "
2570 "changed from '%s' to '%s'", (u_longlong_t)dvd->vdev_guid,
2571 old, new);
2572
2573 if (dvd->vdev_enc_sysfs_path)
2574 spa_strfree(dvd->vdev_enc_sysfs_path);
2575
2576 if (svd->vdev_enc_sysfs_path) {
2577 dvd->vdev_enc_sysfs_path = spa_strdup(
2578 svd->vdev_enc_sysfs_path);
2579 } else {
2580 dvd->vdev_enc_sysfs_path = NULL;
2581 }
2582 }
2583 }
2584
2585 /*
2586 * Recursively copy vdev paths from one vdev to another. Source and destination
2587 * vdev trees must have same geometry otherwise return error. Intended to copy
2588 * paths from userland config into MOS config.
2589 */
2590 int
vdev_copy_path_strict(vdev_t * svd,vdev_t * dvd)2591 vdev_copy_path_strict(vdev_t *svd, vdev_t *dvd)
2592 {
2593 if ((svd->vdev_ops == &vdev_missing_ops) ||
2594 (svd->vdev_ishole && dvd->vdev_ishole) ||
2595 (dvd->vdev_ops == &vdev_indirect_ops))
2596 return (0);
2597
2598 if (svd->vdev_ops != dvd->vdev_ops) {
2599 vdev_dbgmsg(svd, "vdev_copy_path: vdev type mismatch: %s != %s",
2600 svd->vdev_ops->vdev_op_type, dvd->vdev_ops->vdev_op_type);
2601 return (SET_ERROR(EINVAL));
2602 }
2603
2604 if (svd->vdev_guid != dvd->vdev_guid) {
2605 vdev_dbgmsg(svd, "vdev_copy_path: guids mismatch (%llu != "
2606 "%llu)", (u_longlong_t)svd->vdev_guid,
2607 (u_longlong_t)dvd->vdev_guid);
2608 return (SET_ERROR(EINVAL));
2609 }
2610
2611 if (svd->vdev_children != dvd->vdev_children) {
2612 vdev_dbgmsg(svd, "vdev_copy_path: children count mismatch: "
2613 "%llu != %llu", (u_longlong_t)svd->vdev_children,
2614 (u_longlong_t)dvd->vdev_children);
2615 return (SET_ERROR(EINVAL));
2616 }
2617
2618 for (uint64_t i = 0; i < svd->vdev_children; i++) {
2619 int error = vdev_copy_path_strict(svd->vdev_child[i],
2620 dvd->vdev_child[i]);
2621 if (error != 0)
2622 return (error);
2623 }
2624
2625 if (svd->vdev_ops->vdev_op_leaf)
2626 vdev_copy_path_impl(svd, dvd);
2627
2628 return (0);
2629 }
2630
2631 static void
vdev_copy_path_search(vdev_t * stvd,vdev_t * dvd)2632 vdev_copy_path_search(vdev_t *stvd, vdev_t *dvd)
2633 {
2634 ASSERT(stvd->vdev_top == stvd);
2635 ASSERT3U(stvd->vdev_id, ==, dvd->vdev_top->vdev_id);
2636
2637 for (uint64_t i = 0; i < dvd->vdev_children; i++) {
2638 vdev_copy_path_search(stvd, dvd->vdev_child[i]);
2639 }
2640
2641 if (!dvd->vdev_ops->vdev_op_leaf || !vdev_is_concrete(dvd))
2642 return;
2643
2644 /*
2645 * The idea here is that while a vdev can shift positions within
2646 * a top vdev (when replacing, attaching mirror, etc.) it cannot
2647 * step outside of it.
2648 */
2649 vdev_t *vd = vdev_lookup_by_guid(stvd, dvd->vdev_guid);
2650
2651 if (vd == NULL || vd->vdev_ops != dvd->vdev_ops)
2652 return;
2653
2654 ASSERT(vd->vdev_ops->vdev_op_leaf);
2655
2656 vdev_copy_path_impl(vd, dvd);
2657 }
2658
2659 /*
2660 * Recursively copy vdev paths from one root vdev to another. Source and
2661 * destination vdev trees may differ in geometry. For each destination leaf
2662 * vdev, search a vdev with the same guid and top vdev id in the source.
2663 * Intended to copy paths from userland config into MOS config.
2664 */
2665 void
vdev_copy_path_relaxed(vdev_t * srvd,vdev_t * drvd)2666 vdev_copy_path_relaxed(vdev_t *srvd, vdev_t *drvd)
2667 {
2668 uint64_t children = MIN(srvd->vdev_children, drvd->vdev_children);
2669 ASSERT(srvd->vdev_ops == &vdev_root_ops);
2670 ASSERT(drvd->vdev_ops == &vdev_root_ops);
2671
2672 for (uint64_t i = 0; i < children; i++) {
2673 vdev_copy_path_search(srvd->vdev_child[i],
2674 drvd->vdev_child[i]);
2675 }
2676 }
2677
2678 /*
2679 * Close a virtual device.
2680 */
2681 void
vdev_close(vdev_t * vd)2682 vdev_close(vdev_t *vd)
2683 {
2684 vdev_t *pvd = vd->vdev_parent;
2685 spa_t *spa __maybe_unused = vd->vdev_spa;
2686
2687 ASSERT(vd != NULL);
2688 ASSERT(vd->vdev_open_thread == curthread ||
2689 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2690
2691 /*
2692 * If our parent is reopening, then we are as well, unless we are
2693 * going offline.
2694 */
2695 if (pvd != NULL && pvd->vdev_reopening)
2696 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
2697
2698 vd->vdev_ops->vdev_op_close(vd);
2699
2700 /*
2701 * We record the previous state before we close it, so that if we are
2702 * doing a reopen(), we don't generate FMA ereports if we notice that
2703 * it's still faulted.
2704 */
2705 vd->vdev_prevstate = vd->vdev_state;
2706
2707 if (vd->vdev_offline)
2708 vd->vdev_state = VDEV_STATE_OFFLINE;
2709 else
2710 vd->vdev_state = VDEV_STATE_CLOSED;
2711 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2712 }
2713
2714 void
vdev_hold(vdev_t * vd)2715 vdev_hold(vdev_t *vd)
2716 {
2717 spa_t *spa = vd->vdev_spa;
2718
2719 ASSERT(spa_is_root(spa));
2720 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
2721 return;
2722
2723 for (int c = 0; c < vd->vdev_children; c++)
2724 vdev_hold(vd->vdev_child[c]);
2725
2726 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_hold != NULL)
2727 vd->vdev_ops->vdev_op_hold(vd);
2728 }
2729
2730 void
vdev_rele(vdev_t * vd)2731 vdev_rele(vdev_t *vd)
2732 {
2733 ASSERT(spa_is_root(vd->vdev_spa));
2734 for (int c = 0; c < vd->vdev_children; c++)
2735 vdev_rele(vd->vdev_child[c]);
2736
2737 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_ops->vdev_op_rele != NULL)
2738 vd->vdev_ops->vdev_op_rele(vd);
2739 }
2740
2741 /*
2742 * Reopen all interior vdevs and any unopened leaves. We don't actually
2743 * reopen leaf vdevs which had previously been opened as they might deadlock
2744 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
2745 * If the leaf has never been opened then open it, as usual.
2746 */
2747 void
vdev_reopen(vdev_t * vd)2748 vdev_reopen(vdev_t *vd)
2749 {
2750 spa_t *spa = vd->vdev_spa;
2751
2752 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2753
2754 /* set the reopening flag unless we're taking the vdev offline */
2755 vd->vdev_reopening = !vd->vdev_offline;
2756 vdev_close(vd);
2757 (void) vdev_open(vd);
2758
2759 /*
2760 * Call vdev_validate() here to make sure we have the same device.
2761 * Otherwise, a device with an invalid label could be successfully
2762 * opened in response to vdev_reopen().
2763 */
2764 if (vd->vdev_aux) {
2765 (void) vdev_validate_aux(vd);
2766 if (vdev_readable(vd) && vdev_writeable(vd) &&
2767 vd->vdev_aux == &spa->spa_l2cache) {
2768 /*
2769 * In case the vdev is present we should evict all ARC
2770 * buffers and pointers to log blocks and reclaim their
2771 * space before restoring its contents to L2ARC.
2772 */
2773 if (l2arc_vdev_present(vd)) {
2774 l2arc_rebuild_vdev(vd, B_TRUE);
2775 } else {
2776 l2arc_add_vdev(spa, vd);
2777 }
2778 spa_async_request(spa, SPA_ASYNC_L2CACHE_REBUILD);
2779 spa_async_request(spa, SPA_ASYNC_L2CACHE_TRIM);
2780 }
2781 } else {
2782 (void) vdev_validate(vd);
2783 }
2784
2785 /*
2786 * Recheck if resilver is still needed and cancel any
2787 * scheduled resilver if resilver is unneeded.
2788 */
2789 if (!vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL) &&
2790 spa->spa_async_tasks & SPA_ASYNC_RESILVER) {
2791 mutex_enter(&spa->spa_async_lock);
2792 spa->spa_async_tasks &= ~SPA_ASYNC_RESILVER;
2793 mutex_exit(&spa->spa_async_lock);
2794 }
2795
2796 /*
2797 * Reassess parent vdev's health.
2798 */
2799 vdev_propagate_state(vd);
2800 }
2801
2802 int
vdev_create(vdev_t * vd,uint64_t txg,boolean_t isreplacing)2803 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
2804 {
2805 int error;
2806
2807 /*
2808 * Normally, partial opens (e.g. of a mirror) are allowed.
2809 * For a create, however, we want to fail the request if
2810 * there are any components we can't open.
2811 */
2812 error = vdev_open(vd);
2813
2814 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
2815 vdev_close(vd);
2816 return (error ? error : SET_ERROR(ENXIO));
2817 }
2818
2819 /*
2820 * Recursively load DTLs and initialize all labels.
2821 */
2822 if ((error = vdev_dtl_load(vd)) != 0 ||
2823 (error = vdev_label_init(vd, txg, isreplacing ?
2824 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
2825 vdev_close(vd);
2826 return (error);
2827 }
2828
2829 return (0);
2830 }
2831
2832 void
vdev_metaslab_set_size(vdev_t * vd)2833 vdev_metaslab_set_size(vdev_t *vd)
2834 {
2835 uint64_t asize = vd->vdev_asize;
2836 uint64_t ms_count = asize >> zfs_vdev_default_ms_shift;
2837 uint64_t ms_shift;
2838
2839 /*
2840 * There are two dimensions to the metaslab sizing calculation:
2841 * the size of the metaslab and the count of metaslabs per vdev.
2842 *
2843 * The default values used below are a good balance between memory
2844 * usage (larger metaslab size means more memory needed for loaded
2845 * metaslabs; more metaslabs means more memory needed for the
2846 * metaslab_t structs), metaslab load time (larger metaslabs take
2847 * longer to load), and metaslab sync time (more metaslabs means
2848 * more time spent syncing all of them).
2849 *
2850 * In general, we aim for zfs_vdev_default_ms_count (200) metaslabs.
2851 * The range of the dimensions are as follows:
2852 *
2853 * 2^29 <= ms_size <= 2^34
2854 * 16 <= ms_count <= 131,072
2855 *
2856 * On the lower end of vdev sizes, we aim for metaslabs sizes of
2857 * at least 512MB (2^29) to minimize fragmentation effects when
2858 * testing with smaller devices. However, the count constraint
2859 * of at least 16 metaslabs will override this minimum size goal.
2860 *
2861 * On the upper end of vdev sizes, we aim for a maximum metaslab
2862 * size of 16GB. However, we will cap the total count to 2^17
2863 * metaslabs to keep our memory footprint in check and let the
2864 * metaslab size grow from there if that limit is hit.
2865 *
2866 * The net effect of applying above constrains is summarized below.
2867 *
2868 * vdev size metaslab count
2869 * --------------|-----------------
2870 * < 8GB ~16
2871 * 8GB - 100GB one per 512MB
2872 * 100GB - 3TB ~200
2873 * 3TB - 2PB one per 16GB
2874 * > 2PB ~131,072
2875 * --------------------------------
2876 *
2877 * Finally, note that all of the above calculate the initial
2878 * number of metaslabs. Expanding a top-level vdev will result
2879 * in additional metaslabs being allocated making it possible
2880 * to exceed the zfs_vdev_ms_count_limit.
2881 */
2882
2883 if (ms_count < zfs_vdev_min_ms_count)
2884 ms_shift = highbit64(asize / zfs_vdev_min_ms_count);
2885 else if (ms_count > zfs_vdev_default_ms_count)
2886 ms_shift = highbit64(asize / zfs_vdev_default_ms_count);
2887 else
2888 ms_shift = zfs_vdev_default_ms_shift;
2889
2890 if (ms_shift < SPA_MAXBLOCKSHIFT) {
2891 ms_shift = SPA_MAXBLOCKSHIFT;
2892 } else if (ms_shift > zfs_vdev_max_ms_shift) {
2893 ms_shift = zfs_vdev_max_ms_shift;
2894 /* cap the total count to constrain memory footprint */
2895 if ((asize >> ms_shift) > zfs_vdev_ms_count_limit)
2896 ms_shift = highbit64(asize / zfs_vdev_ms_count_limit);
2897 }
2898
2899 vd->vdev_ms_shift = ms_shift;
2900 ASSERT3U(vd->vdev_ms_shift, >=, SPA_MAXBLOCKSHIFT);
2901 }
2902
2903 void
vdev_dirty(vdev_t * vd,int flags,void * arg,uint64_t txg)2904 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
2905 {
2906 ASSERT(vd == vd->vdev_top);
2907 /* indirect vdevs don't have metaslabs or dtls */
2908 ASSERT(vdev_is_concrete(vd) || flags == 0);
2909 ASSERT(ISP2(flags));
2910 ASSERT(spa_writeable(vd->vdev_spa));
2911
2912 if (flags & VDD_METASLAB)
2913 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
2914
2915 if (flags & VDD_DTL)
2916 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
2917
2918 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
2919 }
2920
2921 void
vdev_dirty_leaves(vdev_t * vd,int flags,uint64_t txg)2922 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
2923 {
2924 for (int c = 0; c < vd->vdev_children; c++)
2925 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
2926
2927 if (vd->vdev_ops->vdev_op_leaf)
2928 vdev_dirty(vd->vdev_top, flags, vd, txg);
2929 }
2930
2931 /*
2932 * DTLs.
2933 *
2934 * A vdev's DTL (dirty time log) is the set of transaction groups for which
2935 * the vdev has less than perfect replication. There are four kinds of DTL:
2936 *
2937 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
2938 *
2939 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
2940 *
2941 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
2942 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
2943 * txgs that was scrubbed.
2944 *
2945 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
2946 * persistent errors or just some device being offline.
2947 * Unlike the other three, the DTL_OUTAGE map is not generally
2948 * maintained; it's only computed when needed, typically to
2949 * determine whether a device can be detached.
2950 *
2951 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
2952 * either has the data or it doesn't.
2953 *
2954 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
2955 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
2956 * if any child is less than fully replicated, then so is its parent.
2957 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
2958 * comprising only those txgs which appear in 'maxfaults' or more children;
2959 * those are the txgs we don't have enough replication to read. For example,
2960 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
2961 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
2962 * two child DTL_MISSING maps.
2963 *
2964 * It should be clear from the above that to compute the DTLs and outage maps
2965 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
2966 * Therefore, that is all we keep on disk. When loading the pool, or after
2967 * a configuration change, we generate all other DTLs from first principles.
2968 */
2969 void
vdev_dtl_dirty(vdev_t * vd,vdev_dtl_type_t t,uint64_t txg,uint64_t size)2970 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2971 {
2972 zfs_range_tree_t *rt = vd->vdev_dtl[t];
2973
2974 ASSERT(t < DTL_TYPES);
2975 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2976 ASSERT(spa_writeable(vd->vdev_spa));
2977
2978 mutex_enter(&vd->vdev_dtl_lock);
2979 if (!zfs_range_tree_contains(rt, txg, size))
2980 zfs_range_tree_add(rt, txg, size);
2981 mutex_exit(&vd->vdev_dtl_lock);
2982 }
2983
2984 boolean_t
vdev_dtl_contains(vdev_t * vd,vdev_dtl_type_t t,uint64_t txg,uint64_t size)2985 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
2986 {
2987 zfs_range_tree_t *rt = vd->vdev_dtl[t];
2988 boolean_t dirty = B_FALSE;
2989
2990 ASSERT(t < DTL_TYPES);
2991 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
2992
2993 /*
2994 * While we are loading the pool, the DTLs have not been loaded yet.
2995 * This isn't a problem but it can result in devices being tried
2996 * which are known to not have the data. In which case, the import
2997 * is relying on the checksum to ensure that we get the right data.
2998 * Note that while importing we are only reading the MOS, which is
2999 * always checksummed.
3000 */
3001 mutex_enter(&vd->vdev_dtl_lock);
3002 if (!zfs_range_tree_is_empty(rt))
3003 dirty = zfs_range_tree_contains(rt, txg, size);
3004 mutex_exit(&vd->vdev_dtl_lock);
3005
3006 return (dirty);
3007 }
3008
3009 boolean_t
vdev_dtl_empty(vdev_t * vd,vdev_dtl_type_t t)3010 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
3011 {
3012 zfs_range_tree_t *rt = vd->vdev_dtl[t];
3013 boolean_t empty;
3014
3015 mutex_enter(&vd->vdev_dtl_lock);
3016 empty = zfs_range_tree_is_empty(rt);
3017 mutex_exit(&vd->vdev_dtl_lock);
3018
3019 return (empty);
3020 }
3021
3022 /*
3023 * Check if the txg falls within the range which must be
3024 * resilvered. DVAs outside this range can always be skipped.
3025 */
3026 boolean_t
vdev_default_need_resilver(vdev_t * vd,const dva_t * dva,size_t psize,uint64_t phys_birth)3027 vdev_default_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
3028 uint64_t phys_birth)
3029 {
3030 (void) dva, (void) psize;
3031
3032 /* Set by sequential resilver. */
3033 if (phys_birth == TXG_UNKNOWN)
3034 return (B_TRUE);
3035
3036 return (vdev_dtl_contains(vd, DTL_PARTIAL, phys_birth, 1));
3037 }
3038
3039 /*
3040 * Returns B_TRUE if the vdev determines the DVA needs to be resilvered.
3041 */
3042 boolean_t
vdev_dtl_need_resilver(vdev_t * vd,const dva_t * dva,size_t psize,uint64_t phys_birth)3043 vdev_dtl_need_resilver(vdev_t *vd, const dva_t *dva, size_t psize,
3044 uint64_t phys_birth)
3045 {
3046 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
3047
3048 if (vd->vdev_ops->vdev_op_need_resilver == NULL ||
3049 vd->vdev_ops->vdev_op_leaf)
3050 return (B_TRUE);
3051
3052 return (vd->vdev_ops->vdev_op_need_resilver(vd, dva, psize,
3053 phys_birth));
3054 }
3055
3056 /*
3057 * Returns the lowest txg in the DTL range.
3058 */
3059 static uint64_t
vdev_dtl_min(vdev_t * vd)3060 vdev_dtl_min(vdev_t *vd)
3061 {
3062 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
3063 ASSERT3U(zfs_range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
3064 ASSERT0(vd->vdev_children);
3065
3066 return (zfs_range_tree_min(vd->vdev_dtl[DTL_MISSING]) - 1);
3067 }
3068
3069 /*
3070 * Returns the highest txg in the DTL.
3071 */
3072 static uint64_t
vdev_dtl_max(vdev_t * vd)3073 vdev_dtl_max(vdev_t *vd)
3074 {
3075 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
3076 ASSERT3U(zfs_range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
3077 ASSERT0(vd->vdev_children);
3078
3079 return (zfs_range_tree_max(vd->vdev_dtl[DTL_MISSING]));
3080 }
3081
3082 /*
3083 * Determine if a resilvering vdev should remove any DTL entries from
3084 * its range. If the vdev was resilvering for the entire duration of the
3085 * scan then it should excise that range from its DTLs. Otherwise, this
3086 * vdev is considered partially resilvered and should leave its DTL
3087 * entries intact. The comment in vdev_dtl_reassess() describes how we
3088 * excise the DTLs.
3089 */
3090 static boolean_t
vdev_dtl_should_excise(vdev_t * vd,boolean_t rebuild_done)3091 vdev_dtl_should_excise(vdev_t *vd, boolean_t rebuild_done)
3092 {
3093 ASSERT0(vd->vdev_children);
3094
3095 if (vd->vdev_state < VDEV_STATE_DEGRADED)
3096 return (B_FALSE);
3097
3098 if (vd->vdev_resilver_deferred)
3099 return (B_FALSE);
3100
3101 if (zfs_range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]))
3102 return (B_TRUE);
3103
3104 if (rebuild_done) {
3105 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3106 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
3107
3108 /* Rebuild not initiated by attach */
3109 if (vd->vdev_rebuild_txg == 0)
3110 return (B_TRUE);
3111
3112 /*
3113 * When a rebuild completes without error then all missing data
3114 * up to the rebuild max txg has been reconstructed and the DTL
3115 * is eligible for excision.
3116 */
3117 if (vrp->vrp_rebuild_state == VDEV_REBUILD_COMPLETE &&
3118 vdev_dtl_max(vd) <= vrp->vrp_max_txg) {
3119 ASSERT3U(vrp->vrp_min_txg, <=, vdev_dtl_min(vd));
3120 ASSERT3U(vrp->vrp_min_txg, <, vd->vdev_rebuild_txg);
3121 ASSERT3U(vd->vdev_rebuild_txg, <=, vrp->vrp_max_txg);
3122 return (B_TRUE);
3123 }
3124 } else {
3125 dsl_scan_t *scn = vd->vdev_spa->spa_dsl_pool->dp_scan;
3126 dsl_scan_phys_t *scnp __maybe_unused = &scn->scn_phys;
3127
3128 /* Resilver not initiated by attach */
3129 if (vd->vdev_resilver_txg == 0)
3130 return (B_TRUE);
3131
3132 /*
3133 * When a resilver is initiated the scan will assign the
3134 * scn_max_txg value to the highest txg value that exists
3135 * in all DTLs. If this device's max DTL is not part of this
3136 * scan (i.e. it is not in the range (scn_min_txg, scn_max_txg]
3137 * then it is not eligible for excision.
3138 */
3139 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
3140 ASSERT3U(scnp->scn_min_txg, <=, vdev_dtl_min(vd));
3141 ASSERT3U(scnp->scn_min_txg, <, vd->vdev_resilver_txg);
3142 ASSERT3U(vd->vdev_resilver_txg, <=, scnp->scn_max_txg);
3143 return (B_TRUE);
3144 }
3145 }
3146
3147 return (B_FALSE);
3148 }
3149
3150 /*
3151 * Reassess DTLs after a config change or scrub completion. If txg == 0 no
3152 * write operations will be issued to the pool.
3153 */
3154 static void
vdev_dtl_reassess_impl(vdev_t * vd,uint64_t txg,uint64_t scrub_txg,boolean_t scrub_done,boolean_t rebuild_done,boolean_t faulting)3155 vdev_dtl_reassess_impl(vdev_t *vd, uint64_t txg, uint64_t scrub_txg,
3156 boolean_t scrub_done, boolean_t rebuild_done, boolean_t faulting)
3157 {
3158 spa_t *spa = vd->vdev_spa;
3159 avl_tree_t reftree;
3160 int minref;
3161
3162 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
3163
3164 for (int c = 0; c < vd->vdev_children; c++)
3165 vdev_dtl_reassess_impl(vd->vdev_child[c], txg,
3166 scrub_txg, scrub_done, rebuild_done, faulting);
3167
3168 if (vd == spa->spa_root_vdev || !vdev_is_concrete(vd) || vd->vdev_aux)
3169 return;
3170
3171 if (vd->vdev_ops->vdev_op_leaf) {
3172 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
3173 vdev_rebuild_t *vr = &vd->vdev_top->vdev_rebuild_config;
3174 boolean_t check_excise = B_FALSE;
3175 boolean_t wasempty = B_TRUE;
3176
3177 mutex_enter(&vd->vdev_dtl_lock);
3178
3179 /*
3180 * If requested, pretend the scan or rebuild completed cleanly.
3181 */
3182 if (zfs_scan_ignore_errors) {
3183 if (scn != NULL)
3184 scn->scn_phys.scn_errors = 0;
3185 if (vr != NULL)
3186 vr->vr_rebuild_phys.vrp_errors = 0;
3187 }
3188
3189 if (scrub_txg != 0 &&
3190 !zfs_range_tree_is_empty(vd->vdev_dtl[DTL_MISSING])) {
3191 wasempty = B_FALSE;
3192 zfs_dbgmsg("guid:%llu txg:%llu scrub:%llu started:%d "
3193 "dtl:%llu/%llu errors:%llu",
3194 (u_longlong_t)vd->vdev_guid, (u_longlong_t)txg,
3195 (u_longlong_t)scrub_txg, spa->spa_scrub_started,
3196 (u_longlong_t)vdev_dtl_min(vd),
3197 (u_longlong_t)vdev_dtl_max(vd),
3198 (u_longlong_t)(scn ? scn->scn_phys.scn_errors : 0));
3199 }
3200
3201 /*
3202 * If we've completed a scrub/resilver or a rebuild cleanly
3203 * then determine if this vdev should remove any DTLs. We
3204 * only want to excise regions on vdevs that were available
3205 * during the entire duration of this scan.
3206 */
3207 if (rebuild_done &&
3208 vr != NULL && vr->vr_rebuild_phys.vrp_errors == 0) {
3209 check_excise = B_TRUE;
3210 } else {
3211 if (spa->spa_scrub_started ||
3212 (scn != NULL && scn->scn_phys.scn_errors == 0)) {
3213 check_excise = B_TRUE;
3214 }
3215 }
3216
3217 if (scrub_txg && check_excise &&
3218 vdev_dtl_should_excise(vd, rebuild_done)) {
3219 /*
3220 * We completed a scrub, resilver or rebuild up to
3221 * scrub_txg. If we did it without rebooting, then
3222 * the scrub dtl will be valid, so excise the old
3223 * region and fold in the scrub dtl. Otherwise,
3224 * leave the dtl as-is if there was an error.
3225 *
3226 * There's little trick here: to excise the beginning
3227 * of the DTL_MISSING map, we put it into a reference
3228 * tree and then add a segment with refcnt -1 that
3229 * covers the range [0, scrub_txg). This means
3230 * that each txg in that range has refcnt -1 or 0.
3231 * We then add DTL_SCRUB with a refcnt of 2, so that
3232 * entries in the range [0, scrub_txg) will have a
3233 * positive refcnt -- either 1 or 2. We then convert
3234 * the reference tree into the new DTL_MISSING map.
3235 */
3236 space_reftree_create(&reftree);
3237 space_reftree_add_map(&reftree,
3238 vd->vdev_dtl[DTL_MISSING], 1);
3239 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
3240 space_reftree_add_map(&reftree,
3241 vd->vdev_dtl[DTL_SCRUB], 2);
3242 space_reftree_generate_map(&reftree,
3243 vd->vdev_dtl[DTL_MISSING], 1);
3244 space_reftree_destroy(&reftree);
3245
3246 if (!zfs_range_tree_is_empty(
3247 vd->vdev_dtl[DTL_MISSING])) {
3248 zfs_dbgmsg("update DTL_MISSING:%llu/%llu",
3249 (u_longlong_t)vdev_dtl_min(vd),
3250 (u_longlong_t)vdev_dtl_max(vd));
3251 } else if (!wasempty) {
3252 zfs_dbgmsg("DTL_MISSING is now empty");
3253 }
3254 }
3255 zfs_range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
3256 zfs_range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3257 zfs_range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
3258 if (scrub_done)
3259 zfs_range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL,
3260 NULL);
3261 zfs_range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
3262
3263 /*
3264 * For the faulting case, treat members of a replacing vdev
3265 * as if they are not available. It's more likely than not that
3266 * a vdev in a replacing vdev could encounter read errors so
3267 * treat it as not being able to contribute.
3268 */
3269 if (!vdev_readable(vd) ||
3270 (faulting && vd->vdev_parent != NULL &&
3271 vd->vdev_parent->vdev_ops == &vdev_replacing_ops)) {
3272 zfs_range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
3273 } else {
3274 zfs_range_tree_walk(vd->vdev_dtl[DTL_MISSING],
3275 zfs_range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
3276 }
3277
3278 /*
3279 * If the vdev was resilvering or rebuilding and no longer
3280 * has any DTLs then reset the appropriate flag and dirty
3281 * the top level so that we persist the change.
3282 */
3283 if (txg != 0 &&
3284 zfs_range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3285 zfs_range_tree_is_empty(vd->vdev_dtl[DTL_OUTAGE])) {
3286 if (vd->vdev_rebuild_txg != 0) {
3287 vd->vdev_rebuild_txg = 0;
3288 vdev_config_dirty(vd->vdev_top);
3289 } else if (vd->vdev_resilver_txg != 0) {
3290 vd->vdev_resilver_txg = 0;
3291 vdev_config_dirty(vd->vdev_top);
3292 }
3293 }
3294
3295 mutex_exit(&vd->vdev_dtl_lock);
3296
3297 if (txg != 0)
3298 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
3299 } else {
3300 mutex_enter(&vd->vdev_dtl_lock);
3301 for (int t = 0; t < DTL_TYPES; t++) {
3302 /* account for child's outage in parent's missing map */
3303 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
3304 if (t == DTL_SCRUB) {
3305 /* leaf vdevs only */
3306 continue;
3307 }
3308 if (t == DTL_PARTIAL) {
3309 /* i.e. non-zero */
3310 minref = 1;
3311 } else if (vdev_get_nparity(vd) != 0) {
3312 /* RAIDZ, DRAID */
3313 minref = vdev_get_nparity(vd) + 1;
3314 } else {
3315 /* any kind of mirror */
3316 minref = vd->vdev_children;
3317 }
3318 space_reftree_create(&reftree);
3319 for (int c = 0; c < vd->vdev_children; c++) {
3320 vdev_t *cvd = vd->vdev_child[c];
3321 mutex_enter(&cvd->vdev_dtl_lock);
3322 space_reftree_add_map(&reftree,
3323 cvd->vdev_dtl[s], 1);
3324 mutex_exit(&cvd->vdev_dtl_lock);
3325 }
3326 space_reftree_generate_map(&reftree,
3327 vd->vdev_dtl[t], minref);
3328 space_reftree_destroy(&reftree);
3329 }
3330 mutex_exit(&vd->vdev_dtl_lock);
3331 }
3332
3333 if (vd->vdev_top->vdev_ops == &vdev_raidz_ops) {
3334 raidz_dtl_reassessed(vd);
3335 }
3336 }
3337
3338 void
vdev_dtl_reassess(vdev_t * vd,uint64_t txg,uint64_t scrub_txg,boolean_t scrub_done,boolean_t rebuild_done)3339 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg,
3340 boolean_t scrub_done, boolean_t rebuild_done)
3341 {
3342 return (vdev_dtl_reassess_impl(vd, txg, scrub_txg, scrub_done,
3343 rebuild_done, B_FALSE));
3344 }
3345
3346 /*
3347 * Iterate over all the vdevs except spare, and post kobj events
3348 */
3349 void
vdev_post_kobj_evt(vdev_t * vd)3350 vdev_post_kobj_evt(vdev_t *vd)
3351 {
3352 if (vd->vdev_ops->vdev_op_kobj_evt_post &&
3353 vd->vdev_kobj_flag == B_FALSE) {
3354 vd->vdev_kobj_flag = B_TRUE;
3355 vd->vdev_ops->vdev_op_kobj_evt_post(vd);
3356 }
3357
3358 for (int c = 0; c < vd->vdev_children; c++)
3359 vdev_post_kobj_evt(vd->vdev_child[c]);
3360 }
3361
3362 /*
3363 * Iterate over all the vdevs except spare, and clear kobj events
3364 */
3365 void
vdev_clear_kobj_evt(vdev_t * vd)3366 vdev_clear_kobj_evt(vdev_t *vd)
3367 {
3368 vd->vdev_kobj_flag = B_FALSE;
3369
3370 for (int c = 0; c < vd->vdev_children; c++)
3371 vdev_clear_kobj_evt(vd->vdev_child[c]);
3372 }
3373
3374 int
vdev_dtl_load(vdev_t * vd)3375 vdev_dtl_load(vdev_t *vd)
3376 {
3377 spa_t *spa = vd->vdev_spa;
3378 objset_t *mos = spa->spa_meta_objset;
3379 zfs_range_tree_t *rt;
3380 int error = 0;
3381
3382 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
3383 ASSERT(vdev_is_concrete(vd));
3384
3385 /*
3386 * If the dtl cannot be sync'd there is no need to open it.
3387 */
3388 if (spa->spa_mode == SPA_MODE_READ && !spa->spa_read_spacemaps)
3389 return (0);
3390
3391 error = space_map_open(&vd->vdev_dtl_sm, mos,
3392 vd->vdev_dtl_object, 0, -1ULL, 0);
3393 if (error)
3394 return (error);
3395 ASSERT(vd->vdev_dtl_sm != NULL);
3396
3397 rt = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64, NULL, 0, 0);
3398 error = space_map_load(vd->vdev_dtl_sm, rt, SM_ALLOC);
3399 if (error == 0) {
3400 mutex_enter(&vd->vdev_dtl_lock);
3401 zfs_range_tree_walk(rt, zfs_range_tree_add,
3402 vd->vdev_dtl[DTL_MISSING]);
3403 mutex_exit(&vd->vdev_dtl_lock);
3404 }
3405
3406 zfs_range_tree_vacate(rt, NULL, NULL);
3407 zfs_range_tree_destroy(rt);
3408
3409 return (error);
3410 }
3411
3412 for (int c = 0; c < vd->vdev_children; c++) {
3413 error = vdev_dtl_load(vd->vdev_child[c]);
3414 if (error != 0)
3415 break;
3416 }
3417
3418 return (error);
3419 }
3420
3421 static void
vdev_zap_allocation_data(vdev_t * vd,dmu_tx_t * tx)3422 vdev_zap_allocation_data(vdev_t *vd, dmu_tx_t *tx)
3423 {
3424 spa_t *spa = vd->vdev_spa;
3425 objset_t *mos = spa->spa_meta_objset;
3426 vdev_alloc_bias_t alloc_bias = vd->vdev_alloc_bias;
3427 const char *string;
3428
3429 ASSERT(alloc_bias != VDEV_BIAS_NONE);
3430
3431 string =
3432 (alloc_bias == VDEV_BIAS_LOG) ? VDEV_ALLOC_BIAS_LOG :
3433 (alloc_bias == VDEV_BIAS_SPECIAL) ? VDEV_ALLOC_BIAS_SPECIAL :
3434 (alloc_bias == VDEV_BIAS_DEDUP) ? VDEV_ALLOC_BIAS_DEDUP : NULL;
3435
3436 ASSERT(string != NULL);
3437 VERIFY0(zap_add(mos, vd->vdev_top_zap, VDEV_TOP_ZAP_ALLOCATION_BIAS,
3438 1, strlen(string) + 1, string, tx));
3439
3440 if (alloc_bias == VDEV_BIAS_SPECIAL || alloc_bias == VDEV_BIAS_DEDUP) {
3441 spa_activate_allocation_classes(spa, tx);
3442 }
3443 }
3444
3445 void
vdev_destroy_unlink_zap(vdev_t * vd,uint64_t zapobj,dmu_tx_t * tx)3446 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
3447 {
3448 spa_t *spa = vd->vdev_spa;
3449
3450 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
3451 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3452 zapobj, tx));
3453 }
3454
3455 uint64_t
vdev_create_link_zap(vdev_t * vd,dmu_tx_t * tx)3456 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
3457 {
3458 spa_t *spa = vd->vdev_spa;
3459 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
3460 DMU_OT_NONE, 0, tx);
3461
3462 ASSERT(zap != 0);
3463 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
3464 zap, tx));
3465
3466 return (zap);
3467 }
3468
3469 void
vdev_construct_zaps(vdev_t * vd,dmu_tx_t * tx)3470 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
3471 {
3472 if (vd->vdev_ops != &vdev_hole_ops &&
3473 vd->vdev_ops != &vdev_missing_ops &&
3474 vd->vdev_ops != &vdev_root_ops &&
3475 !vd->vdev_top->vdev_removing) {
3476 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
3477 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
3478 }
3479 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
3480 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
3481 if (vd->vdev_alloc_bias != VDEV_BIAS_NONE)
3482 vdev_zap_allocation_data(vd, tx);
3483 }
3484 }
3485 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_root_zap == 0 &&
3486 spa_feature_is_enabled(vd->vdev_spa, SPA_FEATURE_AVZ_V2)) {
3487 if (!spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_AVZ_V2))
3488 spa_feature_incr(vd->vdev_spa, SPA_FEATURE_AVZ_V2, tx);
3489 vd->vdev_root_zap = vdev_create_link_zap(vd, tx);
3490 }
3491
3492 for (uint64_t i = 0; i < vd->vdev_children; i++) {
3493 vdev_construct_zaps(vd->vdev_child[i], tx);
3494 }
3495 }
3496
3497 static void
vdev_dtl_sync(vdev_t * vd,uint64_t txg)3498 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
3499 {
3500 spa_t *spa = vd->vdev_spa;
3501 zfs_range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
3502 objset_t *mos = spa->spa_meta_objset;
3503 zfs_range_tree_t *rtsync;
3504 dmu_tx_t *tx;
3505 uint64_t object = space_map_object(vd->vdev_dtl_sm);
3506
3507 ASSERT(vdev_is_concrete(vd));
3508 ASSERT(vd->vdev_ops->vdev_op_leaf);
3509
3510 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3511
3512 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
3513 mutex_enter(&vd->vdev_dtl_lock);
3514 space_map_free(vd->vdev_dtl_sm, tx);
3515 space_map_close(vd->vdev_dtl_sm);
3516 vd->vdev_dtl_sm = NULL;
3517 mutex_exit(&vd->vdev_dtl_lock);
3518
3519 /*
3520 * We only destroy the leaf ZAP for detached leaves or for
3521 * removed log devices. Removed data devices handle leaf ZAP
3522 * cleanup later, once cancellation is no longer possible.
3523 */
3524 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
3525 vd->vdev_top->vdev_islog)) {
3526 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
3527 vd->vdev_leaf_zap = 0;
3528 }
3529
3530 dmu_tx_commit(tx);
3531 return;
3532 }
3533
3534 if (vd->vdev_dtl_sm == NULL) {
3535 uint64_t new_object;
3536
3537 new_object = space_map_alloc(mos, zfs_vdev_dtl_sm_blksz, tx);
3538 VERIFY3U(new_object, !=, 0);
3539
3540 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
3541 0, -1ULL, 0));
3542 ASSERT(vd->vdev_dtl_sm != NULL);
3543 }
3544
3545 rtsync = zfs_range_tree_create(NULL, ZFS_RANGE_SEG64, NULL, 0, 0);
3546
3547 mutex_enter(&vd->vdev_dtl_lock);
3548 zfs_range_tree_walk(rt, zfs_range_tree_add, rtsync);
3549 mutex_exit(&vd->vdev_dtl_lock);
3550
3551 space_map_truncate(vd->vdev_dtl_sm, zfs_vdev_dtl_sm_blksz, tx);
3552 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, SM_NO_VDEVID, tx);
3553 zfs_range_tree_vacate(rtsync, NULL, NULL);
3554
3555 zfs_range_tree_destroy(rtsync);
3556
3557 /*
3558 * If the object for the space map has changed then dirty
3559 * the top level so that we update the config.
3560 */
3561 if (object != space_map_object(vd->vdev_dtl_sm)) {
3562 vdev_dbgmsg(vd, "txg %llu, spa %s, DTL old object %llu, "
3563 "new object %llu", (u_longlong_t)txg, spa_name(spa),
3564 (u_longlong_t)object,
3565 (u_longlong_t)space_map_object(vd->vdev_dtl_sm));
3566 vdev_config_dirty(vd->vdev_top);
3567 }
3568
3569 dmu_tx_commit(tx);
3570 }
3571
3572 /*
3573 * Determine whether the specified vdev can be
3574 * - offlined
3575 * - detached
3576 * - removed
3577 * - faulted
3578 * without losing data.
3579 */
3580 boolean_t
vdev_dtl_required(vdev_t * vd)3581 vdev_dtl_required(vdev_t *vd)
3582 {
3583 spa_t *spa = vd->vdev_spa;
3584 vdev_t *tvd = vd->vdev_top;
3585 uint8_t cant_read = vd->vdev_cant_read;
3586 boolean_t required;
3587 boolean_t faulting = vd->vdev_state == VDEV_STATE_FAULTED;
3588
3589 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3590
3591 if (vd == spa->spa_root_vdev || vd == tvd)
3592 return (B_TRUE);
3593
3594 /*
3595 * Temporarily mark the device as unreadable, and then determine
3596 * whether this results in any DTL outages in the top-level vdev.
3597 * If not, we can safely offline/detach/remove the device.
3598 */
3599 vd->vdev_cant_read = B_TRUE;
3600 vdev_dtl_reassess_impl(tvd, 0, 0, B_FALSE, B_FALSE, faulting);
3601 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
3602 vd->vdev_cant_read = cant_read;
3603 vdev_dtl_reassess_impl(tvd, 0, 0, B_FALSE, B_FALSE, faulting);
3604
3605 if (!required && zio_injection_enabled) {
3606 required = !!zio_handle_device_injection(vd, NULL,
3607 SET_ERROR(ECHILD));
3608 }
3609
3610 return (required);
3611 }
3612
3613 /*
3614 * Determine if resilver is needed, and if so the txg range.
3615 */
3616 boolean_t
vdev_resilver_needed(vdev_t * vd,uint64_t * minp,uint64_t * maxp)3617 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
3618 {
3619 boolean_t needed = B_FALSE;
3620 uint64_t thismin = UINT64_MAX;
3621 uint64_t thismax = 0;
3622
3623 if (vd->vdev_children == 0) {
3624 mutex_enter(&vd->vdev_dtl_lock);
3625 if (!zfs_range_tree_is_empty(vd->vdev_dtl[DTL_MISSING]) &&
3626 vdev_writeable(vd)) {
3627
3628 thismin = vdev_dtl_min(vd);
3629 thismax = vdev_dtl_max(vd);
3630 needed = B_TRUE;
3631 }
3632 mutex_exit(&vd->vdev_dtl_lock);
3633 } else {
3634 for (int c = 0; c < vd->vdev_children; c++) {
3635 vdev_t *cvd = vd->vdev_child[c];
3636 uint64_t cmin, cmax;
3637
3638 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
3639 thismin = MIN(thismin, cmin);
3640 thismax = MAX(thismax, cmax);
3641 needed = B_TRUE;
3642 }
3643 }
3644 }
3645
3646 if (needed && minp) {
3647 *minp = thismin;
3648 *maxp = thismax;
3649 }
3650 return (needed);
3651 }
3652
3653 /*
3654 * Gets the checkpoint space map object from the vdev's ZAP. On success sm_obj
3655 * will contain either the checkpoint spacemap object or zero if none exists.
3656 * All other errors are returned to the caller.
3657 */
3658 int
vdev_checkpoint_sm_object(vdev_t * vd,uint64_t * sm_obj)3659 vdev_checkpoint_sm_object(vdev_t *vd, uint64_t *sm_obj)
3660 {
3661 ASSERT0(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER));
3662
3663 if (vd->vdev_top_zap == 0) {
3664 *sm_obj = 0;
3665 return (0);
3666 }
3667
3668 int error = zap_lookup(spa_meta_objset(vd->vdev_spa), vd->vdev_top_zap,
3669 VDEV_TOP_ZAP_POOL_CHECKPOINT_SM, sizeof (uint64_t), 1, sm_obj);
3670 if (error == ENOENT) {
3671 *sm_obj = 0;
3672 error = 0;
3673 }
3674
3675 return (error);
3676 }
3677
3678 int
vdev_load(vdev_t * vd)3679 vdev_load(vdev_t *vd)
3680 {
3681 int children = vd->vdev_children;
3682 int error = 0;
3683 taskq_t *tq = NULL;
3684
3685 /*
3686 * It's only worthwhile to use the taskq for the root vdev, because the
3687 * slow part is metaslab_init, and that only happens for top-level
3688 * vdevs.
3689 */
3690 if (vd->vdev_ops == &vdev_root_ops && vd->vdev_children > 0) {
3691 tq = taskq_create("vdev_load", children, minclsyspri,
3692 children, children, TASKQ_PREPOPULATE);
3693 }
3694
3695 /*
3696 * Recursively load all children.
3697 */
3698 for (int c = 0; c < vd->vdev_children; c++) {
3699 vdev_t *cvd = vd->vdev_child[c];
3700
3701 if (tq == NULL || vdev_uses_zvols(cvd)) {
3702 cvd->vdev_load_error = vdev_load(cvd);
3703 } else {
3704 VERIFY(taskq_dispatch(tq, vdev_load_child,
3705 cvd, TQ_SLEEP) != TASKQID_INVALID);
3706 }
3707 }
3708
3709 if (tq != NULL) {
3710 taskq_wait(tq);
3711 taskq_destroy(tq);
3712 }
3713
3714 for (int c = 0; c < vd->vdev_children; c++) {
3715 int error = vd->vdev_child[c]->vdev_load_error;
3716
3717 if (error != 0)
3718 return (error);
3719 }
3720
3721 vdev_set_deflate_ratio(vd);
3722
3723 if (vd->vdev_ops == &vdev_raidz_ops) {
3724 error = vdev_raidz_load(vd);
3725 if (error != 0)
3726 return (error);
3727 }
3728
3729 /*
3730 * On spa_load path, grab the allocation bias from our zap
3731 */
3732 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3733 spa_t *spa = vd->vdev_spa;
3734 char bias_str[64];
3735
3736 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3737 VDEV_TOP_ZAP_ALLOCATION_BIAS, 1, sizeof (bias_str),
3738 bias_str);
3739 if (error == 0) {
3740 ASSERT(vd->vdev_alloc_bias == VDEV_BIAS_NONE);
3741 vd->vdev_alloc_bias = vdev_derive_alloc_bias(bias_str);
3742 } else if (error != ENOENT) {
3743 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3744 VDEV_AUX_CORRUPT_DATA);
3745 vdev_dbgmsg(vd, "vdev_load: zap_lookup(top_zap=%llu) "
3746 "failed [error=%d]",
3747 (u_longlong_t)vd->vdev_top_zap, error);
3748 return (error);
3749 }
3750 }
3751
3752 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3753 spa_t *spa = vd->vdev_spa;
3754 uint64_t failfast;
3755
3756 error = zap_lookup(spa->spa_meta_objset, vd->vdev_top_zap,
3757 vdev_prop_to_name(VDEV_PROP_FAILFAST), sizeof (failfast),
3758 1, &failfast);
3759 if (error == 0) {
3760 vd->vdev_failfast = failfast & 1;
3761 } else if (error == ENOENT) {
3762 vd->vdev_failfast = vdev_prop_default_numeric(
3763 VDEV_PROP_FAILFAST);
3764 } else {
3765 vdev_dbgmsg(vd,
3766 "vdev_load: zap_lookup(top_zap=%llu) "
3767 "failed [error=%d]",
3768 (u_longlong_t)vd->vdev_top_zap, error);
3769 }
3770 }
3771
3772 /*
3773 * Load any rebuild state from the top-level vdev zap.
3774 */
3775 if (vd == vd->vdev_top && vd->vdev_top_zap != 0) {
3776 error = vdev_rebuild_load(vd);
3777 if (error && error != ENOTSUP) {
3778 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3779 VDEV_AUX_CORRUPT_DATA);
3780 vdev_dbgmsg(vd, "vdev_load: vdev_rebuild_load "
3781 "failed [error=%d]", error);
3782 return (error);
3783 }
3784 }
3785
3786 if (vd->vdev_top_zap != 0 || vd->vdev_leaf_zap != 0) {
3787 uint64_t zapobj;
3788
3789 if (vd->vdev_top_zap != 0)
3790 zapobj = vd->vdev_top_zap;
3791 else
3792 zapobj = vd->vdev_leaf_zap;
3793
3794 error = vdev_prop_get_int(vd, VDEV_PROP_CHECKSUM_N,
3795 &vd->vdev_checksum_n);
3796 if (error && error != ENOENT)
3797 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3798 "failed [error=%d]", (u_longlong_t)zapobj, error);
3799
3800 error = vdev_prop_get_int(vd, VDEV_PROP_CHECKSUM_T,
3801 &vd->vdev_checksum_t);
3802 if (error && error != ENOENT)
3803 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3804 "failed [error=%d]", (u_longlong_t)zapobj, error);
3805
3806 error = vdev_prop_get_int(vd, VDEV_PROP_IO_N,
3807 &vd->vdev_io_n);
3808 if (error && error != ENOENT)
3809 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3810 "failed [error=%d]", (u_longlong_t)zapobj, error);
3811
3812 error = vdev_prop_get_int(vd, VDEV_PROP_IO_T,
3813 &vd->vdev_io_t);
3814 if (error && error != ENOENT)
3815 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3816 "failed [error=%d]", (u_longlong_t)zapobj, error);
3817
3818 error = vdev_prop_get_int(vd, VDEV_PROP_SLOW_IO_N,
3819 &vd->vdev_slow_io_n);
3820 if (error && error != ENOENT)
3821 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3822 "failed [error=%d]", (u_longlong_t)zapobj, error);
3823
3824 error = vdev_prop_get_int(vd, VDEV_PROP_SLOW_IO_T,
3825 &vd->vdev_slow_io_t);
3826 if (error && error != ENOENT)
3827 vdev_dbgmsg(vd, "vdev_load: zap_lookup(zap=%llu) "
3828 "failed [error=%d]", (u_longlong_t)zapobj, error);
3829 }
3830
3831 /*
3832 * If this is a top-level vdev, initialize its metaslabs.
3833 */
3834 if (vd == vd->vdev_top && vdev_is_concrete(vd)) {
3835 vdev_metaslab_group_create(vd);
3836
3837 if (vd->vdev_ashift == 0 || vd->vdev_asize == 0) {
3838 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3839 VDEV_AUX_CORRUPT_DATA);
3840 vdev_dbgmsg(vd, "vdev_load: invalid size. ashift=%llu, "
3841 "asize=%llu", (u_longlong_t)vd->vdev_ashift,
3842 (u_longlong_t)vd->vdev_asize);
3843 return (SET_ERROR(ENXIO));
3844 }
3845
3846 error = vdev_metaslab_init(vd, 0);
3847 if (error != 0) {
3848 vdev_dbgmsg(vd, "vdev_load: metaslab_init failed "
3849 "[error=%d]", error);
3850 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3851 VDEV_AUX_CORRUPT_DATA);
3852 return (error);
3853 }
3854
3855 uint64_t checkpoint_sm_obj;
3856 error = vdev_checkpoint_sm_object(vd, &checkpoint_sm_obj);
3857 if (error == 0 && checkpoint_sm_obj != 0) {
3858 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3859 ASSERT(vd->vdev_asize != 0);
3860 ASSERT3P(vd->vdev_checkpoint_sm, ==, NULL);
3861
3862 error = space_map_open(&vd->vdev_checkpoint_sm,
3863 mos, checkpoint_sm_obj, 0, vd->vdev_asize,
3864 vd->vdev_ashift);
3865 if (error != 0) {
3866 vdev_dbgmsg(vd, "vdev_load: space_map_open "
3867 "failed for checkpoint spacemap (obj %llu) "
3868 "[error=%d]",
3869 (u_longlong_t)checkpoint_sm_obj, error);
3870 return (error);
3871 }
3872 ASSERT3P(vd->vdev_checkpoint_sm, !=, NULL);
3873
3874 /*
3875 * Since the checkpoint_sm contains free entries
3876 * exclusively we can use space_map_allocated() to
3877 * indicate the cumulative checkpointed space that
3878 * has been freed.
3879 */
3880 vd->vdev_stat.vs_checkpoint_space =
3881 -space_map_allocated(vd->vdev_checkpoint_sm);
3882 vd->vdev_spa->spa_checkpoint_info.sci_dspace +=
3883 vd->vdev_stat.vs_checkpoint_space;
3884 } else if (error != 0) {
3885 vdev_dbgmsg(vd, "vdev_load: failed to retrieve "
3886 "checkpoint space map object from vdev ZAP "
3887 "[error=%d]", error);
3888 return (error);
3889 }
3890 }
3891
3892 /*
3893 * If this is a leaf vdev, load its DTL.
3894 */
3895 if (vd->vdev_ops->vdev_op_leaf && (error = vdev_dtl_load(vd)) != 0) {
3896 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3897 VDEV_AUX_CORRUPT_DATA);
3898 vdev_dbgmsg(vd, "vdev_load: vdev_dtl_load failed "
3899 "[error=%d]", error);
3900 return (error);
3901 }
3902
3903 uint64_t obsolete_sm_object;
3904 error = vdev_obsolete_sm_object(vd, &obsolete_sm_object);
3905 if (error == 0 && obsolete_sm_object != 0) {
3906 objset_t *mos = vd->vdev_spa->spa_meta_objset;
3907 ASSERT(vd->vdev_asize != 0);
3908 ASSERT3P(vd->vdev_obsolete_sm, ==, NULL);
3909
3910 if ((error = space_map_open(&vd->vdev_obsolete_sm, mos,
3911 obsolete_sm_object, 0, vd->vdev_asize, 0))) {
3912 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
3913 VDEV_AUX_CORRUPT_DATA);
3914 vdev_dbgmsg(vd, "vdev_load: space_map_open failed for "
3915 "obsolete spacemap (obj %llu) [error=%d]",
3916 (u_longlong_t)obsolete_sm_object, error);
3917 return (error);
3918 }
3919 } else if (error != 0) {
3920 vdev_dbgmsg(vd, "vdev_load: failed to retrieve obsolete "
3921 "space map object from vdev ZAP [error=%d]", error);
3922 return (error);
3923 }
3924
3925 return (0);
3926 }
3927
3928 /*
3929 * The special vdev case is used for hot spares and l2cache devices. Its
3930 * sole purpose it to set the vdev state for the associated vdev. To do this,
3931 * we make sure that we can open the underlying device, then try to read the
3932 * label, and make sure that the label is sane and that it hasn't been
3933 * repurposed to another pool.
3934 */
3935 int
vdev_validate_aux(vdev_t * vd)3936 vdev_validate_aux(vdev_t *vd)
3937 {
3938 nvlist_t *label;
3939 uint64_t guid, version;
3940 uint64_t state;
3941
3942 if (!vdev_readable(vd))
3943 return (0);
3944
3945 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
3946 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3947 VDEV_AUX_CORRUPT_DATA);
3948 return (-1);
3949 }
3950
3951 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
3952 !SPA_VERSION_IS_SUPPORTED(version) ||
3953 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
3954 guid != vd->vdev_guid ||
3955 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
3956 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
3957 VDEV_AUX_CORRUPT_DATA);
3958 nvlist_free(label);
3959 return (-1);
3960 }
3961
3962 /*
3963 * We don't actually check the pool state here. If it's in fact in
3964 * use by another pool, we update this fact on the fly when requested.
3965 */
3966 nvlist_free(label);
3967 return (0);
3968 }
3969
3970 static void
vdev_destroy_ms_flush_data(vdev_t * vd,dmu_tx_t * tx)3971 vdev_destroy_ms_flush_data(vdev_t *vd, dmu_tx_t *tx)
3972 {
3973 objset_t *mos = spa_meta_objset(vd->vdev_spa);
3974
3975 if (vd->vdev_top_zap == 0)
3976 return;
3977
3978 uint64_t object = 0;
3979 int err = zap_lookup(mos, vd->vdev_top_zap,
3980 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, sizeof (uint64_t), 1, &object);
3981 if (err == ENOENT)
3982 return;
3983 VERIFY0(err);
3984
3985 VERIFY0(dmu_object_free(mos, object, tx));
3986 VERIFY0(zap_remove(mos, vd->vdev_top_zap,
3987 VDEV_TOP_ZAP_MS_UNFLUSHED_PHYS_TXGS, tx));
3988 }
3989
3990 /*
3991 * Free the objects used to store this vdev's spacemaps, and the array
3992 * that points to them.
3993 */
3994 void
vdev_destroy_spacemaps(vdev_t * vd,dmu_tx_t * tx)3995 vdev_destroy_spacemaps(vdev_t *vd, dmu_tx_t *tx)
3996 {
3997 if (vd->vdev_ms_array == 0)
3998 return;
3999
4000 objset_t *mos = vd->vdev_spa->spa_meta_objset;
4001 uint64_t array_count = vd->vdev_asize >> vd->vdev_ms_shift;
4002 size_t array_bytes = array_count * sizeof (uint64_t);
4003 uint64_t *smobj_array = kmem_alloc(array_bytes, KM_SLEEP);
4004 VERIFY0(dmu_read(mos, vd->vdev_ms_array, 0,
4005 array_bytes, smobj_array, 0));
4006
4007 for (uint64_t i = 0; i < array_count; i++) {
4008 uint64_t smobj = smobj_array[i];
4009 if (smobj == 0)
4010 continue;
4011
4012 space_map_free_obj(mos, smobj, tx);
4013 }
4014
4015 kmem_free(smobj_array, array_bytes);
4016 VERIFY0(dmu_object_free(mos, vd->vdev_ms_array, tx));
4017 vdev_destroy_ms_flush_data(vd, tx);
4018 vd->vdev_ms_array = 0;
4019 }
4020
4021 static void
vdev_remove_empty_log(vdev_t * vd,uint64_t txg)4022 vdev_remove_empty_log(vdev_t *vd, uint64_t txg)
4023 {
4024 spa_t *spa = vd->vdev_spa;
4025
4026 ASSERT(vd->vdev_islog);
4027 ASSERT(vd == vd->vdev_top);
4028 ASSERT3U(txg, ==, spa_syncing_txg(spa));
4029
4030 dmu_tx_t *tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
4031
4032 vdev_destroy_spacemaps(vd, tx);
4033 if (vd->vdev_top_zap != 0) {
4034 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
4035 vd->vdev_top_zap = 0;
4036 }
4037
4038 dmu_tx_commit(tx);
4039 }
4040
4041 void
vdev_sync_done(vdev_t * vd,uint64_t txg)4042 vdev_sync_done(vdev_t *vd, uint64_t txg)
4043 {
4044 metaslab_t *msp;
4045 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
4046
4047 ASSERT(vdev_is_concrete(vd));
4048
4049 while ((msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
4050 != NULL)
4051 metaslab_sync_done(msp, txg);
4052
4053 if (reassess) {
4054 metaslab_sync_reassess(vd->vdev_mg);
4055 if (vd->vdev_log_mg != NULL)
4056 metaslab_sync_reassess(vd->vdev_log_mg);
4057 }
4058 }
4059
4060 void
vdev_sync(vdev_t * vd,uint64_t txg)4061 vdev_sync(vdev_t *vd, uint64_t txg)
4062 {
4063 spa_t *spa = vd->vdev_spa;
4064 vdev_t *lvd;
4065 metaslab_t *msp;
4066
4067 ASSERT3U(txg, ==, spa->spa_syncing_txg);
4068 dmu_tx_t *tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
4069 if (zfs_range_tree_space(vd->vdev_obsolete_segments) > 0) {
4070 ASSERT(vd->vdev_removing ||
4071 vd->vdev_ops == &vdev_indirect_ops);
4072
4073 vdev_indirect_sync_obsolete(vd, tx);
4074
4075 /*
4076 * If the vdev is indirect, it can't have dirty
4077 * metaslabs or DTLs.
4078 */
4079 if (vd->vdev_ops == &vdev_indirect_ops) {
4080 ASSERT(txg_list_empty(&vd->vdev_ms_list, txg));
4081 ASSERT(txg_list_empty(&vd->vdev_dtl_list, txg));
4082 dmu_tx_commit(tx);
4083 return;
4084 }
4085 }
4086
4087 ASSERT(vdev_is_concrete(vd));
4088
4089 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0 &&
4090 !vd->vdev_removing) {
4091 ASSERT(vd == vd->vdev_top);
4092 ASSERT0(vd->vdev_indirect_config.vic_mapping_object);
4093 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
4094 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
4095 ASSERT(vd->vdev_ms_array != 0);
4096 vdev_config_dirty(vd);
4097 }
4098
4099 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
4100 metaslab_sync(msp, txg);
4101 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
4102 }
4103
4104 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
4105 vdev_dtl_sync(lvd, txg);
4106
4107 /*
4108 * If this is an empty log device being removed, destroy the
4109 * metadata associated with it.
4110 */
4111 if (vd->vdev_islog && vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
4112 vdev_remove_empty_log(vd, txg);
4113
4114 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
4115 dmu_tx_commit(tx);
4116 }
4117
4118 /*
4119 * Return the amount of space that should be (or was) allocated for the given
4120 * psize (compressed block size) in the given TXG. Note that for expanded
4121 * RAIDZ vdevs, the size allocated for older BP's may be larger. See
4122 * vdev_raidz_asize().
4123 */
4124 uint64_t
vdev_psize_to_asize_txg(vdev_t * vd,uint64_t psize,uint64_t txg)4125 vdev_psize_to_asize_txg(vdev_t *vd, uint64_t psize, uint64_t txg)
4126 {
4127 return (vd->vdev_ops->vdev_op_asize(vd, psize, txg));
4128 }
4129
4130 uint64_t
vdev_psize_to_asize(vdev_t * vd,uint64_t psize)4131 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
4132 {
4133 return (vdev_psize_to_asize_txg(vd, psize, 0));
4134 }
4135
4136 /*
4137 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
4138 * not be opened, and no I/O is attempted.
4139 */
4140 int
vdev_fault(spa_t * spa,uint64_t guid,vdev_aux_t aux)4141 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
4142 {
4143 vdev_t *vd, *tvd;
4144
4145 spa_vdev_state_enter(spa, SCL_NONE);
4146
4147 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4148 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4149
4150 if (!vd->vdev_ops->vdev_op_leaf)
4151 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4152
4153 tvd = vd->vdev_top;
4154
4155 /*
4156 * If user did a 'zpool offline -f' then make the fault persist across
4157 * reboots.
4158 */
4159 if (aux == VDEV_AUX_EXTERNAL_PERSIST) {
4160 /*
4161 * There are two kinds of forced faults: temporary and
4162 * persistent. Temporary faults go away at pool import, while
4163 * persistent faults stay set. Both types of faults can be
4164 * cleared with a zpool clear.
4165 *
4166 * We tell if a vdev is persistently faulted by looking at the
4167 * ZPOOL_CONFIG_AUX_STATE nvpair. If it's set to "external" at
4168 * import then it's a persistent fault. Otherwise, it's
4169 * temporary. We get ZPOOL_CONFIG_AUX_STATE set to "external"
4170 * by setting vd.vdev_stat.vs_aux to VDEV_AUX_EXTERNAL. This
4171 * tells vdev_config_generate() (which gets run later) to set
4172 * ZPOOL_CONFIG_AUX_STATE to "external" in the nvlist.
4173 */
4174 vd->vdev_stat.vs_aux = VDEV_AUX_EXTERNAL;
4175 vd->vdev_tmpoffline = B_FALSE;
4176 aux = VDEV_AUX_EXTERNAL;
4177 } else {
4178 vd->vdev_tmpoffline = B_TRUE;
4179 }
4180
4181 /*
4182 * We don't directly use the aux state here, but if we do a
4183 * vdev_reopen(), we need this value to be present to remember why we
4184 * were faulted.
4185 */
4186 vd->vdev_label_aux = aux;
4187
4188 /*
4189 * Faulted state takes precedence over degraded.
4190 */
4191 vd->vdev_delayed_close = B_FALSE;
4192 vd->vdev_faulted = 1ULL;
4193 vd->vdev_degraded = 0ULL;
4194 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
4195
4196 /*
4197 * If this device has the only valid copy of the data, then
4198 * back off and simply mark the vdev as degraded instead.
4199 */
4200 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
4201 vd->vdev_degraded = 1ULL;
4202 vd->vdev_faulted = 0ULL;
4203
4204 /*
4205 * If we reopen the device and it's not dead, only then do we
4206 * mark it degraded.
4207 */
4208 vdev_reopen(tvd);
4209
4210 if (vdev_readable(vd))
4211 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
4212 }
4213
4214 return (spa_vdev_state_exit(spa, vd, 0));
4215 }
4216
4217 /*
4218 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
4219 * user that something is wrong. The vdev continues to operate as normal as far
4220 * as I/O is concerned.
4221 */
4222 int
vdev_degrade(spa_t * spa,uint64_t guid,vdev_aux_t aux)4223 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
4224 {
4225 vdev_t *vd;
4226
4227 spa_vdev_state_enter(spa, SCL_NONE);
4228
4229 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4230 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4231
4232 if (!vd->vdev_ops->vdev_op_leaf)
4233 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4234
4235 /*
4236 * If the vdev is already faulted, then don't do anything.
4237 */
4238 if (vd->vdev_faulted || vd->vdev_degraded)
4239 return (spa_vdev_state_exit(spa, NULL, 0));
4240
4241 vd->vdev_degraded = 1ULL;
4242 if (!vdev_is_dead(vd))
4243 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
4244 aux);
4245
4246 return (spa_vdev_state_exit(spa, vd, 0));
4247 }
4248
4249 int
vdev_remove_wanted(spa_t * spa,uint64_t guid)4250 vdev_remove_wanted(spa_t *spa, uint64_t guid)
4251 {
4252 vdev_t *vd;
4253
4254 spa_vdev_state_enter(spa, SCL_NONE);
4255
4256 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4257 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4258
4259 /*
4260 * If the vdev is already removed, or expanding which can trigger
4261 * repartition add/remove events, then don't do anything.
4262 */
4263 if (vd->vdev_removed || vd->vdev_expanding)
4264 return (spa_vdev_state_exit(spa, NULL, 0));
4265
4266 /*
4267 * Confirm the vdev has been removed, otherwise don't do anything.
4268 */
4269 if (vd->vdev_ops->vdev_op_leaf && !zio_wait(vdev_probe(vd, NULL)))
4270 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(EEXIST)));
4271
4272 vd->vdev_remove_wanted = B_TRUE;
4273 spa_async_request(spa, SPA_ASYNC_REMOVE);
4274
4275 return (spa_vdev_state_exit(spa, vd, 0));
4276 }
4277
4278
4279 /*
4280 * Online the given vdev.
4281 *
4282 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
4283 * spare device should be detached when the device finishes resilvering.
4284 * Second, the online should be treated like a 'test' online case, so no FMA
4285 * events are generated if the device fails to open.
4286 */
4287 int
vdev_online(spa_t * spa,uint64_t guid,uint64_t flags,vdev_state_t * newstate)4288 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
4289 {
4290 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
4291 boolean_t wasoffline;
4292 vdev_state_t oldstate;
4293
4294 spa_vdev_state_enter(spa, SCL_NONE);
4295
4296 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4297 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4298
4299 wasoffline = (vd->vdev_offline || vd->vdev_tmpoffline);
4300 oldstate = vd->vdev_state;
4301
4302 tvd = vd->vdev_top;
4303 vd->vdev_offline = B_FALSE;
4304 vd->vdev_tmpoffline = B_FALSE;
4305 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
4306 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
4307
4308 /* XXX - L2ARC 1.0 does not support expansion */
4309 if (!vd->vdev_aux) {
4310 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4311 pvd->vdev_expanding = !!((flags & ZFS_ONLINE_EXPAND) ||
4312 spa->spa_autoexpand);
4313 vd->vdev_expansion_time = gethrestime_sec();
4314 }
4315
4316 vdev_reopen(tvd);
4317 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
4318
4319 if (!vd->vdev_aux) {
4320 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
4321 pvd->vdev_expanding = B_FALSE;
4322 }
4323
4324 if (newstate)
4325 *newstate = vd->vdev_state;
4326 if ((flags & ZFS_ONLINE_UNSPARE) &&
4327 !vdev_is_dead(vd) && vd->vdev_parent &&
4328 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4329 vd->vdev_parent->vdev_child[0] == vd)
4330 vd->vdev_unspare = B_TRUE;
4331
4332 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
4333
4334 /* XXX - L2ARC 1.0 does not support expansion */
4335 if (vd->vdev_aux)
4336 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
4337 spa->spa_ccw_fail_time = 0;
4338 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
4339 }
4340
4341 /* Restart initializing if necessary */
4342 mutex_enter(&vd->vdev_initialize_lock);
4343 if (vdev_writeable(vd) &&
4344 vd->vdev_initialize_thread == NULL &&
4345 vd->vdev_initialize_state == VDEV_INITIALIZE_ACTIVE) {
4346 (void) vdev_initialize(vd);
4347 }
4348 mutex_exit(&vd->vdev_initialize_lock);
4349
4350 /*
4351 * Restart trimming if necessary. We do not restart trimming for cache
4352 * devices here. This is triggered by l2arc_rebuild_vdev()
4353 * asynchronously for the whole device or in l2arc_evict() as it evicts
4354 * space for upcoming writes.
4355 */
4356 mutex_enter(&vd->vdev_trim_lock);
4357 if (vdev_writeable(vd) && !vd->vdev_isl2cache &&
4358 vd->vdev_trim_thread == NULL &&
4359 vd->vdev_trim_state == VDEV_TRIM_ACTIVE) {
4360 (void) vdev_trim(vd, vd->vdev_trim_rate, vd->vdev_trim_partial,
4361 vd->vdev_trim_secure);
4362 }
4363 mutex_exit(&vd->vdev_trim_lock);
4364
4365 if (wasoffline ||
4366 (oldstate < VDEV_STATE_DEGRADED &&
4367 vd->vdev_state >= VDEV_STATE_DEGRADED)) {
4368 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_ONLINE);
4369
4370 /*
4371 * Asynchronously detach spare vdev if resilver or
4372 * rebuild is not required
4373 */
4374 if (vd->vdev_unspare &&
4375 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4376 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool) &&
4377 !vdev_rebuild_active(tvd))
4378 spa_async_request(spa, SPA_ASYNC_DETACH_SPARE);
4379 }
4380 return (spa_vdev_state_exit(spa, vd, 0));
4381 }
4382
4383 static int
vdev_offline_locked(spa_t * spa,uint64_t guid,uint64_t flags)4384 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
4385 {
4386 vdev_t *vd, *tvd;
4387 int error = 0;
4388 uint64_t generation;
4389 metaslab_group_t *mg;
4390
4391 top:
4392 spa_vdev_state_enter(spa, SCL_ALLOC);
4393
4394 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4395 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENODEV)));
4396
4397 if (!vd->vdev_ops->vdev_op_leaf)
4398 return (spa_vdev_state_exit(spa, NULL, SET_ERROR(ENOTSUP)));
4399
4400 if (vd->vdev_ops == &vdev_draid_spare_ops)
4401 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4402
4403 tvd = vd->vdev_top;
4404 mg = tvd->vdev_mg;
4405 generation = spa->spa_config_generation + 1;
4406
4407 /*
4408 * If the device isn't already offline, try to offline it.
4409 */
4410 if (!vd->vdev_offline) {
4411 /*
4412 * If this device has the only valid copy of some data,
4413 * don't allow it to be offlined. Log devices are always
4414 * expendable.
4415 */
4416 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4417 vdev_dtl_required(vd))
4418 return (spa_vdev_state_exit(spa, NULL,
4419 SET_ERROR(EBUSY)));
4420
4421 /*
4422 * If the top-level is a slog and it has had allocations
4423 * then proceed. We check that the vdev's metaslab group
4424 * is not NULL since it's possible that we may have just
4425 * added this vdev but not yet initialized its metaslabs.
4426 */
4427 if (tvd->vdev_islog && mg != NULL) {
4428 /*
4429 * Prevent any future allocations.
4430 */
4431 ASSERT3P(tvd->vdev_log_mg, ==, NULL);
4432 metaslab_group_passivate(mg);
4433 (void) spa_vdev_state_exit(spa, vd, 0);
4434
4435 error = spa_reset_logs(spa);
4436
4437 /*
4438 * If the log device was successfully reset but has
4439 * checkpointed data, do not offline it.
4440 */
4441 if (error == 0 &&
4442 tvd->vdev_checkpoint_sm != NULL) {
4443 ASSERT3U(space_map_allocated(
4444 tvd->vdev_checkpoint_sm), !=, 0);
4445 error = ZFS_ERR_CHECKPOINT_EXISTS;
4446 }
4447
4448 spa_vdev_state_enter(spa, SCL_ALLOC);
4449
4450 /*
4451 * Check to see if the config has changed.
4452 */
4453 if (error || generation != spa->spa_config_generation) {
4454 metaslab_group_activate(mg);
4455 if (error)
4456 return (spa_vdev_state_exit(spa,
4457 vd, error));
4458 (void) spa_vdev_state_exit(spa, vd, 0);
4459 goto top;
4460 }
4461 ASSERT0(tvd->vdev_stat.vs_alloc);
4462 }
4463
4464 /*
4465 * Offline this device and reopen its top-level vdev.
4466 * If the top-level vdev is a log device then just offline
4467 * it. Otherwise, if this action results in the top-level
4468 * vdev becoming unusable, undo it and fail the request.
4469 */
4470 vd->vdev_offline = B_TRUE;
4471 vdev_reopen(tvd);
4472
4473 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
4474 vdev_is_dead(tvd)) {
4475 vd->vdev_offline = B_FALSE;
4476 vdev_reopen(tvd);
4477 return (spa_vdev_state_exit(spa, NULL,
4478 SET_ERROR(EBUSY)));
4479 }
4480
4481 /*
4482 * Add the device back into the metaslab rotor so that
4483 * once we online the device it's open for business.
4484 */
4485 if (tvd->vdev_islog && mg != NULL)
4486 metaslab_group_activate(mg);
4487 }
4488
4489 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
4490
4491 return (spa_vdev_state_exit(spa, vd, 0));
4492 }
4493
4494 int
vdev_offline(spa_t * spa,uint64_t guid,uint64_t flags)4495 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
4496 {
4497 int error;
4498
4499 mutex_enter(&spa->spa_vdev_top_lock);
4500 error = vdev_offline_locked(spa, guid, flags);
4501 mutex_exit(&spa->spa_vdev_top_lock);
4502
4503 return (error);
4504 }
4505
4506 /*
4507 * Clear the error counts associated with this vdev. Unlike vdev_online() and
4508 * vdev_offline(), we assume the spa config is locked. We also clear all
4509 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
4510 */
4511 void
vdev_clear(spa_t * spa,vdev_t * vd)4512 vdev_clear(spa_t *spa, vdev_t *vd)
4513 {
4514 vdev_t *rvd = spa->spa_root_vdev;
4515
4516 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
4517
4518 if (vd == NULL)
4519 vd = rvd;
4520
4521 vd->vdev_stat.vs_read_errors = 0;
4522 vd->vdev_stat.vs_write_errors = 0;
4523 vd->vdev_stat.vs_checksum_errors = 0;
4524 vd->vdev_stat.vs_dio_verify_errors = 0;
4525 vd->vdev_stat.vs_slow_ios = 0;
4526
4527 for (int c = 0; c < vd->vdev_children; c++)
4528 vdev_clear(spa, vd->vdev_child[c]);
4529
4530 /*
4531 * It makes no sense to "clear" an indirect or removed vdev.
4532 */
4533 if (!vdev_is_concrete(vd) || vd->vdev_removed)
4534 return;
4535
4536 /*
4537 * If we're in the FAULTED state or have experienced failed I/O, then
4538 * clear the persistent state and attempt to reopen the device. We
4539 * also mark the vdev config dirty, so that the new faulted state is
4540 * written out to disk.
4541 */
4542 if (vd->vdev_faulted || vd->vdev_degraded ||
4543 !vdev_readable(vd) || !vdev_writeable(vd)) {
4544 /*
4545 * When reopening in response to a clear event, it may be due to
4546 * a fmadm repair request. In this case, if the device is
4547 * still broken, we want to still post the ereport again.
4548 */
4549 vd->vdev_forcefault = B_TRUE;
4550
4551 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
4552 vd->vdev_cant_read = B_FALSE;
4553 vd->vdev_cant_write = B_FALSE;
4554 vd->vdev_stat.vs_aux = 0;
4555
4556 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
4557
4558 vd->vdev_forcefault = B_FALSE;
4559
4560 if (vd != rvd && vdev_writeable(vd->vdev_top))
4561 vdev_state_dirty(vd->vdev_top);
4562
4563 /* If a resilver isn't required, check if vdevs can be culled */
4564 if (vd->vdev_aux == NULL && !vdev_is_dead(vd) &&
4565 !dsl_scan_resilvering(spa->spa_dsl_pool) &&
4566 !dsl_scan_resilver_scheduled(spa->spa_dsl_pool))
4567 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
4568
4569 spa_event_notify(spa, vd, NULL, ESC_ZFS_VDEV_CLEAR);
4570 }
4571
4572 /*
4573 * When clearing a FMA-diagnosed fault, we always want to
4574 * unspare the device, as we assume that the original spare was
4575 * done in response to the FMA fault.
4576 */
4577 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
4578 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4579 vd->vdev_parent->vdev_child[0] == vd)
4580 vd->vdev_unspare = B_TRUE;
4581
4582 /* Clear recent error events cache (i.e. duplicate events tracking) */
4583 zfs_ereport_clear(spa, vd);
4584 }
4585
4586 boolean_t
vdev_is_dead(vdev_t * vd)4587 vdev_is_dead(vdev_t *vd)
4588 {
4589 /*
4590 * Holes and missing devices are always considered "dead".
4591 * This simplifies the code since we don't have to check for
4592 * these types of devices in the various code paths.
4593 * Instead we rely on the fact that we skip over dead devices
4594 * before issuing I/O to them.
4595 */
4596 return (vd->vdev_state < VDEV_STATE_DEGRADED ||
4597 vd->vdev_ops == &vdev_hole_ops ||
4598 vd->vdev_ops == &vdev_missing_ops);
4599 }
4600
4601 boolean_t
vdev_readable(vdev_t * vd)4602 vdev_readable(vdev_t *vd)
4603 {
4604 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
4605 }
4606
4607 boolean_t
vdev_writeable(vdev_t * vd)4608 vdev_writeable(vdev_t *vd)
4609 {
4610 return (!vdev_is_dead(vd) && !vd->vdev_cant_write &&
4611 vdev_is_concrete(vd));
4612 }
4613
4614 boolean_t
vdev_allocatable(vdev_t * vd)4615 vdev_allocatable(vdev_t *vd)
4616 {
4617 uint64_t state = vd->vdev_state;
4618
4619 /*
4620 * We currently allow allocations from vdevs which may be in the
4621 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
4622 * fails to reopen then we'll catch it later when we're holding
4623 * the proper locks. Note that we have to get the vdev state
4624 * in a local variable because although it changes atomically,
4625 * we're asking two separate questions about it.
4626 */
4627 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
4628 !vd->vdev_cant_write && vdev_is_concrete(vd) &&
4629 vd->vdev_mg->mg_initialized);
4630 }
4631
4632 boolean_t
vdev_accessible(vdev_t * vd,zio_t * zio)4633 vdev_accessible(vdev_t *vd, zio_t *zio)
4634 {
4635 ASSERT(zio->io_vd == vd);
4636
4637 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
4638 return (B_FALSE);
4639
4640 if (zio->io_type == ZIO_TYPE_READ)
4641 return (!vd->vdev_cant_read);
4642
4643 if (zio->io_type == ZIO_TYPE_WRITE)
4644 return (!vd->vdev_cant_write);
4645
4646 return (B_TRUE);
4647 }
4648
4649 static void
vdev_get_child_stat(vdev_t * cvd,vdev_stat_t * vs,vdev_stat_t * cvs)4650 vdev_get_child_stat(vdev_t *cvd, vdev_stat_t *vs, vdev_stat_t *cvs)
4651 {
4652 /*
4653 * Exclude the dRAID spare when aggregating to avoid double counting
4654 * the ops and bytes. These IOs are counted by the physical leaves.
4655 */
4656 if (cvd->vdev_ops == &vdev_draid_spare_ops)
4657 return;
4658
4659 for (int t = 0; t < VS_ZIO_TYPES; t++) {
4660 vs->vs_ops[t] += cvs->vs_ops[t];
4661 vs->vs_bytes[t] += cvs->vs_bytes[t];
4662 }
4663
4664 cvs->vs_scan_removing = cvd->vdev_removing;
4665 }
4666
4667 /*
4668 * Get extended stats
4669 */
4670 static void
vdev_get_child_stat_ex(vdev_t * cvd,vdev_stat_ex_t * vsx,vdev_stat_ex_t * cvsx)4671 vdev_get_child_stat_ex(vdev_t *cvd, vdev_stat_ex_t *vsx, vdev_stat_ex_t *cvsx)
4672 {
4673 (void) cvd;
4674
4675 int t, b;
4676 for (t = 0; t < ZIO_TYPES; t++) {
4677 for (b = 0; b < ARRAY_SIZE(vsx->vsx_disk_histo[0]); b++)
4678 vsx->vsx_disk_histo[t][b] += cvsx->vsx_disk_histo[t][b];
4679
4680 for (b = 0; b < ARRAY_SIZE(vsx->vsx_total_histo[0]); b++) {
4681 vsx->vsx_total_histo[t][b] +=
4682 cvsx->vsx_total_histo[t][b];
4683 }
4684 }
4685
4686 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4687 for (b = 0; b < ARRAY_SIZE(vsx->vsx_queue_histo[0]); b++) {
4688 vsx->vsx_queue_histo[t][b] +=
4689 cvsx->vsx_queue_histo[t][b];
4690 }
4691 vsx->vsx_active_queue[t] += cvsx->vsx_active_queue[t];
4692 vsx->vsx_pend_queue[t] += cvsx->vsx_pend_queue[t];
4693
4694 for (b = 0; b < ARRAY_SIZE(vsx->vsx_ind_histo[0]); b++)
4695 vsx->vsx_ind_histo[t][b] += cvsx->vsx_ind_histo[t][b];
4696
4697 for (b = 0; b < ARRAY_SIZE(vsx->vsx_agg_histo[0]); b++)
4698 vsx->vsx_agg_histo[t][b] += cvsx->vsx_agg_histo[t][b];
4699 }
4700
4701 }
4702
4703 boolean_t
vdev_is_spacemap_addressable(vdev_t * vd)4704 vdev_is_spacemap_addressable(vdev_t *vd)
4705 {
4706 if (spa_feature_is_active(vd->vdev_spa, SPA_FEATURE_SPACEMAP_V2))
4707 return (B_TRUE);
4708
4709 /*
4710 * If double-word space map entries are not enabled we assume
4711 * 47 bits of the space map entry are dedicated to the entry's
4712 * offset (see SM_OFFSET_BITS in space_map.h). We then use that
4713 * to calculate the maximum address that can be described by a
4714 * space map entry for the given device.
4715 */
4716 uint64_t shift = vd->vdev_ashift + SM_OFFSET_BITS;
4717
4718 if (shift >= 63) /* detect potential overflow */
4719 return (B_TRUE);
4720
4721 return (vd->vdev_asize < (1ULL << shift));
4722 }
4723
4724 /*
4725 * Get statistics for the given vdev.
4726 */
4727 static void
vdev_get_stats_ex_impl(vdev_t * vd,vdev_stat_t * vs,vdev_stat_ex_t * vsx)4728 vdev_get_stats_ex_impl(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4729 {
4730 int t;
4731 /*
4732 * If we're getting stats on the root vdev, aggregate the I/O counts
4733 * over all top-level vdevs (i.e. the direct children of the root).
4734 */
4735 if (!vd->vdev_ops->vdev_op_leaf) {
4736 if (vs) {
4737 memset(vs->vs_ops, 0, sizeof (vs->vs_ops));
4738 memset(vs->vs_bytes, 0, sizeof (vs->vs_bytes));
4739 }
4740 if (vsx)
4741 memset(vsx, 0, sizeof (*vsx));
4742
4743 for (int c = 0; c < vd->vdev_children; c++) {
4744 vdev_t *cvd = vd->vdev_child[c];
4745 vdev_stat_t *cvs = &cvd->vdev_stat;
4746 vdev_stat_ex_t *cvsx = &cvd->vdev_stat_ex;
4747
4748 vdev_get_stats_ex_impl(cvd, cvs, cvsx);
4749 if (vs)
4750 vdev_get_child_stat(cvd, vs, cvs);
4751 if (vsx)
4752 vdev_get_child_stat_ex(cvd, vsx, cvsx);
4753 }
4754 } else {
4755 /*
4756 * We're a leaf. Just copy our ZIO active queue stats in. The
4757 * other leaf stats are updated in vdev_stat_update().
4758 */
4759 if (!vsx)
4760 return;
4761
4762 memcpy(vsx, &vd->vdev_stat_ex, sizeof (vd->vdev_stat_ex));
4763
4764 for (t = 0; t < ZIO_PRIORITY_NUM_QUEUEABLE; t++) {
4765 vsx->vsx_active_queue[t] = vd->vdev_queue.vq_cactive[t];
4766 vsx->vsx_pend_queue[t] = vdev_queue_class_length(vd, t);
4767 }
4768 }
4769 }
4770
4771 void
vdev_get_stats_ex(vdev_t * vd,vdev_stat_t * vs,vdev_stat_ex_t * vsx)4772 vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
4773 {
4774 vdev_t *tvd = vd->vdev_top;
4775 mutex_enter(&vd->vdev_stat_lock);
4776 if (vs) {
4777 memcpy(vs, &vd->vdev_stat, sizeof (*vs));
4778 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
4779 vs->vs_state = vd->vdev_state;
4780 vs->vs_rsize = vdev_get_min_asize(vd);
4781
4782 if (vd->vdev_ops->vdev_op_leaf) {
4783 vs->vs_pspace = vd->vdev_psize;
4784 vs->vs_rsize += VDEV_LABEL_START_SIZE +
4785 VDEV_LABEL_END_SIZE;
4786 /*
4787 * Report initializing progress. Since we don't
4788 * have the initializing locks held, this is only
4789 * an estimate (although a fairly accurate one).
4790 */
4791 vs->vs_initialize_bytes_done =
4792 vd->vdev_initialize_bytes_done;
4793 vs->vs_initialize_bytes_est =
4794 vd->vdev_initialize_bytes_est;
4795 vs->vs_initialize_state = vd->vdev_initialize_state;
4796 vs->vs_initialize_action_time =
4797 vd->vdev_initialize_action_time;
4798
4799 /*
4800 * Report manual TRIM progress. Since we don't have
4801 * the manual TRIM locks held, this is only an
4802 * estimate (although fairly accurate one).
4803 */
4804 vs->vs_trim_notsup = !vd->vdev_has_trim;
4805 vs->vs_trim_bytes_done = vd->vdev_trim_bytes_done;
4806 vs->vs_trim_bytes_est = vd->vdev_trim_bytes_est;
4807 vs->vs_trim_state = vd->vdev_trim_state;
4808 vs->vs_trim_action_time = vd->vdev_trim_action_time;
4809
4810 /* Set when there is a deferred resilver. */
4811 vs->vs_resilver_deferred = vd->vdev_resilver_deferred;
4812 }
4813
4814 /*
4815 * Report expandable space on top-level, non-auxiliary devices
4816 * only. The expandable space is reported in terms of metaslab
4817 * sized units since that determines how much space the pool
4818 * can expand.
4819 */
4820 if (vd->vdev_aux == NULL && tvd != NULL) {
4821 vs->vs_esize = P2ALIGN_TYPED(
4822 vd->vdev_max_asize - vd->vdev_asize,
4823 1ULL << tvd->vdev_ms_shift, uint64_t);
4824 }
4825
4826 vs->vs_configured_ashift = vd->vdev_top != NULL
4827 ? vd->vdev_top->vdev_ashift : vd->vdev_ashift;
4828 vs->vs_logical_ashift = vd->vdev_logical_ashift;
4829 if (vd->vdev_physical_ashift <= ASHIFT_MAX)
4830 vs->vs_physical_ashift = vd->vdev_physical_ashift;
4831 else
4832 vs->vs_physical_ashift = 0;
4833
4834 /*
4835 * Report fragmentation and rebuild progress for top-level,
4836 * non-auxiliary, concrete devices.
4837 */
4838 if (vd->vdev_aux == NULL && vd == vd->vdev_top &&
4839 vdev_is_concrete(vd)) {
4840 /*
4841 * The vdev fragmentation rating doesn't take into
4842 * account the embedded slog metaslab (vdev_log_mg).
4843 * Since it's only one metaslab, it would have a tiny
4844 * impact on the overall fragmentation.
4845 */
4846 vs->vs_fragmentation = (vd->vdev_mg != NULL) ?
4847 vd->vdev_mg->mg_fragmentation : 0;
4848 }
4849 vs->vs_noalloc = MAX(vd->vdev_noalloc,
4850 tvd ? tvd->vdev_noalloc : 0);
4851 }
4852
4853 vdev_get_stats_ex_impl(vd, vs, vsx);
4854 mutex_exit(&vd->vdev_stat_lock);
4855 }
4856
4857 void
vdev_get_stats(vdev_t * vd,vdev_stat_t * vs)4858 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
4859 {
4860 return (vdev_get_stats_ex(vd, vs, NULL));
4861 }
4862
4863 void
vdev_clear_stats(vdev_t * vd)4864 vdev_clear_stats(vdev_t *vd)
4865 {
4866 mutex_enter(&vd->vdev_stat_lock);
4867 vd->vdev_stat.vs_space = 0;
4868 vd->vdev_stat.vs_dspace = 0;
4869 vd->vdev_stat.vs_alloc = 0;
4870 mutex_exit(&vd->vdev_stat_lock);
4871 }
4872
4873 void
vdev_scan_stat_init(vdev_t * vd)4874 vdev_scan_stat_init(vdev_t *vd)
4875 {
4876 vdev_stat_t *vs = &vd->vdev_stat;
4877
4878 for (int c = 0; c < vd->vdev_children; c++)
4879 vdev_scan_stat_init(vd->vdev_child[c]);
4880
4881 mutex_enter(&vd->vdev_stat_lock);
4882 vs->vs_scan_processed = 0;
4883 mutex_exit(&vd->vdev_stat_lock);
4884 }
4885
4886 void
vdev_stat_update(zio_t * zio,uint64_t psize)4887 vdev_stat_update(zio_t *zio, uint64_t psize)
4888 {
4889 spa_t *spa = zio->io_spa;
4890 vdev_t *rvd = spa->spa_root_vdev;
4891 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
4892 vdev_t *pvd;
4893 uint64_t txg = zio->io_txg;
4894 /* Suppress ASAN false positive */
4895 #ifdef __SANITIZE_ADDRESS__
4896 vdev_stat_t *vs = vd ? &vd->vdev_stat : NULL;
4897 vdev_stat_ex_t *vsx = vd ? &vd->vdev_stat_ex : NULL;
4898 #else
4899 vdev_stat_t *vs = &vd->vdev_stat;
4900 vdev_stat_ex_t *vsx = &vd->vdev_stat_ex;
4901 #endif
4902 zio_type_t type = zio->io_type;
4903 int flags = zio->io_flags;
4904
4905 /*
4906 * If this i/o is a gang leader, it didn't do any actual work.
4907 */
4908 if (zio->io_gang_tree)
4909 return;
4910
4911 if (zio->io_error == 0) {
4912 /*
4913 * If this is a root i/o, don't count it -- we've already
4914 * counted the top-level vdevs, and vdev_get_stats() will
4915 * aggregate them when asked. This reduces contention on
4916 * the root vdev_stat_lock and implicitly handles blocks
4917 * that compress away to holes, for which there is no i/o.
4918 * (Holes never create vdev children, so all the counters
4919 * remain zero, which is what we want.)
4920 *
4921 * Note: this only applies to successful i/o (io_error == 0)
4922 * because unlike i/o counts, errors are not additive.
4923 * When reading a ditto block, for example, failure of
4924 * one top-level vdev does not imply a root-level error.
4925 */
4926 if (vd == rvd)
4927 return;
4928
4929 ASSERT(vd == zio->io_vd);
4930
4931 if (flags & ZIO_FLAG_IO_BYPASS)
4932 return;
4933
4934 mutex_enter(&vd->vdev_stat_lock);
4935
4936 if (flags & ZIO_FLAG_IO_REPAIR) {
4937 /*
4938 * Repair is the result of a resilver issued by the
4939 * scan thread (spa_sync).
4940 */
4941 if (flags & ZIO_FLAG_SCAN_THREAD) {
4942 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
4943 dsl_scan_phys_t *scn_phys = &scn->scn_phys;
4944 uint64_t *processed = &scn_phys->scn_processed;
4945
4946 if (vd->vdev_ops->vdev_op_leaf)
4947 atomic_add_64(processed, psize);
4948 vs->vs_scan_processed += psize;
4949 }
4950
4951 /*
4952 * Repair is the result of a rebuild issued by the
4953 * rebuild thread (vdev_rebuild_thread). To avoid
4954 * double counting repaired bytes the virtual dRAID
4955 * spare vdev is excluded from the processed bytes.
4956 */
4957 if (zio->io_priority == ZIO_PRIORITY_REBUILD) {
4958 vdev_t *tvd = vd->vdev_top;
4959 vdev_rebuild_t *vr = &tvd->vdev_rebuild_config;
4960 vdev_rebuild_phys_t *vrp = &vr->vr_rebuild_phys;
4961 uint64_t *rebuilt = &vrp->vrp_bytes_rebuilt;
4962
4963 if (vd->vdev_ops->vdev_op_leaf &&
4964 vd->vdev_ops != &vdev_draid_spare_ops) {
4965 atomic_add_64(rebuilt, psize);
4966 }
4967 vs->vs_rebuild_processed += psize;
4968 }
4969
4970 if (flags & ZIO_FLAG_SELF_HEAL)
4971 vs->vs_self_healed += psize;
4972 }
4973
4974 /*
4975 * The bytes/ops/histograms are recorded at the leaf level and
4976 * aggregated into the higher level vdevs in vdev_get_stats().
4977 */
4978 if (vd->vdev_ops->vdev_op_leaf &&
4979 (zio->io_priority < ZIO_PRIORITY_NUM_QUEUEABLE)) {
4980 zio_type_t vs_type = type;
4981 zio_priority_t priority = zio->io_priority;
4982
4983 /*
4984 * TRIM ops and bytes are reported to user space as
4985 * ZIO_TYPE_FLUSH. This is done to preserve the
4986 * vdev_stat_t structure layout for user space.
4987 */
4988 if (type == ZIO_TYPE_TRIM)
4989 vs_type = ZIO_TYPE_FLUSH;
4990
4991 /*
4992 * Solely for the purposes of 'zpool iostat -lqrw'
4993 * reporting use the priority to categorize the IO.
4994 * Only the following are reported to user space:
4995 *
4996 * ZIO_PRIORITY_SYNC_READ,
4997 * ZIO_PRIORITY_SYNC_WRITE,
4998 * ZIO_PRIORITY_ASYNC_READ,
4999 * ZIO_PRIORITY_ASYNC_WRITE,
5000 * ZIO_PRIORITY_SCRUB,
5001 * ZIO_PRIORITY_TRIM,
5002 * ZIO_PRIORITY_REBUILD.
5003 */
5004 if (priority == ZIO_PRIORITY_INITIALIZING) {
5005 ASSERT3U(type, ==, ZIO_TYPE_WRITE);
5006 priority = ZIO_PRIORITY_ASYNC_WRITE;
5007 } else if (priority == ZIO_PRIORITY_REMOVAL) {
5008 priority = ((type == ZIO_TYPE_WRITE) ?
5009 ZIO_PRIORITY_ASYNC_WRITE :
5010 ZIO_PRIORITY_ASYNC_READ);
5011 }
5012
5013 vs->vs_ops[vs_type]++;
5014 vs->vs_bytes[vs_type] += psize;
5015
5016 if (flags & ZIO_FLAG_DELEGATED) {
5017 vsx->vsx_agg_histo[priority]
5018 [RQ_HISTO(zio->io_size)]++;
5019 } else {
5020 vsx->vsx_ind_histo[priority]
5021 [RQ_HISTO(zio->io_size)]++;
5022 }
5023
5024 if (zio->io_delta && zio->io_delay) {
5025 vsx->vsx_queue_histo[priority]
5026 [L_HISTO(zio->io_delta - zio->io_delay)]++;
5027 vsx->vsx_disk_histo[type]
5028 [L_HISTO(zio->io_delay)]++;
5029 vsx->vsx_total_histo[type]
5030 [L_HISTO(zio->io_delta)]++;
5031 }
5032 }
5033
5034 mutex_exit(&vd->vdev_stat_lock);
5035 return;
5036 }
5037
5038 if (flags & ZIO_FLAG_SPECULATIVE)
5039 return;
5040
5041 /*
5042 * If this is an I/O error that is going to be retried, then ignore the
5043 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
5044 * hard errors, when in reality they can happen for any number of
5045 * innocuous reasons (bus resets, MPxIO link failure, etc).
5046 */
5047 if (zio->io_error == EIO &&
5048 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
5049 return;
5050
5051 /*
5052 * Intent logs writes won't propagate their error to the root
5053 * I/O so don't mark these types of failures as pool-level
5054 * errors.
5055 */
5056 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
5057 return;
5058
5059 if (type == ZIO_TYPE_WRITE && txg != 0 &&
5060 (!(flags & ZIO_FLAG_IO_REPAIR) ||
5061 (flags & ZIO_FLAG_SCAN_THREAD) ||
5062 spa->spa_claiming)) {
5063 /*
5064 * This is either a normal write (not a repair), or it's
5065 * a repair induced by the scrub thread, or it's a repair
5066 * made by zil_claim() during spa_load() in the first txg.
5067 * In the normal case, we commit the DTL change in the same
5068 * txg as the block was born. In the scrub-induced repair
5069 * case, we know that scrubs run in first-pass syncing context,
5070 * so we commit the DTL change in spa_syncing_txg(spa).
5071 * In the zil_claim() case, we commit in spa_first_txg(spa).
5072 *
5073 * We currently do not make DTL entries for failed spontaneous
5074 * self-healing writes triggered by normal (non-scrubbing)
5075 * reads, because we have no transactional context in which to
5076 * do so -- and it's not clear that it'd be desirable anyway.
5077 */
5078 if (vd->vdev_ops->vdev_op_leaf) {
5079 uint64_t commit_txg = txg;
5080 if (flags & ZIO_FLAG_SCAN_THREAD) {
5081 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
5082 ASSERT(spa_sync_pass(spa) == 1);
5083 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
5084 commit_txg = spa_syncing_txg(spa);
5085 } else if (spa->spa_claiming) {
5086 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
5087 commit_txg = spa_first_txg(spa);
5088 }
5089 ASSERT(commit_txg >= spa_syncing_txg(spa));
5090 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
5091 return;
5092 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
5093 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
5094 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
5095 }
5096 if (vd != rvd)
5097 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
5098 }
5099 }
5100
5101 int64_t
vdev_deflated_space(vdev_t * vd,int64_t space)5102 vdev_deflated_space(vdev_t *vd, int64_t space)
5103 {
5104 ASSERT((space & (SPA_MINBLOCKSIZE-1)) == 0);
5105 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
5106
5107 return ((space >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio);
5108 }
5109
5110 /*
5111 * Update the in-core space usage stats for this vdev, its metaslab class,
5112 * and the root vdev.
5113 */
5114 void
vdev_space_update(vdev_t * vd,int64_t alloc_delta,int64_t defer_delta,int64_t space_delta)5115 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
5116 int64_t space_delta)
5117 {
5118 (void) defer_delta;
5119 int64_t dspace_delta;
5120 spa_t *spa = vd->vdev_spa;
5121 vdev_t *rvd = spa->spa_root_vdev;
5122
5123 ASSERT(vd == vd->vdev_top);
5124
5125 /*
5126 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
5127 * factor. We must calculate this here and not at the root vdev
5128 * because the root vdev's psize-to-asize is simply the max of its
5129 * children's, thus not accurate enough for us.
5130 */
5131 dspace_delta = vdev_deflated_space(vd, space_delta);
5132
5133 mutex_enter(&vd->vdev_stat_lock);
5134 /* ensure we won't underflow */
5135 if (alloc_delta < 0) {
5136 ASSERT3U(vd->vdev_stat.vs_alloc, >=, -alloc_delta);
5137 }
5138
5139 vd->vdev_stat.vs_alloc += alloc_delta;
5140 vd->vdev_stat.vs_space += space_delta;
5141 vd->vdev_stat.vs_dspace += dspace_delta;
5142 mutex_exit(&vd->vdev_stat_lock);
5143
5144 /* every class but log contributes to root space stats */
5145 if (vd->vdev_mg != NULL && !vd->vdev_islog) {
5146 ASSERT(!vd->vdev_isl2cache);
5147 mutex_enter(&rvd->vdev_stat_lock);
5148 rvd->vdev_stat.vs_alloc += alloc_delta;
5149 rvd->vdev_stat.vs_space += space_delta;
5150 rvd->vdev_stat.vs_dspace += dspace_delta;
5151 mutex_exit(&rvd->vdev_stat_lock);
5152 }
5153 /* Note: metaslab_class_space_update moved to metaslab_space_update */
5154 }
5155
5156 /*
5157 * Mark a top-level vdev's config as dirty, placing it on the dirty list
5158 * so that it will be written out next time the vdev configuration is synced.
5159 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
5160 */
5161 void
vdev_config_dirty(vdev_t * vd)5162 vdev_config_dirty(vdev_t *vd)
5163 {
5164 spa_t *spa = vd->vdev_spa;
5165 vdev_t *rvd = spa->spa_root_vdev;
5166 int c;
5167
5168 ASSERT(spa_writeable(spa));
5169
5170 /*
5171 * If this is an aux vdev (as with l2cache and spare devices), then we
5172 * update the vdev config manually and set the sync flag.
5173 */
5174 if (vd->vdev_aux != NULL) {
5175 spa_aux_vdev_t *sav = vd->vdev_aux;
5176 nvlist_t **aux;
5177 uint_t naux;
5178
5179 for (c = 0; c < sav->sav_count; c++) {
5180 if (sav->sav_vdevs[c] == vd)
5181 break;
5182 }
5183
5184 if (c == sav->sav_count) {
5185 /*
5186 * We're being removed. There's nothing more to do.
5187 */
5188 ASSERT(sav->sav_sync == B_TRUE);
5189 return;
5190 }
5191
5192 sav->sav_sync = B_TRUE;
5193
5194 if (nvlist_lookup_nvlist_array(sav->sav_config,
5195 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
5196 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
5197 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
5198 }
5199
5200 ASSERT(c < naux);
5201
5202 /*
5203 * Setting the nvlist in the middle if the array is a little
5204 * sketchy, but it will work.
5205 */
5206 nvlist_free(aux[c]);
5207 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
5208
5209 return;
5210 }
5211
5212 /*
5213 * The dirty list is protected by the SCL_CONFIG lock. The caller
5214 * must either hold SCL_CONFIG as writer, or must be the sync thread
5215 * (which holds SCL_CONFIG as reader). There's only one sync thread,
5216 * so this is sufficient to ensure mutual exclusion.
5217 */
5218 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
5219 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5220 spa_config_held(spa, SCL_CONFIG, RW_READER)));
5221
5222 if (vd == rvd) {
5223 for (c = 0; c < rvd->vdev_children; c++)
5224 vdev_config_dirty(rvd->vdev_child[c]);
5225 } else {
5226 ASSERT(vd == vd->vdev_top);
5227
5228 if (!list_link_active(&vd->vdev_config_dirty_node) &&
5229 vdev_is_concrete(vd)) {
5230 list_insert_head(&spa->spa_config_dirty_list, vd);
5231 }
5232 }
5233 }
5234
5235 void
vdev_config_clean(vdev_t * vd)5236 vdev_config_clean(vdev_t *vd)
5237 {
5238 spa_t *spa = vd->vdev_spa;
5239
5240 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
5241 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5242 spa_config_held(spa, SCL_CONFIG, RW_READER)));
5243
5244 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
5245 list_remove(&spa->spa_config_dirty_list, vd);
5246 }
5247
5248 /*
5249 * Mark a top-level vdev's state as dirty, so that the next pass of
5250 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
5251 * the state changes from larger config changes because they require
5252 * much less locking, and are often needed for administrative actions.
5253 */
5254 void
vdev_state_dirty(vdev_t * vd)5255 vdev_state_dirty(vdev_t *vd)
5256 {
5257 spa_t *spa = vd->vdev_spa;
5258
5259 ASSERT(spa_writeable(spa));
5260 ASSERT(vd == vd->vdev_top);
5261
5262 /*
5263 * The state list is protected by the SCL_STATE lock. The caller
5264 * must either hold SCL_STATE as writer, or must be the sync thread
5265 * (which holds SCL_STATE as reader). There's only one sync thread,
5266 * so this is sufficient to ensure mutual exclusion.
5267 */
5268 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
5269 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5270 spa_config_held(spa, SCL_STATE, RW_READER)));
5271
5272 if (!list_link_active(&vd->vdev_state_dirty_node) &&
5273 vdev_is_concrete(vd))
5274 list_insert_head(&spa->spa_state_dirty_list, vd);
5275 }
5276
5277 void
vdev_state_clean(vdev_t * vd)5278 vdev_state_clean(vdev_t *vd)
5279 {
5280 spa_t *spa = vd->vdev_spa;
5281
5282 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
5283 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
5284 spa_config_held(spa, SCL_STATE, RW_READER)));
5285
5286 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
5287 list_remove(&spa->spa_state_dirty_list, vd);
5288 }
5289
5290 /*
5291 * Propagate vdev state up from children to parent.
5292 */
5293 void
vdev_propagate_state(vdev_t * vd)5294 vdev_propagate_state(vdev_t *vd)
5295 {
5296 spa_t *spa = vd->vdev_spa;
5297 vdev_t *rvd = spa->spa_root_vdev;
5298 int degraded = 0, faulted = 0;
5299 int corrupted = 0;
5300 vdev_t *child;
5301
5302 if (vd->vdev_children > 0) {
5303 for (int c = 0; c < vd->vdev_children; c++) {
5304 child = vd->vdev_child[c];
5305
5306 /*
5307 * Don't factor holes or indirect vdevs into the
5308 * decision.
5309 */
5310 if (!vdev_is_concrete(child))
5311 continue;
5312
5313 if (!vdev_readable(child) ||
5314 (!vdev_writeable(child) && spa_writeable(spa))) {
5315 /*
5316 * Root special: if there is a top-level log
5317 * device, treat the root vdev as if it were
5318 * degraded.
5319 */
5320 if (child->vdev_islog && vd == rvd)
5321 degraded++;
5322 else
5323 faulted++;
5324 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
5325 degraded++;
5326 }
5327
5328 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
5329 corrupted++;
5330 }
5331
5332 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
5333
5334 /*
5335 * Root special: if there is a top-level vdev that cannot be
5336 * opened due to corrupted metadata, then propagate the root
5337 * vdev's aux state as 'corrupt' rather than 'insufficient
5338 * replicas'.
5339 */
5340 if (corrupted && vd == rvd &&
5341 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
5342 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
5343 VDEV_AUX_CORRUPT_DATA);
5344 }
5345
5346 if (vd->vdev_parent)
5347 vdev_propagate_state(vd->vdev_parent);
5348 }
5349
5350 /*
5351 * Set a vdev's state. If this is during an open, we don't update the parent
5352 * state, because we're in the process of opening children depth-first.
5353 * Otherwise, we propagate the change to the parent.
5354 *
5355 * If this routine places a device in a faulted state, an appropriate ereport is
5356 * generated.
5357 */
5358 void
vdev_set_state(vdev_t * vd,boolean_t isopen,vdev_state_t state,vdev_aux_t aux)5359 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
5360 {
5361 uint64_t save_state;
5362 spa_t *spa = vd->vdev_spa;
5363
5364 if (state == vd->vdev_state) {
5365 /*
5366 * Since vdev_offline() code path is already in an offline
5367 * state we can miss a statechange event to OFFLINE. Check
5368 * the previous state to catch this condition.
5369 */
5370 if (vd->vdev_ops->vdev_op_leaf &&
5371 (state == VDEV_STATE_OFFLINE) &&
5372 (vd->vdev_prevstate >= VDEV_STATE_FAULTED)) {
5373 /* post an offline state change */
5374 zfs_post_state_change(spa, vd, vd->vdev_prevstate);
5375 }
5376 vd->vdev_stat.vs_aux = aux;
5377 return;
5378 }
5379
5380 save_state = vd->vdev_state;
5381
5382 vd->vdev_state = state;
5383 vd->vdev_stat.vs_aux = aux;
5384
5385 /*
5386 * If we are setting the vdev state to anything but an open state, then
5387 * always close the underlying device unless the device has requested
5388 * a delayed close (i.e. we're about to remove or fault the device).
5389 * Otherwise, we keep accessible but invalid devices open forever.
5390 * We don't call vdev_close() itself, because that implies some extra
5391 * checks (offline, etc) that we don't want here. This is limited to
5392 * leaf devices, because otherwise closing the device will affect other
5393 * children.
5394 */
5395 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
5396 vd->vdev_ops->vdev_op_leaf)
5397 vd->vdev_ops->vdev_op_close(vd);
5398
5399 if (vd->vdev_removed &&
5400 state == VDEV_STATE_CANT_OPEN &&
5401 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
5402 /*
5403 * If the previous state is set to VDEV_STATE_REMOVED, then this
5404 * device was previously marked removed and someone attempted to
5405 * reopen it. If this failed due to a nonexistent device, then
5406 * keep the device in the REMOVED state. We also let this be if
5407 * it is one of our special test online cases, which is only
5408 * attempting to online the device and shouldn't generate an FMA
5409 * fault.
5410 */
5411 vd->vdev_state = VDEV_STATE_REMOVED;
5412 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
5413 } else if (state == VDEV_STATE_REMOVED) {
5414 vd->vdev_removed = B_TRUE;
5415 } else if (state == VDEV_STATE_CANT_OPEN) {
5416 /*
5417 * If we fail to open a vdev during an import or recovery, we
5418 * mark it as "not available", which signifies that it was
5419 * never there to begin with. Failure to open such a device
5420 * is not considered an error.
5421 */
5422 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
5423 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
5424 vd->vdev_ops->vdev_op_leaf)
5425 vd->vdev_not_present = 1;
5426
5427 /*
5428 * Post the appropriate ereport. If the 'prevstate' field is
5429 * set to something other than VDEV_STATE_UNKNOWN, it indicates
5430 * that this is part of a vdev_reopen(). In this case, we don't
5431 * want to post the ereport if the device was already in the
5432 * CANT_OPEN state beforehand.
5433 *
5434 * If the 'checkremove' flag is set, then this is an attempt to
5435 * online the device in response to an insertion event. If we
5436 * hit this case, then we have detected an insertion event for a
5437 * faulted or offline device that wasn't in the removed state.
5438 * In this scenario, we don't post an ereport because we are
5439 * about to replace the device, or attempt an online with
5440 * vdev_forcefault, which will generate the fault for us.
5441 */
5442 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
5443 !vd->vdev_not_present && !vd->vdev_checkremove &&
5444 vd != spa->spa_root_vdev) {
5445 const char *class;
5446
5447 switch (aux) {
5448 case VDEV_AUX_OPEN_FAILED:
5449 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
5450 break;
5451 case VDEV_AUX_CORRUPT_DATA:
5452 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
5453 break;
5454 case VDEV_AUX_NO_REPLICAS:
5455 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
5456 break;
5457 case VDEV_AUX_BAD_GUID_SUM:
5458 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
5459 break;
5460 case VDEV_AUX_TOO_SMALL:
5461 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
5462 break;
5463 case VDEV_AUX_BAD_LABEL:
5464 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
5465 break;
5466 case VDEV_AUX_BAD_ASHIFT:
5467 class = FM_EREPORT_ZFS_DEVICE_BAD_ASHIFT;
5468 break;
5469 default:
5470 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
5471 }
5472
5473 (void) zfs_ereport_post(class, spa, vd, NULL, NULL,
5474 save_state);
5475 }
5476
5477 /* Erase any notion of persistent removed state */
5478 vd->vdev_removed = B_FALSE;
5479 } else {
5480 vd->vdev_removed = B_FALSE;
5481 }
5482
5483 /*
5484 * Notify ZED of any significant state-change on a leaf vdev.
5485 *
5486 */
5487 if (vd->vdev_ops->vdev_op_leaf) {
5488 /* preserve original state from a vdev_reopen() */
5489 if ((vd->vdev_prevstate != VDEV_STATE_UNKNOWN) &&
5490 (vd->vdev_prevstate != vd->vdev_state) &&
5491 (save_state <= VDEV_STATE_CLOSED))
5492 save_state = vd->vdev_prevstate;
5493
5494 /* filter out state change due to initial vdev_open */
5495 if (save_state > VDEV_STATE_CLOSED)
5496 zfs_post_state_change(spa, vd, save_state);
5497 }
5498
5499 if (!isopen && vd->vdev_parent)
5500 vdev_propagate_state(vd->vdev_parent);
5501 }
5502
5503 boolean_t
vdev_children_are_offline(vdev_t * vd)5504 vdev_children_are_offline(vdev_t *vd)
5505 {
5506 ASSERT(!vd->vdev_ops->vdev_op_leaf);
5507
5508 for (uint64_t i = 0; i < vd->vdev_children; i++) {
5509 if (vd->vdev_child[i]->vdev_state != VDEV_STATE_OFFLINE)
5510 return (B_FALSE);
5511 }
5512
5513 return (B_TRUE);
5514 }
5515
5516 /*
5517 * Check the vdev configuration to ensure that it's capable of supporting
5518 * a root pool. We do not support partial configuration.
5519 */
5520 boolean_t
vdev_is_bootable(vdev_t * vd)5521 vdev_is_bootable(vdev_t *vd)
5522 {
5523 if (!vd->vdev_ops->vdev_op_leaf) {
5524 const char *vdev_type = vd->vdev_ops->vdev_op_type;
5525
5526 if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0)
5527 return (B_FALSE);
5528 }
5529
5530 for (int c = 0; c < vd->vdev_children; c++) {
5531 if (!vdev_is_bootable(vd->vdev_child[c]))
5532 return (B_FALSE);
5533 }
5534 return (B_TRUE);
5535 }
5536
5537 boolean_t
vdev_is_concrete(vdev_t * vd)5538 vdev_is_concrete(vdev_t *vd)
5539 {
5540 vdev_ops_t *ops = vd->vdev_ops;
5541 if (ops == &vdev_indirect_ops || ops == &vdev_hole_ops ||
5542 ops == &vdev_missing_ops || ops == &vdev_root_ops) {
5543 return (B_FALSE);
5544 } else {
5545 return (B_TRUE);
5546 }
5547 }
5548
5549 /*
5550 * Determine if a log device has valid content. If the vdev was
5551 * removed or faulted in the MOS config then we know that
5552 * the content on the log device has already been written to the pool.
5553 */
5554 boolean_t
vdev_log_state_valid(vdev_t * vd)5555 vdev_log_state_valid(vdev_t *vd)
5556 {
5557 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
5558 !vd->vdev_removed)
5559 return (B_TRUE);
5560
5561 for (int c = 0; c < vd->vdev_children; c++)
5562 if (vdev_log_state_valid(vd->vdev_child[c]))
5563 return (B_TRUE);
5564
5565 return (B_FALSE);
5566 }
5567
5568 /*
5569 * Expand a vdev if possible.
5570 */
5571 void
vdev_expand(vdev_t * vd,uint64_t txg)5572 vdev_expand(vdev_t *vd, uint64_t txg)
5573 {
5574 ASSERT(vd->vdev_top == vd);
5575 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5576 ASSERT(vdev_is_concrete(vd));
5577
5578 vdev_set_deflate_ratio(vd);
5579
5580 if ((vd->vdev_spa->spa_raidz_expand == NULL ||
5581 vd->vdev_spa->spa_raidz_expand->vre_vdev_id != vd->vdev_id) &&
5582 (vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count &&
5583 vdev_is_concrete(vd)) {
5584 vdev_metaslab_group_create(vd);
5585 VERIFY(vdev_metaslab_init(vd, txg) == 0);
5586 vdev_config_dirty(vd);
5587 }
5588 }
5589
5590 /*
5591 * Split a vdev.
5592 */
5593 void
vdev_split(vdev_t * vd)5594 vdev_split(vdev_t *vd)
5595 {
5596 vdev_t *cvd, *pvd = vd->vdev_parent;
5597
5598 VERIFY3U(pvd->vdev_children, >, 1);
5599
5600 vdev_remove_child(pvd, vd);
5601 vdev_compact_children(pvd);
5602
5603 ASSERT3P(pvd->vdev_child, !=, NULL);
5604
5605 cvd = pvd->vdev_child[0];
5606 if (pvd->vdev_children == 1) {
5607 vdev_remove_parent(cvd);
5608 cvd->vdev_splitting = B_TRUE;
5609 }
5610 vdev_propagate_state(cvd);
5611 }
5612
5613 void
vdev_deadman(vdev_t * vd,const char * tag)5614 vdev_deadman(vdev_t *vd, const char *tag)
5615 {
5616 for (int c = 0; c < vd->vdev_children; c++) {
5617 vdev_t *cvd = vd->vdev_child[c];
5618
5619 vdev_deadman(cvd, tag);
5620 }
5621
5622 if (vd->vdev_ops->vdev_op_leaf) {
5623 vdev_queue_t *vq = &vd->vdev_queue;
5624
5625 mutex_enter(&vq->vq_lock);
5626 if (vq->vq_active > 0) {
5627 spa_t *spa = vd->vdev_spa;
5628 zio_t *fio;
5629 uint64_t delta;
5630
5631 zfs_dbgmsg("slow vdev: %s has %u active IOs",
5632 vd->vdev_path, vq->vq_active);
5633
5634 /*
5635 * Look at the head of all the pending queues,
5636 * if any I/O has been outstanding for longer than
5637 * the spa_deadman_synctime invoke the deadman logic.
5638 */
5639 fio = list_head(&vq->vq_active_list);
5640 delta = gethrtime() - fio->io_timestamp;
5641 if (delta > spa_deadman_synctime(spa))
5642 zio_deadman(fio, tag);
5643 }
5644 mutex_exit(&vq->vq_lock);
5645 }
5646 }
5647
5648 void
vdev_defer_resilver(vdev_t * vd)5649 vdev_defer_resilver(vdev_t *vd)
5650 {
5651 ASSERT(vd->vdev_ops->vdev_op_leaf);
5652
5653 vd->vdev_resilver_deferred = B_TRUE;
5654 vd->vdev_spa->spa_resilver_deferred = B_TRUE;
5655 }
5656
5657 /*
5658 * Clears the resilver deferred flag on all leaf devs under vd. Returns
5659 * B_TRUE if we have devices that need to be resilvered and are available to
5660 * accept resilver I/Os.
5661 */
5662 boolean_t
vdev_clear_resilver_deferred(vdev_t * vd,dmu_tx_t * tx)5663 vdev_clear_resilver_deferred(vdev_t *vd, dmu_tx_t *tx)
5664 {
5665 boolean_t resilver_needed = B_FALSE;
5666 spa_t *spa = vd->vdev_spa;
5667
5668 for (int c = 0; c < vd->vdev_children; c++) {
5669 vdev_t *cvd = vd->vdev_child[c];
5670 resilver_needed |= vdev_clear_resilver_deferred(cvd, tx);
5671 }
5672
5673 if (vd == spa->spa_root_vdev &&
5674 spa_feature_is_active(spa, SPA_FEATURE_RESILVER_DEFER)) {
5675 spa_feature_decr(spa, SPA_FEATURE_RESILVER_DEFER, tx);
5676 vdev_config_dirty(vd);
5677 spa->spa_resilver_deferred = B_FALSE;
5678 return (resilver_needed);
5679 }
5680
5681 if (!vdev_is_concrete(vd) || vd->vdev_aux ||
5682 !vd->vdev_ops->vdev_op_leaf)
5683 return (resilver_needed);
5684
5685 vd->vdev_resilver_deferred = B_FALSE;
5686
5687 return (!vdev_is_dead(vd) && !vd->vdev_offline &&
5688 vdev_resilver_needed(vd, NULL, NULL));
5689 }
5690
5691 boolean_t
vdev_xlate_is_empty(zfs_range_seg64_t * rs)5692 vdev_xlate_is_empty(zfs_range_seg64_t *rs)
5693 {
5694 return (rs->rs_start == rs->rs_end);
5695 }
5696
5697 /*
5698 * Translate a logical range to the first contiguous physical range for the
5699 * specified vdev_t. This function is initially called with a leaf vdev and
5700 * will walk each parent vdev until it reaches a top-level vdev. Once the
5701 * top-level is reached the physical range is initialized and the recursive
5702 * function begins to unwind. As it unwinds it calls the parent's vdev
5703 * specific translation function to do the real conversion.
5704 */
5705 void
vdev_xlate(vdev_t * vd,const zfs_range_seg64_t * logical_rs,zfs_range_seg64_t * physical_rs,zfs_range_seg64_t * remain_rs)5706 vdev_xlate(vdev_t *vd, const zfs_range_seg64_t *logical_rs,
5707 zfs_range_seg64_t *physical_rs, zfs_range_seg64_t *remain_rs)
5708 {
5709 /*
5710 * Walk up the vdev tree
5711 */
5712 if (vd != vd->vdev_top) {
5713 vdev_xlate(vd->vdev_parent, logical_rs, physical_rs,
5714 remain_rs);
5715 } else {
5716 /*
5717 * We've reached the top-level vdev, initialize the physical
5718 * range to the logical range and set an empty remaining
5719 * range then start to unwind.
5720 */
5721 physical_rs->rs_start = logical_rs->rs_start;
5722 physical_rs->rs_end = logical_rs->rs_end;
5723
5724 remain_rs->rs_start = logical_rs->rs_start;
5725 remain_rs->rs_end = logical_rs->rs_start;
5726
5727 return;
5728 }
5729
5730 vdev_t *pvd = vd->vdev_parent;
5731 ASSERT3P(pvd, !=, NULL);
5732 ASSERT3P(pvd->vdev_ops->vdev_op_xlate, !=, NULL);
5733
5734 /*
5735 * As this recursive function unwinds, translate the logical
5736 * range into its physical and any remaining components by calling
5737 * the vdev specific translate function.
5738 */
5739 zfs_range_seg64_t intermediate = { 0 };
5740 pvd->vdev_ops->vdev_op_xlate(vd, physical_rs, &intermediate, remain_rs);
5741
5742 physical_rs->rs_start = intermediate.rs_start;
5743 physical_rs->rs_end = intermediate.rs_end;
5744 }
5745
5746 void
vdev_xlate_walk(vdev_t * vd,const zfs_range_seg64_t * logical_rs,vdev_xlate_func_t * func,void * arg)5747 vdev_xlate_walk(vdev_t *vd, const zfs_range_seg64_t *logical_rs,
5748 vdev_xlate_func_t *func, void *arg)
5749 {
5750 zfs_range_seg64_t iter_rs = *logical_rs;
5751 zfs_range_seg64_t physical_rs;
5752 zfs_range_seg64_t remain_rs;
5753
5754 while (!vdev_xlate_is_empty(&iter_rs)) {
5755
5756 vdev_xlate(vd, &iter_rs, &physical_rs, &remain_rs);
5757
5758 /*
5759 * With raidz and dRAID, it's possible that the logical range
5760 * does not live on this leaf vdev. Only when there is a non-
5761 * zero physical size call the provided function.
5762 */
5763 if (!vdev_xlate_is_empty(&physical_rs))
5764 func(arg, &physical_rs);
5765
5766 iter_rs = remain_rs;
5767 }
5768 }
5769
5770 static char *
vdev_name(vdev_t * vd,char * buf,int buflen)5771 vdev_name(vdev_t *vd, char *buf, int buflen)
5772 {
5773 if (vd->vdev_path == NULL) {
5774 if (strcmp(vd->vdev_ops->vdev_op_type, "root") == 0) {
5775 strlcpy(buf, vd->vdev_spa->spa_name, buflen);
5776 } else if (!vd->vdev_ops->vdev_op_leaf) {
5777 snprintf(buf, buflen, "%s-%llu",
5778 vd->vdev_ops->vdev_op_type,
5779 (u_longlong_t)vd->vdev_id);
5780 }
5781 } else {
5782 strlcpy(buf, vd->vdev_path, buflen);
5783 }
5784 return (buf);
5785 }
5786
5787 /*
5788 * Look at the vdev tree and determine whether any devices are currently being
5789 * replaced.
5790 */
5791 boolean_t
vdev_replace_in_progress(vdev_t * vdev)5792 vdev_replace_in_progress(vdev_t *vdev)
5793 {
5794 ASSERT(spa_config_held(vdev->vdev_spa, SCL_ALL, RW_READER) != 0);
5795
5796 if (vdev->vdev_ops == &vdev_replacing_ops)
5797 return (B_TRUE);
5798
5799 /*
5800 * A 'spare' vdev indicates that we have a replace in progress, unless
5801 * it has exactly two children, and the second, the hot spare, has
5802 * finished being resilvered.
5803 */
5804 if (vdev->vdev_ops == &vdev_spare_ops && (vdev->vdev_children > 2 ||
5805 !vdev_dtl_empty(vdev->vdev_child[1], DTL_MISSING)))
5806 return (B_TRUE);
5807
5808 for (int i = 0; i < vdev->vdev_children; i++) {
5809 if (vdev_replace_in_progress(vdev->vdev_child[i]))
5810 return (B_TRUE);
5811 }
5812
5813 return (B_FALSE);
5814 }
5815
5816 /*
5817 * Add a (source=src, propname=propval) list to an nvlist.
5818 */
5819 static void
vdev_prop_add_list(nvlist_t * nvl,const char * propname,const char * strval,uint64_t intval,zprop_source_t src)5820 vdev_prop_add_list(nvlist_t *nvl, const char *propname, const char *strval,
5821 uint64_t intval, zprop_source_t src)
5822 {
5823 nvlist_t *propval;
5824
5825 propval = fnvlist_alloc();
5826 fnvlist_add_uint64(propval, ZPROP_SOURCE, src);
5827
5828 if (strval != NULL)
5829 fnvlist_add_string(propval, ZPROP_VALUE, strval);
5830 else
5831 fnvlist_add_uint64(propval, ZPROP_VALUE, intval);
5832
5833 fnvlist_add_nvlist(nvl, propname, propval);
5834 nvlist_free(propval);
5835 }
5836
5837 static void
vdev_props_set_sync(void * arg,dmu_tx_t * tx)5838 vdev_props_set_sync(void *arg, dmu_tx_t *tx)
5839 {
5840 vdev_t *vd;
5841 nvlist_t *nvp = arg;
5842 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
5843 objset_t *mos = spa->spa_meta_objset;
5844 nvpair_t *elem = NULL;
5845 uint64_t vdev_guid;
5846 uint64_t objid;
5847 nvlist_t *nvprops;
5848
5849 vdev_guid = fnvlist_lookup_uint64(nvp, ZPOOL_VDEV_PROPS_SET_VDEV);
5850 nvprops = fnvlist_lookup_nvlist(nvp, ZPOOL_VDEV_PROPS_SET_PROPS);
5851 vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE);
5852
5853 /* this vdev could get removed while waiting for this sync task */
5854 if (vd == NULL)
5855 return;
5856
5857 /*
5858 * Set vdev property values in the vdev props mos object.
5859 */
5860 if (vd->vdev_root_zap != 0) {
5861 objid = vd->vdev_root_zap;
5862 } else if (vd->vdev_top_zap != 0) {
5863 objid = vd->vdev_top_zap;
5864 } else if (vd->vdev_leaf_zap != 0) {
5865 objid = vd->vdev_leaf_zap;
5866 } else {
5867 panic("unexpected vdev type");
5868 }
5869
5870 mutex_enter(&spa->spa_props_lock);
5871
5872 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5873 uint64_t intval;
5874 const char *strval;
5875 vdev_prop_t prop;
5876 const char *propname = nvpair_name(elem);
5877 zprop_type_t proptype;
5878
5879 switch (prop = vdev_name_to_prop(propname)) {
5880 case VDEV_PROP_USERPROP:
5881 if (vdev_prop_user(propname)) {
5882 strval = fnvpair_value_string(elem);
5883 if (strlen(strval) == 0) {
5884 /* remove the property if value == "" */
5885 (void) zap_remove(mos, objid, propname,
5886 tx);
5887 } else {
5888 VERIFY0(zap_update(mos, objid, propname,
5889 1, strlen(strval) + 1, strval, tx));
5890 }
5891 spa_history_log_internal(spa, "vdev set", tx,
5892 "vdev_guid=%llu: %s=%s",
5893 (u_longlong_t)vdev_guid, nvpair_name(elem),
5894 strval);
5895 }
5896 break;
5897 default:
5898 /* normalize the property name */
5899 propname = vdev_prop_to_name(prop);
5900 proptype = vdev_prop_get_type(prop);
5901
5902 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5903 ASSERT(proptype == PROP_TYPE_STRING);
5904 strval = fnvpair_value_string(elem);
5905 VERIFY0(zap_update(mos, objid, propname,
5906 1, strlen(strval) + 1, strval, tx));
5907 spa_history_log_internal(spa, "vdev set", tx,
5908 "vdev_guid=%llu: %s=%s",
5909 (u_longlong_t)vdev_guid, nvpair_name(elem),
5910 strval);
5911 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5912 intval = fnvpair_value_uint64(elem);
5913
5914 if (proptype == PROP_TYPE_INDEX) {
5915 const char *unused;
5916 VERIFY0(vdev_prop_index_to_string(
5917 prop, intval, &unused));
5918 }
5919 VERIFY0(zap_update(mos, objid, propname,
5920 sizeof (uint64_t), 1, &intval, tx));
5921 spa_history_log_internal(spa, "vdev set", tx,
5922 "vdev_guid=%llu: %s=%lld",
5923 (u_longlong_t)vdev_guid,
5924 nvpair_name(elem), (longlong_t)intval);
5925 } else {
5926 panic("invalid vdev property type %u",
5927 nvpair_type(elem));
5928 }
5929 }
5930
5931 }
5932
5933 mutex_exit(&spa->spa_props_lock);
5934 }
5935
5936 int
vdev_prop_set(vdev_t * vd,nvlist_t * innvl,nvlist_t * outnvl)5937 vdev_prop_set(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
5938 {
5939 spa_t *spa = vd->vdev_spa;
5940 nvpair_t *elem = NULL;
5941 uint64_t vdev_guid;
5942 nvlist_t *nvprops;
5943 int error = 0;
5944
5945 ASSERT(vd != NULL);
5946
5947 /* Check that vdev has a zap we can use */
5948 if (vd->vdev_root_zap == 0 &&
5949 vd->vdev_top_zap == 0 &&
5950 vd->vdev_leaf_zap == 0)
5951 return (SET_ERROR(EINVAL));
5952
5953 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_SET_VDEV,
5954 &vdev_guid) != 0)
5955 return (SET_ERROR(EINVAL));
5956
5957 if (nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_SET_PROPS,
5958 &nvprops) != 0)
5959 return (SET_ERROR(EINVAL));
5960
5961 if ((vd = spa_lookup_by_guid(spa, vdev_guid, B_TRUE)) == NULL)
5962 return (SET_ERROR(EINVAL));
5963
5964 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
5965 const char *propname = nvpair_name(elem);
5966 vdev_prop_t prop = vdev_name_to_prop(propname);
5967 uint64_t intval = 0;
5968 const char *strval = NULL;
5969
5970 if (prop == VDEV_PROP_USERPROP && !vdev_prop_user(propname)) {
5971 error = EINVAL;
5972 goto end;
5973 }
5974
5975 if (prop != VDEV_PROP_USERPROP && vdev_prop_readonly(prop)) {
5976 error = EROFS;
5977 goto end;
5978 }
5979
5980 /* Special Processing */
5981 switch (prop) {
5982 case VDEV_PROP_PATH:
5983 if (vd->vdev_path == NULL) {
5984 error = EROFS;
5985 break;
5986 }
5987 if (nvpair_value_string(elem, &strval) != 0) {
5988 error = EINVAL;
5989 break;
5990 }
5991 /* New path must start with /dev/ */
5992 if (strncmp(strval, "/dev/", 5)) {
5993 error = EINVAL;
5994 break;
5995 }
5996 error = spa_vdev_setpath(spa, vdev_guid, strval);
5997 break;
5998 case VDEV_PROP_ALLOCATING:
5999 if (nvpair_value_uint64(elem, &intval) != 0) {
6000 error = EINVAL;
6001 break;
6002 }
6003 if (intval != vd->vdev_noalloc)
6004 break;
6005 if (intval == 0)
6006 error = spa_vdev_noalloc(spa, vdev_guid);
6007 else
6008 error = spa_vdev_alloc(spa, vdev_guid);
6009 break;
6010 case VDEV_PROP_FAILFAST:
6011 if (nvpair_value_uint64(elem, &intval) != 0) {
6012 error = EINVAL;
6013 break;
6014 }
6015 vd->vdev_failfast = intval & 1;
6016 break;
6017 case VDEV_PROP_CHECKSUM_N:
6018 if (nvpair_value_uint64(elem, &intval) != 0) {
6019 error = EINVAL;
6020 break;
6021 }
6022 vd->vdev_checksum_n = intval;
6023 break;
6024 case VDEV_PROP_CHECKSUM_T:
6025 if (nvpair_value_uint64(elem, &intval) != 0) {
6026 error = EINVAL;
6027 break;
6028 }
6029 vd->vdev_checksum_t = intval;
6030 break;
6031 case VDEV_PROP_IO_N:
6032 if (nvpair_value_uint64(elem, &intval) != 0) {
6033 error = EINVAL;
6034 break;
6035 }
6036 vd->vdev_io_n = intval;
6037 break;
6038 case VDEV_PROP_IO_T:
6039 if (nvpair_value_uint64(elem, &intval) != 0) {
6040 error = EINVAL;
6041 break;
6042 }
6043 vd->vdev_io_t = intval;
6044 break;
6045 case VDEV_PROP_SLOW_IO_N:
6046 if (nvpair_value_uint64(elem, &intval) != 0) {
6047 error = EINVAL;
6048 break;
6049 }
6050 vd->vdev_slow_io_n = intval;
6051 break;
6052 case VDEV_PROP_SLOW_IO_T:
6053 if (nvpair_value_uint64(elem, &intval) != 0) {
6054 error = EINVAL;
6055 break;
6056 }
6057 vd->vdev_slow_io_t = intval;
6058 break;
6059 default:
6060 /* Most processing is done in vdev_props_set_sync */
6061 break;
6062 }
6063 end:
6064 if (error != 0) {
6065 intval = error;
6066 vdev_prop_add_list(outnvl, propname, strval, intval, 0);
6067 return (error);
6068 }
6069 }
6070
6071 return (dsl_sync_task(spa->spa_name, NULL, vdev_props_set_sync,
6072 innvl, 6, ZFS_SPACE_CHECK_EXTRA_RESERVED));
6073 }
6074
6075 int
vdev_prop_get(vdev_t * vd,nvlist_t * innvl,nvlist_t * outnvl)6076 vdev_prop_get(vdev_t *vd, nvlist_t *innvl, nvlist_t *outnvl)
6077 {
6078 spa_t *spa = vd->vdev_spa;
6079 objset_t *mos = spa->spa_meta_objset;
6080 int err = 0;
6081 uint64_t objid;
6082 uint64_t vdev_guid;
6083 nvpair_t *elem = NULL;
6084 nvlist_t *nvprops = NULL;
6085 uint64_t intval = 0;
6086 char *strval = NULL;
6087 const char *propname = NULL;
6088 vdev_prop_t prop;
6089
6090 ASSERT(vd != NULL);
6091 ASSERT(mos != NULL);
6092
6093 if (nvlist_lookup_uint64(innvl, ZPOOL_VDEV_PROPS_GET_VDEV,
6094 &vdev_guid) != 0)
6095 return (SET_ERROR(EINVAL));
6096
6097 nvlist_lookup_nvlist(innvl, ZPOOL_VDEV_PROPS_GET_PROPS, &nvprops);
6098
6099 if (vd->vdev_root_zap != 0) {
6100 objid = vd->vdev_root_zap;
6101 } else if (vd->vdev_top_zap != 0) {
6102 objid = vd->vdev_top_zap;
6103 } else if (vd->vdev_leaf_zap != 0) {
6104 objid = vd->vdev_leaf_zap;
6105 } else {
6106 return (SET_ERROR(EINVAL));
6107 }
6108 ASSERT(objid != 0);
6109
6110 mutex_enter(&spa->spa_props_lock);
6111
6112 if (nvprops != NULL) {
6113 char namebuf[64] = { 0 };
6114
6115 while ((elem = nvlist_next_nvpair(nvprops, elem)) != NULL) {
6116 intval = 0;
6117 strval = NULL;
6118 propname = nvpair_name(elem);
6119 prop = vdev_name_to_prop(propname);
6120 zprop_source_t src = ZPROP_SRC_DEFAULT;
6121 uint64_t integer_size, num_integers;
6122
6123 switch (prop) {
6124 /* Special Read-only Properties */
6125 case VDEV_PROP_NAME:
6126 strval = vdev_name(vd, namebuf,
6127 sizeof (namebuf));
6128 if (strval == NULL)
6129 continue;
6130 vdev_prop_add_list(outnvl, propname, strval, 0,
6131 ZPROP_SRC_NONE);
6132 continue;
6133 case VDEV_PROP_CAPACITY:
6134 /* percent used */
6135 intval = (vd->vdev_stat.vs_dspace == 0) ? 0 :
6136 (vd->vdev_stat.vs_alloc * 100 /
6137 vd->vdev_stat.vs_dspace);
6138 vdev_prop_add_list(outnvl, propname, NULL,
6139 intval, ZPROP_SRC_NONE);
6140 continue;
6141 case VDEV_PROP_STATE:
6142 vdev_prop_add_list(outnvl, propname, NULL,
6143 vd->vdev_state, ZPROP_SRC_NONE);
6144 continue;
6145 case VDEV_PROP_GUID:
6146 vdev_prop_add_list(outnvl, propname, NULL,
6147 vd->vdev_guid, ZPROP_SRC_NONE);
6148 continue;
6149 case VDEV_PROP_ASIZE:
6150 vdev_prop_add_list(outnvl, propname, NULL,
6151 vd->vdev_asize, ZPROP_SRC_NONE);
6152 continue;
6153 case VDEV_PROP_PSIZE:
6154 vdev_prop_add_list(outnvl, propname, NULL,
6155 vd->vdev_psize, ZPROP_SRC_NONE);
6156 continue;
6157 case VDEV_PROP_ASHIFT:
6158 vdev_prop_add_list(outnvl, propname, NULL,
6159 vd->vdev_ashift, ZPROP_SRC_NONE);
6160 continue;
6161 case VDEV_PROP_SIZE:
6162 vdev_prop_add_list(outnvl, propname, NULL,
6163 vd->vdev_stat.vs_dspace, ZPROP_SRC_NONE);
6164 continue;
6165 case VDEV_PROP_FREE:
6166 vdev_prop_add_list(outnvl, propname, NULL,
6167 vd->vdev_stat.vs_dspace -
6168 vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
6169 continue;
6170 case VDEV_PROP_ALLOCATED:
6171 vdev_prop_add_list(outnvl, propname, NULL,
6172 vd->vdev_stat.vs_alloc, ZPROP_SRC_NONE);
6173 continue;
6174 case VDEV_PROP_EXPANDSZ:
6175 vdev_prop_add_list(outnvl, propname, NULL,
6176 vd->vdev_stat.vs_esize, ZPROP_SRC_NONE);
6177 continue;
6178 case VDEV_PROP_FRAGMENTATION:
6179 vdev_prop_add_list(outnvl, propname, NULL,
6180 vd->vdev_stat.vs_fragmentation,
6181 ZPROP_SRC_NONE);
6182 continue;
6183 case VDEV_PROP_PARITY:
6184 vdev_prop_add_list(outnvl, propname, NULL,
6185 vdev_get_nparity(vd), ZPROP_SRC_NONE);
6186 continue;
6187 case VDEV_PROP_PATH:
6188 if (vd->vdev_path == NULL)
6189 continue;
6190 vdev_prop_add_list(outnvl, propname,
6191 vd->vdev_path, 0, ZPROP_SRC_NONE);
6192 continue;
6193 case VDEV_PROP_DEVID:
6194 if (vd->vdev_devid == NULL)
6195 continue;
6196 vdev_prop_add_list(outnvl, propname,
6197 vd->vdev_devid, 0, ZPROP_SRC_NONE);
6198 continue;
6199 case VDEV_PROP_PHYS_PATH:
6200 if (vd->vdev_physpath == NULL)
6201 continue;
6202 vdev_prop_add_list(outnvl, propname,
6203 vd->vdev_physpath, 0, ZPROP_SRC_NONE);
6204 continue;
6205 case VDEV_PROP_ENC_PATH:
6206 if (vd->vdev_enc_sysfs_path == NULL)
6207 continue;
6208 vdev_prop_add_list(outnvl, propname,
6209 vd->vdev_enc_sysfs_path, 0, ZPROP_SRC_NONE);
6210 continue;
6211 case VDEV_PROP_FRU:
6212 if (vd->vdev_fru == NULL)
6213 continue;
6214 vdev_prop_add_list(outnvl, propname,
6215 vd->vdev_fru, 0, ZPROP_SRC_NONE);
6216 continue;
6217 case VDEV_PROP_PARENT:
6218 if (vd->vdev_parent != NULL) {
6219 strval = vdev_name(vd->vdev_parent,
6220 namebuf, sizeof (namebuf));
6221 vdev_prop_add_list(outnvl, propname,
6222 strval, 0, ZPROP_SRC_NONE);
6223 }
6224 continue;
6225 case VDEV_PROP_CHILDREN:
6226 if (vd->vdev_children > 0)
6227 strval = kmem_zalloc(ZAP_MAXVALUELEN,
6228 KM_SLEEP);
6229 for (uint64_t i = 0; i < vd->vdev_children;
6230 i++) {
6231 const char *vname;
6232
6233 vname = vdev_name(vd->vdev_child[i],
6234 namebuf, sizeof (namebuf));
6235 if (vname == NULL)
6236 vname = "(unknown)";
6237 if (strlen(strval) > 0)
6238 strlcat(strval, ",",
6239 ZAP_MAXVALUELEN);
6240 strlcat(strval, vname, ZAP_MAXVALUELEN);
6241 }
6242 if (strval != NULL) {
6243 vdev_prop_add_list(outnvl, propname,
6244 strval, 0, ZPROP_SRC_NONE);
6245 kmem_free(strval, ZAP_MAXVALUELEN);
6246 }
6247 continue;
6248 case VDEV_PROP_NUMCHILDREN:
6249 vdev_prop_add_list(outnvl, propname, NULL,
6250 vd->vdev_children, ZPROP_SRC_NONE);
6251 continue;
6252 case VDEV_PROP_READ_ERRORS:
6253 vdev_prop_add_list(outnvl, propname, NULL,
6254 vd->vdev_stat.vs_read_errors,
6255 ZPROP_SRC_NONE);
6256 continue;
6257 case VDEV_PROP_WRITE_ERRORS:
6258 vdev_prop_add_list(outnvl, propname, NULL,
6259 vd->vdev_stat.vs_write_errors,
6260 ZPROP_SRC_NONE);
6261 continue;
6262 case VDEV_PROP_CHECKSUM_ERRORS:
6263 vdev_prop_add_list(outnvl, propname, NULL,
6264 vd->vdev_stat.vs_checksum_errors,
6265 ZPROP_SRC_NONE);
6266 continue;
6267 case VDEV_PROP_INITIALIZE_ERRORS:
6268 vdev_prop_add_list(outnvl, propname, NULL,
6269 vd->vdev_stat.vs_initialize_errors,
6270 ZPROP_SRC_NONE);
6271 continue;
6272 case VDEV_PROP_TRIM_ERRORS:
6273 vdev_prop_add_list(outnvl, propname, NULL,
6274 vd->vdev_stat.vs_trim_errors,
6275 ZPROP_SRC_NONE);
6276 continue;
6277 case VDEV_PROP_SLOW_IOS:
6278 vdev_prop_add_list(outnvl, propname, NULL,
6279 vd->vdev_stat.vs_slow_ios,
6280 ZPROP_SRC_NONE);
6281 continue;
6282 case VDEV_PROP_OPS_NULL:
6283 vdev_prop_add_list(outnvl, propname, NULL,
6284 vd->vdev_stat.vs_ops[ZIO_TYPE_NULL],
6285 ZPROP_SRC_NONE);
6286 continue;
6287 case VDEV_PROP_OPS_READ:
6288 vdev_prop_add_list(outnvl, propname, NULL,
6289 vd->vdev_stat.vs_ops[ZIO_TYPE_READ],
6290 ZPROP_SRC_NONE);
6291 continue;
6292 case VDEV_PROP_OPS_WRITE:
6293 vdev_prop_add_list(outnvl, propname, NULL,
6294 vd->vdev_stat.vs_ops[ZIO_TYPE_WRITE],
6295 ZPROP_SRC_NONE);
6296 continue;
6297 case VDEV_PROP_OPS_FREE:
6298 vdev_prop_add_list(outnvl, propname, NULL,
6299 vd->vdev_stat.vs_ops[ZIO_TYPE_FREE],
6300 ZPROP_SRC_NONE);
6301 continue;
6302 case VDEV_PROP_OPS_CLAIM:
6303 vdev_prop_add_list(outnvl, propname, NULL,
6304 vd->vdev_stat.vs_ops[ZIO_TYPE_CLAIM],
6305 ZPROP_SRC_NONE);
6306 continue;
6307 case VDEV_PROP_OPS_TRIM:
6308 /*
6309 * TRIM ops and bytes are reported to user
6310 * space as ZIO_TYPE_FLUSH. This is done to
6311 * preserve the vdev_stat_t structure layout
6312 * for user space.
6313 */
6314 vdev_prop_add_list(outnvl, propname, NULL,
6315 vd->vdev_stat.vs_ops[ZIO_TYPE_FLUSH],
6316 ZPROP_SRC_NONE);
6317 continue;
6318 case VDEV_PROP_BYTES_NULL:
6319 vdev_prop_add_list(outnvl, propname, NULL,
6320 vd->vdev_stat.vs_bytes[ZIO_TYPE_NULL],
6321 ZPROP_SRC_NONE);
6322 continue;
6323 case VDEV_PROP_BYTES_READ:
6324 vdev_prop_add_list(outnvl, propname, NULL,
6325 vd->vdev_stat.vs_bytes[ZIO_TYPE_READ],
6326 ZPROP_SRC_NONE);
6327 continue;
6328 case VDEV_PROP_BYTES_WRITE:
6329 vdev_prop_add_list(outnvl, propname, NULL,
6330 vd->vdev_stat.vs_bytes[ZIO_TYPE_WRITE],
6331 ZPROP_SRC_NONE);
6332 continue;
6333 case VDEV_PROP_BYTES_FREE:
6334 vdev_prop_add_list(outnvl, propname, NULL,
6335 vd->vdev_stat.vs_bytes[ZIO_TYPE_FREE],
6336 ZPROP_SRC_NONE);
6337 continue;
6338 case VDEV_PROP_BYTES_CLAIM:
6339 vdev_prop_add_list(outnvl, propname, NULL,
6340 vd->vdev_stat.vs_bytes[ZIO_TYPE_CLAIM],
6341 ZPROP_SRC_NONE);
6342 continue;
6343 case VDEV_PROP_BYTES_TRIM:
6344 /*
6345 * TRIM ops and bytes are reported to user
6346 * space as ZIO_TYPE_FLUSH. This is done to
6347 * preserve the vdev_stat_t structure layout
6348 * for user space.
6349 */
6350 vdev_prop_add_list(outnvl, propname, NULL,
6351 vd->vdev_stat.vs_bytes[ZIO_TYPE_FLUSH],
6352 ZPROP_SRC_NONE);
6353 continue;
6354 case VDEV_PROP_REMOVING:
6355 vdev_prop_add_list(outnvl, propname, NULL,
6356 vd->vdev_removing, ZPROP_SRC_NONE);
6357 continue;
6358 case VDEV_PROP_RAIDZ_EXPANDING:
6359 /* Only expose this for raidz */
6360 if (vd->vdev_ops == &vdev_raidz_ops) {
6361 vdev_prop_add_list(outnvl, propname,
6362 NULL, vd->vdev_rz_expanding,
6363 ZPROP_SRC_NONE);
6364 }
6365 continue;
6366 case VDEV_PROP_TRIM_SUPPORT:
6367 /* only valid for leaf vdevs */
6368 if (vd->vdev_ops->vdev_op_leaf) {
6369 vdev_prop_add_list(outnvl, propname,
6370 NULL, vd->vdev_has_trim,
6371 ZPROP_SRC_NONE);
6372 }
6373 continue;
6374 /* Numeric Properites */
6375 case VDEV_PROP_ALLOCATING:
6376 /* Leaf vdevs cannot have this property */
6377 if (vd->vdev_mg == NULL &&
6378 vd->vdev_top != NULL) {
6379 src = ZPROP_SRC_NONE;
6380 intval = ZPROP_BOOLEAN_NA;
6381 } else {
6382 err = vdev_prop_get_int(vd, prop,
6383 &intval);
6384 if (err && err != ENOENT)
6385 break;
6386
6387 if (intval ==
6388 vdev_prop_default_numeric(prop))
6389 src = ZPROP_SRC_DEFAULT;
6390 else
6391 src = ZPROP_SRC_LOCAL;
6392 }
6393
6394 vdev_prop_add_list(outnvl, propname, NULL,
6395 intval, src);
6396 break;
6397 case VDEV_PROP_FAILFAST:
6398 src = ZPROP_SRC_LOCAL;
6399 strval = NULL;
6400
6401 err = zap_lookup(mos, objid, nvpair_name(elem),
6402 sizeof (uint64_t), 1, &intval);
6403 if (err == ENOENT) {
6404 intval = vdev_prop_default_numeric(
6405 prop);
6406 err = 0;
6407 } else if (err) {
6408 break;
6409 }
6410 if (intval == vdev_prop_default_numeric(prop))
6411 src = ZPROP_SRC_DEFAULT;
6412
6413 vdev_prop_add_list(outnvl, propname, strval,
6414 intval, src);
6415 break;
6416 case VDEV_PROP_CHECKSUM_N:
6417 case VDEV_PROP_CHECKSUM_T:
6418 case VDEV_PROP_IO_N:
6419 case VDEV_PROP_IO_T:
6420 case VDEV_PROP_SLOW_IO_N:
6421 case VDEV_PROP_SLOW_IO_T:
6422 err = vdev_prop_get_int(vd, prop, &intval);
6423 if (err && err != ENOENT)
6424 break;
6425
6426 if (intval == vdev_prop_default_numeric(prop))
6427 src = ZPROP_SRC_DEFAULT;
6428 else
6429 src = ZPROP_SRC_LOCAL;
6430
6431 vdev_prop_add_list(outnvl, propname, NULL,
6432 intval, src);
6433 break;
6434 /* Text Properties */
6435 case VDEV_PROP_COMMENT:
6436 /* Exists in the ZAP below */
6437 /* FALLTHRU */
6438 case VDEV_PROP_USERPROP:
6439 /* User Properites */
6440 src = ZPROP_SRC_LOCAL;
6441
6442 err = zap_length(mos, objid, nvpair_name(elem),
6443 &integer_size, &num_integers);
6444 if (err)
6445 break;
6446
6447 switch (integer_size) {
6448 case 8:
6449 /* User properties cannot be integers */
6450 err = EINVAL;
6451 break;
6452 case 1:
6453 /* string property */
6454 strval = kmem_alloc(num_integers,
6455 KM_SLEEP);
6456 err = zap_lookup(mos, objid,
6457 nvpair_name(elem), 1,
6458 num_integers, strval);
6459 if (err) {
6460 kmem_free(strval,
6461 num_integers);
6462 break;
6463 }
6464 vdev_prop_add_list(outnvl, propname,
6465 strval, 0, src);
6466 kmem_free(strval, num_integers);
6467 break;
6468 }
6469 break;
6470 default:
6471 err = ENOENT;
6472 break;
6473 }
6474 if (err)
6475 break;
6476 }
6477 } else {
6478 /*
6479 * Get all properties from the MOS vdev property object.
6480 */
6481 zap_cursor_t zc;
6482 zap_attribute_t *za = zap_attribute_alloc();
6483 for (zap_cursor_init(&zc, mos, objid);
6484 (err = zap_cursor_retrieve(&zc, za)) == 0;
6485 zap_cursor_advance(&zc)) {
6486 intval = 0;
6487 strval = NULL;
6488 zprop_source_t src = ZPROP_SRC_DEFAULT;
6489 propname = za->za_name;
6490
6491 switch (za->za_integer_length) {
6492 case 8:
6493 /* We do not allow integer user properties */
6494 /* This is likely an internal value */
6495 break;
6496 case 1:
6497 /* string property */
6498 strval = kmem_alloc(za->za_num_integers,
6499 KM_SLEEP);
6500 err = zap_lookup(mos, objid, za->za_name, 1,
6501 za->za_num_integers, strval);
6502 if (err) {
6503 kmem_free(strval, za->za_num_integers);
6504 break;
6505 }
6506 vdev_prop_add_list(outnvl, propname, strval, 0,
6507 src);
6508 kmem_free(strval, za->za_num_integers);
6509 break;
6510
6511 default:
6512 break;
6513 }
6514 }
6515 zap_cursor_fini(&zc);
6516 zap_attribute_free(za);
6517 }
6518
6519 mutex_exit(&spa->spa_props_lock);
6520 if (err && err != ENOENT) {
6521 return (err);
6522 }
6523
6524 return (0);
6525 }
6526
6527 EXPORT_SYMBOL(vdev_fault);
6528 EXPORT_SYMBOL(vdev_degrade);
6529 EXPORT_SYMBOL(vdev_online);
6530 EXPORT_SYMBOL(vdev_offline);
6531 EXPORT_SYMBOL(vdev_clear);
6532
6533 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_count, UINT, ZMOD_RW,
6534 "Target number of metaslabs per top-level vdev");
6535
6536 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, default_ms_shift, UINT, ZMOD_RW,
6537 "Default lower limit for metaslab size");
6538
6539 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, max_ms_shift, UINT, ZMOD_RW,
6540 "Default upper limit for metaslab size");
6541
6542 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, min_ms_count, UINT, ZMOD_RW,
6543 "Minimum number of metaslabs per top-level vdev");
6544
6545 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, ms_count_limit, UINT, ZMOD_RW,
6546 "Practical upper limit of total metaslabs per top-level vdev");
6547
6548 ZFS_MODULE_PARAM(zfs, zfs_, slow_io_events_per_second, UINT, ZMOD_RW,
6549 "Rate limit slow IO (delay) events to this many per second");
6550
6551 ZFS_MODULE_PARAM(zfs, zfs_, deadman_events_per_second, UINT, ZMOD_RW,
6552 "Rate limit hung IO (deadman) events to this many per second");
6553
6554 ZFS_MODULE_PARAM(zfs, zfs_, dio_write_verify_events_per_second, UINT, ZMOD_RW,
6555 "Rate Direct I/O write verify events to this many per second");
6556
6557 ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, direct_write_verify, UINT, ZMOD_RW,
6558 "Direct I/O writes will perform for checksum verification before "
6559 "commiting write");
6560
6561 ZFS_MODULE_PARAM(zfs, zfs_, checksum_events_per_second, UINT, ZMOD_RW,
6562 "Rate limit checksum events to this many checksum errors per second "
6563 "(do not set below ZED threshold).");
6564
6565 ZFS_MODULE_PARAM(zfs, zfs_, scan_ignore_errors, INT, ZMOD_RW,
6566 "Ignore errors during resilver/scrub");
6567
6568 ZFS_MODULE_PARAM(zfs_vdev, vdev_, validate_skip, INT, ZMOD_RW,
6569 "Bypass vdev_validate()");
6570
6571 ZFS_MODULE_PARAM(zfs, zfs_, nocacheflush, INT, ZMOD_RW,
6572 "Disable cache flushes");
6573
6574 ZFS_MODULE_PARAM(zfs, zfs_, embedded_slog_min_ms, UINT, ZMOD_RW,
6575 "Minimum number of metaslabs required to dedicate one for log blocks");
6576
6577 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, min_auto_ashift,
6578 param_set_min_auto_ashift, param_get_uint, ZMOD_RW,
6579 "Minimum ashift used when creating new top-level vdevs");
6580
6581 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, max_auto_ashift,
6582 param_set_max_auto_ashift, param_get_uint, ZMOD_RW,
6583 "Maximum ashift used when optimizing for logical -> physical sector "
6584 "size on new top-level vdevs");
6585
6586 ZFS_MODULE_PARAM_CALL(zfs_vdev, zfs_vdev_, raidz_impl,
6587 param_set_raidz_impl, param_get_raidz_impl, ZMOD_RW,
6588 "RAIDZ implementation");
6589