1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 2199653d4eSeschrock 22fa9e4066Sahrens /* 233f9d6ad7SLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 248704186eSDan McDonald * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 25ad135b5dSChristopher Siden * Copyright (c) 2012 by Delphix. All rights reserved. 26fa9e4066Sahrens */ 27fa9e4066Sahrens 28fa9e4066Sahrens #include <sys/spa.h> 29fa9e4066Sahrens #include <sys/spa_impl.h> 30fa9e4066Sahrens #include <sys/nvpair.h> 31fa9e4066Sahrens #include <sys/uio.h> 32fa9e4066Sahrens #include <sys/fs/zfs.h> 33fa9e4066Sahrens #include <sys/vdev_impl.h> 34fa9e4066Sahrens #include <sys/zfs_ioctl.h> 3595173954Sek #include <sys/utsname.h> 3695173954Sek #include <sys/systeminfo.h> 3795173954Sek #include <sys/sunddi.h> 38ad135b5dSChristopher Siden #include <sys/zfeature.h> 39ea8dc4b6Seschrock #ifdef _KERNEL 40ea8dc4b6Seschrock #include <sys/kobj.h> 415679c89fSjv #include <sys/zone.h> 42ea8dc4b6Seschrock #endif 43ea8dc4b6Seschrock 44fa9e4066Sahrens /* 45fa9e4066Sahrens * Pool configuration repository. 46fa9e4066Sahrens * 472f8aaab3Seschrock * Pool configuration is stored as a packed nvlist on the filesystem. By 482f8aaab3Seschrock * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot 492f8aaab3Seschrock * (when the ZFS module is loaded). Pools can also have the 'cachefile' 502f8aaab3Seschrock * property set that allows them to be stored in an alternate location until 512f8aaab3Seschrock * the control of external software. 52fa9e4066Sahrens * 532f8aaab3Seschrock * For each cache file, we have a single nvlist which holds all the 542f8aaab3Seschrock * configuration information. When the module loads, we read this information 552f8aaab3Seschrock * from /etc/zfs/zpool.cache and populate the SPA namespace. This namespace is 562f8aaab3Seschrock * maintained independently in spa.c. Whenever the namespace is modified, or 572f8aaab3Seschrock * the configuration of a pool is changed, we call spa_config_sync(), which 582f8aaab3Seschrock * walks through all the active pools and writes the configuration to disk. 59fa9e4066Sahrens */ 60fa9e4066Sahrens 61fa9e4066Sahrens static uint64_t spa_config_generation = 1; 62fa9e4066Sahrens 63fa9e4066Sahrens /* 64fa9e4066Sahrens * This can be overridden in userland to preserve an alternate namespace for 65fa9e4066Sahrens * userland pools when doing testing. 66fa9e4066Sahrens */ 67c5904d13Seschrock const char *spa_config_path = ZPOOL_CACHE; 68fa9e4066Sahrens 69fa9e4066Sahrens /* 70fa9e4066Sahrens * Called when the module is first loaded, this routine loads the configuration 71fa9e4066Sahrens * file into the SPA namespace. It does not actually open or load the pools; it 72fa9e4066Sahrens * only populates the namespace. 73fa9e4066Sahrens */ 74fa9e4066Sahrens void 75fa9e4066Sahrens spa_config_load(void) 76fa9e4066Sahrens { 77fa9e4066Sahrens void *buf = NULL; 78fa9e4066Sahrens nvlist_t *nvlist, *child; 79fa9e4066Sahrens nvpair_t *nvpair; 80e14bb325SJeff Bonwick char *pathname; 81ea8dc4b6Seschrock struct _buf *file; 82b1b8ab34Slling uint64_t fsize; 83fa9e4066Sahrens 84fa9e4066Sahrens /* 85fa9e4066Sahrens * Open the configuration file. 86fa9e4066Sahrens */ 87e14bb325SJeff Bonwick pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 88e14bb325SJeff Bonwick 89e14bb325SJeff Bonwick (void) snprintf(pathname, MAXPATHLEN, "%s%s", 90c5904d13Seschrock (rootdir != NULL) ? "./" : "", spa_config_path); 91ea8dc4b6Seschrock 92ea8dc4b6Seschrock file = kobj_open_file(pathname); 93e14bb325SJeff Bonwick 94e14bb325SJeff Bonwick kmem_free(pathname, MAXPATHLEN); 95e14bb325SJeff Bonwick 96ea8dc4b6Seschrock if (file == (struct _buf *)-1) 97fa9e4066Sahrens return; 98fa9e4066Sahrens 99b1b8ab34Slling if (kobj_get_filesize(file, &fsize) != 0) 100fa9e4066Sahrens goto out; 101fa9e4066Sahrens 102b1b8ab34Slling buf = kmem_alloc(fsize, KM_SLEEP); 103fa9e4066Sahrens 104ea8dc4b6Seschrock /* 105ea8dc4b6Seschrock * Read the nvlist from the file. 106ea8dc4b6Seschrock */ 107b1b8ab34Slling if (kobj_read_file(file, buf, fsize, 0) < 0) 108fa9e4066Sahrens goto out; 109fa9e4066Sahrens 110fa9e4066Sahrens /* 111fa9e4066Sahrens * Unpack the nvlist. 112fa9e4066Sahrens */ 113b1b8ab34Slling if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0) 114fa9e4066Sahrens goto out; 115fa9e4066Sahrens 116fa9e4066Sahrens /* 117fa9e4066Sahrens * Iterate over all elements in the nvlist, creating a new spa_t for 118fa9e4066Sahrens * each one with the specified configuration. 119fa9e4066Sahrens */ 120fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 121fa9e4066Sahrens nvpair = NULL; 122fa9e4066Sahrens while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) { 123fa9e4066Sahrens if (nvpair_type(nvpair) != DATA_TYPE_NVLIST) 124fa9e4066Sahrens continue; 125fa9e4066Sahrens 126fa9e4066Sahrens VERIFY(nvpair_value_nvlist(nvpair, &child) == 0); 127fa9e4066Sahrens 128fa9e4066Sahrens if (spa_lookup(nvpair_name(nvpair)) != NULL) 129fa9e4066Sahrens continue; 130468c413aSTim Haley (void) spa_add(nvpair_name(nvpair), child, NULL); 131fa9e4066Sahrens } 132fa9e4066Sahrens mutex_exit(&spa_namespace_lock); 133fa9e4066Sahrens 134fa9e4066Sahrens nvlist_free(nvlist); 135fa9e4066Sahrens 136fa9e4066Sahrens out: 137fa9e4066Sahrens if (buf != NULL) 138b1b8ab34Slling kmem_free(buf, fsize); 139fa9e4066Sahrens 140ea8dc4b6Seschrock kobj_close_file(file); 141fa9e4066Sahrens } 142fa9e4066Sahrens 1432f8aaab3Seschrock static void 144c5904d13Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl) 1452f8aaab3Seschrock { 1462f8aaab3Seschrock size_t buflen; 1472f8aaab3Seschrock char *buf; 1482f8aaab3Seschrock vnode_t *vp; 1492f8aaab3Seschrock int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX; 150e14bb325SJeff Bonwick char *temp; 151c5904d13Seschrock 152c5904d13Seschrock /* 153c5904d13Seschrock * If the nvlist is empty (NULL), then remove the old cachefile. 154c5904d13Seschrock */ 155c5904d13Seschrock if (nvl == NULL) { 156c5904d13Seschrock (void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE); 157c5904d13Seschrock return; 158c5904d13Seschrock } 1592f8aaab3Seschrock 160fa9e4066Sahrens /* 161fa9e4066Sahrens * Pack the configuration into a buffer. 162fa9e4066Sahrens */ 163c5904d13Seschrock VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0); 164fa9e4066Sahrens 165fa9e4066Sahrens buf = kmem_alloc(buflen, KM_SLEEP); 166e14bb325SJeff Bonwick temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 167fa9e4066Sahrens 168c5904d13Seschrock VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR, 169ea8dc4b6Seschrock KM_SLEEP) == 0); 170fa9e4066Sahrens 171fa9e4066Sahrens /* 172fa9e4066Sahrens * Write the configuration to disk. We need to do the traditional 173fa9e4066Sahrens * 'write to temporary file, sync, move over original' to make sure we 174fa9e4066Sahrens * always have a consistent view of the data. 175fa9e4066Sahrens */ 176e14bb325SJeff Bonwick (void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path); 177fa9e4066Sahrens 178e14bb325SJeff Bonwick if (vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) == 0) { 179e14bb325SJeff Bonwick if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE, 180e14bb325SJeff Bonwick 0, RLIM64_INFINITY, kcred, NULL) == 0 && 181e14bb325SJeff Bonwick VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) { 182e14bb325SJeff Bonwick (void) vn_rename(temp, dp->scd_path, UIO_SYSSPACE); 183e14bb325SJeff Bonwick } 184e14bb325SJeff Bonwick (void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL); 185e14bb325SJeff Bonwick VN_RELE(vp); 186fa9e4066Sahrens } 187fa9e4066Sahrens 188e14bb325SJeff Bonwick (void) vn_remove(temp, UIO_SYSSPACE, RMFILE); 189fa9e4066Sahrens 190fa9e4066Sahrens kmem_free(buf, buflen); 191e14bb325SJeff Bonwick kmem_free(temp, MAXPATHLEN); 1922f8aaab3Seschrock } 1932f8aaab3Seschrock 1942f8aaab3Seschrock /* 195c5904d13Seschrock * Synchronize pool configuration to disk. This must be called with the 196c5904d13Seschrock * namespace lock held. 1972f8aaab3Seschrock */ 1982f8aaab3Seschrock void 199c5904d13Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent) 2002f8aaab3Seschrock { 201c5904d13Seschrock spa_config_dirent_t *dp, *tdp; 202c5904d13Seschrock nvlist_t *nvl; 2032f8aaab3Seschrock 2042f8aaab3Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 2052f8aaab3Seschrock 2064f0f5e5bSVictor Latushkin if (rootdir == NULL || !(spa_mode_global & FWRITE)) 2079fddc96aSGeorge Wilson return; 2089fddc96aSGeorge Wilson 209c5904d13Seschrock /* 210c5904d13Seschrock * Iterate over all cachefiles for the pool, past or present. When the 211c5904d13Seschrock * cachefile is changed, the new one is pushed onto this list, allowing 212c5904d13Seschrock * us to update previous cachefiles that no longer contain this pool. 213c5904d13Seschrock */ 214c5904d13Seschrock for (dp = list_head(&target->spa_config_list); dp != NULL; 215c5904d13Seschrock dp = list_next(&target->spa_config_list, dp)) { 216e14bb325SJeff Bonwick spa_t *spa = NULL; 217c5904d13Seschrock if (dp->scd_path == NULL) 218c5904d13Seschrock continue; 219c5904d13Seschrock 220c5904d13Seschrock /* 221c5904d13Seschrock * Iterate over all pools, adding any matching pools to 'nvl'. 222c5904d13Seschrock */ 223c5904d13Seschrock nvl = NULL; 224c5904d13Seschrock while ((spa = spa_next(spa)) != NULL) { 225fb02ae02SGeorge Wilson /* 226fb02ae02SGeorge Wilson * Skip over our own pool if we're about to remove 227fb02ae02SGeorge Wilson * ourselves from the spa namespace or any pool that 228fb02ae02SGeorge Wilson * is readonly. Since we cannot guarantee that a 229fb02ae02SGeorge Wilson * readonly pool would successfully import upon reboot, 230fb02ae02SGeorge Wilson * we don't allow them to be written to the cache file. 231fb02ae02SGeorge Wilson */ 232fb02ae02SGeorge Wilson if ((spa == target && removing) || 233fb02ae02SGeorge Wilson !spa_writeable(spa)) 234c5904d13Seschrock continue; 235c5904d13Seschrock 236e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 237c5904d13Seschrock tdp = list_head(&spa->spa_config_list); 238e14bb325SJeff Bonwick if (spa->spa_config == NULL || 239e14bb325SJeff Bonwick tdp->scd_path == NULL || 240e14bb325SJeff Bonwick strcmp(tdp->scd_path, dp->scd_path) != 0) { 241e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 242c5904d13Seschrock continue; 243e14bb325SJeff Bonwick } 244c5904d13Seschrock 245c5904d13Seschrock if (nvl == NULL) 246c5904d13Seschrock VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, 247c5904d13Seschrock KM_SLEEP) == 0); 248c5904d13Seschrock 249c5904d13Seschrock VERIFY(nvlist_add_nvlist(nvl, spa->spa_name, 250c5904d13Seschrock spa->spa_config) == 0); 251e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 252c5904d13Seschrock } 253c5904d13Seschrock 254c5904d13Seschrock spa_config_write(dp, nvl); 255c5904d13Seschrock nvlist_free(nvl); 256c5904d13Seschrock } 2572f8aaab3Seschrock 2582f8aaab3Seschrock /* 259c5904d13Seschrock * Remove any config entries older than the current one. 2602f8aaab3Seschrock */ 261c5904d13Seschrock dp = list_head(&target->spa_config_list); 262c5904d13Seschrock while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) { 263c5904d13Seschrock list_remove(&target->spa_config_list, tdp); 264c5904d13Seschrock if (tdp->scd_path != NULL) 265c5904d13Seschrock spa_strfree(tdp->scd_path); 266c5904d13Seschrock kmem_free(tdp, sizeof (spa_config_dirent_t)); 2672f8aaab3Seschrock } 2682f8aaab3Seschrock 2692f8aaab3Seschrock spa_config_generation++; 270c5904d13Seschrock 271c5904d13Seschrock if (postsysevent) 272c5904d13Seschrock spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC); 273fa9e4066Sahrens } 274fa9e4066Sahrens 275fa9e4066Sahrens /* 2760373e76bSbonwick * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache, 277fa9e4066Sahrens * and we don't want to allow the local zone to see all the pools anyway. 278fa9e4066Sahrens * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration 279fa9e4066Sahrens * information for all pool visible within the zone. 280fa9e4066Sahrens */ 281fa9e4066Sahrens nvlist_t * 282fa9e4066Sahrens spa_all_configs(uint64_t *generation) 283fa9e4066Sahrens { 284fa9e4066Sahrens nvlist_t *pools; 285e14bb325SJeff Bonwick spa_t *spa = NULL; 286fa9e4066Sahrens 287fa9e4066Sahrens if (*generation == spa_config_generation) 288fa9e4066Sahrens return (NULL); 289fa9e4066Sahrens 290ea8dc4b6Seschrock VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0); 291fa9e4066Sahrens 292fa9e4066Sahrens mutex_enter(&spa_namespace_lock); 293fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) { 294fa9e4066Sahrens if (INGLOBALZONE(curproc) || 295fa9e4066Sahrens zone_dataset_visible(spa_name(spa), NULL)) { 296e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 297fa9e4066Sahrens VERIFY(nvlist_add_nvlist(pools, spa_name(spa), 298fa9e4066Sahrens spa->spa_config) == 0); 299e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 300fa9e4066Sahrens } 301fa9e4066Sahrens } 302fa9e4066Sahrens *generation = spa_config_generation; 303e14bb325SJeff Bonwick mutex_exit(&spa_namespace_lock); 304fa9e4066Sahrens 305fa9e4066Sahrens return (pools); 306fa9e4066Sahrens } 307fa9e4066Sahrens 308fa9e4066Sahrens void 309fa9e4066Sahrens spa_config_set(spa_t *spa, nvlist_t *config) 310fa9e4066Sahrens { 311e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock); 312fa9e4066Sahrens if (spa->spa_config != NULL) 313fa9e4066Sahrens nvlist_free(spa->spa_config); 314fa9e4066Sahrens spa->spa_config = config; 315e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock); 316fa9e4066Sahrens } 317fa9e4066Sahrens 318fa9e4066Sahrens /* 319fa9e4066Sahrens * Generate the pool's configuration based on the current in-core state. 320*f7170741SWill Andrews * 321fa9e4066Sahrens * We infer whether to generate a complete config or just one top-level config 322fa9e4066Sahrens * based on whether vd is the root vdev. 323fa9e4066Sahrens */ 324fa9e4066Sahrens nvlist_t * 325fa9e4066Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats) 326fa9e4066Sahrens { 327fa9e4066Sahrens nvlist_t *config, *nvroot; 328fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev; 32995173954Sek unsigned long hostid = 0; 330e14bb325SJeff Bonwick boolean_t locked = B_FALSE; 3311195e687SMark J Musante uint64_t split_guid; 332fa9e4066Sahrens 333e14bb325SJeff Bonwick if (vd == NULL) { 334fa9e4066Sahrens vd = rvd; 335e14bb325SJeff Bonwick locked = B_TRUE; 336e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 337e14bb325SJeff Bonwick } 338e14bb325SJeff Bonwick 339e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) == 340e14bb325SJeff Bonwick (SCL_CONFIG | SCL_STATE)); 341fa9e4066Sahrens 342fa9e4066Sahrens /* 343fa9e4066Sahrens * If txg is -1, report the current value of spa->spa_config_txg. 344fa9e4066Sahrens */ 345fa9e4066Sahrens if (txg == -1ULL) 346fa9e4066Sahrens txg = spa->spa_config_txg; 347fa9e4066Sahrens 348ea8dc4b6Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 349fa9e4066Sahrens 350fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 35199653d4eSeschrock spa_version(spa)) == 0); 352fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 353fa9e4066Sahrens spa_name(spa)) == 0); 354fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 355fa9e4066Sahrens spa_state(spa)) == 0); 356fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 357fa9e4066Sahrens txg) == 0); 358fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 359fa9e4066Sahrens spa_guid(spa)) == 0); 3608704186eSDan McDonald VERIFY(spa->spa_comment == NULL || nvlist_add_string(config, 3618704186eSDan McDonald ZPOOL_CONFIG_COMMENT, spa->spa_comment) == 0); 3628704186eSDan McDonald 3638704186eSDan McDonald 3645679c89fSjv #ifdef _KERNEL 3655679c89fSjv hostid = zone_get_hostid(NULL); 3665679c89fSjv #else /* _KERNEL */ 3675679c89fSjv /* 3685679c89fSjv * We're emulating the system's hostid in userland, so we can't use 3695679c89fSjv * zone_get_hostid(). 3705679c89fSjv */ 37195173954Sek (void) ddi_strtoul(hw_serial, NULL, 10, &hostid); 3725679c89fSjv #endif /* _KERNEL */ 37317194a52Slling if (hostid != 0) { 37417194a52Slling VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 3758654d025Sperrin hostid) == 0); 37617194a52Slling } 37795173954Sek VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 37895173954Sek utsname.nodename) == 0); 379fa9e4066Sahrens 380fa9e4066Sahrens if (vd != rvd) { 381fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID, 382fa9e4066Sahrens vd->vdev_top->vdev_guid) == 0); 383fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID, 384fa9e4066Sahrens vd->vdev_guid) == 0); 38599653d4eSeschrock if (vd->vdev_isspare) 38699653d4eSeschrock VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE, 38799653d4eSeschrock 1ULL) == 0); 3888654d025Sperrin if (vd->vdev_islog) 3898654d025Sperrin VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG, 3908654d025Sperrin 1ULL) == 0); 391fa9e4066Sahrens vd = vd->vdev_top; /* label contains top config */ 3921195e687SMark J Musante } else { 3931195e687SMark J Musante /* 3941195e687SMark J Musante * Only add the (potentially large) split information 3951195e687SMark J Musante * in the mos config, and not in the vdev labels 3961195e687SMark J Musante */ 3971195e687SMark J Musante if (spa->spa_config_splitting != NULL) 3981195e687SMark J Musante VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT, 3991195e687SMark J Musante spa->spa_config_splitting) == 0); 400fa9e4066Sahrens } 401fa9e4066Sahrens 40288ecc943SGeorge Wilson /* 40388ecc943SGeorge Wilson * Add the top-level config. We even add this on pools which 4044b964adaSGeorge Wilson * don't support holes in the namespace. 40588ecc943SGeorge Wilson */ 40688ecc943SGeorge Wilson vdev_top_config_generate(spa, config); 40788ecc943SGeorge Wilson 4081195e687SMark J Musante /* 4091195e687SMark J Musante * If we're splitting, record the original pool's guid. 4101195e687SMark J Musante */ 4111195e687SMark J Musante if (spa->spa_config_splitting != NULL && 4121195e687SMark J Musante nvlist_lookup_uint64(spa->spa_config_splitting, 4131195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) { 4141195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID, 4151195e687SMark J Musante split_guid) == 0); 4161195e687SMark J Musante } 4171195e687SMark J Musante 4183f9d6ad7SLin Ling nvroot = vdev_config_generate(spa, vd, getstats, 0); 419fa9e4066Sahrens VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 420fa9e4066Sahrens nvlist_free(nvroot); 421fa9e4066Sahrens 422ad135b5dSChristopher Siden /* 423ad135b5dSChristopher Siden * Store what's necessary for reading the MOS in the label. 424ad135b5dSChristopher Siden */ 425ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 426ad135b5dSChristopher Siden spa->spa_label_features) == 0); 427ad135b5dSChristopher Siden 4289eb19f4dSGeorge Wilson if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) { 4299eb19f4dSGeorge Wilson ddt_histogram_t *ddh; 4309eb19f4dSGeorge Wilson ddt_stat_t *dds; 4319eb19f4dSGeorge Wilson ddt_object_t *ddo; 4329eb19f4dSGeorge Wilson 4339eb19f4dSGeorge Wilson ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP); 4349eb19f4dSGeorge Wilson ddt_get_dedup_histogram(spa, ddh); 4359eb19f4dSGeorge Wilson VERIFY(nvlist_add_uint64_array(config, 4369eb19f4dSGeorge Wilson ZPOOL_CONFIG_DDT_HISTOGRAM, 4379eb19f4dSGeorge Wilson (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t)) == 0); 4389eb19f4dSGeorge Wilson kmem_free(ddh, sizeof (ddt_histogram_t)); 4399eb19f4dSGeorge Wilson 4409eb19f4dSGeorge Wilson ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP); 4419eb19f4dSGeorge Wilson ddt_get_dedup_object_stats(spa, ddo); 4429eb19f4dSGeorge Wilson VERIFY(nvlist_add_uint64_array(config, 4439eb19f4dSGeorge Wilson ZPOOL_CONFIG_DDT_OBJ_STATS, 4449eb19f4dSGeorge Wilson (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t)) == 0); 4459eb19f4dSGeorge Wilson kmem_free(ddo, sizeof (ddt_object_t)); 4469eb19f4dSGeorge Wilson 4479eb19f4dSGeorge Wilson dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP); 4489eb19f4dSGeorge Wilson ddt_get_dedup_stats(spa, dds); 4499eb19f4dSGeorge Wilson VERIFY(nvlist_add_uint64_array(config, 4509eb19f4dSGeorge Wilson ZPOOL_CONFIG_DDT_STATS, 4519eb19f4dSGeorge Wilson (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t)) == 0); 4529eb19f4dSGeorge Wilson kmem_free(dds, sizeof (ddt_stat_t)); 4539eb19f4dSGeorge Wilson } 4549eb19f4dSGeorge Wilson 455e14bb325SJeff Bonwick if (locked) 456e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 457e14bb325SJeff Bonwick 458fa9e4066Sahrens return (config); 459fa9e4066Sahrens } 4600373e76bSbonwick 461e7cbe64fSgw /* 462e7cbe64fSgw * Update all disk labels, generate a fresh config based on the current 463e7cbe64fSgw * in-core state, and sync the global config cache (do not sync the config 464e7cbe64fSgw * cache if this is a booting rootpool). 465e7cbe64fSgw */ 466e7cbe64fSgw void 467bc758434SLin Ling spa_config_update(spa_t *spa, int what) 4680373e76bSbonwick { 4690373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev; 4700373e76bSbonwick uint64_t txg; 4710373e76bSbonwick int c; 4720373e76bSbonwick 4730373e76bSbonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4740373e76bSbonwick 475e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4760373e76bSbonwick txg = spa_last_synced_txg(spa) + 1; 4770373e76bSbonwick if (what == SPA_CONFIG_UPDATE_POOL) { 4780373e76bSbonwick vdev_config_dirty(rvd); 4790373e76bSbonwick } else { 4800373e76bSbonwick /* 4810373e76bSbonwick * If we have top-level vdevs that were added but have 4820373e76bSbonwick * not yet been prepared for allocation, do that now. 4830373e76bSbonwick * (It's safe now because the config cache is up to date, 4840373e76bSbonwick * so it will be able to translate the new DVAs.) 4850373e76bSbonwick * See comments in spa_vdev_add() for full details. 4860373e76bSbonwick */ 4870373e76bSbonwick for (c = 0; c < rvd->vdev_children; c++) { 4880373e76bSbonwick vdev_t *tvd = rvd->vdev_child[c]; 489573ca77eSGeorge Wilson if (tvd->vdev_ms_array == 0) 490573ca77eSGeorge Wilson vdev_metaslab_set_size(tvd); 491573ca77eSGeorge Wilson vdev_expand(tvd, txg); 4920373e76bSbonwick } 4930373e76bSbonwick } 494e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG); 4950373e76bSbonwick 4960373e76bSbonwick /* 4970373e76bSbonwick * Wait for the mosconfig to be regenerated and synced. 4980373e76bSbonwick */ 4990373e76bSbonwick txg_wait_synced(spa->spa_dsl_pool, txg); 5000373e76bSbonwick 5010373e76bSbonwick /* 5020373e76bSbonwick * Update the global config cache to reflect the new mosconfig. 5030373e76bSbonwick */ 504bc758434SLin Ling if (!spa->spa_is_root) 505c5904d13Seschrock spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL); 5060373e76bSbonwick 5070373e76bSbonwick if (what == SPA_CONFIG_UPDATE_POOL) 508bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS); 5090373e76bSbonwick } 510