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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/spa.h> 29 #include <sys/spa_impl.h> 30 #include <sys/nvpair.h> 31 #include <sys/uio.h> 32 #include <sys/fs/zfs.h> 33 #include <sys/vdev_impl.h> 34 #include <sys/zfs_ioctl.h> 35 #ifdef _KERNEL 36 #include <sys/kobj.h> 37 #endif 38 39 extern int modrootloaded; 40 41 /* 42 * Pool configuration repository. 43 * 44 * The configuration for all pools, in addition to being stored on disk, is 45 * stored in /kernel/drv/zpool.cache as a packed nvlist. The kernel maintains 46 * this list as pools are created, destroyed, or modified. 47 * 48 * We have a single nvlist which holds all the configuration information. When 49 * the module loads, we read this information from the cache and populate the 50 * SPA namespace. This namespace is maintained independently in spa.c. 51 * Whenever the namespace is modified, or the configuration of a pool is 52 * changed, we call spa_config_sync(), which walks through all the active pools 53 * and writes the configuration to disk. 54 */ 55 56 static uint64_t spa_config_generation = 1; 57 58 /* 59 * This can be overridden in userland to preserve an alternate namespace for 60 * userland pools when doing testing. 61 */ 62 const char *spa_config_dir = ZPOOL_CACHE_DIR; 63 64 /* 65 * Called when the module is first loaded, this routine loads the configuration 66 * file into the SPA namespace. It does not actually open or load the pools; it 67 * only populates the namespace. 68 */ 69 void 70 spa_config_load(void) 71 { 72 void *buf = NULL; 73 nvlist_t *nvlist, *child; 74 nvpair_t *nvpair; 75 spa_t *spa; 76 char pathname[128]; 77 struct _buf *file; 78 struct bootstat bst; 79 80 /* 81 * Open the configuration file. 82 */ 83 (void) snprintf(pathname, sizeof (pathname), "%s%s/%s", 84 (modrootloaded) ? "./" : "", spa_config_dir, ZPOOL_CACHE_FILE); 85 86 file = kobj_open_file(pathname); 87 if (file == (struct _buf *)-1) 88 return; 89 90 if (kobj_fstat(file->_fd, &bst) != 0) 91 goto out; 92 93 buf = kmem_alloc(bst.st_size, KM_SLEEP); 94 95 /* 96 * Read the nvlist from the file. 97 */ 98 if (kobj_read_file(file, buf, bst.st_size, 0) < 0) 99 goto out; 100 101 /* 102 * Unpack the nvlist. 103 */ 104 if (nvlist_unpack(buf, bst.st_size, &nvlist, KM_SLEEP) != 0) 105 goto out; 106 107 /* 108 * Iterate over all elements in the nvlist, creating a new spa_t for 109 * each one with the specified configuration. 110 */ 111 mutex_enter(&spa_namespace_lock); 112 nvpair = NULL; 113 while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) { 114 115 if (nvpair_type(nvpair) != DATA_TYPE_NVLIST) 116 continue; 117 118 VERIFY(nvpair_value_nvlist(nvpair, &child) == 0); 119 120 if (spa_lookup(nvpair_name(nvpair)) != NULL) 121 continue; 122 spa = spa_add(nvpair_name(nvpair)); 123 124 /* 125 * We blindly duplicate the configuration here. If it's 126 * invalid, we will catch it when the pool is first opened. 127 */ 128 VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0); 129 } 130 mutex_exit(&spa_namespace_lock); 131 132 nvlist_free(nvlist); 133 134 out: 135 if (buf != NULL) 136 kmem_free(buf, bst.st_size); 137 138 kobj_close_file(file); 139 } 140 141 /* 142 * Synchronize all pools to disk. This must be called with the namespace lock 143 * held. 144 */ 145 void 146 spa_config_sync(void) 147 { 148 spa_t *spa = NULL; 149 nvlist_t *config; 150 size_t buflen; 151 char *buf; 152 vnode_t *vp; 153 int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX; 154 char pathname[128]; 155 char pathname2[128]; 156 157 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 158 159 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 160 161 /* 162 * Add all known pools to the configuration list, ignoring those with 163 * alternate root paths. 164 */ 165 spa = NULL; 166 while ((spa = spa_next(spa)) != NULL) { 167 mutex_enter(&spa->spa_config_cache_lock); 168 if (spa->spa_config && spa->spa_name && spa->spa_root == NULL) 169 VERIFY(nvlist_add_nvlist(config, spa->spa_name, 170 spa->spa_config) == 0); 171 mutex_exit(&spa->spa_config_cache_lock); 172 } 173 174 /* 175 * Pack the configuration into a buffer. 176 */ 177 VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0); 178 179 buf = kmem_alloc(buflen, KM_SLEEP); 180 181 VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR, 182 KM_SLEEP) == 0); 183 184 /* 185 * Write the configuration to disk. We need to do the traditional 186 * 'write to temporary file, sync, move over original' to make sure we 187 * always have a consistent view of the data. 188 */ 189 (void) snprintf(pathname, sizeof (pathname), "%s/%s", spa_config_dir, 190 ZPOOL_CACHE_TMP); 191 192 if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0) 193 goto out; 194 195 if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE, 196 0, RLIM64_INFINITY, kcred, NULL) == 0 && 197 VOP_FSYNC(vp, FSYNC, kcred) == 0) { 198 (void) snprintf(pathname2, sizeof (pathname2), "%s/%s", 199 spa_config_dir, ZPOOL_CACHE_FILE); 200 (void) vn_rename(pathname, pathname2, UIO_SYSSPACE); 201 } 202 203 (void) VOP_CLOSE(vp, oflags, 1, 0, kcred); 204 VN_RELE(vp); 205 206 out: 207 (void) vn_remove(pathname, UIO_SYSSPACE, RMFILE); 208 spa_config_generation++; 209 210 kmem_free(buf, buflen); 211 nvlist_free(config); 212 } 213 214 /* 215 * Sigh. Inside a local zone, we don't have access to /kernel/drv/zpool.cache, 216 * and we don't want to allow the local zone to see all the pools anyway. 217 * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration 218 * information for all pool visible within the zone. 219 */ 220 nvlist_t * 221 spa_all_configs(uint64_t *generation) 222 { 223 nvlist_t *pools; 224 spa_t *spa; 225 226 if (*generation == spa_config_generation) 227 return (NULL); 228 229 VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0); 230 231 spa = NULL; 232 mutex_enter(&spa_namespace_lock); 233 while ((spa = spa_next(spa)) != NULL) { 234 if (INGLOBALZONE(curproc) || 235 zone_dataset_visible(spa_name(spa), NULL)) { 236 mutex_enter(&spa->spa_config_cache_lock); 237 VERIFY(nvlist_add_nvlist(pools, spa_name(spa), 238 spa->spa_config) == 0); 239 mutex_exit(&spa->spa_config_cache_lock); 240 } 241 } 242 mutex_exit(&spa_namespace_lock); 243 244 *generation = spa_config_generation; 245 246 return (pools); 247 } 248 249 void 250 spa_config_set(spa_t *spa, nvlist_t *config) 251 { 252 mutex_enter(&spa->spa_config_cache_lock); 253 if (spa->spa_config != NULL) 254 nvlist_free(spa->spa_config); 255 spa->spa_config = config; 256 mutex_exit(&spa->spa_config_cache_lock); 257 } 258 259 /* 260 * Generate the pool's configuration based on the current in-core state. 261 * We infer whether to generate a complete config or just one top-level config 262 * based on whether vd is the root vdev. 263 */ 264 nvlist_t * 265 spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats) 266 { 267 nvlist_t *config, *nvroot; 268 vdev_t *rvd = spa->spa_root_vdev; 269 270 if (vd == NULL) 271 vd = rvd; 272 273 /* 274 * If txg is -1, report the current value of spa->spa_config_txg. 275 * If txg is any other non-zero value, update spa->spa_config_txg. 276 */ 277 if (txg == -1ULL) 278 txg = spa->spa_config_txg; 279 else if (txg != 0 && vd == rvd) 280 spa->spa_config_txg = txg; 281 282 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 283 284 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 285 UBERBLOCK_VERSION) == 0); 286 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 287 spa_name(spa)) == 0); 288 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 289 spa_state(spa)) == 0); 290 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 291 txg) == 0); 292 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 293 spa_guid(spa)) == 0); 294 295 if (vd != rvd) { 296 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID, 297 vd->vdev_top->vdev_guid) == 0); 298 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID, 299 vd->vdev_guid) == 0); 300 vd = vd->vdev_top; /* label contains top config */ 301 } 302 303 nvroot = vdev_config_generate(vd, getstats); 304 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 305 nvlist_free(nvroot); 306 307 return (config); 308 } 309