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