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