xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_root.c (revision 6f7938128a2c5e23f4b970ea101137eadd1470a1)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22dcba9f3fSGeorge Wilson  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
264263d13fSGeorge Wilson /*
275cabbc6bSPrashanth Sreenivasa  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
284263d13fSGeorge Wilson  */
294263d13fSGeorge Wilson 
30fa9e4066Sahrens #include <sys/zfs_context.h>
31fa9e4066Sahrens #include <sys/spa.h>
32fa9e4066Sahrens #include <sys/vdev_impl.h>
33fa9e4066Sahrens #include <sys/zio.h>
34fa9e4066Sahrens #include <sys/fs/zfs.h>
35fa9e4066Sahrens 
36fa9e4066Sahrens /*
37fa9e4066Sahrens  * Virtual device vector for the pool's root vdev.
38fa9e4066Sahrens  */
39fa9e4066Sahrens 
40*6f793812SPavel Zakharov static uint64_t
41*6f793812SPavel Zakharov vdev_root_core_tvds(vdev_t *vd)
42*6f793812SPavel Zakharov {
43*6f793812SPavel Zakharov 	uint64_t tvds = 0;
44*6f793812SPavel Zakharov 
45*6f793812SPavel Zakharov 	for (uint64_t c = 0; c < vd->vdev_children; c++) {
46*6f793812SPavel Zakharov 		vdev_t *cvd = vd->vdev_child[c];
47*6f793812SPavel Zakharov 
48*6f793812SPavel Zakharov 		if (!cvd->vdev_ishole && !cvd->vdev_islog &&
49*6f793812SPavel Zakharov 		    cvd->vdev_ops != &vdev_indirect_ops) {
50*6f793812SPavel Zakharov 			tvds++;
51*6f793812SPavel Zakharov 		}
52*6f793812SPavel Zakharov 	}
53*6f793812SPavel Zakharov 
54*6f793812SPavel Zakharov 	return (tvds);
55*6f793812SPavel Zakharov }
56*6f793812SPavel Zakharov 
5744cd46caSbillm /*
5844cd46caSbillm  * We should be able to tolerate one failure with absolutely no damage
5944cd46caSbillm  * to our metadata.  Two failures will take out space maps, a bunch of
6044cd46caSbillm  * indirect block trees, meta dnodes, dnodes, etc.  Probably not a happy
6144cd46caSbillm  * place to live.  When we get smarter, we can liberalize this policy.
6244cd46caSbillm  * e.g. If we haven't lost two consecutive top-level vdevs, then we are
6344cd46caSbillm  * probably fine.  Adding bean counters during alloc/free can make this
6444cd46caSbillm  * future guesswork more accurate.
6544cd46caSbillm  */
66*6f793812SPavel Zakharov static boolean_t
67*6f793812SPavel Zakharov too_many_errors(vdev_t *vd, uint64_t numerrors)
6844cd46caSbillm {
69*6f793812SPavel Zakharov 	uint64_t tvds;
70*6f793812SPavel Zakharov 
71*6f793812SPavel Zakharov 	if (numerrors == 0)
72*6f793812SPavel Zakharov 		return (B_FALSE);
73*6f793812SPavel Zakharov 
74*6f793812SPavel Zakharov 	tvds = vdev_root_core_tvds(vd);
75*6f793812SPavel Zakharov 	ASSERT3U(numerrors, <=, tvds);
76*6f793812SPavel Zakharov 
77*6f793812SPavel Zakharov 	if (numerrors == tvds)
78*6f793812SPavel Zakharov 		return (B_TRUE);
79*6f793812SPavel Zakharov 
80*6f793812SPavel Zakharov 	return (numerrors > spa_missing_tvds_allowed(vd->vdev_spa));
8144cd46caSbillm }
8244cd46caSbillm 
83fa9e4066Sahrens static int
844263d13fSGeorge Wilson vdev_root_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
854263d13fSGeorge Wilson     uint64_t *ashift)
86fa9e4066Sahrens {
87*6f793812SPavel Zakharov 	spa_t *spa = vd->vdev_spa;
88fa9e4066Sahrens 	int lasterror = 0;
8944cd46caSbillm 	int numerrors = 0;
90fa9e4066Sahrens 
91fa9e4066Sahrens 	if (vd->vdev_children == 0) {
92fa9e4066Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
93be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
94fa9e4066Sahrens 	}
95fa9e4066Sahrens 
96f64c0e34SEric Taylor 	vdev_open_children(vd);
97f64c0e34SEric Taylor 
98f64c0e34SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++) {
990a4e9518Sgw 		vdev_t *cvd = vd->vdev_child[c];
100fa9e4066Sahrens 
101f64c0e34SEric Taylor 		if (cvd->vdev_open_error && !cvd->vdev_islog) {
102f64c0e34SEric Taylor 			lasterror = cvd->vdev_open_error;
10344cd46caSbillm 			numerrors++;
104fa9e4066Sahrens 		}
105fa9e4066Sahrens 	}
106fa9e4066Sahrens 
107*6f793812SPavel Zakharov 	if (spa_load_state(spa) != SPA_LOAD_NONE)
108*6f793812SPavel Zakharov 		spa_set_missing_tvds(spa, numerrors);
109*6f793812SPavel Zakharov 
11051ece835Seschrock 	if (too_many_errors(vd, numerrors)) {
11151ece835Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
11251ece835Seschrock 		return (lasterror);
11344cd46caSbillm 	}
114fa9e4066Sahrens 
115ecc2d604Sbonwick 	*asize = 0;
1164263d13fSGeorge Wilson 	*max_asize = 0;
117ecc2d604Sbonwick 	*ashift = 0;
118ecc2d604Sbonwick 
11944cd46caSbillm 	return (0);
120fa9e4066Sahrens }
121fa9e4066Sahrens 
122fa9e4066Sahrens static void
123fa9e4066Sahrens vdev_root_close(vdev_t *vd)
124fa9e4066Sahrens {
125f64c0e34SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
126fa9e4066Sahrens 		vdev_close(vd->vdev_child[c]);
127fa9e4066Sahrens }
128fa9e4066Sahrens 
129fa9e4066Sahrens static void
130fa9e4066Sahrens vdev_root_state_change(vdev_t *vd, int faulted, int degraded)
131fa9e4066Sahrens {
13251ece835Seschrock 	if (too_many_errors(vd, faulted)) {
13351ece835Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13451ece835Seschrock 		    VDEV_AUX_NO_REPLICAS);
135*6f793812SPavel Zakharov 	} else if (degraded || faulted) {
136ea8dc4b6Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
1370a4e9518Sgw 	} else {
138ea8dc4b6Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
1390a4e9518Sgw 	}
140fa9e4066Sahrens }
141fa9e4066Sahrens 
142fa9e4066Sahrens vdev_ops_t vdev_root_ops = {
143fa9e4066Sahrens 	vdev_root_open,
144fa9e4066Sahrens 	vdev_root_close,
145fa9e4066Sahrens 	vdev_default_asize,
146fa9e4066Sahrens 	NULL,			/* io_start - not applicable to the root */
147fa9e4066Sahrens 	NULL,			/* io_done - not applicable to the root */
148fa9e4066Sahrens 	vdev_root_state_change,
149dcba9f3fSGeorge Wilson 	NULL,
150dcba9f3fSGeorge Wilson 	NULL,
1515cabbc6bSPrashanth Sreenivasa 	NULL,
152fa9e4066Sahrens 	VDEV_TYPE_ROOT,		/* name of this vdev type */
153fa9e4066Sahrens 	B_FALSE			/* not a leaf vdev */
154fa9e4066Sahrens };
155