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 #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> 363975Sek110237 #include <sys/utsname.h> 373975Sek110237 #include <sys/systeminfo.h> 383975Sek110237 #include <sys/sunddi.h> 391544Seschrock #ifdef _KERNEL 401544Seschrock #include <sys/kobj.h> 411544Seschrock #endif 421544Seschrock 43789Sahrens /* 44789Sahrens * Pool configuration repository. 45789Sahrens * 465363Seschrock * Pool configuration is stored as a packed nvlist on the filesystem. By 475363Seschrock * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot 485363Seschrock * (when the ZFS module is loaded). Pools can also have the 'cachefile' 495363Seschrock * property set that allows them to be stored in an alternate location until 505363Seschrock * the control of external software. 51789Sahrens * 525363Seschrock * For each cache file, we have a single nvlist which holds all the 535363Seschrock * configuration information. When the module loads, we read this information 545363Seschrock * from /etc/zfs/zpool.cache and populate the SPA namespace. This namespace is 555363Seschrock * maintained independently in spa.c. Whenever the namespace is modified, or 565363Seschrock * the configuration of a pool is changed, we call spa_config_sync(), which 575363Seschrock * walks through all the active pools and writes the configuration to disk. 58789Sahrens */ 59789Sahrens 60789Sahrens static uint64_t spa_config_generation = 1; 61789Sahrens 62789Sahrens /* 63789Sahrens * This can be overridden in userland to preserve an alternate namespace for 64789Sahrens * userland pools when doing testing. 65789Sahrens */ 66*6643Seschrock const char *spa_config_path = ZPOOL_CACHE; 67789Sahrens 68789Sahrens /* 69789Sahrens * Called when the module is first loaded, this routine loads the configuration 70789Sahrens * file into the SPA namespace. It does not actually open or load the pools; it 71789Sahrens * only populates the namespace. 72789Sahrens */ 73789Sahrens void 74789Sahrens spa_config_load(void) 75789Sahrens { 76789Sahrens void *buf = NULL; 77789Sahrens nvlist_t *nvlist, *child; 78789Sahrens nvpair_t *nvpair; 79789Sahrens spa_t *spa; 80789Sahrens char pathname[128]; 811544Seschrock struct _buf *file; 823912Slling uint64_t fsize; 83789Sahrens 84789Sahrens /* 85789Sahrens * Open the configuration file. 86789Sahrens */ 87*6643Seschrock (void) snprintf(pathname, sizeof (pathname), "%s%s", 88*6643Seschrock (rootdir != NULL) ? "./" : "", spa_config_path); 891544Seschrock 901544Seschrock file = kobj_open_file(pathname); 911544Seschrock if (file == (struct _buf *)-1) 92789Sahrens return; 93789Sahrens 943912Slling if (kobj_get_filesize(file, &fsize) != 0) 951544Seschrock goto out; 961544Seschrock 973912Slling buf = kmem_alloc(fsize, KM_SLEEP); 981544Seschrock 99789Sahrens /* 100789Sahrens * Read the nvlist from the file. 101789Sahrens */ 1023912Slling if (kobj_read_file(file, buf, fsize, 0) < 0) 103789Sahrens goto out; 104789Sahrens 105789Sahrens /* 106789Sahrens * Unpack the nvlist. 107789Sahrens */ 1083912Slling if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0) 109789Sahrens goto out; 110789Sahrens 111789Sahrens /* 112789Sahrens * Iterate over all elements in the nvlist, creating a new spa_t for 113789Sahrens * each one with the specified configuration. 114789Sahrens */ 115789Sahrens mutex_enter(&spa_namespace_lock); 116789Sahrens nvpair = NULL; 117789Sahrens while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) { 118789Sahrens 119789Sahrens if (nvpair_type(nvpair) != DATA_TYPE_NVLIST) 120789Sahrens continue; 121789Sahrens 122789Sahrens VERIFY(nvpair_value_nvlist(nvpair, &child) == 0); 123789Sahrens 124789Sahrens if (spa_lookup(nvpair_name(nvpair)) != NULL) 125789Sahrens continue; 1261635Sbonwick spa = spa_add(nvpair_name(nvpair), NULL); 127789Sahrens 128789Sahrens /* 129789Sahrens * We blindly duplicate the configuration here. If it's 130789Sahrens * invalid, we will catch it when the pool is first opened. 131789Sahrens */ 132789Sahrens VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0); 133789Sahrens } 134789Sahrens mutex_exit(&spa_namespace_lock); 135789Sahrens 136789Sahrens nvlist_free(nvlist); 137789Sahrens 138789Sahrens out: 139789Sahrens if (buf != NULL) 1403912Slling kmem_free(buf, fsize); 141789Sahrens 1421544Seschrock kobj_close_file(file); 143789Sahrens } 144789Sahrens 145*6643Seschrock static void 146*6643Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl) 1475363Seschrock { 148789Sahrens size_t buflen; 149789Sahrens char *buf; 150789Sahrens vnode_t *vp; 151789Sahrens int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX; 152*6643Seschrock char tempname[128]; 153*6643Seschrock 154*6643Seschrock /* 155*6643Seschrock * If the nvlist is empty (NULL), then remove the old cachefile. 156*6643Seschrock */ 157*6643Seschrock if (nvl == NULL) { 158*6643Seschrock (void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE); 159*6643Seschrock return; 160*6643Seschrock } 161789Sahrens 162789Sahrens /* 163789Sahrens * Pack the configuration into a buffer. 164789Sahrens */ 165*6643Seschrock VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0); 166789Sahrens 167789Sahrens buf = kmem_alloc(buflen, KM_SLEEP); 168789Sahrens 169*6643Seschrock VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR, 1701544Seschrock KM_SLEEP) == 0); 171789Sahrens 172789Sahrens /* 173789Sahrens * Write the configuration to disk. We need to do the traditional 174789Sahrens * 'write to temporary file, sync, move over original' to make sure we 175789Sahrens * always have a consistent view of the data. 176789Sahrens */ 177*6643Seschrock (void) snprintf(tempname, sizeof (tempname), "%s.tmp", dp->scd_path); 178789Sahrens 179*6643Seschrock if (vn_open(tempname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0) 180789Sahrens goto out; 181789Sahrens 182789Sahrens if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE, 183789Sahrens 0, RLIM64_INFINITY, kcred, NULL) == 0 && 1845331Samw VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) { 185*6643Seschrock (void) vn_rename(tempname, dp->scd_path, UIO_SYSSPACE); 186789Sahrens } 187789Sahrens 1885331Samw (void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL); 189789Sahrens VN_RELE(vp); 190789Sahrens 191789Sahrens out: 192*6643Seschrock (void) vn_remove(tempname, UIO_SYSSPACE, RMFILE); 1935363Seschrock kmem_free(buf, buflen); 1945363Seschrock } 1955363Seschrock 1965363Seschrock /* 197*6643Seschrock * Synchronize pool configuration to disk. This must be called with the 198*6643Seschrock * namespace lock held. 1995363Seschrock */ 2005363Seschrock void 201*6643Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent) 2025363Seschrock { 2035363Seschrock spa_t *spa = NULL; 204*6643Seschrock spa_config_dirent_t *dp, *tdp; 205*6643Seschrock nvlist_t *nvl; 2065363Seschrock 2075363Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 208789Sahrens 209*6643Seschrock /* 210*6643Seschrock * Iterate over all cachefiles for the pool, past or present. When the 211*6643Seschrock * cachefile is changed, the new one is pushed onto this list, allowing 212*6643Seschrock * us to update previous cachefiles that no longer contain this pool. 213*6643Seschrock */ 214*6643Seschrock for (dp = list_head(&target->spa_config_list); dp != NULL; 215*6643Seschrock dp = list_next(&target->spa_config_list, dp)) { 216*6643Seschrock spa = NULL; 217*6643Seschrock if (dp->scd_path == NULL) 218*6643Seschrock continue; 219*6643Seschrock 220*6643Seschrock /* 221*6643Seschrock * Iterate over all pools, adding any matching pools to 'nvl'. 222*6643Seschrock */ 223*6643Seschrock nvl = NULL; 224*6643Seschrock while ((spa = spa_next(spa)) != NULL) { 225*6643Seschrock if (spa->spa_config == NULL || spa->spa_name == NULL) 226*6643Seschrock continue; 227*6643Seschrock 228*6643Seschrock if (spa == target && removing) 229*6643Seschrock continue; 230*6643Seschrock 231*6643Seschrock tdp = list_head(&spa->spa_config_list); 232*6643Seschrock ASSERT(tdp != NULL); 233*6643Seschrock if (tdp->scd_path == NULL || 234*6643Seschrock strcmp(tdp->scd_path, dp->scd_path) != 0) 235*6643Seschrock continue; 236*6643Seschrock 237*6643Seschrock if (nvl == NULL) 238*6643Seschrock VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, 239*6643Seschrock KM_SLEEP) == 0); 240*6643Seschrock 241*6643Seschrock VERIFY(nvlist_add_nvlist(nvl, spa->spa_name, 242*6643Seschrock spa->spa_config) == 0); 243*6643Seschrock } 244*6643Seschrock 245*6643Seschrock spa_config_write(dp, nvl); 246*6643Seschrock nvlist_free(nvl); 247*6643Seschrock } 2485363Seschrock 2495363Seschrock /* 250*6643Seschrock * Remove any config entries older than the current one. 2515363Seschrock */ 252*6643Seschrock dp = list_head(&target->spa_config_list); 253*6643Seschrock while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) { 254*6643Seschrock list_remove(&target->spa_config_list, tdp); 255*6643Seschrock if (tdp->scd_path != NULL) 256*6643Seschrock spa_strfree(tdp->scd_path); 257*6643Seschrock kmem_free(tdp, sizeof (spa_config_dirent_t)); 2585363Seschrock } 2595363Seschrock 2605363Seschrock spa_config_generation++; 261*6643Seschrock 262*6643Seschrock if (postsysevent) 263*6643Seschrock spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC); 264789Sahrens } 265789Sahrens 266789Sahrens /* 2671635Sbonwick * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache, 268789Sahrens * and we don't want to allow the local zone to see all the pools anyway. 269789Sahrens * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration 270789Sahrens * information for all pool visible within the zone. 271789Sahrens */ 272789Sahrens nvlist_t * 273789Sahrens spa_all_configs(uint64_t *generation) 274789Sahrens { 275789Sahrens nvlist_t *pools; 276789Sahrens spa_t *spa; 277789Sahrens 278789Sahrens if (*generation == spa_config_generation) 279789Sahrens return (NULL); 280789Sahrens 2811544Seschrock VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0); 282789Sahrens 283789Sahrens spa = NULL; 284789Sahrens mutex_enter(&spa_namespace_lock); 285789Sahrens while ((spa = spa_next(spa)) != NULL) { 286789Sahrens if (INGLOBALZONE(curproc) || 287789Sahrens zone_dataset_visible(spa_name(spa), NULL)) { 288789Sahrens mutex_enter(&spa->spa_config_cache_lock); 289789Sahrens VERIFY(nvlist_add_nvlist(pools, spa_name(spa), 290789Sahrens spa->spa_config) == 0); 291789Sahrens mutex_exit(&spa->spa_config_cache_lock); 292789Sahrens } 293789Sahrens } 294789Sahrens mutex_exit(&spa_namespace_lock); 295789Sahrens 296789Sahrens *generation = spa_config_generation; 297789Sahrens 298789Sahrens return (pools); 299789Sahrens } 300789Sahrens 301789Sahrens void 302789Sahrens spa_config_set(spa_t *spa, nvlist_t *config) 303789Sahrens { 304789Sahrens mutex_enter(&spa->spa_config_cache_lock); 305789Sahrens if (spa->spa_config != NULL) 306789Sahrens nvlist_free(spa->spa_config); 307789Sahrens spa->spa_config = config; 308789Sahrens mutex_exit(&spa->spa_config_cache_lock); 309789Sahrens } 310789Sahrens 311789Sahrens /* 312789Sahrens * Generate the pool's configuration based on the current in-core state. 313789Sahrens * We infer whether to generate a complete config or just one top-level config 314789Sahrens * based on whether vd is the root vdev. 315789Sahrens */ 316789Sahrens nvlist_t * 317789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats) 318789Sahrens { 319789Sahrens nvlist_t *config, *nvroot; 320789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3213975Sek110237 unsigned long hostid = 0; 322789Sahrens 3234787Sahrens ASSERT(spa_config_held(spa, RW_READER) || 3244787Sahrens spa_config_held(spa, RW_WRITER)); 3251635Sbonwick 326789Sahrens if (vd == NULL) 327789Sahrens vd = rvd; 328789Sahrens 329789Sahrens /* 330789Sahrens * If txg is -1, report the current value of spa->spa_config_txg. 331789Sahrens */ 332789Sahrens if (txg == -1ULL) 333789Sahrens txg = spa->spa_config_txg; 334789Sahrens 3351544Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 336789Sahrens 337789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 3382082Seschrock spa_version(spa)) == 0); 339789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 340789Sahrens spa_name(spa)) == 0); 341789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 342789Sahrens spa_state(spa)) == 0); 343789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 344789Sahrens txg) == 0); 345789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 346789Sahrens spa_guid(spa)) == 0); 3473975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &hostid); 3484178Slling if (hostid != 0) { 3494178Slling VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 3504527Sperrin hostid) == 0); 3514178Slling } 3523975Sek110237 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 3533975Sek110237 utsname.nodename) == 0); 354789Sahrens 355789Sahrens if (vd != rvd) { 356789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID, 357789Sahrens vd->vdev_top->vdev_guid) == 0); 358789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID, 359789Sahrens vd->vdev_guid) == 0); 3602082Seschrock if (vd->vdev_isspare) 3612082Seschrock VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE, 3622082Seschrock 1ULL) == 0); 3634527Sperrin if (vd->vdev_islog) 3644527Sperrin VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG, 3654527Sperrin 1ULL) == 0); 366789Sahrens vd = vd->vdev_top; /* label contains top config */ 367789Sahrens } 368789Sahrens 3695450Sbrendan nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE, B_FALSE); 370789Sahrens VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 371789Sahrens nvlist_free(nvroot); 372789Sahrens 373789Sahrens return (config); 374789Sahrens } 3751635Sbonwick 3761635Sbonwick /* 3776423Sgw25295 * For a pool that's not currently a booting rootpool, update all disk labels, 3786423Sgw25295 * generate a fresh config based on the current in-core state, and sync the 3796423Sgw25295 * global config cache. 3801635Sbonwick */ 3811635Sbonwick void 3821635Sbonwick spa_config_update(spa_t *spa, int what) 3831635Sbonwick { 3846423Sgw25295 spa_config_update_common(spa, what, FALSE); 3856423Sgw25295 } 3866423Sgw25295 3876423Sgw25295 /* 3886423Sgw25295 * Update all disk labels, generate a fresh config based on the current 3896423Sgw25295 * in-core state, and sync the global config cache (do not sync the config 3906423Sgw25295 * cache if this is a booting rootpool). 3916423Sgw25295 */ 3926423Sgw25295 void 3936423Sgw25295 spa_config_update_common(spa_t *spa, int what, boolean_t isroot) 3946423Sgw25295 { 3951635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 3961635Sbonwick uint64_t txg; 3971635Sbonwick int c; 3981635Sbonwick 3991635Sbonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4001635Sbonwick 4011635Sbonwick spa_config_enter(spa, RW_WRITER, FTAG); 4021635Sbonwick txg = spa_last_synced_txg(spa) + 1; 4031635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) { 4041635Sbonwick vdev_config_dirty(rvd); 4051635Sbonwick } else { 4061635Sbonwick /* 4071635Sbonwick * If we have top-level vdevs that were added but have 4081635Sbonwick * not yet been prepared for allocation, do that now. 4091635Sbonwick * (It's safe now because the config cache is up to date, 4101635Sbonwick * so it will be able to translate the new DVAs.) 4111635Sbonwick * See comments in spa_vdev_add() for full details. 4121635Sbonwick */ 4131635Sbonwick for (c = 0; c < rvd->vdev_children; c++) { 4141635Sbonwick vdev_t *tvd = rvd->vdev_child[c]; 4151635Sbonwick if (tvd->vdev_ms_array == 0) { 4161635Sbonwick vdev_init(tvd, txg); 4171635Sbonwick vdev_config_dirty(tvd); 4181635Sbonwick } 4191635Sbonwick } 4201635Sbonwick } 4211635Sbonwick spa_config_exit(spa, FTAG); 4221635Sbonwick 4231635Sbonwick /* 4241635Sbonwick * Wait for the mosconfig to be regenerated and synced. 4251635Sbonwick */ 4261635Sbonwick txg_wait_synced(spa->spa_dsl_pool, txg); 4271635Sbonwick 4281635Sbonwick /* 4291635Sbonwick * Update the global config cache to reflect the new mosconfig. 4301635Sbonwick */ 4316423Sgw25295 if (!isroot) 432*6643Seschrock spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL); 4331635Sbonwick 4341635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) 4356423Sgw25295 spa_config_update_common(spa, SPA_CONFIG_UPDATE_VDEVS, isroot); 4361635Sbonwick } 437