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