1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011 by Delphix. All rights reserved. 25 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 26 */ 27 28 29 #include <sys/zfs_context.h> 30 #include <sys/fm/fs/zfs.h> 31 #include <sys/spa_impl.h> 32 #include <sys/zio.h> 33 #include <sys/zio_checksum.h> 34 #include <sys/dmu.h> 35 #include <sys/dmu_tx.h> 36 #include <sys/zap.h> 37 #include <sys/zil.h> 38 #include <sys/ddt.h> 39 #include <sys/vdev_impl.h> 40 #include <sys/vdev_os.h> 41 #include <sys/vdev_removal.h> 42 #include <sys/vdev_indirect_mapping.h> 43 #include <sys/vdev_indirect_births.h> 44 #include <sys/metaslab.h> 45 #include <sys/metaslab_impl.h> 46 #include <sys/uberblock_impl.h> 47 #include <sys/txg.h> 48 #include <sys/avl.h> 49 #include <sys/bpobj.h> 50 #include <sys/dmu_traverse.h> 51 #include <sys/dmu_objset.h> 52 #include <sys/unique.h> 53 #include <sys/dsl_pool.h> 54 #include <sys/dsl_dataset.h> 55 #include <sys/dsl_dir.h> 56 #include <sys/dsl_prop.h> 57 #include <sys/dsl_synctask.h> 58 #include <sys/fs/zfs.h> 59 #include <sys/arc.h> 60 #include <sys/callb.h> 61 #include <sys/spa_boot.h> 62 #include <sys/zfs_ioctl.h> 63 #include <sys/dsl_scan.h> 64 #include <sys/dmu_send.h> 65 #include <sys/dsl_destroy.h> 66 #include <sys/dsl_userhold.h> 67 #include <sys/zfeature.h> 68 #include <sys/zvol.h> 69 #include <sys/abd.h> 70 #include <sys/callb.h> 71 #include <sys/zone.h> 72 73 #include "zfs_prop.h" 74 #include "zfs_comutil.h" 75 76 static nvlist_t * 77 spa_generate_rootconf(const char *name) 78 { 79 nvlist_t **configs, **tops; 80 nvlist_t *config; 81 nvlist_t *best_cfg, *nvtop, *nvroot; 82 uint64_t *holes; 83 uint64_t best_txg; 84 uint64_t nchildren; 85 uint64_t pgid; 86 uint64_t count; 87 uint64_t i; 88 uint_t nholes; 89 90 if (vdev_geom_read_pool_label(name, &configs, &count) != 0) 91 return (NULL); 92 93 ASSERT3U(count, !=, 0); 94 best_txg = 0; 95 for (i = 0; i < count; i++) { 96 uint64_t txg; 97 98 txg = fnvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG); 99 if (txg > best_txg) { 100 best_txg = txg; 101 best_cfg = configs[i]; 102 } 103 } 104 105 nchildren = 1; 106 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren); 107 holes = NULL; 108 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY, 109 &holes, &nholes); 110 111 tops = kmem_zalloc(nchildren * sizeof (void *), KM_SLEEP); 112 for (i = 0; i < nchildren; i++) { 113 if (i >= count) 114 break; 115 if (configs[i] == NULL) 116 continue; 117 nvtop = fnvlist_lookup_nvlist(configs[i], 118 ZPOOL_CONFIG_VDEV_TREE); 119 tops[i] = fnvlist_dup(nvtop); 120 } 121 for (i = 0; holes != NULL && i < nholes; i++) { 122 if (i >= nchildren) 123 continue; 124 if (tops[holes[i]] != NULL) 125 continue; 126 tops[holes[i]] = fnvlist_alloc(); 127 fnvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE, 128 VDEV_TYPE_HOLE); 129 fnvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID, holes[i]); 130 fnvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID, 0); 131 } 132 for (i = 0; i < nchildren; i++) { 133 if (tops[i] != NULL) 134 continue; 135 tops[i] = fnvlist_alloc(); 136 fnvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE, 137 VDEV_TYPE_MISSING); 138 fnvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID, i); 139 fnvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID, 0); 140 } 141 142 /* 143 * Create pool config based on the best vdev config. 144 */ 145 config = fnvlist_dup(best_cfg); 146 147 /* 148 * Put this pool's top-level vdevs into a root vdev. 149 */ 150 pgid = fnvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID); 151 nvroot = fnvlist_alloc(); 152 fnvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT); 153 fnvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL); 154 fnvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid); 155 fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, 156 (const nvlist_t * const *)tops, nchildren); 157 158 /* 159 * Replace the existing vdev_tree with the new root vdev in 160 * this pool's configuration (remove the old, add the new). 161 */ 162 fnvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot); 163 164 /* 165 * Drop vdev config elements that should not be present at pool level. 166 */ 167 fnvlist_remove(config, ZPOOL_CONFIG_GUID); 168 fnvlist_remove(config, ZPOOL_CONFIG_TOP_GUID); 169 170 for (i = 0; i < count; i++) 171 fnvlist_free(configs[i]); 172 kmem_free(configs, count * sizeof (void *)); 173 for (i = 0; i < nchildren; i++) 174 fnvlist_free(tops[i]); 175 kmem_free(tops, nchildren * sizeof (void *)); 176 fnvlist_free(nvroot); 177 return (config); 178 } 179 180 int 181 spa_import_rootpool(const char *name, bool checkpointrewind) 182 { 183 spa_t *spa; 184 vdev_t *rvd; 185 nvlist_t *config, *nvtop; 186 char *pname; 187 int error; 188 189 /* 190 * Read the label from the boot device and generate a configuration. 191 */ 192 config = spa_generate_rootconf(name); 193 194 mutex_enter(&spa_namespace_lock); 195 if (config != NULL) { 196 pname = fnvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME); 197 VERIFY0(strcmp(name, pname)); 198 199 if ((spa = spa_lookup(pname)) != NULL) { 200 /* 201 * The pool could already be imported, 202 * e.g., after reboot -r. 203 */ 204 if (spa->spa_state == POOL_STATE_ACTIVE) { 205 mutex_exit(&spa_namespace_lock); 206 fnvlist_free(config); 207 return (0); 208 } 209 210 /* 211 * Remove the existing root pool from the namespace so 212 * that we can replace it with the correct config 213 * we just read in. 214 */ 215 spa_remove(spa); 216 } 217 spa = spa_add(pname, config, NULL); 218 219 /* 220 * Set spa_ubsync.ub_version as it can be used in vdev_alloc() 221 * via spa_version(). 222 */ 223 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, 224 &spa->spa_ubsync.ub_version) != 0) 225 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; 226 } else if ((spa = spa_lookup(name)) == NULL) { 227 mutex_exit(&spa_namespace_lock); 228 fnvlist_free(config); 229 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'", 230 name); 231 return (EIO); 232 } else { 233 config = fnvlist_dup(spa->spa_config); 234 } 235 spa->spa_is_root = B_TRUE; 236 spa->spa_import_flags = ZFS_IMPORT_VERBATIM; 237 if (checkpointrewind) { 238 spa->spa_import_flags |= ZFS_IMPORT_CHECKPOINT; 239 } 240 241 /* 242 * Build up a vdev tree based on the boot device's label config. 243 */ 244 nvtop = fnvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE); 245 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 246 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, 247 VDEV_ALLOC_ROOTPOOL); 248 spa_config_exit(spa, SCL_ALL, FTAG); 249 if (error) { 250 mutex_exit(&spa_namespace_lock); 251 fnvlist_free(config); 252 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", 253 pname); 254 return (error); 255 } 256 257 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 258 vdev_free(rvd); 259 spa_config_exit(spa, SCL_ALL, FTAG); 260 mutex_exit(&spa_namespace_lock); 261 262 fnvlist_free(config); 263 return (0); 264 } 265 266 const char * 267 spa_history_zone(void) 268 { 269 return ("freebsd"); 270 } 271 272 void 273 spa_import_os(spa_t *spa) 274 { 275 (void) spa; 276 } 277 278 void 279 spa_export_os(spa_t *spa) 280 { 281 (void) spa; 282 } 283 284 void 285 spa_activate_os(spa_t *spa) 286 { 287 (void) spa; 288 } 289 290 void 291 spa_deactivate_os(spa_t *spa) 292 { 293 (void) spa; 294 } 295