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