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