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