~ubuntu-branches/ubuntu/quantal/zfs-fuse/quantal

« back to all changes in this revision

Viewing changes to src/lib/libzpool/vdev.c

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey, Mike Hommey, Seth Heeren
  • Date: 2010-06-30 18:03:52 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100630180352-d3jq25ytbcl23q3y
Tags: 0.6.9-1
* New upstream release.

[ Mike Hommey ]
* debian/control:
  - Build depend on libssl-dev and libattr1-dev, now required to build.
  - Build depend on docbook-xml to avoid xsltproc I/O error loading
    docbook DTD.
  - Add suggestions for a NFS server and kpartx.
* debian/man/*, debian/copyright, debian/rules: Remove manual pages, they
  are now shipped upstream.
* debian/copyright: Change download link.
* src/SConstruct:
  - Add an optim option to the build system.
  - Add support for DESTDIR.
  - Force debug=1 to mean optim, no strip, no debug.
  - Use -ffunction-sections, -fdata-sections, and --gc-sections flags to
    reduce the binary sizes.
* src/lib/libumem/SConscript: Cleanup src/lib/libumem when cleaning up
  build directory.
* src/cmd/*/SConscript: Don't link zfs, zpool and zdb against libssl.
* src/lib/libumem/SConscript: Only build static libumem.
* src/lib/libumem/sol_compat.h:
  - Add atomic cas support for sparc.
  - Use atomic functions from libsolcompat in libumem on unsupported
    platforms.
* debian/rules:
  - Set optimization level in build system according to DEB_BUILD_OPTIONS.
  - Build with debug=1 to have unstripped binaries ; dh_strip will do the
    right thing.
  - Don't depend on the local location of the docbook XSLT stylesheets.
    Use the catalogged url in place of the full path.
  - Don't clean src/.sconsign.dblite and src/path.pyc.
  - Set all destination directories when installing with scons.
  - Install bash completion and zfsrc files.
  - Don't use scons cache when building.
* debian/prerm: Remove /var/lib/zfs/zpool.cache in prerm.
* debian/dirs: Create /etc/bash_completion.d.
* debian/watch: Fix watch file.
* debian/rules, debian/control, debian/compat: Switch to dh.
* debian/README.Debian: Update README.Debian.
* debian/zfs-fuse.man.xml: Update zfs-fuse manual page.
* debian/zfs-fuse.init: Start sharing datasets marked as such at daemon
  startup.
* debian/rules, debian/control: Use config.guess and config.sub from
  autotools-dev.

[ Seth Heeren ]
* debian/zfs-fuse.man.xml:
  Added notes on the precedence, zfsrc, commandline, initscript vs.
  /etc/default/zfs-fuse on some systems.
* debian/zfs-fuse.init, debian/zfs-fuse.default: Deprecating DAEMON_OPTS.
* debian/zfs-fuse.init:
  - Removing import -a -f.
  - Removing the now unnecessary 'sleep 2'.
  - Extended shutdown wait to allow for zfs-fuse daemon's own shutdown
    timeouts.
  - Re-ordered dubious PATH setting.
* debian/zfs-fuse.init: Move existing zpool.cache to new location if
  possible.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
 
22
22
/*
23
 
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 
23
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24
24
 * Use is subject to license terms.
25
25
 */
26
26
 
40
40
#include <sys/fs/zfs.h>
41
41
#include <sys/arc.h>
42
42
#include <sys/zil.h>
 
43
#include <syslog.h>
 
44
#include <libintl.h>
43
45
 
44
46
/*
45
47
 * Virtual device management.
54
56
        &vdev_disk_ops,
55
57
        &vdev_file_ops,
56
58
        &vdev_missing_ops,
 
59
        &vdev_hole_ops,
57
60
        NULL
58
61
};
59
62
 
84
87
{
85
88
        uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
86
89
        uint64_t csize;
87
 
        uint64_t c;
88
90
 
89
 
        for (c = 0; c < vd->vdev_children; c++) {
 
91
        for (int c = 0; c < vd->vdev_children; c++) {
90
92
                csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
91
93
                asize = MAX(asize, csize);
92
94
        }
95
97
}
96
98
 
97
99
/*
98
 
 * Get the replaceable or attachable device size.
99
 
 * If the parent is a mirror or raidz, the replaceable size is the minimum
100
 
 * psize of all its children. For the rest, just return our own psize.
101
 
 *
102
 
 * e.g.
103
 
 *                      psize   rsize
104
 
 * root                 -       -
105
 
 *      mirror/raidz    -       -
106
 
 *          disk1       20g     20g
107
 
 *          disk2       40g     20g
108
 
 *      disk3           80g     80g
 
100
 * Get the minimum allocatable size. We define the allocatable size as
 
101
 * the vdev's asize rounded to the nearest metaslab. This allows us to
 
102
 * replace or attach devices which don't have the same physical size but
 
103
 * can still satisfy the same number of allocations.
109
104
 */
110
105
uint64_t
111
 
vdev_get_rsize(vdev_t *vd)
112
 
