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.
25*b4952e17SGeorge Wilson * Copyright (c) 2013 by Delphix. All rights reserved.
26fa9e4066Sahrens */
27fa9e4066Sahrens
28fa9e4066Sahrens #include <sys/spa.h>
293cb69f73SWill Andrews #include <sys/fm/fs/zfs.h>
30fa9e4066Sahrens #include <sys/spa_impl.h>
31fa9e4066Sahrens #include <sys/nvpair.h>
32fa9e4066Sahrens #include <sys/uio.h>
33fa9e4066Sahrens #include <sys/fs/zfs.h>
34fa9e4066Sahrens #include <sys/vdev_impl.h>
35fa9e4066Sahrens #include <sys/zfs_ioctl.h>
3695173954Sek110237 #include <sys/utsname.h>
3795173954Sek110237 #include <sys/systeminfo.h>
3895173954Sek110237 #include <sys/sunddi.h>
39ad135b5dSChristopher Siden #include <sys/zfeature.h>
40ea8dc4b6Seschrock #ifdef _KERNEL
41ea8dc4b6Seschrock #include <sys/kobj.h>
425679c89fSjv227347 #include <sys/zone.h>
43ea8dc4b6Seschrock #endif
44ea8dc4b6Seschrock
45fa9e4066Sahrens /*
46fa9e4066Sahrens * Pool configuration repository.
47fa9e4066Sahrens *
482f8aaab3Seschrock * Pool configuration is stored as a packed nvlist on the filesystem. By
492f8aaab3Seschrock * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
502f8aaab3Seschrock * (when the ZFS module is loaded). Pools can also have the 'cachefile'
512f8aaab3Seschrock * property set that allows them to be stored in an alternate location until
522f8aaab3Seschrock * the control of external software.
53fa9e4066Sahrens *
542f8aaab3Seschrock * For each cache file, we have a single nvlist which holds all the
552f8aaab3Seschrock * configuration information. When the module loads, we read this information
562f8aaab3Seschrock * from /etc/zfs/zpool.cache and populate the SPA namespace. This namespace is
572f8aaab3Seschrock * maintained independently in spa.c. Whenever the namespace is modified, or
582f8aaab3Seschrock * the configuration of a pool is changed, we call spa_config_sync(), which
592f8aaab3Seschrock * walks through all the active pools and writes the configuration to disk.
60fa9e4066Sahrens */
61fa9e4066Sahrens
62fa9e4066Sahrens static uint64_t spa_config_generation = 1;
63fa9e4066Sahrens
64fa9e4066Sahrens /*
65fa9e4066Sahrens * This can be overridden in userland to preserve an alternate namespace for
66fa9e4066Sahrens * userland pools when doing testing.
67fa9e4066Sahrens */
68c5904d13Seschrock const char *spa_config_path = ZPOOL_CACHE;
69fa9e4066Sahrens
70fa9e4066Sahrens /*
71fa9e4066Sahrens * Called when the module is first loaded, this routine loads the configuration
72fa9e4066Sahrens * file into the SPA namespace. It does not actually open or load the pools; it
73fa9e4066Sahrens * only populates the namespace.
74fa9e4066Sahrens */
75fa9e4066Sahrens void
spa_config_load(void)76fa9e4066Sahrens spa_config_load(void)
77fa9e4066Sahrens {
78fa9e4066Sahrens void *buf = NULL;
79fa9e4066Sahrens nvlist_t *nvlist, *child;
80fa9e4066Sahrens nvpair_t *nvpair;
81e14bb325SJeff Bonwick char *pathname;
82ea8dc4b6Seschrock struct _buf *file;
83b1b8ab34Slling uint64_t fsize;
84fa9e4066Sahrens
85fa9e4066Sahrens /*
86fa9e4066Sahrens * Open the configuration file.
87fa9e4066Sahrens */
88e14bb325SJeff Bonwick pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
89e14bb325SJeff Bonwick
90e14bb325SJeff Bonwick (void) snprintf(pathname, MAXPATHLEN, "%s%s",
91c5904d13Seschrock (rootdir != NULL) ? "./" : "", spa_config_path);
92ea8dc4b6Seschrock
93ea8dc4b6Seschrock file = kobj_open_file(pathname);
94e14bb325SJeff Bonwick
95e14bb325SJeff Bonwick kmem_free(pathname, MAXPATHLEN);
96e14bb325SJeff Bonwick
97ea8dc4b6Seschrock if (file == (struct _buf *)-1)
98fa9e4066Sahrens return;
99fa9e4066Sahrens
100b1b8ab34Slling if (kobj_get_filesize(file, &fsize) != 0)
101ea8dc4b6Seschrock goto out;
102ea8dc4b6Seschrock
103b1b8ab34Slling buf = kmem_alloc(fsize, KM_SLEEP);
104ea8dc4b6Seschrock
105fa9e4066Sahrens /*
106fa9e4066Sahrens * Read the nvlist from the file.
107fa9e4066Sahrens */
108b1b8ab34Slling if (kobj_read_file(file, buf, fsize, 0) < 0)
109fa9e4066Sahrens goto out;
110fa9e4066Sahrens
111fa9e4066Sahrens /*
112fa9e4066Sahrens * Unpack the nvlist.
113fa9e4066Sahrens */
114b1b8ab34Slling if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
115fa9e4066Sahrens goto out;
116fa9e4066Sahrens
117fa9e4066Sahrens /*
118fa9e4066Sahrens * Iterate over all elements in the nvlist, creating a new spa_t for
119fa9e4066Sahrens * each one with the specified configuration.
120fa9e4066Sahrens */
121fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
122fa9e4066Sahrens nvpair = NULL;
123fa9e4066Sahrens while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
124fa9e4066Sahrens if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
125fa9e4066Sahrens continue;
126fa9e4066Sahrens
127fa9e4066Sahrens VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
128fa9e4066Sahrens
129fa9e4066Sahrens if (spa_lookup(nvpair_name(nvpair)) != NULL)
130fa9e4066Sahrens continue;
131468c413aSTim Haley (void) spa_add(nvpair_name(nvpair), child, NULL);
132fa9e4066Sahrens }
133fa9e4066Sahrens mutex_exit(&spa_namespace_lock);
134fa9e4066Sahrens
135fa9e4066Sahrens nvlist_free(nvlist);
136fa9e4066Sahrens
137fa9e4066Sahrens out:
138fa9e4066Sahrens if (buf != NULL)
139b1b8ab34Slling kmem_free(buf, fsize);
140fa9e4066Sahrens
141ea8dc4b6Seschrock kobj_close_file(file);
142fa9e4066Sahrens }
143fa9e4066Sahrens
1443cb69f73SWill Andrews static int
spa_config_write(spa_config_dirent_t * dp,nvlist_t * nvl)145c5904d13Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
1462f8aaab3Seschrock {
147fa9e4066Sahrens size_t buflen;
148fa9e4066Sahrens char *buf;
149fa9e4066Sahrens vnode_t *vp;
150fa9e4066Sahrens int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
151e14bb325SJeff Bonwick char *temp;
1523cb69f73SWill Andrews int err;
153c5904d13Seschrock
154c5904d13Seschrock /*
155c5904d13Seschrock * If the nvlist is empty (NULL), then remove the old cachefile.
156c5904d13Seschrock */
157c5904d13Seschrock if (nvl == NULL) {
1583cb69f73SWill Andrews err = vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
1593cb69f73SWill Andrews return (err);
160c5904d13Seschrock }
161fa9e4066Sahrens
162fa9e4066Sahrens /*
163fa9e4066Sahrens * Pack the configuration into a buffer.
164fa9e4066Sahrens */
165c5904d13Seschrock VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0);
166fa9e4066Sahrens
167fa9e4066Sahrens buf = kmem_alloc(buflen, KM_SLEEP);
168e14bb325SJeff Bonwick temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
169fa9e4066Sahrens
170c5904d13Seschrock VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR,
171ea8dc4b6Seschrock KM_SLEEP) == 0);
172fa9e4066Sahrens
173fa9e4066Sahrens /*
174fa9e4066Sahrens * Write the configuration to disk. We need to do the traditional
175fa9e4066Sahrens * 'write to temporary file, sync, move over original' to make sure we
176fa9e4066Sahrens * always have a consistent view of the data.
177fa9e4066Sahrens */
178e14bb325SJeff Bonwick (void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
179fa9e4066Sahrens
1803cb69f73SWill Andrews err = vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0);
1813cb69f73SWill Andrews if (err == 0) {
1823cb69f73SWill Andrews err = vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
1833cb69f73SWill Andrews 0, RLIM64_INFINITY, kcred, NULL);
1843cb69f73SWill Andrews if (err == 0)
1853cb69f73SWill Andrews err = VOP_FSYNC(vp, FSYNC, kcred, NULL);
1863cb69f73SWill Andrews if (err == 0)
1873cb69f73SWill Andrews err = vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
188da6c28aaSamw (void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
189fa9e4066Sahrens VN_RELE(vp);
190e14bb325SJeff Bonwick }
191fa9e4066Sahrens
192e14bb325SJeff Bonwick (void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
193e14bb325SJeff Bonwick
194fa9e4066Sahrens kmem_free(buf, buflen);
195e14bb325SJeff Bonwick kmem_free(temp, MAXPATHLEN);
1963cb69f73SWill Andrews return (err);
1972f8aaab3Seschrock }
1982f8aaab3Seschrock
1992f8aaab3Seschrock /*
200c5904d13Seschrock * Synchronize pool configuration to disk. This must be called with the
201*b4952e17SGeorge Wilson * namespace lock held. Synchronizing the pool cache is typically done after
202*b4952e17SGeorge Wilson * the configuration has been synced to the MOS. This exposes a window where
203*b4952e17SGeorge Wilson * the MOS config will have been updated but the cache file has not. If
204*b4952e17SGeorge Wilson * the system were to crash at that instant then the cached config may not
205*b4952e17SGeorge Wilson * contain the correct information to open the pool and an explicity import
206*b4952e17SGeorge Wilson * would be required.
2072f8aaab3Seschrock */
2082f8aaab3Seschrock void
spa_config_sync(spa_t * target,boolean_t removing,boolean_t postsysevent)209c5904d13Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent)
2102f8aaab3Seschrock {
211c5904d13Seschrock spa_config_dirent_t *dp, *tdp;
212c5904d13Seschrock nvlist_t *nvl;
2133cb69f73SWill Andrews boolean_t ccw_failure;
2143cb69f73SWill Andrews int error;
2152f8aaab3Seschrock
2162f8aaab3Seschrock ASSERT(MUTEX_HELD(&spa_namespace_lock));
2172f8aaab3Seschrock
2184f0f5e5bSVictor Latushkin if (rootdir == NULL || !(spa_mode_global & FWRITE))
2199fddc96aSGeorge Wilson return;
2209fddc96aSGeorge Wilson
221c5904d13Seschrock /*
222c5904d13Seschrock * Iterate over all cachefiles for the pool, past or present. When the
223c5904d13Seschrock * cachefile is changed, the new one is pushed onto this list, allowing
224c5904d13Seschrock * us to update previous cachefiles that no longer contain this pool.
225c5904d13Seschrock */
2263cb69f73SWill Andrews ccw_failure = B_FALSE;
227c5904d13Seschrock for (dp = list_head(&target->spa_config_list); dp != NULL;
228c5904d13Seschrock dp = list_next(&target->spa_config_list, dp)) {
229e14bb325SJeff Bonwick spa_t *spa = NULL;
230c5904d13Seschrock if (dp->scd_path == NULL)
231c5904d13Seschrock continue;
2322f8aaab3Seschrock
2332f8aaab3Seschrock /*
234c5904d13Seschrock * Iterate over all pools, adding any matching pools to 'nvl'.
2352f8aaab3Seschrock */
236c5904d13Seschrock nvl = NULL;
237c5904d13Seschrock while ((spa = spa_next(spa)) != NULL) {
238fb02ae02SGeorge Wilson /*
239fb02ae02SGeorge Wilson * Skip over our own pool if we're about to remove
240fb02ae02SGeorge Wilson * ourselves from the spa namespace or any pool that
241fb02ae02SGeorge Wilson * is readonly. Since we cannot guarantee that a
242fb02ae02SGeorge Wilson * readonly pool would successfully import upon reboot,
243fb02ae02SGeorge Wilson * we don't allow them to be written to the cache file.
244fb02ae02SGeorge Wilson */
245fb02ae02SGeorge Wilson if ((spa == target && removing) ||
246fb02ae02SGeorge Wilson !spa_writeable(spa))
247c5904d13Seschrock continue;
248c5904d13Seschrock
249e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock);
250c5904d13Seschrock tdp = list_head(&spa->spa_config_list);
251e14bb325SJeff Bonwick if (spa->spa_config == NULL ||
252e14bb325SJeff Bonwick tdp->scd_path == NULL ||
253e14bb325SJeff Bonwick strcmp(tdp->scd_path, dp->scd_path) != 0) {
254e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock);
255c5904d13Seschrock continue;
256e14bb325SJeff Bonwick }
257c5904d13Seschrock
258c5904d13Seschrock if (nvl == NULL)
259c5904d13Seschrock VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
260c5904d13Seschrock KM_SLEEP) == 0);
261c5904d13Seschrock
262c5904d13Seschrock VERIFY(nvlist_add_nvlist(nvl, spa->spa_name,
263c5904d13Seschrock spa->spa_config) == 0);
264e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock);
265c5904d13Seschrock }
266c5904d13Seschrock
2673cb69f73SWill Andrews error = spa_config_write(dp, nvl);
2683cb69f73SWill Andrews if (error != 0)
2693cb69f73SWill Andrews ccw_failure = B_TRUE;
270c5904d13Seschrock nvlist_free(nvl);
271c5904d13Seschrock }
272c5904d13Seschrock
2733cb69f73SWill Andrews if (ccw_failure) {
2743cb69f73SWill Andrews /*
2753cb69f73SWill Andrews * Keep trying so that configuration data is
2763cb69f73SWill Andrews * written if/when any temporary filesystem
2773cb69f73SWill Andrews * resource issues are resolved.
2783cb69f73SWill Andrews */
2793cb69f73SWill Andrews if (target->spa_ccw_fail_time == 0) {
2803cb69f73SWill Andrews zfs_ereport_post(FM_EREPORT_ZFS_CONFIG_CACHE_WRITE,
2813cb69f73SWill Andrews target, NULL, NULL, 0, 0);
2823cb69f73SWill Andrews }
2833cb69f73SWill Andrews target->spa_ccw_fail_time = gethrtime();
2843cb69f73SWill Andrews spa_async_request(target, SPA_ASYNC_CONFIG_UPDATE);
2853cb69f73SWill Andrews } else {
2863cb69f73SWill Andrews /*
2873cb69f73SWill Andrews * Do not rate limit future attempts to update
2883cb69f73SWill Andrews * the config cache.
2893cb69f73SWill Andrews */
2903cb69f73SWill Andrews target->spa_ccw_fail_time = 0;
2913cb69f73SWill Andrews }
2923cb69f73SWill Andrews
293c5904d13Seschrock /*
294c5904d13Seschrock * Remove any config entries older than the current one.
295c5904d13Seschrock */
296c5904d13Seschrock dp = list_head(&target->spa_config_list);
297c5904d13Seschrock while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
298c5904d13Seschrock list_remove(&target->spa_config_list, tdp);
299c5904d13Seschrock if (tdp->scd_path != NULL)
300c5904d13Seschrock spa_strfree(tdp->scd_path);
301c5904d13Seschrock kmem_free(tdp, sizeof (spa_config_dirent_t));
3022f8aaab3Seschrock }
3032f8aaab3Seschrock
3042f8aaab3Seschrock spa_config_generation++;
305c5904d13Seschrock
306c5904d13Seschrock if (postsysevent)
307c5904d13Seschrock spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC);
308fa9e4066Sahrens }
309fa9e4066Sahrens
310fa9e4066Sahrens /*
3110373e76bSbonwick * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
312fa9e4066Sahrens * and we don't want to allow the local zone to see all the pools anyway.
313fa9e4066Sahrens * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
314fa9e4066Sahrens * information for all pool visible within the zone.
315fa9e4066Sahrens */
316fa9e4066Sahrens nvlist_t *
spa_all_configs(uint64_t * generation)317fa9e4066Sahrens spa_all_configs(uint64_t *generation)
318fa9e4066Sahrens {
319fa9e4066Sahrens nvlist_t *pools;
320e14bb325SJeff Bonwick spa_t *spa = NULL;
321fa9e4066Sahrens
322fa9e4066Sahrens if (*generation == spa_config_generation)
323fa9e4066Sahrens return (NULL);
324fa9e4066Sahrens
325ea8dc4b6Seschrock VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
326fa9e4066Sahrens
327fa9e4066Sahrens mutex_enter(&spa_namespace_lock);
328fa9e4066Sahrens while ((spa = spa_next(spa)) != NULL) {
329fa9e4066Sahrens if (INGLOBALZONE(curproc) ||
330fa9e4066Sahrens zone_dataset_visible(spa_name(spa), NULL)) {
331e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock);
332fa9e4066Sahrens VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
333fa9e4066Sahrens spa->spa_config) == 0);
334e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock);
335fa9e4066Sahrens }
336fa9e4066Sahrens }
337fa9e4066Sahrens *generation = spa_config_generation;
338e14bb325SJeff Bonwick mutex_exit(&spa_namespace_lock);
339fa9e4066Sahrens
340fa9e4066Sahrens return (pools);
341fa9e4066Sahrens }
342fa9e4066Sahrens
343fa9e4066Sahrens void
spa_config_set(spa_t * spa,nvlist_t * config)344fa9e4066Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
345fa9e4066Sahrens {
346e14bb325SJeff Bonwick mutex_enter(&spa->spa_props_lock);
347fa9e4066Sahrens nvlist_free(spa->spa_config);
348fa9e4066Sahrens spa->spa_config = config;
349e14bb325SJeff Bonwick mutex_exit(&spa->spa_props_lock);
350fa9e4066Sahrens }
351fa9e4066Sahrens
352fa9e4066Sahrens /*
353fa9e4066Sahrens * Generate the pool's configuration based on the current in-core state.
354f7170741SWill Andrews *
355fa9e4066Sahrens * We infer whether to generate a complete config or just one top-level config
356fa9e4066Sahrens * based on whether vd is the root vdev.
357fa9e4066Sahrens */
358fa9e4066Sahrens nvlist_t *
spa_config_generate(spa_t * spa,vdev_t * vd,uint64_t txg,int getstats)359fa9e4066Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
360fa9e4066Sahrens {
361fa9e4066Sahrens nvlist_t *config, *nvroot;
362fa9e4066Sahrens vdev_t *rvd = spa->spa_root_vdev;
36395173954Sek110237 unsigned long hostid = 0;
364e14bb325SJeff Bonwick boolean_t locked = B_FALSE;
3651195e687SMark J Musante uint64_t split_guid;
366fa9e4066Sahrens
367e14bb325SJeff Bonwick if (vd == NULL) {
368fa9e4066Sahrens vd = rvd;
369e14bb325SJeff Bonwick locked = B_TRUE;
370e14bb325SJeff Bonwick spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
371e14bb325SJeff Bonwick }
372e14bb325SJeff Bonwick
373e14bb325SJeff Bonwick ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
374e14bb325SJeff Bonwick (SCL_CONFIG | SCL_STATE));
375fa9e4066Sahrens
376fa9e4066Sahrens /*
377fa9e4066Sahrens * If txg is -1, report the current value of spa->spa_config_txg.
378fa9e4066Sahrens */
379fa9e4066Sahrens if (txg == -1ULL)
380fa9e4066Sahrens txg = spa->spa_config_txg;
381fa9e4066Sahrens
382ea8dc4b6Seschrock VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
383fa9e4066Sahrens
384fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
38599653d4eSeschrock spa_version(spa)) == 0);
386fa9e4066Sahrens VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
387fa9e4066Sahrens spa_name(spa)) == 0);
388fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
389fa9e4066Sahrens spa_state(spa)) == 0);
390fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
391fa9e4066Sahrens txg) == 0);
392fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
393fa9e4066Sahrens spa_guid(spa)) == 0);
3948704186eSDan McDonald VERIFY(spa->spa_comment == NULL || nvlist_add_string(config,
3958704186eSDan McDonald ZPOOL_CONFIG_COMMENT, spa->spa_comment) == 0);
3968704186eSDan McDonald
3978704186eSDan McDonald
3985679c89fSjv227347 #ifdef _KERNEL
3995679c89fSjv227347 hostid = zone_get_hostid(NULL);
4005679c89fSjv227347 #else /* _KERNEL */
4015679c89fSjv227347 /*
4025679c89fSjv227347 * We're emulating the system's hostid in userland, so we can't use
4035679c89fSjv227347 * zone_get_hostid().
4045679c89fSjv227347 */
40595173954Sek110237 (void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
4065679c89fSjv227347 #endif /* _KERNEL */
40717194a52Slling if (hostid != 0) {
40895173954Sek110237 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
40995173954Sek110237 hostid) == 0);
41017194a52Slling }
41195173954Sek110237 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
41295173954Sek110237 utsname.nodename) == 0);
413fa9e4066Sahrens
414fa9e4066Sahrens if (vd != rvd) {
415fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
416fa9e4066Sahrens vd->vdev_top->vdev_guid) == 0);
417fa9e4066Sahrens VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
418fa9e4066Sahrens vd->vdev_guid) == 0);
41999653d4eSeschrock if (vd->vdev_isspare)
42099653d4eSeschrock VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
4218654d025Sperrin 1ULL) == 0);
4228654d025Sperrin if (vd->vdev_islog)
4238654d025Sperrin VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
42499653d4eSeschrock 1ULL) == 0);
425fa9e4066Sahrens vd = vd->vdev_top; /* label contains top config */
4261195e687SMark J Musante } else {
4271195e687SMark J Musante /*
4281195e687SMark J Musante * Only add the (potentially large) split information
4291195e687SMark J Musante * in the mos config, and not in the vdev labels
4301195e687SMark J Musante */
4311195e687SMark J Musante if (spa->spa_config_splitting != NULL)
4321195e687SMark J Musante VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
4331195e687SMark J Musante spa->spa_config_splitting) == 0);
434fa9e4066Sahrens }
435fa9e4066Sahrens
43688ecc943SGeorge Wilson /*
43788ecc943SGeorge Wilson * Add the top-level config. We even add this on pools which
4384b964adaSGeorge Wilson * don't support holes in the namespace.
43988ecc943SGeorge Wilson */
44088ecc943SGeorge Wilson vdev_top_config_generate(spa, config);
44188ecc943SGeorge Wilson
4421195e687SMark J Musante /*
4431195e687SMark J Musante * If we're splitting, record the original pool's guid.
4441195e687SMark J Musante */
4451195e687SMark J Musante if (spa->spa_config_splitting != NULL &&
4461195e687SMark J Musante nvlist_lookup_uint64(spa->spa_config_splitting,
4471195e687SMark J Musante ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
4481195e687SMark J Musante VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID,
4491195e687SMark J Musante split_guid) == 0);
4501195e687SMark J Musante }
4511195e687SMark J Musante
4523f9d6ad7SLin Ling nvroot = vdev_config_generate(spa, vd, getstats, 0);
453fa9e4066Sahrens VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
454fa9e4066Sahrens nvlist_free(nvroot);
455fa9e4066Sahrens
456ad135b5dSChristopher Siden /*
457ad135b5dSChristopher Siden * Store what's necessary for reading the MOS in the label.
458ad135b5dSChristopher Siden */
459ad135b5dSChristopher Siden VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
460ad135b5dSChristopher Siden spa->spa_label_features) == 0);
461ad135b5dSChristopher Siden
4629eb19f4dSGeorge Wilson if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
4639eb19f4dSGeorge Wilson ddt_histogram_t *ddh;
4649eb19f4dSGeorge Wilson ddt_stat_t *dds;
4659eb19f4dSGeorge Wilson ddt_object_t *ddo;
4669eb19f4dSGeorge Wilson
4679eb19f4dSGeorge Wilson ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
4689eb19f4dSGeorge Wilson ddt_get_dedup_histogram(spa, ddh);
4699eb19f4dSGeorge Wilson VERIFY(nvlist_add_uint64_array(config,
4709eb19f4dSGeorge Wilson ZPOOL_CONFIG_DDT_HISTOGRAM,
4719eb19f4dSGeorge Wilson (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t)) == 0);
4729eb19f4dSGeorge Wilson kmem_free(ddh, sizeof (ddt_histogram_t));
4739eb19f4dSGeorge Wilson
4749eb19f4dSGeorge Wilson ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
4759eb19f4dSGeorge Wilson ddt_get_dedup_object_stats(spa, ddo);
4769eb19f4dSGeorge Wilson VERIFY(nvlist_add_uint64_array(config,
4779eb19f4dSGeorge Wilson ZPOOL_CONFIG_DDT_OBJ_STATS,
4789eb19f4dSGeorge Wilson (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t)) == 0);
4799eb19f4dSGeorge Wilson kmem_free(ddo, sizeof (ddt_object_t));
4809eb19f4dSGeorge Wilson
4819eb19f4dSGeorge Wilson dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
4829eb19f4dSGeorge Wilson ddt_get_dedup_stats(spa, dds);
4839eb19f4dSGeorge Wilson VERIFY(nvlist_add_uint64_array(config,
4849eb19f4dSGeorge Wilson ZPOOL_CONFIG_DDT_STATS,
4859eb19f4dSGeorge Wilson (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t)) == 0);
4869eb19f4dSGeorge Wilson kmem_free(dds, sizeof (ddt_stat_t));
4879eb19f4dSGeorge Wilson }
4889eb19f4dSGeorge Wilson
489e14bb325SJeff Bonwick if (locked)
490e14bb325SJeff Bonwick spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
491e14bb325SJeff Bonwick
492fa9e4066Sahrens return (config);
493fa9e4066Sahrens }
4940373e76bSbonwick
4950373e76bSbonwick /*
496e7cbe64fSgw25295 * Update all disk labels, generate a fresh config based on the current
497e7cbe64fSgw25295 * in-core state, and sync the global config cache (do not sync the config
498e7cbe64fSgw25295 * cache if this is a booting rootpool).
499e7cbe64fSgw25295 */
500e7cbe64fSgw25295 void
spa_config_update(spa_t * spa,int what)501bc758434SLin Ling spa_config_update(spa_t *spa, int what)
502e7cbe64fSgw25295 {
5030373e76bSbonwick vdev_t *rvd = spa->spa_root_vdev;
5040373e76bSbonwick uint64_t txg;
5050373e76bSbonwick int c;
5060373e76bSbonwick
5070373e76bSbonwick ASSERT(MUTEX_HELD(&spa_namespace_lock));
5080373e76bSbonwick
509e14bb325SJeff Bonwick spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5100373e76bSbonwick txg = spa_last_synced_txg(spa) + 1;
5110373e76bSbonwick if (what == SPA_CONFIG_UPDATE_POOL) {
5120373e76bSbonwick vdev_config_dirty(rvd);
5130373e76bSbonwick } else {
5140373e76bSbonwick /*
5150373e76bSbonwick * If we have top-level vdevs that were added but have
5160373e76bSbonwick * not yet been prepared for allocation, do that now.
5170373e76bSbonwick * (It's safe now because the config cache is up to date,
5180373e76bSbonwick * so it will be able to translate the new DVAs.)
5190373e76bSbonwick * See comments in spa_vdev_add() for full details.
5200373e76bSbonwick */
5210373e76bSbonwick for (c = 0; c < rvd->vdev_children; c++) {
5220373e76bSbonwick vdev_t *tvd = rvd->vdev_child[c];
523573ca77eSGeorge Wilson if (tvd->vdev_ms_array == 0)
524573ca77eSGeorge Wilson vdev_metaslab_set_size(tvd);
525573ca77eSGeorge Wilson vdev_expand(tvd, txg);
5260373e76bSbonwick }
5270373e76bSbonwick }
528e14bb325SJeff Bonwick spa_config_exit(spa, SCL_ALL, FTAG);
5290373e76bSbonwick
5300373e76bSbonwick /*
5310373e76bSbonwick * Wait for the mosconfig to be regenerated and synced.
5320373e76bSbonwick */
5330373e76bSbonwick txg_wait_synced(spa->spa_dsl_pool, txg);
5340373e76bSbonwick
5350373e76bSbonwick /*
5360373e76bSbonwick * Update the global config cache to reflect the new mosconfig.
5370373e76bSbonwick */
538bc758434SLin Ling if (!spa->spa_is_root)
539c5904d13Seschrock spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL);
5400373e76bSbonwick
5410373e76bSbonwick if (what == SPA_CONFIG_UPDATE_POOL)
542bc758434SLin Ling spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
5430373e76bSbonwick }
544