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 */ 212082Seschrock 22789Sahrens /* 236423Sgw25295 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #include <sys/spa.h> 28789Sahrens #include <sys/spa_impl.h> 29789Sahrens #include <sys/nvpair.h> 30789Sahrens #include <sys/uio.h> 31789Sahrens #include <sys/fs/zfs.h> 32789Sahrens #include <sys/vdev_impl.h> 33789Sahrens #include <sys/zfs_ioctl.h> 343975Sek110237 #include <sys/utsname.h> 353975Sek110237 #include <sys/systeminfo.h> 363975Sek110237 #include <sys/sunddi.h> 371544Seschrock #ifdef _KERNEL 381544Seschrock #include <sys/kobj.h> 391544Seschrock #endif 401544Seschrock 41789Sahrens /* 42789Sahrens * Pool configuration repository. 43789Sahrens * 445363Seschrock * Pool configuration is stored as a packed nvlist on the filesystem. By 455363Seschrock * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot 465363Seschrock * (when the ZFS module is loaded). Pools can also have the 'cachefile' 475363Seschrock * property set that allows them to be stored in an alternate location until 485363Seschrock * the control of external software. 49789Sahrens * 505363Seschrock * For each cache file, we have a single nvlist which holds all the 515363Seschrock * configuration information. When the module loads, we read this information 525363Seschrock * from /etc/zfs/zpool.cache and populate the SPA namespace. This namespace is 535363Seschrock * maintained independently in spa.c. Whenever the namespace is modified, or 545363Seschrock * the configuration of a pool is changed, we call spa_config_sync(), which 555363Seschrock * walks through all the active pools and writes the configuration to disk. 56789Sahrens */ 57789Sahrens 58789Sahrens static uint64_t spa_config_generation = 1; 59789Sahrens 60789Sahrens /* 61789Sahrens * This can be overridden in userland to preserve an alternate namespace for 62789Sahrens * userland pools when doing testing. 63789Sahrens */ 646643Seschrock const char *spa_config_path = ZPOOL_CACHE; 65789Sahrens 66789Sahrens /* 67789Sahrens * Called when the module is first loaded, this routine loads the configuration 68789Sahrens * file into the SPA namespace. It does not actually open or load the pools; it 69789Sahrens * only populates the namespace. 70789Sahrens */ 71789Sahrens void 72789Sahrens spa_config_load(void) 73789Sahrens { 74789Sahrens void *buf = NULL; 75789Sahrens nvlist_t *nvlist, *child; 76789Sahrens nvpair_t *nvpair; 77789Sahrens spa_t *spa; 78*7754SJeff.Bonwick@Sun.COM char *pathname; 791544Seschrock struct _buf *file; 803912Slling uint64_t fsize; 81789Sahrens 82789Sahrens /* 83789Sahrens * Open the configuration file. 84789Sahrens */ 85*7754SJeff.Bonwick@Sun.COM pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 86*7754SJeff.Bonwick@Sun.COM 87*7754SJeff.Bonwick@Sun.COM (void) snprintf(pathname, MAXPATHLEN, "%s%s", 886643Seschrock (rootdir != NULL) ? "./" : "", spa_config_path); 891544Seschrock 901544Seschrock file = kobj_open_file(pathname); 91*7754SJeff.Bonwick@Sun.COM 92*7754SJeff.Bonwick@Sun.COM kmem_free(pathname, MAXPATHLEN); 93*7754SJeff.Bonwick@Sun.COM 941544Seschrock if (file == (struct _buf *)-1) 95789Sahrens return; 96789Sahrens 973912Slling if (kobj_get_filesize(file, &fsize) != 0) 981544Seschrock goto out; 991544Seschrock 1003912Slling buf = kmem_alloc(fsize, KM_SLEEP); 1011544Seschrock 102789Sahrens /* 103789Sahrens * Read the nvlist from the file. 104789Sahrens */ 1053912Slling if (kobj_read_file(file, buf, fsize, 0) < 0) 106789Sahrens goto out; 107789Sahrens 108789Sahrens /* 109789Sahrens * Unpack the nvlist. 110789Sahrens */ 1113912Slling if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0) 112789Sahrens goto out; 113789Sahrens 114789Sahrens /* 115789Sahrens * Iterate over all elements in the nvlist, creating a new spa_t for 116789Sahrens * each one with the specified configuration. 117789Sahrens */ 118789Sahrens mutex_enter(&spa_namespace_lock); 119789Sahrens nvpair = NULL; 120789Sahrens while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) { 121789Sahrens 122789Sahrens if (nvpair_type(nvpair) != DATA_TYPE_NVLIST) 123789Sahrens continue; 124789Sahrens 125789Sahrens VERIFY(nvpair_value_nvlist(nvpair, &child) == 0); 126789Sahrens 127789Sahrens if (spa_lookup(nvpair_name(nvpair)) != NULL) 128789Sahrens continue; 1291635Sbonwick spa = spa_add(nvpair_name(nvpair), NULL); 130789Sahrens 131789Sahrens /* 132789Sahrens * We blindly duplicate the configuration here. If it's 133789Sahrens * invalid, we will catch it when the pool is first opened. 134789Sahrens */ 135789Sahrens VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0); 136789Sahrens } 137789Sahrens mutex_exit(&spa_namespace_lock); 138789Sahrens 139789Sahrens nvlist_free(nvlist); 140789Sahrens 141789Sahrens out: 142789Sahrens if (buf != NULL) 1433912Slling kmem_free(buf, fsize); 144789Sahrens 1451544Seschrock kobj_close_file(file); 146789Sahrens } 147789Sahrens 1486643Seschrock static void 1496643Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl) 1505363Seschrock { 151789Sahrens size_t buflen; 152789Sahrens char *buf; 153789Sahrens vnode_t *vp; 154789Sahrens int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX; 155*7754SJeff.Bonwick@Sun.COM char *temp; 1566643Seschrock 1576643Seschrock /* 1586643Seschrock * If the nvlist is empty (NULL), then remove the old cachefile. 1596643Seschrock */ 1606643Seschrock if (nvl == NULL) { 1616643Seschrock (void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE); 1626643Seschrock return; 1636643Seschrock } 164789Sahrens 165789Sahrens /* 166789Sahrens * Pack the configuration into a buffer. 167789Sahrens */ 1686643Seschrock VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0); 169789Sahrens 170789Sahrens buf = kmem_alloc(buflen, KM_SLEEP); 171*7754SJeff.Bonwick@Sun.COM temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 172789Sahrens 1736643Seschrock VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR, 1741544Seschrock KM_SLEEP) == 0); 175789Sahrens 176789Sahrens /* 177789Sahrens * Write the configuration to disk. We need to do the traditional 178789Sahrens * 'write to temporary file, sync, move over original' to make sure we 179789Sahrens * always have a consistent view of the data. 180789Sahrens */ 181*7754SJeff.Bonwick@Sun.COM (void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path); 182789Sahrens 183*7754SJeff.Bonwick@Sun.COM if (vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) == 0) { 184*7754SJeff.Bonwick@Sun.COM if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE, 185*7754SJeff.Bonwick@Sun.COM 0, RLIM64_INFINITY, kcred, NULL) == 0 && 186*7754SJeff.Bonwick@Sun.COM VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) { 187*7754SJeff.Bonwick@Sun.COM (void) vn_rename(temp, dp->scd_path, UIO_SYSSPACE); 188*7754SJeff.Bonwick@Sun.COM } 189*7754SJeff.Bonwick@Sun.COM (void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL); 190*7754SJeff.Bonwick@Sun.COM VN_RELE(vp); 191789Sahrens } 192789Sahrens 193*7754SJeff.Bonwick@Sun.COM (void) vn_remove(temp, UIO_SYSSPACE, RMFILE); 194789Sahrens 1955363Seschrock kmem_free(buf, buflen); 196*7754SJeff.Bonwick@Sun.COM kmem_free(temp, MAXPATHLEN); 1975363Seschrock } 1985363Seschrock 1995363Seschrock /* 2006643Seschrock * Synchronize pool configuration to disk. This must be called with the 2016643Seschrock * namespace lock held. 2025363Seschrock */ 2035363Seschrock void 2046643Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent) 2055363Seschrock { 2066643Seschrock spa_config_dirent_t *dp, *tdp; 2076643Seschrock nvlist_t *nvl; 2085363Seschrock 2095363Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 210789Sahrens 2116643Seschrock /* 2126643Seschrock * Iterate over all cachefiles for the pool, past or present. When the 2136643Seschrock * cachefile is changed, the new one is pushed onto this list, allowing 2146643Seschrock * us to update previous cachefiles that no longer contain this pool. 2156643Seschrock */ 2166643Seschrock for (dp = list_head(&target->spa_config_list); dp != NULL; 2176643Seschrock dp = list_next(&target->spa_config_list, dp)) { 218*7754SJeff.Bonwick@Sun.COM spa_t *spa = NULL; 2196643Seschrock if (dp->scd_path == NULL) 2206643Seschrock continue; 2216643Seschrock 2226643Seschrock /* 2236643Seschrock * Iterate over all pools, adding any matching pools to 'nvl'. 2246643Seschrock */ 2256643Seschrock nvl = NULL; 2266643Seschrock while ((spa = spa_next(spa)) != NULL) { 2276643Seschrock if (spa == target && removing) 2286643Seschrock continue; 2296643Seschrock 230*7754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 2316643Seschrock tdp = list_head(&spa->spa_config_list); 232*7754SJeff.Bonwick@Sun.COM if (spa->spa_config == NULL || 233*7754SJeff.Bonwick@Sun.COM tdp->scd_path == NULL || 234*7754SJeff.Bonwick@Sun.COM strcmp(tdp->scd_path, dp->scd_path) != 0) { 235*7754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 2366643Seschrock continue; 237*7754SJeff.Bonwick@Sun.COM } 2386643Seschrock 2396643Seschrock if (nvl == NULL) 2406643Seschrock VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, 2416643Seschrock KM_SLEEP) == 0); 2426643Seschrock 2436643Seschrock VERIFY(nvlist_add_nvlist(nvl, spa->spa_name, 2446643Seschrock spa->spa_config) == 0); 245*7754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 2466643Seschrock } 2476643Seschrock 2486643Seschrock spa_config_write(dp, nvl); 2496643Seschrock nvlist_free(nvl); 2506643Seschrock } 2515363Seschrock 2525363Seschrock /* 2536643Seschrock * Remove any config entries older than the current one. 2545363Seschrock */ 2556643Seschrock dp = list_head(&target->spa_config_list); 2566643Seschrock while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) { 2576643Seschrock list_remove(&target->spa_config_list, tdp); 2586643Seschrock if (tdp->scd_path != NULL) 2596643Seschrock spa_strfree(tdp->scd_path); 2606643Seschrock kmem_free(tdp, sizeof (spa_config_dirent_t)); 2615363Seschrock } 2625363Seschrock 2635363Seschrock spa_config_generation++; 2646643Seschrock 2656643Seschrock if (postsysevent) 2666643Seschrock spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC); 267789Sahrens } 268789Sahrens 269789Sahrens /* 2701635Sbonwick * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache, 271789Sahrens * and we don't want to allow the local zone to see all the pools anyway. 272789Sahrens * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration 273789Sahrens * information for all pool visible within the zone. 274789Sahrens */ 275789Sahrens nvlist_t * 276789Sahrens spa_all_configs(uint64_t *generation) 277789Sahrens { 278789Sahrens nvlist_t *pools; 279*7754SJeff.Bonwick@Sun.COM spa_t *spa = NULL; 280789Sahrens 281789Sahrens if (*generation == spa_config_generation) 282789Sahrens return (NULL); 283789Sahrens 2841544Seschrock VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0); 285789Sahrens 286789Sahrens mutex_enter(&spa_namespace_lock); 287789Sahrens while ((spa = spa_next(spa)) != NULL) { 288789Sahrens if (INGLOBALZONE(curproc) || 289789Sahrens zone_dataset_visible(spa_name(spa), NULL)) { 290*7754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 291789Sahrens VERIFY(nvlist_add_nvlist(pools, spa_name(spa), 292789Sahrens spa->spa_config) == 0); 293*7754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 294789Sahrens } 295789Sahrens } 296*7754SJeff.Bonwick@Sun.COM *generation = spa_config_generation; 297789Sahrens mutex_exit(&spa_namespace_lock); 298789Sahrens 299789Sahrens return (pools); 300789Sahrens } 301789Sahrens 302789Sahrens void 303789Sahrens spa_config_set(spa_t *spa, nvlist_t *config) 304789Sahrens { 305*7754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 306789Sahrens if (spa->spa_config != NULL) 307789Sahrens nvlist_free(spa->spa_config); 308789Sahrens spa->spa_config = config; 309*7754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 310789Sahrens } 311789Sahrens 312789Sahrens /* 313789Sahrens * Generate the pool's configuration based on the current in-core state. 314789Sahrens * We infer whether to generate a complete config or just one top-level config 315789Sahrens * based on whether vd is the root vdev. 316789Sahrens */ 317789Sahrens nvlist_t * 318789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats) 319789Sahrens { 320789Sahrens nvlist_t *config, *nvroot; 321789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3223975Sek110237 unsigned long hostid = 0; 323*7754SJeff.Bonwick@Sun.COM boolean_t locked = B_FALSE; 324789Sahrens 325*7754SJeff.Bonwick@Sun.COM if (vd == NULL) { 326*7754SJeff.Bonwick@Sun.COM vd = rvd; 327*7754SJeff.Bonwick@Sun.COM locked = B_TRUE; 328*7754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 329*7754SJeff.Bonwick@Sun.COM } 3301635Sbonwick 331*7754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) == 332*7754SJeff.Bonwick@Sun.COM (SCL_CONFIG | SCL_STATE)); 333789Sahrens 334789Sahrens /* 335789Sahrens * If txg is -1, report the current value of spa->spa_config_txg. 336789Sahrens */ 337789Sahrens if (txg == -1ULL) 338789Sahrens txg = spa->spa_config_txg; 339789Sahrens 3401544Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 341789Sahrens 342789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 3432082Seschrock spa_version(spa)) == 0); 344789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 345789Sahrens spa_name(spa)) == 0); 346789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 347789Sahrens spa_state(spa)) == 0); 348789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 349789Sahrens txg) == 0); 350789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 351789Sahrens spa_guid(spa)) == 0); 3523975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &hostid); 3534178Slling if (hostid != 0) { 3544178Slling VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 3554527Sperrin hostid) == 0); 3564178Slling } 3573975Sek110237 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 3583975Sek110237 utsname.nodename) == 0); 359789Sahrens 360789Sahrens if (vd != rvd) { 361789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID, 362789Sahrens vd->vdev_top->vdev_guid) == 0); 363789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID, 364789Sahrens vd->vdev_guid) == 0); 3652082Seschrock if (vd->vdev_isspare) 3662082Seschrock VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE, 3672082Seschrock 1ULL) == 0); 3684527Sperrin if (vd->vdev_islog) 3694527Sperrin VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG, 3704527Sperrin 1ULL) == 0); 371789Sahrens vd = vd->vdev_top; /* label contains top config */ 372789Sahrens } 373789Sahrens 3745450Sbrendan nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE, B_FALSE); 375789Sahrens VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 376789Sahrens nvlist_free(nvroot); 377789Sahrens 378*7754SJeff.Bonwick@Sun.COM if (locked) 379*7754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 380*7754SJeff.Bonwick@Sun.COM 381789Sahrens return (config); 382789Sahrens } 3831635Sbonwick 3841635Sbonwick /* 3856423Sgw25295 * For a pool that's not currently a booting rootpool, update all disk labels, 3866423Sgw25295 * generate a fresh config based on the current in-core state, and sync the 3876423Sgw25295 * global config cache. 3881635Sbonwick */ 3891635Sbonwick void 3901635Sbonwick spa_config_update(spa_t *spa, int what) 3911635Sbonwick { 3926423Sgw25295 spa_config_update_common(spa, what, FALSE); 3936423Sgw25295 } 3946423Sgw25295 3956423Sgw25295 /* 3966423Sgw25295 * Update all disk labels, generate a fresh config based on the current 3976423Sgw25295 * in-core state, and sync the global config cache (do not sync the config 3986423Sgw25295 * cache if this is a booting rootpool). 3996423Sgw25295 */ 4006423Sgw25295 void 4016423Sgw25295 spa_config_update_common(spa_t *spa, int what, boolean_t isroot) 4026423Sgw25295 { 4031635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 4041635Sbonwick uint64_t txg; 4051635Sbonwick int c; 4061635Sbonwick 4071635Sbonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4081635Sbonwick 409*7754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4101635Sbonwick txg = spa_last_synced_txg(spa) + 1; 4111635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) { 4121635Sbonwick vdev_config_dirty(rvd); 4131635Sbonwick } else { 4141635Sbonwick /* 4151635Sbonwick * If we have top-level vdevs that were added but have 4161635Sbonwick * not yet been prepared for allocation, do that now. 4171635Sbonwick * (It's safe now because the config cache is up to date, 4181635Sbonwick * so it will be able to translate the new DVAs.) 4191635Sbonwick * See comments in spa_vdev_add() for full details. 4201635Sbonwick */ 4211635Sbonwick for (c = 0; c < rvd->vdev_children; c++) { 4221635Sbonwick vdev_t *tvd = rvd->vdev_child[c]; 4231635Sbonwick if (tvd->vdev_ms_array == 0) { 4241635Sbonwick vdev_init(tvd, txg); 4251635Sbonwick vdev_config_dirty(tvd); 4261635Sbonwick } 4271635Sbonwick } 4281635Sbonwick } 429*7754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 4301635Sbonwick 4311635Sbonwick /* 4321635Sbonwick * Wait for the mosconfig to be regenerated and synced. 4331635Sbonwick */ 4341635Sbonwick txg_wait_synced(spa->spa_dsl_pool, txg); 4351635Sbonwick 4361635Sbonwick /* 4371635Sbonwick * Update the global config cache to reflect the new mosconfig. 4381635Sbonwick */ 4396423Sgw25295 if (!isroot) 4406643Seschrock spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL); 4411635Sbonwick 4421635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) 4436423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_VDEVS, isroot); 4441635Sbonwick } 445