{
113
 
        vdev_t *pvd, *cvd;
114
 
        uint64_t c, rsize;
115
 
 
116
 
        pvd = vd->vdev_parent;
117
 
 
118
 
        /*
119
 
         * If our parent is NULL or the root, just return our own psize.
120
 
         */
121
 
        if (pvd == NULL || pvd->vdev_parent == NULL)
122
 
                return (vd->vdev_psize);
123
 
 
124
 
        rsize = 0;
125
 
 
126
 
        for (c = 0; c < pvd->vdev_children; c++) {
127
 
                cvd = pvd->vdev_child[c];
128
 
                rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
129
 
        }
130
 
 
131
 
        return (rsize);
 
106
vdev_get_min_asize(vdev_t *vd)
 
107
{
 
108
        vdev_t *pvd = vd->vdev_parent;
 
109
 
 
110
        /*
 
111
         * The our parent is NULL (inactive spare or cache) or is the root,
 
112
         * just return our own asize.
 
113
         */
 
114
        if (pvd == NULL)
 
115
                return (vd->vdev_asize);
 
116
 
 
117
        /*
 
118
         * The top-level vdev just returns the allocatable size rounded
 
119
         * to the nearest metaslab.
 
120
         */
 
121
        if (vd == vd->vdev_top)
 
122
                return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
 
123
 
 
124
        /*
 
125
         * The allocatable space for a raidz vdev is N * sizeof(smallest child),
 
126
         * so each child must provide at least 1/Nth of its asize.
 
127
         */
 
128
        if (pvd->vdev_ops == &vdev_raidz_ops)
 
129
                return (pvd->vdev_min_asize / pvd->vdev_children);
 
130
 
 
131
        return (pvd->vdev_min_asize);
 
132
}
 
133
 
 
134
void
 
135
vdev_set_min_asize(vdev_t *vd)
 
136
{
 
137
        vd->vdev_min_asize = vdev_get_min_asize(vd);
 
138
 
 
139
        for (int c = 0; c < vd->vdev_children; c++)
 
140
                vdev_set_min_asize(vd->vdev_child[c]);
132
141
}
133
142
 
134
143
vdev_t *
149
158
vdev_t *
150
159
vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
151
160
{
152
 
        int c;
153
161
        vdev_t *mvd;
154
162
 
155
163
        if (vd->vdev_guid == guid)
156
164
                return (vd);
157
165
 
158
 
        for (c = 0; c < vd->vdev_children; c++)
 
166
        for (int c = 0; c < vd->vdev_children; c++)
159
167
                if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
160
168
                    NULL)
161
169
                        return (mvd);
251
259
{
252
260
        vdev_t **newchild, *cvd;
253
261
        int oldc = pvd->vdev_children;
254
 
        int newc, c;
 
262
        int newc;
255
263
 
256
264
        ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
257
265
 
258
 
        for (c = newc = 0; c < oldc; c++)
 
266
        for (int c = newc = 0; c < oldc; c++)
259
267
                if (pvd->vdev_child[c])
260
268
                        newc++;
261
269
 
262
270
        newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
263
271
 
264
 
        for (c = newc = 0; c < oldc; c++) {
 
272
        for (int c = newc = 0; c < oldc; c++) {
265
273
                if ((cvd = pvd->vdev_child[c]) != NULL) {
266
274
                        newchild[newc] = cvd;
267
275
                        cvd->vdev_id = newc++;
276
284
/*
277
285
 * Allocate and minimally initialize a vdev_t.
278
286
 */
279
 
static vdev_t *
 
287
vdev_t *
280
288
vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
281
289
{
282
290
        vdev_t *vd;
288
296
                spa->spa_root_vdev = vd;
289
297
        }
290
298
 
291
 
        if (guid == 0) {
 
299
        if (guid == 0 && ops != &vdev_hole_ops) {
292
300
                if (spa->spa_root_vdev == vd) {
293
301
                        /*
294
302
                         * The root vdev's guid will also be the pool guid,
295
303
                         * which must be unique among all pools.
296
304
                         */
297
 
                        while (guid == 0 || spa_guid_exists(guid, 0))
298
 
                                guid = spa_get_random(-1ULL);
 
305
                        guid = spa_generate_guid(NULL);
299
306
                } else {
300
307
                        /*
301
308
                         * Any other vdev's guid must be unique within the pool.
302
309
                         */
303
 
                        while (guid == 0 ||
304
 
                            spa_guid_exists(spa_guid(spa), guid))
305
 
                                guid = spa_get_random(-1ULL);
 
310
                        guid = spa_generate_guid(spa);
306
311
                }
307
312
                ASSERT(!spa_guid_exists(spa_guid(spa), guid));
308
313
        }
313
318
        vd->vdev_guid_sum = guid;
314
319
        vd->vdev_ops = ops;
315
320
        vd->vdev_state = VDEV_STATE_CLOSED;
 
321
        vd->vdev_ishole = (ops == &vdev_hole_ops);
316
322
 
317
323
        mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
318
324
        mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
373
379
        } else if (alloctype == VDEV_ALLOC_L2CACHE) {
374
380
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
375
381
                        return (EINVAL);
 
382
        } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
 
383
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
 
384
                        return (EINVAL);
376
385
        }
377
386
 
378
387
        /*
389
398
        if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
390
399
                return (ENOTSUP);
391
400
 
 
401
        if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
 
402
                return (ENOTSUP);
 
403
 
392
404
        /*
393
405
         * Set the nparity property for RAID-Z vdevs.
394
406
         */
396
408
        if (ops == &vdev_raidz_ops) {
397
409
                if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
398
410
                    &nparity) == 0) {
399
 
                        /*
400
 
                         * Currently, we can only support 2 parity devices.
401
 
                         */
402
 
                        if (nparity == 0 || nparity > 2)
 
411
                        if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
403
412
                                return (EINVAL);
404
413
                        /*
405
 
                         * Older versions can only support 1 parity device.
 
414
                         * Previous versions could only support 1 or 2 parity
 
415
                         * device.
406
416
                         */
407
 
                        if (nparity == 2 &&
408
 
                            spa_version(spa) < SPA_VERSION_RAID6)
 
417
                        if (nparity > 1 &&
 
418
                            spa_version(spa) < SPA_VERSION_RAIDZ2)
 
419
                                return (ENOTSUP);
 
420
                        if (nparity > 2 &&
 
421
                            spa_version(spa) < SPA_VERSION_RAIDZ3)
409
422
                                return (ENOTSUP);
410
423
                } else {
411
424
                        /*
412
425
                         * We require the parity to be specified for SPAs that
413
426
                         * support multiple parity levels.
414
427
                         */
415
 
                        if (spa_version(spa) >= SPA_VERSION_RAID6)
 
428
                        if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
416
429
                                return (EINVAL);
417
430
                        /*
418
431
                         * Otherwise, we default to 1 parity device for RAID-Z.
460
473
        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
461
474
 
462
475
        /*
 
476
         * Retrieve the vdev creation time.
 
477
         */
 
478
        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
 
479
            &vd->vdev_crtxg);
 
480
 
 
481
        /*
463
482
         * If we're a top-level vdev, try to load the allocation parameters.
464
483
         */
465
 
        if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
 
484
        if (parent && !parent->vdev_parent &&
 
485
            (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
466
486
                (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
467
487
                    &vd->vdev_ms_array);
468
488
                (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
471
491
                    &vd->vdev_asize);
472
492
        }
473
493
 
 
494
        if (parent && !parent->vdev_parent) {
 
495
                ASSERT(alloctype == VDEV_ALLOC_LOAD ||
 
496
                    alloctype == VDEV_ALLOC_ADD ||
 
497
                    alloctype == VDEV_ALLOC_SPLIT ||
 
498
                    alloctype == VDEV_ALLOC_ROOTPOOL);
 
499
                vd->vdev_mg = metaslab_group_create(islog ?
 
500
                    spa_log_class(spa) : spa_normal_class(spa), vd);
 
501
        }
 
502
 
474
503
        /*
475
504
         * If we're a leaf vdev, try to load the DTL object and other state.
476
505
         */
477
506
        if (vd->vdev_ops->vdev_op_leaf &&
478
 
            (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) {
 
507
            (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
 
508
            alloctype == VDEV_ALLOC_ROOTPOOL)) {
479
509
                if (alloctype == VDEV_ALLOC_LOAD) {
480
510
                        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
481
511
                            &vd->vdev_dtl_smo.smo_object);
482
512
                        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
483
513
                            &vd->vdev_unspare);
484
514
                }
 
515
 
 
516
                if (alloctype == VDEV_ALLOC_ROOTPOOL) {
 
517
                        uint64_t spare = 0;
 
518
 
 
519
                        if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
 
520
                            &spare) == 0 && spare)
 
521
                                spa_spare_add(vd);
 
522
                }
 
523
 
485
524
                (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
486
525
                    &vd->vdev_offline);
487
526
 
488
527
                /*
489
528
                 * When importing a pool, we want to ignore the persistent fault
490
529
                 * state, as the diagnosis made on another system may not be
491
 
                 * valid in the current context.
 
530
                 * valid in the current context.  Local vdevs will
 
531
                 * remain in the faulted state.
492
532
                 */
493
 
                if (spa->spa_load_state == SPA_LOAD_OPEN) {
 
533
                if (spa_load_state(spa) == SPA_LOAD_OPEN) {
494
534
                        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
495
535
                            &vd->vdev_faulted);
496
536
                        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
497
537
                            &vd->vdev_degraded);
498
538
                        (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
499
539
                            &vd->vdev_removed);
 
540
 
 
541
                        if (vd->vdev_faulted || vd->vdev_degraded) {
 
542
                                char *aux;
 
543
 
 
544
                                vd->vdev_label_aux =
 
545
                                    VDEV_AUX_ERR_EXCEEDED;
 
546
                                if (nvlist_lookup_string(nv,
 
547
                                    ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
 
548
                                    strcmp(aux, "external") == 0)
 
549
                                        vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
 
550
                        }
500
551
                }
501
552
        }
502
553
 
513
564
void
514
565
vdev_free(vdev_t *vd)
515
566
{
516
 
        int c;
517
567
        spa_t *spa = vd->vdev_spa;
518
568
 
519
569
        /*
523
573
        vdev_close(vd);
524
574
 
525
575
        ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
 
576
        ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
526
577
 
527
578
        /*
528
579
         * Free all children.
529
580
         */
530
 
        for (c = 0; c < vd->vdev_children; c++)
 
581
        for (int c = 0; c < vd->vdev_children; c++)
531
582
                vdev_free(vd->vdev_child[c]);
532
583
 
533
584
        ASSERT(vd->vdev_child == NULL);
536
587
        /*
537
588
         * Discard allocation state.
538
589
         */
539
 
        if (vd == vd->vdev_top)
 
590
        if (vd->vdev_mg != NULL) {
540
591
                vdev_metaslab_fini(vd);
 
592
                metaslab_group_destroy(vd->vdev_mg);
 
593
        }
541
594
 
542
595
        ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
543
596
        ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
657
710
static void
658
711
vdev_top_update(vdev_t *tvd, vdev_t *vd)
659
712
{
660
 
        int c;
661
 
 
662
713
        if (vd == NULL)
663
714
                return;
664
715
 
665
716
        vd->vdev_top = tvd;
666
717
 
667
 
        for (c = 0; c < vd->vdev_children; c++)
 
718
        for (int c = 0; c < vd->vdev_children; c++)
668
719
                vdev_top_update(tvd, vd->vdev_child[c]);
669
720
}
670
721
 
683
734
        mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
684
735
 
685
736
        mvd->vdev_asize = cvd->vdev_asize;
 
737
        mvd->vdev_min_asize = cvd->vdev_min_asize;
686
738
        mvd->vdev_ashift = cvd->vdev_ashift;
687
739
        mvd->vdev_state = cvd->vdev_state;
 
740
        mvd->vdev_crtxg = cvd->vdev_crtxg;
688
741
 
689
742
        vdev_remove_child(pvd, cvd);
690
743
        vdev_add_child(pvd, mvd);
726
779
         */
727
780
        if (mvd->vdev_top == mvd) {
728
781
                uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
 
782
                cvd->vdev_orig_guid = cvd->vdev_guid;
729
783
                cvd->vdev_guid += guid_delta;
730
784
                cvd->vdev_guid_sum += guid_delta;
731
785
        }
745
799
{
746
800
        spa_t *spa = vd->vdev_spa;
747
801
        objset_t *mos = spa->spa_meta_objset;
748
 
        metaslab_class_t *mc;
749
802
        uint64_t m;
750
803
        uint64_t oldc = vd->vdev_ms_count;
751
804
        uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
752
805
        metaslab_t **mspp;
753
806
        int error;
754
807
 
755
 
        if (vd->vdev_ms_shift == 0)     /* not being allocated from yet */
 
808
        ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
 
809
 
 
810
        /*
 
811
         * This vdev is not being allocated from yet or is a hole.
 
812
         */
 
813
        if (vd->vdev_ms_shift == 0)
756
814
                return (0);
757
815
 
 
816
        ASSERT(!vd->vdev_ishole);
 
817
 
758
818
        /*
759
819
         * Compute the raidz-deflation ratio.  Note, we hard-code
760
820
         * in 128k (1 << 17) because it is the current "typical" blocksize.
766
826
 
767
827
        ASSERT(oldc <= newc);
768
828
 
769
 
        if (vd->vdev_islog)
770
 
                mc = spa->spa_log_class;
771
 
        else
772
 
                mc = spa->spa_normal_class;
773
 
 
774
 
        if (vd->vdev_mg == NULL)
775
 
                vd->vdev_mg = metaslab_group_create(mc, vd);
776
 
 
777
829
        mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
778
830
 
779
831
        if (oldc != 0) {
808
860
                    m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
809
861
        }
810
862
 
 
863
        if (txg == 0)
 
864
                spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
 
865
 
 
866
        if (oldc == 0)
 
867
                metaslab_group_activate(vd->vdev_mg);
 
868
 
 
869
        if (txg == 0)
 
870
                spa_config_exit(spa, SCL_ALLOC, FTAG);
 
871
 
811
872
        return (0);
812
873
}
813
874
 
818
879
        uint64_t count = vd->vdev_ms_count;
819
880
 
820
881
        if (vd->vdev_ms != NULL) {
 
882
                metaslab_group_passivate(vd->vdev_mg);
821
883
                for (m = 0; m < count; m++)
822
884
                        if (vd->vdev_ms[m] != NULL)
823
885
                                metaslab_fini(vd->vdev_ms[m]);
977
1039
        return (NULL);
978
1040
}
979
1041
 
 
1042
static void
 
1043
vdev_open_child(void *arg)
 
1044
{
 
1045
        vdev_t *vd = arg;
 
1046
 
 
1047
        vd->vdev_open_thread = curthread;
 
1048
        vd->vdev_open_error = vdev_open(vd);
 
1049
        vd->vdev_open_thread = NULL;
 
1050
}
 
1051
 
 
1052
boolean_t
 
1053
vdev_uses_zvols(vdev_t *vd)
 
1054
{
 
1055
        if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
 
1056
            strlen(ZVOL_DIR)) == 0)
 
1057
                return (B_TRUE);
 
1058
        for (int c = 0; c < vd->vdev_children; c++)
 
1059
                if (vdev_uses_zvols(vd->vdev_child[c]))
 
1060
                        return (B_TRUE);
 
1061
        return (B_FALSE);
 
1062
}
 
1063
 
 
1064
void
 
1065
vdev_open_children(vdev_t *vd)
 
1066
{
 
1067
        taskq_t *tq;
 
1068
        int children = vd->vdev_children;
 
1069
 
 
1070
        /*
 
1071
         * in order to handle pools on top of zvols, do the opens
 
1072
         * in a single thread so that the same thread holds the
 
1073
         * spa_namespace_lock
 
1074
         */
 
1075
        if (vdev_uses_zvols(vd)) {
 
1076
                for (int c = 0; c < children; c++)
 
1077
                        vd->vdev_child[c]->vdev_open_error =
 
1078
                            vdev_open(vd->vdev_child[c]);
 
1079
                return;
 
1080
        }
 
1081
        tq = taskq_create("vdev_open", children, minclsyspri,
 
1082
            children, children, TASKQ_PREPOPULATE);
 
1083
 
 
1084
        for (int c = 0; c < children; c++)
 
1085
                VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
 
1086
                    TQ_SLEEP) != NULL);
 
1087
 
 
1088
        taskq_destroy(tq);
 
1089
}
 
