1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 221544Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/spa.h> 29789Sahrens #include <sys/spa_impl.h> 30789Sahrens #include <sys/nvpair.h> 31789Sahrens #include <sys/uio.h> 32789Sahrens #include <sys/fs/zfs.h> 33789Sahrens #include <sys/vdev_impl.h> 34789Sahrens #include <sys/zfs_ioctl.h> 351544Seschrock #ifdef _KERNEL 361544Seschrock #include <sys/kobj.h> 371544Seschrock #endif 381544Seschrock 39789Sahrens /* 40789Sahrens * Pool configuration repository. 41789Sahrens * 42789Sahrens * The configuration for all pools, in addition to being stored on disk, is 43*1635Sbonwick * stored in /etc/zfs/zpool.cache as a packed nvlist. The kernel maintains 44789Sahrens * this list as pools are created, destroyed, or modified. 45789Sahrens * 46789Sahrens * We have a single nvlist which holds all the configuration information. When 47789Sahrens * the module loads, we read this information from the cache and populate the 48789Sahrens * SPA namespace. This namespace is maintained independently in spa.c. 49789Sahrens * Whenever the namespace is modified, or the configuration of a pool is 50789Sahrens * changed, we call spa_config_sync(), which walks through all the active pools 51789Sahrens * and writes the configuration to disk. 52789Sahrens */ 53789Sahrens 54789Sahrens static uint64_t spa_config_generation = 1; 55789Sahrens 56789Sahrens /* 57789Sahrens * This can be overridden in userland to preserve an alternate namespace for 58789Sahrens * userland pools when doing testing. 59789Sahrens */ 60789Sahrens const char *spa_config_dir = ZPOOL_CACHE_DIR; 61789Sahrens 62789Sahrens /* 63789Sahrens * Called when the module is first loaded, this routine loads the configuration 64789Sahrens * file into the SPA namespace. It does not actually open or load the pools; it 65789Sahrens * only populates the namespace. 66789Sahrens */ 67789Sahrens void 68789Sahrens spa_config_load(void) 69789Sahrens { 70789Sahrens void *buf = NULL; 71789Sahrens nvlist_t *nvlist, *child; 72789Sahrens nvpair_t *nvpair; 73789Sahrens spa_t *spa; 74789Sahrens char pathname[128]; 751544Seschrock struct _buf *file; 761544Seschrock struct bootstat bst; 77789Sahrens 78789Sahrens /* 79789Sahrens * Open the configuration file. 80789Sahrens */ 811544Seschrock (void) snprintf(pathname, sizeof (pathname), "%s%s/%s", 82*1635Sbonwick (rootdir != NULL) ? "./" : "", spa_config_dir, ZPOOL_CACHE_FILE); 831544Seschrock 841544Seschrock file = kobj_open_file(pathname); 851544Seschrock if (file == (struct _buf *)-1) 86789Sahrens return; 87789Sahrens 881544Seschrock if (kobj_fstat(file->_fd, &bst) != 0) 891544Seschrock goto out; 901544Seschrock 911544Seschrock buf = kmem_alloc(bst.st_size, KM_SLEEP); 921544Seschrock 93789Sahrens /* 94789Sahrens * Read the nvlist from the file. 95789Sahrens */ 961544Seschrock if (kobj_read_file(file, buf, bst.st_size, 0) < 0) 97789Sahrens goto out; 98789Sahrens 99789Sahrens /* 100789Sahrens * Unpack the nvlist. 101789Sahrens */ 1021544Seschrock if (nvlist_unpack(buf, bst.st_size, &nvlist, KM_SLEEP) != 0) 103789Sahrens goto out; 104789Sahrens 105789Sahrens /* 106789Sahrens * Iterate over all elements in the nvlist, creating a new spa_t for 107789Sahrens * each one with the specified configuration. 108789Sahrens */ 109789Sahrens mutex_enter(&spa_namespace_lock); 110789Sahrens nvpair = NULL; 111789Sahrens while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) { 112789Sahrens 113789Sahrens if (nvpair_type(nvpair) != DATA_TYPE_NVLIST) 114789Sahrens continue; 115789Sahrens 116789Sahrens VERIFY(nvpair_value_nvlist(nvpair, &child) == 0); 117789Sahrens 118789Sahrens if (spa_lookup(nvpair_name(nvpair)) != NULL) 119789Sahrens continue; 120*1635Sbonwick spa = spa_add(nvpair_name(nvpair), NULL); 121789Sahrens 122789Sahrens /* 123789Sahrens * We blindly duplicate the configuration here. If it's 124789Sahrens * invalid, we will catch it when the pool is first opened. 125789Sahrens */ 126789Sahrens VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0); 127789Sahrens } 128789Sahrens mutex_exit(&spa_namespace_lock); 129789Sahrens 130789Sahrens nvlist_free(nvlist); 131789Sahrens 132789Sahrens out: 133789Sahrens if (buf != NULL) 1341544Seschrock kmem_free(buf, bst.st_size); 135789Sahrens 1361544Seschrock kobj_close_file(file); 137789Sahrens } 138789Sahrens 139789Sahrens /* 140789Sahrens * Synchronize all pools to disk. This must be called with the namespace lock 141789Sahrens * held. 142789Sahrens */ 143789Sahrens void 144789Sahrens spa_config_sync(void) 145789Sahrens { 146789Sahrens spa_t *spa = NULL; 147789Sahrens nvlist_t *config; 148789Sahrens size_t buflen; 149789Sahrens char *buf; 150789Sahrens vnode_t *vp; 151789Sahrens int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX; 152789Sahrens char pathname[128]; 153789Sahrens char pathname2[128]; 154789Sahrens 155789Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 156789Sahrens 1571544Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 158789Sahrens 159789Sahrens /* 160789Sahrens * Add all known pools to the configuration list, ignoring those with 161789Sahrens * alternate root paths. 162789Sahrens */ 163789Sahrens spa = NULL; 164789Sahrens while ((spa = spa_next(spa)) != NULL) { 165789Sahrens mutex_enter(&spa->spa_config_cache_lock); 166789Sahrens if (spa->spa_config && spa->spa_name && spa->spa_root == NULL) 167789Sahrens VERIFY(nvlist_add_nvlist(config, spa->spa_name, 168789Sahrens spa->spa_config) == 0); 169789Sahrens mutex_exit(&spa->spa_config_cache_lock); 170789Sahrens } 171789Sahrens 172789Sahrens /* 173789Sahrens * Pack the configuration into a buffer. 174789Sahrens */ 175789Sahrens VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0); 176789Sahrens 177789Sahrens buf = kmem_alloc(buflen, KM_SLEEP); 178789Sahrens 1791544Seschrock VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR, 1801544Seschrock KM_SLEEP) == 0); 181789Sahrens 182789Sahrens /* 183789Sahrens * Write the configuration to disk. We need to do the traditional 184789Sahrens * 'write to temporary file, sync, move over original' to make sure we 185789Sahrens * always have a consistent view of the data. 186789Sahrens */ 187789Sahrens (void) snprintf(pathname, sizeof (pathname), "%s/%s", spa_config_dir, 188789Sahrens ZPOOL_CACHE_TMP); 189789Sahrens 190789Sahrens if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0) 191789Sahrens goto out; 192789Sahrens 193789Sahrens if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE, 194789Sahrens 0, RLIM64_INFINITY, kcred, NULL) == 0 && 195789Sahrens VOP_FSYNC(vp, FSYNC, kcred) == 0) { 196789Sahrens (void) snprintf(pathname2, sizeof (pathname2), "%s/%s", 197789Sahrens spa_config_dir, ZPOOL_CACHE_FILE); 198789Sahrens (void) vn_rename(pathname, pathname2, UIO_SYSSPACE); 199789Sahrens } 200789Sahrens 201789Sahrens (void) VOP_CLOSE(vp, oflags, 1, 0, kcred); 202789Sahrens VN_RELE(vp); 203789Sahrens 204789Sahrens out: 205789Sahrens (void) vn_remove(pathname, UIO_SYSSPACE, RMFILE); 206789Sahrens spa_config_generation++; 207789Sahrens 208789Sahrens kmem_free(buf, buflen); 209789Sahrens nvlist_free(config); 210789Sahrens } 211789Sahrens 212789Sahrens /* 213*1635Sbonwick * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache, 214789Sahrens * and we don't want to allow the local zone to see all the pools anyway. 215789Sahrens * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration 216789Sahrens * information for all pool visible within the zone. 217789Sahrens */ 218789Sahrens nvlist_t * 219789Sahrens spa_all_configs(uint64_t *generation) 220789Sahrens { 221789Sahrens nvlist_t *pools; 222789Sahrens spa_t *spa; 223789Sahrens 224789Sahrens if (*generation == spa_config_generation) 225789Sahrens return (NULL); 226789Sahrens 2271544Seschrock VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0); 228789Sahrens 229789Sahrens spa = NULL; 230789Sahrens mutex_enter(&spa_namespace_lock); 231789Sahrens while ((spa = spa_next(spa)) != NULL) { 232789Sahrens if (INGLOBALZONE(curproc) || 233789Sahrens zone_dataset_visible(spa_name(spa), NULL)) { 234789Sahrens mutex_enter(&spa->spa_config_cache_lock); 235789Sahrens VERIFY(nvlist_add_nvlist(pools, spa_name(spa), 236789Sahrens spa->spa_config) == 0); 237789Sahrens mutex_exit(&spa->spa_config_cache_lock); 238789Sahrens } 239789Sahrens } 240789Sahrens mutex_exit(&spa_namespace_lock); 241789Sahrens 242789Sahrens *generation = spa_config_generation; 243789Sahrens 244789Sahrens return (pools); 245789Sahrens } 246789Sahrens 247789Sahrens void 248789Sahrens spa_config_set(spa_t *spa, nvlist_t *config) 249789Sahrens { 250789Sahrens mutex_enter(&spa->spa_config_cache_lock); 251789Sahrens if (spa->spa_config != NULL) 252789Sahrens nvlist_free(spa->spa_config); 253789Sahrens spa->spa_config = config; 254789Sahrens mutex_exit(&spa->spa_config_cache_lock); 255789Sahrens } 256789Sahrens 257789Sahrens /* 258789Sahrens * Generate the pool's configuration based on the current in-core state. 259789Sahrens * We infer whether to generate a complete config or just one top-level config 260789Sahrens * based on whether vd is the root vdev. 261789Sahrens */ 262789Sahrens nvlist_t * 263789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats) 264789Sahrens { 265789Sahrens nvlist_t *config, *nvroot; 266789Sahrens vdev_t *rvd = spa->spa_root_vdev; 267789Sahrens 268*1635Sbonwick ASSERT(spa_config_held(spa, RW_READER)); 269*1635Sbonwick 270789Sahrens if (vd == NULL) 271789Sahrens vd = rvd; 272789Sahrens 273789Sahrens /* 274789Sahrens * If txg is -1, report the current value of spa->spa_config_txg. 275789Sahrens */ 276789Sahrens if (txg == -1ULL) 277789Sahrens txg = spa->spa_config_txg; 278789Sahrens 2791544Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 280789Sahrens 281789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 282789Sahrens UBERBLOCK_VERSION) == 0); 283789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 284789Sahrens spa_name(spa)) == 0); 285789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 286789Sahrens spa_state(spa)) == 0); 287789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 288789Sahrens txg) == 0); 289789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 290789Sahrens spa_guid(spa)) == 0); 291789Sahrens 292789Sahrens if (vd != rvd) { 293789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID, 294789Sahrens vd->vdev_top->vdev_guid) == 0); 295789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID, 296789Sahrens vd->vdev_guid) == 0); 297789Sahrens vd = vd->vdev_top; /* label contains top config */ 298789Sahrens } 299789Sahrens 300789Sahrens nvroot = vdev_config_generate(vd, getstats); 301789Sahrens VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 302789Sahrens nvlist_free(nvroot); 303789Sahrens 304789Sahrens return (config); 305789Sahrens } 306*1635Sbonwick 307*1635Sbonwick /* 308*1635Sbonwick * Update all disk labels, generate a fresh config based on the current 309*1635Sbonwick * in-core state, and sync the global config cache. 310*1635Sbonwick */ 311*1635Sbonwick void 312*1635Sbonwick spa_config_update(spa_t *spa, int what) 313*1635Sbonwick { 314*1635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 315*1635Sbonwick uint64_t txg; 316*1635Sbonwick int c; 317*1635Sbonwick 318*1635Sbonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 319*1635Sbonwick 320*1635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 321*1635Sbonwick txg = spa_last_synced_txg(spa) + 1; 322*1635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) { 323*1635Sbonwick vdev_config_dirty(rvd); 324*1635Sbonwick } else { 325*1635Sbonwick /* 326*1635Sbonwick * If we have top-level vdevs that were added but have 327*1635Sbonwick * not yet been prepared for allocation, do that now. 328*1635Sbonwick * (It's safe now because the config cache is up to date, 329*1635Sbonwick * so it will be able to translate the new DVAs.) 330*1635Sbonwick * See comments in spa_vdev_add() for full details. 331*1635Sbonwick */ 332*1635Sbonwick for (c = 0; c < rvd->vdev_children; c++) { 333*1635Sbonwick vdev_t *tvd = rvd->vdev_child[c]; 334*1635Sbonwick if (tvd->vdev_ms_array == 0) { 335*1635Sbonwick vdev_init(tvd, txg); 336*1635Sbonwick vdev_config_dirty(tvd); 337*1635Sbonwick } 338*1635Sbonwick } 339*1635Sbonwick } 340*1635Sbonwick spa_config_exit(spa, FTAG); 341*1635Sbonwick 342*1635Sbonwick /* 343*1635Sbonwick * Wait for the mosconfig to be regenerated and synced. 344*1635Sbonwick */ 345*1635Sbonwick txg_wait_synced(spa->spa_dsl_pool, txg); 346*1635Sbonwick 347*1635Sbonwick /* 348*1635Sbonwick * Update the global config cache to reflect the new mosconfig. 349*1635Sbonwick */ 350*1635Sbonwick spa_config_sync(); 351*1635Sbonwick 352*1635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) 353*1635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS); 354*1635Sbonwick } 355