xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_root.c (revision 5329:33cb98223b2d)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*5329Sgw25295  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/vdev_impl.h>
31789Sahrens #include <sys/zio.h>
32789Sahrens #include <sys/fs/zfs.h>
33789Sahrens 
34789Sahrens /*
35789Sahrens  * Virtual device vector for the pool's root vdev.
36789Sahrens  */
37789Sahrens 
381775Sbillm /*
391775Sbillm  * We should be able to tolerate one failure with absolutely no damage
401775Sbillm  * to our metadata.  Two failures will take out space maps, a bunch of
411775Sbillm  * indirect block trees, meta dnodes, dnodes, etc.  Probably not a happy
421775Sbillm  * place to live.  When we get smarter, we can liberalize this policy.
431775Sbillm  * e.g. If we haven't lost two consecutive top-level vdevs, then we are
441775Sbillm  * probably fine.  Adding bean counters during alloc/free can make this
451775Sbillm  * future guesswork more accurate.
461775Sbillm  */
471775Sbillm static int
481775Sbillm too_many_errors(vdev_t *vd, int numerrors)
491775Sbillm {
50*5329Sgw25295 	ASSERT3U(numerrors, <=, vd->vdev_children);
51*5329Sgw25295 	return (numerrors == vd->vdev_children);
521775Sbillm }
531775Sbillm 
54789Sahrens static int
55789Sahrens vdev_root_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
56789Sahrens {
57*5329Sgw25295 	int c;
58789Sahrens 	int lasterror = 0;
591775Sbillm 	int numerrors = 0;
60789Sahrens 
61789Sahrens 	if (vd->vdev_children == 0) {
62789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
63789Sahrens 		return (EINVAL);
64789Sahrens 	}
65789Sahrens 
66789Sahrens 	for (c = 0; c < vd->vdev_children; c++) {
67*5329Sgw25295 		vdev_t *cvd = vd->vdev_child[c];
68*5329Sgw25295 		int error;
69789Sahrens 
70789Sahrens 		if ((error = vdev_open(cvd)) != 0) {
71789Sahrens 			lasterror = error;
721775Sbillm 			numerrors++;
73789Sahrens 			continue;
74789Sahrens 		}
75789Sahrens 	}
76789Sahrens 
77*5329Sgw25295 	if (numerrors > 0) {
78*5329Sgw25295 		if (!too_many_errors(vd, numerrors)) {
79*5329Sgw25295 			/* XXX - should not be explicitly setting this state */
80*5329Sgw25295 			vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED,
81*5329Sgw25295 			    VDEV_AUX_NO_REPLICAS);
82*5329Sgw25295 		} else {
83*5329Sgw25295 			vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
84*5329Sgw25295 			return (lasterror);
85*5329Sgw25295 		}
861775Sbillm 	}
87789Sahrens 
881732Sbonwick 	*asize = 0;
891732Sbonwick 	*ashift = 0;
901732Sbonwick 
911775Sbillm 	return (0);
92789Sahrens }
93789Sahrens 
94789Sahrens static void
95789Sahrens vdev_root_close(vdev_t *vd)
96789Sahrens {
97789Sahrens 	int c;
98789Sahrens 
99789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
100789Sahrens 		vdev_close(vd->vdev_child[c]);
101789Sahrens }
102789Sahrens 
103789Sahrens static void
104789Sahrens vdev_root_state_change(vdev_t *vd, int faulted, int degraded)
105789Sahrens {
106*5329Sgw25295 	if (faulted) {
107*5329Sgw25295 		if (too_many_errors(vd, faulted))
108*5329Sgw25295 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
109*5329Sgw25295 			    VDEV_AUX_NO_REPLICAS);
110*5329Sgw25295 		else
111*5329Sgw25295 			vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED,
112*5329Sgw25295 			    VDEV_AUX_NO_REPLICAS);
113*5329Sgw25295 	} else if (degraded) {
1141544Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
115*5329Sgw25295 	} else {
1161544Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
117*5329Sgw25295 	}
118789Sahrens }
119789Sahrens 
120789Sahrens vdev_ops_t vdev_root_ops = {
121789Sahrens 	vdev_root_open,
122789Sahrens 	vdev_root_close,
123*5329Sgw25295 	NULL,
124789Sahrens 	vdev_default_asize,
125789Sahrens 	NULL,			/* io_start - not applicable to the root */
126789Sahrens 	NULL,			/* io_done - not applicable to the root */
127789Sahrens 	vdev_root_state_change,
128789Sahrens 	VDEV_TYPE_ROOT,		/* name of this vdev type */
129789Sahrens 	B_FALSE			/* not a leaf vdev */
130789Sahrens };
131