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