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) 2012, Joyent, Inc. All rights reserved.
24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2014 by Saso Kiselkov. All rights reserved.
26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
30 * DVA-based Adjustable Replacement Cache
32 * While much of the theory of operation used here is
33 * based on the self-tuning, low overhead replacement cache
34 * presented by Megiddo and Modha at FAST 2003, there are some
35 * significant differences:
37 * 1. The Megiddo and Modha model assumes any page is evictable.
38 * Pages in its cache cannot be "locked" into memory. This makes
39 * the eviction algorithm simple: evict the last page in the list.
40 * This also make the performance characteristics easy to reason
41 * about. Our cache is not so simple. At any given moment, some
42 * subset of the blocks in the cache are un-evictable because we
43 * have handed out a reference to them. Blocks are only evictable
44 * when there are no external references active. This makes
45 * eviction far more problematic: we choose to evict the evictable
46 * blocks that are the "lowest" in the list.
48 * There are times when it is not possible to evict the requested
49 * space. In these circumstances we are unable to adjust the cache
50 * size. To prevent the cache growing unbounded at these times we
51 * implement a "cache throttle" that slows the flow of new data
52 * into the cache until we can make space available.
54 * 2. The Megiddo and Modha model assumes a fixed cache size.
55 * Pages are evicted when the cache is full and there is a cache
56 * miss. Our model has a variable sized cache. It grows with
57 * high use, but also tries to react to memory pressure from the
58 * operating system: decreasing its size when system memory is
61 * 3. The Megiddo and Modha model assumes a fixed page size. All
62 * elements of the cache are therefore exactly the same size. So
63 * when adjusting the cache size following a cache miss, its simply
64 * a matter of choosing a single page to evict. In our model, we
65 * have variable sized cache blocks (rangeing from 512 bytes to
66 * 128K bytes). We therefore choose a set of blocks to evict to make
67 * space for a cache miss that approximates as closely as possible
68 * the space used by the new block.
70 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
71 * by N. Megiddo & D. Modha, FAST 2003
77 * A new reference to a cache buffer can be obtained in two
78 * ways: 1) via a hash table lookup using the DVA as a key,
79 * or 2) via one of the ARC lists. The arc_read() interface
80 * uses method 1, while the internal ARC algorithms for
81 * adjusting the cache use method 2. We therefore provide two
82 * types of locks: 1) the hash table lock array, and 2) the
85 * Buffers do not have their own mutexes, rather they rely on the
86 * hash table mutexes for the bulk of their protection (i.e. most
87 * fields in the arc_buf_hdr_t are protected by these mutexes).
89 * buf_hash_find() returns the appropriate mutex (held) when it
90 * locates the requested buffer in the hash table. It returns
91 * NULL for the mutex if the buffer was not in the table.
93 * buf_hash_remove() expects the appropriate hash mutex to be
94 * already held before it is invoked.
96 * Each ARC state also has a mutex which is used to protect the
97 * buffer list associated with the state. When attempting to
98 * obtain a hash table lock while holding an ARC list lock you
99 * must use: mutex_tryenter() to avoid deadlock. Also note that
100 * the active state mutex must be held before the ghost state mutex.
102 * It as also possible to register a callback which is run when the
103 * arc_meta_limit is reached and no buffers can be safely evicted. In
104 * this case the arc user should drop a reference on some arc buffers so
105 * they can be reclaimed and the arc_meta_limit honored. For example,
106 * when using the ZPL each dentry holds a references on a znode. These
107 * dentries must be pruned before the arc buffer holding the znode can
110 * Note that the majority of the performance stats are manipulated
111 * with atomic operations.
113 * The L2ARC uses the l2ad_mtx on each vdev for the following:
115 * - L2ARC buflist creation
116 * - L2ARC buflist eviction
117 * - L2ARC write completion, which walks L2ARC buflists
118 * - ARC header destruction, as it removes from L2ARC buflists
119 * - ARC header release, as it removes from L2ARC buflists
125 * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure.
126 * This structure can point either to a block that is still in the cache or to
127 * one that is only accessible in an L2 ARC device, or it can provide
128 * information about a block that was recently evicted. If a block is
129 * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough
130 * information to retrieve it from the L2ARC device. This information is
131 * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block
132 * that is in this state cannot access the data directly.
134 * Blocks that are actively being referenced or have not been evicted
135 * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within
136 * the arc_buf_hdr_t that will point to the data block in memory. A block can
137 * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC
138 * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and
139 * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd).
141 * The L1ARC's data pointer may or may not be uncompressed. The ARC has the
142 * ability to store the physical data (b_pabd) associated with the DVA of the
143 * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block,
144 * it will match its on-disk compression characteristics. This behavior can be
145 * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the
146 * compressed ARC functionality is disabled, the b_pabd will point to an
147 * uncompressed version of the on-disk data.
149 * Data in the L1ARC is not accessed by consumers of the ARC directly. Each
150 * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it.
151 * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC
152 * consumer. The ARC will provide references to this data and will keep it
153 * cached until it is no longer in use. The ARC caches only the L1ARC's physical
154 * data block and will evict any arc_buf_t that is no longer referenced. The
155 * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the
156 * "overhead_size" kstat.
158 * Depending on the consumer, an arc_buf_t can be requested in uncompressed or
159 * compressed form. The typical case is that consumers will want uncompressed
160 * data, and when that happens a new data buffer is allocated where the data is
161 * decompressed for them to use. Currently the only consumer who wants
162 * compressed arc_buf_t's is "zfs send", when it streams data exactly as it
163 * exists on disk. When this happens, the arc_buf_t's data buffer is shared
164 * with the arc_buf_hdr_t.
166 * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The
167 * first one is owned by a compressed send consumer (and therefore references
168 * the same compressed data buffer as the arc_buf_hdr_t) and the second could be
169 * used by any other consumer (and has its own uncompressed copy of the data
184 * | b_buf +------------>+-----------+ arc_buf_t
185 * | b_pabd +-+ |b_next +---->+-----------+
186 * +-----------+ | |-----------| |b_next +-->NULL
187 * | |b_comp = T | +-----------+
188 * | |b_data +-+ |b_comp = F |
189 * | +-----------+ | |b_data +-+
190 * +->+------+ | +-----------+ |
192 * data | |<--------------+ | uncompressed
193 * +------+ compressed, | data
194 * shared +-->+------+
199 * When a consumer reads a block, the ARC must first look to see if the
200 * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new
201 * arc_buf_t and either copies uncompressed data into a new data buffer from an
202 * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a
203 * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the
204 * hdr is compressed and the desired compression characteristics of the
205 * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the
206 * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be
207 * the last buffer in the hdr's b_buf list, however a shared compressed buf can
208 * be anywhere in the hdr's list.
210 * The diagram below shows an example of an uncompressed ARC hdr that is
211 * sharing its data with an arc_buf_t (note that the shared uncompressed buf is
212 * the last element in the buf list):
224 * | | arc_buf_t (shared)
225 * | b_buf +------------>+---------+ arc_buf_t
226 * | | |b_next +---->+---------+
227 * | b_pabd +-+ |---------| |b_next +-->NULL
228 * +-----------+ | | | +---------+
230 * | +---------+ | |b_data +-+
231 * +->+------+ | +---------+ |
233 * uncompressed | | | |
236 * | uncompressed | | |
239 * +---------------------------------+
241 * Writing to the ARC requires that the ARC first discard the hdr's b_pabd
242 * since the physical block is about to be rewritten. The new data contents
243 * will be contained in the arc_buf_t. As the I/O pipeline performs the write,
244 * it may compress the data before writing it to disk. The ARC will be called
245 * with the transformed data and will bcopy the transformed on-disk block into
246 * a newly allocated b_pabd. Writes are always done into buffers which have
247 * either been loaned (and hence are new and don't have other readers) or
248 * buffers which have been released (and hence have their own hdr, if there
249 * were originally other readers of the buf's original hdr). This ensures that
250 * the ARC only needs to update a single buf and its hdr after a write occurs.
252 * When the L2ARC is in use, it will also take advantage of the b_pabd. The
253 * L2ARC will always write the contents of b_pabd to the L2ARC. This means
254 * that when compressed ARC is enabled that the L2ARC blocks are identical
255 * to the on-disk block in the main data pool. This provides a significant
256 * advantage since the ARC can leverage the bp's checksum when reading from the
257 * L2ARC to determine if the contents are valid. However, if the compressed
258 * ARC is disabled, then the L2ARC's block must be transformed to look
259 * like the physical block in the main data pool before comparing the
260 * checksum and determining its validity.
262 * The L1ARC has a slightly different system for storing encrypted data.
263 * Raw (encrypted + possibly compressed) data has a few subtle differences from
264 * data that is just compressed. The biggest difference is that it is not
265 * possible to decrypt encrypted data (or visa versa) if the keys aren't loaded.
266 * The other difference is that encryption cannot be treated as a suggestion.
267 * If a caller would prefer compressed data, but they actually wind up with
268 * uncompressed data the worst thing that could happen is there might be a
269 * performance hit. If the caller requests encrypted data, however, we must be
270 * sure they actually get it or else secret information could be leaked. Raw
271 * data is stored in hdr->b_crypt_hdr.b_rabd. An encrypted header, therefore,
272 * may have both an encrypted version and a decrypted version of its data at
273 * once. When a caller needs a raw arc_buf_t, it is allocated and the data is
274 * copied out of this header. To avoid complications with b_pabd, raw buffers
280 #include <sys/spa_impl.h>
281 #include <sys/zio_compress.h>
282 #include <sys/zio_checksum.h>
283 #include <sys/zfs_context.h>
285 #include <sys/refcount.h>
286 #include <sys/vdev.h>
287 #include <sys/vdev_impl.h>
288 #include <sys/dsl_pool.h>
289 #include <sys/zio_checksum.h>
290 #include <sys/multilist.h>
293 #include <sys/fm/fs/zfs.h>
295 #include <sys/vmsystm.h>
297 #include <sys/fs/swapnode.h>
299 #include <linux/mm_compat.h>
300 #include <linux/page_compat.h>
302 #include <sys/callb.h>
303 #include <sys/kstat.h>
304 #include <sys/dmu_tx.h>
305 #include <zfs_fletcher.h>
306 #include <sys/arc_impl.h>
307 #include <sys/trace_arc.h>
310 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
311 boolean_t arc_watch
= B_FALSE
;
314 static kmutex_t arc_reclaim_lock
;
315 static kcondvar_t arc_reclaim_thread_cv
;
316 static boolean_t arc_reclaim_thread_exit
;
317 static kcondvar_t arc_reclaim_waiters_cv
;
320 * The number of headers to evict in arc_evict_state_impl() before
321 * dropping the sublist lock and evicting from another sublist. A lower
322 * value means we're more likely to evict the "correct" header (i.e. the
323 * oldest header in the arc state), but comes with higher overhead
324 * (i.e. more invocations of arc_evict_state_impl()).
326 int zfs_arc_evict_batch_limit
= 10;
328 /* number of seconds before growing cache again */
329 static int arc_grow_retry
= 5;
331 /* shift of arc_c for calculating overflow limit in arc_get_data_impl */
332 int zfs_arc_overflow_shift
= 8;
334 /* shift of arc_c for calculating both min and max arc_p */
335 static int arc_p_min_shift
= 4;
337 /* log2(fraction of arc to reclaim) */
338 static int arc_shrink_shift
= 7;
340 /* percent of pagecache to reclaim arc to */
342 static uint_t zfs_arc_pc_percent
= 0;
346 * log2(fraction of ARC which must be free to allow growing).
347 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
348 * when reading a new block into the ARC, we will evict an equal-sized block
351 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
352 * we will still not allow it to grow.
354 int arc_no_grow_shift
= 5;
358 * minimum lifespan of a prefetch block in clock ticks
359 * (initialized in arc_init())
361 static int arc_min_prefetch_ms
;
362 static int arc_min_prescient_prefetch_ms
;
365 * If this percent of memory is free, don't throttle.
367 int arc_lotsfree_percent
= 10;
372 * The arc has filled available memory and has now warmed up.
374 static boolean_t arc_warm
;
377 * log2 fraction of the zio arena to keep free.
379 int arc_zio_arena_free_shift
= 2;
382 * These tunables are for performance analysis.
384 unsigned long zfs_arc_max
= 0;
385 unsigned long zfs_arc_min
= 0;
386 unsigned long zfs_arc_meta_limit
= 0;
387 unsigned long zfs_arc_meta_min
= 0;
388 unsigned long zfs_arc_dnode_limit
= 0;
389 unsigned long zfs_arc_dnode_reduce_percent
= 10;
390 int zfs_arc_grow_retry
= 0;
391 int zfs_arc_shrink_shift
= 0;
392 int zfs_arc_p_min_shift
= 0;
393 int zfs_arc_average_blocksize
= 8 * 1024; /* 8KB */
395 int zfs_compressed_arc_enabled
= B_TRUE
;
398 * ARC will evict meta buffers that exceed arc_meta_limit. This
399 * tunable make arc_meta_limit adjustable for different workloads.
401 unsigned long zfs_arc_meta_limit_percent
= 75;
404 * Percentage that can be consumed by dnodes of ARC meta buffers.
406 unsigned long zfs_arc_dnode_limit_percent
= 10;
409 * These tunables are Linux specific
411 unsigned long zfs_arc_sys_free
= 0;
412 int zfs_arc_min_prefetch_ms
= 0;
413 int zfs_arc_min_prescient_prefetch_ms
= 0;
414 int zfs_arc_p_dampener_disable
= 1;
415 int zfs_arc_meta_prune
= 10000;
416 int zfs_arc_meta_strategy
= ARC_STRATEGY_META_BALANCED
;
417 int zfs_arc_meta_adjust_restarts
= 4096;
418 int zfs_arc_lotsfree_percent
= 10;
421 static arc_state_t ARC_anon
;
422 static arc_state_t ARC_mru
;
423 static arc_state_t ARC_mru_ghost
;
424 static arc_state_t ARC_mfu
;
425 static arc_state_t ARC_mfu_ghost
;
426 static arc_state_t ARC_l2c_only
;
428 typedef struct arc_stats
{
429 kstat_named_t arcstat_hits
;
430 kstat_named_t arcstat_misses
;
431 kstat_named_t arcstat_demand_data_hits
;
432 kstat_named_t arcstat_demand_data_misses
;
433 kstat_named_t arcstat_demand_metadata_hits
;
434 kstat_named_t arcstat_demand_metadata_misses
;
435 kstat_named_t arcstat_prefetch_data_hits
;
436 kstat_named_t arcstat_prefetch_data_misses
;
437 kstat_named_t arcstat_prefetch_metadata_hits
;
438 kstat_named_t arcstat_prefetch_metadata_misses
;
439 kstat_named_t arcstat_mru_hits
;
440 kstat_named_t arcstat_mru_ghost_hits
;
441 kstat_named_t arcstat_mfu_hits
;
442 kstat_named_t arcstat_mfu_ghost_hits
;
443 kstat_named_t arcstat_deleted
;
445 * Number of buffers that could not be evicted because the hash lock
446 * was held by another thread. The lock may not necessarily be held
447 * by something using the same buffer, since hash locks are shared
448 * by multiple buffers.
450 kstat_named_t arcstat_mutex_miss
;
452 * Number of buffers skipped when updating the access state due to the
453 * header having already been released after acquiring the hash lock.
455 kstat_named_t arcstat_access_skip
;
457 * Number of buffers skipped because they have I/O in progress, are
458 * indirect prefetch buffers that have not lived long enough, or are
459 * not from the spa we're trying to evict from.
461 kstat_named_t arcstat_evict_skip
;
463 * Number of times arc_evict_state() was unable to evict enough
464 * buffers to reach its target amount.
466 kstat_named_t arcstat_evict_not_enough
;
467 kstat_named_t arcstat_evict_l2_cached
;
468 kstat_named_t arcstat_evict_l2_eligible
;
469 kstat_named_t arcstat_evict_l2_ineligible
;
470 kstat_named_t arcstat_evict_l2_skip
;
471 kstat_named_t arcstat_hash_elements
;
472 kstat_named_t arcstat_hash_elements_max
;
473 kstat_named_t arcstat_hash_collisions
;
474 kstat_named_t arcstat_hash_chains
;
475 kstat_named_t arcstat_hash_chain_max
;
476 kstat_named_t arcstat_p
;
477 kstat_named_t arcstat_c
;
478 kstat_named_t arcstat_c_min
;
479 kstat_named_t arcstat_c_max
;
480 kstat_named_t arcstat_size
;
482 * Number of compressed bytes stored in the arc_buf_hdr_t's b_pabd.
483 * Note that the compressed bytes may match the uncompressed bytes
484 * if the block is either not compressed or compressed arc is disabled.
486 kstat_named_t arcstat_compressed_size
;
488 * Uncompressed size of the data stored in b_pabd. If compressed
489 * arc is disabled then this value will be identical to the stat
492 kstat_named_t arcstat_uncompressed_size
;
494 * Number of bytes stored in all the arc_buf_t's. This is classified
495 * as "overhead" since this data is typically short-lived and will
496 * be evicted from the arc when it becomes unreferenced unless the
497 * zfs_keep_uncompressed_metadata or zfs_keep_uncompressed_level
498 * values have been set (see comment in dbuf.c for more information).
500 kstat_named_t arcstat_overhead_size
;
502 * Number of bytes consumed by internal ARC structures necessary
503 * for tracking purposes; these structures are not actually
504 * backed by ARC buffers. This includes arc_buf_hdr_t structures
505 * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only
506 * caches), and arc_buf_t structures (allocated via arc_buf_t
509 kstat_named_t arcstat_hdr_size
;
511 * Number of bytes consumed by ARC buffers of type equal to
512 * ARC_BUFC_DATA. This is generally consumed by buffers backing
513 * on disk user data (e.g. plain file contents).
515 kstat_named_t arcstat_data_size
;
517 * Number of bytes consumed by ARC buffers of type equal to
518 * ARC_BUFC_METADATA. This is generally consumed by buffers
519 * backing on disk data that is used for internal ZFS
520 * structures (e.g. ZAP, dnode, indirect blocks, etc).
522 kstat_named_t arcstat_metadata_size
;
524 * Number of bytes consumed by dmu_buf_impl_t objects.
526 kstat_named_t arcstat_dbuf_size
;
528 * Number of bytes consumed by dnode_t objects.
530 kstat_named_t arcstat_dnode_size
;
532 * Number of bytes consumed by bonus buffers.
534 kstat_named_t arcstat_bonus_size
;
536 * Total number of bytes consumed by ARC buffers residing in the
537 * arc_anon state. This includes *all* buffers in the arc_anon
538 * state; e.g. data, metadata, evictable, and unevictable buffers
539 * are all included in this value.
541 kstat_named_t arcstat_anon_size
;
543 * Number of bytes consumed by ARC buffers that meet the
544 * following criteria: backing buffers of type ARC_BUFC_DATA,
545 * residing in the arc_anon state, and are eligible for eviction
546 * (e.g. have no outstanding holds on the buffer).
548 kstat_named_t arcstat_anon_evictable_data
;
550 * Number of bytes consumed by ARC buffers that meet the
551 * following criteria: backing buffers of type ARC_BUFC_METADATA,
552 * residing in the arc_anon state, and are eligible for eviction
553 * (e.g. have no outstanding holds on the buffer).
555 kstat_named_t arcstat_anon_evictable_metadata
;
557 * Total number of bytes consumed by ARC buffers residing in the
558 * arc_mru state. This includes *all* buffers in the arc_mru
559 * state; e.g. data, metadata, evictable, and unevictable buffers
560 * are all included in this value.
562 kstat_named_t arcstat_mru_size
;
564 * Number of bytes consumed by ARC buffers that meet the
565 * following criteria: backing buffers of type ARC_BUFC_DATA,
566 * residing in the arc_mru state, and are eligible for eviction
567 * (e.g. have no outstanding holds on the buffer).
569 kstat_named_t arcstat_mru_evictable_data
;
571 * Number of bytes consumed by ARC buffers that meet the
572 * following criteria: backing buffers of type ARC_BUFC_METADATA,
573 * residing in the arc_mru state, and are eligible for eviction
574 * (e.g. have no outstanding holds on the buffer).
576 kstat_named_t arcstat_mru_evictable_metadata
;
578 * Total number of bytes that *would have been* consumed by ARC
579 * buffers in the arc_mru_ghost state. The key thing to note
580 * here, is the fact that this size doesn't actually indicate
581 * RAM consumption. The ghost lists only consist of headers and
582 * don't actually have ARC buffers linked off of these headers.
583 * Thus, *if* the headers had associated ARC buffers, these
584 * buffers *would have* consumed this number of bytes.
586 kstat_named_t arcstat_mru_ghost_size
;
588 * Number of bytes that *would have been* consumed by ARC
589 * buffers that are eligible for eviction, of type
590 * ARC_BUFC_DATA, and linked off the arc_mru_ghost state.
592 kstat_named_t arcstat_mru_ghost_evictable_data
;
594 * Number of bytes that *would have been* consumed by ARC
595 * buffers that are eligible for eviction, of type
596 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
598 kstat_named_t arcstat_mru_ghost_evictable_metadata
;
600 * Total number of bytes consumed by ARC buffers residing in the
601 * arc_mfu state. This includes *all* buffers in the arc_mfu
602 * state; e.g. data, metadata, evictable, and unevictable buffers
603 * are all included in this value.
605 kstat_named_t arcstat_mfu_size
;
607 * Number of bytes consumed by ARC buffers that are eligible for
608 * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu
611 kstat_named_t arcstat_mfu_evictable_data
;
613 * Number of bytes consumed by ARC buffers that are eligible for
614 * eviction, of type ARC_BUFC_METADATA, and reside in the
617 kstat_named_t arcstat_mfu_evictable_metadata
;
619 * Total number of bytes that *would have been* consumed by ARC
620 * buffers in the arc_mfu_ghost state. See the comment above
621 * arcstat_mru_ghost_size for more details.
623 kstat_named_t arcstat_mfu_ghost_size
;
625 * Number of bytes that *would have been* consumed by ARC
626 * buffers that are eligible for eviction, of type
627 * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state.
629 kstat_named_t arcstat_mfu_ghost_evictable_data
;
631 * Number of bytes that *would have been* consumed by ARC
632 * buffers that are eligible for eviction, of type
633 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
635 kstat_named_t arcstat_mfu_ghost_evictable_metadata
;
636 kstat_named_t arcstat_l2_hits
;
637 kstat_named_t arcstat_l2_misses
;
638 kstat_named_t arcstat_l2_feeds
;
639 kstat_named_t arcstat_l2_rw_clash
;
640 kstat_named_t arcstat_l2_read_bytes
;
641 kstat_named_t arcstat_l2_write_bytes
;
642 kstat_named_t arcstat_l2_writes_sent
;
643 kstat_named_t arcstat_l2_writes_done
;
644 kstat_named_t arcstat_l2_writes_error
;
645 kstat_named_t arcstat_l2_writes_lock_retry
;
646 kstat_named_t arcstat_l2_evict_lock_retry
;
647 kstat_named_t arcstat_l2_evict_reading
;
648 kstat_named_t arcstat_l2_evict_l1cached
;
649 kstat_named_t arcstat_l2_free_on_write
;
650 kstat_named_t arcstat_l2_abort_lowmem
;
651 kstat_named_t arcstat_l2_cksum_bad
;
652 kstat_named_t arcstat_l2_io_error
;
653 kstat_named_t arcstat_l2_lsize
;
654 kstat_named_t arcstat_l2_psize
;
655 kstat_named_t arcstat_l2_hdr_size
;
656 kstat_named_t arcstat_memory_throttle_count
;
657 kstat_named_t arcstat_memory_direct_count
;
658 kstat_named_t arcstat_memory_indirect_count
;
659 kstat_named_t arcstat_memory_all_bytes
;
660 kstat_named_t arcstat_memory_free_bytes
;
661 kstat_named_t arcstat_memory_available_bytes
;
662 kstat_named_t arcstat_no_grow
;
663 kstat_named_t arcstat_tempreserve
;
664 kstat_named_t arcstat_loaned_bytes
;
665 kstat_named_t arcstat_prune
;
666 kstat_named_t arcstat_meta_used
;
667 kstat_named_t arcstat_meta_limit
;
668 kstat_named_t arcstat_dnode_limit
;
669 kstat_named_t arcstat_meta_max
;
670 kstat_named_t arcstat_meta_min
;
671 kstat_named_t arcstat_async_upgrade_sync
;
672 kstat_named_t arcstat_demand_hit_predictive_prefetch
;
673 kstat_named_t arcstat_demand_hit_prescient_prefetch
;
674 kstat_named_t arcstat_need_free
;
675 kstat_named_t arcstat_sys_free
;
676 kstat_named_t arcstat_raw_size
;
679 static arc_stats_t arc_stats
= {
680 { "hits", KSTAT_DATA_UINT64
},
681 { "misses", KSTAT_DATA_UINT64
},
682 { "demand_data_hits", KSTAT_DATA_UINT64
},
683 { "demand_data_misses", KSTAT_DATA_UINT64
},
684 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
685 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
686 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
687 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
688 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
689 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
690 { "mru_hits", KSTAT_DATA_UINT64
},
691 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
692 { "mfu_hits", KSTAT_DATA_UINT64
},
693 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
694 { "deleted", KSTAT_DATA_UINT64
},
695 { "mutex_miss", KSTAT_DATA_UINT64
},
696 { "access_skip", KSTAT_DATA_UINT64
},
697 { "evict_skip", KSTAT_DATA_UINT64
},
698 { "evict_not_enough", KSTAT_DATA_UINT64
},
699 { "evict_l2_cached", KSTAT_DATA_UINT64
},
700 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
701 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
702 { "evict_l2_skip", KSTAT_DATA_UINT64
},
703 { "hash_elements", KSTAT_DATA_UINT64
},
704 { "hash_elements_max", KSTAT_DATA_UINT64
},
705 { "hash_collisions", KSTAT_DATA_UINT64
},
706 { "hash_chains", KSTAT_DATA_UINT64
},
707 { "hash_chain_max", KSTAT_DATA_UINT64
},
708 { "p", KSTAT_DATA_UINT64
},
709 { "c", KSTAT_DATA_UINT64
},
710 { "c_min", KSTAT_DATA_UINT64
},
711 { "c_max", KSTAT_DATA_UINT64
},
712 { "size", KSTAT_DATA_UINT64
},
713 { "compressed_size", KSTAT_DATA_UINT64
},
714 { "uncompressed_size", KSTAT_DATA_UINT64
},
715 { "overhead_size", KSTAT_DATA_UINT64
},
716 { "hdr_size", KSTAT_DATA_UINT64
},
717 { "data_size", KSTAT_DATA_UINT64
},
718 { "metadata_size", KSTAT_DATA_UINT64
},
719 { "dbuf_size", KSTAT_DATA_UINT64
},
720 { "dnode_size", KSTAT_DATA_UINT64
},
721 { "bonus_size", KSTAT_DATA_UINT64
},
722 { "anon_size", KSTAT_DATA_UINT64
},
723 { "anon_evictable_data", KSTAT_DATA_UINT64
},
724 { "anon_evictable_metadata", KSTAT_DATA_UINT64
},
725 { "mru_size", KSTAT_DATA_UINT64
},
726 { "mru_evictable_data", KSTAT_DATA_UINT64
},
727 { "mru_evictable_metadata", KSTAT_DATA_UINT64
},
728 { "mru_ghost_size", KSTAT_DATA_UINT64
},
729 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64
},
730 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
731 { "mfu_size", KSTAT_DATA_UINT64
},
732 { "mfu_evictable_data", KSTAT_DATA_UINT64
},
733 { "mfu_evictable_metadata", KSTAT_DATA_UINT64
},
734 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
735 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64
},
736 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
737 { "l2_hits", KSTAT_DATA_UINT64
},
738 { "l2_misses", KSTAT_DATA_UINT64
},
739 { "l2_feeds", KSTAT_DATA_UINT64
},
740 { "l2_rw_clash", KSTAT_DATA_UINT64
},
741 { "l2_read_bytes", KSTAT_DATA_UINT64
},
742 { "l2_write_bytes", KSTAT_DATA_UINT64
},
743 { "l2_writes_sent", KSTAT_DATA_UINT64
},
744 { "l2_writes_done", KSTAT_DATA_UINT64
},
745 { "l2_writes_error", KSTAT_DATA_UINT64
},
746 { "l2_writes_lock_retry", KSTAT_DATA_UINT64
},
747 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
748 { "l2_evict_reading", KSTAT_DATA_UINT64
},
749 { "l2_evict_l1cached", KSTAT_DATA_UINT64
},
750 { "l2_free_on_write", KSTAT_DATA_UINT64
},
751 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
752 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
753 { "l2_io_error", KSTAT_DATA_UINT64
},
754 { "l2_size", KSTAT_DATA_UINT64
},
755 { "l2_asize", KSTAT_DATA_UINT64
},
756 { "l2_hdr_size", KSTAT_DATA_UINT64
},
757 { "memory_throttle_count", KSTAT_DATA_UINT64
},
758 { "memory_direct_count", KSTAT_DATA_UINT64
},
759 { "memory_indirect_count", KSTAT_DATA_UINT64
},
760 { "memory_all_bytes", KSTAT_DATA_UINT64
},
761 { "memory_free_bytes", KSTAT_DATA_UINT64
},
762 { "memory_available_bytes", KSTAT_DATA_INT64
},
763 { "arc_no_grow", KSTAT_DATA_UINT64
},
764 { "arc_tempreserve", KSTAT_DATA_UINT64
},
765 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
766 { "arc_prune", KSTAT_DATA_UINT64
},
767 { "arc_meta_used", KSTAT_DATA_UINT64
},
768 { "arc_meta_limit", KSTAT_DATA_UINT64
},
769 { "arc_dnode_limit", KSTAT_DATA_UINT64
},
770 { "arc_meta_max", KSTAT_DATA_UINT64
},
771 { "arc_meta_min", KSTAT_DATA_UINT64
},
772 { "async_upgrade_sync", KSTAT_DATA_UINT64
},
773 { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64
},
774 { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64
},
775 { "arc_need_free", KSTAT_DATA_UINT64
},
776 { "arc_sys_free", KSTAT_DATA_UINT64
},
777 { "arc_raw_size", KSTAT_DATA_UINT64
}
780 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
782 #define ARCSTAT_INCR(stat, val) \
783 atomic_add_64(&arc_stats.stat.value.ui64, (val))
785 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
786 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
788 #define ARCSTAT_MAX(stat, val) { \
790 while ((val) > (m = arc_stats.stat.value.ui64) && \
791 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
795 #define ARCSTAT_MAXSTAT(stat) \
796 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
799 * We define a macro to allow ARC hits/misses to be easily broken down by
800 * two separate conditions, giving a total of four different subtypes for
801 * each of hits and misses (so eight statistics total).
803 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
806 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
808 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
812 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
814 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
819 static arc_state_t
*arc_anon
;
820 static arc_state_t
*arc_mru
;
821 static arc_state_t
*arc_mru_ghost
;
822 static arc_state_t
*arc_mfu
;
823 static arc_state_t
*arc_mfu_ghost
;
824 static arc_state_t
*arc_l2c_only
;
827 * There are several ARC variables that are critical to export as kstats --
828 * but we don't want to have to grovel around in the kstat whenever we wish to
829 * manipulate them. For these variables, we therefore define them to be in
830 * terms of the statistic variable. This assures that we are not introducing
831 * the possibility of inconsistency by having shadow copies of the variables,
832 * while still allowing the code to be readable.
834 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
835 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
836 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
837 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
838 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
839 #define arc_no_grow ARCSTAT(arcstat_no_grow) /* do not grow cache size */
840 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
841 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
842 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
843 #define arc_dnode_limit ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */
844 #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */
845 #define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */
846 #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
847 #define arc_dbuf_size ARCSTAT(arcstat_dbuf_size) /* dbuf metadata */
848 #define arc_dnode_size ARCSTAT(arcstat_dnode_size) /* dnode metadata */
849 #define arc_bonus_size ARCSTAT(arcstat_bonus_size) /* bonus buffer metadata */
850 #define arc_need_free ARCSTAT(arcstat_need_free) /* bytes to be freed */
851 #define arc_sys_free ARCSTAT(arcstat_sys_free) /* target system free bytes */
853 /* size of all b_rabd's in entire arc */
854 #define arc_raw_size ARCSTAT(arcstat_raw_size)
855 /* compressed size of entire arc */
856 #define arc_compressed_size ARCSTAT(arcstat_compressed_size)
857 /* uncompressed size of entire arc */
858 #define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size)
859 /* number of bytes in the arc from arc_buf_t's */
860 #define arc_overhead_size ARCSTAT(arcstat_overhead_size)
862 static list_t arc_prune_list
;
863 static kmutex_t arc_prune_mtx
;
864 static taskq_t
*arc_prune_taskq
;
866 #define GHOST_STATE(state) \
867 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
868 (state) == arc_l2c_only)
870 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
871 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
872 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
873 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
874 #define HDR_PRESCIENT_PREFETCH(hdr) \
875 ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH)
876 #define HDR_COMPRESSION_ENABLED(hdr) \
877 ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC)
879 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
880 #define HDR_L2_READING(hdr) \
881 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
882 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
883 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
884 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
885 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
886 #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED)
887 #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH)
888 #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA)
890 #define HDR_ISTYPE_METADATA(hdr) \
891 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
892 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
894 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
895 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
896 #define HDR_HAS_RABD(hdr) \
897 (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \
898 (hdr)->b_crypt_hdr.b_rabd != NULL)
899 #define HDR_ENCRYPTED(hdr) \
900 (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
901 #define HDR_AUTHENTICATED(hdr) \
902 (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
904 /* For storing compression mode in b_flags */
905 #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1)
907 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \
908 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS))
909 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \
910 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp));
912 #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL)
913 #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED)
914 #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED)
915 #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED)
921 #define HDR_FULL_CRYPT_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
922 #define HDR_FULL_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_crypt_hdr))
923 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
926 * Hash table routines
929 #define HT_LOCK_ALIGN 64
930 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
935 unsigned char pad
[HT_LOCK_PAD
];
939 #define BUF_LOCKS 8192
940 typedef struct buf_hash_table
{
942 arc_buf_hdr_t
**ht_table
;
943 struct ht_lock ht_locks
[BUF_LOCKS
];
946 static buf_hash_table_t buf_hash_table
;
948 #define BUF_HASH_INDEX(spa, dva, birth) \
949 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
950 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
951 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
952 #define HDR_LOCK(hdr) \
953 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
955 uint64_t zfs_crc64_table
[256];
961 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
962 #define L2ARC_HEADROOM 2 /* num of writes */
965 * If we discover during ARC scan any buffers to be compressed, we boost
966 * our headroom for the next scanning cycle by this percentage multiple.
968 #define L2ARC_HEADROOM_BOOST 200
969 #define L2ARC_FEED_SECS 1 /* caching interval secs */
970 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
973 * We can feed L2ARC from two states of ARC buffers, mru and mfu,
974 * and each of the state has two types: data and metadata.
976 #define L2ARC_FEED_TYPES 4
978 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
979 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
981 /* L2ARC Performance Tunables */
982 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
983 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
984 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
985 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
986 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
987 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
988 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
989 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
990 int l2arc_norw
= B_FALSE
; /* no reads during writes */
995 static list_t L2ARC_dev_list
; /* device list */
996 static list_t
*l2arc_dev_list
; /* device list pointer */
997 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
998 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
999 static list_t L2ARC_free_on_write
; /* free after write buf list */
1000 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
1001 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
1002 static uint64_t l2arc_ndev
; /* number of devices */
1004 typedef struct l2arc_read_callback
{
1005 arc_buf_hdr_t
*l2rcb_hdr
; /* read header */
1006 blkptr_t l2rcb_bp
; /* original blkptr */
1007 zbookmark_phys_t l2rcb_zb
; /* original bookmark */
1008 int l2rcb_flags
; /* original flags */
1009 abd_t
*l2rcb_abd
; /* temporary buffer */
1010 } l2arc_read_callback_t
;
1012 typedef struct l2arc_data_free
{
1013 /* protected by l2arc_free_on_write_mtx */
1016 arc_buf_contents_t l2df_type
;
1017 list_node_t l2df_list_node
;
1018 } l2arc_data_free_t
;
1020 typedef enum arc_fill_flags
{
1021 ARC_FILL_LOCKED
= 1 << 0, /* hdr lock is held */
1022 ARC_FILL_COMPRESSED
= 1 << 1, /* fill with compressed data */
1023 ARC_FILL_ENCRYPTED
= 1 << 2, /* fill with encrypted data */
1024 ARC_FILL_NOAUTH
= 1 << 3, /* don't attempt to authenticate */
1025 ARC_FILL_IN_PLACE
= 1 << 4 /* fill in place (special case) */
1028 static kmutex_t l2arc_feed_thr_lock
;
1029 static kcondvar_t l2arc_feed_thr_cv
;
1030 static uint8_t l2arc_thread_exit
;
1032 static abd_t
*arc_get_data_abd(arc_buf_hdr_t
*, uint64_t, void *);
1033 static void *arc_get_data_buf(arc_buf_hdr_t
*, uint64_t, void *);
1034 static void arc_get_data_impl(arc_buf_hdr_t
*, uint64_t, void *);
1035 static void arc_free_data_abd(arc_buf_hdr_t
*, abd_t
*, uint64_t, void *);
1036 static void arc_free_data_buf(arc_buf_hdr_t
*, void *, uint64_t, void *);
1037 static void arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
);
1038 static void arc_hdr_free_abd(arc_buf_hdr_t
*, boolean_t
);
1039 static void arc_hdr_alloc_abd(arc_buf_hdr_t
*, boolean_t
);
1040 static void arc_access(arc_buf_hdr_t
*, kmutex_t
*);
1041 static boolean_t
arc_is_overflowing(void);
1042 static void arc_buf_watch(arc_buf_t
*);
1043 static void arc_tuning_update(void);
1044 static void arc_prune_async(int64_t);
1045 static uint64_t arc_all_memory(void);
1047 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t
*);
1048 static uint32_t arc_bufc_to_flags(arc_buf_contents_t
);
1049 static inline void arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
1050 static inline void arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
1052 static boolean_t
l2arc_write_eligible(uint64_t, arc_buf_hdr_t
*);
1053 static void l2arc_read_done(zio_t
*);
1056 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
1058 uint8_t *vdva
= (uint8_t *)dva
;
1059 uint64_t crc
= -1ULL;
1062 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
1064 for (i
= 0; i
< sizeof (dva_t
); i
++)
1065 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ vdva
[i
]) & 0xFF];
1067 crc
^= (spa
>>8) ^ birth
;
1072 #define HDR_EMPTY(hdr) \
1073 ((hdr)->b_dva.dva_word[0] == 0 && \
1074 (hdr)->b_dva.dva_word[1] == 0)
1076 #define HDR_EQUAL(spa, dva, birth, hdr) \
1077 ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
1078 ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
1079 ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa)
1082 buf_discard_identity(arc_buf_hdr_t
*hdr
)
1084 hdr
->b_dva
.dva_word
[0] = 0;
1085 hdr
->b_dva
.dva_word
[1] = 0;
1089 static arc_buf_hdr_t
*
1090 buf_hash_find(uint64_t spa
, const blkptr_t
*bp
, kmutex_t
**lockp
)
1092 const dva_t
*dva
= BP_IDENTITY(bp
);
1093 uint64_t birth
= BP_PHYSICAL_BIRTH(bp
);
1094 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
1095 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1098 mutex_enter(hash_lock
);
1099 for (hdr
= buf_hash_table
.ht_table
[idx
]; hdr
!= NULL
;
1100 hdr
= hdr
->b_hash_next
) {
1101 if (HDR_EQUAL(spa
, dva
, birth
, hdr
)) {
1106 mutex_exit(hash_lock
);
1112 * Insert an entry into the hash table. If there is already an element
1113 * equal to elem in the hash table, then the already existing element
1114 * will be returned and the new element will not be inserted.
1115 * Otherwise returns NULL.
1116 * If lockp == NULL, the caller is assumed to already hold the hash lock.
1118 static arc_buf_hdr_t
*
1119 buf_hash_insert(arc_buf_hdr_t
*hdr
, kmutex_t
**lockp
)
1121 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1122 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1123 arc_buf_hdr_t
*fhdr
;
1126 ASSERT(!DVA_IS_EMPTY(&hdr
->b_dva
));
1127 ASSERT(hdr
->b_birth
!= 0);
1128 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1130 if (lockp
!= NULL
) {
1132 mutex_enter(hash_lock
);
1134 ASSERT(MUTEX_HELD(hash_lock
));
1137 for (fhdr
= buf_hash_table
.ht_table
[idx
], i
= 0; fhdr
!= NULL
;
1138 fhdr
= fhdr
->b_hash_next
, i
++) {
1139 if (HDR_EQUAL(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
, fhdr
))
1143 hdr
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
1144 buf_hash_table
.ht_table
[idx
] = hdr
;
1145 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1147 /* collect some hash table performance data */
1149 ARCSTAT_BUMP(arcstat_hash_collisions
);
1151 ARCSTAT_BUMP(arcstat_hash_chains
);
1153 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
1156 ARCSTAT_BUMP(arcstat_hash_elements
);
1157 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
1163 buf_hash_remove(arc_buf_hdr_t
*hdr
)
1165 arc_buf_hdr_t
*fhdr
, **hdrp
;
1166 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1168 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
1169 ASSERT(HDR_IN_HASH_TABLE(hdr
));
1171 hdrp
= &buf_hash_table
.ht_table
[idx
];
1172 while ((fhdr
= *hdrp
) != hdr
) {
1173 ASSERT3P(fhdr
, !=, NULL
);
1174 hdrp
= &fhdr
->b_hash_next
;
1176 *hdrp
= hdr
->b_hash_next
;
1177 hdr
->b_hash_next
= NULL
;
1178 arc_hdr_clear_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1180 /* collect some hash table performance data */
1181 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
1183 if (buf_hash_table
.ht_table
[idx
] &&
1184 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
1185 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
1189 * Global data structures and functions for the buf kmem cache.
1192 static kmem_cache_t
*hdr_full_cache
;
1193 static kmem_cache_t
*hdr_full_crypt_cache
;
1194 static kmem_cache_t
*hdr_l2only_cache
;
1195 static kmem_cache_t
*buf_cache
;
1202 #if defined(_KERNEL) && defined(HAVE_SPL)
1204 * Large allocations which do not require contiguous pages
1205 * should be using vmem_free() in the linux kernel\
1207 vmem_free(buf_hash_table
.ht_table
,
1208 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1210 kmem_free(buf_hash_table
.ht_table
,
1211 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1213 for (i
= 0; i
< BUF_LOCKS
; i
++)
1214 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
1215 kmem_cache_destroy(hdr_full_cache
);
1216 kmem_cache_destroy(hdr_full_crypt_cache
);
1217 kmem_cache_destroy(hdr_l2only_cache
);
1218 kmem_cache_destroy(buf_cache
);
1222 * Constructor callback - called when the cache is empty
1223 * and a new buf is requested.
1227 hdr_full_cons(void *vbuf
, void *unused
, int kmflag
)
1229 arc_buf_hdr_t
*hdr
= vbuf
;
1231 bzero(hdr
, HDR_FULL_SIZE
);
1232 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
1233 cv_init(&hdr
->b_l1hdr
.b_cv
, NULL
, CV_DEFAULT
, NULL
);
1234 refcount_create(&hdr
->b_l1hdr
.b_refcnt
);
1235 mutex_init(&hdr
->b_l1hdr
.b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1236 list_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1237 list_link_init(&hdr
->b_l2hdr
.b_l2node
);
1238 multilist_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1239 arc_space_consume(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1246 hdr_full_crypt_cons(void *vbuf
, void *unused
, int kmflag
)
1248 arc_buf_hdr_t
*hdr
= vbuf
;
1250 hdr_full_cons(vbuf
, unused
, kmflag
);
1251 bzero(&hdr
->b_crypt_hdr
, sizeof (hdr
->b_crypt_hdr
));
1252 arc_space_consume(sizeof (hdr
->b_crypt_hdr
), ARC_SPACE_HDRS
);
1259 hdr_l2only_cons(void *vbuf
, void *unused
, int kmflag
)
1261 arc_buf_hdr_t
*hdr
= vbuf
;
1263 bzero(hdr
, HDR_L2ONLY_SIZE
);
1264 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1271 buf_cons(void *vbuf
, void *unused
, int kmflag
)
1273 arc_buf_t
*buf
= vbuf
;
1275 bzero(buf
, sizeof (arc_buf_t
));
1276 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1277 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1283 * Destructor callback - called when a cached buf is
1284 * no longer required.
1288 hdr_full_dest(void *vbuf
, void *unused
)
1290 arc_buf_hdr_t
*hdr
= vbuf
;
1292 ASSERT(HDR_EMPTY(hdr
));
1293 cv_destroy(&hdr
->b_l1hdr
.b_cv
);
1294 refcount_destroy(&hdr
->b_l1hdr
.b_refcnt
);
1295 mutex_destroy(&hdr
->b_l1hdr
.b_freeze_lock
);
1296 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1297 arc_space_return(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1302 hdr_full_crypt_dest(void *vbuf
, void *unused
)
1304 arc_buf_hdr_t
*hdr
= vbuf
;
1306 hdr_full_dest(vbuf
, unused
);
1307 arc_space_return(sizeof (hdr
->b_crypt_hdr
), ARC_SPACE_HDRS
);
1312 hdr_l2only_dest(void *vbuf
, void *unused
)
1314 ASSERTV(arc_buf_hdr_t
*hdr
= vbuf
);
1316 ASSERT(HDR_EMPTY(hdr
));
1317 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1322 buf_dest(void *vbuf
, void *unused
)
1324 arc_buf_t
*buf
= vbuf
;
1326 mutex_destroy(&buf
->b_evict_lock
);
1327 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1331 * Reclaim callback -- invoked when memory is low.
1335 hdr_recl(void *unused
)
1337 dprintf("hdr_recl called\n");
1339 * umem calls the reclaim func when we destroy the buf cache,
1340 * which is after we do arc_fini().
1343 cv_signal(&arc_reclaim_thread_cv
);
1349 uint64_t *ct
= NULL
;
1350 uint64_t hsize
= 1ULL << 12;
1354 * The hash table is big enough to fill all of physical memory
1355 * with an average block size of zfs_arc_average_blocksize (default 8K).
1356 * By default, the table will take up
1357 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1359 while (hsize
* zfs_arc_average_blocksize
< arc_all_memory())
1362 buf_hash_table
.ht_mask
= hsize
- 1;
1363 #if defined(_KERNEL) && defined(HAVE_SPL)
1365 * Large allocations which do not require contiguous pages
1366 * should be using vmem_alloc() in the linux kernel
1368 buf_hash_table
.ht_table
=
1369 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1371 buf_hash_table
.ht_table
=
1372 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1374 if (buf_hash_table
.ht_table
== NULL
) {
1375 ASSERT(hsize
> (1ULL << 8));
1380 hdr_full_cache
= kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE
,
1381 0, hdr_full_cons
, hdr_full_dest
, hdr_recl
, NULL
, NULL
, 0);
1382 hdr_full_crypt_cache
= kmem_cache_create("arc_buf_hdr_t_full_crypt",
1383 HDR_FULL_CRYPT_SIZE
, 0, hdr_full_crypt_cons
, hdr_full_crypt_dest
,
1384 hdr_recl
, NULL
, NULL
, 0);
1385 hdr_l2only_cache
= kmem_cache_create("arc_buf_hdr_t_l2only",
1386 HDR_L2ONLY_SIZE
, 0, hdr_l2only_cons
, hdr_l2only_dest
, hdr_recl
,
1388 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1389 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1391 for (i
= 0; i
< 256; i
++)
1392 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1393 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1395 for (i
= 0; i
< BUF_LOCKS
; i
++) {
1396 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
1397 NULL
, MUTEX_DEFAULT
, NULL
);
1401 #define ARC_MINTIME (hz>>4) /* 62 ms */
1404 * This is the size that the buf occupies in memory. If the buf is compressed,
1405 * it will correspond to the compressed size. You should use this method of
1406 * getting the buf size unless you explicitly need the logical size.
1409 arc_buf_size(arc_buf_t
*buf
)
1411 return (ARC_BUF_COMPRESSED(buf
) ?
1412 HDR_GET_PSIZE(buf
->b_hdr
) : HDR_GET_LSIZE(buf
->b_hdr
));
1416 arc_buf_lsize(arc_buf_t
*buf
)
1418 return (HDR_GET_LSIZE(buf
->b_hdr
));
1422 * This function will return B_TRUE if the buffer is encrypted in memory.
1423 * This buffer can be decrypted by calling arc_untransform().
1426 arc_is_encrypted(arc_buf_t
*buf
)
1428 return (ARC_BUF_ENCRYPTED(buf
) != 0);
1432 * Returns B_TRUE if the buffer represents data that has not had its MAC
1436 arc_is_unauthenticated(arc_buf_t
*buf
)
1438 return (HDR_NOAUTH(buf
->b_hdr
) != 0);
1442 arc_get_raw_params(arc_buf_t
*buf
, boolean_t
*byteorder
, uint8_t *salt
,
1443 uint8_t *iv
, uint8_t *mac
)
1445 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1447 ASSERT(HDR_PROTECTED(hdr
));
1449 bcopy(hdr
->b_crypt_hdr
.b_salt
, salt
, ZIO_DATA_SALT_LEN
);
1450 bcopy(hdr
->b_crypt_hdr
.b_iv
, iv
, ZIO_DATA_IV_LEN
);
1451 bcopy(hdr
->b_crypt_hdr
.b_mac
, mac
, ZIO_DATA_MAC_LEN
);
1452 *byteorder
= (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
1453 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
1457 * Indicates how this buffer is compressed in memory. If it is not compressed
1458 * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with
1459 * arc_untransform() as long as it is also unencrypted.
1462 arc_get_compression(arc_buf_t
*buf
)
1464 return (ARC_BUF_COMPRESSED(buf
) ?
1465 HDR_GET_COMPRESS(buf
->b_hdr
) : ZIO_COMPRESS_OFF
);
1469 * Return the compression algorithm used to store this data in the ARC. If ARC
1470 * compression is enabled or this is an encrypted block, this will be the same
1471 * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF.
1473 static inline enum zio_compress
1474 arc_hdr_get_compress(arc_buf_hdr_t
*hdr
)
1476 return (HDR_COMPRESSION_ENABLED(hdr
) ?
1477 HDR_GET_COMPRESS(hdr
) : ZIO_COMPRESS_OFF
);
1480 static inline boolean_t
1481 arc_buf_is_shared(arc_buf_t
*buf
)
1483 boolean_t shared
= (buf
->b_data
!= NULL
&&
1484 buf
->b_hdr
->b_l1hdr
.b_pabd
!= NULL
&&
1485 abd_is_linear(buf
->b_hdr
->b_l1hdr
.b_pabd
) &&
1486 buf
->b_data
== abd_to_buf(buf
->b_hdr
->b_l1hdr
.b_pabd
));
1487 IMPLY(shared
, HDR_SHARED_DATA(buf
->b_hdr
));
1488 IMPLY(shared
, ARC_BUF_SHARED(buf
));
1489 IMPLY(shared
, ARC_BUF_COMPRESSED(buf
) || ARC_BUF_LAST(buf
));
1492 * It would be nice to assert arc_can_share() too, but the "hdr isn't
1493 * already being shared" requirement prevents us from doing that.
1500 * Free the checksum associated with this header. If there is no checksum, this
1504 arc_cksum_free(arc_buf_hdr_t
*hdr
)
1506 ASSERT(HDR_HAS_L1HDR(hdr
));
1508 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1509 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
) {
1510 kmem_free(hdr
->b_l1hdr
.b_freeze_cksum
, sizeof (zio_cksum_t
));
1511 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
1513 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1517 * Return true iff at least one of the bufs on hdr is not compressed.
1518 * Encrypted buffers count as compressed.
1521 arc_hdr_has_uncompressed_buf(arc_buf_hdr_t
*hdr
)
1523 for (arc_buf_t
*b
= hdr
->b_l1hdr
.b_buf
; b
!= NULL
; b
= b
->b_next
) {
1524 if (!ARC_BUF_COMPRESSED(b
)) {
1533 * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data
1534 * matches the checksum that is stored in the hdr. If there is no checksum,
1535 * or if the buf is compressed, this is a no-op.
1538 arc_cksum_verify(arc_buf_t
*buf
)
1540 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1543 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1546 if (ARC_BUF_COMPRESSED(buf
)) {
1547 ASSERT(hdr
->b_l1hdr
.b_freeze_cksum
== NULL
||
1548 arc_hdr_has_uncompressed_buf(hdr
));
1552 ASSERT(HDR_HAS_L1HDR(hdr
));
1554 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1555 if (hdr
->b_l1hdr
.b_freeze_cksum
== NULL
|| HDR_IO_ERROR(hdr
)) {
1556 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1560 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
, &zc
);
1561 if (!ZIO_CHECKSUM_EQUAL(*hdr
->b_l1hdr
.b_freeze_cksum
, zc
))
1562 panic("buffer modified while frozen!");
1563 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1567 * This function makes the assumption that data stored in the L2ARC
1568 * will be transformed exactly as it is in the main pool. Because of
1569 * this we can verify the checksum against the reading process's bp.
1572 arc_cksum_is_equal(arc_buf_hdr_t
*hdr
, zio_t
*zio
)
1574 ASSERT(!BP_IS_EMBEDDED(zio
->io_bp
));
1575 VERIFY3U(BP_GET_PSIZE(zio
->io_bp
), ==, HDR_GET_PSIZE(hdr
));
1578 * Block pointers always store the checksum for the logical data.
1579 * If the block pointer has the gang bit set, then the checksum
1580 * it represents is for the reconstituted data and not for an
1581 * individual gang member. The zio pipeline, however, must be able to
1582 * determine the checksum of each of the gang constituents so it
1583 * treats the checksum comparison differently than what we need
1584 * for l2arc blocks. This prevents us from using the
1585 * zio_checksum_error() interface directly. Instead we must call the
1586 * zio_checksum_error_impl() so that we can ensure the checksum is
1587 * generated using the correct checksum algorithm and accounts for the
1588 * logical I/O size and not just a gang fragment.
1590 return (zio_checksum_error_impl(zio
->io_spa
, zio
->io_bp
,
1591 BP_GET_CHECKSUM(zio
->io_bp
), zio
->io_abd
, zio
->io_size
,
1592 zio
->io_offset
, NULL
) == 0);
1596 * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a
1597 * checksum and attaches it to the buf's hdr so that we can ensure that the buf
1598 * isn't modified later on. If buf is compressed or there is already a checksum
1599 * on the hdr, this is a no-op (we only checksum uncompressed bufs).
1602 arc_cksum_compute(arc_buf_t
*buf
)
1604 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1606 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1609 ASSERT(HDR_HAS_L1HDR(hdr
));
1611 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1612 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
) {
1613 ASSERT(arc_hdr_has_uncompressed_buf(hdr
));
1614 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1616 } else if (ARC_BUF_COMPRESSED(buf
)) {
1617 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1621 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
1622 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1623 hdr
->b_l1hdr
.b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1625 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
,
1626 hdr
->b_l1hdr
.b_freeze_cksum
);
1627 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1633 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1635 panic("Got SIGSEGV at address: 0x%lx\n", (long)si
->si_addr
);
1641 arc_buf_unwatch(arc_buf_t
*buf
)
1645 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1646 PROT_READ
| PROT_WRITE
));
1653 arc_buf_watch(arc_buf_t
*buf
)
1657 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1662 static arc_buf_contents_t
1663 arc_buf_type(arc_buf_hdr_t
*hdr
)
1665 arc_buf_contents_t type
;
1666 if (HDR_ISTYPE_METADATA(hdr
)) {
1667 type
= ARC_BUFC_METADATA
;
1669 type
= ARC_BUFC_DATA
;
1671 VERIFY3U(hdr
->b_type
, ==, type
);
1676 arc_is_metadata(arc_buf_t
*buf
)
1678 return (HDR_ISTYPE_METADATA(buf
->b_hdr
) != 0);
1682 arc_bufc_to_flags(arc_buf_contents_t type
)
1686 /* metadata field is 0 if buffer contains normal data */
1688 case ARC_BUFC_METADATA
:
1689 return (ARC_FLAG_BUFC_METADATA
);
1693 panic("undefined ARC buffer type!");
1694 return ((uint32_t)-1);
1698 arc_buf_thaw(arc_buf_t
*buf
)
1700 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1702 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
1703 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1705 arc_cksum_verify(buf
);
1708 * Compressed buffers do not manipulate the b_freeze_cksum or
1709 * allocate b_thawed.
1711 if (ARC_BUF_COMPRESSED(buf
)) {
1712 ASSERT(hdr
->b_l1hdr
.b_freeze_cksum
== NULL
||
1713 arc_hdr_has_uncompressed_buf(hdr
));
1717 ASSERT(HDR_HAS_L1HDR(hdr
));
1718 arc_cksum_free(hdr
);
1719 arc_buf_unwatch(buf
);
1723 arc_buf_freeze(arc_buf_t
*buf
)
1725 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1726 kmutex_t
*hash_lock
;
1728 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1731 if (ARC_BUF_COMPRESSED(buf
)) {
1732 ASSERT(hdr
->b_l1hdr
.b_freeze_cksum
== NULL
||
1733 arc_hdr_has_uncompressed_buf(hdr
));
1737 hash_lock
= HDR_LOCK(hdr
);
1738 mutex_enter(hash_lock
);
1740 ASSERT(HDR_HAS_L1HDR(hdr
));
1741 ASSERT(hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
||
1742 hdr
->b_l1hdr
.b_state
== arc_anon
);
1743 arc_cksum_compute(buf
);
1744 mutex_exit(hash_lock
);
1748 * The arc_buf_hdr_t's b_flags should never be modified directly. Instead,
1749 * the following functions should be used to ensure that the flags are
1750 * updated in a thread-safe way. When manipulating the flags either
1751 * the hash_lock must be held or the hdr must be undiscoverable. This
1752 * ensures that we're not racing with any other threads when updating
1756 arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1758 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
1759 hdr
->b_flags
|= flags
;
1763 arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1765 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
1766 hdr
->b_flags
&= ~flags
;
1770 * Setting the compression bits in the arc_buf_hdr_t's b_flags is
1771 * done in a special way since we have to clear and set bits
1772 * at the same time. Consumers that wish to set the compression bits
1773 * must use this function to ensure that the flags are updated in
1774 * thread-safe manner.
1777 arc_hdr_set_compress(arc_buf_hdr_t
*hdr
, enum zio_compress cmp
)
1779 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
1782 * Holes and embedded blocks will always have a psize = 0 so
1783 * we ignore the compression of the blkptr and set the
1784 * want to uncompress them. Mark them as uncompressed.
1786 if (!zfs_compressed_arc_enabled
|| HDR_GET_PSIZE(hdr
) == 0) {
1787 arc_hdr_clear_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1788 ASSERT(!HDR_COMPRESSION_ENABLED(hdr
));
1790 arc_hdr_set_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1791 ASSERT(HDR_COMPRESSION_ENABLED(hdr
));
1794 HDR_SET_COMPRESS(hdr
, cmp
);
1795 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, cmp
);
1799 * Looks for another buf on the same hdr which has the data decompressed, copies
1800 * from it, and returns true. If no such buf exists, returns false.
1803 arc_buf_try_copy_decompressed_data(arc_buf_t
*buf
)
1805 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1806 boolean_t copied
= B_FALSE
;
1808 ASSERT(HDR_HAS_L1HDR(hdr
));
1809 ASSERT3P(buf
->b_data
, !=, NULL
);
1810 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1812 for (arc_buf_t
*from
= hdr
->b_l1hdr
.b_buf
; from
!= NULL
;
1813 from
= from
->b_next
) {
1814 /* can't use our own data buffer */
1819 if (!ARC_BUF_COMPRESSED(from
)) {
1820 bcopy(from
->b_data
, buf
->b_data
, arc_buf_size(buf
));
1827 * There were no decompressed bufs, so there should not be a
1828 * checksum on the hdr either.
1830 EQUIV(!copied
, hdr
->b_l1hdr
.b_freeze_cksum
== NULL
);
1836 * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t.
1839 arc_hdr_size(arc_buf_hdr_t
*hdr
)
1843 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
1844 HDR_GET_PSIZE(hdr
) > 0) {
1845 size
= HDR_GET_PSIZE(hdr
);
1847 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, 0);
1848 size
= HDR_GET_LSIZE(hdr
);
1854 arc_hdr_authenticate(arc_buf_hdr_t
*hdr
, spa_t
*spa
, uint64_t dsobj
)
1858 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
1859 uint64_t psize
= HDR_GET_PSIZE(hdr
);
1860 void *tmpbuf
= NULL
;
1861 abd_t
*abd
= hdr
->b_l1hdr
.b_pabd
;
1863 ASSERT(HDR_LOCK(hdr
) == NULL
|| MUTEX_HELD(HDR_LOCK(hdr
)));
1864 ASSERT(HDR_AUTHENTICATED(hdr
));
1865 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1868 * The MAC is calculated on the compressed data that is stored on disk.
1869 * However, if compressed arc is disabled we will only have the
1870 * decompressed data available to us now. Compress it into a temporary
1871 * abd so we can verify the MAC. The performance overhead of this will
1872 * be relatively low, since most objects in an encrypted objset will
1873 * be encrypted (instead of authenticated) anyway.
1875 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1876 !HDR_COMPRESSION_ENABLED(hdr
)) {
1877 tmpbuf
= zio_buf_alloc(lsize
);
1878 abd
= abd_get_from_buf(tmpbuf
, lsize
);
1879 abd_take_ownership_of_buf(abd
, B_TRUE
);
1881 csize
= zio_compress_data(HDR_GET_COMPRESS(hdr
),
1882 hdr
->b_l1hdr
.b_pabd
, tmpbuf
, lsize
);
1883 ASSERT3U(csize
, <=, psize
);
1884 abd_zero_off(abd
, csize
, psize
- csize
);
1888 * Authentication is best effort. We authenticate whenever the key is
1889 * available. If we succeed we clear ARC_FLAG_NOAUTH.
1891 if (hdr
->b_crypt_hdr
.b_ot
== DMU_OT_OBJSET
) {
1892 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, ZIO_COMPRESS_OFF
);
1893 ASSERT3U(lsize
, ==, psize
);
1894 ret
= spa_do_crypt_objset_mac_abd(B_FALSE
, spa
, dsobj
, abd
,
1895 psize
, hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1897 ret
= spa_do_crypt_mac_abd(B_FALSE
, spa
, dsobj
, abd
, psize
,
1898 hdr
->b_crypt_hdr
.b_mac
);
1902 arc_hdr_clear_flags(hdr
, ARC_FLAG_NOAUTH
);
1903 else if (ret
!= ENOENT
)
1919 * This function will take a header that only has raw encrypted data in
1920 * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in
1921 * b_l1hdr.b_pabd. If designated in the header flags, this function will
1922 * also decompress the data.
1925 arc_hdr_decrypt(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
)
1930 boolean_t no_crypt
= B_FALSE
;
1931 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1933 ASSERT(HDR_LOCK(hdr
) == NULL
|| MUTEX_HELD(HDR_LOCK(hdr
)));
1934 ASSERT(HDR_ENCRYPTED(hdr
));
1936 arc_hdr_alloc_abd(hdr
, B_FALSE
);
1938 ret
= spa_do_crypt_abd(B_FALSE
, spa
, zb
, hdr
->b_crypt_hdr
.b_ot
,
1939 B_FALSE
, bswap
, hdr
->b_crypt_hdr
.b_salt
, hdr
->b_crypt_hdr
.b_iv
,
1940 hdr
->b_crypt_hdr
.b_mac
, HDR_GET_PSIZE(hdr
), hdr
->b_l1hdr
.b_pabd
,
1941 hdr
->b_crypt_hdr
.b_rabd
, &no_crypt
);
1946 abd_copy(hdr
->b_l1hdr
.b_pabd
, hdr
->b_crypt_hdr
.b_rabd
,
1947 HDR_GET_PSIZE(hdr
));
1951 * If this header has disabled arc compression but the b_pabd is
1952 * compressed after decrypting it, we need to decompress the newly
1955 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1956 !HDR_COMPRESSION_ENABLED(hdr
)) {
1958 * We want to make sure that we are correctly honoring the
1959 * zfs_abd_scatter_enabled setting, so we allocate an abd here
1960 * and then loan a buffer from it, rather than allocating a
1961 * linear buffer and wrapping it in an abd later.
1963 cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
);
1964 tmp
= abd_borrow_buf(cabd
, arc_hdr_size(hdr
));
1966 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
1967 hdr
->b_l1hdr
.b_pabd
, tmp
, HDR_GET_PSIZE(hdr
),
1968 HDR_GET_LSIZE(hdr
));
1970 abd_return_buf(cabd
, tmp
, arc_hdr_size(hdr
));
1974 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
1975 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
1976 arc_hdr_size(hdr
), hdr
);
1977 hdr
->b_l1hdr
.b_pabd
= cabd
;
1983 arc_hdr_free_abd(hdr
, B_FALSE
);
1985 arc_free_data_buf(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
1991 * This function is called during arc_buf_fill() to prepare the header's
1992 * abd plaintext pointer for use. This involves authenticated protected
1993 * data and decrypting encrypted data into the plaintext abd.
1996 arc_fill_hdr_crypt(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, spa_t
*spa
,
1997 const zbookmark_phys_t
*zb
, boolean_t noauth
)
2001 ASSERT(HDR_PROTECTED(hdr
));
2003 if (hash_lock
!= NULL
)
2004 mutex_enter(hash_lock
);
2006 if (HDR_NOAUTH(hdr
) && !noauth
) {
2008 * The caller requested authenticated data but our data has
2009 * not been authenticated yet. Verify the MAC now if we can.
2011 ret
= arc_hdr_authenticate(hdr
, spa
, zb
->zb_objset
);
2014 } else if (HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
== NULL
) {
2016 * If we only have the encrypted version of the data, but the
2017 * unencrypted version was requested we take this opportunity
2018 * to store the decrypted version in the header for future use.
2020 ret
= arc_hdr_decrypt(hdr
, spa
, zb
);
2025 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
2027 if (hash_lock
!= NULL
)
2028 mutex_exit(hash_lock
);
2033 if (hash_lock
!= NULL
)
2034 mutex_exit(hash_lock
);
2040 * This function is used by the dbuf code to decrypt bonus buffers in place.
2041 * The dbuf code itself doesn't have any locking for decrypting a shared dnode
2042 * block, so we use the hash lock here to protect against concurrent calls to
2046 arc_buf_untransform_in_place(arc_buf_t
*buf
, kmutex_t
*hash_lock
)
2048 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2050 ASSERT(HDR_ENCRYPTED(hdr
));
2051 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
2052 ASSERT(HDR_LOCK(hdr
) == NULL
|| MUTEX_HELD(HDR_LOCK(hdr
)));
2053 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
2055 zio_crypt_copy_dnode_bonus(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
2057 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
2058 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
2059 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
2063 * Given a buf that has a data buffer attached to it, this function will
2064 * efficiently fill the buf with data of the specified compression setting from
2065 * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr
2066 * are already sharing a data buf, no copy is performed.
2068 * If the buf is marked as compressed but uncompressed data was requested, this
2069 * will allocate a new data buffer for the buf, remove that flag, and fill the
2070 * buf with uncompressed data. You can't request a compressed buf on a hdr with
2071 * uncompressed data, and (since we haven't added support for it yet) if you
2072 * want compressed data your buf must already be marked as compressed and have
2073 * the correct-sized data buffer.
2076 arc_buf_fill(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2077 arc_fill_flags_t flags
)
2080 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2081 boolean_t hdr_compressed
=
2082 (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
2083 boolean_t compressed
= (flags
& ARC_FILL_COMPRESSED
) != 0;
2084 boolean_t encrypted
= (flags
& ARC_FILL_ENCRYPTED
) != 0;
2085 dmu_object_byteswap_t bswap
= hdr
->b_l1hdr
.b_byteswap
;
2086 kmutex_t
*hash_lock
= (flags
& ARC_FILL_LOCKED
) ? NULL
: HDR_LOCK(hdr
);
2088 ASSERT3P(buf
->b_data
, !=, NULL
);
2089 IMPLY(compressed
, hdr_compressed
|| ARC_BUF_ENCRYPTED(buf
));
2090 IMPLY(compressed
, ARC_BUF_COMPRESSED(buf
));
2091 IMPLY(encrypted
, HDR_ENCRYPTED(hdr
));
2092 IMPLY(encrypted
, ARC_BUF_ENCRYPTED(buf
));
2093 IMPLY(encrypted
, ARC_BUF_COMPRESSED(buf
));
2094 IMPLY(encrypted
, !ARC_BUF_SHARED(buf
));
2097 * If the caller wanted encrypted data we just need to copy it from
2098 * b_rabd and potentially byteswap it. We won't be able to do any
2099 * further transforms on it.
2102 ASSERT(HDR_HAS_RABD(hdr
));
2103 abd_copy_to_buf(buf
->b_data
, hdr
->b_crypt_hdr
.b_rabd
,
2104 HDR_GET_PSIZE(hdr
));
2109 * Adjust encrypted and authenticated headers to accomodate the
2110 * request if needed.
2112 if (HDR_PROTECTED(hdr
)) {
2113 error
= arc_fill_hdr_crypt(hdr
, hash_lock
, spa
,
2114 zb
, !!(flags
& ARC_FILL_NOAUTH
));
2116 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2122 * There is a special case here for dnode blocks which are
2123 * decrypting their bonus buffers. These blocks may request to
2124 * be decrypted in-place. This is necessary because there may
2125 * be many dnodes pointing into this buffer and there is
2126 * currently no method to synchronize replacing the backing
2127 * b_data buffer and updating all of the pointers. Here we use
2128 * the hash lock to ensure there are no races. If the need
2129 * arises for other types to be decrypted in-place, they must
2130 * add handling here as well.
2132 if ((flags
& ARC_FILL_IN_PLACE
) != 0) {
2133 ASSERT(!hdr_compressed
);
2134 ASSERT(!compressed
);
2137 if (HDR_ENCRYPTED(hdr
) && ARC_BUF_ENCRYPTED(buf
)) {
2138 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
2140 if (hash_lock
!= NULL
)
2141 mutex_enter(hash_lock
);
2142 arc_buf_untransform_in_place(buf
, hash_lock
);
2143 if (hash_lock
!= NULL
)
2144 mutex_exit(hash_lock
);
2146 /* Compute the hdr's checksum if necessary */
2147 arc_cksum_compute(buf
);
2153 if (hdr_compressed
== compressed
) {
2154 if (!arc_buf_is_shared(buf
)) {
2155 abd_copy_to_buf(buf
->b_data
, hdr
->b_l1hdr
.b_pabd
,
2159 ASSERT(hdr_compressed
);
2160 ASSERT(!compressed
);
2161 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, HDR_GET_PSIZE(hdr
));
2164 * If the buf is sharing its data with the hdr, unlink it and
2165 * allocate a new data buffer for the buf.
2167 if (arc_buf_is_shared(buf
)) {
2168 ASSERT(ARC_BUF_COMPRESSED(buf
));
2170 /* We need to give the buf it's own b_data */
2171 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
2173 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2174 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2176 /* Previously overhead was 0; just add new overhead */
2177 ARCSTAT_INCR(arcstat_overhead_size
, HDR_GET_LSIZE(hdr
));
2178 } else if (ARC_BUF_COMPRESSED(buf
)) {
2179 /* We need to reallocate the buf's b_data */
2180 arc_free_data_buf(hdr
, buf
->b_data
, HDR_GET_PSIZE(hdr
),
2183 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2185 /* We increased the size of b_data; update overhead */
2186 ARCSTAT_INCR(arcstat_overhead_size
,
2187 HDR_GET_LSIZE(hdr
) - HDR_GET_PSIZE(hdr
));
2191 * Regardless of the buf's previous compression settings, it
2192 * should not be compressed at the end of this function.
2194 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
2197 * Try copying the data from another buf which already has a
2198 * decompressed version. If that's not possible, it's time to
2199 * bite the bullet and decompress the data from the hdr.
2201 if (arc_buf_try_copy_decompressed_data(buf
)) {
2202 /* Skip byteswapping and checksumming (already done) */
2203 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, !=, NULL
);
2206 error
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
2207 hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
2208 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
2211 * Absent hardware errors or software bugs, this should
2212 * be impossible, but log it anyway so we can debug it.
2216 "hdr %p, compress %d, psize %d, lsize %d",
2217 hdr
, arc_hdr_get_compress(hdr
),
2218 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
2219 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2220 return (SET_ERROR(EIO
));
2226 /* Byteswap the buf's data if necessary */
2227 if (bswap
!= DMU_BSWAP_NUMFUNCS
) {
2228 ASSERT(!HDR_SHARED_DATA(hdr
));
2229 ASSERT3U(bswap
, <, DMU_BSWAP_NUMFUNCS
);
2230 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, HDR_GET_LSIZE(hdr
));
2233 /* Compute the hdr's checksum if necessary */
2234 arc_cksum_compute(buf
);
2240 * If this function is being called to decrypt an encrypted buffer or verify an
2241 * authenticated one, the key must be loaded and a mapping must be made
2242 * available in the keystore via spa_keystore_create_mapping() or one of its
2246 arc_untransform(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2250 arc_fill_flags_t flags
= 0;
2253 flags
|= ARC_FILL_IN_PLACE
;
2255 ret
= arc_buf_fill(buf
, spa
, zb
, flags
);
2256 if (ret
== ECKSUM
) {
2258 * Convert authentication and decryption errors to EIO
2259 * (and generate an ereport) before leaving the ARC.
2261 ret
= SET_ERROR(EIO
);
2262 spa_log_error(spa
, zb
);
2263 zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION
,
2264 spa
, NULL
, zb
, NULL
, 0, 0);
2271 * Increment the amount of evictable space in the arc_state_t's refcount.
2272 * We account for the space used by the hdr and the arc buf individually
2273 * so that we can add and remove them from the refcount individually.
2276 arc_evictable_space_increment(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2278 arc_buf_contents_t type
= arc_buf_type(hdr
);
2280 ASSERT(HDR_HAS_L1HDR(hdr
));
2282 if (GHOST_STATE(state
)) {
2283 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2284 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2285 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2286 ASSERT(!HDR_HAS_RABD(hdr
));
2287 (void) refcount_add_many(&state
->arcs_esize
[type
],
2288 HDR_GET_LSIZE(hdr
), hdr
);
2292 ASSERT(!GHOST_STATE(state
));
2293 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2294 (void) refcount_add_many(&state
->arcs_esize
[type
],
2295 arc_hdr_size(hdr
), hdr
);
2297 if (HDR_HAS_RABD(hdr
)) {
2298 (void) refcount_add_many(&state
->arcs_esize
[type
],
2299 HDR_GET_PSIZE(hdr
), hdr
);
2302 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2303 buf
= buf
->b_next
) {
2304 if (arc_buf_is_shared(buf
))
2306 (void) refcount_add_many(&state
->arcs_esize
[type
],
2307 arc_buf_size(buf
), buf
);
2312 * Decrement the amount of evictable space in the arc_state_t's refcount.
2313 * We account for the space used by the hdr and the arc buf individually
2314 * so that we can add and remove them from the refcount individually.
2317 arc_evictable_space_decrement(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2319 arc_buf_contents_t type
= arc_buf_type(hdr
);
2321 ASSERT(HDR_HAS_L1HDR(hdr
));
2323 if (GHOST_STATE(state
)) {
2324 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2325 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2326 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2327 ASSERT(!HDR_HAS_RABD(hdr
));
2328 (void) refcount_remove_many(&state
->arcs_esize
[type
],
2329 HDR_GET_LSIZE(hdr
), hdr
);
2333 ASSERT(!GHOST_STATE(state
));
2334 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2335 (void) refcount_remove_many(&state
->arcs_esize
[type
],
2336 arc_hdr_size(hdr
), hdr
);
2338 if (HDR_HAS_RABD(hdr
)) {
2339 (void) refcount_remove_many(&state
->arcs_esize
[type
],
2340 HDR_GET_PSIZE(hdr
), hdr
);
2343 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2344 buf
= buf
->b_next
) {
2345 if (arc_buf_is_shared(buf
))
2347 (void) refcount_remove_many(&state
->arcs_esize
[type
],
2348 arc_buf_size(buf
), buf
);
2353 * Add a reference to this hdr indicating that someone is actively
2354 * referencing that memory. When the refcount transitions from 0 to 1,
2355 * we remove it from the respective arc_state_t list to indicate that
2356 * it is not evictable.
2359 add_reference(arc_buf_hdr_t
*hdr
, void *tag
)
2363 ASSERT(HDR_HAS_L1HDR(hdr
));
2364 if (!MUTEX_HELD(HDR_LOCK(hdr
))) {
2365 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
2366 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2367 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2370 state
= hdr
->b_l1hdr
.b_state
;
2372 if ((refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
) == 1) &&
2373 (state
!= arc_anon
)) {
2374 /* We don't use the L2-only state list. */
2375 if (state
!= arc_l2c_only
) {
2376 multilist_remove(state
->arcs_list
[arc_buf_type(hdr
)],
2378 arc_evictable_space_decrement(hdr
, state
);
2380 /* remove the prefetch flag if we get a reference */
2381 arc_hdr_clear_flags(hdr
, ARC_FLAG_PREFETCH
);
2386 * Remove a reference from this hdr. When the reference transitions from
2387 * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's
2388 * list making it eligible for eviction.
2391 remove_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
2394 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2396 ASSERT(HDR_HAS_L1HDR(hdr
));
2397 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
2398 ASSERT(!GHOST_STATE(state
));
2401 * arc_l2c_only counts as a ghost state so we don't need to explicitly
2402 * check to prevent usage of the arc_l2c_only list.
2404 if (((cnt
= refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
)) == 0) &&
2405 (state
!= arc_anon
)) {
2406 multilist_insert(state
->arcs_list
[arc_buf_type(hdr
)], hdr
);
2407 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
2408 arc_evictable_space_increment(hdr
, state
);
2414 * Returns detailed information about a specific arc buffer. When the
2415 * state_index argument is set the function will calculate the arc header
2416 * list position for its arc state. Since this requires a linear traversal
2417 * callers are strongly encourage not to do this. However, it can be helpful
2418 * for targeted analysis so the functionality is provided.
2421 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
2423 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
2424 l1arc_buf_hdr_t
*l1hdr
= NULL
;
2425 l2arc_buf_hdr_t
*l2hdr
= NULL
;
2426 arc_state_t
*state
= NULL
;
2428 memset(abi
, 0, sizeof (arc_buf_info_t
));
2433 abi
->abi_flags
= hdr
->b_flags
;
2435 if (HDR_HAS_L1HDR(hdr
)) {
2436 l1hdr
= &hdr
->b_l1hdr
;
2437 state
= l1hdr
->b_state
;
2439 if (HDR_HAS_L2HDR(hdr
))
2440 l2hdr
= &hdr
->b_l2hdr
;
2443 abi
->abi_bufcnt
= l1hdr
->b_bufcnt
;
2444 abi
->abi_access
= l1hdr
->b_arc_access
;
2445 abi
->abi_mru_hits
= l1hdr
->b_mru_hits
;
2446 abi
->abi_mru_ghost_hits
= l1hdr
->b_mru_ghost_hits
;
2447 abi
->abi_mfu_hits
= l1hdr
->b_mfu_hits
;
2448 abi
->abi_mfu_ghost_hits
= l1hdr
->b_mfu_ghost_hits
;
2449 abi
->abi_holds
= refcount_count(&l1hdr
->b_refcnt
);
2453 abi
->abi_l2arc_dattr
= l2hdr
->b_daddr
;
2454 abi
->abi_l2arc_hits
= l2hdr
->b_hits
;
2457 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
2458 abi
->abi_state_contents
= arc_buf_type(hdr
);
2459 abi
->abi_size
= arc_hdr_size(hdr
);
2463 * Move the supplied buffer to the indicated state. The hash lock
2464 * for the buffer must be held by the caller.
2467 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*hdr
,
2468 kmutex_t
*hash_lock
)
2470 arc_state_t
*old_state
;
2473 boolean_t update_old
, update_new
;
2474 arc_buf_contents_t buftype
= arc_buf_type(hdr
);
2477 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
2478 * in arc_read() when bringing a buffer out of the L2ARC. However, the
2479 * L1 hdr doesn't always exist when we change state to arc_anon before
2480 * destroying a header, in which case reallocating to add the L1 hdr is
2483 if (HDR_HAS_L1HDR(hdr
)) {
2484 old_state
= hdr
->b_l1hdr
.b_state
;
2485 refcnt
= refcount_count(&hdr
->b_l1hdr
.b_refcnt
);
2486 bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
2487 update_old
= (bufcnt
> 0 || hdr
->b_l1hdr
.b_pabd
!= NULL
||
2490 old_state
= arc_l2c_only
;
2493 update_old
= B_FALSE
;
2495 update_new
= update_old
;
2497 ASSERT(MUTEX_HELD(hash_lock
));
2498 ASSERT3P(new_state
, !=, old_state
);
2499 ASSERT(!GHOST_STATE(new_state
) || bufcnt
== 0);
2500 ASSERT(old_state
!= arc_anon
|| bufcnt
<= 1);
2503 * If this buffer is evictable, transfer it from the
2504 * old state list to the new state list.
2507 if (old_state
!= arc_anon
&& old_state
!= arc_l2c_only
) {
2508 ASSERT(HDR_HAS_L1HDR(hdr
));
2509 multilist_remove(old_state
->arcs_list
[buftype
], hdr
);
2511 if (GHOST_STATE(old_state
)) {
2513 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2514 update_old
= B_TRUE
;
2516 arc_evictable_space_decrement(hdr
, old_state
);
2518 if (new_state
!= arc_anon
&& new_state
!= arc_l2c_only
) {
2520 * An L1 header always exists here, since if we're
2521 * moving to some L1-cached state (i.e. not l2c_only or
2522 * anonymous), we realloc the header to add an L1hdr
2525 ASSERT(HDR_HAS_L1HDR(hdr
));
2526 multilist_insert(new_state
->arcs_list
[buftype
], hdr
);
2528 if (GHOST_STATE(new_state
)) {
2530 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2531 update_new
= B_TRUE
;
2533 arc_evictable_space_increment(hdr
, new_state
);
2537 ASSERT(!HDR_EMPTY(hdr
));
2538 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(hdr
))
2539 buf_hash_remove(hdr
);
2541 /* adjust state sizes (ignore arc_l2c_only) */
2543 if (update_new
&& new_state
!= arc_l2c_only
) {
2544 ASSERT(HDR_HAS_L1HDR(hdr
));
2545 if (GHOST_STATE(new_state
)) {
2549 * When moving a header to a ghost state, we first
2550 * remove all arc buffers. Thus, we'll have a
2551 * bufcnt of zero, and no arc buffer to use for
2552 * the reference. As a result, we use the arc
2553 * header pointer for the reference.
2555 (void) refcount_add_many(&new_state
->arcs_size
,
2556 HDR_GET_LSIZE(hdr
), hdr
);
2557 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2558 ASSERT(!HDR_HAS_RABD(hdr
));
2560 uint32_t buffers
= 0;
2563 * Each individual buffer holds a unique reference,
2564 * thus we must remove each of these references one
2567 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2568 buf
= buf
->b_next
) {
2569 ASSERT3U(bufcnt
, !=, 0);
2573 * When the arc_buf_t is sharing the data
2574 * block with the hdr, the owner of the
2575 * reference belongs to the hdr. Only
2576 * add to the refcount if the arc_buf_t is
2579 if (arc_buf_is_shared(buf
))
2582 (void) refcount_add_many(&new_state
->arcs_size
,
2583 arc_buf_size(buf
), buf
);
2585 ASSERT3U(bufcnt
, ==, buffers
);
2587 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2588 (void) refcount_add_many(&new_state
->arcs_size
,
2589 arc_hdr_size(hdr
), hdr
);
2592 if (HDR_HAS_RABD(hdr
)) {
2593 (void) refcount_add_many(&new_state
->arcs_size
,
2594 HDR_GET_PSIZE(hdr
), hdr
);
2599 if (update_old
&& old_state
!= arc_l2c_only
) {
2600 ASSERT(HDR_HAS_L1HDR(hdr
));
2601 if (GHOST_STATE(old_state
)) {
2603 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2604 ASSERT(!HDR_HAS_RABD(hdr
));
2607 * When moving a header off of a ghost state,
2608 * the header will not contain any arc buffers.
2609 * We use the arc header pointer for the reference
2610 * which is exactly what we did when we put the
2611 * header on the ghost state.
2614 (void) refcount_remove_many(&old_state
->arcs_size
,
2615 HDR_GET_LSIZE(hdr
), hdr
);
2617 uint32_t buffers
= 0;
2620 * Each individual buffer holds a unique reference,
2621 * thus we must remove each of these references one
2624 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2625 buf
= buf
->b_next
) {
2626 ASSERT3U(bufcnt
, !=, 0);
2630 * When the arc_buf_t is sharing the data
2631 * block with the hdr, the owner of the
2632 * reference belongs to the hdr. Only
2633 * add to the refcount if the arc_buf_t is
2636 if (arc_buf_is_shared(buf
))
2639 (void) refcount_remove_many(
2640 &old_state
->arcs_size
, arc_buf_size(buf
),
2643 ASSERT3U(bufcnt
, ==, buffers
);
2644 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
2647 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2648 (void) refcount_remove_many(
2649 &old_state
->arcs_size
, arc_hdr_size(hdr
),
2653 if (HDR_HAS_RABD(hdr
)) {
2654 (void) refcount_remove_many(
2655 &old_state
->arcs_size
, HDR_GET_PSIZE(hdr
),
2661 if (HDR_HAS_L1HDR(hdr
))
2662 hdr
->b_l1hdr
.b_state
= new_state
;
2665 * L2 headers should never be on the L2 state list since they don't
2666 * have L1 headers allocated.
2668 ASSERT(multilist_is_empty(arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]) &&
2669 multilist_is_empty(arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]));
2673 arc_space_consume(uint64_t space
, arc_space_type_t type
)
2675 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2680 case ARC_SPACE_DATA
:
2681 ARCSTAT_INCR(arcstat_data_size
, space
);
2683 case ARC_SPACE_META
:
2684 ARCSTAT_INCR(arcstat_metadata_size
, space
);
2686 case ARC_SPACE_BONUS
:
2687 ARCSTAT_INCR(arcstat_bonus_size
, space
);
2689 case ARC_SPACE_DNODE
:
2690 ARCSTAT_INCR(arcstat_dnode_size
, space
);
2692 case ARC_SPACE_DBUF
:
2693 ARCSTAT_INCR(arcstat_dbuf_size
, space
);
2695 case ARC_SPACE_HDRS
:
2696 ARCSTAT_INCR(arcstat_hdr_size
, space
);
2698 case ARC_SPACE_L2HDRS
:
2699 ARCSTAT_INCR(arcstat_l2_hdr_size
, space
);
2703 if (type
!= ARC_SPACE_DATA
)
2704 ARCSTAT_INCR(arcstat_meta_used
, space
);
2706 atomic_add_64(&arc_size
, space
);
2710 arc_space_return(uint64_t space
, arc_space_type_t type
)
2712 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2717 case ARC_SPACE_DATA
:
2718 ARCSTAT_INCR(arcstat_data_size
, -space
);
2720 case ARC_SPACE_META
:
2721 ARCSTAT_INCR(arcstat_metadata_size
, -space
);
2723 case ARC_SPACE_BONUS
:
2724 ARCSTAT_INCR(arcstat_bonus_size
, -space
);
2726 case ARC_SPACE_DNODE
:
2727 ARCSTAT_INCR(arcstat_dnode_size
, -space
);
2729 case ARC_SPACE_DBUF
:
2730 ARCSTAT_INCR(arcstat_dbuf_size
, -space
);
2732 case ARC_SPACE_HDRS
:
2733 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
2735 case ARC_SPACE_L2HDRS
:
2736 ARCSTAT_INCR(arcstat_l2_hdr_size
, -space
);
2740 if (type
!= ARC_SPACE_DATA
) {
2741 ASSERT(arc_meta_used
>= space
);
2742 if (arc_meta_max
< arc_meta_used
)
2743 arc_meta_max
= arc_meta_used
;
2744 ARCSTAT_INCR(arcstat_meta_used
, -space
);
2747 ASSERT(arc_size
>= space
);
2748 atomic_add_64(&arc_size
, -space
);
2752 * Given a hdr and a buf, returns whether that buf can share its b_data buffer
2753 * with the hdr's b_pabd.
2756 arc_can_share(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2759 * The criteria for sharing a hdr's data are:
2760 * 1. the buffer is not encrypted
2761 * 2. the hdr's compression matches the buf's compression
2762 * 3. the hdr doesn't need to be byteswapped
2763 * 4. the hdr isn't already being shared
2764 * 5. the buf is either compressed or it is the last buf in the hdr list
2766 * Criterion #5 maintains the invariant that shared uncompressed
2767 * bufs must be the final buf in the hdr's b_buf list. Reading this, you
2768 * might ask, "if a compressed buf is allocated first, won't that be the
2769 * last thing in the list?", but in that case it's impossible to create
2770 * a shared uncompressed buf anyway (because the hdr must be compressed
2771 * to have the compressed buf). You might also think that #3 is
2772 * sufficient to make this guarantee, however it's possible
2773 * (specifically in the rare L2ARC write race mentioned in
2774 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that
2775 * is sharable, but wasn't at the time of its allocation. Rather than
2776 * allow a new shared uncompressed buf to be created and then shuffle
2777 * the list around to make it the last element, this simply disallows
2778 * sharing if the new buf isn't the first to be added.
2780 ASSERT3P(buf
->b_hdr
, ==, hdr
);
2781 boolean_t hdr_compressed
=
2782 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
;
2783 boolean_t buf_compressed
= ARC_BUF_COMPRESSED(buf
) != 0;
2784 return (!ARC_BUF_ENCRYPTED(buf
) &&
2785 buf_compressed
== hdr_compressed
&&
2786 hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
&&
2787 !HDR_SHARED_DATA(hdr
) &&
2788 (ARC_BUF_LAST(buf
) || ARC_BUF_COMPRESSED(buf
)));
2792 * Allocate a buf for this hdr. If you care about the data that's in the hdr,
2793 * or if you want a compressed buffer, pass those flags in. Returns 0 if the
2794 * copy was made successfully, or an error code otherwise.
2797 arc_buf_alloc_impl(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2798 void *tag
, boolean_t encrypted
, boolean_t compressed
, boolean_t noauth
,
2799 boolean_t fill
, arc_buf_t
**ret
)
2802 arc_fill_flags_t flags
= ARC_FILL_LOCKED
;
2804 ASSERT(HDR_HAS_L1HDR(hdr
));
2805 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
2806 VERIFY(hdr
->b_type
== ARC_BUFC_DATA
||
2807 hdr
->b_type
== ARC_BUFC_METADATA
);
2808 ASSERT3P(ret
, !=, NULL
);
2809 ASSERT3P(*ret
, ==, NULL
);
2810 IMPLY(encrypted
, compressed
);
2812 hdr
->b_l1hdr
.b_mru_hits
= 0;
2813 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
2814 hdr
->b_l1hdr
.b_mfu_hits
= 0;
2815 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
2816 hdr
->b_l1hdr
.b_l2_hits
= 0;
2818 buf
= *ret
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
2821 buf
->b_next
= hdr
->b_l1hdr
.b_buf
;
2824 add_reference(hdr
, tag
);
2827 * We're about to change the hdr's b_flags. We must either
2828 * hold the hash_lock or be undiscoverable.
2830 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
2833 * Only honor requests for compressed bufs if the hdr is actually
2834 * compressed. This must be overriden if the buffer is encrypted since
2835 * encrypted buffers cannot be decompressed.
2838 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2839 buf
->b_flags
|= ARC_BUF_FLAG_ENCRYPTED
;
2840 flags
|= ARC_FILL_COMPRESSED
| ARC_FILL_ENCRYPTED
;
2841 } else if (compressed
&&
2842 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
2843 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2844 flags
|= ARC_FILL_COMPRESSED
;
2849 flags
|= ARC_FILL_NOAUTH
;
2853 * If the hdr's data can be shared then we share the data buffer and
2854 * set the appropriate bit in the hdr's b_flags to indicate the hdr is
2855 * allocate a new buffer to store the buf's data.
2857 * There are two additional restrictions here because we're sharing
2858 * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be
2859 * actively involved in an L2ARC write, because if this buf is used by
2860 * an arc_write() then the hdr's data buffer will be released when the
2861 * write completes, even though the L2ARC write might still be using it.
2862 * Second, the hdr's ABD must be linear so that the buf's user doesn't
2863 * need to be ABD-aware.
2865 boolean_t can_share
= arc_can_share(hdr
, buf
) && !HDR_L2_WRITING(hdr
) &&
2866 hdr
->b_l1hdr
.b_pabd
!= NULL
&& abd_is_linear(hdr
->b_l1hdr
.b_pabd
);
2868 /* Set up b_data and sharing */
2870 buf
->b_data
= abd_to_buf(hdr
->b_l1hdr
.b_pabd
);
2871 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2872 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2875 arc_get_data_buf(hdr
, arc_buf_size(buf
), buf
);
2876 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
2878 VERIFY3P(buf
->b_data
, !=, NULL
);
2880 hdr
->b_l1hdr
.b_buf
= buf
;
2881 hdr
->b_l1hdr
.b_bufcnt
+= 1;
2883 hdr
->b_crypt_hdr
.b_ebufcnt
+= 1;
2886 * If the user wants the data from the hdr, we need to either copy or
2887 * decompress the data.
2890 ASSERT3P(zb
, !=, NULL
);
2891 return (arc_buf_fill(buf
, spa
, zb
, flags
));
2897 static char *arc_onloan_tag
= "onloan";
2900 arc_loaned_bytes_update(int64_t delta
)
2902 atomic_add_64(&arc_loaned_bytes
, delta
);
2904 /* assert that it did not wrap around */
2905 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
2909 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
2910 * flight data by arc_tempreserve_space() until they are "returned". Loaned
2911 * buffers must be returned to the arc before they can be used by the DMU or
2915 arc_loan_buf(spa_t
*spa
, boolean_t is_metadata
, int size
)
2917 arc_buf_t
*buf
= arc_alloc_buf(spa
, arc_onloan_tag
,
2918 is_metadata
? ARC_BUFC_METADATA
: ARC_BUFC_DATA
, size
);
2920 arc_loaned_bytes_update(arc_buf_size(buf
));
2926 arc_loan_compressed_buf(spa_t
*spa
, uint64_t psize
, uint64_t lsize
,
2927 enum zio_compress compression_type
)
2929 arc_buf_t
*buf
= arc_alloc_compressed_buf(spa
, arc_onloan_tag
,
2930 psize
, lsize
, compression_type
);
2932 arc_loaned_bytes_update(arc_buf_size(buf
));
2938 arc_loan_raw_buf(spa_t
*spa
, uint64_t dsobj
, boolean_t byteorder
,
2939 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
2940 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
2941 enum zio_compress compression_type
)
2943 arc_buf_t
*buf
= arc_alloc_raw_buf(spa
, arc_onloan_tag
, dsobj
,
2944 byteorder
, salt
, iv
, mac
, ot
, psize
, lsize
, compression_type
);
2946 atomic_add_64(&arc_loaned_bytes
, psize
);
2952 * Return a loaned arc buffer to the arc.
2955 arc_return_buf(arc_buf_t
*buf
, void *tag
)
2957 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2959 ASSERT3P(buf
->b_data
, !=, NULL
);
2960 ASSERT(HDR_HAS_L1HDR(hdr
));
2961 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2962 (void) refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2964 arc_loaned_bytes_update(-arc_buf_size(buf
));
2967 /* Detach an arc_buf from a dbuf (tag) */
2969 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
2971 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2973 ASSERT3P(buf
->b_data
, !=, NULL
);
2974 ASSERT(HDR_HAS_L1HDR(hdr
));
2975 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2976 (void) refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2978 arc_loaned_bytes_update(arc_buf_size(buf
));
2982 l2arc_free_abd_on_write(abd_t
*abd
, size_t size
, arc_buf_contents_t type
)
2984 l2arc_data_free_t
*df
= kmem_alloc(sizeof (*df
), KM_SLEEP
);
2987 df
->l2df_size
= size
;
2988 df
->l2df_type
= type
;
2989 mutex_enter(&l2arc_free_on_write_mtx
);
2990 list_insert_head(l2arc_free_on_write
, df
);
2991 mutex_exit(&l2arc_free_on_write_mtx
);
2995 arc_hdr_free_on_write(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
2997 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2998 arc_buf_contents_t type
= arc_buf_type(hdr
);
2999 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
3001 /* protected by hash lock, if in the hash table */
3002 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
3003 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3004 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
3006 (void) refcount_remove_many(&state
->arcs_esize
[type
],
3009 (void) refcount_remove_many(&state
->arcs_size
, size
, hdr
);
3010 if (type
== ARC_BUFC_METADATA
) {
3011 arc_space_return(size
, ARC_SPACE_META
);
3013 ASSERT(type
== ARC_BUFC_DATA
);
3014 arc_space_return(size
, ARC_SPACE_DATA
);
3018 l2arc_free_abd_on_write(hdr
->b_crypt_hdr
.b_rabd
, size
, type
);
3020 l2arc_free_abd_on_write(hdr
->b_l1hdr
.b_pabd
, size
, type
);
3025 * Share the arc_buf_t's data with the hdr. Whenever we are sharing the
3026 * data buffer, we transfer the refcount ownership to the hdr and update
3027 * the appropriate kstats.
3030 arc_share_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3032 ASSERT(arc_can_share(hdr
, buf
));
3033 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3034 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
3035 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
3038 * Start sharing the data buffer. We transfer the
3039 * refcount ownership to the hdr since it always owns
3040 * the refcount whenever an arc_buf_t is shared.
3042 refcount_transfer_ownership(&hdr
->b_l1hdr
.b_state
->arcs_size
, buf
, hdr
);
3043 hdr
->b_l1hdr
.b_pabd
= abd_get_from_buf(buf
->b_data
, arc_buf_size(buf
));
3044 abd_take_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
,
3045 HDR_ISTYPE_METADATA(hdr
));
3046 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3047 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
3050 * Since we've transferred ownership to the hdr we need
3051 * to increment its compressed and uncompressed kstats and
3052 * decrement the overhead size.
3054 ARCSTAT_INCR(arcstat_compressed_size
, arc_hdr_size(hdr
));
3055 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3056 ARCSTAT_INCR(arcstat_overhead_size
, -arc_buf_size(buf
));
3060 arc_unshare_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3062 ASSERT(arc_buf_is_shared(buf
));
3063 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3064 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
3067 * We are no longer sharing this buffer so we need
3068 * to transfer its ownership to the rightful owner.
3070 refcount_transfer_ownership(&hdr
->b_l1hdr
.b_state
->arcs_size
, hdr
, buf
);
3071 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3072 abd_release_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
);
3073 abd_put(hdr
->b_l1hdr
.b_pabd
);
3074 hdr
->b_l1hdr
.b_pabd
= NULL
;
3075 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
3078 * Since the buffer is no longer shared between
3079 * the arc buf and the hdr, count it as overhead.
3081 ARCSTAT_INCR(arcstat_compressed_size
, -arc_hdr_size(hdr
));
3082 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3083 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
3087 * Remove an arc_buf_t from the hdr's buf list and return the last
3088 * arc_buf_t on the list. If no buffers remain on the list then return
3092 arc_buf_remove(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3094 ASSERT(HDR_HAS_L1HDR(hdr
));
3095 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
3097 arc_buf_t
**bufp
= &hdr
->b_l1hdr
.b_buf
;
3098 arc_buf_t
*lastbuf
= NULL
;
3101 * Remove the buf from the hdr list and locate the last
3102 * remaining buffer on the list.
3104 while (*bufp
!= NULL
) {
3106 *bufp
= buf
->b_next
;
3109 * If we've removed a buffer in the middle of
3110 * the list then update the lastbuf and update
3113 if (*bufp
!= NULL
) {
3115 bufp
= &(*bufp
)->b_next
;
3119 ASSERT3P(lastbuf
, !=, buf
);
3120 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, lastbuf
!= NULL
);
3121 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, hdr
->b_l1hdr
.b_buf
!= NULL
);
3122 IMPLY(lastbuf
!= NULL
, ARC_BUF_LAST(lastbuf
));
3128 * Free up buf->b_data and pull the arc_buf_t off of the the arc_buf_hdr_t's
3132 arc_buf_destroy_impl(arc_buf_t
*buf
)
3134 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3137 * Free up the data associated with the buf but only if we're not
3138 * sharing this with the hdr. If we are sharing it with the hdr, the
3139 * hdr is responsible for doing the free.
3141 if (buf
->b_data
!= NULL
) {
3143 * We're about to change the hdr's b_flags. We must either
3144 * hold the hash_lock or be undiscoverable.
3146 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
3148 arc_cksum_verify(buf
);
3149 arc_buf_unwatch(buf
);
3151 if (arc_buf_is_shared(buf
)) {
3152 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3154 uint64_t size
= arc_buf_size(buf
);
3155 arc_free_data_buf(hdr
, buf
->b_data
, size
, buf
);
3156 ARCSTAT_INCR(arcstat_overhead_size
, -size
);
3160 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3161 hdr
->b_l1hdr
.b_bufcnt
-= 1;
3163 if (ARC_BUF_ENCRYPTED(buf
)) {
3164 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
3167 * If we have no more encrypted buffers and we've
3168 * already gotten a copy of the decrypted data we can
3169 * free b_rabd to save some space.
3171 if (hdr
->b_crypt_hdr
.b_ebufcnt
== 0 &&
3172 HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
!= NULL
&&
3173 !HDR_IO_IN_PROGRESS(hdr
)) {
3174 arc_hdr_free_abd(hdr
, B_TRUE
);
3179 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
3181 if (ARC_BUF_SHARED(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
3183 * If the current arc_buf_t is sharing its data buffer with the
3184 * hdr, then reassign the hdr's b_pabd to share it with the new
3185 * buffer at the end of the list. The shared buffer is always
3186 * the last one on the hdr's buffer list.
3188 * There is an equivalent case for compressed bufs, but since
3189 * they aren't guaranteed to be the last buf in the list and
3190 * that is an exceedingly rare case, we just allow that space be
3191 * wasted temporarily. We must also be careful not to share
3192 * encrypted buffers, since they cannot be shared.
3194 if (lastbuf
!= NULL
&& !ARC_BUF_ENCRYPTED(lastbuf
)) {
3195 /* Only one buf can be shared at once */
3196 VERIFY(!arc_buf_is_shared(lastbuf
));
3197 /* hdr is uncompressed so can't have compressed buf */
3198 VERIFY(!ARC_BUF_COMPRESSED(lastbuf
));
3200 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3201 arc_hdr_free_abd(hdr
, B_FALSE
);
3204 * We must setup a new shared block between the
3205 * last buffer and the hdr. The data would have
3206 * been allocated by the arc buf so we need to transfer
3207 * ownership to the hdr since it's now being shared.
3209 arc_share_buf(hdr
, lastbuf
);
3211 } else if (HDR_SHARED_DATA(hdr
)) {
3213 * Uncompressed shared buffers are always at the end
3214 * of the list. Compressed buffers don't have the
3215 * same requirements. This makes it hard to
3216 * simply assert that the lastbuf is shared so
3217 * we rely on the hdr's compression flags to determine
3218 * if we have a compressed, shared buffer.
3220 ASSERT3P(lastbuf
, !=, NULL
);
3221 ASSERT(arc_buf_is_shared(lastbuf
) ||
3222 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
3226 * Free the checksum if we're removing the last uncompressed buf from
3229 if (!arc_hdr_has_uncompressed_buf(hdr
)) {
3230 arc_cksum_free(hdr
);
3233 /* clean up the buf */
3235 kmem_cache_free(buf_cache
, buf
);
3239 arc_hdr_alloc_abd(arc_buf_hdr_t
*hdr
, boolean_t alloc_rdata
)
3243 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
3244 ASSERT(HDR_HAS_L1HDR(hdr
));
3245 ASSERT(!HDR_SHARED_DATA(hdr
) || alloc_rdata
);
3246 IMPLY(alloc_rdata
, HDR_PROTECTED(hdr
));
3249 size
= HDR_GET_PSIZE(hdr
);
3250 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, ==, NULL
);
3251 hdr
->b_crypt_hdr
.b_rabd
= arc_get_data_abd(hdr
, size
, hdr
);
3252 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, !=, NULL
);
3253 ARCSTAT_INCR(arcstat_raw_size
, size
);
3255 size
= arc_hdr_size(hdr
);
3256 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3257 hdr
->b_l1hdr
.b_pabd
= arc_get_data_abd(hdr
, size
, hdr
);
3258 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3261 ARCSTAT_INCR(arcstat_compressed_size
, size
);
3262 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3266 arc_hdr_free_abd(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
3268 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
3270 ASSERT(HDR_HAS_L1HDR(hdr
));
3271 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
3272 IMPLY(free_rdata
, HDR_HAS_RABD(hdr
));
3275 * If the hdr is currently being written to the l2arc then
3276 * we defer freeing the data by adding it to the l2arc_free_on_write
3277 * list. The l2arc will free the data once it's finished
3278 * writing it to the l2arc device.
3280 if (HDR_L2_WRITING(hdr
)) {
3281 arc_hdr_free_on_write(hdr
, free_rdata
);
3282 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
3283 } else if (free_rdata
) {
3284 arc_free_data_abd(hdr
, hdr
->b_crypt_hdr
.b_rabd
, size
, hdr
);
3286 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
, size
, hdr
);
3290 hdr
->b_crypt_hdr
.b_rabd
= NULL
;
3291 ARCSTAT_INCR(arcstat_raw_size
, -size
);
3293 hdr
->b_l1hdr
.b_pabd
= NULL
;
3296 if (hdr
->b_l1hdr
.b_pabd
== NULL
&& !HDR_HAS_RABD(hdr
))
3297 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
3299 ARCSTAT_INCR(arcstat_compressed_size
, -size
);
3300 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3303 static arc_buf_hdr_t
*
3304 arc_hdr_alloc(uint64_t spa
, int32_t psize
, int32_t lsize
,
3305 boolean_t
protected, enum zio_compress compression_type
,
3306 arc_buf_contents_t type
, boolean_t alloc_rdata
)
3310 VERIFY(type
== ARC_BUFC_DATA
|| type
== ARC_BUFC_METADATA
);
3312 hdr
= kmem_cache_alloc(hdr_full_crypt_cache
, KM_PUSHPAGE
);
3314 hdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
3317 ASSERT(HDR_EMPTY(hdr
));
3318 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3319 HDR_SET_PSIZE(hdr
, psize
);
3320 HDR_SET_LSIZE(hdr
, lsize
);
3324 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L1HDR
);
3325 arc_hdr_set_compress(hdr
, compression_type
);
3327 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
3329 hdr
->b_l1hdr
.b_state
= arc_anon
;
3330 hdr
->b_l1hdr
.b_arc_access
= 0;
3331 hdr
->b_l1hdr
.b_bufcnt
= 0;
3332 hdr
->b_l1hdr
.b_buf
= NULL
;
3335 * Allocate the hdr's buffer. This will contain either
3336 * the compressed or uncompressed data depending on the block
3337 * it references and compressed arc enablement.
3339 arc_hdr_alloc_abd(hdr
, alloc_rdata
);
3340 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3346 * Transition between the two allocation states for the arc_buf_hdr struct.
3347 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
3348 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
3349 * version is used when a cache buffer is only in the L2ARC in order to reduce
3352 static arc_buf_hdr_t
*
3353 arc_hdr_realloc(arc_buf_hdr_t
*hdr
, kmem_cache_t
*old
, kmem_cache_t
*new)
3355 ASSERT(HDR_HAS_L2HDR(hdr
));
3357 arc_buf_hdr_t
*nhdr
;
3358 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3360 ASSERT((old
== hdr_full_cache
&& new == hdr_l2only_cache
) ||
3361 (old
== hdr_l2only_cache
&& new == hdr_full_cache
));
3364 * if the caller wanted a new full header and the header is to be
3365 * encrypted we will actually allocate the header from the full crypt
3366 * cache instead. The same applies to freeing from the old cache.
3368 if (HDR_PROTECTED(hdr
) && new == hdr_full_cache
)
3369 new = hdr_full_crypt_cache
;
3370 if (HDR_PROTECTED(hdr
) && old
== hdr_full_cache
)
3371 old
= hdr_full_crypt_cache
;
3373 nhdr
= kmem_cache_alloc(new, KM_PUSHPAGE
);
3375 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
3376 buf_hash_remove(hdr
);
3378 bcopy(hdr
, nhdr
, HDR_L2ONLY_SIZE
);
3380 if (new == hdr_full_cache
|| new == hdr_full_crypt_cache
) {
3381 arc_hdr_set_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3383 * arc_access and arc_change_state need to be aware that a
3384 * header has just come out of L2ARC, so we set its state to
3385 * l2c_only even though it's about to change.
3387 nhdr
->b_l1hdr
.b_state
= arc_l2c_only
;
3389 /* Verify previous threads set to NULL before freeing */
3390 ASSERT3P(nhdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3391 ASSERT(!HDR_HAS_RABD(hdr
));
3393 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3394 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
3395 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3398 * If we've reached here, We must have been called from
3399 * arc_evict_hdr(), as such we should have already been
3400 * removed from any ghost list we were previously on
3401 * (which protects us from racing with arc_evict_state),
3402 * thus no locking is needed during this check.
3404 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3407 * A buffer must not be moved into the arc_l2c_only
3408 * state if it's not finished being written out to the
3409 * l2arc device. Otherwise, the b_l1hdr.b_pabd field
3410 * might try to be accessed, even though it was removed.
3412 VERIFY(!HDR_L2_WRITING(hdr
));
3413 VERIFY3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3414 ASSERT(!HDR_HAS_RABD(hdr
));
3416 arc_hdr_clear_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3419 * The header has been reallocated so we need to re-insert it into any
3422 (void) buf_hash_insert(nhdr
, NULL
);
3424 ASSERT(list_link_active(&hdr
->b_l2hdr
.b_l2node
));
3426 mutex_enter(&dev
->l2ad_mtx
);
3429 * We must place the realloc'ed header back into the list at
3430 * the same spot. Otherwise, if it's placed earlier in the list,
3431 * l2arc_write_buffers() could find it during the function's
3432 * write phase, and try to write it out to the l2arc.
3434 list_insert_after(&dev
->l2ad_buflist
, hdr
, nhdr
);
3435 list_remove(&dev
->l2ad_buflist
, hdr
);
3437 mutex_exit(&dev
->l2ad_mtx
);
3440 * Since we're using the pointer address as the tag when
3441 * incrementing and decrementing the l2ad_alloc refcount, we
3442 * must remove the old pointer (that we're about to destroy) and
3443 * add the new pointer to the refcount. Otherwise we'd remove
3444 * the wrong pointer address when calling arc_hdr_destroy() later.
3447 (void) refcount_remove_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
), hdr
);
3448 (void) refcount_add_many(&dev
->l2ad_alloc
, arc_hdr_size(nhdr
), nhdr
);
3450 buf_discard_identity(hdr
);
3451 kmem_cache_free(old
, hdr
);
3457 * This function allows an L1 header to be reallocated as a crypt
3458 * header and vice versa. If we are going to a crypt header, the
3459 * new fields will be zeroed out.
3461 static arc_buf_hdr_t
*
3462 arc_hdr_realloc_crypt(arc_buf_hdr_t
*hdr
, boolean_t need_crypt
)
3464 arc_buf_hdr_t
*nhdr
;
3466 kmem_cache_t
*ncache
, *ocache
;
3468 ASSERT(HDR_HAS_L1HDR(hdr
));
3469 ASSERT3U(!!HDR_PROTECTED(hdr
), !=, need_crypt
);
3470 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3471 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3474 ncache
= hdr_full_crypt_cache
;
3475 ocache
= hdr_full_cache
;
3477 ncache
= hdr_full_cache
;
3478 ocache
= hdr_full_crypt_cache
;
3481 nhdr
= kmem_cache_alloc(ncache
, KM_PUSHPAGE
);
3482 bcopy(hdr
, nhdr
, HDR_L2ONLY_SIZE
);
3483 nhdr
->b_l1hdr
.b_freeze_cksum
= hdr
->b_l1hdr
.b_freeze_cksum
;
3484 nhdr
->b_l1hdr
.b_bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
3485 nhdr
->b_l1hdr
.b_byteswap
= hdr
->b_l1hdr
.b_byteswap
;
3486 nhdr
->b_l1hdr
.b_state
= hdr
->b_l1hdr
.b_state
;
3487 nhdr
->b_l1hdr
.b_arc_access
= hdr
->b_l1hdr
.b_arc_access
;
3488 nhdr
->b_l1hdr
.b_mru_hits
= hdr
->b_l1hdr
.b_mru_hits
;
3489 nhdr
->b_l1hdr
.b_mru_ghost_hits
= hdr
->b_l1hdr
.b_mru_ghost_hits
;
3490 nhdr
->b_l1hdr
.b_mfu_hits
= hdr
->b_l1hdr
.b_mfu_hits
;
3491 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= hdr
->b_l1hdr
.b_mfu_ghost_hits
;
3492 nhdr
->b_l1hdr
.b_l2_hits
= hdr
->b_l1hdr
.b_l2_hits
;
3493 nhdr
->b_l1hdr
.b_acb
= hdr
->b_l1hdr
.b_acb
;
3494 nhdr
->b_l1hdr
.b_pabd
= hdr
->b_l1hdr
.b_pabd
;
3495 nhdr
->b_l1hdr
.b_buf
= hdr
->b_l1hdr
.b_buf
;
3498 * This refcount_add() exists only to ensure that the individual
3499 * arc buffers always point to a header that is referenced, avoiding
3500 * a small race condition that could trigger ASSERTs.
3502 (void) refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, FTAG
);
3504 for (buf
= nhdr
->b_l1hdr
.b_buf
; buf
!= NULL
; buf
= buf
->b_next
) {
3505 mutex_enter(&buf
->b_evict_lock
);
3507 mutex_exit(&buf
->b_evict_lock
);
3510 refcount_transfer(&nhdr
->b_l1hdr
.b_refcnt
, &hdr
->b_l1hdr
.b_refcnt
);
3511 (void) refcount_remove(&nhdr
->b_l1hdr
.b_refcnt
, FTAG
);
3514 arc_hdr_set_flags(nhdr
, ARC_FLAG_PROTECTED
);
3516 arc_hdr_clear_flags(nhdr
, ARC_FLAG_PROTECTED
);
3519 buf_discard_identity(hdr
);
3520 kmem_cache_free(ocache
, hdr
);
3526 * This function is used by the send / receive code to convert a newly
3527 * allocated arc_buf_t to one that is suitable for a raw encrypted write. It
3528 * is also used to allow the root objset block to be uupdated without altering
3529 * its embedded MACs. Both block types will always be uncompressed so we do not
3530 * have to worry about compression type or psize.
3533 arc_convert_to_raw(arc_buf_t
*buf
, uint64_t dsobj
, boolean_t byteorder
,
3534 dmu_object_type_t ot
, const uint8_t *salt
, const uint8_t *iv
,
3537 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3539 ASSERT(ot
== DMU_OT_DNODE
|| ot
== DMU_OT_OBJSET
);
3540 ASSERT(HDR_HAS_L1HDR(hdr
));
3541 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3543 buf
->b_flags
|= (ARC_BUF_FLAG_COMPRESSED
| ARC_BUF_FLAG_ENCRYPTED
);
3544 if (!HDR_PROTECTED(hdr
))
3545 hdr
= arc_hdr_realloc_crypt(hdr
, B_TRUE
);
3546 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3547 hdr
->b_crypt_hdr
.b_ot
= ot
;
3548 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3549 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3550 if (!arc_hdr_has_uncompressed_buf(hdr
))
3551 arc_cksum_free(hdr
);
3554 bcopy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3556 bcopy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3558 bcopy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3562 * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller.
3563 * The buf is returned thawed since we expect the consumer to modify it.
3566 arc_alloc_buf(spa_t
*spa
, void *tag
, arc_buf_contents_t type
, int32_t size
)
3568 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), size
, size
,
3569 B_FALSE
, ZIO_COMPRESS_OFF
, type
, B_FALSE
);
3570 ASSERT(!MUTEX_HELD(HDR_LOCK(hdr
)));
3572 arc_buf_t
*buf
= NULL
;
3573 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
, B_FALSE
,
3574 B_FALSE
, B_FALSE
, &buf
));
3581 * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this
3582 * for bufs containing metadata.
3585 arc_alloc_compressed_buf(spa_t
*spa
, void *tag
, uint64_t psize
, uint64_t lsize
,
3586 enum zio_compress compression_type
)
3588 ASSERT3U(lsize
, >, 0);
3589 ASSERT3U(lsize
, >=, psize
);
3590 ASSERT3U(compression_type
, >, ZIO_COMPRESS_OFF
);
3591 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3593 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
3594 B_FALSE
, compression_type
, ARC_BUFC_DATA
, B_FALSE
);
3595 ASSERT(!MUTEX_HELD(HDR_LOCK(hdr
)));
3597 arc_buf_t
*buf
= NULL
;
3598 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
,
3599 B_TRUE
, B_FALSE
, B_FALSE
, &buf
));
3601 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3603 if (!arc_buf_is_shared(buf
)) {
3605 * To ensure that the hdr has the correct data in it if we call
3606 * arc_untransform() on this buf before it's been written to
3607 * disk, it's easiest if we just set up sharing between the
3610 ASSERT(!abd_is_linear(hdr
->b_l1hdr
.b_pabd
));
3611 arc_hdr_free_abd(hdr
, B_FALSE
);
3612 arc_share_buf(hdr
, buf
);
3619 arc_alloc_raw_buf(spa_t
*spa
, void *tag
, uint64_t dsobj
, boolean_t byteorder
,
3620 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
3621 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
3622 enum zio_compress compression_type
)
3626 arc_buf_contents_t type
= DMU_OT_IS_METADATA(ot
) ?
3627 ARC_BUFC_METADATA
: ARC_BUFC_DATA
;
3629 ASSERT3U(lsize
, >, 0);
3630 ASSERT3U(lsize
, >=, psize
);
3631 ASSERT3U(compression_type
, >=, ZIO_COMPRESS_OFF
);
3632 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3634 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
, B_TRUE
,
3635 compression_type
, type
, B_TRUE
);
3636 ASSERT(!MUTEX_HELD(HDR_LOCK(hdr
)));
3638 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3639 hdr
->b_crypt_hdr
.b_ot
= ot
;
3640 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3641 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3642 bcopy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3643 bcopy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3644 bcopy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3647 * This buffer will be considered encrypted even if the ot is not an
3648 * encrypted type. It will become authenticated instead in
3649 * arc_write_ready().
3652 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_TRUE
, B_TRUE
,
3653 B_FALSE
, B_FALSE
, &buf
));
3655 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3661 arc_hdr_l2hdr_destroy(arc_buf_hdr_t
*hdr
)
3663 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
3664 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
3665 uint64_t psize
= arc_hdr_size(hdr
);
3667 ASSERT(MUTEX_HELD(&dev
->l2ad_mtx
));
3668 ASSERT(HDR_HAS_L2HDR(hdr
));
3670 list_remove(&dev
->l2ad_buflist
, hdr
);
3672 ARCSTAT_INCR(arcstat_l2_psize
, -psize
);
3673 ARCSTAT_INCR(arcstat_l2_lsize
, -HDR_GET_LSIZE(hdr
));
3675 vdev_space_update(dev
->l2ad_vdev
, -psize
, 0, 0);
3677 (void) refcount_remove_many(&dev
->l2ad_alloc
, psize
, hdr
);
3678 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
3682 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
3684 if (HDR_HAS_L1HDR(hdr
)) {
3685 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
||
3686 hdr
->b_l1hdr
.b_bufcnt
> 0);
3687 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3688 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3690 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3691 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
3693 if (!HDR_EMPTY(hdr
))
3694 buf_discard_identity(hdr
);
3696 if (HDR_HAS_L2HDR(hdr
)) {
3697 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3698 boolean_t buflist_held
= MUTEX_HELD(&dev
->l2ad_mtx
);
3701 mutex_enter(&dev
->l2ad_mtx
);
3704 * Even though we checked this conditional above, we
3705 * need to check this again now that we have the
3706 * l2ad_mtx. This is because we could be racing with
3707 * another thread calling l2arc_evict() which might have
3708 * destroyed this header's L2 portion as we were waiting
3709 * to acquire the l2ad_mtx. If that happens, we don't
3710 * want to re-destroy the header's L2 portion.
3712 if (HDR_HAS_L2HDR(hdr
))
3713 arc_hdr_l2hdr_destroy(hdr
);
3716 mutex_exit(&dev
->l2ad_mtx
);
3719 if (HDR_HAS_L1HDR(hdr
)) {
3720 arc_cksum_free(hdr
);
3722 while (hdr
->b_l1hdr
.b_buf
!= NULL
)
3723 arc_buf_destroy_impl(hdr
->b_l1hdr
.b_buf
);
3725 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
3726 arc_hdr_free_abd(hdr
, B_FALSE
);
3729 if (HDR_HAS_RABD(hdr
))
3730 arc_hdr_free_abd(hdr
, B_TRUE
);
3733 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3734 if (HDR_HAS_L1HDR(hdr
)) {
3735 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3736 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
3738 if (!HDR_PROTECTED(hdr
)) {
3739 kmem_cache_free(hdr_full_cache
, hdr
);
3741 kmem_cache_free(hdr_full_crypt_cache
, hdr
);
3744 kmem_cache_free(hdr_l2only_cache
, hdr
);
3749 arc_buf_destroy(arc_buf_t
*buf
, void* tag
)
3751 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3752 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
3754 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
3755 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
3756 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3757 VERIFY0(remove_reference(hdr
, NULL
, tag
));
3758 arc_hdr_destroy(hdr
);
3762 mutex_enter(hash_lock
);
3763 ASSERT3P(hdr
, ==, buf
->b_hdr
);
3764 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3765 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3766 ASSERT3P(hdr
->b_l1hdr
.b_state
, !=, arc_anon
);
3767 ASSERT3P(buf
->b_data
, !=, NULL
);
3769 (void) remove_reference(hdr
, hash_lock
, tag
);
3770 arc_buf_destroy_impl(buf
);
3771 mutex_exit(hash_lock
);
3775 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
3776 * state of the header is dependent on its state prior to entering this
3777 * function. The following transitions are possible:
3779 * - arc_mru -> arc_mru_ghost
3780 * - arc_mfu -> arc_mfu_ghost
3781 * - arc_mru_ghost -> arc_l2c_only
3782 * - arc_mru_ghost -> deleted
3783 * - arc_mfu_ghost -> arc_l2c_only
3784 * - arc_mfu_ghost -> deleted
3787 arc_evict_hdr(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
3789 arc_state_t
*evicted_state
, *state
;
3790 int64_t bytes_evicted
= 0;
3791 int min_lifetime
= HDR_PRESCIENT_PREFETCH(hdr
) ?
3792 arc_min_prescient_prefetch_ms
: arc_min_prefetch_ms
;
3794 ASSERT(MUTEX_HELD(hash_lock
));
3795 ASSERT(HDR_HAS_L1HDR(hdr
));
3797 state
= hdr
->b_l1hdr
.b_state
;
3798 if (GHOST_STATE(state
)) {
3799 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3800 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3803 * l2arc_write_buffers() relies on a header's L1 portion
3804 * (i.e. its b_pabd field) during it's write phase.
3805 * Thus, we cannot push a header onto the arc_l2c_only
3806 * state (removing its L1 piece) until the header is
3807 * done being written to the l2arc.
3809 if (HDR_HAS_L2HDR(hdr
) && HDR_L2_WRITING(hdr
)) {
3810 ARCSTAT_BUMP(arcstat_evict_l2_skip
);
3811 return (bytes_evicted
);
3814 ARCSTAT_BUMP(arcstat_deleted
);
3815 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3817 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, hdr
);
3819 if (HDR_HAS_L2HDR(hdr
)) {
3820 ASSERT(hdr
->b_l1hdr
.b_pabd
== NULL
);
3821 ASSERT(!HDR_HAS_RABD(hdr
));
3823 * This buffer is cached on the 2nd Level ARC;
3824 * don't destroy the header.
3826 arc_change_state(arc_l2c_only
, hdr
, hash_lock
);
3828 * dropping from L1+L2 cached to L2-only,
3829 * realloc to remove the L1 header.
3831 hdr
= arc_hdr_realloc(hdr
, hdr_full_cache
,
3834 arc_change_state(arc_anon
, hdr
, hash_lock
);
3835 arc_hdr_destroy(hdr
);
3837 return (bytes_evicted
);
3840 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
3841 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
3843 /* prefetch buffers have a minimum lifespan */
3844 if (HDR_IO_IN_PROGRESS(hdr
) ||
3845 ((hdr
->b_flags
& (ARC_FLAG_PREFETCH
| ARC_FLAG_INDIRECT
)) &&
3846 ddi_get_lbolt() - hdr
->b_l1hdr
.b_arc_access
<
3847 MSEC_TO_TICK(min_lifetime
))) {
3848 ARCSTAT_BUMP(arcstat_evict_skip
);
3849 return (bytes_evicted
);
3852 ASSERT0(refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3853 while (hdr
->b_l1hdr
.b_buf
) {
3854 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
3855 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
3856 ARCSTAT_BUMP(arcstat_mutex_miss
);
3859 if (buf
->b_data
!= NULL
)
3860 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3861 mutex_exit(&buf
->b_evict_lock
);
3862 arc_buf_destroy_impl(buf
);
3865 if (HDR_HAS_L2HDR(hdr
)) {
3866 ARCSTAT_INCR(arcstat_evict_l2_cached
, HDR_GET_LSIZE(hdr
));
3868 if (l2arc_write_eligible(hdr
->b_spa
, hdr
)) {
3869 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
3870 HDR_GET_LSIZE(hdr
));
3872 ARCSTAT_INCR(arcstat_evict_l2_ineligible
,
3873 HDR_GET_LSIZE(hdr
));
3877 if (hdr
->b_l1hdr
.b_bufcnt
== 0) {
3878 arc_cksum_free(hdr
);
3880 bytes_evicted
+= arc_hdr_size(hdr
);
3883 * If this hdr is being evicted and has a compressed
3884 * buffer then we discard it here before we change states.
3885 * This ensures that the accounting is updated correctly
3886 * in arc_free_data_impl().
3888 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
3889 arc_hdr_free_abd(hdr
, B_FALSE
);
3891 if (HDR_HAS_RABD(hdr
))
3892 arc_hdr_free_abd(hdr
, B_TRUE
);
3894 arc_change_state(evicted_state
, hdr
, hash_lock
);
3895 ASSERT(HDR_IN_HASH_TABLE(hdr
));
3896 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
3897 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, hdr
);
3900 return (bytes_evicted
);
3904 arc_evict_state_impl(multilist_t
*ml
, int idx
, arc_buf_hdr_t
*marker
,
3905 uint64_t spa
, int64_t bytes
)
3907 multilist_sublist_t
*mls
;
3908 uint64_t bytes_evicted
= 0;
3910 kmutex_t
*hash_lock
;
3911 int evict_count
= 0;
3913 ASSERT3P(marker
, !=, NULL
);
3914 IMPLY(bytes
< 0, bytes
== ARC_EVICT_ALL
);
3916 mls
= multilist_sublist_lock(ml
, idx
);
3918 for (hdr
= multilist_sublist_prev(mls
, marker
); hdr
!= NULL
;
3919 hdr
= multilist_sublist_prev(mls
, marker
)) {
3920 if ((bytes
!= ARC_EVICT_ALL
&& bytes_evicted
>= bytes
) ||
3921 (evict_count
>= zfs_arc_evict_batch_limit
))
3925 * To keep our iteration location, move the marker
3926 * forward. Since we're not holding hdr's hash lock, we
3927 * must be very careful and not remove 'hdr' from the
3928 * sublist. Otherwise, other consumers might mistake the
3929 * 'hdr' as not being on a sublist when they call the
3930 * multilist_link_active() function (they all rely on
3931 * the hash lock protecting concurrent insertions and
3932 * removals). multilist_sublist_move_forward() was
3933 * specifically implemented to ensure this is the case
3934 * (only 'marker' will be removed and re-inserted).
3936 multilist_sublist_move_forward(mls
, marker
);
3939 * The only case where the b_spa field should ever be
3940 * zero, is the marker headers inserted by
3941 * arc_evict_state(). It's possible for multiple threads
3942 * to be calling arc_evict_state() concurrently (e.g.
3943 * dsl_pool_close() and zio_inject_fault()), so we must
3944 * skip any markers we see from these other threads.
3946 if (hdr
->b_spa
== 0)
3949 /* we're only interested in evicting buffers of a certain spa */
3950 if (spa
!= 0 && hdr
->b_spa
!= spa
) {
3951 ARCSTAT_BUMP(arcstat_evict_skip
);
3955 hash_lock
= HDR_LOCK(hdr
);
3958 * We aren't calling this function from any code path
3959 * that would already be holding a hash lock, so we're
3960 * asserting on this assumption to be defensive in case
3961 * this ever changes. Without this check, it would be
3962 * possible to incorrectly increment arcstat_mutex_miss
3963 * below (e.g. if the code changed such that we called
3964 * this function with a hash lock held).
3966 ASSERT(!MUTEX_HELD(hash_lock
));
3968 if (mutex_tryenter(hash_lock
)) {
3969 uint64_t evicted
= arc_evict_hdr(hdr
, hash_lock
);
3970 mutex_exit(hash_lock
);
3972 bytes_evicted
+= evicted
;
3975 * If evicted is zero, arc_evict_hdr() must have
3976 * decided to skip this header, don't increment
3977 * evict_count in this case.
3983 * If arc_size isn't overflowing, signal any
3984 * threads that might happen to be waiting.
3986 * For each header evicted, we wake up a single
3987 * thread. If we used cv_broadcast, we could
3988 * wake up "too many" threads causing arc_size
3989 * to significantly overflow arc_c; since
3990 * arc_get_data_impl() doesn't check for overflow
3991 * when it's woken up (it doesn't because it's
3992 * possible for the ARC to be overflowing while
3993 * full of un-evictable buffers, and the
3994 * function should proceed in this case).
3996 * If threads are left sleeping, due to not
3997 * using cv_broadcast, they will be woken up
3998 * just before arc_reclaim_thread() sleeps.
4000 mutex_enter(&arc_reclaim_lock
);
4001 if (!arc_is_overflowing())
4002 cv_signal(&arc_reclaim_waiters_cv
);
4003 mutex_exit(&arc_reclaim_lock
);
4005 ARCSTAT_BUMP(arcstat_mutex_miss
);
4009 multilist_sublist_unlock(mls
);
4011 return (bytes_evicted
);
4015 * Evict buffers from the given arc state, until we've removed the
4016 * specified number of bytes. Move the removed buffers to the
4017 * appropriate evict state.
4019 * This function makes a "best effort". It skips over any buffers
4020 * it can't get a hash_lock on, and so, may not catch all candidates.
4021 * It may also return without evicting as much space as requested.
4023 * If bytes is specified using the special value ARC_EVICT_ALL, this
4024 * will evict all available (i.e. unlocked and evictable) buffers from
4025 * the given arc state; which is used by arc_flush().
4028 arc_evict_state(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
4029 arc_buf_contents_t type
)
4031 uint64_t total_evicted
= 0;
4032 multilist_t
*ml
= state
->arcs_list
[type
];
4034 arc_buf_hdr_t
**markers
;
4036 IMPLY(bytes
< 0, bytes
== ARC_EVICT_ALL
);
4038 num_sublists
= multilist_get_num_sublists(ml
);
4041 * If we've tried to evict from each sublist, made some
4042 * progress, but still have not hit the target number of bytes
4043 * to evict, we want to keep trying. The markers allow us to
4044 * pick up where we left off for each individual sublist, rather
4045 * than starting from the tail each time.
4047 markers
= kmem_zalloc(sizeof (*markers
) * num_sublists
, KM_SLEEP
);
4048 for (int i
= 0; i
< num_sublists
; i
++) {
4049 multilist_sublist_t
*mls
;
4051 markers
[i
] = kmem_cache_alloc(hdr_full_cache
, KM_SLEEP
);
4054 * A b_spa of 0 is used to indicate that this header is
4055 * a marker. This fact is used in arc_adjust_type() and
4056 * arc_evict_state_impl().
4058 markers
[i
]->b_spa
= 0;
4060 mls
= multilist_sublist_lock(ml
, i
);
4061 multilist_sublist_insert_tail(mls
, markers
[i
]);
4062 multilist_sublist_unlock(mls
);
4066 * While we haven't hit our target number of bytes to evict, or
4067 * we're evicting all available buffers.
4069 while (total_evicted
< bytes
|| bytes
== ARC_EVICT_ALL
) {
4070 int sublist_idx
= multilist_get_random_index(ml
);
4071 uint64_t scan_evicted
= 0;
4074 * Try to reduce pinned dnodes with a floor of arc_dnode_limit.
4075 * Request that 10% of the LRUs be scanned by the superblock
4078 if (type
== ARC_BUFC_DATA
&& arc_dnode_size
> arc_dnode_limit
)
4079 arc_prune_async((arc_dnode_size
- arc_dnode_limit
) /
4080 sizeof (dnode_t
) / zfs_arc_dnode_reduce_percent
);
4083 * Start eviction using a randomly selected sublist,
4084 * this is to try and evenly balance eviction across all
4085 * sublists. Always starting at the same sublist
4086 * (e.g. index 0) would cause evictions to favor certain
4087 * sublists over others.
4089 for (int i
= 0; i
< num_sublists
; i
++) {
4090 uint64_t bytes_remaining
;
4091 uint64_t bytes_evicted
;
4093 if (bytes
== ARC_EVICT_ALL
)
4094 bytes_remaining
= ARC_EVICT_ALL
;
4095 else if (total_evicted
< bytes
)
4096 bytes_remaining
= bytes
- total_evicted
;
4100 bytes_evicted
= arc_evict_state_impl(ml
, sublist_idx
,
4101 markers
[sublist_idx
], spa
, bytes_remaining
);
4103 scan_evicted
+= bytes_evicted
;
4104 total_evicted
+= bytes_evicted
;
4106 /* we've reached the end, wrap to the beginning */
4107 if (++sublist_idx
>= num_sublists
)
4112 * If we didn't evict anything during this scan, we have
4113 * no reason to believe we'll evict more during another
4114 * scan, so break the loop.
4116 if (scan_evicted
== 0) {
4117 /* This isn't possible, let's make that obvious */
4118 ASSERT3S(bytes
, !=, 0);
4121 * When bytes is ARC_EVICT_ALL, the only way to
4122 * break the loop is when scan_evicted is zero.
4123 * In that case, we actually have evicted enough,
4124 * so we don't want to increment the kstat.
4126 if (bytes
!= ARC_EVICT_ALL
) {
4127 ASSERT3S(total_evicted
, <, bytes
);
4128 ARCSTAT_BUMP(arcstat_evict_not_enough
);
4135 for (int i
= 0; i
< num_sublists
; i
++) {
4136 multilist_sublist_t
*mls
= multilist_sublist_lock(ml
, i
);
4137 multilist_sublist_remove(mls
, markers
[i
]);
4138 multilist_sublist_unlock(mls
);
4140 kmem_cache_free(hdr_full_cache
, markers
[i
]);
4142 kmem_free(markers
, sizeof (*markers
) * num_sublists
);
4144 return (total_evicted
);
4148 * Flush all "evictable" data of the given type from the arc state
4149 * specified. This will not evict any "active" buffers (i.e. referenced).
4151 * When 'retry' is set to B_FALSE, the function will make a single pass
4152 * over the state and evict any buffers that it can. Since it doesn't
4153 * continually retry the eviction, it might end up leaving some buffers
4154 * in the ARC due to lock misses.
4156 * When 'retry' is set to B_TRUE, the function will continually retry the
4157 * eviction until *all* evictable buffers have been removed from the
4158 * state. As a result, if concurrent insertions into the state are
4159 * allowed (e.g. if the ARC isn't shutting down), this function might
4160 * wind up in an infinite loop, continually trying to evict buffers.
4163 arc_flush_state(arc_state_t
*state
, uint64_t spa
, arc_buf_contents_t type
,
4166 uint64_t evicted
= 0;
4168 while (refcount_count(&state
->arcs_esize
[type
]) != 0) {
4169 evicted
+= arc_evict_state(state
, spa
, ARC_EVICT_ALL
, type
);
4179 * Helper function for arc_prune_async() it is responsible for safely
4180 * handling the execution of a registered arc_prune_func_t.
4183 arc_prune_task(void *ptr
)
4185 arc_prune_t
*ap
= (arc_prune_t
*)ptr
;
4186 arc_prune_func_t
*func
= ap
->p_pfunc
;
4189 func(ap
->p_adjust
, ap
->p_private
);
4191 refcount_remove(&ap
->p_refcnt
, func
);
4195 * Notify registered consumers they must drop holds on a portion of the ARC
4196 * buffered they reference. This provides a mechanism to ensure the ARC can
4197 * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This
4198 * is analogous to dnlc_reduce_cache() but more generic.
4200 * This operation is performed asynchronously so it may be safely called
4201 * in the context of the arc_reclaim_thread(). A reference is taken here
4202 * for each registered arc_prune_t and the arc_prune_task() is responsible
4203 * for releasing it once the registered arc_prune_func_t has completed.
4206 arc_prune_async(int64_t adjust
)
4210 mutex_enter(&arc_prune_mtx
);
4211 for (ap
= list_head(&arc_prune_list
); ap
!= NULL
;
4212 ap
= list_next(&arc_prune_list
, ap
)) {
4214 if (refcount_count(&ap
->p_refcnt
) >= 2)
4217 refcount_add(&ap
->p_refcnt
, ap
->p_pfunc
);
4218 ap
->p_adjust
= adjust
;
4219 if (taskq_dispatch(arc_prune_taskq
, arc_prune_task
,
4220 ap
, TQ_SLEEP
) == TASKQID_INVALID
) {
4221 refcount_remove(&ap
->p_refcnt
, ap
->p_pfunc
);
4224 ARCSTAT_BUMP(arcstat_prune
);
4226 mutex_exit(&arc_prune_mtx
);
4230 * Evict the specified number of bytes from the state specified,
4231 * restricting eviction to the spa and type given. This function
4232 * prevents us from trying to evict more from a state's list than
4233 * is "evictable", and to skip evicting altogether when passed a
4234 * negative value for "bytes". In contrast, arc_evict_state() will
4235 * evict everything it can, when passed a negative value for "bytes".
4238 arc_adjust_impl(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
4239 arc_buf_contents_t type
)
4243 if (bytes
> 0 && refcount_count(&state
->arcs_esize
[type
]) > 0) {
4244 delta
= MIN(refcount_count(&state
->arcs_esize
[type
]), bytes
);
4245 return (arc_evict_state(state
, spa
, delta
, type
));
4252 * The goal of this function is to evict enough meta data buffers from the
4253 * ARC in order to enforce the arc_meta_limit. Achieving this is slightly
4254 * more complicated than it appears because it is common for data buffers
4255 * to have holds on meta data buffers. In addition, dnode meta data buffers
4256 * will be held by the dnodes in the block preventing them from being freed.
4257 * This means we can't simply traverse the ARC and expect to always find
4258 * enough unheld meta data buffer to release.
4260 * Therefore, this function has been updated to make alternating passes
4261 * over the ARC releasing data buffers and then newly unheld meta data
4262 * buffers. This ensures forward progress is maintained and arc_meta_used
4263 * will decrease. Normally this is sufficient, but if required the ARC
4264 * will call the registered prune callbacks causing dentry and inodes to
4265 * be dropped from the VFS cache. This will make dnode meta data buffers
4266 * available for reclaim.
4269 arc_adjust_meta_balanced(void)
4271 int64_t delta
, prune
= 0, adjustmnt
;
4272 uint64_t total_evicted
= 0;
4273 arc_buf_contents_t type
= ARC_BUFC_DATA
;
4274 int restarts
= MAX(zfs_arc_meta_adjust_restarts
, 0);
4278 * This slightly differs than the way we evict from the mru in
4279 * arc_adjust because we don't have a "target" value (i.e. no
4280 * "meta" arc_p). As a result, I think we can completely
4281 * cannibalize the metadata in the MRU before we evict the
4282 * metadata from the MFU. I think we probably need to implement a
4283 * "metadata arc_p" value to do this properly.
4285 adjustmnt
= arc_meta_used
- arc_meta_limit
;
4287 if (adjustmnt
> 0 && refcount_count(&arc_mru
->arcs_esize
[type
]) > 0) {
4288 delta
= MIN(refcount_count(&arc_mru
->arcs_esize
[type
]),
4290 total_evicted
+= arc_adjust_impl(arc_mru
, 0, delta
, type
);
4295 * We can't afford to recalculate adjustmnt here. If we do,
4296 * new metadata buffers can sneak into the MRU or ANON lists,
4297 * thus penalize the MFU metadata. Although the fudge factor is
4298 * small, it has been empirically shown to be significant for
4299 * certain workloads (e.g. creating many empty directories). As
4300 * such, we use the original calculation for adjustmnt, and
4301 * simply decrement the amount of data evicted from the MRU.
4304 if (adjustmnt
> 0 && refcount_count(&arc_mfu
->arcs_esize
[type
]) > 0) {
4305 delta
= MIN(refcount_count(&arc_mfu
->arcs_esize
[type
]),
4307 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, delta
, type
);
4310 adjustmnt
= arc_meta_used
- arc_meta_limit
;
4312 if (adjustmnt
> 0 &&
4313 refcount_count(&arc_mru_ghost
->arcs_esize
[type
]) > 0) {
4314 delta
= MIN(adjustmnt
,
4315 refcount_count(&arc_mru_ghost
->arcs_esize
[type
]));
4316 total_evicted
+= arc_adjust_impl(arc_mru_ghost
, 0, delta
, type
);
4320 if (adjustmnt
> 0 &&
4321 refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]) > 0) {
4322 delta
= MIN(adjustmnt
,
4323 refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]));
4324 total_evicted
+= arc_adjust_impl(arc_mfu_ghost
, 0, delta
, type
);
4328 * If after attempting to make the requested adjustment to the ARC
4329 * the meta limit is still being exceeded then request that the
4330 * higher layers drop some cached objects which have holds on ARC
4331 * meta buffers. Requests to the upper layers will be made with
4332 * increasingly large scan sizes until the ARC is below the limit.
4334 if (arc_meta_used
> arc_meta_limit
) {
4335 if (type
== ARC_BUFC_DATA
) {
4336 type
= ARC_BUFC_METADATA
;
4338 type
= ARC_BUFC_DATA
;
4340 if (zfs_arc_meta_prune
) {
4341 prune
+= zfs_arc_meta_prune
;
4342 arc_prune_async(prune
);
4351 return (total_evicted
);
4355 * Evict metadata buffers from the cache, such that arc_meta_used is
4356 * capped by the arc_meta_limit tunable.
4359 arc_adjust_meta_only(void)
4361 uint64_t total_evicted
= 0;
4365 * If we're over the meta limit, we want to evict enough
4366 * metadata to get back under the meta limit. We don't want to
4367 * evict so much that we drop the MRU below arc_p, though. If
4368 * we're over the meta limit more than we're over arc_p, we
4369 * evict some from the MRU here, and some from the MFU below.
4371 target
= MIN((int64_t)(arc_meta_used
- arc_meta_limit
),
4372 (int64_t)(refcount_count(&arc_anon
->arcs_size
) +
4373 refcount_count(&arc_mru
->arcs_size
) - arc_p
));
4375 total_evicted
+= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4378 * Similar to the above, we want to evict enough bytes to get us
4379 * below the meta limit, but not so much as to drop us below the
4380 * space allotted to the MFU (which is defined as arc_c - arc_p).
4382 target
= MIN((int64_t)(arc_meta_used
- arc_meta_limit
),
4383 (int64_t)(refcount_count(&arc_mfu
->arcs_size
) - (arc_c
- arc_p
)));
4385 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4387 return (total_evicted
);
4391 arc_adjust_meta(void)
4393 if (zfs_arc_meta_strategy
== ARC_STRATEGY_META_ONLY
)
4394 return (arc_adjust_meta_only());
4396 return (arc_adjust_meta_balanced());
4400 * Return the type of the oldest buffer in the given arc state
4402 * This function will select a random sublist of type ARC_BUFC_DATA and
4403 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist
4404 * is compared, and the type which contains the "older" buffer will be
4407 static arc_buf_contents_t
4408 arc_adjust_type(arc_state_t
*state
)
4410 multilist_t
*data_ml
= state
->arcs_list
[ARC_BUFC_DATA
];
4411 multilist_t
*meta_ml
= state
->arcs_list
[ARC_BUFC_METADATA
];
4412 int data_idx
= multilist_get_random_index(data_ml
);
4413 int meta_idx
= multilist_get_random_index(meta_ml
);
4414 multilist_sublist_t
*data_mls
;
4415 multilist_sublist_t
*meta_mls
;
4416 arc_buf_contents_t type
;
4417 arc_buf_hdr_t
*data_hdr
;
4418 arc_buf_hdr_t
*meta_hdr
;
4421 * We keep the sublist lock until we're finished, to prevent
4422 * the headers from being destroyed via arc_evict_state().
4424 data_mls
= multilist_sublist_lock(data_ml
, data_idx
);
4425 meta_mls
= multilist_sublist_lock(meta_ml
, meta_idx
);
4428 * These two loops are to ensure we skip any markers that
4429 * might be at the tail of the lists due to arc_evict_state().
4432 for (data_hdr
= multilist_sublist_tail(data_mls
); data_hdr
!= NULL
;
4433 data_hdr
= multilist_sublist_prev(data_mls
, data_hdr
)) {
4434 if (data_hdr
->b_spa
!= 0)
4438 for (meta_hdr
= multilist_sublist_tail(meta_mls
); meta_hdr
!= NULL
;
4439 meta_hdr
= multilist_sublist_prev(meta_mls
, meta_hdr
)) {
4440 if (meta_hdr
->b_spa
!= 0)
4444 if (data_hdr
== NULL
&& meta_hdr
== NULL
) {
4445 type
= ARC_BUFC_DATA
;
4446 } else if (data_hdr
== NULL
) {
4447 ASSERT3P(meta_hdr
, !=, NULL
);
4448 type
= ARC_BUFC_METADATA
;
4449 } else if (meta_hdr
== NULL
) {
4450 ASSERT3P(data_hdr
, !=, NULL
);
4451 type
= ARC_BUFC_DATA
;
4453 ASSERT3P(data_hdr
, !=, NULL
);
4454 ASSERT3P(meta_hdr
, !=, NULL
);
4456 /* The headers can't be on the sublist without an L1 header */
4457 ASSERT(HDR_HAS_L1HDR(data_hdr
));
4458 ASSERT(HDR_HAS_L1HDR(meta_hdr
));
4460 if (data_hdr
->b_l1hdr
.b_arc_access
<
4461 meta_hdr
->b_l1hdr
.b_arc_access
) {
4462 type
= ARC_BUFC_DATA
;
4464 type
= ARC_BUFC_METADATA
;
4468 multilist_sublist_unlock(meta_mls
);
4469 multilist_sublist_unlock(data_mls
);
4475 * Evict buffers from the cache, such that arc_size is capped by arc_c.
4480 uint64_t total_evicted
= 0;
4485 * If we're over arc_meta_limit, we want to correct that before
4486 * potentially evicting data buffers below.
4488 total_evicted
+= arc_adjust_meta();
4493 * If we're over the target cache size, we want to evict enough
4494 * from the list to get back to our target size. We don't want
4495 * to evict too much from the MRU, such that it drops below
4496 * arc_p. So, if we're over our target cache size more than
4497 * the MRU is over arc_p, we'll evict enough to get back to
4498 * arc_p here, and then evict more from the MFU below.
4500 target
= MIN((int64_t)(arc_size
- arc_c
),
4501 (int64_t)(refcount_count(&arc_anon
->arcs_size
) +
4502 refcount_count(&arc_mru
->arcs_size
) + arc_meta_used
- arc_p
));
4505 * If we're below arc_meta_min, always prefer to evict data.
4506 * Otherwise, try to satisfy the requested number of bytes to
4507 * evict from the type which contains older buffers; in an
4508 * effort to keep newer buffers in the cache regardless of their
4509 * type. If we cannot satisfy the number of bytes from this
4510 * type, spill over into the next type.
4512 if (arc_adjust_type(arc_mru
) == ARC_BUFC_METADATA
&&
4513 arc_meta_used
> arc_meta_min
) {
4514 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4515 total_evicted
+= bytes
;
4518 * If we couldn't evict our target number of bytes from
4519 * metadata, we try to get the rest from data.
4524 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
4526 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
4527 total_evicted
+= bytes
;
4530 * If we couldn't evict our target number of bytes from
4531 * data, we try to get the rest from metadata.
4536 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4542 * Now that we've tried to evict enough from the MRU to get its
4543 * size back to arc_p, if we're still above the target cache
4544 * size, we evict the rest from the MFU.
4546 target
= arc_size
- arc_c
;
4548 if (arc_adjust_type(arc_mfu
) == ARC_BUFC_METADATA
&&
4549 arc_meta_used
> arc_meta_min
) {
4550 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4551 total_evicted
+= bytes
;
4554 * If we couldn't evict our target number of bytes from
4555 * metadata, we try to get the rest from data.
4560 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
4562 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
4563 total_evicted
+= bytes
;
4566 * If we couldn't evict our target number of bytes from
4567 * data, we try to get the rest from data.
4572 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4576 * Adjust ghost lists
4578 * In addition to the above, the ARC also defines target values
4579 * for the ghost lists. The sum of the mru list and mru ghost
4580 * list should never exceed the target size of the cache, and
4581 * the sum of the mru list, mfu list, mru ghost list, and mfu
4582 * ghost list should never exceed twice the target size of the
4583 * cache. The following logic enforces these limits on the ghost
4584 * caches, and evicts from them as needed.
4586 target
= refcount_count(&arc_mru
->arcs_size
) +
4587 refcount_count(&arc_mru_ghost
->arcs_size
) - arc_c
;
4589 bytes
= arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_DATA
);
4590 total_evicted
+= bytes
;
4595 arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_METADATA
);
4598 * We assume the sum of the mru list and mfu list is less than
4599 * or equal to arc_c (we enforced this above), which means we
4600 * can use the simpler of the two equations below:
4602 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c
4603 * mru ghost + mfu ghost <= arc_c
4605 target
= refcount_count(&arc_mru_ghost
->arcs_size
) +
4606 refcount_count(&arc_mfu_ghost
->arcs_size
) - arc_c
;
4608 bytes
= arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_DATA
);
4609 total_evicted
+= bytes
;
4614 arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_METADATA
);
4616 return (total_evicted
);
4620 arc_flush(spa_t
*spa
, boolean_t retry
)
4625 * If retry is B_TRUE, a spa must not be specified since we have
4626 * no good way to determine if all of a spa's buffers have been
4627 * evicted from an arc state.
4629 ASSERT(!retry
|| spa
== 0);
4632 guid
= spa_load_guid(spa
);
4634 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_DATA
, retry
);
4635 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_METADATA
, retry
);
4637 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_DATA
, retry
);
4638 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_METADATA
, retry
);
4640 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4641 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4643 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4644 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4648 arc_shrink(int64_t to_free
)
4652 if (c
> to_free
&& c
- to_free
> arc_c_min
) {
4653 arc_c
= c
- to_free
;
4654 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
4655 if (arc_c
> arc_size
)
4656 arc_c
= MAX(arc_size
, arc_c_min
);
4658 arc_p
= (arc_c
>> 1);
4659 ASSERT(arc_c
>= arc_c_min
);
4660 ASSERT((int64_t)arc_p
>= 0);
4665 if (arc_size
> arc_c
)
4666 (void) arc_adjust();
4670 * Return maximum amount of memory that we could possibly use. Reduced
4671 * to half of all memory in user space which is primarily used for testing.
4674 arc_all_memory(void)
4677 #ifdef CONFIG_HIGHMEM
4678 return (ptob(totalram_pages
- totalhigh_pages
));
4680 return (ptob(totalram_pages
));
4681 #endif /* CONFIG_HIGHMEM */
4683 return (ptob(physmem
) / 2);
4684 #endif /* _KERNEL */
4688 * Return the amount of memory that is considered free. In user space
4689 * which is primarily used for testing we pretend that free memory ranges
4690 * from 0-20% of all memory.
4693 arc_free_memory(void)
4696 #ifdef CONFIG_HIGHMEM
4699 return (ptob(si
.freeram
- si
.freehigh
));
4701 return (ptob(nr_free_pages() +
4702 nr_inactive_file_pages() +
4703 nr_inactive_anon_pages() +
4704 nr_slab_reclaimable_pages()));
4706 #endif /* CONFIG_HIGHMEM */
4708 return (spa_get_random(arc_all_memory() * 20 / 100));
4709 #endif /* _KERNEL */
4712 typedef enum free_memory_reason_t
{
4717 FMR_PAGES_PP_MAXIMUM
,
4720 } free_memory_reason_t
;
4722 int64_t last_free_memory
;
4723 free_memory_reason_t last_free_reason
;
4727 * Additional reserve of pages for pp_reserve.
4729 int64_t arc_pages_pp_reserve
= 64;
4732 * Additional reserve of pages for swapfs.
4734 int64_t arc_swapfs_reserve
= 64;
4735 #endif /* _KERNEL */
4738 * Return the amount of memory that can be consumed before reclaim will be
4739 * needed. Positive if there is sufficient free memory, negative indicates
4740 * the amount of memory that needs to be freed up.
4743 arc_available_memory(void)
4745 int64_t lowest
= INT64_MAX
;
4746 free_memory_reason_t r
= FMR_UNKNOWN
;
4753 pgcnt_t needfree
= btop(arc_need_free
);
4754 pgcnt_t lotsfree
= btop(arc_sys_free
);
4755 pgcnt_t desfree
= 0;
4756 pgcnt_t freemem
= btop(arc_free_memory());
4760 n
= PAGESIZE
* (-needfree
);
4768 * check that we're out of range of the pageout scanner. It starts to
4769 * schedule paging if freemem is less than lotsfree and needfree.
4770 * lotsfree is the high-water mark for pageout, and needfree is the
4771 * number of needed free pages. We add extra pages here to make sure
4772 * the scanner doesn't start up while we're freeing memory.
4774 n
= PAGESIZE
* (freemem
- lotsfree
- needfree
- desfree
);
4782 * check to make sure that swapfs has enough space so that anon
4783 * reservations can still succeed. anon_resvmem() checks that the
4784 * availrmem is greater than swapfs_minfree, and the number of reserved
4785 * swap pages. We also add a bit of extra here just to prevent
4786 * circumstances from getting really dire.
4788 n
= PAGESIZE
* (availrmem
- swapfs_minfree
- swapfs_reserve
-
4789 desfree
- arc_swapfs_reserve
);
4792 r
= FMR_SWAPFS_MINFREE
;
4796 * Check that we have enough availrmem that memory locking (e.g., via
4797 * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum
4798 * stores the number of pages that cannot be locked; when availrmem
4799 * drops below pages_pp_maximum, page locking mechanisms such as
4800 * page_pp_lock() will fail.)
4802 n
= PAGESIZE
* (availrmem
- pages_pp_maximum
-
4803 arc_pages_pp_reserve
);
4806 r
= FMR_PAGES_PP_MAXIMUM
;
4812 * If we're on a 32-bit platform, it's possible that we'll exhaust the
4813 * kernel heap space before we ever run out of available physical
4814 * memory. Most checks of the size of the heap_area compare against
4815 * tune.t_minarmem, which is the minimum available real memory that we
4816 * can have in the system. However, this is generally fixed at 25 pages
4817 * which is so low that it's useless. In this comparison, we seek to
4818 * calculate the total heap-size, and reclaim if more than 3/4ths of the
4819 * heap is allocated. (Or, in the calculation, if less than 1/4th is
4822 n
= vmem_size(heap_arena
, VMEM_FREE
) -
4823 (vmem_size(heap_arena
, VMEM_FREE
| VMEM_ALLOC
) >> 2);
4831 * If zio data pages are being allocated out of a separate heap segment,
4832 * then enforce that the size of available vmem for this arena remains
4833 * above about 1/4th (1/(2^arc_zio_arena_free_shift)) free.
4835 * Note that reducing the arc_zio_arena_free_shift keeps more virtual
4836 * memory (in the zio_arena) free, which can avoid memory
4837 * fragmentation issues.
4839 if (zio_arena
!= NULL
) {
4840 n
= (int64_t)vmem_size(zio_arena
, VMEM_FREE
) -
4841 (vmem_size(zio_arena
, VMEM_ALLOC
) >>
4842 arc_zio_arena_free_shift
);
4849 /* Every 100 calls, free a small amount */
4850 if (spa_get_random(100) == 0)
4852 #endif /* _KERNEL */
4854 last_free_memory
= lowest
;
4855 last_free_reason
= r
;
4861 * Determine if the system is under memory pressure and is asking
4862 * to reclaim memory. A return value of B_TRUE indicates that the system
4863 * is under memory pressure and that the arc should adjust accordingly.
4866 arc_reclaim_needed(void)
4868 return (arc_available_memory() < 0);
4872 arc_kmem_reap_now(void)
4875 kmem_cache_t
*prev_cache
= NULL
;
4876 kmem_cache_t
*prev_data_cache
= NULL
;
4877 extern kmem_cache_t
*zio_buf_cache
[];
4878 extern kmem_cache_t
*zio_data_buf_cache
[];
4879 extern kmem_cache_t
*range_seg_cache
;
4882 if ((arc_meta_used
>= arc_meta_limit
) && zfs_arc_meta_prune
) {
4884 * We are exceeding our meta-data cache limit.
4885 * Prune some entries to release holds on meta-data.
4887 arc_prune_async(zfs_arc_meta_prune
);
4891 * Reclaim unused memory from all kmem caches.
4897 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
4899 /* reach upper limit of cache size on 32-bit */
4900 if (zio_buf_cache
[i
] == NULL
)
4903 if (zio_buf_cache
[i
] != prev_cache
) {
4904 prev_cache
= zio_buf_cache
[i
];
4905 kmem_cache_reap_now(zio_buf_cache
[i
]);
4907 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
4908 prev_data_cache
= zio_data_buf_cache
[i
];
4909 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
4912 kmem_cache_reap_now(buf_cache
);
4913 kmem_cache_reap_now(hdr_full_cache
);
4914 kmem_cache_reap_now(hdr_l2only_cache
);
4915 kmem_cache_reap_now(range_seg_cache
);
4917 if (zio_arena
!= NULL
) {
4919 * Ask the vmem arena to reclaim unused memory from its
4922 vmem_qcache_reap(zio_arena
);
4927 * Threads can block in arc_get_data_impl() waiting for this thread to evict
4928 * enough data and signal them to proceed. When this happens, the threads in
4929 * arc_get_data_impl() are sleeping while holding the hash lock for their
4930 * particular arc header. Thus, we must be careful to never sleep on a
4931 * hash lock in this thread. This is to prevent the following deadlock:
4933 * - Thread A sleeps on CV in arc_get_data_impl() holding hash lock "L",
4934 * waiting for the reclaim thread to signal it.
4936 * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter,
4937 * fails, and goes to sleep forever.
4939 * This possible deadlock is avoided by always acquiring a hash lock
4940 * using mutex_tryenter() from arc_reclaim_thread().
4944 arc_reclaim_thread(void *unused
)
4946 fstrans_cookie_t cookie
= spl_fstrans_mark();
4947 hrtime_t growtime
= 0;
4950 CALLB_CPR_INIT(&cpr
, &arc_reclaim_lock
, callb_generic_cpr
, FTAG
);
4952 mutex_enter(&arc_reclaim_lock
);
4953 while (!arc_reclaim_thread_exit
) {
4954 uint64_t evicted
= 0;
4955 uint64_t need_free
= arc_need_free
;
4956 arc_tuning_update();
4959 * This is necessary in order for the mdb ::arc dcmd to
4960 * show up to date information. Since the ::arc command
4961 * does not call the kstat's update function, without
4962 * this call, the command may show stale stats for the
4963 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
4964 * with this change, the data might be up to 1 second
4965 * out of date; but that should suffice. The arc_state_t
4966 * structures can be queried directly if more accurate
4967 * information is needed.
4970 if (arc_ksp
!= NULL
)
4971 arc_ksp
->ks_update(arc_ksp
, KSTAT_READ
);
4973 mutex_exit(&arc_reclaim_lock
);
4976 * We call arc_adjust() before (possibly) calling
4977 * arc_kmem_reap_now(), so that we can wake up
4978 * arc_get_data_buf() sooner.
4980 evicted
= arc_adjust();
4982 int64_t free_memory
= arc_available_memory();
4983 if (free_memory
< 0) {
4985 arc_no_grow
= B_TRUE
;
4989 * Wait at least zfs_grow_retry (default 5) seconds
4990 * before considering growing.
4992 growtime
= gethrtime() + SEC2NSEC(arc_grow_retry
);
4994 arc_kmem_reap_now();
4997 * If we are still low on memory, shrink the ARC
4998 * so that we have arc_shrink_min free space.
5000 free_memory
= arc_available_memory();
5003 (arc_c
>> arc_shrink_shift
) - free_memory
;
5006 to_free
= MAX(to_free
, need_free
);
5008 arc_shrink(to_free
);
5010 } else if (free_memory
< arc_c
>> arc_no_grow_shift
) {
5011 arc_no_grow
= B_TRUE
;
5012 } else if (gethrtime() >= growtime
) {
5013 arc_no_grow
= B_FALSE
;
5016 mutex_enter(&arc_reclaim_lock
);
5019 * If evicted is zero, we couldn't evict anything via
5020 * arc_adjust(). This could be due to hash lock
5021 * collisions, but more likely due to the majority of
5022 * arc buffers being unevictable. Therefore, even if
5023 * arc_size is above arc_c, another pass is unlikely to
5024 * be helpful and could potentially cause us to enter an
5027 if (arc_size
<= arc_c
|| evicted
== 0) {
5029 * We're either no longer overflowing, or we
5030 * can't evict anything more, so we should wake
5031 * up any threads before we go to sleep and remove
5032 * the bytes we were working on from arc_need_free
5033 * since nothing more will be done here.
5035 cv_broadcast(&arc_reclaim_waiters_cv
);
5036 ARCSTAT_INCR(arcstat_need_free
, -need_free
);
5039 * Block until signaled, or after one second (we
5040 * might need to perform arc_kmem_reap_now()
5041 * even if we aren't being signalled)
5043 CALLB_CPR_SAFE_BEGIN(&cpr
);
5044 (void) cv_timedwait_sig_hires(&arc_reclaim_thread_cv
,
5045 &arc_reclaim_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
5046 CALLB_CPR_SAFE_END(&cpr
, &arc_reclaim_lock
);
5050 arc_reclaim_thread_exit
= B_FALSE
;
5051 cv_broadcast(&arc_reclaim_thread_cv
);
5052 CALLB_CPR_EXIT(&cpr
); /* drops arc_reclaim_lock */
5053 spl_fstrans_unmark(cookie
);
5059 * Determine the amount of memory eligible for eviction contained in the
5060 * ARC. All clean data reported by the ghost lists can always be safely
5061 * evicted. Due to arc_c_min, the same does not hold for all clean data
5062 * contained by the regular mru and mfu lists.
5064 * In the case of the regular mru and mfu lists, we need to report as
5065 * much clean data as possible, such that evicting that same reported
5066 * data will not bring arc_size below arc_c_min. Thus, in certain
5067 * circumstances, the total amount of clean data in the mru and mfu
5068 * lists might not actually be evictable.
5070 * The following two distinct cases are accounted for:
5072 * 1. The sum of the amount of dirty data contained by both the mru and
5073 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
5074 * is greater than or equal to arc_c_min.
5075 * (i.e. amount of dirty data >= arc_c_min)
5077 * This is the easy case; all clean data contained by the mru and mfu
5078 * lists is evictable. Evicting all clean data can only drop arc_size
5079 * to the amount of dirty data, which is greater than arc_c_min.
5081 * 2. The sum of the amount of dirty data contained by both the mru and
5082 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
5083 * is less than arc_c_min.
5084 * (i.e. arc_c_min > amount of dirty data)
5086 * 2.1. arc_size is greater than or equal arc_c_min.
5087 * (i.e. arc_size >= arc_c_min > amount of dirty data)
5089 * In this case, not all clean data from the regular mru and mfu
5090 * lists is actually evictable; we must leave enough clean data
5091 * to keep arc_size above arc_c_min. Thus, the maximum amount of
5092 * evictable data from the two lists combined, is exactly the
5093 * difference between arc_size and arc_c_min.
5095 * 2.2. arc_size is less than arc_c_min
5096 * (i.e. arc_c_min > arc_size > amount of dirty data)
5098 * In this case, none of the data contained in the mru and mfu
5099 * lists is evictable, even if it's clean. Since arc_size is
5100 * already below arc_c_min, evicting any more would only
5101 * increase this negative difference.
5104 arc_evictable_memory(void)
5106 uint64_t arc_clean
=
5107 refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]) +
5108 refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]) +
5109 refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]) +
5110 refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
5111 uint64_t arc_dirty
= MAX((int64_t)arc_size
- (int64_t)arc_clean
, 0);
5114 * Scale reported evictable memory in proportion to page cache, cap
5115 * at specified min/max.
5117 uint64_t min
= (ptob(nr_file_pages()) / 100) * zfs_arc_pc_percent
;
5118 min
= MAX(arc_c_min
, MIN(arc_c_max
, min
));
5120 if (arc_dirty
>= min
)
5123 return (MAX((int64_t)arc_size
- (int64_t)min
, 0));
5127 * If sc->nr_to_scan is zero, the caller is requesting a query of the
5128 * number of objects which can potentially be freed. If it is nonzero,
5129 * the request is to free that many objects.
5131 * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks
5132 * in struct shrinker and also require the shrinker to return the number
5135 * Older kernels require the shrinker to return the number of freeable
5136 * objects following the freeing of nr_to_free.
5138 static spl_shrinker_t
5139 __arc_shrinker_func(struct shrinker
*shrink
, struct shrink_control
*sc
)
5143 /* The arc is considered warm once reclaim has occurred */
5144 if (unlikely(arc_warm
== B_FALSE
))
5147 /* Return the potential number of reclaimable pages */
5148 pages
= btop((int64_t)arc_evictable_memory());
5149 if (sc
->nr_to_scan
== 0)
5152 /* Not allowed to perform filesystem reclaim */
5153 if (!(sc
->gfp_mask
& __GFP_FS
))
5154 return (SHRINK_STOP
);
5156 /* Reclaim in progress */
5157 if (mutex_tryenter(&arc_reclaim_lock
) == 0) {
5158 ARCSTAT_INCR(arcstat_need_free
, ptob(sc
->nr_to_scan
));
5162 mutex_exit(&arc_reclaim_lock
);
5165 * Evict the requested number of pages by shrinking arc_c the
5169 arc_shrink(ptob(sc
->nr_to_scan
));
5170 if (current_is_kswapd())
5171 arc_kmem_reap_now();
5172 #ifdef HAVE_SPLIT_SHRINKER_CALLBACK
5173 pages
= MAX((int64_t)pages
-
5174 (int64_t)btop(arc_evictable_memory()), 0);
5176 pages
= btop(arc_evictable_memory());
5179 * We've shrunk what we can, wake up threads.
5181 cv_broadcast(&arc_reclaim_waiters_cv
);
5183 pages
= SHRINK_STOP
;
5186 * When direct reclaim is observed it usually indicates a rapid
5187 * increase in memory pressure. This occurs because the kswapd
5188 * threads were unable to asynchronously keep enough free memory
5189 * available. In this case set arc_no_grow to briefly pause arc
5190 * growth to avoid compounding the memory pressure.
5192 if (current_is_kswapd()) {
5193 ARCSTAT_BUMP(arcstat_memory_indirect_count
);
5195 arc_no_grow
= B_TRUE
;
5196 arc_kmem_reap_now();
5197 ARCSTAT_BUMP(arcstat_memory_direct_count
);
5202 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func
);
5204 SPL_SHRINKER_DECLARE(arc_shrinker
, arc_shrinker_func
, DEFAULT_SEEKS
);
5205 #endif /* _KERNEL */
5208 * Adapt arc info given the number of bytes we are trying to add and
5209 * the state that we are coming from. This function is only called
5210 * when we are adding new content to the cache.
5213 arc_adapt(int bytes
, arc_state_t
*state
)
5216 uint64_t arc_p_min
= (arc_c
>> arc_p_min_shift
);
5217 int64_t mrug_size
= refcount_count(&arc_mru_ghost
->arcs_size
);
5218 int64_t mfug_size
= refcount_count(&arc_mfu_ghost
->arcs_size
);
5220 if (state
== arc_l2c_only
)
5225 * Adapt the target size of the MRU list:
5226 * - if we just hit in the MRU ghost list, then increase
5227 * the target size of the MRU list.
5228 * - if we just hit in the MFU ghost list, then increase
5229 * the target size of the MFU list by decreasing the
5230 * target size of the MRU list.
5232 if (state
== arc_mru_ghost
) {
5233 mult
= (mrug_size
>= mfug_size
) ? 1 : (mfug_size
/ mrug_size
);
5234 if (!zfs_arc_p_dampener_disable
)
5235 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
5237 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
5238 } else if (state
== arc_mfu_ghost
) {
5241 mult
= (mfug_size
>= mrug_size
) ? 1 : (mrug_size
/ mfug_size
);
5242 if (!zfs_arc_p_dampener_disable
)
5243 mult
= MIN(mult
, 10);
5245 delta
= MIN(bytes
* mult
, arc_p
);
5246 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
5248 ASSERT((int64_t)arc_p
>= 0);
5250 if (arc_reclaim_needed()) {
5251 cv_signal(&arc_reclaim_thread_cv
);
5258 if (arc_c
>= arc_c_max
)
5262 * If we're within (2 * maxblocksize) bytes of the target
5263 * cache size, increment the target cache size
5265 ASSERT3U(arc_c
, >=, 2ULL << SPA_MAXBLOCKSHIFT
);
5266 if (arc_size
>= arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
5267 atomic_add_64(&arc_c
, (int64_t)bytes
);
5268 if (arc_c
> arc_c_max
)
5270 else if (state
== arc_anon
)
5271 atomic_add_64(&arc_p
, (int64_t)bytes
);
5275 ASSERT((int64_t)arc_p
>= 0);
5279 * Check if arc_size has grown past our upper threshold, determined by
5280 * zfs_arc_overflow_shift.
5283 arc_is_overflowing(void)
5285 /* Always allow at least one block of overflow */
5286 uint64_t overflow
= MAX(SPA_MAXBLOCKSIZE
,
5287 arc_c
>> zfs_arc_overflow_shift
);
5289 return (arc_size
>= arc_c
+ overflow
);
5293 arc_get_data_abd(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5295 arc_buf_contents_t type
= arc_buf_type(hdr
);
5297 arc_get_data_impl(hdr
, size
, tag
);
5298 if (type
== ARC_BUFC_METADATA
) {
5299 return (abd_alloc(size
, B_TRUE
));
5301 ASSERT(type
== ARC_BUFC_DATA
);
5302 return (abd_alloc(size
, B_FALSE
));
5307 arc_get_data_buf(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5309 arc_buf_contents_t type
= arc_buf_type(hdr
);
5311 arc_get_data_impl(hdr
, size
, tag
);
5312 if (type
== ARC_BUFC_METADATA
) {
5313 return (zio_buf_alloc(size
));
5315 ASSERT(type
== ARC_BUFC_DATA
);
5316 return (zio_data_buf_alloc(size
));
5321 * Allocate a block and return it to the caller. If we are hitting the
5322 * hard limit for the cache size, we must sleep, waiting for the eviction
5323 * thread to catch up. If we're past the target size but below the hard
5324 * limit, we'll only signal the reclaim thread and continue on.
5327 arc_get_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5329 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5330 arc_buf_contents_t type
= arc_buf_type(hdr
);
5332 arc_adapt(size
, state
);
5335 * If arc_size is currently overflowing, and has grown past our
5336 * upper limit, we must be adding data faster than the evict
5337 * thread can evict. Thus, to ensure we don't compound the
5338 * problem by adding more data and forcing arc_size to grow even
5339 * further past it's target size, we halt and wait for the
5340 * eviction thread to catch up.
5342 * It's also possible that the reclaim thread is unable to evict
5343 * enough buffers to get arc_size below the overflow limit (e.g.
5344 * due to buffers being un-evictable, or hash lock collisions).
5345 * In this case, we want to proceed regardless if we're
5346 * overflowing; thus we don't use a while loop here.
5348 if (arc_is_overflowing()) {
5349 mutex_enter(&arc_reclaim_lock
);
5352 * Now that we've acquired the lock, we may no longer be
5353 * over the overflow limit, lets check.
5355 * We're ignoring the case of spurious wake ups. If that
5356 * were to happen, it'd let this thread consume an ARC
5357 * buffer before it should have (i.e. before we're under
5358 * the overflow limit and were signalled by the reclaim
5359 * thread). As long as that is a rare occurrence, it
5360 * shouldn't cause any harm.
5362 if (arc_is_overflowing()) {
5363 cv_signal(&arc_reclaim_thread_cv
);
5364 cv_wait(&arc_reclaim_waiters_cv
, &arc_reclaim_lock
);
5367 mutex_exit(&arc_reclaim_lock
);
5370 VERIFY3U(hdr
->b_type
, ==, type
);
5371 if (type
== ARC_BUFC_METADATA
) {
5372 arc_space_consume(size
, ARC_SPACE_META
);
5374 arc_space_consume(size
, ARC_SPACE_DATA
);
5378 * Update the state size. Note that ghost states have a
5379 * "ghost size" and so don't need to be updated.
5381 if (!GHOST_STATE(state
)) {
5383 (void) refcount_add_many(&state
->arcs_size
, size
, tag
);
5386 * If this is reached via arc_read, the link is
5387 * protected by the hash lock. If reached via
5388 * arc_buf_alloc, the header should not be accessed by
5389 * any other thread. And, if reached via arc_read_done,
5390 * the hash lock will protect it if it's found in the
5391 * hash table; otherwise no other thread should be
5392 * trying to [add|remove]_reference it.
5394 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5395 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5396 (void) refcount_add_many(&state
->arcs_esize
[type
],
5401 * If we are growing the cache, and we are adding anonymous
5402 * data, and we have outgrown arc_p, update arc_p
5404 if (arc_size
< arc_c
&& hdr
->b_l1hdr
.b_state
== arc_anon
&&
5405 (refcount_count(&arc_anon
->arcs_size
) +
5406 refcount_count(&arc_mru
->arcs_size
) > arc_p
))
5407 arc_p
= MIN(arc_c
, arc_p
+ size
);
5412 arc_free_data_abd(arc_buf_hdr_t
*hdr
, abd_t
*abd
, uint64_t size
, void *tag
)
5414 arc_free_data_impl(hdr
, size
, tag
);
5419 arc_free_data_buf(arc_buf_hdr_t
*hdr
, void *buf
, uint64_t size
, void *tag
)
5421 arc_buf_contents_t type
= arc_buf_type(hdr
);
5423 arc_free_data_impl(hdr
, size
, tag
);
5424 if (type
== ARC_BUFC_METADATA
) {
5425 zio_buf_free(buf
, size
);
5427 ASSERT(type
== ARC_BUFC_DATA
);
5428 zio_data_buf_free(buf
, size
);
5433 * Free the arc data buffer.
5436 arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5438 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5439 arc_buf_contents_t type
= arc_buf_type(hdr
);
5441 /* protected by hash lock, if in the hash table */
5442 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5443 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5444 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
5446 (void) refcount_remove_many(&state
->arcs_esize
[type
],
5449 (void) refcount_remove_many(&state
->arcs_size
, size
, tag
);
5451 VERIFY3U(hdr
->b_type
, ==, type
);
5452 if (type
== ARC_BUFC_METADATA
) {
5453 arc_space_return(size
, ARC_SPACE_META
);
5455 ASSERT(type
== ARC_BUFC_DATA
);
5456 arc_space_return(size
, ARC_SPACE_DATA
);
5461 * This routine is called whenever a buffer is accessed.
5462 * NOTE: the hash lock is dropped in this function.
5465 arc_access(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
5469 ASSERT(MUTEX_HELD(hash_lock
));
5470 ASSERT(HDR_HAS_L1HDR(hdr
));
5472 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
5474 * This buffer is not in the cache, and does not
5475 * appear in our "ghost" list. Add the new buffer
5479 ASSERT0(hdr
->b_l1hdr
.b_arc_access
);
5480 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5481 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5482 arc_change_state(arc_mru
, hdr
, hash_lock
);
5484 } else if (hdr
->b_l1hdr
.b_state
== arc_mru
) {
5485 now
= ddi_get_lbolt();
5488 * If this buffer is here because of a prefetch, then either:
5489 * - clear the flag if this is a "referencing" read
5490 * (any subsequent access will bump this into the MFU state).
5492 * - move the buffer to the head of the list if this is
5493 * another prefetch (to make it less likely to be evicted).
5495 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5496 if (refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
5497 /* link protected by hash lock */
5498 ASSERT(multilist_link_active(
5499 &hdr
->b_l1hdr
.b_arc_node
));
5501 arc_hdr_clear_flags(hdr
,
5503 ARC_FLAG_PRESCIENT_PREFETCH
);
5504 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
5505 ARCSTAT_BUMP(arcstat_mru_hits
);
5507 hdr
->b_l1hdr
.b_arc_access
= now
;
5512 * This buffer has been "accessed" only once so far,
5513 * but it is still in the cache. Move it to the MFU
5516 if (ddi_time_after(now
, hdr
->b_l1hdr
.b_arc_access
+
5519 * More than 125ms have passed since we
5520 * instantiated this buffer. Move it to the
5521 * most frequently used state.
5523 hdr
->b_l1hdr
.b_arc_access
= now
;
5524 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5525 arc_change_state(arc_mfu
, hdr
, hash_lock
);
5527 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
5528 ARCSTAT_BUMP(arcstat_mru_hits
);
5529 } else if (hdr
->b_l1hdr
.b_state
== arc_mru_ghost
) {
5530 arc_state_t
*new_state
;
5532 * This buffer has been "accessed" recently, but
5533 * was evicted from the cache. Move it to the
5537 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5538 new_state
= arc_mru
;
5539 if (refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0) {
5540 arc_hdr_clear_flags(hdr
,
5542 ARC_FLAG_PRESCIENT_PREFETCH
);
5544 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5546 new_state
= arc_mfu
;
5547 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5550 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5551 arc_change_state(new_state
, hdr
, hash_lock
);
5553 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_ghost_hits
);
5554 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
5555 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu
) {
5557 * This buffer has been accessed more than once and is
5558 * still in the cache. Keep it in the MFU state.
5560 * NOTE: an add_reference() that occurred when we did
5561 * the arc_read() will have kicked this off the list.
5562 * If it was a prefetch, we will explicitly move it to
5563 * the head of the list now.
5566 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_hits
);
5567 ARCSTAT_BUMP(arcstat_mfu_hits
);
5568 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5569 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu_ghost
) {
5570 arc_state_t
*new_state
= arc_mfu
;
5572 * This buffer has been accessed more than once but has
5573 * been evicted from the cache. Move it back to the
5577 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5579 * This is a prefetch access...
5580 * move this block back to the MRU state.
5582 new_state
= arc_mru
;
5585 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5586 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5587 arc_change_state(new_state
, hdr
, hash_lock
);
5589 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_ghost_hits
);
5590 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
5591 } else if (hdr
->b_l1hdr
.b_state
== arc_l2c_only
) {
5593 * This buffer is on the 2nd Level ARC.
5596 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5597 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5598 arc_change_state(arc_mfu
, hdr
, hash_lock
);
5600 cmn_err(CE_PANIC
, "invalid arc state 0x%p",
5601 hdr
->b_l1hdr
.b_state
);
5606 * This routine is called by dbuf_hold() to update the arc_access() state
5607 * which otherwise would be skipped for entries in the dbuf cache.
5610 arc_buf_access(arc_buf_t
*buf
)
5612 mutex_enter(&buf
->b_evict_lock
);
5613 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5616 * Avoid taking the hash_lock when possible as an optimization.
5617 * The header must be checked again under the hash_lock in order
5618 * to handle the case where it is concurrently being released.
5620 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5621 mutex_exit(&buf
->b_evict_lock
);
5625 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
5626 mutex_enter(hash_lock
);
5628 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5629 mutex_exit(hash_lock
);
5630 mutex_exit(&buf
->b_evict_lock
);
5631 ARCSTAT_BUMP(arcstat_access_skip
);
5635 mutex_exit(&buf
->b_evict_lock
);
5637 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
5638 hdr
->b_l1hdr
.b_state
== arc_mfu
);
5640 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
5641 arc_access(hdr
, hash_lock
);
5642 mutex_exit(hash_lock
);
5644 ARCSTAT_BUMP(arcstat_hits
);
5645 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
) && !HDR_PRESCIENT_PREFETCH(hdr
),
5646 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
), data
, metadata
, hits
);
5649 /* a generic arc_read_done_func_t which you can use */
5652 arc_bcopy_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5653 arc_buf_t
*buf
, void *arg
)
5658 bcopy(buf
->b_data
, arg
, arc_buf_size(buf
));
5659 arc_buf_destroy(buf
, arg
);
5662 /* a generic arc_read_done_func_t */
5665 arc_getbuf_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5666 arc_buf_t
*buf
, void *arg
)
5668 arc_buf_t
**bufp
= arg
;
5674 ASSERT(buf
->b_data
);
5679 arc_hdr_verify(arc_buf_hdr_t
*hdr
, blkptr_t
*bp
)
5681 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
5682 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, 0);
5683 ASSERT3U(arc_hdr_get_compress(hdr
), ==, ZIO_COMPRESS_OFF
);
5685 if (HDR_COMPRESSION_ENABLED(hdr
)) {
5686 ASSERT3U(arc_hdr_get_compress(hdr
), ==,
5687 BP_GET_COMPRESS(bp
));
5689 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
5690 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, BP_GET_PSIZE(bp
));
5691 ASSERT3U(!!HDR_PROTECTED(hdr
), ==, BP_IS_PROTECTED(bp
));
5696 arc_read_done(zio_t
*zio
)
5698 blkptr_t
*bp
= zio
->io_bp
;
5699 arc_buf_hdr_t
*hdr
= zio
->io_private
;
5700 kmutex_t
*hash_lock
= NULL
;
5701 arc_callback_t
*callback_list
;
5702 arc_callback_t
*acb
;
5703 boolean_t freeable
= B_FALSE
;
5706 * The hdr was inserted into hash-table and removed from lists
5707 * prior to starting I/O. We should find this header, since
5708 * it's in the hash table, and it should be legit since it's
5709 * not possible to evict it during the I/O. The only possible
5710 * reason for it not to be found is if we were freed during the
5713 if (HDR_IN_HASH_TABLE(hdr
)) {
5714 arc_buf_hdr_t
*found
;
5716 ASSERT3U(hdr
->b_birth
, ==, BP_PHYSICAL_BIRTH(zio
->io_bp
));
5717 ASSERT3U(hdr
->b_dva
.dva_word
[0], ==,
5718 BP_IDENTITY(zio
->io_bp
)->dva_word
[0]);
5719 ASSERT3U(hdr
->b_dva
.dva_word
[1], ==,
5720 BP_IDENTITY(zio
->io_bp
)->dva_word
[1]);
5722 found
= buf_hash_find(hdr
->b_spa
, zio
->io_bp
, &hash_lock
);
5724 ASSERT((found
== hdr
&&
5725 DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
5726 (found
== hdr
&& HDR_L2_READING(hdr
)));
5727 ASSERT3P(hash_lock
, !=, NULL
);
5730 if (BP_IS_PROTECTED(bp
)) {
5731 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
5732 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
5733 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
5734 hdr
->b_crypt_hdr
.b_iv
);
5736 if (BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
) {
5739 tmpbuf
= abd_borrow_buf_copy(zio
->io_abd
,
5740 sizeof (zil_chain_t
));
5741 zio_crypt_decode_mac_zil(tmpbuf
,
5742 hdr
->b_crypt_hdr
.b_mac
);
5743 abd_return_buf(zio
->io_abd
, tmpbuf
,
5744 sizeof (zil_chain_t
));
5746 zio_crypt_decode_mac_bp(bp
, hdr
->b_crypt_hdr
.b_mac
);
5750 if (zio
->io_error
== 0) {
5751 /* byteswap if necessary */
5752 if (BP_SHOULD_BYTESWAP(zio
->io_bp
)) {
5753 if (BP_GET_LEVEL(zio
->io_bp
) > 0) {
5754 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
5756 hdr
->b_l1hdr
.b_byteswap
=
5757 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
5760 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
5764 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_EVICTED
);
5765 if (l2arc_noprefetch
&& HDR_PREFETCH(hdr
))
5766 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2CACHE
);
5768 callback_list
= hdr
->b_l1hdr
.b_acb
;
5769 ASSERT3P(callback_list
, !=, NULL
);
5771 if (hash_lock
&& zio
->io_error
== 0 &&
5772 hdr
->b_l1hdr
.b_state
== arc_anon
) {
5774 * Only call arc_access on anonymous buffers. This is because
5775 * if we've issued an I/O for an evicted buffer, we've already
5776 * called arc_access (to prevent any simultaneous readers from
5777 * getting confused).
5779 arc_access(hdr
, hash_lock
);
5783 * If a read request has a callback (i.e. acb_done is not NULL), then we
5784 * make a buf containing the data according to the parameters which were
5785 * passed in. The implementation of arc_buf_alloc_impl() ensures that we
5786 * aren't needlessly decompressing the data multiple times.
5788 int callback_cnt
= 0;
5789 for (acb
= callback_list
; acb
!= NULL
; acb
= acb
->acb_next
) {
5795 if (zio
->io_error
!= 0)
5798 int error
= arc_buf_alloc_impl(hdr
, zio
->io_spa
,
5799 &acb
->acb_zb
, acb
->acb_private
, acb
->acb_encrypted
,
5800 acb
->acb_compressed
, acb
->acb_noauth
, B_TRUE
,
5803 (void) remove_reference(hdr
, hash_lock
,
5805 arc_buf_destroy_impl(acb
->acb_buf
);
5806 acb
->acb_buf
= NULL
;
5810 * Assert non-speculative zios didn't fail because an
5811 * encryption key wasn't loaded
5813 ASSERT((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) ||
5817 * If we failed to decrypt, report an error now (as the zio
5818 * layer would have done if it had done the transforms).
5820 if (error
== ECKSUM
) {
5821 ASSERT(BP_IS_PROTECTED(bp
));
5822 error
= SET_ERROR(EIO
);
5823 if ((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
5824 spa_log_error(zio
->io_spa
, &acb
->acb_zb
);
5825 zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION
,
5826 zio
->io_spa
, NULL
, &acb
->acb_zb
, zio
, 0, 0);
5830 if (zio
->io_error
== 0)
5831 zio
->io_error
= error
;
5833 hdr
->b_l1hdr
.b_acb
= NULL
;
5834 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5835 if (callback_cnt
== 0)
5836 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
5838 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
) ||
5839 callback_list
!= NULL
);
5841 if (zio
->io_error
== 0) {
5842 arc_hdr_verify(hdr
, zio
->io_bp
);
5844 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
5845 if (hdr
->b_l1hdr
.b_state
!= arc_anon
)
5846 arc_change_state(arc_anon
, hdr
, hash_lock
);
5847 if (HDR_IN_HASH_TABLE(hdr
))
5848 buf_hash_remove(hdr
);
5849 freeable
= refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
5853 * Broadcast before we drop the hash_lock to avoid the possibility
5854 * that the hdr (and hence the cv) might be freed before we get to
5855 * the cv_broadcast().
5857 cv_broadcast(&hdr
->b_l1hdr
.b_cv
);
5859 if (hash_lock
!= NULL
) {
5860 mutex_exit(hash_lock
);
5863 * This block was freed while we waited for the read to
5864 * complete. It has been removed from the hash table and
5865 * moved to the anonymous state (so that it won't show up
5868 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
5869 freeable
= refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
5872 /* execute each callback and free its structure */
5873 while ((acb
= callback_list
) != NULL
) {
5874 if (acb
->acb_done
) {
5875 acb
->acb_done(zio
, &zio
->io_bookmark
, zio
->io_bp
,
5876 acb
->acb_buf
, acb
->acb_private
);
5879 if (acb
->acb_zio_dummy
!= NULL
) {
5880 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
5881 zio_nowait(acb
->acb_zio_dummy
);
5884 callback_list
= acb
->acb_next
;
5885 kmem_free(acb
, sizeof (arc_callback_t
));
5889 arc_hdr_destroy(hdr
);
5893 * "Read" the block at the specified DVA (in bp) via the
5894 * cache. If the block is found in the cache, invoke the provided
5895 * callback immediately and return. Note that the `zio' parameter
5896 * in the callback will be NULL in this case, since no IO was
5897 * required. If the block is not in the cache pass the read request
5898 * on to the spa with a substitute callback function, so that the
5899 * requested block will be added to the cache.
5901 * If a read request arrives for a block that has a read in-progress,
5902 * either wait for the in-progress read to complete (and return the
5903 * results); or, if this is a read with a "done" func, add a record
5904 * to the read to invoke the "done" func when the read completes,
5905 * and return; or just return.
5907 * arc_read_done() will invoke all the requested "done" functions
5908 * for readers of this block.
5911 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
5912 arc_read_done_func_t
*done
, void *private, zio_priority_t priority
,
5913 int zio_flags
, arc_flags_t
*arc_flags
, const zbookmark_phys_t
*zb
)
5915 arc_buf_hdr_t
*hdr
= NULL
;
5916 kmutex_t
*hash_lock
= NULL
;
5918 uint64_t guid
= spa_load_guid(spa
);
5919 boolean_t compressed_read
= (zio_flags
& ZIO_FLAG_RAW_COMPRESS
) != 0;
5920 boolean_t encrypted_read
= BP_IS_ENCRYPTED(bp
) &&
5921 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
5922 boolean_t noauth_read
= BP_IS_AUTHENTICATED(bp
) &&
5923 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
5926 ASSERT(!BP_IS_EMBEDDED(bp
) ||
5927 BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
);
5930 if (!BP_IS_EMBEDDED(bp
)) {
5932 * Embedded BP's have no DVA and require no I/O to "read".
5933 * Create an anonymous arc buf to back it.
5935 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
5939 * Determine if we have an L1 cache hit or a cache miss. For simplicity
5940 * we maintain encrypted data seperately from compressed / uncompressed
5941 * data. If the user is requesting raw encrypted data and we don't have
5942 * that in the header we will read from disk to guarantee that we can
5943 * get it even if the encryption keys aren't loaded.
5945 if (hdr
!= NULL
&& HDR_HAS_L1HDR(hdr
) && (HDR_HAS_RABD(hdr
) ||
5946 (hdr
->b_l1hdr
.b_pabd
!= NULL
&& !encrypted_read
))) {
5947 arc_buf_t
*buf
= NULL
;
5948 *arc_flags
|= ARC_FLAG_CACHED
;
5950 if (HDR_IO_IN_PROGRESS(hdr
)) {
5951 zio_t
*head_zio
= hdr
->b_l1hdr
.b_acb
->acb_zio_head
;
5953 ASSERT3P(head_zio
, !=, NULL
);
5954 if ((hdr
->b_flags
& ARC_FLAG_PRIO_ASYNC_READ
) &&
5955 priority
== ZIO_PRIORITY_SYNC_READ
) {
5957 * This is a sync read that needs to wait for
5958 * an in-flight async read. Request that the
5959 * zio have its priority upgraded.
5961 zio_change_priority(head_zio
, priority
);
5962 DTRACE_PROBE1(arc__async__upgrade__sync
,
5963 arc_buf_hdr_t
*, hdr
);
5964 ARCSTAT_BUMP(arcstat_async_upgrade_sync
);
5966 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
5967 arc_hdr_clear_flags(hdr
,
5968 ARC_FLAG_PREDICTIVE_PREFETCH
);
5971 if (*arc_flags
& ARC_FLAG_WAIT
) {
5972 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
5973 mutex_exit(hash_lock
);
5976 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
5979 arc_callback_t
*acb
= NULL
;
5981 acb
= kmem_zalloc(sizeof (arc_callback_t
),
5983 acb
->acb_done
= done
;
5984 acb
->acb_private
= private;
5985 acb
->acb_compressed
= compressed_read
;
5986 acb
->acb_encrypted
= encrypted_read
;
5987 acb
->acb_noauth
= noauth_read
;
5990 acb
->acb_zio_dummy
= zio_null(pio
,
5991 spa
, NULL
, NULL
, NULL
, zio_flags
);
5993 ASSERT3P(acb
->acb_done
, !=, NULL
);
5994 acb
->acb_zio_head
= head_zio
;
5995 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
5996 hdr
->b_l1hdr
.b_acb
= acb
;
5997 mutex_exit(hash_lock
);
6000 mutex_exit(hash_lock
);
6004 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
6005 hdr
->b_l1hdr
.b_state
== arc_mfu
);
6008 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
6010 * This is a demand read which does not have to
6011 * wait for i/o because we did a predictive
6012 * prefetch i/o for it, which has completed.
6015 arc__demand__hit__predictive__prefetch
,
6016 arc_buf_hdr_t
*, hdr
);
6018 arcstat_demand_hit_predictive_prefetch
);
6019 arc_hdr_clear_flags(hdr
,
6020 ARC_FLAG_PREDICTIVE_PREFETCH
);
6023 if (hdr
->b_flags
& ARC_FLAG_PRESCIENT_PREFETCH
) {
6025 arcstat_demand_hit_prescient_prefetch
);
6026 arc_hdr_clear_flags(hdr
,
6027 ARC_FLAG_PRESCIENT_PREFETCH
);
6030 ASSERT(!BP_IS_EMBEDDED(bp
) || !BP_IS_HOLE(bp
));
6032 /* Get a buf with the desired data in it. */
6033 rc
= arc_buf_alloc_impl(hdr
, spa
, zb
, private,
6034 encrypted_read
, compressed_read
, noauth_read
,
6038 * Convert authentication and decryption errors
6039 * to EIO (and generate an ereport if needed)
6040 * before leaving the ARC.
6042 rc
= SET_ERROR(EIO
);
6043 if ((zio_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
6044 spa_log_error(spa
, zb
);
6046 FM_EREPORT_ZFS_AUTHENTICATION
,
6047 spa
, NULL
, zb
, NULL
, 0, 0);
6051 (void) remove_reference(hdr
, hash_lock
,
6053 arc_buf_destroy_impl(buf
);
6057 /* assert any errors weren't due to unloaded keys */
6058 ASSERT((zio_flags
& ZIO_FLAG_SPECULATIVE
) ||
6060 } else if (*arc_flags
& ARC_FLAG_PREFETCH
&&
6061 refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
6062 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
6064 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
6065 arc_access(hdr
, hash_lock
);
6066 if (*arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
)
6067 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
6068 if (*arc_flags
& ARC_FLAG_L2CACHE
)
6069 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6070 mutex_exit(hash_lock
);
6071 ARCSTAT_BUMP(arcstat_hits
);
6072 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
6073 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
6074 data
, metadata
, hits
);
6077 done(NULL
, zb
, bp
, buf
, private);
6079 uint64_t lsize
= BP_GET_LSIZE(bp
);
6080 uint64_t psize
= BP_GET_PSIZE(bp
);
6081 arc_callback_t
*acb
;
6084 boolean_t devw
= B_FALSE
;
6089 * Gracefully handle a damaged logical block size as a
6092 if (lsize
> spa_maxblocksize(spa
)) {
6093 rc
= SET_ERROR(ECKSUM
);
6098 /* this block is not in the cache */
6099 arc_buf_hdr_t
*exists
= NULL
;
6100 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
6101 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
6102 BP_IS_PROTECTED(bp
), BP_GET_COMPRESS(bp
), type
,
6105 if (!BP_IS_EMBEDDED(bp
)) {
6106 hdr
->b_dva
= *BP_IDENTITY(bp
);
6107 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
6108 exists
= buf_hash_insert(hdr
, &hash_lock
);
6110 if (exists
!= NULL
) {
6111 /* somebody beat us to the hash insert */
6112 mutex_exit(hash_lock
);
6113 buf_discard_identity(hdr
);
6114 arc_hdr_destroy(hdr
);
6115 goto top
; /* restart the IO request */
6119 * This block is in the ghost cache or encrypted data
6120 * was requested and we didn't have it. If it was
6121 * L2-only (and thus didn't have an L1 hdr),
6122 * we realloc the header to add an L1 hdr.
6124 if (!HDR_HAS_L1HDR(hdr
)) {
6125 hdr
= arc_hdr_realloc(hdr
, hdr_l2only_cache
,
6129 if (GHOST_STATE(hdr
->b_l1hdr
.b_state
)) {
6130 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6131 ASSERT(!HDR_HAS_RABD(hdr
));
6132 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6133 ASSERT0(refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
6134 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
6135 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
6136 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
6138 * If this header already had an IO in progress
6139 * and we are performing another IO to fetch
6140 * encrypted data we must wait until the first
6141 * IO completes so as not to confuse
6142 * arc_read_done(). This should be very rare
6143 * and so the performance impact shouldn't
6146 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
6147 mutex_exit(hash_lock
);
6152 * This is a delicate dance that we play here.
6153 * This hdr might be in the ghost list so we access
6154 * it to move it out of the ghost list before we
6155 * initiate the read. If it's a prefetch then
6156 * it won't have a callback so we'll remove the
6157 * reference that arc_buf_alloc_impl() created. We
6158 * do this after we've called arc_access() to
6159 * avoid hitting an assert in remove_reference().
6161 arc_access(hdr
, hash_lock
);
6162 arc_hdr_alloc_abd(hdr
, encrypted_read
);
6165 if (encrypted_read
) {
6166 ASSERT(HDR_HAS_RABD(hdr
));
6167 size
= HDR_GET_PSIZE(hdr
);
6168 hdr_abd
= hdr
->b_crypt_hdr
.b_rabd
;
6169 zio_flags
|= ZIO_FLAG_RAW
;
6171 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
6172 size
= arc_hdr_size(hdr
);
6173 hdr_abd
= hdr
->b_l1hdr
.b_pabd
;
6175 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
6176 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
6180 * For authenticated bp's, we do not ask the ZIO layer
6181 * to authenticate them since this will cause the entire
6182 * IO to fail if the key isn't loaded. Instead, we
6183 * defer authentication until arc_buf_fill(), which will
6184 * verify the data when the key is available.
6186 if (BP_IS_AUTHENTICATED(bp
))
6187 zio_flags
|= ZIO_FLAG_RAW_ENCRYPT
;
6190 if (*arc_flags
& ARC_FLAG_PREFETCH
&&
6191 refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
))
6192 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
6193 if (*arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
)
6194 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
6195 if (*arc_flags
& ARC_FLAG_L2CACHE
)
6196 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6197 if (BP_IS_AUTHENTICATED(bp
))
6198 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6199 if (BP_GET_LEVEL(bp
) > 0)
6200 arc_hdr_set_flags(hdr
, ARC_FLAG_INDIRECT
);
6201 if (*arc_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
)
6202 arc_hdr_set_flags(hdr
, ARC_FLAG_PREDICTIVE_PREFETCH
);
6203 ASSERT(!GHOST_STATE(hdr
->b_l1hdr
.b_state
));
6205 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
6206 acb
->acb_done
= done
;
6207 acb
->acb_private
= private;
6208 acb
->acb_compressed
= compressed_read
;
6209 acb
->acb_encrypted
= encrypted_read
;
6210 acb
->acb_noauth
= noauth_read
;
6213 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6214 hdr
->b_l1hdr
.b_acb
= acb
;
6215 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6217 if (HDR_HAS_L2HDR(hdr
) &&
6218 (vd
= hdr
->b_l2hdr
.b_dev
->l2ad_vdev
) != NULL
) {
6219 devw
= hdr
->b_l2hdr
.b_dev
->l2ad_writing
;
6220 addr
= hdr
->b_l2hdr
.b_daddr
;
6222 * Lock out L2ARC device removal.
6224 if (vdev_is_dead(vd
) ||
6225 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
6230 * We count both async reads and scrub IOs as asynchronous so
6231 * that both can be upgraded in the event of a cache hit while
6232 * the read IO is still in-flight.
6234 if (priority
== ZIO_PRIORITY_ASYNC_READ
||
6235 priority
== ZIO_PRIORITY_SCRUB
)
6236 arc_hdr_set_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6238 arc_hdr_clear_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6241 * At this point, we have a level 1 cache miss. Try again in
6242 * L2ARC if possible.
6244 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, lsize
);
6246 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
, blkptr_t
*, bp
,
6247 uint64_t, lsize
, zbookmark_phys_t
*, zb
);
6248 ARCSTAT_BUMP(arcstat_misses
);
6249 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
6250 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
6251 data
, metadata
, misses
);
6253 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
6255 * Read from the L2ARC if the following are true:
6256 * 1. The L2ARC vdev was previously cached.
6257 * 2. This buffer still has L2ARC metadata.
6258 * 3. This buffer isn't currently writing to the L2ARC.
6259 * 4. The L2ARC entry wasn't evicted, which may
6260 * also have invalidated the vdev.
6261 * 5. This isn't prefetch and l2arc_noprefetch is set.
6263 if (HDR_HAS_L2HDR(hdr
) &&
6264 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
6265 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
6266 l2arc_read_callback_t
*cb
;
6270 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
6271 ARCSTAT_BUMP(arcstat_l2_hits
);
6272 atomic_inc_32(&hdr
->b_l2hdr
.b_hits
);
6274 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
6276 cb
->l2rcb_hdr
= hdr
;
6279 cb
->l2rcb_flags
= zio_flags
;
6281 asize
= vdev_psize_to_asize(vd
, size
);
6282 if (asize
!= size
) {
6283 abd
= abd_alloc_for_io(asize
,
6284 HDR_ISTYPE_METADATA(hdr
));
6285 cb
->l2rcb_abd
= abd
;
6290 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
6291 addr
+ asize
<= vd
->vdev_psize
-
6292 VDEV_LABEL_END_SIZE
);
6295 * l2arc read. The SCL_L2ARC lock will be
6296 * released by l2arc_read_done().
6297 * Issue a null zio if the underlying buffer
6298 * was squashed to zero size by compression.
6300 ASSERT3U(arc_hdr_get_compress(hdr
), !=,
6301 ZIO_COMPRESS_EMPTY
);
6302 rzio
= zio_read_phys(pio
, vd
, addr
,
6305 l2arc_read_done
, cb
, priority
,
6306 zio_flags
| ZIO_FLAG_DONT_CACHE
|
6308 ZIO_FLAG_DONT_PROPAGATE
|
6309 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
6310 acb
->acb_zio_head
= rzio
;
6312 if (hash_lock
!= NULL
)
6313 mutex_exit(hash_lock
);
6315 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
6317 ARCSTAT_INCR(arcstat_l2_read_bytes
,
6318 HDR_GET_PSIZE(hdr
));
6320 if (*arc_flags
& ARC_FLAG_NOWAIT
) {
6325 ASSERT(*arc_flags
& ARC_FLAG_WAIT
);
6326 if (zio_wait(rzio
) == 0)
6329 /* l2arc read error; goto zio_read() */
6330 if (hash_lock
!= NULL
)
6331 mutex_enter(hash_lock
);
6333 DTRACE_PROBE1(l2arc__miss
,
6334 arc_buf_hdr_t
*, hdr
);
6335 ARCSTAT_BUMP(arcstat_l2_misses
);
6336 if (HDR_L2_WRITING(hdr
))
6337 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
6338 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6342 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6343 if (l2arc_ndev
!= 0) {
6344 DTRACE_PROBE1(l2arc__miss
,
6345 arc_buf_hdr_t
*, hdr
);
6346 ARCSTAT_BUMP(arcstat_l2_misses
);
6350 rzio
= zio_read(pio
, spa
, bp
, hdr_abd
, size
,
6351 arc_read_done
, hdr
, priority
, zio_flags
, zb
);
6352 acb
->acb_zio_head
= rzio
;
6354 if (hash_lock
!= NULL
)
6355 mutex_exit(hash_lock
);
6357 if (*arc_flags
& ARC_FLAG_WAIT
) {
6358 rc
= zio_wait(rzio
);
6362 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
6367 /* embedded bps don't actually go to disk */
6368 if (!BP_IS_EMBEDDED(bp
))
6369 spa_read_history_add(spa
, zb
, *arc_flags
);
6374 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
6378 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
6380 p
->p_private
= private;
6381 list_link_init(&p
->p_node
);
6382 refcount_create(&p
->p_refcnt
);
6384 mutex_enter(&arc_prune_mtx
);
6385 refcount_add(&p
->p_refcnt
, &arc_prune_list
);
6386 list_insert_head(&arc_prune_list
, p
);
6387 mutex_exit(&arc_prune_mtx
);
6393 arc_remove_prune_callback(arc_prune_t
*p
)
6395 boolean_t wait
= B_FALSE
;
6396 mutex_enter(&arc_prune_mtx
);
6397 list_remove(&arc_prune_list
, p
);
6398 if (refcount_remove(&p
->p_refcnt
, &arc_prune_list
) > 0)
6400 mutex_exit(&arc_prune_mtx
);
6402 /* wait for arc_prune_task to finish */
6404 taskq_wait_outstanding(arc_prune_taskq
, 0);
6405 ASSERT0(refcount_count(&p
->p_refcnt
));
6406 refcount_destroy(&p
->p_refcnt
);
6407 kmem_free(p
, sizeof (*p
));
6411 * Notify the arc that a block was freed, and thus will never be used again.
6414 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
6417 kmutex_t
*hash_lock
;
6418 uint64_t guid
= spa_load_guid(spa
);
6420 ASSERT(!BP_IS_EMBEDDED(bp
));
6422 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
6427 * We might be trying to free a block that is still doing I/O
6428 * (i.e. prefetch) or has a reference (i.e. a dedup-ed,
6429 * dmu_sync-ed block). If this block is being prefetched, then it
6430 * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr
6431 * until the I/O completes. A block may also have a reference if it is
6432 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would
6433 * have written the new block to its final resting place on disk but
6434 * without the dedup flag set. This would have left the hdr in the MRU
6435 * state and discoverable. When the txg finally syncs it detects that
6436 * the block was overridden in open context and issues an override I/O.
6437 * Since this is a dedup block, the override I/O will determine if the
6438 * block is already in the DDT. If so, then it will replace the io_bp
6439 * with the bp from the DDT and allow the I/O to finish. When the I/O
6440 * reaches the done callback, dbuf_write_override_done, it will
6441 * check to see if the io_bp and io_bp_override are identical.
6442 * If they are not, then it indicates that the bp was replaced with
6443 * the bp in the DDT and the override bp is freed. This allows
6444 * us to arrive here with a reference on a block that is being
6445 * freed. So if we have an I/O in progress, or a reference to
6446 * this hdr, then we don't destroy the hdr.
6448 if (!HDR_HAS_L1HDR(hdr
) || (!HDR_IO_IN_PROGRESS(hdr
) &&
6449 refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
))) {
6450 arc_change_state(arc_anon
, hdr
, hash_lock
);
6451 arc_hdr_destroy(hdr
);
6452 mutex_exit(hash_lock
);
6454 mutex_exit(hash_lock
);
6460 * Release this buffer from the cache, making it an anonymous buffer. This
6461 * must be done after a read and prior to modifying the buffer contents.
6462 * If the buffer has more than one reference, we must make
6463 * a new hdr for the buffer.
6466 arc_release(arc_buf_t
*buf
, void *tag
)
6468 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6471 * It would be nice to assert that if its DMU metadata (level >
6472 * 0 || it's the dnode file), then it must be syncing context.
6473 * But we don't know that information at this level.
6476 mutex_enter(&buf
->b_evict_lock
);
6478 ASSERT(HDR_HAS_L1HDR(hdr
));
6481 * We don't grab the hash lock prior to this check, because if
6482 * the buffer's header is in the arc_anon state, it won't be
6483 * linked into the hash table.
6485 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
6486 mutex_exit(&buf
->b_evict_lock
);
6487 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6488 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
6489 ASSERT(!HDR_HAS_L2HDR(hdr
));
6490 ASSERT(HDR_EMPTY(hdr
));
6492 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
6493 ASSERT3S(refcount_count(&hdr
->b_l1hdr
.b_refcnt
), ==, 1);
6494 ASSERT(!list_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6496 hdr
->b_l1hdr
.b_arc_access
= 0;
6499 * If the buf is being overridden then it may already
6500 * have a hdr that is not empty.
6502 buf_discard_identity(hdr
);
6508 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
6509 mutex_enter(hash_lock
);
6512 * This assignment is only valid as long as the hash_lock is
6513 * held, we must be careful not to reference state or the
6514 * b_state field after dropping the lock.
6516 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
6517 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
6518 ASSERT3P(state
, !=, arc_anon
);
6520 /* this buffer is not on any list */
6521 ASSERT3S(refcount_count(&hdr
->b_l1hdr
.b_refcnt
), >, 0);
6523 if (HDR_HAS_L2HDR(hdr
)) {
6524 mutex_enter(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6527 * We have to recheck this conditional again now that
6528 * we're holding the l2ad_mtx to prevent a race with
6529 * another thread which might be concurrently calling
6530 * l2arc_evict(). In that case, l2arc_evict() might have
6531 * destroyed the header's L2 portion as we were waiting
6532 * to acquire the l2ad_mtx.
6534 if (HDR_HAS_L2HDR(hdr
))
6535 arc_hdr_l2hdr_destroy(hdr
);
6537 mutex_exit(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6541 * Do we have more than one buf?
6543 if (hdr
->b_l1hdr
.b_bufcnt
> 1) {
6544 arc_buf_hdr_t
*nhdr
;
6545 uint64_t spa
= hdr
->b_spa
;
6546 uint64_t psize
= HDR_GET_PSIZE(hdr
);
6547 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
6548 boolean_t
protected = HDR_PROTECTED(hdr
);
6549 enum zio_compress compress
= arc_hdr_get_compress(hdr
);
6550 arc_buf_contents_t type
= arc_buf_type(hdr
);
6551 VERIFY3U(hdr
->b_type
, ==, type
);
6553 ASSERT(hdr
->b_l1hdr
.b_buf
!= buf
|| buf
->b_next
!= NULL
);
6554 (void) remove_reference(hdr
, hash_lock
, tag
);
6556 if (arc_buf_is_shared(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
6557 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6558 ASSERT(ARC_BUF_LAST(buf
));
6562 * Pull the data off of this hdr and attach it to
6563 * a new anonymous hdr. Also find the last buffer
6564 * in the hdr's buffer list.
6566 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
6567 ASSERT3P(lastbuf
, !=, NULL
);
6570 * If the current arc_buf_t and the hdr are sharing their data
6571 * buffer, then we must stop sharing that block.
6573 if (arc_buf_is_shared(buf
)) {
6574 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6575 VERIFY(!arc_buf_is_shared(lastbuf
));
6578 * First, sever the block sharing relationship between
6579 * buf and the arc_buf_hdr_t.
6581 arc_unshare_buf(hdr
, buf
);
6584 * Now we need to recreate the hdr's b_pabd. Since we
6585 * have lastbuf handy, we try to share with it, but if
6586 * we can't then we allocate a new b_pabd and copy the
6587 * data from buf into it.
6589 if (arc_can_share(hdr
, lastbuf
)) {
6590 arc_share_buf(hdr
, lastbuf
);
6592 arc_hdr_alloc_abd(hdr
, B_FALSE
);
6593 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
,
6594 buf
->b_data
, psize
);
6596 VERIFY3P(lastbuf
->b_data
, !=, NULL
);
6597 } else if (HDR_SHARED_DATA(hdr
)) {
6599 * Uncompressed shared buffers are always at the end
6600 * of the list. Compressed buffers don't have the
6601 * same requirements. This makes it hard to
6602 * simply assert that the lastbuf is shared so
6603 * we rely on the hdr's compression flags to determine
6604 * if we have a compressed, shared buffer.
6606 ASSERT(arc_buf_is_shared(lastbuf
) ||
6607 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
6608 ASSERT(!ARC_BUF_SHARED(buf
));
6611 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
6612 ASSERT3P(state
, !=, arc_l2c_only
);
6614 (void) refcount_remove_many(&state
->arcs_size
,
6615 arc_buf_size(buf
), buf
);
6617 if (refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
6618 ASSERT3P(state
, !=, arc_l2c_only
);
6619 (void) refcount_remove_many(&state
->arcs_esize
[type
],
6620 arc_buf_size(buf
), buf
);
6623 hdr
->b_l1hdr
.b_bufcnt
-= 1;
6624 if (ARC_BUF_ENCRYPTED(buf
))
6625 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
6627 arc_cksum_verify(buf
);
6628 arc_buf_unwatch(buf
);
6630 /* if this is the last uncompressed buf free the checksum */
6631 if (!arc_hdr_has_uncompressed_buf(hdr
))
6632 arc_cksum_free(hdr
);
6634 mutex_exit(hash_lock
);
6637 * Allocate a new hdr. The new hdr will contain a b_pabd
6638 * buffer which will be freed in arc_write().
6640 nhdr
= arc_hdr_alloc(spa
, psize
, lsize
, protected,
6641 compress
, type
, HDR_HAS_RABD(hdr
));
6642 ASSERT3P(nhdr
->b_l1hdr
.b_buf
, ==, NULL
);
6643 ASSERT0(nhdr
->b_l1hdr
.b_bufcnt
);
6644 ASSERT0(refcount_count(&nhdr
->b_l1hdr
.b_refcnt
));
6645 VERIFY3U(nhdr
->b_type
, ==, type
);
6646 ASSERT(!HDR_SHARED_DATA(nhdr
));
6648 nhdr
->b_l1hdr
.b_buf
= buf
;
6649 nhdr
->b_l1hdr
.b_bufcnt
= 1;
6650 if (ARC_BUF_ENCRYPTED(buf
))
6651 nhdr
->b_crypt_hdr
.b_ebufcnt
= 1;
6652 nhdr
->b_l1hdr
.b_mru_hits
= 0;
6653 nhdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
6654 nhdr
->b_l1hdr
.b_mfu_hits
= 0;
6655 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
6656 nhdr
->b_l1hdr
.b_l2_hits
= 0;
6657 (void) refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, tag
);
6660 mutex_exit(&buf
->b_evict_lock
);
6661 (void) refcount_add_many(&arc_anon
->arcs_size
,
6662 HDR_GET_LSIZE(nhdr
), buf
);
6664 mutex_exit(&buf
->b_evict_lock
);
6665 ASSERT(refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 1);
6666 /* protected by hash lock, or hdr is on arc_anon */
6667 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6668 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6669 hdr
->b_l1hdr
.b_mru_hits
= 0;
6670 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
6671 hdr
->b_l1hdr
.b_mfu_hits
= 0;
6672 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
6673 hdr
->b_l1hdr
.b_l2_hits
= 0;
6674 arc_change_state(arc_anon
, hdr
, hash_lock
);
6675 hdr
->b_l1hdr
.b_arc_access
= 0;
6677 mutex_exit(hash_lock
);
6678 buf_discard_identity(hdr
);
6684 arc_released(arc_buf_t
*buf
)
6688 mutex_enter(&buf
->b_evict_lock
);
6689 released
= (buf
->b_data
!= NULL
&&
6690 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
6691 mutex_exit(&buf
->b_evict_lock
);
6697 arc_referenced(arc_buf_t
*buf
)
6701 mutex_enter(&buf
->b_evict_lock
);
6702 referenced
= (refcount_count(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6703 mutex_exit(&buf
->b_evict_lock
);
6704 return (referenced
);
6709 arc_write_ready(zio_t
*zio
)
6711 arc_write_callback_t
*callback
= zio
->io_private
;
6712 arc_buf_t
*buf
= callback
->awcb_buf
;
6713 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6714 blkptr_t
*bp
= zio
->io_bp
;
6715 uint64_t psize
= BP_IS_HOLE(bp
) ? 0 : BP_GET_PSIZE(bp
);
6716 fstrans_cookie_t cookie
= spl_fstrans_mark();
6718 ASSERT(HDR_HAS_L1HDR(hdr
));
6719 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6720 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
6723 * If we're reexecuting this zio because the pool suspended, then
6724 * cleanup any state that was previously set the first time the
6725 * callback was invoked.
6727 if (zio
->io_flags
& ZIO_FLAG_REEXECUTED
) {
6728 arc_cksum_free(hdr
);
6729 arc_buf_unwatch(buf
);
6730 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
6731 if (arc_buf_is_shared(buf
)) {
6732 arc_unshare_buf(hdr
, buf
);
6734 arc_hdr_free_abd(hdr
, B_FALSE
);
6738 if (HDR_HAS_RABD(hdr
))
6739 arc_hdr_free_abd(hdr
, B_TRUE
);
6741 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6742 ASSERT(!HDR_HAS_RABD(hdr
));
6743 ASSERT(!HDR_SHARED_DATA(hdr
));
6744 ASSERT(!arc_buf_is_shared(buf
));
6746 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
6748 if (HDR_IO_IN_PROGRESS(hdr
))
6749 ASSERT(zio
->io_flags
& ZIO_FLAG_REEXECUTED
);
6751 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6753 if (BP_IS_PROTECTED(bp
) != !!HDR_PROTECTED(hdr
))
6754 hdr
= arc_hdr_realloc_crypt(hdr
, BP_IS_PROTECTED(bp
));
6756 if (BP_IS_PROTECTED(bp
)) {
6757 /* ZIL blocks are written through zio_rewrite */
6758 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
6759 ASSERT(HDR_PROTECTED(hdr
));
6761 if (BP_SHOULD_BYTESWAP(bp
)) {
6762 if (BP_GET_LEVEL(bp
) > 0) {
6763 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
6765 hdr
->b_l1hdr
.b_byteswap
=
6766 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
6769 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
6772 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
6773 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
6774 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
6775 hdr
->b_crypt_hdr
.b_iv
);
6776 zio_crypt_decode_mac_bp(bp
, hdr
->b_crypt_hdr
.b_mac
);
6780 * If this block was written for raw encryption but the zio layer
6781 * ended up only authenticating it, adjust the buffer flags now.
6783 if (BP_IS_AUTHENTICATED(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
6784 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6785 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
6786 if (BP_GET_COMPRESS(bp
) == ZIO_COMPRESS_OFF
)
6787 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
6788 } else if (BP_IS_HOLE(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
6789 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
6790 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
6793 /* this must be done after the buffer flags are adjusted */
6794 arc_cksum_compute(buf
);
6796 enum zio_compress compress
;
6797 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
6798 compress
= ZIO_COMPRESS_OFF
;
6800 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
6801 compress
= BP_GET_COMPRESS(bp
);
6803 HDR_SET_PSIZE(hdr
, psize
);
6804 arc_hdr_set_compress(hdr
, compress
);
6806 if (zio
->io_error
!= 0 || psize
== 0)
6810 * Fill the hdr with data. If the buffer is encrypted we have no choice
6811 * but to copy the data into b_radb. If the hdr is compressed, the data
6812 * we want is available from the zio, otherwise we can take it from
6815 * We might be able to share the buf's data with the hdr here. However,
6816 * doing so would cause the ARC to be full of linear ABDs if we write a
6817 * lot of shareable data. As a compromise, we check whether scattered
6818 * ABDs are allowed, and assume that if they are then the user wants
6819 * the ARC to be primarily filled with them regardless of the data being
6820 * written. Therefore, if they're allowed then we allocate one and copy
6821 * the data into it; otherwise, we share the data directly if we can.
6823 if (ARC_BUF_ENCRYPTED(buf
)) {
6824 ASSERT3U(psize
, >, 0);
6825 ASSERT(ARC_BUF_COMPRESSED(buf
));
6826 arc_hdr_alloc_abd(hdr
, B_TRUE
);
6827 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
6828 } else if (zfs_abd_scatter_enabled
|| !arc_can_share(hdr
, buf
)) {
6830 * Ideally, we would always copy the io_abd into b_pabd, but the
6831 * user may have disabled compressed ARC, thus we must check the
6832 * hdr's compression setting rather than the io_bp's.
6834 if (BP_IS_ENCRYPTED(bp
)) {
6835 ASSERT3U(psize
, >, 0);
6836 arc_hdr_alloc_abd(hdr
, B_TRUE
);
6837 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
6838 } else if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
6839 !ARC_BUF_COMPRESSED(buf
)) {
6840 ASSERT3U(psize
, >, 0);
6841 arc_hdr_alloc_abd(hdr
, B_FALSE
);
6842 abd_copy(hdr
->b_l1hdr
.b_pabd
, zio
->io_abd
, psize
);
6844 ASSERT3U(zio
->io_orig_size
, ==, arc_hdr_size(hdr
));
6845 arc_hdr_alloc_abd(hdr
, B_FALSE
);
6846 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
6850 ASSERT3P(buf
->b_data
, ==, abd_to_buf(zio
->io_orig_abd
));
6851 ASSERT3U(zio
->io_orig_size
, ==, arc_buf_size(buf
));
6852 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
6854 arc_share_buf(hdr
, buf
);
6858 arc_hdr_verify(hdr
, bp
);
6859 spl_fstrans_unmark(cookie
);
6863 arc_write_children_ready(zio_t
*zio
)
6865 arc_write_callback_t
*callback
= zio
->io_private
;
6866 arc_buf_t
*buf
= callback
->awcb_buf
;
6868 callback
->awcb_children_ready(zio
, buf
, callback
->awcb_private
);
6872 * The SPA calls this callback for each physical write that happens on behalf
6873 * of a logical write. See the comment in dbuf_write_physdone() for details.
6876 arc_write_physdone(zio_t
*zio
)
6878 arc_write_callback_t
*cb
= zio
->io_private
;
6879 if (cb
->awcb_physdone
!= NULL
)
6880 cb
->awcb_physdone(zio
, cb
->awcb_buf
, cb
->awcb_private
);
6884 arc_write_done(zio_t
*zio
)
6886 arc_write_callback_t
*callback
= zio
->io_private
;
6887 arc_buf_t
*buf
= callback
->awcb_buf
;
6888 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6890 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6892 if (zio
->io_error
== 0) {
6893 arc_hdr_verify(hdr
, zio
->io_bp
);
6895 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
6896 buf_discard_identity(hdr
);
6898 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
6899 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
6902 ASSERT(HDR_EMPTY(hdr
));
6906 * If the block to be written was all-zero or compressed enough to be
6907 * embedded in the BP, no write was performed so there will be no
6908 * dva/birth/checksum. The buffer must therefore remain anonymous
6911 if (!HDR_EMPTY(hdr
)) {
6912 arc_buf_hdr_t
*exists
;
6913 kmutex_t
*hash_lock
;
6915 ASSERT3U(zio
->io_error
, ==, 0);
6917 arc_cksum_verify(buf
);
6919 exists
= buf_hash_insert(hdr
, &hash_lock
);
6920 if (exists
!= NULL
) {
6922 * This can only happen if we overwrite for
6923 * sync-to-convergence, because we remove
6924 * buffers from the hash table when we arc_free().
6926 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
6927 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
6928 panic("bad overwrite, hdr=%p exists=%p",
6929 (void *)hdr
, (void *)exists
);
6930 ASSERT(refcount_is_zero(
6931 &exists
->b_l1hdr
.b_refcnt
));
6932 arc_change_state(arc_anon
, exists
, hash_lock
);
6933 mutex_exit(hash_lock
);
6934 arc_hdr_destroy(exists
);
6935 exists
= buf_hash_insert(hdr
, &hash_lock
);
6936 ASSERT3P(exists
, ==, NULL
);
6937 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
6939 ASSERT(zio
->io_prop
.zp_nopwrite
);
6940 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
6941 panic("bad nopwrite, hdr=%p exists=%p",
6942 (void *)hdr
, (void *)exists
);
6945 ASSERT(hdr
->b_l1hdr
.b_bufcnt
== 1);
6946 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
6947 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
6948 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
6951 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6952 /* if it's not anon, we are doing a scrub */
6953 if (exists
== NULL
&& hdr
->b_l1hdr
.b_state
== arc_anon
)
6954 arc_access(hdr
, hash_lock
);
6955 mutex_exit(hash_lock
);
6957 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6960 ASSERT(!refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
6961 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
6963 abd_put(zio
->io_abd
);
6964 kmem_free(callback
, sizeof (arc_write_callback_t
));
6968 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
6969 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
,
6970 const zio_prop_t
*zp
, arc_write_done_func_t
*ready
,
6971 arc_write_done_func_t
*children_ready
, arc_write_done_func_t
*physdone
,
6972 arc_write_done_func_t
*done
, void *private, zio_priority_t priority
,
6973 int zio_flags
, const zbookmark_phys_t
*zb
)
6975 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6976 arc_write_callback_t
*callback
;
6978 zio_prop_t localprop
= *zp
;
6980 ASSERT3P(ready
, !=, NULL
);
6981 ASSERT3P(done
, !=, NULL
);
6982 ASSERT(!HDR_IO_ERROR(hdr
));
6983 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6984 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6985 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
6987 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6989 if (ARC_BUF_ENCRYPTED(buf
)) {
6990 ASSERT(ARC_BUF_COMPRESSED(buf
));
6991 localprop
.zp_encrypt
= B_TRUE
;
6992 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
6993 localprop
.zp_byteorder
=
6994 (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
6995 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
6996 bcopy(hdr
->b_crypt_hdr
.b_salt
, localprop
.zp_salt
,
6998 bcopy(hdr
->b_crypt_hdr
.b_iv
, localprop
.zp_iv
,
7000 bcopy(hdr
->b_crypt_hdr
.b_mac
, localprop
.zp_mac
,
7002 if (DMU_OT_IS_ENCRYPTED(localprop
.zp_type
)) {
7003 localprop
.zp_nopwrite
= B_FALSE
;
7004 localprop
.zp_copies
=
7005 MIN(localprop
.zp_copies
, SPA_DVAS_PER_BP
- 1);
7007 zio_flags
|= ZIO_FLAG_RAW
;
7008 } else if (ARC_BUF_COMPRESSED(buf
)) {
7009 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, arc_buf_size(buf
));
7010 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
7011 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
7013 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
7014 callback
->awcb_ready
= ready
;
7015 callback
->awcb_children_ready
= children_ready
;
7016 callback
->awcb_physdone
= physdone
;
7017 callback
->awcb_done
= done
;
7018 callback
->awcb_private
= private;
7019 callback
->awcb_buf
= buf
;
7022 * The hdr's b_pabd is now stale, free it now. A new data block
7023 * will be allocated when the zio pipeline calls arc_write_ready().
7025 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
7027 * If the buf is currently sharing the data block with
7028 * the hdr then we need to break that relationship here.
7029 * The hdr will remain with a NULL data pointer and the
7030 * buf will take sole ownership of the block.
7032 if (arc_buf_is_shared(buf
)) {
7033 arc_unshare_buf(hdr
, buf
);
7035 arc_hdr_free_abd(hdr
, B_FALSE
);
7037 VERIFY3P(buf
->b_data
, !=, NULL
);
7040 if (HDR_HAS_RABD(hdr
))
7041 arc_hdr_free_abd(hdr
, B_TRUE
);
7043 if (!(zio_flags
& ZIO_FLAG_RAW
))
7044 arc_hdr_set_compress(hdr
, ZIO_COMPRESS_OFF
);
7046 ASSERT(!arc_buf_is_shared(buf
));
7047 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
7049 zio
= zio_write(pio
, spa
, txg
, bp
,
7050 abd_get_from_buf(buf
->b_data
, HDR_GET_LSIZE(hdr
)),
7051 HDR_GET_LSIZE(hdr
), arc_buf_size(buf
), &localprop
, arc_write_ready
,
7052 (children_ready
!= NULL
) ? arc_write_children_ready
: NULL
,
7053 arc_write_physdone
, arc_write_done
, callback
,
7054 priority
, zio_flags
, zb
);
7060 arc_memory_throttle(uint64_t reserve
, uint64_t txg
)
7063 uint64_t available_memory
= arc_free_memory();
7064 static uint64_t page_load
= 0;
7065 static uint64_t last_txg
= 0;
7069 MIN(available_memory
, vmem_size(heap_arena
, VMEM_FREE
));
7072 if (available_memory
> arc_all_memory() * arc_lotsfree_percent
/ 100)
7075 if (txg
> last_txg
) {
7080 * If we are in pageout, we know that memory is already tight,
7081 * the arc is already going to be evicting, so we just want to
7082 * continue to let page writes occur as quickly as possible.
7084 if (current_is_kswapd()) {
7085 if (page_load
> MAX(arc_sys_free
/ 4, available_memory
) / 4) {
7086 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
7087 return (SET_ERROR(ERESTART
));
7089 /* Note: reserve is inflated, so we deflate */
7090 page_load
+= reserve
/ 8;
7092 } else if (page_load
> 0 && arc_reclaim_needed()) {
7093 /* memory is low, delay before restarting */
7094 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
7095 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
7096 return (SET_ERROR(EAGAIN
));
7104 arc_tempreserve_clear(uint64_t reserve
)
7106 atomic_add_64(&arc_tempreserve
, -reserve
);
7107 ASSERT((int64_t)arc_tempreserve
>= 0);
7111 arc_tempreserve_space(uint64_t reserve
, uint64_t txg
)
7117 reserve
> arc_c
/4 &&
7118 reserve
* 4 > (2ULL << SPA_MAXBLOCKSHIFT
))
7119 arc_c
= MIN(arc_c_max
, reserve
* 4);
7122 * Throttle when the calculated memory footprint for the TXG
7123 * exceeds the target ARC size.
7125 if (reserve
> arc_c
) {
7126 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
7127 return (SET_ERROR(ERESTART
));
7131 * Don't count loaned bufs as in flight dirty data to prevent long
7132 * network delays from blocking transactions that are ready to be
7133 * assigned to a txg.
7136 /* assert that it has not wrapped around */
7137 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
7139 anon_size
= MAX((int64_t)(refcount_count(&arc_anon
->arcs_size
) -
7140 arc_loaned_bytes
), 0);
7143 * Writes will, almost always, require additional memory allocations
7144 * in order to compress/encrypt/etc the data. We therefore need to
7145 * make sure that there is sufficient available memory for this.
7147 error
= arc_memory_throttle(reserve
, txg
);
7152 * Throttle writes when the amount of dirty data in the cache
7153 * gets too large. We try to keep the cache less than half full
7154 * of dirty blocks so that our sync times don't grow too large.
7155 * Note: if two requests come in concurrently, we might let them
7156 * both succeed, when one of them should fail. Not a huge deal.
7159 if (reserve
+ arc_tempreserve
+ anon_size
> arc_c
/ 2 &&
7160 anon_size
> arc_c
/ 4) {
7162 uint64_t meta_esize
=
7163 refcount_count(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7164 uint64_t data_esize
=
7165 refcount_count(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7166 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
7167 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
7168 arc_tempreserve
>> 10, meta_esize
>> 10,
7169 data_esize
>> 10, reserve
>> 10, arc_c
>> 10);
7171 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
7172 return (SET_ERROR(ERESTART
));
7174 atomic_add_64(&arc_tempreserve
, reserve
);
7179 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
7180 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
7182 size
->value
.ui64
= refcount_count(&state
->arcs_size
);
7183 evict_data
->value
.ui64
=
7184 refcount_count(&state
->arcs_esize
[ARC_BUFC_DATA
]);
7185 evict_metadata
->value
.ui64
=
7186 refcount_count(&state
->arcs_esize
[ARC_BUFC_METADATA
]);
7190 arc_kstat_update(kstat_t
*ksp
, int rw
)
7192 arc_stats_t
*as
= ksp
->ks_data
;
7194 if (rw
== KSTAT_WRITE
) {
7195 return (SET_ERROR(EACCES
));
7197 arc_kstat_update_state(arc_anon
,
7198 &as
->arcstat_anon_size
,
7199 &as
->arcstat_anon_evictable_data
,
7200 &as
->arcstat_anon_evictable_metadata
);
7201 arc_kstat_update_state(arc_mru
,
7202 &as
->arcstat_mru_size
,
7203 &as
->arcstat_mru_evictable_data
,
7204 &as
->arcstat_mru_evictable_metadata
);
7205 arc_kstat_update_state(arc_mru_ghost
,
7206 &as
->arcstat_mru_ghost_size
,
7207 &as
->arcstat_mru_ghost_evictable_data
,
7208 &as
->arcstat_mru_ghost_evictable_metadata
);
7209 arc_kstat_update_state(arc_mfu
,
7210 &as
->arcstat_mfu_size
,
7211 &as
->arcstat_mfu_evictable_data
,
7212 &as
->arcstat_mfu_evictable_metadata
);
7213 arc_kstat_update_state(arc_mfu_ghost
,
7214 &as
->arcstat_mfu_ghost_size
,
7215 &as
->arcstat_mfu_ghost_evictable_data
,
7216 &as
->arcstat_mfu_ghost_evictable_metadata
);
7218 as
->arcstat_memory_all_bytes
.value
.ui64
=
7220 as
->arcstat_memory_free_bytes
.value
.ui64
=
7222 as
->arcstat_memory_available_bytes
.value
.i64
=
7223 arc_available_memory();
7230 * This function *must* return indices evenly distributed between all
7231 * sublists of the multilist. This is needed due to how the ARC eviction
7232 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
7233 * distributed between all sublists and uses this assumption when
7234 * deciding which sublist to evict from and how much to evict from it.
7237 arc_state_multilist_index_func(multilist_t
*ml
, void *obj
)
7239 arc_buf_hdr_t
*hdr
= obj
;
7242 * We rely on b_dva to generate evenly distributed index
7243 * numbers using buf_hash below. So, as an added precaution,
7244 * let's make sure we never add empty buffers to the arc lists.
7246 ASSERT(!HDR_EMPTY(hdr
));
7249 * The assumption here, is the hash value for a given
7250 * arc_buf_hdr_t will remain constant throughout its lifetime
7251 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
7252 * Thus, we don't need to store the header's sublist index
7253 * on insertion, as this index can be recalculated on removal.
7255 * Also, the low order bits of the hash value are thought to be
7256 * distributed evenly. Otherwise, in the case that the multilist
7257 * has a power of two number of sublists, each sublists' usage
7258 * would not be evenly distributed.
7260 return (buf_hash(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
) %
7261 multilist_get_num_sublists(ml
));
7265 * Called during module initialization and periodically thereafter to
7266 * apply reasonable changes to the exposed performance tunings. Non-zero
7267 * zfs_* values which differ from the currently set values will be applied.
7270 arc_tuning_update(void)
7272 uint64_t allmem
= arc_all_memory();
7273 unsigned long limit
;
7275 /* Valid range: 64M - <all physical memory> */
7276 if ((zfs_arc_max
) && (zfs_arc_max
!= arc_c_max
) &&
7277 (zfs_arc_max
>= 64 << 20) && (zfs_arc_max
< allmem
) &&
7278 (zfs_arc_max
> arc_c_min
)) {
7279 arc_c_max
= zfs_arc_max
;
7281 arc_p
= (arc_c
>> 1);
7282 if (arc_meta_limit
> arc_c_max
)
7283 arc_meta_limit
= arc_c_max
;
7284 if (arc_dnode_limit
> arc_meta_limit
)
7285 arc_dnode_limit
= arc_meta_limit
;
7288 /* Valid range: 32M - <arc_c_max> */
7289 if ((zfs_arc_min
) && (zfs_arc_min
!= arc_c_min
) &&
7290 (zfs_arc_min
>= 2ULL << SPA_MAXBLOCKSHIFT
) &&
7291 (zfs_arc_min
<= arc_c_max
)) {
7292 arc_c_min
= zfs_arc_min
;
7293 arc_c
= MAX(arc_c
, arc_c_min
);
7296 /* Valid range: 16M - <arc_c_max> */
7297 if ((zfs_arc_meta_min
) && (zfs_arc_meta_min
!= arc_meta_min
) &&
7298 (zfs_arc_meta_min
>= 1ULL << SPA_MAXBLOCKSHIFT
) &&
7299 (zfs_arc_meta_min
<= arc_c_max
)) {
7300 arc_meta_min
= zfs_arc_meta_min
;
7301 if (arc_meta_limit
< arc_meta_min
)
7302 arc_meta_limit
= arc_meta_min
;
7303 if (arc_dnode_limit
< arc_meta_min
)
7304 arc_dnode_limit
= arc_meta_min
;
7307 /* Valid range: <arc_meta_min> - <arc_c_max> */
7308 limit
= zfs_arc_meta_limit
? zfs_arc_meta_limit
:
7309 MIN(zfs_arc_meta_limit_percent
, 100) * arc_c_max
/ 100;
7310 if ((limit
!= arc_meta_limit
) &&
7311 (limit
>= arc_meta_min
) &&
7312 (limit
<= arc_c_max
))
7313 arc_meta_limit
= limit
;
7315 /* Valid range: <arc_meta_min> - <arc_meta_limit> */
7316 limit
= zfs_arc_dnode_limit
? zfs_arc_dnode_limit
:
7317 MIN(zfs_arc_dnode_limit_percent
, 100) * arc_meta_limit
/ 100;
7318 if ((limit
!= arc_dnode_limit
) &&
7319 (limit
>= arc_meta_min
) &&
7320 (limit
<= arc_meta_limit
))
7321 arc_dnode_limit
= limit
;
7323 /* Valid range: 1 - N */
7324 if (zfs_arc_grow_retry
)
7325 arc_grow_retry
= zfs_arc_grow_retry
;
7327 /* Valid range: 1 - N */
7328 if (zfs_arc_shrink_shift
) {
7329 arc_shrink_shift
= zfs_arc_shrink_shift
;
7330 arc_no_grow_shift
= MIN(arc_no_grow_shift
, arc_shrink_shift
-1);
7333 /* Valid range: 1 - N */
7334 if (zfs_arc_p_min_shift
)
7335 arc_p_min_shift
= zfs_arc_p_min_shift
;
7337 /* Valid range: 1 - N ms */
7338 if (zfs_arc_min_prefetch_ms
)
7339 arc_min_prefetch_ms
= zfs_arc_min_prefetch_ms
;
7341 /* Valid range: 1 - N ms */
7342 if (zfs_arc_min_prescient_prefetch_ms
) {
7343 arc_min_prescient_prefetch_ms
=
7344 zfs_arc_min_prescient_prefetch_ms
;
7347 /* Valid range: 0 - 100 */
7348 if ((zfs_arc_lotsfree_percent
>= 0) &&
7349 (zfs_arc_lotsfree_percent
<= 100))
7350 arc_lotsfree_percent
= zfs_arc_lotsfree_percent
;
7352 /* Valid range: 0 - <all physical memory> */
7353 if ((zfs_arc_sys_free
) && (zfs_arc_sys_free
!= arc_sys_free
))
7354 arc_sys_free
= MIN(MAX(zfs_arc_sys_free
, 0), allmem
);
7359 arc_state_init(void)
7361 arc_anon
= &ARC_anon
;
7363 arc_mru_ghost
= &ARC_mru_ghost
;
7365 arc_mfu_ghost
= &ARC_mfu_ghost
;
7366 arc_l2c_only
= &ARC_l2c_only
;
7368 arc_mru
->arcs_list
[ARC_BUFC_METADATA
] =
7369 multilist_create(sizeof (arc_buf_hdr_t
),
7370 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7371 arc_state_multilist_index_func
);
7372 arc_mru
->arcs_list
[ARC_BUFC_DATA
] =
7373 multilist_create(sizeof (arc_buf_hdr_t
),
7374 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7375 arc_state_multilist_index_func
);
7376 arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
] =
7377 multilist_create(sizeof (arc_buf_hdr_t
),
7378 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7379 arc_state_multilist_index_func
);
7380 arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
] =
7381 multilist_create(sizeof (arc_buf_hdr_t
),
7382 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7383 arc_state_multilist_index_func
);
7384 arc_mfu
->arcs_list
[ARC_BUFC_METADATA
] =
7385 multilist_create(sizeof (arc_buf_hdr_t
),
7386 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7387 arc_state_multilist_index_func
);
7388 arc_mfu
->arcs_list
[ARC_BUFC_DATA
] =
7389 multilist_create(sizeof (arc_buf_hdr_t
),
7390 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7391 arc_state_multilist_index_func
);
7392 arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
] =
7393 multilist_create(sizeof (arc_buf_hdr_t
),
7394 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7395 arc_state_multilist_index_func
);
7396 arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
] =
7397 multilist_create(sizeof (arc_buf_hdr_t
),
7398 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7399 arc_state_multilist_index_func
);
7400 arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
] =
7401 multilist_create(sizeof (arc_buf_hdr_t
),
7402 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7403 arc_state_multilist_index_func
);
7404 arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
] =
7405 multilist_create(sizeof (arc_buf_hdr_t
),
7406 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7407 arc_state_multilist_index_func
);
7409 refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7410 refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7411 refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7412 refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7413 refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7414 refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7415 refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7416 refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7417 refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7418 refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7419 refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7420 refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7422 refcount_create(&arc_anon
->arcs_size
);
7423 refcount_create(&arc_mru
->arcs_size
);
7424 refcount_create(&arc_mru_ghost
->arcs_size
);
7425 refcount_create(&arc_mfu
->arcs_size
);
7426 refcount_create(&arc_mfu_ghost
->arcs_size
);
7427 refcount_create(&arc_l2c_only
->arcs_size
);
7429 arc_anon
->arcs_state
= ARC_STATE_ANON
;
7430 arc_mru
->arcs_state
= ARC_STATE_MRU
;
7431 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
7432 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
7433 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
7434 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
7438 arc_state_fini(void)
7440 refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7441 refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7442 refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7443 refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7444 refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7445 refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7446 refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7447 refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7448 refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7449 refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7450 refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7451 refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7453 refcount_destroy(&arc_anon
->arcs_size
);
7454 refcount_destroy(&arc_mru
->arcs_size
);
7455 refcount_destroy(&arc_mru_ghost
->arcs_size
);
7456 refcount_destroy(&arc_mfu
->arcs_size
);
7457 refcount_destroy(&arc_mfu_ghost
->arcs_size
);
7458 refcount_destroy(&arc_l2c_only
->arcs_size
);
7460 multilist_destroy(arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
7461 multilist_destroy(arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7462 multilist_destroy(arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
7463 multilist_destroy(arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7464 multilist_destroy(arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
7465 multilist_destroy(arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7466 multilist_destroy(arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
7467 multilist_destroy(arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7468 multilist_destroy(arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]);
7469 multilist_destroy(arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]);
7473 arc_target_bytes(void)
7481 uint64_t percent
, allmem
= arc_all_memory();
7483 mutex_init(&arc_reclaim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7484 cv_init(&arc_reclaim_thread_cv
, NULL
, CV_DEFAULT
, NULL
);
7485 cv_init(&arc_reclaim_waiters_cv
, NULL
, CV_DEFAULT
, NULL
);
7487 arc_min_prefetch_ms
= 1000;
7488 arc_min_prescient_prefetch_ms
= 6000;
7492 * Register a shrinker to support synchronous (direct) memory
7493 * reclaim from the arc. This is done to prevent kswapd from
7494 * swapping out pages when it is preferable to shrink the arc.
7496 spl_register_shrinker(&arc_shrinker
);
7498 /* Set to 1/64 of all memory or a minimum of 512K */
7499 arc_sys_free
= MAX(allmem
/ 64, (512 * 1024));
7503 /* Set max to 1/2 of all memory */
7504 arc_c_max
= allmem
/ 2;
7507 /* Set min cache to 1/32 of all memory, or 32MB, whichever is more */
7508 arc_c_min
= MAX(allmem
/ 32, 2ULL << SPA_MAXBLOCKSHIFT
);
7511 * In userland, there's only the memory pressure that we artificially
7512 * create (see arc_available_memory()). Don't let arc_c get too
7513 * small, because it can cause transactions to be larger than
7514 * arc_c, causing arc_tempreserve_space() to fail.
7516 arc_c_min
= MAX(arc_c_max
/ 2, 2ULL << SPA_MAXBLOCKSHIFT
);
7520 arc_p
= (arc_c
>> 1);
7523 /* Set min to 1/2 of arc_c_min */
7524 arc_meta_min
= 1ULL << SPA_MAXBLOCKSHIFT
;
7525 /* Initialize maximum observed usage to zero */
7528 * Set arc_meta_limit to a percent of arc_c_max with a floor of
7529 * arc_meta_min, and a ceiling of arc_c_max.
7531 percent
= MIN(zfs_arc_meta_limit_percent
, 100);
7532 arc_meta_limit
= MAX(arc_meta_min
, (percent
* arc_c_max
) / 100);
7533 percent
= MIN(zfs_arc_dnode_limit_percent
, 100);
7534 arc_dnode_limit
= (percent
* arc_meta_limit
) / 100;
7536 /* Apply user specified tunings */
7537 arc_tuning_update();
7539 /* if kmem_flags are set, lets try to use less memory */
7540 if (kmem_debugging())
7542 if (arc_c
< arc_c_min
)
7548 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
7549 offsetof(arc_prune_t
, p_node
));
7550 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
7552 arc_prune_taskq
= taskq_create("arc_prune", max_ncpus
, defclsyspri
,
7553 max_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
7555 arc_reclaim_thread_exit
= B_FALSE
;
7557 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
7558 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
7560 if (arc_ksp
!= NULL
) {
7561 arc_ksp
->ks_data
= &arc_stats
;
7562 arc_ksp
->ks_update
= arc_kstat_update
;
7563 kstat_install(arc_ksp
);
7566 (void) thread_create(NULL
, 0, arc_reclaim_thread
, NULL
, 0, &p0
,
7567 TS_RUN
, defclsyspri
);
7573 * Calculate maximum amount of dirty data per pool.
7575 * If it has been set by a module parameter, take that.
7576 * Otherwise, use a percentage of physical memory defined by
7577 * zfs_dirty_data_max_percent (default 10%) with a cap at
7578 * zfs_dirty_data_max_max (default 4G or 25% of physical memory).
7580 if (zfs_dirty_data_max_max
== 0)
7581 zfs_dirty_data_max_max
= MIN(4ULL * 1024 * 1024 * 1024,
7582 allmem
* zfs_dirty_data_max_max_percent
/ 100);
7584 if (zfs_dirty_data_max
== 0) {
7585 zfs_dirty_data_max
= allmem
*
7586 zfs_dirty_data_max_percent
/ 100;
7587 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
7588 zfs_dirty_data_max_max
);
7598 spl_unregister_shrinker(&arc_shrinker
);
7599 #endif /* _KERNEL */
7601 mutex_enter(&arc_reclaim_lock
);
7602 arc_reclaim_thread_exit
= B_TRUE
;
7604 * The reclaim thread will set arc_reclaim_thread_exit back to
7605 * B_FALSE when it is finished exiting; we're waiting for that.
7607 while (arc_reclaim_thread_exit
) {
7608 cv_signal(&arc_reclaim_thread_cv
);
7609 cv_wait(&arc_reclaim_thread_cv
, &arc_reclaim_lock
);
7611 mutex_exit(&arc_reclaim_lock
);
7613 /* Use B_TRUE to ensure *all* buffers are evicted */
7614 arc_flush(NULL
, B_TRUE
);
7618 if (arc_ksp
!= NULL
) {
7619 kstat_delete(arc_ksp
);
7623 taskq_wait(arc_prune_taskq
);
7624 taskq_destroy(arc_prune_taskq
);
7626 mutex_enter(&arc_prune_mtx
);
7627 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
7628 list_remove(&arc_prune_list
, p
);
7629 refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
7630 refcount_destroy(&p
->p_refcnt
);
7631 kmem_free(p
, sizeof (*p
));
7633 mutex_exit(&arc_prune_mtx
);
7635 list_destroy(&arc_prune_list
);
7636 mutex_destroy(&arc_prune_mtx
);
7637 mutex_destroy(&arc_reclaim_lock
);
7638 cv_destroy(&arc_reclaim_thread_cv
);
7639 cv_destroy(&arc_reclaim_waiters_cv
);
7644 ASSERT0(arc_loaned_bytes
);
7650 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
7651 * It uses dedicated storage devices to hold cached data, which are populated
7652 * using large infrequent writes. The main role of this cache is to boost
7653 * the performance of random read workloads. The intended L2ARC devices
7654 * include short-stroked disks, solid state disks, and other media with
7655 * substantially faster read latency than disk.
7657 * +-----------------------+
7659 * +-----------------------+
7662 * l2arc_feed_thread() arc_read()
7666 * +---------------+ |
7668 * +---------------+ |
7673 * +-------+ +-------+
7675 * | cache | | cache |
7676 * +-------+ +-------+
7677 * +=========+ .-----.
7678 * : L2ARC : |-_____-|
7679 * : devices : | Disks |
7680 * +=========+ `-_____-'
7682 * Read requests are satisfied from the following sources, in order:
7685 * 2) vdev cache of L2ARC devices
7687 * 4) vdev cache of disks
7690 * Some L2ARC device types exhibit extremely slow write performance.
7691 * To accommodate for this there are some significant differences between
7692 * the L2ARC and traditional cache design:
7694 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
7695 * the ARC behave as usual, freeing buffers and placing headers on ghost
7696 * lists. The ARC does not send buffers to the L2ARC during eviction as
7697 * this would add inflated write latencies for all ARC memory pressure.
7699 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
7700 * It does this by periodically scanning buffers from the eviction-end of
7701 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
7702 * not already there. It scans until a headroom of buffers is satisfied,
7703 * which itself is a buffer for ARC eviction. If a compressible buffer is
7704 * found during scanning and selected for writing to an L2ARC device, we
7705 * temporarily boost scanning headroom during the next scan cycle to make
7706 * sure we adapt to compression effects (which might significantly reduce
7707 * the data volume we write to L2ARC). The thread that does this is
7708 * l2arc_feed_thread(), illustrated below; example sizes are included to
7709 * provide a better sense of ratio than this diagram:
7712 * +---------------------+----------+
7713 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
7714 * +---------------------+----------+ | o L2ARC eligible
7715 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
7716 * +---------------------+----------+ |
7717 * 15.9 Gbytes ^ 32 Mbytes |
7719 * l2arc_feed_thread()
7721 * l2arc write hand <--[oooo]--'
7725 * +==============================+
7726 * L2ARC dev |####|#|###|###| |####| ... |
7727 * +==============================+
7730 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
7731 * evicted, then the L2ARC has cached a buffer much sooner than it probably
7732 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
7733 * safe to say that this is an uncommon case, since buffers at the end of
7734 * the ARC lists have moved there due to inactivity.
7736 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
7737 * then the L2ARC simply misses copying some buffers. This serves as a
7738 * pressure valve to prevent heavy read workloads from both stalling the ARC
7739 * with waits and clogging the L2ARC with writes. This also helps prevent
7740 * the potential for the L2ARC to churn if it attempts to cache content too
7741 * quickly, such as during backups of the entire pool.
7743 * 5. After system boot and before the ARC has filled main memory, there are
7744 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
7745 * lists can remain mostly static. Instead of searching from tail of these
7746 * lists as pictured, the l2arc_feed_thread() will search from the list heads
7747 * for eligible buffers, greatly increasing its chance of finding them.
7749 * The L2ARC device write speed is also boosted during this time so that
7750 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
7751 * there are no L2ARC reads, and no fear of degrading read performance
7752 * through increased writes.
7754 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
7755 * the vdev queue can aggregate them into larger and fewer writes. Each
7756 * device is written to in a rotor fashion, sweeping writes through
7757 * available space then repeating.
7759 * 7. The L2ARC does not store dirty content. It never needs to flush
7760 * write buffers back to disk based storage.
7762 * 8. If an ARC buffer is written (and dirtied) which also exists in the
7763 * L2ARC, the now stale L2ARC buffer is immediately dropped.
7765 * The performance of the L2ARC can be tweaked by a number of tunables, which
7766 * may be necessary for different workloads:
7768 * l2arc_write_max max write bytes per interval
7769 * l2arc_write_boost extra write bytes during device warmup
7770 * l2arc_noprefetch skip caching prefetched buffers
7771 * l2arc_headroom number of max device writes to precache
7772 * l2arc_headroom_boost when we find compressed buffers during ARC
7773 * scanning, we multiply headroom by this
7774 * percentage factor for the next scan cycle,
7775 * since more compressed buffers are likely to
7777 * l2arc_feed_secs seconds between L2ARC writing
7779 * Tunables may be removed or added as future performance improvements are
7780 * integrated, and also may become zpool properties.
7782 * There are three key functions that control how the L2ARC warms up:
7784 * l2arc_write_eligible() check if a buffer is eligible to cache
7785 * l2arc_write_size() calculate how much to write
7786 * l2arc_write_interval() calculate sleep delay between writes
7788 * These three functions determine what to write, how much, and how quickly
7793 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*hdr
)
7796 * A buffer is *not* eligible for the L2ARC if it:
7797 * 1. belongs to a different spa.
7798 * 2. is already cached on the L2ARC.
7799 * 3. has an I/O in progress (it may be an incomplete read).
7800 * 4. is flagged not eligible (zfs property).
7802 if (hdr
->b_spa
!= spa_guid
|| HDR_HAS_L2HDR(hdr
) ||
7803 HDR_IO_IN_PROGRESS(hdr
) || !HDR_L2CACHE(hdr
))
7810 l2arc_write_size(void)
7815 * Make sure our globals have meaningful values in case the user
7818 size
= l2arc_write_max
;
7820 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
7821 "be greater than zero, resetting it to the default (%d)",
7823 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
7826 if (arc_warm
== B_FALSE
)
7827 size
+= l2arc_write_boost
;
7834 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
7836 clock_t interval
, next
, now
;
7839 * If the ARC lists are busy, increase our write rate; if the
7840 * lists are stale, idle back. This is achieved by checking
7841 * how much we previously wrote - if it was more than half of
7842 * what we wanted, schedule the next write much sooner.
7844 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
7845 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
7847 interval
= hz
* l2arc_feed_secs
;
7849 now
= ddi_get_lbolt();
7850 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
7856 * Cycle through L2ARC devices. This is how L2ARC load balances.
7857 * If a device is returned, this also returns holding the spa config lock.
7859 static l2arc_dev_t
*
7860 l2arc_dev_get_next(void)
7862 l2arc_dev_t
*first
, *next
= NULL
;
7865 * Lock out the removal of spas (spa_namespace_lock), then removal
7866 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
7867 * both locks will be dropped and a spa config lock held instead.
7869 mutex_enter(&spa_namespace_lock
);
7870 mutex_enter(&l2arc_dev_mtx
);
7872 /* if there are no vdevs, there is nothing to do */
7873 if (l2arc_ndev
== 0)
7877 next
= l2arc_dev_last
;
7879 /* loop around the list looking for a non-faulted vdev */
7881 next
= list_head(l2arc_dev_list
);
7883 next
= list_next(l2arc_dev_list
, next
);
7885 next
= list_head(l2arc_dev_list
);
7888 /* if we have come back to the start, bail out */
7891 else if (next
== first
)
7894 } while (vdev_is_dead(next
->l2ad_vdev
));
7896 /* if we were unable to find any usable vdevs, return NULL */
7897 if (vdev_is_dead(next
->l2ad_vdev
))
7900 l2arc_dev_last
= next
;
7903 mutex_exit(&l2arc_dev_mtx
);
7906 * Grab the config lock to prevent the 'next' device from being
7907 * removed while we are writing to it.
7910 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
7911 mutex_exit(&spa_namespace_lock
);
7917 * Free buffers that were tagged for destruction.
7920 l2arc_do_free_on_write(void)
7923 l2arc_data_free_t
*df
, *df_prev
;
7925 mutex_enter(&l2arc_free_on_write_mtx
);
7926 buflist
= l2arc_free_on_write
;
7928 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
7929 df_prev
= list_prev(buflist
, df
);
7930 ASSERT3P(df
->l2df_abd
, !=, NULL
);
7931 abd_free(df
->l2df_abd
);
7932 list_remove(buflist
, df
);
7933 kmem_free(df
, sizeof (l2arc_data_free_t
));
7936 mutex_exit(&l2arc_free_on_write_mtx
);
7940 * A write to a cache device has completed. Update all headers to allow
7941 * reads from these buffers to begin.
7944 l2arc_write_done(zio_t
*zio
)
7946 l2arc_write_callback_t
*cb
;
7949 arc_buf_hdr_t
*head
, *hdr
, *hdr_prev
;
7950 kmutex_t
*hash_lock
;
7951 int64_t bytes_dropped
= 0;
7953 cb
= zio
->io_private
;
7954 ASSERT3P(cb
, !=, NULL
);
7955 dev
= cb
->l2wcb_dev
;
7956 ASSERT3P(dev
, !=, NULL
);
7957 head
= cb
->l2wcb_head
;
7958 ASSERT3P(head
, !=, NULL
);
7959 buflist
= &dev
->l2ad_buflist
;
7960 ASSERT3P(buflist
, !=, NULL
);
7961 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
7962 l2arc_write_callback_t
*, cb
);
7964 if (zio
->io_error
!= 0)
7965 ARCSTAT_BUMP(arcstat_l2_writes_error
);
7968 * All writes completed, or an error was hit.
7971 mutex_enter(&dev
->l2ad_mtx
);
7972 for (hdr
= list_prev(buflist
, head
); hdr
; hdr
= hdr_prev
) {
7973 hdr_prev
= list_prev(buflist
, hdr
);
7975 hash_lock
= HDR_LOCK(hdr
);
7978 * We cannot use mutex_enter or else we can deadlock
7979 * with l2arc_write_buffers (due to swapping the order
7980 * the hash lock and l2ad_mtx are taken).
7982 if (!mutex_tryenter(hash_lock
)) {
7984 * Missed the hash lock. We must retry so we
7985 * don't leave the ARC_FLAG_L2_WRITING bit set.
7987 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry
);
7990 * We don't want to rescan the headers we've
7991 * already marked as having been written out, so
7992 * we reinsert the head node so we can pick up
7993 * where we left off.
7995 list_remove(buflist
, head
);
7996 list_insert_after(buflist
, hdr
, head
);
7998 mutex_exit(&dev
->l2ad_mtx
);
8001 * We wait for the hash lock to become available
8002 * to try and prevent busy waiting, and increase
8003 * the chance we'll be able to acquire the lock
8004 * the next time around.
8006 mutex_enter(hash_lock
);
8007 mutex_exit(hash_lock
);
8012 * We could not have been moved into the arc_l2c_only
8013 * state while in-flight due to our ARC_FLAG_L2_WRITING
8014 * bit being set. Let's just ensure that's being enforced.
8016 ASSERT(HDR_HAS_L1HDR(hdr
));
8019 * Skipped - drop L2ARC entry and mark the header as no
8020 * longer L2 eligibile.
8022 if (zio
->io_error
!= 0) {
8024 * Error - drop L2ARC entry.
8026 list_remove(buflist
, hdr
);
8027 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
8029 ARCSTAT_INCR(arcstat_l2_psize
, -arc_hdr_size(hdr
));
8030 ARCSTAT_INCR(arcstat_l2_lsize
, -HDR_GET_LSIZE(hdr
));
8032 bytes_dropped
+= arc_hdr_size(hdr
);
8033 (void) refcount_remove_many(&dev
->l2ad_alloc
,
8034 arc_hdr_size(hdr
), hdr
);
8038 * Allow ARC to begin reads and ghost list evictions to
8041 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_WRITING
);
8043 mutex_exit(hash_lock
);
8046 atomic_inc_64(&l2arc_writes_done
);
8047 list_remove(buflist
, head
);
8048 ASSERT(!HDR_HAS_L1HDR(head
));
8049 kmem_cache_free(hdr_l2only_cache
, head
);
8050 mutex_exit(&dev
->l2ad_mtx
);
8052 vdev_space_update(dev
->l2ad_vdev
, -bytes_dropped
, 0, 0);
8054 l2arc_do_free_on_write();
8056 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
8060 l2arc_untransform(zio_t
*zio
, l2arc_read_callback_t
*cb
)
8063 spa_t
*spa
= zio
->io_spa
;
8064 arc_buf_hdr_t
*hdr
= cb
->l2rcb_hdr
;
8065 blkptr_t
*bp
= zio
->io_bp
;
8066 uint8_t salt
[ZIO_DATA_SALT_LEN
];
8067 uint8_t iv
[ZIO_DATA_IV_LEN
];
8068 uint8_t mac
[ZIO_DATA_MAC_LEN
];
8069 boolean_t no_crypt
= B_FALSE
;
8072 * ZIL data is never be written to the L2ARC, so we don't need
8073 * special handling for its unique MAC storage.
8075 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
8076 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
8077 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8080 * If the data was encrypted, decrypt it now. Note that
8081 * we must check the bp here and not the hdr, since the
8082 * hdr does not have its encryption parameters updated
8083 * until arc_read_done().
8085 if (BP_IS_ENCRYPTED(bp
)) {
8086 abd_t
*eabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
);
8088 zio_crypt_decode_params_bp(bp
, salt
, iv
);
8089 zio_crypt_decode_mac_bp(bp
, mac
);
8091 ret
= spa_do_crypt_abd(B_FALSE
, spa
, &cb
->l2rcb_zb
,
8092 BP_GET_TYPE(bp
), BP_GET_DEDUP(bp
), BP_SHOULD_BYTESWAP(bp
),
8093 salt
, iv
, mac
, HDR_GET_PSIZE(hdr
), eabd
,
8094 hdr
->b_l1hdr
.b_pabd
, &no_crypt
);
8096 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8101 * If we actually performed decryption, replace b_pabd
8102 * with the decrypted data. Otherwise we can just throw
8103 * our decryption buffer away.
8106 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8107 arc_hdr_size(hdr
), hdr
);
8108 hdr
->b_l1hdr
.b_pabd
= eabd
;
8111 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8116 * If the L2ARC block was compressed, but ARC compression
8117 * is disabled we decompress the data into a new buffer and
8118 * replace the existing data.
8120 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
8121 !HDR_COMPRESSION_ENABLED(hdr
)) {
8122 abd_t
*cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
);
8123 void *tmp
= abd_borrow_buf(cabd
, arc_hdr_size(hdr
));
8125 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
8126 hdr
->b_l1hdr
.b_pabd
, tmp
, HDR_GET_PSIZE(hdr
),
8127 HDR_GET_LSIZE(hdr
));
8129 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
8130 arc_free_data_abd(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
8134 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
8135 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8136 arc_hdr_size(hdr
), hdr
);
8137 hdr
->b_l1hdr
.b_pabd
= cabd
;
8139 zio
->io_size
= HDR_GET_LSIZE(hdr
);
8150 * A read to a cache device completed. Validate buffer contents before
8151 * handing over to the regular ARC routines.
8154 l2arc_read_done(zio_t
*zio
)
8157 l2arc_read_callback_t
*cb
;
8159 kmutex_t
*hash_lock
;
8160 boolean_t valid_cksum
, using_rdata
;
8162 ASSERT3P(zio
->io_vd
, !=, NULL
);
8163 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
8165 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
8167 cb
= zio
->io_private
;
8168 ASSERT3P(cb
, !=, NULL
);
8169 hdr
= cb
->l2rcb_hdr
;
8170 ASSERT3P(hdr
, !=, NULL
);
8172 hash_lock
= HDR_LOCK(hdr
);
8173 mutex_enter(hash_lock
);
8174 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
8177 * If the data was read into a temporary buffer,
8178 * move it and free the buffer.
8180 if (cb
->l2rcb_abd
!= NULL
) {
8181 ASSERT3U(arc_hdr_size(hdr
), <, zio
->io_size
);
8182 if (zio
->io_error
== 0) {
8183 abd_copy(hdr
->b_l1hdr
.b_pabd
, cb
->l2rcb_abd
,
8188 * The following must be done regardless of whether
8189 * there was an error:
8190 * - free the temporary buffer
8191 * - point zio to the real ARC buffer
8192 * - set zio size accordingly
8193 * These are required because zio is either re-used for
8194 * an I/O of the block in the case of the error
8195 * or the zio is passed to arc_read_done() and it
8198 abd_free(cb
->l2rcb_abd
);
8199 zio
->io_size
= zio
->io_orig_size
= arc_hdr_size(hdr
);
8201 if (BP_IS_ENCRYPTED(&cb
->l2rcb_bp
) &&
8202 (cb
->l2rcb_flags
& ZIO_FLAG_RAW_ENCRYPT
)) {
8203 ASSERT(HDR_HAS_RABD(hdr
));
8204 zio
->io_abd
= zio
->io_orig_abd
=
8205 hdr
->b_crypt_hdr
.b_rabd
;
8207 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8208 zio
->io_abd
= zio
->io_orig_abd
= hdr
->b_l1hdr
.b_pabd
;
8212 ASSERT3P(zio
->io_abd
, !=, NULL
);
8215 * Check this survived the L2ARC journey.
8217 ASSERT(zio
->io_abd
== hdr
->b_l1hdr
.b_pabd
||
8218 (HDR_HAS_RABD(hdr
) && zio
->io_abd
== hdr
->b_crypt_hdr
.b_rabd
));
8219 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
8220 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
8222 valid_cksum
= arc_cksum_is_equal(hdr
, zio
);
8223 using_rdata
= (HDR_HAS_RABD(hdr
) &&
8224 zio
->io_abd
== hdr
->b_crypt_hdr
.b_rabd
);
8227 * b_rabd will always match the data as it exists on disk if it is
8228 * being used. Therefore if we are reading into b_rabd we do not
8229 * attempt to untransform the data.
8231 if (valid_cksum
&& !using_rdata
)
8232 tfm_error
= l2arc_untransform(zio
, cb
);
8234 if (valid_cksum
&& tfm_error
== 0 && zio
->io_error
== 0 &&
8235 !HDR_L2_EVICTED(hdr
)) {
8236 mutex_exit(hash_lock
);
8237 zio
->io_private
= hdr
;
8240 mutex_exit(hash_lock
);
8242 * Buffer didn't survive caching. Increment stats and
8243 * reissue to the original storage device.
8245 if (zio
->io_error
!= 0) {
8246 ARCSTAT_BUMP(arcstat_l2_io_error
);
8248 zio
->io_error
= SET_ERROR(EIO
);
8250 if (!valid_cksum
|| tfm_error
!= 0)
8251 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
8254 * If there's no waiter, issue an async i/o to the primary
8255 * storage now. If there *is* a waiter, the caller must
8256 * issue the i/o in a context where it's OK to block.
8258 if (zio
->io_waiter
== NULL
) {
8259 zio_t
*pio
= zio_unique_parent(zio
);
8260 void *abd
= (using_rdata
) ?
8261 hdr
->b_crypt_hdr
.b_rabd
: hdr
->b_l1hdr
.b_pabd
;
8263 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
8265 zio_nowait(zio_read(pio
, zio
->io_spa
, zio
->io_bp
,
8266 abd
, zio
->io_size
, arc_read_done
,
8267 hdr
, zio
->io_priority
, cb
->l2rcb_flags
,
8272 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
8276 * This is the list priority from which the L2ARC will search for pages to
8277 * cache. This is used within loops (0..3) to cycle through lists in the
8278 * desired order. This order can have a significant effect on cache
8281 * Currently the metadata lists are hit first, MFU then MRU, followed by
8282 * the data lists. This function returns a locked list, and also returns
8285 static multilist_sublist_t
*
8286 l2arc_sublist_lock(int list_num
)
8288 multilist_t
*ml
= NULL
;
8291 ASSERT(list_num
>= 0 && list_num
< L2ARC_FEED_TYPES
);
8295 ml
= arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
8298 ml
= arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
8301 ml
= arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
8304 ml
= arc_mru
->arcs_list
[ARC_BUFC_DATA
];
8311 * Return a randomly-selected sublist. This is acceptable
8312 * because the caller feeds only a little bit of data for each
8313 * call (8MB). Subsequent calls will result in different
8314 * sublists being selected.
8316 idx
= multilist_get_random_index(ml
);
8317 return (multilist_sublist_lock(ml
, idx
));
8321 * Evict buffers from the device write hand to the distance specified in
8322 * bytes. This distance may span populated buffers, it may span nothing.
8323 * This is clearing a region on the L2ARC device ready for writing.
8324 * If the 'all' boolean is set, every buffer is evicted.
8327 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
8330 arc_buf_hdr_t
*hdr
, *hdr_prev
;
8331 kmutex_t
*hash_lock
;
8334 buflist
= &dev
->l2ad_buflist
;
8336 if (!all
&& dev
->l2ad_first
) {
8338 * This is the first sweep through the device. There is
8344 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
8346 * When nearing the end of the device, evict to the end
8347 * before the device write hand jumps to the start.
8349 taddr
= dev
->l2ad_end
;
8351 taddr
= dev
->l2ad_hand
+ distance
;
8353 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
8354 uint64_t, taddr
, boolean_t
, all
);
8357 mutex_enter(&dev
->l2ad_mtx
);
8358 for (hdr
= list_tail(buflist
); hdr
; hdr
= hdr_prev
) {
8359 hdr_prev
= list_prev(buflist
, hdr
);
8361 hash_lock
= HDR_LOCK(hdr
);
8364 * We cannot use mutex_enter or else we can deadlock
8365 * with l2arc_write_buffers (due to swapping the order
8366 * the hash lock and l2ad_mtx are taken).
8368 if (!mutex_tryenter(hash_lock
)) {
8370 * Missed the hash lock. Retry.
8372 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
8373 mutex_exit(&dev
->l2ad_mtx
);
8374 mutex_enter(hash_lock
);
8375 mutex_exit(hash_lock
);
8380 * A header can't be on this list if it doesn't have L2 header.
8382 ASSERT(HDR_HAS_L2HDR(hdr
));
8384 /* Ensure this header has finished being written. */
8385 ASSERT(!HDR_L2_WRITING(hdr
));
8386 ASSERT(!HDR_L2_WRITE_HEAD(hdr
));
8388 if (!all
&& (hdr
->b_l2hdr
.b_daddr
>= taddr
||
8389 hdr
->b_l2hdr
.b_daddr
< dev
->l2ad_hand
)) {
8391 * We've evicted to the target address,
8392 * or the end of the device.
8394 mutex_exit(hash_lock
);
8398 if (!HDR_HAS_L1HDR(hdr
)) {
8399 ASSERT(!HDR_L2_READING(hdr
));
8401 * This doesn't exist in the ARC. Destroy.
8402 * arc_hdr_destroy() will call list_remove()
8403 * and decrement arcstat_l2_lsize.
8405 arc_change_state(arc_anon
, hdr
, hash_lock
);
8406 arc_hdr_destroy(hdr
);
8408 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_l2c_only
);
8409 ARCSTAT_BUMP(arcstat_l2_evict_l1cached
);
8411 * Invalidate issued or about to be issued
8412 * reads, since we may be about to write
8413 * over this location.
8415 if (HDR_L2_READING(hdr
)) {
8416 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
8417 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_EVICTED
);
8420 arc_hdr_l2hdr_destroy(hdr
);
8422 mutex_exit(hash_lock
);
8424 mutex_exit(&dev
->l2ad_mtx
);
8428 * Handle any abd transforms that might be required for writing to the L2ARC.
8429 * If successful, this function will always return an abd with the data
8430 * transformed as it is on disk in a new abd of asize bytes.
8433 l2arc_apply_transforms(spa_t
*spa
, arc_buf_hdr_t
*hdr
, uint64_t asize
,
8438 abd_t
*cabd
= NULL
, *eabd
= NULL
, *to_write
= hdr
->b_l1hdr
.b_pabd
;
8439 enum zio_compress compress
= HDR_GET_COMPRESS(hdr
);
8440 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8441 uint64_t size
= arc_hdr_size(hdr
);
8442 boolean_t ismd
= HDR_ISTYPE_METADATA(hdr
);
8443 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
8444 dsl_crypto_key_t
*dck
= NULL
;
8445 uint8_t mac
[ZIO_DATA_MAC_LEN
] = { 0 };
8446 boolean_t no_crypt
= B_FALSE
;
8448 ASSERT((HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
8449 !HDR_COMPRESSION_ENABLED(hdr
)) ||
8450 HDR_ENCRYPTED(hdr
) || HDR_SHARED_DATA(hdr
) || psize
!= asize
);
8451 ASSERT3U(psize
, <=, asize
);
8454 * If this data simply needs its own buffer, we simply allocate it
8455 * and copy the data. This may be done to elimiate a depedency on a
8456 * shared buffer or to reallocate the buffer to match asize.
8458 if (HDR_HAS_RABD(hdr
) && asize
!= psize
) {
8459 ASSERT3U(asize
, >=, psize
);
8460 to_write
= abd_alloc_for_io(asize
, ismd
);
8461 abd_copy(to_write
, hdr
->b_crypt_hdr
.b_rabd
, psize
);
8463 abd_zero_off(to_write
, psize
, asize
- psize
);
8467 if ((compress
== ZIO_COMPRESS_OFF
|| HDR_COMPRESSION_ENABLED(hdr
)) &&
8468 !HDR_ENCRYPTED(hdr
)) {
8469 ASSERT3U(size
, ==, psize
);
8470 to_write
= abd_alloc_for_io(asize
, ismd
);
8471 abd_copy(to_write
, hdr
->b_l1hdr
.b_pabd
, size
);
8473 abd_zero_off(to_write
, size
, asize
- size
);
8477 if (compress
!= ZIO_COMPRESS_OFF
&& !HDR_COMPRESSION_ENABLED(hdr
)) {
8478 cabd
= abd_alloc_for_io(asize
, ismd
);
8479 tmp
= abd_borrow_buf(cabd
, asize
);
8481 psize
= zio_compress_data(compress
, to_write
, tmp
, size
);
8482 ASSERT3U(psize
, <=, HDR_GET_PSIZE(hdr
));
8484 bzero((char *)tmp
+ psize
, asize
- psize
);
8485 psize
= HDR_GET_PSIZE(hdr
);
8486 abd_return_buf_copy(cabd
, tmp
, asize
);
8490 if (HDR_ENCRYPTED(hdr
)) {
8491 eabd
= abd_alloc_for_io(asize
, ismd
);
8494 * If the dataset was disowned before the buffer
8495 * made it to this point, the key to re-encrypt
8496 * it won't be available. In this case we simply
8497 * won't write the buffer to the L2ARC.
8499 ret
= spa_keystore_lookup_key(spa
, hdr
->b_crypt_hdr
.b_dsobj
,
8504 ret
= zio_do_crypt_abd(B_TRUE
, &dck
->dck_key
,
8505 hdr
->b_crypt_hdr
.b_ot
, bswap
, hdr
->b_crypt_hdr
.b_salt
,
8506 hdr
->b_crypt_hdr
.b_iv
, mac
, psize
, to_write
, eabd
,
8512 abd_copy(eabd
, to_write
, psize
);
8515 abd_zero_off(eabd
, psize
, asize
- psize
);
8517 /* assert that the MAC we got here matches the one we saved */
8518 ASSERT0(bcmp(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
));
8519 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
8521 if (to_write
== cabd
)
8528 ASSERT3P(to_write
, !=, hdr
->b_l1hdr
.b_pabd
);
8529 *abd_out
= to_write
;
8534 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
8545 * Find and write ARC buffers to the L2ARC device.
8547 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
8548 * for reading until they have completed writing.
8549 * The headroom_boost is an in-out parameter used to maintain headroom boost
8550 * state between calls to this function.
8552 * Returns the number of bytes actually written (which may be smaller than
8553 * the delta by which the device hand has changed due to alignment).
8556 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
)
8558 arc_buf_hdr_t
*hdr
, *hdr_prev
, *head
;
8559 uint64_t write_asize
, write_psize
, write_lsize
, headroom
;
8561 l2arc_write_callback_t
*cb
;
8563 uint64_t guid
= spa_load_guid(spa
);
8565 ASSERT3P(dev
->l2ad_vdev
, !=, NULL
);
8568 write_lsize
= write_asize
= write_psize
= 0;
8570 head
= kmem_cache_alloc(hdr_l2only_cache
, KM_PUSHPAGE
);
8571 arc_hdr_set_flags(head
, ARC_FLAG_L2_WRITE_HEAD
| ARC_FLAG_HAS_L2HDR
);
8574 * Copy buffers for L2ARC writing.
8576 for (int try = 0; try < L2ARC_FEED_TYPES
; try++) {
8577 multilist_sublist_t
*mls
= l2arc_sublist_lock(try);
8578 uint64_t passed_sz
= 0;
8580 VERIFY3P(mls
, !=, NULL
);
8583 * L2ARC fast warmup.
8585 * Until the ARC is warm and starts to evict, read from the
8586 * head of the ARC lists rather than the tail.
8588 if (arc_warm
== B_FALSE
)
8589 hdr
= multilist_sublist_head(mls
);
8591 hdr
= multilist_sublist_tail(mls
);
8593 headroom
= target_sz
* l2arc_headroom
;
8594 if (zfs_compressed_arc_enabled
)
8595 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
8597 for (; hdr
; hdr
= hdr_prev
) {
8598 kmutex_t
*hash_lock
;
8599 abd_t
*to_write
= NULL
;
8601 if (arc_warm
== B_FALSE
)
8602 hdr_prev
= multilist_sublist_next(mls
, hdr
);
8604 hdr_prev
= multilist_sublist_prev(mls
, hdr
);
8606 hash_lock
= HDR_LOCK(hdr
);
8607 if (!mutex_tryenter(hash_lock
)) {
8609 * Skip this buffer rather than waiting.
8614 passed_sz
+= HDR_GET_LSIZE(hdr
);
8615 if (passed_sz
> headroom
) {
8619 mutex_exit(hash_lock
);
8623 if (!l2arc_write_eligible(guid
, hdr
)) {
8624 mutex_exit(hash_lock
);
8629 * We rely on the L1 portion of the header below, so
8630 * it's invalid for this header to have been evicted out
8631 * of the ghost cache, prior to being written out. The
8632 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
8634 ASSERT(HDR_HAS_L1HDR(hdr
));
8636 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
8637 ASSERT3U(arc_hdr_size(hdr
), >, 0);
8638 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
8640 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8641 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
,
8644 if ((write_asize
+ asize
) > target_sz
) {
8646 mutex_exit(hash_lock
);
8651 * We rely on the L1 portion of the header below, so
8652 * it's invalid for this header to have been evicted out
8653 * of the ghost cache, prior to being written out. The
8654 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
8656 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_WRITING
);
8657 ASSERT(HDR_HAS_L1HDR(hdr
));
8659 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
8660 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
8662 ASSERT3U(arc_hdr_size(hdr
), >, 0);
8665 * If this header has b_rabd, we can use this since it
8666 * must always match the data exactly as it exists on
8667 * disk. Otherwise, the L2ARC can normally use the
8668 * hdr's data, but if we're sharing data between the
8669 * hdr and one of its bufs, L2ARC needs its own copy of
8670 * the data so that the ZIO below can't race with the
8671 * buf consumer. To ensure that this copy will be
8672 * available for the lifetime of the ZIO and be cleaned
8673 * up afterwards, we add it to the l2arc_free_on_write
8674 * queue. If we need to apply any transforms to the
8675 * data (compression, encryption) we will also need the
8678 if (HDR_HAS_RABD(hdr
) && psize
== asize
) {
8679 to_write
= hdr
->b_crypt_hdr
.b_rabd
;
8680 } else if ((HDR_COMPRESSION_ENABLED(hdr
) ||
8681 HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
) &&
8682 !HDR_ENCRYPTED(hdr
) && !HDR_SHARED_DATA(hdr
) &&
8684 to_write
= hdr
->b_l1hdr
.b_pabd
;
8687 arc_buf_contents_t type
= arc_buf_type(hdr
);
8689 ret
= l2arc_apply_transforms(spa
, hdr
, asize
,
8692 arc_hdr_clear_flags(hdr
,
8693 ARC_FLAG_L2_WRITING
);
8694 mutex_exit(hash_lock
);
8698 l2arc_free_abd_on_write(to_write
, asize
, type
);
8703 * Insert a dummy header on the buflist so
8704 * l2arc_write_done() can find where the
8705 * write buffers begin without searching.
8707 mutex_enter(&dev
->l2ad_mtx
);
8708 list_insert_head(&dev
->l2ad_buflist
, head
);
8709 mutex_exit(&dev
->l2ad_mtx
);
8712 sizeof (l2arc_write_callback_t
), KM_SLEEP
);
8713 cb
->l2wcb_dev
= dev
;
8714 cb
->l2wcb_head
= head
;
8715 pio
= zio_root(spa
, l2arc_write_done
, cb
,
8719 hdr
->b_l2hdr
.b_dev
= dev
;
8720 hdr
->b_l2hdr
.b_hits
= 0;
8722 hdr
->b_l2hdr
.b_daddr
= dev
->l2ad_hand
;
8723 arc_hdr_set_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
8725 mutex_enter(&dev
->l2ad_mtx
);
8726 list_insert_head(&dev
->l2ad_buflist
, hdr
);
8727 mutex_exit(&dev
->l2ad_mtx
);
8729 (void) refcount_add_many(&dev
->l2ad_alloc
,
8730 arc_hdr_size(hdr
), hdr
);
8732 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
8733 hdr
->b_l2hdr
.b_daddr
, asize
, to_write
,
8734 ZIO_CHECKSUM_OFF
, NULL
, hdr
,
8735 ZIO_PRIORITY_ASYNC_WRITE
,
8736 ZIO_FLAG_CANFAIL
, B_FALSE
);
8738 write_lsize
+= HDR_GET_LSIZE(hdr
);
8739 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
8742 write_psize
+= psize
;
8743 write_asize
+= asize
;
8744 dev
->l2ad_hand
+= asize
;
8746 mutex_exit(hash_lock
);
8748 (void) zio_nowait(wzio
);
8751 multilist_sublist_unlock(mls
);
8757 /* No buffers selected for writing? */
8759 ASSERT0(write_lsize
);
8760 ASSERT(!HDR_HAS_L1HDR(head
));
8761 kmem_cache_free(hdr_l2only_cache
, head
);
8765 ASSERT3U(write_asize
, <=, target_sz
);
8766 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
8767 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_psize
);
8768 ARCSTAT_INCR(arcstat_l2_lsize
, write_lsize
);
8769 ARCSTAT_INCR(arcstat_l2_psize
, write_psize
);
8770 vdev_space_update(dev
->l2ad_vdev
, write_psize
, 0, 0);
8773 * Bump device hand to the device start if it is approaching the end.
8774 * l2arc_evict() will already have evicted ahead for this case.
8776 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
8777 dev
->l2ad_hand
= dev
->l2ad_start
;
8778 dev
->l2ad_first
= B_FALSE
;
8781 dev
->l2ad_writing
= B_TRUE
;
8782 (void) zio_wait(pio
);
8783 dev
->l2ad_writing
= B_FALSE
;
8785 return (write_asize
);
8789 * This thread feeds the L2ARC at regular intervals. This is the beating
8790 * heart of the L2ARC.
8794 l2arc_feed_thread(void *unused
)
8799 uint64_t size
, wrote
;
8800 clock_t begin
, next
= ddi_get_lbolt();
8801 fstrans_cookie_t cookie
;
8803 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
8805 mutex_enter(&l2arc_feed_thr_lock
);
8807 cookie
= spl_fstrans_mark();
8808 while (l2arc_thread_exit
== 0) {
8809 CALLB_CPR_SAFE_BEGIN(&cpr
);
8810 (void) cv_timedwait_sig(&l2arc_feed_thr_cv
,
8811 &l2arc_feed_thr_lock
, next
);
8812 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
8813 next
= ddi_get_lbolt() + hz
;
8816 * Quick check for L2ARC devices.
8818 mutex_enter(&l2arc_dev_mtx
);
8819 if (l2arc_ndev
== 0) {
8820 mutex_exit(&l2arc_dev_mtx
);
8823 mutex_exit(&l2arc_dev_mtx
);
8824 begin
= ddi_get_lbolt();
8827 * This selects the next l2arc device to write to, and in
8828 * doing so the next spa to feed from: dev->l2ad_spa. This
8829 * will return NULL if there are now no l2arc devices or if
8830 * they are all faulted.
8832 * If a device is returned, its spa's config lock is also
8833 * held to prevent device removal. l2arc_dev_get_next()
8834 * will grab and release l2arc_dev_mtx.
8836 if ((dev
= l2arc_dev_get_next()) == NULL
)
8839 spa
= dev
->l2ad_spa
;
8840 ASSERT3P(spa
, !=, NULL
);
8843 * If the pool is read-only then force the feed thread to
8844 * sleep a little longer.
8846 if (!spa_writeable(spa
)) {
8847 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
8848 spa_config_exit(spa
, SCL_L2ARC
, dev
);
8853 * Avoid contributing to memory pressure.
8855 if (arc_reclaim_needed()) {
8856 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
8857 spa_config_exit(spa
, SCL_L2ARC
, dev
);
8861 ARCSTAT_BUMP(arcstat_l2_feeds
);
8863 size
= l2arc_write_size();
8866 * Evict L2ARC buffers that will be overwritten.
8868 l2arc_evict(dev
, size
, B_FALSE
);
8871 * Write ARC buffers.
8873 wrote
= l2arc_write_buffers(spa
, dev
, size
);
8876 * Calculate interval between writes.
8878 next
= l2arc_write_interval(begin
, size
, wrote
);
8879 spa_config_exit(spa
, SCL_L2ARC
, dev
);
8881 spl_fstrans_unmark(cookie
);
8883 l2arc_thread_exit
= 0;
8884 cv_broadcast(&l2arc_feed_thr_cv
);
8885 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
8890 l2arc_vdev_present(vdev_t
*vd
)
8894 mutex_enter(&l2arc_dev_mtx
);
8895 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
8896 dev
= list_next(l2arc_dev_list
, dev
)) {
8897 if (dev
->l2ad_vdev
== vd
)
8900 mutex_exit(&l2arc_dev_mtx
);
8902 return (dev
!= NULL
);
8906 * Add a vdev for use by the L2ARC. By this point the spa has already
8907 * validated the vdev and opened it.
8910 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
8912 l2arc_dev_t
*adddev
;
8914 ASSERT(!l2arc_vdev_present(vd
));
8917 * Create a new l2arc device entry.
8919 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
8920 adddev
->l2ad_spa
= spa
;
8921 adddev
->l2ad_vdev
= vd
;
8922 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
8923 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
8924 adddev
->l2ad_hand
= adddev
->l2ad_start
;
8925 adddev
->l2ad_first
= B_TRUE
;
8926 adddev
->l2ad_writing
= B_FALSE
;
8927 list_link_init(&adddev
->l2ad_node
);
8929 mutex_init(&adddev
->l2ad_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
8931 * This is a list of all ARC buffers that are still valid on the
8934 list_create(&adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
8935 offsetof(arc_buf_hdr_t
, b_l2hdr
.b_l2node
));
8937 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
8938 refcount_create(&adddev
->l2ad_alloc
);
8941 * Add device to global list
8943 mutex_enter(&l2arc_dev_mtx
);
8944 list_insert_head(l2arc_dev_list
, adddev
);
8945 atomic_inc_64(&l2arc_ndev
);
8946 mutex_exit(&l2arc_dev_mtx
);
8950 * Remove a vdev from the L2ARC.
8953 l2arc_remove_vdev(vdev_t
*vd
)
8955 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
8958 * Find the device by vdev
8960 mutex_enter(&l2arc_dev_mtx
);
8961 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
8962 nextdev
= list_next(l2arc_dev_list
, dev
);
8963 if (vd
== dev
->l2ad_vdev
) {
8968 ASSERT3P(remdev
, !=, NULL
);
8971 * Remove device from global list
8973 list_remove(l2arc_dev_list
, remdev
);
8974 l2arc_dev_last
= NULL
; /* may have been invalidated */
8975 atomic_dec_64(&l2arc_ndev
);
8976 mutex_exit(&l2arc_dev_mtx
);
8979 * Clear all buflists and ARC references. L2ARC device flush.
8981 l2arc_evict(remdev
, 0, B_TRUE
);
8982 list_destroy(&remdev
->l2ad_buflist
);
8983 mutex_destroy(&remdev
->l2ad_mtx
);
8984 refcount_destroy(&remdev
->l2ad_alloc
);
8985 kmem_free(remdev
, sizeof (l2arc_dev_t
));
8991 l2arc_thread_exit
= 0;
8993 l2arc_writes_sent
= 0;
8994 l2arc_writes_done
= 0;
8996 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
8997 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
8998 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
8999 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9001 l2arc_dev_list
= &L2ARC_dev_list
;
9002 l2arc_free_on_write
= &L2ARC_free_on_write
;
9003 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
9004 offsetof(l2arc_dev_t
, l2ad_node
));
9005 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
9006 offsetof(l2arc_data_free_t
, l2df_list_node
));
9013 * This is called from dmu_fini(), which is called from spa_fini();
9014 * Because of this, we can assume that all l2arc devices have
9015 * already been removed when the pools themselves were removed.
9018 l2arc_do_free_on_write();
9020 mutex_destroy(&l2arc_feed_thr_lock
);
9021 cv_destroy(&l2arc_feed_thr_cv
);
9022 mutex_destroy(&l2arc_dev_mtx
);
9023 mutex_destroy(&l2arc_free_on_write_mtx
);
9025 list_destroy(l2arc_dev_list
);
9026 list_destroy(l2arc_free_on_write
);
9032 if (!(spa_mode_global
& FWRITE
))
9035 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
9036 TS_RUN
, defclsyspri
);
9042 if (!(spa_mode_global
& FWRITE
))
9045 mutex_enter(&l2arc_feed_thr_lock
);
9046 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
9047 l2arc_thread_exit
= 1;
9048 while (l2arc_thread_exit
!= 0)
9049 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
9050 mutex_exit(&l2arc_feed_thr_lock
);
9053 #if defined(_KERNEL) && defined(HAVE_SPL)
9054 EXPORT_SYMBOL(arc_buf_size
);
9055 EXPORT_SYMBOL(arc_write
);
9056 EXPORT_SYMBOL(arc_read
);
9057 EXPORT_SYMBOL(arc_buf_info
);
9058 EXPORT_SYMBOL(arc_getbuf_func
);
9059 EXPORT_SYMBOL(arc_add_prune_callback
);
9060 EXPORT_SYMBOL(arc_remove_prune_callback
);
9063 module_param(zfs_arc_min
, ulong
, 0644);
9064 MODULE_PARM_DESC(zfs_arc_min
, "Min arc size");
9066 module_param(zfs_arc_max
, ulong
, 0644);
9067 MODULE_PARM_DESC(zfs_arc_max
, "Max arc size");
9069 module_param(zfs_arc_meta_limit
, ulong
, 0644);
9070 MODULE_PARM_DESC(zfs_arc_meta_limit
, "Meta limit for arc size");
9072 module_param(zfs_arc_meta_limit_percent
, ulong
, 0644);
9073 MODULE_PARM_DESC(zfs_arc_meta_limit_percent
,
9074 "Percent of arc size for arc meta limit");
9076 module_param(zfs_arc_meta_min
, ulong
, 0644);
9077 MODULE_PARM_DESC(zfs_arc_meta_min
, "Min arc metadata");
9079 module_param(zfs_arc_meta_prune
, int, 0644);
9080 MODULE_PARM_DESC(zfs_arc_meta_prune
, "Meta objects to scan for prune");
9082 module_param(zfs_arc_meta_adjust_restarts
, int, 0644);
9083 MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts
,
9084 "Limit number of restarts in arc_adjust_meta");
9086 module_param(zfs_arc_meta_strategy
, int, 0644);
9087 MODULE_PARM_DESC(zfs_arc_meta_strategy
, "Meta reclaim strategy");
9089 module_param(zfs_arc_grow_retry
, int, 0644);
9090 MODULE_PARM_DESC(zfs_arc_grow_retry
, "Seconds before growing arc size");
9092 module_param(zfs_arc_p_dampener_disable
, int, 0644);
9093 MODULE_PARM_DESC(zfs_arc_p_dampener_disable
, "disable arc_p adapt dampener");
9095 module_param(zfs_arc_shrink_shift
, int, 0644);
9096 MODULE_PARM_DESC(zfs_arc_shrink_shift
, "log2(fraction of arc to reclaim)");
9098 module_param(zfs_arc_pc_percent
, uint
, 0644);
9099 MODULE_PARM_DESC(zfs_arc_pc_percent
,
9100 "Percent of pagecache to reclaim arc to");
9102 module_param(zfs_arc_p_min_shift
, int, 0644);
9103 MODULE_PARM_DESC(zfs_arc_p_min_shift
, "arc_c shift to calc min/max arc_p");
9105 module_param(zfs_arc_average_blocksize
, int, 0444);
9106 MODULE_PARM_DESC(zfs_arc_average_blocksize
, "Target average block size");
9108 module_param(zfs_compressed_arc_enabled
, int, 0644);
9109 MODULE_PARM_DESC(zfs_compressed_arc_enabled
, "Disable compressed arc buffers");
9111 module_param(zfs_arc_min_prefetch_ms
, int, 0644);
9112 MODULE_PARM_DESC(zfs_arc_min_prefetch_ms
, "Min life of prefetch block in ms");
9114 module_param(zfs_arc_min_prescient_prefetch_ms
, int, 0644);
9115 MODULE_PARM_DESC(zfs_arc_min_prescient_prefetch_ms
,
9116 "Min life of prescient prefetched block in ms");
9118 module_param(l2arc_write_max
, ulong
, 0644);
9119 MODULE_PARM_DESC(l2arc_write_max
, "Max write bytes per interval");
9121 module_param(l2arc_write_boost
, ulong
, 0644);
9122 MODULE_PARM_DESC(l2arc_write_boost
, "Extra write bytes during device warmup");
9124 module_param(l2arc_headroom
, ulong
, 0644);
9125 MODULE_PARM_DESC(l2arc_headroom
, "Number of max device writes to precache");
9127 module_param(l2arc_headroom_boost
, ulong
, 0644);
9128 MODULE_PARM_DESC(l2arc_headroom_boost
, "Compressed l2arc_headroom multiplier");
9130 module_param(l2arc_feed_secs
, ulong
, 0644);
9131 MODULE_PARM_DESC(l2arc_feed_secs
, "Seconds between L2ARC writing");
9133 module_param(l2arc_feed_min_ms
, ulong
, 0644);
9134 MODULE_PARM_DESC(l2arc_feed_min_ms
, "Min feed interval in milliseconds");
9136 module_param(l2arc_noprefetch
, int, 0644);
9137 MODULE_PARM_DESC(l2arc_noprefetch
, "Skip caching prefetched buffers");
9139 module_param(l2arc_feed_again
, int, 0644);
9140 MODULE_PARM_DESC(l2arc_feed_again
, "Turbo L2ARC warmup");
9142 module_param(l2arc_norw
, int, 0644);
9143 MODULE_PARM_DESC(l2arc_norw
, "No reads during writes");
9145 module_param(zfs_arc_lotsfree_percent
, int, 0644);
9146 MODULE_PARM_DESC(zfs_arc_lotsfree_percent
,
9147 "System free memory I/O throttle in bytes");
9149 module_param(zfs_arc_sys_free
, ulong
, 0644);
9150 MODULE_PARM_DESC(zfs_arc_sys_free
, "System free memory target size in bytes");
9152 module_param(zfs_arc_dnode_limit
, ulong
, 0644);
9153 MODULE_PARM_DESC(zfs_arc_dnode_limit
, "Minimum bytes of dnodes in arc");
9155 module_param(zfs_arc_dnode_limit_percent
, ulong
, 0644);
9156 MODULE_PARM_DESC(zfs_arc_dnode_limit_percent
,
9157 "Percent of ARC meta buffers for dnodes");
9159 module_param(zfs_arc_dnode_reduce_percent
, ulong
, 0644);
9160 MODULE_PARM_DESC(zfs_arc_dnode_reduce_percent
,
9161 "Percentage of excess dnodes to try to unpin");