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