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