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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2018, Joyent, Inc.
24 * Copyright (c) 2011, 2020, Delphix. All rights reserved.
25 * Copyright (c) 2014, Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2017, Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
28 * Copyright (c) 2020, George Amanakis. All rights reserved.
29 * Copyright (c) 2019, Klara Inc.
30 * Copyright (c) 2019, Allan Jude
31 * Copyright (c) 2020, The FreeBSD Foundation [1]
33 * [1] Portions of this software were developed by Allan Jude
34 * under sponsorship from the FreeBSD Foundation.
38 * DVA-based Adjustable Replacement Cache
40 * While much of the theory of operation used here is
41 * based on the self-tuning, low overhead replacement cache
42 * presented by Megiddo and Modha at FAST 2003, there are some
43 * significant differences:
45 * 1. The Megiddo and Modha model assumes any page is evictable.
46 * Pages in its cache cannot be "locked" into memory. This makes
47 * the eviction algorithm simple: evict the last page in the list.
48 * This also make the performance characteristics easy to reason
49 * about. Our cache is not so simple. At any given moment, some
50 * subset of the blocks in the cache are un-evictable because we
51 * have handed out a reference to them. Blocks are only evictable
52 * when there are no external references active. This makes
53 * eviction far more problematic: we choose to evict the evictable
54 * blocks that are the "lowest" in the list.
56 * There are times when it is not possible to evict the requested
57 * space. In these circumstances we are unable to adjust the cache
58 * size. To prevent the cache growing unbounded at these times we
59 * implement a "cache throttle" that slows the flow of new data
60 * into the cache until we can make space available.
62 * 2. The Megiddo and Modha model assumes a fixed cache size.
63 * Pages are evicted when the cache is full and there is a cache
64 * miss. Our model has a variable sized cache. It grows with
65 * high use, but also tries to react to memory pressure from the
66 * operating system: decreasing its size when system memory is
69 * 3. The Megiddo and Modha model assumes a fixed page size. All
70 * elements of the cache are therefore exactly the same size. So
71 * when adjusting the cache size following a cache miss, its simply
72 * a matter of choosing a single page to evict. In our model, we
73 * have variable sized cache blocks (ranging from 512 bytes to
74 * 128K bytes). We therefore choose a set of blocks to evict to make
75 * space for a cache miss that approximates as closely as possible
76 * the space used by the new block.
78 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
79 * by N. Megiddo & D. Modha, FAST 2003
85 * A new reference to a cache buffer can be obtained in two
86 * ways: 1) via a hash table lookup using the DVA as a key,
87 * or 2) via one of the ARC lists. The arc_read() interface
88 * uses method 1, while the internal ARC algorithms for
89 * adjusting the cache use method 2. We therefore provide two
90 * types of locks: 1) the hash table lock array, and 2) the
93 * Buffers do not have their own mutexes, rather they rely on the
94 * hash table mutexes for the bulk of their protection (i.e. most
95 * fields in the arc_buf_hdr_t are protected by these mutexes).
97 * buf_hash_find() returns the appropriate mutex (held) when it
98 * locates the requested buffer in the hash table. It returns
99 * NULL for the mutex if the buffer was not in the table.
101 * buf_hash_remove() expects the appropriate hash mutex to be
102 * already held before it is invoked.
104 * Each ARC state also has a mutex which is used to protect the
105 * buffer list associated with the state. When attempting to
106 * obtain a hash table lock while holding an ARC list lock you
107 * must use: mutex_tryenter() to avoid deadlock. Also note that
108 * the active state mutex must be held before the ghost state mutex.
110 * It as also possible to register a callback which is run when the
111 * arc_meta_limit is reached and no buffers can be safely evicted. In
112 * this case the arc user should drop a reference on some arc buffers so
113 * they can be reclaimed and the arc_meta_limit honored. For example,
114 * when using the ZPL each dentry holds a references on a znode. These
115 * dentries must be pruned before the arc buffer holding the znode can
118 * Note that the majority of the performance stats are manipulated
119 * with atomic operations.
121 * The L2ARC uses the l2ad_mtx on each vdev for the following:
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
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.
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).
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.
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.
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.
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
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 * +->+------+ | +-----------+ |
200 * data | |<--------------+ | uncompressed
201 * +------+ compressed, | data
202 * shared +-->+------+
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.
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):
232 * | | arc_buf_t (shared)
233 * | b_buf +------------>+---------+ arc_buf_t
234 * | | |b_next +---->+---------+
235 * | b_pabd +-+ |---------| |b_next +-->NULL
236 * +-----------+ | | | +---------+
238 * | +---------+ | |b_data +-+
239 * +->+------+ | +---------+ |
241 * uncompressed | | | |
244 * | uncompressed | | |
247 * +---------------------------------+
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 bcopy 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.
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.
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
288 #include <sys/spa_impl.h>
289 #include <sys/zio_compress.h>
290 #include <sys/zio_checksum.h>
291 #include <sys/zfs_context.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/zio_checksum.h>
298 #include <sys/multilist.h>
301 #include <sys/fm/fs/zfs.h>
302 #include <sys/callb.h>
303 #include <sys/kstat.h>
304 #include <sys/zthr.h>
305 #include <zfs_fletcher.h>
306 #include <sys/arc_impl.h>
307 #include <sys/trace_zfs.h>
308 #include <sys/aggsum.h>
309 #include <cityhash.h>
310 #include <sys/vdev_trim.h>
311 #include <sys/zstd/zstd.h>
314 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
315 boolean_t arc_watch
= B_FALSE
;
319 * This thread's job is to keep enough free memory in the system, by
320 * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves
321 * arc_available_memory().
323 static zthr_t
*arc_reap_zthr
;
326 * This thread's job is to keep arc_size under arc_c, by calling
327 * arc_evict(), which improves arc_is_overflowing().
329 static zthr_t
*arc_evict_zthr
;
331 static kmutex_t arc_evict_lock
;
332 static boolean_t arc_evict_needed
= B_FALSE
;
335 * Count of bytes evicted since boot.
337 static uint64_t arc_evict_count
;
340 * List of arc_evict_waiter_t's, representing threads waiting for the
341 * arc_evict_count to reach specific values.
343 static list_t arc_evict_waiters
;
346 * When arc_is_overflowing(), arc_get_data_impl() waits for this percent of
347 * the requested amount of data to be evicted. For example, by default for
348 * every 2KB that's evicted, 1KB of it may be "reused" by a new allocation.
349 * Since this is above 100%, it ensures that progress is made towards getting
350 * arc_size under arc_c. Since this is finite, it ensures that allocations
351 * can still happen, even during the potentially long time that arc_size is
354 int zfs_arc_eviction_pct
= 200;
357 * The number of headers to evict in arc_evict_state_impl() before
358 * dropping the sublist lock and evicting from another sublist. A lower
359 * value means we're more likely to evict the "correct" header (i.e. the
360 * oldest header in the arc state), but comes with higher overhead
361 * (i.e. more invocations of arc_evict_state_impl()).
363 int zfs_arc_evict_batch_limit
= 10;
365 /* number of seconds before growing cache again */
366 int arc_grow_retry
= 5;
369 * Minimum time between calls to arc_kmem_reap_soon().
371 int arc_kmem_cache_reap_retry_ms
= 1000;
373 /* shift of arc_c for calculating overflow limit in arc_get_data_impl */
374 int zfs_arc_overflow_shift
= 8;
376 /* shift of arc_c for calculating both min and max arc_p */
377 int arc_p_min_shift
= 4;
379 /* log2(fraction of arc to reclaim) */
380 int arc_shrink_shift
= 7;
382 /* percent of pagecache to reclaim arc to */
384 uint_t zfs_arc_pc_percent
= 0;
388 * log2(fraction of ARC which must be free to allow growing).
389 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
390 * when reading a new block into the ARC, we will evict an equal-sized block
393 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
394 * we will still not allow it to grow.
396 int arc_no_grow_shift
= 5;
400 * minimum lifespan of a prefetch block in clock ticks
401 * (initialized in arc_init())
403 static int arc_min_prefetch_ms
;
404 static int arc_min_prescient_prefetch_ms
;
407 * If this percent of memory is free, don't throttle.
409 int arc_lotsfree_percent
= 10;
412 * The arc has filled available memory and has now warmed up.
417 * These tunables are for performance analysis.
419 unsigned long zfs_arc_max
= 0;
420 unsigned long zfs_arc_min
= 0;
421 unsigned long zfs_arc_meta_limit
= 0;
422 unsigned long zfs_arc_meta_min
= 0;
423 unsigned long zfs_arc_dnode_limit
= 0;
424 unsigned long zfs_arc_dnode_reduce_percent
= 10;
425 int zfs_arc_grow_retry
= 0;
426 int zfs_arc_shrink_shift
= 0;
427 int zfs_arc_p_min_shift
= 0;
428 int zfs_arc_average_blocksize
= 8 * 1024; /* 8KB */
431 * ARC dirty data constraints for arc_tempreserve_space() throttle.
433 unsigned long zfs_arc_dirty_limit_percent
= 50; /* total dirty data limit */
434 unsigned long zfs_arc_anon_limit_percent
= 25; /* anon block dirty limit */
435 unsigned long zfs_arc_pool_dirty_percent
= 20; /* each pool's anon allowance */
438 * Enable or disable compressed arc buffers.
440 int zfs_compressed_arc_enabled
= B_TRUE
;
443 * ARC will evict meta buffers that exceed arc_meta_limit. This
444 * tunable make arc_meta_limit adjustable for different workloads.
446 unsigned long zfs_arc_meta_limit_percent
= 75;
449 * Percentage that can be consumed by dnodes of ARC meta buffers.
451 unsigned long zfs_arc_dnode_limit_percent
= 10;
454 * These tunables are Linux specific
456 unsigned long zfs_arc_sys_free
= 0;
457 int zfs_arc_min_prefetch_ms
= 0;
458 int zfs_arc_min_prescient_prefetch_ms
= 0;
459 int zfs_arc_p_dampener_disable
= 1;
460 int zfs_arc_meta_prune
= 10000;
461 int zfs_arc_meta_strategy
= ARC_STRATEGY_META_BALANCED
;
462 int zfs_arc_meta_adjust_restarts
= 4096;
463 int zfs_arc_lotsfree_percent
= 10;
466 arc_state_t ARC_anon
;
468 arc_state_t ARC_mru_ghost
;
470 arc_state_t ARC_mfu_ghost
;
471 arc_state_t ARC_l2c_only
;
473 arc_stats_t arc_stats
= {
474 { "hits", KSTAT_DATA_UINT64
},
475 { "misses", KSTAT_DATA_UINT64
},
476 { "demand_data_hits", KSTAT_DATA_UINT64
},
477 { "demand_data_misses", KSTAT_DATA_UINT64
},
478 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
479 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
480 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
481 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
482 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
483 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
484 { "mru_hits", KSTAT_DATA_UINT64
},
485 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
486 { "mfu_hits", KSTAT_DATA_UINT64
},
487 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
488 { "deleted", KSTAT_DATA_UINT64
},
489 { "mutex_miss", KSTAT_DATA_UINT64
},
490 { "access_skip", KSTAT_DATA_UINT64
},
491 { "evict_skip", KSTAT_DATA_UINT64
},
492 { "evict_not_enough", KSTAT_DATA_UINT64
},
493 { "evict_l2_cached", KSTAT_DATA_UINT64
},
494 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
495 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
496 { "evict_l2_skip", KSTAT_DATA_UINT64
},
497 { "hash_elements", KSTAT_DATA_UINT64
},
498 { "hash_elements_max", KSTAT_DATA_UINT64
},
499 { "hash_collisions", KSTAT_DATA_UINT64
},
500 { "hash_chains", KSTAT_DATA_UINT64
},
501 { "hash_chain_max", KSTAT_DATA_UINT64
},
502 { "p", KSTAT_DATA_UINT64
},
503 { "c", KSTAT_DATA_UINT64
},
504 { "c_min", KSTAT_DATA_UINT64
},
505 { "c_max", KSTAT_DATA_UINT64
},
506 { "size", KSTAT_DATA_UINT64
},
507 { "compressed_size", KSTAT_DATA_UINT64
},
508 { "uncompressed_size", KSTAT_DATA_UINT64
},
509 { "overhead_size", KSTAT_DATA_UINT64
},
510 { "hdr_size", KSTAT_DATA_UINT64
},
511 { "data_size", KSTAT_DATA_UINT64
},
512 { "metadata_size", KSTAT_DATA_UINT64
},
513 { "dbuf_size", KSTAT_DATA_UINT64
},
514 { "dnode_size", KSTAT_DATA_UINT64
},
515 { "bonus_size", KSTAT_DATA_UINT64
},
516 #if defined(COMPAT_FREEBSD11)
517 { "other_size", KSTAT_DATA_UINT64
},
519 { "anon_size", KSTAT_DATA_UINT64
},
520 { "anon_evictable_data", KSTAT_DATA_UINT64
},
521 { "anon_evictable_metadata", KSTAT_DATA_UINT64
},
522 { "mru_size", KSTAT_DATA_UINT64
},
523 { "mru_evictable_data", KSTAT_DATA_UINT64
},
524 { "mru_evictable_metadata", KSTAT_DATA_UINT64
},
525 { "mru_ghost_size", KSTAT_DATA_UINT64
},
526 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64
},
527 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
528 { "mfu_size", KSTAT_DATA_UINT64
},
529 { "mfu_evictable_data", KSTAT_DATA_UINT64
},
530 { "mfu_evictable_metadata", KSTAT_DATA_UINT64
},
531 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
532 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64
},
533 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
534 { "l2_hits", KSTAT_DATA_UINT64
},
535 { "l2_misses", KSTAT_DATA_UINT64
},
536 { "l2_feeds", KSTAT_DATA_UINT64
},
537 { "l2_rw_clash", KSTAT_DATA_UINT64
},
538 { "l2_read_bytes", KSTAT_DATA_UINT64
},
539 { "l2_write_bytes", KSTAT_DATA_UINT64
},
540 { "l2_writes_sent", KSTAT_DATA_UINT64
},
541 { "l2_writes_done", KSTAT_DATA_UINT64
},
542 { "l2_writes_error", KSTAT_DATA_UINT64
},
543 { "l2_writes_lock_retry", KSTAT_DATA_UINT64
},
544 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
545 { "l2_evict_reading", KSTAT_DATA_UINT64
},
546 { "l2_evict_l1cached", KSTAT_DATA_UINT64
},
547 { "l2_free_on_write", KSTAT_DATA_UINT64
},
548 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
549 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
550 { "l2_io_error", KSTAT_DATA_UINT64
},
551 { "l2_size", KSTAT_DATA_UINT64
},
552 { "l2_asize", KSTAT_DATA_UINT64
},
553 { "l2_hdr_size", KSTAT_DATA_UINT64
},
554 { "l2_log_blk_writes", KSTAT_DATA_UINT64
},
555 { "l2_log_blk_avg_asize", KSTAT_DATA_UINT64
},
556 { "l2_log_blk_asize", KSTAT_DATA_UINT64
},
557 { "l2_log_blk_count", KSTAT_DATA_UINT64
},
558 { "l2_data_to_meta_ratio", KSTAT_DATA_UINT64
},
559 { "l2_rebuild_success", KSTAT_DATA_UINT64
},
560 { "l2_rebuild_unsupported", KSTAT_DATA_UINT64
},
561 { "l2_rebuild_io_errors", KSTAT_DATA_UINT64
},
562 { "l2_rebuild_dh_errors", KSTAT_DATA_UINT64
},
563 { "l2_rebuild_cksum_lb_errors", KSTAT_DATA_UINT64
},
564 { "l2_rebuild_lowmem", KSTAT_DATA_UINT64
},
565 { "l2_rebuild_size", KSTAT_DATA_UINT64
},
566 { "l2_rebuild_asize", KSTAT_DATA_UINT64
},
567 { "l2_rebuild_bufs", KSTAT_DATA_UINT64
},
568 { "l2_rebuild_bufs_precached", KSTAT_DATA_UINT64
},
569 { "l2_rebuild_log_blks", KSTAT_DATA_UINT64
},
570 { "memory_throttle_count", KSTAT_DATA_UINT64
},
571 { "memory_direct_count", KSTAT_DATA_UINT64
},
572 { "memory_indirect_count", KSTAT_DATA_UINT64
},
573 { "memory_all_bytes", KSTAT_DATA_UINT64
},
574 { "memory_free_bytes", KSTAT_DATA_UINT64
},
575 { "memory_available_bytes", KSTAT_DATA_INT64
},
576 { "arc_no_grow", KSTAT_DATA_UINT64
},
577 { "arc_tempreserve", KSTAT_DATA_UINT64
},
578 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
579 { "arc_prune", KSTAT_DATA_UINT64
},
580 { "arc_meta_used", KSTAT_DATA_UINT64
},
581 { "arc_meta_limit", KSTAT_DATA_UINT64
},
582 { "arc_dnode_limit", KSTAT_DATA_UINT64
},
583 { "arc_meta_max", KSTAT_DATA_UINT64
},
584 { "arc_meta_min", KSTAT_DATA_UINT64
},
585 { "async_upgrade_sync", KSTAT_DATA_UINT64
},
586 { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64
},
587 { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64
},
588 { "arc_need_free", KSTAT_DATA_UINT64
},
589 { "arc_sys_free", KSTAT_DATA_UINT64
},
590 { "arc_raw_size", KSTAT_DATA_UINT64
},
591 { "cached_only_in_progress", KSTAT_DATA_UINT64
},
592 { "abd_chunk_waste_size", KSTAT_DATA_UINT64
},
595 #define ARCSTAT_MAX(stat, val) { \
597 while ((val) > (m = arc_stats.stat.value.ui64) && \
598 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
602 #define ARCSTAT_MAXSTAT(stat) \
603 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
606 * We define a macro to allow ARC hits/misses to be easily broken down by
607 * two separate conditions, giving a total of four different subtypes for
608 * each of hits and misses (so eight statistics total).
610 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
613 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
615 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
619 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
621 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
626 * This macro allows us to use kstats as floating averages. Each time we
627 * update this kstat, we first factor it and the update value by
628 * ARCSTAT_AVG_FACTOR to shrink the new value's contribution to the overall
629 * average. This macro assumes that integer loads and stores are atomic, but
630 * is not safe for multiple writers updating the kstat in parallel (only the
631 * last writer's update will remain).
633 #define ARCSTAT_F_AVG_FACTOR 3
634 #define ARCSTAT_F_AVG(stat, value) \
636 uint64_t x = ARCSTAT(stat); \
637 x = x - x / ARCSTAT_F_AVG_FACTOR + \
638 (value) / ARCSTAT_F_AVG_FACTOR; \
644 static arc_state_t
*arc_anon
;
645 static arc_state_t
*arc_mru_ghost
;
646 static arc_state_t
*arc_mfu_ghost
;
647 static arc_state_t
*arc_l2c_only
;
649 arc_state_t
*arc_mru
;
650 arc_state_t
*arc_mfu
;
653 * There are several ARC variables that are critical to export as kstats --
654 * but we don't want to have to grovel around in the kstat whenever we wish to
655 * manipulate them. For these variables, we therefore define them to be in
656 * terms of the statistic variable. This assures that we are not introducing
657 * the possibility of inconsistency by having shadow copies of the variables,
658 * while still allowing the code to be readable.
660 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
661 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
662 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
663 /* max size for dnodes */
664 #define arc_dnode_size_limit ARCSTAT(arcstat_dnode_limit)
665 #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */
666 #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
667 #define arc_need_free ARCSTAT(arcstat_need_free) /* waiting to be evicted */
669 /* size of all b_rabd's in entire arc */
670 #define arc_raw_size ARCSTAT(arcstat_raw_size)
671 /* compressed size of entire arc */
672 #define arc_compressed_size ARCSTAT(arcstat_compressed_size)
673 /* uncompressed size of entire arc */
674 #define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size)
675 /* number of bytes in the arc from arc_buf_t's */
676 #define arc_overhead_size ARCSTAT(arcstat_overhead_size)
679 * There are also some ARC variables that we want to export, but that are
680 * updated so often that having the canonical representation be the statistic
681 * variable causes a performance bottleneck. We want to use aggsum_t's for these
682 * instead, but still be able to export the kstat in the same way as before.
683 * The solution is to always use the aggsum version, except in the kstat update
687 aggsum_t arc_meta_used
;
688 aggsum_t astat_data_size
;
689 aggsum_t astat_metadata_size
;
690 aggsum_t astat_dbuf_size
;
691 aggsum_t astat_dnode_size
;
692 aggsum_t astat_bonus_size
;
693 aggsum_t astat_hdr_size
;
694 aggsum_t astat_l2_hdr_size
;
695 aggsum_t astat_abd_chunk_waste_size
;
697 hrtime_t arc_growtime
;
698 list_t arc_prune_list
;
699 kmutex_t arc_prune_mtx
;
700 taskq_t
*arc_prune_taskq
;
702 #define GHOST_STATE(state) \
703 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
704 (state) == arc_l2c_only)
706 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
707 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
708 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
709 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
710 #define HDR_PRESCIENT_PREFETCH(hdr) \
711 ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH)
712 #define HDR_COMPRESSION_ENABLED(hdr) \
713 ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC)
715 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
716 #define HDR_L2_READING(hdr) \
717 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
718 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
719 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
720 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
721 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
722 #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED)
723 #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH)
724 #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA)
726 #define HDR_ISTYPE_METADATA(hdr) \
727 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
728 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
730 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
731 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
732 #define HDR_HAS_RABD(hdr) \
733 (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \
734 (hdr)->b_crypt_hdr.b_rabd != NULL)
735 #define HDR_ENCRYPTED(hdr) \
736 (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
737 #define HDR_AUTHENTICATED(hdr) \
738 (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
740 /* For storing compression mode in b_flags */
741 #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1)
743 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \
744 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS))
745 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \
746 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp));
748 #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL)
749 #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED)
750 #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED)
751 #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED)
757 #define HDR_FULL_CRYPT_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
758 #define HDR_FULL_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_crypt_hdr))
759 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
762 * Hash table routines
765 #define HT_LOCK_ALIGN 64
766 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
771 unsigned char pad
[HT_LOCK_PAD
];
775 #define BUF_LOCKS 8192
776 typedef struct buf_hash_table
{
778 arc_buf_hdr_t
**ht_table
;
779 struct ht_lock ht_locks
[BUF_LOCKS
];
782 static buf_hash_table_t buf_hash_table
;
784 #define BUF_HASH_INDEX(spa, dva, birth) \
785 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
786 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
787 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
788 #define HDR_LOCK(hdr) \
789 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
791 uint64_t zfs_crc64_table
[256];
797 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
798 #define L2ARC_HEADROOM 2 /* num of writes */
801 * If we discover during ARC scan any buffers to be compressed, we boost
802 * our headroom for the next scanning cycle by this percentage multiple.
804 #define L2ARC_HEADROOM_BOOST 200
805 #define L2ARC_FEED_SECS 1 /* caching interval secs */
806 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
809 * We can feed L2ARC from two states of ARC buffers, mru and mfu,
810 * and each of the state has two types: data and metadata.
812 #define L2ARC_FEED_TYPES 4
814 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
815 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
817 /* L2ARC Performance Tunables */
818 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
819 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
820 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
821 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
822 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
823 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
824 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
825 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
826 int l2arc_norw
= B_FALSE
; /* no reads during writes */
827 int l2arc_meta_percent
= 33; /* limit on headers size */
832 static list_t L2ARC_dev_list
; /* device list */
833 static list_t
*l2arc_dev_list
; /* device list pointer */
834 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
835 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
836 static list_t L2ARC_free_on_write
; /* free after write buf list */
837 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
838 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
839 static uint64_t l2arc_ndev
; /* number of devices */
841 typedef struct l2arc_read_callback
{
842 arc_buf_hdr_t
*l2rcb_hdr
; /* read header */
843 blkptr_t l2rcb_bp
; /* original blkptr */
844 zbookmark_phys_t l2rcb_zb
; /* original bookmark */
845 int l2rcb_flags
; /* original flags */
846 abd_t
*l2rcb_abd
; /* temporary buffer */
847 } l2arc_read_callback_t
;
849 typedef struct l2arc_data_free
{
850 /* protected by l2arc_free_on_write_mtx */
853 arc_buf_contents_t l2df_type
;
854 list_node_t l2df_list_node
;
857 typedef enum arc_fill_flags
{
858 ARC_FILL_LOCKED
= 1 << 0, /* hdr lock is held */
859 ARC_FILL_COMPRESSED
= 1 << 1, /* fill with compressed data */
860 ARC_FILL_ENCRYPTED
= 1 << 2, /* fill with encrypted data */
861 ARC_FILL_NOAUTH
= 1 << 3, /* don't attempt to authenticate */
862 ARC_FILL_IN_PLACE
= 1 << 4 /* fill in place (special case) */
865 static kmutex_t l2arc_feed_thr_lock
;
866 static kcondvar_t l2arc_feed_thr_cv
;
867 static uint8_t l2arc_thread_exit
;
869 static kmutex_t l2arc_rebuild_thr_lock
;
870 static kcondvar_t l2arc_rebuild_thr_cv
;
872 enum arc_hdr_alloc_flags
{
873 ARC_HDR_ALLOC_RDATA
= 0x1,
874 ARC_HDR_DO_ADAPT
= 0x2,
878 static abd_t
*arc_get_data_abd(arc_buf_hdr_t
*, uint64_t, void *, boolean_t
);
879 static void *arc_get_data_buf(arc_buf_hdr_t
*, uint64_t, void *);
880 static void arc_get_data_impl(arc_buf_hdr_t
*, uint64_t, void *, boolean_t
);
881 static void arc_free_data_abd(arc_buf_hdr_t
*, abd_t
*, uint64_t, void *);
882 static void arc_free_data_buf(arc_buf_hdr_t
*, void *, uint64_t, void *);
883 static void arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
);
884 static void arc_hdr_free_abd(arc_buf_hdr_t
*, boolean_t
);
885 static void arc_hdr_alloc_abd(arc_buf_hdr_t
*, int);
886 static void arc_access(arc_buf_hdr_t
*, kmutex_t
*);
887 static void arc_buf_watch(arc_buf_t
*);
889 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t
*);
890 static uint32_t arc_bufc_to_flags(arc_buf_contents_t
);
891 static inline void arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
892 static inline void arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
894 static boolean_t
l2arc_write_eligible(uint64_t, arc_buf_hdr_t
*);
895 static void l2arc_read_done(zio_t
*);
896 static void l2arc_do_free_on_write(void);
899 * l2arc_mfuonly : A ZFS module parameter that controls whether only MFU
900 * metadata and data are cached from ARC into L2ARC.
902 int l2arc_mfuonly
= 0;
906 * l2arc_trim_ahead : A ZFS module parameter that controls how much ahead of
907 * the current write size (l2arc_write_max) we should TRIM if we
908 * have filled the device. It is defined as a percentage of the
909 * write size. If set to 100 we trim twice the space required to
910 * accommodate upcoming writes. A minimum of 64MB will be trimmed.
911 * It also enables TRIM of the whole L2ARC device upon creation or
912 * addition to an existing pool or if the header of the device is
913 * invalid upon importing a pool or onlining a cache device. The
914 * default is 0, which disables TRIM on L2ARC altogether as it can
915 * put significant stress on the underlying storage devices. This
916 * will vary depending of how well the specific device handles
919 unsigned long l2arc_trim_ahead
= 0;
922 * Performance tuning of L2ARC persistence:
924 * l2arc_rebuild_enabled : A ZFS module parameter that controls whether adding
925 * an L2ARC device (either at pool import or later) will attempt
926 * to rebuild L2ARC buffer contents.
927 * l2arc_rebuild_blocks_min_l2size : A ZFS module parameter that controls
928 * whether log blocks are written to the L2ARC device. If the L2ARC
929 * device is less than 1GB, the amount of data l2arc_evict()
930 * evicts is significant compared to the amount of restored L2ARC
931 * data. In this case do not write log blocks in L2ARC in order
932 * not to waste space.
934 int l2arc_rebuild_enabled
= B_TRUE
;
935 unsigned long l2arc_rebuild_blocks_min_l2size
= 1024 * 1024 * 1024;
937 /* L2ARC persistence rebuild control routines. */
938 void l2arc_rebuild_vdev(vdev_t
*vd
, boolean_t reopen
);
939 static void l2arc_dev_rebuild_thread(void *arg
);
940 static int l2arc_rebuild(l2arc_dev_t
*dev
);
942 /* L2ARC persistence read I/O routines. */
943 static int l2arc_dev_hdr_read(l2arc_dev_t
*dev
);
944 static int l2arc_log_blk_read(l2arc_dev_t
*dev
,
945 const l2arc_log_blkptr_t
*this_lp
, const l2arc_log_blkptr_t
*next_lp
,
946 l2arc_log_blk_phys_t
*this_lb
, l2arc_log_blk_phys_t
*next_lb
,
947 zio_t
*this_io
, zio_t
**next_io
);
948 static zio_t
*l2arc_log_blk_fetch(vdev_t
*vd
,
949 const l2arc_log_blkptr_t
*lp
, l2arc_log_blk_phys_t
*lb
);
950 static void l2arc_log_blk_fetch_abort(zio_t
*zio
);
952 /* L2ARC persistence block restoration routines. */
953 static void l2arc_log_blk_restore(l2arc_dev_t
*dev
,
954 const l2arc_log_blk_phys_t
*lb
, uint64_t lb_asize
, uint64_t lb_daddr
);
955 static void l2arc_hdr_restore(const l2arc_log_ent_phys_t
*le
,
958 /* L2ARC persistence write I/O routines. */
959 static void l2arc_log_blk_commit(l2arc_dev_t
*dev
, zio_t
*pio
,
960 l2arc_write_callback_t
*cb
);
962 /* L2ARC persistence auxiliary routines. */
963 boolean_t
l2arc_log_blkptr_valid(l2arc_dev_t
*dev
,
964 const l2arc_log_blkptr_t
*lbp
);
965 static boolean_t
l2arc_log_blk_insert(l2arc_dev_t
*dev
,
966 const arc_buf_hdr_t
*ab
);
967 boolean_t
l2arc_range_check_overlap(uint64_t bottom
,
968 uint64_t top
, uint64_t check
);
969 static void l2arc_blk_fetch_done(zio_t
*zio
);
970 static inline uint64_t
971 l2arc_log_blk_overhead(uint64_t write_sz
, l2arc_dev_t
*dev
);
974 * We use Cityhash for this. It's fast, and has good hash properties without
975 * requiring any large static buffers.
978 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
980 return (cityhash4(spa
, dva
->dva_word
[0], dva
->dva_word
[1], birth
));
983 #define HDR_EMPTY(hdr) \
984 ((hdr)->b_dva.dva_word[0] == 0 && \
985 (hdr)->b_dva.dva_word[1] == 0)
987 #define HDR_EMPTY_OR_LOCKED(hdr) \
988 (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr)))
990 #define HDR_EQUAL(spa, dva, birth, hdr) \
991 ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
992 ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
993 ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa)
996 buf_discard_identity(arc_buf_hdr_t
*hdr
)
998 hdr
->b_dva
.dva_word
[0] = 0;
999 hdr
->b_dva
.dva_word
[1] = 0;
1003 static arc_buf_hdr_t
*
1004 buf_hash_find(uint64_t spa
, const blkptr_t
*bp
, kmutex_t
**lockp
)
1006 const dva_t
*dva
= BP_IDENTITY(bp
);
1007 uint64_t birth
= BP_PHYSICAL_BIRTH(bp
);
1008 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
1009 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1012 mutex_enter(hash_lock
);
1013 for (hdr
= buf_hash_table
.ht_table
[idx
]; hdr
!= NULL
;
1014 hdr
= hdr
->b_hash_next
) {
1015 if (HDR_EQUAL(spa
, dva
, birth
, hdr
)) {
1020 mutex_exit(hash_lock
);
1026 * Insert an entry into the hash table. If there is already an element
1027 * equal to elem in the hash table, then the already existing element
1028 * will be returned and the new element will not be inserted.
1029 * Otherwise returns NULL.
1030 * If lockp == NULL, the caller is assumed to already hold the hash lock.
1032 static arc_buf_hdr_t
*
1033 buf_hash_insert(arc_buf_hdr_t
*hdr
, kmutex_t
**lockp
)
1035 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1036 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1037 arc_buf_hdr_t
*fhdr
;
1040 ASSERT(!DVA_IS_EMPTY(&hdr
->b_dva
));
1041 ASSERT(hdr
->b_birth
!= 0);
1042 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1044 if (lockp
!= NULL
) {
1046 mutex_enter(hash_lock
);
1048 ASSERT(MUTEX_HELD(hash_lock
));
1051 for (fhdr
= buf_hash_table
.ht_table
[idx
], i
= 0; fhdr
!= NULL
;
1052 fhdr
= fhdr
->b_hash_next
, i
++) {
1053 if (HDR_EQUAL(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
, fhdr
))
1057 hdr
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
1058 buf_hash_table
.ht_table
[idx
] = hdr
;
1059 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1061 /* collect some hash table performance data */
1063 ARCSTAT_BUMP(arcstat_hash_collisions
);
1065 ARCSTAT_BUMP(arcstat_hash_chains
);
1067 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
1070 ARCSTAT_BUMP(arcstat_hash_elements
);
1071 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
1077 buf_hash_remove(arc_buf_hdr_t
*hdr
)
1079 arc_buf_hdr_t
*fhdr
, **hdrp
;
1080 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1082 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
1083 ASSERT(HDR_IN_HASH_TABLE(hdr
));
1085 hdrp
= &buf_hash_table
.ht_table
[idx
];
1086 while ((fhdr
= *hdrp
) != hdr
) {
1087 ASSERT3P(fhdr
, !=, NULL
);
1088 hdrp
= &fhdr
->b_hash_next
;
1090 *hdrp
= hdr
->b_hash_next
;
1091 hdr
->b_hash_next
= NULL
;
1092 arc_hdr_clear_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1094 /* collect some hash table performance data */
1095 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
1097 if (buf_hash_table
.ht_table
[idx
] &&
1098 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
1099 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
1103 * Global data structures and functions for the buf kmem cache.
1106 static kmem_cache_t
*hdr_full_cache
;
1107 static kmem_cache_t
*hdr_full_crypt_cache
;
1108 static kmem_cache_t
*hdr_l2only_cache
;
1109 static kmem_cache_t
*buf_cache
;
1116 #if defined(_KERNEL)
1118 * Large allocations which do not require contiguous pages
1119 * should be using vmem_free() in the linux kernel\
1121 vmem_free(buf_hash_table
.ht_table
,
1122 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1124 kmem_free(buf_hash_table
.ht_table
,
1125 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1127 for (i
= 0; i
< BUF_LOCKS
; i
++)
1128 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
1129 kmem_cache_destroy(hdr_full_cache
);
1130 kmem_cache_destroy(hdr_full_crypt_cache
);
1131 kmem_cache_destroy(hdr_l2only_cache
);
1132 kmem_cache_destroy(buf_cache
);
1136 * Constructor callback - called when the cache is empty
1137 * and a new buf is requested.
1141 hdr_full_cons(void *vbuf
, void *unused
, int kmflag
)
1143 arc_buf_hdr_t
*hdr
= vbuf
;
1145 bzero(hdr
, HDR_FULL_SIZE
);
1146 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
1147 cv_init(&hdr
->b_l1hdr
.b_cv
, NULL
, CV_DEFAULT
, NULL
);
1148 zfs_refcount_create(&hdr
->b_l1hdr
.b_refcnt
);
1149 mutex_init(&hdr
->b_l1hdr
.b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1150 list_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1151 list_link_init(&hdr
->b_l2hdr
.b_l2node
);
1152 multilist_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1153 arc_space_consume(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1160 hdr_full_crypt_cons(void *vbuf
, void *unused
, int kmflag
)
1162 arc_buf_hdr_t
*hdr
= vbuf
;
1164 hdr_full_cons(vbuf
, unused
, kmflag
);
1165 bzero(&hdr
->b_crypt_hdr
, sizeof (hdr
->b_crypt_hdr
));
1166 arc_space_consume(sizeof (hdr
->b_crypt_hdr
), ARC_SPACE_HDRS
);
1173 hdr_l2only_cons(void *vbuf
, void *unused
, int kmflag
)
1175 arc_buf_hdr_t
*hdr
= vbuf
;
1177 bzero(hdr
, HDR_L2ONLY_SIZE
);
1178 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1185 buf_cons(void *vbuf
, void *unused
, int kmflag
)
1187 arc_buf_t
*buf
= vbuf
;
1189 bzero(buf
, sizeof (arc_buf_t
));
1190 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1191 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1197 * Destructor callback - called when a cached buf is
1198 * no longer required.
1202 hdr_full_dest(void *vbuf
, void *unused
)
1204 arc_buf_hdr_t
*hdr
= vbuf
;
1206 ASSERT(HDR_EMPTY(hdr
));
1207 cv_destroy(&hdr
->b_l1hdr
.b_cv
);
1208 zfs_refcount_destroy(&hdr
->b_l1hdr
.b_refcnt
);
1209 mutex_destroy(&hdr
->b_l1hdr
.b_freeze_lock
);
1210 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1211 arc_space_return(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1216 hdr_full_crypt_dest(void *vbuf
, void *unused
)
1218 arc_buf_hdr_t
*hdr
= vbuf
;
1220 hdr_full_dest(vbuf
, unused
);
1221 arc_space_return(sizeof (hdr
->b_crypt_hdr
), ARC_SPACE_HDRS
);
1226 hdr_l2only_dest(void *vbuf
, void *unused
)
1228 arc_buf_hdr_t
*hdr __maybe_unused
= vbuf
;
1230 ASSERT(HDR_EMPTY(hdr
));
1231 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1236 buf_dest(void *vbuf
, void *unused
)
1238 arc_buf_t
*buf
= vbuf
;
1240 mutex_destroy(&buf
->b_evict_lock
);
1241 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1247 uint64_t *ct
= NULL
;
1248 uint64_t hsize
= 1ULL << 12;
1252 * The hash table is big enough to fill all of physical memory
1253 * with an average block size of zfs_arc_average_blocksize (default 8K).
1254 * By default, the table will take up
1255 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1257 while (hsize
* zfs_arc_average_blocksize
< arc_all_memory())
1260 buf_hash_table
.ht_mask
= hsize
- 1;
1261 #if defined(_KERNEL)
1263 * Large allocations which do not require contiguous pages
1264 * should be using vmem_alloc() in the linux kernel
1266 buf_hash_table
.ht_table
=
1267 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1269 buf_hash_table
.ht_table
=
1270 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1272 if (buf_hash_table
.ht_table
== NULL
) {
1273 ASSERT(hsize
> (1ULL << 8));
1278 hdr_full_cache
= kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE
,
1279 0, hdr_full_cons
, hdr_full_dest
, NULL
, NULL
, NULL
, 0);
1280 hdr_full_crypt_cache
= kmem_cache_create("arc_buf_hdr_t_full_crypt",
1281 HDR_FULL_CRYPT_SIZE
, 0, hdr_full_crypt_cons
, hdr_full_crypt_dest
,
1282 NULL
, NULL
, NULL
, 0);
1283 hdr_l2only_cache
= kmem_cache_create("arc_buf_hdr_t_l2only",
1284 HDR_L2ONLY_SIZE
, 0, hdr_l2only_cons
, hdr_l2only_dest
, NULL
,
1286 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1287 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1289 for (i
= 0; i
< 256; i
++)
1290 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1291 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1293 for (i
= 0; i
< BUF_LOCKS
; i
++) {
1294 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
1295 NULL
, MUTEX_DEFAULT
, NULL
);
1299 #define ARC_MINTIME (hz>>4) /* 62 ms */
1302 * This is the size that the buf occupies in memory. If the buf is compressed,
1303 * it will correspond to the compressed size. You should use this method of
1304 * getting the buf size unless you explicitly need the logical size.
1307 arc_buf_size(arc_buf_t
*buf
)
1309 return (ARC_BUF_COMPRESSED(buf
) ?
1310 HDR_GET_PSIZE(buf
->b_hdr
) : HDR_GET_LSIZE(buf
->b_hdr
));
1314 arc_buf_lsize(arc_buf_t
*buf
)
1316 return (HDR_GET_LSIZE(buf
->b_hdr
));
1320 * This function will return B_TRUE if the buffer is encrypted in memory.
1321 * This buffer can be decrypted by calling arc_untransform().
1324 arc_is_encrypted(arc_buf_t
*buf
)
1326 return (ARC_BUF_ENCRYPTED(buf
) != 0);
1330 * Returns B_TRUE if the buffer represents data that has not had its MAC
1334 arc_is_unauthenticated(arc_buf_t
*buf
)
1336 return (HDR_NOAUTH(buf
->b_hdr
) != 0);
1340 arc_get_raw_params(arc_buf_t
*buf
, boolean_t
*byteorder
, uint8_t *salt
,
1341 uint8_t *iv
, uint8_t *mac
)
1343 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1345 ASSERT(HDR_PROTECTED(hdr
));
1347 bcopy(hdr
->b_crypt_hdr
.b_salt
, salt
, ZIO_DATA_SALT_LEN
);
1348 bcopy(hdr
->b_crypt_hdr
.b_iv
, iv
, ZIO_DATA_IV_LEN
);
1349 bcopy(hdr
->b_crypt_hdr
.b_mac
, mac
, ZIO_DATA_MAC_LEN
);
1350 *byteorder
= (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
1351 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
1355 * Indicates how this buffer is compressed in memory. If it is not compressed
1356 * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with
1357 * arc_untransform() as long as it is also unencrypted.
1360 arc_get_compression(arc_buf_t
*buf
)
1362 return (ARC_BUF_COMPRESSED(buf
) ?
1363 HDR_GET_COMPRESS(buf
->b_hdr
) : ZIO_COMPRESS_OFF
);
1367 * Return the compression algorithm used to store this data in the ARC. If ARC
1368 * compression is enabled or this is an encrypted block, this will be the same
1369 * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF.
1371 static inline enum zio_compress
1372 arc_hdr_get_compress(arc_buf_hdr_t
*hdr
)
1374 return (HDR_COMPRESSION_ENABLED(hdr
) ?
1375 HDR_GET_COMPRESS(hdr
) : ZIO_COMPRESS_OFF
);
1379 arc_get_complevel(arc_buf_t
*buf
)
1381 return (buf
->b_hdr
->b_complevel
);
1384 static inline boolean_t
1385 arc_buf_is_shared(arc_buf_t
*buf
)
1387 boolean_t shared
= (buf
->b_data
!= NULL
&&
1388 buf
->b_hdr
->b_l1hdr
.b_pabd
!= NULL
&&
1389 abd_is_linear(buf
->b_hdr
->b_l1hdr
.b_pabd
) &&
1390 buf
->b_data
== abd_to_buf(buf
->b_hdr
->b_l1hdr
.b_pabd
));
1391 IMPLY(shared
, HDR_SHARED_DATA(buf
->b_hdr
));
1392 IMPLY(shared
, ARC_BUF_SHARED(buf
));
1393 IMPLY(shared
, ARC_BUF_COMPRESSED(buf
) || ARC_BUF_LAST(buf
));
1396 * It would be nice to assert arc_can_share() too, but the "hdr isn't
1397 * already being shared" requirement prevents us from doing that.
1404 * Free the checksum associated with this header. If there is no checksum, this
1408 arc_cksum_free(arc_buf_hdr_t
*hdr
)
1410 ASSERT(HDR_HAS_L1HDR(hdr
));
1412 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1413 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
) {
1414 kmem_free(hdr
->b_l1hdr
.b_freeze_cksum
, sizeof (zio_cksum_t
));
1415 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
1417 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1421 * Return true iff at least one of the bufs on hdr is not compressed.
1422 * Encrypted buffers count as compressed.
1425 arc_hdr_has_uncompressed_buf(arc_buf_hdr_t
*hdr
)
1427 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY_OR_LOCKED(hdr
));
1429 for (arc_buf_t
*b
= hdr
->b_l1hdr
.b_buf
; b
!= NULL
; b
= b
->b_next
) {
1430 if (!ARC_BUF_COMPRESSED(b
)) {
1439 * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data
1440 * matches the checksum that is stored in the hdr. If there is no checksum,
1441 * or if the buf is compressed, this is a no-op.
1444 arc_cksum_verify(arc_buf_t
*buf
)
1446 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1449 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1452 if (ARC_BUF_COMPRESSED(buf
))
1455 ASSERT(HDR_HAS_L1HDR(hdr
));
1457 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1459 if (hdr
->b_l1hdr
.b_freeze_cksum
== NULL
|| HDR_IO_ERROR(hdr
)) {
1460 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1464 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
, &zc
);
1465 if (!ZIO_CHECKSUM_EQUAL(*hdr
->b_l1hdr
.b_freeze_cksum
, zc
))
1466 panic("buffer modified while frozen!");
1467 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1471 * This function makes the assumption that data stored in the L2ARC
1472 * will be transformed exactly as it is in the main pool. Because of
1473 * this we can verify the checksum against the reading process's bp.
1476 arc_cksum_is_equal(arc_buf_hdr_t
*hdr
, zio_t
*zio
)
1478 ASSERT(!BP_IS_EMBEDDED(zio
->io_bp
));
1479 VERIFY3U(BP_GET_PSIZE(zio
->io_bp
), ==, HDR_GET_PSIZE(hdr
));
1482 * Block pointers always store the checksum for the logical data.
1483 * If the block pointer has the gang bit set, then the checksum
1484 * it represents is for the reconstituted data and not for an
1485 * individual gang member. The zio pipeline, however, must be able to
1486 * determine the checksum of each of the gang constituents so it
1487 * treats the checksum comparison differently than what we need
1488 * for l2arc blocks. This prevents us from using the
1489 * zio_checksum_error() interface directly. Instead we must call the
1490 * zio_checksum_error_impl() so that we can ensure the checksum is
1491 * generated using the correct checksum algorithm and accounts for the
1492 * logical I/O size and not just a gang fragment.
1494 return (zio_checksum_error_impl(zio
->io_spa
, zio
->io_bp
,
1495 BP_GET_CHECKSUM(zio
->io_bp
), zio
->io_abd
, zio
->io_size
,
1496 zio
->io_offset
, NULL
) == 0);
1500 * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a
1501 * checksum and attaches it to the buf's hdr so that we can ensure that the buf
1502 * isn't modified later on. If buf is compressed or there is already a checksum
1503 * on the hdr, this is a no-op (we only checksum uncompressed bufs).
1506 arc_cksum_compute(arc_buf_t
*buf
)
1508 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1510 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1513 ASSERT(HDR_HAS_L1HDR(hdr
));
1515 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1516 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
|| ARC_BUF_COMPRESSED(buf
)) {
1517 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1521 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
1522 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1523 hdr
->b_l1hdr
.b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1525 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
,
1526 hdr
->b_l1hdr
.b_freeze_cksum
);
1527 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1533 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1535 panic("Got SIGSEGV at address: 0x%lx\n", (long)si
->si_addr
);
1541 arc_buf_unwatch(arc_buf_t
*buf
)
1545 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1546 PROT_READ
| PROT_WRITE
));
1553 arc_buf_watch(arc_buf_t
*buf
)
1557 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1562 static arc_buf_contents_t
1563 arc_buf_type(arc_buf_hdr_t
*hdr
)
1565 arc_buf_contents_t type
;
1566 if (HDR_ISTYPE_METADATA(hdr
)) {
1567 type
= ARC_BUFC_METADATA
;
1569 type
= ARC_BUFC_DATA
;
1571 VERIFY3U(hdr
->b_type
, ==, type
);
1576 arc_is_metadata(arc_buf_t
*buf
)
1578 return (HDR_ISTYPE_METADATA(buf
->b_hdr
) != 0);
1582 arc_bufc_to_flags(arc_buf_contents_t type
)
1586 /* metadata field is 0 if buffer contains normal data */
1588 case ARC_BUFC_METADATA
:
1589 return (ARC_FLAG_BUFC_METADATA
);
1593 panic("undefined ARC buffer type!");
1594 return ((uint32_t)-1);
1598 arc_buf_thaw(arc_buf_t
*buf
)
1600 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1602 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
1603 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1605 arc_cksum_verify(buf
);
1608 * Compressed buffers do not manipulate the b_freeze_cksum.
1610 if (ARC_BUF_COMPRESSED(buf
))
1613 ASSERT(HDR_HAS_L1HDR(hdr
));
1614 arc_cksum_free(hdr
);
1615 arc_buf_unwatch(buf
);
1619 arc_buf_freeze(arc_buf_t
*buf
)
1621 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1624 if (ARC_BUF_COMPRESSED(buf
))
1627 ASSERT(HDR_HAS_L1HDR(buf
->b_hdr
));
1628 arc_cksum_compute(buf
);
1632 * The arc_buf_hdr_t's b_flags should never be modified directly. Instead,
1633 * the following functions should be used to ensure that the flags are
1634 * updated in a thread-safe way. When manipulating the flags either
1635 * the hash_lock must be held or the hdr must be undiscoverable. This
1636 * ensures that we're not racing with any other threads when updating
1640 arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1642 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1643 hdr
->b_flags
|= flags
;
1647 arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1649 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1650 hdr
->b_flags
&= ~flags
;
1654 * Setting the compression bits in the arc_buf_hdr_t's b_flags is
1655 * done in a special way since we have to clear and set bits
1656 * at the same time. Consumers that wish to set the compression bits
1657 * must use this function to ensure that the flags are updated in
1658 * thread-safe manner.
1661 arc_hdr_set_compress(arc_buf_hdr_t
*hdr
, enum zio_compress cmp
)
1663 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1666 * Holes and embedded blocks will always have a psize = 0 so
1667 * we ignore the compression of the blkptr and set the
1668 * want to uncompress them. Mark them as uncompressed.
1670 if (!zfs_compressed_arc_enabled
|| HDR_GET_PSIZE(hdr
) == 0) {
1671 arc_hdr_clear_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1672 ASSERT(!HDR_COMPRESSION_ENABLED(hdr
));
1674 arc_hdr_set_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1675 ASSERT(HDR_COMPRESSION_ENABLED(hdr
));
1678 HDR_SET_COMPRESS(hdr
, cmp
);
1679 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, cmp
);
1683 * Looks for another buf on the same hdr which has the data decompressed, copies
1684 * from it, and returns true. If no such buf exists, returns false.
1687 arc_buf_try_copy_decompressed_data(arc_buf_t
*buf
)
1689 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1690 boolean_t copied
= B_FALSE
;
1692 ASSERT(HDR_HAS_L1HDR(hdr
));
1693 ASSERT3P(buf
->b_data
, !=, NULL
);
1694 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1696 for (arc_buf_t
*from
= hdr
->b_l1hdr
.b_buf
; from
!= NULL
;
1697 from
= from
->b_next
) {
1698 /* can't use our own data buffer */
1703 if (!ARC_BUF_COMPRESSED(from
)) {
1704 bcopy(from
->b_data
, buf
->b_data
, arc_buf_size(buf
));
1711 * There were no decompressed bufs, so there should not be a
1712 * checksum on the hdr either.
1714 if (zfs_flags
& ZFS_DEBUG_MODIFY
)
1715 EQUIV(!copied
, hdr
->b_l1hdr
.b_freeze_cksum
== NULL
);
1721 * Allocates an ARC buf header that's in an evicted & L2-cached state.
1722 * This is used during l2arc reconstruction to make empty ARC buffers
1723 * which circumvent the regular disk->arc->l2arc path and instead come
1724 * into being in the reverse order, i.e. l2arc->arc.
1726 static arc_buf_hdr_t
*
1727 arc_buf_alloc_l2only(size_t size
, arc_buf_contents_t type
, l2arc_dev_t
*dev
,
1728 dva_t dva
, uint64_t daddr
, int32_t psize
, uint64_t birth
,
1729 enum zio_compress compress
, uint8_t complevel
, boolean_t
protected,
1735 hdr
= kmem_cache_alloc(hdr_l2only_cache
, KM_SLEEP
);
1736 hdr
->b_birth
= birth
;
1739 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L2HDR
);
1740 HDR_SET_LSIZE(hdr
, size
);
1741 HDR_SET_PSIZE(hdr
, psize
);
1742 arc_hdr_set_compress(hdr
, compress
);
1743 hdr
->b_complevel
= complevel
;
1745 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
1747 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
1748 hdr
->b_spa
= spa_load_guid(dev
->l2ad_vdev
->vdev_spa
);
1752 hdr
->b_l2hdr
.b_dev
= dev
;
1753 hdr
->b_l2hdr
.b_daddr
= daddr
;
1759 * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t.
1762 arc_hdr_size(arc_buf_hdr_t
*hdr
)
1766 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
1767 HDR_GET_PSIZE(hdr
) > 0) {
1768 size
= HDR_GET_PSIZE(hdr
);
1770 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, 0);
1771 size
= HDR_GET_LSIZE(hdr
);
1777 arc_hdr_authenticate(arc_buf_hdr_t
*hdr
, spa_t
*spa
, uint64_t dsobj
)
1781 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
1782 uint64_t psize
= HDR_GET_PSIZE(hdr
);
1783 void *tmpbuf
= NULL
;
1784 abd_t
*abd
= hdr
->b_l1hdr
.b_pabd
;
1786 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1787 ASSERT(HDR_AUTHENTICATED(hdr
));
1788 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1791 * The MAC is calculated on the compressed data that is stored on disk.
1792 * However, if compressed arc is disabled we will only have the
1793 * decompressed data available to us now. Compress it into a temporary
1794 * abd so we can verify the MAC. The performance overhead of this will
1795 * be relatively low, since most objects in an encrypted objset will
1796 * be encrypted (instead of authenticated) anyway.
1798 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1799 !HDR_COMPRESSION_ENABLED(hdr
)) {
1800 tmpbuf
= zio_buf_alloc(lsize
);
1801 abd
= abd_get_from_buf(tmpbuf
, lsize
);
1802 abd_take_ownership_of_buf(abd
, B_TRUE
);
1803 csize
= zio_compress_data(HDR_GET_COMPRESS(hdr
),
1804 hdr
->b_l1hdr
.b_pabd
, tmpbuf
, lsize
, hdr
->b_complevel
);
1805 ASSERT3U(csize
, <=, psize
);
1806 abd_zero_off(abd
, csize
, psize
- csize
);
1810 * Authentication is best effort. We authenticate whenever the key is
1811 * available. If we succeed we clear ARC_FLAG_NOAUTH.
1813 if (hdr
->b_crypt_hdr
.b_ot
== DMU_OT_OBJSET
) {
1814 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, ZIO_COMPRESS_OFF
);
1815 ASSERT3U(lsize
, ==, psize
);
1816 ret
= spa_do_crypt_objset_mac_abd(B_FALSE
, spa
, dsobj
, abd
,
1817 psize
, hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1819 ret
= spa_do_crypt_mac_abd(B_FALSE
, spa
, dsobj
, abd
, psize
,
1820 hdr
->b_crypt_hdr
.b_mac
);
1824 arc_hdr_clear_flags(hdr
, ARC_FLAG_NOAUTH
);
1825 else if (ret
!= ENOENT
)
1841 * This function will take a header that only has raw encrypted data in
1842 * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in
1843 * b_l1hdr.b_pabd. If designated in the header flags, this function will
1844 * also decompress the data.
1847 arc_hdr_decrypt(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
)
1852 boolean_t no_crypt
= B_FALSE
;
1853 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1855 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1856 ASSERT(HDR_ENCRYPTED(hdr
));
1858 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
);
1860 ret
= spa_do_crypt_abd(B_FALSE
, spa
, zb
, hdr
->b_crypt_hdr
.b_ot
,
1861 B_FALSE
, bswap
, hdr
->b_crypt_hdr
.b_salt
, hdr
->b_crypt_hdr
.b_iv
,
1862 hdr
->b_crypt_hdr
.b_mac
, HDR_GET_PSIZE(hdr
), hdr
->b_l1hdr
.b_pabd
,
1863 hdr
->b_crypt_hdr
.b_rabd
, &no_crypt
);
1868 abd_copy(hdr
->b_l1hdr
.b_pabd
, hdr
->b_crypt_hdr
.b_rabd
,
1869 HDR_GET_PSIZE(hdr
));
1873 * If this header has disabled arc compression but the b_pabd is
1874 * compressed after decrypting it, we need to decompress the newly
1877 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1878 !HDR_COMPRESSION_ENABLED(hdr
)) {
1880 * We want to make sure that we are correctly honoring the
1881 * zfs_abd_scatter_enabled setting, so we allocate an abd here
1882 * and then loan a buffer from it, rather than allocating a
1883 * linear buffer and wrapping it in an abd later.
1885 cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
, B_TRUE
);
1886 tmp
= abd_borrow_buf(cabd
, arc_hdr_size(hdr
));
1888 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
1889 hdr
->b_l1hdr
.b_pabd
, tmp
, HDR_GET_PSIZE(hdr
),
1890 HDR_GET_LSIZE(hdr
), &hdr
->b_complevel
);
1892 abd_return_buf(cabd
, tmp
, arc_hdr_size(hdr
));
1896 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
1897 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
1898 arc_hdr_size(hdr
), hdr
);
1899 hdr
->b_l1hdr
.b_pabd
= cabd
;
1905 arc_hdr_free_abd(hdr
, B_FALSE
);
1907 arc_free_data_buf(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
1913 * This function is called during arc_buf_fill() to prepare the header's
1914 * abd plaintext pointer for use. This involves authenticated protected
1915 * data and decrypting encrypted data into the plaintext abd.
1918 arc_fill_hdr_crypt(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, spa_t
*spa
,
1919 const zbookmark_phys_t
*zb
, boolean_t noauth
)
1923 ASSERT(HDR_PROTECTED(hdr
));
1925 if (hash_lock
!= NULL
)
1926 mutex_enter(hash_lock
);
1928 if (HDR_NOAUTH(hdr
) && !noauth
) {
1930 * The caller requested authenticated data but our data has
1931 * not been authenticated yet. Verify the MAC now if we can.
1933 ret
= arc_hdr_authenticate(hdr
, spa
, zb
->zb_objset
);
1936 } else if (HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
== NULL
) {
1938 * If we only have the encrypted version of the data, but the
1939 * unencrypted version was requested we take this opportunity
1940 * to store the decrypted version in the header for future use.
1942 ret
= arc_hdr_decrypt(hdr
, spa
, zb
);
1947 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1949 if (hash_lock
!= NULL
)
1950 mutex_exit(hash_lock
);
1955 if (hash_lock
!= NULL
)
1956 mutex_exit(hash_lock
);
1962 * This function is used by the dbuf code to decrypt bonus buffers in place.
1963 * The dbuf code itself doesn't have any locking for decrypting a shared dnode
1964 * block, so we use the hash lock here to protect against concurrent calls to
1968 arc_buf_untransform_in_place(arc_buf_t
*buf
, kmutex_t
*hash_lock
)
1970 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1972 ASSERT(HDR_ENCRYPTED(hdr
));
1973 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
1974 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1975 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1977 zio_crypt_copy_dnode_bonus(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
1979 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
1980 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
1981 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
1985 * Given a buf that has a data buffer attached to it, this function will
1986 * efficiently fill the buf with data of the specified compression setting from
1987 * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr
1988 * are already sharing a data buf, no copy is performed.
1990 * If the buf is marked as compressed but uncompressed data was requested, this
1991 * will allocate a new data buffer for the buf, remove that flag, and fill the
1992 * buf with uncompressed data. You can't request a compressed buf on a hdr with
1993 * uncompressed data, and (since we haven't added support for it yet) if you
1994 * want compressed data your buf must already be marked as compressed and have
1995 * the correct-sized data buffer.
1998 arc_buf_fill(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
1999 arc_fill_flags_t flags
)
2002 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2003 boolean_t hdr_compressed
=
2004 (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
2005 boolean_t compressed
= (flags
& ARC_FILL_COMPRESSED
) != 0;
2006 boolean_t encrypted
= (flags
& ARC_FILL_ENCRYPTED
) != 0;
2007 dmu_object_byteswap_t bswap
= hdr
->b_l1hdr
.b_byteswap
;
2008 kmutex_t
*hash_lock
= (flags
& ARC_FILL_LOCKED
) ? NULL
: HDR_LOCK(hdr
);
2010 ASSERT3P(buf
->b_data
, !=, NULL
);
2011 IMPLY(compressed
, hdr_compressed
|| ARC_BUF_ENCRYPTED(buf
));
2012 IMPLY(compressed
, ARC_BUF_COMPRESSED(buf
));
2013 IMPLY(encrypted
, HDR_ENCRYPTED(hdr
));
2014 IMPLY(encrypted
, ARC_BUF_ENCRYPTED(buf
));
2015 IMPLY(encrypted
, ARC_BUF_COMPRESSED(buf
));
2016 IMPLY(encrypted
, !ARC_BUF_SHARED(buf
));
2019 * If the caller wanted encrypted data we just need to copy it from
2020 * b_rabd and potentially byteswap it. We won't be able to do any
2021 * further transforms on it.
2024 ASSERT(HDR_HAS_RABD(hdr
));
2025 abd_copy_to_buf(buf
->b_data
, hdr
->b_crypt_hdr
.b_rabd
,
2026 HDR_GET_PSIZE(hdr
));
2031 * Adjust encrypted and authenticated headers to accommodate
2032 * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are
2033 * allowed to fail decryption due to keys not being loaded
2034 * without being marked as an IO error.
2036 if (HDR_PROTECTED(hdr
)) {
2037 error
= arc_fill_hdr_crypt(hdr
, hash_lock
, spa
,
2038 zb
, !!(flags
& ARC_FILL_NOAUTH
));
2039 if (error
== EACCES
&& (flags
& ARC_FILL_IN_PLACE
) != 0) {
2041 } else if (error
!= 0) {
2042 if (hash_lock
!= NULL
)
2043 mutex_enter(hash_lock
);
2044 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2045 if (hash_lock
!= NULL
)
2046 mutex_exit(hash_lock
);
2052 * There is a special case here for dnode blocks which are
2053 * decrypting their bonus buffers. These blocks may request to
2054 * be decrypted in-place. This is necessary because there may
2055 * be many dnodes pointing into this buffer and there is
2056 * currently no method to synchronize replacing the backing
2057 * b_data buffer and updating all of the pointers. Here we use
2058 * the hash lock to ensure there are no races. If the need
2059 * arises for other types to be decrypted in-place, they must
2060 * add handling here as well.
2062 if ((flags
& ARC_FILL_IN_PLACE
) != 0) {
2063 ASSERT(!hdr_compressed
);
2064 ASSERT(!compressed
);
2067 if (HDR_ENCRYPTED(hdr
) && ARC_BUF_ENCRYPTED(buf
)) {
2068 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
2070 if (hash_lock
!= NULL
)
2071 mutex_enter(hash_lock
);
2072 arc_buf_untransform_in_place(buf
, hash_lock
);
2073 if (hash_lock
!= NULL
)
2074 mutex_exit(hash_lock
);
2076 /* Compute the hdr's checksum if necessary */
2077 arc_cksum_compute(buf
);
2083 if (hdr_compressed
== compressed
) {
2084 if (!arc_buf_is_shared(buf
)) {
2085 abd_copy_to_buf(buf
->b_data
, hdr
->b_l1hdr
.b_pabd
,
2089 ASSERT(hdr_compressed
);
2090 ASSERT(!compressed
);
2091 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, HDR_GET_PSIZE(hdr
));
2094 * If the buf is sharing its data with the hdr, unlink it and
2095 * allocate a new data buffer for the buf.
2097 if (arc_buf_is_shared(buf
)) {
2098 ASSERT(ARC_BUF_COMPRESSED(buf
));
2100 /* We need to give the buf its own b_data */
2101 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
2103 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2104 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2106 /* Previously overhead was 0; just add new overhead */
2107 ARCSTAT_INCR(arcstat_overhead_size
, HDR_GET_LSIZE(hdr
));
2108 } else if (ARC_BUF_COMPRESSED(buf
)) {
2109 /* We need to reallocate the buf's b_data */
2110 arc_free_data_buf(hdr
, buf
->b_data
, HDR_GET_PSIZE(hdr
),
2113 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2115 /* We increased the size of b_data; update overhead */
2116 ARCSTAT_INCR(arcstat_overhead_size
,
2117 HDR_GET_LSIZE(hdr
) - HDR_GET_PSIZE(hdr
));
2121 * Regardless of the buf's previous compression settings, it
2122 * should not be compressed at the end of this function.
2124 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
2127 * Try copying the data from another buf which already has a
2128 * decompressed version. If that's not possible, it's time to
2129 * bite the bullet and decompress the data from the hdr.
2131 if (arc_buf_try_copy_decompressed_data(buf
)) {
2132 /* Skip byteswapping and checksumming (already done) */
2135 error
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
2136 hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
2137 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
),
2141 * Absent hardware errors or software bugs, this should
2142 * be impossible, but log it anyway so we can debug it.
2146 "hdr %px, compress %d, psize %d, lsize %d",
2147 hdr
, arc_hdr_get_compress(hdr
),
2148 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
2149 if (hash_lock
!= NULL
)
2150 mutex_enter(hash_lock
);
2151 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2152 if (hash_lock
!= NULL
)
2153 mutex_exit(hash_lock
);
2154 return (SET_ERROR(EIO
));
2160 /* Byteswap the buf's data if necessary */
2161 if (bswap
!= DMU_BSWAP_NUMFUNCS
) {
2162 ASSERT(!HDR_SHARED_DATA(hdr
));
2163 ASSERT3U(bswap
, <, DMU_BSWAP_NUMFUNCS
);
2164 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, HDR_GET_LSIZE(hdr
));
2167 /* Compute the hdr's checksum if necessary */
2168 arc_cksum_compute(buf
);
2174 * If this function is being called to decrypt an encrypted buffer or verify an
2175 * authenticated one, the key must be loaded and a mapping must be made
2176 * available in the keystore via spa_keystore_create_mapping() or one of its
2180 arc_untransform(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2184 arc_fill_flags_t flags
= 0;
2187 flags
|= ARC_FILL_IN_PLACE
;
2189 ret
= arc_buf_fill(buf
, spa
, zb
, flags
);
2190 if (ret
== ECKSUM
) {
2192 * Convert authentication and decryption errors to EIO
2193 * (and generate an ereport) before leaving the ARC.
2195 ret
= SET_ERROR(EIO
);
2196 spa_log_error(spa
, zb
);
2197 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION
,
2198 spa
, NULL
, zb
, NULL
, 0);
2205 * Increment the amount of evictable space in the arc_state_t's refcount.
2206 * We account for the space used by the hdr and the arc buf individually
2207 * so that we can add and remove them from the refcount individually.
2210 arc_evictable_space_increment(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2212 arc_buf_contents_t type
= arc_buf_type(hdr
);
2214 ASSERT(HDR_HAS_L1HDR(hdr
));
2216 if (GHOST_STATE(state
)) {
2217 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2218 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2219 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2220 ASSERT(!HDR_HAS_RABD(hdr
));
2221 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2222 HDR_GET_LSIZE(hdr
), hdr
);
2226 ASSERT(!GHOST_STATE(state
));
2227 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2228 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2229 arc_hdr_size(hdr
), hdr
);
2231 if (HDR_HAS_RABD(hdr
)) {
2232 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2233 HDR_GET_PSIZE(hdr
), hdr
);
2236 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2237 buf
= buf
->b_next
) {
2238 if (arc_buf_is_shared(buf
))
2240 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2241 arc_buf_size(buf
), buf
);
2246 * Decrement the amount of evictable space in the arc_state_t's refcount.
2247 * We account for the space used by the hdr and the arc buf individually
2248 * so that we can add and remove them from the refcount individually.
2251 arc_evictable_space_decrement(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2253 arc_buf_contents_t type
= arc_buf_type(hdr
);
2255 ASSERT(HDR_HAS_L1HDR(hdr
));
2257 if (GHOST_STATE(state
)) {
2258 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2259 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2260 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2261 ASSERT(!HDR_HAS_RABD(hdr
));
2262 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2263 HDR_GET_LSIZE(hdr
), hdr
);
2267 ASSERT(!GHOST_STATE(state
));
2268 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2269 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2270 arc_hdr_size(hdr
), hdr
);
2272 if (HDR_HAS_RABD(hdr
)) {
2273 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2274 HDR_GET_PSIZE(hdr
), hdr
);
2277 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2278 buf
= buf
->b_next
) {
2279 if (arc_buf_is_shared(buf
))
2281 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2282 arc_buf_size(buf
), buf
);
2287 * Add a reference to this hdr indicating that someone is actively
2288 * referencing that memory. When the refcount transitions from 0 to 1,
2289 * we remove it from the respective arc_state_t list to indicate that
2290 * it is not evictable.
2293 add_reference(arc_buf_hdr_t
*hdr
, void *tag
)
2297 ASSERT(HDR_HAS_L1HDR(hdr
));
2298 if (!HDR_EMPTY(hdr
) && !MUTEX_HELD(HDR_LOCK(hdr
))) {
2299 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
2300 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2301 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2304 state
= hdr
->b_l1hdr
.b_state
;
2306 if ((zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
) == 1) &&
2307 (state
!= arc_anon
)) {
2308 /* We don't use the L2-only state list. */
2309 if (state
!= arc_l2c_only
) {
2310 multilist_remove(state
->arcs_list
[arc_buf_type(hdr
)],
2312 arc_evictable_space_decrement(hdr
, state
);
2314 /* remove the prefetch flag if we get a reference */
2315 arc_hdr_clear_flags(hdr
, ARC_FLAG_PREFETCH
);
2320 * Remove a reference from this hdr. When the reference transitions from
2321 * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's
2322 * list making it eligible for eviction.
2325 remove_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
2328 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2330 ASSERT(HDR_HAS_L1HDR(hdr
));
2331 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
2332 ASSERT(!GHOST_STATE(state
));
2335 * arc_l2c_only counts as a ghost state so we don't need to explicitly
2336 * check to prevent usage of the arc_l2c_only list.
2338 if (((cnt
= zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
)) == 0) &&
2339 (state
!= arc_anon
)) {
2340 multilist_insert(state
->arcs_list
[arc_buf_type(hdr
)], hdr
);
2341 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
2342 arc_evictable_space_increment(hdr
, state
);
2348 * Returns detailed information about a specific arc buffer. When the
2349 * state_index argument is set the function will calculate the arc header
2350 * list position for its arc state. Since this requires a linear traversal
2351 * callers are strongly encourage not to do this. However, it can be helpful
2352 * for targeted analysis so the functionality is provided.
2355 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
2357 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
2358 l1arc_buf_hdr_t
*l1hdr
= NULL
;
2359 l2arc_buf_hdr_t
*l2hdr
= NULL
;
2360 arc_state_t
*state
= NULL
;
2362 memset(abi
, 0, sizeof (arc_buf_info_t
));
2367 abi
->abi_flags
= hdr
->b_flags
;
2369 if (HDR_HAS_L1HDR(hdr
)) {
2370 l1hdr
= &hdr
->b_l1hdr
;
2371 state
= l1hdr
->b_state
;
2373 if (HDR_HAS_L2HDR(hdr
))
2374 l2hdr
= &hdr
->b_l2hdr
;
2377 abi
->abi_bufcnt
= l1hdr
->b_bufcnt
;
2378 abi
->abi_access
= l1hdr
->b_arc_access
;
2379 abi
->abi_mru_hits
= l1hdr
->b_mru_hits
;
2380 abi
->abi_mru_ghost_hits
= l1hdr
->b_mru_ghost_hits
;
2381 abi
->abi_mfu_hits
= l1hdr
->b_mfu_hits
;
2382 abi
->abi_mfu_ghost_hits
= l1hdr
->b_mfu_ghost_hits
;
2383 abi
->abi_holds
= zfs_refcount_count(&l1hdr
->b_refcnt
);
2387 abi
->abi_l2arc_dattr
= l2hdr
->b_daddr
;
2388 abi
->abi_l2arc_hits
= l2hdr
->b_hits
;
2391 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
2392 abi
->abi_state_contents
= arc_buf_type(hdr
);
2393 abi
->abi_size
= arc_hdr_size(hdr
);
2397 * Move the supplied buffer to the indicated state. The hash lock
2398 * for the buffer must be held by the caller.
2401 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*hdr
,
2402 kmutex_t
*hash_lock
)
2404 arc_state_t
*old_state
;
2407 boolean_t update_old
, update_new
;
2408 arc_buf_contents_t buftype
= arc_buf_type(hdr
);
2411 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
2412 * in arc_read() when bringing a buffer out of the L2ARC. However, the
2413 * L1 hdr doesn't always exist when we change state to arc_anon before
2414 * destroying a header, in which case reallocating to add the L1 hdr is
2417 if (HDR_HAS_L1HDR(hdr
)) {
2418 old_state
= hdr
->b_l1hdr
.b_state
;
2419 refcnt
= zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
);
2420 bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
2421 update_old
= (bufcnt
> 0 || hdr
->b_l1hdr
.b_pabd
!= NULL
||
2424 old_state
= arc_l2c_only
;
2427 update_old
= B_FALSE
;
2429 update_new
= update_old
;
2431 ASSERT(MUTEX_HELD(hash_lock
));
2432 ASSERT3P(new_state
, !=, old_state
);
2433 ASSERT(!GHOST_STATE(new_state
) || bufcnt
== 0);
2434 ASSERT(old_state
!= arc_anon
|| bufcnt
<= 1);
2437 * If this buffer is evictable, transfer it from the
2438 * old state list to the new state list.
2441 if (old_state
!= arc_anon
&& old_state
!= arc_l2c_only
) {
2442 ASSERT(HDR_HAS_L1HDR(hdr
));
2443 multilist_remove(old_state
->arcs_list
[buftype
], hdr
);
2445 if (GHOST_STATE(old_state
)) {
2447 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2448 update_old
= B_TRUE
;
2450 arc_evictable_space_decrement(hdr
, old_state
);
2452 if (new_state
!= arc_anon
&& new_state
!= arc_l2c_only
) {
2454 * An L1 header always exists here, since if we're
2455 * moving to some L1-cached state (i.e. not l2c_only or
2456 * anonymous), we realloc the header to add an L1hdr
2459 ASSERT(HDR_HAS_L1HDR(hdr
));
2460 multilist_insert(new_state
->arcs_list
[buftype
], hdr
);
2462 if (GHOST_STATE(new_state
)) {
2464 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2465 update_new
= B_TRUE
;
2467 arc_evictable_space_increment(hdr
, new_state
);
2471 ASSERT(!HDR_EMPTY(hdr
));
2472 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(hdr
))
2473 buf_hash_remove(hdr
);
2475 /* adjust state sizes (ignore arc_l2c_only) */
2477 if (update_new
&& new_state
!= arc_l2c_only
) {
2478 ASSERT(HDR_HAS_L1HDR(hdr
));
2479 if (GHOST_STATE(new_state
)) {
2483 * When moving a header to a ghost state, we first
2484 * remove all arc buffers. Thus, we'll have a
2485 * bufcnt of zero, and no arc buffer to use for
2486 * the reference. As a result, we use the arc
2487 * header pointer for the reference.
2489 (void) zfs_refcount_add_many(&new_state
->arcs_size
,
2490 HDR_GET_LSIZE(hdr
), hdr
);
2491 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2492 ASSERT(!HDR_HAS_RABD(hdr
));
2494 uint32_t buffers
= 0;
2497 * Each individual buffer holds a unique reference,
2498 * thus we must remove each of these references one
2501 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2502 buf
= buf
->b_next
) {
2503 ASSERT3U(bufcnt
, !=, 0);
2507 * When the arc_buf_t is sharing the data
2508 * block with the hdr, the owner of the
2509 * reference belongs to the hdr. Only
2510 * add to the refcount if the arc_buf_t is
2513 if (arc_buf_is_shared(buf
))
2516 (void) zfs_refcount_add_many(
2517 &new_state
->arcs_size
,
2518 arc_buf_size(buf
), buf
);
2520 ASSERT3U(bufcnt
, ==, buffers
);
2522 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2523 (void) zfs_refcount_add_many(
2524 &new_state
->arcs_size
,
2525 arc_hdr_size(hdr
), hdr
);
2528 if (HDR_HAS_RABD(hdr
)) {
2529 (void) zfs_refcount_add_many(
2530 &new_state
->arcs_size
,
2531 HDR_GET_PSIZE(hdr
), hdr
);
2536 if (update_old
&& old_state
!= arc_l2c_only
) {
2537 ASSERT(HDR_HAS_L1HDR(hdr
));
2538 if (GHOST_STATE(old_state
)) {
2540 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2541 ASSERT(!HDR_HAS_RABD(hdr
));
2544 * When moving a header off of a ghost state,
2545 * the header will not contain any arc buffers.
2546 * We use the arc header pointer for the reference
2547 * which is exactly what we did when we put the
2548 * header on the ghost state.
2551 (void) zfs_refcount_remove_many(&old_state
->arcs_size
,
2552 HDR_GET_LSIZE(hdr
), hdr
);
2554 uint32_t buffers
= 0;
2557 * Each individual buffer holds a unique reference,
2558 * thus we must remove each of these references one
2561 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2562 buf
= buf
->b_next
) {
2563 ASSERT3U(bufcnt
, !=, 0);
2567 * When the arc_buf_t is sharing the data
2568 * block with the hdr, the owner of the
2569 * reference belongs to the hdr. Only
2570 * add to the refcount if the arc_buf_t is
2573 if (arc_buf_is_shared(buf
))
2576 (void) zfs_refcount_remove_many(
2577 &old_state
->arcs_size
, arc_buf_size(buf
),
2580 ASSERT3U(bufcnt
, ==, buffers
);
2581 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
2584 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2585 (void) zfs_refcount_remove_many(
2586 &old_state
->arcs_size
, arc_hdr_size(hdr
),
2590 if (HDR_HAS_RABD(hdr
)) {
2591 (void) zfs_refcount_remove_many(
2592 &old_state
->arcs_size
, HDR_GET_PSIZE(hdr
),
2598 if (HDR_HAS_L1HDR(hdr
))
2599 hdr
->b_l1hdr
.b_state
= new_state
;
2602 * L2 headers should never be on the L2 state list since they don't
2603 * have L1 headers allocated.
2605 ASSERT(multilist_is_empty(arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]) &&
2606 multilist_is_empty(arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]));
2610 arc_space_consume(uint64_t space
, arc_space_type_t type
)
2612 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2617 case ARC_SPACE_DATA
:
2618 aggsum_add(&astat_data_size
, space
);
2620 case ARC_SPACE_META
:
2621 aggsum_add(&astat_metadata_size
, space
);
2623 case ARC_SPACE_BONUS
:
2624 aggsum_add(&astat_bonus_size
, space
);
2626 case ARC_SPACE_DNODE
:
2627 aggsum_add(&astat_dnode_size
, space
);
2629 case ARC_SPACE_DBUF
:
2630 aggsum_add(&astat_dbuf_size
, space
);
2632 case ARC_SPACE_HDRS
:
2633 aggsum_add(&astat_hdr_size
, space
);
2635 case ARC_SPACE_L2HDRS
:
2636 aggsum_add(&astat_l2_hdr_size
, space
);
2638 case ARC_SPACE_ABD_CHUNK_WASTE
:
2640 * Note: this includes space wasted by all scatter ABD's, not
2641 * just those allocated by the ARC. But the vast majority of
2642 * scatter ABD's come from the ARC, because other users are
2645 aggsum_add(&astat_abd_chunk_waste_size
, space
);
2649 if (type
!= ARC_SPACE_DATA
&& type
!= ARC_SPACE_ABD_CHUNK_WASTE
)
2650 aggsum_add(&arc_meta_used
, space
);
2652 aggsum_add(&arc_size
, space
);
2656 arc_space_return(uint64_t space
, arc_space_type_t type
)
2658 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2663 case ARC_SPACE_DATA
:
2664 aggsum_add(&astat_data_size
, -space
);
2666 case ARC_SPACE_META
:
2667 aggsum_add(&astat_metadata_size
, -space
);
2669 case ARC_SPACE_BONUS
:
2670 aggsum_add(&astat_bonus_size
, -space
);
2672 case ARC_SPACE_DNODE
:
2673 aggsum_add(&astat_dnode_size
, -space
);
2675 case ARC_SPACE_DBUF
:
2676 aggsum_add(&astat_dbuf_size
, -space
);
2678 case ARC_SPACE_HDRS
:
2679 aggsum_add(&astat_hdr_size
, -space
);
2681 case ARC_SPACE_L2HDRS
:
2682 aggsum_add(&astat_l2_hdr_size
, -space
);
2684 case ARC_SPACE_ABD_CHUNK_WASTE
:
2685 aggsum_add(&astat_abd_chunk_waste_size
, -space
);
2689 if (type
!= ARC_SPACE_DATA
&& type
!= ARC_SPACE_ABD_CHUNK_WASTE
) {
2690 ASSERT(aggsum_compare(&arc_meta_used
, space
) >= 0);
2692 * We use the upper bound here rather than the precise value
2693 * because the arc_meta_max value doesn't need to be
2694 * precise. It's only consumed by humans via arcstats.
2696 if (arc_meta_max
< aggsum_upper_bound(&arc_meta_used
))
2697 arc_meta_max
= aggsum_upper_bound(&arc_meta_used
);
2698 aggsum_add(&arc_meta_used
, -space
);
2701 ASSERT(aggsum_compare(&arc_size
, space
) >= 0);
2702 aggsum_add(&arc_size
, -space
);
2706 * Given a hdr and a buf, returns whether that buf can share its b_data buffer
2707 * with the hdr's b_pabd.
2710 arc_can_share(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2713 * The criteria for sharing a hdr's data are:
2714 * 1. the buffer is not encrypted
2715 * 2. the hdr's compression matches the buf's compression
2716 * 3. the hdr doesn't need to be byteswapped
2717 * 4. the hdr isn't already being shared
2718 * 5. the buf is either compressed or it is the last buf in the hdr list
2720 * Criterion #5 maintains the invariant that shared uncompressed
2721 * bufs must be the final buf in the hdr's b_buf list. Reading this, you
2722 * might ask, "if a compressed buf is allocated first, won't that be the
2723 * last thing in the list?", but in that case it's impossible to create
2724 * a shared uncompressed buf anyway (because the hdr must be compressed
2725 * to have the compressed buf). You might also think that #3 is
2726 * sufficient to make this guarantee, however it's possible
2727 * (specifically in the rare L2ARC write race mentioned in
2728 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that
2729 * is shareable, but wasn't at the time of its allocation. Rather than
2730 * allow a new shared uncompressed buf to be created and then shuffle
2731 * the list around to make it the last element, this simply disallows
2732 * sharing if the new buf isn't the first to be added.
2734 ASSERT3P(buf
->b_hdr
, ==, hdr
);
2735 boolean_t hdr_compressed
=
2736 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
;
2737 boolean_t buf_compressed
= ARC_BUF_COMPRESSED(buf
) != 0;
2738 return (!ARC_BUF_ENCRYPTED(buf
) &&
2739 buf_compressed
== hdr_compressed
&&
2740 hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
&&
2741 !HDR_SHARED_DATA(hdr
) &&
2742 (ARC_BUF_LAST(buf
) || ARC_BUF_COMPRESSED(buf
)));
2746 * Allocate a buf for this hdr. If you care about the data that's in the hdr,
2747 * or if you want a compressed buffer, pass those flags in. Returns 0 if the
2748 * copy was made successfully, or an error code otherwise.
2751 arc_buf_alloc_impl(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2752 void *tag
, boolean_t encrypted
, boolean_t compressed
, boolean_t noauth
,
2753 boolean_t fill
, arc_buf_t
**ret
)
2756 arc_fill_flags_t flags
= ARC_FILL_LOCKED
;
2758 ASSERT(HDR_HAS_L1HDR(hdr
));
2759 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
2760 VERIFY(hdr
->b_type
== ARC_BUFC_DATA
||
2761 hdr
->b_type
== ARC_BUFC_METADATA
);
2762 ASSERT3P(ret
, !=, NULL
);
2763 ASSERT3P(*ret
, ==, NULL
);
2764 IMPLY(encrypted
, compressed
);
2766 hdr
->b_l1hdr
.b_mru_hits
= 0;
2767 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
2768 hdr
->b_l1hdr
.b_mfu_hits
= 0;
2769 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
2770 hdr
->b_l1hdr
.b_l2_hits
= 0;
2772 buf
= *ret
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
2775 buf
->b_next
= hdr
->b_l1hdr
.b_buf
;
2778 add_reference(hdr
, tag
);
2781 * We're about to change the hdr's b_flags. We must either
2782 * hold the hash_lock or be undiscoverable.
2784 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
2787 * Only honor requests for compressed bufs if the hdr is actually
2788 * compressed. This must be overridden if the buffer is encrypted since
2789 * encrypted buffers cannot be decompressed.
2792 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2793 buf
->b_flags
|= ARC_BUF_FLAG_ENCRYPTED
;
2794 flags
|= ARC_FILL_COMPRESSED
| ARC_FILL_ENCRYPTED
;
2795 } else if (compressed
&&
2796 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
2797 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2798 flags
|= ARC_FILL_COMPRESSED
;
2803 flags
|= ARC_FILL_NOAUTH
;
2807 * If the hdr's data can be shared then we share the data buffer and
2808 * set the appropriate bit in the hdr's b_flags to indicate the hdr is
2809 * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new
2810 * buffer to store the buf's data.
2812 * There are two additional restrictions here because we're sharing
2813 * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be
2814 * actively involved in an L2ARC write, because if this buf is used by
2815 * an arc_write() then the hdr's data buffer will be released when the
2816 * write completes, even though the L2ARC write might still be using it.
2817 * Second, the hdr's ABD must be linear so that the buf's user doesn't
2818 * need to be ABD-aware. It must be allocated via
2819 * zio_[data_]buf_alloc(), not as a page, because we need to be able
2820 * to abd_release_ownership_of_buf(), which isn't allowed on "linear
2821 * page" buffers because the ABD code needs to handle freeing them
2824 boolean_t can_share
= arc_can_share(hdr
, buf
) &&
2825 !HDR_L2_WRITING(hdr
) &&
2826 hdr
->b_l1hdr
.b_pabd
!= NULL
&&
2827 abd_is_linear(hdr
->b_l1hdr
.b_pabd
) &&
2828 !abd_is_linear_page(hdr
->b_l1hdr
.b_pabd
);
2830 /* Set up b_data and sharing */
2832 buf
->b_data
= abd_to_buf(hdr
->b_l1hdr
.b_pabd
);
2833 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2834 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2837 arc_get_data_buf(hdr
, arc_buf_size(buf
), buf
);
2838 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
2840 VERIFY3P(buf
->b_data
, !=, NULL
);
2842 hdr
->b_l1hdr
.b_buf
= buf
;
2843 hdr
->b_l1hdr
.b_bufcnt
+= 1;
2845 hdr
->b_crypt_hdr
.b_ebufcnt
+= 1;
2848 * If the user wants the data from the hdr, we need to either copy or
2849 * decompress the data.
2852 ASSERT3P(zb
, !=, NULL
);
2853 return (arc_buf_fill(buf
, spa
, zb
, flags
));
2859 static char *arc_onloan_tag
= "onloan";
2862 arc_loaned_bytes_update(int64_t delta
)
2864 atomic_add_64(&arc_loaned_bytes
, delta
);
2866 /* assert that it did not wrap around */
2867 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
2871 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
2872 * flight data by arc_tempreserve_space() until they are "returned". Loaned
2873 * buffers must be returned to the arc before they can be used by the DMU or
2877 arc_loan_buf(spa_t
*spa
, boolean_t is_metadata
, int size
)
2879 arc_buf_t
*buf
= arc_alloc_buf(spa
, arc_onloan_tag
,
2880 is_metadata
? ARC_BUFC_METADATA
: ARC_BUFC_DATA
, size
);
2882 arc_loaned_bytes_update(arc_buf_size(buf
));
2888 arc_loan_compressed_buf(spa_t
*spa
, uint64_t psize
, uint64_t lsize
,
2889 enum zio_compress compression_type
, uint8_t complevel
)
2891 arc_buf_t
*buf
= arc_alloc_compressed_buf(spa
, arc_onloan_tag
,
2892 psize
, lsize
, compression_type
, complevel
);
2894 arc_loaned_bytes_update(arc_buf_size(buf
));
2900 arc_loan_raw_buf(spa_t
*spa
, uint64_t dsobj
, boolean_t byteorder
,
2901 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
2902 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
2903 enum zio_compress compression_type
, uint8_t complevel
)
2905 arc_buf_t
*buf
= arc_alloc_raw_buf(spa
, arc_onloan_tag
, dsobj
,
2906 byteorder
, salt
, iv
, mac
, ot
, psize
, lsize
, compression_type
,
2909 atomic_add_64(&arc_loaned_bytes
, psize
);
2915 * Return a loaned arc buffer to the arc.
2918 arc_return_buf(arc_buf_t
*buf
, void *tag
)
2920 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2922 ASSERT3P(buf
->b_data
, !=, NULL
);
2923 ASSERT(HDR_HAS_L1HDR(hdr
));
2924 (void) zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2925 (void) zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2927 arc_loaned_bytes_update(-arc_buf_size(buf
));
2930 /* Detach an arc_buf from a dbuf (tag) */
2932 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
2934 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2936 ASSERT3P(buf
->b_data
, !=, NULL
);
2937 ASSERT(HDR_HAS_L1HDR(hdr
));
2938 (void) zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2939 (void) zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2941 arc_loaned_bytes_update(arc_buf_size(buf
));
2945 l2arc_free_abd_on_write(abd_t
*abd
, size_t size
, arc_buf_contents_t type
)
2947 l2arc_data_free_t
*df
= kmem_alloc(sizeof (*df
), KM_SLEEP
);
2950 df
->l2df_size
= size
;
2951 df
->l2df_type
= type
;
2952 mutex_enter(&l2arc_free_on_write_mtx
);
2953 list_insert_head(l2arc_free_on_write
, df
);
2954 mutex_exit(&l2arc_free_on_write_mtx
);
2958 arc_hdr_free_on_write(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
2960 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2961 arc_buf_contents_t type
= arc_buf_type(hdr
);
2962 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
2964 /* protected by hash lock, if in the hash table */
2965 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
2966 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2967 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
2969 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2972 (void) zfs_refcount_remove_many(&state
->arcs_size
, size
, hdr
);
2973 if (type
== ARC_BUFC_METADATA
) {
2974 arc_space_return(size
, ARC_SPACE_META
);
2976 ASSERT(type
== ARC_BUFC_DATA
);
2977 arc_space_return(size
, ARC_SPACE_DATA
);
2981 l2arc_free_abd_on_write(hdr
->b_crypt_hdr
.b_rabd
, size
, type
);
2983 l2arc_free_abd_on_write(hdr
->b_l1hdr
.b_pabd
, size
, type
);
2988 * Share the arc_buf_t's data with the hdr. Whenever we are sharing the
2989 * data buffer, we transfer the refcount ownership to the hdr and update
2990 * the appropriate kstats.
2993 arc_share_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2995 ASSERT(arc_can_share(hdr
, buf
));
2996 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2997 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
2998 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3001 * Start sharing the data buffer. We transfer the
3002 * refcount ownership to the hdr since it always owns
3003 * the refcount whenever an arc_buf_t is shared.
3005 zfs_refcount_transfer_ownership_many(&hdr
->b_l1hdr
.b_state
->arcs_size
,
3006 arc_hdr_size(hdr
), buf
, hdr
);
3007 hdr
->b_l1hdr
.b_pabd
= abd_get_from_buf(buf
->b_data
, arc_buf_size(buf
));
3008 abd_take_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
,
3009 HDR_ISTYPE_METADATA(hdr
));
3010 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3011 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
3014 * Since we've transferred ownership to the hdr we need
3015 * to increment its compressed and uncompressed kstats and
3016 * decrement the overhead size.
3018 ARCSTAT_INCR(arcstat_compressed_size
, arc_hdr_size(hdr
));
3019 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3020 ARCSTAT_INCR(arcstat_overhead_size
, -arc_buf_size(buf
));
3024 arc_unshare_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3026 ASSERT(arc_buf_is_shared(buf
));
3027 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3028 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3031 * We are no longer sharing this buffer so we need
3032 * to transfer its ownership to the rightful owner.
3034 zfs_refcount_transfer_ownership_many(&hdr
->b_l1hdr
.b_state
->arcs_size
,
3035 arc_hdr_size(hdr
), hdr
, buf
);
3036 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3037 abd_release_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
);
3038 abd_put(hdr
->b_l1hdr
.b_pabd
);
3039 hdr
->b_l1hdr
.b_pabd
= NULL
;
3040 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
3043 * Since the buffer is no longer shared between
3044 * the arc buf and the hdr, count it as overhead.
3046 ARCSTAT_INCR(arcstat_compressed_size
, -arc_hdr_size(hdr
));
3047 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3048 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
3052 * Remove an arc_buf_t from the hdr's buf list and return the last
3053 * arc_buf_t on the list. If no buffers remain on the list then return
3057 arc_buf_remove(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3059 ASSERT(HDR_HAS_L1HDR(hdr
));
3060 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3062 arc_buf_t
**bufp
= &hdr
->b_l1hdr
.b_buf
;
3063 arc_buf_t
*lastbuf
= NULL
;
3066 * Remove the buf from the hdr list and locate the last
3067 * remaining buffer on the list.
3069 while (*bufp
!= NULL
) {
3071 *bufp
= buf
->b_next
;
3074 * If we've removed a buffer in the middle of
3075 * the list then update the lastbuf and update
3078 if (*bufp
!= NULL
) {
3080 bufp
= &(*bufp
)->b_next
;
3084 ASSERT3P(lastbuf
, !=, buf
);
3085 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, lastbuf
!= NULL
);
3086 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, hdr
->b_l1hdr
.b_buf
!= NULL
);
3087 IMPLY(lastbuf
!= NULL
, ARC_BUF_LAST(lastbuf
));
3093 * Free up buf->b_data and pull the arc_buf_t off of the arc_buf_hdr_t's
3097 arc_buf_destroy_impl(arc_buf_t
*buf
)
3099 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3102 * Free up the data associated with the buf but only if we're not
3103 * sharing this with the hdr. If we are sharing it with the hdr, the
3104 * hdr is responsible for doing the free.
3106 if (buf
->b_data
!= NULL
) {
3108 * We're about to change the hdr's b_flags. We must either
3109 * hold the hash_lock or be undiscoverable.
3111 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3113 arc_cksum_verify(buf
);
3114 arc_buf_unwatch(buf
);
3116 if (arc_buf_is_shared(buf
)) {
3117 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3119 uint64_t size
= arc_buf_size(buf
);
3120 arc_free_data_buf(hdr
, buf
->b_data
, size
, buf
);
3121 ARCSTAT_INCR(arcstat_overhead_size
, -size
);
3125 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3126 hdr
->b_l1hdr
.b_bufcnt
-= 1;
3128 if (ARC_BUF_ENCRYPTED(buf
)) {
3129 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
3132 * If we have no more encrypted buffers and we've
3133 * already gotten a copy of the decrypted data we can
3134 * free b_rabd to save some space.
3136 if (hdr
->b_crypt_hdr
.b_ebufcnt
== 0 &&
3137 HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
!= NULL
&&
3138 !HDR_IO_IN_PROGRESS(hdr
)) {
3139 arc_hdr_free_abd(hdr
, B_TRUE
);
3144 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
3146 if (ARC_BUF_SHARED(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
3148 * If the current arc_buf_t is sharing its data buffer with the
3149 * hdr, then reassign the hdr's b_pabd to share it with the new
3150 * buffer at the end of the list. The shared buffer is always
3151 * the last one on the hdr's buffer list.
3153 * There is an equivalent case for compressed bufs, but since
3154 * they aren't guaranteed to be the last buf in the list and
3155 * that is an exceedingly rare case, we just allow that space be
3156 * wasted temporarily. We must also be careful not to share
3157 * encrypted buffers, since they cannot be shared.
3159 if (lastbuf
!= NULL
&& !ARC_BUF_ENCRYPTED(lastbuf
)) {
3160 /* Only one buf can be shared at once */
3161 VERIFY(!arc_buf_is_shared(lastbuf
));
3162 /* hdr is uncompressed so can't have compressed buf */
3163 VERIFY(!ARC_BUF_COMPRESSED(lastbuf
));
3165 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3166 arc_hdr_free_abd(hdr
, B_FALSE
);
3169 * We must setup a new shared block between the
3170 * last buffer and the hdr. The data would have
3171 * been allocated by the arc buf so we need to transfer
3172 * ownership to the hdr since it's now being shared.
3174 arc_share_buf(hdr
, lastbuf
);
3176 } else if (HDR_SHARED_DATA(hdr
)) {
3178 * Uncompressed shared buffers are always at the end
3179 * of the list. Compressed buffers don't have the
3180 * same requirements. This makes it hard to
3181 * simply assert that the lastbuf is shared so
3182 * we rely on the hdr's compression flags to determine
3183 * if we have a compressed, shared buffer.
3185 ASSERT3P(lastbuf
, !=, NULL
);
3186 ASSERT(arc_buf_is_shared(lastbuf
) ||
3187 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
3191 * Free the checksum if we're removing the last uncompressed buf from
3194 if (!arc_hdr_has_uncompressed_buf(hdr
)) {
3195 arc_cksum_free(hdr
);
3198 /* clean up the buf */
3200 kmem_cache_free(buf_cache
, buf
);
3204 arc_hdr_alloc_abd(arc_buf_hdr_t
*hdr
, int alloc_flags
)
3207 boolean_t alloc_rdata
= ((alloc_flags
& ARC_HDR_ALLOC_RDATA
) != 0);
3208 boolean_t do_adapt
= ((alloc_flags
& ARC_HDR_DO_ADAPT
) != 0);
3210 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
3211 ASSERT(HDR_HAS_L1HDR(hdr
));
3212 ASSERT(!HDR_SHARED_DATA(hdr
) || alloc_rdata
);
3213 IMPLY(alloc_rdata
, HDR_PROTECTED(hdr
));
3216 size
= HDR_GET_PSIZE(hdr
);
3217 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, ==, NULL
);
3218 hdr
->b_crypt_hdr
.b_rabd
= arc_get_data_abd(hdr
, size
, hdr
,
3220 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, !=, NULL
);
3221 ARCSTAT_INCR(arcstat_raw_size
, size
);
3223 size
= arc_hdr_size(hdr
);
3224 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3225 hdr
->b_l1hdr
.b_pabd
= arc_get_data_abd(hdr
, size
, hdr
,
3227 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3230 ARCSTAT_INCR(arcstat_compressed_size
, size
);
3231 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3235 arc_hdr_free_abd(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
3237 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
3239 ASSERT(HDR_HAS_L1HDR(hdr
));
3240 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
3241 IMPLY(free_rdata
, HDR_HAS_RABD(hdr
));
3244 * If the hdr is currently being written to the l2arc then
3245 * we defer freeing the data by adding it to the l2arc_free_on_write
3246 * list. The l2arc will free the data once it's finished
3247 * writing it to the l2arc device.
3249 if (HDR_L2_WRITING(hdr
)) {
3250 arc_hdr_free_on_write(hdr
, free_rdata
);
3251 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
3252 } else if (free_rdata
) {
3253 arc_free_data_abd(hdr
, hdr
->b_crypt_hdr
.b_rabd
, size
, hdr
);
3255 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
, size
, hdr
);
3259 hdr
->b_crypt_hdr
.b_rabd
= NULL
;
3260 ARCSTAT_INCR(arcstat_raw_size
, -size
);
3262 hdr
->b_l1hdr
.b_pabd
= NULL
;
3265 if (hdr
->b_l1hdr
.b_pabd
== NULL
&& !HDR_HAS_RABD(hdr
))
3266 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
3268 ARCSTAT_INCR(arcstat_compressed_size
, -size
);
3269 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3272 static arc_buf_hdr_t
*
3273 arc_hdr_alloc(uint64_t spa
, int32_t psize
, int32_t lsize
,
3274 boolean_t
protected, enum zio_compress compression_type
, uint8_t complevel
,
3275 arc_buf_contents_t type
, boolean_t alloc_rdata
)
3278 int flags
= ARC_HDR_DO_ADAPT
;
3280 VERIFY(type
== ARC_BUFC_DATA
|| type
== ARC_BUFC_METADATA
);
3282 hdr
= kmem_cache_alloc(hdr_full_crypt_cache
, KM_PUSHPAGE
);
3284 hdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
3286 flags
|= alloc_rdata
? ARC_HDR_ALLOC_RDATA
: 0;
3288 ASSERT(HDR_EMPTY(hdr
));
3289 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3290 HDR_SET_PSIZE(hdr
, psize
);
3291 HDR_SET_LSIZE(hdr
, lsize
);
3295 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L1HDR
);
3296 arc_hdr_set_compress(hdr
, compression_type
);
3297 hdr
->b_complevel
= complevel
;
3299 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
3301 hdr
->b_l1hdr
.b_state
= arc_anon
;
3302 hdr
->b_l1hdr
.b_arc_access
= 0;
3303 hdr
->b_l1hdr
.b_bufcnt
= 0;
3304 hdr
->b_l1hdr
.b_buf
= NULL
;
3307 * Allocate the hdr's buffer. This will contain either
3308 * the compressed or uncompressed data depending on the block
3309 * it references and compressed arc enablement.
3311 arc_hdr_alloc_abd(hdr
, flags
);
3312 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3318 * Transition between the two allocation states for the arc_buf_hdr struct.
3319 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
3320 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
3321 * version is used when a cache buffer is only in the L2ARC in order to reduce
3324 static arc_buf_hdr_t
*
3325 arc_hdr_realloc(arc_buf_hdr_t
*hdr
, kmem_cache_t
*old
, kmem_cache_t
*new)
3327 ASSERT(HDR_HAS_L2HDR(hdr
));
3329 arc_buf_hdr_t
*nhdr
;
3330 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3332 ASSERT((old
== hdr_full_cache
&& new == hdr_l2only_cache
) ||
3333 (old
== hdr_l2only_cache
&& new == hdr_full_cache
));
3336 * if the caller wanted a new full header and the header is to be
3337 * encrypted we will actually allocate the header from the full crypt
3338 * cache instead. The same applies to freeing from the old cache.
3340 if (HDR_PROTECTED(hdr
) && new == hdr_full_cache
)
3341 new = hdr_full_crypt_cache
;
3342 if (HDR_PROTECTED(hdr
) && old
== hdr_full_cache
)
3343 old
= hdr_full_crypt_cache
;
3345 nhdr
= kmem_cache_alloc(new, KM_PUSHPAGE
);
3347 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
3348 buf_hash_remove(hdr
);
3350 bcopy(hdr
, nhdr
, HDR_L2ONLY_SIZE
);
3352 if (new == hdr_full_cache
|| new == hdr_full_crypt_cache
) {
3353 arc_hdr_set_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3355 * arc_access and arc_change_state need to be aware that a
3356 * header has just come out of L2ARC, so we set its state to
3357 * l2c_only even though it's about to change.
3359 nhdr
->b_l1hdr
.b_state
= arc_l2c_only
;
3361 /* Verify previous threads set to NULL before freeing */
3362 ASSERT3P(nhdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3363 ASSERT(!HDR_HAS_RABD(hdr
));
3365 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3366 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
3367 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3370 * If we've reached here, We must have been called from
3371 * arc_evict_hdr(), as such we should have already been
3372 * removed from any ghost list we were previously on
3373 * (which protects us from racing with arc_evict_state),
3374 * thus no locking is needed during this check.
3376 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3379 * A buffer must not be moved into the arc_l2c_only
3380 * state if it's not finished being written out to the
3381 * l2arc device. Otherwise, the b_l1hdr.b_pabd field
3382 * might try to be accessed, even though it was removed.
3384 VERIFY(!HDR_L2_WRITING(hdr
));
3385 VERIFY3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3386 ASSERT(!HDR_HAS_RABD(hdr
));
3388 arc_hdr_clear_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3391 * The header has been reallocated so we need to re-insert it into any
3394 (void) buf_hash_insert(nhdr
, NULL
);
3396 ASSERT(list_link_active(&hdr
->b_l2hdr
.b_l2node
));
3398 mutex_enter(&dev
->l2ad_mtx
);
3401 * We must place the realloc'ed header back into the list at
3402 * the same spot. Otherwise, if it's placed earlier in the list,
3403 * l2arc_write_buffers() could find it during the function's
3404 * write phase, and try to write it out to the l2arc.
3406 list_insert_after(&dev
->l2ad_buflist
, hdr
, nhdr
);
3407 list_remove(&dev
->l2ad_buflist
, hdr
);
3409 mutex_exit(&dev
->l2ad_mtx
);
3412 * Since we're using the pointer address as the tag when
3413 * incrementing and decrementing the l2ad_alloc refcount, we
3414 * must remove the old pointer (that we're about to destroy) and
3415 * add the new pointer to the refcount. Otherwise we'd remove
3416 * the wrong pointer address when calling arc_hdr_destroy() later.
3419 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
,
3420 arc_hdr_size(hdr
), hdr
);
3421 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
3422 arc_hdr_size(nhdr
), nhdr
);
3424 buf_discard_identity(hdr
);
3425 kmem_cache_free(old
, hdr
);
3431 * This function allows an L1 header to be reallocated as a crypt
3432 * header and vice versa. If we are going to a crypt header, the
3433 * new fields will be zeroed out.
3435 static arc_buf_hdr_t
*
3436 arc_hdr_realloc_crypt(arc_buf_hdr_t
*hdr
, boolean_t need_crypt
)
3438 arc_buf_hdr_t
*nhdr
;
3440 kmem_cache_t
*ncache
, *ocache
;
3441 unsigned nsize
, osize
;
3444 * This function requires that hdr is in the arc_anon state.
3445 * Therefore it won't have any L2ARC data for us to worry
3448 ASSERT(HDR_HAS_L1HDR(hdr
));
3449 ASSERT(!HDR_HAS_L2HDR(hdr
));
3450 ASSERT3U(!!HDR_PROTECTED(hdr
), !=, need_crypt
);
3451 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3452 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3453 ASSERT(!list_link_active(&hdr
->b_l2hdr
.b_l2node
));
3454 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3457 ncache
= hdr_full_crypt_cache
;
3458 nsize
= sizeof (hdr
->b_crypt_hdr
);
3459 ocache
= hdr_full_cache
;
3460 osize
= HDR_FULL_SIZE
;
3462 ncache
= hdr_full_cache
;
3463 nsize
= HDR_FULL_SIZE
;
3464 ocache
= hdr_full_crypt_cache
;
3465 osize
= sizeof (hdr
->b_crypt_hdr
);
3468 nhdr
= kmem_cache_alloc(ncache
, KM_PUSHPAGE
);
3471 * Copy all members that aren't locks or condvars to the new header.
3472 * No lists are pointing to us (as we asserted above), so we don't
3473 * need to worry about the list nodes.
3475 nhdr
->b_dva
= hdr
->b_dva
;
3476 nhdr
->b_birth
= hdr
->b_birth
;
3477 nhdr
->b_type
= hdr
->b_type
;
3478 nhdr
->b_flags
= hdr
->b_flags
;
3479 nhdr
->b_psize
= hdr
->b_psize
;
3480 nhdr
->b_lsize
= hdr
->b_lsize
;
3481 nhdr
->b_spa
= hdr
->b_spa
;
3482 nhdr
->b_l1hdr
.b_freeze_cksum
= hdr
->b_l1hdr
.b_freeze_cksum
;
3483 nhdr
->b_l1hdr
.b_bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
3484 nhdr
->b_l1hdr
.b_byteswap
= hdr
->b_l1hdr
.b_byteswap
;
3485 nhdr
->b_l1hdr
.b_state
= hdr
->b_l1hdr
.b_state
;
3486 nhdr
->b_l1hdr
.b_arc_access
= hdr
->b_l1hdr
.b_arc_access
;
3487 nhdr
->b_l1hdr
.b_mru_hits
= hdr
->b_l1hdr
.b_mru_hits
;
3488 nhdr
->b_l1hdr
.b_mru_ghost_hits
= hdr
->b_l1hdr
.b_mru_ghost_hits
;
3489 nhdr
->b_l1hdr
.b_mfu_hits
= hdr
->b_l1hdr
.b_mfu_hits
;
3490 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= hdr
->b_l1hdr
.b_mfu_ghost_hits
;
3491 nhdr
->b_l1hdr
.b_l2_hits
= hdr
->b_l1hdr
.b_l2_hits
;
3492 nhdr
->b_l1hdr
.b_acb
= hdr
->b_l1hdr
.b_acb
;
3493 nhdr
->b_l1hdr
.b_pabd
= hdr
->b_l1hdr
.b_pabd
;
3496 * This zfs_refcount_add() exists only to ensure that the individual
3497 * arc buffers always point to a header that is referenced, avoiding
3498 * a small race condition that could trigger ASSERTs.
3500 (void) zfs_refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, FTAG
);
3501 nhdr
->b_l1hdr
.b_buf
= hdr
->b_l1hdr
.b_buf
;
3502 for (buf
= nhdr
->b_l1hdr
.b_buf
; buf
!= NULL
; buf
= buf
->b_next
) {
3503 mutex_enter(&buf
->b_evict_lock
);
3505 mutex_exit(&buf
->b_evict_lock
);
3508 zfs_refcount_transfer(&nhdr
->b_l1hdr
.b_refcnt
, &hdr
->b_l1hdr
.b_refcnt
);
3509 (void) zfs_refcount_remove(&nhdr
->b_l1hdr
.b_refcnt
, FTAG
);
3510 ASSERT0(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3513 arc_hdr_set_flags(nhdr
, ARC_FLAG_PROTECTED
);
3515 arc_hdr_clear_flags(nhdr
, ARC_FLAG_PROTECTED
);
3518 /* unset all members of the original hdr */
3519 bzero(&hdr
->b_dva
, sizeof (dva_t
));
3521 hdr
->b_type
= ARC_BUFC_INVALID
;
3526 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
3527 hdr
->b_l1hdr
.b_buf
= NULL
;
3528 hdr
->b_l1hdr
.b_bufcnt
= 0;
3529 hdr
->b_l1hdr
.b_byteswap
= 0;
3530 hdr
->b_l1hdr
.b_state
= NULL
;
3531 hdr
->b_l1hdr
.b_arc_access
= 0;
3532 hdr
->b_l1hdr
.b_mru_hits
= 0;
3533 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
3534 hdr
->b_l1hdr
.b_mfu_hits
= 0;
3535 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
3536 hdr
->b_l1hdr
.b_l2_hits
= 0;
3537 hdr
->b_l1hdr
.b_acb
= NULL
;
3538 hdr
->b_l1hdr
.b_pabd
= NULL
;
3540 if (ocache
== hdr_full_crypt_cache
) {
3541 ASSERT(!HDR_HAS_RABD(hdr
));
3542 hdr
->b_crypt_hdr
.b_ot
= DMU_OT_NONE
;
3543 hdr
->b_crypt_hdr
.b_ebufcnt
= 0;
3544 hdr
->b_crypt_hdr
.b_dsobj
= 0;
3545 bzero(hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3546 bzero(hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3547 bzero(hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3550 buf_discard_identity(hdr
);
3551 kmem_cache_free(ocache
, hdr
);
3557 * This function is used by the send / receive code to convert a newly
3558 * allocated arc_buf_t to one that is suitable for a raw encrypted write. It
3559 * is also used to allow the root objset block to be updated without altering
3560 * its embedded MACs. Both block types will always be uncompressed so we do not
3561 * have to worry about compression type or psize.
3564 arc_convert_to_raw(arc_buf_t
*buf
, uint64_t dsobj
, boolean_t byteorder
,
3565 dmu_object_type_t ot
, const uint8_t *salt
, const uint8_t *iv
,
3568 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3570 ASSERT(ot
== DMU_OT_DNODE
|| ot
== DMU_OT_OBJSET
);
3571 ASSERT(HDR_HAS_L1HDR(hdr
));
3572 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3574 buf
->b_flags
|= (ARC_BUF_FLAG_COMPRESSED
| ARC_BUF_FLAG_ENCRYPTED
);
3575 if (!HDR_PROTECTED(hdr
))
3576 hdr
= arc_hdr_realloc_crypt(hdr
, B_TRUE
);
3577 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3578 hdr
->b_crypt_hdr
.b_ot
= ot
;
3579 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3580 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3581 if (!arc_hdr_has_uncompressed_buf(hdr
))
3582 arc_cksum_free(hdr
);
3585 bcopy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3587 bcopy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3589 bcopy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3593 * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller.
3594 * The buf is returned thawed since we expect the consumer to modify it.
3597 arc_alloc_buf(spa_t
*spa
, void *tag
, arc_buf_contents_t type
, int32_t size
)
3599 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), size
, size
,
3600 B_FALSE
, ZIO_COMPRESS_OFF
, 0, type
, B_FALSE
);
3602 arc_buf_t
*buf
= NULL
;
3603 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
, B_FALSE
,
3604 B_FALSE
, B_FALSE
, &buf
));
3611 * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this
3612 * for bufs containing metadata.
3615 arc_alloc_compressed_buf(spa_t
*spa
, void *tag
, uint64_t psize
, uint64_t lsize
,
3616 enum zio_compress compression_type
, uint8_t complevel
)
3618 ASSERT3U(lsize
, >, 0);
3619 ASSERT3U(lsize
, >=, psize
);
3620 ASSERT3U(compression_type
, >, ZIO_COMPRESS_OFF
);
3621 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3623 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
3624 B_FALSE
, compression_type
, complevel
, ARC_BUFC_DATA
, B_FALSE
);
3626 arc_buf_t
*buf
= NULL
;
3627 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
,
3628 B_TRUE
, B_FALSE
, B_FALSE
, &buf
));
3630 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3632 if (!arc_buf_is_shared(buf
)) {
3634 * To ensure that the hdr has the correct data in it if we call
3635 * arc_untransform() on this buf before it's been written to
3636 * disk, it's easiest if we just set up sharing between the
3639 arc_hdr_free_abd(hdr
, B_FALSE
);
3640 arc_share_buf(hdr
, buf
);
3647 arc_alloc_raw_buf(spa_t
*spa
, void *tag
, uint64_t dsobj
, boolean_t byteorder
,
3648 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
3649 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
3650 enum zio_compress compression_type
, uint8_t complevel
)
3654 arc_buf_contents_t type
= DMU_OT_IS_METADATA(ot
) ?
3655 ARC_BUFC_METADATA
: ARC_BUFC_DATA
;
3657 ASSERT3U(lsize
, >, 0);
3658 ASSERT3U(lsize
, >=, psize
);
3659 ASSERT3U(compression_type
, >=, ZIO_COMPRESS_OFF
);
3660 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3662 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
, B_TRUE
,
3663 compression_type
, complevel
, type
, B_TRUE
);
3665 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3666 hdr
->b_crypt_hdr
.b_ot
= ot
;
3667 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3668 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3669 bcopy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3670 bcopy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3671 bcopy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3674 * This buffer will be considered encrypted even if the ot is not an
3675 * encrypted type. It will become authenticated instead in
3676 * arc_write_ready().
3679 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_TRUE
, B_TRUE
,
3680 B_FALSE
, B_FALSE
, &buf
));
3682 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3688 arc_hdr_l2hdr_destroy(arc_buf_hdr_t
*hdr
)
3690 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
3691 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
3692 uint64_t psize
= HDR_GET_PSIZE(hdr
);
3693 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
3695 ASSERT(MUTEX_HELD(&dev
->l2ad_mtx
));
3696 ASSERT(HDR_HAS_L2HDR(hdr
));
3698 list_remove(&dev
->l2ad_buflist
, hdr
);
3700 ARCSTAT_INCR(arcstat_l2_psize
, -psize
);
3701 ARCSTAT_INCR(arcstat_l2_lsize
, -HDR_GET_LSIZE(hdr
));
3703 vdev_space_update(dev
->l2ad_vdev
, -asize
, 0, 0);
3705 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
),
3707 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
3711 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
3713 if (HDR_HAS_L1HDR(hdr
)) {
3714 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
||
3715 hdr
->b_l1hdr
.b_bufcnt
> 0);
3716 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3717 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3719 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3720 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
3722 if (HDR_HAS_L2HDR(hdr
)) {
3723 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3724 boolean_t buflist_held
= MUTEX_HELD(&dev
->l2ad_mtx
);
3727 mutex_enter(&dev
->l2ad_mtx
);
3730 * Even though we checked this conditional above, we
3731 * need to check this again now that we have the
3732 * l2ad_mtx. This is because we could be racing with
3733 * another thread calling l2arc_evict() which might have
3734 * destroyed this header's L2 portion as we were waiting
3735 * to acquire the l2ad_mtx. If that happens, we don't
3736 * want to re-destroy the header's L2 portion.
3738 if (HDR_HAS_L2HDR(hdr
))
3739 arc_hdr_l2hdr_destroy(hdr
);
3742 mutex_exit(&dev
->l2ad_mtx
);
3746 * The header's identify can only be safely discarded once it is no
3747 * longer discoverable. This requires removing it from the hash table
3748 * and the l2arc header list. After this point the hash lock can not
3749 * be used to protect the header.
3751 if (!HDR_EMPTY(hdr
))
3752 buf_discard_identity(hdr
);
3754 if (HDR_HAS_L1HDR(hdr
)) {
3755 arc_cksum_free(hdr
);
3757 while (hdr
->b_l1hdr
.b_buf
!= NULL
)
3758 arc_buf_destroy_impl(hdr
->b_l1hdr
.b_buf
);
3760 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
3761 arc_hdr_free_abd(hdr
, B_FALSE
);
3763 if (HDR_HAS_RABD(hdr
))
3764 arc_hdr_free_abd(hdr
, B_TRUE
);
3767 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3768 if (HDR_HAS_L1HDR(hdr
)) {
3769 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3770 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
3772 if (!HDR_PROTECTED(hdr
)) {
3773 kmem_cache_free(hdr_full_cache
, hdr
);
3775 kmem_cache_free(hdr_full_crypt_cache
, hdr
);
3778 kmem_cache_free(hdr_l2only_cache
, hdr
);
3783 arc_buf_destroy(arc_buf_t
*buf
, void* tag
)
3785 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3787 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
3788 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
3789 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3790 VERIFY0(remove_reference(hdr
, NULL
, tag
));
3791 arc_hdr_destroy(hdr
);
3795 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
3796 mutex_enter(hash_lock
);
3798 ASSERT3P(hdr
, ==, buf
->b_hdr
);
3799 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3800 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3801 ASSERT3P(hdr
->b_l1hdr
.b_state
, !=, arc_anon
);
3802 ASSERT3P(buf
->b_data
, !=, NULL
);
3804 (void) remove_reference(hdr
, hash_lock
, tag
);
3805 arc_buf_destroy_impl(buf
);
3806 mutex_exit(hash_lock
);
3810 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
3811 * state of the header is dependent on its state prior to entering this
3812 * function. The following transitions are possible:
3814 * - arc_mru -> arc_mru_ghost
3815 * - arc_mfu -> arc_mfu_ghost
3816 * - arc_mru_ghost -> arc_l2c_only
3817 * - arc_mru_ghost -> deleted
3818 * - arc_mfu_ghost -> arc_l2c_only
3819 * - arc_mfu_ghost -> deleted
3822 arc_evict_hdr(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
3824 arc_state_t
*evicted_state
, *state
;
3825 int64_t bytes_evicted
= 0;
3826 int min_lifetime
= HDR_PRESCIENT_PREFETCH(hdr
) ?
3827 arc_min_prescient_prefetch_ms
: arc_min_prefetch_ms
;
3829 ASSERT(MUTEX_HELD(hash_lock
));
3830 ASSERT(HDR_HAS_L1HDR(hdr
));
3832 state
= hdr
->b_l1hdr
.b_state
;
3833 if (GHOST_STATE(state
)) {
3834 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3835 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3838 * l2arc_write_buffers() relies on a header's L1 portion
3839 * (i.e. its b_pabd field) during it's write phase.
3840 * Thus, we cannot push a header onto the arc_l2c_only
3841 * state (removing its L1 piece) until the header is
3842 * done being written to the l2arc.
3844 if (HDR_HAS_L2HDR(hdr
) && HDR_L2_WRITING(hdr
)) {
3845 ARCSTAT_BUMP(arcstat_evict_l2_skip
);
3846 return (bytes_evicted
);
3849 ARCSTAT_BUMP(arcstat_deleted
);
3850 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3852 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, hdr
);
3854 if (HDR_HAS_L2HDR(hdr
)) {
3855 ASSERT(hdr
->b_l1hdr
.b_pabd
== NULL
);
3856 ASSERT(!HDR_HAS_RABD(hdr
));
3858 * This buffer is cached on the 2nd Level ARC;
3859 * don't destroy the header.
3861 arc_change_state(arc_l2c_only
, hdr
, hash_lock
);
3863 * dropping from L1+L2 cached to L2-only,
3864 * realloc to remove the L1 header.
3866 hdr
= arc_hdr_realloc(hdr
, hdr_full_cache
,
3869 arc_change_state(arc_anon
, hdr
, hash_lock
);
3870 arc_hdr_destroy(hdr
);
3872 return (bytes_evicted
);
3875 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
3876 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
3878 /* prefetch buffers have a minimum lifespan */
3879 if (HDR_IO_IN_PROGRESS(hdr
) ||
3880 ((hdr
->b_flags
& (ARC_FLAG_PREFETCH
| ARC_FLAG_INDIRECT
)) &&
3881 ddi_get_lbolt() - hdr
->b_l1hdr
.b_arc_access
<
3882 MSEC_TO_TICK(min_lifetime
))) {
3883 ARCSTAT_BUMP(arcstat_evict_skip
);
3884 return (bytes_evicted
);
3887 ASSERT0(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3888 while (hdr
->b_l1hdr
.b_buf
) {
3889 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
3890 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
3891 ARCSTAT_BUMP(arcstat_mutex_miss
);
3894 if (buf
->b_data
!= NULL
)
3895 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3896 mutex_exit(&buf
->b_evict_lock
);
3897 arc_buf_destroy_impl(buf
);
3900 if (HDR_HAS_L2HDR(hdr
)) {
3901 ARCSTAT_INCR(arcstat_evict_l2_cached
, HDR_GET_LSIZE(hdr
));
3903 if (l2arc_write_eligible(hdr
->b_spa
, hdr
)) {
3904 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
3905 HDR_GET_LSIZE(hdr
));
3907 ARCSTAT_INCR(arcstat_evict_l2_ineligible
,
3908 HDR_GET_LSIZE(hdr
));
3912 if (hdr
->b_l1hdr
.b_bufcnt
== 0) {
3913 arc_cksum_free(hdr
);
3915 bytes_evicted
+= arc_hdr_size(hdr
);
3918 * If this hdr is being evicted and has a compressed
3919 * buffer then we discard it here before we change states.
3920 * This ensures that the accounting is updated correctly
3921 * in arc_free_data_impl().
3923 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
3924 arc_hdr_free_abd(hdr
, B_FALSE
);
3926 if (HDR_HAS_RABD(hdr
))
3927 arc_hdr_free_abd(hdr
, B_TRUE
);
3929 arc_change_state(evicted_state
, hdr
, hash_lock
);
3930 ASSERT(HDR_IN_HASH_TABLE(hdr
));
3931 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
3932 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, hdr
);
3935 return (bytes_evicted
);
3939 arc_set_need_free(void)
3941 ASSERT(MUTEX_HELD(&arc_evict_lock
));
3942 int64_t remaining
= arc_free_memory() - arc_sys_free
/ 2;
3943 arc_evict_waiter_t
*aw
= list_tail(&arc_evict_waiters
);
3945 arc_need_free
= MAX(-remaining
, 0);
3948 MAX(-remaining
, (int64_t)(aw
->aew_count
- arc_evict_count
));
3953 arc_evict_state_impl(multilist_t
*ml
, int idx
, arc_buf_hdr_t
*marker
,
3954 uint64_t spa
, int64_t bytes
)
3956 multilist_sublist_t
*mls
;
3957 uint64_t bytes_evicted
= 0;
3959 kmutex_t
*hash_lock
;
3960 int evict_count
= 0;
3962 ASSERT3P(marker
, !=, NULL
);
3963 IMPLY(bytes
< 0, bytes
== ARC_EVICT_ALL
);
3965 mls
= multilist_sublist_lock(ml
, idx
);
3967 for (hdr
= multilist_sublist_prev(mls
, marker
); hdr
!= NULL
;
3968 hdr
= multilist_sublist_prev(mls
, marker
)) {
3969 if ((bytes
!= ARC_EVICT_ALL
&& bytes_evicted
>= bytes
) ||
3970 (evict_count
>= zfs_arc_evict_batch_limit
))
3974 * To keep our iteration location, move the marker
3975 * forward. Since we're not holding hdr's hash lock, we
3976 * must be very careful and not remove 'hdr' from the
3977 * sublist. Otherwise, other consumers might mistake the
3978 * 'hdr' as not being on a sublist when they call the
3979 * multilist_link_active() function (they all rely on
3980 * the hash lock protecting concurrent insertions and
3981 * removals). multilist_sublist_move_forward() was
3982 * specifically implemented to ensure this is the case
3983 * (only 'marker' will be removed and re-inserted).
3985 multilist_sublist_move_forward(mls
, marker
);
3988 * The only case where the b_spa field should ever be
3989 * zero, is the marker headers inserted by
3990 * arc_evict_state(). It's possible for multiple threads
3991 * to be calling arc_evict_state() concurrently (e.g.
3992 * dsl_pool_close() and zio_inject_fault()), so we must
3993 * skip any markers we see from these other threads.
3995 if (hdr
->b_spa
== 0)
3998 /* we're only interested in evicting buffers of a certain spa */
3999 if (spa
!= 0 && hdr
->b_spa
!= spa
) {
4000 ARCSTAT_BUMP(arcstat_evict_skip
);
4004 hash_lock
= HDR_LOCK(hdr
);
4007 * We aren't calling this function from any code path
4008 * that would already be holding a hash lock, so we're
4009 * asserting on this assumption to be defensive in case
4010 * this ever changes. Without this check, it would be
4011 * possible to incorrectly increment arcstat_mutex_miss
4012 * below (e.g. if the code changed such that we called
4013 * this function with a hash lock held).
4015 ASSERT(!MUTEX_HELD(hash_lock
));
4017 if (mutex_tryenter(hash_lock
)) {
4018 uint64_t evicted
= arc_evict_hdr(hdr
, hash_lock
);
4019 mutex_exit(hash_lock
);
4021 bytes_evicted
+= evicted
;
4024 * If evicted is zero, arc_evict_hdr() must have
4025 * decided to skip this header, don't increment
4026 * evict_count in this case.
4032 ARCSTAT_BUMP(arcstat_mutex_miss
);
4036 multilist_sublist_unlock(mls
);
4039 * Increment the count of evicted bytes, and wake up any threads that
4040 * are waiting for the count to reach this value. Since the list is
4041 * ordered by ascending aew_count, we pop off the beginning of the
4042 * list until we reach the end, or a waiter that's past the current
4043 * "count". Doing this outside the loop reduces the number of times
4044 * we need to acquire the global arc_evict_lock.
4046 * Only wake when there's sufficient free memory in the system
4047 * (specifically, arc_sys_free/2, which by default is a bit more than
4048 * 1/64th of RAM). See the comments in arc_wait_for_eviction().
4050 mutex_enter(&arc_evict_lock
);
4051 arc_evict_count
+= bytes_evicted
;
4053 if ((int64_t)(arc_free_memory() - arc_sys_free
/ 2) > 0) {
4054 arc_evict_waiter_t
*aw
;
4055 while ((aw
= list_head(&arc_evict_waiters
)) != NULL
&&
4056 aw
->aew_count
<= arc_evict_count
) {
4057 list_remove(&arc_evict_waiters
, aw
);
4058 cv_broadcast(&aw
->aew_cv
);
4061 arc_set_need_free();
4062 mutex_exit(&arc_evict_lock
);
4065 * If the ARC size is reduced from arc_c_max to arc_c_min (especially
4066 * if the average cached block is small), eviction can be on-CPU for
4067 * many seconds. To ensure that other threads that may be bound to
4068 * this CPU are able to make progress, make a voluntary preemption
4073 return (bytes_evicted
);
4077 * Evict buffers from the given arc state, until we've removed the
4078 * specified number of bytes. Move the removed buffers to the
4079 * appropriate evict state.
4081 * This function makes a "best effort". It skips over any buffers
4082 * it can't get a hash_lock on, and so, may not catch all candidates.
4083 * It may also return without evicting as much space as requested.
4085 * If bytes is specified using the special value ARC_EVICT_ALL, this
4086 * will evict all available (i.e. unlocked and evictable) buffers from
4087 * the given arc state; which is used by arc_flush().
4090 arc_evict_state(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
4091 arc_buf_contents_t type
)
4093 uint64_t total_evicted
= 0;
4094 multilist_t
*ml
= state
->arcs_list
[type
];
4096 arc_buf_hdr_t
**markers
;
4098 IMPLY(bytes
< 0, bytes
== ARC_EVICT_ALL
);
4100 num_sublists
= multilist_get_num_sublists(ml
);
4103 * If we've tried to evict from each sublist, made some
4104 * progress, but still have not hit the target number of bytes
4105 * to evict, we want to keep trying. The markers allow us to
4106 * pick up where we left off for each individual sublist, rather
4107 * than starting from the tail each time.
4109 markers
= kmem_zalloc(sizeof (*markers
) * num_sublists
, KM_SLEEP
);
4110 for (int i
= 0; i
< num_sublists
; i
++) {
4111 multilist_sublist_t
*mls
;
4113 markers
[i
] = kmem_cache_alloc(hdr_full_cache
, KM_SLEEP
);
4116 * A b_spa of 0 is used to indicate that this header is
4117 * a marker. This fact is used in arc_evict_type() and
4118 * arc_evict_state_impl().
4120 markers
[i
]->b_spa
= 0;
4122 mls
= multilist_sublist_lock(ml
, i
);
4123 multilist_sublist_insert_tail(mls
, markers
[i
]);
4124 multilist_sublist_unlock(mls
);
4128 * While we haven't hit our target number of bytes to evict, or
4129 * we're evicting all available buffers.
4131 while (total_evicted
< bytes
|| bytes
== ARC_EVICT_ALL
) {
4132 int sublist_idx
= multilist_get_random_index(ml
);
4133 uint64_t scan_evicted
= 0;
4136 * Try to reduce pinned dnodes with a floor of arc_dnode_limit.
4137 * Request that 10% of the LRUs be scanned by the superblock
4140 if (type
== ARC_BUFC_DATA
&& aggsum_compare(&astat_dnode_size
,
4141 arc_dnode_size_limit
) > 0) {
4142 arc_prune_async((aggsum_upper_bound(&astat_dnode_size
) -
4143 arc_dnode_size_limit
) / sizeof (dnode_t
) /
4144 zfs_arc_dnode_reduce_percent
);
4148 * Start eviction using a randomly selected sublist,
4149 * this is to try and evenly balance eviction across all
4150 * sublists. Always starting at the same sublist
4151 * (e.g. index 0) would cause evictions to favor certain
4152 * sublists over others.
4154 for (int i
= 0; i
< num_sublists
; i
++) {
4155 uint64_t bytes_remaining
;
4156 uint64_t bytes_evicted
;
4158 if (bytes
== ARC_EVICT_ALL
)
4159 bytes_remaining
= ARC_EVICT_ALL
;
4160 else if (total_evicted
< bytes
)
4161 bytes_remaining
= bytes
- total_evicted
;
4165 bytes_evicted
= arc_evict_state_impl(ml
, sublist_idx
,
4166 markers
[sublist_idx
], spa
, bytes_remaining
);
4168 scan_evicted
+= bytes_evicted
;
4169 total_evicted
+= bytes_evicted
;
4171 /* we've reached the end, wrap to the beginning */
4172 if (++sublist_idx
>= num_sublists
)
4177 * If we didn't evict anything during this scan, we have
4178 * no reason to believe we'll evict more during another
4179 * scan, so break the loop.
4181 if (scan_evicted
== 0) {
4182 /* This isn't possible, let's make that obvious */
4183 ASSERT3S(bytes
, !=, 0);
4186 * When bytes is ARC_EVICT_ALL, the only way to
4187 * break the loop is when scan_evicted is zero.
4188 * In that case, we actually have evicted enough,
4189 * so we don't want to increment the kstat.
4191 if (bytes
!= ARC_EVICT_ALL
) {
4192 ASSERT3S(total_evicted
, <, bytes
);
4193 ARCSTAT_BUMP(arcstat_evict_not_enough
);
4200 for (int i
= 0; i
< num_sublists
; i
++) {
4201 multilist_sublist_t
*mls
= multilist_sublist_lock(ml
, i
);
4202 multilist_sublist_remove(mls
, markers
[i
]);
4203 multilist_sublist_unlock(mls
);
4205 kmem_cache_free(hdr_full_cache
, markers
[i
]);
4207 kmem_free(markers
, sizeof (*markers
) * num_sublists
);
4209 return (total_evicted
);
4213 * Flush all "evictable" data of the given type from the arc state
4214 * specified. This will not evict any "active" buffers (i.e. referenced).
4216 * When 'retry' is set to B_FALSE, the function will make a single pass
4217 * over the state and evict any buffers that it can. Since it doesn't
4218 * continually retry the eviction, it might end up leaving some buffers
4219 * in the ARC due to lock misses.
4221 * When 'retry' is set to B_TRUE, the function will continually retry the
4222 * eviction until *all* evictable buffers have been removed from the
4223 * state. As a result, if concurrent insertions into the state are
4224 * allowed (e.g. if the ARC isn't shutting down), this function might
4225 * wind up in an infinite loop, continually trying to evict buffers.
4228 arc_flush_state(arc_state_t
*state
, uint64_t spa
, arc_buf_contents_t type
,
4231 uint64_t evicted
= 0;
4233 while (zfs_refcount_count(&state
->arcs_esize
[type
]) != 0) {
4234 evicted
+= arc_evict_state(state
, spa
, ARC_EVICT_ALL
, type
);
4244 * Evict the specified number of bytes from the state specified,
4245 * restricting eviction to the spa and type given. This function
4246 * prevents us from trying to evict more from a state's list than
4247 * is "evictable", and to skip evicting altogether when passed a
4248 * negative value for "bytes". In contrast, arc_evict_state() will
4249 * evict everything it can, when passed a negative value for "bytes".
4252 arc_evict_impl(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
4253 arc_buf_contents_t type
)
4257 if (bytes
> 0 && zfs_refcount_count(&state
->arcs_esize
[type
]) > 0) {
4258 delta
= MIN(zfs_refcount_count(&state
->arcs_esize
[type
]),
4260 return (arc_evict_state(state
, spa
, delta
, type
));
4267 * The goal of this function is to evict enough meta data buffers from the
4268 * ARC in order to enforce the arc_meta_limit. Achieving this is slightly
4269 * more complicated than it appears because it is common for data buffers
4270 * to have holds on meta data buffers. In addition, dnode meta data buffers
4271 * will be held by the dnodes in the block preventing them from being freed.
4272 * This means we can't simply traverse the ARC and expect to always find
4273 * enough unheld meta data buffer to release.
4275 * Therefore, this function has been updated to make alternating passes
4276 * over the ARC releasing data buffers and then newly unheld meta data
4277 * buffers. This ensures forward progress is maintained and meta_used
4278 * will decrease. Normally this is sufficient, but if required the ARC
4279 * will call the registered prune callbacks causing dentry and inodes to
4280 * be dropped from the VFS cache. This will make dnode meta data buffers
4281 * available for reclaim.
4284 arc_evict_meta_balanced(uint64_t meta_used
)
4286 int64_t delta
, prune
= 0, adjustmnt
;
4287 uint64_t total_evicted
= 0;
4288 arc_buf_contents_t type
= ARC_BUFC_DATA
;
4289 int restarts
= MAX(zfs_arc_meta_adjust_restarts
, 0);
4293 * This slightly differs than the way we evict from the mru in
4294 * arc_evict because we don't have a "target" value (i.e. no
4295 * "meta" arc_p). As a result, I think we can completely
4296 * cannibalize the metadata in the MRU before we evict the
4297 * metadata from the MFU. I think we probably need to implement a
4298 * "metadata arc_p" value to do this properly.
4300 adjustmnt
= meta_used
- arc_meta_limit
;
4302 if (adjustmnt
> 0 &&
4303 zfs_refcount_count(&arc_mru
->arcs_esize
[type
]) > 0) {
4304 delta
= MIN(zfs_refcount_count(&arc_mru
->arcs_esize
[type
]),
4306 total_evicted
+= arc_evict_impl(arc_mru
, 0, delta
, type
);
4311 * We can't afford to recalculate adjustmnt here. If we do,
4312 * new metadata buffers can sneak into the MRU or ANON lists,
4313 * thus penalize the MFU metadata. Although the fudge factor is
4314 * small, it has been empirically shown to be significant for
4315 * certain workloads (e.g. creating many empty directories). As
4316 * such, we use the original calculation for adjustmnt, and
4317 * simply decrement the amount of data evicted from the MRU.
4320 if (adjustmnt
> 0 &&
4321 zfs_refcount_count(&arc_mfu
->arcs_esize
[type
]) > 0) {
4322 delta
= MIN(zfs_refcount_count(&arc_mfu
->arcs_esize
[type
]),
4324 total_evicted
+= arc_evict_impl(arc_mfu
, 0, delta
, type
);
4327 adjustmnt
= meta_used
- arc_meta_limit
;
4329 if (adjustmnt
> 0 &&
4330 zfs_refcount_count(&arc_mru_ghost
->arcs_esize
[type
]) > 0) {
4331 delta
= MIN(adjustmnt
,
4332 zfs_refcount_count(&arc_mru_ghost
->arcs_esize
[type
]));
4333 total_evicted
+= arc_evict_impl(arc_mru_ghost
, 0, delta
, type
);
4337 if (adjustmnt
> 0 &&
4338 zfs_refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]) > 0) {
4339 delta
= MIN(adjustmnt
,
4340 zfs_refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]));
4341 total_evicted
+= arc_evict_impl(arc_mfu_ghost
, 0, delta
, type
);
4345 * If after attempting to make the requested adjustment to the ARC
4346 * the meta limit is still being exceeded then request that the
4347 * higher layers drop some cached objects which have holds on ARC
4348 * meta buffers. Requests to the upper layers will be made with
4349 * increasingly large scan sizes until the ARC is below the limit.
4351 if (meta_used
> arc_meta_limit
) {
4352 if (type
== ARC_BUFC_DATA
) {
4353 type
= ARC_BUFC_METADATA
;
4355 type
= ARC_BUFC_DATA
;
4357 if (zfs_arc_meta_prune
) {
4358 prune
+= zfs_arc_meta_prune
;
4359 arc_prune_async(prune
);
4368 return (total_evicted
);
4372 * Evict metadata buffers from the cache, such that arc_meta_used is
4373 * capped by the arc_meta_limit tunable.
4376 arc_evict_meta_only(uint64_t meta_used
)
4378 uint64_t total_evicted
= 0;
4382 * If we're over the meta limit, we want to evict enough
4383 * metadata to get back under the meta limit. We don't want to
4384 * evict so much that we drop the MRU below arc_p, though. If
4385 * we're over the meta limit more than we're over arc_p, we
4386 * evict some from the MRU here, and some from the MFU below.
4388 target
= MIN((int64_t)(meta_used
- arc_meta_limit
),
4389 (int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) +
4390 zfs_refcount_count(&arc_mru
->arcs_size
) - arc_p
));
4392 total_evicted
+= arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4395 * Similar to the above, we want to evict enough bytes to get us
4396 * below the meta limit, but not so much as to drop us below the
4397 * space allotted to the MFU (which is defined as arc_c - arc_p).
4399 target
= MIN((int64_t)(meta_used
- arc_meta_limit
),
4400 (int64_t)(zfs_refcount_count(&arc_mfu
->arcs_size
) -
4403 total_evicted
+= arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4405 return (total_evicted
);
4409 arc_evict_meta(uint64_t meta_used
)
4411 if (zfs_arc_meta_strategy
== ARC_STRATEGY_META_ONLY
)
4412 return (arc_evict_meta_only(meta_used
));
4414 return (arc_evict_meta_balanced(meta_used
));
4418 * Return the type of the oldest buffer in the given arc state
4420 * This function will select a random sublist of type ARC_BUFC_DATA and
4421 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist
4422 * is compared, and the type which contains the "older" buffer will be
4425 static arc_buf_contents_t
4426 arc_evict_type(arc_state_t
*state
)
4428 multilist_t
*data_ml
= state
->arcs_list
[ARC_BUFC_DATA
];
4429 multilist_t
*meta_ml
= state
->arcs_list
[ARC_BUFC_METADATA
];
4430 int data_idx
= multilist_get_random_index(data_ml
);
4431 int meta_idx
= multilist_get_random_index(meta_ml
);
4432 multilist_sublist_t
*data_mls
;
4433 multilist_sublist_t
*meta_mls
;
4434 arc_buf_contents_t type
;
4435 arc_buf_hdr_t
*data_hdr
;
4436 arc_buf_hdr_t
*meta_hdr
;
4439 * We keep the sublist lock until we're finished, to prevent
4440 * the headers from being destroyed via arc_evict_state().
4442 data_mls
= multilist_sublist_lock(data_ml
, data_idx
);
4443 meta_mls
= multilist_sublist_lock(meta_ml
, meta_idx
);
4446 * These two loops are to ensure we skip any markers that
4447 * might be at the tail of the lists due to arc_evict_state().
4450 for (data_hdr
= multilist_sublist_tail(data_mls
); data_hdr
!= NULL
;
4451 data_hdr
= multilist_sublist_prev(data_mls
, data_hdr
)) {
4452 if (data_hdr
->b_spa
!= 0)
4456 for (meta_hdr
= multilist_sublist_tail(meta_mls
); meta_hdr
!= NULL
;
4457 meta_hdr
= multilist_sublist_prev(meta_mls
, meta_hdr
)) {
4458 if (meta_hdr
->b_spa
!= 0)
4462 if (data_hdr
== NULL
&& meta_hdr
== NULL
) {
4463 type
= ARC_BUFC_DATA
;
4464 } else if (data_hdr
== NULL
) {
4465 ASSERT3P(meta_hdr
, !=, NULL
);
4466 type
= ARC_BUFC_METADATA
;
4467 } else if (meta_hdr
== NULL
) {
4468 ASSERT3P(data_hdr
, !=, NULL
);
4469 type
= ARC_BUFC_DATA
;
4471 ASSERT3P(data_hdr
, !=, NULL
);
4472 ASSERT3P(meta_hdr
, !=, NULL
);
4474 /* The headers can't be on the sublist without an L1 header */
4475 ASSERT(HDR_HAS_L1HDR(data_hdr
));
4476 ASSERT(HDR_HAS_L1HDR(meta_hdr
));
4478 if (data_hdr
->b_l1hdr
.b_arc_access
<
4479 meta_hdr
->b_l1hdr
.b_arc_access
) {
4480 type
= ARC_BUFC_DATA
;
4482 type
= ARC_BUFC_METADATA
;
4486 multilist_sublist_unlock(meta_mls
);
4487 multilist_sublist_unlock(data_mls
);
4493 * Evict buffers from the cache, such that arc_size is capped by arc_c.
4498 uint64_t total_evicted
= 0;
4501 uint64_t asize
= aggsum_value(&arc_size
);
4502 uint64_t ameta
= aggsum_value(&arc_meta_used
);
4505 * If we're over arc_meta_limit, we want to correct that before
4506 * potentially evicting data buffers below.
4508 total_evicted
+= arc_evict_meta(ameta
);
4513 * If we're over the target cache size, we want to evict enough
4514 * from the list to get back to our target size. We don't want
4515 * to evict too much from the MRU, such that it drops below
4516 * arc_p. So, if we're over our target cache size more than
4517 * the MRU is over arc_p, we'll evict enough to get back to
4518 * arc_p here, and then evict more from the MFU below.
4520 target
= MIN((int64_t)(asize
- arc_c
),
4521 (int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) +
4522 zfs_refcount_count(&arc_mru
->arcs_size
) + ameta
- arc_p
));
4525 * If we're below arc_meta_min, always prefer to evict data.
4526 * Otherwise, try to satisfy the requested number of bytes to
4527 * evict from the type which contains older buffers; in an
4528 * effort to keep newer buffers in the cache regardless of their
4529 * type. If we cannot satisfy the number of bytes from this
4530 * type, spill over into the next type.
4532 if (arc_evict_type(arc_mru
) == ARC_BUFC_METADATA
&&
4533 ameta
> arc_meta_min
) {
4534 bytes
= arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4535 total_evicted
+= bytes
;
4538 * If we couldn't evict our target number of bytes from
4539 * metadata, we try to get the rest from data.
4544 arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
4546 bytes
= arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
4547 total_evicted
+= bytes
;
4550 * If we couldn't evict our target number of bytes from
4551 * data, we try to get the rest from metadata.
4556 arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4560 * Re-sum ARC stats after the first round of evictions.
4562 asize
= aggsum_value(&arc_size
);
4563 ameta
= aggsum_value(&arc_meta_used
);
4569 * Now that we've tried to evict enough from the MRU to get its
4570 * size back to arc_p, if we're still above the target cache
4571 * size, we evict the rest from the MFU.
4573 target
= asize
- arc_c
;
4575 if (arc_evict_type(arc_mfu
) == ARC_BUFC_METADATA
&&
4576 ameta
> arc_meta_min
) {
4577 bytes
= arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4578 total_evicted
+= bytes
;
4581 * If we couldn't evict our target number of bytes from
4582 * metadata, we try to get the rest from data.
4587 arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
4589 bytes
= arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
4590 total_evicted
+= bytes
;
4593 * If we couldn't evict our target number of bytes from
4594 * data, we try to get the rest from data.
4599 arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4603 * Adjust ghost lists
4605 * In addition to the above, the ARC also defines target values
4606 * for the ghost lists. The sum of the mru list and mru ghost
4607 * list should never exceed the target size of the cache, and
4608 * the sum of the mru list, mfu list, mru ghost list, and mfu
4609 * ghost list should never exceed twice the target size of the
4610 * cache. The following logic enforces these limits on the ghost
4611 * caches, and evicts from them as needed.
4613 target
= zfs_refcount_count(&arc_mru
->arcs_size
) +
4614 zfs_refcount_count(&arc_mru_ghost
->arcs_size
) - arc_c
;
4616 bytes
= arc_evict_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_DATA
);
4617 total_evicted
+= bytes
;
4622 arc_evict_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_METADATA
);
4625 * We assume the sum of the mru list and mfu list is less than
4626 * or equal to arc_c (we enforced this above), which means we
4627 * can use the simpler of the two equations below:
4629 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c
4630 * mru ghost + mfu ghost <= arc_c
4632 target
= zfs_refcount_count(&arc_mru_ghost
->arcs_size
) +
4633 zfs_refcount_count(&arc_mfu_ghost
->arcs_size
) - arc_c
;
4635 bytes
= arc_evict_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_DATA
);
4636 total_evicted
+= bytes
;
4641 arc_evict_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_METADATA
);
4643 return (total_evicted
);
4647 arc_flush(spa_t
*spa
, boolean_t retry
)
4652 * If retry is B_TRUE, a spa must not be specified since we have
4653 * no good way to determine if all of a spa's buffers have been
4654 * evicted from an arc state.
4656 ASSERT(!retry
|| spa
== 0);
4659 guid
= spa_load_guid(spa
);
4661 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_DATA
, retry
);
4662 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_METADATA
, retry
);
4664 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_DATA
, retry
);
4665 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_METADATA
, retry
);
4667 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4668 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4670 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4671 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4675 arc_reduce_target_size(int64_t to_free
)
4677 uint64_t asize
= aggsum_value(&arc_size
);
4680 * All callers want the ARC to actually evict (at least) this much
4681 * memory. Therefore we reduce from the lower of the current size and
4682 * the target size. This way, even if arc_c is much higher than
4683 * arc_size (as can be the case after many calls to arc_freed(), we will
4684 * immediately have arc_c < arc_size and therefore the arc_evict_zthr
4687 uint64_t c
= MIN(arc_c
, asize
);
4689 if (c
> to_free
&& c
- to_free
> arc_c_min
) {
4690 arc_c
= c
- to_free
;
4691 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
4693 arc_p
= (arc_c
>> 1);
4694 ASSERT(arc_c
>= arc_c_min
);
4695 ASSERT((int64_t)arc_p
>= 0);
4700 if (asize
> arc_c
) {
4701 /* See comment in arc_evict_cb_check() on why lock+flag */
4702 mutex_enter(&arc_evict_lock
);
4703 arc_evict_needed
= B_TRUE
;
4704 mutex_exit(&arc_evict_lock
);
4705 zthr_wakeup(arc_evict_zthr
);
4710 * Determine if the system is under memory pressure and is asking
4711 * to reclaim memory. A return value of B_TRUE indicates that the system
4712 * is under memory pressure and that the arc should adjust accordingly.
4715 arc_reclaim_needed(void)
4717 return (arc_available_memory() < 0);
4721 arc_kmem_reap_soon(void)
4724 kmem_cache_t
*prev_cache
= NULL
;
4725 kmem_cache_t
*prev_data_cache
= NULL
;
4726 extern kmem_cache_t
*zio_buf_cache
[];
4727 extern kmem_cache_t
*zio_data_buf_cache
[];
4730 if ((aggsum_compare(&arc_meta_used
, arc_meta_limit
) >= 0) &&
4731 zfs_arc_meta_prune
) {
4733 * We are exceeding our meta-data cache limit.
4734 * Prune some entries to release holds on meta-data.
4736 arc_prune_async(zfs_arc_meta_prune
);
4740 * Reclaim unused memory from all kmem caches.
4746 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
4748 /* reach upper limit of cache size on 32-bit */
4749 if (zio_buf_cache
[i
] == NULL
)
4752 if (zio_buf_cache
[i
] != prev_cache
) {
4753 prev_cache
= zio_buf_cache
[i
];
4754 kmem_cache_reap_now(zio_buf_cache
[i
]);
4756 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
4757 prev_data_cache
= zio_data_buf_cache
[i
];
4758 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
4761 kmem_cache_reap_now(buf_cache
);
4762 kmem_cache_reap_now(hdr_full_cache
);
4763 kmem_cache_reap_now(hdr_l2only_cache
);
4764 kmem_cache_reap_now(zfs_btree_leaf_cache
);
4765 abd_cache_reap_now();
4770 arc_evict_cb_check(void *arg
, zthr_t
*zthr
)
4773 * This is necessary so that any changes which may have been made to
4774 * many of the zfs_arc_* module parameters will be propagated to
4775 * their actual internal variable counterparts. Without this,
4776 * changing those module params at runtime would have no effect.
4778 arc_tuning_update(B_FALSE
);
4781 * This is necessary in order to keep the kstat information
4782 * up to date for tools that display kstat data such as the
4783 * mdb ::arc dcmd and the Linux crash utility. These tools
4784 * typically do not call kstat's update function, but simply
4785 * dump out stats from the most recent update. Without
4786 * this call, these commands may show stale stats for the
4787 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
4788 * with this change, the data might be up to 1 second
4789 * out of date(the arc_evict_zthr has a maximum sleep
4790 * time of 1 second); but that should suffice. The
4791 * arc_state_t structures can be queried directly if more
4792 * accurate information is needed.
4794 if (arc_ksp
!= NULL
)
4795 arc_ksp
->ks_update(arc_ksp
, KSTAT_READ
);
4798 * We have to rely on arc_wait_for_eviction() to tell us when to
4799 * evict, rather than checking if we are overflowing here, so that we
4800 * are sure to not leave arc_wait_for_eviction() waiting on aew_cv.
4801 * If we have become "not overflowing" since arc_wait_for_eviction()
4802 * checked, we need to wake it up. We could broadcast the CV here,
4803 * but arc_wait_for_eviction() may have not yet gone to sleep. We
4804 * would need to use a mutex to ensure that this function doesn't
4805 * broadcast until arc_wait_for_eviction() has gone to sleep (e.g.
4806 * the arc_evict_lock). However, the lock ordering of such a lock
4807 * would necessarily be incorrect with respect to the zthr_lock,
4808 * which is held before this function is called, and is held by
4809 * arc_wait_for_eviction() when it calls zthr_wakeup().
4811 return (arc_evict_needed
);
4815 * Keep arc_size under arc_c by running arc_evict which evicts data
4820 arc_evict_cb(void *arg
, zthr_t
*zthr
)
4822 uint64_t evicted
= 0;
4823 fstrans_cookie_t cookie
= spl_fstrans_mark();
4825 /* Evict from cache */
4826 evicted
= arc_evict();
4829 * If evicted is zero, we couldn't evict anything
4830 * via arc_evict(). This could be due to hash lock
4831 * collisions, but more likely due to the majority of
4832 * arc buffers being unevictable. Therefore, even if
4833 * arc_size is above arc_c, another pass is unlikely to
4834 * be helpful and could potentially cause us to enter an
4835 * infinite loop. Additionally, zthr_iscancelled() is
4836 * checked here so that if the arc is shutting down, the
4837 * broadcast will wake any remaining arc evict waiters.
4839 mutex_enter(&arc_evict_lock
);
4840 arc_evict_needed
= !zthr_iscancelled(arc_evict_zthr
) &&
4841 evicted
> 0 && aggsum_compare(&arc_size
, arc_c
) > 0;
4842 if (!arc_evict_needed
) {
4844 * We're either no longer overflowing, or we
4845 * can't evict anything more, so we should wake
4846 * arc_get_data_impl() sooner.
4848 arc_evict_waiter_t
*aw
;
4849 while ((aw
= list_remove_head(&arc_evict_waiters
)) != NULL
) {
4850 cv_broadcast(&aw
->aew_cv
);
4852 arc_set_need_free();
4854 mutex_exit(&arc_evict_lock
);
4855 spl_fstrans_unmark(cookie
);
4860 arc_reap_cb_check(void *arg
, zthr_t
*zthr
)
4862 int64_t free_memory
= arc_available_memory();
4863 static int reap_cb_check_counter
= 0;
4866 * If a kmem reap is already active, don't schedule more. We must
4867 * check for this because kmem_cache_reap_soon() won't actually
4868 * block on the cache being reaped (this is to prevent callers from
4869 * becoming implicitly blocked by a system-wide kmem reap -- which,
4870 * on a system with many, many full magazines, can take minutes).
4872 if (!kmem_cache_reap_active() && free_memory
< 0) {
4874 arc_no_grow
= B_TRUE
;
4877 * Wait at least zfs_grow_retry (default 5) seconds
4878 * before considering growing.
4880 arc_growtime
= gethrtime() + SEC2NSEC(arc_grow_retry
);
4882 } else if (free_memory
< arc_c
>> arc_no_grow_shift
) {
4883 arc_no_grow
= B_TRUE
;
4884 } else if (gethrtime() >= arc_growtime
) {
4885 arc_no_grow
= B_FALSE
;
4889 * Called unconditionally every 60 seconds to reclaim unused
4890 * zstd compression and decompression context. This is done
4891 * here to avoid the need for an independent thread.
4893 if (!((reap_cb_check_counter
++) % 60))
4894 zfs_zstd_cache_reap_now();
4900 * Keep enough free memory in the system by reaping the ARC's kmem
4901 * caches. To cause more slabs to be reapable, we may reduce the
4902 * target size of the cache (arc_c), causing the arc_evict_cb()
4903 * to free more buffers.
4907 arc_reap_cb(void *arg
, zthr_t
*zthr
)
4909 int64_t free_memory
;
4910 fstrans_cookie_t cookie
= spl_fstrans_mark();
4913 * Kick off asynchronous kmem_reap()'s of all our caches.
4915 arc_kmem_reap_soon();
4918 * Wait at least arc_kmem_cache_reap_retry_ms between
4919 * arc_kmem_reap_soon() calls. Without this check it is possible to
4920 * end up in a situation where we spend lots of time reaping
4921 * caches, while we're near arc_c_min. Waiting here also gives the
4922 * subsequent free memory check a chance of finding that the
4923 * asynchronous reap has already freed enough memory, and we don't
4924 * need to call arc_reduce_target_size().
4926 delay((hz
* arc_kmem_cache_reap_retry_ms
+ 999) / 1000);
4929 * Reduce the target size as needed to maintain the amount of free
4930 * memory in the system at a fraction of the arc_size (1/128th by
4931 * default). If oversubscribed (free_memory < 0) then reduce the
4932 * target arc_size by the deficit amount plus the fractional
4933 * amount. If free memory is positive but less then the fractional
4934 * amount, reduce by what is needed to hit the fractional amount.
4936 free_memory
= arc_available_memory();
4939 (arc_c
>> arc_shrink_shift
) - free_memory
;
4941 arc_reduce_target_size(to_free
);
4943 spl_fstrans_unmark(cookie
);
4948 * Determine the amount of memory eligible for eviction contained in the
4949 * ARC. All clean data reported by the ghost lists can always be safely
4950 * evicted. Due to arc_c_min, the same does not hold for all clean data
4951 * contained by the regular mru and mfu lists.
4953 * In the case of the regular mru and mfu lists, we need to report as
4954 * much clean data as possible, such that evicting that same reported
4955 * data will not bring arc_size below arc_c_min. Thus, in certain
4956 * circumstances, the total amount of clean data in the mru and mfu
4957 * lists might not actually be evictable.
4959 * The following two distinct cases are accounted for:
4961 * 1. The sum of the amount of dirty data contained by both the mru and
4962 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
4963 * is greater than or equal to arc_c_min.
4964 * (i.e. amount of dirty data >= arc_c_min)
4966 * This is the easy case; all clean data contained by the mru and mfu
4967 * lists is evictable. Evicting all clean data can only drop arc_size
4968 * to the amount of dirty data, which is greater than arc_c_min.
4970 * 2. The sum of the amount of dirty data contained by both the mru and
4971 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
4972 * is less than arc_c_min.
4973 * (i.e. arc_c_min > amount of dirty data)
4975 * 2.1. arc_size is greater than or equal arc_c_min.
4976 * (i.e. arc_size >= arc_c_min > amount of dirty data)
4978 * In this case, not all clean data from the regular mru and mfu
4979 * lists is actually evictable; we must leave enough clean data
4980 * to keep arc_size above arc_c_min. Thus, the maximum amount of
4981 * evictable data from the two lists combined, is exactly the
4982 * difference between arc_size and arc_c_min.
4984 * 2.2. arc_size is less than arc_c_min
4985 * (i.e. arc_c_min > arc_size > amount of dirty data)
4987 * In this case, none of the data contained in the mru and mfu
4988 * lists is evictable, even if it's clean. Since arc_size is
4989 * already below arc_c_min, evicting any more would only
4990 * increase this negative difference.
4993 #endif /* _KERNEL */
4996 * Adapt arc info given the number of bytes we are trying to add and
4997 * the state that we are coming from. This function is only called
4998 * when we are adding new content to the cache.
5001 arc_adapt(int bytes
, arc_state_t
*state
)
5004 uint64_t arc_p_min
= (arc_c
>> arc_p_min_shift
);
5005 int64_t mrug_size
= zfs_refcount_count(&arc_mru_ghost
->arcs_size
);
5006 int64_t mfug_size
= zfs_refcount_count(&arc_mfu_ghost
->arcs_size
);
5010 * Adapt the target size of the MRU list:
5011 * - if we just hit in the MRU ghost list, then increase
5012 * the target size of the MRU list.
5013 * - if we just hit in the MFU ghost list, then increase
5014 * the target size of the MFU list by decreasing the
5015 * target size of the MRU list.
5017 if (state
== arc_mru_ghost
) {
5018 mult
= (mrug_size
>= mfug_size
) ? 1 : (mfug_size
/ mrug_size
);
5019 if (!zfs_arc_p_dampener_disable
)
5020 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
5022 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
5023 } else if (state
== arc_mfu_ghost
) {
5026 mult
= (mfug_size
>= mrug_size
) ? 1 : (mrug_size
/ mfug_size
);
5027 if (!zfs_arc_p_dampener_disable
)
5028 mult
= MIN(mult
, 10);
5030 delta
= MIN(bytes
* mult
, arc_p
);
5031 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
5033 ASSERT((int64_t)arc_p
>= 0);
5036 * Wake reap thread if we do not have any available memory
5038 if (arc_reclaim_needed()) {
5039 zthr_wakeup(arc_reap_zthr
);
5046 if (arc_c
>= arc_c_max
)
5050 * If we're within (2 * maxblocksize) bytes of the target
5051 * cache size, increment the target cache size
5053 ASSERT3U(arc_c
, >=, 2ULL << SPA_MAXBLOCKSHIFT
);
5054 if (aggsum_upper_bound(&arc_size
) >=
5055 arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
5056 atomic_add_64(&arc_c
, (int64_t)bytes
);
5057 if (arc_c
> arc_c_max
)
5059 else if (state
== arc_anon
)
5060 atomic_add_64(&arc_p
, (int64_t)bytes
);
5064 ASSERT((int64_t)arc_p
>= 0);
5068 * Check if arc_size has grown past our upper threshold, determined by
5069 * zfs_arc_overflow_shift.
5072 arc_is_overflowing(void)
5074 /* Always allow at least one block of overflow */
5075 int64_t overflow
= MAX(SPA_MAXBLOCKSIZE
,
5076 arc_c
>> zfs_arc_overflow_shift
);
5079 * We just compare the lower bound here for performance reasons. Our
5080 * primary goals are to make sure that the arc never grows without
5081 * bound, and that it can reach its maximum size. This check
5082 * accomplishes both goals. The maximum amount we could run over by is
5083 * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block
5084 * in the ARC. In practice, that's in the tens of MB, which is low
5085 * enough to be safe.
5087 return (aggsum_lower_bound(&arc_size
) >= (int64_t)arc_c
+ overflow
);
5091 arc_get_data_abd(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
,
5094 arc_buf_contents_t type
= arc_buf_type(hdr
);
5096 arc_get_data_impl(hdr
, size
, tag
, do_adapt
);
5097 if (type
== ARC_BUFC_METADATA
) {
5098 return (abd_alloc(size
, B_TRUE
));
5100 ASSERT(type
== ARC_BUFC_DATA
);
5101 return (abd_alloc(size
, B_FALSE
));
5106 arc_get_data_buf(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5108 arc_buf_contents_t type
= arc_buf_type(hdr
);
5110 arc_get_data_impl(hdr
, size
, tag
, B_TRUE
);
5111 if (type
== ARC_BUFC_METADATA
) {
5112 return (zio_buf_alloc(size
));
5114 ASSERT(type
== ARC_BUFC_DATA
);
5115 return (zio_data_buf_alloc(size
));
5120 * Wait for the specified amount of data (in bytes) to be evicted from the
5121 * ARC, and for there to be sufficient free memory in the system. Waiting for
5122 * eviction ensures that the memory used by the ARC decreases. Waiting for
5123 * free memory ensures that the system won't run out of free pages, regardless
5124 * of ARC behavior and settings. See arc_lowmem_init().
5127 arc_wait_for_eviction(uint64_t amount
)
5129 mutex_enter(&arc_evict_lock
);
5130 if (arc_is_overflowing()) {
5131 arc_evict_needed
= B_TRUE
;
5132 zthr_wakeup(arc_evict_zthr
);
5135 arc_evict_waiter_t aw
;
5136 list_link_init(&aw
.aew_node
);
5137 cv_init(&aw
.aew_cv
, NULL
, CV_DEFAULT
, NULL
);
5139 arc_evict_waiter_t
*last
=
5140 list_tail(&arc_evict_waiters
);
5142 ASSERT3U(last
->aew_count
, >, arc_evict_count
);
5143 aw
.aew_count
= last
->aew_count
+ amount
;
5145 aw
.aew_count
= arc_evict_count
+ amount
;
5148 list_insert_tail(&arc_evict_waiters
, &aw
);
5150 arc_set_need_free();
5152 DTRACE_PROBE3(arc__wait__for__eviction
,
5154 uint64_t, arc_evict_count
,
5155 uint64_t, aw
.aew_count
);
5158 * We will be woken up either when arc_evict_count
5159 * reaches aew_count, or when the ARC is no longer
5160 * overflowing and eviction completes.
5162 cv_wait(&aw
.aew_cv
, &arc_evict_lock
);
5165 * In case of "false" wakeup, we will still be on the
5168 if (list_link_active(&aw
.aew_node
))
5169 list_remove(&arc_evict_waiters
, &aw
);
5171 cv_destroy(&aw
.aew_cv
);
5174 mutex_exit(&arc_evict_lock
);
5178 * Allocate a block and return it to the caller. If we are hitting the
5179 * hard limit for the cache size, we must sleep, waiting for the eviction
5180 * thread to catch up. If we're past the target size but below the hard
5181 * limit, we'll only signal the reclaim thread and continue on.
5184 arc_get_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
,
5187 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5188 arc_buf_contents_t type
= arc_buf_type(hdr
);
5191 arc_adapt(size
, state
);
5194 * If arc_size is currently overflowing, we must be adding data
5195 * faster than we are evicting. To ensure we don't compound the
5196 * problem by adding more data and forcing arc_size to grow even
5197 * further past it's target size, we wait for the eviction thread to
5198 * make some progress. We also wait for there to be sufficient free
5199 * memory in the system, as measured by arc_free_memory().
5201 * Specifically, we wait for zfs_arc_eviction_pct percent of the
5202 * requested size to be evicted. This should be more than 100%, to
5203 * ensure that that progress is also made towards getting arc_size
5204 * under arc_c. See the comment above zfs_arc_eviction_pct.
5206 * We do the overflowing check without holding the arc_evict_lock to
5207 * reduce lock contention in this hot path. Note that
5208 * arc_wait_for_eviction() will acquire the lock and check again to
5209 * ensure we are truly overflowing before blocking.
5211 if (arc_is_overflowing()) {
5212 arc_wait_for_eviction(size
*
5213 zfs_arc_eviction_pct
/ 100);
5216 VERIFY3U(hdr
->b_type
, ==, type
);
5217 if (type
== ARC_BUFC_METADATA
) {
5218 arc_space_consume(size
, ARC_SPACE_META
);
5220 arc_space_consume(size
, ARC_SPACE_DATA
);
5224 * Update the state size. Note that ghost states have a
5225 * "ghost size" and so don't need to be updated.
5227 if (!GHOST_STATE(state
)) {
5229 (void) zfs_refcount_add_many(&state
->arcs_size
, size
, tag
);
5232 * If this is reached via arc_read, the link is
5233 * protected by the hash lock. If reached via
5234 * arc_buf_alloc, the header should not be accessed by
5235 * any other thread. And, if reached via arc_read_done,
5236 * the hash lock will protect it if it's found in the
5237 * hash table; otherwise no other thread should be
5238 * trying to [add|remove]_reference it.
5240 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5241 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5242 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
5247 * If we are growing the cache, and we are adding anonymous
5248 * data, and we have outgrown arc_p, update arc_p
5250 if (aggsum_upper_bound(&arc_size
) < arc_c
&&
5251 hdr
->b_l1hdr
.b_state
== arc_anon
&&
5252 (zfs_refcount_count(&arc_anon
->arcs_size
) +
5253 zfs_refcount_count(&arc_mru
->arcs_size
) > arc_p
))
5254 arc_p
= MIN(arc_c
, arc_p
+ size
);
5259 arc_free_data_abd(arc_buf_hdr_t
*hdr
, abd_t
*abd
, uint64_t size
, void *tag
)
5261 arc_free_data_impl(hdr
, size
, tag
);
5266 arc_free_data_buf(arc_buf_hdr_t
*hdr
, void *buf
, uint64_t size
, void *tag
)
5268 arc_buf_contents_t type
= arc_buf_type(hdr
);
5270 arc_free_data_impl(hdr
, size
, tag
);
5271 if (type
== ARC_BUFC_METADATA
) {
5272 zio_buf_free(buf
, size
);
5274 ASSERT(type
== ARC_BUFC_DATA
);
5275 zio_data_buf_free(buf
, size
);
5280 * Free the arc data buffer.
5283 arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5285 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5286 arc_buf_contents_t type
= arc_buf_type(hdr
);
5288 /* protected by hash lock, if in the hash table */
5289 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5290 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5291 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
5293 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
5296 (void) zfs_refcount_remove_many(&state
->arcs_size
, size
, tag
);
5298 VERIFY3U(hdr
->b_type
, ==, type
);
5299 if (type
== ARC_BUFC_METADATA
) {
5300 arc_space_return(size
, ARC_SPACE_META
);
5302 ASSERT(type
== ARC_BUFC_DATA
);
5303 arc_space_return(size
, ARC_SPACE_DATA
);
5308 * This routine is called whenever a buffer is accessed.
5309 * NOTE: the hash lock is dropped in this function.
5312 arc_access(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
5316 ASSERT(MUTEX_HELD(hash_lock
));
5317 ASSERT(HDR_HAS_L1HDR(hdr
));
5319 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
5321 * This buffer is not in the cache, and does not
5322 * appear in our "ghost" list. Add the new buffer
5326 ASSERT0(hdr
->b_l1hdr
.b_arc_access
);
5327 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5328 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5329 arc_change_state(arc_mru
, hdr
, hash_lock
);
5331 } else if (hdr
->b_l1hdr
.b_state
== arc_mru
) {
5332 now
= ddi_get_lbolt();
5335 * If this buffer is here because of a prefetch, then either:
5336 * - clear the flag if this is a "referencing" read
5337 * (any subsequent access will bump this into the MFU state).
5339 * - move the buffer to the head of the list if this is
5340 * another prefetch (to make it less likely to be evicted).
5342 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5343 if (zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
5344 /* link protected by hash lock */
5345 ASSERT(multilist_link_active(
5346 &hdr
->b_l1hdr
.b_arc_node
));
5348 arc_hdr_clear_flags(hdr
,
5350 ARC_FLAG_PRESCIENT_PREFETCH
);
5351 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
5352 ARCSTAT_BUMP(arcstat_mru_hits
);
5354 hdr
->b_l1hdr
.b_arc_access
= now
;
5359 * This buffer has been "accessed" only once so far,
5360 * but it is still in the cache. Move it to the MFU
5363 if (ddi_time_after(now
, hdr
->b_l1hdr
.b_arc_access
+
5366 * More than 125ms have passed since we
5367 * instantiated this buffer. Move it to the
5368 * most frequently used state.
5370 hdr
->b_l1hdr
.b_arc_access
= now
;
5371 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5372 arc_change_state(arc_mfu
, hdr
, hash_lock
);
5374 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
5375 ARCSTAT_BUMP(arcstat_mru_hits
);
5376 } else if (hdr
->b_l1hdr
.b_state
== arc_mru_ghost
) {
5377 arc_state_t
*new_state
;
5379 * This buffer has been "accessed" recently, but
5380 * was evicted from the cache. Move it to the
5384 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5385 new_state
= arc_mru
;
5386 if (zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0) {
5387 arc_hdr_clear_flags(hdr
,
5389 ARC_FLAG_PRESCIENT_PREFETCH
);
5391 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5393 new_state
= arc_mfu
;
5394 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5397 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5398 arc_change_state(new_state
, hdr
, hash_lock
);
5400 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_ghost_hits
);
5401 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
5402 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu
) {
5404 * This buffer has been accessed more than once and is
5405 * still in the cache. Keep it in the MFU state.
5407 * NOTE: an add_reference() that occurred when we did
5408 * the arc_read() will have kicked this off the list.
5409 * If it was a prefetch, we will explicitly move it to
5410 * the head of the list now.
5413 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_hits
);
5414 ARCSTAT_BUMP(arcstat_mfu_hits
);
5415 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5416 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu_ghost
) {
5417 arc_state_t
*new_state
= arc_mfu
;
5419 * This buffer has been accessed more than once but has
5420 * been evicted from the cache. Move it back to the
5424 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5426 * This is a prefetch access...
5427 * move this block back to the MRU state.
5429 new_state
= arc_mru
;
5432 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5433 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5434 arc_change_state(new_state
, hdr
, hash_lock
);
5436 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_ghost_hits
);
5437 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
5438 } else if (hdr
->b_l1hdr
.b_state
== arc_l2c_only
) {
5440 * This buffer is on the 2nd Level ARC.
5443 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5444 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5445 arc_change_state(arc_mfu
, hdr
, hash_lock
);
5447 cmn_err(CE_PANIC
, "invalid arc state 0x%p",
5448 hdr
->b_l1hdr
.b_state
);
5453 * This routine is called by dbuf_hold() to update the arc_access() state
5454 * which otherwise would be skipped for entries in the dbuf cache.
5457 arc_buf_access(arc_buf_t
*buf
)
5459 mutex_enter(&buf
->b_evict_lock
);
5460 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5463 * Avoid taking the hash_lock when possible as an optimization.
5464 * The header must be checked again under the hash_lock in order
5465 * to handle the case where it is concurrently being released.
5467 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5468 mutex_exit(&buf
->b_evict_lock
);
5472 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
5473 mutex_enter(hash_lock
);
5475 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5476 mutex_exit(hash_lock
);
5477 mutex_exit(&buf
->b_evict_lock
);
5478 ARCSTAT_BUMP(arcstat_access_skip
);
5482 mutex_exit(&buf
->b_evict_lock
);
5484 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
5485 hdr
->b_l1hdr
.b_state
== arc_mfu
);
5487 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
5488 arc_access(hdr
, hash_lock
);
5489 mutex_exit(hash_lock
);
5491 ARCSTAT_BUMP(arcstat_hits
);
5492 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
) && !HDR_PRESCIENT_PREFETCH(hdr
),
5493 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
), data
, metadata
, hits
);
5496 /* a generic arc_read_done_func_t which you can use */
5499 arc_bcopy_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5500 arc_buf_t
*buf
, void *arg
)
5505 bcopy(buf
->b_data
, arg
, arc_buf_size(buf
));
5506 arc_buf_destroy(buf
, arg
);
5509 /* a generic arc_read_done_func_t */
5512 arc_getbuf_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5513 arc_buf_t
*buf
, void *arg
)
5515 arc_buf_t
**bufp
= arg
;
5518 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
5521 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
5523 ASSERT(buf
->b_data
!= NULL
);
5528 arc_hdr_verify(arc_buf_hdr_t
*hdr
, blkptr_t
*bp
)
5530 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
5531 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, 0);
5532 ASSERT3U(arc_hdr_get_compress(hdr
), ==, ZIO_COMPRESS_OFF
);
5534 if (HDR_COMPRESSION_ENABLED(hdr
)) {
5535 ASSERT3U(arc_hdr_get_compress(hdr
), ==,
5536 BP_GET_COMPRESS(bp
));
5538 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
5539 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, BP_GET_PSIZE(bp
));
5540 ASSERT3U(!!HDR_PROTECTED(hdr
), ==, BP_IS_PROTECTED(bp
));
5545 arc_read_done(zio_t
*zio
)
5547 blkptr_t
*bp
= zio
->io_bp
;
5548 arc_buf_hdr_t
*hdr
= zio
->io_private
;
5549 kmutex_t
*hash_lock
= NULL
;
5550 arc_callback_t
*callback_list
;
5551 arc_callback_t
*acb
;
5552 boolean_t freeable
= B_FALSE
;
5555 * The hdr was inserted into hash-table and removed from lists
5556 * prior to starting I/O. We should find this header, since
5557 * it's in the hash table, and it should be legit since it's
5558 * not possible to evict it during the I/O. The only possible
5559 * reason for it not to be found is if we were freed during the
5562 if (HDR_IN_HASH_TABLE(hdr
)) {
5563 arc_buf_hdr_t
*found
;
5565 ASSERT3U(hdr
->b_birth
, ==, BP_PHYSICAL_BIRTH(zio
->io_bp
));
5566 ASSERT3U(hdr
->b_dva
.dva_word
[0], ==,
5567 BP_IDENTITY(zio
->io_bp
)->dva_word
[0]);
5568 ASSERT3U(hdr
->b_dva
.dva_word
[1], ==,
5569 BP_IDENTITY(zio
->io_bp
)->dva_word
[1]);
5571 found
= buf_hash_find(hdr
->b_spa
, zio
->io_bp
, &hash_lock
);
5573 ASSERT((found
== hdr
&&
5574 DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
5575 (found
== hdr
&& HDR_L2_READING(hdr
)));
5576 ASSERT3P(hash_lock
, !=, NULL
);
5579 if (BP_IS_PROTECTED(bp
)) {
5580 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
5581 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
5582 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
5583 hdr
->b_crypt_hdr
.b_iv
);
5585 if (BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
) {
5588 tmpbuf
= abd_borrow_buf_copy(zio
->io_abd
,
5589 sizeof (zil_chain_t
));
5590 zio_crypt_decode_mac_zil(tmpbuf
,
5591 hdr
->b_crypt_hdr
.b_mac
);
5592 abd_return_buf(zio
->io_abd
, tmpbuf
,
5593 sizeof (zil_chain_t
));
5595 zio_crypt_decode_mac_bp(bp
, hdr
->b_crypt_hdr
.b_mac
);
5599 if (zio
->io_error
== 0) {
5600 /* byteswap if necessary */
5601 if (BP_SHOULD_BYTESWAP(zio
->io_bp
)) {
5602 if (BP_GET_LEVEL(zio
->io_bp
) > 0) {
5603 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
5605 hdr
->b_l1hdr
.b_byteswap
=
5606 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
5609 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
5611 if (!HDR_L2_READING(hdr
)) {
5612 hdr
->b_complevel
= zio
->io_prop
.zp_complevel
;
5616 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_EVICTED
);
5617 if (l2arc_noprefetch
&& HDR_PREFETCH(hdr
))
5618 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2CACHE
);
5620 callback_list
= hdr
->b_l1hdr
.b_acb
;
5621 ASSERT3P(callback_list
, !=, NULL
);
5623 if (hash_lock
&& zio
->io_error
== 0 &&
5624 hdr
->b_l1hdr
.b_state
== arc_anon
) {
5626 * Only call arc_access on anonymous buffers. This is because
5627 * if we've issued an I/O for an evicted buffer, we've already
5628 * called arc_access (to prevent any simultaneous readers from
5629 * getting confused).
5631 arc_access(hdr
, hash_lock
);
5635 * If a read request has a callback (i.e. acb_done is not NULL), then we
5636 * make a buf containing the data according to the parameters which were
5637 * passed in. The implementation of arc_buf_alloc_impl() ensures that we
5638 * aren't needlessly decompressing the data multiple times.
5640 int callback_cnt
= 0;
5641 for (acb
= callback_list
; acb
!= NULL
; acb
= acb
->acb_next
) {
5647 if (zio
->io_error
!= 0)
5650 int error
= arc_buf_alloc_impl(hdr
, zio
->io_spa
,
5651 &acb
->acb_zb
, acb
->acb_private
, acb
->acb_encrypted
,
5652 acb
->acb_compressed
, acb
->acb_noauth
, B_TRUE
,
5656 * Assert non-speculative zios didn't fail because an
5657 * encryption key wasn't loaded
5659 ASSERT((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) ||
5663 * If we failed to decrypt, report an error now (as the zio
5664 * layer would have done if it had done the transforms).
5666 if (error
== ECKSUM
) {
5667 ASSERT(BP_IS_PROTECTED(bp
));
5668 error
= SET_ERROR(EIO
);
5669 if ((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
5670 spa_log_error(zio
->io_spa
, &acb
->acb_zb
);
5671 (void) zfs_ereport_post(
5672 FM_EREPORT_ZFS_AUTHENTICATION
,
5673 zio
->io_spa
, NULL
, &acb
->acb_zb
, zio
, 0);
5679 * Decompression or decryption failed. Set
5680 * io_error so that when we call acb_done
5681 * (below), we will indicate that the read
5682 * failed. Note that in the unusual case
5683 * where one callback is compressed and another
5684 * uncompressed, we will mark all of them
5685 * as failed, even though the uncompressed
5686 * one can't actually fail. In this case,
5687 * the hdr will not be anonymous, because
5688 * if there are multiple callbacks, it's
5689 * because multiple threads found the same
5690 * arc buf in the hash table.
5692 zio
->io_error
= error
;
5697 * If there are multiple callbacks, we must have the hash lock,
5698 * because the only way for multiple threads to find this hdr is
5699 * in the hash table. This ensures that if there are multiple
5700 * callbacks, the hdr is not anonymous. If it were anonymous,
5701 * we couldn't use arc_buf_destroy() in the error case below.
5703 ASSERT(callback_cnt
< 2 || hash_lock
!= NULL
);
5705 hdr
->b_l1hdr
.b_acb
= NULL
;
5706 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5707 if (callback_cnt
== 0)
5708 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
5710 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
) ||
5711 callback_list
!= NULL
);
5713 if (zio
->io_error
== 0) {
5714 arc_hdr_verify(hdr
, zio
->io_bp
);
5716 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
5717 if (hdr
->b_l1hdr
.b_state
!= arc_anon
)
5718 arc_change_state(arc_anon
, hdr
, hash_lock
);
5719 if (HDR_IN_HASH_TABLE(hdr
))
5720 buf_hash_remove(hdr
);
5721 freeable
= zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
5725 * Broadcast before we drop the hash_lock to avoid the possibility
5726 * that the hdr (and hence the cv) might be freed before we get to
5727 * the cv_broadcast().
5729 cv_broadcast(&hdr
->b_l1hdr
.b_cv
);
5731 if (hash_lock
!= NULL
) {
5732 mutex_exit(hash_lock
);
5735 * This block was freed while we waited for the read to
5736 * complete. It has been removed from the hash table and
5737 * moved to the anonymous state (so that it won't show up
5740 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
5741 freeable
= zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
5744 /* execute each callback and free its structure */
5745 while ((acb
= callback_list
) != NULL
) {
5746 if (acb
->acb_done
!= NULL
) {
5747 if (zio
->io_error
!= 0 && acb
->acb_buf
!= NULL
) {
5749 * If arc_buf_alloc_impl() fails during
5750 * decompression, the buf will still be
5751 * allocated, and needs to be freed here.
5753 arc_buf_destroy(acb
->acb_buf
,
5755 acb
->acb_buf
= NULL
;
5757 acb
->acb_done(zio
, &zio
->io_bookmark
, zio
->io_bp
,
5758 acb
->acb_buf
, acb
->acb_private
);
5761 if (acb
->acb_zio_dummy
!= NULL
) {
5762 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
5763 zio_nowait(acb
->acb_zio_dummy
);
5766 callback_list
= acb
->acb_next
;
5767 kmem_free(acb
, sizeof (arc_callback_t
));
5771 arc_hdr_destroy(hdr
);
5775 * "Read" the block at the specified DVA (in bp) via the
5776 * cache. If the block is found in the cache, invoke the provided
5777 * callback immediately and return. Note that the `zio' parameter
5778 * in the callback will be NULL in this case, since no IO was
5779 * required. If the block is not in the cache pass the read request
5780 * on to the spa with a substitute callback function, so that the
5781 * requested block will be added to the cache.
5783 * If a read request arrives for a block that has a read in-progress,
5784 * either wait for the in-progress read to complete (and return the
5785 * results); or, if this is a read with a "done" func, add a record
5786 * to the read to invoke the "done" func when the read completes,
5787 * and return; or just return.
5789 * arc_read_done() will invoke all the requested "done" functions
5790 * for readers of this block.
5793 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
5794 arc_read_done_func_t
*done
, void *private, zio_priority_t priority
,
5795 int zio_flags
, arc_flags_t
*arc_flags
, const zbookmark_phys_t
*zb
)
5797 arc_buf_hdr_t
*hdr
= NULL
;
5798 kmutex_t
*hash_lock
= NULL
;
5800 uint64_t guid
= spa_load_guid(spa
);
5801 boolean_t compressed_read
= (zio_flags
& ZIO_FLAG_RAW_COMPRESS
) != 0;
5802 boolean_t encrypted_read
= BP_IS_ENCRYPTED(bp
) &&
5803 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
5804 boolean_t noauth_read
= BP_IS_AUTHENTICATED(bp
) &&
5805 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
5806 boolean_t embedded_bp
= !!BP_IS_EMBEDDED(bp
);
5809 ASSERT(!embedded_bp
||
5810 BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
);
5811 ASSERT(!BP_IS_HOLE(bp
));
5812 ASSERT(!BP_IS_REDACTED(bp
));
5815 * Normally SPL_FSTRANS will already be set since kernel threads which
5816 * expect to call the DMU interfaces will set it when created. System
5817 * calls are similarly handled by setting/cleaning the bit in the
5818 * registered callback (module/os/.../zfs/zpl_*).
5820 * External consumers such as Lustre which call the exported DMU
5821 * interfaces may not have set SPL_FSTRANS. To avoid a deadlock
5822 * on the hash_lock always set and clear the bit.
5824 fstrans_cookie_t cookie
= spl_fstrans_mark();
5828 * Embedded BP's have no DVA and require no I/O to "read".
5829 * Create an anonymous arc buf to back it.
5831 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
5835 * Determine if we have an L1 cache hit or a cache miss. For simplicity
5836 * we maintain encrypted data separately from compressed / uncompressed
5837 * data. If the user is requesting raw encrypted data and we don't have
5838 * that in the header we will read from disk to guarantee that we can
5839 * get it even if the encryption keys aren't loaded.
5841 if (hdr
!= NULL
&& HDR_HAS_L1HDR(hdr
) && (HDR_HAS_RABD(hdr
) ||
5842 (hdr
->b_l1hdr
.b_pabd
!= NULL
&& !encrypted_read
))) {
5843 arc_buf_t
*buf
= NULL
;
5844 *arc_flags
|= ARC_FLAG_CACHED
;
5846 if (HDR_IO_IN_PROGRESS(hdr
)) {
5847 zio_t
*head_zio
= hdr
->b_l1hdr
.b_acb
->acb_zio_head
;
5849 if (*arc_flags
& ARC_FLAG_CACHED_ONLY
) {
5850 mutex_exit(hash_lock
);
5851 ARCSTAT_BUMP(arcstat_cached_only_in_progress
);
5852 rc
= SET_ERROR(ENOENT
);
5856 ASSERT3P(head_zio
, !=, NULL
);
5857 if ((hdr
->b_flags
& ARC_FLAG_PRIO_ASYNC_READ
) &&
5858 priority
== ZIO_PRIORITY_SYNC_READ
) {
5860 * This is a sync read that needs to wait for
5861 * an in-flight async read. Request that the
5862 * zio have its priority upgraded.
5864 zio_change_priority(head_zio
, priority
);
5865 DTRACE_PROBE1(arc__async__upgrade__sync
,
5866 arc_buf_hdr_t
*, hdr
);
5867 ARCSTAT_BUMP(arcstat_async_upgrade_sync
);
5869 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
5870 arc_hdr_clear_flags(hdr
,
5871 ARC_FLAG_PREDICTIVE_PREFETCH
);
5874 if (*arc_flags
& ARC_FLAG_WAIT
) {
5875 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
5876 mutex_exit(hash_lock
);
5879 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
5882 arc_callback_t
*acb
= NULL
;
5884 acb
= kmem_zalloc(sizeof (arc_callback_t
),
5886 acb
->acb_done
= done
;
5887 acb
->acb_private
= private;
5888 acb
->acb_compressed
= compressed_read
;
5889 acb
->acb_encrypted
= encrypted_read
;
5890 acb
->acb_noauth
= noauth_read
;
5893 acb
->acb_zio_dummy
= zio_null(pio
,
5894 spa
, NULL
, NULL
, NULL
, zio_flags
);
5896 ASSERT3P(acb
->acb_done
, !=, NULL
);
5897 acb
->acb_zio_head
= head_zio
;
5898 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
5899 hdr
->b_l1hdr
.b_acb
= acb
;
5900 mutex_exit(hash_lock
);
5903 mutex_exit(hash_lock
);
5907 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
5908 hdr
->b_l1hdr
.b_state
== arc_mfu
);
5911 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
5913 * This is a demand read which does not have to
5914 * wait for i/o because we did a predictive
5915 * prefetch i/o for it, which has completed.
5918 arc__demand__hit__predictive__prefetch
,
5919 arc_buf_hdr_t
*, hdr
);
5921 arcstat_demand_hit_predictive_prefetch
);
5922 arc_hdr_clear_flags(hdr
,
5923 ARC_FLAG_PREDICTIVE_PREFETCH
);
5926 if (hdr
->b_flags
& ARC_FLAG_PRESCIENT_PREFETCH
) {
5928 arcstat_demand_hit_prescient_prefetch
);
5929 arc_hdr_clear_flags(hdr
,
5930 ARC_FLAG_PRESCIENT_PREFETCH
);
5933 ASSERT(!embedded_bp
|| !BP_IS_HOLE(bp
));
5935 /* Get a buf with the desired data in it. */
5936 rc
= arc_buf_alloc_impl(hdr
, spa
, zb
, private,
5937 encrypted_read
, compressed_read
, noauth_read
,
5941 * Convert authentication and decryption errors
5942 * to EIO (and generate an ereport if needed)
5943 * before leaving the ARC.
5945 rc
= SET_ERROR(EIO
);
5946 if ((zio_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
5947 spa_log_error(spa
, zb
);
5948 (void) zfs_ereport_post(
5949 FM_EREPORT_ZFS_AUTHENTICATION
,
5950 spa
, NULL
, zb
, NULL
, 0);
5954 (void) remove_reference(hdr
, hash_lock
,
5956 arc_buf_destroy_impl(buf
);
5960 /* assert any errors weren't due to unloaded keys */
5961 ASSERT((zio_flags
& ZIO_FLAG_SPECULATIVE
) ||
5963 } else if (*arc_flags
& ARC_FLAG_PREFETCH
&&
5964 zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
5965 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
5967 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
5968 arc_access(hdr
, hash_lock
);
5969 if (*arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
)
5970 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
5971 if (*arc_flags
& ARC_FLAG_L2CACHE
)
5972 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
5973 mutex_exit(hash_lock
);
5974 ARCSTAT_BUMP(arcstat_hits
);
5975 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
5976 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
5977 data
, metadata
, hits
);
5980 done(NULL
, zb
, bp
, buf
, private);
5982 uint64_t lsize
= BP_GET_LSIZE(bp
);
5983 uint64_t psize
= BP_GET_PSIZE(bp
);
5984 arc_callback_t
*acb
;
5987 boolean_t devw
= B_FALSE
;
5990 int alloc_flags
= encrypted_read
? ARC_HDR_ALLOC_RDATA
: 0;
5992 if (*arc_flags
& ARC_FLAG_CACHED_ONLY
) {
5993 rc
= SET_ERROR(ENOENT
);
5994 if (hash_lock
!= NULL
)
5995 mutex_exit(hash_lock
);
6000 * Gracefully handle a damaged logical block size as a
6003 if (lsize
> spa_maxblocksize(spa
)) {
6004 rc
= SET_ERROR(ECKSUM
);
6005 if (hash_lock
!= NULL
)
6006 mutex_exit(hash_lock
);
6012 * This block is not in the cache or it has
6015 arc_buf_hdr_t
*exists
= NULL
;
6016 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
6017 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
6018 BP_IS_PROTECTED(bp
), BP_GET_COMPRESS(bp
), 0, type
,
6022 hdr
->b_dva
= *BP_IDENTITY(bp
);
6023 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
6024 exists
= buf_hash_insert(hdr
, &hash_lock
);
6026 if (exists
!= NULL
) {
6027 /* somebody beat us to the hash insert */
6028 mutex_exit(hash_lock
);
6029 buf_discard_identity(hdr
);
6030 arc_hdr_destroy(hdr
);
6031 goto top
; /* restart the IO request */
6035 * This block is in the ghost cache or encrypted data
6036 * was requested and we didn't have it. If it was
6037 * L2-only (and thus didn't have an L1 hdr),
6038 * we realloc the header to add an L1 hdr.
6040 if (!HDR_HAS_L1HDR(hdr
)) {
6041 hdr
= arc_hdr_realloc(hdr
, hdr_l2only_cache
,
6045 if (GHOST_STATE(hdr
->b_l1hdr
.b_state
)) {
6046 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6047 ASSERT(!HDR_HAS_RABD(hdr
));
6048 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6049 ASSERT0(zfs_refcount_count(
6050 &hdr
->b_l1hdr
.b_refcnt
));
6051 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
6052 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
6053 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
6055 * If this header already had an IO in progress
6056 * and we are performing another IO to fetch
6057 * encrypted data we must wait until the first
6058 * IO completes so as not to confuse
6059 * arc_read_done(). This should be very rare
6060 * and so the performance impact shouldn't
6063 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
6064 mutex_exit(hash_lock
);
6069 * This is a delicate dance that we play here.
6070 * This hdr might be in the ghost list so we access
6071 * it to move it out of the ghost list before we
6072 * initiate the read. If it's a prefetch then
6073 * it won't have a callback so we'll remove the
6074 * reference that arc_buf_alloc_impl() created. We
6075 * do this after we've called arc_access() to
6076 * avoid hitting an assert in remove_reference().
6078 arc_adapt(arc_hdr_size(hdr
), hdr
->b_l1hdr
.b_state
);
6079 arc_access(hdr
, hash_lock
);
6080 arc_hdr_alloc_abd(hdr
, alloc_flags
);
6083 if (encrypted_read
) {
6084 ASSERT(HDR_HAS_RABD(hdr
));
6085 size
= HDR_GET_PSIZE(hdr
);
6086 hdr_abd
= hdr
->b_crypt_hdr
.b_rabd
;
6087 zio_flags
|= ZIO_FLAG_RAW
;
6089 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
6090 size
= arc_hdr_size(hdr
);
6091 hdr_abd
= hdr
->b_l1hdr
.b_pabd
;
6093 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
6094 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
6098 * For authenticated bp's, we do not ask the ZIO layer
6099 * to authenticate them since this will cause the entire
6100 * IO to fail if the key isn't loaded. Instead, we
6101 * defer authentication until arc_buf_fill(), which will
6102 * verify the data when the key is available.
6104 if (BP_IS_AUTHENTICATED(bp
))
6105 zio_flags
|= ZIO_FLAG_RAW_ENCRYPT
;
6108 if (*arc_flags
& ARC_FLAG_PREFETCH
&&
6109 zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
))
6110 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
6111 if (*arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
)
6112 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
6113 if (*arc_flags
& ARC_FLAG_L2CACHE
)
6114 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6115 if (BP_IS_AUTHENTICATED(bp
))
6116 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6117 if (BP_GET_LEVEL(bp
) > 0)
6118 arc_hdr_set_flags(hdr
, ARC_FLAG_INDIRECT
);
6119 if (*arc_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
)
6120 arc_hdr_set_flags(hdr
, ARC_FLAG_PREDICTIVE_PREFETCH
);
6121 ASSERT(!GHOST_STATE(hdr
->b_l1hdr
.b_state
));
6123 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
6124 acb
->acb_done
= done
;
6125 acb
->acb_private
= private;
6126 acb
->acb_compressed
= compressed_read
;
6127 acb
->acb_encrypted
= encrypted_read
;
6128 acb
->acb_noauth
= noauth_read
;
6131 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6132 hdr
->b_l1hdr
.b_acb
= acb
;
6133 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6135 if (HDR_HAS_L2HDR(hdr
) &&
6136 (vd
= hdr
->b_l2hdr
.b_dev
->l2ad_vdev
) != NULL
) {
6137 devw
= hdr
->b_l2hdr
.b_dev
->l2ad_writing
;
6138 addr
= hdr
->b_l2hdr
.b_daddr
;
6140 * Lock out L2ARC device removal.
6142 if (vdev_is_dead(vd
) ||
6143 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
6148 * We count both async reads and scrub IOs as asynchronous so
6149 * that both can be upgraded in the event of a cache hit while
6150 * the read IO is still in-flight.
6152 if (priority
== ZIO_PRIORITY_ASYNC_READ
||
6153 priority
== ZIO_PRIORITY_SCRUB
)
6154 arc_hdr_set_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6156 arc_hdr_clear_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6159 * At this point, we have a level 1 cache miss or a blkptr
6160 * with embedded data. Try again in L2ARC if possible.
6162 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, lsize
);
6165 * Skip ARC stat bump for block pointers with embedded
6166 * data. The data are read from the blkptr itself via
6167 * decode_embedded_bp_compressed().
6170 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
,
6171 blkptr_t
*, bp
, uint64_t, lsize
,
6172 zbookmark_phys_t
*, zb
);
6173 ARCSTAT_BUMP(arcstat_misses
);
6174 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
6175 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
), data
,
6179 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
6181 * Read from the L2ARC if the following are true:
6182 * 1. The L2ARC vdev was previously cached.
6183 * 2. This buffer still has L2ARC metadata.
6184 * 3. This buffer isn't currently writing to the L2ARC.
6185 * 4. The L2ARC entry wasn't evicted, which may
6186 * also have invalidated the vdev.
6187 * 5. This isn't prefetch and l2arc_noprefetch is set.
6189 if (HDR_HAS_L2HDR(hdr
) &&
6190 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
6191 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
6192 l2arc_read_callback_t
*cb
;
6196 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
6197 ARCSTAT_BUMP(arcstat_l2_hits
);
6198 atomic_inc_32(&hdr
->b_l2hdr
.b_hits
);
6200 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
6202 cb
->l2rcb_hdr
= hdr
;
6205 cb
->l2rcb_flags
= zio_flags
;
6208 * When Compressed ARC is disabled, but the
6209 * L2ARC block is compressed, arc_hdr_size()
6210 * will have returned LSIZE rather than PSIZE.
6212 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
6213 !HDR_COMPRESSION_ENABLED(hdr
) &&
6214 HDR_GET_PSIZE(hdr
) != 0) {
6215 size
= HDR_GET_PSIZE(hdr
);
6218 asize
= vdev_psize_to_asize(vd
, size
);
6219 if (asize
!= size
) {
6220 abd
= abd_alloc_for_io(asize
,
6221 HDR_ISTYPE_METADATA(hdr
));
6222 cb
->l2rcb_abd
= abd
;
6227 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
6228 addr
+ asize
<= vd
->vdev_psize
-
6229 VDEV_LABEL_END_SIZE
);
6232 * l2arc read. The SCL_L2ARC lock will be
6233 * released by l2arc_read_done().
6234 * Issue a null zio if the underlying buffer
6235 * was squashed to zero size by compression.
6237 ASSERT3U(arc_hdr_get_compress(hdr
), !=,
6238 ZIO_COMPRESS_EMPTY
);
6239 rzio
= zio_read_phys(pio
, vd
, addr
,
6242 l2arc_read_done
, cb
, priority
,
6243 zio_flags
| ZIO_FLAG_DONT_CACHE
|
6245 ZIO_FLAG_DONT_PROPAGATE
|
6246 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
6247 acb
->acb_zio_head
= rzio
;
6249 if (hash_lock
!= NULL
)
6250 mutex_exit(hash_lock
);
6252 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
6254 ARCSTAT_INCR(arcstat_l2_read_bytes
,
6255 HDR_GET_PSIZE(hdr
));
6257 if (*arc_flags
& ARC_FLAG_NOWAIT
) {
6262 ASSERT(*arc_flags
& ARC_FLAG_WAIT
);
6263 if (zio_wait(rzio
) == 0)
6266 /* l2arc read error; goto zio_read() */
6267 if (hash_lock
!= NULL
)
6268 mutex_enter(hash_lock
);
6270 DTRACE_PROBE1(l2arc__miss
,
6271 arc_buf_hdr_t
*, hdr
);
6272 ARCSTAT_BUMP(arcstat_l2_misses
);
6273 if (HDR_L2_WRITING(hdr
))
6274 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
6275 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6279 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6281 * Skip ARC stat bump for block pointers with
6282 * embedded data. The data are read from the blkptr
6283 * itself via decode_embedded_bp_compressed().
6285 if (l2arc_ndev
!= 0 && !embedded_bp
) {
6286 DTRACE_PROBE1(l2arc__miss
,
6287 arc_buf_hdr_t
*, hdr
);
6288 ARCSTAT_BUMP(arcstat_l2_misses
);
6292 rzio
= zio_read(pio
, spa
, bp
, hdr_abd
, size
,
6293 arc_read_done
, hdr
, priority
, zio_flags
, zb
);
6294 acb
->acb_zio_head
= rzio
;
6296 if (hash_lock
!= NULL
)
6297 mutex_exit(hash_lock
);
6299 if (*arc_flags
& ARC_FLAG_WAIT
) {
6300 rc
= zio_wait(rzio
);
6304 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
6309 /* embedded bps don't actually go to disk */
6311 spa_read_history_add(spa
, zb
, *arc_flags
);
6312 spl_fstrans_unmark(cookie
);
6317 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
6321 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
6323 p
->p_private
= private;
6324 list_link_init(&p
->p_node
);
6325 zfs_refcount_create(&p
->p_refcnt
);
6327 mutex_enter(&arc_prune_mtx
);
6328 zfs_refcount_add(&p
->p_refcnt
, &arc_prune_list
);
6329 list_insert_head(&arc_prune_list
, p
);
6330 mutex_exit(&arc_prune_mtx
);
6336 arc_remove_prune_callback(arc_prune_t
*p
)
6338 boolean_t wait
= B_FALSE
;
6339 mutex_enter(&arc_prune_mtx
);
6340 list_remove(&arc_prune_list
, p
);
6341 if (zfs_refcount_remove(&p
->p_refcnt
, &arc_prune_list
) > 0)
6343 mutex_exit(&arc_prune_mtx
);
6345 /* wait for arc_prune_task to finish */
6347 taskq_wait_outstanding(arc_prune_taskq
, 0);
6348 ASSERT0(zfs_refcount_count(&p
->p_refcnt
));
6349 zfs_refcount_destroy(&p
->p_refcnt
);
6350 kmem_free(p
, sizeof (*p
));
6354 * Notify the arc that a block was freed, and thus will never be used again.
6357 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
6360 kmutex_t
*hash_lock
;
6361 uint64_t guid
= spa_load_guid(spa
);
6363 ASSERT(!BP_IS_EMBEDDED(bp
));
6365 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
6370 * We might be trying to free a block that is still doing I/O
6371 * (i.e. prefetch) or has a reference (i.e. a dedup-ed,
6372 * dmu_sync-ed block). If this block is being prefetched, then it
6373 * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr
6374 * until the I/O completes. A block may also have a reference if it is
6375 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would
6376 * have written the new block to its final resting place on disk but
6377 * without the dedup flag set. This would have left the hdr in the MRU
6378 * state and discoverable. When the txg finally syncs it detects that
6379 * the block was overridden in open context and issues an override I/O.
6380 * Since this is a dedup block, the override I/O will determine if the
6381 * block is already in the DDT. If so, then it will replace the io_bp
6382 * with the bp from the DDT and allow the I/O to finish. When the I/O
6383 * reaches the done callback, dbuf_write_override_done, it will
6384 * check to see if the io_bp and io_bp_override are identical.
6385 * If they are not, then it indicates that the bp was replaced with
6386 * the bp in the DDT and the override bp is freed. This allows
6387 * us to arrive here with a reference on a block that is being
6388 * freed. So if we have an I/O in progress, or a reference to
6389 * this hdr, then we don't destroy the hdr.
6391 if (!HDR_HAS_L1HDR(hdr
) || (!HDR_IO_IN_PROGRESS(hdr
) &&
6392 zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
))) {
6393 arc_change_state(arc_anon
, hdr
, hash_lock
);
6394 arc_hdr_destroy(hdr
);
6395 mutex_exit(hash_lock
);
6397 mutex_exit(hash_lock
);
6403 * Release this buffer from the cache, making it an anonymous buffer. This
6404 * must be done after a read and prior to modifying the buffer contents.
6405 * If the buffer has more than one reference, we must make
6406 * a new hdr for the buffer.
6409 arc_release(arc_buf_t
*buf
, void *tag
)
6411 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6414 * It would be nice to assert that if its DMU metadata (level >
6415 * 0 || it's the dnode file), then it must be syncing context.
6416 * But we don't know that information at this level.
6419 mutex_enter(&buf
->b_evict_lock
);
6421 ASSERT(HDR_HAS_L1HDR(hdr
));
6424 * We don't grab the hash lock prior to this check, because if
6425 * the buffer's header is in the arc_anon state, it won't be
6426 * linked into the hash table.
6428 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
6429 mutex_exit(&buf
->b_evict_lock
);
6430 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6431 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
6432 ASSERT(!HDR_HAS_L2HDR(hdr
));
6433 ASSERT(HDR_EMPTY(hdr
));
6435 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
6436 ASSERT3S(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
), ==, 1);
6437 ASSERT(!list_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6439 hdr
->b_l1hdr
.b_arc_access
= 0;
6442 * If the buf is being overridden then it may already
6443 * have a hdr that is not empty.
6445 buf_discard_identity(hdr
);
6451 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
6452 mutex_enter(hash_lock
);
6455 * This assignment is only valid as long as the hash_lock is
6456 * held, we must be careful not to reference state or the
6457 * b_state field after dropping the lock.
6459 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
6460 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
6461 ASSERT3P(state
, !=, arc_anon
);
6463 /* this buffer is not on any list */
6464 ASSERT3S(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
), >, 0);
6466 if (HDR_HAS_L2HDR(hdr
)) {
6467 mutex_enter(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6470 * We have to recheck this conditional again now that
6471 * we're holding the l2ad_mtx to prevent a race with
6472 * another thread which might be concurrently calling
6473 * l2arc_evict(). In that case, l2arc_evict() might have
6474 * destroyed the header's L2 portion as we were waiting
6475 * to acquire the l2ad_mtx.
6477 if (HDR_HAS_L2HDR(hdr
))
6478 arc_hdr_l2hdr_destroy(hdr
);
6480 mutex_exit(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6484 * Do we have more than one buf?
6486 if (hdr
->b_l1hdr
.b_bufcnt
> 1) {
6487 arc_buf_hdr_t
*nhdr
;
6488 uint64_t spa
= hdr
->b_spa
;
6489 uint64_t psize
= HDR_GET_PSIZE(hdr
);
6490 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
6491 boolean_t
protected = HDR_PROTECTED(hdr
);
6492 enum zio_compress compress
= arc_hdr_get_compress(hdr
);
6493 arc_buf_contents_t type
= arc_buf_type(hdr
);
6494 VERIFY3U(hdr
->b_type
, ==, type
);
6496 ASSERT(hdr
->b_l1hdr
.b_buf
!= buf
|| buf
->b_next
!= NULL
);
6497 (void) remove_reference(hdr
, hash_lock
, tag
);
6499 if (arc_buf_is_shared(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
6500 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6501 ASSERT(ARC_BUF_LAST(buf
));
6505 * Pull the data off of this hdr and attach it to
6506 * a new anonymous hdr. Also find the last buffer
6507 * in the hdr's buffer list.
6509 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
6510 ASSERT3P(lastbuf
, !=, NULL
);
6513 * If the current arc_buf_t and the hdr are sharing their data
6514 * buffer, then we must stop sharing that block.
6516 if (arc_buf_is_shared(buf
)) {
6517 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6518 VERIFY(!arc_buf_is_shared(lastbuf
));
6521 * First, sever the block sharing relationship between
6522 * buf and the arc_buf_hdr_t.
6524 arc_unshare_buf(hdr
, buf
);
6527 * Now we need to recreate the hdr's b_pabd. Since we
6528 * have lastbuf handy, we try to share with it, but if
6529 * we can't then we allocate a new b_pabd and copy the
6530 * data from buf into it.
6532 if (arc_can_share(hdr
, lastbuf
)) {
6533 arc_share_buf(hdr
, lastbuf
);
6535 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
);
6536 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
,
6537 buf
->b_data
, psize
);
6539 VERIFY3P(lastbuf
->b_data
, !=, NULL
);
6540 } else if (HDR_SHARED_DATA(hdr
)) {
6542 * Uncompressed shared buffers are always at the end
6543 * of the list. Compressed buffers don't have the
6544 * same requirements. This makes it hard to
6545 * simply assert that the lastbuf is shared so
6546 * we rely on the hdr's compression flags to determine
6547 * if we have a compressed, shared buffer.
6549 ASSERT(arc_buf_is_shared(lastbuf
) ||
6550 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
6551 ASSERT(!ARC_BUF_SHARED(buf
));
6554 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
6555 ASSERT3P(state
, !=, arc_l2c_only
);
6557 (void) zfs_refcount_remove_many(&state
->arcs_size
,
6558 arc_buf_size(buf
), buf
);
6560 if (zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
6561 ASSERT3P(state
, !=, arc_l2c_only
);
6562 (void) zfs_refcount_remove_many(
6563 &state
->arcs_esize
[type
],
6564 arc_buf_size(buf
), buf
);
6567 hdr
->b_l1hdr
.b_bufcnt
-= 1;
6568 if (ARC_BUF_ENCRYPTED(buf
))
6569 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
6571 arc_cksum_verify(buf
);
6572 arc_buf_unwatch(buf
);
6574 /* if this is the last uncompressed buf free the checksum */
6575 if (!arc_hdr_has_uncompressed_buf(hdr
))
6576 arc_cksum_free(hdr
);
6578 mutex_exit(hash_lock
);
6581 * Allocate a new hdr. The new hdr will contain a b_pabd
6582 * buffer which will be freed in arc_write().
6584 nhdr
= arc_hdr_alloc(spa
, psize
, lsize
, protected,
6585 compress
, hdr
->b_complevel
, type
, HDR_HAS_RABD(hdr
));
6586 ASSERT3P(nhdr
->b_l1hdr
.b_buf
, ==, NULL
);
6587 ASSERT0(nhdr
->b_l1hdr
.b_bufcnt
);
6588 ASSERT0(zfs_refcount_count(&nhdr
->b_l1hdr
.b_refcnt
));
6589 VERIFY3U(nhdr
->b_type
, ==, type
);
6590 ASSERT(!HDR_SHARED_DATA(nhdr
));
6592 nhdr
->b_l1hdr
.b_buf
= buf
;
6593 nhdr
->b_l1hdr
.b_bufcnt
= 1;
6594 if (ARC_BUF_ENCRYPTED(buf
))
6595 nhdr
->b_crypt_hdr
.b_ebufcnt
= 1;
6596 nhdr
->b_l1hdr
.b_mru_hits
= 0;
6597 nhdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
6598 nhdr
->b_l1hdr
.b_mfu_hits
= 0;
6599 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
6600 nhdr
->b_l1hdr
.b_l2_hits
= 0;
6601 (void) zfs_refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, tag
);
6604 mutex_exit(&buf
->b_evict_lock
);
6605 (void) zfs_refcount_add_many(&arc_anon
->arcs_size
,
6606 arc_buf_size(buf
), buf
);
6608 mutex_exit(&buf
->b_evict_lock
);
6609 ASSERT(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 1);
6610 /* protected by hash lock, or hdr is on arc_anon */
6611 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6612 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6613 hdr
->b_l1hdr
.b_mru_hits
= 0;
6614 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
6615 hdr
->b_l1hdr
.b_mfu_hits
= 0;
6616 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
6617 hdr
->b_l1hdr
.b_l2_hits
= 0;
6618 arc_change_state(arc_anon
, hdr
, hash_lock
);
6619 hdr
->b_l1hdr
.b_arc_access
= 0;
6621 mutex_exit(hash_lock
);
6622 buf_discard_identity(hdr
);
6628 arc_released(arc_buf_t
*buf
)
6632 mutex_enter(&buf
->b_evict_lock
);
6633 released
= (buf
->b_data
!= NULL
&&
6634 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
6635 mutex_exit(&buf
->b_evict_lock
);
6641 arc_referenced(arc_buf_t
*buf
)
6645 mutex_enter(&buf
->b_evict_lock
);
6646 referenced
= (zfs_refcount_count(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6647 mutex_exit(&buf
->b_evict_lock
);
6648 return (referenced
);
6653 arc_write_ready(zio_t
*zio
)
6655 arc_write_callback_t
*callback
= zio
->io_private
;
6656 arc_buf_t
*buf
= callback
->awcb_buf
;
6657 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6658 blkptr_t
*bp
= zio
->io_bp
;
6659 uint64_t psize
= BP_IS_HOLE(bp
) ? 0 : BP_GET_PSIZE(bp
);
6660 fstrans_cookie_t cookie
= spl_fstrans_mark();
6662 ASSERT(HDR_HAS_L1HDR(hdr
));
6663 ASSERT(!zfs_refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6664 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
6667 * If we're reexecuting this zio because the pool suspended, then
6668 * cleanup any state that was previously set the first time the
6669 * callback was invoked.
6671 if (zio
->io_flags
& ZIO_FLAG_REEXECUTED
) {
6672 arc_cksum_free(hdr
);
6673 arc_buf_unwatch(buf
);
6674 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
6675 if (arc_buf_is_shared(buf
)) {
6676 arc_unshare_buf(hdr
, buf
);
6678 arc_hdr_free_abd(hdr
, B_FALSE
);
6682 if (HDR_HAS_RABD(hdr
))
6683 arc_hdr_free_abd(hdr
, B_TRUE
);
6685 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6686 ASSERT(!HDR_HAS_RABD(hdr
));
6687 ASSERT(!HDR_SHARED_DATA(hdr
));
6688 ASSERT(!arc_buf_is_shared(buf
));
6690 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
6692 if (HDR_IO_IN_PROGRESS(hdr
))
6693 ASSERT(zio
->io_flags
& ZIO_FLAG_REEXECUTED
);
6695 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6697 if (BP_IS_PROTECTED(bp
) != !!HDR_PROTECTED(hdr
))
6698 hdr
= arc_hdr_realloc_crypt(hdr
, BP_IS_PROTECTED(bp
));
6700 if (BP_IS_PROTECTED(bp
)) {
6701 /* ZIL blocks are written through zio_rewrite */
6702 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
6703 ASSERT(HDR_PROTECTED(hdr
));
6705 if (BP_SHOULD_BYTESWAP(bp
)) {
6706 if (BP_GET_LEVEL(bp
) > 0) {
6707 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
6709 hdr
->b_l1hdr
.b_byteswap
=
6710 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
6713 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
6716 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
6717 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
6718 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
6719 hdr
->b_crypt_hdr
.b_iv
);
6720 zio_crypt_decode_mac_bp(bp
, hdr
->b_crypt_hdr
.b_mac
);
6724 * If this block was written for raw encryption but the zio layer
6725 * ended up only authenticating it, adjust the buffer flags now.
6727 if (BP_IS_AUTHENTICATED(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
6728 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6729 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
6730 if (BP_GET_COMPRESS(bp
) == ZIO_COMPRESS_OFF
)
6731 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
6732 } else if (BP_IS_HOLE(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
6733 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
6734 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
6737 /* this must be done after the buffer flags are adjusted */
6738 arc_cksum_compute(buf
);
6740 enum zio_compress compress
;
6741 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
6742 compress
= ZIO_COMPRESS_OFF
;
6744 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
6745 compress
= BP_GET_COMPRESS(bp
);
6747 HDR_SET_PSIZE(hdr
, psize
);
6748 arc_hdr_set_compress(hdr
, compress
);
6749 hdr
->b_complevel
= zio
->io_prop
.zp_complevel
;
6751 if (zio
->io_error
!= 0 || psize
== 0)
6755 * Fill the hdr with data. If the buffer is encrypted we have no choice
6756 * but to copy the data into b_radb. If the hdr is compressed, the data
6757 * we want is available from the zio, otherwise we can take it from
6760 * We might be able to share the buf's data with the hdr here. However,
6761 * doing so would cause the ARC to be full of linear ABDs if we write a
6762 * lot of shareable data. As a compromise, we check whether scattered
6763 * ABDs are allowed, and assume that if they are then the user wants
6764 * the ARC to be primarily filled with them regardless of the data being
6765 * written. Therefore, if they're allowed then we allocate one and copy
6766 * the data into it; otherwise, we share the data directly if we can.
6768 if (ARC_BUF_ENCRYPTED(buf
)) {
6769 ASSERT3U(psize
, >, 0);
6770 ASSERT(ARC_BUF_COMPRESSED(buf
));
6771 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
|ARC_HDR_ALLOC_RDATA
);
6772 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
6773 } else if (zfs_abd_scatter_enabled
|| !arc_can_share(hdr
, buf
)) {
6775 * Ideally, we would always copy the io_abd into b_pabd, but the
6776 * user may have disabled compressed ARC, thus we must check the
6777 * hdr's compression setting rather than the io_bp's.
6779 if (BP_IS_ENCRYPTED(bp
)) {
6780 ASSERT3U(psize
, >, 0);
6781 arc_hdr_alloc_abd(hdr
,
6782 ARC_HDR_DO_ADAPT
|ARC_HDR_ALLOC_RDATA
);
6783 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
6784 } else if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
6785 !ARC_BUF_COMPRESSED(buf
)) {
6786 ASSERT3U(psize
, >, 0);
6787 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
);
6788 abd_copy(hdr
->b_l1hdr
.b_pabd
, zio
->io_abd
, psize
);
6790 ASSERT3U(zio
->io_orig_size
, ==, arc_hdr_size(hdr
));
6791 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
);
6792 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
6796 ASSERT3P(buf
->b_data
, ==, abd_to_buf(zio
->io_orig_abd
));
6797 ASSERT3U(zio
->io_orig_size
, ==, arc_buf_size(buf
));
6798 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
6800 arc_share_buf(hdr
, buf
);
6804 arc_hdr_verify(hdr
, bp
);
6805 spl_fstrans_unmark(cookie
);
6809 arc_write_children_ready(zio_t
*zio
)
6811 arc_write_callback_t
*callback
= zio
->io_private
;
6812 arc_buf_t
*buf
= callback
->awcb_buf
;
6814 callback
->awcb_children_ready(zio
, buf
, callback
->awcb_private
);
6818 * The SPA calls this callback for each physical write that happens on behalf
6819 * of a logical write. See the comment in dbuf_write_physdone() for details.
6822 arc_write_physdone(zio_t
*zio
)
6824 arc_write_callback_t
*cb
= zio
->io_private
;
6825 if (cb
->awcb_physdone
!= NULL
)
6826 cb
->awcb_physdone(zio
, cb
->awcb_buf
, cb
->awcb_private
);
6830 arc_write_done(zio_t
*zio
)
6832 arc_write_callback_t
*callback
= zio
->io_private
;
6833 arc_buf_t
*buf
= callback
->awcb_buf
;
6834 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6836 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6838 if (zio
->io_error
== 0) {
6839 arc_hdr_verify(hdr
, zio
->io_bp
);
6841 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
6842 buf_discard_identity(hdr
);
6844 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
6845 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
6848 ASSERT(HDR_EMPTY(hdr
));
6852 * If the block to be written was all-zero or compressed enough to be
6853 * embedded in the BP, no write was performed so there will be no
6854 * dva/birth/checksum. The buffer must therefore remain anonymous
6857 if (!HDR_EMPTY(hdr
)) {
6858 arc_buf_hdr_t
*exists
;
6859 kmutex_t
*hash_lock
;
6861 ASSERT3U(zio
->io_error
, ==, 0);
6863 arc_cksum_verify(buf
);
6865 exists
= buf_hash_insert(hdr
, &hash_lock
);
6866 if (exists
!= NULL
) {
6868 * This can only happen if we overwrite for
6869 * sync-to-convergence, because we remove
6870 * buffers from the hash table when we arc_free().
6872 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
6873 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
6874 panic("bad overwrite, hdr=%p exists=%p",
6875 (void *)hdr
, (void *)exists
);
6876 ASSERT(zfs_refcount_is_zero(
6877 &exists
->b_l1hdr
.b_refcnt
));
6878 arc_change_state(arc_anon
, exists
, hash_lock
);
6879 arc_hdr_destroy(exists
);
6880 mutex_exit(hash_lock
);
6881 exists
= buf_hash_insert(hdr
, &hash_lock
);
6882 ASSERT3P(exists
, ==, NULL
);
6883 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
6885 ASSERT(zio
->io_prop
.zp_nopwrite
);
6886 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
6887 panic("bad nopwrite, hdr=%p exists=%p",
6888 (void *)hdr
, (void *)exists
);
6891 ASSERT(hdr
->b_l1hdr
.b_bufcnt
== 1);
6892 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
6893 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
6894 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
6897 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6898 /* if it's not anon, we are doing a scrub */
6899 if (exists
== NULL
&& hdr
->b_l1hdr
.b_state
== arc_anon
)
6900 arc_access(hdr
, hash_lock
);
6901 mutex_exit(hash_lock
);
6903 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6906 ASSERT(!zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
6907 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
6909 abd_put(zio
->io_abd
);
6910 kmem_free(callback
, sizeof (arc_write_callback_t
));
6914 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
6915 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
,
6916 const zio_prop_t
*zp
, arc_write_done_func_t
*ready
,
6917 arc_write_done_func_t
*children_ready
, arc_write_done_func_t
*physdone
,
6918 arc_write_done_func_t
*done
, void *private, zio_priority_t priority
,
6919 int zio_flags
, const zbookmark_phys_t
*zb
)
6921 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6922 arc_write_callback_t
*callback
;
6924 zio_prop_t localprop
= *zp
;
6926 ASSERT3P(ready
, !=, NULL
);
6927 ASSERT3P(done
, !=, NULL
);
6928 ASSERT(!HDR_IO_ERROR(hdr
));
6929 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6930 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6931 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
6933 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6935 if (ARC_BUF_ENCRYPTED(buf
)) {
6936 ASSERT(ARC_BUF_COMPRESSED(buf
));
6937 localprop
.zp_encrypt
= B_TRUE
;
6938 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
6939 localprop
.zp_complevel
= hdr
->b_complevel
;
6940 localprop
.zp_byteorder
=
6941 (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
6942 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
6943 bcopy(hdr
->b_crypt_hdr
.b_salt
, localprop
.zp_salt
,
6945 bcopy(hdr
->b_crypt_hdr
.b_iv
, localprop
.zp_iv
,
6947 bcopy(hdr
->b_crypt_hdr
.b_mac
, localprop
.zp_mac
,
6949 if (DMU_OT_IS_ENCRYPTED(localprop
.zp_type
)) {
6950 localprop
.zp_nopwrite
= B_FALSE
;
6951 localprop
.zp_copies
=
6952 MIN(localprop
.zp_copies
, SPA_DVAS_PER_BP
- 1);
6954 zio_flags
|= ZIO_FLAG_RAW
;
6955 } else if (ARC_BUF_COMPRESSED(buf
)) {
6956 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, arc_buf_size(buf
));
6957 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
6958 localprop
.zp_complevel
= hdr
->b_complevel
;
6959 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
6961 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
6962 callback
->awcb_ready
= ready
;
6963 callback
->awcb_children_ready
= children_ready
;
6964 callback
->awcb_physdone
= physdone
;
6965 callback
->awcb_done
= done
;
6966 callback
->awcb_private
= private;
6967 callback
->awcb_buf
= buf
;
6970 * The hdr's b_pabd is now stale, free it now. A new data block
6971 * will be allocated when the zio pipeline calls arc_write_ready().
6973 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
6975 * If the buf is currently sharing the data block with
6976 * the hdr then we need to break that relationship here.
6977 * The hdr will remain with a NULL data pointer and the
6978 * buf will take sole ownership of the block.
6980 if (arc_buf_is_shared(buf
)) {
6981 arc_unshare_buf(hdr
, buf
);
6983 arc_hdr_free_abd(hdr
, B_FALSE
);
6985 VERIFY3P(buf
->b_data
, !=, NULL
);
6988 if (HDR_HAS_RABD(hdr
))
6989 arc_hdr_free_abd(hdr
, B_TRUE
);
6991 if (!(zio_flags
& ZIO_FLAG_RAW
))
6992 arc_hdr_set_compress(hdr
, ZIO_COMPRESS_OFF
);
6994 ASSERT(!arc_buf_is_shared(buf
));
6995 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6997 zio
= zio_write(pio
, spa
, txg
, bp
,
6998 abd_get_from_buf(buf
->b_data
, HDR_GET_LSIZE(hdr
)),
6999 HDR_GET_LSIZE(hdr
), arc_buf_size(buf
), &localprop
, arc_write_ready
,
7000 (children_ready
!= NULL
) ? arc_write_children_ready
: NULL
,
7001 arc_write_physdone
, arc_write_done
, callback
,
7002 priority
, zio_flags
, zb
);
7008 arc_tempreserve_clear(uint64_t reserve
)
7010 atomic_add_64(&arc_tempreserve
, -reserve
);
7011 ASSERT((int64_t)arc_tempreserve
>= 0);
7015 arc_tempreserve_space(spa_t
*spa
, uint64_t reserve
, uint64_t txg
)
7021 reserve
> arc_c
/4 &&
7022 reserve
* 4 > (2ULL << SPA_MAXBLOCKSHIFT
))
7023 arc_c
= MIN(arc_c_max
, reserve
* 4);
7026 * Throttle when the calculated memory footprint for the TXG
7027 * exceeds the target ARC size.
7029 if (reserve
> arc_c
) {
7030 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
7031 return (SET_ERROR(ERESTART
));
7035 * Don't count loaned bufs as in flight dirty data to prevent long
7036 * network delays from blocking transactions that are ready to be
7037 * assigned to a txg.
7040 /* assert that it has not wrapped around */
7041 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
7043 anon_size
= MAX((int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) -
7044 arc_loaned_bytes
), 0);
7047 * Writes will, almost always, require additional memory allocations
7048 * in order to compress/encrypt/etc the data. We therefore need to
7049 * make sure that there is sufficient available memory for this.
7051 error
= arc_memory_throttle(spa
, reserve
, txg
);
7056 * Throttle writes when the amount of dirty data in the cache
7057 * gets too large. We try to keep the cache less than half full
7058 * of dirty blocks so that our sync times don't grow too large.
7060 * In the case of one pool being built on another pool, we want
7061 * to make sure we don't end up throttling the lower (backing)
7062 * pool when the upper pool is the majority contributor to dirty
7063 * data. To insure we make forward progress during throttling, we
7064 * also check the current pool's net dirty data and only throttle
7065 * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty
7066 * data in the cache.
7068 * Note: if two requests come in concurrently, we might let them
7069 * both succeed, when one of them should fail. Not a huge deal.
7071 uint64_t total_dirty
= reserve
+ arc_tempreserve
+ anon_size
;
7072 uint64_t spa_dirty_anon
= spa_dirty_data(spa
);
7073 uint64_t rarc_c
= arc_warm
? arc_c
: arc_c_max
;
7074 if (total_dirty
> rarc_c
* zfs_arc_dirty_limit_percent
/ 100 &&
7075 anon_size
> rarc_c
* zfs_arc_anon_limit_percent
/ 100 &&
7076 spa_dirty_anon
> anon_size
* zfs_arc_pool_dirty_percent
/ 100) {
7078 uint64_t meta_esize
= zfs_refcount_count(
7079 &arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7080 uint64_t data_esize
=
7081 zfs_refcount_count(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7082 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
7083 "anon_data=%lluK tempreserve=%lluK rarc_c=%lluK\n",
7084 arc_tempreserve
>> 10, meta_esize
>> 10,
7085 data_esize
>> 10, reserve
>> 10, rarc_c
>> 10);
7087 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
7088 return (SET_ERROR(ERESTART
));
7090 atomic_add_64(&arc_tempreserve
, reserve
);
7095 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
7096 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
7098 size
->value
.ui64
= zfs_refcount_count(&state
->arcs_size
);
7099 evict_data
->value
.ui64
=
7100 zfs_refcount_count(&state
->arcs_esize
[ARC_BUFC_DATA
]);
7101 evict_metadata
->value
.ui64
=
7102 zfs_refcount_count(&state
->arcs_esize
[ARC_BUFC_METADATA
]);
7106 arc_kstat_update(kstat_t
*ksp
, int rw
)
7108 arc_stats_t
*as
= ksp
->ks_data
;
7110 if (rw
== KSTAT_WRITE
) {
7111 return (SET_ERROR(EACCES
));
7113 arc_kstat_update_state(arc_anon
,
7114 &as
->arcstat_anon_size
,
7115 &as
->arcstat_anon_evictable_data
,
7116 &as
->arcstat_anon_evictable_metadata
);
7117 arc_kstat_update_state(arc_mru
,
7118 &as
->arcstat_mru_size
,
7119 &as
->arcstat_mru_evictable_data
,
7120 &as
->arcstat_mru_evictable_metadata
);
7121 arc_kstat_update_state(arc_mru_ghost
,
7122 &as
->arcstat_mru_ghost_size
,
7123 &as
->arcstat_mru_ghost_evictable_data
,
7124 &as
->arcstat_mru_ghost_evictable_metadata
);
7125 arc_kstat_update_state(arc_mfu
,
7126 &as
->arcstat_mfu_size
,
7127 &as
->arcstat_mfu_evictable_data
,
7128 &as
->arcstat_mfu_evictable_metadata
);
7129 arc_kstat_update_state(arc_mfu_ghost
,
7130 &as
->arcstat_mfu_ghost_size
,
7131 &as
->arcstat_mfu_ghost_evictable_data
,
7132 &as
->arcstat_mfu_ghost_evictable_metadata
);
7134 ARCSTAT(arcstat_size
) = aggsum_value(&arc_size
);
7135 ARCSTAT(arcstat_meta_used
) = aggsum_value(&arc_meta_used
);
7136 ARCSTAT(arcstat_data_size
) = aggsum_value(&astat_data_size
);
7137 ARCSTAT(arcstat_metadata_size
) =
7138 aggsum_value(&astat_metadata_size
);
7139 ARCSTAT(arcstat_hdr_size
) = aggsum_value(&astat_hdr_size
);
7140 ARCSTAT(arcstat_l2_hdr_size
) = aggsum_value(&astat_l2_hdr_size
);
7141 ARCSTAT(arcstat_dbuf_size
) = aggsum_value(&astat_dbuf_size
);
7142 #if defined(COMPAT_FREEBSD11)
7143 ARCSTAT(arcstat_other_size
) = aggsum_value(&astat_bonus_size
) +
7144 aggsum_value(&astat_dnode_size
) +
7145 aggsum_value(&astat_dbuf_size
);
7147 ARCSTAT(arcstat_dnode_size
) = aggsum_value(&astat_dnode_size
);
7148 ARCSTAT(arcstat_bonus_size
) = aggsum_value(&astat_bonus_size
);
7149 ARCSTAT(arcstat_abd_chunk_waste_size
) =
7150 aggsum_value(&astat_abd_chunk_waste_size
);
7152 as
->arcstat_memory_all_bytes
.value
.ui64
=
7154 as
->arcstat_memory_free_bytes
.value
.ui64
=
7156 as
->arcstat_memory_available_bytes
.value
.i64
=
7157 arc_available_memory();
7164 * This function *must* return indices evenly distributed between all
7165 * sublists of the multilist. This is needed due to how the ARC eviction
7166 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
7167 * distributed between all sublists and uses this assumption when
7168 * deciding which sublist to evict from and how much to evict from it.
7171 arc_state_multilist_index_func(multilist_t
*ml
, void *obj
)
7173 arc_buf_hdr_t
*hdr
= obj
;
7176 * We rely on b_dva to generate evenly distributed index
7177 * numbers using buf_hash below. So, as an added precaution,
7178 * let's make sure we never add empty buffers to the arc lists.
7180 ASSERT(!HDR_EMPTY(hdr
));
7183 * The assumption here, is the hash value for a given
7184 * arc_buf_hdr_t will remain constant throughout its lifetime
7185 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
7186 * Thus, we don't need to store the header's sublist index
7187 * on insertion, as this index can be recalculated on removal.
7189 * Also, the low order bits of the hash value are thought to be
7190 * distributed evenly. Otherwise, in the case that the multilist
7191 * has a power of two number of sublists, each sublists' usage
7192 * would not be evenly distributed.
7194 return (buf_hash(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
) %
7195 multilist_get_num_sublists(ml
));
7198 #define WARN_IF_TUNING_IGNORED(tuning, value, do_warn) do { \
7199 if ((do_warn) && (tuning) && ((tuning) != (value))) { \
7201 "ignoring tunable %s (using %llu instead)", \
7202 (#tuning), (value)); \
7207 * Called during module initialization and periodically thereafter to
7208 * apply reasonable changes to the exposed performance tunings. Can also be
7209 * called explicitly by param_set_arc_*() functions when ARC tunables are
7210 * updated manually. Non-zero zfs_* values which differ from the currently set
7211 * values will be applied.
7214 arc_tuning_update(boolean_t verbose
)
7216 uint64_t allmem
= arc_all_memory();
7217 unsigned long limit
;
7219 /* Valid range: 32M - <arc_c_max> */
7220 if ((zfs_arc_min
) && (zfs_arc_min
!= arc_c_min
) &&
7221 (zfs_arc_min
>= 2ULL << SPA_MAXBLOCKSHIFT
) &&
7222 (zfs_arc_min
<= arc_c_max
)) {
7223 arc_c_min
= zfs_arc_min
;
7224 arc_c
= MAX(arc_c
, arc_c_min
);
7226 WARN_IF_TUNING_IGNORED(zfs_arc_min
, arc_c_min
, verbose
);
7228 /* Valid range: 64M - <all physical memory> */
7229 if ((zfs_arc_max
) && (zfs_arc_max
!= arc_c_max
) &&
7230 (zfs_arc_max
>= 64 << 20) && (zfs_arc_max
< allmem
) &&
7231 (zfs_arc_max
> arc_c_min
)) {
7232 arc_c_max
= zfs_arc_max
;
7233 arc_c
= MIN(arc_c
, arc_c_max
);
7234 arc_p
= (arc_c
>> 1);
7235 if (arc_meta_limit
> arc_c_max
)
7236 arc_meta_limit
= arc_c_max
;
7237 if (arc_dnode_size_limit
> arc_meta_limit
)
7238 arc_dnode_size_limit
= arc_meta_limit
;
7240 WARN_IF_TUNING_IGNORED(zfs_arc_max
, arc_c_max
, verbose
);
7242 /* Valid range: 16M - <arc_c_max> */
7243 if ((zfs_arc_meta_min
) && (zfs_arc_meta_min
!= arc_meta_min
) &&
7244 (zfs_arc_meta_min
>= 1ULL << SPA_MAXBLOCKSHIFT
) &&
7245 (zfs_arc_meta_min
<= arc_c_max
)) {
7246 arc_meta_min
= zfs_arc_meta_min
;
7247 if (arc_meta_limit
< arc_meta_min
)
7248 arc_meta_limit
= arc_meta_min
;
7249 if (arc_dnode_size_limit
< arc_meta_min
)
7250 arc_dnode_size_limit
= arc_meta_min
;
7252 WARN_IF_TUNING_IGNORED(zfs_arc_meta_min
, arc_meta_min
, verbose
);
7254 /* Valid range: <arc_meta_min> - <arc_c_max> */
7255 limit
= zfs_arc_meta_limit
? zfs_arc_meta_limit
:
7256 MIN(zfs_arc_meta_limit_percent
, 100) * arc_c_max
/ 100;
7257 if ((limit
!= arc_meta_limit
) &&
7258 (limit
>= arc_meta_min
) &&
7259 (limit
<= arc_c_max
))
7260 arc_meta_limit
= limit
;
7261 WARN_IF_TUNING_IGNORED(zfs_arc_meta_limit
, arc_meta_limit
, verbose
);
7263 /* Valid range: <arc_meta_min> - <arc_meta_limit> */
7264 limit
= zfs_arc_dnode_limit
? zfs_arc_dnode_limit
:
7265 MIN(zfs_arc_dnode_limit_percent
, 100) * arc_meta_limit
/ 100;
7266 if ((limit
!= arc_dnode_size_limit
) &&
7267 (limit
>= arc_meta_min
) &&
7268 (limit
<= arc_meta_limit
))
7269 arc_dnode_size_limit
= limit
;
7270 WARN_IF_TUNING_IGNORED(zfs_arc_dnode_limit
, arc_dnode_size_limit
,
7273 /* Valid range: 1 - N */
7274 if (zfs_arc_grow_retry
)
7275 arc_grow_retry
= zfs_arc_grow_retry
;
7277 /* Valid range: 1 - N */
7278 if (zfs_arc_shrink_shift
) {
7279 arc_shrink_shift
= zfs_arc_shrink_shift
;
7280 arc_no_grow_shift
= MIN(arc_no_grow_shift
, arc_shrink_shift
-1);
7283 /* Valid range: 1 - N */
7284 if (zfs_arc_p_min_shift
)
7285 arc_p_min_shift
= zfs_arc_p_min_shift
;
7287 /* Valid range: 1 - N ms */
7288 if (zfs_arc_min_prefetch_ms
)
7289 arc_min_prefetch_ms
= zfs_arc_min_prefetch_ms
;
7291 /* Valid range: 1 - N ms */
7292 if (zfs_arc_min_prescient_prefetch_ms
) {
7293 arc_min_prescient_prefetch_ms
=
7294 zfs_arc_min_prescient_prefetch_ms
;
7297 /* Valid range: 0 - 100 */
7298 if ((zfs_arc_lotsfree_percent
>= 0) &&
7299 (zfs_arc_lotsfree_percent
<= 100))
7300 arc_lotsfree_percent
= zfs_arc_lotsfree_percent
;
7301 WARN_IF_TUNING_IGNORED(zfs_arc_lotsfree_percent
, arc_lotsfree_percent
,
7304 /* Valid range: 0 - <all physical memory> */
7305 if ((zfs_arc_sys_free
) && (zfs_arc_sys_free
!= arc_sys_free
))
7306 arc_sys_free
= MIN(MAX(zfs_arc_sys_free
, 0), allmem
);
7307 WARN_IF_TUNING_IGNORED(zfs_arc_sys_free
, arc_sys_free
, verbose
);
7311 arc_state_init(void)
7313 arc_anon
= &ARC_anon
;
7315 arc_mru_ghost
= &ARC_mru_ghost
;
7317 arc_mfu_ghost
= &ARC_mfu_ghost
;
7318 arc_l2c_only
= &ARC_l2c_only
;
7320 arc_mru
->arcs_list
[ARC_BUFC_METADATA
] =
7321 multilist_create(sizeof (arc_buf_hdr_t
),
7322 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7323 arc_state_multilist_index_func
);
7324 arc_mru
->arcs_list
[ARC_BUFC_DATA
] =
7325 multilist_create(sizeof (arc_buf_hdr_t
),
7326 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7327 arc_state_multilist_index_func
);
7328 arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
] =
7329 multilist_create(sizeof (arc_buf_hdr_t
),
7330 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7331 arc_state_multilist_index_func
);
7332 arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
] =
7333 multilist_create(sizeof (arc_buf_hdr_t
),
7334 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7335 arc_state_multilist_index_func
);
7336 arc_mfu
->arcs_list
[ARC_BUFC_METADATA
] =
7337 multilist_create(sizeof (arc_buf_hdr_t
),
7338 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7339 arc_state_multilist_index_func
);
7340 arc_mfu
->arcs_list
[ARC_BUFC_DATA
] =
7341 multilist_create(sizeof (arc_buf_hdr_t
),
7342 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7343 arc_state_multilist_index_func
);
7344 arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
] =
7345 multilist_create(sizeof (arc_buf_hdr_t
),
7346 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7347 arc_state_multilist_index_func
);
7348 arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
] =
7349 multilist_create(sizeof (arc_buf_hdr_t
),
7350 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7351 arc_state_multilist_index_func
);
7352 arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
] =
7353 multilist_create(sizeof (arc_buf_hdr_t
),
7354 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7355 arc_state_multilist_index_func
);
7356 arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
] =
7357 multilist_create(sizeof (arc_buf_hdr_t
),
7358 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7359 arc_state_multilist_index_func
);
7361 zfs_refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7362 zfs_refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7363 zfs_refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7364 zfs_refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7365 zfs_refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7366 zfs_refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7367 zfs_refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7368 zfs_refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7369 zfs_refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7370 zfs_refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7371 zfs_refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7372 zfs_refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7374 zfs_refcount_create(&arc_anon
->arcs_size
);
7375 zfs_refcount_create(&arc_mru
->arcs_size
);
7376 zfs_refcount_create(&arc_mru_ghost
->arcs_size
);
7377 zfs_refcount_create(&arc_mfu
->arcs_size
);
7378 zfs_refcount_create(&arc_mfu_ghost
->arcs_size
);
7379 zfs_refcount_create(&arc_l2c_only
->arcs_size
);
7381 aggsum_init(&arc_meta_used
, 0);
7382 aggsum_init(&arc_size
, 0);
7383 aggsum_init(&astat_data_size
, 0);
7384 aggsum_init(&astat_metadata_size
, 0);
7385 aggsum_init(&astat_hdr_size
, 0);
7386 aggsum_init(&astat_l2_hdr_size
, 0);
7387 aggsum_init(&astat_bonus_size
, 0);
7388 aggsum_init(&astat_dnode_size
, 0);
7389 aggsum_init(&astat_dbuf_size
, 0);
7390 aggsum_init(&astat_abd_chunk_waste_size
, 0);
7392 arc_anon
->arcs_state
= ARC_STATE_ANON
;
7393 arc_mru
->arcs_state
= ARC_STATE_MRU
;
7394 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
7395 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
7396 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
7397 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
7401 arc_state_fini(void)
7403 zfs_refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7404 zfs_refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7405 zfs_refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7406 zfs_refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7407 zfs_refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7408 zfs_refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7409 zfs_refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7410 zfs_refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7411 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7412 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7413 zfs_refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7414 zfs_refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7416 zfs_refcount_destroy(&arc_anon
->arcs_size
);
7417 zfs_refcount_destroy(&arc_mru
->arcs_size
);
7418 zfs_refcount_destroy(&arc_mru_ghost
->arcs_size
);
7419 zfs_refcount_destroy(&arc_mfu
->arcs_size
);
7420 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_size
);
7421 zfs_refcount_destroy(&arc_l2c_only
->arcs_size
);
7423 multilist_destroy(arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
7424 multilist_destroy(arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7425 multilist_destroy(arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
7426 multilist_destroy(arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7427 multilist_destroy(arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
7428 multilist_destroy(arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7429 multilist_destroy(arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
7430 multilist_destroy(arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7431 multilist_destroy(arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]);
7432 multilist_destroy(arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]);
7434 aggsum_fini(&arc_meta_used
);
7435 aggsum_fini(&arc_size
);
7436 aggsum_fini(&astat_data_size
);
7437 aggsum_fini(&astat_metadata_size
);
7438 aggsum_fini(&astat_hdr_size
);
7439 aggsum_fini(&astat_l2_hdr_size
);
7440 aggsum_fini(&astat_bonus_size
);
7441 aggsum_fini(&astat_dnode_size
);
7442 aggsum_fini(&astat_dbuf_size
);
7443 aggsum_fini(&astat_abd_chunk_waste_size
);
7447 arc_target_bytes(void)
7455 uint64_t percent
, allmem
= arc_all_memory();
7456 mutex_init(&arc_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7457 list_create(&arc_evict_waiters
, sizeof (arc_evict_waiter_t
),
7458 offsetof(arc_evict_waiter_t
, aew_node
));
7460 arc_min_prefetch_ms
= 1000;
7461 arc_min_prescient_prefetch_ms
= 6000;
7463 #if defined(_KERNEL)
7467 /* Set min cache to 1/32 of all memory, or 32MB, whichever is more. */
7468 arc_c_min
= MAX(allmem
/ 32, 2ULL << SPA_MAXBLOCKSHIFT
);
7470 /* How to set default max varies by platform. */
7471 arc_c_max
= arc_default_max(arc_c_min
, allmem
);
7475 * In userland, there's only the memory pressure that we artificially
7476 * create (see arc_available_memory()). Don't let arc_c get too
7477 * small, because it can cause transactions to be larger than
7478 * arc_c, causing arc_tempreserve_space() to fail.
7480 arc_c_min
= MAX(arc_c_max
/ 2, 2ULL << SPA_MAXBLOCKSHIFT
);
7484 arc_p
= (arc_c
>> 1);
7486 /* Set min to 1/2 of arc_c_min */
7487 arc_meta_min
= 1ULL << SPA_MAXBLOCKSHIFT
;
7488 /* Initialize maximum observed usage to zero */
7491 * Set arc_meta_limit to a percent of arc_c_max with a floor of
7492 * arc_meta_min, and a ceiling of arc_c_max.
7494 percent
= MIN(zfs_arc_meta_limit_percent
, 100);
7495 arc_meta_limit
= MAX(arc_meta_min
, (percent
* arc_c_max
) / 100);
7496 percent
= MIN(zfs_arc_dnode_limit_percent
, 100);
7497 arc_dnode_size_limit
= (percent
* arc_meta_limit
) / 100;
7499 /* Apply user specified tunings */
7500 arc_tuning_update(B_TRUE
);
7502 /* if kmem_flags are set, lets try to use less memory */
7503 if (kmem_debugging())
7505 if (arc_c
< arc_c_min
)
7512 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
7513 offsetof(arc_prune_t
, p_node
));
7514 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
7516 arc_prune_taskq
= taskq_create("arc_prune", boot_ncpus
, defclsyspri
,
7517 boot_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
7519 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
7520 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
7522 if (arc_ksp
!= NULL
) {
7523 arc_ksp
->ks_data
= &arc_stats
;
7524 arc_ksp
->ks_update
= arc_kstat_update
;
7525 kstat_install(arc_ksp
);
7528 arc_evict_zthr
= zthr_create_timer("arc_evict",
7529 arc_evict_cb_check
, arc_evict_cb
, NULL
, SEC2NSEC(1));
7530 arc_reap_zthr
= zthr_create_timer("arc_reap",
7531 arc_reap_cb_check
, arc_reap_cb
, NULL
, SEC2NSEC(1));
7536 * Calculate maximum amount of dirty data per pool.
7538 * If it has been set by a module parameter, take that.
7539 * Otherwise, use a percentage of physical memory defined by
7540 * zfs_dirty_data_max_percent (default 10%) with a cap at
7541 * zfs_dirty_data_max_max (default 4G or 25% of physical memory).
7544 if (zfs_dirty_data_max_max
== 0)
7545 zfs_dirty_data_max_max
= MIN(4ULL * 1024 * 1024 * 1024,
7546 allmem
* zfs_dirty_data_max_max_percent
/ 100);
7548 if (zfs_dirty_data_max_max
== 0)
7549 zfs_dirty_data_max_max
= MIN(1ULL * 1024 * 1024 * 1024,
7550 allmem
* zfs_dirty_data_max_max_percent
/ 100);
7553 if (zfs_dirty_data_max
== 0) {
7554 zfs_dirty_data_max
= allmem
*
7555 zfs_dirty_data_max_percent
/ 100;
7556 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
7557 zfs_dirty_data_max_max
);
7568 #endif /* _KERNEL */
7570 /* Use B_TRUE to ensure *all* buffers are evicted */
7571 arc_flush(NULL
, B_TRUE
);
7573 if (arc_ksp
!= NULL
) {
7574 kstat_delete(arc_ksp
);
7578 taskq_wait(arc_prune_taskq
);
7579 taskq_destroy(arc_prune_taskq
);
7581 mutex_enter(&arc_prune_mtx
);
7582 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
7583 list_remove(&arc_prune_list
, p
);
7584 zfs_refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
7585 zfs_refcount_destroy(&p
->p_refcnt
);
7586 kmem_free(p
, sizeof (*p
));
7588 mutex_exit(&arc_prune_mtx
);
7590 list_destroy(&arc_prune_list
);
7591 mutex_destroy(&arc_prune_mtx
);
7593 (void) zthr_cancel(arc_evict_zthr
);
7594 (void) zthr_cancel(arc_reap_zthr
);
7596 mutex_destroy(&arc_evict_lock
);
7597 list_destroy(&arc_evict_waiters
);
7600 * Free any buffers that were tagged for destruction. This needs
7601 * to occur before arc_state_fini() runs and destroys the aggsum
7602 * values which are updated when freeing scatter ABDs.
7604 l2arc_do_free_on_write();
7607 * buf_fini() must proceed arc_state_fini() because buf_fin() may
7608 * trigger the release of kmem magazines, which can callback to
7609 * arc_space_return() which accesses aggsums freed in act_state_fini().
7615 * We destroy the zthrs after all the ARC state has been
7616 * torn down to avoid the case of them receiving any
7617 * wakeup() signals after they are destroyed.
7619 zthr_destroy(arc_evict_zthr
);
7620 zthr_destroy(arc_reap_zthr
);
7622 ASSERT0(arc_loaned_bytes
);
7628 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
7629 * It uses dedicated storage devices to hold cached data, which are populated
7630 * using large infrequent writes. The main role of this cache is to boost
7631 * the performance of random read workloads. The intended L2ARC devices
7632 * include short-stroked disks, solid state disks, and other media with
7633 * substantially faster read latency than disk.
7635 * +-----------------------+
7637 * +-----------------------+
7640 * l2arc_feed_thread() arc_read()
7644 * +---------------+ |
7646 * +---------------+ |
7651 * +-------+ +-------+
7653 * | cache | | cache |
7654 * +-------+ +-------+
7655 * +=========+ .-----.
7656 * : L2ARC : |-_____-|
7657 * : devices : | Disks |
7658 * +=========+ `-_____-'
7660 * Read requests are satisfied from the following sources, in order:
7663 * 2) vdev cache of L2ARC devices
7665 * 4) vdev cache of disks
7668 * Some L2ARC device types exhibit extremely slow write performance.
7669 * To accommodate for this there are some significant differences between
7670 * the L2ARC and traditional cache design:
7672 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
7673 * the ARC behave as usual, freeing buffers and placing headers on ghost
7674 * lists. The ARC does not send buffers to the L2ARC during eviction as
7675 * this would add inflated write latencies for all ARC memory pressure.
7677 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
7678 * It does this by periodically scanning buffers from the eviction-end of
7679 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
7680 * not already there. It scans until a headroom of buffers is satisfied,
7681 * which itself is a buffer for ARC eviction. If a compressible buffer is
7682 * found during scanning and selected for writing to an L2ARC device, we
7683 * temporarily boost scanning headroom during the next scan cycle to make
7684 * sure we adapt to compression effects (which might significantly reduce
7685 * the data volume we write to L2ARC). The thread that does this is
7686 * l2arc_feed_thread(), illustrated below; example sizes are included to
7687 * provide a better sense of ratio than this diagram:
7690 * +---------------------+----------+
7691 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
7692 * +---------------------+----------+ | o L2ARC eligible
7693 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
7694 * +---------------------+----------+ |
7695 * 15.9 Gbytes ^ 32 Mbytes |
7697 * l2arc_feed_thread()
7699 * l2arc write hand <--[oooo]--'
7703 * +==============================+
7704 * L2ARC dev |####|#|###|###| |####| ... |
7705 * +==============================+
7708 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
7709 * evicted, then the L2ARC has cached a buffer much sooner than it probably
7710 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
7711 * safe to say that this is an uncommon case, since buffers at the end of
7712 * the ARC lists have moved there due to inactivity.
7714 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
7715 * then the L2ARC simply misses copying some buffers. This serves as a
7716 * pressure valve to prevent heavy read workloads from both stalling the ARC
7717 * with waits and clogging the L2ARC with writes. This also helps prevent
7718 * the potential for the L2ARC to churn if it attempts to cache content too
7719 * quickly, such as during backups of the entire pool.
7721 * 5. After system boot and before the ARC has filled main memory, there are
7722 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
7723 * lists can remain mostly static. Instead of searching from tail of these
7724 * lists as pictured, the l2arc_feed_thread() will search from the list heads
7725 * for eligible buffers, greatly increasing its chance of finding them.
7727 * The L2ARC device write speed is also boosted during this time so that
7728 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
7729 * there are no L2ARC reads, and no fear of degrading read performance
7730 * through increased writes.
7732 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
7733 * the vdev queue can aggregate them into larger and fewer writes. Each
7734 * device is written to in a rotor fashion, sweeping writes through
7735 * available space then repeating.
7737 * 7. The L2ARC does not store dirty content. It never needs to flush
7738 * write buffers back to disk based storage.
7740 * 8. If an ARC buffer is written (and dirtied) which also exists in the
7741 * L2ARC, the now stale L2ARC buffer is immediately dropped.
7743 * The performance of the L2ARC can be tweaked by a number of tunables, which
7744 * may be necessary for different workloads:
7746 * l2arc_write_max max write bytes per interval
7747 * l2arc_write_boost extra write bytes during device warmup
7748 * l2arc_noprefetch skip caching prefetched buffers
7749 * l2arc_headroom number of max device writes to precache
7750 * l2arc_headroom_boost when we find compressed buffers during ARC
7751 * scanning, we multiply headroom by this
7752 * percentage factor for the next scan cycle,
7753 * since more compressed buffers are likely to
7755 * l2arc_feed_secs seconds between L2ARC writing
7757 * Tunables may be removed or added as future performance improvements are
7758 * integrated, and also may become zpool properties.
7760 * There are three key functions that control how the L2ARC warms up:
7762 * l2arc_write_eligible() check if a buffer is eligible to cache
7763 * l2arc_write_size() calculate how much to write
7764 * l2arc_write_interval() calculate sleep delay between writes
7766 * These three functions determine what to write, how much, and how quickly
7769 * L2ARC persistence:
7771 * When writing buffers to L2ARC, we periodically add some metadata to
7772 * make sure we can pick them up after reboot, thus dramatically reducing
7773 * the impact that any downtime has on the performance of storage systems
7774 * with large caches.
7776 * The implementation works fairly simply by integrating the following two
7779 * *) When writing to the L2ARC, we occasionally write a "l2arc log block",
7780 * which is an additional piece of metadata which describes what's been
7781 * written. This allows us to rebuild the arc_buf_hdr_t structures of the
7782 * main ARC buffers. There are 2 linked-lists of log blocks headed by
7783 * dh_start_lbps[2]. We alternate which chain we append to, so they are
7784 * time-wise and offset-wise interleaved, but that is an optimization rather
7785 * than for correctness. The log block also includes a pointer to the
7786 * previous block in its chain.
7788 * *) We reserve SPA_MINBLOCKSIZE of space at the start of each L2ARC device
7789 * for our header bookkeeping purposes. This contains a device header,
7790 * which contains our top-level reference structures. We update it each
7791 * time we write a new log block, so that we're able to locate it in the
7792 * L2ARC device. If this write results in an inconsistent device header
7793 * (e.g. due to power failure), we detect this by verifying the header's
7794 * checksum and simply fail to reconstruct the L2ARC after reboot.
7796 * Implementation diagram:
7798 * +=== L2ARC device (not to scale) ======================================+
7799 * | ___two newest log block pointers__.__________ |
7800 * | / \dh_start_lbps[1] |
7801 * | / \ \dh_start_lbps[0]|
7803 * ||L2 dev|....|lb |bufs |lb |bufs |lb |bufs |lb |bufs |lb |---(empty)---|
7804 * || hdr| ^ /^ /^ / / |
7805 * |+------+ ...--\-------/ \-----/--\------/ / |
7806 * | \--------------/ \--------------/ |
7807 * +======================================================================+
7809 * As can be seen on the diagram, rather than using a simple linked list,
7810 * we use a pair of linked lists with alternating elements. This is a
7811 * performance enhancement due to the fact that we only find out the
7812 * address of the next log block access once the current block has been
7813 * completely read in. Obviously, this hurts performance, because we'd be
7814 * keeping the device's I/O queue at only a 1 operation deep, thus
7815 * incurring a large amount of I/O round-trip latency. Having two lists
7816 * allows us to fetch two log blocks ahead of where we are currently
7817 * rebuilding L2ARC buffers.
7819 * On-device data structures:
7821 * L2ARC device header: l2arc_dev_hdr_phys_t
7822 * L2ARC log block: l2arc_log_blk_phys_t
7824 * L2ARC reconstruction:
7826 * When writing data, we simply write in the standard rotary fashion,
7827 * evicting buffers as we go and simply writing new data over them (writing
7828 * a new log block every now and then). This obviously means that once we
7829 * loop around the end of the device, we will start cutting into an already
7830 * committed log block (and its referenced data buffers), like so:
7832 * current write head__ __old tail
7835 * <--|bufs |lb |bufs |lb | |bufs |lb |bufs |lb |-->
7836 * ^ ^^^^^^^^^___________________________________
7838 * <<nextwrite>> may overwrite this blk and/or its bufs --'
7840 * When importing the pool, we detect this situation and use it to stop
7841 * our scanning process (see l2arc_rebuild).
7843 * There is one significant caveat to consider when rebuilding ARC contents
7844 * from an L2ARC device: what about invalidated buffers? Given the above
7845 * construction, we cannot update blocks which we've already written to amend
7846 * them to remove buffers which were invalidated. Thus, during reconstruction,
7847 * we might be populating the cache with buffers for data that's not on the
7848 * main pool anymore, or may have been overwritten!
7850 * As it turns out, this isn't a problem. Every arc_read request includes
7851 * both the DVA and, crucially, the birth TXG of the BP the caller is
7852 * looking for. So even if the cache were populated by completely rotten
7853 * blocks for data that had been long deleted and/or overwritten, we'll
7854 * never actually return bad data from the cache, since the DVA with the
7855 * birth TXG uniquely identify a block in space and time - once created,
7856 * a block is immutable on disk. The worst thing we have done is wasted
7857 * some time and memory at l2arc rebuild to reconstruct outdated ARC
7858 * entries that will get dropped from the l2arc as it is being updated
7861 * L2ARC buffers that have been evicted by l2arc_evict() ahead of the write
7862 * hand are not restored. This is done by saving the offset (in bytes)
7863 * l2arc_evict() has evicted to in the L2ARC device header and taking it
7864 * into account when restoring buffers.
7868 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*hdr
)
7871 * A buffer is *not* eligible for the L2ARC if it:
7872 * 1. belongs to a different spa.
7873 * 2. is already cached on the L2ARC.
7874 * 3. has an I/O in progress (it may be an incomplete read).
7875 * 4. is flagged not eligible (zfs property).
7877 if (hdr
->b_spa
!= spa_guid
|| HDR_HAS_L2HDR(hdr
) ||
7878 HDR_IO_IN_PROGRESS(hdr
) || !HDR_L2CACHE(hdr
))
7885 l2arc_write_size(l2arc_dev_t
*dev
)
7887 uint64_t size
, dev_size
, tsize
;
7890 * Make sure our globals have meaningful values in case the user
7893 size
= l2arc_write_max
;
7895 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
7896 "be greater than zero, resetting it to the default (%d)",
7898 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
7901 if (arc_warm
== B_FALSE
)
7902 size
+= l2arc_write_boost
;
7905 * Make sure the write size does not exceed the size of the cache
7906 * device. This is important in l2arc_evict(), otherwise infinite
7907 * iteration can occur.
7909 dev_size
= dev
->l2ad_end
- dev
->l2ad_start
;
7910 tsize
= size
+ l2arc_log_blk_overhead(size
, dev
);
7911 if (dev
->l2ad_vdev
->vdev_has_trim
&& l2arc_trim_ahead
> 0)
7912 tsize
+= MAX(64 * 1024 * 1024,
7913 (tsize
* l2arc_trim_ahead
) / 100);
7915 if (tsize
>= dev_size
) {
7916 cmn_err(CE_NOTE
, "l2arc_write_max or l2arc_write_boost "
7917 "plus the overhead of log blocks (persistent L2ARC, "
7918 "%llu bytes) exceeds the size of the cache device "
7919 "(guid %llu), resetting them to the default (%d)",
7920 l2arc_log_blk_overhead(size
, dev
),
7921 dev
->l2ad_vdev
->vdev_guid
, L2ARC_WRITE_SIZE
);
7922 size
= l2arc_write_max
= l2arc_write_boost
= L2ARC_WRITE_SIZE
;
7924 if (arc_warm
== B_FALSE
)
7925 size
+= l2arc_write_boost
;
7933 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
7935 clock_t interval
, next
, now
;
7938 * If the ARC lists are busy, increase our write rate; if the
7939 * lists are stale, idle back. This is achieved by checking
7940 * how much we previously wrote - if it was more than half of
7941 * what we wanted, schedule the next write much sooner.
7943 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
7944 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
7946 interval
= hz
* l2arc_feed_secs
;
7948 now
= ddi_get_lbolt();
7949 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
7955 * Cycle through L2ARC devices. This is how L2ARC load balances.
7956 * If a device is returned, this also returns holding the spa config lock.
7958 static l2arc_dev_t
*
7959 l2arc_dev_get_next(void)
7961 l2arc_dev_t
*first
, *next
= NULL
;
7964 * Lock out the removal of spas (spa_namespace_lock), then removal
7965 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
7966 * both locks will be dropped and a spa config lock held instead.
7968 mutex_enter(&spa_namespace_lock
);
7969 mutex_enter(&l2arc_dev_mtx
);
7971 /* if there are no vdevs, there is nothing to do */
7972 if (l2arc_ndev
== 0)
7976 next
= l2arc_dev_last
;
7978 /* loop around the list looking for a non-faulted vdev */
7980 next
= list_head(l2arc_dev_list
);
7982 next
= list_next(l2arc_dev_list
, next
);
7984 next
= list_head(l2arc_dev_list
);
7987 /* if we have come back to the start, bail out */
7990 else if (next
== first
)
7993 } while (vdev_is_dead(next
->l2ad_vdev
) || next
->l2ad_rebuild
||
7994 next
->l2ad_trim_all
);
7996 /* if we were unable to find any usable vdevs, return NULL */
7997 if (vdev_is_dead(next
->l2ad_vdev
) || next
->l2ad_rebuild
||
7998 next
->l2ad_trim_all
)
8001 l2arc_dev_last
= next
;
8004 mutex_exit(&l2arc_dev_mtx
);
8007 * Grab the config lock to prevent the 'next' device from being
8008 * removed while we are writing to it.
8011 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
8012 mutex_exit(&spa_namespace_lock
);
8018 * Free buffers that were tagged for destruction.
8021 l2arc_do_free_on_write(void)
8024 l2arc_data_free_t
*df
, *df_prev
;
8026 mutex_enter(&l2arc_free_on_write_mtx
);
8027 buflist
= l2arc_free_on_write
;
8029 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
8030 df_prev
= list_prev(buflist
, df
);
8031 ASSERT3P(df
->l2df_abd
, !=, NULL
);
8032 abd_free(df
->l2df_abd
);
8033 list_remove(buflist
, df
);
8034 kmem_free(df
, sizeof (l2arc_data_free_t
));
8037 mutex_exit(&l2arc_free_on_write_mtx
);
8041 * A write to a cache device has completed. Update all headers to allow
8042 * reads from these buffers to begin.
8045 l2arc_write_done(zio_t
*zio
)
8047 l2arc_write_callback_t
*cb
;
8048 l2arc_lb_abd_buf_t
*abd_buf
;
8049 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
8051 l2arc_dev_hdr_phys_t
*l2dhdr
;
8053 arc_buf_hdr_t
*head
, *hdr
, *hdr_prev
;
8054 kmutex_t
*hash_lock
;
8055 int64_t bytes_dropped
= 0;
8057 cb
= zio
->io_private
;
8058 ASSERT3P(cb
, !=, NULL
);
8059 dev
= cb
->l2wcb_dev
;
8060 l2dhdr
= dev
->l2ad_dev_hdr
;
8061 ASSERT3P(dev
, !=, NULL
);
8062 head
= cb
->l2wcb_head
;
8063 ASSERT3P(head
, !=, NULL
);
8064 buflist
= &dev
->l2ad_buflist
;
8065 ASSERT3P(buflist
, !=, NULL
);
8066 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
8067 l2arc_write_callback_t
*, cb
);
8069 if (zio
->io_error
!= 0)
8070 ARCSTAT_BUMP(arcstat_l2_writes_error
);
8073 * All writes completed, or an error was hit.
8076 mutex_enter(&dev
->l2ad_mtx
);
8077 for (hdr
= list_prev(buflist
, head
); hdr
; hdr
= hdr_prev
) {
8078 hdr_prev
= list_prev(buflist
, hdr
);
8080 hash_lock
= HDR_LOCK(hdr
);
8083 * We cannot use mutex_enter or else we can deadlock
8084 * with l2arc_write_buffers (due to swapping the order
8085 * the hash lock and l2ad_mtx are taken).
8087 if (!mutex_tryenter(hash_lock
)) {
8089 * Missed the hash lock. We must retry so we
8090 * don't leave the ARC_FLAG_L2_WRITING bit set.
8092 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry
);
8095 * We don't want to rescan the headers we've
8096 * already marked as having been written out, so
8097 * we reinsert the head node so we can pick up
8098 * where we left off.
8100 list_remove(buflist
, head
);
8101 list_insert_after(buflist
, hdr
, head
);
8103 mutex_exit(&dev
->l2ad_mtx
);
8106 * We wait for the hash lock to become available
8107 * to try and prevent busy waiting, and increase
8108 * the chance we'll be able to acquire the lock
8109 * the next time around.
8111 mutex_enter(hash_lock
);
8112 mutex_exit(hash_lock
);
8117 * We could not have been moved into the arc_l2c_only
8118 * state while in-flight due to our ARC_FLAG_L2_WRITING
8119 * bit being set. Let's just ensure that's being enforced.
8121 ASSERT(HDR_HAS_L1HDR(hdr
));
8124 * Skipped - drop L2ARC entry and mark the header as no
8125 * longer L2 eligibile.
8127 if (zio
->io_error
!= 0) {
8129 * Error - drop L2ARC entry.
8131 list_remove(buflist
, hdr
);
8132 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
8134 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8135 ARCSTAT_INCR(arcstat_l2_psize
, -psize
);
8136 ARCSTAT_INCR(arcstat_l2_lsize
, -HDR_GET_LSIZE(hdr
));
8139 vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
8140 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
,
8141 arc_hdr_size(hdr
), hdr
);
8145 * Allow ARC to begin reads and ghost list evictions to
8148 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_WRITING
);
8150 mutex_exit(hash_lock
);
8154 * Free the allocated abd buffers for writing the log blocks.
8155 * If the zio failed reclaim the allocated space and remove the
8156 * pointers to these log blocks from the log block pointer list
8157 * of the L2ARC device.
8159 while ((abd_buf
= list_remove_tail(&cb
->l2wcb_abd_list
)) != NULL
) {
8160 abd_free(abd_buf
->abd
);
8161 zio_buf_free(abd_buf
, sizeof (*abd_buf
));
8162 if (zio
->io_error
!= 0) {
8163 lb_ptr_buf
= list_remove_head(&dev
->l2ad_lbptr_list
);
8165 * L2BLK_GET_PSIZE returns aligned size for log
8169 L2BLK_GET_PSIZE((lb_ptr_buf
->lb_ptr
)->lbp_prop
);
8170 bytes_dropped
+= asize
;
8171 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, -asize
);
8172 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count
);
8173 zfs_refcount_remove_many(&dev
->l2ad_lb_asize
, asize
,
8175 zfs_refcount_remove(&dev
->l2ad_lb_count
, lb_ptr_buf
);
8176 kmem_free(lb_ptr_buf
->lb_ptr
,
8177 sizeof (l2arc_log_blkptr_t
));
8178 kmem_free(lb_ptr_buf
, sizeof (l2arc_lb_ptr_buf_t
));
8181 list_destroy(&cb
->l2wcb_abd_list
);
8183 if (zio
->io_error
!= 0) {
8185 * Restore the lbps array in the header to its previous state.
8186 * If the list of log block pointers is empty, zero out the
8187 * log block pointers in the device header.
8189 lb_ptr_buf
= list_head(&dev
->l2ad_lbptr_list
);
8190 for (int i
= 0; i
< 2; i
++) {
8191 if (lb_ptr_buf
== NULL
) {
8193 * If the list is empty zero out the device
8194 * header. Otherwise zero out the second log
8195 * block pointer in the header.
8198 bzero(l2dhdr
, dev
->l2ad_dev_hdr_asize
);
8200 bzero(&l2dhdr
->dh_start_lbps
[i
],
8201 sizeof (l2arc_log_blkptr_t
));
8205 bcopy(lb_ptr_buf
->lb_ptr
, &l2dhdr
->dh_start_lbps
[i
],
8206 sizeof (l2arc_log_blkptr_t
));
8207 lb_ptr_buf
= list_next(&dev
->l2ad_lbptr_list
,
8212 atomic_inc_64(&l2arc_writes_done
);
8213 list_remove(buflist
, head
);
8214 ASSERT(!HDR_HAS_L1HDR(head
));
8215 kmem_cache_free(hdr_l2only_cache
, head
);
8216 mutex_exit(&dev
->l2ad_mtx
);
8218 ASSERT(dev
->l2ad_vdev
!= NULL
);
8219 vdev_space_update(dev
->l2ad_vdev
, -bytes_dropped
, 0, 0);
8221 l2arc_do_free_on_write();
8223 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
8227 l2arc_untransform(zio_t
*zio
, l2arc_read_callback_t
*cb
)
8230 spa_t
*spa
= zio
->io_spa
;
8231 arc_buf_hdr_t
*hdr
= cb
->l2rcb_hdr
;
8232 blkptr_t
*bp
= zio
->io_bp
;
8233 uint8_t salt
[ZIO_DATA_SALT_LEN
];
8234 uint8_t iv
[ZIO_DATA_IV_LEN
];
8235 uint8_t mac
[ZIO_DATA_MAC_LEN
];
8236 boolean_t no_crypt
= B_FALSE
;
8239 * ZIL data is never be written to the L2ARC, so we don't need
8240 * special handling for its unique MAC storage.
8242 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
8243 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
8244 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8247 * If the data was encrypted, decrypt it now. Note that
8248 * we must check the bp here and not the hdr, since the
8249 * hdr does not have its encryption parameters updated
8250 * until arc_read_done().
8252 if (BP_IS_ENCRYPTED(bp
)) {
8253 abd_t
*eabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
,
8256 zio_crypt_decode_params_bp(bp
, salt
, iv
);
8257 zio_crypt_decode_mac_bp(bp
, mac
);
8259 ret
= spa_do_crypt_abd(B_FALSE
, spa
, &cb
->l2rcb_zb
,
8260 BP_GET_TYPE(bp
), BP_GET_DEDUP(bp
), BP_SHOULD_BYTESWAP(bp
),
8261 salt
, iv
, mac
, HDR_GET_PSIZE(hdr
), eabd
,
8262 hdr
->b_l1hdr
.b_pabd
, &no_crypt
);
8264 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8269 * If we actually performed decryption, replace b_pabd
8270 * with the decrypted data. Otherwise we can just throw
8271 * our decryption buffer away.
8274 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8275 arc_hdr_size(hdr
), hdr
);
8276 hdr
->b_l1hdr
.b_pabd
= eabd
;
8279 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8284 * If the L2ARC block was compressed, but ARC compression
8285 * is disabled we decompress the data into a new buffer and
8286 * replace the existing data.
8288 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
8289 !HDR_COMPRESSION_ENABLED(hdr
)) {
8290 abd_t
*cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
,
8292 void *tmp
= abd_borrow_buf(cabd
, arc_hdr_size(hdr
));
8294 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
8295 hdr
->b_l1hdr
.b_pabd
, tmp
, HDR_GET_PSIZE(hdr
),
8296 HDR_GET_LSIZE(hdr
), &hdr
->b_complevel
);
8298 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
8299 arc_free_data_abd(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
8303 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
8304 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8305 arc_hdr_size(hdr
), hdr
);
8306 hdr
->b_l1hdr
.b_pabd
= cabd
;
8308 zio
->io_size
= HDR_GET_LSIZE(hdr
);
8319 * A read to a cache device completed. Validate buffer contents before
8320 * handing over to the regular ARC routines.
8323 l2arc_read_done(zio_t
*zio
)
8326 l2arc_read_callback_t
*cb
= zio
->io_private
;
8328 kmutex_t
*hash_lock
;
8329 boolean_t valid_cksum
;
8330 boolean_t using_rdata
= (BP_IS_ENCRYPTED(&cb
->l2rcb_bp
) &&
8331 (cb
->l2rcb_flags
& ZIO_FLAG_RAW_ENCRYPT
));
8333 ASSERT3P(zio
->io_vd
, !=, NULL
);
8334 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
8336 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
8338 ASSERT3P(cb
, !=, NULL
);
8339 hdr
= cb
->l2rcb_hdr
;
8340 ASSERT3P(hdr
, !=, NULL
);
8342 hash_lock
= HDR_LOCK(hdr
);
8343 mutex_enter(hash_lock
);
8344 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
8347 * If the data was read into a temporary buffer,
8348 * move it and free the buffer.
8350 if (cb
->l2rcb_abd
!= NULL
) {
8351 ASSERT3U(arc_hdr_size(hdr
), <, zio
->io_size
);
8352 if (zio
->io_error
== 0) {
8354 abd_copy(hdr
->b_crypt_hdr
.b_rabd
,
8355 cb
->l2rcb_abd
, arc_hdr_size(hdr
));
8357 abd_copy(hdr
->b_l1hdr
.b_pabd
,
8358 cb
->l2rcb_abd
, arc_hdr_size(hdr
));
8363 * The following must be done regardless of whether
8364 * there was an error:
8365 * - free the temporary buffer
8366 * - point zio to the real ARC buffer
8367 * - set zio size accordingly
8368 * These are required because zio is either re-used for
8369 * an I/O of the block in the case of the error
8370 * or the zio is passed to arc_read_done() and it
8373 abd_free(cb
->l2rcb_abd
);
8374 zio
->io_size
= zio
->io_orig_size
= arc_hdr_size(hdr
);
8377 ASSERT(HDR_HAS_RABD(hdr
));
8378 zio
->io_abd
= zio
->io_orig_abd
=
8379 hdr
->b_crypt_hdr
.b_rabd
;
8381 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8382 zio
->io_abd
= zio
->io_orig_abd
= hdr
->b_l1hdr
.b_pabd
;
8386 ASSERT3P(zio
->io_abd
, !=, NULL
);
8389 * Check this survived the L2ARC journey.
8391 ASSERT(zio
->io_abd
== hdr
->b_l1hdr
.b_pabd
||
8392 (HDR_HAS_RABD(hdr
) && zio
->io_abd
== hdr
->b_crypt_hdr
.b_rabd
));
8393 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
8394 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
8395 zio
->io_prop
.zp_complevel
= hdr
->b_complevel
;
8397 valid_cksum
= arc_cksum_is_equal(hdr
, zio
);
8400 * b_rabd will always match the data as it exists on disk if it is
8401 * being used. Therefore if we are reading into b_rabd we do not
8402 * attempt to untransform the data.
8404 if (valid_cksum
&& !using_rdata
)
8405 tfm_error
= l2arc_untransform(zio
, cb
);
8407 if (valid_cksum
&& tfm_error
== 0 && zio
->io_error
== 0 &&
8408 !HDR_L2_EVICTED(hdr
)) {
8409 mutex_exit(hash_lock
);
8410 zio
->io_private
= hdr
;
8414 * Buffer didn't survive caching. Increment stats and
8415 * reissue to the original storage device.
8417 if (zio
->io_error
!= 0) {
8418 ARCSTAT_BUMP(arcstat_l2_io_error
);
8420 zio
->io_error
= SET_ERROR(EIO
);
8422 if (!valid_cksum
|| tfm_error
!= 0)
8423 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
8426 * If there's no waiter, issue an async i/o to the primary
8427 * storage now. If there *is* a waiter, the caller must
8428 * issue the i/o in a context where it's OK to block.
8430 if (zio
->io_waiter
== NULL
) {
8431 zio_t
*pio
= zio_unique_parent(zio
);
8432 void *abd
= (using_rdata
) ?
8433 hdr
->b_crypt_hdr
.b_rabd
: hdr
->b_l1hdr
.b_pabd
;
8435 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
8437 zio
= zio_read(pio
, zio
->io_spa
, zio
->io_bp
,
8438 abd
, zio
->io_size
, arc_read_done
,
8439 hdr
, zio
->io_priority
, cb
->l2rcb_flags
,
8443 * Original ZIO will be freed, so we need to update
8444 * ARC header with the new ZIO pointer to be used
8445 * by zio_change_priority() in arc_read().
8447 for (struct arc_callback
*acb
= hdr
->b_l1hdr
.b_acb
;
8448 acb
!= NULL
; acb
= acb
->acb_next
)
8449 acb
->acb_zio_head
= zio
;
8451 mutex_exit(hash_lock
);
8454 mutex_exit(hash_lock
);
8458 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
8462 * This is the list priority from which the L2ARC will search for pages to
8463 * cache. This is used within loops (0..3) to cycle through lists in the
8464 * desired order. This order can have a significant effect on cache
8467 * Currently the metadata lists are hit first, MFU then MRU, followed by
8468 * the data lists. This function returns a locked list, and also returns
8471 static multilist_sublist_t
*
8472 l2arc_sublist_lock(int list_num
)
8474 multilist_t
*ml
= NULL
;
8477 ASSERT(list_num
>= 0 && list_num
< L2ARC_FEED_TYPES
);
8481 ml
= arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
8484 ml
= arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
8487 ml
= arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
8490 ml
= arc_mru
->arcs_list
[ARC_BUFC_DATA
];
8497 * Return a randomly-selected sublist. This is acceptable
8498 * because the caller feeds only a little bit of data for each
8499 * call (8MB). Subsequent calls will result in different
8500 * sublists being selected.
8502 idx
= multilist_get_random_index(ml
);
8503 return (multilist_sublist_lock(ml
, idx
));
8507 * Calculates the maximum overhead of L2ARC metadata log blocks for a given
8508 * L2ARC write size. l2arc_evict and l2arc_write_size need to include this
8509 * overhead in processing to make sure there is enough headroom available
8510 * when writing buffers.
8512 static inline uint64_t
8513 l2arc_log_blk_overhead(uint64_t write_sz
, l2arc_dev_t
*dev
)
8515 if (dev
->l2ad_log_entries
== 0) {
8518 uint64_t log_entries
= write_sz
>> SPA_MINBLOCKSHIFT
;
8520 uint64_t log_blocks
= (log_entries
+
8521 dev
->l2ad_log_entries
- 1) /
8522 dev
->l2ad_log_entries
;
8524 return (vdev_psize_to_asize(dev
->l2ad_vdev
,
8525 sizeof (l2arc_log_blk_phys_t
)) * log_blocks
);
8530 * Evict buffers from the device write hand to the distance specified in
8531 * bytes. This distance may span populated buffers, it may span nothing.
8532 * This is clearing a region on the L2ARC device ready for writing.
8533 * If the 'all' boolean is set, every buffer is evicted.
8536 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
8539 arc_buf_hdr_t
*hdr
, *hdr_prev
;
8540 kmutex_t
*hash_lock
;
8542 l2arc_lb_ptr_buf_t
*lb_ptr_buf
, *lb_ptr_buf_prev
;
8543 vdev_t
*vd
= dev
->l2ad_vdev
;
8546 buflist
= &dev
->l2ad_buflist
;
8549 * We need to add in the worst case scenario of log block overhead.
8551 distance
+= l2arc_log_blk_overhead(distance
, dev
);
8552 if (vd
->vdev_has_trim
&& l2arc_trim_ahead
> 0) {
8554 * Trim ahead of the write size 64MB or (l2arc_trim_ahead/100)
8555 * times the write size, whichever is greater.
8557 distance
+= MAX(64 * 1024 * 1024,
8558 (distance
* l2arc_trim_ahead
) / 100);
8563 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- distance
)) {
8565 * When there is no space to accommodate upcoming writes,
8566 * evict to the end. Then bump the write and evict hands
8567 * to the start and iterate. This iteration does not
8568 * happen indefinitely as we make sure in
8569 * l2arc_write_size() that when the write hand is reset,
8570 * the write size does not exceed the end of the device.
8573 taddr
= dev
->l2ad_end
;
8575 taddr
= dev
->l2ad_hand
+ distance
;
8577 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
8578 uint64_t, taddr
, boolean_t
, all
);
8582 * This check has to be placed after deciding whether to
8585 if (dev
->l2ad_first
) {
8587 * This is the first sweep through the device. There is
8588 * nothing to evict. We have already trimmmed the
8594 * Trim the space to be evicted.
8596 if (vd
->vdev_has_trim
&& dev
->l2ad_evict
< taddr
&&
8597 l2arc_trim_ahead
> 0) {
8599 * We have to drop the spa_config lock because
8600 * vdev_trim_range() will acquire it.
8601 * l2ad_evict already accounts for the label
8602 * size. To prevent vdev_trim_ranges() from
8603 * adding it again, we subtract it from
8606 spa_config_exit(dev
->l2ad_spa
, SCL_L2ARC
, dev
);
8607 vdev_trim_simple(vd
,
8608 dev
->l2ad_evict
- VDEV_LABEL_START_SIZE
,
8609 taddr
- dev
->l2ad_evict
);
8610 spa_config_enter(dev
->l2ad_spa
, SCL_L2ARC
, dev
,
8615 * When rebuilding L2ARC we retrieve the evict hand
8616 * from the header of the device. Of note, l2arc_evict()
8617 * does not actually delete buffers from the cache
8618 * device, but trimming may do so depending on the
8619 * hardware implementation. Thus keeping track of the
8620 * evict hand is useful.
8622 dev
->l2ad_evict
= MAX(dev
->l2ad_evict
, taddr
);
8627 mutex_enter(&dev
->l2ad_mtx
);
8629 * We have to account for evicted log blocks. Run vdev_space_update()
8630 * on log blocks whose offset (in bytes) is before the evicted offset
8631 * (in bytes) by searching in the list of pointers to log blocks
8632 * present in the L2ARC device.
8634 for (lb_ptr_buf
= list_tail(&dev
->l2ad_lbptr_list
); lb_ptr_buf
;
8635 lb_ptr_buf
= lb_ptr_buf_prev
) {
8637 lb_ptr_buf_prev
= list_prev(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
8639 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
8640 uint64_t asize
= L2BLK_GET_PSIZE(
8641 (lb_ptr_buf
->lb_ptr
)->lbp_prop
);
8644 * We don't worry about log blocks left behind (ie
8645 * lbp_payload_start < l2ad_hand) because l2arc_write_buffers()
8646 * will never write more than l2arc_evict() evicts.
8648 if (!all
&& l2arc_log_blkptr_valid(dev
, lb_ptr_buf
->lb_ptr
)) {
8651 vdev_space_update(vd
, -asize
, 0, 0);
8652 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, -asize
);
8653 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count
);
8654 zfs_refcount_remove_many(&dev
->l2ad_lb_asize
, asize
,
8656 zfs_refcount_remove(&dev
->l2ad_lb_count
, lb_ptr_buf
);
8657 list_remove(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
8658 kmem_free(lb_ptr_buf
->lb_ptr
,
8659 sizeof (l2arc_log_blkptr_t
));
8660 kmem_free(lb_ptr_buf
, sizeof (l2arc_lb_ptr_buf_t
));
8664 for (hdr
= list_tail(buflist
); hdr
; hdr
= hdr_prev
) {
8665 hdr_prev
= list_prev(buflist
, hdr
);
8667 ASSERT(!HDR_EMPTY(hdr
));
8668 hash_lock
= HDR_LOCK(hdr
);
8671 * We cannot use mutex_enter or else we can deadlock
8672 * with l2arc_write_buffers (due to swapping the order
8673 * the hash lock and l2ad_mtx are taken).
8675 if (!mutex_tryenter(hash_lock
)) {
8677 * Missed the hash lock. Retry.
8679 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
8680 mutex_exit(&dev
->l2ad_mtx
);
8681 mutex_enter(hash_lock
);
8682 mutex_exit(hash_lock
);
8687 * A header can't be on this list if it doesn't have L2 header.
8689 ASSERT(HDR_HAS_L2HDR(hdr
));
8691 /* Ensure this header has finished being written. */
8692 ASSERT(!HDR_L2_WRITING(hdr
));
8693 ASSERT(!HDR_L2_WRITE_HEAD(hdr
));
8695 if (!all
&& (hdr
->b_l2hdr
.b_daddr
>= dev
->l2ad_evict
||
8696 hdr
->b_l2hdr
.b_daddr
< dev
->l2ad_hand
)) {
8698 * We've evicted to the target address,
8699 * or the end of the device.
8701 mutex_exit(hash_lock
);
8705 if (!HDR_HAS_L1HDR(hdr
)) {
8706 ASSERT(!HDR_L2_READING(hdr
));
8708 * This doesn't exist in the ARC. Destroy.
8709 * arc_hdr_destroy() will call list_remove()
8710 * and decrement arcstat_l2_lsize.
8712 arc_change_state(arc_anon
, hdr
, hash_lock
);
8713 arc_hdr_destroy(hdr
);
8715 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_l2c_only
);
8716 ARCSTAT_BUMP(arcstat_l2_evict_l1cached
);
8718 * Invalidate issued or about to be issued
8719 * reads, since we may be about to write
8720 * over this location.
8722 if (HDR_L2_READING(hdr
)) {
8723 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
8724 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_EVICTED
);
8727 arc_hdr_l2hdr_destroy(hdr
);
8729 mutex_exit(hash_lock
);
8731 mutex_exit(&dev
->l2ad_mtx
);
8735 * We need to check if we evict all buffers, otherwise we may iterate
8738 if (!all
&& rerun
) {
8740 * Bump device hand to the device start if it is approaching the
8741 * end. l2arc_evict() has already evicted ahead for this case.
8743 dev
->l2ad_hand
= dev
->l2ad_start
;
8744 dev
->l2ad_evict
= dev
->l2ad_start
;
8745 dev
->l2ad_first
= B_FALSE
;
8751 * In case of cache device removal (all) the following
8752 * assertions may be violated without functional consequences
8753 * as the device is about to be removed.
8755 ASSERT3U(dev
->l2ad_hand
+ distance
, <, dev
->l2ad_end
);
8756 if (!dev
->l2ad_first
)
8757 ASSERT3U(dev
->l2ad_hand
, <, dev
->l2ad_evict
);
8762 * Handle any abd transforms that might be required for writing to the L2ARC.
8763 * If successful, this function will always return an abd with the data
8764 * transformed as it is on disk in a new abd of asize bytes.
8767 l2arc_apply_transforms(spa_t
*spa
, arc_buf_hdr_t
*hdr
, uint64_t asize
,
8772 abd_t
*cabd
= NULL
, *eabd
= NULL
, *to_write
= hdr
->b_l1hdr
.b_pabd
;
8773 enum zio_compress compress
= HDR_GET_COMPRESS(hdr
);
8774 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8775 uint64_t size
= arc_hdr_size(hdr
);
8776 boolean_t ismd
= HDR_ISTYPE_METADATA(hdr
);
8777 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
8778 dsl_crypto_key_t
*dck
= NULL
;
8779 uint8_t mac
[ZIO_DATA_MAC_LEN
] = { 0 };
8780 boolean_t no_crypt
= B_FALSE
;
8782 ASSERT((HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
8783 !HDR_COMPRESSION_ENABLED(hdr
)) ||
8784 HDR_ENCRYPTED(hdr
) || HDR_SHARED_DATA(hdr
) || psize
!= asize
);
8785 ASSERT3U(psize
, <=, asize
);
8788 * If this data simply needs its own buffer, we simply allocate it
8789 * and copy the data. This may be done to eliminate a dependency on a
8790 * shared buffer or to reallocate the buffer to match asize.
8792 if (HDR_HAS_RABD(hdr
) && asize
!= psize
) {
8793 ASSERT3U(asize
, >=, psize
);
8794 to_write
= abd_alloc_for_io(asize
, ismd
);
8795 abd_copy(to_write
, hdr
->b_crypt_hdr
.b_rabd
, psize
);
8797 abd_zero_off(to_write
, psize
, asize
- psize
);
8801 if ((compress
== ZIO_COMPRESS_OFF
|| HDR_COMPRESSION_ENABLED(hdr
)) &&
8802 !HDR_ENCRYPTED(hdr
)) {
8803 ASSERT3U(size
, ==, psize
);
8804 to_write
= abd_alloc_for_io(asize
, ismd
);
8805 abd_copy(to_write
, hdr
->b_l1hdr
.b_pabd
, size
);
8807 abd_zero_off(to_write
, size
, asize
- size
);
8811 if (compress
!= ZIO_COMPRESS_OFF
&& !HDR_COMPRESSION_ENABLED(hdr
)) {
8812 cabd
= abd_alloc_for_io(asize
, ismd
);
8813 tmp
= abd_borrow_buf(cabd
, asize
);
8815 psize
= zio_compress_data(compress
, to_write
, tmp
, size
,
8818 if (psize
>= size
) {
8819 abd_return_buf(cabd
, tmp
, asize
);
8820 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_OFF
);
8822 abd_copy(to_write
, hdr
->b_l1hdr
.b_pabd
, size
);
8824 abd_zero_off(to_write
, size
, asize
- size
);
8827 ASSERT3U(psize
, <=, HDR_GET_PSIZE(hdr
));
8829 bzero((char *)tmp
+ psize
, asize
- psize
);
8830 psize
= HDR_GET_PSIZE(hdr
);
8831 abd_return_buf_copy(cabd
, tmp
, asize
);
8836 if (HDR_ENCRYPTED(hdr
)) {
8837 eabd
= abd_alloc_for_io(asize
, ismd
);
8840 * If the dataset was disowned before the buffer
8841 * made it to this point, the key to re-encrypt
8842 * it won't be available. In this case we simply
8843 * won't write the buffer to the L2ARC.
8845 ret
= spa_keystore_lookup_key(spa
, hdr
->b_crypt_hdr
.b_dsobj
,
8850 ret
= zio_do_crypt_abd(B_TRUE
, &dck
->dck_key
,
8851 hdr
->b_crypt_hdr
.b_ot
, bswap
, hdr
->b_crypt_hdr
.b_salt
,
8852 hdr
->b_crypt_hdr
.b_iv
, mac
, psize
, to_write
, eabd
,
8858 abd_copy(eabd
, to_write
, psize
);
8861 abd_zero_off(eabd
, psize
, asize
- psize
);
8863 /* assert that the MAC we got here matches the one we saved */
8864 ASSERT0(bcmp(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
));
8865 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
8867 if (to_write
== cabd
)
8874 ASSERT3P(to_write
, !=, hdr
->b_l1hdr
.b_pabd
);
8875 *abd_out
= to_write
;
8880 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
8891 l2arc_blk_fetch_done(zio_t
*zio
)
8893 l2arc_read_callback_t
*cb
;
8895 cb
= zio
->io_private
;
8896 if (cb
->l2rcb_abd
!= NULL
)
8897 abd_put(cb
->l2rcb_abd
);
8898 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
8902 * Find and write ARC buffers to the L2ARC device.
8904 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
8905 * for reading until they have completed writing.
8906 * The headroom_boost is an in-out parameter used to maintain headroom boost
8907 * state between calls to this function.
8909 * Returns the number of bytes actually written (which may be smaller than
8910 * the delta by which the device hand has changed due to alignment and the
8911 * writing of log blocks).
8914 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
)
8916 arc_buf_hdr_t
*hdr
, *hdr_prev
, *head
;
8917 uint64_t write_asize
, write_psize
, write_lsize
, headroom
;
8919 l2arc_write_callback_t
*cb
= NULL
;
8921 uint64_t guid
= spa_load_guid(spa
);
8923 ASSERT3P(dev
->l2ad_vdev
, !=, NULL
);
8926 write_lsize
= write_asize
= write_psize
= 0;
8928 head
= kmem_cache_alloc(hdr_l2only_cache
, KM_PUSHPAGE
);
8929 arc_hdr_set_flags(head
, ARC_FLAG_L2_WRITE_HEAD
| ARC_FLAG_HAS_L2HDR
);
8932 * Copy buffers for L2ARC writing.
8934 for (int try = 0; try < L2ARC_FEED_TYPES
; try++) {
8936 * If try == 1 or 3, we cache MRU metadata and data
8939 if (l2arc_mfuonly
) {
8940 if (try == 1 || try == 3)
8944 multilist_sublist_t
*mls
= l2arc_sublist_lock(try);
8945 uint64_t passed_sz
= 0;
8947 VERIFY3P(mls
, !=, NULL
);
8950 * L2ARC fast warmup.
8952 * Until the ARC is warm and starts to evict, read from the
8953 * head of the ARC lists rather than the tail.
8955 if (arc_warm
== B_FALSE
)
8956 hdr
= multilist_sublist_head(mls
);
8958 hdr
= multilist_sublist_tail(mls
);
8960 headroom
= target_sz
* l2arc_headroom
;
8961 if (zfs_compressed_arc_enabled
)
8962 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
8964 for (; hdr
; hdr
= hdr_prev
) {
8965 kmutex_t
*hash_lock
;
8966 abd_t
*to_write
= NULL
;
8968 if (arc_warm
== B_FALSE
)
8969 hdr_prev
= multilist_sublist_next(mls
, hdr
);
8971 hdr_prev
= multilist_sublist_prev(mls
, hdr
);
8973 hash_lock
= HDR_LOCK(hdr
);
8974 if (!mutex_tryenter(hash_lock
)) {
8976 * Skip this buffer rather than waiting.
8981 passed_sz
+= HDR_GET_LSIZE(hdr
);
8982 if (l2arc_headroom
!= 0 && passed_sz
> headroom
) {
8986 mutex_exit(hash_lock
);
8990 if (!l2arc_write_eligible(guid
, hdr
)) {
8991 mutex_exit(hash_lock
);
8996 * We rely on the L1 portion of the header below, so
8997 * it's invalid for this header to have been evicted out
8998 * of the ghost cache, prior to being written out. The
8999 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
9001 ASSERT(HDR_HAS_L1HDR(hdr
));
9003 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
9004 ASSERT3U(arc_hdr_size(hdr
), >, 0);
9005 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
9007 uint64_t psize
= HDR_GET_PSIZE(hdr
);
9008 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
,
9011 if ((write_asize
+ asize
) > target_sz
) {
9013 mutex_exit(hash_lock
);
9018 * We rely on the L1 portion of the header below, so
9019 * it's invalid for this header to have been evicted out
9020 * of the ghost cache, prior to being written out. The
9021 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
9023 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_WRITING
);
9024 ASSERT(HDR_HAS_L1HDR(hdr
));
9026 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
9027 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
9029 ASSERT3U(arc_hdr_size(hdr
), >, 0);
9032 * If this header has b_rabd, we can use this since it
9033 * must always match the data exactly as it exists on
9034 * disk. Otherwise, the L2ARC can normally use the
9035 * hdr's data, but if we're sharing data between the
9036 * hdr and one of its bufs, L2ARC needs its own copy of
9037 * the data so that the ZIO below can't race with the
9038 * buf consumer. To ensure that this copy will be
9039 * available for the lifetime of the ZIO and be cleaned
9040 * up afterwards, we add it to the l2arc_free_on_write
9041 * queue. If we need to apply any transforms to the
9042 * data (compression, encryption) we will also need the
9045 if (HDR_HAS_RABD(hdr
) && psize
== asize
) {
9046 to_write
= hdr
->b_crypt_hdr
.b_rabd
;
9047 } else if ((HDR_COMPRESSION_ENABLED(hdr
) ||
9048 HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
) &&
9049 !HDR_ENCRYPTED(hdr
) && !HDR_SHARED_DATA(hdr
) &&
9051 to_write
= hdr
->b_l1hdr
.b_pabd
;
9054 arc_buf_contents_t type
= arc_buf_type(hdr
);
9056 ret
= l2arc_apply_transforms(spa
, hdr
, asize
,
9059 arc_hdr_clear_flags(hdr
,
9060 ARC_FLAG_L2_WRITING
);
9061 mutex_exit(hash_lock
);
9065 l2arc_free_abd_on_write(to_write
, asize
, type
);
9070 * Insert a dummy header on the buflist so
9071 * l2arc_write_done() can find where the
9072 * write buffers begin without searching.
9074 mutex_enter(&dev
->l2ad_mtx
);
9075 list_insert_head(&dev
->l2ad_buflist
, head
);
9076 mutex_exit(&dev
->l2ad_mtx
);
9079 sizeof (l2arc_write_callback_t
), KM_SLEEP
);
9080 cb
->l2wcb_dev
= dev
;
9081 cb
->l2wcb_head
= head
;
9083 * Create a list to save allocated abd buffers
9084 * for l2arc_log_blk_commit().
9086 list_create(&cb
->l2wcb_abd_list
,
9087 sizeof (l2arc_lb_abd_buf_t
),
9088 offsetof(l2arc_lb_abd_buf_t
, node
));
9089 pio
= zio_root(spa
, l2arc_write_done
, cb
,
9093 hdr
->b_l2hdr
.b_dev
= dev
;
9094 hdr
->b_l2hdr
.b_hits
= 0;
9096 hdr
->b_l2hdr
.b_daddr
= dev
->l2ad_hand
;
9097 arc_hdr_set_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
9099 mutex_enter(&dev
->l2ad_mtx
);
9100 list_insert_head(&dev
->l2ad_buflist
, hdr
);
9101 mutex_exit(&dev
->l2ad_mtx
);
9103 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
9104 arc_hdr_size(hdr
), hdr
);
9106 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
9107 hdr
->b_l2hdr
.b_daddr
, asize
, to_write
,
9108 ZIO_CHECKSUM_OFF
, NULL
, hdr
,
9109 ZIO_PRIORITY_ASYNC_WRITE
,
9110 ZIO_FLAG_CANFAIL
, B_FALSE
);
9112 write_lsize
+= HDR_GET_LSIZE(hdr
);
9113 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
9116 write_psize
+= psize
;
9117 write_asize
+= asize
;
9118 dev
->l2ad_hand
+= asize
;
9119 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
9121 mutex_exit(hash_lock
);
9124 * Append buf info to current log and commit if full.
9125 * arcstat_l2_{size,asize} kstats are updated
9128 if (l2arc_log_blk_insert(dev
, hdr
))
9129 l2arc_log_blk_commit(dev
, pio
, cb
);
9134 multilist_sublist_unlock(mls
);
9140 /* No buffers selected for writing? */
9142 ASSERT0(write_lsize
);
9143 ASSERT(!HDR_HAS_L1HDR(head
));
9144 kmem_cache_free(hdr_l2only_cache
, head
);
9147 * Although we did not write any buffers l2ad_evict may
9150 l2arc_dev_hdr_update(dev
);
9155 if (!dev
->l2ad_first
)
9156 ASSERT3U(dev
->l2ad_hand
, <=, dev
->l2ad_evict
);
9158 ASSERT3U(write_asize
, <=, target_sz
);
9159 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
9160 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_psize
);
9161 ARCSTAT_INCR(arcstat_l2_lsize
, write_lsize
);
9162 ARCSTAT_INCR(arcstat_l2_psize
, write_psize
);
9164 dev
->l2ad_writing
= B_TRUE
;
9165 (void) zio_wait(pio
);
9166 dev
->l2ad_writing
= B_FALSE
;
9169 * Update the device header after the zio completes as
9170 * l2arc_write_done() may have updated the memory holding the log block
9171 * pointers in the device header.
9173 l2arc_dev_hdr_update(dev
);
9175 return (write_asize
);
9179 l2arc_hdr_limit_reached(void)
9181 int64_t s
= aggsum_upper_bound(&astat_l2_hdr_size
);
9183 return (arc_reclaim_needed() || (s
> arc_meta_limit
* 3 / 4) ||
9184 (s
> (arc_warm
? arc_c
: arc_c_max
) * l2arc_meta_percent
/ 100));
9188 * This thread feeds the L2ARC at regular intervals. This is the beating
9189 * heart of the L2ARC.
9193 l2arc_feed_thread(void *unused
)
9198 uint64_t size
, wrote
;
9199 clock_t begin
, next
= ddi_get_lbolt();
9200 fstrans_cookie_t cookie
;
9202 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
9204 mutex_enter(&l2arc_feed_thr_lock
);
9206 cookie
= spl_fstrans_mark();
9207 while (l2arc_thread_exit
== 0) {
9208 CALLB_CPR_SAFE_BEGIN(&cpr
);
9209 (void) cv_timedwait_idle(&l2arc_feed_thr_cv
,
9210 &l2arc_feed_thr_lock
, next
);
9211 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
9212 next
= ddi_get_lbolt() + hz
;
9215 * Quick check for L2ARC devices.
9217 mutex_enter(&l2arc_dev_mtx
);
9218 if (l2arc_ndev
== 0) {
9219 mutex_exit(&l2arc_dev_mtx
);
9222 mutex_exit(&l2arc_dev_mtx
);
9223 begin
= ddi_get_lbolt();
9226 * This selects the next l2arc device to write to, and in
9227 * doing so the next spa to feed from: dev->l2ad_spa. This
9228 * will return NULL if there are now no l2arc devices or if
9229 * they are all faulted.
9231 * If a device is returned, its spa's config lock is also
9232 * held to prevent device removal. l2arc_dev_get_next()
9233 * will grab and release l2arc_dev_mtx.
9235 if ((dev
= l2arc_dev_get_next()) == NULL
)
9238 spa
= dev
->l2ad_spa
;
9239 ASSERT3P(spa
, !=, NULL
);
9242 * If the pool is read-only then force the feed thread to
9243 * sleep a little longer.
9245 if (!spa_writeable(spa
)) {
9246 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
9247 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9252 * Avoid contributing to memory pressure.
9254 if (l2arc_hdr_limit_reached()) {
9255 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
9256 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9260 ARCSTAT_BUMP(arcstat_l2_feeds
);
9262 size
= l2arc_write_size(dev
);
9265 * Evict L2ARC buffers that will be overwritten.
9267 l2arc_evict(dev
, size
, B_FALSE
);
9270 * Write ARC buffers.
9272 wrote
= l2arc_write_buffers(spa
, dev
, size
);
9275 * Calculate interval between writes.
9277 next
= l2arc_write_interval(begin
, size
, wrote
);
9278 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9280 spl_fstrans_unmark(cookie
);
9282 l2arc_thread_exit
= 0;
9283 cv_broadcast(&l2arc_feed_thr_cv
);
9284 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
9289 l2arc_vdev_present(vdev_t
*vd
)
9291 return (l2arc_vdev_get(vd
) != NULL
);
9295 * Returns the l2arc_dev_t associated with a particular vdev_t or NULL if
9296 * the vdev_t isn't an L2ARC device.
9299 l2arc_vdev_get(vdev_t
*vd
)
9303 mutex_enter(&l2arc_dev_mtx
);
9304 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
9305 dev
= list_next(l2arc_dev_list
, dev
)) {
9306 if (dev
->l2ad_vdev
== vd
)
9309 mutex_exit(&l2arc_dev_mtx
);
9315 * Add a vdev for use by the L2ARC. By this point the spa has already
9316 * validated the vdev and opened it.
9319 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
9321 l2arc_dev_t
*adddev
;
9322 uint64_t l2dhdr_asize
;
9324 ASSERT(!l2arc_vdev_present(vd
));
9327 * Create a new l2arc device entry.
9329 adddev
= vmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
9330 adddev
->l2ad_spa
= spa
;
9331 adddev
->l2ad_vdev
= vd
;
9332 /* leave extra size for an l2arc device header */
9333 l2dhdr_asize
= adddev
->l2ad_dev_hdr_asize
=
9334 MAX(sizeof (*adddev
->l2ad_dev_hdr
), 1 << vd
->vdev_ashift
);
9335 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
+ l2dhdr_asize
;
9336 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
9337 ASSERT3U(adddev
->l2ad_start
, <, adddev
->l2ad_end
);
9338 adddev
->l2ad_hand
= adddev
->l2ad_start
;
9339 adddev
->l2ad_evict
= adddev
->l2ad_start
;
9340 adddev
->l2ad_first
= B_TRUE
;
9341 adddev
->l2ad_writing
= B_FALSE
;
9342 adddev
->l2ad_trim_all
= B_FALSE
;
9343 list_link_init(&adddev
->l2ad_node
);
9344 adddev
->l2ad_dev_hdr
= kmem_zalloc(l2dhdr_asize
, KM_SLEEP
);
9346 mutex_init(&adddev
->l2ad_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9348 * This is a list of all ARC buffers that are still valid on the
9351 list_create(&adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
9352 offsetof(arc_buf_hdr_t
, b_l2hdr
.b_l2node
));
9355 * This is a list of pointers to log blocks that are still present
9358 list_create(&adddev
->l2ad_lbptr_list
, sizeof (l2arc_lb_ptr_buf_t
),
9359 offsetof(l2arc_lb_ptr_buf_t
, node
));
9361 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
9362 zfs_refcount_create(&adddev
->l2ad_alloc
);
9363 zfs_refcount_create(&adddev
->l2ad_lb_asize
);
9364 zfs_refcount_create(&adddev
->l2ad_lb_count
);
9367 * Add device to global list
9369 mutex_enter(&l2arc_dev_mtx
);
9370 list_insert_head(l2arc_dev_list
, adddev
);
9371 atomic_inc_64(&l2arc_ndev
);
9372 mutex_exit(&l2arc_dev_mtx
);
9375 * Decide if vdev is eligible for L2ARC rebuild
9377 l2arc_rebuild_vdev(adddev
->l2ad_vdev
, B_FALSE
);
9381 l2arc_rebuild_vdev(vdev_t
*vd
, boolean_t reopen
)
9383 l2arc_dev_t
*dev
= NULL
;
9384 l2arc_dev_hdr_phys_t
*l2dhdr
;
9385 uint64_t l2dhdr_asize
;
9388 boolean_t l2dhdr_valid
= B_TRUE
;
9390 dev
= l2arc_vdev_get(vd
);
9391 ASSERT3P(dev
, !=, NULL
);
9392 spa
= dev
->l2ad_spa
;
9393 l2dhdr
= dev
->l2ad_dev_hdr
;
9394 l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
9397 * The L2ARC has to hold at least the payload of one log block for
9398 * them to be restored (persistent L2ARC). The payload of a log block
9399 * depends on the amount of its log entries. We always write log blocks
9400 * with 1022 entries. How many of them are committed or restored depends
9401 * on the size of the L2ARC device. Thus the maximum payload of
9402 * one log block is 1022 * SPA_MAXBLOCKSIZE = 16GB. If the L2ARC device
9403 * is less than that, we reduce the amount of committed and restored
9404 * log entries per block so as to enable persistence.
9406 if (dev
->l2ad_end
< l2arc_rebuild_blocks_min_l2size
) {
9407 dev
->l2ad_log_entries
= 0;
9409 dev
->l2ad_log_entries
= MIN((dev
->l2ad_end
-
9410 dev
->l2ad_start
) >> SPA_MAXBLOCKSHIFT
,
9411 L2ARC_LOG_BLK_MAX_ENTRIES
);
9415 * Read the device header, if an error is returned do not rebuild L2ARC.
9417 if ((err
= l2arc_dev_hdr_read(dev
)) != 0)
9418 l2dhdr_valid
= B_FALSE
;
9420 if (l2dhdr_valid
&& dev
->l2ad_log_entries
> 0) {
9422 * If we are onlining a cache device (vdev_reopen) that was
9423 * still present (l2arc_vdev_present()) and rebuild is enabled,
9424 * we should evict all ARC buffers and pointers to log blocks
9425 * and reclaim their space before restoring its contents to
9429 if (!l2arc_rebuild_enabled
) {
9432 l2arc_evict(dev
, 0, B_TRUE
);
9433 /* start a new log block */
9434 dev
->l2ad_log_ent_idx
= 0;
9435 dev
->l2ad_log_blk_payload_asize
= 0;
9436 dev
->l2ad_log_blk_payload_start
= 0;
9440 * Just mark the device as pending for a rebuild. We won't
9441 * be starting a rebuild in line here as it would block pool
9442 * import. Instead spa_load_impl will hand that off to an
9443 * async task which will call l2arc_spa_rebuild_start.
9445 dev
->l2ad_rebuild
= B_TRUE
;
9446 } else if (spa_writeable(spa
)) {
9448 * In this case TRIM the whole device if l2arc_trim_ahead > 0,
9449 * otherwise create a new header. We zero out the memory holding
9450 * the header to reset dh_start_lbps. If we TRIM the whole
9451 * device the new header will be written by
9452 * vdev_trim_l2arc_thread() at the end of the TRIM to update the
9453 * trim_state in the header too. When reading the header, if
9454 * trim_state is not VDEV_TRIM_COMPLETE and l2arc_trim_ahead > 0
9455 * we opt to TRIM the whole device again.
9457 if (l2arc_trim_ahead
> 0) {
9458 dev
->l2ad_trim_all
= B_TRUE
;
9460 bzero(l2dhdr
, l2dhdr_asize
);
9461 l2arc_dev_hdr_update(dev
);
9467 * Remove a vdev from the L2ARC.
9470 l2arc_remove_vdev(vdev_t
*vd
)
9472 l2arc_dev_t
*remdev
= NULL
;
9475 * Find the device by vdev
9477 remdev
= l2arc_vdev_get(vd
);
9478 ASSERT3P(remdev
, !=, NULL
);
9481 * Cancel any ongoing or scheduled rebuild.
9483 mutex_enter(&l2arc_rebuild_thr_lock
);
9484 if (remdev
->l2ad_rebuild_began
== B_TRUE
) {
9485 remdev
->l2ad_rebuild_cancel
= B_TRUE
;
9486 while (remdev
->l2ad_rebuild
== B_TRUE
)
9487 cv_wait(&l2arc_rebuild_thr_cv
, &l2arc_rebuild_thr_lock
);
9489 mutex_exit(&l2arc_rebuild_thr_lock
);
9492 * Remove device from global list
9494 mutex_enter(&l2arc_dev_mtx
);
9495 list_remove(l2arc_dev_list
, remdev
);
9496 l2arc_dev_last
= NULL
; /* may have been invalidated */
9497 atomic_dec_64(&l2arc_ndev
);
9498 mutex_exit(&l2arc_dev_mtx
);
9501 * Clear all buflists and ARC references. L2ARC device flush.
9503 l2arc_evict(remdev
, 0, B_TRUE
);
9504 list_destroy(&remdev
->l2ad_buflist
);
9505 ASSERT(list_is_empty(&remdev
->l2ad_lbptr_list
));
9506 list_destroy(&remdev
->l2ad_lbptr_list
);
9507 mutex_destroy(&remdev
->l2ad_mtx
);
9508 zfs_refcount_destroy(&remdev
->l2ad_alloc
);
9509 zfs_refcount_destroy(&remdev
->l2ad_lb_asize
);
9510 zfs_refcount_destroy(&remdev
->l2ad_lb_count
);
9511 kmem_free(remdev
->l2ad_dev_hdr
, remdev
->l2ad_dev_hdr_asize
);
9512 vmem_free(remdev
, sizeof (l2arc_dev_t
));
9518 l2arc_thread_exit
= 0;
9520 l2arc_writes_sent
= 0;
9521 l2arc_writes_done
= 0;
9523 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
9524 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
9525 mutex_init(&l2arc_rebuild_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
9526 cv_init(&l2arc_rebuild_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
9527 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9528 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9530 l2arc_dev_list
= &L2ARC_dev_list
;
9531 l2arc_free_on_write
= &L2ARC_free_on_write
;
9532 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
9533 offsetof(l2arc_dev_t
, l2ad_node
));
9534 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
9535 offsetof(l2arc_data_free_t
, l2df_list_node
));
9541 mutex_destroy(&l2arc_feed_thr_lock
);
9542 cv_destroy(&l2arc_feed_thr_cv
);
9543 mutex_destroy(&l2arc_rebuild_thr_lock
);
9544 cv_destroy(&l2arc_rebuild_thr_cv
);
9545 mutex_destroy(&l2arc_dev_mtx
);
9546 mutex_destroy(&l2arc_free_on_write_mtx
);
9548 list_destroy(l2arc_dev_list
);
9549 list_destroy(l2arc_free_on_write
);
9555 if (!(spa_mode_global
& SPA_MODE_WRITE
))
9558 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
9559 TS_RUN
, defclsyspri
);
9565 if (!(spa_mode_global
& SPA_MODE_WRITE
))
9568 mutex_enter(&l2arc_feed_thr_lock
);
9569 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
9570 l2arc_thread_exit
= 1;
9571 while (l2arc_thread_exit
!= 0)
9572 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
9573 mutex_exit(&l2arc_feed_thr_lock
);
9577 * Punches out rebuild threads for the L2ARC devices in a spa. This should
9578 * be called after pool import from the spa async thread, since starting
9579 * these threads directly from spa_import() will make them part of the
9580 * "zpool import" context and delay process exit (and thus pool import).
9583 l2arc_spa_rebuild_start(spa_t
*spa
)
9585 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
9588 * Locate the spa's l2arc devices and kick off rebuild threads.
9590 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
9592 l2arc_vdev_get(spa
->spa_l2cache
.sav_vdevs
[i
]);
9594 /* Don't attempt a rebuild if the vdev is UNAVAIL */
9597 mutex_enter(&l2arc_rebuild_thr_lock
);
9598 if (dev
->l2ad_rebuild
&& !dev
->l2ad_rebuild_cancel
) {
9599 dev
->l2ad_rebuild_began
= B_TRUE
;
9600 (void) thread_create(NULL
, 0, l2arc_dev_rebuild_thread
,
9601 dev
, 0, &p0
, TS_RUN
, minclsyspri
);
9603 mutex_exit(&l2arc_rebuild_thr_lock
);
9608 * Main entry point for L2ARC rebuilding.
9611 l2arc_dev_rebuild_thread(void *arg
)
9613 l2arc_dev_t
*dev
= arg
;
9615 VERIFY(!dev
->l2ad_rebuild_cancel
);
9616 VERIFY(dev
->l2ad_rebuild
);
9617 (void) l2arc_rebuild(dev
);
9618 mutex_enter(&l2arc_rebuild_thr_lock
);
9619 dev
->l2ad_rebuild_began
= B_FALSE
;
9620 dev
->l2ad_rebuild
= B_FALSE
;
9621 mutex_exit(&l2arc_rebuild_thr_lock
);
9627 * This function implements the actual L2ARC metadata rebuild. It:
9628 * starts reading the log block chain and restores each block's contents
9629 * to memory (reconstructing arc_buf_hdr_t's).
9631 * Operation stops under any of the following conditions:
9633 * 1) We reach the end of the log block chain.
9634 * 2) We encounter *any* error condition (cksum errors, io errors)
9637 l2arc_rebuild(l2arc_dev_t
*dev
)
9639 vdev_t
*vd
= dev
->l2ad_vdev
;
9640 spa_t
*spa
= vd
->vdev_spa
;
9642 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
9643 l2arc_log_blk_phys_t
*this_lb
, *next_lb
;
9644 zio_t
*this_io
= NULL
, *next_io
= NULL
;
9645 l2arc_log_blkptr_t lbps
[2];
9646 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
9647 boolean_t lock_held
;
9649 this_lb
= vmem_zalloc(sizeof (*this_lb
), KM_SLEEP
);
9650 next_lb
= vmem_zalloc(sizeof (*next_lb
), KM_SLEEP
);
9653 * We prevent device removal while issuing reads to the device,
9654 * then during the rebuilding phases we drop this lock again so
9655 * that a spa_unload or device remove can be initiated - this is
9656 * safe, because the spa will signal us to stop before removing
9657 * our device and wait for us to stop.
9659 spa_config_enter(spa
, SCL_L2ARC
, vd
, RW_READER
);
9663 * Retrieve the persistent L2ARC device state.
9664 * L2BLK_GET_PSIZE returns aligned size for log blocks.
9666 dev
->l2ad_evict
= MAX(l2dhdr
->dh_evict
, dev
->l2ad_start
);
9667 dev
->l2ad_hand
= MAX(l2dhdr
->dh_start_lbps
[0].lbp_daddr
+
9668 L2BLK_GET_PSIZE((&l2dhdr
->dh_start_lbps
[0])->lbp_prop
),
9670 dev
->l2ad_first
= !!(l2dhdr
->dh_flags
& L2ARC_DEV_HDR_EVICT_FIRST
);
9672 vd
->vdev_trim_action_time
= l2dhdr
->dh_trim_action_time
;
9673 vd
->vdev_trim_state
= l2dhdr
->dh_trim_state
;
9676 * In case the zfs module parameter l2arc_rebuild_enabled is false
9677 * we do not start the rebuild process.
9679 if (!l2arc_rebuild_enabled
)
9682 /* Prepare the rebuild process */
9683 bcopy(l2dhdr
->dh_start_lbps
, lbps
, sizeof (lbps
));
9685 /* Start the rebuild process */
9687 if (!l2arc_log_blkptr_valid(dev
, &lbps
[0]))
9690 if ((err
= l2arc_log_blk_read(dev
, &lbps
[0], &lbps
[1],
9691 this_lb
, next_lb
, this_io
, &next_io
)) != 0)
9695 * Our memory pressure valve. If the system is running low
9696 * on memory, rather than swamping memory with new ARC buf
9697 * hdrs, we opt not to rebuild the L2ARC. At this point,
9698 * however, we have already set up our L2ARC dev to chain in
9699 * new metadata log blocks, so the user may choose to offline/
9700 * online the L2ARC dev at a later time (or re-import the pool)
9701 * to reconstruct it (when there's less memory pressure).
9703 if (l2arc_hdr_limit_reached()) {
9704 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_lowmem
);
9705 cmn_err(CE_NOTE
, "System running low on memory, "
9706 "aborting L2ARC rebuild.");
9707 err
= SET_ERROR(ENOMEM
);
9711 spa_config_exit(spa
, SCL_L2ARC
, vd
);
9712 lock_held
= B_FALSE
;
9715 * Now that we know that the next_lb checks out alright, we
9716 * can start reconstruction from this log block.
9717 * L2BLK_GET_PSIZE returns aligned size for log blocks.
9719 uint64_t asize
= L2BLK_GET_PSIZE((&lbps
[0])->lbp_prop
);
9720 l2arc_log_blk_restore(dev
, this_lb
, asize
, lbps
[0].lbp_daddr
);
9723 * log block restored, include its pointer in the list of
9724 * pointers to log blocks present in the L2ARC device.
9726 lb_ptr_buf
= kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t
), KM_SLEEP
);
9727 lb_ptr_buf
->lb_ptr
= kmem_zalloc(sizeof (l2arc_log_blkptr_t
),
9729 bcopy(&lbps
[0], lb_ptr_buf
->lb_ptr
,
9730 sizeof (l2arc_log_blkptr_t
));
9731 mutex_enter(&dev
->l2ad_mtx
);
9732 list_insert_tail(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
9733 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, asize
);
9734 ARCSTAT_BUMP(arcstat_l2_log_blk_count
);
9735 zfs_refcount_add_many(&dev
->l2ad_lb_asize
, asize
, lb_ptr_buf
);
9736 zfs_refcount_add(&dev
->l2ad_lb_count
, lb_ptr_buf
);
9737 mutex_exit(&dev
->l2ad_mtx
);
9738 vdev_space_update(vd
, asize
, 0, 0);
9741 * Protection against loops of log blocks:
9743 * l2ad_hand l2ad_evict
9745 * l2ad_start |=======================================| l2ad_end
9746 * -----|||----|||---|||----|||
9748 * ---|||---|||----|||---|||
9751 * In this situation the pointer of log block (4) passes
9752 * l2arc_log_blkptr_valid() but the log block should not be
9753 * restored as it is overwritten by the payload of log block
9754 * (0). Only log blocks (0)-(3) should be restored. We check
9755 * whether l2ad_evict lies in between the payload starting
9756 * offset of the next log block (lbps[1].lbp_payload_start)
9757 * and the payload starting offset of the present log block
9758 * (lbps[0].lbp_payload_start). If true and this isn't the
9759 * first pass, we are looping from the beginning and we should
9762 if (l2arc_range_check_overlap(lbps
[1].lbp_payload_start
,
9763 lbps
[0].lbp_payload_start
, dev
->l2ad_evict
) &&
9769 mutex_enter(&l2arc_rebuild_thr_lock
);
9770 if (dev
->l2ad_rebuild_cancel
) {
9771 dev
->l2ad_rebuild
= B_FALSE
;
9772 cv_signal(&l2arc_rebuild_thr_cv
);
9773 mutex_exit(&l2arc_rebuild_thr_lock
);
9774 err
= SET_ERROR(ECANCELED
);
9777 mutex_exit(&l2arc_rebuild_thr_lock
);
9778 if (spa_config_tryenter(spa
, SCL_L2ARC
, vd
,
9784 * L2ARC config lock held by somebody in writer,
9785 * possibly due to them trying to remove us. They'll
9786 * likely to want us to shut down, so after a little
9787 * delay, we check l2ad_rebuild_cancel and retry
9794 * Continue with the next log block.
9797 lbps
[1] = this_lb
->lb_prev_lbp
;
9798 PTR_SWAP(this_lb
, next_lb
);
9803 if (this_io
!= NULL
)
9804 l2arc_log_blk_fetch_abort(this_io
);
9806 if (next_io
!= NULL
)
9807 l2arc_log_blk_fetch_abort(next_io
);
9808 vmem_free(this_lb
, sizeof (*this_lb
));
9809 vmem_free(next_lb
, sizeof (*next_lb
));
9811 if (!l2arc_rebuild_enabled
) {
9812 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
9814 } else if (err
== 0 && zfs_refcount_count(&dev
->l2ad_lb_count
) > 0) {
9815 ARCSTAT_BUMP(arcstat_l2_rebuild_success
);
9816 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
9817 "successful, restored %llu blocks",
9818 (u_longlong_t
)zfs_refcount_count(&dev
->l2ad_lb_count
));
9819 } else if (err
== 0 && zfs_refcount_count(&dev
->l2ad_lb_count
) == 0) {
9821 * No error but also nothing restored, meaning the lbps array
9822 * in the device header points to invalid/non-present log
9823 * blocks. Reset the header.
9825 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
9826 "no valid log blocks");
9827 bzero(l2dhdr
, dev
->l2ad_dev_hdr_asize
);
9828 l2arc_dev_hdr_update(dev
);
9829 } else if (err
== ECANCELED
) {
9831 * In case the rebuild was canceled do not log to spa history
9832 * log as the pool may be in the process of being removed.
9834 zfs_dbgmsg("L2ARC rebuild aborted, restored %llu blocks",
9835 zfs_refcount_count(&dev
->l2ad_lb_count
));
9836 } else if (err
!= 0) {
9837 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
9838 "aborted, restored %llu blocks",
9839 (u_longlong_t
)zfs_refcount_count(&dev
->l2ad_lb_count
));
9843 spa_config_exit(spa
, SCL_L2ARC
, vd
);
9849 * Attempts to read the device header on the provided L2ARC device and writes
9850 * it to `hdr'. On success, this function returns 0, otherwise the appropriate
9851 * error code is returned.
9854 l2arc_dev_hdr_read(l2arc_dev_t
*dev
)
9858 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
9859 const uint64_t l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
9862 guid
= spa_guid(dev
->l2ad_vdev
->vdev_spa
);
9864 abd
= abd_get_from_buf(l2dhdr
, l2dhdr_asize
);
9866 err
= zio_wait(zio_read_phys(NULL
, dev
->l2ad_vdev
,
9867 VDEV_LABEL_START_SIZE
, l2dhdr_asize
, abd
,
9868 ZIO_CHECKSUM_LABEL
, NULL
, NULL
, ZIO_PRIORITY_ASYNC_READ
,
9869 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_CANFAIL
|
9870 ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
|
9871 ZIO_FLAG_SPECULATIVE
, B_FALSE
));
9876 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_dh_errors
);
9877 zfs_dbgmsg("L2ARC IO error (%d) while reading device header, "
9878 "vdev guid: %llu", err
, dev
->l2ad_vdev
->vdev_guid
);
9882 if (l2dhdr
->dh_magic
== BSWAP_64(L2ARC_DEV_HDR_MAGIC
))
9883 byteswap_uint64_array(l2dhdr
, sizeof (*l2dhdr
));
9885 if (l2dhdr
->dh_magic
!= L2ARC_DEV_HDR_MAGIC
||
9886 l2dhdr
->dh_spa_guid
!= guid
||
9887 l2dhdr
->dh_vdev_guid
!= dev
->l2ad_vdev
->vdev_guid
||
9888 l2dhdr
->dh_version
!= L2ARC_PERSISTENT_VERSION
||
9889 l2dhdr
->dh_log_entries
!= dev
->l2ad_log_entries
||
9890 l2dhdr
->dh_end
!= dev
->l2ad_end
||
9891 !l2arc_range_check_overlap(dev
->l2ad_start
, dev
->l2ad_end
,
9892 l2dhdr
->dh_evict
) ||
9893 (l2dhdr
->dh_trim_state
!= VDEV_TRIM_COMPLETE
&&
9894 l2arc_trim_ahead
> 0)) {
9896 * Attempt to rebuild a device containing no actual dev hdr
9897 * or containing a header from some other pool or from another
9898 * version of persistent L2ARC.
9900 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_unsupported
);
9901 return (SET_ERROR(ENOTSUP
));
9908 * Reads L2ARC log blocks from storage and validates their contents.
9910 * This function implements a simple fetcher to make sure that while
9911 * we're processing one buffer the L2ARC is already fetching the next
9914 * The arguments this_lp and next_lp point to the current and next log block
9915 * address in the block chain. Similarly, this_lb and next_lb hold the
9916 * l2arc_log_blk_phys_t's of the current and next L2ARC blk.
9918 * The `this_io' and `next_io' arguments are used for block fetching.
9919 * When issuing the first blk IO during rebuild, you should pass NULL for
9920 * `this_io'. This function will then issue a sync IO to read the block and
9921 * also issue an async IO to fetch the next block in the block chain. The
9922 * fetched IO is returned in `next_io'. On subsequent calls to this
9923 * function, pass the value returned in `next_io' from the previous call
9924 * as `this_io' and a fresh `next_io' pointer to hold the next fetch IO.
9925 * Prior to the call, you should initialize your `next_io' pointer to be
9926 * NULL. If no fetch IO was issued, the pointer is left set at NULL.
9928 * On success, this function returns 0, otherwise it returns an appropriate
9929 * error code. On error the fetching IO is aborted and cleared before
9930 * returning from this function. Therefore, if we return `success', the
9931 * caller can assume that we have taken care of cleanup of fetch IOs.
9934 l2arc_log_blk_read(l2arc_dev_t
*dev
,
9935 const l2arc_log_blkptr_t
*this_lbp
, const l2arc_log_blkptr_t
*next_lbp
,
9936 l2arc_log_blk_phys_t
*this_lb
, l2arc_log_blk_phys_t
*next_lb
,
9937 zio_t
*this_io
, zio_t
**next_io
)
9944 ASSERT(this_lbp
!= NULL
&& next_lbp
!= NULL
);
9945 ASSERT(this_lb
!= NULL
&& next_lb
!= NULL
);
9946 ASSERT(next_io
!= NULL
&& *next_io
== NULL
);
9947 ASSERT(l2arc_log_blkptr_valid(dev
, this_lbp
));
9950 * Check to see if we have issued the IO for this log block in a
9951 * previous run. If not, this is the first call, so issue it now.
9953 if (this_io
== NULL
) {
9954 this_io
= l2arc_log_blk_fetch(dev
->l2ad_vdev
, this_lbp
,
9959 * Peek to see if we can start issuing the next IO immediately.
9961 if (l2arc_log_blkptr_valid(dev
, next_lbp
)) {
9963 * Start issuing IO for the next log block early - this
9964 * should help keep the L2ARC device busy while we
9965 * decompress and restore this log block.
9967 *next_io
= l2arc_log_blk_fetch(dev
->l2ad_vdev
, next_lbp
,
9971 /* Wait for the IO to read this log block to complete */
9972 if ((err
= zio_wait(this_io
)) != 0) {
9973 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_io_errors
);
9974 zfs_dbgmsg("L2ARC IO error (%d) while reading log block, "
9975 "offset: %llu, vdev guid: %llu", err
, this_lbp
->lbp_daddr
,
9976 dev
->l2ad_vdev
->vdev_guid
);
9981 * Make sure the buffer checks out.
9982 * L2BLK_GET_PSIZE returns aligned size for log blocks.
9984 asize
= L2BLK_GET_PSIZE((this_lbp
)->lbp_prop
);
9985 fletcher_4_native(this_lb
, asize
, NULL
, &cksum
);
9986 if (!ZIO_CHECKSUM_EQUAL(cksum
, this_lbp
->lbp_cksum
)) {
9987 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_cksum_lb_errors
);
9988 zfs_dbgmsg("L2ARC log block cksum failed, offset: %llu, "
9989 "vdev guid: %llu, l2ad_hand: %llu, l2ad_evict: %llu",
9990 this_lbp
->lbp_daddr
, dev
->l2ad_vdev
->vdev_guid
,
9991 dev
->l2ad_hand
, dev
->l2ad_evict
);
9992 err
= SET_ERROR(ECKSUM
);
9996 /* Now we can take our time decoding this buffer */
9997 switch (L2BLK_GET_COMPRESS((this_lbp
)->lbp_prop
)) {
9998 case ZIO_COMPRESS_OFF
:
10000 case ZIO_COMPRESS_LZ4
:
10001 abd
= abd_alloc_for_io(asize
, B_TRUE
);
10002 abd_copy_from_buf_off(abd
, this_lb
, 0, asize
);
10003 if ((err
= zio_decompress_data(
10004 L2BLK_GET_COMPRESS((this_lbp
)->lbp_prop
),
10005 abd
, this_lb
, asize
, sizeof (*this_lb
), NULL
)) != 0) {
10006 err
= SET_ERROR(EINVAL
);
10011 err
= SET_ERROR(EINVAL
);
10014 if (this_lb
->lb_magic
== BSWAP_64(L2ARC_LOG_BLK_MAGIC
))
10015 byteswap_uint64_array(this_lb
, sizeof (*this_lb
));
10016 if (this_lb
->lb_magic
!= L2ARC_LOG_BLK_MAGIC
) {
10017 err
= SET_ERROR(EINVAL
);
10021 /* Abort an in-flight fetch I/O in case of error */
10022 if (err
!= 0 && *next_io
!= NULL
) {
10023 l2arc_log_blk_fetch_abort(*next_io
);
10032 * Restores the payload of a log block to ARC. This creates empty ARC hdr
10033 * entries which only contain an l2arc hdr, essentially restoring the
10034 * buffers to their L2ARC evicted state. This function also updates space
10035 * usage on the L2ARC vdev to make sure it tracks restored buffers.
10038 l2arc_log_blk_restore(l2arc_dev_t
*dev
, const l2arc_log_blk_phys_t
*lb
,
10039 uint64_t lb_asize
, uint64_t lb_daddr
)
10041 uint64_t size
= 0, asize
= 0;
10042 uint64_t log_entries
= dev
->l2ad_log_entries
;
10045 * Usually arc_adapt() is called only for data, not headers, but
10046 * since we may allocate significant amount of memory here, let ARC
10049 arc_adapt(log_entries
* HDR_L2ONLY_SIZE
, arc_l2c_only
);
10051 for (int i
= log_entries
- 1; i
>= 0; i
--) {
10053 * Restore goes in the reverse temporal direction to preserve
10054 * correct temporal ordering of buffers in the l2ad_buflist.
10055 * l2arc_hdr_restore also does a list_insert_tail instead of
10056 * list_insert_head on the l2ad_buflist:
10058 * LIST l2ad_buflist LIST
10059 * HEAD <------ (time) ------ TAIL
10060 * direction +-----+-----+-----+-----+-----+ direction
10061 * of l2arc <== | buf | buf | buf | buf | buf | ===> of rebuild
10062 * fill +-----+-----+-----+-----+-----+
10066 * l2arc_feed_thread l2arc_rebuild
10067 * will place new bufs here restores bufs here
10069 * During l2arc_rebuild() the device is not used by
10070 * l2arc_feed_thread() as dev->l2ad_rebuild is set to true.
10072 size
+= L2BLK_GET_LSIZE((&lb
->lb_entries
[i
])->le_prop
);
10073 asize
+= vdev_psize_to_asize(dev
->l2ad_vdev
,
10074 L2BLK_GET_PSIZE((&lb
->lb_entries
[i
])->le_prop
));
10075 l2arc_hdr_restore(&lb
->lb_entries
[i
], dev
);
10079 * Record rebuild stats:
10080 * size Logical size of restored buffers in the L2ARC
10081 * asize Aligned size of restored buffers in the L2ARC
10083 ARCSTAT_INCR(arcstat_l2_rebuild_size
, size
);
10084 ARCSTAT_INCR(arcstat_l2_rebuild_asize
, asize
);
10085 ARCSTAT_INCR(arcstat_l2_rebuild_bufs
, log_entries
);
10086 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize
, lb_asize
);
10087 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio
, asize
/ lb_asize
);
10088 ARCSTAT_BUMP(arcstat_l2_rebuild_log_blks
);
10092 * Restores a single ARC buf hdr from a log entry. The ARC buffer is put
10093 * into a state indicating that it has been evicted to L2ARC.
10096 l2arc_hdr_restore(const l2arc_log_ent_phys_t
*le
, l2arc_dev_t
*dev
)
10098 arc_buf_hdr_t
*hdr
, *exists
;
10099 kmutex_t
*hash_lock
;
10100 arc_buf_contents_t type
= L2BLK_GET_TYPE((le
)->le_prop
);
10104 * Do all the allocation before grabbing any locks, this lets us
10105 * sleep if memory is full and we don't have to deal with failed
10108 hdr
= arc_buf_alloc_l2only(L2BLK_GET_LSIZE((le
)->le_prop
), type
,
10109 dev
, le
->le_dva
, le
->le_daddr
,
10110 L2BLK_GET_PSIZE((le
)->le_prop
), le
->le_birth
,
10111 L2BLK_GET_COMPRESS((le
)->le_prop
), le
->le_complevel
,
10112 L2BLK_GET_PROTECTED((le
)->le_prop
),
10113 L2BLK_GET_PREFETCH((le
)->le_prop
));
10114 asize
= vdev_psize_to_asize(dev
->l2ad_vdev
,
10115 L2BLK_GET_PSIZE((le
)->le_prop
));
10118 * vdev_space_update() has to be called before arc_hdr_destroy() to
10119 * avoid underflow since the latter also calls the former.
10121 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10123 ARCSTAT_INCR(arcstat_l2_lsize
, HDR_GET_LSIZE(hdr
));
10124 ARCSTAT_INCR(arcstat_l2_psize
, HDR_GET_PSIZE(hdr
));
10126 mutex_enter(&dev
->l2ad_mtx
);
10127 list_insert_tail(&dev
->l2ad_buflist
, hdr
);
10128 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
), hdr
);
10129 mutex_exit(&dev
->l2ad_mtx
);
10131 exists
= buf_hash_insert(hdr
, &hash_lock
);
10133 /* Buffer was already cached, no need to restore it. */
10134 arc_hdr_destroy(hdr
);
10136 * If the buffer is already cached, check whether it has
10137 * L2ARC metadata. If not, enter them and update the flag.
10138 * This is important is case of onlining a cache device, since
10139 * we previously evicted all L2ARC metadata from ARC.
10141 if (!HDR_HAS_L2HDR(exists
)) {
10142 arc_hdr_set_flags(exists
, ARC_FLAG_HAS_L2HDR
);
10143 exists
->b_l2hdr
.b_dev
= dev
;
10144 exists
->b_l2hdr
.b_daddr
= le
->le_daddr
;
10145 mutex_enter(&dev
->l2ad_mtx
);
10146 list_insert_tail(&dev
->l2ad_buflist
, exists
);
10147 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
10148 arc_hdr_size(exists
), exists
);
10149 mutex_exit(&dev
->l2ad_mtx
);
10150 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10151 ARCSTAT_INCR(arcstat_l2_lsize
, HDR_GET_LSIZE(exists
));
10152 ARCSTAT_INCR(arcstat_l2_psize
, HDR_GET_PSIZE(exists
));
10154 ARCSTAT_BUMP(arcstat_l2_rebuild_bufs_precached
);
10157 mutex_exit(hash_lock
);
10161 * Starts an asynchronous read IO to read a log block. This is used in log
10162 * block reconstruction to start reading the next block before we are done
10163 * decoding and reconstructing the current block, to keep the l2arc device
10164 * nice and hot with read IO to process.
10165 * The returned zio will contain a newly allocated memory buffers for the IO
10166 * data which should then be freed by the caller once the zio is no longer
10167 * needed (i.e. due to it having completed). If you wish to abort this
10168 * zio, you should do so using l2arc_log_blk_fetch_abort, which takes
10169 * care of disposing of the allocated buffers correctly.
10172 l2arc_log_blk_fetch(vdev_t
*vd
, const l2arc_log_blkptr_t
*lbp
,
10173 l2arc_log_blk_phys_t
*lb
)
10177 l2arc_read_callback_t
*cb
;
10179 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
10180 asize
= L2BLK_GET_PSIZE((lbp
)->lbp_prop
);
10181 ASSERT(asize
<= sizeof (l2arc_log_blk_phys_t
));
10183 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
), KM_SLEEP
);
10184 cb
->l2rcb_abd
= abd_get_from_buf(lb
, asize
);
10185 pio
= zio_root(vd
->vdev_spa
, l2arc_blk_fetch_done
, cb
,
10186 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
|
10187 ZIO_FLAG_DONT_RETRY
);
10188 (void) zio_nowait(zio_read_phys(pio
, vd
, lbp
->lbp_daddr
, asize
,
10189 cb
->l2rcb_abd
, ZIO_CHECKSUM_OFF
, NULL
, NULL
,
10190 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_CANFAIL
|
10191 ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
, B_FALSE
));
10197 * Aborts a zio returned from l2arc_log_blk_fetch and frees the data
10198 * buffers allocated for it.
10201 l2arc_log_blk_fetch_abort(zio_t
*zio
)
10203 (void) zio_wait(zio
);
10207 * Creates a zio to update the device header on an l2arc device.
10210 l2arc_dev_hdr_update(l2arc_dev_t
*dev
)
10212 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10213 const uint64_t l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
10217 VERIFY(spa_config_held(dev
->l2ad_spa
, SCL_STATE_ALL
, RW_READER
));
10219 l2dhdr
->dh_magic
= L2ARC_DEV_HDR_MAGIC
;
10220 l2dhdr
->dh_version
= L2ARC_PERSISTENT_VERSION
;
10221 l2dhdr
->dh_spa_guid
= spa_guid(dev
->l2ad_vdev
->vdev_spa
);
10222 l2dhdr
->dh_vdev_guid
= dev
->l2ad_vdev
->vdev_guid
;
10223 l2dhdr
->dh_log_entries
= dev
->l2ad_log_entries
;
10224 l2dhdr
->dh_evict
= dev
->l2ad_evict
;
10225 l2dhdr
->dh_start
= dev
->l2ad_start
;
10226 l2dhdr
->dh_end
= dev
->l2ad_end
;
10227 l2dhdr
->dh_lb_asize
= zfs_refcount_count(&dev
->l2ad_lb_asize
);
10228 l2dhdr
->dh_lb_count
= zfs_refcount_count(&dev
->l2ad_lb_count
);
10229 l2dhdr
->dh_flags
= 0;
10230 l2dhdr
->dh_trim_action_time
= dev
->l2ad_vdev
->vdev_trim_action_time
;
10231 l2dhdr
->dh_trim_state
= dev
->l2ad_vdev
->vdev_trim_state
;
10232 if (dev
->l2ad_first
)
10233 l2dhdr
->dh_flags
|= L2ARC_DEV_HDR_EVICT_FIRST
;
10235 abd
= abd_get_from_buf(l2dhdr
, l2dhdr_asize
);
10237 err
= zio_wait(zio_write_phys(NULL
, dev
->l2ad_vdev
,
10238 VDEV_LABEL_START_SIZE
, l2dhdr_asize
, abd
, ZIO_CHECKSUM_LABEL
, NULL
,
10239 NULL
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_CANFAIL
, B_FALSE
));
10244 zfs_dbgmsg("L2ARC IO error (%d) while writing device header, "
10245 "vdev guid: %llu", err
, dev
->l2ad_vdev
->vdev_guid
);
10250 * Commits a log block to the L2ARC device. This routine is invoked from
10251 * l2arc_write_buffers when the log block fills up.
10252 * This function allocates some memory to temporarily hold the serialized
10253 * buffer to be written. This is then released in l2arc_write_done.
10256 l2arc_log_blk_commit(l2arc_dev_t
*dev
, zio_t
*pio
, l2arc_write_callback_t
*cb
)
10258 l2arc_log_blk_phys_t
*lb
= &dev
->l2ad_log_blk
;
10259 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10260 uint64_t psize
, asize
;
10262 l2arc_lb_abd_buf_t
*abd_buf
;
10264 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
10266 VERIFY3S(dev
->l2ad_log_ent_idx
, ==, dev
->l2ad_log_entries
);
10268 tmpbuf
= zio_buf_alloc(sizeof (*lb
));
10269 abd_buf
= zio_buf_alloc(sizeof (*abd_buf
));
10270 abd_buf
->abd
= abd_get_from_buf(lb
, sizeof (*lb
));
10271 lb_ptr_buf
= kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t
), KM_SLEEP
);
10272 lb_ptr_buf
->lb_ptr
= kmem_zalloc(sizeof (l2arc_log_blkptr_t
), KM_SLEEP
);
10274 /* link the buffer into the block chain */
10275 lb
->lb_prev_lbp
= l2dhdr
->dh_start_lbps
[1];
10276 lb
->lb_magic
= L2ARC_LOG_BLK_MAGIC
;
10279 * l2arc_log_blk_commit() may be called multiple times during a single
10280 * l2arc_write_buffers() call. Save the allocated abd buffers in a list
10281 * so we can free them in l2arc_write_done() later on.
10283 list_insert_tail(&cb
->l2wcb_abd_list
, abd_buf
);
10285 /* try to compress the buffer */
10286 psize
= zio_compress_data(ZIO_COMPRESS_LZ4
,
10287 abd_buf
->abd
, tmpbuf
, sizeof (*lb
), 0);
10289 /* a log block is never entirely zero */
10290 ASSERT(psize
!= 0);
10291 asize
= vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
10292 ASSERT(asize
<= sizeof (*lb
));
10295 * Update the start log block pointer in the device header to point
10296 * to the log block we're about to write.
10298 l2dhdr
->dh_start_lbps
[1] = l2dhdr
->dh_start_lbps
[0];
10299 l2dhdr
->dh_start_lbps
[0].lbp_daddr
= dev
->l2ad_hand
;
10300 l2dhdr
->dh_start_lbps
[0].lbp_payload_asize
=
10301 dev
->l2ad_log_blk_payload_asize
;
10302 l2dhdr
->dh_start_lbps
[0].lbp_payload_start
=
10303 dev
->l2ad_log_blk_payload_start
;
10306 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
, sizeof (*lb
));
10308 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
, asize
);
10309 L2BLK_SET_CHECKSUM(
10310 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10311 ZIO_CHECKSUM_FLETCHER_4
);
10312 if (asize
< sizeof (*lb
)) {
10313 /* compression succeeded */
10314 bzero(tmpbuf
+ psize
, asize
- psize
);
10315 L2BLK_SET_COMPRESS(
10316 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10319 /* compression failed */
10320 bcopy(lb
, tmpbuf
, sizeof (*lb
));
10321 L2BLK_SET_COMPRESS(
10322 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10326 /* checksum what we're about to write */
10327 fletcher_4_native(tmpbuf
, asize
, NULL
,
10328 &l2dhdr
->dh_start_lbps
[0].lbp_cksum
);
10330 abd_put(abd_buf
->abd
);
10332 /* perform the write itself */
10333 abd_buf
->abd
= abd_get_from_buf(tmpbuf
, sizeof (*lb
));
10334 abd_take_ownership_of_buf(abd_buf
->abd
, B_TRUE
);
10335 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
, dev
->l2ad_hand
,
10336 asize
, abd_buf
->abd
, ZIO_CHECKSUM_OFF
, NULL
, NULL
,
10337 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_CANFAIL
, B_FALSE
);
10338 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
, zio_t
*, wzio
);
10339 (void) zio_nowait(wzio
);
10341 dev
->l2ad_hand
+= asize
;
10343 * Include the committed log block's pointer in the list of pointers
10344 * to log blocks present in the L2ARC device.
10346 bcopy(&l2dhdr
->dh_start_lbps
[0], lb_ptr_buf
->lb_ptr
,
10347 sizeof (l2arc_log_blkptr_t
));
10348 mutex_enter(&dev
->l2ad_mtx
);
10349 list_insert_head(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
10350 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, asize
);
10351 ARCSTAT_BUMP(arcstat_l2_log_blk_count
);
10352 zfs_refcount_add_many(&dev
->l2ad_lb_asize
, asize
, lb_ptr_buf
);
10353 zfs_refcount_add(&dev
->l2ad_lb_count
, lb_ptr_buf
);
10354 mutex_exit(&dev
->l2ad_mtx
);
10355 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10357 /* bump the kstats */
10358 ARCSTAT_INCR(arcstat_l2_write_bytes
, asize
);
10359 ARCSTAT_BUMP(arcstat_l2_log_blk_writes
);
10360 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize
, asize
);
10361 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio
,
10362 dev
->l2ad_log_blk_payload_asize
/ asize
);
10364 /* start a new log block */
10365 dev
->l2ad_log_ent_idx
= 0;
10366 dev
->l2ad_log_blk_payload_asize
= 0;
10367 dev
->l2ad_log_blk_payload_start
= 0;
10371 * Validates an L2ARC log block address to make sure that it can be read
10372 * from the provided L2ARC device.
10375 l2arc_log_blkptr_valid(l2arc_dev_t
*dev
, const l2arc_log_blkptr_t
*lbp
)
10377 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
10378 uint64_t asize
= L2BLK_GET_PSIZE((lbp
)->lbp_prop
);
10379 uint64_t end
= lbp
->lbp_daddr
+ asize
- 1;
10380 uint64_t start
= lbp
->lbp_payload_start
;
10381 boolean_t evicted
= B_FALSE
;
10384 * A log block is valid if all of the following conditions are true:
10385 * - it fits entirely (including its payload) between l2ad_start and
10387 * - it has a valid size
10388 * - neither the log block itself nor part of its payload was evicted
10389 * by l2arc_evict():
10391 * l2ad_hand l2ad_evict
10396 * l2ad_start ============================================ l2ad_end
10397 * --------------------------||||
10404 l2arc_range_check_overlap(start
, end
, dev
->l2ad_hand
) ||
10405 l2arc_range_check_overlap(start
, end
, dev
->l2ad_evict
) ||
10406 l2arc_range_check_overlap(dev
->l2ad_hand
, dev
->l2ad_evict
, start
) ||
10407 l2arc_range_check_overlap(dev
->l2ad_hand
, dev
->l2ad_evict
, end
);
10409 return (start
>= dev
->l2ad_start
&& end
<= dev
->l2ad_end
&&
10410 asize
> 0 && asize
<= sizeof (l2arc_log_blk_phys_t
) &&
10411 (!evicted
|| dev
->l2ad_first
));
10415 * Inserts ARC buffer header `hdr' into the current L2ARC log block on
10416 * the device. The buffer being inserted must be present in L2ARC.
10417 * Returns B_TRUE if the L2ARC log block is full and needs to be committed
10418 * to L2ARC, or B_FALSE if it still has room for more ARC buffers.
10421 l2arc_log_blk_insert(l2arc_dev_t
*dev
, const arc_buf_hdr_t
*hdr
)
10423 l2arc_log_blk_phys_t
*lb
= &dev
->l2ad_log_blk
;
10424 l2arc_log_ent_phys_t
*le
;
10426 if (dev
->l2ad_log_entries
== 0)
10429 int index
= dev
->l2ad_log_ent_idx
++;
10431 ASSERT3S(index
, <, dev
->l2ad_log_entries
);
10432 ASSERT(HDR_HAS_L2HDR(hdr
));
10434 le
= &lb
->lb_entries
[index
];
10435 bzero(le
, sizeof (*le
));
10436 le
->le_dva
= hdr
->b_dva
;
10437 le
->le_birth
= hdr
->b_birth
;
10438 le
->le_daddr
= hdr
->b_l2hdr
.b_daddr
;
10440 dev
->l2ad_log_blk_payload_start
= le
->le_daddr
;
10441 L2BLK_SET_LSIZE((le
)->le_prop
, HDR_GET_LSIZE(hdr
));
10442 L2BLK_SET_PSIZE((le
)->le_prop
, HDR_GET_PSIZE(hdr
));
10443 L2BLK_SET_COMPRESS((le
)->le_prop
, HDR_GET_COMPRESS(hdr
));
10444 le
->le_complevel
= hdr
->b_complevel
;
10445 L2BLK_SET_TYPE((le
)->le_prop
, hdr
->b_type
);
10446 L2BLK_SET_PROTECTED((le
)->le_prop
, !!(HDR_PROTECTED(hdr
)));
10447 L2BLK_SET_PREFETCH((le
)->le_prop
, !!(HDR_PREFETCH(hdr
)));
10449 dev
->l2ad_log_blk_payload_asize
+= vdev_psize_to_asize(dev
->l2ad_vdev
,
10450 HDR_GET_PSIZE(hdr
));
10452 return (dev
->l2ad_log_ent_idx
== dev
->l2ad_log_entries
);
10456 * Checks whether a given L2ARC device address sits in a time-sequential
10457 * range. The trick here is that the L2ARC is a rotary buffer, so we can't
10458 * just do a range comparison, we need to handle the situation in which the
10459 * range wraps around the end of the L2ARC device. Arguments:
10460 * bottom -- Lower end of the range to check (written to earlier).
10461 * top -- Upper end of the range to check (written to later).
10462 * check -- The address for which we want to determine if it sits in
10463 * between the top and bottom.
10465 * The 3-way conditional below represents the following cases:
10467 * bottom < top : Sequentially ordered case:
10468 * <check>--------+-------------------+
10469 * | (overlap here?) |
10471 * |---------------<bottom>============<top>--------------|
10473 * bottom > top: Looped-around case:
10474 * <check>--------+------------------+
10475 * | (overlap here?) |
10477 * |===============<top>---------------<bottom>===========|
10480 * +---------------+---------<check>
10482 * top == bottom : Just a single address comparison.
10485 l2arc_range_check_overlap(uint64_t bottom
, uint64_t top
, uint64_t check
)
10488 return (bottom
<= check
&& check
<= top
);
10489 else if (bottom
> top
)
10490 return (check
<= top
|| bottom
<= check
);
10492 return (check
== top
);
10495 EXPORT_SYMBOL(arc_buf_size
);
10496 EXPORT_SYMBOL(arc_write
);
10497 EXPORT_SYMBOL(arc_read
);
10498 EXPORT_SYMBOL(arc_buf_info
);
10499 EXPORT_SYMBOL(arc_getbuf_func
);
10500 EXPORT_SYMBOL(arc_add_prune_callback
);
10501 EXPORT_SYMBOL(arc_remove_prune_callback
);
10503 /* BEGIN CSTYLED */
10504 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min
, param_set_arc_long
,
10505 param_get_long
, ZMOD_RW
, "Min arc size");
10507 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, max
, param_set_arc_long
,
10508 param_get_long
, ZMOD_RW
, "Max arc size");
10510 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, meta_limit
, param_set_arc_long
,
10511 param_get_long
, ZMOD_RW
, "Metadata limit for arc size");
10513 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, meta_limit_percent
,
10514 param_set_arc_long
, param_get_long
, ZMOD_RW
,
10515 "Percent of arc size for arc meta limit");
10517 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, meta_min
, param_set_arc_long
,
10518 param_get_long
, ZMOD_RW
, "Min arc metadata");
10520 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, meta_prune
, INT
, ZMOD_RW
,
10521 "Meta objects to scan for prune");
10523 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, meta_adjust_restarts
, INT
, ZMOD_RW
,
10524 "Limit number of restarts in arc_evict_meta");
10526 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, meta_strategy
, INT
, ZMOD_RW
,
10527 "Meta reclaim strategy");
10529 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, grow_retry
, param_set_arc_int
,
10530 param_get_int
, ZMOD_RW
, "Seconds before growing arc size");
10532 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, p_dampener_disable
, INT
, ZMOD_RW
,
10533 "Disable arc_p adapt dampener");
10535 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, shrink_shift
, param_set_arc_int
,
10536 param_get_int
, ZMOD_RW
, "log2(fraction of arc to reclaim)");
10538 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, pc_percent
, UINT
, ZMOD_RW
,
10539 "Percent of pagecache to reclaim arc to");
10541 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, p_min_shift
, param_set_arc_int
,
10542 param_get_int
, ZMOD_RW
, "arc_c shift to calc min/max arc_p");
10544 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, average_blocksize
, INT
, ZMOD_RD
,
10545 "Target average block size");
10547 ZFS_MODULE_PARAM(zfs
, zfs_
, compressed_arc_enabled
, INT
, ZMOD_RW
,
10548 "Disable compressed arc buffers");
10550 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min_prefetch_ms
, param_set_arc_int
,
10551 param_get_int
, ZMOD_RW
, "Min life of prefetch block in ms");
10553 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min_prescient_prefetch_ms
,
10554 param_set_arc_int
, param_get_int
, ZMOD_RW
,
10555 "Min life of prescient prefetched block in ms");
10557 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, write_max
, ULONG
, ZMOD_RW
,
10558 "Max write bytes per interval");
10560 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, write_boost
, ULONG
, ZMOD_RW
,
10561 "Extra write bytes during device warmup");
10563 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, headroom
, ULONG
, ZMOD_RW
,
10564 "Number of max device writes to precache");
10566 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, headroom_boost
, ULONG
, ZMOD_RW
,
10567 "Compressed l2arc_headroom multiplier");
10569 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, trim_ahead
, ULONG
, ZMOD_RW
,
10570 "TRIM ahead L2ARC write size multiplier");
10572 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_secs
, ULONG
, ZMOD_RW
,
10573 "Seconds between L2ARC writing");
10575 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_min_ms
, ULONG
, ZMOD_RW
,
10576 "Min feed interval in milliseconds");
10578 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, noprefetch
, INT
, ZMOD_RW
,
10579 "Skip caching prefetched buffers");
10581 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_again
, INT
, ZMOD_RW
,
10582 "Turbo L2ARC warmup");
10584 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, norw
, INT
, ZMOD_RW
,
10585 "No reads during writes");
10587 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, meta_percent
, INT
, ZMOD_RW
,
10588 "Percent of ARC size allowed for L2ARC-only headers");
10590 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, rebuild_enabled
, INT
, ZMOD_RW
,
10591 "Rebuild the L2ARC when importing a pool");
10593 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, rebuild_blocks_min_l2size
, ULONG
, ZMOD_RW
,
10594 "Min size in bytes to write rebuild log blocks in L2ARC");
10596 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, mfuonly
, INT
, ZMOD_RW
,
10597 "Cache only MFU data from ARC into L2ARC");
10599 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, lotsfree_percent
, param_set_arc_int
,
10600 param_get_int
, ZMOD_RW
, "System free memory I/O throttle in bytes");
10602 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, sys_free
, param_set_arc_long
,
10603 param_get_long
, ZMOD_RW
, "System free memory target size in bytes");
10605 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, dnode_limit
, param_set_arc_long
,
10606 param_get_long
, ZMOD_RW
, "Minimum bytes of dnodes in arc");
10608 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, dnode_limit_percent
,
10609 param_set_arc_long
, param_get_long
, ZMOD_RW
,
10610 "Percent of ARC meta buffers for dnodes");
10612 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, dnode_reduce_percent
, ULONG
, ZMOD_RW
,
10613 "Percentage of excess dnodes to try to unpin");
10615 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, eviction_pct
, INT
, ZMOD_RW
,
10616 "When full, ARC allocation waits for eviction of this % of alloc size");
10618 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, evict_batch_limit
, INT
, ZMOD_RW
,
10619 "The number of headers to evict per sublist before moving to the next");