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) 2018, Joyent, Inc. 24 * Copyright (c) 2011, 2020, Delphix. All rights reserved. 25 * Copyright (c) 2014, Saso Kiselkov. All rights reserved. 26 * Copyright (c) 2017, Nexenta Systems, Inc. All rights reserved. 27 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. 28 * Copyright (c) 2020, George Amanakis. All rights reserved. 29 * Copyright (c) 2019, Klara Inc. 30 * Copyright (c) 2019, Allan Jude 31 * Copyright (c) 2020, The FreeBSD Foundation [1] 32 * 33 * [1] Portions of this software were developed by Allan Jude 34 * under sponsorship from the FreeBSD Foundation. 35 */ 36 37 /* 38 * DVA-based Adjustable Replacement Cache 39 * 40 * While much of the theory of operation used here is 41 * based on the self-tuning, low overhead replacement cache 42 * presented by Megiddo and Modha at FAST 2003, there are some 43 * significant differences: 44 * 45 * 1. The Megiddo and Modha model assumes any page is evictable. 46 * Pages in its cache cannot be "locked" into memory. This makes 47 * the eviction algorithm simple: evict the last page in the list. 48 * This also make the performance characteristics easy to reason 49 * about. Our cache is not so simple. At any given moment, some 50 * subset of the blocks in the cache are un-evictable because we 51 * have handed out a reference to them. Blocks are only evictable 52 * when there are no external references active. This makes 53 * eviction far more problematic: we choose to evict the evictable 54 * blocks that are the "lowest" in the list. 55 * 56 * There are times when it is not possible to evict the requested 57 * space. In these circumstances we are unable to adjust the cache 58 * size. To prevent the cache growing unbounded at these times we 59 * implement a "cache throttle" that slows the flow of new data 60 * into the cache until we can make space available. 61 * 62 * 2. The Megiddo and Modha model assumes a fixed cache size. 63 * Pages are evicted when the cache is full and there is a cache 64 * miss. Our model has a variable sized cache. It grows with 65 * high use, but also tries to react to memory pressure from the 66 * operating system: decreasing its size when system memory is 67 * tight. 68 * 69 * 3. The Megiddo and Modha model assumes a fixed page size. All 70 * elements of the cache are therefore exactly the same size. So 71 * when adjusting the cache size following a cache miss, its simply 72 * a matter of choosing a single page to evict. In our model, we 73 * have variable sized cache blocks (ranging from 512 bytes to 74 * 128K bytes). We therefore choose a set of blocks to evict to make 75 * space for a cache miss that approximates as closely as possible 76 * the space used by the new block. 77 * 78 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache" 79 * by N. Megiddo & D. Modha, FAST 2003 80 */ 81 82 /* 83 * The locking model: 84 * 85 * A new reference to a cache buffer can be obtained in two 86 * ways: 1) via a hash table lookup using the DVA as a key, 87 * or 2) via one of the ARC lists. The arc_read() interface 88 * uses method 1, while the internal ARC algorithms for 89 * adjusting the cache use method 2. We therefore provide two 90 * types of locks: 1) the hash table lock array, and 2) the 91 * ARC list locks. 92 * 93 * Buffers do not have their own mutexes, rather they rely on the 94 * hash table mutexes for the bulk of their protection (i.e. most 95 * fields in the arc_buf_hdr_t are protected by these mutexes). 96 * 97 * buf_hash_find() returns the appropriate mutex (held) when it 98 * locates the requested buffer in the hash table. It returns 99 * NULL for the mutex if the buffer was not in the table. 100 * 101 * buf_hash_remove() expects the appropriate hash mutex to be 102 * already held before it is invoked. 103 * 104 * Each ARC state also has a mutex which is used to protect the 105 * buffer list associated with the state. When attempting to 106 * obtain a hash table lock while holding an ARC list lock you 107 * must use: mutex_tryenter() to avoid deadlock. Also note that 108 * the active state mutex must be held before the ghost state mutex. 109 * 110 * It as also possible to register a callback which is run when the 111 * metadata limit is reached and no buffers can be safely evicted. In 112 * this case the arc user should drop a reference on some arc buffers so 113 * they can be reclaimed. For example, when using the ZPL each dentry 114 * holds a references on a znode. These dentries must be pruned before 115 * the arc buffer holding the znode can be safely evicted. 116 * 117 * Note that the majority of the performance stats are manipulated 118 * with atomic operations. 119 * 120 * The L2ARC uses the l2ad_mtx on each vdev for the following: 121 * 122 * - L2ARC buflist creation 123 * - L2ARC buflist eviction 124 * - L2ARC write completion, which walks L2ARC buflists 125 * - ARC header destruction, as it removes from L2ARC buflists 126 * - ARC header release, as it removes from L2ARC buflists 127 */ 128 129 /* 130 * ARC operation: 131 * 132 * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure. 133 * This structure can point either to a block that is still in the cache or to 134 * one that is only accessible in an L2 ARC device, or it can provide 135 * information about a block that was recently evicted. If a block is 136 * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough 137 * information to retrieve it from the L2ARC device. This information is 138 * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block 139 * that is in this state cannot access the data directly. 140 * 141 * Blocks that are actively being referenced or have not been evicted 142 * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within 143 * the arc_buf_hdr_t that will point to the data block in memory. A block can 144 * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC 145 * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and 146 * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd). 147 * 148 * The L1ARC's data pointer may or may not be uncompressed. The ARC has the 149 * ability to store the physical data (b_pabd) associated with the DVA of the 150 * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block, 151 * it will match its on-disk compression characteristics. This behavior can be 152 * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the 153 * compressed ARC functionality is disabled, the b_pabd will point to an 154 * uncompressed version of the on-disk data. 155 * 156 * Data in the L1ARC is not accessed by consumers of the ARC directly. Each 157 * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it. 158 * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC 159 * consumer. The ARC will provide references to this data and will keep it 160 * cached until it is no longer in use. The ARC caches only the L1ARC's physical 161 * data block and will evict any arc_buf_t that is no longer referenced. The 162 * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the 163 * "overhead_size" kstat. 164 * 165 * Depending on the consumer, an arc_buf_t can be requested in uncompressed or 166 * compressed form. The typical case is that consumers will want uncompressed 167 * data, and when that happens a new data buffer is allocated where the data is 168 * decompressed for them to use. Currently the only consumer who wants 169 * compressed arc_buf_t's is "zfs send", when it streams data exactly as it 170 * exists on disk. When this happens, the arc_buf_t's data buffer is shared 171 * with the arc_buf_hdr_t. 172 * 173 * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The 174 * first one is owned by a compressed send consumer (and therefore references 175 * the same compressed data buffer as the arc_buf_hdr_t) and the second could be 176 * used by any other consumer (and has its own uncompressed copy of the data 177 * buffer). 178 * 179 * arc_buf_hdr_t 180 * +-----------+ 181 * | fields | 182 * | common to | 183 * | L1- and | 184 * | L2ARC | 185 * +-----------+ 186 * | l2arc_buf_hdr_t 187 * | | 188 * +-----------+ 189 * | l1arc_buf_hdr_t 190 * | | arc_buf_t 191 * | b_buf +------------>+-----------+ arc_buf_t 192 * | b_pabd +-+ |b_next +---->+-----------+ 193 * +-----------+ | |-----------| |b_next +-->NULL 194 * | |b_comp = T | +-----------+ 195 * | |b_data +-+ |b_comp = F | 196 * | +-----------+ | |b_data +-+ 197 * +->+------+ | +-----------+ | 198 * compressed | | | | 199 * data | |<--------------+ | uncompressed 200 * +------+ compressed, | data 201 * shared +-->+------+ 202 * data | | 203 * | | 204 * +------+ 205 * 206 * When a consumer reads a block, the ARC must first look to see if the 207 * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new 208 * arc_buf_t and either copies uncompressed data into a new data buffer from an 209 * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a 210 * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the 211 * hdr is compressed and the desired compression characteristics of the 212 * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the 213 * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be 214 * the last buffer in the hdr's b_buf list, however a shared compressed buf can 215 * be anywhere in the hdr's list. 216 * 217 * The diagram below shows an example of an uncompressed ARC hdr that is 218 * sharing its data with an arc_buf_t (note that the shared uncompressed buf is 219 * the last element in the buf list): 220 * 221 * arc_buf_hdr_t 222 * +-----------+ 223 * | | 224 * | | 225 * | | 226 * +-----------+ 227 * l2arc_buf_hdr_t| | 228 * | | 229 * +-----------+ 230 * l1arc_buf_hdr_t| | 231 * | | arc_buf_t (shared) 232 * | b_buf +------------>+---------+ arc_buf_t 233 * | | |b_next +---->+---------+ 234 * | b_pabd +-+ |---------| |b_next +-->NULL 235 * +-----------+ | | | +---------+ 236 * | |b_data +-+ | | 237 * | +---------+ | |b_data +-+ 238 * +->+------+ | +---------+ | 239 * | | | | 240 * uncompressed | | | | 241 * data +------+ | | 242 * ^ +->+------+ | 243 * | uncompressed | | | 244 * | data | | | 245 * | +------+ | 246 * +---------------------------------+ 247 * 248 * Writing to the ARC requires that the ARC first discard the hdr's b_pabd 249 * since the physical block is about to be rewritten. The new data contents 250 * will be contained in the arc_buf_t. As the I/O pipeline performs the write, 251 * it may compress the data before writing it to disk. The ARC will be called 252 * with the transformed data and will memcpy the transformed on-disk block into 253 * a newly allocated b_pabd. Writes are always done into buffers which have 254 * either been loaned (and hence are new and don't have other readers) or 255 * buffers which have been released (and hence have their own hdr, if there 256 * were originally other readers of the buf's original hdr). This ensures that 257 * the ARC only needs to update a single buf and its hdr after a write occurs. 258 * 259 * When the L2ARC is in use, it will also take advantage of the b_pabd. The 260 * L2ARC will always write the contents of b_pabd to the L2ARC. This means 261 * that when compressed ARC is enabled that the L2ARC blocks are identical 262 * to the on-disk block in the main data pool. This provides a significant 263 * advantage since the ARC can leverage the bp's checksum when reading from the 264 * L2ARC to determine if the contents are valid. However, if the compressed 265 * ARC is disabled, then the L2ARC's block must be transformed to look 266 * like the physical block in the main data pool before comparing the 267 * checksum and determining its validity. 268 * 269 * The L1ARC has a slightly different system for storing encrypted data. 270 * Raw (encrypted + possibly compressed) data has a few subtle differences from 271 * data that is just compressed. The biggest difference is that it is not 272 * possible to decrypt encrypted data (or vice-versa) if the keys aren't loaded. 273 * The other difference is that encryption cannot be treated as a suggestion. 274 * If a caller would prefer compressed data, but they actually wind up with 275 * uncompressed data the worst thing that could happen is there might be a 276 * performance hit. If the caller requests encrypted data, however, we must be 277 * sure they actually get it or else secret information could be leaked. Raw 278 * data is stored in hdr->b_crypt_hdr.b_rabd. An encrypted header, therefore, 279 * may have both an encrypted version and a decrypted version of its data at 280 * once. When a caller needs a raw arc_buf_t, it is allocated and the data is 281 * copied out of this header. To avoid complications with b_pabd, raw buffers 282 * cannot be shared. 283 */ 284 285 #include <sys/spa.h> 286 #include <sys/zio.h> 287 #include <sys/spa_impl.h> 288 #include <sys/zio_compress.h> 289 #include <sys/zio_checksum.h> 290 #include <sys/zfs_context.h> 291 #include <sys/arc.h> 292 #include <sys/zfs_refcount.h> 293 #include <sys/vdev.h> 294 #include <sys/vdev_impl.h> 295 #include <sys/dsl_pool.h> 296 #include <sys/multilist.h> 297 #include <sys/abd.h> 298 #include <sys/zil.h> 299 #include <sys/fm/fs/zfs.h> 300 #include <sys/callb.h> 301 #include <sys/kstat.h> 302 #include <sys/zthr.h> 303 #include <zfs_fletcher.h> 304 #include <sys/arc_impl.h> 305 #include <sys/trace_zfs.h> 306 #include <sys/aggsum.h> 307 #include <sys/wmsum.h> 308 #include <cityhash.h> 309 #include <sys/vdev_trim.h> 310 #include <sys/zfs_racct.h> 311 #include <sys/zstd/zstd.h> 312 313 #ifndef _KERNEL 314 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */ 315 boolean_t arc_watch = B_FALSE; 316 #endif 317 318 /* 319 * This thread's job is to keep enough free memory in the system, by 320 * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves 321 * arc_available_memory(). 322 */ 323 static zthr_t *arc_reap_zthr; 324 325 /* 326 * This thread's job is to keep arc_size under arc_c, by calling 327 * arc_evict(), which improves arc_is_overflowing(). 328 */ 329 static zthr_t *arc_evict_zthr; 330 static arc_buf_hdr_t **arc_state_evict_markers; 331 static int arc_state_evict_marker_count; 332 333 static kmutex_t arc_evict_lock; 334 static boolean_t arc_evict_needed = B_FALSE; 335 static clock_t arc_last_uncached_flush; 336 337 /* 338 * Count of bytes evicted since boot. 339 */ 340 static uint64_t arc_evict_count; 341 342 /* 343 * List of arc_evict_waiter_t's, representing threads waiting for the 344 * arc_evict_count to reach specific values. 345 */ 346 static list_t arc_evict_waiters; 347 348 /* 349 * When arc_is_overflowing(), arc_get_data_impl() waits for this percent of 350 * the requested amount of data to be evicted. For example, by default for 351 * every 2KB that's evicted, 1KB of it may be "reused" by a new allocation. 352 * Since this is above 100%, it ensures that progress is made towards getting 353 * arc_size under arc_c. Since this is finite, it ensures that allocations 354 * can still happen, even during the potentially long time that arc_size is 355 * more than arc_c. 356 */ 357 static uint_t zfs_arc_eviction_pct = 200; 358 359 /* 360 * The number of headers to evict in arc_evict_state_impl() before 361 * dropping the sublist lock and evicting from another sublist. A lower 362 * value means we're more likely to evict the "correct" header (i.e. the 363 * oldest header in the arc state), but comes with higher overhead 364 * (i.e. more invocations of arc_evict_state_impl()). 365 */ 366 static uint_t zfs_arc_evict_batch_limit = 10; 367 368 /* number of seconds before growing cache again */ 369 uint_t arc_grow_retry = 5; 370 371 /* 372 * Minimum time between calls to arc_kmem_reap_soon(). 373 */ 374 static const int arc_kmem_cache_reap_retry_ms = 1000; 375 376 /* shift of arc_c for calculating overflow limit in arc_get_data_impl */ 377 static int zfs_arc_overflow_shift = 8; 378 379 /* log2(fraction of arc to reclaim) */ 380 uint_t arc_shrink_shift = 7; 381 382 /* percent of pagecache to reclaim arc to */ 383 #ifdef _KERNEL 384 uint_t zfs_arc_pc_percent = 0; 385 #endif 386 387 /* 388 * log2(fraction of ARC which must be free to allow growing). 389 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory, 390 * when reading a new block into the ARC, we will evict an equal-sized block 391 * from the ARC. 392 * 393 * This must be less than arc_shrink_shift, so that when we shrink the ARC, 394 * we will still not allow it to grow. 395 */ 396 uint_t arc_no_grow_shift = 5; 397 398 399 /* 400 * minimum lifespan of a prefetch block in clock ticks 401 * (initialized in arc_init()) 402 */ 403 static uint_t arc_min_prefetch_ms; 404 static uint_t arc_min_prescient_prefetch_ms; 405 406 /* 407 * If this percent of memory is free, don't throttle. 408 */ 409 uint_t arc_lotsfree_percent = 10; 410 411 /* 412 * The arc has filled available memory and has now warmed up. 413 */ 414 boolean_t arc_warm; 415 416 /* 417 * These tunables are for performance analysis. 418 */ 419 uint64_t zfs_arc_max = 0; 420 uint64_t zfs_arc_min = 0; 421 static uint64_t zfs_arc_dnode_limit = 0; 422 static uint_t zfs_arc_dnode_reduce_percent = 10; 423 static uint_t zfs_arc_grow_retry = 0; 424 static uint_t zfs_arc_shrink_shift = 0; 425 uint_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */ 426 427 /* 428 * ARC dirty data constraints for arc_tempreserve_space() throttle: 429 * * total dirty data limit 430 * * anon block dirty limit 431 * * each pool's anon allowance 432 */ 433 static const unsigned long zfs_arc_dirty_limit_percent = 50; 434 static const unsigned long zfs_arc_anon_limit_percent = 25; 435 static const unsigned long zfs_arc_pool_dirty_percent = 20; 436 437 /* 438 * Enable or disable compressed arc buffers. 439 */ 440 int zfs_compressed_arc_enabled = B_TRUE; 441 442 /* 443 * Balance between metadata and data on ghost hits. Values above 100 444 * increase metadata caching by proportionally reducing effect of ghost 445 * data hits on target data/metadata rate. 446 */ 447 static uint_t zfs_arc_meta_balance = 500; 448 449 /* 450 * Percentage that can be consumed by dnodes of ARC meta buffers. 451 */ 452 static uint_t zfs_arc_dnode_limit_percent = 10; 453 454 /* 455 * These tunables are Linux-specific 456 */ 457 static uint64_t zfs_arc_sys_free = 0; 458 static uint_t zfs_arc_min_prefetch_ms = 0; 459 static uint_t zfs_arc_min_prescient_prefetch_ms = 0; 460 static uint_t zfs_arc_lotsfree_percent = 10; 461 462 /* 463 * Number of arc_prune threads 464 */ 465 static int zfs_arc_prune_task_threads = 1; 466 467 /* The 7 states: */ 468 arc_state_t ARC_anon; 469 arc_state_t ARC_mru; 470 arc_state_t ARC_mru_ghost; 471 arc_state_t ARC_mfu; 472 arc_state_t ARC_mfu_ghost; 473 arc_state_t ARC_l2c_only; 474 arc_state_t ARC_uncached; 475 476 arc_stats_t arc_stats = { 477 { "hits", KSTAT_DATA_UINT64 }, 478 { "iohits", KSTAT_DATA_UINT64 }, 479 { "misses", KSTAT_DATA_UINT64 }, 480 { "demand_data_hits", KSTAT_DATA_UINT64 }, 481 { "demand_data_iohits", KSTAT_DATA_UINT64 }, 482 { "demand_data_misses", KSTAT_DATA_UINT64 }, 483 { "demand_metadata_hits", KSTAT_DATA_UINT64 }, 484 { "demand_metadata_iohits", KSTAT_DATA_UINT64 }, 485 { "demand_metadata_misses", KSTAT_DATA_UINT64 }, 486 { "prefetch_data_hits", KSTAT_DATA_UINT64 }, 487 { "prefetch_data_iohits", KSTAT_DATA_UINT64 }, 488 { "prefetch_data_misses", KSTAT_DATA_UINT64 }, 489 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 }, 490 { "prefetch_metadata_iohits", KSTAT_DATA_UINT64 }, 491 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 }, 492 { "mru_hits", KSTAT_DATA_UINT64 }, 493 { "mru_ghost_hits", KSTAT_DATA_UINT64 }, 494 { "mfu_hits", KSTAT_DATA_UINT64 }, 495 { "mfu_ghost_hits", KSTAT_DATA_UINT64 }, 496 { "uncached_hits", KSTAT_DATA_UINT64 }, 497 { "deleted", KSTAT_DATA_UINT64 }, 498 { "mutex_miss", KSTAT_DATA_UINT64 }, 499 { "access_skip", KSTAT_DATA_UINT64 }, 500 { "evict_skip", KSTAT_DATA_UINT64 }, 501 { "evict_not_enough", KSTAT_DATA_UINT64 }, 502 { "evict_l2_cached", KSTAT_DATA_UINT64 }, 503 { "evict_l2_eligible", KSTAT_DATA_UINT64 }, 504 { "evict_l2_eligible_mfu", KSTAT_DATA_UINT64 }, 505 { "evict_l2_eligible_mru", KSTAT_DATA_UINT64 }, 506 { "evict_l2_ineligible", KSTAT_DATA_UINT64 }, 507 { "evict_l2_skip", KSTAT_DATA_UINT64 }, 508 { "hash_elements", KSTAT_DATA_UINT64 }, 509 { "hash_elements_max", KSTAT_DATA_UINT64 }, 510 { "hash_collisions", KSTAT_DATA_UINT64 }, 511 { "hash_chains", KSTAT_DATA_UINT64 }, 512 { "hash_chain_max", KSTAT_DATA_UINT64 }, 513 { "meta", KSTAT_DATA_UINT64 }, 514 { "pd", KSTAT_DATA_UINT64 }, 515 { "pm", KSTAT_DATA_UINT64 }, 516 { "c", KSTAT_DATA_UINT64 }, 517 { "c_min", KSTAT_DATA_UINT64 }, 518 { "c_max", KSTAT_DATA_UINT64 }, 519 { "size", KSTAT_DATA_UINT64 }, 520 { "compressed_size", KSTAT_DATA_UINT64 }, 521 { "uncompressed_size", KSTAT_DATA_UINT64 }, 522 { "overhead_size", KSTAT_DATA_UINT64 }, 523 { "hdr_size", KSTAT_DATA_UINT64 }, 524 { "data_size", KSTAT_DATA_UINT64 }, 525 { "metadata_size", KSTAT_DATA_UINT64 }, 526 { "dbuf_size", KSTAT_DATA_UINT64 }, 527 { "dnode_size", KSTAT_DATA_UINT64 }, 528 { "bonus_size", KSTAT_DATA_UINT64 }, 529 #if defined(COMPAT_FREEBSD11) 530 { "other_size", KSTAT_DATA_UINT64 }, 531 #endif 532 { "anon_size", KSTAT_DATA_UINT64 }, 533 { "anon_data", KSTAT_DATA_UINT64 }, 534 { "anon_metadata", KSTAT_DATA_UINT64 }, 535 { "anon_evictable_data", KSTAT_DATA_UINT64 }, 536 { "anon_evictable_metadata", KSTAT_DATA_UINT64 }, 537 { "mru_size", KSTAT_DATA_UINT64 }, 538 { "mru_data", KSTAT_DATA_UINT64 }, 539 { "mru_metadata", KSTAT_DATA_UINT64 }, 540 { "mru_evictable_data", KSTAT_DATA_UINT64 }, 541 { "mru_evictable_metadata", KSTAT_DATA_UINT64 }, 542 { "mru_ghost_size", KSTAT_DATA_UINT64 }, 543 { "mru_ghost_data", KSTAT_DATA_UINT64 }, 544 { "mru_ghost_metadata", KSTAT_DATA_UINT64 }, 545 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 }, 546 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 547 { "mfu_size", KSTAT_DATA_UINT64 }, 548 { "mfu_data", KSTAT_DATA_UINT64 }, 549 { "mfu_metadata", KSTAT_DATA_UINT64 }, 550 { "mfu_evictable_data", KSTAT_DATA_UINT64 }, 551 { "mfu_evictable_metadata", KSTAT_DATA_UINT64 }, 552 { "mfu_ghost_size", KSTAT_DATA_UINT64 }, 553 { "mfu_ghost_data", KSTAT_DATA_UINT64 }, 554 { "mfu_ghost_metadata", KSTAT_DATA_UINT64 }, 555 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 }, 556 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 }, 557 { "uncached_size", KSTAT_DATA_UINT64 }, 558 { "uncached_data", KSTAT_DATA_UINT64 }, 559 { "uncached_metadata", KSTAT_DATA_UINT64 }, 560 { "uncached_evictable_data", KSTAT_DATA_UINT64 }, 561 { "uncached_evictable_metadata", KSTAT_DATA_UINT64 }, 562 { "l2_hits", KSTAT_DATA_UINT64 }, 563 { "l2_misses", KSTAT_DATA_UINT64 }, 564 { "l2_prefetch_asize", KSTAT_DATA_UINT64 }, 565 { "l2_mru_asize", KSTAT_DATA_UINT64 }, 566 { "l2_mfu_asize", KSTAT_DATA_UINT64 }, 567 { "l2_bufc_data_asize", KSTAT_DATA_UINT64 }, 568 { "l2_bufc_metadata_asize", KSTAT_DATA_UINT64 }, 569 { "l2_feeds", KSTAT_DATA_UINT64 }, 570 { "l2_rw_clash", KSTAT_DATA_UINT64 }, 571 { "l2_read_bytes", KSTAT_DATA_UINT64 }, 572 { "l2_write_bytes", KSTAT_DATA_UINT64 }, 573 { "l2_writes_sent", KSTAT_DATA_UINT64 }, 574 { "l2_writes_done", KSTAT_DATA_UINT64 }, 575 { "l2_writes_error", KSTAT_DATA_UINT64 }, 576 { "l2_writes_lock_retry", KSTAT_DATA_UINT64 }, 577 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 }, 578 { "l2_evict_reading", KSTAT_DATA_UINT64 }, 579 { "l2_evict_l1cached", KSTAT_DATA_UINT64 }, 580 { "l2_free_on_write", KSTAT_DATA_UINT64 }, 581 { "l2_abort_lowmem", KSTAT_DATA_UINT64 }, 582 { "l2_cksum_bad", KSTAT_DATA_UINT64 }, 583 { "l2_io_error", KSTAT_DATA_UINT64 }, 584 { "l2_size", KSTAT_DATA_UINT64 }, 585 { "l2_asize", KSTAT_DATA_UINT64 }, 586 { "l2_hdr_size", KSTAT_DATA_UINT64 }, 587 { "l2_log_blk_writes", KSTAT_DATA_UINT64 }, 588 { "l2_log_blk_avg_asize", KSTAT_DATA_UINT64 }, 589 { "l2_log_blk_asize", KSTAT_DATA_UINT64 }, 590 { "l2_log_blk_count", KSTAT_DATA_UINT64 }, 591 { "l2_data_to_meta_ratio", KSTAT_DATA_UINT64 }, 592 { "l2_rebuild_success", KSTAT_DATA_UINT64 }, 593 { "l2_rebuild_unsupported", KSTAT_DATA_UINT64 }, 594 { "l2_rebuild_io_errors", KSTAT_DATA_UINT64 }, 595 { "l2_rebuild_dh_errors", KSTAT_DATA_UINT64 }, 596 { "l2_rebuild_cksum_lb_errors", KSTAT_DATA_UINT64 }, 597 { "l2_rebuild_lowmem", KSTAT_DATA_UINT64 }, 598 { "l2_rebuild_size", KSTAT_DATA_UINT64 }, 599 { "l2_rebuild_asize", KSTAT_DATA_UINT64 }, 600 { "l2_rebuild_bufs", KSTAT_DATA_UINT64 }, 601 { "l2_rebuild_bufs_precached", KSTAT_DATA_UINT64 }, 602 { "l2_rebuild_log_blks", KSTAT_DATA_UINT64 }, 603 { "memory_throttle_count", KSTAT_DATA_UINT64 }, 604 { "memory_direct_count", KSTAT_DATA_UINT64 }, 605 { "memory_indirect_count", KSTAT_DATA_UINT64 }, 606 { "memory_all_bytes", KSTAT_DATA_UINT64 }, 607 { "memory_free_bytes", KSTAT_DATA_UINT64 }, 608 { "memory_available_bytes", KSTAT_DATA_INT64 }, 609 { "arc_no_grow", KSTAT_DATA_UINT64 }, 610 { "arc_tempreserve", KSTAT_DATA_UINT64 }, 611 { "arc_loaned_bytes", KSTAT_DATA_UINT64 }, 612 { "arc_prune", KSTAT_DATA_UINT64 }, 613 { "arc_meta_used", KSTAT_DATA_UINT64 }, 614 { "arc_dnode_limit", KSTAT_DATA_UINT64 }, 615 { "async_upgrade_sync", KSTAT_DATA_UINT64 }, 616 { "predictive_prefetch", KSTAT_DATA_UINT64 }, 617 { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 }, 618 { "demand_iohit_predictive_prefetch", KSTAT_DATA_UINT64 }, 619 { "prescient_prefetch", KSTAT_DATA_UINT64 }, 620 { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64 }, 621 { "demand_iohit_prescient_prefetch", KSTAT_DATA_UINT64 }, 622 { "arc_need_free", KSTAT_DATA_UINT64 }, 623 { "arc_sys_free", KSTAT_DATA_UINT64 }, 624 { "arc_raw_size", KSTAT_DATA_UINT64 }, 625 { "cached_only_in_progress", KSTAT_DATA_UINT64 }, 626 { "abd_chunk_waste_size", KSTAT_DATA_UINT64 }, 627 }; 628 629 arc_sums_t arc_sums; 630 631 #define ARCSTAT_MAX(stat, val) { \ 632 uint64_t m; \ 633 while ((val) > (m = arc_stats.stat.value.ui64) && \ 634 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \ 635 continue; \ 636 } 637 638 /* 639 * We define a macro to allow ARC hits/misses to be easily broken down by 640 * two separate conditions, giving a total of four different subtypes for 641 * each of hits and misses (so eight statistics total). 642 */ 643 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \ 644 if (cond1) { \ 645 if (cond2) { \ 646 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \ 647 } else { \ 648 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \ 649 } \ 650 } else { \ 651 if (cond2) { \ 652 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \ 653 } else { \ 654 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\ 655 } \ 656 } 657 658 /* 659 * This macro allows us to use kstats as floating averages. Each time we 660 * update this kstat, we first factor it and the update value by 661 * ARCSTAT_AVG_FACTOR to shrink the new value's contribution to the overall 662 * average. This macro assumes that integer loads and stores are atomic, but 663 * is not safe for multiple writers updating the kstat in parallel (only the 664 * last writer's update will remain). 665 */ 666 #define ARCSTAT_F_AVG_FACTOR 3 667 #define ARCSTAT_F_AVG(stat, value) \ 668 do { \ 669 uint64_t x = ARCSTAT(stat); \ 670 x = x - x / ARCSTAT_F_AVG_FACTOR + \ 671 (value) / ARCSTAT_F_AVG_FACTOR; \ 672 ARCSTAT(stat) = x; \ 673 } while (0) 674 675 static kstat_t *arc_ksp; 676 677 /* 678 * There are several ARC variables that are critical to export as kstats -- 679 * but we don't want to have to grovel around in the kstat whenever we wish to 680 * manipulate them. For these variables, we therefore define them to be in 681 * terms of the statistic variable. This assures that we are not introducing 682 * the possibility of inconsistency by having shadow copies of the variables, 683 * while still allowing the code to be readable. 684 */ 685 #define arc_tempreserve ARCSTAT(arcstat_tempreserve) 686 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes) 687 #define arc_dnode_limit ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */ 688 #define arc_need_free ARCSTAT(arcstat_need_free) /* waiting to be evicted */ 689 690 hrtime_t arc_growtime; 691 list_t arc_prune_list; 692 kmutex_t arc_prune_mtx; 693 taskq_t *arc_prune_taskq; 694 695 #define GHOST_STATE(state) \ 696 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \ 697 (state) == arc_l2c_only) 698 699 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE) 700 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) 701 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR) 702 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH) 703 #define HDR_PRESCIENT_PREFETCH(hdr) \ 704 ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH) 705 #define HDR_COMPRESSION_ENABLED(hdr) \ 706 ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC) 707 708 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE) 709 #define HDR_UNCACHED(hdr) ((hdr)->b_flags & ARC_FLAG_UNCACHED) 710 #define HDR_L2_READING(hdr) \ 711 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \ 712 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)) 713 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING) 714 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED) 715 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD) 716 #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED) 717 #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH) 718 #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA) 719 720 #define HDR_ISTYPE_METADATA(hdr) \ 721 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA) 722 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr)) 723 724 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR) 725 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR) 726 #define HDR_HAS_RABD(hdr) \ 727 (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \ 728 (hdr)->b_crypt_hdr.b_rabd != NULL) 729 #define HDR_ENCRYPTED(hdr) \ 730 (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot)) 731 #define HDR_AUTHENTICATED(hdr) \ 732 (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot)) 733 734 /* For storing compression mode in b_flags */ 735 #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1) 736 737 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \ 738 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS)) 739 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \ 740 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp)); 741 742 #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL) 743 #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED) 744 #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED) 745 #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED) 746 747 /* 748 * Other sizes 749 */ 750 751 #define HDR_FULL_CRYPT_SIZE ((int64_t)sizeof (arc_buf_hdr_t)) 752 #define HDR_FULL_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_crypt_hdr)) 753 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr)) 754 755 /* 756 * Hash table routines 757 */ 758 759 #define BUF_LOCKS 2048 760 typedef struct buf_hash_table { 761 uint64_t ht_mask; 762 arc_buf_hdr_t **ht_table; 763 kmutex_t ht_locks[BUF_LOCKS] ____cacheline_aligned; 764 } buf_hash_table_t; 765 766 static buf_hash_table_t buf_hash_table; 767 768 #define BUF_HASH_INDEX(spa, dva, birth) \ 769 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask) 770 #define BUF_HASH_LOCK(idx) (&buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)]) 771 #define HDR_LOCK(hdr) \ 772 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth))) 773 774 uint64_t zfs_crc64_table[256]; 775 776 /* 777 * Level 2 ARC 778 */ 779 780 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */ 781 #define L2ARC_HEADROOM 2 /* num of writes */ 782 783 /* 784 * If we discover during ARC scan any buffers to be compressed, we boost 785 * our headroom for the next scanning cycle by this percentage multiple. 786 */ 787 #define L2ARC_HEADROOM_BOOST 200 788 #define L2ARC_FEED_SECS 1 /* caching interval secs */ 789 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */ 790 791 /* 792 * We can feed L2ARC from two states of ARC buffers, mru and mfu, 793 * and each of the state has two types: data and metadata. 794 */ 795 #define L2ARC_FEED_TYPES 4 796 797 /* L2ARC Performance Tunables */ 798 uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */ 799 uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */ 800 uint64_t l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */ 801 uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST; 802 uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */ 803 uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */ 804 int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */ 805 int l2arc_feed_again = B_TRUE; /* turbo warmup */ 806 int l2arc_norw = B_FALSE; /* no reads during writes */ 807 static uint_t l2arc_meta_percent = 33; /* limit on headers size */ 808 809 /* 810 * L2ARC Internals 811 */ 812 static list_t L2ARC_dev_list; /* device list */ 813 static list_t *l2arc_dev_list; /* device list pointer */ 814 static kmutex_t l2arc_dev_mtx; /* device list mutex */ 815 static l2arc_dev_t *l2arc_dev_last; /* last device used */ 816 static list_t L2ARC_free_on_write; /* free after write buf list */ 817 static list_t *l2arc_free_on_write; /* free after write list ptr */ 818 static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */ 819 static uint64_t l2arc_ndev; /* number of devices */ 820 821 typedef struct l2arc_read_callback { 822 arc_buf_hdr_t *l2rcb_hdr; /* read header */ 823 blkptr_t l2rcb_bp; /* original blkptr */ 824 zbookmark_phys_t l2rcb_zb; /* original bookmark */ 825 int l2rcb_flags; /* original flags */ 826 abd_t *l2rcb_abd; /* temporary buffer */ 827 } l2arc_read_callback_t; 828 829 typedef struct l2arc_data_free { 830 /* protected by l2arc_free_on_write_mtx */ 831 abd_t *l2df_abd; 832 size_t l2df_size; 833 arc_buf_contents_t l2df_type; 834 list_node_t l2df_list_node; 835 } l2arc_data_free_t; 836 837 typedef enum arc_fill_flags { 838 ARC_FILL_LOCKED = 1 << 0, /* hdr lock is held */ 839 ARC_FILL_COMPRESSED = 1 << 1, /* fill with compressed data */ 840 ARC_FILL_ENCRYPTED = 1 << 2, /* fill with encrypted data */ 841 ARC_FILL_NOAUTH = 1 << 3, /* don't attempt to authenticate */ 842 ARC_FILL_IN_PLACE = 1 << 4 /* fill in place (special case) */ 843 } arc_fill_flags_t; 844 845 typedef enum arc_ovf_level { 846 ARC_OVF_NONE, /* ARC within target size. */ 847 ARC_OVF_SOME, /* ARC is slightly overflowed. */ 848 ARC_OVF_SEVERE /* ARC is severely overflowed. */ 849 } arc_ovf_level_t; 850 851 static kmutex_t l2arc_feed_thr_lock; 852 static kcondvar_t l2arc_feed_thr_cv; 853 static uint8_t l2arc_thread_exit; 854 855 static kmutex_t l2arc_rebuild_thr_lock; 856 static kcondvar_t l2arc_rebuild_thr_cv; 857 858 enum arc_hdr_alloc_flags { 859 ARC_HDR_ALLOC_RDATA = 0x1, 860 ARC_HDR_USE_RESERVE = 0x4, 861 ARC_HDR_ALLOC_LINEAR = 0x8, 862 }; 863 864 865 static abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, const void *, int); 866 static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, const void *); 867 static void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, const void *, int); 868 static void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, const void *); 869 static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, const void *); 870 static void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, 871 const void *tag); 872 static void arc_hdr_free_abd(arc_buf_hdr_t *, boolean_t); 873 static void arc_hdr_alloc_abd(arc_buf_hdr_t *, int); 874 static void arc_hdr_destroy(arc_buf_hdr_t *); 875 static void arc_access(arc_buf_hdr_t *, arc_flags_t, boolean_t); 876 static void arc_buf_watch(arc_buf_t *); 877 static void arc_change_state(arc_state_t *, arc_buf_hdr_t *); 878 879 static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *); 880 static uint32_t arc_bufc_to_flags(arc_buf_contents_t); 881 static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 882 static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags); 883 884 static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *); 885 static void l2arc_read_done(zio_t *); 886 static void l2arc_do_free_on_write(void); 887 static void l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr, 888 boolean_t state_only); 889 890 #define l2arc_hdr_arcstats_increment(hdr) \ 891 l2arc_hdr_arcstats_update((hdr), B_TRUE, B_FALSE) 892 #define l2arc_hdr_arcstats_decrement(hdr) \ 893 l2arc_hdr_arcstats_update((hdr), B_FALSE, B_FALSE) 894 #define l2arc_hdr_arcstats_increment_state(hdr) \ 895 l2arc_hdr_arcstats_update((hdr), B_TRUE, B_TRUE) 896 #define l2arc_hdr_arcstats_decrement_state(hdr) \ 897 l2arc_hdr_arcstats_update((hdr), B_FALSE, B_TRUE) 898 899 /* 900 * l2arc_exclude_special : A zfs module parameter that controls whether buffers 901 * present on special vdevs are eligibile for caching in L2ARC. If 902 * set to 1, exclude dbufs on special vdevs from being cached to 903 * L2ARC. 904 */ 905 int l2arc_exclude_special = 0; 906 907 /* 908 * l2arc_mfuonly : A ZFS module parameter that controls whether only MFU 909 * metadata and data are cached from ARC into L2ARC. 910 */ 911 static int l2arc_mfuonly = 0; 912 913 /* 914 * L2ARC TRIM 915 * l2arc_trim_ahead : A ZFS module parameter that controls how much ahead of 916 * the current write size (l2arc_write_max) we should TRIM if we 917 * have filled the device. It is defined as a percentage of the 918 * write size. If set to 100 we trim twice the space required to 919 * accommodate upcoming writes. A minimum of 64MB will be trimmed. 920 * It also enables TRIM of the whole L2ARC device upon creation or 921 * addition to an existing pool or if the header of the device is 922 * invalid upon importing a pool or onlining a cache device. The 923 * default is 0, which disables TRIM on L2ARC altogether as it can 924 * put significant stress on the underlying storage devices. This 925 * will vary depending of how well the specific device handles 926 * these commands. 927 */ 928 static uint64_t l2arc_trim_ahead = 0; 929 930 /* 931 * Performance tuning of L2ARC persistence: 932 * 933 * l2arc_rebuild_enabled : A ZFS module parameter that controls whether adding 934 * an L2ARC device (either at pool import or later) will attempt 935 * to rebuild L2ARC buffer contents. 936 * l2arc_rebuild_blocks_min_l2size : A ZFS module parameter that controls 937 * whether log blocks are written to the L2ARC device. If the L2ARC 938 * device is less than 1GB, the amount of data l2arc_evict() 939 * evicts is significant compared to the amount of restored L2ARC 940 * data. In this case do not write log blocks in L2ARC in order 941 * not to waste space. 942 */ 943 static int l2arc_rebuild_enabled = B_TRUE; 944 static uint64_t l2arc_rebuild_blocks_min_l2size = 1024 * 1024 * 1024; 945 946 /* L2ARC persistence rebuild control routines. */ 947 void l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen); 948 static __attribute__((noreturn)) void l2arc_dev_rebuild_thread(void *arg); 949 static int l2arc_rebuild(l2arc_dev_t *dev); 950 951 /* L2ARC persistence read I/O routines. */ 952 static int l2arc_dev_hdr_read(l2arc_dev_t *dev); 953 static int l2arc_log_blk_read(l2arc_dev_t *dev, 954 const l2arc_log_blkptr_t *this_lp, const l2arc_log_blkptr_t *next_lp, 955 l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb, 956 zio_t *this_io, zio_t **next_io); 957 static zio_t *l2arc_log_blk_fetch(vdev_t *vd, 958 const l2arc_log_blkptr_t *lp, l2arc_log_blk_phys_t *lb); 959 static void l2arc_log_blk_fetch_abort(zio_t *zio); 960 961 /* L2ARC persistence block restoration routines. */ 962 static void l2arc_log_blk_restore(l2arc_dev_t *dev, 963 const l2arc_log_blk_phys_t *lb, uint64_t lb_asize); 964 static void l2arc_hdr_restore(const l2arc_log_ent_phys_t *le, 965 l2arc_dev_t *dev); 966 967 /* L2ARC persistence write I/O routines. */ 968 static uint64_t l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, 969 l2arc_write_callback_t *cb); 970 971 /* L2ARC persistence auxiliary routines. */ 972 boolean_t l2arc_log_blkptr_valid(l2arc_dev_t *dev, 973 const l2arc_log_blkptr_t *lbp); 974 static boolean_t l2arc_log_blk_insert(l2arc_dev_t *dev, 975 const arc_buf_hdr_t *ab); 976 boolean_t l2arc_range_check_overlap(uint64_t bottom, 977 uint64_t top, uint64_t check); 978 static void l2arc_blk_fetch_done(zio_t *zio); 979 static inline uint64_t 980 l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev); 981 982 /* 983 * We use Cityhash for this. It's fast, and has good hash properties without 984 * requiring any large static buffers. 985 */ 986 static uint64_t 987 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth) 988 { 989 return (cityhash4(spa, dva->dva_word[0], dva->dva_word[1], birth)); 990 } 991 992 #define HDR_EMPTY(hdr) \ 993 ((hdr)->b_dva.dva_word[0] == 0 && \ 994 (hdr)->b_dva.dva_word[1] == 0) 995 996 #define HDR_EMPTY_OR_LOCKED(hdr) \ 997 (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr))) 998 999 #define HDR_EQUAL(spa, dva, birth, hdr) \ 1000 ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \ 1001 ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \ 1002 ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa) 1003 1004 static void 1005 buf_discard_identity(arc_buf_hdr_t *hdr) 1006 { 1007 hdr->b_dva.dva_word[0] = 0; 1008 hdr->b_dva.dva_word[1] = 0; 1009 hdr->b_birth = 0; 1010 } 1011 1012 static arc_buf_hdr_t * 1013 buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp) 1014 { 1015 const dva_t *dva = BP_IDENTITY(bp); 1016 uint64_t birth = BP_PHYSICAL_BIRTH(bp); 1017 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth); 1018 kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1019 arc_buf_hdr_t *hdr; 1020 1021 mutex_enter(hash_lock); 1022 for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL; 1023 hdr = hdr->b_hash_next) { 1024 if (HDR_EQUAL(spa, dva, birth, hdr)) { 1025 *lockp = hash_lock; 1026 return (hdr); 1027 } 1028 } 1029 mutex_exit(hash_lock); 1030 *lockp = NULL; 1031 return (NULL); 1032 } 1033 1034 /* 1035 * Insert an entry into the hash table. If there is already an element 1036 * equal to elem in the hash table, then the already existing element 1037 * will be returned and the new element will not be inserted. 1038 * Otherwise returns NULL. 1039 * If lockp == NULL, the caller is assumed to already hold the hash lock. 1040 */ 1041 static arc_buf_hdr_t * 1042 buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp) 1043 { 1044 uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1045 kmutex_t *hash_lock = BUF_HASH_LOCK(idx); 1046 arc_buf_hdr_t *fhdr; 1047 uint32_t i; 1048 1049 ASSERT(!DVA_IS_EMPTY(&hdr->b_dva)); 1050 ASSERT(hdr->b_birth != 0); 1051 ASSERT(!HDR_IN_HASH_TABLE(hdr)); 1052 1053 if (lockp != NULL) { 1054 *lockp = hash_lock; 1055 mutex_enter(hash_lock); 1056 } else { 1057 ASSERT(MUTEX_HELD(hash_lock)); 1058 } 1059 1060 for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL; 1061 fhdr = fhdr->b_hash_next, i++) { 1062 if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr)) 1063 return (fhdr); 1064 } 1065 1066 hdr->b_hash_next = buf_hash_table.ht_table[idx]; 1067 buf_hash_table.ht_table[idx] = hdr; 1068 arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1069 1070 /* collect some hash table performance data */ 1071 if (i > 0) { 1072 ARCSTAT_BUMP(arcstat_hash_collisions); 1073 if (i == 1) 1074 ARCSTAT_BUMP(arcstat_hash_chains); 1075 1076 ARCSTAT_MAX(arcstat_hash_chain_max, i); 1077 } 1078 uint64_t he = atomic_inc_64_nv( 1079 &arc_stats.arcstat_hash_elements.value.ui64); 1080 ARCSTAT_MAX(arcstat_hash_elements_max, he); 1081 1082 return (NULL); 1083 } 1084 1085 static void 1086 buf_hash_remove(arc_buf_hdr_t *hdr) 1087 { 1088 arc_buf_hdr_t *fhdr, **hdrp; 1089 uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth); 1090 1091 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx))); 1092 ASSERT(HDR_IN_HASH_TABLE(hdr)); 1093 1094 hdrp = &buf_hash_table.ht_table[idx]; 1095 while ((fhdr = *hdrp) != hdr) { 1096 ASSERT3P(fhdr, !=, NULL); 1097 hdrp = &fhdr->b_hash_next; 1098 } 1099 *hdrp = hdr->b_hash_next; 1100 hdr->b_hash_next = NULL; 1101 arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE); 1102 1103 /* collect some hash table performance data */ 1104 atomic_dec_64(&arc_stats.arcstat_hash_elements.value.ui64); 1105 1106 if (buf_hash_table.ht_table[idx] && 1107 buf_hash_table.ht_table[idx]->b_hash_next == NULL) 1108 ARCSTAT_BUMPDOWN(arcstat_hash_chains); 1109 } 1110 1111 /* 1112 * Global data structures and functions for the buf kmem cache. 1113 */ 1114 1115 static kmem_cache_t *hdr_full_cache; 1116 static kmem_cache_t *hdr_full_crypt_cache; 1117 static kmem_cache_t *hdr_l2only_cache; 1118 static kmem_cache_t *buf_cache; 1119 1120 static void 1121 buf_fini(void) 1122 { 1123 #if defined(_KERNEL) 1124 /* 1125 * Large allocations which do not require contiguous pages 1126 * should be using vmem_free() in the linux kernel\ 1127 */ 1128 vmem_free(buf_hash_table.ht_table, 1129 (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1130 #else 1131 kmem_free(buf_hash_table.ht_table, 1132 (buf_hash_table.ht_mask + 1) * sizeof (void *)); 1133 #endif 1134 for (int i = 0; i < BUF_LOCKS; i++) 1135 mutex_destroy(BUF_HASH_LOCK(i)); 1136 kmem_cache_destroy(hdr_full_cache); 1137 kmem_cache_destroy(hdr_full_crypt_cache); 1138 kmem_cache_destroy(hdr_l2only_cache); 1139 kmem_cache_destroy(buf_cache); 1140 } 1141 1142 /* 1143 * Constructor callback - called when the cache is empty 1144 * and a new buf is requested. 1145 */ 1146 static int 1147 hdr_full_cons(void *vbuf, void *unused, int kmflag) 1148 { 1149 (void) unused, (void) kmflag; 1150 arc_buf_hdr_t *hdr = vbuf; 1151 1152 memset(hdr, 0, HDR_FULL_SIZE); 1153 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 1154 cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL); 1155 zfs_refcount_create(&hdr->b_l1hdr.b_refcnt); 1156 #ifdef ZFS_DEBUG 1157 mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL); 1158 #endif 1159 multilist_link_init(&hdr->b_l1hdr.b_arc_node); 1160 list_link_init(&hdr->b_l2hdr.b_l2node); 1161 arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1162 1163 return (0); 1164 } 1165 1166 static int 1167 hdr_full_crypt_cons(void *vbuf, void *unused, int kmflag) 1168 { 1169 (void) unused; 1170 arc_buf_hdr_t *hdr = vbuf; 1171 1172 hdr_full_cons(vbuf, unused, kmflag); 1173 memset(&hdr->b_crypt_hdr, 0, sizeof (hdr->b_crypt_hdr)); 1174 arc_space_consume(sizeof (hdr->b_crypt_hdr), ARC_SPACE_HDRS); 1175 1176 return (0); 1177 } 1178 1179 static int 1180 hdr_l2only_cons(void *vbuf, void *unused, int kmflag) 1181 { 1182 (void) unused, (void) kmflag; 1183 arc_buf_hdr_t *hdr = vbuf; 1184 1185 memset(hdr, 0, HDR_L2ONLY_SIZE); 1186 arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1187 1188 return (0); 1189 } 1190 1191 static int 1192 buf_cons(void *vbuf, void *unused, int kmflag) 1193 { 1194 (void) unused, (void) kmflag; 1195 arc_buf_t *buf = vbuf; 1196 1197 memset(buf, 0, sizeof (arc_buf_t)); 1198 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1199 1200 return (0); 1201 } 1202 1203 /* 1204 * Destructor callback - called when a cached buf is 1205 * no longer required. 1206 */ 1207 static void 1208 hdr_full_dest(void *vbuf, void *unused) 1209 { 1210 (void) unused; 1211 arc_buf_hdr_t *hdr = vbuf; 1212 1213 ASSERT(HDR_EMPTY(hdr)); 1214 cv_destroy(&hdr->b_l1hdr.b_cv); 1215 zfs_refcount_destroy(&hdr->b_l1hdr.b_refcnt); 1216 #ifdef ZFS_DEBUG 1217 mutex_destroy(&hdr->b_l1hdr.b_freeze_lock); 1218 #endif 1219 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 1220 arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS); 1221 } 1222 1223 static void 1224 hdr_full_crypt_dest(void *vbuf, void *unused) 1225 { 1226 (void) vbuf, (void) unused; 1227 1228 hdr_full_dest(vbuf, unused); 1229 arc_space_return(sizeof (((arc_buf_hdr_t *)NULL)->b_crypt_hdr), 1230 ARC_SPACE_HDRS); 1231 } 1232 1233 static void 1234 hdr_l2only_dest(void *vbuf, void *unused) 1235 { 1236 (void) unused; 1237 arc_buf_hdr_t *hdr = vbuf; 1238 1239 ASSERT(HDR_EMPTY(hdr)); 1240 arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS); 1241 } 1242 1243 static void 1244 buf_dest(void *vbuf, void *unused) 1245 { 1246 (void) unused; 1247 (void) vbuf; 1248 1249 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS); 1250 } 1251 1252 static void 1253 buf_init(void) 1254 { 1255 uint64_t *ct = NULL; 1256 uint64_t hsize = 1ULL << 12; 1257 int i, j; 1258 1259 /* 1260 * The hash table is big enough to fill all of physical memory 1261 * with an average block size of zfs_arc_average_blocksize (default 8K). 1262 * By default, the table will take up 1263 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 1264 */ 1265 while (hsize * zfs_arc_average_blocksize < arc_all_memory()) 1266 hsize <<= 1; 1267 retry: 1268 buf_hash_table.ht_mask = hsize - 1; 1269 #if defined(_KERNEL) 1270 /* 1271 * Large allocations which do not require contiguous pages 1272 * should be using vmem_alloc() in the linux kernel 1273 */ 1274 buf_hash_table.ht_table = 1275 vmem_zalloc(hsize * sizeof (void*), KM_SLEEP); 1276 #else 1277 buf_hash_table.ht_table = 1278 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP); 1279 #endif 1280 if (buf_hash_table.ht_table == NULL) { 1281 ASSERT(hsize > (1ULL << 8)); 1282 hsize >>= 1; 1283 goto retry; 1284 } 1285 1286 hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE, 1287 0, hdr_full_cons, hdr_full_dest, NULL, NULL, NULL, 0); 1288 hdr_full_crypt_cache = kmem_cache_create("arc_buf_hdr_t_full_crypt", 1289 HDR_FULL_CRYPT_SIZE, 0, hdr_full_crypt_cons, hdr_full_crypt_dest, 1290 NULL, NULL, NULL, 0); 1291 hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only", 1292 HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, NULL, 1293 NULL, NULL, 0); 1294 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t), 1295 0, buf_cons, buf_dest, NULL, NULL, NULL, 0); 1296 1297 for (i = 0; i < 256; i++) 1298 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--) 1299 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY); 1300 1301 for (i = 0; i < BUF_LOCKS; i++) 1302 mutex_init(BUF_HASH_LOCK(i), NULL, MUTEX_DEFAULT, NULL); 1303 } 1304 1305 #define ARC_MINTIME (hz>>4) /* 62 ms */ 1306 1307 /* 1308 * This is the size that the buf occupies in memory. If the buf is compressed, 1309 * it will correspond to the compressed size. You should use this method of 1310 * getting the buf size unless you explicitly need the logical size. 1311 */ 1312 uint64_t 1313 arc_buf_size(arc_buf_t *buf) 1314 { 1315 return (ARC_BUF_COMPRESSED(buf) ? 1316 HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr)); 1317 } 1318 1319 uint64_t 1320 arc_buf_lsize(arc_buf_t *buf) 1321 { 1322 return (HDR_GET_LSIZE(buf->b_hdr)); 1323 } 1324 1325 /* 1326 * This function will return B_TRUE if the buffer is encrypted in memory. 1327 * This buffer can be decrypted by calling arc_untransform(). 1328 */ 1329 boolean_t 1330 arc_is_encrypted(arc_buf_t *buf) 1331 { 1332 return (ARC_BUF_ENCRYPTED(buf) != 0); 1333 } 1334 1335 /* 1336 * Returns B_TRUE if the buffer represents data that has not had its MAC 1337 * verified yet. 1338 */ 1339 boolean_t 1340 arc_is_unauthenticated(arc_buf_t *buf) 1341 { 1342 return (HDR_NOAUTH(buf->b_hdr) != 0); 1343 } 1344 1345 void 1346 arc_get_raw_params(arc_buf_t *buf, boolean_t *byteorder, uint8_t *salt, 1347 uint8_t *iv, uint8_t *mac) 1348 { 1349 arc_buf_hdr_t *hdr = buf->b_hdr; 1350 1351 ASSERT(HDR_PROTECTED(hdr)); 1352 1353 memcpy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN); 1354 memcpy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN); 1355 memcpy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN); 1356 *byteorder = (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? 1357 ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; 1358 } 1359 1360 /* 1361 * Indicates how this buffer is compressed in memory. If it is not compressed 1362 * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with 1363 * arc_untransform() as long as it is also unencrypted. 1364 */ 1365 enum zio_compress 1366 arc_get_compression(arc_buf_t *buf) 1367 { 1368 return (ARC_BUF_COMPRESSED(buf) ? 1369 HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF); 1370 } 1371 1372 /* 1373 * Return the compression algorithm used to store this data in the ARC. If ARC 1374 * compression is enabled or this is an encrypted block, this will be the same 1375 * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF. 1376 */ 1377 static inline enum zio_compress 1378 arc_hdr_get_compress(arc_buf_hdr_t *hdr) 1379 { 1380 return (HDR_COMPRESSION_ENABLED(hdr) ? 1381 HDR_GET_COMPRESS(hdr) : ZIO_COMPRESS_OFF); 1382 } 1383 1384 uint8_t 1385 arc_get_complevel(arc_buf_t *buf) 1386 { 1387 return (buf->b_hdr->b_complevel); 1388 } 1389 1390 static inline boolean_t 1391 arc_buf_is_shared(arc_buf_t *buf) 1392 { 1393 boolean_t shared = (buf->b_data != NULL && 1394 buf->b_hdr->b_l1hdr.b_pabd != NULL && 1395 abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) && 1396 buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd)); 1397 IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr)); 1398 IMPLY(shared, ARC_BUF_SHARED(buf)); 1399 IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf)); 1400 1401 /* 1402 * It would be nice to assert arc_can_share() too, but the "hdr isn't 1403 * already being shared" requirement prevents us from doing that. 1404 */ 1405 1406 return (shared); 1407 } 1408 1409 /* 1410 * Free the checksum associated with this header. If there is no checksum, this 1411 * is a no-op. 1412 */ 1413 static inline void 1414 arc_cksum_free(arc_buf_hdr_t *hdr) 1415 { 1416 #ifdef ZFS_DEBUG 1417 ASSERT(HDR_HAS_L1HDR(hdr)); 1418 1419 mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1420 if (hdr->b_l1hdr.b_freeze_cksum != NULL) { 1421 kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t)); 1422 hdr->b_l1hdr.b_freeze_cksum = NULL; 1423 } 1424 mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1425 #endif 1426 } 1427 1428 /* 1429 * Return true iff at least one of the bufs on hdr is not compressed. 1430 * Encrypted buffers count as compressed. 1431 */ 1432 static boolean_t 1433 arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr) 1434 { 1435 ASSERT(hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY_OR_LOCKED(hdr)); 1436 1437 for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) { 1438 if (!ARC_BUF_COMPRESSED(b)) { 1439 return (B_TRUE); 1440 } 1441 } 1442 return (B_FALSE); 1443 } 1444 1445 1446 /* 1447 * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data 1448 * matches the checksum that is stored in the hdr. If there is no checksum, 1449 * or if the buf is compressed, this is a no-op. 1450 */ 1451 static void 1452 arc_cksum_verify(arc_buf_t *buf) 1453 { 1454 #ifdef ZFS_DEBUG 1455 arc_buf_hdr_t *hdr = buf->b_hdr; 1456 zio_cksum_t zc; 1457 1458 if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1459 return; 1460 1461 if (ARC_BUF_COMPRESSED(buf)) 1462 return; 1463 1464 ASSERT(HDR_HAS_L1HDR(hdr)); 1465 1466 mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1467 1468 if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) { 1469 mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1470 return; 1471 } 1472 1473 fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc); 1474 if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc)) 1475 panic("buffer modified while frozen!"); 1476 mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1477 #endif 1478 } 1479 1480 /* 1481 * This function makes the assumption that data stored in the L2ARC 1482 * will be transformed exactly as it is in the main pool. Because of 1483 * this we can verify the checksum against the reading process's bp. 1484 */ 1485 static boolean_t 1486 arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio) 1487 { 1488 ASSERT(!BP_IS_EMBEDDED(zio->io_bp)); 1489 VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr)); 1490 1491 /* 1492 * Block pointers always store the checksum for the logical data. 1493 * If the block pointer has the gang bit set, then the checksum 1494 * it represents is for the reconstituted data and not for an 1495 * individual gang member. The zio pipeline, however, must be able to 1496 * determine the checksum of each of the gang constituents so it 1497 * treats the checksum comparison differently than what we need 1498 * for l2arc blocks. This prevents us from using the 1499 * zio_checksum_error() interface directly. Instead we must call the 1500 * zio_checksum_error_impl() so that we can ensure the checksum is 1501 * generated using the correct checksum algorithm and accounts for the 1502 * logical I/O size and not just a gang fragment. 1503 */ 1504 return (zio_checksum_error_impl(zio->io_spa, zio->io_bp, 1505 BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size, 1506 zio->io_offset, NULL) == 0); 1507 } 1508 1509 /* 1510 * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a 1511 * checksum and attaches it to the buf's hdr so that we can ensure that the buf 1512 * isn't modified later on. If buf is compressed or there is already a checksum 1513 * on the hdr, this is a no-op (we only checksum uncompressed bufs). 1514 */ 1515 static void 1516 arc_cksum_compute(arc_buf_t *buf) 1517 { 1518 if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1519 return; 1520 1521 #ifdef ZFS_DEBUG 1522 arc_buf_hdr_t *hdr = buf->b_hdr; 1523 ASSERT(HDR_HAS_L1HDR(hdr)); 1524 mutex_enter(&hdr->b_l1hdr.b_freeze_lock); 1525 if (hdr->b_l1hdr.b_freeze_cksum != NULL || ARC_BUF_COMPRESSED(buf)) { 1526 mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1527 return; 1528 } 1529 1530 ASSERT(!ARC_BUF_ENCRYPTED(buf)); 1531 ASSERT(!ARC_BUF_COMPRESSED(buf)); 1532 hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), 1533 KM_SLEEP); 1534 fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, 1535 hdr->b_l1hdr.b_freeze_cksum); 1536 mutex_exit(&hdr->b_l1hdr.b_freeze_lock); 1537 #endif 1538 arc_buf_watch(buf); 1539 } 1540 1541 #ifndef _KERNEL 1542 void 1543 arc_buf_sigsegv(int sig, siginfo_t *si, void *unused) 1544 { 1545 (void) sig, (void) unused; 1546 panic("Got SIGSEGV at address: 0x%lx\n", (long)si->si_addr); 1547 } 1548 #endif 1549 1550 static void 1551 arc_buf_unwatch(arc_buf_t *buf) 1552 { 1553 #ifndef _KERNEL 1554 if (arc_watch) { 1555 ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), 1556 PROT_READ | PROT_WRITE)); 1557 } 1558 #else 1559 (void) buf; 1560 #endif 1561 } 1562 1563 static void 1564 arc_buf_watch(arc_buf_t *buf) 1565 { 1566 #ifndef _KERNEL 1567 if (arc_watch) 1568 ASSERT0(mprotect(buf->b_data, arc_buf_size(buf), 1569 PROT_READ)); 1570 #else 1571 (void) buf; 1572 #endif 1573 } 1574 1575 static arc_buf_contents_t 1576 arc_buf_type(arc_buf_hdr_t *hdr) 1577 { 1578 arc_buf_contents_t type; 1579 if (HDR_ISTYPE_METADATA(hdr)) { 1580 type = ARC_BUFC_METADATA; 1581 } else { 1582 type = ARC_BUFC_DATA; 1583 } 1584 VERIFY3U(hdr->b_type, ==, type); 1585 return (type); 1586 } 1587 1588 boolean_t 1589 arc_is_metadata(arc_buf_t *buf) 1590 { 1591 return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0); 1592 } 1593 1594 static uint32_t 1595 arc_bufc_to_flags(arc_buf_contents_t type) 1596 { 1597 switch (type) { 1598 case ARC_BUFC_DATA: 1599 /* metadata field is 0 if buffer contains normal data */ 1600 return (0); 1601 case ARC_BUFC_METADATA: 1602 return (ARC_FLAG_BUFC_METADATA); 1603 default: 1604 break; 1605 } 1606 panic("undefined ARC buffer type!"); 1607 return ((uint32_t)-1); 1608 } 1609 1610 void 1611 arc_buf_thaw(arc_buf_t *buf) 1612 { 1613 arc_buf_hdr_t *hdr = buf->b_hdr; 1614 1615 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 1616 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 1617 1618 arc_cksum_verify(buf); 1619 1620 /* 1621 * Compressed buffers do not manipulate the b_freeze_cksum. 1622 */ 1623 if (ARC_BUF_COMPRESSED(buf)) 1624 return; 1625 1626 ASSERT(HDR_HAS_L1HDR(hdr)); 1627 arc_cksum_free(hdr); 1628 arc_buf_unwatch(buf); 1629 } 1630 1631 void 1632 arc_buf_freeze(arc_buf_t *buf) 1633 { 1634 if (!(zfs_flags & ZFS_DEBUG_MODIFY)) 1635 return; 1636 1637 if (ARC_BUF_COMPRESSED(buf)) 1638 return; 1639 1640 ASSERT(HDR_HAS_L1HDR(buf->b_hdr)); 1641 arc_cksum_compute(buf); 1642 } 1643 1644 /* 1645 * The arc_buf_hdr_t's b_flags should never be modified directly. Instead, 1646 * the following functions should be used to ensure that the flags are 1647 * updated in a thread-safe way. When manipulating the flags either 1648 * the hash_lock must be held or the hdr must be undiscoverable. This 1649 * ensures that we're not racing with any other threads when updating 1650 * the flags. 1651 */ 1652 static inline void 1653 arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 1654 { 1655 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 1656 hdr->b_flags |= flags; 1657 } 1658 1659 static inline void 1660 arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags) 1661 { 1662 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 1663 hdr->b_flags &= ~flags; 1664 } 1665 1666 /* 1667 * Setting the compression bits in the arc_buf_hdr_t's b_flags is 1668 * done in a special way since we have to clear and set bits 1669 * at the same time. Consumers that wish to set the compression bits 1670 * must use this function to ensure that the flags are updated in 1671 * thread-safe manner. 1672 */ 1673 static void 1674 arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp) 1675 { 1676 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 1677 1678 /* 1679 * Holes and embedded blocks will always have a psize = 0 so 1680 * we ignore the compression of the blkptr and set the 1681 * want to uncompress them. Mark them as uncompressed. 1682 */ 1683 if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) { 1684 arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 1685 ASSERT(!HDR_COMPRESSION_ENABLED(hdr)); 1686 } else { 1687 arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC); 1688 ASSERT(HDR_COMPRESSION_ENABLED(hdr)); 1689 } 1690 1691 HDR_SET_COMPRESS(hdr, cmp); 1692 ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp); 1693 } 1694 1695 /* 1696 * Looks for another buf on the same hdr which has the data decompressed, copies 1697 * from it, and returns true. If no such buf exists, returns false. 1698 */ 1699 static boolean_t 1700 arc_buf_try_copy_decompressed_data(arc_buf_t *buf) 1701 { 1702 arc_buf_hdr_t *hdr = buf->b_hdr; 1703 boolean_t copied = B_FALSE; 1704 1705 ASSERT(HDR_HAS_L1HDR(hdr)); 1706 ASSERT3P(buf->b_data, !=, NULL); 1707 ASSERT(!ARC_BUF_COMPRESSED(buf)); 1708 1709 for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL; 1710 from = from->b_next) { 1711 /* can't use our own data buffer */ 1712 if (from == buf) { 1713 continue; 1714 } 1715 1716 if (!ARC_BUF_COMPRESSED(from)) { 1717 memcpy(buf->b_data, from->b_data, arc_buf_size(buf)); 1718 copied = B_TRUE; 1719 break; 1720 } 1721 } 1722 1723 #ifdef ZFS_DEBUG 1724 /* 1725 * There were no decompressed bufs, so there should not be a 1726 * checksum on the hdr either. 1727 */ 1728 if (zfs_flags & ZFS_DEBUG_MODIFY) 1729 EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL); 1730 #endif 1731 1732 return (copied); 1733 } 1734 1735 /* 1736 * Allocates an ARC buf header that's in an evicted & L2-cached state. 1737 * This is used during l2arc reconstruction to make empty ARC buffers 1738 * which circumvent the regular disk->arc->l2arc path and instead come 1739 * into being in the reverse order, i.e. l2arc->arc. 1740 */ 1741 static arc_buf_hdr_t * 1742 arc_buf_alloc_l2only(size_t size, arc_buf_contents_t type, l2arc_dev_t *dev, 1743 dva_t dva, uint64_t daddr, int32_t psize, uint64_t birth, 1744 enum zio_compress compress, uint8_t complevel, boolean_t protected, 1745 boolean_t prefetch, arc_state_type_t arcs_state) 1746 { 1747 arc_buf_hdr_t *hdr; 1748 1749 ASSERT(size != 0); 1750 hdr = kmem_cache_alloc(hdr_l2only_cache, KM_SLEEP); 1751 hdr->b_birth = birth; 1752 hdr->b_type = type; 1753 hdr->b_flags = 0; 1754 arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L2HDR); 1755 HDR_SET_LSIZE(hdr, size); 1756 HDR_SET_PSIZE(hdr, psize); 1757 arc_hdr_set_compress(hdr, compress); 1758 hdr->b_complevel = complevel; 1759 if (protected) 1760 arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); 1761 if (prefetch) 1762 arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 1763 hdr->b_spa = spa_load_guid(dev->l2ad_vdev->vdev_spa); 1764 1765 hdr->b_dva = dva; 1766 1767 hdr->b_l2hdr.b_dev = dev; 1768 hdr->b_l2hdr.b_daddr = daddr; 1769 hdr->b_l2hdr.b_arcs_state = arcs_state; 1770 1771 return (hdr); 1772 } 1773 1774 /* 1775 * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t. 1776 */ 1777 static uint64_t 1778 arc_hdr_size(arc_buf_hdr_t *hdr) 1779 { 1780 uint64_t size; 1781 1782 if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && 1783 HDR_GET_PSIZE(hdr) > 0) { 1784 size = HDR_GET_PSIZE(hdr); 1785 } else { 1786 ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0); 1787 size = HDR_GET_LSIZE(hdr); 1788 } 1789 return (size); 1790 } 1791 1792 static int 1793 arc_hdr_authenticate(arc_buf_hdr_t *hdr, spa_t *spa, uint64_t dsobj) 1794 { 1795 int ret; 1796 uint64_t csize; 1797 uint64_t lsize = HDR_GET_LSIZE(hdr); 1798 uint64_t psize = HDR_GET_PSIZE(hdr); 1799 void *tmpbuf = NULL; 1800 abd_t *abd = hdr->b_l1hdr.b_pabd; 1801 1802 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 1803 ASSERT(HDR_AUTHENTICATED(hdr)); 1804 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 1805 1806 /* 1807 * The MAC is calculated on the compressed data that is stored on disk. 1808 * However, if compressed arc is disabled we will only have the 1809 * decompressed data available to us now. Compress it into a temporary 1810 * abd so we can verify the MAC. The performance overhead of this will 1811 * be relatively low, since most objects in an encrypted objset will 1812 * be encrypted (instead of authenticated) anyway. 1813 */ 1814 if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 1815 !HDR_COMPRESSION_ENABLED(hdr)) { 1816 1817 csize = zio_compress_data(HDR_GET_COMPRESS(hdr), 1818 hdr->b_l1hdr.b_pabd, &tmpbuf, lsize, hdr->b_complevel); 1819 ASSERT3P(tmpbuf, !=, NULL); 1820 ASSERT3U(csize, <=, psize); 1821 abd = abd_get_from_buf(tmpbuf, lsize); 1822 abd_take_ownership_of_buf(abd, B_TRUE); 1823 abd_zero_off(abd, csize, psize - csize); 1824 } 1825 1826 /* 1827 * Authentication is best effort. We authenticate whenever the key is 1828 * available. If we succeed we clear ARC_FLAG_NOAUTH. 1829 */ 1830 if (hdr->b_crypt_hdr.b_ot == DMU_OT_OBJSET) { 1831 ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF); 1832 ASSERT3U(lsize, ==, psize); 1833 ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, dsobj, abd, 1834 psize, hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); 1835 } else { 1836 ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, abd, psize, 1837 hdr->b_crypt_hdr.b_mac); 1838 } 1839 1840 if (ret == 0) 1841 arc_hdr_clear_flags(hdr, ARC_FLAG_NOAUTH); 1842 else if (ret != ENOENT) 1843 goto error; 1844 1845 if (tmpbuf != NULL) 1846 abd_free(abd); 1847 1848 return (0); 1849 1850 error: 1851 if (tmpbuf != NULL) 1852 abd_free(abd); 1853 1854 return (ret); 1855 } 1856 1857 /* 1858 * This function will take a header that only has raw encrypted data in 1859 * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in 1860 * b_l1hdr.b_pabd. If designated in the header flags, this function will 1861 * also decompress the data. 1862 */ 1863 static int 1864 arc_hdr_decrypt(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb) 1865 { 1866 int ret; 1867 abd_t *cabd = NULL; 1868 void *tmp = NULL; 1869 boolean_t no_crypt = B_FALSE; 1870 boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); 1871 1872 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 1873 ASSERT(HDR_ENCRYPTED(hdr)); 1874 1875 arc_hdr_alloc_abd(hdr, 0); 1876 1877 ret = spa_do_crypt_abd(B_FALSE, spa, zb, hdr->b_crypt_hdr.b_ot, 1878 B_FALSE, bswap, hdr->b_crypt_hdr.b_salt, hdr->b_crypt_hdr.b_iv, 1879 hdr->b_crypt_hdr.b_mac, HDR_GET_PSIZE(hdr), hdr->b_l1hdr.b_pabd, 1880 hdr->b_crypt_hdr.b_rabd, &no_crypt); 1881 if (ret != 0) 1882 goto error; 1883 1884 if (no_crypt) { 1885 abd_copy(hdr->b_l1hdr.b_pabd, hdr->b_crypt_hdr.b_rabd, 1886 HDR_GET_PSIZE(hdr)); 1887 } 1888 1889 /* 1890 * If this header has disabled arc compression but the b_pabd is 1891 * compressed after decrypting it, we need to decompress the newly 1892 * decrypted data. 1893 */ 1894 if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 1895 !HDR_COMPRESSION_ENABLED(hdr)) { 1896 /* 1897 * We want to make sure that we are correctly honoring the 1898 * zfs_abd_scatter_enabled setting, so we allocate an abd here 1899 * and then loan a buffer from it, rather than allocating a 1900 * linear buffer and wrapping it in an abd later. 1901 */ 1902 cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, 0); 1903 tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); 1904 1905 ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), 1906 hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), 1907 HDR_GET_LSIZE(hdr), &hdr->b_complevel); 1908 if (ret != 0) { 1909 abd_return_buf(cabd, tmp, arc_hdr_size(hdr)); 1910 goto error; 1911 } 1912 1913 abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); 1914 arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, 1915 arc_hdr_size(hdr), hdr); 1916 hdr->b_l1hdr.b_pabd = cabd; 1917 } 1918 1919 return (0); 1920 1921 error: 1922 arc_hdr_free_abd(hdr, B_FALSE); 1923 if (cabd != NULL) 1924 arc_free_data_buf(hdr, cabd, arc_hdr_size(hdr), hdr); 1925 1926 return (ret); 1927 } 1928 1929 /* 1930 * This function is called during arc_buf_fill() to prepare the header's 1931 * abd plaintext pointer for use. This involves authenticated protected 1932 * data and decrypting encrypted data into the plaintext abd. 1933 */ 1934 static int 1935 arc_fill_hdr_crypt(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, spa_t *spa, 1936 const zbookmark_phys_t *zb, boolean_t noauth) 1937 { 1938 int ret; 1939 1940 ASSERT(HDR_PROTECTED(hdr)); 1941 1942 if (hash_lock != NULL) 1943 mutex_enter(hash_lock); 1944 1945 if (HDR_NOAUTH(hdr) && !noauth) { 1946 /* 1947 * The caller requested authenticated data but our data has 1948 * not been authenticated yet. Verify the MAC now if we can. 1949 */ 1950 ret = arc_hdr_authenticate(hdr, spa, zb->zb_objset); 1951 if (ret != 0) 1952 goto error; 1953 } else if (HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd == NULL) { 1954 /* 1955 * If we only have the encrypted version of the data, but the 1956 * unencrypted version was requested we take this opportunity 1957 * to store the decrypted version in the header for future use. 1958 */ 1959 ret = arc_hdr_decrypt(hdr, spa, zb); 1960 if (ret != 0) 1961 goto error; 1962 } 1963 1964 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 1965 1966 if (hash_lock != NULL) 1967 mutex_exit(hash_lock); 1968 1969 return (0); 1970 1971 error: 1972 if (hash_lock != NULL) 1973 mutex_exit(hash_lock); 1974 1975 return (ret); 1976 } 1977 1978 /* 1979 * This function is used by the dbuf code to decrypt bonus buffers in place. 1980 * The dbuf code itself doesn't have any locking for decrypting a shared dnode 1981 * block, so we use the hash lock here to protect against concurrent calls to 1982 * arc_buf_fill(). 1983 */ 1984 static void 1985 arc_buf_untransform_in_place(arc_buf_t *buf) 1986 { 1987 arc_buf_hdr_t *hdr = buf->b_hdr; 1988 1989 ASSERT(HDR_ENCRYPTED(hdr)); 1990 ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); 1991 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 1992 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 1993 1994 zio_crypt_copy_dnode_bonus(hdr->b_l1hdr.b_pabd, buf->b_data, 1995 arc_buf_size(buf)); 1996 buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; 1997 buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; 1998 hdr->b_crypt_hdr.b_ebufcnt -= 1; 1999 } 2000 2001 /* 2002 * Given a buf that has a data buffer attached to it, this function will 2003 * efficiently fill the buf with data of the specified compression setting from 2004 * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr 2005 * are already sharing a data buf, no copy is performed. 2006 * 2007 * If the buf is marked as compressed but uncompressed data was requested, this 2008 * will allocate a new data buffer for the buf, remove that flag, and fill the 2009 * buf with uncompressed data. You can't request a compressed buf on a hdr with 2010 * uncompressed data, and (since we haven't added support for it yet) if you 2011 * want compressed data your buf must already be marked as compressed and have 2012 * the correct-sized data buffer. 2013 */ 2014 static int 2015 arc_buf_fill(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, 2016 arc_fill_flags_t flags) 2017 { 2018 int error = 0; 2019 arc_buf_hdr_t *hdr = buf->b_hdr; 2020 boolean_t hdr_compressed = 2021 (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); 2022 boolean_t compressed = (flags & ARC_FILL_COMPRESSED) != 0; 2023 boolean_t encrypted = (flags & ARC_FILL_ENCRYPTED) != 0; 2024 dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap; 2025 kmutex_t *hash_lock = (flags & ARC_FILL_LOCKED) ? NULL : HDR_LOCK(hdr); 2026 2027 ASSERT3P(buf->b_data, !=, NULL); 2028 IMPLY(compressed, hdr_compressed || ARC_BUF_ENCRYPTED(buf)); 2029 IMPLY(compressed, ARC_BUF_COMPRESSED(buf)); 2030 IMPLY(encrypted, HDR_ENCRYPTED(hdr)); 2031 IMPLY(encrypted, ARC_BUF_ENCRYPTED(buf)); 2032 IMPLY(encrypted, ARC_BUF_COMPRESSED(buf)); 2033 IMPLY(encrypted, !ARC_BUF_SHARED(buf)); 2034 2035 /* 2036 * If the caller wanted encrypted data we just need to copy it from 2037 * b_rabd and potentially byteswap it. We won't be able to do any 2038 * further transforms on it. 2039 */ 2040 if (encrypted) { 2041 ASSERT(HDR_HAS_RABD(hdr)); 2042 abd_copy_to_buf(buf->b_data, hdr->b_crypt_hdr.b_rabd, 2043 HDR_GET_PSIZE(hdr)); 2044 goto byteswap; 2045 } 2046 2047 /* 2048 * Adjust encrypted and authenticated headers to accommodate 2049 * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are 2050 * allowed to fail decryption due to keys not being loaded 2051 * without being marked as an IO error. 2052 */ 2053 if (HDR_PROTECTED(hdr)) { 2054 error = arc_fill_hdr_crypt(hdr, hash_lock, spa, 2055 zb, !!(flags & ARC_FILL_NOAUTH)); 2056 if (error == EACCES && (flags & ARC_FILL_IN_PLACE) != 0) { 2057 return (error); 2058 } else if (error != 0) { 2059 if (hash_lock != NULL) 2060 mutex_enter(hash_lock); 2061 arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); 2062 if (hash_lock != NULL) 2063 mutex_exit(hash_lock); 2064 return (error); 2065 } 2066 } 2067 2068 /* 2069 * There is a special case here for dnode blocks which are 2070 * decrypting their bonus buffers. These blocks may request to 2071 * be decrypted in-place. This is necessary because there may 2072 * be many dnodes pointing into this buffer and there is 2073 * currently no method to synchronize replacing the backing 2074 * b_data buffer and updating all of the pointers. Here we use 2075 * the hash lock to ensure there are no races. If the need 2076 * arises for other types to be decrypted in-place, they must 2077 * add handling here as well. 2078 */ 2079 if ((flags & ARC_FILL_IN_PLACE) != 0) { 2080 ASSERT(!hdr_compressed); 2081 ASSERT(!compressed); 2082 ASSERT(!encrypted); 2083 2084 if (HDR_ENCRYPTED(hdr) && ARC_BUF_ENCRYPTED(buf)) { 2085 ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE); 2086 2087 if (hash_lock != NULL) 2088 mutex_enter(hash_lock); 2089 arc_buf_untransform_in_place(buf); 2090 if (hash_lock != NULL) 2091 mutex_exit(hash_lock); 2092 2093 /* Compute the hdr's checksum if necessary */ 2094 arc_cksum_compute(buf); 2095 } 2096 2097 return (0); 2098 } 2099 2100 if (hdr_compressed == compressed) { 2101 if (!arc_buf_is_shared(buf)) { 2102 abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd, 2103 arc_buf_size(buf)); 2104 } 2105 } else { 2106 ASSERT(hdr_compressed); 2107 ASSERT(!compressed); 2108 2109 /* 2110 * If the buf is sharing its data with the hdr, unlink it and 2111 * allocate a new data buffer for the buf. 2112 */ 2113 if (arc_buf_is_shared(buf)) { 2114 ASSERT(ARC_BUF_COMPRESSED(buf)); 2115 2116 /* We need to give the buf its own b_data */ 2117 buf->b_flags &= ~ARC_BUF_FLAG_SHARED; 2118 buf->b_data = 2119 arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 2120 arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 2121 2122 /* Previously overhead was 0; just add new overhead */ 2123 ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr)); 2124 } else if (ARC_BUF_COMPRESSED(buf)) { 2125 /* We need to reallocate the buf's b_data */ 2126 arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr), 2127 buf); 2128 buf->b_data = 2129 arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf); 2130 2131 /* We increased the size of b_data; update overhead */ 2132 ARCSTAT_INCR(arcstat_overhead_size, 2133 HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr)); 2134 } 2135 2136 /* 2137 * Regardless of the buf's previous compression settings, it 2138 * should not be compressed at the end of this function. 2139 */ 2140 buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; 2141 2142 /* 2143 * Try copying the data from another buf which already has a 2144 * decompressed version. If that's not possible, it's time to 2145 * bite the bullet and decompress the data from the hdr. 2146 */ 2147 if (arc_buf_try_copy_decompressed_data(buf)) { 2148 /* Skip byteswapping and checksumming (already done) */ 2149 return (0); 2150 } else { 2151 error = zio_decompress_data(HDR_GET_COMPRESS(hdr), 2152 hdr->b_l1hdr.b_pabd, buf->b_data, 2153 HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr), 2154 &hdr->b_complevel); 2155 2156 /* 2157 * Absent hardware errors or software bugs, this should 2158 * be impossible, but log it anyway so we can debug it. 2159 */ 2160 if (error != 0) { 2161 zfs_dbgmsg( 2162 "hdr %px, compress %d, psize %d, lsize %d", 2163 hdr, arc_hdr_get_compress(hdr), 2164 HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr)); 2165 if (hash_lock != NULL) 2166 mutex_enter(hash_lock); 2167 arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); 2168 if (hash_lock != NULL) 2169 mutex_exit(hash_lock); 2170 return (SET_ERROR(EIO)); 2171 } 2172 } 2173 } 2174 2175 byteswap: 2176 /* Byteswap the buf's data if necessary */ 2177 if (bswap != DMU_BSWAP_NUMFUNCS) { 2178 ASSERT(!HDR_SHARED_DATA(hdr)); 2179 ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS); 2180 dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr)); 2181 } 2182 2183 /* Compute the hdr's checksum if necessary */ 2184 arc_cksum_compute(buf); 2185 2186 return (0); 2187 } 2188 2189 /* 2190 * If this function is being called to decrypt an encrypted buffer or verify an 2191 * authenticated one, the key must be loaded and a mapping must be made 2192 * available in the keystore via spa_keystore_create_mapping() or one of its 2193 * callers. 2194 */ 2195 int 2196 arc_untransform(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb, 2197 boolean_t in_place) 2198 { 2199 int ret; 2200 arc_fill_flags_t flags = 0; 2201 2202 if (in_place) 2203 flags |= ARC_FILL_IN_PLACE; 2204 2205 ret = arc_buf_fill(buf, spa, zb, flags); 2206 if (ret == ECKSUM) { 2207 /* 2208 * Convert authentication and decryption errors to EIO 2209 * (and generate an ereport) before leaving the ARC. 2210 */ 2211 ret = SET_ERROR(EIO); 2212 spa_log_error(spa, zb, &buf->b_hdr->b_birth); 2213 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION, 2214 spa, NULL, zb, NULL, 0); 2215 } 2216 2217 return (ret); 2218 } 2219 2220 /* 2221 * Increment the amount of evictable space in the arc_state_t's refcount. 2222 * We account for the space used by the hdr and the arc buf individually 2223 * so that we can add and remove them from the refcount individually. 2224 */ 2225 static void 2226 arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state) 2227 { 2228 arc_buf_contents_t type = arc_buf_type(hdr); 2229 2230 ASSERT(HDR_HAS_L1HDR(hdr)); 2231 2232 if (GHOST_STATE(state)) { 2233 ASSERT0(hdr->b_l1hdr.b_bufcnt); 2234 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2235 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2236 ASSERT(!HDR_HAS_RABD(hdr)); 2237 (void) zfs_refcount_add_many(&state->arcs_esize[type], 2238 HDR_GET_LSIZE(hdr), hdr); 2239 return; 2240 } 2241 2242 if (hdr->b_l1hdr.b_pabd != NULL) { 2243 (void) zfs_refcount_add_many(&state->arcs_esize[type], 2244 arc_hdr_size(hdr), hdr); 2245 } 2246 if (HDR_HAS_RABD(hdr)) { 2247 (void) zfs_refcount_add_many(&state->arcs_esize[type], 2248 HDR_GET_PSIZE(hdr), hdr); 2249 } 2250 2251 for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2252 buf = buf->b_next) { 2253 if (arc_buf_is_shared(buf)) 2254 continue; 2255 (void) zfs_refcount_add_many(&state->arcs_esize[type], 2256 arc_buf_size(buf), buf); 2257 } 2258 } 2259 2260 /* 2261 * Decrement the amount of evictable space in the arc_state_t's refcount. 2262 * We account for the space used by the hdr and the arc buf individually 2263 * so that we can add and remove them from the refcount individually. 2264 */ 2265 static void 2266 arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state) 2267 { 2268 arc_buf_contents_t type = arc_buf_type(hdr); 2269 2270 ASSERT(HDR_HAS_L1HDR(hdr)); 2271 2272 if (GHOST_STATE(state)) { 2273 ASSERT0(hdr->b_l1hdr.b_bufcnt); 2274 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2275 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2276 ASSERT(!HDR_HAS_RABD(hdr)); 2277 (void) zfs_refcount_remove_many(&state->arcs_esize[type], 2278 HDR_GET_LSIZE(hdr), hdr); 2279 return; 2280 } 2281 2282 if (hdr->b_l1hdr.b_pabd != NULL) { 2283 (void) zfs_refcount_remove_many(&state->arcs_esize[type], 2284 arc_hdr_size(hdr), hdr); 2285 } 2286 if (HDR_HAS_RABD(hdr)) { 2287 (void) zfs_refcount_remove_many(&state->arcs_esize[type], 2288 HDR_GET_PSIZE(hdr), hdr); 2289 } 2290 2291 for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2292 buf = buf->b_next) { 2293 if (arc_buf_is_shared(buf)) 2294 continue; 2295 (void) zfs_refcount_remove_many(&state->arcs_esize[type], 2296 arc_buf_size(buf), buf); 2297 } 2298 } 2299 2300 /* 2301 * Add a reference to this hdr indicating that someone is actively 2302 * referencing that memory. When the refcount transitions from 0 to 1, 2303 * we remove it from the respective arc_state_t list to indicate that 2304 * it is not evictable. 2305 */ 2306 static void 2307 add_reference(arc_buf_hdr_t *hdr, const void *tag) 2308 { 2309 arc_state_t *state = hdr->b_l1hdr.b_state; 2310 2311 ASSERT(HDR_HAS_L1HDR(hdr)); 2312 if (!HDR_EMPTY(hdr) && !MUTEX_HELD(HDR_LOCK(hdr))) { 2313 ASSERT(state == arc_anon); 2314 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2315 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 2316 } 2317 2318 if ((zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) && 2319 state != arc_anon && state != arc_l2c_only) { 2320 /* We don't use the L2-only state list. */ 2321 multilist_remove(&state->arcs_list[arc_buf_type(hdr)], hdr); 2322 arc_evictable_space_decrement(hdr, state); 2323 } 2324 } 2325 2326 /* 2327 * Remove a reference from this hdr. When the reference transitions from 2328 * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's 2329 * list making it eligible for eviction. 2330 */ 2331 static int 2332 remove_reference(arc_buf_hdr_t *hdr, const void *tag) 2333 { 2334 int cnt; 2335 arc_state_t *state = hdr->b_l1hdr.b_state; 2336 2337 ASSERT(HDR_HAS_L1HDR(hdr)); 2338 ASSERT(state == arc_anon || MUTEX_HELD(HDR_LOCK(hdr))); 2339 ASSERT(!GHOST_STATE(state)); /* arc_l2c_only counts as a ghost. */ 2340 2341 if ((cnt = zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) != 0) 2342 return (cnt); 2343 2344 if (state == arc_anon) { 2345 arc_hdr_destroy(hdr); 2346 return (0); 2347 } 2348 if (state == arc_uncached && !HDR_PREFETCH(hdr)) { 2349 arc_change_state(arc_anon, hdr); 2350 arc_hdr_destroy(hdr); 2351 return (0); 2352 } 2353 multilist_insert(&state->arcs_list[arc_buf_type(hdr)], hdr); 2354 arc_evictable_space_increment(hdr, state); 2355 return (0); 2356 } 2357 2358 /* 2359 * Returns detailed information about a specific arc buffer. When the 2360 * state_index argument is set the function will calculate the arc header 2361 * list position for its arc state. Since this requires a linear traversal 2362 * callers are strongly encourage not to do this. However, it can be helpful 2363 * for targeted analysis so the functionality is provided. 2364 */ 2365 void 2366 arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index) 2367 { 2368 (void) state_index; 2369 arc_buf_hdr_t *hdr = ab->b_hdr; 2370 l1arc_buf_hdr_t *l1hdr = NULL; 2371 l2arc_buf_hdr_t *l2hdr = NULL; 2372 arc_state_t *state = NULL; 2373 2374 memset(abi, 0, sizeof (arc_buf_info_t)); 2375 2376 if (hdr == NULL) 2377 return; 2378 2379 abi->abi_flags = hdr->b_flags; 2380 2381 if (HDR_HAS_L1HDR(hdr)) { 2382 l1hdr = &hdr->b_l1hdr; 2383 state = l1hdr->b_state; 2384 } 2385 if (HDR_HAS_L2HDR(hdr)) 2386 l2hdr = &hdr->b_l2hdr; 2387 2388 if (l1hdr) { 2389 abi->abi_bufcnt = l1hdr->b_bufcnt; 2390 abi->abi_access = l1hdr->b_arc_access; 2391 abi->abi_mru_hits = l1hdr->b_mru_hits; 2392 abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits; 2393 abi->abi_mfu_hits = l1hdr->b_mfu_hits; 2394 abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits; 2395 abi->abi_holds = zfs_refcount_count(&l1hdr->b_refcnt); 2396 } 2397 2398 if (l2hdr) { 2399 abi->abi_l2arc_dattr = l2hdr->b_daddr; 2400 abi->abi_l2arc_hits = l2hdr->b_hits; 2401 } 2402 2403 abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON; 2404 abi->abi_state_contents = arc_buf_type(hdr); 2405 abi->abi_size = arc_hdr_size(hdr); 2406 } 2407 2408 /* 2409 * Move the supplied buffer to the indicated state. The hash lock 2410 * for the buffer must be held by the caller. 2411 */ 2412 static void 2413 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr) 2414 { 2415 arc_state_t *old_state; 2416 int64_t refcnt; 2417 uint32_t bufcnt; 2418 boolean_t update_old, update_new; 2419 arc_buf_contents_t type = arc_buf_type(hdr); 2420 2421 /* 2422 * We almost always have an L1 hdr here, since we call arc_hdr_realloc() 2423 * in arc_read() when bringing a buffer out of the L2ARC. However, the 2424 * L1 hdr doesn't always exist when we change state to arc_anon before 2425 * destroying a header, in which case reallocating to add the L1 hdr is 2426 * pointless. 2427 */ 2428 if (HDR_HAS_L1HDR(hdr)) { 2429 old_state = hdr->b_l1hdr.b_state; 2430 refcnt = zfs_refcount_count(&hdr->b_l1hdr.b_refcnt); 2431 bufcnt = hdr->b_l1hdr.b_bufcnt; 2432 update_old = (bufcnt > 0 || hdr->b_l1hdr.b_pabd != NULL || 2433 HDR_HAS_RABD(hdr)); 2434 2435 IMPLY(GHOST_STATE(old_state), bufcnt == 0); 2436 IMPLY(GHOST_STATE(new_state), bufcnt == 0); 2437 IMPLY(GHOST_STATE(old_state), hdr->b_l1hdr.b_buf == NULL); 2438 IMPLY(GHOST_STATE(new_state), hdr->b_l1hdr.b_buf == NULL); 2439 IMPLY(old_state == arc_anon, bufcnt <= 1); 2440 } else { 2441 old_state = arc_l2c_only; 2442 refcnt = 0; 2443 bufcnt = 0; 2444 update_old = B_FALSE; 2445 } 2446 update_new = update_old; 2447 if (GHOST_STATE(old_state)) 2448 update_old = B_TRUE; 2449 if (GHOST_STATE(new_state)) 2450 update_new = B_TRUE; 2451 2452 ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 2453 ASSERT3P(new_state, !=, old_state); 2454 2455 /* 2456 * If this buffer is evictable, transfer it from the 2457 * old state list to the new state list. 2458 */ 2459 if (refcnt == 0) { 2460 if (old_state != arc_anon && old_state != arc_l2c_only) { 2461 ASSERT(HDR_HAS_L1HDR(hdr)); 2462 /* remove_reference() saves on insert. */ 2463 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 2464 multilist_remove(&old_state->arcs_list[type], 2465 hdr); 2466 arc_evictable_space_decrement(hdr, old_state); 2467 } 2468 } 2469 if (new_state != arc_anon && new_state != arc_l2c_only) { 2470 /* 2471 * An L1 header always exists here, since if we're 2472 * moving to some L1-cached state (i.e. not l2c_only or 2473 * anonymous), we realloc the header to add an L1hdr 2474 * beforehand. 2475 */ 2476 ASSERT(HDR_HAS_L1HDR(hdr)); 2477 multilist_insert(&new_state->arcs_list[type], hdr); 2478 arc_evictable_space_increment(hdr, new_state); 2479 } 2480 } 2481 2482 ASSERT(!HDR_EMPTY(hdr)); 2483 if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr)) 2484 buf_hash_remove(hdr); 2485 2486 /* adjust state sizes (ignore arc_l2c_only) */ 2487 2488 if (update_new && new_state != arc_l2c_only) { 2489 ASSERT(HDR_HAS_L1HDR(hdr)); 2490 if (GHOST_STATE(new_state)) { 2491 ASSERT0(bufcnt); 2492 2493 /* 2494 * When moving a header to a ghost state, we first 2495 * remove all arc buffers. Thus, we'll have a 2496 * bufcnt of zero, and no arc buffer to use for 2497 * the reference. As a result, we use the arc 2498 * header pointer for the reference. 2499 */ 2500 (void) zfs_refcount_add_many( 2501 &new_state->arcs_size[type], 2502 HDR_GET_LSIZE(hdr), hdr); 2503 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2504 ASSERT(!HDR_HAS_RABD(hdr)); 2505 } else { 2506 uint32_t buffers = 0; 2507 2508 /* 2509 * Each individual buffer holds a unique reference, 2510 * thus we must remove each of these references one 2511 * at a time. 2512 */ 2513 for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2514 buf = buf->b_next) { 2515 ASSERT3U(bufcnt, !=, 0); 2516 buffers++; 2517 2518 /* 2519 * When the arc_buf_t is sharing the data 2520 * block with the hdr, the owner of the 2521 * reference belongs to the hdr. Only 2522 * add to the refcount if the arc_buf_t is 2523 * not shared. 2524 */ 2525 if (arc_buf_is_shared(buf)) 2526 continue; 2527 2528 (void) zfs_refcount_add_many( 2529 &new_state->arcs_size[type], 2530 arc_buf_size(buf), buf); 2531 } 2532 ASSERT3U(bufcnt, ==, buffers); 2533 2534 if (hdr->b_l1hdr.b_pabd != NULL) { 2535 (void) zfs_refcount_add_many( 2536 &new_state->arcs_size[type], 2537 arc_hdr_size(hdr), hdr); 2538 } 2539 2540 if (HDR_HAS_RABD(hdr)) { 2541 (void) zfs_refcount_add_many( 2542 &new_state->arcs_size[type], 2543 HDR_GET_PSIZE(hdr), hdr); 2544 } 2545 } 2546 } 2547 2548 if (update_old && old_state != arc_l2c_only) { 2549 ASSERT(HDR_HAS_L1HDR(hdr)); 2550 if (GHOST_STATE(old_state)) { 2551 ASSERT0(bufcnt); 2552 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2553 ASSERT(!HDR_HAS_RABD(hdr)); 2554 2555 /* 2556 * When moving a header off of a ghost state, 2557 * the header will not contain any arc buffers. 2558 * We use the arc header pointer for the reference 2559 * which is exactly what we did when we put the 2560 * header on the ghost state. 2561 */ 2562 2563 (void) zfs_refcount_remove_many( 2564 &old_state->arcs_size[type], 2565 HDR_GET_LSIZE(hdr), hdr); 2566 } else { 2567 uint32_t buffers = 0; 2568 2569 /* 2570 * Each individual buffer holds a unique reference, 2571 * thus we must remove each of these references one 2572 * at a time. 2573 */ 2574 for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL; 2575 buf = buf->b_next) { 2576 ASSERT3U(bufcnt, !=, 0); 2577 buffers++; 2578 2579 /* 2580 * When the arc_buf_t is sharing the data 2581 * block with the hdr, the owner of the 2582 * reference belongs to the hdr. Only 2583 * add to the refcount if the arc_buf_t is 2584 * not shared. 2585 */ 2586 if (arc_buf_is_shared(buf)) 2587 continue; 2588 2589 (void) zfs_refcount_remove_many( 2590 &old_state->arcs_size[type], 2591 arc_buf_size(buf), buf); 2592 } 2593 ASSERT3U(bufcnt, ==, buffers); 2594 ASSERT(hdr->b_l1hdr.b_pabd != NULL || 2595 HDR_HAS_RABD(hdr)); 2596 2597 if (hdr->b_l1hdr.b_pabd != NULL) { 2598 (void) zfs_refcount_remove_many( 2599 &old_state->arcs_size[type], 2600 arc_hdr_size(hdr), hdr); 2601 } 2602 2603 if (HDR_HAS_RABD(hdr)) { 2604 (void) zfs_refcount_remove_many( 2605 &old_state->arcs_size[type], 2606 HDR_GET_PSIZE(hdr), hdr); 2607 } 2608 } 2609 } 2610 2611 if (HDR_HAS_L1HDR(hdr)) { 2612 hdr->b_l1hdr.b_state = new_state; 2613 2614 if (HDR_HAS_L2HDR(hdr) && new_state != arc_l2c_only) { 2615 l2arc_hdr_arcstats_decrement_state(hdr); 2616 hdr->b_l2hdr.b_arcs_state = new_state->arcs_state; 2617 l2arc_hdr_arcstats_increment_state(hdr); 2618 } 2619 } 2620 } 2621 2622 void 2623 arc_space_consume(uint64_t space, arc_space_type_t type) 2624 { 2625 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 2626 2627 switch (type) { 2628 default: 2629 break; 2630 case ARC_SPACE_DATA: 2631 ARCSTAT_INCR(arcstat_data_size, space); 2632 break; 2633 case ARC_SPACE_META: 2634 ARCSTAT_INCR(arcstat_metadata_size, space); 2635 break; 2636 case ARC_SPACE_BONUS: 2637 ARCSTAT_INCR(arcstat_bonus_size, space); 2638 break; 2639 case ARC_SPACE_DNODE: 2640 ARCSTAT_INCR(arcstat_dnode_size, space); 2641 break; 2642 case ARC_SPACE_DBUF: 2643 ARCSTAT_INCR(arcstat_dbuf_size, space); 2644 break; 2645 case ARC_SPACE_HDRS: 2646 ARCSTAT_INCR(arcstat_hdr_size, space); 2647 break; 2648 case ARC_SPACE_L2HDRS: 2649 aggsum_add(&arc_sums.arcstat_l2_hdr_size, space); 2650 break; 2651 case ARC_SPACE_ABD_CHUNK_WASTE: 2652 /* 2653 * Note: this includes space wasted by all scatter ABD's, not 2654 * just those allocated by the ARC. But the vast majority of 2655 * scatter ABD's come from the ARC, because other users are 2656 * very short-lived. 2657 */ 2658 ARCSTAT_INCR(arcstat_abd_chunk_waste_size, space); 2659 break; 2660 } 2661 2662 if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE) 2663 ARCSTAT_INCR(arcstat_meta_used, space); 2664 2665 aggsum_add(&arc_sums.arcstat_size, space); 2666 } 2667 2668 void 2669 arc_space_return(uint64_t space, arc_space_type_t type) 2670 { 2671 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES); 2672 2673 switch (type) { 2674 default: 2675 break; 2676 case ARC_SPACE_DATA: 2677 ARCSTAT_INCR(arcstat_data_size, -space); 2678 break; 2679 case ARC_SPACE_META: 2680 ARCSTAT_INCR(arcstat_metadata_size, -space); 2681 break; 2682 case ARC_SPACE_BONUS: 2683 ARCSTAT_INCR(arcstat_bonus_size, -space); 2684 break; 2685 case ARC_SPACE_DNODE: 2686 ARCSTAT_INCR(arcstat_dnode_size, -space); 2687 break; 2688 case ARC_SPACE_DBUF: 2689 ARCSTAT_INCR(arcstat_dbuf_size, -space); 2690 break; 2691 case ARC_SPACE_HDRS: 2692 ARCSTAT_INCR(arcstat_hdr_size, -space); 2693 break; 2694 case ARC_SPACE_L2HDRS: 2695 aggsum_add(&arc_sums.arcstat_l2_hdr_size, -space); 2696 break; 2697 case ARC_SPACE_ABD_CHUNK_WASTE: 2698 ARCSTAT_INCR(arcstat_abd_chunk_waste_size, -space); 2699 break; 2700 } 2701 2702 if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE) 2703 ARCSTAT_INCR(arcstat_meta_used, -space); 2704 2705 ASSERT(aggsum_compare(&arc_sums.arcstat_size, space) >= 0); 2706 aggsum_add(&arc_sums.arcstat_size, -space); 2707 } 2708 2709 /* 2710 * Given a hdr and a buf, returns whether that buf can share its b_data buffer 2711 * with the hdr's b_pabd. 2712 */ 2713 static boolean_t 2714 arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2715 { 2716 /* 2717 * The criteria for sharing a hdr's data are: 2718 * 1. the buffer is not encrypted 2719 * 2. the hdr's compression matches the buf's compression 2720 * 3. the hdr doesn't need to be byteswapped 2721 * 4. the hdr isn't already being shared 2722 * 5. the buf is either compressed or it is the last buf in the hdr list 2723 * 2724 * Criterion #5 maintains the invariant that shared uncompressed 2725 * bufs must be the final buf in the hdr's b_buf list. Reading this, you 2726 * might ask, "if a compressed buf is allocated first, won't that be the 2727 * last thing in the list?", but in that case it's impossible to create 2728 * a shared uncompressed buf anyway (because the hdr must be compressed 2729 * to have the compressed buf). You might also think that #3 is 2730 * sufficient to make this guarantee, however it's possible 2731 * (specifically in the rare L2ARC write race mentioned in 2732 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that 2733 * is shareable, but wasn't at the time of its allocation. Rather than 2734 * allow a new shared uncompressed buf to be created and then shuffle 2735 * the list around to make it the last element, this simply disallows 2736 * sharing if the new buf isn't the first to be added. 2737 */ 2738 ASSERT3P(buf->b_hdr, ==, hdr); 2739 boolean_t hdr_compressed = 2740 arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF; 2741 boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0; 2742 return (!ARC_BUF_ENCRYPTED(buf) && 2743 buf_compressed == hdr_compressed && 2744 hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS && 2745 !HDR_SHARED_DATA(hdr) && 2746 (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf))); 2747 } 2748 2749 /* 2750 * Allocate a buf for this hdr. If you care about the data that's in the hdr, 2751 * or if you want a compressed buffer, pass those flags in. Returns 0 if the 2752 * copy was made successfully, or an error code otherwise. 2753 */ 2754 static int 2755 arc_buf_alloc_impl(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb, 2756 const void *tag, boolean_t encrypted, boolean_t compressed, 2757 boolean_t noauth, boolean_t fill, arc_buf_t **ret) 2758 { 2759 arc_buf_t *buf; 2760 arc_fill_flags_t flags = ARC_FILL_LOCKED; 2761 2762 ASSERT(HDR_HAS_L1HDR(hdr)); 2763 ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 2764 VERIFY(hdr->b_type == ARC_BUFC_DATA || 2765 hdr->b_type == ARC_BUFC_METADATA); 2766 ASSERT3P(ret, !=, NULL); 2767 ASSERT3P(*ret, ==, NULL); 2768 IMPLY(encrypted, compressed); 2769 2770 buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE); 2771 buf->b_hdr = hdr; 2772 buf->b_data = NULL; 2773 buf->b_next = hdr->b_l1hdr.b_buf; 2774 buf->b_flags = 0; 2775 2776 add_reference(hdr, tag); 2777 2778 /* 2779 * We're about to change the hdr's b_flags. We must either 2780 * hold the hash_lock or be undiscoverable. 2781 */ 2782 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 2783 2784 /* 2785 * Only honor requests for compressed bufs if the hdr is actually 2786 * compressed. This must be overridden if the buffer is encrypted since 2787 * encrypted buffers cannot be decompressed. 2788 */ 2789 if (encrypted) { 2790 buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; 2791 buf->b_flags |= ARC_BUF_FLAG_ENCRYPTED; 2792 flags |= ARC_FILL_COMPRESSED | ARC_FILL_ENCRYPTED; 2793 } else if (compressed && 2794 arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { 2795 buf->b_flags |= ARC_BUF_FLAG_COMPRESSED; 2796 flags |= ARC_FILL_COMPRESSED; 2797 } 2798 2799 if (noauth) { 2800 ASSERT0(encrypted); 2801 flags |= ARC_FILL_NOAUTH; 2802 } 2803 2804 /* 2805 * If the hdr's data can be shared then we share the data buffer and 2806 * set the appropriate bit in the hdr's b_flags to indicate the hdr is 2807 * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new 2808 * buffer to store the buf's data. 2809 * 2810 * There are two additional restrictions here because we're sharing 2811 * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be 2812 * actively involved in an L2ARC write, because if this buf is used by 2813 * an arc_write() then the hdr's data buffer will be released when the 2814 * write completes, even though the L2ARC write might still be using it. 2815 * Second, the hdr's ABD must be linear so that the buf's user doesn't 2816 * need to be ABD-aware. It must be allocated via 2817 * zio_[data_]buf_alloc(), not as a page, because we need to be able 2818 * to abd_release_ownership_of_buf(), which isn't allowed on "linear 2819 * page" buffers because the ABD code needs to handle freeing them 2820 * specially. 2821 */ 2822 boolean_t can_share = arc_can_share(hdr, buf) && 2823 !HDR_L2_WRITING(hdr) && 2824 hdr->b_l1hdr.b_pabd != NULL && 2825 abd_is_linear(hdr->b_l1hdr.b_pabd) && 2826 !abd_is_linear_page(hdr->b_l1hdr.b_pabd); 2827 2828 /* Set up b_data and sharing */ 2829 if (can_share) { 2830 buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd); 2831 buf->b_flags |= ARC_BUF_FLAG_SHARED; 2832 arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 2833 } else { 2834 buf->b_data = 2835 arc_get_data_buf(hdr, arc_buf_size(buf), buf); 2836 ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); 2837 } 2838 VERIFY3P(buf->b_data, !=, NULL); 2839 2840 hdr->b_l1hdr.b_buf = buf; 2841 hdr->b_l1hdr.b_bufcnt += 1; 2842 if (encrypted) 2843 hdr->b_crypt_hdr.b_ebufcnt += 1; 2844 2845 /* 2846 * If the user wants the data from the hdr, we need to either copy or 2847 * decompress the data. 2848 */ 2849 if (fill) { 2850 ASSERT3P(zb, !=, NULL); 2851 return (arc_buf_fill(buf, spa, zb, flags)); 2852 } 2853 2854 return (0); 2855 } 2856 2857 static const char *arc_onloan_tag = "onloan"; 2858 2859 static inline void 2860 arc_loaned_bytes_update(int64_t delta) 2861 { 2862 atomic_add_64(&arc_loaned_bytes, delta); 2863 2864 /* assert that it did not wrap around */ 2865 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); 2866 } 2867 2868 /* 2869 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in 2870 * flight data by arc_tempreserve_space() until they are "returned". Loaned 2871 * buffers must be returned to the arc before they can be used by the DMU or 2872 * freed. 2873 */ 2874 arc_buf_t * 2875 arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size) 2876 { 2877 arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag, 2878 is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size); 2879 2880 arc_loaned_bytes_update(arc_buf_size(buf)); 2881 2882 return (buf); 2883 } 2884 2885 arc_buf_t * 2886 arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize, 2887 enum zio_compress compression_type, uint8_t complevel) 2888 { 2889 arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag, 2890 psize, lsize, compression_type, complevel); 2891 2892 arc_loaned_bytes_update(arc_buf_size(buf)); 2893 2894 return (buf); 2895 } 2896 2897 arc_buf_t * 2898 arc_loan_raw_buf(spa_t *spa, uint64_t dsobj, boolean_t byteorder, 2899 const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, 2900 dmu_object_type_t ot, uint64_t psize, uint64_t lsize, 2901 enum zio_compress compression_type, uint8_t complevel) 2902 { 2903 arc_buf_t *buf = arc_alloc_raw_buf(spa, arc_onloan_tag, dsobj, 2904 byteorder, salt, iv, mac, ot, psize, lsize, compression_type, 2905 complevel); 2906 2907 atomic_add_64(&arc_loaned_bytes, psize); 2908 return (buf); 2909 } 2910 2911 2912 /* 2913 * Return a loaned arc buffer to the arc. 2914 */ 2915 void 2916 arc_return_buf(arc_buf_t *buf, const void *tag) 2917 { 2918 arc_buf_hdr_t *hdr = buf->b_hdr; 2919 2920 ASSERT3P(buf->b_data, !=, NULL); 2921 ASSERT(HDR_HAS_L1HDR(hdr)); 2922 (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag); 2923 (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2924 2925 arc_loaned_bytes_update(-arc_buf_size(buf)); 2926 } 2927 2928 /* Detach an arc_buf from a dbuf (tag) */ 2929 void 2930 arc_loan_inuse_buf(arc_buf_t *buf, const void *tag) 2931 { 2932 arc_buf_hdr_t *hdr = buf->b_hdr; 2933 2934 ASSERT3P(buf->b_data, !=, NULL); 2935 ASSERT(HDR_HAS_L1HDR(hdr)); 2936 (void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag); 2937 (void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag); 2938 2939 arc_loaned_bytes_update(arc_buf_size(buf)); 2940 } 2941 2942 static void 2943 l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type) 2944 { 2945 l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP); 2946 2947 df->l2df_abd = abd; 2948 df->l2df_size = size; 2949 df->l2df_type = type; 2950 mutex_enter(&l2arc_free_on_write_mtx); 2951 list_insert_head(l2arc_free_on_write, df); 2952 mutex_exit(&l2arc_free_on_write_mtx); 2953 } 2954 2955 static void 2956 arc_hdr_free_on_write(arc_buf_hdr_t *hdr, boolean_t free_rdata) 2957 { 2958 arc_state_t *state = hdr->b_l1hdr.b_state; 2959 arc_buf_contents_t type = arc_buf_type(hdr); 2960 uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); 2961 2962 /* protected by hash lock, if in the hash table */ 2963 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 2964 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 2965 ASSERT(state != arc_anon && state != arc_l2c_only); 2966 2967 (void) zfs_refcount_remove_many(&state->arcs_esize[type], 2968 size, hdr); 2969 } 2970 (void) zfs_refcount_remove_many(&state->arcs_size[type], size, hdr); 2971 if (type == ARC_BUFC_METADATA) { 2972 arc_space_return(size, ARC_SPACE_META); 2973 } else { 2974 ASSERT(type == ARC_BUFC_DATA); 2975 arc_space_return(size, ARC_SPACE_DATA); 2976 } 2977 2978 if (free_rdata) { 2979 l2arc_free_abd_on_write(hdr->b_crypt_hdr.b_rabd, size, type); 2980 } else { 2981 l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type); 2982 } 2983 } 2984 2985 /* 2986 * Share the arc_buf_t's data with the hdr. Whenever we are sharing the 2987 * data buffer, we transfer the refcount ownership to the hdr and update 2988 * the appropriate kstats. 2989 */ 2990 static void 2991 arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 2992 { 2993 ASSERT(arc_can_share(hdr, buf)); 2994 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 2995 ASSERT(!ARC_BUF_ENCRYPTED(buf)); 2996 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 2997 2998 /* 2999 * Start sharing the data buffer. We transfer the 3000 * refcount ownership to the hdr since it always owns 3001 * the refcount whenever an arc_buf_t is shared. 3002 */ 3003 zfs_refcount_transfer_ownership_many( 3004 &hdr->b_l1hdr.b_state->arcs_size[arc_buf_type(hdr)], 3005 arc_hdr_size(hdr), buf, hdr); 3006 hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf)); 3007 abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd, 3008 HDR_ISTYPE_METADATA(hdr)); 3009 arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA); 3010 buf->b_flags |= ARC_BUF_FLAG_SHARED; 3011 3012 /* 3013 * Since we've transferred ownership to the hdr we need 3014 * to increment its compressed and uncompressed kstats and 3015 * decrement the overhead size. 3016 */ 3017 ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr)); 3018 ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 3019 ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf)); 3020 } 3021 3022 static void 3023 arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf) 3024 { 3025 ASSERT(arc_buf_is_shared(buf)); 3026 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 3027 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 3028 3029 /* 3030 * We are no longer sharing this buffer so we need 3031 * to transfer its ownership to the rightful owner. 3032 */ 3033 zfs_refcount_transfer_ownership_many( 3034 &hdr->b_l1hdr.b_state->arcs_size[arc_buf_type(hdr)], 3035 arc_hdr_size(hdr), hdr, buf); 3036 arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 3037 abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd); 3038 abd_free(hdr->b_l1hdr.b_pabd); 3039 hdr->b_l1hdr.b_pabd = NULL; 3040 buf->b_flags &= ~ARC_BUF_FLAG_SHARED; 3041 3042 /* 3043 * Since the buffer is no longer shared between 3044 * the arc buf and the hdr, count it as overhead. 3045 */ 3046 ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr)); 3047 ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 3048 ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf)); 3049 } 3050 3051 /* 3052 * Remove an arc_buf_t from the hdr's buf list and return the last 3053 * arc_buf_t on the list. If no buffers remain on the list then return 3054 * NULL. 3055 */ 3056 static arc_buf_t * 3057 arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf) 3058 { 3059 ASSERT(HDR_HAS_L1HDR(hdr)); 3060 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 3061 3062 arc_buf_t **bufp = &hdr->b_l1hdr.b_buf; 3063 arc_buf_t *lastbuf = NULL; 3064 3065 /* 3066 * Remove the buf from the hdr list and locate the last 3067 * remaining buffer on the list. 3068 */ 3069 while (*bufp != NULL) { 3070 if (*bufp == buf) 3071 *bufp = buf->b_next; 3072 3073 /* 3074 * If we've removed a buffer in the middle of 3075 * the list then update the lastbuf and update 3076 * bufp. 3077 */ 3078 if (*bufp != NULL) { 3079 lastbuf = *bufp; 3080 bufp = &(*bufp)->b_next; 3081 } 3082 } 3083 buf->b_next = NULL; 3084 ASSERT3P(lastbuf, !=, buf); 3085 IMPLY(hdr->b_l1hdr.b_bufcnt > 0, lastbuf != NULL); 3086 IMPLY(hdr->b_l1hdr.b_bufcnt > 0, hdr->b_l1hdr.b_buf != NULL); 3087 IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf)); 3088 3089 return (lastbuf); 3090 } 3091 3092 /* 3093 * Free up buf->b_data and pull the arc_buf_t off of the arc_buf_hdr_t's 3094 * list and free it. 3095 */ 3096 static void 3097 arc_buf_destroy_impl(arc_buf_t *buf) 3098 { 3099 arc_buf_hdr_t *hdr = buf->b_hdr; 3100 3101 /* 3102 * Free up the data associated with the buf but only if we're not 3103 * sharing this with the hdr. If we are sharing it with the hdr, the 3104 * hdr is responsible for doing the free. 3105 */ 3106 if (buf->b_data != NULL) { 3107 /* 3108 * We're about to change the hdr's b_flags. We must either 3109 * hold the hash_lock or be undiscoverable. 3110 */ 3111 ASSERT(HDR_EMPTY_OR_LOCKED(hdr)); 3112 3113 arc_cksum_verify(buf); 3114 arc_buf_unwatch(buf); 3115 3116 if (arc_buf_is_shared(buf)) { 3117 arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA); 3118 } else { 3119 uint64_t size = arc_buf_size(buf); 3120 arc_free_data_buf(hdr, buf->b_data, size, buf); 3121 ARCSTAT_INCR(arcstat_overhead_size, -size); 3122 } 3123 buf->b_data = NULL; 3124 3125 ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 3126 hdr->b_l1hdr.b_bufcnt -= 1; 3127 3128 if (ARC_BUF_ENCRYPTED(buf)) { 3129 hdr->b_crypt_hdr.b_ebufcnt -= 1; 3130 3131 /* 3132 * If we have no more encrypted buffers and we've 3133 * already gotten a copy of the decrypted data we can 3134 * free b_rabd to save some space. 3135 */ 3136 if (hdr->b_crypt_hdr.b_ebufcnt == 0 && 3137 HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd != NULL && 3138 !HDR_IO_IN_PROGRESS(hdr)) { 3139 arc_hdr_free_abd(hdr, B_TRUE); 3140 } 3141 } 3142 } 3143 3144 arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); 3145 3146 if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) { 3147 /* 3148 * If the current arc_buf_t is sharing its data buffer with the 3149 * hdr, then reassign the hdr's b_pabd to share it with the new 3150 * buffer at the end of the list. The shared buffer is always 3151 * the last one on the hdr's buffer list. 3152 * 3153 * There is an equivalent case for compressed bufs, but since 3154 * they aren't guaranteed to be the last buf in the list and 3155 * that is an exceedingly rare case, we just allow that space be 3156 * wasted temporarily. We must also be careful not to share 3157 * encrypted buffers, since they cannot be shared. 3158 */ 3159 if (lastbuf != NULL && !ARC_BUF_ENCRYPTED(lastbuf)) { 3160 /* Only one buf can be shared at once */ 3161 VERIFY(!arc_buf_is_shared(lastbuf)); 3162 /* hdr is uncompressed so can't have compressed buf */ 3163 VERIFY(!ARC_BUF_COMPRESSED(lastbuf)); 3164 3165 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 3166 arc_hdr_free_abd(hdr, B_FALSE); 3167 3168 /* 3169 * We must setup a new shared block between the 3170 * last buffer and the hdr. The data would have 3171 * been allocated by the arc buf so we need to transfer 3172 * ownership to the hdr since it's now being shared. 3173 */ 3174 arc_share_buf(hdr, lastbuf); 3175 } 3176 } else if (HDR_SHARED_DATA(hdr)) { 3177 /* 3178 * Uncompressed shared buffers are always at the end 3179 * of the list. Compressed buffers don't have the 3180 * same requirements. This makes it hard to 3181 * simply assert that the lastbuf is shared so 3182 * we rely on the hdr's compression flags to determine 3183 * if we have a compressed, shared buffer. 3184 */ 3185 ASSERT3P(lastbuf, !=, NULL); 3186 ASSERT(arc_buf_is_shared(lastbuf) || 3187 arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); 3188 } 3189 3190 /* 3191 * Free the checksum if we're removing the last uncompressed buf from 3192 * this hdr. 3193 */ 3194 if (!arc_hdr_has_uncompressed_buf(hdr)) { 3195 arc_cksum_free(hdr); 3196 } 3197 3198 /* clean up the buf */ 3199 buf->b_hdr = NULL; 3200 kmem_cache_free(buf_cache, buf); 3201 } 3202 3203 static void 3204 arc_hdr_alloc_abd(arc_buf_hdr_t *hdr, int alloc_flags) 3205 { 3206 uint64_t size; 3207 boolean_t alloc_rdata = ((alloc_flags & ARC_HDR_ALLOC_RDATA) != 0); 3208 3209 ASSERT3U(HDR_GET_LSIZE(hdr), >, 0); 3210 ASSERT(HDR_HAS_L1HDR(hdr)); 3211 ASSERT(!HDR_SHARED_DATA(hdr) || alloc_rdata); 3212 IMPLY(alloc_rdata, HDR_PROTECTED(hdr)); 3213 3214 if (alloc_rdata) { 3215 size = HDR_GET_PSIZE(hdr); 3216 ASSERT3P(hdr->b_crypt_hdr.b_rabd, ==, NULL); 3217 hdr->b_crypt_hdr.b_rabd = arc_get_data_abd(hdr, size, hdr, 3218 alloc_flags); 3219 ASSERT3P(hdr->b_crypt_hdr.b_rabd, !=, NULL); 3220 ARCSTAT_INCR(arcstat_raw_size, size); 3221 } else { 3222 size = arc_hdr_size(hdr); 3223 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 3224 hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, size, hdr, 3225 alloc_flags); 3226 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 3227 } 3228 3229 ARCSTAT_INCR(arcstat_compressed_size, size); 3230 ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr)); 3231 } 3232 3233 static void 3234 arc_hdr_free_abd(arc_buf_hdr_t *hdr, boolean_t free_rdata) 3235 { 3236 uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr); 3237 3238 ASSERT(HDR_HAS_L1HDR(hdr)); 3239 ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); 3240 IMPLY(free_rdata, HDR_HAS_RABD(hdr)); 3241 3242 /* 3243 * If the hdr is currently being written to the l2arc then 3244 * we defer freeing the data by adding it to the l2arc_free_on_write 3245 * list. The l2arc will free the data once it's finished 3246 * writing it to the l2arc device. 3247 */ 3248 if (HDR_L2_WRITING(hdr)) { 3249 arc_hdr_free_on_write(hdr, free_rdata); 3250 ARCSTAT_BUMP(arcstat_l2_free_on_write); 3251 } else if (free_rdata) { 3252 arc_free_data_abd(hdr, hdr->b_crypt_hdr.b_rabd, size, hdr); 3253 } else { 3254 arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, size, hdr); 3255 } 3256 3257 if (free_rdata) { 3258 hdr->b_crypt_hdr.b_rabd = NULL; 3259 ARCSTAT_INCR(arcstat_raw_size, -size); 3260 } else { 3261 hdr->b_l1hdr.b_pabd = NULL; 3262 } 3263 3264 if (hdr->b_l1hdr.b_pabd == NULL && !HDR_HAS_RABD(hdr)) 3265 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 3266 3267 ARCSTAT_INCR(arcstat_compressed_size, -size); 3268 ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr)); 3269 } 3270 3271 /* 3272 * Allocate empty anonymous ARC header. The header will get its identity 3273 * assigned and buffers attached later as part of read or write operations. 3274 * 3275 * In case of read arc_read() assigns header its identify (b_dva + b_birth), 3276 * inserts it into ARC hash to become globally visible and allocates physical 3277 * (b_pabd) or raw (b_rabd) ABD buffer to read into from disk. On disk read 3278 * completion arc_read_done() allocates ARC buffer(s) as needed, potentially 3279 * sharing one of them with the physical ABD buffer. 3280 * 3281 * In case of write arc_alloc_buf() allocates ARC buffer to be filled with 3282 * data. Then after compression and/or encryption arc_write_ready() allocates 3283 * and fills (or potentially shares) physical (b_pabd) or raw (b_rabd) ABD 3284 * buffer. On disk write completion arc_write_done() assigns the header its 3285 * new identity (b_dva + b_birth) and inserts into ARC hash. 3286 * 3287 * In case of partial overwrite the old data is read first as described. Then 3288 * arc_release() either allocates new anonymous ARC header and moves the ARC 3289 * buffer to it, or reuses the old ARC header by discarding its identity and 3290 * removing it from ARC hash. After buffer modification normal write process 3291 * follows as described. 3292 */ 3293 static arc_buf_hdr_t * 3294 arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize, 3295 boolean_t protected, enum zio_compress compression_type, uint8_t complevel, 3296 arc_buf_contents_t type) 3297 { 3298 arc_buf_hdr_t *hdr; 3299 3300 VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA); 3301 if (protected) { 3302 hdr = kmem_cache_alloc(hdr_full_crypt_cache, KM_PUSHPAGE); 3303 } else { 3304 hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE); 3305 } 3306 3307 ASSERT(HDR_EMPTY(hdr)); 3308 #ifdef ZFS_DEBUG 3309 ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 3310 #endif 3311 HDR_SET_PSIZE(hdr, psize); 3312 HDR_SET_LSIZE(hdr, lsize); 3313 hdr->b_spa = spa; 3314 hdr->b_type = type; 3315 hdr->b_flags = 0; 3316 arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR); 3317 arc_hdr_set_compress(hdr, compression_type); 3318 hdr->b_complevel = complevel; 3319 if (protected) 3320 arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED); 3321 3322 hdr->b_l1hdr.b_state = arc_anon; 3323 hdr->b_l1hdr.b_arc_access = 0; 3324 hdr->b_l1hdr.b_mru_hits = 0; 3325 hdr->b_l1hdr.b_mru_ghost_hits = 0; 3326 hdr->b_l1hdr.b_mfu_hits = 0; 3327 hdr->b_l1hdr.b_mfu_ghost_hits = 0; 3328 hdr->b_l1hdr.b_bufcnt = 0; 3329 hdr->b_l1hdr.b_buf = NULL; 3330 3331 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3332 3333 return (hdr); 3334 } 3335 3336 /* 3337 * Transition between the two allocation states for the arc_buf_hdr struct. 3338 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without 3339 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller 3340 * version is used when a cache buffer is only in the L2ARC in order to reduce 3341 * memory usage. 3342 */ 3343 static arc_buf_hdr_t * 3344 arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new) 3345 { 3346 ASSERT(HDR_HAS_L2HDR(hdr)); 3347 3348 arc_buf_hdr_t *nhdr; 3349 l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 3350 3351 ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) || 3352 (old == hdr_l2only_cache && new == hdr_full_cache)); 3353 3354 /* 3355 * if the caller wanted a new full header and the header is to be 3356 * encrypted we will actually allocate the header from the full crypt 3357 * cache instead. The same applies to freeing from the old cache. 3358 */ 3359 if (HDR_PROTECTED(hdr) && new == hdr_full_cache) 3360 new = hdr_full_crypt_cache; 3361 if (HDR_PROTECTED(hdr) && old == hdr_full_cache) 3362 old = hdr_full_crypt_cache; 3363 3364 nhdr = kmem_cache_alloc(new, KM_PUSHPAGE); 3365 3366 ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 3367 buf_hash_remove(hdr); 3368 3369 memcpy(nhdr, hdr, HDR_L2ONLY_SIZE); 3370 3371 if (new == hdr_full_cache || new == hdr_full_crypt_cache) { 3372 arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR); 3373 /* 3374 * arc_access and arc_change_state need to be aware that a 3375 * header has just come out of L2ARC, so we set its state to 3376 * l2c_only even though it's about to change. 3377 */ 3378 nhdr->b_l1hdr.b_state = arc_l2c_only; 3379 3380 /* Verify previous threads set to NULL before freeing */ 3381 ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL); 3382 ASSERT(!HDR_HAS_RABD(hdr)); 3383 } else { 3384 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 3385 ASSERT0(hdr->b_l1hdr.b_bufcnt); 3386 #ifdef ZFS_DEBUG 3387 ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 3388 #endif 3389 3390 /* 3391 * If we've reached here, We must have been called from 3392 * arc_evict_hdr(), as such we should have already been 3393 * removed from any ghost list we were previously on 3394 * (which protects us from racing with arc_evict_state), 3395 * thus no locking is needed during this check. 3396 */ 3397 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 3398 3399 /* 3400 * A buffer must not be moved into the arc_l2c_only 3401 * state if it's not finished being written out to the 3402 * l2arc device. Otherwise, the b_l1hdr.b_pabd field 3403 * might try to be accessed, even though it was removed. 3404 */ 3405 VERIFY(!HDR_L2_WRITING(hdr)); 3406 VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL); 3407 ASSERT(!HDR_HAS_RABD(hdr)); 3408 3409 arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR); 3410 } 3411 /* 3412 * The header has been reallocated so we need to re-insert it into any 3413 * lists it was on. 3414 */ 3415 (void) buf_hash_insert(nhdr, NULL); 3416 3417 ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node)); 3418 3419 mutex_enter(&dev->l2ad_mtx); 3420 3421 /* 3422 * We must place the realloc'ed header back into the list at 3423 * the same spot. Otherwise, if it's placed earlier in the list, 3424 * l2arc_write_buffers() could find it during the function's 3425 * write phase, and try to write it out to the l2arc. 3426 */ 3427 list_insert_after(&dev->l2ad_buflist, hdr, nhdr); 3428 list_remove(&dev->l2ad_buflist, hdr); 3429 3430 mutex_exit(&dev->l2ad_mtx); 3431 3432 /* 3433 * Since we're using the pointer address as the tag when 3434 * incrementing and decrementing the l2ad_alloc refcount, we 3435 * must remove the old pointer (that we're about to destroy) and 3436 * add the new pointer to the refcount. Otherwise we'd remove 3437 * the wrong pointer address when calling arc_hdr_destroy() later. 3438 */ 3439 3440 (void) zfs_refcount_remove_many(&dev->l2ad_alloc, 3441 arc_hdr_size(hdr), hdr); 3442 (void) zfs_refcount_add_many(&dev->l2ad_alloc, 3443 arc_hdr_size(nhdr), nhdr); 3444 3445 buf_discard_identity(hdr); 3446 kmem_cache_free(old, hdr); 3447 3448 return (nhdr); 3449 } 3450 3451 /* 3452 * This function allows an L1 header to be reallocated as a crypt 3453 * header and vice versa. If we are going to a crypt header, the 3454 * new fields will be zeroed out. 3455 */ 3456 static arc_buf_hdr_t * 3457 arc_hdr_realloc_crypt(arc_buf_hdr_t *hdr, boolean_t need_crypt) 3458 { 3459 arc_buf_hdr_t *nhdr; 3460 arc_buf_t *buf; 3461 kmem_cache_t *ncache, *ocache; 3462 3463 /* 3464 * This function requires that hdr is in the arc_anon state. 3465 * Therefore it won't have any L2ARC data for us to worry 3466 * about copying. 3467 */ 3468 ASSERT(HDR_HAS_L1HDR(hdr)); 3469 ASSERT(!HDR_HAS_L2HDR(hdr)); 3470 ASSERT3U(!!HDR_PROTECTED(hdr), !=, need_crypt); 3471 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 3472 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 3473 ASSERT(!list_link_active(&hdr->b_l2hdr.b_l2node)); 3474 ASSERT3P(hdr->b_hash_next, ==, NULL); 3475 3476 if (need_crypt) { 3477 ncache = hdr_full_crypt_cache; 3478 ocache = hdr_full_cache; 3479 } else { 3480 ncache = hdr_full_cache; 3481 ocache = hdr_full_crypt_cache; 3482 } 3483 3484 nhdr = kmem_cache_alloc(ncache, KM_PUSHPAGE); 3485 3486 /* 3487 * Copy all members that aren't locks or condvars to the new header. 3488 * No lists are pointing to us (as we asserted above), so we don't 3489 * need to worry about the list nodes. 3490 */ 3491 nhdr->b_dva = hdr->b_dva; 3492 nhdr->b_birth = hdr->b_birth; 3493 nhdr->b_type = hdr->b_type; 3494 nhdr->b_flags = hdr->b_flags; 3495 nhdr->b_psize = hdr->b_psize; 3496 nhdr->b_lsize = hdr->b_lsize; 3497 nhdr->b_spa = hdr->b_spa; 3498 #ifdef ZFS_DEBUG 3499 nhdr->b_l1hdr.b_freeze_cksum = hdr->b_l1hdr.b_freeze_cksum; 3500 #endif 3501 nhdr->b_l1hdr.b_bufcnt = hdr->b_l1hdr.b_bufcnt; 3502 nhdr->b_l1hdr.b_byteswap = hdr->b_l1hdr.b_byteswap; 3503 nhdr->b_l1hdr.b_state = hdr->b_l1hdr.b_state; 3504 nhdr->b_l1hdr.b_arc_access = hdr->b_l1hdr.b_arc_access; 3505 nhdr->b_l1hdr.b_mru_hits = hdr->b_l1hdr.b_mru_hits; 3506 nhdr->b_l1hdr.b_mru_ghost_hits = hdr->b_l1hdr.b_mru_ghost_hits; 3507 nhdr->b_l1hdr.b_mfu_hits = hdr->b_l1hdr.b_mfu_hits; 3508 nhdr->b_l1hdr.b_mfu_ghost_hits = hdr->b_l1hdr.b_mfu_ghost_hits; 3509 nhdr->b_l1hdr.b_acb = hdr->b_l1hdr.b_acb; 3510 nhdr->b_l1hdr.b_pabd = hdr->b_l1hdr.b_pabd; 3511 3512 /* 3513 * This zfs_refcount_add() exists only to ensure that the individual 3514 * arc buffers always point to a header that is referenced, avoiding 3515 * a small race condition that could trigger ASSERTs. 3516 */ 3517 (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, FTAG); 3518 nhdr->b_l1hdr.b_buf = hdr->b_l1hdr.b_buf; 3519 for (buf = nhdr->b_l1hdr.b_buf; buf != NULL; buf = buf->b_next) 3520 buf->b_hdr = nhdr; 3521 3522 zfs_refcount_transfer(&nhdr->b_l1hdr.b_refcnt, &hdr->b_l1hdr.b_refcnt); 3523 (void) zfs_refcount_remove(&nhdr->b_l1hdr.b_refcnt, FTAG); 3524 ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); 3525 3526 if (need_crypt) { 3527 arc_hdr_set_flags(nhdr, ARC_FLAG_PROTECTED); 3528 } else { 3529 arc_hdr_clear_flags(nhdr, ARC_FLAG_PROTECTED); 3530 } 3531 3532 /* unset all members of the original hdr */ 3533 memset(&hdr->b_dva, 0, sizeof (dva_t)); 3534 hdr->b_birth = 0; 3535 hdr->b_type = 0; 3536 hdr->b_flags = 0; 3537 hdr->b_psize = 0; 3538 hdr->b_lsize = 0; 3539 hdr->b_spa = 0; 3540 #ifdef ZFS_DEBUG 3541 hdr->b_l1hdr.b_freeze_cksum = NULL; 3542 #endif 3543 hdr->b_l1hdr.b_buf = NULL; 3544 hdr->b_l1hdr.b_bufcnt = 0; 3545 hdr->b_l1hdr.b_byteswap = 0; 3546 hdr->b_l1hdr.b_state = NULL; 3547 hdr->b_l1hdr.b_arc_access = 0; 3548 hdr->b_l1hdr.b_mru_hits = 0; 3549 hdr->b_l1hdr.b_mru_ghost_hits = 0; 3550 hdr->b_l1hdr.b_mfu_hits = 0; 3551 hdr->b_l1hdr.b_mfu_ghost_hits = 0; 3552 hdr->b_l1hdr.b_acb = NULL; 3553 hdr->b_l1hdr.b_pabd = NULL; 3554 3555 if (ocache == hdr_full_crypt_cache) { 3556 ASSERT(!HDR_HAS_RABD(hdr)); 3557 hdr->b_crypt_hdr.b_ot = DMU_OT_NONE; 3558 hdr->b_crypt_hdr.b_ebufcnt = 0; 3559 hdr->b_crypt_hdr.b_dsobj = 0; 3560 memset(hdr->b_crypt_hdr.b_salt, 0, ZIO_DATA_SALT_LEN); 3561 memset(hdr->b_crypt_hdr.b_iv, 0, ZIO_DATA_IV_LEN); 3562 memset(hdr->b_crypt_hdr.b_mac, 0, ZIO_DATA_MAC_LEN); 3563 } 3564 3565 buf_discard_identity(hdr); 3566 kmem_cache_free(ocache, hdr); 3567 3568 return (nhdr); 3569 } 3570 3571 /* 3572 * This function is used by the send / receive code to convert a newly 3573 * allocated arc_buf_t to one that is suitable for a raw encrypted write. It 3574 * is also used to allow the root objset block to be updated without altering 3575 * its embedded MACs. Both block types will always be uncompressed so we do not 3576 * have to worry about compression type or psize. 3577 */ 3578 void 3579 arc_convert_to_raw(arc_buf_t *buf, uint64_t dsobj, boolean_t byteorder, 3580 dmu_object_type_t ot, const uint8_t *salt, const uint8_t *iv, 3581 const uint8_t *mac) 3582 { 3583 arc_buf_hdr_t *hdr = buf->b_hdr; 3584 3585 ASSERT(ot == DMU_OT_DNODE || ot == DMU_OT_OBJSET); 3586 ASSERT(HDR_HAS_L1HDR(hdr)); 3587 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 3588 3589 buf->b_flags |= (ARC_BUF_FLAG_COMPRESSED | ARC_BUF_FLAG_ENCRYPTED); 3590 if (!HDR_PROTECTED(hdr)) 3591 hdr = arc_hdr_realloc_crypt(hdr, B_TRUE); 3592 hdr->b_crypt_hdr.b_dsobj = dsobj; 3593 hdr->b_crypt_hdr.b_ot = ot; 3594 hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? 3595 DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); 3596 if (!arc_hdr_has_uncompressed_buf(hdr)) 3597 arc_cksum_free(hdr); 3598 3599 if (salt != NULL) 3600 memcpy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN); 3601 if (iv != NULL) 3602 memcpy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN); 3603 if (mac != NULL) 3604 memcpy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN); 3605 } 3606 3607 /* 3608 * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller. 3609 * The buf is returned thawed since we expect the consumer to modify it. 3610 */ 3611 arc_buf_t * 3612 arc_alloc_buf(spa_t *spa, const void *tag, arc_buf_contents_t type, 3613 int32_t size) 3614 { 3615 arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size, 3616 B_FALSE, ZIO_COMPRESS_OFF, 0, type); 3617 3618 arc_buf_t *buf = NULL; 3619 VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, B_FALSE, 3620 B_FALSE, B_FALSE, &buf)); 3621 arc_buf_thaw(buf); 3622 3623 return (buf); 3624 } 3625 3626 /* 3627 * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this 3628 * for bufs containing metadata. 3629 */ 3630 arc_buf_t * 3631 arc_alloc_compressed_buf(spa_t *spa, const void *tag, uint64_t psize, 3632 uint64_t lsize, enum zio_compress compression_type, uint8_t complevel) 3633 { 3634 ASSERT3U(lsize, >, 0); 3635 ASSERT3U(lsize, >=, psize); 3636 ASSERT3U(compression_type, >, ZIO_COMPRESS_OFF); 3637 ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); 3638 3639 arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 3640 B_FALSE, compression_type, complevel, ARC_BUFC_DATA); 3641 3642 arc_buf_t *buf = NULL; 3643 VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, 3644 B_TRUE, B_FALSE, B_FALSE, &buf)); 3645 arc_buf_thaw(buf); 3646 3647 /* 3648 * To ensure that the hdr has the correct data in it if we call 3649 * arc_untransform() on this buf before it's been written to disk, 3650 * it's easiest if we just set up sharing between the buf and the hdr. 3651 */ 3652 arc_share_buf(hdr, buf); 3653 3654 return (buf); 3655 } 3656 3657 arc_buf_t * 3658 arc_alloc_raw_buf(spa_t *spa, const void *tag, uint64_t dsobj, 3659 boolean_t byteorder, const uint8_t *salt, const uint8_t *iv, 3660 const uint8_t *mac, dmu_object_type_t ot, uint64_t psize, uint64_t lsize, 3661 enum zio_compress compression_type, uint8_t complevel) 3662 { 3663 arc_buf_hdr_t *hdr; 3664 arc_buf_t *buf; 3665 arc_buf_contents_t type = DMU_OT_IS_METADATA(ot) ? 3666 ARC_BUFC_METADATA : ARC_BUFC_DATA; 3667 3668 ASSERT3U(lsize, >, 0); 3669 ASSERT3U(lsize, >=, psize); 3670 ASSERT3U(compression_type, >=, ZIO_COMPRESS_OFF); 3671 ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS); 3672 3673 hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, B_TRUE, 3674 compression_type, complevel, type); 3675 3676 hdr->b_crypt_hdr.b_dsobj = dsobj; 3677 hdr->b_crypt_hdr.b_ot = ot; 3678 hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ? 3679 DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot); 3680 memcpy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN); 3681 memcpy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN); 3682 memcpy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN); 3683 3684 /* 3685 * This buffer will be considered encrypted even if the ot is not an 3686 * encrypted type. It will become authenticated instead in 3687 * arc_write_ready(). 3688 */ 3689 buf = NULL; 3690 VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_TRUE, B_TRUE, 3691 B_FALSE, B_FALSE, &buf)); 3692 arc_buf_thaw(buf); 3693 3694 return (buf); 3695 } 3696 3697 static void 3698 l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr, 3699 boolean_t state_only) 3700 { 3701 l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 3702 l2arc_dev_t *dev = l2hdr->b_dev; 3703 uint64_t lsize = HDR_GET_LSIZE(hdr); 3704 uint64_t psize = HDR_GET_PSIZE(hdr); 3705 uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); 3706 arc_buf_contents_t type = hdr->b_type; 3707 int64_t lsize_s; 3708 int64_t psize_s; 3709 int64_t asize_s; 3710 3711 if (incr) { 3712 lsize_s = lsize; 3713 psize_s = psize; 3714 asize_s = asize; 3715 } else { 3716 lsize_s = -lsize; 3717 psize_s = -psize; 3718 asize_s = -asize; 3719 } 3720 3721 /* If the buffer is a prefetch, count it as such. */ 3722 if (HDR_PREFETCH(hdr)) { 3723 ARCSTAT_INCR(arcstat_l2_prefetch_asize, asize_s); 3724 } else { 3725 /* 3726 * We use the value stored in the L2 header upon initial 3727 * caching in L2ARC. This value will be updated in case 3728 * an MRU/MRU_ghost buffer transitions to MFU but the L2ARC 3729 * metadata (log entry) cannot currently be updated. Having 3730 * the ARC state in the L2 header solves the problem of a 3731 * possibly absent L1 header (apparent in buffers restored 3732 * from persistent L2ARC). 3733 */ 3734 switch (hdr->b_l2hdr.b_arcs_state) { 3735 case ARC_STATE_MRU_GHOST: 3736 case ARC_STATE_MRU: 3737 ARCSTAT_INCR(arcstat_l2_mru_asize, asize_s); 3738 break; 3739 case ARC_STATE_MFU_GHOST: 3740 case ARC_STATE_MFU: 3741 ARCSTAT_INCR(arcstat_l2_mfu_asize, asize_s); 3742 break; 3743 default: 3744 break; 3745 } 3746 } 3747 3748 if (state_only) 3749 return; 3750 3751 ARCSTAT_INCR(arcstat_l2_psize, psize_s); 3752 ARCSTAT_INCR(arcstat_l2_lsize, lsize_s); 3753 3754 switch (type) { 3755 case ARC_BUFC_DATA: 3756 ARCSTAT_INCR(arcstat_l2_bufc_data_asize, asize_s); 3757 break; 3758 case ARC_BUFC_METADATA: 3759 ARCSTAT_INCR(arcstat_l2_bufc_metadata_asize, asize_s); 3760 break; 3761 default: 3762 break; 3763 } 3764 } 3765 3766 3767 static void 3768 arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr) 3769 { 3770 l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr; 3771 l2arc_dev_t *dev = l2hdr->b_dev; 3772 uint64_t psize = HDR_GET_PSIZE(hdr); 3773 uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); 3774 3775 ASSERT(MUTEX_HELD(&dev->l2ad_mtx)); 3776 ASSERT(HDR_HAS_L2HDR(hdr)); 3777 3778 list_remove(&dev->l2ad_buflist, hdr); 3779 3780 l2arc_hdr_arcstats_decrement(hdr); 3781 vdev_space_update(dev->l2ad_vdev, -asize, 0, 0); 3782 3783 (void) zfs_refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr), 3784 hdr); 3785 arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 3786 } 3787 3788 static void 3789 arc_hdr_destroy(arc_buf_hdr_t *hdr) 3790 { 3791 if (HDR_HAS_L1HDR(hdr)) { 3792 ASSERT(hdr->b_l1hdr.b_buf == NULL || 3793 hdr->b_l1hdr.b_bufcnt > 0); 3794 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 3795 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon); 3796 } 3797 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3798 ASSERT(!HDR_IN_HASH_TABLE(hdr)); 3799 3800 if (HDR_HAS_L2HDR(hdr)) { 3801 l2arc_dev_t *dev = hdr->b_l2hdr.b_dev; 3802 boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx); 3803 3804 if (!buflist_held) 3805 mutex_enter(&dev->l2ad_mtx); 3806 3807 /* 3808 * Even though we checked this conditional above, we 3809 * need to check this again now that we have the 3810 * l2ad_mtx. This is because we could be racing with 3811 * another thread calling l2arc_evict() which might have 3812 * destroyed this header's L2 portion as we were waiting 3813 * to acquire the l2ad_mtx. If that happens, we don't 3814 * want to re-destroy the header's L2 portion. 3815 */ 3816 if (HDR_HAS_L2HDR(hdr)) { 3817 3818 if (!HDR_EMPTY(hdr)) 3819 buf_discard_identity(hdr); 3820 3821 arc_hdr_l2hdr_destroy(hdr); 3822 } 3823 3824 if (!buflist_held) 3825 mutex_exit(&dev->l2ad_mtx); 3826 } 3827 3828 /* 3829 * The header's identify can only be safely discarded once it is no 3830 * longer discoverable. This requires removing it from the hash table 3831 * and the l2arc header list. After this point the hash lock can not 3832 * be used to protect the header. 3833 */ 3834 if (!HDR_EMPTY(hdr)) 3835 buf_discard_identity(hdr); 3836 3837 if (HDR_HAS_L1HDR(hdr)) { 3838 arc_cksum_free(hdr); 3839 3840 while (hdr->b_l1hdr.b_buf != NULL) 3841 arc_buf_destroy_impl(hdr->b_l1hdr.b_buf); 3842 3843 if (hdr->b_l1hdr.b_pabd != NULL) 3844 arc_hdr_free_abd(hdr, B_FALSE); 3845 3846 if (HDR_HAS_RABD(hdr)) 3847 arc_hdr_free_abd(hdr, B_TRUE); 3848 } 3849 3850 ASSERT3P(hdr->b_hash_next, ==, NULL); 3851 if (HDR_HAS_L1HDR(hdr)) { 3852 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 3853 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 3854 #ifdef ZFS_DEBUG 3855 ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 3856 #endif 3857 3858 if (!HDR_PROTECTED(hdr)) { 3859 kmem_cache_free(hdr_full_cache, hdr); 3860 } else { 3861 kmem_cache_free(hdr_full_crypt_cache, hdr); 3862 } 3863 } else { 3864 kmem_cache_free(hdr_l2only_cache, hdr); 3865 } 3866 } 3867 3868 void 3869 arc_buf_destroy(arc_buf_t *buf, const void *tag) 3870 { 3871 arc_buf_hdr_t *hdr = buf->b_hdr; 3872 3873 if (hdr->b_l1hdr.b_state == arc_anon) { 3874 ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 3875 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3876 VERIFY0(remove_reference(hdr, tag)); 3877 return; 3878 } 3879 3880 kmutex_t *hash_lock = HDR_LOCK(hdr); 3881 mutex_enter(hash_lock); 3882 3883 ASSERT3P(hdr, ==, buf->b_hdr); 3884 ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 3885 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 3886 ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon); 3887 ASSERT3P(buf->b_data, !=, NULL); 3888 3889 arc_buf_destroy_impl(buf); 3890 (void) remove_reference(hdr, tag); 3891 mutex_exit(hash_lock); 3892 } 3893 3894 /* 3895 * Evict the arc_buf_hdr that is provided as a parameter. The resultant 3896 * state of the header is dependent on its state prior to entering this 3897 * function. The following transitions are possible: 3898 * 3899 * - arc_mru -> arc_mru_ghost 3900 * - arc_mfu -> arc_mfu_ghost 3901 * - arc_mru_ghost -> arc_l2c_only 3902 * - arc_mru_ghost -> deleted 3903 * - arc_mfu_ghost -> arc_l2c_only 3904 * - arc_mfu_ghost -> deleted 3905 * - arc_uncached -> deleted 3906 * 3907 * Return total size of evicted data buffers for eviction progress tracking. 3908 * When evicting from ghost states return logical buffer size to make eviction 3909 * progress at the same (or at least comparable) rate as from non-ghost states. 3910 * 3911 * Return *real_evicted for actual ARC size reduction to wake up threads 3912 * waiting for it. For non-ghost states it includes size of evicted data 3913 * buffers (the headers are not freed there). For ghost states it includes 3914 * only the evicted headers size. 3915 */ 3916 static int64_t 3917 arc_evict_hdr(arc_buf_hdr_t *hdr, uint64_t *real_evicted) 3918 { 3919 arc_state_t *evicted_state, *state; 3920 int64_t bytes_evicted = 0; 3921 uint_t min_lifetime = HDR_PRESCIENT_PREFETCH(hdr) ? 3922 arc_min_prescient_prefetch_ms : arc_min_prefetch_ms; 3923 3924 ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 3925 ASSERT(HDR_HAS_L1HDR(hdr)); 3926 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 3927 ASSERT0(hdr->b_l1hdr.b_bufcnt); 3928 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 3929 ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt)); 3930 3931 *real_evicted = 0; 3932 state = hdr->b_l1hdr.b_state; 3933 if (GHOST_STATE(state)) { 3934 3935 /* 3936 * l2arc_write_buffers() relies on a header's L1 portion 3937 * (i.e. its b_pabd field) during it's write phase. 3938 * Thus, we cannot push a header onto the arc_l2c_only 3939 * state (removing its L1 piece) until the header is 3940 * done being written to the l2arc. 3941 */ 3942 if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) { 3943 ARCSTAT_BUMP(arcstat_evict_l2_skip); 3944 return (bytes_evicted); 3945 } 3946 3947 ARCSTAT_BUMP(arcstat_deleted); 3948 bytes_evicted += HDR_GET_LSIZE(hdr); 3949 3950 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr); 3951 3952 if (HDR_HAS_L2HDR(hdr)) { 3953 ASSERT(hdr->b_l1hdr.b_pabd == NULL); 3954 ASSERT(!HDR_HAS_RABD(hdr)); 3955 /* 3956 * This buffer is cached on the 2nd Level ARC; 3957 * don't destroy the header. 3958 */ 3959 arc_change_state(arc_l2c_only, hdr); 3960 /* 3961 * dropping from L1+L2 cached to L2-only, 3962 * realloc to remove the L1 header. 3963 */ 3964 (void) arc_hdr_realloc(hdr, hdr_full_cache, 3965 hdr_l2only_cache); 3966 *real_evicted += HDR_FULL_SIZE - HDR_L2ONLY_SIZE; 3967 } else { 3968 arc_change_state(arc_anon, hdr); 3969 arc_hdr_destroy(hdr); 3970 *real_evicted += HDR_FULL_SIZE; 3971 } 3972 return (bytes_evicted); 3973 } 3974 3975 ASSERT(state == arc_mru || state == arc_mfu || state == arc_uncached); 3976 evicted_state = (state == arc_uncached) ? arc_anon : 3977 ((state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost); 3978 3979 /* prefetch buffers have a minimum lifespan */ 3980 if ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) && 3981 ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access < 3982 MSEC_TO_TICK(min_lifetime)) { 3983 ARCSTAT_BUMP(arcstat_evict_skip); 3984 return (bytes_evicted); 3985 } 3986 3987 if (HDR_HAS_L2HDR(hdr)) { 3988 ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr)); 3989 } else { 3990 if (l2arc_write_eligible(hdr->b_spa, hdr)) { 3991 ARCSTAT_INCR(arcstat_evict_l2_eligible, 3992 HDR_GET_LSIZE(hdr)); 3993 3994 switch (state->arcs_state) { 3995 case ARC_STATE_MRU: 3996 ARCSTAT_INCR( 3997 arcstat_evict_l2_eligible_mru, 3998 HDR_GET_LSIZE(hdr)); 3999 break; 4000 case ARC_STATE_MFU: 4001 ARCSTAT_INCR( 4002 arcstat_evict_l2_eligible_mfu, 4003 HDR_GET_LSIZE(hdr)); 4004 break; 4005 default: 4006 break; 4007 } 4008 } else { 4009 ARCSTAT_INCR(arcstat_evict_l2_ineligible, 4010 HDR_GET_LSIZE(hdr)); 4011 } 4012 } 4013 4014 bytes_evicted += arc_hdr_size(hdr); 4015 *real_evicted += arc_hdr_size(hdr); 4016 4017 /* 4018 * If this hdr is being evicted and has a compressed buffer then we 4019 * discard it here before we change states. This ensures that the 4020 * accounting is updated correctly in arc_free_data_impl(). 4021 */ 4022 if (hdr->b_l1hdr.b_pabd != NULL) 4023 arc_hdr_free_abd(hdr, B_FALSE); 4024 4025 if (HDR_HAS_RABD(hdr)) 4026 arc_hdr_free_abd(hdr, B_TRUE); 4027 4028 arc_change_state(evicted_state, hdr); 4029 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr); 4030 if (evicted_state == arc_anon) { 4031 arc_hdr_destroy(hdr); 4032 *real_evicted += HDR_FULL_SIZE; 4033 } else { 4034 ASSERT(HDR_IN_HASH_TABLE(hdr)); 4035 } 4036 4037 return (bytes_evicted); 4038 } 4039 4040 static void 4041 arc_set_need_free(void) 4042 { 4043 ASSERT(MUTEX_HELD(&arc_evict_lock)); 4044 int64_t remaining = arc_free_memory() - arc_sys_free / 2; 4045 arc_evict_waiter_t *aw = list_tail(&arc_evict_waiters); 4046 if (aw == NULL) { 4047 arc_need_free = MAX(-remaining, 0); 4048 } else { 4049 arc_need_free = 4050 MAX(-remaining, (int64_t)(aw->aew_count - arc_evict_count)); 4051 } 4052 } 4053 4054 static uint64_t 4055 arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker, 4056 uint64_t spa, uint64_t bytes) 4057 { 4058 multilist_sublist_t *mls; 4059 uint64_t bytes_evicted = 0, real_evicted = 0; 4060 arc_buf_hdr_t *hdr; 4061 kmutex_t *hash_lock; 4062 uint_t evict_count = zfs_arc_evict_batch_limit; 4063 4064 ASSERT3P(marker, !=, NULL); 4065 4066 mls = multilist_sublist_lock(ml, idx); 4067 4068 for (hdr = multilist_sublist_prev(mls, marker); likely(hdr != NULL); 4069 hdr = multilist_sublist_prev(mls, marker)) { 4070 if ((evict_count == 0) || (bytes_evicted >= bytes)) 4071 break; 4072 4073 /* 4074 * To keep our iteration location, move the marker 4075 * forward. Since we're not holding hdr's hash lock, we 4076 * must be very careful and not remove 'hdr' from the 4077 * sublist. Otherwise, other consumers might mistake the 4078 * 'hdr' as not being on a sublist when they call the 4079 * multilist_link_active() function (they all rely on 4080 * the hash lock protecting concurrent insertions and 4081 * removals). multilist_sublist_move_forward() was 4082 * specifically implemented to ensure this is the case 4083 * (only 'marker' will be removed and re-inserted). 4084 */ 4085 multilist_sublist_move_forward(mls, marker); 4086 4087 /* 4088 * The only case where the b_spa field should ever be 4089 * zero, is the marker headers inserted by 4090 * arc_evict_state(). It's possible for multiple threads 4091 * to be calling arc_evict_state() concurrently (e.g. 4092 * dsl_pool_close() and zio_inject_fault()), so we must 4093 * skip any markers we see from these other threads. 4094 */ 4095 if (hdr->b_spa == 0) 4096 continue; 4097 4098 /* we're only interested in evicting buffers of a certain spa */ 4099 if (spa != 0 && hdr->b_spa != spa) { 4100 ARCSTAT_BUMP(arcstat_evict_skip); 4101 continue; 4102 } 4103 4104 hash_lock = HDR_LOCK(hdr); 4105 4106 /* 4107 * We aren't calling this function from any code path 4108 * that would already be holding a hash lock, so we're 4109 * asserting on this assumption to be defensive in case 4110 * this ever changes. Without this check, it would be 4111 * possible to incorrectly increment arcstat_mutex_miss 4112 * below (e.g. if the code changed such that we called 4113 * this function with a hash lock held). 4114 */ 4115 ASSERT(!MUTEX_HELD(hash_lock)); 4116 4117 if (mutex_tryenter(hash_lock)) { 4118 uint64_t revicted; 4119 uint64_t evicted = arc_evict_hdr(hdr, &revicted); 4120 mutex_exit(hash_lock); 4121 4122 bytes_evicted += evicted; 4123 real_evicted += revicted; 4124 4125 /* 4126 * If evicted is zero, arc_evict_hdr() must have 4127 * decided to skip this header, don't increment 4128 * evict_count in this case. 4129 */ 4130 if (evicted != 0) 4131 evict_count--; 4132 4133 } else { 4134 ARCSTAT_BUMP(arcstat_mutex_miss); 4135 } 4136 } 4137 4138 multilist_sublist_unlock(mls); 4139 4140 /* 4141 * Increment the count of evicted bytes, and wake up any threads that 4142 * are waiting for the count to reach this value. Since the list is 4143 * ordered by ascending aew_count, we pop off the beginning of the 4144 * list until we reach the end, or a waiter that's past the current 4145 * "count". Doing this outside the loop reduces the number of times 4146 * we need to acquire the global arc_evict_lock. 4147 * 4148 * Only wake when there's sufficient free memory in the system 4149 * (specifically, arc_sys_free/2, which by default is a bit more than 4150 * 1/64th of RAM). See the comments in arc_wait_for_eviction(). 4151 */ 4152 mutex_enter(&arc_evict_lock); 4153 arc_evict_count += real_evicted; 4154 4155 if (arc_free_memory() > arc_sys_free / 2) { 4156 arc_evict_waiter_t *aw; 4157 while ((aw = list_head(&arc_evict_waiters)) != NULL && 4158 aw->aew_count <= arc_evict_count) { 4159 list_remove(&arc_evict_waiters, aw); 4160 cv_broadcast(&aw->aew_cv); 4161 } 4162 } 4163 arc_set_need_free(); 4164 mutex_exit(&arc_evict_lock); 4165 4166 /* 4167 * If the ARC size is reduced from arc_c_max to arc_c_min (especially 4168 * if the average cached block is small), eviction can be on-CPU for 4169 * many seconds. To ensure that other threads that may be bound to 4170 * this CPU are able to make progress, make a voluntary preemption 4171 * call here. 4172 */ 4173 kpreempt(KPREEMPT_SYNC); 4174 4175 return (bytes_evicted); 4176 } 4177 4178 /* 4179 * Allocate an array of buffer headers used as placeholders during arc state 4180 * eviction. 4181 */ 4182 static arc_buf_hdr_t ** 4183 arc_state_alloc_markers(int count) 4184 { 4185 arc_buf_hdr_t **markers; 4186 4187 markers = kmem_zalloc(sizeof (*markers) * count, KM_SLEEP); 4188 for (int i = 0; i < count; i++) { 4189 markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP); 4190 4191 /* 4192 * A b_spa of 0 is used to indicate that this header is 4193 * a marker. This fact is used in arc_evict_state_impl(). 4194 */ 4195 markers[i]->b_spa = 0; 4196 4197 } 4198 return (markers); 4199 } 4200 4201 static void 4202 arc_state_free_markers(arc_buf_hdr_t **markers, int count) 4203 { 4204 for (int i = 0; i < count; i++) 4205 kmem_cache_free(hdr_full_cache, markers[i]); 4206 kmem_free(markers, sizeof (*markers) * count); 4207 } 4208 4209 /* 4210 * Evict buffers from the given arc state, until we've removed the 4211 * specified number of bytes. Move the removed buffers to the 4212 * appropriate evict state. 4213 * 4214 * This function makes a "best effort". It skips over any buffers 4215 * it can't get a hash_lock on, and so, may not catch all candidates. 4216 * It may also return without evicting as much space as requested. 4217 * 4218 * If bytes is specified using the special value ARC_EVICT_ALL, this 4219 * will evict all available (i.e. unlocked and evictable) buffers from 4220 * the given arc state; which is used by arc_flush(). 4221 */ 4222 static uint64_t 4223 arc_evict_state(arc_state_t *state, arc_buf_contents_t type, uint64_t spa, 4224 uint64_t bytes) 4225 { 4226 uint64_t total_evicted = 0; 4227 multilist_t *ml = &state->arcs_list[type]; 4228 int num_sublists; 4229 arc_buf_hdr_t **markers; 4230 4231 num_sublists = multilist_get_num_sublists(ml); 4232 4233 /* 4234 * If we've tried to evict from each sublist, made some 4235 * progress, but still have not hit the target number of bytes 4236 * to evict, we want to keep trying. The markers allow us to 4237 * pick up where we left off for each individual sublist, rather 4238 * than starting from the tail each time. 4239 */ 4240 if (zthr_iscurthread(arc_evict_zthr)) { 4241 markers = arc_state_evict_markers; 4242 ASSERT3S(num_sublists, <=, arc_state_evict_marker_count); 4243 } else { 4244 markers = arc_state_alloc_markers(num_sublists); 4245 } 4246 for (int i = 0; i < num_sublists; i++) { 4247 multilist_sublist_t *mls; 4248 4249 mls = multilist_sublist_lock(ml, i); 4250 multilist_sublist_insert_tail(mls, markers[i]); 4251 multilist_sublist_unlock(mls); 4252 } 4253 4254 /* 4255 * While we haven't hit our target number of bytes to evict, or 4256 * we're evicting all available buffers. 4257 */ 4258 while (total_evicted < bytes) { 4259 int sublist_idx = multilist_get_random_index(ml); 4260 uint64_t scan_evicted = 0; 4261 4262 /* 4263 * Start eviction using a randomly selected sublist, 4264 * this is to try and evenly balance eviction across all 4265 * sublists. Always starting at the same sublist 4266 * (e.g. index 0) would cause evictions to favor certain 4267 * sublists over others. 4268 */ 4269 for (int i = 0; i < num_sublists; i++) { 4270 uint64_t bytes_remaining; 4271 uint64_t bytes_evicted; 4272 4273 if (total_evicted < bytes) 4274 bytes_remaining = bytes - total_evicted; 4275 else 4276 break; 4277 4278 bytes_evicted = arc_evict_state_impl(ml, sublist_idx, 4279 markers[sublist_idx], spa, bytes_remaining); 4280 4281 scan_evicted += bytes_evicted; 4282 total_evicted += bytes_evicted; 4283 4284 /* we've reached the end, wrap to the beginning */ 4285 if (++sublist_idx >= num_sublists) 4286 sublist_idx = 0; 4287 } 4288 4289 /* 4290 * If we didn't evict anything during this scan, we have 4291 * no reason to believe we'll evict more during another 4292 * scan, so break the loop. 4293 */ 4294 if (scan_evicted == 0) { 4295 /* This isn't possible, let's make that obvious */ 4296 ASSERT3S(bytes, !=, 0); 4297 4298 /* 4299 * When bytes is ARC_EVICT_ALL, the only way to 4300 * break the loop is when scan_evicted is zero. 4301 * In that case, we actually have evicted enough, 4302 * so we don't want to increment the kstat. 4303 */ 4304 if (bytes != ARC_EVICT_ALL) { 4305 ASSERT3S(total_evicted, <, bytes); 4306 ARCSTAT_BUMP(arcstat_evict_not_enough); 4307 } 4308 4309 break; 4310 } 4311 } 4312 4313 for (int i = 0; i < num_sublists; i++) { 4314 multilist_sublist_t *mls = multilist_sublist_lock(ml, i); 4315 multilist_sublist_remove(mls, markers[i]); 4316 multilist_sublist_unlock(mls); 4317 } 4318 if (markers != arc_state_evict_markers) 4319 arc_state_free_markers(markers, num_sublists); 4320 4321 return (total_evicted); 4322 } 4323 4324 /* 4325 * Flush all "evictable" data of the given type from the arc state 4326 * specified. This will not evict any "active" buffers (i.e. referenced). 4327 * 4328 * When 'retry' is set to B_FALSE, the function will make a single pass 4329 * over the state and evict any buffers that it can. Since it doesn't 4330 * continually retry the eviction, it might end up leaving some buffers 4331 * in the ARC due to lock misses. 4332 * 4333 * When 'retry' is set to B_TRUE, the function will continually retry the 4334 * eviction until *all* evictable buffers have been removed from the 4335 * state. As a result, if concurrent insertions into the state are 4336 * allowed (e.g. if the ARC isn't shutting down), this function might 4337 * wind up in an infinite loop, continually trying to evict buffers. 4338 */ 4339 static uint64_t 4340 arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type, 4341 boolean_t retry) 4342 { 4343 uint64_t evicted = 0; 4344 4345 while (zfs_refcount_count(&state->arcs_esize[type]) != 0) { 4346 evicted += arc_evict_state(state, type, spa, ARC_EVICT_ALL); 4347 4348 if (!retry) 4349 break; 4350 } 4351 4352 return (evicted); 4353 } 4354 4355 /* 4356 * Evict the specified number of bytes from the state specified. This 4357 * function prevents us from trying to evict more from a state's list 4358 * than is "evictable", and to skip evicting altogether when passed a 4359 * negative value for "bytes". In contrast, arc_evict_state() will 4360 * evict everything it can, when passed a negative value for "bytes". 4361 */ 4362 static uint64_t 4363 arc_evict_impl(arc_state_t *state, arc_buf_contents_t type, int64_t bytes) 4364 { 4365 uint64_t delta; 4366 4367 if (bytes > 0 && zfs_refcount_count(&state->arcs_esize[type]) > 0) { 4368 delta = MIN(zfs_refcount_count(&state->arcs_esize[type]), 4369 bytes); 4370 return (arc_evict_state(state, type, 0, delta)); 4371 } 4372 4373 return (0); 4374 } 4375 4376 /* 4377 * Adjust specified fraction, taking into account initial ghost state(s) size, 4378 * ghost hit bytes towards increasing the fraction, ghost hit bytes towards 4379 * decreasing it, plus a balance factor, controlling the decrease rate, used 4380 * to balance metadata vs data. 4381 */ 4382 static uint64_t 4383 arc_evict_adj(uint64_t frac, uint64_t total, uint64_t up, uint64_t down, 4384 uint_t balance) 4385 { 4386 if (total < 8 || up + down == 0) 4387 return (frac); 4388 4389 /* 4390 * We should not have more ghost hits than ghost size, but they 4391 * may get close. Restrict maximum adjustment in that case. 4392 */ 4393 if (up + down >= total / 4) { 4394 uint64_t scale = (up + down) / (total / 8); 4395 up /= scale; 4396 down /= scale; 4397 } 4398 4399 /* Get maximal dynamic range by choosing optimal shifts. */ 4400 int s = highbit64(total); 4401 s = MIN(64 - s, 32); 4402 4403 uint64_t ofrac = (1ULL << 32) - frac; 4404 4405 if (frac >= 4 * ofrac) 4406 up /= frac / (2 * ofrac + 1); 4407 up = (up << s) / (total >> (32 - s)); 4408 if (ofrac >= 4 * frac) 4409 down /= ofrac / (2 * frac + 1); 4410 down = (down << s) / (total >> (32 - s)); 4411 down = down * 100 / balance; 4412 4413 return (frac + up - down); 4414 } 4415 4416 /* 4417 * Evict buffers from the cache, such that arcstat_size is capped by arc_c. 4418 */ 4419 static uint64_t 4420 arc_evict(void) 4421 { 4422 uint64_t asize, bytes, total_evicted = 0; 4423 int64_t e, mrud, mrum, mfud, mfum, w; 4424 static uint64_t ogrd, ogrm, ogfd, ogfm; 4425 static uint64_t gsrd, gsrm, gsfd, gsfm; 4426 uint64_t ngrd, ngrm, ngfd, ngfm; 4427 4428 /* Get current size of ARC states we can evict from. */ 4429 mrud = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_DATA]) + 4430 zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_DATA]); 4431 mrum = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_METADATA]) + 4432 zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_METADATA]); 4433 mfud = zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_DATA]); 4434 mfum = zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_METADATA]); 4435 uint64_t d = mrud + mfud; 4436 uint64_t m = mrum + mfum; 4437 uint64_t t = d + m; 4438 4439 /* Get ARC ghost hits since last eviction. */ 4440 ngrd = wmsum_value(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA]); 4441 uint64_t grd = ngrd - ogrd; 4442 ogrd = ngrd; 4443 ngrm = wmsum_value(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA]); 4444 uint64_t grm = ngrm - ogrm; 4445 ogrm = ngrm; 4446 ngfd = wmsum_value(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA]); 4447 uint64_t gfd = ngfd - ogfd; 4448 ogfd = ngfd; 4449 ngfm = wmsum_value(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA]); 4450 uint64_t gfm = ngfm - ogfm; 4451 ogfm = ngfm; 4452 4453 /* Adjust ARC states balance based on ghost hits. */ 4454 arc_meta = arc_evict_adj(arc_meta, gsrd + gsrm + gsfd + gsfm, 4455 grm + gfm, grd + gfd, zfs_arc_meta_balance); 4456 arc_pd = arc_evict_adj(arc_pd, gsrd + gsfd, grd, gfd, 100); 4457 arc_pm = arc_evict_adj(arc_pm, gsrm + gsfm, grm, gfm, 100); 4458 4459 asize = aggsum_value(&arc_sums.arcstat_size); 4460 int64_t wt = t - (asize - arc_c); 4461 4462 /* 4463 * Try to reduce pinned dnodes if more than 3/4 of wanted metadata 4464 * target is not evictable or if they go over arc_dnode_limit. 4465 */ 4466 int64_t prune = 0; 4467 int64_t dn = wmsum_value(&arc_sums.arcstat_dnode_size); 4468 w = wt * (int64_t)(arc_meta >> 16) >> 16; 4469 if (zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_METADATA]) + 4470 zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_METADATA]) - 4471 zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_METADATA]) - 4472 zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]) > 4473 w * 3 / 4) { 4474 prune = dn / sizeof (dnode_t) * 4475 zfs_arc_dnode_reduce_percent / 100; 4476 } else if (dn > arc_dnode_limit) { 4477 prune = (dn - arc_dnode_limit) / sizeof (dnode_t) * 4478 zfs_arc_dnode_reduce_percent / 100; 4479 } 4480 if (prune > 0) 4481 arc_prune_async(prune); 4482 4483 /* Evict MRU metadata. */ 4484 w = wt * (int64_t)(arc_meta * arc_pm >> 48) >> 16; 4485 e = MIN((int64_t)(asize - arc_c), (int64_t)(mrum - w)); 4486 bytes = arc_evict_impl(arc_mru, ARC_BUFC_METADATA, e); 4487 total_evicted += bytes; 4488 mrum -= bytes; 4489 asize -= bytes; 4490 4491 /* Evict MFU metadata. */ 4492 w = wt * (int64_t)(arc_meta >> 16) >> 16; 4493 e = MIN((int64_t)(asize - arc_c), (int64_t)(m - w)); 4494 bytes = arc_evict_impl(arc_mfu, ARC_BUFC_METADATA, e); 4495 total_evicted += bytes; 4496 mfum -= bytes; 4497 asize -= bytes; 4498 4499 /* Evict MRU data. */ 4500 wt -= m - total_evicted; 4501 w = wt * (int64_t)(arc_pd >> 16) >> 16; 4502 e = MIN((int64_t)(asize - arc_c), (int64_t)(mrud - w)); 4503 bytes = arc_evict_impl(arc_mru, ARC_BUFC_DATA, e); 4504 total_evicted += bytes; 4505 mrud -= bytes; 4506 asize -= bytes; 4507 4508 /* Evict MFU data. */ 4509 e = asize - arc_c; 4510 bytes = arc_evict_impl(arc_mfu, ARC_BUFC_DATA, e); 4511 mfud -= bytes; 4512 total_evicted += bytes; 4513 4514 /* 4515 * Evict ghost lists 4516 * 4517 * Size of each state's ghost list represents how much that state 4518 * may grow by shrinking the other states. Would it need to shrink 4519 * other states to zero (that is unlikely), its ghost size would be 4520 * equal to sum of other three state sizes. But excessive ghost 4521 * size may result in false ghost hits (too far back), that may 4522 * never result in real cache hits if several states are competing. 4523 * So choose some arbitraty point of 1/2 of other state sizes. 4524 */ 4525 gsrd = (mrum + mfud + mfum) / 2; 4526 e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]) - 4527 gsrd; 4528 (void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_DATA, e); 4529 4530 gsrm = (mrud + mfud + mfum) / 2; 4531 e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]) - 4532 gsrm; 4533 (void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_METADATA, e); 4534 4535 gsfd = (mrud + mrum + mfum) / 2; 4536 e = zfs_refcount_count(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]) - 4537 gsfd; 4538 (void) arc_evict_impl(arc_mfu_ghost, ARC_BUFC_DATA, e); 4539 4540 gsfm = (mrud + mrum + mfud) / 2; 4541 e = zfs_refcount_count(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]) - 4542 gsfm; 4543 (void) arc_evict_impl(arc_mfu_ghost, ARC_BUFC_METADATA, e); 4544 4545 return (total_evicted); 4546 } 4547 4548 void 4549 arc_flush(spa_t *spa, boolean_t retry) 4550 { 4551 uint64_t guid = 0; 4552 4553 /* 4554 * If retry is B_TRUE, a spa must not be specified since we have 4555 * no good way to determine if all of a spa's buffers have been 4556 * evicted from an arc state. 4557 */ 4558 ASSERT(!retry || spa == NULL); 4559 4560 if (spa != NULL) 4561 guid = spa_load_guid(spa); 4562 4563 (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry); 4564 (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry); 4565 4566 (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry); 4567 (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry); 4568 4569 (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry); 4570 (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry); 4571 4572 (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry); 4573 (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry); 4574 4575 (void) arc_flush_state(arc_uncached, guid, ARC_BUFC_DATA, retry); 4576 (void) arc_flush_state(arc_uncached, guid, ARC_BUFC_METADATA, retry); 4577 } 4578 4579 void 4580 arc_reduce_target_size(int64_t to_free) 4581 { 4582 uint64_t c = arc_c; 4583 4584 if (c <= arc_c_min) 4585 return; 4586 4587 /* 4588 * All callers want the ARC to actually evict (at least) this much 4589 * memory. Therefore we reduce from the lower of the current size and 4590 * the target size. This way, even if arc_c is much higher than 4591 * arc_size (as can be the case after many calls to arc_freed(), we will 4592 * immediately have arc_c < arc_size and therefore the arc_evict_zthr 4593 * will evict. 4594 */ 4595 uint64_t asize = aggsum_value(&arc_sums.arcstat_size); 4596 if (asize < c) 4597 to_free += c - asize; 4598 arc_c = MAX((int64_t)c - to_free, (int64_t)arc_c_min); 4599 4600 /* See comment in arc_evict_cb_check() on why lock+flag */ 4601 mutex_enter(&arc_evict_lock); 4602 arc_evict_needed = B_TRUE; 4603 mutex_exit(&arc_evict_lock); 4604 zthr_wakeup(arc_evict_zthr); 4605 } 4606 4607 /* 4608 * Determine if the system is under memory pressure and is asking 4609 * to reclaim memory. A return value of B_TRUE indicates that the system 4610 * is under memory pressure and that the arc should adjust accordingly. 4611 */ 4612 boolean_t 4613 arc_reclaim_needed(void) 4614 { 4615 return (arc_available_memory() < 0); 4616 } 4617 4618 void 4619 arc_kmem_reap_soon(void) 4620 { 4621 size_t i; 4622 kmem_cache_t *prev_cache = NULL; 4623 kmem_cache_t *prev_data_cache = NULL; 4624 4625 #ifdef _KERNEL 4626 #if defined(_ILP32) 4627 /* 4628 * Reclaim unused memory from all kmem caches. 4629 */ 4630 kmem_reap(); 4631 #endif 4632 #endif 4633 4634 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) { 4635 #if defined(_ILP32) 4636 /* reach upper limit of cache size on 32-bit */ 4637 if (zio_buf_cache[i] == NULL) 4638 break; 4639 #endif 4640 if (zio_buf_cache[i] != prev_cache) { 4641 prev_cache = zio_buf_cache[i]; 4642 kmem_cache_reap_now(zio_buf_cache[i]); 4643 } 4644 if (zio_data_buf_cache[i] != prev_data_cache) { 4645 prev_data_cache = zio_data_buf_cache[i]; 4646 kmem_cache_reap_now(zio_data_buf_cache[i]); 4647 } 4648 } 4649 kmem_cache_reap_now(buf_cache); 4650 kmem_cache_reap_now(hdr_full_cache); 4651 kmem_cache_reap_now(hdr_l2only_cache); 4652 kmem_cache_reap_now(zfs_btree_leaf_cache); 4653 abd_cache_reap_now(); 4654 } 4655 4656 static boolean_t 4657 arc_evict_cb_check(void *arg, zthr_t *zthr) 4658 { 4659 (void) arg, (void) zthr; 4660 4661 #ifdef ZFS_DEBUG 4662 /* 4663 * This is necessary in order to keep the kstat information 4664 * up to date for tools that display kstat data such as the 4665 * mdb ::arc dcmd and the Linux crash utility. These tools 4666 * typically do not call kstat's update function, but simply 4667 * dump out stats from the most recent update. Without 4668 * this call, these commands may show stale stats for the 4669 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even 4670 * with this call, the data might be out of date if the 4671 * evict thread hasn't been woken recently; but that should 4672 * suffice. The arc_state_t structures can be queried 4673 * directly if more accurate information is needed. 4674 */ 4675 if (arc_ksp != NULL) 4676 arc_ksp->ks_update(arc_ksp, KSTAT_READ); 4677 #endif 4678 4679 /* 4680 * We have to rely on arc_wait_for_eviction() to tell us when to 4681 * evict, rather than checking if we are overflowing here, so that we 4682 * are sure to not leave arc_wait_for_eviction() waiting on aew_cv. 4683 * If we have become "not overflowing" since arc_wait_for_eviction() 4684 * checked, we need to wake it up. We could broadcast the CV here, 4685 * but arc_wait_for_eviction() may have not yet gone to sleep. We 4686 * would need to use a mutex to ensure that this function doesn't 4687 * broadcast until arc_wait_for_eviction() has gone to sleep (e.g. 4688 * the arc_evict_lock). However, the lock ordering of such a lock 4689 * would necessarily be incorrect with respect to the zthr_lock, 4690 * which is held before this function is called, and is held by 4691 * arc_wait_for_eviction() when it calls zthr_wakeup(). 4692 */ 4693 if (arc_evict_needed) 4694 return (B_TRUE); 4695 4696 /* 4697 * If we have buffers in uncached state, evict them periodically. 4698 */ 4699 return ((zfs_refcount_count(&arc_uncached->arcs_esize[ARC_BUFC_DATA]) + 4700 zfs_refcount_count(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]) && 4701 ddi_get_lbolt() - arc_last_uncached_flush > 4702 MSEC_TO_TICK(arc_min_prefetch_ms / 2))); 4703 } 4704 4705 /* 4706 * Keep arc_size under arc_c by running arc_evict which evicts data 4707 * from the ARC. 4708 */ 4709 static void 4710 arc_evict_cb(void *arg, zthr_t *zthr) 4711 { 4712 (void) arg, (void) zthr; 4713 4714 uint64_t evicted = 0; 4715 fstrans_cookie_t cookie = spl_fstrans_mark(); 4716 4717 /* Always try to evict from uncached state. */ 4718 arc_last_uncached_flush = ddi_get_lbolt(); 4719 evicted += arc_flush_state(arc_uncached, 0, ARC_BUFC_DATA, B_FALSE); 4720 evicted += arc_flush_state(arc_uncached, 0, ARC_BUFC_METADATA, B_FALSE); 4721 4722 /* Evict from other states only if told to. */ 4723 if (arc_evict_needed) 4724 evicted += arc_evict(); 4725 4726 /* 4727 * If evicted is zero, we couldn't evict anything 4728 * via arc_evict(). This could be due to hash lock 4729 * collisions, but more likely due to the majority of 4730 * arc buffers being unevictable. Therefore, even if 4731 * arc_size is above arc_c, another pass is unlikely to 4732 * be helpful and could potentially cause us to enter an 4733 * infinite loop. Additionally, zthr_iscancelled() is 4734 * checked here so that if the arc is shutting down, the 4735 * broadcast will wake any remaining arc evict waiters. 4736 */ 4737 mutex_enter(&arc_evict_lock); 4738 arc_evict_needed = !zthr_iscancelled(arc_evict_zthr) && 4739 evicted > 0 && aggsum_compare(&arc_sums.arcstat_size, arc_c) > 0; 4740 if (!arc_evict_needed) { 4741 /* 4742 * We're either no longer overflowing, or we 4743 * can't evict anything more, so we should wake 4744 * arc_get_data_impl() sooner. 4745 */ 4746 arc_evict_waiter_t *aw; 4747 while ((aw = list_remove_head(&arc_evict_waiters)) != NULL) { 4748 cv_broadcast(&aw->aew_cv); 4749 } 4750 arc_set_need_free(); 4751 } 4752 mutex_exit(&arc_evict_lock); 4753 spl_fstrans_unmark(cookie); 4754 } 4755 4756 static boolean_t 4757 arc_reap_cb_check(void *arg, zthr_t *zthr) 4758 { 4759 (void) arg, (void) zthr; 4760 4761 int64_t free_memory = arc_available_memory(); 4762 static int reap_cb_check_counter = 0; 4763 4764 /* 4765 * If a kmem reap is already active, don't schedule more. We must 4766 * check for this because kmem_cache_reap_soon() won't actually 4767 * block on the cache being reaped (this is to prevent callers from 4768 * becoming implicitly blocked by a system-wide kmem reap -- which, 4769 * on a system with many, many full magazines, can take minutes). 4770 */ 4771 if (!kmem_cache_reap_active() && free_memory < 0) { 4772 4773 arc_no_grow = B_TRUE; 4774 arc_warm = B_TRUE; 4775 /* 4776 * Wait at least zfs_grow_retry (default 5) seconds 4777 * before considering growing. 4778 */ 4779 arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry); 4780 return (B_TRUE); 4781 } else if (free_memory < arc_c >> arc_no_grow_shift) { 4782 arc_no_grow = B_TRUE; 4783 } else if (gethrtime() >= arc_growtime) { 4784 arc_no_grow = B_FALSE; 4785 } 4786 4787 /* 4788 * Called unconditionally every 60 seconds to reclaim unused 4789 * zstd compression and decompression context. This is done 4790 * here to avoid the need for an independent thread. 4791 */ 4792 if (!((reap_cb_check_counter++) % 60)) 4793 zfs_zstd_cache_reap_now(); 4794 4795 return (B_FALSE); 4796 } 4797 4798 /* 4799 * Keep enough free memory in the system by reaping the ARC's kmem 4800 * caches. To cause more slabs to be reapable, we may reduce the 4801 * target size of the cache (arc_c), causing the arc_evict_cb() 4802 * to free more buffers. 4803 */ 4804 static void 4805 arc_reap_cb(void *arg, zthr_t *zthr) 4806 { 4807 (void) arg, (void) zthr; 4808 4809 int64_t free_memory; 4810 fstrans_cookie_t cookie = spl_fstrans_mark(); 4811 4812 /* 4813 * Kick off asynchronous kmem_reap()'s of all our caches. 4814 */ 4815 arc_kmem_reap_soon(); 4816 4817 /* 4818 * Wait at least arc_kmem_cache_reap_retry_ms between 4819 * arc_kmem_reap_soon() calls. Without this check it is possible to 4820 * end up in a situation where we spend lots of time reaping 4821 * caches, while we're near arc_c_min. Waiting here also gives the 4822 * subsequent free memory check a chance of finding that the 4823 * asynchronous reap has already freed enough memory, and we don't 4824 * need to call arc_reduce_target_size(). 4825 */ 4826 delay((hz * arc_kmem_cache_reap_retry_ms + 999) / 1000); 4827 4828 /* 4829 * Reduce the target size as needed to maintain the amount of free 4830 * memory in the system at a fraction of the arc_size (1/128th by 4831 * default). If oversubscribed (free_memory < 0) then reduce the 4832 * target arc_size by the deficit amount plus the fractional 4833 * amount. If free memory is positive but less than the fractional 4834 * amount, reduce by what is needed to hit the fractional amount. 4835 */ 4836 free_memory = arc_available_memory(); 4837 4838 int64_t can_free = arc_c - arc_c_min; 4839 if (can_free > 0) { 4840 int64_t to_free = (can_free >> arc_shrink_shift) - free_memory; 4841 if (to_free > 0) 4842 arc_reduce_target_size(to_free); 4843 } 4844 spl_fstrans_unmark(cookie); 4845 } 4846 4847 #ifdef _KERNEL 4848 /* 4849 * Determine the amount of memory eligible for eviction contained in the 4850 * ARC. All clean data reported by the ghost lists can always be safely 4851 * evicted. Due to arc_c_min, the same does not hold for all clean data 4852 * contained by the regular mru and mfu lists. 4853 * 4854 * In the case of the regular mru and mfu lists, we need to report as 4855 * much clean data as possible, such that evicting that same reported 4856 * data will not bring arc_size below arc_c_min. Thus, in certain 4857 * circumstances, the total amount of clean data in the mru and mfu 4858 * lists might not actually be evictable. 4859 * 4860 * The following two distinct cases are accounted for: 4861 * 4862 * 1. The sum of the amount of dirty data contained by both the mru and 4863 * mfu lists, plus the ARC's other accounting (e.g. the anon list), 4864 * is greater than or equal to arc_c_min. 4865 * (i.e. amount of dirty data >= arc_c_min) 4866 * 4867 * This is the easy case; all clean data contained by the mru and mfu 4868 * lists is evictable. Evicting all clean data can only drop arc_size 4869 * to the amount of dirty data, which is greater than arc_c_min. 4870 * 4871 * 2. The sum of the amount of dirty data contained by both the mru and 4872 * mfu lists, plus the ARC's other accounting (e.g. the anon list), 4873 * is less than arc_c_min. 4874 * (i.e. arc_c_min > amount of dirty data) 4875 * 4876 * 2.1. arc_size is greater than or equal arc_c_min. 4877 * (i.e. arc_size >= arc_c_min > amount of dirty data) 4878 * 4879 * In this case, not all clean data from the regular mru and mfu 4880 * lists is actually evictable; we must leave enough clean data 4881 * to keep arc_size above arc_c_min. Thus, the maximum amount of 4882 * evictable data from the two lists combined, is exactly the 4883 * difference between arc_size and arc_c_min. 4884 * 4885 * 2.2. arc_size is less than arc_c_min 4886 * (i.e. arc_c_min > arc_size > amount of dirty data) 4887 * 4888 * In this case, none of the data contained in the mru and mfu 4889 * lists is evictable, even if it's clean. Since arc_size is 4890 * already below arc_c_min, evicting any more would only 4891 * increase this negative difference. 4892 */ 4893 4894 #endif /* _KERNEL */ 4895 4896 /* 4897 * Adapt arc info given the number of bytes we are trying to add and 4898 * the state that we are coming from. This function is only called 4899 * when we are adding new content to the cache. 4900 */ 4901 static void 4902 arc_adapt(uint64_t bytes) 4903 { 4904 /* 4905 * Wake reap thread if we do not have any available memory 4906 */ 4907 if (arc_reclaim_needed()) { 4908 zthr_wakeup(arc_reap_zthr); 4909 return; 4910 } 4911 4912 if (arc_no_grow) 4913 return; 4914 4915 if (arc_c >= arc_c_max) 4916 return; 4917 4918 /* 4919 * If we're within (2 * maxblocksize) bytes of the target 4920 * cache size, increment the target cache size 4921 */ 4922 if (aggsum_upper_bound(&arc_sums.arcstat_size) + 4923 2 * SPA_MAXBLOCKSIZE >= arc_c) { 4924 uint64_t dc = MAX(bytes, SPA_OLD_MAXBLOCKSIZE); 4925 if (atomic_add_64_nv(&arc_c, dc) > arc_c_max) 4926 arc_c = arc_c_max; 4927 } 4928 } 4929 4930 /* 4931 * Check if arc_size has grown past our upper threshold, determined by 4932 * zfs_arc_overflow_shift. 4933 */ 4934 static arc_ovf_level_t 4935 arc_is_overflowing(boolean_t use_reserve) 4936 { 4937 /* Always allow at least one block of overflow */ 4938 int64_t overflow = MAX(SPA_MAXBLOCKSIZE, 4939 arc_c >> zfs_arc_overflow_shift); 4940 4941 /* 4942 * We just compare the lower bound here for performance reasons. Our 4943 * primary goals are to make sure that the arc never grows without 4944 * bound, and that it can reach its maximum size. This check 4945 * accomplishes both goals. The maximum amount we could run over by is 4946 * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block 4947 * in the ARC. In practice, that's in the tens of MB, which is low 4948 * enough to be safe. 4949 */ 4950 int64_t over = aggsum_lower_bound(&arc_sums.arcstat_size) - 4951 arc_c - overflow / 2; 4952 if (!use_reserve) 4953 overflow /= 2; 4954 return (over < 0 ? ARC_OVF_NONE : 4955 over < overflow ? ARC_OVF_SOME : ARC_OVF_SEVERE); 4956 } 4957 4958 static abd_t * 4959 arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, const void *tag, 4960 int alloc_flags) 4961 { 4962 arc_buf_contents_t type = arc_buf_type(hdr); 4963 4964 arc_get_data_impl(hdr, size, tag, alloc_flags); 4965 if (alloc_flags & ARC_HDR_ALLOC_LINEAR) 4966 return (abd_alloc_linear(size, type == ARC_BUFC_METADATA)); 4967 else 4968 return (abd_alloc(size, type == ARC_BUFC_METADATA)); 4969 } 4970 4971 static void * 4972 arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, const void *tag) 4973 { 4974 arc_buf_contents_t type = arc_buf_type(hdr); 4975 4976 arc_get_data_impl(hdr, size, tag, 0); 4977 if (type == ARC_BUFC_METADATA) { 4978 return (zio_buf_alloc(size)); 4979 } else { 4980 ASSERT(type == ARC_BUFC_DATA); 4981 return (zio_data_buf_alloc(size)); 4982 } 4983 } 4984 4985 /* 4986 * Wait for the specified amount of data (in bytes) to be evicted from the 4987 * ARC, and for there to be sufficient free memory in the system. Waiting for 4988 * eviction ensures that the memory used by the ARC decreases. Waiting for 4989 * free memory ensures that the system won't run out of free pages, regardless 4990 * of ARC behavior and settings. See arc_lowmem_init(). 4991 */ 4992 void 4993 arc_wait_for_eviction(uint64_t amount, boolean_t use_reserve) 4994 { 4995 switch (arc_is_overflowing(use_reserve)) { 4996 case ARC_OVF_NONE: 4997 return; 4998 case ARC_OVF_SOME: 4999 /* 5000 * This is a bit racy without taking arc_evict_lock, but the 5001 * worst that can happen is we either call zthr_wakeup() extra 5002 * time due to race with other thread here, or the set flag 5003 * get cleared by arc_evict_cb(), which is unlikely due to 5004 * big hysteresis, but also not important since at this level 5005 * of overflow the eviction is purely advisory. Same time 5006 * taking the global lock here every time without waiting for 5007 * the actual eviction creates a significant lock contention. 5008 */ 5009 if (!arc_evict_needed) { 5010 arc_evict_needed = B_TRUE; 5011 zthr_wakeup(arc_evict_zthr); 5012 } 5013 return; 5014 case ARC_OVF_SEVERE: 5015 default: 5016 { 5017 arc_evict_waiter_t aw; 5018 list_link_init(&aw.aew_node); 5019 cv_init(&aw.aew_cv, NULL, CV_DEFAULT, NULL); 5020 5021 uint64_t last_count = 0; 5022 mutex_enter(&arc_evict_lock); 5023 if (!list_is_empty(&arc_evict_waiters)) { 5024 arc_evict_waiter_t *last = 5025 list_tail(&arc_evict_waiters); 5026 last_count = last->aew_count; 5027 } else if (!arc_evict_needed) { 5028 arc_evict_needed = B_TRUE; 5029 zthr_wakeup(arc_evict_zthr); 5030 } 5031 /* 5032 * Note, the last waiter's count may be less than 5033 * arc_evict_count if we are low on memory in which 5034 * case arc_evict_state_impl() may have deferred 5035 * wakeups (but still incremented arc_evict_count). 5036 */ 5037 aw.aew_count = MAX(last_count, arc_evict_count) + amount; 5038 5039 list_insert_tail(&arc_evict_waiters, &aw); 5040 5041 arc_set_need_free(); 5042 5043 DTRACE_PROBE3(arc__wait__for__eviction, 5044 uint64_t, amount, 5045 uint64_t, arc_evict_count, 5046 uint64_t, aw.aew_count); 5047 5048 /* 5049 * We will be woken up either when arc_evict_count reaches 5050 * aew_count, or when the ARC is no longer overflowing and 5051 * eviction completes. 5052 * In case of "false" wakeup, we will still be on the list. 5053 */ 5054 do { 5055 cv_wait(&aw.aew_cv, &arc_evict_lock); 5056 } while (list_link_active(&aw.aew_node)); 5057 mutex_exit(&arc_evict_lock); 5058 5059 cv_destroy(&aw.aew_cv); 5060 } 5061 } 5062 } 5063 5064 /* 5065 * Allocate a block and return it to the caller. If we are hitting the 5066 * hard limit for the cache size, we must sleep, waiting for the eviction 5067 * thread to catch up. If we're past the target size but below the hard 5068 * limit, we'll only signal the reclaim thread and continue on. 5069 */ 5070 static void 5071 arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, const void *tag, 5072 int alloc_flags) 5073 { 5074 arc_adapt(size); 5075 5076 /* 5077 * If arc_size is currently overflowing, we must be adding data 5078 * faster than we are evicting. To ensure we don't compound the 5079 * problem by adding more data and forcing arc_size to grow even 5080 * further past it's target size, we wait for the eviction thread to 5081 * make some progress. We also wait for there to be sufficient free 5082 * memory in the system, as measured by arc_free_memory(). 5083 * 5084 * Specifically, we wait for zfs_arc_eviction_pct percent of the 5085 * requested size to be evicted. This should be more than 100%, to 5086 * ensure that that progress is also made towards getting arc_size 5087 * under arc_c. See the comment above zfs_arc_eviction_pct. 5088 */ 5089 arc_wait_for_eviction(size * zfs_arc_eviction_pct / 100, 5090 alloc_flags & ARC_HDR_USE_RESERVE); 5091 5092 arc_buf_contents_t type = arc_buf_type(hdr); 5093 if (type == ARC_BUFC_METADATA) { 5094 arc_space_consume(size, ARC_SPACE_META); 5095 } else { 5096 arc_space_consume(size, ARC_SPACE_DATA); 5097 } 5098 5099 /* 5100 * Update the state size. Note that ghost states have a 5101 * "ghost size" and so don't need to be updated. 5102 */ 5103 arc_state_t *state = hdr->b_l1hdr.b_state; 5104 if (!GHOST_STATE(state)) { 5105 5106 (void) zfs_refcount_add_many(&state->arcs_size[type], size, 5107 tag); 5108 5109 /* 5110 * If this is reached via arc_read, the link is 5111 * protected by the hash lock. If reached via 5112 * arc_buf_alloc, the header should not be accessed by 5113 * any other thread. And, if reached via arc_read_done, 5114 * the hash lock will protect it if it's found in the 5115 * hash table; otherwise no other thread should be 5116 * trying to [add|remove]_reference it. 5117 */ 5118 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 5119 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5120 (void) zfs_refcount_add_many(&state->arcs_esize[type], 5121 size, tag); 5122 } 5123 } 5124 } 5125 5126 static void 5127 arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size, 5128 const void *tag) 5129 { 5130 arc_free_data_impl(hdr, size, tag); 5131 abd_free(abd); 5132 } 5133 5134 static void 5135 arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, const void *tag) 5136 { 5137 arc_buf_contents_t type = arc_buf_type(hdr); 5138 5139 arc_free_data_impl(hdr, size, tag); 5140 if (type == ARC_BUFC_METADATA) { 5141 zio_buf_free(buf, size); 5142 } else { 5143 ASSERT(type == ARC_BUFC_DATA); 5144 zio_data_buf_free(buf, size); 5145 } 5146 } 5147 5148 /* 5149 * Free the arc data buffer. 5150 */ 5151 static void 5152 arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, const void *tag) 5153 { 5154 arc_state_t *state = hdr->b_l1hdr.b_state; 5155 arc_buf_contents_t type = arc_buf_type(hdr); 5156 5157 /* protected by hash lock, if in the hash table */ 5158 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) { 5159 ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)); 5160 ASSERT(state != arc_anon && state != arc_l2c_only); 5161 5162 (void) zfs_refcount_remove_many(&state->arcs_esize[type], 5163 size, tag); 5164 } 5165 (void) zfs_refcount_remove_many(&state->arcs_size[type], size, tag); 5166 5167 VERIFY3U(hdr->b_type, ==, type); 5168 if (type == ARC_BUFC_METADATA) { 5169 arc_space_return(size, ARC_SPACE_META); 5170 } else { 5171 ASSERT(type == ARC_BUFC_DATA); 5172 arc_space_return(size, ARC_SPACE_DATA); 5173 } 5174 } 5175 5176 /* 5177 * This routine is called whenever a buffer is accessed. 5178 */ 5179 static void 5180 arc_access(arc_buf_hdr_t *hdr, arc_flags_t arc_flags, boolean_t hit) 5181 { 5182 ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 5183 ASSERT(HDR_HAS_L1HDR(hdr)); 5184 5185 /* 5186 * Update buffer prefetch status. 5187 */ 5188 boolean_t was_prefetch = HDR_PREFETCH(hdr); 5189 boolean_t now_prefetch = arc_flags & ARC_FLAG_PREFETCH; 5190 if (was_prefetch != now_prefetch) { 5191 if (was_prefetch) { 5192 ARCSTAT_CONDSTAT(hit, demand_hit, demand_iohit, 5193 HDR_PRESCIENT_PREFETCH(hdr), prescient, predictive, 5194 prefetch); 5195 } 5196 if (HDR_HAS_L2HDR(hdr)) 5197 l2arc_hdr_arcstats_decrement_state(hdr); 5198 if (was_prefetch) { 5199 arc_hdr_clear_flags(hdr, 5200 ARC_FLAG_PREFETCH | ARC_FLAG_PRESCIENT_PREFETCH); 5201 } else { 5202 arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH); 5203 } 5204 if (HDR_HAS_L2HDR(hdr)) 5205 l2arc_hdr_arcstats_increment_state(hdr); 5206 } 5207 if (now_prefetch) { 5208 if (arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) { 5209 arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH); 5210 ARCSTAT_BUMP(arcstat_prescient_prefetch); 5211 } else { 5212 ARCSTAT_BUMP(arcstat_predictive_prefetch); 5213 } 5214 } 5215 if (arc_flags & ARC_FLAG_L2CACHE) 5216 arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 5217 5218 clock_t now = ddi_get_lbolt(); 5219 if (hdr->b_l1hdr.b_state == arc_anon) { 5220 arc_state_t *new_state; 5221 /* 5222 * This buffer is not in the cache, and does not appear in 5223 * our "ghost" lists. Add it to the MRU or uncached state. 5224 */ 5225 ASSERT0(hdr->b_l1hdr.b_arc_access); 5226 hdr->b_l1hdr.b_arc_access = now; 5227 if (HDR_UNCACHED(hdr)) { 5228 new_state = arc_uncached; 5229 DTRACE_PROBE1(new_state__uncached, arc_buf_hdr_t *, 5230 hdr); 5231 } else { 5232 new_state = arc_mru; 5233 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 5234 } 5235 arc_change_state(new_state, hdr); 5236 } else if (hdr->b_l1hdr.b_state == arc_mru) { 5237 /* 5238 * This buffer has been accessed once recently and either 5239 * its read is still in progress or it is in the cache. 5240 */ 5241 if (HDR_IO_IN_PROGRESS(hdr)) { 5242 hdr->b_l1hdr.b_arc_access = now; 5243 return; 5244 } 5245 hdr->b_l1hdr.b_mru_hits++; 5246 ARCSTAT_BUMP(arcstat_mru_hits); 5247 5248 /* 5249 * If the previous access was a prefetch, then it already 5250 * handled possible promotion, so nothing more to do for now. 5251 */ 5252 if (was_prefetch) { 5253 hdr->b_l1hdr.b_arc_access = now; 5254 return; 5255 } 5256 5257 /* 5258 * If more than ARC_MINTIME have passed from the previous 5259 * hit, promote the buffer to the MFU state. 5260 */ 5261 if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access + 5262 ARC_MINTIME)) { 5263 hdr->b_l1hdr.b_arc_access = now; 5264 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 5265 arc_change_state(arc_mfu, hdr); 5266 } 5267 } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) { 5268 arc_state_t *new_state; 5269 /* 5270 * This buffer has been accessed once recently, but was 5271 * evicted from the cache. Would we have bigger MRU, it 5272 * would be an MRU hit, so handle it the same way, except 5273 * we don't need to check the previous access time. 5274 */ 5275 hdr->b_l1hdr.b_mru_ghost_hits++; 5276 ARCSTAT_BUMP(arcstat_mru_ghost_hits); 5277 hdr->b_l1hdr.b_arc_access = now; 5278 wmsum_add(&arc_mru_ghost->arcs_hits[arc_buf_type(hdr)], 5279 arc_hdr_size(hdr)); 5280 if (was_prefetch) { 5281 new_state = arc_mru; 5282 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 5283 } else { 5284 new_state = arc_mfu; 5285 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 5286 } 5287 arc_change_state(new_state, hdr); 5288 } else if (hdr->b_l1hdr.b_state == arc_mfu) { 5289 /* 5290 * This buffer has been accessed more than once and either 5291 * still in the cache or being restored from one of ghosts. 5292 */ 5293 if (!HDR_IO_IN_PROGRESS(hdr)) { 5294 hdr->b_l1hdr.b_mfu_hits++; 5295 ARCSTAT_BUMP(arcstat_mfu_hits); 5296 } 5297 hdr->b_l1hdr.b_arc_access = now; 5298 } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) { 5299 /* 5300 * This buffer has been accessed more than once recently, but 5301 * has been evicted from the cache. Would we have bigger MFU 5302 * it would stay in cache, so move it back to MFU state. 5303 */ 5304 hdr->b_l1hdr.b_mfu_ghost_hits++; 5305 ARCSTAT_BUMP(arcstat_mfu_ghost_hits); 5306 hdr->b_l1hdr.b_arc_access = now; 5307 wmsum_add(&arc_mfu_ghost->arcs_hits[arc_buf_type(hdr)], 5308 arc_hdr_size(hdr)); 5309 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr); 5310 arc_change_state(arc_mfu, hdr); 5311 } else if (hdr->b_l1hdr.b_state == arc_uncached) { 5312 /* 5313 * This buffer is uncacheable, but we got a hit. Probably 5314 * a demand read after prefetch. Nothing more to do here. 5315 */ 5316 if (!HDR_IO_IN_PROGRESS(hdr)) 5317 ARCSTAT_BUMP(arcstat_uncached_hits); 5318 hdr->b_l1hdr.b_arc_access = now; 5319 } else if (hdr->b_l1hdr.b_state == arc_l2c_only) { 5320 /* 5321 * This buffer is on the 2nd Level ARC and was not accessed 5322 * for a long time, so treat it as new and put into MRU. 5323 */ 5324 hdr->b_l1hdr.b_arc_access = now; 5325 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr); 5326 arc_change_state(arc_mru, hdr); 5327 } else { 5328 cmn_err(CE_PANIC, "invalid arc state 0x%p", 5329 hdr->b_l1hdr.b_state); 5330 } 5331 } 5332 5333 /* 5334 * This routine is called by dbuf_hold() to update the arc_access() state 5335 * which otherwise would be skipped for entries in the dbuf cache. 5336 */ 5337 void 5338 arc_buf_access(arc_buf_t *buf) 5339 { 5340 arc_buf_hdr_t *hdr = buf->b_hdr; 5341 5342 /* 5343 * Avoid taking the hash_lock when possible as an optimization. 5344 * The header must be checked again under the hash_lock in order 5345 * to handle the case where it is concurrently being released. 5346 */ 5347 if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) 5348 return; 5349 5350 kmutex_t *hash_lock = HDR_LOCK(hdr); 5351 mutex_enter(hash_lock); 5352 5353 if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) { 5354 mutex_exit(hash_lock); 5355 ARCSTAT_BUMP(arcstat_access_skip); 5356 return; 5357 } 5358 5359 ASSERT(hdr->b_l1hdr.b_state == arc_mru || 5360 hdr->b_l1hdr.b_state == arc_mfu || 5361 hdr->b_l1hdr.b_state == arc_uncached); 5362 5363 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 5364 arc_access(hdr, 0, B_TRUE); 5365 mutex_exit(hash_lock); 5366 5367 ARCSTAT_BUMP(arcstat_hits); 5368 ARCSTAT_CONDSTAT(B_TRUE /* demand */, demand, prefetch, 5369 !HDR_ISTYPE_METADATA(hdr), data, metadata, hits); 5370 } 5371 5372 /* a generic arc_read_done_func_t which you can use */ 5373 void 5374 arc_bcopy_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, 5375 arc_buf_t *buf, void *arg) 5376 { 5377 (void) zio, (void) zb, (void) bp; 5378 5379 if (buf == NULL) 5380 return; 5381 5382 memcpy(arg, buf->b_data, arc_buf_size(buf)); 5383 arc_buf_destroy(buf, arg); 5384 } 5385 5386 /* a generic arc_read_done_func_t */ 5387 void 5388 arc_getbuf_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, 5389 arc_buf_t *buf, void *arg) 5390 { 5391 (void) zb, (void) bp; 5392 arc_buf_t **bufp = arg; 5393 5394 if (buf == NULL) { 5395 ASSERT(zio == NULL || zio->io_error != 0); 5396 *bufp = NULL; 5397 } else { 5398 ASSERT(zio == NULL || zio->io_error == 0); 5399 *bufp = buf; 5400 ASSERT(buf->b_data != NULL); 5401 } 5402 } 5403 5404 static void 5405 arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp) 5406 { 5407 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { 5408 ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0); 5409 ASSERT3U(arc_hdr_get_compress(hdr), ==, ZIO_COMPRESS_OFF); 5410 } else { 5411 if (HDR_COMPRESSION_ENABLED(hdr)) { 5412 ASSERT3U(arc_hdr_get_compress(hdr), ==, 5413 BP_GET_COMPRESS(bp)); 5414 } 5415 ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); 5416 ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp)); 5417 ASSERT3U(!!HDR_PROTECTED(hdr), ==, BP_IS_PROTECTED(bp)); 5418 } 5419 } 5420 5421 static void 5422 arc_read_done(zio_t *zio) 5423 { 5424 blkptr_t *bp = zio->io_bp; 5425 arc_buf_hdr_t *hdr = zio->io_private; 5426 kmutex_t *hash_lock = NULL; 5427 arc_callback_t *callback_list; 5428 arc_callback_t *acb; 5429 5430 /* 5431 * The hdr was inserted into hash-table and removed from lists 5432 * prior to starting I/O. We should find this header, since 5433 * it's in the hash table, and it should be legit since it's 5434 * not possible to evict it during the I/O. The only possible 5435 * reason for it not to be found is if we were freed during the 5436 * read. 5437 */ 5438 if (HDR_IN_HASH_TABLE(hdr)) { 5439 arc_buf_hdr_t *found; 5440 5441 ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp)); 5442 ASSERT3U(hdr->b_dva.dva_word[0], ==, 5443 BP_IDENTITY(zio->io_bp)->dva_word[0]); 5444 ASSERT3U(hdr->b_dva.dva_word[1], ==, 5445 BP_IDENTITY(zio->io_bp)->dva_word[1]); 5446 5447 found = buf_hash_find(hdr->b_spa, zio->io_bp, &hash_lock); 5448 5449 ASSERT((found == hdr && 5450 DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) || 5451 (found == hdr && HDR_L2_READING(hdr))); 5452 ASSERT3P(hash_lock, !=, NULL); 5453 } 5454 5455 if (BP_IS_PROTECTED(bp)) { 5456 hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); 5457 hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; 5458 zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, 5459 hdr->b_crypt_hdr.b_iv); 5460 5461 if (zio->io_error == 0) { 5462 if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) { 5463 void *tmpbuf; 5464 5465 tmpbuf = abd_borrow_buf_copy(zio->io_abd, 5466 sizeof (zil_chain_t)); 5467 zio_crypt_decode_mac_zil(tmpbuf, 5468 hdr->b_crypt_hdr.b_mac); 5469 abd_return_buf(zio->io_abd, tmpbuf, 5470 sizeof (zil_chain_t)); 5471 } else { 5472 zio_crypt_decode_mac_bp(bp, 5473 hdr->b_crypt_hdr.b_mac); 5474 } 5475 } 5476 } 5477 5478 if (zio->io_error == 0) { 5479 /* byteswap if necessary */ 5480 if (BP_SHOULD_BYTESWAP(zio->io_bp)) { 5481 if (BP_GET_LEVEL(zio->io_bp) > 0) { 5482 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; 5483 } else { 5484 hdr->b_l1hdr.b_byteswap = 5485 DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp)); 5486 } 5487 } else { 5488 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 5489 } 5490 if (!HDR_L2_READING(hdr)) { 5491 hdr->b_complevel = zio->io_prop.zp_complevel; 5492 } 5493 } 5494 5495 arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED); 5496 if (l2arc_noprefetch && HDR_PREFETCH(hdr)) 5497 arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE); 5498 5499 callback_list = hdr->b_l1hdr.b_acb; 5500 ASSERT3P(callback_list, !=, NULL); 5501 hdr->b_l1hdr.b_acb = NULL; 5502 5503 /* 5504 * If a read request has a callback (i.e. acb_done is not NULL), then we 5505 * make a buf containing the data according to the parameters which were 5506 * passed in. The implementation of arc_buf_alloc_impl() ensures that we 5507 * aren't needlessly decompressing the data multiple times. 5508 */ 5509 int callback_cnt = 0; 5510 for (acb = callback_list; acb != NULL; acb = acb->acb_next) { 5511 5512 /* We need the last one to call below in original order. */ 5513 callback_list = acb; 5514 5515 if (!acb->acb_done || acb->acb_nobuf) 5516 continue; 5517 5518 callback_cnt++; 5519 5520 if (zio->io_error != 0) 5521 continue; 5522 5523 int error = arc_buf_alloc_impl(hdr, zio->io_spa, 5524 &acb->acb_zb, acb->acb_private, acb->acb_encrypted, 5525 acb->acb_compressed, acb->acb_noauth, B_TRUE, 5526 &acb->acb_buf); 5527 5528 /* 5529 * Assert non-speculative zios didn't fail because an 5530 * encryption key wasn't loaded 5531 */ 5532 ASSERT((zio->io_flags & ZIO_FLAG_SPECULATIVE) || 5533 error != EACCES); 5534 5535 /* 5536 * If we failed to decrypt, report an error now (as the zio 5537 * layer would have done if it had done the transforms). 5538 */ 5539 if (error == ECKSUM) { 5540 ASSERT(BP_IS_PROTECTED(bp)); 5541 error = SET_ERROR(EIO); 5542 if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) { 5543 spa_log_error(zio->io_spa, &acb->acb_zb, 5544 &zio->io_bp->blk_birth); 5545 (void) zfs_ereport_post( 5546 FM_EREPORT_ZFS_AUTHENTICATION, 5547 zio->io_spa, NULL, &acb->acb_zb, zio, 0); 5548 } 5549 } 5550 5551 if (error != 0) { 5552 /* 5553 * Decompression or decryption failed. Set 5554 * io_error so that when we call acb_done 5555 * (below), we will indicate that the read 5556 * failed. Note that in the unusual case 5557 * where one callback is compressed and another 5558 * uncompressed, we will mark all of them 5559 * as failed, even though the uncompressed 5560 * one can't actually fail. In this case, 5561 * the hdr will not be anonymous, because 5562 * if there are multiple callbacks, it's 5563 * because multiple threads found the same 5564 * arc buf in the hash table. 5565 */ 5566 zio->io_error = error; 5567 } 5568 } 5569 5570 /* 5571 * If there are multiple callbacks, we must have the hash lock, 5572 * because the only way for multiple threads to find this hdr is 5573 * in the hash table. This ensures that if there are multiple 5574 * callbacks, the hdr is not anonymous. If it were anonymous, 5575 * we couldn't use arc_buf_destroy() in the error case below. 5576 */ 5577 ASSERT(callback_cnt < 2 || hash_lock != NULL); 5578 5579 if (zio->io_error == 0) { 5580 arc_hdr_verify(hdr, zio->io_bp); 5581 } else { 5582 arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR); 5583 if (hdr->b_l1hdr.b_state != arc_anon) 5584 arc_change_state(arc_anon, hdr); 5585 if (HDR_IN_HASH_TABLE(hdr)) 5586 buf_hash_remove(hdr); 5587 } 5588 5589 /* 5590 * Broadcast before we drop the hash_lock to avoid the possibility 5591 * that the hdr (and hence the cv) might be freed before we get to 5592 * the cv_broadcast(). 5593 */ 5594 cv_broadcast(&hdr->b_l1hdr.b_cv); 5595 5596 arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5597 (void) remove_reference(hdr, hdr); 5598 5599 if (hash_lock != NULL) 5600 mutex_exit(hash_lock); 5601 5602 /* execute each callback and free its structure */ 5603 while ((acb = callback_list) != NULL) { 5604 if (acb->acb_done != NULL) { 5605 if (zio->io_error != 0 && acb->acb_buf != NULL) { 5606 /* 5607 * If arc_buf_alloc_impl() fails during 5608 * decompression, the buf will still be 5609 * allocated, and needs to be freed here. 5610 */ 5611 arc_buf_destroy(acb->acb_buf, 5612 acb->acb_private); 5613 acb->acb_buf = NULL; 5614 } 5615 acb->acb_done(zio, &zio->io_bookmark, zio->io_bp, 5616 acb->acb_buf, acb->acb_private); 5617 } 5618 5619 if (acb->acb_zio_dummy != NULL) { 5620 acb->acb_zio_dummy->io_error = zio->io_error; 5621 zio_nowait(acb->acb_zio_dummy); 5622 } 5623 5624 callback_list = acb->acb_prev; 5625 if (acb->acb_wait) { 5626 mutex_enter(&acb->acb_wait_lock); 5627 acb->acb_wait_error = zio->io_error; 5628 acb->acb_wait = B_FALSE; 5629 cv_signal(&acb->acb_wait_cv); 5630 mutex_exit(&acb->acb_wait_lock); 5631 /* acb will be freed by the waiting thread. */ 5632 } else { 5633 kmem_free(acb, sizeof (arc_callback_t)); 5634 } 5635 } 5636 } 5637 5638 /* 5639 * "Read" the block at the specified DVA (in bp) via the 5640 * cache. If the block is found in the cache, invoke the provided 5641 * callback immediately and return. Note that the `zio' parameter 5642 * in the callback will be NULL in this case, since no IO was 5643 * required. If the block is not in the cache pass the read request 5644 * on to the spa with a substitute callback function, so that the 5645 * requested block will be added to the cache. 5646 * 5647 * If a read request arrives for a block that has a read in-progress, 5648 * either wait for the in-progress read to complete (and return the 5649 * results); or, if this is a read with a "done" func, add a record 5650 * to the read to invoke the "done" func when the read completes, 5651 * and return; or just return. 5652 * 5653 * arc_read_done() will invoke all the requested "done" functions 5654 * for readers of this block. 5655 */ 5656 int 5657 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, 5658 arc_read_done_func_t *done, void *private, zio_priority_t priority, 5659 int zio_flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb) 5660 { 5661 arc_buf_hdr_t *hdr = NULL; 5662 kmutex_t *hash_lock = NULL; 5663 zio_t *rzio; 5664 uint64_t guid = spa_load_guid(spa); 5665 boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW_COMPRESS) != 0; 5666 boolean_t encrypted_read = BP_IS_ENCRYPTED(bp) && 5667 (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; 5668 boolean_t noauth_read = BP_IS_AUTHENTICATED(bp) && 5669 (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0; 5670 boolean_t embedded_bp = !!BP_IS_EMBEDDED(bp); 5671 boolean_t no_buf = *arc_flags & ARC_FLAG_NO_BUF; 5672 arc_buf_t *buf = NULL; 5673 int rc = 0; 5674 5675 ASSERT(!embedded_bp || 5676 BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 5677 ASSERT(!BP_IS_HOLE(bp)); 5678 ASSERT(!BP_IS_REDACTED(bp)); 5679 5680 /* 5681 * Normally SPL_FSTRANS will already be set since kernel threads which 5682 * expect to call the DMU interfaces will set it when created. System 5683 * calls are similarly handled by setting/cleaning the bit in the 5684 * registered callback (module/os/.../zfs/zpl_*). 5685 * 5686 * External consumers such as Lustre which call the exported DMU 5687 * interfaces may not have set SPL_FSTRANS. To avoid a deadlock 5688 * on the hash_lock always set and clear the bit. 5689 */ 5690 fstrans_cookie_t cookie = spl_fstrans_mark(); 5691 top: 5692 /* 5693 * Verify the block pointer contents are reasonable. This should 5694 * always be the case since the blkptr is protected by a checksum. 5695 * However, if there is damage it's desirable to detect this early 5696 * and treat it as a checksum error. This allows an alternate blkptr 5697 * to be tried when one is available (e.g. ditto blocks). 5698 */ 5699 if (!zfs_blkptr_verify(spa, bp, (zio_flags & ZIO_FLAG_CONFIG_WRITER) ? 5700 BLK_CONFIG_HELD : BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) { 5701 rc = SET_ERROR(ECKSUM); 5702 goto done; 5703 } 5704 5705 if (!embedded_bp) { 5706 /* 5707 * Embedded BP's have no DVA and require no I/O to "read". 5708 * Create an anonymous arc buf to back it. 5709 */ 5710 hdr = buf_hash_find(guid, bp, &hash_lock); 5711 } 5712 5713 /* 5714 * Determine if we have an L1 cache hit or a cache miss. For simplicity 5715 * we maintain encrypted data separately from compressed / uncompressed 5716 * data. If the user is requesting raw encrypted data and we don't have 5717 * that in the header we will read from disk to guarantee that we can 5718 * get it even if the encryption keys aren't loaded. 5719 */ 5720 if (hdr != NULL && HDR_HAS_L1HDR(hdr) && (HDR_HAS_RABD(hdr) || 5721 (hdr->b_l1hdr.b_pabd != NULL && !encrypted_read))) { 5722 boolean_t is_data = !HDR_ISTYPE_METADATA(hdr); 5723 5724 if (HDR_IO_IN_PROGRESS(hdr)) { 5725 if (*arc_flags & ARC_FLAG_CACHED_ONLY) { 5726 mutex_exit(hash_lock); 5727 ARCSTAT_BUMP(arcstat_cached_only_in_progress); 5728 rc = SET_ERROR(ENOENT); 5729 goto done; 5730 } 5731 5732 zio_t *head_zio = hdr->b_l1hdr.b_acb->acb_zio_head; 5733 ASSERT3P(head_zio, !=, NULL); 5734 if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) && 5735 priority == ZIO_PRIORITY_SYNC_READ) { 5736 /* 5737 * This is a sync read that needs to wait for 5738 * an in-flight async read. Request that the 5739 * zio have its priority upgraded. 5740 */ 5741 zio_change_priority(head_zio, priority); 5742 DTRACE_PROBE1(arc__async__upgrade__sync, 5743 arc_buf_hdr_t *, hdr); 5744 ARCSTAT_BUMP(arcstat_async_upgrade_sync); 5745 } 5746 5747 DTRACE_PROBE1(arc__iohit, arc_buf_hdr_t *, hdr); 5748 arc_access(hdr, *arc_flags, B_FALSE); 5749 5750 /* 5751 * If there are multiple threads reading the same block 5752 * and that block is not yet in the ARC, then only one 5753 * thread will do the physical I/O and all other 5754 * threads will wait until that I/O completes. 5755 * Synchronous reads use the acb_wait_cv whereas nowait 5756 * reads register a callback. Both are signalled/called 5757 * in arc_read_done. 5758 * 5759 * Errors of the physical I/O may need to be propagated. 5760 * Synchronous read errors are returned here from 5761 * arc_read_done via acb_wait_error. Nowait reads 5762 * attach the acb_zio_dummy zio to pio and 5763 * arc_read_done propagates the physical I/O's io_error 5764 * to acb_zio_dummy, and thereby to pio. 5765 */ 5766 arc_callback_t *acb = NULL; 5767 if (done || pio || *arc_flags & ARC_FLAG_WAIT) { 5768 acb = kmem_zalloc(sizeof (arc_callback_t), 5769 KM_SLEEP); 5770 acb->acb_done = done; 5771 acb->acb_private = private; 5772 acb->acb_compressed = compressed_read; 5773 acb->acb_encrypted = encrypted_read; 5774 acb->acb_noauth = noauth_read; 5775 acb->acb_nobuf = no_buf; 5776 if (*arc_flags & ARC_FLAG_WAIT) { 5777 acb->acb_wait = B_TRUE; 5778 mutex_init(&acb->acb_wait_lock, NULL, 5779 MUTEX_DEFAULT, NULL); 5780 cv_init(&acb->acb_wait_cv, NULL, 5781 CV_DEFAULT, NULL); 5782 } 5783 acb->acb_zb = *zb; 5784 if (pio != NULL) { 5785 acb->acb_zio_dummy = zio_null(pio, 5786 spa, NULL, NULL, NULL, zio_flags); 5787 } 5788 acb->acb_zio_head = head_zio; 5789 acb->acb_next = hdr->b_l1hdr.b_acb; 5790 if (hdr->b_l1hdr.b_acb) 5791 hdr->b_l1hdr.b_acb->acb_prev = acb; 5792 hdr->b_l1hdr.b_acb = acb; 5793 } 5794 mutex_exit(hash_lock); 5795 5796 ARCSTAT_BUMP(arcstat_iohits); 5797 ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH), 5798 demand, prefetch, is_data, data, metadata, iohits); 5799 5800 if (*arc_flags & ARC_FLAG_WAIT) { 5801 mutex_enter(&acb->acb_wait_lock); 5802 while (acb->acb_wait) { 5803 cv_wait(&acb->acb_wait_cv, 5804 &acb->acb_wait_lock); 5805 } 5806 rc = acb->acb_wait_error; 5807 mutex_exit(&acb->acb_wait_lock); 5808 mutex_destroy(&acb->acb_wait_lock); 5809 cv_destroy(&acb->acb_wait_cv); 5810 kmem_free(acb, sizeof (arc_callback_t)); 5811 } 5812 goto out; 5813 } 5814 5815 ASSERT(hdr->b_l1hdr.b_state == arc_mru || 5816 hdr->b_l1hdr.b_state == arc_mfu || 5817 hdr->b_l1hdr.b_state == arc_uncached); 5818 5819 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr); 5820 arc_access(hdr, *arc_flags, B_TRUE); 5821 5822 if (done && !no_buf) { 5823 ASSERT(!embedded_bp || !BP_IS_HOLE(bp)); 5824 5825 /* Get a buf with the desired data in it. */ 5826 rc = arc_buf_alloc_impl(hdr, spa, zb, private, 5827 encrypted_read, compressed_read, noauth_read, 5828 B_TRUE, &buf); 5829 if (rc == ECKSUM) { 5830 /* 5831 * Convert authentication and decryption errors 5832 * to EIO (and generate an ereport if needed) 5833 * before leaving the ARC. 5834 */ 5835 rc = SET_ERROR(EIO); 5836 if ((zio_flags & ZIO_FLAG_SPECULATIVE) == 0) { 5837 spa_log_error(spa, zb, &hdr->b_birth); 5838 (void) zfs_ereport_post( 5839 FM_EREPORT_ZFS_AUTHENTICATION, 5840 spa, NULL, zb, NULL, 0); 5841 } 5842 } 5843 if (rc != 0) { 5844 arc_buf_destroy_impl(buf); 5845 buf = NULL; 5846 (void) remove_reference(hdr, private); 5847 } 5848 5849 /* assert any errors weren't due to unloaded keys */ 5850 ASSERT((zio_flags & ZIO_FLAG_SPECULATIVE) || 5851 rc != EACCES); 5852 } 5853 mutex_exit(hash_lock); 5854 ARCSTAT_BUMP(arcstat_hits); 5855 ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH), 5856 demand, prefetch, is_data, data, metadata, hits); 5857 *arc_flags |= ARC_FLAG_CACHED; 5858 goto done; 5859 } else { 5860 uint64_t lsize = BP_GET_LSIZE(bp); 5861 uint64_t psize = BP_GET_PSIZE(bp); 5862 arc_callback_t *acb; 5863 vdev_t *vd = NULL; 5864 uint64_t addr = 0; 5865 boolean_t devw = B_FALSE; 5866 uint64_t size; 5867 abd_t *hdr_abd; 5868 int alloc_flags = encrypted_read ? ARC_HDR_ALLOC_RDATA : 0; 5869 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp); 5870 5871 if (*arc_flags & ARC_FLAG_CACHED_ONLY) { 5872 if (hash_lock != NULL) 5873 mutex_exit(hash_lock); 5874 rc = SET_ERROR(ENOENT); 5875 goto done; 5876 } 5877 5878 if (hdr == NULL) { 5879 /* 5880 * This block is not in the cache or it has 5881 * embedded data. 5882 */ 5883 arc_buf_hdr_t *exists = NULL; 5884 hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, 5885 BP_IS_PROTECTED(bp), BP_GET_COMPRESS(bp), 0, type); 5886 5887 if (!embedded_bp) { 5888 hdr->b_dva = *BP_IDENTITY(bp); 5889 hdr->b_birth = BP_PHYSICAL_BIRTH(bp); 5890 exists = buf_hash_insert(hdr, &hash_lock); 5891 } 5892 if (exists != NULL) { 5893 /* somebody beat us to the hash insert */ 5894 mutex_exit(hash_lock); 5895 buf_discard_identity(hdr); 5896 arc_hdr_destroy(hdr); 5897 goto top; /* restart the IO request */ 5898 } 5899 } else { 5900 /* 5901 * This block is in the ghost cache or encrypted data 5902 * was requested and we didn't have it. If it was 5903 * L2-only (and thus didn't have an L1 hdr), 5904 * we realloc the header to add an L1 hdr. 5905 */ 5906 if (!HDR_HAS_L1HDR(hdr)) { 5907 hdr = arc_hdr_realloc(hdr, hdr_l2only_cache, 5908 hdr_full_cache); 5909 } 5910 5911 if (GHOST_STATE(hdr->b_l1hdr.b_state)) { 5912 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 5913 ASSERT(!HDR_HAS_RABD(hdr)); 5914 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 5915 ASSERT0(zfs_refcount_count( 5916 &hdr->b_l1hdr.b_refcnt)); 5917 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL); 5918 #ifdef ZFS_DEBUG 5919 ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL); 5920 #endif 5921 } else if (HDR_IO_IN_PROGRESS(hdr)) { 5922 /* 5923 * If this header already had an IO in progress 5924 * and we are performing another IO to fetch 5925 * encrypted data we must wait until the first 5926 * IO completes so as not to confuse 5927 * arc_read_done(). This should be very rare 5928 * and so the performance impact shouldn't 5929 * matter. 5930 */ 5931 cv_wait(&hdr->b_l1hdr.b_cv, hash_lock); 5932 mutex_exit(hash_lock); 5933 goto top; 5934 } 5935 } 5936 if (*arc_flags & ARC_FLAG_UNCACHED) { 5937 arc_hdr_set_flags(hdr, ARC_FLAG_UNCACHED); 5938 if (!encrypted_read) 5939 alloc_flags |= ARC_HDR_ALLOC_LINEAR; 5940 } 5941 5942 /* 5943 * Take additional reference for IO_IN_PROGRESS. It stops 5944 * arc_access() from putting this header without any buffers 5945 * and so other references but obviously nonevictable onto 5946 * the evictable list of MRU or MFU state. 5947 */ 5948 add_reference(hdr, hdr); 5949 if (!embedded_bp) 5950 arc_access(hdr, *arc_flags, B_FALSE); 5951 arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 5952 arc_hdr_alloc_abd(hdr, alloc_flags); 5953 if (encrypted_read) { 5954 ASSERT(HDR_HAS_RABD(hdr)); 5955 size = HDR_GET_PSIZE(hdr); 5956 hdr_abd = hdr->b_crypt_hdr.b_rabd; 5957 zio_flags |= ZIO_FLAG_RAW; 5958 } else { 5959 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 5960 size = arc_hdr_size(hdr); 5961 hdr_abd = hdr->b_l1hdr.b_pabd; 5962 5963 if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) { 5964 zio_flags |= ZIO_FLAG_RAW_COMPRESS; 5965 } 5966 5967 /* 5968 * For authenticated bp's, we do not ask the ZIO layer 5969 * to authenticate them since this will cause the entire 5970 * IO to fail if the key isn't loaded. Instead, we 5971 * defer authentication until arc_buf_fill(), which will 5972 * verify the data when the key is available. 5973 */ 5974 if (BP_IS_AUTHENTICATED(bp)) 5975 zio_flags |= ZIO_FLAG_RAW_ENCRYPT; 5976 } 5977 5978 if (BP_IS_AUTHENTICATED(bp)) 5979 arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); 5980 if (BP_GET_LEVEL(bp) > 0) 5981 arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT); 5982 ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state)); 5983 5984 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP); 5985 acb->acb_done = done; 5986 acb->acb_private = private; 5987 acb->acb_compressed = compressed_read; 5988 acb->acb_encrypted = encrypted_read; 5989 acb->acb_noauth = noauth_read; 5990 acb->acb_zb = *zb; 5991 5992 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 5993 hdr->b_l1hdr.b_acb = acb; 5994 5995 if (HDR_HAS_L2HDR(hdr) && 5996 (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) { 5997 devw = hdr->b_l2hdr.b_dev->l2ad_writing; 5998 addr = hdr->b_l2hdr.b_daddr; 5999 /* 6000 * Lock out L2ARC device removal. 6001 */ 6002 if (vdev_is_dead(vd) || 6003 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER)) 6004 vd = NULL; 6005 } 6006 6007 /* 6008 * We count both async reads and scrub IOs as asynchronous so 6009 * that both can be upgraded in the event of a cache hit while 6010 * the read IO is still in-flight. 6011 */ 6012 if (priority == ZIO_PRIORITY_ASYNC_READ || 6013 priority == ZIO_PRIORITY_SCRUB) 6014 arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 6015 else 6016 arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ); 6017 6018 /* 6019 * At this point, we have a level 1 cache miss or a blkptr 6020 * with embedded data. Try again in L2ARC if possible. 6021 */ 6022 ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize); 6023 6024 /* 6025 * Skip ARC stat bump for block pointers with embedded 6026 * data. The data are read from the blkptr itself via 6027 * decode_embedded_bp_compressed(). 6028 */ 6029 if (!embedded_bp) { 6030 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, 6031 blkptr_t *, bp, uint64_t, lsize, 6032 zbookmark_phys_t *, zb); 6033 ARCSTAT_BUMP(arcstat_misses); 6034 ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH), 6035 demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data, 6036 metadata, misses); 6037 zfs_racct_read(size, 1); 6038 } 6039 6040 /* Check if the spa even has l2 configured */ 6041 const boolean_t spa_has_l2 = l2arc_ndev != 0 && 6042 spa->spa_l2cache.sav_count > 0; 6043 6044 if (vd != NULL && spa_has_l2 && !(l2arc_norw && devw)) { 6045 /* 6046 * Read from the L2ARC if the following are true: 6047 * 1. The L2ARC vdev was previously cached. 6048 * 2. This buffer still has L2ARC metadata. 6049 * 3. This buffer isn't currently writing to the L2ARC. 6050 * 4. The L2ARC entry wasn't evicted, which may 6051 * also have invalidated the vdev. 6052 * 5. This isn't prefetch or l2arc_noprefetch is 0. 6053 */ 6054 if (HDR_HAS_L2HDR(hdr) && 6055 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) && 6056 !(l2arc_noprefetch && 6057 (*arc_flags & ARC_FLAG_PREFETCH))) { 6058 l2arc_read_callback_t *cb; 6059 abd_t *abd; 6060 uint64_t asize; 6061 6062 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr); 6063 ARCSTAT_BUMP(arcstat_l2_hits); 6064 hdr->b_l2hdr.b_hits++; 6065 6066 cb = kmem_zalloc(sizeof (l2arc_read_callback_t), 6067 KM_SLEEP); 6068 cb->l2rcb_hdr = hdr; 6069 cb->l2rcb_bp = *bp; 6070 cb->l2rcb_zb = *zb; 6071 cb->l2rcb_flags = zio_flags; 6072 6073 /* 6074 * When Compressed ARC is disabled, but the 6075 * L2ARC block is compressed, arc_hdr_size() 6076 * will have returned LSIZE rather than PSIZE. 6077 */ 6078 if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 6079 !HDR_COMPRESSION_ENABLED(hdr) && 6080 HDR_GET_PSIZE(hdr) != 0) { 6081 size = HDR_GET_PSIZE(hdr); 6082 } 6083 6084 asize = vdev_psize_to_asize(vd, size); 6085 if (asize != size) { 6086 abd = abd_alloc_for_io(asize, 6087 HDR_ISTYPE_METADATA(hdr)); 6088 cb->l2rcb_abd = abd; 6089 } else { 6090 abd = hdr_abd; 6091 } 6092 6093 ASSERT(addr >= VDEV_LABEL_START_SIZE && 6094 addr + asize <= vd->vdev_psize - 6095 VDEV_LABEL_END_SIZE); 6096 6097 /* 6098 * l2arc read. The SCL_L2ARC lock will be 6099 * released by l2arc_read_done(). 6100 * Issue a null zio if the underlying buffer 6101 * was squashed to zero size by compression. 6102 */ 6103 ASSERT3U(arc_hdr_get_compress(hdr), !=, 6104 ZIO_COMPRESS_EMPTY); 6105 rzio = zio_read_phys(pio, vd, addr, 6106 asize, abd, 6107 ZIO_CHECKSUM_OFF, 6108 l2arc_read_done, cb, priority, 6109 zio_flags | ZIO_FLAG_CANFAIL | 6110 ZIO_FLAG_DONT_PROPAGATE | 6111 ZIO_FLAG_DONT_RETRY, B_FALSE); 6112 acb->acb_zio_head = rzio; 6113 6114 if (hash_lock != NULL) 6115 mutex_exit(hash_lock); 6116 6117 DTRACE_PROBE2(l2arc__read, vdev_t *, vd, 6118 zio_t *, rzio); 6119 ARCSTAT_INCR(arcstat_l2_read_bytes, 6120 HDR_GET_PSIZE(hdr)); 6121 6122 if (*arc_flags & ARC_FLAG_NOWAIT) { 6123 zio_nowait(rzio); 6124 goto out; 6125 } 6126 6127 ASSERT(*arc_flags & ARC_FLAG_WAIT); 6128 if (zio_wait(rzio) == 0) 6129 goto out; 6130 6131 /* l2arc read error; goto zio_read() */ 6132 if (hash_lock != NULL) 6133 mutex_enter(hash_lock); 6134 } else { 6135 DTRACE_PROBE1(l2arc__miss, 6136 arc_buf_hdr_t *, hdr); 6137 ARCSTAT_BUMP(arcstat_l2_misses); 6138 if (HDR_L2_WRITING(hdr)) 6139 ARCSTAT_BUMP(arcstat_l2_rw_clash); 6140 spa_config_exit(spa, SCL_L2ARC, vd); 6141 } 6142 } else { 6143 if (vd != NULL) 6144 spa_config_exit(spa, SCL_L2ARC, vd); 6145 6146 /* 6147 * Only a spa with l2 should contribute to l2 6148 * miss stats. (Including the case of having a 6149 * faulted cache device - that's also a miss.) 6150 */ 6151 if (spa_has_l2) { 6152 /* 6153 * Skip ARC stat bump for block pointers with 6154 * embedded data. The data are read from the 6155 * blkptr itself via 6156 * decode_embedded_bp_compressed(). 6157 */ 6158 if (!embedded_bp) { 6159 DTRACE_PROBE1(l2arc__miss, 6160 arc_buf_hdr_t *, hdr); 6161 ARCSTAT_BUMP(arcstat_l2_misses); 6162 } 6163 } 6164 } 6165 6166 rzio = zio_read(pio, spa, bp, hdr_abd, size, 6167 arc_read_done, hdr, priority, zio_flags, zb); 6168 acb->acb_zio_head = rzio; 6169 6170 if (hash_lock != NULL) 6171 mutex_exit(hash_lock); 6172 6173 if (*arc_flags & ARC_FLAG_WAIT) { 6174 rc = zio_wait(rzio); 6175 goto out; 6176 } 6177 6178 ASSERT(*arc_flags & ARC_FLAG_NOWAIT); 6179 zio_nowait(rzio); 6180 } 6181 6182 out: 6183 /* embedded bps don't actually go to disk */ 6184 if (!embedded_bp) 6185 spa_read_history_add(spa, zb, *arc_flags); 6186 spl_fstrans_unmark(cookie); 6187 return (rc); 6188 6189 done: 6190 if (done) 6191 done(NULL, zb, bp, buf, private); 6192 if (pio && rc != 0) { 6193 zio_t *zio = zio_null(pio, spa, NULL, NULL, NULL, zio_flags); 6194 zio->io_error = rc; 6195 zio_nowait(zio); 6196 } 6197 goto out; 6198 } 6199 6200 arc_prune_t * 6201 arc_add_prune_callback(arc_prune_func_t *func, void *private) 6202 { 6203 arc_prune_t *p; 6204 6205 p = kmem_alloc(sizeof (*p), KM_SLEEP); 6206 p->p_pfunc = func; 6207 p->p_private = private; 6208 list_link_init(&p->p_node); 6209 zfs_refcount_create(&p->p_refcnt); 6210 6211 mutex_enter(&arc_prune_mtx); 6212 zfs_refcount_add(&p->p_refcnt, &arc_prune_list); 6213 list_insert_head(&arc_prune_list, p); 6214 mutex_exit(&arc_prune_mtx); 6215 6216 return (p); 6217 } 6218 6219 void 6220 arc_remove_prune_callback(arc_prune_t *p) 6221 { 6222 boolean_t wait = B_FALSE; 6223 mutex_enter(&arc_prune_mtx); 6224 list_remove(&arc_prune_list, p); 6225 if (zfs_refcount_remove(&p->p_refcnt, &arc_prune_list) > 0) 6226 wait = B_TRUE; 6227 mutex_exit(&arc_prune_mtx); 6228 6229 /* wait for arc_prune_task to finish */ 6230 if (wait) 6231 taskq_wait_outstanding(arc_prune_taskq, 0); 6232 ASSERT0(zfs_refcount_count(&p->p_refcnt)); 6233 zfs_refcount_destroy(&p->p_refcnt); 6234 kmem_free(p, sizeof (*p)); 6235 } 6236 6237 /* 6238 * Notify the arc that a block was freed, and thus will never be used again. 6239 */ 6240 void 6241 arc_freed(spa_t *spa, const blkptr_t *bp) 6242 { 6243 arc_buf_hdr_t *hdr; 6244 kmutex_t *hash_lock; 6245 uint64_t guid = spa_load_guid(spa); 6246 6247 ASSERT(!BP_IS_EMBEDDED(bp)); 6248 6249 hdr = buf_hash_find(guid, bp, &hash_lock); 6250 if (hdr == NULL) 6251 return; 6252 6253 /* 6254 * We might be trying to free a block that is still doing I/O 6255 * (i.e. prefetch) or has some other reference (i.e. a dedup-ed, 6256 * dmu_sync-ed block). A block may also have a reference if it is 6257 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would 6258 * have written the new block to its final resting place on disk but 6259 * without the dedup flag set. This would have left the hdr in the MRU 6260 * state and discoverable. When the txg finally syncs it detects that 6261 * the block was overridden in open context and issues an override I/O. 6262 * Since this is a dedup block, the override I/O will determine if the 6263 * block is already in the DDT. If so, then it will replace the io_bp 6264 * with the bp from the DDT and allow the I/O to finish. When the I/O 6265 * reaches the done callback, dbuf_write_override_done, it will 6266 * check to see if the io_bp and io_bp_override are identical. 6267 * If they are not, then it indicates that the bp was replaced with 6268 * the bp in the DDT and the override bp is freed. This allows 6269 * us to arrive here with a reference on a block that is being 6270 * freed. So if we have an I/O in progress, or a reference to 6271 * this hdr, then we don't destroy the hdr. 6272 */ 6273 if (!HDR_HAS_L1HDR(hdr) || 6274 zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 6275 arc_change_state(arc_anon, hdr); 6276 arc_hdr_destroy(hdr); 6277 mutex_exit(hash_lock); 6278 } else { 6279 mutex_exit(hash_lock); 6280 } 6281 6282 } 6283 6284 /* 6285 * Release this buffer from the cache, making it an anonymous buffer. This 6286 * must be done after a read and prior to modifying the buffer contents. 6287 * If the buffer has more than one reference, we must make 6288 * a new hdr for the buffer. 6289 */ 6290 void 6291 arc_release(arc_buf_t *buf, const void *tag) 6292 { 6293 arc_buf_hdr_t *hdr = buf->b_hdr; 6294 6295 /* 6296 * It would be nice to assert that if its DMU metadata (level > 6297 * 0 || it's the dnode file), then it must be syncing context. 6298 * But we don't know that information at this level. 6299 */ 6300 6301 ASSERT(HDR_HAS_L1HDR(hdr)); 6302 6303 /* 6304 * We don't grab the hash lock prior to this check, because if 6305 * the buffer's header is in the arc_anon state, it won't be 6306 * linked into the hash table. 6307 */ 6308 if (hdr->b_l1hdr.b_state == arc_anon) { 6309 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 6310 ASSERT(!HDR_IN_HASH_TABLE(hdr)); 6311 ASSERT(!HDR_HAS_L2HDR(hdr)); 6312 6313 ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 6314 ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1); 6315 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 6316 6317 hdr->b_l1hdr.b_arc_access = 0; 6318 6319 /* 6320 * If the buf is being overridden then it may already 6321 * have a hdr that is not empty. 6322 */ 6323 buf_discard_identity(hdr); 6324 arc_buf_thaw(buf); 6325 6326 return; 6327 } 6328 6329 kmutex_t *hash_lock = HDR_LOCK(hdr); 6330 mutex_enter(hash_lock); 6331 6332 /* 6333 * This assignment is only valid as long as the hash_lock is 6334 * held, we must be careful not to reference state or the 6335 * b_state field after dropping the lock. 6336 */ 6337 arc_state_t *state = hdr->b_l1hdr.b_state; 6338 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 6339 ASSERT3P(state, !=, arc_anon); 6340 6341 /* this buffer is not on any list */ 6342 ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0); 6343 6344 if (HDR_HAS_L2HDR(hdr)) { 6345 mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx); 6346 6347 /* 6348 * We have to recheck this conditional again now that 6349 * we're holding the l2ad_mtx to prevent a race with 6350 * another thread which might be concurrently calling 6351 * l2arc_evict(). In that case, l2arc_evict() might have 6352 * destroyed the header's L2 portion as we were waiting 6353 * to acquire the l2ad_mtx. 6354 */ 6355 if (HDR_HAS_L2HDR(hdr)) 6356 arc_hdr_l2hdr_destroy(hdr); 6357 6358 mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx); 6359 } 6360 6361 /* 6362 * Do we have more than one buf? 6363 */ 6364 if (hdr->b_l1hdr.b_bufcnt > 1) { 6365 arc_buf_hdr_t *nhdr; 6366 uint64_t spa = hdr->b_spa; 6367 uint64_t psize = HDR_GET_PSIZE(hdr); 6368 uint64_t lsize = HDR_GET_LSIZE(hdr); 6369 boolean_t protected = HDR_PROTECTED(hdr); 6370 enum zio_compress compress = arc_hdr_get_compress(hdr); 6371 arc_buf_contents_t type = arc_buf_type(hdr); 6372 VERIFY3U(hdr->b_type, ==, type); 6373 6374 ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL); 6375 VERIFY3S(remove_reference(hdr, tag), >, 0); 6376 6377 if (arc_buf_is_shared(buf) && !ARC_BUF_COMPRESSED(buf)) { 6378 ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); 6379 ASSERT(ARC_BUF_LAST(buf)); 6380 } 6381 6382 /* 6383 * Pull the data off of this hdr and attach it to 6384 * a new anonymous hdr. Also find the last buffer 6385 * in the hdr's buffer list. 6386 */ 6387 arc_buf_t *lastbuf = arc_buf_remove(hdr, buf); 6388 ASSERT3P(lastbuf, !=, NULL); 6389 6390 /* 6391 * If the current arc_buf_t and the hdr are sharing their data 6392 * buffer, then we must stop sharing that block. 6393 */ 6394 if (arc_buf_is_shared(buf)) { 6395 ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf); 6396 VERIFY(!arc_buf_is_shared(lastbuf)); 6397 6398 /* 6399 * First, sever the block sharing relationship between 6400 * buf and the arc_buf_hdr_t. 6401 */ 6402 arc_unshare_buf(hdr, buf); 6403 6404 /* 6405 * Now we need to recreate the hdr's b_pabd. Since we 6406 * have lastbuf handy, we try to share with it, but if 6407 * we can't then we allocate a new b_pabd and copy the 6408 * data from buf into it. 6409 */ 6410 if (arc_can_share(hdr, lastbuf)) { 6411 arc_share_buf(hdr, lastbuf); 6412 } else { 6413 arc_hdr_alloc_abd(hdr, 0); 6414 abd_copy_from_buf(hdr->b_l1hdr.b_pabd, 6415 buf->b_data, psize); 6416 } 6417 VERIFY3P(lastbuf->b_data, !=, NULL); 6418 } else if (HDR_SHARED_DATA(hdr)) { 6419 /* 6420 * Uncompressed shared buffers are always at the end 6421 * of the list. Compressed buffers don't have the 6422 * same requirements. This makes it hard to 6423 * simply assert that the lastbuf is shared so 6424 * we rely on the hdr's compression flags to determine 6425 * if we have a compressed, shared buffer. 6426 */ 6427 ASSERT(arc_buf_is_shared(lastbuf) || 6428 arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF); 6429 ASSERT(!ARC_BUF_SHARED(buf)); 6430 } 6431 6432 ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr)); 6433 ASSERT3P(state, !=, arc_l2c_only); 6434 6435 (void) zfs_refcount_remove_many(&state->arcs_size[type], 6436 arc_buf_size(buf), buf); 6437 6438 if (zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) { 6439 ASSERT3P(state, !=, arc_l2c_only); 6440 (void) zfs_refcount_remove_many( 6441 &state->arcs_esize[type], 6442 arc_buf_size(buf), buf); 6443 } 6444 6445 hdr->b_l1hdr.b_bufcnt -= 1; 6446 if (ARC_BUF_ENCRYPTED(buf)) 6447 hdr->b_crypt_hdr.b_ebufcnt -= 1; 6448 6449 arc_cksum_verify(buf); 6450 arc_buf_unwatch(buf); 6451 6452 /* if this is the last uncompressed buf free the checksum */ 6453 if (!arc_hdr_has_uncompressed_buf(hdr)) 6454 arc_cksum_free(hdr); 6455 6456 mutex_exit(hash_lock); 6457 6458 nhdr = arc_hdr_alloc(spa, psize, lsize, protected, 6459 compress, hdr->b_complevel, type); 6460 ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL); 6461 ASSERT0(nhdr->b_l1hdr.b_bufcnt); 6462 ASSERT0(zfs_refcount_count(&nhdr->b_l1hdr.b_refcnt)); 6463 VERIFY3U(nhdr->b_type, ==, type); 6464 ASSERT(!HDR_SHARED_DATA(nhdr)); 6465 6466 nhdr->b_l1hdr.b_buf = buf; 6467 nhdr->b_l1hdr.b_bufcnt = 1; 6468 if (ARC_BUF_ENCRYPTED(buf)) 6469 nhdr->b_crypt_hdr.b_ebufcnt = 1; 6470 (void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, tag); 6471 buf->b_hdr = nhdr; 6472 6473 (void) zfs_refcount_add_many(&arc_anon->arcs_size[type], 6474 arc_buf_size(buf), buf); 6475 } else { 6476 ASSERT(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 1); 6477 /* protected by hash lock, or hdr is on arc_anon */ 6478 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node)); 6479 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 6480 hdr->b_l1hdr.b_mru_hits = 0; 6481 hdr->b_l1hdr.b_mru_ghost_hits = 0; 6482 hdr->b_l1hdr.b_mfu_hits = 0; 6483 hdr->b_l1hdr.b_mfu_ghost_hits = 0; 6484 arc_change_state(arc_anon, hdr); 6485 hdr->b_l1hdr.b_arc_access = 0; 6486 6487 mutex_exit(hash_lock); 6488 buf_discard_identity(hdr); 6489 arc_buf_thaw(buf); 6490 } 6491 } 6492 6493 int 6494 arc_released(arc_buf_t *buf) 6495 { 6496 return (buf->b_data != NULL && 6497 buf->b_hdr->b_l1hdr.b_state == arc_anon); 6498 } 6499 6500 #ifdef ZFS_DEBUG 6501 int 6502 arc_referenced(arc_buf_t *buf) 6503 { 6504 return (zfs_refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt)); 6505 } 6506 #endif 6507 6508 static void 6509 arc_write_ready(zio_t *zio) 6510 { 6511 arc_write_callback_t *callback = zio->io_private; 6512 arc_buf_t *buf = callback->awcb_buf; 6513 arc_buf_hdr_t *hdr = buf->b_hdr; 6514 blkptr_t *bp = zio->io_bp; 6515 uint64_t psize = BP_IS_HOLE(bp) ? 0 : BP_GET_PSIZE(bp); 6516 fstrans_cookie_t cookie = spl_fstrans_mark(); 6517 6518 ASSERT(HDR_HAS_L1HDR(hdr)); 6519 ASSERT(!zfs_refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt)); 6520 ASSERT(hdr->b_l1hdr.b_bufcnt > 0); 6521 6522 /* 6523 * If we're reexecuting this zio because the pool suspended, then 6524 * cleanup any state that was previously set the first time the 6525 * callback was invoked. 6526 */ 6527 if (zio->io_flags & ZIO_FLAG_REEXECUTED) { 6528 arc_cksum_free(hdr); 6529 arc_buf_unwatch(buf); 6530 if (hdr->b_l1hdr.b_pabd != NULL) { 6531 if (arc_buf_is_shared(buf)) { 6532 arc_unshare_buf(hdr, buf); 6533 } else { 6534 arc_hdr_free_abd(hdr, B_FALSE); 6535 } 6536 } 6537 6538 if (HDR_HAS_RABD(hdr)) 6539 arc_hdr_free_abd(hdr, B_TRUE); 6540 } 6541 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 6542 ASSERT(!HDR_HAS_RABD(hdr)); 6543 ASSERT(!HDR_SHARED_DATA(hdr)); 6544 ASSERT(!arc_buf_is_shared(buf)); 6545 6546 callback->awcb_ready(zio, buf, callback->awcb_private); 6547 6548 if (HDR_IO_IN_PROGRESS(hdr)) { 6549 ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED); 6550 } else { 6551 arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 6552 add_reference(hdr, hdr); /* For IO_IN_PROGRESS. */ 6553 } 6554 6555 if (BP_IS_PROTECTED(bp) != !!HDR_PROTECTED(hdr)) 6556 hdr = arc_hdr_realloc_crypt(hdr, BP_IS_PROTECTED(bp)); 6557 6558 if (BP_IS_PROTECTED(bp)) { 6559 /* ZIL blocks are written through zio_rewrite */ 6560 ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); 6561 ASSERT(HDR_PROTECTED(hdr)); 6562 6563 if (BP_SHOULD_BYTESWAP(bp)) { 6564 if (BP_GET_LEVEL(bp) > 0) { 6565 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64; 6566 } else { 6567 hdr->b_l1hdr.b_byteswap = 6568 DMU_OT_BYTESWAP(BP_GET_TYPE(bp)); 6569 } 6570 } else { 6571 hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS; 6572 } 6573 6574 hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp); 6575 hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset; 6576 zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt, 6577 hdr->b_crypt_hdr.b_iv); 6578 zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac); 6579 } 6580 6581 /* 6582 * If this block was written for raw encryption but the zio layer 6583 * ended up only authenticating it, adjust the buffer flags now. 6584 */ 6585 if (BP_IS_AUTHENTICATED(bp) && ARC_BUF_ENCRYPTED(buf)) { 6586 arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH); 6587 buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; 6588 if (BP_GET_COMPRESS(bp) == ZIO_COMPRESS_OFF) 6589 buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; 6590 } else if (BP_IS_HOLE(bp) && ARC_BUF_ENCRYPTED(buf)) { 6591 buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED; 6592 buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED; 6593 } 6594 6595 /* this must be done after the buffer flags are adjusted */ 6596 arc_cksum_compute(buf); 6597 6598 enum zio_compress compress; 6599 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) { 6600 compress = ZIO_COMPRESS_OFF; 6601 } else { 6602 ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp)); 6603 compress = BP_GET_COMPRESS(bp); 6604 } 6605 HDR_SET_PSIZE(hdr, psize); 6606 arc_hdr_set_compress(hdr, compress); 6607 hdr->b_complevel = zio->io_prop.zp_complevel; 6608 6609 if (zio->io_error != 0 || psize == 0) 6610 goto out; 6611 6612 /* 6613 * Fill the hdr with data. If the buffer is encrypted we have no choice 6614 * but to copy the data into b_radb. If the hdr is compressed, the data 6615 * we want is available from the zio, otherwise we can take it from 6616 * the buf. 6617 * 6618 * We might be able to share the buf's data with the hdr here. However, 6619 * doing so would cause the ARC to be full of linear ABDs if we write a 6620 * lot of shareable data. As a compromise, we check whether scattered 6621 * ABDs are allowed, and assume that if they are then the user wants 6622 * the ARC to be primarily filled with them regardless of the data being 6623 * written. Therefore, if they're allowed then we allocate one and copy 6624 * the data into it; otherwise, we share the data directly if we can. 6625 */ 6626 if (ARC_BUF_ENCRYPTED(buf)) { 6627 ASSERT3U(psize, >, 0); 6628 ASSERT(ARC_BUF_COMPRESSED(buf)); 6629 arc_hdr_alloc_abd(hdr, ARC_HDR_ALLOC_RDATA | 6630 ARC_HDR_USE_RESERVE); 6631 abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); 6632 } else if (!(HDR_UNCACHED(hdr) || 6633 abd_size_alloc_linear(arc_buf_size(buf))) || 6634 !arc_can_share(hdr, buf)) { 6635 /* 6636 * Ideally, we would always copy the io_abd into b_pabd, but the 6637 * user may have disabled compressed ARC, thus we must check the 6638 * hdr's compression setting rather than the io_bp's. 6639 */ 6640 if (BP_IS_ENCRYPTED(bp)) { 6641 ASSERT3U(psize, >, 0); 6642 arc_hdr_alloc_abd(hdr, ARC_HDR_ALLOC_RDATA | 6643 ARC_HDR_USE_RESERVE); 6644 abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize); 6645 } else if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF && 6646 !ARC_BUF_COMPRESSED(buf)) { 6647 ASSERT3U(psize, >, 0); 6648 arc_hdr_alloc_abd(hdr, ARC_HDR_USE_RESERVE); 6649 abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize); 6650 } else { 6651 ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr)); 6652 arc_hdr_alloc_abd(hdr, ARC_HDR_USE_RESERVE); 6653 abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data, 6654 arc_buf_size(buf)); 6655 } 6656 } else { 6657 ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd)); 6658 ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf)); 6659 ASSERT3U(hdr->b_l1hdr.b_bufcnt, ==, 1); 6660 6661 arc_share_buf(hdr, buf); 6662 } 6663 6664 out: 6665 arc_hdr_verify(hdr, bp); 6666 spl_fstrans_unmark(cookie); 6667 } 6668 6669 static void 6670 arc_write_children_ready(zio_t *zio) 6671 { 6672 arc_write_callback_t *callback = zio->io_private; 6673 arc_buf_t *buf = callback->awcb_buf; 6674 6675 callback->awcb_children_ready(zio, buf, callback->awcb_private); 6676 } 6677 6678 /* 6679 * The SPA calls this callback for each physical write that happens on behalf 6680 * of a logical write. See the comment in dbuf_write_physdone() for details. 6681 */ 6682 static void 6683 arc_write_physdone(zio_t *zio) 6684 { 6685 arc_write_callback_t *cb = zio->io_private; 6686 if (cb->awcb_physdone != NULL) 6687 cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private); 6688 } 6689 6690 static void 6691 arc_write_done(zio_t *zio) 6692 { 6693 arc_write_callback_t *callback = zio->io_private; 6694 arc_buf_t *buf = callback->awcb_buf; 6695 arc_buf_hdr_t *hdr = buf->b_hdr; 6696 6697 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 6698 6699 if (zio->io_error == 0) { 6700 arc_hdr_verify(hdr, zio->io_bp); 6701 6702 if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) { 6703 buf_discard_identity(hdr); 6704 } else { 6705 hdr->b_dva = *BP_IDENTITY(zio->io_bp); 6706 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp); 6707 } 6708 } else { 6709 ASSERT(HDR_EMPTY(hdr)); 6710 } 6711 6712 /* 6713 * If the block to be written was all-zero or compressed enough to be 6714 * embedded in the BP, no write was performed so there will be no 6715 * dva/birth/checksum. The buffer must therefore remain anonymous 6716 * (and uncached). 6717 */ 6718 if (!HDR_EMPTY(hdr)) { 6719 arc_buf_hdr_t *exists; 6720 kmutex_t *hash_lock; 6721 6722 ASSERT3U(zio->io_error, ==, 0); 6723 6724 arc_cksum_verify(buf); 6725 6726 exists = buf_hash_insert(hdr, &hash_lock); 6727 if (exists != NULL) { 6728 /* 6729 * This can only happen if we overwrite for 6730 * sync-to-convergence, because we remove 6731 * buffers from the hash table when we arc_free(). 6732 */ 6733 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 6734 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 6735 panic("bad overwrite, hdr=%p exists=%p", 6736 (void *)hdr, (void *)exists); 6737 ASSERT(zfs_refcount_is_zero( 6738 &exists->b_l1hdr.b_refcnt)); 6739 arc_change_state(arc_anon, exists); 6740 arc_hdr_destroy(exists); 6741 mutex_exit(hash_lock); 6742 exists = buf_hash_insert(hdr, &hash_lock); 6743 ASSERT3P(exists, ==, NULL); 6744 } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) { 6745 /* nopwrite */ 6746 ASSERT(zio->io_prop.zp_nopwrite); 6747 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp)) 6748 panic("bad nopwrite, hdr=%p exists=%p", 6749 (void *)hdr, (void *)exists); 6750 } else { 6751 /* Dedup */ 6752 ASSERT(hdr->b_l1hdr.b_bufcnt == 1); 6753 ASSERT(hdr->b_l1hdr.b_state == arc_anon); 6754 ASSERT(BP_GET_DEDUP(zio->io_bp)); 6755 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0); 6756 } 6757 } 6758 arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 6759 VERIFY3S(remove_reference(hdr, hdr), >, 0); 6760 /* if it's not anon, we are doing a scrub */ 6761 if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon) 6762 arc_access(hdr, 0, B_FALSE); 6763 mutex_exit(hash_lock); 6764 } else { 6765 arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS); 6766 VERIFY3S(remove_reference(hdr, hdr), >, 0); 6767 } 6768 6769 callback->awcb_done(zio, buf, callback->awcb_private); 6770 6771 abd_free(zio->io_abd); 6772 kmem_free(callback, sizeof (arc_write_callback_t)); 6773 } 6774 6775 zio_t * 6776 arc_write(zio_t *pio, spa_t *spa, uint64_t txg, 6777 blkptr_t *bp, arc_buf_t *buf, boolean_t uncached, boolean_t l2arc, 6778 const zio_prop_t *zp, arc_write_done_func_t *ready, 6779 arc_write_done_func_t *children_ready, arc_write_done_func_t *physdone, 6780 arc_write_done_func_t *done, void *private, zio_priority_t priority, 6781 int zio_flags, const zbookmark_phys_t *zb) 6782 { 6783 arc_buf_hdr_t *hdr = buf->b_hdr; 6784 arc_write_callback_t *callback; 6785 zio_t *zio; 6786 zio_prop_t localprop = *zp; 6787 6788 ASSERT3P(ready, !=, NULL); 6789 ASSERT3P(done, !=, NULL); 6790 ASSERT(!HDR_IO_ERROR(hdr)); 6791 ASSERT(!HDR_IO_IN_PROGRESS(hdr)); 6792 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL); 6793 ASSERT3U(hdr->b_l1hdr.b_bufcnt, >, 0); 6794 if (uncached) 6795 arc_hdr_set_flags(hdr, ARC_FLAG_UNCACHED); 6796 else if (l2arc) 6797 arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE); 6798 6799 if (ARC_BUF_ENCRYPTED(buf)) { 6800 ASSERT(ARC_BUF_COMPRESSED(buf)); 6801 localprop.zp_encrypt = B_TRUE; 6802 localprop.zp_compress = HDR_GET_COMPRESS(hdr); 6803 localprop.zp_complevel = hdr->b_complevel; 6804 localprop.zp_byteorder = 6805 (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ? 6806 ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER; 6807 memcpy(localprop.zp_salt, hdr->b_crypt_hdr.b_salt, 6808 ZIO_DATA_SALT_LEN); 6809 memcpy(localprop.zp_iv, hdr->b_crypt_hdr.b_iv, 6810 ZIO_DATA_IV_LEN); 6811 memcpy(localprop.zp_mac, hdr->b_crypt_hdr.b_mac, 6812 ZIO_DATA_MAC_LEN); 6813 if (DMU_OT_IS_ENCRYPTED(localprop.zp_type)) { 6814 localprop.zp_nopwrite = B_FALSE; 6815 localprop.zp_copies = 6816 MIN(localprop.zp_copies, SPA_DVAS_PER_BP - 1); 6817 } 6818 zio_flags |= ZIO_FLAG_RAW; 6819 } else if (ARC_BUF_COMPRESSED(buf)) { 6820 ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf)); 6821 localprop.zp_compress = HDR_GET_COMPRESS(hdr); 6822 localprop.zp_complevel = hdr->b_complevel; 6823 zio_flags |= ZIO_FLAG_RAW_COMPRESS; 6824 } 6825 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP); 6826 callback->awcb_ready = ready; 6827 callback->awcb_children_ready = children_ready; 6828 callback->awcb_physdone = physdone; 6829 callback->awcb_done = done; 6830 callback->awcb_private = private; 6831 callback->awcb_buf = buf; 6832 6833 /* 6834 * The hdr's b_pabd is now stale, free it now. A new data block 6835 * will be allocated when the zio pipeline calls arc_write_ready(). 6836 */ 6837 if (hdr->b_l1hdr.b_pabd != NULL) { 6838 /* 6839 * If the buf is currently sharing the data block with 6840 * the hdr then we need to break that relationship here. 6841 * The hdr will remain with a NULL data pointer and the 6842 * buf will take sole ownership of the block. 6843 */ 6844 if (arc_buf_is_shared(buf)) { 6845 arc_unshare_buf(hdr, buf); 6846 } else { 6847 arc_hdr_free_abd(hdr, B_FALSE); 6848 } 6849 VERIFY3P(buf->b_data, !=, NULL); 6850 } 6851 6852 if (HDR_HAS_RABD(hdr)) 6853 arc_hdr_free_abd(hdr, B_TRUE); 6854 6855 if (!(zio_flags & ZIO_FLAG_RAW)) 6856 arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF); 6857 6858 ASSERT(!arc_buf_is_shared(buf)); 6859 ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL); 6860 6861 zio = zio_write(pio, spa, txg, bp, 6862 abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)), 6863 HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready, 6864 (children_ready != NULL) ? arc_write_children_ready : NULL, 6865 arc_write_physdone, arc_write_done, callback, 6866 priority, zio_flags, zb); 6867 6868 return (zio); 6869 } 6870 6871 void 6872 arc_tempreserve_clear(uint64_t reserve) 6873 { 6874 atomic_add_64(&arc_tempreserve, -reserve); 6875 ASSERT((int64_t)arc_tempreserve >= 0); 6876 } 6877 6878 int 6879 arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg) 6880 { 6881 int error; 6882 uint64_t anon_size; 6883 6884 if (!arc_no_grow && 6885 reserve > arc_c/4 && 6886 reserve * 4 > (2ULL << SPA_MAXBLOCKSHIFT)) 6887 arc_c = MIN(arc_c_max, reserve * 4); 6888 6889 /* 6890 * Throttle when the calculated memory footprint for the TXG 6891 * exceeds the target ARC size. 6892 */ 6893 if (reserve > arc_c) { 6894 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve); 6895 return (SET_ERROR(ERESTART)); 6896 } 6897 6898 /* 6899 * Don't count loaned bufs as in flight dirty data to prevent long 6900 * network delays from blocking transactions that are ready to be 6901 * assigned to a txg. 6902 */ 6903 6904 /* assert that it has not wrapped around */ 6905 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0); 6906 6907 anon_size = MAX((int64_t) 6908 (zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_DATA]) + 6909 zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_METADATA]) - 6910 arc_loaned_bytes), 0); 6911 6912 /* 6913 * Writes will, almost always, require additional memory allocations 6914 * in order to compress/encrypt/etc the data. We therefore need to 6915 * make sure that there is sufficient available memory for this. 6916 */ 6917 error = arc_memory_throttle(spa, reserve, txg); 6918 if (error != 0) 6919 return (error); 6920 6921 /* 6922 * Throttle writes when the amount of dirty data in the cache 6923 * gets too large. We try to keep the cache less than half full 6924 * of dirty blocks so that our sync times don't grow too large. 6925 * 6926 * In the case of one pool being built on another pool, we want 6927 * to make sure we don't end up throttling the lower (backing) 6928 * pool when the upper pool is the majority contributor to dirty 6929 * data. To insure we make forward progress during throttling, we 6930 * also check the current pool's net dirty data and only throttle 6931 * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty 6932 * data in the cache. 6933 * 6934 * Note: if two requests come in concurrently, we might let them 6935 * both succeed, when one of them should fail. Not a huge deal. 6936 */ 6937 uint64_t total_dirty = reserve + arc_tempreserve + anon_size; 6938 uint64_t spa_dirty_anon = spa_dirty_data(spa); 6939 uint64_t rarc_c = arc_warm ? arc_c : arc_c_max; 6940 if (total_dirty > rarc_c * zfs_arc_dirty_limit_percent / 100 && 6941 anon_size > rarc_c * zfs_arc_anon_limit_percent / 100 && 6942 spa_dirty_anon > anon_size * zfs_arc_pool_dirty_percent / 100) { 6943 #ifdef ZFS_DEBUG 6944 uint64_t meta_esize = zfs_refcount_count( 6945 &arc_anon->arcs_esize[ARC_BUFC_METADATA]); 6946 uint64_t data_esize = 6947 zfs_refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 6948 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK " 6949 "anon_data=%lluK tempreserve=%lluK rarc_c=%lluK\n", 6950 (u_longlong_t)arc_tempreserve >> 10, 6951 (u_longlong_t)meta_esize >> 10, 6952 (u_longlong_t)data_esize >> 10, 6953 (u_longlong_t)reserve >> 10, 6954 (u_longlong_t)rarc_c >> 10); 6955 #endif 6956 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle); 6957 return (SET_ERROR(ERESTART)); 6958 } 6959 atomic_add_64(&arc_tempreserve, reserve); 6960 return (0); 6961 } 6962 6963 static void 6964 arc_kstat_update_state(arc_state_t *state, kstat_named_t *size, 6965 kstat_named_t *data, kstat_named_t *metadata, 6966 kstat_named_t *evict_data, kstat_named_t *evict_metadata) 6967 { 6968 data->value.ui64 = 6969 zfs_refcount_count(&state->arcs_size[ARC_BUFC_DATA]); 6970 metadata->value.ui64 = 6971 zfs_refcount_count(&state->arcs_size[ARC_BUFC_METADATA]); 6972 size->value.ui64 = data->value.ui64 + metadata->value.ui64; 6973 evict_data->value.ui64 = 6974 zfs_refcount_count(&state->arcs_esize[ARC_BUFC_DATA]); 6975 evict_metadata->value.ui64 = 6976 zfs_refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]); 6977 } 6978 6979 static int 6980 arc_kstat_update(kstat_t *ksp, int rw) 6981 { 6982 arc_stats_t *as = ksp->ks_data; 6983 6984 if (rw == KSTAT_WRITE) 6985 return (SET_ERROR(EACCES)); 6986 6987 as->arcstat_hits.value.ui64 = 6988 wmsum_value(&arc_sums.arcstat_hits); 6989 as->arcstat_iohits.value.ui64 = 6990 wmsum_value(&arc_sums.arcstat_iohits); 6991 as->arcstat_misses.value.ui64 = 6992 wmsum_value(&arc_sums.arcstat_misses); 6993 as->arcstat_demand_data_hits.value.ui64 = 6994 wmsum_value(&arc_sums.arcstat_demand_data_hits); 6995 as->arcstat_demand_data_iohits.value.ui64 = 6996 wmsum_value(&arc_sums.arcstat_demand_data_iohits); 6997 as->arcstat_demand_data_misses.value.ui64 = 6998 wmsum_value(&arc_sums.arcstat_demand_data_misses); 6999 as->arcstat_demand_metadata_hits.value.ui64 = 7000 wmsum_value(&arc_sums.arcstat_demand_metadata_hits); 7001 as->arcstat_demand_metadata_iohits.value.ui64 = 7002 wmsum_value(&arc_sums.arcstat_demand_metadata_iohits); 7003 as->arcstat_demand_metadata_misses.value.ui64 = 7004 wmsum_value(&arc_sums.arcstat_demand_metadata_misses); 7005 as->arcstat_prefetch_data_hits.value.ui64 = 7006 wmsum_value(&arc_sums.arcstat_prefetch_data_hits); 7007 as->arcstat_prefetch_data_iohits.value.ui64 = 7008 wmsum_value(&arc_sums.arcstat_prefetch_data_iohits); 7009 as->arcstat_prefetch_data_misses.value.ui64 = 7010 wmsum_value(&arc_sums.arcstat_prefetch_data_misses); 7011 as->arcstat_prefetch_metadata_hits.value.ui64 = 7012 wmsum_value(&arc_sums.arcstat_prefetch_metadata_hits); 7013 as->arcstat_prefetch_metadata_iohits.value.ui64 = 7014 wmsum_value(&arc_sums.arcstat_prefetch_metadata_iohits); 7015 as->arcstat_prefetch_metadata_misses.value.ui64 = 7016 wmsum_value(&arc_sums.arcstat_prefetch_metadata_misses); 7017 as->arcstat_mru_hits.value.ui64 = 7018 wmsum_value(&arc_sums.arcstat_mru_hits); 7019 as->arcstat_mru_ghost_hits.value.ui64 = 7020 wmsum_value(&arc_sums.arcstat_mru_ghost_hits); 7021 as->arcstat_mfu_hits.value.ui64 = 7022 wmsum_value(&arc_sums.arcstat_mfu_hits); 7023 as->arcstat_mfu_ghost_hits.value.ui64 = 7024 wmsum_value(&arc_sums.arcstat_mfu_ghost_hits); 7025 as->arcstat_uncached_hits.value.ui64 = 7026 wmsum_value(&arc_sums.arcstat_uncached_hits); 7027 as->arcstat_deleted.value.ui64 = 7028 wmsum_value(&arc_sums.arcstat_deleted); 7029 as->arcstat_mutex_miss.value.ui64 = 7030 wmsum_value(&arc_sums.arcstat_mutex_miss); 7031 as->arcstat_access_skip.value.ui64 = 7032 wmsum_value(&arc_sums.arcstat_access_skip); 7033 as->arcstat_evict_skip.value.ui64 = 7034 wmsum_value(&arc_sums.arcstat_evict_skip); 7035 as->arcstat_evict_not_enough.value.ui64 = 7036 wmsum_value(&arc_sums.arcstat_evict_not_enough); 7037 as->arcstat_evict_l2_cached.value.ui64 = 7038 wmsum_value(&arc_sums.arcstat_evict_l2_cached); 7039 as->arcstat_evict_l2_eligible.value.ui64 = 7040 wmsum_value(&arc_sums.arcstat_evict_l2_eligible); 7041 as->arcstat_evict_l2_eligible_mfu.value.ui64 = 7042 wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mfu); 7043 as->arcstat_evict_l2_eligible_mru.value.ui64 = 7044 wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mru); 7045 as->arcstat_evict_l2_ineligible.value.ui64 = 7046 wmsum_value(&arc_sums.arcstat_evict_l2_ineligible); 7047 as->arcstat_evict_l2_skip.value.ui64 = 7048 wmsum_value(&arc_sums.arcstat_evict_l2_skip); 7049 as->arcstat_hash_collisions.value.ui64 = 7050 wmsum_value(&arc_sums.arcstat_hash_collisions); 7051 as->arcstat_hash_chains.value.ui64 = 7052 wmsum_value(&arc_sums.arcstat_hash_chains); 7053 as->arcstat_size.value.ui64 = 7054 aggsum_value(&arc_sums.arcstat_size); 7055 as->arcstat_compressed_size.value.ui64 = 7056 wmsum_value(&arc_sums.arcstat_compressed_size); 7057 as->arcstat_uncompressed_size.value.ui64 = 7058 wmsum_value(&arc_sums.arcstat_uncompressed_size); 7059 as->arcstat_overhead_size.value.ui64 = 7060 wmsum_value(&arc_sums.arcstat_overhead_size); 7061 as->arcstat_hdr_size.value.ui64 = 7062 wmsum_value(&arc_sums.arcstat_hdr_size); 7063 as->arcstat_data_size.value.ui64 = 7064 wmsum_value(&arc_sums.arcstat_data_size); 7065 as->arcstat_metadata_size.value.ui64 = 7066 wmsum_value(&arc_sums.arcstat_metadata_size); 7067 as->arcstat_dbuf_size.value.ui64 = 7068 wmsum_value(&arc_sums.arcstat_dbuf_size); 7069 #if defined(COMPAT_FREEBSD11) 7070 as->arcstat_other_size.value.ui64 = 7071 wmsum_value(&arc_sums.arcstat_bonus_size) + 7072 wmsum_value(&arc_sums.arcstat_dnode_size) + 7073 wmsum_value(&arc_sums.arcstat_dbuf_size); 7074 #endif 7075 7076 arc_kstat_update_state(arc_anon, 7077 &as->arcstat_anon_size, 7078 &as->arcstat_anon_data, 7079 &as->arcstat_anon_metadata, 7080 &as->arcstat_anon_evictable_data, 7081 &as->arcstat_anon_evictable_metadata); 7082 arc_kstat_update_state(arc_mru, 7083 &as->arcstat_mru_size, 7084 &as->arcstat_mru_data, 7085 &as->arcstat_mru_metadata, 7086 &as->arcstat_mru_evictable_data, 7087 &as->arcstat_mru_evictable_metadata); 7088 arc_kstat_update_state(arc_mru_ghost, 7089 &as->arcstat_mru_ghost_size, 7090 &as->arcstat_mru_ghost_data, 7091 &as->arcstat_mru_ghost_metadata, 7092 &as->arcstat_mru_ghost_evictable_data, 7093 &as->arcstat_mru_ghost_evictable_metadata); 7094 arc_kstat_update_state(arc_mfu, 7095 &as->arcstat_mfu_size, 7096 &as->arcstat_mfu_data, 7097 &as->arcstat_mfu_metadata, 7098 &as->arcstat_mfu_evictable_data, 7099 &as->arcstat_mfu_evictable_metadata); 7100 arc_kstat_update_state(arc_mfu_ghost, 7101 &as->arcstat_mfu_ghost_size, 7102 &as->arcstat_mfu_ghost_data, 7103 &as->arcstat_mfu_ghost_metadata, 7104 &as->arcstat_mfu_ghost_evictable_data, 7105 &as->arcstat_mfu_ghost_evictable_metadata); 7106 arc_kstat_update_state(arc_uncached, 7107 &as->arcstat_uncached_size, 7108 &as->arcstat_uncached_data, 7109 &as->arcstat_uncached_metadata, 7110 &as->arcstat_uncached_evictable_data, 7111 &as->arcstat_uncached_evictable_metadata); 7112 7113 as->arcstat_dnode_size.value.ui64 = 7114 wmsum_value(&arc_sums.arcstat_dnode_size); 7115 as->arcstat_bonus_size.value.ui64 = 7116 wmsum_value(&arc_sums.arcstat_bonus_size); 7117 as->arcstat_l2_hits.value.ui64 = 7118 wmsum_value(&arc_sums.arcstat_l2_hits); 7119 as->arcstat_l2_misses.value.ui64 = 7120 wmsum_value(&arc_sums.arcstat_l2_misses); 7121 as->arcstat_l2_prefetch_asize.value.ui64 = 7122 wmsum_value(&arc_sums.arcstat_l2_prefetch_asize); 7123 as->arcstat_l2_mru_asize.value.ui64 = 7124 wmsum_value(&arc_sums.arcstat_l2_mru_asize); 7125 as->arcstat_l2_mfu_asize.value.ui64 = 7126 wmsum_value(&arc_sums.arcstat_l2_mfu_asize); 7127 as->arcstat_l2_bufc_data_asize.value.ui64 = 7128 wmsum_value(&arc_sums.arcstat_l2_bufc_data_asize); 7129 as->arcstat_l2_bufc_metadata_asize.value.ui64 = 7130 wmsum_value(&arc_sums.arcstat_l2_bufc_metadata_asize); 7131 as->arcstat_l2_feeds.value.ui64 = 7132 wmsum_value(&arc_sums.arcstat_l2_feeds); 7133 as->arcstat_l2_rw_clash.value.ui64 = 7134 wmsum_value(&arc_sums.arcstat_l2_rw_clash); 7135 as->arcstat_l2_read_bytes.value.ui64 = 7136 wmsum_value(&arc_sums.arcstat_l2_read_bytes); 7137 as->arcstat_l2_write_bytes.value.ui64 = 7138 wmsum_value(&arc_sums.arcstat_l2_write_bytes); 7139 as->arcstat_l2_writes_sent.value.ui64 = 7140 wmsum_value(&arc_sums.arcstat_l2_writes_sent); 7141 as->arcstat_l2_writes_done.value.ui64 = 7142 wmsum_value(&arc_sums.arcstat_l2_writes_done); 7143 as->arcstat_l2_writes_error.value.ui64 = 7144 wmsum_value(&arc_sums.arcstat_l2_writes_error); 7145 as->arcstat_l2_writes_lock_retry.value.ui64 = 7146 wmsum_value(&arc_sums.arcstat_l2_writes_lock_retry); 7147 as->arcstat_l2_evict_lock_retry.value.ui64 = 7148 wmsum_value(&arc_sums.arcstat_l2_evict_lock_retry); 7149 as->arcstat_l2_evict_reading.value.ui64 = 7150 wmsum_value(&arc_sums.arcstat_l2_evict_reading); 7151 as->arcstat_l2_evict_l1cached.value.ui64 = 7152 wmsum_value(&arc_sums.arcstat_l2_evict_l1cached); 7153 as->arcstat_l2_free_on_write.value.ui64 = 7154 wmsum_value(&arc_sums.arcstat_l2_free_on_write); 7155 as->arcstat_l2_abort_lowmem.value.ui64 = 7156 wmsum_value(&arc_sums.arcstat_l2_abort_lowmem); 7157 as->arcstat_l2_cksum_bad.value.ui64 = 7158 wmsum_value(&arc_sums.arcstat_l2_cksum_bad); 7159 as->arcstat_l2_io_error.value.ui64 = 7160 wmsum_value(&arc_sums.arcstat_l2_io_error); 7161 as->arcstat_l2_lsize.value.ui64 = 7162 wmsum_value(&arc_sums.arcstat_l2_lsize); 7163 as->arcstat_l2_psize.value.ui64 = 7164 wmsum_value(&arc_sums.arcstat_l2_psize); 7165 as->arcstat_l2_hdr_size.value.ui64 = 7166 aggsum_value(&arc_sums.arcstat_l2_hdr_size); 7167 as->arcstat_l2_log_blk_writes.value.ui64 = 7168 wmsum_value(&arc_sums.arcstat_l2_log_blk_writes); 7169 as->arcstat_l2_log_blk_asize.value.ui64 = 7170 wmsum_value(&arc_sums.arcstat_l2_log_blk_asize); 7171 as->arcstat_l2_log_blk_count.value.ui64 = 7172 wmsum_value(&arc_sums.arcstat_l2_log_blk_count); 7173 as->arcstat_l2_rebuild_success.value.ui64 = 7174 wmsum_value(&arc_sums.arcstat_l2_rebuild_success); 7175 as->arcstat_l2_rebuild_abort_unsupported.value.ui64 = 7176 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_unsupported); 7177 as->arcstat_l2_rebuild_abort_io_errors.value.ui64 = 7178 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_io_errors); 7179 as->arcstat_l2_rebuild_abort_dh_errors.value.ui64 = 7180 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_dh_errors); 7181 as->arcstat_l2_rebuild_abort_cksum_lb_errors.value.ui64 = 7182 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors); 7183 as->arcstat_l2_rebuild_abort_lowmem.value.ui64 = 7184 wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_lowmem); 7185 as->arcstat_l2_rebuild_size.value.ui64 = 7186 wmsum_value(&arc_sums.arcstat_l2_rebuild_size); 7187 as->arcstat_l2_rebuild_asize.value.ui64 = 7188 wmsum_value(&arc_sums.arcstat_l2_rebuild_asize); 7189 as->arcstat_l2_rebuild_bufs.value.ui64 = 7190 wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs); 7191 as->arcstat_l2_rebuild_bufs_precached.value.ui64 = 7192 wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs_precached); 7193 as->arcstat_l2_rebuild_log_blks.value.ui64 = 7194 wmsum_value(&arc_sums.arcstat_l2_rebuild_log_blks); 7195 as->arcstat_memory_throttle_count.value.ui64 = 7196 wmsum_value(&arc_sums.arcstat_memory_throttle_count); 7197 as->arcstat_memory_direct_count.value.ui64 = 7198 wmsum_value(&arc_sums.arcstat_memory_direct_count); 7199 as->arcstat_memory_indirect_count.value.ui64 = 7200 wmsum_value(&arc_sums.arcstat_memory_indirect_count); 7201 7202 as->arcstat_memory_all_bytes.value.ui64 = 7203 arc_all_memory(); 7204 as->arcstat_memory_free_bytes.value.ui64 = 7205 arc_free_memory(); 7206 as->arcstat_memory_available_bytes.value.i64 = 7207 arc_available_memory(); 7208 7209 as->arcstat_prune.value.ui64 = 7210 wmsum_value(&arc_sums.arcstat_prune); 7211 as->arcstat_meta_used.value.ui64 = 7212 wmsum_value(&arc_sums.arcstat_meta_used); 7213 as->arcstat_async_upgrade_sync.value.ui64 = 7214 wmsum_value(&arc_sums.arcstat_async_upgrade_sync); 7215 as->arcstat_predictive_prefetch.value.ui64 = 7216 wmsum_value(&arc_sums.arcstat_predictive_prefetch); 7217 as->arcstat_demand_hit_predictive_prefetch.value.ui64 = 7218 wmsum_value(&arc_sums.arcstat_demand_hit_predictive_prefetch); 7219 as->arcstat_demand_iohit_predictive_prefetch.value.ui64 = 7220 wmsum_value(&arc_sums.arcstat_demand_iohit_predictive_prefetch); 7221 as->arcstat_prescient_prefetch.value.ui64 = 7222 wmsum_value(&arc_sums.arcstat_prescient_prefetch); 7223 as->arcstat_demand_hit_prescient_prefetch.value.ui64 = 7224 wmsum_value(&arc_sums.arcstat_demand_hit_prescient_prefetch); 7225 as->arcstat_demand_iohit_prescient_prefetch.value.ui64 = 7226 wmsum_value(&arc_sums.arcstat_demand_iohit_prescient_prefetch); 7227 as->arcstat_raw_size.value.ui64 = 7228 wmsum_value(&arc_sums.arcstat_raw_size); 7229 as->arcstat_cached_only_in_progress.value.ui64 = 7230 wmsum_value(&arc_sums.arcstat_cached_only_in_progress); 7231 as->arcstat_abd_chunk_waste_size.value.ui64 = 7232 wmsum_value(&arc_sums.arcstat_abd_chunk_waste_size); 7233 7234 return (0); 7235 } 7236 7237 /* 7238 * This function *must* return indices evenly distributed between all 7239 * sublists of the multilist. This is needed due to how the ARC eviction 7240 * code is laid out; arc_evict_state() assumes ARC buffers are evenly 7241 * distributed between all sublists and uses this assumption when 7242 * deciding which sublist to evict from and how much to evict from it. 7243 */ 7244 static unsigned int 7245 arc_state_multilist_index_func(multilist_t *ml, void *obj) 7246 { 7247 arc_buf_hdr_t *hdr = obj; 7248 7249 /* 7250 * We rely on b_dva to generate evenly distributed index 7251 * numbers using buf_hash below. So, as an added precaution, 7252 * let's make sure we never add empty buffers to the arc lists. 7253 */ 7254 ASSERT(!HDR_EMPTY(hdr)); 7255 7256 /* 7257 * The assumption here, is the hash value for a given 7258 * arc_buf_hdr_t will remain constant throughout its lifetime 7259 * (i.e. its b_spa, b_dva, and b_birth fields don't change). 7260 * Thus, we don't need to store the header's sublist index 7261 * on insertion, as this index can be recalculated on removal. 7262 * 7263 * Also, the low order bits of the hash value are thought to be 7264 * distributed evenly. Otherwise, in the case that the multilist 7265 * has a power of two number of sublists, each sublists' usage 7266 * would not be evenly distributed. In this context full 64bit 7267 * division would be a waste of time, so limit it to 32 bits. 7268 */ 7269 return ((unsigned int)buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) % 7270 multilist_get_num_sublists(ml)); 7271 } 7272 7273 static unsigned int 7274 arc_state_l2c_multilist_index_func(multilist_t *ml, void *obj) 7275 { 7276 panic("Header %p insert into arc_l2c_only %p", obj, ml); 7277 } 7278 7279 #define WARN_IF_TUNING_IGNORED(tuning, value, do_warn) do { \ 7280 if ((do_warn) && (tuning) && ((tuning) != (value))) { \ 7281 cmn_err(CE_WARN, \ 7282 "ignoring tunable %s (using %llu instead)", \ 7283 (#tuning), (u_longlong_t)(value)); \ 7284 } \ 7285 } while (0) 7286 7287 /* 7288 * Called during module initialization and periodically thereafter to 7289 * apply reasonable changes to the exposed performance tunings. Can also be 7290 * called explicitly by param_set_arc_*() functions when ARC tunables are 7291 * updated manually. Non-zero zfs_* values which differ from the currently set 7292 * values will be applied. 7293 */ 7294 void 7295 arc_tuning_update(boolean_t verbose) 7296 { 7297 uint64_t allmem = arc_all_memory(); 7298 7299 /* Valid range: 32M - <arc_c_max> */ 7300 if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) && 7301 (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) && 7302 (zfs_arc_min <= arc_c_max)) { 7303 arc_c_min = zfs_arc_min; 7304 arc_c = MAX(arc_c, arc_c_min); 7305 } 7306 WARN_IF_TUNING_IGNORED(zfs_arc_min, arc_c_min, verbose); 7307 7308 /* Valid range: 64M - <all physical memory> */ 7309 if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) && 7310 (zfs_arc_max >= MIN_ARC_MAX) && (zfs_arc_max < allmem) && 7311 (zfs_arc_max > arc_c_min)) { 7312 arc_c_max = zfs_arc_max; 7313 arc_c = MIN(arc_c, arc_c_max); 7314 if (arc_dnode_limit > arc_c_max) 7315 arc_dnode_limit = arc_c_max; 7316 } 7317 WARN_IF_TUNING_IGNORED(zfs_arc_max, arc_c_max, verbose); 7318 7319 /* Valid range: 0 - <all physical memory> */ 7320 arc_dnode_limit = zfs_arc_dnode_limit ? zfs_arc_dnode_limit : 7321 MIN(zfs_arc_dnode_limit_percent, 100) * arc_c_max / 100; 7322 WARN_IF_TUNING_IGNORED(zfs_arc_dnode_limit, arc_dnode_limit, verbose); 7323 7324 /* Valid range: 1 - N */ 7325 if (zfs_arc_grow_retry) 7326 arc_grow_retry = zfs_arc_grow_retry; 7327 7328 /* Valid range: 1 - N */ 7329 if (zfs_arc_shrink_shift) { 7330 arc_shrink_shift = zfs_arc_shrink_shift; 7331 arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1); 7332 } 7333 7334 /* Valid range: 1 - N ms */ 7335 if (zfs_arc_min_prefetch_ms) 7336 arc_min_prefetch_ms = zfs_arc_min_prefetch_ms; 7337 7338 /* Valid range: 1 - N ms */ 7339 if (zfs_arc_min_prescient_prefetch_ms) { 7340 arc_min_prescient_prefetch_ms = 7341 zfs_arc_min_prescient_prefetch_ms; 7342 } 7343 7344 /* Valid range: 0 - 100 */ 7345 if (zfs_arc_lotsfree_percent <= 100) 7346 arc_lotsfree_percent = zfs_arc_lotsfree_percent; 7347 WARN_IF_TUNING_IGNORED(zfs_arc_lotsfree_percent, arc_lotsfree_percent, 7348 verbose); 7349 7350 /* Valid range: 0 - <all physical memory> */ 7351 if ((zfs_arc_sys_free) && (zfs_arc_sys_free != arc_sys_free)) 7352 arc_sys_free = MIN(zfs_arc_sys_free, allmem); 7353 WARN_IF_TUNING_IGNORED(zfs_arc_sys_free, arc_sys_free, verbose); 7354 } 7355 7356 static void 7357 arc_state_multilist_init(multilist_t *ml, 7358 multilist_sublist_index_func_t *index_func, int *maxcountp) 7359 { 7360 multilist_create(ml, sizeof (arc_buf_hdr_t), 7361 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), index_func); 7362 *maxcountp = MAX(*maxcountp, multilist_get_num_sublists(ml)); 7363 } 7364 7365 static void 7366 arc_state_init(void) 7367 { 7368 int num_sublists = 0; 7369 7370 arc_state_multilist_init(&arc_mru->arcs_list[ARC_BUFC_METADATA], 7371 arc_state_multilist_index_func, &num_sublists); 7372 arc_state_multilist_init(&arc_mru->arcs_list[ARC_BUFC_DATA], 7373 arc_state_multilist_index_func, &num_sublists); 7374 arc_state_multilist_init(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA], 7375 arc_state_multilist_index_func, &num_sublists); 7376 arc_state_multilist_init(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA], 7377 arc_state_multilist_index_func, &num_sublists); 7378 arc_state_multilist_init(&arc_mfu->arcs_list[ARC_BUFC_METADATA], 7379 arc_state_multilist_index_func, &num_sublists); 7380 arc_state_multilist_init(&arc_mfu->arcs_list[ARC_BUFC_DATA], 7381 arc_state_multilist_index_func, &num_sublists); 7382 arc_state_multilist_init(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA], 7383 arc_state_multilist_index_func, &num_sublists); 7384 arc_state_multilist_init(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA], 7385 arc_state_multilist_index_func, &num_sublists); 7386 arc_state_multilist_init(&arc_uncached->arcs_list[ARC_BUFC_METADATA], 7387 arc_state_multilist_index_func, &num_sublists); 7388 arc_state_multilist_init(&arc_uncached->arcs_list[ARC_BUFC_DATA], 7389 arc_state_multilist_index_func, &num_sublists); 7390 7391 /* 7392 * L2 headers should never be on the L2 state list since they don't 7393 * have L1 headers allocated. Special index function asserts that. 7394 */ 7395 arc_state_multilist_init(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA], 7396 arc_state_l2c_multilist_index_func, &num_sublists); 7397 arc_state_multilist_init(&arc_l2c_only->arcs_list[ARC_BUFC_DATA], 7398 arc_state_l2c_multilist_index_func, &num_sublists); 7399 7400 /* 7401 * Keep track of the number of markers needed to reclaim buffers from 7402 * any ARC state. The markers will be pre-allocated so as to minimize 7403 * the number of memory allocations performed by the eviction thread. 7404 */ 7405 arc_state_evict_marker_count = num_sublists; 7406 7407 zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 7408 zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 7409 zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 7410 zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 7411 zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 7412 zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 7413 zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 7414 zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 7415 zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 7416 zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 7417 zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 7418 zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 7419 zfs_refcount_create(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]); 7420 zfs_refcount_create(&arc_uncached->arcs_esize[ARC_BUFC_DATA]); 7421 7422 zfs_refcount_create(&arc_anon->arcs_size[ARC_BUFC_DATA]); 7423 zfs_refcount_create(&arc_anon->arcs_size[ARC_BUFC_METADATA]); 7424 zfs_refcount_create(&arc_mru->arcs_size[ARC_BUFC_DATA]); 7425 zfs_refcount_create(&arc_mru->arcs_size[ARC_BUFC_METADATA]); 7426 zfs_refcount_create(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]); 7427 zfs_refcount_create(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]); 7428 zfs_refcount_create(&arc_mfu->arcs_size[ARC_BUFC_DATA]); 7429 zfs_refcount_create(&arc_mfu->arcs_size[ARC_BUFC_METADATA]); 7430 zfs_refcount_create(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]); 7431 zfs_refcount_create(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]); 7432 zfs_refcount_create(&arc_l2c_only->arcs_size[ARC_BUFC_DATA]); 7433 zfs_refcount_create(&arc_l2c_only->arcs_size[ARC_BUFC_METADATA]); 7434 zfs_refcount_create(&arc_uncached->arcs_size[ARC_BUFC_DATA]); 7435 zfs_refcount_create(&arc_uncached->arcs_size[ARC_BUFC_METADATA]); 7436 7437 wmsum_init(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA], 0); 7438 wmsum_init(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA], 0); 7439 wmsum_init(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA], 0); 7440 wmsum_init(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA], 0); 7441 7442 wmsum_init(&arc_sums.arcstat_hits, 0); 7443 wmsum_init(&arc_sums.arcstat_iohits, 0); 7444 wmsum_init(&arc_sums.arcstat_misses, 0); 7445 wmsum_init(&arc_sums.arcstat_demand_data_hits, 0); 7446 wmsum_init(&arc_sums.arcstat_demand_data_iohits, 0); 7447 wmsum_init(&arc_sums.arcstat_demand_data_misses, 0); 7448 wmsum_init(&arc_sums.arcstat_demand_metadata_hits, 0); 7449 wmsum_init(&arc_sums.arcstat_demand_metadata_iohits, 0); 7450 wmsum_init(&arc_sums.arcstat_demand_metadata_misses, 0); 7451 wmsum_init(&arc_sums.arcstat_prefetch_data_hits, 0); 7452 wmsum_init(&arc_sums.arcstat_prefetch_data_iohits, 0); 7453 wmsum_init(&arc_sums.arcstat_prefetch_data_misses, 0); 7454 wmsum_init(&arc_sums.arcstat_prefetch_metadata_hits, 0); 7455 wmsum_init(&arc_sums.arcstat_prefetch_metadata_iohits, 0); 7456 wmsum_init(&arc_sums.arcstat_prefetch_metadata_misses, 0); 7457 wmsum_init(&arc_sums.arcstat_mru_hits, 0); 7458 wmsum_init(&arc_sums.arcstat_mru_ghost_hits, 0); 7459 wmsum_init(&arc_sums.arcstat_mfu_hits, 0); 7460 wmsum_init(&arc_sums.arcstat_mfu_ghost_hits, 0); 7461 wmsum_init(&arc_sums.arcstat_uncached_hits, 0); 7462 wmsum_init(&arc_sums.arcstat_deleted, 0); 7463 wmsum_init(&arc_sums.arcstat_mutex_miss, 0); 7464 wmsum_init(&arc_sums.arcstat_access_skip, 0); 7465 wmsum_init(&arc_sums.arcstat_evict_skip, 0); 7466 wmsum_init(&arc_sums.arcstat_evict_not_enough, 0); 7467 wmsum_init(&arc_sums.arcstat_evict_l2_cached, 0); 7468 wmsum_init(&arc_sums.arcstat_evict_l2_eligible, 0); 7469 wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mfu, 0); 7470 wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mru, 0); 7471 wmsum_init(&arc_sums.arcstat_evict_l2_ineligible, 0); 7472 wmsum_init(&arc_sums.arcstat_evict_l2_skip, 0); 7473 wmsum_init(&arc_sums.arcstat_hash_collisions, 0); 7474 wmsum_init(&arc_sums.arcstat_hash_chains, 0); 7475 aggsum_init(&arc_sums.arcstat_size, 0); 7476 wmsum_init(&arc_sums.arcstat_compressed_size, 0); 7477 wmsum_init(&arc_sums.arcstat_uncompressed_size, 0); 7478 wmsum_init(&arc_sums.arcstat_overhead_size, 0); 7479 wmsum_init(&arc_sums.arcstat_hdr_size, 0); 7480 wmsum_init(&arc_sums.arcstat_data_size, 0); 7481 wmsum_init(&arc_sums.arcstat_metadata_size, 0); 7482 wmsum_init(&arc_sums.arcstat_dbuf_size, 0); 7483 wmsum_init(&arc_sums.arcstat_dnode_size, 0); 7484 wmsum_init(&arc_sums.arcstat_bonus_size, 0); 7485 wmsum_init(&arc_sums.arcstat_l2_hits, 0); 7486 wmsum_init(&arc_sums.arcstat_l2_misses, 0); 7487 wmsum_init(&arc_sums.arcstat_l2_prefetch_asize, 0); 7488 wmsum_init(&arc_sums.arcstat_l2_mru_asize, 0); 7489 wmsum_init(&arc_sums.arcstat_l2_mfu_asize, 0); 7490 wmsum_init(&arc_sums.arcstat_l2_bufc_data_asize, 0); 7491 wmsum_init(&arc_sums.arcstat_l2_bufc_metadata_asize, 0); 7492 wmsum_init(&arc_sums.arcstat_l2_feeds, 0); 7493 wmsum_init(&arc_sums.arcstat_l2_rw_clash, 0); 7494 wmsum_init(&arc_sums.arcstat_l2_read_bytes, 0); 7495 wmsum_init(&arc_sums.arcstat_l2_write_bytes, 0); 7496 wmsum_init(&arc_sums.arcstat_l2_writes_sent, 0); 7497 wmsum_init(&arc_sums.arcstat_l2_writes_done, 0); 7498 wmsum_init(&arc_sums.arcstat_l2_writes_error, 0); 7499 wmsum_init(&arc_sums.arcstat_l2_writes_lock_retry, 0); 7500 wmsum_init(&arc_sums.arcstat_l2_evict_lock_retry, 0); 7501 wmsum_init(&arc_sums.arcstat_l2_evict_reading, 0); 7502 wmsum_init(&arc_sums.arcstat_l2_evict_l1cached, 0); 7503 wmsum_init(&arc_sums.arcstat_l2_free_on_write, 0); 7504 wmsum_init(&arc_sums.arcstat_l2_abort_lowmem, 0); 7505 wmsum_init(&arc_sums.arcstat_l2_cksum_bad, 0); 7506 wmsum_init(&arc_sums.arcstat_l2_io_error, 0); 7507 wmsum_init(&arc_sums.arcstat_l2_lsize, 0); 7508 wmsum_init(&arc_sums.arcstat_l2_psize, 0); 7509 aggsum_init(&arc_sums.arcstat_l2_hdr_size, 0); 7510 wmsum_init(&arc_sums.arcstat_l2_log_blk_writes, 0); 7511 wmsum_init(&arc_sums.arcstat_l2_log_blk_asize, 0); 7512 wmsum_init(&arc_sums.arcstat_l2_log_blk_count, 0); 7513 wmsum_init(&arc_sums.arcstat_l2_rebuild_success, 0); 7514 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_unsupported, 0); 7515 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_io_errors, 0); 7516 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_dh_errors, 0); 7517 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors, 0); 7518 wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_lowmem, 0); 7519 wmsum_init(&arc_sums.arcstat_l2_rebuild_size, 0); 7520 wmsum_init(&arc_sums.arcstat_l2_rebuild_asize, 0); 7521 wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs, 0); 7522 wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs_precached, 0); 7523 wmsum_init(&arc_sums.arcstat_l2_rebuild_log_blks, 0); 7524 wmsum_init(&arc_sums.arcstat_memory_throttle_count, 0); 7525 wmsum_init(&arc_sums.arcstat_memory_direct_count, 0); 7526 wmsum_init(&arc_sums.arcstat_memory_indirect_count, 0); 7527 wmsum_init(&arc_sums.arcstat_prune, 0); 7528 wmsum_init(&arc_sums.arcstat_meta_used, 0); 7529 wmsum_init(&arc_sums.arcstat_async_upgrade_sync, 0); 7530 wmsum_init(&arc_sums.arcstat_predictive_prefetch, 0); 7531 wmsum_init(&arc_sums.arcstat_demand_hit_predictive_prefetch, 0); 7532 wmsum_init(&arc_sums.arcstat_demand_iohit_predictive_prefetch, 0); 7533 wmsum_init(&arc_sums.arcstat_prescient_prefetch, 0); 7534 wmsum_init(&arc_sums.arcstat_demand_hit_prescient_prefetch, 0); 7535 wmsum_init(&arc_sums.arcstat_demand_iohit_prescient_prefetch, 0); 7536 wmsum_init(&arc_sums.arcstat_raw_size, 0); 7537 wmsum_init(&arc_sums.arcstat_cached_only_in_progress, 0); 7538 wmsum_init(&arc_sums.arcstat_abd_chunk_waste_size, 0); 7539 7540 arc_anon->arcs_state = ARC_STATE_ANON; 7541 arc_mru->arcs_state = ARC_STATE_MRU; 7542 arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST; 7543 arc_mfu->arcs_state = ARC_STATE_MFU; 7544 arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST; 7545 arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY; 7546 arc_uncached->arcs_state = ARC_STATE_UNCACHED; 7547 } 7548 7549 static void 7550 arc_state_fini(void) 7551 { 7552 zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]); 7553 zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]); 7554 zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]); 7555 zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]); 7556 zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]); 7557 zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]); 7558 zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]); 7559 zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]); 7560 zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]); 7561 zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]); 7562 zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]); 7563 zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]); 7564 zfs_refcount_destroy(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]); 7565 zfs_refcount_destroy(&arc_uncached->arcs_esize[ARC_BUFC_DATA]); 7566 7567 zfs_refcount_destroy(&arc_anon->arcs_size[ARC_BUFC_DATA]); 7568 zfs_refcount_destroy(&arc_anon->arcs_size[ARC_BUFC_METADATA]); 7569 zfs_refcount_destroy(&arc_mru->arcs_size[ARC_BUFC_DATA]); 7570 zfs_refcount_destroy(&arc_mru->arcs_size[ARC_BUFC_METADATA]); 7571 zfs_refcount_destroy(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]); 7572 zfs_refcount_destroy(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]); 7573 zfs_refcount_destroy(&arc_mfu->arcs_size[ARC_BUFC_DATA]); 7574 zfs_refcount_destroy(&arc_mfu->arcs_size[ARC_BUFC_METADATA]); 7575 zfs_refcount_destroy(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]); 7576 zfs_refcount_destroy(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]); 7577 zfs_refcount_destroy(&arc_l2c_only->arcs_size[ARC_BUFC_DATA]); 7578 zfs_refcount_destroy(&arc_l2c_only->arcs_size[ARC_BUFC_METADATA]); 7579 zfs_refcount_destroy(&arc_uncached->arcs_size[ARC_BUFC_DATA]); 7580 zfs_refcount_destroy(&arc_uncached->arcs_size[ARC_BUFC_METADATA]); 7581 7582 multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]); 7583 multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]); 7584 multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]); 7585 multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]); 7586 multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]); 7587 multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]); 7588 multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]); 7589 multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]); 7590 multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA]); 7591 multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]); 7592 multilist_destroy(&arc_uncached->arcs_list[ARC_BUFC_METADATA]); 7593 multilist_destroy(&arc_uncached->arcs_list[ARC_BUFC_DATA]); 7594 7595 wmsum_fini(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA]); 7596 wmsum_fini(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA]); 7597 wmsum_fini(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA]); 7598 wmsum_fini(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA]); 7599 7600 wmsum_fini(&arc_sums.arcstat_hits); 7601 wmsum_fini(&arc_sums.arcstat_iohits); 7602 wmsum_fini(&arc_sums.arcstat_misses); 7603 wmsum_fini(&arc_sums.arcstat_demand_data_hits); 7604 wmsum_fini(&arc_sums.arcstat_demand_data_iohits); 7605 wmsum_fini(&arc_sums.arcstat_demand_data_misses); 7606 wmsum_fini(&arc_sums.arcstat_demand_metadata_hits); 7607 wmsum_fini(&arc_sums.arcstat_demand_metadata_iohits); 7608 wmsum_fini(&arc_sums.arcstat_demand_metadata_misses); 7609 wmsum_fini(&arc_sums.arcstat_prefetch_data_hits); 7610 wmsum_fini(&arc_sums.arcstat_prefetch_data_iohits); 7611 wmsum_fini(&arc_sums.arcstat_prefetch_data_misses); 7612 wmsum_fini(&arc_sums.arcstat_prefetch_metadata_hits); 7613 wmsum_fini(&arc_sums.arcstat_prefetch_metadata_iohits); 7614 wmsum_fini(&arc_sums.arcstat_prefetch_metadata_misses); 7615 wmsum_fini(&arc_sums.arcstat_mru_hits); 7616 wmsum_fini(&arc_sums.arcstat_mru_ghost_hits); 7617 wmsum_fini(&arc_sums.arcstat_mfu_hits); 7618 wmsum_fini(&arc_sums.arcstat_mfu_ghost_hits); 7619 wmsum_fini(&arc_sums.arcstat_uncached_hits); 7620 wmsum_fini(&arc_sums.arcstat_deleted); 7621 wmsum_fini(&arc_sums.arcstat_mutex_miss); 7622 wmsum_fini(&arc_sums.arcstat_access_skip); 7623 wmsum_fini(&arc_sums.arcstat_evict_skip); 7624 wmsum_fini(&arc_sums.arcstat_evict_not_enough); 7625 wmsum_fini(&arc_sums.arcstat_evict_l2_cached); 7626 wmsum_fini(&arc_sums.arcstat_evict_l2_eligible); 7627 wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mfu); 7628 wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mru); 7629 wmsum_fini(&arc_sums.arcstat_evict_l2_ineligible); 7630 wmsum_fini(&arc_sums.arcstat_evict_l2_skip); 7631 wmsum_fini(&arc_sums.arcstat_hash_collisions); 7632 wmsum_fini(&arc_sums.arcstat_hash_chains); 7633 aggsum_fini(&arc_sums.arcstat_size); 7634 wmsum_fini(&arc_sums.arcstat_compressed_size); 7635 wmsum_fini(&arc_sums.arcstat_uncompressed_size); 7636 wmsum_fini(&arc_sums.arcstat_overhead_size); 7637 wmsum_fini(&arc_sums.arcstat_hdr_size); 7638 wmsum_fini(&arc_sums.arcstat_data_size); 7639 wmsum_fini(&arc_sums.arcstat_metadata_size); 7640 wmsum_fini(&arc_sums.arcstat_dbuf_size); 7641 wmsum_fini(&arc_sums.arcstat_dnode_size); 7642 wmsum_fini(&arc_sums.arcstat_bonus_size); 7643 wmsum_fini(&arc_sums.arcstat_l2_hits); 7644 wmsum_fini(&arc_sums.arcstat_l2_misses); 7645 wmsum_fini(&arc_sums.arcstat_l2_prefetch_asize); 7646 wmsum_fini(&arc_sums.arcstat_l2_mru_asize); 7647 wmsum_fini(&arc_sums.arcstat_l2_mfu_asize); 7648 wmsum_fini(&arc_sums.arcstat_l2_bufc_data_asize); 7649 wmsum_fini(&arc_sums.arcstat_l2_bufc_metadata_asize); 7650 wmsum_fini(&arc_sums.arcstat_l2_feeds); 7651 wmsum_fini(&arc_sums.arcstat_l2_rw_clash); 7652 wmsum_fini(&arc_sums.arcstat_l2_read_bytes); 7653 wmsum_fini(&arc_sums.arcstat_l2_write_bytes); 7654 wmsum_fini(&arc_sums.arcstat_l2_writes_sent); 7655 wmsum_fini(&arc_sums.arcstat_l2_writes_done); 7656 wmsum_fini(&arc_sums.arcstat_l2_writes_error); 7657 wmsum_fini(&arc_sums.arcstat_l2_writes_lock_retry); 7658 wmsum_fini(&arc_sums.arcstat_l2_evict_lock_retry); 7659 wmsum_fini(&arc_sums.arcstat_l2_evict_reading); 7660 wmsum_fini(&arc_sums.arcstat_l2_evict_l1cached); 7661 wmsum_fini(&arc_sums.arcstat_l2_free_on_write); 7662 wmsum_fini(&arc_sums.arcstat_l2_abort_lowmem); 7663 wmsum_fini(&arc_sums.arcstat_l2_cksum_bad); 7664 wmsum_fini(&arc_sums.arcstat_l2_io_error); 7665 wmsum_fini(&arc_sums.arcstat_l2_lsize); 7666 wmsum_fini(&arc_sums.arcstat_l2_psize); 7667 aggsum_fini(&arc_sums.arcstat_l2_hdr_size); 7668 wmsum_fini(&arc_sums.arcstat_l2_log_blk_writes); 7669 wmsum_fini(&arc_sums.arcstat_l2_log_blk_asize); 7670 wmsum_fini(&arc_sums.arcstat_l2_log_blk_count); 7671 wmsum_fini(&arc_sums.arcstat_l2_rebuild_success); 7672 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_unsupported); 7673 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_io_errors); 7674 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_dh_errors); 7675 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors); 7676 wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_lowmem); 7677 wmsum_fini(&arc_sums.arcstat_l2_rebuild_size); 7678 wmsum_fini(&arc_sums.arcstat_l2_rebuild_asize); 7679 wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs); 7680 wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs_precached); 7681 wmsum_fini(&arc_sums.arcstat_l2_rebuild_log_blks); 7682 wmsum_fini(&arc_sums.arcstat_memory_throttle_count); 7683 wmsum_fini(&arc_sums.arcstat_memory_direct_count); 7684 wmsum_fini(&arc_sums.arcstat_memory_indirect_count); 7685 wmsum_fini(&arc_sums.arcstat_prune); 7686 wmsum_fini(&arc_sums.arcstat_meta_used); 7687 wmsum_fini(&arc_sums.arcstat_async_upgrade_sync); 7688 wmsum_fini(&arc_sums.arcstat_predictive_prefetch); 7689 wmsum_fini(&arc_sums.arcstat_demand_hit_predictive_prefetch); 7690 wmsum_fini(&arc_sums.arcstat_demand_iohit_predictive_prefetch); 7691 wmsum_fini(&arc_sums.arcstat_prescient_prefetch); 7692 wmsum_fini(&arc_sums.arcstat_demand_hit_prescient_prefetch); 7693 wmsum_fini(&arc_sums.arcstat_demand_iohit_prescient_prefetch); 7694 wmsum_fini(&arc_sums.arcstat_raw_size); 7695 wmsum_fini(&arc_sums.arcstat_cached_only_in_progress); 7696 wmsum_fini(&arc_sums.arcstat_abd_chunk_waste_size); 7697 } 7698 7699 uint64_t 7700 arc_target_bytes(void) 7701 { 7702 return (arc_c); 7703 } 7704 7705 void 7706 arc_set_limits(uint64_t allmem) 7707 { 7708 /* Set min cache to 1/32 of all memory, or 32MB, whichever is more. */ 7709 arc_c_min = MAX(allmem / 32, 2ULL << SPA_MAXBLOCKSHIFT); 7710 7711 /* How to set default max varies by platform. */ 7712 arc_c_max = arc_default_max(arc_c_min, allmem); 7713 } 7714 void 7715 arc_init(void) 7716 { 7717 uint64_t percent, allmem = arc_all_memory(); 7718 mutex_init(&arc_evict_lock, NULL, MUTEX_DEFAULT, NULL); 7719 list_create(&arc_evict_waiters, sizeof (arc_evict_waiter_t), 7720 offsetof(arc_evict_waiter_t, aew_node)); 7721 7722 arc_min_prefetch_ms = 1000; 7723 arc_min_prescient_prefetch_ms = 6000; 7724 7725 #if defined(_KERNEL) 7726 arc_lowmem_init(); 7727 #endif 7728 7729 arc_set_limits(allmem); 7730 7731 #ifdef _KERNEL 7732 /* 7733 * If zfs_arc_max is non-zero at init, meaning it was set in the kernel 7734 * environment before the module was loaded, don't block setting the 7735 * maximum because it is less than arc_c_min, instead, reset arc_c_min 7736 * to a lower value. 7737 * zfs_arc_min will be handled by arc_tuning_update(). 7738 */ 7739 if (zfs_arc_max != 0 && zfs_arc_max >= MIN_ARC_MAX && 7740 zfs_arc_max < allmem) { 7741 arc_c_max = zfs_arc_max; 7742 if (arc_c_min >= arc_c_max) { 7743 arc_c_min = MAX(zfs_arc_max / 2, 7744 2ULL << SPA_MAXBLOCKSHIFT); 7745 } 7746 } 7747 #else 7748 /* 7749 * In userland, there's only the memory pressure that we artificially 7750 * create (see arc_available_memory()). Don't let arc_c get too 7751 * small, because it can cause transactions to be larger than 7752 * arc_c, causing arc_tempreserve_space() to fail. 7753 */ 7754 arc_c_min = MAX(arc_c_max / 2, 2ULL << SPA_MAXBLOCKSHIFT); 7755 #endif 7756 7757 arc_c = arc_c_min; 7758 /* 7759 * 32-bit fixed point fractions of metadata from total ARC size, 7760 * MRU data from all data and MRU metadata from all metadata. 7761 */ 7762 arc_meta = (1ULL << 32) / 4; /* Metadata is 25% of arc_c. */ 7763 arc_pd = (1ULL << 32) / 2; /* Data MRU is 50% of data. */ 7764 arc_pm = (1ULL << 32) / 2; /* Metadata MRU is 50% of metadata. */ 7765 7766 percent = MIN(zfs_arc_dnode_limit_percent, 100); 7767 arc_dnode_limit = arc_c_max * percent / 100; 7768 7769 /* Apply user specified tunings */ 7770 arc_tuning_update(B_TRUE); 7771 7772 /* if kmem_flags are set, lets try to use less memory */ 7773 if (kmem_debugging()) 7774 arc_c = arc_c / 2; 7775 if (arc_c < arc_c_min) 7776 arc_c = arc_c_min; 7777 7778 arc_register_hotplug(); 7779 7780 arc_state_init(); 7781 7782 buf_init(); 7783 7784 list_create(&arc_prune_list, sizeof (arc_prune_t), 7785 offsetof(arc_prune_t, p_node)); 7786 mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL); 7787 7788 arc_prune_taskq = taskq_create("arc_prune", zfs_arc_prune_task_threads, 7789 defclsyspri, 100, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC); 7790 7791 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED, 7792 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL); 7793 7794 if (arc_ksp != NULL) { 7795 arc_ksp->ks_data = &arc_stats; 7796 arc_ksp->ks_update = arc_kstat_update; 7797 kstat_install(arc_ksp); 7798 } 7799 7800 arc_state_evict_markers = 7801 arc_state_alloc_markers(arc_state_evict_marker_count); 7802 arc_evict_zthr = zthr_create_timer("arc_evict", 7803 arc_evict_cb_check, arc_evict_cb, NULL, SEC2NSEC(1), defclsyspri); 7804 arc_reap_zthr = zthr_create_timer("arc_reap", 7805 arc_reap_cb_check, arc_reap_cb, NULL, SEC2NSEC(1), minclsyspri); 7806 7807 arc_warm = B_FALSE; 7808 7809 /* 7810 * Calculate maximum amount of dirty data per pool. 7811 * 7812 * If it has been set by a module parameter, take that. 7813 * Otherwise, use a percentage of physical memory defined by 7814 * zfs_dirty_data_max_percent (default 10%) with a cap at 7815 * zfs_dirty_data_max_max (default 4G or 25% of physical memory). 7816 */ 7817 #ifdef __LP64__ 7818 if (zfs_dirty_data_max_max == 0) 7819 zfs_dirty_data_max_max = MIN(4ULL * 1024 * 1024 * 1024, 7820 allmem * zfs_dirty_data_max_max_percent / 100); 7821 #else 7822 if (zfs_dirty_data_max_max == 0) 7823 zfs_dirty_data_max_max = MIN(1ULL * 1024 * 1024 * 1024, 7824 allmem * zfs_dirty_data_max_max_percent / 100); 7825 #endif 7826 7827 if (zfs_dirty_data_max == 0) { 7828 zfs_dirty_data_max = allmem * 7829 zfs_dirty_data_max_percent / 100; 7830 zfs_dirty_data_max = MIN(zfs_dirty_data_max, 7831 zfs_dirty_data_max_max); 7832 } 7833 7834 if (zfs_wrlog_data_max == 0) { 7835 7836 /* 7837 * dp_wrlog_total is reduced for each txg at the end of 7838 * spa_sync(). However, dp_dirty_total is reduced every time 7839 * a block is written out. Thus under normal operation, 7840 * dp_wrlog_total could grow 2 times as big as 7841 * zfs_dirty_data_max. 7842 */ 7843 zfs_wrlog_data_max = zfs_dirty_data_max * 2; 7844 } 7845 } 7846 7847 void 7848 arc_fini(void) 7849 { 7850 arc_prune_t *p; 7851 7852 #ifdef _KERNEL 7853 arc_lowmem_fini(); 7854 #endif /* _KERNEL */ 7855 7856 /* Use B_TRUE to ensure *all* buffers are evicted */ 7857 arc_flush(NULL, B_TRUE); 7858 7859 if (arc_ksp != NULL) { 7860 kstat_delete(arc_ksp); 7861 arc_ksp = NULL; 7862 } 7863 7864 taskq_wait(arc_prune_taskq); 7865 taskq_destroy(arc_prune_taskq); 7866 7867 mutex_enter(&arc_prune_mtx); 7868 while ((p = list_remove_head(&arc_prune_list)) != NULL) { 7869 zfs_refcount_remove(&p->p_refcnt, &arc_prune_list); 7870 zfs_refcount_destroy(&p->p_refcnt); 7871 kmem_free(p, sizeof (*p)); 7872 } 7873 mutex_exit(&arc_prune_mtx); 7874 7875 list_destroy(&arc_prune_list); 7876 mutex_destroy(&arc_prune_mtx); 7877 7878 (void) zthr_cancel(arc_evict_zthr); 7879 (void) zthr_cancel(arc_reap_zthr); 7880 arc_state_free_markers(arc_state_evict_markers, 7881 arc_state_evict_marker_count); 7882 7883 mutex_destroy(&arc_evict_lock); 7884 list_destroy(&arc_evict_waiters); 7885 7886 /* 7887 * Free any buffers that were tagged for destruction. This needs 7888 * to occur before arc_state_fini() runs and destroys the aggsum 7889 * values which are updated when freeing scatter ABDs. 7890 */ 7891 l2arc_do_free_on_write(); 7892 7893 /* 7894 * buf_fini() must proceed arc_state_fini() because buf_fin() may 7895 * trigger the release of kmem magazines, which can callback to 7896 * arc_space_return() which accesses aggsums freed in act_state_fini(). 7897 */ 7898 buf_fini(); 7899 arc_state_fini(); 7900 7901 arc_unregister_hotplug(); 7902 7903 /* 7904 * We destroy the zthrs after all the ARC state has been 7905 * torn down to avoid the case of them receiving any 7906 * wakeup() signals after they are destroyed. 7907 */ 7908 zthr_destroy(arc_evict_zthr); 7909 zthr_destroy(arc_reap_zthr); 7910 7911 ASSERT0(arc_loaned_bytes); 7912 } 7913 7914 /* 7915 * Level 2 ARC 7916 * 7917 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk. 7918 * It uses dedicated storage devices to hold cached data, which are populated 7919 * using large infrequent writes. The main role of this cache is to boost 7920 * the performance of random read workloads. The intended L2ARC devices 7921 * include short-stroked disks, solid state disks, and other media with 7922 * substantially faster read latency than disk. 7923 * 7924 * +-----------------------+ 7925 * | ARC | 7926 * +-----------------------+ 7927 * | ^ ^ 7928 * | | | 7929 * l2arc_feed_thread() arc_read() 7930 * | | | 7931 * | l2arc read | 7932 * V | | 7933 * +---------------+ | 7934 * | L2ARC | | 7935 * +---------------+ | 7936 * | ^ | 7937 * l2arc_write() | | 7938 * | | | 7939 * V | | 7940 * +-------+ +-------+ 7941 * | vdev | | vdev | 7942 * | cache | | cache | 7943 * +-------+ +-------+ 7944 * +=========+ .-----. 7945 * : L2ARC : |-_____-| 7946 * : devices : | Disks | 7947 * +=========+ `-_____-' 7948 * 7949 * Read requests are satisfied from the following sources, in order: 7950 * 7951 * 1) ARC 7952 * 2) vdev cache of L2ARC devices 7953 * 3) L2ARC devices 7954 * 4) vdev cache of disks 7955 * 5) disks 7956 * 7957 * Some L2ARC device types exhibit extremely slow write performance. 7958 * To accommodate for this there are some significant differences between 7959 * the L2ARC and traditional cache design: 7960 * 7961 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from 7962 * the ARC behave as usual, freeing buffers and placing headers on ghost 7963 * lists. The ARC does not send buffers to the L2ARC during eviction as 7964 * this would add inflated write latencies for all ARC memory pressure. 7965 * 7966 * 2. The L2ARC attempts to cache data from the ARC before it is evicted. 7967 * It does this by periodically scanning buffers from the eviction-end of 7968 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are 7969 * not already there. It scans until a headroom of buffers is satisfied, 7970 * which itself is a buffer for ARC eviction. If a compressible buffer is 7971 * found during scanning and selected for writing to an L2ARC device, we 7972 * temporarily boost scanning headroom during the next scan cycle to make 7973 * sure we adapt to compression effects (which might significantly reduce 7974 * the data volume we write to L2ARC). The thread that does this is 7975 * l2arc_feed_thread(), illustrated below; example sizes are included to 7976 * provide a better sense of ratio than this diagram: 7977 * 7978 * head --> tail 7979 * +---------------------+----------+ 7980 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC 7981 * +---------------------+----------+ | o L2ARC eligible 7982 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer 7983 * +---------------------+----------+ | 7984 * 15.9 Gbytes ^ 32 Mbytes | 7985 * headroom | 7986 * l2arc_feed_thread() 7987 * | 7988 * l2arc write hand <--[oooo]--' 7989 * | 8 Mbyte 7990 * | write max 7991 * V 7992 * +==============================+ 7993 * L2ARC dev |####|#|###|###| |####| ... | 7994 * +==============================+ 7995 * 32 Gbytes 7996 * 7997 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of 7998 * evicted, then the L2ARC has cached a buffer much sooner than it probably 7999 * needed to, potentially wasting L2ARC device bandwidth and storage. It is 8000 * safe to say that this is an uncommon case, since buffers at the end of 8001 * the ARC lists have moved there due to inactivity. 8002 * 8003 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom, 8004 * then the L2ARC simply misses copying some buffers. This serves as a 8005 * pressure valve to prevent heavy read workloads from both stalling the ARC 8006 * with waits and clogging the L2ARC with writes. This also helps prevent 8007 * the potential for the L2ARC to churn if it attempts to cache content too 8008 * quickly, such as during backups of the entire pool. 8009 * 8010 * 5. After system boot and before the ARC has filled main memory, there are 8011 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru 8012 * lists can remain mostly static. Instead of searching from tail of these 8013 * lists as pictured, the l2arc_feed_thread() will search from the list heads 8014 * for eligible buffers, greatly increasing its chance of finding them. 8015 * 8016 * The L2ARC device write speed is also boosted during this time so that 8017 * the L2ARC warms up faster. Since there have been no ARC evictions yet, 8018 * there are no L2ARC reads, and no fear of degrading read performance 8019 * through increased writes. 8020 * 8021 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that 8022 * the vdev queue can aggregate them into larger and fewer writes. Each 8023 * device is written to in a rotor fashion, sweeping writes through 8024 * available space then repeating. 8025 * 8026 * 7. The L2ARC does not store dirty content. It never needs to flush 8027 * write buffers back to disk based storage. 8028 * 8029 * 8. If an ARC buffer is written (and dirtied) which also exists in the 8030 * L2ARC, the now stale L2ARC buffer is immediately dropped. 8031 * 8032 * The performance of the L2ARC can be tweaked by a number of tunables, which 8033 * may be necessary for different workloads: 8034 * 8035 * l2arc_write_max max write bytes per interval 8036 * l2arc_write_boost extra write bytes during device warmup 8037 * l2arc_noprefetch skip caching prefetched buffers 8038 * l2arc_headroom number of max device writes to precache 8039 * l2arc_headroom_boost when we find compressed buffers during ARC 8040 * scanning, we multiply headroom by this 8041 * percentage factor for the next scan cycle, 8042 * since more compressed buffers are likely to 8043 * be present 8044 * l2arc_feed_secs seconds between L2ARC writing 8045 * 8046 * Tunables may be removed or added as future performance improvements are 8047 * integrated, and also may become zpool properties. 8048 * 8049 * There are three key functions that control how the L2ARC warms up: 8050 * 8051 * l2arc_write_eligible() check if a buffer is eligible to cache 8052 * l2arc_write_size() calculate how much to write 8053 * l2arc_write_interval() calculate sleep delay between writes 8054 * 8055 * These three functions determine what to write, how much, and how quickly 8056 * to send writes. 8057 * 8058 * L2ARC persistence: 8059 * 8060 * When writing buffers to L2ARC, we periodically add some metadata to 8061 * make sure we can pick them up after reboot, thus dramatically reducing 8062 * the impact that any downtime has on the performance of storage systems 8063 * with large caches. 8064 * 8065 * The implementation works fairly simply by integrating the following two 8066 * modifications: 8067 * 8068 * *) When writing to the L2ARC, we occasionally write a "l2arc log block", 8069 * which is an additional piece of metadata which describes what's been 8070 * written. This allows us to rebuild the arc_buf_hdr_t structures of the 8071 * main ARC buffers. There are 2 linked-lists of log blocks headed by 8072 * dh_start_lbps[2]. We alternate which chain we append to, so they are 8073 * time-wise and offset-wise interleaved, but that is an optimization rather 8074 * than for correctness. The log block also includes a pointer to the 8075 * previous block in its chain. 8076 * 8077 * *) We reserve SPA_MINBLOCKSIZE of space at the start of each L2ARC device 8078 * for our header bookkeeping purposes. This contains a device header, 8079 * which contains our top-level reference structures. We update it each 8080 * time we write a new log block, so that we're able to locate it in the 8081 * L2ARC device. If this write results in an inconsistent device header 8082 * (e.g. due to power failure), we detect this by verifying the header's 8083 * checksum and simply fail to reconstruct the L2ARC after reboot. 8084 * 8085 * Implementation diagram: 8086 * 8087 * +=== L2ARC device (not to scale) ======================================+ 8088 * | ___two newest log block pointers__.__________ | 8089 * | / \dh_start_lbps[1] | 8090 * | / \ \dh_start_lbps[0]| 8091 * |.___/__. V V | 8092 * ||L2 dev|....|lb |bufs |lb |bufs |lb |bufs |lb |bufs |lb |---(empty)---| 8093 * || hdr| ^ /^ /^ / / | 8094 * |+------+ ...--\-------/ \-----/--\------/ / | 8095 * | \--------------/ \--------------/ | 8096 * +======================================================================+ 8097 * 8098 * As can be seen on the diagram, rather than using a simple linked list, 8099 * we use a pair of linked lists with alternating elements. This is a 8100 * performance enhancement due to the fact that we only find out the 8101 * address of the next log block access once the current block has been 8102 * completely read in. Obviously, this hurts performance, because we'd be 8103 * keeping the device's I/O queue at only a 1 operation deep, thus 8104 * incurring a large amount of I/O round-trip latency. Having two lists 8105 * allows us to fetch two log blocks ahead of where we are currently 8106 * rebuilding L2ARC buffers. 8107 * 8108 * On-device data structures: 8109 * 8110 * L2ARC device header: l2arc_dev_hdr_phys_t 8111 * L2ARC log block: l2arc_log_blk_phys_t 8112 * 8113 * L2ARC reconstruction: 8114 * 8115 * When writing data, we simply write in the standard rotary fashion, 8116 * evicting buffers as we go and simply writing new data over them (writing 8117 * a new log block every now and then). This obviously means that once we 8118 * loop around the end of the device, we will start cutting into an already 8119 * committed log block (and its referenced data buffers), like so: 8120 * 8121 * current write head__ __old tail 8122 * \ / 8123 * V V 8124 * <--|bufs |lb |bufs |lb | |bufs |lb |bufs |lb |--> 8125 * ^ ^^^^^^^^^___________________________________ 8126 * | \ 8127 * <<nextwrite>> may overwrite this blk and/or its bufs --' 8128 * 8129 * When importing the pool, we detect this situation and use it to stop 8130 * our scanning process (see l2arc_rebuild). 8131 * 8132 * There is one significant caveat to consider when rebuilding ARC contents 8133 * from an L2ARC device: what about invalidated buffers? Given the above 8134 * construction, we cannot update blocks which we've already written to amend 8135 * them to remove buffers which were invalidated. Thus, during reconstruction, 8136 * we might be populating the cache with buffers for data that's not on the 8137 * main pool anymore, or may have been overwritten! 8138 * 8139 * As it turns out, this isn't a problem. Every arc_read request includes 8140 * both the DVA and, crucially, the birth TXG of the BP the caller is 8141 * looking for. So even if the cache were populated by completely rotten 8142 * blocks for data that had been long deleted and/or overwritten, we'll 8143 * never actually return bad data from the cache, since the DVA with the 8144 * birth TXG uniquely identify a block in space and time - once created, 8145 * a block is immutable on disk. The worst thing we have done is wasted 8146 * some time and memory at l2arc rebuild to reconstruct outdated ARC 8147 * entries that will get dropped from the l2arc as it is being updated 8148 * with new blocks. 8149 * 8150 * L2ARC buffers that have been evicted by l2arc_evict() ahead of the write 8151 * hand are not restored. This is done by saving the offset (in bytes) 8152 * l2arc_evict() has evicted to in the L2ARC device header and taking it 8153 * into account when restoring buffers. 8154 */ 8155 8156 static boolean_t 8157 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr) 8158 { 8159 /* 8160 * A buffer is *not* eligible for the L2ARC if it: 8161 * 1. belongs to a different spa. 8162 * 2. is already cached on the L2ARC. 8163 * 3. has an I/O in progress (it may be an incomplete read). 8164 * 4. is flagged not eligible (zfs property). 8165 */ 8166 if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) || 8167 HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr)) 8168 return (B_FALSE); 8169 8170 return (B_TRUE); 8171 } 8172 8173 static uint64_t 8174 l2arc_write_size(l2arc_dev_t *dev) 8175 { 8176 uint64_t size; 8177 8178 /* 8179 * Make sure our globals have meaningful values in case the user 8180 * altered them. 8181 */ 8182 size = l2arc_write_max; 8183 if (size == 0) { 8184 cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must " 8185 "be greater than zero, resetting it to the default (%d)", 8186 L2ARC_WRITE_SIZE); 8187 size = l2arc_write_max = L2ARC_WRITE_SIZE; 8188 } 8189 8190 if (arc_warm == B_FALSE) 8191 size += l2arc_write_boost; 8192 8193 /* We need to add in the worst case scenario of log block overhead. */ 8194 size += l2arc_log_blk_overhead(size, dev); 8195 if (dev->l2ad_vdev->vdev_has_trim && l2arc_trim_ahead > 0) { 8196 /* 8197 * Trim ahead of the write size 64MB or (l2arc_trim_ahead/100) 8198 * times the writesize, whichever is greater. 8199 */ 8200 size += MAX(64 * 1024 * 1024, 8201 (size * l2arc_trim_ahead) / 100); 8202 } 8203 8204 /* 8205 * Make sure the write size does not exceed the size of the cache 8206 * device. This is important in l2arc_evict(), otherwise infinite 8207 * iteration can occur. 8208 */ 8209 if (size > dev->l2ad_end - dev->l2ad_start) { 8210 cmn_err(CE_NOTE, "l2arc_write_max or l2arc_write_boost " 8211 "plus the overhead of log blocks (persistent L2ARC, " 8212 "%llu bytes) exceeds the size of the cache device " 8213 "(guid %llu), resetting them to the default (%d)", 8214 (u_longlong_t)l2arc_log_blk_overhead(size, dev), 8215 (u_longlong_t)dev->l2ad_vdev->vdev_guid, L2ARC_WRITE_SIZE); 8216 8217 size = l2arc_write_max = l2arc_write_boost = L2ARC_WRITE_SIZE; 8218 8219 if (l2arc_trim_ahead > 1) { 8220 cmn_err(CE_NOTE, "l2arc_trim_ahead set to 1"); 8221 l2arc_trim_ahead = 1; 8222 } 8223 8224 if (arc_warm == B_FALSE) 8225 size += l2arc_write_boost; 8226 8227 size += l2arc_log_blk_overhead(size, dev); 8228 if (dev->l2ad_vdev->vdev_has_trim && l2arc_trim_ahead > 0) { 8229 size += MAX(64 * 1024 * 1024, 8230 (size * l2arc_trim_ahead) / 100); 8231 } 8232 } 8233 8234 return (size); 8235 8236 } 8237 8238 static clock_t 8239 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote) 8240 { 8241 clock_t interval, next, now; 8242 8243 /* 8244 * If the ARC lists are busy, increase our write rate; if the 8245 * lists are stale, idle back. This is achieved by checking 8246 * how much we previously wrote - if it was more than half of 8247 * what we wanted, schedule the next write much sooner. 8248 */ 8249 if (l2arc_feed_again && wrote > (wanted / 2)) 8250 interval = (hz * l2arc_feed_min_ms) / 1000; 8251 else 8252 interval = hz * l2arc_feed_secs; 8253 8254 now = ddi_get_lbolt(); 8255 next = MAX(now, MIN(now + interval, began + interval)); 8256 8257 return (next); 8258 } 8259 8260 /* 8261 * Cycle through L2ARC devices. This is how L2ARC load balances. 8262 * If a device is returned, this also returns holding the spa config lock. 8263 */ 8264 static l2arc_dev_t * 8265 l2arc_dev_get_next(void) 8266 { 8267 l2arc_dev_t *first, *next = NULL; 8268 8269 /* 8270 * Lock out the removal of spas (spa_namespace_lock), then removal 8271 * of cache devices (l2arc_dev_mtx). Once a device has been selected, 8272 * both locks will be dropped and a spa config lock held instead. 8273 */ 8274 mutex_enter(&spa_namespace_lock); 8275 mutex_enter(&l2arc_dev_mtx); 8276 8277 /* if there are no vdevs, there is nothing to do */ 8278 if (l2arc_ndev == 0) 8279 goto out; 8280 8281 first = NULL; 8282 next = l2arc_dev_last; 8283 do { 8284 /* loop around the list looking for a non-faulted vdev */ 8285 if (next == NULL) { 8286 next = list_head(l2arc_dev_list); 8287 } else { 8288 next = list_next(l2arc_dev_list, next); 8289 if (next == NULL) 8290 next = list_head(l2arc_dev_list); 8291 } 8292 8293 /* if we have come back to the start, bail out */ 8294 if (first == NULL) 8295 first = next; 8296 else if (next == first) 8297 break; 8298 8299 ASSERT3P(next, !=, NULL); 8300 } while (vdev_is_dead(next->l2ad_vdev) || next->l2ad_rebuild || 8301 next->l2ad_trim_all); 8302 8303 /* if we were unable to find any usable vdevs, return NULL */ 8304 if (vdev_is_dead(next->l2ad_vdev) || next->l2ad_rebuild || 8305 next->l2ad_trim_all) 8306 next = NULL; 8307 8308 l2arc_dev_last = next; 8309 8310 out: 8311 mutex_exit(&l2arc_dev_mtx); 8312 8313 /* 8314 * Grab the config lock to prevent the 'next' device from being 8315 * removed while we are writing to it. 8316 */ 8317 if (next != NULL) 8318 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER); 8319 mutex_exit(&spa_namespace_lock); 8320 8321 return (next); 8322 } 8323 8324 /* 8325 * Free buffers that were tagged for destruction. 8326 */ 8327 static void 8328 l2arc_do_free_on_write(void) 8329 { 8330 l2arc_data_free_t *df; 8331 8332 mutex_enter(&l2arc_free_on_write_mtx); 8333 while ((df = list_remove_head(l2arc_free_on_write)) != NULL) { 8334 ASSERT3P(df->l2df_abd, !=, NULL); 8335 abd_free(df->l2df_abd); 8336 kmem_free(df, sizeof (l2arc_data_free_t)); 8337 } 8338 mutex_exit(&l2arc_free_on_write_mtx); 8339 } 8340 8341 /* 8342 * A write to a cache device has completed. Update all headers to allow 8343 * reads from these buffers to begin. 8344 */ 8345 static void 8346 l2arc_write_done(zio_t *zio) 8347 { 8348 l2arc_write_callback_t *cb; 8349 l2arc_lb_abd_buf_t *abd_buf; 8350 l2arc_lb_ptr_buf_t *lb_ptr_buf; 8351 l2arc_dev_t *dev; 8352 l2arc_dev_hdr_phys_t *l2dhdr; 8353 list_t *buflist; 8354 arc_buf_hdr_t *head, *hdr, *hdr_prev; 8355 kmutex_t *hash_lock; 8356 int64_t bytes_dropped = 0; 8357 8358 cb = zio->io_private; 8359 ASSERT3P(cb, !=, NULL); 8360 dev = cb->l2wcb_dev; 8361 l2dhdr = dev->l2ad_dev_hdr; 8362 ASSERT3P(dev, !=, NULL); 8363 head = cb->l2wcb_head; 8364 ASSERT3P(head, !=, NULL); 8365 buflist = &dev->l2ad_buflist; 8366 ASSERT3P(buflist, !=, NULL); 8367 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio, 8368 l2arc_write_callback_t *, cb); 8369 8370 /* 8371 * All writes completed, or an error was hit. 8372 */ 8373 top: 8374 mutex_enter(&dev->l2ad_mtx); 8375 for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) { 8376 hdr_prev = list_prev(buflist, hdr); 8377 8378 hash_lock = HDR_LOCK(hdr); 8379 8380 /* 8381 * We cannot use mutex_enter or else we can deadlock 8382 * with l2arc_write_buffers (due to swapping the order 8383 * the hash lock and l2ad_mtx are taken). 8384 */ 8385 if (!mutex_tryenter(hash_lock)) { 8386 /* 8387 * Missed the hash lock. We must retry so we 8388 * don't leave the ARC_FLAG_L2_WRITING bit set. 8389 */ 8390 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry); 8391 8392 /* 8393 * We don't want to rescan the headers we've 8394 * already marked as having been written out, so 8395 * we reinsert the head node so we can pick up 8396 * where we left off. 8397 */ 8398 list_remove(buflist, head); 8399 list_insert_after(buflist, hdr, head); 8400 8401 mutex_exit(&dev->l2ad_mtx); 8402 8403 /* 8404 * We wait for the hash lock to become available 8405 * to try and prevent busy waiting, and increase 8406 * the chance we'll be able to acquire the lock 8407 * the next time around. 8408 */ 8409 mutex_enter(hash_lock); 8410 mutex_exit(hash_lock); 8411 goto top; 8412 } 8413 8414 /* 8415 * We could not have been moved into the arc_l2c_only 8416 * state while in-flight due to our ARC_FLAG_L2_WRITING 8417 * bit being set. Let's just ensure that's being enforced. 8418 */ 8419 ASSERT(HDR_HAS_L1HDR(hdr)); 8420 8421 /* 8422 * Skipped - drop L2ARC entry and mark the header as no 8423 * longer L2 eligibile. 8424 */ 8425 if (zio->io_error != 0) { 8426 /* 8427 * Error - drop L2ARC entry. 8428 */ 8429 list_remove(buflist, hdr); 8430 arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR); 8431 8432 uint64_t psize = HDR_GET_PSIZE(hdr); 8433 l2arc_hdr_arcstats_decrement(hdr); 8434 8435 bytes_dropped += 8436 vdev_psize_to_asize(dev->l2ad_vdev, psize); 8437 (void) zfs_refcount_remove_many(&dev->l2ad_alloc, 8438 arc_hdr_size(hdr), hdr); 8439 } 8440 8441 /* 8442 * Allow ARC to begin reads and ghost list evictions to 8443 * this L2ARC entry. 8444 */ 8445 arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING); 8446 8447 mutex_exit(hash_lock); 8448 } 8449 8450 /* 8451 * Free the allocated abd buffers for writing the log blocks. 8452 * If the zio failed reclaim the allocated space and remove the 8453 * pointers to these log blocks from the log block pointer list 8454 * of the L2ARC device. 8455 */ 8456 while ((abd_buf = list_remove_tail(&cb->l2wcb_abd_list)) != NULL) { 8457 abd_free(abd_buf->abd); 8458 zio_buf_free(abd_buf, sizeof (*abd_buf)); 8459 if (zio->io_error != 0) { 8460 lb_ptr_buf = list_remove_head(&dev->l2ad_lbptr_list); 8461 /* 8462 * L2BLK_GET_PSIZE returns aligned size for log 8463 * blocks. 8464 */ 8465 uint64_t asize = 8466 L2BLK_GET_PSIZE((lb_ptr_buf->lb_ptr)->lbp_prop); 8467 bytes_dropped += asize; 8468 ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize); 8469 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count); 8470 zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize, 8471 lb_ptr_buf); 8472 zfs_refcount_remove(&dev->l2ad_lb_count, lb_ptr_buf); 8473 kmem_free(lb_ptr_buf->lb_ptr, 8474 sizeof (l2arc_log_blkptr_t)); 8475 kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t)); 8476 } 8477 } 8478 list_destroy(&cb->l2wcb_abd_list); 8479 8480 if (zio->io_error != 0) { 8481 ARCSTAT_BUMP(arcstat_l2_writes_error); 8482 8483 /* 8484 * Restore the lbps array in the header to its previous state. 8485 * If the list of log block pointers is empty, zero out the 8486 * log block pointers in the device header. 8487 */ 8488 lb_ptr_buf = list_head(&dev->l2ad_lbptr_list); 8489 for (int i = 0; i < 2; i++) { 8490 if (lb_ptr_buf == NULL) { 8491 /* 8492 * If the list is empty zero out the device 8493 * header. Otherwise zero out the second log 8494 * block pointer in the header. 8495 */ 8496 if (i == 0) { 8497 memset(l2dhdr, 0, 8498 dev->l2ad_dev_hdr_asize); 8499 } else { 8500 memset(&l2dhdr->dh_start_lbps[i], 0, 8501 sizeof (l2arc_log_blkptr_t)); 8502 } 8503 break; 8504 } 8505 memcpy(&l2dhdr->dh_start_lbps[i], lb_ptr_buf->lb_ptr, 8506 sizeof (l2arc_log_blkptr_t)); 8507 lb_ptr_buf = list_next(&dev->l2ad_lbptr_list, 8508 lb_ptr_buf); 8509 } 8510 } 8511 8512 ARCSTAT_BUMP(arcstat_l2_writes_done); 8513 list_remove(buflist, head); 8514 ASSERT(!HDR_HAS_L1HDR(head)); 8515 kmem_cache_free(hdr_l2only_cache, head); 8516 mutex_exit(&dev->l2ad_mtx); 8517 8518 ASSERT(dev->l2ad_vdev != NULL); 8519 vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0); 8520 8521 l2arc_do_free_on_write(); 8522 8523 kmem_free(cb, sizeof (l2arc_write_callback_t)); 8524 } 8525 8526 static int 8527 l2arc_untransform(zio_t *zio, l2arc_read_callback_t *cb) 8528 { 8529 int ret; 8530 spa_t *spa = zio->io_spa; 8531 arc_buf_hdr_t *hdr = cb->l2rcb_hdr; 8532 blkptr_t *bp = zio->io_bp; 8533 uint8_t salt[ZIO_DATA_SALT_LEN]; 8534 uint8_t iv[ZIO_DATA_IV_LEN]; 8535 uint8_t mac[ZIO_DATA_MAC_LEN]; 8536 boolean_t no_crypt = B_FALSE; 8537 8538 /* 8539 * ZIL data is never be written to the L2ARC, so we don't need 8540 * special handling for its unique MAC storage. 8541 */ 8542 ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG); 8543 ASSERT(MUTEX_HELD(HDR_LOCK(hdr))); 8544 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 8545 8546 /* 8547 * If the data was encrypted, decrypt it now. Note that 8548 * we must check the bp here and not the hdr, since the 8549 * hdr does not have its encryption parameters updated 8550 * until arc_read_done(). 8551 */ 8552 if (BP_IS_ENCRYPTED(bp)) { 8553 abd_t *eabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, 8554 ARC_HDR_USE_RESERVE); 8555 8556 zio_crypt_decode_params_bp(bp, salt, iv); 8557 zio_crypt_decode_mac_bp(bp, mac); 8558 8559 ret = spa_do_crypt_abd(B_FALSE, spa, &cb->l2rcb_zb, 8560 BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), 8561 salt, iv, mac, HDR_GET_PSIZE(hdr), eabd, 8562 hdr->b_l1hdr.b_pabd, &no_crypt); 8563 if (ret != 0) { 8564 arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); 8565 goto error; 8566 } 8567 8568 /* 8569 * If we actually performed decryption, replace b_pabd 8570 * with the decrypted data. Otherwise we can just throw 8571 * our decryption buffer away. 8572 */ 8573 if (!no_crypt) { 8574 arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, 8575 arc_hdr_size(hdr), hdr); 8576 hdr->b_l1hdr.b_pabd = eabd; 8577 zio->io_abd = eabd; 8578 } else { 8579 arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr); 8580 } 8581 } 8582 8583 /* 8584 * If the L2ARC block was compressed, but ARC compression 8585 * is disabled we decompress the data into a new buffer and 8586 * replace the existing data. 8587 */ 8588 if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 8589 !HDR_COMPRESSION_ENABLED(hdr)) { 8590 abd_t *cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, 8591 ARC_HDR_USE_RESERVE); 8592 void *tmp = abd_borrow_buf(cabd, arc_hdr_size(hdr)); 8593 8594 ret = zio_decompress_data(HDR_GET_COMPRESS(hdr), 8595 hdr->b_l1hdr.b_pabd, tmp, HDR_GET_PSIZE(hdr), 8596 HDR_GET_LSIZE(hdr), &hdr->b_complevel); 8597 if (ret != 0) { 8598 abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); 8599 arc_free_data_abd(hdr, cabd, arc_hdr_size(hdr), hdr); 8600 goto error; 8601 } 8602 8603 abd_return_buf_copy(cabd, tmp, arc_hdr_size(hdr)); 8604 arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, 8605 arc_hdr_size(hdr), hdr); 8606 hdr->b_l1hdr.b_pabd = cabd; 8607 zio->io_abd = cabd; 8608 zio->io_size = HDR_GET_LSIZE(hdr); 8609 } 8610 8611 return (0); 8612 8613 error: 8614 return (ret); 8615 } 8616 8617 8618 /* 8619 * A read to a cache device completed. Validate buffer contents before 8620 * handing over to the regular ARC routines. 8621 */ 8622 static void 8623 l2arc_read_done(zio_t *zio) 8624 { 8625 int tfm_error = 0; 8626 l2arc_read_callback_t *cb = zio->io_private; 8627 arc_buf_hdr_t *hdr; 8628 kmutex_t *hash_lock; 8629 boolean_t valid_cksum; 8630 boolean_t using_rdata = (BP_IS_ENCRYPTED(&cb->l2rcb_bp) && 8631 (cb->l2rcb_flags & ZIO_FLAG_RAW_ENCRYPT)); 8632 8633 ASSERT3P(zio->io_vd, !=, NULL); 8634 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE); 8635 8636 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd); 8637 8638 ASSERT3P(cb, !=, NULL); 8639 hdr = cb->l2rcb_hdr; 8640 ASSERT3P(hdr, !=, NULL); 8641 8642 hash_lock = HDR_LOCK(hdr); 8643 mutex_enter(hash_lock); 8644 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr)); 8645 8646 /* 8647 * If the data was read into a temporary buffer, 8648 * move it and free the buffer. 8649 */ 8650 if (cb->l2rcb_abd != NULL) { 8651 ASSERT3U(arc_hdr_size(hdr), <, zio->io_size); 8652 if (zio->io_error == 0) { 8653 if (using_rdata) { 8654 abd_copy(hdr->b_crypt_hdr.b_rabd, 8655 cb->l2rcb_abd, arc_hdr_size(hdr)); 8656 } else { 8657 abd_copy(hdr->b_l1hdr.b_pabd, 8658 cb->l2rcb_abd, arc_hdr_size(hdr)); 8659 } 8660 } 8661 8662 /* 8663 * The following must be done regardless of whether 8664 * there was an error: 8665 * - free the temporary buffer 8666 * - point zio to the real ARC buffer 8667 * - set zio size accordingly 8668 * These are required because zio is either re-used for 8669 * an I/O of the block in the case of the error 8670 * or the zio is passed to arc_read_done() and it 8671 * needs real data. 8672 */ 8673 abd_free(cb->l2rcb_abd); 8674 zio->io_size = zio->io_orig_size = arc_hdr_size(hdr); 8675 8676 if (using_rdata) { 8677 ASSERT(HDR_HAS_RABD(hdr)); 8678 zio->io_abd = zio->io_orig_abd = 8679 hdr->b_crypt_hdr.b_rabd; 8680 } else { 8681 ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL); 8682 zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd; 8683 } 8684 } 8685 8686 ASSERT3P(zio->io_abd, !=, NULL); 8687 8688 /* 8689 * Check this survived the L2ARC journey. 8690 */ 8691 ASSERT(zio->io_abd == hdr->b_l1hdr.b_pabd || 8692 (HDR_HAS_RABD(hdr) && zio->io_abd == hdr->b_crypt_hdr.b_rabd)); 8693 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */ 8694 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */ 8695 zio->io_prop.zp_complevel = hdr->b_complevel; 8696 8697 valid_cksum = arc_cksum_is_equal(hdr, zio); 8698 8699 /* 8700 * b_rabd will always match the data as it exists on disk if it is 8701 * being used. Therefore if we are reading into b_rabd we do not 8702 * attempt to untransform the data. 8703 */ 8704 if (valid_cksum && !using_rdata) 8705 tfm_error = l2arc_untransform(zio, cb); 8706 8707 if (valid_cksum && tfm_error == 0 && zio->io_error == 0 && 8708 !HDR_L2_EVICTED(hdr)) { 8709 mutex_exit(hash_lock); 8710 zio->io_private = hdr; 8711 arc_read_done(zio); 8712 } else { 8713 /* 8714 * Buffer didn't survive caching. Increment stats and 8715 * reissue to the original storage device. 8716 */ 8717 if (zio->io_error != 0) { 8718 ARCSTAT_BUMP(arcstat_l2_io_error); 8719 } else { 8720 zio->io_error = SET_ERROR(EIO); 8721 } 8722 if (!valid_cksum || tfm_error != 0) 8723 ARCSTAT_BUMP(arcstat_l2_cksum_bad); 8724 8725 /* 8726 * If there's no waiter, issue an async i/o to the primary 8727 * storage now. If there *is* a waiter, the caller must 8728 * issue the i/o in a context where it's OK to block. 8729 */ 8730 if (zio->io_waiter == NULL) { 8731 zio_t *pio = zio_unique_parent(zio); 8732 void *abd = (using_rdata) ? 8733 hdr->b_crypt_hdr.b_rabd : hdr->b_l1hdr.b_pabd; 8734 8735 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL); 8736 8737 zio = zio_read(pio, zio->io_spa, zio->io_bp, 8738 abd, zio->io_size, arc_read_done, 8739 hdr, zio->io_priority, cb->l2rcb_flags, 8740 &cb->l2rcb_zb); 8741 8742 /* 8743 * Original ZIO will be freed, so we need to update 8744 * ARC header with the new ZIO pointer to be used 8745 * by zio_change_priority() in arc_read(). 8746 */ 8747 for (struct arc_callback *acb = hdr->b_l1hdr.b_acb; 8748 acb != NULL; acb = acb->acb_next) 8749 acb->acb_zio_head = zio; 8750 8751 mutex_exit(hash_lock); 8752 zio_nowait(zio); 8753 } else { 8754 mutex_exit(hash_lock); 8755 } 8756 } 8757 8758 kmem_free(cb, sizeof (l2arc_read_callback_t)); 8759 } 8760 8761 /* 8762 * This is the list priority from which the L2ARC will search for pages to 8763 * cache. This is used within loops (0..3) to cycle through lists in the 8764 * desired order. This order can have a significant effect on cache 8765 * performance. 8766 * 8767 * Currently the metadata lists are hit first, MFU then MRU, followed by 8768 * the data lists. This function returns a locked list, and also returns 8769 * the lock pointer. 8770 */ 8771 static multilist_sublist_t * 8772 l2arc_sublist_lock(int list_num) 8773 { 8774 multilist_t *ml = NULL; 8775 unsigned int idx; 8776 8777 ASSERT(list_num >= 0 && list_num < L2ARC_FEED_TYPES); 8778 8779 switch (list_num) { 8780 case 0: 8781 ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA]; 8782 break; 8783 case 1: 8784 ml = &arc_mru->arcs_list[ARC_BUFC_METADATA]; 8785 break; 8786 case 2: 8787 ml = &arc_mfu->arcs_list[ARC_BUFC_DATA]; 8788 break; 8789 case 3: 8790 ml = &arc_mru->arcs_list[ARC_BUFC_DATA]; 8791 break; 8792 default: 8793 return (NULL); 8794 } 8795 8796 /* 8797 * Return a randomly-selected sublist. This is acceptable 8798 * because the caller feeds only a little bit of data for each 8799 * call (8MB). Subsequent calls will result in different 8800 * sublists being selected. 8801 */ 8802 idx = multilist_get_random_index(ml); 8803 return (multilist_sublist_lock(ml, idx)); 8804 } 8805 8806 /* 8807 * Calculates the maximum overhead of L2ARC metadata log blocks for a given 8808 * L2ARC write size. l2arc_evict and l2arc_write_size need to include this 8809 * overhead in processing to make sure there is enough headroom available 8810 * when writing buffers. 8811 */ 8812 static inline uint64_t 8813 l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev) 8814 { 8815 if (dev->l2ad_log_entries == 0) { 8816 return (0); 8817 } else { 8818 uint64_t log_entries = write_sz >> SPA_MINBLOCKSHIFT; 8819 8820 uint64_t log_blocks = (log_entries + 8821 dev->l2ad_log_entries - 1) / 8822 dev->l2ad_log_entries; 8823 8824 return (vdev_psize_to_asize(dev->l2ad_vdev, 8825 sizeof (l2arc_log_blk_phys_t)) * log_blocks); 8826 } 8827 } 8828 8829 /* 8830 * Evict buffers from the device write hand to the distance specified in 8831 * bytes. This distance may span populated buffers, it may span nothing. 8832 * This is clearing a region on the L2ARC device ready for writing. 8833 * If the 'all' boolean is set, every buffer is evicted. 8834 */ 8835 static void 8836 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all) 8837 { 8838 list_t *buflist; 8839 arc_buf_hdr_t *hdr, *hdr_prev; 8840 kmutex_t *hash_lock; 8841 uint64_t taddr; 8842 l2arc_lb_ptr_buf_t *lb_ptr_buf, *lb_ptr_buf_prev; 8843 vdev_t *vd = dev->l2ad_vdev; 8844 boolean_t rerun; 8845 8846 buflist = &dev->l2ad_buflist; 8847 8848 top: 8849 rerun = B_FALSE; 8850 if (dev->l2ad_hand + distance > dev->l2ad_end) { 8851 /* 8852 * When there is no space to accommodate upcoming writes, 8853 * evict to the end. Then bump the write and evict hands 8854 * to the start and iterate. This iteration does not 8855 * happen indefinitely as we make sure in 8856 * l2arc_write_size() that when the write hand is reset, 8857 * the write size does not exceed the end of the device. 8858 */ 8859 rerun = B_TRUE; 8860 taddr = dev->l2ad_end; 8861 } else { 8862 taddr = dev->l2ad_hand + distance; 8863 } 8864 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist, 8865 uint64_t, taddr, boolean_t, all); 8866 8867 if (!all) { 8868 /* 8869 * This check has to be placed after deciding whether to 8870 * iterate (rerun). 8871 */ 8872 if (dev->l2ad_first) { 8873 /* 8874 * This is the first sweep through the device. There is 8875 * nothing to evict. We have already trimmmed the 8876 * whole device. 8877 */ 8878 goto out; 8879 } else { 8880 /* 8881 * Trim the space to be evicted. 8882 */ 8883 if (vd->vdev_has_trim && dev->l2ad_evict < taddr && 8884 l2arc_trim_ahead > 0) { 8885 /* 8886 * We have to drop the spa_config lock because 8887 * vdev_trim_range() will acquire it. 8888 * l2ad_evict already accounts for the label 8889 * size. To prevent vdev_trim_ranges() from 8890 * adding it again, we subtract it from 8891 * l2ad_evict. 8892 */ 8893 spa_config_exit(dev->l2ad_spa, SCL_L2ARC, dev); 8894 vdev_trim_simple(vd, 8895 dev->l2ad_evict - VDEV_LABEL_START_SIZE, 8896 taddr - dev->l2ad_evict); 8897 spa_config_enter(dev->l2ad_spa, SCL_L2ARC, dev, 8898 RW_READER); 8899 } 8900 8901 /* 8902 * When rebuilding L2ARC we retrieve the evict hand 8903 * from the header of the device. Of note, l2arc_evict() 8904 * does not actually delete buffers from the cache 8905 * device, but trimming may do so depending on the 8906 * hardware implementation. Thus keeping track of the 8907 * evict hand is useful. 8908 */ 8909 dev->l2ad_evict = MAX(dev->l2ad_evict, taddr); 8910 } 8911 } 8912 8913 retry: 8914 mutex_enter(&dev->l2ad_mtx); 8915 /* 8916 * We have to account for evicted log blocks. Run vdev_space_update() 8917 * on log blocks whose offset (in bytes) is before the evicted offset 8918 * (in bytes) by searching in the list of pointers to log blocks 8919 * present in the L2ARC device. 8920 */ 8921 for (lb_ptr_buf = list_tail(&dev->l2ad_lbptr_list); lb_ptr_buf; 8922 lb_ptr_buf = lb_ptr_buf_prev) { 8923 8924 lb_ptr_buf_prev = list_prev(&dev->l2ad_lbptr_list, lb_ptr_buf); 8925 8926 /* L2BLK_GET_PSIZE returns aligned size for log blocks */ 8927 uint64_t asize = L2BLK_GET_PSIZE( 8928 (lb_ptr_buf->lb_ptr)->lbp_prop); 8929 8930 /* 8931 * We don't worry about log blocks left behind (ie 8932 * lbp_payload_start < l2ad_hand) because l2arc_write_buffers() 8933 * will never write more than l2arc_evict() evicts. 8934 */ 8935 if (!all && l2arc_log_blkptr_valid(dev, lb_ptr_buf->lb_ptr)) { 8936 break; 8937 } else { 8938 vdev_space_update(vd, -asize, 0, 0); 8939 ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize); 8940 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count); 8941 zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize, 8942 lb_ptr_buf); 8943 zfs_refcount_remove(&dev->l2ad_lb_count, lb_ptr_buf); 8944 list_remove(&dev->l2ad_lbptr_list, lb_ptr_buf); 8945 kmem_free(lb_ptr_buf->lb_ptr, 8946 sizeof (l2arc_log_blkptr_t)); 8947 kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t)); 8948 } 8949 } 8950 8951 for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) { 8952 hdr_prev = list_prev(buflist, hdr); 8953 8954 ASSERT(!HDR_EMPTY(hdr)); 8955 hash_lock = HDR_LOCK(hdr); 8956 8957 /* 8958 * We cannot use mutex_enter or else we can deadlock 8959 * with l2arc_write_buffers (due to swapping the order 8960 * the hash lock and l2ad_mtx are taken). 8961 */ 8962 if (!mutex_tryenter(hash_lock)) { 8963 /* 8964 * Missed the hash lock. Retry. 8965 */ 8966 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry); 8967 mutex_exit(&dev->l2ad_mtx); 8968 mutex_enter(hash_lock); 8969 mutex_exit(hash_lock); 8970 goto retry; 8971 } 8972 8973 /* 8974 * A header can't be on this list if it doesn't have L2 header. 8975 */ 8976 ASSERT(HDR_HAS_L2HDR(hdr)); 8977 8978 /* Ensure this header has finished being written. */ 8979 ASSERT(!HDR_L2_WRITING(hdr)); 8980 ASSERT(!HDR_L2_WRITE_HEAD(hdr)); 8981 8982 if (!all && (hdr->b_l2hdr.b_daddr >= dev->l2ad_evict || 8983 hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) { 8984 /* 8985 * We've evicted to the target address, 8986 * or the end of the device. 8987 */ 8988 mutex_exit(hash_lock); 8989 break; 8990 } 8991 8992 if (!HDR_HAS_L1HDR(hdr)) { 8993 ASSERT(!HDR_L2_READING(hdr)); 8994 /* 8995 * This doesn't exist in the ARC. Destroy. 8996 * arc_hdr_destroy() will call list_remove() 8997 * and decrement arcstat_l2_lsize. 8998 */ 8999 arc_change_state(arc_anon, hdr); 9000 arc_hdr_destroy(hdr); 9001 } else { 9002 ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only); 9003 ARCSTAT_BUMP(arcstat_l2_evict_l1cached); 9004 /* 9005 * Invalidate issued or about to be issued 9006 * reads, since we may be about to write 9007 * over this location. 9008 */ 9009 if (HDR_L2_READING(hdr)) { 9010 ARCSTAT_BUMP(arcstat_l2_evict_reading); 9011 arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED); 9012 } 9013 9014 arc_hdr_l2hdr_destroy(hdr); 9015 } 9016 mutex_exit(hash_lock); 9017 } 9018 mutex_exit(&dev->l2ad_mtx); 9019 9020 out: 9021 /* 9022 * We need to check if we evict all buffers, otherwise we may iterate 9023 * unnecessarily. 9024 */ 9025 if (!all && rerun) { 9026 /* 9027 * Bump device hand to the device start if it is approaching the 9028 * end. l2arc_evict() has already evicted ahead for this case. 9029 */ 9030 dev->l2ad_hand = dev->l2ad_start; 9031 dev->l2ad_evict = dev->l2ad_start; 9032 dev->l2ad_first = B_FALSE; 9033 goto top; 9034 } 9035 9036 if (!all) { 9037 /* 9038 * In case of cache device removal (all) the following 9039 * assertions may be violated without functional consequences 9040 * as the device is about to be removed. 9041 */ 9042 ASSERT3U(dev->l2ad_hand + distance, <, dev->l2ad_end); 9043 if (!dev->l2ad_first) 9044 ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict); 9045 } 9046 } 9047 9048 /* 9049 * Handle any abd transforms that might be required for writing to the L2ARC. 9050 * If successful, this function will always return an abd with the data 9051 * transformed as it is on disk in a new abd of asize bytes. 9052 */ 9053 static int 9054 l2arc_apply_transforms(spa_t *spa, arc_buf_hdr_t *hdr, uint64_t asize, 9055 abd_t **abd_out) 9056 { 9057 int ret; 9058 void *tmp = NULL; 9059 abd_t *cabd = NULL, *eabd = NULL, *to_write = hdr->b_l1hdr.b_pabd; 9060 enum zio_compress compress = HDR_GET_COMPRESS(hdr); 9061 uint64_t psize = HDR_GET_PSIZE(hdr); 9062 uint64_t size = arc_hdr_size(hdr); 9063 boolean_t ismd = HDR_ISTYPE_METADATA(hdr); 9064 boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS); 9065 dsl_crypto_key_t *dck = NULL; 9066 uint8_t mac[ZIO_DATA_MAC_LEN] = { 0 }; 9067 boolean_t no_crypt = B_FALSE; 9068 9069 ASSERT((HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF && 9070 !HDR_COMPRESSION_ENABLED(hdr)) || 9071 HDR_ENCRYPTED(hdr) || HDR_SHARED_DATA(hdr) || psize != asize); 9072 ASSERT3U(psize, <=, asize); 9073 9074 /* 9075 * If this data simply needs its own buffer, we simply allocate it 9076 * and copy the data. This may be done to eliminate a dependency on a 9077 * shared buffer or to reallocate the buffer to match asize. 9078 */ 9079 if (HDR_HAS_RABD(hdr) && asize != psize) { 9080 ASSERT3U(asize, >=, psize); 9081 to_write = abd_alloc_for_io(asize, ismd); 9082 abd_copy(to_write, hdr->b_crypt_hdr.b_rabd, psize); 9083 if (psize != asize) 9084 abd_zero_off(to_write, psize, asize - psize); 9085 goto out; 9086 } 9087 9088 if ((compress == ZIO_COMPRESS_OFF || HDR_COMPRESSION_ENABLED(hdr)) && 9089 !HDR_ENCRYPTED(hdr)) { 9090 ASSERT3U(size, ==, psize); 9091 to_write = abd_alloc_for_io(asize, ismd); 9092 abd_copy(to_write, hdr->b_l1hdr.b_pabd, size); 9093 if (size != asize) 9094 abd_zero_off(to_write, size, asize - size); 9095 goto out; 9096 } 9097 9098 if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) { 9099 /* 9100 * In some cases, we can wind up with size > asize, so 9101 * we need to opt for the larger allocation option here. 9102 * 9103 * (We also need abd_return_buf_copy in all cases because 9104 * it's an ASSERT() to modify the buffer before returning it 9105 * with arc_return_buf(), and all the compressors 9106 * write things before deciding to fail compression in nearly 9107 * every case.) 9108 */ 9109 cabd = abd_alloc_for_io(size, ismd); 9110 tmp = abd_borrow_buf(cabd, size); 9111 9112 psize = zio_compress_data(compress, to_write, &tmp, size, 9113 hdr->b_complevel); 9114 9115 if (psize >= asize) { 9116 psize = HDR_GET_PSIZE(hdr); 9117 abd_return_buf_copy(cabd, tmp, size); 9118 HDR_SET_COMPRESS(hdr, ZIO_COMPRESS_OFF); 9119 to_write = cabd; 9120 abd_copy(to_write, hdr->b_l1hdr.b_pabd, psize); 9121 if (psize != asize) 9122 abd_zero_off(to_write, psize, asize - psize); 9123 goto encrypt; 9124 } 9125 ASSERT3U(psize, <=, HDR_GET_PSIZE(hdr)); 9126 if (psize < asize) 9127 memset((char *)tmp + psize, 0, asize - psize); 9128 psize = HDR_GET_PSIZE(hdr); 9129 abd_return_buf_copy(cabd, tmp, size); 9130 to_write = cabd; 9131 } 9132 9133 encrypt: 9134 if (HDR_ENCRYPTED(hdr)) { 9135 eabd = abd_alloc_for_io(asize, ismd); 9136 9137 /* 9138 * If the dataset was disowned before the buffer 9139 * made it to this point, the key to re-encrypt 9140 * it won't be available. In this case we simply 9141 * won't write the buffer to the L2ARC. 9142 */ 9143 ret = spa_keystore_lookup_key(spa, hdr->b_crypt_hdr.b_dsobj, 9144 FTAG, &dck); 9145 if (ret != 0) 9146 goto error; 9147 9148 ret = zio_do_crypt_abd(B_TRUE, &dck->dck_key, 9149 hdr->b_crypt_hdr.b_ot, bswap, hdr->b_crypt_hdr.b_salt, 9150 hdr->b_crypt_hdr.b_iv, mac, psize, to_write, eabd, 9151 &no_crypt); 9152 if (ret != 0) 9153 goto error; 9154 9155 if (no_crypt) 9156 abd_copy(eabd, to_write, psize); 9157 9158 if (psize != asize) 9159 abd_zero_off(eabd, psize, asize - psize); 9160 9161 /* assert that the MAC we got here matches the one we saved */ 9162 ASSERT0(memcmp(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN)); 9163 spa_keystore_dsl_key_rele(spa, dck, FTAG); 9164 9165 if (to_write == cabd) 9166 abd_free(cabd); 9167 9168 to_write = eabd; 9169 } 9170 9171 out: 9172 ASSERT3P(to_write, !=, hdr->b_l1hdr.b_pabd); 9173 *abd_out = to_write; 9174 return (0); 9175 9176 error: 9177 if (dck != NULL) 9178 spa_keystore_dsl_key_rele(spa, dck, FTAG); 9179 if (cabd != NULL) 9180 abd_free(cabd); 9181 if (eabd != NULL) 9182 abd_free(eabd); 9183 9184 *abd_out = NULL; 9185 return (ret); 9186 } 9187 9188 static void 9189 l2arc_blk_fetch_done(zio_t *zio) 9190 { 9191 l2arc_read_callback_t *cb; 9192 9193 cb = zio->io_private; 9194 if (cb->l2rcb_abd != NULL) 9195 abd_free(cb->l2rcb_abd); 9196 kmem_free(cb, sizeof (l2arc_read_callback_t)); 9197 } 9198 9199 /* 9200 * Find and write ARC buffers to the L2ARC device. 9201 * 9202 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid 9203 * for reading until they have completed writing. 9204 * The headroom_boost is an in-out parameter used to maintain headroom boost 9205 * state between calls to this function. 9206 * 9207 * Returns the number of bytes actually written (which may be smaller than 9208 * the delta by which the device hand has changed due to alignment and the 9209 * writing of log blocks). 9210 */ 9211 static uint64_t 9212 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz) 9213 { 9214 arc_buf_hdr_t *hdr, *hdr_prev, *head; 9215 uint64_t write_asize, write_psize, write_lsize, headroom; 9216 boolean_t full; 9217 l2arc_write_callback_t *cb = NULL; 9218 zio_t *pio, *wzio; 9219 uint64_t guid = spa_load_guid(spa); 9220 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; 9221 9222 ASSERT3P(dev->l2ad_vdev, !=, NULL); 9223 9224 pio = NULL; 9225 write_lsize = write_asize = write_psize = 0; 9226 full = B_FALSE; 9227 head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE); 9228 arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR); 9229 9230 /* 9231 * Copy buffers for L2ARC writing. 9232 */ 9233 for (int pass = 0; pass < L2ARC_FEED_TYPES; pass++) { 9234 /* 9235 * If pass == 1 or 3, we cache MRU metadata and data 9236 * respectively. 9237 */ 9238 if (l2arc_mfuonly) { 9239 if (pass == 1 || pass == 3) 9240 continue; 9241 } 9242 9243 multilist_sublist_t *mls = l2arc_sublist_lock(pass); 9244 uint64_t passed_sz = 0; 9245 9246 VERIFY3P(mls, !=, NULL); 9247 9248 /* 9249 * L2ARC fast warmup. 9250 * 9251 * Until the ARC is warm and starts to evict, read from the 9252 * head of the ARC lists rather than the tail. 9253 */ 9254 if (arc_warm == B_FALSE) 9255 hdr = multilist_sublist_head(mls); 9256 else 9257 hdr = multilist_sublist_tail(mls); 9258 9259 headroom = target_sz * l2arc_headroom; 9260 if (zfs_compressed_arc_enabled) 9261 headroom = (headroom * l2arc_headroom_boost) / 100; 9262 9263 for (; hdr; hdr = hdr_prev) { 9264 kmutex_t *hash_lock; 9265 abd_t *to_write = NULL; 9266 9267 if (arc_warm == B_FALSE) 9268 hdr_prev = multilist_sublist_next(mls, hdr); 9269 else 9270 hdr_prev = multilist_sublist_prev(mls, hdr); 9271 9272 hash_lock = HDR_LOCK(hdr); 9273 if (!mutex_tryenter(hash_lock)) { 9274 /* 9275 * Skip this buffer rather than waiting. 9276 */ 9277 continue; 9278 } 9279 9280 passed_sz += HDR_GET_LSIZE(hdr); 9281 if (l2arc_headroom != 0 && passed_sz > headroom) { 9282 /* 9283 * Searched too far. 9284 */ 9285 mutex_exit(hash_lock); 9286 break; 9287 } 9288 9289 if (!l2arc_write_eligible(guid, hdr)) { 9290 mutex_exit(hash_lock); 9291 continue; 9292 } 9293 9294 ASSERT(HDR_HAS_L1HDR(hdr)); 9295 9296 ASSERT3U(HDR_GET_PSIZE(hdr), >, 0); 9297 ASSERT3U(arc_hdr_size(hdr), >, 0); 9298 ASSERT(hdr->b_l1hdr.b_pabd != NULL || 9299 HDR_HAS_RABD(hdr)); 9300 uint64_t psize = HDR_GET_PSIZE(hdr); 9301 uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev, 9302 psize); 9303 9304 /* 9305 * If the allocated size of this buffer plus the max 9306 * size for the pending log block exceeds the evicted 9307 * target size, terminate writing buffers for this run. 9308 */ 9309 if (write_asize + asize + 9310 sizeof (l2arc_log_blk_phys_t) > target_sz) { 9311 full = B_TRUE; 9312 mutex_exit(hash_lock); 9313 break; 9314 } 9315 9316 /* 9317 * We rely on the L1 portion of the header below, so 9318 * it's invalid for this header to have been evicted out 9319 * of the ghost cache, prior to being written out. The 9320 * ARC_FLAG_L2_WRITING bit ensures this won't happen. 9321 */ 9322 arc_hdr_set_flags(hdr, ARC_FLAG_L2_WRITING); 9323 9324 /* 9325 * If this header has b_rabd, we can use this since it 9326 * must always match the data exactly as it exists on 9327 * disk. Otherwise, the L2ARC can normally use the 9328 * hdr's data, but if we're sharing data between the 9329 * hdr and one of its bufs, L2ARC needs its own copy of 9330 * the data so that the ZIO below can't race with the 9331 * buf consumer. To ensure that this copy will be 9332 * available for the lifetime of the ZIO and be cleaned 9333 * up afterwards, we add it to the l2arc_free_on_write 9334 * queue. If we need to apply any transforms to the 9335 * data (compression, encryption) we will also need the 9336 * extra buffer. 9337 */ 9338 if (HDR_HAS_RABD(hdr) && psize == asize) { 9339 to_write = hdr->b_crypt_hdr.b_rabd; 9340 } else if ((HDR_COMPRESSION_ENABLED(hdr) || 9341 HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) && 9342 !HDR_ENCRYPTED(hdr) && !HDR_SHARED_DATA(hdr) && 9343 psize == asize) { 9344 to_write = hdr->b_l1hdr.b_pabd; 9345 } else { 9346 int ret; 9347 arc_buf_contents_t type = arc_buf_type(hdr); 9348 9349 ret = l2arc_apply_transforms(spa, hdr, asize, 9350 &to_write); 9351 if (ret != 0) { 9352 arc_hdr_clear_flags(hdr, 9353 ARC_FLAG_L2_WRITING); 9354 mutex_exit(hash_lock); 9355 continue; 9356 } 9357 9358 l2arc_free_abd_on_write(to_write, asize, type); 9359 } 9360 9361 if (pio == NULL) { 9362 /* 9363 * Insert a dummy header on the buflist so 9364 * l2arc_write_done() can find where the 9365 * write buffers begin without searching. 9366 */ 9367 mutex_enter(&dev->l2ad_mtx); 9368 list_insert_head(&dev->l2ad_buflist, head); 9369 mutex_exit(&dev->l2ad_mtx); 9370 9371 cb = kmem_alloc( 9372 sizeof (l2arc_write_callback_t), KM_SLEEP); 9373 cb->l2wcb_dev = dev; 9374 cb->l2wcb_head = head; 9375 /* 9376 * Create a list to save allocated abd buffers 9377 * for l2arc_log_blk_commit(). 9378 */ 9379 list_create(&cb->l2wcb_abd_list, 9380 sizeof (l2arc_lb_abd_buf_t), 9381 offsetof(l2arc_lb_abd_buf_t, node)); 9382 pio = zio_root(spa, l2arc_write_done, cb, 9383 ZIO_FLAG_CANFAIL); 9384 } 9385 9386 hdr->b_l2hdr.b_dev = dev; 9387 hdr->b_l2hdr.b_hits = 0; 9388 9389 hdr->b_l2hdr.b_daddr = dev->l2ad_hand; 9390 hdr->b_l2hdr.b_arcs_state = 9391 hdr->b_l1hdr.b_state->arcs_state; 9392 arc_hdr_set_flags(hdr, ARC_FLAG_HAS_L2HDR); 9393 9394 mutex_enter(&dev->l2ad_mtx); 9395 list_insert_head(&dev->l2ad_buflist, hdr); 9396 mutex_exit(&dev->l2ad_mtx); 9397 9398 (void) zfs_refcount_add_many(&dev->l2ad_alloc, 9399 arc_hdr_size(hdr), hdr); 9400 9401 wzio = zio_write_phys(pio, dev->l2ad_vdev, 9402 hdr->b_l2hdr.b_daddr, asize, to_write, 9403 ZIO_CHECKSUM_OFF, NULL, hdr, 9404 ZIO_PRIORITY_ASYNC_WRITE, 9405 ZIO_FLAG_CANFAIL, B_FALSE); 9406 9407 write_lsize += HDR_GET_LSIZE(hdr); 9408 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, 9409 zio_t *, wzio); 9410 9411 write_psize += psize; 9412 write_asize += asize; 9413 dev->l2ad_hand += asize; 9414 l2arc_hdr_arcstats_increment(hdr); 9415 vdev_space_update(dev->l2ad_vdev, asize, 0, 0); 9416 9417 mutex_exit(hash_lock); 9418 9419 /* 9420 * Append buf info to current log and commit if full. 9421 * arcstat_l2_{size,asize} kstats are updated 9422 * internally. 9423 */ 9424 if (l2arc_log_blk_insert(dev, hdr)) { 9425 /* 9426 * l2ad_hand will be adjusted in 9427 * l2arc_log_blk_commit(). 9428 */ 9429 write_asize += 9430 l2arc_log_blk_commit(dev, pio, cb); 9431 } 9432 9433 zio_nowait(wzio); 9434 } 9435 9436 multilist_sublist_unlock(mls); 9437 9438 if (full == B_TRUE) 9439 break; 9440 } 9441 9442 /* No buffers selected for writing? */ 9443 if (pio == NULL) { 9444 ASSERT0(write_lsize); 9445 ASSERT(!HDR_HAS_L1HDR(head)); 9446 kmem_cache_free(hdr_l2only_cache, head); 9447 9448 /* 9449 * Although we did not write any buffers l2ad_evict may 9450 * have advanced. 9451 */ 9452 if (dev->l2ad_evict != l2dhdr->dh_evict) 9453 l2arc_dev_hdr_update(dev); 9454 9455 return (0); 9456 } 9457 9458 if (!dev->l2ad_first) 9459 ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict); 9460 9461 ASSERT3U(write_asize, <=, target_sz); 9462 ARCSTAT_BUMP(arcstat_l2_writes_sent); 9463 ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize); 9464 9465 dev->l2ad_writing = B_TRUE; 9466 (void) zio_wait(pio); 9467 dev->l2ad_writing = B_FALSE; 9468 9469 /* 9470 * Update the device header after the zio completes as 9471 * l2arc_write_done() may have updated the memory holding the log block 9472 * pointers in the device header. 9473 */ 9474 l2arc_dev_hdr_update(dev); 9475 9476 return (write_asize); 9477 } 9478 9479 static boolean_t 9480 l2arc_hdr_limit_reached(void) 9481 { 9482 int64_t s = aggsum_upper_bound(&arc_sums.arcstat_l2_hdr_size); 9483 9484 return (arc_reclaim_needed() || 9485 (s > (arc_warm ? arc_c : arc_c_max) * l2arc_meta_percent / 100)); 9486 } 9487 9488 /* 9489 * This thread feeds the L2ARC at regular intervals. This is the beating 9490 * heart of the L2ARC. 9491 */ 9492 static __attribute__((noreturn)) void 9493 l2arc_feed_thread(void *unused) 9494 { 9495 (void) unused; 9496 callb_cpr_t cpr; 9497 l2arc_dev_t *dev; 9498 spa_t *spa; 9499 uint64_t size, wrote; 9500 clock_t begin, next = ddi_get_lbolt(); 9501 fstrans_cookie_t cookie; 9502 9503 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG); 9504 9505 mutex_enter(&l2arc_feed_thr_lock); 9506 9507 cookie = spl_fstrans_mark(); 9508 while (l2arc_thread_exit == 0) { 9509 CALLB_CPR_SAFE_BEGIN(&cpr); 9510 (void) cv_timedwait_idle(&l2arc_feed_thr_cv, 9511 &l2arc_feed_thr_lock, next); 9512 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock); 9513 next = ddi_get_lbolt() + hz; 9514 9515 /* 9516 * Quick check for L2ARC devices. 9517 */ 9518 mutex_enter(&l2arc_dev_mtx); 9519 if (l2arc_ndev == 0) { 9520 mutex_exit(&l2arc_dev_mtx); 9521 continue; 9522 } 9523 mutex_exit(&l2arc_dev_mtx); 9524 begin = ddi_get_lbolt(); 9525 9526 /* 9527 * This selects the next l2arc device to write to, and in 9528 * doing so the next spa to feed from: dev->l2ad_spa. This 9529 * will return NULL if there are now no l2arc devices or if 9530 * they are all faulted. 9531 * 9532 * If a device is returned, its spa's config lock is also 9533 * held to prevent device removal. l2arc_dev_get_next() 9534 * will grab and release l2arc_dev_mtx. 9535 */ 9536 if ((dev = l2arc_dev_get_next()) == NULL) 9537 continue; 9538 9539 spa = dev->l2ad_spa; 9540 ASSERT3P(spa, !=, NULL); 9541 9542 /* 9543 * If the pool is read-only then force the feed thread to 9544 * sleep a little longer. 9545 */ 9546 if (!spa_writeable(spa)) { 9547 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz; 9548 spa_config_exit(spa, SCL_L2ARC, dev); 9549 continue; 9550 } 9551 9552 /* 9553 * Avoid contributing to memory pressure. 9554 */ 9555 if (l2arc_hdr_limit_reached()) { 9556 ARCSTAT_BUMP(arcstat_l2_abort_lowmem); 9557 spa_config_exit(spa, SCL_L2ARC, dev); 9558 continue; 9559 } 9560 9561 ARCSTAT_BUMP(arcstat_l2_feeds); 9562 9563 size = l2arc_write_size(dev); 9564 9565 /* 9566 * Evict L2ARC buffers that will be overwritten. 9567 */ 9568 l2arc_evict(dev, size, B_FALSE); 9569 9570 /* 9571 * Write ARC buffers. 9572 */ 9573 wrote = l2arc_write_buffers(spa, dev, size); 9574 9575 /* 9576 * Calculate interval between writes. 9577 */ 9578 next = l2arc_write_interval(begin, size, wrote); 9579 spa_config_exit(spa, SCL_L2ARC, dev); 9580 } 9581 spl_fstrans_unmark(cookie); 9582 9583 l2arc_thread_exit = 0; 9584 cv_broadcast(&l2arc_feed_thr_cv); 9585 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */ 9586 thread_exit(); 9587 } 9588 9589 boolean_t 9590 l2arc_vdev_present(vdev_t *vd) 9591 { 9592 return (l2arc_vdev_get(vd) != NULL); 9593 } 9594 9595 /* 9596 * Returns the l2arc_dev_t associated with a particular vdev_t or NULL if 9597 * the vdev_t isn't an L2ARC device. 9598 */ 9599 l2arc_dev_t * 9600 l2arc_vdev_get(vdev_t *vd) 9601 { 9602 l2arc_dev_t *dev; 9603 9604 mutex_enter(&l2arc_dev_mtx); 9605 for (dev = list_head(l2arc_dev_list); dev != NULL; 9606 dev = list_next(l2arc_dev_list, dev)) { 9607 if (dev->l2ad_vdev == vd) 9608 break; 9609 } 9610 mutex_exit(&l2arc_dev_mtx); 9611 9612 return (dev); 9613 } 9614 9615 static void 9616 l2arc_rebuild_dev(l2arc_dev_t *dev, boolean_t reopen) 9617 { 9618 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; 9619 uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; 9620 spa_t *spa = dev->l2ad_spa; 9621 9622 /* 9623 * The L2ARC has to hold at least the payload of one log block for 9624 * them to be restored (persistent L2ARC). The payload of a log block 9625 * depends on the amount of its log entries. We always write log blocks 9626 * with 1022 entries. How many of them are committed or restored depends 9627 * on the size of the L2ARC device. Thus the maximum payload of 9628 * one log block is 1022 * SPA_MAXBLOCKSIZE = 16GB. If the L2ARC device 9629 * is less than that, we reduce the amount of committed and restored 9630 * log entries per block so as to enable persistence. 9631 */ 9632 if (dev->l2ad_end < l2arc_rebuild_blocks_min_l2size) { 9633 dev->l2ad_log_entries = 0; 9634 } else { 9635 dev->l2ad_log_entries = MIN((dev->l2ad_end - 9636 dev->l2ad_start) >> SPA_MAXBLOCKSHIFT, 9637 L2ARC_LOG_BLK_MAX_ENTRIES); 9638 } 9639 9640 /* 9641 * Read the device header, if an error is returned do not rebuild L2ARC. 9642 */ 9643 if (l2arc_dev_hdr_read(dev) == 0 && dev->l2ad_log_entries > 0) { 9644 /* 9645 * If we are onlining a cache device (vdev_reopen) that was 9646 * still present (l2arc_vdev_present()) and rebuild is enabled, 9647 * we should evict all ARC buffers and pointers to log blocks 9648 * and reclaim their space before restoring its contents to 9649 * L2ARC. 9650 */ 9651 if (reopen) { 9652 if (!l2arc_rebuild_enabled) { 9653 return; 9654 } else { 9655 l2arc_evict(dev, 0, B_TRUE); 9656 /* start a new log block */ 9657 dev->l2ad_log_ent_idx = 0; 9658 dev->l2ad_log_blk_payload_asize = 0; 9659 dev->l2ad_log_blk_payload_start = 0; 9660 } 9661 } 9662 /* 9663 * Just mark the device as pending for a rebuild. We won't 9664 * be starting a rebuild in line here as it would block pool 9665 * import. Instead spa_load_impl will hand that off to an 9666 * async task which will call l2arc_spa_rebuild_start. 9667 */ 9668 dev->l2ad_rebuild = B_TRUE; 9669 } else if (spa_writeable(spa)) { 9670 /* 9671 * In this case TRIM the whole device if l2arc_trim_ahead > 0, 9672 * otherwise create a new header. We zero out the memory holding 9673 * the header to reset dh_start_lbps. If we TRIM the whole 9674 * device the new header will be written by 9675 * vdev_trim_l2arc_thread() at the end of the TRIM to update the 9676 * trim_state in the header too. When reading the header, if 9677 * trim_state is not VDEV_TRIM_COMPLETE and l2arc_trim_ahead > 0 9678 * we opt to TRIM the whole device again. 9679 */ 9680 if (l2arc_trim_ahead > 0) { 9681 dev->l2ad_trim_all = B_TRUE; 9682 } else { 9683 memset(l2dhdr, 0, l2dhdr_asize); 9684 l2arc_dev_hdr_update(dev); 9685 } 9686 } 9687 } 9688 9689 /* 9690 * Add a vdev for use by the L2ARC. By this point the spa has already 9691 * validated the vdev and opened it. 9692 */ 9693 void 9694 l2arc_add_vdev(spa_t *spa, vdev_t *vd) 9695 { 9696 l2arc_dev_t *adddev; 9697 uint64_t l2dhdr_asize; 9698 9699 ASSERT(!l2arc_vdev_present(vd)); 9700 9701 /* 9702 * Create a new l2arc device entry. 9703 */ 9704 adddev = vmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP); 9705 adddev->l2ad_spa = spa; 9706 adddev->l2ad_vdev = vd; 9707 /* leave extra size for an l2arc device header */ 9708 l2dhdr_asize = adddev->l2ad_dev_hdr_asize = 9709 MAX(sizeof (*adddev->l2ad_dev_hdr), 1 << vd->vdev_ashift); 9710 adddev->l2ad_start = VDEV_LABEL_START_SIZE + l2dhdr_asize; 9711 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd); 9712 ASSERT3U(adddev->l2ad_start, <, adddev->l2ad_end); 9713 adddev->l2ad_hand = adddev->l2ad_start; 9714 adddev->l2ad_evict = adddev->l2ad_start; 9715 adddev->l2ad_first = B_TRUE; 9716 adddev->l2ad_writing = B_FALSE; 9717 adddev->l2ad_trim_all = B_FALSE; 9718 list_link_init(&adddev->l2ad_node); 9719 adddev->l2ad_dev_hdr = kmem_zalloc(l2dhdr_asize, KM_SLEEP); 9720 9721 mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL); 9722 /* 9723 * This is a list of all ARC buffers that are still valid on the 9724 * device. 9725 */ 9726 list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t), 9727 offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node)); 9728 9729 /* 9730 * This is a list of pointers to log blocks that are still present 9731 * on the device. 9732 */ 9733 list_create(&adddev->l2ad_lbptr_list, sizeof (l2arc_lb_ptr_buf_t), 9734 offsetof(l2arc_lb_ptr_buf_t, node)); 9735 9736 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand); 9737 zfs_refcount_create(&adddev->l2ad_alloc); 9738 zfs_refcount_create(&adddev->l2ad_lb_asize); 9739 zfs_refcount_create(&adddev->l2ad_lb_count); 9740 9741 /* 9742 * Decide if dev is eligible for L2ARC rebuild or whole device 9743 * trimming. This has to happen before the device is added in the 9744 * cache device list and l2arc_dev_mtx is released. Otherwise 9745 * l2arc_feed_thread() might already start writing on the 9746 * device. 9747 */ 9748 l2arc_rebuild_dev(adddev, B_FALSE); 9749 9750 /* 9751 * Add device to global list 9752 */ 9753 mutex_enter(&l2arc_dev_mtx); 9754 list_insert_head(l2arc_dev_list, adddev); 9755 atomic_inc_64(&l2arc_ndev); 9756 mutex_exit(&l2arc_dev_mtx); 9757 } 9758 9759 /* 9760 * Decide if a vdev is eligible for L2ARC rebuild, called from vdev_reopen() 9761 * in case of onlining a cache device. 9762 */ 9763 void 9764 l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen) 9765 { 9766 l2arc_dev_t *dev = NULL; 9767 9768 dev = l2arc_vdev_get(vd); 9769 ASSERT3P(dev, !=, NULL); 9770 9771 /* 9772 * In contrast to l2arc_add_vdev() we do not have to worry about 9773 * l2arc_feed_thread() invalidating previous content when onlining a 9774 * cache device. The device parameters (l2ad*) are not cleared when 9775 * offlining the device and writing new buffers will not invalidate 9776 * all previous content. In worst case only buffers that have not had 9777 * their log block written to the device will be lost. 9778 * When onlining the cache device (ie offline->online without exporting 9779 * the pool in between) this happens: 9780 * vdev_reopen() -> vdev_open() -> l2arc_rebuild_vdev() 9781 * | | 9782 * vdev_is_dead() = B_FALSE l2ad_rebuild = B_TRUE 9783 * During the time where vdev_is_dead = B_FALSE and until l2ad_rebuild 9784 * is set to B_TRUE we might write additional buffers to the device. 9785 */ 9786 l2arc_rebuild_dev(dev, reopen); 9787 } 9788 9789 /* 9790 * Remove a vdev from the L2ARC. 9791 */ 9792 void 9793 l2arc_remove_vdev(vdev_t *vd) 9794 { 9795 l2arc_dev_t *remdev = NULL; 9796 9797 /* 9798 * Find the device by vdev 9799 */ 9800 remdev = l2arc_vdev_get(vd); 9801 ASSERT3P(remdev, !=, NULL); 9802 9803 /* 9804 * Cancel any ongoing or scheduled rebuild. 9805 */ 9806 mutex_enter(&l2arc_rebuild_thr_lock); 9807 if (remdev->l2ad_rebuild_began == B_TRUE) { 9808 remdev->l2ad_rebuild_cancel = B_TRUE; 9809 while (remdev->l2ad_rebuild == B_TRUE) 9810 cv_wait(&l2arc_rebuild_thr_cv, &l2arc_rebuild_thr_lock); 9811 } 9812 mutex_exit(&l2arc_rebuild_thr_lock); 9813 9814 /* 9815 * Remove device from global list 9816 */ 9817 mutex_enter(&l2arc_dev_mtx); 9818 list_remove(l2arc_dev_list, remdev); 9819 l2arc_dev_last = NULL; /* may have been invalidated */ 9820 atomic_dec_64(&l2arc_ndev); 9821 mutex_exit(&l2arc_dev_mtx); 9822 9823 /* 9824 * Clear all buflists and ARC references. L2ARC device flush. 9825 */ 9826 l2arc_evict(remdev, 0, B_TRUE); 9827 list_destroy(&remdev->l2ad_buflist); 9828 ASSERT(list_is_empty(&remdev->l2ad_lbptr_list)); 9829 list_destroy(&remdev->l2ad_lbptr_list); 9830 mutex_destroy(&remdev->l2ad_mtx); 9831 zfs_refcount_destroy(&remdev->l2ad_alloc); 9832 zfs_refcount_destroy(&remdev->l2ad_lb_asize); 9833 zfs_refcount_destroy(&remdev->l2ad_lb_count); 9834 kmem_free(remdev->l2ad_dev_hdr, remdev->l2ad_dev_hdr_asize); 9835 vmem_free(remdev, sizeof (l2arc_dev_t)); 9836 } 9837 9838 void 9839 l2arc_init(void) 9840 { 9841 l2arc_thread_exit = 0; 9842 l2arc_ndev = 0; 9843 9844 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL); 9845 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL); 9846 mutex_init(&l2arc_rebuild_thr_lock, NULL, MUTEX_DEFAULT, NULL); 9847 cv_init(&l2arc_rebuild_thr_cv, NULL, CV_DEFAULT, NULL); 9848 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 9849 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL); 9850 9851 l2arc_dev_list = &L2ARC_dev_list; 9852 l2arc_free_on_write = &L2ARC_free_on_write; 9853 list_create(l2arc_dev_list, sizeof (l2arc_dev_t), 9854 offsetof(l2arc_dev_t, l2ad_node)); 9855 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t), 9856 offsetof(l2arc_data_free_t, l2df_list_node)); 9857 } 9858 9859 void 9860 l2arc_fini(void) 9861 { 9862 mutex_destroy(&l2arc_feed_thr_lock); 9863 cv_destroy(&l2arc_feed_thr_cv); 9864 mutex_destroy(&l2arc_rebuild_thr_lock); 9865 cv_destroy(&l2arc_rebuild_thr_cv); 9866 mutex_destroy(&l2arc_dev_mtx); 9867 mutex_destroy(&l2arc_free_on_write_mtx); 9868 9869 list_destroy(l2arc_dev_list); 9870 list_destroy(l2arc_free_on_write); 9871 } 9872 9873 void 9874 l2arc_start(void) 9875 { 9876 if (!(spa_mode_global & SPA_MODE_WRITE)) 9877 return; 9878 9879 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0, 9880 TS_RUN, defclsyspri); 9881 } 9882 9883 void 9884 l2arc_stop(void) 9885 { 9886 if (!(spa_mode_global & SPA_MODE_WRITE)) 9887 return; 9888 9889 mutex_enter(&l2arc_feed_thr_lock); 9890 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */ 9891 l2arc_thread_exit = 1; 9892 while (l2arc_thread_exit != 0) 9893 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock); 9894 mutex_exit(&l2arc_feed_thr_lock); 9895 } 9896 9897 /* 9898 * Punches out rebuild threads for the L2ARC devices in a spa. This should 9899 * be called after pool import from the spa async thread, since starting 9900 * these threads directly from spa_import() will make them part of the 9901 * "zpool import" context and delay process exit (and thus pool import). 9902 */ 9903 void 9904 l2arc_spa_rebuild_start(spa_t *spa) 9905 { 9906 ASSERT(MUTEX_HELD(&spa_namespace_lock)); 9907 9908 /* 9909 * Locate the spa's l2arc devices and kick off rebuild threads. 9910 */ 9911 for (int i = 0; i < spa->spa_l2cache.sav_count; i++) { 9912 l2arc_dev_t *dev = 9913 l2arc_vdev_get(spa->spa_l2cache.sav_vdevs[i]); 9914 if (dev == NULL) { 9915 /* Don't attempt a rebuild if the vdev is UNAVAIL */ 9916 continue; 9917 } 9918 mutex_enter(&l2arc_rebuild_thr_lock); 9919 if (dev->l2ad_rebuild && !dev->l2ad_rebuild_cancel) { 9920 dev->l2ad_rebuild_began = B_TRUE; 9921 (void) thread_create(NULL, 0, l2arc_dev_rebuild_thread, 9922 dev, 0, &p0, TS_RUN, minclsyspri); 9923 } 9924 mutex_exit(&l2arc_rebuild_thr_lock); 9925 } 9926 } 9927 9928 /* 9929 * Main entry point for L2ARC rebuilding. 9930 */ 9931 static __attribute__((noreturn)) void 9932 l2arc_dev_rebuild_thread(void *arg) 9933 { 9934 l2arc_dev_t *dev = arg; 9935 9936 VERIFY(!dev->l2ad_rebuild_cancel); 9937 VERIFY(dev->l2ad_rebuild); 9938 (void) l2arc_rebuild(dev); 9939 mutex_enter(&l2arc_rebuild_thr_lock); 9940 dev->l2ad_rebuild_began = B_FALSE; 9941 dev->l2ad_rebuild = B_FALSE; 9942 mutex_exit(&l2arc_rebuild_thr_lock); 9943 9944 thread_exit(); 9945 } 9946 9947 /* 9948 * This function implements the actual L2ARC metadata rebuild. It: 9949 * starts reading the log block chain and restores each block's contents 9950 * to memory (reconstructing arc_buf_hdr_t's). 9951 * 9952 * Operation stops under any of the following conditions: 9953 * 9954 * 1) We reach the end of the log block chain. 9955 * 2) We encounter *any* error condition (cksum errors, io errors) 9956 */ 9957 static int 9958 l2arc_rebuild(l2arc_dev_t *dev) 9959 { 9960 vdev_t *vd = dev->l2ad_vdev; 9961 spa_t *spa = vd->vdev_spa; 9962 int err = 0; 9963 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; 9964 l2arc_log_blk_phys_t *this_lb, *next_lb; 9965 zio_t *this_io = NULL, *next_io = NULL; 9966 l2arc_log_blkptr_t lbps[2]; 9967 l2arc_lb_ptr_buf_t *lb_ptr_buf; 9968 boolean_t lock_held; 9969 9970 this_lb = vmem_zalloc(sizeof (*this_lb), KM_SLEEP); 9971 next_lb = vmem_zalloc(sizeof (*next_lb), KM_SLEEP); 9972 9973 /* 9974 * We prevent device removal while issuing reads to the device, 9975 * then during the rebuilding phases we drop this lock again so 9976 * that a spa_unload or device remove can be initiated - this is 9977 * safe, because the spa will signal us to stop before removing 9978 * our device and wait for us to stop. 9979 */ 9980 spa_config_enter(spa, SCL_L2ARC, vd, RW_READER); 9981 lock_held = B_TRUE; 9982 9983 /* 9984 * Retrieve the persistent L2ARC device state. 9985 * L2BLK_GET_PSIZE returns aligned size for log blocks. 9986 */ 9987 dev->l2ad_evict = MAX(l2dhdr->dh_evict, dev->l2ad_start); 9988 dev->l2ad_hand = MAX(l2dhdr->dh_start_lbps[0].lbp_daddr + 9989 L2BLK_GET_PSIZE((&l2dhdr->dh_start_lbps[0])->lbp_prop), 9990 dev->l2ad_start); 9991 dev->l2ad_first = !!(l2dhdr->dh_flags & L2ARC_DEV_HDR_EVICT_FIRST); 9992 9993 vd->vdev_trim_action_time = l2dhdr->dh_trim_action_time; 9994 vd->vdev_trim_state = l2dhdr->dh_trim_state; 9995 9996 /* 9997 * In case the zfs module parameter l2arc_rebuild_enabled is false 9998 * we do not start the rebuild process. 9999 */ 10000 if (!l2arc_rebuild_enabled) 10001 goto out; 10002 10003 /* Prepare the rebuild process */ 10004 memcpy(lbps, l2dhdr->dh_start_lbps, sizeof (lbps)); 10005 10006 /* Start the rebuild process */ 10007 for (;;) { 10008 if (!l2arc_log_blkptr_valid(dev, &lbps[0])) 10009 break; 10010 10011 if ((err = l2arc_log_blk_read(dev, &lbps[0], &lbps[1], 10012 this_lb, next_lb, this_io, &next_io)) != 0) 10013 goto out; 10014 10015 /* 10016 * Our memory pressure valve. If the system is running low 10017 * on memory, rather than swamping memory with new ARC buf 10018 * hdrs, we opt not to rebuild the L2ARC. At this point, 10019 * however, we have already set up our L2ARC dev to chain in 10020 * new metadata log blocks, so the user may choose to offline/ 10021 * online the L2ARC dev at a later time (or re-import the pool) 10022 * to reconstruct it (when there's less memory pressure). 10023 */ 10024 if (l2arc_hdr_limit_reached()) { 10025 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_lowmem); 10026 cmn_err(CE_NOTE, "System running low on memory, " 10027 "aborting L2ARC rebuild."); 10028 err = SET_ERROR(ENOMEM); 10029 goto out; 10030 } 10031 10032 spa_config_exit(spa, SCL_L2ARC, vd); 10033 lock_held = B_FALSE; 10034 10035 /* 10036 * Now that we know that the next_lb checks out alright, we 10037 * can start reconstruction from this log block. 10038 * L2BLK_GET_PSIZE returns aligned size for log blocks. 10039 */ 10040 uint64_t asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop); 10041 l2arc_log_blk_restore(dev, this_lb, asize); 10042 10043 /* 10044 * log block restored, include its pointer in the list of 10045 * pointers to log blocks present in the L2ARC device. 10046 */ 10047 lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP); 10048 lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t), 10049 KM_SLEEP); 10050 memcpy(lb_ptr_buf->lb_ptr, &lbps[0], 10051 sizeof (l2arc_log_blkptr_t)); 10052 mutex_enter(&dev->l2ad_mtx); 10053 list_insert_tail(&dev->l2ad_lbptr_list, lb_ptr_buf); 10054 ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize); 10055 ARCSTAT_BUMP(arcstat_l2_log_blk_count); 10056 zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf); 10057 zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf); 10058 mutex_exit(&dev->l2ad_mtx); 10059 vdev_space_update(vd, asize, 0, 0); 10060 10061 /* 10062 * Protection against loops of log blocks: 10063 * 10064 * l2ad_hand l2ad_evict 10065 * V V 10066 * l2ad_start |=======================================| l2ad_end 10067 * -----|||----|||---|||----||| 10068 * (3) (2) (1) (0) 10069 * ---|||---|||----|||---||| 10070 * (7) (6) (5) (4) 10071 * 10072 * In this situation the pointer of log block (4) passes 10073 * l2arc_log_blkptr_valid() but the log block should not be 10074 * restored as it is overwritten by the payload of log block 10075 * (0). Only log blocks (0)-(3) should be restored. We check 10076 * whether l2ad_evict lies in between the payload starting 10077 * offset of the next log block (lbps[1].lbp_payload_start) 10078 * and the payload starting offset of the present log block 10079 * (lbps[0].lbp_payload_start). If true and this isn't the 10080 * first pass, we are looping from the beginning and we should 10081 * stop. 10082 */ 10083 if (l2arc_range_check_overlap(lbps[1].lbp_payload_start, 10084 lbps[0].lbp_payload_start, dev->l2ad_evict) && 10085 !dev->l2ad_first) 10086 goto out; 10087 10088 kpreempt(KPREEMPT_SYNC); 10089 for (;;) { 10090 mutex_enter(&l2arc_rebuild_thr_lock); 10091 if (dev->l2ad_rebuild_cancel) { 10092 dev->l2ad_rebuild = B_FALSE; 10093 cv_signal(&l2arc_rebuild_thr_cv); 10094 mutex_exit(&l2arc_rebuild_thr_lock); 10095 err = SET_ERROR(ECANCELED); 10096 goto out; 10097 } 10098 mutex_exit(&l2arc_rebuild_thr_lock); 10099 if (spa_config_tryenter(spa, SCL_L2ARC, vd, 10100 RW_READER)) { 10101 lock_held = B_TRUE; 10102 break; 10103 } 10104 /* 10105 * L2ARC config lock held by somebody in writer, 10106 * possibly due to them trying to remove us. They'll 10107 * likely to want us to shut down, so after a little 10108 * delay, we check l2ad_rebuild_cancel and retry 10109 * the lock again. 10110 */ 10111 delay(1); 10112 } 10113 10114 /* 10115 * Continue with the next log block. 10116 */ 10117 lbps[0] = lbps[1]; 10118 lbps[1] = this_lb->lb_prev_lbp; 10119 PTR_SWAP(this_lb, next_lb); 10120 this_io = next_io; 10121 next_io = NULL; 10122 } 10123 10124 if (this_io != NULL) 10125 l2arc_log_blk_fetch_abort(this_io); 10126 out: 10127 if (next_io != NULL) 10128 l2arc_log_blk_fetch_abort(next_io); 10129 vmem_free(this_lb, sizeof (*this_lb)); 10130 vmem_free(next_lb, sizeof (*next_lb)); 10131 10132 if (!l2arc_rebuild_enabled) { 10133 spa_history_log_internal(spa, "L2ARC rebuild", NULL, 10134 "disabled"); 10135 } else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) > 0) { 10136 ARCSTAT_BUMP(arcstat_l2_rebuild_success); 10137 spa_history_log_internal(spa, "L2ARC rebuild", NULL, 10138 "successful, restored %llu blocks", 10139 (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); 10140 } else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) == 0) { 10141 /* 10142 * No error but also nothing restored, meaning the lbps array 10143 * in the device header points to invalid/non-present log 10144 * blocks. Reset the header. 10145 */ 10146 spa_history_log_internal(spa, "L2ARC rebuild", NULL, 10147 "no valid log blocks"); 10148 memset(l2dhdr, 0, dev->l2ad_dev_hdr_asize); 10149 l2arc_dev_hdr_update(dev); 10150 } else if (err == ECANCELED) { 10151 /* 10152 * In case the rebuild was canceled do not log to spa history 10153 * log as the pool may be in the process of being removed. 10154 */ 10155 zfs_dbgmsg("L2ARC rebuild aborted, restored %llu blocks", 10156 (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); 10157 } else if (err != 0) { 10158 spa_history_log_internal(spa, "L2ARC rebuild", NULL, 10159 "aborted, restored %llu blocks", 10160 (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count)); 10161 } 10162 10163 if (lock_held) 10164 spa_config_exit(spa, SCL_L2ARC, vd); 10165 10166 return (err); 10167 } 10168 10169 /* 10170 * Attempts to read the device header on the provided L2ARC device and writes 10171 * it to `hdr'. On success, this function returns 0, otherwise the appropriate 10172 * error code is returned. 10173 */ 10174 static int 10175 l2arc_dev_hdr_read(l2arc_dev_t *dev) 10176 { 10177 int err; 10178 uint64_t guid; 10179 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; 10180 const uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; 10181 abd_t *abd; 10182 10183 guid = spa_guid(dev->l2ad_vdev->vdev_spa); 10184 10185 abd = abd_get_from_buf(l2dhdr, l2dhdr_asize); 10186 10187 err = zio_wait(zio_read_phys(NULL, dev->l2ad_vdev, 10188 VDEV_LABEL_START_SIZE, l2dhdr_asize, abd, 10189 ZIO_CHECKSUM_LABEL, NULL, NULL, ZIO_PRIORITY_SYNC_READ, 10190 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY | 10191 ZIO_FLAG_SPECULATIVE, B_FALSE)); 10192 10193 abd_free(abd); 10194 10195 if (err != 0) { 10196 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_dh_errors); 10197 zfs_dbgmsg("L2ARC IO error (%d) while reading device header, " 10198 "vdev guid: %llu", err, 10199 (u_longlong_t)dev->l2ad_vdev->vdev_guid); 10200 return (err); 10201 } 10202 10203 if (l2dhdr->dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC)) 10204 byteswap_uint64_array(l2dhdr, sizeof (*l2dhdr)); 10205 10206 if (l2dhdr->dh_magic != L2ARC_DEV_HDR_MAGIC || 10207 l2dhdr->dh_spa_guid != guid || 10208 l2dhdr->dh_vdev_guid != dev->l2ad_vdev->vdev_guid || 10209 l2dhdr->dh_version != L2ARC_PERSISTENT_VERSION || 10210 l2dhdr->dh_log_entries != dev->l2ad_log_entries || 10211 l2dhdr->dh_end != dev->l2ad_end || 10212 !l2arc_range_check_overlap(dev->l2ad_start, dev->l2ad_end, 10213 l2dhdr->dh_evict) || 10214 (l2dhdr->dh_trim_state != VDEV_TRIM_COMPLETE && 10215 l2arc_trim_ahead > 0)) { 10216 /* 10217 * Attempt to rebuild a device containing no actual dev hdr 10218 * or containing a header from some other pool or from another 10219 * version of persistent L2ARC. 10220 */ 10221 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_unsupported); 10222 return (SET_ERROR(ENOTSUP)); 10223 } 10224 10225 return (0); 10226 } 10227 10228 /* 10229 * Reads L2ARC log blocks from storage and validates their contents. 10230 * 10231 * This function implements a simple fetcher to make sure that while 10232 * we're processing one buffer the L2ARC is already fetching the next 10233 * one in the chain. 10234 * 10235 * The arguments this_lp and next_lp point to the current and next log block 10236 * address in the block chain. Similarly, this_lb and next_lb hold the 10237 * l2arc_log_blk_phys_t's of the current and next L2ARC blk. 10238 * 10239 * The `this_io' and `next_io' arguments are used for block fetching. 10240 * When issuing the first blk IO during rebuild, you should pass NULL for 10241 * `this_io'. This function will then issue a sync IO to read the block and 10242 * also issue an async IO to fetch the next block in the block chain. The 10243 * fetched IO is returned in `next_io'. On subsequent calls to this 10244 * function, pass the value returned in `next_io' from the previous call 10245 * as `this_io' and a fresh `next_io' pointer to hold the next fetch IO. 10246 * Prior to the call, you should initialize your `next_io' pointer to be 10247 * NULL. If no fetch IO was issued, the pointer is left set at NULL. 10248 * 10249 * On success, this function returns 0, otherwise it returns an appropriate 10250 * error code. On error the fetching IO is aborted and cleared before 10251 * returning from this function. Therefore, if we return `success', the 10252 * caller can assume that we have taken care of cleanup of fetch IOs. 10253 */ 10254 static int 10255 l2arc_log_blk_read(l2arc_dev_t *dev, 10256 const l2arc_log_blkptr_t *this_lbp, const l2arc_log_blkptr_t *next_lbp, 10257 l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb, 10258 zio_t *this_io, zio_t **next_io) 10259 { 10260 int err = 0; 10261 zio_cksum_t cksum; 10262 abd_t *abd = NULL; 10263 uint64_t asize; 10264 10265 ASSERT(this_lbp != NULL && next_lbp != NULL); 10266 ASSERT(this_lb != NULL && next_lb != NULL); 10267 ASSERT(next_io != NULL && *next_io == NULL); 10268 ASSERT(l2arc_log_blkptr_valid(dev, this_lbp)); 10269 10270 /* 10271 * Check to see if we have issued the IO for this log block in a 10272 * previous run. If not, this is the first call, so issue it now. 10273 */ 10274 if (this_io == NULL) { 10275 this_io = l2arc_log_blk_fetch(dev->l2ad_vdev, this_lbp, 10276 this_lb); 10277 } 10278 10279 /* 10280 * Peek to see if we can start issuing the next IO immediately. 10281 */ 10282 if (l2arc_log_blkptr_valid(dev, next_lbp)) { 10283 /* 10284 * Start issuing IO for the next log block early - this 10285 * should help keep the L2ARC device busy while we 10286 * decompress and restore this log block. 10287 */ 10288 *next_io = l2arc_log_blk_fetch(dev->l2ad_vdev, next_lbp, 10289 next_lb); 10290 } 10291 10292 /* Wait for the IO to read this log block to complete */ 10293 if ((err = zio_wait(this_io)) != 0) { 10294 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_io_errors); 10295 zfs_dbgmsg("L2ARC IO error (%d) while reading log block, " 10296 "offset: %llu, vdev guid: %llu", err, 10297 (u_longlong_t)this_lbp->lbp_daddr, 10298 (u_longlong_t)dev->l2ad_vdev->vdev_guid); 10299 goto cleanup; 10300 } 10301 10302 /* 10303 * Make sure the buffer checks out. 10304 * L2BLK_GET_PSIZE returns aligned size for log blocks. 10305 */ 10306 asize = L2BLK_GET_PSIZE((this_lbp)->lbp_prop); 10307 fletcher_4_native(this_lb, asize, NULL, &cksum); 10308 if (!ZIO_CHECKSUM_EQUAL(cksum, this_lbp->lbp_cksum)) { 10309 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_cksum_lb_errors); 10310 zfs_dbgmsg("L2ARC log block cksum failed, offset: %llu, " 10311 "vdev guid: %llu, l2ad_hand: %llu, l2ad_evict: %llu", 10312 (u_longlong_t)this_lbp->lbp_daddr, 10313 (u_longlong_t)dev->l2ad_vdev->vdev_guid, 10314 (u_longlong_t)dev->l2ad_hand, 10315 (u_longlong_t)dev->l2ad_evict); 10316 err = SET_ERROR(ECKSUM); 10317 goto cleanup; 10318 } 10319 10320 /* Now we can take our time decoding this buffer */ 10321 switch (L2BLK_GET_COMPRESS((this_lbp)->lbp_prop)) { 10322 case ZIO_COMPRESS_OFF: 10323 break; 10324 case ZIO_COMPRESS_LZ4: 10325 abd = abd_alloc_for_io(asize, B_TRUE); 10326 abd_copy_from_buf_off(abd, this_lb, 0, asize); 10327 if ((err = zio_decompress_data( 10328 L2BLK_GET_COMPRESS((this_lbp)->lbp_prop), 10329 abd, this_lb, asize, sizeof (*this_lb), NULL)) != 0) { 10330 err = SET_ERROR(EINVAL); 10331 goto cleanup; 10332 } 10333 break; 10334 default: 10335 err = SET_ERROR(EINVAL); 10336 goto cleanup; 10337 } 10338 if (this_lb->lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC)) 10339 byteswap_uint64_array(this_lb, sizeof (*this_lb)); 10340 if (this_lb->lb_magic != L2ARC_LOG_BLK_MAGIC) { 10341 err = SET_ERROR(EINVAL); 10342 goto cleanup; 10343 } 10344 cleanup: 10345 /* Abort an in-flight fetch I/O in case of error */ 10346 if (err != 0 && *next_io != NULL) { 10347 l2arc_log_blk_fetch_abort(*next_io); 10348 *next_io = NULL; 10349 } 10350 if (abd != NULL) 10351 abd_free(abd); 10352 return (err); 10353 } 10354 10355 /* 10356 * Restores the payload of a log block to ARC. This creates empty ARC hdr 10357 * entries which only contain an l2arc hdr, essentially restoring the 10358 * buffers to their L2ARC evicted state. This function also updates space 10359 * usage on the L2ARC vdev to make sure it tracks restored buffers. 10360 */ 10361 static void 10362 l2arc_log_blk_restore(l2arc_dev_t *dev, const l2arc_log_blk_phys_t *lb, 10363 uint64_t lb_asize) 10364 { 10365 uint64_t size = 0, asize = 0; 10366 uint64_t log_entries = dev->l2ad_log_entries; 10367 10368 /* 10369 * Usually arc_adapt() is called only for data, not headers, but 10370 * since we may allocate significant amount of memory here, let ARC 10371 * grow its arc_c. 10372 */ 10373 arc_adapt(log_entries * HDR_L2ONLY_SIZE); 10374 10375 for (int i = log_entries - 1; i >= 0; i--) { 10376 /* 10377 * Restore goes in the reverse temporal direction to preserve 10378 * correct temporal ordering of buffers in the l2ad_buflist. 10379 * l2arc_hdr_restore also does a list_insert_tail instead of 10380 * list_insert_head on the l2ad_buflist: 10381 * 10382 * LIST l2ad_buflist LIST 10383 * HEAD <------ (time) ------ TAIL 10384 * direction +-----+-----+-----+-----+-----+ direction 10385 * of l2arc <== | buf | buf | buf | buf | buf | ===> of rebuild 10386 * fill +-----+-----+-----+-----+-----+ 10387 * ^ ^ 10388 * | | 10389 * | | 10390 * l2arc_feed_thread l2arc_rebuild 10391 * will place new bufs here restores bufs here 10392 * 10393 * During l2arc_rebuild() the device is not used by 10394 * l2arc_feed_thread() as dev->l2ad_rebuild is set to true. 10395 */ 10396 size += L2BLK_GET_LSIZE((&lb->lb_entries[i])->le_prop); 10397 asize += vdev_psize_to_asize(dev->l2ad_vdev, 10398 L2BLK_GET_PSIZE((&lb->lb_entries[i])->le_prop)); 10399 l2arc_hdr_restore(&lb->lb_entries[i], dev); 10400 } 10401 10402 /* 10403 * Record rebuild stats: 10404 * size Logical size of restored buffers in the L2ARC 10405 * asize Aligned size of restored buffers in the L2ARC 10406 */ 10407 ARCSTAT_INCR(arcstat_l2_rebuild_size, size); 10408 ARCSTAT_INCR(arcstat_l2_rebuild_asize, asize); 10409 ARCSTAT_INCR(arcstat_l2_rebuild_bufs, log_entries); 10410 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, lb_asize); 10411 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio, asize / lb_asize); 10412 ARCSTAT_BUMP(arcstat_l2_rebuild_log_blks); 10413 } 10414 10415 /* 10416 * Restores a single ARC buf hdr from a log entry. The ARC buffer is put 10417 * into a state indicating that it has been evicted to L2ARC. 10418 */ 10419 static void 10420 l2arc_hdr_restore(const l2arc_log_ent_phys_t *le, l2arc_dev_t *dev) 10421 { 10422 arc_buf_hdr_t *hdr, *exists; 10423 kmutex_t *hash_lock; 10424 arc_buf_contents_t type = L2BLK_GET_TYPE((le)->le_prop); 10425 uint64_t asize; 10426 10427 /* 10428 * Do all the allocation before grabbing any locks, this lets us 10429 * sleep if memory is full and we don't have to deal with failed 10430 * allocations. 10431 */ 10432 hdr = arc_buf_alloc_l2only(L2BLK_GET_LSIZE((le)->le_prop), type, 10433 dev, le->le_dva, le->le_daddr, 10434 L2BLK_GET_PSIZE((le)->le_prop), le->le_birth, 10435 L2BLK_GET_COMPRESS((le)->le_prop), le->le_complevel, 10436 L2BLK_GET_PROTECTED((le)->le_prop), 10437 L2BLK_GET_PREFETCH((le)->le_prop), 10438 L2BLK_GET_STATE((le)->le_prop)); 10439 asize = vdev_psize_to_asize(dev->l2ad_vdev, 10440 L2BLK_GET_PSIZE((le)->le_prop)); 10441 10442 /* 10443 * vdev_space_update() has to be called before arc_hdr_destroy() to 10444 * avoid underflow since the latter also calls vdev_space_update(). 10445 */ 10446 l2arc_hdr_arcstats_increment(hdr); 10447 vdev_space_update(dev->l2ad_vdev, asize, 0, 0); 10448 10449 mutex_enter(&dev->l2ad_mtx); 10450 list_insert_tail(&dev->l2ad_buflist, hdr); 10451 (void) zfs_refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(hdr), hdr); 10452 mutex_exit(&dev->l2ad_mtx); 10453 10454 exists = buf_hash_insert(hdr, &hash_lock); 10455 if (exists) { 10456 /* Buffer was already cached, no need to restore it. */ 10457 arc_hdr_destroy(hdr); 10458 /* 10459 * If the buffer is already cached, check whether it has 10460 * L2ARC metadata. If not, enter them and update the flag. 10461 * This is important is case of onlining a cache device, since 10462 * we previously evicted all L2ARC metadata from ARC. 10463 */ 10464 if (!HDR_HAS_L2HDR(exists)) { 10465 arc_hdr_set_flags(exists, ARC_FLAG_HAS_L2HDR); 10466 exists->b_l2hdr.b_dev = dev; 10467 exists->b_l2hdr.b_daddr = le->le_daddr; 10468 exists->b_l2hdr.b_arcs_state = 10469 L2BLK_GET_STATE((le)->le_prop); 10470 mutex_enter(&dev->l2ad_mtx); 10471 list_insert_tail(&dev->l2ad_buflist, exists); 10472 (void) zfs_refcount_add_many(&dev->l2ad_alloc, 10473 arc_hdr_size(exists), exists); 10474 mutex_exit(&dev->l2ad_mtx); 10475 l2arc_hdr_arcstats_increment(exists); 10476 vdev_space_update(dev->l2ad_vdev, asize, 0, 0); 10477 } 10478 ARCSTAT_BUMP(arcstat_l2_rebuild_bufs_precached); 10479 } 10480 10481 mutex_exit(hash_lock); 10482 } 10483 10484 /* 10485 * Starts an asynchronous read IO to read a log block. This is used in log 10486 * block reconstruction to start reading the next block before we are done 10487 * decoding and reconstructing the current block, to keep the l2arc device 10488 * nice and hot with read IO to process. 10489 * The returned zio will contain a newly allocated memory buffers for the IO 10490 * data which should then be freed by the caller once the zio is no longer 10491 * needed (i.e. due to it having completed). If you wish to abort this 10492 * zio, you should do so using l2arc_log_blk_fetch_abort, which takes 10493 * care of disposing of the allocated buffers correctly. 10494 */ 10495 static zio_t * 10496 l2arc_log_blk_fetch(vdev_t *vd, const l2arc_log_blkptr_t *lbp, 10497 l2arc_log_blk_phys_t *lb) 10498 { 10499 uint32_t asize; 10500 zio_t *pio; 10501 l2arc_read_callback_t *cb; 10502 10503 /* L2BLK_GET_PSIZE returns aligned size for log blocks */ 10504 asize = L2BLK_GET_PSIZE((lbp)->lbp_prop); 10505 ASSERT(asize <= sizeof (l2arc_log_blk_phys_t)); 10506 10507 cb = kmem_zalloc(sizeof (l2arc_read_callback_t), KM_SLEEP); 10508 cb->l2rcb_abd = abd_get_from_buf(lb, asize); 10509 pio = zio_root(vd->vdev_spa, l2arc_blk_fetch_done, cb, 10510 ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY); 10511 (void) zio_nowait(zio_read_phys(pio, vd, lbp->lbp_daddr, asize, 10512 cb->l2rcb_abd, ZIO_CHECKSUM_OFF, NULL, NULL, 10513 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL | 10514 ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY, B_FALSE)); 10515 10516 return (pio); 10517 } 10518 10519 /* 10520 * Aborts a zio returned from l2arc_log_blk_fetch and frees the data 10521 * buffers allocated for it. 10522 */ 10523 static void 10524 l2arc_log_blk_fetch_abort(zio_t *zio) 10525 { 10526 (void) zio_wait(zio); 10527 } 10528 10529 /* 10530 * Creates a zio to update the device header on an l2arc device. 10531 */ 10532 void 10533 l2arc_dev_hdr_update(l2arc_dev_t *dev) 10534 { 10535 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; 10536 const uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize; 10537 abd_t *abd; 10538 int err; 10539 10540 VERIFY(spa_config_held(dev->l2ad_spa, SCL_STATE_ALL, RW_READER)); 10541 10542 l2dhdr->dh_magic = L2ARC_DEV_HDR_MAGIC; 10543 l2dhdr->dh_version = L2ARC_PERSISTENT_VERSION; 10544 l2dhdr->dh_spa_guid = spa_guid(dev->l2ad_vdev->vdev_spa); 10545 l2dhdr->dh_vdev_guid = dev->l2ad_vdev->vdev_guid; 10546 l2dhdr->dh_log_entries = dev->l2ad_log_entries; 10547 l2dhdr->dh_evict = dev->l2ad_evict; 10548 l2dhdr->dh_start = dev->l2ad_start; 10549 l2dhdr->dh_end = dev->l2ad_end; 10550 l2dhdr->dh_lb_asize = zfs_refcount_count(&dev->l2ad_lb_asize); 10551 l2dhdr->dh_lb_count = zfs_refcount_count(&dev->l2ad_lb_count); 10552 l2dhdr->dh_flags = 0; 10553 l2dhdr->dh_trim_action_time = dev->l2ad_vdev->vdev_trim_action_time; 10554 l2dhdr->dh_trim_state = dev->l2ad_vdev->vdev_trim_state; 10555 if (dev->l2ad_first) 10556 l2dhdr->dh_flags |= L2ARC_DEV_HDR_EVICT_FIRST; 10557 10558 abd = abd_get_from_buf(l2dhdr, l2dhdr_asize); 10559 10560 err = zio_wait(zio_write_phys(NULL, dev->l2ad_vdev, 10561 VDEV_LABEL_START_SIZE, l2dhdr_asize, abd, ZIO_CHECKSUM_LABEL, NULL, 10562 NULL, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE)); 10563 10564 abd_free(abd); 10565 10566 if (err != 0) { 10567 zfs_dbgmsg("L2ARC IO error (%d) while writing device header, " 10568 "vdev guid: %llu", err, 10569 (u_longlong_t)dev->l2ad_vdev->vdev_guid); 10570 } 10571 } 10572 10573 /* 10574 * Commits a log block to the L2ARC device. This routine is invoked from 10575 * l2arc_write_buffers when the log block fills up. 10576 * This function allocates some memory to temporarily hold the serialized 10577 * buffer to be written. This is then released in l2arc_write_done. 10578 */ 10579 static uint64_t 10580 l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, l2arc_write_callback_t *cb) 10581 { 10582 l2arc_log_blk_phys_t *lb = &dev->l2ad_log_blk; 10583 l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr; 10584 uint64_t psize, asize; 10585 zio_t *wzio; 10586 l2arc_lb_abd_buf_t *abd_buf; 10587 uint8_t *tmpbuf = NULL; 10588 l2arc_lb_ptr_buf_t *lb_ptr_buf; 10589 10590 VERIFY3S(dev->l2ad_log_ent_idx, ==, dev->l2ad_log_entries); 10591 10592 abd_buf = zio_buf_alloc(sizeof (*abd_buf)); 10593 abd_buf->abd = abd_get_from_buf(lb, sizeof (*lb)); 10594 lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP); 10595 lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t), KM_SLEEP); 10596 10597 /* link the buffer into the block chain */ 10598 lb->lb_prev_lbp = l2dhdr->dh_start_lbps[1]; 10599 lb->lb_magic = L2ARC_LOG_BLK_MAGIC; 10600 10601 /* 10602 * l2arc_log_blk_commit() may be called multiple times during a single 10603 * l2arc_write_buffers() call. Save the allocated abd buffers in a list 10604 * so we can free them in l2arc_write_done() later on. 10605 */ 10606 list_insert_tail(&cb->l2wcb_abd_list, abd_buf); 10607 10608 /* try to compress the buffer */ 10609 psize = zio_compress_data(ZIO_COMPRESS_LZ4, 10610 abd_buf->abd, (void **) &tmpbuf, sizeof (*lb), 0); 10611 10612 /* a log block is never entirely zero */ 10613 ASSERT(psize != 0); 10614 asize = vdev_psize_to_asize(dev->l2ad_vdev, psize); 10615 ASSERT(asize <= sizeof (*lb)); 10616 10617 /* 10618 * Update the start log block pointer in the device header to point 10619 * to the log block we're about to write. 10620 */ 10621 l2dhdr->dh_start_lbps[1] = l2dhdr->dh_start_lbps[0]; 10622 l2dhdr->dh_start_lbps[0].lbp_daddr = dev->l2ad_hand; 10623 l2dhdr->dh_start_lbps[0].lbp_payload_asize = 10624 dev->l2ad_log_blk_payload_asize; 10625 l2dhdr->dh_start_lbps[0].lbp_payload_start = 10626 dev->l2ad_log_blk_payload_start; 10627 L2BLK_SET_LSIZE( 10628 (&l2dhdr->dh_start_lbps[0])->lbp_prop, sizeof (*lb)); 10629 L2BLK_SET_PSIZE( 10630 (&l2dhdr->dh_start_lbps[0])->lbp_prop, asize); 10631 L2BLK_SET_CHECKSUM( 10632 (&l2dhdr->dh_start_lbps[0])->lbp_prop, 10633 ZIO_CHECKSUM_FLETCHER_4); 10634 if (asize < sizeof (*lb)) { 10635 /* compression succeeded */ 10636 memset(tmpbuf + psize, 0, asize - psize); 10637 L2BLK_SET_COMPRESS( 10638 (&l2dhdr->dh_start_lbps[0])->lbp_prop, 10639 ZIO_COMPRESS_LZ4); 10640 } else { 10641 /* compression failed */ 10642 memcpy(tmpbuf, lb, sizeof (*lb)); 10643 L2BLK_SET_COMPRESS( 10644 (&l2dhdr->dh_start_lbps[0])->lbp_prop, 10645 ZIO_COMPRESS_OFF); 10646 } 10647 10648 /* checksum what we're about to write */ 10649 fletcher_4_native(tmpbuf, asize, NULL, 10650 &l2dhdr->dh_start_lbps[0].lbp_cksum); 10651 10652 abd_free(abd_buf->abd); 10653 10654 /* perform the write itself */ 10655 abd_buf->abd = abd_get_from_buf(tmpbuf, sizeof (*lb)); 10656 abd_take_ownership_of_buf(abd_buf->abd, B_TRUE); 10657 wzio = zio_write_phys(pio, dev->l2ad_vdev, dev->l2ad_hand, 10658 asize, abd_buf->abd, ZIO_CHECKSUM_OFF, NULL, NULL, 10659 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE); 10660 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, zio_t *, wzio); 10661 (void) zio_nowait(wzio); 10662 10663 dev->l2ad_hand += asize; 10664 /* 10665 * Include the committed log block's pointer in the list of pointers 10666 * to log blocks present in the L2ARC device. 10667 */ 10668 memcpy(lb_ptr_buf->lb_ptr, &l2dhdr->dh_start_lbps[0], 10669 sizeof (l2arc_log_blkptr_t)); 10670 mutex_enter(&dev->l2ad_mtx); 10671 list_insert_head(&dev->l2ad_lbptr_list, lb_ptr_buf); 10672 ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize); 10673 ARCSTAT_BUMP(arcstat_l2_log_blk_count); 10674 zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf); 10675 zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf); 10676 mutex_exit(&dev->l2ad_mtx); 10677 vdev_space_update(dev->l2ad_vdev, asize, 0, 0); 10678 10679 /* bump the kstats */ 10680 ARCSTAT_INCR(arcstat_l2_write_bytes, asize); 10681 ARCSTAT_BUMP(arcstat_l2_log_blk_writes); 10682 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, asize); 10683 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio, 10684 dev->l2ad_log_blk_payload_asize / asize); 10685 10686 /* start a new log block */ 10687 dev->l2ad_log_ent_idx = 0; 10688 dev->l2ad_log_blk_payload_asize = 0; 10689 dev->l2ad_log_blk_payload_start = 0; 10690 10691 return (asize); 10692 } 10693 10694 /* 10695 * Validates an L2ARC log block address to make sure that it can be read 10696 * from the provided L2ARC device. 10697 */ 10698 boolean_t 10699 l2arc_log_blkptr_valid(l2arc_dev_t *dev, const l2arc_log_blkptr_t *lbp) 10700 { 10701 /* L2BLK_GET_PSIZE returns aligned size for log blocks */ 10702 uint64_t asize = L2BLK_GET_PSIZE((lbp)->lbp_prop); 10703 uint64_t end = lbp->lbp_daddr + asize - 1; 10704 uint64_t start = lbp->lbp_payload_start; 10705 boolean_t evicted = B_FALSE; 10706 10707 /* 10708 * A log block is valid if all of the following conditions are true: 10709 * - it fits entirely (including its payload) between l2ad_start and 10710 * l2ad_end 10711 * - it has a valid size 10712 * - neither the log block itself nor part of its payload was evicted 10713 * by l2arc_evict(): 10714 * 10715 * l2ad_hand l2ad_evict 10716 * | | lbp_daddr 10717 * | start | | end 10718 * | | | | | 10719 * V V V V V 10720 * l2ad_start ============================================ l2ad_end 10721 * --------------------------|||| 10722 * ^ ^ 10723 * | log block 10724 * payload 10725 */ 10726 10727 evicted = 10728 l2arc_range_check_overlap(start, end, dev->l2ad_hand) || 10729 l2arc_range_check_overlap(start, end, dev->l2ad_evict) || 10730 l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, start) || 10731 l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, end); 10732 10733 return (start >= dev->l2ad_start && end <= dev->l2ad_end && 10734 asize > 0 && asize <= sizeof (l2arc_log_blk_phys_t) && 10735 (!evicted || dev->l2ad_first)); 10736 } 10737 10738 /* 10739 * Inserts ARC buffer header `hdr' into the current L2ARC log block on 10740 * the device. The buffer being inserted must be present in L2ARC. 10741 * Returns B_TRUE if the L2ARC log block is full and needs to be committed 10742 * to L2ARC, or B_FALSE if it still has room for more ARC buffers. 10743 */ 10744 static boolean_t 10745 l2arc_log_blk_insert(l2arc_dev_t *dev, const arc_buf_hdr_t *hdr) 10746 { 10747 l2arc_log_blk_phys_t *lb = &dev->l2ad_log_blk; 10748 l2arc_log_ent_phys_t *le; 10749 10750 if (dev->l2ad_log_entries == 0) 10751 return (B_FALSE); 10752 10753 int index = dev->l2ad_log_ent_idx++; 10754 10755 ASSERT3S(index, <, dev->l2ad_log_entries); 10756 ASSERT(HDR_HAS_L2HDR(hdr)); 10757 10758 le = &lb->lb_entries[index]; 10759 memset(le, 0, sizeof (*le)); 10760 le->le_dva = hdr->b_dva; 10761 le->le_birth = hdr->b_birth; 10762 le->le_daddr = hdr->b_l2hdr.b_daddr; 10763 if (index == 0) 10764 dev->l2ad_log_blk_payload_start = le->le_daddr; 10765 L2BLK_SET_LSIZE((le)->le_prop, HDR_GET_LSIZE(hdr)); 10766 L2BLK_SET_PSIZE((le)->le_prop, HDR_GET_PSIZE(hdr)); 10767 L2BLK_SET_COMPRESS((le)->le_prop, HDR_GET_COMPRESS(hdr)); 10768 le->le_complevel = hdr->b_complevel; 10769 L2BLK_SET_TYPE((le)->le_prop, hdr->b_type); 10770 L2BLK_SET_PROTECTED((le)->le_prop, !!(HDR_PROTECTED(hdr))); 10771 L2BLK_SET_PREFETCH((le)->le_prop, !!(HDR_PREFETCH(hdr))); 10772 L2BLK_SET_STATE((le)->le_prop, hdr->b_l1hdr.b_state->arcs_state); 10773 10774 dev->l2ad_log_blk_payload_asize += vdev_psize_to_asize(dev->l2ad_vdev, 10775 HDR_GET_PSIZE(hdr)); 10776 10777 return (dev->l2ad_log_ent_idx == dev->l2ad_log_entries); 10778 } 10779 10780 /* 10781 * Checks whether a given L2ARC device address sits in a time-sequential 10782 * range. The trick here is that the L2ARC is a rotary buffer, so we can't 10783 * just do a range comparison, we need to handle the situation in which the 10784 * range wraps around the end of the L2ARC device. Arguments: 10785 * bottom -- Lower end of the range to check (written to earlier). 10786 * top -- Upper end of the range to check (written to later). 10787 * check -- The address for which we want to determine if it sits in 10788 * between the top and bottom. 10789 * 10790 * The 3-way conditional below represents the following cases: 10791 * 10792 * bottom < top : Sequentially ordered case: 10793 * <check>--------+-------------------+ 10794 * | (overlap here?) | 10795 * L2ARC dev V V 10796 * |---------------<bottom>============<top>--------------| 10797 * 10798 * bottom > top: Looped-around case: 10799 * <check>--------+------------------+ 10800 * | (overlap here?) | 10801 * L2ARC dev V V 10802 * |===============<top>---------------<bottom>===========| 10803 * ^ ^ 10804 * | (or here?) | 10805 * +---------------+---------<check> 10806 * 10807 * top == bottom : Just a single address comparison. 10808 */ 10809 boolean_t 10810 l2arc_range_check_overlap(uint64_t bottom, uint64_t top, uint64_t check) 10811 { 10812 if (bottom < top) 10813 return (bottom <= check && check <= top); 10814 else if (bottom > top) 10815 return (check <= top || bottom <= check); 10816 else 10817 return (check == top); 10818 } 10819 10820 EXPORT_SYMBOL(arc_buf_size); 10821 EXPORT_SYMBOL(arc_write); 10822 EXPORT_SYMBOL(arc_read); 10823 EXPORT_SYMBOL(arc_buf_info); 10824 EXPORT_SYMBOL(arc_getbuf_func); 10825 EXPORT_SYMBOL(arc_add_prune_callback); 10826 EXPORT_SYMBOL(arc_remove_prune_callback); 10827 10828 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min, param_set_arc_min, 10829 spl_param_get_u64, ZMOD_RW, "Minimum ARC size in bytes"); 10830 10831 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, max, param_set_arc_max, 10832 spl_param_get_u64, ZMOD_RW, "Maximum ARC size in bytes"); 10833 10834 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, meta_balance, UINT, ZMOD_RW, 10835 "Balance between metadata and data on ghost hits."); 10836 10837 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, grow_retry, param_set_arc_int, 10838 param_get_uint, ZMOD_RW, "Seconds before growing ARC size"); 10839 10840 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, shrink_shift, param_set_arc_int, 10841 param_get_uint, ZMOD_RW, "log2(fraction of ARC to reclaim)"); 10842 10843 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, pc_percent, UINT, ZMOD_RW, 10844 "Percent of pagecache to reclaim ARC to"); 10845 10846 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, average_blocksize, UINT, ZMOD_RD, 10847 "Target average block size"); 10848 10849 ZFS_MODULE_PARAM(zfs, zfs_, compressed_arc_enabled, INT, ZMOD_RW, 10850 "Disable compressed ARC buffers"); 10851 10852 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prefetch_ms, param_set_arc_int, 10853 param_get_uint, ZMOD_RW, "Min life of prefetch block in ms"); 10854 10855 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prescient_prefetch_ms, 10856 param_set_arc_int, param_get_uint, ZMOD_RW, 10857 "Min life of prescient prefetched block in ms"); 10858 10859 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_max, U64, ZMOD_RW, 10860 "Max write bytes per interval"); 10861 10862 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_boost, U64, ZMOD_RW, 10863 "Extra write bytes during device warmup"); 10864 10865 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom, U64, ZMOD_RW, 10866 "Number of max device writes to precache"); 10867 10868 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom_boost, U64, ZMOD_RW, 10869 "Compressed l2arc_headroom multiplier"); 10870 10871 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, trim_ahead, U64, ZMOD_RW, 10872 "TRIM ahead L2ARC write size multiplier"); 10873 10874 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_secs, U64, ZMOD_RW, 10875 "Seconds between L2ARC writing"); 10876 10877 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_min_ms, U64, ZMOD_RW, 10878 "Min feed interval in milliseconds"); 10879 10880 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, noprefetch, INT, ZMOD_RW, 10881 "Skip caching prefetched buffers"); 10882 10883 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_again, INT, ZMOD_RW, 10884 "Turbo L2ARC warmup"); 10885 10886 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, norw, INT, ZMOD_RW, 10887 "No reads during writes"); 10888 10889 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, meta_percent, UINT, ZMOD_RW, 10890 "Percent of ARC size allowed for L2ARC-only headers"); 10891 10892 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_enabled, INT, ZMOD_RW, 10893 "Rebuild the L2ARC when importing a pool"); 10894 10895 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_blocks_min_l2size, U64, ZMOD_RW, 10896 "Min size in bytes to write rebuild log blocks in L2ARC"); 10897 10898 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, mfuonly, INT, ZMOD_RW, 10899 "Cache only MFU data from ARC into L2ARC"); 10900 10901 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, exclude_special, INT, ZMOD_RW, 10902 "Exclude dbufs on special vdevs from being cached to L2ARC if set."); 10903 10904 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, lotsfree_percent, param_set_arc_int, 10905 param_get_uint, ZMOD_RW, "System free memory I/O throttle in bytes"); 10906 10907 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, sys_free, param_set_arc_u64, 10908 spl_param_get_u64, ZMOD_RW, "System free memory target size in bytes"); 10909 10910 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit, param_set_arc_u64, 10911 spl_param_get_u64, ZMOD_RW, "Minimum bytes of dnodes in ARC"); 10912 10913 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit_percent, 10914 param_set_arc_int, param_get_uint, ZMOD_RW, 10915 "Percent of ARC meta buffers for dnodes"); 10916 10917 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, dnode_reduce_percent, UINT, ZMOD_RW, 10918 "Percentage of excess dnodes to try to unpin"); 10919 10920 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, eviction_pct, UINT, ZMOD_RW, 10921 "When full, ARC allocation waits for eviction of this % of alloc size"); 10922 10923 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, evict_batch_limit, UINT, ZMOD_RW, 10924 "The number of headers to evict per sublist before moving to the next"); 10925 10926 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, prune_task_threads, INT, ZMOD_RW, 10927 "Number of arc_prune threads"); 10928