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 /* 23*12296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24789Sahrens */ 25789Sahrens 26789Sahrens #include <sys/spa.h> 27789Sahrens #include <sys/spa_impl.h> 28789Sahrens #include <sys/nvpair.h> 29789Sahrens #include <sys/uio.h> 30789Sahrens #include <sys/fs/zfs.h> 31789Sahrens #include <sys/vdev_impl.h> 32789Sahrens #include <sys/zfs_ioctl.h> 333975Sek110237 #include <sys/utsname.h> 343975Sek110237 #include <sys/systeminfo.h> 353975Sek110237 #include <sys/sunddi.h> 361544Seschrock #ifdef _KERNEL 371544Seschrock #include <sys/kobj.h> 388662SJordan.Vaughan@Sun.com #include <sys/zone.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; 777754SJeff.Bonwick@Sun.COM char *pathname; 781544Seschrock struct _buf *file; 793912Slling uint64_t fsize; 80789Sahrens 81789Sahrens /* 82789Sahrens * Open the configuration file. 83789Sahrens */ 847754SJeff.Bonwick@Sun.COM pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 857754SJeff.Bonwick@Sun.COM 867754SJeff.Bonwick@Sun.COM (void) snprintf(pathname, MAXPATHLEN, "%s%s", 876643Seschrock (rootdir != NULL) ? "./" : "", spa_config_path); 881544Seschrock 891544Seschrock file = kobj_open_file(pathname); 907754SJeff.Bonwick@Sun.COM 917754SJeff.Bonwick@Sun.COM kmem_free(pathname, MAXPATHLEN); 927754SJeff.Bonwick@Sun.COM 931544Seschrock if (file == (struct _buf *)-1) 94789Sahrens return; 95789Sahrens 963912Slling if (kobj_get_filesize(file, &fsize) != 0) 971544Seschrock goto out; 981544Seschrock 993912Slling buf = kmem_alloc(fsize, KM_SLEEP); 1001544Seschrock 101789Sahrens /* 102789Sahrens * Read the nvlist from the file. 103789Sahrens */ 1043912Slling if (kobj_read_file(file, buf, fsize, 0) < 0) 105789Sahrens goto out; 106789Sahrens 107789Sahrens /* 108789Sahrens * Unpack the nvlist. 109789Sahrens */ 1103912Slling if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0) 111789Sahrens goto out; 112789Sahrens 113789Sahrens /* 114789Sahrens * Iterate over all elements in the nvlist, creating a new spa_t for 115789Sahrens * each one with the specified configuration. 116789Sahrens */ 117789Sahrens mutex_enter(&spa_namespace_lock); 118789Sahrens nvpair = NULL; 119789Sahrens while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) { 120789Sahrens if (nvpair_type(nvpair) != DATA_TYPE_NVLIST) 121789Sahrens continue; 122789Sahrens 123789Sahrens VERIFY(nvpair_value_nvlist(nvpair, &child) == 0); 124789Sahrens 125789Sahrens if (spa_lookup(nvpair_name(nvpair)) != NULL) 126789Sahrens continue; 12710921STim.Haley@Sun.COM (void) spa_add(nvpair_name(nvpair), child, NULL); 128789Sahrens } 129789Sahrens mutex_exit(&spa_namespace_lock); 130789Sahrens 131789Sahrens nvlist_free(nvlist); 132789Sahrens 133789Sahrens out: 134789Sahrens if (buf != NULL) 1353912Slling kmem_free(buf, fsize); 136789Sahrens 1371544Seschrock kobj_close_file(file); 138789Sahrens } 139789Sahrens 1406643Seschrock static void 1416643Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl) 1425363Seschrock { 143789Sahrens size_t buflen; 144789Sahrens char *buf; 145789Sahrens vnode_t *vp; 146789Sahrens int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX; 1477754SJeff.Bonwick@Sun.COM char *temp; 1486643Seschrock 1496643Seschrock /* 1506643Seschrock * If the nvlist is empty (NULL), then remove the old cachefile. 1516643Seschrock */ 1526643Seschrock if (nvl == NULL) { 1536643Seschrock (void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE); 1546643Seschrock return; 1556643Seschrock } 156789Sahrens 157789Sahrens /* 158789Sahrens * Pack the configuration into a buffer. 159789Sahrens */ 1606643Seschrock VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0); 161789Sahrens 162789Sahrens buf = kmem_alloc(buflen, KM_SLEEP); 1637754SJeff.Bonwick@Sun.COM temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 164789Sahrens 1656643Seschrock VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR, 1661544Seschrock KM_SLEEP) == 0); 167789Sahrens 168789Sahrens /* 169789Sahrens * Write the configuration to disk. We need to do the traditional 170789Sahrens * 'write to temporary file, sync, move over original' to make sure we 171789Sahrens * always have a consistent view of the data. 172789Sahrens */ 1737754SJeff.Bonwick@Sun.COM (void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path); 174789Sahrens 1757754SJeff.Bonwick@Sun.COM if (vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) == 0) { 1767754SJeff.Bonwick@Sun.COM if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE, 1777754SJeff.Bonwick@Sun.COM 0, RLIM64_INFINITY, kcred, NULL) == 0 && 1787754SJeff.Bonwick@Sun.COM VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) { 1797754SJeff.Bonwick@Sun.COM (void) vn_rename(temp, dp->scd_path, UIO_SYSSPACE); 1807754SJeff.Bonwick@Sun.COM } 1817754SJeff.Bonwick@Sun.COM (void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL); 1827754SJeff.Bonwick@Sun.COM VN_RELE(vp); 183789Sahrens } 184789Sahrens 1857754SJeff.Bonwick@Sun.COM (void) vn_remove(temp, UIO_SYSSPACE, RMFILE); 186789Sahrens 1875363Seschrock kmem_free(buf, buflen); 1887754SJeff.Bonwick@Sun.COM kmem_free(temp, MAXPATHLEN); 1895363Seschrock } 1905363Seschrock 1915363Seschrock /* 1926643Seschrock * Synchronize pool configuration to disk. This must be called with the 1936643Seschrock * namespace lock held. 1945363Seschrock */ 1955363Seschrock void 1966643Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent) 1975363Seschrock { 1986643Seschrock spa_config_dirent_t *dp, *tdp; 1996643Seschrock nvlist_t *nvl; 2005363Seschrock 2015363Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock)); 202789Sahrens 20310000SVictor.Latushkin@Sun.COM if (rootdir == NULL || !(spa_mode_global & FWRITE)) 2048225SGeorge.Wilson@Sun.COM return; 2058225SGeorge.Wilson@Sun.COM 2066643Seschrock /* 2076643Seschrock * Iterate over all cachefiles for the pool, past or present. When the 2086643Seschrock * cachefile is changed, the new one is pushed onto this list, allowing 2096643Seschrock * us to update previous cachefiles that no longer contain this pool. 2106643Seschrock */ 2116643Seschrock for (dp = list_head(&target->spa_config_list); dp != NULL; 2126643Seschrock dp = list_next(&target->spa_config_list, dp)) { 2137754SJeff.Bonwick@Sun.COM spa_t *spa = NULL; 2146643Seschrock if (dp->scd_path == NULL) 2156643Seschrock continue; 2166643Seschrock 2176643Seschrock /* 2186643Seschrock * Iterate over all pools, adding any matching pools to 'nvl'. 2196643Seschrock */ 2206643Seschrock nvl = NULL; 2216643Seschrock while ((spa = spa_next(spa)) != NULL) { 2226643Seschrock if (spa == target && removing) 2236643Seschrock continue; 2246643Seschrock 2257754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 2266643Seschrock tdp = list_head(&spa->spa_config_list); 2277754SJeff.Bonwick@Sun.COM if (spa->spa_config == NULL || 2287754SJeff.Bonwick@Sun.COM tdp->scd_path == NULL || 2297754SJeff.Bonwick@Sun.COM strcmp(tdp->scd_path, dp->scd_path) != 0) { 2307754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 2316643Seschrock continue; 2327754SJeff.Bonwick@Sun.COM } 2336643Seschrock 2346643Seschrock if (nvl == NULL) 2356643Seschrock VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, 2366643Seschrock KM_SLEEP) == 0); 2376643Seschrock 2386643Seschrock VERIFY(nvlist_add_nvlist(nvl, spa->spa_name, 2396643Seschrock spa->spa_config) == 0); 2407754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 2416643Seschrock } 2426643Seschrock 2436643Seschrock spa_config_write(dp, nvl); 2446643Seschrock nvlist_free(nvl); 2456643Seschrock } 2465363Seschrock 2475363Seschrock /* 2486643Seschrock * Remove any config entries older than the current one. 2495363Seschrock */ 2506643Seschrock dp = list_head(&target->spa_config_list); 2516643Seschrock while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) { 2526643Seschrock list_remove(&target->spa_config_list, tdp); 2536643Seschrock if (tdp->scd_path != NULL) 2546643Seschrock spa_strfree(tdp->scd_path); 2556643Seschrock kmem_free(tdp, sizeof (spa_config_dirent_t)); 2565363Seschrock } 2575363Seschrock 2585363Seschrock spa_config_generation++; 2596643Seschrock 2606643Seschrock if (postsysevent) 2616643Seschrock spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC); 262789Sahrens } 263789Sahrens 264789Sahrens /* 2651635Sbonwick * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache, 266789Sahrens * and we don't want to allow the local zone to see all the pools anyway. 267789Sahrens * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration 268789Sahrens * information for all pool visible within the zone. 269789Sahrens */ 270789Sahrens nvlist_t * 271789Sahrens spa_all_configs(uint64_t *generation) 272789Sahrens { 273789Sahrens nvlist_t *pools; 2747754SJeff.Bonwick@Sun.COM spa_t *spa = NULL; 275789Sahrens 276789Sahrens if (*generation == spa_config_generation) 277789Sahrens return (NULL); 278789Sahrens 2791544Seschrock VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0); 280789Sahrens 281789Sahrens mutex_enter(&spa_namespace_lock); 282789Sahrens while ((spa = spa_next(spa)) != NULL) { 283789Sahrens if (INGLOBALZONE(curproc) || 284789Sahrens zone_dataset_visible(spa_name(spa), NULL)) { 2857754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 286789Sahrens VERIFY(nvlist_add_nvlist(pools, spa_name(spa), 287789Sahrens spa->spa_config) == 0); 2887754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 289789Sahrens } 290789Sahrens } 2917754SJeff.Bonwick@Sun.COM *generation = spa_config_generation; 292789Sahrens mutex_exit(&spa_namespace_lock); 293789Sahrens 294789Sahrens return (pools); 295789Sahrens } 296789Sahrens 297789Sahrens void 298789Sahrens spa_config_set(spa_t *spa, nvlist_t *config) 299789Sahrens { 3007754SJeff.Bonwick@Sun.COM mutex_enter(&spa->spa_props_lock); 301789Sahrens if (spa->spa_config != NULL) 302789Sahrens nvlist_free(spa->spa_config); 303789Sahrens spa->spa_config = config; 3047754SJeff.Bonwick@Sun.COM mutex_exit(&spa->spa_props_lock); 305789Sahrens } 306789Sahrens 30710921STim.Haley@Sun.COM /* Add discovered rewind info, if any to the provided nvlist */ 30810921STim.Haley@Sun.COM void 30910921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa_t *spa, nvlist_t *tonvl) 31010921STim.Haley@Sun.COM { 31110921STim.Haley@Sun.COM int64_t loss = 0; 31210921STim.Haley@Sun.COM 31310921STim.Haley@Sun.COM if (tonvl == NULL || spa->spa_load_txg == 0) 31410921STim.Haley@Sun.COM return; 31510921STim.Haley@Sun.COM 31610921STim.Haley@Sun.COM VERIFY(nvlist_add_uint64(tonvl, ZPOOL_CONFIG_LOAD_TIME, 31710921STim.Haley@Sun.COM spa->spa_load_txg_ts) == 0); 31810921STim.Haley@Sun.COM if (spa->spa_last_ubsync_txg) 31910921STim.Haley@Sun.COM loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; 32010921STim.Haley@Sun.COM VERIFY(nvlist_add_int64(tonvl, ZPOOL_CONFIG_REWIND_TIME, loss) == 0); 32110921STim.Haley@Sun.COM VERIFY(nvlist_add_uint64(tonvl, ZPOOL_CONFIG_LOAD_DATA_ERRORS, 32210921STim.Haley@Sun.COM spa->spa_load_data_errors) == 0); 32310921STim.Haley@Sun.COM } 32410921STim.Haley@Sun.COM 325789Sahrens /* 326789Sahrens * Generate the pool's configuration based on the current in-core state. 327789Sahrens * We infer whether to generate a complete config or just one top-level config 328789Sahrens * based on whether vd is the root vdev. 329789Sahrens */ 330789Sahrens nvlist_t * 331789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats) 332789Sahrens { 333789Sahrens nvlist_t *config, *nvroot; 334789Sahrens vdev_t *rvd = spa->spa_root_vdev; 3353975Sek110237 unsigned long hostid = 0; 3367754SJeff.Bonwick@Sun.COM boolean_t locked = B_FALSE; 33711422SMark.Musante@Sun.COM uint64_t split_guid; 338789Sahrens 3397754SJeff.Bonwick@Sun.COM if (vd == NULL) { 3407754SJeff.Bonwick@Sun.COM vd = rvd; 3417754SJeff.Bonwick@Sun.COM locked = B_TRUE; 3427754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 3437754SJeff.Bonwick@Sun.COM } 3441635Sbonwick 3457754SJeff.Bonwick@Sun.COM ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) == 3467754SJeff.Bonwick@Sun.COM (SCL_CONFIG | SCL_STATE)); 347789Sahrens 348789Sahrens /* 349789Sahrens * If txg is -1, report the current value of spa->spa_config_txg. 350789Sahrens */ 351789Sahrens if (txg == -1ULL) 352789Sahrens txg = spa->spa_config_txg; 353789Sahrens 3541544Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0); 355789Sahrens 356789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, 3572082Seschrock spa_version(spa)) == 0); 358789Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, 359789Sahrens spa_name(spa)) == 0); 360789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, 361789Sahrens spa_state(spa)) == 0); 362789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, 363789Sahrens txg) == 0); 364789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, 365789Sahrens spa_guid(spa)) == 0); 3668662SJordan.Vaughan@Sun.com #ifdef _KERNEL 3678662SJordan.Vaughan@Sun.com hostid = zone_get_hostid(NULL); 3688662SJordan.Vaughan@Sun.com #else /* _KERNEL */ 3698662SJordan.Vaughan@Sun.com /* 3708662SJordan.Vaughan@Sun.com * We're emulating the system's hostid in userland, so we can't use 3718662SJordan.Vaughan@Sun.com * zone_get_hostid(). 3728662SJordan.Vaughan@Sun.com */ 3733975Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &hostid); 3748662SJordan.Vaughan@Sun.com #endif /* _KERNEL */ 3754178Slling if (hostid != 0) { 3764178Slling VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, 3774527Sperrin hostid) == 0); 3784178Slling } 3793975Sek110237 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, 3803975Sek110237 utsname.nodename) == 0); 381789Sahrens 382789Sahrens if (vd != rvd) { 383789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID, 384789Sahrens vd->vdev_top->vdev_guid) == 0); 385789Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID, 386789Sahrens vd->vdev_guid) == 0); 3872082Seschrock if (vd->vdev_isspare) 3882082Seschrock VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE, 3892082Seschrock 1ULL) == 0); 3904527Sperrin if (vd->vdev_islog) 3914527Sperrin VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG, 3924527Sperrin 1ULL) == 0); 393789Sahrens vd = vd->vdev_top; /* label contains top config */ 39411422SMark.Musante@Sun.COM } else { 39511422SMark.Musante@Sun.COM /* 39611422SMark.Musante@Sun.COM * Only add the (potentially large) split information 39711422SMark.Musante@Sun.COM * in the mos config, and not in the vdev labels 39811422SMark.Musante@Sun.COM */ 39911422SMark.Musante@Sun.COM if (spa->spa_config_splitting != NULL) 40011422SMark.Musante@Sun.COM VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT, 40111422SMark.Musante@Sun.COM spa->spa_config_splitting) == 0); 402789Sahrens } 403789Sahrens 40410594SGeorge.Wilson@Sun.COM /* 40510594SGeorge.Wilson@Sun.COM * Add the top-level config. We even add this on pools which 40610594SGeorge.Wilson@Sun.COM * don't support holes in the namespace as older pools will 40710594SGeorge.Wilson@Sun.COM * just ignore it. 40810594SGeorge.Wilson@Sun.COM */ 40910594SGeorge.Wilson@Sun.COM vdev_top_config_generate(spa, config); 41010594SGeorge.Wilson@Sun.COM 41111422SMark.Musante@Sun.COM /* 41211422SMark.Musante@Sun.COM * If we're splitting, record the original pool's guid. 41311422SMark.Musante@Sun.COM */ 41411422SMark.Musante@Sun.COM if (spa->spa_config_splitting != NULL && 41511422SMark.Musante@Sun.COM nvlist_lookup_uint64(spa->spa_config_splitting, 41611422SMark.Musante@Sun.COM ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) { 41711422SMark.Musante@Sun.COM VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID, 41811422SMark.Musante@Sun.COM split_guid) == 0); 41911422SMark.Musante@Sun.COM } 42011422SMark.Musante@Sun.COM 421*12296SLin.Ling@Sun.COM nvroot = vdev_config_generate(spa, vd, getstats, 0); 422789Sahrens VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); 423789Sahrens nvlist_free(nvroot); 424789Sahrens 42511149SGeorge.Wilson@Sun.COM if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) { 42611149SGeorge.Wilson@Sun.COM ddt_histogram_t *ddh; 42711149SGeorge.Wilson@Sun.COM ddt_stat_t *dds; 42811149SGeorge.Wilson@Sun.COM ddt_object_t *ddo; 42911149SGeorge.Wilson@Sun.COM 43011149SGeorge.Wilson@Sun.COM ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP); 43111149SGeorge.Wilson@Sun.COM ddt_get_dedup_histogram(spa, ddh); 43211149SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64_array(config, 43311149SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_DDT_HISTOGRAM, 43411149SGeorge.Wilson@Sun.COM (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t)) == 0); 43511149SGeorge.Wilson@Sun.COM kmem_free(ddh, sizeof (ddt_histogram_t)); 43611149SGeorge.Wilson@Sun.COM 43711149SGeorge.Wilson@Sun.COM ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP); 43811149SGeorge.Wilson@Sun.COM ddt_get_dedup_object_stats(spa, ddo); 43911149SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64_array(config, 44011149SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_DDT_OBJ_STATS, 44111149SGeorge.Wilson@Sun.COM (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t)) == 0); 44211149SGeorge.Wilson@Sun.COM kmem_free(ddo, sizeof (ddt_object_t)); 44311149SGeorge.Wilson@Sun.COM 44411149SGeorge.Wilson@Sun.COM dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP); 44511149SGeorge.Wilson@Sun.COM ddt_get_dedup_stats(spa, dds); 44611149SGeorge.Wilson@Sun.COM VERIFY(nvlist_add_uint64_array(config, 44711149SGeorge.Wilson@Sun.COM ZPOOL_CONFIG_DDT_STATS, 44811149SGeorge.Wilson@Sun.COM (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t)) == 0); 44911149SGeorge.Wilson@Sun.COM kmem_free(dds, sizeof (ddt_stat_t)); 45011149SGeorge.Wilson@Sun.COM } 45111149SGeorge.Wilson@Sun.COM 45210921STim.Haley@Sun.COM spa_rewind_data_to_nvlist(spa, config); 45310921STim.Haley@Sun.COM 4547754SJeff.Bonwick@Sun.COM if (locked) 4557754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 4567754SJeff.Bonwick@Sun.COM 457789Sahrens return (config); 458789Sahrens } 4591635Sbonwick 4601635Sbonwick /* 4616423Sgw25295 * Update all disk labels, generate a fresh config based on the current 4626423Sgw25295 * in-core state, and sync the global config cache (do not sync the config 4636423Sgw25295 * cache if this is a booting rootpool). 4646423Sgw25295 */ 4656423Sgw25295 void 46610100SLin.Ling@Sun.COM spa_config_update(spa_t *spa, int what) 4676423Sgw25295 { 4681635Sbonwick vdev_t *rvd = spa->spa_root_vdev; 4691635Sbonwick uint64_t txg; 4701635Sbonwick int c; 4711635Sbonwick 4721635Sbonwick ASSERT(MUTEX_HELD(&spa_namespace_lock)); 4731635Sbonwick 4747754SJeff.Bonwick@Sun.COM spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 4751635Sbonwick txg = spa_last_synced_txg(spa) + 1; 4761635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) { 4771635Sbonwick vdev_config_dirty(rvd); 4781635Sbonwick } else { 4791635Sbonwick /* 4801635Sbonwick * If we have top-level vdevs that were added but have 4811635Sbonwick * not yet been prepared for allocation, do that now. 4821635Sbonwick * (It's safe now because the config cache is up to date, 4831635Sbonwick * so it will be able to translate the new DVAs.) 4841635Sbonwick * See comments in spa_vdev_add() for full details. 4851635Sbonwick */ 4861635Sbonwick for (c = 0; c < rvd->vdev_children; c++) { 4871635Sbonwick vdev_t *tvd = rvd->vdev_child[c]; 4889816SGeorge.Wilson@Sun.COM if (tvd->vdev_ms_array == 0) 4899816SGeorge.Wilson@Sun.COM vdev_metaslab_set_size(tvd); 4909816SGeorge.Wilson@Sun.COM vdev_expand(tvd, txg); 4911635Sbonwick } 4921635Sbonwick } 4937754SJeff.Bonwick@Sun.COM spa_config_exit(spa, SCL_ALL, FTAG); 4941635Sbonwick 4951635Sbonwick /* 4961635Sbonwick * Wait for the mosconfig to be regenerated and synced. 4971635Sbonwick */ 4981635Sbonwick txg_wait_synced(spa->spa_dsl_pool, txg); 4991635Sbonwick 5001635Sbonwick /* 5011635Sbonwick * Update the global config cache to reflect the new mosconfig. 5021635Sbonwick */ 50310100SLin.Ling@Sun.COM if (!spa->spa_is_root) 5046643Seschrock spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL); 5051635Sbonwick 5061635Sbonwick if (what == SPA_CONFIG_UPDATE_POOL) 50710100SLin.Ling@Sun.COM spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS); 5081635Sbonwick } 509