1090
 
980
1091
/*
981
1092
 * Prepare a virtual device for access.
982
1093
 */
985
1096
{
986
1097
        spa_t *spa = vd->vdev_spa;
987
1098
        int error;
988
 
        int c;
989
1099
        uint64_t osize = 0;
990
1100
        uint64_t asize, psize;
991
1101
        uint64_t ashift = 0;
992
1102
 
993
 
        ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
994
 
 
 
1103
        ASSERT(vd->vdev_open_thread == curthread ||
 
1104
            spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
995
1105
        ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
996
1106
            vd->vdev_state == VDEV_STATE_CANT_OPEN ||
997
1107
            vd->vdev_state == VDEV_STATE_OFFLINE);
999
1109
        vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1000
1110
        vd->vdev_cant_read = B_FALSE;
1001
1111
        vd->vdev_cant_write = B_FALSE;
 
1112
        vd->vdev_min_asize = vdev_get_min_asize(vd);
1002
1113
 
 
1114
        /*
 
1115
         * If this vdev is not removed, check its fault status.  If it's
 
1116
         * faulted, bail out of the open.
 
1117
         */
1003
1118
        if (!vd->vdev_removed && vd->vdev_faulted) {
1004
1119
                ASSERT(vd->vdev_children == 0);
 
1120
                ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
 
1121
                    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1005
1122
                vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1006
 
                    VDEV_AUX_ERR_EXCEEDED);
 
1123
                    vd->vdev_label_aux);
1007
1124
                return (ENXIO);
1008
1125
        } else if (vd->vdev_offline) {
1009
 
                dprintf("vdev_open(): vd->vdev_offline\n");
1010
1126
                ASSERT(vd->vdev_children == 0);
1011
1127
                vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1012
1128
                return (ENXIO);
1014
1130
 
1015
1131
        error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
1016
1132
 
 
1133
        /*
 
1134
         * Reset the vdev_reopening flag so that we actually close
 
1135
         * the vdev on error.
 
1136
         */
 
1137
        vd->vdev_reopening = B_FALSE;
1017
1138
        if (zio_injection_enabled && error == 0)
1018
1139
                error = zio_handle_device_injection(vd, NULL, ENXIO);
1019
1140
 
1029
1150
 
1030
1151
        vd->vdev_removed = B_FALSE;
1031
1152
 
 
1153
        /*
 
1154
         * Recheck the faulted flag now that we have confirmed that
 
1155
         * the vdev is accessible.  If we're faulted, bail.
 
1156
         */
 
1157
        if (vd->vdev_faulted) {
 
1158
                ASSERT(vd->vdev_children == 0);
 
1159
                ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
 
1160
                    vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
 
1161
                vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
 
1162
                    vd->vdev_label_aux);
 
1163
                return (ENXIO);
 
1164
        }
 
1165
 
1032
1166
        if (vd->vdev_degraded) {
1033
1167
                ASSERT(vd->vdev_children == 0);
1034
1168
                vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1035
1169
                    VDEV_AUX_ERR_EXCEEDED);
1036
1170
        } else {
1037
 
                vd->vdev_state = VDEV_STATE_HEALTHY;
 
1171
                vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1038
1172
        }
1039
1173
 
1040
 
        for (c = 0; c < vd->vdev_children; c++)
 
1174
        /*
 
1175
         * For hole or missing vdevs we just return success.
 
1176
         */
 
1177
        if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
 
1178
                return (0);
 
1179
 
 
1180
        for (int c = 0; c < vd->vdev_children; c++) {
1041
1181
                if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1042
1182
                        vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1043
1183
                            VDEV_AUX_NONE);
1044
1184
                        break;
1045
1185
                }
 
1186
        }
1046
1187
 
1047
1188
        osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1048
1189
 
1067
1208
 
1068
1209
        vd->vdev_psize = psize;
1069
1210
 
 
1211
        /*
 
1212
         * Make sure the allocatable size hasn't shrunk.
 
1213
         */
 
1214
        if (asize < vd->vdev_min_asize) {
 
1215
                vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
 
1216
                    VDEV_AUX_BAD_LABEL);
 
1217
                return (EINVAL);
 
1218
        }
 
1219
 
1070
1220
        if (vd->vdev_asize == 0) {
1071
1221
                /*
1072
1222
                 * This is the first-ever open, so use the computed values.
1083
1233
                            VDEV_AUX_BAD_LABEL);
1084
1234
                        return (EINVAL);
1085
1235
                }
1086
 
 
1087
 
                /*
1088
 
                 * Make sure the device hasn't shrunk.
1089
 
                 */
1090
 
                if (asize < vd->vdev_asize) {
1091
 
                        vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1092
 
                            VDEV_AUX_BAD_LABEL);
1093
 
                        return (EINVAL);
1094
 
                }
1095
 
 
1096
 
                /*
1097
 
                 * If all children are healthy and the asize has increased,
1098
 
                 * then we've experienced dynamic LUN growth.
1099
 
                 */
1100
 
                if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1101
 
                    asize > vd->vdev_asize) {
1102
 
                        vd->vdev_asize = asize;
1103
 
                }
1104
1236
        }
1105
1237
 
1106
1238
        /*
 
1239
         * If all children are healthy and the asize has increased,
 
1240
         * then we've experienced dynamic LUN growth.  If automatic
 
1241
         * expansion is enabled then use the additional space.
 
1242
         */
 
1243
        /* Force spa_autoexpand = 1 here - it's not initialised at this
 
1244
         * point in linux, and we want it initialised to be able to update
 
1245
         * the vdev size here while importing a pool */
 
1246
        spa->spa_autoexpand = 1;
 
1247
        if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
 
1248
            (vd->vdev_expanding || spa->spa_autoexpand))
 
1249
                vd->vdev_asize = asize;
 
