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