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