1250
 
 
1251
        vdev_set_min_asize(vd);
 
1252
 
 
1253
        /*
1107
1254
         * Ensure we can issue some IO before declaring the
1108
1255
         * vdev open for business.
1109
1256
         */
1140
1287
vdev_validate(vdev_t *vd)
1141
1288
{
1142
1289
        spa_t *spa = vd->vdev_spa;
1143
 
        int c;
1144
1290
        nvlist_t *label;
1145
 
        uint64_t guid, top_guid;
 
1291
        uint64_t guid = 0, top_guid;
1146
1292
        uint64_t state;
1147
1293
 
1148
 
        for (c = 0; c < vd->vdev_children; c++)
 
1294
        for (int c = 0; c < vd->vdev_children; c++)
1149
1295
                if (vdev_validate(vd->vdev_child[c]) != 0)
1150
1296
                        return (EBADF);
1151
1297
 
1155
1301
         * overwrite the previous state.
1156
1302
         */
1157
1303
        if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
 
1304
                uint64_t aux_guid = 0;
 
1305
                nvlist_t *nvl;
1158
1306
 
1159
1307
                if ((label = vdev_label_read_config(vd)) == NULL) {
1160
1308
                        vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1162
1310
                        return (0);
1163
1311
                }
1164
1312
 
 
1313
                /*
 
1314
                 * Determine if this vdev has been split off into another
 
1315
                 * pool.  If so, then refuse to open it.
 
1316
                 */
 
1317
                if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
 
1318
                    &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
 
1319
                        vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
 
1320
                            VDEV_AUX_SPLIT_POOL);
 
1321
                        nvlist_free(label);
 
1322
                        return (0);
 
1323
                }
 
1324
 
1165
1325
                if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
1166
1326
                    &guid) != 0 || guid != spa_guid(spa)) {
1167
1327
                        vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1170
1330
                        return (0);
1171
1331
                }
1172
1332
 
 
1333
                if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
 
1334
                    != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
 
1335
                    &aux_guid) != 0)
 
1336
                        aux_guid = 0;
 
1337
 
1173
1338
                /*
1174
1339
                 * If this vdev just became a top-level vdev because its
1175
1340
                 * sibling was detached, it will have adopted the parent's
1177
1342
                 * Fortunately, either version of the label will have the
1178
1343
                 * same top guid, so if we're a top-level vdev, we can
1179
1344
                 * safely compare to that instead.
 
1345
                 *
 
1346
                 * If we split this vdev off instead, then we also check the
 
1347
                 * original pool's guid.  We don't want to consider the vdev
 
1348
                 * corrupt if it is partway through a split operation.
1180
1349
                 */
1181
1350
                if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1182
1351
                    &guid) != 0 ||
1183
1352
                    nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1184
1353
                    &top_guid) != 0 ||
1185
 
                    (vd->vdev_guid != guid &&
 
1354
                    ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1186
1355
                    (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1187
1356
                        vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1188
1357
                            VDEV_AUX_CORRUPT_DATA);
1200
1369
 
1201
1370
                nvlist_free(label);
1202
1371
 
1203
 
                if (spa->spa_load_state == SPA_LOAD_OPEN &&
 
1372
                /*
 
1373
                 * If spa->spa_load_verbatim is true, no need to check the
 
1374
                 * state of the pool.
 
1375
                 */
 
1376
                if (!spa->spa_load_verbatim &&
 
1377
                    spa_load_state(spa) == SPA_LOAD_OPEN &&
1204
1378
                    state != POOL_STATE_ACTIVE)
1205
1379
                        return (EBADF);
1206
1380
 
1223
1397
vdev_close(vdev_t *vd)
1224
1398
{
1225
1399
        spa_t *spa = vd->vdev_spa;
 
1400
        vdev_t *pvd = vd->vdev_parent;
1226
1401
 
1227
1402
        ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1228
1403
 
 
1404
        /*
 
1405
         * If our parent is reopening, then we are as well, unless we are
 
1406
         * going offline.
 
1407
         */
 
1408
        if (pvd != NULL && pvd->vdev_reopening)
 
1409
                vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
 
1410
 
1229
1411
        vd->vdev_ops->vdev_op_close(vd);
1230
1412
 
1231
1413
        vdev_cache_purge(vd);
1232
1414
 
1233
1415
        /*
1234
 
         * We record the previous state before we close it, so  that if we are
 
1416
         * We record the previous state before we close it, so that if we are
1235
1417
         * doing a reopen(), we don't generate FMA ereports if we notice that
1236
1418
         * it's still faulted.
1237
1419
         */
1244
1426
        vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1245
1427
}
1246
1428
 
 
1429
/*
 
1430
 * Reopen all interior vdevs and any unopened leaves.  We don't actually
 
1431
 * reopen leaf vdevs which had previously been opened as they might deadlock
 
1432
 * on the spa_config_lock.  Instead we only obtain the leaf's physical size.
 
1433
 * If the leaf has never been opened then open it, as usual.
 
1434
 */
1247
1435
void
1248
1436
vdev_reopen(vdev_t *vd)
1249
1437
{
1251
1439
 
1252
1440
        ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1253
1441
 
 
1442
        /* set the reopening flag unless we're taking the vdev offline */
 
1443
        vd->vdev_reopening = !vd->vdev_offline;
1254
1444
        vdev_close(vd);
1255
1445
        (void) vdev_open(vd);
1256
1446
 
1263
1453
                (void) vdev_validate_aux(vd);
1264
1454
                if (vdev_readable(vd) && vdev_writeable(vd) &&
1265
1455
                    vd->vdev_aux == &spa->spa_l2cache &&
1266
 
                    !l2arc_vdev_present(vd)) {
1267
 
                        uint64_t size = vdev_get_rsize(vd);
1268
 
                        l2arc_add_vdev(spa, vd,
1269
 
                            VDEV_LABEL_START_SIZE,
1270
 
                            size - VDEV_LABEL_START_SIZE);
1271
 
                }
 
1456
                    !l2arc_vdev_present(vd))
 
1457
                        l2arc_add_vdev(spa, vd);
1272
1458
        } else {
1273
1459
                (void) vdev_validate(vd);
1274
1460
        }
1308
1494
        return (0);
1309
1495
}
1310
1496
 
1311
 
/*
1312
 
 * The is the latter half of vdev_create().  It is distinct because it
1313
 
 * involves initiating transactions in order to do metaslab creation.
1314
 
 * For creation, we want to try to create all vdevs at once and then undo it
1315
 
 * if anything fails; this is much harder if we have pending transactions.
1316
 
 */
1317
1497
void
1318
 
vdev_init(vdev_t *vd, uint64_t txg)
 
1498
vdev_metaslab_set_size(vdev_t *vd)
1319
1499
{
1320
1500
        /*
1321
1501
         * Aim for roughly 200 metaslabs per vdev.
1322
1502
         */
1323
1503
        vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1324
1504
        vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1325
 
 
1326
 
        /*
1327
 
         * Initialize the vdev's metaslabs.  This can't fail because
1328
 
         * there's nothing to read when creating all new metaslabs.
1329
 
         */
1330
 
        VERIFY(vdev_metaslab_init(vd, txg) == 0);
1331
1505
}
1332
1506
 
