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