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