1333
1507
void
1334
1508
vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1335
1509
{
1336
1510
        ASSERT(vd == vd->vdev_top);
 
1511
        ASSERT(!vd->vdev_ishole);
1337
1512
        ASSERT(ISP2(flags));
1338
1513
 
1339
1514
        if (flags & VDD_METASLAB)
1349
1524
 * DTLs.
1350
1525
 *
1351
1526
 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1352
 
 * the vdev has less than perfect replication.  There are three kinds of DTL:
 
1527
 * the vdev has less than perfect replication.  There are four kinds of DTL:
1353
1528
 *
1354
1529
 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1355
1530
 *
1443
1618
                vdev_dtl_reassess(vd->vdev_child[c], txg,
1444
1619
                    scrub_txg, scrub_done);
1445
1620
 
1446
 
        if (vd == spa->spa_root_vdev)
 
1621
        if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
1447
1622
                return;
1448
1623
 
1449
1624
        if (vd->vdev_ops->vdev_op_leaf) {
1450
1625
                mutex_enter(&vd->vdev_dtl_lock);
1451
1626
                if (scrub_txg != 0 &&
1452
1627
                    (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
1453
 
                        /* XXX should check scrub_done? */
1454
1628
                        /*
1455
1629
                         * We completed a scrub up to scrub_txg.  If we
1456
1630
                         * did it without rebooting, then the scrub dtl
1498
1672
 
1499
1673
        mutex_enter(&vd->vdev_dtl_lock);
1500
1674
        for (int t = 0; t < DTL_TYPES; t++) {
 
1675
                /* account for child's outage in parent's missing map */
 
1676
                int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
1501
1677
                if (t == DTL_SCRUB)
1502
1678
                        continue;                       /* leaf vdevs only */
1503
1679
                if (t == DTL_PARTIAL)
1510
1686
                for (int c = 0; c < vd->vdev_children; c++) {
1511
1687
                        vdev_t *cvd = vd->vdev_child[c];
1512
1688
                        mutex_enter(&cvd->vdev_dtl_lock);
1513
 
                        space_map_ref_add_map(&reftree, &cvd->vdev_dtl[t], 1);
 
1689
                        space_map_ref_add_map(&reftree, &cvd->vdev_dtl[s], 1);
1514
1690
                        mutex_exit(&cvd->vdev_dtl_lock);
1515
1691
                }
1516
1692
                space_map_ref_generate_map(&reftree, &vd->vdev_dtl[t], minref);
1533
1709
        if (smo->smo_object == 0)
1534
1710
                return (0);
1535
1711
 
 
1712
        ASSERT(!vd->vdev_ishole);
 
1713
 
1536
1714
        if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1537
1715
                return (error);
1538
1716
 
1560
1738
        dmu_buf_t *db;
1561
1739
        dmu_tx_t *tx;
1562
1740
 
 
1741
        ASSERT(!vd->vdev_ishole);
 
1742
 
1563
1743
        tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1564
1744
 
1565
1745
        if (vd->vdev_detached) {
1696
1876
        /*
1697
1877
         * If this is a top-level vdev, initialize its metaslabs.
1698
1878
         */
1699
 
        if (vd == vd->vdev_top &&
 
1879
        if (vd == vd->vdev_top && !vd->vdev_ishole &&
1700
1880
            (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1701
1881
            vdev_metaslab_init(vd, 0) != 0))
1702
1882
                vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1753
1933
}
1754
1934
 
1755
1935
void
 
1936
vdev_remove(vdev_t *vd, uint64_t txg)
 
1937
{
 
1938
        spa_t *spa = vd->vdev_spa;
 
1939
        objset_t *mos = spa->spa_meta_objset;
 
1940
        dmu_tx_t *tx;
 
1941
 
 
1942
        tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
 
1943
 
 
1944
        if (vd->vdev_dtl_smo.smo_object) {
 
1945
                ASSERT3U(vd->vdev_dtl_smo.smo_alloc, ==, 0);
 
1946
                (void) dmu_object_free(mos, vd->vdev_dtl_smo.smo_object, tx);
 
1947
                vd->vdev_dtl_smo.smo_object = 0;
 
1948
        }
 
1949
 
 
1950
        if (vd->vdev_ms != NULL) {
 
1951
                for (int m = 0; m < vd->vdev_ms_count; m++) {
 
1952
                        metaslab_t *msp = vd->vdev_ms[m];
 
1953
 
 
1954
                        if (msp == NULL || msp->ms_smo.smo_object == 0)
 
1955
                                continue;
 
1956
 
 
1957
                        ASSERT3U(msp->ms_smo.smo_alloc, ==, 0);
 
1958
                        (void) dmu_object_free(mos, msp->ms_smo.smo_object, tx);
 
1959
                        msp->ms_smo.smo_object = 0;
 
1960
                }
 
1961
        }
 
1962
 
 
1963
        if (vd->vdev_ms_array) {
 
1964
                (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
 
1965
                vd->vdev_ms_array = 0;
 
1966
                vd->vdev_ms_shift = 0;
 
1967
        }
 
1968
        dmu_tx_commit(tx);
 
1969
}
 
1970
 
 
1971
void
1756
1972
vdev_sync_done(vdev_t *vd, uint64_t txg)
1757
1973
{
1758
1974
        metaslab_t *msp;
 
1975
        boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
 
1976
 
 
1977
        ASSERT(!vd->vdev_ishole);
1759
1978
 
1760
1979
        while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1761
1980
                metaslab_sync_done(msp, txg);
 
1981
 
 
1982
        if (reassess)
 
1983
                metaslab_sync_reassess(vd->vdev_mg);
1762
1984
}
1763
1985
 
1764
1986
void
1769
1991
        metaslab_t *msp;
1770
1992
        dmu_tx_t *tx;
1771
1993
 
 
1994
        ASSERT(!vd->vdev_ishole);
 
1995
 
1772
1996
        if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1773
1997
                ASSERT(vd == vd->vdev_top);
1774
1998
                tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1779
2003
                dmu_tx_commit(tx);
1780
2004
        }
1781
2005
 
 
2006
        if (vd->vdev_removing)
 
2007
                vdev_remove(vd, txg);
 
2008
 
1782
2009
        while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1783
2010
                metaslab_sync(msp, txg);
1784
2011
                (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1801
2028
 * not be opened, and no I/O is attempted.
1802
2029
 */
1803
2030
int
1804
 
vdev_fault(spa_t *spa, uint64_t guid)
 
2031
vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
1805
2032
{
1806
2033
        vdev_t *vd;
1807
2034
 
1808
 
        spa_vdev_state_enter(spa);
 
2035
        spa_vdev_state_enter(spa, SCL_NONE);
1809
2036
 
1810
2037
        if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1811
2038
                return (spa_vdev_state_exit(spa, NULL, ENODEV));
1814
2041
                return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1815
2042
 
1816
2043
        /*
 
2044
         * We don't directly use the aux state here, but if we do a
 
2045
         * vdev_reopen(), we need this value to be present to remember why we
 
2046
         * were faulted.
 
2047
         */
 
2048
        vd->vdev_label_aux = aux;
 
2049
 
 
2050
        /*
1817
2051
         * Faulted state takes precedence over degraded.
1818
2052
         */
1819
2053
        vd->vdev_faulted = 1ULL;
1820
2054
        vd->vdev_degraded = 0ULL;
1821
 
        vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
 
2055
        vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
1822
2056
 
1823
2057
        /*
1824
2058
         * If marking the vdev as faulted cause the top-level vdev to become
1825
2059
         * unavailable, then back off and simply mark the vdev as degraded
1826
2060
         * instead.
1827
2061
         */
1828
 
        if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
 
2062
        if (vdev_is_dead(vd->vdev_top) && !vd->vdev_islog &&
 
2063
            vd->vdev_aux == NULL) {
1829
2064
                vd->vdev_degraded = 1ULL;
1830
2065
                vd->vdev_faulted = 0ULL;
1831
2066
 
1835
2070
                 */
1836
2071
                vdev_reopen(vd);
1837
2072
 
1838
 
                if (vdev_readable(vd)) {
1839
 
                        vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1840
 
                            VDEV_AUX_ERR_EXCEEDED);
1841
 
                }
 
2073
                if (vdev_readable(vd))
 
2074
                        vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
1842
2075
        }
1843
2076
 
1844
2077
        return (spa_vdev_state_exit(spa, vd, 0));
1850
2083
 * as I/O is concerned.
1851
2084
 */
1852
2085
int
1853
 
vdev_degrade(spa_t *spa, uint64_t guid)
 
2086
vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
1854
2087
{
1855
2088
        vdev_t *vd;
1856
2089
 
1857
 
        spa_vdev_state_enter(spa);
 
2090
        spa_vdev_state_enter(spa, SCL_NONE);
1858
2091
 
1859
2092
        if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1860
2093
                return (spa_vdev_state_exit(spa, NULL, ENODEV));
1871
2104
        vd->vdev_degraded = 1ULL;
1872
2105
        if (!vdev_is_dead(vd))
1873
2106
                vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1874
 
                    VDEV_AUX_ERR_EXCEEDED);
 
2107
                    aux);
1875
2108
 
1876
2109
        return (spa_vdev_state_exit(spa, vd, 0));
1877
2110
}
1885
2118
int
1886
2119
vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1887
2120
{
1888
 
        vdev_t *vd;
 
2121
        vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
1889
2122
 
1890
 
        spa_vdev_state_enter(spa);
 
2123
        spa_vdev_state_enter(spa, SCL_NONE);
1891
2124
 
1892
2125
        if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1893
2126
                return (spa_vdev_state_exit(spa, NULL, ENODEV));
1895
2128
        if (!vd->vdev_ops->vdev_op_leaf)
1896
2129
                return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1897
2130
 
 
2131
        tvd = vd->vdev_top;
1898
2132
        vd->vdev_offline = B_FALSE;
1899
2133
        vd->vdev_tmpoffline = B_FALSE;
1900
2134
        vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
1901
2135
        vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
1902
 
        vdev_reopen(vd->vdev_top);
 
2136
 
 
2137
        /* XXX - L2ARC 1.0 does not support expansion */
 
2138
        if (!vd->vdev_aux) {
 
2139
                for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
 
2140
                        pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
 
2141
        }
 
2142
 
 
2143
        vdev_reopen(tvd);
1903
2144
        vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
1904
2145
 
 
2146
        if (!vd->vdev_aux) {
 
2147
                for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
 
2148
                        pvd->vdev_expanding = B_FALSE;
 
2149
        }
 
2150
 
1905
2151
        if (newstate)
1906
2152
                *newstate = vd->vdev_state;
1907
2153
        if ((flags & ZFS_ONLINE_UNSPARE) &&
1910
2156
            vd->vdev_parent->vdev_child[0] == vd)
1911
2157
                vd->vdev_unspare = B_TRUE;
1912
2158
 
 
2159
        if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
 
2160
 
 
2161
                /* XXX - L2ARC 1.0 does not support expansion */
 
2162
                if (vd->vdev_aux)
 
2163
                        return (spa_vdev_state_exit(spa, vd, ENOTSUP));
 
2164
                spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
 
2165
        }
1913
2166
        return (spa_vdev_state_exit(spa, vd, 0));
1914
2167
}
1915
2168
 
1916
 
int
1917
 
vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
 
2169
static int
 
2170
vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
1918
2171
{
1919
2172
        vdev_t *vd, *tvd;
1920
 
        int error;
 
2173
        int error = 0;
 
2174
        uint64_t generation;
 
2175
        metaslab_group_t *mg;
1921
2176
 
1922
 
        spa_vdev_state_enter(spa);
 
2177
top:
 
2178
        spa_vdev_state_enter(spa, SCL_ALLOC);
1923
2179
 
1924
2180
        if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1925
2181
                return (spa_vdev_state_exit(spa, NULL, ENODEV));
1928
2184
                return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1929
2185
 
1930
2186
        tvd = vd->vdev_top;
 
2187
        mg = tvd->vdev_mg;
 
2188
        generation = spa->spa_config_generation + 1;
1931
2189
 
1932
2190
        /*
1933
2191
         * If the device isn't already offline, try to offline it.
1943
2201
                        return (spa_vdev_state_exit(spa, NULL, EBUSY));
1944
2202
 
1945
2203
                /*
 
2204
                 * If the top-level is a slog and it has had allocations
 
2205
                 * then proceed.  We check that the vdev's metaslab group
 
2206
                 * is not NULL since it's possible that we may have just
 
2207
                 * added this vdev but not yet initialized its metaslabs.
 
2208
                 */
 
2209
                if (tvd->vdev_islog && mg != NULL) {
 
2210
                        /*
 
2211
                         * Prevent any future allocations.
 
2212
                         */
 
2213
                        metaslab_group_passivate(mg);
 
2214
                        (void) spa_vdev_state_exit(spa, vd, 0);
 
2215
 
 
2216
                        error = spa_offline_log(spa);
 
2217
 
 
2218
                        spa_vdev_state_enter(spa, SCL_ALLOC);
 
2219
 
 
2220
                        /*
 
2221
                         * Check to see if the config has changed.
 
2222
                         */
 
2223
                        if (error || generation != spa->spa_config_generation) {
 
2224
                                metaslab_group_activate(mg);
 
2225
                                if (error)
 
2226
                                        return (spa_vdev_state_exit(spa,
 
2227
                                            vd, error));
 
2228
                                (void) spa_vdev_state_exit(spa, vd, 0);
 
2229
                                goto top;
 
2230
                        }
 
2231
                        ASSERT3U(tvd->vdev_stat.vs_alloc, ==, 0);
 
2232
                }
 
2233
 
 
2234
                /*
1946
2235
                 * Offline this device and reopen its top-level vdev.
1947
2236
                 * If the top-level vdev is a log device then just offline
1948
2237
                 * it. Otherwise, if this action results in the top-level
1949
2238
                 * vdev becoming unusable, undo it and fail the request.
1950
2239
                 */
1951
2240
                vd->vdev_offline = B_TRUE;
 
2241
                /* Explicitely call vdev_close before vdev_reopen because
 
2242
                 * otherwise the reopen flag forbids vdev_close */
 
2243
                vdev_close(vd);
1952
2244
                vdev_reopen(tvd);
1953
2245
 
1954
2246
                if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
1957
2249
                        vdev_reopen(tvd);
1958
2250
                        return (spa_vdev_state_exit(spa, NULL, EBUSY));
1959
2251
                }
 
2252
 
 
2253
                /*
 
2254
                 * Add the device back into the metaslab rotor so that
 
2255
                 * once we online the device it's open for business.
 
2256
                 */
 
2257
                if (tvd->vdev_islog && mg != NULL)
 
2258
                        metaslab_group_activate(mg);
1960
2259
        }
1961
2260
 
1962
2261
        vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
1963
2262
 
1964
 
        if (!tvd->vdev_islog || !vdev_is_dead(tvd))
1965
 
                return (spa_vdev_state_exit(spa, vd, 0));
1966
 
 
1967
 
        (void) spa_vdev_state_exit(spa, vd, 0);
1968
 
 
1969
 
        error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1970
 
            NULL, DS_FIND_CHILDREN);
1971
 
        if (error) {
1972
 
                (void) vdev_online(spa, guid, 0, NULL);
1973
 
                return (error);
1974
 
        }
1975
 
        /*
1976
 
         * If we successfully offlined the log device then we need to
1977
 
         * sync out the current txg so that the "stubby" block can be
1978
 
         * removed by zil_sync().
1979
 
         */
1980
 
        txg_wait_synced(spa->spa_dsl_pool, 0);
1981
 
        return (0);
 
2263
        return (spa_vdev_state_exit(spa, vd, 0));
 
2264
}
 
