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