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