2265
 
 
2266
int
 
2267
vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
 
2268
{
 
2269
        int error;
 
2270
 
 
2271
        mutex_enter(&spa->spa_vdev_top_lock);
 
2272
        error = vdev_offline_locked(spa, guid, flags);
 
2273
        mutex_exit(&spa->spa_vdev_top_lock);
 
2274
 
 
2275
        return (error);
1982
2276
}
1983
2277
 
1984
2278
/*
2012
2306
        if (vd->vdev_faulted || vd->vdev_degraded ||
2013
2307
            !vdev_readable(vd) || !vdev_writeable(vd)) {
2014
2308
 
 
2309
                /*
 
2310
                 * When reopening in reponse to a clear event, it may be due to
 
2311
                 * a fmadm repair request.  In this case, if the device is
 
2312
                 * still broken, we want to still post the ereport again.
 
2313
                 */
 
2314
                vd->vdev_forcefault = B_TRUE;
 
2315
 
2015
2316
                vd->vdev_faulted = vd->vdev_degraded = 0;
2016
2317
                vd->vdev_cant_read = B_FALSE;
2017
2318
                vd->vdev_cant_write = B_FALSE;
2018
2319
 
2019
2320
                vdev_reopen(vd);
2020
2321
 
 
2322
                vd->vdev_forcefault = B_FALSE;
 
2323
 
2021
2324
                if (vd != rvd)
2022
2325
                        vdev_state_dirty(vd->vdev_top);
2023
2326
 
2026
2329
 
2027
2330
                spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
2028
2331
        }
 
2332
 
 
2333
        /*
 
2334
         * When clearing a FMA-diagnosed fault, we always want to
 
2335
         * unspare the device, as we assume that the original spare was
 
2336
         * done in response to the FMA fault.
 
2337
         */
 
2338
        if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
 
2339
            vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
 
2340
            vd->vdev_parent->vdev_child[0] == vd)
 
2341
                vd->vdev_unspare = B_TRUE;
2029
2342
}
2030
2343
 
2031
2344
boolean_t
2032
2345
vdev_is_dead(vdev_t *vd)
2033
2346
{
2034
 
        return (vd->vdev_state < VDEV_STATE_DEGRADED);
 
2347
        /*
 
2348
         * Holes and missing devices are always considered "dead".
 
2349
         * This simplifies the code since we don't have to check for
 
2350
         * these types of devices in the various code paths.
 
2351
         * Instead we rely on the fact that we skip over dead devices
 
2352
         * before issuing I/O to them.
 
2353
         */
 
2354
        return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
 
2355
            vd->vdev_ops == &vdev_missing_ops);
2035
2356
}
2036
2357
 
2037
2358
boolean_t
2060
2381
         * we're asking two separate questions about it.
2061
2382
         */
2062
2383
        return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
2063
 
            !vd->vdev_cant_write);
 
2384
            !vd->vdev_cant_write && !vd->vdev_ishole && !vd->vdev_removing);
2064
2385
}
2065
2386
 
2066
2387
boolean_t
2093
2414
        vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
2094
2415
        vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2095
2416
        vs->vs_state = vd->vdev_state;
2096
 
        vs->vs_rsize = vdev_get_rsize(vd);
 
2417
        vs->vs_rsize = vdev_get_min_asize(vd);
 
2418
        if (vd->vdev_ops->vdev_op_leaf)
 
2419
                vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2097
2420
        mutex_exit(&vd->vdev_stat_lock);
2098
2421
 
2099
2422
        /*
2196
2519
            !(zio->io_flags & ZIO_FLAG_IO_RETRY))
2197
2520
                return;
2198
2521
 
 
2522
        /*
 
2523
         * Intent logs writes won't propagate their error to the root
 
2524
         * I/O so don't mark these types of failures as pool-level
 
2525
         * errors.
 
2526
         */
 
2527
        if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
 
2528
                return;
 
2529
 
2199
2530
        mutex_enter(&vd->vdev_stat_lock);
2200
2531
        if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2201
2532
                if (zio->io_error == ECKSUM)
2209
2540
 
