165918Sasmodai/* 265918Sasmodai * CDDL HEADER START 365918Sasmodai * 465918Sasmodai * The contents of this file are subject to the terms of the 565918Sasmodai * Common Development and Distribution License (the "License"). 665918Sasmodai * You may not use this file except in compliance with the License. 765918Sasmodai * 865918Sasmodai * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 965918Sasmodai * or http://www.opensolaris.org/os/licensing. 1065918Sasmodai * See the License for the specific language governing permissions 1165918Sasmodai * and limitations under the License. 1265918Sasmodai * 1365918Sasmodai * When distributing Covered Code, include this CDDL HEADER in each 1465918Sasmodai * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1565918Sasmodai * If applicable, add the following below this CDDL HEADER, with the 1665918Sasmodai * fields enclosed by brackets "[]" replaced with your own identifying 1765918Sasmodai * information: Portions Copyright [yyyy] [name of copyright owner] 1865918Sasmodai * 1965918Sasmodai * CDDL HEADER END 2065918Sasmodai */ 2165918Sasmodai/* 2265918Sasmodai * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2365918Sasmodai * Copyright (c) 2013 by Delphix. All rights reserved. 2465918Sasmodai * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 2565918Sasmodai * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 2665918Sasmodai */ 2765918Sasmodai 2865918Sasmodai#include <sys/zfs_context.h> 2972112Sasmodai#include <sys/spa_impl.h> 3065918Sasmodai#include <sys/spa_boot.h> 3165918Sasmodai#include <sys/zio.h> 3272112Sasmodai#include <sys/zio_checksum.h> 3365918Sasmodai#include <sys/zio_compress.h> 3498415Sobrien#include <sys/dmu.h> 3598415Sobrien#include <sys/dmu_tx.h> 3669356Sasmodai#include <sys/zap.h> 3769356Sasmodai#include <sys/zil.h> 3865918Sasmodai#include <sys/vdev_impl.h> 3972112Sasmodai#include <sys/metaslab.h> 4065918Sasmodai#include <sys/uberblock_impl.h> 4172112Sasmodai#include <sys/txg.h> 4265918Sasmodai#include <sys/avl.h> 4365918Sasmodai#include <sys/unique.h> 4498415Sobrien#include <sys/dsl_pool.h> 4598415Sobrien#include <sys/dsl_dir.h> 4672112Sasmodai#include <sys/dsl_prop.h> 47#include <sys/dsl_scan.h> 48#include <sys/fs/zfs.h> 49#include <sys/metaslab_impl.h> 50#include <sys/arc.h> 51#include <sys/ddt.h> 52#include "zfs_prop.h" 53#include "zfeature_common.h" 54 55/* 56 * SPA locking 57 * 58 * There are four basic locks for managing spa_t structures: 59 * 60 * spa_namespace_lock (global mutex) 61 * 62 * This lock must be acquired to do any of the following: 63 * 64 * - Lookup a spa_t by name 65 * - Add or remove a spa_t from the namespace 66 * - Increase spa_refcount from non-zero 67 * - Check if spa_refcount is zero 68 * - Rename a spa_t 69 * - add/remove/attach/detach devices 70 * - Held for the duration of create/destroy/import/export 71 * 72 * It does not need to handle recursion. A create or destroy may 73 * reference objects (files or zvols) in other pools, but by 74 * definition they must have an existing reference, and will never need 75 * to lookup a spa_t by name. 76 * 77 * spa_refcount (per-spa refcount_t protected by mutex) 78 * 79 * This reference count keep track of any active users of the spa_t. The 80 * spa_t cannot be destroyed or freed while this is non-zero. Internally, 81 * the refcount is never really 'zero' - opening a pool implicitly keeps 82 * some references in the DMU. Internally we check against spa_minref, but 83 * present the image of a zero/non-zero value to consumers. 84 * 85 * spa_config_lock[] (per-spa array of rwlocks) 86 * 87 * This protects the spa_t from config changes, and must be held in 88 * the following circumstances: 89 * 90 * - RW_READER to perform I/O to the spa 91 * - RW_WRITER to change the vdev config 92 * 93 * The locking order is fairly straightforward: 94 * 95 * spa_namespace_lock -> spa_refcount 96 * 97 * The namespace lock must be acquired to increase the refcount from 0 98 * or to check if it is zero. 99 * 100 * spa_refcount -> spa_config_lock[] 101 * 102 * There must be at least one valid reference on the spa_t to acquire 103 * the config lock. 104 * 105 * spa_namespace_lock -> spa_config_lock[] 106 * 107 * The namespace lock must always be taken before the config lock. 108 * 109 * 110 * The spa_namespace_lock can be acquired directly and is globally visible. 111 * 112 * The namespace is manipulated using the following functions, all of which 113 * require the spa_namespace_lock to be held. 114 * 115 * spa_lookup() Lookup a spa_t by name. 116 * 117 * spa_add() Create a new spa_t in the namespace. 118 * 119 * spa_remove() Remove a spa_t from the namespace. This also 120 * frees up any memory associated with the spa_t. 121 * 122 * spa_next() Returns the next spa_t in the system, or the 123 * first if NULL is passed. 124 * 125 * spa_evict_all() Shutdown and remove all spa_t structures in 126 * the system. 127 * 128 * spa_guid_exists() Determine whether a pool/device guid exists. 129 * 130 * The spa_refcount is manipulated using the following functions: 131 * 132 * spa_open_ref() Adds a reference to the given spa_t. Must be 133 * called with spa_namespace_lock held if the 134 * refcount is currently zero. 135 * 136 * spa_close() Remove a reference from the spa_t. This will 137 * not free the spa_t or remove it from the 138 * namespace. No locking is required. 139 * 140 * spa_refcount_zero() Returns true if the refcount is currently 141 * zero. Must be called with spa_namespace_lock 142 * held. 143 * 144 * The spa_config_lock[] is an array of rwlocks, ordered as follows: 145 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 146 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 147 * 148 * To read the configuration, it suffices to hold one of these locks as reader. 149 * To modify the configuration, you must hold all locks as writer. To modify 150 * vdev state without altering the vdev tree's topology (e.g. online/offline), 151 * you must hold SCL_STATE and SCL_ZIO as writer. 152 * 153 * We use these distinct config locks to avoid recursive lock entry. 154 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 155 * block allocations (SCL_ALLOC), which may require reading space maps 156 * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 157 * 158 * The spa config locks cannot be normal rwlocks because we need the 159 * ability to hand off ownership. For example, SCL_ZIO is acquired 160 * by the issuing thread and later released by an interrupt thread. 161 * They do, however, obey the usual write-wanted semantics to prevent 162 * writer (i.e. system administrator) starvation. 163 * 164 * The lock acquisition rules are as follows: 165 * 166 * SCL_CONFIG 167 * Protects changes to the vdev tree topology, such as vdev 168 * add/remove/attach/detach. Protects the dirty config list 169 * (spa_config_dirty_list) and the set of spares and l2arc devices. 170 * 171 * SCL_STATE 172 * Protects changes to pool state and vdev state, such as vdev 173 * online/offline/fault/degrade/clear. Protects the dirty state list 174 * (spa_state_dirty_list) and global pool state (spa_state). 175 * 176 * SCL_ALLOC 177 * Protects changes to metaslab groups and classes. 178 * Held as reader by metaslab_alloc() and metaslab_claim(). 179 * 180 * SCL_ZIO 181 * Held by bp-level zios (those which have no io_vd upon entry) 182 * to prevent changes to the vdev tree. The bp-level zio implicitly 183 * protects all of its vdev child zios, which do not hold SCL_ZIO. 184 * 185 * SCL_FREE 186 * Protects changes to metaslab groups and classes. 187 * Held as reader by metaslab_free(). SCL_FREE is distinct from 188 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 189 * blocks in zio_done() while another i/o that holds either 190 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 191 * 192 * SCL_VDEV 193 * Held as reader to prevent changes to the vdev tree during trivial 194 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 195 * other locks, and lower than all of them, to ensure that it's safe 196 * to acquire regardless of caller context. 197 * 198 * In addition, the following rules apply: 199 * 200 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 201 * The lock ordering is SCL_CONFIG > spa_props_lock. 202 * 203 * (b) I/O operations on leaf vdevs. For any zio operation that takes 204 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 205 * or zio_write_phys() -- the caller must ensure that the config cannot 206 * cannot change in the interim, and that the vdev cannot be reopened. 207 * SCL_STATE as reader suffices for both. 208 * 209 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 210 * 211 * spa_vdev_enter() Acquire the namespace lock and the config lock 212 * for writing. 213 * 214 * spa_vdev_exit() Release the config lock, wait for all I/O 215 * to complete, sync the updated configs to the 216 * cache, and release the namespace lock. 217 * 218 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 219 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 220 * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 221 * 222 * spa_rename() is also implemented within this file since it requires 223 * manipulation of the namespace. 224 */ 225 226static avl_tree_t spa_namespace_avl; 227kmutex_t spa_namespace_lock; 228static kcondvar_t spa_namespace_cv; 229static int spa_active_count; 230int spa_max_replication_override = SPA_DVAS_PER_BP; 231 232static kmutex_t spa_spare_lock; 233static avl_tree_t spa_spare_avl; 234static kmutex_t spa_l2cache_lock; 235static avl_tree_t spa_l2cache_avl; 236 237kmem_cache_t *spa_buffer_pool; 238int spa_mode_global; 239 240#ifdef ZFS_DEBUG 241/* Everything except dprintf and spa is on by default in debug builds */ 242int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SPA); 243#else 244int zfs_flags = 0; 245#endif 246 247/* 248 * zfs_recover can be set to nonzero to attempt to recover from 249 * otherwise-fatal errors, typically caused by on-disk corruption. When 250 * set, calls to zfs_panic_recover() will turn into warning messages. 251 */ 252int zfs_recover = 0; 253SYSCTL_DECL(_vfs_zfs); 254TUNABLE_INT("vfs.zfs.recover", &zfs_recover); 255SYSCTL_INT(_vfs_zfs, OID_AUTO, recover, CTLFLAG_RDTUN, &zfs_recover, 0, 256 "Try to recover from otherwise-fatal errors."); 257 258extern int zfs_txg_synctime_ms; 259 260/* 261 * Expiration time in units of zfs_txg_synctime_ms. This value has two 262 * meanings. First it is used to determine when the spa_deadman logic 263 * should fire. By default the spa_deadman will fire if spa_sync has 264 * not completed in 1000 * zfs_txg_synctime_ms (i.e. 1000 seconds). 265 * Secondly, the value determines if an I/O is considered "hung". 266 * Any I/O that has not completed in zfs_deadman_synctime is considered 267 * "hung" resulting in a system panic. 268 * 1000 zfs_txg_synctime_ms (i.e. 1000 seconds). 269 */ 270uint64_t zfs_deadman_synctime = 1000ULL; 271TUNABLE_QUAD("vfs.zfs.deadman_synctime", &zfs_deadman_synctime); 272SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, deadman_synctime, CTLFLAG_RDTUN, 273 &zfs_deadman_synctime, 0, 274 "Stalled ZFS I/O expiration time in units of vfs.zfs.txg.synctime_ms"); 275 276/* 277 * Default value of -1 for zfs_deadman_enabled is resolved in 278 * zfs_deadman_init() 279 */ 280int zfs_deadman_enabled = -1; 281TUNABLE_INT("vfs.zfs.deadman_enabled", &zfs_deadman_enabled); 282SYSCTL_INT(_vfs_zfs, OID_AUTO, deadman_enabled, CTLFLAG_RDTUN, 283 &zfs_deadman_enabled, 0, "Kernel panic on stalled ZFS I/O"); 284 285#ifndef illumos 286#ifdef _KERNEL 287static void 288zfs_deadman_init() 289{ 290 /* 291 * If we are not i386 or amd64 or in a virtual machine, 292 * disable ZFS deadman thread by default 293 */ 294 if (zfs_deadman_enabled == -1) { 295#if defined(__amd64__) || defined(__i386__) 296 zfs_deadman_enabled = (vm_guest == VM_GUEST_NO) ? 1 : 0; 297#else 298 zfs_deadman_enabled = 0; 299#endif 300 } 301} 302#endif /* _KERNEL */ 303#endif /* !illumos */ 304 305/* 306 * ========================================================================== 307 * SPA config locking 308 * ========================================================================== 309 */ 310static void 311spa_config_lock_init(spa_t *spa) 312{ 313 for (int i = 0; i < SCL_LOCKS; i++) { 314 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 315 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 316 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 317 refcount_create_untracked(&scl->scl_count); 318 scl->scl_writer = NULL; 319 scl->scl_write_wanted = 0; 320 } 321} 322 323static void 324spa_config_lock_destroy(spa_t *spa) 325{ 326 for (int i = 0; i < SCL_LOCKS; i++) { 327 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 328 mutex_destroy(&scl->scl_lock); 329 cv_destroy(&scl->scl_cv); 330 refcount_destroy(&scl->scl_count); 331 ASSERT(scl->scl_writer == NULL); 332 ASSERT(scl->scl_write_wanted == 0); 333 } 334} 335 336int 337spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 338{ 339 for (int i = 0; i < SCL_LOCKS; i++) { 340 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 341 if (!(locks & (1 << i))) 342 continue; 343 mutex_enter(&scl->scl_lock); 344 if (rw == RW_READER) { 345 if (scl->scl_writer || scl->scl_write_wanted) { 346 mutex_exit(&scl->scl_lock); 347 spa_config_exit(spa, locks ^ (1 << i), tag); 348 return (0); 349 } 350 } else { 351 ASSERT(scl->scl_writer != curthread); 352 if (!refcount_is_zero(&scl->scl_count)) { 353 mutex_exit(&scl->scl_lock); 354 spa_config_exit(spa, locks ^ (1 << i), tag); 355 return (0); 356 } 357 scl->scl_writer = curthread; 358 } 359 (void) refcount_add(&scl->scl_count, tag); 360 mutex_exit(&scl->scl_lock); 361 } 362 return (1); 363} 364 365void 366spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 367{ 368 int wlocks_held = 0; 369 370 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 371 372 for (int i = 0; i < SCL_LOCKS; i++) { 373 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 374 if (scl->scl_writer == curthread) 375 wlocks_held |= (1 << i); 376 if (!(locks & (1 << i))) 377 continue; 378 mutex_enter(&scl->scl_lock); 379 if (rw == RW_READER) { 380 while (scl->scl_writer || scl->scl_write_wanted) { 381 cv_wait(&scl->scl_cv, &scl->scl_lock); 382 } 383 } else { 384 ASSERT(scl->scl_writer != curthread); 385 while (!refcount_is_zero(&scl->scl_count)) { 386 scl->scl_write_wanted++; 387 cv_wait(&scl->scl_cv, &scl->scl_lock); 388 scl->scl_write_wanted--; 389 } 390 scl->scl_writer = curthread; 391 } 392 (void) refcount_add(&scl->scl_count, tag); 393 mutex_exit(&scl->scl_lock); 394 } 395 ASSERT(wlocks_held <= locks); 396} 397 398void 399spa_config_exit(spa_t *spa, int locks, void *tag) 400{ 401 for (int i = SCL_LOCKS - 1; i >= 0; i--) { 402 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 403 if (!(locks & (1 << i))) 404 continue; 405 mutex_enter(&scl->scl_lock); 406 ASSERT(!refcount_is_zero(&scl->scl_count)); 407 if (refcount_remove(&scl->scl_count, tag) == 0) { 408 ASSERT(scl->scl_writer == NULL || 409 scl->scl_writer == curthread); 410 scl->scl_writer = NULL; /* OK in either case */ 411 cv_broadcast(&scl->scl_cv); 412 } 413 mutex_exit(&scl->scl_lock); 414 } 415} 416 417int 418spa_config_held(spa_t *spa, int locks, krw_t rw) 419{ 420 int locks_held = 0; 421 422 for (int i = 0; i < SCL_LOCKS; i++) { 423 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 424 if (!(locks & (1 << i))) 425 continue; 426 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) || 427 (rw == RW_WRITER && scl->scl_writer == curthread)) 428 locks_held |= 1 << i; 429 } 430 431 return (locks_held); 432} 433 434/* 435 * ========================================================================== 436 * SPA namespace functions 437 * ========================================================================== 438 */ 439 440/* 441 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 442 * Returns NULL if no matching spa_t is found. 443 */ 444spa_t * 445spa_lookup(const char *name) 446{ 447 static spa_t search; /* spa_t is large; don't allocate on stack */ 448 spa_t *spa; 449 avl_index_t where; 450 char *cp; 451 452 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 453 454 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 455 456 /* 457 * If it's a full dataset name, figure out the pool name and 458 * just use that. 459 */ 460 cp = strpbrk(search.spa_name, "/@"); 461 if (cp != NULL) 462 *cp = '\0'; 463 464 spa = avl_find(&spa_namespace_avl, &search, &where); 465 466 return (spa); 467} 468 469/* 470 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 471 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 472 * looking for potentially hung I/Os. 473 */ 474void 475spa_deadman(void *arg) 476{ 477 spa_t *spa = arg; 478 479 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 480 (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 481 ++spa->spa_deadman_calls); 482 if (zfs_deadman_enabled) 483 vdev_deadman(spa->spa_root_vdev); 484} 485 486/* 487 * Create an uninitialized spa_t with the given name. Requires 488 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 489 * exist by calling spa_lookup() first. 490 */ 491spa_t * 492spa_add(const char *name, nvlist_t *config, const char *altroot) 493{ 494 spa_t *spa; 495 spa_config_dirent_t *dp; 496#ifdef illumos 497 cyc_handler_t hdlr; 498 cyc_time_t when; 499#endif 500 501 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 502 503 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 504 505 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 506 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 507 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 508 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 509 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 510 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 511 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 512 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 513 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 514 515 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 516 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 517 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 518 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 519 520 for (int t = 0; t < TXG_SIZE; t++) 521 bplist_create(&spa->spa_free_bplist[t]); 522 523 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 524 spa->spa_state = POOL_STATE_UNINITIALIZED; 525 spa->spa_freeze_txg = UINT64_MAX; 526 spa->spa_final_txg = UINT64_MAX; 527 spa->spa_load_max_txg = UINT64_MAX; 528 spa->spa_proc = &p0; 529 spa->spa_proc_state = SPA_PROC_NONE; 530 531#ifdef illumos 532 hdlr.cyh_func = spa_deadman; 533 hdlr.cyh_arg = spa; 534 hdlr.cyh_level = CY_LOW_LEVEL; 535#endif 536 537 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime * 538 zfs_txg_synctime_ms); 539 540#ifdef illumos 541 /* 542 * This determines how often we need to check for hung I/Os after 543 * the cyclic has already fired. Since checking for hung I/Os is 544 * an expensive operation we don't want to check too frequently. 545 * Instead wait for 5 synctimes before checking again. 546 */ 547 when.cyt_interval = MSEC2NSEC(5 * zfs_txg_synctime_ms); 548 when.cyt_when = CY_INFINITY; 549 mutex_enter(&cpu_lock); 550 spa->spa_deadman_cycid = cyclic_add(&hdlr, &when); 551 mutex_exit(&cpu_lock); 552#else /* !illumos */ 553#ifdef _KERNEL 554 callout_init(&spa->spa_deadman_cycid, CALLOUT_MPSAFE); 555#endif 556#endif 557 refcount_create(&spa->spa_refcount); 558 spa_config_lock_init(spa); 559 560 avl_add(&spa_namespace_avl, spa); 561 562 /* 563 * Set the alternate root, if there is one. 564 */ 565 if (altroot) { 566 spa->spa_root = spa_strdup(altroot); 567 spa_active_count++; 568 } 569 570 /* 571 * Every pool starts with the default cachefile 572 */ 573 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 574 offsetof(spa_config_dirent_t, scd_link)); 575 576 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 577 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 578 list_insert_head(&spa->spa_config_list, dp); 579 580 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 581 KM_SLEEP) == 0); 582 583 if (config != NULL) { 584 nvlist_t *features; 585 586 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 587 &features) == 0) { 588 VERIFY(nvlist_dup(features, &spa->spa_label_features, 589 0) == 0); 590 } 591 592 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 593 } 594 595 if (spa->spa_label_features == NULL) { 596 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 597 KM_SLEEP) == 0); 598 } 599 600 spa->spa_debug = ((zfs_flags & ZFS_DEBUG_SPA) != 0); 601 602 return (spa); 603} 604 605/* 606 * Removes a spa_t from the namespace, freeing up any memory used. Requires 607 * spa_namespace_lock. This is called only after the spa_t has been closed and 608 * deactivated. 609 */ 610void 611spa_remove(spa_t *spa) 612{ 613 spa_config_dirent_t *dp; 614 615 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 616 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 617 618 nvlist_free(spa->spa_config_splitting); 619 620 avl_remove(&spa_namespace_avl, spa); 621 cv_broadcast(&spa_namespace_cv); 622 623 if (spa->spa_root) { 624 spa_strfree(spa->spa_root); 625 spa_active_count--; 626 } 627 628 while ((dp = list_head(&spa->spa_config_list)) != NULL) { 629 list_remove(&spa->spa_config_list, dp); 630 if (dp->scd_path != NULL) 631 spa_strfree(dp->scd_path); 632 kmem_free(dp, sizeof (spa_config_dirent_t)); 633 } 634 635 list_destroy(&spa->spa_config_list); 636 637 nvlist_free(spa->spa_label_features); 638 nvlist_free(spa->spa_load_info); 639 spa_config_set(spa, NULL); 640 641#ifdef illumos 642 mutex_enter(&cpu_lock); 643 if (spa->spa_deadman_cycid != CYCLIC_NONE) 644 cyclic_remove(spa->spa_deadman_cycid); 645 mutex_exit(&cpu_lock); 646 spa->spa_deadman_cycid = CYCLIC_NONE; 647#else /* !illumos */ 648#ifdef _KERNEL 649 callout_drain(&spa->spa_deadman_cycid); 650#endif 651#endif 652 653 refcount_destroy(&spa->spa_refcount); 654 655 spa_config_lock_destroy(spa); 656 657 for (int t = 0; t < TXG_SIZE; t++) 658 bplist_destroy(&spa->spa_free_bplist[t]); 659 660 cv_destroy(&spa->spa_async_cv); 661 cv_destroy(&spa->spa_proc_cv); 662 cv_destroy(&spa->spa_scrub_io_cv); 663 cv_destroy(&spa->spa_suspend_cv); 664 665 mutex_destroy(&spa->spa_async_lock); 666 mutex_destroy(&spa->spa_errlist_lock); 667 mutex_destroy(&spa->spa_errlog_lock); 668 mutex_destroy(&spa->spa_history_lock); 669 mutex_destroy(&spa->spa_proc_lock); 670 mutex_destroy(&spa->spa_props_lock); 671 mutex_destroy(&spa->spa_scrub_lock); 672 mutex_destroy(&spa->spa_suspend_lock); 673 mutex_destroy(&spa->spa_vdev_top_lock); 674 675 kmem_free(spa, sizeof (spa_t)); 676} 677 678/* 679 * Given a pool, return the next pool in the namespace, or NULL if there is 680 * none. If 'prev' is NULL, return the first pool. 681 */ 682spa_t * 683spa_next(spa_t *prev) 684{ 685 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 686 687 if (prev) 688 return (AVL_NEXT(&spa_namespace_avl, prev)); 689 else 690 return (avl_first(&spa_namespace_avl)); 691} 692 693/* 694 * ========================================================================== 695 * SPA refcount functions 696 * ========================================================================== 697 */ 698 699/* 700 * Add a reference to the given spa_t. Must have at least one reference, or 701 * have the namespace lock held. 702 */ 703void 704spa_open_ref(spa_t *spa, void *tag) 705{ 706 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref || 707 MUTEX_HELD(&spa_namespace_lock)); 708 (void) refcount_add(&spa->spa_refcount, tag); 709} 710 711/* 712 * Remove a reference to the given spa_t. Must have at least one reference, or 713 * have the namespace lock held. 714 */ 715void 716spa_close(spa_t *spa, void *tag) 717{ 718 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref || 719 MUTEX_HELD(&spa_namespace_lock)); 720 (void) refcount_remove(&spa->spa_refcount, tag); 721} 722 723/* 724 * Check to see if the spa refcount is zero. Must be called with 725 * spa_namespace_lock held. We really compare against spa_minref, which is the 726 * number of references acquired when opening a pool 727 */ 728boolean_t 729spa_refcount_zero(spa_t *spa) 730{ 731 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 732 733 return (refcount_count(&spa->spa_refcount) == spa->spa_minref); 734} 735 736/* 737 * ========================================================================== 738 * SPA spare and l2cache tracking 739 * ========================================================================== 740 */ 741 742/* 743 * Hot spares and cache devices are tracked using the same code below, 744 * for 'auxiliary' devices. 745 */ 746 747typedef struct spa_aux { 748 uint64_t aux_guid; 749 uint64_t aux_pool; 750 avl_node_t aux_avl; 751 int aux_count; 752} spa_aux_t; 753 754static int 755spa_aux_compare(const void *a, const void *b) 756{ 757 const spa_aux_t *sa = a; 758 const spa_aux_t *sb = b; 759 760 if (sa->aux_guid < sb->aux_guid) 761 return (-1); 762 else if (sa->aux_guid > sb->aux_guid) 763 return (1); 764 else 765 return (0); 766} 767 768void 769spa_aux_add(vdev_t *vd, avl_tree_t *avl) 770{ 771 avl_index_t where; 772 spa_aux_t search; 773 spa_aux_t *aux; 774 775 search.aux_guid = vd->vdev_guid; 776 if ((aux = avl_find(avl, &search, &where)) != NULL) { 777 aux->aux_count++; 778 } else { 779 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 780 aux->aux_guid = vd->vdev_guid; 781 aux->aux_count = 1; 782 avl_insert(avl, aux, where); 783 } 784} 785 786void 787spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 788{ 789 spa_aux_t search; 790 spa_aux_t *aux; 791 avl_index_t where; 792 793 search.aux_guid = vd->vdev_guid; 794 aux = avl_find(avl, &search, &where); 795 796 ASSERT(aux != NULL); 797 798 if (--aux->aux_count == 0) { 799 avl_remove(avl, aux); 800 kmem_free(aux, sizeof (spa_aux_t)); 801 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 802 aux->aux_pool = 0ULL; 803 } 804} 805 806boolean_t 807spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 808{ 809 spa_aux_t search, *found; 810 811 search.aux_guid = guid; 812 found = avl_find(avl, &search, NULL); 813 814 if (pool) { 815 if (found) 816 *pool = found->aux_pool; 817 else 818 *pool = 0ULL; 819 } 820 821 if (refcnt) { 822 if (found) 823 *refcnt = found->aux_count; 824 else 825 *refcnt = 0; 826 } 827 828 return (found != NULL); 829} 830 831void 832spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 833{ 834 spa_aux_t search, *found; 835 avl_index_t where; 836 837 search.aux_guid = vd->vdev_guid; 838 found = avl_find(avl, &search, &where); 839 ASSERT(found != NULL); 840 ASSERT(found->aux_pool == 0ULL); 841 842 found->aux_pool = spa_guid(vd->vdev_spa); 843} 844 845/* 846 * Spares are tracked globally due to the following constraints: 847 * 848 * - A spare may be part of multiple pools. 849 * - A spare may be added to a pool even if it's actively in use within 850 * another pool. 851 * - A spare in use in any pool can only be the source of a replacement if 852 * the target is a spare in the same pool. 853 * 854 * We keep track of all spares on the system through the use of a reference 855 * counted AVL tree. When a vdev is added as a spare, or used as a replacement 856 * spare, then we bump the reference count in the AVL tree. In addition, we set 857 * the 'vdev_isspare' member to indicate that the device is a spare (active or 858 * inactive). When a spare is made active (used to replace a device in the 859 * pool), we also keep track of which pool its been made a part of. 860 * 861 * The 'spa_spare_lock' protects the AVL tree. These functions are normally 862 * called under the spa_namespace lock as part of vdev reconfiguration. The 863 * separate spare lock exists for the status query path, which does not need to 864 * be completely consistent with respect to other vdev configuration changes. 865 */ 866 867static int 868spa_spare_compare(const void *a, const void *b) 869{ 870 return (spa_aux_compare(a, b)); 871} 872 873void 874spa_spare_add(vdev_t *vd) 875{ 876 mutex_enter(&spa_spare_lock); 877 ASSERT(!vd->vdev_isspare); 878 spa_aux_add(vd, &spa_spare_avl); 879 vd->vdev_isspare = B_TRUE; 880 mutex_exit(&spa_spare_lock); 881} 882 883void 884spa_spare_remove(vdev_t *vd) 885{ 886 mutex_enter(&spa_spare_lock); 887 ASSERT(vd->vdev_isspare); 888 spa_aux_remove(vd, &spa_spare_avl); 889 vd->vdev_isspare = B_FALSE; 890 mutex_exit(&spa_spare_lock); 891} 892 893boolean_t 894spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 895{ 896 boolean_t found; 897 898 mutex_enter(&spa_spare_lock); 899 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 900 mutex_exit(&spa_spare_lock); 901 902 return (found); 903} 904 905void 906spa_spare_activate(vdev_t *vd) 907{ 908 mutex_enter(&spa_spare_lock); 909 ASSERT(vd->vdev_isspare); 910 spa_aux_activate(vd, &spa_spare_avl); 911 mutex_exit(&spa_spare_lock); 912} 913 914/* 915 * Level 2 ARC devices are tracked globally for the same reasons as spares. 916 * Cache devices currently only support one pool per cache device, and so 917 * for these devices the aux reference count is currently unused beyond 1. 918 */ 919 920static int 921spa_l2cache_compare(const void *a, const void *b) 922{ 923 return (spa_aux_compare(a, b)); 924} 925 926void 927spa_l2cache_add(vdev_t *vd) 928{ 929 mutex_enter(&spa_l2cache_lock); 930 ASSERT(!vd->vdev_isl2cache); 931 spa_aux_add(vd, &spa_l2cache_avl); 932 vd->vdev_isl2cache = B_TRUE; 933 mutex_exit(&spa_l2cache_lock); 934} 935 936void 937spa_l2cache_remove(vdev_t *vd) 938{ 939 mutex_enter(&spa_l2cache_lock); 940 ASSERT(vd->vdev_isl2cache); 941 spa_aux_remove(vd, &spa_l2cache_avl); 942 vd->vdev_isl2cache = B_FALSE; 943 mutex_exit(&spa_l2cache_lock); 944} 945 946boolean_t 947spa_l2cache_exists(uint64_t guid, uint64_t *pool) 948{ 949 boolean_t found; 950 951 mutex_enter(&spa_l2cache_lock); 952 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 953 mutex_exit(&spa_l2cache_lock); 954 955 return (found); 956} 957 958void 959spa_l2cache_activate(vdev_t *vd) 960{ 961 mutex_enter(&spa_l2cache_lock); 962 ASSERT(vd->vdev_isl2cache); 963 spa_aux_activate(vd, &spa_l2cache_avl); 964 mutex_exit(&spa_l2cache_lock); 965} 966 967/* 968 * ========================================================================== 969 * SPA vdev locking 970 * ========================================================================== 971 */ 972 973/* 974 * Lock the given spa_t for the purpose of adding or removing a vdev. 975 * Grabs the global spa_namespace_lock plus the spa config lock for writing. 976 * It returns the next transaction group for the spa_t. 977 */ 978uint64_t 979spa_vdev_enter(spa_t *spa) 980{ 981 mutex_enter(&spa->spa_vdev_top_lock); 982 mutex_enter(&spa_namespace_lock); 983 return (spa_vdev_config_enter(spa)); 984} 985 986/* 987 * Internal implementation for spa_vdev_enter(). Used when a vdev 988 * operation requires multiple syncs (i.e. removing a device) while 989 * keeping the spa_namespace_lock held. 990 */ 991uint64_t 992spa_vdev_config_enter(spa_t *spa) 993{ 994 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 995 996 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 997 998 return (spa_last_synced_txg(spa) + 1); 999} 1000 1001/* 1002 * Used in combination with spa_vdev_config_enter() to allow the syncing 1003 * of multiple transactions without releasing the spa_namespace_lock. 1004 */ 1005void 1006spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 1007{ 1008 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1009 1010 int config_changed = B_FALSE; 1011 1012 ASSERT(txg > spa_last_synced_txg(spa)); 1013 1014 spa->spa_pending_vdev = NULL; 1015 1016 /* 1017 * Reassess the DTLs. 1018 */ 1019 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 1020 1021 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 1022 config_changed = B_TRUE; 1023 spa->spa_config_generation++; 1024 } 1025 1026 /* 1027 * Verify the metaslab classes. 1028 */ 1029 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1030 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 1031 1032 spa_config_exit(spa, SCL_ALL, spa); 1033 1034 /* 1035 * Panic the system if the specified tag requires it. This 1036 * is useful for ensuring that configurations are updated 1037 * transactionally. 1038 */ 1039 if (zio_injection_enabled) 1040 zio_handle_panic_injection(spa, tag, 0); 1041 1042 /* 1043 * Note: this txg_wait_synced() is important because it ensures 1044 * that there won't be more than one config change per txg. 1045 * This allows us to use the txg as the generation number. 1046 */ 1047 if (error == 0) 1048 txg_wait_synced(spa->spa_dsl_pool, txg); 1049 1050 if (vd != NULL) { 1051 ASSERT(!vd->vdev_detached || vd->vdev_dtl_smo.smo_object == 0); 1052 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1053 vdev_free(vd); 1054 spa_config_exit(spa, SCL_ALL, spa); 1055 } 1056 1057 /* 1058 * If the config changed, update the config cache. 1059 */ 1060 if (config_changed) 1061 spa_config_sync(spa, B_FALSE, B_TRUE); 1062} 1063 1064/* 1065 * Unlock the spa_t after adding or removing a vdev. Besides undoing the 1066 * locking of spa_vdev_enter(), we also want make sure the transactions have 1067 * synced to disk, and then update the global configuration cache with the new 1068 * information. 1069 */ 1070int 1071spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 1072{ 1073 spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1074 mutex_exit(&spa_namespace_lock); 1075 mutex_exit(&spa->spa_vdev_top_lock); 1076 1077 return (error); 1078} 1079 1080/* 1081 * Lock the given spa_t for the purpose of changing vdev state. 1082 */ 1083void 1084spa_vdev_state_enter(spa_t *spa, int oplocks) 1085{ 1086 int locks = SCL_STATE_ALL | oplocks; 1087 1088 /* 1089 * Root pools may need to read of the underlying devfs filesystem 1090 * when opening up a vdev. Unfortunately if we're holding the 1091 * SCL_ZIO lock it will result in a deadlock when we try to issue 1092 * the read from the root filesystem. Instead we "prefetch" 1093 * the associated vnodes that we need prior to opening the 1094 * underlying devices and cache them so that we can prevent 1095 * any I/O when we are doing the actual open. 1096 */ 1097 if (spa_is_root(spa)) { 1098 int low = locks & ~(SCL_ZIO - 1); 1099 int high = locks & ~low; 1100 1101 spa_config_enter(spa, high, spa, RW_WRITER); 1102 vdev_hold(spa->spa_root_vdev); 1103 spa_config_enter(spa, low, spa, RW_WRITER); 1104 } else { 1105 spa_config_enter(spa, locks, spa, RW_WRITER); 1106 } 1107 spa->spa_vdev_locks = locks; 1108} 1109 1110int 1111spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1112{ 1113 boolean_t config_changed = B_FALSE; 1114 1115 if (vd != NULL || error == 0) 1116 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 1117 0, 0, B_FALSE); 1118 1119 if (vd != NULL) { 1120 vdev_state_dirty(vd->vdev_top); 1121 config_changed = B_TRUE; 1122 spa->spa_config_generation++; 1123 } 1124 1125 if (spa_is_root(spa)) 1126 vdev_rele(spa->spa_root_vdev); 1127 1128 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 1129 spa_config_exit(spa, spa->spa_vdev_locks, spa); 1130 1131 /* 1132 * If anything changed, wait for it to sync. This ensures that, 1133 * from the system administrator's perspective, zpool(1M) commands 1134 * are synchronous. This is important for things like zpool offline: 1135 * when the command completes, you expect no further I/O from ZFS. 1136 */ 1137 if (vd != NULL) 1138 txg_wait_synced(spa->spa_dsl_pool, 0); 1139 1140 /* 1141 * If the config changed, update the config cache. 1142 */ 1143 if (config_changed) { 1144 mutex_enter(&spa_namespace_lock); 1145 spa_config_sync(spa, B_FALSE, B_TRUE); 1146 mutex_exit(&spa_namespace_lock); 1147 } 1148 1149 return (error); 1150} 1151 1152/* 1153 * ========================================================================== 1154 * Miscellaneous functions 1155 * ========================================================================== 1156 */ 1157 1158void 1159spa_activate_mos_feature(spa_t *spa, const char *feature) 1160{ 1161 (void) nvlist_add_boolean(spa->spa_label_features, feature); 1162 vdev_config_dirty(spa->spa_root_vdev); 1163} 1164 1165void 1166spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1167{ 1168 (void) nvlist_remove_all(spa->spa_label_features, feature); 1169 vdev_config_dirty(spa->spa_root_vdev); 1170} 1171 1172/* 1173 * Rename a spa_t. 1174 */ 1175int 1176spa_rename(const char *name, const char *newname) 1177{ 1178 spa_t *spa; 1179 int err; 1180 1181 /* 1182 * Lookup the spa_t and grab the config lock for writing. We need to 1183 * actually open the pool so that we can sync out the necessary labels. 1184 * It's OK to call spa_open() with the namespace lock held because we 1185 * allow recursive calls for other reasons. 1186 */ 1187 mutex_enter(&spa_namespace_lock); 1188 if ((err = spa_open(name, &spa, FTAG)) != 0) { 1189 mutex_exit(&spa_namespace_lock); 1190 return (err); 1191 } 1192 1193 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1194 1195 avl_remove(&spa_namespace_avl, spa); 1196 (void) strlcpy(spa->spa_name, newname, sizeof (spa->spa_name)); 1197 avl_add(&spa_namespace_avl, spa); 1198 1199 /* 1200 * Sync all labels to disk with the new names by marking the root vdev 1201 * dirty and waiting for it to sync. It will pick up the new pool name 1202 * during the sync. 1203 */ 1204 vdev_config_dirty(spa->spa_root_vdev); 1205 1206 spa_config_exit(spa, SCL_ALL, FTAG); 1207 1208 txg_wait_synced(spa->spa_dsl_pool, 0); 1209 1210 /* 1211 * Sync the updated config cache. 1212 */ 1213 spa_config_sync(spa, B_FALSE, B_TRUE); 1214 1215 spa_close(spa, FTAG); 1216 1217 mutex_exit(&spa_namespace_lock); 1218 1219 return (0); 1220} 1221 1222/* 1223 * Return the spa_t associated with given pool_guid, if it exists. If 1224 * device_guid is non-zero, determine whether the pool exists *and* contains 1225 * a device with the specified device_guid. 1226 */ 1227spa_t * 1228spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1229{ 1230 spa_t *spa; 1231 avl_tree_t *t = &spa_namespace_avl; 1232 1233 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1234 1235 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1236 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1237 continue; 1238 if (spa->spa_root_vdev == NULL) 1239 continue; 1240 if (spa_guid(spa) == pool_guid) { 1241 if (device_guid == 0) 1242 break; 1243 1244 if (vdev_lookup_by_guid(spa->spa_root_vdev, 1245 device_guid) != NULL) 1246 break; 1247 1248 /* 1249 * Check any devices we may be in the process of adding. 1250 */ 1251 if (spa->spa_pending_vdev) { 1252 if (vdev_lookup_by_guid(spa->spa_pending_vdev, 1253 device_guid) != NULL) 1254 break; 1255 } 1256 } 1257 } 1258 1259 return (spa); 1260} 1261 1262/* 1263 * Determine whether a pool with the given pool_guid exists. 1264 */ 1265boolean_t 1266spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1267{ 1268 return (spa_by_guid(pool_guid, device_guid) != NULL); 1269} 1270 1271char * 1272spa_strdup(const char *s) 1273{ 1274 size_t len; 1275 char *new; 1276 1277 len = strlen(s); 1278 new = kmem_alloc(len + 1, KM_SLEEP); 1279 bcopy(s, new, len); 1280 new[len] = '\0'; 1281 1282 return (new); 1283} 1284 1285void 1286spa_strfree(char *s) 1287{ 1288 kmem_free(s, strlen(s) + 1); 1289} 1290 1291uint64_t 1292spa_get_random(uint64_t range) 1293{ 1294 uint64_t r; 1295 1296 ASSERT(range != 0); 1297 1298 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1299 1300 return (r % range); 1301} 1302 1303uint64_t 1304spa_generate_guid(spa_t *spa) 1305{ 1306 uint64_t guid = spa_get_random(-1ULL); 1307 1308 if (spa != NULL) { 1309 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 1310 guid = spa_get_random(-1ULL); 1311 } else { 1312 while (guid == 0 || spa_guid_exists(guid, 0)) 1313 guid = spa_get_random(-1ULL); 1314 } 1315 1316 return (guid); 1317} 1318 1319void 1320sprintf_blkptr(char *buf, const blkptr_t *bp) 1321{ 1322 char type[256]; 1323 char *checksum = NULL; 1324 char *compress = NULL; 1325 1326 if (bp != NULL) { 1327 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1328 dmu_object_byteswap_t bswap = 1329 DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1330 (void) snprintf(type, sizeof (type), "bswap %s %s", 1331 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1332 "metadata" : "data", 1333 dmu_ot_byteswap[bswap].ob_name); 1334 } else { 1335 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1336 sizeof (type)); 1337 } 1338 checksum = zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 1339 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1340 } 1341 1342 SPRINTF_BLKPTR(snprintf, ' ', buf, bp, type, checksum, compress); 1343} 1344 1345void 1346spa_freeze(spa_t *spa) 1347{ 1348 uint64_t freeze_txg = 0; 1349 1350 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1351 if (spa->spa_freeze_txg == UINT64_MAX) { 1352 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1353 spa->spa_freeze_txg = freeze_txg; 1354 } 1355 spa_config_exit(spa, SCL_ALL, FTAG); 1356 if (freeze_txg != 0) 1357 txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1358} 1359 1360void 1361zfs_panic_recover(const char *fmt, ...) 1362{ 1363 va_list adx; 1364 1365 va_start(adx, fmt); 1366 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 1367 va_end(adx); 1368} 1369 1370/* 1371 * This is a stripped-down version of strtoull, suitable only for converting 1372 * lowercase hexadecimal numbers that don't overflow. 1373 */ 1374uint64_t 1375zfs_strtonum(const char *str, char **nptr) 1376{ 1377 uint64_t val = 0; 1378 char c; 1379 int digit; 1380 1381 while ((c = *str) != '\0') { 1382 if (c >= '0' && c <= '9') 1383 digit = c - '0'; 1384 else if (c >= 'a' && c <= 'f') 1385 digit = 10 + c - 'a'; 1386 else 1387 break; 1388 1389 val *= 16; 1390 val += digit; 1391 1392 str++; 1393 } 1394 1395 if (nptr) 1396 *nptr = (char *)str; 1397 1398 return (val); 1399} 1400 1401/* 1402 * ========================================================================== 1403 * Accessor functions 1404 * ========================================================================== 1405 */ 1406 1407boolean_t 1408spa_shutting_down(spa_t *spa) 1409{ 1410 return (spa->spa_async_suspended); 1411} 1412 1413dsl_pool_t * 1414spa_get_dsl(spa_t *spa) 1415{ 1416 return (spa->spa_dsl_pool); 1417} 1418 1419boolean_t 1420spa_is_initializing(spa_t *spa) 1421{ 1422 return (spa->spa_is_initializing); 1423} 1424 1425blkptr_t * 1426spa_get_rootblkptr(spa_t *spa) 1427{ 1428 return (&spa->spa_ubsync.ub_rootbp); 1429} 1430 1431void 1432spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1433{ 1434 spa->spa_uberblock.ub_rootbp = *bp; 1435} 1436 1437void 1438spa_altroot(spa_t *spa, char *buf, size_t buflen) 1439{ 1440 if (spa->spa_root == NULL) 1441 buf[0] = '\0'; 1442 else 1443 (void) strncpy(buf, spa->spa_root, buflen); 1444} 1445 1446int 1447spa_sync_pass(spa_t *spa) 1448{ 1449 return (spa->spa_sync_pass); 1450} 1451 1452char * 1453spa_name(spa_t *spa) 1454{ 1455 return (spa->spa_name); 1456} 1457 1458uint64_t 1459spa_guid(spa_t *spa) 1460{ 1461 dsl_pool_t *dp = spa_get_dsl(spa); 1462 uint64_t guid; 1463 1464 /* 1465 * If we fail to parse the config during spa_load(), we can go through 1466 * the error path (which posts an ereport) and end up here with no root 1467 * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1468 * this case. 1469 */ 1470 if (spa->spa_root_vdev == NULL) 1471 return (spa->spa_config_guid); 1472 1473 guid = spa->spa_last_synced_guid != 0 ? 1474 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1475 1476 /* 1477 * Return the most recently synced out guid unless we're 1478 * in syncing context. 1479 */ 1480 if (dp && dsl_pool_sync_context(dp)) 1481 return (spa->spa_root_vdev->vdev_guid); 1482 else 1483 return (guid); 1484} 1485 1486uint64_t 1487spa_load_guid(spa_t *spa) 1488{ 1489 /* 1490 * This is a GUID that exists solely as a reference for the 1491 * purposes of the arc. It is generated at load time, and 1492 * is never written to persistent storage. 1493 */ 1494 return (spa->spa_load_guid); 1495} 1496 1497uint64_t 1498spa_last_synced_txg(spa_t *spa) 1499{ 1500 return (spa->spa_ubsync.ub_txg); 1501} 1502 1503uint64_t 1504spa_first_txg(spa_t *spa) 1505{ 1506 return (spa->spa_first_txg); 1507} 1508 1509uint64_t 1510spa_syncing_txg(spa_t *spa) 1511{ 1512 return (spa->spa_syncing_txg); 1513} 1514 1515pool_state_t 1516spa_state(spa_t *spa) 1517{ 1518 return (spa->spa_state); 1519} 1520 1521spa_load_state_t 1522spa_load_state(spa_t *spa) 1523{ 1524 return (spa->spa_load_state); 1525} 1526 1527uint64_t 1528spa_freeze_txg(spa_t *spa) 1529{ 1530 return (spa->spa_freeze_txg); 1531} 1532 1533/* ARGSUSED */ 1534uint64_t 1535spa_get_asize(spa_t *spa, uint64_t lsize) 1536{ 1537 /* 1538 * The worst case is single-sector max-parity RAID-Z blocks, in which 1539 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 1540 * times the size; so just assume that. Add to this the fact that 1541 * we can have up to 3 DVAs per bp, and one more factor of 2 because 1542 * the block may be dittoed with up to 3 DVAs by ddt_sync(). 1543 */ 1544 return (lsize * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2); 1545} 1546 1547uint64_t 1548spa_get_dspace(spa_t *spa) 1549{ 1550 return (spa->spa_dspace); 1551} 1552 1553void 1554spa_update_dspace(spa_t *spa) 1555{ 1556 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1557 ddt_get_dedup_dspace(spa); 1558} 1559 1560/* 1561 * Return the failure mode that has been set to this pool. The default 1562 * behavior will be to block all I/Os when a complete failure occurs. 1563 */ 1564uint8_t 1565spa_get_failmode(spa_t *spa) 1566{ 1567 return (spa->spa_failmode); 1568} 1569 1570boolean_t 1571spa_suspended(spa_t *spa) 1572{ 1573 return (spa->spa_suspended); 1574} 1575 1576uint64_t 1577spa_version(spa_t *spa) 1578{ 1579 return (spa->spa_ubsync.ub_version); 1580} 1581 1582boolean_t 1583spa_deflate(spa_t *spa) 1584{ 1585 return (spa->spa_deflate); 1586} 1587 1588metaslab_class_t * 1589spa_normal_class(spa_t *spa) 1590{ 1591 return (spa->spa_normal_class); 1592} 1593 1594metaslab_class_t * 1595spa_log_class(spa_t *spa) 1596{ 1597 return (spa->spa_log_class); 1598} 1599 1600int 1601spa_max_replication(spa_t *spa) 1602{ 1603 /* 1604 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 1605 * handle BPs with more than one DVA allocated. Set our max 1606 * replication level accordingly. 1607 */ 1608 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 1609 return (1); 1610 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1611} 1612 1613int 1614spa_prev_software_version(spa_t *spa) 1615{ 1616 return (spa->spa_prev_software_version); 1617} 1618 1619uint64_t 1620spa_deadman_synctime(spa_t *spa) 1621{ 1622 return (spa->spa_deadman_synctime); 1623} 1624 1625uint64_t 1626dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 1627{ 1628 uint64_t asize = DVA_GET_ASIZE(dva); 1629 uint64_t dsize = asize; 1630 1631 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1632 1633 if (asize != 0 && spa->spa_deflate) { 1634 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 1635 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 1636 } 1637 1638 return (dsize); 1639} 1640 1641uint64_t 1642bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 1643{ 1644 uint64_t dsize = 0; 1645 1646 for (int d = 0; d < SPA_DVAS_PER_BP; d++) 1647 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1648 1649 return (dsize); 1650} 1651 1652uint64_t 1653bp_get_dsize(spa_t *spa, const blkptr_t *bp) 1654{ 1655 uint64_t dsize = 0; 1656 1657 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1658 1659 for (int d = 0; d < SPA_DVAS_PER_BP; d++) 1660 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1661 1662 spa_config_exit(spa, SCL_VDEV, FTAG); 1663 1664 return (dsize); 1665} 1666 1667/* 1668 * ========================================================================== 1669 * Initialization and Termination 1670 * ========================================================================== 1671 */ 1672 1673static int 1674spa_name_compare(const void *a1, const void *a2) 1675{ 1676 const spa_t *s1 = a1; 1677 const spa_t *s2 = a2; 1678 int s; 1679 1680 s = strcmp(s1->spa_name, s2->spa_name); 1681 if (s > 0) 1682 return (1); 1683 if (s < 0) 1684 return (-1); 1685 return (0); 1686} 1687 1688int 1689spa_busy(void) 1690{ 1691 return (spa_active_count); 1692} 1693 1694void 1695spa_boot_init() 1696{ 1697 spa_config_load(); 1698} 1699 1700#ifdef _KERNEL 1701EVENTHANDLER_DEFINE(mountroot, spa_boot_init, NULL, 0); 1702#endif 1703 1704void 1705spa_init(int mode) 1706{ 1707 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 1708 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 1709 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 1710 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 1711 1712 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 1713 offsetof(spa_t, spa_avl)); 1714 1715 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 1716 offsetof(spa_aux_t, aux_avl)); 1717 1718 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 1719 offsetof(spa_aux_t, aux_avl)); 1720 1721 spa_mode_global = mode; 1722 1723#ifdef illumos 1724#ifdef _KERNEL 1725 spa_arch_init(); 1726#else 1727 if (spa_mode_global != FREAD && dprintf_find_string("watch")) { 1728 arc_procfd = open("/proc/self/ctl", O_WRONLY); 1729 if (arc_procfd == -1) { 1730 perror("could not enable watchpoints: " 1731 "opening /proc/self/ctl failed: "); 1732 } else { 1733 arc_watch = B_TRUE; 1734 } 1735 } 1736#endif 1737#endif /* illumos */ 1738 refcount_sysinit(); 1739 unique_init(); 1740 space_map_init(); 1741 zio_init(); 1742 dmu_init(); 1743 zil_init(); 1744 vdev_cache_stat_init(); 1745 zfs_prop_init(); 1746 zpool_prop_init(); 1747 zpool_feature_init(); 1748 spa_config_load(); 1749 l2arc_start(); 1750#ifndef illumos 1751#ifdef _KERNEL 1752 zfs_deadman_init(); 1753#endif 1754#endif /* !illumos */ 1755} 1756 1757void 1758spa_fini(void) 1759{ 1760 l2arc_stop(); 1761 1762 spa_evict_all(); 1763 1764 vdev_cache_stat_fini(); 1765 zil_fini(); 1766 dmu_fini(); 1767 zio_fini(); 1768 space_map_fini(); 1769 unique_fini(); 1770 refcount_fini(); 1771 1772 avl_destroy(&spa_namespace_avl); 1773 avl_destroy(&spa_spare_avl); 1774 avl_destroy(&spa_l2cache_avl); 1775 1776 cv_destroy(&spa_namespace_cv); 1777 mutex_destroy(&spa_namespace_lock); 1778 mutex_destroy(&spa_spare_lock); 1779 mutex_destroy(&spa_l2cache_lock); 1780} 1781 1782/* 1783 * Return whether this pool has slogs. No locking needed. 1784 * It's not a problem if the wrong answer is returned as it's only for 1785 * performance and not correctness 1786 */ 1787boolean_t 1788spa_has_slogs(spa_t *spa) 1789{ 1790 return (spa->spa_log_class->mc_rotor != NULL); 1791} 1792 1793spa_log_state_t 1794spa_get_log_state(spa_t *spa) 1795{ 1796 return (spa->spa_log_state); 1797} 1798 1799void 1800spa_set_log_state(spa_t *spa, spa_log_state_t state) 1801{ 1802 spa->spa_log_state = state; 1803} 1804 1805boolean_t 1806spa_is_root(spa_t *spa) 1807{ 1808 return (spa->spa_is_root); 1809} 1810 1811boolean_t 1812spa_writeable(spa_t *spa) 1813{ 1814 return (!!(spa->spa_mode & FWRITE)); 1815} 1816 1817int 1818spa_mode(spa_t *spa) 1819{ 1820 return (spa->spa_mode); 1821} 1822 1823uint64_t 1824spa_bootfs(spa_t *spa) 1825{ 1826 return (spa->spa_bootfs); 1827} 1828 1829uint64_t 1830spa_delegation(spa_t *spa) 1831{ 1832 return (spa->spa_delegation); 1833} 1834 1835objset_t * 1836spa_meta_objset(spa_t *spa) 1837{ 1838 return (spa->spa_meta_objset); 1839} 1840 1841enum zio_checksum 1842spa_dedup_checksum(spa_t *spa) 1843{ 1844 return (spa->spa_dedup_checksum); 1845} 1846 1847/* 1848 * Reset pool scan stat per scan pass (or reboot). 1849 */ 1850void 1851spa_scan_stat_init(spa_t *spa) 1852{ 1853 /* data not stored on disk */ 1854 spa->spa_scan_pass_start = gethrestime_sec(); 1855 spa->spa_scan_pass_exam = 0; 1856 vdev_scan_stat_init(spa->spa_root_vdev); 1857} 1858 1859/* 1860 * Get scan stats for zpool status reports 1861 */ 1862int 1863spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 1864{ 1865 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 1866 1867 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE) 1868 return (SET_ERROR(ENOENT)); 1869 bzero(ps, sizeof (pool_scan_stat_t)); 1870 1871 /* data stored on disk */ 1872 ps->pss_func = scn->scn_phys.scn_func; 1873 ps->pss_start_time = scn->scn_phys.scn_start_time; 1874 ps->pss_end_time = scn->scn_phys.scn_end_time; 1875 ps->pss_to_examine = scn->scn_phys.scn_to_examine; 1876 ps->pss_examined = scn->scn_phys.scn_examined; 1877 ps->pss_to_process = scn->scn_phys.scn_to_process; 1878 ps->pss_processed = scn->scn_phys.scn_processed; 1879 ps->pss_errors = scn->scn_phys.scn_errors; 1880 ps->pss_state = scn->scn_phys.scn_state; 1881 1882 /* data not stored on disk */ 1883 ps->pss_pass_start = spa->spa_scan_pass_start; 1884 ps->pss_pass_exam = spa->spa_scan_pass_exam; 1885 1886 return (0); 1887} 1888 1889boolean_t 1890spa_debug_enabled(spa_t *spa) 1891{ 1892 return (spa->spa_debug); 1893} 1894