Add -p switch to "zpool get"
[zfs.git] / module / zfs / vdev_root.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  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 /*
27  * Copyright (c) 2012 by Delphix. All rights reserved.
28  */
29
30 #include <sys/zfs_context.h>
31 #include <sys/spa.h>
32 #include <sys/vdev_impl.h>
33 #include <sys/zio.h>
34 #include <sys/fs/zfs.h>
35
36 /*
37  * Virtual device vector for the pool's root vdev.
38  */
39
40 /*
41  * We should be able to tolerate one failure with absolutely no damage
42  * to our metadata.  Two failures will take out space maps, a bunch of
43  * indirect block trees, meta dnodes, dnodes, etc.  Probably not a happy
44  * place to live.  When we get smarter, we can liberalize this policy.
45  * e.g. If we haven't lost two consecutive top-level vdevs, then we are
46  * probably fine.  Adding bean counters during alloc/free can make this
47  * future guesswork more accurate.
48  */
49 static int
50 too_many_errors(vdev_t *vd, int numerrors)
51 {
52         ASSERT3U(numerrors, <=, vd->vdev_children);
53         return (numerrors > 0);
54 }
55
56 static int
57 vdev_root_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
58     uint64_t *ashift)
59 {
60         int lasterror = 0;
61         int numerrors = 0;
62         int c;
63
64         if (vd->vdev_children == 0) {
65                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
66                 return (EINVAL);
67         }
68
69         vdev_open_children(vd);
70
71         for (c = 0; c < vd->vdev_children; c++) {
72                 vdev_t *cvd = vd->vdev_child[c];
73
74                 if (cvd->vdev_open_error && !cvd->vdev_islog) {
75                         lasterror = cvd->vdev_open_error;
76                         numerrors++;
77                 }
78         }
79
80         if (too_many_errors(vd, numerrors)) {
81                 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
82                 return (lasterror);
83         }
84
85         *asize = 0;
86         *max_asize = 0;
87         *ashift = 0;
88
89         return (0);
90 }
91
92 static void
93 vdev_root_close(vdev_t *vd)
94 {
95         int c;
96
97         for (c = 0; c < vd->vdev_children; c++)
98                 vdev_close(vd->vdev_child[c]);
99 }
100
101 static void
102 vdev_root_state_change(vdev_t *vd, int faulted, int degraded)
103 {
104         if (too_many_errors(vd, faulted)) {
105                 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
106                     VDEV_AUX_NO_REPLICAS);
107         } else if (degraded) {
108                 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
109         } else {
110                 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
111         }
112 }
113
114 vdev_ops_t vdev_root_ops = {
115         vdev_root_open,
116         vdev_root_close,
117         vdev_default_asize,
118         NULL,                   /* io_start - not applicable to the root */
119         NULL,                   /* io_done - not applicable to the root */
120         vdev_root_state_change,
121         NULL,
122         NULL,
123         VDEV_TYPE_ROOT,         /* name of this vdev type */
124         B_FALSE                 /* not a leaf vdev */
125 };