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