4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2018, Joyent, Inc.
24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2014 by Saso Kiselkov. All rights reserved.
26 * Copyright 2017 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/shrinker.h>
296 #include <sys/vmsystm.h>
298 #include <linux/page_compat.h>
300 #include <sys/callb.h>
301 #include <sys/kstat.h>
302 #include <sys/zthr.h>
303 #include <zfs_fletcher.h>
304 #include <sys/arc_impl.h>
305 #include <sys/trace_arc.h>
306 #include <sys/aggsum.h>
307 #include <sys/cityhash.h>
310 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
311 boolean_t arc_watch
= B_FALSE
;
315 * This thread's job is to keep enough free memory in the system, by
316 * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves
317 * arc_available_memory().
319 static zthr_t
*arc_reap_zthr
;
322 * This thread's job is to keep arc_size under arc_c, by calling
323 * arc_adjust(), which improves arc_is_overflowing().
325 static zthr_t
*arc_adjust_zthr
;
327 static kmutex_t arc_adjust_lock
;
328 static kcondvar_t arc_adjust_waiters_cv
;
329 static boolean_t arc_adjust_needed
= B_FALSE
;
332 * The number of headers to evict in arc_evict_state_impl() before
333 * dropping the sublist lock and evicting from another sublist. A lower
334 * value means we're more likely to evict the "correct" header (i.e. the
335 * oldest header in the arc state), but comes with higher overhead
336 * (i.e. more invocations of arc_evict_state_impl()).
338 int zfs_arc_evict_batch_limit
= 10;
340 /* number of seconds before growing cache again */
341 static int arc_grow_retry
= 5;
344 * Minimum time between calls to arc_kmem_reap_soon().
346 int arc_kmem_cache_reap_retry_ms
= 1000;
348 /* shift of arc_c for calculating overflow limit in arc_get_data_impl */
349 int zfs_arc_overflow_shift
= 8;
351 /* shift of arc_c for calculating both min and max arc_p */
352 int arc_p_min_shift
= 4;
354 /* log2(fraction of arc to reclaim) */
355 static int arc_shrink_shift
= 7;
357 /* percent of pagecache to reclaim arc to */
359 static uint_t zfs_arc_pc_percent
= 0;
363 * log2(fraction of ARC which must be free to allow growing).
364 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
365 * when reading a new block into the ARC, we will evict an equal-sized block
368 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
369 * we will still not allow it to grow.
371 int arc_no_grow_shift
= 5;
375 * minimum lifespan of a prefetch block in clock ticks
376 * (initialized in arc_init())
378 static int arc_min_prefetch_ms
;
379 static int arc_min_prescient_prefetch_ms
;
382 * If this percent of memory is free, don't throttle.
384 int arc_lotsfree_percent
= 10;
387 * hdr_recl() uses this to determine if the arc is up and running.
389 static boolean_t arc_initialized
;
392 * The arc has filled available memory and has now warmed up.
394 static boolean_t arc_warm
;
397 * log2 fraction of the zio arena to keep free.
399 int arc_zio_arena_free_shift
= 2;
402 * These tunables are for performance analysis.
404 unsigned long zfs_arc_max
= 0;
405 unsigned long zfs_arc_min
= 0;
406 unsigned long zfs_arc_meta_limit
= 0;
407 unsigned long zfs_arc_meta_min
= 0;
408 unsigned long zfs_arc_dnode_limit
= 0;
409 unsigned long zfs_arc_dnode_reduce_percent
= 10;
410 int zfs_arc_grow_retry
= 0;
411 int zfs_arc_shrink_shift
= 0;
412 int zfs_arc_p_min_shift
= 0;
413 int zfs_arc_average_blocksize
= 8 * 1024; /* 8KB */
416 * ARC dirty data constraints for arc_tempreserve_space() throttle.
418 unsigned long zfs_arc_dirty_limit_percent
= 50; /* total dirty data limit */
419 unsigned long zfs_arc_anon_limit_percent
= 25; /* anon block dirty limit */
420 unsigned long zfs_arc_pool_dirty_percent
= 20; /* each pool's anon allowance */
423 * Enable or disable compressed arc buffers.
425 int zfs_compressed_arc_enabled
= B_TRUE
;
428 * ARC will evict meta buffers that exceed arc_meta_limit. This
429 * tunable make arc_meta_limit adjustable for different workloads.
431 unsigned long zfs_arc_meta_limit_percent
= 75;
434 * Percentage that can be consumed by dnodes of ARC meta buffers.
436 unsigned long zfs_arc_dnode_limit_percent
= 10;
439 * These tunables are Linux specific
441 unsigned long zfs_arc_sys_free
= 0;
442 int zfs_arc_min_prefetch_ms
= 0;
443 int zfs_arc_min_prescient_prefetch_ms
= 0;
444 int zfs_arc_p_dampener_disable
= 1;
445 int zfs_arc_meta_prune
= 10000;
446 int zfs_arc_meta_strategy
= ARC_STRATEGY_META_BALANCED
;
447 int zfs_arc_meta_adjust_restarts
= 4096;
448 int zfs_arc_lotsfree_percent
= 10;
451 static arc_state_t ARC_anon
;
452 static arc_state_t ARC_mru
;
453 static arc_state_t ARC_mru_ghost
;
454 static arc_state_t ARC_mfu
;
455 static arc_state_t ARC_mfu_ghost
;
456 static arc_state_t ARC_l2c_only
;
458 typedef struct arc_stats
{
459 kstat_named_t arcstat_hits
;
460 kstat_named_t arcstat_misses
;
461 kstat_named_t arcstat_demand_data_hits
;
462 kstat_named_t arcstat_demand_data_misses
;
463 kstat_named_t arcstat_demand_metadata_hits
;
464 kstat_named_t arcstat_demand_metadata_misses
;
465 kstat_named_t arcstat_prefetch_data_hits
;
466 kstat_named_t arcstat_prefetch_data_misses
;
467 kstat_named_t arcstat_prefetch_metadata_hits
;
468 kstat_named_t arcstat_prefetch_metadata_misses
;
469 kstat_named_t arcstat_mru_hits
;
470 kstat_named_t arcstat_mru_ghost_hits
;
471 kstat_named_t arcstat_mfu_hits
;
472 kstat_named_t arcstat_mfu_ghost_hits
;
473 kstat_named_t arcstat_deleted
;
475 * Number of buffers that could not be evicted because the hash lock
476 * was held by another thread. The lock may not necessarily be held
477 * by something using the same buffer, since hash locks are shared
478 * by multiple buffers.
480 kstat_named_t arcstat_mutex_miss
;
482 * Number of buffers skipped when updating the access state due to the
483 * header having already been released after acquiring the hash lock.
485 kstat_named_t arcstat_access_skip
;
487 * Number of buffers skipped because they have I/O in progress, are
488 * indirect prefetch buffers that have not lived long enough, or are
489 * not from the spa we're trying to evict from.
491 kstat_named_t arcstat_evict_skip
;
493 * Number of times arc_evict_state() was unable to evict enough
494 * buffers to reach its target amount.
496 kstat_named_t arcstat_evict_not_enough
;
497 kstat_named_t arcstat_evict_l2_cached
;
498 kstat_named_t arcstat_evict_l2_eligible
;
499 kstat_named_t arcstat_evict_l2_ineligible
;
500 kstat_named_t arcstat_evict_l2_skip
;
501 kstat_named_t arcstat_hash_elements
;
502 kstat_named_t arcstat_hash_elements_max
;
503 kstat_named_t arcstat_hash_collisions
;
504 kstat_named_t arcstat_hash_chains
;
505 kstat_named_t arcstat_hash_chain_max
;
506 kstat_named_t arcstat_p
;
507 kstat_named_t arcstat_c
;
508 kstat_named_t arcstat_c_min
;
509 kstat_named_t arcstat_c_max
;
510 /* Not updated directly; only synced in arc_kstat_update. */
511 kstat_named_t arcstat_size
;
513 * Number of compressed bytes stored in the arc_buf_hdr_t's b_pabd.
514 * Note that the compressed bytes may match the uncompressed bytes
515 * if the block is either not compressed or compressed arc is disabled.
517 kstat_named_t arcstat_compressed_size
;
519 * Uncompressed size of the data stored in b_pabd. If compressed
520 * arc is disabled then this value will be identical to the stat
523 kstat_named_t arcstat_uncompressed_size
;
525 * Number of bytes stored in all the arc_buf_t's. This is classified
526 * as "overhead" since this data is typically short-lived and will
527 * be evicted from the arc when it becomes unreferenced unless the
528 * zfs_keep_uncompressed_metadata or zfs_keep_uncompressed_level
529 * values have been set (see comment in dbuf.c for more information).
531 kstat_named_t arcstat_overhead_size
;
533 * Number of bytes consumed by internal ARC structures necessary
534 * for tracking purposes; these structures are not actually
535 * backed by ARC buffers. This includes arc_buf_hdr_t structures
536 * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only
537 * caches), and arc_buf_t structures (allocated via arc_buf_t
539 * Not updated directly; only synced in arc_kstat_update.
541 kstat_named_t arcstat_hdr_size
;
543 * Number of bytes consumed by ARC buffers of type equal to
544 * ARC_BUFC_DATA. This is generally consumed by buffers backing
545 * on disk user data (e.g. plain file contents).
546 * Not updated directly; only synced in arc_kstat_update.
548 kstat_named_t arcstat_data_size
;
550 * Number of bytes consumed by ARC buffers of type equal to
551 * ARC_BUFC_METADATA. This is generally consumed by buffers
552 * backing on disk data that is used for internal ZFS
553 * structures (e.g. ZAP, dnode, indirect blocks, etc).
554 * Not updated directly; only synced in arc_kstat_update.
556 kstat_named_t arcstat_metadata_size
;
558 * Number of bytes consumed by dmu_buf_impl_t objects.
559 * Not updated directly; only synced in arc_kstat_update.
561 kstat_named_t arcstat_dbuf_size
;
563 * Number of bytes consumed by dnode_t objects.
564 * Not updated directly; only synced in arc_kstat_update.
566 kstat_named_t arcstat_dnode_size
;
568 * Number of bytes consumed by bonus buffers.
569 * Not updated directly; only synced in arc_kstat_update.
571 kstat_named_t arcstat_bonus_size
;
573 * Total number of bytes consumed by ARC buffers residing in the
574 * arc_anon state. This includes *all* buffers in the arc_anon
575 * state; e.g. data, metadata, evictable, and unevictable buffers
576 * are all included in this value.
577 * Not updated directly; only synced in arc_kstat_update.
579 kstat_named_t arcstat_anon_size
;
581 * Number of bytes consumed by ARC buffers that meet the
582 * following criteria: backing buffers of type ARC_BUFC_DATA,
583 * residing in the arc_anon state, and are eligible for eviction
584 * (e.g. have no outstanding holds on the buffer).
585 * Not updated directly; only synced in arc_kstat_update.
587 kstat_named_t arcstat_anon_evictable_data
;
589 * Number of bytes consumed by ARC buffers that meet the
590 * following criteria: backing buffers of type ARC_BUFC_METADATA,
591 * residing in the arc_anon state, and are eligible for eviction
592 * (e.g. have no outstanding holds on the buffer).
593 * Not updated directly; only synced in arc_kstat_update.
595 kstat_named_t arcstat_anon_evictable_metadata
;
597 * Total number of bytes consumed by ARC buffers residing in the
598 * arc_mru state. This includes *all* buffers in the arc_mru
599 * state; e.g. data, metadata, evictable, and unevictable buffers
600 * are all included in this value.
601 * Not updated directly; only synced in arc_kstat_update.
603 kstat_named_t arcstat_mru_size
;
605 * Number of bytes consumed by ARC buffers that meet the
606 * following criteria: backing buffers of type ARC_BUFC_DATA,
607 * residing in the arc_mru state, and are eligible for eviction
608 * (e.g. have no outstanding holds on the buffer).
609 * Not updated directly; only synced in arc_kstat_update.
611 kstat_named_t arcstat_mru_evictable_data
;
613 * Number of bytes consumed by ARC buffers that meet the
614 * following criteria: backing buffers of type ARC_BUFC_METADATA,
615 * residing in the arc_mru state, and are eligible for eviction
616 * (e.g. have no outstanding holds on the buffer).
617 * Not updated directly; only synced in arc_kstat_update.
619 kstat_named_t arcstat_mru_evictable_metadata
;
621 * Total number of bytes that *would have been* consumed by ARC
622 * buffers in the arc_mru_ghost state. The key thing to note
623 * here, is the fact that this size doesn't actually indicate
624 * RAM consumption. The ghost lists only consist of headers and
625 * don't actually have ARC buffers linked off of these headers.
626 * Thus, *if* the headers had associated ARC buffers, these
627 * buffers *would have* consumed this number of bytes.
628 * Not updated directly; only synced in arc_kstat_update.
630 kstat_named_t arcstat_mru_ghost_size
;
632 * Number of bytes that *would have been* consumed by ARC
633 * buffers that are eligible for eviction, of type
634 * ARC_BUFC_DATA, and linked off the arc_mru_ghost state.
635 * Not updated directly; only synced in arc_kstat_update.
637 kstat_named_t arcstat_mru_ghost_evictable_data
;
639 * Number of bytes that *would have been* consumed by ARC
640 * buffers that are eligible for eviction, of type
641 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
642 * Not updated directly; only synced in arc_kstat_update.
644 kstat_named_t arcstat_mru_ghost_evictable_metadata
;
646 * Total number of bytes consumed by ARC buffers residing in the
647 * arc_mfu state. This includes *all* buffers in the arc_mfu
648 * state; e.g. data, metadata, evictable, and unevictable buffers
649 * are all included in this value.
650 * Not updated directly; only synced in arc_kstat_update.
652 kstat_named_t arcstat_mfu_size
;
654 * Number of bytes consumed by ARC buffers that are eligible for
655 * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu
657 * Not updated directly; only synced in arc_kstat_update.
659 kstat_named_t arcstat_mfu_evictable_data
;
661 * Number of bytes consumed by ARC buffers that are eligible for
662 * eviction, of type ARC_BUFC_METADATA, and reside in the
664 * Not updated directly; only synced in arc_kstat_update.
666 kstat_named_t arcstat_mfu_evictable_metadata
;
668 * Total number of bytes that *would have been* consumed by ARC
669 * buffers in the arc_mfu_ghost state. See the comment above
670 * arcstat_mru_ghost_size for more details.
671 * Not updated directly; only synced in arc_kstat_update.
673 kstat_named_t arcstat_mfu_ghost_size
;
675 * Number of bytes that *would have been* consumed by ARC
676 * buffers that are eligible for eviction, of type
677 * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state.
678 * Not updated directly; only synced in arc_kstat_update.
680 kstat_named_t arcstat_mfu_ghost_evictable_data
;
682 * Number of bytes that *would have been* consumed by ARC
683 * buffers that are eligible for eviction, of type
684 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
685 * Not updated directly; only synced in arc_kstat_update.
687 kstat_named_t arcstat_mfu_ghost_evictable_metadata
;
688 kstat_named_t arcstat_l2_hits
;
689 kstat_named_t arcstat_l2_misses
;
690 kstat_named_t arcstat_l2_feeds
;
691 kstat_named_t arcstat_l2_rw_clash
;
692 kstat_named_t arcstat_l2_read_bytes
;
693 kstat_named_t arcstat_l2_write_bytes
;
694 kstat_named_t arcstat_l2_writes_sent
;
695 kstat_named_t arcstat_l2_writes_done
;
696 kstat_named_t arcstat_l2_writes_error
;
697 kstat_named_t arcstat_l2_writes_lock_retry
;
698 kstat_named_t arcstat_l2_evict_lock_retry
;
699 kstat_named_t arcstat_l2_evict_reading
;
700 kstat_named_t arcstat_l2_evict_l1cached
;
701 kstat_named_t arcstat_l2_free_on_write
;
702 kstat_named_t arcstat_l2_abort_lowmem
;
703 kstat_named_t arcstat_l2_cksum_bad
;
704 kstat_named_t arcstat_l2_io_error
;
705 kstat_named_t arcstat_l2_lsize
;
706 kstat_named_t arcstat_l2_psize
;
707 /* Not updated directly; only synced in arc_kstat_update. */
708 kstat_named_t arcstat_l2_hdr_size
;
709 kstat_named_t arcstat_memory_throttle_count
;
710 kstat_named_t arcstat_memory_direct_count
;
711 kstat_named_t arcstat_memory_indirect_count
;
712 kstat_named_t arcstat_memory_all_bytes
;
713 kstat_named_t arcstat_memory_free_bytes
;
714 kstat_named_t arcstat_memory_available_bytes
;
715 kstat_named_t arcstat_no_grow
;
716 kstat_named_t arcstat_tempreserve
;
717 kstat_named_t arcstat_loaned_bytes
;
718 kstat_named_t arcstat_prune
;
719 /* Not updated directly; only synced in arc_kstat_update. */
720 kstat_named_t arcstat_meta_used
;
721 kstat_named_t arcstat_meta_limit
;
722 kstat_named_t arcstat_dnode_limit
;
723 kstat_named_t arcstat_meta_max
;
724 kstat_named_t arcstat_meta_min
;
725 kstat_named_t arcstat_async_upgrade_sync
;
726 kstat_named_t arcstat_demand_hit_predictive_prefetch
;
727 kstat_named_t arcstat_demand_hit_prescient_prefetch
;
728 kstat_named_t arcstat_need_free
;
729 kstat_named_t arcstat_sys_free
;
730 kstat_named_t arcstat_raw_size
;
733 static arc_stats_t arc_stats
= {
734 { "hits", KSTAT_DATA_UINT64
},
735 { "misses", KSTAT_DATA_UINT64
},
736 { "demand_data_hits", KSTAT_DATA_UINT64
},
737 { "demand_data_misses", KSTAT_DATA_UINT64
},
738 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
739 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
740 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
741 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
742 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
743 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
744 { "mru_hits", KSTAT_DATA_UINT64
},
745 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
746 { "mfu_hits", KSTAT_DATA_UINT64
},
747 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
748 { "deleted", KSTAT_DATA_UINT64
},
749 { "mutex_miss", KSTAT_DATA_UINT64
},
750 { "access_skip", KSTAT_DATA_UINT64
},
751 { "evict_skip", KSTAT_DATA_UINT64
},
752 { "evict_not_enough", KSTAT_DATA_UINT64
},
753 { "evict_l2_cached", KSTAT_DATA_UINT64
},
754 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
755 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
756 { "evict_l2_skip", KSTAT_DATA_UINT64
},
757 { "hash_elements", KSTAT_DATA_UINT64
},
758 { "hash_elements_max", KSTAT_DATA_UINT64
},
759 { "hash_collisions", KSTAT_DATA_UINT64
},
760 { "hash_chains", KSTAT_DATA_UINT64
},
761 { "hash_chain_max", KSTAT_DATA_UINT64
},
762 { "p", KSTAT_DATA_UINT64
},
763 { "c", KSTAT_DATA_UINT64
},
764 { "c_min", KSTAT_DATA_UINT64
},
765 { "c_max", KSTAT_DATA_UINT64
},
766 { "size", KSTAT_DATA_UINT64
},
767 { "compressed_size", KSTAT_DATA_UINT64
},
768 { "uncompressed_size", KSTAT_DATA_UINT64
},
769 { "overhead_size", KSTAT_DATA_UINT64
},
770 { "hdr_size", KSTAT_DATA_UINT64
},
771 { "data_size", KSTAT_DATA_UINT64
},
772 { "metadata_size", KSTAT_DATA_UINT64
},
773 { "dbuf_size", KSTAT_DATA_UINT64
},
774 { "dnode_size", KSTAT_DATA_UINT64
},
775 { "bonus_size", KSTAT_DATA_UINT64
},
776 { "anon_size", KSTAT_DATA_UINT64
},
777 { "anon_evictable_data", KSTAT_DATA_UINT64
},
778 { "anon_evictable_metadata", KSTAT_DATA_UINT64
},
779 { "mru_size", KSTAT_DATA_UINT64
},
780 { "mru_evictable_data", KSTAT_DATA_UINT64
},
781 { "mru_evictable_metadata", KSTAT_DATA_UINT64
},
782 { "mru_ghost_size", KSTAT_DATA_UINT64
},
783 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64
},
784 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
785 { "mfu_size", KSTAT_DATA_UINT64
},
786 { "mfu_evictable_data", KSTAT_DATA_UINT64
},
787 { "mfu_evictable_metadata", KSTAT_DATA_UINT64
},
788 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
789 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64
},
790 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
791 { "l2_hits", KSTAT_DATA_UINT64
},
792 { "l2_misses", KSTAT_DATA_UINT64
},
793 { "l2_feeds", KSTAT_DATA_UINT64
},
794 { "l2_rw_clash", KSTAT_DATA_UINT64
},
795 { "l2_read_bytes", KSTAT_DATA_UINT64
},
796 { "l2_write_bytes", KSTAT_DATA_UINT64
},
797 { "l2_writes_sent", KSTAT_DATA_UINT64
},
798 { "l2_writes_done", KSTAT_DATA_UINT64
},
799 { "l2_writes_error", KSTAT_DATA_UINT64
},
800 { "l2_writes_lock_retry", KSTAT_DATA_UINT64
},
801 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
802 { "l2_evict_reading", KSTAT_DATA_UINT64
},
803 { "l2_evict_l1cached", KSTAT_DATA_UINT64
},
804 { "l2_free_on_write", KSTAT_DATA_UINT64
},
805 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
806 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
807 { "l2_io_error", KSTAT_DATA_UINT64
},
808 { "l2_size", KSTAT_DATA_UINT64
},
809 { "l2_asize", KSTAT_DATA_UINT64
},
810 { "l2_hdr_size", KSTAT_DATA_UINT64
},
811 { "memory_throttle_count", KSTAT_DATA_UINT64
},
812 { "memory_direct_count", KSTAT_DATA_UINT64
},
813 { "memory_indirect_count", KSTAT_DATA_UINT64
},
814 { "memory_all_bytes", KSTAT_DATA_UINT64
},
815 { "memory_free_bytes", KSTAT_DATA_UINT64
},
816 { "memory_available_bytes", KSTAT_DATA_INT64
},
817 { "arc_no_grow", KSTAT_DATA_UINT64
},
818 { "arc_tempreserve", KSTAT_DATA_UINT64
},
819 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
820 { "arc_prune", KSTAT_DATA_UINT64
},
821 { "arc_meta_used", KSTAT_DATA_UINT64
},
822 { "arc_meta_limit", KSTAT_DATA_UINT64
},
823 { "arc_dnode_limit", KSTAT_DATA_UINT64
},
824 { "arc_meta_max", KSTAT_DATA_UINT64
},
825 { "arc_meta_min", KSTAT_DATA_UINT64
},
826 { "async_upgrade_sync", KSTAT_DATA_UINT64
},
827 { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64
},
828 { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64
},
829 { "arc_need_free", KSTAT_DATA_UINT64
},
830 { "arc_sys_free", KSTAT_DATA_UINT64
},
831 { "arc_raw_size", KSTAT_DATA_UINT64
}
834 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
836 #define ARCSTAT_INCR(stat, val) \
837 atomic_add_64(&arc_stats.stat.value.ui64, (val))
839 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
840 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
842 #define ARCSTAT_MAX(stat, val) { \
844 while ((val) > (m = arc_stats.stat.value.ui64) && \
845 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
849 #define ARCSTAT_MAXSTAT(stat) \
850 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
853 * We define a macro to allow ARC hits/misses to be easily broken down by
854 * two separate conditions, giving a total of four different subtypes for
855 * each of hits and misses (so eight statistics total).
857 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
860 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
862 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
866 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
868 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
873 static arc_state_t
*arc_anon
;
874 static arc_state_t
*arc_mru
;
875 static arc_state_t
*arc_mru_ghost
;
876 static arc_state_t
*arc_mfu
;
877 static arc_state_t
*arc_mfu_ghost
;
878 static arc_state_t
*arc_l2c_only
;
881 * There are several ARC variables that are critical to export as kstats --
882 * but we don't want to have to grovel around in the kstat whenever we wish to
883 * manipulate them. For these variables, we therefore define them to be in
884 * terms of the statistic variable. This assures that we are not introducing
885 * the possibility of inconsistency by having shadow copies of the variables,
886 * while still allowing the code to be readable.
888 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
889 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
890 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
891 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
892 #define arc_no_grow ARCSTAT(arcstat_no_grow) /* do not grow cache size */
893 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
894 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
895 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
896 #define arc_dnode_limit ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */
897 #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */
898 #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
899 #define arc_need_free ARCSTAT(arcstat_need_free) /* bytes to be freed */
900 #define arc_sys_free ARCSTAT(arcstat_sys_free) /* target system free bytes */
902 /* size of all b_rabd's in entire arc */
903 #define arc_raw_size ARCSTAT(arcstat_raw_size)
904 /* compressed size of entire arc */
905 #define arc_compressed_size ARCSTAT(arcstat_compressed_size)
906 /* uncompressed size of entire arc */
907 #define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size)
908 /* number of bytes in the arc from arc_buf_t's */
909 #define arc_overhead_size ARCSTAT(arcstat_overhead_size)
912 * There are also some ARC variables that we want to export, but that are
913 * updated so often that having the canonical representation be the statistic
914 * variable causes a performance bottleneck. We want to use aggsum_t's for these
915 * instead, but still be able to export the kstat in the same way as before.
916 * The solution is to always use the aggsum version, except in the kstat update
920 aggsum_t arc_meta_used
;
921 aggsum_t astat_data_size
;
922 aggsum_t astat_metadata_size
;
923 aggsum_t astat_dbuf_size
;
924 aggsum_t astat_dnode_size
;
925 aggsum_t astat_bonus_size
;
926 aggsum_t astat_hdr_size
;
927 aggsum_t astat_l2_hdr_size
;
929 static hrtime_t arc_growtime
;
930 static list_t arc_prune_list
;
931 static kmutex_t arc_prune_mtx
;
932 static taskq_t
*arc_prune_taskq
;
934 #define GHOST_STATE(state) \
935 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
936 (state) == arc_l2c_only)
938 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
939 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
940 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
941 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
942 #define HDR_PRESCIENT_PREFETCH(hdr) \
943 ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH)
944 #define HDR_COMPRESSION_ENABLED(hdr) \
945 ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC)
947 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
948 #define HDR_L2_READING(hdr) \
949 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
950 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
951 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
952 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
953 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
954 #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED)
955 #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH)
956 #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA)
958 #define HDR_ISTYPE_METADATA(hdr) \
959 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
960 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
962 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
963 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
964 #define HDR_HAS_RABD(hdr) \
965 (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \
966 (hdr)->b_crypt_hdr.b_rabd != NULL)
967 #define HDR_ENCRYPTED(hdr) \
968 (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
969 #define HDR_AUTHENTICATED(hdr) \
970 (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
972 /* For storing compression mode in b_flags */
973 #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1)
975 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \
976 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS))
977 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \
978 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp));
980 #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL)
981 #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED)
982 #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED)
983 #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED)
989 #define HDR_FULL_CRYPT_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
990 #define HDR_FULL_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_crypt_hdr))
991 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
994 * Hash table routines
997 #define HT_LOCK_ALIGN 64
998 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
1003 unsigned char pad
[HT_LOCK_PAD
];
1007 #define BUF_LOCKS 8192
1008 typedef struct buf_hash_table
{
1010 arc_buf_hdr_t
**ht_table
;
1011 struct ht_lock ht_locks
[BUF_LOCKS
];
1014 static buf_hash_table_t buf_hash_table
;
1016 #define BUF_HASH_INDEX(spa, dva, birth) \
1017 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
1018 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
1019 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
1020 #define HDR_LOCK(hdr) \
1021 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
1023 uint64_t zfs_crc64_table
[256];
1029 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
1030 #define L2ARC_HEADROOM 2 /* num of writes */
1033 * If we discover during ARC scan any buffers to be compressed, we boost
1034 * our headroom for the next scanning cycle by this percentage multiple.
1036 #define L2ARC_HEADROOM_BOOST 200
1037 #define L2ARC_FEED_SECS 1 /* caching interval secs */
1038 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
1041 * We can feed L2ARC from two states of ARC buffers, mru and mfu,
1042 * and each of the state has two types: data and metadata.
1044 #define L2ARC_FEED_TYPES 4
1046 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
1047 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
1049 /* L2ARC Performance Tunables */
1050 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
1051 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
1052 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
1053 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
1054 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
1055 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
1056 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
1057 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
1058 int l2arc_norw
= B_FALSE
; /* no reads during writes */
1063 static list_t L2ARC_dev_list
; /* device list */
1064 static list_t
*l2arc_dev_list
; /* device list pointer */
1065 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
1066 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
1067 static list_t L2ARC_free_on_write
; /* free after write buf list */
1068 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
1069 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
1070 static uint64_t l2arc_ndev
; /* number of devices */
1072 typedef struct l2arc_read_callback
{
1073 arc_buf_hdr_t
*l2rcb_hdr
; /* read header */
1074 blkptr_t l2rcb_bp
; /* original blkptr */
1075 zbookmark_phys_t l2rcb_zb
; /* original bookmark */
1076 int l2rcb_flags
; /* original flags */
1077 abd_t
*l2rcb_abd
; /* temporary buffer */
1078 } l2arc_read_callback_t
;
1080 typedef struct l2arc_data_free
{
1081 /* protected by l2arc_free_on_write_mtx */
1084 arc_buf_contents_t l2df_type
;
1085 list_node_t l2df_list_node
;
1086 } l2arc_data_free_t
;
1088 typedef enum arc_fill_flags
{
1089 ARC_FILL_LOCKED
= 1 << 0, /* hdr lock is held */
1090 ARC_FILL_COMPRESSED
= 1 << 1, /* fill with compressed data */
1091 ARC_FILL_ENCRYPTED
= 1 << 2, /* fill with encrypted data */
1092 ARC_FILL_NOAUTH
= 1 << 3, /* don't attempt to authenticate */
1093 ARC_FILL_IN_PLACE
= 1 << 4 /* fill in place (special case) */
1096 static kmutex_t l2arc_feed_thr_lock
;
1097 static kcondvar_t l2arc_feed_thr_cv
;
1098 static uint8_t l2arc_thread_exit
;
1100 static abd_t
*arc_get_data_abd(arc_buf_hdr_t
*, uint64_t, void *);
1101 static void *arc_get_data_buf(arc_buf_hdr_t
*, uint64_t, void *);
1102 static void arc_get_data_impl(arc_buf_hdr_t
*, uint64_t, void *);
1103 static void arc_free_data_abd(arc_buf_hdr_t
*, abd_t
*, uint64_t, void *);
1104 static void arc_free_data_buf(arc_buf_hdr_t
*, void *, uint64_t, void *);
1105 static void arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
);
1106 static void arc_hdr_free_abd(arc_buf_hdr_t
*, boolean_t
);
1107 static void arc_hdr_alloc_abd(arc_buf_hdr_t
*, boolean_t
);
1108 static void arc_access(arc_buf_hdr_t
*, kmutex_t
*);
1109 static boolean_t
arc_is_overflowing(void);
1110 static void arc_buf_watch(arc_buf_t
*);
1111 static void arc_tuning_update(void);
1112 static void arc_prune_async(int64_t);
1113 static uint64_t arc_all_memory(void);
1115 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t
*);
1116 static uint32_t arc_bufc_to_flags(arc_buf_contents_t
);
1117 static inline void arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
1118 static inline void arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
1120 static boolean_t
l2arc_write_eligible(uint64_t, arc_buf_hdr_t
*);
1121 static void l2arc_read_done(zio_t
*);
1125 * We use Cityhash for this. It's fast, and has good hash properties without
1126 * requiring any large static buffers.
1129 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
1131 return (cityhash4(spa
, dva
->dva_word
[0], dva
->dva_word
[1], birth
));
1134 #define HDR_EMPTY(hdr) \
1135 ((hdr)->b_dva.dva_word[0] == 0 && \
1136 (hdr)->b_dva.dva_word[1] == 0)
1138 #define HDR_EMPTY_OR_LOCKED(hdr) \
1139 (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr)))
1141 #define HDR_EQUAL(spa, dva, birth, hdr) \
1142 ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
1143 ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
1144 ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa)
1147 buf_discard_identity(arc_buf_hdr_t
*hdr
)
1149 hdr
->b_dva
.dva_word
[0] = 0;
1150 hdr
->b_dva
.dva_word
[1] = 0;
1154 static arc_buf_hdr_t
*
1155 buf_hash_find(uint64_t spa
, const blkptr_t
*bp
, kmutex_t
**lockp
)
1157 const dva_t
*dva
= BP_IDENTITY(bp
);
1158 uint64_t birth
= BP_PHYSICAL_BIRTH(bp
);
1159 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
1160 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1163 mutex_enter(hash_lock
);
1164 for (hdr
= buf_hash_table
.ht_table
[idx
]; hdr
!= NULL
;
1165 hdr
= hdr
->b_hash_next
) {
1166 if (HDR_EQUAL(spa
, dva
, birth
, hdr
)) {
1171 mutex_exit(hash_lock
);
1177 * Insert an entry into the hash table. If there is already an element
1178 * equal to elem in the hash table, then the already existing element
1179 * will be returned and the new element will not be inserted.
1180 * Otherwise returns NULL.
1181 * If lockp == NULL, the caller is assumed to already hold the hash lock.
1183 static arc_buf_hdr_t
*
1184 buf_hash_insert(arc_buf_hdr_t
*hdr
, kmutex_t
**lockp
)
1186 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1187 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1188 arc_buf_hdr_t
*fhdr
;
1191 ASSERT(!DVA_IS_EMPTY(&hdr
->b_dva
));
1192 ASSERT(hdr
->b_birth
!= 0);
1193 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1195 if (lockp
!= NULL
) {
1197 mutex_enter(hash_lock
);
1199 ASSERT(MUTEX_HELD(hash_lock
));
1202 for (fhdr
= buf_hash_table
.ht_table
[idx
], i
= 0; fhdr
!= NULL
;
1203 fhdr
= fhdr
->b_hash_next
, i
++) {
1204 if (HDR_EQUAL(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
, fhdr
))
1208 hdr
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
1209 buf_hash_table
.ht_table
[idx
] = hdr
;
1210 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1212 /* collect some hash table performance data */
1214 ARCSTAT_BUMP(arcstat_hash_collisions
);
1216 ARCSTAT_BUMP(arcstat_hash_chains
);
1218 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
1221 ARCSTAT_BUMP(arcstat_hash_elements
);
1222 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
1228 buf_hash_remove(arc_buf_hdr_t
*hdr
)
1230 arc_buf_hdr_t
*fhdr
, **hdrp
;
1231 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1233 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
1234 ASSERT(HDR_IN_HASH_TABLE(hdr
));
1236 hdrp
= &buf_hash_table
.ht_table
[idx
];
1237 while ((fhdr
= *hdrp
) != hdr
) {
1238 ASSERT3P(fhdr
, !=, NULL
);
1239 hdrp
= &fhdr
->b_hash_next
;
1241 *hdrp
= hdr
->b_hash_next
;
1242 hdr
->b_hash_next
= NULL
;
1243 arc_hdr_clear_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1245 /* collect some hash table performance data */
1246 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
1248 if (buf_hash_table
.ht_table
[idx
] &&
1249 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
1250 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
1254 * Global data structures and functions for the buf kmem cache.
1257 static kmem_cache_t
*hdr_full_cache
;
1258 static kmem_cache_t
*hdr_full_crypt_cache
;
1259 static kmem_cache_t
*hdr_l2only_cache
;
1260 static kmem_cache_t
*buf_cache
;
1267 #if defined(_KERNEL)
1269 * Large allocations which do not require contiguous pages
1270 * should be using vmem_free() in the linux kernel\
1272 vmem_free(buf_hash_table
.ht_table
,
1273 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1275 kmem_free(buf_hash_table
.ht_table
,
1276 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1278 for (i
= 0; i
< BUF_LOCKS
; i
++)
1279 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
1280 kmem_cache_destroy(hdr_full_cache
);
1281 kmem_cache_destroy(hdr_full_crypt_cache
);
1282 kmem_cache_destroy(hdr_l2only_cache
);
1283 kmem_cache_destroy(buf_cache
);
1287 * Constructor callback - called when the cache is empty
1288 * and a new buf is requested.
1292 hdr_full_cons(void *vbuf
, void *unused
, int kmflag
)
1294 arc_buf_hdr_t
*hdr
= vbuf
;
1296 bzero(hdr
, HDR_FULL_SIZE
);
1297 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
1298 cv_init(&hdr
->b_l1hdr
.b_cv
, NULL
, CV_DEFAULT
, NULL
);
1299 zfs_refcount_create(&hdr
->b_l1hdr
.b_refcnt
);
1300 mutex_init(&hdr
->b_l1hdr
.b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1301 list_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1302 list_link_init(&hdr
->b_l2hdr
.b_l2node
);
1303 multilist_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1304 arc_space_consume(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1311 hdr_full_crypt_cons(void *vbuf
, void *unused
, int kmflag
)
1313 arc_buf_hdr_t
*hdr
= vbuf
;
1315 hdr_full_cons(vbuf
, unused
, kmflag
);
1316 bzero(&hdr
->b_crypt_hdr
, sizeof (hdr
->b_crypt_hdr
));
1317 arc_space_consume(sizeof (hdr
->b_crypt_hdr
), ARC_SPACE_HDRS
);
1324 hdr_l2only_cons(void *vbuf
, void *unused
, int kmflag
)
1326 arc_buf_hdr_t
*hdr
= vbuf
;
1328 bzero(hdr
, HDR_L2ONLY_SIZE
);
1329 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1336 buf_cons(void *vbuf
, void *unused
, int kmflag
)
1338 arc_buf_t
*buf
= vbuf
;
1340 bzero(buf
, sizeof (arc_buf_t
));
1341 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1342 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1348 * Destructor callback - called when a cached buf is
1349 * no longer required.
1353 hdr_full_dest(void *vbuf
, void *unused
)
1355 arc_buf_hdr_t
*hdr
= vbuf
;
1357 ASSERT(HDR_EMPTY(hdr
));
1358 cv_destroy(&hdr
->b_l1hdr
.b_cv
);
1359 zfs_refcount_destroy(&hdr
->b_l1hdr
.b_refcnt
);
1360 mutex_destroy(&hdr
->b_l1hdr
.b_freeze_lock
);
1361 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1362 arc_space_return(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1367 hdr_full_crypt_dest(void *vbuf
, void *unused
)
1369 arc_buf_hdr_t
*hdr
= vbuf
;
1371 hdr_full_dest(vbuf
, unused
);
1372 arc_space_return(sizeof (hdr
->b_crypt_hdr
), ARC_SPACE_HDRS
);
1377 hdr_l2only_dest(void *vbuf
, void *unused
)
1379 ASSERTV(arc_buf_hdr_t
*hdr
= vbuf
);
1381 ASSERT(HDR_EMPTY(hdr
));
1382 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1387 buf_dest(void *vbuf
, void *unused
)
1389 arc_buf_t
*buf
= vbuf
;
1391 mutex_destroy(&buf
->b_evict_lock
);
1392 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1396 * Reclaim callback -- invoked when memory is low.
1400 hdr_recl(void *unused
)
1402 dprintf("hdr_recl called\n");
1404 * umem calls the reclaim func when we destroy the buf cache,
1405 * which is after we do arc_fini().
1407 if (arc_initialized
)
1408 zthr_wakeup(arc_reap_zthr
);
1414 uint64_t *ct
= NULL
;
1415 uint64_t hsize
= 1ULL << 12;
1419 * The hash table is big enough to fill all of physical memory
1420 * with an average block size of zfs_arc_average_blocksize (default 8K).
1421 * By default, the table will take up
1422 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1424 while (hsize
* zfs_arc_average_blocksize
< arc_all_memory())
1427 buf_hash_table
.ht_mask
= hsize
- 1;
1428 #if defined(_KERNEL)
1430 * Large allocations which do not require contiguous pages
1431 * should be using vmem_alloc() in the linux kernel
1433 buf_hash_table
.ht_table
=
1434 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1436 buf_hash_table
.ht_table
=
1437 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1439 if (buf_hash_table
.ht_table
== NULL
) {
1440 ASSERT(hsize
> (1ULL << 8));
1445 hdr_full_cache
= kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE
,
1446 0, hdr_full_cons
, hdr_full_dest
, hdr_recl
, NULL
, NULL
, 0);
1447 hdr_full_crypt_cache
= kmem_cache_create("arc_buf_hdr_t_full_crypt",
1448 HDR_FULL_CRYPT_SIZE
, 0, hdr_full_crypt_cons
, hdr_full_crypt_dest
,
1449 hdr_recl
, NULL
, NULL
, 0);
1450 hdr_l2only_cache
= kmem_cache_create("arc_buf_hdr_t_l2only",
1451 HDR_L2ONLY_SIZE
, 0, hdr_l2only_cons
, hdr_l2only_dest
, hdr_recl
,
1453 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1454 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1456 for (i
= 0; i
< 256; i
++)
1457 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1458 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1460 for (i
= 0; i
< BUF_LOCKS
; i
++) {
1461 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
1462 NULL
, MUTEX_DEFAULT
, NULL
);
1466 #define ARC_MINTIME (hz>>4) /* 62 ms */
1469 * This is the size that the buf occupies in memory. If the buf is compressed,
1470 * it will correspond to the compressed size. You should use this method of
1471 * getting the buf size unless you explicitly need the logical size.
1474 arc_buf_size(arc_buf_t
*buf
)
1476 return (ARC_BUF_COMPRESSED(buf
) ?
1477 HDR_GET_PSIZE(buf
->b_hdr
) : HDR_GET_LSIZE(buf
->b_hdr
));
1481 arc_buf_lsize(arc_buf_t
*buf
)
1483 return (HDR_GET_LSIZE(buf
->b_hdr
));
1487 * This function will return B_TRUE if the buffer is encrypted in memory.
1488 * This buffer can be decrypted by calling arc_untransform().
1491 arc_is_encrypted(arc_buf_t
*buf
)
1493 return (ARC_BUF_ENCRYPTED(buf
) != 0);
1497 * Returns B_TRUE if the buffer represents data that has not had its MAC
1501 arc_is_unauthenticated(arc_buf_t
*buf
)
1503 return (HDR_NOAUTH(buf
->b_hdr
) != 0);
1507 arc_get_raw_params(arc_buf_t
*buf
, boolean_t
*byteorder
, uint8_t *salt
,
1508 uint8_t *iv
, uint8_t *mac
)
1510 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1512 ASSERT(HDR_PROTECTED(hdr
));
1514 bcopy(hdr
->b_crypt_hdr
.b_salt
, salt
, ZIO_DATA_SALT_LEN
);
1515 bcopy(hdr
->b_crypt_hdr
.b_iv
, iv
, ZIO_DATA_IV_LEN
);
1516 bcopy(hdr
->b_crypt_hdr
.b_mac
, mac
, ZIO_DATA_MAC_LEN
);
1517 *byteorder
= (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
1518 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
1522 * Indicates how this buffer is compressed in memory. If it is not compressed
1523 * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with
1524 * arc_untransform() as long as it is also unencrypted.
1527 arc_get_compression(arc_buf_t
*buf
)
1529 return (ARC_BUF_COMPRESSED(buf
) ?
1530 HDR_GET_COMPRESS(buf
->b_hdr
) : ZIO_COMPRESS_OFF
);
1534 * Return the compression algorithm used to store this data in the ARC. If ARC
1535 * compression is enabled or this is an encrypted block, this will be the same
1536 * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF.
1538 static inline enum zio_compress
1539 arc_hdr_get_compress(arc_buf_hdr_t
*hdr
)
1541 return (HDR_COMPRESSION_ENABLED(hdr
) ?
1542 HDR_GET_COMPRESS(hdr
) : ZIO_COMPRESS_OFF
);
1545 static inline boolean_t
1546 arc_buf_is_shared(arc_buf_t
*buf
)
1548 boolean_t shared
= (buf
->b_data
!= NULL
&&
1549 buf
->b_hdr
->b_l1hdr
.b_pabd
!= NULL
&&
1550 abd_is_linear(buf
->b_hdr
->b_l1hdr
.b_pabd
) &&
1551 buf
->b_data
== abd_to_buf(buf
->b_hdr
->b_l1hdr
.b_pabd
));
1552 IMPLY(shared
, HDR_SHARED_DATA(buf
->b_hdr
));
1553 IMPLY(shared
, ARC_BUF_SHARED(buf
));
1554 IMPLY(shared
, ARC_BUF_COMPRESSED(buf
) || ARC_BUF_LAST(buf
));
1557 * It would be nice to assert arc_can_share() too, but the "hdr isn't
1558 * already being shared" requirement prevents us from doing that.
1565 * Free the checksum associated with this header. If there is no checksum, this
1569 arc_cksum_free(arc_buf_hdr_t
*hdr
)
1571 ASSERT(HDR_HAS_L1HDR(hdr
));
1573 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1574 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
) {
1575 kmem_free(hdr
->b_l1hdr
.b_freeze_cksum
, sizeof (zio_cksum_t
));
1576 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
1578 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1582 * Return true iff at least one of the bufs on hdr is not compressed.
1583 * Encrypted buffers count as compressed.
1586 arc_hdr_has_uncompressed_buf(arc_buf_hdr_t
*hdr
)
1588 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY_OR_LOCKED(hdr
));
1590 for (arc_buf_t
*b
= hdr
->b_l1hdr
.b_buf
; b
!= NULL
; b
= b
->b_next
) {
1591 if (!ARC_BUF_COMPRESSED(b
)) {
1600 * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data
1601 * matches the checksum that is stored in the hdr. If there is no checksum,
1602 * or if the buf is compressed, this is a no-op.
1605 arc_cksum_verify(arc_buf_t
*buf
)
1607 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1610 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1613 if (ARC_BUF_COMPRESSED(buf
))
1616 ASSERT(HDR_HAS_L1HDR(hdr
));
1618 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1620 if (hdr
->b_l1hdr
.b_freeze_cksum
== NULL
|| HDR_IO_ERROR(hdr
)) {
1621 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1625 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
, &zc
);
1626 if (!ZIO_CHECKSUM_EQUAL(*hdr
->b_l1hdr
.b_freeze_cksum
, zc
))
1627 panic("buffer modified while frozen!");
1628 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1632 * This function makes the assumption that data stored in the L2ARC
1633 * will be transformed exactly as it is in the main pool. Because of
1634 * this we can verify the checksum against the reading process's bp.
1637 arc_cksum_is_equal(arc_buf_hdr_t
*hdr
, zio_t
*zio
)
1639 ASSERT(!BP_IS_EMBEDDED(zio
->io_bp
));
1640 VERIFY3U(BP_GET_PSIZE(zio
->io_bp
), ==, HDR_GET_PSIZE(hdr
));
1643 * Block pointers always store the checksum for the logical data.
1644 * If the block pointer has the gang bit set, then the checksum
1645 * it represents is for the reconstituted data and not for an
1646 * individual gang member. The zio pipeline, however, must be able to
1647 * determine the checksum of each of the gang constituents so it
1648 * treats the checksum comparison differently than what we need
1649 * for l2arc blocks. This prevents us from using the
1650 * zio_checksum_error() interface directly. Instead we must call the
1651 * zio_checksum_error_impl() so that we can ensure the checksum is
1652 * generated using the correct checksum algorithm and accounts for the
1653 * logical I/O size and not just a gang fragment.
1655 return (zio_checksum_error_impl(zio
->io_spa
, zio
->io_bp
,
1656 BP_GET_CHECKSUM(zio
->io_bp
), zio
->io_abd
, zio
->io_size
,
1657 zio
->io_offset
, NULL
) == 0);
1661 * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a
1662 * checksum and attaches it to the buf's hdr so that we can ensure that the buf
1663 * isn't modified later on. If buf is compressed or there is already a checksum
1664 * on the hdr, this is a no-op (we only checksum uncompressed bufs).
1667 arc_cksum_compute(arc_buf_t
*buf
)
1669 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1671 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1674 ASSERT(HDR_HAS_L1HDR(hdr
));
1676 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1677 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
|| ARC_BUF_COMPRESSED(buf
)) {
1678 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1682 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
1683 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1684 hdr
->b_l1hdr
.b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1686 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
,
1687 hdr
->b_l1hdr
.b_freeze_cksum
);
1688 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1694 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1696 panic("Got SIGSEGV at address: 0x%lx\n", (long)si
->si_addr
);
1702 arc_buf_unwatch(arc_buf_t
*buf
)
1706 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1707 PROT_READ
| PROT_WRITE
));
1714 arc_buf_watch(arc_buf_t
*buf
)
1718 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1723 static arc_buf_contents_t
1724 arc_buf_type(arc_buf_hdr_t
*hdr
)
1726 arc_buf_contents_t type
;
1727 if (HDR_ISTYPE_METADATA(hdr
)) {
1728 type
= ARC_BUFC_METADATA
;
1730 type
= ARC_BUFC_DATA
;
1732 VERIFY3U(hdr
->b_type
, ==, type
);
1737 arc_is_metadata(arc_buf_t
*buf
)
1739 return (HDR_ISTYPE_METADATA(buf
->b_hdr
) != 0);
1743 arc_bufc_to_flags(arc_buf_contents_t type
)
1747 /* metadata field is 0 if buffer contains normal data */
1749 case ARC_BUFC_METADATA
:
1750 return (ARC_FLAG_BUFC_METADATA
);
1754 panic("undefined ARC buffer type!");
1755 return ((uint32_t)-1);
1759 arc_buf_thaw(arc_buf_t
*buf
)
1761 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1763 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
1764 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1766 arc_cksum_verify(buf
);
1769 * Compressed buffers do not manipulate the b_freeze_cksum.
1771 if (ARC_BUF_COMPRESSED(buf
))
1774 ASSERT(HDR_HAS_L1HDR(hdr
));
1775 arc_cksum_free(hdr
);
1776 arc_buf_unwatch(buf
);
1780 arc_buf_freeze(arc_buf_t
*buf
)
1782 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1785 if (ARC_BUF_COMPRESSED(buf
))
1788 ASSERT(HDR_HAS_L1HDR(buf
->b_hdr
));
1789 arc_cksum_compute(buf
);
1793 * The arc_buf_hdr_t's b_flags should never be modified directly. Instead,
1794 * the following functions should be used to ensure that the flags are
1795 * updated in a thread-safe way. When manipulating the flags either
1796 * the hash_lock must be held or the hdr must be undiscoverable. This
1797 * ensures that we're not racing with any other threads when updating
1801 arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1803 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1804 hdr
->b_flags
|= flags
;
1808 arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1810 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1811 hdr
->b_flags
&= ~flags
;
1815 * Setting the compression bits in the arc_buf_hdr_t's b_flags is
1816 * done in a special way since we have to clear and set bits
1817 * at the same time. Consumers that wish to set the compression bits
1818 * must use this function to ensure that the flags are updated in
1819 * thread-safe manner.
1822 arc_hdr_set_compress(arc_buf_hdr_t
*hdr
, enum zio_compress cmp
)
1824 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1827 * Holes and embedded blocks will always have a psize = 0 so
1828 * we ignore the compression of the blkptr and set the
1829 * want to uncompress them. Mark them as uncompressed.
1831 if (!zfs_compressed_arc_enabled
|| HDR_GET_PSIZE(hdr
) == 0) {
1832 arc_hdr_clear_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1833 ASSERT(!HDR_COMPRESSION_ENABLED(hdr
));
1835 arc_hdr_set_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1836 ASSERT(HDR_COMPRESSION_ENABLED(hdr
));
1839 HDR_SET_COMPRESS(hdr
, cmp
);
1840 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, cmp
);
1844 * Looks for another buf on the same hdr which has the data decompressed, copies
1845 * from it, and returns true. If no such buf exists, returns false.
1848 arc_buf_try_copy_decompressed_data(arc_buf_t
*buf
)
1850 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1851 boolean_t copied
= B_FALSE
;
1853 ASSERT(HDR_HAS_L1HDR(hdr
));
1854 ASSERT3P(buf
->b_data
, !=, NULL
);
1855 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1857 for (arc_buf_t
*from
= hdr
->b_l1hdr
.b_buf
; from
!= NULL
;
1858 from
= from
->b_next
) {
1859 /* can't use our own data buffer */
1864 if (!ARC_BUF_COMPRESSED(from
)) {
1865 bcopy(from
->b_data
, buf
->b_data
, arc_buf_size(buf
));
1872 * There were no decompressed bufs, so there should not be a
1873 * checksum on the hdr either.
1875 EQUIV(!copied
, hdr
->b_l1hdr
.b_freeze_cksum
== NULL
);
1881 * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t.
1884 arc_hdr_size(arc_buf_hdr_t
*hdr
)
1888 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
1889 HDR_GET_PSIZE(hdr
) > 0) {
1890 size
= HDR_GET_PSIZE(hdr
);
1892 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, 0);
1893 size
= HDR_GET_LSIZE(hdr
);
1899 arc_hdr_authenticate(arc_buf_hdr_t
*hdr
, spa_t
*spa
, uint64_t dsobj
)
1903 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
1904 uint64_t psize
= HDR_GET_PSIZE(hdr
);
1905 void *tmpbuf
= NULL
;
1906 abd_t
*abd
= hdr
->b_l1hdr
.b_pabd
;
1908 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1909 ASSERT(HDR_AUTHENTICATED(hdr
));
1910 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1913 * The MAC is calculated on the compressed data that is stored on disk.
1914 * However, if compressed arc is disabled we will only have the
1915 * decompressed data available to us now. Compress it into a temporary
1916 * abd so we can verify the MAC. The performance overhead of this will
1917 * be relatively low, since most objects in an encrypted objset will
1918 * be encrypted (instead of authenticated) anyway.
1920 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1921 !HDR_COMPRESSION_ENABLED(hdr
)) {
1922 tmpbuf
= zio_buf_alloc(lsize
);
1923 abd
= abd_get_from_buf(tmpbuf
, lsize
);
1924 abd_take_ownership_of_buf(abd
, B_TRUE
);
1926 csize
= zio_compress_data(HDR_GET_COMPRESS(hdr
),
1927 hdr
->b_l1hdr
.b_pabd
, tmpbuf
, lsize
);
1928 ASSERT3U(csize
, <=, psize
);
1929 abd_zero_off(abd
, csize
, psize
- csize
);
1933 * Authentication is best effort. We authenticate whenever the key is
1934 * available. If we succeed we clear ARC_FLAG_NOAUTH.
1936 if (hdr
->b_crypt_hdr
.b_ot
== DMU_OT_OBJSET
) {
1937 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, ZIO_COMPRESS_OFF
);
1938 ASSERT3U(lsize
, ==, psize
);
1939 ret
= spa_do_crypt_objset_mac_abd(B_FALSE
, spa
, dsobj
, abd
,
1940 psize
, hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1942 ret
= spa_do_crypt_mac_abd(B_FALSE
, spa
, dsobj
, abd
, psize
,
1943 hdr
->b_crypt_hdr
.b_mac
);
1947 arc_hdr_clear_flags(hdr
, ARC_FLAG_NOAUTH
);
1948 else if (ret
!= ENOENT
)
1964 * This function will take a header that only has raw encrypted data in
1965 * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in
1966 * b_l1hdr.b_pabd. If designated in the header flags, this function will
1967 * also decompress the data.
1970 arc_hdr_decrypt(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
)
1975 boolean_t no_crypt
= B_FALSE
;
1976 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1978 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1979 ASSERT(HDR_ENCRYPTED(hdr
));
1981 arc_hdr_alloc_abd(hdr
, B_FALSE
);
1983 ret
= spa_do_crypt_abd(B_FALSE
, spa
, zb
, hdr
->b_crypt_hdr
.b_ot
,
1984 B_FALSE
, bswap
, hdr
->b_crypt_hdr
.b_salt
, hdr
->b_crypt_hdr
.b_iv
,
1985 hdr
->b_crypt_hdr
.b_mac
, HDR_GET_PSIZE(hdr
), hdr
->b_l1hdr
.b_pabd
,
1986 hdr
->b_crypt_hdr
.b_rabd
, &no_crypt
);
1991 abd_copy(hdr
->b_l1hdr
.b_pabd
, hdr
->b_crypt_hdr
.b_rabd
,
1992 HDR_GET_PSIZE(hdr
));
1996 * If this header has disabled arc compression but the b_pabd is
1997 * compressed after decrypting it, we need to decompress the newly
2000 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
2001 !HDR_COMPRESSION_ENABLED(hdr
)) {
2003 * We want to make sure that we are correctly honoring the
2004 * zfs_abd_scatter_enabled setting, so we allocate an abd here
2005 * and then loan a buffer from it, rather than allocating a
2006 * linear buffer and wrapping it in an abd later.
2008 cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
);
2009 tmp
= abd_borrow_buf(cabd
, arc_hdr_size(hdr
));
2011 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
2012 hdr
->b_l1hdr
.b_pabd
, tmp
, HDR_GET_PSIZE(hdr
),
2013 HDR_GET_LSIZE(hdr
));
2015 abd_return_buf(cabd
, tmp
, arc_hdr_size(hdr
));
2019 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
2020 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
2021 arc_hdr_size(hdr
), hdr
);
2022 hdr
->b_l1hdr
.b_pabd
= cabd
;
2028 arc_hdr_free_abd(hdr
, B_FALSE
);
2030 arc_free_data_buf(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
2036 * This function is called during arc_buf_fill() to prepare the header's
2037 * abd plaintext pointer for use. This involves authenticated protected
2038 * data and decrypting encrypted data into the plaintext abd.
2041 arc_fill_hdr_crypt(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, spa_t
*spa
,
2042 const zbookmark_phys_t
*zb
, boolean_t noauth
)
2046 ASSERT(HDR_PROTECTED(hdr
));
2048 if (hash_lock
!= NULL
)
2049 mutex_enter(hash_lock
);
2051 if (HDR_NOAUTH(hdr
) && !noauth
) {
2053 * The caller requested authenticated data but our data has
2054 * not been authenticated yet. Verify the MAC now if we can.
2056 ret
= arc_hdr_authenticate(hdr
, spa
, zb
->zb_objset
);
2059 } else if (HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
== NULL
) {
2061 * If we only have the encrypted version of the data, but the
2062 * unencrypted version was requested we take this opportunity
2063 * to store the decrypted version in the header for future use.
2065 ret
= arc_hdr_decrypt(hdr
, spa
, zb
);
2070 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
2072 if (hash_lock
!= NULL
)
2073 mutex_exit(hash_lock
);
2078 if (hash_lock
!= NULL
)
2079 mutex_exit(hash_lock
);
2085 * This function is used by the dbuf code to decrypt bonus buffers in place.
2086 * The dbuf code itself doesn't have any locking for decrypting a shared dnode
2087 * block, so we use the hash lock here to protect against concurrent calls to
2091 arc_buf_untransform_in_place(arc_buf_t
*buf
, kmutex_t
*hash_lock
)
2093 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2095 ASSERT(HDR_ENCRYPTED(hdr
));
2096 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
2097 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
2098 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
2100 zio_crypt_copy_dnode_bonus(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
2102 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
2103 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
2104 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
2108 * Given a buf that has a data buffer attached to it, this function will
2109 * efficiently fill the buf with data of the specified compression setting from
2110 * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr
2111 * are already sharing a data buf, no copy is performed.
2113 * If the buf is marked as compressed but uncompressed data was requested, this
2114 * will allocate a new data buffer for the buf, remove that flag, and fill the
2115 * buf with uncompressed data. You can't request a compressed buf on a hdr with
2116 * uncompressed data, and (since we haven't added support for it yet) if you
2117 * want compressed data your buf must already be marked as compressed and have
2118 * the correct-sized data buffer.
2121 arc_buf_fill(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2122 arc_fill_flags_t flags
)
2125 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2126 boolean_t hdr_compressed
=
2127 (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
2128 boolean_t compressed
= (flags
& ARC_FILL_COMPRESSED
) != 0;
2129 boolean_t encrypted
= (flags
& ARC_FILL_ENCRYPTED
) != 0;
2130 dmu_object_byteswap_t bswap
= hdr
->b_l1hdr
.b_byteswap
;
2131 kmutex_t
*hash_lock
= (flags
& ARC_FILL_LOCKED
) ? NULL
: HDR_LOCK(hdr
);
2133 ASSERT3P(buf
->b_data
, !=, NULL
);
2134 IMPLY(compressed
, hdr_compressed
|| ARC_BUF_ENCRYPTED(buf
));
2135 IMPLY(compressed
, ARC_BUF_COMPRESSED(buf
));
2136 IMPLY(encrypted
, HDR_ENCRYPTED(hdr
));
2137 IMPLY(encrypted
, ARC_BUF_ENCRYPTED(buf
));
2138 IMPLY(encrypted
, ARC_BUF_COMPRESSED(buf
));
2139 IMPLY(encrypted
, !ARC_BUF_SHARED(buf
));
2142 * If the caller wanted encrypted data we just need to copy it from
2143 * b_rabd and potentially byteswap it. We won't be able to do any
2144 * further transforms on it.
2147 ASSERT(HDR_HAS_RABD(hdr
));
2148 abd_copy_to_buf(buf
->b_data
, hdr
->b_crypt_hdr
.b_rabd
,
2149 HDR_GET_PSIZE(hdr
));
2154 * Adjust encrypted and authenticated headers to accomodate
2155 * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are
2156 * allowed to fail decryption due to keys not being loaded
2157 * without being marked as an IO error.
2159 if (HDR_PROTECTED(hdr
)) {
2160 error
= arc_fill_hdr_crypt(hdr
, hash_lock
, spa
,
2161 zb
, !!(flags
& ARC_FILL_NOAUTH
));
2162 if (error
== EACCES
&& (flags
& ARC_FILL_IN_PLACE
) != 0) {
2164 } else if (error
!= 0) {
2165 if (hash_lock
!= NULL
)
2166 mutex_enter(hash_lock
);
2167 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2168 if (hash_lock
!= NULL
)
2169 mutex_exit(hash_lock
);
2175 * There is a special case here for dnode blocks which are
2176 * decrypting their bonus buffers. These blocks may request to
2177 * be decrypted in-place. This is necessary because there may
2178 * be many dnodes pointing into this buffer and there is
2179 * currently no method to synchronize replacing the backing
2180 * b_data buffer and updating all of the pointers. Here we use
2181 * the hash lock to ensure there are no races. If the need
2182 * arises for other types to be decrypted in-place, they must
2183 * add handling here as well.
2185 if ((flags
& ARC_FILL_IN_PLACE
) != 0) {
2186 ASSERT(!hdr_compressed
);
2187 ASSERT(!compressed
);
2190 if (HDR_ENCRYPTED(hdr
) && ARC_BUF_ENCRYPTED(buf
)) {
2191 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
2193 if (hash_lock
!= NULL
)
2194 mutex_enter(hash_lock
);
2195 arc_buf_untransform_in_place(buf
, hash_lock
);
2196 if (hash_lock
!= NULL
)
2197 mutex_exit(hash_lock
);
2199 /* Compute the hdr's checksum if necessary */
2200 arc_cksum_compute(buf
);
2206 if (hdr_compressed
== compressed
) {
2207 if (!arc_buf_is_shared(buf
)) {
2208 abd_copy_to_buf(buf
->b_data
, hdr
->b_l1hdr
.b_pabd
,
2212 ASSERT(hdr_compressed
);
2213 ASSERT(!compressed
);
2214 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, HDR_GET_PSIZE(hdr
));
2217 * If the buf is sharing its data with the hdr, unlink it and
2218 * allocate a new data buffer for the buf.
2220 if (arc_buf_is_shared(buf
)) {
2221 ASSERT(ARC_BUF_COMPRESSED(buf
));
2223 /* We need to give the buf it's own b_data */
2224 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
2226 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2227 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2229 /* Previously overhead was 0; just add new overhead */
2230 ARCSTAT_INCR(arcstat_overhead_size
, HDR_GET_LSIZE(hdr
));
2231 } else if (ARC_BUF_COMPRESSED(buf
)) {
2232 /* We need to reallocate the buf's b_data */
2233 arc_free_data_buf(hdr
, buf
->b_data
, HDR_GET_PSIZE(hdr
),
2236 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2238 /* We increased the size of b_data; update overhead */
2239 ARCSTAT_INCR(arcstat_overhead_size
,
2240 HDR_GET_LSIZE(hdr
) - HDR_GET_PSIZE(hdr
));
2244 * Regardless of the buf's previous compression settings, it
2245 * should not be compressed at the end of this function.
2247 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
2250 * Try copying the data from another buf which already has a
2251 * decompressed version. If that's not possible, it's time to
2252 * bite the bullet and decompress the data from the hdr.
2254 if (arc_buf_try_copy_decompressed_data(buf
)) {
2255 /* Skip byteswapping and checksumming (already done) */
2256 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, !=, NULL
);
2259 error
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
2260 hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
2261 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
2264 * Absent hardware errors or software bugs, this should
2265 * be impossible, but log it anyway so we can debug it.
2269 "hdr %p, compress %d, psize %d, lsize %d",
2270 hdr
, arc_hdr_get_compress(hdr
),
2271 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
2272 if (hash_lock
!= NULL
)
2273 mutex_enter(hash_lock
);
2274 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2275 if (hash_lock
!= NULL
)
2276 mutex_exit(hash_lock
);
2277 return (SET_ERROR(EIO
));
2283 /* Byteswap the buf's data if necessary */
2284 if (bswap
!= DMU_BSWAP_NUMFUNCS
) {
2285 ASSERT(!HDR_SHARED_DATA(hdr
));
2286 ASSERT3U(bswap
, <, DMU_BSWAP_NUMFUNCS
);
2287 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, HDR_GET_LSIZE(hdr
));
2290 /* Compute the hdr's checksum if necessary */
2291 arc_cksum_compute(buf
);
2297 * If this function is being called to decrypt an encrypted buffer or verify an
2298 * authenticated one, the key must be loaded and a mapping must be made
2299 * available in the keystore via spa_keystore_create_mapping() or one of its
2303 arc_untransform(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2307 arc_fill_flags_t flags
= 0;
2310 flags
|= ARC_FILL_IN_PLACE
;
2312 ret
= arc_buf_fill(buf
, spa
, zb
, flags
);
2313 if (ret
== ECKSUM
) {
2315 * Convert authentication and decryption errors to EIO
2316 * (and generate an ereport) before leaving the ARC.
2318 ret
= SET_ERROR(EIO
);
2319 spa_log_error(spa
, zb
);
2320 zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION
,
2321 spa
, NULL
, zb
, NULL
, 0, 0);
2328 * Increment the amount of evictable space in the arc_state_t's refcount.
2329 * We account for the space used by the hdr and the arc buf individually
2330 * so that we can add and remove them from the refcount individually.
2333 arc_evictable_space_increment(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2335 arc_buf_contents_t type
= arc_buf_type(hdr
);
2337 ASSERT(HDR_HAS_L1HDR(hdr
));
2339 if (GHOST_STATE(state
)) {
2340 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2341 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2342 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2343 ASSERT(!HDR_HAS_RABD(hdr
));
2344 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2345 HDR_GET_LSIZE(hdr
), hdr
);
2349 ASSERT(!GHOST_STATE(state
));
2350 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2351 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2352 arc_hdr_size(hdr
), hdr
);
2354 if (HDR_HAS_RABD(hdr
)) {
2355 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2356 HDR_GET_PSIZE(hdr
), hdr
);
2359 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2360 buf
= buf
->b_next
) {
2361 if (arc_buf_is_shared(buf
))
2363 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2364 arc_buf_size(buf
), buf
);
2369 * Decrement the amount of evictable space in the arc_state_t's refcount.
2370 * We account for the space used by the hdr and the arc buf individually
2371 * so that we can add and remove them from the refcount individually.
2374 arc_evictable_space_decrement(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2376 arc_buf_contents_t type
= arc_buf_type(hdr
);
2378 ASSERT(HDR_HAS_L1HDR(hdr
));
2380 if (GHOST_STATE(state
)) {
2381 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2382 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2383 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2384 ASSERT(!HDR_HAS_RABD(hdr
));
2385 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2386 HDR_GET_LSIZE(hdr
), hdr
);
2390 ASSERT(!GHOST_STATE(state
));
2391 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2392 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2393 arc_hdr_size(hdr
), hdr
);
2395 if (HDR_HAS_RABD(hdr
)) {
2396 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2397 HDR_GET_PSIZE(hdr
), hdr
);
2400 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2401 buf
= buf
->b_next
) {
2402 if (arc_buf_is_shared(buf
))
2404 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2405 arc_buf_size(buf
), buf
);
2410 * Add a reference to this hdr indicating that someone is actively
2411 * referencing that memory. When the refcount transitions from 0 to 1,
2412 * we remove it from the respective arc_state_t list to indicate that
2413 * it is not evictable.
2416 add_reference(arc_buf_hdr_t
*hdr
, void *tag
)
2420 ASSERT(HDR_HAS_L1HDR(hdr
));
2421 if (!HDR_EMPTY(hdr
) && !MUTEX_HELD(HDR_LOCK(hdr
))) {
2422 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
2423 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2424 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2427 state
= hdr
->b_l1hdr
.b_state
;
2429 if ((zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
) == 1) &&
2430 (state
!= arc_anon
)) {
2431 /* We don't use the L2-only state list. */
2432 if (state
!= arc_l2c_only
) {
2433 multilist_remove(state
->arcs_list
[arc_buf_type(hdr
)],
2435 arc_evictable_space_decrement(hdr
, state
);
2437 /* remove the prefetch flag if we get a reference */
2438 arc_hdr_clear_flags(hdr
, ARC_FLAG_PREFETCH
);
2443 * Remove a reference from this hdr. When the reference transitions from
2444 * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's
2445 * list making it eligible for eviction.
2448 remove_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
2451 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2453 ASSERT(HDR_HAS_L1HDR(hdr
));
2454 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
2455 ASSERT(!GHOST_STATE(state
));
2458 * arc_l2c_only counts as a ghost state so we don't need to explicitly
2459 * check to prevent usage of the arc_l2c_only list.
2461 if (((cnt
= zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
)) == 0) &&
2462 (state
!= arc_anon
)) {
2463 multilist_insert(state
->arcs_list
[arc_buf_type(hdr
)], hdr
);
2464 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
2465 arc_evictable_space_increment(hdr
, state
);
2471 * Returns detailed information about a specific arc buffer. When the
2472 * state_index argument is set the function will calculate the arc header
2473 * list position for its arc state. Since this requires a linear traversal
2474 * callers are strongly encourage not to do this. However, it can be helpful
2475 * for targeted analysis so the functionality is provided.
2478 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
2480 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
2481 l1arc_buf_hdr_t
*l1hdr
= NULL
;
2482 l2arc_buf_hdr_t
*l2hdr
= NULL
;
2483 arc_state_t
*state
= NULL
;
2485 memset(abi
, 0, sizeof (arc_buf_info_t
));
2490 abi
->abi_flags
= hdr
->b_flags
;
2492 if (HDR_HAS_L1HDR(hdr
)) {
2493 l1hdr
= &hdr
->b_l1hdr
;
2494 state
= l1hdr
->b_state
;
2496 if (HDR_HAS_L2HDR(hdr
))
2497 l2hdr
= &hdr
->b_l2hdr
;
2500 abi
->abi_bufcnt
= l1hdr
->b_bufcnt
;
2501 abi
->abi_access
= l1hdr
->b_arc_access
;
2502 abi
->abi_mru_hits
= l1hdr
->b_mru_hits
;
2503 abi
->abi_mru_ghost_hits
= l1hdr
->b_mru_ghost_hits
;
2504 abi
->abi_mfu_hits
= l1hdr
->b_mfu_hits
;
2505 abi
->abi_mfu_ghost_hits
= l1hdr
->b_mfu_ghost_hits
;
2506 abi
->abi_holds
= zfs_refcount_count(&l1hdr
->b_refcnt
);
2510 abi
->abi_l2arc_dattr
= l2hdr
->b_daddr
;
2511 abi
->abi_l2arc_hits
= l2hdr
->b_hits
;
2514 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
2515 abi
->abi_state_contents
= arc_buf_type(hdr
);
2516 abi
->abi_size
= arc_hdr_size(hdr
);
2520 * Move the supplied buffer to the indicated state. The hash lock
2521 * for the buffer must be held by the caller.
2524 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*hdr
,
2525 kmutex_t
*hash_lock
)
2527 arc_state_t
*old_state
;
2530 boolean_t update_old
, update_new
;
2531 arc_buf_contents_t buftype
= arc_buf_type(hdr
);
2534 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
2535 * in arc_read() when bringing a buffer out of the L2ARC. However, the
2536 * L1 hdr doesn't always exist when we change state to arc_anon before
2537 * destroying a header, in which case reallocating to add the L1 hdr is
2540 if (HDR_HAS_L1HDR(hdr
)) {
2541 old_state
= hdr
->b_l1hdr
.b_state
;
2542 refcnt
= zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
);
2543 bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
2544 update_old
= (bufcnt
> 0 || hdr
->b_l1hdr
.b_pabd
!= NULL
||
2547 old_state
= arc_l2c_only
;
2550 update_old
= B_FALSE
;
2552 update_new
= update_old
;
2554 ASSERT(MUTEX_HELD(hash_lock
));
2555 ASSERT3P(new_state
, !=, old_state
);
2556 ASSERT(!GHOST_STATE(new_state
) || bufcnt
== 0);
2557 ASSERT(old_state
!= arc_anon
|| bufcnt
<= 1);
2560 * If this buffer is evictable, transfer it from the
2561 * old state list to the new state list.
2564 if (old_state
!= arc_anon
&& old_state
!= arc_l2c_only
) {
2565 ASSERT(HDR_HAS_L1HDR(hdr
));
2566 multilist_remove(old_state
->arcs_list
[buftype
], hdr
);
2568 if (GHOST_STATE(old_state
)) {
2570 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2571 update_old
= B_TRUE
;
2573 arc_evictable_space_decrement(hdr
, old_state
);
2575 if (new_state
!= arc_anon
&& new_state
!= arc_l2c_only
) {
2577 * An L1 header always exists here, since if we're
2578 * moving to some L1-cached state (i.e. not l2c_only or
2579 * anonymous), we realloc the header to add an L1hdr
2582 ASSERT(HDR_HAS_L1HDR(hdr
));
2583 multilist_insert(new_state
->arcs_list
[buftype
], hdr
);
2585 if (GHOST_STATE(new_state
)) {
2587 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2588 update_new
= B_TRUE
;
2590 arc_evictable_space_increment(hdr
, new_state
);
2594 ASSERT(!HDR_EMPTY(hdr
));
2595 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(hdr
))
2596 buf_hash_remove(hdr
);
2598 /* adjust state sizes (ignore arc_l2c_only) */
2600 if (update_new
&& new_state
!= arc_l2c_only
) {
2601 ASSERT(HDR_HAS_L1HDR(hdr
));
2602 if (GHOST_STATE(new_state
)) {
2606 * When moving a header to a ghost state, we first
2607 * remove all arc buffers. Thus, we'll have a
2608 * bufcnt of zero, and no arc buffer to use for
2609 * the reference. As a result, we use the arc
2610 * header pointer for the reference.
2612 (void) zfs_refcount_add_many(&new_state
->arcs_size
,
2613 HDR_GET_LSIZE(hdr
), hdr
);
2614 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2615 ASSERT(!HDR_HAS_RABD(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) zfs_refcount_add_many(
2640 &new_state
->arcs_size
,
2641 arc_buf_size(buf
), buf
);
2643 ASSERT3U(bufcnt
, ==, buffers
);
2645 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2646 (void) zfs_refcount_add_many(
2647 &new_state
->arcs_size
,
2648 arc_hdr_size(hdr
), hdr
);
2651 if (HDR_HAS_RABD(hdr
)) {
2652 (void) zfs_refcount_add_many(
2653 &new_state
->arcs_size
,
2654 HDR_GET_PSIZE(hdr
), hdr
);
2659 if (update_old
&& old_state
!= arc_l2c_only
) {
2660 ASSERT(HDR_HAS_L1HDR(hdr
));
2661 if (GHOST_STATE(old_state
)) {
2663 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2664 ASSERT(!HDR_HAS_RABD(hdr
));
2667 * When moving a header off of a ghost state,
2668 * the header will not contain any arc buffers.
2669 * We use the arc header pointer for the reference
2670 * which is exactly what we did when we put the
2671 * header on the ghost state.
2674 (void) zfs_refcount_remove_many(&old_state
->arcs_size
,
2675 HDR_GET_LSIZE(hdr
), hdr
);
2677 uint32_t buffers
= 0;
2680 * Each individual buffer holds a unique reference,
2681 * thus we must remove each of these references one
2684 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2685 buf
= buf
->b_next
) {
2686 ASSERT3U(bufcnt
, !=, 0);
2690 * When the arc_buf_t is sharing the data
2691 * block with the hdr, the owner of the
2692 * reference belongs to the hdr. Only
2693 * add to the refcount if the arc_buf_t is
2696 if (arc_buf_is_shared(buf
))
2699 (void) zfs_refcount_remove_many(
2700 &old_state
->arcs_size
, arc_buf_size(buf
),
2703 ASSERT3U(bufcnt
, ==, buffers
);
2704 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
2707 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2708 (void) zfs_refcount_remove_many(
2709 &old_state
->arcs_size
, arc_hdr_size(hdr
),
2713 if (HDR_HAS_RABD(hdr
)) {
2714 (void) zfs_refcount_remove_many(
2715 &old_state
->arcs_size
, HDR_GET_PSIZE(hdr
),
2721 if (HDR_HAS_L1HDR(hdr
))
2722 hdr
->b_l1hdr
.b_state
= new_state
;
2725 * L2 headers should never be on the L2 state list since they don't
2726 * have L1 headers allocated.
2728 ASSERT(multilist_is_empty(arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]) &&
2729 multilist_is_empty(arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]));
2733 arc_space_consume(uint64_t space
, arc_space_type_t type
)
2735 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2740 case ARC_SPACE_DATA
:
2741 aggsum_add(&astat_data_size
, space
);
2743 case ARC_SPACE_META
:
2744 aggsum_add(&astat_metadata_size
, space
);
2746 case ARC_SPACE_BONUS
:
2747 aggsum_add(&astat_bonus_size
, space
);
2749 case ARC_SPACE_DNODE
:
2750 aggsum_add(&astat_dnode_size
, space
);
2752 case ARC_SPACE_DBUF
:
2753 aggsum_add(&astat_dbuf_size
, space
);
2755 case ARC_SPACE_HDRS
:
2756 aggsum_add(&astat_hdr_size
, space
);
2758 case ARC_SPACE_L2HDRS
:
2759 aggsum_add(&astat_l2_hdr_size
, space
);
2763 if (type
!= ARC_SPACE_DATA
)
2764 aggsum_add(&arc_meta_used
, space
);
2766 aggsum_add(&arc_size
, space
);
2770 arc_space_return(uint64_t space
, arc_space_type_t type
)
2772 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2777 case ARC_SPACE_DATA
:
2778 aggsum_add(&astat_data_size
, -space
);
2780 case ARC_SPACE_META
:
2781 aggsum_add(&astat_metadata_size
, -space
);
2783 case ARC_SPACE_BONUS
:
2784 aggsum_add(&astat_bonus_size
, -space
);
2786 case ARC_SPACE_DNODE
:
2787 aggsum_add(&astat_dnode_size
, -space
);
2789 case ARC_SPACE_DBUF
:
2790 aggsum_add(&astat_dbuf_size
, -space
);
2792 case ARC_SPACE_HDRS
:
2793 aggsum_add(&astat_hdr_size
, -space
);
2795 case ARC_SPACE_L2HDRS
:
2796 aggsum_add(&astat_l2_hdr_size
, -space
);
2800 if (type
!= ARC_SPACE_DATA
) {
2801 ASSERT(aggsum_compare(&arc_meta_used
, space
) >= 0);
2803 * We use the upper bound here rather than the precise value
2804 * because the arc_meta_max value doesn't need to be
2805 * precise. It's only consumed by humans via arcstats.
2807 if (arc_meta_max
< aggsum_upper_bound(&arc_meta_used
))
2808 arc_meta_max
= aggsum_upper_bound(&arc_meta_used
);
2809 aggsum_add(&arc_meta_used
, -space
);
2812 ASSERT(aggsum_compare(&arc_size
, space
) >= 0);
2813 aggsum_add(&arc_size
, -space
);
2817 * Given a hdr and a buf, returns whether that buf can share its b_data buffer
2818 * with the hdr's b_pabd.
2821 arc_can_share(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2824 * The criteria for sharing a hdr's data are:
2825 * 1. the buffer is not encrypted
2826 * 2. the hdr's compression matches the buf's compression
2827 * 3. the hdr doesn't need to be byteswapped
2828 * 4. the hdr isn't already being shared
2829 * 5. the buf is either compressed or it is the last buf in the hdr list
2831 * Criterion #5 maintains the invariant that shared uncompressed
2832 * bufs must be the final buf in the hdr's b_buf list. Reading this, you
2833 * might ask, "if a compressed buf is allocated first, won't that be the
2834 * last thing in the list?", but in that case it's impossible to create
2835 * a shared uncompressed buf anyway (because the hdr must be compressed
2836 * to have the compressed buf). You might also think that #3 is
2837 * sufficient to make this guarantee, however it's possible
2838 * (specifically in the rare L2ARC write race mentioned in
2839 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that
2840 * is sharable, but wasn't at the time of its allocation. Rather than
2841 * allow a new shared uncompressed buf to be created and then shuffle
2842 * the list around to make it the last element, this simply disallows
2843 * sharing if the new buf isn't the first to be added.
2845 ASSERT3P(buf
->b_hdr
, ==, hdr
);
2846 boolean_t hdr_compressed
=
2847 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
;
2848 boolean_t buf_compressed
= ARC_BUF_COMPRESSED(buf
) != 0;
2849 return (!ARC_BUF_ENCRYPTED(buf
) &&
2850 buf_compressed
== hdr_compressed
&&
2851 hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
&&
2852 !HDR_SHARED_DATA(hdr
) &&
2853 (ARC_BUF_LAST(buf
) || ARC_BUF_COMPRESSED(buf
)));
2857 * Allocate a buf for this hdr. If you care about the data that's in the hdr,
2858 * or if you want a compressed buffer, pass those flags in. Returns 0 if the
2859 * copy was made successfully, or an error code otherwise.
2862 arc_buf_alloc_impl(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2863 void *tag
, boolean_t encrypted
, boolean_t compressed
, boolean_t noauth
,
2864 boolean_t fill
, arc_buf_t
**ret
)
2867 arc_fill_flags_t flags
= ARC_FILL_LOCKED
;
2869 ASSERT(HDR_HAS_L1HDR(hdr
));
2870 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
2871 VERIFY(hdr
->b_type
== ARC_BUFC_DATA
||
2872 hdr
->b_type
== ARC_BUFC_METADATA
);
2873 ASSERT3P(ret
, !=, NULL
);
2874 ASSERT3P(*ret
, ==, NULL
);
2875 IMPLY(encrypted
, compressed
);
2877 hdr
->b_l1hdr
.b_mru_hits
= 0;
2878 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
2879 hdr
->b_l1hdr
.b_mfu_hits
= 0;
2880 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
2881 hdr
->b_l1hdr
.b_l2_hits
= 0;
2883 buf
= *ret
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
2886 buf
->b_next
= hdr
->b_l1hdr
.b_buf
;
2889 add_reference(hdr
, tag
);
2892 * We're about to change the hdr's b_flags. We must either
2893 * hold the hash_lock or be undiscoverable.
2895 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
2898 * Only honor requests for compressed bufs if the hdr is actually
2899 * compressed. This must be overriden if the buffer is encrypted since
2900 * encrypted buffers cannot be decompressed.
2903 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2904 buf
->b_flags
|= ARC_BUF_FLAG_ENCRYPTED
;
2905 flags
|= ARC_FILL_COMPRESSED
| ARC_FILL_ENCRYPTED
;
2906 } else if (compressed
&&
2907 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
2908 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2909 flags
|= ARC_FILL_COMPRESSED
;
2914 flags
|= ARC_FILL_NOAUTH
;
2918 * If the hdr's data can be shared then we share the data buffer and
2919 * set the appropriate bit in the hdr's b_flags to indicate the hdr is
2920 * allocate a new buffer to store the buf's data.
2922 * There are two additional restrictions here because we're sharing
2923 * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be
2924 * actively involved in an L2ARC write, because if this buf is used by
2925 * an arc_write() then the hdr's data buffer will be released when the
2926 * write completes, even though the L2ARC write might still be using it.
2927 * Second, the hdr's ABD must be linear so that the buf's user doesn't
2928 * need to be ABD-aware.
2930 boolean_t can_share
= arc_can_share(hdr
, buf
) && !HDR_L2_WRITING(hdr
) &&
2931 hdr
->b_l1hdr
.b_pabd
!= NULL
&& abd_is_linear(hdr
->b_l1hdr
.b_pabd
);
2933 /* Set up b_data and sharing */
2935 buf
->b_data
= abd_to_buf(hdr
->b_l1hdr
.b_pabd
);
2936 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2937 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2940 arc_get_data_buf(hdr
, arc_buf_size(buf
), buf
);
2941 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
2943 VERIFY3P(buf
->b_data
, !=, NULL
);
2945 hdr
->b_l1hdr
.b_buf
= buf
;
2946 hdr
->b_l1hdr
.b_bufcnt
+= 1;
2948 hdr
->b_crypt_hdr
.b_ebufcnt
+= 1;
2951 * If the user wants the data from the hdr, we need to either copy or
2952 * decompress the data.
2955 ASSERT3P(zb
, !=, NULL
);
2956 return (arc_buf_fill(buf
, spa
, zb
, flags
));
2962 static char *arc_onloan_tag
= "onloan";
2965 arc_loaned_bytes_update(int64_t delta
)
2967 atomic_add_64(&arc_loaned_bytes
, delta
);
2969 /* assert that it did not wrap around */
2970 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
2974 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
2975 * flight data by arc_tempreserve_space() until they are "returned". Loaned
2976 * buffers must be returned to the arc before they can be used by the DMU or
2980 arc_loan_buf(spa_t
*spa
, boolean_t is_metadata
, int size
)
2982 arc_buf_t
*buf
= arc_alloc_buf(spa
, arc_onloan_tag
,
2983 is_metadata
? ARC_BUFC_METADATA
: ARC_BUFC_DATA
, size
);
2985 arc_loaned_bytes_update(arc_buf_size(buf
));
2991 arc_loan_compressed_buf(spa_t
*spa
, uint64_t psize
, uint64_t lsize
,
2992 enum zio_compress compression_type
)
2994 arc_buf_t
*buf
= arc_alloc_compressed_buf(spa
, arc_onloan_tag
,
2995 psize
, lsize
, compression_type
);
2997 arc_loaned_bytes_update(arc_buf_size(buf
));
3003 arc_loan_raw_buf(spa_t
*spa
, uint64_t dsobj
, boolean_t byteorder
,
3004 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
3005 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
3006 enum zio_compress compression_type
)
3008 arc_buf_t
*buf
= arc_alloc_raw_buf(spa
, arc_onloan_tag
, dsobj
,
3009 byteorder
, salt
, iv
, mac
, ot
, psize
, lsize
, compression_type
);
3011 atomic_add_64(&arc_loaned_bytes
, psize
);
3017 * Return a loaned arc buffer to the arc.
3020 arc_return_buf(arc_buf_t
*buf
, void *tag
)
3022 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3024 ASSERT3P(buf
->b_data
, !=, NULL
);
3025 ASSERT(HDR_HAS_L1HDR(hdr
));
3026 (void) zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
3027 (void) zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
3029 arc_loaned_bytes_update(-arc_buf_size(buf
));
3032 /* Detach an arc_buf from a dbuf (tag) */
3034 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
3036 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3038 ASSERT3P(buf
->b_data
, !=, NULL
);
3039 ASSERT(HDR_HAS_L1HDR(hdr
));
3040 (void) zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
3041 (void) zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
);
3043 arc_loaned_bytes_update(arc_buf_size(buf
));
3047 l2arc_free_abd_on_write(abd_t
*abd
, size_t size
, arc_buf_contents_t type
)
3049 l2arc_data_free_t
*df
= kmem_alloc(sizeof (*df
), KM_SLEEP
);
3052 df
->l2df_size
= size
;
3053 df
->l2df_type
= type
;
3054 mutex_enter(&l2arc_free_on_write_mtx
);
3055 list_insert_head(l2arc_free_on_write
, df
);
3056 mutex_exit(&l2arc_free_on_write_mtx
);
3060 arc_hdr_free_on_write(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
3062 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
3063 arc_buf_contents_t type
= arc_buf_type(hdr
);
3064 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
3066 /* protected by hash lock, if in the hash table */
3067 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
3068 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3069 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
3071 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
3074 (void) zfs_refcount_remove_many(&state
->arcs_size
, size
, hdr
);
3075 if (type
== ARC_BUFC_METADATA
) {
3076 arc_space_return(size
, ARC_SPACE_META
);
3078 ASSERT(type
== ARC_BUFC_DATA
);
3079 arc_space_return(size
, ARC_SPACE_DATA
);
3083 l2arc_free_abd_on_write(hdr
->b_crypt_hdr
.b_rabd
, size
, type
);
3085 l2arc_free_abd_on_write(hdr
->b_l1hdr
.b_pabd
, size
, type
);
3090 * Share the arc_buf_t's data with the hdr. Whenever we are sharing the
3091 * data buffer, we transfer the refcount ownership to the hdr and update
3092 * the appropriate kstats.
3095 arc_share_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3097 ASSERT(arc_can_share(hdr
, buf
));
3098 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3099 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
3100 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3103 * Start sharing the data buffer. We transfer the
3104 * refcount ownership to the hdr since it always owns
3105 * the refcount whenever an arc_buf_t is shared.
3107 zfs_refcount_transfer_ownership_many(&hdr
->b_l1hdr
.b_state
->arcs_size
,
3108 arc_hdr_size(hdr
), buf
, hdr
);
3109 hdr
->b_l1hdr
.b_pabd
= abd_get_from_buf(buf
->b_data
, arc_buf_size(buf
));
3110 abd_take_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
,
3111 HDR_ISTYPE_METADATA(hdr
));
3112 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3113 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
3116 * Since we've transferred ownership to the hdr we need
3117 * to increment its compressed and uncompressed kstats and
3118 * decrement the overhead size.
3120 ARCSTAT_INCR(arcstat_compressed_size
, arc_hdr_size(hdr
));
3121 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3122 ARCSTAT_INCR(arcstat_overhead_size
, -arc_buf_size(buf
));
3126 arc_unshare_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3128 ASSERT(arc_buf_is_shared(buf
));
3129 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3130 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3133 * We are no longer sharing this buffer so we need
3134 * to transfer its ownership to the rightful owner.
3136 zfs_refcount_transfer_ownership_many(&hdr
->b_l1hdr
.b_state
->arcs_size
,
3137 arc_hdr_size(hdr
), hdr
, buf
);
3138 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3139 abd_release_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
);
3140 abd_put(hdr
->b_l1hdr
.b_pabd
);
3141 hdr
->b_l1hdr
.b_pabd
= NULL
;
3142 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
3145 * Since the buffer is no longer shared between
3146 * the arc buf and the hdr, count it as overhead.
3148 ARCSTAT_INCR(arcstat_compressed_size
, -arc_hdr_size(hdr
));
3149 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3150 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
3154 * Remove an arc_buf_t from the hdr's buf list and return the last
3155 * arc_buf_t on the list. If no buffers remain on the list then return
3159 arc_buf_remove(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3161 ASSERT(HDR_HAS_L1HDR(hdr
));
3162 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3164 arc_buf_t
**bufp
= &hdr
->b_l1hdr
.b_buf
;
3165 arc_buf_t
*lastbuf
= NULL
;
3168 * Remove the buf from the hdr list and locate the last
3169 * remaining buffer on the list.
3171 while (*bufp
!= NULL
) {
3173 *bufp
= buf
->b_next
;
3176 * If we've removed a buffer in the middle of
3177 * the list then update the lastbuf and update
3180 if (*bufp
!= NULL
) {
3182 bufp
= &(*bufp
)->b_next
;
3186 ASSERT3P(lastbuf
, !=, buf
);
3187 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, lastbuf
!= NULL
);
3188 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, hdr
->b_l1hdr
.b_buf
!= NULL
);
3189 IMPLY(lastbuf
!= NULL
, ARC_BUF_LAST(lastbuf
));
3195 * Free up buf->b_data and pull the arc_buf_t off of the the arc_buf_hdr_t's
3199 arc_buf_destroy_impl(arc_buf_t
*buf
)
3201 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3204 * Free up the data associated with the buf but only if we're not
3205 * sharing this with the hdr. If we are sharing it with the hdr, the
3206 * hdr is responsible for doing the free.
3208 if (buf
->b_data
!= NULL
) {
3210 * We're about to change the hdr's b_flags. We must either
3211 * hold the hash_lock or be undiscoverable.
3213 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3215 arc_cksum_verify(buf
);
3216 arc_buf_unwatch(buf
);
3218 if (arc_buf_is_shared(buf
)) {
3219 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3221 uint64_t size
= arc_buf_size(buf
);
3222 arc_free_data_buf(hdr
, buf
->b_data
, size
, buf
);
3223 ARCSTAT_INCR(arcstat_overhead_size
, -size
);
3227 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3228 hdr
->b_l1hdr
.b_bufcnt
-= 1;
3230 if (ARC_BUF_ENCRYPTED(buf
)) {
3231 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
3234 * If we have no more encrypted buffers and we've
3235 * already gotten a copy of the decrypted data we can
3236 * free b_rabd to save some space.
3238 if (hdr
->b_crypt_hdr
.b_ebufcnt
== 0 &&
3239 HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
!= NULL
&&
3240 !HDR_IO_IN_PROGRESS(hdr
)) {
3241 arc_hdr_free_abd(hdr
, B_TRUE
);
3246 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
3248 if (ARC_BUF_SHARED(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
3250 * If the current arc_buf_t is sharing its data buffer with the
3251 * hdr, then reassign the hdr's b_pabd to share it with the new
3252 * buffer at the end of the list. The shared buffer is always
3253 * the last one on the hdr's buffer list.
3255 * There is an equivalent case for compressed bufs, but since
3256 * they aren't guaranteed to be the last buf in the list and
3257 * that is an exceedingly rare case, we just allow that space be
3258 * wasted temporarily. We must also be careful not to share
3259 * encrypted buffers, since they cannot be shared.
3261 if (lastbuf
!= NULL
&& !ARC_BUF_ENCRYPTED(lastbuf
)) {
3262 /* Only one buf can be shared at once */
3263 VERIFY(!arc_buf_is_shared(lastbuf
));
3264 /* hdr is uncompressed so can't have compressed buf */
3265 VERIFY(!ARC_BUF_COMPRESSED(lastbuf
));
3267 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3268 arc_hdr_free_abd(hdr
, B_FALSE
);
3271 * We must setup a new shared block between the
3272 * last buffer and the hdr. The data would have
3273 * been allocated by the arc buf so we need to transfer
3274 * ownership to the hdr since it's now being shared.
3276 arc_share_buf(hdr
, lastbuf
);
3278 } else if (HDR_SHARED_DATA(hdr
)) {
3280 * Uncompressed shared buffers are always at the end
3281 * of the list. Compressed buffers don't have the
3282 * same requirements. This makes it hard to
3283 * simply assert that the lastbuf is shared so
3284 * we rely on the hdr's compression flags to determine
3285 * if we have a compressed, shared buffer.
3287 ASSERT3P(lastbuf
, !=, NULL
);
3288 ASSERT(arc_buf_is_shared(lastbuf
) ||
3289 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
3293 * Free the checksum if we're removing the last uncompressed buf from
3296 if (!arc_hdr_has_uncompressed_buf(hdr
)) {
3297 arc_cksum_free(hdr
);
3300 /* clean up the buf */
3302 kmem_cache_free(buf_cache
, buf
);
3306 arc_hdr_alloc_abd(arc_buf_hdr_t
*hdr
, boolean_t alloc_rdata
)
3310 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
3311 ASSERT(HDR_HAS_L1HDR(hdr
));
3312 ASSERT(!HDR_SHARED_DATA(hdr
) || alloc_rdata
);
3313 IMPLY(alloc_rdata
, HDR_PROTECTED(hdr
));
3316 size
= HDR_GET_PSIZE(hdr
);
3317 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, ==, NULL
);
3318 hdr
->b_crypt_hdr
.b_rabd
= arc_get_data_abd(hdr
, size
, hdr
);
3319 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, !=, NULL
);
3320 ARCSTAT_INCR(arcstat_raw_size
, size
);
3322 size
= arc_hdr_size(hdr
);
3323 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3324 hdr
->b_l1hdr
.b_pabd
= arc_get_data_abd(hdr
, size
, hdr
);
3325 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3328 ARCSTAT_INCR(arcstat_compressed_size
, size
);
3329 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3333 arc_hdr_free_abd(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
3335 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
3337 ASSERT(HDR_HAS_L1HDR(hdr
));
3338 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
3339 IMPLY(free_rdata
, HDR_HAS_RABD(hdr
));
3342 * If the hdr is currently being written to the l2arc then
3343 * we defer freeing the data by adding it to the l2arc_free_on_write
3344 * list. The l2arc will free the data once it's finished
3345 * writing it to the l2arc device.
3347 if (HDR_L2_WRITING(hdr
)) {
3348 arc_hdr_free_on_write(hdr
, free_rdata
);
3349 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
3350 } else if (free_rdata
) {
3351 arc_free_data_abd(hdr
, hdr
->b_crypt_hdr
.b_rabd
, size
, hdr
);
3353 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
, size
, hdr
);
3357 hdr
->b_crypt_hdr
.b_rabd
= NULL
;
3358 ARCSTAT_INCR(arcstat_raw_size
, -size
);
3360 hdr
->b_l1hdr
.b_pabd
= NULL
;
3363 if (hdr
->b_l1hdr
.b_pabd
== NULL
&& !HDR_HAS_RABD(hdr
))
3364 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
3366 ARCSTAT_INCR(arcstat_compressed_size
, -size
);
3367 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3370 static arc_buf_hdr_t
*
3371 arc_hdr_alloc(uint64_t spa
, int32_t psize
, int32_t lsize
,
3372 boolean_t
protected, enum zio_compress compression_type
,
3373 arc_buf_contents_t type
, boolean_t alloc_rdata
)
3377 VERIFY(type
== ARC_BUFC_DATA
|| type
== ARC_BUFC_METADATA
);
3379 hdr
= kmem_cache_alloc(hdr_full_crypt_cache
, KM_PUSHPAGE
);
3381 hdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
3384 ASSERT(HDR_EMPTY(hdr
));
3385 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3386 HDR_SET_PSIZE(hdr
, psize
);
3387 HDR_SET_LSIZE(hdr
, lsize
);
3391 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L1HDR
);
3392 arc_hdr_set_compress(hdr
, compression_type
);
3394 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
3396 hdr
->b_l1hdr
.b_state
= arc_anon
;
3397 hdr
->b_l1hdr
.b_arc_access
= 0;
3398 hdr
->b_l1hdr
.b_bufcnt
= 0;
3399 hdr
->b_l1hdr
.b_buf
= NULL
;
3402 * Allocate the hdr's buffer. This will contain either
3403 * the compressed or uncompressed data depending on the block
3404 * it references and compressed arc enablement.
3406 arc_hdr_alloc_abd(hdr
, alloc_rdata
);
3407 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3413 * Transition between the two allocation states for the arc_buf_hdr struct.
3414 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
3415 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
3416 * version is used when a cache buffer is only in the L2ARC in order to reduce
3419 static arc_buf_hdr_t
*
3420 arc_hdr_realloc(arc_buf_hdr_t
*hdr
, kmem_cache_t
*old
, kmem_cache_t
*new)
3422 ASSERT(HDR_HAS_L2HDR(hdr
));
3424 arc_buf_hdr_t
*nhdr
;
3425 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3427 ASSERT((old
== hdr_full_cache
&& new == hdr_l2only_cache
) ||
3428 (old
== hdr_l2only_cache
&& new == hdr_full_cache
));
3431 * if the caller wanted a new full header and the header is to be
3432 * encrypted we will actually allocate the header from the full crypt
3433 * cache instead. The same applies to freeing from the old cache.
3435 if (HDR_PROTECTED(hdr
) && new == hdr_full_cache
)
3436 new = hdr_full_crypt_cache
;
3437 if (HDR_PROTECTED(hdr
) && old
== hdr_full_cache
)
3438 old
= hdr_full_crypt_cache
;
3440 nhdr
= kmem_cache_alloc(new, KM_PUSHPAGE
);
3442 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
3443 buf_hash_remove(hdr
);
3445 bcopy(hdr
, nhdr
, HDR_L2ONLY_SIZE
);
3447 if (new == hdr_full_cache
|| new == hdr_full_crypt_cache
) {
3448 arc_hdr_set_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3450 * arc_access and arc_change_state need to be aware that a
3451 * header has just come out of L2ARC, so we set its state to
3452 * l2c_only even though it's about to change.
3454 nhdr
->b_l1hdr
.b_state
= arc_l2c_only
;
3456 /* Verify previous threads set to NULL before freeing */
3457 ASSERT3P(nhdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3458 ASSERT(!HDR_HAS_RABD(hdr
));
3460 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3461 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
3462 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3465 * If we've reached here, We must have been called from
3466 * arc_evict_hdr(), as such we should have already been
3467 * removed from any ghost list we were previously on
3468 * (which protects us from racing with arc_evict_state),
3469 * thus no locking is needed during this check.
3471 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3474 * A buffer must not be moved into the arc_l2c_only
3475 * state if it's not finished being written out to the
3476 * l2arc device. Otherwise, the b_l1hdr.b_pabd field
3477 * might try to be accessed, even though it was removed.
3479 VERIFY(!HDR_L2_WRITING(hdr
));
3480 VERIFY3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3481 ASSERT(!HDR_HAS_RABD(hdr
));
3483 arc_hdr_clear_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3486 * The header has been reallocated so we need to re-insert it into any
3489 (void) buf_hash_insert(nhdr
, NULL
);
3491 ASSERT(list_link_active(&hdr
->b_l2hdr
.b_l2node
));
3493 mutex_enter(&dev
->l2ad_mtx
);
3496 * We must place the realloc'ed header back into the list at
3497 * the same spot. Otherwise, if it's placed earlier in the list,
3498 * l2arc_write_buffers() could find it during the function's
3499 * write phase, and try to write it out to the l2arc.
3501 list_insert_after(&dev
->l2ad_buflist
, hdr
, nhdr
);
3502 list_remove(&dev
->l2ad_buflist
, hdr
);
3504 mutex_exit(&dev
->l2ad_mtx
);
3507 * Since we're using the pointer address as the tag when
3508 * incrementing and decrementing the l2ad_alloc refcount, we
3509 * must remove the old pointer (that we're about to destroy) and
3510 * add the new pointer to the refcount. Otherwise we'd remove
3511 * the wrong pointer address when calling arc_hdr_destroy() later.
3514 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
,
3515 arc_hdr_size(hdr
), hdr
);
3516 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
3517 arc_hdr_size(nhdr
), nhdr
);
3519 buf_discard_identity(hdr
);
3520 kmem_cache_free(old
, hdr
);
3526 * This function allows an L1 header to be reallocated as a crypt
3527 * header and vice versa. If we are going to a crypt header, the
3528 * new fields will be zeroed out.
3530 static arc_buf_hdr_t
*
3531 arc_hdr_realloc_crypt(arc_buf_hdr_t
*hdr
, boolean_t need_crypt
)
3533 arc_buf_hdr_t
*nhdr
;
3535 kmem_cache_t
*ncache
, *ocache
;
3536 unsigned nsize
, osize
;
3539 * This function requires that hdr is in the arc_anon state.
3540 * Therefore it won't have any L2ARC data for us to worry
3543 ASSERT(HDR_HAS_L1HDR(hdr
));
3544 ASSERT(!HDR_HAS_L2HDR(hdr
));
3545 ASSERT3U(!!HDR_PROTECTED(hdr
), !=, need_crypt
);
3546 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3547 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3548 ASSERT(!list_link_active(&hdr
->b_l2hdr
.b_l2node
));
3549 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3552 ncache
= hdr_full_crypt_cache
;
3553 nsize
= sizeof (hdr
->b_crypt_hdr
);
3554 ocache
= hdr_full_cache
;
3555 osize
= HDR_FULL_SIZE
;
3557 ncache
= hdr_full_cache
;
3558 nsize
= HDR_FULL_SIZE
;
3559 ocache
= hdr_full_crypt_cache
;
3560 osize
= sizeof (hdr
->b_crypt_hdr
);
3563 nhdr
= kmem_cache_alloc(ncache
, KM_PUSHPAGE
);
3566 * Copy all members that aren't locks or condvars to the new header.
3567 * No lists are pointing to us (as we asserted above), so we don't
3568 * need to worry about the list nodes.
3570 nhdr
->b_dva
= hdr
->b_dva
;
3571 nhdr
->b_birth
= hdr
->b_birth
;
3572 nhdr
->b_type
= hdr
->b_type
;
3573 nhdr
->b_flags
= hdr
->b_flags
;
3574 nhdr
->b_psize
= hdr
->b_psize
;
3575 nhdr
->b_lsize
= hdr
->b_lsize
;
3576 nhdr
->b_spa
= hdr
->b_spa
;
3577 nhdr
->b_l1hdr
.b_freeze_cksum
= hdr
->b_l1hdr
.b_freeze_cksum
;
3578 nhdr
->b_l1hdr
.b_bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
3579 nhdr
->b_l1hdr
.b_byteswap
= hdr
->b_l1hdr
.b_byteswap
;
3580 nhdr
->b_l1hdr
.b_state
= hdr
->b_l1hdr
.b_state
;
3581 nhdr
->b_l1hdr
.b_arc_access
= hdr
->b_l1hdr
.b_arc_access
;
3582 nhdr
->b_l1hdr
.b_mru_hits
= hdr
->b_l1hdr
.b_mru_hits
;
3583 nhdr
->b_l1hdr
.b_mru_ghost_hits
= hdr
->b_l1hdr
.b_mru_ghost_hits
;
3584 nhdr
->b_l1hdr
.b_mfu_hits
= hdr
->b_l1hdr
.b_mfu_hits
;
3585 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= hdr
->b_l1hdr
.b_mfu_ghost_hits
;
3586 nhdr
->b_l1hdr
.b_l2_hits
= hdr
->b_l1hdr
.b_l2_hits
;
3587 nhdr
->b_l1hdr
.b_acb
= hdr
->b_l1hdr
.b_acb
;
3588 nhdr
->b_l1hdr
.b_pabd
= hdr
->b_l1hdr
.b_pabd
;
3591 * This zfs_refcount_add() exists only to ensure that the individual
3592 * arc buffers always point to a header that is referenced, avoiding
3593 * a small race condition that could trigger ASSERTs.
3595 (void) zfs_refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, FTAG
);
3596 nhdr
->b_l1hdr
.b_buf
= hdr
->b_l1hdr
.b_buf
;
3597 for (buf
= nhdr
->b_l1hdr
.b_buf
; buf
!= NULL
; buf
= buf
->b_next
) {
3598 mutex_enter(&buf
->b_evict_lock
);
3600 mutex_exit(&buf
->b_evict_lock
);
3603 zfs_refcount_transfer(&nhdr
->b_l1hdr
.b_refcnt
, &hdr
->b_l1hdr
.b_refcnt
);
3604 (void) zfs_refcount_remove(&nhdr
->b_l1hdr
.b_refcnt
, FTAG
);
3605 ASSERT0(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3608 arc_hdr_set_flags(nhdr
, ARC_FLAG_PROTECTED
);
3610 arc_hdr_clear_flags(nhdr
, ARC_FLAG_PROTECTED
);
3613 /* unset all members of the original hdr */
3614 bzero(&hdr
->b_dva
, sizeof (dva_t
));
3616 hdr
->b_type
= ARC_BUFC_INVALID
;
3621 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
3622 hdr
->b_l1hdr
.b_buf
= NULL
;
3623 hdr
->b_l1hdr
.b_bufcnt
= 0;
3624 hdr
->b_l1hdr
.b_byteswap
= 0;
3625 hdr
->b_l1hdr
.b_state
= NULL
;
3626 hdr
->b_l1hdr
.b_arc_access
= 0;
3627 hdr
->b_l1hdr
.b_mru_hits
= 0;
3628 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
3629 hdr
->b_l1hdr
.b_mfu_hits
= 0;
3630 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
3631 hdr
->b_l1hdr
.b_l2_hits
= 0;
3632 hdr
->b_l1hdr
.b_acb
= NULL
;
3633 hdr
->b_l1hdr
.b_pabd
= NULL
;
3635 if (ocache
== hdr_full_crypt_cache
) {
3636 ASSERT(!HDR_HAS_RABD(hdr
));
3637 hdr
->b_crypt_hdr
.b_ot
= DMU_OT_NONE
;
3638 hdr
->b_crypt_hdr
.b_ebufcnt
= 0;
3639 hdr
->b_crypt_hdr
.b_dsobj
= 0;
3640 bzero(hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3641 bzero(hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3642 bzero(hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3645 buf_discard_identity(hdr
);
3646 kmem_cache_free(ocache
, hdr
);
3652 * This function is used by the send / receive code to convert a newly
3653 * allocated arc_buf_t to one that is suitable for a raw encrypted write. It
3654 * is also used to allow the root objset block to be uupdated without altering
3655 * its embedded MACs. Both block types will always be uncompressed so we do not
3656 * have to worry about compression type or psize.
3659 arc_convert_to_raw(arc_buf_t
*buf
, uint64_t dsobj
, boolean_t byteorder
,
3660 dmu_object_type_t ot
, const uint8_t *salt
, const uint8_t *iv
,
3663 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3665 ASSERT(ot
== DMU_OT_DNODE
|| ot
== DMU_OT_OBJSET
);
3666 ASSERT(HDR_HAS_L1HDR(hdr
));
3667 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3669 buf
->b_flags
|= (ARC_BUF_FLAG_COMPRESSED
| ARC_BUF_FLAG_ENCRYPTED
);
3670 if (!HDR_PROTECTED(hdr
))
3671 hdr
= arc_hdr_realloc_crypt(hdr
, B_TRUE
);
3672 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3673 hdr
->b_crypt_hdr
.b_ot
= ot
;
3674 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3675 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3676 if (!arc_hdr_has_uncompressed_buf(hdr
))
3677 arc_cksum_free(hdr
);
3680 bcopy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3682 bcopy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3684 bcopy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3688 * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller.
3689 * The buf is returned thawed since we expect the consumer to modify it.
3692 arc_alloc_buf(spa_t
*spa
, void *tag
, arc_buf_contents_t type
, int32_t size
)
3694 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), size
, size
,
3695 B_FALSE
, ZIO_COMPRESS_OFF
, type
, B_FALSE
);
3697 arc_buf_t
*buf
= NULL
;
3698 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
, B_FALSE
,
3699 B_FALSE
, B_FALSE
, &buf
));
3706 * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this
3707 * for bufs containing metadata.
3710 arc_alloc_compressed_buf(spa_t
*spa
, void *tag
, uint64_t psize
, uint64_t lsize
,
3711 enum zio_compress compression_type
)
3713 ASSERT3U(lsize
, >, 0);
3714 ASSERT3U(lsize
, >=, psize
);
3715 ASSERT3U(compression_type
, >, ZIO_COMPRESS_OFF
);
3716 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3718 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
3719 B_FALSE
, compression_type
, ARC_BUFC_DATA
, B_FALSE
);
3721 arc_buf_t
*buf
= NULL
;
3722 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
,
3723 B_TRUE
, B_FALSE
, B_FALSE
, &buf
));
3725 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3727 if (!arc_buf_is_shared(buf
)) {
3729 * To ensure that the hdr has the correct data in it if we call
3730 * arc_untransform() on this buf before it's been written to
3731 * disk, it's easiest if we just set up sharing between the
3734 ASSERT(!abd_is_linear(hdr
->b_l1hdr
.b_pabd
));
3735 arc_hdr_free_abd(hdr
, B_FALSE
);
3736 arc_share_buf(hdr
, buf
);
3743 arc_alloc_raw_buf(spa_t
*spa
, void *tag
, uint64_t dsobj
, boolean_t byteorder
,
3744 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
3745 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
3746 enum zio_compress compression_type
)
3750 arc_buf_contents_t type
= DMU_OT_IS_METADATA(ot
) ?
3751 ARC_BUFC_METADATA
: ARC_BUFC_DATA
;
3753 ASSERT3U(lsize
, >, 0);
3754 ASSERT3U(lsize
, >=, psize
);
3755 ASSERT3U(compression_type
, >=, ZIO_COMPRESS_OFF
);
3756 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3758 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
, B_TRUE
,
3759 compression_type
, type
, B_TRUE
);
3761 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3762 hdr
->b_crypt_hdr
.b_ot
= ot
;
3763 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3764 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3765 bcopy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3766 bcopy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3767 bcopy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3770 * This buffer will be considered encrypted even if the ot is not an
3771 * encrypted type. It will become authenticated instead in
3772 * arc_write_ready().
3775 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_TRUE
, B_TRUE
,
3776 B_FALSE
, B_FALSE
, &buf
));
3778 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3784 arc_hdr_l2hdr_destroy(arc_buf_hdr_t
*hdr
)
3786 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
3787 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
3788 uint64_t psize
= HDR_GET_PSIZE(hdr
);
3789 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
3791 ASSERT(MUTEX_HELD(&dev
->l2ad_mtx
));
3792 ASSERT(HDR_HAS_L2HDR(hdr
));
3794 list_remove(&dev
->l2ad_buflist
, hdr
);
3796 ARCSTAT_INCR(arcstat_l2_psize
, -psize
);
3797 ARCSTAT_INCR(arcstat_l2_lsize
, -HDR_GET_LSIZE(hdr
));
3799 vdev_space_update(dev
->l2ad_vdev
, -asize
, 0, 0);
3801 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
),
3803 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
3807 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
3809 if (HDR_HAS_L1HDR(hdr
)) {
3810 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
||
3811 hdr
->b_l1hdr
.b_bufcnt
> 0);
3812 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3813 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3815 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3816 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
3818 if (HDR_HAS_L2HDR(hdr
)) {
3819 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3820 boolean_t buflist_held
= MUTEX_HELD(&dev
->l2ad_mtx
);
3823 mutex_enter(&dev
->l2ad_mtx
);
3826 * Even though we checked this conditional above, we
3827 * need to check this again now that we have the
3828 * l2ad_mtx. This is because we could be racing with
3829 * another thread calling l2arc_evict() which might have
3830 * destroyed this header's L2 portion as we were waiting
3831 * to acquire the l2ad_mtx. If that happens, we don't
3832 * want to re-destroy the header's L2 portion.
3834 if (HDR_HAS_L2HDR(hdr
))
3835 arc_hdr_l2hdr_destroy(hdr
);
3838 mutex_exit(&dev
->l2ad_mtx
);
3842 * The header's identify can only be safely discarded once it is no
3843 * longer discoverable. This requires removing it from the hash table
3844 * and the l2arc header list. After this point the hash lock can not
3845 * be used to protect the header.
3847 if (!HDR_EMPTY(hdr
))
3848 buf_discard_identity(hdr
);
3850 if (HDR_HAS_L1HDR(hdr
)) {
3851 arc_cksum_free(hdr
);
3853 while (hdr
->b_l1hdr
.b_buf
!= NULL
)
3854 arc_buf_destroy_impl(hdr
->b_l1hdr
.b_buf
);
3856 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
3857 arc_hdr_free_abd(hdr
, B_FALSE
);
3859 if (HDR_HAS_RABD(hdr
))
3860 arc_hdr_free_abd(hdr
, B_TRUE
);
3863 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3864 if (HDR_HAS_L1HDR(hdr
)) {
3865 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3866 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
3868 if (!HDR_PROTECTED(hdr
)) {
3869 kmem_cache_free(hdr_full_cache
, hdr
);
3871 kmem_cache_free(hdr_full_crypt_cache
, hdr
);
3874 kmem_cache_free(hdr_l2only_cache
, hdr
);
3879 arc_buf_destroy(arc_buf_t
*buf
, void* tag
)
3881 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3883 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
3884 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
3885 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3886 VERIFY0(remove_reference(hdr
, NULL
, tag
));
3887 arc_hdr_destroy(hdr
);
3891 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
3892 mutex_enter(hash_lock
);
3894 ASSERT3P(hdr
, ==, buf
->b_hdr
);
3895 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3896 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3897 ASSERT3P(hdr
->b_l1hdr
.b_state
, !=, arc_anon
);
3898 ASSERT3P(buf
->b_data
, !=, NULL
);
3900 (void) remove_reference(hdr
, hash_lock
, tag
);
3901 arc_buf_destroy_impl(buf
);
3902 mutex_exit(hash_lock
);
3906 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
3907 * state of the header is dependent on its state prior to entering this
3908 * function. The following transitions are possible:
3910 * - arc_mru -> arc_mru_ghost
3911 * - arc_mfu -> arc_mfu_ghost
3912 * - arc_mru_ghost -> arc_l2c_only
3913 * - arc_mru_ghost -> deleted
3914 * - arc_mfu_ghost -> arc_l2c_only
3915 * - arc_mfu_ghost -> deleted
3918 arc_evict_hdr(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
3920 arc_state_t
*evicted_state
, *state
;
3921 int64_t bytes_evicted
= 0;
3922 int min_lifetime
= HDR_PRESCIENT_PREFETCH(hdr
) ?
3923 arc_min_prescient_prefetch_ms
: arc_min_prefetch_ms
;
3925 ASSERT(MUTEX_HELD(hash_lock
));
3926 ASSERT(HDR_HAS_L1HDR(hdr
));
3928 state
= hdr
->b_l1hdr
.b_state
;
3929 if (GHOST_STATE(state
)) {
3930 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3931 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3934 * l2arc_write_buffers() relies on a header's L1 portion
3935 * (i.e. its b_pabd field) during it's write phase.
3936 * Thus, we cannot push a header onto the arc_l2c_only
3937 * state (removing its L1 piece) until the header is
3938 * done being written to the l2arc.
3940 if (HDR_HAS_L2HDR(hdr
) && HDR_L2_WRITING(hdr
)) {
3941 ARCSTAT_BUMP(arcstat_evict_l2_skip
);
3942 return (bytes_evicted
);
3945 ARCSTAT_BUMP(arcstat_deleted
);
3946 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3948 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, hdr
);
3950 if (HDR_HAS_L2HDR(hdr
)) {
3951 ASSERT(hdr
->b_l1hdr
.b_pabd
== NULL
);
3952 ASSERT(!HDR_HAS_RABD(hdr
));
3954 * This buffer is cached on the 2nd Level ARC;
3955 * don't destroy the header.
3957 arc_change_state(arc_l2c_only
, hdr
, hash_lock
);
3959 * dropping from L1+L2 cached to L2-only,
3960 * realloc to remove the L1 header.
3962 hdr
= arc_hdr_realloc(hdr
, hdr_full_cache
,
3965 arc_change_state(arc_anon
, hdr
, hash_lock
);
3966 arc_hdr_destroy(hdr
);
3968 return (bytes_evicted
);
3971 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
3972 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
3974 /* prefetch buffers have a minimum lifespan */
3975 if (HDR_IO_IN_PROGRESS(hdr
) ||
3976 ((hdr
->b_flags
& (ARC_FLAG_PREFETCH
| ARC_FLAG_INDIRECT
)) &&
3977 ddi_get_lbolt() - hdr
->b_l1hdr
.b_arc_access
<
3978 MSEC_TO_TICK(min_lifetime
))) {
3979 ARCSTAT_BUMP(arcstat_evict_skip
);
3980 return (bytes_evicted
);
3983 ASSERT0(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3984 while (hdr
->b_l1hdr
.b_buf
) {
3985 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
3986 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
3987 ARCSTAT_BUMP(arcstat_mutex_miss
);
3990 if (buf
->b_data
!= NULL
)
3991 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3992 mutex_exit(&buf
->b_evict_lock
);
3993 arc_buf_destroy_impl(buf
);
3996 if (HDR_HAS_L2HDR(hdr
)) {
3997 ARCSTAT_INCR(arcstat_evict_l2_cached
, HDR_GET_LSIZE(hdr
));
3999 if (l2arc_write_eligible(hdr
->b_spa
, hdr
)) {
4000 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
4001 HDR_GET_LSIZE(hdr
));
4003 ARCSTAT_INCR(arcstat_evict_l2_ineligible
,
4004 HDR_GET_LSIZE(hdr
));
4008 if (hdr
->b_l1hdr
.b_bufcnt
== 0) {
4009 arc_cksum_free(hdr
);
4011 bytes_evicted
+= arc_hdr_size(hdr
);
4014 * If this hdr is being evicted and has a compressed
4015 * buffer then we discard it here before we change states.
4016 * This ensures that the accounting is updated correctly
4017 * in arc_free_data_impl().
4019 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
4020 arc_hdr_free_abd(hdr
, B_FALSE
);
4022 if (HDR_HAS_RABD(hdr
))
4023 arc_hdr_free_abd(hdr
, B_TRUE
);
4025 arc_change_state(evicted_state
, hdr
, hash_lock
);
4026 ASSERT(HDR_IN_HASH_TABLE(hdr
));
4027 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
4028 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, hdr
);
4031 return (bytes_evicted
);
4035 arc_evict_state_impl(multilist_t
*ml
, int idx
, arc_buf_hdr_t
*marker
,
4036 uint64_t spa
, int64_t bytes
)
4038 multilist_sublist_t
*mls
;
4039 uint64_t bytes_evicted
= 0;
4041 kmutex_t
*hash_lock
;
4042 int evict_count
= 0;
4044 ASSERT3P(marker
, !=, NULL
);
4045 IMPLY(bytes
< 0, bytes
== ARC_EVICT_ALL
);
4047 mls
= multilist_sublist_lock(ml
, idx
);
4049 for (hdr
= multilist_sublist_prev(mls
, marker
); hdr
!= NULL
;
4050 hdr
= multilist_sublist_prev(mls
, marker
)) {
4051 if ((bytes
!= ARC_EVICT_ALL
&& bytes_evicted
>= bytes
) ||
4052 (evict_count
>= zfs_arc_evict_batch_limit
))
4056 * To keep our iteration location, move the marker
4057 * forward. Since we're not holding hdr's hash lock, we
4058 * must be very careful and not remove 'hdr' from the
4059 * sublist. Otherwise, other consumers might mistake the
4060 * 'hdr' as not being on a sublist when they call the
4061 * multilist_link_active() function (they all rely on
4062 * the hash lock protecting concurrent insertions and
4063 * removals). multilist_sublist_move_forward() was
4064 * specifically implemented to ensure this is the case
4065 * (only 'marker' will be removed and re-inserted).
4067 multilist_sublist_move_forward(mls
, marker
);
4070 * The only case where the b_spa field should ever be
4071 * zero, is the marker headers inserted by
4072 * arc_evict_state(). It's possible for multiple threads
4073 * to be calling arc_evict_state() concurrently (e.g.
4074 * dsl_pool_close() and zio_inject_fault()), so we must
4075 * skip any markers we see from these other threads.
4077 if (hdr
->b_spa
== 0)
4080 /* we're only interested in evicting buffers of a certain spa */
4081 if (spa
!= 0 && hdr
->b_spa
!= spa
) {
4082 ARCSTAT_BUMP(arcstat_evict_skip
);
4086 hash_lock
= HDR_LOCK(hdr
);
4089 * We aren't calling this function from any code path
4090 * that would already be holding a hash lock, so we're
4091 * asserting on this assumption to be defensive in case
4092 * this ever changes. Without this check, it would be
4093 * possible to incorrectly increment arcstat_mutex_miss
4094 * below (e.g. if the code changed such that we called
4095 * this function with a hash lock held).
4097 ASSERT(!MUTEX_HELD(hash_lock
));
4099 if (mutex_tryenter(hash_lock
)) {
4100 uint64_t evicted
= arc_evict_hdr(hdr
, hash_lock
);
4101 mutex_exit(hash_lock
);
4103 bytes_evicted
+= evicted
;
4106 * If evicted is zero, arc_evict_hdr() must have
4107 * decided to skip this header, don't increment
4108 * evict_count in this case.
4114 * If arc_size isn't overflowing, signal any
4115 * threads that might happen to be waiting.
4117 * For each header evicted, we wake up a single
4118 * thread. If we used cv_broadcast, we could
4119 * wake up "too many" threads causing arc_size
4120 * to significantly overflow arc_c; since
4121 * arc_get_data_impl() doesn't check for overflow
4122 * when it's woken up (it doesn't because it's
4123 * possible for the ARC to be overflowing while
4124 * full of un-evictable buffers, and the
4125 * function should proceed in this case).
4127 * If threads are left sleeping, due to not
4128 * using cv_broadcast here, they will be woken
4129 * up via cv_broadcast in arc_adjust_cb() just
4130 * before arc_adjust_zthr sleeps.
4132 mutex_enter(&arc_adjust_lock
);
4133 if (!arc_is_overflowing())
4134 cv_signal(&arc_adjust_waiters_cv
);
4135 mutex_exit(&arc_adjust_lock
);
4137 ARCSTAT_BUMP(arcstat_mutex_miss
);
4141 multilist_sublist_unlock(mls
);
4143 return (bytes_evicted
);
4147 * Evict buffers from the given arc state, until we've removed the
4148 * specified number of bytes. Move the removed buffers to the
4149 * appropriate evict state.
4151 * This function makes a "best effort". It skips over any buffers
4152 * it can't get a hash_lock on, and so, may not catch all candidates.
4153 * It may also return without evicting as much space as requested.
4155 * If bytes is specified using the special value ARC_EVICT_ALL, this
4156 * will evict all available (i.e. unlocked and evictable) buffers from
4157 * the given arc state; which is used by arc_flush().
4160 arc_evict_state(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
4161 arc_buf_contents_t type
)
4163 uint64_t total_evicted
= 0;
4164 multilist_t
*ml
= state
->arcs_list
[type
];
4166 arc_buf_hdr_t
**markers
;
4168 IMPLY(bytes
< 0, bytes
== ARC_EVICT_ALL
);
4170 num_sublists
= multilist_get_num_sublists(ml
);
4173 * If we've tried to evict from each sublist, made some
4174 * progress, but still have not hit the target number of bytes
4175 * to evict, we want to keep trying. The markers allow us to
4176 * pick up where we left off for each individual sublist, rather
4177 * than starting from the tail each time.
4179 markers
= kmem_zalloc(sizeof (*markers
) * num_sublists
, KM_SLEEP
);
4180 for (int i
= 0; i
< num_sublists
; i
++) {
4181 multilist_sublist_t
*mls
;
4183 markers
[i
] = kmem_cache_alloc(hdr_full_cache
, KM_SLEEP
);
4186 * A b_spa of 0 is used to indicate that this header is
4187 * a marker. This fact is used in arc_adjust_type() and
4188 * arc_evict_state_impl().
4190 markers
[i
]->b_spa
= 0;
4192 mls
= multilist_sublist_lock(ml
, i
);
4193 multilist_sublist_insert_tail(mls
, markers
[i
]);
4194 multilist_sublist_unlock(mls
);
4198 * While we haven't hit our target number of bytes to evict, or
4199 * we're evicting all available buffers.
4201 while (total_evicted
< bytes
|| bytes
== ARC_EVICT_ALL
) {
4202 int sublist_idx
= multilist_get_random_index(ml
);
4203 uint64_t scan_evicted
= 0;
4206 * Try to reduce pinned dnodes with a floor of arc_dnode_limit.
4207 * Request that 10% of the LRUs be scanned by the superblock
4210 if (type
== ARC_BUFC_DATA
&& aggsum_compare(&astat_dnode_size
,
4211 arc_dnode_limit
) > 0) {
4212 arc_prune_async((aggsum_upper_bound(&astat_dnode_size
) -
4213 arc_dnode_limit
) / sizeof (dnode_t
) /
4214 zfs_arc_dnode_reduce_percent
);
4218 * Start eviction using a randomly selected sublist,
4219 * this is to try and evenly balance eviction across all
4220 * sublists. Always starting at the same sublist
4221 * (e.g. index 0) would cause evictions to favor certain
4222 * sublists over others.
4224 for (int i
= 0; i
< num_sublists
; i
++) {
4225 uint64_t bytes_remaining
;
4226 uint64_t bytes_evicted
;
4228 if (bytes
== ARC_EVICT_ALL
)
4229 bytes_remaining
= ARC_EVICT_ALL
;
4230 else if (total_evicted
< bytes
)
4231 bytes_remaining
= bytes
- total_evicted
;
4235 bytes_evicted
= arc_evict_state_impl(ml
, sublist_idx
,
4236 markers
[sublist_idx
], spa
, bytes_remaining
);
4238 scan_evicted
+= bytes_evicted
;
4239 total_evicted
+= bytes_evicted
;
4241 /* we've reached the end, wrap to the beginning */
4242 if (++sublist_idx
>= num_sublists
)
4247 * If we didn't evict anything during this scan, we have
4248 * no reason to believe we'll evict more during another
4249 * scan, so break the loop.
4251 if (scan_evicted
== 0) {
4252 /* This isn't possible, let's make that obvious */
4253 ASSERT3S(bytes
, !=, 0);
4256 * When bytes is ARC_EVICT_ALL, the only way to
4257 * break the loop is when scan_evicted is zero.
4258 * In that case, we actually have evicted enough,
4259 * so we don't want to increment the kstat.
4261 if (bytes
!= ARC_EVICT_ALL
) {
4262 ASSERT3S(total_evicted
, <, bytes
);
4263 ARCSTAT_BUMP(arcstat_evict_not_enough
);
4270 for (int i
= 0; i
< num_sublists
; i
++) {
4271 multilist_sublist_t
*mls
= multilist_sublist_lock(ml
, i
);
4272 multilist_sublist_remove(mls
, markers
[i
]);
4273 multilist_sublist_unlock(mls
);
4275 kmem_cache_free(hdr_full_cache
, markers
[i
]);
4277 kmem_free(markers
, sizeof (*markers
) * num_sublists
);
4279 return (total_evicted
);
4283 * Flush all "evictable" data of the given type from the arc state
4284 * specified. This will not evict any "active" buffers (i.e. referenced).
4286 * When 'retry' is set to B_FALSE, the function will make a single pass
4287 * over the state and evict any buffers that it can. Since it doesn't
4288 * continually retry the eviction, it might end up leaving some buffers
4289 * in the ARC due to lock misses.
4291 * When 'retry' is set to B_TRUE, the function will continually retry the
4292 * eviction until *all* evictable buffers have been removed from the
4293 * state. As a result, if concurrent insertions into the state are
4294 * allowed (e.g. if the ARC isn't shutting down), this function might
4295 * wind up in an infinite loop, continually trying to evict buffers.
4298 arc_flush_state(arc_state_t
*state
, uint64_t spa
, arc_buf_contents_t type
,
4301 uint64_t evicted
= 0;
4303 while (zfs_refcount_count(&state
->arcs_esize
[type
]) != 0) {
4304 evicted
+= arc_evict_state(state
, spa
, ARC_EVICT_ALL
, type
);
4314 * Helper function for arc_prune_async() it is responsible for safely
4315 * handling the execution of a registered arc_prune_func_t.
4318 arc_prune_task(void *ptr
)
4320 arc_prune_t
*ap
= (arc_prune_t
*)ptr
;
4321 arc_prune_func_t
*func
= ap
->p_pfunc
;
4324 func(ap
->p_adjust
, ap
->p_private
);
4326 zfs_refcount_remove(&ap
->p_refcnt
, func
);
4330 * Notify registered consumers they must drop holds on a portion of the ARC
4331 * buffered they reference. This provides a mechanism to ensure the ARC can
4332 * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This
4333 * is analogous to dnlc_reduce_cache() but more generic.
4335 * This operation is performed asynchronously so it may be safely called
4336 * in the context of the arc_reclaim_thread(). A reference is taken here
4337 * for each registered arc_prune_t and the arc_prune_task() is responsible
4338 * for releasing it once the registered arc_prune_func_t has completed.
4341 arc_prune_async(int64_t adjust
)
4345 mutex_enter(&arc_prune_mtx
);
4346 for (ap
= list_head(&arc_prune_list
); ap
!= NULL
;
4347 ap
= list_next(&arc_prune_list
, ap
)) {
4349 if (zfs_refcount_count(&ap
->p_refcnt
) >= 2)
4352 zfs_refcount_add(&ap
->p_refcnt
, ap
->p_pfunc
);
4353 ap
->p_adjust
= adjust
;
4354 if (taskq_dispatch(arc_prune_taskq
, arc_prune_task
,
4355 ap
, TQ_SLEEP
) == TASKQID_INVALID
) {
4356 zfs_refcount_remove(&ap
->p_refcnt
, ap
->p_pfunc
);
4359 ARCSTAT_BUMP(arcstat_prune
);
4361 mutex_exit(&arc_prune_mtx
);
4365 * Evict the specified number of bytes from the state specified,
4366 * restricting eviction to the spa and type given. This function
4367 * prevents us from trying to evict more from a state's list than
4368 * is "evictable", and to skip evicting altogether when passed a
4369 * negative value for "bytes". In contrast, arc_evict_state() will
4370 * evict everything it can, when passed a negative value for "bytes".
4373 arc_adjust_impl(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
4374 arc_buf_contents_t type
)
4378 if (bytes
> 0 && zfs_refcount_count(&state
->arcs_esize
[type
]) > 0) {
4379 delta
= MIN(zfs_refcount_count(&state
->arcs_esize
[type
]),
4381 return (arc_evict_state(state
, spa
, delta
, type
));
4388 * The goal of this function is to evict enough meta data buffers from the
4389 * ARC in order to enforce the arc_meta_limit. Achieving this is slightly
4390 * more complicated than it appears because it is common for data buffers
4391 * to have holds on meta data buffers. In addition, dnode meta data buffers
4392 * will be held by the dnodes in the block preventing them from being freed.
4393 * This means we can't simply traverse the ARC and expect to always find
4394 * enough unheld meta data buffer to release.
4396 * Therefore, this function has been updated to make alternating passes
4397 * over the ARC releasing data buffers and then newly unheld meta data
4398 * buffers. This ensures forward progress is maintained and meta_used
4399 * will decrease. Normally this is sufficient, but if required the ARC
4400 * will call the registered prune callbacks causing dentry and inodes to
4401 * be dropped from the VFS cache. This will make dnode meta data buffers
4402 * available for reclaim.
4405 arc_adjust_meta_balanced(uint64_t meta_used
)
4407 int64_t delta
, prune
= 0, adjustmnt
;
4408 uint64_t total_evicted
= 0;
4409 arc_buf_contents_t type
= ARC_BUFC_DATA
;
4410 int restarts
= MAX(zfs_arc_meta_adjust_restarts
, 0);
4414 * This slightly differs than the way we evict from the mru in
4415 * arc_adjust because we don't have a "target" value (i.e. no
4416 * "meta" arc_p). As a result, I think we can completely
4417 * cannibalize the metadata in the MRU before we evict the
4418 * metadata from the MFU. I think we probably need to implement a
4419 * "metadata arc_p" value to do this properly.
4421 adjustmnt
= meta_used
- arc_meta_limit
;
4423 if (adjustmnt
> 0 &&
4424 zfs_refcount_count(&arc_mru
->arcs_esize
[type
]) > 0) {
4425 delta
= MIN(zfs_refcount_count(&arc_mru
->arcs_esize
[type
]),
4427 total_evicted
+= arc_adjust_impl(arc_mru
, 0, delta
, type
);
4432 * We can't afford to recalculate adjustmnt here. If we do,
4433 * new metadata buffers can sneak into the MRU or ANON lists,
4434 * thus penalize the MFU metadata. Although the fudge factor is
4435 * small, it has been empirically shown to be significant for
4436 * certain workloads (e.g. creating many empty directories). As
4437 * such, we use the original calculation for adjustmnt, and
4438 * simply decrement the amount of data evicted from the MRU.
4441 if (adjustmnt
> 0 &&
4442 zfs_refcount_count(&arc_mfu
->arcs_esize
[type
]) > 0) {
4443 delta
= MIN(zfs_refcount_count(&arc_mfu
->arcs_esize
[type
]),
4445 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, delta
, type
);
4448 adjustmnt
= meta_used
- arc_meta_limit
;
4450 if (adjustmnt
> 0 &&
4451 zfs_refcount_count(&arc_mru_ghost
->arcs_esize
[type
]) > 0) {
4452 delta
= MIN(adjustmnt
,
4453 zfs_refcount_count(&arc_mru_ghost
->arcs_esize
[type
]));
4454 total_evicted
+= arc_adjust_impl(arc_mru_ghost
, 0, delta
, type
);
4458 if (adjustmnt
> 0 &&
4459 zfs_refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]) > 0) {
4460 delta
= MIN(adjustmnt
,
4461 zfs_refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]));
4462 total_evicted
+= arc_adjust_impl(arc_mfu_ghost
, 0, delta
, type
);
4466 * If after attempting to make the requested adjustment to the ARC
4467 * the meta limit is still being exceeded then request that the
4468 * higher layers drop some cached objects which have holds on ARC
4469 * meta buffers. Requests to the upper layers will be made with
4470 * increasingly large scan sizes until the ARC is below the limit.
4472 if (meta_used
> arc_meta_limit
) {
4473 if (type
== ARC_BUFC_DATA
) {
4474 type
= ARC_BUFC_METADATA
;
4476 type
= ARC_BUFC_DATA
;
4478 if (zfs_arc_meta_prune
) {
4479 prune
+= zfs_arc_meta_prune
;
4480 arc_prune_async(prune
);
4489 return (total_evicted
);
4493 * Evict metadata buffers from the cache, such that arc_meta_used is
4494 * capped by the arc_meta_limit tunable.
4497 arc_adjust_meta_only(uint64_t meta_used
)
4499 uint64_t total_evicted
= 0;
4503 * If we're over the meta limit, we want to evict enough
4504 * metadata to get back under the meta limit. We don't want to
4505 * evict so much that we drop the MRU below arc_p, though. If
4506 * we're over the meta limit more than we're over arc_p, we
4507 * evict some from the MRU here, and some from the MFU below.
4509 target
= MIN((int64_t)(meta_used
- arc_meta_limit
),
4510 (int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) +
4511 zfs_refcount_count(&arc_mru
->arcs_size
) - arc_p
));
4513 total_evicted
+= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4516 * Similar to the above, we want to evict enough bytes to get us
4517 * below the meta limit, but not so much as to drop us below the
4518 * space allotted to the MFU (which is defined as arc_c - arc_p).
4520 target
= MIN((int64_t)(meta_used
- arc_meta_limit
),
4521 (int64_t)(zfs_refcount_count(&arc_mfu
->arcs_size
) -
4524 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4526 return (total_evicted
);
4530 arc_adjust_meta(uint64_t meta_used
)
4532 if (zfs_arc_meta_strategy
== ARC_STRATEGY_META_ONLY
)
4533 return (arc_adjust_meta_only(meta_used
));
4535 return (arc_adjust_meta_balanced(meta_used
));
4539 * Return the type of the oldest buffer in the given arc state
4541 * This function will select a random sublist of type ARC_BUFC_DATA and
4542 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist
4543 * is compared, and the type which contains the "older" buffer will be
4546 static arc_buf_contents_t
4547 arc_adjust_type(arc_state_t
*state
)
4549 multilist_t
*data_ml
= state
->arcs_list
[ARC_BUFC_DATA
];
4550 multilist_t
*meta_ml
= state
->arcs_list
[ARC_BUFC_METADATA
];
4551 int data_idx
= multilist_get_random_index(data_ml
);
4552 int meta_idx
= multilist_get_random_index(meta_ml
);
4553 multilist_sublist_t
*data_mls
;
4554 multilist_sublist_t
*meta_mls
;
4555 arc_buf_contents_t type
;
4556 arc_buf_hdr_t
*data_hdr
;
4557 arc_buf_hdr_t
*meta_hdr
;
4560 * We keep the sublist lock until we're finished, to prevent
4561 * the headers from being destroyed via arc_evict_state().
4563 data_mls
= multilist_sublist_lock(data_ml
, data_idx
);
4564 meta_mls
= multilist_sublist_lock(meta_ml
, meta_idx
);
4567 * These two loops are to ensure we skip any markers that
4568 * might be at the tail of the lists due to arc_evict_state().
4571 for (data_hdr
= multilist_sublist_tail(data_mls
); data_hdr
!= NULL
;
4572 data_hdr
= multilist_sublist_prev(data_mls
, data_hdr
)) {
4573 if (data_hdr
->b_spa
!= 0)
4577 for (meta_hdr
= multilist_sublist_tail(meta_mls
); meta_hdr
!= NULL
;
4578 meta_hdr
= multilist_sublist_prev(meta_mls
, meta_hdr
)) {
4579 if (meta_hdr
->b_spa
!= 0)
4583 if (data_hdr
== NULL
&& meta_hdr
== NULL
) {
4584 type
= ARC_BUFC_DATA
;
4585 } else if (data_hdr
== NULL
) {
4586 ASSERT3P(meta_hdr
, !=, NULL
);
4587 type
= ARC_BUFC_METADATA
;
4588 } else if (meta_hdr
== NULL
) {
4589 ASSERT3P(data_hdr
, !=, NULL
);
4590 type
= ARC_BUFC_DATA
;
4592 ASSERT3P(data_hdr
, !=, NULL
);
4593 ASSERT3P(meta_hdr
, !=, NULL
);
4595 /* The headers can't be on the sublist without an L1 header */
4596 ASSERT(HDR_HAS_L1HDR(data_hdr
));
4597 ASSERT(HDR_HAS_L1HDR(meta_hdr
));
4599 if (data_hdr
->b_l1hdr
.b_arc_access
<
4600 meta_hdr
->b_l1hdr
.b_arc_access
) {
4601 type
= ARC_BUFC_DATA
;
4603 type
= ARC_BUFC_METADATA
;
4607 multilist_sublist_unlock(meta_mls
);
4608 multilist_sublist_unlock(data_mls
);
4614 * Evict buffers from the cache, such that arc_size is capped by arc_c.
4619 uint64_t total_evicted
= 0;
4622 uint64_t asize
= aggsum_value(&arc_size
);
4623 uint64_t ameta
= aggsum_value(&arc_meta_used
);
4626 * If we're over arc_meta_limit, we want to correct that before
4627 * potentially evicting data buffers below.
4629 total_evicted
+= arc_adjust_meta(ameta
);
4634 * If we're over the target cache size, we want to evict enough
4635 * from the list to get back to our target size. We don't want
4636 * to evict too much from the MRU, such that it drops below
4637 * arc_p. So, if we're over our target cache size more than
4638 * the MRU is over arc_p, we'll evict enough to get back to
4639 * arc_p here, and then evict more from the MFU below.
4641 target
= MIN((int64_t)(asize
- arc_c
),
4642 (int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) +
4643 zfs_refcount_count(&arc_mru
->arcs_size
) + ameta
- arc_p
));
4646 * If we're below arc_meta_min, always prefer to evict data.
4647 * Otherwise, try to satisfy the requested number of bytes to
4648 * evict from the type which contains older buffers; in an
4649 * effort to keep newer buffers in the cache regardless of their
4650 * type. If we cannot satisfy the number of bytes from this
4651 * type, spill over into the next type.
4653 if (arc_adjust_type(arc_mru
) == ARC_BUFC_METADATA
&&
4654 ameta
> arc_meta_min
) {
4655 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4656 total_evicted
+= bytes
;
4659 * If we couldn't evict our target number of bytes from
4660 * metadata, we try to get the rest from data.
4665 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
4667 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
4668 total_evicted
+= bytes
;
4671 * If we couldn't evict our target number of bytes from
4672 * data, we try to get the rest from metadata.
4677 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4681 * Re-sum ARC stats after the first round of evictions.
4683 asize
= aggsum_value(&arc_size
);
4684 ameta
= aggsum_value(&arc_meta_used
);
4690 * Now that we've tried to evict enough from the MRU to get its
4691 * size back to arc_p, if we're still above the target cache
4692 * size, we evict the rest from the MFU.
4694 target
= asize
- arc_c
;
4696 if (arc_adjust_type(arc_mfu
) == ARC_BUFC_METADATA
&&
4697 ameta
> arc_meta_min
) {
4698 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4699 total_evicted
+= bytes
;
4702 * If we couldn't evict our target number of bytes from
4703 * metadata, we try to get the rest from data.
4708 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
4710 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
4711 total_evicted
+= bytes
;
4714 * If we couldn't evict our target number of bytes from
4715 * data, we try to get the rest from data.
4720 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4724 * Adjust ghost lists
4726 * In addition to the above, the ARC also defines target values
4727 * for the ghost lists. The sum of the mru list and mru ghost
4728 * list should never exceed the target size of the cache, and
4729 * the sum of the mru list, mfu list, mru ghost list, and mfu
4730 * ghost list should never exceed twice the target size of the
4731 * cache. The following logic enforces these limits on the ghost
4732 * caches, and evicts from them as needed.
4734 target
= zfs_refcount_count(&arc_mru
->arcs_size
) +
4735 zfs_refcount_count(&arc_mru_ghost
->arcs_size
) - arc_c
;
4737 bytes
= arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_DATA
);
4738 total_evicted
+= bytes
;
4743 arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_METADATA
);
4746 * We assume the sum of the mru list and mfu list is less than
4747 * or equal to arc_c (we enforced this above), which means we
4748 * can use the simpler of the two equations below:
4750 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c
4751 * mru ghost + mfu ghost <= arc_c
4753 target
= zfs_refcount_count(&arc_mru_ghost
->arcs_size
) +
4754 zfs_refcount_count(&arc_mfu_ghost
->arcs_size
) - arc_c
;
4756 bytes
= arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_DATA
);
4757 total_evicted
+= bytes
;
4762 arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_METADATA
);
4764 return (total_evicted
);
4768 arc_flush(spa_t
*spa
, boolean_t retry
)
4773 * If retry is B_TRUE, a spa must not be specified since we have
4774 * no good way to determine if all of a spa's buffers have been
4775 * evicted from an arc state.
4777 ASSERT(!retry
|| spa
== 0);
4780 guid
= spa_load_guid(spa
);
4782 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_DATA
, retry
);
4783 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_METADATA
, retry
);
4785 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_DATA
, retry
);
4786 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_METADATA
, retry
);
4788 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4789 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4791 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4792 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4796 arc_reduce_target_size(int64_t to_free
)
4798 uint64_t asize
= aggsum_value(&arc_size
);
4801 if (c
> to_free
&& c
- to_free
> arc_c_min
) {
4802 arc_c
= c
- to_free
;
4803 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
4805 arc_c
= MAX(asize
, arc_c_min
);
4807 arc_p
= (arc_c
>> 1);
4808 ASSERT(arc_c
>= arc_c_min
);
4809 ASSERT((int64_t)arc_p
>= 0);
4814 if (asize
> arc_c
) {
4815 /* See comment in arc_adjust_cb_check() on why lock+flag */
4816 mutex_enter(&arc_adjust_lock
);
4817 arc_adjust_needed
= B_TRUE
;
4818 mutex_exit(&arc_adjust_lock
);
4819 zthr_wakeup(arc_adjust_zthr
);
4823 * Return maximum amount of memory that we could possibly use. Reduced
4824 * to half of all memory in user space which is primarily used for testing.
4827 arc_all_memory(void)
4830 #ifdef CONFIG_HIGHMEM
4831 return (ptob(zfs_totalram_pages
- totalhigh_pages
));
4833 return (ptob(zfs_totalram_pages
));
4834 #endif /* CONFIG_HIGHMEM */
4836 return (ptob(physmem
) / 2);
4837 #endif /* _KERNEL */
4841 * Return the amount of memory that is considered free. In user space
4842 * which is primarily used for testing we pretend that free memory ranges
4843 * from 0-20% of all memory.
4846 arc_free_memory(void)
4849 #ifdef CONFIG_HIGHMEM
4852 return (ptob(si
.freeram
- si
.freehigh
));
4854 return (ptob(nr_free_pages() +
4855 nr_inactive_file_pages() +
4856 nr_inactive_anon_pages() +
4857 nr_slab_reclaimable_pages()));
4859 #endif /* CONFIG_HIGHMEM */
4861 return (spa_get_random(arc_all_memory() * 20 / 100));
4862 #endif /* _KERNEL */
4865 typedef enum free_memory_reason_t
{
4870 FMR_PAGES_PP_MAXIMUM
,
4873 } free_memory_reason_t
;
4875 int64_t last_free_memory
;
4876 free_memory_reason_t last_free_reason
;
4880 * Additional reserve of pages for pp_reserve.
4882 int64_t arc_pages_pp_reserve
= 64;
4885 * Additional reserve of pages for swapfs.
4887 int64_t arc_swapfs_reserve
= 64;
4888 #endif /* _KERNEL */
4891 * Return the amount of memory that can be consumed before reclaim will be
4892 * needed. Positive if there is sufficient free memory, negative indicates
4893 * the amount of memory that needs to be freed up.
4896 arc_available_memory(void)
4898 int64_t lowest
= INT64_MAX
;
4899 free_memory_reason_t r
= FMR_UNKNOWN
;
4906 pgcnt_t needfree
= btop(arc_need_free
);
4907 pgcnt_t lotsfree
= btop(arc_sys_free
);
4908 pgcnt_t desfree
= 0;
4909 pgcnt_t freemem
= btop(arc_free_memory());
4913 n
= PAGESIZE
* (-needfree
);
4921 * check that we're out of range of the pageout scanner. It starts to
4922 * schedule paging if freemem is less than lotsfree and needfree.
4923 * lotsfree is the high-water mark for pageout, and needfree is the
4924 * number of needed free pages. We add extra pages here to make sure
4925 * the scanner doesn't start up while we're freeing memory.
4927 n
= PAGESIZE
* (freemem
- lotsfree
- needfree
- desfree
);
4935 * check to make sure that swapfs has enough space so that anon
4936 * reservations can still succeed. anon_resvmem() checks that the
4937 * availrmem is greater than swapfs_minfree, and the number of reserved
4938 * swap pages. We also add a bit of extra here just to prevent
4939 * circumstances from getting really dire.
4941 n
= PAGESIZE
* (availrmem
- swapfs_minfree
- swapfs_reserve
-
4942 desfree
- arc_swapfs_reserve
);
4945 r
= FMR_SWAPFS_MINFREE
;
4949 * Check that we have enough availrmem that memory locking (e.g., via
4950 * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum
4951 * stores the number of pages that cannot be locked; when availrmem
4952 * drops below pages_pp_maximum, page locking mechanisms such as
4953 * page_pp_lock() will fail.)
4955 n
= PAGESIZE
* (availrmem
- pages_pp_maximum
-
4956 arc_pages_pp_reserve
);
4959 r
= FMR_PAGES_PP_MAXIMUM
;
4965 * If we're on a 32-bit platform, it's possible that we'll exhaust the
4966 * kernel heap space before we ever run out of available physical
4967 * memory. Most checks of the size of the heap_area compare against
4968 * tune.t_minarmem, which is the minimum available real memory that we
4969 * can have in the system. However, this is generally fixed at 25 pages
4970 * which is so low that it's useless. In this comparison, we seek to
4971 * calculate the total heap-size, and reclaim if more than 3/4ths of the
4972 * heap is allocated. (Or, in the calculation, if less than 1/4th is
4975 n
= vmem_size(heap_arena
, VMEM_FREE
) -
4976 (vmem_size(heap_arena
, VMEM_FREE
| VMEM_ALLOC
) >> 2);
4984 * If zio data pages are being allocated out of a separate heap segment,
4985 * then enforce that the size of available vmem for this arena remains
4986 * above about 1/4th (1/(2^arc_zio_arena_free_shift)) free.
4988 * Note that reducing the arc_zio_arena_free_shift keeps more virtual
4989 * memory (in the zio_arena) free, which can avoid memory
4990 * fragmentation issues.
4992 if (zio_arena
!= NULL
) {
4993 n
= (int64_t)vmem_size(zio_arena
, VMEM_FREE
) -
4994 (vmem_size(zio_arena
, VMEM_ALLOC
) >>
4995 arc_zio_arena_free_shift
);
5002 /* Every 100 calls, free a small amount */
5003 if (spa_get_random(100) == 0)
5005 #endif /* _KERNEL */
5007 last_free_memory
= lowest
;
5008 last_free_reason
= r
;
5014 * Determine if the system is under memory pressure and is asking
5015 * to reclaim memory. A return value of B_TRUE indicates that the system
5016 * is under memory pressure and that the arc should adjust accordingly.
5019 arc_reclaim_needed(void)
5021 return (arc_available_memory() < 0);
5025 arc_kmem_reap_soon(void)
5028 kmem_cache_t
*prev_cache
= NULL
;
5029 kmem_cache_t
*prev_data_cache
= NULL
;
5030 extern kmem_cache_t
*zio_buf_cache
[];
5031 extern kmem_cache_t
*zio_data_buf_cache
[];
5032 extern kmem_cache_t
*range_seg_cache
;
5035 if ((aggsum_compare(&arc_meta_used
, arc_meta_limit
) >= 0) &&
5036 zfs_arc_meta_prune
) {
5038 * We are exceeding our meta-data cache limit.
5039 * Prune some entries to release holds on meta-data.
5041 arc_prune_async(zfs_arc_meta_prune
);
5045 * Reclaim unused memory from all kmem caches.
5051 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
5053 /* reach upper limit of cache size on 32-bit */
5054 if (zio_buf_cache
[i
] == NULL
)
5057 if (zio_buf_cache
[i
] != prev_cache
) {
5058 prev_cache
= zio_buf_cache
[i
];
5059 kmem_cache_reap_now(zio_buf_cache
[i
]);
5061 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
5062 prev_data_cache
= zio_data_buf_cache
[i
];
5063 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
5066 kmem_cache_reap_now(buf_cache
);
5067 kmem_cache_reap_now(hdr_full_cache
);
5068 kmem_cache_reap_now(hdr_l2only_cache
);
5069 kmem_cache_reap_now(range_seg_cache
);
5071 if (zio_arena
!= NULL
) {
5073 * Ask the vmem arena to reclaim unused memory from its
5076 vmem_qcache_reap(zio_arena
);
5082 arc_adjust_cb_check(void *arg
, zthr_t
*zthr
)
5085 * This is necessary so that any changes which may have been made to
5086 * many of the zfs_arc_* module parameters will be propagated to
5087 * their actual internal variable counterparts. Without this,
5088 * changing those module params at runtime would have no effect.
5090 arc_tuning_update();
5093 * This is necessary in order to keep the kstat information
5094 * up to date for tools that display kstat data such as the
5095 * mdb ::arc dcmd and the Linux crash utility. These tools
5096 * typically do not call kstat's update function, but simply
5097 * dump out stats from the most recent update. Without
5098 * this call, these commands may show stale stats for the
5099 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
5100 * with this change, the data might be up to 1 second
5101 * out of date(the arc_adjust_zthr has a maximum sleep
5102 * time of 1 second); but that should suffice. The
5103 * arc_state_t structures can be queried directly if more
5104 * accurate information is needed.
5106 if (arc_ksp
!= NULL
)
5107 arc_ksp
->ks_update(arc_ksp
, KSTAT_READ
);
5110 * We have to rely on arc_get_data_impl() to tell us when to adjust,
5111 * rather than checking if we are overflowing here, so that we are
5112 * sure to not leave arc_get_data_impl() waiting on
5113 * arc_adjust_waiters_cv. If we have become "not overflowing" since
5114 * arc_get_data_impl() checked, we need to wake it up. We could
5115 * broadcast the CV here, but arc_get_data_impl() may have not yet
5116 * gone to sleep. We would need to use a mutex to ensure that this
5117 * function doesn't broadcast until arc_get_data_impl() has gone to
5118 * sleep (e.g. the arc_adjust_lock). However, the lock ordering of
5119 * such a lock would necessarily be incorrect with respect to the
5120 * zthr_lock, which is held before this function is called, and is
5121 * held by arc_get_data_impl() when it calls zthr_wakeup().
5123 return (arc_adjust_needed
);
5127 * Keep arc_size under arc_c by running arc_adjust which evicts data
5132 arc_adjust_cb(void *arg
, zthr_t
*zthr
)
5134 uint64_t evicted
= 0;
5135 fstrans_cookie_t cookie
= spl_fstrans_mark();
5137 /* Evict from cache */
5138 evicted
= arc_adjust();
5141 * If evicted is zero, we couldn't evict anything
5142 * via arc_adjust(). This could be due to hash lock
5143 * collisions, but more likely due to the majority of
5144 * arc buffers being unevictable. Therefore, even if
5145 * arc_size is above arc_c, another pass is unlikely to
5146 * be helpful and could potentially cause us to enter an
5147 * infinite loop. Additionally, zthr_iscancelled() is
5148 * checked here so that if the arc is shutting down, the
5149 * broadcast will wake any remaining arc adjust waiters.
5151 mutex_enter(&arc_adjust_lock
);
5152 arc_adjust_needed
= !zthr_iscancelled(arc_adjust_zthr
) &&
5153 evicted
> 0 && aggsum_compare(&arc_size
, arc_c
) > 0;
5154 if (!arc_adjust_needed
) {
5156 * We're either no longer overflowing, or we
5157 * can't evict anything more, so we should wake
5158 * arc_get_data_impl() sooner.
5160 cv_broadcast(&arc_adjust_waiters_cv
);
5163 mutex_exit(&arc_adjust_lock
);
5164 spl_fstrans_unmark(cookie
);
5169 arc_reap_cb_check(void *arg
, zthr_t
*zthr
)
5171 int64_t free_memory
= arc_available_memory();
5174 * If a kmem reap is already active, don't schedule more. We must
5175 * check for this because kmem_cache_reap_soon() won't actually
5176 * block on the cache being reaped (this is to prevent callers from
5177 * becoming implicitly blocked by a system-wide kmem reap -- which,
5178 * on a system with many, many full magazines, can take minutes).
5180 if (!kmem_cache_reap_active() && free_memory
< 0) {
5182 arc_no_grow
= B_TRUE
;
5185 * Wait at least zfs_grow_retry (default 5) seconds
5186 * before considering growing.
5188 arc_growtime
= gethrtime() + SEC2NSEC(arc_grow_retry
);
5190 } else if (free_memory
< arc_c
>> arc_no_grow_shift
) {
5191 arc_no_grow
= B_TRUE
;
5192 } else if (gethrtime() >= arc_growtime
) {
5193 arc_no_grow
= B_FALSE
;
5200 * Keep enough free memory in the system by reaping the ARC's kmem
5201 * caches. To cause more slabs to be reapable, we may reduce the
5202 * target size of the cache (arc_c), causing the arc_adjust_cb()
5203 * to free more buffers.
5207 arc_reap_cb(void *arg
, zthr_t
*zthr
)
5209 int64_t free_memory
;
5210 fstrans_cookie_t cookie
= spl_fstrans_mark();
5213 * Kick off asynchronous kmem_reap()'s of all our caches.
5215 arc_kmem_reap_soon();
5218 * Wait at least arc_kmem_cache_reap_retry_ms between
5219 * arc_kmem_reap_soon() calls. Without this check it is possible to
5220 * end up in a situation where we spend lots of time reaping
5221 * caches, while we're near arc_c_min. Waiting here also gives the
5222 * subsequent free memory check a chance of finding that the
5223 * asynchronous reap has already freed enough memory, and we don't
5224 * need to call arc_reduce_target_size().
5226 delay((hz
* arc_kmem_cache_reap_retry_ms
+ 999) / 1000);
5229 * Reduce the target size as needed to maintain the amount of free
5230 * memory in the system at a fraction of the arc_size (1/128th by
5231 * default). If oversubscribed (free_memory < 0) then reduce the
5232 * target arc_size by the deficit amount plus the fractional
5233 * amount. If free memory is positive but less then the fractional
5234 * amount, reduce by what is needed to hit the fractional amount.
5236 free_memory
= arc_available_memory();
5239 (arc_c
>> arc_shrink_shift
) - free_memory
;
5242 to_free
= MAX(to_free
, arc_need_free
);
5244 arc_reduce_target_size(to_free
);
5246 spl_fstrans_unmark(cookie
);
5251 * Determine the amount of memory eligible for eviction contained in the
5252 * ARC. All clean data reported by the ghost lists can always be safely
5253 * evicted. Due to arc_c_min, the same does not hold for all clean data
5254 * contained by the regular mru and mfu lists.
5256 * In the case of the regular mru and mfu lists, we need to report as
5257 * much clean data as possible, such that evicting that same reported
5258 * data will not bring arc_size below arc_c_min. Thus, in certain
5259 * circumstances, the total amount of clean data in the mru and mfu
5260 * lists might not actually be evictable.
5262 * The following two distinct cases are accounted for:
5264 * 1. The sum of the amount of dirty data contained by both the mru and
5265 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
5266 * is greater than or equal to arc_c_min.
5267 * (i.e. amount of dirty data >= arc_c_min)
5269 * This is the easy case; all clean data contained by the mru and mfu
5270 * lists is evictable. Evicting all clean data can only drop arc_size
5271 * to the amount of dirty data, which is greater than arc_c_min.
5273 * 2. The sum of the amount of dirty data contained by both the mru and
5274 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
5275 * is less than arc_c_min.
5276 * (i.e. arc_c_min > amount of dirty data)
5278 * 2.1. arc_size is greater than or equal arc_c_min.
5279 * (i.e. arc_size >= arc_c_min > amount of dirty data)
5281 * In this case, not all clean data from the regular mru and mfu
5282 * lists is actually evictable; we must leave enough clean data
5283 * to keep arc_size above arc_c_min. Thus, the maximum amount of
5284 * evictable data from the two lists combined, is exactly the
5285 * difference between arc_size and arc_c_min.
5287 * 2.2. arc_size is less than arc_c_min
5288 * (i.e. arc_c_min > arc_size > amount of dirty data)
5290 * In this case, none of the data contained in the mru and mfu
5291 * lists is evictable, even if it's clean. Since arc_size is
5292 * already below arc_c_min, evicting any more would only
5293 * increase this negative difference.
5296 arc_evictable_memory(void)
5298 int64_t asize
= aggsum_value(&arc_size
);
5299 uint64_t arc_clean
=
5300 zfs_refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]) +
5301 zfs_refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]) +
5302 zfs_refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]) +
5303 zfs_refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
5304 uint64_t arc_dirty
= MAX((int64_t)asize
- (int64_t)arc_clean
, 0);
5307 * Scale reported evictable memory in proportion to page cache, cap
5308 * at specified min/max.
5310 uint64_t min
= (ptob(nr_file_pages()) / 100) * zfs_arc_pc_percent
;
5311 min
= MAX(arc_c_min
, MIN(arc_c_max
, min
));
5313 if (arc_dirty
>= min
)
5316 return (MAX((int64_t)asize
- (int64_t)min
, 0));
5320 * If sc->nr_to_scan is zero, the caller is requesting a query of the
5321 * number of objects which can potentially be freed. If it is nonzero,
5322 * the request is to free that many objects.
5324 * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks
5325 * in struct shrinker and also require the shrinker to return the number
5328 * Older kernels require the shrinker to return the number of freeable
5329 * objects following the freeing of nr_to_free.
5331 static spl_shrinker_t
5332 __arc_shrinker_func(struct shrinker
*shrink
, struct shrink_control
*sc
)
5336 /* The arc is considered warm once reclaim has occurred */
5337 if (unlikely(arc_warm
== B_FALSE
))
5340 /* Return the potential number of reclaimable pages */
5341 pages
= btop((int64_t)arc_evictable_memory());
5342 if (sc
->nr_to_scan
== 0)
5345 /* Not allowed to perform filesystem reclaim */
5346 if (!(sc
->gfp_mask
& __GFP_FS
))
5347 return (SHRINK_STOP
);
5349 /* Reclaim in progress */
5350 if (mutex_tryenter(&arc_adjust_lock
) == 0) {
5351 ARCSTAT_INCR(arcstat_need_free
, ptob(sc
->nr_to_scan
));
5355 mutex_exit(&arc_adjust_lock
);
5358 * Evict the requested number of pages by shrinking arc_c the
5362 arc_reduce_target_size(ptob(sc
->nr_to_scan
));
5363 if (current_is_kswapd())
5364 arc_kmem_reap_soon();
5365 #ifdef HAVE_SPLIT_SHRINKER_CALLBACK
5366 pages
= MAX((int64_t)pages
-
5367 (int64_t)btop(arc_evictable_memory()), 0);
5369 pages
= btop(arc_evictable_memory());
5372 * We've shrunk what we can, wake up threads.
5374 cv_broadcast(&arc_adjust_waiters_cv
);
5376 pages
= SHRINK_STOP
;
5379 * When direct reclaim is observed it usually indicates a rapid
5380 * increase in memory pressure. This occurs because the kswapd
5381 * threads were unable to asynchronously keep enough free memory
5382 * available. In this case set arc_no_grow to briefly pause arc
5383 * growth to avoid compounding the memory pressure.
5385 if (current_is_kswapd()) {
5386 ARCSTAT_BUMP(arcstat_memory_indirect_count
);
5388 arc_no_grow
= B_TRUE
;
5389 arc_kmem_reap_soon();
5390 ARCSTAT_BUMP(arcstat_memory_direct_count
);
5395 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func
);
5397 SPL_SHRINKER_DECLARE(arc_shrinker
, arc_shrinker_func
, DEFAULT_SEEKS
);
5398 #endif /* _KERNEL */
5401 * Adapt arc info given the number of bytes we are trying to add and
5402 * the state that we are coming from. This function is only called
5403 * when we are adding new content to the cache.
5406 arc_adapt(int bytes
, arc_state_t
*state
)
5409 uint64_t arc_p_min
= (arc_c
>> arc_p_min_shift
);
5410 int64_t mrug_size
= zfs_refcount_count(&arc_mru_ghost
->arcs_size
);
5411 int64_t mfug_size
= zfs_refcount_count(&arc_mfu_ghost
->arcs_size
);
5413 if (state
== arc_l2c_only
)
5418 * Adapt the target size of the MRU list:
5419 * - if we just hit in the MRU ghost list, then increase
5420 * the target size of the MRU list.
5421 * - if we just hit in the MFU ghost list, then increase
5422 * the target size of the MFU list by decreasing the
5423 * target size of the MRU list.
5425 if (state
== arc_mru_ghost
) {
5426 mult
= (mrug_size
>= mfug_size
) ? 1 : (mfug_size
/ mrug_size
);
5427 if (!zfs_arc_p_dampener_disable
)
5428 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
5430 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
5431 } else if (state
== arc_mfu_ghost
) {
5434 mult
= (mfug_size
>= mrug_size
) ? 1 : (mrug_size
/ mfug_size
);
5435 if (!zfs_arc_p_dampener_disable
)
5436 mult
= MIN(mult
, 10);
5438 delta
= MIN(bytes
* mult
, arc_p
);
5439 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
5441 ASSERT((int64_t)arc_p
>= 0);
5444 * Wake reap thread if we do not have any available memory
5446 if (arc_reclaim_needed()) {
5447 zthr_wakeup(arc_reap_zthr
);
5454 if (arc_c
>= arc_c_max
)
5458 * If we're within (2 * maxblocksize) bytes of the target
5459 * cache size, increment the target cache size
5461 ASSERT3U(arc_c
, >=, 2ULL << SPA_MAXBLOCKSHIFT
);
5462 if (aggsum_compare(&arc_size
, arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) >=
5464 atomic_add_64(&arc_c
, (int64_t)bytes
);
5465 if (arc_c
> arc_c_max
)
5467 else if (state
== arc_anon
)
5468 atomic_add_64(&arc_p
, (int64_t)bytes
);
5472 ASSERT((int64_t)arc_p
>= 0);
5476 * Check if arc_size has grown past our upper threshold, determined by
5477 * zfs_arc_overflow_shift.
5480 arc_is_overflowing(void)
5482 /* Always allow at least one block of overflow */
5483 uint64_t overflow
= MAX(SPA_MAXBLOCKSIZE
,
5484 arc_c
>> zfs_arc_overflow_shift
);
5487 * We just compare the lower bound here for performance reasons. Our
5488 * primary goals are to make sure that the arc never grows without
5489 * bound, and that it can reach its maximum size. This check
5490 * accomplishes both goals. The maximum amount we could run over by is
5491 * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block
5492 * in the ARC. In practice, that's in the tens of MB, which is low
5493 * enough to be safe.
5495 return (aggsum_lower_bound(&arc_size
) >= arc_c
+ overflow
);
5499 arc_get_data_abd(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5501 arc_buf_contents_t type
= arc_buf_type(hdr
);
5503 arc_get_data_impl(hdr
, size
, tag
);
5504 if (type
== ARC_BUFC_METADATA
) {
5505 return (abd_alloc(size
, B_TRUE
));
5507 ASSERT(type
== ARC_BUFC_DATA
);
5508 return (abd_alloc(size
, B_FALSE
));
5513 arc_get_data_buf(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5515 arc_buf_contents_t type
= arc_buf_type(hdr
);
5517 arc_get_data_impl(hdr
, size
, tag
);
5518 if (type
== ARC_BUFC_METADATA
) {
5519 return (zio_buf_alloc(size
));
5521 ASSERT(type
== ARC_BUFC_DATA
);
5522 return (zio_data_buf_alloc(size
));
5527 * Allocate a block and return it to the caller. If we are hitting the
5528 * hard limit for the cache size, we must sleep, waiting for the eviction
5529 * thread to catch up. If we're past the target size but below the hard
5530 * limit, we'll only signal the reclaim thread and continue on.
5533 arc_get_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5535 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5536 arc_buf_contents_t type
= arc_buf_type(hdr
);
5538 arc_adapt(size
, state
);
5541 * If arc_size is currently overflowing, and has grown past our
5542 * upper limit, we must be adding data faster than the evict
5543 * thread can evict. Thus, to ensure we don't compound the
5544 * problem by adding more data and forcing arc_size to grow even
5545 * further past it's target size, we halt and wait for the
5546 * eviction thread to catch up.
5548 * It's also possible that the reclaim thread is unable to evict
5549 * enough buffers to get arc_size below the overflow limit (e.g.
5550 * due to buffers being un-evictable, or hash lock collisions).
5551 * In this case, we want to proceed regardless if we're
5552 * overflowing; thus we don't use a while loop here.
5554 if (arc_is_overflowing()) {
5555 mutex_enter(&arc_adjust_lock
);
5558 * Now that we've acquired the lock, we may no longer be
5559 * over the overflow limit, lets check.
5561 * We're ignoring the case of spurious wake ups. If that
5562 * were to happen, it'd let this thread consume an ARC
5563 * buffer before it should have (i.e. before we're under
5564 * the overflow limit and were signalled by the reclaim
5565 * thread). As long as that is a rare occurrence, it
5566 * shouldn't cause any harm.
5568 if (arc_is_overflowing()) {
5569 arc_adjust_needed
= B_TRUE
;
5570 zthr_wakeup(arc_adjust_zthr
);
5571 (void) cv_wait(&arc_adjust_waiters_cv
,
5574 mutex_exit(&arc_adjust_lock
);
5577 VERIFY3U(hdr
->b_type
, ==, type
);
5578 if (type
== ARC_BUFC_METADATA
) {
5579 arc_space_consume(size
, ARC_SPACE_META
);
5581 arc_space_consume(size
, ARC_SPACE_DATA
);
5585 * Update the state size. Note that ghost states have a
5586 * "ghost size" and so don't need to be updated.
5588 if (!GHOST_STATE(state
)) {
5590 (void) zfs_refcount_add_many(&state
->arcs_size
, size
, tag
);
5593 * If this is reached via arc_read, the link is
5594 * protected by the hash lock. If reached via
5595 * arc_buf_alloc, the header should not be accessed by
5596 * any other thread. And, if reached via arc_read_done,
5597 * the hash lock will protect it if it's found in the
5598 * hash table; otherwise no other thread should be
5599 * trying to [add|remove]_reference it.
5601 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5602 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5603 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
5608 * If we are growing the cache, and we are adding anonymous
5609 * data, and we have outgrown arc_p, update arc_p
5611 if (aggsum_compare(&arc_size
, arc_c
) < 0 &&
5612 hdr
->b_l1hdr
.b_state
== arc_anon
&&
5613 (zfs_refcount_count(&arc_anon
->arcs_size
) +
5614 zfs_refcount_count(&arc_mru
->arcs_size
) > arc_p
))
5615 arc_p
= MIN(arc_c
, arc_p
+ size
);
5620 arc_free_data_abd(arc_buf_hdr_t
*hdr
, abd_t
*abd
, uint64_t size
, void *tag
)
5622 arc_free_data_impl(hdr
, size
, tag
);
5627 arc_free_data_buf(arc_buf_hdr_t
*hdr
, void *buf
, uint64_t size
, void *tag
)
5629 arc_buf_contents_t type
= arc_buf_type(hdr
);
5631 arc_free_data_impl(hdr
, size
, tag
);
5632 if (type
== ARC_BUFC_METADATA
) {
5633 zio_buf_free(buf
, size
);
5635 ASSERT(type
== ARC_BUFC_DATA
);
5636 zio_data_buf_free(buf
, size
);
5641 * Free the arc data buffer.
5644 arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5646 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5647 arc_buf_contents_t type
= arc_buf_type(hdr
);
5649 /* protected by hash lock, if in the hash table */
5650 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5651 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5652 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
5654 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
5657 (void) zfs_refcount_remove_many(&state
->arcs_size
, size
, tag
);
5659 VERIFY3U(hdr
->b_type
, ==, type
);
5660 if (type
== ARC_BUFC_METADATA
) {
5661 arc_space_return(size
, ARC_SPACE_META
);
5663 ASSERT(type
== ARC_BUFC_DATA
);
5664 arc_space_return(size
, ARC_SPACE_DATA
);
5669 * This routine is called whenever a buffer is accessed.
5670 * NOTE: the hash lock is dropped in this function.
5673 arc_access(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
5677 ASSERT(MUTEX_HELD(hash_lock
));
5678 ASSERT(HDR_HAS_L1HDR(hdr
));
5680 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
5682 * This buffer is not in the cache, and does not
5683 * appear in our "ghost" list. Add the new buffer
5687 ASSERT0(hdr
->b_l1hdr
.b_arc_access
);
5688 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5689 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5690 arc_change_state(arc_mru
, hdr
, hash_lock
);
5692 } else if (hdr
->b_l1hdr
.b_state
== arc_mru
) {
5693 now
= ddi_get_lbolt();
5696 * If this buffer is here because of a prefetch, then either:
5697 * - clear the flag if this is a "referencing" read
5698 * (any subsequent access will bump this into the MFU state).
5700 * - move the buffer to the head of the list if this is
5701 * another prefetch (to make it less likely to be evicted).
5703 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5704 if (zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
5705 /* link protected by hash lock */
5706 ASSERT(multilist_link_active(
5707 &hdr
->b_l1hdr
.b_arc_node
));
5709 arc_hdr_clear_flags(hdr
,
5711 ARC_FLAG_PRESCIENT_PREFETCH
);
5712 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
5713 ARCSTAT_BUMP(arcstat_mru_hits
);
5715 hdr
->b_l1hdr
.b_arc_access
= now
;
5720 * This buffer has been "accessed" only once so far,
5721 * but it is still in the cache. Move it to the MFU
5724 if (ddi_time_after(now
, hdr
->b_l1hdr
.b_arc_access
+
5727 * More than 125ms have passed since we
5728 * instantiated this buffer. Move it to the
5729 * most frequently used state.
5731 hdr
->b_l1hdr
.b_arc_access
= now
;
5732 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5733 arc_change_state(arc_mfu
, hdr
, hash_lock
);
5735 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
5736 ARCSTAT_BUMP(arcstat_mru_hits
);
5737 } else if (hdr
->b_l1hdr
.b_state
== arc_mru_ghost
) {
5738 arc_state_t
*new_state
;
5740 * This buffer has been "accessed" recently, but
5741 * was evicted from the cache. Move it to the
5745 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5746 new_state
= arc_mru
;
5747 if (zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0) {
5748 arc_hdr_clear_flags(hdr
,
5750 ARC_FLAG_PRESCIENT_PREFETCH
);
5752 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5754 new_state
= arc_mfu
;
5755 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5758 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5759 arc_change_state(new_state
, hdr
, hash_lock
);
5761 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_ghost_hits
);
5762 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
5763 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu
) {
5765 * This buffer has been accessed more than once and is
5766 * still in the cache. Keep it in the MFU state.
5768 * NOTE: an add_reference() that occurred when we did
5769 * the arc_read() will have kicked this off the list.
5770 * If it was a prefetch, we will explicitly move it to
5771 * the head of the list now.
5774 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_hits
);
5775 ARCSTAT_BUMP(arcstat_mfu_hits
);
5776 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5777 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu_ghost
) {
5778 arc_state_t
*new_state
= arc_mfu
;
5780 * This buffer has been accessed more than once but has
5781 * been evicted from the cache. Move it back to the
5785 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5787 * This is a prefetch access...
5788 * move this block back to the MRU state.
5790 new_state
= arc_mru
;
5793 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5794 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5795 arc_change_state(new_state
, hdr
, hash_lock
);
5797 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_ghost_hits
);
5798 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
5799 } else if (hdr
->b_l1hdr
.b_state
== arc_l2c_only
) {
5801 * This buffer is on the 2nd Level ARC.
5804 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5805 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5806 arc_change_state(arc_mfu
, hdr
, hash_lock
);
5808 cmn_err(CE_PANIC
, "invalid arc state 0x%p",
5809 hdr
->b_l1hdr
.b_state
);
5814 * This routine is called by dbuf_hold() to update the arc_access() state
5815 * which otherwise would be skipped for entries in the dbuf cache.
5818 arc_buf_access(arc_buf_t
*buf
)
5820 mutex_enter(&buf
->b_evict_lock
);
5821 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5824 * Avoid taking the hash_lock when possible as an optimization.
5825 * The header must be checked again under the hash_lock in order
5826 * to handle the case where it is concurrently being released.
5828 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5829 mutex_exit(&buf
->b_evict_lock
);
5833 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
5834 mutex_enter(hash_lock
);
5836 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5837 mutex_exit(hash_lock
);
5838 mutex_exit(&buf
->b_evict_lock
);
5839 ARCSTAT_BUMP(arcstat_access_skip
);
5843 mutex_exit(&buf
->b_evict_lock
);
5845 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
5846 hdr
->b_l1hdr
.b_state
== arc_mfu
);
5848 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
5849 arc_access(hdr
, hash_lock
);
5850 mutex_exit(hash_lock
);
5852 ARCSTAT_BUMP(arcstat_hits
);
5853 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
) && !HDR_PRESCIENT_PREFETCH(hdr
),
5854 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
), data
, metadata
, hits
);
5857 /* a generic arc_read_done_func_t which you can use */
5860 arc_bcopy_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5861 arc_buf_t
*buf
, void *arg
)
5866 bcopy(buf
->b_data
, arg
, arc_buf_size(buf
));
5867 arc_buf_destroy(buf
, arg
);
5870 /* a generic arc_read_done_func_t */
5873 arc_getbuf_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5874 arc_buf_t
*buf
, void *arg
)
5876 arc_buf_t
**bufp
= arg
;
5879 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
5882 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
5884 ASSERT(buf
->b_data
!= NULL
);
5889 arc_hdr_verify(arc_buf_hdr_t
*hdr
, blkptr_t
*bp
)
5891 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
5892 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, 0);
5893 ASSERT3U(arc_hdr_get_compress(hdr
), ==, ZIO_COMPRESS_OFF
);
5895 if (HDR_COMPRESSION_ENABLED(hdr
)) {
5896 ASSERT3U(arc_hdr_get_compress(hdr
), ==,
5897 BP_GET_COMPRESS(bp
));
5899 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
5900 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, BP_GET_PSIZE(bp
));
5901 ASSERT3U(!!HDR_PROTECTED(hdr
), ==, BP_IS_PROTECTED(bp
));
5906 arc_read_done(zio_t
*zio
)
5908 blkptr_t
*bp
= zio
->io_bp
;
5909 arc_buf_hdr_t
*hdr
= zio
->io_private
;
5910 kmutex_t
*hash_lock
= NULL
;
5911 arc_callback_t
*callback_list
;
5912 arc_callback_t
*acb
;
5913 boolean_t freeable
= B_FALSE
;
5916 * The hdr was inserted into hash-table and removed from lists
5917 * prior to starting I/O. We should find this header, since
5918 * it's in the hash table, and it should be legit since it's
5919 * not possible to evict it during the I/O. The only possible
5920 * reason for it not to be found is if we were freed during the
5923 if (HDR_IN_HASH_TABLE(hdr
)) {
5924 arc_buf_hdr_t
*found
;
5926 ASSERT3U(hdr
->b_birth
, ==, BP_PHYSICAL_BIRTH(zio
->io_bp
));
5927 ASSERT3U(hdr
->b_dva
.dva_word
[0], ==,
5928 BP_IDENTITY(zio
->io_bp
)->dva_word
[0]);
5929 ASSERT3U(hdr
->b_dva
.dva_word
[1], ==,
5930 BP_IDENTITY(zio
->io_bp
)->dva_word
[1]);
5932 found
= buf_hash_find(hdr
->b_spa
, zio
->io_bp
, &hash_lock
);
5934 ASSERT((found
== hdr
&&
5935 DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
5936 (found
== hdr
&& HDR_L2_READING(hdr
)));
5937 ASSERT3P(hash_lock
, !=, NULL
);
5940 if (BP_IS_PROTECTED(bp
)) {
5941 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
5942 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
5943 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
5944 hdr
->b_crypt_hdr
.b_iv
);
5946 if (BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
) {
5949 tmpbuf
= abd_borrow_buf_copy(zio
->io_abd
,
5950 sizeof (zil_chain_t
));
5951 zio_crypt_decode_mac_zil(tmpbuf
,
5952 hdr
->b_crypt_hdr
.b_mac
);
5953 abd_return_buf(zio
->io_abd
, tmpbuf
,
5954 sizeof (zil_chain_t
));
5956 zio_crypt_decode_mac_bp(bp
, hdr
->b_crypt_hdr
.b_mac
);
5960 if (zio
->io_error
== 0) {
5961 /* byteswap if necessary */
5962 if (BP_SHOULD_BYTESWAP(zio
->io_bp
)) {
5963 if (BP_GET_LEVEL(zio
->io_bp
) > 0) {
5964 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
5966 hdr
->b_l1hdr
.b_byteswap
=
5967 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
5970 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
5974 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_EVICTED
);
5975 if (l2arc_noprefetch
&& HDR_PREFETCH(hdr
))
5976 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2CACHE
);
5978 callback_list
= hdr
->b_l1hdr
.b_acb
;
5979 ASSERT3P(callback_list
, !=, NULL
);
5981 if (hash_lock
&& zio
->io_error
== 0 &&
5982 hdr
->b_l1hdr
.b_state
== arc_anon
) {
5984 * Only call arc_access on anonymous buffers. This is because
5985 * if we've issued an I/O for an evicted buffer, we've already
5986 * called arc_access (to prevent any simultaneous readers from
5987 * getting confused).
5989 arc_access(hdr
, hash_lock
);
5993 * If a read request has a callback (i.e. acb_done is not NULL), then we
5994 * make a buf containing the data according to the parameters which were
5995 * passed in. The implementation of arc_buf_alloc_impl() ensures that we
5996 * aren't needlessly decompressing the data multiple times.
5998 int callback_cnt
= 0;
5999 for (acb
= callback_list
; acb
!= NULL
; acb
= acb
->acb_next
) {
6005 if (zio
->io_error
!= 0)
6008 int error
= arc_buf_alloc_impl(hdr
, zio
->io_spa
,
6009 &acb
->acb_zb
, acb
->acb_private
, acb
->acb_encrypted
,
6010 acb
->acb_compressed
, acb
->acb_noauth
, B_TRUE
,
6014 * Assert non-speculative zios didn't fail because an
6015 * encryption key wasn't loaded
6017 ASSERT((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) ||
6021 * If we failed to decrypt, report an error now (as the zio
6022 * layer would have done if it had done the transforms).
6024 if (error
== ECKSUM
) {
6025 ASSERT(BP_IS_PROTECTED(bp
));
6026 error
= SET_ERROR(EIO
);
6027 if ((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
6028 spa_log_error(zio
->io_spa
, &acb
->acb_zb
);
6029 zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION
,
6030 zio
->io_spa
, NULL
, &acb
->acb_zb
, zio
, 0, 0);
6036 * Decompression or decryption failed. Set
6037 * io_error so that when we call acb_done
6038 * (below), we will indicate that the read
6039 * failed. Note that in the unusual case
6040 * where one callback is compressed and another
6041 * uncompressed, we will mark all of them
6042 * as failed, even though the uncompressed
6043 * one can't actually fail. In this case,
6044 * the hdr will not be anonymous, because
6045 * if there are multiple callbacks, it's
6046 * because multiple threads found the same
6047 * arc buf in the hash table.
6049 zio
->io_error
= error
;
6054 * If there are multiple callbacks, we must have the hash lock,
6055 * because the only way for multiple threads to find this hdr is
6056 * in the hash table. This ensures that if there are multiple
6057 * callbacks, the hdr is not anonymous. If it were anonymous,
6058 * we couldn't use arc_buf_destroy() in the error case below.
6060 ASSERT(callback_cnt
< 2 || hash_lock
!= NULL
);
6062 hdr
->b_l1hdr
.b_acb
= NULL
;
6063 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6064 if (callback_cnt
== 0)
6065 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
6067 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
) ||
6068 callback_list
!= NULL
);
6070 if (zio
->io_error
== 0) {
6071 arc_hdr_verify(hdr
, zio
->io_bp
);
6073 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
6074 if (hdr
->b_l1hdr
.b_state
!= arc_anon
)
6075 arc_change_state(arc_anon
, hdr
, hash_lock
);
6076 if (HDR_IN_HASH_TABLE(hdr
))
6077 buf_hash_remove(hdr
);
6078 freeable
= zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
6082 * Broadcast before we drop the hash_lock to avoid the possibility
6083 * that the hdr (and hence the cv) might be freed before we get to
6084 * the cv_broadcast().
6086 cv_broadcast(&hdr
->b_l1hdr
.b_cv
);
6088 if (hash_lock
!= NULL
) {
6089 mutex_exit(hash_lock
);
6092 * This block was freed while we waited for the read to
6093 * complete. It has been removed from the hash table and
6094 * moved to the anonymous state (so that it won't show up
6097 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
6098 freeable
= zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
6101 /* execute each callback and free its structure */
6102 while ((acb
= callback_list
) != NULL
) {
6103 if (acb
->acb_done
!= NULL
) {
6104 if (zio
->io_error
!= 0 && acb
->acb_buf
!= NULL
) {
6106 * If arc_buf_alloc_impl() fails during
6107 * decompression, the buf will still be
6108 * allocated, and needs to be freed here.
6110 arc_buf_destroy(acb
->acb_buf
,
6112 acb
->acb_buf
= NULL
;
6114 acb
->acb_done(zio
, &zio
->io_bookmark
, zio
->io_bp
,
6115 acb
->acb_buf
, acb
->acb_private
);
6118 if (acb
->acb_zio_dummy
!= NULL
) {
6119 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
6120 zio_nowait(acb
->acb_zio_dummy
);
6123 callback_list
= acb
->acb_next
;
6124 kmem_free(acb
, sizeof (arc_callback_t
));
6128 arc_hdr_destroy(hdr
);
6132 * "Read" the block at the specified DVA (in bp) via the
6133 * cache. If the block is found in the cache, invoke the provided
6134 * callback immediately and return. Note that the `zio' parameter
6135 * in the callback will be NULL in this case, since no IO was
6136 * required. If the block is not in the cache pass the read request
6137 * on to the spa with a substitute callback function, so that the
6138 * requested block will be added to the cache.
6140 * If a read request arrives for a block that has a read in-progress,
6141 * either wait for the in-progress read to complete (and return the
6142 * results); or, if this is a read with a "done" func, add a record
6143 * to the read to invoke the "done" func when the read completes,
6144 * and return; or just return.
6146 * arc_read_done() will invoke all the requested "done" functions
6147 * for readers of this block.
6150 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
6151 arc_read_done_func_t
*done
, void *private, zio_priority_t priority
,
6152 int zio_flags
, arc_flags_t
*arc_flags
, const zbookmark_phys_t
*zb
)
6154 arc_buf_hdr_t
*hdr
= NULL
;
6155 kmutex_t
*hash_lock
= NULL
;
6157 uint64_t guid
= spa_load_guid(spa
);
6158 boolean_t compressed_read
= (zio_flags
& ZIO_FLAG_RAW_COMPRESS
) != 0;
6159 boolean_t encrypted_read
= BP_IS_ENCRYPTED(bp
) &&
6160 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
6161 boolean_t noauth_read
= BP_IS_AUTHENTICATED(bp
) &&
6162 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
6163 boolean_t embedded_bp
= !!BP_IS_EMBEDDED(bp
);
6166 ASSERT(!embedded_bp
||
6167 BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
);
6172 * Embedded BP's have no DVA and require no I/O to "read".
6173 * Create an anonymous arc buf to back it.
6175 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
6179 * Determine if we have an L1 cache hit or a cache miss. For simplicity
6180 * we maintain encrypted data seperately from compressed / uncompressed
6181 * data. If the user is requesting raw encrypted data and we don't have
6182 * that in the header we will read from disk to guarantee that we can
6183 * get it even if the encryption keys aren't loaded.
6185 if (hdr
!= NULL
&& HDR_HAS_L1HDR(hdr
) && (HDR_HAS_RABD(hdr
) ||
6186 (hdr
->b_l1hdr
.b_pabd
!= NULL
&& !encrypted_read
))) {
6187 arc_buf_t
*buf
= NULL
;
6188 *arc_flags
|= ARC_FLAG_CACHED
;
6190 if (HDR_IO_IN_PROGRESS(hdr
)) {
6191 zio_t
*head_zio
= hdr
->b_l1hdr
.b_acb
->acb_zio_head
;
6193 ASSERT3P(head_zio
, !=, NULL
);
6194 if ((hdr
->b_flags
& ARC_FLAG_PRIO_ASYNC_READ
) &&
6195 priority
== ZIO_PRIORITY_SYNC_READ
) {
6197 * This is a sync read that needs to wait for
6198 * an in-flight async read. Request that the
6199 * zio have its priority upgraded.
6201 zio_change_priority(head_zio
, priority
);
6202 DTRACE_PROBE1(arc__async__upgrade__sync
,
6203 arc_buf_hdr_t
*, hdr
);
6204 ARCSTAT_BUMP(arcstat_async_upgrade_sync
);
6206 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
6207 arc_hdr_clear_flags(hdr
,
6208 ARC_FLAG_PREDICTIVE_PREFETCH
);
6211 if (*arc_flags
& ARC_FLAG_WAIT
) {
6212 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
6213 mutex_exit(hash_lock
);
6216 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
6219 arc_callback_t
*acb
= NULL
;
6221 acb
= kmem_zalloc(sizeof (arc_callback_t
),
6223 acb
->acb_done
= done
;
6224 acb
->acb_private
= private;
6225 acb
->acb_compressed
= compressed_read
;
6226 acb
->acb_encrypted
= encrypted_read
;
6227 acb
->acb_noauth
= noauth_read
;
6230 acb
->acb_zio_dummy
= zio_null(pio
,
6231 spa
, NULL
, NULL
, NULL
, zio_flags
);
6233 ASSERT3P(acb
->acb_done
, !=, NULL
);
6234 acb
->acb_zio_head
= head_zio
;
6235 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
6236 hdr
->b_l1hdr
.b_acb
= acb
;
6237 mutex_exit(hash_lock
);
6240 mutex_exit(hash_lock
);
6244 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
6245 hdr
->b_l1hdr
.b_state
== arc_mfu
);
6248 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
6250 * This is a demand read which does not have to
6251 * wait for i/o because we did a predictive
6252 * prefetch i/o for it, which has completed.
6255 arc__demand__hit__predictive__prefetch
,
6256 arc_buf_hdr_t
*, hdr
);
6258 arcstat_demand_hit_predictive_prefetch
);
6259 arc_hdr_clear_flags(hdr
,
6260 ARC_FLAG_PREDICTIVE_PREFETCH
);
6263 if (hdr
->b_flags
& ARC_FLAG_PRESCIENT_PREFETCH
) {
6265 arcstat_demand_hit_prescient_prefetch
);
6266 arc_hdr_clear_flags(hdr
,
6267 ARC_FLAG_PRESCIENT_PREFETCH
);
6270 ASSERT(!embedded_bp
|| !BP_IS_HOLE(bp
));
6272 /* Get a buf with the desired data in it. */
6273 rc
= arc_buf_alloc_impl(hdr
, spa
, zb
, private,
6274 encrypted_read
, compressed_read
, noauth_read
,
6278 * Convert authentication and decryption errors
6279 * to EIO (and generate an ereport if needed)
6280 * before leaving the ARC.
6282 rc
= SET_ERROR(EIO
);
6283 if ((zio_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
6284 spa_log_error(spa
, zb
);
6286 FM_EREPORT_ZFS_AUTHENTICATION
,
6287 spa
, NULL
, zb
, NULL
, 0, 0);
6291 (void) remove_reference(hdr
, hash_lock
,
6293 arc_buf_destroy_impl(buf
);
6297 /* assert any errors weren't due to unloaded keys */
6298 ASSERT((zio_flags
& ZIO_FLAG_SPECULATIVE
) ||
6300 } else if (*arc_flags
& ARC_FLAG_PREFETCH
&&
6301 zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
6302 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
6304 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
6305 arc_access(hdr
, hash_lock
);
6306 if (*arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
)
6307 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
6308 if (*arc_flags
& ARC_FLAG_L2CACHE
)
6309 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6310 mutex_exit(hash_lock
);
6311 ARCSTAT_BUMP(arcstat_hits
);
6312 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
6313 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
6314 data
, metadata
, hits
);
6317 done(NULL
, zb
, bp
, buf
, private);
6319 uint64_t lsize
= BP_GET_LSIZE(bp
);
6320 uint64_t psize
= BP_GET_PSIZE(bp
);
6321 arc_callback_t
*acb
;
6324 boolean_t devw
= B_FALSE
;
6329 * Gracefully handle a damaged logical block size as a
6332 if (lsize
> spa_maxblocksize(spa
)) {
6333 rc
= SET_ERROR(ECKSUM
);
6339 * This block is not in the cache or it has
6342 arc_buf_hdr_t
*exists
= NULL
;
6343 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
6344 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
6345 BP_IS_PROTECTED(bp
), BP_GET_COMPRESS(bp
), type
,
6349 hdr
->b_dva
= *BP_IDENTITY(bp
);
6350 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
6351 exists
= buf_hash_insert(hdr
, &hash_lock
);
6353 if (exists
!= NULL
) {
6354 /* somebody beat us to the hash insert */
6355 mutex_exit(hash_lock
);
6356 buf_discard_identity(hdr
);
6357 arc_hdr_destroy(hdr
);
6358 goto top
; /* restart the IO request */
6362 * This block is in the ghost cache or encrypted data
6363 * was requested and we didn't have it. If it was
6364 * L2-only (and thus didn't have an L1 hdr),
6365 * we realloc the header to add an L1 hdr.
6367 if (!HDR_HAS_L1HDR(hdr
)) {
6368 hdr
= arc_hdr_realloc(hdr
, hdr_l2only_cache
,
6372 if (GHOST_STATE(hdr
->b_l1hdr
.b_state
)) {
6373 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6374 ASSERT(!HDR_HAS_RABD(hdr
));
6375 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6376 ASSERT0(zfs_refcount_count(
6377 &hdr
->b_l1hdr
.b_refcnt
));
6378 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
6379 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
6380 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
6382 * If this header already had an IO in progress
6383 * and we are performing another IO to fetch
6384 * encrypted data we must wait until the first
6385 * IO completes so as not to confuse
6386 * arc_read_done(). This should be very rare
6387 * and so the performance impact shouldn't
6390 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
6391 mutex_exit(hash_lock
);
6396 * This is a delicate dance that we play here.
6397 * This hdr might be in the ghost list so we access
6398 * it to move it out of the ghost list before we
6399 * initiate the read. If it's a prefetch then
6400 * it won't have a callback so we'll remove the
6401 * reference that arc_buf_alloc_impl() created. We
6402 * do this after we've called arc_access() to
6403 * avoid hitting an assert in remove_reference().
6405 arc_access(hdr
, hash_lock
);
6406 arc_hdr_alloc_abd(hdr
, encrypted_read
);
6409 if (encrypted_read
) {
6410 ASSERT(HDR_HAS_RABD(hdr
));
6411 size
= HDR_GET_PSIZE(hdr
);
6412 hdr_abd
= hdr
->b_crypt_hdr
.b_rabd
;
6413 zio_flags
|= ZIO_FLAG_RAW
;
6415 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
6416 size
= arc_hdr_size(hdr
);
6417 hdr_abd
= hdr
->b_l1hdr
.b_pabd
;
6419 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
6420 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
6424 * For authenticated bp's, we do not ask the ZIO layer
6425 * to authenticate them since this will cause the entire
6426 * IO to fail if the key isn't loaded. Instead, we
6427 * defer authentication until arc_buf_fill(), which will
6428 * verify the data when the key is available.
6430 if (BP_IS_AUTHENTICATED(bp
))
6431 zio_flags
|= ZIO_FLAG_RAW_ENCRYPT
;
6434 if (*arc_flags
& ARC_FLAG_PREFETCH
&&
6435 zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
))
6436 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
6437 if (*arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
)
6438 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
6439 if (*arc_flags
& ARC_FLAG_L2CACHE
)
6440 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6441 if (BP_IS_AUTHENTICATED(bp
))
6442 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6443 if (BP_GET_LEVEL(bp
) > 0)
6444 arc_hdr_set_flags(hdr
, ARC_FLAG_INDIRECT
);
6445 if (*arc_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
)
6446 arc_hdr_set_flags(hdr
, ARC_FLAG_PREDICTIVE_PREFETCH
);
6447 ASSERT(!GHOST_STATE(hdr
->b_l1hdr
.b_state
));
6449 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
6450 acb
->acb_done
= done
;
6451 acb
->acb_private
= private;
6452 acb
->acb_compressed
= compressed_read
;
6453 acb
->acb_encrypted
= encrypted_read
;
6454 acb
->acb_noauth
= noauth_read
;
6457 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6458 hdr
->b_l1hdr
.b_acb
= acb
;
6459 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6461 if (HDR_HAS_L2HDR(hdr
) &&
6462 (vd
= hdr
->b_l2hdr
.b_dev
->l2ad_vdev
) != NULL
) {
6463 devw
= hdr
->b_l2hdr
.b_dev
->l2ad_writing
;
6464 addr
= hdr
->b_l2hdr
.b_daddr
;
6466 * Lock out L2ARC device removal.
6468 if (vdev_is_dead(vd
) ||
6469 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
6474 * We count both async reads and scrub IOs as asynchronous so
6475 * that both can be upgraded in the event of a cache hit while
6476 * the read IO is still in-flight.
6478 if (priority
== ZIO_PRIORITY_ASYNC_READ
||
6479 priority
== ZIO_PRIORITY_SCRUB
)
6480 arc_hdr_set_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6482 arc_hdr_clear_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6485 * At this point, we have a level 1 cache miss or a blkptr
6486 * with embedded data. Try again in L2ARC if possible.
6488 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, lsize
);
6491 * Skip ARC stat bump for block pointers with embedded
6492 * data. The data are read from the blkptr itself via
6493 * decode_embedded_bp_compressed().
6496 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
,
6497 blkptr_t
*, bp
, uint64_t, lsize
,
6498 zbookmark_phys_t
*, zb
);
6499 ARCSTAT_BUMP(arcstat_misses
);
6500 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
6501 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
), data
,
6505 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
6507 * Read from the L2ARC if the following are true:
6508 * 1. The L2ARC vdev was previously cached.
6509 * 2. This buffer still has L2ARC metadata.
6510 * 3. This buffer isn't currently writing to the L2ARC.
6511 * 4. The L2ARC entry wasn't evicted, which may
6512 * also have invalidated the vdev.
6513 * 5. This isn't prefetch and l2arc_noprefetch is set.
6515 if (HDR_HAS_L2HDR(hdr
) &&
6516 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
6517 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
6518 l2arc_read_callback_t
*cb
;
6522 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
6523 ARCSTAT_BUMP(arcstat_l2_hits
);
6524 atomic_inc_32(&hdr
->b_l2hdr
.b_hits
);
6526 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
6528 cb
->l2rcb_hdr
= hdr
;
6531 cb
->l2rcb_flags
= zio_flags
;
6533 asize
= vdev_psize_to_asize(vd
, size
);
6534 if (asize
!= size
) {
6535 abd
= abd_alloc_for_io(asize
,
6536 HDR_ISTYPE_METADATA(hdr
));
6537 cb
->l2rcb_abd
= abd
;
6542 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
6543 addr
+ asize
<= vd
->vdev_psize
-
6544 VDEV_LABEL_END_SIZE
);
6547 * l2arc read. The SCL_L2ARC lock will be
6548 * released by l2arc_read_done().
6549 * Issue a null zio if the underlying buffer
6550 * was squashed to zero size by compression.
6552 ASSERT3U(arc_hdr_get_compress(hdr
), !=,
6553 ZIO_COMPRESS_EMPTY
);
6554 rzio
= zio_read_phys(pio
, vd
, addr
,
6557 l2arc_read_done
, cb
, priority
,
6558 zio_flags
| ZIO_FLAG_DONT_CACHE
|
6560 ZIO_FLAG_DONT_PROPAGATE
|
6561 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
6562 acb
->acb_zio_head
= rzio
;
6564 if (hash_lock
!= NULL
)
6565 mutex_exit(hash_lock
);
6567 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
6569 ARCSTAT_INCR(arcstat_l2_read_bytes
,
6570 HDR_GET_PSIZE(hdr
));
6572 if (*arc_flags
& ARC_FLAG_NOWAIT
) {
6577 ASSERT(*arc_flags
& ARC_FLAG_WAIT
);
6578 if (zio_wait(rzio
) == 0)
6581 /* l2arc read error; goto zio_read() */
6582 if (hash_lock
!= NULL
)
6583 mutex_enter(hash_lock
);
6585 DTRACE_PROBE1(l2arc__miss
,
6586 arc_buf_hdr_t
*, hdr
);
6587 ARCSTAT_BUMP(arcstat_l2_misses
);
6588 if (HDR_L2_WRITING(hdr
))
6589 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
6590 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6594 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6596 * Skip ARC stat bump for block pointers with
6597 * embedded data. The data are read from the blkptr
6598 * itself via decode_embedded_bp_compressed().
6600 if (l2arc_ndev
!= 0 && !embedded_bp
) {
6601 DTRACE_PROBE1(l2arc__miss
,
6602 arc_buf_hdr_t
*, hdr
);
6603 ARCSTAT_BUMP(arcstat_l2_misses
);
6607 rzio
= zio_read(pio
, spa
, bp
, hdr_abd
, size
,
6608 arc_read_done
, hdr
, priority
, zio_flags
, zb
);
6609 acb
->acb_zio_head
= rzio
;
6611 if (hash_lock
!= NULL
)
6612 mutex_exit(hash_lock
);
6614 if (*arc_flags
& ARC_FLAG_WAIT
) {
6615 rc
= zio_wait(rzio
);
6619 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
6624 /* embedded bps don't actually go to disk */
6626 spa_read_history_add(spa
, zb
, *arc_flags
);
6631 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
6635 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
6637 p
->p_private
= private;
6638 list_link_init(&p
->p_node
);
6639 zfs_refcount_create(&p
->p_refcnt
);
6641 mutex_enter(&arc_prune_mtx
);
6642 zfs_refcount_add(&p
->p_refcnt
, &arc_prune_list
);
6643 list_insert_head(&arc_prune_list
, p
);
6644 mutex_exit(&arc_prune_mtx
);
6650 arc_remove_prune_callback(arc_prune_t
*p
)
6652 boolean_t wait
= B_FALSE
;
6653 mutex_enter(&arc_prune_mtx
);
6654 list_remove(&arc_prune_list
, p
);
6655 if (zfs_refcount_remove(&p
->p_refcnt
, &arc_prune_list
) > 0)
6657 mutex_exit(&arc_prune_mtx
);
6659 /* wait for arc_prune_task to finish */
6661 taskq_wait_outstanding(arc_prune_taskq
, 0);
6662 ASSERT0(zfs_refcount_count(&p
->p_refcnt
));
6663 zfs_refcount_destroy(&p
->p_refcnt
);
6664 kmem_free(p
, sizeof (*p
));
6668 * Notify the arc that a block was freed, and thus will never be used again.
6671 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
6674 kmutex_t
*hash_lock
;
6675 uint64_t guid
= spa_load_guid(spa
);
6677 ASSERT(!BP_IS_EMBEDDED(bp
));
6679 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
6684 * We might be trying to free a block that is still doing I/O
6685 * (i.e. prefetch) or has a reference (i.e. a dedup-ed,
6686 * dmu_sync-ed block). If this block is being prefetched, then it
6687 * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr
6688 * until the I/O completes. A block may also have a reference if it is
6689 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would
6690 * have written the new block to its final resting place on disk but
6691 * without the dedup flag set. This would have left the hdr in the MRU
6692 * state and discoverable. When the txg finally syncs it detects that
6693 * the block was overridden in open context and issues an override I/O.
6694 * Since this is a dedup block, the override I/O will determine if the
6695 * block is already in the DDT. If so, then it will replace the io_bp
6696 * with the bp from the DDT and allow the I/O to finish. When the I/O
6697 * reaches the done callback, dbuf_write_override_done, it will
6698 * check to see if the io_bp and io_bp_override are identical.
6699 * If they are not, then it indicates that the bp was replaced with
6700 * the bp in the DDT and the override bp is freed. This allows
6701 * us to arrive here with a reference on a block that is being
6702 * freed. So if we have an I/O in progress, or a reference to
6703 * this hdr, then we don't destroy the hdr.
6705 if (!HDR_HAS_L1HDR(hdr
) || (!HDR_IO_IN_PROGRESS(hdr
) &&
6706 zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
))) {
6707 arc_change_state(arc_anon
, hdr
, hash_lock
);
6708 arc_hdr_destroy(hdr
);
6709 mutex_exit(hash_lock
);
6711 mutex_exit(hash_lock
);
6717 * Release this buffer from the cache, making it an anonymous buffer. This
6718 * must be done after a read and prior to modifying the buffer contents.
6719 * If the buffer has more than one reference, we must make
6720 * a new hdr for the buffer.
6723 arc_release(arc_buf_t
*buf
, void *tag
)
6725 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6728 * It would be nice to assert that if its DMU metadata (level >
6729 * 0 || it's the dnode file), then it must be syncing context.
6730 * But we don't know that information at this level.
6733 mutex_enter(&buf
->b_evict_lock
);
6735 ASSERT(HDR_HAS_L1HDR(hdr
));
6738 * We don't grab the hash lock prior to this check, because if
6739 * the buffer's header is in the arc_anon state, it won't be
6740 * linked into the hash table.
6742 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
6743 mutex_exit(&buf
->b_evict_lock
);
6744 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6745 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
6746 ASSERT(!HDR_HAS_L2HDR(hdr
));
6747 ASSERT(HDR_EMPTY(hdr
));
6749 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
6750 ASSERT3S(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
), ==, 1);
6751 ASSERT(!list_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6753 hdr
->b_l1hdr
.b_arc_access
= 0;
6756 * If the buf is being overridden then it may already
6757 * have a hdr that is not empty.
6759 buf_discard_identity(hdr
);
6765 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
6766 mutex_enter(hash_lock
);
6769 * This assignment is only valid as long as the hash_lock is
6770 * held, we must be careful not to reference state or the
6771 * b_state field after dropping the lock.
6773 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
6774 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
6775 ASSERT3P(state
, !=, arc_anon
);
6777 /* this buffer is not on any list */
6778 ASSERT3S(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
), >, 0);
6780 if (HDR_HAS_L2HDR(hdr
)) {
6781 mutex_enter(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6784 * We have to recheck this conditional again now that
6785 * we're holding the l2ad_mtx to prevent a race with
6786 * another thread which might be concurrently calling
6787 * l2arc_evict(). In that case, l2arc_evict() might have
6788 * destroyed the header's L2 portion as we were waiting
6789 * to acquire the l2ad_mtx.
6791 if (HDR_HAS_L2HDR(hdr
))
6792 arc_hdr_l2hdr_destroy(hdr
);
6794 mutex_exit(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6798 * Do we have more than one buf?
6800 if (hdr
->b_l1hdr
.b_bufcnt
> 1) {
6801 arc_buf_hdr_t
*nhdr
;
6802 uint64_t spa
= hdr
->b_spa
;
6803 uint64_t psize
= HDR_GET_PSIZE(hdr
);
6804 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
6805 boolean_t
protected = HDR_PROTECTED(hdr
);
6806 enum zio_compress compress
= arc_hdr_get_compress(hdr
);
6807 arc_buf_contents_t type
= arc_buf_type(hdr
);
6808 VERIFY3U(hdr
->b_type
, ==, type
);
6810 ASSERT(hdr
->b_l1hdr
.b_buf
!= buf
|| buf
->b_next
!= NULL
);
6811 (void) remove_reference(hdr
, hash_lock
, tag
);
6813 if (arc_buf_is_shared(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
6814 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6815 ASSERT(ARC_BUF_LAST(buf
));
6819 * Pull the data off of this hdr and attach it to
6820 * a new anonymous hdr. Also find the last buffer
6821 * in the hdr's buffer list.
6823 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
6824 ASSERT3P(lastbuf
, !=, NULL
);
6827 * If the current arc_buf_t and the hdr are sharing their data
6828 * buffer, then we must stop sharing that block.
6830 if (arc_buf_is_shared(buf
)) {
6831 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6832 VERIFY(!arc_buf_is_shared(lastbuf
));
6835 * First, sever the block sharing relationship between
6836 * buf and the arc_buf_hdr_t.
6838 arc_unshare_buf(hdr
, buf
);
6841 * Now we need to recreate the hdr's b_pabd. Since we
6842 * have lastbuf handy, we try to share with it, but if
6843 * we can't then we allocate a new b_pabd and copy the
6844 * data from buf into it.
6846 if (arc_can_share(hdr
, lastbuf
)) {
6847 arc_share_buf(hdr
, lastbuf
);
6849 arc_hdr_alloc_abd(hdr
, B_FALSE
);
6850 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
,
6851 buf
->b_data
, psize
);
6853 VERIFY3P(lastbuf
->b_data
, !=, NULL
);
6854 } else if (HDR_SHARED_DATA(hdr
)) {
6856 * Uncompressed shared buffers are always at the end
6857 * of the list. Compressed buffers don't have the
6858 * same requirements. This makes it hard to
6859 * simply assert that the lastbuf is shared so
6860 * we rely on the hdr's compression flags to determine
6861 * if we have a compressed, shared buffer.
6863 ASSERT(arc_buf_is_shared(lastbuf
) ||
6864 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
6865 ASSERT(!ARC_BUF_SHARED(buf
));
6868 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
6869 ASSERT3P(state
, !=, arc_l2c_only
);
6871 (void) zfs_refcount_remove_many(&state
->arcs_size
,
6872 arc_buf_size(buf
), buf
);
6874 if (zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
6875 ASSERT3P(state
, !=, arc_l2c_only
);
6876 (void) zfs_refcount_remove_many(
6877 &state
->arcs_esize
[type
],
6878 arc_buf_size(buf
), buf
);
6881 hdr
->b_l1hdr
.b_bufcnt
-= 1;
6882 if (ARC_BUF_ENCRYPTED(buf
))
6883 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
6885 arc_cksum_verify(buf
);
6886 arc_buf_unwatch(buf
);
6888 /* if this is the last uncompressed buf free the checksum */
6889 if (!arc_hdr_has_uncompressed_buf(hdr
))
6890 arc_cksum_free(hdr
);
6892 mutex_exit(hash_lock
);
6895 * Allocate a new hdr. The new hdr will contain a b_pabd
6896 * buffer which will be freed in arc_write().
6898 nhdr
= arc_hdr_alloc(spa
, psize
, lsize
, protected,
6899 compress
, type
, HDR_HAS_RABD(hdr
));
6900 ASSERT3P(nhdr
->b_l1hdr
.b_buf
, ==, NULL
);
6901 ASSERT0(nhdr
->b_l1hdr
.b_bufcnt
);
6902 ASSERT0(zfs_refcount_count(&nhdr
->b_l1hdr
.b_refcnt
));
6903 VERIFY3U(nhdr
->b_type
, ==, type
);
6904 ASSERT(!HDR_SHARED_DATA(nhdr
));
6906 nhdr
->b_l1hdr
.b_buf
= buf
;
6907 nhdr
->b_l1hdr
.b_bufcnt
= 1;
6908 if (ARC_BUF_ENCRYPTED(buf
))
6909 nhdr
->b_crypt_hdr
.b_ebufcnt
= 1;
6910 nhdr
->b_l1hdr
.b_mru_hits
= 0;
6911 nhdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
6912 nhdr
->b_l1hdr
.b_mfu_hits
= 0;
6913 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
6914 nhdr
->b_l1hdr
.b_l2_hits
= 0;
6915 (void) zfs_refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, tag
);
6918 mutex_exit(&buf
->b_evict_lock
);
6919 (void) zfs_refcount_add_many(&arc_anon
->arcs_size
,
6920 arc_buf_size(buf
), buf
);
6922 mutex_exit(&buf
->b_evict_lock
);
6923 ASSERT(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 1);
6924 /* protected by hash lock, or hdr is on arc_anon */
6925 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6926 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6927 hdr
->b_l1hdr
.b_mru_hits
= 0;
6928 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
6929 hdr
->b_l1hdr
.b_mfu_hits
= 0;
6930 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
6931 hdr
->b_l1hdr
.b_l2_hits
= 0;
6932 arc_change_state(arc_anon
, hdr
, hash_lock
);
6933 hdr
->b_l1hdr
.b_arc_access
= 0;
6935 mutex_exit(hash_lock
);
6936 buf_discard_identity(hdr
);
6942 arc_released(arc_buf_t
*buf
)
6946 mutex_enter(&buf
->b_evict_lock
);
6947 released
= (buf
->b_data
!= NULL
&&
6948 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
6949 mutex_exit(&buf
->b_evict_lock
);
6955 arc_referenced(arc_buf_t
*buf
)
6959 mutex_enter(&buf
->b_evict_lock
);
6960 referenced
= (zfs_refcount_count(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6961 mutex_exit(&buf
->b_evict_lock
);
6962 return (referenced
);
6967 arc_write_ready(zio_t
*zio
)
6969 arc_write_callback_t
*callback
= zio
->io_private
;
6970 arc_buf_t
*buf
= callback
->awcb_buf
;
6971 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6972 blkptr_t
*bp
= zio
->io_bp
;
6973 uint64_t psize
= BP_IS_HOLE(bp
) ? 0 : BP_GET_PSIZE(bp
);
6974 fstrans_cookie_t cookie
= spl_fstrans_mark();
6976 ASSERT(HDR_HAS_L1HDR(hdr
));
6977 ASSERT(!zfs_refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6978 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
6981 * If we're reexecuting this zio because the pool suspended, then
6982 * cleanup any state that was previously set the first time the
6983 * callback was invoked.
6985 if (zio
->io_flags
& ZIO_FLAG_REEXECUTED
) {
6986 arc_cksum_free(hdr
);
6987 arc_buf_unwatch(buf
);
6988 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
6989 if (arc_buf_is_shared(buf
)) {
6990 arc_unshare_buf(hdr
, buf
);
6992 arc_hdr_free_abd(hdr
, B_FALSE
);
6996 if (HDR_HAS_RABD(hdr
))
6997 arc_hdr_free_abd(hdr
, B_TRUE
);
6999 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
7000 ASSERT(!HDR_HAS_RABD(hdr
));
7001 ASSERT(!HDR_SHARED_DATA(hdr
));
7002 ASSERT(!arc_buf_is_shared(buf
));
7004 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
7006 if (HDR_IO_IN_PROGRESS(hdr
))
7007 ASSERT(zio
->io_flags
& ZIO_FLAG_REEXECUTED
);
7009 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
7011 if (BP_IS_PROTECTED(bp
) != !!HDR_PROTECTED(hdr
))
7012 hdr
= arc_hdr_realloc_crypt(hdr
, BP_IS_PROTECTED(bp
));
7014 if (BP_IS_PROTECTED(bp
)) {
7015 /* ZIL blocks are written through zio_rewrite */
7016 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
7017 ASSERT(HDR_PROTECTED(hdr
));
7019 if (BP_SHOULD_BYTESWAP(bp
)) {
7020 if (BP_GET_LEVEL(bp
) > 0) {
7021 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
7023 hdr
->b_l1hdr
.b_byteswap
=
7024 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
7027 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
7030 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
7031 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
7032 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
7033 hdr
->b_crypt_hdr
.b_iv
);
7034 zio_crypt_decode_mac_bp(bp
, hdr
->b_crypt_hdr
.b_mac
);
7038 * If this block was written for raw encryption but the zio layer
7039 * ended up only authenticating it, adjust the buffer flags now.
7041 if (BP_IS_AUTHENTICATED(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
7042 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
7043 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
7044 if (BP_GET_COMPRESS(bp
) == ZIO_COMPRESS_OFF
)
7045 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
7046 } else if (BP_IS_HOLE(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
7047 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
7048 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
7051 /* this must be done after the buffer flags are adjusted */
7052 arc_cksum_compute(buf
);
7054 enum zio_compress compress
;
7055 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
7056 compress
= ZIO_COMPRESS_OFF
;
7058 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
7059 compress
= BP_GET_COMPRESS(bp
);
7061 HDR_SET_PSIZE(hdr
, psize
);
7062 arc_hdr_set_compress(hdr
, compress
);
7064 if (zio
->io_error
!= 0 || psize
== 0)
7068 * Fill the hdr with data. If the buffer is encrypted we have no choice
7069 * but to copy the data into b_radb. If the hdr is compressed, the data
7070 * we want is available from the zio, otherwise we can take it from
7073 * We might be able to share the buf's data with the hdr here. However,
7074 * doing so would cause the ARC to be full of linear ABDs if we write a
7075 * lot of shareable data. As a compromise, we check whether scattered
7076 * ABDs are allowed, and assume that if they are then the user wants
7077 * the ARC to be primarily filled with them regardless of the data being
7078 * written. Therefore, if they're allowed then we allocate one and copy
7079 * the data into it; otherwise, we share the data directly if we can.
7081 if (ARC_BUF_ENCRYPTED(buf
)) {
7082 ASSERT3U(psize
, >, 0);
7083 ASSERT(ARC_BUF_COMPRESSED(buf
));
7084 arc_hdr_alloc_abd(hdr
, B_TRUE
);
7085 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
7086 } else if (zfs_abd_scatter_enabled
|| !arc_can_share(hdr
, buf
)) {
7088 * Ideally, we would always copy the io_abd into b_pabd, but the
7089 * user may have disabled compressed ARC, thus we must check the
7090 * hdr's compression setting rather than the io_bp's.
7092 if (BP_IS_ENCRYPTED(bp
)) {
7093 ASSERT3U(psize
, >, 0);
7094 arc_hdr_alloc_abd(hdr
, B_TRUE
);
7095 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
7096 } else if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
7097 !ARC_BUF_COMPRESSED(buf
)) {
7098 ASSERT3U(psize
, >, 0);
7099 arc_hdr_alloc_abd(hdr
, B_FALSE
);
7100 abd_copy(hdr
->b_l1hdr
.b_pabd
, zio
->io_abd
, psize
);
7102 ASSERT3U(zio
->io_orig_size
, ==, arc_hdr_size(hdr
));
7103 arc_hdr_alloc_abd(hdr
, B_FALSE
);
7104 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
7108 ASSERT3P(buf
->b_data
, ==, abd_to_buf(zio
->io_orig_abd
));
7109 ASSERT3U(zio
->io_orig_size
, ==, arc_buf_size(buf
));
7110 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
7112 arc_share_buf(hdr
, buf
);
7116 arc_hdr_verify(hdr
, bp
);
7117 spl_fstrans_unmark(cookie
);
7121 arc_write_children_ready(zio_t
*zio
)
7123 arc_write_callback_t
*callback
= zio
->io_private
;
7124 arc_buf_t
*buf
= callback
->awcb_buf
;
7126 callback
->awcb_children_ready(zio
, buf
, callback
->awcb_private
);
7130 * The SPA calls this callback for each physical write that happens on behalf
7131 * of a logical write. See the comment in dbuf_write_physdone() for details.
7134 arc_write_physdone(zio_t
*zio
)
7136 arc_write_callback_t
*cb
= zio
->io_private
;
7137 if (cb
->awcb_physdone
!= NULL
)
7138 cb
->awcb_physdone(zio
, cb
->awcb_buf
, cb
->awcb_private
);
7142 arc_write_done(zio_t
*zio
)
7144 arc_write_callback_t
*callback
= zio
->io_private
;
7145 arc_buf_t
*buf
= callback
->awcb_buf
;
7146 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
7148 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
7150 if (zio
->io_error
== 0) {
7151 arc_hdr_verify(hdr
, zio
->io_bp
);
7153 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
7154 buf_discard_identity(hdr
);
7156 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
7157 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
7160 ASSERT(HDR_EMPTY(hdr
));
7164 * If the block to be written was all-zero or compressed enough to be
7165 * embedded in the BP, no write was performed so there will be no
7166 * dva/birth/checksum. The buffer must therefore remain anonymous
7169 if (!HDR_EMPTY(hdr
)) {
7170 arc_buf_hdr_t
*exists
;
7171 kmutex_t
*hash_lock
;
7173 ASSERT3U(zio
->io_error
, ==, 0);
7175 arc_cksum_verify(buf
);
7177 exists
= buf_hash_insert(hdr
, &hash_lock
);
7178 if (exists
!= NULL
) {
7180 * This can only happen if we overwrite for
7181 * sync-to-convergence, because we remove
7182 * buffers from the hash table when we arc_free().
7184 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
7185 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
7186 panic("bad overwrite, hdr=%p exists=%p",
7187 (void *)hdr
, (void *)exists
);
7188 ASSERT(zfs_refcount_is_zero(
7189 &exists
->b_l1hdr
.b_refcnt
));
7190 arc_change_state(arc_anon
, exists
, hash_lock
);
7191 arc_hdr_destroy(exists
);
7192 mutex_exit(hash_lock
);
7193 exists
= buf_hash_insert(hdr
, &hash_lock
);
7194 ASSERT3P(exists
, ==, NULL
);
7195 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
7197 ASSERT(zio
->io_prop
.zp_nopwrite
);
7198 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
7199 panic("bad nopwrite, hdr=%p exists=%p",
7200 (void *)hdr
, (void *)exists
);
7203 ASSERT(hdr
->b_l1hdr
.b_bufcnt
== 1);
7204 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
7205 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
7206 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
7209 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
7210 /* if it's not anon, we are doing a scrub */
7211 if (exists
== NULL
&& hdr
->b_l1hdr
.b_state
== arc_anon
)
7212 arc_access(hdr
, hash_lock
);
7213 mutex_exit(hash_lock
);
7215 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
7218 ASSERT(!zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
7219 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
7221 abd_put(zio
->io_abd
);
7222 kmem_free(callback
, sizeof (arc_write_callback_t
));
7226 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
7227 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
,
7228 const zio_prop_t
*zp
, arc_write_done_func_t
*ready
,
7229 arc_write_done_func_t
*children_ready
, arc_write_done_func_t
*physdone
,
7230 arc_write_done_func_t
*done
, void *private, zio_priority_t priority
,
7231 int zio_flags
, const zbookmark_phys_t
*zb
)
7233 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
7234 arc_write_callback_t
*callback
;
7236 zio_prop_t localprop
= *zp
;
7238 ASSERT3P(ready
, !=, NULL
);
7239 ASSERT3P(done
, !=, NULL
);
7240 ASSERT(!HDR_IO_ERROR(hdr
));
7241 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
7242 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
7243 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
7245 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
7247 if (ARC_BUF_ENCRYPTED(buf
)) {
7248 ASSERT(ARC_BUF_COMPRESSED(buf
));
7249 localprop
.zp_encrypt
= B_TRUE
;
7250 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
7251 localprop
.zp_byteorder
=
7252 (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
7253 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
7254 bcopy(hdr
->b_crypt_hdr
.b_salt
, localprop
.zp_salt
,
7256 bcopy(hdr
->b_crypt_hdr
.b_iv
, localprop
.zp_iv
,
7258 bcopy(hdr
->b_crypt_hdr
.b_mac
, localprop
.zp_mac
,
7260 if (DMU_OT_IS_ENCRYPTED(localprop
.zp_type
)) {
7261 localprop
.zp_nopwrite
= B_FALSE
;
7262 localprop
.zp_copies
=
7263 MIN(localprop
.zp_copies
, SPA_DVAS_PER_BP
- 1);
7265 zio_flags
|= ZIO_FLAG_RAW
;
7266 } else if (ARC_BUF_COMPRESSED(buf
)) {
7267 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, arc_buf_size(buf
));
7268 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
7269 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
7271 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
7272 callback
->awcb_ready
= ready
;
7273 callback
->awcb_children_ready
= children_ready
;
7274 callback
->awcb_physdone
= physdone
;
7275 callback
->awcb_done
= done
;
7276 callback
->awcb_private
= private;
7277 callback
->awcb_buf
= buf
;
7280 * The hdr's b_pabd is now stale, free it now. A new data block
7281 * will be allocated when the zio pipeline calls arc_write_ready().
7283 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
7285 * If the buf is currently sharing the data block with
7286 * the hdr then we need to break that relationship here.
7287 * The hdr will remain with a NULL data pointer and the
7288 * buf will take sole ownership of the block.
7290 if (arc_buf_is_shared(buf
)) {
7291 arc_unshare_buf(hdr
, buf
);
7293 arc_hdr_free_abd(hdr
, B_FALSE
);
7295 VERIFY3P(buf
->b_data
, !=, NULL
);
7298 if (HDR_HAS_RABD(hdr
))
7299 arc_hdr_free_abd(hdr
, B_TRUE
);
7301 if (!(zio_flags
& ZIO_FLAG_RAW
))
7302 arc_hdr_set_compress(hdr
, ZIO_COMPRESS_OFF
);
7304 ASSERT(!arc_buf_is_shared(buf
));
7305 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
7307 zio
= zio_write(pio
, spa
, txg
, bp
,
7308 abd_get_from_buf(buf
->b_data
, HDR_GET_LSIZE(hdr
)),
7309 HDR_GET_LSIZE(hdr
), arc_buf_size(buf
), &localprop
, arc_write_ready
,
7310 (children_ready
!= NULL
) ? arc_write_children_ready
: NULL
,
7311 arc_write_physdone
, arc_write_done
, callback
,
7312 priority
, zio_flags
, zb
);
7318 arc_memory_throttle(spa_t
*spa
, uint64_t reserve
, uint64_t txg
)
7321 uint64_t available_memory
= arc_free_memory();
7325 MIN(available_memory
, vmem_size(heap_arena
, VMEM_FREE
));
7328 if (available_memory
> arc_all_memory() * arc_lotsfree_percent
/ 100)
7331 if (txg
> spa
->spa_lowmem_last_txg
) {
7332 spa
->spa_lowmem_last_txg
= txg
;
7333 spa
->spa_lowmem_page_load
= 0;
7336 * If we are in pageout, we know that memory is already tight,
7337 * the arc is already going to be evicting, so we just want to
7338 * continue to let page writes occur as quickly as possible.
7340 if (current_is_kswapd()) {
7341 if (spa
->spa_lowmem_page_load
>
7342 MAX(arc_sys_free
/ 4, available_memory
) / 4) {
7343 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
7344 return (SET_ERROR(ERESTART
));
7346 /* Note: reserve is inflated, so we deflate */
7347 atomic_add_64(&spa
->spa_lowmem_page_load
, reserve
/ 8);
7349 } else if (spa
->spa_lowmem_page_load
> 0 && arc_reclaim_needed()) {
7350 /* memory is low, delay before restarting */
7351 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
7352 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
7353 return (SET_ERROR(EAGAIN
));
7355 spa
->spa_lowmem_page_load
= 0;
7356 #endif /* _KERNEL */
7361 arc_tempreserve_clear(uint64_t reserve
)
7363 atomic_add_64(&arc_tempreserve
, -reserve
);
7364 ASSERT((int64_t)arc_tempreserve
>= 0);
7368 arc_tempreserve_space(spa_t
*spa
, uint64_t reserve
, uint64_t txg
)
7374 reserve
> arc_c
/4 &&
7375 reserve
* 4 > (2ULL << SPA_MAXBLOCKSHIFT
))
7376 arc_c
= MIN(arc_c_max
, reserve
* 4);
7379 * Throttle when the calculated memory footprint for the TXG
7380 * exceeds the target ARC size.
7382 if (reserve
> arc_c
) {
7383 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
7384 return (SET_ERROR(ERESTART
));
7388 * Don't count loaned bufs as in flight dirty data to prevent long
7389 * network delays from blocking transactions that are ready to be
7390 * assigned to a txg.
7393 /* assert that it has not wrapped around */
7394 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
7396 anon_size
= MAX((int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) -
7397 arc_loaned_bytes
), 0);
7400 * Writes will, almost always, require additional memory allocations
7401 * in order to compress/encrypt/etc the data. We therefore need to
7402 * make sure that there is sufficient available memory for this.
7404 error
= arc_memory_throttle(spa
, reserve
, txg
);
7409 * Throttle writes when the amount of dirty data in the cache
7410 * gets too large. We try to keep the cache less than half full
7411 * of dirty blocks so that our sync times don't grow too large.
7413 * In the case of one pool being built on another pool, we want
7414 * to make sure we don't end up throttling the lower (backing)
7415 * pool when the upper pool is the majority contributor to dirty
7416 * data. To insure we make forward progress during throttling, we
7417 * also check the current pool's net dirty data and only throttle
7418 * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty
7419 * data in the cache.
7421 * Note: if two requests come in concurrently, we might let them
7422 * both succeed, when one of them should fail. Not a huge deal.
7424 uint64_t total_dirty
= reserve
+ arc_tempreserve
+ anon_size
;
7425 uint64_t spa_dirty_anon
= spa_dirty_data(spa
);
7427 if (total_dirty
> arc_c
* zfs_arc_dirty_limit_percent
/ 100 &&
7428 anon_size
> arc_c
* zfs_arc_anon_limit_percent
/ 100 &&
7429 spa_dirty_anon
> anon_size
* zfs_arc_pool_dirty_percent
/ 100) {
7431 uint64_t meta_esize
= zfs_refcount_count(
7432 &arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7433 uint64_t data_esize
=
7434 zfs_refcount_count(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7435 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
7436 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
7437 arc_tempreserve
>> 10, meta_esize
>> 10,
7438 data_esize
>> 10, reserve
>> 10, arc_c
>> 10);
7440 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
7441 return (SET_ERROR(ERESTART
));
7443 atomic_add_64(&arc_tempreserve
, reserve
);
7448 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
7449 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
7451 size
->value
.ui64
= zfs_refcount_count(&state
->arcs_size
);
7452 evict_data
->value
.ui64
=
7453 zfs_refcount_count(&state
->arcs_esize
[ARC_BUFC_DATA
]);
7454 evict_metadata
->value
.ui64
=
7455 zfs_refcount_count(&state
->arcs_esize
[ARC_BUFC_METADATA
]);
7459 arc_kstat_update(kstat_t
*ksp
, int rw
)
7461 arc_stats_t
*as
= ksp
->ks_data
;
7463 if (rw
== KSTAT_WRITE
) {
7464 return (SET_ERROR(EACCES
));
7466 arc_kstat_update_state(arc_anon
,
7467 &as
->arcstat_anon_size
,
7468 &as
->arcstat_anon_evictable_data
,
7469 &as
->arcstat_anon_evictable_metadata
);
7470 arc_kstat_update_state(arc_mru
,
7471 &as
->arcstat_mru_size
,
7472 &as
->arcstat_mru_evictable_data
,
7473 &as
->arcstat_mru_evictable_metadata
);
7474 arc_kstat_update_state(arc_mru_ghost
,
7475 &as
->arcstat_mru_ghost_size
,
7476 &as
->arcstat_mru_ghost_evictable_data
,
7477 &as
->arcstat_mru_ghost_evictable_metadata
);
7478 arc_kstat_update_state(arc_mfu
,
7479 &as
->arcstat_mfu_size
,
7480 &as
->arcstat_mfu_evictable_data
,
7481 &as
->arcstat_mfu_evictable_metadata
);
7482 arc_kstat_update_state(arc_mfu_ghost
,
7483 &as
->arcstat_mfu_ghost_size
,
7484 &as
->arcstat_mfu_ghost_evictable_data
,
7485 &as
->arcstat_mfu_ghost_evictable_metadata
);
7487 ARCSTAT(arcstat_size
) = aggsum_value(&arc_size
);
7488 ARCSTAT(arcstat_meta_used
) = aggsum_value(&arc_meta_used
);
7489 ARCSTAT(arcstat_data_size
) = aggsum_value(&astat_data_size
);
7490 ARCSTAT(arcstat_metadata_size
) =
7491 aggsum_value(&astat_metadata_size
);
7492 ARCSTAT(arcstat_hdr_size
) = aggsum_value(&astat_hdr_size
);
7493 ARCSTAT(arcstat_l2_hdr_size
) = aggsum_value(&astat_l2_hdr_size
);
7494 ARCSTAT(arcstat_dbuf_size
) = aggsum_value(&astat_dbuf_size
);
7495 ARCSTAT(arcstat_dnode_size
) = aggsum_value(&astat_dnode_size
);
7496 ARCSTAT(arcstat_bonus_size
) = aggsum_value(&astat_bonus_size
);
7498 as
->arcstat_memory_all_bytes
.value
.ui64
=
7500 as
->arcstat_memory_free_bytes
.value
.ui64
=
7502 as
->arcstat_memory_available_bytes
.value
.i64
=
7503 arc_available_memory();
7510 * This function *must* return indices evenly distributed between all
7511 * sublists of the multilist. This is needed due to how the ARC eviction
7512 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
7513 * distributed between all sublists and uses this assumption when
7514 * deciding which sublist to evict from and how much to evict from it.
7517 arc_state_multilist_index_func(multilist_t
*ml
, void *obj
)
7519 arc_buf_hdr_t
*hdr
= obj
;
7522 * We rely on b_dva to generate evenly distributed index
7523 * numbers using buf_hash below. So, as an added precaution,
7524 * let's make sure we never add empty buffers to the arc lists.
7526 ASSERT(!HDR_EMPTY(hdr
));
7529 * The assumption here, is the hash value for a given
7530 * arc_buf_hdr_t will remain constant throughout its lifetime
7531 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
7532 * Thus, we don't need to store the header's sublist index
7533 * on insertion, as this index can be recalculated on removal.
7535 * Also, the low order bits of the hash value are thought to be
7536 * distributed evenly. Otherwise, in the case that the multilist
7537 * has a power of two number of sublists, each sublists' usage
7538 * would not be evenly distributed.
7540 return (buf_hash(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
) %
7541 multilist_get_num_sublists(ml
));
7545 * Called during module initialization and periodically thereafter to
7546 * apply reasonable changes to the exposed performance tunings. Non-zero
7547 * zfs_* values which differ from the currently set values will be applied.
7550 arc_tuning_update(void)
7552 uint64_t allmem
= arc_all_memory();
7553 unsigned long limit
;
7555 /* Valid range: 64M - <all physical memory> */
7556 if ((zfs_arc_max
) && (zfs_arc_max
!= arc_c_max
) &&
7557 (zfs_arc_max
>= 64 << 20) && (zfs_arc_max
< allmem
) &&
7558 (zfs_arc_max
> arc_c_min
)) {
7559 arc_c_max
= zfs_arc_max
;
7561 arc_p
= (arc_c
>> 1);
7562 if (arc_meta_limit
> arc_c_max
)
7563 arc_meta_limit
= arc_c_max
;
7564 if (arc_dnode_limit
> arc_meta_limit
)
7565 arc_dnode_limit
= arc_meta_limit
;
7568 /* Valid range: 32M - <arc_c_max> */
7569 if ((zfs_arc_min
) && (zfs_arc_min
!= arc_c_min
) &&
7570 (zfs_arc_min
>= 2ULL << SPA_MAXBLOCKSHIFT
) &&
7571 (zfs_arc_min
<= arc_c_max
)) {
7572 arc_c_min
= zfs_arc_min
;
7573 arc_c
= MAX(arc_c
, arc_c_min
);
7576 /* Valid range: 16M - <arc_c_max> */
7577 if ((zfs_arc_meta_min
) && (zfs_arc_meta_min
!= arc_meta_min
) &&
7578 (zfs_arc_meta_min
>= 1ULL << SPA_MAXBLOCKSHIFT
) &&
7579 (zfs_arc_meta_min
<= arc_c_max
)) {
7580 arc_meta_min
= zfs_arc_meta_min
;
7581 if (arc_meta_limit
< arc_meta_min
)
7582 arc_meta_limit
= arc_meta_min
;
7583 if (arc_dnode_limit
< arc_meta_min
)
7584 arc_dnode_limit
= arc_meta_min
;
7587 /* Valid range: <arc_meta_min> - <arc_c_max> */
7588 limit
= zfs_arc_meta_limit
? zfs_arc_meta_limit
:
7589 MIN(zfs_arc_meta_limit_percent
, 100) * arc_c_max
/ 100;
7590 if ((limit
!= arc_meta_limit
) &&
7591 (limit
>= arc_meta_min
) &&
7592 (limit
<= arc_c_max
))
7593 arc_meta_limit
= limit
;
7595 /* Valid range: <arc_meta_min> - <arc_meta_limit> */
7596 limit
= zfs_arc_dnode_limit
? zfs_arc_dnode_limit
:
7597 MIN(zfs_arc_dnode_limit_percent
, 100) * arc_meta_limit
/ 100;
7598 if ((limit
!= arc_dnode_limit
) &&
7599 (limit
>= arc_meta_min
) &&
7600 (limit
<= arc_meta_limit
))
7601 arc_dnode_limit
= limit
;
7603 /* Valid range: 1 - N */
7604 if (zfs_arc_grow_retry
)
7605 arc_grow_retry
= zfs_arc_grow_retry
;
7607 /* Valid range: 1 - N */
7608 if (zfs_arc_shrink_shift
) {
7609 arc_shrink_shift
= zfs_arc_shrink_shift
;
7610 arc_no_grow_shift
= MIN(arc_no_grow_shift
, arc_shrink_shift
-1);
7613 /* Valid range: 1 - N */
7614 if (zfs_arc_p_min_shift
)
7615 arc_p_min_shift
= zfs_arc_p_min_shift
;
7617 /* Valid range: 1 - N ms */
7618 if (zfs_arc_min_prefetch_ms
)
7619 arc_min_prefetch_ms
= zfs_arc_min_prefetch_ms
;
7621 /* Valid range: 1 - N ms */
7622 if (zfs_arc_min_prescient_prefetch_ms
) {
7623 arc_min_prescient_prefetch_ms
=
7624 zfs_arc_min_prescient_prefetch_ms
;
7627 /* Valid range: 0 - 100 */
7628 if ((zfs_arc_lotsfree_percent
>= 0) &&
7629 (zfs_arc_lotsfree_percent
<= 100))
7630 arc_lotsfree_percent
= zfs_arc_lotsfree_percent
;
7632 /* Valid range: 0 - <all physical memory> */
7633 if ((zfs_arc_sys_free
) && (zfs_arc_sys_free
!= arc_sys_free
))
7634 arc_sys_free
= MIN(MAX(zfs_arc_sys_free
, 0), allmem
);
7639 arc_state_init(void)
7641 arc_anon
= &ARC_anon
;
7643 arc_mru_ghost
= &ARC_mru_ghost
;
7645 arc_mfu_ghost
= &ARC_mfu_ghost
;
7646 arc_l2c_only
= &ARC_l2c_only
;
7648 arc_mru
->arcs_list
[ARC_BUFC_METADATA
] =
7649 multilist_create(sizeof (arc_buf_hdr_t
),
7650 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7651 arc_state_multilist_index_func
);
7652 arc_mru
->arcs_list
[ARC_BUFC_DATA
] =
7653 multilist_create(sizeof (arc_buf_hdr_t
),
7654 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7655 arc_state_multilist_index_func
);
7656 arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
] =
7657 multilist_create(sizeof (arc_buf_hdr_t
),
7658 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7659 arc_state_multilist_index_func
);
7660 arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
] =
7661 multilist_create(sizeof (arc_buf_hdr_t
),
7662 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7663 arc_state_multilist_index_func
);
7664 arc_mfu
->arcs_list
[ARC_BUFC_METADATA
] =
7665 multilist_create(sizeof (arc_buf_hdr_t
),
7666 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7667 arc_state_multilist_index_func
);
7668 arc_mfu
->arcs_list
[ARC_BUFC_DATA
] =
7669 multilist_create(sizeof (arc_buf_hdr_t
),
7670 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7671 arc_state_multilist_index_func
);
7672 arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
] =
7673 multilist_create(sizeof (arc_buf_hdr_t
),
7674 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7675 arc_state_multilist_index_func
);
7676 arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
] =
7677 multilist_create(sizeof (arc_buf_hdr_t
),
7678 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7679 arc_state_multilist_index_func
);
7680 arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
] =
7681 multilist_create(sizeof (arc_buf_hdr_t
),
7682 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7683 arc_state_multilist_index_func
);
7684 arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
] =
7685 multilist_create(sizeof (arc_buf_hdr_t
),
7686 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7687 arc_state_multilist_index_func
);
7689 zfs_refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7690 zfs_refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7691 zfs_refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7692 zfs_refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7693 zfs_refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7694 zfs_refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7695 zfs_refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7696 zfs_refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7697 zfs_refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7698 zfs_refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7699 zfs_refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7700 zfs_refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7702 zfs_refcount_create(&arc_anon
->arcs_size
);
7703 zfs_refcount_create(&arc_mru
->arcs_size
);
7704 zfs_refcount_create(&arc_mru_ghost
->arcs_size
);
7705 zfs_refcount_create(&arc_mfu
->arcs_size
);
7706 zfs_refcount_create(&arc_mfu_ghost
->arcs_size
);
7707 zfs_refcount_create(&arc_l2c_only
->arcs_size
);
7709 aggsum_init(&arc_meta_used
, 0);
7710 aggsum_init(&arc_size
, 0);
7711 aggsum_init(&astat_data_size
, 0);
7712 aggsum_init(&astat_metadata_size
, 0);
7713 aggsum_init(&astat_hdr_size
, 0);
7714 aggsum_init(&astat_l2_hdr_size
, 0);
7715 aggsum_init(&astat_bonus_size
, 0);
7716 aggsum_init(&astat_dnode_size
, 0);
7717 aggsum_init(&astat_dbuf_size
, 0);
7719 arc_anon
->arcs_state
= ARC_STATE_ANON
;
7720 arc_mru
->arcs_state
= ARC_STATE_MRU
;
7721 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
7722 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
7723 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
7724 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
7728 arc_state_fini(void)
7730 zfs_refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7731 zfs_refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7732 zfs_refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7733 zfs_refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7734 zfs_refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7735 zfs_refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7736 zfs_refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7737 zfs_refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7738 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7739 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7740 zfs_refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7741 zfs_refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7743 zfs_refcount_destroy(&arc_anon
->arcs_size
);
7744 zfs_refcount_destroy(&arc_mru
->arcs_size
);
7745 zfs_refcount_destroy(&arc_mru_ghost
->arcs_size
);
7746 zfs_refcount_destroy(&arc_mfu
->arcs_size
);
7747 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_size
);
7748 zfs_refcount_destroy(&arc_l2c_only
->arcs_size
);
7750 multilist_destroy(arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
7751 multilist_destroy(arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7752 multilist_destroy(arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
7753 multilist_destroy(arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7754 multilist_destroy(arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
7755 multilist_destroy(arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7756 multilist_destroy(arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
7757 multilist_destroy(arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7758 multilist_destroy(arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]);
7759 multilist_destroy(arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]);
7761 aggsum_fini(&arc_meta_used
);
7762 aggsum_fini(&arc_size
);
7763 aggsum_fini(&astat_data_size
);
7764 aggsum_fini(&astat_metadata_size
);
7765 aggsum_fini(&astat_hdr_size
);
7766 aggsum_fini(&astat_l2_hdr_size
);
7767 aggsum_fini(&astat_bonus_size
);
7768 aggsum_fini(&astat_dnode_size
);
7769 aggsum_fini(&astat_dbuf_size
);
7773 arc_target_bytes(void)
7781 uint64_t percent
, allmem
= arc_all_memory();
7782 mutex_init(&arc_adjust_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7783 cv_init(&arc_adjust_waiters_cv
, NULL
, CV_DEFAULT
, NULL
);
7785 arc_min_prefetch_ms
= 1000;
7786 arc_min_prescient_prefetch_ms
= 6000;
7790 * Register a shrinker to support synchronous (direct) memory
7791 * reclaim from the arc. This is done to prevent kswapd from
7792 * swapping out pages when it is preferable to shrink the arc.
7794 spl_register_shrinker(&arc_shrinker
);
7796 /* Set to 1/64 of all memory or a minimum of 512K */
7797 arc_sys_free
= MAX(allmem
/ 64, (512 * 1024));
7801 /* Set max to 1/2 of all memory */
7802 arc_c_max
= allmem
/ 2;
7805 /* Set min cache to 1/32 of all memory, or 32MB, whichever is more */
7806 arc_c_min
= MAX(allmem
/ 32, 2ULL << SPA_MAXBLOCKSHIFT
);
7809 * In userland, there's only the memory pressure that we artificially
7810 * create (see arc_available_memory()). Don't let arc_c get too
7811 * small, because it can cause transactions to be larger than
7812 * arc_c, causing arc_tempreserve_space() to fail.
7814 arc_c_min
= MAX(arc_c_max
/ 2, 2ULL << SPA_MAXBLOCKSHIFT
);
7818 arc_p
= (arc_c
>> 1);
7820 /* Set min to 1/2 of arc_c_min */
7821 arc_meta_min
= 1ULL << SPA_MAXBLOCKSHIFT
;
7822 /* Initialize maximum observed usage to zero */
7825 * Set arc_meta_limit to a percent of arc_c_max with a floor of
7826 * arc_meta_min, and a ceiling of arc_c_max.
7828 percent
= MIN(zfs_arc_meta_limit_percent
, 100);
7829 arc_meta_limit
= MAX(arc_meta_min
, (percent
* arc_c_max
) / 100);
7830 percent
= MIN(zfs_arc_dnode_limit_percent
, 100);
7831 arc_dnode_limit
= (percent
* arc_meta_limit
) / 100;
7833 /* Apply user specified tunings */
7834 arc_tuning_update();
7836 /* if kmem_flags are set, lets try to use less memory */
7837 if (kmem_debugging())
7839 if (arc_c
< arc_c_min
)
7845 * The arc must be "uninitialized", so that hdr_recl() (which is
7846 * registered by buf_init()) will not access arc_reap_zthr before
7849 ASSERT(!arc_initialized
);
7852 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
7853 offsetof(arc_prune_t
, p_node
));
7854 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
7856 arc_prune_taskq
= taskq_create("arc_prune", max_ncpus
, defclsyspri
,
7857 max_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
7859 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
7860 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
7862 if (arc_ksp
!= NULL
) {
7863 arc_ksp
->ks_data
= &arc_stats
;
7864 arc_ksp
->ks_update
= arc_kstat_update
;
7865 kstat_install(arc_ksp
);
7868 arc_adjust_zthr
= zthr_create(arc_adjust_cb_check
,
7869 arc_adjust_cb
, NULL
);
7870 arc_reap_zthr
= zthr_create_timer(arc_reap_cb_check
,
7871 arc_reap_cb
, NULL
, SEC2NSEC(1));
7873 arc_initialized
= B_TRUE
;
7877 * Calculate maximum amount of dirty data per pool.
7879 * If it has been set by a module parameter, take that.
7880 * Otherwise, use a percentage of physical memory defined by
7881 * zfs_dirty_data_max_percent (default 10%) with a cap at
7882 * zfs_dirty_data_max_max (default 4G or 25% of physical memory).
7884 if (zfs_dirty_data_max_max
== 0)
7885 zfs_dirty_data_max_max
= MIN(4ULL * 1024 * 1024 * 1024,
7886 allmem
* zfs_dirty_data_max_max_percent
/ 100);
7888 if (zfs_dirty_data_max
== 0) {
7889 zfs_dirty_data_max
= allmem
*
7890 zfs_dirty_data_max_percent
/ 100;
7891 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
7892 zfs_dirty_data_max_max
);
7902 spl_unregister_shrinker(&arc_shrinker
);
7903 #endif /* _KERNEL */
7905 /* Use B_TRUE to ensure *all* buffers are evicted */
7906 arc_flush(NULL
, B_TRUE
);
7908 arc_initialized
= B_FALSE
;
7910 if (arc_ksp
!= NULL
) {
7911 kstat_delete(arc_ksp
);
7915 taskq_wait(arc_prune_taskq
);
7916 taskq_destroy(arc_prune_taskq
);
7918 mutex_enter(&arc_prune_mtx
);
7919 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
7920 list_remove(&arc_prune_list
, p
);
7921 zfs_refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
7922 zfs_refcount_destroy(&p
->p_refcnt
);
7923 kmem_free(p
, sizeof (*p
));
7925 mutex_exit(&arc_prune_mtx
);
7927 list_destroy(&arc_prune_list
);
7928 mutex_destroy(&arc_prune_mtx
);
7929 (void) zthr_cancel(arc_adjust_zthr
);
7930 zthr_destroy(arc_adjust_zthr
);
7932 (void) zthr_cancel(arc_reap_zthr
);
7933 zthr_destroy(arc_reap_zthr
);
7935 mutex_destroy(&arc_adjust_lock
);
7936 cv_destroy(&arc_adjust_waiters_cv
);
7939 * buf_fini() must proceed arc_state_fini() because buf_fin() may
7940 * trigger the release of kmem magazines, which can callback to
7941 * arc_space_return() which accesses aggsums freed in act_state_fini().
7946 ASSERT0(arc_loaned_bytes
);
7952 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
7953 * It uses dedicated storage devices to hold cached data, which are populated
7954 * using large infrequent writes. The main role of this cache is to boost
7955 * the performance of random read workloads. The intended L2ARC devices
7956 * include short-stroked disks, solid state disks, and other media with
7957 * substantially faster read latency than disk.
7959 * +-----------------------+
7961 * +-----------------------+
7964 * l2arc_feed_thread() arc_read()
7968 * +---------------+ |
7970 * +---------------+ |
7975 * +-------+ +-------+
7977 * | cache | | cache |
7978 * +-------+ +-------+
7979 * +=========+ .-----.
7980 * : L2ARC : |-_____-|
7981 * : devices : | Disks |
7982 * +=========+ `-_____-'
7984 * Read requests are satisfied from the following sources, in order:
7987 * 2) vdev cache of L2ARC devices
7989 * 4) vdev cache of disks
7992 * Some L2ARC device types exhibit extremely slow write performance.
7993 * To accommodate for this there are some significant differences between
7994 * the L2ARC and traditional cache design:
7996 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
7997 * the ARC behave as usual, freeing buffers and placing headers on ghost
7998 * lists. The ARC does not send buffers to the L2ARC during eviction as
7999 * this would add inflated write latencies for all ARC memory pressure.
8001 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
8002 * It does this by periodically scanning buffers from the eviction-end of
8003 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
8004 * not already there. It scans until a headroom of buffers is satisfied,
8005 * which itself is a buffer for ARC eviction. If a compressible buffer is
8006 * found during scanning and selected for writing to an L2ARC device, we
8007 * temporarily boost scanning headroom during the next scan cycle to make
8008 * sure we adapt to compression effects (which might significantly reduce
8009 * the data volume we write to L2ARC). The thread that does this is
8010 * l2arc_feed_thread(), illustrated below; example sizes are included to
8011 * provide a better sense of ratio than this diagram:
8014 * +---------------------+----------+
8015 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
8016 * +---------------------+----------+ | o L2ARC eligible
8017 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
8018 * +---------------------+----------+ |
8019 * 15.9 Gbytes ^ 32 Mbytes |
8021 * l2arc_feed_thread()
8023 * l2arc write hand <--[oooo]--'
8027 * +==============================+
8028 * L2ARC dev |####|#|###|###| |####| ... |
8029 * +==============================+
8032 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
8033 * evicted, then the L2ARC has cached a buffer much sooner than it probably
8034 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
8035 * safe to say that this is an uncommon case, since buffers at the end of
8036 * the ARC lists have moved there due to inactivity.
8038 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
8039 * then the L2ARC simply misses copying some buffers. This serves as a
8040 * pressure valve to prevent heavy read workloads from both stalling the ARC
8041 * with waits and clogging the L2ARC with writes. This also helps prevent
8042 * the potential for the L2ARC to churn if it attempts to cache content too
8043 * quickly, such as during backups of the entire pool.
8045 * 5. After system boot and before the ARC has filled main memory, there are
8046 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
8047 * lists can remain mostly static. Instead of searching from tail of these
8048 * lists as pictured, the l2arc_feed_thread() will search from the list heads
8049 * for eligible buffers, greatly increasing its chance of finding them.
8051 * The L2ARC device write speed is also boosted during this time so that
8052 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
8053 * there are no L2ARC reads, and no fear of degrading read performance
8054 * through increased writes.
8056 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
8057 * the vdev queue can aggregate them into larger and fewer writes. Each
8058 * device is written to in a rotor fashion, sweeping writes through
8059 * available space then repeating.
8061 * 7. The L2ARC does not store dirty content. It never needs to flush
8062 * write buffers back to disk based storage.
8064 * 8. If an ARC buffer is written (and dirtied) which also exists in the
8065 * L2ARC, the now stale L2ARC buffer is immediately dropped.
8067 * The performance of the L2ARC can be tweaked by a number of tunables, which
8068 * may be necessary for different workloads:
8070 * l2arc_write_max max write bytes per interval
8071 * l2arc_write_boost extra write bytes during device warmup
8072 * l2arc_noprefetch skip caching prefetched buffers
8073 * l2arc_headroom number of max device writes to precache
8074 * l2arc_headroom_boost when we find compressed buffers during ARC
8075 * scanning, we multiply headroom by this
8076 * percentage factor for the next scan cycle,
8077 * since more compressed buffers are likely to
8079 * l2arc_feed_secs seconds between L2ARC writing
8081 * Tunables may be removed or added as future performance improvements are
8082 * integrated, and also may become zpool properties.
8084 * There are three key functions that control how the L2ARC warms up:
8086 * l2arc_write_eligible() check if a buffer is eligible to cache
8087 * l2arc_write_size() calculate how much to write
8088 * l2arc_write_interval() calculate sleep delay between writes
8090 * These three functions determine what to write, how much, and how quickly
8095 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*hdr
)
8098 * A buffer is *not* eligible for the L2ARC if it:
8099 * 1. belongs to a different spa.
8100 * 2. is already cached on the L2ARC.
8101 * 3. has an I/O in progress (it may be an incomplete read).
8102 * 4. is flagged not eligible (zfs property).
8104 if (hdr
->b_spa
!= spa_guid
|| HDR_HAS_L2HDR(hdr
) ||
8105 HDR_IO_IN_PROGRESS(hdr
) || !HDR_L2CACHE(hdr
))
8112 l2arc_write_size(void)
8117 * Make sure our globals have meaningful values in case the user
8120 size
= l2arc_write_max
;
8122 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
8123 "be greater than zero, resetting it to the default (%d)",
8125 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
8128 if (arc_warm
== B_FALSE
)
8129 size
+= l2arc_write_boost
;
8136 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
8138 clock_t interval
, next
, now
;
8141 * If the ARC lists are busy, increase our write rate; if the
8142 * lists are stale, idle back. This is achieved by checking
8143 * how much we previously wrote - if it was more than half of
8144 * what we wanted, schedule the next write much sooner.
8146 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
8147 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
8149 interval
= hz
* l2arc_feed_secs
;
8151 now
= ddi_get_lbolt();
8152 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
8158 * Cycle through L2ARC devices. This is how L2ARC load balances.
8159 * If a device is returned, this also returns holding the spa config lock.
8161 static l2arc_dev_t
*
8162 l2arc_dev_get_next(void)
8164 l2arc_dev_t
*first
, *next
= NULL
;
8167 * Lock out the removal of spas (spa_namespace_lock), then removal
8168 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
8169 * both locks will be dropped and a spa config lock held instead.
8171 mutex_enter(&spa_namespace_lock
);
8172 mutex_enter(&l2arc_dev_mtx
);
8174 /* if there are no vdevs, there is nothing to do */
8175 if (l2arc_ndev
== 0)
8179 next
= l2arc_dev_last
;
8181 /* loop around the list looking for a non-faulted vdev */
8183 next
= list_head(l2arc_dev_list
);
8185 next
= list_next(l2arc_dev_list
, next
);
8187 next
= list_head(l2arc_dev_list
);
8190 /* if we have come back to the start, bail out */
8193 else if (next
== first
)
8196 } while (vdev_is_dead(next
->l2ad_vdev
));
8198 /* if we were unable to find any usable vdevs, return NULL */
8199 if (vdev_is_dead(next
->l2ad_vdev
))
8202 l2arc_dev_last
= next
;
8205 mutex_exit(&l2arc_dev_mtx
);
8208 * Grab the config lock to prevent the 'next' device from being
8209 * removed while we are writing to it.
8212 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
8213 mutex_exit(&spa_namespace_lock
);
8219 * Free buffers that were tagged for destruction.
8222 l2arc_do_free_on_write(void)
8225 l2arc_data_free_t
*df
, *df_prev
;
8227 mutex_enter(&l2arc_free_on_write_mtx
);
8228 buflist
= l2arc_free_on_write
;
8230 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
8231 df_prev
= list_prev(buflist
, df
);
8232 ASSERT3P(df
->l2df_abd
, !=, NULL
);
8233 abd_free(df
->l2df_abd
);
8234 list_remove(buflist
, df
);
8235 kmem_free(df
, sizeof (l2arc_data_free_t
));
8238 mutex_exit(&l2arc_free_on_write_mtx
);
8242 * A write to a cache device has completed. Update all headers to allow
8243 * reads from these buffers to begin.
8246 l2arc_write_done(zio_t
*zio
)
8248 l2arc_write_callback_t
*cb
;
8251 arc_buf_hdr_t
*head
, *hdr
, *hdr_prev
;
8252 kmutex_t
*hash_lock
;
8253 int64_t bytes_dropped
= 0;
8255 cb
= zio
->io_private
;
8256 ASSERT3P(cb
, !=, NULL
);
8257 dev
= cb
->l2wcb_dev
;
8258 ASSERT3P(dev
, !=, NULL
);
8259 head
= cb
->l2wcb_head
;
8260 ASSERT3P(head
, !=, NULL
);
8261 buflist
= &dev
->l2ad_buflist
;
8262 ASSERT3P(buflist
, !=, NULL
);
8263 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
8264 l2arc_write_callback_t
*, cb
);
8266 if (zio
->io_error
!= 0)
8267 ARCSTAT_BUMP(arcstat_l2_writes_error
);
8270 * All writes completed, or an error was hit.
8273 mutex_enter(&dev
->l2ad_mtx
);
8274 for (hdr
= list_prev(buflist
, head
); hdr
; hdr
= hdr_prev
) {
8275 hdr_prev
= list_prev(buflist
, hdr
);
8277 hash_lock
= HDR_LOCK(hdr
);
8280 * We cannot use mutex_enter or else we can deadlock
8281 * with l2arc_write_buffers (due to swapping the order
8282 * the hash lock and l2ad_mtx are taken).
8284 if (!mutex_tryenter(hash_lock
)) {
8286 * Missed the hash lock. We must retry so we
8287 * don't leave the ARC_FLAG_L2_WRITING bit set.
8289 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry
);
8292 * We don't want to rescan the headers we've
8293 * already marked as having been written out, so
8294 * we reinsert the head node so we can pick up
8295 * where we left off.
8297 list_remove(buflist
, head
);
8298 list_insert_after(buflist
, hdr
, head
);
8300 mutex_exit(&dev
->l2ad_mtx
);
8303 * We wait for the hash lock to become available
8304 * to try and prevent busy waiting, and increase
8305 * the chance we'll be able to acquire the lock
8306 * the next time around.
8308 mutex_enter(hash_lock
);
8309 mutex_exit(hash_lock
);
8314 * We could not have been moved into the arc_l2c_only
8315 * state while in-flight due to our ARC_FLAG_L2_WRITING
8316 * bit being set. Let's just ensure that's being enforced.
8318 ASSERT(HDR_HAS_L1HDR(hdr
));
8321 * Skipped - drop L2ARC entry and mark the header as no
8322 * longer L2 eligibile.
8324 if (zio
->io_error
!= 0) {
8326 * Error - drop L2ARC entry.
8328 list_remove(buflist
, hdr
);
8329 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
8331 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8332 ARCSTAT_INCR(arcstat_l2_psize
, -psize
);
8333 ARCSTAT_INCR(arcstat_l2_lsize
, -HDR_GET_LSIZE(hdr
));
8336 vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
8337 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
,
8338 arc_hdr_size(hdr
), hdr
);
8342 * Allow ARC to begin reads and ghost list evictions to
8345 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_WRITING
);
8347 mutex_exit(hash_lock
);
8350 atomic_inc_64(&l2arc_writes_done
);
8351 list_remove(buflist
, head
);
8352 ASSERT(!HDR_HAS_L1HDR(head
));
8353 kmem_cache_free(hdr_l2only_cache
, head
);
8354 mutex_exit(&dev
->l2ad_mtx
);
8356 vdev_space_update(dev
->l2ad_vdev
, -bytes_dropped
, 0, 0);
8358 l2arc_do_free_on_write();
8360 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
8364 l2arc_untransform(zio_t
*zio
, l2arc_read_callback_t
*cb
)
8367 spa_t
*spa
= zio
->io_spa
;
8368 arc_buf_hdr_t
*hdr
= cb
->l2rcb_hdr
;
8369 blkptr_t
*bp
= zio
->io_bp
;
8370 uint8_t salt
[ZIO_DATA_SALT_LEN
];
8371 uint8_t iv
[ZIO_DATA_IV_LEN
];
8372 uint8_t mac
[ZIO_DATA_MAC_LEN
];
8373 boolean_t no_crypt
= B_FALSE
;
8376 * ZIL data is never be written to the L2ARC, so we don't need
8377 * special handling for its unique MAC storage.
8379 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
8380 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
8381 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8384 * If the data was encrypted, decrypt it now. Note that
8385 * we must check the bp here and not the hdr, since the
8386 * hdr does not have its encryption parameters updated
8387 * until arc_read_done().
8389 if (BP_IS_ENCRYPTED(bp
)) {
8390 abd_t
*eabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
);
8392 zio_crypt_decode_params_bp(bp
, salt
, iv
);
8393 zio_crypt_decode_mac_bp(bp
, mac
);
8395 ret
= spa_do_crypt_abd(B_FALSE
, spa
, &cb
->l2rcb_zb
,
8396 BP_GET_TYPE(bp
), BP_GET_DEDUP(bp
), BP_SHOULD_BYTESWAP(bp
),
8397 salt
, iv
, mac
, HDR_GET_PSIZE(hdr
), eabd
,
8398 hdr
->b_l1hdr
.b_pabd
, &no_crypt
);
8400 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8405 * If we actually performed decryption, replace b_pabd
8406 * with the decrypted data. Otherwise we can just throw
8407 * our decryption buffer away.
8410 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8411 arc_hdr_size(hdr
), hdr
);
8412 hdr
->b_l1hdr
.b_pabd
= eabd
;
8415 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8420 * If the L2ARC block was compressed, but ARC compression
8421 * is disabled we decompress the data into a new buffer and
8422 * replace the existing data.
8424 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
8425 !HDR_COMPRESSION_ENABLED(hdr
)) {
8426 abd_t
*cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
);
8427 void *tmp
= abd_borrow_buf(cabd
, arc_hdr_size(hdr
));
8429 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
8430 hdr
->b_l1hdr
.b_pabd
, tmp
, HDR_GET_PSIZE(hdr
),
8431 HDR_GET_LSIZE(hdr
));
8433 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
8434 arc_free_data_abd(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
8438 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
8439 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8440 arc_hdr_size(hdr
), hdr
);
8441 hdr
->b_l1hdr
.b_pabd
= cabd
;
8443 zio
->io_size
= HDR_GET_LSIZE(hdr
);
8454 * A read to a cache device completed. Validate buffer contents before
8455 * handing over to the regular ARC routines.
8458 l2arc_read_done(zio_t
*zio
)
8461 l2arc_read_callback_t
*cb
= zio
->io_private
;
8463 kmutex_t
*hash_lock
;
8464 boolean_t valid_cksum
;
8465 boolean_t using_rdata
= (BP_IS_ENCRYPTED(&cb
->l2rcb_bp
) &&
8466 (cb
->l2rcb_flags
& ZIO_FLAG_RAW_ENCRYPT
));
8468 ASSERT3P(zio
->io_vd
, !=, NULL
);
8469 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
8471 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
8473 ASSERT3P(cb
, !=, NULL
);
8474 hdr
= cb
->l2rcb_hdr
;
8475 ASSERT3P(hdr
, !=, NULL
);
8477 hash_lock
= HDR_LOCK(hdr
);
8478 mutex_enter(hash_lock
);
8479 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
8482 * If the data was read into a temporary buffer,
8483 * move it and free the buffer.
8485 if (cb
->l2rcb_abd
!= NULL
) {
8486 ASSERT3U(arc_hdr_size(hdr
), <, zio
->io_size
);
8487 if (zio
->io_error
== 0) {
8489 abd_copy(hdr
->b_crypt_hdr
.b_rabd
,
8490 cb
->l2rcb_abd
, arc_hdr_size(hdr
));
8492 abd_copy(hdr
->b_l1hdr
.b_pabd
,
8493 cb
->l2rcb_abd
, arc_hdr_size(hdr
));
8498 * The following must be done regardless of whether
8499 * there was an error:
8500 * - free the temporary buffer
8501 * - point zio to the real ARC buffer
8502 * - set zio size accordingly
8503 * These are required because zio is either re-used for
8504 * an I/O of the block in the case of the error
8505 * or the zio is passed to arc_read_done() and it
8508 abd_free(cb
->l2rcb_abd
);
8509 zio
->io_size
= zio
->io_orig_size
= arc_hdr_size(hdr
);
8512 ASSERT(HDR_HAS_RABD(hdr
));
8513 zio
->io_abd
= zio
->io_orig_abd
=
8514 hdr
->b_crypt_hdr
.b_rabd
;
8516 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8517 zio
->io_abd
= zio
->io_orig_abd
= hdr
->b_l1hdr
.b_pabd
;
8521 ASSERT3P(zio
->io_abd
, !=, NULL
);
8524 * Check this survived the L2ARC journey.
8526 ASSERT(zio
->io_abd
== hdr
->b_l1hdr
.b_pabd
||
8527 (HDR_HAS_RABD(hdr
) && zio
->io_abd
== hdr
->b_crypt_hdr
.b_rabd
));
8528 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
8529 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
8531 valid_cksum
= arc_cksum_is_equal(hdr
, zio
);
8534 * b_rabd will always match the data as it exists on disk if it is
8535 * being used. Therefore if we are reading into b_rabd we do not
8536 * attempt to untransform the data.
8538 if (valid_cksum
&& !using_rdata
)
8539 tfm_error
= l2arc_untransform(zio
, cb
);
8541 if (valid_cksum
&& tfm_error
== 0 && zio
->io_error
== 0 &&
8542 !HDR_L2_EVICTED(hdr
)) {
8543 mutex_exit(hash_lock
);
8544 zio
->io_private
= hdr
;
8547 mutex_exit(hash_lock
);
8549 * Buffer didn't survive caching. Increment stats and
8550 * reissue to the original storage device.
8552 if (zio
->io_error
!= 0) {
8553 ARCSTAT_BUMP(arcstat_l2_io_error
);
8555 zio
->io_error
= SET_ERROR(EIO
);
8557 if (!valid_cksum
|| tfm_error
!= 0)
8558 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
8561 * If there's no waiter, issue an async i/o to the primary
8562 * storage now. If there *is* a waiter, the caller must
8563 * issue the i/o in a context where it's OK to block.
8565 if (zio
->io_waiter
== NULL
) {
8566 zio_t
*pio
= zio_unique_parent(zio
);
8567 void *abd
= (using_rdata
) ?
8568 hdr
->b_crypt_hdr
.b_rabd
: hdr
->b_l1hdr
.b_pabd
;
8570 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
8572 zio_nowait(zio_read(pio
, zio
->io_spa
, zio
->io_bp
,
8573 abd
, zio
->io_size
, arc_read_done
,
8574 hdr
, zio
->io_priority
, cb
->l2rcb_flags
,
8579 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
8583 * This is the list priority from which the L2ARC will search for pages to
8584 * cache. This is used within loops (0..3) to cycle through lists in the
8585 * desired order. This order can have a significant effect on cache
8588 * Currently the metadata lists are hit first, MFU then MRU, followed by
8589 * the data lists. This function returns a locked list, and also returns
8592 static multilist_sublist_t
*
8593 l2arc_sublist_lock(int list_num
)
8595 multilist_t
*ml
= NULL
;
8598 ASSERT(list_num
>= 0 && list_num
< L2ARC_FEED_TYPES
);
8602 ml
= arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
8605 ml
= arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
8608 ml
= arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
8611 ml
= arc_mru
->arcs_list
[ARC_BUFC_DATA
];
8618 * Return a randomly-selected sublist. This is acceptable
8619 * because the caller feeds only a little bit of data for each
8620 * call (8MB). Subsequent calls will result in different
8621 * sublists being selected.
8623 idx
= multilist_get_random_index(ml
);
8624 return (multilist_sublist_lock(ml
, idx
));
8628 * Evict buffers from the device write hand to the distance specified in
8629 * bytes. This distance may span populated buffers, it may span nothing.
8630 * This is clearing a region on the L2ARC device ready for writing.
8631 * If the 'all' boolean is set, every buffer is evicted.
8634 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
8637 arc_buf_hdr_t
*hdr
, *hdr_prev
;
8638 kmutex_t
*hash_lock
;
8641 buflist
= &dev
->l2ad_buflist
;
8643 if (!all
&& dev
->l2ad_first
) {
8645 * This is the first sweep through the device. There is
8651 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
8653 * When nearing the end of the device, evict to the end
8654 * before the device write hand jumps to the start.
8656 taddr
= dev
->l2ad_end
;
8658 taddr
= dev
->l2ad_hand
+ distance
;
8660 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
8661 uint64_t, taddr
, boolean_t
, all
);
8664 mutex_enter(&dev
->l2ad_mtx
);
8665 for (hdr
= list_tail(buflist
); hdr
; hdr
= hdr_prev
) {
8666 hdr_prev
= list_prev(buflist
, hdr
);
8668 ASSERT(!HDR_EMPTY(hdr
));
8669 hash_lock
= HDR_LOCK(hdr
);
8672 * We cannot use mutex_enter or else we can deadlock
8673 * with l2arc_write_buffers (due to swapping the order
8674 * the hash lock and l2ad_mtx are taken).
8676 if (!mutex_tryenter(hash_lock
)) {
8678 * Missed the hash lock. Retry.
8680 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
8681 mutex_exit(&dev
->l2ad_mtx
);
8682 mutex_enter(hash_lock
);
8683 mutex_exit(hash_lock
);
8688 * A header can't be on this list if it doesn't have L2 header.
8690 ASSERT(HDR_HAS_L2HDR(hdr
));
8692 /* Ensure this header has finished being written. */
8693 ASSERT(!HDR_L2_WRITING(hdr
));
8694 ASSERT(!HDR_L2_WRITE_HEAD(hdr
));
8696 if (!all
&& (hdr
->b_l2hdr
.b_daddr
>= taddr
||
8697 hdr
->b_l2hdr
.b_daddr
< dev
->l2ad_hand
)) {
8699 * We've evicted to the target address,
8700 * or the end of the device.
8702 mutex_exit(hash_lock
);
8706 if (!HDR_HAS_L1HDR(hdr
)) {
8707 ASSERT(!HDR_L2_READING(hdr
));
8709 * This doesn't exist in the ARC. Destroy.
8710 * arc_hdr_destroy() will call list_remove()
8711 * and decrement arcstat_l2_lsize.
8713 arc_change_state(arc_anon
, hdr
, hash_lock
);
8714 arc_hdr_destroy(hdr
);
8716 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_l2c_only
);
8717 ARCSTAT_BUMP(arcstat_l2_evict_l1cached
);
8719 * Invalidate issued or about to be issued
8720 * reads, since we may be about to write
8721 * over this location.
8723 if (HDR_L2_READING(hdr
)) {
8724 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
8725 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_EVICTED
);
8728 arc_hdr_l2hdr_destroy(hdr
);
8730 mutex_exit(hash_lock
);
8732 mutex_exit(&dev
->l2ad_mtx
);
8736 * Handle any abd transforms that might be required for writing to the L2ARC.
8737 * If successful, this function will always return an abd with the data
8738 * transformed as it is on disk in a new abd of asize bytes.
8741 l2arc_apply_transforms(spa_t
*spa
, arc_buf_hdr_t
*hdr
, uint64_t asize
,
8746 abd_t
*cabd
= NULL
, *eabd
= NULL
, *to_write
= hdr
->b_l1hdr
.b_pabd
;
8747 enum zio_compress compress
= HDR_GET_COMPRESS(hdr
);
8748 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8749 uint64_t size
= arc_hdr_size(hdr
);
8750 boolean_t ismd
= HDR_ISTYPE_METADATA(hdr
);
8751 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
8752 dsl_crypto_key_t
*dck
= NULL
;
8753 uint8_t mac
[ZIO_DATA_MAC_LEN
] = { 0 };
8754 boolean_t no_crypt
= B_FALSE
;
8756 ASSERT((HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
8757 !HDR_COMPRESSION_ENABLED(hdr
)) ||
8758 HDR_ENCRYPTED(hdr
) || HDR_SHARED_DATA(hdr
) || psize
!= asize
);
8759 ASSERT3U(psize
, <=, asize
);
8762 * If this data simply needs its own buffer, we simply allocate it
8763 * and copy the data. This may be done to elimiate a depedency on a
8764 * shared buffer or to reallocate the buffer to match asize.
8766 if (HDR_HAS_RABD(hdr
) && asize
!= psize
) {
8767 ASSERT3U(asize
, >=, psize
);
8768 to_write
= abd_alloc_for_io(asize
, ismd
);
8769 abd_copy(to_write
, hdr
->b_crypt_hdr
.b_rabd
, psize
);
8771 abd_zero_off(to_write
, psize
, asize
- psize
);
8775 if ((compress
== ZIO_COMPRESS_OFF
|| HDR_COMPRESSION_ENABLED(hdr
)) &&
8776 !HDR_ENCRYPTED(hdr
)) {
8777 ASSERT3U(size
, ==, psize
);
8778 to_write
= abd_alloc_for_io(asize
, ismd
);
8779 abd_copy(to_write
, hdr
->b_l1hdr
.b_pabd
, size
);
8781 abd_zero_off(to_write
, size
, asize
- size
);
8785 if (compress
!= ZIO_COMPRESS_OFF
&& !HDR_COMPRESSION_ENABLED(hdr
)) {
8786 cabd
= abd_alloc_for_io(asize
, ismd
);
8787 tmp
= abd_borrow_buf(cabd
, asize
);
8789 psize
= zio_compress_data(compress
, to_write
, tmp
, size
);
8790 ASSERT3U(psize
, <=, HDR_GET_PSIZE(hdr
));
8792 bzero((char *)tmp
+ psize
, asize
- psize
);
8793 psize
= HDR_GET_PSIZE(hdr
);
8794 abd_return_buf_copy(cabd
, tmp
, asize
);
8798 if (HDR_ENCRYPTED(hdr
)) {
8799 eabd
= abd_alloc_for_io(asize
, ismd
);
8802 * If the dataset was disowned before the buffer
8803 * made it to this point, the key to re-encrypt
8804 * it won't be available. In this case we simply
8805 * won't write the buffer to the L2ARC.
8807 ret
= spa_keystore_lookup_key(spa
, hdr
->b_crypt_hdr
.b_dsobj
,
8812 ret
= zio_do_crypt_abd(B_TRUE
, &dck
->dck_key
,
8813 hdr
->b_crypt_hdr
.b_ot
, bswap
, hdr
->b_crypt_hdr
.b_salt
,
8814 hdr
->b_crypt_hdr
.b_iv
, mac
, psize
, to_write
, eabd
,
8820 abd_copy(eabd
, to_write
, psize
);
8823 abd_zero_off(eabd
, psize
, asize
- psize
);
8825 /* assert that the MAC we got here matches the one we saved */
8826 ASSERT0(bcmp(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
));
8827 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
8829 if (to_write
== cabd
)
8836 ASSERT3P(to_write
, !=, hdr
->b_l1hdr
.b_pabd
);
8837 *abd_out
= to_write
;
8842 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
8853 * Find and write ARC buffers to the L2ARC device.
8855 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
8856 * for reading until they have completed writing.
8857 * The headroom_boost is an in-out parameter used to maintain headroom boost
8858 * state between calls to this function.
8860 * Returns the number of bytes actually written (which may be smaller than
8861 * the delta by which the device hand has changed due to alignment).
8864 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
)
8866 arc_buf_hdr_t
*hdr
, *hdr_prev
, *head
;
8867 uint64_t write_asize
, write_psize
, write_lsize
, headroom
;
8869 l2arc_write_callback_t
*cb
;
8871 uint64_t guid
= spa_load_guid(spa
);
8873 ASSERT3P(dev
->l2ad_vdev
, !=, NULL
);
8876 write_lsize
= write_asize
= write_psize
= 0;
8878 head
= kmem_cache_alloc(hdr_l2only_cache
, KM_PUSHPAGE
);
8879 arc_hdr_set_flags(head
, ARC_FLAG_L2_WRITE_HEAD
| ARC_FLAG_HAS_L2HDR
);
8882 * Copy buffers for L2ARC writing.
8884 for (int try = 0; try < L2ARC_FEED_TYPES
; try++) {
8885 multilist_sublist_t
*mls
= l2arc_sublist_lock(try);
8886 uint64_t passed_sz
= 0;
8888 VERIFY3P(mls
, !=, NULL
);
8891 * L2ARC fast warmup.
8893 * Until the ARC is warm and starts to evict, read from the
8894 * head of the ARC lists rather than the tail.
8896 if (arc_warm
== B_FALSE
)
8897 hdr
= multilist_sublist_head(mls
);
8899 hdr
= multilist_sublist_tail(mls
);
8901 headroom
= target_sz
* l2arc_headroom
;
8902 if (zfs_compressed_arc_enabled
)
8903 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
8905 for (; hdr
; hdr
= hdr_prev
) {
8906 kmutex_t
*hash_lock
;
8907 abd_t
*to_write
= NULL
;
8909 if (arc_warm
== B_FALSE
)
8910 hdr_prev
= multilist_sublist_next(mls
, hdr
);
8912 hdr_prev
= multilist_sublist_prev(mls
, hdr
);
8914 hash_lock
= HDR_LOCK(hdr
);
8915 if (!mutex_tryenter(hash_lock
)) {
8917 * Skip this buffer rather than waiting.
8922 passed_sz
+= HDR_GET_LSIZE(hdr
);
8923 if (passed_sz
> headroom
) {
8927 mutex_exit(hash_lock
);
8931 if (!l2arc_write_eligible(guid
, hdr
)) {
8932 mutex_exit(hash_lock
);
8937 * We rely on the L1 portion of the header below, so
8938 * it's invalid for this header to have been evicted out
8939 * of the ghost cache, prior to being written out. The
8940 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
8942 ASSERT(HDR_HAS_L1HDR(hdr
));
8944 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
8945 ASSERT3U(arc_hdr_size(hdr
), >, 0);
8946 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
8948 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8949 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
,
8952 if ((write_asize
+ asize
) > target_sz
) {
8954 mutex_exit(hash_lock
);
8959 * We rely on the L1 portion of the header below, so
8960 * it's invalid for this header to have been evicted out
8961 * of the ghost cache, prior to being written out. The
8962 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
8964 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_WRITING
);
8965 ASSERT(HDR_HAS_L1HDR(hdr
));
8967 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
8968 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
8970 ASSERT3U(arc_hdr_size(hdr
), >, 0);
8973 * If this header has b_rabd, we can use this since it
8974 * must always match the data exactly as it exists on
8975 * disk. Otherwise, the L2ARC can normally use the
8976 * hdr's data, but if we're sharing data between the
8977 * hdr and one of its bufs, L2ARC needs its own copy of
8978 * the data so that the ZIO below can't race with the
8979 * buf consumer. To ensure that this copy will be
8980 * available for the lifetime of the ZIO and be cleaned
8981 * up afterwards, we add it to the l2arc_free_on_write
8982 * queue. If we need to apply any transforms to the
8983 * data (compression, encryption) we will also need the
8986 if (HDR_HAS_RABD(hdr
) && psize
== asize
) {
8987 to_write
= hdr
->b_crypt_hdr
.b_rabd
;
8988 } else if ((HDR_COMPRESSION_ENABLED(hdr
) ||
8989 HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
) &&
8990 !HDR_ENCRYPTED(hdr
) && !HDR_SHARED_DATA(hdr
) &&
8992 to_write
= hdr
->b_l1hdr
.b_pabd
;
8995 arc_buf_contents_t type
= arc_buf_type(hdr
);
8997 ret
= l2arc_apply_transforms(spa
, hdr
, asize
,
9000 arc_hdr_clear_flags(hdr
,
9001 ARC_FLAG_L2_WRITING
);
9002 mutex_exit(hash_lock
);
9006 l2arc_free_abd_on_write(to_write
, asize
, type
);
9011 * Insert a dummy header on the buflist so
9012 * l2arc_write_done() can find where the
9013 * write buffers begin without searching.
9015 mutex_enter(&dev
->l2ad_mtx
);
9016 list_insert_head(&dev
->l2ad_buflist
, head
);
9017 mutex_exit(&dev
->l2ad_mtx
);
9020 sizeof (l2arc_write_callback_t
), KM_SLEEP
);
9021 cb
->l2wcb_dev
= dev
;
9022 cb
->l2wcb_head
= head
;
9023 pio
= zio_root(spa
, l2arc_write_done
, cb
,
9027 hdr
->b_l2hdr
.b_dev
= dev
;
9028 hdr
->b_l2hdr
.b_hits
= 0;
9030 hdr
->b_l2hdr
.b_daddr
= dev
->l2ad_hand
;
9031 arc_hdr_set_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
9033 mutex_enter(&dev
->l2ad_mtx
);
9034 list_insert_head(&dev
->l2ad_buflist
, hdr
);
9035 mutex_exit(&dev
->l2ad_mtx
);
9037 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
9038 arc_hdr_size(hdr
), hdr
);
9040 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
9041 hdr
->b_l2hdr
.b_daddr
, asize
, to_write
,
9042 ZIO_CHECKSUM_OFF
, NULL
, hdr
,
9043 ZIO_PRIORITY_ASYNC_WRITE
,
9044 ZIO_FLAG_CANFAIL
, B_FALSE
);
9046 write_lsize
+= HDR_GET_LSIZE(hdr
);
9047 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
9050 write_psize
+= psize
;
9051 write_asize
+= asize
;
9052 dev
->l2ad_hand
+= asize
;
9053 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
9055 mutex_exit(hash_lock
);
9057 (void) zio_nowait(wzio
);
9060 multilist_sublist_unlock(mls
);
9066 /* No buffers selected for writing? */
9068 ASSERT0(write_lsize
);
9069 ASSERT(!HDR_HAS_L1HDR(head
));
9070 kmem_cache_free(hdr_l2only_cache
, head
);
9074 ASSERT3U(write_asize
, <=, target_sz
);
9075 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
9076 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_psize
);
9077 ARCSTAT_INCR(arcstat_l2_lsize
, write_lsize
);
9078 ARCSTAT_INCR(arcstat_l2_psize
, write_psize
);
9081 * Bump device hand to the device start if it is approaching the end.
9082 * l2arc_evict() will already have evicted ahead for this case.
9084 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
9085 dev
->l2ad_hand
= dev
->l2ad_start
;
9086 dev
->l2ad_first
= B_FALSE
;
9089 dev
->l2ad_writing
= B_TRUE
;
9090 (void) zio_wait(pio
);
9091 dev
->l2ad_writing
= B_FALSE
;
9093 return (write_asize
);
9097 * This thread feeds the L2ARC at regular intervals. This is the beating
9098 * heart of the L2ARC.
9102 l2arc_feed_thread(void *unused
)
9107 uint64_t size
, wrote
;
9108 clock_t begin
, next
= ddi_get_lbolt();
9109 fstrans_cookie_t cookie
;
9111 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
9113 mutex_enter(&l2arc_feed_thr_lock
);
9115 cookie
= spl_fstrans_mark();
9116 while (l2arc_thread_exit
== 0) {
9117 CALLB_CPR_SAFE_BEGIN(&cpr
);
9118 (void) cv_timedwait_sig(&l2arc_feed_thr_cv
,
9119 &l2arc_feed_thr_lock
, next
);
9120 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
9121 next
= ddi_get_lbolt() + hz
;
9124 * Quick check for L2ARC devices.
9126 mutex_enter(&l2arc_dev_mtx
);
9127 if (l2arc_ndev
== 0) {
9128 mutex_exit(&l2arc_dev_mtx
);
9131 mutex_exit(&l2arc_dev_mtx
);
9132 begin
= ddi_get_lbolt();
9135 * This selects the next l2arc device to write to, and in
9136 * doing so the next spa to feed from: dev->l2ad_spa. This
9137 * will return NULL if there are now no l2arc devices or if
9138 * they are all faulted.
9140 * If a device is returned, its spa's config lock is also
9141 * held to prevent device removal. l2arc_dev_get_next()
9142 * will grab and release l2arc_dev_mtx.
9144 if ((dev
= l2arc_dev_get_next()) == NULL
)
9147 spa
= dev
->l2ad_spa
;
9148 ASSERT3P(spa
, !=, NULL
);
9151 * If the pool is read-only then force the feed thread to
9152 * sleep a little longer.
9154 if (!spa_writeable(spa
)) {
9155 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
9156 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9161 * Avoid contributing to memory pressure.
9163 if (arc_reclaim_needed()) {
9164 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
9165 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9169 ARCSTAT_BUMP(arcstat_l2_feeds
);
9171 size
= l2arc_write_size();
9174 * Evict L2ARC buffers that will be overwritten.
9176 l2arc_evict(dev
, size
, B_FALSE
);
9179 * Write ARC buffers.
9181 wrote
= l2arc_write_buffers(spa
, dev
, size
);
9184 * Calculate interval between writes.
9186 next
= l2arc_write_interval(begin
, size
, wrote
);
9187 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9189 spl_fstrans_unmark(cookie
);
9191 l2arc_thread_exit
= 0;
9192 cv_broadcast(&l2arc_feed_thr_cv
);
9193 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
9198 l2arc_vdev_present(vdev_t
*vd
)
9202 mutex_enter(&l2arc_dev_mtx
);
9203 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
9204 dev
= list_next(l2arc_dev_list
, dev
)) {
9205 if (dev
->l2ad_vdev
== vd
)
9208 mutex_exit(&l2arc_dev_mtx
);
9210 return (dev
!= NULL
);
9214 * Add a vdev for use by the L2ARC. By this point the spa has already
9215 * validated the vdev and opened it.
9218 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
9220 l2arc_dev_t
*adddev
;
9222 ASSERT(!l2arc_vdev_present(vd
));
9225 * Create a new l2arc device entry.
9227 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
9228 adddev
->l2ad_spa
= spa
;
9229 adddev
->l2ad_vdev
= vd
;
9230 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
9231 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
9232 adddev
->l2ad_hand
= adddev
->l2ad_start
;
9233 adddev
->l2ad_first
= B_TRUE
;
9234 adddev
->l2ad_writing
= B_FALSE
;
9235 list_link_init(&adddev
->l2ad_node
);
9237 mutex_init(&adddev
->l2ad_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9239 * This is a list of all ARC buffers that are still valid on the
9242 list_create(&adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
9243 offsetof(arc_buf_hdr_t
, b_l2hdr
.b_l2node
));
9245 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
9246 zfs_refcount_create(&adddev
->l2ad_alloc
);
9249 * Add device to global list
9251 mutex_enter(&l2arc_dev_mtx
);
9252 list_insert_head(l2arc_dev_list
, adddev
);
9253 atomic_inc_64(&l2arc_ndev
);
9254 mutex_exit(&l2arc_dev_mtx
);
9258 * Remove a vdev from the L2ARC.
9261 l2arc_remove_vdev(vdev_t
*vd
)
9263 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
9266 * Find the device by vdev
9268 mutex_enter(&l2arc_dev_mtx
);
9269 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
9270 nextdev
= list_next(l2arc_dev_list
, dev
);
9271 if (vd
== dev
->l2ad_vdev
) {
9276 ASSERT3P(remdev
, !=, NULL
);
9279 * Remove device from global list
9281 list_remove(l2arc_dev_list
, remdev
);
9282 l2arc_dev_last
= NULL
; /* may have been invalidated */
9283 atomic_dec_64(&l2arc_ndev
);
9284 mutex_exit(&l2arc_dev_mtx
);
9287 * Clear all buflists and ARC references. L2ARC device flush.
9289 l2arc_evict(remdev
, 0, B_TRUE
);
9290 list_destroy(&remdev
->l2ad_buflist
);
9291 mutex_destroy(&remdev
->l2ad_mtx
);
9292 zfs_refcount_destroy(&remdev
->l2ad_alloc
);
9293 kmem_free(remdev
, sizeof (l2arc_dev_t
));
9299 l2arc_thread_exit
= 0;
9301 l2arc_writes_sent
= 0;
9302 l2arc_writes_done
= 0;
9304 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
9305 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
9306 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9307 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9309 l2arc_dev_list
= &L2ARC_dev_list
;
9310 l2arc_free_on_write
= &L2ARC_free_on_write
;
9311 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
9312 offsetof(l2arc_dev_t
, l2ad_node
));
9313 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
9314 offsetof(l2arc_data_free_t
, l2df_list_node
));
9321 * This is called from dmu_fini(), which is called from spa_fini();
9322 * Because of this, we can assume that all l2arc devices have
9323 * already been removed when the pools themselves were removed.
9326 l2arc_do_free_on_write();
9328 mutex_destroy(&l2arc_feed_thr_lock
);
9329 cv_destroy(&l2arc_feed_thr_cv
);
9330 mutex_destroy(&l2arc_dev_mtx
);
9331 mutex_destroy(&l2arc_free_on_write_mtx
);
9333 list_destroy(l2arc_dev_list
);
9334 list_destroy(l2arc_free_on_write
);
9340 if (!(spa_mode_global
& FWRITE
))
9343 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
9344 TS_RUN
, defclsyspri
);
9350 if (!(spa_mode_global
& FWRITE
))
9353 mutex_enter(&l2arc_feed_thr_lock
);
9354 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
9355 l2arc_thread_exit
= 1;
9356 while (l2arc_thread_exit
!= 0)
9357 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
9358 mutex_exit(&l2arc_feed_thr_lock
);
9361 #if defined(_KERNEL)
9362 EXPORT_SYMBOL(arc_buf_size
);
9363 EXPORT_SYMBOL(arc_write
);
9364 EXPORT_SYMBOL(arc_read
);
9365 EXPORT_SYMBOL(arc_buf_info
);
9366 EXPORT_SYMBOL(arc_getbuf_func
);
9367 EXPORT_SYMBOL(arc_add_prune_callback
);
9368 EXPORT_SYMBOL(arc_remove_prune_callback
);
9371 module_param(zfs_arc_min
, ulong
, 0644);
9372 MODULE_PARM_DESC(zfs_arc_min
, "Min arc size");
9374 module_param(zfs_arc_max
, ulong
, 0644);
9375 MODULE_PARM_DESC(zfs_arc_max
, "Max arc size");
9377 module_param(zfs_arc_meta_limit
, ulong
, 0644);
9378 MODULE_PARM_DESC(zfs_arc_meta_limit
, "Meta limit for arc size");
9380 module_param(zfs_arc_meta_limit_percent
, ulong
, 0644);
9381 MODULE_PARM_DESC(zfs_arc_meta_limit_percent
,
9382 "Percent of arc size for arc meta limit");
9384 module_param(zfs_arc_meta_min
, ulong
, 0644);
9385 MODULE_PARM_DESC(zfs_arc_meta_min
, "Min arc metadata");
9387 module_param(zfs_arc_meta_prune
, int, 0644);
9388 MODULE_PARM_DESC(zfs_arc_meta_prune
, "Meta objects to scan for prune");
9390 module_param(zfs_arc_meta_adjust_restarts
, int, 0644);
9391 MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts
,
9392 "Limit number of restarts in arc_adjust_meta");
9394 module_param(zfs_arc_meta_strategy
, int, 0644);
9395 MODULE_PARM_DESC(zfs_arc_meta_strategy
, "Meta reclaim strategy");
9397 module_param(zfs_arc_grow_retry
, int, 0644);
9398 MODULE_PARM_DESC(zfs_arc_grow_retry
, "Seconds before growing arc size");
9400 module_param(zfs_arc_p_dampener_disable
, int, 0644);
9401 MODULE_PARM_DESC(zfs_arc_p_dampener_disable
, "disable arc_p adapt dampener");
9403 module_param(zfs_arc_shrink_shift
, int, 0644);
9404 MODULE_PARM_DESC(zfs_arc_shrink_shift
, "log2(fraction of arc to reclaim)");
9406 module_param(zfs_arc_pc_percent
, uint
, 0644);
9407 MODULE_PARM_DESC(zfs_arc_pc_percent
,
9408 "Percent of pagecache to reclaim arc to");
9410 module_param(zfs_arc_p_min_shift
, int, 0644);
9411 MODULE_PARM_DESC(zfs_arc_p_min_shift
, "arc_c shift to calc min/max arc_p");
9413 module_param(zfs_arc_average_blocksize
, int, 0444);
9414 MODULE_PARM_DESC(zfs_arc_average_blocksize
, "Target average block size");
9416 module_param(zfs_compressed_arc_enabled
, int, 0644);
9417 MODULE_PARM_DESC(zfs_compressed_arc_enabled
, "Disable compressed arc buffers");
9419 module_param(zfs_arc_min_prefetch_ms
, int, 0644);
9420 MODULE_PARM_DESC(zfs_arc_min_prefetch_ms
, "Min life of prefetch block in ms");
9422 module_param(zfs_arc_min_prescient_prefetch_ms
, int, 0644);
9423 MODULE_PARM_DESC(zfs_arc_min_prescient_prefetch_ms
,
9424 "Min life of prescient prefetched block in ms");
9426 module_param(l2arc_write_max
, ulong
, 0644);
9427 MODULE_PARM_DESC(l2arc_write_max
, "Max write bytes per interval");
9429 module_param(l2arc_write_boost
, ulong
, 0644);
9430 MODULE_PARM_DESC(l2arc_write_boost
, "Extra write bytes during device warmup");
9432 module_param(l2arc_headroom
, ulong
, 0644);
9433 MODULE_PARM_DESC(l2arc_headroom
, "Number of max device writes to precache");
9435 module_param(l2arc_headroom_boost
, ulong
, 0644);
9436 MODULE_PARM_DESC(l2arc_headroom_boost
, "Compressed l2arc_headroom multiplier");
9438 module_param(l2arc_feed_secs
, ulong
, 0644);
9439 MODULE_PARM_DESC(l2arc_feed_secs
, "Seconds between L2ARC writing");
9441 module_param(l2arc_feed_min_ms
, ulong
, 0644);
9442 MODULE_PARM_DESC(l2arc_feed_min_ms
, "Min feed interval in milliseconds");
9444 module_param(l2arc_noprefetch
, int, 0644);
9445 MODULE_PARM_DESC(l2arc_noprefetch
, "Skip caching prefetched buffers");
9447 module_param(l2arc_feed_again
, int, 0644);
9448 MODULE_PARM_DESC(l2arc_feed_again
, "Turbo L2ARC warmup");
9450 module_param(l2arc_norw
, int, 0644);
9451 MODULE_PARM_DESC(l2arc_norw
, "No reads during writes");
9453 module_param(zfs_arc_lotsfree_percent
, int, 0644);
9454 MODULE_PARM_DESC(zfs_arc_lotsfree_percent
,
9455 "System free memory I/O throttle in bytes");
9457 module_param(zfs_arc_sys_free
, ulong
, 0644);
9458 MODULE_PARM_DESC(zfs_arc_sys_free
, "System free memory target size in bytes");
9460 module_param(zfs_arc_dnode_limit
, ulong
, 0644);
9461 MODULE_PARM_DESC(zfs_arc_dnode_limit
, "Minimum bytes of dnodes in arc");
9463 module_param(zfs_arc_dnode_limit_percent
, ulong
, 0644);
9464 MODULE_PARM_DESC(zfs_arc_dnode_limit_percent
,
9465 "Percent of ARC meta buffers for dnodes");
9467 module_param(zfs_arc_dnode_reduce_percent
, ulong
, 0644);
9468 MODULE_PARM_DESC(zfs_arc_dnode_reduce_percent
,
9469 "Percentage of excess dnodes to try to unpin");