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