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 /*
27094e47e9SGeorge Wilson * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28*ac04831dSMike Gerdts * Copyright 2019 Joyent, Inc.
294263d13fSGeorge Wilson */
304263d13fSGeorge Wilson
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens #include <sys/spa.h>
33fa9e4066Sahrens #include <sys/vdev_impl.h>
34fa9e4066Sahrens #include <sys/zio.h>
35fa9e4066Sahrens #include <sys/fs/zfs.h>
36fa9e4066Sahrens
37fa9e4066Sahrens /*
38fa9e4066Sahrens * Virtual device vector for the pool's root vdev.
39fa9e4066Sahrens */
40fa9e4066Sahrens
416f793812SPavel Zakharov static uint64_t
vdev_root_core_tvds(vdev_t * vd)426f793812SPavel Zakharov vdev_root_core_tvds(vdev_t *vd)
436f793812SPavel Zakharov {
446f793812SPavel Zakharov uint64_t tvds = 0;
456f793812SPavel Zakharov
466f793812SPavel Zakharov for (uint64_t c = 0; c < vd->vdev_children; c++) {
476f793812SPavel Zakharov vdev_t *cvd = vd->vdev_child[c];
486f793812SPavel Zakharov
496f793812SPavel Zakharov if (!cvd->vdev_ishole && !cvd->vdev_islog &&
506f793812SPavel Zakharov cvd->vdev_ops != &vdev_indirect_ops) {
516f793812SPavel Zakharov tvds++;
526f793812SPavel Zakharov }
536f793812SPavel Zakharov }
546f793812SPavel Zakharov
556f793812SPavel Zakharov return (tvds);
566f793812SPavel Zakharov }
576f793812SPavel Zakharov
5844cd46caSbillm /*
5944cd46caSbillm * We should be able to tolerate one failure with absolutely no damage
6044cd46caSbillm * to our metadata. Two failures will take out space maps, a bunch of
6144cd46caSbillm * indirect block trees, meta dnodes, dnodes, etc. Probably not a happy
6244cd46caSbillm * place to live. When we get smarter, we can liberalize this policy.
6344cd46caSbillm * e.g. If we haven't lost two consecutive top-level vdevs, then we are
6444cd46caSbillm * probably fine. Adding bean counters during alloc/free can make this
6544cd46caSbillm * future guesswork more accurate.
6644cd46caSbillm */
676f793812SPavel Zakharov static boolean_t
too_many_errors(vdev_t * vd,uint64_t numerrors)686f793812SPavel Zakharov too_many_errors(vdev_t *vd, uint64_t numerrors)
6944cd46caSbillm {
706f793812SPavel Zakharov uint64_t tvds;
716f793812SPavel Zakharov
726f793812SPavel Zakharov if (numerrors == 0)
736f793812SPavel Zakharov return (B_FALSE);
746f793812SPavel Zakharov
756f793812SPavel Zakharov tvds = vdev_root_core_tvds(vd);
766f793812SPavel Zakharov ASSERT3U(numerrors, <=, tvds);
776f793812SPavel Zakharov
786f793812SPavel Zakharov if (numerrors == tvds)
796f793812SPavel Zakharov return (B_TRUE);
806f793812SPavel Zakharov
816f793812SPavel Zakharov return (numerrors > spa_missing_tvds_allowed(vd->vdev_spa));
8244cd46caSbillm }
8344cd46caSbillm
84fa9e4066Sahrens static int
vdev_root_open(vdev_t * vd,uint64_t * asize,uint64_t * max_asize,uint64_t * ashift)854263d13fSGeorge Wilson vdev_root_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
864263d13fSGeorge Wilson uint64_t *ashift)
87fa9e4066Sahrens {
886f793812SPavel Zakharov spa_t *spa = vd->vdev_spa;
89fa9e4066Sahrens int lasterror = 0;
9044cd46caSbillm int numerrors = 0;
91fa9e4066Sahrens
92fa9e4066Sahrens if (vd->vdev_children == 0) {
93fa9e4066Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
94be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL));
95fa9e4066Sahrens }
96fa9e4066Sahrens
97f64c0e34SEric Taylor vdev_open_children(vd);
98f64c0e34SEric Taylor
99f64c0e34SEric Taylor for (int c = 0; c < vd->vdev_children; c++) {
1000a4e9518Sgw vdev_t *cvd = vd->vdev_child[c];
101fa9e4066Sahrens
102f64c0e34SEric Taylor if (cvd->vdev_open_error && !cvd->vdev_islog) {
103f64c0e34SEric Taylor lasterror = cvd->vdev_open_error;
10444cd46caSbillm numerrors++;
105fa9e4066Sahrens }
106fa9e4066Sahrens }
107fa9e4066Sahrens
1086f793812SPavel Zakharov if (spa_load_state(spa) != SPA_LOAD_NONE)
1096f793812SPavel Zakharov spa_set_missing_tvds(spa, numerrors);
1106f793812SPavel Zakharov
11151ece835Seschrock if (too_many_errors(vd, numerrors)) {
11251ece835Seschrock vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
11351ece835Seschrock return (lasterror);
11444cd46caSbillm }
115fa9e4066Sahrens
116ecc2d604Sbonwick *asize = 0;
1174263d13fSGeorge Wilson *max_asize = 0;
118ecc2d604Sbonwick *ashift = 0;
119ecc2d604Sbonwick
12044cd46caSbillm return (0);
121fa9e4066Sahrens }
122fa9e4066Sahrens
123fa9e4066Sahrens static void
vdev_root_close(vdev_t * vd)124fa9e4066Sahrens vdev_root_close(vdev_t *vd)
125fa9e4066Sahrens {
126f64c0e34SEric Taylor for (int c = 0; c < vd->vdev_children; c++)
127fa9e4066Sahrens vdev_close(vd->vdev_child[c]);
128fa9e4066Sahrens }
129fa9e4066Sahrens
130fa9e4066Sahrens static void
vdev_root_state_change(vdev_t * vd,int faulted,int degraded)131fa9e4066Sahrens vdev_root_state_change(vdev_t *vd, int faulted, int degraded)
132fa9e4066Sahrens {
13351ece835Seschrock if (too_many_errors(vd, faulted)) {
13451ece835Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13551ece835Seschrock VDEV_AUX_NO_REPLICAS);
1366f793812SPavel Zakharov } else if (degraded || faulted) {
137ea8dc4b6Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
1380a4e9518Sgw } else {
139ea8dc4b6Seschrock vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
1400a4e9518Sgw }
141fa9e4066Sahrens }
142fa9e4066Sahrens
143fa9e4066Sahrens vdev_ops_t vdev_root_ops = {
144a3874b8bSToomas Soome .vdev_op_open = vdev_root_open,
145a3874b8bSToomas Soome .vdev_op_close = vdev_root_close,
146a3874b8bSToomas Soome .vdev_op_asize = vdev_default_asize,
147a3874b8bSToomas Soome .vdev_op_io_start = NULL, /* not applicable to the root */
148a3874b8bSToomas Soome .vdev_op_io_done = NULL, /* not applicable to the root */
149a3874b8bSToomas Soome .vdev_op_state_change = vdev_root_state_change,
150a3874b8bSToomas Soome .vdev_op_need_resilver = NULL,
151a3874b8bSToomas Soome .vdev_op_hold = NULL,
152a3874b8bSToomas Soome .vdev_op_rele = NULL,
153a3874b8bSToomas Soome .vdev_op_remap = NULL,
154a3874b8bSToomas Soome .vdev_op_xlate = NULL,
155*ac04831dSMike Gerdts .vdev_op_dumpio = NULL,
156a3874b8bSToomas Soome .vdev_op_type = VDEV_TYPE_ROOT, /* name of this vdev type */
157a3874b8bSToomas Soome .vdev_op_leaf = B_FALSE /* not a leaf vdev */
158fa9e4066Sahrens };
159