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