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 https://opensource.org/licenses/CDDL-1.0. 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, 2019 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) 2017 Datto Inc. 28 * Copyright (c) 2017, Intel Corporation. 29 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. 30 */ 31 32 #include <sys/zfs_context.h> 33 #include <sys/zfs_chksum.h> 34 #include <sys/spa_impl.h> 35 #include <sys/zio.h> 36 #include <sys/zio_checksum.h> 37 #include <sys/zio_compress.h> 38 #include <sys/dmu.h> 39 #include <sys/dmu_tx.h> 40 #include <sys/zap.h> 41 #include <sys/zil.h> 42 #include <sys/vdev_impl.h> 43 #include <sys/vdev_initialize.h> 44 #include <sys/vdev_trim.h> 45 #include <sys/vdev_file.h> 46 #include <sys/vdev_raidz.h> 47 #include <sys/metaslab.h> 48 #include <sys/uberblock_impl.h> 49 #include <sys/txg.h> 50 #include <sys/avl.h> 51 #include <sys/unique.h> 52 #include <sys/dsl_pool.h> 53 #include <sys/dsl_dir.h> 54 #include <sys/dsl_prop.h> 55 #include <sys/fm/util.h> 56 #include <sys/dsl_scan.h> 57 #include <sys/fs/zfs.h> 58 #include <sys/metaslab_impl.h> 59 #include <sys/arc.h> 60 #include <sys/brt.h> 61 #include <sys/ddt.h> 62 #include <sys/kstat.h> 63 #include "zfs_prop.h" 64 #include <sys/btree.h> 65 #include <sys/zfeature.h> 66 #include <sys/qat.h> 67 #include <sys/zstd/zstd.h> 68 69 /* 70 * SPA locking 71 * 72 * There are three basic locks for managing spa_t structures: 73 * 74 * spa_namespace_lock (global mutex) 75 * 76 * This lock must be acquired to do any of the following: 77 * 78 * - Lookup a spa_t by name 79 * - Add or remove a spa_t from the namespace 80 * - Increase spa_refcount from non-zero 81 * - Check if spa_refcount is zero 82 * - Rename a spa_t 83 * - add/remove/attach/detach devices 84 * - Held for the duration of create/destroy/import/export 85 * 86 * It does not need to handle recursion. A create or destroy may 87 * reference objects (files or zvols) in other pools, but by 88 * definition they must have an existing reference, and will never need 89 * to lookup a spa_t by name. 90 * 91 * spa_refcount (per-spa zfs_refcount_t protected by mutex) 92 * 93 * This reference count keep track of any active users of the spa_t. The 94 * spa_t cannot be destroyed or freed while this is non-zero. Internally, 95 * the refcount is never really 'zero' - opening a pool implicitly keeps 96 * some references in the DMU. Internally we check against spa_minref, but 97 * present the image of a zero/non-zero value to consumers. 98 * 99 * spa_config_lock[] (per-spa array of rwlocks) 100 * 101 * This protects the spa_t from config changes, and must be held in 102 * the following circumstances: 103 * 104 * - RW_READER to perform I/O to the spa 105 * - RW_WRITER to change the vdev config 106 * 107 * The locking order is fairly straightforward: 108 * 109 * spa_namespace_lock -> spa_refcount 110 * 111 * The namespace lock must be acquired to increase the refcount from 0 112 * or to check if it is zero. 113 * 114 * spa_refcount -> spa_config_lock[] 115 * 116 * There must be at least one valid reference on the spa_t to acquire 117 * the config lock. 118 * 119 * spa_namespace_lock -> spa_config_lock[] 120 * 121 * The namespace lock must always be taken before the config lock. 122 * 123 * 124 * The spa_namespace_lock can be acquired directly and is globally visible. 125 * 126 * The namespace is manipulated using the following functions, all of which 127 * require the spa_namespace_lock to be held. 128 * 129 * spa_lookup() Lookup a spa_t by name. 130 * 131 * spa_add() Create a new spa_t in the namespace. 132 * 133 * spa_remove() Remove a spa_t from the namespace. This also 134 * frees up any memory associated with the spa_t. 135 * 136 * spa_next() Returns the next spa_t in the system, or the 137 * first if NULL is passed. 138 * 139 * spa_evict_all() Shutdown and remove all spa_t structures in 140 * the system. 141 * 142 * spa_guid_exists() Determine whether a pool/device guid exists. 143 * 144 * The spa_refcount is manipulated using the following functions: 145 * 146 * spa_open_ref() Adds a reference to the given spa_t. Must be 147 * called with spa_namespace_lock held if the 148 * refcount is currently zero. 149 * 150 * spa_close() Remove a reference from the spa_t. This will 151 * not free the spa_t or remove it from the 152 * namespace. No locking is required. 153 * 154 * spa_refcount_zero() Returns true if the refcount is currently 155 * zero. Must be called with spa_namespace_lock 156 * held. 157 * 158 * The spa_config_lock[] is an array of rwlocks, ordered as follows: 159 * SCL_CONFIG > SCL_STATE > SCL_ALLOC > SCL_ZIO > SCL_FREE > SCL_VDEV. 160 * spa_config_lock[] is manipulated with spa_config_{enter,exit,held}(). 161 * 162 * To read the configuration, it suffices to hold one of these locks as reader. 163 * To modify the configuration, you must hold all locks as writer. To modify 164 * vdev state without altering the vdev tree's topology (e.g. online/offline), 165 * you must hold SCL_STATE and SCL_ZIO as writer. 166 * 167 * We use these distinct config locks to avoid recursive lock entry. 168 * For example, spa_sync() (which holds SCL_CONFIG as reader) induces 169 * block allocations (SCL_ALLOC), which may require reading space maps 170 * from disk (dmu_read() -> zio_read() -> SCL_ZIO). 171 * 172 * The spa config locks cannot be normal rwlocks because we need the 173 * ability to hand off ownership. For example, SCL_ZIO is acquired 174 * by the issuing thread and later released by an interrupt thread. 175 * They do, however, obey the usual write-wanted semantics to prevent 176 * writer (i.e. system administrator) starvation. 177 * 178 * The lock acquisition rules are as follows: 179 * 180 * SCL_CONFIG 181 * Protects changes to the vdev tree topology, such as vdev 182 * add/remove/attach/detach. Protects the dirty config list 183 * (spa_config_dirty_list) and the set of spares and l2arc devices. 184 * 185 * SCL_STATE 186 * Protects changes to pool state and vdev state, such as vdev 187 * online/offline/fault/degrade/clear. Protects the dirty state list 188 * (spa_state_dirty_list) and global pool state (spa_state). 189 * 190 * SCL_ALLOC 191 * Protects changes to metaslab groups and classes. 192 * Held as reader by metaslab_alloc() and metaslab_claim(). 193 * 194 * SCL_ZIO 195 * Held by bp-level zios (those which have no io_vd upon entry) 196 * to prevent changes to the vdev tree. The bp-level zio implicitly 197 * protects all of its vdev child zios, which do not hold SCL_ZIO. 198 * 199 * SCL_FREE 200 * Protects changes to metaslab groups and classes. 201 * Held as reader by metaslab_free(). SCL_FREE is distinct from 202 * SCL_ALLOC, and lower than SCL_ZIO, so that we can safely free 203 * blocks in zio_done() while another i/o that holds either 204 * SCL_ALLOC or SCL_ZIO is waiting for this i/o to complete. 205 * 206 * SCL_VDEV 207 * Held as reader to prevent changes to the vdev tree during trivial 208 * inquiries such as bp_get_dsize(). SCL_VDEV is distinct from the 209 * other locks, and lower than all of them, to ensure that it's safe 210 * to acquire regardless of caller context. 211 * 212 * In addition, the following rules apply: 213 * 214 * (a) spa_props_lock protects pool properties, spa_config and spa_config_list. 215 * The lock ordering is SCL_CONFIG > spa_props_lock. 216 * 217 * (b) I/O operations on leaf vdevs. For any zio operation that takes 218 * an explicit vdev_t argument -- such as zio_ioctl(), zio_read_phys(), 219 * or zio_write_phys() -- the caller must ensure that the config cannot 220 * cannot change in the interim, and that the vdev cannot be reopened. 221 * SCL_STATE as reader suffices for both. 222 * 223 * The vdev configuration is protected by spa_vdev_enter() / spa_vdev_exit(). 224 * 225 * spa_vdev_enter() Acquire the namespace lock and the config lock 226 * for writing. 227 * 228 * spa_vdev_exit() Release the config lock, wait for all I/O 229 * to complete, sync the updated configs to the 230 * cache, and release the namespace lock. 231 * 232 * vdev state is protected by spa_vdev_state_enter() / spa_vdev_state_exit(). 233 * Like spa_vdev_enter/exit, these are convenience wrappers -- the actual 234 * locking is, always, based on spa_namespace_lock and spa_config_lock[]. 235 */ 236 237 static avl_tree_t spa_namespace_avl; 238 kmutex_t spa_namespace_lock; 239 static kcondvar_t spa_namespace_cv; 240 static const int spa_max_replication_override = SPA_DVAS_PER_BP; 241 242 static kmutex_t spa_spare_lock; 243 static avl_tree_t spa_spare_avl; 244 static kmutex_t spa_l2cache_lock; 245 static avl_tree_t spa_l2cache_avl; 246 247 spa_mode_t spa_mode_global = SPA_MODE_UNINIT; 248 249 #ifdef ZFS_DEBUG 250 /* 251 * Everything except dprintf, set_error, spa, and indirect_remap is on 252 * by default in debug builds. 253 */ 254 int zfs_flags = ~(ZFS_DEBUG_DPRINTF | ZFS_DEBUG_SET_ERROR | 255 ZFS_DEBUG_INDIRECT_REMAP); 256 #else 257 int zfs_flags = 0; 258 #endif 259 260 /* 261 * zfs_recover can be set to nonzero to attempt to recover from 262 * otherwise-fatal errors, typically caused by on-disk corruption. When 263 * set, calls to zfs_panic_recover() will turn into warning messages. 264 * This should only be used as a last resort, as it typically results 265 * in leaked space, or worse. 266 */ 267 int zfs_recover = B_FALSE; 268 269 /* 270 * If destroy encounters an EIO while reading metadata (e.g. indirect 271 * blocks), space referenced by the missing metadata can not be freed. 272 * Normally this causes the background destroy to become "stalled", as 273 * it is unable to make forward progress. While in this stalled state, 274 * all remaining space to free from the error-encountering filesystem is 275 * "temporarily leaked". Set this flag to cause it to ignore the EIO, 276 * permanently leak the space from indirect blocks that can not be read, 277 * and continue to free everything else that it can. 278 * 279 * The default, "stalling" behavior is useful if the storage partially 280 * fails (i.e. some but not all i/os fail), and then later recovers. In 281 * this case, we will be able to continue pool operations while it is 282 * partially failed, and when it recovers, we can continue to free the 283 * space, with no leaks. However, note that this case is actually 284 * fairly rare. 285 * 286 * Typically pools either (a) fail completely (but perhaps temporarily, 287 * e.g. a top-level vdev going offline), or (b) have localized, 288 * permanent errors (e.g. disk returns the wrong data due to bit flip or 289 * firmware bug). In case (a), this setting does not matter because the 290 * pool will be suspended and the sync thread will not be able to make 291 * forward progress regardless. In case (b), because the error is 292 * permanent, the best we can do is leak the minimum amount of space, 293 * which is what setting this flag will do. Therefore, it is reasonable 294 * for this flag to normally be set, but we chose the more conservative 295 * approach of not setting it, so that there is no possibility of 296 * leaking space in the "partial temporary" failure case. 297 */ 298 int zfs_free_leak_on_eio = B_FALSE; 299 300 /* 301 * Expiration time in milliseconds. This value has two meanings. First it is 302 * used to determine when the spa_deadman() logic should fire. By default the 303 * spa_deadman() will fire if spa_sync() has not completed in 600 seconds. 304 * Secondly, the value determines if an I/O is considered "hung". Any I/O that 305 * has not completed in zfs_deadman_synctime_ms is considered "hung" resulting 306 * in one of three behaviors controlled by zfs_deadman_failmode. 307 */ 308 uint64_t zfs_deadman_synctime_ms = 600000UL; /* 10 min. */ 309 310 /* 311 * This value controls the maximum amount of time zio_wait() will block for an 312 * outstanding IO. By default this is 300 seconds at which point the "hung" 313 * behavior will be applied as described for zfs_deadman_synctime_ms. 314 */ 315 uint64_t zfs_deadman_ziotime_ms = 300000UL; /* 5 min. */ 316 317 /* 318 * Check time in milliseconds. This defines the frequency at which we check 319 * for hung I/O. 320 */ 321 uint64_t zfs_deadman_checktime_ms = 60000UL; /* 1 min. */ 322 323 /* 324 * By default the deadman is enabled. 325 */ 326 int zfs_deadman_enabled = B_TRUE; 327 328 /* 329 * Controls the behavior of the deadman when it detects a "hung" I/O. 330 * Valid values are zfs_deadman_failmode=<wait|continue|panic>. 331 * 332 * wait - Wait for the "hung" I/O (default) 333 * continue - Attempt to recover from a "hung" I/O 334 * panic - Panic the system 335 */ 336 const char *zfs_deadman_failmode = "wait"; 337 338 /* 339 * The worst case is single-sector max-parity RAID-Z blocks, in which 340 * case the space requirement is exactly (VDEV_RAIDZ_MAXPARITY + 1) 341 * times the size; so just assume that. Add to this the fact that 342 * we can have up to 3 DVAs per bp, and one more factor of 2 because 343 * the block may be dittoed with up to 3 DVAs by ddt_sync(). All together, 344 * the worst case is: 345 * (VDEV_RAIDZ_MAXPARITY + 1) * SPA_DVAS_PER_BP * 2 == 24 346 */ 347 uint_t spa_asize_inflation = 24; 348 349 /* 350 * Normally, we don't allow the last 3.2% (1/(2^spa_slop_shift)) of space in 351 * the pool to be consumed (bounded by spa_max_slop). This ensures that we 352 * don't run the pool completely out of space, due to unaccounted changes (e.g. 353 * to the MOS). It also limits the worst-case time to allocate space. If we 354 * have less than this amount of free space, most ZPL operations (e.g. write, 355 * create) will return ENOSPC. The ZIL metaslabs (spa_embedded_log_class) are 356 * also part of this 3.2% of space which can't be consumed by normal writes; 357 * the slop space "proper" (spa_get_slop_space()) is decreased by the embedded 358 * log space. 359 * 360 * Certain operations (e.g. file removal, most administrative actions) can 361 * use half the slop space. They will only return ENOSPC if less than half 362 * the slop space is free. Typically, once the pool has less than the slop 363 * space free, the user will use these operations to free up space in the pool. 364 * These are the operations that call dsl_pool_adjustedsize() with the netfree 365 * argument set to TRUE. 366 * 367 * Operations that are almost guaranteed to free up space in the absence of 368 * a pool checkpoint can use up to three quarters of the slop space 369 * (e.g zfs destroy). 370 * 371 * A very restricted set of operations are always permitted, regardless of 372 * the amount of free space. These are the operations that call 373 * dsl_sync_task(ZFS_SPACE_CHECK_NONE). If these operations result in a net 374 * increase in the amount of space used, it is possible to run the pool 375 * completely out of space, causing it to be permanently read-only. 376 * 377 * Note that on very small pools, the slop space will be larger than 378 * 3.2%, in an effort to have it be at least spa_min_slop (128MB), 379 * but we never allow it to be more than half the pool size. 380 * 381 * Further, on very large pools, the slop space will be smaller than 382 * 3.2%, to avoid reserving much more space than we actually need; bounded 383 * by spa_max_slop (128GB). 384 * 385 * See also the comments in zfs_space_check_t. 386 */ 387 uint_t spa_slop_shift = 5; 388 static const uint64_t spa_min_slop = 128ULL * 1024 * 1024; 389 static const uint64_t spa_max_slop = 128ULL * 1024 * 1024 * 1024; 390 static const int spa_allocators = 4; 391 392 393 void 394 spa_load_failed(spa_t *spa, const char *fmt, ...) 395 { 396 va_list adx; 397 char buf[256]; 398 399 va_start(adx, fmt); 400 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 401 va_end(adx); 402 403 zfs_dbgmsg("spa_load(%s, config %s): FAILED: %s", spa->spa_name, 404 spa->spa_trust_config ? "trusted" : "untrusted", buf); 405 } 406 407 void 408 spa_load_note(spa_t *spa, const char *fmt, ...) 409 { 410 va_list adx; 411 char buf[256]; 412 413 va_start(adx, fmt); 414 (void) vsnprintf(buf, sizeof (buf), fmt, adx); 415 va_end(adx); 416 417 zfs_dbgmsg("spa_load(%s, config %s): %s", spa->spa_name, 418 spa->spa_trust_config ? "trusted" : "untrusted", buf); 419 } 420 421 /* 422 * By default dedup and user data indirects land in the special class 423 */ 424 static int zfs_ddt_data_is_special = B_TRUE; 425 static int zfs_user_indirect_is_special = B_TRUE; 426 427 /* 428 * The percentage of special class final space reserved for metadata only. 429 * Once we allocate 100 - zfs_special_class_metadata_reserve_pct we only 430 * let metadata into the class. 431 */ 432 static uint_t zfs_special_class_metadata_reserve_pct = 25; 433 434 /* 435 * ========================================================================== 436 * SPA config locking 437 * ========================================================================== 438 */ 439 static void 440 spa_config_lock_init(spa_t *spa) 441 { 442 for (int i = 0; i < SCL_LOCKS; i++) { 443 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 444 mutex_init(&scl->scl_lock, NULL, MUTEX_DEFAULT, NULL); 445 cv_init(&scl->scl_cv, NULL, CV_DEFAULT, NULL); 446 scl->scl_writer = NULL; 447 scl->scl_write_wanted = 0; 448 scl->scl_count = 0; 449 } 450 } 451 452 static void 453 spa_config_lock_destroy(spa_t *spa) 454 { 455 for (int i = 0; i < SCL_LOCKS; i++) { 456 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 457 mutex_destroy(&scl->scl_lock); 458 cv_destroy(&scl->scl_cv); 459 ASSERT(scl->scl_writer == NULL); 460 ASSERT(scl->scl_write_wanted == 0); 461 ASSERT(scl->scl_count == 0); 462 } 463 } 464 465 int 466 spa_config_tryenter(spa_t *spa, int locks, const void *tag, krw_t rw) 467 { 468 for (int i = 0; i < SCL_LOCKS; i++) { 469 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 470 if (!(locks & (1 << i))) 471 continue; 472 mutex_enter(&scl->scl_lock); 473 if (rw == RW_READER) { 474 if (scl->scl_writer || scl->scl_write_wanted) { 475 mutex_exit(&scl->scl_lock); 476 spa_config_exit(spa, locks & ((1 << i) - 1), 477 tag); 478 return (0); 479 } 480 } else { 481 ASSERT(scl->scl_writer != curthread); 482 if (scl->scl_count != 0) { 483 mutex_exit(&scl->scl_lock); 484 spa_config_exit(spa, locks & ((1 << i) - 1), 485 tag); 486 return (0); 487 } 488 scl->scl_writer = curthread; 489 } 490 scl->scl_count++; 491 mutex_exit(&scl->scl_lock); 492 } 493 return (1); 494 } 495 496 static void 497 spa_config_enter_impl(spa_t *spa, int locks, const void *tag, krw_t rw, 498 int mmp_flag) 499 { 500 (void) tag; 501 int wlocks_held = 0; 502 503 ASSERT3U(SCL_LOCKS, <, sizeof (wlocks_held) * NBBY); 504 505 for (int i = 0; i < SCL_LOCKS; i++) { 506 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 507 if (scl->scl_writer == curthread) 508 wlocks_held |= (1 << i); 509 if (!(locks & (1 << i))) 510 continue; 511 mutex_enter(&scl->scl_lock); 512 if (rw == RW_READER) { 513 while (scl->scl_writer || 514 (!mmp_flag && scl->scl_write_wanted)) { 515 cv_wait(&scl->scl_cv, &scl->scl_lock); 516 } 517 } else { 518 ASSERT(scl->scl_writer != curthread); 519 while (scl->scl_count != 0) { 520 scl->scl_write_wanted++; 521 cv_wait(&scl->scl_cv, &scl->scl_lock); 522 scl->scl_write_wanted--; 523 } 524 scl->scl_writer = curthread; 525 } 526 scl->scl_count++; 527 mutex_exit(&scl->scl_lock); 528 } 529 ASSERT3U(wlocks_held, <=, locks); 530 } 531 532 void 533 spa_config_enter(spa_t *spa, int locks, const void *tag, krw_t rw) 534 { 535 spa_config_enter_impl(spa, locks, tag, rw, 0); 536 } 537 538 /* 539 * The spa_config_enter_mmp() allows the mmp thread to cut in front of 540 * outstanding write lock requests. This is needed since the mmp updates are 541 * time sensitive and failure to service them promptly will result in a 542 * suspended pool. This pool suspension has been seen in practice when there is 543 * a single disk in a pool that is responding slowly and presumably about to 544 * fail. 545 */ 546 547 void 548 spa_config_enter_mmp(spa_t *spa, int locks, const void *tag, krw_t rw) 549 { 550 spa_config_enter_impl(spa, locks, tag, rw, 1); 551 } 552 553 void 554 spa_config_exit(spa_t *spa, int locks, const void *tag) 555 { 556 (void) tag; 557 for (int i = SCL_LOCKS - 1; i >= 0; i--) { 558 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 559 if (!(locks & (1 << i))) 560 continue; 561 mutex_enter(&scl->scl_lock); 562 ASSERT(scl->scl_count > 0); 563 if (--scl->scl_count == 0) { 564 ASSERT(scl->scl_writer == NULL || 565 scl->scl_writer == curthread); 566 scl->scl_writer = NULL; /* OK in either case */ 567 cv_broadcast(&scl->scl_cv); 568 } 569 mutex_exit(&scl->scl_lock); 570 } 571 } 572 573 int 574 spa_config_held(spa_t *spa, int locks, krw_t rw) 575 { 576 int locks_held = 0; 577 578 for (int i = 0; i < SCL_LOCKS; i++) { 579 spa_config_lock_t *scl = &spa->spa_config_lock[i]; 580 if (!(locks & (1 << i))) 581 continue; 582 if ((rw == RW_READER && scl->scl_count != 0) || 583 (rw == RW_WRITER && scl->scl_writer == curthread)) 584 locks_held |= 1 << i; 585 } 586 587 return (locks_held); 588 } 589 590 /* 591 * ========================================================================== 592 * SPA namespace functions 593 * ========================================================================== 594 */ 595 596 /* 597 * Lookup the named spa_t in the AVL tree. The spa_namespace_lock must be held. 598 * Returns NULL if no matching spa_t is found. 599 */ 600 spa_t * 601 spa_lookup(const char *name) 602 { 603 static spa_t search; /* spa_t is large; don't allocate on stack */ 604 spa_t *spa; 605 avl_index_t where; 606 char *cp; 607 608 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 609 610 (void) strlcpy(search.spa_name, name, sizeof (search.spa_name)); 611 612 /* 613 * If it's a full dataset name, figure out the pool name and 614 * just use that. 615 */ 616 cp = strpbrk(search.spa_name, "/@#"); 617 if (cp != NULL) 618 *cp = '\0'; 619 620 spa = avl_find(&spa_namespace_avl, &search, &where); 621 622 return (spa); 623 } 624 625 /* 626 * Fires when spa_sync has not completed within zfs_deadman_synctime_ms. 627 * If the zfs_deadman_enabled flag is set then it inspects all vdev queues 628 * looking for potentially hung I/Os. 629 */ 630 void 631 spa_deadman(void *arg) 632 { 633 spa_t *spa = arg; 634 635 /* Disable the deadman if the pool is suspended. */ 636 if (spa_suspended(spa)) 637 return; 638 639 zfs_dbgmsg("slow spa_sync: started %llu seconds ago, calls %llu", 640 (gethrtime() - spa->spa_sync_starttime) / NANOSEC, 641 (u_longlong_t)++spa->spa_deadman_calls); 642 if (zfs_deadman_enabled) 643 vdev_deadman(spa->spa_root_vdev, FTAG); 644 645 spa->spa_deadman_tqid = taskq_dispatch_delay(system_delay_taskq, 646 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + 647 MSEC_TO_TICK(zfs_deadman_checktime_ms)); 648 } 649 650 static int 651 spa_log_sm_sort_by_txg(const void *va, const void *vb) 652 { 653 const spa_log_sm_t *a = va; 654 const spa_log_sm_t *b = vb; 655 656 return (TREE_CMP(a->sls_txg, b->sls_txg)); 657 } 658 659 /* 660 * Create an uninitialized spa_t with the given name. Requires 661 * spa_namespace_lock. The caller must ensure that the spa_t doesn't already 662 * exist by calling spa_lookup() first. 663 */ 664 spa_t * 665 spa_add(const char *name, nvlist_t *config, const char *altroot) 666 { 667 spa_t *spa; 668 spa_config_dirent_t *dp; 669 670 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 671 672 spa = kmem_zalloc(sizeof (spa_t), KM_SLEEP); 673 674 mutex_init(&spa->spa_async_lock, NULL, MUTEX_DEFAULT, NULL); 675 mutex_init(&spa->spa_errlist_lock, NULL, MUTEX_DEFAULT, NULL); 676 mutex_init(&spa->spa_errlog_lock, NULL, MUTEX_DEFAULT, NULL); 677 mutex_init(&spa->spa_evicting_os_lock, NULL, MUTEX_DEFAULT, NULL); 678 mutex_init(&spa->spa_history_lock, NULL, MUTEX_DEFAULT, NULL); 679 mutex_init(&spa->spa_proc_lock, NULL, MUTEX_DEFAULT, NULL); 680 mutex_init(&spa->spa_props_lock, NULL, MUTEX_DEFAULT, NULL); 681 mutex_init(&spa->spa_cksum_tmpls_lock, NULL, MUTEX_DEFAULT, NULL); 682 mutex_init(&spa->spa_scrub_lock, NULL, MUTEX_DEFAULT, NULL); 683 mutex_init(&spa->spa_suspend_lock, NULL, MUTEX_DEFAULT, NULL); 684 mutex_init(&spa->spa_vdev_top_lock, NULL, MUTEX_DEFAULT, NULL); 685 mutex_init(&spa->spa_feat_stats_lock, NULL, MUTEX_DEFAULT, NULL); 686 mutex_init(&spa->spa_flushed_ms_lock, NULL, MUTEX_DEFAULT, NULL); 687 mutex_init(&spa->spa_activities_lock, NULL, MUTEX_DEFAULT, NULL); 688 689 cv_init(&spa->spa_async_cv, NULL, CV_DEFAULT, NULL); 690 cv_init(&spa->spa_evicting_os_cv, NULL, CV_DEFAULT, NULL); 691 cv_init(&spa->spa_proc_cv, NULL, CV_DEFAULT, NULL); 692 cv_init(&spa->spa_scrub_io_cv, NULL, CV_DEFAULT, NULL); 693 cv_init(&spa->spa_suspend_cv, NULL, CV_DEFAULT, NULL); 694 cv_init(&spa->spa_activities_cv, NULL, CV_DEFAULT, NULL); 695 cv_init(&spa->spa_waiters_cv, NULL, CV_DEFAULT, NULL); 696 697 for (int t = 0; t < TXG_SIZE; t++) 698 bplist_create(&spa->spa_free_bplist[t]); 699 700 (void) strlcpy(spa->spa_name, name, sizeof (spa->spa_name)); 701 spa->spa_state = POOL_STATE_UNINITIALIZED; 702 spa->spa_freeze_txg = UINT64_MAX; 703 spa->spa_final_txg = UINT64_MAX; 704 spa->spa_load_max_txg = UINT64_MAX; 705 spa->spa_proc = &p0; 706 spa->spa_proc_state = SPA_PROC_NONE; 707 spa->spa_trust_config = B_TRUE; 708 spa->spa_hostid = zone_get_hostid(NULL); 709 710 spa->spa_deadman_synctime = MSEC2NSEC(zfs_deadman_synctime_ms); 711 spa->spa_deadman_ziotime = MSEC2NSEC(zfs_deadman_ziotime_ms); 712 spa_set_deadman_failmode(spa, zfs_deadman_failmode); 713 714 zfs_refcount_create(&spa->spa_refcount); 715 spa_config_lock_init(spa); 716 spa_stats_init(spa); 717 718 avl_add(&spa_namespace_avl, spa); 719 720 /* 721 * Set the alternate root, if there is one. 722 */ 723 if (altroot) 724 spa->spa_root = spa_strdup(altroot); 725 726 spa->spa_alloc_count = spa_allocators; 727 spa->spa_allocs = kmem_zalloc(spa->spa_alloc_count * 728 sizeof (spa_alloc_t), KM_SLEEP); 729 for (int i = 0; i < spa->spa_alloc_count; i++) { 730 mutex_init(&spa->spa_allocs[i].spaa_lock, NULL, MUTEX_DEFAULT, 731 NULL); 732 avl_create(&spa->spa_allocs[i].spaa_tree, zio_bookmark_compare, 733 sizeof (zio_t), offsetof(zio_t, io_alloc_node)); 734 } 735 avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed, 736 sizeof (metaslab_t), offsetof(metaslab_t, ms_spa_txg_node)); 737 avl_create(&spa->spa_sm_logs_by_txg, spa_log_sm_sort_by_txg, 738 sizeof (spa_log_sm_t), offsetof(spa_log_sm_t, sls_node)); 739 list_create(&spa->spa_log_summary, sizeof (log_summary_entry_t), 740 offsetof(log_summary_entry_t, lse_node)); 741 742 /* 743 * Every pool starts with the default cachefile 744 */ 745 list_create(&spa->spa_config_list, sizeof (spa_config_dirent_t), 746 offsetof(spa_config_dirent_t, scd_link)); 747 748 dp = kmem_zalloc(sizeof (spa_config_dirent_t), KM_SLEEP); 749 dp->scd_path = altroot ? NULL : spa_strdup(spa_config_path); 750 list_insert_head(&spa->spa_config_list, dp); 751 752 VERIFY(nvlist_alloc(&spa->spa_load_info, NV_UNIQUE_NAME, 753 KM_SLEEP) == 0); 754 755 if (config != NULL) { 756 nvlist_t *features; 757 758 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_FEATURES_FOR_READ, 759 &features) == 0) { 760 VERIFY(nvlist_dup(features, &spa->spa_label_features, 761 0) == 0); 762 } 763 764 VERIFY(nvlist_dup(config, &spa->spa_config, 0) == 0); 765 } 766 767 if (spa->spa_label_features == NULL) { 768 VERIFY(nvlist_alloc(&spa->spa_label_features, NV_UNIQUE_NAME, 769 KM_SLEEP) == 0); 770 } 771 772 spa->spa_min_ashift = INT_MAX; 773 spa->spa_max_ashift = 0; 774 spa->spa_min_alloc = INT_MAX; 775 776 /* Reset cached value */ 777 spa->spa_dedup_dspace = ~0ULL; 778 779 /* 780 * As a pool is being created, treat all features as disabled by 781 * setting SPA_FEATURE_DISABLED for all entries in the feature 782 * refcount cache. 783 */ 784 for (int i = 0; i < SPA_FEATURES; i++) { 785 spa->spa_feat_refcount_cache[i] = SPA_FEATURE_DISABLED; 786 } 787 788 list_create(&spa->spa_leaf_list, sizeof (vdev_t), 789 offsetof(vdev_t, vdev_leaf_node)); 790 791 return (spa); 792 } 793 794 /* 795 * Removes a spa_t from the namespace, freeing up any memory used. Requires 796 * spa_namespace_lock. This is called only after the spa_t has been closed and 797 * deactivated. 798 */ 799 void 800 spa_remove(spa_t *spa) 801 { 802 spa_config_dirent_t *dp; 803 804 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 805 ASSERT(spa_state(spa) == POOL_STATE_UNINITIALIZED); 806 ASSERT3U(zfs_refcount_count(&spa->spa_refcount), ==, 0); 807 ASSERT0(spa->spa_waiters); 808 809 nvlist_free(spa->spa_config_splitting); 810 811 avl_remove(&spa_namespace_avl, spa); 812 cv_broadcast(&spa_namespace_cv); 813 814 if (spa->spa_root) 815 spa_strfree(spa->spa_root); 816 817 while ((dp = list_remove_head(&spa->spa_config_list)) != NULL) { 818 if (dp->scd_path != NULL) 819 spa_strfree(dp->scd_path); 820 kmem_free(dp, sizeof (spa_config_dirent_t)); 821 } 822 823 for (int i = 0; i < spa->spa_alloc_count; i++) { 824 avl_destroy(&spa->spa_allocs[i].spaa_tree); 825 mutex_destroy(&spa->spa_allocs[i].spaa_lock); 826 } 827 kmem_free(spa->spa_allocs, spa->spa_alloc_count * 828 sizeof (spa_alloc_t)); 829 830 avl_destroy(&spa->spa_metaslabs_by_flushed); 831 avl_destroy(&spa->spa_sm_logs_by_txg); 832 list_destroy(&spa->spa_log_summary); 833 list_destroy(&spa->spa_config_list); 834 list_destroy(&spa->spa_leaf_list); 835 836 nvlist_free(spa->spa_label_features); 837 nvlist_free(spa->spa_load_info); 838 nvlist_free(spa->spa_feat_stats); 839 spa_config_set(spa, NULL); 840 841 zfs_refcount_destroy(&spa->spa_refcount); 842 843 spa_stats_destroy(spa); 844 spa_config_lock_destroy(spa); 845 846 for (int t = 0; t < TXG_SIZE; t++) 847 bplist_destroy(&spa->spa_free_bplist[t]); 848 849 zio_checksum_templates_free(spa); 850 851 cv_destroy(&spa->spa_async_cv); 852 cv_destroy(&spa->spa_evicting_os_cv); 853 cv_destroy(&spa->spa_proc_cv); 854 cv_destroy(&spa->spa_scrub_io_cv); 855 cv_destroy(&spa->spa_suspend_cv); 856 cv_destroy(&spa->spa_activities_cv); 857 cv_destroy(&spa->spa_waiters_cv); 858 859 mutex_destroy(&spa->spa_flushed_ms_lock); 860 mutex_destroy(&spa->spa_async_lock); 861 mutex_destroy(&spa->spa_errlist_lock); 862 mutex_destroy(&spa->spa_errlog_lock); 863 mutex_destroy(&spa->spa_evicting_os_lock); 864 mutex_destroy(&spa->spa_history_lock); 865 mutex_destroy(&spa->spa_proc_lock); 866 mutex_destroy(&spa->spa_props_lock); 867 mutex_destroy(&spa->spa_cksum_tmpls_lock); 868 mutex_destroy(&spa->spa_scrub_lock); 869 mutex_destroy(&spa->spa_suspend_lock); 870 mutex_destroy(&spa->spa_vdev_top_lock); 871 mutex_destroy(&spa->spa_feat_stats_lock); 872 mutex_destroy(&spa->spa_activities_lock); 873 874 kmem_free(spa, sizeof (spa_t)); 875 } 876 877 /* 878 * Given a pool, return the next pool in the namespace, or NULL if there is 879 * none. If 'prev' is NULL, return the first pool. 880 */ 881 spa_t * 882 spa_next(spa_t *prev) 883 { 884 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 885 886 if (prev) 887 return (AVL_NEXT(&spa_namespace_avl, prev)); 888 else 889 return (avl_first(&spa_namespace_avl)); 890 } 891 892 /* 893 * ========================================================================== 894 * SPA refcount functions 895 * ========================================================================== 896 */ 897 898 /* 899 * Add a reference to the given spa_t. Must have at least one reference, or 900 * have the namespace lock held. 901 */ 902 void 903 spa_open_ref(spa_t *spa, const void *tag) 904 { 905 ASSERT(zfs_refcount_count(&spa->spa_refcount) >= spa->spa_minref || 906 MUTEX_HELD(&spa_namespace_lock)); 907 (void) zfs_refcount_add(&spa->spa_refcount, tag); 908 } 909 910 /* 911 * Remove a reference to the given spa_t. Must have at least one reference, or 912 * have the namespace lock held. 913 */ 914 void 915 spa_close(spa_t *spa, const void *tag) 916 { 917 ASSERT(zfs_refcount_count(&spa->spa_refcount) > spa->spa_minref || 918 MUTEX_HELD(&spa_namespace_lock)); 919 (void) zfs_refcount_remove(&spa->spa_refcount, tag); 920 } 921 922 /* 923 * Remove a reference to the given spa_t held by a dsl dir that is 924 * being asynchronously released. Async releases occur from a taskq 925 * performing eviction of dsl datasets and dirs. The namespace lock 926 * isn't held and the hold by the object being evicted may contribute to 927 * spa_minref (e.g. dataset or directory released during pool export), 928 * so the asserts in spa_close() do not apply. 929 */ 930 void 931 spa_async_close(spa_t *spa, const void *tag) 932 { 933 (void) zfs_refcount_remove(&spa->spa_refcount, tag); 934 } 935 936 /* 937 * Check to see if the spa refcount is zero. Must be called with 938 * spa_namespace_lock held. We really compare against spa_minref, which is the 939 * number of references acquired when opening a pool 940 */ 941 boolean_t 942 spa_refcount_zero(spa_t *spa) 943 { 944 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 945 946 return (zfs_refcount_count(&spa->spa_refcount) == spa->spa_minref); 947 } 948 949 /* 950 * ========================================================================== 951 * SPA spare and l2cache tracking 952 * ========================================================================== 953 */ 954 955 /* 956 * Hot spares and cache devices are tracked using the same code below, 957 * for 'auxiliary' devices. 958 */ 959 960 typedef struct spa_aux { 961 uint64_t aux_guid; 962 uint64_t aux_pool; 963 avl_node_t aux_avl; 964 int aux_count; 965 } spa_aux_t; 966 967 static inline int 968 spa_aux_compare(const void *a, const void *b) 969 { 970 const spa_aux_t *sa = (const spa_aux_t *)a; 971 const spa_aux_t *sb = (const spa_aux_t *)b; 972 973 return (TREE_CMP(sa->aux_guid, sb->aux_guid)); 974 } 975 976 static void 977 spa_aux_add(vdev_t *vd, avl_tree_t *avl) 978 { 979 avl_index_t where; 980 spa_aux_t search; 981 spa_aux_t *aux; 982 983 search.aux_guid = vd->vdev_guid; 984 if ((aux = avl_find(avl, &search, &where)) != NULL) { 985 aux->aux_count++; 986 } else { 987 aux = kmem_zalloc(sizeof (spa_aux_t), KM_SLEEP); 988 aux->aux_guid = vd->vdev_guid; 989 aux->aux_count = 1; 990 avl_insert(avl, aux, where); 991 } 992 } 993 994 static void 995 spa_aux_remove(vdev_t *vd, avl_tree_t *avl) 996 { 997 spa_aux_t search; 998 spa_aux_t *aux; 999 avl_index_t where; 1000 1001 search.aux_guid = vd->vdev_guid; 1002 aux = avl_find(avl, &search, &where); 1003 1004 ASSERT(aux != NULL); 1005 1006 if (--aux->aux_count == 0) { 1007 avl_remove(avl, aux); 1008 kmem_free(aux, sizeof (spa_aux_t)); 1009 } else if (aux->aux_pool == spa_guid(vd->vdev_spa)) { 1010 aux->aux_pool = 0ULL; 1011 } 1012 } 1013 1014 static boolean_t 1015 spa_aux_exists(uint64_t guid, uint64_t *pool, int *refcnt, avl_tree_t *avl) 1016 { 1017 spa_aux_t search, *found; 1018 1019 search.aux_guid = guid; 1020 found = avl_find(avl, &search, NULL); 1021 1022 if (pool) { 1023 if (found) 1024 *pool = found->aux_pool; 1025 else 1026 *pool = 0ULL; 1027 } 1028 1029 if (refcnt) { 1030 if (found) 1031 *refcnt = found->aux_count; 1032 else 1033 *refcnt = 0; 1034 } 1035 1036 return (found != NULL); 1037 } 1038 1039 static void 1040 spa_aux_activate(vdev_t *vd, avl_tree_t *avl) 1041 { 1042 spa_aux_t search, *found; 1043 avl_index_t where; 1044 1045 search.aux_guid = vd->vdev_guid; 1046 found = avl_find(avl, &search, &where); 1047 ASSERT(found != NULL); 1048 ASSERT(found->aux_pool == 0ULL); 1049 1050 found->aux_pool = spa_guid(vd->vdev_spa); 1051 } 1052 1053 /* 1054 * Spares are tracked globally due to the following constraints: 1055 * 1056 * - A spare may be part of multiple pools. 1057 * - A spare may be added to a pool even if it's actively in use within 1058 * another pool. 1059 * - A spare in use in any pool can only be the source of a replacement if 1060 * the target is a spare in the same pool. 1061 * 1062 * We keep track of all spares on the system through the use of a reference 1063 * counted AVL tree. When a vdev is added as a spare, or used as a replacement 1064 * spare, then we bump the reference count in the AVL tree. In addition, we set 1065 * the 'vdev_isspare' member to indicate that the device is a spare (active or 1066 * inactive). When a spare is made active (used to replace a device in the 1067 * pool), we also keep track of which pool its been made a part of. 1068 * 1069 * The 'spa_spare_lock' protects the AVL tree. These functions are normally 1070 * called under the spa_namespace lock as part of vdev reconfiguration. The 1071 * separate spare lock exists for the status query path, which does not need to 1072 * be completely consistent with respect to other vdev configuration changes. 1073 */ 1074 1075 static int 1076 spa_spare_compare(const void *a, const void *b) 1077 { 1078 return (spa_aux_compare(a, b)); 1079 } 1080 1081 void 1082 spa_spare_add(vdev_t *vd) 1083 { 1084 mutex_enter(&spa_spare_lock); 1085 ASSERT(!vd->vdev_isspare); 1086 spa_aux_add(vd, &spa_spare_avl); 1087 vd->vdev_isspare = B_TRUE; 1088 mutex_exit(&spa_spare_lock); 1089 } 1090 1091 void 1092 spa_spare_remove(vdev_t *vd) 1093 { 1094 mutex_enter(&spa_spare_lock); 1095 ASSERT(vd->vdev_isspare); 1096 spa_aux_remove(vd, &spa_spare_avl); 1097 vd->vdev_isspare = B_FALSE; 1098 mutex_exit(&spa_spare_lock); 1099 } 1100 1101 boolean_t 1102 spa_spare_exists(uint64_t guid, uint64_t *pool, int *refcnt) 1103 { 1104 boolean_t found; 1105 1106 mutex_enter(&spa_spare_lock); 1107 found = spa_aux_exists(guid, pool, refcnt, &spa_spare_avl); 1108 mutex_exit(&spa_spare_lock); 1109 1110 return (found); 1111 } 1112 1113 void 1114 spa_spare_activate(vdev_t *vd) 1115 { 1116 mutex_enter(&spa_spare_lock); 1117 ASSERT(vd->vdev_isspare); 1118 spa_aux_activate(vd, &spa_spare_avl); 1119 mutex_exit(&spa_spare_lock); 1120 } 1121 1122 /* 1123 * Level 2 ARC devices are tracked globally for the same reasons as spares. 1124 * Cache devices currently only support one pool per cache device, and so 1125 * for these devices the aux reference count is currently unused beyond 1. 1126 */ 1127 1128 static int 1129 spa_l2cache_compare(const void *a, const void *b) 1130 { 1131 return (spa_aux_compare(a, b)); 1132 } 1133 1134 void 1135 spa_l2cache_add(vdev_t *vd) 1136 { 1137 mutex_enter(&spa_l2cache_lock); 1138 ASSERT(!vd->vdev_isl2cache); 1139 spa_aux_add(vd, &spa_l2cache_avl); 1140 vd->vdev_isl2cache = B_TRUE; 1141 mutex_exit(&spa_l2cache_lock); 1142 } 1143 1144 void 1145 spa_l2cache_remove(vdev_t *vd) 1146 { 1147 mutex_enter(&spa_l2cache_lock); 1148 ASSERT(vd->vdev_isl2cache); 1149 spa_aux_remove(vd, &spa_l2cache_avl); 1150 vd->vdev_isl2cache = B_FALSE; 1151 mutex_exit(&spa_l2cache_lock); 1152 } 1153 1154 boolean_t 1155 spa_l2cache_exists(uint64_t guid, uint64_t *pool) 1156 { 1157 boolean_t found; 1158 1159 mutex_enter(&spa_l2cache_lock); 1160 found = spa_aux_exists(guid, pool, NULL, &spa_l2cache_avl); 1161 mutex_exit(&spa_l2cache_lock); 1162 1163 return (found); 1164 } 1165 1166 void 1167 spa_l2cache_activate(vdev_t *vd) 1168 { 1169 mutex_enter(&spa_l2cache_lock); 1170 ASSERT(vd->vdev_isl2cache); 1171 spa_aux_activate(vd, &spa_l2cache_avl); 1172 mutex_exit(&spa_l2cache_lock); 1173 } 1174 1175 /* 1176 * ========================================================================== 1177 * SPA vdev locking 1178 * ========================================================================== 1179 */ 1180 1181 /* 1182 * Lock the given spa_t for the purpose of adding or removing a vdev. 1183 * Grabs the global spa_namespace_lock plus the spa config lock for writing. 1184 * It returns the next transaction group for the spa_t. 1185 */ 1186 uint64_t 1187 spa_vdev_enter(spa_t *spa) 1188 { 1189 mutex_enter(&spa->spa_vdev_top_lock); 1190 mutex_enter(&spa_namespace_lock); 1191 1192 vdev_autotrim_stop_all(spa); 1193 1194 return (spa_vdev_config_enter(spa)); 1195 } 1196 1197 /* 1198 * The same as spa_vdev_enter() above but additionally takes the guid of 1199 * the vdev being detached. When there is a rebuild in process it will be 1200 * suspended while the vdev tree is modified then resumed by spa_vdev_exit(). 1201 * The rebuild is canceled if only a single child remains after the detach. 1202 */ 1203 uint64_t 1204 spa_vdev_detach_enter(spa_t *spa, uint64_t guid) 1205 { 1206 mutex_enter(&spa->spa_vdev_top_lock); 1207 mutex_enter(&spa_namespace_lock); 1208 1209 vdev_autotrim_stop_all(spa); 1210 1211 if (guid != 0) { 1212 vdev_t *vd = spa_lookup_by_guid(spa, guid, B_FALSE); 1213 if (vd) { 1214 vdev_rebuild_stop_wait(vd->vdev_top); 1215 } 1216 } 1217 1218 return (spa_vdev_config_enter(spa)); 1219 } 1220 1221 /* 1222 * Internal implementation for spa_vdev_enter(). Used when a vdev 1223 * operation requires multiple syncs (i.e. removing a device) while 1224 * keeping the spa_namespace_lock held. 1225 */ 1226 uint64_t 1227 spa_vdev_config_enter(spa_t *spa) 1228 { 1229 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1230 1231 spa_config_enter(spa, SCL_ALL, spa, RW_WRITER); 1232 1233 return (spa_last_synced_txg(spa) + 1); 1234 } 1235 1236 /* 1237 * Used in combination with spa_vdev_config_enter() to allow the syncing 1238 * of multiple transactions without releasing the spa_namespace_lock. 1239 */ 1240 void 1241 spa_vdev_config_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error, 1242 const char *tag) 1243 { 1244 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1245 1246 int config_changed = B_FALSE; 1247 1248 ASSERT(txg > spa_last_synced_txg(spa)); 1249 1250 spa->spa_pending_vdev = NULL; 1251 1252 /* 1253 * Reassess the DTLs. 1254 */ 1255 vdev_dtl_reassess(spa->spa_root_vdev, 0, 0, B_FALSE, B_FALSE); 1256 1257 if (error == 0 && !list_is_empty(&spa->spa_config_dirty_list)) { 1258 config_changed = B_TRUE; 1259 spa->spa_config_generation++; 1260 } 1261 1262 /* 1263 * Verify the metaslab classes. 1264 */ 1265 ASSERT(metaslab_class_validate(spa_normal_class(spa)) == 0); 1266 ASSERT(metaslab_class_validate(spa_log_class(spa)) == 0); 1267 ASSERT(metaslab_class_validate(spa_embedded_log_class(spa)) == 0); 1268 ASSERT(metaslab_class_validate(spa_special_class(spa)) == 0); 1269 ASSERT(metaslab_class_validate(spa_dedup_class(spa)) == 0); 1270 1271 spa_config_exit(spa, SCL_ALL, spa); 1272 1273 /* 1274 * Panic the system if the specified tag requires it. This 1275 * is useful for ensuring that configurations are updated 1276 * transactionally. 1277 */ 1278 if (zio_injection_enabled) 1279 zio_handle_panic_injection(spa, tag, 0); 1280 1281 /* 1282 * Note: this txg_wait_synced() is important because it ensures 1283 * that there won't be more than one config change per txg. 1284 * This allows us to use the txg as the generation number. 1285 */ 1286 if (error == 0) 1287 txg_wait_synced(spa->spa_dsl_pool, txg); 1288 1289 if (vd != NULL) { 1290 ASSERT(!vd->vdev_detached || vd->vdev_dtl_sm == NULL); 1291 if (vd->vdev_ops->vdev_op_leaf) { 1292 mutex_enter(&vd->vdev_initialize_lock); 1293 vdev_initialize_stop(vd, VDEV_INITIALIZE_CANCELED, 1294 NULL); 1295 mutex_exit(&vd->vdev_initialize_lock); 1296 1297 mutex_enter(&vd->vdev_trim_lock); 1298 vdev_trim_stop(vd, VDEV_TRIM_CANCELED, NULL); 1299 mutex_exit(&vd->vdev_trim_lock); 1300 } 1301 1302 /* 1303 * The vdev may be both a leaf and top-level device. 1304 */ 1305 vdev_autotrim_stop_wait(vd); 1306 1307 spa_config_enter(spa, SCL_STATE_ALL, spa, RW_WRITER); 1308 vdev_free(vd); 1309 spa_config_exit(spa, SCL_STATE_ALL, spa); 1310 } 1311 1312 /* 1313 * If the config changed, update the config cache. 1314 */ 1315 if (config_changed) 1316 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_TRUE); 1317 } 1318 1319 /* 1320 * Unlock the spa_t after adding or removing a vdev. Besides undoing the 1321 * locking of spa_vdev_enter(), we also want make sure the transactions have 1322 * synced to disk, and then update the global configuration cache with the new 1323 * information. 1324 */ 1325 int 1326 spa_vdev_exit(spa_t *spa, vdev_t *vd, uint64_t txg, int error) 1327 { 1328 vdev_autotrim_restart(spa); 1329 vdev_rebuild_restart(spa); 1330 1331 spa_vdev_config_exit(spa, vd, txg, error, FTAG); 1332 mutex_exit(&spa_namespace_lock); 1333 mutex_exit(&spa->spa_vdev_top_lock); 1334 1335 return (error); 1336 } 1337 1338 /* 1339 * Lock the given spa_t for the purpose of changing vdev state. 1340 */ 1341 void 1342 spa_vdev_state_enter(spa_t *spa, int oplocks) 1343 { 1344 int locks = SCL_STATE_ALL | oplocks; 1345 1346 /* 1347 * Root pools may need to read of the underlying devfs filesystem 1348 * when opening up a vdev. Unfortunately if we're holding the 1349 * SCL_ZIO lock it will result in a deadlock when we try to issue 1350 * the read from the root filesystem. Instead we "prefetch" 1351 * the associated vnodes that we need prior to opening the 1352 * underlying devices and cache them so that we can prevent 1353 * any I/O when we are doing the actual open. 1354 */ 1355 if (spa_is_root(spa)) { 1356 int low = locks & ~(SCL_ZIO - 1); 1357 int high = locks & ~low; 1358 1359 spa_config_enter(spa, high, spa, RW_WRITER); 1360 vdev_hold(spa->spa_root_vdev); 1361 spa_config_enter(spa, low, spa, RW_WRITER); 1362 } else { 1363 spa_config_enter(spa, locks, spa, RW_WRITER); 1364 } 1365 spa->spa_vdev_locks = locks; 1366 } 1367 1368 int 1369 spa_vdev_state_exit(spa_t *spa, vdev_t *vd, int error) 1370 { 1371 boolean_t config_changed = B_FALSE; 1372 vdev_t *vdev_top; 1373 1374 if (vd == NULL || vd == spa->spa_root_vdev) { 1375 vdev_top = spa->spa_root_vdev; 1376 } else { 1377 vdev_top = vd->vdev_top; 1378 } 1379 1380 if (vd != NULL || error == 0) 1381 vdev_dtl_reassess(vdev_top, 0, 0, B_FALSE, B_FALSE); 1382 1383 if (vd != NULL) { 1384 if (vd != spa->spa_root_vdev) 1385 vdev_state_dirty(vdev_top); 1386 1387 config_changed = B_TRUE; 1388 spa->spa_config_generation++; 1389 } 1390 1391 if (spa_is_root(spa)) 1392 vdev_rele(spa->spa_root_vdev); 1393 1394 ASSERT3U(spa->spa_vdev_locks, >=, SCL_STATE_ALL); 1395 spa_config_exit(spa, spa->spa_vdev_locks, spa); 1396 1397 /* 1398 * If anything changed, wait for it to sync. This ensures that, 1399 * from the system administrator's perspective, zpool(8) commands 1400 * are synchronous. This is important for things like zpool offline: 1401 * when the command completes, you expect no further I/O from ZFS. 1402 */ 1403 if (vd != NULL) 1404 txg_wait_synced(spa->spa_dsl_pool, 0); 1405 1406 /* 1407 * If the config changed, update the config cache. 1408 */ 1409 if (config_changed) { 1410 mutex_enter(&spa_namespace_lock); 1411 spa_write_cachefile(spa, B_FALSE, B_TRUE, B_FALSE); 1412 mutex_exit(&spa_namespace_lock); 1413 } 1414 1415 return (error); 1416 } 1417 1418 /* 1419 * ========================================================================== 1420 * Miscellaneous functions 1421 * ========================================================================== 1422 */ 1423 1424 void 1425 spa_activate_mos_feature(spa_t *spa, const char *feature, dmu_tx_t *tx) 1426 { 1427 if (!nvlist_exists(spa->spa_label_features, feature)) { 1428 fnvlist_add_boolean(spa->spa_label_features, feature); 1429 /* 1430 * When we are creating the pool (tx_txg==TXG_INITIAL), we can't 1431 * dirty the vdev config because lock SCL_CONFIG is not held. 1432 * Thankfully, in this case we don't need to dirty the config 1433 * because it will be written out anyway when we finish 1434 * creating the pool. 1435 */ 1436 if (tx->tx_txg != TXG_INITIAL) 1437 vdev_config_dirty(spa->spa_root_vdev); 1438 } 1439 } 1440 1441 void 1442 spa_deactivate_mos_feature(spa_t *spa, const char *feature) 1443 { 1444 if (nvlist_remove_all(spa->spa_label_features, feature) == 0) 1445 vdev_config_dirty(spa->spa_root_vdev); 1446 } 1447 1448 /* 1449 * Return the spa_t associated with given pool_guid, if it exists. If 1450 * device_guid is non-zero, determine whether the pool exists *and* contains 1451 * a device with the specified device_guid. 1452 */ 1453 spa_t * 1454 spa_by_guid(uint64_t pool_guid, uint64_t device_guid) 1455 { 1456 spa_t *spa; 1457 avl_tree_t *t = &spa_namespace_avl; 1458 1459 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 1460 1461 for (spa = avl_first(t); spa != NULL; spa = AVL_NEXT(t, spa)) { 1462 if (spa->spa_state == POOL_STATE_UNINITIALIZED) 1463 continue; 1464 if (spa->spa_root_vdev == NULL) 1465 continue; 1466 if (spa_guid(spa) == pool_guid) { 1467 if (device_guid == 0) 1468 break; 1469 1470 if (vdev_lookup_by_guid(spa->spa_root_vdev, 1471 device_guid) != NULL) 1472 break; 1473 1474 /* 1475 * Check any devices we may be in the process of adding. 1476 */ 1477 if (spa->spa_pending_vdev) { 1478 if (vdev_lookup_by_guid(spa->spa_pending_vdev, 1479 device_guid) != NULL) 1480 break; 1481 } 1482 } 1483 } 1484 1485 return (spa); 1486 } 1487 1488 /* 1489 * Determine whether a pool with the given pool_guid exists. 1490 */ 1491 boolean_t 1492 spa_guid_exists(uint64_t pool_guid, uint64_t device_guid) 1493 { 1494 return (spa_by_guid(pool_guid, device_guid) != NULL); 1495 } 1496 1497 char * 1498 spa_strdup(const char *s) 1499 { 1500 size_t len; 1501 char *new; 1502 1503 len = strlen(s); 1504 new = kmem_alloc(len + 1, KM_SLEEP); 1505 memcpy(new, s, len + 1); 1506 1507 return (new); 1508 } 1509 1510 void 1511 spa_strfree(char *s) 1512 { 1513 kmem_free(s, strlen(s) + 1); 1514 } 1515 1516 uint64_t 1517 spa_generate_guid(spa_t *spa) 1518 { 1519 uint64_t guid; 1520 1521 if (spa != NULL) { 1522 do { 1523 (void) random_get_pseudo_bytes((void *)&guid, 1524 sizeof (guid)); 1525 } while (guid == 0 || spa_guid_exists(spa_guid(spa), guid)); 1526 } else { 1527 do { 1528 (void) random_get_pseudo_bytes((void *)&guid, 1529 sizeof (guid)); 1530 } while (guid == 0 || spa_guid_exists(guid, 0)); 1531 } 1532 1533 return (guid); 1534 } 1535 1536 void 1537 snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp) 1538 { 1539 char type[256]; 1540 const char *checksum = NULL; 1541 const char *compress = NULL; 1542 1543 if (bp != NULL) { 1544 if (BP_GET_TYPE(bp) & DMU_OT_NEWTYPE) { 1545 dmu_object_byteswap_t bswap = 1546 DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 1547 (void) snprintf(type, sizeof (type), "bswap %s %s", 1548 DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) ? 1549 "metadata" : "data", 1550 dmu_ot_byteswap[bswap].ob_name); 1551 } else { 1552 (void) strlcpy(type, dmu_ot[BP_GET_TYPE(bp)].ot_name, 1553 sizeof (type)); 1554 } 1555 if (!BP_IS_EMBEDDED(bp)) { 1556 checksum = 1557 zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_name; 1558 } 1559 compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name; 1560 } 1561 1562 SNPRINTF_BLKPTR(kmem_scnprintf, ' ', buf, buflen, bp, type, checksum, 1563 compress); 1564 } 1565 1566 void 1567 spa_freeze(spa_t *spa) 1568 { 1569 uint64_t freeze_txg = 0; 1570 1571 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 1572 if (spa->spa_freeze_txg == UINT64_MAX) { 1573 freeze_txg = spa_last_synced_txg(spa) + TXG_SIZE; 1574 spa->spa_freeze_txg = freeze_txg; 1575 } 1576 spa_config_exit(spa, SCL_ALL, FTAG); 1577 if (freeze_txg != 0) 1578 txg_wait_synced(spa_get_dsl(spa), freeze_txg); 1579 } 1580 1581 void 1582 zfs_panic_recover(const char *fmt, ...) 1583 { 1584 va_list adx; 1585 1586 va_start(adx, fmt); 1587 vcmn_err(zfs_recover ? CE_WARN : CE_PANIC, fmt, adx); 1588 va_end(adx); 1589 } 1590 1591 /* 1592 * This is a stripped-down version of strtoull, suitable only for converting 1593 * lowercase hexadecimal numbers that don't overflow. 1594 */ 1595 uint64_t 1596 zfs_strtonum(const char *str, char **nptr) 1597 { 1598 uint64_t val = 0; 1599 char c; 1600 int digit; 1601 1602 while ((c = *str) != '\0') { 1603 if (c >= '0' && c <= '9') 1604 digit = c - '0'; 1605 else if (c >= 'a' && c <= 'f') 1606 digit = 10 + c - 'a'; 1607 else 1608 break; 1609 1610 val *= 16; 1611 val += digit; 1612 1613 str++; 1614 } 1615 1616 if (nptr) 1617 *nptr = (char *)str; 1618 1619 return (val); 1620 } 1621 1622 void 1623 spa_activate_allocation_classes(spa_t *spa, dmu_tx_t *tx) 1624 { 1625 /* 1626 * We bump the feature refcount for each special vdev added to the pool 1627 */ 1628 ASSERT(spa_feature_is_enabled(spa, SPA_FEATURE_ALLOCATION_CLASSES)); 1629 spa_feature_incr(spa, SPA_FEATURE_ALLOCATION_CLASSES, tx); 1630 } 1631 1632 /* 1633 * ========================================================================== 1634 * Accessor functions 1635 * ========================================================================== 1636 */ 1637 1638 boolean_t 1639 spa_shutting_down(spa_t *spa) 1640 { 1641 return (spa->spa_async_suspended); 1642 } 1643 1644 dsl_pool_t * 1645 spa_get_dsl(spa_t *spa) 1646 { 1647 return (spa->spa_dsl_pool); 1648 } 1649 1650 boolean_t 1651 spa_is_initializing(spa_t *spa) 1652 { 1653 return (spa->spa_is_initializing); 1654 } 1655 1656 boolean_t 1657 spa_indirect_vdevs_loaded(spa_t *spa) 1658 { 1659 return (spa->spa_indirect_vdevs_loaded); 1660 } 1661 1662 blkptr_t * 1663 spa_get_rootblkptr(spa_t *spa) 1664 { 1665 return (&spa->spa_ubsync.ub_rootbp); 1666 } 1667 1668 void 1669 spa_set_rootblkptr(spa_t *spa, const blkptr_t *bp) 1670 { 1671 spa->spa_uberblock.ub_rootbp = *bp; 1672 } 1673 1674 void 1675 spa_altroot(spa_t *spa, char *buf, size_t buflen) 1676 { 1677 if (spa->spa_root == NULL) 1678 buf[0] = '\0'; 1679 else 1680 (void) strlcpy(buf, spa->spa_root, buflen); 1681 } 1682 1683 uint32_t 1684 spa_sync_pass(spa_t *spa) 1685 { 1686 return (spa->spa_sync_pass); 1687 } 1688 1689 char * 1690 spa_name(spa_t *spa) 1691 { 1692 return (spa->spa_name); 1693 } 1694 1695 uint64_t 1696 spa_guid(spa_t *spa) 1697 { 1698 dsl_pool_t *dp = spa_get_dsl(spa); 1699 uint64_t guid; 1700 1701 /* 1702 * If we fail to parse the config during spa_load(), we can go through 1703 * the error path (which posts an ereport) and end up here with no root 1704 * vdev. We stash the original pool guid in 'spa_config_guid' to handle 1705 * this case. 1706 */ 1707 if (spa->spa_root_vdev == NULL) 1708 return (spa->spa_config_guid); 1709 1710 guid = spa->spa_last_synced_guid != 0 ? 1711 spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid; 1712 1713 /* 1714 * Return the most recently synced out guid unless we're 1715 * in syncing context. 1716 */ 1717 if (dp && dsl_pool_sync_context(dp)) 1718 return (spa->spa_root_vdev->vdev_guid); 1719 else 1720 return (guid); 1721 } 1722 1723 uint64_t 1724 spa_load_guid(spa_t *spa) 1725 { 1726 /* 1727 * This is a GUID that exists solely as a reference for the 1728 * purposes of the arc. It is generated at load time, and 1729 * is never written to persistent storage. 1730 */ 1731 return (spa->spa_load_guid); 1732 } 1733 1734 uint64_t 1735 spa_last_synced_txg(spa_t *spa) 1736 { 1737 return (spa->spa_ubsync.ub_txg); 1738 } 1739 1740 uint64_t 1741 spa_first_txg(spa_t *spa) 1742 { 1743 return (spa->spa_first_txg); 1744 } 1745 1746 uint64_t 1747 spa_syncing_txg(spa_t *spa) 1748 { 1749 return (spa->spa_syncing_txg); 1750 } 1751 1752 /* 1753 * Return the last txg where data can be dirtied. The final txgs 1754 * will be used to just clear out any deferred frees that remain. 1755 */ 1756 uint64_t 1757 spa_final_dirty_txg(spa_t *spa) 1758 { 1759 return (spa->spa_final_txg - TXG_DEFER_SIZE); 1760 } 1761 1762 pool_state_t 1763 spa_state(spa_t *spa) 1764 { 1765 return (spa->spa_state); 1766 } 1767 1768 spa_load_state_t 1769 spa_load_state(spa_t *spa) 1770 { 1771 return (spa->spa_load_state); 1772 } 1773 1774 uint64_t 1775 spa_freeze_txg(spa_t *spa) 1776 { 1777 return (spa->spa_freeze_txg); 1778 } 1779 1780 /* 1781 * Return the inflated asize for a logical write in bytes. This is used by the 1782 * DMU to calculate the space a logical write will require on disk. 1783 * If lsize is smaller than the largest physical block size allocatable on this 1784 * pool we use its value instead, since the write will end up using the whole 1785 * block anyway. 1786 */ 1787 uint64_t 1788 spa_get_worst_case_asize(spa_t *spa, uint64_t lsize) 1789 { 1790 if (lsize == 0) 1791 return (0); /* No inflation needed */ 1792 return (MAX(lsize, 1 << spa->spa_max_ashift) * spa_asize_inflation); 1793 } 1794 1795 /* 1796 * Return the amount of slop space in bytes. It is typically 1/32 of the pool 1797 * (3.2%), minus the embedded log space. On very small pools, it may be 1798 * slightly larger than this. On very large pools, it will be capped to 1799 * the value of spa_max_slop. The embedded log space is not included in 1800 * spa_dspace. By subtracting it, the usable space (per "zfs list") is a 1801 * constant 97% of the total space, regardless of metaslab size (assuming the 1802 * default spa_slop_shift=5 and a non-tiny pool). 1803 * 1804 * See the comment above spa_slop_shift for more details. 1805 */ 1806 uint64_t 1807 spa_get_slop_space(spa_t *spa) 1808 { 1809 uint64_t space = 0; 1810 uint64_t slop = 0; 1811 1812 /* 1813 * Make sure spa_dedup_dspace has been set. 1814 */ 1815 if (spa->spa_dedup_dspace == ~0ULL) 1816 spa_update_dspace(spa); 1817 1818 /* 1819 * spa_get_dspace() includes the space only logically "used" by 1820 * deduplicated data, so since it's not useful to reserve more 1821 * space with more deduplicated data, we subtract that out here. 1822 */ 1823 space = spa_get_dspace(spa) - spa->spa_dedup_dspace; 1824 slop = MIN(space >> spa_slop_shift, spa_max_slop); 1825 1826 /* 1827 * Subtract the embedded log space, but no more than half the (3.2%) 1828 * unusable space. Note, the "no more than half" is only relevant if 1829 * zfs_embedded_slog_min_ms >> spa_slop_shift < 2, which is not true by 1830 * default. 1831 */ 1832 uint64_t embedded_log = 1833 metaslab_class_get_dspace(spa_embedded_log_class(spa)); 1834 slop -= MIN(embedded_log, slop >> 1); 1835 1836 /* 1837 * Slop space should be at least spa_min_slop, but no more than half 1838 * the entire pool. 1839 */ 1840 slop = MAX(slop, MIN(space >> 1, spa_min_slop)); 1841 return (slop); 1842 } 1843 1844 uint64_t 1845 spa_get_dspace(spa_t *spa) 1846 { 1847 return (spa->spa_dspace); 1848 } 1849 1850 uint64_t 1851 spa_get_checkpoint_space(spa_t *spa) 1852 { 1853 return (spa->spa_checkpoint_info.sci_dspace); 1854 } 1855 1856 void 1857 spa_update_dspace(spa_t *spa) 1858 { 1859 spa->spa_dspace = metaslab_class_get_dspace(spa_normal_class(spa)) + 1860 ddt_get_dedup_dspace(spa) + brt_get_dspace(spa); 1861 if (spa->spa_nonallocating_dspace > 0) { 1862 /* 1863 * Subtract the space provided by all non-allocating vdevs that 1864 * contribute to dspace. If a file is overwritten, its old 1865 * blocks are freed and new blocks are allocated. If there are 1866 * no snapshots of the file, the available space should remain 1867 * the same. The old blocks could be freed from the 1868 * non-allocating vdev, but the new blocks must be allocated on 1869 * other (allocating) vdevs. By reserving the entire size of 1870 * the non-allocating vdevs (including allocated space), we 1871 * ensure that there will be enough space on the allocating 1872 * vdevs for this file overwrite to succeed. 1873 * 1874 * Note that the DMU/DSL doesn't actually know or care 1875 * how much space is allocated (it does its own tracking 1876 * of how much space has been logically used). So it 1877 * doesn't matter that the data we are moving may be 1878 * allocated twice (on the old device and the new device). 1879 */ 1880 ASSERT3U(spa->spa_dspace, >=, spa->spa_nonallocating_dspace); 1881 spa->spa_dspace -= spa->spa_nonallocating_dspace; 1882 } 1883 } 1884 1885 /* 1886 * Return the failure mode that has been set to this pool. The default 1887 * behavior will be to block all I/Os when a complete failure occurs. 1888 */ 1889 uint64_t 1890 spa_get_failmode(spa_t *spa) 1891 { 1892 return (spa->spa_failmode); 1893 } 1894 1895 boolean_t 1896 spa_suspended(spa_t *spa) 1897 { 1898 return (spa->spa_suspended != ZIO_SUSPEND_NONE); 1899 } 1900 1901 uint64_t 1902 spa_version(spa_t *spa) 1903 { 1904 return (spa->spa_ubsync.ub_version); 1905 } 1906 1907 boolean_t 1908 spa_deflate(spa_t *spa) 1909 { 1910 return (spa->spa_deflate); 1911 } 1912 1913 metaslab_class_t * 1914 spa_normal_class(spa_t *spa) 1915 { 1916 return (spa->spa_normal_class); 1917 } 1918 1919 metaslab_class_t * 1920 spa_log_class(spa_t *spa) 1921 { 1922 return (spa->spa_log_class); 1923 } 1924 1925 metaslab_class_t * 1926 spa_embedded_log_class(spa_t *spa) 1927 { 1928 return (spa->spa_embedded_log_class); 1929 } 1930 1931 metaslab_class_t * 1932 spa_special_class(spa_t *spa) 1933 { 1934 return (spa->spa_special_class); 1935 } 1936 1937 metaslab_class_t * 1938 spa_dedup_class(spa_t *spa) 1939 { 1940 return (spa->spa_dedup_class); 1941 } 1942 1943 /* 1944 * Locate an appropriate allocation class 1945 */ 1946 metaslab_class_t * 1947 spa_preferred_class(spa_t *spa, uint64_t size, dmu_object_type_t objtype, 1948 uint_t level, uint_t special_smallblk) 1949 { 1950 /* 1951 * ZIL allocations determine their class in zio_alloc_zil(). 1952 */ 1953 ASSERT(objtype != DMU_OT_INTENT_LOG); 1954 1955 boolean_t has_special_class = spa->spa_special_class->mc_groups != 0; 1956 1957 if (DMU_OT_IS_DDT(objtype)) { 1958 if (spa->spa_dedup_class->mc_groups != 0) 1959 return (spa_dedup_class(spa)); 1960 else if (has_special_class && zfs_ddt_data_is_special) 1961 return (spa_special_class(spa)); 1962 else 1963 return (spa_normal_class(spa)); 1964 } 1965 1966 /* Indirect blocks for user data can land in special if allowed */ 1967 if (level > 0 && (DMU_OT_IS_FILE(objtype) || objtype == DMU_OT_ZVOL)) { 1968 if (has_special_class && zfs_user_indirect_is_special) 1969 return (spa_special_class(spa)); 1970 else 1971 return (spa_normal_class(spa)); 1972 } 1973 1974 if (DMU_OT_IS_METADATA(objtype) || level > 0) { 1975 if (has_special_class) 1976 return (spa_special_class(spa)); 1977 else 1978 return (spa_normal_class(spa)); 1979 } 1980 1981 /* 1982 * Allow small file blocks in special class in some cases (like 1983 * for the dRAID vdev feature). But always leave a reserve of 1984 * zfs_special_class_metadata_reserve_pct exclusively for metadata. 1985 */ 1986 if (DMU_OT_IS_FILE(objtype) && 1987 has_special_class && size <= special_smallblk) { 1988 metaslab_class_t *special = spa_special_class(spa); 1989 uint64_t alloc = metaslab_class_get_alloc(special); 1990 uint64_t space = metaslab_class_get_space(special); 1991 uint64_t limit = 1992 (space * (100 - zfs_special_class_metadata_reserve_pct)) 1993 / 100; 1994 1995 if (alloc < limit) 1996 return (special); 1997 } 1998 1999 return (spa_normal_class(spa)); 2000 } 2001 2002 void 2003 spa_evicting_os_register(spa_t *spa, objset_t *os) 2004 { 2005 mutex_enter(&spa->spa_evicting_os_lock); 2006 list_insert_head(&spa->spa_evicting_os_list, os); 2007 mutex_exit(&spa->spa_evicting_os_lock); 2008 } 2009 2010 void 2011 spa_evicting_os_deregister(spa_t *spa, objset_t *os) 2012 { 2013 mutex_enter(&spa->spa_evicting_os_lock); 2014 list_remove(&spa->spa_evicting_os_list, os); 2015 cv_broadcast(&spa->spa_evicting_os_cv); 2016 mutex_exit(&spa->spa_evicting_os_lock); 2017 } 2018 2019 void 2020 spa_evicting_os_wait(spa_t *spa) 2021 { 2022 mutex_enter(&spa->spa_evicting_os_lock); 2023 while (!list_is_empty(&spa->spa_evicting_os_list)) 2024 cv_wait(&spa->spa_evicting_os_cv, &spa->spa_evicting_os_lock); 2025 mutex_exit(&spa->spa_evicting_os_lock); 2026 2027 dmu_buf_user_evict_wait(); 2028 } 2029 2030 int 2031 spa_max_replication(spa_t *spa) 2032 { 2033 /* 2034 * As of SPA_VERSION == SPA_VERSION_DITTO_BLOCKS, we are able to 2035 * handle BPs with more than one DVA allocated. Set our max 2036 * replication level accordingly. 2037 */ 2038 if (spa_version(spa) < SPA_VERSION_DITTO_BLOCKS) 2039 return (1); 2040 return (MIN(SPA_DVAS_PER_BP, spa_max_replication_override)); 2041 } 2042 2043 int 2044 spa_prev_software_version(spa_t *spa) 2045 { 2046 return (spa->spa_prev_software_version); 2047 } 2048 2049 uint64_t 2050 spa_deadman_synctime(spa_t *spa) 2051 { 2052 return (spa->spa_deadman_synctime); 2053 } 2054 2055 spa_autotrim_t 2056 spa_get_autotrim(spa_t *spa) 2057 { 2058 return (spa->spa_autotrim); 2059 } 2060 2061 uint64_t 2062 spa_deadman_ziotime(spa_t *spa) 2063 { 2064 return (spa->spa_deadman_ziotime); 2065 } 2066 2067 uint64_t 2068 spa_get_deadman_failmode(spa_t *spa) 2069 { 2070 return (spa->spa_deadman_failmode); 2071 } 2072 2073 void 2074 spa_set_deadman_failmode(spa_t *spa, const char *failmode) 2075 { 2076 if (strcmp(failmode, "wait") == 0) 2077 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT; 2078 else if (strcmp(failmode, "continue") == 0) 2079 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_CONTINUE; 2080 else if (strcmp(failmode, "panic") == 0) 2081 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_PANIC; 2082 else 2083 spa->spa_deadman_failmode = ZIO_FAILURE_MODE_WAIT; 2084 } 2085 2086 void 2087 spa_set_deadman_ziotime(hrtime_t ns) 2088 { 2089 spa_t *spa = NULL; 2090 2091 if (spa_mode_global != SPA_MODE_UNINIT) { 2092 mutex_enter(&spa_namespace_lock); 2093 while ((spa = spa_next(spa)) != NULL) 2094 spa->spa_deadman_ziotime = ns; 2095 mutex_exit(&spa_namespace_lock); 2096 } 2097 } 2098 2099 void 2100 spa_set_deadman_synctime(hrtime_t ns) 2101 { 2102 spa_t *spa = NULL; 2103 2104 if (spa_mode_global != SPA_MODE_UNINIT) { 2105 mutex_enter(&spa_namespace_lock); 2106 while ((spa = spa_next(spa)) != NULL) 2107 spa->spa_deadman_synctime = ns; 2108 mutex_exit(&spa_namespace_lock); 2109 } 2110 } 2111 2112 uint64_t 2113 dva_get_dsize_sync(spa_t *spa, const dva_t *dva) 2114 { 2115 uint64_t asize = DVA_GET_ASIZE(dva); 2116 uint64_t dsize = asize; 2117 2118 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0); 2119 2120 if (asize != 0 && spa->spa_deflate) { 2121 vdev_t *vd = vdev_lookup_top(spa, DVA_GET_VDEV(dva)); 2122 if (vd != NULL) 2123 dsize = (asize >> SPA_MINBLOCKSHIFT) * 2124 vd->vdev_deflate_ratio; 2125 } 2126 2127 return (dsize); 2128 } 2129 2130 uint64_t 2131 bp_get_dsize_sync(spa_t *spa, const blkptr_t *bp) 2132 { 2133 uint64_t dsize = 0; 2134 2135 for (int d = 0; d < BP_GET_NDVAS(bp); d++) 2136 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 2137 2138 return (dsize); 2139 } 2140 2141 uint64_t 2142 bp_get_dsize(spa_t *spa, const blkptr_t *bp) 2143 { 2144 uint64_t dsize = 0; 2145 2146 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2147 2148 for (int d = 0; d < BP_GET_NDVAS(bp); d++) 2149 dsize += dva_get_dsize_sync(spa, &bp->blk_dva[d]); 2150 2151 spa_config_exit(spa, SCL_VDEV, FTAG); 2152 2153 return (dsize); 2154 } 2155 2156 uint64_t 2157 spa_dirty_data(spa_t *spa) 2158 { 2159 return (spa->spa_dsl_pool->dp_dirty_total); 2160 } 2161 2162 /* 2163 * ========================================================================== 2164 * SPA Import Progress Routines 2165 * ========================================================================== 2166 */ 2167 2168 typedef struct spa_import_progress { 2169 uint64_t pool_guid; /* unique id for updates */ 2170 char *pool_name; 2171 spa_load_state_t spa_load_state; 2172 uint64_t mmp_sec_remaining; /* MMP activity check */ 2173 uint64_t spa_load_max_txg; /* rewind txg */ 2174 procfs_list_node_t smh_node; 2175 } spa_import_progress_t; 2176 2177 spa_history_list_t *spa_import_progress_list = NULL; 2178 2179 static int 2180 spa_import_progress_show_header(struct seq_file *f) 2181 { 2182 seq_printf(f, "%-20s %-14s %-14s %-12s %s\n", "pool_guid", 2183 "load_state", "multihost_secs", "max_txg", 2184 "pool_name"); 2185 return (0); 2186 } 2187 2188 static int 2189 spa_import_progress_show(struct seq_file *f, void *data) 2190 { 2191 spa_import_progress_t *sip = (spa_import_progress_t *)data; 2192 2193 seq_printf(f, "%-20llu %-14llu %-14llu %-12llu %s\n", 2194 (u_longlong_t)sip->pool_guid, (u_longlong_t)sip->spa_load_state, 2195 (u_longlong_t)sip->mmp_sec_remaining, 2196 (u_longlong_t)sip->spa_load_max_txg, 2197 (sip->pool_name ? sip->pool_name : "-")); 2198 2199 return (0); 2200 } 2201 2202 /* Remove oldest elements from list until there are no more than 'size' left */ 2203 static void 2204 spa_import_progress_truncate(spa_history_list_t *shl, unsigned int size) 2205 { 2206 spa_import_progress_t *sip; 2207 while (shl->size > size) { 2208 sip = list_remove_head(&shl->procfs_list.pl_list); 2209 if (sip->pool_name) 2210 spa_strfree(sip->pool_name); 2211 kmem_free(sip, sizeof (spa_import_progress_t)); 2212 shl->size--; 2213 } 2214 2215 IMPLY(size == 0, list_is_empty(&shl->procfs_list.pl_list)); 2216 } 2217 2218 static void 2219 spa_import_progress_init(void) 2220 { 2221 spa_import_progress_list = kmem_zalloc(sizeof (spa_history_list_t), 2222 KM_SLEEP); 2223 2224 spa_import_progress_list->size = 0; 2225 2226 spa_import_progress_list->procfs_list.pl_private = 2227 spa_import_progress_list; 2228 2229 procfs_list_install("zfs", 2230 NULL, 2231 "import_progress", 2232 0644, 2233 &spa_import_progress_list->procfs_list, 2234 spa_import_progress_show, 2235 spa_import_progress_show_header, 2236 NULL, 2237 offsetof(spa_import_progress_t, smh_node)); 2238 } 2239 2240 static void 2241 spa_import_progress_destroy(void) 2242 { 2243 spa_history_list_t *shl = spa_import_progress_list; 2244 procfs_list_uninstall(&shl->procfs_list); 2245 spa_import_progress_truncate(shl, 0); 2246 procfs_list_destroy(&shl->procfs_list); 2247 kmem_free(shl, sizeof (spa_history_list_t)); 2248 } 2249 2250 int 2251 spa_import_progress_set_state(uint64_t pool_guid, 2252 spa_load_state_t load_state) 2253 { 2254 spa_history_list_t *shl = spa_import_progress_list; 2255 spa_import_progress_t *sip; 2256 int error = ENOENT; 2257 2258 if (shl->size == 0) 2259 return (0); 2260 2261 mutex_enter(&shl->procfs_list.pl_lock); 2262 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL; 2263 sip = list_prev(&shl->procfs_list.pl_list, sip)) { 2264 if (sip->pool_guid == pool_guid) { 2265 sip->spa_load_state = load_state; 2266 error = 0; 2267 break; 2268 } 2269 } 2270 mutex_exit(&shl->procfs_list.pl_lock); 2271 2272 return (error); 2273 } 2274 2275 int 2276 spa_import_progress_set_max_txg(uint64_t pool_guid, uint64_t load_max_txg) 2277 { 2278 spa_history_list_t *shl = spa_import_progress_list; 2279 spa_import_progress_t *sip; 2280 int error = ENOENT; 2281 2282 if (shl->size == 0) 2283 return (0); 2284 2285 mutex_enter(&shl->procfs_list.pl_lock); 2286 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL; 2287 sip = list_prev(&shl->procfs_list.pl_list, sip)) { 2288 if (sip->pool_guid == pool_guid) { 2289 sip->spa_load_max_txg = load_max_txg; 2290 error = 0; 2291 break; 2292 } 2293 } 2294 mutex_exit(&shl->procfs_list.pl_lock); 2295 2296 return (error); 2297 } 2298 2299 int 2300 spa_import_progress_set_mmp_check(uint64_t pool_guid, 2301 uint64_t mmp_sec_remaining) 2302 { 2303 spa_history_list_t *shl = spa_import_progress_list; 2304 spa_import_progress_t *sip; 2305 int error = ENOENT; 2306 2307 if (shl->size == 0) 2308 return (0); 2309 2310 mutex_enter(&shl->procfs_list.pl_lock); 2311 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL; 2312 sip = list_prev(&shl->procfs_list.pl_list, sip)) { 2313 if (sip->pool_guid == pool_guid) { 2314 sip->mmp_sec_remaining = mmp_sec_remaining; 2315 error = 0; 2316 break; 2317 } 2318 } 2319 mutex_exit(&shl->procfs_list.pl_lock); 2320 2321 return (error); 2322 } 2323 2324 /* 2325 * A new import is in progress, add an entry. 2326 */ 2327 void 2328 spa_import_progress_add(spa_t *spa) 2329 { 2330 spa_history_list_t *shl = spa_import_progress_list; 2331 spa_import_progress_t *sip; 2332 const char *poolname = NULL; 2333 2334 sip = kmem_zalloc(sizeof (spa_import_progress_t), KM_SLEEP); 2335 sip->pool_guid = spa_guid(spa); 2336 2337 (void) nvlist_lookup_string(spa->spa_config, ZPOOL_CONFIG_POOL_NAME, 2338 &poolname); 2339 if (poolname == NULL) 2340 poolname = spa_name(spa); 2341 sip->pool_name = spa_strdup(poolname); 2342 sip->spa_load_state = spa_load_state(spa); 2343 2344 mutex_enter(&shl->procfs_list.pl_lock); 2345 procfs_list_add(&shl->procfs_list, sip); 2346 shl->size++; 2347 mutex_exit(&shl->procfs_list.pl_lock); 2348 } 2349 2350 void 2351 spa_import_progress_remove(uint64_t pool_guid) 2352 { 2353 spa_history_list_t *shl = spa_import_progress_list; 2354 spa_import_progress_t *sip; 2355 2356 mutex_enter(&shl->procfs_list.pl_lock); 2357 for (sip = list_tail(&shl->procfs_list.pl_list); sip != NULL; 2358 sip = list_prev(&shl->procfs_list.pl_list, sip)) { 2359 if (sip->pool_guid == pool_guid) { 2360 if (sip->pool_name) 2361 spa_strfree(sip->pool_name); 2362 list_remove(&shl->procfs_list.pl_list, sip); 2363 shl->size--; 2364 kmem_free(sip, sizeof (spa_import_progress_t)); 2365 break; 2366 } 2367 } 2368 mutex_exit(&shl->procfs_list.pl_lock); 2369 } 2370 2371 /* 2372 * ========================================================================== 2373 * Initialization and Termination 2374 * ========================================================================== 2375 */ 2376 2377 static int 2378 spa_name_compare(const void *a1, const void *a2) 2379 { 2380 const spa_t *s1 = a1; 2381 const spa_t *s2 = a2; 2382 int s; 2383 2384 s = strcmp(s1->spa_name, s2->spa_name); 2385 2386 return (TREE_ISIGN(s)); 2387 } 2388 2389 void 2390 spa_boot_init(void) 2391 { 2392 spa_config_load(); 2393 } 2394 2395 void 2396 spa_init(spa_mode_t mode) 2397 { 2398 mutex_init(&spa_namespace_lock, NULL, MUTEX_DEFAULT, NULL); 2399 mutex_init(&spa_spare_lock, NULL, MUTEX_DEFAULT, NULL); 2400 mutex_init(&spa_l2cache_lock, NULL, MUTEX_DEFAULT, NULL); 2401 cv_init(&spa_namespace_cv, NULL, CV_DEFAULT, NULL); 2402 2403 avl_create(&spa_namespace_avl, spa_name_compare, sizeof (spa_t), 2404 offsetof(spa_t, spa_avl)); 2405 2406 avl_create(&spa_spare_avl, spa_spare_compare, sizeof (spa_aux_t), 2407 offsetof(spa_aux_t, aux_avl)); 2408 2409 avl_create(&spa_l2cache_avl, spa_l2cache_compare, sizeof (spa_aux_t), 2410 offsetof(spa_aux_t, aux_avl)); 2411 2412 spa_mode_global = mode; 2413 2414 #ifndef _KERNEL 2415 if (spa_mode_global != SPA_MODE_READ && dprintf_find_string("watch")) { 2416 struct sigaction sa; 2417 2418 sa.sa_flags = SA_SIGINFO; 2419 sigemptyset(&sa.sa_mask); 2420 sa.sa_sigaction = arc_buf_sigsegv; 2421 2422 if (sigaction(SIGSEGV, &sa, NULL) == -1) { 2423 perror("could not enable watchpoints: " 2424 "sigaction(SIGSEGV, ...) = "); 2425 } else { 2426 arc_watch = B_TRUE; 2427 } 2428 } 2429 #endif 2430 2431 fm_init(); 2432 zfs_refcount_init(); 2433 unique_init(); 2434 zfs_btree_init(); 2435 metaslab_stat_init(); 2436 brt_init(); 2437 ddt_init(); 2438 zio_init(); 2439 dmu_init(); 2440 zil_init(); 2441 vdev_mirror_stat_init(); 2442 vdev_raidz_math_init(); 2443 vdev_file_init(); 2444 zfs_prop_init(); 2445 chksum_init(); 2446 zpool_prop_init(); 2447 zpool_feature_init(); 2448 spa_config_load(); 2449 vdev_prop_init(); 2450 l2arc_start(); 2451 scan_init(); 2452 qat_init(); 2453 spa_import_progress_init(); 2454 } 2455 2456 void 2457 spa_fini(void) 2458 { 2459 l2arc_stop(); 2460 2461 spa_evict_all(); 2462 2463 vdev_file_fini(); 2464 vdev_mirror_stat_fini(); 2465 vdev_raidz_math_fini(); 2466 chksum_fini(); 2467 zil_fini(); 2468 dmu_fini(); 2469 zio_fini(); 2470 ddt_fini(); 2471 brt_fini(); 2472 metaslab_stat_fini(); 2473 zfs_btree_fini(); 2474 unique_fini(); 2475 zfs_refcount_fini(); 2476 fm_fini(); 2477 scan_fini(); 2478 qat_fini(); 2479 spa_import_progress_destroy(); 2480 2481 avl_destroy(&spa_namespace_avl); 2482 avl_destroy(&spa_spare_avl); 2483 avl_destroy(&spa_l2cache_avl); 2484 2485 cv_destroy(&spa_namespace_cv); 2486 mutex_destroy(&spa_namespace_lock); 2487 mutex_destroy(&spa_spare_lock); 2488 mutex_destroy(&spa_l2cache_lock); 2489 } 2490 2491 /* 2492 * Return whether this pool has a dedicated slog device. No locking needed. 2493 * It's not a problem if the wrong answer is returned as it's only for 2494 * performance and not correctness. 2495 */ 2496 boolean_t 2497 spa_has_slogs(spa_t *spa) 2498 { 2499 return (spa->spa_log_class->mc_groups != 0); 2500 } 2501 2502 spa_log_state_t 2503 spa_get_log_state(spa_t *spa) 2504 { 2505 return (spa->spa_log_state); 2506 } 2507 2508 void 2509 spa_set_log_state(spa_t *spa, spa_log_state_t state) 2510 { 2511 spa->spa_log_state = state; 2512 } 2513 2514 boolean_t 2515 spa_is_root(spa_t *spa) 2516 { 2517 return (spa->spa_is_root); 2518 } 2519 2520 boolean_t 2521 spa_writeable(spa_t *spa) 2522 { 2523 return (!!(spa->spa_mode & SPA_MODE_WRITE) && spa->spa_trust_config); 2524 } 2525 2526 /* 2527 * Returns true if there is a pending sync task in any of the current 2528 * syncing txg, the current quiescing txg, or the current open txg. 2529 */ 2530 boolean_t 2531 spa_has_pending_synctask(spa_t *spa) 2532 { 2533 return (!txg_all_lists_empty(&spa->spa_dsl_pool->dp_sync_tasks) || 2534 !txg_all_lists_empty(&spa->spa_dsl_pool->dp_early_sync_tasks)); 2535 } 2536 2537 spa_mode_t 2538 spa_mode(spa_t *spa) 2539 { 2540 return (spa->spa_mode); 2541 } 2542 2543 uint64_t 2544 spa_bootfs(spa_t *spa) 2545 { 2546 return (spa->spa_bootfs); 2547 } 2548 2549 uint64_t 2550 spa_delegation(spa_t *spa) 2551 { 2552 return (spa->spa_delegation); 2553 } 2554 2555 objset_t * 2556 spa_meta_objset(spa_t *spa) 2557 { 2558 return (spa->spa_meta_objset); 2559 } 2560 2561 enum zio_checksum 2562 spa_dedup_checksum(spa_t *spa) 2563 { 2564 return (spa->spa_dedup_checksum); 2565 } 2566 2567 /* 2568 * Reset pool scan stat per scan pass (or reboot). 2569 */ 2570 void 2571 spa_scan_stat_init(spa_t *spa) 2572 { 2573 /* data not stored on disk */ 2574 spa->spa_scan_pass_start = gethrestime_sec(); 2575 if (dsl_scan_is_paused_scrub(spa->spa_dsl_pool->dp_scan)) 2576 spa->spa_scan_pass_scrub_pause = spa->spa_scan_pass_start; 2577 else 2578 spa->spa_scan_pass_scrub_pause = 0; 2579 2580 if (dsl_errorscrub_is_paused(spa->spa_dsl_pool->dp_scan)) 2581 spa->spa_scan_pass_errorscrub_pause = spa->spa_scan_pass_start; 2582 else 2583 spa->spa_scan_pass_errorscrub_pause = 0; 2584 2585 spa->spa_scan_pass_scrub_spent_paused = 0; 2586 spa->spa_scan_pass_exam = 0; 2587 spa->spa_scan_pass_issued = 0; 2588 2589 // error scrub stats 2590 spa->spa_scan_pass_errorscrub_spent_paused = 0; 2591 } 2592 2593 /* 2594 * Get scan stats for zpool status reports 2595 */ 2596 int 2597 spa_scan_get_stats(spa_t *spa, pool_scan_stat_t *ps) 2598 { 2599 dsl_scan_t *scn = spa->spa_dsl_pool ? spa->spa_dsl_pool->dp_scan : NULL; 2600 2601 if (scn == NULL || (scn->scn_phys.scn_func == POOL_SCAN_NONE && 2602 scn->errorscrub_phys.dep_func == POOL_SCAN_NONE)) 2603 return (SET_ERROR(ENOENT)); 2604 2605 memset(ps, 0, sizeof (pool_scan_stat_t)); 2606 2607 /* data stored on disk */ 2608 ps->pss_func = scn->scn_phys.scn_func; 2609 ps->pss_state = scn->scn_phys.scn_state; 2610 ps->pss_start_time = scn->scn_phys.scn_start_time; 2611 ps->pss_end_time = scn->scn_phys.scn_end_time; 2612 ps->pss_to_examine = scn->scn_phys.scn_to_examine; 2613 ps->pss_examined = scn->scn_phys.scn_examined; 2614 ps->pss_to_process = scn->scn_phys.scn_to_process; 2615 ps->pss_processed = scn->scn_phys.scn_processed; 2616 ps->pss_errors = scn->scn_phys.scn_errors; 2617 2618 /* data not stored on disk */ 2619 ps->pss_pass_exam = spa->spa_scan_pass_exam; 2620 ps->pss_pass_start = spa->spa_scan_pass_start; 2621 ps->pss_pass_scrub_pause = spa->spa_scan_pass_scrub_pause; 2622 ps->pss_pass_scrub_spent_paused = spa->spa_scan_pass_scrub_spent_paused; 2623 ps->pss_pass_issued = spa->spa_scan_pass_issued; 2624 ps->pss_issued = 2625 scn->scn_issued_before_pass + spa->spa_scan_pass_issued; 2626 2627 /* error scrub data stored on disk */ 2628 ps->pss_error_scrub_func = scn->errorscrub_phys.dep_func; 2629 ps->pss_error_scrub_state = scn->errorscrub_phys.dep_state; 2630 ps->pss_error_scrub_start = scn->errorscrub_phys.dep_start_time; 2631 ps->pss_error_scrub_end = scn->errorscrub_phys.dep_end_time; 2632 ps->pss_error_scrub_examined = scn->errorscrub_phys.dep_examined; 2633 ps->pss_error_scrub_to_be_examined = 2634 scn->errorscrub_phys.dep_to_examine; 2635 2636 /* error scrub data not stored on disk */ 2637 ps->pss_pass_error_scrub_pause = spa->spa_scan_pass_errorscrub_pause; 2638 2639 return (0); 2640 } 2641 2642 int 2643 spa_maxblocksize(spa_t *spa) 2644 { 2645 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) 2646 return (SPA_MAXBLOCKSIZE); 2647 else 2648 return (SPA_OLD_MAXBLOCKSIZE); 2649 } 2650 2651 2652 /* 2653 * Returns the txg that the last device removal completed. No indirect mappings 2654 * have been added since this txg. 2655 */ 2656 uint64_t 2657 spa_get_last_removal_txg(spa_t *spa) 2658 { 2659 uint64_t vdevid; 2660 uint64_t ret = -1ULL; 2661 2662 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 2663 /* 2664 * sr_prev_indirect_vdev is only modified while holding all the 2665 * config locks, so it is sufficient to hold SCL_VDEV as reader when 2666 * examining it. 2667 */ 2668 vdevid = spa->spa_removing_phys.sr_prev_indirect_vdev; 2669 2670 while (vdevid != -1ULL) { 2671 vdev_t *vd = vdev_lookup_top(spa, vdevid); 2672 vdev_indirect_births_t *vib = vd->vdev_indirect_births; 2673 2674 ASSERT3P(vd->vdev_ops, ==, &vdev_indirect_ops); 2675 2676 /* 2677 * If the removal did not remap any data, we don't care. 2678 */ 2679 if (vdev_indirect_births_count(vib) != 0) { 2680 ret = vdev_indirect_births_last_entry_txg(vib); 2681 break; 2682 } 2683 2684 vdevid = vd->vdev_indirect_config.vic_prev_indirect_vdev; 2685 } 2686 spa_config_exit(spa, SCL_VDEV, FTAG); 2687 2688 IMPLY(ret != -1ULL, 2689 spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 2690 2691 return (ret); 2692 } 2693 2694 int 2695 spa_maxdnodesize(spa_t *spa) 2696 { 2697 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE)) 2698 return (DNODE_MAX_SIZE); 2699 else 2700 return (DNODE_MIN_SIZE); 2701 } 2702 2703 boolean_t 2704 spa_multihost(spa_t *spa) 2705 { 2706 return (spa->spa_multihost ? B_TRUE : B_FALSE); 2707 } 2708 2709 uint32_t 2710 spa_get_hostid(spa_t *spa) 2711 { 2712 return (spa->spa_hostid); 2713 } 2714 2715 boolean_t 2716 spa_trust_config(spa_t *spa) 2717 { 2718 return (spa->spa_trust_config); 2719 } 2720 2721 uint64_t 2722 spa_missing_tvds_allowed(spa_t *spa) 2723 { 2724 return (spa->spa_missing_tvds_allowed); 2725 } 2726 2727 space_map_t * 2728 spa_syncing_log_sm(spa_t *spa) 2729 { 2730 return (spa->spa_syncing_log_sm); 2731 } 2732 2733 void 2734 spa_set_missing_tvds(spa_t *spa, uint64_t missing) 2735 { 2736 spa->spa_missing_tvds = missing; 2737 } 2738 2739 /* 2740 * Return the pool state string ("ONLINE", "DEGRADED", "SUSPENDED", etc). 2741 */ 2742 const char * 2743 spa_state_to_name(spa_t *spa) 2744 { 2745 ASSERT3P(spa, !=, NULL); 2746 2747 /* 2748 * it is possible for the spa to exist, without root vdev 2749 * as the spa transitions during import/export 2750 */ 2751 vdev_t *rvd = spa->spa_root_vdev; 2752 if (rvd == NULL) { 2753 return ("TRANSITIONING"); 2754 } 2755 vdev_state_t state = rvd->vdev_state; 2756 vdev_aux_t aux = rvd->vdev_stat.vs_aux; 2757 2758 if (spa_suspended(spa) && 2759 (spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)) 2760 return ("SUSPENDED"); 2761 2762 switch (state) { 2763 case VDEV_STATE_CLOSED: 2764 case VDEV_STATE_OFFLINE: 2765 return ("OFFLINE"); 2766 case VDEV_STATE_REMOVED: 2767 return ("REMOVED"); 2768 case VDEV_STATE_CANT_OPEN: 2769 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG) 2770 return ("FAULTED"); 2771 else if (aux == VDEV_AUX_SPLIT_POOL) 2772 return ("SPLIT"); 2773 else 2774 return ("UNAVAIL"); 2775 case VDEV_STATE_FAULTED: 2776 return ("FAULTED"); 2777 case VDEV_STATE_DEGRADED: 2778 return ("DEGRADED"); 2779 case VDEV_STATE_HEALTHY: 2780 return ("ONLINE"); 2781 default: 2782 break; 2783 } 2784 2785 return ("UNKNOWN"); 2786 } 2787 2788 boolean_t 2789 spa_top_vdevs_spacemap_addressable(spa_t *spa) 2790 { 2791 vdev_t *rvd = spa->spa_root_vdev; 2792 for (uint64_t c = 0; c < rvd->vdev_children; c++) { 2793 if (!vdev_is_spacemap_addressable(rvd->vdev_child[c])) 2794 return (B_FALSE); 2795 } 2796 return (B_TRUE); 2797 } 2798 2799 boolean_t 2800 spa_has_checkpoint(spa_t *spa) 2801 { 2802 return (spa->spa_checkpoint_txg != 0); 2803 } 2804 2805 boolean_t 2806 spa_importing_readonly_checkpoint(spa_t *spa) 2807 { 2808 return ((spa->spa_import_flags & ZFS_IMPORT_CHECKPOINT) && 2809 spa->spa_mode == SPA_MODE_READ); 2810 } 2811 2812 uint64_t 2813 spa_min_claim_txg(spa_t *spa) 2814 { 2815 uint64_t checkpoint_txg = spa->spa_uberblock.ub_checkpoint_txg; 2816 2817 if (checkpoint_txg != 0) 2818 return (checkpoint_txg + 1); 2819 2820 return (spa->spa_first_txg); 2821 } 2822 2823 /* 2824 * If there is a checkpoint, async destroys may consume more space from 2825 * the pool instead of freeing it. In an attempt to save the pool from 2826 * getting suspended when it is about to run out of space, we stop 2827 * processing async destroys. 2828 */ 2829 boolean_t 2830 spa_suspend_async_destroy(spa_t *spa) 2831 { 2832 dsl_pool_t *dp = spa_get_dsl(spa); 2833 2834 uint64_t unreserved = dsl_pool_unreserved_space(dp, 2835 ZFS_SPACE_CHECK_EXTRA_RESERVED); 2836 uint64_t used = dsl_dir_phys(dp->dp_root_dir)->dd_used_bytes; 2837 uint64_t avail = (unreserved > used) ? (unreserved - used) : 0; 2838 2839 if (spa_has_checkpoint(spa) && avail == 0) 2840 return (B_TRUE); 2841 2842 return (B_FALSE); 2843 } 2844 2845 #if defined(_KERNEL) 2846 2847 int 2848 param_set_deadman_failmode_common(const char *val) 2849 { 2850 spa_t *spa = NULL; 2851 char *p; 2852 2853 if (val == NULL) 2854 return (SET_ERROR(EINVAL)); 2855 2856 if ((p = strchr(val, '\n')) != NULL) 2857 *p = '\0'; 2858 2859 if (strcmp(val, "wait") != 0 && strcmp(val, "continue") != 0 && 2860 strcmp(val, "panic")) 2861 return (SET_ERROR(EINVAL)); 2862 2863 if (spa_mode_global != SPA_MODE_UNINIT) { 2864 mutex_enter(&spa_namespace_lock); 2865 while ((spa = spa_next(spa)) != NULL) 2866 spa_set_deadman_failmode(spa, val); 2867 mutex_exit(&spa_namespace_lock); 2868 } 2869 2870 return (0); 2871 } 2872 #endif 2873 2874 /* Namespace manipulation */ 2875 EXPORT_SYMBOL(spa_lookup); 2876 EXPORT_SYMBOL(spa_add); 2877 EXPORT_SYMBOL(spa_remove); 2878 EXPORT_SYMBOL(spa_next); 2879 2880 /* Refcount functions */ 2881 EXPORT_SYMBOL(spa_open_ref); 2882 EXPORT_SYMBOL(spa_close); 2883 EXPORT_SYMBOL(spa_refcount_zero); 2884 2885 /* Pool configuration lock */ 2886 EXPORT_SYMBOL(spa_config_tryenter); 2887 EXPORT_SYMBOL(spa_config_enter); 2888 EXPORT_SYMBOL(spa_config_exit); 2889 EXPORT_SYMBOL(spa_config_held); 2890 2891 /* Pool vdev add/remove lock */ 2892 EXPORT_SYMBOL(spa_vdev_enter); 2893 EXPORT_SYMBOL(spa_vdev_exit); 2894 2895 /* Pool vdev state change lock */ 2896 EXPORT_SYMBOL(spa_vdev_state_enter); 2897 EXPORT_SYMBOL(spa_vdev_state_exit); 2898 2899 /* Accessor functions */ 2900 EXPORT_SYMBOL(spa_shutting_down); 2901 EXPORT_SYMBOL(spa_get_dsl); 2902 EXPORT_SYMBOL(spa_get_rootblkptr); 2903 EXPORT_SYMBOL(spa_set_rootblkptr); 2904 EXPORT_SYMBOL(spa_altroot); 2905 EXPORT_SYMBOL(spa_sync_pass); 2906 EXPORT_SYMBOL(spa_name); 2907 EXPORT_SYMBOL(spa_guid); 2908 EXPORT_SYMBOL(spa_last_synced_txg); 2909 EXPORT_SYMBOL(spa_first_txg); 2910 EXPORT_SYMBOL(spa_syncing_txg); 2911 EXPORT_SYMBOL(spa_version); 2912 EXPORT_SYMBOL(spa_state); 2913 EXPORT_SYMBOL(spa_load_state); 2914 EXPORT_SYMBOL(spa_freeze_txg); 2915 EXPORT_SYMBOL(spa_get_dspace); 2916 EXPORT_SYMBOL(spa_update_dspace); 2917 EXPORT_SYMBOL(spa_deflate); 2918 EXPORT_SYMBOL(spa_normal_class); 2919 EXPORT_SYMBOL(spa_log_class); 2920 EXPORT_SYMBOL(spa_special_class); 2921 EXPORT_SYMBOL(spa_preferred_class); 2922 EXPORT_SYMBOL(spa_max_replication); 2923 EXPORT_SYMBOL(spa_prev_software_version); 2924 EXPORT_SYMBOL(spa_get_failmode); 2925 EXPORT_SYMBOL(spa_suspended); 2926 EXPORT_SYMBOL(spa_bootfs); 2927 EXPORT_SYMBOL(spa_delegation); 2928 EXPORT_SYMBOL(spa_meta_objset); 2929 EXPORT_SYMBOL(spa_maxblocksize); 2930 EXPORT_SYMBOL(spa_maxdnodesize); 2931 2932 /* Miscellaneous support routines */ 2933 EXPORT_SYMBOL(spa_guid_exists); 2934 EXPORT_SYMBOL(spa_strdup); 2935 EXPORT_SYMBOL(spa_strfree); 2936 EXPORT_SYMBOL(spa_generate_guid); 2937 EXPORT_SYMBOL(snprintf_blkptr); 2938 EXPORT_SYMBOL(spa_freeze); 2939 EXPORT_SYMBOL(spa_upgrade); 2940 EXPORT_SYMBOL(spa_evict_all); 2941 EXPORT_SYMBOL(spa_lookup_by_guid); 2942 EXPORT_SYMBOL(spa_has_spare); 2943 EXPORT_SYMBOL(dva_get_dsize_sync); 2944 EXPORT_SYMBOL(bp_get_dsize_sync); 2945 EXPORT_SYMBOL(bp_get_dsize); 2946 EXPORT_SYMBOL(spa_has_slogs); 2947 EXPORT_SYMBOL(spa_is_root); 2948 EXPORT_SYMBOL(spa_writeable); 2949 EXPORT_SYMBOL(spa_mode); 2950 EXPORT_SYMBOL(spa_namespace_lock); 2951 EXPORT_SYMBOL(spa_trust_config); 2952 EXPORT_SYMBOL(spa_missing_tvds_allowed); 2953 EXPORT_SYMBOL(spa_set_missing_tvds); 2954 EXPORT_SYMBOL(spa_state_to_name); 2955 EXPORT_SYMBOL(spa_importing_readonly_checkpoint); 2956 EXPORT_SYMBOL(spa_min_claim_txg); 2957 EXPORT_SYMBOL(spa_suspend_async_destroy); 2958 EXPORT_SYMBOL(spa_has_checkpoint); 2959 EXPORT_SYMBOL(spa_top_vdevs_spacemap_addressable); 2960 2961 ZFS_MODULE_PARAM(zfs, zfs_, flags, UINT, ZMOD_RW, 2962 "Set additional debugging flags"); 2963 2964 ZFS_MODULE_PARAM(zfs, zfs_, recover, INT, ZMOD_RW, 2965 "Set to attempt to recover from fatal errors"); 2966 2967 ZFS_MODULE_PARAM(zfs, zfs_, free_leak_on_eio, INT, ZMOD_RW, 2968 "Set to ignore IO errors during free and permanently leak the space"); 2969 2970 ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, checktime_ms, U64, ZMOD_RW, 2971 "Dead I/O check interval in milliseconds"); 2972 2973 ZFS_MODULE_PARAM(zfs_deadman, zfs_deadman_, enabled, INT, ZMOD_RW, 2974 "Enable deadman timer"); 2975 2976 ZFS_MODULE_PARAM(zfs_spa, spa_, asize_inflation, UINT, ZMOD_RW, 2977 "SPA size estimate multiplication factor"); 2978 2979 ZFS_MODULE_PARAM(zfs, zfs_, ddt_data_is_special, INT, ZMOD_RW, 2980 "Place DDT data into the special class"); 2981 2982 ZFS_MODULE_PARAM(zfs, zfs_, user_indirect_is_special, INT, ZMOD_RW, 2983 "Place user data indirect blocks into the special class"); 2984 2985 /* BEGIN CSTYLED */ 2986 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, failmode, 2987 param_set_deadman_failmode, param_get_charp, ZMOD_RW, 2988 "Failmode for deadman timer"); 2989 2990 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, synctime_ms, 2991 param_set_deadman_synctime, spl_param_get_u64, ZMOD_RW, 2992 "Pool sync expiration time in milliseconds"); 2993 2994 ZFS_MODULE_PARAM_CALL(zfs_deadman, zfs_deadman_, ziotime_ms, 2995 param_set_deadman_ziotime, spl_param_get_u64, ZMOD_RW, 2996 "IO expiration time in milliseconds"); 2997 2998 ZFS_MODULE_PARAM(zfs, zfs_, special_class_metadata_reserve_pct, UINT, ZMOD_RW, 2999 "Small file blocks in special vdevs depends on this much " 3000 "free space available"); 3001 /* END CSTYLED */ 3002 3003 ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift, 3004 param_get_uint, ZMOD_RW, "Reserved free space in pool"); 3005