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