xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_root.c (revision f64c0e34235c0ee36e44e9ff1cf0cd3764ed227d)
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 /*
22*f64c0e34SEric Taylor  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <sys/zfs_context.h>
27fa9e4066Sahrens #include <sys/spa.h>
28fa9e4066Sahrens #include <sys/vdev_impl.h>
29fa9e4066Sahrens #include <sys/zio.h>
30fa9e4066Sahrens #include <sys/fs/zfs.h>
31fa9e4066Sahrens 
32fa9e4066Sahrens /*
33fa9e4066Sahrens  * Virtual device vector for the pool's root vdev.
34fa9e4066Sahrens  */
35fa9e4066Sahrens 
3644cd46caSbillm /*
3744cd46caSbillm  * We should be able to tolerate one failure with absolutely no damage
3844cd46caSbillm  * to our metadata.  Two failures will take out space maps, a bunch of
3944cd46caSbillm  * indirect block trees, meta dnodes, dnodes, etc.  Probably not a happy
4044cd46caSbillm  * place to live.  When we get smarter, we can liberalize this policy.
4144cd46caSbillm  * e.g. If we haven't lost two consecutive top-level vdevs, then we are
4244cd46caSbillm  * probably fine.  Adding bean counters during alloc/free can make this
4344cd46caSbillm  * future guesswork more accurate.
4444cd46caSbillm  */
4544cd46caSbillm static int
4644cd46caSbillm too_many_errors(vdev_t *vd, int numerrors)
4744cd46caSbillm {
480a4e9518Sgw 	ASSERT3U(numerrors, <=, vd->vdev_children);
4951ece835Seschrock 	return (numerrors > 0);
5044cd46caSbillm }
5144cd46caSbillm 
52fa9e4066Sahrens static int
53fa9e4066Sahrens vdev_root_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
54fa9e4066Sahrens {
55fa9e4066Sahrens 	int lasterror = 0;
5644cd46caSbillm 	int numerrors = 0;
57fa9e4066Sahrens 
58fa9e4066Sahrens 	if (vd->vdev_children == 0) {
59fa9e4066Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
60fa9e4066Sahrens 		return (EINVAL);
61fa9e4066Sahrens 	}
62fa9e4066Sahrens 
63*f64c0e34SEric Taylor 	vdev_open_children(vd);
64*f64c0e34SEric Taylor 
65*f64c0e34SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++) {
660a4e9518Sgw 		vdev_t *cvd = vd->vdev_child[c];
67fa9e4066Sahrens 
68*f64c0e34SEric Taylor 		if (cvd->vdev_open_error && !cvd->vdev_islog) {
69*f64c0e34SEric Taylor 			lasterror = cvd->vdev_open_error;
7044cd46caSbillm 			numerrors++;
71fa9e4066Sahrens 		}
72fa9e4066Sahrens 	}
73fa9e4066Sahrens 
7451ece835Seschrock 	if (too_many_errors(vd, numerrors)) {
7551ece835Seschrock 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
7651ece835Seschrock 		return (lasterror);
7744cd46caSbillm 	}
78fa9e4066Sahrens 
79ecc2d604Sbonwick 	*asize = 0;
80ecc2d604Sbonwick 	*ashift = 0;
81ecc2d604Sbonwick 
8244cd46caSbillm 	return (0);
83fa9e4066Sahrens }
84fa9e4066Sahrens 
85fa9e4066Sahrens static void
86fa9e4066Sahrens vdev_root_close(vdev_t *vd)
87fa9e4066Sahrens {
88*f64c0e34SEric Taylor 	for (int c = 0; c < vd->vdev_children; c++)
89fa9e4066Sahrens 		vdev_close(vd->vdev_child[c]);
90fa9e4066Sahrens }
91fa9e4066Sahrens 
92fa9e4066Sahrens static void
93fa9e4066Sahrens vdev_root_state_change(vdev_t *vd, int faulted, int degraded)
94fa9e4066Sahrens {
9551ece835Seschrock 	if (too_many_errors(vd, faulted)) {
9651ece835Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
9751ece835Seschrock 		    VDEV_AUX_NO_REPLICAS);
980a4e9518Sgw 	} else if (degraded) {
99ea8dc4b6Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
1000a4e9518Sgw 	} else {
101ea8dc4b6Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
1020a4e9518Sgw 	}
103fa9e4066Sahrens }
104fa9e4066Sahrens 
105fa9e4066Sahrens vdev_ops_t vdev_root_ops = {
106fa9e4066Sahrens 	vdev_root_open,
107fa9e4066Sahrens 	vdev_root_close,
108fa9e4066Sahrens 	vdev_default_asize,
109fa9e4066Sahrens 	NULL,			/* io_start - not applicable to the root */
110fa9e4066Sahrens 	NULL,			/* io_done - not applicable to the root */
111fa9e4066Sahrens 	vdev_root_state_change,
112fa9e4066Sahrens 	VDEV_TYPE_ROOT,		/* name of this vdev type */
113fa9e4066Sahrens 	B_FALSE			/* not a leaf vdev */
114fa9e4066Sahrens };
115