1 /* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 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> 28 */ 29 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/fs/zfs.h> 36 #include <sys/vdev_impl.h> 37 #include <sys/zfs_ioctl.h> 38 #include <sys/systeminfo.h> 39 #include <sys/sunddi.h> 40 #include <sys/zfeature.h> 41 #include <sys/zfs_file.h> 42 #include <sys/zfs_context.h> 43 #ifdef _KERNEL 44 #include <sys/zone.h> 45 #endif 46 47 /* 48 * Pool configuration repository. 49 * 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. 55 * 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. 62 */ 63 64 static uint64_t spa_config_generation = 1; 65 66 /* 67 * This can be overridden in userland to preserve an alternate namespace for 68 * userland pools when doing testing. 69 */ 70 char *spa_config_path = ZPOOL_CACHE; 71 int zfs_autoimport_disable = 1; 72 73 /* 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. 77 */ 78 void 79 spa_config_load(void) 80 { 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; 89 90 #ifdef _KERNEL 91 if (zfs_autoimport_disable) 92 return; 93 #endif 94 95 /* 96 * Open the configuration file. 97 */ 98 pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 99 100 (void) snprintf(pathname, MAXPATHLEN, "%s", spa_config_path); 101 102 err = zfs_file_open(pathname, O_RDONLY, 0, &fp); 103 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); 109 110 if (err) 111 return; 112 113 if (zfs_file_getattr(fp, &zfa)) 114 goto out; 115 116 fsize = zfa.zfa_size; 117 buf = kmem_alloc(fsize, KM_SLEEP); 118 119 /* 120 * Read the nvlist from the file. 121 */ 122 if (zfs_file_read(fp, buf, fsize, NULL) < 0) 123 goto out; 124 125 /* 126 * Unpack the nvlist. 127 */ 128 if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0) 129 goto out; 130 131 /* 132 * Iterate over all elements in the nvlist, creating a new spa_t for 133 * each one with the specified configuration. 134 */ 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; 140 141 child = fnvpair_value_nvlist(nvpair); 142 143 if (spa_lookup(nvpair_name(nvpair)) != NULL) 144 continue; 145 (void) spa_add(nvpair_name(nvpair), child, NULL); 146 } 147 mutex_exit(&spa_namespace_lock); 148 149 nvlist_free(nvlist); 150 151 out: 152 if (buf != NULL) 153 kmem_free(buf, fsize); 154 155 zfs_file_close(fp); 156 } 157 158 static int 159 spa_config_remove(spa_config_dirent_t *dp) 160 { 161 int error = 0; 162 163 /* 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. 167 */ 168 error = zfs_file_unlink(dp->scd_path); 169 if (error == EOPNOTSUPP) { 170 int flags = O_RDWR | O_TRUNC; 171 zfs_file_t *fp; 172 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); 177 } 178 } 179 180 return (error); 181 } 182 183 static int 184 spa_config_write(spa_config_dirent_t *dp, nvlist_t *nvl) 185 { 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; 192 193 /* 194 * If the nvlist is empty (NULL), then remove the old cachefile. 195 */ 196 if (nvl == NULL) { 197 err = spa_config_remove(dp); 198 if (err == ENOENT) 199 err = 0; 200 201 return (err); 202 } 203 204 /* 205 * Pack the configuration into a buffer. 206 */ 207 buf = fnvlist_pack(nvl, &buflen); 208 temp = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 209 210 /* 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. 215 */ 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); 221 222 zfs_file_close(fp); 223 if (err) 224 (void) spa_config_remove(dp); 225 } 226 fnvlist_pack_free(buf, buflen); 227 kmem_free(temp, MAXPATHLEN); 228 return (err); 229 } 230 231 /* 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. 239 */ 240 void 241 spa_write_cachefile(spa_t *target, boolean_t removing, boolean_t postsysevent) 242 { 243 spa_config_dirent_t *dp, *tdp; 244 nvlist_t *nvl; 245 char *pool_name; 246 boolean_t ccw_failure; 247 int error = 0; 248 249 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 250 251 if (!(spa_mode_global & SPA_MODE_WRITE)) 252 return; 253 254 /* 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. 258 */ 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; 265 266 /* 267 * Iterate over all pools, adding any matching pools to 'nvl'. 268 */ 269 nvl = NULL; 270 while ((spa = spa_next(spa)) != NULL) { 271 /* 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. 277 */ 278 if ((spa == target && removing) || 279 !spa_writeable(spa)) 280 continue; 281 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; 290 } 291 292 if (nvl == NULL) 293 nvl = fnvlist_alloc(); 294 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); 300 301 fnvlist_add_nvlist(nvl, pool_name, spa->spa_config); 302 mutex_exit(&spa->spa_props_lock); 303 } 304 305 error = spa_config_write(dp, nvl); 306 if (error != 0) 307 ccw_failure = B_TRUE; 308 nvlist_free(nvl); 309 } 310 311 if (ccw_failure) { 312 /* 313 * Keep trying so that configuration data is 314 * written if/when any temporary filesystem 315 * resource issues are resolved. 316 */ 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); 321 } 322 target->spa_ccw_fail_time = gethrtime(); 323 spa_async_request(target, SPA_ASYNC_CONFIG_UPDATE); 324 } else { 325 /* 326 * Do not rate limit future attempts to update 327 * the config cache. 328 */ 329 target->spa_ccw_fail_time = 0; 330 } 331 332 /* 333 * Remove any config entries older than the current one. 334 */ 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)); 341 } 342 343 spa_config_generation++; 344 345 if (postsysevent) 346 spa_event_notify(target, NULL, NULL, ESC_ZFS_CONFIG_SYNC); 347 } 348 349 /* 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. 354 */ 355 nvlist_t * 356 spa_all_configs(uint64_t *generation) 357 { 358 nvlist_t *pools; 359 spa_t *spa = NULL; 360 361 if (*generation == spa_config_generation) 362 return (NULL); 363 364 pools = fnvlist_alloc(); 365 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); 374 } 375 } 376 *generation = spa_config_generation; 377 mutex_exit(&spa_namespace_lock); 378 379 return (pools); 380 } 381 382 void 383 spa_config_set(spa_t *spa, nvlist_t *config) 384 { 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); 390 } 391 392 /* 393 * Generate the pool's configuration based on the current in-core state. 394 * 395 * We infer whether to generate a complete config or just one top-level config 396 * based on whether vd is the root vdev. 397 */ 398 nvlist_t * 399 spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats) 400 { 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; 407 408 if (vd == NULL) { 409 vd = rvd; 410 locked = B_TRUE; 411 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); 412 } 413 414 ASSERT(spa_config_held(spa, SCL_CONFIG | SCL_STATE, RW_READER) == 415 (SCL_CONFIG | SCL_STATE)); 416 417 /* 418 * If txg is -1, report the current value of spa->spa_config_txg. 419 */ 420 if (txg == -1ULL) 421 txg = spa->spa_config_txg; 422 423 /* 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. 432 */ 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); 438 439 config = fnvlist_alloc(); 440 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); 453 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); 458 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 { 473 /* 474 * Only add the (potentially large) split information 475 * in the mos config, and not in the vdev labels 476 */ 477 if (spa->spa_config_splitting != NULL) 478 fnvlist_add_nvlist(config, ZPOOL_CONFIG_SPLIT, 479 spa->spa_config_splitting); 480 481 fnvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS); 482 483 config_gen_flags |= VDEV_CONFIG_MOS; 484 } 485 486 /* 487 * Add the top-level config. We even add this on pools which 488 * don't support holes in the namespace. 489 */ 490 vdev_top_config_generate(spa, config); 491 492 /* 493 * If we're splitting, record the original pool's guid. 494 */ 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); 499 } 500 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); 504 505 /* 506 * Store what's necessary for reading the MOS in the label. 507 */ 508 fnvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 509 spa->spa_label_features); 510 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; 515 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)); 522 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)); 529 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)); 536 } 537 538 if (locked) 539 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); 540 541 return (config); 542 } 543 544 /* 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). 548 */ 549 void 550 spa_config_update(spa_t *spa, int what) 551 { 552 vdev_t *rvd = spa->spa_root_vdev; 553 uint64_t txg; 554 int c; 555 556 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 557 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 { 563 /* 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. 569 */ 570 for (c = 0; c < rvd->vdev_children; c++) { 571 vdev_t *tvd = rvd->vdev_child[c]; 572 573 /* 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. 579 */ 580 if (!vdev_is_concrete(tvd) || 581 (tvd->vdev_islog && tvd->vdev_removing)) 582 continue; 583 584 if (tvd->vdev_ms_array == 0) 585 vdev_metaslab_set_size(tvd); 586 vdev_expand(tvd, txg); 587 } 588 } 589 spa_config_exit(spa, SCL_ALL, FTAG); 590 591 /* 592 * Wait for the mosconfig to be regenerated and synced. 593 */ 594 txg_wait_synced(spa->spa_dsl_pool, txg); 595 596 /* 597 * Update the global config cache to reflect the new mosconfig. 598 */ 599 if (!spa->spa_is_root) { 600 spa_write_cachefile(spa, B_FALSE, 601 what != SPA_CONFIG_UPDATE_POOL); 602 } 603 604 if (what == SPA_CONFIG_UPDATE_POOL) 605 spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS); 606 } 607 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); 613 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 620 621 ZFS_MODULE_PARAM(zfs, zfs_, autoimport_disable, INT, ZMOD_RW, 622 "Disable pool import at module load"); 623 /* END CSTYLED */ 624