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