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