xref: /freebsd/sys/contrib/openzfs/module/zfs/spa_config.c (revision 2c48331d28f16c0efce5a72a81e7d71668c4a158)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25*2c48331dSMatt Macy  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26eda14cbcSMatt Macy  * Copyright 2017 Joyent, Inc.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <sys/spa.h>
30eda14cbcSMatt Macy #include <sys/file.h>
31eda14cbcSMatt Macy #include <sys/fm/fs/zfs.h>
32eda14cbcSMatt Macy #include <sys/spa_impl.h>
33eda14cbcSMatt Macy #include <sys/nvpair.h>
34eda14cbcSMatt Macy #include <sys/uio.h>
35eda14cbcSMatt Macy #include <sys/fs/zfs.h>
36eda14cbcSMatt Macy #include <sys/vdev_impl.h>
37eda14cbcSMatt Macy #include <sys/zfs_ioctl.h>
38eda14cbcSMatt Macy #include <sys/systeminfo.h>
39eda14cbcSMatt Macy #include <sys/sunddi.h>
40eda14cbcSMatt Macy #include <sys/zfeature.h>
41eda14cbcSMatt Macy #include <sys/zfs_file.h>
42eda14cbcSMatt Macy #ifdef _KERNEL
43eda14cbcSMatt Macy #include <sys/zone.h>
44eda14cbcSMatt Macy #endif
45eda14cbcSMatt Macy 
46eda14cbcSMatt Macy /*
47eda14cbcSMatt Macy  * Pool configuration repository.
48eda14cbcSMatt Macy  *
49eda14cbcSMatt Macy  * Pool configuration is stored as a packed nvlist on the filesystem.  By
50eda14cbcSMatt Macy  * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
51eda14cbcSMatt Macy  * (when the ZFS module is loaded).  Pools can also have the 'cachefile'
52eda14cbcSMatt Macy  * property set that allows them to be stored in an alternate location until
53eda14cbcSMatt Macy  * the control of external software.
54eda14cbcSMatt Macy  *
55eda14cbcSMatt Macy  * For each cache file, we have a single nvlist which holds all the
56eda14cbcSMatt Macy  * configuration information.  When the module loads, we read this information
57eda14cbcSMatt Macy  * from /etc/zfs/zpool.cache and populate the SPA namespace.  This namespace is
58eda14cbcSMatt Macy  * maintained independently in spa.c.  Whenever the namespace is modified, or
59eda14cbcSMatt Macy  * the configuration of a pool is changed, we call spa_write_cachefile(), which
60eda14cbcSMatt Macy  * walks through all the active pools and writes the configuration to disk.
61eda14cbcSMatt Macy  */
62eda14cbcSMatt Macy 
63eda14cbcSMatt Macy static uint64_t spa_config_generation = 1;
64eda14cbcSMatt Macy 
65eda14cbcSMatt Macy /*
66eda14cbcSMatt Macy  * This can be overridden in userland to preserve an alternate namespace for
67eda14cbcSMatt Macy  * userland pools when doing testing.
68eda14cbcSMatt Macy  */
69eda14cbcSMatt Macy char *spa_config_path = ZPOOL_CACHE;
70eda14cbcSMatt Macy int zfs_autoimport_disable = 1;
71eda14cbcSMatt Macy 
72eda14cbcSMatt Macy /*
73eda14cbcSMatt Macy  * Called when the module is first loaded, this routine loads the configuration
74eda14cbcSMatt Macy  * file into the SPA namespace.  It does not actually open or load the pools; it
75eda14cbcSMatt Macy  * only populates the namespace.
76eda14cbcSMatt Macy  */
77eda14cbcSMatt Macy void
78eda14cbcSMatt Macy spa_config_load(void)
79eda14cbcSMatt Macy {
80eda14cbcSMatt Macy 	void *buf = NULL;
81eda14cbcSMatt Macy 	nvlist_t *nvlist, *child;
82eda14cbcSMatt Macy 	nvpair_t *nvpair;
83eda14cbcSMatt Macy 	char *pathname;
84eda14cbcSMatt Macy 	zfs_file_t *fp;
85eda14cbcSMatt Macy 	zfs_file_attr_t zfa;
86eda14cbcSMatt Macy 	uint64_t fsize;
87eda14cbcSMatt Macy 	int err;
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy #ifdef _KERNEL
90eda14cbcSMatt Macy 	if (zfs_autoimport_disable)
91eda14cbcSMatt Macy 		return;
92eda14cbcSMatt Macy #endif
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy 	/*
95eda14cbcSMatt Macy 	 * Open the configuration file.
96eda14cbcSMatt Macy 	 */
97eda14cbcSMatt Macy 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy 	(void) snprintf(pathname, MAXPATHLEN, "%s", spa_config_path);
100eda14cbcSMatt Macy 
101eda14cbcSMatt Macy 	err = zfs_file_open(pathname, O_RDONLY, 0, &fp);
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy #ifdef __FreeBSD__
104eda14cbcSMatt Macy 	if (err)
105eda14cbcSMatt Macy 		err = zfs_file_open(ZPOOL_CACHE_BOOT, O_RDONLY, 0, &fp);
106eda14cbcSMatt Macy #endif
107eda14cbcSMatt Macy 	kmem_free(pathname, MAXPATHLEN);
108eda14cbcSMatt Macy 
109eda14cbcSMatt Macy 	if (err)
110eda14cbcSMatt Macy 		return;
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 	if (zfs_file_getattr(fp, &zfa))
113eda14cbcSMatt Macy 		goto out;
114eda14cbcSMatt Macy 
115eda14cbcSMatt Macy 	fsize = zfa.zfa_size;
116eda14cbcSMatt Macy 	buf = kmem_alloc(fsize, KM_SLEEP);
117eda14cbcSMatt Macy 
118eda14cbcSMatt Macy 	/*
119eda14cbcSMatt Macy 	 * Read the nvlist from the file.
120eda14cbcSMatt Macy 	 */
121eda14cbcSMatt Macy 	if (zfs_file_read(fp, buf, fsize, NULL) < 0)
122eda14cbcSMatt Macy 		goto out;
123eda14cbcSMatt Macy 
124eda14cbcSMatt Macy 	/*
125eda14cbcSMatt Macy 	 * Unpack the nvlist.
126eda14cbcSMatt Macy 	 */
127eda14cbcSMatt Macy 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
128eda14cbcSMatt Macy 		goto out;
129eda14cbcSMatt Macy 
130eda14cbcSMatt Macy 	/*
131eda14cbcSMatt Macy 	 * Iterate over all elements in the nvlist, creating a new spa_t for
132eda14cbcSMatt Macy 	 * each one with the specified configuration.
133eda14cbcSMatt Macy 	 */
134eda14cbcSMatt Macy 	mutex_enter(&spa_namespace_lock);
135eda14cbcSMatt Macy 	nvpair = NULL;
136eda14cbcSMatt Macy 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
137eda14cbcSMatt Macy 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
138eda14cbcSMatt Macy 			continue;
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy 		child = fnvpair_value_nvlist(nvpair);
141eda14cbcSMatt Macy 
142eda14cbcSMatt Macy 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
143eda14cbcSMatt Macy 			continue;
144eda14cbcSMatt Macy 		(void) spa_add(nvpair_name(nvpair), child, NULL);
145eda14cbcSMatt Macy 	}
146eda14cbcSMatt Macy 	mutex_exit(&spa_namespace_lock);
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy 	nvlist_free(nvlist);
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy out:
151eda14cbcSMatt Macy 	if (buf != NULL)
152eda14cbcSMatt Macy 		kmem_free(buf, fsize);
153eda14cbcSMatt Macy 
154eda14cbcSMatt Macy 	zfs_file_close(fp);
155eda14cbcSMatt Macy }
156eda14cbcSMatt Macy 
157eda14cbcSMatt Macy static int
158eda14cbcSMatt Macy spa_config_remove(spa_config_dirent_t *dp)
159eda14cbcSMatt Macy {
160eda14cbcSMatt Macy 	int error = 0;
161eda14cbcSMatt Macy 
162eda14cbcSMatt Macy 	/*
163eda14cbcSMatt Macy 	 * Remove the cache file.  If zfs_file_unlink() in not supported by the
164eda14cbcSMatt Macy 	 * platform fallback to truncating the file which is functionally
165eda14cbcSMatt Macy 	 * equivalent.
166eda14cbcSMatt Macy 	 */
167eda14cbcSMatt Macy 	error = zfs_file_unlink(dp->scd_path);
168eda14cbcSMatt Macy 	if (error == EOPNOTSUPP) {
169eda14cbcSMatt Macy 		int flags = O_RDWR | O_TRUNC;
170eda14cbcSMatt Macy 		zfs_file_t *fp;
171eda14cbcSMatt Macy 
172eda14cbcSMatt Macy 		error = zfs_file_open(dp->scd_path, flags, 0644, &fp);
173eda14cbcSMatt Macy 		if (error == 0) {
174eda14cbcSMatt Macy 			(void) zfs_file_fsync(fp, O_SYNC);
175eda14cbcSMatt Macy 			(void) zfs_file_close(fp);
176eda14cbcSMatt Macy 		}
177eda14cbcSMatt Macy 	}
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy 	return (error);
180eda14cbcSMatt Macy }
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy static int
183eda14cbcSMatt Macy spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl)
184eda14cbcSMatt Macy {
185eda14cbcSMatt Macy 	size_t buflen;
186eda14cbcSMatt Macy 	char *buf;
187eda14cbcSMatt Macy 	int oflags = O_RDWR | O_TRUNC | O_CREAT | O_LARGEFILE;
188eda14cbcSMatt Macy 	char *temp;
189eda14cbcSMatt Macy 	int err;
190eda14cbcSMatt Macy 	zfs_file_t *fp;
191eda14cbcSMatt Macy 
192eda14cbcSMatt Macy 	/*
193eda14cbcSMatt Macy 	 * If the nvlist is empty (NULL), then remove the old cachefile.
194eda14cbcSMatt Macy 	 */
195eda14cbcSMatt Macy 	if (nvl == NULL) {
196eda14cbcSMatt Macy 		err = spa_config_remove(dp);
197eda14cbcSMatt Macy 		if (err == ENOENT)
198eda14cbcSMatt Macy 			err = 0;
199eda14cbcSMatt Macy 
200eda14cbcSMatt Macy 		return (err);
201eda14cbcSMatt Macy 	}
202eda14cbcSMatt Macy 
203eda14cbcSMatt Macy 	/*
204eda14cbcSMatt Macy 	 * Pack the configuration into a buffer.
205eda14cbcSMatt Macy 	 */
206eda14cbcSMatt Macy 	buf = fnvlist_pack(nvl, &buflen);
207eda14cbcSMatt Macy 	temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
208eda14cbcSMatt Macy 
209eda14cbcSMatt Macy 	/*
210eda14cbcSMatt Macy 	 * Write the configuration to disk.  Due to the complexity involved
211eda14cbcSMatt Macy 	 * in performing a rename and remove from within the kernel the file
212eda14cbcSMatt Macy 	 * is instead truncated and overwritten in place.  This way we always
213eda14cbcSMatt Macy 	 * have a consistent view of the data or a zero length file.
214eda14cbcSMatt Macy 	 */
215eda14cbcSMatt Macy 	err = zfs_file_open(dp->scd_path, oflags, 0644, &fp);
216eda14cbcSMatt Macy 	if (err == 0) {
217eda14cbcSMatt Macy 		err = zfs_file_write(fp, buf, buflen, NULL);
218eda14cbcSMatt Macy 		if (err == 0)
219eda14cbcSMatt Macy 			err = zfs_file_fsync(fp, O_SYNC);
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy 		zfs_file_close(fp);
222eda14cbcSMatt Macy 		if (err)
223eda14cbcSMatt Macy 			(void) spa_config_remove(dp);
224eda14cbcSMatt Macy 	}
225eda14cbcSMatt Macy 	fnvlist_pack_free(buf, buflen);
226eda14cbcSMatt Macy 	kmem_free(temp, MAXPATHLEN);
227eda14cbcSMatt Macy 	return (err);
228eda14cbcSMatt Macy }
229eda14cbcSMatt Macy 
230eda14cbcSMatt Macy /*
231eda14cbcSMatt Macy  * Synchronize pool configuration to disk.  This must be called with the
232eda14cbcSMatt Macy  * namespace lock held. Synchronizing the pool cache is typically done after
233eda14cbcSMatt Macy  * the configuration has been synced to the MOS. This exposes a window where
234eda14cbcSMatt Macy  * the MOS config will have been updated but the cache file has not. If
235eda14cbcSMatt Macy  * the system were to crash at that instant then the cached config may not
236eda14cbcSMatt Macy  * contain the correct information to open the pool and an explicit import
237eda14cbcSMatt Macy  * would be required.
238eda14cbcSMatt Macy  */
239eda14cbcSMatt Macy void
240eda14cbcSMatt Macy spa_write_cachefile(spa_t *target, boolean_t removing, boolean_t postsysevent)
241eda14cbcSMatt Macy {
242eda14cbcSMatt Macy 	spa_config_dirent_t *dp, *tdp;
243eda14cbcSMatt Macy 	nvlist_t *nvl;
244eda14cbcSMatt Macy 	char *pool_name;
245eda14cbcSMatt Macy 	boolean_t ccw_failure;
246eda14cbcSMatt Macy 	int error = 0;
247eda14cbcSMatt Macy 
248eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
249eda14cbcSMatt Macy 
250eda14cbcSMatt Macy 	if (!(spa_mode_global & SPA_MODE_WRITE))
251eda14cbcSMatt Macy 		return;
252eda14cbcSMatt Macy 
253eda14cbcSMatt Macy 	/*
254eda14cbcSMatt Macy 	 * Iterate over all cachefiles for the pool, past or present.  When the
255eda14cbcSMatt Macy 	 * cachefile is changed, the new one is pushed onto this list, allowing
256eda14cbcSMatt Macy 	 * us to update previous cachefiles that no longer contain this pool.
257eda14cbcSMatt Macy 	 */
258eda14cbcSMatt Macy 	ccw_failure = B_FALSE;
259eda14cbcSMatt Macy 	for (dp = list_head(&target->spa_config_list); dp != NULL;
260eda14cbcSMatt Macy 	    dp = list_next(&target->spa_config_list, dp)) {
261eda14cbcSMatt Macy 		spa_t *spa = NULL;
262eda14cbcSMatt Macy 		if (dp->scd_path == NULL)
263eda14cbcSMatt Macy 			continue;
264eda14cbcSMatt Macy 
265eda14cbcSMatt Macy 		/*
266eda14cbcSMatt Macy 		 * Iterate over all pools, adding any matching pools to 'nvl'.
267eda14cbcSMatt Macy 		 */
268eda14cbcSMatt Macy 		nvl = NULL;
269eda14cbcSMatt Macy 		while ((spa = spa_next(spa)) != NULL) {
270eda14cbcSMatt Macy 			/*
271eda14cbcSMatt Macy 			 * Skip over our own pool if we're about to remove
272eda14cbcSMatt Macy 			 * ourselves from the spa namespace or any pool that
273eda14cbcSMatt Macy 			 * is readonly. Since we cannot guarantee that a
274eda14cbcSMatt Macy 			 * readonly pool would successfully import upon reboot,
275eda14cbcSMatt Macy 			 * we don't allow them to be written to the cache file.
276eda14cbcSMatt Macy 			 */
277eda14cbcSMatt Macy 			if ((spa == target && removing) ||
278eda14cbcSMatt Macy 			    !spa_writeable(spa))
279eda14cbcSMatt Macy 				continue;
280eda14cbcSMatt Macy 
281eda14cbcSMatt Macy 			mutex_enter(&spa->spa_props_lock);
282eda14cbcSMatt Macy 			tdp = list_head(&spa->spa_config_list);
283eda14cbcSMatt Macy 			if (spa->spa_config == NULL ||
284eda14cbcSMatt Macy 			    tdp == NULL ||
285eda14cbcSMatt Macy 			    tdp->scd_path == NULL ||
286eda14cbcSMatt Macy 			    strcmp(tdp->scd_path, dp->scd_path) != 0) {
287eda14cbcSMatt Macy 				mutex_exit(&spa->spa_props_lock);
288eda14cbcSMatt Macy 				continue;
289eda14cbcSMatt Macy 			}
290eda14cbcSMatt Macy 
291eda14cbcSMatt Macy 			if (nvl == NULL)
292eda14cbcSMatt Macy 				nvl = fnvlist_alloc();
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 			if (spa->spa_import_flags & ZFS_IMPORT_TEMP_NAME)
295eda14cbcSMatt Macy 				pool_name = fnvlist_lookup_string(
296eda14cbcSMatt Macy 				    spa->spa_config, ZPOOL_CONFIG_POOL_NAME);
297eda14cbcSMatt Macy 			else
298eda14cbcSMatt Macy 				pool_name = spa_name(spa);
299eda14cbcSMatt Macy 
300eda14cbcSMatt Macy 			fnvlist_add_nvlist(nvl, pool_name, spa->spa_config);
301eda14cbcSMatt Macy 			mutex_exit(&spa->spa_props_lock);
302eda14cbcSMatt Macy 		}
303eda14cbcSMatt Macy 
304eda14cbcSMatt Macy 		error = spa_config_write(dp, nvl);
305eda14cbcSMatt Macy 		if (error != 0)
306eda14cbcSMatt Macy 			ccw_failure = B_TRUE;
307eda14cbcSMatt Macy 		nvlist_free(nvl);
308eda14cbcSMatt Macy 	}
309eda14cbcSMatt Macy 
310eda14cbcSMatt Macy 	if (ccw_failure) {
311eda14cbcSMatt Macy 		/*
312eda14cbcSMatt Macy 		 * Keep trying so that configuration data is
313eda14cbcSMatt Macy 		 * written if/when any temporary filesystem
314eda14cbcSMatt Macy 		 * resource issues are resolved.
315eda14cbcSMatt Macy 		 */
316eda14cbcSMatt Macy 		if (target->spa_ccw_fail_time == 0) {
317eac7052fSMatt Macy 			(void) zfs_ereport_post(
318eac7052fSMatt Macy 			    FM_EREPORT_ZFS_CONFIG_CACHE_WRITE,
319*2c48331dSMatt Macy 			    target, NULL, NULL, NULL, 0);
320eda14cbcSMatt Macy 		}
321eda14cbcSMatt Macy 		target->spa_ccw_fail_time = gethrtime();
322eda14cbcSMatt Macy 		spa_async_request(target, SPA_ASYNC_CONFIG_UPDATE);
323eda14cbcSMatt Macy 	} else {
324eda14cbcSMatt Macy 		/*
325eda14cbcSMatt Macy 		 * Do not rate limit future attempts to update
326eda14cbcSMatt Macy 		 * the config cache.
327eda14cbcSMatt Macy 		 */
328eda14cbcSMatt Macy 		target->spa_ccw_fail_time = 0;
329eda14cbcSMatt Macy 	}
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 	/*
332eda14cbcSMatt Macy 	 * Remove any config entries older than the current one.
333eda14cbcSMatt Macy 	 */
334eda14cbcSMatt Macy 	dp = list_head(&target->spa_config_list);
335eda14cbcSMatt Macy 	while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
336eda14cbcSMatt Macy 		list_remove(&target->spa_config_list, tdp);
337eda14cbcSMatt Macy 		if (tdp->scd_path != NULL)
338eda14cbcSMatt Macy 			spa_strfree(tdp->scd_path);
339eda14cbcSMatt Macy 		kmem_free(tdp, sizeof (spa_config_dirent_t));
340eda14cbcSMatt Macy 	}
341eda14cbcSMatt Macy 
342eda14cbcSMatt Macy 	spa_config_generation++;
343eda14cbcSMatt Macy 
344eda14cbcSMatt Macy 	if (postsysevent)
345eda14cbcSMatt Macy 		spa_event_notify(target, NULL, NULL, ESC_ZFS_CONFIG_SYNC);
346eda14cbcSMatt Macy }
347eda14cbcSMatt Macy 
348eda14cbcSMatt Macy /*
349eda14cbcSMatt Macy  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
350eda14cbcSMatt Macy  * and we don't want to allow the local zone to see all the pools anyway.
351eda14cbcSMatt Macy  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
352eda14cbcSMatt Macy  * information for all pool visible within the zone.
353eda14cbcSMatt Macy  */
354eda14cbcSMatt Macy nvlist_t *
355eda14cbcSMatt Macy spa_all_configs(uint64_t *generation)
356eda14cbcSMatt Macy {
357eda14cbcSMatt Macy 	nvlist_t *pools;
358eda14cbcSMatt Macy 	spa_t *spa = NULL;
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy 	if (*generation == spa_config_generation)
361eda14cbcSMatt Macy 		return (NULL);
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	pools = fnvlist_alloc();
364eda14cbcSMatt Macy 
365eda14cbcSMatt Macy 	mutex_enter(&spa_namespace_lock);
366eda14cbcSMatt Macy 	while ((spa = spa_next(spa)) != NULL) {
367eda14cbcSMatt Macy 		if (INGLOBALZONE(curproc) ||
368eda14cbcSMatt Macy 		    zone_dataset_visible(spa_name(spa), NULL)) {
369eda14cbcSMatt Macy 			mutex_enter(&spa->spa_props_lock);
370eda14cbcSMatt Macy 			fnvlist_add_nvlist(pools, spa_name(spa),
371eda14cbcSMatt Macy 			    spa->spa_config);
372eda14cbcSMatt Macy 			mutex_exit(&spa->spa_props_lock);
373eda14cbcSMatt Macy 		}
374eda14cbcSMatt Macy 	}
375eda14cbcSMatt Macy 	*generation = spa_config_generation;
376eda14cbcSMatt Macy 	mutex_exit(&spa_namespace_lock);
377eda14cbcSMatt Macy 
378eda14cbcSMatt Macy 	return (pools);
379eda14cbcSMatt Macy }
380eda14cbcSMatt Macy 
381eda14cbcSMatt Macy void
382eda14cbcSMatt Macy spa_config_set(spa_t *spa, nvlist_t *config)
383eda14cbcSMatt Macy {
384eda14cbcSMatt Macy 	mutex_enter(&spa->spa_props_lock);
385eda14cbcSMatt Macy 	if (spa->spa_config != NULL && spa->spa_config != config)
386eda14cbcSMatt Macy 		nvlist_free(spa->spa_config);
387eda14cbcSMatt Macy 	spa->spa_config = config;
388eda14cbcSMatt Macy 	mutex_exit(&spa->spa_props_lock);
389eda14cbcSMatt Macy }
390eda14cbcSMatt Macy 
391eda14cbcSMatt Macy /*
392eda14cbcSMatt Macy  * Generate the pool's configuration based on the current in-core state.
393eda14cbcSMatt Macy  *
394eda14cbcSMatt Macy  * We infer whether to generate a complete config or just one top-level config
395eda14cbcSMatt Macy  * based on whether vd is the root vdev.
396eda14cbcSMatt Macy  */
397eda14cbcSMatt Macy nvlist_t *
398eda14cbcSMatt Macy spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
399eda14cbcSMatt Macy {
400eda14cbcSMatt Macy 	nvlist_t *config, *nvroot;
401eda14cbcSMatt Macy 	vdev_t *rvd = spa->spa_root_vdev;
402eda14cbcSMatt Macy 	unsigned long hostid = 0;
403eda14cbcSMatt Macy 	boolean_t locked = B_FALSE;
404eda14cbcSMatt Macy 	uint64_t split_guid;
405eda14cbcSMatt Macy 	char *pool_name;
406eda14cbcSMatt Macy 
407eda14cbcSMatt Macy 	if (vd == NULL) {
408eda14cbcSMatt Macy 		vd = rvd;
409eda14cbcSMatt Macy 		locked = B_TRUE;
410eda14cbcSMatt Macy 		spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
411eda14cbcSMatt Macy 	}
412eda14cbcSMatt Macy 
413eda14cbcSMatt Macy 	ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
414eda14cbcSMatt Macy 	    (SCL_CONFIG | SCL_STATE));
415eda14cbcSMatt Macy 
416eda14cbcSMatt Macy 	/*
417eda14cbcSMatt Macy 	 * If txg is -1, report the current value of spa->spa_config_txg.
418eda14cbcSMatt Macy 	 */
419eda14cbcSMatt Macy 	if (txg == -1ULL)
420eda14cbcSMatt Macy 		txg = spa->spa_config_txg;
421eda14cbcSMatt Macy 
422eda14cbcSMatt Macy 	/*
423eda14cbcSMatt Macy 	 * Originally, users had to handle spa namespace collisions by either
424eda14cbcSMatt Macy 	 * exporting the already imported pool or by specifying a new name for
425eda14cbcSMatt Macy 	 * the pool with a conflicting name. In the case of root pools from
426eda14cbcSMatt Macy 	 * virtual guests, neither approach to collision resolution is
427eda14cbcSMatt Macy 	 * reasonable. This is addressed by extending the new name syntax with
428eda14cbcSMatt Macy 	 * an option to specify that the new name is temporary. When specified,
429eda14cbcSMatt Macy 	 * ZFS_IMPORT_TEMP_NAME will be set in spa->spa_import_flags to tell us
430eda14cbcSMatt Macy 	 * to use the previous name, which we do below.
431eda14cbcSMatt Macy 	 */
432eda14cbcSMatt Macy 	if (spa->spa_import_flags & ZFS_IMPORT_TEMP_NAME) {
433eda14cbcSMatt Macy 		VERIFY0(nvlist_lookup_string(spa->spa_config,
434eda14cbcSMatt Macy 		    ZPOOL_CONFIG_POOL_NAME, &pool_name));
435eda14cbcSMatt Macy 	} else
436eda14cbcSMatt Macy 		pool_name = spa_name(spa);
437eda14cbcSMatt Macy 
438eda14cbcSMatt Macy 	config = fnvlist_alloc();
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy 	fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa));
441eda14cbcSMatt Macy 	fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, pool_name);
442eda14cbcSMatt Macy 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, spa_state(spa));
443eda14cbcSMatt Macy 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, txg);
444eda14cbcSMatt Macy 	fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, spa_guid(spa));
445eda14cbcSMatt Macy 	fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, spa->spa_errata);
446eda14cbcSMatt Macy 	if (spa->spa_comment != NULL)
447eda14cbcSMatt Macy 		fnvlist_add_string(config, ZPOOL_CONFIG_COMMENT,
448eda14cbcSMatt Macy 		    spa->spa_comment);
449eda14cbcSMatt Macy 
450eda14cbcSMatt Macy 	hostid = spa_get_hostid(spa);
451eda14cbcSMatt Macy 	if (hostid != 0)
452eda14cbcSMatt Macy 		fnvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, hostid);
453eda14cbcSMatt Macy 	fnvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, utsname()->nodename);
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy 	int config_gen_flags = 0;
456eda14cbcSMatt Macy 	if (vd != rvd) {
457eda14cbcSMatt Macy 		fnvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
458eda14cbcSMatt Macy 		    vd->vdev_top->vdev_guid);
459eda14cbcSMatt Macy 		fnvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
460eda14cbcSMatt Macy 		    vd->vdev_guid);
461eda14cbcSMatt Macy 		if (vd->vdev_isspare)
462eda14cbcSMatt Macy 			fnvlist_add_uint64(config,
463eda14cbcSMatt Macy 			    ZPOOL_CONFIG_IS_SPARE, 1ULL);
464eda14cbcSMatt Macy 		if (vd->vdev_islog)
465eda14cbcSMatt Macy 			fnvlist_add_uint64(config,
466eda14cbcSMatt Macy 			    ZPOOL_CONFIG_IS_LOG, 1ULL);
467eda14cbcSMatt Macy 		vd = vd->vdev_top;		/* label contains top config */
468eda14cbcSMatt Macy 	} else {
469eda14cbcSMatt Macy 		/*
470eda14cbcSMatt Macy 		 * Only add the (potentially large) split information
471eda14cbcSMatt Macy 		 * in the mos config, and not in the vdev labels
472eda14cbcSMatt Macy 		 */
473eda14cbcSMatt Macy 		if (spa->spa_config_splitting != NULL)
474eda14cbcSMatt Macy 			fnvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
475eda14cbcSMatt Macy 			    spa->spa_config_splitting);
476eda14cbcSMatt Macy 
477eda14cbcSMatt Macy 		fnvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS);
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 		config_gen_flags |= VDEV_CONFIG_MOS;
480eda14cbcSMatt Macy 	}
481eda14cbcSMatt Macy 
482eda14cbcSMatt Macy 	/*
483eda14cbcSMatt Macy 	 * Add the top-level config.  We even add this on pools which
484eda14cbcSMatt Macy 	 * don't support holes in the namespace.
485eda14cbcSMatt Macy 	 */
486eda14cbcSMatt Macy 	vdev_top_config_generate(spa, config);
487eda14cbcSMatt Macy 
488eda14cbcSMatt Macy 	/*
489eda14cbcSMatt Macy 	 * If we're splitting, record the original pool's guid.
490eda14cbcSMatt Macy 	 */
491eda14cbcSMatt Macy 	if (spa->spa_config_splitting != NULL &&
492eda14cbcSMatt Macy 	    nvlist_lookup_uint64(spa->spa_config_splitting,
493eda14cbcSMatt Macy 	    ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
494eda14cbcSMatt Macy 		fnvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID, split_guid);
495eda14cbcSMatt Macy 	}
496eda14cbcSMatt Macy 
497eda14cbcSMatt Macy 	nvroot = vdev_config_generate(spa, vd, getstats, config_gen_flags);
498eda14cbcSMatt Macy 	fnvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot);
499eda14cbcSMatt Macy 	nvlist_free(nvroot);
500eda14cbcSMatt Macy 
501eda14cbcSMatt Macy 	/*
502eda14cbcSMatt Macy 	 * Store what's necessary for reading the MOS in the label.
503eda14cbcSMatt Macy 	 */
504eda14cbcSMatt Macy 	fnvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
505eda14cbcSMatt Macy 	    spa->spa_label_features);
506eda14cbcSMatt Macy 
507eda14cbcSMatt Macy 	if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
508eda14cbcSMatt Macy 		ddt_histogram_t *ddh;
509eda14cbcSMatt Macy 		ddt_stat_t *dds;
510eda14cbcSMatt Macy 		ddt_object_t *ddo;
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy 		ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
513eda14cbcSMatt Macy 		ddt_get_dedup_histogram(spa, ddh);
514eda14cbcSMatt Macy 		fnvlist_add_uint64_array(config,
515eda14cbcSMatt Macy 		    ZPOOL_CONFIG_DDT_HISTOGRAM,
516eda14cbcSMatt Macy 		    (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t));
517eda14cbcSMatt Macy 		kmem_free(ddh, sizeof (ddt_histogram_t));
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy 		ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
520eda14cbcSMatt Macy 		ddt_get_dedup_object_stats(spa, ddo);
521eda14cbcSMatt Macy 		fnvlist_add_uint64_array(config,
522eda14cbcSMatt Macy 		    ZPOOL_CONFIG_DDT_OBJ_STATS,
523eda14cbcSMatt Macy 		    (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t));
524eda14cbcSMatt Macy 		kmem_free(ddo, sizeof (ddt_object_t));
525eda14cbcSMatt Macy 
526eda14cbcSMatt Macy 		dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
527eda14cbcSMatt Macy 		ddt_get_dedup_stats(spa, dds);
528eda14cbcSMatt Macy 		fnvlist_add_uint64_array(config,
529eda14cbcSMatt Macy 		    ZPOOL_CONFIG_DDT_STATS,
530eda14cbcSMatt Macy 		    (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t));
531eda14cbcSMatt Macy 		kmem_free(dds, sizeof (ddt_stat_t));
532eda14cbcSMatt Macy 	}
533eda14cbcSMatt Macy 
534eda14cbcSMatt Macy 	if (locked)
535eda14cbcSMatt Macy 		spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
536eda14cbcSMatt Macy 
537eda14cbcSMatt Macy 	return (config);
538eda14cbcSMatt Macy }
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy /*
541eda14cbcSMatt Macy  * Update all disk labels, generate a fresh config based on the current
542eda14cbcSMatt Macy  * in-core state, and sync the global config cache (do not sync the config
543eda14cbcSMatt Macy  * cache if this is a booting rootpool).
544eda14cbcSMatt Macy  */
545eda14cbcSMatt Macy void
546eda14cbcSMatt Macy spa_config_update(spa_t *spa, int what)
547eda14cbcSMatt Macy {
548eda14cbcSMatt Macy 	vdev_t *rvd = spa->spa_root_vdev;
549eda14cbcSMatt Macy 	uint64_t txg;
550eda14cbcSMatt Macy 	int c;
551eda14cbcSMatt Macy 
552eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
553eda14cbcSMatt Macy 
554eda14cbcSMatt Macy 	spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
555eda14cbcSMatt Macy 	txg = spa_last_synced_txg(spa) + 1;
556eda14cbcSMatt Macy 	if (what == SPA_CONFIG_UPDATE_POOL) {
557eda14cbcSMatt Macy 		vdev_config_dirty(rvd);
558eda14cbcSMatt Macy 	} else {
559eda14cbcSMatt Macy 		/*
560eda14cbcSMatt Macy 		 * If we have top-level vdevs that were added but have
561eda14cbcSMatt Macy 		 * not yet been prepared for allocation, do that now.
562eda14cbcSMatt Macy 		 * (It's safe now because the config cache is up to date,
563eda14cbcSMatt Macy 		 * so it will be able to translate the new DVAs.)
564eda14cbcSMatt Macy 		 * See comments in spa_vdev_add() for full details.
565eda14cbcSMatt Macy 		 */
566eda14cbcSMatt Macy 		for (c = 0; c < rvd->vdev_children; c++) {
567eda14cbcSMatt Macy 			vdev_t *tvd = rvd->vdev_child[c];
568eda14cbcSMatt Macy 
569eda14cbcSMatt Macy 			/*
570eda14cbcSMatt Macy 			 * Explicitly skip vdevs that are indirect or
571eda14cbcSMatt Macy 			 * log vdevs that are being removed. The reason
572eda14cbcSMatt Macy 			 * is that both of those can have vdev_ms_array
573eda14cbcSMatt Macy 			 * set to 0 and we wouldn't want to change their
574eda14cbcSMatt Macy 			 * metaslab size nor call vdev_expand() on them.
575eda14cbcSMatt Macy 			 */
576eda14cbcSMatt Macy 			if (!vdev_is_concrete(tvd) ||
577eda14cbcSMatt Macy 			    (tvd->vdev_islog && tvd->vdev_removing))
578eda14cbcSMatt Macy 				continue;
579eda14cbcSMatt Macy 
580*2c48331dSMatt Macy 			if (tvd->vdev_ms_array == 0)
581eda14cbcSMatt Macy 				vdev_metaslab_set_size(tvd);
582eda14cbcSMatt Macy 			vdev_expand(tvd, txg);
583eda14cbcSMatt Macy 		}
584eda14cbcSMatt Macy 	}
585eda14cbcSMatt Macy 	spa_config_exit(spa, SCL_ALL, FTAG);
586eda14cbcSMatt Macy 
587eda14cbcSMatt Macy 	/*
588eda14cbcSMatt Macy 	 * Wait for the mosconfig to be regenerated and synced.
589eda14cbcSMatt Macy 	 */
590eda14cbcSMatt Macy 	txg_wait_synced(spa->spa_dsl_pool, txg);
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 	/*
593eda14cbcSMatt Macy 	 * Update the global config cache to reflect the new mosconfig.
594eda14cbcSMatt Macy 	 */
595eda14cbcSMatt Macy 	if (!spa->spa_is_root) {
596eda14cbcSMatt Macy 		spa_write_cachefile(spa, B_FALSE,
597eda14cbcSMatt Macy 		    what != SPA_CONFIG_UPDATE_POOL);
598eda14cbcSMatt Macy 	}
599eda14cbcSMatt Macy 
600eda14cbcSMatt Macy 	if (what == SPA_CONFIG_UPDATE_POOL)
601eda14cbcSMatt Macy 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
602eda14cbcSMatt Macy }
603eda14cbcSMatt Macy 
604eda14cbcSMatt Macy EXPORT_SYMBOL(spa_config_load);
605eda14cbcSMatt Macy EXPORT_SYMBOL(spa_all_configs);
606eda14cbcSMatt Macy EXPORT_SYMBOL(spa_config_set);
607eda14cbcSMatt Macy EXPORT_SYMBOL(spa_config_generate);
608eda14cbcSMatt Macy EXPORT_SYMBOL(spa_config_update);
609eda14cbcSMatt Macy 
610eda14cbcSMatt Macy /* BEGIN CSTYLED */
611eda14cbcSMatt Macy #ifdef __linux__
612eda14cbcSMatt Macy /* string sysctls require a char array on FreeBSD */
613eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_spa, spa_, config_path, STRING, ZMOD_RD,
614eda14cbcSMatt Macy 	"SPA config file (/etc/zfs/zpool.cache)");
615eda14cbcSMatt Macy #endif
616eda14cbcSMatt Macy 
617eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, autoimport_disable, INT, ZMOD_RW,
618eda14cbcSMatt Macy 	"Disable pool import at module load");
619eda14cbcSMatt Macy /* END CSTYLED */
620