xref: /titanic_44/usr/src/uts/common/fs/zfs/vdev_root.c (revision be6fd75a69ae679453d9cda5bff3326111e6d1ca)
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 /*
27*be6fd75aSMatthew Ahrens  * Copyright (c) 2013 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 
4044cd46caSbillm /*
4144cd46caSbillm  * We should be able to tolerate one failure with absolutely no damage
4244cd46caSbillm  * to our metadata.  Two failures will take out space maps, a bunch of
4344cd46caSbillm  * indirect block trees, meta dnodes, dnodes, etc.  Probably not a happy
4444cd46caSbillm  * place to live.  When we get smarter, we can liberalize this policy.
4544cd46caSbillm  * e.g. If we haven't lost two consecutive top-level vdevs, then we are
4644cd46caSbillm  * probably fine.  Adding bean counters during alloc/free can make this
4744cd46caSbillm  * future guesswork more accurate.
4844cd46caSbillm  */
4944cd46caSbillm static int
too_many_errors(vdev_t * vd,int numerrors)5044cd46caSbillm too_many_errors(vdev_t *vd, int numerrors)
5144cd46caSbillm {
520a4e9518Sgw25295 	ASSERT3U(numerrors, <=, vd->vdev_children);
5351ece835Seschrock 	return (numerrors > 0);
5444cd46caSbillm }
5544cd46caSbillm 
56fa9e4066Sahrens static int
vdev_root_open(vdev_t * vd,uint64_t * asize,uint64_t * max_asize,uint64_t * ashift)574263d13fSGeorge Wilson vdev_root_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
584263d13fSGeorge Wilson     uint64_t *ashift)
59fa9e4066Sahrens {
60fa9e4066Sahrens 	int lasterror = 0;
6144cd46caSbillm 	int numerrors = 0;
62fa9e4066Sahrens 
63fa9e4066Sahrens 	if (vd->vdev_children == 0) {
64fa9e4066Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
65*be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
66fa9e4066Sahrens 	}
67fa9e4066Sahrens 
68f64c0e34SEric Taylor 	vdev_open_children(vd);
69fa9e4066Sahrens 
70f64c0e34SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++) {
71f64c0e34SEric Taylor 		vdev_t *cvd = vd->vdev_child[c];
72f64c0e34SEric Taylor 
73f64c0e34SEric Taylor 		if (cvd->vdev_open_error && !cvd->vdev_islog) {
74f64c0e34SEric Taylor 			lasterror = cvd->vdev_open_error;
7544cd46caSbillm 			numerrors++;
76fa9e4066Sahrens 		}
77fa9e4066Sahrens 	}
78fa9e4066Sahrens 
7951ece835Seschrock 	if (too_many_errors(vd, numerrors)) {
80fa9e4066Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
8144cd46caSbillm 		return (lasterror);
8244cd46caSbillm 	}
83fa9e4066Sahrens 
84ecc2d604Sbonwick 	*asize = 0;
854263d13fSGeorge Wilson 	*max_asize = 0;
86ecc2d604Sbonwick 	*ashift = 0;
87ecc2d604Sbonwick 
8844cd46caSbillm 	return (0);
89fa9e4066Sahrens }
90fa9e4066Sahrens 
91fa9e4066Sahrens static void
vdev_root_close(vdev_t * vd)92fa9e4066Sahrens vdev_root_close(vdev_t *vd)
93fa9e4066Sahrens {
94f64c0e34SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
95fa9e4066Sahrens 		vdev_close(vd->vdev_child[c]);
96fa9e4066Sahrens }
97fa9e4066Sahrens 
98fa9e4066Sahrens static void
vdev_root_state_change(vdev_t * vd,int faulted,int degraded)99fa9e4066Sahrens vdev_root_state_change(vdev_t *vd, int faulted, int degraded)
100fa9e4066Sahrens {
10151ece835Seschrock 	if (too_many_errors(vd, faulted)) {
102ea8dc4b6Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
103ea8dc4b6Seschrock 		    VDEV_AUX_NO_REPLICAS);
1040a4e9518Sgw25295 	} else if (degraded) {
1050a4e9518Sgw25295 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
1060a4e9518Sgw25295 	} else {
107ea8dc4b6Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
108fa9e4066Sahrens 	}
1090a4e9518Sgw25295 }
110fa9e4066Sahrens 
111fa9e4066Sahrens vdev_ops_t vdev_root_ops = {
112fa9e4066Sahrens 	vdev_root_open,
113fa9e4066Sahrens 	vdev_root_close,
114fa9e4066Sahrens 	vdev_default_asize,
115fa9e4066Sahrens 	NULL,			/* io_start - not applicable to the root */
116fa9e4066Sahrens 	NULL,			/* io_done - not applicable to the root */
117fa9e4066Sahrens 	vdev_root_state_change,
118dcba9f3fSGeorge Wilson 	NULL,
119dcba9f3fSGeorge Wilson 	NULL,
120fa9e4066Sahrens 	VDEV_TYPE_ROOT,		/* name of this vdev type */
121fa9e4066Sahrens 	B_FALSE			/* not a leaf vdev */
122fa9e4066Sahrens };
123