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