xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision 9fddc96a)
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 /*
23e7cbe64fSgw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/spa.h>
28fa9e4066Sahrens #include <sys/spa_impl.h>
29fa9e4066Sahrens #include <sys/nvpair.h>
30fa9e4066Sahrens #include <sys/uio.h>
31fa9e4066Sahrens #include <sys/fs/zfs.h>
32fa9e4066Sahrens #include <sys/vdev_impl.h>
33fa9e4066Sahrens #include <sys/zfs_ioctl.h>
3495173954Sek #include <sys/utsname.h>
3595173954Sek #include <sys/systeminfo.h>
3695173954Sek #include <sys/sunddi.h>
37ea8dc4b6Seschrock #ifdef _KERNEL
38ea8dc4b6Seschrock #include <sys/kobj.h>
39ea8dc4b6Seschrock #endif
40ea8dc4b6Seschrock 
41fa9e4066Sahrens /*
42fa9e4066Sahrens  * Pool configuration repository.
43fa9e4066Sahrens  *
442f8aaab3Seschrock  * Pool configuration is stored as a packed nvlist on the filesystem.  By
452f8aaab3Seschrock  * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
462f8aaab3Seschrock  * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
472f8aaab3Seschrock  * property set that allows them to be stored in an alternate location until
482f8aaab3Seschrock  * the control of external software.
49fa9e4066Sahrens  *
502f8aaab3Seschrock  * For each cache file, we have a single nvlist which holds all the
512f8aaab3Seschrock  * configuration information.  When the module loads, we read this information
522f8aaab3Seschrock  * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
532f8aaab3Seschrock  * maintained independently in spa.c.  Whenever the namespace is modified, or
542f8aaab3Seschrock  * the configuration of a pool is changed, we call spa_config_sync(), which
552f8aaab3Seschrock  * walks through all the active pools and writes the configuration to disk.
56fa9e4066Sahrens  */
57fa9e4066Sahrens 
58fa9e4066Sahrens static uint64_t spa_config_generation = 1;
59fa9e4066Sahrens 
60fa9e4066Sahrens /*
61fa9e4066Sahrens  * This can be overridden in userland to preserve an alternate namespace for
62fa9e4066Sahrens  * userland pools when doing testing.
63fa9e4066Sahrens  */
64c5904d13Seschrock const char *spa_config_path = ZPOOL_CACHE;
65fa9e4066Sahrens 
66fa9e4066Sahrens /*
67fa9e4066Sahrens  * Called when the module is first loaded, this routine loads the configuration
68fa9e4066Sahrens  * file into the SPA namespace.  It does not actually open or load the pools; it
69fa9e4066Sahrens  * only populates the namespace.
70fa9e4066Sahrens  */
71fa9e4066Sahrens void
72fa9e4066Sahrens spa_config_load(void)
73fa9e4066Sahrens {
74fa9e4066Sahrens 	void *buf = NULL;
75fa9e4066Sahrens 	nvlist_t *nvlist, *child;
76fa9e4066Sahrens 	nvpair_t *nvpair;
77fa9e4066Sahrens 	spa_t *spa;
78e14bb325SJeff Bonwick 	char *pathname;
79ea8dc4b6Seschrock 	struct _buf *file;
80b1b8ab34Slling 	uint64_t fsize;
81fa9e4066Sahrens 
82fa9e4066Sahrens 	/*
83fa9e4066Sahrens 	 * Open the configuration file.
84fa9e4066Sahrens 	 */
85e14bb325SJeff Bonwick 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
86e14bb325SJeff Bonwick 
87e14bb325SJeff Bonwick 	(void) snprintf(pathname, MAXPATHLEN, "%s%s",
88c5904d13Seschrock 	    (rootdir != NULL) ? "./" : "", spa_config_path);
89ea8dc4b6Seschrock 
90ea8dc4b6Seschrock 	file = kobj_open_file(pathname);
91e14bb325SJeff Bonwick 
92e14bb325SJeff Bonwick 	kmem_free(pathname, MAXPATHLEN);
93e14bb325SJeff Bonwick 
94ea8dc4b6Seschrock 	if (file == (struct _buf *)-1)
95fa9e4066Sahrens 		return;
96fa9e4066Sahrens 
97b1b8ab34Slling 	if (kobj_get_filesize(file, &fsize) != 0)
98fa9e4066Sahrens 		goto out;
99fa9e4066Sahrens 
100b1b8ab34Slling 	buf = kmem_alloc(fsize, KM_SLEEP);
101fa9e4066Sahrens 
102ea8dc4b6Seschrock 	/*
103ea8dc4b6Seschrock 	 * Read the nvlist from the file.
104ea8dc4b6Seschrock 	 */
105b1b8ab34Slling 	if (kobj_read_file(file, buf, fsize, 0) < 0)
106fa9e4066Sahrens 		goto out;
107fa9e4066Sahrens 
108fa9e4066Sahrens 	/*
109fa9e4066Sahrens 	 * Unpack the nvlist.
110fa9e4066Sahrens 	 */
111b1b8ab34Slling 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
112fa9e4066Sahrens 		goto out;
113fa9e4066Sahrens 
114fa9e4066Sahrens 	/*
115fa9e4066Sahrens 	 * Iterate over all elements in the nvlist, creating a new spa_t for
116fa9e4066Sahrens 	 * each one with the specified configuration.
117fa9e4066Sahrens 	 */
118fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
119fa9e4066Sahrens 	nvpair = NULL;
120fa9e4066Sahrens 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
121fa9e4066Sahrens 
122fa9e4066Sahrens 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
123fa9e4066Sahrens 			continue;
124fa9e4066Sahrens 
125fa9e4066Sahrens 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
126fa9e4066Sahrens 
127fa9e4066Sahrens 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
128fa9e4066Sahrens 			continue;
1290373e76bSbonwick 		spa = spa_add(nvpair_name(nvpair), NULL);
130fa9e4066Sahrens 
131fa9e4066Sahrens 		/*
132fa9e4066Sahrens 		 * We blindly duplicate the configuration here.  If it's
133fa9e4066Sahrens 		 * invalid, we will catch it when the pool is first opened.
134fa9e4066Sahrens 		 */
135fa9e4066Sahrens 		VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0);
136fa9e4066Sahrens 	}
137fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
138fa9e4066Sahrens 
139fa9e4066Sahrens 	nvlist_free(nvlist);
140fa9e4066Sahrens 
141fa9e4066Sahrens out:
142fa9e4066Sahrens 	if (buf != NULL)
143b1b8ab34Slling 		kmem_free(buf, fsize);
144fa9e4066Sahrens 
145ea8dc4b6Seschrock 	kobj_close_file(file);
146fa9e4066Sahrens }
147fa9e4066Sahrens 
1482f8aaab3Seschrock static void
149c5904d13Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
1502f8aaab3Seschrock {
1512f8aaab3Seschrock 	size_t buflen;
1522f8aaab3Seschrock 	char *buf;
1532f8aaab3Seschrock 	vnode_t *vp;
1542f8aaab3Seschrock 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
155e14bb325SJeff Bonwick 	char *temp;
156c5904d13Seschrock 
157c5904d13Seschrock 	/*
158c5904d13Seschrock 	 * If the nvlist is empty (NULL), then remove the old cachefile.
159c5904d13Seschrock 	 */
160c5904d13Seschrock 	if (nvl == NULL) {
161c5904d13Seschrock 		(void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
162c5904d13Seschrock 		return;
163c5904d13Seschrock 	}
1642f8aaab3Seschrock 
165fa9e4066Sahrens 	/*
166fa9e4066Sahrens 	 * Pack the configuration into a buffer.
167fa9e4066Sahrens 	 */
168c5904d13Seschrock 	VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0);
169fa9e4066Sahrens 
170fa9e4066Sahrens 	buf = kmem_alloc(buflen, KM_SLEEP);
171e14bb325SJeff Bonwick 	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
172fa9e4066Sahrens 
173c5904d13Seschrock 	VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR,
174ea8dc4b6Seschrock 	    KM_SLEEP) == 0);
175fa9e4066Sahrens 
176fa9e4066Sahrens 	/*
177fa9e4066Sahrens 	 * Write the configuration to disk.  We need to do the traditional
178fa9e4066Sahrens 	 * 'write to temporary file, sync, move over original' to make sure we
179fa9e4066Sahrens 	 * always have a consistent view of the data.
180fa9e4066Sahrens 	 */
181e14bb325SJeff Bonwick 	(void) snprintf(temp, MAXPATHLEN, "%s.tmp", dp->scd_path);
182fa9e4066Sahrens 
183e14bb325SJeff Bonwick 	if (vn_open(temp, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) == 0) {
184e14bb325SJeff Bonwick 		if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
185e14bb325SJeff Bonwick 		    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
186e14bb325SJeff Bonwick 		    VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) {
187e14bb325SJeff Bonwick 			(void) vn_rename(temp, dp->scd_path, UIO_SYSSPACE);
188e14bb325SJeff Bonwick 		}
189e14bb325SJeff Bonwick 		(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
190e14bb325SJeff Bonwick 		VN_RELE(vp);
191fa9e4066Sahrens 	}
192fa9e4066Sahrens 
193e14bb325SJeff Bonwick 	(void) vn_remove(temp, UIO_SYSSPACE, RMFILE);
194fa9e4066Sahrens 
195fa9e4066Sahrens 	kmem_free(buf, buflen);
196e14bb325SJeff Bonwick 	kmem_free(temp, MAXPATHLEN);
1972f8aaab3Seschrock }
1982f8aaab3Seschrock 
1992f8aaab3Seschrock /*
200c5904d13Seschrock  * Synchronize pool configuration to disk.  This must be called with the
201c5904d13Seschrock  * namespace lock held.
2022f8aaab3Seschrock  */
2032f8aaab3Seschrock void
204c5904d13Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent)
2052f8aaab3Seschrock {
206c5904d13Seschrock 	spa_config_dirent_t *dp, *tdp;
207c5904d13Seschrock 	nvlist_t *nvl;
2082f8aaab3Seschrock 
2092f8aaab3Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
2102f8aaab3Seschrock 
211*9fddc96aSGeorge Wilson 	if (rootdir == NULL)
212*9fddc96aSGeorge Wilson 		return;
213*9fddc96aSGeorge Wilson 
214c5904d13Seschrock 	/*
215c5904d13Seschrock 	 * Iterate over all cachefiles for the pool, past or present.  When the
216c5904d13Seschrock 	 * cachefile is changed, the new one is pushed onto this list, allowing
217c5904d13Seschrock 	 * us to update previous cachefiles that no longer contain this pool.
218c5904d13Seschrock 	 */
219c5904d13Seschrock 	for (dp = list_head(&target->spa_config_list); dp != NULL;
220c5904d13Seschrock 	    dp = list_next(&target->spa_config_list, dp)) {
221e14bb325SJeff Bonwick 		spa_t *spa = NULL;
222c5904d13Seschrock 		if (dp->scd_path == NULL)
223c5904d13Seschrock 			continue;
224c5904d13Seschrock 
225c5904d13Seschrock 		/*
226c5904d13Seschrock 		 * Iterate over all pools, adding any matching pools to 'nvl'.
227c5904d13Seschrock 		 */
228c5904d13Seschrock 		nvl = NULL;
229c5904d13Seschrock 		while ((spa = spa_next(spa)) != NULL) {
230c5904d13Seschrock 			if (spa == target && removing)
231c5904d13Seschrock 				continue;
232c5904d13Seschrock 
233e14bb325SJeff Bonwick 			mutex_enter(&spa->spa_props_lock);
234c5904d13Seschrock 			tdp = list_head(&spa->spa_config_list);
235e14bb325SJeff Bonwick 			if (spa->spa_config == NULL ||
236e14bb325SJeff Bonwick 			    tdp->scd_path == NULL ||
237e14bb325SJeff Bonwick 			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
238e14bb325SJeff Bonwick 				mutex_exit(&spa->spa_props_lock);
239c5904d13Seschrock 				continue;
240e14bb325SJeff Bonwick 			}
241c5904d13Seschrock 
242c5904d13Seschrock 			if (nvl == NULL)
243c5904d13Seschrock 				VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
244c5904d13Seschrock 				    KM_SLEEP) == 0);
245c5904d13Seschrock 
246c5904d13Seschrock 			VERIFY(nvlist_add_nvlist(nvl, spa->spa_name,
247c5904d13Seschrock 			    spa->spa_config) == 0);
248e14bb325SJeff Bonwick 			mutex_exit(&spa->spa_props_lock);
249c5904d13Seschrock 		}
250c5904d13Seschrock 
251c5904d13Seschrock 		spa_config_write(dp, nvl);
252c5904d13Seschrock 		nvlist_free(nvl);
253c5904d13Seschrock 	}
2542f8aaab3Seschrock 
2552f8aaab3Seschrock 	/*
256c5904d13Seschrock 	 * Remove any config entries older than the current one.
2572f8aaab3Seschrock 	 */
258c5904d13Seschrock 	dp = list_head(&target->spa_config_list);
259c5904d13Seschrock 	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
260c5904d13Seschrock 		list_remove(&target->spa_config_list, tdp);
261c5904d13Seschrock 		if (tdp->scd_path != NULL)
262c5904d13Seschrock 			spa_strfree(tdp->scd_path);
263c5904d13Seschrock 		kmem_free(tdp, sizeof (spa_config_dirent_t));
2642f8aaab3Seschrock 	}
2652f8aaab3Seschrock 
2662f8aaab3Seschrock 	spa_config_generation++;
267c5904d13Seschrock 
268c5904d13Seschrock 	if (postsysevent)
269c5904d13Seschrock 		spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC);
270fa9e4066Sahrens }
271fa9e4066Sahrens 
272fa9e4066Sahrens /*
2730373e76bSbonwick  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
274fa9e4066Sahrens  * and we don't want to allow the local zone to see all the pools anyway.
275fa9e4066Sahrens  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
276fa9e4066Sahrens  * information for all pool visible within the zone.
277fa9e4066Sahrens  */
278fa9e4066Sahrens nvlist_t *
279fa9e4066Sahrens spa_all_configs(uint64_t *generation)
280fa9e4066Sahrens {
281fa9e4066Sahrens 	nvlist_t *pools;
282e14bb325SJeff Bonwick 	spa_t *spa = NULL;
283fa9e4066Sahrens 
284fa9e4066Sahrens 	if (*generation == spa_config_generation)
285fa9e4066Sahrens 		return (NULL);
286fa9e4066Sahrens 
287ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
288fa9e4066Sahrens 
289fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
290fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL) {
291fa9e4066Sahrens 		if (INGLOBALZONE(curproc) ||
292fa9e4066Sahrens 		    zone_dataset_visible(spa_name(spa), NULL)) {
293e14bb325SJeff Bonwick 			mutex_enter(&spa->spa_props_lock);
294fa9e4066Sahrens 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
295fa9e4066Sahrens 			    spa->spa_config) == 0);
296e14bb325SJeff Bonwick 			mutex_exit(&spa->spa_props_lock);
297fa9e4066Sahrens 		}
298fa9e4066Sahrens 	}
299fa9e4066Sahrens 	*generation = spa_config_generation;
300e14bb325SJeff Bonwick 	mutex_exit(&spa_namespace_lock);
301fa9e4066Sahrens 
302fa9e4066Sahrens 	return (pools);
303fa9e4066Sahrens }
304fa9e4066Sahrens 
305fa9e4066Sahrens void
306fa9e4066Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
307fa9e4066Sahrens {
308e14bb325SJeff Bonwick 	mutex_enter(&spa->spa_props_lock);
309fa9e4066Sahrens 	if (spa->spa_config != NULL)
310fa9e4066Sahrens 		nvlist_free(spa->spa_config);
311fa9e4066Sahrens 	spa->spa_config = config;
312e14bb325SJeff Bonwick 	mutex_exit(&spa->spa_props_lock);
313fa9e4066Sahrens }
314fa9e4066Sahrens 
315fa9e4066Sahrens /*
316fa9e4066Sahrens  * Generate the pool's configuration based on the current in-core state.
317fa9e4066Sahrens  * We infer whether to generate a complete config or just one top-level config
318fa9e4066Sahrens  * based on whether vd is the root vdev.
319fa9e4066Sahrens  */
320fa9e4066Sahrens nvlist_t *
321fa9e4066Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
322fa9e4066Sahrens {
323fa9e4066Sahrens 	nvlist_t *config, *nvroot;
324fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
32595173954Sek 	unsigned long hostid = 0;
326e14bb325SJeff Bonwick 	boolean_t locked = B_FALSE;
327fa9e4066Sahrens 
328e14bb325SJeff Bonwick 	if (vd == NULL) {
329fa9e4066Sahrens 		vd = rvd;
330e14bb325SJeff Bonwick 		locked = B_TRUE;
331e14bb325SJeff Bonwick 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
332e14bb325SJeff Bonwick 	}
333e14bb325SJeff Bonwick 
334e14bb325SJeff Bonwick 	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
335e14bb325SJeff Bonwick 	    (SCL_CONFIG | SCL_STATE));
336fa9e4066Sahrens 
337fa9e4066Sahrens 	/*
338fa9e4066Sahrens 	 * If txg is -1, report the current value of spa->spa_config_txg.
339fa9e4066Sahrens 	 */
340fa9e4066Sahrens 	if (txg == -1ULL)
341fa9e4066Sahrens 		txg = spa->spa_config_txg;
342fa9e4066Sahrens 
343ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
344fa9e4066Sahrens 
345fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
34699653d4eSeschrock 	    spa_version(spa)) == 0);
347fa9e4066Sahrens 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
348fa9e4066Sahrens 	    spa_name(spa)) == 0);
349fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
350fa9e4066Sahrens 	    spa_state(spa)) == 0);
351fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
352fa9e4066Sahrens 	    txg) == 0);
353fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
354fa9e4066Sahrens 	    spa_guid(spa)) == 0);
35595173954Sek 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
35617194a52Slling 	if (hostid != 0) {
35717194a52Slling 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
3588654d025Sperrin 		    hostid) == 0);
35917194a52Slling 	}
36095173954Sek 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
36195173954Sek 	    utsname.nodename) == 0);
362fa9e4066Sahrens 
363fa9e4066Sahrens 	if (vd != rvd) {
364fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
365fa9e4066Sahrens 		    vd->vdev_top->vdev_guid) == 0);
366fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
367fa9e4066Sahrens 		    vd->vdev_guid) == 0);
36899653d4eSeschrock 		if (vd->vdev_isspare)
36999653d4eSeschrock 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
37099653d4eSeschrock 			    1ULL) == 0);
3718654d025Sperrin 		if (vd->vdev_islog)
3728654d025Sperrin 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
3738654d025Sperrin 			    1ULL) == 0);
374fa9e4066Sahrens 		vd = vd->vdev_top;		/* label contains top config */
375fa9e4066Sahrens 	}
376fa9e4066Sahrens 
377fa94a07fSbrendan 	nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE, B_FALSE);
378fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
379fa9e4066Sahrens 	nvlist_free(nvroot);
380fa9e4066Sahrens 
381e14bb325SJeff Bonwick 	if (locked)
382e14bb325SJeff Bonwick 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
383e14bb325SJeff Bonwick 
384fa9e4066Sahrens 	return (config);
385fa9e4066Sahrens }
3860373e76bSbonwick 
3870373e76bSbonwick /*
388e7cbe64fSgw  * For a pool that's not currently a booting rootpool, update all disk labels,
389e7cbe64fSgw  * generate a fresh config based on the current in-core state, and sync the
390e7cbe64fSgw  * global config cache.
3910373e76bSbonwick  */
3920373e76bSbonwick void
3930373e76bSbonwick spa_config_update(spa_t *spa, int what)
394e7cbe64fSgw {
395e7cbe64fSgw 	spa_config_update_common(spa, what, FALSE);
396e7cbe64fSgw }
397e7cbe64fSgw 
398e7cbe64fSgw /*
399e7cbe64fSgw  * Update all disk labels, generate a fresh config based on the current
400e7cbe64fSgw  * in-core state, and sync the global config cache (do not sync the config
401e7cbe64fSgw  * cache if this is a booting rootpool).
402e7cbe64fSgw  */
403e7cbe64fSgw void
404e7cbe64fSgw spa_config_update_common(spa_t *spa, int what, boolean_t isroot)
4050373e76bSbonwick {
4060373e76bSbonwick 	vdev_t *rvd = spa->spa_root_vdev;
4070373e76bSbonwick 	uint64_t txg;
4080373e76bSbonwick 	int c;
4090373e76bSbonwick 
4100373e76bSbonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
4110373e76bSbonwick 
412e14bb325SJeff Bonwick 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4130373e76bSbonwick 	txg = spa_last_synced_txg(spa) + 1;
4140373e76bSbonwick 	if (what == SPA_CONFIG_UPDATE_POOL) {
4150373e76bSbonwick 		vdev_config_dirty(rvd);
4160373e76bSbonwick 	} else {
4170373e76bSbonwick 		/*
4180373e76bSbonwick 		 * If we have top-level vdevs that were added but have
4190373e76bSbonwick 		 * not yet been prepared for allocation, do that now.
4200373e76bSbonwick 		 * (It's safe now because the config cache is up to date,
4210373e76bSbonwick 		 * so it will be able to translate the new DVAs.)
4220373e76bSbonwick 		 * See comments in spa_vdev_add() for full details.
4230373e76bSbonwick 		 */
4240373e76bSbonwick 		for (c = 0; c < rvd->vdev_children; c++) {
4250373e76bSbonwick 			vdev_t *tvd = rvd->vdev_child[c];
4260373e76bSbonwick 			if (tvd->vdev_ms_array == 0) {
4270373e76bSbonwick 				vdev_init(tvd, txg);
4280373e76bSbonwick 				vdev_config_dirty(tvd);
4290373e76bSbonwick 			}
4300373e76bSbonwick 		}
4310373e76bSbonwick 	}
432e14bb325SJeff Bonwick 	spa_config_exit(spa, SCL_ALL, FTAG);
4330373e76bSbonwick 
4340373e76bSbonwick 	/*
4350373e76bSbonwick 	 * Wait for the mosconfig to be regenerated and synced.
4360373e76bSbonwick 	 */
4370373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, txg);
4380373e76bSbonwick 
4390373e76bSbonwick 	/*
4400373e76bSbonwick 	 * Update the global config cache to reflect the new mosconfig.
4410373e76bSbonwick 	 */
442e7cbe64fSgw 	if (!isroot)
443c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL);
4440373e76bSbonwick 
4450373e76bSbonwick 	if (what == SPA_CONFIG_UPDATE_POOL)
446e7cbe64fSgw 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_VDEVS, isroot);
4470373e76bSbonwick }
448