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
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
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 */
70a0b956f5SMartin Matuska char *spa_config_path = (char *)ZPOOL_CACHE;
71e92ffd9bSMartin Matuska #ifdef _KERNEL
72e92ffd9bSMartin Matuska static int zfs_autoimport_disable = B_TRUE;
73e92ffd9bSMartin 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
spa_config_load(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
spa_config_remove(spa_config_dirent_t * dp)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
spa_config_write(spa_config_dirent_t * dp,nvlist_t * nvl)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
spa_write_cachefile(spa_t * target,boolean_t removing,boolean_t postsysevent,boolean_t postblkidevent)243be181ee2SMartin Matuska spa_write_cachefile(spa_t *target, boolean_t removing, boolean_t postsysevent,
244be181ee2SMartin Matuska boolean_t postblkidevent)
245eda14cbcSMatt Macy {
246eda14cbcSMatt Macy spa_config_dirent_t *dp, *tdp;
247eda14cbcSMatt Macy nvlist_t *nvl;
2482a58b312SMartin Matuska const char *pool_name;
249eda14cbcSMatt Macy boolean_t ccw_failure;
250eda14cbcSMatt Macy int error = 0;
251eda14cbcSMatt Macy
252eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&spa_namespace_lock));
253eda14cbcSMatt Macy
254eda14cbcSMatt Macy if (!(spa_mode_global & SPA_MODE_WRITE))
255eda14cbcSMatt Macy return;
256eda14cbcSMatt Macy
257eda14cbcSMatt Macy /*
258eda14cbcSMatt Macy * Iterate over all cachefiles for the pool, past or present. When the
259eda14cbcSMatt Macy * cachefile is changed, the new one is pushed onto this list, allowing
260eda14cbcSMatt Macy * us to update previous cachefiles that no longer contain this pool.
261eda14cbcSMatt Macy */
262eda14cbcSMatt Macy ccw_failure = B_FALSE;
263eda14cbcSMatt Macy for (dp = list_head(&target->spa_config_list); dp != NULL;
264eda14cbcSMatt Macy dp = list_next(&target->spa_config_list, dp)) {
265eda14cbcSMatt Macy spa_t *spa = NULL;
266eda14cbcSMatt Macy if (dp->scd_path == NULL)
267eda14cbcSMatt Macy continue;
268eda14cbcSMatt Macy
269eda14cbcSMatt Macy /*
270eda14cbcSMatt Macy * Iterate over all pools, adding any matching pools to 'nvl'.
271eda14cbcSMatt Macy */
272eda14cbcSMatt Macy nvl = NULL;
273eda14cbcSMatt Macy while ((spa = spa_next(spa)) != NULL) {
274eda14cbcSMatt Macy /*
275eda14cbcSMatt Macy * Skip over our own pool if we're about to remove
276eda14cbcSMatt Macy * ourselves from the spa namespace or any pool that
277eda14cbcSMatt Macy * is readonly. Since we cannot guarantee that a
278eda14cbcSMatt Macy * readonly pool would successfully import upon reboot,
279eda14cbcSMatt Macy * we don't allow them to be written to the cache file.
280eda14cbcSMatt Macy */
281eda14cbcSMatt Macy if ((spa == target && removing) ||
282eda14cbcSMatt Macy !spa_writeable(spa))
283eda14cbcSMatt Macy continue;
284eda14cbcSMatt Macy
285eda14cbcSMatt Macy mutex_enter(&spa->spa_props_lock);
286eda14cbcSMatt Macy tdp = list_head(&spa->spa_config_list);
287eda14cbcSMatt Macy if (spa->spa_config == NULL ||
288eda14cbcSMatt Macy tdp == NULL ||
289eda14cbcSMatt Macy tdp->scd_path == NULL ||
290eda14cbcSMatt Macy strcmp(tdp->scd_path, dp->scd_path) != 0) {
291eda14cbcSMatt Macy mutex_exit(&spa->spa_props_lock);
292eda14cbcSMatt Macy continue;
293eda14cbcSMatt Macy }
294eda14cbcSMatt Macy
295eda14cbcSMatt Macy if (nvl == NULL)
296eda14cbcSMatt Macy nvl = fnvlist_alloc();
297eda14cbcSMatt Macy
298eda14cbcSMatt Macy if (spa->spa_import_flags & ZFS_IMPORT_TEMP_NAME)
299eda14cbcSMatt Macy pool_name = fnvlist_lookup_string(
300eda14cbcSMatt Macy spa->spa_config, ZPOOL_CONFIG_POOL_NAME);
301eda14cbcSMatt Macy else
302eda14cbcSMatt Macy pool_name = spa_name(spa);
303eda14cbcSMatt Macy
304eda14cbcSMatt Macy fnvlist_add_nvlist(nvl, pool_name, spa->spa_config);
305eda14cbcSMatt Macy mutex_exit(&spa->spa_props_lock);
306eda14cbcSMatt Macy }
307eda14cbcSMatt Macy
308eda14cbcSMatt Macy error = spa_config_write(dp, nvl);
309eda14cbcSMatt Macy if (error != 0)
310eda14cbcSMatt Macy ccw_failure = B_TRUE;
311eda14cbcSMatt Macy nvlist_free(nvl);
312eda14cbcSMatt Macy }
313eda14cbcSMatt Macy
314eda14cbcSMatt Macy if (ccw_failure) {
315eda14cbcSMatt Macy /*
316eda14cbcSMatt Macy * Keep trying so that configuration data is
317eda14cbcSMatt Macy * written if/when any temporary filesystem
318eda14cbcSMatt Macy * resource issues are resolved.
319eda14cbcSMatt Macy */
320eda14cbcSMatt Macy if (target->spa_ccw_fail_time == 0) {
321eac7052fSMatt Macy (void) zfs_ereport_post(
322eac7052fSMatt Macy FM_EREPORT_ZFS_CONFIG_CACHE_WRITE,
3232c48331dSMatt Macy target, NULL, NULL, NULL, 0);
324eda14cbcSMatt Macy }
325eda14cbcSMatt Macy target->spa_ccw_fail_time = gethrtime();
326eda14cbcSMatt Macy spa_async_request(target, SPA_ASYNC_CONFIG_UPDATE);
327eda14cbcSMatt Macy } else {
328eda14cbcSMatt Macy /*
329eda14cbcSMatt Macy * Do not rate limit future attempts to update
330eda14cbcSMatt Macy * the config cache.
331eda14cbcSMatt Macy */
332eda14cbcSMatt Macy target->spa_ccw_fail_time = 0;
333eda14cbcSMatt Macy }
334eda14cbcSMatt Macy
335eda14cbcSMatt Macy /*
336eda14cbcSMatt Macy * Remove any config entries older than the current one.
337eda14cbcSMatt Macy */
338eda14cbcSMatt Macy dp = list_head(&target->spa_config_list);
339eda14cbcSMatt Macy while ((tdp = list_next(&target->spa_config_list, dp)) != NULL) {
340eda14cbcSMatt Macy list_remove(&target->spa_config_list, tdp);
341eda14cbcSMatt Macy if (tdp->scd_path != NULL)
342eda14cbcSMatt Macy spa_strfree(tdp->scd_path);
343eda14cbcSMatt Macy kmem_free(tdp, sizeof (spa_config_dirent_t));
344eda14cbcSMatt Macy }
345eda14cbcSMatt Macy
346eda14cbcSMatt Macy spa_config_generation++;
347eda14cbcSMatt Macy
348eda14cbcSMatt Macy if (postsysevent)
349eda14cbcSMatt Macy spa_event_notify(target, NULL, NULL, ESC_ZFS_CONFIG_SYNC);
350be181ee2SMartin Matuska
351be181ee2SMartin Matuska /*
352be181ee2SMartin Matuska * Post udev event to sync blkid information if the pool is created
353be181ee2SMartin Matuska * or a new vdev is added to the pool.
354be181ee2SMartin Matuska */
355be181ee2SMartin Matuska if ((target->spa_root_vdev) && postblkidevent) {
356be181ee2SMartin Matuska vdev_post_kobj_evt(target->spa_root_vdev);
357be181ee2SMartin Matuska for (int i = 0; i < target->spa_l2cache.sav_count; i++)
358be181ee2SMartin Matuska vdev_post_kobj_evt(target->spa_l2cache.sav_vdevs[i]);
35915f0b8c3SMartin Matuska for (int i = 0; i < target->spa_spares.sav_count; i++)
36015f0b8c3SMartin Matuska vdev_post_kobj_evt(target->spa_spares.sav_vdevs[i]);
361be181ee2SMartin Matuska }
362eda14cbcSMatt Macy }
363eda14cbcSMatt Macy
364eda14cbcSMatt Macy /*
365eda14cbcSMatt Macy * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
366eda14cbcSMatt Macy * and we don't want to allow the local zone to see all the pools anyway.
367eda14cbcSMatt Macy * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
368eda14cbcSMatt Macy * information for all pool visible within the zone.
369eda14cbcSMatt Macy */
370*6c1e79dfSMartin Matuska int
spa_all_configs(uint64_t * generation,nvlist_t ** pools)371*6c1e79dfSMartin Matuska spa_all_configs(uint64_t *generation, nvlist_t **pools)
372eda14cbcSMatt Macy {
373eda14cbcSMatt Macy spa_t *spa = NULL;
374eda14cbcSMatt Macy
375eda14cbcSMatt Macy if (*generation == spa_config_generation)
376*6c1e79dfSMartin Matuska return (SET_ERROR(EEXIST));
377eda14cbcSMatt Macy
378*6c1e79dfSMartin Matuska int error = mutex_enter_interruptible(&spa_namespace_lock);
379*6c1e79dfSMartin Matuska if (error)
380*6c1e79dfSMartin Matuska return (SET_ERROR(EINTR));
381eda14cbcSMatt Macy
382*6c1e79dfSMartin Matuska *pools = fnvlist_alloc();
383eda14cbcSMatt Macy while ((spa = spa_next(spa)) != NULL) {
384eda14cbcSMatt Macy if (INGLOBALZONE(curproc) ||
385eda14cbcSMatt Macy zone_dataset_visible(spa_name(spa), NULL)) {
386eda14cbcSMatt Macy mutex_enter(&spa->spa_props_lock);
387*6c1e79dfSMartin Matuska fnvlist_add_nvlist(*pools, spa_name(spa),
388eda14cbcSMatt Macy spa->spa_config);
389eda14cbcSMatt Macy mutex_exit(&spa->spa_props_lock);
390eda14cbcSMatt Macy }
391eda14cbcSMatt Macy }
392eda14cbcSMatt Macy *generation = spa_config_generation;
393eda14cbcSMatt Macy mutex_exit(&spa_namespace_lock);
394eda14cbcSMatt Macy
395*6c1e79dfSMartin Matuska return (0);
396eda14cbcSMatt Macy }
397eda14cbcSMatt Macy
398eda14cbcSMatt Macy void
spa_config_set(spa_t * spa,nvlist_t * config)399eda14cbcSMatt Macy spa_config_set(spa_t *spa, nvlist_t *config)
400eda14cbcSMatt Macy {
401eda14cbcSMatt Macy mutex_enter(&spa->spa_props_lock);
402eda14cbcSMatt Macy if (spa->spa_config != NULL && spa->spa_config != config)
403eda14cbcSMatt Macy nvlist_free(spa->spa_config);
404eda14cbcSMatt Macy spa->spa_config = config;
405eda14cbcSMatt Macy mutex_exit(&spa->spa_props_lock);
406eda14cbcSMatt Macy }
407eda14cbcSMatt Macy
408eda14cbcSMatt Macy /*
409eda14cbcSMatt Macy * Generate the pool's configuration based on the current in-core state.
410eda14cbcSMatt Macy *
411eda14cbcSMatt Macy * We infer whether to generate a complete config or just one top-level config
412eda14cbcSMatt Macy * based on whether vd is the root vdev.
413eda14cbcSMatt Macy */
414eda14cbcSMatt Macy nvlist_t *
spa_config_generate(spa_t * spa,vdev_t * vd,uint64_t txg,int getstats)415eda14cbcSMatt Macy spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
416eda14cbcSMatt Macy {
417eda14cbcSMatt Macy nvlist_t *config, *nvroot;
418eda14cbcSMatt Macy vdev_t *rvd = spa->spa_root_vdev;
419eda14cbcSMatt Macy unsigned long hostid = 0;
420eda14cbcSMatt Macy boolean_t locked = B_FALSE;
421eda14cbcSMatt Macy uint64_t split_guid;
4222a58b312SMartin Matuska const char *pool_name;
423eda14cbcSMatt Macy
424eda14cbcSMatt Macy if (vd == NULL) {
425eda14cbcSMatt Macy vd = rvd;
426eda14cbcSMatt Macy locked = B_TRUE;
427eda14cbcSMatt Macy spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
428eda14cbcSMatt Macy }
429eda14cbcSMatt Macy
430eda14cbcSMatt Macy ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
431eda14cbcSMatt Macy (SCL_CONFIG | SCL_STATE));
432eda14cbcSMatt Macy
433eda14cbcSMatt Macy /*
434eda14cbcSMatt Macy * If txg is -1, report the current value of spa->spa_config_txg.
435eda14cbcSMatt Macy */
436eda14cbcSMatt Macy if (txg == -1ULL)
437eda14cbcSMatt Macy txg = spa->spa_config_txg;
438eda14cbcSMatt Macy
439eda14cbcSMatt Macy /*
440eda14cbcSMatt Macy * Originally, users had to handle spa namespace collisions by either
441eda14cbcSMatt Macy * exporting the already imported pool or by specifying a new name for
442eda14cbcSMatt Macy * the pool with a conflicting name. In the case of root pools from
443eda14cbcSMatt Macy * virtual guests, neither approach to collision resolution is
444eda14cbcSMatt Macy * reasonable. This is addressed by extending the new name syntax with
445eda14cbcSMatt Macy * an option to specify that the new name is temporary. When specified,
446eda14cbcSMatt Macy * ZFS_IMPORT_TEMP_NAME will be set in spa->spa_import_flags to tell us
447eda14cbcSMatt Macy * to use the previous name, which we do below.
448eda14cbcSMatt Macy */
449eda14cbcSMatt Macy if (spa->spa_import_flags & ZFS_IMPORT_TEMP_NAME) {
450eda14cbcSMatt Macy VERIFY0(nvlist_lookup_string(spa->spa_config,
451eda14cbcSMatt Macy ZPOOL_CONFIG_POOL_NAME, &pool_name));
452eda14cbcSMatt Macy } else
453eda14cbcSMatt Macy pool_name = spa_name(spa);
454eda14cbcSMatt Macy
455eda14cbcSMatt Macy config = fnvlist_alloc();
456eda14cbcSMatt Macy
457eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, spa_version(spa));
458eda14cbcSMatt Macy fnvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, pool_name);
459eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, spa_state(spa));
460eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, txg);
461eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, spa_guid(spa));
462eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA, spa->spa_errata);
463eda14cbcSMatt Macy if (spa->spa_comment != NULL)
464eda14cbcSMatt Macy fnvlist_add_string(config, ZPOOL_CONFIG_COMMENT,
465eda14cbcSMatt Macy spa->spa_comment);
466ee36e25aSMartin Matuska if (spa->spa_compatibility != NULL)
467ee36e25aSMartin Matuska fnvlist_add_string(config, ZPOOL_CONFIG_COMPATIBILITY,
468ee36e25aSMartin Matuska spa->spa_compatibility);
469eda14cbcSMatt Macy
470eda14cbcSMatt Macy hostid = spa_get_hostid(spa);
471eda14cbcSMatt Macy if (hostid != 0)
472eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID, hostid);
473eda14cbcSMatt Macy fnvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME, utsname()->nodename);
474eda14cbcSMatt Macy
475eda14cbcSMatt Macy int config_gen_flags = 0;
476eda14cbcSMatt Macy if (vd != rvd) {
477eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
478eda14cbcSMatt Macy vd->vdev_top->vdev_guid);
479eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
480eda14cbcSMatt Macy vd->vdev_guid);
481eda14cbcSMatt Macy if (vd->vdev_isspare)
482eda14cbcSMatt Macy fnvlist_add_uint64(config,
483eda14cbcSMatt Macy ZPOOL_CONFIG_IS_SPARE, 1ULL);
484eda14cbcSMatt Macy if (vd->vdev_islog)
485eda14cbcSMatt Macy fnvlist_add_uint64(config,
486eda14cbcSMatt Macy ZPOOL_CONFIG_IS_LOG, 1ULL);
487eda14cbcSMatt Macy vd = vd->vdev_top; /* label contains top config */
488eda14cbcSMatt Macy } else {
489eda14cbcSMatt Macy /*
490eda14cbcSMatt Macy * Only add the (potentially large) split information
491eda14cbcSMatt Macy * in the mos config, and not in the vdev labels
492eda14cbcSMatt Macy */
493eda14cbcSMatt Macy if (spa->spa_config_splitting != NULL)
494eda14cbcSMatt Macy fnvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT,
495eda14cbcSMatt Macy spa->spa_config_splitting);
496eda14cbcSMatt Macy
497eda14cbcSMatt Macy fnvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS);
498eda14cbcSMatt Macy
499eda14cbcSMatt Macy config_gen_flags |= VDEV_CONFIG_MOS;
500eda14cbcSMatt Macy }
501eda14cbcSMatt Macy
502eda14cbcSMatt Macy /*
503eda14cbcSMatt Macy * Add the top-level config. We even add this on pools which
504eda14cbcSMatt Macy * don't support holes in the namespace.
505eda14cbcSMatt Macy */
506eda14cbcSMatt Macy vdev_top_config_generate(spa, config);
507eda14cbcSMatt Macy
508eda14cbcSMatt Macy /*
509eda14cbcSMatt Macy * If we're splitting, record the original pool's guid.
510eda14cbcSMatt Macy */
511eda14cbcSMatt Macy if (spa->spa_config_splitting != NULL &&
512eda14cbcSMatt Macy nvlist_lookup_uint64(spa->spa_config_splitting,
513eda14cbcSMatt Macy ZPOOL_CONFIG_SPLIT_GUID, &split_guid) == 0) {
514eda14cbcSMatt Macy fnvlist_add_uint64(config, ZPOOL_CONFIG_SPLIT_GUID, split_guid);
515eda14cbcSMatt Macy }
516eda14cbcSMatt Macy
517eda14cbcSMatt Macy nvroot = vdev_config_generate(spa, vd, getstats, config_gen_flags);
518eda14cbcSMatt Macy fnvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot);
519eda14cbcSMatt Macy nvlist_free(nvroot);
520eda14cbcSMatt Macy
521eda14cbcSMatt Macy /*
522eda14cbcSMatt Macy * Store what's necessary for reading the MOS in the label.
523eda14cbcSMatt Macy */
524eda14cbcSMatt Macy fnvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ,
525eda14cbcSMatt Macy spa->spa_label_features);
526eda14cbcSMatt Macy
527eda14cbcSMatt Macy if (getstats && spa_load_state(spa) == SPA_LOAD_NONE) {
528eda14cbcSMatt Macy ddt_histogram_t *ddh;
529eda14cbcSMatt Macy ddt_stat_t *dds;
530eda14cbcSMatt Macy ddt_object_t *ddo;
531eda14cbcSMatt Macy
532eda14cbcSMatt Macy ddh = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
533eda14cbcSMatt Macy ddt_get_dedup_histogram(spa, ddh);
534eda14cbcSMatt Macy fnvlist_add_uint64_array(config,
535eda14cbcSMatt Macy ZPOOL_CONFIG_DDT_HISTOGRAM,
536eda14cbcSMatt Macy (uint64_t *)ddh, sizeof (*ddh) / sizeof (uint64_t));
537eda14cbcSMatt Macy kmem_free(ddh, sizeof (ddt_histogram_t));
538eda14cbcSMatt Macy
539eda14cbcSMatt Macy ddo = kmem_zalloc(sizeof (ddt_object_t), KM_SLEEP);
540eda14cbcSMatt Macy ddt_get_dedup_object_stats(spa, ddo);
541eda14cbcSMatt Macy fnvlist_add_uint64_array(config,
542eda14cbcSMatt Macy ZPOOL_CONFIG_DDT_OBJ_STATS,
543eda14cbcSMatt Macy (uint64_t *)ddo, sizeof (*ddo) / sizeof (uint64_t));
544eda14cbcSMatt Macy kmem_free(ddo, sizeof (ddt_object_t));
545eda14cbcSMatt Macy
546eda14cbcSMatt Macy dds = kmem_zalloc(sizeof (ddt_stat_t), KM_SLEEP);
547eda14cbcSMatt Macy ddt_get_dedup_stats(spa, dds);
548eda14cbcSMatt Macy fnvlist_add_uint64_array(config,
549eda14cbcSMatt Macy ZPOOL_CONFIG_DDT_STATS,
550eda14cbcSMatt Macy (uint64_t *)dds, sizeof (*dds) / sizeof (uint64_t));
551eda14cbcSMatt Macy kmem_free(dds, sizeof (ddt_stat_t));
552eda14cbcSMatt Macy }
553eda14cbcSMatt Macy
554eda14cbcSMatt Macy if (locked)
555eda14cbcSMatt Macy spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
556eda14cbcSMatt Macy
557eda14cbcSMatt Macy return (config);
558eda14cbcSMatt Macy }
559eda14cbcSMatt Macy
560eda14cbcSMatt Macy /*
561eda14cbcSMatt Macy * Update all disk labels, generate a fresh config based on the current
562eda14cbcSMatt Macy * in-core state, and sync the global config cache (do not sync the config
563eda14cbcSMatt Macy * cache if this is a booting rootpool).
564eda14cbcSMatt Macy */
565eda14cbcSMatt Macy void
spa_config_update(spa_t * spa,int what)566eda14cbcSMatt Macy spa_config_update(spa_t *spa, int what)
567eda14cbcSMatt Macy {
568eda14cbcSMatt Macy vdev_t *rvd = spa->spa_root_vdev;
569eda14cbcSMatt Macy uint64_t txg;
570eda14cbcSMatt Macy int c;
571eda14cbcSMatt Macy
572eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&spa_namespace_lock));
573eda14cbcSMatt Macy
574eda14cbcSMatt Macy spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
575eda14cbcSMatt Macy txg = spa_last_synced_txg(spa) + 1;
576eda14cbcSMatt Macy if (what == SPA_CONFIG_UPDATE_POOL) {
577eda14cbcSMatt Macy vdev_config_dirty(rvd);
578eda14cbcSMatt Macy } else {
579eda14cbcSMatt Macy /*
580eda14cbcSMatt Macy * If we have top-level vdevs that were added but have
581eda14cbcSMatt Macy * not yet been prepared for allocation, do that now.
582eda14cbcSMatt Macy * (It's safe now because the config cache is up to date,
583eda14cbcSMatt Macy * so it will be able to translate the new DVAs.)
584eda14cbcSMatt Macy * See comments in spa_vdev_add() for full details.
585eda14cbcSMatt Macy */
586eda14cbcSMatt Macy for (c = 0; c < rvd->vdev_children; c++) {
587eda14cbcSMatt Macy vdev_t *tvd = rvd->vdev_child[c];
588eda14cbcSMatt Macy
589eda14cbcSMatt Macy /*
590eda14cbcSMatt Macy * Explicitly skip vdevs that are indirect or
591eda14cbcSMatt Macy * log vdevs that are being removed. The reason
592eda14cbcSMatt Macy * is that both of those can have vdev_ms_array
593eda14cbcSMatt Macy * set to 0 and we wouldn't want to change their
594eda14cbcSMatt Macy * metaslab size nor call vdev_expand() on them.
595eda14cbcSMatt Macy */
596eda14cbcSMatt Macy if (!vdev_is_concrete(tvd) ||
597eda14cbcSMatt Macy (tvd->vdev_islog && tvd->vdev_removing))
598eda14cbcSMatt Macy continue;
599eda14cbcSMatt Macy
6002c48331dSMatt Macy if (tvd->vdev_ms_array == 0)
601eda14cbcSMatt Macy vdev_metaslab_set_size(tvd);
602eda14cbcSMatt Macy vdev_expand(tvd, txg);
603eda14cbcSMatt Macy }
604eda14cbcSMatt Macy }
605eda14cbcSMatt Macy spa_config_exit(spa, SCL_ALL, FTAG);
606eda14cbcSMatt Macy
607eda14cbcSMatt Macy /*
608eda14cbcSMatt Macy * Wait for the mosconfig to be regenerated and synced.
609eda14cbcSMatt Macy */
610eda14cbcSMatt Macy txg_wait_synced(spa->spa_dsl_pool, txg);
611eda14cbcSMatt Macy
612eda14cbcSMatt Macy /*
613eda14cbcSMatt Macy * Update the global config cache to reflect the new mosconfig.
614eda14cbcSMatt Macy */
615eda14cbcSMatt Macy if (!spa->spa_is_root) {
616eda14cbcSMatt Macy spa_write_cachefile(spa, B_FALSE,
617be181ee2SMartin Matuska what != SPA_CONFIG_UPDATE_POOL,
618eda14cbcSMatt Macy what != SPA_CONFIG_UPDATE_POOL);
619eda14cbcSMatt Macy }
620eda14cbcSMatt Macy
621eda14cbcSMatt Macy if (what == SPA_CONFIG_UPDATE_POOL)
622eda14cbcSMatt Macy spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
623eda14cbcSMatt Macy }
624eda14cbcSMatt Macy
625eda14cbcSMatt Macy EXPORT_SYMBOL(spa_config_load);
626eda14cbcSMatt Macy EXPORT_SYMBOL(spa_all_configs);
627eda14cbcSMatt Macy EXPORT_SYMBOL(spa_config_set);
628eda14cbcSMatt Macy EXPORT_SYMBOL(spa_config_generate);
629eda14cbcSMatt Macy EXPORT_SYMBOL(spa_config_update);
630eda14cbcSMatt Macy
631eda14cbcSMatt Macy #ifdef __linux__
632eda14cbcSMatt Macy /* string sysctls require a char array on FreeBSD */
633eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_spa, spa_, config_path, STRING, ZMOD_RD,
634eda14cbcSMatt Macy "SPA config file (/etc/zfs/zpool.cache)");
635eda14cbcSMatt Macy #endif
636eda14cbcSMatt Macy
637eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs, zfs_, autoimport_disable, INT, ZMOD_RW,
638eda14cbcSMatt Macy "Disable pool import at module load");
639