2210
2541
        if (type == ZIO_TYPE_WRITE && txg != 0 &&
2211
2542
            (!(flags & ZIO_FLAG_IO_REPAIR) ||
2212
 
            (flags & ZIO_FLAG_SCRUB_THREAD))) {
 
2543
            (flags & ZIO_FLAG_SCRUB_THREAD) ||
 
2544
            spa->spa_claiming)) {
2213
2545
                /*
2214
 
                 * This is either a normal write (not a repair), or it's a
2215
 
                 * repair induced by the scrub thread.  In the normal case,
2216
 
                 * we commit the DTL change in the same txg as the block
2217
 
                 * was born.  In the scrub-induced repair case, we know that
2218
 
                 * scrubs run in first-pass syncing context, so we commit
2219
 
                 * the DTL change in spa->spa_syncing_txg.
 
2546
                 * This is either a normal write (not a repair), or it's
 
2547
                 * a repair induced by the scrub thread, or it's a repair
 
2548
                 * made by zil_claim() during spa_load() in the first txg.
 
2549
                 * In the normal case, we commit the DTL change in the same
 
2550
                 * txg as the block was born.  In the scrub-induced repair
 
2551
                 * case, we know that scrubs run in first-pass syncing context,
 
2552
                 * so we commit the DTL change in spa_syncing_txg(spa).
 
2553
                 * In the zil_claim() case, we commit in spa_first_txg(spa).
2220
2554
                 *
2221
2555
                 * We currently do not make DTL entries for failed spontaneous
2222
2556
                 * self-healing writes triggered by normal (non-scrubbing)
2229
2563
                                ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2230
2564
                                ASSERT(spa_sync_pass(spa) == 1);
2231
2565
                                vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2232
 
                                commit_txg = spa->spa_syncing_txg;
 
2566
                                commit_txg = spa_syncing_txg(spa);
 
2567
                        } else if (spa->spa_claiming) {
 
2568
                                ASSERT(flags & ZIO_FLAG_IO_REPAIR);
 
2569
                                commit_txg = spa_first_txg(spa);
2233
2570
                        }
2234
 
                        ASSERT(commit_txg >= spa->spa_syncing_txg);
 
2571
                        ASSERT(commit_txg >= spa_syncing_txg(spa));
2235
2572
                        if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2236
2573
                                return;
2237
2574
                        for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2246
2583
void
2247
2584
vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2248
2585
{
2249
 
        int c;
2250
2586
        vdev_stat_t *vs = &vd->vdev_stat;
2251
2587
 
2252
 
        for (c = 0; c < vd->vdev_children; c++)
 
2588
        for (int c = 0; c < vd->vdev_children; c++)
2253
2589
                vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2254
2590
 
2255
2591
        mutex_enter(&vd->vdev_stat_lock);
2274
2610
}
2275
2611
 
2276
2612
/*
2277
 
 * Update the in-core space usage stats for this vdev and the root vdev.
 
2613
 * Update the in-core space usage stats for this vdev, its metaslab class,
 
2614
 * and the root vdev.
2278
2615
 */
2279
2616
void
2280
 
vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
2281
 
    boolean_t update_root)
 
2617
vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
 
2618
    int64_t space_delta)
