xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision c5904d138f3bdf0762dbf452a43d5a5c387ea6a8)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/spa.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>
3695173954Sek #include <sys/utsname.h>
3795173954Sek #include <sys/systeminfo.h>
3895173954Sek #include <sys/sunddi.h>
39ea8dc4b6Seschrock #ifdef _KERNEL
40ea8dc4b6Seschrock #include <sys/kobj.h>
41ea8dc4b6Seschrock #endif
42ea8dc4b6Seschrock 
43fa9e4066Sahrens /*
44fa9e4066Sahrens  * Pool configuration repository.
45fa9e4066Sahrens  *
462f8aaab3Seschrock  * Pool configuration is stored as a packed nvlist on the filesystem.  By
472f8aaab3Seschrock  * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
482f8aaab3Seschrock  * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
492f8aaab3Seschrock  * property set that allows them to be stored in an alternate location until
502f8aaab3Seschrock  * the control of external software.
51fa9e4066Sahrens  *
522f8aaab3Seschrock  * For each cache file, we have a single nvlist which holds all the
532f8aaab3Seschrock  * configuration information.  When the module loads, we read this information
542f8aaab3Seschrock  * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
552f8aaab3Seschrock  * maintained independently in spa.c.  Whenever the namespace is modified, or
562f8aaab3Seschrock  * the configuration of a pool is changed, we call spa_config_sync(), which
572f8aaab3Seschrock  * walks through all the active pools and writes the configuration to disk.
58fa9e4066Sahrens  */
59fa9e4066Sahrens 
60fa9e4066Sahrens static uint64_t spa_config_generation = 1;
61fa9e4066Sahrens 
62fa9e4066Sahrens /*
63fa9e4066Sahrens  * This can be overridden in userland to preserve an alternate namespace for
64fa9e4066Sahrens  * userland pools when doing testing.
65fa9e4066Sahrens  */
66*c5904d13Seschrock const char *spa_config_path = ZPOOL_CACHE;
67fa9e4066Sahrens 
68fa9e4066Sahrens /*
69fa9e4066Sahrens  * Called when the module is first loaded, this routine loads the configuration
70fa9e4066Sahrens  * file into the SPA namespace.  It does not actually open or load the pools; it
71fa9e4066Sahrens  * only populates the namespace.
72fa9e4066Sahrens  */
73fa9e4066Sahrens void
74fa9e4066Sahrens spa_config_load(void)
75fa9e4066Sahrens {
76fa9e4066Sahrens 	void *buf = NULL;
77fa9e4066Sahrens 	nvlist_t *nvlist, *child;
78fa9e4066Sahrens 	nvpair_t *nvpair;
79fa9e4066Sahrens 	spa_t *spa;
80fa9e4066Sahrens 	char pathname[128];
81ea8dc4b6Seschrock 	struct _buf *file;
82b1b8ab34Slling 	uint64_t fsize;
83fa9e4066Sahrens 
84fa9e4066Sahrens 	/*
85fa9e4066Sahrens 	 * Open the configuration file.
86fa9e4066Sahrens 	 */
87*c5904d13Seschrock 	(void) snprintf(pathname, sizeof (pathname), "%s%s",
88*c5904d13Seschrock 	    (rootdir != NULL) ? "./" : "", spa_config_path);
89ea8dc4b6Seschrock 
90ea8dc4b6Seschrock 	file = kobj_open_file(pathname);
91ea8dc4b6Seschrock 	if (file == (struct _buf *)-1)
92fa9e4066Sahrens 		return;
93fa9e4066Sahrens 
94b1b8ab34Slling 	if (kobj_get_filesize(file, &fsize) != 0)
95fa9e4066Sahrens 		goto out;
96fa9e4066Sahrens 
97b1b8ab34Slling 	buf = kmem_alloc(fsize, KM_SLEEP);
98fa9e4066Sahrens 
99ea8dc4b6Seschrock 	/*
100ea8dc4b6Seschrock 	 * Read the nvlist from the file.
101ea8dc4b6Seschrock 	 */
102b1b8ab34Slling 	if (kobj_read_file(file, buf, fsize, 0) < 0)
103fa9e4066Sahrens 		goto out;
104fa9e4066Sahrens 
105fa9e4066Sahrens 	/*
106fa9e4066Sahrens 	 * Unpack the nvlist.
107fa9e4066Sahrens 	 */
108b1b8ab34Slling 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
109fa9e4066Sahrens 		goto out;
110fa9e4066Sahrens 
111fa9e4066Sahrens 	/*
112fa9e4066Sahrens 	 * Iterate over all elements in the nvlist, creating a new spa_t for
113fa9e4066Sahrens 	 * each one with the specified configuration.
114fa9e4066Sahrens 	 */
115fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
116fa9e4066Sahrens 	nvpair = NULL;
117fa9e4066Sahrens 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
118fa9e4066Sahrens 
119fa9e4066Sahrens 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
120fa9e4066Sahrens 			continue;
121fa9e4066Sahrens 
122fa9e4066Sahrens 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
123fa9e4066Sahrens 
124fa9e4066Sahrens 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
125fa9e4066Sahrens 			continue;
1260373e76bSbonwick 		spa = spa_add(nvpair_name(nvpair), NULL);
127fa9e4066Sahrens 
128fa9e4066Sahrens 		/*
129fa9e4066Sahrens 		 * We blindly duplicate the configuration here.  If it's
130fa9e4066Sahrens 		 * invalid, we will catch it when the pool is first opened.
131fa9e4066Sahrens 		 */
132fa9e4066Sahrens 		VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0);
133fa9e4066Sahrens 	}
134fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
135fa9e4066Sahrens 
136fa9e4066Sahrens 	nvlist_free(nvlist);
137fa9e4066Sahrens 
138fa9e4066Sahrens out:
139fa9e4066Sahrens 	if (buf != NULL)
140b1b8ab34Slling 		kmem_free(buf, fsize);
141fa9e4066Sahrens 
142ea8dc4b6Seschrock 	kobj_close_file(file);
143fa9e4066Sahrens }
144fa9e4066Sahrens 
1452f8aaab3Seschrock static void
146*c5904d13Seschrock spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
1472f8aaab3Seschrock {
1482f8aaab3Seschrock 	size_t buflen;
1492f8aaab3Seschrock 	char *buf;
1502f8aaab3Seschrock 	vnode_t *vp;
1512f8aaab3Seschrock 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
152*c5904d13Seschrock 	char tempname[128];
153*c5904d13Seschrock 
154*c5904d13Seschrock 	/*
155*c5904d13Seschrock 	 * If the nvlist is empty (NULL), then remove the old cachefile.
156*c5904d13Seschrock 	 */
157*c5904d13Seschrock 	if (nvl == NULL) {
158*c5904d13Seschrock 		(void) vn_remove(dp->scd_path, UIO_SYSSPACE, RMFILE);
159*c5904d13Seschrock 		return;
160*c5904d13Seschrock 	}
1612f8aaab3Seschrock 
162fa9e4066Sahrens 	/*
163fa9e4066Sahrens 	 * Pack the configuration into a buffer.
164fa9e4066Sahrens 	 */
165*c5904d13Seschrock 	VERIFY(nvlist_size(nvl, &buflen, NV_ENCODE_XDR) == 0);
166fa9e4066Sahrens 
167fa9e4066Sahrens 	buf = kmem_alloc(buflen, KM_SLEEP);
168fa9e4066Sahrens 
169*c5904d13Seschrock 	VERIFY(nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_XDR,
170ea8dc4b6Seschrock 	    KM_SLEEP) == 0);
171fa9e4066Sahrens 
172fa9e4066Sahrens 	/*
173fa9e4066Sahrens 	 * Write the configuration to disk.  We need to do the traditional
174fa9e4066Sahrens 	 * 'write to temporary file, sync, move over original' to make sure we
175fa9e4066Sahrens 	 * always have a consistent view of the data.
176fa9e4066Sahrens 	 */
177*c5904d13Seschrock 	(void) snprintf(tempname, sizeof (tempname), "%s.tmp", dp->scd_path);
178fa9e4066Sahrens 
179*c5904d13Seschrock 	if (vn_open(tempname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0)
180fa9e4066Sahrens 		goto out;
181fa9e4066Sahrens 
182fa9e4066Sahrens 	if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
183fa9e4066Sahrens 	    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
184da6c28aaSamw 	    VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) {
185*c5904d13Seschrock 		(void) vn_rename(tempname, dp->scd_path, UIO_SYSSPACE);
186fa9e4066Sahrens 	}
187fa9e4066Sahrens 
188da6c28aaSamw 	(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
189fa9e4066Sahrens 	VN_RELE(vp);
190fa9e4066Sahrens 
191fa9e4066Sahrens out:
192*c5904d13Seschrock 	(void) vn_remove(tempname, UIO_SYSSPACE, RMFILE);
193fa9e4066Sahrens 	kmem_free(buf, buflen);
1942f8aaab3Seschrock }
1952f8aaab3Seschrock 
1962f8aaab3Seschrock /*
197*c5904d13Seschrock  * Synchronize pool configuration to disk.  This must be called with the
198*c5904d13Seschrock  * namespace lock held.
1992f8aaab3Seschrock  */
2002f8aaab3Seschrock void
201*c5904d13Seschrock spa_config_sync(spa_t *target, boolean_t removing, boolean_t postsysevent)
2022f8aaab3Seschrock {
2032f8aaab3Seschrock 	spa_t *spa = NULL;
204*c5904d13Seschrock 	spa_config_dirent_t *dp, *tdp;
205*c5904d13Seschrock 	nvlist_t *nvl;
2062f8aaab3Seschrock 
2072f8aaab3Seschrock 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
2082f8aaab3Seschrock 
209*c5904d13Seschrock 	/*
210*c5904d13Seschrock 	 * Iterate over all cachefiles for the pool, past or present.  When the
211*c5904d13Seschrock 	 * cachefile is changed, the new one is pushed onto this list, allowing
212*c5904d13Seschrock 	 * us to update previous cachefiles that no longer contain this pool.
213*c5904d13Seschrock 	 */
214*c5904d13Seschrock 	for (dp = list_head(&target->spa_config_list); dp != NULL;
215*c5904d13Seschrock 	    dp = list_next(&target->spa_config_list, dp)) {
216*c5904d13Seschrock 		spa = NULL;
217*c5904d13Seschrock 		if (dp->scd_path == NULL)
218*c5904d13Seschrock 			continue;
219*c5904d13Seschrock 
220*c5904d13Seschrock 		/*
221*c5904d13Seschrock 		 * Iterate over all pools, adding any matching pools to 'nvl'.
222*c5904d13Seschrock 		 */
223*c5904d13Seschrock 		nvl = NULL;
224*c5904d13Seschrock 		while ((spa = spa_next(spa)) != NULL) {
225*c5904d13Seschrock 			if (spa->spa_config == NULL || spa->spa_name == NULL)
226*c5904d13Seschrock 				continue;
227*c5904d13Seschrock 
228*c5904d13Seschrock 			if (spa == target && removing)
229*c5904d13Seschrock 				continue;
230*c5904d13Seschrock 
231*c5904d13Seschrock 			tdp = list_head(&spa->spa_config_list);
232*c5904d13Seschrock 			ASSERT(tdp != NULL);
233*c5904d13Seschrock 			if (tdp->scd_path == NULL ||
234*c5904d13Seschrock 			    strcmp(tdp->scd_path, dp->scd_path) != 0)
235*c5904d13Seschrock 				continue;
236*c5904d13Seschrock 
237*c5904d13Seschrock 			if (nvl == NULL)
238*c5904d13Seschrock 				VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
239*c5904d13Seschrock 				    KM_SLEEP) == 0);
240*c5904d13Seschrock 
241*c5904d13Seschrock 			VERIFY(nvlist_add_nvlist(nvl, spa->spa_name,
242*c5904d13Seschrock 			    spa->spa_config) == 0);
243*c5904d13Seschrock 		}
244*c5904d13Seschrock 
245*c5904d13Seschrock 		spa_config_write(dp, nvl);
246*c5904d13Seschrock 		nvlist_free(nvl);
247*c5904d13Seschrock 	}
2482f8aaab3Seschrock 
2492f8aaab3Seschrock 	/*
250*c5904d13Seschrock 	 * Remove any config entries older than the current one.
2512f8aaab3Seschrock 	 */
252*c5904d13Seschrock 	dp = list_head(&target->spa_config_list);
253*c5904d13Seschrock 	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
254*c5904d13Seschrock 		list_remove(&target->spa_config_list, tdp);
255*c5904d13Seschrock 		if (tdp->scd_path != NULL)
256*c5904d13Seschrock 			spa_strfree(tdp->scd_path);
257*c5904d13Seschrock 		kmem_free(tdp, sizeof (spa_config_dirent_t));
2582f8aaab3Seschrock 	}
2592f8aaab3Seschrock 
2602f8aaab3Seschrock 	spa_config_generation++;
261*c5904d13Seschrock 
262*c5904d13Seschrock 	if (postsysevent)
263*c5904d13Seschrock 		spa_event_notify(target, NULL, ESC_ZFS_CONFIG_SYNC);
264fa9e4066Sahrens }
265fa9e4066Sahrens 
266fa9e4066Sahrens /*
2670373e76bSbonwick  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
268fa9e4066Sahrens  * and we don't want to allow the local zone to see all the pools anyway.
269fa9e4066Sahrens  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
270fa9e4066Sahrens  * information for all pool visible within the zone.
271fa9e4066Sahrens  */
272fa9e4066Sahrens nvlist_t *
273fa9e4066Sahrens spa_all_configs(uint64_t *generation)
274fa9e4066Sahrens {
275fa9e4066Sahrens 	nvlist_t *pools;
276fa9e4066Sahrens 	spa_t *spa;
277fa9e4066Sahrens 
278fa9e4066Sahrens 	if (*generation == spa_config_generation)
279fa9e4066Sahrens 		return (NULL);
280fa9e4066Sahrens 
281ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
282fa9e4066Sahrens 
283fa9e4066Sahrens 	spa = NULL;
284fa9e4066Sahrens 	mutex_enter(&spa_namespace_lock);
285fa9e4066Sahrens 	while ((spa = spa_next(spa)) != NULL) {
286fa9e4066Sahrens 		if (INGLOBALZONE(curproc) ||
287fa9e4066Sahrens 		    zone_dataset_visible(spa_name(spa), NULL)) {
288fa9e4066Sahrens 			mutex_enter(&spa->spa_config_cache_lock);
289fa9e4066Sahrens 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
290fa9e4066Sahrens 			    spa->spa_config) == 0);
291fa9e4066Sahrens 			mutex_exit(&spa->spa_config_cache_lock);
292fa9e4066Sahrens 		}
293fa9e4066Sahrens 	}
294fa9e4066Sahrens 	mutex_exit(&spa_namespace_lock);
295fa9e4066Sahrens 
296fa9e4066Sahrens 	*generation = spa_config_generation;
297fa9e4066Sahrens 
298fa9e4066Sahrens 	return (pools);
299fa9e4066Sahrens }
300fa9e4066Sahrens 
301fa9e4066Sahrens void
302fa9e4066Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
303fa9e4066Sahrens {
304fa9e4066Sahrens 	mutex_enter(&spa->spa_config_cache_lock);
305fa9e4066Sahrens 	if (spa->spa_config != NULL)
306fa9e4066Sahrens 		nvlist_free(spa->spa_config);
307fa9e4066Sahrens 	spa->spa_config = config;
308fa9e4066Sahrens 	mutex_exit(&spa->spa_config_cache_lock);
309fa9e4066Sahrens }
310fa9e4066Sahrens 
311fa9e4066Sahrens /*
312fa9e4066Sahrens  * Generate the pool's configuration based on the current in-core state.
313fa9e4066Sahrens  * We infer whether to generate a complete config or just one top-level config
314fa9e4066Sahrens  * based on whether vd is the root vdev.
315fa9e4066Sahrens  */
316fa9e4066Sahrens nvlist_t *
317fa9e4066Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
318fa9e4066Sahrens {
319fa9e4066Sahrens 	nvlist_t *config, *nvroot;
320fa9e4066Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
32195173954Sek 	unsigned long hostid = 0;
322fa9e4066Sahrens 
32391ebeef5Sahrens 	ASSERT(spa_config_held(spa, RW_READER) ||
32491ebeef5Sahrens 	    spa_config_held(spa, RW_WRITER));
3250373e76bSbonwick 
326fa9e4066Sahrens 	if (vd == NULL)
327fa9e4066Sahrens 		vd = rvd;
328fa9e4066Sahrens 
329fa9e4066Sahrens 	/*
330fa9e4066Sahrens 	 * If txg is -1, report the current value of spa->spa_config_txg.
331fa9e4066Sahrens 	 */
332fa9e4066Sahrens 	if (txg == -1ULL)
333fa9e4066Sahrens 		txg = spa->spa_config_txg;
334fa9e4066Sahrens 
335ea8dc4b6Seschrock 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
336fa9e4066Sahrens 
337fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
33899653d4eSeschrock 	    spa_version(spa)) == 0);
339fa9e4066Sahrens 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
340fa9e4066Sahrens 	    spa_name(spa)) == 0);
341fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
342fa9e4066Sahrens 	    spa_state(spa)) == 0);
343fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
344fa9e4066Sahrens 	    txg) == 0);
345fa9e4066Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
346fa9e4066Sahrens 	    spa_guid(spa)) == 0);
34795173954Sek 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
34817194a52Slling 	if (hostid != 0) {
34917194a52Slling 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
3508654d025Sperrin 		    hostid) == 0);
35117194a52Slling 	}
35295173954Sek 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
35395173954Sek 	    utsname.nodename) == 0);
354fa9e4066Sahrens 
355fa9e4066Sahrens 	if (vd != rvd) {
356fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
357fa9e4066Sahrens 		    vd->vdev_top->vdev_guid) == 0);
358fa9e4066Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
359fa9e4066Sahrens 		    vd->vdev_guid) == 0);
36099653d4eSeschrock 		if (vd->vdev_isspare)
36199653d4eSeschrock 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
36299653d4eSeschrock 			    1ULL) == 0);
3638654d025Sperrin 		if (vd->vdev_islog)
3648654d025Sperrin 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
3658654d025Sperrin 			    1ULL) == 0);
366fa9e4066Sahrens 		vd = vd->vdev_top;		/* label contains top config */
367fa9e4066Sahrens 	}
368fa9e4066Sahrens 
369fa94a07fSbrendan 	nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE, B_FALSE);
370fa9e4066Sahrens 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
371fa9e4066Sahrens 	nvlist_free(nvroot);
372fa9e4066Sahrens 
373fa9e4066Sahrens 	return (config);
374fa9e4066Sahrens }
3750373e76bSbonwick 
3760373e76bSbonwick /*
377e7cbe64fSgw  * For a pool that's not currently a booting rootpool, update all disk labels,
378e7cbe64fSgw  * generate a fresh config based on the current in-core state, and sync the
379e7cbe64fSgw  * global config cache.
3800373e76bSbonwick  */
3810373e76bSbonwick void
3820373e76bSbonwick spa_config_update(spa_t *spa, int what)
383e7cbe64fSgw {
384e7cbe64fSgw 	spa_config_update_common(spa, what, FALSE);
385e7cbe64fSgw }
386e7cbe64fSgw 
387e7cbe64fSgw /*
388e7cbe64fSgw  * Update all disk labels, generate a fresh config based on the current
389e7cbe64fSgw  * in-core state, and sync the global config cache (do not sync the config
390e7cbe64fSgw  * cache if this is a booting rootpool).
391e7cbe64fSgw  */
392e7cbe64fSgw void
393e7cbe64fSgw spa_config_update_common(spa_t *spa, int what, boolean_t isroot)
3940373e76bSbonwick {
3950373e76bSbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3960373e76bSbonwick 	uint64_t txg;
3970373e76bSbonwick 	int c;
3980373e76bSbonwick 
3990373e76bSbonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
4000373e76bSbonwick 
4010373e76bSbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
4020373e76bSbonwick 	txg = spa_last_synced_txg(spa) + 1;
4030373e76bSbonwick 	if (what == SPA_CONFIG_UPDATE_POOL) {
4040373e76bSbonwick 		vdev_config_dirty(rvd);
4050373e76bSbonwick 	} else {
4060373e76bSbonwick 		/*
4070373e76bSbonwick 		 * If we have top-level vdevs that were added but have
4080373e76bSbonwick 		 * not yet been prepared for allocation, do that now.
4090373e76bSbonwick 		 * (It's safe now because the config cache is up to date,
4100373e76bSbonwick 		 * so it will be able to translate the new DVAs.)
4110373e76bSbonwick 		 * See comments in spa_vdev_add() for full details.
4120373e76bSbonwick 		 */
4130373e76bSbonwick 		for (c = 0; c < rvd->vdev_children; c++) {
4140373e76bSbonwick 			vdev_t *tvd = rvd->vdev_child[c];
4150373e76bSbonwick 			if (tvd->vdev_ms_array == 0) {
4160373e76bSbonwick 				vdev_init(tvd, txg);
4170373e76bSbonwick 				vdev_config_dirty(tvd);
4180373e76bSbonwick 			}
4190373e76bSbonwick 		}
4200373e76bSbonwick 	}
4210373e76bSbonwick 	spa_config_exit(spa, FTAG);
4220373e76bSbonwick 
4230373e76bSbonwick 	/*
4240373e76bSbonwick 	 * Wait for the mosconfig to be regenerated and synced.
4250373e76bSbonwick 	 */
4260373e76bSbonwick 	txg_wait_synced(spa->spa_dsl_pool, txg);
4270373e76bSbonwick 
4280373e76bSbonwick 	/*
4290373e76bSbonwick 	 * Update the global config cache to reflect the new mosconfig.
4300373e76bSbonwick 	 */
431e7cbe64fSgw 	if (!isroot)
432*c5904d13Seschrock 		spa_config_sync(spa, B_FALSE, what != SPA_CONFIG_UPDATE_POOL);
4330373e76bSbonwick 
4340373e76bSbonwick 	if (what == SPA_CONFIG_UPDATE_POOL)
435e7cbe64fSgw 		spa_config_update_common(spa, SPA_CONFIG_UPDATE_VDEVS, isroot);
4360373e76bSbonwick }
437