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