2282
2619
{
2283
2620
        int64_t dspace_delta = space_delta;
2284
2621
        spa_t *spa = vd->vdev_spa;
2285
2622
        vdev_t *rvd = spa->spa_root_vdev;
 
2623
        metaslab_group_t *mg = vd->vdev_mg;
 
2624
        metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2286
2625
 
2287
2626
        ASSERT(vd == vd->vdev_top);
2288
2627
 
2298
2637
            vd->vdev_deflate_ratio;
2299
2638
 
2300
2639
        mutex_enter(&vd->vdev_stat_lock);
 
2640
        vd->vdev_stat.vs_alloc += alloc_delta;
2301
2641
        vd->vdev_stat.vs_space += space_delta;
2302
 
        vd->vdev_stat.vs_alloc += alloc_delta;
2303
2642
        vd->vdev_stat.vs_dspace += dspace_delta;
2304
2643
        mutex_exit(&vd->vdev_stat_lock);
2305
2644
 
2306
 
        if (update_root) {
2307
 
                ASSERT(rvd == vd->vdev_parent);
2308
 
                ASSERT(vd->vdev_ms_count != 0);
2309
 
 
2310
 
                /*
2311
 
                 * Don't count non-normal (e.g. intent log) space as part of
2312
 
                 * the pool's capacity.
2313
 
                 */
2314
 
                if (vd->vdev_mg->mg_class != spa->spa_normal_class)
2315
 
                        return;
2316
 
 
 
2645
        if (mc == spa_normal_class(spa)) {
2317
2646
                mutex_enter(&rvd->vdev_stat_lock);
 
2647
                rvd->vdev_stat.vs_alloc += alloc_delta;
2318
2648
                rvd->vdev_stat.vs_space += space_delta;
2319
 
                rvd->vdev_stat.vs_alloc += alloc_delta;
2320
2649
                rvd->vdev_stat.vs_dspace += dspace_delta;
2321
2650
                mutex_exit(&rvd->vdev_stat_lock);
2322
2651
        }
 
2652
 
 
2653
        if (mc != NULL) {
 
2654
                ASSERT(rvd == vd->vdev_parent);
 
2655
                ASSERT(vd->vdev_ms_count != 0);
 
2656
 
 
2657
                metaslab_class_space_update(mc,
 
2658
                    alloc_delta, defer_delta, space_delta, dspace_delta);
 
2659
        }
2323
2660
}
2324
2661
 
2325
2662
/*
2392
2729
        } else {
2393
2730
                ASSERT(vd == vd->vdev_top);
2394
2731
 
2395
 
                if (!list_link_active(&vd->vdev_config_dirty_node))
 
2732
                if (!list_link_active(&vd->vdev_config_dirty_node) &&
 
2733
                    !vd->vdev_ishole)
2396
2734
                        list_insert_head(&spa->spa_config_dirty_list, vd);
2397
2735
        }
2398
2736
}
2433
2771
            (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2434
2772
            spa_config_held(spa, SCL_STATE, RW_READER)));
2435
2773
 
2436
 
        if (!list_link_active(&vd->vdev_state_dirty_node))
 
2774
        if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
2437
2775
                list_insert_head(&spa->spa_state_dirty_list, vd);
2438
2776
}
2439
2777
 
2460
2798
        vdev_t *rvd = spa->spa_root_vdev;
2461
2799
        int degraded = 0, faulted = 0;
2462
2800
        int corrupted = 0;
2463
 
        int c;
2464
2801
        vdev_t *child;
2465
2802
 
2466
2803
        if (vd->vdev_children > 0) {
2467
 
                for (c = 0; c < vd->vdev_children; c++) {
 
2804
                for (int c = 0; c < vd->vdev_children; c++) {
2468
2805
                        child = vd->vdev_child[c];
2469
2806
 
 
2807
                        /*
 
2808
                         * Don't factor holes into the decision.
 
2809
                         */
 
2810
                        if (child->vdev_ishole)
 
2811
                                continue;
 
2812
 
2470
2813
                        if (!vdev_readable(child) ||
2471
2814
                            (!vdev_writeable(child) && spa_writeable(spa))) {
2472
2815
                                /*
2504
2847
                vdev_propagate_state(vd->vdev_parent);
2505
2848
}
2506
2849
 
 
2850
static char old_name[MAXNAMELEN];
 
2851
static time_t old_time;
 
2852
static vdev_state_t old_state;
 
2853
 
 
2854
/* This zpool_state_to_name is a copy of the one from libzfs.
 
2855
 * taken from user land, in zfs-fuse we don't have all these problems to
 
2856
 * communicate between the 2... ! */
 
2857
static char *
 
2858
zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
 
2859
{
 
2860
        switch (state) {
 
2861
        case VDEV_STATE_CLOSED:
 
2862
        case VDEV_STATE_OFFLINE:
 
2863
                return (gettext("OFFLINE"));
 
2864
        case VDEV_STATE_REMOVED:
 
2865
                return (gettext("REMOVED"));
 
2866
        case VDEV_STATE_CANT_OPEN:
 
2867
                if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
 
2868
                        return (gettext("FAULTED"));
 
2869
                else if (aux == VDEV_AUX_SPLIT_POOL)
 
2870
                        return (gettext("SPLIT"));
 
2871
                else
 
2872
                        return (gettext("UNAVAIL"));
 
2873
        case VDEV_STATE_FAULTED:
 
2874
                return (gettext("FAULTED"));
 
2875
        case VDEV_STATE_DEGRADED:
 
2876
                return (gettext("DEGRADED"));
 
2877
        case VDEV_STATE_HEALTHY:
 
2878
                return (gettext("ONLINE"));
 
2879
        }
 
2880
 
 
2881
        return (gettext("UNKNOWN"));
 
2882
}
 
2883
 
 
2884
static int vdev_check_children(vdev_t *vd) {
 
2885
    /* Check 1st that it's not just because of an offline vdev :
 
2886
     * browse the children looking for 1 which in a state !=
 
2887
     * online && offline, check recursively */
 
2888
    int n;
 
2889
    int found = 0;
 
2890
    for (n=0; n<vd->vdev_children; n++) {
 
2891
        if (vd->vdev_child[n]->vdev_children) {
 
2892
            found = vdev_check_children(vd->vdev_child[n]);
 
2893
            if (found)
 
2894
                break;
 
2895
        } else {
 
2896
            vdev_state_t st = vd->vdev_child[n]->vdev_state;
 
2897
            if (st != VDEV_STATE_HEALTHY && st != VDEV_STATE_OFFLINE) {
 
2898
                found = 1;
 
2899
                break;
 
2900
            }
 
2901
        }
 
2902
    }
 
2903
    return found;
 
2904
}
 
2905
 
2507
2906
/*
2508
2907
 * Set a vdev's state.  If this is during an open, we don't update the parent
2509
2908
 * state, because we're in the process of opening children depth-first.
2515
2914
void
2516
2915
vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2517
2916
{
2518
 
        uint64_t save_state;
 
2917
        vdev_state_t save_state;
2519
2918
        spa_t *spa = vd->vdev_spa;
2520
2919
 
2521
2920
        if (state == vd->vdev_state) {
2539
2938
        if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
2540
2939
                vd->vdev_ops->vdev_op_close(vd);
2541
2940
 
 
2941
        /*
 
2942
         * If we have brought this vdev back into service, we need
 
2943
         * to notify fmd so that it can gracefully repair any outstanding
 
2944
         * cases due to a missing device.  We do this in all cases, even those
 
2945
         * that probably don't correlate to a repaired fault.  This is sure to
 
2946
         * catch all cases, and we let the zfs-retire agent sort it out.  If
 
2947
         * this is a transient state it's OK, as the retire agent will
 
2948
         * double-check the state of the vdev before repairing it.
 
2949
         */
 
2950
        if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
 
2951
            vd->vdev_prevstate != state)
 
2952
                zfs_post_state_change(spa, vd);
 
2953
 
2542
2954
        if (vd->vdev_removed &&
2543
2955
            state == VDEV_STATE_CANT_OPEN &&
2544
2956
            (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
2554
2966
                vd->vdev_state = VDEV_STATE_REMOVED;
2555
2967
                vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2556
2968
        } else if (state == VDEV_STATE_REMOVED) {
2557
 
                /*
2558
 
                 * Indicate to the ZFS DE that this device has been removed, and
2559
 
                 * any recent errors should be ignored.
2560
 
                 */
2561
 
                zfs_post_remove(spa, vd);
2562
2969
                vd->vdev_removed = B_TRUE;
2563
2970
        } else if (state == VDEV_STATE_CANT_OPEN) {
2564
2971
                /*
2567
2974
                 * begin with.  Failure to open such a device is not considered
2568
2975
                 * an error.
2569
2976
                 */
2570
 
                if (spa->spa_load_state == SPA_LOAD_IMPORT &&
 
2977
                if (spa_load_state(spa) == SPA_LOAD_IMPORT &&
2571
2978
                    vd->vdev_ops->vdev_op_leaf)
2572
2979
                        vd->vdev_not_present = 1;
2573
2980
 
2628
3035
 
2629
3036
        if (!isopen && vd->vdev_parent)
2630
3037
                vdev_propagate_state(vd->vdev_parent);
 
3038
        if ((state == VDEV_STATE_HEALTHY && save_state == VDEV_STATE_DEGRADED)
 
3039
                || (state == VDEV_STATE_DEGRADED &&
 
3040
                    save_state == VDEV_STATE_HEALTHY)) {
 
3041
            char cmd[2048];
 
3042
            spa_t *top;
 
3043
            if (vd->vdev_top)
 
3044
                top = vd->vdev_top->vdev_spa;
 
3045
            else
 
3046
                top = vd->vdev_spa;
 
3047
            time_t mytime = time(NULL);
 
3048
            if (mytime - old_time < 30 && !strcmp(top->spa_name,old_name) &&
 
3049
                    old_state == state) {
 
3050
                /* Already got the same alert for this pool less than 30s ago */
 
3051
                return;
 
3052
            }
 
3053
            if (state == VDEV_STATE_DEGRADED) {
 
3054
                int found = vdev_check_children(vd);
 
3055
                if (!found)
 
3056
                    return; // nothing of interest here
 
3057
            }
 
3058
            if (strcasecmp(top->spa_name,"$import")) {
 
3059
                snprintf(cmd,2048,"/etc/zfs/zfs_pool_alert %s &",top->spa_name);
 
3060
                syslog(LOG_WARNING,"running zfs_pool_alert for pool %s, status %s prev status %s",top->spa_name,zpool_state_to_name(state,save_state),
 
3061
                        zpool_state_to_name(save_state,state));
 
3062
                int ret = system(cmd);
 
3063
                if (ret == -1) 
 
3064
                    syslog(LOG_WARNING,"fork failed for zfs_pool_alert");
 
3065
                /* We won't get the return code of the actual command since
 
3066
                 * it's executed in the background. So if the fork worked
 
3067
                 * then this is the job of the zfs_pool_alert to track
 
3068
                 * error conditions */
 
3069
            }
 
3070
            old_time = mytime;
 
3071
            strcpy(old_name,top->spa_name);
 
3072
            old_state = state;
 
3073
        }
2631
3074
}
2632
3075
 
2633
3076
/*
2639
3082
boolean_t
2640
3083
vdev_is_bootable(vdev_t *vd)
2641
3084
{
2642
 
        int c;
2643
 
 
2644
3085
        if (!vd->vdev_ops->vdev_op_leaf) {
2645
3086
                char *vdev_type = vd->vdev_ops->vdev_op_type;
2646
3087
 
2655
3096
                return (B_FALSE);
2656
3097
        }
2657
3098
 
2658
 
        for (c = 0; c < vd->vdev_children; c++) {
 
3099
        for (int c = 0; c < vd->vdev_children; c++) {
2659
3100
                if (!vdev_is_bootable(vd->vdev_child[c]))
2660
3101
                        return (B_FALSE);
2661
3102
        }
2662
3103
        return (B_TRUE);
2663
3104
}
2664
3105
 
 
3106
/*
 
3107
 * Load the state from the original vdev tree (ovd) which
 
3108
 * we've retrieved from the MOS config object. If the original
 
3109
 * vdev was offline then we transfer that state to the device
 
3110
 * in the current vdev tree (nvd).
 
3111
 */
2665
3112
void
2666
 
vdev_load_log_state(vdev_t *vd, nvlist_t *nv)
 
3113
vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
2667
3114
{
2668
 
        uint_t c, children;
2669
 
        nvlist_t **child;
2670
 
        uint64_t val;
2671
 
        spa_t *spa = vd->vdev_spa;
2672
 
 
2673
 
        if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2674
 
            &child, &children) == 0) {
2675
 
                for (c = 0; c < children; c++)
2676
 
                        vdev_load_log_state(vd->vdev_child[c], child[c]);
2677
 
        }
2678
 
 
2679
 
        if (vd->vdev_ops->vdev_op_leaf && nvlist_lookup_uint64(nv,
2680
 
            ZPOOL_CONFIG_OFFLINE, &val) == 0 && val) {
2681
 
 
 
3115
        spa_t *spa = nvd->vdev_spa;
 
3116
 
 
3117
        ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
 
3118
        ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
 
3119
 
 
3120
        for (int c = 0; c < nvd->vdev_children; c++)
 
3121
                vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
 
3122
 
 
3123
        if (nvd->vdev_ops->vdev_op_leaf && ovd->vdev_offline) {
2682
3124
                /*
2683
3125
                 * It would be nice to call vdev_offline()
2684
3126
                 * directly but the pool isn't fully loaded and
2685
3127
                 * the txg threads have not been started yet.
2686
3128
                 */
2687
 
                spa_config_enter(spa, SCL_STATE_ALL, FTAG, RW_WRITER);
2688
 
                vd->vdev_offline = val;
2689
 
                vdev_reopen(vd->vdev_top);
2690
 
                spa_config_exit(spa, SCL_STATE_ALL, FTAG);
2691
 
        }
 
3129
                nvd->vdev_offline = ovd->vdev_offline;
 
3130
                vdev_reopen(nvd->vdev_top);
 
3131
        }
 
3132
}
 
3133
 
 
3134
/*
 
3135
 * Expand a vdev if possible.
 
3136
 */
 
3137
void
 
3138
vdev_expand(vdev_t *vd, uint64_t txg)
 
3139
{
 
3140
        ASSERT(vd->vdev_top == vd);
 
3141
        ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
 
3142
 
 
3143
        if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
 
3144
                VERIFY(vdev_metaslab_init(vd, txg) == 0);
 
3145
                vdev_config_dirty(vd);
 
3146
        }
 
3147
}
 
3148
 
 
3149
/*
 
3150
 * Split a vdev.
 
3151
 */
 
3152
void
 
3153
vdev_split(vdev_t *vd)
 
3154
{
 
3155
        vdev_t *cvd, *pvd = vd->vdev_parent;
 
3156
 
 
3157
        vdev_remove_child(pvd, vd);
 
3158
        vdev_compact_children(pvd);
 
3159
 
 
3160
        cvd = pvd->vdev_child[0];
 
3161
        if (pvd->vdev_children == 1) {
 
3162
                vdev_remove_parent(cvd);
 
3163
                cvd->vdev_splitting = B_TRUE;
 
3164
        }
 
3165
        vdev_propagate_state(cvd);
2692
3166
}