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