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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2011, 2018 by Delphix. All rights reserved. 24 * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 25 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 26 * Copyright 2013 Saso Kiselkov. All rights reserved. 27 * Copyright (c) 2014 Integros [integros.com] 28 * Copyright (c) 2017 Datto Inc. 29 */ 30 31 #include <sys/zfs_context.h> 32 #include <sys/spa_impl.h> 33 #include <sys/spa_boot.h> 34 #include <sys/zio.h> 35 #include <sys/zio_checksum.h> 36 #include <sys/zio_compress.h> 37 #include <sys/dmu.h> 38 #include <sys/dmu_tx.h> 39 #include <sys/zap.h> 40 #include <sys/zil.h> 41 #include <sys/vdev_impl.h> 42 #include <sys/vdev_initialize.h> 43 #include <sys/metaslab.h> 44 #include <sys/uberblock_impl.h> 45 #include <sys/txg.h> 46 #include <sys/avl.h> 47 #include <sys/unique.h> 48 #include <sys/dsl_pool.h> 49 #include <sys/dsl_dir.h> 50 #include <sys/dsl_prop.h> 51 #include <sys/dsl_scan.h> 52 #include <sys/fs/zfs.h> 53 #include <sys/metaslab_impl.h> 54 #include <sys/arc.h> 55 #include <sys/ddt.h> 56 #include "zfs_prop.h" 57 #include <sys/zfeature.h> 58 59 /* 60 * SPA locking 61 * 62 * There are four basic locks for managing spa_t structures: 63 * 64 * spa_namespace_lock (global mutex) 65 * 66 * This lock must be acquired to do any of the following: 67 * 68 * - Lookup a spa_t by name 69 * - Add or remove a spa_t from the namespace 70 * - Increase spa_refcount from non-zero 71 * - Check if spa_refcount is zero 72 * - Rename a spa_t 73 * - add/remove/attach/detach devices 74 * - Held for the duration of create/destroy/import/export 75 * 76 * It does not need to handle recursion. A create or destroy may 77 * reference objects (files or zvols) in other pools, but by 78 * definition they must have an existing reference, and will never need 79 * to lookup a spa_t by name. 80 * 81 * spa_refcount (per-spa refcount_t protected by mutex) 82 * 83 * This reference count keep track of any active users of the spa_t. The 84 * spa_t cannot be destroyed or freed while this is non-zero. Internally, 85 * the refcount is never really 'zero' - opening a pool implicitly keeps 86 * some references in the DMU. Internally we check against spa_minref, but 87 * present the image of a zero/non-zero value to consumers. 88 * 89 * spa_config_lock[] (per-spa array of rwlocks) 90 * 91 * This protects the spa_t from config changes, and must be held in 92 * the following circumstances: 93 * 94 * - RW_READER to perform I/O to the spa 95 * - RW_WRITER to change the vdev config 96 * 97 * The locking order is fairly straightforward: 98 * 99 * spa_namespace_lock -> spa_refcount 100 * 101 * The namespace lock must be acquired to increase the refcount from 0 102 * or to check if it is zero. 103 * 104 * spa_refcount -> spa_config_lock[] 105 * 106 * There must be at least one valid reference on the spa_t to acquire 107 * the config lock. 108 * 109 * spa_namespace_lock -> spa_config_lock[] 110 * 111 * The namespace lock must always be taken before the config lock. 112 * 113 * 114 * The spa_namespace_lock can be acquired directly and is globally visible. 115 * 116 * The namespace is manipulated using the following functions, all of which 117 * require the spa_namespace_lock to be held. 118 * 119 * spa_lookup() Lookup a spa_t by name. 120 * 121 * spa_add() Create a new spa_t in the namespace. 122 * 123 * spa_remove() Remove a spa_t from the namespace. This also 124 * frees up any memory associated with the spa_t. 125 * 126 * spa_next() Returns the next spa_t in the system, or the 127 * first if NULL is passed. 128 * 129 * spa_evict_all() Shutdown and remove all spa_t structures in 130 * the system. 131 * 132 * spa_guid_exists() Determine whether a pool/device guid exists. 133 * 134 * The spa_refcount is manipulated using the following functions: 135 * 136 * spa_open_ref() Adds a reference to the given spa_t. Must be 137 * called with spa_namespace_lock held if the 138 * refcount is currently zero. 139 * 140 * spa_close() Remove a reference from the spa_t. This will 141 * not free the spa_t or remove it from the 142 * namespace. No locking is required. 143 * 144 * spa_refcount_zero() Returns true if the refcount is currently 145 * zero. Must be called with spa_namespace_lock 146 * held. 147 * 148 * The spa_config_lock[] is an array of rwlocks, ordered as follows: 149 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 150 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 151 * 152 * To read the configuration, it suffices to hold one of these locks as reader. 153 * To modify the configuration, you must hold all locks as writer. To modify 154 * vdev state without altering the vdev tree's topology (e.g. online/offline), 155 * you must hold SCL_STATE and SCL_ZIO as writer. 156 * 157 * We use these distinct config locks to avoid recursive lock entry. 158 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 159 * block allocations (SCL_ALLOC), which may require reading space maps 160 * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 161 * 162 * The spa config locks cannot be normal rwlocks because we need the 163 * ability to hand off ownership. For example, SCL_ZIO is acquired 164 * by the issuing thread and later released by an interrupt thread. 165 * They do, however, obey the usual write-wanted semantics to prevent 166 * writer (i.e. system administrator) starvation. 167 * 168 * The lock acquisition rules are as follows: 169 * 170 * SCL_CONFIG 171 * Protects changes to the vdev tree topology, such as vdev 172 * add/remove/attach/detach. Protects the dirty config list 173 * (spa_config_dirty_list) and the set of spares and l2arc devices. 174 * 175 * SCL_STATE 176 * Protects changes to pool state and vdev state, such as vdev 177 * online/offline/fault/degrade/clear. Protects the dirty state list 178 * (spa_state_dirty_list) and global pool state (spa_state). 179 * 180 * SCL_ALLOC 181 * Protects changes to metaslab groups and classes. 182 * Held as reader by metaslab_alloc() and metaslab_claim(). 183 * 184 * SCL_ZIO 185 * Held by bp-level zios (those which have no io_vd upon entry) 186 * to prevent changes to the vdev tree. The bp-level zio implicitly 187 * protects all of its vdev child zios, which do not hold SCL_ZIO. 188 * 189 * SCL_FREE 190 * Protects changes to metaslab groups and classes. 191 * Held as reader by metaslab_free(). SCL_FREE is distinct from 192 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 193 * blocks in zio_done() while another i/o that holds either 194 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 195 * 196 * SCL_VDEV 197 * Held as reader to prevent changes to the vdev tree during trivial 198 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 199 * other locks, and lower than all of them, to ensure that it's safe 200 * to acquire regardless of caller context. 201 * 202 * In addition, the following rules apply: 203 * 204 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 205 * The lock ordering is SCL_CONFIG > spa_props_lock. 206 * 207 * (b) I/O operations on leaf vdevs. For any zio operation that takes 208 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 209 * or zio_write_phys() -- the caller must ensure that the config cannot 210 * cannot change in the interim, and that the vdev cannot be reopened. 211 * SCL_STATE as reader suffices for both. 212 * 213 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 214 * 215 * spa_vdev_enter() Acquire the namespace lock and the config lock 216 * for writing. 217 * 218 * spa_vdev_exit() Release the config lock, wait for all I/O 219 * to complete, sync the updated configs to the 220 * cache, and release the namespace lock. 221 * 222 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 223 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 224 * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 225 */ 226 227 static avl_tree_t spa_namespace_avl; 228 kmutex_t spa_namespace_lock; 229 static kcondvar_t spa_namespace_cv; 230 static int spa_active_count; 231 int spa_max_replication_override = SPA_DVAS_PER_BP; 232 233 static kmutex_t spa_spare_lock; 234 static avl_tree_t spa_spare_avl; 235 static kmutex_t spa_l2cache_lock; 236 static avl_tree_t spa_l2cache_avl; 237 238 kmem_cache_t *spa_buffer_pool; 239 int spa_mode_global; 240 241 #ifdef ZFS_DEBUG 242 /* 243 * Everything except dprintf, spa, and indirect_remap is on by default 244 * in debug builds. 245 */ 246 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_INDIRECT_REMAP); 247 #else 248 int zfs_flags = 0; 249 #endif 250 251 /* 252 * zfs_recover can be set to nonzero to attempt to recover from 253 * otherwise-fatal errors, typically caused by on-disk corruption. When 254 * set, calls to zfs_panic_recover() will turn into warning messages. 255 * This should only be used as a last resort, as it typically results 256 * in leaked space, or worse. 257 */ 258 boolean_t zfs_recover = B_FALSE; 259 260 /* 261 * If destroy encounters an EIO while reading metadata (e.g. indirect 262 * blocks), space referenced by the missing metadata can not be freed. 263 * Normally this causes the background destroy to become "stalled", as 264 * it is unable to make forward progress. While in this stalled state, 265 * all remaining space to free from the error-encountering filesystem is 266 * "temporarily leaked". Set this flag to cause it to ignore the EIO, 267 * permanently leak the space from indirect blocks that can not be read, 268 * and continue to free everything else that it can. 269 * 270 * The default, "stalling" behavior is useful if the storage partially 271 * fails (i.e. some but not all i/os fail), and then later recovers. In 272 * this case, we will be able to continue pool operations while it is 273 * partially failed, and when it recovers, we can continue to free the 274 * space, with no leaks. However, note that this case is actually 275 * fairly rare. 276 * 277 * Typically pools either (a) fail completely (but perhaps temporarily, 278 * e.g. a top-level vdev going offline), or (b) have localized, 279 * permanent errors (e.g. disk returns the wrong data due to bit flip or 280 * firmware bug). In case (a), this setting does not matter because the 281 * pool will be suspended and the sync thread will not be able to make 282 * forward progress regardless. In case (b), because the error is 283 * permanent, the best we can do is leak the minimum amount of space, 284 * which is what setting this flag will do. Therefore, it is reasonable 285 * for this flag to normally be set, but we chose the more conservative 286 * approach of not setting it, so that there is no possibility of 287 * leaking space in the "partial temporary" failure case. 288 */ 289 boolean_t zfs_free_leak_on_eio = B_FALSE; 290 291 /* 292 * Expiration time in milliseconds. This value has two meanings. First it is 293 * used to determine when the spa_deadman() logic should fire. By default the 294 * spa_deadman() will fire if spa_sync() has not completed in 1000 seconds. 295 * Secondly, the value determines if an I/O is considered "hung". Any I/O that 296 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting 297 * in a system panic. 298 */ 299 uint64_t zfs_deadman_synctime_ms = 1000000ULL; 300 301 /* 302 * Check time in milliseconds. This defines the frequency at which we check 303 * for hung I/O. 304 */ 305 uint64_t zfs_deadman_checktime_ms = 5000ULL; 306 307 /* 308 * Override the zfs deadman behavior via /etc/system. By default the 309 * deadman is enabled except on VMware and sparc deployments. 310 */ 311 int zfs_deadman_enabled = -1; 312 313 /* 314 * The worst case is single-sector max-parity RAID-Z blocks, in which 315 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 316 * times the size; so just assume that. Add to this the fact that 317 * we can have up to 3 DVAs per bp, and one more factor of 2 because 318 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together, 319 * the worst case is: 320 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24 321 */ 322 int spa_asize_inflation = 24; 323 324 /* 325 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in 326 * the pool to be consumed. This ensures that we don't run the pool 327 * completely out of space, due to unaccounted changes (e.g. to the MOS). 328 * It also limits the worst-case time to allocate space. If we have 329 * less than this amount of free space, most ZPL operations (e.g. write, 330 * create) will return ENOSPC. 331 * 332 * Certain operations (e.g. file removal, most administrative actions) can 333 * use half the slop space. They will only return ENOSPC if less than half 334 * the slop space is free. Typically, once the pool has less than the slop 335 * space free, the user will use these operations to free up space in the pool. 336 * These are the operations that call dsl_pool_adjustedsize() with the netfree 337 * argument set to TRUE. 338 * 339 * Operations that are almost guaranteed to free up space in the absence of 340 * a pool checkpoint can use up to three quarters of the slop space 341 * (e.g zfs destroy). 342 * 343 * A very restricted set of operations are always permitted, regardless of 344 * the amount of free space. These are the operations that call 345 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net 346 * increase in the amount of space used, it is possible to run the pool 347 * completely out of space, causing it to be permanently read-only. 348 * 349 * Note that on very small pools, the slop space will be larger than 350 * 3.2%, in an effort to have it be at least spa_min_slop (128MB), 351 * but we never allow it to be more than half the pool size. 352 * 353 * See also the comments in zfs_space_check_t. 354 */ 355 int spa_slop_shift = 5; 356 uint64_t spa_min_slop = 128 * 1024 * 1024; 357 358 int spa_allocators = 4; 359 360 /*PRINTFLIKE2*/ 361 void 362 spa_load_failed(spa_t *spa, const char *fmt, ...) 363 { 364 va_list adx; 365 char buf[256]; 366 367 va_start(adx, fmt); 368 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 369 va_end(adx); 370 371 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name, 372 spa->spa_trust_config ? "trusted" : "untrusted", buf); 373 } 374 375 /*PRINTFLIKE2*/ 376 void 377 spa_load_note(spa_t *spa, const char *fmt, ...) 378 { 379 va_list adx; 380 char buf[256]; 381 382 va_start(adx, fmt); 383 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 384 va_end(adx); 385 386 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name, 387 spa->spa_trust_config ? "trusted" : "untrusted", buf); 388 } 389 390 /* 391 * ========================================================================== 392 * SPA config locking 393 * ========================================================================== 394 */ 395 static void 396 spa_config_lock_init(spa_t *spa) 397 { 398 for (int i = 0; i < SCL_LOCKS; i++) { 399 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 400 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 401 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 402 refcount_create_untracked(&scl->scl_count); 403 scl->scl_writer = NULL; 404 scl->scl_write_wanted = 0; 405 } 406 } 407 408 static void 409 spa_config_lock_destroy(spa_t *spa) 410 { 411 for (int i = 0; i < SCL_LOCKS; i++) { 412 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 413 mutex_destroy(&scl->scl_lock); 414 cv_destroy(&scl->scl_cv); 415 refcount_destroy(&scl->scl_count); 416 ASSERT(scl->scl_writer == NULL); 417 ASSERT(scl->scl_write_wanted == 0); 418 } 419 } 420 421 int 422 spa_config_tryenter(spa_t *spa, int locks, void *tag, krw_t rw) 423 { 424 for (int i = 0; i < SCL_LOCKS; i++) { 425 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 426 if (!(locks & (1 << i))) 427 continue; 428 mutex_enter(&scl->scl_lock); 429 if (rw == RW_READER) { 430 if (scl->scl_writer || scl->scl_write_wanted) { 431 mutex_exit(&scl->scl_lock); 432 spa_config_exit(spa, locks & ((1 << i) - 1), 433 tag); 434 return (0); 435 } 436 } else { 437 ASSERT(scl->scl_writer != curthread); 438 if (!refcount_is_zero(&scl->scl_count)) { 439 mutex_exit(&scl->scl_lock); 440 spa_config_exit(spa, locks & ((1 << i) - 1), 441 tag); 442 return (0); 443 } 444 scl->scl_writer = curthread; 445 } 446 (void) refcount_add(&scl->scl_count, tag); 447 mutex_exit(&scl->scl_lock); 448 } 449 return (1); 450 } 451 452 void 453 spa_config_enter(spa_t *spa, int locks, void *tag, krw_t rw) 454 { 455 int wlocks_held = 0; 456 457 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 458 459 for (int i = 0; i < SCL_LOCKS; i++) { 460 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 461 if (scl->scl_writer == curthread) 462 wlocks_held |= (1 << i); 463 if (!(locks & (1 << i))) 464 continue; 465 mutex_enter(&scl->scl_lock); 466 if (rw == RW_READER) { 467 while (scl->scl_writer || scl->scl_write_wanted) { 468 cv_wait(&scl->scl_cv, &scl->scl_lock); 469 } 470 } else { 471 ASSERT(scl->scl_writer != curthread); 472 while (!refcount_is_zero(&scl->scl_count)) { 473 scl->scl_write_wanted++; 474 cv_wait(&scl->scl_cv, &scl->scl_lock); 475 scl->scl_write_wanted--; 476 } 477 scl->scl_writer = curthread; 478 } 479 (void) refcount_add(&scl->scl_count, tag); 480 mutex_exit(&scl->scl_lock); 481 } 482 ASSERT3U(wlocks_held, <=, locks); 483 } 484 485 void 486 spa_config_exit(spa_t *spa, int locks, void *tag) 487 { 488 for (int i = SCL_LOCKS - 1; i >= 0; i--) { 489 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 490 if (!(locks & (1 << i))) 491 continue; 492 mutex_enter(&scl->scl_lock); 493 ASSERT(!refcount_is_zero(&scl->scl_count)); 494 if (refcount_remove(&scl->scl_count, tag) == 0) { 495 ASSERT(scl->scl_writer == NULL || 496 scl->scl_writer == curthread); 497 scl->scl_writer = NULL; /* OK in either case */ 498 cv_broadcast(&scl->scl_cv); 499 } 500 mutex_exit(&scl->scl_lock); 501 } 502 } 503 504 int 505 spa_config_held(spa_t *spa, int locks, krw_t rw) 506 { 507 int locks_held = 0; 508 509 for (int i = 0; i < SCL_LOCKS; i++) { 510 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 511 if (!(locks & (1 << i))) 512 continue; 513 if ((rw == RW_READER && !refcount_is_zero(&scl->scl_count)) || 514 (rw == RW_WRITER && scl->scl_writer == curthread)) 515 locks_held |= 1 << i; 516 } 517 518 return (locks_held); 519 } 520 521 /* 522 * ========================================================================== 523 * SPA namespace functions 524 * ========================================================================== 525 */ 526 527 /* 528 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 529 * Returns NULL if no matching spa_t is found. 530 */ 531 spa_t * 532 spa_lookup(const char *name) 533 { 534 static spa_t search; /* spa_t is large; don't allocate on stack */ 535 spa_t *spa; 536 avl_index_t where; 537 char *cp; 538 539 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 540 541 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 542 543 /* 544 * If it's a full dataset name, figure out the pool name and 545 * just use that. 546 */ 547 cp = strpbrk(search.spa_name, "/@#"); 548 if (cp != NULL) 549 *cp = '\0'; 550 551 spa = avl_find(&spa_namespace_avl, &search, &where); 552 553 return (spa); 554 } 555 556 /* 557 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 558 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 559 * looking for potentially hung I/Os. 560 */ 561 void 562 spa_deadman(void *arg) 563 { 564 spa_t *spa = arg; 565 566 /* 567 * Disable the deadman timer if the pool is suspended. 568 */ 569 if (spa_suspended(spa)) { 570 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY)); 571 return; 572 } 573 574 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 575 (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 576 ++spa->spa_deadman_calls); 577 if (zfs_deadman_enabled) 578 vdev_deadman(spa->spa_root_vdev); 579 } 580 581 /* 582 * Create an uninitialized spa_t with the given name. Requires 583 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 584 * exist by calling spa_lookup() first. 585 */ 586 spa_t * 587 spa_add(const char *name, nvlist_t *config, const char *altroot) 588 { 589 spa_t *spa; 590 spa_config_dirent_t *dp; 591 cyc_handler_t hdlr; 592 cyc_time_t when; 593 594 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 595 596 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 597 598 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 599 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 600 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 601 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL); 602 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 603 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 604 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 605 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL); 606 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 607 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 608 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 609 mutex_init(&spa->spa_iokstat_lock, NULL, MUTEX_DEFAULT, NULL); 610 611 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 612 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL); 613 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 614 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 615 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 616 617 for (int t = 0; t < TXG_SIZE; t++) 618 bplist_create(&spa->spa_free_bplist[t]); 619 620 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 621 spa->spa_state = POOL_STATE_UNINITIALIZED; 622 spa->spa_freeze_txg = UINT64_MAX; 623 spa->spa_final_txg = UINT64_MAX; 624 spa->spa_load_max_txg = UINT64_MAX; 625 spa->spa_proc = &p0; 626 spa->spa_proc_state = SPA_PROC_NONE; 627 spa->spa_trust_config = B_TRUE; 628 629 hdlr.cyh_func = spa_deadman; 630 hdlr.cyh_arg = spa; 631 hdlr.cyh_level = CY_LOW_LEVEL; 632 633 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms); 634 635 /* 636 * This determines how often we need to check for hung I/Os after 637 * the cyclic has already fired. Since checking for hung I/Os is 638 * an expensive operation we don't want to check too frequently. 639 * Instead wait for 5 seconds before checking again. 640 */ 641 when.cyt_interval = MSEC2NSEC(zfs_deadman_checktime_ms); 642 when.cyt_when = CY_INFINITY; 643 mutex_enter(&cpu_lock); 644 spa->spa_deadman_cycid = cyclic_add(&hdlr, &when); 645 mutex_exit(&cpu_lock); 646 647 refcount_create(&spa->spa_refcount); 648 spa_config_lock_init(spa); 649 650 avl_add(&spa_namespace_avl, spa); 651 652 /* 653 * Set the alternate root, if there is one. 654 */ 655 if (altroot) { 656 spa->spa_root = spa_strdup(altroot); 657 spa_active_count++; 658 } 659 660 spa->spa_alloc_count = spa_allocators; 661 spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count * 662 sizeof (kmutex_t), KM_SLEEP); 663 spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count * 664 sizeof (avl_tree_t), KM_SLEEP); 665 for (int i = 0; i < spa->spa_alloc_count; i++) { 666 mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL); 667 avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare, 668 sizeof (zio_t), offsetof(zio_t, io_alloc_node)); 669 } 670 671 /* 672 * Every pool starts with the default cachefile 673 */ 674 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 675 offsetof(spa_config_dirent_t, scd_link)); 676 677 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 678 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 679 list_insert_head(&spa->spa_config_list, dp); 680 681 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 682 KM_SLEEP) == 0); 683 684 if (config != NULL) { 685 nvlist_t *features; 686 687 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 688 &features) == 0) { 689 VERIFY(nvlist_dup(features, &spa->spa_label_features, 690 0) == 0); 691 } 692 693 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 694 } 695 696 if (spa->spa_label_features == NULL) { 697 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 698 KM_SLEEP) == 0); 699 } 700 701 spa->spa_iokstat = kstat_create("zfs", 0, name, 702 "disk", KSTAT_TYPE_IO, 1, 0); 703 if (spa->spa_iokstat) { 704 spa->spa_iokstat->ks_lock = &spa->spa_iokstat_lock; 705 kstat_install(spa->spa_iokstat); 706 } 707 708 spa->spa_min_ashift = INT_MAX; 709 spa->spa_max_ashift = 0; 710 711 /* 712 * As a pool is being created, treat all features as disabled by 713 * setting SPA_FEATURE_DISABLED for all entries in the feature 714 * refcount cache. 715 */ 716 for (int i = 0; i < SPA_FEATURES; i++) { 717 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED; 718 } 719 720 return (spa); 721 } 722 723 /* 724 * Removes a spa_t from the namespace, freeing up any memory used. Requires 725 * spa_namespace_lock. This is called only after the spa_t has been closed and 726 * deactivated. 727 */ 728 void 729 spa_remove(spa_t *spa) 730 { 731 spa_config_dirent_t *dp; 732 733 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 734 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); 735 ASSERT3U(refcount_count(&spa->spa_refcount), ==, 0); 736 737 nvlist_free(spa->spa_config_splitting); 738 739 avl_remove(&spa_namespace_avl, spa); 740 cv_broadcast(&spa_namespace_cv); 741 742 if (spa->spa_root) { 743 spa_strfree(spa->spa_root); 744 spa_active_count--; 745 } 746 747 while ((dp = list_head(&spa->spa_config_list)) != NULL) { 748 list_remove(&spa->spa_config_list, dp); 749 if (dp->scd_path != NULL) 750 spa_strfree(dp->scd_path); 751 kmem_free(dp, sizeof (spa_config_dirent_t)); 752 } 753 754 for (int i = 0; i < spa->spa_alloc_count; i++) { 755 avl_destroy(&spa->spa_alloc_trees[i]); 756 mutex_destroy(&spa->spa_alloc_locks[i]); 757 } 758 kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count * 759 sizeof (kmutex_t)); 760 kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count * 761 sizeof (avl_tree_t)); 762 763 list_destroy(&spa->spa_config_list); 764 765 nvlist_free(spa->spa_label_features); 766 nvlist_free(spa->spa_load_info); 767 spa_config_set(spa, NULL); 768 769 mutex_enter(&cpu_lock); 770 if (spa->spa_deadman_cycid != CYCLIC_NONE) 771 cyclic_remove(spa->spa_deadman_cycid); 772 mutex_exit(&cpu_lock); 773 spa->spa_deadman_cycid = CYCLIC_NONE; 774 775 refcount_destroy(&spa->spa_refcount); 776 777 spa_config_lock_destroy(spa); 778 779 kstat_delete(spa->spa_iokstat); 780 spa->spa_iokstat = NULL; 781 782 for (int t = 0; t < TXG_SIZE; t++) 783 bplist_destroy(&spa->spa_free_bplist[t]); 784 785 zio_checksum_templates_free(spa); 786 787 cv_destroy(&spa->spa_async_cv); 788 cv_destroy(&spa->spa_evicting_os_cv); 789 cv_destroy(&spa->spa_proc_cv); 790 cv_destroy(&spa->spa_scrub_io_cv); 791 cv_destroy(&spa->spa_suspend_cv); 792 793 mutex_destroy(&spa->spa_async_lock); 794 mutex_destroy(&spa->spa_errlist_lock); 795 mutex_destroy(&spa->spa_errlog_lock); 796 mutex_destroy(&spa->spa_evicting_os_lock); 797 mutex_destroy(&spa->spa_history_lock); 798 mutex_destroy(&spa->spa_proc_lock); 799 mutex_destroy(&spa->spa_props_lock); 800 mutex_destroy(&spa->spa_cksum_tmpls_lock); 801 mutex_destroy(&spa->spa_scrub_lock); 802 mutex_destroy(&spa->spa_suspend_lock); 803 mutex_destroy(&spa->spa_vdev_top_lock); 804 mutex_destroy(&spa->spa_iokstat_lock); 805 806 kmem_free(spa, sizeof (spa_t)); 807 } 808 809 /* 810 * Given a pool, return the next pool in the namespace, or NULL if there is 811 * none. If 'prev' is NULL, return the first pool. 812 */ 813 spa_t * 814 spa_next(spa_t *prev) 815 { 816 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 817 818 if (prev) 819 return (AVL_NEXT(&spa_namespace_avl, prev)); 820 else 821 return (avl_first(&spa_namespace_avl)); 822 } 823 824 /* 825 * ========================================================================== 826 * SPA refcount functions 827 * ========================================================================== 828 */ 829 830 /* 831 * Add a reference to the given spa_t. Must have at least one reference, or 832 * have the namespace lock held. 833 */ 834 void 835 spa_open_ref(spa_t *spa, void *tag) 836 { 837 ASSERT(refcount_count(&spa->spa_refcount) >= spa->spa_minref || 838 MUTEX_HELD(&spa_namespace_lock)); 839 (void) refcount_add(&spa->spa_refcount, tag); 840 } 841 842 /* 843 * Remove a reference to the given spa_t. Must have at least one reference, or 844 * have the namespace lock held. 845 */ 846 void 847 spa_close(spa_t *spa, void *tag) 848 { 849 ASSERT(refcount_count(&spa->spa_refcount) > spa->spa_minref || 850 MUTEX_HELD(&spa_namespace_lock)); 851 (void) refcount_remove(&spa->spa_refcount, tag); 852 } 853 854 /* 855 * Remove a reference to the given spa_t held by a dsl dir that is 856 * being asynchronously released. Async releases occur from a taskq 857 * performing eviction of dsl datasets and dirs. The namespace lock 858 * isn't held and the hold by the object being evicted may contribute to 859 * spa_minref (e.g. dataset or directory released during pool export), 860 * so the asserts in spa_close() do not apply. 861 */ 862 void 863 spa_async_close(spa_t *spa, void *tag) 864 { 865 (void) refcount_remove(&spa->spa_refcount, tag); 866 } 867 868 /* 869 * Check to see if the spa refcount is zero. Must be called with 870 * spa_namespace_lock held. We really compare against spa_minref, which is the 871 * number of references acquired when opening a pool 872 */ 873 boolean_t 874 spa_refcount_zero(spa_t *spa) 875 { 876 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 877 878 return (refcount_count(&spa->spa_refcount) == spa->spa_minref); 879 } 880 881 /* 882 * ========================================================================== 883 * SPA spare and l2cache tracking 884 * ========================================================================== 885 */ 886 887 /* 888 * Hot spares and cache devices are tracked using the same code below, 889 * for 'auxiliary' devices. 890 */ 891 892 typedef struct spa_aux { 893 uint64_t aux_guid; 894 uint64_t aux_pool; 895 avl_node_t aux_avl; 896 int aux_count; 897 } spa_aux_t; 898 899 static int 900 spa_aux_compare(const void *a, const void *b) 901 { 902 const spa_aux_t *sa = a; 903 const spa_aux_t *sb = b; 904 905 if (sa->aux_guid < sb->aux_guid) 906 return (-1); 907 else if (sa->aux_guid > sb->aux_guid) 908 return (1); 909 else 910 return (0); 911 } 912 913 void 914 spa_aux_add(vdev_t *vd, avl_tree_t *avl) 915 { 916 avl_index_t where; 917 spa_aux_t search; 918 spa_aux_t *aux; 919 920 search.aux_guid = vd->vdev_guid; 921 if ((aux = avl_find(avl, &search, &where)) != NULL) { 922 aux->aux_count++; 923 } else { 924 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 925 aux->aux_guid = vd->vdev_guid; 926 aux->aux_count = 1; 927 avl_insert(avl, aux, where); 928 } 929 } 930 931 void 932 spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 933 { 934 spa_aux_t search; 935 spa_aux_t *aux; 936 avl_index_t where; 937 938 search.aux_guid = vd->vdev_guid; 939 aux = avl_find(avl, &search, &where); 940 941 ASSERT(aux != NULL); 942 943 if (--aux->aux_count == 0) { 944 avl_remove(avl, aux); 945 kmem_free(aux, sizeof (spa_aux_t)); 946 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 947 aux->aux_pool = 0ULL; 948 } 949 } 950 951 boolean_t 952 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 953 { 954 spa_aux_t search, *found; 955 956 search.aux_guid = guid; 957 found = avl_find(avl, &search, NULL); 958 959 if (pool) { 960 if (found) 961 *pool = found->aux_pool; 962 else 963 *pool = 0ULL; 964 } 965 966 if (refcnt) { 967 if (found) 968 *refcnt = found->aux_count; 969 else 970 *refcnt = 0; 971 } 972 973 return (found != NULL); 974 } 975 976 void 977 spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 978 { 979 spa_aux_t search, *found; 980 avl_index_t where; 981 982 search.aux_guid = vd->vdev_guid; 983 found = avl_find(avl, &search, &where); 984 ASSERT(found != NULL); 985 ASSERT(found->aux_pool == 0ULL); 986 987 found->aux_pool = spa_guid(vd->vdev_spa); 988 } 989 990 /* 991 * Spares are tracked globally due to the following constraints: 992 * 993 * - A spare may be part of multiple pools. 994 * - A spare may be added to a pool even if it's actively in use within 995 * another pool. 996 * - A spare in use in any pool can only be the source of a replacement if 997 * the target is a spare in the same pool. 998 * 999 * We keep track of all spares on the system through the use of a reference 1000 * counted AVL tree. When a vdev is added as a spare, or used as a replacement 1001 * spare, then we bump the reference count in the AVL tree. In addition, we set 1002 * the 'vdev_isspare' member to indicate that the device is a spare (active or 1003 * inactive). When a spare is made active (used to replace a device in the 1004 * pool), we also keep track of which pool its been made a part of. 1005 * 1006 * The 'spa_spare_lock' protects the AVL tree. These functions are normally 1007 * called under the spa_namespace lock as part of vdev reconfiguration. The 1008 * separate spare lock exists for the status query path, which does not need to 1009 * be completely consistent with respect to other vdev configuration changes. 1010 */ 1011 1012 static int 1013 spa_spare_compare(const void *a, const void *b) 1014 { 1015 return (spa_aux_compare(a, b)); 1016 } 1017 1018 void 1019 spa_spare_add(vdev_t *vd) 1020 { 1021 mutex_enter(&spa_spare_lock); 1022 ASSERT(!vd->vdev_isspare); 1023 spa_aux_add(vd, &spa_spare_avl); 1024 vd->vdev_isspare = B_TRUE; 1025 mutex_exit(&spa_spare_lock); 1026 } 1027 1028 void 1029 spa_spare_remove(vdev_t *vd) 1030 { 1031 mutex_enter(&spa_spare_lock); 1032 ASSERT(vd->vdev_isspare); 1033 spa_aux_remove(vd, &spa_spare_avl); 1034 vd->vdev_isspare = B_FALSE; 1035 mutex_exit(&spa_spare_lock); 1036 } 1037 1038 boolean_t 1039 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 1040 { 1041 boolean_t found; 1042 1043 mutex_enter(&spa_spare_lock); 1044 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 1045 mutex_exit(&spa_spare_lock); 1046 1047 return (found); 1048 } 1049 1050 void 1051 spa_spare_activate(vdev_t *vd) 1052 { 1053 mutex_enter(&spa_spare_lock); 1054 ASSERT(vd->vdev_isspare); 1055 spa_aux_activate(vd, &spa_spare_avl); 1056 mutex_exit(&spa_spare_lock); 1057 } 1058 1059 /* 1060 * Level 2 ARC devices are tracked globally for the same reasons as spares. 1061 * Cache devices currently only support one pool per cache device, and so 1062 * for these devices the aux reference count is currently unused beyond 1. 1063 */ 1064 1065 static int 1066 spa_l2cache_compare(const void *a, const void *b) 1067 { 1068 return (spa_aux_compare(a, b)); 1069 } 1070 1071 void 1072 spa_l2cache_add(vdev_t *vd) 1073 { 1074 mutex_enter(&spa_l2cache_lock); 1075 ASSERT(!vd->vdev_isl2cache); 1076 spa_aux_add(vd, &spa_l2cache_avl); 1077 vd->vdev_isl2cache = B_TRUE; 1078 mutex_exit(&spa_l2cache_lock); 1079 } 1080 1081 void 1082 spa_l2cache_remove(vdev_t *vd) 1083 { 1084 mutex_enter(&spa_l2cache_lock); 1085 ASSERT(vd->vdev_isl2cache); 1086 spa_aux_remove(vd, &spa_l2cache_avl); 1087 vd->vdev_isl2cache = B_FALSE; 1088 mutex_exit(&spa_l2cache_lock); 1089 } 1090 1091 boolean_t 1092 spa_l2cache_exists(uint64_t guid, uint64_t *pool) 1093 { 1094 boolean_t found; 1095 1096 mutex_enter(&spa_l2cache_lock); 1097 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 1098 mutex_exit(&spa_l2cache_lock); 1099 1100 return (found); 1101 } 1102 1103 void 1104 spa_l2cache_activate(vdev_t *vd) 1105 { 1106 mutex_enter(&spa_l2cache_lock); 1107 ASSERT(vd->vdev_isl2cache); 1108 spa_aux_activate(vd, &spa_l2cache_avl); 1109 mutex_exit(&spa_l2cache_lock); 1110 } 1111 1112 /* 1113 * ========================================================================== 1114 * SPA vdev locking 1115 * ========================================================================== 1116 */ 1117 1118 /* 1119 * Lock the given spa_t for the purpose of adding or removing a vdev. 1120 * Grabs the global spa_namespace_lock plus the spa config lock for writing. 1121 * It returns the next transaction group for the spa_t. 1122 */ 1123 uint64_t 1124 spa_vdev_enter(spa_t *spa) 1125 { 1126 mutex_enter(&spa->spa_vdev_top_lock); 1127 mutex_enter(&spa_namespace_lock); 1128 return (spa_vdev_config_enter(spa)); 1129 } 1130 1131 /* 1132 * Internal implementation for spa_vdev_enter(). Used when a vdev 1133 * operation requires multiple syncs (i.e. removing a device) while 1134 * keeping the spa_namespace_lock held. 1135 */ 1136 uint64_t 1137 spa_vdev_config_enter(spa_t *spa) 1138 { 1139 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1140 1141 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1142 1143 return (spa_last_synced_txg(spa) + 1); 1144 } 1145 1146 /* 1147 * Used in combination with spa_vdev_config_enter() to allow the syncing 1148 * of multiple transactions without releasing the spa_namespace_lock. 1149 */ 1150 void 1151 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, char *tag) 1152 { 1153 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1154 1155 int config_changed = B_FALSE; 1156 1157 ASSERT(txg > spa_last_synced_txg(spa)); 1158 1159 spa->spa_pending_vdev = NULL; 1160 1161 /* 1162 * Reassess the DTLs. 1163 */ 1164 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE); 1165 1166 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 1167 config_changed = B_TRUE; 1168 spa->spa_config_generation++; 1169 } 1170 1171 /* 1172 * Verify the metaslab classes. 1173 */ 1174 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1175 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 1176 1177 spa_config_exit(spa, SCL_ALL, spa); 1178 1179 /* 1180 * Panic the system if the specified tag requires it. This 1181 * is useful for ensuring that configurations are updated 1182 * transactionally. 1183 */ 1184 if (zio_injection_enabled) 1185 zio_handle_panic_injection(spa, tag, 0); 1186 1187 /* 1188 * Note: this txg_wait_synced() is important because it ensures 1189 * that there won't be more than one config change per txg. 1190 * This allows us to use the txg as the generation number. 1191 */ 1192 if (error == 0) 1193 txg_wait_synced(spa->spa_dsl_pool, txg); 1194 1195 if (vd != NULL) { 1196 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL); 1197 if (vd->vdev_ops->vdev_op_leaf) { 1198 mutex_enter(&vd->vdev_initialize_lock); 1199 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED); 1200 mutex_exit(&vd->vdev_initialize_lock); 1201 } 1202 1203 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1204 vdev_free(vd); 1205 spa_config_exit(spa, SCL_ALL, spa); 1206 } 1207 1208 /* 1209 * If the config changed, update the config cache. 1210 */ 1211 if (config_changed) 1212 spa_write_cachefile(spa, B_FALSE, B_TRUE); 1213 } 1214 1215 /* 1216 * Unlock the spa_t after adding or removing a vdev. Besides undoing the 1217 * locking of spa_vdev_enter(), we also want make sure the transactions have 1218 * synced to disk, and then update the global configuration cache with the new 1219 * information. 1220 */ 1221 int 1222 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 1223 { 1224 spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1225 mutex_exit(&spa_namespace_lock); 1226 mutex_exit(&spa->spa_vdev_top_lock); 1227 1228 return (error); 1229 } 1230 1231 /* 1232 * Lock the given spa_t for the purpose of changing vdev state. 1233 */ 1234 void 1235 spa_vdev_state_enter(spa_t *spa, int oplocks) 1236 { 1237 int locks = SCL_STATE_ALL | oplocks; 1238 1239 /* 1240 * Root pools may need to read of the underlying devfs filesystem 1241 * when opening up a vdev. Unfortunately if we're holding the 1242 * SCL_ZIO lock it will result in a deadlock when we try to issue 1243 * the read from the root filesystem. Instead we "prefetch" 1244 * the associated vnodes that we need prior to opening the 1245 * underlying devices and cache them so that we can prevent 1246 * any I/O when we are doing the actual open. 1247 */ 1248 if (spa_is_root(spa)) { 1249 int low = locks & ~(SCL_ZIO - 1); 1250 int high = locks & ~low; 1251 1252 spa_config_enter(spa, high, spa, RW_WRITER); 1253 vdev_hold(spa->spa_root_vdev); 1254 spa_config_enter(spa, low, spa, RW_WRITER); 1255 } else { 1256 spa_config_enter(spa, locks, spa, RW_WRITER); 1257 } 1258 spa->spa_vdev_locks = locks; 1259 } 1260 1261 int 1262 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1263 { 1264 boolean_t config_changed = B_FALSE; 1265 1266 if (vd != NULL || error == 0) 1267 vdev_dtl_reassess(vd ? vd->vdev_top : spa->spa_root_vdev, 1268 0, 0, B_FALSE); 1269 1270 if (vd != NULL) { 1271 vdev_state_dirty(vd->vdev_top); 1272 config_changed = B_TRUE; 1273 spa->spa_config_generation++; 1274 } 1275 1276 if (spa_is_root(spa)) 1277 vdev_rele(spa->spa_root_vdev); 1278 1279 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 1280 spa_config_exit(spa, spa->spa_vdev_locks, spa); 1281 1282 /* 1283 * If anything changed, wait for it to sync. This ensures that, 1284 * from the system administrator's perspective, zpool(1M) commands 1285 * are synchronous. This is important for things like zpool offline: 1286 * when the command completes, you expect no further I/O from ZFS. 1287 */ 1288 if (vd != NULL) 1289 txg_wait_synced(spa->spa_dsl_pool, 0); 1290 1291 /* 1292 * If the config changed, update the config cache. 1293 */ 1294 if (config_changed) { 1295 mutex_enter(&spa_namespace_lock); 1296 spa_write_cachefile(spa, B_FALSE, B_TRUE); 1297 mutex_exit(&spa_namespace_lock); 1298 } 1299 1300 return (error); 1301 } 1302 1303 /* 1304 * ========================================================================== 1305 * Miscellaneous functions 1306 * ========================================================================== 1307 */ 1308 1309 void 1310 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx) 1311 { 1312 if (!nvlist_exists(spa->spa_label_features, feature)) { 1313 fnvlist_add_boolean(spa->spa_label_features, feature); 1314 /* 1315 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't 1316 * dirty the vdev config because lock SCL_CONFIG is not held. 1317 * Thankfully, in this case we don't need to dirty the config 1318 * because it will be written out anyway when we finish 1319 * creating the pool. 1320 */ 1321 if (tx->tx_txg != TXG_INITIAL) 1322 vdev_config_dirty(spa->spa_root_vdev); 1323 } 1324 } 1325 1326 void 1327 spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1328 { 1329 if (nvlist_remove_all(spa->spa_label_features, feature) == 0) 1330 vdev_config_dirty(spa->spa_root_vdev); 1331 } 1332 1333 /* 1334 * Return the spa_t associated with given pool_guid, if it exists. If 1335 * device_guid is non-zero, determine whether the pool exists *and* contains 1336 * a device with the specified device_guid. 1337 */ 1338 spa_t * 1339 spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1340 { 1341 spa_t *spa; 1342 avl_tree_t *t = &spa_namespace_avl; 1343 1344 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1345 1346 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1347 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1348 continue; 1349 if (spa->spa_root_vdev == NULL) 1350 continue; 1351 if (spa_guid(spa) == pool_guid) { 1352 if (device_guid == 0) 1353 break; 1354 1355 if (vdev_lookup_by_guid(spa->spa_root_vdev, 1356 device_guid) != NULL) 1357 break; 1358 1359 /* 1360 * Check any devices we may be in the process of adding. 1361 */ 1362 if (spa->spa_pending_vdev) { 1363 if (vdev_lookup_by_guid(spa->spa_pending_vdev, 1364 device_guid) != NULL) 1365 break; 1366 } 1367 } 1368 } 1369 1370 return (spa); 1371 } 1372 1373 /* 1374 * Determine whether a pool with the given pool_guid exists. 1375 */ 1376 boolean_t 1377 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1378 { 1379 return (spa_by_guid(pool_guid, device_guid) != NULL); 1380 } 1381 1382 char * 1383 spa_strdup(const char *s) 1384 { 1385 size_t len; 1386 char *new; 1387 1388 len = strlen(s); 1389 new = kmem_alloc(len + 1, KM_SLEEP); 1390 bcopy(s, new, len); 1391 new[len] = '\0'; 1392 1393 return (new); 1394 } 1395 1396 void 1397 spa_strfree(char *s) 1398 { 1399 kmem_free(s, strlen(s) + 1); 1400 } 1401 1402 uint64_t 1403 spa_get_random(uint64_t range) 1404 { 1405 uint64_t r; 1406 1407 ASSERT(range != 0); 1408 1409 (void) random_get_pseudo_bytes((void *)&r, sizeof (uint64_t)); 1410 1411 return (r % range); 1412 } 1413 1414 uint64_t 1415 spa_generate_guid(spa_t *spa) 1416 { 1417 uint64_t guid = spa_get_random(-1ULL); 1418 1419 if (spa != NULL) { 1420 while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)) 1421 guid = spa_get_random(-1ULL); 1422 } else { 1423 while (guid == 0 || spa_guid_exists(guid, 0)) 1424 guid = spa_get_random(-1ULL); 1425 } 1426 1427 return (guid); 1428 } 1429 1430 void 1431 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp) 1432 { 1433 char type[256]; 1434 char *checksum = NULL; 1435 char *compress = NULL; 1436 1437 if (bp != NULL) { 1438 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1439 dmu_object_byteswap_t bswap = 1440 DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1441 (void) snprintf(type, sizeof (type), "bswap %s %s", 1442 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1443 "metadata" : "data", 1444 dmu_ot_byteswap[bswap].ob_name); 1445 } else { 1446 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1447 sizeof (type)); 1448 } 1449 if (!BP_IS_EMBEDDED(bp)) { 1450 checksum = 1451 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 1452 } 1453 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1454 } 1455 1456 SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum, 1457 compress); 1458 } 1459 1460 void 1461 spa_freeze(spa_t *spa) 1462 { 1463 uint64_t freeze_txg = 0; 1464 1465 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1466 if (spa->spa_freeze_txg == UINT64_MAX) { 1467 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1468 spa->spa_freeze_txg = freeze_txg; 1469 } 1470 spa_config_exit(spa, SCL_ALL, FTAG); 1471 if (freeze_txg != 0) 1472 txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1473 } 1474 1475 void 1476 zfs_panic_recover(const char *fmt, ...) 1477 { 1478 va_list adx; 1479 1480 va_start(adx, fmt); 1481 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 1482 va_end(adx); 1483 } 1484 1485 /* 1486 * This is a stripped-down version of strtoull, suitable only for converting 1487 * lowercase hexadecimal numbers that don't overflow. 1488 */ 1489 uint64_t 1490 zfs_strtonum(const char *str, char **nptr) 1491 { 1492 uint64_t val = 0; 1493 char c; 1494 int digit; 1495 1496 while ((c = *str) != '\0') { 1497 if (c >= '0' && c <= '9') 1498 digit = c - '0'; 1499 else if (c >= 'a' && c <= 'f') 1500 digit = 10 + c - 'a'; 1501 else 1502 break; 1503 1504 val *= 16; 1505 val += digit; 1506 1507 str++; 1508 } 1509 1510 if (nptr) 1511 *nptr = (char *)str; 1512 1513 return (val); 1514 } 1515 1516 /* 1517 * ========================================================================== 1518 * Accessor functions 1519 * ========================================================================== 1520 */ 1521 1522 boolean_t 1523 spa_shutting_down(spa_t *spa) 1524 { 1525 return (spa->spa_async_suspended); 1526 } 1527 1528 dsl_pool_t * 1529 spa_get_dsl(spa_t *spa) 1530 { 1531 return (spa->spa_dsl_pool); 1532 } 1533 1534 boolean_t 1535 spa_is_initializing(spa_t *spa) 1536 { 1537 return (spa->spa_is_initializing); 1538 } 1539 1540 boolean_t 1541 spa_indirect_vdevs_loaded(spa_t *spa) 1542 { 1543 return (spa->spa_indirect_vdevs_loaded); 1544 } 1545 1546 blkptr_t * 1547 spa_get_rootblkptr(spa_t *spa) 1548 { 1549 return (&spa->spa_ubsync.ub_rootbp); 1550 } 1551 1552 void 1553 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1554 { 1555 spa->spa_uberblock.ub_rootbp = *bp; 1556 } 1557 1558 void 1559 spa_altroot(spa_t *spa, char *buf, size_t buflen) 1560 { 1561 if (spa->spa_root == NULL) 1562 buf[0] = '\0'; 1563 else 1564 (void) strncpy(buf, spa->spa_root, buflen); 1565 } 1566 1567 int 1568 spa_sync_pass(spa_t *spa) 1569 { 1570 return (spa->spa_sync_pass); 1571 } 1572 1573 char * 1574 spa_name(spa_t *spa) 1575 { 1576 return (spa->spa_name); 1577 } 1578 1579 uint64_t 1580 spa_guid(spa_t *spa) 1581 { 1582 dsl_pool_t *dp = spa_get_dsl(spa); 1583 uint64_t guid; 1584 1585 /* 1586 * If we fail to parse the config during spa_load(), we can go through 1587 * the error path (which posts an ereport) and end up here with no root 1588 * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1589 * this case. 1590 */ 1591 if (spa->spa_root_vdev == NULL) 1592 return (spa->spa_config_guid); 1593 1594 guid = spa->spa_last_synced_guid != 0 ? 1595 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1596 1597 /* 1598 * Return the most recently synced out guid unless we're 1599 * in syncing context. 1600 */ 1601 if (dp && dsl_pool_sync_context(dp)) 1602 return (spa->spa_root_vdev->vdev_guid); 1603 else 1604 return (guid); 1605 } 1606 1607 uint64_t 1608 spa_load_guid(spa_t *spa) 1609 { 1610 /* 1611 * This is a GUID that exists solely as a reference for the 1612 * purposes of the arc. It is generated at load time, and 1613 * is never written to persistent storage. 1614 */ 1615 return (spa->spa_load_guid); 1616 } 1617 1618 uint64_t 1619 spa_last_synced_txg(spa_t *spa) 1620 { 1621 return (spa->spa_ubsync.ub_txg); 1622 } 1623 1624 uint64_t 1625 spa_first_txg(spa_t *spa) 1626 { 1627 return (spa->spa_first_txg); 1628 } 1629 1630 uint64_t 1631 spa_syncing_txg(spa_t *spa) 1632 { 1633 return (spa->spa_syncing_txg); 1634 } 1635 1636 /* 1637 * Return the last txg where data can be dirtied. The final txgs 1638 * will be used to just clear out any deferred frees that remain. 1639 */ 1640 uint64_t 1641 spa_final_dirty_txg(spa_t *spa) 1642 { 1643 return (spa->spa_final_txg - TXG_DEFER_SIZE); 1644 } 1645 1646 pool_state_t 1647 spa_state(spa_t *spa) 1648 { 1649 return (spa->spa_state); 1650 } 1651 1652 spa_load_state_t 1653 spa_load_state(spa_t *spa) 1654 { 1655 return (spa->spa_load_state); 1656 } 1657 1658 uint64_t 1659 spa_freeze_txg(spa_t *spa) 1660 { 1661 return (spa->spa_freeze_txg); 1662 } 1663 1664 /* ARGSUSED */ 1665 uint64_t 1666 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize) 1667 { 1668 return (lsize * spa_asize_inflation); 1669 } 1670 1671 /* 1672 * Return the amount of slop space in bytes. It is 1/32 of the pool (3.2%), 1673 * or at least 128MB, unless that would cause it to be more than half the 1674 * pool size. 1675 * 1676 * See the comment above spa_slop_shift for details. 1677 */ 1678 uint64_t 1679 spa_get_slop_space(spa_t *spa) 1680 { 1681 uint64_t space = spa_get_dspace(spa); 1682 return (MAX(space >> spa_slop_shift, MIN(space >> 1, spa_min_slop))); 1683 } 1684 1685 uint64_t 1686 spa_get_dspace(spa_t *spa) 1687 { 1688 return (spa->spa_dspace); 1689 } 1690 1691 uint64_t 1692 spa_get_checkpoint_space(spa_t *spa) 1693 { 1694 return (spa->spa_checkpoint_info.sci_dspace); 1695 } 1696 1697 void 1698 spa_update_dspace(spa_t *spa) 1699 { 1700 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1701 ddt_get_dedup_dspace(spa); 1702 if (spa->spa_vdev_removal != NULL) { 1703 /* 1704 * We can't allocate from the removing device, so 1705 * subtract its size. This prevents the DMU/DSL from 1706 * filling up the (now smaller) pool while we are in the 1707 * middle of removing the device. 1708 * 1709 * Note that the DMU/DSL doesn't actually know or care 1710 * how much space is allocated (it does its own tracking 1711 * of how much space has been logically used). So it 1712 * doesn't matter that the data we are moving may be 1713 * allocated twice (on the old device and the new 1714 * device). 1715 */ 1716 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1717 vdev_t *vd = 1718 vdev_lookup_top(spa, spa->spa_vdev_removal->svr_vdev_id); 1719 spa->spa_dspace -= spa_deflate(spa) ? 1720 vd->vdev_stat.vs_dspace : vd->vdev_stat.vs_space; 1721 spa_config_exit(spa, SCL_VDEV, FTAG); 1722 } 1723 } 1724 1725 /* 1726 * Return the failure mode that has been set to this pool. The default 1727 * behavior will be to block all I/Os when a complete failure occurs. 1728 */ 1729 uint8_t 1730 spa_get_failmode(spa_t *spa) 1731 { 1732 return (spa->spa_failmode); 1733 } 1734 1735 boolean_t 1736 spa_suspended(spa_t *spa) 1737 { 1738 return (spa->spa_suspended); 1739 } 1740 1741 uint64_t 1742 spa_version(spa_t *spa) 1743 { 1744 return (spa->spa_ubsync.ub_version); 1745 } 1746 1747 boolean_t 1748 spa_deflate(spa_t *spa) 1749 { 1750 return (spa->spa_deflate); 1751 } 1752 1753 metaslab_class_t * 1754 spa_normal_class(spa_t *spa) 1755 { 1756 return (spa->spa_normal_class); 1757 } 1758 1759 metaslab_class_t * 1760 spa_log_class(spa_t *spa) 1761 { 1762 return (spa->spa_log_class); 1763 } 1764 1765 void 1766 spa_evicting_os_register(spa_t *spa, objset_t *os) 1767 { 1768 mutex_enter(&spa->spa_evicting_os_lock); 1769 list_insert_head(&spa->spa_evicting_os_list, os); 1770 mutex_exit(&spa->spa_evicting_os_lock); 1771 } 1772 1773 void 1774 spa_evicting_os_deregister(spa_t *spa, objset_t *os) 1775 { 1776 mutex_enter(&spa->spa_evicting_os_lock); 1777 list_remove(&spa->spa_evicting_os_list, os); 1778 cv_broadcast(&spa->spa_evicting_os_cv); 1779 mutex_exit(&spa->spa_evicting_os_lock); 1780 } 1781 1782 void 1783 spa_evicting_os_wait(spa_t *spa) 1784 { 1785 mutex_enter(&spa->spa_evicting_os_lock); 1786 while (!list_is_empty(&spa->spa_evicting_os_list)) 1787 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock); 1788 mutex_exit(&spa->spa_evicting_os_lock); 1789 1790 dmu_buf_user_evict_wait(); 1791 } 1792 1793 int 1794 spa_max_replication(spa_t *spa) 1795 { 1796 /* 1797 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 1798 * handle BPs with more than one DVA allocated. Set our max 1799 * replication level accordingly. 1800 */ 1801 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 1802 return (1); 1803 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 1804 } 1805 1806 int 1807 spa_prev_software_version(spa_t *spa) 1808 { 1809 return (spa->spa_prev_software_version); 1810 } 1811 1812 uint64_t 1813 spa_deadman_synctime(spa_t *spa) 1814 { 1815 return (spa->spa_deadman_synctime); 1816 } 1817 1818 uint64_t 1819 dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 1820 { 1821 uint64_t asize = DVA_GET_ASIZE(dva); 1822 uint64_t dsize = asize; 1823 1824 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 1825 1826 if (asize != 0 && spa->spa_deflate) { 1827 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 1828 dsize = (asize >> SPA_MINBLOCKSHIFT) * vd->vdev_deflate_ratio; 1829 } 1830 1831 return (dsize); 1832 } 1833 1834 uint64_t 1835 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 1836 { 1837 uint64_t dsize = 0; 1838 1839 for (int d = 0; d < BP_GET_NDVAS(bp); d++) 1840 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1841 1842 return (dsize); 1843 } 1844 1845 uint64_t 1846 bp_get_dsize(spa_t *spa, const blkptr_t *bp) 1847 { 1848 uint64_t dsize = 0; 1849 1850 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 1851 1852 for (int d = 0; d < BP_GET_NDVAS(bp); d++) 1853 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 1854 1855 spa_config_exit(spa, SCL_VDEV, FTAG); 1856 1857 return (dsize); 1858 } 1859 1860 uint64_t 1861 spa_dirty_data(spa_t *spa) 1862 { 1863 return (spa->spa_dsl_pool->dp_dirty_total); 1864 } 1865 1866 /* 1867 * ========================================================================== 1868 * Initialization and Termination 1869 * ========================================================================== 1870 */ 1871 1872 static int 1873 spa_name_compare(const void *a1, const void *a2) 1874 { 1875 const spa_t *s1 = a1; 1876 const spa_t *s2 = a2; 1877 int s; 1878 1879 s = strcmp(s1->spa_name, s2->spa_name); 1880 if (s > 0) 1881 return (1); 1882 if (s < 0) 1883 return (-1); 1884 return (0); 1885 } 1886 1887 int 1888 spa_busy(void) 1889 { 1890 return (spa_active_count); 1891 } 1892 1893 void 1894 spa_boot_init() 1895 { 1896 spa_config_load(); 1897 } 1898 1899 void 1900 spa_init(int mode) 1901 { 1902 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 1903 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 1904 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 1905 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 1906 1907 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 1908 offsetof(spa_t, spa_avl)); 1909 1910 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 1911 offsetof(spa_aux_t, aux_avl)); 1912 1913 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 1914 offsetof(spa_aux_t, aux_avl)); 1915 1916 spa_mode_global = mode; 1917 1918 #ifdef _KERNEL 1919 spa_arch_init(); 1920 #else 1921 if (spa_mode_global != FREAD && dprintf_find_string("watch")) { 1922 arc_procfd = open("/proc/self/ctl", O_WRONLY); 1923 if (arc_procfd == -1) { 1924 perror("could not enable watchpoints: " 1925 "opening /proc/self/ctl failed: "); 1926 } else { 1927 arc_watch = B_TRUE; 1928 } 1929 } 1930 #endif 1931 1932 refcount_init(); 1933 unique_init(); 1934 range_tree_init(); 1935 metaslab_alloc_trace_init(); 1936 zio_init(); 1937 dmu_init(); 1938 zil_init(); 1939 vdev_cache_stat_init(); 1940 zfs_prop_init(); 1941 zpool_prop_init(); 1942 zpool_feature_init(); 1943 spa_config_load(); 1944 l2arc_start(); 1945 } 1946 1947 void 1948 spa_fini(void) 1949 { 1950 l2arc_stop(); 1951 1952 spa_evict_all(); 1953 1954 vdev_cache_stat_fini(); 1955 zil_fini(); 1956 dmu_fini(); 1957 zio_fini(); 1958 metaslab_alloc_trace_fini(); 1959 range_tree_fini(); 1960 unique_fini(); 1961 refcount_fini(); 1962 1963 avl_destroy(&spa_namespace_avl); 1964 avl_destroy(&spa_spare_avl); 1965 avl_destroy(&spa_l2cache_avl); 1966 1967 cv_destroy(&spa_namespace_cv); 1968 mutex_destroy(&spa_namespace_lock); 1969 mutex_destroy(&spa_spare_lock); 1970 mutex_destroy(&spa_l2cache_lock); 1971 } 1972 1973 /* 1974 * Return whether this pool has slogs. No locking needed. 1975 * It's not a problem if the wrong answer is returned as it's only for 1976 * performance and not correctness 1977 */ 1978 boolean_t 1979 spa_has_slogs(spa_t *spa) 1980 { 1981 return (spa->spa_log_class->mc_rotor != NULL); 1982 } 1983 1984 spa_log_state_t 1985 spa_get_log_state(spa_t *spa) 1986 { 1987 return (spa->spa_log_state); 1988 } 1989 1990 void 1991 spa_set_log_state(spa_t *spa, spa_log_state_t state) 1992 { 1993 spa->spa_log_state = state; 1994 } 1995 1996 boolean_t 1997 spa_is_root(spa_t *spa) 1998 { 1999 return (spa->spa_is_root); 2000 } 2001 2002 boolean_t 2003 spa_writeable(spa_t *spa) 2004 { 2005 return (!!(spa->spa_mode & FWRITE) && spa->spa_trust_config); 2006 } 2007 2008 /* 2009 * Returns true if there is a pending sync task in any of the current 2010 * syncing txg, the current quiescing txg, or the current open txg. 2011 */ 2012 boolean_t 2013 spa_has_pending_synctask(spa_t *spa) 2014 { 2015 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) || 2016 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks)); 2017 } 2018 2019 int 2020 spa_mode(spa_t *spa) 2021 { 2022 return (spa->spa_mode); 2023 } 2024 2025 uint64_t 2026 spa_bootfs(spa_t *spa) 2027 { 2028 return (spa->spa_bootfs); 2029 } 2030 2031 uint64_t 2032 spa_delegation(spa_t *spa) 2033 { 2034 return (spa->spa_delegation); 2035 } 2036 2037 objset_t * 2038 spa_meta_objset(spa_t *spa) 2039 { 2040 return (spa->spa_meta_objset); 2041 } 2042 2043 enum zio_checksum 2044 spa_dedup_checksum(spa_t *spa) 2045 { 2046 return (spa->spa_dedup_checksum); 2047 } 2048 2049 /* 2050 * Reset pool scan stat per scan pass (or reboot). 2051 */ 2052 void 2053 spa_scan_stat_init(spa_t *spa) 2054 { 2055 /* data not stored on disk */ 2056 spa->spa_scan_pass_start = gethrestime_sec(); 2057 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan)) 2058 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start; 2059 else 2060 spa->spa_scan_pass_scrub_pause = 0; 2061 spa->spa_scan_pass_scrub_spent_paused = 0; 2062 spa->spa_scan_pass_exam = 0; 2063 vdev_scan_stat_init(spa->spa_root_vdev); 2064 } 2065 2066 /* 2067 * Get scan stats for zpool status reports 2068 */ 2069 int 2070 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 2071 { 2072 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 2073 2074 if (scn == NULL || scn->scn_phys.scn_func == POOL_SCAN_NONE) 2075 return (SET_ERROR(ENOENT)); 2076 bzero(ps, sizeof (pool_scan_stat_t)); 2077 2078 /* data stored on disk */ 2079 ps->pss_func = scn->scn_phys.scn_func; 2080 ps->pss_start_time = scn->scn_phys.scn_start_time; 2081 ps->pss_end_time = scn->scn_phys.scn_end_time; 2082 ps->pss_to_examine = scn->scn_phys.scn_to_examine; 2083 ps->pss_examined = scn->scn_phys.scn_examined; 2084 ps->pss_to_process = scn->scn_phys.scn_to_process; 2085 ps->pss_processed = scn->scn_phys.scn_processed; 2086 ps->pss_errors = scn->scn_phys.scn_errors; 2087 ps->pss_state = scn->scn_phys.scn_state; 2088 2089 /* data not stored on disk */ 2090 ps->pss_pass_start = spa->spa_scan_pass_start; 2091 ps->pss_pass_exam = spa->spa_scan_pass_exam; 2092 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause; 2093 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused; 2094 2095 return (0); 2096 } 2097 2098 int 2099 spa_maxblocksize(spa_t *spa) 2100 { 2101 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) 2102 return (SPA_MAXBLOCKSIZE); 2103 else 2104 return (SPA_OLD_MAXBLOCKSIZE); 2105 } 2106 2107 /* 2108 * Returns the txg that the last device removal completed. No indirect mappings 2109 * have been added since this txg. 2110 */ 2111 uint64_t 2112 spa_get_last_removal_txg(spa_t *spa) 2113 { 2114 uint64_t vdevid; 2115 uint64_t ret = -1ULL; 2116 2117 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2118 /* 2119 * sr_prev_indirect_vdev is only modified while holding all the 2120 * config locks, so it is sufficient to hold SCL_VDEV as reader when 2121 * examining it. 2122 */ 2123 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev; 2124 2125 while (vdevid != -1ULL) { 2126 vdev_t *vd = vdev_lookup_top(spa, vdevid); 2127 vdev_indirect_births_t *vib = vd->vdev_indirect_births; 2128 2129 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 2130 2131 /* 2132 * If the removal did not remap any data, we don't care. 2133 */ 2134 if (vdev_indirect_births_count(vib) != 0) { 2135 ret = vdev_indirect_births_last_entry_txg(vib); 2136 break; 2137 } 2138 2139 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev; 2140 } 2141 spa_config_exit(spa, SCL_VDEV, FTAG); 2142 2143 IMPLY(ret != -1ULL, 2144 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 2145 2146 return (ret); 2147 } 2148 2149 boolean_t 2150 spa_trust_config(spa_t *spa) 2151 { 2152 return (spa->spa_trust_config); 2153 } 2154 2155 uint64_t 2156 spa_missing_tvds_allowed(spa_t *spa) 2157 { 2158 return (spa->spa_missing_tvds_allowed); 2159 } 2160 2161 void 2162 spa_set_missing_tvds(spa_t *spa, uint64_t missing) 2163 { 2164 spa->spa_missing_tvds = missing; 2165 } 2166 2167 boolean_t 2168 spa_top_vdevs_spacemap_addressable(spa_t *spa) 2169 { 2170 vdev_t *rvd = spa->spa_root_vdev; 2171 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 2172 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c])) 2173 return (B_FALSE); 2174 } 2175 return (B_TRUE); 2176 } 2177 2178 boolean_t 2179 spa_has_checkpoint(spa_t *spa) 2180 { 2181 return (spa->spa_checkpoint_txg != 0); 2182 } 2183 2184 boolean_t 2185 spa_importing_readonly_checkpoint(spa_t *spa) 2186 { 2187 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) && 2188 spa->spa_mode == FREAD); 2189 } 2190 2191 uint64_t 2192 spa_min_claim_txg(spa_t *spa) 2193 { 2194 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg; 2195 2196 if (checkpoint_txg != 0) 2197 return (checkpoint_txg + 1); 2198 2199 return (spa->spa_first_txg); 2200 } 2201 2202 /* 2203 * If there is a checkpoint, async destroys may consume more space from 2204 * the pool instead of freeing it. In an attempt to save the pool from 2205 * getting suspended when it is about to run out of space, we stop 2206 * processing async destroys. 2207 */ 2208 boolean_t 2209 spa_suspend_async_destroy(spa_t *spa) 2210 { 2211 dsl_pool_t *dp = spa_get_dsl(spa); 2212 2213 uint64_t unreserved = dsl_pool_unreserved_space(dp, 2214 ZFS_SPACE_CHECK_EXTRA_RESERVED); 2215 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes; 2216 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0; 2217 2218 if (spa_has_checkpoint(spa) && avail == 0) 2219 return (B_TRUE); 2220 2221 return (B_FALSE); 2222 } 2223