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