4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2018, Joyent, Inc.
24 * Copyright (c) 2011, 2020, Delphix. All rights reserved.
25 * Copyright (c) 2014, Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2017, Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
28 * Copyright (c) 2020, George Amanakis. All rights reserved.
29 * Copyright (c) 2019, Klara Inc.
30 * Copyright (c) 2019, Allan Jude
31 * Copyright (c) 2020, The FreeBSD Foundation [1]
33 * [1] Portions of this software were developed by Allan Jude
34 * under sponsorship from the FreeBSD Foundation.
38 * DVA-based Adjustable Replacement Cache
40 * While much of the theory of operation used here is
41 * based on the self-tuning, low overhead replacement cache
42 * presented by Megiddo and Modha at FAST 2003, there are some
43 * significant differences:
45 * 1. The Megiddo and Modha model assumes any page is evictable.
46 * Pages in its cache cannot be "locked" into memory. This makes
47 * the eviction algorithm simple: evict the last page in the list.
48 * This also make the performance characteristics easy to reason
49 * about. Our cache is not so simple. At any given moment, some
50 * subset of the blocks in the cache are un-evictable because we
51 * have handed out a reference to them. Blocks are only evictable
52 * when there are no external references active. This makes
53 * eviction far more problematic: we choose to evict the evictable
54 * blocks that are the "lowest" in the list.
56 * There are times when it is not possible to evict the requested
57 * space. In these circumstances we are unable to adjust the cache
58 * size. To prevent the cache growing unbounded at these times we
59 * implement a "cache throttle" that slows the flow of new data
60 * into the cache until we can make space available.
62 * 2. The Megiddo and Modha model assumes a fixed cache size.
63 * Pages are evicted when the cache is full and there is a cache
64 * miss. Our model has a variable sized cache. It grows with
65 * high use, but also tries to react to memory pressure from the
66 * operating system: decreasing its size when system memory is
69 * 3. The Megiddo and Modha model assumes a fixed page size. All
70 * elements of the cache are therefore exactly the same size. So
71 * when adjusting the cache size following a cache miss, its simply
72 * a matter of choosing a single page to evict. In our model, we
73 * have variable sized cache blocks (ranging from 512 bytes to
74 * 128K bytes). We therefore choose a set of blocks to evict to make
75 * space for a cache miss that approximates as closely as possible
76 * the space used by the new block.
78 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
79 * by N. Megiddo & D. Modha, FAST 2003
85 * A new reference to a cache buffer can be obtained in two
86 * ways: 1) via a hash table lookup using the DVA as a key,
87 * or 2) via one of the ARC lists. The arc_read() interface
88 * uses method 1, while the internal ARC algorithms for
89 * adjusting the cache use method 2. We therefore provide two
90 * types of locks: 1) the hash table lock array, and 2) the
93 * Buffers do not have their own mutexes, rather they rely on the
94 * hash table mutexes for the bulk of their protection (i.e. most
95 * fields in the arc_buf_hdr_t are protected by these mutexes).
97 * buf_hash_find() returns the appropriate mutex (held) when it
98 * locates the requested buffer in the hash table. It returns
99 * NULL for the mutex if the buffer was not in the table.
101 * buf_hash_remove() expects the appropriate hash mutex to be
102 * already held before it is invoked.
104 * Each ARC state also has a mutex which is used to protect the
105 * buffer list associated with the state. When attempting to
106 * obtain a hash table lock while holding an ARC list lock you
107 * must use: mutex_tryenter() to avoid deadlock. Also note that
108 * the active state mutex must be held before the ghost state mutex.
110 * It as also possible to register a callback which is run when the
111 * arc_meta_limit is reached and no buffers can be safely evicted. In
112 * this case the arc user should drop a reference on some arc buffers so
113 * they can be reclaimed and the arc_meta_limit honored. For example,
114 * when using the ZPL each dentry holds a references on a znode. These
115 * dentries must be pruned before the arc buffer holding the znode can
118 * Note that the majority of the performance stats are manipulated
119 * with atomic operations.
121 * The L2ARC uses the l2ad_mtx on each vdev for the following:
123 * - L2ARC buflist creation
124 * - L2ARC buflist eviction
125 * - L2ARC write completion, which walks L2ARC buflists
126 * - ARC header destruction, as it removes from L2ARC buflists
127 * - ARC header release, as it removes from L2ARC buflists
133 * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure.
134 * This structure can point either to a block that is still in the cache or to
135 * one that is only accessible in an L2 ARC device, or it can provide
136 * information about a block that was recently evicted. If a block is
137 * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough
138 * information to retrieve it from the L2ARC device. This information is
139 * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block
140 * that is in this state cannot access the data directly.
142 * Blocks that are actively being referenced or have not been evicted
143 * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within
144 * the arc_buf_hdr_t that will point to the data block in memory. A block can
145 * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC
146 * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and
147 * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd).
149 * The L1ARC's data pointer may or may not be uncompressed. The ARC has the
150 * ability to store the physical data (b_pabd) associated with the DVA of the
151 * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block,
152 * it will match its on-disk compression characteristics. This behavior can be
153 * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the
154 * compressed ARC functionality is disabled, the b_pabd will point to an
155 * uncompressed version of the on-disk data.
157 * Data in the L1ARC is not accessed by consumers of the ARC directly. Each
158 * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it.
159 * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC
160 * consumer. The ARC will provide references to this data and will keep it
161 * cached until it is no longer in use. The ARC caches only the L1ARC's physical
162 * data block and will evict any arc_buf_t that is no longer referenced. The
163 * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the
164 * "overhead_size" kstat.
166 * Depending on the consumer, an arc_buf_t can be requested in uncompressed or
167 * compressed form. The typical case is that consumers will want uncompressed
168 * data, and when that happens a new data buffer is allocated where the data is
169 * decompressed for them to use. Currently the only consumer who wants
170 * compressed arc_buf_t's is "zfs send", when it streams data exactly as it
171 * exists on disk. When this happens, the arc_buf_t's data buffer is shared
172 * with the arc_buf_hdr_t.
174 * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The
175 * first one is owned by a compressed send consumer (and therefore references
176 * the same compressed data buffer as the arc_buf_hdr_t) and the second could be
177 * used by any other consumer (and has its own uncompressed copy of the data
192 * | b_buf +------------>+-----------+ arc_buf_t
193 * | b_pabd +-+ |b_next +---->+-----------+
194 * +-----------+ | |-----------| |b_next +-->NULL
195 * | |b_comp = T | +-----------+
196 * | |b_data +-+ |b_comp = F |
197 * | +-----------+ | |b_data +-+
198 * +->+------+ | +-----------+ |
200 * data | |<--------------+ | uncompressed
201 * +------+ compressed, | data
202 * shared +-->+------+
207 * When a consumer reads a block, the ARC must first look to see if the
208 * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new
209 * arc_buf_t and either copies uncompressed data into a new data buffer from an
210 * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a
211 * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the
212 * hdr is compressed and the desired compression characteristics of the
213 * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the
214 * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be
215 * the last buffer in the hdr's b_buf list, however a shared compressed buf can
216 * be anywhere in the hdr's list.
218 * The diagram below shows an example of an uncompressed ARC hdr that is
219 * sharing its data with an arc_buf_t (note that the shared uncompressed buf is
220 * the last element in the buf list):
232 * | | arc_buf_t (shared)
233 * | b_buf +------------>+---------+ arc_buf_t
234 * | | |b_next +---->+---------+
235 * | b_pabd +-+ |---------| |b_next +-->NULL
236 * +-----------+ | | | +---------+
238 * | +---------+ | |b_data +-+
239 * +->+------+ | +---------+ |
241 * uncompressed | | | |
244 * | uncompressed | | |
247 * +---------------------------------+
249 * Writing to the ARC requires that the ARC first discard the hdr's b_pabd
250 * since the physical block is about to be rewritten. The new data contents
251 * will be contained in the arc_buf_t. As the I/O pipeline performs the write,
252 * it may compress the data before writing it to disk. The ARC will be called
253 * with the transformed data and will bcopy the transformed on-disk block into
254 * a newly allocated b_pabd. Writes are always done into buffers which have
255 * either been loaned (and hence are new and don't have other readers) or
256 * buffers which have been released (and hence have their own hdr, if there
257 * were originally other readers of the buf's original hdr). This ensures that
258 * the ARC only needs to update a single buf and its hdr after a write occurs.
260 * When the L2ARC is in use, it will also take advantage of the b_pabd. The
261 * L2ARC will always write the contents of b_pabd to the L2ARC. This means
262 * that when compressed ARC is enabled that the L2ARC blocks are identical
263 * to the on-disk block in the main data pool. This provides a significant
264 * advantage since the ARC can leverage the bp's checksum when reading from the
265 * L2ARC to determine if the contents are valid. However, if the compressed
266 * ARC is disabled, then the L2ARC's block must be transformed to look
267 * like the physical block in the main data pool before comparing the
268 * checksum and determining its validity.
270 * The L1ARC has a slightly different system for storing encrypted data.
271 * Raw (encrypted + possibly compressed) data has a few subtle differences from
272 * data that is just compressed. The biggest difference is that it is not
273 * possible to decrypt encrypted data (or vice-versa) if the keys aren't loaded.
274 * The other difference is that encryption cannot be treated as a suggestion.
275 * If a caller would prefer compressed data, but they actually wind up with
276 * uncompressed data the worst thing that could happen is there might be a
277 * performance hit. If the caller requests encrypted data, however, we must be
278 * sure they actually get it or else secret information could be leaked. Raw
279 * data is stored in hdr->b_crypt_hdr.b_rabd. An encrypted header, therefore,
280 * may have both an encrypted version and a decrypted version of its data at
281 * once. When a caller needs a raw arc_buf_t, it is allocated and the data is
282 * copied out of this header. To avoid complications with b_pabd, raw buffers
288 #include <sys/spa_impl.h>
289 #include <sys/zio_compress.h>
290 #include <sys/zio_checksum.h>
291 #include <sys/zfs_context.h>
293 #include <sys/zfs_refcount.h>
294 #include <sys/vdev.h>
295 #include <sys/vdev_impl.h>
296 #include <sys/dsl_pool.h>
297 #include <sys/multilist.h>
300 #include <sys/fm/fs/zfs.h>
301 #include <sys/callb.h>
302 #include <sys/kstat.h>
303 #include <sys/zthr.h>
304 #include <zfs_fletcher.h>
305 #include <sys/arc_impl.h>
306 #include <sys/trace_zfs.h>
307 #include <sys/aggsum.h>
308 #include <sys/wmsum.h>
309 #include <cityhash.h>
310 #include <sys/vdev_trim.h>
311 #include <sys/zfs_racct.h>
312 #include <sys/zstd/zstd.h>
315 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
316 boolean_t arc_watch
= B_FALSE
;
320 * This thread's job is to keep enough free memory in the system, by
321 * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves
322 * arc_available_memory().
324 static zthr_t
*arc_reap_zthr
;
327 * This thread's job is to keep arc_size under arc_c, by calling
328 * arc_evict(), which improves arc_is_overflowing().
330 static zthr_t
*arc_evict_zthr
;
332 static kmutex_t arc_evict_lock
;
333 static boolean_t arc_evict_needed
= B_FALSE
;
336 * Count of bytes evicted since boot.
338 static uint64_t arc_evict_count
;
341 * List of arc_evict_waiter_t's, representing threads waiting for the
342 * arc_evict_count to reach specific values.
344 static list_t arc_evict_waiters
;
347 * When arc_is_overflowing(), arc_get_data_impl() waits for this percent of
348 * the requested amount of data to be evicted. For example, by default for
349 * every 2KB that's evicted, 1KB of it may be "reused" by a new allocation.
350 * Since this is above 100%, it ensures that progress is made towards getting
351 * arc_size under arc_c. Since this is finite, it ensures that allocations
352 * can still happen, even during the potentially long time that arc_size is
355 static int zfs_arc_eviction_pct
= 200;
358 * The number of headers to evict in arc_evict_state_impl() before
359 * dropping the sublist lock and evicting from another sublist. A lower
360 * value means we're more likely to evict the "correct" header (i.e. the
361 * oldest header in the arc state), but comes with higher overhead
362 * (i.e. more invocations of arc_evict_state_impl()).
364 static int zfs_arc_evict_batch_limit
= 10;
366 /* number of seconds before growing cache again */
367 int arc_grow_retry
= 5;
370 * Minimum time between calls to arc_kmem_reap_soon().
372 static const int arc_kmem_cache_reap_retry_ms
= 1000;
374 /* shift of arc_c for calculating overflow limit in arc_get_data_impl */
375 static int zfs_arc_overflow_shift
= 8;
377 /* shift of arc_c for calculating both min and max arc_p */
378 static int arc_p_min_shift
= 4;
380 /* log2(fraction of arc to reclaim) */
381 int arc_shrink_shift
= 7;
383 /* percent of pagecache to reclaim arc to */
385 uint_t zfs_arc_pc_percent
= 0;
389 * log2(fraction of ARC which must be free to allow growing).
390 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
391 * when reading a new block into the ARC, we will evict an equal-sized block
394 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
395 * we will still not allow it to grow.
397 int arc_no_grow_shift
= 5;
401 * minimum lifespan of a prefetch block in clock ticks
402 * (initialized in arc_init())
404 static int arc_min_prefetch_ms
;
405 static int arc_min_prescient_prefetch_ms
;
408 * If this percent of memory is free, don't throttle.
410 int arc_lotsfree_percent
= 10;
413 * The arc has filled available memory and has now warmed up.
418 * These tunables are for performance analysis.
420 unsigned long zfs_arc_max
= 0;
421 unsigned long zfs_arc_min
= 0;
422 unsigned long zfs_arc_meta_limit
= 0;
423 unsigned long zfs_arc_meta_min
= 0;
424 static unsigned long zfs_arc_dnode_limit
= 0;
425 static unsigned long zfs_arc_dnode_reduce_percent
= 10;
426 static int zfs_arc_grow_retry
= 0;
427 static int zfs_arc_shrink_shift
= 0;
428 static int zfs_arc_p_min_shift
= 0;
429 int zfs_arc_average_blocksize
= 8 * 1024; /* 8KB */
432 * ARC dirty data constraints for arc_tempreserve_space() throttle:
433 * * total dirty data limit
434 * * anon block dirty limit
435 * * each pool's anon allowance
437 static const unsigned long zfs_arc_dirty_limit_percent
= 50;
438 static const unsigned long zfs_arc_anon_limit_percent
= 25;
439 static const unsigned long zfs_arc_pool_dirty_percent
= 20;
442 * Enable or disable compressed arc buffers.
444 int zfs_compressed_arc_enabled
= B_TRUE
;
447 * ARC will evict meta buffers that exceed arc_meta_limit. This
448 * tunable make arc_meta_limit adjustable for different workloads.
450 static unsigned long zfs_arc_meta_limit_percent
= 75;
453 * Percentage that can be consumed by dnodes of ARC meta buffers.
455 static unsigned long zfs_arc_dnode_limit_percent
= 10;
458 * These tunables are Linux-specific
460 static unsigned long zfs_arc_sys_free
= 0;
461 static int zfs_arc_min_prefetch_ms
= 0;
462 static int zfs_arc_min_prescient_prefetch_ms
= 0;
463 static int zfs_arc_p_dampener_disable
= 1;
464 static int zfs_arc_meta_prune
= 10000;
465 static int zfs_arc_meta_strategy
= ARC_STRATEGY_META_BALANCED
;
466 static int zfs_arc_meta_adjust_restarts
= 4096;
467 static int zfs_arc_lotsfree_percent
= 10;
470 * Number of arc_prune threads
472 static int zfs_arc_prune_task_threads
= 1;
475 arc_state_t ARC_anon
;
477 arc_state_t ARC_mru_ghost
;
479 arc_state_t ARC_mfu_ghost
;
480 arc_state_t ARC_l2c_only
;
482 arc_stats_t arc_stats
= {
483 { "hits", KSTAT_DATA_UINT64
},
484 { "misses", KSTAT_DATA_UINT64
},
485 { "demand_data_hits", KSTAT_DATA_UINT64
},
486 { "demand_data_misses", KSTAT_DATA_UINT64
},
487 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
488 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
489 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
490 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
491 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
492 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
493 { "mru_hits", KSTAT_DATA_UINT64
},
494 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
495 { "mfu_hits", KSTAT_DATA_UINT64
},
496 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
497 { "deleted", KSTAT_DATA_UINT64
},
498 { "mutex_miss", KSTAT_DATA_UINT64
},
499 { "access_skip", KSTAT_DATA_UINT64
},
500 { "evict_skip", KSTAT_DATA_UINT64
},
501 { "evict_not_enough", KSTAT_DATA_UINT64
},
502 { "evict_l2_cached", KSTAT_DATA_UINT64
},
503 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
504 { "evict_l2_eligible_mfu", KSTAT_DATA_UINT64
},
505 { "evict_l2_eligible_mru", KSTAT_DATA_UINT64
},
506 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
507 { "evict_l2_skip", KSTAT_DATA_UINT64
},
508 { "hash_elements", KSTAT_DATA_UINT64
},
509 { "hash_elements_max", KSTAT_DATA_UINT64
},
510 { "hash_collisions", KSTAT_DATA_UINT64
},
511 { "hash_chains", KSTAT_DATA_UINT64
},
512 { "hash_chain_max", KSTAT_DATA_UINT64
},
513 { "p", KSTAT_DATA_UINT64
},
514 { "c", KSTAT_DATA_UINT64
},
515 { "c_min", KSTAT_DATA_UINT64
},
516 { "c_max", KSTAT_DATA_UINT64
},
517 { "size", KSTAT_DATA_UINT64
},
518 { "compressed_size", KSTAT_DATA_UINT64
},
519 { "uncompressed_size", KSTAT_DATA_UINT64
},
520 { "overhead_size", KSTAT_DATA_UINT64
},
521 { "hdr_size", KSTAT_DATA_UINT64
},
522 { "data_size", KSTAT_DATA_UINT64
},
523 { "metadata_size", KSTAT_DATA_UINT64
},
524 { "dbuf_size", KSTAT_DATA_UINT64
},
525 { "dnode_size", KSTAT_DATA_UINT64
},
526 { "bonus_size", KSTAT_DATA_UINT64
},
527 #if defined(COMPAT_FREEBSD11)
528 { "other_size", KSTAT_DATA_UINT64
},
530 { "anon_size", KSTAT_DATA_UINT64
},
531 { "anon_evictable_data", KSTAT_DATA_UINT64
},
532 { "anon_evictable_metadata", KSTAT_DATA_UINT64
},
533 { "mru_size", KSTAT_DATA_UINT64
},
534 { "mru_evictable_data", KSTAT_DATA_UINT64
},
535 { "mru_evictable_metadata", KSTAT_DATA_UINT64
},
536 { "mru_ghost_size", KSTAT_DATA_UINT64
},
537 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64
},
538 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
539 { "mfu_size", KSTAT_DATA_UINT64
},
540 { "mfu_evictable_data", KSTAT_DATA_UINT64
},
541 { "mfu_evictable_metadata", KSTAT_DATA_UINT64
},
542 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
543 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64
},
544 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
545 { "l2_hits", KSTAT_DATA_UINT64
},
546 { "l2_misses", KSTAT_DATA_UINT64
},
547 { "l2_prefetch_asize", KSTAT_DATA_UINT64
},
548 { "l2_mru_asize", KSTAT_DATA_UINT64
},
549 { "l2_mfu_asize", KSTAT_DATA_UINT64
},
550 { "l2_bufc_data_asize", KSTAT_DATA_UINT64
},
551 { "l2_bufc_metadata_asize", KSTAT_DATA_UINT64
},
552 { "l2_feeds", KSTAT_DATA_UINT64
},
553 { "l2_rw_clash", KSTAT_DATA_UINT64
},
554 { "l2_read_bytes", KSTAT_DATA_UINT64
},
555 { "l2_write_bytes", KSTAT_DATA_UINT64
},
556 { "l2_writes_sent", KSTAT_DATA_UINT64
},
557 { "l2_writes_done", KSTAT_DATA_UINT64
},
558 { "l2_writes_error", KSTAT_DATA_UINT64
},
559 { "l2_writes_lock_retry", KSTAT_DATA_UINT64
},
560 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
561 { "l2_evict_reading", KSTAT_DATA_UINT64
},
562 { "l2_evict_l1cached", KSTAT_DATA_UINT64
},
563 { "l2_free_on_write", KSTAT_DATA_UINT64
},
564 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
565 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
566 { "l2_io_error", KSTAT_DATA_UINT64
},
567 { "l2_size", KSTAT_DATA_UINT64
},
568 { "l2_asize", KSTAT_DATA_UINT64
},
569 { "l2_hdr_size", KSTAT_DATA_UINT64
},
570 { "l2_log_blk_writes", KSTAT_DATA_UINT64
},
571 { "l2_log_blk_avg_asize", KSTAT_DATA_UINT64
},
572 { "l2_log_blk_asize", KSTAT_DATA_UINT64
},
573 { "l2_log_blk_count", KSTAT_DATA_UINT64
},
574 { "l2_data_to_meta_ratio", KSTAT_DATA_UINT64
},
575 { "l2_rebuild_success", KSTAT_DATA_UINT64
},
576 { "l2_rebuild_unsupported", KSTAT_DATA_UINT64
},
577 { "l2_rebuild_io_errors", KSTAT_DATA_UINT64
},
578 { "l2_rebuild_dh_errors", KSTAT_DATA_UINT64
},
579 { "l2_rebuild_cksum_lb_errors", KSTAT_DATA_UINT64
},
580 { "l2_rebuild_lowmem", KSTAT_DATA_UINT64
},
581 { "l2_rebuild_size", KSTAT_DATA_UINT64
},
582 { "l2_rebuild_asize", KSTAT_DATA_UINT64
},
583 { "l2_rebuild_bufs", KSTAT_DATA_UINT64
},
584 { "l2_rebuild_bufs_precached", KSTAT_DATA_UINT64
},
585 { "l2_rebuild_log_blks", KSTAT_DATA_UINT64
},
586 { "memory_throttle_count", KSTAT_DATA_UINT64
},
587 { "memory_direct_count", KSTAT_DATA_UINT64
},
588 { "memory_indirect_count", KSTAT_DATA_UINT64
},
589 { "memory_all_bytes", KSTAT_DATA_UINT64
},
590 { "memory_free_bytes", KSTAT_DATA_UINT64
},
591 { "memory_available_bytes", KSTAT_DATA_INT64
},
592 { "arc_no_grow", KSTAT_DATA_UINT64
},
593 { "arc_tempreserve", KSTAT_DATA_UINT64
},
594 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
595 { "arc_prune", KSTAT_DATA_UINT64
},
596 { "arc_meta_used", KSTAT_DATA_UINT64
},
597 { "arc_meta_limit", KSTAT_DATA_UINT64
},
598 { "arc_dnode_limit", KSTAT_DATA_UINT64
},
599 { "arc_meta_max", KSTAT_DATA_UINT64
},
600 { "arc_meta_min", KSTAT_DATA_UINT64
},
601 { "async_upgrade_sync", KSTAT_DATA_UINT64
},
602 { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64
},
603 { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64
},
604 { "arc_need_free", KSTAT_DATA_UINT64
},
605 { "arc_sys_free", KSTAT_DATA_UINT64
},
606 { "arc_raw_size", KSTAT_DATA_UINT64
},
607 { "cached_only_in_progress", KSTAT_DATA_UINT64
},
608 { "abd_chunk_waste_size", KSTAT_DATA_UINT64
},
613 #define ARCSTAT_MAX(stat, val) { \
615 while ((val) > (m = arc_stats.stat.value.ui64) && \
616 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
621 * We define a macro to allow ARC hits/misses to be easily broken down by
622 * two separate conditions, giving a total of four different subtypes for
623 * each of hits and misses (so eight statistics total).
625 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
628 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
630 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
634 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
636 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
641 * This macro allows us to use kstats as floating averages. Each time we
642 * update this kstat, we first factor it and the update value by
643 * ARCSTAT_AVG_FACTOR to shrink the new value's contribution to the overall
644 * average. This macro assumes that integer loads and stores are atomic, but
645 * is not safe for multiple writers updating the kstat in parallel (only the
646 * last writer's update will remain).
648 #define ARCSTAT_F_AVG_FACTOR 3
649 #define ARCSTAT_F_AVG(stat, value) \
651 uint64_t x = ARCSTAT(stat); \
652 x = x - x / ARCSTAT_F_AVG_FACTOR + \
653 (value) / ARCSTAT_F_AVG_FACTOR; \
657 static kstat_t
*arc_ksp
;
660 * There are several ARC variables that are critical to export as kstats --
661 * but we don't want to have to grovel around in the kstat whenever we wish to
662 * manipulate them. For these variables, we therefore define them to be in
663 * terms of the statistic variable. This assures that we are not introducing
664 * the possibility of inconsistency by having shadow copies of the variables,
665 * while still allowing the code to be readable.
667 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
668 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
669 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
670 /* max size for dnodes */
671 #define arc_dnode_size_limit ARCSTAT(arcstat_dnode_limit)
672 #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */
673 #define arc_need_free ARCSTAT(arcstat_need_free) /* waiting to be evicted */
675 hrtime_t arc_growtime
;
676 list_t arc_prune_list
;
677 kmutex_t arc_prune_mtx
;
678 taskq_t
*arc_prune_taskq
;
680 #define GHOST_STATE(state) \
681 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
682 (state) == arc_l2c_only)
684 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
685 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
686 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
687 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
688 #define HDR_PRESCIENT_PREFETCH(hdr) \
689 ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH)
690 #define HDR_COMPRESSION_ENABLED(hdr) \
691 ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC)
693 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
694 #define HDR_L2_READING(hdr) \
695 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
696 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
697 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
698 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
699 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
700 #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED)
701 #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH)
702 #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA)
704 #define HDR_ISTYPE_METADATA(hdr) \
705 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
706 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
708 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
709 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
710 #define HDR_HAS_RABD(hdr) \
711 (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \
712 (hdr)->b_crypt_hdr.b_rabd != NULL)
713 #define HDR_ENCRYPTED(hdr) \
714 (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
715 #define HDR_AUTHENTICATED(hdr) \
716 (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
718 /* For storing compression mode in b_flags */
719 #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1)
721 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \
722 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS))
723 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \
724 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp));
726 #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL)
727 #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED)
728 #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED)
729 #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED)
735 #define HDR_FULL_CRYPT_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
736 #define HDR_FULL_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_crypt_hdr))
737 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
740 * Hash table routines
743 #define BUF_LOCKS 2048
744 typedef struct buf_hash_table
{
746 arc_buf_hdr_t
**ht_table
;
747 kmutex_t ht_locks
[BUF_LOCKS
] ____cacheline_aligned
;
750 static buf_hash_table_t buf_hash_table
;
752 #define BUF_HASH_INDEX(spa, dva, birth) \
753 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
754 #define BUF_HASH_LOCK(idx) (&buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
755 #define HDR_LOCK(hdr) \
756 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
758 uint64_t zfs_crc64_table
[256];
764 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
765 #define L2ARC_HEADROOM 2 /* num of writes */
768 * If we discover during ARC scan any buffers to be compressed, we boost
769 * our headroom for the next scanning cycle by this percentage multiple.
771 #define L2ARC_HEADROOM_BOOST 200
772 #define L2ARC_FEED_SECS 1 /* caching interval secs */
773 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
776 * We can feed L2ARC from two states of ARC buffers, mru and mfu,
777 * and each of the state has two types: data and metadata.
779 #define L2ARC_FEED_TYPES 4
781 /* L2ARC Performance Tunables */
782 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
783 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
784 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
785 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
786 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
787 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
788 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
789 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
790 int l2arc_norw
= B_FALSE
; /* no reads during writes */
791 static int l2arc_meta_percent
= 33; /* limit on headers size */
796 static list_t L2ARC_dev_list
; /* device list */
797 static list_t
*l2arc_dev_list
; /* device list pointer */
798 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
799 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
800 static list_t L2ARC_free_on_write
; /* free after write buf list */
801 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
802 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
803 static uint64_t l2arc_ndev
; /* number of devices */
805 typedef struct l2arc_read_callback
{
806 arc_buf_hdr_t
*l2rcb_hdr
; /* read header */
807 blkptr_t l2rcb_bp
; /* original blkptr */
808 zbookmark_phys_t l2rcb_zb
; /* original bookmark */
809 int l2rcb_flags
; /* original flags */
810 abd_t
*l2rcb_abd
; /* temporary buffer */
811 } l2arc_read_callback_t
;
813 typedef struct l2arc_data_free
{
814 /* protected by l2arc_free_on_write_mtx */
817 arc_buf_contents_t l2df_type
;
818 list_node_t l2df_list_node
;
821 typedef enum arc_fill_flags
{
822 ARC_FILL_LOCKED
= 1 << 0, /* hdr lock is held */
823 ARC_FILL_COMPRESSED
= 1 << 1, /* fill with compressed data */
824 ARC_FILL_ENCRYPTED
= 1 << 2, /* fill with encrypted data */
825 ARC_FILL_NOAUTH
= 1 << 3, /* don't attempt to authenticate */
826 ARC_FILL_IN_PLACE
= 1 << 4 /* fill in place (special case) */
829 typedef enum arc_ovf_level
{
830 ARC_OVF_NONE
, /* ARC within target size. */
831 ARC_OVF_SOME
, /* ARC is slightly overflowed. */
832 ARC_OVF_SEVERE
/* ARC is severely overflowed. */
835 static kmutex_t l2arc_feed_thr_lock
;
836 static kcondvar_t l2arc_feed_thr_cv
;
837 static uint8_t l2arc_thread_exit
;
839 static kmutex_t l2arc_rebuild_thr_lock
;
840 static kcondvar_t l2arc_rebuild_thr_cv
;
842 enum arc_hdr_alloc_flags
{
843 ARC_HDR_ALLOC_RDATA
= 0x1,
844 ARC_HDR_DO_ADAPT
= 0x2,
845 ARC_HDR_USE_RESERVE
= 0x4,
849 static abd_t
*arc_get_data_abd(arc_buf_hdr_t
*, uint64_t, void *, int);
850 static void *arc_get_data_buf(arc_buf_hdr_t
*, uint64_t, void *);
851 static void arc_get_data_impl(arc_buf_hdr_t
*, uint64_t, void *, int);
852 static void arc_free_data_abd(arc_buf_hdr_t
*, abd_t
*, uint64_t, void *);
853 static void arc_free_data_buf(arc_buf_hdr_t
*, void *, uint64_t, void *);
854 static void arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
);
855 static void arc_hdr_free_abd(arc_buf_hdr_t
*, boolean_t
);
856 static void arc_hdr_alloc_abd(arc_buf_hdr_t
*, int);
857 static void arc_access(arc_buf_hdr_t
*, kmutex_t
*);
858 static void arc_buf_watch(arc_buf_t
*);
860 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t
*);
861 static uint32_t arc_bufc_to_flags(arc_buf_contents_t
);
862 static inline void arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
863 static inline void arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
865 static boolean_t
l2arc_write_eligible(uint64_t, arc_buf_hdr_t
*);
866 static void l2arc_read_done(zio_t
*);
867 static void l2arc_do_free_on_write(void);
868 static void l2arc_hdr_arcstats_update(arc_buf_hdr_t
*hdr
, boolean_t incr
,
869 boolean_t state_only
);
871 #define l2arc_hdr_arcstats_increment(hdr) \
872 l2arc_hdr_arcstats_update((hdr), B_TRUE, B_FALSE)
873 #define l2arc_hdr_arcstats_decrement(hdr) \
874 l2arc_hdr_arcstats_update((hdr), B_FALSE, B_FALSE)
875 #define l2arc_hdr_arcstats_increment_state(hdr) \
876 l2arc_hdr_arcstats_update((hdr), B_TRUE, B_TRUE)
877 #define l2arc_hdr_arcstats_decrement_state(hdr) \
878 l2arc_hdr_arcstats_update((hdr), B_FALSE, B_TRUE)
881 * l2arc_exclude_special : A zfs module parameter that controls whether buffers
882 * present on special vdevs are eligibile for caching in L2ARC. If
883 * set to 1, exclude dbufs on special vdevs from being cached to
886 int l2arc_exclude_special
= 0;
889 * l2arc_mfuonly : A ZFS module parameter that controls whether only MFU
890 * metadata and data are cached from ARC into L2ARC.
892 static int l2arc_mfuonly
= 0;
896 * l2arc_trim_ahead : A ZFS module parameter that controls how much ahead of
897 * the current write size (l2arc_write_max) we should TRIM if we
898 * have filled the device. It is defined as a percentage of the
899 * write size. If set to 100 we trim twice the space required to
900 * accommodate upcoming writes. A minimum of 64MB will be trimmed.
901 * It also enables TRIM of the whole L2ARC device upon creation or
902 * addition to an existing pool or if the header of the device is
903 * invalid upon importing a pool or onlining a cache device. The
904 * default is 0, which disables TRIM on L2ARC altogether as it can
905 * put significant stress on the underlying storage devices. This
906 * will vary depending of how well the specific device handles
909 static unsigned long l2arc_trim_ahead
= 0;
912 * Performance tuning of L2ARC persistence:
914 * l2arc_rebuild_enabled : A ZFS module parameter that controls whether adding
915 * an L2ARC device (either at pool import or later) will attempt
916 * to rebuild L2ARC buffer contents.
917 * l2arc_rebuild_blocks_min_l2size : A ZFS module parameter that controls
918 * whether log blocks are written to the L2ARC device. If the L2ARC
919 * device is less than 1GB, the amount of data l2arc_evict()
920 * evicts is significant compared to the amount of restored L2ARC
921 * data. In this case do not write log blocks in L2ARC in order
922 * not to waste space.
924 static int l2arc_rebuild_enabled
= B_TRUE
;
925 static unsigned long l2arc_rebuild_blocks_min_l2size
= 1024 * 1024 * 1024;
927 /* L2ARC persistence rebuild control routines. */
928 void l2arc_rebuild_vdev(vdev_t
*vd
, boolean_t reopen
);
929 static void l2arc_dev_rebuild_thread(void *arg
);
930 static int l2arc_rebuild(l2arc_dev_t
*dev
);
932 /* L2ARC persistence read I/O routines. */
933 static int l2arc_dev_hdr_read(l2arc_dev_t
*dev
);
934 static int l2arc_log_blk_read(l2arc_dev_t
*dev
,
935 const l2arc_log_blkptr_t
*this_lp
, const l2arc_log_blkptr_t
*next_lp
,
936 l2arc_log_blk_phys_t
*this_lb
, l2arc_log_blk_phys_t
*next_lb
,
937 zio_t
*this_io
, zio_t
**next_io
);
938 static zio_t
*l2arc_log_blk_fetch(vdev_t
*vd
,
939 const l2arc_log_blkptr_t
*lp
, l2arc_log_blk_phys_t
*lb
);
940 static void l2arc_log_blk_fetch_abort(zio_t
*zio
);
942 /* L2ARC persistence block restoration routines. */
943 static void l2arc_log_blk_restore(l2arc_dev_t
*dev
,
944 const l2arc_log_blk_phys_t
*lb
, uint64_t lb_asize
);
945 static void l2arc_hdr_restore(const l2arc_log_ent_phys_t
*le
,
948 /* L2ARC persistence write I/O routines. */
949 static void l2arc_log_blk_commit(l2arc_dev_t
*dev
, zio_t
*pio
,
950 l2arc_write_callback_t
*cb
);
952 /* L2ARC persistence auxiliary routines. */
953 boolean_t
l2arc_log_blkptr_valid(l2arc_dev_t
*dev
,
954 const l2arc_log_blkptr_t
*lbp
);
955 static boolean_t
l2arc_log_blk_insert(l2arc_dev_t
*dev
,
956 const arc_buf_hdr_t
*ab
);
957 boolean_t
l2arc_range_check_overlap(uint64_t bottom
,
958 uint64_t top
, uint64_t check
);
959 static void l2arc_blk_fetch_done(zio_t
*zio
);
960 static inline uint64_t
961 l2arc_log_blk_overhead(uint64_t write_sz
, l2arc_dev_t
*dev
);
964 * We use Cityhash for this. It's fast, and has good hash properties without
965 * requiring any large static buffers.
968 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
970 return (cityhash4(spa
, dva
->dva_word
[0], dva
->dva_word
[1], birth
));
973 #define HDR_EMPTY(hdr) \
974 ((hdr)->b_dva.dva_word[0] == 0 && \
975 (hdr)->b_dva.dva_word[1] == 0)
977 #define HDR_EMPTY_OR_LOCKED(hdr) \
978 (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr)))
980 #define HDR_EQUAL(spa, dva, birth, hdr) \
981 ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
982 ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
983 ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa)
986 buf_discard_identity(arc_buf_hdr_t
*hdr
)
988 hdr
->b_dva
.dva_word
[0] = 0;
989 hdr
->b_dva
.dva_word
[1] = 0;
993 static arc_buf_hdr_t
*
994 buf_hash_find(uint64_t spa
, const blkptr_t
*bp
, kmutex_t
**lockp
)
996 const dva_t
*dva
= BP_IDENTITY(bp
);
997 uint64_t birth
= BP_PHYSICAL_BIRTH(bp
);
998 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
999 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1002 mutex_enter(hash_lock
);
1003 for (hdr
= buf_hash_table
.ht_table
[idx
]; hdr
!= NULL
;
1004 hdr
= hdr
->b_hash_next
) {
1005 if (HDR_EQUAL(spa
, dva
, birth
, hdr
)) {
1010 mutex_exit(hash_lock
);
1016 * Insert an entry into the hash table. If there is already an element
1017 * equal to elem in the hash table, then the already existing element
1018 * will be returned and the new element will not be inserted.
1019 * Otherwise returns NULL.
1020 * If lockp == NULL, the caller is assumed to already hold the hash lock.
1022 static arc_buf_hdr_t
*
1023 buf_hash_insert(arc_buf_hdr_t
*hdr
, kmutex_t
**lockp
)
1025 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1026 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1027 arc_buf_hdr_t
*fhdr
;
1030 ASSERT(!DVA_IS_EMPTY(&hdr
->b_dva
));
1031 ASSERT(hdr
->b_birth
!= 0);
1032 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1034 if (lockp
!= NULL
) {
1036 mutex_enter(hash_lock
);
1038 ASSERT(MUTEX_HELD(hash_lock
));
1041 for (fhdr
= buf_hash_table
.ht_table
[idx
], i
= 0; fhdr
!= NULL
;
1042 fhdr
= fhdr
->b_hash_next
, i
++) {
1043 if (HDR_EQUAL(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
, fhdr
))
1047 hdr
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
1048 buf_hash_table
.ht_table
[idx
] = hdr
;
1049 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1051 /* collect some hash table performance data */
1053 ARCSTAT_BUMP(arcstat_hash_collisions
);
1055 ARCSTAT_BUMP(arcstat_hash_chains
);
1057 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
1059 uint64_t he
= atomic_inc_64_nv(
1060 &arc_stats
.arcstat_hash_elements
.value
.ui64
);
1061 ARCSTAT_MAX(arcstat_hash_elements_max
, he
);
1067 buf_hash_remove(arc_buf_hdr_t
*hdr
)
1069 arc_buf_hdr_t
*fhdr
, **hdrp
;
1070 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1072 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
1073 ASSERT(HDR_IN_HASH_TABLE(hdr
));
1075 hdrp
= &buf_hash_table
.ht_table
[idx
];
1076 while ((fhdr
= *hdrp
) != hdr
) {
1077 ASSERT3P(fhdr
, !=, NULL
);
1078 hdrp
= &fhdr
->b_hash_next
;
1080 *hdrp
= hdr
->b_hash_next
;
1081 hdr
->b_hash_next
= NULL
;
1082 arc_hdr_clear_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1084 /* collect some hash table performance data */
1085 atomic_dec_64(&arc_stats
.arcstat_hash_elements
.value
.ui64
);
1087 if (buf_hash_table
.ht_table
[idx
] &&
1088 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
1089 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
1093 * Global data structures and functions for the buf kmem cache.
1096 static kmem_cache_t
*hdr_full_cache
;
1097 static kmem_cache_t
*hdr_full_crypt_cache
;
1098 static kmem_cache_t
*hdr_l2only_cache
;
1099 static kmem_cache_t
*buf_cache
;
1104 #if defined(_KERNEL)
1106 * Large allocations which do not require contiguous pages
1107 * should be using vmem_free() in the linux kernel\
1109 vmem_free(buf_hash_table
.ht_table
,
1110 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1112 kmem_free(buf_hash_table
.ht_table
,
1113 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1115 for (int i
= 0; i
< BUF_LOCKS
; i
++)
1116 mutex_destroy(BUF_HASH_LOCK(i
));
1117 kmem_cache_destroy(hdr_full_cache
);
1118 kmem_cache_destroy(hdr_full_crypt_cache
);
1119 kmem_cache_destroy(hdr_l2only_cache
);
1120 kmem_cache_destroy(buf_cache
);
1124 * Constructor callback - called when the cache is empty
1125 * and a new buf is requested.
1128 hdr_full_cons(void *vbuf
, void *unused
, int kmflag
)
1130 (void) unused
, (void) kmflag
;
1131 arc_buf_hdr_t
*hdr
= vbuf
;
1133 bzero(hdr
, HDR_FULL_SIZE
);
1134 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
1135 cv_init(&hdr
->b_l1hdr
.b_cv
, NULL
, CV_DEFAULT
, NULL
);
1136 zfs_refcount_create(&hdr
->b_l1hdr
.b_refcnt
);
1137 mutex_init(&hdr
->b_l1hdr
.b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1138 list_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1139 list_link_init(&hdr
->b_l2hdr
.b_l2node
);
1140 multilist_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1141 arc_space_consume(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1147 hdr_full_crypt_cons(void *vbuf
, void *unused
, int kmflag
)
1150 arc_buf_hdr_t
*hdr
= vbuf
;
1152 hdr_full_cons(vbuf
, unused
, kmflag
);
1153 bzero(&hdr
->b_crypt_hdr
, sizeof (hdr
->b_crypt_hdr
));
1154 arc_space_consume(sizeof (hdr
->b_crypt_hdr
), ARC_SPACE_HDRS
);
1160 hdr_l2only_cons(void *vbuf
, void *unused
, int kmflag
)
1162 (void) unused
, (void) kmflag
;
1163 arc_buf_hdr_t
*hdr
= vbuf
;
1165 bzero(hdr
, HDR_L2ONLY_SIZE
);
1166 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1172 buf_cons(void *vbuf
, void *unused
, int kmflag
)
1174 (void) unused
, (void) kmflag
;
1175 arc_buf_t
*buf
= vbuf
;
1177 bzero(buf
, sizeof (arc_buf_t
));
1178 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1179 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1185 * Destructor callback - called when a cached buf is
1186 * no longer required.
1189 hdr_full_dest(void *vbuf
, void *unused
)
1192 arc_buf_hdr_t
*hdr
= vbuf
;
1194 ASSERT(HDR_EMPTY(hdr
));
1195 cv_destroy(&hdr
->b_l1hdr
.b_cv
);
1196 zfs_refcount_destroy(&hdr
->b_l1hdr
.b_refcnt
);
1197 mutex_destroy(&hdr
->b_l1hdr
.b_freeze_lock
);
1198 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1199 arc_space_return(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1203 hdr_full_crypt_dest(void *vbuf
, void *unused
)
1206 arc_buf_hdr_t
*hdr
= vbuf
;
1208 hdr_full_dest(vbuf
, unused
);
1209 arc_space_return(sizeof (hdr
->b_crypt_hdr
), ARC_SPACE_HDRS
);
1213 hdr_l2only_dest(void *vbuf
, void *unused
)
1216 arc_buf_hdr_t
*hdr
= vbuf
;
1218 ASSERT(HDR_EMPTY(hdr
));
1219 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1223 buf_dest(void *vbuf
, void *unused
)
1226 arc_buf_t
*buf
= vbuf
;
1228 mutex_destroy(&buf
->b_evict_lock
);
1229 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1235 uint64_t *ct
= NULL
;
1236 uint64_t hsize
= 1ULL << 12;
1240 * The hash table is big enough to fill all of physical memory
1241 * with an average block size of zfs_arc_average_blocksize (default 8K).
1242 * By default, the table will take up
1243 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1245 while (hsize
* zfs_arc_average_blocksize
< arc_all_memory())
1248 buf_hash_table
.ht_mask
= hsize
- 1;
1249 #if defined(_KERNEL)
1251 * Large allocations which do not require contiguous pages
1252 * should be using vmem_alloc() in the linux kernel
1254 buf_hash_table
.ht_table
=
1255 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1257 buf_hash_table
.ht_table
=
1258 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1260 if (buf_hash_table
.ht_table
== NULL
) {
1261 ASSERT(hsize
> (1ULL << 8));
1266 hdr_full_cache
= kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE
,
1267 0, hdr_full_cons
, hdr_full_dest
, NULL
, NULL
, NULL
, 0);
1268 hdr_full_crypt_cache
= kmem_cache_create("arc_buf_hdr_t_full_crypt",
1269 HDR_FULL_CRYPT_SIZE
, 0, hdr_full_crypt_cons
, hdr_full_crypt_dest
,
1270 NULL
, NULL
, NULL
, 0);
1271 hdr_l2only_cache
= kmem_cache_create("arc_buf_hdr_t_l2only",
1272 HDR_L2ONLY_SIZE
, 0, hdr_l2only_cons
, hdr_l2only_dest
, NULL
,
1274 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1275 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1277 for (i
= 0; i
< 256; i
++)
1278 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1279 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1281 for (i
= 0; i
< BUF_LOCKS
; i
++)
1282 mutex_init(BUF_HASH_LOCK(i
), NULL
, MUTEX_DEFAULT
, NULL
);
1285 #define ARC_MINTIME (hz>>4) /* 62 ms */
1288 * This is the size that the buf occupies in memory. If the buf is compressed,
1289 * it will correspond to the compressed size. You should use this method of
1290 * getting the buf size unless you explicitly need the logical size.
1293 arc_buf_size(arc_buf_t
*buf
)
1295 return (ARC_BUF_COMPRESSED(buf
) ?
1296 HDR_GET_PSIZE(buf
->b_hdr
) : HDR_GET_LSIZE(buf
->b_hdr
));
1300 arc_buf_lsize(arc_buf_t
*buf
)
1302 return (HDR_GET_LSIZE(buf
->b_hdr
));
1306 * This function will return B_TRUE if the buffer is encrypted in memory.
1307 * This buffer can be decrypted by calling arc_untransform().
1310 arc_is_encrypted(arc_buf_t
*buf
)
1312 return (ARC_BUF_ENCRYPTED(buf
) != 0);
1316 * Returns B_TRUE if the buffer represents data that has not had its MAC
1320 arc_is_unauthenticated(arc_buf_t
*buf
)
1322 return (HDR_NOAUTH(buf
->b_hdr
) != 0);
1326 arc_get_raw_params(arc_buf_t
*buf
, boolean_t
*byteorder
, uint8_t *salt
,
1327 uint8_t *iv
, uint8_t *mac
)
1329 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1331 ASSERT(HDR_PROTECTED(hdr
));
1333 bcopy(hdr
->b_crypt_hdr
.b_salt
, salt
, ZIO_DATA_SALT_LEN
);
1334 bcopy(hdr
->b_crypt_hdr
.b_iv
, iv
, ZIO_DATA_IV_LEN
);
1335 bcopy(hdr
->b_crypt_hdr
.b_mac
, mac
, ZIO_DATA_MAC_LEN
);
1336 *byteorder
= (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
1337 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
1341 * Indicates how this buffer is compressed in memory. If it is not compressed
1342 * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with
1343 * arc_untransform() as long as it is also unencrypted.
1346 arc_get_compression(arc_buf_t
*buf
)
1348 return (ARC_BUF_COMPRESSED(buf
) ?
1349 HDR_GET_COMPRESS(buf
->b_hdr
) : ZIO_COMPRESS_OFF
);
1353 * Return the compression algorithm used to store this data in the ARC. If ARC
1354 * compression is enabled or this is an encrypted block, this will be the same
1355 * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF.
1357 static inline enum zio_compress
1358 arc_hdr_get_compress(arc_buf_hdr_t
*hdr
)
1360 return (HDR_COMPRESSION_ENABLED(hdr
) ?
1361 HDR_GET_COMPRESS(hdr
) : ZIO_COMPRESS_OFF
);
1365 arc_get_complevel(arc_buf_t
*buf
)
1367 return (buf
->b_hdr
->b_complevel
);
1370 static inline boolean_t
1371 arc_buf_is_shared(arc_buf_t
*buf
)
1373 boolean_t shared
= (buf
->b_data
!= NULL
&&
1374 buf
->b_hdr
->b_l1hdr
.b_pabd
!= NULL
&&
1375 abd_is_linear(buf
->b_hdr
->b_l1hdr
.b_pabd
) &&
1376 buf
->b_data
== abd_to_buf(buf
->b_hdr
->b_l1hdr
.b_pabd
));
1377 IMPLY(shared
, HDR_SHARED_DATA(buf
->b_hdr
));
1378 IMPLY(shared
, ARC_BUF_SHARED(buf
));
1379 IMPLY(shared
, ARC_BUF_COMPRESSED(buf
) || ARC_BUF_LAST(buf
));
1382 * It would be nice to assert arc_can_share() too, but the "hdr isn't
1383 * already being shared" requirement prevents us from doing that.
1390 * Free the checksum associated with this header. If there is no checksum, this
1394 arc_cksum_free(arc_buf_hdr_t
*hdr
)
1396 ASSERT(HDR_HAS_L1HDR(hdr
));
1398 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1399 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
) {
1400 kmem_free(hdr
->b_l1hdr
.b_freeze_cksum
, sizeof (zio_cksum_t
));
1401 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
1403 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1407 * Return true iff at least one of the bufs on hdr is not compressed.
1408 * Encrypted buffers count as compressed.
1411 arc_hdr_has_uncompressed_buf(arc_buf_hdr_t
*hdr
)
1413 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY_OR_LOCKED(hdr
));
1415 for (arc_buf_t
*b
= hdr
->b_l1hdr
.b_buf
; b
!= NULL
; b
= b
->b_next
) {
1416 if (!ARC_BUF_COMPRESSED(b
)) {
1425 * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data
1426 * matches the checksum that is stored in the hdr. If there is no checksum,
1427 * or if the buf is compressed, this is a no-op.
1430 arc_cksum_verify(arc_buf_t
*buf
)
1432 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1435 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1438 if (ARC_BUF_COMPRESSED(buf
))
1441 ASSERT(HDR_HAS_L1HDR(hdr
));
1443 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1445 if (hdr
->b_l1hdr
.b_freeze_cksum
== NULL
|| HDR_IO_ERROR(hdr
)) {
1446 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1450 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
, &zc
);
1451 if (!ZIO_CHECKSUM_EQUAL(*hdr
->b_l1hdr
.b_freeze_cksum
, zc
))
1452 panic("buffer modified while frozen!");
1453 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1457 * This function makes the assumption that data stored in the L2ARC
1458 * will be transformed exactly as it is in the main pool. Because of
1459 * this we can verify the checksum against the reading process's bp.
1462 arc_cksum_is_equal(arc_buf_hdr_t
*hdr
, zio_t
*zio
)
1464 ASSERT(!BP_IS_EMBEDDED(zio
->io_bp
));
1465 VERIFY3U(BP_GET_PSIZE(zio
->io_bp
), ==, HDR_GET_PSIZE(hdr
));
1468 * Block pointers always store the checksum for the logical data.
1469 * If the block pointer has the gang bit set, then the checksum
1470 * it represents is for the reconstituted data and not for an
1471 * individual gang member. The zio pipeline, however, must be able to
1472 * determine the checksum of each of the gang constituents so it
1473 * treats the checksum comparison differently than what we need
1474 * for l2arc blocks. This prevents us from using the
1475 * zio_checksum_error() interface directly. Instead we must call the
1476 * zio_checksum_error_impl() so that we can ensure the checksum is
1477 * generated using the correct checksum algorithm and accounts for the
1478 * logical I/O size and not just a gang fragment.
1480 return (zio_checksum_error_impl(zio
->io_spa
, zio
->io_bp
,
1481 BP_GET_CHECKSUM(zio
->io_bp
), zio
->io_abd
, zio
->io_size
,
1482 zio
->io_offset
, NULL
) == 0);
1486 * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a
1487 * checksum and attaches it to the buf's hdr so that we can ensure that the buf
1488 * isn't modified later on. If buf is compressed or there is already a checksum
1489 * on the hdr, this is a no-op (we only checksum uncompressed bufs).
1492 arc_cksum_compute(arc_buf_t
*buf
)
1494 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1496 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1499 ASSERT(HDR_HAS_L1HDR(hdr
));
1501 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1502 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
|| ARC_BUF_COMPRESSED(buf
)) {
1503 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1507 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
1508 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1509 hdr
->b_l1hdr
.b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1511 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
,
1512 hdr
->b_l1hdr
.b_freeze_cksum
);
1513 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1519 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1521 (void) sig
, (void) unused
;
1522 panic("Got SIGSEGV at address: 0x%lx\n", (long)si
->si_addr
);
1527 arc_buf_unwatch(arc_buf_t
*buf
)
1531 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1532 PROT_READ
| PROT_WRITE
));
1540 arc_buf_watch(arc_buf_t
*buf
)
1544 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1551 static arc_buf_contents_t
1552 arc_buf_type(arc_buf_hdr_t
*hdr
)
1554 arc_buf_contents_t type
;
1555 if (HDR_ISTYPE_METADATA(hdr
)) {
1556 type
= ARC_BUFC_METADATA
;
1558 type
= ARC_BUFC_DATA
;
1560 VERIFY3U(hdr
->b_type
, ==, type
);
1565 arc_is_metadata(arc_buf_t
*buf
)
1567 return (HDR_ISTYPE_METADATA(buf
->b_hdr
) != 0);
1571 arc_bufc_to_flags(arc_buf_contents_t type
)
1575 /* metadata field is 0 if buffer contains normal data */
1577 case ARC_BUFC_METADATA
:
1578 return (ARC_FLAG_BUFC_METADATA
);
1582 panic("undefined ARC buffer type!");
1583 return ((uint32_t)-1);
1587 arc_buf_thaw(arc_buf_t
*buf
)
1589 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1591 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
1592 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1594 arc_cksum_verify(buf
);
1597 * Compressed buffers do not manipulate the b_freeze_cksum.
1599 if (ARC_BUF_COMPRESSED(buf
))
1602 ASSERT(HDR_HAS_L1HDR(hdr
));
1603 arc_cksum_free(hdr
);
1604 arc_buf_unwatch(buf
);
1608 arc_buf_freeze(arc_buf_t
*buf
)
1610 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1613 if (ARC_BUF_COMPRESSED(buf
))
1616 ASSERT(HDR_HAS_L1HDR(buf
->b_hdr
));
1617 arc_cksum_compute(buf
);
1621 * The arc_buf_hdr_t's b_flags should never be modified directly. Instead,
1622 * the following functions should be used to ensure that the flags are
1623 * updated in a thread-safe way. When manipulating the flags either
1624 * the hash_lock must be held or the hdr must be undiscoverable. This
1625 * ensures that we're not racing with any other threads when updating
1629 arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1631 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1632 hdr
->b_flags
|= flags
;
1636 arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1638 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1639 hdr
->b_flags
&= ~flags
;
1643 * Setting the compression bits in the arc_buf_hdr_t's b_flags is
1644 * done in a special way since we have to clear and set bits
1645 * at the same time. Consumers that wish to set the compression bits
1646 * must use this function to ensure that the flags are updated in
1647 * thread-safe manner.
1650 arc_hdr_set_compress(arc_buf_hdr_t
*hdr
, enum zio_compress cmp
)
1652 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1655 * Holes and embedded blocks will always have a psize = 0 so
1656 * we ignore the compression of the blkptr and set the
1657 * want to uncompress them. Mark them as uncompressed.
1659 if (!zfs_compressed_arc_enabled
|| HDR_GET_PSIZE(hdr
) == 0) {
1660 arc_hdr_clear_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1661 ASSERT(!HDR_COMPRESSION_ENABLED(hdr
));
1663 arc_hdr_set_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1664 ASSERT(HDR_COMPRESSION_ENABLED(hdr
));
1667 HDR_SET_COMPRESS(hdr
, cmp
);
1668 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, cmp
);
1672 * Looks for another buf on the same hdr which has the data decompressed, copies
1673 * from it, and returns true. If no such buf exists, returns false.
1676 arc_buf_try_copy_decompressed_data(arc_buf_t
*buf
)
1678 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1679 boolean_t copied
= B_FALSE
;
1681 ASSERT(HDR_HAS_L1HDR(hdr
));
1682 ASSERT3P(buf
->b_data
, !=, NULL
);
1683 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1685 for (arc_buf_t
*from
= hdr
->b_l1hdr
.b_buf
; from
!= NULL
;
1686 from
= from
->b_next
) {
1687 /* can't use our own data buffer */
1692 if (!ARC_BUF_COMPRESSED(from
)) {
1693 bcopy(from
->b_data
, buf
->b_data
, arc_buf_size(buf
));
1700 * There were no decompressed bufs, so there should not be a
1701 * checksum on the hdr either.
1703 if (zfs_flags
& ZFS_DEBUG_MODIFY
)
1704 EQUIV(!copied
, hdr
->b_l1hdr
.b_freeze_cksum
== NULL
);
1710 * Allocates an ARC buf header that's in an evicted & L2-cached state.
1711 * This is used during l2arc reconstruction to make empty ARC buffers
1712 * which circumvent the regular disk->arc->l2arc path and instead come
1713 * into being in the reverse order, i.e. l2arc->arc.
1715 static arc_buf_hdr_t
*
1716 arc_buf_alloc_l2only(size_t size
, arc_buf_contents_t type
, l2arc_dev_t
*dev
,
1717 dva_t dva
, uint64_t daddr
, int32_t psize
, uint64_t birth
,
1718 enum zio_compress compress
, uint8_t complevel
, boolean_t
protected,
1719 boolean_t prefetch
, arc_state_type_t arcs_state
)
1724 hdr
= kmem_cache_alloc(hdr_l2only_cache
, KM_SLEEP
);
1725 hdr
->b_birth
= birth
;
1728 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L2HDR
);
1729 HDR_SET_LSIZE(hdr
, size
);
1730 HDR_SET_PSIZE(hdr
, psize
);
1731 arc_hdr_set_compress(hdr
, compress
);
1732 hdr
->b_complevel
= complevel
;
1734 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
1736 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
1737 hdr
->b_spa
= spa_load_guid(dev
->l2ad_vdev
->vdev_spa
);
1741 hdr
->b_l2hdr
.b_dev
= dev
;
1742 hdr
->b_l2hdr
.b_daddr
= daddr
;
1743 hdr
->b_l2hdr
.b_arcs_state
= arcs_state
;
1749 * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t.
1752 arc_hdr_size(arc_buf_hdr_t
*hdr
)
1756 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
1757 HDR_GET_PSIZE(hdr
) > 0) {
1758 size
= HDR_GET_PSIZE(hdr
);
1760 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, 0);
1761 size
= HDR_GET_LSIZE(hdr
);
1767 arc_hdr_authenticate(arc_buf_hdr_t
*hdr
, spa_t
*spa
, uint64_t dsobj
)
1771 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
1772 uint64_t psize
= HDR_GET_PSIZE(hdr
);
1773 void *tmpbuf
= NULL
;
1774 abd_t
*abd
= hdr
->b_l1hdr
.b_pabd
;
1776 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1777 ASSERT(HDR_AUTHENTICATED(hdr
));
1778 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1781 * The MAC is calculated on the compressed data that is stored on disk.
1782 * However, if compressed arc is disabled we will only have the
1783 * decompressed data available to us now. Compress it into a temporary
1784 * abd so we can verify the MAC. The performance overhead of this will
1785 * be relatively low, since most objects in an encrypted objset will
1786 * be encrypted (instead of authenticated) anyway.
1788 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1789 !HDR_COMPRESSION_ENABLED(hdr
)) {
1790 tmpbuf
= zio_buf_alloc(lsize
);
1791 abd
= abd_get_from_buf(tmpbuf
, lsize
);
1792 abd_take_ownership_of_buf(abd
, B_TRUE
);
1793 csize
= zio_compress_data(HDR_GET_COMPRESS(hdr
),
1794 hdr
->b_l1hdr
.b_pabd
, tmpbuf
, lsize
, hdr
->b_complevel
);
1795 ASSERT3U(csize
, <=, psize
);
1796 abd_zero_off(abd
, csize
, psize
- csize
);
1800 * Authentication is best effort. We authenticate whenever the key is
1801 * available. If we succeed we clear ARC_FLAG_NOAUTH.
1803 if (hdr
->b_crypt_hdr
.b_ot
== DMU_OT_OBJSET
) {
1804 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, ZIO_COMPRESS_OFF
);
1805 ASSERT3U(lsize
, ==, psize
);
1806 ret
= spa_do_crypt_objset_mac_abd(B_FALSE
, spa
, dsobj
, abd
,
1807 psize
, hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1809 ret
= spa_do_crypt_mac_abd(B_FALSE
, spa
, dsobj
, abd
, psize
,
1810 hdr
->b_crypt_hdr
.b_mac
);
1814 arc_hdr_clear_flags(hdr
, ARC_FLAG_NOAUTH
);
1815 else if (ret
!= ENOENT
)
1831 * This function will take a header that only has raw encrypted data in
1832 * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in
1833 * b_l1hdr.b_pabd. If designated in the header flags, this function will
1834 * also decompress the data.
1837 arc_hdr_decrypt(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
)
1842 boolean_t no_crypt
= B_FALSE
;
1843 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1845 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1846 ASSERT(HDR_ENCRYPTED(hdr
));
1848 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
);
1850 ret
= spa_do_crypt_abd(B_FALSE
, spa
, zb
, hdr
->b_crypt_hdr
.b_ot
,
1851 B_FALSE
, bswap
, hdr
->b_crypt_hdr
.b_salt
, hdr
->b_crypt_hdr
.b_iv
,
1852 hdr
->b_crypt_hdr
.b_mac
, HDR_GET_PSIZE(hdr
), hdr
->b_l1hdr
.b_pabd
,
1853 hdr
->b_crypt_hdr
.b_rabd
, &no_crypt
);
1858 abd_copy(hdr
->b_l1hdr
.b_pabd
, hdr
->b_crypt_hdr
.b_rabd
,
1859 HDR_GET_PSIZE(hdr
));
1863 * If this header has disabled arc compression but the b_pabd is
1864 * compressed after decrypting it, we need to decompress the newly
1867 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1868 !HDR_COMPRESSION_ENABLED(hdr
)) {
1870 * We want to make sure that we are correctly honoring the
1871 * zfs_abd_scatter_enabled setting, so we allocate an abd here
1872 * and then loan a buffer from it, rather than allocating a
1873 * linear buffer and wrapping it in an abd later.
1875 cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
,
1877 tmp
= abd_borrow_buf(cabd
, arc_hdr_size(hdr
));
1879 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
1880 hdr
->b_l1hdr
.b_pabd
, tmp
, HDR_GET_PSIZE(hdr
),
1881 HDR_GET_LSIZE(hdr
), &hdr
->b_complevel
);
1883 abd_return_buf(cabd
, tmp
, arc_hdr_size(hdr
));
1887 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
1888 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
1889 arc_hdr_size(hdr
), hdr
);
1890 hdr
->b_l1hdr
.b_pabd
= cabd
;
1896 arc_hdr_free_abd(hdr
, B_FALSE
);
1898 arc_free_data_buf(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
1904 * This function is called during arc_buf_fill() to prepare the header's
1905 * abd plaintext pointer for use. This involves authenticated protected
1906 * data and decrypting encrypted data into the plaintext abd.
1909 arc_fill_hdr_crypt(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, spa_t
*spa
,
1910 const zbookmark_phys_t
*zb
, boolean_t noauth
)
1914 ASSERT(HDR_PROTECTED(hdr
));
1916 if (hash_lock
!= NULL
)
1917 mutex_enter(hash_lock
);
1919 if (HDR_NOAUTH(hdr
) && !noauth
) {
1921 * The caller requested authenticated data but our data has
1922 * not been authenticated yet. Verify the MAC now if we can.
1924 ret
= arc_hdr_authenticate(hdr
, spa
, zb
->zb_objset
);
1927 } else if (HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
== NULL
) {
1929 * If we only have the encrypted version of the data, but the
1930 * unencrypted version was requested we take this opportunity
1931 * to store the decrypted version in the header for future use.
1933 ret
= arc_hdr_decrypt(hdr
, spa
, zb
);
1938 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1940 if (hash_lock
!= NULL
)
1941 mutex_exit(hash_lock
);
1946 if (hash_lock
!= NULL
)
1947 mutex_exit(hash_lock
);
1953 * This function is used by the dbuf code to decrypt bonus buffers in place.
1954 * The dbuf code itself doesn't have any locking for decrypting a shared dnode
1955 * block, so we use the hash lock here to protect against concurrent calls to
1959 arc_buf_untransform_in_place(arc_buf_t
*buf
)
1961 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1963 ASSERT(HDR_ENCRYPTED(hdr
));
1964 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
1965 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1966 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1968 zio_crypt_copy_dnode_bonus(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
1970 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
1971 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
1972 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
1976 * Given a buf that has a data buffer attached to it, this function will
1977 * efficiently fill the buf with data of the specified compression setting from
1978 * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr
1979 * are already sharing a data buf, no copy is performed.
1981 * If the buf is marked as compressed but uncompressed data was requested, this
1982 * will allocate a new data buffer for the buf, remove that flag, and fill the
1983 * buf with uncompressed data. You can't request a compressed buf on a hdr with
1984 * uncompressed data, and (since we haven't added support for it yet) if you
1985 * want compressed data your buf must already be marked as compressed and have
1986 * the correct-sized data buffer.
1989 arc_buf_fill(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
1990 arc_fill_flags_t flags
)
1993 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1994 boolean_t hdr_compressed
=
1995 (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
1996 boolean_t compressed
= (flags
& ARC_FILL_COMPRESSED
) != 0;
1997 boolean_t encrypted
= (flags
& ARC_FILL_ENCRYPTED
) != 0;
1998 dmu_object_byteswap_t bswap
= hdr
->b_l1hdr
.b_byteswap
;
1999 kmutex_t
*hash_lock
= (flags
& ARC_FILL_LOCKED
) ? NULL
: HDR_LOCK(hdr
);
2001 ASSERT3P(buf
->b_data
, !=, NULL
);
2002 IMPLY(compressed
, hdr_compressed
|| ARC_BUF_ENCRYPTED(buf
));
2003 IMPLY(compressed
, ARC_BUF_COMPRESSED(buf
));
2004 IMPLY(encrypted
, HDR_ENCRYPTED(hdr
));
2005 IMPLY(encrypted
, ARC_BUF_ENCRYPTED(buf
));
2006 IMPLY(encrypted
, ARC_BUF_COMPRESSED(buf
));
2007 IMPLY(encrypted
, !ARC_BUF_SHARED(buf
));
2010 * If the caller wanted encrypted data we just need to copy it from
2011 * b_rabd and potentially byteswap it. We won't be able to do any
2012 * further transforms on it.
2015 ASSERT(HDR_HAS_RABD(hdr
));
2016 abd_copy_to_buf(buf
->b_data
, hdr
->b_crypt_hdr
.b_rabd
,
2017 HDR_GET_PSIZE(hdr
));
2022 * Adjust encrypted and authenticated headers to accommodate
2023 * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are
2024 * allowed to fail decryption due to keys not being loaded
2025 * without being marked as an IO error.
2027 if (HDR_PROTECTED(hdr
)) {
2028 error
= arc_fill_hdr_crypt(hdr
, hash_lock
, spa
,
2029 zb
, !!(flags
& ARC_FILL_NOAUTH
));
2030 if (error
== EACCES
&& (flags
& ARC_FILL_IN_PLACE
) != 0) {
2032 } else if (error
!= 0) {
2033 if (hash_lock
!= NULL
)
2034 mutex_enter(hash_lock
);
2035 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2036 if (hash_lock
!= NULL
)
2037 mutex_exit(hash_lock
);
2043 * There is a special case here for dnode blocks which are
2044 * decrypting their bonus buffers. These blocks may request to
2045 * be decrypted in-place. This is necessary because there may
2046 * be many dnodes pointing into this buffer and there is
2047 * currently no method to synchronize replacing the backing
2048 * b_data buffer and updating all of the pointers. Here we use
2049 * the hash lock to ensure there are no races. If the need
2050 * arises for other types to be decrypted in-place, they must
2051 * add handling here as well.
2053 if ((flags
& ARC_FILL_IN_PLACE
) != 0) {
2054 ASSERT(!hdr_compressed
);
2055 ASSERT(!compressed
);
2058 if (HDR_ENCRYPTED(hdr
) && ARC_BUF_ENCRYPTED(buf
)) {
2059 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
2061 if (hash_lock
!= NULL
)
2062 mutex_enter(hash_lock
);
2063 arc_buf_untransform_in_place(buf
);
2064 if (hash_lock
!= NULL
)
2065 mutex_exit(hash_lock
);
2067 /* Compute the hdr's checksum if necessary */
2068 arc_cksum_compute(buf
);
2074 if (hdr_compressed
== compressed
) {
2075 if (!arc_buf_is_shared(buf
)) {
2076 abd_copy_to_buf(buf
->b_data
, hdr
->b_l1hdr
.b_pabd
,
2080 ASSERT(hdr_compressed
);
2081 ASSERT(!compressed
);
2084 * If the buf is sharing its data with the hdr, unlink it and
2085 * allocate a new data buffer for the buf.
2087 if (arc_buf_is_shared(buf
)) {
2088 ASSERT(ARC_BUF_COMPRESSED(buf
));
2090 /* We need to give the buf its own b_data */
2091 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
2093 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2094 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2096 /* Previously overhead was 0; just add new overhead */
2097 ARCSTAT_INCR(arcstat_overhead_size
, HDR_GET_LSIZE(hdr
));
2098 } else if (ARC_BUF_COMPRESSED(buf
)) {
2099 /* We need to reallocate the buf's b_data */
2100 arc_free_data_buf(hdr
, buf
->b_data
, HDR_GET_PSIZE(hdr
),
2103 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2105 /* We increased the size of b_data; update overhead */
2106 ARCSTAT_INCR(arcstat_overhead_size
,
2107 HDR_GET_LSIZE(hdr
) - HDR_GET_PSIZE(hdr
));
2111 * Regardless of the buf's previous compression settings, it
2112 * should not be compressed at the end of this function.
2114 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
2117 * Try copying the data from another buf which already has a
2118 * decompressed version. If that's not possible, it's time to
2119 * bite the bullet and decompress the data from the hdr.
2121 if (arc_buf_try_copy_decompressed_data(buf
)) {
2122 /* Skip byteswapping and checksumming (already done) */
2125 error
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
2126 hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
2127 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
),
2131 * Absent hardware errors or software bugs, this should
2132 * be impossible, but log it anyway so we can debug it.
2136 "hdr %px, compress %d, psize %d, lsize %d",
2137 hdr
, arc_hdr_get_compress(hdr
),
2138 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
2139 if (hash_lock
!= NULL
)
2140 mutex_enter(hash_lock
);
2141 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2142 if (hash_lock
!= NULL
)
2143 mutex_exit(hash_lock
);
2144 return (SET_ERROR(EIO
));
2150 /* Byteswap the buf's data if necessary */
2151 if (bswap
!= DMU_BSWAP_NUMFUNCS
) {
2152 ASSERT(!HDR_SHARED_DATA(hdr
));
2153 ASSERT3U(bswap
, <, DMU_BSWAP_NUMFUNCS
);
2154 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, HDR_GET_LSIZE(hdr
));
2157 /* Compute the hdr's checksum if necessary */
2158 arc_cksum_compute(buf
);
2164 * If this function is being called to decrypt an encrypted buffer or verify an
2165 * authenticated one, the key must be loaded and a mapping must be made
2166 * available in the keystore via spa_keystore_create_mapping() or one of its
2170 arc_untransform(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2174 arc_fill_flags_t flags
= 0;
2177 flags
|= ARC_FILL_IN_PLACE
;
2179 ret
= arc_buf_fill(buf
, spa
, zb
, flags
);
2180 if (ret
== ECKSUM
) {
2182 * Convert authentication and decryption errors to EIO
2183 * (and generate an ereport) before leaving the ARC.
2185 ret
= SET_ERROR(EIO
);
2186 spa_log_error(spa
, zb
);
2187 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION
,
2188 spa
, NULL
, zb
, NULL
, 0);
2195 * Increment the amount of evictable space in the arc_state_t's refcount.
2196 * We account for the space used by the hdr and the arc buf individually
2197 * so that we can add and remove them from the refcount individually.
2200 arc_evictable_space_increment(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2202 arc_buf_contents_t type
= arc_buf_type(hdr
);
2204 ASSERT(HDR_HAS_L1HDR(hdr
));
2206 if (GHOST_STATE(state
)) {
2207 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2208 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2209 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2210 ASSERT(!HDR_HAS_RABD(hdr
));
2211 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2212 HDR_GET_LSIZE(hdr
), hdr
);
2216 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2217 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2218 arc_hdr_size(hdr
), hdr
);
2220 if (HDR_HAS_RABD(hdr
)) {
2221 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2222 HDR_GET_PSIZE(hdr
), hdr
);
2225 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2226 buf
= buf
->b_next
) {
2227 if (arc_buf_is_shared(buf
))
2229 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2230 arc_buf_size(buf
), buf
);
2235 * Decrement the amount of evictable space in the arc_state_t's refcount.
2236 * We account for the space used by the hdr and the arc buf individually
2237 * so that we can add and remove them from the refcount individually.
2240 arc_evictable_space_decrement(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2242 arc_buf_contents_t type
= arc_buf_type(hdr
);
2244 ASSERT(HDR_HAS_L1HDR(hdr
));
2246 if (GHOST_STATE(state
)) {
2247 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2248 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2249 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2250 ASSERT(!HDR_HAS_RABD(hdr
));
2251 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2252 HDR_GET_LSIZE(hdr
), hdr
);
2256 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2257 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2258 arc_hdr_size(hdr
), hdr
);
2260 if (HDR_HAS_RABD(hdr
)) {
2261 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2262 HDR_GET_PSIZE(hdr
), hdr
);
2265 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2266 buf
= buf
->b_next
) {
2267 if (arc_buf_is_shared(buf
))
2269 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2270 arc_buf_size(buf
), buf
);
2275 * Add a reference to this hdr indicating that someone is actively
2276 * referencing that memory. When the refcount transitions from 0 to 1,
2277 * we remove it from the respective arc_state_t list to indicate that
2278 * it is not evictable.
2281 add_reference(arc_buf_hdr_t
*hdr
, void *tag
)
2285 ASSERT(HDR_HAS_L1HDR(hdr
));
2286 if (!HDR_EMPTY(hdr
) && !MUTEX_HELD(HDR_LOCK(hdr
))) {
2287 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
2288 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2289 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2292 state
= hdr
->b_l1hdr
.b_state
;
2294 if ((zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
) == 1) &&
2295 (state
!= arc_anon
)) {
2296 /* We don't use the L2-only state list. */
2297 if (state
!= arc_l2c_only
) {
2298 multilist_remove(&state
->arcs_list
[arc_buf_type(hdr
)],
2300 arc_evictable_space_decrement(hdr
, state
);
2302 /* remove the prefetch flag if we get a reference */
2303 if (HDR_HAS_L2HDR(hdr
))
2304 l2arc_hdr_arcstats_decrement_state(hdr
);
2305 arc_hdr_clear_flags(hdr
, ARC_FLAG_PREFETCH
);
2306 if (HDR_HAS_L2HDR(hdr
))
2307 l2arc_hdr_arcstats_increment_state(hdr
);
2312 * Remove a reference from this hdr. When the reference transitions from
2313 * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's
2314 * list making it eligible for eviction.
2317 remove_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
2320 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2322 ASSERT(HDR_HAS_L1HDR(hdr
));
2323 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
2324 ASSERT(!GHOST_STATE(state
));
2327 * arc_l2c_only counts as a ghost state so we don't need to explicitly
2328 * check to prevent usage of the arc_l2c_only list.
2330 if (((cnt
= zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
)) == 0) &&
2331 (state
!= arc_anon
)) {
2332 multilist_insert(&state
->arcs_list
[arc_buf_type(hdr
)], hdr
);
2333 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
2334 arc_evictable_space_increment(hdr
, state
);
2340 * Returns detailed information about a specific arc buffer. When the
2341 * state_index argument is set the function will calculate the arc header
2342 * list position for its arc state. Since this requires a linear traversal
2343 * callers are strongly encourage not to do this. However, it can be helpful
2344 * for targeted analysis so the functionality is provided.
2347 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
2350 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
2351 l1arc_buf_hdr_t
*l1hdr
= NULL
;
2352 l2arc_buf_hdr_t
*l2hdr
= NULL
;
2353 arc_state_t
*state
= NULL
;
2355 memset(abi
, 0, sizeof (arc_buf_info_t
));
2360 abi
->abi_flags
= hdr
->b_flags
;
2362 if (HDR_HAS_L1HDR(hdr
)) {
2363 l1hdr
= &hdr
->b_l1hdr
;
2364 state
= l1hdr
->b_state
;
2366 if (HDR_HAS_L2HDR(hdr
))
2367 l2hdr
= &hdr
->b_l2hdr
;
2370 abi
->abi_bufcnt
= l1hdr
->b_bufcnt
;
2371 abi
->abi_access
= l1hdr
->b_arc_access
;
2372 abi
->abi_mru_hits
= l1hdr
->b_mru_hits
;
2373 abi
->abi_mru_ghost_hits
= l1hdr
->b_mru_ghost_hits
;
2374 abi
->abi_mfu_hits
= l1hdr
->b_mfu_hits
;
2375 abi
->abi_mfu_ghost_hits
= l1hdr
->b_mfu_ghost_hits
;
2376 abi
->abi_holds
= zfs_refcount_count(&l1hdr
->b_refcnt
);
2380 abi
->abi_l2arc_dattr
= l2hdr
->b_daddr
;
2381 abi
->abi_l2arc_hits
= l2hdr
->b_hits
;
2384 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
2385 abi
->abi_state_contents
= arc_buf_type(hdr
);
2386 abi
->abi_size
= arc_hdr_size(hdr
);
2390 * Move the supplied buffer to the indicated state. The hash lock
2391 * for the buffer must be held by the caller.
2394 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*hdr
,
2395 kmutex_t
*hash_lock
)
2397 arc_state_t
*old_state
;
2400 boolean_t update_old
, update_new
;
2401 arc_buf_contents_t buftype
= arc_buf_type(hdr
);
2404 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
2405 * in arc_read() when bringing a buffer out of the L2ARC. However, the
2406 * L1 hdr doesn't always exist when we change state to arc_anon before
2407 * destroying a header, in which case reallocating to add the L1 hdr is
2410 if (HDR_HAS_L1HDR(hdr
)) {
2411 old_state
= hdr
->b_l1hdr
.b_state
;
2412 refcnt
= zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
);
2413 bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
2414 update_old
= (bufcnt
> 0 || hdr
->b_l1hdr
.b_pabd
!= NULL
||
2417 old_state
= arc_l2c_only
;
2420 update_old
= B_FALSE
;
2422 update_new
= update_old
;
2424 ASSERT(MUTEX_HELD(hash_lock
));
2425 ASSERT3P(new_state
, !=, old_state
);
2426 ASSERT(!GHOST_STATE(new_state
) || bufcnt
== 0);
2427 ASSERT(old_state
!= arc_anon
|| bufcnt
<= 1);
2430 * If this buffer is evictable, transfer it from the
2431 * old state list to the new state list.
2434 if (old_state
!= arc_anon
&& old_state
!= arc_l2c_only
) {
2435 ASSERT(HDR_HAS_L1HDR(hdr
));
2436 multilist_remove(&old_state
->arcs_list
[buftype
], hdr
);
2438 if (GHOST_STATE(old_state
)) {
2440 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2441 update_old
= B_TRUE
;
2443 arc_evictable_space_decrement(hdr
, old_state
);
2445 if (new_state
!= arc_anon
&& new_state
!= arc_l2c_only
) {
2447 * An L1 header always exists here, since if we're
2448 * moving to some L1-cached state (i.e. not l2c_only or
2449 * anonymous), we realloc the header to add an L1hdr
2452 ASSERT(HDR_HAS_L1HDR(hdr
));
2453 multilist_insert(&new_state
->arcs_list
[buftype
], hdr
);
2455 if (GHOST_STATE(new_state
)) {
2457 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2458 update_new
= B_TRUE
;
2460 arc_evictable_space_increment(hdr
, new_state
);
2464 ASSERT(!HDR_EMPTY(hdr
));
2465 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(hdr
))
2466 buf_hash_remove(hdr
);
2468 /* adjust state sizes (ignore arc_l2c_only) */
2470 if (update_new
&& new_state
!= arc_l2c_only
) {
2471 ASSERT(HDR_HAS_L1HDR(hdr
));
2472 if (GHOST_STATE(new_state
)) {
2476 * When moving a header to a ghost state, we first
2477 * remove all arc buffers. Thus, we'll have a
2478 * bufcnt of zero, and no arc buffer to use for
2479 * the reference. As a result, we use the arc
2480 * header pointer for the reference.
2482 (void) zfs_refcount_add_many(&new_state
->arcs_size
,
2483 HDR_GET_LSIZE(hdr
), hdr
);
2484 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2485 ASSERT(!HDR_HAS_RABD(hdr
));
2487 uint32_t buffers
= 0;
2490 * Each individual buffer holds a unique reference,
2491 * thus we must remove each of these references one
2494 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2495 buf
= buf
->b_next
) {
2496 ASSERT3U(bufcnt
, !=, 0);
2500 * When the arc_buf_t is sharing the data
2501 * block with the hdr, the owner of the
2502 * reference belongs to the hdr. Only
2503 * add to the refcount if the arc_buf_t is
2506 if (arc_buf_is_shared(buf
))
2509 (void) zfs_refcount_add_many(
2510 &new_state
->arcs_size
,
2511 arc_buf_size(buf
), buf
);
2513 ASSERT3U(bufcnt
, ==, buffers
);
2515 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2516 (void) zfs_refcount_add_many(
2517 &new_state
->arcs_size
,
2518 arc_hdr_size(hdr
), hdr
);
2521 if (HDR_HAS_RABD(hdr
)) {
2522 (void) zfs_refcount_add_many(
2523 &new_state
->arcs_size
,
2524 HDR_GET_PSIZE(hdr
), hdr
);
2529 if (update_old
&& old_state
!= arc_l2c_only
) {
2530 ASSERT(HDR_HAS_L1HDR(hdr
));
2531 if (GHOST_STATE(old_state
)) {
2533 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2534 ASSERT(!HDR_HAS_RABD(hdr
));
2537 * When moving a header off of a ghost state,
2538 * the header will not contain any arc buffers.
2539 * We use the arc header pointer for the reference
2540 * which is exactly what we did when we put the
2541 * header on the ghost state.
2544 (void) zfs_refcount_remove_many(&old_state
->arcs_size
,
2545 HDR_GET_LSIZE(hdr
), hdr
);
2547 uint32_t buffers
= 0;
2550 * Each individual buffer holds a unique reference,
2551 * thus we must remove each of these references one
2554 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2555 buf
= buf
->b_next
) {
2556 ASSERT3U(bufcnt
, !=, 0);
2560 * When the arc_buf_t is sharing the data
2561 * block with the hdr, the owner of the
2562 * reference belongs to the hdr. Only
2563 * add to the refcount if the arc_buf_t is
2566 if (arc_buf_is_shared(buf
))
2569 (void) zfs_refcount_remove_many(
2570 &old_state
->arcs_size
, arc_buf_size(buf
),
2573 ASSERT3U(bufcnt
, ==, buffers
);
2574 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
2577 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2578 (void) zfs_refcount_remove_many(
2579 &old_state
->arcs_size
, arc_hdr_size(hdr
),
2583 if (HDR_HAS_RABD(hdr
)) {
2584 (void) zfs_refcount_remove_many(
2585 &old_state
->arcs_size
, HDR_GET_PSIZE(hdr
),
2591 if (HDR_HAS_L1HDR(hdr
)) {
2592 hdr
->b_l1hdr
.b_state
= new_state
;
2594 if (HDR_HAS_L2HDR(hdr
) && new_state
!= arc_l2c_only
) {
2595 l2arc_hdr_arcstats_decrement_state(hdr
);
2596 hdr
->b_l2hdr
.b_arcs_state
= new_state
->arcs_state
;
2597 l2arc_hdr_arcstats_increment_state(hdr
);
2603 arc_space_consume(uint64_t space
, arc_space_type_t type
)
2605 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2610 case ARC_SPACE_DATA
:
2611 ARCSTAT_INCR(arcstat_data_size
, space
);
2613 case ARC_SPACE_META
:
2614 ARCSTAT_INCR(arcstat_metadata_size
, space
);
2616 case ARC_SPACE_BONUS
:
2617 ARCSTAT_INCR(arcstat_bonus_size
, space
);
2619 case ARC_SPACE_DNODE
:
2620 aggsum_add(&arc_sums
.arcstat_dnode_size
, space
);
2622 case ARC_SPACE_DBUF
:
2623 ARCSTAT_INCR(arcstat_dbuf_size
, space
);
2625 case ARC_SPACE_HDRS
:
2626 ARCSTAT_INCR(arcstat_hdr_size
, space
);
2628 case ARC_SPACE_L2HDRS
:
2629 aggsum_add(&arc_sums
.arcstat_l2_hdr_size
, space
);
2631 case ARC_SPACE_ABD_CHUNK_WASTE
:
2633 * Note: this includes space wasted by all scatter ABD's, not
2634 * just those allocated by the ARC. But the vast majority of
2635 * scatter ABD's come from the ARC, because other users are
2638 ARCSTAT_INCR(arcstat_abd_chunk_waste_size
, space
);
2642 if (type
!= ARC_SPACE_DATA
&& type
!= ARC_SPACE_ABD_CHUNK_WASTE
)
2643 aggsum_add(&arc_sums
.arcstat_meta_used
, space
);
2645 aggsum_add(&arc_sums
.arcstat_size
, space
);
2649 arc_space_return(uint64_t space
, arc_space_type_t type
)
2651 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2656 case ARC_SPACE_DATA
:
2657 ARCSTAT_INCR(arcstat_data_size
, -space
);
2659 case ARC_SPACE_META
:
2660 ARCSTAT_INCR(arcstat_metadata_size
, -space
);
2662 case ARC_SPACE_BONUS
:
2663 ARCSTAT_INCR(arcstat_bonus_size
, -space
);
2665 case ARC_SPACE_DNODE
:
2666 aggsum_add(&arc_sums
.arcstat_dnode_size
, -space
);
2668 case ARC_SPACE_DBUF
:
2669 ARCSTAT_INCR(arcstat_dbuf_size
, -space
);
2671 case ARC_SPACE_HDRS
:
2672 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
2674 case ARC_SPACE_L2HDRS
:
2675 aggsum_add(&arc_sums
.arcstat_l2_hdr_size
, -space
);
2677 case ARC_SPACE_ABD_CHUNK_WASTE
:
2678 ARCSTAT_INCR(arcstat_abd_chunk_waste_size
, -space
);
2682 if (type
!= ARC_SPACE_DATA
&& type
!= ARC_SPACE_ABD_CHUNK_WASTE
) {
2683 ASSERT(aggsum_compare(&arc_sums
.arcstat_meta_used
,
2685 ARCSTAT_MAX(arcstat_meta_max
,
2686 aggsum_upper_bound(&arc_sums
.arcstat_meta_used
));
2687 aggsum_add(&arc_sums
.arcstat_meta_used
, -space
);
2690 ASSERT(aggsum_compare(&arc_sums
.arcstat_size
, space
) >= 0);
2691 aggsum_add(&arc_sums
.arcstat_size
, -space
);
2695 * Given a hdr and a buf, returns whether that buf can share its b_data buffer
2696 * with the hdr's b_pabd.
2699 arc_can_share(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2702 * The criteria for sharing a hdr's data are:
2703 * 1. the buffer is not encrypted
2704 * 2. the hdr's compression matches the buf's compression
2705 * 3. the hdr doesn't need to be byteswapped
2706 * 4. the hdr isn't already being shared
2707 * 5. the buf is either compressed or it is the last buf in the hdr list
2709 * Criterion #5 maintains the invariant that shared uncompressed
2710 * bufs must be the final buf in the hdr's b_buf list. Reading this, you
2711 * might ask, "if a compressed buf is allocated first, won't that be the
2712 * last thing in the list?", but in that case it's impossible to create
2713 * a shared uncompressed buf anyway (because the hdr must be compressed
2714 * to have the compressed buf). You might also think that #3 is
2715 * sufficient to make this guarantee, however it's possible
2716 * (specifically in the rare L2ARC write race mentioned in
2717 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that
2718 * is shareable, but wasn't at the time of its allocation. Rather than
2719 * allow a new shared uncompressed buf to be created and then shuffle
2720 * the list around to make it the last element, this simply disallows
2721 * sharing if the new buf isn't the first to be added.
2723 ASSERT3P(buf
->b_hdr
, ==, hdr
);
2724 boolean_t hdr_compressed
=
2725 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
;
2726 boolean_t buf_compressed
= ARC_BUF_COMPRESSED(buf
) != 0;
2727 return (!ARC_BUF_ENCRYPTED(buf
) &&
2728 buf_compressed
== hdr_compressed
&&
2729 hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
&&
2730 !HDR_SHARED_DATA(hdr
) &&
2731 (ARC_BUF_LAST(buf
) || ARC_BUF_COMPRESSED(buf
)));
2735 * Allocate a buf for this hdr. If you care about the data that's in the hdr,
2736 * or if you want a compressed buffer, pass those flags in. Returns 0 if the
2737 * copy was made successfully, or an error code otherwise.
2740 arc_buf_alloc_impl(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2741 void *tag
, boolean_t encrypted
, boolean_t compressed
, boolean_t noauth
,
2742 boolean_t fill
, arc_buf_t
**ret
)
2745 arc_fill_flags_t flags
= ARC_FILL_LOCKED
;
2747 ASSERT(HDR_HAS_L1HDR(hdr
));
2748 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
2749 VERIFY(hdr
->b_type
== ARC_BUFC_DATA
||
2750 hdr
->b_type
== ARC_BUFC_METADATA
);
2751 ASSERT3P(ret
, !=, NULL
);
2752 ASSERT3P(*ret
, ==, NULL
);
2753 IMPLY(encrypted
, compressed
);
2755 buf
= *ret
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
2758 buf
->b_next
= hdr
->b_l1hdr
.b_buf
;
2761 add_reference(hdr
, tag
);
2764 * We're about to change the hdr's b_flags. We must either
2765 * hold the hash_lock or be undiscoverable.
2767 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
2770 * Only honor requests for compressed bufs if the hdr is actually
2771 * compressed. This must be overridden if the buffer is encrypted since
2772 * encrypted buffers cannot be decompressed.
2775 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2776 buf
->b_flags
|= ARC_BUF_FLAG_ENCRYPTED
;
2777 flags
|= ARC_FILL_COMPRESSED
| ARC_FILL_ENCRYPTED
;
2778 } else if (compressed
&&
2779 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
2780 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2781 flags
|= ARC_FILL_COMPRESSED
;
2786 flags
|= ARC_FILL_NOAUTH
;
2790 * If the hdr's data can be shared then we share the data buffer and
2791 * set the appropriate bit in the hdr's b_flags to indicate the hdr is
2792 * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new
2793 * buffer to store the buf's data.
2795 * There are two additional restrictions here because we're sharing
2796 * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be
2797 * actively involved in an L2ARC write, because if this buf is used by
2798 * an arc_write() then the hdr's data buffer will be released when the
2799 * write completes, even though the L2ARC write might still be using it.
2800 * Second, the hdr's ABD must be linear so that the buf's user doesn't
2801 * need to be ABD-aware. It must be allocated via
2802 * zio_[data_]buf_alloc(), not as a page, because we need to be able
2803 * to abd_release_ownership_of_buf(), which isn't allowed on "linear
2804 * page" buffers because the ABD code needs to handle freeing them
2807 boolean_t can_share
= arc_can_share(hdr
, buf
) &&
2808 !HDR_L2_WRITING(hdr
) &&
2809 hdr
->b_l1hdr
.b_pabd
!= NULL
&&
2810 abd_is_linear(hdr
->b_l1hdr
.b_pabd
) &&
2811 !abd_is_linear_page(hdr
->b_l1hdr
.b_pabd
);
2813 /* Set up b_data and sharing */
2815 buf
->b_data
= abd_to_buf(hdr
->b_l1hdr
.b_pabd
);
2816 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2817 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2820 arc_get_data_buf(hdr
, arc_buf_size(buf
), buf
);
2821 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
2823 VERIFY3P(buf
->b_data
, !=, NULL
);
2825 hdr
->b_l1hdr
.b_buf
= buf
;
2826 hdr
->b_l1hdr
.b_bufcnt
+= 1;
2828 hdr
->b_crypt_hdr
.b_ebufcnt
+= 1;
2831 * If the user wants the data from the hdr, we need to either copy or
2832 * decompress the data.
2835 ASSERT3P(zb
, !=, NULL
);
2836 return (arc_buf_fill(buf
, spa
, zb
, flags
));
2842 static char *arc_onloan_tag
= "onloan";
2845 arc_loaned_bytes_update(int64_t delta
)
2847 atomic_add_64(&arc_loaned_bytes
, delta
);
2849 /* assert that it did not wrap around */
2850 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
2854 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
2855 * flight data by arc_tempreserve_space() until they are "returned". Loaned
2856 * buffers must be returned to the arc before they can be used by the DMU or
2860 arc_loan_buf(spa_t
*spa
, boolean_t is_metadata
, int size
)
2862 arc_buf_t
*buf
= arc_alloc_buf(spa
, arc_onloan_tag
,
2863 is_metadata
? ARC_BUFC_METADATA
: ARC_BUFC_DATA
, size
);
2865 arc_loaned_bytes_update(arc_buf_size(buf
));
2871 arc_loan_compressed_buf(spa_t
*spa
, uint64_t psize
, uint64_t lsize
,
2872 enum zio_compress compression_type
, uint8_t complevel
)
2874 arc_buf_t
*buf
= arc_alloc_compressed_buf(spa
, arc_onloan_tag
,
2875 psize
, lsize
, compression_type
, complevel
);
2877 arc_loaned_bytes_update(arc_buf_size(buf
));
2883 arc_loan_raw_buf(spa_t
*spa
, uint64_t dsobj
, boolean_t byteorder
,
2884 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
2885 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
2886 enum zio_compress compression_type
, uint8_t complevel
)
2888 arc_buf_t
*buf
= arc_alloc_raw_buf(spa
, arc_onloan_tag
, dsobj
,
2889 byteorder
, salt
, iv
, mac
, ot
, psize
, lsize
, compression_type
,
2892 atomic_add_64(&arc_loaned_bytes
, psize
);
2898 * Return a loaned arc buffer to the arc.
2901 arc_return_buf(arc_buf_t
*buf
, void *tag
)
2903 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2905 ASSERT3P(buf
->b_data
, !=, NULL
);
2906 ASSERT(HDR_HAS_L1HDR(hdr
));
2907 (void) zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2908 (void) zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2910 arc_loaned_bytes_update(-arc_buf_size(buf
));
2913 /* Detach an arc_buf from a dbuf (tag) */
2915 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
2917 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2919 ASSERT3P(buf
->b_data
, !=, NULL
);
2920 ASSERT(HDR_HAS_L1HDR(hdr
));
2921 (void) zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2922 (void) zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2924 arc_loaned_bytes_update(arc_buf_size(buf
));
2928 l2arc_free_abd_on_write(abd_t
*abd
, size_t size
, arc_buf_contents_t type
)
2930 l2arc_data_free_t
*df
= kmem_alloc(sizeof (*df
), KM_SLEEP
);
2933 df
->l2df_size
= size
;
2934 df
->l2df_type
= type
;
2935 mutex_enter(&l2arc_free_on_write_mtx
);
2936 list_insert_head(l2arc_free_on_write
, df
);
2937 mutex_exit(&l2arc_free_on_write_mtx
);
2941 arc_hdr_free_on_write(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
2943 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2944 arc_buf_contents_t type
= arc_buf_type(hdr
);
2945 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
2947 /* protected by hash lock, if in the hash table */
2948 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
2949 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2950 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
2952 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2955 (void) zfs_refcount_remove_many(&state
->arcs_size
, size
, hdr
);
2956 if (type
== ARC_BUFC_METADATA
) {
2957 arc_space_return(size
, ARC_SPACE_META
);
2959 ASSERT(type
== ARC_BUFC_DATA
);
2960 arc_space_return(size
, ARC_SPACE_DATA
);
2964 l2arc_free_abd_on_write(hdr
->b_crypt_hdr
.b_rabd
, size
, type
);
2966 l2arc_free_abd_on_write(hdr
->b_l1hdr
.b_pabd
, size
, type
);
2971 * Share the arc_buf_t's data with the hdr. Whenever we are sharing the
2972 * data buffer, we transfer the refcount ownership to the hdr and update
2973 * the appropriate kstats.
2976 arc_share_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2978 ASSERT(arc_can_share(hdr
, buf
));
2979 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2980 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
2981 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
2984 * Start sharing the data buffer. We transfer the
2985 * refcount ownership to the hdr since it always owns
2986 * the refcount whenever an arc_buf_t is shared.
2988 zfs_refcount_transfer_ownership_many(&hdr
->b_l1hdr
.b_state
->arcs_size
,
2989 arc_hdr_size(hdr
), buf
, hdr
);
2990 hdr
->b_l1hdr
.b_pabd
= abd_get_from_buf(buf
->b_data
, arc_buf_size(buf
));
2991 abd_take_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
,
2992 HDR_ISTYPE_METADATA(hdr
));
2993 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2994 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2997 * Since we've transferred ownership to the hdr we need
2998 * to increment its compressed and uncompressed kstats and
2999 * decrement the overhead size.
3001 ARCSTAT_INCR(arcstat_compressed_size
, arc_hdr_size(hdr
));
3002 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3003 ARCSTAT_INCR(arcstat_overhead_size
, -arc_buf_size(buf
));
3007 arc_unshare_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3009 ASSERT(arc_buf_is_shared(buf
));
3010 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3011 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3014 * We are no longer sharing this buffer so we need
3015 * to transfer its ownership to the rightful owner.
3017 zfs_refcount_transfer_ownership_many(&hdr
->b_l1hdr
.b_state
->arcs_size
,
3018 arc_hdr_size(hdr
), hdr
, buf
);
3019 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3020 abd_release_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
);
3021 abd_free(hdr
->b_l1hdr
.b_pabd
);
3022 hdr
->b_l1hdr
.b_pabd
= NULL
;
3023 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
3026 * Since the buffer is no longer shared between
3027 * the arc buf and the hdr, count it as overhead.
3029 ARCSTAT_INCR(arcstat_compressed_size
, -arc_hdr_size(hdr
));
3030 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3031 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
3035 * Remove an arc_buf_t from the hdr's buf list and return the last
3036 * arc_buf_t on the list. If no buffers remain on the list then return
3040 arc_buf_remove(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3042 ASSERT(HDR_HAS_L1HDR(hdr
));
3043 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3045 arc_buf_t
**bufp
= &hdr
->b_l1hdr
.b_buf
;
3046 arc_buf_t
*lastbuf
= NULL
;
3049 * Remove the buf from the hdr list and locate the last
3050 * remaining buffer on the list.
3052 while (*bufp
!= NULL
) {
3054 *bufp
= buf
->b_next
;
3057 * If we've removed a buffer in the middle of
3058 * the list then update the lastbuf and update
3061 if (*bufp
!= NULL
) {
3063 bufp
= &(*bufp
)->b_next
;
3067 ASSERT3P(lastbuf
, !=, buf
);
3068 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, lastbuf
!= NULL
);
3069 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, hdr
->b_l1hdr
.b_buf
!= NULL
);
3070 IMPLY(lastbuf
!= NULL
, ARC_BUF_LAST(lastbuf
));
3076 * Free up buf->b_data and pull the arc_buf_t off of the arc_buf_hdr_t's
3080 arc_buf_destroy_impl(arc_buf_t
*buf
)
3082 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3085 * Free up the data associated with the buf but only if we're not
3086 * sharing this with the hdr. If we are sharing it with the hdr, the
3087 * hdr is responsible for doing the free.
3089 if (buf
->b_data
!= NULL
) {
3091 * We're about to change the hdr's b_flags. We must either
3092 * hold the hash_lock or be undiscoverable.
3094 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3096 arc_cksum_verify(buf
);
3097 arc_buf_unwatch(buf
);
3099 if (arc_buf_is_shared(buf
)) {
3100 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3102 uint64_t size
= arc_buf_size(buf
);
3103 arc_free_data_buf(hdr
, buf
->b_data
, size
, buf
);
3104 ARCSTAT_INCR(arcstat_overhead_size
, -size
);
3108 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3109 hdr
->b_l1hdr
.b_bufcnt
-= 1;
3111 if (ARC_BUF_ENCRYPTED(buf
)) {
3112 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
3115 * If we have no more encrypted buffers and we've
3116 * already gotten a copy of the decrypted data we can
3117 * free b_rabd to save some space.
3119 if (hdr
->b_crypt_hdr
.b_ebufcnt
== 0 &&
3120 HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
!= NULL
&&
3121 !HDR_IO_IN_PROGRESS(hdr
)) {
3122 arc_hdr_free_abd(hdr
, B_TRUE
);
3127 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
3129 if (ARC_BUF_SHARED(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
3131 * If the current arc_buf_t is sharing its data buffer with the
3132 * hdr, then reassign the hdr's b_pabd to share it with the new
3133 * buffer at the end of the list. The shared buffer is always
3134 * the last one on the hdr's buffer list.
3136 * There is an equivalent case for compressed bufs, but since
3137 * they aren't guaranteed to be the last buf in the list and
3138 * that is an exceedingly rare case, we just allow that space be
3139 * wasted temporarily. We must also be careful not to share
3140 * encrypted buffers, since they cannot be shared.
3142 if (lastbuf
!= NULL
&& !ARC_BUF_ENCRYPTED(lastbuf
)) {
3143 /* Only one buf can be shared at once */
3144 VERIFY(!arc_buf_is_shared(lastbuf
));
3145 /* hdr is uncompressed so can't have compressed buf */
3146 VERIFY(!ARC_BUF_COMPRESSED(lastbuf
));
3148 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3149 arc_hdr_free_abd(hdr
, B_FALSE
);
3152 * We must setup a new shared block between the
3153 * last buffer and the hdr. The data would have
3154 * been allocated by the arc buf so we need to transfer
3155 * ownership to the hdr since it's now being shared.
3157 arc_share_buf(hdr
, lastbuf
);
3159 } else if (HDR_SHARED_DATA(hdr
)) {
3161 * Uncompressed shared buffers are always at the end
3162 * of the list. Compressed buffers don't have the
3163 * same requirements. This makes it hard to
3164 * simply assert that the lastbuf is shared so
3165 * we rely on the hdr's compression flags to determine
3166 * if we have a compressed, shared buffer.
3168 ASSERT3P(lastbuf
, !=, NULL
);
3169 ASSERT(arc_buf_is_shared(lastbuf
) ||
3170 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
3174 * Free the checksum if we're removing the last uncompressed buf from
3177 if (!arc_hdr_has_uncompressed_buf(hdr
)) {
3178 arc_cksum_free(hdr
);
3181 /* clean up the buf */
3183 kmem_cache_free(buf_cache
, buf
);
3187 arc_hdr_alloc_abd(arc_buf_hdr_t
*hdr
, int alloc_flags
)
3190 boolean_t alloc_rdata
= ((alloc_flags
& ARC_HDR_ALLOC_RDATA
) != 0);
3192 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
3193 ASSERT(HDR_HAS_L1HDR(hdr
));
3194 ASSERT(!HDR_SHARED_DATA(hdr
) || alloc_rdata
);
3195 IMPLY(alloc_rdata
, HDR_PROTECTED(hdr
));
3198 size
= HDR_GET_PSIZE(hdr
);
3199 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, ==, NULL
);
3200 hdr
->b_crypt_hdr
.b_rabd
= arc_get_data_abd(hdr
, size
, hdr
,
3202 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, !=, NULL
);
3203 ARCSTAT_INCR(arcstat_raw_size
, size
);
3205 size
= arc_hdr_size(hdr
);
3206 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3207 hdr
->b_l1hdr
.b_pabd
= arc_get_data_abd(hdr
, size
, hdr
,
3209 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3212 ARCSTAT_INCR(arcstat_compressed_size
, size
);
3213 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3217 arc_hdr_free_abd(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
3219 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
3221 ASSERT(HDR_HAS_L1HDR(hdr
));
3222 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
3223 IMPLY(free_rdata
, HDR_HAS_RABD(hdr
));
3226 * If the hdr is currently being written to the l2arc then
3227 * we defer freeing the data by adding it to the l2arc_free_on_write
3228 * list. The l2arc will free the data once it's finished
3229 * writing it to the l2arc device.
3231 if (HDR_L2_WRITING(hdr
)) {
3232 arc_hdr_free_on_write(hdr
, free_rdata
);
3233 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
3234 } else if (free_rdata
) {
3235 arc_free_data_abd(hdr
, hdr
->b_crypt_hdr
.b_rabd
, size
, hdr
);
3237 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
, size
, hdr
);
3241 hdr
->b_crypt_hdr
.b_rabd
= NULL
;
3242 ARCSTAT_INCR(arcstat_raw_size
, -size
);
3244 hdr
->b_l1hdr
.b_pabd
= NULL
;
3247 if (hdr
->b_l1hdr
.b_pabd
== NULL
&& !HDR_HAS_RABD(hdr
))
3248 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
3250 ARCSTAT_INCR(arcstat_compressed_size
, -size
);
3251 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3255 * Allocate empty anonymous ARC header. The header will get its identity
3256 * assigned and buffers attached later as part of read or write operations.
3258 * In case of read arc_read() assigns header its identify (b_dva + b_birth),
3259 * inserts it into ARC hash to become globally visible and allocates physical
3260 * (b_pabd) or raw (b_rabd) ABD buffer to read into from disk. On disk read
3261 * completion arc_read_done() allocates ARC buffer(s) as needed, potentially
3262 * sharing one of them with the physical ABD buffer.
3264 * In case of write arc_alloc_buf() allocates ARC buffer to be filled with
3265 * data. Then after compression and/or encryption arc_write_ready() allocates
3266 * and fills (or potentially shares) physical (b_pabd) or raw (b_rabd) ABD
3267 * buffer. On disk write completion arc_write_done() assigns the header its
3268 * new identity (b_dva + b_birth) and inserts into ARC hash.
3270 * In case of partial overwrite the old data is read first as described. Then
3271 * arc_release() either allocates new anonymous ARC header and moves the ARC
3272 * buffer to it, or reuses the old ARC header by discarding its identity and
3273 * removing it from ARC hash. After buffer modification normal write process
3274 * follows as described.
3276 static arc_buf_hdr_t
*
3277 arc_hdr_alloc(uint64_t spa
, int32_t psize
, int32_t lsize
,
3278 boolean_t
protected, enum zio_compress compression_type
, uint8_t complevel
,
3279 arc_buf_contents_t type
)
3283 VERIFY(type
== ARC_BUFC_DATA
|| type
== ARC_BUFC_METADATA
);
3285 hdr
= kmem_cache_alloc(hdr_full_crypt_cache
, KM_PUSHPAGE
);
3287 hdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
3290 ASSERT(HDR_EMPTY(hdr
));
3291 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3292 HDR_SET_PSIZE(hdr
, psize
);
3293 HDR_SET_LSIZE(hdr
, lsize
);
3297 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L1HDR
);
3298 arc_hdr_set_compress(hdr
, compression_type
);
3299 hdr
->b_complevel
= complevel
;
3301 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
3303 hdr
->b_l1hdr
.b_state
= arc_anon
;
3304 hdr
->b_l1hdr
.b_arc_access
= 0;
3305 hdr
->b_l1hdr
.b_mru_hits
= 0;
3306 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
3307 hdr
->b_l1hdr
.b_mfu_hits
= 0;
3308 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
3309 hdr
->b_l1hdr
.b_bufcnt
= 0;
3310 hdr
->b_l1hdr
.b_buf
= NULL
;
3312 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3318 * Transition between the two allocation states for the arc_buf_hdr struct.
3319 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
3320 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
3321 * version is used when a cache buffer is only in the L2ARC in order to reduce
3324 static arc_buf_hdr_t
*
3325 arc_hdr_realloc(arc_buf_hdr_t
*hdr
, kmem_cache_t
*old
, kmem_cache_t
*new)
3327 ASSERT(HDR_HAS_L2HDR(hdr
));
3329 arc_buf_hdr_t
*nhdr
;
3330 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3332 ASSERT((old
== hdr_full_cache
&& new == hdr_l2only_cache
) ||
3333 (old
== hdr_l2only_cache
&& new == hdr_full_cache
));
3336 * if the caller wanted a new full header and the header is to be
3337 * encrypted we will actually allocate the header from the full crypt
3338 * cache instead. The same applies to freeing from the old cache.
3340 if (HDR_PROTECTED(hdr
) && new == hdr_full_cache
)
3341 new = hdr_full_crypt_cache
;
3342 if (HDR_PROTECTED(hdr
) && old
== hdr_full_cache
)
3343 old
= hdr_full_crypt_cache
;
3345 nhdr
= kmem_cache_alloc(new, KM_PUSHPAGE
);
3347 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
3348 buf_hash_remove(hdr
);
3350 bcopy(hdr
, nhdr
, HDR_L2ONLY_SIZE
);
3352 if (new == hdr_full_cache
|| new == hdr_full_crypt_cache
) {
3353 arc_hdr_set_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3355 * arc_access and arc_change_state need to be aware that a
3356 * header has just come out of L2ARC, so we set its state to
3357 * l2c_only even though it's about to change.
3359 nhdr
->b_l1hdr
.b_state
= arc_l2c_only
;
3361 /* Verify previous threads set to NULL before freeing */
3362 ASSERT3P(nhdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3363 ASSERT(!HDR_HAS_RABD(hdr
));
3365 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3366 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
3367 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3370 * If we've reached here, We must have been called from
3371 * arc_evict_hdr(), as such we should have already been
3372 * removed from any ghost list we were previously on
3373 * (which protects us from racing with arc_evict_state),
3374 * thus no locking is needed during this check.
3376 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3379 * A buffer must not be moved into the arc_l2c_only
3380 * state if it's not finished being written out to the
3381 * l2arc device. Otherwise, the b_l1hdr.b_pabd field
3382 * might try to be accessed, even though it was removed.
3384 VERIFY(!HDR_L2_WRITING(hdr
));
3385 VERIFY3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3386 ASSERT(!HDR_HAS_RABD(hdr
));
3388 arc_hdr_clear_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3391 * The header has been reallocated so we need to re-insert it into any
3394 (void) buf_hash_insert(nhdr
, NULL
);
3396 ASSERT(list_link_active(&hdr
->b_l2hdr
.b_l2node
));
3398 mutex_enter(&dev
->l2ad_mtx
);
3401 * We must place the realloc'ed header back into the list at
3402 * the same spot. Otherwise, if it's placed earlier in the list,
3403 * l2arc_write_buffers() could find it during the function's
3404 * write phase, and try to write it out to the l2arc.
3406 list_insert_after(&dev
->l2ad_buflist
, hdr
, nhdr
);
3407 list_remove(&dev
->l2ad_buflist
, hdr
);
3409 mutex_exit(&dev
->l2ad_mtx
);
3412 * Since we're using the pointer address as the tag when
3413 * incrementing and decrementing the l2ad_alloc refcount, we
3414 * must remove the old pointer (that we're about to destroy) and
3415 * add the new pointer to the refcount. Otherwise we'd remove
3416 * the wrong pointer address when calling arc_hdr_destroy() later.
3419 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
,
3420 arc_hdr_size(hdr
), hdr
);
3421 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
3422 arc_hdr_size(nhdr
), nhdr
);
3424 buf_discard_identity(hdr
);
3425 kmem_cache_free(old
, hdr
);
3431 * This function allows an L1 header to be reallocated as a crypt
3432 * header and vice versa. If we are going to a crypt header, the
3433 * new fields will be zeroed out.
3435 static arc_buf_hdr_t
*
3436 arc_hdr_realloc_crypt(arc_buf_hdr_t
*hdr
, boolean_t need_crypt
)
3438 arc_buf_hdr_t
*nhdr
;
3440 kmem_cache_t
*ncache
, *ocache
;
3443 * This function requires that hdr is in the arc_anon state.
3444 * Therefore it won't have any L2ARC data for us to worry
3447 ASSERT(HDR_HAS_L1HDR(hdr
));
3448 ASSERT(!HDR_HAS_L2HDR(hdr
));
3449 ASSERT3U(!!HDR_PROTECTED(hdr
), !=, need_crypt
);
3450 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3451 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3452 ASSERT(!list_link_active(&hdr
->b_l2hdr
.b_l2node
));
3453 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3456 ncache
= hdr_full_crypt_cache
;
3457 ocache
= hdr_full_cache
;
3459 ncache
= hdr_full_cache
;
3460 ocache
= hdr_full_crypt_cache
;
3463 nhdr
= kmem_cache_alloc(ncache
, KM_PUSHPAGE
);
3466 * Copy all members that aren't locks or condvars to the new header.
3467 * No lists are pointing to us (as we asserted above), so we don't
3468 * need to worry about the list nodes.
3470 nhdr
->b_dva
= hdr
->b_dva
;
3471 nhdr
->b_birth
= hdr
->b_birth
;
3472 nhdr
->b_type
= hdr
->b_type
;
3473 nhdr
->b_flags
= hdr
->b_flags
;
3474 nhdr
->b_psize
= hdr
->b_psize
;
3475 nhdr
->b_lsize
= hdr
->b_lsize
;
3476 nhdr
->b_spa
= hdr
->b_spa
;
3477 nhdr
->b_l1hdr
.b_freeze_cksum
= hdr
->b_l1hdr
.b_freeze_cksum
;
3478 nhdr
->b_l1hdr
.b_bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
3479 nhdr
->b_l1hdr
.b_byteswap
= hdr
->b_l1hdr
.b_byteswap
;
3480 nhdr
->b_l1hdr
.b_state
= hdr
->b_l1hdr
.b_state
;
3481 nhdr
->b_l1hdr
.b_arc_access
= hdr
->b_l1hdr
.b_arc_access
;
3482 nhdr
->b_l1hdr
.b_mru_hits
= hdr
->b_l1hdr
.b_mru_hits
;
3483 nhdr
->b_l1hdr
.b_mru_ghost_hits
= hdr
->b_l1hdr
.b_mru_ghost_hits
;
3484 nhdr
->b_l1hdr
.b_mfu_hits
= hdr
->b_l1hdr
.b_mfu_hits
;
3485 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= hdr
->b_l1hdr
.b_mfu_ghost_hits
;
3486 nhdr
->b_l1hdr
.b_acb
= hdr
->b_l1hdr
.b_acb
;
3487 nhdr
->b_l1hdr
.b_pabd
= hdr
->b_l1hdr
.b_pabd
;
3490 * This zfs_refcount_add() exists only to ensure that the individual
3491 * arc buffers always point to a header that is referenced, avoiding
3492 * a small race condition that could trigger ASSERTs.
3494 (void) zfs_refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, FTAG
);
3495 nhdr
->b_l1hdr
.b_buf
= hdr
->b_l1hdr
.b_buf
;
3496 for (buf
= nhdr
->b_l1hdr
.b_buf
; buf
!= NULL
; buf
= buf
->b_next
) {
3497 mutex_enter(&buf
->b_evict_lock
);
3499 mutex_exit(&buf
->b_evict_lock
);
3502 zfs_refcount_transfer(&nhdr
->b_l1hdr
.b_refcnt
, &hdr
->b_l1hdr
.b_refcnt
);
3503 (void) zfs_refcount_remove(&nhdr
->b_l1hdr
.b_refcnt
, FTAG
);
3504 ASSERT0(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3507 arc_hdr_set_flags(nhdr
, ARC_FLAG_PROTECTED
);
3509 arc_hdr_clear_flags(nhdr
, ARC_FLAG_PROTECTED
);
3512 /* unset all members of the original hdr */
3513 bzero(&hdr
->b_dva
, sizeof (dva_t
));
3515 hdr
->b_type
= ARC_BUFC_INVALID
;
3520 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
3521 hdr
->b_l1hdr
.b_buf
= NULL
;
3522 hdr
->b_l1hdr
.b_bufcnt
= 0;
3523 hdr
->b_l1hdr
.b_byteswap
= 0;
3524 hdr
->b_l1hdr
.b_state
= NULL
;
3525 hdr
->b_l1hdr
.b_arc_access
= 0;
3526 hdr
->b_l1hdr
.b_mru_hits
= 0;
3527 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
3528 hdr
->b_l1hdr
.b_mfu_hits
= 0;
3529 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
3530 hdr
->b_l1hdr
.b_acb
= NULL
;
3531 hdr
->b_l1hdr
.b_pabd
= NULL
;
3533 if (ocache
== hdr_full_crypt_cache
) {
3534 ASSERT(!HDR_HAS_RABD(hdr
));
3535 hdr
->b_crypt_hdr
.b_ot
= DMU_OT_NONE
;
3536 hdr
->b_crypt_hdr
.b_ebufcnt
= 0;
3537 hdr
->b_crypt_hdr
.b_dsobj
= 0;
3538 bzero(hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3539 bzero(hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3540 bzero(hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3543 buf_discard_identity(hdr
);
3544 kmem_cache_free(ocache
, hdr
);
3550 * This function is used by the send / receive code to convert a newly
3551 * allocated arc_buf_t to one that is suitable for a raw encrypted write. It
3552 * is also used to allow the root objset block to be updated without altering
3553 * its embedded MACs. Both block types will always be uncompressed so we do not
3554 * have to worry about compression type or psize.
3557 arc_convert_to_raw(arc_buf_t
*buf
, uint64_t dsobj
, boolean_t byteorder
,
3558 dmu_object_type_t ot
, const uint8_t *salt
, const uint8_t *iv
,
3561 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3563 ASSERT(ot
== DMU_OT_DNODE
|| ot
== DMU_OT_OBJSET
);
3564 ASSERT(HDR_HAS_L1HDR(hdr
));
3565 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3567 buf
->b_flags
|= (ARC_BUF_FLAG_COMPRESSED
| ARC_BUF_FLAG_ENCRYPTED
);
3568 if (!HDR_PROTECTED(hdr
))
3569 hdr
= arc_hdr_realloc_crypt(hdr
, B_TRUE
);
3570 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3571 hdr
->b_crypt_hdr
.b_ot
= ot
;
3572 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3573 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3574 if (!arc_hdr_has_uncompressed_buf(hdr
))
3575 arc_cksum_free(hdr
);
3578 bcopy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3580 bcopy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3582 bcopy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3586 * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller.
3587 * The buf is returned thawed since we expect the consumer to modify it.
3590 arc_alloc_buf(spa_t
*spa
, void *tag
, arc_buf_contents_t type
, int32_t size
)
3592 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), size
, size
,
3593 B_FALSE
, ZIO_COMPRESS_OFF
, 0, type
);
3595 arc_buf_t
*buf
= NULL
;
3596 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
, B_FALSE
,
3597 B_FALSE
, B_FALSE
, &buf
));
3604 * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this
3605 * for bufs containing metadata.
3608 arc_alloc_compressed_buf(spa_t
*spa
, void *tag
, uint64_t psize
, uint64_t lsize
,
3609 enum zio_compress compression_type
, uint8_t complevel
)
3611 ASSERT3U(lsize
, >, 0);
3612 ASSERT3U(lsize
, >=, psize
);
3613 ASSERT3U(compression_type
, >, ZIO_COMPRESS_OFF
);
3614 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3616 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
3617 B_FALSE
, compression_type
, complevel
, ARC_BUFC_DATA
);
3619 arc_buf_t
*buf
= NULL
;
3620 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
,
3621 B_TRUE
, B_FALSE
, B_FALSE
, &buf
));
3623 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3626 * To ensure that the hdr has the correct data in it if we call
3627 * arc_untransform() on this buf before it's been written to disk,
3628 * it's easiest if we just set up sharing between the buf and the hdr.
3630 arc_share_buf(hdr
, buf
);
3636 arc_alloc_raw_buf(spa_t
*spa
, void *tag
, uint64_t dsobj
, boolean_t byteorder
,
3637 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
3638 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
3639 enum zio_compress compression_type
, uint8_t complevel
)
3643 arc_buf_contents_t type
= DMU_OT_IS_METADATA(ot
) ?
3644 ARC_BUFC_METADATA
: ARC_BUFC_DATA
;
3646 ASSERT3U(lsize
, >, 0);
3647 ASSERT3U(lsize
, >=, psize
);
3648 ASSERT3U(compression_type
, >=, ZIO_COMPRESS_OFF
);
3649 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3651 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
, B_TRUE
,
3652 compression_type
, complevel
, type
);
3654 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3655 hdr
->b_crypt_hdr
.b_ot
= ot
;
3656 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3657 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3658 bcopy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
3659 bcopy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
3660 bcopy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
3663 * This buffer will be considered encrypted even if the ot is not an
3664 * encrypted type. It will become authenticated instead in
3665 * arc_write_ready().
3668 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_TRUE
, B_TRUE
,
3669 B_FALSE
, B_FALSE
, &buf
));
3671 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3677 l2arc_hdr_arcstats_update(arc_buf_hdr_t
*hdr
, boolean_t incr
,
3678 boolean_t state_only
)
3680 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
3681 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
3682 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
3683 uint64_t psize
= HDR_GET_PSIZE(hdr
);
3684 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
3685 arc_buf_contents_t type
= hdr
->b_type
;
3700 /* If the buffer is a prefetch, count it as such. */
3701 if (HDR_PREFETCH(hdr
)) {
3702 ARCSTAT_INCR(arcstat_l2_prefetch_asize
, asize_s
);
3705 * We use the value stored in the L2 header upon initial
3706 * caching in L2ARC. This value will be updated in case
3707 * an MRU/MRU_ghost buffer transitions to MFU but the L2ARC
3708 * metadata (log entry) cannot currently be updated. Having
3709 * the ARC state in the L2 header solves the problem of a
3710 * possibly absent L1 header (apparent in buffers restored
3711 * from persistent L2ARC).
3713 switch (hdr
->b_l2hdr
.b_arcs_state
) {
3714 case ARC_STATE_MRU_GHOST
:
3716 ARCSTAT_INCR(arcstat_l2_mru_asize
, asize_s
);
3718 case ARC_STATE_MFU_GHOST
:
3720 ARCSTAT_INCR(arcstat_l2_mfu_asize
, asize_s
);
3730 ARCSTAT_INCR(arcstat_l2_psize
, psize_s
);
3731 ARCSTAT_INCR(arcstat_l2_lsize
, lsize_s
);
3735 ARCSTAT_INCR(arcstat_l2_bufc_data_asize
, asize_s
);
3737 case ARC_BUFC_METADATA
:
3738 ARCSTAT_INCR(arcstat_l2_bufc_metadata_asize
, asize_s
);
3747 arc_hdr_l2hdr_destroy(arc_buf_hdr_t
*hdr
)
3749 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
3750 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
3751 uint64_t psize
= HDR_GET_PSIZE(hdr
);
3752 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
3754 ASSERT(MUTEX_HELD(&dev
->l2ad_mtx
));
3755 ASSERT(HDR_HAS_L2HDR(hdr
));
3757 list_remove(&dev
->l2ad_buflist
, hdr
);
3759 l2arc_hdr_arcstats_decrement(hdr
);
3760 vdev_space_update(dev
->l2ad_vdev
, -asize
, 0, 0);
3762 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
),
3764 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
3768 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
3770 if (HDR_HAS_L1HDR(hdr
)) {
3771 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
||
3772 hdr
->b_l1hdr
.b_bufcnt
> 0);
3773 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3774 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3776 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3777 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
3779 if (HDR_HAS_L2HDR(hdr
)) {
3780 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3781 boolean_t buflist_held
= MUTEX_HELD(&dev
->l2ad_mtx
);
3784 mutex_enter(&dev
->l2ad_mtx
);
3787 * Even though we checked this conditional above, we
3788 * need to check this again now that we have the
3789 * l2ad_mtx. This is because we could be racing with
3790 * another thread calling l2arc_evict() which might have
3791 * destroyed this header's L2 portion as we were waiting
3792 * to acquire the l2ad_mtx. If that happens, we don't
3793 * want to re-destroy the header's L2 portion.
3795 if (HDR_HAS_L2HDR(hdr
)) {
3797 if (!HDR_EMPTY(hdr
))
3798 buf_discard_identity(hdr
);
3800 arc_hdr_l2hdr_destroy(hdr
);
3804 mutex_exit(&dev
->l2ad_mtx
);
3808 * The header's identify can only be safely discarded once it is no
3809 * longer discoverable. This requires removing it from the hash table
3810 * and the l2arc header list. After this point the hash lock can not
3811 * be used to protect the header.
3813 if (!HDR_EMPTY(hdr
))
3814 buf_discard_identity(hdr
);
3816 if (HDR_HAS_L1HDR(hdr
)) {
3817 arc_cksum_free(hdr
);
3819 while (hdr
->b_l1hdr
.b_buf
!= NULL
)
3820 arc_buf_destroy_impl(hdr
->b_l1hdr
.b_buf
);
3822 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
3823 arc_hdr_free_abd(hdr
, B_FALSE
);
3825 if (HDR_HAS_RABD(hdr
))
3826 arc_hdr_free_abd(hdr
, B_TRUE
);
3829 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3830 if (HDR_HAS_L1HDR(hdr
)) {
3831 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3832 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
3834 if (!HDR_PROTECTED(hdr
)) {
3835 kmem_cache_free(hdr_full_cache
, hdr
);
3837 kmem_cache_free(hdr_full_crypt_cache
, hdr
);
3840 kmem_cache_free(hdr_l2only_cache
, hdr
);
3845 arc_buf_destroy(arc_buf_t
*buf
, void* tag
)
3847 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3849 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
3850 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
3851 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3852 VERIFY0(remove_reference(hdr
, NULL
, tag
));
3853 arc_hdr_destroy(hdr
);
3857 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
3858 mutex_enter(hash_lock
);
3860 ASSERT3P(hdr
, ==, buf
->b_hdr
);
3861 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3862 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3863 ASSERT3P(hdr
->b_l1hdr
.b_state
, !=, arc_anon
);
3864 ASSERT3P(buf
->b_data
, !=, NULL
);
3866 (void) remove_reference(hdr
, hash_lock
, tag
);
3867 arc_buf_destroy_impl(buf
);
3868 mutex_exit(hash_lock
);
3872 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
3873 * state of the header is dependent on its state prior to entering this
3874 * function. The following transitions are possible:
3876 * - arc_mru -> arc_mru_ghost
3877 * - arc_mfu -> arc_mfu_ghost
3878 * - arc_mru_ghost -> arc_l2c_only
3879 * - arc_mru_ghost -> deleted
3880 * - arc_mfu_ghost -> arc_l2c_only
3881 * - arc_mfu_ghost -> deleted
3883 * Return total size of evicted data buffers for eviction progress tracking.
3884 * When evicting from ghost states return logical buffer size to make eviction
3885 * progress at the same (or at least comparable) rate as from non-ghost states.
3887 * Return *real_evicted for actual ARC size reduction to wake up threads
3888 * waiting for it. For non-ghost states it includes size of evicted data
3889 * buffers (the headers are not freed there). For ghost states it includes
3890 * only the evicted headers size.
3893 arc_evict_hdr(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, uint64_t *real_evicted
)
3895 arc_state_t
*evicted_state
, *state
;
3896 int64_t bytes_evicted
= 0;
3897 int min_lifetime
= HDR_PRESCIENT_PREFETCH(hdr
) ?
3898 arc_min_prescient_prefetch_ms
: arc_min_prefetch_ms
;
3900 ASSERT(MUTEX_HELD(hash_lock
));
3901 ASSERT(HDR_HAS_L1HDR(hdr
));
3904 state
= hdr
->b_l1hdr
.b_state
;
3905 if (GHOST_STATE(state
)) {
3906 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3907 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3910 * l2arc_write_buffers() relies on a header's L1 portion
3911 * (i.e. its b_pabd field) during it's write phase.
3912 * Thus, we cannot push a header onto the arc_l2c_only
3913 * state (removing its L1 piece) until the header is
3914 * done being written to the l2arc.
3916 if (HDR_HAS_L2HDR(hdr
) && HDR_L2_WRITING(hdr
)) {
3917 ARCSTAT_BUMP(arcstat_evict_l2_skip
);
3918 return (bytes_evicted
);
3921 ARCSTAT_BUMP(arcstat_deleted
);
3922 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3924 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, hdr
);
3926 if (HDR_HAS_L2HDR(hdr
)) {
3927 ASSERT(hdr
->b_l1hdr
.b_pabd
== NULL
);
3928 ASSERT(!HDR_HAS_RABD(hdr
));
3930 * This buffer is cached on the 2nd Level ARC;
3931 * don't destroy the header.
3933 arc_change_state(arc_l2c_only
, hdr
, hash_lock
);
3935 * dropping from L1+L2 cached to L2-only,
3936 * realloc to remove the L1 header.
3938 hdr
= arc_hdr_realloc(hdr
, hdr_full_cache
,
3940 *real_evicted
+= HDR_FULL_SIZE
- HDR_L2ONLY_SIZE
;
3942 arc_change_state(arc_anon
, hdr
, hash_lock
);
3943 arc_hdr_destroy(hdr
);
3944 *real_evicted
+= HDR_FULL_SIZE
;
3946 return (bytes_evicted
);
3949 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
3950 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
3952 /* prefetch buffers have a minimum lifespan */
3953 if (HDR_IO_IN_PROGRESS(hdr
) ||
3954 ((hdr
->b_flags
& (ARC_FLAG_PREFETCH
| ARC_FLAG_INDIRECT
)) &&
3955 ddi_get_lbolt() - hdr
->b_l1hdr
.b_arc_access
<
3956 MSEC_TO_TICK(min_lifetime
))) {
3957 ARCSTAT_BUMP(arcstat_evict_skip
);
3958 return (bytes_evicted
);
3961 ASSERT0(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3962 while (hdr
->b_l1hdr
.b_buf
) {
3963 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
3964 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
3965 ARCSTAT_BUMP(arcstat_mutex_miss
);
3968 if (buf
->b_data
!= NULL
) {
3969 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3970 *real_evicted
+= HDR_GET_LSIZE(hdr
);
3972 mutex_exit(&buf
->b_evict_lock
);
3973 arc_buf_destroy_impl(buf
);
3976 if (HDR_HAS_L2HDR(hdr
)) {
3977 ARCSTAT_INCR(arcstat_evict_l2_cached
, HDR_GET_LSIZE(hdr
));
3979 if (l2arc_write_eligible(hdr
->b_spa
, hdr
)) {
3980 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
3981 HDR_GET_LSIZE(hdr
));
3983 switch (state
->arcs_state
) {
3986 arcstat_evict_l2_eligible_mru
,
3987 HDR_GET_LSIZE(hdr
));
3991 arcstat_evict_l2_eligible_mfu
,
3992 HDR_GET_LSIZE(hdr
));
3998 ARCSTAT_INCR(arcstat_evict_l2_ineligible
,
3999 HDR_GET_LSIZE(hdr
));
4003 if (hdr
->b_l1hdr
.b_bufcnt
== 0) {
4004 arc_cksum_free(hdr
);
4006 bytes_evicted
+= arc_hdr_size(hdr
);
4007 *real_evicted
+= arc_hdr_size(hdr
);
4010 * If this hdr is being evicted and has a compressed
4011 * buffer then we discard it here before we change states.
4012 * This ensures that the accounting is updated correctly
4013 * in arc_free_data_impl().
4015 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
4016 arc_hdr_free_abd(hdr
, B_FALSE
);
4018 if (HDR_HAS_RABD(hdr
))
4019 arc_hdr_free_abd(hdr
, B_TRUE
);
4021 arc_change_state(evicted_state
, hdr
, hash_lock
);
4022 ASSERT(HDR_IN_HASH_TABLE(hdr
));
4023 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
4024 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, hdr
);
4027 return (bytes_evicted
);
4031 arc_set_need_free(void)
4033 ASSERT(MUTEX_HELD(&arc_evict_lock
));
4034 int64_t remaining
= arc_free_memory() - arc_sys_free
/ 2;
4035 arc_evict_waiter_t
*aw
= list_tail(&arc_evict_waiters
);
4037 arc_need_free
= MAX(-remaining
, 0);
4040 MAX(-remaining
, (int64_t)(aw
->aew_count
- arc_evict_count
));
4045 arc_evict_state_impl(multilist_t
*ml
, int idx
, arc_buf_hdr_t
*marker
,
4046 uint64_t spa
, uint64_t bytes
)
4048 multilist_sublist_t
*mls
;
4049 uint64_t bytes_evicted
= 0, real_evicted
= 0;
4051 kmutex_t
*hash_lock
;
4052 int evict_count
= zfs_arc_evict_batch_limit
;
4054 ASSERT3P(marker
, !=, NULL
);
4056 mls
= multilist_sublist_lock(ml
, idx
);
4058 for (hdr
= multilist_sublist_prev(mls
, marker
); likely(hdr
!= NULL
);
4059 hdr
= multilist_sublist_prev(mls
, marker
)) {
4060 if ((evict_count
<= 0) || (bytes_evicted
>= bytes
))
4064 * To keep our iteration location, move the marker
4065 * forward. Since we're not holding hdr's hash lock, we
4066 * must be very careful and not remove 'hdr' from the
4067 * sublist. Otherwise, other consumers might mistake the
4068 * 'hdr' as not being on a sublist when they call the
4069 * multilist_link_active() function (they all rely on
4070 * the hash lock protecting concurrent insertions and
4071 * removals). multilist_sublist_move_forward() was
4072 * specifically implemented to ensure this is the case
4073 * (only 'marker' will be removed and re-inserted).
4075 multilist_sublist_move_forward(mls
, marker
);
4078 * The only case where the b_spa field should ever be
4079 * zero, is the marker headers inserted by
4080 * arc_evict_state(). It's possible for multiple threads
4081 * to be calling arc_evict_state() concurrently (e.g.
4082 * dsl_pool_close() and zio_inject_fault()), so we must
4083 * skip any markers we see from these other threads.
4085 if (hdr
->b_spa
== 0)
4088 /* we're only interested in evicting buffers of a certain spa */
4089 if (spa
!= 0 && hdr
->b_spa
!= spa
) {
4090 ARCSTAT_BUMP(arcstat_evict_skip
);
4094 hash_lock
= HDR_LOCK(hdr
);
4097 * We aren't calling this function from any code path
4098 * that would already be holding a hash lock, so we're
4099 * asserting on this assumption to be defensive in case
4100 * this ever changes. Without this check, it would be
4101 * possible to incorrectly increment arcstat_mutex_miss
4102 * below (e.g. if the code changed such that we called
4103 * this function with a hash lock held).
4105 ASSERT(!MUTEX_HELD(hash_lock
));
4107 if (mutex_tryenter(hash_lock
)) {
4109 uint64_t evicted
= arc_evict_hdr(hdr
, hash_lock
,
4111 mutex_exit(hash_lock
);
4113 bytes_evicted
+= evicted
;
4114 real_evicted
+= revicted
;
4117 * If evicted is zero, arc_evict_hdr() must have
4118 * decided to skip this header, don't increment
4119 * evict_count in this case.
4125 ARCSTAT_BUMP(arcstat_mutex_miss
);
4129 multilist_sublist_unlock(mls
);
4132 * Increment the count of evicted bytes, and wake up any threads that
4133 * are waiting for the count to reach this value. Since the list is
4134 * ordered by ascending aew_count, we pop off the beginning of the
4135 * list until we reach the end, or a waiter that's past the current
4136 * "count". Doing this outside the loop reduces the number of times
4137 * we need to acquire the global arc_evict_lock.
4139 * Only wake when there's sufficient free memory in the system
4140 * (specifically, arc_sys_free/2, which by default is a bit more than
4141 * 1/64th of RAM). See the comments in arc_wait_for_eviction().
4143 mutex_enter(&arc_evict_lock
);
4144 arc_evict_count
+= real_evicted
;
4146 if (arc_free_memory() > arc_sys_free
/ 2) {
4147 arc_evict_waiter_t
*aw
;
4148 while ((aw
= list_head(&arc_evict_waiters
)) != NULL
&&
4149 aw
->aew_count
<= arc_evict_count
) {
4150 list_remove(&arc_evict_waiters
, aw
);
4151 cv_broadcast(&aw
->aew_cv
);
4154 arc_set_need_free();
4155 mutex_exit(&arc_evict_lock
);
4158 * If the ARC size is reduced from arc_c_max to arc_c_min (especially
4159 * if the average cached block is small), eviction can be on-CPU for
4160 * many seconds. To ensure that other threads that may be bound to
4161 * this CPU are able to make progress, make a voluntary preemption
4166 return (bytes_evicted
);
4170 * Evict buffers from the given arc state, until we've removed the
4171 * specified number of bytes. Move the removed buffers to the
4172 * appropriate evict state.
4174 * This function makes a "best effort". It skips over any buffers
4175 * it can't get a hash_lock on, and so, may not catch all candidates.
4176 * It may also return without evicting as much space as requested.
4178 * If bytes is specified using the special value ARC_EVICT_ALL, this
4179 * will evict all available (i.e. unlocked and evictable) buffers from
4180 * the given arc state; which is used by arc_flush().
4183 arc_evict_state(arc_state_t
*state
, uint64_t spa
, uint64_t bytes
,
4184 arc_buf_contents_t type
)
4186 uint64_t total_evicted
= 0;
4187 multilist_t
*ml
= &state
->arcs_list
[type
];
4189 arc_buf_hdr_t
**markers
;
4191 num_sublists
= multilist_get_num_sublists(ml
);
4194 * If we've tried to evict from each sublist, made some
4195 * progress, but still have not hit the target number of bytes
4196 * to evict, we want to keep trying. The markers allow us to
4197 * pick up where we left off for each individual sublist, rather
4198 * than starting from the tail each time.
4200 markers
= kmem_zalloc(sizeof (*markers
) * num_sublists
, KM_SLEEP
);
4201 for (int i
= 0; i
< num_sublists
; i
++) {
4202 multilist_sublist_t
*mls
;
4204 markers
[i
] = kmem_cache_alloc(hdr_full_cache
, KM_SLEEP
);
4207 * A b_spa of 0 is used to indicate that this header is
4208 * a marker. This fact is used in arc_evict_type() and
4209 * arc_evict_state_impl().
4211 markers
[i
]->b_spa
= 0;
4213 mls
= multilist_sublist_lock(ml
, i
);
4214 multilist_sublist_insert_tail(mls
, markers
[i
]);
4215 multilist_sublist_unlock(mls
);
4219 * While we haven't hit our target number of bytes to evict, or
4220 * we're evicting all available buffers.
4222 while (total_evicted
< bytes
) {
4223 int sublist_idx
= multilist_get_random_index(ml
);
4224 uint64_t scan_evicted
= 0;
4227 * Try to reduce pinned dnodes with a floor of arc_dnode_limit.
4228 * Request that 10% of the LRUs be scanned by the superblock
4231 if (type
== ARC_BUFC_DATA
&& aggsum_compare(
4232 &arc_sums
.arcstat_dnode_size
, arc_dnode_size_limit
) > 0) {
4233 arc_prune_async((aggsum_upper_bound(
4234 &arc_sums
.arcstat_dnode_size
) -
4235 arc_dnode_size_limit
) / sizeof (dnode_t
) /
4236 zfs_arc_dnode_reduce_percent
);
4240 * Start eviction using a randomly selected sublist,
4241 * this is to try and evenly balance eviction across all
4242 * sublists. Always starting at the same sublist
4243 * (e.g. index 0) would cause evictions to favor certain
4244 * sublists over others.
4246 for (int i
= 0; i
< num_sublists
; i
++) {
4247 uint64_t bytes_remaining
;
4248 uint64_t bytes_evicted
;
4250 if (total_evicted
< bytes
)
4251 bytes_remaining
= bytes
- total_evicted
;
4255 bytes_evicted
= arc_evict_state_impl(ml
, sublist_idx
,
4256 markers
[sublist_idx
], spa
, bytes_remaining
);
4258 scan_evicted
+= bytes_evicted
;
4259 total_evicted
+= bytes_evicted
;
4261 /* we've reached the end, wrap to the beginning */
4262 if (++sublist_idx
>= num_sublists
)
4267 * If we didn't evict anything during this scan, we have
4268 * no reason to believe we'll evict more during another
4269 * scan, so break the loop.
4271 if (scan_evicted
== 0) {
4272 /* This isn't possible, let's make that obvious */
4273 ASSERT3S(bytes
, !=, 0);
4276 * When bytes is ARC_EVICT_ALL, the only way to
4277 * break the loop is when scan_evicted is zero.
4278 * In that case, we actually have evicted enough,
4279 * so we don't want to increment the kstat.
4281 if (bytes
!= ARC_EVICT_ALL
) {
4282 ASSERT3S(total_evicted
, <, bytes
);
4283 ARCSTAT_BUMP(arcstat_evict_not_enough
);
4290 for (int i
= 0; i
< num_sublists
; i
++) {
4291 multilist_sublist_t
*mls
= multilist_sublist_lock(ml
, i
);
4292 multilist_sublist_remove(mls
, markers
[i
]);
4293 multilist_sublist_unlock(mls
);
4295 kmem_cache_free(hdr_full_cache
, markers
[i
]);
4297 kmem_free(markers
, sizeof (*markers
) * num_sublists
);
4299 return (total_evicted
);
4303 * Flush all "evictable" data of the given type from the arc state
4304 * specified. This will not evict any "active" buffers (i.e. referenced).
4306 * When 'retry' is set to B_FALSE, the function will make a single pass
4307 * over the state and evict any buffers that it can. Since it doesn't
4308 * continually retry the eviction, it might end up leaving some buffers
4309 * in the ARC due to lock misses.
4311 * When 'retry' is set to B_TRUE, the function will continually retry the
4312 * eviction until *all* evictable buffers have been removed from the
4313 * state. As a result, if concurrent insertions into the state are
4314 * allowed (e.g. if the ARC isn't shutting down), this function might
4315 * wind up in an infinite loop, continually trying to evict buffers.
4318 arc_flush_state(arc_state_t
*state
, uint64_t spa
, arc_buf_contents_t type
,
4321 uint64_t evicted
= 0;
4323 while (zfs_refcount_count(&state
->arcs_esize
[type
]) != 0) {
4324 evicted
+= arc_evict_state(state
, spa
, ARC_EVICT_ALL
, type
);
4334 * Evict the specified number of bytes from the state specified,
4335 * restricting eviction to the spa and type given. This function
4336 * prevents us from trying to evict more from a state's list than
4337 * is "evictable", and to skip evicting altogether when passed a
4338 * negative value for "bytes". In contrast, arc_evict_state() will
4339 * evict everything it can, when passed a negative value for "bytes".
4342 arc_evict_impl(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
4343 arc_buf_contents_t type
)
4347 if (bytes
> 0 && zfs_refcount_count(&state
->arcs_esize
[type
]) > 0) {
4348 delta
= MIN(zfs_refcount_count(&state
->arcs_esize
[type
]),
4350 return (arc_evict_state(state
, spa
, delta
, type
));
4357 * The goal of this function is to evict enough meta data buffers from the
4358 * ARC in order to enforce the arc_meta_limit. Achieving this is slightly
4359 * more complicated than it appears because it is common for data buffers
4360 * to have holds on meta data buffers. In addition, dnode meta data buffers
4361 * will be held by the dnodes in the block preventing them from being freed.
4362 * This means we can't simply traverse the ARC and expect to always find
4363 * enough unheld meta data buffer to release.
4365 * Therefore, this function has been updated to make alternating passes
4366 * over the ARC releasing data buffers and then newly unheld meta data
4367 * buffers. This ensures forward progress is maintained and meta_used
4368 * will decrease. Normally this is sufficient, but if required the ARC
4369 * will call the registered prune callbacks causing dentry and inodes to
4370 * be dropped from the VFS cache. This will make dnode meta data buffers
4371 * available for reclaim.
4374 arc_evict_meta_balanced(uint64_t meta_used
)
4376 int64_t delta
, prune
= 0, adjustmnt
;
4377 uint64_t total_evicted
= 0;
4378 arc_buf_contents_t type
= ARC_BUFC_DATA
;
4379 int restarts
= MAX(zfs_arc_meta_adjust_restarts
, 0);
4383 * This slightly differs than the way we evict from the mru in
4384 * arc_evict because we don't have a "target" value (i.e. no
4385 * "meta" arc_p). As a result, I think we can completely
4386 * cannibalize the metadata in the MRU before we evict the
4387 * metadata from the MFU. I think we probably need to implement a
4388 * "metadata arc_p" value to do this properly.
4390 adjustmnt
= meta_used
- arc_meta_limit
;
4392 if (adjustmnt
> 0 &&
4393 zfs_refcount_count(&arc_mru
->arcs_esize
[type
]) > 0) {
4394 delta
= MIN(zfs_refcount_count(&arc_mru
->arcs_esize
[type
]),
4396 total_evicted
+= arc_evict_impl(arc_mru
, 0, delta
, type
);
4401 * We can't afford to recalculate adjustmnt here. If we do,
4402 * new metadata buffers can sneak into the MRU or ANON lists,
4403 * thus penalize the MFU metadata. Although the fudge factor is
4404 * small, it has been empirically shown to be significant for
4405 * certain workloads (e.g. creating many empty directories). As
4406 * such, we use the original calculation for adjustmnt, and
4407 * simply decrement the amount of data evicted from the MRU.
4410 if (adjustmnt
> 0 &&
4411 zfs_refcount_count(&arc_mfu
->arcs_esize
[type
]) > 0) {
4412 delta
= MIN(zfs_refcount_count(&arc_mfu
->arcs_esize
[type
]),
4414 total_evicted
+= arc_evict_impl(arc_mfu
, 0, delta
, type
);
4417 adjustmnt
= meta_used
- arc_meta_limit
;
4419 if (adjustmnt
> 0 &&
4420 zfs_refcount_count(&arc_mru_ghost
->arcs_esize
[type
]) > 0) {
4421 delta
= MIN(adjustmnt
,
4422 zfs_refcount_count(&arc_mru_ghost
->arcs_esize
[type
]));
4423 total_evicted
+= arc_evict_impl(arc_mru_ghost
, 0, delta
, type
);
4427 if (adjustmnt
> 0 &&
4428 zfs_refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]) > 0) {
4429 delta
= MIN(adjustmnt
,
4430 zfs_refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]));
4431 total_evicted
+= arc_evict_impl(arc_mfu_ghost
, 0, delta
, type
);
4435 * If after attempting to make the requested adjustment to the ARC
4436 * the meta limit is still being exceeded then request that the
4437 * higher layers drop some cached objects which have holds on ARC
4438 * meta buffers. Requests to the upper layers will be made with
4439 * increasingly large scan sizes until the ARC is below the limit.
4441 if (meta_used
> arc_meta_limit
) {
4442 if (type
== ARC_BUFC_DATA
) {
4443 type
= ARC_BUFC_METADATA
;
4445 type
= ARC_BUFC_DATA
;
4447 if (zfs_arc_meta_prune
) {
4448 prune
+= zfs_arc_meta_prune
;
4449 arc_prune_async(prune
);
4458 return (total_evicted
);
4462 * Evict metadata buffers from the cache, such that arcstat_meta_used is
4463 * capped by the arc_meta_limit tunable.
4466 arc_evict_meta_only(uint64_t meta_used
)
4468 uint64_t total_evicted
= 0;
4472 * If we're over the meta limit, we want to evict enough
4473 * metadata to get back under the meta limit. We don't want to
4474 * evict so much that we drop the MRU below arc_p, though. If
4475 * we're over the meta limit more than we're over arc_p, we
4476 * evict some from the MRU here, and some from the MFU below.
4478 target
= MIN((int64_t)(meta_used
- arc_meta_limit
),
4479 (int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) +
4480 zfs_refcount_count(&arc_mru
->arcs_size
) - arc_p
));
4482 total_evicted
+= arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4485 * Similar to the above, we want to evict enough bytes to get us
4486 * below the meta limit, but not so much as to drop us below the
4487 * space allotted to the MFU (which is defined as arc_c - arc_p).
4489 target
= MIN((int64_t)(meta_used
- arc_meta_limit
),
4490 (int64_t)(zfs_refcount_count(&arc_mfu
->arcs_size
) -
4493 total_evicted
+= arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4495 return (total_evicted
);
4499 arc_evict_meta(uint64_t meta_used
)
4501 if (zfs_arc_meta_strategy
== ARC_STRATEGY_META_ONLY
)
4502 return (arc_evict_meta_only(meta_used
));
4504 return (arc_evict_meta_balanced(meta_used
));
4508 * Return the type of the oldest buffer in the given arc state
4510 * This function will select a random sublist of type ARC_BUFC_DATA and
4511 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist
4512 * is compared, and the type which contains the "older" buffer will be
4515 static arc_buf_contents_t
4516 arc_evict_type(arc_state_t
*state
)
4518 multilist_t
*data_ml
= &state
->arcs_list
[ARC_BUFC_DATA
];
4519 multilist_t
*meta_ml
= &state
->arcs_list
[ARC_BUFC_METADATA
];
4520 int data_idx
= multilist_get_random_index(data_ml
);
4521 int meta_idx
= multilist_get_random_index(meta_ml
);
4522 multilist_sublist_t
*data_mls
;
4523 multilist_sublist_t
*meta_mls
;
4524 arc_buf_contents_t type
;
4525 arc_buf_hdr_t
*data_hdr
;
4526 arc_buf_hdr_t
*meta_hdr
;
4529 * We keep the sublist lock until we're finished, to prevent
4530 * the headers from being destroyed via arc_evict_state().
4532 data_mls
= multilist_sublist_lock(data_ml
, data_idx
);
4533 meta_mls
= multilist_sublist_lock(meta_ml
, meta_idx
);
4536 * These two loops are to ensure we skip any markers that
4537 * might be at the tail of the lists due to arc_evict_state().
4540 for (data_hdr
= multilist_sublist_tail(data_mls
); data_hdr
!= NULL
;
4541 data_hdr
= multilist_sublist_prev(data_mls
, data_hdr
)) {
4542 if (data_hdr
->b_spa
!= 0)
4546 for (meta_hdr
= multilist_sublist_tail(meta_mls
); meta_hdr
!= NULL
;
4547 meta_hdr
= multilist_sublist_prev(meta_mls
, meta_hdr
)) {
4548 if (meta_hdr
->b_spa
!= 0)
4552 if (data_hdr
== NULL
&& meta_hdr
== NULL
) {
4553 type
= ARC_BUFC_DATA
;
4554 } else if (data_hdr
== NULL
) {
4555 ASSERT3P(meta_hdr
, !=, NULL
);
4556 type
= ARC_BUFC_METADATA
;
4557 } else if (meta_hdr
== NULL
) {
4558 ASSERT3P(data_hdr
, !=, NULL
);
4559 type
= ARC_BUFC_DATA
;
4561 ASSERT3P(data_hdr
, !=, NULL
);
4562 ASSERT3P(meta_hdr
, !=, NULL
);
4564 /* The headers can't be on the sublist without an L1 header */
4565 ASSERT(HDR_HAS_L1HDR(data_hdr
));
4566 ASSERT(HDR_HAS_L1HDR(meta_hdr
));
4568 if (data_hdr
->b_l1hdr
.b_arc_access
<
4569 meta_hdr
->b_l1hdr
.b_arc_access
) {
4570 type
= ARC_BUFC_DATA
;
4572 type
= ARC_BUFC_METADATA
;
4576 multilist_sublist_unlock(meta_mls
);
4577 multilist_sublist_unlock(data_mls
);
4583 * Evict buffers from the cache, such that arcstat_size is capped by arc_c.
4588 uint64_t total_evicted
= 0;
4591 uint64_t asize
= aggsum_value(&arc_sums
.arcstat_size
);
4592 uint64_t ameta
= aggsum_value(&arc_sums
.arcstat_meta_used
);
4595 * If we're over arc_meta_limit, we want to correct that before
4596 * potentially evicting data buffers below.
4598 total_evicted
+= arc_evict_meta(ameta
);
4603 * If we're over the target cache size, we want to evict enough
4604 * from the list to get back to our target size. We don't want
4605 * to evict too much from the MRU, such that it drops below
4606 * arc_p. So, if we're over our target cache size more than
4607 * the MRU is over arc_p, we'll evict enough to get back to
4608 * arc_p here, and then evict more from the MFU below.
4610 target
= MIN((int64_t)(asize
- arc_c
),
4611 (int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) +
4612 zfs_refcount_count(&arc_mru
->arcs_size
) + ameta
- arc_p
));
4615 * If we're below arc_meta_min, always prefer to evict data.
4616 * Otherwise, try to satisfy the requested number of bytes to
4617 * evict from the type which contains older buffers; in an
4618 * effort to keep newer buffers in the cache regardless of their
4619 * type. If we cannot satisfy the number of bytes from this
4620 * type, spill over into the next type.
4622 if (arc_evict_type(arc_mru
) == ARC_BUFC_METADATA
&&
4623 ameta
> arc_meta_min
) {
4624 bytes
= arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4625 total_evicted
+= bytes
;
4628 * If we couldn't evict our target number of bytes from
4629 * metadata, we try to get the rest from data.
4634 arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
4636 bytes
= arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
4637 total_evicted
+= bytes
;
4640 * If we couldn't evict our target number of bytes from
4641 * data, we try to get the rest from metadata.
4646 arc_evict_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
4650 * Re-sum ARC stats after the first round of evictions.
4652 asize
= aggsum_value(&arc_sums
.arcstat_size
);
4653 ameta
= aggsum_value(&arc_sums
.arcstat_meta_used
);
4659 * Now that we've tried to evict enough from the MRU to get its
4660 * size back to arc_p, if we're still above the target cache
4661 * size, we evict the rest from the MFU.
4663 target
= asize
- arc_c
;
4665 if (arc_evict_type(arc_mfu
) == ARC_BUFC_METADATA
&&
4666 ameta
> arc_meta_min
) {
4667 bytes
= arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4668 total_evicted
+= bytes
;
4671 * If we couldn't evict our target number of bytes from
4672 * metadata, we try to get the rest from data.
4677 arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
4679 bytes
= arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
4680 total_evicted
+= bytes
;
4683 * If we couldn't evict our target number of bytes from
4684 * data, we try to get the rest from data.
4689 arc_evict_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
4693 * Adjust ghost lists
4695 * In addition to the above, the ARC also defines target values
4696 * for the ghost lists. The sum of the mru list and mru ghost
4697 * list should never exceed the target size of the cache, and
4698 * the sum of the mru list, mfu list, mru ghost list, and mfu
4699 * ghost list should never exceed twice the target size of the
4700 * cache. The following logic enforces these limits on the ghost
4701 * caches, and evicts from them as needed.
4703 target
= zfs_refcount_count(&arc_mru
->arcs_size
) +
4704 zfs_refcount_count(&arc_mru_ghost
->arcs_size
) - arc_c
;
4706 bytes
= arc_evict_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_DATA
);
4707 total_evicted
+= bytes
;
4712 arc_evict_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_METADATA
);
4715 * We assume the sum of the mru list and mfu list is less than
4716 * or equal to arc_c (we enforced this above), which means we
4717 * can use the simpler of the two equations below:
4719 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c
4720 * mru ghost + mfu ghost <= arc_c
4722 target
= zfs_refcount_count(&arc_mru_ghost
->arcs_size
) +
4723 zfs_refcount_count(&arc_mfu_ghost
->arcs_size
) - arc_c
;
4725 bytes
= arc_evict_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_DATA
);
4726 total_evicted
+= bytes
;
4731 arc_evict_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_METADATA
);
4733 return (total_evicted
);
4737 arc_flush(spa_t
*spa
, boolean_t retry
)
4742 * If retry is B_TRUE, a spa must not be specified since we have
4743 * no good way to determine if all of a spa's buffers have been
4744 * evicted from an arc state.
4746 ASSERT(!retry
|| spa
== 0);
4749 guid
= spa_load_guid(spa
);
4751 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_DATA
, retry
);
4752 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_METADATA
, retry
);
4754 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_DATA
, retry
);
4755 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_METADATA
, retry
);
4757 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4758 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4760 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4761 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4765 arc_reduce_target_size(int64_t to_free
)
4767 uint64_t asize
= aggsum_value(&arc_sums
.arcstat_size
);
4770 * All callers want the ARC to actually evict (at least) this much
4771 * memory. Therefore we reduce from the lower of the current size and
4772 * the target size. This way, even if arc_c is much higher than
4773 * arc_size (as can be the case after many calls to arc_freed(), we will
4774 * immediately have arc_c < arc_size and therefore the arc_evict_zthr
4777 uint64_t c
= MIN(arc_c
, asize
);
4779 if (c
> to_free
&& c
- to_free
> arc_c_min
) {
4780 arc_c
= c
- to_free
;
4781 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
4783 arc_p
= (arc_c
>> 1);
4784 ASSERT(arc_c
>= arc_c_min
);
4785 ASSERT((int64_t)arc_p
>= 0);
4790 if (asize
> arc_c
) {
4791 /* See comment in arc_evict_cb_check() on why lock+flag */
4792 mutex_enter(&arc_evict_lock
);
4793 arc_evict_needed
= B_TRUE
;
4794 mutex_exit(&arc_evict_lock
);
4795 zthr_wakeup(arc_evict_zthr
);
4800 * Determine if the system is under memory pressure and is asking
4801 * to reclaim memory. A return value of B_TRUE indicates that the system
4802 * is under memory pressure and that the arc should adjust accordingly.
4805 arc_reclaim_needed(void)
4807 return (arc_available_memory() < 0);
4811 arc_kmem_reap_soon(void)
4814 kmem_cache_t
*prev_cache
= NULL
;
4815 kmem_cache_t
*prev_data_cache
= NULL
;
4818 if ((aggsum_compare(&arc_sums
.arcstat_meta_used
,
4819 arc_meta_limit
) >= 0) && zfs_arc_meta_prune
) {
4821 * We are exceeding our meta-data cache limit.
4822 * Prune some entries to release holds on meta-data.
4824 arc_prune_async(zfs_arc_meta_prune
);
4828 * Reclaim unused memory from all kmem caches.
4834 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
4836 /* reach upper limit of cache size on 32-bit */
4837 if (zio_buf_cache
[i
] == NULL
)
4840 if (zio_buf_cache
[i
] != prev_cache
) {
4841 prev_cache
= zio_buf_cache
[i
];
4842 kmem_cache_reap_now(zio_buf_cache
[i
]);
4844 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
4845 prev_data_cache
= zio_data_buf_cache
[i
];
4846 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
4849 kmem_cache_reap_now(buf_cache
);
4850 kmem_cache_reap_now(hdr_full_cache
);
4851 kmem_cache_reap_now(hdr_l2only_cache
);
4852 kmem_cache_reap_now(zfs_btree_leaf_cache
);
4853 abd_cache_reap_now();
4857 arc_evict_cb_check(void *arg
, zthr_t
*zthr
)
4859 (void) arg
, (void) zthr
;
4863 * This is necessary in order to keep the kstat information
4864 * up to date for tools that display kstat data such as the
4865 * mdb ::arc dcmd and the Linux crash utility. These tools
4866 * typically do not call kstat's update function, but simply
4867 * dump out stats from the most recent update. Without
4868 * this call, these commands may show stale stats for the
4869 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
4870 * with this call, the data might be out of date if the
4871 * evict thread hasn't been woken recently; but that should
4872 * suffice. The arc_state_t structures can be queried
4873 * directly if more accurate information is needed.
4875 if (arc_ksp
!= NULL
)
4876 arc_ksp
->ks_update(arc_ksp
, KSTAT_READ
);
4880 * We have to rely on arc_wait_for_eviction() to tell us when to
4881 * evict, rather than checking if we are overflowing here, so that we
4882 * are sure to not leave arc_wait_for_eviction() waiting on aew_cv.
4883 * If we have become "not overflowing" since arc_wait_for_eviction()
4884 * checked, we need to wake it up. We could broadcast the CV here,
4885 * but arc_wait_for_eviction() may have not yet gone to sleep. We
4886 * would need to use a mutex to ensure that this function doesn't
4887 * broadcast until arc_wait_for_eviction() has gone to sleep (e.g.
4888 * the arc_evict_lock). However, the lock ordering of such a lock
4889 * would necessarily be incorrect with respect to the zthr_lock,
4890 * which is held before this function is called, and is held by
4891 * arc_wait_for_eviction() when it calls zthr_wakeup().
4893 return (arc_evict_needed
);
4897 * Keep arc_size under arc_c by running arc_evict which evicts data
4901 arc_evict_cb(void *arg
, zthr_t
*zthr
)
4903 (void) arg
, (void) zthr
;
4905 uint64_t evicted
= 0;
4906 fstrans_cookie_t cookie
= spl_fstrans_mark();
4908 /* Evict from cache */
4909 evicted
= arc_evict();
4912 * If evicted is zero, we couldn't evict anything
4913 * via arc_evict(). This could be due to hash lock
4914 * collisions, but more likely due to the majority of
4915 * arc buffers being unevictable. Therefore, even if
4916 * arc_size is above arc_c, another pass is unlikely to
4917 * be helpful and could potentially cause us to enter an
4918 * infinite loop. Additionally, zthr_iscancelled() is
4919 * checked here so that if the arc is shutting down, the
4920 * broadcast will wake any remaining arc evict waiters.
4922 mutex_enter(&arc_evict_lock
);
4923 arc_evict_needed
= !zthr_iscancelled(arc_evict_zthr
) &&
4924 evicted
> 0 && aggsum_compare(&arc_sums
.arcstat_size
, arc_c
) > 0;
4925 if (!arc_evict_needed
) {
4927 * We're either no longer overflowing, or we
4928 * can't evict anything more, so we should wake
4929 * arc_get_data_impl() sooner.
4931 arc_evict_waiter_t
*aw
;
4932 while ((aw
= list_remove_head(&arc_evict_waiters
)) != NULL
) {
4933 cv_broadcast(&aw
->aew_cv
);
4935 arc_set_need_free();
4937 mutex_exit(&arc_evict_lock
);
4938 spl_fstrans_unmark(cookie
);
4942 arc_reap_cb_check(void *arg
, zthr_t
*zthr
)
4944 (void) arg
, (void) zthr
;
4946 int64_t free_memory
= arc_available_memory();
4947 static int reap_cb_check_counter
= 0;
4950 * If a kmem reap is already active, don't schedule more. We must
4951 * check for this because kmem_cache_reap_soon() won't actually
4952 * block on the cache being reaped (this is to prevent callers from
4953 * becoming implicitly blocked by a system-wide kmem reap -- which,
4954 * on a system with many, many full magazines, can take minutes).
4956 if (!kmem_cache_reap_active() && free_memory
< 0) {
4958 arc_no_grow
= B_TRUE
;
4961 * Wait at least zfs_grow_retry (default 5) seconds
4962 * before considering growing.
4964 arc_growtime
= gethrtime() + SEC2NSEC(arc_grow_retry
);
4966 } else if (free_memory
< arc_c
>> arc_no_grow_shift
) {
4967 arc_no_grow
= B_TRUE
;
4968 } else if (gethrtime() >= arc_growtime
) {
4969 arc_no_grow
= B_FALSE
;
4973 * Called unconditionally every 60 seconds to reclaim unused
4974 * zstd compression and decompression context. This is done
4975 * here to avoid the need for an independent thread.
4977 if (!((reap_cb_check_counter
++) % 60))
4978 zfs_zstd_cache_reap_now();
4984 * Keep enough free memory in the system by reaping the ARC's kmem
4985 * caches. To cause more slabs to be reapable, we may reduce the
4986 * target size of the cache (arc_c), causing the arc_evict_cb()
4987 * to free more buffers.
4990 arc_reap_cb(void *arg
, zthr_t
*zthr
)
4992 (void) arg
, (void) zthr
;
4994 int64_t free_memory
;
4995 fstrans_cookie_t cookie
= spl_fstrans_mark();
4998 * Kick off asynchronous kmem_reap()'s of all our caches.
5000 arc_kmem_reap_soon();
5003 * Wait at least arc_kmem_cache_reap_retry_ms between
5004 * arc_kmem_reap_soon() calls. Without this check it is possible to
5005 * end up in a situation where we spend lots of time reaping
5006 * caches, while we're near arc_c_min. Waiting here also gives the
5007 * subsequent free memory check a chance of finding that the
5008 * asynchronous reap has already freed enough memory, and we don't
5009 * need to call arc_reduce_target_size().
5011 delay((hz
* arc_kmem_cache_reap_retry_ms
+ 999) / 1000);
5014 * Reduce the target size as needed to maintain the amount of free
5015 * memory in the system at a fraction of the arc_size (1/128th by
5016 * default). If oversubscribed (free_memory < 0) then reduce the
5017 * target arc_size by the deficit amount plus the fractional
5018 * amount. If free memory is positive but less than the fractional
5019 * amount, reduce by what is needed to hit the fractional amount.
5021 free_memory
= arc_available_memory();
5024 (arc_c
>> arc_shrink_shift
) - free_memory
;
5026 arc_reduce_target_size(to_free
);
5028 spl_fstrans_unmark(cookie
);
5033 * Determine the amount of memory eligible for eviction contained in the
5034 * ARC. All clean data reported by the ghost lists can always be safely
5035 * evicted. Due to arc_c_min, the same does not hold for all clean data
5036 * contained by the regular mru and mfu lists.
5038 * In the case of the regular mru and mfu lists, we need to report as
5039 * much clean data as possible, such that evicting that same reported
5040 * data will not bring arc_size below arc_c_min. Thus, in certain
5041 * circumstances, the total amount of clean data in the mru and mfu
5042 * lists might not actually be evictable.
5044 * The following two distinct cases are accounted for:
5046 * 1. The sum of the amount of dirty data contained by both the mru and
5047 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
5048 * is greater than or equal to arc_c_min.
5049 * (i.e. amount of dirty data >= arc_c_min)
5051 * This is the easy case; all clean data contained by the mru and mfu
5052 * lists is evictable. Evicting all clean data can only drop arc_size
5053 * to the amount of dirty data, which is greater than arc_c_min.
5055 * 2. The sum of the amount of dirty data contained by both the mru and
5056 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
5057 * is less than arc_c_min.
5058 * (i.e. arc_c_min > amount of dirty data)
5060 * 2.1. arc_size is greater than or equal arc_c_min.
5061 * (i.e. arc_size >= arc_c_min > amount of dirty data)
5063 * In this case, not all clean data from the regular mru and mfu
5064 * lists is actually evictable; we must leave enough clean data
5065 * to keep arc_size above arc_c_min. Thus, the maximum amount of
5066 * evictable data from the two lists combined, is exactly the
5067 * difference between arc_size and arc_c_min.
5069 * 2.2. arc_size is less than arc_c_min
5070 * (i.e. arc_c_min > arc_size > amount of dirty data)
5072 * In this case, none of the data contained in the mru and mfu
5073 * lists is evictable, even if it's clean. Since arc_size is
5074 * already below arc_c_min, evicting any more would only
5075 * increase this negative difference.
5078 #endif /* _KERNEL */
5081 * Adapt arc info given the number of bytes we are trying to add and
5082 * the state that we are coming from. This function is only called
5083 * when we are adding new content to the cache.
5086 arc_adapt(int bytes
, arc_state_t
*state
)
5089 uint64_t arc_p_min
= (arc_c
>> arc_p_min_shift
);
5090 int64_t mrug_size
= zfs_refcount_count(&arc_mru_ghost
->arcs_size
);
5091 int64_t mfug_size
= zfs_refcount_count(&arc_mfu_ghost
->arcs_size
);
5095 * Adapt the target size of the MRU list:
5096 * - if we just hit in the MRU ghost list, then increase
5097 * the target size of the MRU list.
5098 * - if we just hit in the MFU ghost list, then increase
5099 * the target size of the MFU list by decreasing the
5100 * target size of the MRU list.
5102 if (state
== arc_mru_ghost
) {
5103 mult
= (mrug_size
>= mfug_size
) ? 1 : (mfug_size
/ mrug_size
);
5104 if (!zfs_arc_p_dampener_disable
)
5105 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
5107 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
5108 } else if (state
== arc_mfu_ghost
) {
5111 mult
= (mfug_size
>= mrug_size
) ? 1 : (mrug_size
/ mfug_size
);
5112 if (!zfs_arc_p_dampener_disable
)
5113 mult
= MIN(mult
, 10);
5115 delta
= MIN(bytes
* mult
, arc_p
);
5116 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
5118 ASSERT((int64_t)arc_p
>= 0);
5121 * Wake reap thread if we do not have any available memory
5123 if (arc_reclaim_needed()) {
5124 zthr_wakeup(arc_reap_zthr
);
5131 if (arc_c
>= arc_c_max
)
5135 * If we're within (2 * maxblocksize) bytes of the target
5136 * cache size, increment the target cache size
5138 ASSERT3U(arc_c
, >=, 2ULL << SPA_MAXBLOCKSHIFT
);
5139 if (aggsum_upper_bound(&arc_sums
.arcstat_size
) >=
5140 arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
5141 atomic_add_64(&arc_c
, (int64_t)bytes
);
5142 if (arc_c
> arc_c_max
)
5144 else if (state
== arc_anon
)
5145 atomic_add_64(&arc_p
, (int64_t)bytes
);
5149 ASSERT((int64_t)arc_p
>= 0);
5153 * Check if arc_size has grown past our upper threshold, determined by
5154 * zfs_arc_overflow_shift.
5156 static arc_ovf_level_t
5157 arc_is_overflowing(boolean_t use_reserve
)
5159 /* Always allow at least one block of overflow */
5160 int64_t overflow
= MAX(SPA_MAXBLOCKSIZE
,
5161 arc_c
>> zfs_arc_overflow_shift
);
5164 * We just compare the lower bound here for performance reasons. Our
5165 * primary goals are to make sure that the arc never grows without
5166 * bound, and that it can reach its maximum size. This check
5167 * accomplishes both goals. The maximum amount we could run over by is
5168 * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block
5169 * in the ARC. In practice, that's in the tens of MB, which is low
5170 * enough to be safe.
5172 int64_t over
= aggsum_lower_bound(&arc_sums
.arcstat_size
) -
5173 arc_c
- overflow
/ 2;
5176 return (over
< 0 ? ARC_OVF_NONE
:
5177 over
< overflow
? ARC_OVF_SOME
: ARC_OVF_SEVERE
);
5181 arc_get_data_abd(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
,
5184 arc_buf_contents_t type
= arc_buf_type(hdr
);
5186 arc_get_data_impl(hdr
, size
, tag
, alloc_flags
);
5187 if (type
== ARC_BUFC_METADATA
) {
5188 return (abd_alloc(size
, B_TRUE
));
5190 ASSERT(type
== ARC_BUFC_DATA
);
5191 return (abd_alloc(size
, B_FALSE
));
5196 arc_get_data_buf(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5198 arc_buf_contents_t type
= arc_buf_type(hdr
);
5200 arc_get_data_impl(hdr
, size
, tag
, ARC_HDR_DO_ADAPT
);
5201 if (type
== ARC_BUFC_METADATA
) {
5202 return (zio_buf_alloc(size
));
5204 ASSERT(type
== ARC_BUFC_DATA
);
5205 return (zio_data_buf_alloc(size
));
5210 * Wait for the specified amount of data (in bytes) to be evicted from the
5211 * ARC, and for there to be sufficient free memory in the system. Waiting for
5212 * eviction ensures that the memory used by the ARC decreases. Waiting for
5213 * free memory ensures that the system won't run out of free pages, regardless
5214 * of ARC behavior and settings. See arc_lowmem_init().
5217 arc_wait_for_eviction(uint64_t amount
, boolean_t use_reserve
)
5219 switch (arc_is_overflowing(use_reserve
)) {
5224 * This is a bit racy without taking arc_evict_lock, but the
5225 * worst that can happen is we either call zthr_wakeup() extra
5226 * time due to race with other thread here, or the set flag
5227 * get cleared by arc_evict_cb(), which is unlikely due to
5228 * big hysteresis, but also not important since at this level
5229 * of overflow the eviction is purely advisory. Same time
5230 * taking the global lock here every time without waiting for
5231 * the actual eviction creates a significant lock contention.
5233 if (!arc_evict_needed
) {
5234 arc_evict_needed
= B_TRUE
;
5235 zthr_wakeup(arc_evict_zthr
);
5238 case ARC_OVF_SEVERE
:
5241 arc_evict_waiter_t aw
;
5242 list_link_init(&aw
.aew_node
);
5243 cv_init(&aw
.aew_cv
, NULL
, CV_DEFAULT
, NULL
);
5245 uint64_t last_count
= 0;
5246 mutex_enter(&arc_evict_lock
);
5247 if (!list_is_empty(&arc_evict_waiters
)) {
5248 arc_evict_waiter_t
*last
=
5249 list_tail(&arc_evict_waiters
);
5250 last_count
= last
->aew_count
;
5251 } else if (!arc_evict_needed
) {
5252 arc_evict_needed
= B_TRUE
;
5253 zthr_wakeup(arc_evict_zthr
);
5256 * Note, the last waiter's count may be less than
5257 * arc_evict_count if we are low on memory in which
5258 * case arc_evict_state_impl() may have deferred
5259 * wakeups (but still incremented arc_evict_count).
5261 aw
.aew_count
= MAX(last_count
, arc_evict_count
) + amount
;
5263 list_insert_tail(&arc_evict_waiters
, &aw
);
5265 arc_set_need_free();
5267 DTRACE_PROBE3(arc__wait__for__eviction
,
5269 uint64_t, arc_evict_count
,
5270 uint64_t, aw
.aew_count
);
5273 * We will be woken up either when arc_evict_count reaches
5274 * aew_count, or when the ARC is no longer overflowing and
5275 * eviction completes.
5276 * In case of "false" wakeup, we will still be on the list.
5279 cv_wait(&aw
.aew_cv
, &arc_evict_lock
);
5280 } while (list_link_active(&aw
.aew_node
));
5281 mutex_exit(&arc_evict_lock
);
5283 cv_destroy(&aw
.aew_cv
);
5289 * Allocate a block and return it to the caller. If we are hitting the
5290 * hard limit for the cache size, we must sleep, waiting for the eviction
5291 * thread to catch up. If we're past the target size but below the hard
5292 * limit, we'll only signal the reclaim thread and continue on.
5295 arc_get_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
,
5298 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5299 arc_buf_contents_t type
= arc_buf_type(hdr
);
5301 if (alloc_flags
& ARC_HDR_DO_ADAPT
)
5302 arc_adapt(size
, state
);
5305 * If arc_size is currently overflowing, we must be adding data
5306 * faster than we are evicting. To ensure we don't compound the
5307 * problem by adding more data and forcing arc_size to grow even
5308 * further past it's target size, we wait for the eviction thread to
5309 * make some progress. We also wait for there to be sufficient free
5310 * memory in the system, as measured by arc_free_memory().
5312 * Specifically, we wait for zfs_arc_eviction_pct percent of the
5313 * requested size to be evicted. This should be more than 100%, to
5314 * ensure that that progress is also made towards getting arc_size
5315 * under arc_c. See the comment above zfs_arc_eviction_pct.
5317 arc_wait_for_eviction(size
* zfs_arc_eviction_pct
/ 100,
5318 alloc_flags
& ARC_HDR_USE_RESERVE
);
5320 VERIFY3U(hdr
->b_type
, ==, type
);
5321 if (type
== ARC_BUFC_METADATA
) {
5322 arc_space_consume(size
, ARC_SPACE_META
);
5324 arc_space_consume(size
, ARC_SPACE_DATA
);
5328 * Update the state size. Note that ghost states have a
5329 * "ghost size" and so don't need to be updated.
5331 if (!GHOST_STATE(state
)) {
5333 (void) zfs_refcount_add_many(&state
->arcs_size
, size
, tag
);
5336 * If this is reached via arc_read, the link is
5337 * protected by the hash lock. If reached via
5338 * arc_buf_alloc, the header should not be accessed by
5339 * any other thread. And, if reached via arc_read_done,
5340 * the hash lock will protect it if it's found in the
5341 * hash table; otherwise no other thread should be
5342 * trying to [add|remove]_reference it.
5344 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5345 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5346 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
5351 * If we are growing the cache, and we are adding anonymous
5352 * data, and we have outgrown arc_p, update arc_p
5354 if (aggsum_upper_bound(&arc_sums
.arcstat_size
) < arc_c
&&
5355 hdr
->b_l1hdr
.b_state
== arc_anon
&&
5356 (zfs_refcount_count(&arc_anon
->arcs_size
) +
5357 zfs_refcount_count(&arc_mru
->arcs_size
) > arc_p
))
5358 arc_p
= MIN(arc_c
, arc_p
+ size
);
5363 arc_free_data_abd(arc_buf_hdr_t
*hdr
, abd_t
*abd
, uint64_t size
, void *tag
)
5365 arc_free_data_impl(hdr
, size
, tag
);
5370 arc_free_data_buf(arc_buf_hdr_t
*hdr
, void *buf
, uint64_t size
, void *tag
)
5372 arc_buf_contents_t type
= arc_buf_type(hdr
);
5374 arc_free_data_impl(hdr
, size
, tag
);
5375 if (type
== ARC_BUFC_METADATA
) {
5376 zio_buf_free(buf
, size
);
5378 ASSERT(type
== ARC_BUFC_DATA
);
5379 zio_data_buf_free(buf
, size
);
5384 * Free the arc data buffer.
5387 arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
5389 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5390 arc_buf_contents_t type
= arc_buf_type(hdr
);
5392 /* protected by hash lock, if in the hash table */
5393 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5394 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5395 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
5397 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
5400 (void) zfs_refcount_remove_many(&state
->arcs_size
, size
, tag
);
5402 VERIFY3U(hdr
->b_type
, ==, type
);
5403 if (type
== ARC_BUFC_METADATA
) {
5404 arc_space_return(size
, ARC_SPACE_META
);
5406 ASSERT(type
== ARC_BUFC_DATA
);
5407 arc_space_return(size
, ARC_SPACE_DATA
);
5412 * This routine is called whenever a buffer is accessed.
5413 * NOTE: the hash lock is dropped in this function.
5416 arc_access(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
5420 ASSERT(MUTEX_HELD(hash_lock
));
5421 ASSERT(HDR_HAS_L1HDR(hdr
));
5423 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
5425 * This buffer is not in the cache, and does not
5426 * appear in our "ghost" list. Add the new buffer
5430 ASSERT0(hdr
->b_l1hdr
.b_arc_access
);
5431 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5432 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5433 arc_change_state(arc_mru
, hdr
, hash_lock
);
5435 } else if (hdr
->b_l1hdr
.b_state
== arc_mru
) {
5436 now
= ddi_get_lbolt();
5439 * If this buffer is here because of a prefetch, then either:
5440 * - clear the flag if this is a "referencing" read
5441 * (any subsequent access will bump this into the MFU state).
5443 * - move the buffer to the head of the list if this is
5444 * another prefetch (to make it less likely to be evicted).
5446 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5447 if (zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
5448 /* link protected by hash lock */
5449 ASSERT(multilist_link_active(
5450 &hdr
->b_l1hdr
.b_arc_node
));
5452 if (HDR_HAS_L2HDR(hdr
))
5453 l2arc_hdr_arcstats_decrement_state(hdr
);
5454 arc_hdr_clear_flags(hdr
,
5456 ARC_FLAG_PRESCIENT_PREFETCH
);
5457 hdr
->b_l1hdr
.b_mru_hits
++;
5458 ARCSTAT_BUMP(arcstat_mru_hits
);
5459 if (HDR_HAS_L2HDR(hdr
))
5460 l2arc_hdr_arcstats_increment_state(hdr
);
5462 hdr
->b_l1hdr
.b_arc_access
= now
;
5467 * This buffer has been "accessed" only once so far,
5468 * but it is still in the cache. Move it to the MFU
5471 if (ddi_time_after(now
, hdr
->b_l1hdr
.b_arc_access
+
5474 * More than 125ms have passed since we
5475 * instantiated this buffer. Move it to the
5476 * most frequently used state.
5478 hdr
->b_l1hdr
.b_arc_access
= now
;
5479 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5480 arc_change_state(arc_mfu
, hdr
, hash_lock
);
5482 hdr
->b_l1hdr
.b_mru_hits
++;
5483 ARCSTAT_BUMP(arcstat_mru_hits
);
5484 } else if (hdr
->b_l1hdr
.b_state
== arc_mru_ghost
) {
5485 arc_state_t
*new_state
;
5487 * This buffer has been "accessed" recently, but
5488 * was evicted from the cache. Move it to the
5491 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5492 new_state
= arc_mru
;
5493 if (zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0) {
5494 if (HDR_HAS_L2HDR(hdr
))
5495 l2arc_hdr_arcstats_decrement_state(hdr
);
5496 arc_hdr_clear_flags(hdr
,
5498 ARC_FLAG_PRESCIENT_PREFETCH
);
5499 if (HDR_HAS_L2HDR(hdr
))
5500 l2arc_hdr_arcstats_increment_state(hdr
);
5502 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5504 new_state
= arc_mfu
;
5505 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5508 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5509 arc_change_state(new_state
, hdr
, hash_lock
);
5511 hdr
->b_l1hdr
.b_mru_ghost_hits
++;
5512 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
5513 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu
) {
5515 * This buffer has been accessed more than once and is
5516 * still in the cache. Keep it in the MFU state.
5518 * NOTE: an add_reference() that occurred when we did
5519 * the arc_read() will have kicked this off the list.
5520 * If it was a prefetch, we will explicitly move it to
5521 * the head of the list now.
5524 hdr
->b_l1hdr
.b_mfu_hits
++;
5525 ARCSTAT_BUMP(arcstat_mfu_hits
);
5526 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5527 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu_ghost
) {
5528 arc_state_t
*new_state
= arc_mfu
;
5530 * This buffer has been accessed more than once but has
5531 * been evicted from the cache. Move it back to the
5535 if (HDR_PREFETCH(hdr
) || HDR_PRESCIENT_PREFETCH(hdr
)) {
5537 * This is a prefetch access...
5538 * move this block back to the MRU state.
5540 new_state
= arc_mru
;
5543 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5544 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5545 arc_change_state(new_state
, hdr
, hash_lock
);
5547 hdr
->b_l1hdr
.b_mfu_ghost_hits
++;
5548 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
5549 } else if (hdr
->b_l1hdr
.b_state
== arc_l2c_only
) {
5551 * This buffer is on the 2nd Level ARC.
5554 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
5555 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5556 arc_change_state(arc_mfu
, hdr
, hash_lock
);
5558 cmn_err(CE_PANIC
, "invalid arc state 0x%p",
5559 hdr
->b_l1hdr
.b_state
);
5564 * This routine is called by dbuf_hold() to update the arc_access() state
5565 * which otherwise would be skipped for entries in the dbuf cache.
5568 arc_buf_access(arc_buf_t
*buf
)
5570 mutex_enter(&buf
->b_evict_lock
);
5571 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5574 * Avoid taking the hash_lock when possible as an optimization.
5575 * The header must be checked again under the hash_lock in order
5576 * to handle the case where it is concurrently being released.
5578 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5579 mutex_exit(&buf
->b_evict_lock
);
5583 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
5584 mutex_enter(hash_lock
);
5586 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5587 mutex_exit(hash_lock
);
5588 mutex_exit(&buf
->b_evict_lock
);
5589 ARCSTAT_BUMP(arcstat_access_skip
);
5593 mutex_exit(&buf
->b_evict_lock
);
5595 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
5596 hdr
->b_l1hdr
.b_state
== arc_mfu
);
5598 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
5599 arc_access(hdr
, hash_lock
);
5600 mutex_exit(hash_lock
);
5602 ARCSTAT_BUMP(arcstat_hits
);
5603 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
) && !HDR_PRESCIENT_PREFETCH(hdr
),
5604 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
), data
, metadata
, hits
);
5607 /* a generic arc_read_done_func_t which you can use */
5609 arc_bcopy_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5610 arc_buf_t
*buf
, void *arg
)
5612 (void) zio
, (void) zb
, (void) bp
;
5617 bcopy(buf
->b_data
, arg
, arc_buf_size(buf
));
5618 arc_buf_destroy(buf
, arg
);
5621 /* a generic arc_read_done_func_t */
5623 arc_getbuf_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5624 arc_buf_t
*buf
, void *arg
)
5626 (void) zb
, (void) bp
;
5627 arc_buf_t
**bufp
= arg
;
5630 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
5633 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
5635 ASSERT(buf
->b_data
!= NULL
);
5640 arc_hdr_verify(arc_buf_hdr_t
*hdr
, blkptr_t
*bp
)
5642 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
5643 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, 0);
5644 ASSERT3U(arc_hdr_get_compress(hdr
), ==, ZIO_COMPRESS_OFF
);
5646 if (HDR_COMPRESSION_ENABLED(hdr
)) {
5647 ASSERT3U(arc_hdr_get_compress(hdr
), ==,
5648 BP_GET_COMPRESS(bp
));
5650 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
5651 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, BP_GET_PSIZE(bp
));
5652 ASSERT3U(!!HDR_PROTECTED(hdr
), ==, BP_IS_PROTECTED(bp
));
5657 arc_read_done(zio_t
*zio
)
5659 blkptr_t
*bp
= zio
->io_bp
;
5660 arc_buf_hdr_t
*hdr
= zio
->io_private
;
5661 kmutex_t
*hash_lock
= NULL
;
5662 arc_callback_t
*callback_list
;
5663 arc_callback_t
*acb
;
5664 boolean_t freeable
= B_FALSE
;
5667 * The hdr was inserted into hash-table and removed from lists
5668 * prior to starting I/O. We should find this header, since
5669 * it's in the hash table, and it should be legit since it's
5670 * not possible to evict it during the I/O. The only possible
5671 * reason for it not to be found is if we were freed during the
5674 if (HDR_IN_HASH_TABLE(hdr
)) {
5675 arc_buf_hdr_t
*found
;
5677 ASSERT3U(hdr
->b_birth
, ==, BP_PHYSICAL_BIRTH(zio
->io_bp
));
5678 ASSERT3U(hdr
->b_dva
.dva_word
[0], ==,
5679 BP_IDENTITY(zio
->io_bp
)->dva_word
[0]);
5680 ASSERT3U(hdr
->b_dva
.dva_word
[1], ==,
5681 BP_IDENTITY(zio
->io_bp
)->dva_word
[1]);
5683 found
= buf_hash_find(hdr
->b_spa
, zio
->io_bp
, &hash_lock
);
5685 ASSERT((found
== hdr
&&
5686 DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
5687 (found
== hdr
&& HDR_L2_READING(hdr
)));
5688 ASSERT3P(hash_lock
, !=, NULL
);
5691 if (BP_IS_PROTECTED(bp
)) {
5692 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
5693 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
5694 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
5695 hdr
->b_crypt_hdr
.b_iv
);
5697 if (zio
->io_error
== 0) {
5698 if (BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
) {
5701 tmpbuf
= abd_borrow_buf_copy(zio
->io_abd
,
5702 sizeof (zil_chain_t
));
5703 zio_crypt_decode_mac_zil(tmpbuf
,
5704 hdr
->b_crypt_hdr
.b_mac
);
5705 abd_return_buf(zio
->io_abd
, tmpbuf
,
5706 sizeof (zil_chain_t
));
5708 zio_crypt_decode_mac_bp(bp
,
5709 hdr
->b_crypt_hdr
.b_mac
);
5714 if (zio
->io_error
== 0) {
5715 /* byteswap if necessary */
5716 if (BP_SHOULD_BYTESWAP(zio
->io_bp
)) {
5717 if (BP_GET_LEVEL(zio
->io_bp
) > 0) {
5718 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
5720 hdr
->b_l1hdr
.b_byteswap
=
5721 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
5724 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
5726 if (!HDR_L2_READING(hdr
)) {
5727 hdr
->b_complevel
= zio
->io_prop
.zp_complevel
;
5731 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_EVICTED
);
5732 if (l2arc_noprefetch
&& HDR_PREFETCH(hdr
))
5733 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2CACHE
);
5735 callback_list
= hdr
->b_l1hdr
.b_acb
;
5736 ASSERT3P(callback_list
, !=, NULL
);
5738 if (hash_lock
&& zio
->io_error
== 0 &&
5739 hdr
->b_l1hdr
.b_state
== arc_anon
) {
5741 * Only call arc_access on anonymous buffers. This is because
5742 * if we've issued an I/O for an evicted buffer, we've already
5743 * called arc_access (to prevent any simultaneous readers from
5744 * getting confused).
5746 arc_access(hdr
, hash_lock
);
5750 * If a read request has a callback (i.e. acb_done is not NULL), then we
5751 * make a buf containing the data according to the parameters which were
5752 * passed in. The implementation of arc_buf_alloc_impl() ensures that we
5753 * aren't needlessly decompressing the data multiple times.
5755 int callback_cnt
= 0;
5756 for (acb
= callback_list
; acb
!= NULL
; acb
= acb
->acb_next
) {
5757 if (!acb
->acb_done
|| acb
->acb_nobuf
)
5762 if (zio
->io_error
!= 0)
5765 int error
= arc_buf_alloc_impl(hdr
, zio
->io_spa
,
5766 &acb
->acb_zb
, acb
->acb_private
, acb
->acb_encrypted
,
5767 acb
->acb_compressed
, acb
->acb_noauth
, B_TRUE
,
5771 * Assert non-speculative zios didn't fail because an
5772 * encryption key wasn't loaded
5774 ASSERT((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) ||
5778 * If we failed to decrypt, report an error now (as the zio
5779 * layer would have done if it had done the transforms).
5781 if (error
== ECKSUM
) {
5782 ASSERT(BP_IS_PROTECTED(bp
));
5783 error
= SET_ERROR(EIO
);
5784 if ((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
5785 spa_log_error(zio
->io_spa
, &acb
->acb_zb
);
5786 (void) zfs_ereport_post(
5787 FM_EREPORT_ZFS_AUTHENTICATION
,
5788 zio
->io_spa
, NULL
, &acb
->acb_zb
, zio
, 0);
5794 * Decompression or decryption failed. Set
5795 * io_error so that when we call acb_done
5796 * (below), we will indicate that the read
5797 * failed. Note that in the unusual case
5798 * where one callback is compressed and another
5799 * uncompressed, we will mark all of them
5800 * as failed, even though the uncompressed
5801 * one can't actually fail. In this case,
5802 * the hdr will not be anonymous, because
5803 * if there are multiple callbacks, it's
5804 * because multiple threads found the same
5805 * arc buf in the hash table.
5807 zio
->io_error
= error
;
5812 * If there are multiple callbacks, we must have the hash lock,
5813 * because the only way for multiple threads to find this hdr is
5814 * in the hash table. This ensures that if there are multiple
5815 * callbacks, the hdr is not anonymous. If it were anonymous,
5816 * we couldn't use arc_buf_destroy() in the error case below.
5818 ASSERT(callback_cnt
< 2 || hash_lock
!= NULL
);
5820 hdr
->b_l1hdr
.b_acb
= NULL
;
5821 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5822 if (callback_cnt
== 0)
5823 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
5825 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
) ||
5826 callback_list
!= NULL
);
5828 if (zio
->io_error
== 0) {
5829 arc_hdr_verify(hdr
, zio
->io_bp
);
5831 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
5832 if (hdr
->b_l1hdr
.b_state
!= arc_anon
)
5833 arc_change_state(arc_anon
, hdr
, hash_lock
);
5834 if (HDR_IN_HASH_TABLE(hdr
))
5835 buf_hash_remove(hdr
);
5836 freeable
= zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
5840 * Broadcast before we drop the hash_lock to avoid the possibility
5841 * that the hdr (and hence the cv) might be freed before we get to
5842 * the cv_broadcast().
5844 cv_broadcast(&hdr
->b_l1hdr
.b_cv
);
5846 if (hash_lock
!= NULL
) {
5847 mutex_exit(hash_lock
);
5850 * This block was freed while we waited for the read to
5851 * complete. It has been removed from the hash table and
5852 * moved to the anonymous state (so that it won't show up
5855 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
5856 freeable
= zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
5859 /* execute each callback and free its structure */
5860 while ((acb
= callback_list
) != NULL
) {
5861 if (acb
->acb_done
!= NULL
) {
5862 if (zio
->io_error
!= 0 && acb
->acb_buf
!= NULL
) {
5864 * If arc_buf_alloc_impl() fails during
5865 * decompression, the buf will still be
5866 * allocated, and needs to be freed here.
5868 arc_buf_destroy(acb
->acb_buf
,
5870 acb
->acb_buf
= NULL
;
5872 acb
->acb_done(zio
, &zio
->io_bookmark
, zio
->io_bp
,
5873 acb
->acb_buf
, acb
->acb_private
);
5876 if (acb
->acb_zio_dummy
!= NULL
) {
5877 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
5878 zio_nowait(acb
->acb_zio_dummy
);
5881 callback_list
= acb
->acb_next
;
5882 kmem_free(acb
, sizeof (arc_callback_t
));
5886 arc_hdr_destroy(hdr
);
5890 * "Read" the block at the specified DVA (in bp) via the
5891 * cache. If the block is found in the cache, invoke the provided
5892 * callback immediately and return. Note that the `zio' parameter
5893 * in the callback will be NULL in this case, since no IO was
5894 * required. If the block is not in the cache pass the read request
5895 * on to the spa with a substitute callback function, so that the
5896 * requested block will be added to the cache.
5898 * If a read request arrives for a block that has a read in-progress,
5899 * either wait for the in-progress read to complete (and return the
5900 * results); or, if this is a read with a "done" func, add a record
5901 * to the read to invoke the "done" func when the read completes,
5902 * and return; or just return.
5904 * arc_read_done() will invoke all the requested "done" functions
5905 * for readers of this block.
5908 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
5909 arc_read_done_func_t
*done
, void *private, zio_priority_t priority
,
5910 int zio_flags
, arc_flags_t
*arc_flags
, const zbookmark_phys_t
*zb
)
5912 arc_buf_hdr_t
*hdr
= NULL
;
5913 kmutex_t
*hash_lock
= NULL
;
5915 uint64_t guid
= spa_load_guid(spa
);
5916 boolean_t compressed_read
= (zio_flags
& ZIO_FLAG_RAW_COMPRESS
) != 0;
5917 boolean_t encrypted_read
= BP_IS_ENCRYPTED(bp
) &&
5918 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
5919 boolean_t noauth_read
= BP_IS_AUTHENTICATED(bp
) &&
5920 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
5921 boolean_t embedded_bp
= !!BP_IS_EMBEDDED(bp
);
5922 boolean_t no_buf
= *arc_flags
& ARC_FLAG_NO_BUF
;
5925 ASSERT(!embedded_bp
||
5926 BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
);
5927 ASSERT(!BP_IS_HOLE(bp
));
5928 ASSERT(!BP_IS_REDACTED(bp
));
5931 * Normally SPL_FSTRANS will already be set since kernel threads which
5932 * expect to call the DMU interfaces will set it when created. System
5933 * calls are similarly handled by setting/cleaning the bit in the
5934 * registered callback (module/os/.../zfs/zpl_*).
5936 * External consumers such as Lustre which call the exported DMU
5937 * interfaces may not have set SPL_FSTRANS. To avoid a deadlock
5938 * on the hash_lock always set and clear the bit.
5940 fstrans_cookie_t cookie
= spl_fstrans_mark();
5943 * Verify the block pointer contents are reasonable. This should
5944 * always be the case since the blkptr is protected by a checksum.
5945 * However, if there is damage it's desirable to detect this early
5946 * and treat it as a checksum error. This allows an alternate blkptr
5947 * to be tried when one is available (e.g. ditto blocks).
5949 if (!zfs_blkptr_verify(spa
, bp
, zio_flags
& ZIO_FLAG_CONFIG_WRITER
,
5951 rc
= SET_ERROR(ECKSUM
);
5957 * Embedded BP's have no DVA and require no I/O to "read".
5958 * Create an anonymous arc buf to back it.
5960 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
5964 * Determine if we have an L1 cache hit or a cache miss. For simplicity
5965 * we maintain encrypted data separately from compressed / uncompressed
5966 * data. If the user is requesting raw encrypted data and we don't have
5967 * that in the header we will read from disk to guarantee that we can
5968 * get it even if the encryption keys aren't loaded.
5970 if (hdr
!= NULL
&& HDR_HAS_L1HDR(hdr
) && (HDR_HAS_RABD(hdr
) ||
5971 (hdr
->b_l1hdr
.b_pabd
!= NULL
&& !encrypted_read
))) {
5972 arc_buf_t
*buf
= NULL
;
5973 *arc_flags
|= ARC_FLAG_CACHED
;
5975 if (HDR_IO_IN_PROGRESS(hdr
)) {
5976 zio_t
*head_zio
= hdr
->b_l1hdr
.b_acb
->acb_zio_head
;
5978 if (*arc_flags
& ARC_FLAG_CACHED_ONLY
) {
5979 mutex_exit(hash_lock
);
5980 ARCSTAT_BUMP(arcstat_cached_only_in_progress
);
5981 rc
= SET_ERROR(ENOENT
);
5985 ASSERT3P(head_zio
, !=, NULL
);
5986 if ((hdr
->b_flags
& ARC_FLAG_PRIO_ASYNC_READ
) &&
5987 priority
== ZIO_PRIORITY_SYNC_READ
) {
5989 * This is a sync read that needs to wait for
5990 * an in-flight async read. Request that the
5991 * zio have its priority upgraded.
5993 zio_change_priority(head_zio
, priority
);
5994 DTRACE_PROBE1(arc__async__upgrade__sync
,
5995 arc_buf_hdr_t
*, hdr
);
5996 ARCSTAT_BUMP(arcstat_async_upgrade_sync
);
5998 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
5999 arc_hdr_clear_flags(hdr
,
6000 ARC_FLAG_PREDICTIVE_PREFETCH
);
6003 if (*arc_flags
& ARC_FLAG_WAIT
) {
6004 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
6005 mutex_exit(hash_lock
);
6008 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
6011 arc_callback_t
*acb
= NULL
;
6013 acb
= kmem_zalloc(sizeof (arc_callback_t
),
6015 acb
->acb_done
= done
;
6016 acb
->acb_private
= private;
6017 acb
->acb_compressed
= compressed_read
;
6018 acb
->acb_encrypted
= encrypted_read
;
6019 acb
->acb_noauth
= noauth_read
;
6020 acb
->acb_nobuf
= no_buf
;
6023 acb
->acb_zio_dummy
= zio_null(pio
,
6024 spa
, NULL
, NULL
, NULL
, zio_flags
);
6026 ASSERT3P(acb
->acb_done
, !=, NULL
);
6027 acb
->acb_zio_head
= head_zio
;
6028 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
6029 hdr
->b_l1hdr
.b_acb
= acb
;
6031 mutex_exit(hash_lock
);
6035 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
6036 hdr
->b_l1hdr
.b_state
== arc_mfu
);
6038 if (done
&& !no_buf
) {
6039 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
6041 * This is a demand read which does not have to
6042 * wait for i/o because we did a predictive
6043 * prefetch i/o for it, which has completed.
6046 arc__demand__hit__predictive__prefetch
,
6047 arc_buf_hdr_t
*, hdr
);
6049 arcstat_demand_hit_predictive_prefetch
);
6050 arc_hdr_clear_flags(hdr
,
6051 ARC_FLAG_PREDICTIVE_PREFETCH
);
6054 if (hdr
->b_flags
& ARC_FLAG_PRESCIENT_PREFETCH
) {
6056 arcstat_demand_hit_prescient_prefetch
);
6057 arc_hdr_clear_flags(hdr
,
6058 ARC_FLAG_PRESCIENT_PREFETCH
);
6061 ASSERT(!embedded_bp
|| !BP_IS_HOLE(bp
));
6063 /* Get a buf with the desired data in it. */
6064 rc
= arc_buf_alloc_impl(hdr
, spa
, zb
, private,
6065 encrypted_read
, compressed_read
, noauth_read
,
6069 * Convert authentication and decryption errors
6070 * to EIO (and generate an ereport if needed)
6071 * before leaving the ARC.
6073 rc
= SET_ERROR(EIO
);
6074 if ((zio_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
6075 spa_log_error(spa
, zb
);
6076 (void) zfs_ereport_post(
6077 FM_EREPORT_ZFS_AUTHENTICATION
,
6078 spa
, NULL
, zb
, NULL
, 0);
6082 (void) remove_reference(hdr
, hash_lock
,
6084 arc_buf_destroy_impl(buf
);
6088 /* assert any errors weren't due to unloaded keys */
6089 ASSERT((zio_flags
& ZIO_FLAG_SPECULATIVE
) ||
6091 } else if (*arc_flags
& ARC_FLAG_PREFETCH
&&
6092 zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
6093 if (HDR_HAS_L2HDR(hdr
))
6094 l2arc_hdr_arcstats_decrement_state(hdr
);
6095 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
6096 if (HDR_HAS_L2HDR(hdr
))
6097 l2arc_hdr_arcstats_increment_state(hdr
);
6099 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
6100 arc_access(hdr
, hash_lock
);
6101 if (*arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
)
6102 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
6103 if (*arc_flags
& ARC_FLAG_L2CACHE
)
6104 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6105 mutex_exit(hash_lock
);
6106 ARCSTAT_BUMP(arcstat_hits
);
6107 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
6108 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
6109 data
, metadata
, hits
);
6112 done(NULL
, zb
, bp
, buf
, private);
6114 uint64_t lsize
= BP_GET_LSIZE(bp
);
6115 uint64_t psize
= BP_GET_PSIZE(bp
);
6116 arc_callback_t
*acb
;
6119 boolean_t devw
= B_FALSE
;
6122 int alloc_flags
= encrypted_read
? ARC_HDR_ALLOC_RDATA
: 0;
6124 if (*arc_flags
& ARC_FLAG_CACHED_ONLY
) {
6125 rc
= SET_ERROR(ENOENT
);
6126 if (hash_lock
!= NULL
)
6127 mutex_exit(hash_lock
);
6133 * This block is not in the cache or it has
6136 arc_buf_hdr_t
*exists
= NULL
;
6137 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
6138 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
6139 BP_IS_PROTECTED(bp
), BP_GET_COMPRESS(bp
), 0, type
);
6142 hdr
->b_dva
= *BP_IDENTITY(bp
);
6143 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
6144 exists
= buf_hash_insert(hdr
, &hash_lock
);
6146 if (exists
!= NULL
) {
6147 /* somebody beat us to the hash insert */
6148 mutex_exit(hash_lock
);
6149 buf_discard_identity(hdr
);
6150 arc_hdr_destroy(hdr
);
6151 goto top
; /* restart the IO request */
6153 alloc_flags
|= ARC_HDR_DO_ADAPT
;
6156 * This block is in the ghost cache or encrypted data
6157 * was requested and we didn't have it. If it was
6158 * L2-only (and thus didn't have an L1 hdr),
6159 * we realloc the header to add an L1 hdr.
6161 if (!HDR_HAS_L1HDR(hdr
)) {
6162 hdr
= arc_hdr_realloc(hdr
, hdr_l2only_cache
,
6166 if (GHOST_STATE(hdr
->b_l1hdr
.b_state
)) {
6167 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6168 ASSERT(!HDR_HAS_RABD(hdr
));
6169 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6170 ASSERT0(zfs_refcount_count(
6171 &hdr
->b_l1hdr
.b_refcnt
));
6172 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
6173 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
6174 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
6176 * If this header already had an IO in progress
6177 * and we are performing another IO to fetch
6178 * encrypted data we must wait until the first
6179 * IO completes so as not to confuse
6180 * arc_read_done(). This should be very rare
6181 * and so the performance impact shouldn't
6184 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
6185 mutex_exit(hash_lock
);
6190 * This is a delicate dance that we play here.
6191 * This hdr might be in the ghost list so we access
6192 * it to move it out of the ghost list before we
6193 * initiate the read. If it's a prefetch then
6194 * it won't have a callback so we'll remove the
6195 * reference that arc_buf_alloc_impl() created. We
6196 * do this after we've called arc_access() to
6197 * avoid hitting an assert in remove_reference().
6199 arc_adapt(arc_hdr_size(hdr
), hdr
->b_l1hdr
.b_state
);
6200 arc_access(hdr
, hash_lock
);
6203 arc_hdr_alloc_abd(hdr
, alloc_flags
);
6204 if (encrypted_read
) {
6205 ASSERT(HDR_HAS_RABD(hdr
));
6206 size
= HDR_GET_PSIZE(hdr
);
6207 hdr_abd
= hdr
->b_crypt_hdr
.b_rabd
;
6208 zio_flags
|= ZIO_FLAG_RAW
;
6210 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
6211 size
= arc_hdr_size(hdr
);
6212 hdr_abd
= hdr
->b_l1hdr
.b_pabd
;
6214 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
6215 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
6219 * For authenticated bp's, we do not ask the ZIO layer
6220 * to authenticate them since this will cause the entire
6221 * IO to fail if the key isn't loaded. Instead, we
6222 * defer authentication until arc_buf_fill(), which will
6223 * verify the data when the key is available.
6225 if (BP_IS_AUTHENTICATED(bp
))
6226 zio_flags
|= ZIO_FLAG_RAW_ENCRYPT
;
6229 if (*arc_flags
& ARC_FLAG_PREFETCH
&&
6230 zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
6231 if (HDR_HAS_L2HDR(hdr
))
6232 l2arc_hdr_arcstats_decrement_state(hdr
);
6233 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
6234 if (HDR_HAS_L2HDR(hdr
))
6235 l2arc_hdr_arcstats_increment_state(hdr
);
6237 if (*arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
)
6238 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
6239 if (*arc_flags
& ARC_FLAG_L2CACHE
)
6240 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6241 if (BP_IS_AUTHENTICATED(bp
))
6242 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6243 if (BP_GET_LEVEL(bp
) > 0)
6244 arc_hdr_set_flags(hdr
, ARC_FLAG_INDIRECT
);
6245 if (*arc_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
)
6246 arc_hdr_set_flags(hdr
, ARC_FLAG_PREDICTIVE_PREFETCH
);
6247 ASSERT(!GHOST_STATE(hdr
->b_l1hdr
.b_state
));
6249 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
6250 acb
->acb_done
= done
;
6251 acb
->acb_private
= private;
6252 acb
->acb_compressed
= compressed_read
;
6253 acb
->acb_encrypted
= encrypted_read
;
6254 acb
->acb_noauth
= noauth_read
;
6257 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6258 hdr
->b_l1hdr
.b_acb
= acb
;
6259 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6261 if (HDR_HAS_L2HDR(hdr
) &&
6262 (vd
= hdr
->b_l2hdr
.b_dev
->l2ad_vdev
) != NULL
) {
6263 devw
= hdr
->b_l2hdr
.b_dev
->l2ad_writing
;
6264 addr
= hdr
->b_l2hdr
.b_daddr
;
6266 * Lock out L2ARC device removal.
6268 if (vdev_is_dead(vd
) ||
6269 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
6274 * We count both async reads and scrub IOs as asynchronous so
6275 * that both can be upgraded in the event of a cache hit while
6276 * the read IO is still in-flight.
6278 if (priority
== ZIO_PRIORITY_ASYNC_READ
||
6279 priority
== ZIO_PRIORITY_SCRUB
)
6280 arc_hdr_set_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6282 arc_hdr_clear_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6285 * At this point, we have a level 1 cache miss or a blkptr
6286 * with embedded data. Try again in L2ARC if possible.
6288 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, lsize
);
6291 * Skip ARC stat bump for block pointers with embedded
6292 * data. The data are read from the blkptr itself via
6293 * decode_embedded_bp_compressed().
6296 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
,
6297 blkptr_t
*, bp
, uint64_t, lsize
,
6298 zbookmark_phys_t
*, zb
);
6299 ARCSTAT_BUMP(arcstat_misses
);
6300 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
6301 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
), data
,
6303 zfs_racct_read(size
, 1);
6306 /* Check if the spa even has l2 configured */
6307 const boolean_t spa_has_l2
= l2arc_ndev
!= 0 &&
6308 spa
->spa_l2cache
.sav_count
> 0;
6310 if (vd
!= NULL
&& spa_has_l2
&& !(l2arc_norw
&& devw
)) {
6312 * Read from the L2ARC if the following are true:
6313 * 1. The L2ARC vdev was previously cached.
6314 * 2. This buffer still has L2ARC metadata.
6315 * 3. This buffer isn't currently writing to the L2ARC.
6316 * 4. The L2ARC entry wasn't evicted, which may
6317 * also have invalidated the vdev.
6318 * 5. This isn't prefetch or l2arc_noprefetch is 0.
6320 if (HDR_HAS_L2HDR(hdr
) &&
6321 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
6322 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
6323 l2arc_read_callback_t
*cb
;
6327 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
6328 ARCSTAT_BUMP(arcstat_l2_hits
);
6329 hdr
->b_l2hdr
.b_hits
++;
6331 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
6333 cb
->l2rcb_hdr
= hdr
;
6336 cb
->l2rcb_flags
= zio_flags
;
6339 * When Compressed ARC is disabled, but the
6340 * L2ARC block is compressed, arc_hdr_size()
6341 * will have returned LSIZE rather than PSIZE.
6343 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
6344 !HDR_COMPRESSION_ENABLED(hdr
) &&
6345 HDR_GET_PSIZE(hdr
) != 0) {
6346 size
= HDR_GET_PSIZE(hdr
);
6349 asize
= vdev_psize_to_asize(vd
, size
);
6350 if (asize
!= size
) {
6351 abd
= abd_alloc_for_io(asize
,
6352 HDR_ISTYPE_METADATA(hdr
));
6353 cb
->l2rcb_abd
= abd
;
6358 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
6359 addr
+ asize
<= vd
->vdev_psize
-
6360 VDEV_LABEL_END_SIZE
);
6363 * l2arc read. The SCL_L2ARC lock will be
6364 * released by l2arc_read_done().
6365 * Issue a null zio if the underlying buffer
6366 * was squashed to zero size by compression.
6368 ASSERT3U(arc_hdr_get_compress(hdr
), !=,
6369 ZIO_COMPRESS_EMPTY
);
6370 rzio
= zio_read_phys(pio
, vd
, addr
,
6373 l2arc_read_done
, cb
, priority
,
6374 zio_flags
| ZIO_FLAG_DONT_CACHE
|
6376 ZIO_FLAG_DONT_PROPAGATE
|
6377 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
6378 acb
->acb_zio_head
= rzio
;
6380 if (hash_lock
!= NULL
)
6381 mutex_exit(hash_lock
);
6383 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
6385 ARCSTAT_INCR(arcstat_l2_read_bytes
,
6386 HDR_GET_PSIZE(hdr
));
6388 if (*arc_flags
& ARC_FLAG_NOWAIT
) {
6393 ASSERT(*arc_flags
& ARC_FLAG_WAIT
);
6394 if (zio_wait(rzio
) == 0)
6397 /* l2arc read error; goto zio_read() */
6398 if (hash_lock
!= NULL
)
6399 mutex_enter(hash_lock
);
6401 DTRACE_PROBE1(l2arc__miss
,
6402 arc_buf_hdr_t
*, hdr
);
6403 ARCSTAT_BUMP(arcstat_l2_misses
);
6404 if (HDR_L2_WRITING(hdr
))
6405 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
6406 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6410 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6413 * Only a spa with l2 should contribute to l2
6414 * miss stats. (Including the case of having a
6415 * faulted cache device - that's also a miss.)
6419 * Skip ARC stat bump for block pointers with
6420 * embedded data. The data are read from the
6422 * decode_embedded_bp_compressed().
6425 DTRACE_PROBE1(l2arc__miss
,
6426 arc_buf_hdr_t
*, hdr
);
6427 ARCSTAT_BUMP(arcstat_l2_misses
);
6432 rzio
= zio_read(pio
, spa
, bp
, hdr_abd
, size
,
6433 arc_read_done
, hdr
, priority
, zio_flags
, zb
);
6434 acb
->acb_zio_head
= rzio
;
6436 if (hash_lock
!= NULL
)
6437 mutex_exit(hash_lock
);
6439 if (*arc_flags
& ARC_FLAG_WAIT
) {
6440 rc
= zio_wait(rzio
);
6444 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
6449 /* embedded bps don't actually go to disk */
6451 spa_read_history_add(spa
, zb
, *arc_flags
);
6452 spl_fstrans_unmark(cookie
);
6457 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
6461 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
6463 p
->p_private
= private;
6464 list_link_init(&p
->p_node
);
6465 zfs_refcount_create(&p
->p_refcnt
);
6467 mutex_enter(&arc_prune_mtx
);
6468 zfs_refcount_add(&p
->p_refcnt
, &arc_prune_list
);
6469 list_insert_head(&arc_prune_list
, p
);
6470 mutex_exit(&arc_prune_mtx
);
6476 arc_remove_prune_callback(arc_prune_t
*p
)
6478 boolean_t wait
= B_FALSE
;
6479 mutex_enter(&arc_prune_mtx
);
6480 list_remove(&arc_prune_list
, p
);
6481 if (zfs_refcount_remove(&p
->p_refcnt
, &arc_prune_list
) > 0)
6483 mutex_exit(&arc_prune_mtx
);
6485 /* wait for arc_prune_task to finish */
6487 taskq_wait_outstanding(arc_prune_taskq
, 0);
6488 ASSERT0(zfs_refcount_count(&p
->p_refcnt
));
6489 zfs_refcount_destroy(&p
->p_refcnt
);
6490 kmem_free(p
, sizeof (*p
));
6494 * Notify the arc that a block was freed, and thus will never be used again.
6497 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
6500 kmutex_t
*hash_lock
;
6501 uint64_t guid
= spa_load_guid(spa
);
6503 ASSERT(!BP_IS_EMBEDDED(bp
));
6505 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
6510 * We might be trying to free a block that is still doing I/O
6511 * (i.e. prefetch) or has a reference (i.e. a dedup-ed,
6512 * dmu_sync-ed block). If this block is being prefetched, then it
6513 * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr
6514 * until the I/O completes. A block may also have a reference if it is
6515 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would
6516 * have written the new block to its final resting place on disk but
6517 * without the dedup flag set. This would have left the hdr in the MRU
6518 * state and discoverable. When the txg finally syncs it detects that
6519 * the block was overridden in open context and issues an override I/O.
6520 * Since this is a dedup block, the override I/O will determine if the
6521 * block is already in the DDT. If so, then it will replace the io_bp
6522 * with the bp from the DDT and allow the I/O to finish. When the I/O
6523 * reaches the done callback, dbuf_write_override_done, it will
6524 * check to see if the io_bp and io_bp_override are identical.
6525 * If they are not, then it indicates that the bp was replaced with
6526 * the bp in the DDT and the override bp is freed. This allows
6527 * us to arrive here with a reference on a block that is being
6528 * freed. So if we have an I/O in progress, or a reference to
6529 * this hdr, then we don't destroy the hdr.
6531 if (!HDR_HAS_L1HDR(hdr
) || (!HDR_IO_IN_PROGRESS(hdr
) &&
6532 zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
))) {
6533 arc_change_state(arc_anon
, hdr
, hash_lock
);
6534 arc_hdr_destroy(hdr
);
6535 mutex_exit(hash_lock
);
6537 mutex_exit(hash_lock
);
6543 * Release this buffer from the cache, making it an anonymous buffer. This
6544 * must be done after a read and prior to modifying the buffer contents.
6545 * If the buffer has more than one reference, we must make
6546 * a new hdr for the buffer.
6549 arc_release(arc_buf_t
*buf
, void *tag
)
6551 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6554 * It would be nice to assert that if its DMU metadata (level >
6555 * 0 || it's the dnode file), then it must be syncing context.
6556 * But we don't know that information at this level.
6559 mutex_enter(&buf
->b_evict_lock
);
6561 ASSERT(HDR_HAS_L1HDR(hdr
));
6564 * We don't grab the hash lock prior to this check, because if
6565 * the buffer's header is in the arc_anon state, it won't be
6566 * linked into the hash table.
6568 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
6569 mutex_exit(&buf
->b_evict_lock
);
6570 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6571 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
6572 ASSERT(!HDR_HAS_L2HDR(hdr
));
6574 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
6575 ASSERT3S(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
), ==, 1);
6576 ASSERT(!list_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6578 hdr
->b_l1hdr
.b_arc_access
= 0;
6581 * If the buf is being overridden then it may already
6582 * have a hdr that is not empty.
6584 buf_discard_identity(hdr
);
6590 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
6591 mutex_enter(hash_lock
);
6594 * This assignment is only valid as long as the hash_lock is
6595 * held, we must be careful not to reference state or the
6596 * b_state field after dropping the lock.
6598 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
6599 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
6600 ASSERT3P(state
, !=, arc_anon
);
6602 /* this buffer is not on any list */
6603 ASSERT3S(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
), >, 0);
6605 if (HDR_HAS_L2HDR(hdr
)) {
6606 mutex_enter(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6609 * We have to recheck this conditional again now that
6610 * we're holding the l2ad_mtx to prevent a race with
6611 * another thread which might be concurrently calling
6612 * l2arc_evict(). In that case, l2arc_evict() might have
6613 * destroyed the header's L2 portion as we were waiting
6614 * to acquire the l2ad_mtx.
6616 if (HDR_HAS_L2HDR(hdr
))
6617 arc_hdr_l2hdr_destroy(hdr
);
6619 mutex_exit(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6623 * Do we have more than one buf?
6625 if (hdr
->b_l1hdr
.b_bufcnt
> 1) {
6626 arc_buf_hdr_t
*nhdr
;
6627 uint64_t spa
= hdr
->b_spa
;
6628 uint64_t psize
= HDR_GET_PSIZE(hdr
);
6629 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
6630 boolean_t
protected = HDR_PROTECTED(hdr
);
6631 enum zio_compress compress
= arc_hdr_get_compress(hdr
);
6632 arc_buf_contents_t type
= arc_buf_type(hdr
);
6633 VERIFY3U(hdr
->b_type
, ==, type
);
6635 ASSERT(hdr
->b_l1hdr
.b_buf
!= buf
|| buf
->b_next
!= NULL
);
6636 (void) remove_reference(hdr
, hash_lock
, tag
);
6638 if (arc_buf_is_shared(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
6639 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6640 ASSERT(ARC_BUF_LAST(buf
));
6644 * Pull the data off of this hdr and attach it to
6645 * a new anonymous hdr. Also find the last buffer
6646 * in the hdr's buffer list.
6648 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
6649 ASSERT3P(lastbuf
, !=, NULL
);
6652 * If the current arc_buf_t and the hdr are sharing their data
6653 * buffer, then we must stop sharing that block.
6655 if (arc_buf_is_shared(buf
)) {
6656 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6657 VERIFY(!arc_buf_is_shared(lastbuf
));
6660 * First, sever the block sharing relationship between
6661 * buf and the arc_buf_hdr_t.
6663 arc_unshare_buf(hdr
, buf
);
6666 * Now we need to recreate the hdr's b_pabd. Since we
6667 * have lastbuf handy, we try to share with it, but if
6668 * we can't then we allocate a new b_pabd and copy the
6669 * data from buf into it.
6671 if (arc_can_share(hdr
, lastbuf
)) {
6672 arc_share_buf(hdr
, lastbuf
);
6674 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
);
6675 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
,
6676 buf
->b_data
, psize
);
6678 VERIFY3P(lastbuf
->b_data
, !=, NULL
);
6679 } else if (HDR_SHARED_DATA(hdr
)) {
6681 * Uncompressed shared buffers are always at the end
6682 * of the list. Compressed buffers don't have the
6683 * same requirements. This makes it hard to
6684 * simply assert that the lastbuf is shared so
6685 * we rely on the hdr's compression flags to determine
6686 * if we have a compressed, shared buffer.
6688 ASSERT(arc_buf_is_shared(lastbuf
) ||
6689 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
6690 ASSERT(!ARC_BUF_SHARED(buf
));
6693 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
6694 ASSERT3P(state
, !=, arc_l2c_only
);
6696 (void) zfs_refcount_remove_many(&state
->arcs_size
,
6697 arc_buf_size(buf
), buf
);
6699 if (zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
6700 ASSERT3P(state
, !=, arc_l2c_only
);
6701 (void) zfs_refcount_remove_many(
6702 &state
->arcs_esize
[type
],
6703 arc_buf_size(buf
), buf
);
6706 hdr
->b_l1hdr
.b_bufcnt
-= 1;
6707 if (ARC_BUF_ENCRYPTED(buf
))
6708 hdr
->b_crypt_hdr
.b_ebufcnt
-= 1;
6710 arc_cksum_verify(buf
);
6711 arc_buf_unwatch(buf
);
6713 /* if this is the last uncompressed buf free the checksum */
6714 if (!arc_hdr_has_uncompressed_buf(hdr
))
6715 arc_cksum_free(hdr
);
6717 mutex_exit(hash_lock
);
6720 * Allocate a new hdr. The new hdr will contain a b_pabd
6721 * buffer which will be freed in arc_write().
6723 nhdr
= arc_hdr_alloc(spa
, psize
, lsize
, protected,
6724 compress
, hdr
->b_complevel
, type
);
6725 ASSERT3P(nhdr
->b_l1hdr
.b_buf
, ==, NULL
);
6726 ASSERT0(nhdr
->b_l1hdr
.b_bufcnt
);
6727 ASSERT0(zfs_refcount_count(&nhdr
->b_l1hdr
.b_refcnt
));
6728 VERIFY3U(nhdr
->b_type
, ==, type
);
6729 ASSERT(!HDR_SHARED_DATA(nhdr
));
6731 nhdr
->b_l1hdr
.b_buf
= buf
;
6732 nhdr
->b_l1hdr
.b_bufcnt
= 1;
6733 if (ARC_BUF_ENCRYPTED(buf
))
6734 nhdr
->b_crypt_hdr
.b_ebufcnt
= 1;
6735 (void) zfs_refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, tag
);
6738 mutex_exit(&buf
->b_evict_lock
);
6739 (void) zfs_refcount_add_many(&arc_anon
->arcs_size
,
6740 arc_buf_size(buf
), buf
);
6742 mutex_exit(&buf
->b_evict_lock
);
6743 ASSERT(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 1);
6744 /* protected by hash lock, or hdr is on arc_anon */
6745 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6746 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6747 hdr
->b_l1hdr
.b_mru_hits
= 0;
6748 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
6749 hdr
->b_l1hdr
.b_mfu_hits
= 0;
6750 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
6751 arc_change_state(arc_anon
, hdr
, hash_lock
);
6752 hdr
->b_l1hdr
.b_arc_access
= 0;
6754 mutex_exit(hash_lock
);
6755 buf_discard_identity(hdr
);
6761 arc_released(arc_buf_t
*buf
)
6765 mutex_enter(&buf
->b_evict_lock
);
6766 released
= (buf
->b_data
!= NULL
&&
6767 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
6768 mutex_exit(&buf
->b_evict_lock
);
6774 arc_referenced(arc_buf_t
*buf
)
6778 mutex_enter(&buf
->b_evict_lock
);
6779 referenced
= (zfs_refcount_count(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6780 mutex_exit(&buf
->b_evict_lock
);
6781 return (referenced
);
6786 arc_write_ready(zio_t
*zio
)
6788 arc_write_callback_t
*callback
= zio
->io_private
;
6789 arc_buf_t
*buf
= callback
->awcb_buf
;
6790 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6791 blkptr_t
*bp
= zio
->io_bp
;
6792 uint64_t psize
= BP_IS_HOLE(bp
) ? 0 : BP_GET_PSIZE(bp
);
6793 fstrans_cookie_t cookie
= spl_fstrans_mark();
6795 ASSERT(HDR_HAS_L1HDR(hdr
));
6796 ASSERT(!zfs_refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6797 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
6800 * If we're reexecuting this zio because the pool suspended, then
6801 * cleanup any state that was previously set the first time the
6802 * callback was invoked.
6804 if (zio
->io_flags
& ZIO_FLAG_REEXECUTED
) {
6805 arc_cksum_free(hdr
);
6806 arc_buf_unwatch(buf
);
6807 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
6808 if (arc_buf_is_shared(buf
)) {
6809 arc_unshare_buf(hdr
, buf
);
6811 arc_hdr_free_abd(hdr
, B_FALSE
);
6815 if (HDR_HAS_RABD(hdr
))
6816 arc_hdr_free_abd(hdr
, B_TRUE
);
6818 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6819 ASSERT(!HDR_HAS_RABD(hdr
));
6820 ASSERT(!HDR_SHARED_DATA(hdr
));
6821 ASSERT(!arc_buf_is_shared(buf
));
6823 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
6825 if (HDR_IO_IN_PROGRESS(hdr
))
6826 ASSERT(zio
->io_flags
& ZIO_FLAG_REEXECUTED
);
6828 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6830 if (BP_IS_PROTECTED(bp
) != !!HDR_PROTECTED(hdr
))
6831 hdr
= arc_hdr_realloc_crypt(hdr
, BP_IS_PROTECTED(bp
));
6833 if (BP_IS_PROTECTED(bp
)) {
6834 /* ZIL blocks are written through zio_rewrite */
6835 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
6836 ASSERT(HDR_PROTECTED(hdr
));
6838 if (BP_SHOULD_BYTESWAP(bp
)) {
6839 if (BP_GET_LEVEL(bp
) > 0) {
6840 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
6842 hdr
->b_l1hdr
.b_byteswap
=
6843 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
6846 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
6849 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
6850 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
6851 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
6852 hdr
->b_crypt_hdr
.b_iv
);
6853 zio_crypt_decode_mac_bp(bp
, hdr
->b_crypt_hdr
.b_mac
);
6857 * If this block was written for raw encryption but the zio layer
6858 * ended up only authenticating it, adjust the buffer flags now.
6860 if (BP_IS_AUTHENTICATED(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
6861 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6862 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
6863 if (BP_GET_COMPRESS(bp
) == ZIO_COMPRESS_OFF
)
6864 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
6865 } else if (BP_IS_HOLE(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
6866 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
6867 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
6870 /* this must be done after the buffer flags are adjusted */
6871 arc_cksum_compute(buf
);
6873 enum zio_compress compress
;
6874 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
6875 compress
= ZIO_COMPRESS_OFF
;
6877 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
6878 compress
= BP_GET_COMPRESS(bp
);
6880 HDR_SET_PSIZE(hdr
, psize
);
6881 arc_hdr_set_compress(hdr
, compress
);
6882 hdr
->b_complevel
= zio
->io_prop
.zp_complevel
;
6884 if (zio
->io_error
!= 0 || psize
== 0)
6888 * Fill the hdr with data. If the buffer is encrypted we have no choice
6889 * but to copy the data into b_radb. If the hdr is compressed, the data
6890 * we want is available from the zio, otherwise we can take it from
6893 * We might be able to share the buf's data with the hdr here. However,
6894 * doing so would cause the ARC to be full of linear ABDs if we write a
6895 * lot of shareable data. As a compromise, we check whether scattered
6896 * ABDs are allowed, and assume that if they are then the user wants
6897 * the ARC to be primarily filled with them regardless of the data being
6898 * written. Therefore, if they're allowed then we allocate one and copy
6899 * the data into it; otherwise, we share the data directly if we can.
6901 if (ARC_BUF_ENCRYPTED(buf
)) {
6902 ASSERT3U(psize
, >, 0);
6903 ASSERT(ARC_BUF_COMPRESSED(buf
));
6904 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
| ARC_HDR_ALLOC_RDATA
|
6905 ARC_HDR_USE_RESERVE
);
6906 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
6907 } else if (!abd_size_alloc_linear(arc_buf_size(buf
)) ||
6908 !arc_can_share(hdr
, buf
)) {
6910 * Ideally, we would always copy the io_abd into b_pabd, but the
6911 * user may have disabled compressed ARC, thus we must check the
6912 * hdr's compression setting rather than the io_bp's.
6914 if (BP_IS_ENCRYPTED(bp
)) {
6915 ASSERT3U(psize
, >, 0);
6916 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
|
6917 ARC_HDR_ALLOC_RDATA
| ARC_HDR_USE_RESERVE
);
6918 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
6919 } else if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
6920 !ARC_BUF_COMPRESSED(buf
)) {
6921 ASSERT3U(psize
, >, 0);
6922 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
|
6923 ARC_HDR_USE_RESERVE
);
6924 abd_copy(hdr
->b_l1hdr
.b_pabd
, zio
->io_abd
, psize
);
6926 ASSERT3U(zio
->io_orig_size
, ==, arc_hdr_size(hdr
));
6927 arc_hdr_alloc_abd(hdr
, ARC_HDR_DO_ADAPT
|
6928 ARC_HDR_USE_RESERVE
);
6929 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
6933 ASSERT3P(buf
->b_data
, ==, abd_to_buf(zio
->io_orig_abd
));
6934 ASSERT3U(zio
->io_orig_size
, ==, arc_buf_size(buf
));
6935 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
6937 arc_share_buf(hdr
, buf
);
6941 arc_hdr_verify(hdr
, bp
);
6942 spl_fstrans_unmark(cookie
);
6946 arc_write_children_ready(zio_t
*zio
)
6948 arc_write_callback_t
*callback
= zio
->io_private
;
6949 arc_buf_t
*buf
= callback
->awcb_buf
;
6951 callback
->awcb_children_ready(zio
, buf
, callback
->awcb_private
);
6955 * The SPA calls this callback for each physical write that happens on behalf
6956 * of a logical write. See the comment in dbuf_write_physdone() for details.
6959 arc_write_physdone(zio_t
*zio
)
6961 arc_write_callback_t
*cb
= zio
->io_private
;
6962 if (cb
->awcb_physdone
!= NULL
)
6963 cb
->awcb_physdone(zio
, cb
->awcb_buf
, cb
->awcb_private
);
6967 arc_write_done(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
;
6973 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6975 if (zio
->io_error
== 0) {
6976 arc_hdr_verify(hdr
, zio
->io_bp
);
6978 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
6979 buf_discard_identity(hdr
);
6981 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
6982 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
6985 ASSERT(HDR_EMPTY(hdr
));
6989 * If the block to be written was all-zero or compressed enough to be
6990 * embedded in the BP, no write was performed so there will be no
6991 * dva/birth/checksum. The buffer must therefore remain anonymous
6994 if (!HDR_EMPTY(hdr
)) {
6995 arc_buf_hdr_t
*exists
;
6996 kmutex_t
*hash_lock
;
6998 ASSERT3U(zio
->io_error
, ==, 0);
7000 arc_cksum_verify(buf
);
7002 exists
= buf_hash_insert(hdr
, &hash_lock
);
7003 if (exists
!= NULL
) {
7005 * This can only happen if we overwrite for
7006 * sync-to-convergence, because we remove
7007 * buffers from the hash table when we arc_free().
7009 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
7010 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
7011 panic("bad overwrite, hdr=%p exists=%p",
7012 (void *)hdr
, (void *)exists
);
7013 ASSERT(zfs_refcount_is_zero(
7014 &exists
->b_l1hdr
.b_refcnt
));
7015 arc_change_state(arc_anon
, exists
, hash_lock
);
7016 arc_hdr_destroy(exists
);
7017 mutex_exit(hash_lock
);
7018 exists
= buf_hash_insert(hdr
, &hash_lock
);
7019 ASSERT3P(exists
, ==, NULL
);
7020 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
7022 ASSERT(zio
->io_prop
.zp_nopwrite
);
7023 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
7024 panic("bad nopwrite, hdr=%p exists=%p",
7025 (void *)hdr
, (void *)exists
);
7028 ASSERT(hdr
->b_l1hdr
.b_bufcnt
== 1);
7029 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
7030 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
7031 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
7034 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
7035 /* if it's not anon, we are doing a scrub */
7036 if (exists
== NULL
&& hdr
->b_l1hdr
.b_state
== arc_anon
)
7037 arc_access(hdr
, hash_lock
);
7038 mutex_exit(hash_lock
);
7040 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
7043 ASSERT(!zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
7044 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
7046 abd_free(zio
->io_abd
);
7047 kmem_free(callback
, sizeof (arc_write_callback_t
));
7051 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
7052 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
,
7053 const zio_prop_t
*zp
, arc_write_done_func_t
*ready
,
7054 arc_write_done_func_t
*children_ready
, arc_write_done_func_t
*physdone
,
7055 arc_write_done_func_t
*done
, void *private, zio_priority_t priority
,
7056 int zio_flags
, const zbookmark_phys_t
*zb
)
7058 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
7059 arc_write_callback_t
*callback
;
7061 zio_prop_t localprop
= *zp
;
7063 ASSERT3P(ready
, !=, NULL
);
7064 ASSERT3P(done
, !=, NULL
);
7065 ASSERT(!HDR_IO_ERROR(hdr
));
7066 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
7067 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
7068 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
7070 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
7072 if (ARC_BUF_ENCRYPTED(buf
)) {
7073 ASSERT(ARC_BUF_COMPRESSED(buf
));
7074 localprop
.zp_encrypt
= B_TRUE
;
7075 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
7076 localprop
.zp_complevel
= hdr
->b_complevel
;
7077 localprop
.zp_byteorder
=
7078 (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
7079 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
7080 bcopy(hdr
->b_crypt_hdr
.b_salt
, localprop
.zp_salt
,
7082 bcopy(hdr
->b_crypt_hdr
.b_iv
, localprop
.zp_iv
,
7084 bcopy(hdr
->b_crypt_hdr
.b_mac
, localprop
.zp_mac
,
7086 if (DMU_OT_IS_ENCRYPTED(localprop
.zp_type
)) {
7087 localprop
.zp_nopwrite
= B_FALSE
;
7088 localprop
.zp_copies
=
7089 MIN(localprop
.zp_copies
, SPA_DVAS_PER_BP
- 1);
7091 zio_flags
|= ZIO_FLAG_RAW
;
7092 } else if (ARC_BUF_COMPRESSED(buf
)) {
7093 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, arc_buf_size(buf
));
7094 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
7095 localprop
.zp_complevel
= hdr
->b_complevel
;
7096 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
7098 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
7099 callback
->awcb_ready
= ready
;
7100 callback
->awcb_children_ready
= children_ready
;
7101 callback
->awcb_physdone
= physdone
;
7102 callback
->awcb_done
= done
;
7103 callback
->awcb_private
= private;
7104 callback
->awcb_buf
= buf
;
7107 * The hdr's b_pabd is now stale, free it now. A new data block
7108 * will be allocated when the zio pipeline calls arc_write_ready().
7110 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
7112 * If the buf is currently sharing the data block with
7113 * the hdr then we need to break that relationship here.
7114 * The hdr will remain with a NULL data pointer and the
7115 * buf will take sole ownership of the block.
7117 if (arc_buf_is_shared(buf
)) {
7118 arc_unshare_buf(hdr
, buf
);
7120 arc_hdr_free_abd(hdr
, B_FALSE
);
7122 VERIFY3P(buf
->b_data
, !=, NULL
);
7125 if (HDR_HAS_RABD(hdr
))
7126 arc_hdr_free_abd(hdr
, B_TRUE
);
7128 if (!(zio_flags
& ZIO_FLAG_RAW
))
7129 arc_hdr_set_compress(hdr
, ZIO_COMPRESS_OFF
);
7131 ASSERT(!arc_buf_is_shared(buf
));
7132 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
7134 zio
= zio_write(pio
, spa
, txg
, bp
,
7135 abd_get_from_buf(buf
->b_data
, HDR_GET_LSIZE(hdr
)),
7136 HDR_GET_LSIZE(hdr
), arc_buf_size(buf
), &localprop
, arc_write_ready
,
7137 (children_ready
!= NULL
) ? arc_write_children_ready
: NULL
,
7138 arc_write_physdone
, arc_write_done
, callback
,
7139 priority
, zio_flags
, zb
);
7145 arc_tempreserve_clear(uint64_t reserve
)
7147 atomic_add_64(&arc_tempreserve
, -reserve
);
7148 ASSERT((int64_t)arc_tempreserve
>= 0);
7152 arc_tempreserve_space(spa_t
*spa
, uint64_t reserve
, uint64_t txg
)
7158 reserve
> arc_c
/4 &&
7159 reserve
* 4 > (2ULL << SPA_MAXBLOCKSHIFT
))
7160 arc_c
= MIN(arc_c_max
, reserve
* 4);
7163 * Throttle when the calculated memory footprint for the TXG
7164 * exceeds the target ARC size.
7166 if (reserve
> arc_c
) {
7167 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
7168 return (SET_ERROR(ERESTART
));
7172 * Don't count loaned bufs as in flight dirty data to prevent long
7173 * network delays from blocking transactions that are ready to be
7174 * assigned to a txg.
7177 /* assert that it has not wrapped around */
7178 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
7180 anon_size
= MAX((int64_t)(zfs_refcount_count(&arc_anon
->arcs_size
) -
7181 arc_loaned_bytes
), 0);
7184 * Writes will, almost always, require additional memory allocations
7185 * in order to compress/encrypt/etc the data. We therefore need to
7186 * make sure that there is sufficient available memory for this.
7188 error
= arc_memory_throttle(spa
, reserve
, txg
);
7193 * Throttle writes when the amount of dirty data in the cache
7194 * gets too large. We try to keep the cache less than half full
7195 * of dirty blocks so that our sync times don't grow too large.
7197 * In the case of one pool being built on another pool, we want
7198 * to make sure we don't end up throttling the lower (backing)
7199 * pool when the upper pool is the majority contributor to dirty
7200 * data. To insure we make forward progress during throttling, we
7201 * also check the current pool's net dirty data and only throttle
7202 * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty
7203 * data in the cache.
7205 * Note: if two requests come in concurrently, we might let them
7206 * both succeed, when one of them should fail. Not a huge deal.
7208 uint64_t total_dirty
= reserve
+ arc_tempreserve
+ anon_size
;
7209 uint64_t spa_dirty_anon
= spa_dirty_data(spa
);
7210 uint64_t rarc_c
= arc_warm
? arc_c
: arc_c_max
;
7211 if (total_dirty
> rarc_c
* zfs_arc_dirty_limit_percent
/ 100 &&
7212 anon_size
> rarc_c
* zfs_arc_anon_limit_percent
/ 100 &&
7213 spa_dirty_anon
> anon_size
* zfs_arc_pool_dirty_percent
/ 100) {
7215 uint64_t meta_esize
= zfs_refcount_count(
7216 &arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7217 uint64_t data_esize
=
7218 zfs_refcount_count(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7219 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
7220 "anon_data=%lluK tempreserve=%lluK rarc_c=%lluK\n",
7221 (u_longlong_t
)arc_tempreserve
>> 10,
7222 (u_longlong_t
)meta_esize
>> 10,
7223 (u_longlong_t
)data_esize
>> 10,
7224 (u_longlong_t
)reserve
>> 10,
7225 (u_longlong_t
)rarc_c
>> 10);
7227 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
7228 return (SET_ERROR(ERESTART
));
7230 atomic_add_64(&arc_tempreserve
, reserve
);
7235 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
7236 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
7238 size
->value
.ui64
= zfs_refcount_count(&state
->arcs_size
);
7239 evict_data
->value
.ui64
=
7240 zfs_refcount_count(&state
->arcs_esize
[ARC_BUFC_DATA
]);
7241 evict_metadata
->value
.ui64
=
7242 zfs_refcount_count(&state
->arcs_esize
[ARC_BUFC_METADATA
]);
7246 arc_kstat_update(kstat_t
*ksp
, int rw
)
7248 arc_stats_t
*as
= ksp
->ks_data
;
7250 if (rw
== KSTAT_WRITE
)
7251 return (SET_ERROR(EACCES
));
7253 as
->arcstat_hits
.value
.ui64
=
7254 wmsum_value(&arc_sums
.arcstat_hits
);
7255 as
->arcstat_misses
.value
.ui64
=
7256 wmsum_value(&arc_sums
.arcstat_misses
);
7257 as
->arcstat_demand_data_hits
.value
.ui64
=
7258 wmsum_value(&arc_sums
.arcstat_demand_data_hits
);
7259 as
->arcstat_demand_data_misses
.value
.ui64
=
7260 wmsum_value(&arc_sums
.arcstat_demand_data_misses
);
7261 as
->arcstat_demand_metadata_hits
.value
.ui64
=
7262 wmsum_value(&arc_sums
.arcstat_demand_metadata_hits
);
7263 as
->arcstat_demand_metadata_misses
.value
.ui64
=
7264 wmsum_value(&arc_sums
.arcstat_demand_metadata_misses
);
7265 as
->arcstat_prefetch_data_hits
.value
.ui64
=
7266 wmsum_value(&arc_sums
.arcstat_prefetch_data_hits
);
7267 as
->arcstat_prefetch_data_misses
.value
.ui64
=
7268 wmsum_value(&arc_sums
.arcstat_prefetch_data_misses
);
7269 as
->arcstat_prefetch_metadata_hits
.value
.ui64
=
7270 wmsum_value(&arc_sums
.arcstat_prefetch_metadata_hits
);
7271 as
->arcstat_prefetch_metadata_misses
.value
.ui64
=
7272 wmsum_value(&arc_sums
.arcstat_prefetch_metadata_misses
);
7273 as
->arcstat_mru_hits
.value
.ui64
=
7274 wmsum_value(&arc_sums
.arcstat_mru_hits
);
7275 as
->arcstat_mru_ghost_hits
.value
.ui64
=
7276 wmsum_value(&arc_sums
.arcstat_mru_ghost_hits
);
7277 as
->arcstat_mfu_hits
.value
.ui64
=
7278 wmsum_value(&arc_sums
.arcstat_mfu_hits
);
7279 as
->arcstat_mfu_ghost_hits
.value
.ui64
=
7280 wmsum_value(&arc_sums
.arcstat_mfu_ghost_hits
);
7281 as
->arcstat_deleted
.value
.ui64
=
7282 wmsum_value(&arc_sums
.arcstat_deleted
);
7283 as
->arcstat_mutex_miss
.value
.ui64
=
7284 wmsum_value(&arc_sums
.arcstat_mutex_miss
);
7285 as
->arcstat_access_skip
.value
.ui64
=
7286 wmsum_value(&arc_sums
.arcstat_access_skip
);
7287 as
->arcstat_evict_skip
.value
.ui64
=
7288 wmsum_value(&arc_sums
.arcstat_evict_skip
);
7289 as
->arcstat_evict_not_enough
.value
.ui64
=
7290 wmsum_value(&arc_sums
.arcstat_evict_not_enough
);
7291 as
->arcstat_evict_l2_cached
.value
.ui64
=
7292 wmsum_value(&arc_sums
.arcstat_evict_l2_cached
);
7293 as
->arcstat_evict_l2_eligible
.value
.ui64
=
7294 wmsum_value(&arc_sums
.arcstat_evict_l2_eligible
);
7295 as
->arcstat_evict_l2_eligible_mfu
.value
.ui64
=
7296 wmsum_value(&arc_sums
.arcstat_evict_l2_eligible_mfu
);
7297 as
->arcstat_evict_l2_eligible_mru
.value
.ui64
=
7298 wmsum_value(&arc_sums
.arcstat_evict_l2_eligible_mru
);
7299 as
->arcstat_evict_l2_ineligible
.value
.ui64
=
7300 wmsum_value(&arc_sums
.arcstat_evict_l2_ineligible
);
7301 as
->arcstat_evict_l2_skip
.value
.ui64
=
7302 wmsum_value(&arc_sums
.arcstat_evict_l2_skip
);
7303 as
->arcstat_hash_collisions
.value
.ui64
=
7304 wmsum_value(&arc_sums
.arcstat_hash_collisions
);
7305 as
->arcstat_hash_chains
.value
.ui64
=
7306 wmsum_value(&arc_sums
.arcstat_hash_chains
);
7307 as
->arcstat_size
.value
.ui64
=
7308 aggsum_value(&arc_sums
.arcstat_size
);
7309 as
->arcstat_compressed_size
.value
.ui64
=
7310 wmsum_value(&arc_sums
.arcstat_compressed_size
);
7311 as
->arcstat_uncompressed_size
.value
.ui64
=
7312 wmsum_value(&arc_sums
.arcstat_uncompressed_size
);
7313 as
->arcstat_overhead_size
.value
.ui64
=
7314 wmsum_value(&arc_sums
.arcstat_overhead_size
);
7315 as
->arcstat_hdr_size
.value
.ui64
=
7316 wmsum_value(&arc_sums
.arcstat_hdr_size
);
7317 as
->arcstat_data_size
.value
.ui64
=
7318 wmsum_value(&arc_sums
.arcstat_data_size
);
7319 as
->arcstat_metadata_size
.value
.ui64
=
7320 wmsum_value(&arc_sums
.arcstat_metadata_size
);
7321 as
->arcstat_dbuf_size
.value
.ui64
=
7322 wmsum_value(&arc_sums
.arcstat_dbuf_size
);
7323 #if defined(COMPAT_FREEBSD11)
7324 as
->arcstat_other_size
.value
.ui64
=
7325 wmsum_value(&arc_sums
.arcstat_bonus_size
) +
7326 aggsum_value(&arc_sums
.arcstat_dnode_size
) +
7327 wmsum_value(&arc_sums
.arcstat_dbuf_size
);
7330 arc_kstat_update_state(arc_anon
,
7331 &as
->arcstat_anon_size
,
7332 &as
->arcstat_anon_evictable_data
,
7333 &as
->arcstat_anon_evictable_metadata
);
7334 arc_kstat_update_state(arc_mru
,
7335 &as
->arcstat_mru_size
,
7336 &as
->arcstat_mru_evictable_data
,
7337 &as
->arcstat_mru_evictable_metadata
);
7338 arc_kstat_update_state(arc_mru_ghost
,
7339 &as
->arcstat_mru_ghost_size
,
7340 &as
->arcstat_mru_ghost_evictable_data
,
7341 &as
->arcstat_mru_ghost_evictable_metadata
);
7342 arc_kstat_update_state(arc_mfu
,
7343 &as
->arcstat_mfu_size
,
7344 &as
->arcstat_mfu_evictable_data
,
7345 &as
->arcstat_mfu_evictable_metadata
);
7346 arc_kstat_update_state(arc_mfu_ghost
,
7347 &as
->arcstat_mfu_ghost_size
,
7348 &as
->arcstat_mfu_ghost_evictable_data
,
7349 &as
->arcstat_mfu_ghost_evictable_metadata
);
7351 as
->arcstat_dnode_size
.value
.ui64
=
7352 aggsum_value(&arc_sums
.arcstat_dnode_size
);
7353 as
->arcstat_bonus_size
.value
.ui64
=
7354 wmsum_value(&arc_sums
.arcstat_bonus_size
);
7355 as
->arcstat_l2_hits
.value
.ui64
=
7356 wmsum_value(&arc_sums
.arcstat_l2_hits
);
7357 as
->arcstat_l2_misses
.value
.ui64
=
7358 wmsum_value(&arc_sums
.arcstat_l2_misses
);
7359 as
->arcstat_l2_prefetch_asize
.value
.ui64
=
7360 wmsum_value(&arc_sums
.arcstat_l2_prefetch_asize
);
7361 as
->arcstat_l2_mru_asize
.value
.ui64
=
7362 wmsum_value(&arc_sums
.arcstat_l2_mru_asize
);
7363 as
->arcstat_l2_mfu_asize
.value
.ui64
=
7364 wmsum_value(&arc_sums
.arcstat_l2_mfu_asize
);
7365 as
->arcstat_l2_bufc_data_asize
.value
.ui64
=
7366 wmsum_value(&arc_sums
.arcstat_l2_bufc_data_asize
);
7367 as
->arcstat_l2_bufc_metadata_asize
.value
.ui64
=
7368 wmsum_value(&arc_sums
.arcstat_l2_bufc_metadata_asize
);
7369 as
->arcstat_l2_feeds
.value
.ui64
=
7370 wmsum_value(&arc_sums
.arcstat_l2_feeds
);
7371 as
->arcstat_l2_rw_clash
.value
.ui64
=
7372 wmsum_value(&arc_sums
.arcstat_l2_rw_clash
);
7373 as
->arcstat_l2_read_bytes
.value
.ui64
=
7374 wmsum_value(&arc_sums
.arcstat_l2_read_bytes
);
7375 as
->arcstat_l2_write_bytes
.value
.ui64
=
7376 wmsum_value(&arc_sums
.arcstat_l2_write_bytes
);
7377 as
->arcstat_l2_writes_sent
.value
.ui64
=
7378 wmsum_value(&arc_sums
.arcstat_l2_writes_sent
);
7379 as
->arcstat_l2_writes_done
.value
.ui64
=
7380 wmsum_value(&arc_sums
.arcstat_l2_writes_done
);
7381 as
->arcstat_l2_writes_error
.value
.ui64
=
7382 wmsum_value(&arc_sums
.arcstat_l2_writes_error
);
7383 as
->arcstat_l2_writes_lock_retry
.value
.ui64
=
7384 wmsum_value(&arc_sums
.arcstat_l2_writes_lock_retry
);
7385 as
->arcstat_l2_evict_lock_retry
.value
.ui64
=
7386 wmsum_value(&arc_sums
.arcstat_l2_evict_lock_retry
);
7387 as
->arcstat_l2_evict_reading
.value
.ui64
=
7388 wmsum_value(&arc_sums
.arcstat_l2_evict_reading
);
7389 as
->arcstat_l2_evict_l1cached
.value
.ui64
=
7390 wmsum_value(&arc_sums
.arcstat_l2_evict_l1cached
);
7391 as
->arcstat_l2_free_on_write
.value
.ui64
=
7392 wmsum_value(&arc_sums
.arcstat_l2_free_on_write
);
7393 as
->arcstat_l2_abort_lowmem
.value
.ui64
=
7394 wmsum_value(&arc_sums
.arcstat_l2_abort_lowmem
);
7395 as
->arcstat_l2_cksum_bad
.value
.ui64
=
7396 wmsum_value(&arc_sums
.arcstat_l2_cksum_bad
);
7397 as
->arcstat_l2_io_error
.value
.ui64
=
7398 wmsum_value(&arc_sums
.arcstat_l2_io_error
);
7399 as
->arcstat_l2_lsize
.value
.ui64
=
7400 wmsum_value(&arc_sums
.arcstat_l2_lsize
);
7401 as
->arcstat_l2_psize
.value
.ui64
=
7402 wmsum_value(&arc_sums
.arcstat_l2_psize
);
7403 as
->arcstat_l2_hdr_size
.value
.ui64
=
7404 aggsum_value(&arc_sums
.arcstat_l2_hdr_size
);
7405 as
->arcstat_l2_log_blk_writes
.value
.ui64
=
7406 wmsum_value(&arc_sums
.arcstat_l2_log_blk_writes
);
7407 as
->arcstat_l2_log_blk_asize
.value
.ui64
=
7408 wmsum_value(&arc_sums
.arcstat_l2_log_blk_asize
);
7409 as
->arcstat_l2_log_blk_count
.value
.ui64
=
7410 wmsum_value(&arc_sums
.arcstat_l2_log_blk_count
);
7411 as
->arcstat_l2_rebuild_success
.value
.ui64
=
7412 wmsum_value(&arc_sums
.arcstat_l2_rebuild_success
);
7413 as
->arcstat_l2_rebuild_abort_unsupported
.value
.ui64
=
7414 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_unsupported
);
7415 as
->arcstat_l2_rebuild_abort_io_errors
.value
.ui64
=
7416 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_io_errors
);
7417 as
->arcstat_l2_rebuild_abort_dh_errors
.value
.ui64
=
7418 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_dh_errors
);
7419 as
->arcstat_l2_rebuild_abort_cksum_lb_errors
.value
.ui64
=
7420 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_cksum_lb_errors
);
7421 as
->arcstat_l2_rebuild_abort_lowmem
.value
.ui64
=
7422 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_lowmem
);
7423 as
->arcstat_l2_rebuild_size
.value
.ui64
=
7424 wmsum_value(&arc_sums
.arcstat_l2_rebuild_size
);
7425 as
->arcstat_l2_rebuild_asize
.value
.ui64
=
7426 wmsum_value(&arc_sums
.arcstat_l2_rebuild_asize
);
7427 as
->arcstat_l2_rebuild_bufs
.value
.ui64
=
7428 wmsum_value(&arc_sums
.arcstat_l2_rebuild_bufs
);
7429 as
->arcstat_l2_rebuild_bufs_precached
.value
.ui64
=
7430 wmsum_value(&arc_sums
.arcstat_l2_rebuild_bufs_precached
);
7431 as
->arcstat_l2_rebuild_log_blks
.value
.ui64
=
7432 wmsum_value(&arc_sums
.arcstat_l2_rebuild_log_blks
);
7433 as
->arcstat_memory_throttle_count
.value
.ui64
=
7434 wmsum_value(&arc_sums
.arcstat_memory_throttle_count
);
7435 as
->arcstat_memory_direct_count
.value
.ui64
=
7436 wmsum_value(&arc_sums
.arcstat_memory_direct_count
);
7437 as
->arcstat_memory_indirect_count
.value
.ui64
=
7438 wmsum_value(&arc_sums
.arcstat_memory_indirect_count
);
7440 as
->arcstat_memory_all_bytes
.value
.ui64
=
7442 as
->arcstat_memory_free_bytes
.value
.ui64
=
7444 as
->arcstat_memory_available_bytes
.value
.i64
=
7445 arc_available_memory();
7447 as
->arcstat_prune
.value
.ui64
=
7448 wmsum_value(&arc_sums
.arcstat_prune
);
7449 as
->arcstat_meta_used
.value
.ui64
=
7450 aggsum_value(&arc_sums
.arcstat_meta_used
);
7451 as
->arcstat_async_upgrade_sync
.value
.ui64
=
7452 wmsum_value(&arc_sums
.arcstat_async_upgrade_sync
);
7453 as
->arcstat_demand_hit_predictive_prefetch
.value
.ui64
=
7454 wmsum_value(&arc_sums
.arcstat_demand_hit_predictive_prefetch
);
7455 as
->arcstat_demand_hit_prescient_prefetch
.value
.ui64
=
7456 wmsum_value(&arc_sums
.arcstat_demand_hit_prescient_prefetch
);
7457 as
->arcstat_raw_size
.value
.ui64
=
7458 wmsum_value(&arc_sums
.arcstat_raw_size
);
7459 as
->arcstat_cached_only_in_progress
.value
.ui64
=
7460 wmsum_value(&arc_sums
.arcstat_cached_only_in_progress
);
7461 as
->arcstat_abd_chunk_waste_size
.value
.ui64
=
7462 wmsum_value(&arc_sums
.arcstat_abd_chunk_waste_size
);
7468 * This function *must* return indices evenly distributed between all
7469 * sublists of the multilist. This is needed due to how the ARC eviction
7470 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
7471 * distributed between all sublists and uses this assumption when
7472 * deciding which sublist to evict from and how much to evict from it.
7475 arc_state_multilist_index_func(multilist_t
*ml
, void *obj
)
7477 arc_buf_hdr_t
*hdr
= obj
;
7480 * We rely on b_dva to generate evenly distributed index
7481 * numbers using buf_hash below. So, as an added precaution,
7482 * let's make sure we never add empty buffers to the arc lists.
7484 ASSERT(!HDR_EMPTY(hdr
));
7487 * The assumption here, is the hash value for a given
7488 * arc_buf_hdr_t will remain constant throughout its lifetime
7489 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
7490 * Thus, we don't need to store the header's sublist index
7491 * on insertion, as this index can be recalculated on removal.
7493 * Also, the low order bits of the hash value are thought to be
7494 * distributed evenly. Otherwise, in the case that the multilist
7495 * has a power of two number of sublists, each sublists' usage
7496 * would not be evenly distributed. In this context full 64bit
7497 * division would be a waste of time, so limit it to 32 bits.
7499 return ((unsigned int)buf_hash(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
) %
7500 multilist_get_num_sublists(ml
));
7504 arc_state_l2c_multilist_index_func(multilist_t
*ml
, void *obj
)
7506 panic("Header %p insert into arc_l2c_only %p", obj
, ml
);
7509 #define WARN_IF_TUNING_IGNORED(tuning, value, do_warn) do { \
7510 if ((do_warn) && (tuning) && ((tuning) != (value))) { \
7512 "ignoring tunable %s (using %llu instead)", \
7513 (#tuning), (u_longlong_t)(value)); \
7518 * Called during module initialization and periodically thereafter to
7519 * apply reasonable changes to the exposed performance tunings. Can also be
7520 * called explicitly by param_set_arc_*() functions when ARC tunables are
7521 * updated manually. Non-zero zfs_* values which differ from the currently set
7522 * values will be applied.
7525 arc_tuning_update(boolean_t verbose
)
7527 uint64_t allmem
= arc_all_memory();
7528 unsigned long limit
;
7530 /* Valid range: 32M - <arc_c_max> */
7531 if ((zfs_arc_min
) && (zfs_arc_min
!= arc_c_min
) &&
7532 (zfs_arc_min
>= 2ULL << SPA_MAXBLOCKSHIFT
) &&
7533 (zfs_arc_min
<= arc_c_max
)) {
7534 arc_c_min
= zfs_arc_min
;
7535 arc_c
= MAX(arc_c
, arc_c_min
);
7537 WARN_IF_TUNING_IGNORED(zfs_arc_min
, arc_c_min
, verbose
);
7539 /* Valid range: 64M - <all physical memory> */
7540 if ((zfs_arc_max
) && (zfs_arc_max
!= arc_c_max
) &&
7541 (zfs_arc_max
>= MIN_ARC_MAX
) && (zfs_arc_max
< allmem
) &&
7542 (zfs_arc_max
> arc_c_min
)) {
7543 arc_c_max
= zfs_arc_max
;
7544 arc_c
= MIN(arc_c
, arc_c_max
);
7545 arc_p
= (arc_c
>> 1);
7546 if (arc_meta_limit
> arc_c_max
)
7547 arc_meta_limit
= arc_c_max
;
7548 if (arc_dnode_size_limit
> arc_meta_limit
)
7549 arc_dnode_size_limit
= arc_meta_limit
;
7551 WARN_IF_TUNING_IGNORED(zfs_arc_max
, arc_c_max
, verbose
);
7553 /* Valid range: 16M - <arc_c_max> */
7554 if ((zfs_arc_meta_min
) && (zfs_arc_meta_min
!= arc_meta_min
) &&
7555 (zfs_arc_meta_min
>= 1ULL << SPA_MAXBLOCKSHIFT
) &&
7556 (zfs_arc_meta_min
<= arc_c_max
)) {
7557 arc_meta_min
= zfs_arc_meta_min
;
7558 if (arc_meta_limit
< arc_meta_min
)
7559 arc_meta_limit
= arc_meta_min
;
7560 if (arc_dnode_size_limit
< arc_meta_min
)
7561 arc_dnode_size_limit
= arc_meta_min
;
7563 WARN_IF_TUNING_IGNORED(zfs_arc_meta_min
, arc_meta_min
, verbose
);
7565 /* Valid range: <arc_meta_min> - <arc_c_max> */
7566 limit
= zfs_arc_meta_limit
? zfs_arc_meta_limit
:
7567 MIN(zfs_arc_meta_limit_percent
, 100) * arc_c_max
/ 100;
7568 if ((limit
!= arc_meta_limit
) &&
7569 (limit
>= arc_meta_min
) &&
7570 (limit
<= arc_c_max
))
7571 arc_meta_limit
= limit
;
7572 WARN_IF_TUNING_IGNORED(zfs_arc_meta_limit
, arc_meta_limit
, verbose
);
7574 /* Valid range: <arc_meta_min> - <arc_meta_limit> */
7575 limit
= zfs_arc_dnode_limit
? zfs_arc_dnode_limit
:
7576 MIN(zfs_arc_dnode_limit_percent
, 100) * arc_meta_limit
/ 100;
7577 if ((limit
!= arc_dnode_size_limit
) &&
7578 (limit
>= arc_meta_min
) &&
7579 (limit
<= arc_meta_limit
))
7580 arc_dnode_size_limit
= limit
;
7581 WARN_IF_TUNING_IGNORED(zfs_arc_dnode_limit
, arc_dnode_size_limit
,
7584 /* Valid range: 1 - N */
7585 if (zfs_arc_grow_retry
)
7586 arc_grow_retry
= zfs_arc_grow_retry
;
7588 /* Valid range: 1 - N */
7589 if (zfs_arc_shrink_shift
) {
7590 arc_shrink_shift
= zfs_arc_shrink_shift
;
7591 arc_no_grow_shift
= MIN(arc_no_grow_shift
, arc_shrink_shift
-1);
7594 /* Valid range: 1 - N */
7595 if (zfs_arc_p_min_shift
)
7596 arc_p_min_shift
= zfs_arc_p_min_shift
;
7598 /* Valid range: 1 - N ms */
7599 if (zfs_arc_min_prefetch_ms
)
7600 arc_min_prefetch_ms
= zfs_arc_min_prefetch_ms
;
7602 /* Valid range: 1 - N ms */
7603 if (zfs_arc_min_prescient_prefetch_ms
) {
7604 arc_min_prescient_prefetch_ms
=
7605 zfs_arc_min_prescient_prefetch_ms
;
7608 /* Valid range: 0 - 100 */
7609 if ((zfs_arc_lotsfree_percent
>= 0) &&
7610 (zfs_arc_lotsfree_percent
<= 100))
7611 arc_lotsfree_percent
= zfs_arc_lotsfree_percent
;
7612 WARN_IF_TUNING_IGNORED(zfs_arc_lotsfree_percent
, arc_lotsfree_percent
,
7615 /* Valid range: 0 - <all physical memory> */
7616 if ((zfs_arc_sys_free
) && (zfs_arc_sys_free
!= arc_sys_free
))
7617 arc_sys_free
= MIN(MAX(zfs_arc_sys_free
, 0), allmem
);
7618 WARN_IF_TUNING_IGNORED(zfs_arc_sys_free
, arc_sys_free
, verbose
);
7622 arc_state_init(void)
7624 multilist_create(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
7625 sizeof (arc_buf_hdr_t
),
7626 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7627 arc_state_multilist_index_func
);
7628 multilist_create(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
7629 sizeof (arc_buf_hdr_t
),
7630 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7631 arc_state_multilist_index_func
);
7632 multilist_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
7633 sizeof (arc_buf_hdr_t
),
7634 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7635 arc_state_multilist_index_func
);
7636 multilist_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
7637 sizeof (arc_buf_hdr_t
),
7638 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7639 arc_state_multilist_index_func
);
7640 multilist_create(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
7641 sizeof (arc_buf_hdr_t
),
7642 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7643 arc_state_multilist_index_func
);
7644 multilist_create(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
7645 sizeof (arc_buf_hdr_t
),
7646 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7647 arc_state_multilist_index_func
);
7648 multilist_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
7649 sizeof (arc_buf_hdr_t
),
7650 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7651 arc_state_multilist_index_func
);
7652 multilist_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
7653 sizeof (arc_buf_hdr_t
),
7654 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7655 arc_state_multilist_index_func
);
7657 * L2 headers should never be on the L2 state list since they don't
7658 * have L1 headers allocated. Special index function asserts that.
7660 multilist_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
7661 sizeof (arc_buf_hdr_t
),
7662 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7663 arc_state_l2c_multilist_index_func
);
7664 multilist_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
7665 sizeof (arc_buf_hdr_t
),
7666 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
7667 arc_state_l2c_multilist_index_func
);
7669 zfs_refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7670 zfs_refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7671 zfs_refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7672 zfs_refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7673 zfs_refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7674 zfs_refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7675 zfs_refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7676 zfs_refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7677 zfs_refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7678 zfs_refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7679 zfs_refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7680 zfs_refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7682 zfs_refcount_create(&arc_anon
->arcs_size
);
7683 zfs_refcount_create(&arc_mru
->arcs_size
);
7684 zfs_refcount_create(&arc_mru_ghost
->arcs_size
);
7685 zfs_refcount_create(&arc_mfu
->arcs_size
);
7686 zfs_refcount_create(&arc_mfu_ghost
->arcs_size
);
7687 zfs_refcount_create(&arc_l2c_only
->arcs_size
);
7689 wmsum_init(&arc_sums
.arcstat_hits
, 0);
7690 wmsum_init(&arc_sums
.arcstat_misses
, 0);
7691 wmsum_init(&arc_sums
.arcstat_demand_data_hits
, 0);
7692 wmsum_init(&arc_sums
.arcstat_demand_data_misses
, 0);
7693 wmsum_init(&arc_sums
.arcstat_demand_metadata_hits
, 0);
7694 wmsum_init(&arc_sums
.arcstat_demand_metadata_misses
, 0);
7695 wmsum_init(&arc_sums
.arcstat_prefetch_data_hits
, 0);
7696 wmsum_init(&arc_sums
.arcstat_prefetch_data_misses
, 0);
7697 wmsum_init(&arc_sums
.arcstat_prefetch_metadata_hits
, 0);
7698 wmsum_init(&arc_sums
.arcstat_prefetch_metadata_misses
, 0);
7699 wmsum_init(&arc_sums
.arcstat_mru_hits
, 0);
7700 wmsum_init(&arc_sums
.arcstat_mru_ghost_hits
, 0);
7701 wmsum_init(&arc_sums
.arcstat_mfu_hits
, 0);
7702 wmsum_init(&arc_sums
.arcstat_mfu_ghost_hits
, 0);
7703 wmsum_init(&arc_sums
.arcstat_deleted
, 0);
7704 wmsum_init(&arc_sums
.arcstat_mutex_miss
, 0);
7705 wmsum_init(&arc_sums
.arcstat_access_skip
, 0);
7706 wmsum_init(&arc_sums
.arcstat_evict_skip
, 0);
7707 wmsum_init(&arc_sums
.arcstat_evict_not_enough
, 0);
7708 wmsum_init(&arc_sums
.arcstat_evict_l2_cached
, 0);
7709 wmsum_init(&arc_sums
.arcstat_evict_l2_eligible
, 0);
7710 wmsum_init(&arc_sums
.arcstat_evict_l2_eligible_mfu
, 0);
7711 wmsum_init(&arc_sums
.arcstat_evict_l2_eligible_mru
, 0);
7712 wmsum_init(&arc_sums
.arcstat_evict_l2_ineligible
, 0);
7713 wmsum_init(&arc_sums
.arcstat_evict_l2_skip
, 0);
7714 wmsum_init(&arc_sums
.arcstat_hash_collisions
, 0);
7715 wmsum_init(&arc_sums
.arcstat_hash_chains
, 0);
7716 aggsum_init(&arc_sums
.arcstat_size
, 0);
7717 wmsum_init(&arc_sums
.arcstat_compressed_size
, 0);
7718 wmsum_init(&arc_sums
.arcstat_uncompressed_size
, 0);
7719 wmsum_init(&arc_sums
.arcstat_overhead_size
, 0);
7720 wmsum_init(&arc_sums
.arcstat_hdr_size
, 0);
7721 wmsum_init(&arc_sums
.arcstat_data_size
, 0);
7722 wmsum_init(&arc_sums
.arcstat_metadata_size
, 0);
7723 wmsum_init(&arc_sums
.arcstat_dbuf_size
, 0);
7724 aggsum_init(&arc_sums
.arcstat_dnode_size
, 0);
7725 wmsum_init(&arc_sums
.arcstat_bonus_size
, 0);
7726 wmsum_init(&arc_sums
.arcstat_l2_hits
, 0);
7727 wmsum_init(&arc_sums
.arcstat_l2_misses
, 0);
7728 wmsum_init(&arc_sums
.arcstat_l2_prefetch_asize
, 0);
7729 wmsum_init(&arc_sums
.arcstat_l2_mru_asize
, 0);
7730 wmsum_init(&arc_sums
.arcstat_l2_mfu_asize
, 0);
7731 wmsum_init(&arc_sums
.arcstat_l2_bufc_data_asize
, 0);
7732 wmsum_init(&arc_sums
.arcstat_l2_bufc_metadata_asize
, 0);
7733 wmsum_init(&arc_sums
.arcstat_l2_feeds
, 0);
7734 wmsum_init(&arc_sums
.arcstat_l2_rw_clash
, 0);
7735 wmsum_init(&arc_sums
.arcstat_l2_read_bytes
, 0);
7736 wmsum_init(&arc_sums
.arcstat_l2_write_bytes
, 0);
7737 wmsum_init(&arc_sums
.arcstat_l2_writes_sent
, 0);
7738 wmsum_init(&arc_sums
.arcstat_l2_writes_done
, 0);
7739 wmsum_init(&arc_sums
.arcstat_l2_writes_error
, 0);
7740 wmsum_init(&arc_sums
.arcstat_l2_writes_lock_retry
, 0);
7741 wmsum_init(&arc_sums
.arcstat_l2_evict_lock_retry
, 0);
7742 wmsum_init(&arc_sums
.arcstat_l2_evict_reading
, 0);
7743 wmsum_init(&arc_sums
.arcstat_l2_evict_l1cached
, 0);
7744 wmsum_init(&arc_sums
.arcstat_l2_free_on_write
, 0);
7745 wmsum_init(&arc_sums
.arcstat_l2_abort_lowmem
, 0);
7746 wmsum_init(&arc_sums
.arcstat_l2_cksum_bad
, 0);
7747 wmsum_init(&arc_sums
.arcstat_l2_io_error
, 0);
7748 wmsum_init(&arc_sums
.arcstat_l2_lsize
, 0);
7749 wmsum_init(&arc_sums
.arcstat_l2_psize
, 0);
7750 aggsum_init(&arc_sums
.arcstat_l2_hdr_size
, 0);
7751 wmsum_init(&arc_sums
.arcstat_l2_log_blk_writes
, 0);
7752 wmsum_init(&arc_sums
.arcstat_l2_log_blk_asize
, 0);
7753 wmsum_init(&arc_sums
.arcstat_l2_log_blk_count
, 0);
7754 wmsum_init(&arc_sums
.arcstat_l2_rebuild_success
, 0);
7755 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_unsupported
, 0);
7756 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_io_errors
, 0);
7757 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_dh_errors
, 0);
7758 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_cksum_lb_errors
, 0);
7759 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_lowmem
, 0);
7760 wmsum_init(&arc_sums
.arcstat_l2_rebuild_size
, 0);
7761 wmsum_init(&arc_sums
.arcstat_l2_rebuild_asize
, 0);
7762 wmsum_init(&arc_sums
.arcstat_l2_rebuild_bufs
, 0);
7763 wmsum_init(&arc_sums
.arcstat_l2_rebuild_bufs_precached
, 0);
7764 wmsum_init(&arc_sums
.arcstat_l2_rebuild_log_blks
, 0);
7765 wmsum_init(&arc_sums
.arcstat_memory_throttle_count
, 0);
7766 wmsum_init(&arc_sums
.arcstat_memory_direct_count
, 0);
7767 wmsum_init(&arc_sums
.arcstat_memory_indirect_count
, 0);
7768 wmsum_init(&arc_sums
.arcstat_prune
, 0);
7769 aggsum_init(&arc_sums
.arcstat_meta_used
, 0);
7770 wmsum_init(&arc_sums
.arcstat_async_upgrade_sync
, 0);
7771 wmsum_init(&arc_sums
.arcstat_demand_hit_predictive_prefetch
, 0);
7772 wmsum_init(&arc_sums
.arcstat_demand_hit_prescient_prefetch
, 0);
7773 wmsum_init(&arc_sums
.arcstat_raw_size
, 0);
7774 wmsum_init(&arc_sums
.arcstat_cached_only_in_progress
, 0);
7775 wmsum_init(&arc_sums
.arcstat_abd_chunk_waste_size
, 0);
7777 arc_anon
->arcs_state
= ARC_STATE_ANON
;
7778 arc_mru
->arcs_state
= ARC_STATE_MRU
;
7779 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
7780 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
7781 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
7782 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
7786 arc_state_fini(void)
7788 zfs_refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7789 zfs_refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7790 zfs_refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7791 zfs_refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7792 zfs_refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7793 zfs_refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7794 zfs_refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7795 zfs_refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7796 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7797 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7798 zfs_refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7799 zfs_refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7801 zfs_refcount_destroy(&arc_anon
->arcs_size
);
7802 zfs_refcount_destroy(&arc_mru
->arcs_size
);
7803 zfs_refcount_destroy(&arc_mru_ghost
->arcs_size
);
7804 zfs_refcount_destroy(&arc_mfu
->arcs_size
);
7805 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_size
);
7806 zfs_refcount_destroy(&arc_l2c_only
->arcs_size
);
7808 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
7809 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7810 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
7811 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7812 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
7813 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7814 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
7815 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7816 multilist_destroy(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]);
7817 multilist_destroy(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]);
7819 wmsum_fini(&arc_sums
.arcstat_hits
);
7820 wmsum_fini(&arc_sums
.arcstat_misses
);
7821 wmsum_fini(&arc_sums
.arcstat_demand_data_hits
);
7822 wmsum_fini(&arc_sums
.arcstat_demand_data_misses
);
7823 wmsum_fini(&arc_sums
.arcstat_demand_metadata_hits
);
7824 wmsum_fini(&arc_sums
.arcstat_demand_metadata_misses
);
7825 wmsum_fini(&arc_sums
.arcstat_prefetch_data_hits
);
7826 wmsum_fini(&arc_sums
.arcstat_prefetch_data_misses
);
7827 wmsum_fini(&arc_sums
.arcstat_prefetch_metadata_hits
);
7828 wmsum_fini(&arc_sums
.arcstat_prefetch_metadata_misses
);
7829 wmsum_fini(&arc_sums
.arcstat_mru_hits
);
7830 wmsum_fini(&arc_sums
.arcstat_mru_ghost_hits
);
7831 wmsum_fini(&arc_sums
.arcstat_mfu_hits
);
7832 wmsum_fini(&arc_sums
.arcstat_mfu_ghost_hits
);
7833 wmsum_fini(&arc_sums
.arcstat_deleted
);
7834 wmsum_fini(&arc_sums
.arcstat_mutex_miss
);
7835 wmsum_fini(&arc_sums
.arcstat_access_skip
);
7836 wmsum_fini(&arc_sums
.arcstat_evict_skip
);
7837 wmsum_fini(&arc_sums
.arcstat_evict_not_enough
);
7838 wmsum_fini(&arc_sums
.arcstat_evict_l2_cached
);
7839 wmsum_fini(&arc_sums
.arcstat_evict_l2_eligible
);
7840 wmsum_fini(&arc_sums
.arcstat_evict_l2_eligible_mfu
);
7841 wmsum_fini(&arc_sums
.arcstat_evict_l2_eligible_mru
);
7842 wmsum_fini(&arc_sums
.arcstat_evict_l2_ineligible
);
7843 wmsum_fini(&arc_sums
.arcstat_evict_l2_skip
);
7844 wmsum_fini(&arc_sums
.arcstat_hash_collisions
);
7845 wmsum_fini(&arc_sums
.arcstat_hash_chains
);
7846 aggsum_fini(&arc_sums
.arcstat_size
);
7847 wmsum_fini(&arc_sums
.arcstat_compressed_size
);
7848 wmsum_fini(&arc_sums
.arcstat_uncompressed_size
);
7849 wmsum_fini(&arc_sums
.arcstat_overhead_size
);
7850 wmsum_fini(&arc_sums
.arcstat_hdr_size
);
7851 wmsum_fini(&arc_sums
.arcstat_data_size
);
7852 wmsum_fini(&arc_sums
.arcstat_metadata_size
);
7853 wmsum_fini(&arc_sums
.arcstat_dbuf_size
);
7854 aggsum_fini(&arc_sums
.arcstat_dnode_size
);
7855 wmsum_fini(&arc_sums
.arcstat_bonus_size
);
7856 wmsum_fini(&arc_sums
.arcstat_l2_hits
);
7857 wmsum_fini(&arc_sums
.arcstat_l2_misses
);
7858 wmsum_fini(&arc_sums
.arcstat_l2_prefetch_asize
);
7859 wmsum_fini(&arc_sums
.arcstat_l2_mru_asize
);
7860 wmsum_fini(&arc_sums
.arcstat_l2_mfu_asize
);
7861 wmsum_fini(&arc_sums
.arcstat_l2_bufc_data_asize
);
7862 wmsum_fini(&arc_sums
.arcstat_l2_bufc_metadata_asize
);
7863 wmsum_fini(&arc_sums
.arcstat_l2_feeds
);
7864 wmsum_fini(&arc_sums
.arcstat_l2_rw_clash
);
7865 wmsum_fini(&arc_sums
.arcstat_l2_read_bytes
);
7866 wmsum_fini(&arc_sums
.arcstat_l2_write_bytes
);
7867 wmsum_fini(&arc_sums
.arcstat_l2_writes_sent
);
7868 wmsum_fini(&arc_sums
.arcstat_l2_writes_done
);
7869 wmsum_fini(&arc_sums
.arcstat_l2_writes_error
);
7870 wmsum_fini(&arc_sums
.arcstat_l2_writes_lock_retry
);
7871 wmsum_fini(&arc_sums
.arcstat_l2_evict_lock_retry
);
7872 wmsum_fini(&arc_sums
.arcstat_l2_evict_reading
);
7873 wmsum_fini(&arc_sums
.arcstat_l2_evict_l1cached
);
7874 wmsum_fini(&arc_sums
.arcstat_l2_free_on_write
);
7875 wmsum_fini(&arc_sums
.arcstat_l2_abort_lowmem
);
7876 wmsum_fini(&arc_sums
.arcstat_l2_cksum_bad
);
7877 wmsum_fini(&arc_sums
.arcstat_l2_io_error
);
7878 wmsum_fini(&arc_sums
.arcstat_l2_lsize
);
7879 wmsum_fini(&arc_sums
.arcstat_l2_psize
);
7880 aggsum_fini(&arc_sums
.arcstat_l2_hdr_size
);
7881 wmsum_fini(&arc_sums
.arcstat_l2_log_blk_writes
);
7882 wmsum_fini(&arc_sums
.arcstat_l2_log_blk_asize
);
7883 wmsum_fini(&arc_sums
.arcstat_l2_log_blk_count
);
7884 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_success
);
7885 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_unsupported
);
7886 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_io_errors
);
7887 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_dh_errors
);
7888 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_cksum_lb_errors
);
7889 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_lowmem
);
7890 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_size
);
7891 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_asize
);
7892 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_bufs
);
7893 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_bufs_precached
);
7894 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_log_blks
);
7895 wmsum_fini(&arc_sums
.arcstat_memory_throttle_count
);
7896 wmsum_fini(&arc_sums
.arcstat_memory_direct_count
);
7897 wmsum_fini(&arc_sums
.arcstat_memory_indirect_count
);
7898 wmsum_fini(&arc_sums
.arcstat_prune
);
7899 aggsum_fini(&arc_sums
.arcstat_meta_used
);
7900 wmsum_fini(&arc_sums
.arcstat_async_upgrade_sync
);
7901 wmsum_fini(&arc_sums
.arcstat_demand_hit_predictive_prefetch
);
7902 wmsum_fini(&arc_sums
.arcstat_demand_hit_prescient_prefetch
);
7903 wmsum_fini(&arc_sums
.arcstat_raw_size
);
7904 wmsum_fini(&arc_sums
.arcstat_cached_only_in_progress
);
7905 wmsum_fini(&arc_sums
.arcstat_abd_chunk_waste_size
);
7909 arc_target_bytes(void)
7915 arc_set_limits(uint64_t allmem
)
7917 /* Set min cache to 1/32 of all memory, or 32MB, whichever is more. */
7918 arc_c_min
= MAX(allmem
/ 32, 2ULL << SPA_MAXBLOCKSHIFT
);
7920 /* How to set default max varies by platform. */
7921 arc_c_max
= arc_default_max(arc_c_min
, allmem
);
7926 uint64_t percent
, allmem
= arc_all_memory();
7927 mutex_init(&arc_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7928 list_create(&arc_evict_waiters
, sizeof (arc_evict_waiter_t
),
7929 offsetof(arc_evict_waiter_t
, aew_node
));
7931 arc_min_prefetch_ms
= 1000;
7932 arc_min_prescient_prefetch_ms
= 6000;
7934 #if defined(_KERNEL)
7938 arc_set_limits(allmem
);
7942 * If zfs_arc_max is non-zero at init, meaning it was set in the kernel
7943 * environment before the module was loaded, don't block setting the
7944 * maximum because it is less than arc_c_min, instead, reset arc_c_min
7946 * zfs_arc_min will be handled by arc_tuning_update().
7948 if (zfs_arc_max
!= 0 && zfs_arc_max
>= MIN_ARC_MAX
&&
7949 zfs_arc_max
< allmem
) {
7950 arc_c_max
= zfs_arc_max
;
7951 if (arc_c_min
>= arc_c_max
) {
7952 arc_c_min
= MAX(zfs_arc_max
/ 2,
7953 2ULL << SPA_MAXBLOCKSHIFT
);
7958 * In userland, there's only the memory pressure that we artificially
7959 * create (see arc_available_memory()). Don't let arc_c get too
7960 * small, because it can cause transactions to be larger than
7961 * arc_c, causing arc_tempreserve_space() to fail.
7963 arc_c_min
= MAX(arc_c_max
/ 2, 2ULL << SPA_MAXBLOCKSHIFT
);
7967 arc_p
= (arc_c
>> 1);
7969 /* Set min to 1/2 of arc_c_min */
7970 arc_meta_min
= 1ULL << SPA_MAXBLOCKSHIFT
;
7972 * Set arc_meta_limit to a percent of arc_c_max with a floor of
7973 * arc_meta_min, and a ceiling of arc_c_max.
7975 percent
= MIN(zfs_arc_meta_limit_percent
, 100);
7976 arc_meta_limit
= MAX(arc_meta_min
, (percent
* arc_c_max
) / 100);
7977 percent
= MIN(zfs_arc_dnode_limit_percent
, 100);
7978 arc_dnode_size_limit
= (percent
* arc_meta_limit
) / 100;
7980 /* Apply user specified tunings */
7981 arc_tuning_update(B_TRUE
);
7983 /* if kmem_flags are set, lets try to use less memory */
7984 if (kmem_debugging())
7986 if (arc_c
< arc_c_min
)
7989 arc_register_hotplug();
7995 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
7996 offsetof(arc_prune_t
, p_node
));
7997 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
7999 arc_prune_taskq
= taskq_create("arc_prune", zfs_arc_prune_task_threads
,
8000 defclsyspri
, 100, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
8002 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
8003 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
8005 if (arc_ksp
!= NULL
) {
8006 arc_ksp
->ks_data
= &arc_stats
;
8007 arc_ksp
->ks_update
= arc_kstat_update
;
8008 kstat_install(arc_ksp
);
8011 arc_evict_zthr
= zthr_create("arc_evict",
8012 arc_evict_cb_check
, arc_evict_cb
, NULL
, defclsyspri
);
8013 arc_reap_zthr
= zthr_create_timer("arc_reap",
8014 arc_reap_cb_check
, arc_reap_cb
, NULL
, SEC2NSEC(1), minclsyspri
);
8019 * Calculate maximum amount of dirty data per pool.
8021 * If it has been set by a module parameter, take that.
8022 * Otherwise, use a percentage of physical memory defined by
8023 * zfs_dirty_data_max_percent (default 10%) with a cap at
8024 * zfs_dirty_data_max_max (default 4G or 25% of physical memory).
8027 if (zfs_dirty_data_max_max
== 0)
8028 zfs_dirty_data_max_max
= MIN(4ULL * 1024 * 1024 * 1024,
8029 allmem
* zfs_dirty_data_max_max_percent
/ 100);
8031 if (zfs_dirty_data_max_max
== 0)
8032 zfs_dirty_data_max_max
= MIN(1ULL * 1024 * 1024 * 1024,
8033 allmem
* zfs_dirty_data_max_max_percent
/ 100);
8036 if (zfs_dirty_data_max
== 0) {
8037 zfs_dirty_data_max
= allmem
*
8038 zfs_dirty_data_max_percent
/ 100;
8039 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
8040 zfs_dirty_data_max_max
);
8043 if (zfs_wrlog_data_max
== 0) {
8046 * dp_wrlog_total is reduced for each txg at the end of
8047 * spa_sync(). However, dp_dirty_total is reduced every time
8048 * a block is written out. Thus under normal operation,
8049 * dp_wrlog_total could grow 2 times as big as
8050 * zfs_dirty_data_max.
8052 zfs_wrlog_data_max
= zfs_dirty_data_max
* 2;
8063 #endif /* _KERNEL */
8065 /* Use B_TRUE to ensure *all* buffers are evicted */
8066 arc_flush(NULL
, B_TRUE
);
8068 if (arc_ksp
!= NULL
) {
8069 kstat_delete(arc_ksp
);
8073 taskq_wait(arc_prune_taskq
);
8074 taskq_destroy(arc_prune_taskq
);
8076 mutex_enter(&arc_prune_mtx
);
8077 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
8078 list_remove(&arc_prune_list
, p
);
8079 zfs_refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
8080 zfs_refcount_destroy(&p
->p_refcnt
);
8081 kmem_free(p
, sizeof (*p
));
8083 mutex_exit(&arc_prune_mtx
);
8085 list_destroy(&arc_prune_list
);
8086 mutex_destroy(&arc_prune_mtx
);
8088 (void) zthr_cancel(arc_evict_zthr
);
8089 (void) zthr_cancel(arc_reap_zthr
);
8091 mutex_destroy(&arc_evict_lock
);
8092 list_destroy(&arc_evict_waiters
);
8095 * Free any buffers that were tagged for destruction. This needs
8096 * to occur before arc_state_fini() runs and destroys the aggsum
8097 * values which are updated when freeing scatter ABDs.
8099 l2arc_do_free_on_write();
8102 * buf_fini() must proceed arc_state_fini() because buf_fin() may
8103 * trigger the release of kmem magazines, which can callback to
8104 * arc_space_return() which accesses aggsums freed in act_state_fini().
8109 arc_unregister_hotplug();
8112 * We destroy the zthrs after all the ARC state has been
8113 * torn down to avoid the case of them receiving any
8114 * wakeup() signals after they are destroyed.
8116 zthr_destroy(arc_evict_zthr
);
8117 zthr_destroy(arc_reap_zthr
);
8119 ASSERT0(arc_loaned_bytes
);
8125 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
8126 * It uses dedicated storage devices to hold cached data, which are populated
8127 * using large infrequent writes. The main role of this cache is to boost
8128 * the performance of random read workloads. The intended L2ARC devices
8129 * include short-stroked disks, solid state disks, and other media with
8130 * substantially faster read latency than disk.
8132 * +-----------------------+
8134 * +-----------------------+
8137 * l2arc_feed_thread() arc_read()
8141 * +---------------+ |
8143 * +---------------+ |
8148 * +-------+ +-------+
8150 * | cache | | cache |
8151 * +-------+ +-------+
8152 * +=========+ .-----.
8153 * : L2ARC : |-_____-|
8154 * : devices : | Disks |
8155 * +=========+ `-_____-'
8157 * Read requests are satisfied from the following sources, in order:
8160 * 2) vdev cache of L2ARC devices
8162 * 4) vdev cache of disks
8165 * Some L2ARC device types exhibit extremely slow write performance.
8166 * To accommodate for this there are some significant differences between
8167 * the L2ARC and traditional cache design:
8169 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
8170 * the ARC behave as usual, freeing buffers and placing headers on ghost
8171 * lists. The ARC does not send buffers to the L2ARC during eviction as
8172 * this would add inflated write latencies for all ARC memory pressure.
8174 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
8175 * It does this by periodically scanning buffers from the eviction-end of
8176 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
8177 * not already there. It scans until a headroom of buffers is satisfied,
8178 * which itself is a buffer for ARC eviction. If a compressible buffer is
8179 * found during scanning and selected for writing to an L2ARC device, we
8180 * temporarily boost scanning headroom during the next scan cycle to make
8181 * sure we adapt to compression effects (which might significantly reduce
8182 * the data volume we write to L2ARC). The thread that does this is
8183 * l2arc_feed_thread(), illustrated below; example sizes are included to
8184 * provide a better sense of ratio than this diagram:
8187 * +---------------------+----------+
8188 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
8189 * +---------------------+----------+ | o L2ARC eligible
8190 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
8191 * +---------------------+----------+ |
8192 * 15.9 Gbytes ^ 32 Mbytes |
8194 * l2arc_feed_thread()
8196 * l2arc write hand <--[oooo]--'
8200 * +==============================+
8201 * L2ARC dev |####|#|###|###| |####| ... |
8202 * +==============================+
8205 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
8206 * evicted, then the L2ARC has cached a buffer much sooner than it probably
8207 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
8208 * safe to say that this is an uncommon case, since buffers at the end of
8209 * the ARC lists have moved there due to inactivity.
8211 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
8212 * then the L2ARC simply misses copying some buffers. This serves as a
8213 * pressure valve to prevent heavy read workloads from both stalling the ARC
8214 * with waits and clogging the L2ARC with writes. This also helps prevent
8215 * the potential for the L2ARC to churn if it attempts to cache content too
8216 * quickly, such as during backups of the entire pool.
8218 * 5. After system boot and before the ARC has filled main memory, there are
8219 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
8220 * lists can remain mostly static. Instead of searching from tail of these
8221 * lists as pictured, the l2arc_feed_thread() will search from the list heads
8222 * for eligible buffers, greatly increasing its chance of finding them.
8224 * The L2ARC device write speed is also boosted during this time so that
8225 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
8226 * there are no L2ARC reads, and no fear of degrading read performance
8227 * through increased writes.
8229 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
8230 * the vdev queue can aggregate them into larger and fewer writes. Each
8231 * device is written to in a rotor fashion, sweeping writes through
8232 * available space then repeating.
8234 * 7. The L2ARC does not store dirty content. It never needs to flush
8235 * write buffers back to disk based storage.
8237 * 8. If an ARC buffer is written (and dirtied) which also exists in the
8238 * L2ARC, the now stale L2ARC buffer is immediately dropped.
8240 * The performance of the L2ARC can be tweaked by a number of tunables, which
8241 * may be necessary for different workloads:
8243 * l2arc_write_max max write bytes per interval
8244 * l2arc_write_boost extra write bytes during device warmup
8245 * l2arc_noprefetch skip caching prefetched buffers
8246 * l2arc_headroom number of max device writes to precache
8247 * l2arc_headroom_boost when we find compressed buffers during ARC
8248 * scanning, we multiply headroom by this
8249 * percentage factor for the next scan cycle,
8250 * since more compressed buffers are likely to
8252 * l2arc_feed_secs seconds between L2ARC writing
8254 * Tunables may be removed or added as future performance improvements are
8255 * integrated, and also may become zpool properties.
8257 * There are three key functions that control how the L2ARC warms up:
8259 * l2arc_write_eligible() check if a buffer is eligible to cache
8260 * l2arc_write_size() calculate how much to write
8261 * l2arc_write_interval() calculate sleep delay between writes
8263 * These three functions determine what to write, how much, and how quickly
8266 * L2ARC persistence:
8268 * When writing buffers to L2ARC, we periodically add some metadata to
8269 * make sure we can pick them up after reboot, thus dramatically reducing
8270 * the impact that any downtime has on the performance of storage systems
8271 * with large caches.
8273 * The implementation works fairly simply by integrating the following two
8276 * *) When writing to the L2ARC, we occasionally write a "l2arc log block",
8277 * which is an additional piece of metadata which describes what's been
8278 * written. This allows us to rebuild the arc_buf_hdr_t structures of the
8279 * main ARC buffers. There are 2 linked-lists of log blocks headed by
8280 * dh_start_lbps[2]. We alternate which chain we append to, so they are
8281 * time-wise and offset-wise interleaved, but that is an optimization rather
8282 * than for correctness. The log block also includes a pointer to the
8283 * previous block in its chain.
8285 * *) We reserve SPA_MINBLOCKSIZE of space at the start of each L2ARC device
8286 * for our header bookkeeping purposes. This contains a device header,
8287 * which contains our top-level reference structures. We update it each
8288 * time we write a new log block, so that we're able to locate it in the
8289 * L2ARC device. If this write results in an inconsistent device header
8290 * (e.g. due to power failure), we detect this by verifying the header's
8291 * checksum and simply fail to reconstruct the L2ARC after reboot.
8293 * Implementation diagram:
8295 * +=== L2ARC device (not to scale) ======================================+
8296 * | ___two newest log block pointers__.__________ |
8297 * | / \dh_start_lbps[1] |
8298 * | / \ \dh_start_lbps[0]|
8300 * ||L2 dev|....|lb |bufs |lb |bufs |lb |bufs |lb |bufs |lb |---(empty)---|
8301 * || hdr| ^ /^ /^ / / |
8302 * |+------+ ...--\-------/ \-----/--\------/ / |
8303 * | \--------------/ \--------------/ |
8304 * +======================================================================+
8306 * As can be seen on the diagram, rather than using a simple linked list,
8307 * we use a pair of linked lists with alternating elements. This is a
8308 * performance enhancement due to the fact that we only find out the
8309 * address of the next log block access once the current block has been
8310 * completely read in. Obviously, this hurts performance, because we'd be
8311 * keeping the device's I/O queue at only a 1 operation deep, thus
8312 * incurring a large amount of I/O round-trip latency. Having two lists
8313 * allows us to fetch two log blocks ahead of where we are currently
8314 * rebuilding L2ARC buffers.
8316 * On-device data structures:
8318 * L2ARC device header: l2arc_dev_hdr_phys_t
8319 * L2ARC log block: l2arc_log_blk_phys_t
8321 * L2ARC reconstruction:
8323 * When writing data, we simply write in the standard rotary fashion,
8324 * evicting buffers as we go and simply writing new data over them (writing
8325 * a new log block every now and then). This obviously means that once we
8326 * loop around the end of the device, we will start cutting into an already
8327 * committed log block (and its referenced data buffers), like so:
8329 * current write head__ __old tail
8332 * <--|bufs |lb |bufs |lb | |bufs |lb |bufs |lb |-->
8333 * ^ ^^^^^^^^^___________________________________
8335 * <<nextwrite>> may overwrite this blk and/or its bufs --'
8337 * When importing the pool, we detect this situation and use it to stop
8338 * our scanning process (see l2arc_rebuild).
8340 * There is one significant caveat to consider when rebuilding ARC contents
8341 * from an L2ARC device: what about invalidated buffers? Given the above
8342 * construction, we cannot update blocks which we've already written to amend
8343 * them to remove buffers which were invalidated. Thus, during reconstruction,
8344 * we might be populating the cache with buffers for data that's not on the
8345 * main pool anymore, or may have been overwritten!
8347 * As it turns out, this isn't a problem. Every arc_read request includes
8348 * both the DVA and, crucially, the birth TXG of the BP the caller is
8349 * looking for. So even if the cache were populated by completely rotten
8350 * blocks for data that had been long deleted and/or overwritten, we'll
8351 * never actually return bad data from the cache, since the DVA with the
8352 * birth TXG uniquely identify a block in space and time - once created,
8353 * a block is immutable on disk. The worst thing we have done is wasted
8354 * some time and memory at l2arc rebuild to reconstruct outdated ARC
8355 * entries that will get dropped from the l2arc as it is being updated
8358 * L2ARC buffers that have been evicted by l2arc_evict() ahead of the write
8359 * hand are not restored. This is done by saving the offset (in bytes)
8360 * l2arc_evict() has evicted to in the L2ARC device header and taking it
8361 * into account when restoring buffers.
8365 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*hdr
)
8368 * A buffer is *not* eligible for the L2ARC if it:
8369 * 1. belongs to a different spa.
8370 * 2. is already cached on the L2ARC.
8371 * 3. has an I/O in progress (it may be an incomplete read).
8372 * 4. is flagged not eligible (zfs property).
8374 if (hdr
->b_spa
!= spa_guid
|| HDR_HAS_L2HDR(hdr
) ||
8375 HDR_IO_IN_PROGRESS(hdr
) || !HDR_L2CACHE(hdr
))
8382 l2arc_write_size(l2arc_dev_t
*dev
)
8384 uint64_t size
, dev_size
, tsize
;
8387 * Make sure our globals have meaningful values in case the user
8390 size
= l2arc_write_max
;
8392 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
8393 "be greater than zero, resetting it to the default (%d)",
8395 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
8398 if (arc_warm
== B_FALSE
)
8399 size
+= l2arc_write_boost
;
8402 * Make sure the write size does not exceed the size of the cache
8403 * device. This is important in l2arc_evict(), otherwise infinite
8404 * iteration can occur.
8406 dev_size
= dev
->l2ad_end
- dev
->l2ad_start
;
8407 tsize
= size
+ l2arc_log_blk_overhead(size
, dev
);
8408 if (dev
->l2ad_vdev
->vdev_has_trim
&& l2arc_trim_ahead
> 0)
8409 tsize
+= MAX(64 * 1024 * 1024,
8410 (tsize
* l2arc_trim_ahead
) / 100);
8412 if (tsize
>= dev_size
) {
8413 cmn_err(CE_NOTE
, "l2arc_write_max or l2arc_write_boost "
8414 "plus the overhead of log blocks (persistent L2ARC, "
8415 "%llu bytes) exceeds the size of the cache device "
8416 "(guid %llu), resetting them to the default (%d)",
8417 (u_longlong_t
)l2arc_log_blk_overhead(size
, dev
),
8418 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
, L2ARC_WRITE_SIZE
);
8419 size
= l2arc_write_max
= l2arc_write_boost
= L2ARC_WRITE_SIZE
;
8421 if (arc_warm
== B_FALSE
)
8422 size
+= l2arc_write_boost
;
8430 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
8432 clock_t interval
, next
, now
;
8435 * If the ARC lists are busy, increase our write rate; if the
8436 * lists are stale, idle back. This is achieved by checking
8437 * how much we previously wrote - if it was more than half of
8438 * what we wanted, schedule the next write much sooner.
8440 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
8441 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
8443 interval
= hz
* l2arc_feed_secs
;
8445 now
= ddi_get_lbolt();
8446 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
8452 * Cycle through L2ARC devices. This is how L2ARC load balances.
8453 * If a device is returned, this also returns holding the spa config lock.
8455 static l2arc_dev_t
*
8456 l2arc_dev_get_next(void)
8458 l2arc_dev_t
*first
, *next
= NULL
;
8461 * Lock out the removal of spas (spa_namespace_lock), then removal
8462 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
8463 * both locks will be dropped and a spa config lock held instead.
8465 mutex_enter(&spa_namespace_lock
);
8466 mutex_enter(&l2arc_dev_mtx
);
8468 /* if there are no vdevs, there is nothing to do */
8469 if (l2arc_ndev
== 0)
8473 next
= l2arc_dev_last
;
8475 /* loop around the list looking for a non-faulted vdev */
8477 next
= list_head(l2arc_dev_list
);
8479 next
= list_next(l2arc_dev_list
, next
);
8481 next
= list_head(l2arc_dev_list
);
8484 /* if we have come back to the start, bail out */
8487 else if (next
== first
)
8490 } while (vdev_is_dead(next
->l2ad_vdev
) || next
->l2ad_rebuild
||
8491 next
->l2ad_trim_all
);
8493 /* if we were unable to find any usable vdevs, return NULL */
8494 if (vdev_is_dead(next
->l2ad_vdev
) || next
->l2ad_rebuild
||
8495 next
->l2ad_trim_all
)
8498 l2arc_dev_last
= next
;
8501 mutex_exit(&l2arc_dev_mtx
);
8504 * Grab the config lock to prevent the 'next' device from being
8505 * removed while we are writing to it.
8508 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
8509 mutex_exit(&spa_namespace_lock
);
8515 * Free buffers that were tagged for destruction.
8518 l2arc_do_free_on_write(void)
8521 l2arc_data_free_t
*df
, *df_prev
;
8523 mutex_enter(&l2arc_free_on_write_mtx
);
8524 buflist
= l2arc_free_on_write
;
8526 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
8527 df_prev
= list_prev(buflist
, df
);
8528 ASSERT3P(df
->l2df_abd
, !=, NULL
);
8529 abd_free(df
->l2df_abd
);
8530 list_remove(buflist
, df
);
8531 kmem_free(df
, sizeof (l2arc_data_free_t
));
8534 mutex_exit(&l2arc_free_on_write_mtx
);
8538 * A write to a cache device has completed. Update all headers to allow
8539 * reads from these buffers to begin.
8542 l2arc_write_done(zio_t
*zio
)
8544 l2arc_write_callback_t
*cb
;
8545 l2arc_lb_abd_buf_t
*abd_buf
;
8546 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
8548 l2arc_dev_hdr_phys_t
*l2dhdr
;
8550 arc_buf_hdr_t
*head
, *hdr
, *hdr_prev
;
8551 kmutex_t
*hash_lock
;
8552 int64_t bytes_dropped
= 0;
8554 cb
= zio
->io_private
;
8555 ASSERT3P(cb
, !=, NULL
);
8556 dev
= cb
->l2wcb_dev
;
8557 l2dhdr
= dev
->l2ad_dev_hdr
;
8558 ASSERT3P(dev
, !=, NULL
);
8559 head
= cb
->l2wcb_head
;
8560 ASSERT3P(head
, !=, NULL
);
8561 buflist
= &dev
->l2ad_buflist
;
8562 ASSERT3P(buflist
, !=, NULL
);
8563 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
8564 l2arc_write_callback_t
*, cb
);
8567 * All writes completed, or an error was hit.
8570 mutex_enter(&dev
->l2ad_mtx
);
8571 for (hdr
= list_prev(buflist
, head
); hdr
; hdr
= hdr_prev
) {
8572 hdr_prev
= list_prev(buflist
, hdr
);
8574 hash_lock
= HDR_LOCK(hdr
);
8577 * We cannot use mutex_enter or else we can deadlock
8578 * with l2arc_write_buffers (due to swapping the order
8579 * the hash lock and l2ad_mtx are taken).
8581 if (!mutex_tryenter(hash_lock
)) {
8583 * Missed the hash lock. We must retry so we
8584 * don't leave the ARC_FLAG_L2_WRITING bit set.
8586 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry
);
8589 * We don't want to rescan the headers we've
8590 * already marked as having been written out, so
8591 * we reinsert the head node so we can pick up
8592 * where we left off.
8594 list_remove(buflist
, head
);
8595 list_insert_after(buflist
, hdr
, head
);
8597 mutex_exit(&dev
->l2ad_mtx
);
8600 * We wait for the hash lock to become available
8601 * to try and prevent busy waiting, and increase
8602 * the chance we'll be able to acquire the lock
8603 * the next time around.
8605 mutex_enter(hash_lock
);
8606 mutex_exit(hash_lock
);
8611 * We could not have been moved into the arc_l2c_only
8612 * state while in-flight due to our ARC_FLAG_L2_WRITING
8613 * bit being set. Let's just ensure that's being enforced.
8615 ASSERT(HDR_HAS_L1HDR(hdr
));
8618 * Skipped - drop L2ARC entry and mark the header as no
8619 * longer L2 eligibile.
8621 if (zio
->io_error
!= 0) {
8623 * Error - drop L2ARC entry.
8625 list_remove(buflist
, hdr
);
8626 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
8628 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8629 l2arc_hdr_arcstats_decrement(hdr
);
8632 vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
8633 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
,
8634 arc_hdr_size(hdr
), hdr
);
8638 * Allow ARC to begin reads and ghost list evictions to
8641 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_WRITING
);
8643 mutex_exit(hash_lock
);
8647 * Free the allocated abd buffers for writing the log blocks.
8648 * If the zio failed reclaim the allocated space and remove the
8649 * pointers to these log blocks from the log block pointer list
8650 * of the L2ARC device.
8652 while ((abd_buf
= list_remove_tail(&cb
->l2wcb_abd_list
)) != NULL
) {
8653 abd_free(abd_buf
->abd
);
8654 zio_buf_free(abd_buf
, sizeof (*abd_buf
));
8655 if (zio
->io_error
!= 0) {
8656 lb_ptr_buf
= list_remove_head(&dev
->l2ad_lbptr_list
);
8658 * L2BLK_GET_PSIZE returns aligned size for log
8662 L2BLK_GET_PSIZE((lb_ptr_buf
->lb_ptr
)->lbp_prop
);
8663 bytes_dropped
+= asize
;
8664 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, -asize
);
8665 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count
);
8666 zfs_refcount_remove_many(&dev
->l2ad_lb_asize
, asize
,
8668 zfs_refcount_remove(&dev
->l2ad_lb_count
, lb_ptr_buf
);
8669 kmem_free(lb_ptr_buf
->lb_ptr
,
8670 sizeof (l2arc_log_blkptr_t
));
8671 kmem_free(lb_ptr_buf
, sizeof (l2arc_lb_ptr_buf_t
));
8674 list_destroy(&cb
->l2wcb_abd_list
);
8676 if (zio
->io_error
!= 0) {
8677 ARCSTAT_BUMP(arcstat_l2_writes_error
);
8680 * Restore the lbps array in the header to its previous state.
8681 * If the list of log block pointers is empty, zero out the
8682 * log block pointers in the device header.
8684 lb_ptr_buf
= list_head(&dev
->l2ad_lbptr_list
);
8685 for (int i
= 0; i
< 2; i
++) {
8686 if (lb_ptr_buf
== NULL
) {
8688 * If the list is empty zero out the device
8689 * header. Otherwise zero out the second log
8690 * block pointer in the header.
8693 bzero(l2dhdr
, dev
->l2ad_dev_hdr_asize
);
8695 bzero(&l2dhdr
->dh_start_lbps
[i
],
8696 sizeof (l2arc_log_blkptr_t
));
8700 bcopy(lb_ptr_buf
->lb_ptr
, &l2dhdr
->dh_start_lbps
[i
],
8701 sizeof (l2arc_log_blkptr_t
));
8702 lb_ptr_buf
= list_next(&dev
->l2ad_lbptr_list
,
8707 ARCSTAT_BUMP(arcstat_l2_writes_done
);
8708 list_remove(buflist
, head
);
8709 ASSERT(!HDR_HAS_L1HDR(head
));
8710 kmem_cache_free(hdr_l2only_cache
, head
);
8711 mutex_exit(&dev
->l2ad_mtx
);
8713 ASSERT(dev
->l2ad_vdev
!= NULL
);
8714 vdev_space_update(dev
->l2ad_vdev
, -bytes_dropped
, 0, 0);
8716 l2arc_do_free_on_write();
8718 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
8722 l2arc_untransform(zio_t
*zio
, l2arc_read_callback_t
*cb
)
8725 spa_t
*spa
= zio
->io_spa
;
8726 arc_buf_hdr_t
*hdr
= cb
->l2rcb_hdr
;
8727 blkptr_t
*bp
= zio
->io_bp
;
8728 uint8_t salt
[ZIO_DATA_SALT_LEN
];
8729 uint8_t iv
[ZIO_DATA_IV_LEN
];
8730 uint8_t mac
[ZIO_DATA_MAC_LEN
];
8731 boolean_t no_crypt
= B_FALSE
;
8734 * ZIL data is never be written to the L2ARC, so we don't need
8735 * special handling for its unique MAC storage.
8737 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
8738 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
8739 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8742 * If the data was encrypted, decrypt it now. Note that
8743 * we must check the bp here and not the hdr, since the
8744 * hdr does not have its encryption parameters updated
8745 * until arc_read_done().
8747 if (BP_IS_ENCRYPTED(bp
)) {
8748 abd_t
*eabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
,
8749 ARC_HDR_DO_ADAPT
| ARC_HDR_USE_RESERVE
);
8751 zio_crypt_decode_params_bp(bp
, salt
, iv
);
8752 zio_crypt_decode_mac_bp(bp
, mac
);
8754 ret
= spa_do_crypt_abd(B_FALSE
, spa
, &cb
->l2rcb_zb
,
8755 BP_GET_TYPE(bp
), BP_GET_DEDUP(bp
), BP_SHOULD_BYTESWAP(bp
),
8756 salt
, iv
, mac
, HDR_GET_PSIZE(hdr
), eabd
,
8757 hdr
->b_l1hdr
.b_pabd
, &no_crypt
);
8759 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8764 * If we actually performed decryption, replace b_pabd
8765 * with the decrypted data. Otherwise we can just throw
8766 * our decryption buffer away.
8769 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8770 arc_hdr_size(hdr
), hdr
);
8771 hdr
->b_l1hdr
.b_pabd
= eabd
;
8774 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8779 * If the L2ARC block was compressed, but ARC compression
8780 * is disabled we decompress the data into a new buffer and
8781 * replace the existing data.
8783 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
8784 !HDR_COMPRESSION_ENABLED(hdr
)) {
8785 abd_t
*cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
,
8786 ARC_HDR_DO_ADAPT
| ARC_HDR_USE_RESERVE
);
8787 void *tmp
= abd_borrow_buf(cabd
, arc_hdr_size(hdr
));
8789 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
8790 hdr
->b_l1hdr
.b_pabd
, tmp
, HDR_GET_PSIZE(hdr
),
8791 HDR_GET_LSIZE(hdr
), &hdr
->b_complevel
);
8793 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
8794 arc_free_data_abd(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
8798 abd_return_buf_copy(cabd
, tmp
, arc_hdr_size(hdr
));
8799 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8800 arc_hdr_size(hdr
), hdr
);
8801 hdr
->b_l1hdr
.b_pabd
= cabd
;
8803 zio
->io_size
= HDR_GET_LSIZE(hdr
);
8814 * A read to a cache device completed. Validate buffer contents before
8815 * handing over to the regular ARC routines.
8818 l2arc_read_done(zio_t
*zio
)
8821 l2arc_read_callback_t
*cb
= zio
->io_private
;
8823 kmutex_t
*hash_lock
;
8824 boolean_t valid_cksum
;
8825 boolean_t using_rdata
= (BP_IS_ENCRYPTED(&cb
->l2rcb_bp
) &&
8826 (cb
->l2rcb_flags
& ZIO_FLAG_RAW_ENCRYPT
));
8828 ASSERT3P(zio
->io_vd
, !=, NULL
);
8829 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
8831 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
8833 ASSERT3P(cb
, !=, NULL
);
8834 hdr
= cb
->l2rcb_hdr
;
8835 ASSERT3P(hdr
, !=, NULL
);
8837 hash_lock
= HDR_LOCK(hdr
);
8838 mutex_enter(hash_lock
);
8839 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
8842 * If the data was read into a temporary buffer,
8843 * move it and free the buffer.
8845 if (cb
->l2rcb_abd
!= NULL
) {
8846 ASSERT3U(arc_hdr_size(hdr
), <, zio
->io_size
);
8847 if (zio
->io_error
== 0) {
8849 abd_copy(hdr
->b_crypt_hdr
.b_rabd
,
8850 cb
->l2rcb_abd
, arc_hdr_size(hdr
));
8852 abd_copy(hdr
->b_l1hdr
.b_pabd
,
8853 cb
->l2rcb_abd
, arc_hdr_size(hdr
));
8858 * The following must be done regardless of whether
8859 * there was an error:
8860 * - free the temporary buffer
8861 * - point zio to the real ARC buffer
8862 * - set zio size accordingly
8863 * These are required because zio is either re-used for
8864 * an I/O of the block in the case of the error
8865 * or the zio is passed to arc_read_done() and it
8868 abd_free(cb
->l2rcb_abd
);
8869 zio
->io_size
= zio
->io_orig_size
= arc_hdr_size(hdr
);
8872 ASSERT(HDR_HAS_RABD(hdr
));
8873 zio
->io_abd
= zio
->io_orig_abd
=
8874 hdr
->b_crypt_hdr
.b_rabd
;
8876 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8877 zio
->io_abd
= zio
->io_orig_abd
= hdr
->b_l1hdr
.b_pabd
;
8881 ASSERT3P(zio
->io_abd
, !=, NULL
);
8884 * Check this survived the L2ARC journey.
8886 ASSERT(zio
->io_abd
== hdr
->b_l1hdr
.b_pabd
||
8887 (HDR_HAS_RABD(hdr
) && zio
->io_abd
== hdr
->b_crypt_hdr
.b_rabd
));
8888 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
8889 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
8890 zio
->io_prop
.zp_complevel
= hdr
->b_complevel
;
8892 valid_cksum
= arc_cksum_is_equal(hdr
, zio
);
8895 * b_rabd will always match the data as it exists on disk if it is
8896 * being used. Therefore if we are reading into b_rabd we do not
8897 * attempt to untransform the data.
8899 if (valid_cksum
&& !using_rdata
)
8900 tfm_error
= l2arc_untransform(zio
, cb
);
8902 if (valid_cksum
&& tfm_error
== 0 && zio
->io_error
== 0 &&
8903 !HDR_L2_EVICTED(hdr
)) {
8904 mutex_exit(hash_lock
);
8905 zio
->io_private
= hdr
;
8909 * Buffer didn't survive caching. Increment stats and
8910 * reissue to the original storage device.
8912 if (zio
->io_error
!= 0) {
8913 ARCSTAT_BUMP(arcstat_l2_io_error
);
8915 zio
->io_error
= SET_ERROR(EIO
);
8917 if (!valid_cksum
|| tfm_error
!= 0)
8918 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
8921 * If there's no waiter, issue an async i/o to the primary
8922 * storage now. If there *is* a waiter, the caller must
8923 * issue the i/o in a context where it's OK to block.
8925 if (zio
->io_waiter
== NULL
) {
8926 zio_t
*pio
= zio_unique_parent(zio
);
8927 void *abd
= (using_rdata
) ?
8928 hdr
->b_crypt_hdr
.b_rabd
: hdr
->b_l1hdr
.b_pabd
;
8930 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
8932 zio
= zio_read(pio
, zio
->io_spa
, zio
->io_bp
,
8933 abd
, zio
->io_size
, arc_read_done
,
8934 hdr
, zio
->io_priority
, cb
->l2rcb_flags
,
8938 * Original ZIO will be freed, so we need to update
8939 * ARC header with the new ZIO pointer to be used
8940 * by zio_change_priority() in arc_read().
8942 for (struct arc_callback
*acb
= hdr
->b_l1hdr
.b_acb
;
8943 acb
!= NULL
; acb
= acb
->acb_next
)
8944 acb
->acb_zio_head
= zio
;
8946 mutex_exit(hash_lock
);
8949 mutex_exit(hash_lock
);
8953 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
8957 * This is the list priority from which the L2ARC will search for pages to
8958 * cache. This is used within loops (0..3) to cycle through lists in the
8959 * desired order. This order can have a significant effect on cache
8962 * Currently the metadata lists are hit first, MFU then MRU, followed by
8963 * the data lists. This function returns a locked list, and also returns
8966 static multilist_sublist_t
*
8967 l2arc_sublist_lock(int list_num
)
8969 multilist_t
*ml
= NULL
;
8972 ASSERT(list_num
>= 0 && list_num
< L2ARC_FEED_TYPES
);
8976 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
8979 ml
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
8982 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
8985 ml
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
8992 * Return a randomly-selected sublist. This is acceptable
8993 * because the caller feeds only a little bit of data for each
8994 * call (8MB). Subsequent calls will result in different
8995 * sublists being selected.
8997 idx
= multilist_get_random_index(ml
);
8998 return (multilist_sublist_lock(ml
, idx
));
9002 * Calculates the maximum overhead of L2ARC metadata log blocks for a given
9003 * L2ARC write size. l2arc_evict and l2arc_write_size need to include this
9004 * overhead in processing to make sure there is enough headroom available
9005 * when writing buffers.
9007 static inline uint64_t
9008 l2arc_log_blk_overhead(uint64_t write_sz
, l2arc_dev_t
*dev
)
9010 if (dev
->l2ad_log_entries
== 0) {
9013 uint64_t log_entries
= write_sz
>> SPA_MINBLOCKSHIFT
;
9015 uint64_t log_blocks
= (log_entries
+
9016 dev
->l2ad_log_entries
- 1) /
9017 dev
->l2ad_log_entries
;
9019 return (vdev_psize_to_asize(dev
->l2ad_vdev
,
9020 sizeof (l2arc_log_blk_phys_t
)) * log_blocks
);
9025 * Evict buffers from the device write hand to the distance specified in
9026 * bytes. This distance may span populated buffers, it may span nothing.
9027 * This is clearing a region on the L2ARC device ready for writing.
9028 * If the 'all' boolean is set, every buffer is evicted.
9031 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
9034 arc_buf_hdr_t
*hdr
, *hdr_prev
;
9035 kmutex_t
*hash_lock
;
9037 l2arc_lb_ptr_buf_t
*lb_ptr_buf
, *lb_ptr_buf_prev
;
9038 vdev_t
*vd
= dev
->l2ad_vdev
;
9041 buflist
= &dev
->l2ad_buflist
;
9044 * We need to add in the worst case scenario of log block overhead.
9046 distance
+= l2arc_log_blk_overhead(distance
, dev
);
9047 if (vd
->vdev_has_trim
&& l2arc_trim_ahead
> 0) {
9049 * Trim ahead of the write size 64MB or (l2arc_trim_ahead/100)
9050 * times the write size, whichever is greater.
9052 distance
+= MAX(64 * 1024 * 1024,
9053 (distance
* l2arc_trim_ahead
) / 100);
9058 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- distance
)) {
9060 * When there is no space to accommodate upcoming writes,
9061 * evict to the end. Then bump the write and evict hands
9062 * to the start and iterate. This iteration does not
9063 * happen indefinitely as we make sure in
9064 * l2arc_write_size() that when the write hand is reset,
9065 * the write size does not exceed the end of the device.
9068 taddr
= dev
->l2ad_end
;
9070 taddr
= dev
->l2ad_hand
+ distance
;
9072 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
9073 uint64_t, taddr
, boolean_t
, all
);
9077 * This check has to be placed after deciding whether to
9080 if (dev
->l2ad_first
) {
9082 * This is the first sweep through the device. There is
9083 * nothing to evict. We have already trimmmed the
9089 * Trim the space to be evicted.
9091 if (vd
->vdev_has_trim
&& dev
->l2ad_evict
< taddr
&&
9092 l2arc_trim_ahead
> 0) {
9094 * We have to drop the spa_config lock because
9095 * vdev_trim_range() will acquire it.
9096 * l2ad_evict already accounts for the label
9097 * size. To prevent vdev_trim_ranges() from
9098 * adding it again, we subtract it from
9101 spa_config_exit(dev
->l2ad_spa
, SCL_L2ARC
, dev
);
9102 vdev_trim_simple(vd
,
9103 dev
->l2ad_evict
- VDEV_LABEL_START_SIZE
,
9104 taddr
- dev
->l2ad_evict
);
9105 spa_config_enter(dev
->l2ad_spa
, SCL_L2ARC
, dev
,
9110 * When rebuilding L2ARC we retrieve the evict hand
9111 * from the header of the device. Of note, l2arc_evict()
9112 * does not actually delete buffers from the cache
9113 * device, but trimming may do so depending on the
9114 * hardware implementation. Thus keeping track of the
9115 * evict hand is useful.
9117 dev
->l2ad_evict
= MAX(dev
->l2ad_evict
, taddr
);
9122 mutex_enter(&dev
->l2ad_mtx
);
9124 * We have to account for evicted log blocks. Run vdev_space_update()
9125 * on log blocks whose offset (in bytes) is before the evicted offset
9126 * (in bytes) by searching in the list of pointers to log blocks
9127 * present in the L2ARC device.
9129 for (lb_ptr_buf
= list_tail(&dev
->l2ad_lbptr_list
); lb_ptr_buf
;
9130 lb_ptr_buf
= lb_ptr_buf_prev
) {
9132 lb_ptr_buf_prev
= list_prev(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
9134 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
9135 uint64_t asize
= L2BLK_GET_PSIZE(
9136 (lb_ptr_buf
->lb_ptr
)->lbp_prop
);
9139 * We don't worry about log blocks left behind (ie
9140 * lbp_payload_start < l2ad_hand) because l2arc_write_buffers()
9141 * will never write more than l2arc_evict() evicts.
9143 if (!all
&& l2arc_log_blkptr_valid(dev
, lb_ptr_buf
->lb_ptr
)) {
9146 vdev_space_update(vd
, -asize
, 0, 0);
9147 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, -asize
);
9148 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count
);
9149 zfs_refcount_remove_many(&dev
->l2ad_lb_asize
, asize
,
9151 zfs_refcount_remove(&dev
->l2ad_lb_count
, lb_ptr_buf
);
9152 list_remove(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
9153 kmem_free(lb_ptr_buf
->lb_ptr
,
9154 sizeof (l2arc_log_blkptr_t
));
9155 kmem_free(lb_ptr_buf
, sizeof (l2arc_lb_ptr_buf_t
));
9159 for (hdr
= list_tail(buflist
); hdr
; hdr
= hdr_prev
) {
9160 hdr_prev
= list_prev(buflist
, hdr
);
9162 ASSERT(!HDR_EMPTY(hdr
));
9163 hash_lock
= HDR_LOCK(hdr
);
9166 * We cannot use mutex_enter or else we can deadlock
9167 * with l2arc_write_buffers (due to swapping the order
9168 * the hash lock and l2ad_mtx are taken).
9170 if (!mutex_tryenter(hash_lock
)) {
9172 * Missed the hash lock. Retry.
9174 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
9175 mutex_exit(&dev
->l2ad_mtx
);
9176 mutex_enter(hash_lock
);
9177 mutex_exit(hash_lock
);
9182 * A header can't be on this list if it doesn't have L2 header.
9184 ASSERT(HDR_HAS_L2HDR(hdr
));
9186 /* Ensure this header has finished being written. */
9187 ASSERT(!HDR_L2_WRITING(hdr
));
9188 ASSERT(!HDR_L2_WRITE_HEAD(hdr
));
9190 if (!all
&& (hdr
->b_l2hdr
.b_daddr
>= dev
->l2ad_evict
||
9191 hdr
->b_l2hdr
.b_daddr
< dev
->l2ad_hand
)) {
9193 * We've evicted to the target address,
9194 * or the end of the device.
9196 mutex_exit(hash_lock
);
9200 if (!HDR_HAS_L1HDR(hdr
)) {
9201 ASSERT(!HDR_L2_READING(hdr
));
9203 * This doesn't exist in the ARC. Destroy.
9204 * arc_hdr_destroy() will call list_remove()
9205 * and decrement arcstat_l2_lsize.
9207 arc_change_state(arc_anon
, hdr
, hash_lock
);
9208 arc_hdr_destroy(hdr
);
9210 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_l2c_only
);
9211 ARCSTAT_BUMP(arcstat_l2_evict_l1cached
);
9213 * Invalidate issued or about to be issued
9214 * reads, since we may be about to write
9215 * over this location.
9217 if (HDR_L2_READING(hdr
)) {
9218 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
9219 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_EVICTED
);
9222 arc_hdr_l2hdr_destroy(hdr
);
9224 mutex_exit(hash_lock
);
9226 mutex_exit(&dev
->l2ad_mtx
);
9230 * We need to check if we evict all buffers, otherwise we may iterate
9233 if (!all
&& rerun
) {
9235 * Bump device hand to the device start if it is approaching the
9236 * end. l2arc_evict() has already evicted ahead for this case.
9238 dev
->l2ad_hand
= dev
->l2ad_start
;
9239 dev
->l2ad_evict
= dev
->l2ad_start
;
9240 dev
->l2ad_first
= B_FALSE
;
9246 * In case of cache device removal (all) the following
9247 * assertions may be violated without functional consequences
9248 * as the device is about to be removed.
9250 ASSERT3U(dev
->l2ad_hand
+ distance
, <, dev
->l2ad_end
);
9251 if (!dev
->l2ad_first
)
9252 ASSERT3U(dev
->l2ad_hand
, <, dev
->l2ad_evict
);
9257 * Handle any abd transforms that might be required for writing to the L2ARC.
9258 * If successful, this function will always return an abd with the data
9259 * transformed as it is on disk in a new abd of asize bytes.
9262 l2arc_apply_transforms(spa_t
*spa
, arc_buf_hdr_t
*hdr
, uint64_t asize
,
9267 abd_t
*cabd
= NULL
, *eabd
= NULL
, *to_write
= hdr
->b_l1hdr
.b_pabd
;
9268 enum zio_compress compress
= HDR_GET_COMPRESS(hdr
);
9269 uint64_t psize
= HDR_GET_PSIZE(hdr
);
9270 uint64_t size
= arc_hdr_size(hdr
);
9271 boolean_t ismd
= HDR_ISTYPE_METADATA(hdr
);
9272 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
9273 dsl_crypto_key_t
*dck
= NULL
;
9274 uint8_t mac
[ZIO_DATA_MAC_LEN
] = { 0 };
9275 boolean_t no_crypt
= B_FALSE
;
9277 ASSERT((HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
9278 !HDR_COMPRESSION_ENABLED(hdr
)) ||
9279 HDR_ENCRYPTED(hdr
) || HDR_SHARED_DATA(hdr
) || psize
!= asize
);
9280 ASSERT3U(psize
, <=, asize
);
9283 * If this data simply needs its own buffer, we simply allocate it
9284 * and copy the data. This may be done to eliminate a dependency on a
9285 * shared buffer or to reallocate the buffer to match asize.
9287 if (HDR_HAS_RABD(hdr
) && asize
!= psize
) {
9288 ASSERT3U(asize
, >=, psize
);
9289 to_write
= abd_alloc_for_io(asize
, ismd
);
9290 abd_copy(to_write
, hdr
->b_crypt_hdr
.b_rabd
, psize
);
9292 abd_zero_off(to_write
, psize
, asize
- psize
);
9296 if ((compress
== ZIO_COMPRESS_OFF
|| HDR_COMPRESSION_ENABLED(hdr
)) &&
9297 !HDR_ENCRYPTED(hdr
)) {
9298 ASSERT3U(size
, ==, psize
);
9299 to_write
= abd_alloc_for_io(asize
, ismd
);
9300 abd_copy(to_write
, hdr
->b_l1hdr
.b_pabd
, size
);
9302 abd_zero_off(to_write
, size
, asize
- size
);
9306 if (compress
!= ZIO_COMPRESS_OFF
&& !HDR_COMPRESSION_ENABLED(hdr
)) {
9307 cabd
= abd_alloc_for_io(asize
, ismd
);
9308 tmp
= abd_borrow_buf(cabd
, asize
);
9310 psize
= zio_compress_data(compress
, to_write
, tmp
, size
,
9313 if (psize
>= size
) {
9314 abd_return_buf(cabd
, tmp
, asize
);
9315 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_OFF
);
9317 abd_copy(to_write
, hdr
->b_l1hdr
.b_pabd
, size
);
9319 abd_zero_off(to_write
, size
, asize
- size
);
9322 ASSERT3U(psize
, <=, HDR_GET_PSIZE(hdr
));
9324 bzero((char *)tmp
+ psize
, asize
- psize
);
9325 psize
= HDR_GET_PSIZE(hdr
);
9326 abd_return_buf_copy(cabd
, tmp
, asize
);
9331 if (HDR_ENCRYPTED(hdr
)) {
9332 eabd
= abd_alloc_for_io(asize
, ismd
);
9335 * If the dataset was disowned before the buffer
9336 * made it to this point, the key to re-encrypt
9337 * it won't be available. In this case we simply
9338 * won't write the buffer to the L2ARC.
9340 ret
= spa_keystore_lookup_key(spa
, hdr
->b_crypt_hdr
.b_dsobj
,
9345 ret
= zio_do_crypt_abd(B_TRUE
, &dck
->dck_key
,
9346 hdr
->b_crypt_hdr
.b_ot
, bswap
, hdr
->b_crypt_hdr
.b_salt
,
9347 hdr
->b_crypt_hdr
.b_iv
, mac
, psize
, to_write
, eabd
,
9353 abd_copy(eabd
, to_write
, psize
);
9356 abd_zero_off(eabd
, psize
, asize
- psize
);
9358 /* assert that the MAC we got here matches the one we saved */
9359 ASSERT0(bcmp(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
));
9360 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
9362 if (to_write
== cabd
)
9369 ASSERT3P(to_write
, !=, hdr
->b_l1hdr
.b_pabd
);
9370 *abd_out
= to_write
;
9375 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
9386 l2arc_blk_fetch_done(zio_t
*zio
)
9388 l2arc_read_callback_t
*cb
;
9390 cb
= zio
->io_private
;
9391 if (cb
->l2rcb_abd
!= NULL
)
9392 abd_free(cb
->l2rcb_abd
);
9393 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
9397 * Find and write ARC buffers to the L2ARC device.
9399 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
9400 * for reading until they have completed writing.
9401 * The headroom_boost is an in-out parameter used to maintain headroom boost
9402 * state between calls to this function.
9404 * Returns the number of bytes actually written (which may be smaller than
9405 * the delta by which the device hand has changed due to alignment and the
9406 * writing of log blocks).
9409 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
)
9411 arc_buf_hdr_t
*hdr
, *hdr_prev
, *head
;
9412 uint64_t write_asize
, write_psize
, write_lsize
, headroom
;
9414 l2arc_write_callback_t
*cb
= NULL
;
9416 uint64_t guid
= spa_load_guid(spa
);
9417 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
9419 ASSERT3P(dev
->l2ad_vdev
, !=, NULL
);
9422 write_lsize
= write_asize
= write_psize
= 0;
9424 head
= kmem_cache_alloc(hdr_l2only_cache
, KM_PUSHPAGE
);
9425 arc_hdr_set_flags(head
, ARC_FLAG_L2_WRITE_HEAD
| ARC_FLAG_HAS_L2HDR
);
9428 * Copy buffers for L2ARC writing.
9430 for (int pass
= 0; pass
< L2ARC_FEED_TYPES
; pass
++) {
9432 * If pass == 1 or 3, we cache MRU metadata and data
9435 if (l2arc_mfuonly
) {
9436 if (pass
== 1 || pass
== 3)
9440 multilist_sublist_t
*mls
= l2arc_sublist_lock(pass
);
9441 uint64_t passed_sz
= 0;
9443 VERIFY3P(mls
, !=, NULL
);
9446 * L2ARC fast warmup.
9448 * Until the ARC is warm and starts to evict, read from the
9449 * head of the ARC lists rather than the tail.
9451 if (arc_warm
== B_FALSE
)
9452 hdr
= multilist_sublist_head(mls
);
9454 hdr
= multilist_sublist_tail(mls
);
9456 headroom
= target_sz
* l2arc_headroom
;
9457 if (zfs_compressed_arc_enabled
)
9458 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
9460 for (; hdr
; hdr
= hdr_prev
) {
9461 kmutex_t
*hash_lock
;
9462 abd_t
*to_write
= NULL
;
9464 if (arc_warm
== B_FALSE
)
9465 hdr_prev
= multilist_sublist_next(mls
, hdr
);
9467 hdr_prev
= multilist_sublist_prev(mls
, hdr
);
9469 hash_lock
= HDR_LOCK(hdr
);
9470 if (!mutex_tryenter(hash_lock
)) {
9472 * Skip this buffer rather than waiting.
9477 passed_sz
+= HDR_GET_LSIZE(hdr
);
9478 if (l2arc_headroom
!= 0 && passed_sz
> headroom
) {
9482 mutex_exit(hash_lock
);
9486 if (!l2arc_write_eligible(guid
, hdr
)) {
9487 mutex_exit(hash_lock
);
9491 ASSERT(HDR_HAS_L1HDR(hdr
));
9493 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
9494 ASSERT3U(arc_hdr_size(hdr
), >, 0);
9495 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
9497 uint64_t psize
= HDR_GET_PSIZE(hdr
);
9498 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
,
9501 if ((write_asize
+ asize
) > target_sz
) {
9503 mutex_exit(hash_lock
);
9508 * We rely on the L1 portion of the header below, so
9509 * it's invalid for this header to have been evicted out
9510 * of the ghost cache, prior to being written out. The
9511 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
9513 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_WRITING
);
9516 * If this header has b_rabd, we can use this since it
9517 * must always match the data exactly as it exists on
9518 * disk. Otherwise, the L2ARC can normally use the
9519 * hdr's data, but if we're sharing data between the
9520 * hdr and one of its bufs, L2ARC needs its own copy of
9521 * the data so that the ZIO below can't race with the
9522 * buf consumer. To ensure that this copy will be
9523 * available for the lifetime of the ZIO and be cleaned
9524 * up afterwards, we add it to the l2arc_free_on_write
9525 * queue. If we need to apply any transforms to the
9526 * data (compression, encryption) we will also need the
9529 if (HDR_HAS_RABD(hdr
) && psize
== asize
) {
9530 to_write
= hdr
->b_crypt_hdr
.b_rabd
;
9531 } else if ((HDR_COMPRESSION_ENABLED(hdr
) ||
9532 HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
) &&
9533 !HDR_ENCRYPTED(hdr
) && !HDR_SHARED_DATA(hdr
) &&
9535 to_write
= hdr
->b_l1hdr
.b_pabd
;
9538 arc_buf_contents_t type
= arc_buf_type(hdr
);
9540 ret
= l2arc_apply_transforms(spa
, hdr
, asize
,
9543 arc_hdr_clear_flags(hdr
,
9544 ARC_FLAG_L2_WRITING
);
9545 mutex_exit(hash_lock
);
9549 l2arc_free_abd_on_write(to_write
, asize
, type
);
9554 * Insert a dummy header on the buflist so
9555 * l2arc_write_done() can find where the
9556 * write buffers begin without searching.
9558 mutex_enter(&dev
->l2ad_mtx
);
9559 list_insert_head(&dev
->l2ad_buflist
, head
);
9560 mutex_exit(&dev
->l2ad_mtx
);
9563 sizeof (l2arc_write_callback_t
), KM_SLEEP
);
9564 cb
->l2wcb_dev
= dev
;
9565 cb
->l2wcb_head
= head
;
9567 * Create a list to save allocated abd buffers
9568 * for l2arc_log_blk_commit().
9570 list_create(&cb
->l2wcb_abd_list
,
9571 sizeof (l2arc_lb_abd_buf_t
),
9572 offsetof(l2arc_lb_abd_buf_t
, node
));
9573 pio
= zio_root(spa
, l2arc_write_done
, cb
,
9577 hdr
->b_l2hdr
.b_dev
= dev
;
9578 hdr
->b_l2hdr
.b_hits
= 0;
9580 hdr
->b_l2hdr
.b_daddr
= dev
->l2ad_hand
;
9581 hdr
->b_l2hdr
.b_arcs_state
=
9582 hdr
->b_l1hdr
.b_state
->arcs_state
;
9583 arc_hdr_set_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
9585 mutex_enter(&dev
->l2ad_mtx
);
9586 list_insert_head(&dev
->l2ad_buflist
, hdr
);
9587 mutex_exit(&dev
->l2ad_mtx
);
9589 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
9590 arc_hdr_size(hdr
), hdr
);
9592 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
9593 hdr
->b_l2hdr
.b_daddr
, asize
, to_write
,
9594 ZIO_CHECKSUM_OFF
, NULL
, hdr
,
9595 ZIO_PRIORITY_ASYNC_WRITE
,
9596 ZIO_FLAG_CANFAIL
, B_FALSE
);
9598 write_lsize
+= HDR_GET_LSIZE(hdr
);
9599 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
9602 write_psize
+= psize
;
9603 write_asize
+= asize
;
9604 dev
->l2ad_hand
+= asize
;
9605 l2arc_hdr_arcstats_increment(hdr
);
9606 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
9608 mutex_exit(hash_lock
);
9611 * Append buf info to current log and commit if full.
9612 * arcstat_l2_{size,asize} kstats are updated
9615 if (l2arc_log_blk_insert(dev
, hdr
))
9616 l2arc_log_blk_commit(dev
, pio
, cb
);
9621 multilist_sublist_unlock(mls
);
9627 /* No buffers selected for writing? */
9629 ASSERT0(write_lsize
);
9630 ASSERT(!HDR_HAS_L1HDR(head
));
9631 kmem_cache_free(hdr_l2only_cache
, head
);
9634 * Although we did not write any buffers l2ad_evict may
9637 if (dev
->l2ad_evict
!= l2dhdr
->dh_evict
)
9638 l2arc_dev_hdr_update(dev
);
9643 if (!dev
->l2ad_first
)
9644 ASSERT3U(dev
->l2ad_hand
, <=, dev
->l2ad_evict
);
9646 ASSERT3U(write_asize
, <=, target_sz
);
9647 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
9648 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_psize
);
9650 dev
->l2ad_writing
= B_TRUE
;
9651 (void) zio_wait(pio
);
9652 dev
->l2ad_writing
= B_FALSE
;
9655 * Update the device header after the zio completes as
9656 * l2arc_write_done() may have updated the memory holding the log block
9657 * pointers in the device header.
9659 l2arc_dev_hdr_update(dev
);
9661 return (write_asize
);
9665 l2arc_hdr_limit_reached(void)
9667 int64_t s
= aggsum_upper_bound(&arc_sums
.arcstat_l2_hdr_size
);
9669 return (arc_reclaim_needed() || (s
> arc_meta_limit
* 3 / 4) ||
9670 (s
> (arc_warm
? arc_c
: arc_c_max
) * l2arc_meta_percent
/ 100));
9674 * This thread feeds the L2ARC at regular intervals. This is the beating
9675 * heart of the L2ARC.
9678 l2arc_feed_thread(void *unused
)
9684 uint64_t size
, wrote
;
9685 clock_t begin
, next
= ddi_get_lbolt();
9686 fstrans_cookie_t cookie
;
9688 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
9690 mutex_enter(&l2arc_feed_thr_lock
);
9692 cookie
= spl_fstrans_mark();
9693 while (l2arc_thread_exit
== 0) {
9694 CALLB_CPR_SAFE_BEGIN(&cpr
);
9695 (void) cv_timedwait_idle(&l2arc_feed_thr_cv
,
9696 &l2arc_feed_thr_lock
, next
);
9697 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
9698 next
= ddi_get_lbolt() + hz
;
9701 * Quick check for L2ARC devices.
9703 mutex_enter(&l2arc_dev_mtx
);
9704 if (l2arc_ndev
== 0) {
9705 mutex_exit(&l2arc_dev_mtx
);
9708 mutex_exit(&l2arc_dev_mtx
);
9709 begin
= ddi_get_lbolt();
9712 * This selects the next l2arc device to write to, and in
9713 * doing so the next spa to feed from: dev->l2ad_spa. This
9714 * will return NULL if there are now no l2arc devices or if
9715 * they are all faulted.
9717 * If a device is returned, its spa's config lock is also
9718 * held to prevent device removal. l2arc_dev_get_next()
9719 * will grab and release l2arc_dev_mtx.
9721 if ((dev
= l2arc_dev_get_next()) == NULL
)
9724 spa
= dev
->l2ad_spa
;
9725 ASSERT3P(spa
, !=, NULL
);
9728 * If the pool is read-only then force the feed thread to
9729 * sleep a little longer.
9731 if (!spa_writeable(spa
)) {
9732 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
9733 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9738 * Avoid contributing to memory pressure.
9740 if (l2arc_hdr_limit_reached()) {
9741 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
9742 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9746 ARCSTAT_BUMP(arcstat_l2_feeds
);
9748 size
= l2arc_write_size(dev
);
9751 * Evict L2ARC buffers that will be overwritten.
9753 l2arc_evict(dev
, size
, B_FALSE
);
9756 * Write ARC buffers.
9758 wrote
= l2arc_write_buffers(spa
, dev
, size
);
9761 * Calculate interval between writes.
9763 next
= l2arc_write_interval(begin
, size
, wrote
);
9764 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9766 spl_fstrans_unmark(cookie
);
9768 l2arc_thread_exit
= 0;
9769 cv_broadcast(&l2arc_feed_thr_cv
);
9770 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
9775 l2arc_vdev_present(vdev_t
*vd
)
9777 return (l2arc_vdev_get(vd
) != NULL
);
9781 * Returns the l2arc_dev_t associated with a particular vdev_t or NULL if
9782 * the vdev_t isn't an L2ARC device.
9785 l2arc_vdev_get(vdev_t
*vd
)
9789 mutex_enter(&l2arc_dev_mtx
);
9790 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
9791 dev
= list_next(l2arc_dev_list
, dev
)) {
9792 if (dev
->l2ad_vdev
== vd
)
9795 mutex_exit(&l2arc_dev_mtx
);
9801 l2arc_rebuild_dev(l2arc_dev_t
*dev
, boolean_t reopen
)
9803 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
9804 uint64_t l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
9805 spa_t
*spa
= dev
->l2ad_spa
;
9808 * The L2ARC has to hold at least the payload of one log block for
9809 * them to be restored (persistent L2ARC). The payload of a log block
9810 * depends on the amount of its log entries. We always write log blocks
9811 * with 1022 entries. How many of them are committed or restored depends
9812 * on the size of the L2ARC device. Thus the maximum payload of
9813 * one log block is 1022 * SPA_MAXBLOCKSIZE = 16GB. If the L2ARC device
9814 * is less than that, we reduce the amount of committed and restored
9815 * log entries per block so as to enable persistence.
9817 if (dev
->l2ad_end
< l2arc_rebuild_blocks_min_l2size
) {
9818 dev
->l2ad_log_entries
= 0;
9820 dev
->l2ad_log_entries
= MIN((dev
->l2ad_end
-
9821 dev
->l2ad_start
) >> SPA_MAXBLOCKSHIFT
,
9822 L2ARC_LOG_BLK_MAX_ENTRIES
);
9826 * Read the device header, if an error is returned do not rebuild L2ARC.
9828 if (l2arc_dev_hdr_read(dev
) == 0 && dev
->l2ad_log_entries
> 0) {
9830 * If we are onlining a cache device (vdev_reopen) that was
9831 * still present (l2arc_vdev_present()) and rebuild is enabled,
9832 * we should evict all ARC buffers and pointers to log blocks
9833 * and reclaim their space before restoring its contents to
9837 if (!l2arc_rebuild_enabled
) {
9840 l2arc_evict(dev
, 0, B_TRUE
);
9841 /* start a new log block */
9842 dev
->l2ad_log_ent_idx
= 0;
9843 dev
->l2ad_log_blk_payload_asize
= 0;
9844 dev
->l2ad_log_blk_payload_start
= 0;
9848 * Just mark the device as pending for a rebuild. We won't
9849 * be starting a rebuild in line here as it would block pool
9850 * import. Instead spa_load_impl will hand that off to an
9851 * async task which will call l2arc_spa_rebuild_start.
9853 dev
->l2ad_rebuild
= B_TRUE
;
9854 } else if (spa_writeable(spa
)) {
9856 * In this case TRIM the whole device if l2arc_trim_ahead > 0,
9857 * otherwise create a new header. We zero out the memory holding
9858 * the header to reset dh_start_lbps. If we TRIM the whole
9859 * device the new header will be written by
9860 * vdev_trim_l2arc_thread() at the end of the TRIM to update the
9861 * trim_state in the header too. When reading the header, if
9862 * trim_state is not VDEV_TRIM_COMPLETE and l2arc_trim_ahead > 0
9863 * we opt to TRIM the whole device again.
9865 if (l2arc_trim_ahead
> 0) {
9866 dev
->l2ad_trim_all
= B_TRUE
;
9868 bzero(l2dhdr
, l2dhdr_asize
);
9869 l2arc_dev_hdr_update(dev
);
9875 * Add a vdev for use by the L2ARC. By this point the spa has already
9876 * validated the vdev and opened it.
9879 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
9881 l2arc_dev_t
*adddev
;
9882 uint64_t l2dhdr_asize
;
9884 ASSERT(!l2arc_vdev_present(vd
));
9887 * Create a new l2arc device entry.
9889 adddev
= vmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
9890 adddev
->l2ad_spa
= spa
;
9891 adddev
->l2ad_vdev
= vd
;
9892 /* leave extra size for an l2arc device header */
9893 l2dhdr_asize
= adddev
->l2ad_dev_hdr_asize
=
9894 MAX(sizeof (*adddev
->l2ad_dev_hdr
), 1 << vd
->vdev_ashift
);
9895 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
+ l2dhdr_asize
;
9896 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
9897 ASSERT3U(adddev
->l2ad_start
, <, adddev
->l2ad_end
);
9898 adddev
->l2ad_hand
= adddev
->l2ad_start
;
9899 adddev
->l2ad_evict
= adddev
->l2ad_start
;
9900 adddev
->l2ad_first
= B_TRUE
;
9901 adddev
->l2ad_writing
= B_FALSE
;
9902 adddev
->l2ad_trim_all
= B_FALSE
;
9903 list_link_init(&adddev
->l2ad_node
);
9904 adddev
->l2ad_dev_hdr
= kmem_zalloc(l2dhdr_asize
, KM_SLEEP
);
9906 mutex_init(&adddev
->l2ad_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9908 * This is a list of all ARC buffers that are still valid on the
9911 list_create(&adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
9912 offsetof(arc_buf_hdr_t
, b_l2hdr
.b_l2node
));
9915 * This is a list of pointers to log blocks that are still present
9918 list_create(&adddev
->l2ad_lbptr_list
, sizeof (l2arc_lb_ptr_buf_t
),
9919 offsetof(l2arc_lb_ptr_buf_t
, node
));
9921 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
9922 zfs_refcount_create(&adddev
->l2ad_alloc
);
9923 zfs_refcount_create(&adddev
->l2ad_lb_asize
);
9924 zfs_refcount_create(&adddev
->l2ad_lb_count
);
9927 * Decide if dev is eligible for L2ARC rebuild or whole device
9928 * trimming. This has to happen before the device is added in the
9929 * cache device list and l2arc_dev_mtx is released. Otherwise
9930 * l2arc_feed_thread() might already start writing on the
9933 l2arc_rebuild_dev(adddev
, B_FALSE
);
9936 * Add device to global list
9938 mutex_enter(&l2arc_dev_mtx
);
9939 list_insert_head(l2arc_dev_list
, adddev
);
9940 atomic_inc_64(&l2arc_ndev
);
9941 mutex_exit(&l2arc_dev_mtx
);
9945 * Decide if a vdev is eligible for L2ARC rebuild, called from vdev_reopen()
9946 * in case of onlining a cache device.
9949 l2arc_rebuild_vdev(vdev_t
*vd
, boolean_t reopen
)
9951 l2arc_dev_t
*dev
= NULL
;
9953 dev
= l2arc_vdev_get(vd
);
9954 ASSERT3P(dev
, !=, NULL
);
9957 * In contrast to l2arc_add_vdev() we do not have to worry about
9958 * l2arc_feed_thread() invalidating previous content when onlining a
9959 * cache device. The device parameters (l2ad*) are not cleared when
9960 * offlining the device and writing new buffers will not invalidate
9961 * all previous content. In worst case only buffers that have not had
9962 * their log block written to the device will be lost.
9963 * When onlining the cache device (ie offline->online without exporting
9964 * the pool in between) this happens:
9965 * vdev_reopen() -> vdev_open() -> l2arc_rebuild_vdev()
9967 * vdev_is_dead() = B_FALSE l2ad_rebuild = B_TRUE
9968 * During the time where vdev_is_dead = B_FALSE and until l2ad_rebuild
9969 * is set to B_TRUE we might write additional buffers to the device.
9971 l2arc_rebuild_dev(dev
, reopen
);
9975 * Remove a vdev from the L2ARC.
9978 l2arc_remove_vdev(vdev_t
*vd
)
9980 l2arc_dev_t
*remdev
= NULL
;
9983 * Find the device by vdev
9985 remdev
= l2arc_vdev_get(vd
);
9986 ASSERT3P(remdev
, !=, NULL
);
9989 * Cancel any ongoing or scheduled rebuild.
9991 mutex_enter(&l2arc_rebuild_thr_lock
);
9992 if (remdev
->l2ad_rebuild_began
== B_TRUE
) {
9993 remdev
->l2ad_rebuild_cancel
= B_TRUE
;
9994 while (remdev
->l2ad_rebuild
== B_TRUE
)
9995 cv_wait(&l2arc_rebuild_thr_cv
, &l2arc_rebuild_thr_lock
);
9997 mutex_exit(&l2arc_rebuild_thr_lock
);
10000 * Remove device from global list
10002 mutex_enter(&l2arc_dev_mtx
);
10003 list_remove(l2arc_dev_list
, remdev
);
10004 l2arc_dev_last
= NULL
; /* may have been invalidated */
10005 atomic_dec_64(&l2arc_ndev
);
10006 mutex_exit(&l2arc_dev_mtx
);
10009 * Clear all buflists and ARC references. L2ARC device flush.
10011 l2arc_evict(remdev
, 0, B_TRUE
);
10012 list_destroy(&remdev
->l2ad_buflist
);
10013 ASSERT(list_is_empty(&remdev
->l2ad_lbptr_list
));
10014 list_destroy(&remdev
->l2ad_lbptr_list
);
10015 mutex_destroy(&remdev
->l2ad_mtx
);
10016 zfs_refcount_destroy(&remdev
->l2ad_alloc
);
10017 zfs_refcount_destroy(&remdev
->l2ad_lb_asize
);
10018 zfs_refcount_destroy(&remdev
->l2ad_lb_count
);
10019 kmem_free(remdev
->l2ad_dev_hdr
, remdev
->l2ad_dev_hdr_asize
);
10020 vmem_free(remdev
, sizeof (l2arc_dev_t
));
10026 l2arc_thread_exit
= 0;
10029 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
10030 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
10031 mutex_init(&l2arc_rebuild_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
10032 cv_init(&l2arc_rebuild_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
10033 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
10034 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
10036 l2arc_dev_list
= &L2ARC_dev_list
;
10037 l2arc_free_on_write
= &L2ARC_free_on_write
;
10038 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
10039 offsetof(l2arc_dev_t
, l2ad_node
));
10040 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
10041 offsetof(l2arc_data_free_t
, l2df_list_node
));
10047 mutex_destroy(&l2arc_feed_thr_lock
);
10048 cv_destroy(&l2arc_feed_thr_cv
);
10049 mutex_destroy(&l2arc_rebuild_thr_lock
);
10050 cv_destroy(&l2arc_rebuild_thr_cv
);
10051 mutex_destroy(&l2arc_dev_mtx
);
10052 mutex_destroy(&l2arc_free_on_write_mtx
);
10054 list_destroy(l2arc_dev_list
);
10055 list_destroy(l2arc_free_on_write
);
10061 if (!(spa_mode_global
& SPA_MODE_WRITE
))
10064 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
10065 TS_RUN
, defclsyspri
);
10071 if (!(spa_mode_global
& SPA_MODE_WRITE
))
10074 mutex_enter(&l2arc_feed_thr_lock
);
10075 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
10076 l2arc_thread_exit
= 1;
10077 while (l2arc_thread_exit
!= 0)
10078 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
10079 mutex_exit(&l2arc_feed_thr_lock
);
10083 * Punches out rebuild threads for the L2ARC devices in a spa. This should
10084 * be called after pool import from the spa async thread, since starting
10085 * these threads directly from spa_import() will make them part of the
10086 * "zpool import" context and delay process exit (and thus pool import).
10089 l2arc_spa_rebuild_start(spa_t
*spa
)
10091 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
10094 * Locate the spa's l2arc devices and kick off rebuild threads.
10096 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
10098 l2arc_vdev_get(spa
->spa_l2cache
.sav_vdevs
[i
]);
10100 /* Don't attempt a rebuild if the vdev is UNAVAIL */
10103 mutex_enter(&l2arc_rebuild_thr_lock
);
10104 if (dev
->l2ad_rebuild
&& !dev
->l2ad_rebuild_cancel
) {
10105 dev
->l2ad_rebuild_began
= B_TRUE
;
10106 (void) thread_create(NULL
, 0, l2arc_dev_rebuild_thread
,
10107 dev
, 0, &p0
, TS_RUN
, minclsyspri
);
10109 mutex_exit(&l2arc_rebuild_thr_lock
);
10114 * Main entry point for L2ARC rebuilding.
10117 l2arc_dev_rebuild_thread(void *arg
)
10119 l2arc_dev_t
*dev
= arg
;
10121 VERIFY(!dev
->l2ad_rebuild_cancel
);
10122 VERIFY(dev
->l2ad_rebuild
);
10123 (void) l2arc_rebuild(dev
);
10124 mutex_enter(&l2arc_rebuild_thr_lock
);
10125 dev
->l2ad_rebuild_began
= B_FALSE
;
10126 dev
->l2ad_rebuild
= B_FALSE
;
10127 mutex_exit(&l2arc_rebuild_thr_lock
);
10133 * This function implements the actual L2ARC metadata rebuild. It:
10134 * starts reading the log block chain and restores each block's contents
10135 * to memory (reconstructing arc_buf_hdr_t's).
10137 * Operation stops under any of the following conditions:
10139 * 1) We reach the end of the log block chain.
10140 * 2) We encounter *any* error condition (cksum errors, io errors)
10143 l2arc_rebuild(l2arc_dev_t
*dev
)
10145 vdev_t
*vd
= dev
->l2ad_vdev
;
10146 spa_t
*spa
= vd
->vdev_spa
;
10148 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10149 l2arc_log_blk_phys_t
*this_lb
, *next_lb
;
10150 zio_t
*this_io
= NULL
, *next_io
= NULL
;
10151 l2arc_log_blkptr_t lbps
[2];
10152 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
10153 boolean_t lock_held
;
10155 this_lb
= vmem_zalloc(sizeof (*this_lb
), KM_SLEEP
);
10156 next_lb
= vmem_zalloc(sizeof (*next_lb
), KM_SLEEP
);
10159 * We prevent device removal while issuing reads to the device,
10160 * then during the rebuilding phases we drop this lock again so
10161 * that a spa_unload or device remove can be initiated - this is
10162 * safe, because the spa will signal us to stop before removing
10163 * our device and wait for us to stop.
10165 spa_config_enter(spa
, SCL_L2ARC
, vd
, RW_READER
);
10166 lock_held
= B_TRUE
;
10169 * Retrieve the persistent L2ARC device state.
10170 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10172 dev
->l2ad_evict
= MAX(l2dhdr
->dh_evict
, dev
->l2ad_start
);
10173 dev
->l2ad_hand
= MAX(l2dhdr
->dh_start_lbps
[0].lbp_daddr
+
10174 L2BLK_GET_PSIZE((&l2dhdr
->dh_start_lbps
[0])->lbp_prop
),
10176 dev
->l2ad_first
= !!(l2dhdr
->dh_flags
& L2ARC_DEV_HDR_EVICT_FIRST
);
10178 vd
->vdev_trim_action_time
= l2dhdr
->dh_trim_action_time
;
10179 vd
->vdev_trim_state
= l2dhdr
->dh_trim_state
;
10182 * In case the zfs module parameter l2arc_rebuild_enabled is false
10183 * we do not start the rebuild process.
10185 if (!l2arc_rebuild_enabled
)
10188 /* Prepare the rebuild process */
10189 bcopy(l2dhdr
->dh_start_lbps
, lbps
, sizeof (lbps
));
10191 /* Start the rebuild process */
10193 if (!l2arc_log_blkptr_valid(dev
, &lbps
[0]))
10196 if ((err
= l2arc_log_blk_read(dev
, &lbps
[0], &lbps
[1],
10197 this_lb
, next_lb
, this_io
, &next_io
)) != 0)
10201 * Our memory pressure valve. If the system is running low
10202 * on memory, rather than swamping memory with new ARC buf
10203 * hdrs, we opt not to rebuild the L2ARC. At this point,
10204 * however, we have already set up our L2ARC dev to chain in
10205 * new metadata log blocks, so the user may choose to offline/
10206 * online the L2ARC dev at a later time (or re-import the pool)
10207 * to reconstruct it (when there's less memory pressure).
10209 if (l2arc_hdr_limit_reached()) {
10210 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_lowmem
);
10211 cmn_err(CE_NOTE
, "System running low on memory, "
10212 "aborting L2ARC rebuild.");
10213 err
= SET_ERROR(ENOMEM
);
10217 spa_config_exit(spa
, SCL_L2ARC
, vd
);
10218 lock_held
= B_FALSE
;
10221 * Now that we know that the next_lb checks out alright, we
10222 * can start reconstruction from this log block.
10223 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10225 uint64_t asize
= L2BLK_GET_PSIZE((&lbps
[0])->lbp_prop
);
10226 l2arc_log_blk_restore(dev
, this_lb
, asize
);
10229 * log block restored, include its pointer in the list of
10230 * pointers to log blocks present in the L2ARC device.
10232 lb_ptr_buf
= kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t
), KM_SLEEP
);
10233 lb_ptr_buf
->lb_ptr
= kmem_zalloc(sizeof (l2arc_log_blkptr_t
),
10235 bcopy(&lbps
[0], lb_ptr_buf
->lb_ptr
,
10236 sizeof (l2arc_log_blkptr_t
));
10237 mutex_enter(&dev
->l2ad_mtx
);
10238 list_insert_tail(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
10239 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, asize
);
10240 ARCSTAT_BUMP(arcstat_l2_log_blk_count
);
10241 zfs_refcount_add_many(&dev
->l2ad_lb_asize
, asize
, lb_ptr_buf
);
10242 zfs_refcount_add(&dev
->l2ad_lb_count
, lb_ptr_buf
);
10243 mutex_exit(&dev
->l2ad_mtx
);
10244 vdev_space_update(vd
, asize
, 0, 0);
10247 * Protection against loops of log blocks:
10249 * l2ad_hand l2ad_evict
10251 * l2ad_start |=======================================| l2ad_end
10252 * -----|||----|||---|||----|||
10254 * ---|||---|||----|||---|||
10257 * In this situation the pointer of log block (4) passes
10258 * l2arc_log_blkptr_valid() but the log block should not be
10259 * restored as it is overwritten by the payload of log block
10260 * (0). Only log blocks (0)-(3) should be restored. We check
10261 * whether l2ad_evict lies in between the payload starting
10262 * offset of the next log block (lbps[1].lbp_payload_start)
10263 * and the payload starting offset of the present log block
10264 * (lbps[0].lbp_payload_start). If true and this isn't the
10265 * first pass, we are looping from the beginning and we should
10268 if (l2arc_range_check_overlap(lbps
[1].lbp_payload_start
,
10269 lbps
[0].lbp_payload_start
, dev
->l2ad_evict
) &&
10275 mutex_enter(&l2arc_rebuild_thr_lock
);
10276 if (dev
->l2ad_rebuild_cancel
) {
10277 dev
->l2ad_rebuild
= B_FALSE
;
10278 cv_signal(&l2arc_rebuild_thr_cv
);
10279 mutex_exit(&l2arc_rebuild_thr_lock
);
10280 err
= SET_ERROR(ECANCELED
);
10283 mutex_exit(&l2arc_rebuild_thr_lock
);
10284 if (spa_config_tryenter(spa
, SCL_L2ARC
, vd
,
10286 lock_held
= B_TRUE
;
10290 * L2ARC config lock held by somebody in writer,
10291 * possibly due to them trying to remove us. They'll
10292 * likely to want us to shut down, so after a little
10293 * delay, we check l2ad_rebuild_cancel and retry
10300 * Continue with the next log block.
10303 lbps
[1] = this_lb
->lb_prev_lbp
;
10304 PTR_SWAP(this_lb
, next_lb
);
10309 if (this_io
!= NULL
)
10310 l2arc_log_blk_fetch_abort(this_io
);
10312 if (next_io
!= NULL
)
10313 l2arc_log_blk_fetch_abort(next_io
);
10314 vmem_free(this_lb
, sizeof (*this_lb
));
10315 vmem_free(next_lb
, sizeof (*next_lb
));
10317 if (!l2arc_rebuild_enabled
) {
10318 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
10320 } else if (err
== 0 && zfs_refcount_count(&dev
->l2ad_lb_count
) > 0) {
10321 ARCSTAT_BUMP(arcstat_l2_rebuild_success
);
10322 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
10323 "successful, restored %llu blocks",
10324 (u_longlong_t
)zfs_refcount_count(&dev
->l2ad_lb_count
));
10325 } else if (err
== 0 && zfs_refcount_count(&dev
->l2ad_lb_count
) == 0) {
10327 * No error but also nothing restored, meaning the lbps array
10328 * in the device header points to invalid/non-present log
10329 * blocks. Reset the header.
10331 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
10332 "no valid log blocks");
10333 bzero(l2dhdr
, dev
->l2ad_dev_hdr_asize
);
10334 l2arc_dev_hdr_update(dev
);
10335 } else if (err
== ECANCELED
) {
10337 * In case the rebuild was canceled do not log to spa history
10338 * log as the pool may be in the process of being removed.
10340 zfs_dbgmsg("L2ARC rebuild aborted, restored %llu blocks",
10341 (u_longlong_t
)zfs_refcount_count(&dev
->l2ad_lb_count
));
10342 } else if (err
!= 0) {
10343 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
10344 "aborted, restored %llu blocks",
10345 (u_longlong_t
)zfs_refcount_count(&dev
->l2ad_lb_count
));
10349 spa_config_exit(spa
, SCL_L2ARC
, vd
);
10355 * Attempts to read the device header on the provided L2ARC device and writes
10356 * it to `hdr'. On success, this function returns 0, otherwise the appropriate
10357 * error code is returned.
10360 l2arc_dev_hdr_read(l2arc_dev_t
*dev
)
10364 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10365 const uint64_t l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
10368 guid
= spa_guid(dev
->l2ad_vdev
->vdev_spa
);
10370 abd
= abd_get_from_buf(l2dhdr
, l2dhdr_asize
);
10372 err
= zio_wait(zio_read_phys(NULL
, dev
->l2ad_vdev
,
10373 VDEV_LABEL_START_SIZE
, l2dhdr_asize
, abd
,
10374 ZIO_CHECKSUM_LABEL
, NULL
, NULL
, ZIO_PRIORITY_SYNC_READ
,
10375 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_CANFAIL
|
10376 ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
|
10377 ZIO_FLAG_SPECULATIVE
, B_FALSE
));
10382 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_dh_errors
);
10383 zfs_dbgmsg("L2ARC IO error (%d) while reading device header, "
10384 "vdev guid: %llu", err
,
10385 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
);
10389 if (l2dhdr
->dh_magic
== BSWAP_64(L2ARC_DEV_HDR_MAGIC
))
10390 byteswap_uint64_array(l2dhdr
, sizeof (*l2dhdr
));
10392 if (l2dhdr
->dh_magic
!= L2ARC_DEV_HDR_MAGIC
||
10393 l2dhdr
->dh_spa_guid
!= guid
||
10394 l2dhdr
->dh_vdev_guid
!= dev
->l2ad_vdev
->vdev_guid
||
10395 l2dhdr
->dh_version
!= L2ARC_PERSISTENT_VERSION
||
10396 l2dhdr
->dh_log_entries
!= dev
->l2ad_log_entries
||
10397 l2dhdr
->dh_end
!= dev
->l2ad_end
||
10398 !l2arc_range_check_overlap(dev
->l2ad_start
, dev
->l2ad_end
,
10399 l2dhdr
->dh_evict
) ||
10400 (l2dhdr
->dh_trim_state
!= VDEV_TRIM_COMPLETE
&&
10401 l2arc_trim_ahead
> 0)) {
10403 * Attempt to rebuild a device containing no actual dev hdr
10404 * or containing a header from some other pool or from another
10405 * version of persistent L2ARC.
10407 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_unsupported
);
10408 return (SET_ERROR(ENOTSUP
));
10415 * Reads L2ARC log blocks from storage and validates their contents.
10417 * This function implements a simple fetcher to make sure that while
10418 * we're processing one buffer the L2ARC is already fetching the next
10419 * one in the chain.
10421 * The arguments this_lp and next_lp point to the current and next log block
10422 * address in the block chain. Similarly, this_lb and next_lb hold the
10423 * l2arc_log_blk_phys_t's of the current and next L2ARC blk.
10425 * The `this_io' and `next_io' arguments are used for block fetching.
10426 * When issuing the first blk IO during rebuild, you should pass NULL for
10427 * `this_io'. This function will then issue a sync IO to read the block and
10428 * also issue an async IO to fetch the next block in the block chain. The
10429 * fetched IO is returned in `next_io'. On subsequent calls to this
10430 * function, pass the value returned in `next_io' from the previous call
10431 * as `this_io' and a fresh `next_io' pointer to hold the next fetch IO.
10432 * Prior to the call, you should initialize your `next_io' pointer to be
10433 * NULL. If no fetch IO was issued, the pointer is left set at NULL.
10435 * On success, this function returns 0, otherwise it returns an appropriate
10436 * error code. On error the fetching IO is aborted and cleared before
10437 * returning from this function. Therefore, if we return `success', the
10438 * caller can assume that we have taken care of cleanup of fetch IOs.
10441 l2arc_log_blk_read(l2arc_dev_t
*dev
,
10442 const l2arc_log_blkptr_t
*this_lbp
, const l2arc_log_blkptr_t
*next_lbp
,
10443 l2arc_log_blk_phys_t
*this_lb
, l2arc_log_blk_phys_t
*next_lb
,
10444 zio_t
*this_io
, zio_t
**next_io
)
10451 ASSERT(this_lbp
!= NULL
&& next_lbp
!= NULL
);
10452 ASSERT(this_lb
!= NULL
&& next_lb
!= NULL
);
10453 ASSERT(next_io
!= NULL
&& *next_io
== NULL
);
10454 ASSERT(l2arc_log_blkptr_valid(dev
, this_lbp
));
10457 * Check to see if we have issued the IO for this log block in a
10458 * previous run. If not, this is the first call, so issue it now.
10460 if (this_io
== NULL
) {
10461 this_io
= l2arc_log_blk_fetch(dev
->l2ad_vdev
, this_lbp
,
10466 * Peek to see if we can start issuing the next IO immediately.
10468 if (l2arc_log_blkptr_valid(dev
, next_lbp
)) {
10470 * Start issuing IO for the next log block early - this
10471 * should help keep the L2ARC device busy while we
10472 * decompress and restore this log block.
10474 *next_io
= l2arc_log_blk_fetch(dev
->l2ad_vdev
, next_lbp
,
10478 /* Wait for the IO to read this log block to complete */
10479 if ((err
= zio_wait(this_io
)) != 0) {
10480 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_io_errors
);
10481 zfs_dbgmsg("L2ARC IO error (%d) while reading log block, "
10482 "offset: %llu, vdev guid: %llu", err
,
10483 (u_longlong_t
)this_lbp
->lbp_daddr
,
10484 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
);
10489 * Make sure the buffer checks out.
10490 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10492 asize
= L2BLK_GET_PSIZE((this_lbp
)->lbp_prop
);
10493 fletcher_4_native(this_lb
, asize
, NULL
, &cksum
);
10494 if (!ZIO_CHECKSUM_EQUAL(cksum
, this_lbp
->lbp_cksum
)) {
10495 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_cksum_lb_errors
);
10496 zfs_dbgmsg("L2ARC log block cksum failed, offset: %llu, "
10497 "vdev guid: %llu, l2ad_hand: %llu, l2ad_evict: %llu",
10498 (u_longlong_t
)this_lbp
->lbp_daddr
,
10499 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
,
10500 (u_longlong_t
)dev
->l2ad_hand
,
10501 (u_longlong_t
)dev
->l2ad_evict
);
10502 err
= SET_ERROR(ECKSUM
);
10506 /* Now we can take our time decoding this buffer */
10507 switch (L2BLK_GET_COMPRESS((this_lbp
)->lbp_prop
)) {
10508 case ZIO_COMPRESS_OFF
:
10510 case ZIO_COMPRESS_LZ4
:
10511 abd
= abd_alloc_for_io(asize
, B_TRUE
);
10512 abd_copy_from_buf_off(abd
, this_lb
, 0, asize
);
10513 if ((err
= zio_decompress_data(
10514 L2BLK_GET_COMPRESS((this_lbp
)->lbp_prop
),
10515 abd
, this_lb
, asize
, sizeof (*this_lb
), NULL
)) != 0) {
10516 err
= SET_ERROR(EINVAL
);
10521 err
= SET_ERROR(EINVAL
);
10524 if (this_lb
->lb_magic
== BSWAP_64(L2ARC_LOG_BLK_MAGIC
))
10525 byteswap_uint64_array(this_lb
, sizeof (*this_lb
));
10526 if (this_lb
->lb_magic
!= L2ARC_LOG_BLK_MAGIC
) {
10527 err
= SET_ERROR(EINVAL
);
10531 /* Abort an in-flight fetch I/O in case of error */
10532 if (err
!= 0 && *next_io
!= NULL
) {
10533 l2arc_log_blk_fetch_abort(*next_io
);
10542 * Restores the payload of a log block to ARC. This creates empty ARC hdr
10543 * entries which only contain an l2arc hdr, essentially restoring the
10544 * buffers to their L2ARC evicted state. This function also updates space
10545 * usage on the L2ARC vdev to make sure it tracks restored buffers.
10548 l2arc_log_blk_restore(l2arc_dev_t
*dev
, const l2arc_log_blk_phys_t
*lb
,
10551 uint64_t size
= 0, asize
= 0;
10552 uint64_t log_entries
= dev
->l2ad_log_entries
;
10555 * Usually arc_adapt() is called only for data, not headers, but
10556 * since we may allocate significant amount of memory here, let ARC
10559 arc_adapt(log_entries
* HDR_L2ONLY_SIZE
, arc_l2c_only
);
10561 for (int i
= log_entries
- 1; i
>= 0; i
--) {
10563 * Restore goes in the reverse temporal direction to preserve
10564 * correct temporal ordering of buffers in the l2ad_buflist.
10565 * l2arc_hdr_restore also does a list_insert_tail instead of
10566 * list_insert_head on the l2ad_buflist:
10568 * LIST l2ad_buflist LIST
10569 * HEAD <------ (time) ------ TAIL
10570 * direction +-----+-----+-----+-----+-----+ direction
10571 * of l2arc <== | buf | buf | buf | buf | buf | ===> of rebuild
10572 * fill +-----+-----+-----+-----+-----+
10576 * l2arc_feed_thread l2arc_rebuild
10577 * will place new bufs here restores bufs here
10579 * During l2arc_rebuild() the device is not used by
10580 * l2arc_feed_thread() as dev->l2ad_rebuild is set to true.
10582 size
+= L2BLK_GET_LSIZE((&lb
->lb_entries
[i
])->le_prop
);
10583 asize
+= vdev_psize_to_asize(dev
->l2ad_vdev
,
10584 L2BLK_GET_PSIZE((&lb
->lb_entries
[i
])->le_prop
));
10585 l2arc_hdr_restore(&lb
->lb_entries
[i
], dev
);
10589 * Record rebuild stats:
10590 * size Logical size of restored buffers in the L2ARC
10591 * asize Aligned size of restored buffers in the L2ARC
10593 ARCSTAT_INCR(arcstat_l2_rebuild_size
, size
);
10594 ARCSTAT_INCR(arcstat_l2_rebuild_asize
, asize
);
10595 ARCSTAT_INCR(arcstat_l2_rebuild_bufs
, log_entries
);
10596 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize
, lb_asize
);
10597 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio
, asize
/ lb_asize
);
10598 ARCSTAT_BUMP(arcstat_l2_rebuild_log_blks
);
10602 * Restores a single ARC buf hdr from a log entry. The ARC buffer is put
10603 * into a state indicating that it has been evicted to L2ARC.
10606 l2arc_hdr_restore(const l2arc_log_ent_phys_t
*le
, l2arc_dev_t
*dev
)
10608 arc_buf_hdr_t
*hdr
, *exists
;
10609 kmutex_t
*hash_lock
;
10610 arc_buf_contents_t type
= L2BLK_GET_TYPE((le
)->le_prop
);
10614 * Do all the allocation before grabbing any locks, this lets us
10615 * sleep if memory is full and we don't have to deal with failed
10618 hdr
= arc_buf_alloc_l2only(L2BLK_GET_LSIZE((le
)->le_prop
), type
,
10619 dev
, le
->le_dva
, le
->le_daddr
,
10620 L2BLK_GET_PSIZE((le
)->le_prop
), le
->le_birth
,
10621 L2BLK_GET_COMPRESS((le
)->le_prop
), le
->le_complevel
,
10622 L2BLK_GET_PROTECTED((le
)->le_prop
),
10623 L2BLK_GET_PREFETCH((le
)->le_prop
),
10624 L2BLK_GET_STATE((le
)->le_prop
));
10625 asize
= vdev_psize_to_asize(dev
->l2ad_vdev
,
10626 L2BLK_GET_PSIZE((le
)->le_prop
));
10629 * vdev_space_update() has to be called before arc_hdr_destroy() to
10630 * avoid underflow since the latter also calls vdev_space_update().
10632 l2arc_hdr_arcstats_increment(hdr
);
10633 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10635 mutex_enter(&dev
->l2ad_mtx
);
10636 list_insert_tail(&dev
->l2ad_buflist
, hdr
);
10637 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
), hdr
);
10638 mutex_exit(&dev
->l2ad_mtx
);
10640 exists
= buf_hash_insert(hdr
, &hash_lock
);
10642 /* Buffer was already cached, no need to restore it. */
10643 arc_hdr_destroy(hdr
);
10645 * If the buffer is already cached, check whether it has
10646 * L2ARC metadata. If not, enter them and update the flag.
10647 * This is important is case of onlining a cache device, since
10648 * we previously evicted all L2ARC metadata from ARC.
10650 if (!HDR_HAS_L2HDR(exists
)) {
10651 arc_hdr_set_flags(exists
, ARC_FLAG_HAS_L2HDR
);
10652 exists
->b_l2hdr
.b_dev
= dev
;
10653 exists
->b_l2hdr
.b_daddr
= le
->le_daddr
;
10654 exists
->b_l2hdr
.b_arcs_state
=
10655 L2BLK_GET_STATE((le
)->le_prop
);
10656 mutex_enter(&dev
->l2ad_mtx
);
10657 list_insert_tail(&dev
->l2ad_buflist
, exists
);
10658 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
10659 arc_hdr_size(exists
), exists
);
10660 mutex_exit(&dev
->l2ad_mtx
);
10661 l2arc_hdr_arcstats_increment(exists
);
10662 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10664 ARCSTAT_BUMP(arcstat_l2_rebuild_bufs_precached
);
10667 mutex_exit(hash_lock
);
10671 * Starts an asynchronous read IO to read a log block. This is used in log
10672 * block reconstruction to start reading the next block before we are done
10673 * decoding and reconstructing the current block, to keep the l2arc device
10674 * nice and hot with read IO to process.
10675 * The returned zio will contain a newly allocated memory buffers for the IO
10676 * data which should then be freed by the caller once the zio is no longer
10677 * needed (i.e. due to it having completed). If you wish to abort this
10678 * zio, you should do so using l2arc_log_blk_fetch_abort, which takes
10679 * care of disposing of the allocated buffers correctly.
10682 l2arc_log_blk_fetch(vdev_t
*vd
, const l2arc_log_blkptr_t
*lbp
,
10683 l2arc_log_blk_phys_t
*lb
)
10687 l2arc_read_callback_t
*cb
;
10689 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
10690 asize
= L2BLK_GET_PSIZE((lbp
)->lbp_prop
);
10691 ASSERT(asize
<= sizeof (l2arc_log_blk_phys_t
));
10693 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
), KM_SLEEP
);
10694 cb
->l2rcb_abd
= abd_get_from_buf(lb
, asize
);
10695 pio
= zio_root(vd
->vdev_spa
, l2arc_blk_fetch_done
, cb
,
10696 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
|
10697 ZIO_FLAG_DONT_RETRY
);
10698 (void) zio_nowait(zio_read_phys(pio
, vd
, lbp
->lbp_daddr
, asize
,
10699 cb
->l2rcb_abd
, ZIO_CHECKSUM_OFF
, NULL
, NULL
,
10700 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_CANFAIL
|
10701 ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
, B_FALSE
));
10707 * Aborts a zio returned from l2arc_log_blk_fetch and frees the data
10708 * buffers allocated for it.
10711 l2arc_log_blk_fetch_abort(zio_t
*zio
)
10713 (void) zio_wait(zio
);
10717 * Creates a zio to update the device header on an l2arc device.
10720 l2arc_dev_hdr_update(l2arc_dev_t
*dev
)
10722 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10723 const uint64_t l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
10727 VERIFY(spa_config_held(dev
->l2ad_spa
, SCL_STATE_ALL
, RW_READER
));
10729 l2dhdr
->dh_magic
= L2ARC_DEV_HDR_MAGIC
;
10730 l2dhdr
->dh_version
= L2ARC_PERSISTENT_VERSION
;
10731 l2dhdr
->dh_spa_guid
= spa_guid(dev
->l2ad_vdev
->vdev_spa
);
10732 l2dhdr
->dh_vdev_guid
= dev
->l2ad_vdev
->vdev_guid
;
10733 l2dhdr
->dh_log_entries
= dev
->l2ad_log_entries
;
10734 l2dhdr
->dh_evict
= dev
->l2ad_evict
;
10735 l2dhdr
->dh_start
= dev
->l2ad_start
;
10736 l2dhdr
->dh_end
= dev
->l2ad_end
;
10737 l2dhdr
->dh_lb_asize
= zfs_refcount_count(&dev
->l2ad_lb_asize
);
10738 l2dhdr
->dh_lb_count
= zfs_refcount_count(&dev
->l2ad_lb_count
);
10739 l2dhdr
->dh_flags
= 0;
10740 l2dhdr
->dh_trim_action_time
= dev
->l2ad_vdev
->vdev_trim_action_time
;
10741 l2dhdr
->dh_trim_state
= dev
->l2ad_vdev
->vdev_trim_state
;
10742 if (dev
->l2ad_first
)
10743 l2dhdr
->dh_flags
|= L2ARC_DEV_HDR_EVICT_FIRST
;
10745 abd
= abd_get_from_buf(l2dhdr
, l2dhdr_asize
);
10747 err
= zio_wait(zio_write_phys(NULL
, dev
->l2ad_vdev
,
10748 VDEV_LABEL_START_SIZE
, l2dhdr_asize
, abd
, ZIO_CHECKSUM_LABEL
, NULL
,
10749 NULL
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_CANFAIL
, B_FALSE
));
10754 zfs_dbgmsg("L2ARC IO error (%d) while writing device header, "
10755 "vdev guid: %llu", err
,
10756 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
);
10761 * Commits a log block to the L2ARC device. This routine is invoked from
10762 * l2arc_write_buffers when the log block fills up.
10763 * This function allocates some memory to temporarily hold the serialized
10764 * buffer to be written. This is then released in l2arc_write_done.
10767 l2arc_log_blk_commit(l2arc_dev_t
*dev
, zio_t
*pio
, l2arc_write_callback_t
*cb
)
10769 l2arc_log_blk_phys_t
*lb
= &dev
->l2ad_log_blk
;
10770 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10771 uint64_t psize
, asize
;
10773 l2arc_lb_abd_buf_t
*abd_buf
;
10775 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
10777 VERIFY3S(dev
->l2ad_log_ent_idx
, ==, dev
->l2ad_log_entries
);
10779 tmpbuf
= zio_buf_alloc(sizeof (*lb
));
10780 abd_buf
= zio_buf_alloc(sizeof (*abd_buf
));
10781 abd_buf
->abd
= abd_get_from_buf(lb
, sizeof (*lb
));
10782 lb_ptr_buf
= kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t
), KM_SLEEP
);
10783 lb_ptr_buf
->lb_ptr
= kmem_zalloc(sizeof (l2arc_log_blkptr_t
), KM_SLEEP
);
10785 /* link the buffer into the block chain */
10786 lb
->lb_prev_lbp
= l2dhdr
->dh_start_lbps
[1];
10787 lb
->lb_magic
= L2ARC_LOG_BLK_MAGIC
;
10790 * l2arc_log_blk_commit() may be called multiple times during a single
10791 * l2arc_write_buffers() call. Save the allocated abd buffers in a list
10792 * so we can free them in l2arc_write_done() later on.
10794 list_insert_tail(&cb
->l2wcb_abd_list
, abd_buf
);
10796 /* try to compress the buffer */
10797 psize
= zio_compress_data(ZIO_COMPRESS_LZ4
,
10798 abd_buf
->abd
, tmpbuf
, sizeof (*lb
), 0);
10800 /* a log block is never entirely zero */
10801 ASSERT(psize
!= 0);
10802 asize
= vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
10803 ASSERT(asize
<= sizeof (*lb
));
10806 * Update the start log block pointer in the device header to point
10807 * to the log block we're about to write.
10809 l2dhdr
->dh_start_lbps
[1] = l2dhdr
->dh_start_lbps
[0];
10810 l2dhdr
->dh_start_lbps
[0].lbp_daddr
= dev
->l2ad_hand
;
10811 l2dhdr
->dh_start_lbps
[0].lbp_payload_asize
=
10812 dev
->l2ad_log_blk_payload_asize
;
10813 l2dhdr
->dh_start_lbps
[0].lbp_payload_start
=
10814 dev
->l2ad_log_blk_payload_start
;
10816 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
, sizeof (*lb
));
10818 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
, asize
);
10819 L2BLK_SET_CHECKSUM(
10820 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10821 ZIO_CHECKSUM_FLETCHER_4
);
10822 if (asize
< sizeof (*lb
)) {
10823 /* compression succeeded */
10824 bzero(tmpbuf
+ psize
, asize
- psize
);
10825 L2BLK_SET_COMPRESS(
10826 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10829 /* compression failed */
10830 bcopy(lb
, tmpbuf
, sizeof (*lb
));
10831 L2BLK_SET_COMPRESS(
10832 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10836 /* checksum what we're about to write */
10837 fletcher_4_native(tmpbuf
, asize
, NULL
,
10838 &l2dhdr
->dh_start_lbps
[0].lbp_cksum
);
10840 abd_free(abd_buf
->abd
);
10842 /* perform the write itself */
10843 abd_buf
->abd
= abd_get_from_buf(tmpbuf
, sizeof (*lb
));
10844 abd_take_ownership_of_buf(abd_buf
->abd
, B_TRUE
);
10845 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
, dev
->l2ad_hand
,
10846 asize
, abd_buf
->abd
, ZIO_CHECKSUM_OFF
, NULL
, NULL
,
10847 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_CANFAIL
, B_FALSE
);
10848 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
, zio_t
*, wzio
);
10849 (void) zio_nowait(wzio
);
10851 dev
->l2ad_hand
+= asize
;
10853 * Include the committed log block's pointer in the list of pointers
10854 * to log blocks present in the L2ARC device.
10856 bcopy(&l2dhdr
->dh_start_lbps
[0], lb_ptr_buf
->lb_ptr
,
10857 sizeof (l2arc_log_blkptr_t
));
10858 mutex_enter(&dev
->l2ad_mtx
);
10859 list_insert_head(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
10860 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, asize
);
10861 ARCSTAT_BUMP(arcstat_l2_log_blk_count
);
10862 zfs_refcount_add_many(&dev
->l2ad_lb_asize
, asize
, lb_ptr_buf
);
10863 zfs_refcount_add(&dev
->l2ad_lb_count
, lb_ptr_buf
);
10864 mutex_exit(&dev
->l2ad_mtx
);
10865 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10867 /* bump the kstats */
10868 ARCSTAT_INCR(arcstat_l2_write_bytes
, asize
);
10869 ARCSTAT_BUMP(arcstat_l2_log_blk_writes
);
10870 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize
, asize
);
10871 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio
,
10872 dev
->l2ad_log_blk_payload_asize
/ asize
);
10874 /* start a new log block */
10875 dev
->l2ad_log_ent_idx
= 0;
10876 dev
->l2ad_log_blk_payload_asize
= 0;
10877 dev
->l2ad_log_blk_payload_start
= 0;
10881 * Validates an L2ARC log block address to make sure that it can be read
10882 * from the provided L2ARC device.
10885 l2arc_log_blkptr_valid(l2arc_dev_t
*dev
, const l2arc_log_blkptr_t
*lbp
)
10887 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
10888 uint64_t asize
= L2BLK_GET_PSIZE((lbp
)->lbp_prop
);
10889 uint64_t end
= lbp
->lbp_daddr
+ asize
- 1;
10890 uint64_t start
= lbp
->lbp_payload_start
;
10891 boolean_t evicted
= B_FALSE
;
10894 * A log block is valid if all of the following conditions are true:
10895 * - it fits entirely (including its payload) between l2ad_start and
10897 * - it has a valid size
10898 * - neither the log block itself nor part of its payload was evicted
10899 * by l2arc_evict():
10901 * l2ad_hand l2ad_evict
10906 * l2ad_start ============================================ l2ad_end
10907 * --------------------------||||
10914 l2arc_range_check_overlap(start
, end
, dev
->l2ad_hand
) ||
10915 l2arc_range_check_overlap(start
, end
, dev
->l2ad_evict
) ||
10916 l2arc_range_check_overlap(dev
->l2ad_hand
, dev
->l2ad_evict
, start
) ||
10917 l2arc_range_check_overlap(dev
->l2ad_hand
, dev
->l2ad_evict
, end
);
10919 return (start
>= dev
->l2ad_start
&& end
<= dev
->l2ad_end
&&
10920 asize
> 0 && asize
<= sizeof (l2arc_log_blk_phys_t
) &&
10921 (!evicted
|| dev
->l2ad_first
));
10925 * Inserts ARC buffer header `hdr' into the current L2ARC log block on
10926 * the device. The buffer being inserted must be present in L2ARC.
10927 * Returns B_TRUE if the L2ARC log block is full and needs to be committed
10928 * to L2ARC, or B_FALSE if it still has room for more ARC buffers.
10931 l2arc_log_blk_insert(l2arc_dev_t
*dev
, const arc_buf_hdr_t
*hdr
)
10933 l2arc_log_blk_phys_t
*lb
= &dev
->l2ad_log_blk
;
10934 l2arc_log_ent_phys_t
*le
;
10936 if (dev
->l2ad_log_entries
== 0)
10939 int index
= dev
->l2ad_log_ent_idx
++;
10941 ASSERT3S(index
, <, dev
->l2ad_log_entries
);
10942 ASSERT(HDR_HAS_L2HDR(hdr
));
10944 le
= &lb
->lb_entries
[index
];
10945 bzero(le
, sizeof (*le
));
10946 le
->le_dva
= hdr
->b_dva
;
10947 le
->le_birth
= hdr
->b_birth
;
10948 le
->le_daddr
= hdr
->b_l2hdr
.b_daddr
;
10950 dev
->l2ad_log_blk_payload_start
= le
->le_daddr
;
10951 L2BLK_SET_LSIZE((le
)->le_prop
, HDR_GET_LSIZE(hdr
));
10952 L2BLK_SET_PSIZE((le
)->le_prop
, HDR_GET_PSIZE(hdr
));
10953 L2BLK_SET_COMPRESS((le
)->le_prop
, HDR_GET_COMPRESS(hdr
));
10954 le
->le_complevel
= hdr
->b_complevel
;
10955 L2BLK_SET_TYPE((le
)->le_prop
, hdr
->b_type
);
10956 L2BLK_SET_PROTECTED((le
)->le_prop
, !!(HDR_PROTECTED(hdr
)));
10957 L2BLK_SET_PREFETCH((le
)->le_prop
, !!(HDR_PREFETCH(hdr
)));
10958 L2BLK_SET_STATE((le
)->le_prop
, hdr
->b_l1hdr
.b_state
->arcs_state
);
10960 dev
->l2ad_log_blk_payload_asize
+= vdev_psize_to_asize(dev
->l2ad_vdev
,
10961 HDR_GET_PSIZE(hdr
));
10963 return (dev
->l2ad_log_ent_idx
== dev
->l2ad_log_entries
);
10967 * Checks whether a given L2ARC device address sits in a time-sequential
10968 * range. The trick here is that the L2ARC is a rotary buffer, so we can't
10969 * just do a range comparison, we need to handle the situation in which the
10970 * range wraps around the end of the L2ARC device. Arguments:
10971 * bottom -- Lower end of the range to check (written to earlier).
10972 * top -- Upper end of the range to check (written to later).
10973 * check -- The address for which we want to determine if it sits in
10974 * between the top and bottom.
10976 * The 3-way conditional below represents the following cases:
10978 * bottom < top : Sequentially ordered case:
10979 * <check>--------+-------------------+
10980 * | (overlap here?) |
10982 * |---------------<bottom>============<top>--------------|
10984 * bottom > top: Looped-around case:
10985 * <check>--------+------------------+
10986 * | (overlap here?) |
10988 * |===============<top>---------------<bottom>===========|
10991 * +---------------+---------<check>
10993 * top == bottom : Just a single address comparison.
10996 l2arc_range_check_overlap(uint64_t bottom
, uint64_t top
, uint64_t check
)
10999 return (bottom
<= check
&& check
<= top
);
11000 else if (bottom
> top
)
11001 return (check
<= top
|| bottom
<= check
);
11003 return (check
== top
);
11006 EXPORT_SYMBOL(arc_buf_size
);
11007 EXPORT_SYMBOL(arc_write
);
11008 EXPORT_SYMBOL(arc_read
);
11009 EXPORT_SYMBOL(arc_buf_info
);
11010 EXPORT_SYMBOL(arc_getbuf_func
);
11011 EXPORT_SYMBOL(arc_add_prune_callback
);
11012 EXPORT_SYMBOL(arc_remove_prune_callback
);
11014 /* BEGIN CSTYLED */
11015 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min
, param_set_arc_min
,
11016 param_get_long
, ZMOD_RW
, "Min arc size");
11018 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, max
, param_set_arc_max
,
11019 param_get_long
, ZMOD_RW
, "Max arc size");
11021 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, meta_limit
, param_set_arc_long
,
11022 param_get_long
, ZMOD_RW
, "Metadata limit for arc size");
11024 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, meta_limit_percent
,
11025 param_set_arc_long
, param_get_long
, ZMOD_RW
,
11026 "Percent of arc size for arc meta limit");
11028 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, meta_min
, param_set_arc_long
,
11029 param_get_long
, ZMOD_RW
, "Min arc metadata");
11031 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, meta_prune
, INT
, ZMOD_RW
,
11032 "Meta objects to scan for prune");
11034 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, meta_adjust_restarts
, INT
, ZMOD_RW
,
11035 "Limit number of restarts in arc_evict_meta");
11037 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, meta_strategy
, INT
, ZMOD_RW
,
11038 "Meta reclaim strategy");
11040 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, grow_retry
, param_set_arc_int
,
11041 param_get_int
, ZMOD_RW
, "Seconds before growing arc size");
11043 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, p_dampener_disable
, INT
, ZMOD_RW
,
11044 "Disable arc_p adapt dampener");
11046 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, shrink_shift
, param_set_arc_int
,
11047 param_get_int
, ZMOD_RW
, "log2(fraction of arc to reclaim)");
11049 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, pc_percent
, UINT
, ZMOD_RW
,
11050 "Percent of pagecache to reclaim arc to");
11052 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, p_min_shift
, param_set_arc_int
,
11053 param_get_int
, ZMOD_RW
, "arc_c shift to calc min/max arc_p");
11055 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, average_blocksize
, INT
, ZMOD_RD
,
11056 "Target average block size");
11058 ZFS_MODULE_PARAM(zfs
, zfs_
, compressed_arc_enabled
, INT
, ZMOD_RW
,
11059 "Disable compressed arc buffers");
11061 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min_prefetch_ms
, param_set_arc_int
,
11062 param_get_int
, ZMOD_RW
, "Min life of prefetch block in ms");
11064 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min_prescient_prefetch_ms
,
11065 param_set_arc_int
, param_get_int
, ZMOD_RW
,
11066 "Min life of prescient prefetched block in ms");
11068 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, write_max
, ULONG
, ZMOD_RW
,
11069 "Max write bytes per interval");
11071 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, write_boost
, ULONG
, ZMOD_RW
,
11072 "Extra write bytes during device warmup");
11074 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, headroom
, ULONG
, ZMOD_RW
,
11075 "Number of max device writes to precache");
11077 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, headroom_boost
, ULONG
, ZMOD_RW
,
11078 "Compressed l2arc_headroom multiplier");
11080 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, trim_ahead
, ULONG
, ZMOD_RW
,
11081 "TRIM ahead L2ARC write size multiplier");
11083 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_secs
, ULONG
, ZMOD_RW
,
11084 "Seconds between L2ARC writing");
11086 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_min_ms
, ULONG
, ZMOD_RW
,
11087 "Min feed interval in milliseconds");
11089 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, noprefetch
, INT
, ZMOD_RW
,
11090 "Skip caching prefetched buffers");
11092 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_again
, INT
, ZMOD_RW
,
11093 "Turbo L2ARC warmup");
11095 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, norw
, INT
, ZMOD_RW
,
11096 "No reads during writes");
11098 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, meta_percent
, INT
, ZMOD_RW
,
11099 "Percent of ARC size allowed for L2ARC-only headers");
11101 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, rebuild_enabled
, INT
, ZMOD_RW
,
11102 "Rebuild the L2ARC when importing a pool");
11104 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, rebuild_blocks_min_l2size
, ULONG
, ZMOD_RW
,
11105 "Min size in bytes to write rebuild log blocks in L2ARC");
11107 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, mfuonly
, INT
, ZMOD_RW
,
11108 "Cache only MFU data from ARC into L2ARC");
11110 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, exclude_special
, INT
, ZMOD_RW
,
11111 "If set to 1 exclude dbufs on special vdevs from being cached to "
11114 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, lotsfree_percent
, param_set_arc_int
,
11115 param_get_int
, ZMOD_RW
, "System free memory I/O throttle in bytes");
11117 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, sys_free
, param_set_arc_long
,
11118 param_get_long
, ZMOD_RW
, "System free memory target size in bytes");
11120 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, dnode_limit
, param_set_arc_long
,
11121 param_get_long
, ZMOD_RW
, "Minimum bytes of dnodes in arc");
11123 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, dnode_limit_percent
,
11124 param_set_arc_long
, param_get_long
, ZMOD_RW
,
11125 "Percent of ARC meta buffers for dnodes");
11127 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, dnode_reduce_percent
, ULONG
, ZMOD_RW
,
11128 "Percentage of excess dnodes to try to unpin");
11130 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, eviction_pct
, INT
, ZMOD_RW
,
11131 "When full, ARC allocation waits for eviction of this % of alloc size");
11133 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, evict_batch_limit
, INT
, ZMOD_RW
,
11134 "The number of headers to evict per sublist before moving to the next");
11136 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, prune_task_threads
, INT
, ZMOD_RW
,
11137 "Number of arc_prune threads");