1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 2012, 2016 by Delphix. All rights reserved. 29 */ 30 31 #include <sys/zfs_context.h> 32 #include <sys/spa.h> 33 #include <sys/vdev_impl.h> 34 #include <sys/zio.h> 35 #include <sys/fs/zfs.h> 36 37 /* 38 * Virtual device vector for the pool's root vdev. 39 */ 40 41 static uint64_t 42 vdev_root_core_tvds(vdev_t *vd) 43 { 44 uint64_t tvds = 0; 45 46 for (uint64_t c = 0; c < vd->vdev_children; c++) { 47 vdev_t *cvd = vd->vdev_child[c]; 48 49 if (!cvd->vdev_ishole && !cvd->vdev_islog && 50 cvd->vdev_ops != &vdev_indirect_ops) { 51 tvds++; 52 } 53 } 54 55 return (tvds); 56 } 57 58 /* 59 * We should be able to tolerate one failure with absolutely no damage 60 * to our metadata. Two failures will take out space maps, a bunch of 61 * indirect block trees, meta dnodes, dnodes, etc. Probably not a happy 62 * place to live. When we get smarter, we can liberalize this policy. 63 * e.g. If we haven't lost two consecutive top-level vdevs, then we are 64 * probably fine. Adding bean counters during alloc/free can make this 65 * future guesswork more accurate. 66 */ 67 static boolean_t 68 too_many_errors(vdev_t *vd, uint64_t numerrors) 69 { 70 uint64_t tvds; 71 72 if (numerrors == 0) 73 return (B_FALSE); 74 75 tvds = vdev_root_core_tvds(vd); 76 ASSERT3U(numerrors, <=, tvds); 77 78 if (numerrors == tvds) 79 return (B_TRUE); 80 81 return (numerrors > spa_missing_tvds_allowed(vd->vdev_spa)); 82 } 83 84 static int 85 vdev_root_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize, 86 uint64_t *ashift, uint64_t *pshift) 87 { 88 spa_t *spa = vd->vdev_spa; 89 int lasterror = 0; 90 int numerrors = 0; 91 92 if (vd->vdev_children == 0) { 93 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 94 return (SET_ERROR(EINVAL)); 95 } 96 97 vdev_open_children(vd); 98 99 for (int c = 0; c < vd->vdev_children; c++) { 100 vdev_t *cvd = vd->vdev_child[c]; 101 102 if (cvd->vdev_open_error && !cvd->vdev_islog && 103 cvd->vdev_ops != &vdev_indirect_ops) { 104 lasterror = cvd->vdev_open_error; 105 numerrors++; 106 } 107 } 108 109 if (spa_load_state(spa) != SPA_LOAD_NONE) 110 spa_set_missing_tvds(spa, numerrors); 111 112 if (too_many_errors(vd, numerrors)) { 113 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS; 114 return (lasterror); 115 } 116 117 *asize = 0; 118 *max_asize = 0; 119 *ashift = 0; 120 *pshift = 0; 121 122 return (0); 123 } 124 125 static void 126 vdev_root_close(vdev_t *vd) 127 { 128 for (int c = 0; c < vd->vdev_children; c++) 129 vdev_close(vd->vdev_child[c]); 130 } 131 132 static void 133 vdev_root_state_change(vdev_t *vd, int faulted, int degraded) 134 { 135 if (too_many_errors(vd, faulted)) { 136 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN, 137 VDEV_AUX_NO_REPLICAS); 138 } else if (degraded || faulted) { 139 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE); 140 } else { 141 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE); 142 } 143 } 144 145 vdev_ops_t vdev_root_ops = { 146 .vdev_op_init = NULL, 147 .vdev_op_fini = NULL, 148 .vdev_op_open = vdev_root_open, 149 .vdev_op_close = vdev_root_close, 150 .vdev_op_psize_to_asize = vdev_default_asize, 151 .vdev_op_asize_to_psize = vdev_default_psize, 152 .vdev_op_min_asize = vdev_default_min_asize, 153 .vdev_op_min_alloc = NULL, 154 .vdev_op_io_start = NULL, /* not applicable to the root */ 155 .vdev_op_io_done = NULL, /* not applicable to the root */ 156 .vdev_op_state_change = vdev_root_state_change, 157 .vdev_op_need_resilver = NULL, 158 .vdev_op_hold = NULL, 159 .vdev_op_rele = NULL, 160 .vdev_op_remap = NULL, 161 .vdev_op_xlate = NULL, 162 .vdev_op_rebuild_asize = NULL, 163 .vdev_op_metaslab_init = NULL, 164 .vdev_op_config_generate = NULL, 165 .vdev_op_nparity = NULL, 166 .vdev_op_ndisks = NULL, 167 .vdev_op_type = VDEV_TYPE_ROOT, /* name of this vdev type */ 168 .vdev_op_leaf = B_FALSE /* not a leaf vdev */ 169 }; 170