]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/vdev.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26
27 #pragma ident   "%Z%%M% %I%     %E% SMI"
28
29 #include <sys/zfs_context.h>
30 #include <sys/fm/fs/zfs.h>
31 #include <sys/spa.h>
32 #include <sys/spa_impl.h>
33 #include <sys/dmu.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/uberblock_impl.h>
37 #include <sys/metaslab.h>
38 #include <sys/metaslab_impl.h>
39 #include <sys/space_map.h>
40 #include <sys/zio.h>
41 #include <sys/zap.h>
42 #include <sys/fs/zfs.h>
43
44 SYSCTL_DECL(_vfs_zfs);
45 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
46
47 /*
48  * Virtual device management.
49  */
50
51 static vdev_ops_t *vdev_ops_table[] = {
52         &vdev_root_ops,
53         &vdev_raidz_ops,
54         &vdev_mirror_ops,
55         &vdev_replacing_ops,
56         &vdev_spare_ops,
57 #ifdef _KERNEL
58         &vdev_geom_ops,
59 #else
60         &vdev_disk_ops,
61         &vdev_file_ops,
62 #endif
63         &vdev_missing_ops,
64         NULL
65 };
66
67 /* maximum scrub/resilver I/O queue */
68 int zfs_scrub_limit = 70;
69
70 /*
71  * Given a vdev type, return the appropriate ops vector.
72  */
73 static vdev_ops_t *
74 vdev_getops(const char *type)
75 {
76         vdev_ops_t *ops, **opspp;
77
78         for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
79                 if (strcmp(ops->vdev_op_type, type) == 0)
80                         break;
81
82         return (ops);
83 }
84
85 /*
86  * Default asize function: return the MAX of psize with the asize of
87  * all children.  This is what's used by anything other than RAID-Z.
88  */
89 uint64_t
90 vdev_default_asize(vdev_t *vd, uint64_t psize)
91 {
92         uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
93         uint64_t csize;
94         uint64_t c;
95
96         for (c = 0; c < vd->vdev_children; c++) {
97                 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
98                 asize = MAX(asize, csize);
99         }
100
101         return (asize);
102 }
103
104 /*
105  * Get the replaceable or attachable device size.
106  * If the parent is a mirror or raidz, the replaceable size is the minimum
107  * psize of all its children. For the rest, just return our own psize.
108  *
109  * e.g.
110  *                      psize   rsize
111  * root                 -       -
112  *      mirror/raidz    -       -
113  *          disk1       20g     20g
114  *          disk2       40g     20g
115  *      disk3           80g     80g
116  */
117 uint64_t
118 vdev_get_rsize(vdev_t *vd)
119 {
120         vdev_t *pvd, *cvd;
121         uint64_t c, rsize;
122
123         pvd = vd->vdev_parent;
124
125         /*
126          * If our parent is NULL or the root, just return our own psize.
127          */
128         if (pvd == NULL || pvd->vdev_parent == NULL)
129                 return (vd->vdev_psize);
130
131         rsize = 0;
132
133         for (c = 0; c < pvd->vdev_children; c++) {
134                 cvd = pvd->vdev_child[c];
135                 rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
136         }
137
138         return (rsize);
139 }
140
141 vdev_t *
142 vdev_lookup_top(spa_t *spa, uint64_t vdev)
143 {
144         vdev_t *rvd = spa->spa_root_vdev;
145
146         if (vdev < rvd->vdev_children)
147                 return (rvd->vdev_child[vdev]);
148
149         return (NULL);
150 }
151
152 vdev_t *
153 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
154 {
155         int c;
156         vdev_t *mvd;
157
158         if (vd->vdev_guid == guid)
159                 return (vd);
160
161         for (c = 0; c < vd->vdev_children; c++)
162                 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
163                     NULL)
164                         return (mvd);
165
166         return (NULL);
167 }
168
169 void
170 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
171 {
172         size_t oldsize, newsize;
173         uint64_t id = cvd->vdev_id;
174         vdev_t **newchild;
175
176         ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
177         ASSERT(cvd->vdev_parent == NULL);
178
179         cvd->vdev_parent = pvd;
180
181         if (pvd == NULL)
182                 return;
183
184         ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
185
186         oldsize = pvd->vdev_children * sizeof (vdev_t *);
187         pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
188         newsize = pvd->vdev_children * sizeof (vdev_t *);
189
190         newchild = kmem_zalloc(newsize, KM_SLEEP);
191         if (pvd->vdev_child != NULL) {
192                 bcopy(pvd->vdev_child, newchild, oldsize);
193                 kmem_free(pvd->vdev_child, oldsize);
194         }
195
196         pvd->vdev_child = newchild;
197         pvd->vdev_child[id] = cvd;
198
199         cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
200         ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
201
202         /*
203          * Walk up all ancestors to update guid sum.
204          */
205         for (; pvd != NULL; pvd = pvd->vdev_parent)
206                 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
207
208         if (cvd->vdev_ops->vdev_op_leaf)
209                 cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
210 }
211
212 void
213 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
214 {
215         int c;
216         uint_t id = cvd->vdev_id;
217
218         ASSERT(cvd->vdev_parent == pvd);
219
220         if (pvd == NULL)
221                 return;
222
223         ASSERT(id < pvd->vdev_children);
224         ASSERT(pvd->vdev_child[id] == cvd);
225
226         pvd->vdev_child[id] = NULL;
227         cvd->vdev_parent = NULL;
228
229         for (c = 0; c < pvd->vdev_children; c++)
230                 if (pvd->vdev_child[c])
231                         break;
232
233         if (c == pvd->vdev_children) {
234                 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
235                 pvd->vdev_child = NULL;
236                 pvd->vdev_children = 0;
237         }
238
239         /*
240          * Walk up all ancestors to update guid sum.
241          */
242         for (; pvd != NULL; pvd = pvd->vdev_parent)
243                 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
244
245         if (cvd->vdev_ops->vdev_op_leaf)
246                 cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
247 }
248
249 /*
250  * Remove any holes in the child array.
251  */
252 void
253 vdev_compact_children(vdev_t *pvd)
254 {
255         vdev_t **newchild, *cvd;
256         int oldc = pvd->vdev_children;
257         int newc, c;
258
259         ASSERT(spa_config_held(pvd->vdev_spa, RW_WRITER));
260
261         for (c = newc = 0; c < oldc; c++)
262                 if (pvd->vdev_child[c])
263                         newc++;
264
265         newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
266
267         for (c = newc = 0; c < oldc; c++) {
268                 if ((cvd = pvd->vdev_child[c]) != NULL) {
269                         newchild[newc] = cvd;
270                         cvd->vdev_id = newc++;
271                 }
272         }
273
274         kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
275         pvd->vdev_child = newchild;
276         pvd->vdev_children = newc;
277 }
278
279 /*
280  * Allocate and minimally initialize a vdev_t.
281  */
282 static vdev_t *
283 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
284 {
285         vdev_t *vd;
286
287         vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
288
289         if (spa->spa_root_vdev == NULL) {
290                 ASSERT(ops == &vdev_root_ops);
291                 spa->spa_root_vdev = vd;
292         }
293
294         if (guid == 0) {
295                 if (spa->spa_root_vdev == vd) {
296                         /*
297                          * The root vdev's guid will also be the pool guid,
298                          * which must be unique among all pools.
299                          */
300                         while (guid == 0 || spa_guid_exists(guid, 0))
301                                 guid = spa_get_random(-1ULL);
302                 } else {
303                         /*
304                          * Any other vdev's guid must be unique within the pool.
305                          */
306                         while (guid == 0 ||
307                             spa_guid_exists(spa_guid(spa), guid))
308                                 guid = spa_get_random(-1ULL);
309                 }
310                 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
311         }
312
313         vd->vdev_spa = spa;
314         vd->vdev_id = id;
315         vd->vdev_guid = guid;
316         vd->vdev_guid_sum = guid;
317         vd->vdev_ops = ops;
318         vd->vdev_state = VDEV_STATE_CLOSED;
319
320         mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
321         mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
322         space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
323         space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
324         txg_list_create(&vd->vdev_ms_list,
325             offsetof(struct metaslab, ms_txg_node));
326         txg_list_create(&vd->vdev_dtl_list,
327             offsetof(struct vdev, vdev_dtl_node));
328         vd->vdev_stat.vs_timestamp = gethrtime();
329
330         return (vd);
331 }
332
333 /*
334  * Free a vdev_t that has been removed from service.
335  */
336 static void
337 vdev_free_common(vdev_t *vd)
338 {
339         spa_t *spa = vd->vdev_spa;
340
341         if (vd->vdev_path)
342                 spa_strfree(vd->vdev_path);
343         if (vd->vdev_devid)
344                 spa_strfree(vd->vdev_devid);
345
346         if (vd->vdev_isspare)
347                 spa_spare_remove(vd);
348
349         txg_list_destroy(&vd->vdev_ms_list);
350         txg_list_destroy(&vd->vdev_dtl_list);
351         mutex_enter(&vd->vdev_dtl_lock);
352         space_map_unload(&vd->vdev_dtl_map);
353         space_map_destroy(&vd->vdev_dtl_map);
354         space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
355         space_map_destroy(&vd->vdev_dtl_scrub);
356         mutex_exit(&vd->vdev_dtl_lock);
357         mutex_destroy(&vd->vdev_dtl_lock);
358         mutex_destroy(&vd->vdev_stat_lock);
359
360         if (vd == spa->spa_root_vdev)
361                 spa->spa_root_vdev = NULL;
362
363         kmem_free(vd, sizeof (vdev_t));
364 }
365
366 /*
367  * Allocate a new vdev.  The 'alloctype' is used to control whether we are
368  * creating a new vdev or loading an existing one - the behavior is slightly
369  * different for each case.
370  */
371 int
372 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
373     int alloctype)
374 {
375         vdev_ops_t *ops;
376         char *type;
377         uint64_t guid = 0;
378         vdev_t *vd;
379
380         ASSERT(spa_config_held(spa, RW_WRITER));
381
382         if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
383                 return (EINVAL);
384
385         if ((ops = vdev_getops(type)) == NULL)
386                 return (EINVAL);
387
388         /*
389          * If this is a load, get the vdev guid from the nvlist.
390          * Otherwise, vdev_alloc_common() will generate one for us.
391          */
392         if (alloctype == VDEV_ALLOC_LOAD) {
393                 uint64_t label_id;
394
395                 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
396                     label_id != id)
397                         return (EINVAL);
398
399                 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
400                         return (EINVAL);
401         } else if (alloctype == VDEV_ALLOC_SPARE) {
402                 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
403                         return (EINVAL);
404         }
405
406         /*
407          * The first allocated vdev must be of type 'root'.
408          */
409         if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
410                 return (EINVAL);
411
412         vd = vdev_alloc_common(spa, id, guid, ops);
413
414         if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
415                 vd->vdev_path = spa_strdup(vd->vdev_path);
416         if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
417                 vd->vdev_devid = spa_strdup(vd->vdev_devid);
418
419         /*
420          * Set the nparity propery for RAID-Z vdevs.
421          */
422         if (ops == &vdev_raidz_ops) {
423                 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
424                     &vd->vdev_nparity) == 0) {
425                         /*
426                          * Currently, we can only support 2 parity devices.
427                          */
428                         if (vd->vdev_nparity > 2)
429                                 return (EINVAL);
430                         /*
431                          * Older versions can only support 1 parity device.
432                          */
433                         if (vd->vdev_nparity == 2 &&
434                             spa_version(spa) < ZFS_VERSION_RAID6)
435                                 return (ENOTSUP);
436
437                 } else {
438                         /*
439                          * We require the parity to be specified for SPAs that
440                          * support multiple parity levels.
441                          */
442                         if (spa_version(spa) >= ZFS_VERSION_RAID6)
443                                 return (EINVAL);
444
445                         /*
446                          * Otherwise, we default to 1 parity device for RAID-Z.
447                          */
448                         vd->vdev_nparity = 1;
449                 }
450         } else {
451                 vd->vdev_nparity = 0;
452         }
453
454         /*
455          * Set the whole_disk property.  If it's not specified, leave the value
456          * as -1.
457          */
458         if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
459             &vd->vdev_wholedisk) != 0)
460                 vd->vdev_wholedisk = -1ULL;
461
462         /*
463          * Look for the 'not present' flag.  This will only be set if the device
464          * was not present at the time of import.
465          */
466         (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
467             &vd->vdev_not_present);
468
469         /*
470          * Get the alignment requirement.
471          */
472         (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
473
474         /*
475          * If we're a top-level vdev, try to load the allocation parameters.
476          */
477         if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
478                 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
479                     &vd->vdev_ms_array);
480                 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
481                     &vd->vdev_ms_shift);
482                 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
483                     &vd->vdev_asize);
484         }
485
486         /*
487          * If we're a leaf vdev, try to load the DTL object and offline state.
488          */
489         if (vd->vdev_ops->vdev_op_leaf && alloctype == VDEV_ALLOC_LOAD) {
490                 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
491                     &vd->vdev_dtl.smo_object);
492                 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
493                     &vd->vdev_offline);
494         }
495
496         /*
497          * Add ourselves to the parent's list of children.
498          */
499         vdev_add_child(parent, vd);
500
501         *vdp = vd;
502
503         return (0);
504 }
505
506 void
507 vdev_free(vdev_t *vd)
508 {
509         int c;
510
511         /*
512          * vdev_free() implies closing the vdev first.  This is simpler than
513          * trying to ensure complicated semantics for all callers.
514          */
515         vdev_close(vd);
516
517         ASSERT(!list_link_active(&vd->vdev_dirty_node));
518
519         /*
520          * Free all children.
521          */
522         for (c = 0; c < vd->vdev_children; c++)
523                 vdev_free(vd->vdev_child[c]);
524
525         ASSERT(vd->vdev_child == NULL);
526         ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
527
528         /*
529          * Discard allocation state.
530          */
531         if (vd == vd->vdev_top)
532                 vdev_metaslab_fini(vd);
533
534         ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
535         ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
536         ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
537
538         /*
539          * Remove this vdev from its parent's child list.
540          */
541         vdev_remove_child(vd->vdev_parent, vd);
542
543         ASSERT(vd->vdev_parent == NULL);
544
545         vdev_free_common(vd);
546 }
547
548 /*
549  * Transfer top-level vdev state from svd to tvd.
550  */
551 static void
552 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
553 {
554         spa_t *spa = svd->vdev_spa;
555         metaslab_t *msp;
556         vdev_t *vd;
557         int t;
558
559         ASSERT(tvd == tvd->vdev_top);
560
561         tvd->vdev_ms_array = svd->vdev_ms_array;
562         tvd->vdev_ms_shift = svd->vdev_ms_shift;
563         tvd->vdev_ms_count = svd->vdev_ms_count;
564
565         svd->vdev_ms_array = 0;
566         svd->vdev_ms_shift = 0;
567         svd->vdev_ms_count = 0;
568
569         tvd->vdev_mg = svd->vdev_mg;
570         tvd->vdev_ms = svd->vdev_ms;
571
572         svd->vdev_mg = NULL;
573         svd->vdev_ms = NULL;
574
575         if (tvd->vdev_mg != NULL)
576                 tvd->vdev_mg->mg_vd = tvd;
577
578         tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
579         tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
580         tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
581
582         svd->vdev_stat.vs_alloc = 0;
583         svd->vdev_stat.vs_space = 0;
584         svd->vdev_stat.vs_dspace = 0;
585
586         for (t = 0; t < TXG_SIZE; t++) {
587                 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
588                         (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
589                 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
590                         (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
591                 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
592                         (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
593         }
594
595         if (list_link_active(&svd->vdev_dirty_node)) {
596                 vdev_config_clean(svd);
597                 vdev_config_dirty(tvd);
598         }
599
600         tvd->vdev_reopen_wanted = svd->vdev_reopen_wanted;
601         svd->vdev_reopen_wanted = 0;
602
603         tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
604         svd->vdev_deflate_ratio = 0;
605 }
606
607 static void
608 vdev_top_update(vdev_t *tvd, vdev_t *vd)
609 {
610         int c;
611
612         if (vd == NULL)
613                 return;
614
615         vd->vdev_top = tvd;
616
617         for (c = 0; c < vd->vdev_children; c++)
618                 vdev_top_update(tvd, vd->vdev_child[c]);
619 }
620
621 /*
622  * Add a mirror/replacing vdev above an existing vdev.
623  */
624 vdev_t *
625 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
626 {
627         spa_t *spa = cvd->vdev_spa;
628         vdev_t *pvd = cvd->vdev_parent;
629         vdev_t *mvd;
630
631         ASSERT(spa_config_held(spa, RW_WRITER));
632
633         mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
634
635         mvd->vdev_asize = cvd->vdev_asize;
636         mvd->vdev_ashift = cvd->vdev_ashift;
637         mvd->vdev_state = cvd->vdev_state;
638
639         vdev_remove_child(pvd, cvd);
640         vdev_add_child(pvd, mvd);
641         cvd->vdev_id = mvd->vdev_children;
642         vdev_add_child(mvd, cvd);
643         vdev_top_update(cvd->vdev_top, cvd->vdev_top);
644
645         if (mvd == mvd->vdev_top)
646                 vdev_top_transfer(cvd, mvd);
647
648         return (mvd);
649 }
650
651 /*
652  * Remove a 1-way mirror/replacing vdev from the tree.
653  */
654 void
655 vdev_remove_parent(vdev_t *cvd)
656 {
657         vdev_t *mvd = cvd->vdev_parent;
658         vdev_t *pvd = mvd->vdev_parent;
659
660         ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
661
662         ASSERT(mvd->vdev_children == 1);
663         ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
664             mvd->vdev_ops == &vdev_replacing_ops ||
665             mvd->vdev_ops == &vdev_spare_ops);
666         cvd->vdev_ashift = mvd->vdev_ashift;
667
668         vdev_remove_child(mvd, cvd);
669         vdev_remove_child(pvd, mvd);
670         cvd->vdev_id = mvd->vdev_id;
671         vdev_add_child(pvd, cvd);
672         /*
673          * If we created a new toplevel vdev, then we need to change the child's
674          * vdev GUID to match the old toplevel vdev.  Otherwise, we could have
675          * detached an offline device, and when we go to import the pool we'll
676          * think we have two toplevel vdevs, instead of a different version of
677          * the same toplevel vdev.
678          */
679         if (cvd->vdev_top == cvd) {
680                 pvd->vdev_guid_sum -= cvd->vdev_guid;
681                 cvd->vdev_guid_sum -= cvd->vdev_guid;
682                 cvd->vdev_guid = mvd->vdev_guid;
683                 cvd->vdev_guid_sum += mvd->vdev_guid;
684                 pvd->vdev_guid_sum += cvd->vdev_guid;
685         }
686         vdev_top_update(cvd->vdev_top, cvd->vdev_top);
687
688         if (cvd == cvd->vdev_top)
689                 vdev_top_transfer(mvd, cvd);
690
691         ASSERT(mvd->vdev_children == 0);
692         vdev_free(mvd);
693 }
694
695 int
696 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
697 {
698         spa_t *spa = vd->vdev_spa;
699         objset_t *mos = spa->spa_meta_objset;
700         metaslab_class_t *mc = spa_metaslab_class_select(spa);
701         uint64_t m;
702         uint64_t oldc = vd->vdev_ms_count;
703         uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
704         metaslab_t **mspp;
705         int error;
706
707         if (vd->vdev_ms_shift == 0)     /* not being allocated from yet */
708                 return (0);
709
710         dprintf("%s oldc %llu newc %llu\n", vdev_description(vd), oldc, newc);
711
712         ASSERT(oldc <= newc);
713
714         if (vd->vdev_mg == NULL)
715                 vd->vdev_mg = metaslab_group_create(mc, vd);
716
717         mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
718
719         if (oldc != 0) {
720                 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
721                 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
722         }
723
724         vd->vdev_ms = mspp;
725         vd->vdev_ms_count = newc;
726
727         for (m = oldc; m < newc; m++) {
728                 space_map_obj_t smo = { 0, 0, 0 };
729                 if (txg == 0) {
730                         uint64_t object = 0;
731                         error = dmu_read(mos, vd->vdev_ms_array,
732                             m * sizeof (uint64_t), sizeof (uint64_t), &object);
733                         if (error)
734                                 return (error);
735                         if (object != 0) {
736                                 dmu_buf_t *db;
737                                 error = dmu_bonus_hold(mos, object, FTAG, &db);
738                                 if (error)
739                                         return (error);
740                                 ASSERT3U(db->db_size, ==, sizeof (smo));
741                                 bcopy(db->db_data, &smo, db->db_size);
742                                 ASSERT3U(smo.smo_object, ==, object);
743                                 dmu_buf_rele(db, FTAG);
744                         }
745                 }
746                 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
747                     m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
748         }
749
750         return (0);
751 }
752
753 void
754 vdev_metaslab_fini(vdev_t *vd)
755 {
756         uint64_t m;
757         uint64_t count = vd->vdev_ms_count;
758
759         if (vd->vdev_ms != NULL) {
760                 for (m = 0; m < count; m++)
761                         if (vd->vdev_ms[m] != NULL)
762                                 metaslab_fini(vd->vdev_ms[m]);
763                 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
764                 vd->vdev_ms = NULL;
765         }
766 }
767
768 /*
769  * Prepare a virtual device for access.
770  */
771 int
772 vdev_open(vdev_t *vd)
773 {
774         int error;
775         int c;
776         uint64_t osize = 0;
777         uint64_t asize, psize;
778         uint64_t ashift = 0;
779
780         ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
781             vd->vdev_state == VDEV_STATE_CANT_OPEN ||
782             vd->vdev_state == VDEV_STATE_OFFLINE);
783
784         if (vd->vdev_fault_mode == VDEV_FAULT_COUNT)
785                 vd->vdev_fault_arg >>= 1;
786         else
787                 vd->vdev_fault_mode = VDEV_FAULT_NONE;
788
789         vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
790
791         if (vd->vdev_ops->vdev_op_leaf) {
792                 vdev_cache_init(vd);
793                 vdev_queue_init(vd);
794                 vd->vdev_cache_active = B_TRUE;
795         }
796
797         if (vd->vdev_offline) {
798                 ASSERT(vd->vdev_children == 0);
799                 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
800                 return (ENXIO);
801         }
802
803         error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
804
805         if (zio_injection_enabled && error == 0)
806                 error = zio_handle_device_injection(vd, ENXIO);
807
808         dprintf("%s = %d, osize %llu, state = %d\n",
809             vdev_description(vd), error, osize, vd->vdev_state);
810
811         if (error) {
812                 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
813                     vd->vdev_stat.vs_aux);
814                 return (error);
815         }
816
817         vd->vdev_state = VDEV_STATE_HEALTHY;
818
819         for (c = 0; c < vd->vdev_children; c++)
820                 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
821                         vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
822                             VDEV_AUX_NONE);
823                         break;
824                 }
825
826         osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
827
828         if (vd->vdev_children == 0) {
829                 if (osize < SPA_MINDEVSIZE) {
830                         vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
831                             VDEV_AUX_TOO_SMALL);
832                         return (EOVERFLOW);
833                 }
834                 psize = osize;
835                 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
836         } else {
837                 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
838                     (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
839                         vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
840                             VDEV_AUX_TOO_SMALL);
841                         return (EOVERFLOW);
842                 }
843                 psize = 0;
844                 asize = osize;
845         }
846
847         vd->vdev_psize = psize;
848
849         if (vd->vdev_asize == 0) {
850                 /*
851                  * This is the first-ever open, so use the computed values.
852                  * For testing purposes, a higher ashift can be requested.
853                  */
854                 vd->vdev_asize = asize;
855                 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
856         } else {
857                 /*
858                  * Make sure the alignment requirement hasn't increased.
859                  */
860                 if (ashift > vd->vdev_top->vdev_ashift) {
861                         vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
862                             VDEV_AUX_BAD_LABEL);
863                         return (EINVAL);
864                 }
865
866                 /*
867                  * Make sure the device hasn't shrunk.
868                  */
869                 if (asize < vd->vdev_asize) {
870                         vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
871                             VDEV_AUX_BAD_LABEL);
872                         return (EINVAL);
873                 }
874
875                 /*
876                  * If all children are healthy and the asize has increased,
877                  * then we've experienced dynamic LUN growth.
878                  */
879                 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
880                     asize > vd->vdev_asize) {
881                         vd->vdev_asize = asize;
882                 }
883         }
884
885         /*
886          * If this is a top-level vdev, compute the raidz-deflation
887          * ratio.  Note, we hard-code in 128k (1<<17) because it is the
888          * current "typical" blocksize.  Even if SPA_MAXBLOCKSIZE
889          * changes, this algorithm must never change, or we will
890          * inconsistently account for existing bp's.
891          */
892         if (vd->vdev_top == vd) {
893                 vd->vdev_deflate_ratio = (1<<17) /
894                     (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
895         }
896
897         /*
898          * This allows the ZFS DE to close cases appropriately.  If a device
899          * goes away and later returns, we want to close the associated case.
900          * But it's not enough to simply post this only when a device goes from
901          * CANT_OPEN -> HEALTHY.  If we reboot the system and the device is
902          * back, we also need to close the case (otherwise we will try to replay
903          * it).  So we have to post this notifier every time.  Since this only
904          * occurs during pool open or error recovery, this should not be an
905          * issue.
906          */
907         zfs_post_ok(vd->vdev_spa, vd);
908
909         return (0);
910 }
911
912 /*
913  * Called once the vdevs are all opened, this routine validates the label
914  * contents.  This needs to be done before vdev_load() so that we don't
915  * inadvertently do repair I/Os to the wrong device, and so that vdev_reopen()
916  * won't succeed if the device has been changed underneath.
917  *
918  * This function will only return failure if one of the vdevs indicates that it
919  * has since been destroyed or exported.  This is only possible if
920  * /etc/zfs/zpool.cache was readonly at the time.  Otherwise, the vdev state
921  * will be updated but the function will return 0.
922  */
923 int
924 vdev_validate(vdev_t *vd)
925 {
926         spa_t *spa = vd->vdev_spa;
927         int c;
928         nvlist_t *label;
929         uint64_t guid;
930         uint64_t state;
931
932         for (c = 0; c < vd->vdev_children; c++)
933                 if (vdev_validate(vd->vdev_child[c]) != 0)
934                         return (EBADF);
935
936         /*
937          * If the device has already failed, or was marked offline, don't do
938          * any further validation.  Otherwise, label I/O will fail and we will
939          * overwrite the previous state.
940          */
941         if (vd->vdev_ops->vdev_op_leaf && !vdev_is_dead(vd)) {
942
943                 if ((label = vdev_label_read_config(vd)) == NULL) {
944                         vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
945                             VDEV_AUX_BAD_LABEL);
946                         return (0);
947                 }
948
949                 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
950                     &guid) != 0 || guid != spa_guid(spa)) {
951                         vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
952                             VDEV_AUX_CORRUPT_DATA);
953                         nvlist_free(label);
954                         return (0);
955                 }
956
957                 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
958                     &guid) != 0 || guid != vd->vdev_guid) {
959                         vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
960                             VDEV_AUX_CORRUPT_DATA);
961                         nvlist_free(label);
962                         return (0);
963                 }
964
965                 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
966                     &state) != 0) {
967                         vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
968                             VDEV_AUX_CORRUPT_DATA);
969                         nvlist_free(label);
970                         return (0);
971                 }
972
973                 nvlist_free(label);
974
975                 if (spa->spa_load_state == SPA_LOAD_OPEN &&
976                     state != POOL_STATE_ACTIVE)
977                         return (EBADF);
978         }
979
980         /*
981          * If we were able to open and validate a vdev that was previously
982          * marked permanently unavailable, clear that state now.
983          */
984         if (vd->vdev_not_present)
985                 vd->vdev_not_present = 0;
986
987         return (0);
988 }
989
990 /*
991  * Close a virtual device.
992  */
993 void
994 vdev_close(vdev_t *vd)
995 {
996         vd->vdev_ops->vdev_op_close(vd);
997
998         if (vd->vdev_cache_active) {
999                 vdev_cache_fini(vd);
1000                 vdev_queue_fini(vd);
1001                 vd->vdev_cache_active = B_FALSE;
1002         }
1003
1004         /*
1005          * We record the previous state before we close it, so  that if we are
1006          * doing a reopen(), we don't generate FMA ereports if we notice that
1007          * it's still faulted.
1008          */
1009         vd->vdev_prevstate = vd->vdev_state;
1010
1011         if (vd->vdev_offline)
1012                 vd->vdev_state = VDEV_STATE_OFFLINE;
1013         else
1014                 vd->vdev_state = VDEV_STATE_CLOSED;
1015         vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1016 }
1017
1018 void
1019 vdev_reopen(vdev_t *vd)
1020 {
1021         spa_t *spa = vd->vdev_spa;
1022
1023         ASSERT(spa_config_held(spa, RW_WRITER));
1024
1025         vdev_close(vd);
1026         (void) vdev_open(vd);
1027
1028         /*
1029          * Call vdev_validate() here to make sure we have the same device.
1030          * Otherwise, a device with an invalid label could be successfully
1031          * opened in response to vdev_reopen().
1032          *
1033          * The downside to this is that if the user is simply experimenting by
1034          * overwriting an entire disk, we'll fault the device rather than
1035          * demonstrate self-healing capabilities.  On the other hand, with
1036          * proper FMA integration, the series of errors we'd see from the device
1037          * would result in a faulted device anyway.  Given that this doesn't
1038          * model any real-world corruption, it's better to catch this here and
1039          * correctly identify that the device has either changed beneath us, or
1040          * is corrupted beyond recognition.
1041          */
1042         (void) vdev_validate(vd);
1043
1044         /*
1045          * Reassess root vdev's health.
1046          */
1047         vdev_propagate_state(spa->spa_root_vdev);
1048 }
1049
1050 int
1051 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1052 {
1053         int error;
1054
1055         /*
1056          * Normally, partial opens (e.g. of a mirror) are allowed.
1057          * For a create, however, we want to fail the request if
1058          * there are any components we can't open.
1059          */
1060         error = vdev_open(vd);
1061
1062         if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1063                 vdev_close(vd);
1064                 return (error ? error : ENXIO);
1065         }
1066
1067         /*
1068          * Recursively initialize all labels.
1069          */
1070         if ((error = vdev_label_init(vd, txg, isreplacing ?
1071             VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1072                 vdev_close(vd);
1073                 return (error);
1074         }
1075
1076         return (0);
1077 }
1078
1079 /*
1080  * The is the latter half of vdev_create().  It is distinct because it
1081  * involves initiating transactions in order to do metaslab creation.
1082  * For creation, we want to try to create all vdevs at once and then undo it
1083  * if anything fails; this is much harder if we have pending transactions.
1084  */
1085 void
1086 vdev_init(vdev_t *vd, uint64_t txg)
1087 {
1088         /*
1089          * Aim for roughly 200 metaslabs per vdev.
1090          */
1091         vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1092         vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1093
1094         /*
1095          * Initialize the vdev's metaslabs.  This can't fail because
1096          * there's nothing to read when creating all new metaslabs.
1097          */
1098         VERIFY(vdev_metaslab_init(vd, txg) == 0);
1099 }
1100
1101 void
1102 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1103 {
1104         ASSERT(vd == vd->vdev_top);
1105         ASSERT(ISP2(flags));
1106
1107         if (flags & VDD_METASLAB)
1108                 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1109
1110         if (flags & VDD_DTL)
1111                 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1112
1113         (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1114 }
1115
1116 void
1117 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1118 {
1119         mutex_enter(sm->sm_lock);
1120         if (!space_map_contains(sm, txg, size))
1121                 space_map_add(sm, txg, size);
1122         mutex_exit(sm->sm_lock);
1123 }
1124
1125 int
1126 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1127 {
1128         int dirty;
1129
1130         /*
1131          * Quick test without the lock -- covers the common case that
1132          * there are no dirty time segments.
1133          */
1134         if (sm->sm_space == 0)
1135                 return (0);
1136
1137         mutex_enter(sm->sm_lock);
1138         dirty = space_map_contains(sm, txg, size);
1139         mutex_exit(sm->sm_lock);
1140
1141         return (dirty);
1142 }
1143
1144 /*
1145  * Reassess DTLs after a config change or scrub completion.
1146  */
1147 void
1148 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1149 {
1150         spa_t *spa = vd->vdev_spa;
1151         int c;
1152
1153         ASSERT(spa_config_held(spa, RW_WRITER));
1154
1155         if (vd->vdev_children == 0) {
1156                 mutex_enter(&vd->vdev_dtl_lock);
1157                 /*
1158                  * We're successfully scrubbed everything up to scrub_txg.
1159                  * Therefore, excise all old DTLs up to that point, then
1160                  * fold in the DTLs for everything we couldn't scrub.
1161                  */
1162                 if (scrub_txg != 0) {
1163                         space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1164                         space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1165                 }
1166                 if (scrub_done)
1167                         space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1168                 mutex_exit(&vd->vdev_dtl_lock);
1169                 if (txg != 0)
1170                         vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1171                 return;
1172         }
1173
1174         /*
1175          * Make sure the DTLs are always correct under the scrub lock.
1176          */
1177         if (vd == spa->spa_root_vdev)
1178                 mutex_enter(&spa->spa_scrub_lock);
1179
1180         mutex_enter(&vd->vdev_dtl_lock);
1181         space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1182         space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1183         mutex_exit(&vd->vdev_dtl_lock);
1184
1185         for (c = 0; c < vd->vdev_children; c++) {
1186                 vdev_t *cvd = vd->vdev_child[c];
1187                 vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1188                 mutex_enter(&vd->vdev_dtl_lock);
1189                 space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1190                 space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1191                 mutex_exit(&vd->vdev_dtl_lock);
1192         }
1193
1194         if (vd == spa->spa_root_vdev)
1195                 mutex_exit(&spa->spa_scrub_lock);
1196 }
1197
1198 static int
1199 vdev_dtl_load(vdev_t *vd)
1200 {
1201         spa_t *spa = vd->vdev_spa;
1202         space_map_obj_t *smo = &vd->vdev_dtl;
1203         objset_t *mos = spa->spa_meta_objset;
1204         dmu_buf_t *db;
1205         int error;
1206
1207         ASSERT(vd->vdev_children == 0);
1208
1209         if (smo->smo_object == 0)
1210                 return (0);
1211
1212         if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1213                 return (error);
1214
1215         ASSERT3U(db->db_size, ==, sizeof (*smo));
1216         bcopy(db->db_data, smo, db->db_size);
1217         dmu_buf_rele(db, FTAG);
1218
1219         mutex_enter(&vd->vdev_dtl_lock);
1220         error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1221         mutex_exit(&vd->vdev_dtl_lock);
1222
1223         return (error);
1224 }
1225
1226 void
1227 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1228 {
1229         spa_t *spa = vd->vdev_spa;
1230         space_map_obj_t *smo = &vd->vdev_dtl;
1231         space_map_t *sm = &vd->vdev_dtl_map;
1232         objset_t *mos = spa->spa_meta_objset;
1233         space_map_t smsync;
1234         kmutex_t smlock;
1235         dmu_buf_t *db;
1236         dmu_tx_t *tx;
1237
1238         dprintf("%s in txg %llu pass %d\n",
1239             vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1240
1241         tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1242
1243         if (vd->vdev_detached) {
1244                 if (smo->smo_object != 0) {
1245                         int err = dmu_object_free(mos, smo->smo_object, tx);
1246                         ASSERT3U(err, ==, 0);
1247                         smo->smo_object = 0;
1248                 }
1249                 dmu_tx_commit(tx);
1250                 dprintf("detach %s committed in txg %llu\n",
1251                     vdev_description(vd), txg);
1252                 return;
1253         }
1254
1255         if (smo->smo_object == 0) {
1256                 ASSERT(smo->smo_objsize == 0);
1257                 ASSERT(smo->smo_alloc == 0);
1258                 smo->smo_object = dmu_object_alloc(mos,
1259                     DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1260                     DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1261                 ASSERT(smo->smo_object != 0);
1262                 vdev_config_dirty(vd->vdev_top);
1263         }
1264
1265         mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1266
1267         space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1268             &smlock);
1269
1270         mutex_enter(&smlock);
1271
1272         mutex_enter(&vd->vdev_dtl_lock);
1273         space_map_walk(sm, space_map_add, &smsync);
1274         mutex_exit(&vd->vdev_dtl_lock);
1275
1276         space_map_truncate(smo, mos, tx);
1277         space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1278
1279         space_map_destroy(&smsync);
1280
1281         mutex_exit(&smlock);
1282         mutex_destroy(&smlock);
1283
1284         VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1285         dmu_buf_will_dirty(db, tx);
1286         ASSERT3U(db->db_size, ==, sizeof (*smo));
1287         bcopy(smo, db->db_data, db->db_size);
1288         dmu_buf_rele(db, FTAG);
1289
1290         dmu_tx_commit(tx);
1291 }
1292
1293 void
1294 vdev_load(vdev_t *vd)
1295 {
1296         int c;
1297
1298         /*
1299          * Recursively load all children.
1300          */
1301         for (c = 0; c < vd->vdev_children; c++)
1302                 vdev_load(vd->vdev_child[c]);
1303
1304         /*
1305          * If this is a top-level vdev, initialize its metaslabs.
1306          */
1307         if (vd == vd->vdev_top &&
1308             (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1309             vdev_metaslab_init(vd, 0) != 0))
1310                 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1311                     VDEV_AUX_CORRUPT_DATA);
1312
1313         /*
1314          * If this is a leaf vdev, load its DTL.
1315          */
1316         if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1317                 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1318                     VDEV_AUX_CORRUPT_DATA);
1319 }
1320
1321 /*
1322  * This special case of vdev_spare() is used for hot spares.  It's sole purpose
1323  * it to set the vdev state for the associated vdev.  To do this, we make sure
1324  * that we can open the underlying device, then try to read the label, and make
1325  * sure that the label is sane and that it hasn't been repurposed to another
1326  * pool.
1327  */
1328 int
1329 vdev_validate_spare(vdev_t *vd)
1330 {
1331         nvlist_t *label;
1332         uint64_t guid, version;
1333         uint64_t state;
1334
1335         if ((label = vdev_label_read_config(vd)) == NULL) {
1336                 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1337                     VDEV_AUX_CORRUPT_DATA);
1338                 return (-1);
1339         }
1340
1341         if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1342             version > ZFS_VERSION ||
1343             nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1344             guid != vd->vdev_guid ||
1345             nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1346                 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1347                     VDEV_AUX_CORRUPT_DATA);
1348                 nvlist_free(label);
1349                 return (-1);
1350         }
1351
1352         spa_spare_add(vd);
1353
1354         /*
1355          * We don't actually check the pool state here.  If it's in fact in
1356          * use by another pool, we update this fact on the fly when requested.
1357          */
1358         nvlist_free(label);
1359         return (0);
1360 }
1361
1362 void
1363 vdev_sync_done(vdev_t *vd, uint64_t txg)
1364 {
1365         metaslab_t *msp;
1366
1367         dprintf("%s txg %llu\n", vdev_description(vd), txg);
1368
1369         while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1370                 metaslab_sync_done(msp, txg);
1371 }
1372
1373 void
1374 vdev_sync(vdev_t *vd, uint64_t txg)
1375 {
1376         spa_t *spa = vd->vdev_spa;
1377         vdev_t *lvd;
1378         metaslab_t *msp;
1379         dmu_tx_t *tx;
1380
1381         dprintf("%s txg %llu pass %d\n",
1382             vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1383
1384         if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1385                 ASSERT(vd == vd->vdev_top);
1386                 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1387                 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1388                     DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1389                 ASSERT(vd->vdev_ms_array != 0);
1390                 vdev_config_dirty(vd);
1391                 dmu_tx_commit(tx);
1392         }
1393
1394         while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1395                 metaslab_sync(msp, txg);
1396                 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1397         }
1398
1399         while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1400                 vdev_dtl_sync(lvd, txg);
1401
1402         (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1403 }
1404
1405 uint64_t
1406 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1407 {
1408         return (vd->vdev_ops->vdev_op_asize(vd, psize));
1409 }
1410
1411 void
1412 vdev_io_start(zio_t *zio)
1413 {
1414         zio->io_vd->vdev_ops->vdev_op_io_start(zio);
1415 }
1416
1417 void
1418 vdev_io_done(zio_t *zio)
1419 {
1420         zio->io_vd->vdev_ops->vdev_op_io_done(zio);
1421 }
1422
1423 const char *
1424 vdev_description(vdev_t *vd)
1425 {
1426         if (vd == NULL || vd->vdev_ops == NULL)
1427                 return ("<unknown>");
1428
1429         if (vd->vdev_path != NULL)
1430                 return (vd->vdev_path);
1431
1432         if (vd->vdev_parent == NULL)
1433                 return (spa_name(vd->vdev_spa));
1434
1435         return (vd->vdev_ops->vdev_op_type);
1436 }
1437
1438 int
1439 vdev_online(spa_t *spa, uint64_t guid)
1440 {
1441         vdev_t *rvd, *vd;
1442         uint64_t txg;
1443
1444         txg = spa_vdev_enter(spa);
1445
1446         rvd = spa->spa_root_vdev;
1447
1448         if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1449                 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1450
1451         if (!vd->vdev_ops->vdev_op_leaf)
1452                 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1453
1454         dprintf("ONLINE: %s\n", vdev_description(vd));
1455
1456         vd->vdev_offline = B_FALSE;
1457         vd->vdev_tmpoffline = B_FALSE;
1458         vdev_reopen(vd->vdev_top);
1459
1460         vdev_config_dirty(vd->vdev_top);
1461
1462         (void) spa_vdev_exit(spa, NULL, txg, 0);
1463
1464         VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1465
1466         return (0);
1467 }
1468
1469 int
1470 vdev_offline(spa_t *spa, uint64_t guid, int istmp)
1471 {
1472         vdev_t *rvd, *vd;
1473         uint64_t txg;
1474
1475         txg = spa_vdev_enter(spa);
1476
1477         rvd = spa->spa_root_vdev;
1478
1479         if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1480                 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1481
1482         if (!vd->vdev_ops->vdev_op_leaf)
1483                 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1484
1485         dprintf("OFFLINE: %s\n", vdev_description(vd));
1486
1487         /*
1488          * If the device isn't already offline, try to offline it.
1489          */
1490         if (!vd->vdev_offline) {
1491                 /*
1492                  * If this device's top-level vdev has a non-empty DTL,
1493                  * don't allow the device to be offlined.
1494                  *
1495                  * XXX -- make this more precise by allowing the offline
1496                  * as long as the remaining devices don't have any DTL holes.
1497                  */
1498                 if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1499                         return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1500
1501                 /*
1502                  * Offline this device and reopen its top-level vdev.
1503                  * If this action results in the top-level vdev becoming
1504                  * unusable, undo it and fail the request.
1505                  */
1506                 vd->vdev_offline = B_TRUE;
1507                 vdev_reopen(vd->vdev_top);
1508                 if (vdev_is_dead(vd->vdev_top)) {
1509                         vd->vdev_offline = B_FALSE;
1510                         vdev_reopen(vd->vdev_top);
1511                         return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1512                 }
1513         }
1514
1515         vd->vdev_tmpoffline = istmp;
1516
1517         vdev_config_dirty(vd->vdev_top);
1518
1519         return (spa_vdev_exit(spa, NULL, txg, 0));
1520 }
1521
1522 /*
1523  * Clear the error counts associated with this vdev.  Unlike vdev_online() and
1524  * vdev_offline(), we assume the spa config is locked.  We also clear all
1525  * children.  If 'vd' is NULL, then the user wants to clear all vdevs.
1526  */
1527 void
1528 vdev_clear(spa_t *spa, vdev_t *vd)
1529 {
1530         int c;
1531
1532         if (vd == NULL)
1533                 vd = spa->spa_root_vdev;
1534
1535         vd->vdev_stat.vs_read_errors = 0;
1536         vd->vdev_stat.vs_write_errors = 0;
1537         vd->vdev_stat.vs_checksum_errors = 0;
1538
1539         for (c = 0; c < vd->vdev_children; c++)
1540                 vdev_clear(spa, vd->vdev_child[c]);
1541 }
1542
1543 int
1544 vdev_is_dead(vdev_t *vd)
1545 {
1546         return (vd->vdev_state <= VDEV_STATE_CANT_OPEN);
1547 }
1548
1549 int
1550 vdev_error_inject(vdev_t *vd, zio_t *zio)
1551 {
1552         int error = 0;
1553
1554         if (vd->vdev_fault_mode == VDEV_FAULT_NONE)
1555                 return (0);
1556
1557         if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0)
1558                 return (0);
1559
1560         switch (vd->vdev_fault_mode) {
1561         case VDEV_FAULT_RANDOM:
1562                 if (spa_get_random(vd->vdev_fault_arg) == 0)
1563                         error = EIO;
1564                 break;
1565
1566         case VDEV_FAULT_COUNT:
1567                 if ((int64_t)--vd->vdev_fault_arg <= 0)
1568                         vd->vdev_fault_mode = VDEV_FAULT_NONE;
1569                 error = EIO;
1570                 break;
1571         }
1572
1573         if (error != 0) {
1574                 dprintf("returning %d for type %d on %s state %d offset %llx\n",
1575                     error, zio->io_type, vdev_description(vd),
1576                     vd->vdev_state, zio->io_offset);
1577         }
1578
1579         return (error);
1580 }
1581
1582 /*
1583  * Get statistics for the given vdev.
1584  */
1585 void
1586 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1587 {
1588         vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1589         int c, t;
1590
1591         mutex_enter(&vd->vdev_stat_lock);
1592         bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1593         vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1594         vs->vs_state = vd->vdev_state;
1595         vs->vs_rsize = vdev_get_rsize(vd);
1596         mutex_exit(&vd->vdev_stat_lock);
1597
1598         /*
1599          * If we're getting stats on the root vdev, aggregate the I/O counts
1600          * over all top-level vdevs (i.e. the direct children of the root).
1601          */
1602         if (vd == rvd) {
1603                 for (c = 0; c < rvd->vdev_children; c++) {
1604                         vdev_t *cvd = rvd->vdev_child[c];
1605                         vdev_stat_t *cvs = &cvd->vdev_stat;
1606
1607                         mutex_enter(&vd->vdev_stat_lock);
1608                         for (t = 0; t < ZIO_TYPES; t++) {
1609                                 vs->vs_ops[t] += cvs->vs_ops[t];
1610                                 vs->vs_bytes[t] += cvs->vs_bytes[t];
1611                         }
1612                         vs->vs_read_errors += cvs->vs_read_errors;
1613                         vs->vs_write_errors += cvs->vs_write_errors;
1614                         vs->vs_checksum_errors += cvs->vs_checksum_errors;
1615                         vs->vs_scrub_examined += cvs->vs_scrub_examined;
1616                         vs->vs_scrub_errors += cvs->vs_scrub_errors;
1617                         mutex_exit(&vd->vdev_stat_lock);
1618                 }
1619         }
1620 }
1621
1622 void
1623 vdev_stat_update(zio_t *zio)
1624 {
1625         vdev_t *vd = zio->io_vd;
1626         vdev_t *pvd;
1627         uint64_t txg = zio->io_txg;
1628         vdev_stat_t *vs = &vd->vdev_stat;
1629         zio_type_t type = zio->io_type;
1630         int flags = zio->io_flags;
1631
1632         if (zio->io_error == 0) {
1633                 if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1634                         mutex_enter(&vd->vdev_stat_lock);
1635                         vs->vs_ops[type]++;
1636                         vs->vs_bytes[type] += zio->io_size;
1637                         mutex_exit(&vd->vdev_stat_lock);
1638                 }
1639                 if ((flags & ZIO_FLAG_IO_REPAIR) &&
1640                     zio->io_delegate_list == NULL) {
1641                         mutex_enter(&vd->vdev_stat_lock);
1642                         if (flags & ZIO_FLAG_SCRUB_THREAD)
1643                                 vs->vs_scrub_repaired += zio->io_size;
1644                         else
1645                                 vs->vs_self_healed += zio->io_size;
1646                         mutex_exit(&vd->vdev_stat_lock);
1647                 }
1648                 return;
1649         }
1650
1651         if (flags & ZIO_FLAG_SPECULATIVE)
1652                 return;
1653
1654         if (!vdev_is_dead(vd)) {
1655                 mutex_enter(&vd->vdev_stat_lock);
1656                 if (type == ZIO_TYPE_READ) {
1657                         if (zio->io_error == ECKSUM)
1658                                 vs->vs_checksum_errors++;
1659                         else
1660                                 vs->vs_read_errors++;
1661                 }
1662                 if (type == ZIO_TYPE_WRITE)
1663                         vs->vs_write_errors++;
1664                 mutex_exit(&vd->vdev_stat_lock);
1665         }
1666
1667         if (type == ZIO_TYPE_WRITE) {
1668                 if (txg == 0 || vd->vdev_children != 0)
1669                         return;
1670                 if (flags & ZIO_FLAG_SCRUB_THREAD) {
1671                         ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1672                         for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1673                                 vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1674                 }
1675                 if (!(flags & ZIO_FLAG_IO_REPAIR)) {
1676                         if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
1677                                 return;
1678                         vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1679                         for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1680                                 vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
1681                 }
1682         }
1683 }
1684
1685 void
1686 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
1687 {
1688         int c;
1689         vdev_stat_t *vs = &vd->vdev_stat;
1690
1691         for (c = 0; c < vd->vdev_children; c++)
1692                 vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
1693
1694         mutex_enter(&vd->vdev_stat_lock);
1695
1696         if (type == POOL_SCRUB_NONE) {
1697                 /*
1698                  * Update completion and end time.  Leave everything else alone
1699                  * so we can report what happened during the previous scrub.
1700                  */
1701                 vs->vs_scrub_complete = complete;
1702                 vs->vs_scrub_end = gethrestime_sec();
1703         } else {
1704                 vs->vs_scrub_type = type;
1705                 vs->vs_scrub_complete = 0;
1706                 vs->vs_scrub_examined = 0;
1707                 vs->vs_scrub_repaired = 0;
1708                 vs->vs_scrub_errors = 0;
1709                 vs->vs_scrub_start = gethrestime_sec();
1710                 vs->vs_scrub_end = 0;
1711         }
1712
1713         mutex_exit(&vd->vdev_stat_lock);
1714 }
1715
1716 /*
1717  * Update the in-core space usage stats for this vdev and the root vdev.
1718  */
1719 void
1720 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta)
1721 {
1722         ASSERT(vd == vd->vdev_top);
1723         int64_t dspace_delta = space_delta;
1724
1725         do {
1726                 if (vd->vdev_ms_count) {
1727                         /*
1728                          * If this is a top-level vdev, apply the
1729                          * inverse of its psize-to-asize (ie. RAID-Z)
1730                          * space-expansion factor.  We must calculate
1731                          * this here and not at the root vdev because
1732                          * the root vdev's psize-to-asize is simply the
1733                          * max of its childrens', thus not accurate
1734                          * enough for us.
1735                          */
1736                         ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
1737                         dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
1738                             vd->vdev_deflate_ratio;
1739                 }
1740
1741                 mutex_enter(&vd->vdev_stat_lock);
1742                 vd->vdev_stat.vs_space += space_delta;
1743                 vd->vdev_stat.vs_alloc += alloc_delta;
1744                 vd->vdev_stat.vs_dspace += dspace_delta;
1745                 mutex_exit(&vd->vdev_stat_lock);
1746         } while ((vd = vd->vdev_parent) != NULL);
1747 }
1748
1749 /*
1750  * Mark a top-level vdev's config as dirty, placing it on the dirty list
1751  * so that it will be written out next time the vdev configuration is synced.
1752  * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
1753  */
1754 void
1755 vdev_config_dirty(vdev_t *vd)
1756 {
1757         spa_t *spa = vd->vdev_spa;
1758         vdev_t *rvd = spa->spa_root_vdev;
1759         int c;
1760
1761         /*
1762          * The dirty list is protected by the config lock.  The caller must
1763          * either hold the config lock as writer, or must be the sync thread
1764          * (which holds the lock as reader).  There's only one sync thread,
1765          * so this is sufficient to ensure mutual exclusion.
1766          */
1767         ASSERT(spa_config_held(spa, RW_WRITER) ||
1768             dsl_pool_sync_context(spa_get_dsl(spa)));
1769
1770         if (vd == rvd) {
1771                 for (c = 0; c < rvd->vdev_children; c++)
1772                         vdev_config_dirty(rvd->vdev_child[c]);
1773         } else {
1774                 ASSERT(vd == vd->vdev_top);
1775
1776                 if (!list_link_active(&vd->vdev_dirty_node))
1777                         list_insert_head(&spa->spa_dirty_list, vd);
1778         }
1779 }
1780
1781 void
1782 vdev_config_clean(vdev_t *vd)
1783 {
1784         spa_t *spa = vd->vdev_spa;
1785
1786         ASSERT(spa_config_held(spa, RW_WRITER) ||
1787             dsl_pool_sync_context(spa_get_dsl(spa)));
1788
1789         ASSERT(list_link_active(&vd->vdev_dirty_node));
1790         list_remove(&spa->spa_dirty_list, vd);
1791 }
1792
1793 void
1794 vdev_propagate_state(vdev_t *vd)
1795 {
1796         vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1797         int degraded = 0, faulted = 0;
1798         int corrupted = 0;
1799         int c;
1800         vdev_t *child;
1801
1802         for (c = 0; c < vd->vdev_children; c++) {
1803                 child = vd->vdev_child[c];
1804                 if (child->vdev_state <= VDEV_STATE_CANT_OPEN)
1805                         faulted++;
1806                 else if (child->vdev_state == VDEV_STATE_DEGRADED)
1807                         degraded++;
1808
1809                 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
1810                         corrupted++;
1811         }
1812
1813         vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
1814
1815         /*
1816          * Root special: if there is a toplevel vdev that cannot be
1817          * opened due to corrupted metadata, then propagate the root
1818          * vdev's aux state as 'corrupt' rather than 'insufficient
1819          * replicas'.
1820          */
1821         if (corrupted && vd == rvd && rvd->vdev_state == VDEV_STATE_CANT_OPEN)
1822                 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
1823                     VDEV_AUX_CORRUPT_DATA);
1824 }
1825
1826 /*
1827  * Set a vdev's state.  If this is during an open, we don't update the parent
1828  * state, because we're in the process of opening children depth-first.
1829  * Otherwise, we propagate the change to the parent.
1830  *
1831  * If this routine places a device in a faulted state, an appropriate ereport is
1832  * generated.
1833  */
1834 void
1835 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
1836 {
1837         uint64_t save_state;
1838
1839         if (state == vd->vdev_state) {
1840                 vd->vdev_stat.vs_aux = aux;
1841                 return;
1842         }
1843
1844         save_state = vd->vdev_state;
1845
1846         vd->vdev_state = state;
1847         vd->vdev_stat.vs_aux = aux;
1848
1849         /*
1850          * If we are setting the vdev state to anything but an open state, then
1851          * always close the underlying device.  Otherwise, we keep accessible
1852          * but invalid devices open forever.  We don't call vdev_close() itself,
1853          * because that implies some extra checks (offline, etc) that we don't
1854          * want here.  This is limited to leaf devices, because otherwise
1855          * closing the device will affect other children.
1856          */
1857         if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
1858                 vd->vdev_ops->vdev_op_close(vd);
1859
1860         if (state == VDEV_STATE_CANT_OPEN) {
1861                 /*
1862                  * If we fail to open a vdev during an import, we mark it as
1863                  * "not available", which signifies that it was never there to
1864                  * begin with.  Failure to open such a device is not considered
1865                  * an error.
1866                  */
1867                 if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT &&
1868                     vd->vdev_ops->vdev_op_leaf)
1869                         vd->vdev_not_present = 1;
1870
1871                 /*
1872                  * Post the appropriate ereport.  If the 'prevstate' field is
1873                  * set to something other than VDEV_STATE_UNKNOWN, it indicates
1874                  * that this is part of a vdev_reopen().  In this case, we don't
1875                  * want to post the ereport if the device was already in the
1876                  * CANT_OPEN state beforehand.
1877                  */
1878                 if (vd->vdev_prevstate != state && !vd->vdev_not_present &&
1879                     vd != vd->vdev_spa->spa_root_vdev) {
1880                         const char *class;
1881
1882                         switch (aux) {
1883                         case VDEV_AUX_OPEN_FAILED:
1884                                 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
1885                                 break;
1886                         case VDEV_AUX_CORRUPT_DATA:
1887                                 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
1888                                 break;
1889                         case VDEV_AUX_NO_REPLICAS:
1890                                 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
1891                                 break;
1892                         case VDEV_AUX_BAD_GUID_SUM:
1893                                 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
1894                                 break;
1895                         case VDEV_AUX_TOO_SMALL:
1896                                 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
1897                                 break;
1898                         case VDEV_AUX_BAD_LABEL:
1899                                 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
1900                                 break;
1901                         default:
1902                                 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
1903                         }
1904
1905                         zfs_ereport_post(class, vd->vdev_spa,
1906                             vd, NULL, save_state, 0);
1907                 }
1908         }
1909
1910         if (isopen)
1911                 return;
1912
1913         if (vd->vdev_parent != NULL)
1914                 vdev_propagate_state(vd->vdev_parent);
1915 }