2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
22dcba9f3George Wilson * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23fa9e406ahrens * Use is subject to license terms.
24fa9e406ahrens */
264263d13George Wilson/*
27094e47eGeorge Wilson * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28ac04831Mike Gerdts * Copyright 2019 Joyent, Inc.
294263d13George Wilson */
304263d13George Wilson
31fa9e406ahrens#include <sys/zfs_context.h>
32fa9e406ahrens#include <sys/spa.h>
33fa9e406ahrens#include <sys/vdev_impl.h>
34fa9e406ahrens#include <sys/zio.h>
35fa9e406ahrens#include <sys/fs/zfs.h>
38fa9e406ahrens * Virtual device vector for the pool's root vdev.
39fa9e406ahrens */
416f79381Pavel Zakharovstatic uint64_t
426f79381Pavel Zakharovvdev_root_core_tvds(vdev_t *vd)
436f79381Pavel Zakharov{
446f79381Pavel Zakharov	uint64_t tvds = 0;
456f79381Pavel Zakharov
466f79381Pavel Zakharov	for (uint64_t c = 0; c < vd->vdev_children; c++) {
476f79381Pavel Zakharov		vdev_t *cvd = vd->vdev_child[c];
486f79381Pavel Zakharov
496f79381Pavel Zakharov		if (!cvd->vdev_ishole && !cvd->vdev_islog &&
506f79381Pavel Zakharov		    cvd->vdev_ops != &vdev_indirect_ops) {
516f79381Pavel Zakharov			tvds++;
526f79381Pavel Zakharov		}
536f79381Pavel Zakharov	}
546f79381Pavel Zakharov
556f79381Pavel Zakharov	return (tvds);
566f79381Pavel Zakharov}
576f79381Pavel Zakharov
5944cd46cbillm * We should be able to tolerate one failure with absolutely no damage
6044cd46cbillm * to our metadata.  Two failures will take out space maps, a bunch of
6144cd46cbillm * indirect block trees, meta dnodes, dnodes, etc.  Probably not a happy
6244cd46cbillm * place to live.  When we get smarter, we can liberalize this policy.
6344cd46cbillm * e.g. If we haven't lost two consecutive top-level vdevs, then we are
6444cd46cbillm * probably fine.  Adding bean counters during alloc/free can make this
6544cd46cbillm * future guesswork more accurate.
6644cd46cbillm */
676f79381Pavel Zakharovstatic boolean_t
686f79381Pavel Zakharovtoo_many_errors(vdev_t *vd, uint64_t numerrors)
706f79381Pavel Zakharov	uint64_t tvds;
716f79381Pavel Zakharov
726f79381Pavel Zakharov	if (numerrors == 0)
736f79381Pavel Zakharov		return (B_FALSE);
746f79381Pavel Zakharov
756f79381Pavel Zakharov	tvds = vdev_root_core_tvds(vd);
766f79381Pavel Zakharov	ASSERT3U(numerrors, <=, tvds);
776f79381Pavel Zakharov
786f79381Pavel Zakharov	if (numerrors == tvds)
796f79381Pavel Zakharov		return (B_TRUE);
806f79381Pavel Zakharov
816f79381Pavel Zakharov	return (numerrors > spa_missing_tvds_allowed(vd->vdev_spa));
84fa9e406ahrensstatic int
854263d13George Wilsonvdev_root_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
864263d13George Wilson    uint64_t *ashift)
886f79381Pavel Zakharov	spa_t *spa = vd->vdev_spa;
89fa9e406ahrens	int lasterror = 0;
9044cd46cbillm	int numerrors = 0;
92fa9e406ahrens	if (vd->vdev_children == 0) {
93fa9e406ahrens		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
94be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
95fa9e406ahrens	}
97f64c0e3Eric Taylor	vdev_open_children(vd);
98f64c0e3Eric Taylor
99f64c0e3Eric Taylor	for (int c = 0; c < vd->vdev_children; c++) {
1000a4e951gw		vdev_t *cvd = vd->vdev_child[c];
102f64c0e3Eric Taylor		if (cvd->vdev_open_error && !cvd->vdev_islog) {
103f64c0e3Eric Taylor			lasterror = cvd->vdev_open_error;
10444cd46cbillm			numerrors++;
105fa9e406ahrens		}
106fa9e406ahrens	}
1086f79381Pavel Zakharov	if (spa_load_state(spa) != SPA_LOAD_NONE)
1096f79381Pavel Zakharov		spa_set_missing_tvds(spa, numerrors);
1106f79381Pavel Zakharov
11151ece83eschrock	if (too_many_errors(vd, numerrors)) {
11251ece83eschrock		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
11351ece83eschrock		return (lasterror);
11444cd46cbillm	}
116ecc2d60bonwick	*asize = 0;
1174263d13George Wilson	*max_asize = 0;
118ecc2d60bonwick	*ashift = 0;
12044cd46cbillm	return (0);
123fa9e406ahrensstatic void
124fa9e406ahrensvdev_root_close(vdev_t *vd)
126f64c0e3Eric Taylor	for (int c = 0; c < vd->vdev_children; c++)
127fa9e406ahrens		vdev_close(vd->vdev_child[c]);
130fa9e406ahrensstatic void
131fa9e406ahrensvdev_root_state_change(vdev_t *vd, int faulted, int degraded)
13351ece83eschrock	if (too_many_errors(vd, faulted)) {
13451ece83eschrock		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
13551ece83eschrock		    VDEV_AUX_NO_REPLICAS);
1366f79381Pavel Zakharov	} else if (degraded || faulted) {
137ea8dc4beschrock		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
1380a4e951gw	} else {
139ea8dc4beschrock		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
1400a4e951gw	}
143fa9e406ahrensvdev_ops_t vdev_root_ops = {
144a3874b8Toomas Soome	.vdev_op_open = vdev_root_open,
145a3874b8Toomas Soome	.vdev_op_close = vdev_root_close,
146a3874b8Toomas Soome	.vdev_op_asize = vdev_default_asize,
147a3874b8Toomas Soome	.vdev_op_io_start = NULL,	/* not applicable to the root */
148a3874b8Toomas Soome	.vdev_op_io_done = NULL,	/* not applicable to the root */
149a3874b8Toomas Soome	.vdev_op_state_change = vdev_root_state_change,
150a3874b8Toomas Soome	.vdev_op_need_resilver = NULL,
151a3874b8Toomas Soome	.vdev_op_hold = NULL,
152a3874b8Toomas Soome	.vdev_op_rele = NULL,
153a3874b8Toomas Soome	.vdev_op_remap = NULL,
154a3874b8Toomas Soome	.vdev_op_xlate = NULL,
155ac04831Mike Gerdts	.vdev_op_dumpio = NULL,
156a3874b8Toomas Soome	.vdev_op_type = VDEV_TYPE_ROOT,	/* name of this vdev type */
157a3874b8Toomas Soome	.vdev_op_leaf = B_FALSE		/* not a leaf vdev */