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