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