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