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 */ 21*2082Seschrock 22789Sahrens /* 231544Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/spa_impl.h> 31789Sahrens #include <sys/nvpair.h> 32789Sahrens #include <sys/uio.h> 33789Sahrens #include <sys/fs/zfs.h> 34789Sahrens #include <sys/vdev_impl.h> 35789Sahrens #include <sys/zfs_ioctl.h> 361544Seschrock #ifdef _KERNEL 371544Seschrock #include <sys/kobj.h> 381544Seschrock #endif 391544Seschrock 40789Sahrens /* 41789Sahrens * Pool configuration repository. 42789Sahrens * 43789Sahrens * The configuration for all pools, in addition to being stored on disk, is 441635Sbonwick * stored in /etc/zfs/zpool.cache as a packed nvlist. The kernel maintains 45789Sahrens * this list as pools are created, destroyed, or modified. 46789Sahrens * 47789Sahrens * We have a single nvlist which holds all the configuration information. When 48789Sahrens * the module loads, we read this information from the cache and populate the 49789Sahrens * SPA namespace. This namespace is maintained independently in spa.c. 50789Sahrens * Whenever the namespace is modified, or the configuration of a pool is 51789Sahrens * changed, we call spa_config_sync(), which walks through all the active pools 52789Sahrens * and writes the configuration to disk. 53789Sahrens */ 54789Sahrens 55789Sahrens static uint64_t spa_config_generation = 1; 56789Sahrens 57789Sahrens /* 58789Sahrens * This can be overridden in userland to preserve an alternate namespace for 59789Sahrens * userland pools when doing testing. 60789Sahrens */ 61789Sahrens const char *spa_config_dir = ZPOOL_CACHE_DIR; 62789Sahrens 63789Sahrens /* 64789Sahrens * Called when the module is first loaded, this routine loads the configuration 65789Sahrens * file into the SPA namespace. It does not actually open or load the pools; it 66789Sahrens * only populates the namespace. 67789Sahrens */ 68789Sahrens void 69789Sahrens spa_config_load(void) 70789Sahrens { 71789Sahrens void *buf = NULL; 72789Sahrens nvlist_t *nvlist, *child; 73789Sahrens nvpair_t *nvpair; 74789Sahrens spa_t *spa; 75789Sahrens char pathname[128]; 761544Seschrock struct _buf *file; 771544Seschrock struct bootstat bst; 78789Sahrens 79789Sahrens /* 80789Sahrens * Open the configuration file. 81789Sahrens */ 821544Seschrock (void) snprintf(pathname, sizeof (pathname), "%s%s/%s", 831635Sbonwick (rootdir != NULL) ? "./" : "", spa_config_dir, ZPOOL_CACHE_FILE); 841544Seschrock 851544Seschrock file = kobj_open_file(pathname); 861544Seschrock if (file == (struct _buf *)-1) 87789Sahrens return; 88789Sahrens 891544Seschrock if (kobj_fstat(file->_fd, &bst) != 0) 901544Seschrock goto out; 911544Seschrock 921544Seschrock buf = kmem_alloc(bst.st_size, KM_SLEEP); 931544Seschrock 94789Sahrens /* 95789Sahrens * Read the nvlist from the file. 96789Sahrens */ 971544Seschrock if (kobj_read_file(file, buf, bst.st_size, 0) < 0) 98789Sahrens goto out; 99789Sahrens 100789Sahrens /* 101789Sahrens * Unpack the nvlist. 102789Sahrens */ 1031544Seschrock if (nvlist_unpack(buf, bst.st_size, &nvlist, KM_SLEEP) != 0) 104789Sahrens goto out; 105789Sahrens 106789Sahrens /* 107789Sahrens * Iterate over all elements in the nvlist, creating a new spa_t for 108789Sahrens * each one with the specified configuration. 109789Sahrens */ 110789Sahrens mutex_enter(&spa_namespace_lock); 111789Sahrens nvpair = NULL; 112789Sahrens while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) { 113789Sahrens 114789Sahrens if (nvpair_type(nvpair) != DATA_TYPE_NVLIST) 115789Sahrens continue; 116789Sahrens 117789Sahrens VERIFY(nvpair_value_nvlist(nvpair, &child) == 0); 118789Sahrens 119789Sahrens if (spa_lookup(nvpair_name(nvpair)) != NULL) 120789Sahrens continue; 1211635Sbonwick spa = spa_add(nvpair_name(nvpair), NULL); 122789Sahrens 123789Sahrens /* 124789Sahrens * We blindly duplicate the configuration here. If it's 125789Sahrens * invalid, we will catch it when the pool is first opened. 126789Sahrens */ 127789Sahrens VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0); 128789Sahrens } 129789Sahrens mutex_exit(&spa_namespace_lock); 130789Sahrens 131789Sahrens nvlist_free(nvlist); 132789Sahrens 133789Sahrens out: 134789Sahrens if (buf != NULL) 1351544Seschrock kmem_free(buf, bst.st_size); 136789Sahrens 1371544Seschrock kobj_close_file(file); 138789Sahrens } 139789Sahrens 140789Sahrens /* 141789Sahrens * Synchronize all pools to disk. This must be called with the namespace lock 142789Sahrens * held. 143789Sahrens */ 144789Sahrens void 145789Sahrens spa_config_sync(void) 146789Sahrens { 147789Sahrens spa_t *spa = NULL; 148789Sahrens nvlist_t *config; 149789Sahrens size_t buflen; 150789Sahrens char *buf; 151789Sahrens vnode_t *vp; 152789Sahrens int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX; 153789Sahrens char pathname[128]; 154789Sahrens char pathname2[128]; 155789Sahrens 156789Sahrens ASSERT(MUTEX_HELD(&spa_namespace_lock)); 157789Sahrens 1581544Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 159789Sahrens 160789Sahrens /* 161789Sahrens * Add all known pools to the configuration list, ignoring those with 162789Sahrens * alternate root paths. 163789Sahrens */ 164789Sahrens spa = NULL; 165789Sahrens while ((spa = spa_next(spa)) != NULL) { 166789Sahrens mutex_enter(&spa->spa_config_cache_lock); 167789Sahrens if (spa->spa_config && spa->spa_name && spa->spa_root == NULL) 168789Sahrens VERIFY(nvlist_add_nvlist(config, spa->spa_name, 169789Sahrens spa->spa_config) == 0); 170789Sahrens mutex_exit(&spa->spa_config_cache_lock); 171789Sahrens } 172789Sahrens 173789Sahrens /* 174789Sahrens * Pack the configuration into a buffer. 175789Sahrens */ 176789Sahrens VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0); 177789Sahrens 178789Sahrens buf = kmem_alloc(buflen, KM_SLEEP); 179789Sahrens 1801544Seschrock VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR, 1811544Seschrock KM_SLEEP) == 0); 182789Sahrens 183789Sahrens /* 184789Sahrens * Write the configuration to disk. We need to do the traditional 185789Sahrens * 'write to temporary file, sync, move over original' to make sure we 186789Sahrens * always have a consistent view of the data. 187789Sahrens */ 188789Sahrens (void) snprintf(pathname, sizeof (pathname), "%s/%s", spa_config_dir, 189789Sahrens ZPOOL_CACHE_TMP); 190789Sahrens 191789Sahrens if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0) 192789Sahrens goto out; 193789Sahrens 194789Sahrens if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE, 195789Sahrens 0, RLIM64_INFINITY, kcred, NULL) == 0 && 196789Sahrens VOP_FSYNC(vp, FSYNC, kcred) == 0) { 197789Sahrens (void) snprintf(pathname2, sizeof (pathname2), "%s/%s", 198789Sahrens spa_config_dir, ZPOOL_CACHE_FILE); 199789Sahrens (void) vn_rename(pathname, pathname2, UIO_SYSSPACE); 200789Sahrens } 201789Sahrens 202789Sahrens (void) VOP_CLOSE(vp, oflags, 1, 0, kcred); 203789Sahrens VN_RELE(vp); 204789Sahrens 205789Sahrens out: 206789Sahrens (void) vn_remove(pathname, UIO_SYSSPACE, RMFILE); 207789Sahrens spa_config_generation++; 208789Sahrens 209789Sahrens kmem_free(buf, buflen); 210789Sahrens nvlist_free(config); 211789Sahrens } 212789Sahrens 213789Sahrens /* 2141635Sbonwick * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache, 215789Sahrens * and we don't want to allow the local zone to see all the pools anyway. 216789Sahrens * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration 217789Sahrens * information for all pool visible within the zone. 218789Sahrens */ 219789Sahrens nvlist_t * 220789Sahrens spa_all_configs(uint64_t *generation) 221789Sahrens { 222789Sahrens nvlist_t *pools; 223789Sahrens spa_t *spa; 224789Sahrens 225789Sahrens if (*generation == spa_config_generation) 226789Sahrens return (NULL); 227789Sahrens 2281544Seschrock VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0); 229789Sahrens 230789Sahrens spa = NULL; 231789Sahrens mutex_enter(&spa_namespace_lock); 232789Sahrens while ((spa = spa_next(spa)) != NULL) { 233789Sahrens if (INGLOBALZONE(curproc) || 234789Sahrens zone_dataset_visible(spa_name(spa), NULL)) { 235789Sahrens mutex_enter(&spa->spa_config_cache_lock); 236789Sahrens VERIFY(nvlist_add_nvlist(pools, spa_name(spa), 237789Sahrens spa->spa_config) == 0); 238789Sahrens mutex_exit(&spa->spa_config_cache_lock); 239789Sahrens } 240789Sahrens } 241789Sahrens mutex_exit(&spa_namespace_lock); 242789Sahrens 243789Sahrens *generation = spa_config_generation; 244789Sahrens 245789Sahrens return (pools); 246789Sahrens } 247789Sahrens 248789Sahrens void 249789Sahrens spa_config_set(spa_t *spa, nvlist_t *config) 250789Sahrens { 251789Sahrens mutex_enter(&spa->spa_config_cache_lock); 252789Sahrens if (spa->spa_config != NULL) 253789Sahrens nvlist_free(spa->spa_config); 254789Sahrens spa->spa_config = config; 255789Sahrens mutex_exit(&spa->spa_config_cache_lock); 256789Sahrens } 257789Sahrens 258789Sahrens /* 259789Sahrens * Generate the pool's configuration based on the current in-core state. 260789Sahrens * We infer whether to generate a complete config or just one top-level config 261789Sahrens * based on whether vd is the root vdev. 262789Sahrens */ 263789Sahrens nvlist_t * 264789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats) 265789Sahrens { 266789Sahrens nvlist_t *config, *nvroot; 267789Sahrens vdev_t *rvd = spa->spa_root_vdev; 268789Sahrens 2691635Sbonwick ASSERT(spa_config_held(spa, RW_READER)); 2701635Sbonwick 271789Sahrens if (vd == NULL) 272789Sahrens vd = rvd; 273789Sahrens 274789Sahrens /* 275789Sahrens * If txg is -1, report the current value of spa->spa_config_txg. 276789Sahrens */ 277789Sahrens if (txg == -1ULL) 278789Sahrens txg = spa->spa_config_txg; 279789Sahrens 2801544Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 281789Sahrens 282789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 283*2082Seschrock spa_version(spa)) == 0); 284789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 285789Sahrens spa_name(spa)) == 0); 286789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 287789Sahrens spa_state(spa)) == 0); 288789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 289789Sahrens txg) == 0); 290789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 291789Sahrens spa_guid(spa)) == 0); 292789Sahrens 293789Sahrens if (vd != rvd) { 294789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID, 295789Sahrens vd->vdev_top->vdev_guid) == 0); 296789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID, 297789Sahrens vd->vdev_guid) == 0); 298*2082Seschrock if (vd->vdev_isspare) 299*2082Seschrock VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE, 300*2082Seschrock 1ULL) == 0); 301789Sahrens vd = vd->vdev_top; /* label contains top config */ 302789Sahrens } 303789Sahrens 304*2082Seschrock nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE); 305789Sahrens VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 306789Sahrens nvlist_free(nvroot); 307789Sahrens 308789Sahrens return (config); 309789Sahrens } 3101635Sbonwick 3111635Sbonwick /* 3121635Sbonwick * Update all disk labels, generate a fresh config based on the current 3131635Sbonwick * in-core state, and sync the global config cache. 3141635Sbonwick */ 3151635Sbonwick void 3161635Sbonwick spa_config_update(spa_t *spa, int what) 3171635Sbonwick { 3181635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3191635Sbonwick uint64_t txg; 3201635Sbonwick int c; 3211635Sbonwick 3221635Sbonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 3231635Sbonwick 3241635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 3251635Sbonwick txg = spa_last_synced_txg(spa) + 1; 3261635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) { 3271635Sbonwick vdev_config_dirty(rvd); 3281635Sbonwick } else { 3291635Sbonwick /* 3301635Sbonwick * If we have top-level vdevs that were added but have 3311635Sbonwick * not yet been prepared for allocation, do that now. 3321635Sbonwick * (It's safe now because the config cache is up to date, 3331635Sbonwick * so it will be able to translate the new DVAs.) 3341635Sbonwick * See comments in spa_vdev_add() for full details. 3351635Sbonwick */ 3361635Sbonwick for (c = 0; c < rvd->vdev_children; c++) { 3371635Sbonwick vdev_t *tvd = rvd->vdev_child[c]; 3381635Sbonwick if (tvd->vdev_ms_array == 0) { 3391635Sbonwick vdev_init(tvd, txg); 3401635Sbonwick vdev_config_dirty(tvd); 3411635Sbonwick } 3421635Sbonwick } 3431635Sbonwick } 3441635Sbonwick spa_config_exit(spa, FTAG); 3451635Sbonwick 3461635Sbonwick /* 3471635Sbonwick * Wait for the mosconfig to be regenerated and synced. 3481635Sbonwick */ 3491635Sbonwick txg_wait_synced(spa->spa_dsl_pool, txg); 3501635Sbonwick 3511635Sbonwick /* 3521635Sbonwick * Update the global config cache to reflect the new mosconfig. 3531635Sbonwick */ 3541635Sbonwick spa_config_sync(); 3551635Sbonwick 3561635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) 3571635Sbonwick spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS); 3581635Sbonwick } 359