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 https://opensource.org/licenses/CDDL-1.0.
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, 2024, Klara Inc.
30 * Copyright (c) 2019, Allan Jude
31 * Copyright (c) 2020, The FreeBSD Foundation [1]
32 * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
34 * [1] Portions of this software were developed by Allan Jude
35 * under sponsorship from the FreeBSD Foundation.
39 * DVA-based Adjustable Replacement Cache
41 * While much of the theory of operation used here is
42 * based on the self-tuning, low overhead replacement cache
43 * presented by Megiddo and Modha at FAST 2003, there are some
44 * significant differences:
46 * 1. The Megiddo and Modha model assumes any page is evictable.
47 * Pages in its cache cannot be "locked" into memory. This makes
48 * the eviction algorithm simple: evict the last page in the list.
49 * This also make the performance characteristics easy to reason
50 * about. Our cache is not so simple. At any given moment, some
51 * subset of the blocks in the cache are un-evictable because we
52 * have handed out a reference to them. Blocks are only evictable
53 * when there are no external references active. This makes
54 * eviction far more problematic: we choose to evict the evictable
55 * blocks that are the "lowest" in the list.
57 * There are times when it is not possible to evict the requested
58 * space. In these circumstances we are unable to adjust the cache
59 * size. To prevent the cache growing unbounded at these times we
60 * implement a "cache throttle" that slows the flow of new data
61 * into the cache until we can make space available.
63 * 2. The Megiddo and Modha model assumes a fixed cache size.
64 * Pages are evicted when the cache is full and there is a cache
65 * miss. Our model has a variable sized cache. It grows with
66 * high use, but also tries to react to memory pressure from the
67 * operating system: decreasing its size when system memory is
70 * 3. The Megiddo and Modha model assumes a fixed page size. All
71 * elements of the cache are therefore exactly the same size. So
72 * when adjusting the cache size following a cache miss, its simply
73 * a matter of choosing a single page to evict. In our model, we
74 * have variable sized cache blocks (ranging from 512 bytes to
75 * 128K bytes). We therefore choose a set of blocks to evict to make
76 * space for a cache miss that approximates as closely as possible
77 * the space used by the new block.
79 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
80 * by N. Megiddo & D. Modha, FAST 2003
86 * A new reference to a cache buffer can be obtained in two
87 * ways: 1) via a hash table lookup using the DVA as a key,
88 * or 2) via one of the ARC lists. The arc_read() interface
89 * uses method 1, while the internal ARC algorithms for
90 * adjusting the cache use method 2. We therefore provide two
91 * types of locks: 1) the hash table lock array, and 2) the
94 * Buffers do not have their own mutexes, rather they rely on the
95 * hash table mutexes for the bulk of their protection (i.e. most
96 * fields in the arc_buf_hdr_t are protected by these mutexes).
98 * buf_hash_find() returns the appropriate mutex (held) when it
99 * locates the requested buffer in the hash table. It returns
100 * NULL for the mutex if the buffer was not in the table.
102 * buf_hash_remove() expects the appropriate hash mutex to be
103 * already held before it is invoked.
105 * Each ARC state also has a mutex which is used to protect the
106 * buffer list associated with the state. When attempting to
107 * obtain a hash table lock while holding an ARC list lock you
108 * must use: mutex_tryenter() to avoid deadlock. Also note that
109 * the active state mutex must be held before the ghost state mutex.
111 * It as also possible to register a callback which is run when the
112 * metadata limit is reached and no buffers can be safely evicted. In
113 * this case the arc user should drop a reference on some arc buffers so
114 * they can be reclaimed. For example, when using the ZPL each dentry
115 * holds a references on a znode. These dentries must be pruned before
116 * the arc buffer holding the znode can be safely evicted.
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 memcpy the transformed on-disk block into
254 * a newly allocated b_pabd. Writes are always done into buffers which have
255 * either been loaned (and hence are new and don't have other readers) or
256 * buffers which have been released (and hence have their own hdr, if there
257 * were originally other readers of the buf's original hdr). This ensures that
258 * the ARC only needs to update a single buf and its hdr after a write occurs.
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
;
331 static arc_buf_hdr_t
**arc_state_evict_markers
;
332 static int arc_state_evict_marker_count
;
334 static kmutex_t arc_evict_lock
;
335 static boolean_t arc_evict_needed
= B_FALSE
;
336 static clock_t arc_last_uncached_flush
;
339 * Count of bytes evicted since boot.
341 static uint64_t arc_evict_count
;
344 * List of arc_evict_waiter_t's, representing threads waiting for the
345 * arc_evict_count to reach specific values.
347 static list_t arc_evict_waiters
;
350 * When arc_is_overflowing(), arc_get_data_impl() waits for this percent of
351 * the requested amount of data to be evicted. For example, by default for
352 * every 2KB that's evicted, 1KB of it may be "reused" by a new allocation.
353 * Since this is above 100%, it ensures that progress is made towards getting
354 * arc_size under arc_c. Since this is finite, it ensures that allocations
355 * can still happen, even during the potentially long time that arc_size is
358 static uint_t zfs_arc_eviction_pct
= 200;
361 * The number of headers to evict in arc_evict_state_impl() before
362 * dropping the sublist lock and evicting from another sublist. A lower
363 * value means we're more likely to evict the "correct" header (i.e. the
364 * oldest header in the arc state), but comes with higher overhead
365 * (i.e. more invocations of arc_evict_state_impl()).
367 static uint_t zfs_arc_evict_batch_limit
= 10;
369 /* number of seconds before growing cache again */
370 uint_t arc_grow_retry
= 5;
373 * Minimum time between calls to arc_kmem_reap_soon().
375 static const int arc_kmem_cache_reap_retry_ms
= 1000;
377 /* shift of arc_c for calculating overflow limit in arc_get_data_impl */
378 static int zfs_arc_overflow_shift
= 8;
380 /* log2(fraction of arc to reclaim) */
381 uint_t 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 uint_t arc_no_grow_shift
= 5;
401 * minimum lifespan of a prefetch block in clock ticks
402 * (initialized in arc_init())
404 static uint_t arc_min_prefetch_ms
;
405 static uint_t arc_min_prescient_prefetch_ms
;
408 * If this percent of memory is free, don't throttle.
410 uint_t arc_lotsfree_percent
= 10;
413 * The arc has filled available memory and has now warmed up.
418 * These tunables are for performance analysis.
420 uint64_t zfs_arc_max
= 0;
421 uint64_t zfs_arc_min
= 0;
422 static uint64_t zfs_arc_dnode_limit
= 0;
423 static uint_t zfs_arc_dnode_reduce_percent
= 10;
424 static uint_t zfs_arc_grow_retry
= 0;
425 static uint_t zfs_arc_shrink_shift
= 0;
426 uint_t zfs_arc_average_blocksize
= 8 * 1024; /* 8KB */
429 * ARC dirty data constraints for arc_tempreserve_space() throttle:
430 * * total dirty data limit
431 * * anon block dirty limit
432 * * each pool's anon allowance
434 static const unsigned long zfs_arc_dirty_limit_percent
= 50;
435 static const unsigned long zfs_arc_anon_limit_percent
= 25;
436 static const unsigned long zfs_arc_pool_dirty_percent
= 20;
439 * Enable or disable compressed arc buffers.
441 int zfs_compressed_arc_enabled
= B_TRUE
;
444 * Balance between metadata and data on ghost hits. Values above 100
445 * increase metadata caching by proportionally reducing effect of ghost
446 * data hits on target data/metadata rate.
448 static uint_t zfs_arc_meta_balance
= 500;
451 * Percentage that can be consumed by dnodes of ARC meta buffers.
453 static uint_t zfs_arc_dnode_limit_percent
= 10;
456 * These tunables are Linux-specific
458 static uint64_t zfs_arc_sys_free
= 0;
459 static uint_t zfs_arc_min_prefetch_ms
= 0;
460 static uint_t zfs_arc_min_prescient_prefetch_ms
= 0;
461 static uint_t zfs_arc_lotsfree_percent
= 10;
464 * Number of arc_prune threads
466 static int zfs_arc_prune_task_threads
= 1;
468 /* Used by spa_export/spa_destroy to flush the arc asynchronously */
469 static taskq_t
*arc_flush_taskq
;
472 arc_state_t ARC_anon
;
474 arc_state_t ARC_mru_ghost
;
476 arc_state_t ARC_mfu_ghost
;
477 arc_state_t ARC_l2c_only
;
478 arc_state_t ARC_uncached
;
480 arc_stats_t arc_stats
= {
481 { "hits", KSTAT_DATA_UINT64
},
482 { "iohits", KSTAT_DATA_UINT64
},
483 { "misses", KSTAT_DATA_UINT64
},
484 { "demand_data_hits", KSTAT_DATA_UINT64
},
485 { "demand_data_iohits", KSTAT_DATA_UINT64
},
486 { "demand_data_misses", KSTAT_DATA_UINT64
},
487 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
488 { "demand_metadata_iohits", KSTAT_DATA_UINT64
},
489 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
490 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
491 { "prefetch_data_iohits", KSTAT_DATA_UINT64
},
492 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
493 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
494 { "prefetch_metadata_iohits", KSTAT_DATA_UINT64
},
495 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
496 { "mru_hits", KSTAT_DATA_UINT64
},
497 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
498 { "mfu_hits", KSTAT_DATA_UINT64
},
499 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
500 { "uncached_hits", KSTAT_DATA_UINT64
},
501 { "deleted", KSTAT_DATA_UINT64
},
502 { "mutex_miss", KSTAT_DATA_UINT64
},
503 { "access_skip", KSTAT_DATA_UINT64
},
504 { "evict_skip", KSTAT_DATA_UINT64
},
505 { "evict_not_enough", KSTAT_DATA_UINT64
},
506 { "evict_l2_cached", KSTAT_DATA_UINT64
},
507 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
508 { "evict_l2_eligible_mfu", KSTAT_DATA_UINT64
},
509 { "evict_l2_eligible_mru", KSTAT_DATA_UINT64
},
510 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
511 { "evict_l2_skip", KSTAT_DATA_UINT64
},
512 { "hash_elements", KSTAT_DATA_UINT64
},
513 { "hash_elements_max", KSTAT_DATA_UINT64
},
514 { "hash_collisions", KSTAT_DATA_UINT64
},
515 { "hash_chains", KSTAT_DATA_UINT64
},
516 { "hash_chain_max", KSTAT_DATA_UINT64
},
517 { "meta", KSTAT_DATA_UINT64
},
518 { "pd", KSTAT_DATA_UINT64
},
519 { "pm", KSTAT_DATA_UINT64
},
520 { "c", KSTAT_DATA_UINT64
},
521 { "c_min", KSTAT_DATA_UINT64
},
522 { "c_max", KSTAT_DATA_UINT64
},
523 { "size", KSTAT_DATA_UINT64
},
524 { "compressed_size", KSTAT_DATA_UINT64
},
525 { "uncompressed_size", KSTAT_DATA_UINT64
},
526 { "overhead_size", KSTAT_DATA_UINT64
},
527 { "hdr_size", KSTAT_DATA_UINT64
},
528 { "data_size", KSTAT_DATA_UINT64
},
529 { "metadata_size", KSTAT_DATA_UINT64
},
530 { "dbuf_size", KSTAT_DATA_UINT64
},
531 { "dnode_size", KSTAT_DATA_UINT64
},
532 { "bonus_size", KSTAT_DATA_UINT64
},
533 #if defined(COMPAT_FREEBSD11)
534 { "other_size", KSTAT_DATA_UINT64
},
536 { "anon_size", KSTAT_DATA_UINT64
},
537 { "anon_data", KSTAT_DATA_UINT64
},
538 { "anon_metadata", KSTAT_DATA_UINT64
},
539 { "anon_evictable_data", KSTAT_DATA_UINT64
},
540 { "anon_evictable_metadata", KSTAT_DATA_UINT64
},
541 { "mru_size", KSTAT_DATA_UINT64
},
542 { "mru_data", KSTAT_DATA_UINT64
},
543 { "mru_metadata", KSTAT_DATA_UINT64
},
544 { "mru_evictable_data", KSTAT_DATA_UINT64
},
545 { "mru_evictable_metadata", KSTAT_DATA_UINT64
},
546 { "mru_ghost_size", KSTAT_DATA_UINT64
},
547 { "mru_ghost_data", KSTAT_DATA_UINT64
},
548 { "mru_ghost_metadata", KSTAT_DATA_UINT64
},
549 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64
},
550 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
551 { "mfu_size", KSTAT_DATA_UINT64
},
552 { "mfu_data", KSTAT_DATA_UINT64
},
553 { "mfu_metadata", KSTAT_DATA_UINT64
},
554 { "mfu_evictable_data", KSTAT_DATA_UINT64
},
555 { "mfu_evictable_metadata", KSTAT_DATA_UINT64
},
556 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
557 { "mfu_ghost_data", KSTAT_DATA_UINT64
},
558 { "mfu_ghost_metadata", KSTAT_DATA_UINT64
},
559 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64
},
560 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
561 { "uncached_size", KSTAT_DATA_UINT64
},
562 { "uncached_data", KSTAT_DATA_UINT64
},
563 { "uncached_metadata", KSTAT_DATA_UINT64
},
564 { "uncached_evictable_data", KSTAT_DATA_UINT64
},
565 { "uncached_evictable_metadata", KSTAT_DATA_UINT64
},
566 { "l2_hits", KSTAT_DATA_UINT64
},
567 { "l2_misses", KSTAT_DATA_UINT64
},
568 { "l2_prefetch_asize", KSTAT_DATA_UINT64
},
569 { "l2_mru_asize", KSTAT_DATA_UINT64
},
570 { "l2_mfu_asize", KSTAT_DATA_UINT64
},
571 { "l2_bufc_data_asize", KSTAT_DATA_UINT64
},
572 { "l2_bufc_metadata_asize", KSTAT_DATA_UINT64
},
573 { "l2_feeds", KSTAT_DATA_UINT64
},
574 { "l2_rw_clash", KSTAT_DATA_UINT64
},
575 { "l2_read_bytes", KSTAT_DATA_UINT64
},
576 { "l2_write_bytes", KSTAT_DATA_UINT64
},
577 { "l2_writes_sent", KSTAT_DATA_UINT64
},
578 { "l2_writes_done", KSTAT_DATA_UINT64
},
579 { "l2_writes_error", KSTAT_DATA_UINT64
},
580 { "l2_writes_lock_retry", KSTAT_DATA_UINT64
},
581 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
582 { "l2_evict_reading", KSTAT_DATA_UINT64
},
583 { "l2_evict_l1cached", KSTAT_DATA_UINT64
},
584 { "l2_free_on_write", KSTAT_DATA_UINT64
},
585 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
586 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
587 { "l2_io_error", KSTAT_DATA_UINT64
},
588 { "l2_size", KSTAT_DATA_UINT64
},
589 { "l2_asize", KSTAT_DATA_UINT64
},
590 { "l2_hdr_size", KSTAT_DATA_UINT64
},
591 { "l2_log_blk_writes", KSTAT_DATA_UINT64
},
592 { "l2_log_blk_avg_asize", KSTAT_DATA_UINT64
},
593 { "l2_log_blk_asize", KSTAT_DATA_UINT64
},
594 { "l2_log_blk_count", KSTAT_DATA_UINT64
},
595 { "l2_data_to_meta_ratio", KSTAT_DATA_UINT64
},
596 { "l2_rebuild_success", KSTAT_DATA_UINT64
},
597 { "l2_rebuild_unsupported", KSTAT_DATA_UINT64
},
598 { "l2_rebuild_io_errors", KSTAT_DATA_UINT64
},
599 { "l2_rebuild_dh_errors", KSTAT_DATA_UINT64
},
600 { "l2_rebuild_cksum_lb_errors", KSTAT_DATA_UINT64
},
601 { "l2_rebuild_lowmem", KSTAT_DATA_UINT64
},
602 { "l2_rebuild_size", KSTAT_DATA_UINT64
},
603 { "l2_rebuild_asize", KSTAT_DATA_UINT64
},
604 { "l2_rebuild_bufs", KSTAT_DATA_UINT64
},
605 { "l2_rebuild_bufs_precached", KSTAT_DATA_UINT64
},
606 { "l2_rebuild_log_blks", KSTAT_DATA_UINT64
},
607 { "memory_throttle_count", KSTAT_DATA_UINT64
},
608 { "memory_direct_count", KSTAT_DATA_UINT64
},
609 { "memory_indirect_count", KSTAT_DATA_UINT64
},
610 { "memory_all_bytes", KSTAT_DATA_UINT64
},
611 { "memory_free_bytes", KSTAT_DATA_UINT64
},
612 { "memory_available_bytes", KSTAT_DATA_INT64
},
613 { "arc_no_grow", KSTAT_DATA_UINT64
},
614 { "arc_tempreserve", KSTAT_DATA_UINT64
},
615 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
616 { "arc_prune", KSTAT_DATA_UINT64
},
617 { "arc_meta_used", KSTAT_DATA_UINT64
},
618 { "arc_dnode_limit", KSTAT_DATA_UINT64
},
619 { "async_upgrade_sync", KSTAT_DATA_UINT64
},
620 { "predictive_prefetch", KSTAT_DATA_UINT64
},
621 { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64
},
622 { "demand_iohit_predictive_prefetch", KSTAT_DATA_UINT64
},
623 { "prescient_prefetch", KSTAT_DATA_UINT64
},
624 { "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64
},
625 { "demand_iohit_prescient_prefetch", KSTAT_DATA_UINT64
},
626 { "arc_need_free", KSTAT_DATA_UINT64
},
627 { "arc_sys_free", KSTAT_DATA_UINT64
},
628 { "arc_raw_size", KSTAT_DATA_UINT64
},
629 { "cached_only_in_progress", KSTAT_DATA_UINT64
},
630 { "abd_chunk_waste_size", KSTAT_DATA_UINT64
},
635 #define ARCSTAT_MAX(stat, val) { \
637 while ((val) > (m = arc_stats.stat.value.ui64) && \
638 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
643 * We define a macro to allow ARC hits/misses to be easily broken down by
644 * two separate conditions, giving a total of four different subtypes for
645 * each of hits and misses (so eight statistics total).
647 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
650 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
652 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
656 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
658 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
663 * This macro allows us to use kstats as floating averages. Each time we
664 * update this kstat, we first factor it and the update value by
665 * ARCSTAT_AVG_FACTOR to shrink the new value's contribution to the overall
666 * average. This macro assumes that integer loads and stores are atomic, but
667 * is not safe for multiple writers updating the kstat in parallel (only the
668 * last writer's update will remain).
670 #define ARCSTAT_F_AVG_FACTOR 3
671 #define ARCSTAT_F_AVG(stat, value) \
673 uint64_t x = ARCSTAT(stat); \
674 x = x - x / ARCSTAT_F_AVG_FACTOR + \
675 (value) / ARCSTAT_F_AVG_FACTOR; \
679 static kstat_t
*arc_ksp
;
682 * There are several ARC variables that are critical to export as kstats --
683 * but we don't want to have to grovel around in the kstat whenever we wish to
684 * manipulate them. For these variables, we therefore define them to be in
685 * terms of the statistic variable. This assures that we are not introducing
686 * the possibility of inconsistency by having shadow copies of the variables,
687 * while still allowing the code to be readable.
689 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
690 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
691 #define arc_dnode_limit ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */
692 #define arc_need_free ARCSTAT(arcstat_need_free) /* waiting to be evicted */
694 hrtime_t arc_growtime
;
695 list_t arc_prune_list
;
696 kmutex_t arc_prune_mtx
;
697 taskq_t
*arc_prune_taskq
;
699 #define GHOST_STATE(state) \
700 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
701 (state) == arc_l2c_only)
703 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
704 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
705 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
706 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
707 #define HDR_PRESCIENT_PREFETCH(hdr) \
708 ((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH)
709 #define HDR_COMPRESSION_ENABLED(hdr) \
710 ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC)
712 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
713 #define HDR_UNCACHED(hdr) ((hdr)->b_flags & ARC_FLAG_UNCACHED)
714 #define HDR_L2_READING(hdr) \
715 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
716 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
717 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
718 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
719 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
720 #define HDR_PROTECTED(hdr) ((hdr)->b_flags & ARC_FLAG_PROTECTED)
721 #define HDR_NOAUTH(hdr) ((hdr)->b_flags & ARC_FLAG_NOAUTH)
722 #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA)
724 #define HDR_ISTYPE_METADATA(hdr) \
725 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
726 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
728 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
729 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
730 #define HDR_HAS_RABD(hdr) \
731 (HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) && \
732 (hdr)->b_crypt_hdr.b_rabd != NULL)
733 #define HDR_ENCRYPTED(hdr) \
734 (HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
735 #define HDR_AUTHENTICATED(hdr) \
736 (HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
738 /* For storing compression mode in b_flags */
739 #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1)
741 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \
742 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS))
743 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \
744 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp));
746 #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL)
747 #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED)
748 #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED)
749 #define ARC_BUF_ENCRYPTED(buf) ((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED)
755 #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
756 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
759 * Hash table routines
762 #define BUF_LOCKS 2048
763 typedef struct buf_hash_table
{
765 arc_buf_hdr_t
**ht_table
;
766 kmutex_t ht_locks
[BUF_LOCKS
] ____cacheline_aligned
;
769 static buf_hash_table_t buf_hash_table
;
771 #define BUF_HASH_INDEX(spa, dva, birth) \
772 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
773 #define BUF_HASH_LOCK(idx) (&buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
774 #define HDR_LOCK(hdr) \
775 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
777 uint64_t zfs_crc64_table
[256];
780 * Asynchronous ARC flush
782 * We track these in a list for arc_async_flush_guid_inuse().
783 * Used for both L1 and L2 async teardown.
785 static list_t arc_async_flush_list
;
786 static kmutex_t arc_async_flush_lock
;
788 typedef struct arc_async_flush
{
789 uint64_t af_spa_guid
;
790 taskq_ent_t af_tqent
;
791 uint_t af_cache_level
; /* 1 or 2 to differentiate node */
800 #define L2ARC_WRITE_SIZE (32 * 1024 * 1024) /* initial write max */
801 #define L2ARC_HEADROOM 8 /* num of writes */
804 * If we discover during ARC scan any buffers to be compressed, we boost
805 * our headroom for the next scanning cycle by this percentage multiple.
807 #define L2ARC_HEADROOM_BOOST 200
808 #define L2ARC_FEED_SECS 1 /* caching interval secs */
809 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
812 * We can feed L2ARC from two states of ARC buffers, mru and mfu,
813 * and each of the state has two types: data and metadata.
815 #define L2ARC_FEED_TYPES 4
817 /* L2ARC Performance Tunables */
818 uint64_t l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
819 uint64_t l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
820 uint64_t l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
821 uint64_t l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
822 uint64_t l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
823 uint64_t l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
824 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
825 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
826 int l2arc_norw
= B_FALSE
; /* no reads during writes */
827 static uint_t l2arc_meta_percent
= 33; /* limit on headers size */
832 static list_t L2ARC_dev_list
; /* device list */
833 static list_t
*l2arc_dev_list
; /* device list pointer */
834 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
835 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
836 static list_t L2ARC_free_on_write
; /* free after write buf list */
837 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
838 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
839 static uint64_t l2arc_ndev
; /* number of devices */
841 typedef struct l2arc_read_callback
{
842 arc_buf_hdr_t
*l2rcb_hdr
; /* read header */
843 blkptr_t l2rcb_bp
; /* original blkptr */
844 zbookmark_phys_t l2rcb_zb
; /* original bookmark */
845 int l2rcb_flags
; /* original flags */
846 abd_t
*l2rcb_abd
; /* temporary buffer */
847 } l2arc_read_callback_t
;
849 typedef struct l2arc_data_free
{
850 /* protected by l2arc_free_on_write_mtx */
853 arc_buf_contents_t l2df_type
;
854 list_node_t l2df_list_node
;
857 typedef enum arc_fill_flags
{
858 ARC_FILL_LOCKED
= 1 << 0, /* hdr lock is held */
859 ARC_FILL_COMPRESSED
= 1 << 1, /* fill with compressed data */
860 ARC_FILL_ENCRYPTED
= 1 << 2, /* fill with encrypted data */
861 ARC_FILL_NOAUTH
= 1 << 3, /* don't attempt to authenticate */
862 ARC_FILL_IN_PLACE
= 1 << 4 /* fill in place (special case) */
865 typedef enum arc_ovf_level
{
866 ARC_OVF_NONE
, /* ARC within target size. */
867 ARC_OVF_SOME
, /* ARC is slightly overflowed. */
868 ARC_OVF_SEVERE
/* ARC is severely overflowed. */
871 static kmutex_t l2arc_feed_thr_lock
;
872 static kcondvar_t l2arc_feed_thr_cv
;
873 static uint8_t l2arc_thread_exit
;
875 static kmutex_t l2arc_rebuild_thr_lock
;
876 static kcondvar_t l2arc_rebuild_thr_cv
;
878 enum arc_hdr_alloc_flags
{
879 ARC_HDR_ALLOC_RDATA
= 0x1,
880 ARC_HDR_USE_RESERVE
= 0x4,
881 ARC_HDR_ALLOC_LINEAR
= 0x8,
885 static abd_t
*arc_get_data_abd(arc_buf_hdr_t
*, uint64_t, const void *, int);
886 static void *arc_get_data_buf(arc_buf_hdr_t
*, uint64_t, const void *);
887 static void arc_get_data_impl(arc_buf_hdr_t
*, uint64_t, const void *, int);
888 static void arc_free_data_abd(arc_buf_hdr_t
*, abd_t
*, uint64_t, const void *);
889 static void arc_free_data_buf(arc_buf_hdr_t
*, void *, uint64_t, const void *);
890 static void arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
,
892 static void arc_hdr_free_abd(arc_buf_hdr_t
*, boolean_t
);
893 static void arc_hdr_alloc_abd(arc_buf_hdr_t
*, int);
894 static void arc_hdr_destroy(arc_buf_hdr_t
*);
895 static void arc_access(arc_buf_hdr_t
*, arc_flags_t
, boolean_t
);
896 static void arc_buf_watch(arc_buf_t
*);
897 static void arc_change_state(arc_state_t
*, arc_buf_hdr_t
*);
899 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t
*);
900 static uint32_t arc_bufc_to_flags(arc_buf_contents_t
);
901 static inline void arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
902 static inline void arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
904 static boolean_t
l2arc_write_eligible(uint64_t, arc_buf_hdr_t
*);
905 static void l2arc_read_done(zio_t
*);
906 static void l2arc_do_free_on_write(void);
907 static void l2arc_hdr_arcstats_update(arc_buf_hdr_t
*hdr
, boolean_t incr
,
908 boolean_t state_only
);
910 static void arc_prune_async(uint64_t adjust
);
912 #define l2arc_hdr_arcstats_increment(hdr) \
913 l2arc_hdr_arcstats_update((hdr), B_TRUE, B_FALSE)
914 #define l2arc_hdr_arcstats_decrement(hdr) \
915 l2arc_hdr_arcstats_update((hdr), B_FALSE, B_FALSE)
916 #define l2arc_hdr_arcstats_increment_state(hdr) \
917 l2arc_hdr_arcstats_update((hdr), B_TRUE, B_TRUE)
918 #define l2arc_hdr_arcstats_decrement_state(hdr) \
919 l2arc_hdr_arcstats_update((hdr), B_FALSE, B_TRUE)
922 * l2arc_exclude_special : A zfs module parameter that controls whether buffers
923 * present on special vdevs are eligibile for caching in L2ARC. If
924 * set to 1, exclude dbufs on special vdevs from being cached to
927 int l2arc_exclude_special
= 0;
930 * l2arc_mfuonly : A ZFS module parameter that controls whether only MFU
931 * metadata and data are cached from ARC into L2ARC.
933 static int l2arc_mfuonly
= 0;
937 * l2arc_trim_ahead : A ZFS module parameter that controls how much ahead of
938 * the current write size (l2arc_write_max) we should TRIM if we
939 * have filled the device. It is defined as a percentage of the
940 * write size. If set to 100 we trim twice the space required to
941 * accommodate upcoming writes. A minimum of 64MB will be trimmed.
942 * It also enables TRIM of the whole L2ARC device upon creation or
943 * addition to an existing pool or if the header of the device is
944 * invalid upon importing a pool or onlining a cache device. The
945 * default is 0, which disables TRIM on L2ARC altogether as it can
946 * put significant stress on the underlying storage devices. This
947 * will vary depending of how well the specific device handles
950 static uint64_t l2arc_trim_ahead
= 0;
953 * Performance tuning of L2ARC persistence:
955 * l2arc_rebuild_enabled : A ZFS module parameter that controls whether adding
956 * an L2ARC device (either at pool import or later) will attempt
957 * to rebuild L2ARC buffer contents.
958 * l2arc_rebuild_blocks_min_l2size : A ZFS module parameter that controls
959 * whether log blocks are written to the L2ARC device. If the L2ARC
960 * device is less than 1GB, the amount of data l2arc_evict()
961 * evicts is significant compared to the amount of restored L2ARC
962 * data. In this case do not write log blocks in L2ARC in order
963 * not to waste space.
965 static int l2arc_rebuild_enabled
= B_TRUE
;
966 static uint64_t l2arc_rebuild_blocks_min_l2size
= 1024 * 1024 * 1024;
968 /* L2ARC persistence rebuild control routines. */
969 void l2arc_rebuild_vdev(vdev_t
*vd
, boolean_t reopen
);
970 static __attribute__((noreturn
)) void l2arc_dev_rebuild_thread(void *arg
);
971 static int l2arc_rebuild(l2arc_dev_t
*dev
);
973 /* L2ARC persistence read I/O routines. */
974 static int l2arc_dev_hdr_read(l2arc_dev_t
*dev
);
975 static int l2arc_log_blk_read(l2arc_dev_t
*dev
,
976 const l2arc_log_blkptr_t
*this_lp
, const l2arc_log_blkptr_t
*next_lp
,
977 l2arc_log_blk_phys_t
*this_lb
, l2arc_log_blk_phys_t
*next_lb
,
978 zio_t
*this_io
, zio_t
**next_io
);
979 static zio_t
*l2arc_log_blk_fetch(vdev_t
*vd
,
980 const l2arc_log_blkptr_t
*lp
, l2arc_log_blk_phys_t
*lb
);
981 static void l2arc_log_blk_fetch_abort(zio_t
*zio
);
983 /* L2ARC persistence block restoration routines. */
984 static void l2arc_log_blk_restore(l2arc_dev_t
*dev
,
985 const l2arc_log_blk_phys_t
*lb
, uint64_t lb_asize
);
986 static void l2arc_hdr_restore(const l2arc_log_ent_phys_t
*le
,
989 /* L2ARC persistence write I/O routines. */
990 static uint64_t l2arc_log_blk_commit(l2arc_dev_t
*dev
, zio_t
*pio
,
991 l2arc_write_callback_t
*cb
);
993 /* L2ARC persistence auxiliary routines. */
994 boolean_t
l2arc_log_blkptr_valid(l2arc_dev_t
*dev
,
995 const l2arc_log_blkptr_t
*lbp
);
996 static boolean_t
l2arc_log_blk_insert(l2arc_dev_t
*dev
,
997 const arc_buf_hdr_t
*ab
);
998 boolean_t
l2arc_range_check_overlap(uint64_t bottom
,
999 uint64_t top
, uint64_t check
);
1000 static void l2arc_blk_fetch_done(zio_t
*zio
);
1001 static inline uint64_t
1002 l2arc_log_blk_overhead(uint64_t write_sz
, l2arc_dev_t
*dev
);
1005 * We use Cityhash for this. It's fast, and has good hash properties without
1006 * requiring any large static buffers.
1009 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
1011 return (cityhash4(spa
, dva
->dva_word
[0], dva
->dva_word
[1], birth
));
1014 #define HDR_EMPTY(hdr) \
1015 ((hdr)->b_dva.dva_word[0] == 0 && \
1016 (hdr)->b_dva.dva_word[1] == 0)
1018 #define HDR_EMPTY_OR_LOCKED(hdr) \
1019 (HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr)))
1021 #define HDR_EQUAL(spa, dva, birth, hdr) \
1022 ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
1023 ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
1024 ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa)
1027 buf_discard_identity(arc_buf_hdr_t
*hdr
)
1029 hdr
->b_dva
.dva_word
[0] = 0;
1030 hdr
->b_dva
.dva_word
[1] = 0;
1034 static arc_buf_hdr_t
*
1035 buf_hash_find(uint64_t spa
, const blkptr_t
*bp
, kmutex_t
**lockp
)
1037 const dva_t
*dva
= BP_IDENTITY(bp
);
1038 uint64_t birth
= BP_GET_BIRTH(bp
);
1039 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
1040 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1043 mutex_enter(hash_lock
);
1044 for (hdr
= buf_hash_table
.ht_table
[idx
]; hdr
!= NULL
;
1045 hdr
= hdr
->b_hash_next
) {
1046 if (HDR_EQUAL(spa
, dva
, birth
, hdr
)) {
1051 mutex_exit(hash_lock
);
1057 * Insert an entry into the hash table. If there is already an element
1058 * equal to elem in the hash table, then the already existing element
1059 * will be returned and the new element will not be inserted.
1060 * Otherwise returns NULL.
1061 * If lockp == NULL, the caller is assumed to already hold the hash lock.
1063 static arc_buf_hdr_t
*
1064 buf_hash_insert(arc_buf_hdr_t
*hdr
, kmutex_t
**lockp
)
1066 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1067 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1068 arc_buf_hdr_t
*fhdr
;
1071 ASSERT(!DVA_IS_EMPTY(&hdr
->b_dva
));
1072 ASSERT(hdr
->b_birth
!= 0);
1073 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1075 if (lockp
!= NULL
) {
1077 mutex_enter(hash_lock
);
1079 ASSERT(MUTEX_HELD(hash_lock
));
1082 for (fhdr
= buf_hash_table
.ht_table
[idx
], i
= 0; fhdr
!= NULL
;
1083 fhdr
= fhdr
->b_hash_next
, i
++) {
1084 if (HDR_EQUAL(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
, fhdr
))
1088 hdr
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
1089 buf_hash_table
.ht_table
[idx
] = hdr
;
1090 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1092 /* collect some hash table performance data */
1094 ARCSTAT_BUMP(arcstat_hash_collisions
);
1096 ARCSTAT_BUMP(arcstat_hash_chains
);
1097 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
1099 ARCSTAT_BUMP(arcstat_hash_elements
);
1105 buf_hash_remove(arc_buf_hdr_t
*hdr
)
1107 arc_buf_hdr_t
*fhdr
, **hdrp
;
1108 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1110 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
1111 ASSERT(HDR_IN_HASH_TABLE(hdr
));
1113 hdrp
= &buf_hash_table
.ht_table
[idx
];
1114 while ((fhdr
= *hdrp
) != hdr
) {
1115 ASSERT3P(fhdr
, !=, NULL
);
1116 hdrp
= &fhdr
->b_hash_next
;
1118 *hdrp
= hdr
->b_hash_next
;
1119 hdr
->b_hash_next
= NULL
;
1120 arc_hdr_clear_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1122 /* collect some hash table performance data */
1123 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
1124 if (buf_hash_table
.ht_table
[idx
] &&
1125 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
1126 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
1130 * Global data structures and functions for the buf kmem cache.
1133 static kmem_cache_t
*hdr_full_cache
;
1134 static kmem_cache_t
*hdr_l2only_cache
;
1135 static kmem_cache_t
*buf_cache
;
1140 #if defined(_KERNEL)
1142 * Large allocations which do not require contiguous pages
1143 * should be using vmem_free() in the linux kernel\
1145 vmem_free(buf_hash_table
.ht_table
,
1146 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1148 kmem_free(buf_hash_table
.ht_table
,
1149 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1151 for (int i
= 0; i
< BUF_LOCKS
; i
++)
1152 mutex_destroy(BUF_HASH_LOCK(i
));
1153 kmem_cache_destroy(hdr_full_cache
);
1154 kmem_cache_destroy(hdr_l2only_cache
);
1155 kmem_cache_destroy(buf_cache
);
1159 * Constructor callback - called when the cache is empty
1160 * and a new buf is requested.
1163 hdr_full_cons(void *vbuf
, void *unused
, int kmflag
)
1165 (void) unused
, (void) kmflag
;
1166 arc_buf_hdr_t
*hdr
= vbuf
;
1168 memset(hdr
, 0, HDR_FULL_SIZE
);
1169 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
1170 zfs_refcount_create(&hdr
->b_l1hdr
.b_refcnt
);
1172 mutex_init(&hdr
->b_l1hdr
.b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1174 multilist_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1175 list_link_init(&hdr
->b_l2hdr
.b_l2node
);
1176 arc_space_consume(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1182 hdr_l2only_cons(void *vbuf
, void *unused
, int kmflag
)
1184 (void) unused
, (void) kmflag
;
1185 arc_buf_hdr_t
*hdr
= vbuf
;
1187 memset(hdr
, 0, HDR_L2ONLY_SIZE
);
1188 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1194 buf_cons(void *vbuf
, void *unused
, int kmflag
)
1196 (void) unused
, (void) kmflag
;
1197 arc_buf_t
*buf
= vbuf
;
1199 memset(buf
, 0, sizeof (arc_buf_t
));
1200 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1206 * Destructor callback - called when a cached buf is
1207 * no longer required.
1210 hdr_full_dest(void *vbuf
, void *unused
)
1213 arc_buf_hdr_t
*hdr
= vbuf
;
1215 ASSERT(HDR_EMPTY(hdr
));
1216 zfs_refcount_destroy(&hdr
->b_l1hdr
.b_refcnt
);
1218 mutex_destroy(&hdr
->b_l1hdr
.b_freeze_lock
);
1220 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1221 arc_space_return(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1225 hdr_l2only_dest(void *vbuf
, void *unused
)
1228 arc_buf_hdr_t
*hdr
= vbuf
;
1230 ASSERT(HDR_EMPTY(hdr
));
1231 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1235 buf_dest(void *vbuf
, void *unused
)
1240 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1246 uint64_t *ct
= NULL
;
1247 uint64_t hsize
= 1ULL << 12;
1251 * The hash table is big enough to fill all of physical memory
1252 * with an average block size of zfs_arc_average_blocksize (default 8K).
1253 * By default, the table will take up
1254 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1256 while (hsize
* zfs_arc_average_blocksize
< arc_all_memory())
1259 buf_hash_table
.ht_mask
= hsize
- 1;
1260 #if defined(_KERNEL)
1262 * Large allocations which do not require contiguous pages
1263 * should be using vmem_alloc() in the linux kernel
1265 buf_hash_table
.ht_table
=
1266 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1268 buf_hash_table
.ht_table
=
1269 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1271 if (buf_hash_table
.ht_table
== NULL
) {
1272 ASSERT(hsize
> (1ULL << 8));
1277 hdr_full_cache
= kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE
,
1278 0, hdr_full_cons
, hdr_full_dest
, NULL
, NULL
, NULL
, KMC_RECLAIMABLE
);
1279 hdr_l2only_cache
= kmem_cache_create("arc_buf_hdr_t_l2only",
1280 HDR_L2ONLY_SIZE
, 0, hdr_l2only_cons
, hdr_l2only_dest
, NULL
,
1282 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1283 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1285 for (i
= 0; i
< 256; i
++)
1286 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1287 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1289 for (i
= 0; i
< BUF_LOCKS
; i
++)
1290 mutex_init(BUF_HASH_LOCK(i
), NULL
, MUTEX_DEFAULT
, NULL
);
1293 #define ARC_MINTIME (hz>>4) /* 62 ms */
1296 * This is the size that the buf occupies in memory. If the buf is compressed,
1297 * it will correspond to the compressed size. You should use this method of
1298 * getting the buf size unless you explicitly need the logical size.
1301 arc_buf_size(arc_buf_t
*buf
)
1303 return (ARC_BUF_COMPRESSED(buf
) ?
1304 HDR_GET_PSIZE(buf
->b_hdr
) : HDR_GET_LSIZE(buf
->b_hdr
));
1308 arc_buf_lsize(arc_buf_t
*buf
)
1310 return (HDR_GET_LSIZE(buf
->b_hdr
));
1314 * This function will return B_TRUE if the buffer is encrypted in memory.
1315 * This buffer can be decrypted by calling arc_untransform().
1318 arc_is_encrypted(arc_buf_t
*buf
)
1320 return (ARC_BUF_ENCRYPTED(buf
) != 0);
1324 * Returns B_TRUE if the buffer represents data that has not had its MAC
1328 arc_is_unauthenticated(arc_buf_t
*buf
)
1330 return (HDR_NOAUTH(buf
->b_hdr
) != 0);
1334 arc_get_raw_params(arc_buf_t
*buf
, boolean_t
*byteorder
, uint8_t *salt
,
1335 uint8_t *iv
, uint8_t *mac
)
1337 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1339 ASSERT(HDR_PROTECTED(hdr
));
1341 memcpy(salt
, hdr
->b_crypt_hdr
.b_salt
, ZIO_DATA_SALT_LEN
);
1342 memcpy(iv
, hdr
->b_crypt_hdr
.b_iv
, ZIO_DATA_IV_LEN
);
1343 memcpy(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
);
1344 *byteorder
= (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
1345 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
1349 * Indicates how this buffer is compressed in memory. If it is not compressed
1350 * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with
1351 * arc_untransform() as long as it is also unencrypted.
1354 arc_get_compression(arc_buf_t
*buf
)
1356 return (ARC_BUF_COMPRESSED(buf
) ?
1357 HDR_GET_COMPRESS(buf
->b_hdr
) : ZIO_COMPRESS_OFF
);
1361 * Return the compression algorithm used to store this data in the ARC. If ARC
1362 * compression is enabled or this is an encrypted block, this will be the same
1363 * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF.
1365 static inline enum zio_compress
1366 arc_hdr_get_compress(arc_buf_hdr_t
*hdr
)
1368 return (HDR_COMPRESSION_ENABLED(hdr
) ?
1369 HDR_GET_COMPRESS(hdr
) : ZIO_COMPRESS_OFF
);
1373 arc_get_complevel(arc_buf_t
*buf
)
1375 return (buf
->b_hdr
->b_complevel
);
1378 static inline boolean_t
1379 arc_buf_is_shared(arc_buf_t
*buf
)
1381 boolean_t shared
= (buf
->b_data
!= NULL
&&
1382 buf
->b_hdr
->b_l1hdr
.b_pabd
!= NULL
&&
1383 abd_is_linear(buf
->b_hdr
->b_l1hdr
.b_pabd
) &&
1384 buf
->b_data
== abd_to_buf(buf
->b_hdr
->b_l1hdr
.b_pabd
));
1385 IMPLY(shared
, HDR_SHARED_DATA(buf
->b_hdr
));
1386 EQUIV(shared
, ARC_BUF_SHARED(buf
));
1387 IMPLY(shared
, ARC_BUF_COMPRESSED(buf
) || ARC_BUF_LAST(buf
));
1390 * It would be nice to assert arc_can_share() too, but the "hdr isn't
1391 * already being shared" requirement prevents us from doing that.
1398 * Free the checksum associated with this header. If there is no checksum, this
1402 arc_cksum_free(arc_buf_hdr_t
*hdr
)
1405 ASSERT(HDR_HAS_L1HDR(hdr
));
1407 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1408 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
) {
1409 kmem_free(hdr
->b_l1hdr
.b_freeze_cksum
, sizeof (zio_cksum_t
));
1410 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
1412 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1417 * Return true iff at least one of the bufs on hdr is not compressed.
1418 * Encrypted buffers count as compressed.
1421 arc_hdr_has_uncompressed_buf(arc_buf_hdr_t
*hdr
)
1423 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY_OR_LOCKED(hdr
));
1425 for (arc_buf_t
*b
= hdr
->b_l1hdr
.b_buf
; b
!= NULL
; b
= b
->b_next
) {
1426 if (!ARC_BUF_COMPRESSED(b
)) {
1435 * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data
1436 * matches the checksum that is stored in the hdr. If there is no checksum,
1437 * or if the buf is compressed, this is a no-op.
1440 arc_cksum_verify(arc_buf_t
*buf
)
1443 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1446 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1449 if (ARC_BUF_COMPRESSED(buf
))
1452 ASSERT(HDR_HAS_L1HDR(hdr
));
1454 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1456 if (hdr
->b_l1hdr
.b_freeze_cksum
== NULL
|| HDR_IO_ERROR(hdr
)) {
1457 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1461 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
, &zc
);
1462 if (!ZIO_CHECKSUM_EQUAL(*hdr
->b_l1hdr
.b_freeze_cksum
, zc
))
1463 panic("buffer modified while frozen!");
1464 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1469 * This function makes the assumption that data stored in the L2ARC
1470 * will be transformed exactly as it is in the main pool. Because of
1471 * this we can verify the checksum against the reading process's bp.
1474 arc_cksum_is_equal(arc_buf_hdr_t
*hdr
, zio_t
*zio
)
1476 ASSERT(!BP_IS_EMBEDDED(zio
->io_bp
));
1477 VERIFY3U(BP_GET_PSIZE(zio
->io_bp
), ==, HDR_GET_PSIZE(hdr
));
1480 * Block pointers always store the checksum for the logical data.
1481 * If the block pointer has the gang bit set, then the checksum
1482 * it represents is for the reconstituted data and not for an
1483 * individual gang member. The zio pipeline, however, must be able to
1484 * determine the checksum of each of the gang constituents so it
1485 * treats the checksum comparison differently than what we need
1486 * for l2arc blocks. This prevents us from using the
1487 * zio_checksum_error() interface directly. Instead we must call the
1488 * zio_checksum_error_impl() so that we can ensure the checksum is
1489 * generated using the correct checksum algorithm and accounts for the
1490 * logical I/O size and not just a gang fragment.
1492 return (zio_checksum_error_impl(zio
->io_spa
, zio
->io_bp
,
1493 BP_GET_CHECKSUM(zio
->io_bp
), zio
->io_abd
, zio
->io_size
,
1494 zio
->io_offset
, NULL
) == 0);
1498 * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a
1499 * checksum and attaches it to the buf's hdr so that we can ensure that the buf
1500 * isn't modified later on. If buf is compressed or there is already a checksum
1501 * on the hdr, this is a no-op (we only checksum uncompressed bufs).
1504 arc_cksum_compute(arc_buf_t
*buf
)
1506 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1510 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1511 ASSERT(HDR_HAS_L1HDR(hdr
));
1512 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1513 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
|| ARC_BUF_COMPRESSED(buf
)) {
1514 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1518 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
1519 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1520 hdr
->b_l1hdr
.b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1522 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), NULL
,
1523 hdr
->b_l1hdr
.b_freeze_cksum
);
1524 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1531 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1533 (void) sig
, (void) unused
;
1534 panic("Got SIGSEGV at address: 0x%lx\n", (long)si
->si_addr
);
1539 arc_buf_unwatch(arc_buf_t
*buf
)
1543 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1544 PROT_READ
| PROT_WRITE
));
1552 arc_buf_watch(arc_buf_t
*buf
)
1556 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1563 static arc_buf_contents_t
1564 arc_buf_type(arc_buf_hdr_t
*hdr
)
1566 arc_buf_contents_t type
;
1567 if (HDR_ISTYPE_METADATA(hdr
)) {
1568 type
= ARC_BUFC_METADATA
;
1570 type
= ARC_BUFC_DATA
;
1572 VERIFY3U(hdr
->b_type
, ==, type
);
1577 arc_is_metadata(arc_buf_t
*buf
)
1579 return (HDR_ISTYPE_METADATA(buf
->b_hdr
) != 0);
1583 arc_bufc_to_flags(arc_buf_contents_t type
)
1587 /* metadata field is 0 if buffer contains normal data */
1589 case ARC_BUFC_METADATA
:
1590 return (ARC_FLAG_BUFC_METADATA
);
1594 panic("undefined ARC buffer type!");
1595 return ((uint32_t)-1);
1599 arc_buf_thaw(arc_buf_t
*buf
)
1601 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1603 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
1604 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1606 arc_cksum_verify(buf
);
1609 * Compressed buffers do not manipulate the b_freeze_cksum.
1611 if (ARC_BUF_COMPRESSED(buf
))
1614 ASSERT(HDR_HAS_L1HDR(hdr
));
1615 arc_cksum_free(hdr
);
1616 arc_buf_unwatch(buf
);
1620 arc_buf_freeze(arc_buf_t
*buf
)
1622 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1625 if (ARC_BUF_COMPRESSED(buf
))
1628 ASSERT(HDR_HAS_L1HDR(buf
->b_hdr
));
1629 arc_cksum_compute(buf
);
1633 * The arc_buf_hdr_t's b_flags should never be modified directly. Instead,
1634 * the following functions should be used to ensure that the flags are
1635 * updated in a thread-safe way. When manipulating the flags either
1636 * the hash_lock must be held or the hdr must be undiscoverable. This
1637 * ensures that we're not racing with any other threads when updating
1641 arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1643 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1644 hdr
->b_flags
|= flags
;
1648 arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1650 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1651 hdr
->b_flags
&= ~flags
;
1655 * Setting the compression bits in the arc_buf_hdr_t's b_flags is
1656 * done in a special way since we have to clear and set bits
1657 * at the same time. Consumers that wish to set the compression bits
1658 * must use this function to ensure that the flags are updated in
1659 * thread-safe manner.
1662 arc_hdr_set_compress(arc_buf_hdr_t
*hdr
, enum zio_compress cmp
)
1664 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1667 * Holes and embedded blocks will always have a psize = 0 so
1668 * we ignore the compression of the blkptr and set the
1669 * want to uncompress them. Mark them as uncompressed.
1671 if (!zfs_compressed_arc_enabled
|| HDR_GET_PSIZE(hdr
) == 0) {
1672 arc_hdr_clear_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1673 ASSERT(!HDR_COMPRESSION_ENABLED(hdr
));
1675 arc_hdr_set_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1676 ASSERT(HDR_COMPRESSION_ENABLED(hdr
));
1679 HDR_SET_COMPRESS(hdr
, cmp
);
1680 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, cmp
);
1684 * Looks for another buf on the same hdr which has the data decompressed, copies
1685 * from it, and returns true. If no such buf exists, returns false.
1688 arc_buf_try_copy_decompressed_data(arc_buf_t
*buf
)
1690 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1691 boolean_t copied
= B_FALSE
;
1693 ASSERT(HDR_HAS_L1HDR(hdr
));
1694 ASSERT3P(buf
->b_data
, !=, NULL
);
1695 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1697 for (arc_buf_t
*from
= hdr
->b_l1hdr
.b_buf
; from
!= NULL
;
1698 from
= from
->b_next
) {
1699 /* can't use our own data buffer */
1704 if (!ARC_BUF_COMPRESSED(from
)) {
1705 memcpy(buf
->b_data
, from
->b_data
, arc_buf_size(buf
));
1713 * There were no decompressed bufs, so there should not be a
1714 * checksum on the hdr either.
1716 if (zfs_flags
& ZFS_DEBUG_MODIFY
)
1717 EQUIV(!copied
, hdr
->b_l1hdr
.b_freeze_cksum
== NULL
);
1724 * Allocates an ARC buf header that's in an evicted & L2-cached state.
1725 * This is used during l2arc reconstruction to make empty ARC buffers
1726 * which circumvent the regular disk->arc->l2arc path and instead come
1727 * into being in the reverse order, i.e. l2arc->arc.
1729 static arc_buf_hdr_t
*
1730 arc_buf_alloc_l2only(size_t size
, arc_buf_contents_t type
, l2arc_dev_t
*dev
,
1731 dva_t dva
, uint64_t daddr
, int32_t psize
, uint64_t asize
, uint64_t birth
,
1732 enum zio_compress compress
, uint8_t complevel
, boolean_t
protected,
1733 boolean_t prefetch
, arc_state_type_t arcs_state
)
1738 ASSERT(dev
->l2ad_vdev
!= NULL
);
1740 hdr
= kmem_cache_alloc(hdr_l2only_cache
, KM_SLEEP
);
1741 hdr
->b_birth
= birth
;
1744 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L2HDR
);
1745 HDR_SET_LSIZE(hdr
, size
);
1746 HDR_SET_PSIZE(hdr
, psize
);
1747 HDR_SET_L2SIZE(hdr
, asize
);
1748 arc_hdr_set_compress(hdr
, compress
);
1749 hdr
->b_complevel
= complevel
;
1751 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
1753 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
1754 hdr
->b_spa
= spa_load_guid(dev
->l2ad_vdev
->vdev_spa
);
1758 hdr
->b_l2hdr
.b_dev
= dev
;
1759 hdr
->b_l2hdr
.b_daddr
= daddr
;
1760 hdr
->b_l2hdr
.b_arcs_state
= arcs_state
;
1766 * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t.
1769 arc_hdr_size(arc_buf_hdr_t
*hdr
)
1773 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
1774 HDR_GET_PSIZE(hdr
) > 0) {
1775 size
= HDR_GET_PSIZE(hdr
);
1777 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, 0);
1778 size
= HDR_GET_LSIZE(hdr
);
1784 arc_hdr_authenticate(arc_buf_hdr_t
*hdr
, spa_t
*spa
, uint64_t dsobj
)
1788 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
1789 uint64_t psize
= HDR_GET_PSIZE(hdr
);
1790 abd_t
*abd
= hdr
->b_l1hdr
.b_pabd
;
1791 boolean_t free_abd
= B_FALSE
;
1793 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1794 ASSERT(HDR_AUTHENTICATED(hdr
));
1795 ASSERT3P(abd
, !=, NULL
);
1798 * The MAC is calculated on the compressed data that is stored on disk.
1799 * However, if compressed arc is disabled we will only have the
1800 * decompressed data available to us now. Compress it into a temporary
1801 * abd so we can verify the MAC. The performance overhead of this will
1802 * be relatively low, since most objects in an encrypted objset will
1803 * be encrypted (instead of authenticated) anyway.
1805 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1806 !HDR_COMPRESSION_ENABLED(hdr
)) {
1808 csize
= zio_compress_data(HDR_GET_COMPRESS(hdr
),
1809 hdr
->b_l1hdr
.b_pabd
, &abd
, lsize
, MIN(lsize
, psize
),
1811 if (csize
>= lsize
|| csize
> psize
) {
1812 ret
= SET_ERROR(EIO
);
1815 ASSERT3P(abd
, !=, NULL
);
1816 abd_zero_off(abd
, csize
, psize
- csize
);
1821 * Authentication is best effort. We authenticate whenever the key is
1822 * available. If we succeed we clear ARC_FLAG_NOAUTH.
1824 if (hdr
->b_crypt_hdr
.b_ot
== DMU_OT_OBJSET
) {
1825 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, ZIO_COMPRESS_OFF
);
1826 ASSERT3U(lsize
, ==, psize
);
1827 ret
= spa_do_crypt_objset_mac_abd(B_FALSE
, spa
, dsobj
, abd
,
1828 psize
, hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1830 ret
= spa_do_crypt_mac_abd(B_FALSE
, spa
, dsobj
, abd
, psize
,
1831 hdr
->b_crypt_hdr
.b_mac
);
1835 arc_hdr_clear_flags(hdr
, ARC_FLAG_NOAUTH
);
1836 else if (ret
== ENOENT
)
1846 * This function will take a header that only has raw encrypted data in
1847 * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in
1848 * b_l1hdr.b_pabd. If designated in the header flags, this function will
1849 * also decompress the data.
1852 arc_hdr_decrypt(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
)
1856 boolean_t no_crypt
= B_FALSE
;
1857 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
1859 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1860 ASSERT(HDR_ENCRYPTED(hdr
));
1862 arc_hdr_alloc_abd(hdr
, 0);
1864 ret
= spa_do_crypt_abd(B_FALSE
, spa
, zb
, hdr
->b_crypt_hdr
.b_ot
,
1865 B_FALSE
, bswap
, hdr
->b_crypt_hdr
.b_salt
, hdr
->b_crypt_hdr
.b_iv
,
1866 hdr
->b_crypt_hdr
.b_mac
, HDR_GET_PSIZE(hdr
), hdr
->b_l1hdr
.b_pabd
,
1867 hdr
->b_crypt_hdr
.b_rabd
, &no_crypt
);
1872 abd_copy(hdr
->b_l1hdr
.b_pabd
, hdr
->b_crypt_hdr
.b_rabd
,
1873 HDR_GET_PSIZE(hdr
));
1877 * If this header has disabled arc compression but the b_pabd is
1878 * compressed after decrypting it, we need to decompress the newly
1881 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1882 !HDR_COMPRESSION_ENABLED(hdr
)) {
1884 * We want to make sure that we are correctly honoring the
1885 * zfs_abd_scatter_enabled setting, so we allocate an abd here
1886 * and then loan a buffer from it, rather than allocating a
1887 * linear buffer and wrapping it in an abd later.
1889 cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
, 0);
1891 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
1892 hdr
->b_l1hdr
.b_pabd
, cabd
, HDR_GET_PSIZE(hdr
),
1893 HDR_GET_LSIZE(hdr
), &hdr
->b_complevel
);
1898 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
1899 arc_hdr_size(hdr
), hdr
);
1900 hdr
->b_l1hdr
.b_pabd
= cabd
;
1906 arc_hdr_free_abd(hdr
, B_FALSE
);
1908 arc_free_data_buf(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
1914 * This function is called during arc_buf_fill() to prepare the header's
1915 * abd plaintext pointer for use. This involves authenticated protected
1916 * data and decrypting encrypted data into the plaintext abd.
1919 arc_fill_hdr_crypt(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, spa_t
*spa
,
1920 const zbookmark_phys_t
*zb
, boolean_t noauth
)
1924 ASSERT(HDR_PROTECTED(hdr
));
1926 if (hash_lock
!= NULL
)
1927 mutex_enter(hash_lock
);
1929 if (HDR_NOAUTH(hdr
) && !noauth
) {
1931 * The caller requested authenticated data but our data has
1932 * not been authenticated yet. Verify the MAC now if we can.
1934 ret
= arc_hdr_authenticate(hdr
, spa
, zb
->zb_objset
);
1937 } else if (HDR_HAS_RABD(hdr
) && hdr
->b_l1hdr
.b_pabd
== NULL
) {
1939 * If we only have the encrypted version of the data, but the
1940 * unencrypted version was requested we take this opportunity
1941 * to store the decrypted version in the header for future use.
1943 ret
= arc_hdr_decrypt(hdr
, spa
, zb
);
1948 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
1950 if (hash_lock
!= NULL
)
1951 mutex_exit(hash_lock
);
1956 if (hash_lock
!= NULL
)
1957 mutex_exit(hash_lock
);
1963 * This function is used by the dbuf code to decrypt bonus buffers in place.
1964 * The dbuf code itself doesn't have any locking for decrypting a shared dnode
1965 * block, so we use the hash lock here to protect against concurrent calls to
1969 arc_buf_untransform_in_place(arc_buf_t
*buf
)
1971 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1973 ASSERT(HDR_ENCRYPTED(hdr
));
1974 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
1975 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
1976 ASSERT3PF(hdr
->b_l1hdr
.b_pabd
, !=, NULL
, "hdr %px buf %px", hdr
, buf
);
1978 zio_crypt_copy_dnode_bonus(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
1980 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
1981 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
1985 * Given a buf that has a data buffer attached to it, this function will
1986 * efficiently fill the buf with data of the specified compression setting from
1987 * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr
1988 * are already sharing a data buf, no copy is performed.
1990 * If the buf is marked as compressed but uncompressed data was requested, this
1991 * will allocate a new data buffer for the buf, remove that flag, and fill the
1992 * buf with uncompressed data. You can't request a compressed buf on a hdr with
1993 * uncompressed data, and (since we haven't added support for it yet) if you
1994 * want compressed data your buf must already be marked as compressed and have
1995 * the correct-sized data buffer.
1998 arc_buf_fill(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
1999 arc_fill_flags_t flags
)
2002 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2003 boolean_t hdr_compressed
=
2004 (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
2005 boolean_t compressed
= (flags
& ARC_FILL_COMPRESSED
) != 0;
2006 boolean_t encrypted
= (flags
& ARC_FILL_ENCRYPTED
) != 0;
2007 dmu_object_byteswap_t bswap
= hdr
->b_l1hdr
.b_byteswap
;
2008 kmutex_t
*hash_lock
= (flags
& ARC_FILL_LOCKED
) ? NULL
: HDR_LOCK(hdr
);
2010 ASSERT3P(buf
->b_data
, !=, NULL
);
2011 IMPLY(compressed
, hdr_compressed
|| ARC_BUF_ENCRYPTED(buf
));
2012 IMPLY(compressed
, ARC_BUF_COMPRESSED(buf
));
2013 IMPLY(encrypted
, HDR_ENCRYPTED(hdr
));
2014 IMPLY(encrypted
, ARC_BUF_ENCRYPTED(buf
));
2015 IMPLY(encrypted
, ARC_BUF_COMPRESSED(buf
));
2016 IMPLY(encrypted
, !arc_buf_is_shared(buf
));
2019 * If the caller wanted encrypted data we just need to copy it from
2020 * b_rabd and potentially byteswap it. We won't be able to do any
2021 * further transforms on it.
2024 ASSERT(HDR_HAS_RABD(hdr
));
2025 abd_copy_to_buf(buf
->b_data
, hdr
->b_crypt_hdr
.b_rabd
,
2026 HDR_GET_PSIZE(hdr
));
2031 * Adjust encrypted and authenticated headers to accommodate
2032 * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are
2033 * allowed to fail decryption due to keys not being loaded
2034 * without being marked as an IO error.
2036 if (HDR_PROTECTED(hdr
)) {
2037 error
= arc_fill_hdr_crypt(hdr
, hash_lock
, spa
,
2038 zb
, !!(flags
& ARC_FILL_NOAUTH
));
2039 if (error
== EACCES
&& (flags
& ARC_FILL_IN_PLACE
) != 0) {
2041 } else if (error
!= 0) {
2042 if (hash_lock
!= NULL
)
2043 mutex_enter(hash_lock
);
2044 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2045 if (hash_lock
!= NULL
)
2046 mutex_exit(hash_lock
);
2052 * There is a special case here for dnode blocks which are
2053 * decrypting their bonus buffers. These blocks may request to
2054 * be decrypted in-place. This is necessary because there may
2055 * be many dnodes pointing into this buffer and there is
2056 * currently no method to synchronize replacing the backing
2057 * b_data buffer and updating all of the pointers. Here we use
2058 * the hash lock to ensure there are no races. If the need
2059 * arises for other types to be decrypted in-place, they must
2060 * add handling here as well.
2062 if ((flags
& ARC_FILL_IN_PLACE
) != 0) {
2063 ASSERT(!hdr_compressed
);
2064 ASSERT(!compressed
);
2067 if (HDR_ENCRYPTED(hdr
) && ARC_BUF_ENCRYPTED(buf
)) {
2068 ASSERT3U(hdr
->b_crypt_hdr
.b_ot
, ==, DMU_OT_DNODE
);
2070 if (hash_lock
!= NULL
)
2071 mutex_enter(hash_lock
);
2072 arc_buf_untransform_in_place(buf
);
2073 if (hash_lock
!= NULL
)
2074 mutex_exit(hash_lock
);
2076 /* Compute the hdr's checksum if necessary */
2077 arc_cksum_compute(buf
);
2083 if (hdr_compressed
== compressed
) {
2084 if (ARC_BUF_SHARED(buf
)) {
2085 ASSERT(arc_buf_is_shared(buf
));
2087 abd_copy_to_buf(buf
->b_data
, hdr
->b_l1hdr
.b_pabd
,
2091 ASSERT(hdr_compressed
);
2092 ASSERT(!compressed
);
2095 * If the buf is sharing its data with the hdr, unlink it and
2096 * allocate a new data buffer for the buf.
2098 if (ARC_BUF_SHARED(buf
)) {
2099 ASSERTF(ARC_BUF_COMPRESSED(buf
),
2100 "buf %p was uncompressed", buf
);
2102 /* We need to give the buf its own b_data */
2103 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
2105 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2106 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2108 /* Previously overhead was 0; just add new overhead */
2109 ARCSTAT_INCR(arcstat_overhead_size
, HDR_GET_LSIZE(hdr
));
2110 } else if (ARC_BUF_COMPRESSED(buf
)) {
2111 ASSERT(!arc_buf_is_shared(buf
));
2113 /* We need to reallocate the buf's b_data */
2114 arc_free_data_buf(hdr
, buf
->b_data
, HDR_GET_PSIZE(hdr
),
2117 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
2119 /* We increased the size of b_data; update overhead */
2120 ARCSTAT_INCR(arcstat_overhead_size
,
2121 HDR_GET_LSIZE(hdr
) - HDR_GET_PSIZE(hdr
));
2125 * Regardless of the buf's previous compression settings, it
2126 * should not be compressed at the end of this function.
2128 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
2131 * Try copying the data from another buf which already has a
2132 * decompressed version. If that's not possible, it's time to
2133 * bite the bullet and decompress the data from the hdr.
2135 if (arc_buf_try_copy_decompressed_data(buf
)) {
2136 /* Skip byteswapping and checksumming (already done) */
2140 abd_get_from_buf_struct(&dabd
, buf
->b_data
,
2141 HDR_GET_LSIZE(hdr
));
2142 error
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
2143 hdr
->b_l1hdr
.b_pabd
, &dabd
,
2144 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
),
2149 * Absent hardware errors or software bugs, this should
2150 * be impossible, but log it anyway so we can debug it.
2154 "hdr %px, compress %d, psize %d, lsize %d",
2155 hdr
, arc_hdr_get_compress(hdr
),
2156 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
2157 if (hash_lock
!= NULL
)
2158 mutex_enter(hash_lock
);
2159 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
2160 if (hash_lock
!= NULL
)
2161 mutex_exit(hash_lock
);
2162 return (SET_ERROR(EIO
));
2168 /* Byteswap the buf's data if necessary */
2169 if (bswap
!= DMU_BSWAP_NUMFUNCS
) {
2170 ASSERT(!HDR_SHARED_DATA(hdr
));
2171 ASSERT3U(bswap
, <, DMU_BSWAP_NUMFUNCS
);
2172 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, HDR_GET_LSIZE(hdr
));
2175 /* Compute the hdr's checksum if necessary */
2176 arc_cksum_compute(buf
);
2182 * If this function is being called to decrypt an encrypted buffer or verify an
2183 * authenticated one, the key must be loaded and a mapping must be made
2184 * available in the keystore via spa_keystore_create_mapping() or one of its
2188 arc_untransform(arc_buf_t
*buf
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2192 arc_fill_flags_t flags
= 0;
2195 flags
|= ARC_FILL_IN_PLACE
;
2197 ret
= arc_buf_fill(buf
, spa
, zb
, flags
);
2198 if (ret
== ECKSUM
) {
2200 * Convert authentication and decryption errors to EIO
2201 * (and generate an ereport) before leaving the ARC.
2203 ret
= SET_ERROR(EIO
);
2204 spa_log_error(spa
, zb
, buf
->b_hdr
->b_birth
);
2205 (void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION
,
2206 spa
, NULL
, zb
, NULL
, 0);
2213 * Increment the amount of evictable space in the arc_state_t's refcount.
2214 * We account for the space used by the hdr and the arc buf individually
2215 * so that we can add and remove them from the refcount individually.
2218 arc_evictable_space_increment(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2220 arc_buf_contents_t type
= arc_buf_type(hdr
);
2222 ASSERT(HDR_HAS_L1HDR(hdr
));
2224 if (GHOST_STATE(state
)) {
2225 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2226 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2227 ASSERT(!HDR_HAS_RABD(hdr
));
2228 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2229 HDR_GET_LSIZE(hdr
), hdr
);
2233 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2234 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2235 arc_hdr_size(hdr
), hdr
);
2237 if (HDR_HAS_RABD(hdr
)) {
2238 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2239 HDR_GET_PSIZE(hdr
), hdr
);
2242 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2243 buf
= buf
->b_next
) {
2244 if (ARC_BUF_SHARED(buf
))
2246 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
2247 arc_buf_size(buf
), buf
);
2252 * Decrement the amount of evictable space in the arc_state_t's refcount.
2253 * We account for the space used by the hdr and the arc buf individually
2254 * so that we can add and remove them from the refcount individually.
2257 arc_evictable_space_decrement(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
2259 arc_buf_contents_t type
= arc_buf_type(hdr
);
2261 ASSERT(HDR_HAS_L1HDR(hdr
));
2263 if (GHOST_STATE(state
)) {
2264 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2265 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2266 ASSERT(!HDR_HAS_RABD(hdr
));
2267 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2268 HDR_GET_LSIZE(hdr
), hdr
);
2272 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2273 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2274 arc_hdr_size(hdr
), hdr
);
2276 if (HDR_HAS_RABD(hdr
)) {
2277 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2278 HDR_GET_PSIZE(hdr
), hdr
);
2281 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2282 buf
= buf
->b_next
) {
2283 if (ARC_BUF_SHARED(buf
))
2285 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2286 arc_buf_size(buf
), buf
);
2291 * Add a reference to this hdr indicating that someone is actively
2292 * referencing that memory. When the refcount transitions from 0 to 1,
2293 * we remove it from the respective arc_state_t list to indicate that
2294 * it is not evictable.
2297 add_reference(arc_buf_hdr_t
*hdr
, const void *tag
)
2299 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2301 ASSERT(HDR_HAS_L1HDR(hdr
));
2302 if (!HDR_EMPTY(hdr
) && !MUTEX_HELD(HDR_LOCK(hdr
))) {
2303 ASSERT(state
== arc_anon
);
2304 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2305 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2308 if ((zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
) == 1) &&
2309 state
!= arc_anon
&& state
!= arc_l2c_only
) {
2310 /* We don't use the L2-only state list. */
2311 multilist_remove(&state
->arcs_list
[arc_buf_type(hdr
)], hdr
);
2312 arc_evictable_space_decrement(hdr
, state
);
2317 * Remove a reference from this hdr. When the reference transitions from
2318 * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's
2319 * list making it eligible for eviction.
2322 remove_reference(arc_buf_hdr_t
*hdr
, const void *tag
)
2325 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2327 ASSERT(HDR_HAS_L1HDR(hdr
));
2328 ASSERT(state
== arc_anon
|| MUTEX_HELD(HDR_LOCK(hdr
)));
2329 ASSERT(!GHOST_STATE(state
)); /* arc_l2c_only counts as a ghost. */
2331 if ((cnt
= zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
)) != 0)
2334 if (state
== arc_anon
) {
2335 arc_hdr_destroy(hdr
);
2338 if (state
== arc_uncached
&& !HDR_PREFETCH(hdr
)) {
2339 arc_change_state(arc_anon
, hdr
);
2340 arc_hdr_destroy(hdr
);
2343 multilist_insert(&state
->arcs_list
[arc_buf_type(hdr
)], hdr
);
2344 arc_evictable_space_increment(hdr
, state
);
2349 * Returns detailed information about a specific arc buffer. When the
2350 * state_index argument is set the function will calculate the arc header
2351 * list position for its arc state. Since this requires a linear traversal
2352 * callers are strongly encourage not to do this. However, it can be helpful
2353 * for targeted analysis so the functionality is provided.
2356 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
2359 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
2360 l1arc_buf_hdr_t
*l1hdr
= NULL
;
2361 l2arc_buf_hdr_t
*l2hdr
= NULL
;
2362 arc_state_t
*state
= NULL
;
2364 memset(abi
, 0, sizeof (arc_buf_info_t
));
2369 abi
->abi_flags
= hdr
->b_flags
;
2371 if (HDR_HAS_L1HDR(hdr
)) {
2372 l1hdr
= &hdr
->b_l1hdr
;
2373 state
= l1hdr
->b_state
;
2375 if (HDR_HAS_L2HDR(hdr
))
2376 l2hdr
= &hdr
->b_l2hdr
;
2379 abi
->abi_bufcnt
= 0;
2380 for (arc_buf_t
*buf
= l1hdr
->b_buf
; buf
; buf
= buf
->b_next
)
2382 abi
->abi_access
= l1hdr
->b_arc_access
;
2383 abi
->abi_mru_hits
= l1hdr
->b_mru_hits
;
2384 abi
->abi_mru_ghost_hits
= l1hdr
->b_mru_ghost_hits
;
2385 abi
->abi_mfu_hits
= l1hdr
->b_mfu_hits
;
2386 abi
->abi_mfu_ghost_hits
= l1hdr
->b_mfu_ghost_hits
;
2387 abi
->abi_holds
= zfs_refcount_count(&l1hdr
->b_refcnt
);
2391 abi
->abi_l2arc_dattr
= l2hdr
->b_daddr
;
2392 abi
->abi_l2arc_hits
= l2hdr
->b_hits
;
2395 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
2396 abi
->abi_state_contents
= arc_buf_type(hdr
);
2397 abi
->abi_size
= arc_hdr_size(hdr
);
2401 * Move the supplied buffer to the indicated state. The hash lock
2402 * for the buffer must be held by the caller.
2405 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*hdr
)
2407 arc_state_t
*old_state
;
2409 boolean_t update_old
, update_new
;
2410 arc_buf_contents_t type
= arc_buf_type(hdr
);
2413 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
2414 * in arc_read() when bringing a buffer out of the L2ARC. However, the
2415 * L1 hdr doesn't always exist when we change state to arc_anon before
2416 * destroying a header, in which case reallocating to add the L1 hdr is
2419 if (HDR_HAS_L1HDR(hdr
)) {
2420 old_state
= hdr
->b_l1hdr
.b_state
;
2421 refcnt
= zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
);
2422 update_old
= (hdr
->b_l1hdr
.b_buf
!= NULL
||
2423 hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
2425 IMPLY(GHOST_STATE(old_state
), hdr
->b_l1hdr
.b_buf
== NULL
);
2426 IMPLY(GHOST_STATE(new_state
), hdr
->b_l1hdr
.b_buf
== NULL
);
2427 IMPLY(old_state
== arc_anon
, hdr
->b_l1hdr
.b_buf
== NULL
||
2428 ARC_BUF_LAST(hdr
->b_l1hdr
.b_buf
));
2430 old_state
= arc_l2c_only
;
2432 update_old
= B_FALSE
;
2434 update_new
= update_old
;
2435 if (GHOST_STATE(old_state
))
2436 update_old
= B_TRUE
;
2437 if (GHOST_STATE(new_state
))
2438 update_new
= B_TRUE
;
2440 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
2441 ASSERT3P(new_state
, !=, old_state
);
2444 * If this buffer is evictable, transfer it from the
2445 * old state list to the new state list.
2448 if (old_state
!= arc_anon
&& old_state
!= arc_l2c_only
) {
2449 ASSERT(HDR_HAS_L1HDR(hdr
));
2450 /* remove_reference() saves on insert. */
2451 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
2452 multilist_remove(&old_state
->arcs_list
[type
],
2454 arc_evictable_space_decrement(hdr
, old_state
);
2457 if (new_state
!= arc_anon
&& new_state
!= arc_l2c_only
) {
2459 * An L1 header always exists here, since if we're
2460 * moving to some L1-cached state (i.e. not l2c_only or
2461 * anonymous), we realloc the header to add an L1hdr
2464 ASSERT(HDR_HAS_L1HDR(hdr
));
2465 multilist_insert(&new_state
->arcs_list
[type
], hdr
);
2466 arc_evictable_space_increment(hdr
, new_state
);
2470 ASSERT(!HDR_EMPTY(hdr
));
2471 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(hdr
))
2472 buf_hash_remove(hdr
);
2474 /* adjust state sizes (ignore arc_l2c_only) */
2476 if (update_new
&& new_state
!= arc_l2c_only
) {
2477 ASSERT(HDR_HAS_L1HDR(hdr
));
2478 if (GHOST_STATE(new_state
)) {
2481 * When moving a header to a ghost state, we first
2482 * remove all arc buffers. Thus, we'll have no arc
2483 * buffer to use for the reference. As a result, we
2484 * use the arc header pointer for the reference.
2486 (void) zfs_refcount_add_many(
2487 &new_state
->arcs_size
[type
],
2488 HDR_GET_LSIZE(hdr
), hdr
);
2489 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2490 ASSERT(!HDR_HAS_RABD(hdr
));
2494 * Each individual buffer holds a unique reference,
2495 * thus we must remove each of these references one
2498 for (arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2499 buf
= buf
->b_next
) {
2502 * When the arc_buf_t is sharing the data
2503 * block with the hdr, the owner of the
2504 * reference belongs to the hdr. Only
2505 * add to the refcount if the arc_buf_t is
2508 if (ARC_BUF_SHARED(buf
))
2511 (void) zfs_refcount_add_many(
2512 &new_state
->arcs_size
[type
],
2513 arc_buf_size(buf
), buf
);
2516 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2517 (void) zfs_refcount_add_many(
2518 &new_state
->arcs_size
[type
],
2519 arc_hdr_size(hdr
), hdr
);
2522 if (HDR_HAS_RABD(hdr
)) {
2523 (void) zfs_refcount_add_many(
2524 &new_state
->arcs_size
[type
],
2525 HDR_GET_PSIZE(hdr
), hdr
);
2530 if (update_old
&& old_state
!= arc_l2c_only
) {
2531 ASSERT(HDR_HAS_L1HDR(hdr
));
2532 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(
2545 &old_state
->arcs_size
[type
],
2546 HDR_GET_LSIZE(hdr
), hdr
);
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
) {
2558 * When the arc_buf_t is sharing the data
2559 * block with the hdr, the owner of the
2560 * reference belongs to the hdr. Only
2561 * add to the refcount if the arc_buf_t is
2564 if (ARC_BUF_SHARED(buf
))
2567 (void) zfs_refcount_remove_many(
2568 &old_state
->arcs_size
[type
],
2569 arc_buf_size(buf
), buf
);
2571 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
2574 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
2575 (void) zfs_refcount_remove_many(
2576 &old_state
->arcs_size
[type
],
2577 arc_hdr_size(hdr
), hdr
);
2580 if (HDR_HAS_RABD(hdr
)) {
2581 (void) zfs_refcount_remove_many(
2582 &old_state
->arcs_size
[type
],
2583 HDR_GET_PSIZE(hdr
), hdr
);
2588 if (HDR_HAS_L1HDR(hdr
)) {
2589 hdr
->b_l1hdr
.b_state
= new_state
;
2591 if (HDR_HAS_L2HDR(hdr
) && new_state
!= arc_l2c_only
) {
2592 l2arc_hdr_arcstats_decrement_state(hdr
);
2593 hdr
->b_l2hdr
.b_arcs_state
= new_state
->arcs_state
;
2594 l2arc_hdr_arcstats_increment_state(hdr
);
2600 arc_space_consume(uint64_t space
, arc_space_type_t type
)
2602 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2607 case ARC_SPACE_DATA
:
2608 ARCSTAT_INCR(arcstat_data_size
, space
);
2610 case ARC_SPACE_META
:
2611 ARCSTAT_INCR(arcstat_metadata_size
, space
);
2613 case ARC_SPACE_BONUS
:
2614 ARCSTAT_INCR(arcstat_bonus_size
, space
);
2616 case ARC_SPACE_DNODE
:
2617 ARCSTAT_INCR(arcstat_dnode_size
, space
);
2619 case ARC_SPACE_DBUF
:
2620 ARCSTAT_INCR(arcstat_dbuf_size
, space
);
2622 case ARC_SPACE_HDRS
:
2623 ARCSTAT_INCR(arcstat_hdr_size
, space
);
2625 case ARC_SPACE_L2HDRS
:
2626 aggsum_add(&arc_sums
.arcstat_l2_hdr_size
, space
);
2628 case ARC_SPACE_ABD_CHUNK_WASTE
:
2630 * Note: this includes space wasted by all scatter ABD's, not
2631 * just those allocated by the ARC. But the vast majority of
2632 * scatter ABD's come from the ARC, because other users are
2635 ARCSTAT_INCR(arcstat_abd_chunk_waste_size
, space
);
2639 if (type
!= ARC_SPACE_DATA
&& type
!= ARC_SPACE_ABD_CHUNK_WASTE
)
2640 ARCSTAT_INCR(arcstat_meta_used
, space
);
2642 aggsum_add(&arc_sums
.arcstat_size
, space
);
2646 arc_space_return(uint64_t space
, arc_space_type_t type
)
2648 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2653 case ARC_SPACE_DATA
:
2654 ARCSTAT_INCR(arcstat_data_size
, -space
);
2656 case ARC_SPACE_META
:
2657 ARCSTAT_INCR(arcstat_metadata_size
, -space
);
2659 case ARC_SPACE_BONUS
:
2660 ARCSTAT_INCR(arcstat_bonus_size
, -space
);
2662 case ARC_SPACE_DNODE
:
2663 ARCSTAT_INCR(arcstat_dnode_size
, -space
);
2665 case ARC_SPACE_DBUF
:
2666 ARCSTAT_INCR(arcstat_dbuf_size
, -space
);
2668 case ARC_SPACE_HDRS
:
2669 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
2671 case ARC_SPACE_L2HDRS
:
2672 aggsum_add(&arc_sums
.arcstat_l2_hdr_size
, -space
);
2674 case ARC_SPACE_ABD_CHUNK_WASTE
:
2675 ARCSTAT_INCR(arcstat_abd_chunk_waste_size
, -space
);
2679 if (type
!= ARC_SPACE_DATA
&& type
!= ARC_SPACE_ABD_CHUNK_WASTE
)
2680 ARCSTAT_INCR(arcstat_meta_used
, -space
);
2682 ASSERT(aggsum_compare(&arc_sums
.arcstat_size
, space
) >= 0);
2683 aggsum_add(&arc_sums
.arcstat_size
, -space
);
2687 * Given a hdr and a buf, returns whether that buf can share its b_data buffer
2688 * with the hdr's b_pabd.
2691 arc_can_share(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2694 * The criteria for sharing a hdr's data are:
2695 * 1. the buffer is not encrypted
2696 * 2. the hdr's compression matches the buf's compression
2697 * 3. the hdr doesn't need to be byteswapped
2698 * 4. the hdr isn't already being shared
2699 * 5. the buf is either compressed or it is the last buf in the hdr list
2701 * Criterion #5 maintains the invariant that shared uncompressed
2702 * bufs must be the final buf in the hdr's b_buf list. Reading this, you
2703 * might ask, "if a compressed buf is allocated first, won't that be the
2704 * last thing in the list?", but in that case it's impossible to create
2705 * a shared uncompressed buf anyway (because the hdr must be compressed
2706 * to have the compressed buf). You might also think that #3 is
2707 * sufficient to make this guarantee, however it's possible
2708 * (specifically in the rare L2ARC write race mentioned in
2709 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that
2710 * is shareable, but wasn't at the time of its allocation. Rather than
2711 * allow a new shared uncompressed buf to be created and then shuffle
2712 * the list around to make it the last element, this simply disallows
2713 * sharing if the new buf isn't the first to be added.
2715 ASSERT3P(buf
->b_hdr
, ==, hdr
);
2716 boolean_t hdr_compressed
=
2717 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
;
2718 boolean_t buf_compressed
= ARC_BUF_COMPRESSED(buf
) != 0;
2719 return (!ARC_BUF_ENCRYPTED(buf
) &&
2720 buf_compressed
== hdr_compressed
&&
2721 hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
&&
2722 !HDR_SHARED_DATA(hdr
) &&
2723 (ARC_BUF_LAST(buf
) || ARC_BUF_COMPRESSED(buf
)));
2727 * Allocate a buf for this hdr. If you care about the data that's in the hdr,
2728 * or if you want a compressed buffer, pass those flags in. Returns 0 if the
2729 * copy was made successfully, or an error code otherwise.
2732 arc_buf_alloc_impl(arc_buf_hdr_t
*hdr
, spa_t
*spa
, const zbookmark_phys_t
*zb
,
2733 const void *tag
, boolean_t encrypted
, boolean_t compressed
,
2734 boolean_t noauth
, boolean_t fill
, arc_buf_t
**ret
)
2737 arc_fill_flags_t flags
= ARC_FILL_LOCKED
;
2739 ASSERT(HDR_HAS_L1HDR(hdr
));
2740 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
2741 VERIFY(hdr
->b_type
== ARC_BUFC_DATA
||
2742 hdr
->b_type
== ARC_BUFC_METADATA
);
2743 ASSERT3P(ret
, !=, NULL
);
2744 ASSERT3P(*ret
, ==, NULL
);
2745 IMPLY(encrypted
, compressed
);
2747 buf
= *ret
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
2750 buf
->b_next
= hdr
->b_l1hdr
.b_buf
;
2753 add_reference(hdr
, tag
);
2756 * We're about to change the hdr's b_flags. We must either
2757 * hold the hash_lock or be undiscoverable.
2759 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
2762 * Only honor requests for compressed bufs if the hdr is actually
2763 * compressed. This must be overridden if the buffer is encrypted since
2764 * encrypted buffers cannot be decompressed.
2767 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2768 buf
->b_flags
|= ARC_BUF_FLAG_ENCRYPTED
;
2769 flags
|= ARC_FILL_COMPRESSED
| ARC_FILL_ENCRYPTED
;
2770 } else if (compressed
&&
2771 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
2772 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2773 flags
|= ARC_FILL_COMPRESSED
;
2778 flags
|= ARC_FILL_NOAUTH
;
2782 * If the hdr's data can be shared then we share the data buffer and
2783 * set the appropriate bit in the hdr's b_flags to indicate the hdr is
2784 * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new
2785 * buffer to store the buf's data.
2787 * There are two additional restrictions here because we're sharing
2788 * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be
2789 * actively involved in an L2ARC write, because if this buf is used by
2790 * an arc_write() then the hdr's data buffer will be released when the
2791 * write completes, even though the L2ARC write might still be using it.
2792 * Second, the hdr's ABD must be linear so that the buf's user doesn't
2793 * need to be ABD-aware. It must be allocated via
2794 * zio_[data_]buf_alloc(), not as a page, because we need to be able
2795 * to abd_release_ownership_of_buf(), which isn't allowed on "linear
2796 * page" buffers because the ABD code needs to handle freeing them
2799 boolean_t can_share
= arc_can_share(hdr
, buf
) &&
2800 !HDR_L2_WRITING(hdr
) &&
2801 hdr
->b_l1hdr
.b_pabd
!= NULL
&&
2802 abd_is_linear(hdr
->b_l1hdr
.b_pabd
) &&
2803 !abd_is_linear_page(hdr
->b_l1hdr
.b_pabd
);
2805 /* Set up b_data and sharing */
2807 buf
->b_data
= abd_to_buf(hdr
->b_l1hdr
.b_pabd
);
2808 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2809 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2812 arc_get_data_buf(hdr
, arc_buf_size(buf
), buf
);
2813 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
2815 VERIFY3P(buf
->b_data
, !=, NULL
);
2817 hdr
->b_l1hdr
.b_buf
= buf
;
2820 * If the user wants the data from the hdr, we need to either copy or
2821 * decompress the data.
2824 ASSERT3P(zb
, !=, NULL
);
2825 return (arc_buf_fill(buf
, spa
, zb
, flags
));
2831 static const char *arc_onloan_tag
= "onloan";
2834 arc_loaned_bytes_update(int64_t delta
)
2836 atomic_add_64(&arc_loaned_bytes
, delta
);
2838 /* assert that it did not wrap around */
2839 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
2843 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
2844 * flight data by arc_tempreserve_space() until they are "returned". Loaned
2845 * buffers must be returned to the arc before they can be used by the DMU or
2849 arc_loan_buf(spa_t
*spa
, boolean_t is_metadata
, int size
)
2851 arc_buf_t
*buf
= arc_alloc_buf(spa
, arc_onloan_tag
,
2852 is_metadata
? ARC_BUFC_METADATA
: ARC_BUFC_DATA
, size
);
2854 arc_loaned_bytes_update(arc_buf_size(buf
));
2860 arc_loan_compressed_buf(spa_t
*spa
, uint64_t psize
, uint64_t lsize
,
2861 enum zio_compress compression_type
, uint8_t complevel
)
2863 arc_buf_t
*buf
= arc_alloc_compressed_buf(spa
, arc_onloan_tag
,
2864 psize
, lsize
, compression_type
, complevel
);
2866 arc_loaned_bytes_update(arc_buf_size(buf
));
2872 arc_loan_raw_buf(spa_t
*spa
, uint64_t dsobj
, boolean_t byteorder
,
2873 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
,
2874 dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
2875 enum zio_compress compression_type
, uint8_t complevel
)
2877 arc_buf_t
*buf
= arc_alloc_raw_buf(spa
, arc_onloan_tag
, dsobj
,
2878 byteorder
, salt
, iv
, mac
, ot
, psize
, lsize
, compression_type
,
2881 atomic_add_64(&arc_loaned_bytes
, psize
);
2887 * Return a loaned arc buffer to the arc.
2890 arc_return_buf(arc_buf_t
*buf
, const void *tag
)
2892 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2894 ASSERT3P(buf
->b_data
, !=, NULL
);
2895 ASSERT(HDR_HAS_L1HDR(hdr
));
2896 (void) zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2897 (void) zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2899 arc_loaned_bytes_update(-arc_buf_size(buf
));
2902 /* Detach an arc_buf from a dbuf (tag) */
2904 arc_loan_inuse_buf(arc_buf_t
*buf
, const void *tag
)
2906 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2908 ASSERT3P(buf
->b_data
, !=, NULL
);
2909 ASSERT(HDR_HAS_L1HDR(hdr
));
2910 (void) zfs_refcount_add(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2911 (void) zfs_refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2913 arc_loaned_bytes_update(arc_buf_size(buf
));
2917 l2arc_free_abd_on_write(abd_t
*abd
, size_t size
, arc_buf_contents_t type
)
2919 l2arc_data_free_t
*df
= kmem_alloc(sizeof (*df
), KM_SLEEP
);
2922 df
->l2df_size
= size
;
2923 df
->l2df_type
= type
;
2924 mutex_enter(&l2arc_free_on_write_mtx
);
2925 list_insert_head(l2arc_free_on_write
, df
);
2926 mutex_exit(&l2arc_free_on_write_mtx
);
2930 arc_hdr_free_on_write(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
2932 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2933 arc_buf_contents_t type
= arc_buf_type(hdr
);
2934 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
2936 /* protected by hash lock, if in the hash table */
2937 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
2938 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2939 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
2941 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
2944 (void) zfs_refcount_remove_many(&state
->arcs_size
[type
], size
, hdr
);
2945 if (type
== ARC_BUFC_METADATA
) {
2946 arc_space_return(size
, ARC_SPACE_META
);
2948 ASSERT(type
== ARC_BUFC_DATA
);
2949 arc_space_return(size
, ARC_SPACE_DATA
);
2953 l2arc_free_abd_on_write(hdr
->b_crypt_hdr
.b_rabd
, size
, type
);
2955 l2arc_free_abd_on_write(hdr
->b_l1hdr
.b_pabd
, size
, type
);
2960 * Share the arc_buf_t's data with the hdr. Whenever we are sharing the
2961 * data buffer, we transfer the refcount ownership to the hdr and update
2962 * the appropriate kstats.
2965 arc_share_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2967 ASSERT(arc_can_share(hdr
, buf
));
2968 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
2969 ASSERT(!ARC_BUF_ENCRYPTED(buf
));
2970 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
2973 * Start sharing the data buffer. We transfer the
2974 * refcount ownership to the hdr since it always owns
2975 * the refcount whenever an arc_buf_t is shared.
2977 zfs_refcount_transfer_ownership_many(
2978 &hdr
->b_l1hdr
.b_state
->arcs_size
[arc_buf_type(hdr
)],
2979 arc_hdr_size(hdr
), buf
, hdr
);
2980 hdr
->b_l1hdr
.b_pabd
= abd_get_from_buf(buf
->b_data
, arc_buf_size(buf
));
2981 abd_take_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
,
2982 HDR_ISTYPE_METADATA(hdr
));
2983 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2984 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2987 * Since we've transferred ownership to the hdr we need
2988 * to increment its compressed and uncompressed kstats and
2989 * decrement the overhead size.
2991 ARCSTAT_INCR(arcstat_compressed_size
, arc_hdr_size(hdr
));
2992 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
2993 ARCSTAT_INCR(arcstat_overhead_size
, -arc_buf_size(buf
));
2997 arc_unshare_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2999 ASSERT(arc_buf_is_shared(buf
));
3000 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3001 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3004 * We are no longer sharing this buffer so we need
3005 * to transfer its ownership to the rightful owner.
3007 zfs_refcount_transfer_ownership_many(
3008 &hdr
->b_l1hdr
.b_state
->arcs_size
[arc_buf_type(hdr
)],
3009 arc_hdr_size(hdr
), hdr
, buf
);
3010 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3011 abd_release_ownership_of_buf(hdr
->b_l1hdr
.b_pabd
);
3012 abd_free(hdr
->b_l1hdr
.b_pabd
);
3013 hdr
->b_l1hdr
.b_pabd
= NULL
;
3014 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
3017 * Since the buffer is no longer shared between
3018 * the arc buf and the hdr, count it as overhead.
3020 ARCSTAT_INCR(arcstat_compressed_size
, -arc_hdr_size(hdr
));
3021 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3022 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
3026 * Remove an arc_buf_t from the hdr's buf list and return the last
3027 * arc_buf_t on the list. If no buffers remain on the list then return
3031 arc_buf_remove(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
3033 ASSERT(HDR_HAS_L1HDR(hdr
));
3034 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3036 arc_buf_t
**bufp
= &hdr
->b_l1hdr
.b_buf
;
3037 arc_buf_t
*lastbuf
= NULL
;
3040 * Remove the buf from the hdr list and locate the last
3041 * remaining buffer on the list.
3043 while (*bufp
!= NULL
) {
3045 *bufp
= buf
->b_next
;
3048 * If we've removed a buffer in the middle of
3049 * the list then update the lastbuf and update
3052 if (*bufp
!= NULL
) {
3054 bufp
= &(*bufp
)->b_next
;
3058 ASSERT3P(lastbuf
, !=, buf
);
3059 IMPLY(lastbuf
!= NULL
, ARC_BUF_LAST(lastbuf
));
3065 * Free up buf->b_data and pull the arc_buf_t off of the arc_buf_hdr_t's
3069 arc_buf_destroy_impl(arc_buf_t
*buf
)
3071 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3074 * Free up the data associated with the buf but only if we're not
3075 * sharing this with the hdr. If we are sharing it with the hdr, the
3076 * hdr is responsible for doing the free.
3078 if (buf
->b_data
!= NULL
) {
3080 * We're about to change the hdr's b_flags. We must either
3081 * hold the hash_lock or be undiscoverable.
3083 ASSERT(HDR_EMPTY_OR_LOCKED(hdr
));
3085 arc_cksum_verify(buf
);
3086 arc_buf_unwatch(buf
);
3088 if (ARC_BUF_SHARED(buf
)) {
3089 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
3091 ASSERT(!arc_buf_is_shared(buf
));
3092 uint64_t size
= arc_buf_size(buf
);
3093 arc_free_data_buf(hdr
, buf
->b_data
, size
, buf
);
3094 ARCSTAT_INCR(arcstat_overhead_size
, -size
);
3099 * If we have no more encrypted buffers and we've already
3100 * gotten a copy of the decrypted data we can free b_rabd
3101 * to save some space.
3103 if (ARC_BUF_ENCRYPTED(buf
) && HDR_HAS_RABD(hdr
) &&
3104 hdr
->b_l1hdr
.b_pabd
!= NULL
&& !HDR_IO_IN_PROGRESS(hdr
)) {
3106 for (b
= hdr
->b_l1hdr
.b_buf
; b
; b
= b
->b_next
) {
3107 if (b
!= buf
&& ARC_BUF_ENCRYPTED(b
))
3111 arc_hdr_free_abd(hdr
, B_TRUE
);
3115 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
3117 if (ARC_BUF_SHARED(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
3119 * If the current arc_buf_t is sharing its data buffer with the
3120 * hdr, then reassign the hdr's b_pabd to share it with the new
3121 * buffer at the end of the list. The shared buffer is always
3122 * the last one on the hdr's buffer list.
3124 * There is an equivalent case for compressed bufs, but since
3125 * they aren't guaranteed to be the last buf in the list and
3126 * that is an exceedingly rare case, we just allow that space be
3127 * wasted temporarily. We must also be careful not to share
3128 * encrypted buffers, since they cannot be shared.
3130 if (lastbuf
!= NULL
&& !ARC_BUF_ENCRYPTED(lastbuf
)) {
3131 /* Only one buf can be shared at once */
3132 ASSERT(!arc_buf_is_shared(lastbuf
));
3133 /* hdr is uncompressed so can't have compressed buf */
3134 ASSERT(!ARC_BUF_COMPRESSED(lastbuf
));
3136 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3137 arc_hdr_free_abd(hdr
, B_FALSE
);
3140 * We must setup a new shared block between the
3141 * last buffer and the hdr. The data would have
3142 * been allocated by the arc buf so we need to transfer
3143 * ownership to the hdr since it's now being shared.
3145 arc_share_buf(hdr
, lastbuf
);
3147 } else if (HDR_SHARED_DATA(hdr
)) {
3149 * Uncompressed shared buffers are always at the end
3150 * of the list. Compressed buffers don't have the
3151 * same requirements. This makes it hard to
3152 * simply assert that the lastbuf is shared so
3153 * we rely on the hdr's compression flags to determine
3154 * if we have a compressed, shared buffer.
3156 ASSERT3P(lastbuf
, !=, NULL
);
3157 ASSERT(arc_buf_is_shared(lastbuf
) ||
3158 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
3162 * Free the checksum if we're removing the last uncompressed buf from
3165 if (!arc_hdr_has_uncompressed_buf(hdr
)) {
3166 arc_cksum_free(hdr
);
3169 /* clean up the buf */
3171 kmem_cache_free(buf_cache
, buf
);
3175 arc_hdr_alloc_abd(arc_buf_hdr_t
*hdr
, int alloc_flags
)
3178 boolean_t alloc_rdata
= ((alloc_flags
& ARC_HDR_ALLOC_RDATA
) != 0);
3180 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
3181 ASSERT(HDR_HAS_L1HDR(hdr
));
3182 ASSERT(!HDR_SHARED_DATA(hdr
) || alloc_rdata
);
3183 IMPLY(alloc_rdata
, HDR_PROTECTED(hdr
));
3186 size
= HDR_GET_PSIZE(hdr
);
3187 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, ==, NULL
);
3188 hdr
->b_crypt_hdr
.b_rabd
= arc_get_data_abd(hdr
, size
, hdr
,
3190 ASSERT3P(hdr
->b_crypt_hdr
.b_rabd
, !=, NULL
);
3191 ARCSTAT_INCR(arcstat_raw_size
, size
);
3193 size
= arc_hdr_size(hdr
);
3194 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3195 hdr
->b_l1hdr
.b_pabd
= arc_get_data_abd(hdr
, size
, hdr
,
3197 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
3200 ARCSTAT_INCR(arcstat_compressed_size
, size
);
3201 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
3205 arc_hdr_free_abd(arc_buf_hdr_t
*hdr
, boolean_t free_rdata
)
3207 uint64_t size
= (free_rdata
) ? HDR_GET_PSIZE(hdr
) : arc_hdr_size(hdr
);
3209 ASSERT(HDR_HAS_L1HDR(hdr
));
3210 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
3211 IMPLY(free_rdata
, HDR_HAS_RABD(hdr
));
3214 * If the hdr is currently being written to the l2arc then
3215 * we defer freeing the data by adding it to the l2arc_free_on_write
3216 * list. The l2arc will free the data once it's finished
3217 * writing it to the l2arc device.
3219 if (HDR_L2_WRITING(hdr
)) {
3220 arc_hdr_free_on_write(hdr
, free_rdata
);
3221 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
3222 } else if (free_rdata
) {
3223 arc_free_data_abd(hdr
, hdr
->b_crypt_hdr
.b_rabd
, size
, hdr
);
3225 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
, size
, hdr
);
3229 hdr
->b_crypt_hdr
.b_rabd
= NULL
;
3230 ARCSTAT_INCR(arcstat_raw_size
, -size
);
3232 hdr
->b_l1hdr
.b_pabd
= NULL
;
3235 if (hdr
->b_l1hdr
.b_pabd
== NULL
&& !HDR_HAS_RABD(hdr
))
3236 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
3238 ARCSTAT_INCR(arcstat_compressed_size
, -size
);
3239 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
3243 * Allocate empty anonymous ARC header. The header will get its identity
3244 * assigned and buffers attached later as part of read or write operations.
3246 * In case of read arc_read() assigns header its identify (b_dva + b_birth),
3247 * inserts it into ARC hash to become globally visible and allocates physical
3248 * (b_pabd) or raw (b_rabd) ABD buffer to read into from disk. On disk read
3249 * completion arc_read_done() allocates ARC buffer(s) as needed, potentially
3250 * sharing one of them with the physical ABD buffer.
3252 * In case of write arc_alloc_buf() allocates ARC buffer to be filled with
3253 * data. Then after compression and/or encryption arc_write_ready() allocates
3254 * and fills (or potentially shares) physical (b_pabd) or raw (b_rabd) ABD
3255 * buffer. On disk write completion arc_write_done() assigns the header its
3256 * new identity (b_dva + b_birth) and inserts into ARC hash.
3258 * In case of partial overwrite the old data is read first as described. Then
3259 * arc_release() either allocates new anonymous ARC header and moves the ARC
3260 * buffer to it, or reuses the old ARC header by discarding its identity and
3261 * removing it from ARC hash. After buffer modification normal write process
3262 * follows as described.
3264 static arc_buf_hdr_t
*
3265 arc_hdr_alloc(uint64_t spa
, int32_t psize
, int32_t lsize
,
3266 boolean_t
protected, enum zio_compress compression_type
, uint8_t complevel
,
3267 arc_buf_contents_t type
)
3271 VERIFY(type
== ARC_BUFC_DATA
|| type
== ARC_BUFC_METADATA
);
3272 hdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
3274 ASSERT(HDR_EMPTY(hdr
));
3276 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3278 HDR_SET_PSIZE(hdr
, psize
);
3279 HDR_SET_LSIZE(hdr
, lsize
);
3283 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L1HDR
);
3284 arc_hdr_set_compress(hdr
, compression_type
);
3285 hdr
->b_complevel
= complevel
;
3287 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
3289 hdr
->b_l1hdr
.b_state
= arc_anon
;
3290 hdr
->b_l1hdr
.b_arc_access
= 0;
3291 hdr
->b_l1hdr
.b_mru_hits
= 0;
3292 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
3293 hdr
->b_l1hdr
.b_mfu_hits
= 0;
3294 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
3295 hdr
->b_l1hdr
.b_buf
= NULL
;
3297 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3303 * Transition between the two allocation states for the arc_buf_hdr struct.
3304 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
3305 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
3306 * version is used when a cache buffer is only in the L2ARC in order to reduce
3309 static arc_buf_hdr_t
*
3310 arc_hdr_realloc(arc_buf_hdr_t
*hdr
, kmem_cache_t
*old
, kmem_cache_t
*new)
3312 ASSERT(HDR_HAS_L2HDR(hdr
));
3314 arc_buf_hdr_t
*nhdr
;
3315 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3317 ASSERT((old
== hdr_full_cache
&& new == hdr_l2only_cache
) ||
3318 (old
== hdr_l2only_cache
&& new == hdr_full_cache
));
3320 nhdr
= kmem_cache_alloc(new, KM_PUSHPAGE
);
3322 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
3323 buf_hash_remove(hdr
);
3325 memcpy(nhdr
, hdr
, HDR_L2ONLY_SIZE
);
3327 if (new == hdr_full_cache
) {
3328 arc_hdr_set_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3330 * arc_access and arc_change_state need to be aware that a
3331 * header has just come out of L2ARC, so we set its state to
3332 * l2c_only even though it's about to change.
3334 nhdr
->b_l1hdr
.b_state
= arc_l2c_only
;
3336 /* Verify previous threads set to NULL before freeing */
3337 ASSERT3P(nhdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3338 ASSERT(!HDR_HAS_RABD(hdr
));
3340 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3342 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3346 * If we've reached here, We must have been called from
3347 * arc_evict_hdr(), as such we should have already been
3348 * removed from any ghost list we were previously on
3349 * (which protects us from racing with arc_evict_state),
3350 * thus no locking is needed during this check.
3352 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3355 * A buffer must not be moved into the arc_l2c_only
3356 * state if it's not finished being written out to the
3357 * l2arc device. Otherwise, the b_l1hdr.b_pabd field
3358 * might try to be accessed, even though it was removed.
3360 VERIFY(!HDR_L2_WRITING(hdr
));
3361 VERIFY3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
3362 ASSERT(!HDR_HAS_RABD(hdr
));
3364 arc_hdr_clear_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
3367 * The header has been reallocated so we need to re-insert it into any
3370 (void) buf_hash_insert(nhdr
, NULL
);
3372 ASSERT(list_link_active(&hdr
->b_l2hdr
.b_l2node
));
3374 mutex_enter(&dev
->l2ad_mtx
);
3377 * We must place the realloc'ed header back into the list at
3378 * the same spot. Otherwise, if it's placed earlier in the list,
3379 * l2arc_write_buffers() could find it during the function's
3380 * write phase, and try to write it out to the l2arc.
3382 list_insert_after(&dev
->l2ad_buflist
, hdr
, nhdr
);
3383 list_remove(&dev
->l2ad_buflist
, hdr
);
3385 mutex_exit(&dev
->l2ad_mtx
);
3388 * Since we're using the pointer address as the tag when
3389 * incrementing and decrementing the l2ad_alloc refcount, we
3390 * must remove the old pointer (that we're about to destroy) and
3391 * add the new pointer to the refcount. Otherwise we'd remove
3392 * the wrong pointer address when calling arc_hdr_destroy() later.
3395 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
,
3396 arc_hdr_size(hdr
), hdr
);
3397 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
3398 arc_hdr_size(nhdr
), nhdr
);
3400 buf_discard_identity(hdr
);
3401 kmem_cache_free(old
, hdr
);
3407 * This function is used by the send / receive code to convert a newly
3408 * allocated arc_buf_t to one that is suitable for a raw encrypted write. It
3409 * is also used to allow the root objset block to be updated without altering
3410 * its embedded MACs. Both block types will always be uncompressed so we do not
3411 * have to worry about compression type or psize.
3414 arc_convert_to_raw(arc_buf_t
*buf
, uint64_t dsobj
, boolean_t byteorder
,
3415 dmu_object_type_t ot
, const uint8_t *salt
, const uint8_t *iv
,
3418 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3420 ASSERT(ot
== DMU_OT_DNODE
|| ot
== DMU_OT_OBJSET
);
3421 ASSERT(HDR_HAS_L1HDR(hdr
));
3422 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3424 buf
->b_flags
|= (ARC_BUF_FLAG_COMPRESSED
| ARC_BUF_FLAG_ENCRYPTED
);
3425 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
3426 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3427 hdr
->b_crypt_hdr
.b_ot
= ot
;
3428 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3429 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3430 if (!arc_hdr_has_uncompressed_buf(hdr
))
3431 arc_cksum_free(hdr
);
3434 memcpy(hdr
->b_crypt_hdr
.b_salt
, salt
, ZIO_DATA_SALT_LEN
);
3436 memcpy(hdr
->b_crypt_hdr
.b_iv
, iv
, ZIO_DATA_IV_LEN
);
3438 memcpy(hdr
->b_crypt_hdr
.b_mac
, mac
, ZIO_DATA_MAC_LEN
);
3442 * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller.
3443 * The buf is returned thawed since we expect the consumer to modify it.
3446 arc_alloc_buf(spa_t
*spa
, const void *tag
, arc_buf_contents_t type
,
3449 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), size
, size
,
3450 B_FALSE
, ZIO_COMPRESS_OFF
, 0, type
);
3452 arc_buf_t
*buf
= NULL
;
3453 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
, B_FALSE
,
3454 B_FALSE
, B_FALSE
, &buf
));
3461 * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this
3462 * for bufs containing metadata.
3465 arc_alloc_compressed_buf(spa_t
*spa
, const void *tag
, uint64_t psize
,
3466 uint64_t lsize
, enum zio_compress compression_type
, uint8_t complevel
)
3468 ASSERT3U(lsize
, >, 0);
3469 ASSERT3U(lsize
, >=, psize
);
3470 ASSERT3U(compression_type
, >, ZIO_COMPRESS_OFF
);
3471 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3473 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
3474 B_FALSE
, compression_type
, complevel
, ARC_BUFC_DATA
);
3476 arc_buf_t
*buf
= NULL
;
3477 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_FALSE
,
3478 B_TRUE
, B_FALSE
, B_FALSE
, &buf
));
3482 * To ensure that the hdr has the correct data in it if we call
3483 * arc_untransform() on this buf before it's been written to disk,
3484 * it's easiest if we just set up sharing between the buf and the hdr.
3486 arc_share_buf(hdr
, buf
);
3492 arc_alloc_raw_buf(spa_t
*spa
, const void *tag
, uint64_t dsobj
,
3493 boolean_t byteorder
, const uint8_t *salt
, const uint8_t *iv
,
3494 const uint8_t *mac
, dmu_object_type_t ot
, uint64_t psize
, uint64_t lsize
,
3495 enum zio_compress compression_type
, uint8_t complevel
)
3499 arc_buf_contents_t type
= DMU_OT_IS_METADATA(ot
) ?
3500 ARC_BUFC_METADATA
: ARC_BUFC_DATA
;
3502 ASSERT3U(lsize
, >, 0);
3503 ASSERT3U(lsize
, >=, psize
);
3504 ASSERT3U(compression_type
, >=, ZIO_COMPRESS_OFF
);
3505 ASSERT3U(compression_type
, <, ZIO_COMPRESS_FUNCTIONS
);
3507 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
, B_TRUE
,
3508 compression_type
, complevel
, type
);
3510 hdr
->b_crypt_hdr
.b_dsobj
= dsobj
;
3511 hdr
->b_crypt_hdr
.b_ot
= ot
;
3512 hdr
->b_l1hdr
.b_byteswap
= (byteorder
== ZFS_HOST_BYTEORDER
) ?
3513 DMU_BSWAP_NUMFUNCS
: DMU_OT_BYTESWAP(ot
);
3514 memcpy(hdr
->b_crypt_hdr
.b_salt
, salt
, ZIO_DATA_SALT_LEN
);
3515 memcpy(hdr
->b_crypt_hdr
.b_iv
, iv
, ZIO_DATA_IV_LEN
);
3516 memcpy(hdr
->b_crypt_hdr
.b_mac
, mac
, ZIO_DATA_MAC_LEN
);
3519 * This buffer will be considered encrypted even if the ot is not an
3520 * encrypted type. It will become authenticated instead in
3521 * arc_write_ready().
3524 VERIFY0(arc_buf_alloc_impl(hdr
, spa
, NULL
, tag
, B_TRUE
, B_TRUE
,
3525 B_FALSE
, B_FALSE
, &buf
));
3532 l2arc_hdr_arcstats_update(arc_buf_hdr_t
*hdr
, boolean_t incr
,
3533 boolean_t state_only
)
3535 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
3536 uint64_t psize
= HDR_GET_PSIZE(hdr
);
3537 uint64_t asize
= HDR_GET_L2SIZE(hdr
);
3538 arc_buf_contents_t type
= hdr
->b_type
;
3543 /* For L2 we expect the header's b_l2size to be valid */
3544 ASSERT3U(asize
, >=, psize
);
3556 /* If the buffer is a prefetch, count it as such. */
3557 if (HDR_PREFETCH(hdr
)) {
3558 ARCSTAT_INCR(arcstat_l2_prefetch_asize
, asize_s
);
3561 * We use the value stored in the L2 header upon initial
3562 * caching in L2ARC. This value will be updated in case
3563 * an MRU/MRU_ghost buffer transitions to MFU but the L2ARC
3564 * metadata (log entry) cannot currently be updated. Having
3565 * the ARC state in the L2 header solves the problem of a
3566 * possibly absent L1 header (apparent in buffers restored
3567 * from persistent L2ARC).
3569 switch (hdr
->b_l2hdr
.b_arcs_state
) {
3570 case ARC_STATE_MRU_GHOST
:
3572 ARCSTAT_INCR(arcstat_l2_mru_asize
, asize_s
);
3574 case ARC_STATE_MFU_GHOST
:
3576 ARCSTAT_INCR(arcstat_l2_mfu_asize
, asize_s
);
3586 ARCSTAT_INCR(arcstat_l2_psize
, psize_s
);
3587 ARCSTAT_INCR(arcstat_l2_lsize
, lsize_s
);
3591 ARCSTAT_INCR(arcstat_l2_bufc_data_asize
, asize_s
);
3593 case ARC_BUFC_METADATA
:
3594 ARCSTAT_INCR(arcstat_l2_bufc_metadata_asize
, asize_s
);
3603 arc_hdr_l2hdr_destroy(arc_buf_hdr_t
*hdr
)
3605 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
3606 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
3608 ASSERT(MUTEX_HELD(&dev
->l2ad_mtx
));
3609 ASSERT(HDR_HAS_L2HDR(hdr
));
3611 list_remove(&dev
->l2ad_buflist
, hdr
);
3613 l2arc_hdr_arcstats_decrement(hdr
);
3614 if (dev
->l2ad_vdev
!= NULL
) {
3615 uint64_t asize
= HDR_GET_L2SIZE(hdr
);
3616 vdev_space_update(dev
->l2ad_vdev
, -asize
, 0, 0);
3619 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
),
3621 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
3625 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
3627 if (HDR_HAS_L1HDR(hdr
)) {
3628 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3629 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
3631 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3632 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
3634 if (HDR_HAS_L2HDR(hdr
)) {
3635 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
3636 boolean_t buflist_held
= MUTEX_HELD(&dev
->l2ad_mtx
);
3639 mutex_enter(&dev
->l2ad_mtx
);
3642 * Even though we checked this conditional above, we
3643 * need to check this again now that we have the
3644 * l2ad_mtx. This is because we could be racing with
3645 * another thread calling l2arc_evict() which might have
3646 * destroyed this header's L2 portion as we were waiting
3647 * to acquire the l2ad_mtx. If that happens, we don't
3648 * want to re-destroy the header's L2 portion.
3650 if (HDR_HAS_L2HDR(hdr
)) {
3652 if (!HDR_EMPTY(hdr
))
3653 buf_discard_identity(hdr
);
3655 arc_hdr_l2hdr_destroy(hdr
);
3659 mutex_exit(&dev
->l2ad_mtx
);
3663 * The header's identify can only be safely discarded once it is no
3664 * longer discoverable. This requires removing it from the hash table
3665 * and the l2arc header list. After this point the hash lock can not
3666 * be used to protect the header.
3668 if (!HDR_EMPTY(hdr
))
3669 buf_discard_identity(hdr
);
3671 if (HDR_HAS_L1HDR(hdr
)) {
3672 arc_cksum_free(hdr
);
3674 while (hdr
->b_l1hdr
.b_buf
!= NULL
)
3675 arc_buf_destroy_impl(hdr
->b_l1hdr
.b_buf
);
3677 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
3678 arc_hdr_free_abd(hdr
, B_FALSE
);
3680 if (HDR_HAS_RABD(hdr
))
3681 arc_hdr_free_abd(hdr
, B_TRUE
);
3684 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3685 if (HDR_HAS_L1HDR(hdr
)) {
3686 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3687 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
3689 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
3691 kmem_cache_free(hdr_full_cache
, hdr
);
3693 kmem_cache_free(hdr_l2only_cache
, hdr
);
3698 arc_buf_destroy(arc_buf_t
*buf
, const void *tag
)
3700 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3702 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
3703 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, buf
);
3704 ASSERT(ARC_BUF_LAST(buf
));
3705 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3706 VERIFY0(remove_reference(hdr
, tag
));
3710 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
3711 mutex_enter(hash_lock
);
3713 ASSERT3P(hdr
, ==, buf
->b_hdr
);
3714 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, NULL
);
3715 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3716 ASSERT3P(hdr
->b_l1hdr
.b_state
, !=, arc_anon
);
3717 ASSERT3P(buf
->b_data
, !=, NULL
);
3719 arc_buf_destroy_impl(buf
);
3720 (void) remove_reference(hdr
, tag
);
3721 mutex_exit(hash_lock
);
3725 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
3726 * state of the header is dependent on its state prior to entering this
3727 * function. The following transitions are possible:
3729 * - arc_mru -> arc_mru_ghost
3730 * - arc_mfu -> arc_mfu_ghost
3731 * - arc_mru_ghost -> arc_l2c_only
3732 * - arc_mru_ghost -> deleted
3733 * - arc_mfu_ghost -> arc_l2c_only
3734 * - arc_mfu_ghost -> deleted
3735 * - arc_uncached -> deleted
3737 * Return total size of evicted data buffers for eviction progress tracking.
3738 * When evicting from ghost states return logical buffer size to make eviction
3739 * progress at the same (or at least comparable) rate as from non-ghost states.
3741 * Return *real_evicted for actual ARC size reduction to wake up threads
3742 * waiting for it. For non-ghost states it includes size of evicted data
3743 * buffers (the headers are not freed there). For ghost states it includes
3744 * only the evicted headers size.
3747 arc_evict_hdr(arc_buf_hdr_t
*hdr
, uint64_t *real_evicted
)
3749 arc_state_t
*evicted_state
, *state
;
3750 int64_t bytes_evicted
= 0;
3751 uint_t min_lifetime
= HDR_PRESCIENT_PREFETCH(hdr
) ?
3752 arc_min_prescient_prefetch_ms
: arc_min_prefetch_ms
;
3754 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
3755 ASSERT(HDR_HAS_L1HDR(hdr
));
3756 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3757 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3758 ASSERT0(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3761 state
= hdr
->b_l1hdr
.b_state
;
3762 if (GHOST_STATE(state
)) {
3765 * l2arc_write_buffers() relies on a header's L1 portion
3766 * (i.e. its b_pabd field) during it's write phase.
3767 * Thus, we cannot push a header onto the arc_l2c_only
3768 * state (removing its L1 piece) until the header is
3769 * done being written to the l2arc.
3771 if (HDR_HAS_L2HDR(hdr
) && HDR_L2_WRITING(hdr
)) {
3772 ARCSTAT_BUMP(arcstat_evict_l2_skip
);
3773 return (bytes_evicted
);
3776 ARCSTAT_BUMP(arcstat_deleted
);
3777 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3779 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, hdr
);
3781 if (HDR_HAS_L2HDR(hdr
)) {
3782 ASSERT(hdr
->b_l1hdr
.b_pabd
== NULL
);
3783 ASSERT(!HDR_HAS_RABD(hdr
));
3785 * This buffer is cached on the 2nd Level ARC;
3786 * don't destroy the header.
3788 arc_change_state(arc_l2c_only
, hdr
);
3790 * dropping from L1+L2 cached to L2-only,
3791 * realloc to remove the L1 header.
3793 (void) arc_hdr_realloc(hdr
, hdr_full_cache
,
3795 *real_evicted
+= HDR_FULL_SIZE
- HDR_L2ONLY_SIZE
;
3797 arc_change_state(arc_anon
, hdr
);
3798 arc_hdr_destroy(hdr
);
3799 *real_evicted
+= HDR_FULL_SIZE
;
3801 return (bytes_evicted
);
3804 ASSERT(state
== arc_mru
|| state
== arc_mfu
|| state
== arc_uncached
);
3805 evicted_state
= (state
== arc_uncached
) ? arc_anon
:
3806 ((state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
);
3808 /* prefetch buffers have a minimum lifespan */
3809 if ((hdr
->b_flags
& (ARC_FLAG_PREFETCH
| ARC_FLAG_INDIRECT
)) &&
3810 ddi_get_lbolt() - hdr
->b_l1hdr
.b_arc_access
<
3811 MSEC_TO_TICK(min_lifetime
)) {
3812 ARCSTAT_BUMP(arcstat_evict_skip
);
3813 return (bytes_evicted
);
3816 if (HDR_HAS_L2HDR(hdr
)) {
3817 ARCSTAT_INCR(arcstat_evict_l2_cached
, HDR_GET_LSIZE(hdr
));
3819 if (l2arc_write_eligible(hdr
->b_spa
, hdr
)) {
3820 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
3821 HDR_GET_LSIZE(hdr
));
3823 switch (state
->arcs_state
) {
3826 arcstat_evict_l2_eligible_mru
,
3827 HDR_GET_LSIZE(hdr
));
3831 arcstat_evict_l2_eligible_mfu
,
3832 HDR_GET_LSIZE(hdr
));
3838 ARCSTAT_INCR(arcstat_evict_l2_ineligible
,
3839 HDR_GET_LSIZE(hdr
));
3843 bytes_evicted
+= arc_hdr_size(hdr
);
3844 *real_evicted
+= arc_hdr_size(hdr
);
3847 * If this hdr is being evicted and has a compressed buffer then we
3848 * discard it here before we change states. This ensures that the
3849 * accounting is updated correctly in arc_free_data_impl().
3851 if (hdr
->b_l1hdr
.b_pabd
!= NULL
)
3852 arc_hdr_free_abd(hdr
, B_FALSE
);
3854 if (HDR_HAS_RABD(hdr
))
3855 arc_hdr_free_abd(hdr
, B_TRUE
);
3857 arc_change_state(evicted_state
, hdr
);
3858 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, hdr
);
3859 if (evicted_state
== arc_anon
) {
3860 arc_hdr_destroy(hdr
);
3861 *real_evicted
+= HDR_FULL_SIZE
;
3863 ASSERT(HDR_IN_HASH_TABLE(hdr
));
3866 return (bytes_evicted
);
3870 arc_set_need_free(void)
3872 ASSERT(MUTEX_HELD(&arc_evict_lock
));
3873 int64_t remaining
= arc_free_memory() - arc_sys_free
/ 2;
3874 arc_evict_waiter_t
*aw
= list_tail(&arc_evict_waiters
);
3876 arc_need_free
= MAX(-remaining
, 0);
3879 MAX(-remaining
, (int64_t)(aw
->aew_count
- arc_evict_count
));
3884 arc_evict_state_impl(multilist_t
*ml
, int idx
, arc_buf_hdr_t
*marker
,
3885 uint64_t spa
, uint64_t bytes
)
3887 multilist_sublist_t
*mls
;
3888 uint64_t bytes_evicted
= 0, real_evicted
= 0;
3890 kmutex_t
*hash_lock
;
3891 uint_t evict_count
= zfs_arc_evict_batch_limit
;
3893 ASSERT3P(marker
, !=, NULL
);
3895 mls
= multilist_sublist_lock_idx(ml
, idx
);
3897 for (hdr
= multilist_sublist_prev(mls
, marker
); likely(hdr
!= NULL
);
3898 hdr
= multilist_sublist_prev(mls
, marker
)) {
3899 if ((evict_count
== 0) || (bytes_evicted
>= bytes
))
3903 * To keep our iteration location, move the marker
3904 * forward. Since we're not holding hdr's hash lock, we
3905 * must be very careful and not remove 'hdr' from the
3906 * sublist. Otherwise, other consumers might mistake the
3907 * 'hdr' as not being on a sublist when they call the
3908 * multilist_link_active() function (they all rely on
3909 * the hash lock protecting concurrent insertions and
3910 * removals). multilist_sublist_move_forward() was
3911 * specifically implemented to ensure this is the case
3912 * (only 'marker' will be removed and re-inserted).
3914 multilist_sublist_move_forward(mls
, marker
);
3917 * The only case where the b_spa field should ever be
3918 * zero, is the marker headers inserted by
3919 * arc_evict_state(). It's possible for multiple threads
3920 * to be calling arc_evict_state() concurrently (e.g.
3921 * dsl_pool_close() and zio_inject_fault()), so we must
3922 * skip any markers we see from these other threads.
3924 if (hdr
->b_spa
== 0)
3927 /* we're only interested in evicting buffers of a certain spa */
3928 if (spa
!= 0 && hdr
->b_spa
!= spa
) {
3929 ARCSTAT_BUMP(arcstat_evict_skip
);
3933 hash_lock
= HDR_LOCK(hdr
);
3936 * We aren't calling this function from any code path
3937 * that would already be holding a hash lock, so we're
3938 * asserting on this assumption to be defensive in case
3939 * this ever changes. Without this check, it would be
3940 * possible to incorrectly increment arcstat_mutex_miss
3941 * below (e.g. if the code changed such that we called
3942 * this function with a hash lock held).
3944 ASSERT(!MUTEX_HELD(hash_lock
));
3946 if (mutex_tryenter(hash_lock
)) {
3948 uint64_t evicted
= arc_evict_hdr(hdr
, &revicted
);
3949 mutex_exit(hash_lock
);
3951 bytes_evicted
+= evicted
;
3952 real_evicted
+= revicted
;
3955 * If evicted is zero, arc_evict_hdr() must have
3956 * decided to skip this header, don't increment
3957 * evict_count in this case.
3963 ARCSTAT_BUMP(arcstat_mutex_miss
);
3967 multilist_sublist_unlock(mls
);
3970 * Increment the count of evicted bytes, and wake up any threads that
3971 * are waiting for the count to reach this value. Since the list is
3972 * ordered by ascending aew_count, we pop off the beginning of the
3973 * list until we reach the end, or a waiter that's past the current
3974 * "count". Doing this outside the loop reduces the number of times
3975 * we need to acquire the global arc_evict_lock.
3977 * Only wake when there's sufficient free memory in the system
3978 * (specifically, arc_sys_free/2, which by default is a bit more than
3979 * 1/64th of RAM). See the comments in arc_wait_for_eviction().
3981 mutex_enter(&arc_evict_lock
);
3982 arc_evict_count
+= real_evicted
;
3984 if (arc_free_memory() > arc_sys_free
/ 2) {
3985 arc_evict_waiter_t
*aw
;
3986 while ((aw
= list_head(&arc_evict_waiters
)) != NULL
&&
3987 aw
->aew_count
<= arc_evict_count
) {
3988 list_remove(&arc_evict_waiters
, aw
);
3989 cv_broadcast(&aw
->aew_cv
);
3992 arc_set_need_free();
3993 mutex_exit(&arc_evict_lock
);
3996 * If the ARC size is reduced from arc_c_max to arc_c_min (especially
3997 * if the average cached block is small), eviction can be on-CPU for
3998 * many seconds. To ensure that other threads that may be bound to
3999 * this CPU are able to make progress, make a voluntary preemption
4002 kpreempt(KPREEMPT_SYNC
);
4004 return (bytes_evicted
);
4007 static arc_buf_hdr_t
*
4008 arc_state_alloc_marker(void)
4010 arc_buf_hdr_t
*marker
= kmem_cache_alloc(hdr_full_cache
, KM_SLEEP
);
4013 * A b_spa of 0 is used to indicate that this header is
4014 * a marker. This fact is used in arc_evict_state_impl().
4022 arc_state_free_marker(arc_buf_hdr_t
*marker
)
4024 kmem_cache_free(hdr_full_cache
, marker
);
4028 * Allocate an array of buffer headers used as placeholders during arc state
4031 static arc_buf_hdr_t
**
4032 arc_state_alloc_markers(int count
)
4034 arc_buf_hdr_t
**markers
;
4036 markers
= kmem_zalloc(sizeof (*markers
) * count
, KM_SLEEP
);
4037 for (int i
= 0; i
< count
; i
++)
4038 markers
[i
] = arc_state_alloc_marker();
4043 arc_state_free_markers(arc_buf_hdr_t
**markers
, int count
)
4045 for (int i
= 0; i
< count
; i
++)
4046 arc_state_free_marker(markers
[i
]);
4047 kmem_free(markers
, sizeof (*markers
) * count
);
4051 * Evict buffers from the given arc state, until we've removed the
4052 * specified number of bytes. Move the removed buffers to the
4053 * appropriate evict state.
4055 * This function makes a "best effort". It skips over any buffers
4056 * it can't get a hash_lock on, and so, may not catch all candidates.
4057 * It may also return without evicting as much space as requested.
4059 * If bytes is specified using the special value ARC_EVICT_ALL, this
4060 * will evict all available (i.e. unlocked and evictable) buffers from
4061 * the given arc state; which is used by arc_flush().
4064 arc_evict_state(arc_state_t
*state
, arc_buf_contents_t type
, uint64_t spa
,
4067 uint64_t total_evicted
= 0;
4068 multilist_t
*ml
= &state
->arcs_list
[type
];
4070 arc_buf_hdr_t
**markers
;
4072 num_sublists
= multilist_get_num_sublists(ml
);
4075 * If we've tried to evict from each sublist, made some
4076 * progress, but still have not hit the target number of bytes
4077 * to evict, we want to keep trying. The markers allow us to
4078 * pick up where we left off for each individual sublist, rather
4079 * than starting from the tail each time.
4081 if (zthr_iscurthread(arc_evict_zthr
)) {
4082 markers
= arc_state_evict_markers
;
4083 ASSERT3S(num_sublists
, <=, arc_state_evict_marker_count
);
4085 markers
= arc_state_alloc_markers(num_sublists
);
4087 for (int i
= 0; i
< num_sublists
; i
++) {
4088 multilist_sublist_t
*mls
;
4090 mls
= multilist_sublist_lock_idx(ml
, i
);
4091 multilist_sublist_insert_tail(mls
, markers
[i
]);
4092 multilist_sublist_unlock(mls
);
4096 * While we haven't hit our target number of bytes to evict, or
4097 * we're evicting all available buffers.
4099 while (total_evicted
< bytes
) {
4100 int sublist_idx
= multilist_get_random_index(ml
);
4101 uint64_t scan_evicted
= 0;
4104 * Start eviction using a randomly selected sublist,
4105 * this is to try and evenly balance eviction across all
4106 * sublists. Always starting at the same sublist
4107 * (e.g. index 0) would cause evictions to favor certain
4108 * sublists over others.
4110 for (int i
= 0; i
< num_sublists
; i
++) {
4111 uint64_t bytes_remaining
;
4112 uint64_t bytes_evicted
;
4114 if (total_evicted
< bytes
)
4115 bytes_remaining
= bytes
- total_evicted
;
4119 bytes_evicted
= arc_evict_state_impl(ml
, sublist_idx
,
4120 markers
[sublist_idx
], spa
, bytes_remaining
);
4122 scan_evicted
+= bytes_evicted
;
4123 total_evicted
+= bytes_evicted
;
4125 /* we've reached the end, wrap to the beginning */
4126 if (++sublist_idx
>= num_sublists
)
4131 * If we didn't evict anything during this scan, we have
4132 * no reason to believe we'll evict more during another
4133 * scan, so break the loop.
4135 if (scan_evicted
== 0) {
4136 /* This isn't possible, let's make that obvious */
4137 ASSERT3S(bytes
, !=, 0);
4140 * When bytes is ARC_EVICT_ALL, the only way to
4141 * break the loop is when scan_evicted is zero.
4142 * In that case, we actually have evicted enough,
4143 * so we don't want to increment the kstat.
4145 if (bytes
!= ARC_EVICT_ALL
) {
4146 ASSERT3S(total_evicted
, <, bytes
);
4147 ARCSTAT_BUMP(arcstat_evict_not_enough
);
4154 for (int i
= 0; i
< num_sublists
; i
++) {
4155 multilist_sublist_t
*mls
= multilist_sublist_lock_idx(ml
, i
);
4156 multilist_sublist_remove(mls
, markers
[i
]);
4157 multilist_sublist_unlock(mls
);
4159 if (markers
!= arc_state_evict_markers
)
4160 arc_state_free_markers(markers
, num_sublists
);
4162 return (total_evicted
);
4166 * Flush all "evictable" data of the given type from the arc state
4167 * specified. This will not evict any "active" buffers (i.e. referenced).
4169 * When 'retry' is set to B_FALSE, the function will make a single pass
4170 * over the state and evict any buffers that it can. Since it doesn't
4171 * continually retry the eviction, it might end up leaving some buffers
4172 * in the ARC due to lock misses.
4174 * When 'retry' is set to B_TRUE, the function will continually retry the
4175 * eviction until *all* evictable buffers have been removed from the
4176 * state. As a result, if concurrent insertions into the state are
4177 * allowed (e.g. if the ARC isn't shutting down), this function might
4178 * wind up in an infinite loop, continually trying to evict buffers.
4181 arc_flush_state(arc_state_t
*state
, uint64_t spa
, arc_buf_contents_t type
,
4184 uint64_t evicted
= 0;
4186 while (zfs_refcount_count(&state
->arcs_esize
[type
]) != 0) {
4187 evicted
+= arc_evict_state(state
, type
, spa
, ARC_EVICT_ALL
);
4197 * Evict the specified number of bytes from the state specified. This
4198 * function prevents us from trying to evict more from a state's list
4199 * than is "evictable", and to skip evicting altogether when passed a
4200 * negative value for "bytes". In contrast, arc_evict_state() will
4201 * evict everything it can, when passed a negative value for "bytes".
4204 arc_evict_impl(arc_state_t
*state
, arc_buf_contents_t type
, int64_t bytes
)
4208 if (bytes
> 0 && zfs_refcount_count(&state
->arcs_esize
[type
]) > 0) {
4209 delta
= MIN(zfs_refcount_count(&state
->arcs_esize
[type
]),
4211 return (arc_evict_state(state
, type
, 0, delta
));
4218 * Adjust specified fraction, taking into account initial ghost state(s) size,
4219 * ghost hit bytes towards increasing the fraction, ghost hit bytes towards
4220 * decreasing it, plus a balance factor, controlling the decrease rate, used
4221 * to balance metadata vs data.
4224 arc_evict_adj(uint64_t frac
, uint64_t total
, uint64_t up
, uint64_t down
,
4227 if (total
< 8 || up
+ down
== 0)
4231 * We should not have more ghost hits than ghost size, but they
4232 * may get close. Restrict maximum adjustment in that case.
4234 if (up
+ down
>= total
/ 4) {
4235 uint64_t scale
= (up
+ down
) / (total
/ 8);
4240 /* Get maximal dynamic range by choosing optimal shifts. */
4241 int s
= highbit64(total
);
4242 s
= MIN(64 - s
, 32);
4244 uint64_t ofrac
= (1ULL << 32) - frac
;
4246 if (frac
>= 4 * ofrac
)
4247 up
/= frac
/ (2 * ofrac
+ 1);
4248 up
= (up
<< s
) / (total
>> (32 - s
));
4249 if (ofrac
>= 4 * frac
)
4250 down
/= ofrac
/ (2 * frac
+ 1);
4251 down
= (down
<< s
) / (total
>> (32 - s
));
4252 down
= down
* 100 / balance
;
4254 return (frac
+ up
- down
);
4258 * Calculate (x * multiplier / divisor) without unnecesary overflows.
4261 arc_mf(uint64_t x
, uint64_t multiplier
, uint64_t divisor
)
4263 uint64_t q
= (x
/ divisor
);
4264 uint64_t r
= (x
% divisor
);
4266 return ((q
* multiplier
) + ((r
* multiplier
) / divisor
));
4270 * Evict buffers from the cache, such that arcstat_size is capped by arc_c.
4275 uint64_t bytes
, total_evicted
= 0;
4276 int64_t e
, mrud
, mrum
, mfud
, mfum
, w
;
4277 static uint64_t ogrd
, ogrm
, ogfd
, ogfm
;
4278 static uint64_t gsrd
, gsrm
, gsfd
, gsfm
;
4279 uint64_t ngrd
, ngrm
, ngfd
, ngfm
;
4281 /* Get current size of ARC states we can evict from. */
4282 mrud
= zfs_refcount_count(&arc_mru
->arcs_size
[ARC_BUFC_DATA
]) +
4283 zfs_refcount_count(&arc_anon
->arcs_size
[ARC_BUFC_DATA
]);
4284 mrum
= zfs_refcount_count(&arc_mru
->arcs_size
[ARC_BUFC_METADATA
]) +
4285 zfs_refcount_count(&arc_anon
->arcs_size
[ARC_BUFC_METADATA
]);
4286 mfud
= zfs_refcount_count(&arc_mfu
->arcs_size
[ARC_BUFC_DATA
]);
4287 mfum
= zfs_refcount_count(&arc_mfu
->arcs_size
[ARC_BUFC_METADATA
]);
4288 uint64_t d
= mrud
+ mfud
;
4289 uint64_t m
= mrum
+ mfum
;
4292 /* Get ARC ghost hits since last eviction. */
4293 ngrd
= wmsum_value(&arc_mru_ghost
->arcs_hits
[ARC_BUFC_DATA
]);
4294 uint64_t grd
= ngrd
- ogrd
;
4296 ngrm
= wmsum_value(&arc_mru_ghost
->arcs_hits
[ARC_BUFC_METADATA
]);
4297 uint64_t grm
= ngrm
- ogrm
;
4299 ngfd
= wmsum_value(&arc_mfu_ghost
->arcs_hits
[ARC_BUFC_DATA
]);
4300 uint64_t gfd
= ngfd
- ogfd
;
4302 ngfm
= wmsum_value(&arc_mfu_ghost
->arcs_hits
[ARC_BUFC_METADATA
]);
4303 uint64_t gfm
= ngfm
- ogfm
;
4306 /* Adjust ARC states balance based on ghost hits. */
4307 arc_meta
= arc_evict_adj(arc_meta
, gsrd
+ gsrm
+ gsfd
+ gsfm
,
4308 grm
+ gfm
, grd
+ gfd
, zfs_arc_meta_balance
);
4309 arc_pd
= arc_evict_adj(arc_pd
, gsrd
+ gsfd
, grd
, gfd
, 100);
4310 arc_pm
= arc_evict_adj(arc_pm
, gsrm
+ gsfm
, grm
, gfm
, 100);
4312 uint64_t asize
= aggsum_value(&arc_sums
.arcstat_size
);
4313 uint64_t ac
= arc_c
;
4314 int64_t wt
= t
- (asize
- ac
);
4317 * Try to reduce pinned dnodes if more than 3/4 of wanted metadata
4318 * target is not evictable or if they go over arc_dnode_limit.
4321 int64_t dn
= wmsum_value(&arc_sums
.arcstat_dnode_size
);
4322 int64_t nem
= zfs_refcount_count(&arc_mru
->arcs_size
[ARC_BUFC_METADATA
])
4323 + zfs_refcount_count(&arc_mfu
->arcs_size
[ARC_BUFC_METADATA
])
4324 - zfs_refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
])
4325 - zfs_refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
4326 w
= wt
* (int64_t)(arc_meta
>> 16) >> 16;
4327 if (nem
> w
* 3 / 4) {
4328 prune
= dn
/ sizeof (dnode_t
) *
4329 zfs_arc_dnode_reduce_percent
/ 100;
4330 if (nem
< w
&& w
> 4)
4331 prune
= arc_mf(prune
, nem
- w
* 3 / 4, w
/ 4);
4333 if (dn
> arc_dnode_limit
) {
4334 prune
= MAX(prune
, (dn
- arc_dnode_limit
) / sizeof (dnode_t
) *
4335 zfs_arc_dnode_reduce_percent
/ 100);
4338 arc_prune_async(prune
);
4340 /* Evict MRU metadata. */
4341 w
= wt
* (int64_t)(arc_meta
* arc_pm
>> 48) >> 16;
4342 e
= MIN((int64_t)(asize
- ac
), (int64_t)(mrum
- w
));
4343 bytes
= arc_evict_impl(arc_mru
, ARC_BUFC_METADATA
, e
);
4344 total_evicted
+= bytes
;
4348 /* Evict MFU metadata. */
4349 w
= wt
* (int64_t)(arc_meta
>> 16) >> 16;
4350 e
= MIN((int64_t)(asize
- ac
), (int64_t)(m
- bytes
- w
));
4351 bytes
= arc_evict_impl(arc_mfu
, ARC_BUFC_METADATA
, e
);
4352 total_evicted
+= bytes
;
4356 /* Evict MRU data. */
4357 wt
-= m
- total_evicted
;
4358 w
= wt
* (int64_t)(arc_pd
>> 16) >> 16;
4359 e
= MIN((int64_t)(asize
- ac
), (int64_t)(mrud
- w
));
4360 bytes
= arc_evict_impl(arc_mru
, ARC_BUFC_DATA
, e
);
4361 total_evicted
+= bytes
;
4365 /* Evict MFU data. */
4367 bytes
= arc_evict_impl(arc_mfu
, ARC_BUFC_DATA
, e
);
4369 total_evicted
+= bytes
;
4374 * Size of each state's ghost list represents how much that state
4375 * may grow by shrinking the other states. Would it need to shrink
4376 * other states to zero (that is unlikely), its ghost size would be
4377 * equal to sum of other three state sizes. But excessive ghost
4378 * size may result in false ghost hits (too far back), that may
4379 * never result in real cache hits if several states are competing.
4380 * So choose some arbitraty point of 1/2 of other state sizes.
4382 gsrd
= (mrum
+ mfud
+ mfum
) / 2;
4383 e
= zfs_refcount_count(&arc_mru_ghost
->arcs_size
[ARC_BUFC_DATA
]) -
4385 (void) arc_evict_impl(arc_mru_ghost
, ARC_BUFC_DATA
, e
);
4387 gsrm
= (mrud
+ mfud
+ mfum
) / 2;
4388 e
= zfs_refcount_count(&arc_mru_ghost
->arcs_size
[ARC_BUFC_METADATA
]) -
4390 (void) arc_evict_impl(arc_mru_ghost
, ARC_BUFC_METADATA
, e
);
4392 gsfd
= (mrud
+ mrum
+ mfum
) / 2;
4393 e
= zfs_refcount_count(&arc_mfu_ghost
->arcs_size
[ARC_BUFC_DATA
]) -
4395 (void) arc_evict_impl(arc_mfu_ghost
, ARC_BUFC_DATA
, e
);
4397 gsfm
= (mrud
+ mrum
+ mfud
) / 2;
4398 e
= zfs_refcount_count(&arc_mfu_ghost
->arcs_size
[ARC_BUFC_METADATA
]) -
4400 (void) arc_evict_impl(arc_mfu_ghost
, ARC_BUFC_METADATA
, e
);
4402 return (total_evicted
);
4406 arc_flush_impl(uint64_t guid
, boolean_t retry
)
4408 ASSERT(!retry
|| guid
== 0);
4410 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_DATA
, retry
);
4411 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_METADATA
, retry
);
4413 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_DATA
, retry
);
4414 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_METADATA
, retry
);
4416 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4417 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4419 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_DATA
, retry
);
4420 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
4422 (void) arc_flush_state(arc_uncached
, guid
, ARC_BUFC_DATA
, retry
);
4423 (void) arc_flush_state(arc_uncached
, guid
, ARC_BUFC_METADATA
, retry
);
4427 arc_flush(spa_t
*spa
, boolean_t retry
)
4430 * If retry is B_TRUE, a spa must not be specified since we have
4431 * no good way to determine if all of a spa's buffers have been
4432 * evicted from an arc state.
4434 ASSERT(!retry
|| spa
== NULL
);
4436 arc_flush_impl(spa
!= NULL
? spa_load_guid(spa
) : 0, retry
);
4439 static arc_async_flush_t
*
4440 arc_async_flush_add(uint64_t spa_guid
, uint_t level
)
4442 arc_async_flush_t
*af
= kmem_alloc(sizeof (*af
), KM_SLEEP
);
4443 af
->af_spa_guid
= spa_guid
;
4444 af
->af_cache_level
= level
;
4445 taskq_init_ent(&af
->af_tqent
);
4446 list_link_init(&af
->af_node
);
4448 mutex_enter(&arc_async_flush_lock
);
4449 list_insert_tail(&arc_async_flush_list
, af
);
4450 mutex_exit(&arc_async_flush_lock
);
4456 arc_async_flush_remove(uint64_t spa_guid
, uint_t level
)
4458 mutex_enter(&arc_async_flush_lock
);
4459 for (arc_async_flush_t
*af
= list_head(&arc_async_flush_list
);
4460 af
!= NULL
; af
= list_next(&arc_async_flush_list
, af
)) {
4461 if (af
->af_spa_guid
== spa_guid
&&
4462 af
->af_cache_level
== level
) {
4463 list_remove(&arc_async_flush_list
, af
);
4464 kmem_free(af
, sizeof (*af
));
4468 mutex_exit(&arc_async_flush_lock
);
4472 arc_flush_task(void *arg
)
4474 arc_async_flush_t
*af
= arg
;
4475 hrtime_t start_time
= gethrtime();
4476 uint64_t spa_guid
= af
->af_spa_guid
;
4478 arc_flush_impl(spa_guid
, B_FALSE
);
4479 arc_async_flush_remove(spa_guid
, af
->af_cache_level
);
4481 uint64_t elaspsed
= NSEC2MSEC(gethrtime() - start_time
);
4483 zfs_dbgmsg("spa %llu arc flushed in %llu ms",
4484 (u_longlong_t
)spa_guid
, (u_longlong_t
)elaspsed
);
4489 * ARC buffers use the spa's load guid and can continue to exist after
4490 * the spa_t is gone (exported). The blocks are orphaned since each
4491 * spa import has a different load guid.
4493 * It's OK if the spa is re-imported while this asynchronous flush is
4494 * still in progress. The new spa_load_guid will be different.
4496 * Also, arc_fini will wait for any arc_flush_task to finish.
4499 arc_flush_async(spa_t
*spa
)
4501 uint64_t spa_guid
= spa_load_guid(spa
);
4502 arc_async_flush_t
*af
= arc_async_flush_add(spa_guid
, 1);
4504 taskq_dispatch_ent(arc_flush_taskq
, arc_flush_task
,
4505 af
, TQ_SLEEP
, &af
->af_tqent
);
4509 * Check if a guid is still in-use as part of an async teardown task
4512 arc_async_flush_guid_inuse(uint64_t spa_guid
)
4514 mutex_enter(&arc_async_flush_lock
);
4515 for (arc_async_flush_t
*af
= list_head(&arc_async_flush_list
);
4516 af
!= NULL
; af
= list_next(&arc_async_flush_list
, af
)) {
4517 if (af
->af_spa_guid
== spa_guid
) {
4518 mutex_exit(&arc_async_flush_lock
);
4522 mutex_exit(&arc_async_flush_lock
);
4527 arc_reduce_target_size(uint64_t to_free
)
4530 * Get the actual arc size. Even if we don't need it, this updates
4531 * the aggsum lower bound estimate for arc_is_overflowing().
4533 uint64_t asize
= aggsum_value(&arc_sums
.arcstat_size
);
4536 * All callers want the ARC to actually evict (at least) this much
4537 * memory. Therefore we reduce from the lower of the current size and
4538 * the target size. This way, even if arc_c is much higher than
4539 * arc_size (as can be the case after many calls to arc_freed(), we will
4540 * immediately have arc_c < arc_size and therefore the arc_evict_zthr
4544 if (c
> arc_c_min
) {
4545 c
= MIN(c
, MAX(asize
, arc_c_min
));
4546 to_free
= MIN(to_free
, c
- arc_c_min
);
4547 arc_c
= c
- to_free
;
4553 * Whether or not we reduced the target size, request eviction if the
4554 * current size is over it now, since caller obviously wants some RAM.
4556 if (asize
> arc_c
) {
4557 /* See comment in arc_evict_cb_check() on why lock+flag */
4558 mutex_enter(&arc_evict_lock
);
4559 arc_evict_needed
= B_TRUE
;
4560 mutex_exit(&arc_evict_lock
);
4561 zthr_wakeup(arc_evict_zthr
);
4568 * Determine if the system is under memory pressure and is asking
4569 * to reclaim memory. A return value of B_TRUE indicates that the system
4570 * is under memory pressure and that the arc should adjust accordingly.
4573 arc_reclaim_needed(void)
4575 return (arc_available_memory() < 0);
4579 arc_kmem_reap_soon(void)
4582 kmem_cache_t
*prev_cache
= NULL
;
4583 kmem_cache_t
*prev_data_cache
= NULL
;
4588 * Reclaim unused memory from all kmem caches.
4594 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
4596 /* reach upper limit of cache size on 32-bit */
4597 if (zio_buf_cache
[i
] == NULL
)
4600 if (zio_buf_cache
[i
] != prev_cache
) {
4601 prev_cache
= zio_buf_cache
[i
];
4602 kmem_cache_reap_now(zio_buf_cache
[i
]);
4604 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
4605 prev_data_cache
= zio_data_buf_cache
[i
];
4606 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
4609 kmem_cache_reap_now(buf_cache
);
4610 kmem_cache_reap_now(hdr_full_cache
);
4611 kmem_cache_reap_now(hdr_l2only_cache
);
4612 kmem_cache_reap_now(zfs_btree_leaf_cache
);
4613 abd_cache_reap_now();
4617 arc_evict_cb_check(void *arg
, zthr_t
*zthr
)
4619 (void) arg
, (void) zthr
;
4623 * This is necessary in order to keep the kstat information
4624 * up to date for tools that display kstat data such as the
4625 * mdb ::arc dcmd and the Linux crash utility. These tools
4626 * typically do not call kstat's update function, but simply
4627 * dump out stats from the most recent update. Without
4628 * this call, these commands may show stale stats for the
4629 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
4630 * with this call, the data might be out of date if the
4631 * evict thread hasn't been woken recently; but that should
4632 * suffice. The arc_state_t structures can be queried
4633 * directly if more accurate information is needed.
4635 if (arc_ksp
!= NULL
)
4636 arc_ksp
->ks_update(arc_ksp
, KSTAT_READ
);
4640 * We have to rely on arc_wait_for_eviction() to tell us when to
4641 * evict, rather than checking if we are overflowing here, so that we
4642 * are sure to not leave arc_wait_for_eviction() waiting on aew_cv.
4643 * If we have become "not overflowing" since arc_wait_for_eviction()
4644 * checked, we need to wake it up. We could broadcast the CV here,
4645 * but arc_wait_for_eviction() may have not yet gone to sleep. We
4646 * would need to use a mutex to ensure that this function doesn't
4647 * broadcast until arc_wait_for_eviction() has gone to sleep (e.g.
4648 * the arc_evict_lock). However, the lock ordering of such a lock
4649 * would necessarily be incorrect with respect to the zthr_lock,
4650 * which is held before this function is called, and is held by
4651 * arc_wait_for_eviction() when it calls zthr_wakeup().
4653 if (arc_evict_needed
)
4657 * If we have buffers in uncached state, evict them periodically.
4659 return ((zfs_refcount_count(&arc_uncached
->arcs_esize
[ARC_BUFC_DATA
]) +
4660 zfs_refcount_count(&arc_uncached
->arcs_esize
[ARC_BUFC_METADATA
]) &&
4661 ddi_get_lbolt() - arc_last_uncached_flush
>
4662 MSEC_TO_TICK(arc_min_prefetch_ms
/ 2)));
4666 * Keep arc_size under arc_c by running arc_evict which evicts data
4670 arc_evict_cb(void *arg
, zthr_t
*zthr
)
4674 uint64_t evicted
= 0;
4675 fstrans_cookie_t cookie
= spl_fstrans_mark();
4677 /* Always try to evict from uncached state. */
4678 arc_last_uncached_flush
= ddi_get_lbolt();
4679 evicted
+= arc_flush_state(arc_uncached
, 0, ARC_BUFC_DATA
, B_FALSE
);
4680 evicted
+= arc_flush_state(arc_uncached
, 0, ARC_BUFC_METADATA
, B_FALSE
);
4682 /* Evict from other states only if told to. */
4683 if (arc_evict_needed
)
4684 evicted
+= arc_evict();
4687 * If evicted is zero, we couldn't evict anything
4688 * via arc_evict(). This could be due to hash lock
4689 * collisions, but more likely due to the majority of
4690 * arc buffers being unevictable. Therefore, even if
4691 * arc_size is above arc_c, another pass is unlikely to
4692 * be helpful and could potentially cause us to enter an
4693 * infinite loop. Additionally, zthr_iscancelled() is
4694 * checked here so that if the arc is shutting down, the
4695 * broadcast will wake any remaining arc evict waiters.
4697 * Note we cancel using zthr instead of arc_evict_zthr
4698 * because the latter may not yet be initializd when the
4699 * callback is first invoked.
4701 mutex_enter(&arc_evict_lock
);
4702 arc_evict_needed
= !zthr_iscancelled(zthr
) &&
4703 evicted
> 0 && aggsum_compare(&arc_sums
.arcstat_size
, arc_c
) > 0;
4704 if (!arc_evict_needed
) {
4706 * We're either no longer overflowing, or we
4707 * can't evict anything more, so we should wake
4708 * arc_get_data_impl() sooner.
4710 arc_evict_waiter_t
*aw
;
4711 while ((aw
= list_remove_head(&arc_evict_waiters
)) != NULL
) {
4712 cv_broadcast(&aw
->aew_cv
);
4714 arc_set_need_free();
4716 mutex_exit(&arc_evict_lock
);
4717 spl_fstrans_unmark(cookie
);
4721 arc_reap_cb_check(void *arg
, zthr_t
*zthr
)
4723 (void) arg
, (void) zthr
;
4725 int64_t free_memory
= arc_available_memory();
4726 static int reap_cb_check_counter
= 0;
4729 * If a kmem reap is already active, don't schedule more. We must
4730 * check for this because kmem_cache_reap_soon() won't actually
4731 * block on the cache being reaped (this is to prevent callers from
4732 * becoming implicitly blocked by a system-wide kmem reap -- which,
4733 * on a system with many, many full magazines, can take minutes).
4735 if (!kmem_cache_reap_active() && free_memory
< 0) {
4737 arc_no_grow
= B_TRUE
;
4740 * Wait at least zfs_grow_retry (default 5) seconds
4741 * before considering growing.
4743 arc_growtime
= gethrtime() + SEC2NSEC(arc_grow_retry
);
4745 } else if (free_memory
< arc_c
>> arc_no_grow_shift
) {
4746 arc_no_grow
= B_TRUE
;
4747 } else if (gethrtime() >= arc_growtime
) {
4748 arc_no_grow
= B_FALSE
;
4752 * Called unconditionally every 60 seconds to reclaim unused
4753 * zstd compression and decompression context. This is done
4754 * here to avoid the need for an independent thread.
4756 if (!((reap_cb_check_counter
++) % 60))
4757 zfs_zstd_cache_reap_now();
4763 * Keep enough free memory in the system by reaping the ARC's kmem
4764 * caches. To cause more slabs to be reapable, we may reduce the
4765 * target size of the cache (arc_c), causing the arc_evict_cb()
4766 * to free more buffers.
4769 arc_reap_cb(void *arg
, zthr_t
*zthr
)
4771 int64_t can_free
, free_memory
, to_free
;
4773 (void) arg
, (void) zthr
;
4774 fstrans_cookie_t cookie
= spl_fstrans_mark();
4777 * Kick off asynchronous kmem_reap()'s of all our caches.
4779 arc_kmem_reap_soon();
4782 * Wait at least arc_kmem_cache_reap_retry_ms between
4783 * arc_kmem_reap_soon() calls. Without this check it is possible to
4784 * end up in a situation where we spend lots of time reaping
4785 * caches, while we're near arc_c_min. Waiting here also gives the
4786 * subsequent free memory check a chance of finding that the
4787 * asynchronous reap has already freed enough memory, and we don't
4788 * need to call arc_reduce_target_size().
4790 delay((hz
* arc_kmem_cache_reap_retry_ms
+ 999) / 1000);
4793 * Reduce the target size as needed to maintain the amount of free
4794 * memory in the system at a fraction of the arc_size (1/128th by
4795 * default). If oversubscribed (free_memory < 0) then reduce the
4796 * target arc_size by the deficit amount plus the fractional
4797 * amount. If free memory is positive but less than the fractional
4798 * amount, reduce by what is needed to hit the fractional amount.
4800 free_memory
= arc_available_memory();
4801 can_free
= arc_c
- arc_c_min
;
4802 to_free
= (MAX(can_free
, 0) >> arc_shrink_shift
) - free_memory
;
4804 arc_reduce_target_size(to_free
);
4805 spl_fstrans_unmark(cookie
);
4810 * Determine the amount of memory eligible for eviction contained in the
4811 * ARC. All clean data reported by the ghost lists can always be safely
4812 * evicted. Due to arc_c_min, the same does not hold for all clean data
4813 * contained by the regular mru and mfu lists.
4815 * In the case of the regular mru and mfu lists, we need to report as
4816 * much clean data as possible, such that evicting that same reported
4817 * data will not bring arc_size below arc_c_min. Thus, in certain
4818 * circumstances, the total amount of clean data in the mru and mfu
4819 * lists might not actually be evictable.
4821 * The following two distinct cases are accounted for:
4823 * 1. The sum of the amount of dirty data contained by both the mru and
4824 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
4825 * is greater than or equal to arc_c_min.
4826 * (i.e. amount of dirty data >= arc_c_min)
4828 * This is the easy case; all clean data contained by the mru and mfu
4829 * lists is evictable. Evicting all clean data can only drop arc_size
4830 * to the amount of dirty data, which is greater than arc_c_min.
4832 * 2. The sum of the amount of dirty data contained by both the mru and
4833 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
4834 * is less than arc_c_min.
4835 * (i.e. arc_c_min > amount of dirty data)
4837 * 2.1. arc_size is greater than or equal arc_c_min.
4838 * (i.e. arc_size >= arc_c_min > amount of dirty data)
4840 * In this case, not all clean data from the regular mru and mfu
4841 * lists is actually evictable; we must leave enough clean data
4842 * to keep arc_size above arc_c_min. Thus, the maximum amount of
4843 * evictable data from the two lists combined, is exactly the
4844 * difference between arc_size and arc_c_min.
4846 * 2.2. arc_size is less than arc_c_min
4847 * (i.e. arc_c_min > arc_size > amount of dirty data)
4849 * In this case, none of the data contained in the mru and mfu
4850 * lists is evictable, even if it's clean. Since arc_size is
4851 * already below arc_c_min, evicting any more would only
4852 * increase this negative difference.
4855 #endif /* _KERNEL */
4858 * Adapt arc info given the number of bytes we are trying to add and
4859 * the state that we are coming from. This function is only called
4860 * when we are adding new content to the cache.
4863 arc_adapt(uint64_t bytes
)
4866 * Wake reap thread if we do not have any available memory
4868 if (arc_reclaim_needed()) {
4869 zthr_wakeup(arc_reap_zthr
);
4876 if (arc_c
>= arc_c_max
)
4880 * If we're within (2 * maxblocksize) bytes of the target
4881 * cache size, increment the target cache size
4883 if (aggsum_upper_bound(&arc_sums
.arcstat_size
) +
4884 2 * SPA_MAXBLOCKSIZE
>= arc_c
) {
4885 uint64_t dc
= MAX(bytes
, SPA_OLD_MAXBLOCKSIZE
);
4886 if (atomic_add_64_nv(&arc_c
, dc
) > arc_c_max
)
4892 * Check if ARC current size has grown past our upper thresholds.
4894 static arc_ovf_level_t
4895 arc_is_overflowing(boolean_t lax
, boolean_t use_reserve
)
4898 * We just compare the lower bound here for performance reasons. Our
4899 * primary goals are to make sure that the arc never grows without
4900 * bound, and that it can reach its maximum size. This check
4901 * accomplishes both goals. The maximum amount we could run over by is
4902 * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block
4903 * in the ARC. In practice, that's in the tens of MB, which is low
4904 * enough to be safe.
4906 int64_t over
= aggsum_lower_bound(&arc_sums
.arcstat_size
) - arc_c
-
4909 /* Always allow at least one block of overflow. */
4911 return (ARC_OVF_NONE
);
4913 /* If we are under memory pressure, report severe overflow. */
4915 return (ARC_OVF_SEVERE
);
4917 /* We are not under pressure, so be more or less relaxed. */
4918 int64_t overflow
= (arc_c
>> zfs_arc_overflow_shift
) / 2;
4921 return (over
< overflow
? ARC_OVF_SOME
: ARC_OVF_SEVERE
);
4925 arc_get_data_abd(arc_buf_hdr_t
*hdr
, uint64_t size
, const void *tag
,
4928 arc_buf_contents_t type
= arc_buf_type(hdr
);
4930 arc_get_data_impl(hdr
, size
, tag
, alloc_flags
);
4931 if (alloc_flags
& ARC_HDR_ALLOC_LINEAR
)
4932 return (abd_alloc_linear(size
, type
== ARC_BUFC_METADATA
));
4934 return (abd_alloc(size
, type
== ARC_BUFC_METADATA
));
4938 arc_get_data_buf(arc_buf_hdr_t
*hdr
, uint64_t size
, const void *tag
)
4940 arc_buf_contents_t type
= arc_buf_type(hdr
);
4942 arc_get_data_impl(hdr
, size
, tag
, 0);
4943 if (type
== ARC_BUFC_METADATA
) {
4944 return (zio_buf_alloc(size
));
4946 ASSERT(type
== ARC_BUFC_DATA
);
4947 return (zio_data_buf_alloc(size
));
4952 * Wait for the specified amount of data (in bytes) to be evicted from the
4953 * ARC, and for there to be sufficient free memory in the system.
4954 * The lax argument specifies that caller does not have a specific reason
4955 * to wait, not aware of any memory pressure. Low memory handlers though
4956 * should set it to B_FALSE to wait for all required evictions to complete.
4957 * The use_reserve argument allows some callers to wait less than others
4958 * to not block critical code paths, possibly blocking other resources.
4961 arc_wait_for_eviction(uint64_t amount
, boolean_t lax
, boolean_t use_reserve
)
4963 switch (arc_is_overflowing(lax
, use_reserve
)) {
4968 * This is a bit racy without taking arc_evict_lock, but the
4969 * worst that can happen is we either call zthr_wakeup() extra
4970 * time due to race with other thread here, or the set flag
4971 * get cleared by arc_evict_cb(), which is unlikely due to
4972 * big hysteresis, but also not important since at this level
4973 * of overflow the eviction is purely advisory. Same time
4974 * taking the global lock here every time without waiting for
4975 * the actual eviction creates a significant lock contention.
4977 if (!arc_evict_needed
) {
4978 arc_evict_needed
= B_TRUE
;
4979 zthr_wakeup(arc_evict_zthr
);
4982 case ARC_OVF_SEVERE
:
4985 arc_evict_waiter_t aw
;
4986 list_link_init(&aw
.aew_node
);
4987 cv_init(&aw
.aew_cv
, NULL
, CV_DEFAULT
, NULL
);
4989 uint64_t last_count
= 0;
4990 mutex_enter(&arc_evict_lock
);
4991 if (!list_is_empty(&arc_evict_waiters
)) {
4992 arc_evict_waiter_t
*last
=
4993 list_tail(&arc_evict_waiters
);
4994 last_count
= last
->aew_count
;
4995 } else if (!arc_evict_needed
) {
4996 arc_evict_needed
= B_TRUE
;
4997 zthr_wakeup(arc_evict_zthr
);
5000 * Note, the last waiter's count may be less than
5001 * arc_evict_count if we are low on memory in which
5002 * case arc_evict_state_impl() may have deferred
5003 * wakeups (but still incremented arc_evict_count).
5005 aw
.aew_count
= MAX(last_count
, arc_evict_count
) + amount
;
5007 list_insert_tail(&arc_evict_waiters
, &aw
);
5009 arc_set_need_free();
5011 DTRACE_PROBE3(arc__wait__for__eviction
,
5013 uint64_t, arc_evict_count
,
5014 uint64_t, aw
.aew_count
);
5017 * We will be woken up either when arc_evict_count reaches
5018 * aew_count, or when the ARC is no longer overflowing and
5019 * eviction completes.
5020 * In case of "false" wakeup, we will still be on the list.
5023 cv_wait(&aw
.aew_cv
, &arc_evict_lock
);
5024 } while (list_link_active(&aw
.aew_node
));
5025 mutex_exit(&arc_evict_lock
);
5027 cv_destroy(&aw
.aew_cv
);
5033 * Allocate a block and return it to the caller. If we are hitting the
5034 * hard limit for the cache size, we must sleep, waiting for the eviction
5035 * thread to catch up. If we're past the target size but below the hard
5036 * limit, we'll only signal the reclaim thread and continue on.
5039 arc_get_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, const void *tag
,
5045 * If arc_size is currently overflowing, we must be adding data
5046 * faster than we are evicting. To ensure we don't compound the
5047 * problem by adding more data and forcing arc_size to grow even
5048 * further past it's target size, we wait for the eviction thread to
5049 * make some progress. We also wait for there to be sufficient free
5050 * memory in the system, as measured by arc_free_memory().
5052 * Specifically, we wait for zfs_arc_eviction_pct percent of the
5053 * requested size to be evicted. This should be more than 100%, to
5054 * ensure that that progress is also made towards getting arc_size
5055 * under arc_c. See the comment above zfs_arc_eviction_pct.
5057 arc_wait_for_eviction(size
* zfs_arc_eviction_pct
/ 100,
5058 B_TRUE
, alloc_flags
& ARC_HDR_USE_RESERVE
);
5060 arc_buf_contents_t type
= arc_buf_type(hdr
);
5061 if (type
== ARC_BUFC_METADATA
) {
5062 arc_space_consume(size
, ARC_SPACE_META
);
5064 arc_space_consume(size
, ARC_SPACE_DATA
);
5068 * Update the state size. Note that ghost states have a
5069 * "ghost size" and so don't need to be updated.
5071 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5072 if (!GHOST_STATE(state
)) {
5074 (void) zfs_refcount_add_many(&state
->arcs_size
[type
], size
,
5078 * If this is reached via arc_read, the link is
5079 * protected by the hash lock. If reached via
5080 * arc_buf_alloc, the header should not be accessed by
5081 * any other thread. And, if reached via arc_read_done,
5082 * the hash lock will protect it if it's found in the
5083 * hash table; otherwise no other thread should be
5084 * trying to [add|remove]_reference it.
5086 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5087 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5088 (void) zfs_refcount_add_many(&state
->arcs_esize
[type
],
5095 arc_free_data_abd(arc_buf_hdr_t
*hdr
, abd_t
*abd
, uint64_t size
,
5098 arc_free_data_impl(hdr
, size
, tag
);
5103 arc_free_data_buf(arc_buf_hdr_t
*hdr
, void *buf
, uint64_t size
, const void *tag
)
5105 arc_buf_contents_t type
= arc_buf_type(hdr
);
5107 arc_free_data_impl(hdr
, size
, tag
);
5108 if (type
== ARC_BUFC_METADATA
) {
5109 zio_buf_free(buf
, size
);
5111 ASSERT(type
== ARC_BUFC_DATA
);
5112 zio_data_buf_free(buf
, size
);
5117 * Free the arc data buffer.
5120 arc_free_data_impl(arc_buf_hdr_t
*hdr
, uint64_t size
, const void *tag
)
5122 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5123 arc_buf_contents_t type
= arc_buf_type(hdr
);
5125 /* protected by hash lock, if in the hash table */
5126 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
5127 ASSERT(zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5128 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
5130 (void) zfs_refcount_remove_many(&state
->arcs_esize
[type
],
5133 (void) zfs_refcount_remove_many(&state
->arcs_size
[type
], size
, tag
);
5135 VERIFY3U(hdr
->b_type
, ==, type
);
5136 if (type
== ARC_BUFC_METADATA
) {
5137 arc_space_return(size
, ARC_SPACE_META
);
5139 ASSERT(type
== ARC_BUFC_DATA
);
5140 arc_space_return(size
, ARC_SPACE_DATA
);
5145 * This routine is called whenever a buffer is accessed.
5148 arc_access(arc_buf_hdr_t
*hdr
, arc_flags_t arc_flags
, boolean_t hit
)
5150 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
5151 ASSERT(HDR_HAS_L1HDR(hdr
));
5154 * Update buffer prefetch status.
5156 boolean_t was_prefetch
= HDR_PREFETCH(hdr
);
5157 boolean_t now_prefetch
= arc_flags
& ARC_FLAG_PREFETCH
;
5158 if (was_prefetch
!= now_prefetch
) {
5160 ARCSTAT_CONDSTAT(hit
, demand_hit
, demand_iohit
,
5161 HDR_PRESCIENT_PREFETCH(hdr
), prescient
, predictive
,
5164 if (HDR_HAS_L2HDR(hdr
))
5165 l2arc_hdr_arcstats_decrement_state(hdr
);
5167 arc_hdr_clear_flags(hdr
,
5168 ARC_FLAG_PREFETCH
| ARC_FLAG_PRESCIENT_PREFETCH
);
5170 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
5172 if (HDR_HAS_L2HDR(hdr
))
5173 l2arc_hdr_arcstats_increment_state(hdr
);
5176 if (arc_flags
& ARC_FLAG_PRESCIENT_PREFETCH
) {
5177 arc_hdr_set_flags(hdr
, ARC_FLAG_PRESCIENT_PREFETCH
);
5178 ARCSTAT_BUMP(arcstat_prescient_prefetch
);
5180 ARCSTAT_BUMP(arcstat_predictive_prefetch
);
5183 if (arc_flags
& ARC_FLAG_L2CACHE
)
5184 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
5186 clock_t now
= ddi_get_lbolt();
5187 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
5188 arc_state_t
*new_state
;
5190 * This buffer is not in the cache, and does not appear in
5191 * our "ghost" lists. Add it to the MRU or uncached state.
5193 ASSERT0(hdr
->b_l1hdr
.b_arc_access
);
5194 hdr
->b_l1hdr
.b_arc_access
= now
;
5195 if (HDR_UNCACHED(hdr
)) {
5196 new_state
= arc_uncached
;
5197 DTRACE_PROBE1(new_state__uncached
, arc_buf_hdr_t
*,
5200 new_state
= arc_mru
;
5201 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5203 arc_change_state(new_state
, hdr
);
5204 } else if (hdr
->b_l1hdr
.b_state
== arc_mru
) {
5206 * This buffer has been accessed once recently and either
5207 * its read is still in progress or it is in the cache.
5209 if (HDR_IO_IN_PROGRESS(hdr
)) {
5210 hdr
->b_l1hdr
.b_arc_access
= now
;
5213 hdr
->b_l1hdr
.b_mru_hits
++;
5214 ARCSTAT_BUMP(arcstat_mru_hits
);
5217 * If the previous access was a prefetch, then it already
5218 * handled possible promotion, so nothing more to do for now.
5221 hdr
->b_l1hdr
.b_arc_access
= now
;
5226 * If more than ARC_MINTIME have passed from the previous
5227 * hit, promote the buffer to the MFU state.
5229 if (ddi_time_after(now
, hdr
->b_l1hdr
.b_arc_access
+
5231 hdr
->b_l1hdr
.b_arc_access
= now
;
5232 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5233 arc_change_state(arc_mfu
, hdr
);
5235 } else if (hdr
->b_l1hdr
.b_state
== arc_mru_ghost
) {
5236 arc_state_t
*new_state
;
5238 * This buffer has been accessed once recently, but was
5239 * evicted from the cache. Would we have bigger MRU, it
5240 * would be an MRU hit, so handle it the same way, except
5241 * we don't need to check the previous access time.
5243 hdr
->b_l1hdr
.b_mru_ghost_hits
++;
5244 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
5245 hdr
->b_l1hdr
.b_arc_access
= now
;
5246 wmsum_add(&arc_mru_ghost
->arcs_hits
[arc_buf_type(hdr
)],
5249 new_state
= arc_mru
;
5250 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5252 new_state
= arc_mfu
;
5253 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5255 arc_change_state(new_state
, hdr
);
5256 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu
) {
5258 * This buffer has been accessed more than once and either
5259 * still in the cache or being restored from one of ghosts.
5261 if (!HDR_IO_IN_PROGRESS(hdr
)) {
5262 hdr
->b_l1hdr
.b_mfu_hits
++;
5263 ARCSTAT_BUMP(arcstat_mfu_hits
);
5265 hdr
->b_l1hdr
.b_arc_access
= now
;
5266 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu_ghost
) {
5268 * This buffer has been accessed more than once recently, but
5269 * has been evicted from the cache. Would we have bigger MFU
5270 * it would stay in cache, so move it back to MFU state.
5272 hdr
->b_l1hdr
.b_mfu_ghost_hits
++;
5273 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
5274 hdr
->b_l1hdr
.b_arc_access
= now
;
5275 wmsum_add(&arc_mfu_ghost
->arcs_hits
[arc_buf_type(hdr
)],
5277 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
5278 arc_change_state(arc_mfu
, hdr
);
5279 } else if (hdr
->b_l1hdr
.b_state
== arc_uncached
) {
5281 * This buffer is uncacheable, but we got a hit. Probably
5282 * a demand read after prefetch. Nothing more to do here.
5284 if (!HDR_IO_IN_PROGRESS(hdr
))
5285 ARCSTAT_BUMP(arcstat_uncached_hits
);
5286 hdr
->b_l1hdr
.b_arc_access
= now
;
5287 } else if (hdr
->b_l1hdr
.b_state
== arc_l2c_only
) {
5289 * This buffer is on the 2nd Level ARC and was not accessed
5290 * for a long time, so treat it as new and put into MRU.
5292 hdr
->b_l1hdr
.b_arc_access
= now
;
5293 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
5294 arc_change_state(arc_mru
, hdr
);
5296 cmn_err(CE_PANIC
, "invalid arc state 0x%p",
5297 hdr
->b_l1hdr
.b_state
);
5302 * This routine is called by dbuf_hold() to update the arc_access() state
5303 * which otherwise would be skipped for entries in the dbuf cache.
5306 arc_buf_access(arc_buf_t
*buf
)
5308 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5311 * Avoid taking the hash_lock when possible as an optimization.
5312 * The header must be checked again under the hash_lock in order
5313 * to handle the case where it is concurrently being released.
5315 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
))
5318 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
5319 mutex_enter(hash_lock
);
5321 if (hdr
->b_l1hdr
.b_state
== arc_anon
|| HDR_EMPTY(hdr
)) {
5322 mutex_exit(hash_lock
);
5323 ARCSTAT_BUMP(arcstat_access_skip
);
5327 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
5328 hdr
->b_l1hdr
.b_state
== arc_mfu
||
5329 hdr
->b_l1hdr
.b_state
== arc_uncached
);
5331 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
5332 arc_access(hdr
, 0, B_TRUE
);
5333 mutex_exit(hash_lock
);
5335 ARCSTAT_BUMP(arcstat_hits
);
5336 ARCSTAT_CONDSTAT(B_TRUE
/* demand */, demand
, prefetch
,
5337 !HDR_ISTYPE_METADATA(hdr
), data
, metadata
, hits
);
5340 /* a generic arc_read_done_func_t which you can use */
5342 arc_bcopy_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5343 arc_buf_t
*buf
, void *arg
)
5345 (void) zio
, (void) zb
, (void) bp
;
5350 memcpy(arg
, buf
->b_data
, arc_buf_size(buf
));
5351 arc_buf_destroy(buf
, arg
);
5354 /* a generic arc_read_done_func_t */
5356 arc_getbuf_func(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
5357 arc_buf_t
*buf
, void *arg
)
5359 (void) zb
, (void) bp
;
5360 arc_buf_t
**bufp
= arg
;
5363 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
5366 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
5368 ASSERT(buf
->b_data
!= NULL
);
5373 arc_hdr_verify(arc_buf_hdr_t
*hdr
, blkptr_t
*bp
)
5375 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
5376 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, 0);
5377 ASSERT3U(arc_hdr_get_compress(hdr
), ==, ZIO_COMPRESS_OFF
);
5379 if (HDR_COMPRESSION_ENABLED(hdr
)) {
5380 ASSERT3U(arc_hdr_get_compress(hdr
), ==,
5381 BP_GET_COMPRESS(bp
));
5383 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
5384 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, BP_GET_PSIZE(bp
));
5385 ASSERT3U(!!HDR_PROTECTED(hdr
), ==, BP_IS_PROTECTED(bp
));
5390 arc_read_done(zio_t
*zio
)
5392 blkptr_t
*bp
= zio
->io_bp
;
5393 arc_buf_hdr_t
*hdr
= zio
->io_private
;
5394 kmutex_t
*hash_lock
= NULL
;
5395 arc_callback_t
*callback_list
;
5396 arc_callback_t
*acb
;
5399 * The hdr was inserted into hash-table and removed from lists
5400 * prior to starting I/O. We should find this header, since
5401 * it's in the hash table, and it should be legit since it's
5402 * not possible to evict it during the I/O. The only possible
5403 * reason for it not to be found is if we were freed during the
5406 if (HDR_IN_HASH_TABLE(hdr
)) {
5407 arc_buf_hdr_t
*found
;
5409 ASSERT3U(hdr
->b_birth
, ==, BP_GET_BIRTH(zio
->io_bp
));
5410 ASSERT3U(hdr
->b_dva
.dva_word
[0], ==,
5411 BP_IDENTITY(zio
->io_bp
)->dva_word
[0]);
5412 ASSERT3U(hdr
->b_dva
.dva_word
[1], ==,
5413 BP_IDENTITY(zio
->io_bp
)->dva_word
[1]);
5415 found
= buf_hash_find(hdr
->b_spa
, zio
->io_bp
, &hash_lock
);
5417 ASSERT((found
== hdr
&&
5418 DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
5419 (found
== hdr
&& HDR_L2_READING(hdr
)));
5420 ASSERT3P(hash_lock
, !=, NULL
);
5423 if (BP_IS_PROTECTED(bp
)) {
5424 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
5425 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
5426 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
5427 hdr
->b_crypt_hdr
.b_iv
);
5429 if (zio
->io_error
== 0) {
5430 if (BP_GET_TYPE(bp
) == DMU_OT_INTENT_LOG
) {
5433 tmpbuf
= abd_borrow_buf_copy(zio
->io_abd
,
5434 sizeof (zil_chain_t
));
5435 zio_crypt_decode_mac_zil(tmpbuf
,
5436 hdr
->b_crypt_hdr
.b_mac
);
5437 abd_return_buf(zio
->io_abd
, tmpbuf
,
5438 sizeof (zil_chain_t
));
5440 zio_crypt_decode_mac_bp(bp
,
5441 hdr
->b_crypt_hdr
.b_mac
);
5446 if (zio
->io_error
== 0) {
5447 /* byteswap if necessary */
5448 if (BP_SHOULD_BYTESWAP(zio
->io_bp
)) {
5449 if (BP_GET_LEVEL(zio
->io_bp
) > 0) {
5450 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
5452 hdr
->b_l1hdr
.b_byteswap
=
5453 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
5456 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
5458 if (!HDR_L2_READING(hdr
)) {
5459 hdr
->b_complevel
= zio
->io_prop
.zp_complevel
;
5463 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_EVICTED
);
5464 if (l2arc_noprefetch
&& HDR_PREFETCH(hdr
))
5465 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2CACHE
);
5467 callback_list
= hdr
->b_l1hdr
.b_acb
;
5468 ASSERT3P(callback_list
, !=, NULL
);
5469 hdr
->b_l1hdr
.b_acb
= NULL
;
5472 * If a read request has a callback (i.e. acb_done is not NULL), then we
5473 * make a buf containing the data according to the parameters which were
5474 * passed in. The implementation of arc_buf_alloc_impl() ensures that we
5475 * aren't needlessly decompressing the data multiple times.
5477 int callback_cnt
= 0;
5478 for (acb
= callback_list
; acb
!= NULL
; acb
= acb
->acb_next
) {
5480 /* We need the last one to call below in original order. */
5481 callback_list
= acb
;
5483 if (!acb
->acb_done
|| acb
->acb_nobuf
)
5488 if (zio
->io_error
!= 0)
5491 int error
= arc_buf_alloc_impl(hdr
, zio
->io_spa
,
5492 &acb
->acb_zb
, acb
->acb_private
, acb
->acb_encrypted
,
5493 acb
->acb_compressed
, acb
->acb_noauth
, B_TRUE
,
5497 * Assert non-speculative zios didn't fail because an
5498 * encryption key wasn't loaded
5500 ASSERT((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) ||
5504 * If we failed to decrypt, report an error now (as the zio
5505 * layer would have done if it had done the transforms).
5507 if (error
== ECKSUM
) {
5508 ASSERT(BP_IS_PROTECTED(bp
));
5509 error
= SET_ERROR(EIO
);
5510 if ((zio
->io_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
5511 spa_log_error(zio
->io_spa
, &acb
->acb_zb
,
5512 BP_GET_LOGICAL_BIRTH(zio
->io_bp
));
5513 (void) zfs_ereport_post(
5514 FM_EREPORT_ZFS_AUTHENTICATION
,
5515 zio
->io_spa
, NULL
, &acb
->acb_zb
, zio
, 0);
5521 * Decompression or decryption failed. Set
5522 * io_error so that when we call acb_done
5523 * (below), we will indicate that the read
5524 * failed. Note that in the unusual case
5525 * where one callback is compressed and another
5526 * uncompressed, we will mark all of them
5527 * as failed, even though the uncompressed
5528 * one can't actually fail. In this case,
5529 * the hdr will not be anonymous, because
5530 * if there are multiple callbacks, it's
5531 * because multiple threads found the same
5532 * arc buf in the hash table.
5534 zio
->io_error
= error
;
5539 * If there are multiple callbacks, we must have the hash lock,
5540 * because the only way for multiple threads to find this hdr is
5541 * in the hash table. This ensures that if there are multiple
5542 * callbacks, the hdr is not anonymous. If it were anonymous,
5543 * we couldn't use arc_buf_destroy() in the error case below.
5545 ASSERT(callback_cnt
< 2 || hash_lock
!= NULL
);
5547 if (zio
->io_error
== 0) {
5548 arc_hdr_verify(hdr
, zio
->io_bp
);
5550 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
5551 if (hdr
->b_l1hdr
.b_state
!= arc_anon
)
5552 arc_change_state(arc_anon
, hdr
);
5553 if (HDR_IN_HASH_TABLE(hdr
))
5554 buf_hash_remove(hdr
);
5557 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5558 (void) remove_reference(hdr
, hdr
);
5560 if (hash_lock
!= NULL
)
5561 mutex_exit(hash_lock
);
5563 /* execute each callback and free its structure */
5564 while ((acb
= callback_list
) != NULL
) {
5565 if (acb
->acb_done
!= NULL
) {
5566 if (zio
->io_error
!= 0 && acb
->acb_buf
!= NULL
) {
5568 * If arc_buf_alloc_impl() fails during
5569 * decompression, the buf will still be
5570 * allocated, and needs to be freed here.
5572 arc_buf_destroy(acb
->acb_buf
,
5574 acb
->acb_buf
= NULL
;
5576 acb
->acb_done(zio
, &zio
->io_bookmark
, zio
->io_bp
,
5577 acb
->acb_buf
, acb
->acb_private
);
5580 if (acb
->acb_zio_dummy
!= NULL
) {
5581 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
5582 zio_nowait(acb
->acb_zio_dummy
);
5585 callback_list
= acb
->acb_prev
;
5586 if (acb
->acb_wait
) {
5587 mutex_enter(&acb
->acb_wait_lock
);
5588 acb
->acb_wait_error
= zio
->io_error
;
5589 acb
->acb_wait
= B_FALSE
;
5590 cv_signal(&acb
->acb_wait_cv
);
5591 mutex_exit(&acb
->acb_wait_lock
);
5592 /* acb will be freed by the waiting thread. */
5594 kmem_free(acb
, sizeof (arc_callback_t
));
5600 * Lookup the block at the specified DVA (in bp), and return the manner in
5601 * which the block is cached. A zero return indicates not cached.
5604 arc_cached(spa_t
*spa
, const blkptr_t
*bp
)
5606 arc_buf_hdr_t
*hdr
= NULL
;
5607 kmutex_t
*hash_lock
= NULL
;
5608 uint64_t guid
= spa_load_guid(spa
);
5611 if (BP_IS_EMBEDDED(bp
))
5612 return (ARC_CACHED_EMBEDDED
);
5614 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
5618 if (HDR_HAS_L1HDR(hdr
)) {
5619 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
5621 * We switch to ensure that any future arc_state_type_t
5622 * changes are handled. This is just a shift to promote
5623 * more compile-time checking.
5625 switch (state
->arcs_state
) {
5626 case ARC_STATE_ANON
:
5629 flags
|= ARC_CACHED_IN_MRU
| ARC_CACHED_IN_L1
;
5632 flags
|= ARC_CACHED_IN_MFU
| ARC_CACHED_IN_L1
;
5634 case ARC_STATE_UNCACHED
:
5635 /* The header is still in L1, probably not for long */
5636 flags
|= ARC_CACHED_IN_L1
;
5642 if (HDR_HAS_L2HDR(hdr
))
5643 flags
|= ARC_CACHED_IN_L2
;
5645 mutex_exit(hash_lock
);
5651 * "Read" the block at the specified DVA (in bp) via the
5652 * cache. If the block is found in the cache, invoke the provided
5653 * callback immediately and return. Note that the `zio' parameter
5654 * in the callback will be NULL in this case, since no IO was
5655 * required. If the block is not in the cache pass the read request
5656 * on to the spa with a substitute callback function, so that the
5657 * requested block will be added to the cache.
5659 * If a read request arrives for a block that has a read in-progress,
5660 * either wait for the in-progress read to complete (and return the
5661 * results); or, if this is a read with a "done" func, add a record
5662 * to the read to invoke the "done" func when the read completes,
5663 * and return; or just return.
5665 * arc_read_done() will invoke all the requested "done" functions
5666 * for readers of this block.
5669 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
5670 arc_read_done_func_t
*done
, void *private, zio_priority_t priority
,
5671 int zio_flags
, arc_flags_t
*arc_flags
, const zbookmark_phys_t
*zb
)
5673 arc_buf_hdr_t
*hdr
= NULL
;
5674 kmutex_t
*hash_lock
= NULL
;
5676 uint64_t guid
= spa_load_guid(spa
);
5677 boolean_t compressed_read
= (zio_flags
& ZIO_FLAG_RAW_COMPRESS
) != 0;
5678 boolean_t encrypted_read
= BP_IS_ENCRYPTED(bp
) &&
5679 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
5680 boolean_t noauth_read
= BP_IS_AUTHENTICATED(bp
) &&
5681 (zio_flags
& ZIO_FLAG_RAW_ENCRYPT
) != 0;
5682 boolean_t embedded_bp
= !!BP_IS_EMBEDDED(bp
);
5683 boolean_t no_buf
= *arc_flags
& ARC_FLAG_NO_BUF
;
5684 arc_buf_t
*buf
= NULL
;
5687 ASSERT(!embedded_bp
||
5688 BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
);
5689 ASSERT(!BP_IS_HOLE(bp
));
5690 ASSERT(!BP_IS_REDACTED(bp
));
5693 * Normally SPL_FSTRANS will already be set since kernel threads which
5694 * expect to call the DMU interfaces will set it when created. System
5695 * calls are similarly handled by setting/cleaning the bit in the
5696 * registered callback (module/os/.../zfs/zpl_*).
5698 * External consumers such as Lustre which call the exported DMU
5699 * interfaces may not have set SPL_FSTRANS. To avoid a deadlock
5700 * on the hash_lock always set and clear the bit.
5702 fstrans_cookie_t cookie
= spl_fstrans_mark();
5706 * Embedded BP's have no DVA and require no I/O to "read".
5707 * Create an anonymous arc buf to back it.
5709 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
5713 * Determine if we have an L1 cache hit or a cache miss. For simplicity
5714 * we maintain encrypted data separately from compressed / uncompressed
5715 * data. If the user is requesting raw encrypted data and we don't have
5716 * that in the header we will read from disk to guarantee that we can
5717 * get it even if the encryption keys aren't loaded.
5719 if (hdr
!= NULL
&& HDR_HAS_L1HDR(hdr
) && (HDR_HAS_RABD(hdr
) ||
5720 (hdr
->b_l1hdr
.b_pabd
!= NULL
&& !encrypted_read
))) {
5721 boolean_t is_data
= !HDR_ISTYPE_METADATA(hdr
);
5724 * Verify the block pointer contents are reasonable. This
5725 * should always be the case since the blkptr is protected by
5728 if (!zfs_blkptr_verify(spa
, bp
, BLK_CONFIG_SKIP
,
5730 mutex_exit(hash_lock
);
5731 rc
= SET_ERROR(ECKSUM
);
5735 if (HDR_IO_IN_PROGRESS(hdr
)) {
5736 if (*arc_flags
& ARC_FLAG_CACHED_ONLY
) {
5737 mutex_exit(hash_lock
);
5738 ARCSTAT_BUMP(arcstat_cached_only_in_progress
);
5739 rc
= SET_ERROR(ENOENT
);
5743 zio_t
*head_zio
= hdr
->b_l1hdr
.b_acb
->acb_zio_head
;
5744 ASSERT3P(head_zio
, !=, NULL
);
5745 if ((hdr
->b_flags
& ARC_FLAG_PRIO_ASYNC_READ
) &&
5746 priority
== ZIO_PRIORITY_SYNC_READ
) {
5748 * This is a sync read that needs to wait for
5749 * an in-flight async read. Request that the
5750 * zio have its priority upgraded.
5752 zio_change_priority(head_zio
, priority
);
5753 DTRACE_PROBE1(arc__async__upgrade__sync
,
5754 arc_buf_hdr_t
*, hdr
);
5755 ARCSTAT_BUMP(arcstat_async_upgrade_sync
);
5758 DTRACE_PROBE1(arc__iohit
, arc_buf_hdr_t
*, hdr
);
5759 arc_access(hdr
, *arc_flags
, B_FALSE
);
5762 * If there are multiple threads reading the same block
5763 * and that block is not yet in the ARC, then only one
5764 * thread will do the physical I/O and all other
5765 * threads will wait until that I/O completes.
5766 * Synchronous reads use the acb_wait_cv whereas nowait
5767 * reads register a callback. Both are signalled/called
5770 * Errors of the physical I/O may need to be propagated.
5771 * Synchronous read errors are returned here from
5772 * arc_read_done via acb_wait_error. Nowait reads
5773 * attach the acb_zio_dummy zio to pio and
5774 * arc_read_done propagates the physical I/O's io_error
5775 * to acb_zio_dummy, and thereby to pio.
5777 arc_callback_t
*acb
= NULL
;
5778 if (done
|| pio
|| *arc_flags
& ARC_FLAG_WAIT
) {
5779 acb
= kmem_zalloc(sizeof (arc_callback_t
),
5781 acb
->acb_done
= done
;
5782 acb
->acb_private
= private;
5783 acb
->acb_compressed
= compressed_read
;
5784 acb
->acb_encrypted
= encrypted_read
;
5785 acb
->acb_noauth
= noauth_read
;
5786 acb
->acb_nobuf
= no_buf
;
5787 if (*arc_flags
& ARC_FLAG_WAIT
) {
5788 acb
->acb_wait
= B_TRUE
;
5789 mutex_init(&acb
->acb_wait_lock
, NULL
,
5790 MUTEX_DEFAULT
, NULL
);
5791 cv_init(&acb
->acb_wait_cv
, NULL
,
5796 acb
->acb_zio_dummy
= zio_null(pio
,
5797 spa
, NULL
, NULL
, NULL
, zio_flags
);
5799 acb
->acb_zio_head
= head_zio
;
5800 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
5801 hdr
->b_l1hdr
.b_acb
->acb_prev
= acb
;
5802 hdr
->b_l1hdr
.b_acb
= acb
;
5804 mutex_exit(hash_lock
);
5806 ARCSTAT_BUMP(arcstat_iohits
);
5807 ARCSTAT_CONDSTAT(!(*arc_flags
& ARC_FLAG_PREFETCH
),
5808 demand
, prefetch
, is_data
, data
, metadata
, iohits
);
5810 if (*arc_flags
& ARC_FLAG_WAIT
) {
5811 mutex_enter(&acb
->acb_wait_lock
);
5812 while (acb
->acb_wait
) {
5813 cv_wait(&acb
->acb_wait_cv
,
5814 &acb
->acb_wait_lock
);
5816 rc
= acb
->acb_wait_error
;
5817 mutex_exit(&acb
->acb_wait_lock
);
5818 mutex_destroy(&acb
->acb_wait_lock
);
5819 cv_destroy(&acb
->acb_wait_cv
);
5820 kmem_free(acb
, sizeof (arc_callback_t
));
5825 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
5826 hdr
->b_l1hdr
.b_state
== arc_mfu
||
5827 hdr
->b_l1hdr
.b_state
== arc_uncached
);
5829 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
5830 arc_access(hdr
, *arc_flags
, B_TRUE
);
5832 if (done
&& !no_buf
) {
5833 ASSERT(!embedded_bp
|| !BP_IS_HOLE(bp
));
5835 /* Get a buf with the desired data in it. */
5836 rc
= arc_buf_alloc_impl(hdr
, spa
, zb
, private,
5837 encrypted_read
, compressed_read
, noauth_read
,
5841 * Convert authentication and decryption errors
5842 * to EIO (and generate an ereport if needed)
5843 * before leaving the ARC.
5845 rc
= SET_ERROR(EIO
);
5846 if ((zio_flags
& ZIO_FLAG_SPECULATIVE
) == 0) {
5847 spa_log_error(spa
, zb
, hdr
->b_birth
);
5848 (void) zfs_ereport_post(
5849 FM_EREPORT_ZFS_AUTHENTICATION
,
5850 spa
, NULL
, zb
, NULL
, 0);
5854 arc_buf_destroy_impl(buf
);
5856 (void) remove_reference(hdr
, private);
5859 /* assert any errors weren't due to unloaded keys */
5860 ASSERT((zio_flags
& ZIO_FLAG_SPECULATIVE
) ||
5863 mutex_exit(hash_lock
);
5864 ARCSTAT_BUMP(arcstat_hits
);
5865 ARCSTAT_CONDSTAT(!(*arc_flags
& ARC_FLAG_PREFETCH
),
5866 demand
, prefetch
, is_data
, data
, metadata
, hits
);
5867 *arc_flags
|= ARC_FLAG_CACHED
;
5870 uint64_t lsize
= BP_GET_LSIZE(bp
);
5871 uint64_t psize
= BP_GET_PSIZE(bp
);
5872 arc_callback_t
*acb
;
5875 boolean_t devw
= B_FALSE
;
5878 int alloc_flags
= encrypted_read
? ARC_HDR_ALLOC_RDATA
: 0;
5879 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
5881 if (*arc_flags
& ARC_FLAG_CACHED_ONLY
) {
5882 if (hash_lock
!= NULL
)
5883 mutex_exit(hash_lock
);
5884 rc
= SET_ERROR(ENOENT
);
5889 * Verify the block pointer contents are reasonable. This
5890 * should always be the case since the blkptr is protected by
5893 if (!zfs_blkptr_verify(spa
, bp
,
5894 (zio_flags
& ZIO_FLAG_CONFIG_WRITER
) ?
5895 BLK_CONFIG_HELD
: BLK_CONFIG_NEEDED
, BLK_VERIFY_LOG
)) {
5896 if (hash_lock
!= NULL
)
5897 mutex_exit(hash_lock
);
5898 rc
= SET_ERROR(ECKSUM
);
5904 * This block is not in the cache or it has
5907 arc_buf_hdr_t
*exists
= NULL
;
5908 hdr
= arc_hdr_alloc(guid
, psize
, lsize
,
5909 BP_IS_PROTECTED(bp
), BP_GET_COMPRESS(bp
), 0, type
);
5912 hdr
->b_dva
= *BP_IDENTITY(bp
);
5913 hdr
->b_birth
= BP_GET_BIRTH(bp
);
5914 exists
= buf_hash_insert(hdr
, &hash_lock
);
5916 if (exists
!= NULL
) {
5917 /* somebody beat us to the hash insert */
5918 mutex_exit(hash_lock
);
5919 buf_discard_identity(hdr
);
5920 arc_hdr_destroy(hdr
);
5921 goto top
; /* restart the IO request */
5925 * This block is in the ghost cache or encrypted data
5926 * was requested and we didn't have it. If it was
5927 * L2-only (and thus didn't have an L1 hdr),
5928 * we realloc the header to add an L1 hdr.
5930 if (!HDR_HAS_L1HDR(hdr
)) {
5931 hdr
= arc_hdr_realloc(hdr
, hdr_l2only_cache
,
5935 if (GHOST_STATE(hdr
->b_l1hdr
.b_state
)) {
5936 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
5937 ASSERT(!HDR_HAS_RABD(hdr
));
5938 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
5939 ASSERT0(zfs_refcount_count(
5940 &hdr
->b_l1hdr
.b_refcnt
));
5941 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
5943 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
5945 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
5947 * If this header already had an IO in progress
5948 * and we are performing another IO to fetch
5949 * encrypted data we must wait until the first
5950 * IO completes so as not to confuse
5951 * arc_read_done(). This should be very rare
5952 * and so the performance impact shouldn't
5955 arc_callback_t
*acb
= kmem_zalloc(
5956 sizeof (arc_callback_t
), KM_SLEEP
);
5957 acb
->acb_wait
= B_TRUE
;
5958 mutex_init(&acb
->acb_wait_lock
, NULL
,
5959 MUTEX_DEFAULT
, NULL
);
5960 cv_init(&acb
->acb_wait_cv
, NULL
, CV_DEFAULT
,
5963 hdr
->b_l1hdr
.b_acb
->acb_zio_head
;
5964 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
5965 hdr
->b_l1hdr
.b_acb
->acb_prev
= acb
;
5966 hdr
->b_l1hdr
.b_acb
= acb
;
5967 mutex_exit(hash_lock
);
5968 mutex_enter(&acb
->acb_wait_lock
);
5969 while (acb
->acb_wait
) {
5970 cv_wait(&acb
->acb_wait_cv
,
5971 &acb
->acb_wait_lock
);
5973 mutex_exit(&acb
->acb_wait_lock
);
5974 mutex_destroy(&acb
->acb_wait_lock
);
5975 cv_destroy(&acb
->acb_wait_cv
);
5976 kmem_free(acb
, sizeof (arc_callback_t
));
5980 if (*arc_flags
& ARC_FLAG_UNCACHED
) {
5981 arc_hdr_set_flags(hdr
, ARC_FLAG_UNCACHED
);
5982 if (!encrypted_read
)
5983 alloc_flags
|= ARC_HDR_ALLOC_LINEAR
;
5987 * Take additional reference for IO_IN_PROGRESS. It stops
5988 * arc_access() from putting this header without any buffers
5989 * and so other references but obviously nonevictable onto
5990 * the evictable list of MRU or MFU state.
5992 add_reference(hdr
, hdr
);
5994 arc_access(hdr
, *arc_flags
, B_FALSE
);
5995 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5996 arc_hdr_alloc_abd(hdr
, alloc_flags
);
5997 if (encrypted_read
) {
5998 ASSERT(HDR_HAS_RABD(hdr
));
5999 size
= HDR_GET_PSIZE(hdr
);
6000 hdr_abd
= hdr
->b_crypt_hdr
.b_rabd
;
6001 zio_flags
|= ZIO_FLAG_RAW
;
6003 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
6004 size
= arc_hdr_size(hdr
);
6005 hdr_abd
= hdr
->b_l1hdr
.b_pabd
;
6007 if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
) {
6008 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
6012 * For authenticated bp's, we do not ask the ZIO layer
6013 * to authenticate them since this will cause the entire
6014 * IO to fail if the key isn't loaded. Instead, we
6015 * defer authentication until arc_buf_fill(), which will
6016 * verify the data when the key is available.
6018 if (BP_IS_AUTHENTICATED(bp
))
6019 zio_flags
|= ZIO_FLAG_RAW_ENCRYPT
;
6022 if (BP_IS_AUTHENTICATED(bp
))
6023 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6024 if (BP_GET_LEVEL(bp
) > 0)
6025 arc_hdr_set_flags(hdr
, ARC_FLAG_INDIRECT
);
6026 ASSERT(!GHOST_STATE(hdr
->b_l1hdr
.b_state
));
6028 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
6029 acb
->acb_done
= done
;
6030 acb
->acb_private
= private;
6031 acb
->acb_compressed
= compressed_read
;
6032 acb
->acb_encrypted
= encrypted_read
;
6033 acb
->acb_noauth
= noauth_read
;
6036 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6037 hdr
->b_l1hdr
.b_acb
= acb
;
6039 if (HDR_HAS_L2HDR(hdr
) &&
6040 (vd
= hdr
->b_l2hdr
.b_dev
->l2ad_vdev
) != NULL
) {
6041 devw
= hdr
->b_l2hdr
.b_dev
->l2ad_writing
;
6042 addr
= hdr
->b_l2hdr
.b_daddr
;
6044 * Lock out L2ARC device removal.
6046 if (vdev_is_dead(vd
) ||
6047 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
6052 * We count both async reads and scrub IOs as asynchronous so
6053 * that both can be upgraded in the event of a cache hit while
6054 * the read IO is still in-flight.
6056 if (priority
== ZIO_PRIORITY_ASYNC_READ
||
6057 priority
== ZIO_PRIORITY_SCRUB
)
6058 arc_hdr_set_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6060 arc_hdr_clear_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
6063 * At this point, we have a level 1 cache miss or a blkptr
6064 * with embedded data. Try again in L2ARC if possible.
6066 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, lsize
);
6069 * Skip ARC stat bump for block pointers with embedded
6070 * data. The data are read from the blkptr itself via
6071 * decode_embedded_bp_compressed().
6074 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
,
6075 blkptr_t
*, bp
, uint64_t, lsize
,
6076 zbookmark_phys_t
*, zb
);
6077 ARCSTAT_BUMP(arcstat_misses
);
6078 ARCSTAT_CONDSTAT(!(*arc_flags
& ARC_FLAG_PREFETCH
),
6079 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
), data
,
6081 zfs_racct_read(spa
, size
, 1, 0);
6084 /* Check if the spa even has l2 configured */
6085 const boolean_t spa_has_l2
= l2arc_ndev
!= 0 &&
6086 spa
->spa_l2cache
.sav_count
> 0;
6088 if (vd
!= NULL
&& spa_has_l2
&& !(l2arc_norw
&& devw
)) {
6090 * Read from the L2ARC if the following are true:
6091 * 1. The L2ARC vdev was previously cached.
6092 * 2. This buffer still has L2ARC metadata.
6093 * 3. This buffer isn't currently writing to the L2ARC.
6094 * 4. The L2ARC entry wasn't evicted, which may
6095 * also have invalidated the vdev.
6097 if (HDR_HAS_L2HDR(hdr
) &&
6098 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
)) {
6099 l2arc_read_callback_t
*cb
;
6103 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
6104 ARCSTAT_BUMP(arcstat_l2_hits
);
6105 hdr
->b_l2hdr
.b_hits
++;
6107 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
6109 cb
->l2rcb_hdr
= hdr
;
6112 cb
->l2rcb_flags
= zio_flags
;
6115 * When Compressed ARC is disabled, but the
6116 * L2ARC block is compressed, arc_hdr_size()
6117 * will have returned LSIZE rather than PSIZE.
6119 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
6120 !HDR_COMPRESSION_ENABLED(hdr
) &&
6121 HDR_GET_PSIZE(hdr
) != 0) {
6122 size
= HDR_GET_PSIZE(hdr
);
6125 asize
= vdev_psize_to_asize(vd
, size
);
6126 if (asize
!= size
) {
6127 abd
= abd_alloc_for_io(asize
,
6128 HDR_ISTYPE_METADATA(hdr
));
6129 cb
->l2rcb_abd
= abd
;
6134 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
6135 addr
+ asize
<= vd
->vdev_psize
-
6136 VDEV_LABEL_END_SIZE
);
6139 * l2arc read. The SCL_L2ARC lock will be
6140 * released by l2arc_read_done().
6141 * Issue a null zio if the underlying buffer
6142 * was squashed to zero size by compression.
6144 ASSERT3U(arc_hdr_get_compress(hdr
), !=,
6145 ZIO_COMPRESS_EMPTY
);
6146 rzio
= zio_read_phys(pio
, vd
, addr
,
6149 l2arc_read_done
, cb
, priority
,
6150 zio_flags
| ZIO_FLAG_CANFAIL
|
6151 ZIO_FLAG_DONT_PROPAGATE
|
6152 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
6153 acb
->acb_zio_head
= rzio
;
6155 if (hash_lock
!= NULL
)
6156 mutex_exit(hash_lock
);
6158 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
6160 ARCSTAT_INCR(arcstat_l2_read_bytes
,
6161 HDR_GET_PSIZE(hdr
));
6163 if (*arc_flags
& ARC_FLAG_NOWAIT
) {
6168 ASSERT(*arc_flags
& ARC_FLAG_WAIT
);
6169 if (zio_wait(rzio
) == 0)
6172 /* l2arc read error; goto zio_read() */
6173 if (hash_lock
!= NULL
)
6174 mutex_enter(hash_lock
);
6176 DTRACE_PROBE1(l2arc__miss
,
6177 arc_buf_hdr_t
*, hdr
);
6178 ARCSTAT_BUMP(arcstat_l2_misses
);
6179 if (HDR_L2_WRITING(hdr
))
6180 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
6181 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6185 spa_config_exit(spa
, SCL_L2ARC
, vd
);
6188 * Only a spa with l2 should contribute to l2
6189 * miss stats. (Including the case of having a
6190 * faulted cache device - that's also a miss.)
6194 * Skip ARC stat bump for block pointers with
6195 * embedded data. The data are read from the
6197 * decode_embedded_bp_compressed().
6200 DTRACE_PROBE1(l2arc__miss
,
6201 arc_buf_hdr_t
*, hdr
);
6202 ARCSTAT_BUMP(arcstat_l2_misses
);
6207 rzio
= zio_read(pio
, spa
, bp
, hdr_abd
, size
,
6208 arc_read_done
, hdr
, priority
, zio_flags
, zb
);
6209 acb
->acb_zio_head
= rzio
;
6211 if (hash_lock
!= NULL
)
6212 mutex_exit(hash_lock
);
6214 if (*arc_flags
& ARC_FLAG_WAIT
) {
6215 rc
= zio_wait(rzio
);
6219 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
6224 /* embedded bps don't actually go to disk */
6226 spa_read_history_add(spa
, zb
, *arc_flags
);
6227 spl_fstrans_unmark(cookie
);
6232 done(NULL
, zb
, bp
, buf
, private);
6233 if (pio
&& rc
!= 0) {
6234 zio_t
*zio
= zio_null(pio
, spa
, NULL
, NULL
, NULL
, zio_flags
);
6242 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
6246 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
6248 p
->p_private
= private;
6249 list_link_init(&p
->p_node
);
6250 zfs_refcount_create(&p
->p_refcnt
);
6252 mutex_enter(&arc_prune_mtx
);
6253 zfs_refcount_add(&p
->p_refcnt
, &arc_prune_list
);
6254 list_insert_head(&arc_prune_list
, p
);
6255 mutex_exit(&arc_prune_mtx
);
6261 arc_remove_prune_callback(arc_prune_t
*p
)
6263 boolean_t wait
= B_FALSE
;
6264 mutex_enter(&arc_prune_mtx
);
6265 list_remove(&arc_prune_list
, p
);
6266 if (zfs_refcount_remove(&p
->p_refcnt
, &arc_prune_list
) > 0)
6268 mutex_exit(&arc_prune_mtx
);
6270 /* wait for arc_prune_task to finish */
6272 taskq_wait_outstanding(arc_prune_taskq
, 0);
6273 ASSERT0(zfs_refcount_count(&p
->p_refcnt
));
6274 zfs_refcount_destroy(&p
->p_refcnt
);
6275 kmem_free(p
, sizeof (*p
));
6279 * Helper function for arc_prune_async() it is responsible for safely
6280 * handling the execution of a registered arc_prune_func_t.
6283 arc_prune_task(void *ptr
)
6285 arc_prune_t
*ap
= (arc_prune_t
*)ptr
;
6286 arc_prune_func_t
*func
= ap
->p_pfunc
;
6289 func(ap
->p_adjust
, ap
->p_private
);
6291 (void) zfs_refcount_remove(&ap
->p_refcnt
, func
);
6295 * Notify registered consumers they must drop holds on a portion of the ARC
6296 * buffers they reference. This provides a mechanism to ensure the ARC can
6297 * honor the metadata limit and reclaim otherwise pinned ARC buffers.
6299 * This operation is performed asynchronously so it may be safely called
6300 * in the context of the arc_reclaim_thread(). A reference is taken here
6301 * for each registered arc_prune_t and the arc_prune_task() is responsible
6302 * for releasing it once the registered arc_prune_func_t has completed.
6305 arc_prune_async(uint64_t adjust
)
6309 mutex_enter(&arc_prune_mtx
);
6310 for (ap
= list_head(&arc_prune_list
); ap
!= NULL
;
6311 ap
= list_next(&arc_prune_list
, ap
)) {
6313 if (zfs_refcount_count(&ap
->p_refcnt
) >= 2)
6316 zfs_refcount_add(&ap
->p_refcnt
, ap
->p_pfunc
);
6317 ap
->p_adjust
= adjust
;
6318 if (taskq_dispatch(arc_prune_taskq
, arc_prune_task
,
6319 ap
, TQ_SLEEP
) == TASKQID_INVALID
) {
6320 (void) zfs_refcount_remove(&ap
->p_refcnt
, ap
->p_pfunc
);
6323 ARCSTAT_BUMP(arcstat_prune
);
6325 mutex_exit(&arc_prune_mtx
);
6329 * Notify the arc that a block was freed, and thus will never be used again.
6332 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
6335 kmutex_t
*hash_lock
;
6336 uint64_t guid
= spa_load_guid(spa
);
6338 ASSERT(!BP_IS_EMBEDDED(bp
));
6340 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
6345 * We might be trying to free a block that is still doing I/O
6346 * (i.e. prefetch) or has some other reference (i.e. a dedup-ed,
6347 * dmu_sync-ed block). A block may also have a reference if it is
6348 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would
6349 * have written the new block to its final resting place on disk but
6350 * without the dedup flag set. This would have left the hdr in the MRU
6351 * state and discoverable. When the txg finally syncs it detects that
6352 * the block was overridden in open context and issues an override I/O.
6353 * Since this is a dedup block, the override I/O will determine if the
6354 * block is already in the DDT. If so, then it will replace the io_bp
6355 * with the bp from the DDT and allow the I/O to finish. When the I/O
6356 * reaches the done callback, dbuf_write_override_done, it will
6357 * check to see if the io_bp and io_bp_override are identical.
6358 * If they are not, then it indicates that the bp was replaced with
6359 * the bp in the DDT and the override bp is freed. This allows
6360 * us to arrive here with a reference on a block that is being
6361 * freed. So if we have an I/O in progress, or a reference to
6362 * this hdr, then we don't destroy the hdr.
6364 if (!HDR_HAS_L1HDR(hdr
) ||
6365 zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
6366 arc_change_state(arc_anon
, hdr
);
6367 arc_hdr_destroy(hdr
);
6368 mutex_exit(hash_lock
);
6370 mutex_exit(hash_lock
);
6376 * Release this buffer from the cache, making it an anonymous buffer. This
6377 * must be done after a read and prior to modifying the buffer contents.
6378 * If the buffer has more than one reference, we must make
6379 * a new hdr for the buffer.
6382 arc_release(arc_buf_t
*buf
, const void *tag
)
6384 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6387 * It would be nice to assert that if its DMU metadata (level >
6388 * 0 || it's the dnode file), then it must be syncing context.
6389 * But we don't know that information at this level.
6392 ASSERT(HDR_HAS_L1HDR(hdr
));
6395 * We don't grab the hash lock prior to this check, because if
6396 * the buffer's header is in the arc_anon state, it won't be
6397 * linked into the hash table.
6399 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
6400 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6401 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
6402 ASSERT(!HDR_HAS_L2HDR(hdr
));
6404 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, buf
);
6405 ASSERT(ARC_BUF_LAST(buf
));
6406 ASSERT3S(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
), ==, 1);
6407 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6409 hdr
->b_l1hdr
.b_arc_access
= 0;
6412 * If the buf is being overridden then it may already
6413 * have a hdr that is not empty.
6415 buf_discard_identity(hdr
);
6421 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
6422 mutex_enter(hash_lock
);
6425 * This assignment is only valid as long as the hash_lock is
6426 * held, we must be careful not to reference state or the
6427 * b_state field after dropping the lock.
6429 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
6430 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
6431 ASSERT3P(state
, !=, arc_anon
);
6433 /* this buffer is not on any list */
6434 ASSERT3S(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
), >, 0);
6436 if (HDR_HAS_L2HDR(hdr
)) {
6437 mutex_enter(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6440 * We have to recheck this conditional again now that
6441 * we're holding the l2ad_mtx to prevent a race with
6442 * another thread which might be concurrently calling
6443 * l2arc_evict(). In that case, l2arc_evict() might have
6444 * destroyed the header's L2 portion as we were waiting
6445 * to acquire the l2ad_mtx.
6447 if (HDR_HAS_L2HDR(hdr
))
6448 arc_hdr_l2hdr_destroy(hdr
);
6450 mutex_exit(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
6454 * Do we have more than one buf?
6456 if (hdr
->b_l1hdr
.b_buf
!= buf
|| !ARC_BUF_LAST(buf
)) {
6457 arc_buf_hdr_t
*nhdr
;
6458 uint64_t spa
= hdr
->b_spa
;
6459 uint64_t psize
= HDR_GET_PSIZE(hdr
);
6460 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
6461 boolean_t
protected = HDR_PROTECTED(hdr
);
6462 enum zio_compress compress
= arc_hdr_get_compress(hdr
);
6463 arc_buf_contents_t type
= arc_buf_type(hdr
);
6464 VERIFY3U(hdr
->b_type
, ==, type
);
6466 ASSERT(hdr
->b_l1hdr
.b_buf
!= buf
|| buf
->b_next
!= NULL
);
6467 VERIFY3S(remove_reference(hdr
, tag
), >, 0);
6469 if (ARC_BUF_SHARED(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
6470 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6471 ASSERT(ARC_BUF_LAST(buf
));
6475 * Pull the data off of this hdr and attach it to
6476 * a new anonymous hdr. Also find the last buffer
6477 * in the hdr's buffer list.
6479 arc_buf_t
*lastbuf
= arc_buf_remove(hdr
, buf
);
6480 ASSERT3P(lastbuf
, !=, NULL
);
6483 * If the current arc_buf_t and the hdr are sharing their data
6484 * buffer, then we must stop sharing that block.
6486 if (ARC_BUF_SHARED(buf
)) {
6487 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
6488 ASSERT(!arc_buf_is_shared(lastbuf
));
6491 * First, sever the block sharing relationship between
6492 * buf and the arc_buf_hdr_t.
6494 arc_unshare_buf(hdr
, buf
);
6497 * Now we need to recreate the hdr's b_pabd. Since we
6498 * have lastbuf handy, we try to share with it, but if
6499 * we can't then we allocate a new b_pabd and copy the
6500 * data from buf into it.
6502 if (arc_can_share(hdr
, lastbuf
)) {
6503 arc_share_buf(hdr
, lastbuf
);
6505 arc_hdr_alloc_abd(hdr
, 0);
6506 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
,
6507 buf
->b_data
, psize
);
6509 VERIFY3P(lastbuf
->b_data
, !=, NULL
);
6510 } else if (HDR_SHARED_DATA(hdr
)) {
6512 * Uncompressed shared buffers are always at the end
6513 * of the list. Compressed buffers don't have the
6514 * same requirements. This makes it hard to
6515 * simply assert that the lastbuf is shared so
6516 * we rely on the hdr's compression flags to determine
6517 * if we have a compressed, shared buffer.
6519 ASSERT(arc_buf_is_shared(lastbuf
) ||
6520 arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
);
6521 ASSERT(!arc_buf_is_shared(buf
));
6524 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
|| HDR_HAS_RABD(hdr
));
6525 ASSERT3P(state
, !=, arc_l2c_only
);
6527 (void) zfs_refcount_remove_many(&state
->arcs_size
[type
],
6528 arc_buf_size(buf
), buf
);
6530 if (zfs_refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
6531 ASSERT3P(state
, !=, arc_l2c_only
);
6532 (void) zfs_refcount_remove_many(
6533 &state
->arcs_esize
[type
],
6534 arc_buf_size(buf
), buf
);
6537 arc_cksum_verify(buf
);
6538 arc_buf_unwatch(buf
);
6540 /* if this is the last uncompressed buf free the checksum */
6541 if (!arc_hdr_has_uncompressed_buf(hdr
))
6542 arc_cksum_free(hdr
);
6544 mutex_exit(hash_lock
);
6546 nhdr
= arc_hdr_alloc(spa
, psize
, lsize
, protected,
6547 compress
, hdr
->b_complevel
, type
);
6548 ASSERT3P(nhdr
->b_l1hdr
.b_buf
, ==, NULL
);
6549 ASSERT0(zfs_refcount_count(&nhdr
->b_l1hdr
.b_refcnt
));
6550 VERIFY3U(nhdr
->b_type
, ==, type
);
6551 ASSERT(!HDR_SHARED_DATA(nhdr
));
6553 nhdr
->b_l1hdr
.b_buf
= buf
;
6554 (void) zfs_refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, tag
);
6557 (void) zfs_refcount_add_many(&arc_anon
->arcs_size
[type
],
6558 arc_buf_size(buf
), buf
);
6560 ASSERT(zfs_refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 1);
6561 /* protected by hash lock, or hdr is on arc_anon */
6562 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
6563 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6564 hdr
->b_l1hdr
.b_mru_hits
= 0;
6565 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
6566 hdr
->b_l1hdr
.b_mfu_hits
= 0;
6567 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
6568 arc_change_state(arc_anon
, hdr
);
6569 hdr
->b_l1hdr
.b_arc_access
= 0;
6571 mutex_exit(hash_lock
);
6572 buf_discard_identity(hdr
);
6578 arc_released(arc_buf_t
*buf
)
6580 return (buf
->b_data
!= NULL
&&
6581 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
6586 arc_referenced(arc_buf_t
*buf
)
6588 return (zfs_refcount_count(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6593 arc_write_ready(zio_t
*zio
)
6595 arc_write_callback_t
*callback
= zio
->io_private
;
6596 arc_buf_t
*buf
= callback
->awcb_buf
;
6597 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6598 blkptr_t
*bp
= zio
->io_bp
;
6599 uint64_t psize
= BP_IS_HOLE(bp
) ? 0 : BP_GET_PSIZE(bp
);
6600 fstrans_cookie_t cookie
= spl_fstrans_mark();
6602 ASSERT(HDR_HAS_L1HDR(hdr
));
6603 ASSERT(!zfs_refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
6604 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, NULL
);
6607 * If we're reexecuting this zio because the pool suspended, then
6608 * cleanup any state that was previously set the first time the
6609 * callback was invoked.
6611 if (zio
->io_flags
& ZIO_FLAG_REEXECUTED
) {
6612 arc_cksum_free(hdr
);
6613 arc_buf_unwatch(buf
);
6614 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
6615 if (ARC_BUF_SHARED(buf
)) {
6616 arc_unshare_buf(hdr
, buf
);
6618 ASSERT(!arc_buf_is_shared(buf
));
6619 arc_hdr_free_abd(hdr
, B_FALSE
);
6623 if (HDR_HAS_RABD(hdr
))
6624 arc_hdr_free_abd(hdr
, B_TRUE
);
6626 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6627 ASSERT(!HDR_HAS_RABD(hdr
));
6628 ASSERT(!HDR_SHARED_DATA(hdr
));
6629 ASSERT(!arc_buf_is_shared(buf
));
6631 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
6633 if (HDR_IO_IN_PROGRESS(hdr
)) {
6634 ASSERT(zio
->io_flags
& ZIO_FLAG_REEXECUTED
);
6636 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6637 add_reference(hdr
, hdr
); /* For IO_IN_PROGRESS. */
6640 if (BP_IS_PROTECTED(bp
)) {
6641 /* ZIL blocks are written through zio_rewrite */
6642 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
6644 if (BP_SHOULD_BYTESWAP(bp
)) {
6645 if (BP_GET_LEVEL(bp
) > 0) {
6646 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
6648 hdr
->b_l1hdr
.b_byteswap
=
6649 DMU_OT_BYTESWAP(BP_GET_TYPE(bp
));
6652 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
6655 arc_hdr_set_flags(hdr
, ARC_FLAG_PROTECTED
);
6656 hdr
->b_crypt_hdr
.b_ot
= BP_GET_TYPE(bp
);
6657 hdr
->b_crypt_hdr
.b_dsobj
= zio
->io_bookmark
.zb_objset
;
6658 zio_crypt_decode_params_bp(bp
, hdr
->b_crypt_hdr
.b_salt
,
6659 hdr
->b_crypt_hdr
.b_iv
);
6660 zio_crypt_decode_mac_bp(bp
, hdr
->b_crypt_hdr
.b_mac
);
6662 arc_hdr_clear_flags(hdr
, ARC_FLAG_PROTECTED
);
6666 * If this block was written for raw encryption but the zio layer
6667 * ended up only authenticating it, adjust the buffer flags now.
6669 if (BP_IS_AUTHENTICATED(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
6670 arc_hdr_set_flags(hdr
, ARC_FLAG_NOAUTH
);
6671 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
6672 if (BP_GET_COMPRESS(bp
) == ZIO_COMPRESS_OFF
)
6673 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
6674 } else if (BP_IS_HOLE(bp
) && ARC_BUF_ENCRYPTED(buf
)) {
6675 buf
->b_flags
&= ~ARC_BUF_FLAG_ENCRYPTED
;
6676 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
6679 /* this must be done after the buffer flags are adjusted */
6680 arc_cksum_compute(buf
);
6682 enum zio_compress compress
;
6683 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
6684 compress
= ZIO_COMPRESS_OFF
;
6686 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
6687 compress
= BP_GET_COMPRESS(bp
);
6689 HDR_SET_PSIZE(hdr
, psize
);
6690 arc_hdr_set_compress(hdr
, compress
);
6691 hdr
->b_complevel
= zio
->io_prop
.zp_complevel
;
6693 if (zio
->io_error
!= 0 || psize
== 0)
6697 * Fill the hdr with data. If the buffer is encrypted we have no choice
6698 * but to copy the data into b_radb. If the hdr is compressed, the data
6699 * we want is available from the zio, otherwise we can take it from
6702 * We might be able to share the buf's data with the hdr here. However,
6703 * doing so would cause the ARC to be full of linear ABDs if we write a
6704 * lot of shareable data. As a compromise, we check whether scattered
6705 * ABDs are allowed, and assume that if they are then the user wants
6706 * the ARC to be primarily filled with them regardless of the data being
6707 * written. Therefore, if they're allowed then we allocate one and copy
6708 * the data into it; otherwise, we share the data directly if we can.
6710 if (ARC_BUF_ENCRYPTED(buf
)) {
6711 ASSERT3U(psize
, >, 0);
6712 ASSERT(ARC_BUF_COMPRESSED(buf
));
6713 arc_hdr_alloc_abd(hdr
, ARC_HDR_ALLOC_RDATA
|
6714 ARC_HDR_USE_RESERVE
);
6715 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
6716 } else if (!(HDR_UNCACHED(hdr
) ||
6717 abd_size_alloc_linear(arc_buf_size(buf
))) ||
6718 !arc_can_share(hdr
, buf
)) {
6720 * Ideally, we would always copy the io_abd into b_pabd, but the
6721 * user may have disabled compressed ARC, thus we must check the
6722 * hdr's compression setting rather than the io_bp's.
6724 if (BP_IS_ENCRYPTED(bp
)) {
6725 ASSERT3U(psize
, >, 0);
6726 arc_hdr_alloc_abd(hdr
, ARC_HDR_ALLOC_RDATA
|
6727 ARC_HDR_USE_RESERVE
);
6728 abd_copy(hdr
->b_crypt_hdr
.b_rabd
, zio
->io_abd
, psize
);
6729 } else if (arc_hdr_get_compress(hdr
) != ZIO_COMPRESS_OFF
&&
6730 !ARC_BUF_COMPRESSED(buf
)) {
6731 ASSERT3U(psize
, >, 0);
6732 arc_hdr_alloc_abd(hdr
, ARC_HDR_USE_RESERVE
);
6733 abd_copy(hdr
->b_l1hdr
.b_pabd
, zio
->io_abd
, psize
);
6735 ASSERT3U(zio
->io_orig_size
, ==, arc_hdr_size(hdr
));
6736 arc_hdr_alloc_abd(hdr
, ARC_HDR_USE_RESERVE
);
6737 abd_copy_from_buf(hdr
->b_l1hdr
.b_pabd
, buf
->b_data
,
6741 ASSERT3P(buf
->b_data
, ==, abd_to_buf(zio
->io_orig_abd
));
6742 ASSERT3U(zio
->io_orig_size
, ==, arc_buf_size(buf
));
6743 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, buf
);
6744 ASSERT(ARC_BUF_LAST(buf
));
6746 arc_share_buf(hdr
, buf
);
6750 arc_hdr_verify(hdr
, bp
);
6751 spl_fstrans_unmark(cookie
);
6755 arc_write_children_ready(zio_t
*zio
)
6757 arc_write_callback_t
*callback
= zio
->io_private
;
6758 arc_buf_t
*buf
= callback
->awcb_buf
;
6760 callback
->awcb_children_ready(zio
, buf
, callback
->awcb_private
);
6764 arc_write_done(zio_t
*zio
)
6766 arc_write_callback_t
*callback
= zio
->io_private
;
6767 arc_buf_t
*buf
= callback
->awcb_buf
;
6768 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6770 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6772 if (zio
->io_error
== 0) {
6773 arc_hdr_verify(hdr
, zio
->io_bp
);
6775 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
6776 buf_discard_identity(hdr
);
6778 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
6779 hdr
->b_birth
= BP_GET_BIRTH(zio
->io_bp
);
6782 ASSERT(HDR_EMPTY(hdr
));
6786 * If the block to be written was all-zero or compressed enough to be
6787 * embedded in the BP, no write was performed so there will be no
6788 * dva/birth/checksum. The buffer must therefore remain anonymous
6791 if (!HDR_EMPTY(hdr
)) {
6792 arc_buf_hdr_t
*exists
;
6793 kmutex_t
*hash_lock
;
6795 ASSERT3U(zio
->io_error
, ==, 0);
6797 arc_cksum_verify(buf
);
6799 exists
= buf_hash_insert(hdr
, &hash_lock
);
6800 if (exists
!= NULL
) {
6802 * This can only happen if we overwrite for
6803 * sync-to-convergence, because we remove
6804 * buffers from the hash table when we arc_free().
6806 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
6807 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
6808 panic("bad overwrite, hdr=%p exists=%p",
6809 (void *)hdr
, (void *)exists
);
6810 ASSERT(zfs_refcount_is_zero(
6811 &exists
->b_l1hdr
.b_refcnt
));
6812 arc_change_state(arc_anon
, exists
);
6813 arc_hdr_destroy(exists
);
6814 mutex_exit(hash_lock
);
6815 exists
= buf_hash_insert(hdr
, &hash_lock
);
6816 ASSERT3P(exists
, ==, NULL
);
6817 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
6819 ASSERT(zio
->io_prop
.zp_nopwrite
);
6820 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
6821 panic("bad nopwrite, hdr=%p exists=%p",
6822 (void *)hdr
, (void *)exists
);
6825 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, NULL
);
6826 ASSERT(ARC_BUF_LAST(hdr
->b_l1hdr
.b_buf
));
6827 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
6828 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
6829 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
6832 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6833 VERIFY3S(remove_reference(hdr
, hdr
), >, 0);
6834 /* if it's not anon, we are doing a scrub */
6835 if (exists
== NULL
&& hdr
->b_l1hdr
.b_state
== arc_anon
)
6836 arc_access(hdr
, 0, B_FALSE
);
6837 mutex_exit(hash_lock
);
6839 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
6840 VERIFY3S(remove_reference(hdr
, hdr
), >, 0);
6843 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
6845 abd_free(zio
->io_abd
);
6846 kmem_free(callback
, sizeof (arc_write_callback_t
));
6850 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
6851 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t uncached
, boolean_t l2arc
,
6852 const zio_prop_t
*zp
, arc_write_done_func_t
*ready
,
6853 arc_write_done_func_t
*children_ready
, arc_write_done_func_t
*done
,
6854 void *private, zio_priority_t priority
, int zio_flags
,
6855 const zbookmark_phys_t
*zb
)
6857 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
6858 arc_write_callback_t
*callback
;
6860 zio_prop_t localprop
= *zp
;
6862 ASSERT3P(ready
, !=, NULL
);
6863 ASSERT3P(done
, !=, NULL
);
6864 ASSERT(!HDR_IO_ERROR(hdr
));
6865 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
6866 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
6867 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, NULL
);
6869 arc_hdr_set_flags(hdr
, ARC_FLAG_UNCACHED
);
6871 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
6873 if (ARC_BUF_ENCRYPTED(buf
)) {
6874 ASSERT(ARC_BUF_COMPRESSED(buf
));
6875 localprop
.zp_encrypt
= B_TRUE
;
6876 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
6877 localprop
.zp_complevel
= hdr
->b_complevel
;
6878 localprop
.zp_byteorder
=
6879 (hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
) ?
6880 ZFS_HOST_BYTEORDER
: !ZFS_HOST_BYTEORDER
;
6881 memcpy(localprop
.zp_salt
, hdr
->b_crypt_hdr
.b_salt
,
6883 memcpy(localprop
.zp_iv
, hdr
->b_crypt_hdr
.b_iv
,
6885 memcpy(localprop
.zp_mac
, hdr
->b_crypt_hdr
.b_mac
,
6887 if (DMU_OT_IS_ENCRYPTED(localprop
.zp_type
)) {
6888 localprop
.zp_nopwrite
= B_FALSE
;
6889 localprop
.zp_copies
=
6890 MIN(localprop
.zp_copies
, SPA_DVAS_PER_BP
- 1);
6892 zio_flags
|= ZIO_FLAG_RAW
;
6893 } else if (ARC_BUF_COMPRESSED(buf
)) {
6894 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, arc_buf_size(buf
));
6895 localprop
.zp_compress
= HDR_GET_COMPRESS(hdr
);
6896 localprop
.zp_complevel
= hdr
->b_complevel
;
6897 zio_flags
|= ZIO_FLAG_RAW_COMPRESS
;
6899 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
6900 callback
->awcb_ready
= ready
;
6901 callback
->awcb_children_ready
= children_ready
;
6902 callback
->awcb_done
= done
;
6903 callback
->awcb_private
= private;
6904 callback
->awcb_buf
= buf
;
6907 * The hdr's b_pabd is now stale, free it now. A new data block
6908 * will be allocated when the zio pipeline calls arc_write_ready().
6910 if (hdr
->b_l1hdr
.b_pabd
!= NULL
) {
6912 * If the buf is currently sharing the data block with
6913 * the hdr then we need to break that relationship here.
6914 * The hdr will remain with a NULL data pointer and the
6915 * buf will take sole ownership of the block.
6917 if (ARC_BUF_SHARED(buf
)) {
6918 arc_unshare_buf(hdr
, buf
);
6920 ASSERT(!arc_buf_is_shared(buf
));
6921 arc_hdr_free_abd(hdr
, B_FALSE
);
6923 VERIFY3P(buf
->b_data
, !=, NULL
);
6926 if (HDR_HAS_RABD(hdr
))
6927 arc_hdr_free_abd(hdr
, B_TRUE
);
6929 if (!(zio_flags
& ZIO_FLAG_RAW
))
6930 arc_hdr_set_compress(hdr
, ZIO_COMPRESS_OFF
);
6932 ASSERT(!arc_buf_is_shared(buf
));
6933 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, ==, NULL
);
6935 zio
= zio_write(pio
, spa
, txg
, bp
,
6936 abd_get_from_buf(buf
->b_data
, HDR_GET_LSIZE(hdr
)),
6937 HDR_GET_LSIZE(hdr
), arc_buf_size(buf
), &localprop
, arc_write_ready
,
6938 (children_ready
!= NULL
) ? arc_write_children_ready
: NULL
,
6939 arc_write_done
, callback
, priority
, zio_flags
, zb
);
6945 arc_tempreserve_clear(uint64_t reserve
)
6947 atomic_add_64(&arc_tempreserve
, -reserve
);
6948 ASSERT((int64_t)arc_tempreserve
>= 0);
6952 arc_tempreserve_space(spa_t
*spa
, uint64_t reserve
, uint64_t txg
)
6958 reserve
> arc_c
/4 &&
6959 reserve
* 4 > (2ULL << SPA_MAXBLOCKSHIFT
))
6960 arc_c
= MIN(arc_c_max
, reserve
* 4);
6963 * Throttle when the calculated memory footprint for the TXG
6964 * exceeds the target ARC size.
6966 if (reserve
> arc_c
) {
6967 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
6968 return (SET_ERROR(ERESTART
));
6972 * Don't count loaned bufs as in flight dirty data to prevent long
6973 * network delays from blocking transactions that are ready to be
6974 * assigned to a txg.
6977 /* assert that it has not wrapped around */
6978 ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes
, 0), >=, 0);
6980 anon_size
= MAX((int64_t)
6981 (zfs_refcount_count(&arc_anon
->arcs_size
[ARC_BUFC_DATA
]) +
6982 zfs_refcount_count(&arc_anon
->arcs_size
[ARC_BUFC_METADATA
]) -
6983 arc_loaned_bytes
), 0);
6986 * Writes will, almost always, require additional memory allocations
6987 * in order to compress/encrypt/etc the data. We therefore need to
6988 * make sure that there is sufficient available memory for this.
6990 error
= arc_memory_throttle(spa
, reserve
, txg
);
6995 * Throttle writes when the amount of dirty data in the cache
6996 * gets too large. We try to keep the cache less than half full
6997 * of dirty blocks so that our sync times don't grow too large.
6999 * In the case of one pool being built on another pool, we want
7000 * to make sure we don't end up throttling the lower (backing)
7001 * pool when the upper pool is the majority contributor to dirty
7002 * data. To insure we make forward progress during throttling, we
7003 * also check the current pool's net dirty data and only throttle
7004 * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty
7005 * data in the cache.
7007 * Note: if two requests come in concurrently, we might let them
7008 * both succeed, when one of them should fail. Not a huge deal.
7010 uint64_t total_dirty
= reserve
+ arc_tempreserve
+ anon_size
;
7011 uint64_t spa_dirty_anon
= spa_dirty_data(spa
);
7012 uint64_t rarc_c
= arc_warm
? arc_c
: arc_c_max
;
7013 if (total_dirty
> rarc_c
* zfs_arc_dirty_limit_percent
/ 100 &&
7014 anon_size
> rarc_c
* zfs_arc_anon_limit_percent
/ 100 &&
7015 spa_dirty_anon
> anon_size
* zfs_arc_pool_dirty_percent
/ 100) {
7017 uint64_t meta_esize
= zfs_refcount_count(
7018 &arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7019 uint64_t data_esize
=
7020 zfs_refcount_count(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7021 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
7022 "anon_data=%lluK tempreserve=%lluK rarc_c=%lluK\n",
7023 (u_longlong_t
)arc_tempreserve
>> 10,
7024 (u_longlong_t
)meta_esize
>> 10,
7025 (u_longlong_t
)data_esize
>> 10,
7026 (u_longlong_t
)reserve
>> 10,
7027 (u_longlong_t
)rarc_c
>> 10);
7029 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
7030 return (SET_ERROR(ERESTART
));
7032 atomic_add_64(&arc_tempreserve
, reserve
);
7037 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
7038 kstat_named_t
*data
, kstat_named_t
*metadata
,
7039 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
7042 zfs_refcount_count(&state
->arcs_size
[ARC_BUFC_DATA
]);
7043 metadata
->value
.ui64
=
7044 zfs_refcount_count(&state
->arcs_size
[ARC_BUFC_METADATA
]);
7045 size
->value
.ui64
= data
->value
.ui64
+ metadata
->value
.ui64
;
7046 evict_data
->value
.ui64
=
7047 zfs_refcount_count(&state
->arcs_esize
[ARC_BUFC_DATA
]);
7048 evict_metadata
->value
.ui64
=
7049 zfs_refcount_count(&state
->arcs_esize
[ARC_BUFC_METADATA
]);
7053 arc_kstat_update(kstat_t
*ksp
, int rw
)
7055 arc_stats_t
*as
= ksp
->ks_data
;
7057 if (rw
== KSTAT_WRITE
)
7058 return (SET_ERROR(EACCES
));
7060 as
->arcstat_hits
.value
.ui64
=
7061 wmsum_value(&arc_sums
.arcstat_hits
);
7062 as
->arcstat_iohits
.value
.ui64
=
7063 wmsum_value(&arc_sums
.arcstat_iohits
);
7064 as
->arcstat_misses
.value
.ui64
=
7065 wmsum_value(&arc_sums
.arcstat_misses
);
7066 as
->arcstat_demand_data_hits
.value
.ui64
=
7067 wmsum_value(&arc_sums
.arcstat_demand_data_hits
);
7068 as
->arcstat_demand_data_iohits
.value
.ui64
=
7069 wmsum_value(&arc_sums
.arcstat_demand_data_iohits
);
7070 as
->arcstat_demand_data_misses
.value
.ui64
=
7071 wmsum_value(&arc_sums
.arcstat_demand_data_misses
);
7072 as
->arcstat_demand_metadata_hits
.value
.ui64
=
7073 wmsum_value(&arc_sums
.arcstat_demand_metadata_hits
);
7074 as
->arcstat_demand_metadata_iohits
.value
.ui64
=
7075 wmsum_value(&arc_sums
.arcstat_demand_metadata_iohits
);
7076 as
->arcstat_demand_metadata_misses
.value
.ui64
=
7077 wmsum_value(&arc_sums
.arcstat_demand_metadata_misses
);
7078 as
->arcstat_prefetch_data_hits
.value
.ui64
=
7079 wmsum_value(&arc_sums
.arcstat_prefetch_data_hits
);
7080 as
->arcstat_prefetch_data_iohits
.value
.ui64
=
7081 wmsum_value(&arc_sums
.arcstat_prefetch_data_iohits
);
7082 as
->arcstat_prefetch_data_misses
.value
.ui64
=
7083 wmsum_value(&arc_sums
.arcstat_prefetch_data_misses
);
7084 as
->arcstat_prefetch_metadata_hits
.value
.ui64
=
7085 wmsum_value(&arc_sums
.arcstat_prefetch_metadata_hits
);
7086 as
->arcstat_prefetch_metadata_iohits
.value
.ui64
=
7087 wmsum_value(&arc_sums
.arcstat_prefetch_metadata_iohits
);
7088 as
->arcstat_prefetch_metadata_misses
.value
.ui64
=
7089 wmsum_value(&arc_sums
.arcstat_prefetch_metadata_misses
);
7090 as
->arcstat_mru_hits
.value
.ui64
=
7091 wmsum_value(&arc_sums
.arcstat_mru_hits
);
7092 as
->arcstat_mru_ghost_hits
.value
.ui64
=
7093 wmsum_value(&arc_sums
.arcstat_mru_ghost_hits
);
7094 as
->arcstat_mfu_hits
.value
.ui64
=
7095 wmsum_value(&arc_sums
.arcstat_mfu_hits
);
7096 as
->arcstat_mfu_ghost_hits
.value
.ui64
=
7097 wmsum_value(&arc_sums
.arcstat_mfu_ghost_hits
);
7098 as
->arcstat_uncached_hits
.value
.ui64
=
7099 wmsum_value(&arc_sums
.arcstat_uncached_hits
);
7100 as
->arcstat_deleted
.value
.ui64
=
7101 wmsum_value(&arc_sums
.arcstat_deleted
);
7102 as
->arcstat_mutex_miss
.value
.ui64
=
7103 wmsum_value(&arc_sums
.arcstat_mutex_miss
);
7104 as
->arcstat_access_skip
.value
.ui64
=
7105 wmsum_value(&arc_sums
.arcstat_access_skip
);
7106 as
->arcstat_evict_skip
.value
.ui64
=
7107 wmsum_value(&arc_sums
.arcstat_evict_skip
);
7108 as
->arcstat_evict_not_enough
.value
.ui64
=
7109 wmsum_value(&arc_sums
.arcstat_evict_not_enough
);
7110 as
->arcstat_evict_l2_cached
.value
.ui64
=
7111 wmsum_value(&arc_sums
.arcstat_evict_l2_cached
);
7112 as
->arcstat_evict_l2_eligible
.value
.ui64
=
7113 wmsum_value(&arc_sums
.arcstat_evict_l2_eligible
);
7114 as
->arcstat_evict_l2_eligible_mfu
.value
.ui64
=
7115 wmsum_value(&arc_sums
.arcstat_evict_l2_eligible_mfu
);
7116 as
->arcstat_evict_l2_eligible_mru
.value
.ui64
=
7117 wmsum_value(&arc_sums
.arcstat_evict_l2_eligible_mru
);
7118 as
->arcstat_evict_l2_ineligible
.value
.ui64
=
7119 wmsum_value(&arc_sums
.arcstat_evict_l2_ineligible
);
7120 as
->arcstat_evict_l2_skip
.value
.ui64
=
7121 wmsum_value(&arc_sums
.arcstat_evict_l2_skip
);
7122 as
->arcstat_hash_elements
.value
.ui64
=
7123 as
->arcstat_hash_elements_max
.value
.ui64
=
7124 wmsum_value(&arc_sums
.arcstat_hash_elements
);
7125 as
->arcstat_hash_collisions
.value
.ui64
=
7126 wmsum_value(&arc_sums
.arcstat_hash_collisions
);
7127 as
->arcstat_hash_chains
.value
.ui64
=
7128 wmsum_value(&arc_sums
.arcstat_hash_chains
);
7129 as
->arcstat_size
.value
.ui64
=
7130 aggsum_value(&arc_sums
.arcstat_size
);
7131 as
->arcstat_compressed_size
.value
.ui64
=
7132 wmsum_value(&arc_sums
.arcstat_compressed_size
);
7133 as
->arcstat_uncompressed_size
.value
.ui64
=
7134 wmsum_value(&arc_sums
.arcstat_uncompressed_size
);
7135 as
->arcstat_overhead_size
.value
.ui64
=
7136 wmsum_value(&arc_sums
.arcstat_overhead_size
);
7137 as
->arcstat_hdr_size
.value
.ui64
=
7138 wmsum_value(&arc_sums
.arcstat_hdr_size
);
7139 as
->arcstat_data_size
.value
.ui64
=
7140 wmsum_value(&arc_sums
.arcstat_data_size
);
7141 as
->arcstat_metadata_size
.value
.ui64
=
7142 wmsum_value(&arc_sums
.arcstat_metadata_size
);
7143 as
->arcstat_dbuf_size
.value
.ui64
=
7144 wmsum_value(&arc_sums
.arcstat_dbuf_size
);
7145 #if defined(COMPAT_FREEBSD11)
7146 as
->arcstat_other_size
.value
.ui64
=
7147 wmsum_value(&arc_sums
.arcstat_bonus_size
) +
7148 wmsum_value(&arc_sums
.arcstat_dnode_size
) +
7149 wmsum_value(&arc_sums
.arcstat_dbuf_size
);
7152 arc_kstat_update_state(arc_anon
,
7153 &as
->arcstat_anon_size
,
7154 &as
->arcstat_anon_data
,
7155 &as
->arcstat_anon_metadata
,
7156 &as
->arcstat_anon_evictable_data
,
7157 &as
->arcstat_anon_evictable_metadata
);
7158 arc_kstat_update_state(arc_mru
,
7159 &as
->arcstat_mru_size
,
7160 &as
->arcstat_mru_data
,
7161 &as
->arcstat_mru_metadata
,
7162 &as
->arcstat_mru_evictable_data
,
7163 &as
->arcstat_mru_evictable_metadata
);
7164 arc_kstat_update_state(arc_mru_ghost
,
7165 &as
->arcstat_mru_ghost_size
,
7166 &as
->arcstat_mru_ghost_data
,
7167 &as
->arcstat_mru_ghost_metadata
,
7168 &as
->arcstat_mru_ghost_evictable_data
,
7169 &as
->arcstat_mru_ghost_evictable_metadata
);
7170 arc_kstat_update_state(arc_mfu
,
7171 &as
->arcstat_mfu_size
,
7172 &as
->arcstat_mfu_data
,
7173 &as
->arcstat_mfu_metadata
,
7174 &as
->arcstat_mfu_evictable_data
,
7175 &as
->arcstat_mfu_evictable_metadata
);
7176 arc_kstat_update_state(arc_mfu_ghost
,
7177 &as
->arcstat_mfu_ghost_size
,
7178 &as
->arcstat_mfu_ghost_data
,
7179 &as
->arcstat_mfu_ghost_metadata
,
7180 &as
->arcstat_mfu_ghost_evictable_data
,
7181 &as
->arcstat_mfu_ghost_evictable_metadata
);
7182 arc_kstat_update_state(arc_uncached
,
7183 &as
->arcstat_uncached_size
,
7184 &as
->arcstat_uncached_data
,
7185 &as
->arcstat_uncached_metadata
,
7186 &as
->arcstat_uncached_evictable_data
,
7187 &as
->arcstat_uncached_evictable_metadata
);
7189 as
->arcstat_dnode_size
.value
.ui64
=
7190 wmsum_value(&arc_sums
.arcstat_dnode_size
);
7191 as
->arcstat_bonus_size
.value
.ui64
=
7192 wmsum_value(&arc_sums
.arcstat_bonus_size
);
7193 as
->arcstat_l2_hits
.value
.ui64
=
7194 wmsum_value(&arc_sums
.arcstat_l2_hits
);
7195 as
->arcstat_l2_misses
.value
.ui64
=
7196 wmsum_value(&arc_sums
.arcstat_l2_misses
);
7197 as
->arcstat_l2_prefetch_asize
.value
.ui64
=
7198 wmsum_value(&arc_sums
.arcstat_l2_prefetch_asize
);
7199 as
->arcstat_l2_mru_asize
.value
.ui64
=
7200 wmsum_value(&arc_sums
.arcstat_l2_mru_asize
);
7201 as
->arcstat_l2_mfu_asize
.value
.ui64
=
7202 wmsum_value(&arc_sums
.arcstat_l2_mfu_asize
);
7203 as
->arcstat_l2_bufc_data_asize
.value
.ui64
=
7204 wmsum_value(&arc_sums
.arcstat_l2_bufc_data_asize
);
7205 as
->arcstat_l2_bufc_metadata_asize
.value
.ui64
=
7206 wmsum_value(&arc_sums
.arcstat_l2_bufc_metadata_asize
);
7207 as
->arcstat_l2_feeds
.value
.ui64
=
7208 wmsum_value(&arc_sums
.arcstat_l2_feeds
);
7209 as
->arcstat_l2_rw_clash
.value
.ui64
=
7210 wmsum_value(&arc_sums
.arcstat_l2_rw_clash
);
7211 as
->arcstat_l2_read_bytes
.value
.ui64
=
7212 wmsum_value(&arc_sums
.arcstat_l2_read_bytes
);
7213 as
->arcstat_l2_write_bytes
.value
.ui64
=
7214 wmsum_value(&arc_sums
.arcstat_l2_write_bytes
);
7215 as
->arcstat_l2_writes_sent
.value
.ui64
=
7216 wmsum_value(&arc_sums
.arcstat_l2_writes_sent
);
7217 as
->arcstat_l2_writes_done
.value
.ui64
=
7218 wmsum_value(&arc_sums
.arcstat_l2_writes_done
);
7219 as
->arcstat_l2_writes_error
.value
.ui64
=
7220 wmsum_value(&arc_sums
.arcstat_l2_writes_error
);
7221 as
->arcstat_l2_writes_lock_retry
.value
.ui64
=
7222 wmsum_value(&arc_sums
.arcstat_l2_writes_lock_retry
);
7223 as
->arcstat_l2_evict_lock_retry
.value
.ui64
=
7224 wmsum_value(&arc_sums
.arcstat_l2_evict_lock_retry
);
7225 as
->arcstat_l2_evict_reading
.value
.ui64
=
7226 wmsum_value(&arc_sums
.arcstat_l2_evict_reading
);
7227 as
->arcstat_l2_evict_l1cached
.value
.ui64
=
7228 wmsum_value(&arc_sums
.arcstat_l2_evict_l1cached
);
7229 as
->arcstat_l2_free_on_write
.value
.ui64
=
7230 wmsum_value(&arc_sums
.arcstat_l2_free_on_write
);
7231 as
->arcstat_l2_abort_lowmem
.value
.ui64
=
7232 wmsum_value(&arc_sums
.arcstat_l2_abort_lowmem
);
7233 as
->arcstat_l2_cksum_bad
.value
.ui64
=
7234 wmsum_value(&arc_sums
.arcstat_l2_cksum_bad
);
7235 as
->arcstat_l2_io_error
.value
.ui64
=
7236 wmsum_value(&arc_sums
.arcstat_l2_io_error
);
7237 as
->arcstat_l2_lsize
.value
.ui64
=
7238 wmsum_value(&arc_sums
.arcstat_l2_lsize
);
7239 as
->arcstat_l2_psize
.value
.ui64
=
7240 wmsum_value(&arc_sums
.arcstat_l2_psize
);
7241 as
->arcstat_l2_hdr_size
.value
.ui64
=
7242 aggsum_value(&arc_sums
.arcstat_l2_hdr_size
);
7243 as
->arcstat_l2_log_blk_writes
.value
.ui64
=
7244 wmsum_value(&arc_sums
.arcstat_l2_log_blk_writes
);
7245 as
->arcstat_l2_log_blk_asize
.value
.ui64
=
7246 wmsum_value(&arc_sums
.arcstat_l2_log_blk_asize
);
7247 as
->arcstat_l2_log_blk_count
.value
.ui64
=
7248 wmsum_value(&arc_sums
.arcstat_l2_log_blk_count
);
7249 as
->arcstat_l2_rebuild_success
.value
.ui64
=
7250 wmsum_value(&arc_sums
.arcstat_l2_rebuild_success
);
7251 as
->arcstat_l2_rebuild_abort_unsupported
.value
.ui64
=
7252 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_unsupported
);
7253 as
->arcstat_l2_rebuild_abort_io_errors
.value
.ui64
=
7254 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_io_errors
);
7255 as
->arcstat_l2_rebuild_abort_dh_errors
.value
.ui64
=
7256 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_dh_errors
);
7257 as
->arcstat_l2_rebuild_abort_cksum_lb_errors
.value
.ui64
=
7258 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_cksum_lb_errors
);
7259 as
->arcstat_l2_rebuild_abort_lowmem
.value
.ui64
=
7260 wmsum_value(&arc_sums
.arcstat_l2_rebuild_abort_lowmem
);
7261 as
->arcstat_l2_rebuild_size
.value
.ui64
=
7262 wmsum_value(&arc_sums
.arcstat_l2_rebuild_size
);
7263 as
->arcstat_l2_rebuild_asize
.value
.ui64
=
7264 wmsum_value(&arc_sums
.arcstat_l2_rebuild_asize
);
7265 as
->arcstat_l2_rebuild_bufs
.value
.ui64
=
7266 wmsum_value(&arc_sums
.arcstat_l2_rebuild_bufs
);
7267 as
->arcstat_l2_rebuild_bufs_precached
.value
.ui64
=
7268 wmsum_value(&arc_sums
.arcstat_l2_rebuild_bufs_precached
);
7269 as
->arcstat_l2_rebuild_log_blks
.value
.ui64
=
7270 wmsum_value(&arc_sums
.arcstat_l2_rebuild_log_blks
);
7271 as
->arcstat_memory_throttle_count
.value
.ui64
=
7272 wmsum_value(&arc_sums
.arcstat_memory_throttle_count
);
7273 as
->arcstat_memory_direct_count
.value
.ui64
=
7274 wmsum_value(&arc_sums
.arcstat_memory_direct_count
);
7275 as
->arcstat_memory_indirect_count
.value
.ui64
=
7276 wmsum_value(&arc_sums
.arcstat_memory_indirect_count
);
7278 as
->arcstat_memory_all_bytes
.value
.ui64
=
7280 as
->arcstat_memory_free_bytes
.value
.ui64
=
7282 as
->arcstat_memory_available_bytes
.value
.i64
=
7283 arc_available_memory();
7285 as
->arcstat_prune
.value
.ui64
=
7286 wmsum_value(&arc_sums
.arcstat_prune
);
7287 as
->arcstat_meta_used
.value
.ui64
=
7288 wmsum_value(&arc_sums
.arcstat_meta_used
);
7289 as
->arcstat_async_upgrade_sync
.value
.ui64
=
7290 wmsum_value(&arc_sums
.arcstat_async_upgrade_sync
);
7291 as
->arcstat_predictive_prefetch
.value
.ui64
=
7292 wmsum_value(&arc_sums
.arcstat_predictive_prefetch
);
7293 as
->arcstat_demand_hit_predictive_prefetch
.value
.ui64
=
7294 wmsum_value(&arc_sums
.arcstat_demand_hit_predictive_prefetch
);
7295 as
->arcstat_demand_iohit_predictive_prefetch
.value
.ui64
=
7296 wmsum_value(&arc_sums
.arcstat_demand_iohit_predictive_prefetch
);
7297 as
->arcstat_prescient_prefetch
.value
.ui64
=
7298 wmsum_value(&arc_sums
.arcstat_prescient_prefetch
);
7299 as
->arcstat_demand_hit_prescient_prefetch
.value
.ui64
=
7300 wmsum_value(&arc_sums
.arcstat_demand_hit_prescient_prefetch
);
7301 as
->arcstat_demand_iohit_prescient_prefetch
.value
.ui64
=
7302 wmsum_value(&arc_sums
.arcstat_demand_iohit_prescient_prefetch
);
7303 as
->arcstat_raw_size
.value
.ui64
=
7304 wmsum_value(&arc_sums
.arcstat_raw_size
);
7305 as
->arcstat_cached_only_in_progress
.value
.ui64
=
7306 wmsum_value(&arc_sums
.arcstat_cached_only_in_progress
);
7307 as
->arcstat_abd_chunk_waste_size
.value
.ui64
=
7308 wmsum_value(&arc_sums
.arcstat_abd_chunk_waste_size
);
7314 * This function *must* return indices evenly distributed between all
7315 * sublists of the multilist. This is needed due to how the ARC eviction
7316 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
7317 * distributed between all sublists and uses this assumption when
7318 * deciding which sublist to evict from and how much to evict from it.
7321 arc_state_multilist_index_func(multilist_t
*ml
, void *obj
)
7323 arc_buf_hdr_t
*hdr
= obj
;
7326 * We rely on b_dva to generate evenly distributed index
7327 * numbers using buf_hash below. So, as an added precaution,
7328 * let's make sure we never add empty buffers to the arc lists.
7330 ASSERT(!HDR_EMPTY(hdr
));
7333 * The assumption here, is the hash value for a given
7334 * arc_buf_hdr_t will remain constant throughout its lifetime
7335 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
7336 * Thus, we don't need to store the header's sublist index
7337 * on insertion, as this index can be recalculated on removal.
7339 * Also, the low order bits of the hash value are thought to be
7340 * distributed evenly. Otherwise, in the case that the multilist
7341 * has a power of two number of sublists, each sublists' usage
7342 * would not be evenly distributed. In this context full 64bit
7343 * division would be a waste of time, so limit it to 32 bits.
7345 return ((unsigned int)buf_hash(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
) %
7346 multilist_get_num_sublists(ml
));
7350 arc_state_l2c_multilist_index_func(multilist_t
*ml
, void *obj
)
7352 panic("Header %p insert into arc_l2c_only %p", obj
, ml
);
7355 #define WARN_IF_TUNING_IGNORED(tuning, value, do_warn) do { \
7356 if ((do_warn) && (tuning) && ((tuning) != (value))) { \
7358 "ignoring tunable %s (using %llu instead)", \
7359 (#tuning), (u_longlong_t)(value)); \
7364 * Called during module initialization and periodically thereafter to
7365 * apply reasonable changes to the exposed performance tunings. Can also be
7366 * called explicitly by param_set_arc_*() functions when ARC tunables are
7367 * updated manually. Non-zero zfs_* values which differ from the currently set
7368 * values will be applied.
7371 arc_tuning_update(boolean_t verbose
)
7373 uint64_t allmem
= arc_all_memory();
7375 /* Valid range: 32M - <arc_c_max> */
7376 if ((zfs_arc_min
) && (zfs_arc_min
!= arc_c_min
) &&
7377 (zfs_arc_min
>= 2ULL << SPA_MAXBLOCKSHIFT
) &&
7378 (zfs_arc_min
<= arc_c_max
)) {
7379 arc_c_min
= zfs_arc_min
;
7380 arc_c
= MAX(arc_c
, arc_c_min
);
7382 WARN_IF_TUNING_IGNORED(zfs_arc_min
, arc_c_min
, verbose
);
7384 /* Valid range: 64M - <all physical memory> */
7385 if ((zfs_arc_max
) && (zfs_arc_max
!= arc_c_max
) &&
7386 (zfs_arc_max
>= MIN_ARC_MAX
) && (zfs_arc_max
< allmem
) &&
7387 (zfs_arc_max
> arc_c_min
)) {
7388 arc_c_max
= zfs_arc_max
;
7389 arc_c
= MIN(arc_c
, arc_c_max
);
7390 if (arc_dnode_limit
> arc_c_max
)
7391 arc_dnode_limit
= arc_c_max
;
7393 WARN_IF_TUNING_IGNORED(zfs_arc_max
, arc_c_max
, verbose
);
7395 /* Valid range: 0 - <all physical memory> */
7396 arc_dnode_limit
= zfs_arc_dnode_limit
? zfs_arc_dnode_limit
:
7397 MIN(zfs_arc_dnode_limit_percent
, 100) * arc_c_max
/ 100;
7398 WARN_IF_TUNING_IGNORED(zfs_arc_dnode_limit
, arc_dnode_limit
, verbose
);
7400 /* Valid range: 1 - N */
7401 if (zfs_arc_grow_retry
)
7402 arc_grow_retry
= zfs_arc_grow_retry
;
7404 /* Valid range: 1 - N */
7405 if (zfs_arc_shrink_shift
) {
7406 arc_shrink_shift
= zfs_arc_shrink_shift
;
7407 arc_no_grow_shift
= MIN(arc_no_grow_shift
, arc_shrink_shift
-1);
7410 /* Valid range: 1 - N ms */
7411 if (zfs_arc_min_prefetch_ms
)
7412 arc_min_prefetch_ms
= zfs_arc_min_prefetch_ms
;
7414 /* Valid range: 1 - N ms */
7415 if (zfs_arc_min_prescient_prefetch_ms
) {
7416 arc_min_prescient_prefetch_ms
=
7417 zfs_arc_min_prescient_prefetch_ms
;
7420 /* Valid range: 0 - 100 */
7421 if (zfs_arc_lotsfree_percent
<= 100)
7422 arc_lotsfree_percent
= zfs_arc_lotsfree_percent
;
7423 WARN_IF_TUNING_IGNORED(zfs_arc_lotsfree_percent
, arc_lotsfree_percent
,
7426 /* Valid range: 0 - <all physical memory> */
7427 if ((zfs_arc_sys_free
) && (zfs_arc_sys_free
!= arc_sys_free
))
7428 arc_sys_free
= MIN(zfs_arc_sys_free
, allmem
);
7429 WARN_IF_TUNING_IGNORED(zfs_arc_sys_free
, arc_sys_free
, verbose
);
7433 arc_state_multilist_init(multilist_t
*ml
,
7434 multilist_sublist_index_func_t
*index_func
, int *maxcountp
)
7436 multilist_create(ml
, sizeof (arc_buf_hdr_t
),
7437 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
), index_func
);
7438 *maxcountp
= MAX(*maxcountp
, multilist_get_num_sublists(ml
));
7442 arc_state_init(void)
7444 int num_sublists
= 0;
7446 arc_state_multilist_init(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
7447 arc_state_multilist_index_func
, &num_sublists
);
7448 arc_state_multilist_init(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
7449 arc_state_multilist_index_func
, &num_sublists
);
7450 arc_state_multilist_init(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
7451 arc_state_multilist_index_func
, &num_sublists
);
7452 arc_state_multilist_init(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
7453 arc_state_multilist_index_func
, &num_sublists
);
7454 arc_state_multilist_init(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
7455 arc_state_multilist_index_func
, &num_sublists
);
7456 arc_state_multilist_init(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
7457 arc_state_multilist_index_func
, &num_sublists
);
7458 arc_state_multilist_init(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
7459 arc_state_multilist_index_func
, &num_sublists
);
7460 arc_state_multilist_init(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
7461 arc_state_multilist_index_func
, &num_sublists
);
7462 arc_state_multilist_init(&arc_uncached
->arcs_list
[ARC_BUFC_METADATA
],
7463 arc_state_multilist_index_func
, &num_sublists
);
7464 arc_state_multilist_init(&arc_uncached
->arcs_list
[ARC_BUFC_DATA
],
7465 arc_state_multilist_index_func
, &num_sublists
);
7468 * L2 headers should never be on the L2 state list since they don't
7469 * have L1 headers allocated. Special index function asserts that.
7471 arc_state_multilist_init(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
7472 arc_state_l2c_multilist_index_func
, &num_sublists
);
7473 arc_state_multilist_init(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
7474 arc_state_l2c_multilist_index_func
, &num_sublists
);
7477 * Keep track of the number of markers needed to reclaim buffers from
7478 * any ARC state. The markers will be pre-allocated so as to minimize
7479 * the number of memory allocations performed by the eviction thread.
7481 arc_state_evict_marker_count
= num_sublists
;
7483 zfs_refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7484 zfs_refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7485 zfs_refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7486 zfs_refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7487 zfs_refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7488 zfs_refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7489 zfs_refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7490 zfs_refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7491 zfs_refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7492 zfs_refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7493 zfs_refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7494 zfs_refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7495 zfs_refcount_create(&arc_uncached
->arcs_esize
[ARC_BUFC_METADATA
]);
7496 zfs_refcount_create(&arc_uncached
->arcs_esize
[ARC_BUFC_DATA
]);
7498 zfs_refcount_create(&arc_anon
->arcs_size
[ARC_BUFC_DATA
]);
7499 zfs_refcount_create(&arc_anon
->arcs_size
[ARC_BUFC_METADATA
]);
7500 zfs_refcount_create(&arc_mru
->arcs_size
[ARC_BUFC_DATA
]);
7501 zfs_refcount_create(&arc_mru
->arcs_size
[ARC_BUFC_METADATA
]);
7502 zfs_refcount_create(&arc_mru_ghost
->arcs_size
[ARC_BUFC_DATA
]);
7503 zfs_refcount_create(&arc_mru_ghost
->arcs_size
[ARC_BUFC_METADATA
]);
7504 zfs_refcount_create(&arc_mfu
->arcs_size
[ARC_BUFC_DATA
]);
7505 zfs_refcount_create(&arc_mfu
->arcs_size
[ARC_BUFC_METADATA
]);
7506 zfs_refcount_create(&arc_mfu_ghost
->arcs_size
[ARC_BUFC_DATA
]);
7507 zfs_refcount_create(&arc_mfu_ghost
->arcs_size
[ARC_BUFC_METADATA
]);
7508 zfs_refcount_create(&arc_l2c_only
->arcs_size
[ARC_BUFC_DATA
]);
7509 zfs_refcount_create(&arc_l2c_only
->arcs_size
[ARC_BUFC_METADATA
]);
7510 zfs_refcount_create(&arc_uncached
->arcs_size
[ARC_BUFC_DATA
]);
7511 zfs_refcount_create(&arc_uncached
->arcs_size
[ARC_BUFC_METADATA
]);
7513 wmsum_init(&arc_mru_ghost
->arcs_hits
[ARC_BUFC_DATA
], 0);
7514 wmsum_init(&arc_mru_ghost
->arcs_hits
[ARC_BUFC_METADATA
], 0);
7515 wmsum_init(&arc_mfu_ghost
->arcs_hits
[ARC_BUFC_DATA
], 0);
7516 wmsum_init(&arc_mfu_ghost
->arcs_hits
[ARC_BUFC_METADATA
], 0);
7518 wmsum_init(&arc_sums
.arcstat_hits
, 0);
7519 wmsum_init(&arc_sums
.arcstat_iohits
, 0);
7520 wmsum_init(&arc_sums
.arcstat_misses
, 0);
7521 wmsum_init(&arc_sums
.arcstat_demand_data_hits
, 0);
7522 wmsum_init(&arc_sums
.arcstat_demand_data_iohits
, 0);
7523 wmsum_init(&arc_sums
.arcstat_demand_data_misses
, 0);
7524 wmsum_init(&arc_sums
.arcstat_demand_metadata_hits
, 0);
7525 wmsum_init(&arc_sums
.arcstat_demand_metadata_iohits
, 0);
7526 wmsum_init(&arc_sums
.arcstat_demand_metadata_misses
, 0);
7527 wmsum_init(&arc_sums
.arcstat_prefetch_data_hits
, 0);
7528 wmsum_init(&arc_sums
.arcstat_prefetch_data_iohits
, 0);
7529 wmsum_init(&arc_sums
.arcstat_prefetch_data_misses
, 0);
7530 wmsum_init(&arc_sums
.arcstat_prefetch_metadata_hits
, 0);
7531 wmsum_init(&arc_sums
.arcstat_prefetch_metadata_iohits
, 0);
7532 wmsum_init(&arc_sums
.arcstat_prefetch_metadata_misses
, 0);
7533 wmsum_init(&arc_sums
.arcstat_mru_hits
, 0);
7534 wmsum_init(&arc_sums
.arcstat_mru_ghost_hits
, 0);
7535 wmsum_init(&arc_sums
.arcstat_mfu_hits
, 0);
7536 wmsum_init(&arc_sums
.arcstat_mfu_ghost_hits
, 0);
7537 wmsum_init(&arc_sums
.arcstat_uncached_hits
, 0);
7538 wmsum_init(&arc_sums
.arcstat_deleted
, 0);
7539 wmsum_init(&arc_sums
.arcstat_mutex_miss
, 0);
7540 wmsum_init(&arc_sums
.arcstat_access_skip
, 0);
7541 wmsum_init(&arc_sums
.arcstat_evict_skip
, 0);
7542 wmsum_init(&arc_sums
.arcstat_evict_not_enough
, 0);
7543 wmsum_init(&arc_sums
.arcstat_evict_l2_cached
, 0);
7544 wmsum_init(&arc_sums
.arcstat_evict_l2_eligible
, 0);
7545 wmsum_init(&arc_sums
.arcstat_evict_l2_eligible_mfu
, 0);
7546 wmsum_init(&arc_sums
.arcstat_evict_l2_eligible_mru
, 0);
7547 wmsum_init(&arc_sums
.arcstat_evict_l2_ineligible
, 0);
7548 wmsum_init(&arc_sums
.arcstat_evict_l2_skip
, 0);
7549 wmsum_init(&arc_sums
.arcstat_hash_elements
, 0);
7550 wmsum_init(&arc_sums
.arcstat_hash_collisions
, 0);
7551 wmsum_init(&arc_sums
.arcstat_hash_chains
, 0);
7552 aggsum_init(&arc_sums
.arcstat_size
, 0);
7553 wmsum_init(&arc_sums
.arcstat_compressed_size
, 0);
7554 wmsum_init(&arc_sums
.arcstat_uncompressed_size
, 0);
7555 wmsum_init(&arc_sums
.arcstat_overhead_size
, 0);
7556 wmsum_init(&arc_sums
.arcstat_hdr_size
, 0);
7557 wmsum_init(&arc_sums
.arcstat_data_size
, 0);
7558 wmsum_init(&arc_sums
.arcstat_metadata_size
, 0);
7559 wmsum_init(&arc_sums
.arcstat_dbuf_size
, 0);
7560 wmsum_init(&arc_sums
.arcstat_dnode_size
, 0);
7561 wmsum_init(&arc_sums
.arcstat_bonus_size
, 0);
7562 wmsum_init(&arc_sums
.arcstat_l2_hits
, 0);
7563 wmsum_init(&arc_sums
.arcstat_l2_misses
, 0);
7564 wmsum_init(&arc_sums
.arcstat_l2_prefetch_asize
, 0);
7565 wmsum_init(&arc_sums
.arcstat_l2_mru_asize
, 0);
7566 wmsum_init(&arc_sums
.arcstat_l2_mfu_asize
, 0);
7567 wmsum_init(&arc_sums
.arcstat_l2_bufc_data_asize
, 0);
7568 wmsum_init(&arc_sums
.arcstat_l2_bufc_metadata_asize
, 0);
7569 wmsum_init(&arc_sums
.arcstat_l2_feeds
, 0);
7570 wmsum_init(&arc_sums
.arcstat_l2_rw_clash
, 0);
7571 wmsum_init(&arc_sums
.arcstat_l2_read_bytes
, 0);
7572 wmsum_init(&arc_sums
.arcstat_l2_write_bytes
, 0);
7573 wmsum_init(&arc_sums
.arcstat_l2_writes_sent
, 0);
7574 wmsum_init(&arc_sums
.arcstat_l2_writes_done
, 0);
7575 wmsum_init(&arc_sums
.arcstat_l2_writes_error
, 0);
7576 wmsum_init(&arc_sums
.arcstat_l2_writes_lock_retry
, 0);
7577 wmsum_init(&arc_sums
.arcstat_l2_evict_lock_retry
, 0);
7578 wmsum_init(&arc_sums
.arcstat_l2_evict_reading
, 0);
7579 wmsum_init(&arc_sums
.arcstat_l2_evict_l1cached
, 0);
7580 wmsum_init(&arc_sums
.arcstat_l2_free_on_write
, 0);
7581 wmsum_init(&arc_sums
.arcstat_l2_abort_lowmem
, 0);
7582 wmsum_init(&arc_sums
.arcstat_l2_cksum_bad
, 0);
7583 wmsum_init(&arc_sums
.arcstat_l2_io_error
, 0);
7584 wmsum_init(&arc_sums
.arcstat_l2_lsize
, 0);
7585 wmsum_init(&arc_sums
.arcstat_l2_psize
, 0);
7586 aggsum_init(&arc_sums
.arcstat_l2_hdr_size
, 0);
7587 wmsum_init(&arc_sums
.arcstat_l2_log_blk_writes
, 0);
7588 wmsum_init(&arc_sums
.arcstat_l2_log_blk_asize
, 0);
7589 wmsum_init(&arc_sums
.arcstat_l2_log_blk_count
, 0);
7590 wmsum_init(&arc_sums
.arcstat_l2_rebuild_success
, 0);
7591 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_unsupported
, 0);
7592 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_io_errors
, 0);
7593 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_dh_errors
, 0);
7594 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_cksum_lb_errors
, 0);
7595 wmsum_init(&arc_sums
.arcstat_l2_rebuild_abort_lowmem
, 0);
7596 wmsum_init(&arc_sums
.arcstat_l2_rebuild_size
, 0);
7597 wmsum_init(&arc_sums
.arcstat_l2_rebuild_asize
, 0);
7598 wmsum_init(&arc_sums
.arcstat_l2_rebuild_bufs
, 0);
7599 wmsum_init(&arc_sums
.arcstat_l2_rebuild_bufs_precached
, 0);
7600 wmsum_init(&arc_sums
.arcstat_l2_rebuild_log_blks
, 0);
7601 wmsum_init(&arc_sums
.arcstat_memory_throttle_count
, 0);
7602 wmsum_init(&arc_sums
.arcstat_memory_direct_count
, 0);
7603 wmsum_init(&arc_sums
.arcstat_memory_indirect_count
, 0);
7604 wmsum_init(&arc_sums
.arcstat_prune
, 0);
7605 wmsum_init(&arc_sums
.arcstat_meta_used
, 0);
7606 wmsum_init(&arc_sums
.arcstat_async_upgrade_sync
, 0);
7607 wmsum_init(&arc_sums
.arcstat_predictive_prefetch
, 0);
7608 wmsum_init(&arc_sums
.arcstat_demand_hit_predictive_prefetch
, 0);
7609 wmsum_init(&arc_sums
.arcstat_demand_iohit_predictive_prefetch
, 0);
7610 wmsum_init(&arc_sums
.arcstat_prescient_prefetch
, 0);
7611 wmsum_init(&arc_sums
.arcstat_demand_hit_prescient_prefetch
, 0);
7612 wmsum_init(&arc_sums
.arcstat_demand_iohit_prescient_prefetch
, 0);
7613 wmsum_init(&arc_sums
.arcstat_raw_size
, 0);
7614 wmsum_init(&arc_sums
.arcstat_cached_only_in_progress
, 0);
7615 wmsum_init(&arc_sums
.arcstat_abd_chunk_waste_size
, 0);
7617 arc_anon
->arcs_state
= ARC_STATE_ANON
;
7618 arc_mru
->arcs_state
= ARC_STATE_MRU
;
7619 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
7620 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
7621 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
7622 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
7623 arc_uncached
->arcs_state
= ARC_STATE_UNCACHED
;
7627 arc_state_fini(void)
7629 zfs_refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
7630 zfs_refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
7631 zfs_refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
7632 zfs_refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
7633 zfs_refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7634 zfs_refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7635 zfs_refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
7636 zfs_refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
7637 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
7638 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
7639 zfs_refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
7640 zfs_refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
7641 zfs_refcount_destroy(&arc_uncached
->arcs_esize
[ARC_BUFC_METADATA
]);
7642 zfs_refcount_destroy(&arc_uncached
->arcs_esize
[ARC_BUFC_DATA
]);
7644 zfs_refcount_destroy(&arc_anon
->arcs_size
[ARC_BUFC_DATA
]);
7645 zfs_refcount_destroy(&arc_anon
->arcs_size
[ARC_BUFC_METADATA
]);
7646 zfs_refcount_destroy(&arc_mru
->arcs_size
[ARC_BUFC_DATA
]);
7647 zfs_refcount_destroy(&arc_mru
->arcs_size
[ARC_BUFC_METADATA
]);
7648 zfs_refcount_destroy(&arc_mru_ghost
->arcs_size
[ARC_BUFC_DATA
]);
7649 zfs_refcount_destroy(&arc_mru_ghost
->arcs_size
[ARC_BUFC_METADATA
]);
7650 zfs_refcount_destroy(&arc_mfu
->arcs_size
[ARC_BUFC_DATA
]);
7651 zfs_refcount_destroy(&arc_mfu
->arcs_size
[ARC_BUFC_METADATA
]);
7652 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_size
[ARC_BUFC_DATA
]);
7653 zfs_refcount_destroy(&arc_mfu_ghost
->arcs_size
[ARC_BUFC_METADATA
]);
7654 zfs_refcount_destroy(&arc_l2c_only
->arcs_size
[ARC_BUFC_DATA
]);
7655 zfs_refcount_destroy(&arc_l2c_only
->arcs_size
[ARC_BUFC_METADATA
]);
7656 zfs_refcount_destroy(&arc_uncached
->arcs_size
[ARC_BUFC_DATA
]);
7657 zfs_refcount_destroy(&arc_uncached
->arcs_size
[ARC_BUFC_METADATA
]);
7659 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
7660 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7661 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
7662 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
7663 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
7664 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7665 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
7666 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
7667 multilist_destroy(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]);
7668 multilist_destroy(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]);
7669 multilist_destroy(&arc_uncached
->arcs_list
[ARC_BUFC_METADATA
]);
7670 multilist_destroy(&arc_uncached
->arcs_list
[ARC_BUFC_DATA
]);
7672 wmsum_fini(&arc_mru_ghost
->arcs_hits
[ARC_BUFC_DATA
]);
7673 wmsum_fini(&arc_mru_ghost
->arcs_hits
[ARC_BUFC_METADATA
]);
7674 wmsum_fini(&arc_mfu_ghost
->arcs_hits
[ARC_BUFC_DATA
]);
7675 wmsum_fini(&arc_mfu_ghost
->arcs_hits
[ARC_BUFC_METADATA
]);
7677 wmsum_fini(&arc_sums
.arcstat_hits
);
7678 wmsum_fini(&arc_sums
.arcstat_iohits
);
7679 wmsum_fini(&arc_sums
.arcstat_misses
);
7680 wmsum_fini(&arc_sums
.arcstat_demand_data_hits
);
7681 wmsum_fini(&arc_sums
.arcstat_demand_data_iohits
);
7682 wmsum_fini(&arc_sums
.arcstat_demand_data_misses
);
7683 wmsum_fini(&arc_sums
.arcstat_demand_metadata_hits
);
7684 wmsum_fini(&arc_sums
.arcstat_demand_metadata_iohits
);
7685 wmsum_fini(&arc_sums
.arcstat_demand_metadata_misses
);
7686 wmsum_fini(&arc_sums
.arcstat_prefetch_data_hits
);
7687 wmsum_fini(&arc_sums
.arcstat_prefetch_data_iohits
);
7688 wmsum_fini(&arc_sums
.arcstat_prefetch_data_misses
);
7689 wmsum_fini(&arc_sums
.arcstat_prefetch_metadata_hits
);
7690 wmsum_fini(&arc_sums
.arcstat_prefetch_metadata_iohits
);
7691 wmsum_fini(&arc_sums
.arcstat_prefetch_metadata_misses
);
7692 wmsum_fini(&arc_sums
.arcstat_mru_hits
);
7693 wmsum_fini(&arc_sums
.arcstat_mru_ghost_hits
);
7694 wmsum_fini(&arc_sums
.arcstat_mfu_hits
);
7695 wmsum_fini(&arc_sums
.arcstat_mfu_ghost_hits
);
7696 wmsum_fini(&arc_sums
.arcstat_uncached_hits
);
7697 wmsum_fini(&arc_sums
.arcstat_deleted
);
7698 wmsum_fini(&arc_sums
.arcstat_mutex_miss
);
7699 wmsum_fini(&arc_sums
.arcstat_access_skip
);
7700 wmsum_fini(&arc_sums
.arcstat_evict_skip
);
7701 wmsum_fini(&arc_sums
.arcstat_evict_not_enough
);
7702 wmsum_fini(&arc_sums
.arcstat_evict_l2_cached
);
7703 wmsum_fini(&arc_sums
.arcstat_evict_l2_eligible
);
7704 wmsum_fini(&arc_sums
.arcstat_evict_l2_eligible_mfu
);
7705 wmsum_fini(&arc_sums
.arcstat_evict_l2_eligible_mru
);
7706 wmsum_fini(&arc_sums
.arcstat_evict_l2_ineligible
);
7707 wmsum_fini(&arc_sums
.arcstat_evict_l2_skip
);
7708 wmsum_fini(&arc_sums
.arcstat_hash_elements
);
7709 wmsum_fini(&arc_sums
.arcstat_hash_collisions
);
7710 wmsum_fini(&arc_sums
.arcstat_hash_chains
);
7711 aggsum_fini(&arc_sums
.arcstat_size
);
7712 wmsum_fini(&arc_sums
.arcstat_compressed_size
);
7713 wmsum_fini(&arc_sums
.arcstat_uncompressed_size
);
7714 wmsum_fini(&arc_sums
.arcstat_overhead_size
);
7715 wmsum_fini(&arc_sums
.arcstat_hdr_size
);
7716 wmsum_fini(&arc_sums
.arcstat_data_size
);
7717 wmsum_fini(&arc_sums
.arcstat_metadata_size
);
7718 wmsum_fini(&arc_sums
.arcstat_dbuf_size
);
7719 wmsum_fini(&arc_sums
.arcstat_dnode_size
);
7720 wmsum_fini(&arc_sums
.arcstat_bonus_size
);
7721 wmsum_fini(&arc_sums
.arcstat_l2_hits
);
7722 wmsum_fini(&arc_sums
.arcstat_l2_misses
);
7723 wmsum_fini(&arc_sums
.arcstat_l2_prefetch_asize
);
7724 wmsum_fini(&arc_sums
.arcstat_l2_mru_asize
);
7725 wmsum_fini(&arc_sums
.arcstat_l2_mfu_asize
);
7726 wmsum_fini(&arc_sums
.arcstat_l2_bufc_data_asize
);
7727 wmsum_fini(&arc_sums
.arcstat_l2_bufc_metadata_asize
);
7728 wmsum_fini(&arc_sums
.arcstat_l2_feeds
);
7729 wmsum_fini(&arc_sums
.arcstat_l2_rw_clash
);
7730 wmsum_fini(&arc_sums
.arcstat_l2_read_bytes
);
7731 wmsum_fini(&arc_sums
.arcstat_l2_write_bytes
);
7732 wmsum_fini(&arc_sums
.arcstat_l2_writes_sent
);
7733 wmsum_fini(&arc_sums
.arcstat_l2_writes_done
);
7734 wmsum_fini(&arc_sums
.arcstat_l2_writes_error
);
7735 wmsum_fini(&arc_sums
.arcstat_l2_writes_lock_retry
);
7736 wmsum_fini(&arc_sums
.arcstat_l2_evict_lock_retry
);
7737 wmsum_fini(&arc_sums
.arcstat_l2_evict_reading
);
7738 wmsum_fini(&arc_sums
.arcstat_l2_evict_l1cached
);
7739 wmsum_fini(&arc_sums
.arcstat_l2_free_on_write
);
7740 wmsum_fini(&arc_sums
.arcstat_l2_abort_lowmem
);
7741 wmsum_fini(&arc_sums
.arcstat_l2_cksum_bad
);
7742 wmsum_fini(&arc_sums
.arcstat_l2_io_error
);
7743 wmsum_fini(&arc_sums
.arcstat_l2_lsize
);
7744 wmsum_fini(&arc_sums
.arcstat_l2_psize
);
7745 aggsum_fini(&arc_sums
.arcstat_l2_hdr_size
);
7746 wmsum_fini(&arc_sums
.arcstat_l2_log_blk_writes
);
7747 wmsum_fini(&arc_sums
.arcstat_l2_log_blk_asize
);
7748 wmsum_fini(&arc_sums
.arcstat_l2_log_blk_count
);
7749 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_success
);
7750 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_unsupported
);
7751 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_io_errors
);
7752 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_dh_errors
);
7753 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_cksum_lb_errors
);
7754 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_abort_lowmem
);
7755 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_size
);
7756 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_asize
);
7757 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_bufs
);
7758 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_bufs_precached
);
7759 wmsum_fini(&arc_sums
.arcstat_l2_rebuild_log_blks
);
7760 wmsum_fini(&arc_sums
.arcstat_memory_throttle_count
);
7761 wmsum_fini(&arc_sums
.arcstat_memory_direct_count
);
7762 wmsum_fini(&arc_sums
.arcstat_memory_indirect_count
);
7763 wmsum_fini(&arc_sums
.arcstat_prune
);
7764 wmsum_fini(&arc_sums
.arcstat_meta_used
);
7765 wmsum_fini(&arc_sums
.arcstat_async_upgrade_sync
);
7766 wmsum_fini(&arc_sums
.arcstat_predictive_prefetch
);
7767 wmsum_fini(&arc_sums
.arcstat_demand_hit_predictive_prefetch
);
7768 wmsum_fini(&arc_sums
.arcstat_demand_iohit_predictive_prefetch
);
7769 wmsum_fini(&arc_sums
.arcstat_prescient_prefetch
);
7770 wmsum_fini(&arc_sums
.arcstat_demand_hit_prescient_prefetch
);
7771 wmsum_fini(&arc_sums
.arcstat_demand_iohit_prescient_prefetch
);
7772 wmsum_fini(&arc_sums
.arcstat_raw_size
);
7773 wmsum_fini(&arc_sums
.arcstat_cached_only_in_progress
);
7774 wmsum_fini(&arc_sums
.arcstat_abd_chunk_waste_size
);
7778 arc_target_bytes(void)
7784 arc_set_limits(uint64_t allmem
)
7786 /* Set min cache to 1/32 of all memory, or 32MB, whichever is more. */
7787 arc_c_min
= MAX(allmem
/ 32, 2ULL << SPA_MAXBLOCKSHIFT
);
7789 /* How to set default max varies by platform. */
7790 arc_c_max
= arc_default_max(arc_c_min
, allmem
);
7795 uint64_t percent
, allmem
= arc_all_memory();
7796 mutex_init(&arc_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7797 list_create(&arc_evict_waiters
, sizeof (arc_evict_waiter_t
),
7798 offsetof(arc_evict_waiter_t
, aew_node
));
7800 arc_min_prefetch_ms
= 1000;
7801 arc_min_prescient_prefetch_ms
= 6000;
7803 #if defined(_KERNEL)
7807 arc_set_limits(allmem
);
7811 * If zfs_arc_max is non-zero at init, meaning it was set in the kernel
7812 * environment before the module was loaded, don't block setting the
7813 * maximum because it is less than arc_c_min, instead, reset arc_c_min
7815 * zfs_arc_min will be handled by arc_tuning_update().
7817 if (zfs_arc_max
!= 0 && zfs_arc_max
>= MIN_ARC_MAX
&&
7818 zfs_arc_max
< allmem
) {
7819 arc_c_max
= zfs_arc_max
;
7820 if (arc_c_min
>= arc_c_max
) {
7821 arc_c_min
= MAX(zfs_arc_max
/ 2,
7822 2ULL << SPA_MAXBLOCKSHIFT
);
7827 * In userland, there's only the memory pressure that we artificially
7828 * create (see arc_available_memory()). Don't let arc_c get too
7829 * small, because it can cause transactions to be larger than
7830 * arc_c, causing arc_tempreserve_space() to fail.
7832 arc_c_min
= MAX(arc_c_max
/ 2, 2ULL << SPA_MAXBLOCKSHIFT
);
7837 * 32-bit fixed point fractions of metadata from total ARC size,
7838 * MRU data from all data and MRU metadata from all metadata.
7840 arc_meta
= (1ULL << 32) / 4; /* Metadata is 25% of arc_c. */
7841 arc_pd
= (1ULL << 32) / 2; /* Data MRU is 50% of data. */
7842 arc_pm
= (1ULL << 32) / 2; /* Metadata MRU is 50% of metadata. */
7844 percent
= MIN(zfs_arc_dnode_limit_percent
, 100);
7845 arc_dnode_limit
= arc_c_max
* percent
/ 100;
7847 /* Apply user specified tunings */
7848 arc_tuning_update(B_TRUE
);
7850 /* if kmem_flags are set, lets try to use less memory */
7851 if (kmem_debugging())
7853 if (arc_c
< arc_c_min
)
7856 arc_register_hotplug();
7862 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
7863 offsetof(arc_prune_t
, p_node
));
7864 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
7866 arc_prune_taskq
= taskq_create("arc_prune", zfs_arc_prune_task_threads
,
7867 defclsyspri
, 100, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
7869 list_create(&arc_async_flush_list
, sizeof (arc_async_flush_t
),
7870 offsetof(arc_async_flush_t
, af_node
));
7871 mutex_init(&arc_async_flush_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7872 arc_flush_taskq
= taskq_create("arc_flush", MIN(boot_ncpus
, 4),
7873 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
7875 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
7876 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
7878 if (arc_ksp
!= NULL
) {
7879 arc_ksp
->ks_data
= &arc_stats
;
7880 arc_ksp
->ks_update
= arc_kstat_update
;
7881 kstat_install(arc_ksp
);
7884 arc_state_evict_markers
=
7885 arc_state_alloc_markers(arc_state_evict_marker_count
);
7886 arc_evict_zthr
= zthr_create_timer("arc_evict",
7887 arc_evict_cb_check
, arc_evict_cb
, NULL
, SEC2NSEC(1), defclsyspri
);
7888 arc_reap_zthr
= zthr_create_timer("arc_reap",
7889 arc_reap_cb_check
, arc_reap_cb
, NULL
, SEC2NSEC(1), minclsyspri
);
7894 * Calculate maximum amount of dirty data per pool.
7896 * If it has been set by a module parameter, take that.
7897 * Otherwise, use a percentage of physical memory defined by
7898 * zfs_dirty_data_max_percent (default 10%) with a cap at
7899 * zfs_dirty_data_max_max (default 4G or 25% of physical memory).
7902 if (zfs_dirty_data_max_max
== 0)
7903 zfs_dirty_data_max_max
= MIN(4ULL * 1024 * 1024 * 1024,
7904 allmem
* zfs_dirty_data_max_max_percent
/ 100);
7906 if (zfs_dirty_data_max_max
== 0)
7907 zfs_dirty_data_max_max
= MIN(1ULL * 1024 * 1024 * 1024,
7908 allmem
* zfs_dirty_data_max_max_percent
/ 100);
7911 if (zfs_dirty_data_max
== 0) {
7912 zfs_dirty_data_max
= allmem
*
7913 zfs_dirty_data_max_percent
/ 100;
7914 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
7915 zfs_dirty_data_max_max
);
7918 if (zfs_wrlog_data_max
== 0) {
7921 * dp_wrlog_total is reduced for each txg at the end of
7922 * spa_sync(). However, dp_dirty_total is reduced every time
7923 * a block is written out. Thus under normal operation,
7924 * dp_wrlog_total could grow 2 times as big as
7925 * zfs_dirty_data_max.
7927 zfs_wrlog_data_max
= zfs_dirty_data_max
* 2;
7938 #endif /* _KERNEL */
7940 /* Wait for any background flushes */
7941 taskq_wait(arc_flush_taskq
);
7942 taskq_destroy(arc_flush_taskq
);
7944 /* Use B_TRUE to ensure *all* buffers are evicted */
7945 arc_flush(NULL
, B_TRUE
);
7947 if (arc_ksp
!= NULL
) {
7948 kstat_delete(arc_ksp
);
7952 taskq_wait(arc_prune_taskq
);
7953 taskq_destroy(arc_prune_taskq
);
7955 list_destroy(&arc_async_flush_list
);
7956 mutex_destroy(&arc_async_flush_lock
);
7958 mutex_enter(&arc_prune_mtx
);
7959 while ((p
= list_remove_head(&arc_prune_list
)) != NULL
) {
7960 (void) zfs_refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
7961 zfs_refcount_destroy(&p
->p_refcnt
);
7962 kmem_free(p
, sizeof (*p
));
7964 mutex_exit(&arc_prune_mtx
);
7966 list_destroy(&arc_prune_list
);
7967 mutex_destroy(&arc_prune_mtx
);
7969 (void) zthr_cancel(arc_evict_zthr
);
7970 (void) zthr_cancel(arc_reap_zthr
);
7971 arc_state_free_markers(arc_state_evict_markers
,
7972 arc_state_evict_marker_count
);
7974 mutex_destroy(&arc_evict_lock
);
7975 list_destroy(&arc_evict_waiters
);
7978 * Free any buffers that were tagged for destruction. This needs
7979 * to occur before arc_state_fini() runs and destroys the aggsum
7980 * values which are updated when freeing scatter ABDs.
7982 l2arc_do_free_on_write();
7985 * buf_fini() must proceed arc_state_fini() because buf_fin() may
7986 * trigger the release of kmem magazines, which can callback to
7987 * arc_space_return() which accesses aggsums freed in act_state_fini().
7992 arc_unregister_hotplug();
7995 * We destroy the zthrs after all the ARC state has been
7996 * torn down to avoid the case of them receiving any
7997 * wakeup() signals after they are destroyed.
7999 zthr_destroy(arc_evict_zthr
);
8000 zthr_destroy(arc_reap_zthr
);
8002 ASSERT0(arc_loaned_bytes
);
8008 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
8009 * It uses dedicated storage devices to hold cached data, which are populated
8010 * using large infrequent writes. The main role of this cache is to boost
8011 * the performance of random read workloads. The intended L2ARC devices
8012 * include short-stroked disks, solid state disks, and other media with
8013 * substantially faster read latency than disk.
8015 * +-----------------------+
8017 * +-----------------------+
8020 * l2arc_feed_thread() arc_read()
8024 * +---------------+ |
8026 * +---------------+ |
8031 * +-------+ +-------+
8033 * | cache | | cache |
8034 * +-------+ +-------+
8035 * +=========+ .-----.
8036 * : L2ARC : |-_____-|
8037 * : devices : | Disks |
8038 * +=========+ `-_____-'
8040 * Read requests are satisfied from the following sources, in order:
8043 * 2) vdev cache of L2ARC devices
8045 * 4) vdev cache of disks
8048 * Some L2ARC device types exhibit extremely slow write performance.
8049 * To accommodate for this there are some significant differences between
8050 * the L2ARC and traditional cache design:
8052 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
8053 * the ARC behave as usual, freeing buffers and placing headers on ghost
8054 * lists. The ARC does not send buffers to the L2ARC during eviction as
8055 * this would add inflated write latencies for all ARC memory pressure.
8057 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
8058 * It does this by periodically scanning buffers from the eviction-end of
8059 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
8060 * not already there. It scans until a headroom of buffers is satisfied,
8061 * which itself is a buffer for ARC eviction. If a compressible buffer is
8062 * found during scanning and selected for writing to an L2ARC device, we
8063 * temporarily boost scanning headroom during the next scan cycle to make
8064 * sure we adapt to compression effects (which might significantly reduce
8065 * the data volume we write to L2ARC). The thread that does this is
8066 * l2arc_feed_thread(), illustrated below; example sizes are included to
8067 * provide a better sense of ratio than this diagram:
8070 * +---------------------+----------+
8071 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
8072 * +---------------------+----------+ | o L2ARC eligible
8073 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
8074 * +---------------------+----------+ |
8075 * 15.9 Gbytes ^ 32 Mbytes |
8077 * l2arc_feed_thread()
8079 * l2arc write hand <--[oooo]--'
8083 * +==============================+
8084 * L2ARC dev |####|#|###|###| |####| ... |
8085 * +==============================+
8088 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
8089 * evicted, then the L2ARC has cached a buffer much sooner than it probably
8090 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
8091 * safe to say that this is an uncommon case, since buffers at the end of
8092 * the ARC lists have moved there due to inactivity.
8094 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
8095 * then the L2ARC simply misses copying some buffers. This serves as a
8096 * pressure valve to prevent heavy read workloads from both stalling the ARC
8097 * with waits and clogging the L2ARC with writes. This also helps prevent
8098 * the potential for the L2ARC to churn if it attempts to cache content too
8099 * quickly, such as during backups of the entire pool.
8101 * 5. After system boot and before the ARC has filled main memory, there are
8102 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
8103 * lists can remain mostly static. Instead of searching from tail of these
8104 * lists as pictured, the l2arc_feed_thread() will search from the list heads
8105 * for eligible buffers, greatly increasing its chance of finding them.
8107 * The L2ARC device write speed is also boosted during this time so that
8108 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
8109 * there are no L2ARC reads, and no fear of degrading read performance
8110 * through increased writes.
8112 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
8113 * the vdev queue can aggregate them into larger and fewer writes. Each
8114 * device is written to in a rotor fashion, sweeping writes through
8115 * available space then repeating.
8117 * 7. The L2ARC does not store dirty content. It never needs to flush
8118 * write buffers back to disk based storage.
8120 * 8. If an ARC buffer is written (and dirtied) which also exists in the
8121 * L2ARC, the now stale L2ARC buffer is immediately dropped.
8123 * The performance of the L2ARC can be tweaked by a number of tunables, which
8124 * may be necessary for different workloads:
8126 * l2arc_write_max max write bytes per interval
8127 * l2arc_write_boost extra write bytes during device warmup
8128 * l2arc_noprefetch skip caching prefetched buffers
8129 * l2arc_headroom number of max device writes to precache
8130 * l2arc_headroom_boost when we find compressed buffers during ARC
8131 * scanning, we multiply headroom by this
8132 * percentage factor for the next scan cycle,
8133 * since more compressed buffers are likely to
8135 * l2arc_feed_secs seconds between L2ARC writing
8137 * Tunables may be removed or added as future performance improvements are
8138 * integrated, and also may become zpool properties.
8140 * There are three key functions that control how the L2ARC warms up:
8142 * l2arc_write_eligible() check if a buffer is eligible to cache
8143 * l2arc_write_size() calculate how much to write
8144 * l2arc_write_interval() calculate sleep delay between writes
8146 * These three functions determine what to write, how much, and how quickly
8149 * L2ARC persistence:
8151 * When writing buffers to L2ARC, we periodically add some metadata to
8152 * make sure we can pick them up after reboot, thus dramatically reducing
8153 * the impact that any downtime has on the performance of storage systems
8154 * with large caches.
8156 * The implementation works fairly simply by integrating the following two
8159 * *) When writing to the L2ARC, we occasionally write a "l2arc log block",
8160 * which is an additional piece of metadata which describes what's been
8161 * written. This allows us to rebuild the arc_buf_hdr_t structures of the
8162 * main ARC buffers. There are 2 linked-lists of log blocks headed by
8163 * dh_start_lbps[2]. We alternate which chain we append to, so they are
8164 * time-wise and offset-wise interleaved, but that is an optimization rather
8165 * than for correctness. The log block also includes a pointer to the
8166 * previous block in its chain.
8168 * *) We reserve SPA_MINBLOCKSIZE of space at the start of each L2ARC device
8169 * for our header bookkeeping purposes. This contains a device header,
8170 * which contains our top-level reference structures. We update it each
8171 * time we write a new log block, so that we're able to locate it in the
8172 * L2ARC device. If this write results in an inconsistent device header
8173 * (e.g. due to power failure), we detect this by verifying the header's
8174 * checksum and simply fail to reconstruct the L2ARC after reboot.
8176 * Implementation diagram:
8178 * +=== L2ARC device (not to scale) ======================================+
8179 * | ___two newest log block pointers__.__________ |
8180 * | / \dh_start_lbps[1] |
8181 * | / \ \dh_start_lbps[0]|
8183 * ||L2 dev|....|lb |bufs |lb |bufs |lb |bufs |lb |bufs |lb |---(empty)---|
8184 * || hdr| ^ /^ /^ / / |
8185 * |+------+ ...--\-------/ \-----/--\------/ / |
8186 * | \--------------/ \--------------/ |
8187 * +======================================================================+
8189 * As can be seen on the diagram, rather than using a simple linked list,
8190 * we use a pair of linked lists with alternating elements. This is a
8191 * performance enhancement due to the fact that we only find out the
8192 * address of the next log block access once the current block has been
8193 * completely read in. Obviously, this hurts performance, because we'd be
8194 * keeping the device's I/O queue at only a 1 operation deep, thus
8195 * incurring a large amount of I/O round-trip latency. Having two lists
8196 * allows us to fetch two log blocks ahead of where we are currently
8197 * rebuilding L2ARC buffers.
8199 * On-device data structures:
8201 * L2ARC device header: l2arc_dev_hdr_phys_t
8202 * L2ARC log block: l2arc_log_blk_phys_t
8204 * L2ARC reconstruction:
8206 * When writing data, we simply write in the standard rotary fashion,
8207 * evicting buffers as we go and simply writing new data over them (writing
8208 * a new log block every now and then). This obviously means that once we
8209 * loop around the end of the device, we will start cutting into an already
8210 * committed log block (and its referenced data buffers), like so:
8212 * current write head__ __old tail
8215 * <--|bufs |lb |bufs |lb | |bufs |lb |bufs |lb |-->
8216 * ^ ^^^^^^^^^___________________________________
8218 * <<nextwrite>> may overwrite this blk and/or its bufs --'
8220 * When importing the pool, we detect this situation and use it to stop
8221 * our scanning process (see l2arc_rebuild).
8223 * There is one significant caveat to consider when rebuilding ARC contents
8224 * from an L2ARC device: what about invalidated buffers? Given the above
8225 * construction, we cannot update blocks which we've already written to amend
8226 * them to remove buffers which were invalidated. Thus, during reconstruction,
8227 * we might be populating the cache with buffers for data that's not on the
8228 * main pool anymore, or may have been overwritten!
8230 * As it turns out, this isn't a problem. Every arc_read request includes
8231 * both the DVA and, crucially, the birth TXG of the BP the caller is
8232 * looking for. So even if the cache were populated by completely rotten
8233 * blocks for data that had been long deleted and/or overwritten, we'll
8234 * never actually return bad data from the cache, since the DVA with the
8235 * birth TXG uniquely identify a block in space and time - once created,
8236 * a block is immutable on disk. The worst thing we have done is wasted
8237 * some time and memory at l2arc rebuild to reconstruct outdated ARC
8238 * entries that will get dropped from the l2arc as it is being updated
8241 * L2ARC buffers that have been evicted by l2arc_evict() ahead of the write
8242 * hand are not restored. This is done by saving the offset (in bytes)
8243 * l2arc_evict() has evicted to in the L2ARC device header and taking it
8244 * into account when restoring buffers.
8248 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*hdr
)
8251 * A buffer is *not* eligible for the L2ARC if it:
8252 * 1. belongs to a different spa.
8253 * 2. is already cached on the L2ARC.
8254 * 3. has an I/O in progress (it may be an incomplete read).
8255 * 4. is flagged not eligible (zfs property).
8257 if (hdr
->b_spa
!= spa_guid
|| HDR_HAS_L2HDR(hdr
) ||
8258 HDR_IO_IN_PROGRESS(hdr
) || !HDR_L2CACHE(hdr
))
8265 l2arc_write_size(l2arc_dev_t
*dev
)
8270 * Make sure our globals have meaningful values in case the user
8273 size
= l2arc_write_max
;
8275 cmn_err(CE_NOTE
, "l2arc_write_max must be greater than zero, "
8276 "resetting it to the default (%d)", L2ARC_WRITE_SIZE
);
8277 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
8280 if (arc_warm
== B_FALSE
)
8281 size
+= l2arc_write_boost
;
8283 /* We need to add in the worst case scenario of log block overhead. */
8284 size
+= l2arc_log_blk_overhead(size
, dev
);
8285 if (dev
->l2ad_vdev
->vdev_has_trim
&& l2arc_trim_ahead
> 0) {
8287 * Trim ahead of the write size 64MB or (l2arc_trim_ahead/100)
8288 * times the writesize, whichever is greater.
8290 size
+= MAX(64 * 1024 * 1024,
8291 (size
* l2arc_trim_ahead
) / 100);
8295 * Make sure the write size does not exceed the size of the cache
8296 * device. This is important in l2arc_evict(), otherwise infinite
8297 * iteration can occur.
8299 size
= MIN(size
, (dev
->l2ad_end
- dev
->l2ad_start
) / 4);
8301 size
= P2ROUNDUP(size
, 1ULL << dev
->l2ad_vdev
->vdev_ashift
);
8308 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
8310 clock_t interval
, next
, now
;
8313 * If the ARC lists are busy, increase our write rate; if the
8314 * lists are stale, idle back. This is achieved by checking
8315 * how much we previously wrote - if it was more than half of
8316 * what we wanted, schedule the next write much sooner.
8318 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
8319 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
8321 interval
= hz
* l2arc_feed_secs
;
8323 now
= ddi_get_lbolt();
8324 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
8330 l2arc_dev_invalid(const l2arc_dev_t
*dev
)
8333 * We want to skip devices that are being rebuilt, trimmed,
8334 * removed, or belong to a spa that is being exported.
8336 return (dev
->l2ad_vdev
== NULL
|| vdev_is_dead(dev
->l2ad_vdev
) ||
8337 dev
->l2ad_rebuild
|| dev
->l2ad_trim_all
||
8338 dev
->l2ad_spa
== NULL
|| dev
->l2ad_spa
->spa_is_exporting
);
8342 * Cycle through L2ARC devices. This is how L2ARC load balances.
8343 * If a device is returned, this also returns holding the spa config lock.
8345 static l2arc_dev_t
*
8346 l2arc_dev_get_next(void)
8348 l2arc_dev_t
*first
, *next
= NULL
;
8351 * Lock out the removal of spas (spa_namespace_lock), then removal
8352 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
8353 * both locks will be dropped and a spa config lock held instead.
8355 mutex_enter(&spa_namespace_lock
);
8356 mutex_enter(&l2arc_dev_mtx
);
8358 /* if there are no vdevs, there is nothing to do */
8359 if (l2arc_ndev
== 0)
8363 next
= l2arc_dev_last
;
8365 /* loop around the list looking for a non-faulted vdev */
8367 next
= list_head(l2arc_dev_list
);
8369 next
= list_next(l2arc_dev_list
, next
);
8371 next
= list_head(l2arc_dev_list
);
8374 /* if we have come back to the start, bail out */
8377 else if (next
== first
)
8380 ASSERT3P(next
, !=, NULL
);
8381 } while (l2arc_dev_invalid(next
));
8383 /* if we were unable to find any usable vdevs, return NULL */
8384 if (l2arc_dev_invalid(next
))
8387 l2arc_dev_last
= next
;
8390 mutex_exit(&l2arc_dev_mtx
);
8393 * Grab the config lock to prevent the 'next' device from being
8394 * removed while we are writing to it.
8397 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
8398 mutex_exit(&spa_namespace_lock
);
8404 * Free buffers that were tagged for destruction.
8407 l2arc_do_free_on_write(void)
8409 l2arc_data_free_t
*df
;
8411 mutex_enter(&l2arc_free_on_write_mtx
);
8412 while ((df
= list_remove_head(l2arc_free_on_write
)) != NULL
) {
8413 ASSERT3P(df
->l2df_abd
, !=, NULL
);
8414 abd_free(df
->l2df_abd
);
8415 kmem_free(df
, sizeof (l2arc_data_free_t
));
8417 mutex_exit(&l2arc_free_on_write_mtx
);
8421 * A write to a cache device has completed. Update all headers to allow
8422 * reads from these buffers to begin.
8425 l2arc_write_done(zio_t
*zio
)
8427 l2arc_write_callback_t
*cb
;
8428 l2arc_lb_abd_buf_t
*abd_buf
;
8429 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
8431 l2arc_dev_hdr_phys_t
*l2dhdr
;
8433 arc_buf_hdr_t
*head
, *hdr
, *hdr_prev
;
8434 kmutex_t
*hash_lock
;
8435 int64_t bytes_dropped
= 0;
8437 cb
= zio
->io_private
;
8438 ASSERT3P(cb
, !=, NULL
);
8439 dev
= cb
->l2wcb_dev
;
8440 l2dhdr
= dev
->l2ad_dev_hdr
;
8441 ASSERT3P(dev
, !=, NULL
);
8442 head
= cb
->l2wcb_head
;
8443 ASSERT3P(head
, !=, NULL
);
8444 buflist
= &dev
->l2ad_buflist
;
8445 ASSERT3P(buflist
, !=, NULL
);
8446 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
8447 l2arc_write_callback_t
*, cb
);
8450 * All writes completed, or an error was hit.
8453 mutex_enter(&dev
->l2ad_mtx
);
8454 for (hdr
= list_prev(buflist
, head
); hdr
; hdr
= hdr_prev
) {
8455 hdr_prev
= list_prev(buflist
, hdr
);
8457 hash_lock
= HDR_LOCK(hdr
);
8460 * We cannot use mutex_enter or else we can deadlock
8461 * with l2arc_write_buffers (due to swapping the order
8462 * the hash lock and l2ad_mtx are taken).
8464 if (!mutex_tryenter(hash_lock
)) {
8466 * Missed the hash lock. We must retry so we
8467 * don't leave the ARC_FLAG_L2_WRITING bit set.
8469 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry
);
8472 * We don't want to rescan the headers we've
8473 * already marked as having been written out, so
8474 * we reinsert the head node so we can pick up
8475 * where we left off.
8477 list_remove(buflist
, head
);
8478 list_insert_after(buflist
, hdr
, head
);
8480 mutex_exit(&dev
->l2ad_mtx
);
8483 * We wait for the hash lock to become available
8484 * to try and prevent busy waiting, and increase
8485 * the chance we'll be able to acquire the lock
8486 * the next time around.
8488 mutex_enter(hash_lock
);
8489 mutex_exit(hash_lock
);
8494 * We could not have been moved into the arc_l2c_only
8495 * state while in-flight due to our ARC_FLAG_L2_WRITING
8496 * bit being set. Let's just ensure that's being enforced.
8498 ASSERT(HDR_HAS_L1HDR(hdr
));
8501 * Skipped - drop L2ARC entry and mark the header as no
8502 * longer L2 eligibile.
8504 if (zio
->io_error
!= 0) {
8506 * Error - drop L2ARC entry.
8508 list_remove(buflist
, hdr
);
8509 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
8511 uint64_t psize
= HDR_GET_PSIZE(hdr
);
8512 l2arc_hdr_arcstats_decrement(hdr
);
8514 ASSERT(dev
->l2ad_vdev
!= NULL
);
8517 vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
8518 (void) zfs_refcount_remove_many(&dev
->l2ad_alloc
,
8519 arc_hdr_size(hdr
), hdr
);
8523 * Allow ARC to begin reads and ghost list evictions to
8526 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_WRITING
);
8528 mutex_exit(hash_lock
);
8532 * Free the allocated abd buffers for writing the log blocks.
8533 * If the zio failed reclaim the allocated space and remove the
8534 * pointers to these log blocks from the log block pointer list
8535 * of the L2ARC device.
8537 while ((abd_buf
= list_remove_tail(&cb
->l2wcb_abd_list
)) != NULL
) {
8538 abd_free(abd_buf
->abd
);
8539 zio_buf_free(abd_buf
, sizeof (*abd_buf
));
8540 if (zio
->io_error
!= 0) {
8541 lb_ptr_buf
= list_remove_head(&dev
->l2ad_lbptr_list
);
8543 * L2BLK_GET_PSIZE returns aligned size for log
8547 L2BLK_GET_PSIZE((lb_ptr_buf
->lb_ptr
)->lbp_prop
);
8548 bytes_dropped
+= asize
;
8549 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, -asize
);
8550 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count
);
8551 zfs_refcount_remove_many(&dev
->l2ad_lb_asize
, asize
,
8553 (void) zfs_refcount_remove(&dev
->l2ad_lb_count
,
8555 kmem_free(lb_ptr_buf
->lb_ptr
,
8556 sizeof (l2arc_log_blkptr_t
));
8557 kmem_free(lb_ptr_buf
, sizeof (l2arc_lb_ptr_buf_t
));
8560 list_destroy(&cb
->l2wcb_abd_list
);
8562 if (zio
->io_error
!= 0) {
8563 ARCSTAT_BUMP(arcstat_l2_writes_error
);
8566 * Restore the lbps array in the header to its previous state.
8567 * If the list of log block pointers is empty, zero out the
8568 * log block pointers in the device header.
8570 lb_ptr_buf
= list_head(&dev
->l2ad_lbptr_list
);
8571 for (int i
= 0; i
< 2; i
++) {
8572 if (lb_ptr_buf
== NULL
) {
8574 * If the list is empty zero out the device
8575 * header. Otherwise zero out the second log
8576 * block pointer in the header.
8580 dev
->l2ad_dev_hdr_asize
);
8582 memset(&l2dhdr
->dh_start_lbps
[i
], 0,
8583 sizeof (l2arc_log_blkptr_t
));
8587 memcpy(&l2dhdr
->dh_start_lbps
[i
], lb_ptr_buf
->lb_ptr
,
8588 sizeof (l2arc_log_blkptr_t
));
8589 lb_ptr_buf
= list_next(&dev
->l2ad_lbptr_list
,
8594 ARCSTAT_BUMP(arcstat_l2_writes_done
);
8595 list_remove(buflist
, head
);
8596 ASSERT(!HDR_HAS_L1HDR(head
));
8597 kmem_cache_free(hdr_l2only_cache
, head
);
8598 mutex_exit(&dev
->l2ad_mtx
);
8600 ASSERT(dev
->l2ad_vdev
!= NULL
);
8601 vdev_space_update(dev
->l2ad_vdev
, -bytes_dropped
, 0, 0);
8603 l2arc_do_free_on_write();
8605 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
8609 l2arc_untransform(zio_t
*zio
, l2arc_read_callback_t
*cb
)
8612 spa_t
*spa
= zio
->io_spa
;
8613 arc_buf_hdr_t
*hdr
= cb
->l2rcb_hdr
;
8614 blkptr_t
*bp
= zio
->io_bp
;
8615 uint8_t salt
[ZIO_DATA_SALT_LEN
];
8616 uint8_t iv
[ZIO_DATA_IV_LEN
];
8617 uint8_t mac
[ZIO_DATA_MAC_LEN
];
8618 boolean_t no_crypt
= B_FALSE
;
8621 * ZIL data is never be written to the L2ARC, so we don't need
8622 * special handling for its unique MAC storage.
8624 ASSERT3U(BP_GET_TYPE(bp
), !=, DMU_OT_INTENT_LOG
);
8625 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
8626 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8629 * If the data was encrypted, decrypt it now. Note that
8630 * we must check the bp here and not the hdr, since the
8631 * hdr does not have its encryption parameters updated
8632 * until arc_read_done().
8634 if (BP_IS_ENCRYPTED(bp
)) {
8635 abd_t
*eabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
,
8636 ARC_HDR_USE_RESERVE
);
8638 zio_crypt_decode_params_bp(bp
, salt
, iv
);
8639 zio_crypt_decode_mac_bp(bp
, mac
);
8641 ret
= spa_do_crypt_abd(B_FALSE
, spa
, &cb
->l2rcb_zb
,
8642 BP_GET_TYPE(bp
), BP_GET_DEDUP(bp
), BP_SHOULD_BYTESWAP(bp
),
8643 salt
, iv
, mac
, HDR_GET_PSIZE(hdr
), eabd
,
8644 hdr
->b_l1hdr
.b_pabd
, &no_crypt
);
8646 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8651 * If we actually performed decryption, replace b_pabd
8652 * with the decrypted data. Otherwise we can just throw
8653 * our decryption buffer away.
8656 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8657 arc_hdr_size(hdr
), hdr
);
8658 hdr
->b_l1hdr
.b_pabd
= eabd
;
8661 arc_free_data_abd(hdr
, eabd
, arc_hdr_size(hdr
), hdr
);
8666 * If the L2ARC block was compressed, but ARC compression
8667 * is disabled we decompress the data into a new buffer and
8668 * replace the existing data.
8670 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
8671 !HDR_COMPRESSION_ENABLED(hdr
)) {
8672 abd_t
*cabd
= arc_get_data_abd(hdr
, arc_hdr_size(hdr
), hdr
,
8673 ARC_HDR_USE_RESERVE
);
8675 ret
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
8676 hdr
->b_l1hdr
.b_pabd
, cabd
, HDR_GET_PSIZE(hdr
),
8677 HDR_GET_LSIZE(hdr
), &hdr
->b_complevel
);
8679 arc_free_data_abd(hdr
, cabd
, arc_hdr_size(hdr
), hdr
);
8683 arc_free_data_abd(hdr
, hdr
->b_l1hdr
.b_pabd
,
8684 arc_hdr_size(hdr
), hdr
);
8685 hdr
->b_l1hdr
.b_pabd
= cabd
;
8687 zio
->io_size
= HDR_GET_LSIZE(hdr
);
8698 * A read to a cache device completed. Validate buffer contents before
8699 * handing over to the regular ARC routines.
8702 l2arc_read_done(zio_t
*zio
)
8705 l2arc_read_callback_t
*cb
= zio
->io_private
;
8707 kmutex_t
*hash_lock
;
8708 boolean_t valid_cksum
;
8709 boolean_t using_rdata
= (BP_IS_ENCRYPTED(&cb
->l2rcb_bp
) &&
8710 (cb
->l2rcb_flags
& ZIO_FLAG_RAW_ENCRYPT
));
8712 ASSERT3P(zio
->io_vd
, !=, NULL
);
8713 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
8715 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
8717 ASSERT3P(cb
, !=, NULL
);
8718 hdr
= cb
->l2rcb_hdr
;
8719 ASSERT3P(hdr
, !=, NULL
);
8721 hash_lock
= HDR_LOCK(hdr
);
8722 mutex_enter(hash_lock
);
8723 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
8726 * If the data was read into a temporary buffer,
8727 * move it and free the buffer.
8729 if (cb
->l2rcb_abd
!= NULL
) {
8730 ASSERT3U(arc_hdr_size(hdr
), <, zio
->io_size
);
8731 if (zio
->io_error
== 0) {
8733 abd_copy(hdr
->b_crypt_hdr
.b_rabd
,
8734 cb
->l2rcb_abd
, arc_hdr_size(hdr
));
8736 abd_copy(hdr
->b_l1hdr
.b_pabd
,
8737 cb
->l2rcb_abd
, arc_hdr_size(hdr
));
8742 * The following must be done regardless of whether
8743 * there was an error:
8744 * - free the temporary buffer
8745 * - point zio to the real ARC buffer
8746 * - set zio size accordingly
8747 * These are required because zio is either re-used for
8748 * an I/O of the block in the case of the error
8749 * or the zio is passed to arc_read_done() and it
8752 abd_free(cb
->l2rcb_abd
);
8753 zio
->io_size
= zio
->io_orig_size
= arc_hdr_size(hdr
);
8756 ASSERT(HDR_HAS_RABD(hdr
));
8757 zio
->io_abd
= zio
->io_orig_abd
=
8758 hdr
->b_crypt_hdr
.b_rabd
;
8760 ASSERT3P(hdr
->b_l1hdr
.b_pabd
, !=, NULL
);
8761 zio
->io_abd
= zio
->io_orig_abd
= hdr
->b_l1hdr
.b_pabd
;
8765 ASSERT3P(zio
->io_abd
, !=, NULL
);
8768 * Check this survived the L2ARC journey.
8770 ASSERT(zio
->io_abd
== hdr
->b_l1hdr
.b_pabd
||
8771 (HDR_HAS_RABD(hdr
) && zio
->io_abd
== hdr
->b_crypt_hdr
.b_rabd
));
8772 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
8773 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
8774 zio
->io_prop
.zp_complevel
= hdr
->b_complevel
;
8776 valid_cksum
= arc_cksum_is_equal(hdr
, zio
);
8779 * b_rabd will always match the data as it exists on disk if it is
8780 * being used. Therefore if we are reading into b_rabd we do not
8781 * attempt to untransform the data.
8783 if (valid_cksum
&& !using_rdata
)
8784 tfm_error
= l2arc_untransform(zio
, cb
);
8786 if (valid_cksum
&& tfm_error
== 0 && zio
->io_error
== 0 &&
8787 !HDR_L2_EVICTED(hdr
)) {
8788 mutex_exit(hash_lock
);
8789 zio
->io_private
= hdr
;
8793 * Buffer didn't survive caching. Increment stats and
8794 * reissue to the original storage device.
8796 if (zio
->io_error
!= 0) {
8797 ARCSTAT_BUMP(arcstat_l2_io_error
);
8799 zio
->io_error
= SET_ERROR(EIO
);
8801 if (!valid_cksum
|| tfm_error
!= 0)
8802 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
8805 * If there's no waiter, issue an async i/o to the primary
8806 * storage now. If there *is* a waiter, the caller must
8807 * issue the i/o in a context where it's OK to block.
8809 if (zio
->io_waiter
== NULL
) {
8810 zio_t
*pio
= zio_unique_parent(zio
);
8811 void *abd
= (using_rdata
) ?
8812 hdr
->b_crypt_hdr
.b_rabd
: hdr
->b_l1hdr
.b_pabd
;
8814 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
8816 zio
= zio_read(pio
, zio
->io_spa
, zio
->io_bp
,
8817 abd
, zio
->io_size
, arc_read_done
,
8818 hdr
, zio
->io_priority
, cb
->l2rcb_flags
,
8822 * Original ZIO will be freed, so we need to update
8823 * ARC header with the new ZIO pointer to be used
8824 * by zio_change_priority() in arc_read().
8826 for (struct arc_callback
*acb
= hdr
->b_l1hdr
.b_acb
;
8827 acb
!= NULL
; acb
= acb
->acb_next
)
8828 acb
->acb_zio_head
= zio
;
8830 mutex_exit(hash_lock
);
8833 mutex_exit(hash_lock
);
8837 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
8841 * This is the list priority from which the L2ARC will search for pages to
8842 * cache. This is used within loops (0..3) to cycle through lists in the
8843 * desired order. This order can have a significant effect on cache
8846 * Currently the metadata lists are hit first, MFU then MRU, followed by
8847 * the data lists. This function returns a locked list, and also returns
8850 static multilist_sublist_t
*
8851 l2arc_sublist_lock(int list_num
)
8853 multilist_t
*ml
= NULL
;
8856 ASSERT(list_num
>= 0 && list_num
< L2ARC_FEED_TYPES
);
8860 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
8863 ml
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
8866 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
8869 ml
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
8876 * Return a randomly-selected sublist. This is acceptable
8877 * because the caller feeds only a little bit of data for each
8878 * call (8MB). Subsequent calls will result in different
8879 * sublists being selected.
8881 idx
= multilist_get_random_index(ml
);
8882 return (multilist_sublist_lock_idx(ml
, idx
));
8886 * Calculates the maximum overhead of L2ARC metadata log blocks for a given
8887 * L2ARC write size. l2arc_evict and l2arc_write_size need to include this
8888 * overhead in processing to make sure there is enough headroom available
8889 * when writing buffers.
8891 static inline uint64_t
8892 l2arc_log_blk_overhead(uint64_t write_sz
, l2arc_dev_t
*dev
)
8894 if (dev
->l2ad_log_entries
== 0) {
8897 ASSERT(dev
->l2ad_vdev
!= NULL
);
8899 uint64_t log_entries
= write_sz
>> SPA_MINBLOCKSHIFT
;
8901 uint64_t log_blocks
= (log_entries
+
8902 dev
->l2ad_log_entries
- 1) /
8903 dev
->l2ad_log_entries
;
8905 return (vdev_psize_to_asize(dev
->l2ad_vdev
,
8906 sizeof (l2arc_log_blk_phys_t
)) * log_blocks
);
8911 * Evict buffers from the device write hand to the distance specified in
8912 * bytes. This distance may span populated buffers, it may span nothing.
8913 * This is clearing a region on the L2ARC device ready for writing.
8914 * If the 'all' boolean is set, every buffer is evicted.
8917 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
8920 arc_buf_hdr_t
*hdr
, *hdr_prev
;
8921 kmutex_t
*hash_lock
;
8923 l2arc_lb_ptr_buf_t
*lb_ptr_buf
, *lb_ptr_buf_prev
;
8924 vdev_t
*vd
= dev
->l2ad_vdev
;
8927 ASSERT(vd
!= NULL
|| all
);
8928 ASSERT(dev
->l2ad_spa
!= NULL
|| all
);
8930 buflist
= &dev
->l2ad_buflist
;
8934 if (dev
->l2ad_hand
+ distance
> dev
->l2ad_end
) {
8936 * When there is no space to accommodate upcoming writes,
8937 * evict to the end. Then bump the write and evict hands
8938 * to the start and iterate. This iteration does not
8939 * happen indefinitely as we make sure in
8940 * l2arc_write_size() that when the write hand is reset,
8941 * the write size does not exceed the end of the device.
8944 taddr
= dev
->l2ad_end
;
8946 taddr
= dev
->l2ad_hand
+ distance
;
8948 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
8949 uint64_t, taddr
, boolean_t
, all
);
8953 * This check has to be placed after deciding whether to
8956 if (dev
->l2ad_first
) {
8958 * This is the first sweep through the device. There is
8959 * nothing to evict. We have already trimmmed the
8965 * Trim the space to be evicted.
8967 if (vd
->vdev_has_trim
&& dev
->l2ad_evict
< taddr
&&
8968 l2arc_trim_ahead
> 0) {
8970 * We have to drop the spa_config lock because
8971 * vdev_trim_range() will acquire it.
8972 * l2ad_evict already accounts for the label
8973 * size. To prevent vdev_trim_ranges() from
8974 * adding it again, we subtract it from
8977 spa_config_exit(dev
->l2ad_spa
, SCL_L2ARC
, dev
);
8978 vdev_trim_simple(vd
,
8979 dev
->l2ad_evict
- VDEV_LABEL_START_SIZE
,
8980 taddr
- dev
->l2ad_evict
);
8981 spa_config_enter(dev
->l2ad_spa
, SCL_L2ARC
, dev
,
8986 * When rebuilding L2ARC we retrieve the evict hand
8987 * from the header of the device. Of note, l2arc_evict()
8988 * does not actually delete buffers from the cache
8989 * device, but trimming may do so depending on the
8990 * hardware implementation. Thus keeping track of the
8991 * evict hand is useful.
8993 dev
->l2ad_evict
= MAX(dev
->l2ad_evict
, taddr
);
8998 mutex_enter(&dev
->l2ad_mtx
);
9000 * We have to account for evicted log blocks. Run vdev_space_update()
9001 * on log blocks whose offset (in bytes) is before the evicted offset
9002 * (in bytes) by searching in the list of pointers to log blocks
9003 * present in the L2ARC device.
9005 for (lb_ptr_buf
= list_tail(&dev
->l2ad_lbptr_list
); lb_ptr_buf
;
9006 lb_ptr_buf
= lb_ptr_buf_prev
) {
9008 lb_ptr_buf_prev
= list_prev(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
9010 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
9011 uint64_t asize
= L2BLK_GET_PSIZE(
9012 (lb_ptr_buf
->lb_ptr
)->lbp_prop
);
9015 * We don't worry about log blocks left behind (ie
9016 * lbp_payload_start < l2ad_hand) because l2arc_write_buffers()
9017 * will never write more than l2arc_evict() evicts.
9019 if (!all
&& l2arc_log_blkptr_valid(dev
, lb_ptr_buf
->lb_ptr
)) {
9023 vdev_space_update(vd
, -asize
, 0, 0);
9024 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, -asize
);
9025 ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count
);
9026 zfs_refcount_remove_many(&dev
->l2ad_lb_asize
, asize
,
9028 (void) zfs_refcount_remove(&dev
->l2ad_lb_count
,
9030 list_remove(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
9031 kmem_free(lb_ptr_buf
->lb_ptr
,
9032 sizeof (l2arc_log_blkptr_t
));
9033 kmem_free(lb_ptr_buf
, sizeof (l2arc_lb_ptr_buf_t
));
9037 for (hdr
= list_tail(buflist
); hdr
; hdr
= hdr_prev
) {
9038 hdr_prev
= list_prev(buflist
, hdr
);
9040 ASSERT(!HDR_EMPTY(hdr
));
9041 hash_lock
= HDR_LOCK(hdr
);
9044 * We cannot use mutex_enter or else we can deadlock
9045 * with l2arc_write_buffers (due to swapping the order
9046 * the hash lock and l2ad_mtx are taken).
9048 if (!mutex_tryenter(hash_lock
)) {
9050 * Missed the hash lock. Retry.
9052 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
9053 mutex_exit(&dev
->l2ad_mtx
);
9054 mutex_enter(hash_lock
);
9055 mutex_exit(hash_lock
);
9060 * A header can't be on this list if it doesn't have L2 header.
9062 ASSERT(HDR_HAS_L2HDR(hdr
));
9064 /* Ensure this header has finished being written. */
9065 ASSERT(!HDR_L2_WRITING(hdr
));
9066 ASSERT(!HDR_L2_WRITE_HEAD(hdr
));
9068 if (!all
&& (hdr
->b_l2hdr
.b_daddr
>= dev
->l2ad_evict
||
9069 hdr
->b_l2hdr
.b_daddr
< dev
->l2ad_hand
)) {
9071 * We've evicted to the target address,
9072 * or the end of the device.
9074 mutex_exit(hash_lock
);
9078 if (!HDR_HAS_L1HDR(hdr
)) {
9079 ASSERT(!HDR_L2_READING(hdr
));
9081 * This doesn't exist in the ARC. Destroy.
9082 * arc_hdr_destroy() will call list_remove()
9083 * and decrement arcstat_l2_lsize.
9085 arc_change_state(arc_anon
, hdr
);
9086 arc_hdr_destroy(hdr
);
9088 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_l2c_only
);
9089 ARCSTAT_BUMP(arcstat_l2_evict_l1cached
);
9091 * Invalidate issued or about to be issued
9092 * reads, since we may be about to write
9093 * over this location.
9095 if (HDR_L2_READING(hdr
)) {
9096 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
9097 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_EVICTED
);
9100 arc_hdr_l2hdr_destroy(hdr
);
9102 mutex_exit(hash_lock
);
9104 mutex_exit(&dev
->l2ad_mtx
);
9108 * We need to check if we evict all buffers, otherwise we may iterate
9111 if (!all
&& rerun
) {
9113 * Bump device hand to the device start if it is approaching the
9114 * end. l2arc_evict() has already evicted ahead for this case.
9116 dev
->l2ad_hand
= dev
->l2ad_start
;
9117 dev
->l2ad_evict
= dev
->l2ad_start
;
9118 dev
->l2ad_first
= B_FALSE
;
9124 * In case of cache device removal (all) the following
9125 * assertions may be violated without functional consequences
9126 * as the device is about to be removed.
9128 ASSERT3U(dev
->l2ad_hand
+ distance
, <=, dev
->l2ad_end
);
9129 if (!dev
->l2ad_first
)
9130 ASSERT3U(dev
->l2ad_hand
, <=, dev
->l2ad_evict
);
9135 * Handle any abd transforms that might be required for writing to the L2ARC.
9136 * If successful, this function will always return an abd with the data
9137 * transformed as it is on disk in a new abd of asize bytes.
9140 l2arc_apply_transforms(spa_t
*spa
, arc_buf_hdr_t
*hdr
, uint64_t asize
,
9144 abd_t
*cabd
= NULL
, *eabd
= NULL
, *to_write
= hdr
->b_l1hdr
.b_pabd
;
9145 enum zio_compress compress
= HDR_GET_COMPRESS(hdr
);
9146 uint64_t psize
= HDR_GET_PSIZE(hdr
);
9147 uint64_t size
= arc_hdr_size(hdr
);
9148 boolean_t ismd
= HDR_ISTYPE_METADATA(hdr
);
9149 boolean_t bswap
= (hdr
->b_l1hdr
.b_byteswap
!= DMU_BSWAP_NUMFUNCS
);
9150 dsl_crypto_key_t
*dck
= NULL
;
9151 uint8_t mac
[ZIO_DATA_MAC_LEN
] = { 0 };
9152 boolean_t no_crypt
= B_FALSE
;
9154 ASSERT((HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
9155 !HDR_COMPRESSION_ENABLED(hdr
)) ||
9156 HDR_ENCRYPTED(hdr
) || HDR_SHARED_DATA(hdr
) || psize
!= asize
);
9157 ASSERT3U(psize
, <=, asize
);
9160 * If this data simply needs its own buffer, we simply allocate it
9161 * and copy the data. This may be done to eliminate a dependency on a
9162 * shared buffer or to reallocate the buffer to match asize.
9164 if (HDR_HAS_RABD(hdr
)) {
9165 ASSERT3U(asize
, >, psize
);
9166 to_write
= abd_alloc_for_io(asize
, ismd
);
9167 abd_copy(to_write
, hdr
->b_crypt_hdr
.b_rabd
, psize
);
9168 abd_zero_off(to_write
, psize
, asize
- psize
);
9172 if ((compress
== ZIO_COMPRESS_OFF
|| HDR_COMPRESSION_ENABLED(hdr
)) &&
9173 !HDR_ENCRYPTED(hdr
)) {
9174 ASSERT3U(size
, ==, psize
);
9175 to_write
= abd_alloc_for_io(asize
, ismd
);
9176 abd_copy(to_write
, hdr
->b_l1hdr
.b_pabd
, size
);
9178 abd_zero_off(to_write
, size
, asize
- size
);
9182 if (compress
!= ZIO_COMPRESS_OFF
&& !HDR_COMPRESSION_ENABLED(hdr
)) {
9183 cabd
= abd_alloc_for_io(MAX(size
, asize
), ismd
);
9184 uint64_t csize
= zio_compress_data(compress
, to_write
, &cabd
,
9185 size
, MIN(size
, psize
), hdr
->b_complevel
);
9186 if (csize
>= size
|| csize
> psize
) {
9188 * We can't re-compress the block into the original
9189 * psize. Even if it fits into asize, it does not
9190 * matter, since checksum will never match on read.
9193 return (SET_ERROR(EIO
));
9196 abd_zero_off(cabd
, csize
, asize
- csize
);
9200 if (HDR_ENCRYPTED(hdr
)) {
9201 eabd
= abd_alloc_for_io(asize
, ismd
);
9204 * If the dataset was disowned before the buffer
9205 * made it to this point, the key to re-encrypt
9206 * it won't be available. In this case we simply
9207 * won't write the buffer to the L2ARC.
9209 ret
= spa_keystore_lookup_key(spa
, hdr
->b_crypt_hdr
.b_dsobj
,
9214 ret
= zio_do_crypt_abd(B_TRUE
, &dck
->dck_key
,
9215 hdr
->b_crypt_hdr
.b_ot
, bswap
, hdr
->b_crypt_hdr
.b_salt
,
9216 hdr
->b_crypt_hdr
.b_iv
, mac
, psize
, to_write
, eabd
,
9222 abd_copy(eabd
, to_write
, psize
);
9225 abd_zero_off(eabd
, psize
, asize
- psize
);
9227 /* assert that the MAC we got here matches the one we saved */
9228 ASSERT0(memcmp(mac
, hdr
->b_crypt_hdr
.b_mac
, ZIO_DATA_MAC_LEN
));
9229 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
9231 if (to_write
== cabd
)
9238 ASSERT3P(to_write
, !=, hdr
->b_l1hdr
.b_pabd
);
9239 *abd_out
= to_write
;
9244 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
9255 l2arc_blk_fetch_done(zio_t
*zio
)
9257 l2arc_read_callback_t
*cb
;
9259 cb
= zio
->io_private
;
9260 if (cb
->l2rcb_abd
!= NULL
)
9261 abd_free(cb
->l2rcb_abd
);
9262 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
9266 * Find and write ARC buffers to the L2ARC device.
9268 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
9269 * for reading until they have completed writing.
9270 * The headroom_boost is an in-out parameter used to maintain headroom boost
9271 * state between calls to this function.
9273 * Returns the number of bytes actually written (which may be smaller than
9274 * the delta by which the device hand has changed due to alignment and the
9275 * writing of log blocks).
9278 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
)
9280 arc_buf_hdr_t
*hdr
, *head
, *marker
;
9281 uint64_t write_asize
, write_psize
, headroom
;
9282 boolean_t full
, from_head
= !arc_warm
;
9283 l2arc_write_callback_t
*cb
= NULL
;
9285 uint64_t guid
= spa_load_guid(spa
);
9286 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
9288 ASSERT3P(dev
->l2ad_vdev
, !=, NULL
);
9291 write_asize
= write_psize
= 0;
9293 head
= kmem_cache_alloc(hdr_l2only_cache
, KM_PUSHPAGE
);
9294 arc_hdr_set_flags(head
, ARC_FLAG_L2_WRITE_HEAD
| ARC_FLAG_HAS_L2HDR
);
9295 marker
= arc_state_alloc_marker();
9298 * Copy buffers for L2ARC writing.
9300 for (int pass
= 0; pass
< L2ARC_FEED_TYPES
; pass
++) {
9302 * pass == 0: MFU meta
9303 * pass == 1: MRU meta
9304 * pass == 2: MFU data
9305 * pass == 3: MRU data
9307 if (l2arc_mfuonly
== 1) {
9308 if (pass
== 1 || pass
== 3)
9310 } else if (l2arc_mfuonly
> 1) {
9315 uint64_t passed_sz
= 0;
9316 headroom
= target_sz
* l2arc_headroom
;
9317 if (zfs_compressed_arc_enabled
)
9318 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
9321 * Until the ARC is warm and starts to evict, read from the
9322 * head of the ARC lists rather than the tail.
9324 multilist_sublist_t
*mls
= l2arc_sublist_lock(pass
);
9325 ASSERT3P(mls
, !=, NULL
);
9327 hdr
= multilist_sublist_head(mls
);
9329 hdr
= multilist_sublist_tail(mls
);
9331 while (hdr
!= NULL
) {
9332 kmutex_t
*hash_lock
;
9333 abd_t
*to_write
= NULL
;
9335 hash_lock
= HDR_LOCK(hdr
);
9336 if (!mutex_tryenter(hash_lock
)) {
9338 /* Skip this buffer rather than waiting. */
9340 hdr
= multilist_sublist_next(mls
, hdr
);
9342 hdr
= multilist_sublist_prev(mls
, hdr
);
9346 passed_sz
+= HDR_GET_LSIZE(hdr
);
9347 if (l2arc_headroom
!= 0 && passed_sz
> headroom
) {
9351 mutex_exit(hash_lock
);
9355 if (!l2arc_write_eligible(guid
, hdr
)) {
9356 mutex_exit(hash_lock
);
9360 ASSERT(HDR_HAS_L1HDR(hdr
));
9361 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
9362 ASSERT3U(arc_hdr_size(hdr
), >, 0);
9363 ASSERT(hdr
->b_l1hdr
.b_pabd
!= NULL
||
9365 uint64_t psize
= HDR_GET_PSIZE(hdr
);
9366 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
,
9370 * If the allocated size of this buffer plus the max
9371 * size for the pending log block exceeds the evicted
9372 * target size, terminate writing buffers for this run.
9374 if (write_asize
+ asize
+
9375 sizeof (l2arc_log_blk_phys_t
) > target_sz
) {
9377 mutex_exit(hash_lock
);
9382 * We should not sleep with sublist lock held or it
9383 * may block ARC eviction. Insert a marker to save
9384 * the position and drop the lock.
9387 multilist_sublist_insert_after(mls
, hdr
,
9390 multilist_sublist_insert_before(mls
, hdr
,
9393 multilist_sublist_unlock(mls
);
9396 * If this header has b_rabd, we can use this since it
9397 * must always match the data exactly as it exists on
9398 * disk. Otherwise, the L2ARC can normally use the
9399 * hdr's data, but if we're sharing data between the
9400 * hdr and one of its bufs, L2ARC needs its own copy of
9401 * the data so that the ZIO below can't race with the
9402 * buf consumer. To ensure that this copy will be
9403 * available for the lifetime of the ZIO and be cleaned
9404 * up afterwards, we add it to the l2arc_free_on_write
9405 * queue. If we need to apply any transforms to the
9406 * data (compression, encryption) we will also need the
9409 if (HDR_HAS_RABD(hdr
) && psize
== asize
) {
9410 to_write
= hdr
->b_crypt_hdr
.b_rabd
;
9411 } else if ((HDR_COMPRESSION_ENABLED(hdr
) ||
9412 HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
) &&
9413 !HDR_ENCRYPTED(hdr
) && !HDR_SHARED_DATA(hdr
) &&
9415 to_write
= hdr
->b_l1hdr
.b_pabd
;
9418 arc_buf_contents_t type
= arc_buf_type(hdr
);
9420 ret
= l2arc_apply_transforms(spa
, hdr
, asize
,
9423 arc_hdr_clear_flags(hdr
,
9425 mutex_exit(hash_lock
);
9429 l2arc_free_abd_on_write(to_write
, asize
, type
);
9432 hdr
->b_l2hdr
.b_dev
= dev
;
9433 hdr
->b_l2hdr
.b_daddr
= dev
->l2ad_hand
;
9434 hdr
->b_l2hdr
.b_hits
= 0;
9435 hdr
->b_l2hdr
.b_arcs_state
=
9436 hdr
->b_l1hdr
.b_state
->arcs_state
;
9437 /* l2arc_hdr_arcstats_update() expects a valid asize */
9438 HDR_SET_L2SIZE(hdr
, asize
);
9439 arc_hdr_set_flags(hdr
, ARC_FLAG_HAS_L2HDR
|
9440 ARC_FLAG_L2_WRITING
);
9442 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
9443 arc_hdr_size(hdr
), hdr
);
9444 l2arc_hdr_arcstats_increment(hdr
);
9445 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
9447 mutex_enter(&dev
->l2ad_mtx
);
9450 * Insert a dummy header on the buflist so
9451 * l2arc_write_done() can find where the
9452 * write buffers begin without searching.
9454 list_insert_head(&dev
->l2ad_buflist
, head
);
9456 list_insert_head(&dev
->l2ad_buflist
, hdr
);
9457 mutex_exit(&dev
->l2ad_mtx
);
9459 boolean_t commit
= l2arc_log_blk_insert(dev
, hdr
);
9460 mutex_exit(hash_lock
);
9464 sizeof (l2arc_write_callback_t
), KM_SLEEP
);
9465 cb
->l2wcb_dev
= dev
;
9466 cb
->l2wcb_head
= head
;
9467 list_create(&cb
->l2wcb_abd_list
,
9468 sizeof (l2arc_lb_abd_buf_t
),
9469 offsetof(l2arc_lb_abd_buf_t
, node
));
9470 pio
= zio_root(spa
, l2arc_write_done
, cb
,
9474 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
9475 dev
->l2ad_hand
, asize
, to_write
,
9476 ZIO_CHECKSUM_OFF
, NULL
, hdr
,
9477 ZIO_PRIORITY_ASYNC_WRITE
,
9478 ZIO_FLAG_CANFAIL
, B_FALSE
);
9480 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
9484 write_psize
+= psize
;
9485 write_asize
+= asize
;
9486 dev
->l2ad_hand
+= asize
;
9489 /* l2ad_hand will be adjusted inside. */
9491 l2arc_log_blk_commit(dev
, pio
, cb
);
9495 multilist_sublist_lock(mls
);
9497 hdr
= multilist_sublist_next(mls
, marker
);
9499 hdr
= multilist_sublist_prev(mls
, marker
);
9500 multilist_sublist_remove(mls
, marker
);
9503 multilist_sublist_unlock(mls
);
9509 arc_state_free_marker(marker
);
9511 /* No buffers selected for writing? */
9513 ASSERT0(write_psize
);
9514 ASSERT(!HDR_HAS_L1HDR(head
));
9515 kmem_cache_free(hdr_l2only_cache
, head
);
9518 * Although we did not write any buffers l2ad_evict may
9521 if (dev
->l2ad_evict
!= l2dhdr
->dh_evict
)
9522 l2arc_dev_hdr_update(dev
);
9527 if (!dev
->l2ad_first
)
9528 ASSERT3U(dev
->l2ad_hand
, <=, dev
->l2ad_evict
);
9530 ASSERT3U(write_asize
, <=, target_sz
);
9531 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
9532 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_psize
);
9534 dev
->l2ad_writing
= B_TRUE
;
9535 (void) zio_wait(pio
);
9536 dev
->l2ad_writing
= B_FALSE
;
9539 * Update the device header after the zio completes as
9540 * l2arc_write_done() may have updated the memory holding the log block
9541 * pointers in the device header.
9543 l2arc_dev_hdr_update(dev
);
9545 return (write_asize
);
9549 l2arc_hdr_limit_reached(void)
9551 int64_t s
= aggsum_upper_bound(&arc_sums
.arcstat_l2_hdr_size
);
9553 return (arc_reclaim_needed() ||
9554 (s
> (arc_warm
? arc_c
: arc_c_max
) * l2arc_meta_percent
/ 100));
9558 * This thread feeds the L2ARC at regular intervals. This is the beating
9559 * heart of the L2ARC.
9561 static __attribute__((noreturn
)) void
9562 l2arc_feed_thread(void *unused
)
9568 uint64_t size
, wrote
;
9569 clock_t begin
, next
= ddi_get_lbolt();
9570 fstrans_cookie_t cookie
;
9572 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
9574 mutex_enter(&l2arc_feed_thr_lock
);
9576 cookie
= spl_fstrans_mark();
9577 while (l2arc_thread_exit
== 0) {
9578 CALLB_CPR_SAFE_BEGIN(&cpr
);
9579 (void) cv_timedwait_idle(&l2arc_feed_thr_cv
,
9580 &l2arc_feed_thr_lock
, next
);
9581 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
9582 next
= ddi_get_lbolt() + hz
;
9585 * Quick check for L2ARC devices.
9587 mutex_enter(&l2arc_dev_mtx
);
9588 if (l2arc_ndev
== 0) {
9589 mutex_exit(&l2arc_dev_mtx
);
9592 mutex_exit(&l2arc_dev_mtx
);
9593 begin
= ddi_get_lbolt();
9596 * This selects the next l2arc device to write to, and in
9597 * doing so the next spa to feed from: dev->l2ad_spa. This
9598 * will return NULL if there are now no l2arc devices or if
9599 * they are all faulted.
9601 * If a device is returned, its spa's config lock is also
9602 * held to prevent device removal. l2arc_dev_get_next()
9603 * will grab and release l2arc_dev_mtx.
9605 if ((dev
= l2arc_dev_get_next()) == NULL
)
9608 spa
= dev
->l2ad_spa
;
9609 ASSERT3P(spa
, !=, NULL
);
9612 * If the pool is read-only then force the feed thread to
9613 * sleep a little longer.
9615 if (!spa_writeable(spa
)) {
9616 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
9617 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9622 * Avoid contributing to memory pressure.
9624 if (l2arc_hdr_limit_reached()) {
9625 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
9626 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9630 ARCSTAT_BUMP(arcstat_l2_feeds
);
9632 size
= l2arc_write_size(dev
);
9635 * Evict L2ARC buffers that will be overwritten.
9637 l2arc_evict(dev
, size
, B_FALSE
);
9640 * Write ARC buffers.
9642 wrote
= l2arc_write_buffers(spa
, dev
, size
);
9645 * Calculate interval between writes.
9647 next
= l2arc_write_interval(begin
, size
, wrote
);
9648 spa_config_exit(spa
, SCL_L2ARC
, dev
);
9650 spl_fstrans_unmark(cookie
);
9652 l2arc_thread_exit
= 0;
9653 cv_broadcast(&l2arc_feed_thr_cv
);
9654 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
9659 l2arc_vdev_present(vdev_t
*vd
)
9661 return (l2arc_vdev_get(vd
) != NULL
);
9665 * Returns the l2arc_dev_t associated with a particular vdev_t or NULL if
9666 * the vdev_t isn't an L2ARC device.
9669 l2arc_vdev_get(vdev_t
*vd
)
9673 mutex_enter(&l2arc_dev_mtx
);
9674 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
9675 dev
= list_next(l2arc_dev_list
, dev
)) {
9676 if (dev
->l2ad_vdev
== vd
)
9679 mutex_exit(&l2arc_dev_mtx
);
9685 l2arc_rebuild_dev(l2arc_dev_t
*dev
, boolean_t reopen
)
9687 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
9688 uint64_t l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
9689 spa_t
*spa
= dev
->l2ad_spa
;
9692 * After a l2arc_remove_vdev(), the spa_t will no longer be valid
9698 * The L2ARC has to hold at least the payload of one log block for
9699 * them to be restored (persistent L2ARC). The payload of a log block
9700 * depends on the amount of its log entries. We always write log blocks
9701 * with 1022 entries. How many of them are committed or restored depends
9702 * on the size of the L2ARC device. Thus the maximum payload of
9703 * one log block is 1022 * SPA_MAXBLOCKSIZE = 16GB. If the L2ARC device
9704 * is less than that, we reduce the amount of committed and restored
9705 * log entries per block so as to enable persistence.
9707 if (dev
->l2ad_end
< l2arc_rebuild_blocks_min_l2size
) {
9708 dev
->l2ad_log_entries
= 0;
9710 dev
->l2ad_log_entries
= MIN((dev
->l2ad_end
-
9711 dev
->l2ad_start
) >> SPA_MAXBLOCKSHIFT
,
9712 L2ARC_LOG_BLK_MAX_ENTRIES
);
9716 * Read the device header, if an error is returned do not rebuild L2ARC.
9718 if (l2arc_dev_hdr_read(dev
) == 0 && dev
->l2ad_log_entries
> 0) {
9720 * If we are onlining a cache device (vdev_reopen) that was
9721 * still present (l2arc_vdev_present()) and rebuild is enabled,
9722 * we should evict all ARC buffers and pointers to log blocks
9723 * and reclaim their space before restoring its contents to
9727 if (!l2arc_rebuild_enabled
) {
9730 l2arc_evict(dev
, 0, B_TRUE
);
9731 /* start a new log block */
9732 dev
->l2ad_log_ent_idx
= 0;
9733 dev
->l2ad_log_blk_payload_asize
= 0;
9734 dev
->l2ad_log_blk_payload_start
= 0;
9738 * Just mark the device as pending for a rebuild. We won't
9739 * be starting a rebuild in line here as it would block pool
9740 * import. Instead spa_load_impl will hand that off to an
9741 * async task which will call l2arc_spa_rebuild_start.
9743 dev
->l2ad_rebuild
= B_TRUE
;
9744 } else if (spa_writeable(spa
)) {
9746 * In this case TRIM the whole device if l2arc_trim_ahead > 0,
9747 * otherwise create a new header. We zero out the memory holding
9748 * the header to reset dh_start_lbps. If we TRIM the whole
9749 * device the new header will be written by
9750 * vdev_trim_l2arc_thread() at the end of the TRIM to update the
9751 * trim_state in the header too. When reading the header, if
9752 * trim_state is not VDEV_TRIM_COMPLETE and l2arc_trim_ahead > 0
9753 * we opt to TRIM the whole device again.
9755 if (l2arc_trim_ahead
> 0) {
9756 dev
->l2ad_trim_all
= B_TRUE
;
9758 memset(l2dhdr
, 0, l2dhdr_asize
);
9759 l2arc_dev_hdr_update(dev
);
9765 * Add a vdev for use by the L2ARC. By this point the spa has already
9766 * validated the vdev and opened it.
9769 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
9771 l2arc_dev_t
*adddev
;
9772 uint64_t l2dhdr_asize
;
9774 ASSERT(!l2arc_vdev_present(vd
));
9777 * Create a new l2arc device entry.
9779 adddev
= vmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
9780 adddev
->l2ad_spa
= spa
;
9781 adddev
->l2ad_vdev
= vd
;
9782 /* leave extra size for an l2arc device header */
9783 l2dhdr_asize
= adddev
->l2ad_dev_hdr_asize
=
9784 MAX(sizeof (*adddev
->l2ad_dev_hdr
), 1 << vd
->vdev_ashift
);
9785 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
+ l2dhdr_asize
;
9786 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
9787 ASSERT3U(adddev
->l2ad_start
, <, adddev
->l2ad_end
);
9788 adddev
->l2ad_hand
= adddev
->l2ad_start
;
9789 adddev
->l2ad_evict
= adddev
->l2ad_start
;
9790 adddev
->l2ad_first
= B_TRUE
;
9791 adddev
->l2ad_writing
= B_FALSE
;
9792 adddev
->l2ad_trim_all
= B_FALSE
;
9793 list_link_init(&adddev
->l2ad_node
);
9794 adddev
->l2ad_dev_hdr
= kmem_zalloc(l2dhdr_asize
, KM_SLEEP
);
9796 mutex_init(&adddev
->l2ad_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9798 * This is a list of all ARC buffers that are still valid on the
9801 list_create(&adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
9802 offsetof(arc_buf_hdr_t
, b_l2hdr
.b_l2node
));
9805 * This is a list of pointers to log blocks that are still present
9808 list_create(&adddev
->l2ad_lbptr_list
, sizeof (l2arc_lb_ptr_buf_t
),
9809 offsetof(l2arc_lb_ptr_buf_t
, node
));
9811 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
9812 zfs_refcount_create(&adddev
->l2ad_alloc
);
9813 zfs_refcount_create(&adddev
->l2ad_lb_asize
);
9814 zfs_refcount_create(&adddev
->l2ad_lb_count
);
9817 * Decide if dev is eligible for L2ARC rebuild or whole device
9818 * trimming. This has to happen before the device is added in the
9819 * cache device list and l2arc_dev_mtx is released. Otherwise
9820 * l2arc_feed_thread() might already start writing on the
9823 l2arc_rebuild_dev(adddev
, B_FALSE
);
9826 * Add device to global list
9828 mutex_enter(&l2arc_dev_mtx
);
9829 list_insert_head(l2arc_dev_list
, adddev
);
9830 atomic_inc_64(&l2arc_ndev
);
9831 mutex_exit(&l2arc_dev_mtx
);
9835 * Decide if a vdev is eligible for L2ARC rebuild, called from vdev_reopen()
9836 * in case of onlining a cache device.
9839 l2arc_rebuild_vdev(vdev_t
*vd
, boolean_t reopen
)
9841 l2arc_dev_t
*dev
= NULL
;
9843 dev
= l2arc_vdev_get(vd
);
9844 ASSERT3P(dev
, !=, NULL
);
9847 * In contrast to l2arc_add_vdev() we do not have to worry about
9848 * l2arc_feed_thread() invalidating previous content when onlining a
9849 * cache device. The device parameters (l2ad*) are not cleared when
9850 * offlining the device and writing new buffers will not invalidate
9851 * all previous content. In worst case only buffers that have not had
9852 * their log block written to the device will be lost.
9853 * When onlining the cache device (ie offline->online without exporting
9854 * the pool in between) this happens:
9855 * vdev_reopen() -> vdev_open() -> l2arc_rebuild_vdev()
9857 * vdev_is_dead() = B_FALSE l2ad_rebuild = B_TRUE
9858 * During the time where vdev_is_dead = B_FALSE and until l2ad_rebuild
9859 * is set to B_TRUE we might write additional buffers to the device.
9861 l2arc_rebuild_dev(dev
, reopen
);
9865 l2arc_dev_t
*rva_l2arc_dev
;
9866 uint64_t rva_spa_gid
;
9867 uint64_t rva_vdev_gid
;
9868 boolean_t rva_async
;
9870 } remove_vdev_args_t
;
9873 l2arc_device_teardown(void *arg
)
9875 remove_vdev_args_t
*rva
= arg
;
9876 l2arc_dev_t
*remdev
= rva
->rva_l2arc_dev
;
9877 hrtime_t start_time
= gethrtime();
9880 * Clear all buflists and ARC references. L2ARC device flush.
9882 l2arc_evict(remdev
, 0, B_TRUE
);
9883 list_destroy(&remdev
->l2ad_buflist
);
9884 ASSERT(list_is_empty(&remdev
->l2ad_lbptr_list
));
9885 list_destroy(&remdev
->l2ad_lbptr_list
);
9886 mutex_destroy(&remdev
->l2ad_mtx
);
9887 zfs_refcount_destroy(&remdev
->l2ad_alloc
);
9888 zfs_refcount_destroy(&remdev
->l2ad_lb_asize
);
9889 zfs_refcount_destroy(&remdev
->l2ad_lb_count
);
9890 kmem_free(remdev
->l2ad_dev_hdr
, remdev
->l2ad_dev_hdr_asize
);
9891 vmem_free(remdev
, sizeof (l2arc_dev_t
));
9893 uint64_t elaspsed
= NSEC2MSEC(gethrtime() - start_time
);
9895 zfs_dbgmsg("spa %llu, vdev %llu removed in %llu ms",
9896 (u_longlong_t
)rva
->rva_spa_gid
,
9897 (u_longlong_t
)rva
->rva_vdev_gid
,
9898 (u_longlong_t
)elaspsed
);
9902 arc_async_flush_remove(rva
->rva_spa_gid
, 2);
9903 kmem_free(rva
, sizeof (remove_vdev_args_t
));
9907 * Remove a vdev from the L2ARC.
9910 l2arc_remove_vdev(vdev_t
*vd
)
9912 spa_t
*spa
= vd
->vdev_spa
;
9913 boolean_t asynchronous
= spa
->spa_state
== POOL_STATE_EXPORTED
||
9914 spa
->spa_state
== POOL_STATE_DESTROYED
;
9917 * Find the device by vdev
9919 l2arc_dev_t
*remdev
= l2arc_vdev_get(vd
);
9920 ASSERT3P(remdev
, !=, NULL
);
9923 * Save info for final teardown
9925 remove_vdev_args_t
*rva
= kmem_alloc(sizeof (remove_vdev_args_t
),
9927 rva
->rva_l2arc_dev
= remdev
;
9928 rva
->rva_spa_gid
= spa_load_guid(spa
);
9929 rva
->rva_vdev_gid
= remdev
->l2ad_vdev
->vdev_guid
;
9932 * Cancel any ongoing or scheduled rebuild.
9934 mutex_enter(&l2arc_rebuild_thr_lock
);
9935 remdev
->l2ad_rebuild_cancel
= B_TRUE
;
9936 if (remdev
->l2ad_rebuild_began
== B_TRUE
) {
9937 while (remdev
->l2ad_rebuild
== B_TRUE
)
9938 cv_wait(&l2arc_rebuild_thr_cv
, &l2arc_rebuild_thr_lock
);
9940 mutex_exit(&l2arc_rebuild_thr_lock
);
9941 rva
->rva_async
= asynchronous
;
9944 * Remove device from global list
9946 ASSERT(spa_config_held(spa
, SCL_L2ARC
, RW_WRITER
) & SCL_L2ARC
);
9947 mutex_enter(&l2arc_dev_mtx
);
9948 list_remove(l2arc_dev_list
, remdev
);
9949 l2arc_dev_last
= NULL
; /* may have been invalidated */
9950 atomic_dec_64(&l2arc_ndev
);
9952 /* During a pool export spa & vdev will no longer be valid */
9954 remdev
->l2ad_spa
= NULL
;
9955 remdev
->l2ad_vdev
= NULL
;
9957 mutex_exit(&l2arc_dev_mtx
);
9959 if (!asynchronous
) {
9960 l2arc_device_teardown(rva
);
9964 arc_async_flush_t
*af
= arc_async_flush_add(rva
->rva_spa_gid
, 2);
9966 taskq_dispatch_ent(arc_flush_taskq
, l2arc_device_teardown
, rva
,
9967 TQ_SLEEP
, &af
->af_tqent
);
9973 l2arc_thread_exit
= 0;
9976 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
9977 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
9978 mutex_init(&l2arc_rebuild_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
9979 cv_init(&l2arc_rebuild_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
9980 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9981 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
9983 l2arc_dev_list
= &L2ARC_dev_list
;
9984 l2arc_free_on_write
= &L2ARC_free_on_write
;
9985 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
9986 offsetof(l2arc_dev_t
, l2ad_node
));
9987 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
9988 offsetof(l2arc_data_free_t
, l2df_list_node
));
9994 mutex_destroy(&l2arc_feed_thr_lock
);
9995 cv_destroy(&l2arc_feed_thr_cv
);
9996 mutex_destroy(&l2arc_rebuild_thr_lock
);
9997 cv_destroy(&l2arc_rebuild_thr_cv
);
9998 mutex_destroy(&l2arc_dev_mtx
);
9999 mutex_destroy(&l2arc_free_on_write_mtx
);
10001 list_destroy(l2arc_dev_list
);
10002 list_destroy(l2arc_free_on_write
);
10008 if (!(spa_mode_global
& SPA_MODE_WRITE
))
10011 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
10012 TS_RUN
, defclsyspri
);
10018 if (!(spa_mode_global
& SPA_MODE_WRITE
))
10021 mutex_enter(&l2arc_feed_thr_lock
);
10022 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
10023 l2arc_thread_exit
= 1;
10024 while (l2arc_thread_exit
!= 0)
10025 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
10026 mutex_exit(&l2arc_feed_thr_lock
);
10030 * Punches out rebuild threads for the L2ARC devices in a spa. This should
10031 * be called after pool import from the spa async thread, since starting
10032 * these threads directly from spa_import() will make them part of the
10033 * "zpool import" context and delay process exit (and thus pool import).
10036 l2arc_spa_rebuild_start(spa_t
*spa
)
10038 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
10041 * Locate the spa's l2arc devices and kick off rebuild threads.
10043 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
10045 l2arc_vdev_get(spa
->spa_l2cache
.sav_vdevs
[i
]);
10047 /* Don't attempt a rebuild if the vdev is UNAVAIL */
10050 mutex_enter(&l2arc_rebuild_thr_lock
);
10051 if (dev
->l2ad_rebuild
&& !dev
->l2ad_rebuild_cancel
) {
10052 dev
->l2ad_rebuild_began
= B_TRUE
;
10053 (void) thread_create(NULL
, 0, l2arc_dev_rebuild_thread
,
10054 dev
, 0, &p0
, TS_RUN
, minclsyspri
);
10056 mutex_exit(&l2arc_rebuild_thr_lock
);
10061 l2arc_spa_rebuild_stop(spa_t
*spa
)
10063 ASSERT(MUTEX_HELD(&spa_namespace_lock
) ||
10064 spa
->spa_export_thread
== curthread
);
10066 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
10068 l2arc_vdev_get(spa
->spa_l2cache
.sav_vdevs
[i
]);
10071 mutex_enter(&l2arc_rebuild_thr_lock
);
10072 dev
->l2ad_rebuild_cancel
= B_TRUE
;
10073 mutex_exit(&l2arc_rebuild_thr_lock
);
10075 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
10077 l2arc_vdev_get(spa
->spa_l2cache
.sav_vdevs
[i
]);
10080 mutex_enter(&l2arc_rebuild_thr_lock
);
10081 if (dev
->l2ad_rebuild_began
== B_TRUE
) {
10082 while (dev
->l2ad_rebuild
== B_TRUE
) {
10083 cv_wait(&l2arc_rebuild_thr_cv
,
10084 &l2arc_rebuild_thr_lock
);
10087 mutex_exit(&l2arc_rebuild_thr_lock
);
10092 * Main entry point for L2ARC rebuilding.
10094 static __attribute__((noreturn
)) void
10095 l2arc_dev_rebuild_thread(void *arg
)
10097 l2arc_dev_t
*dev
= arg
;
10099 VERIFY(dev
->l2ad_rebuild
);
10100 (void) l2arc_rebuild(dev
);
10101 mutex_enter(&l2arc_rebuild_thr_lock
);
10102 dev
->l2ad_rebuild_began
= B_FALSE
;
10103 dev
->l2ad_rebuild
= B_FALSE
;
10104 cv_signal(&l2arc_rebuild_thr_cv
);
10105 mutex_exit(&l2arc_rebuild_thr_lock
);
10111 * This function implements the actual L2ARC metadata rebuild. It:
10112 * starts reading the log block chain and restores each block's contents
10113 * to memory (reconstructing arc_buf_hdr_t's).
10115 * Operation stops under any of the following conditions:
10117 * 1) We reach the end of the log block chain.
10118 * 2) We encounter *any* error condition (cksum errors, io errors)
10121 l2arc_rebuild(l2arc_dev_t
*dev
)
10123 vdev_t
*vd
= dev
->l2ad_vdev
;
10124 spa_t
*spa
= vd
->vdev_spa
;
10126 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10127 l2arc_log_blk_phys_t
*this_lb
, *next_lb
;
10128 zio_t
*this_io
= NULL
, *next_io
= NULL
;
10129 l2arc_log_blkptr_t lbps
[2];
10130 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
10131 boolean_t lock_held
;
10133 this_lb
= vmem_zalloc(sizeof (*this_lb
), KM_SLEEP
);
10134 next_lb
= vmem_zalloc(sizeof (*next_lb
), KM_SLEEP
);
10137 * We prevent device removal while issuing reads to the device,
10138 * then during the rebuilding phases we drop this lock again so
10139 * that a spa_unload or device remove can be initiated - this is
10140 * safe, because the spa will signal us to stop before removing
10141 * our device and wait for us to stop.
10143 spa_config_enter(spa
, SCL_L2ARC
, vd
, RW_READER
);
10144 lock_held
= B_TRUE
;
10147 * Retrieve the persistent L2ARC device state.
10148 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10150 dev
->l2ad_evict
= MAX(l2dhdr
->dh_evict
, dev
->l2ad_start
);
10151 dev
->l2ad_hand
= MAX(l2dhdr
->dh_start_lbps
[0].lbp_daddr
+
10152 L2BLK_GET_PSIZE((&l2dhdr
->dh_start_lbps
[0])->lbp_prop
),
10154 dev
->l2ad_first
= !!(l2dhdr
->dh_flags
& L2ARC_DEV_HDR_EVICT_FIRST
);
10156 vd
->vdev_trim_action_time
= l2dhdr
->dh_trim_action_time
;
10157 vd
->vdev_trim_state
= l2dhdr
->dh_trim_state
;
10160 * In case the zfs module parameter l2arc_rebuild_enabled is false
10161 * we do not start the rebuild process.
10163 if (!l2arc_rebuild_enabled
)
10166 /* Prepare the rebuild process */
10167 memcpy(lbps
, l2dhdr
->dh_start_lbps
, sizeof (lbps
));
10169 /* Start the rebuild process */
10171 if (!l2arc_log_blkptr_valid(dev
, &lbps
[0]))
10174 if ((err
= l2arc_log_blk_read(dev
, &lbps
[0], &lbps
[1],
10175 this_lb
, next_lb
, this_io
, &next_io
)) != 0)
10179 * Our memory pressure valve. If the system is running low
10180 * on memory, rather than swamping memory with new ARC buf
10181 * hdrs, we opt not to rebuild the L2ARC. At this point,
10182 * however, we have already set up our L2ARC dev to chain in
10183 * new metadata log blocks, so the user may choose to offline/
10184 * online the L2ARC dev at a later time (or re-import the pool)
10185 * to reconstruct it (when there's less memory pressure).
10187 if (l2arc_hdr_limit_reached()) {
10188 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_lowmem
);
10189 cmn_err(CE_NOTE
, "System running low on memory, "
10190 "aborting L2ARC rebuild.");
10191 err
= SET_ERROR(ENOMEM
);
10195 spa_config_exit(spa
, SCL_L2ARC
, vd
);
10196 lock_held
= B_FALSE
;
10199 * Now that we know that the next_lb checks out alright, we
10200 * can start reconstruction from this log block.
10201 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10203 uint64_t asize
= L2BLK_GET_PSIZE((&lbps
[0])->lbp_prop
);
10204 l2arc_log_blk_restore(dev
, this_lb
, asize
);
10207 * log block restored, include its pointer in the list of
10208 * pointers to log blocks present in the L2ARC device.
10210 lb_ptr_buf
= kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t
), KM_SLEEP
);
10211 lb_ptr_buf
->lb_ptr
= kmem_zalloc(sizeof (l2arc_log_blkptr_t
),
10213 memcpy(lb_ptr_buf
->lb_ptr
, &lbps
[0],
10214 sizeof (l2arc_log_blkptr_t
));
10215 mutex_enter(&dev
->l2ad_mtx
);
10216 list_insert_tail(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
10217 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, asize
);
10218 ARCSTAT_BUMP(arcstat_l2_log_blk_count
);
10219 zfs_refcount_add_many(&dev
->l2ad_lb_asize
, asize
, lb_ptr_buf
);
10220 zfs_refcount_add(&dev
->l2ad_lb_count
, lb_ptr_buf
);
10221 mutex_exit(&dev
->l2ad_mtx
);
10222 vdev_space_update(vd
, asize
, 0, 0);
10225 * Protection against loops of log blocks:
10227 * l2ad_hand l2ad_evict
10229 * l2ad_start |=======================================| l2ad_end
10230 * -----|||----|||---|||----|||
10232 * ---|||---|||----|||---|||
10235 * In this situation the pointer of log block (4) passes
10236 * l2arc_log_blkptr_valid() but the log block should not be
10237 * restored as it is overwritten by the payload of log block
10238 * (0). Only log blocks (0)-(3) should be restored. We check
10239 * whether l2ad_evict lies in between the payload starting
10240 * offset of the next log block (lbps[1].lbp_payload_start)
10241 * and the payload starting offset of the present log block
10242 * (lbps[0].lbp_payload_start). If true and this isn't the
10243 * first pass, we are looping from the beginning and we should
10246 if (l2arc_range_check_overlap(lbps
[1].lbp_payload_start
,
10247 lbps
[0].lbp_payload_start
, dev
->l2ad_evict
) &&
10251 kpreempt(KPREEMPT_SYNC
);
10253 mutex_enter(&l2arc_rebuild_thr_lock
);
10254 if (dev
->l2ad_rebuild_cancel
) {
10255 mutex_exit(&l2arc_rebuild_thr_lock
);
10256 err
= SET_ERROR(ECANCELED
);
10259 mutex_exit(&l2arc_rebuild_thr_lock
);
10260 if (spa_config_tryenter(spa
, SCL_L2ARC
, vd
,
10262 lock_held
= B_TRUE
;
10266 * L2ARC config lock held by somebody in writer,
10267 * possibly due to them trying to remove us. They'll
10268 * likely to want us to shut down, so after a little
10269 * delay, we check l2ad_rebuild_cancel and retry
10276 * Continue with the next log block.
10279 lbps
[1] = this_lb
->lb_prev_lbp
;
10280 PTR_SWAP(this_lb
, next_lb
);
10285 if (this_io
!= NULL
)
10286 l2arc_log_blk_fetch_abort(this_io
);
10288 if (next_io
!= NULL
)
10289 l2arc_log_blk_fetch_abort(next_io
);
10290 vmem_free(this_lb
, sizeof (*this_lb
));
10291 vmem_free(next_lb
, sizeof (*next_lb
));
10293 if (err
== ECANCELED
) {
10295 * In case the rebuild was canceled do not log to spa history
10296 * log as the pool may be in the process of being removed.
10298 zfs_dbgmsg("L2ARC rebuild aborted, restored %llu blocks",
10299 (u_longlong_t
)zfs_refcount_count(&dev
->l2ad_lb_count
));
10301 } else if (!l2arc_rebuild_enabled
) {
10302 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
10304 } else if (err
== 0 && zfs_refcount_count(&dev
->l2ad_lb_count
) > 0) {
10305 ARCSTAT_BUMP(arcstat_l2_rebuild_success
);
10306 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
10307 "successful, restored %llu blocks",
10308 (u_longlong_t
)zfs_refcount_count(&dev
->l2ad_lb_count
));
10309 } else if (err
== 0 && zfs_refcount_count(&dev
->l2ad_lb_count
) == 0) {
10311 * No error but also nothing restored, meaning the lbps array
10312 * in the device header points to invalid/non-present log
10313 * blocks. Reset the header.
10315 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
10316 "no valid log blocks");
10317 memset(l2dhdr
, 0, dev
->l2ad_dev_hdr_asize
);
10318 l2arc_dev_hdr_update(dev
);
10319 } else if (err
!= 0) {
10320 spa_history_log_internal(spa
, "L2ARC rebuild", NULL
,
10321 "aborted, restored %llu blocks",
10322 (u_longlong_t
)zfs_refcount_count(&dev
->l2ad_lb_count
));
10326 spa_config_exit(spa
, SCL_L2ARC
, vd
);
10332 * Attempts to read the device header on the provided L2ARC device and writes
10333 * it to `hdr'. On success, this function returns 0, otherwise the appropriate
10334 * error code is returned.
10337 l2arc_dev_hdr_read(l2arc_dev_t
*dev
)
10341 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10342 const uint64_t l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
10345 guid
= spa_guid(dev
->l2ad_vdev
->vdev_spa
);
10347 abd
= abd_get_from_buf(l2dhdr
, l2dhdr_asize
);
10349 err
= zio_wait(zio_read_phys(NULL
, dev
->l2ad_vdev
,
10350 VDEV_LABEL_START_SIZE
, l2dhdr_asize
, abd
,
10351 ZIO_CHECKSUM_LABEL
, NULL
, NULL
, ZIO_PRIORITY_SYNC_READ
,
10352 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
|
10353 ZIO_FLAG_SPECULATIVE
, B_FALSE
));
10358 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_dh_errors
);
10359 zfs_dbgmsg("L2ARC IO error (%d) while reading device header, "
10360 "vdev guid: %llu", err
,
10361 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
);
10365 if (l2dhdr
->dh_magic
== BSWAP_64(L2ARC_DEV_HDR_MAGIC
))
10366 byteswap_uint64_array(l2dhdr
, sizeof (*l2dhdr
));
10368 if (l2dhdr
->dh_magic
!= L2ARC_DEV_HDR_MAGIC
||
10369 l2dhdr
->dh_spa_guid
!= guid
||
10370 l2dhdr
->dh_vdev_guid
!= dev
->l2ad_vdev
->vdev_guid
||
10371 l2dhdr
->dh_version
!= L2ARC_PERSISTENT_VERSION
||
10372 l2dhdr
->dh_log_entries
!= dev
->l2ad_log_entries
||
10373 l2dhdr
->dh_end
!= dev
->l2ad_end
||
10374 !l2arc_range_check_overlap(dev
->l2ad_start
, dev
->l2ad_end
,
10375 l2dhdr
->dh_evict
) ||
10376 (l2dhdr
->dh_trim_state
!= VDEV_TRIM_COMPLETE
&&
10377 l2arc_trim_ahead
> 0)) {
10379 * Attempt to rebuild a device containing no actual dev hdr
10380 * or containing a header from some other pool or from another
10381 * version of persistent L2ARC.
10383 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_unsupported
);
10384 return (SET_ERROR(ENOTSUP
));
10391 * Reads L2ARC log blocks from storage and validates their contents.
10393 * This function implements a simple fetcher to make sure that while
10394 * we're processing one buffer the L2ARC is already fetching the next
10395 * one in the chain.
10397 * The arguments this_lp and next_lp point to the current and next log block
10398 * address in the block chain. Similarly, this_lb and next_lb hold the
10399 * l2arc_log_blk_phys_t's of the current and next L2ARC blk.
10401 * The `this_io' and `next_io' arguments are used for block fetching.
10402 * When issuing the first blk IO during rebuild, you should pass NULL for
10403 * `this_io'. This function will then issue a sync IO to read the block and
10404 * also issue an async IO to fetch the next block in the block chain. The
10405 * fetched IO is returned in `next_io'. On subsequent calls to this
10406 * function, pass the value returned in `next_io' from the previous call
10407 * as `this_io' and a fresh `next_io' pointer to hold the next fetch IO.
10408 * Prior to the call, you should initialize your `next_io' pointer to be
10409 * NULL. If no fetch IO was issued, the pointer is left set at NULL.
10411 * On success, this function returns 0, otherwise it returns an appropriate
10412 * error code. On error the fetching IO is aborted and cleared before
10413 * returning from this function. Therefore, if we return `success', the
10414 * caller can assume that we have taken care of cleanup of fetch IOs.
10417 l2arc_log_blk_read(l2arc_dev_t
*dev
,
10418 const l2arc_log_blkptr_t
*this_lbp
, const l2arc_log_blkptr_t
*next_lbp
,
10419 l2arc_log_blk_phys_t
*this_lb
, l2arc_log_blk_phys_t
*next_lb
,
10420 zio_t
*this_io
, zio_t
**next_io
)
10426 ASSERT(this_lbp
!= NULL
&& next_lbp
!= NULL
);
10427 ASSERT(this_lb
!= NULL
&& next_lb
!= NULL
);
10428 ASSERT(next_io
!= NULL
&& *next_io
== NULL
);
10429 ASSERT(l2arc_log_blkptr_valid(dev
, this_lbp
));
10432 * Check to see if we have issued the IO for this log block in a
10433 * previous run. If not, this is the first call, so issue it now.
10435 if (this_io
== NULL
) {
10436 this_io
= l2arc_log_blk_fetch(dev
->l2ad_vdev
, this_lbp
,
10441 * Peek to see if we can start issuing the next IO immediately.
10443 if (l2arc_log_blkptr_valid(dev
, next_lbp
)) {
10445 * Start issuing IO for the next log block early - this
10446 * should help keep the L2ARC device busy while we
10447 * decompress and restore this log block.
10449 *next_io
= l2arc_log_blk_fetch(dev
->l2ad_vdev
, next_lbp
,
10453 /* Wait for the IO to read this log block to complete */
10454 if ((err
= zio_wait(this_io
)) != 0) {
10455 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_io_errors
);
10456 zfs_dbgmsg("L2ARC IO error (%d) while reading log block, "
10457 "offset: %llu, vdev guid: %llu", err
,
10458 (u_longlong_t
)this_lbp
->lbp_daddr
,
10459 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
);
10464 * Make sure the buffer checks out.
10465 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10467 asize
= L2BLK_GET_PSIZE((this_lbp
)->lbp_prop
);
10468 fletcher_4_native(this_lb
, asize
, NULL
, &cksum
);
10469 if (!ZIO_CHECKSUM_EQUAL(cksum
, this_lbp
->lbp_cksum
)) {
10470 ARCSTAT_BUMP(arcstat_l2_rebuild_abort_cksum_lb_errors
);
10471 zfs_dbgmsg("L2ARC log block cksum failed, offset: %llu, "
10472 "vdev guid: %llu, l2ad_hand: %llu, l2ad_evict: %llu",
10473 (u_longlong_t
)this_lbp
->lbp_daddr
,
10474 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
,
10475 (u_longlong_t
)dev
->l2ad_hand
,
10476 (u_longlong_t
)dev
->l2ad_evict
);
10477 err
= SET_ERROR(ECKSUM
);
10481 /* Now we can take our time decoding this buffer */
10482 switch (L2BLK_GET_COMPRESS((this_lbp
)->lbp_prop
)) {
10483 case ZIO_COMPRESS_OFF
:
10485 case ZIO_COMPRESS_LZ4
: {
10486 abd_t
*abd
= abd_alloc_linear(asize
, B_TRUE
);
10487 abd_copy_from_buf_off(abd
, this_lb
, 0, asize
);
10489 abd_get_from_buf_struct(&dabd
, this_lb
, sizeof (*this_lb
));
10490 err
= zio_decompress_data(
10491 L2BLK_GET_COMPRESS((this_lbp
)->lbp_prop
),
10492 abd
, &dabd
, asize
, sizeof (*this_lb
), NULL
);
10496 err
= SET_ERROR(EINVAL
);
10502 err
= SET_ERROR(EINVAL
);
10505 if (this_lb
->lb_magic
== BSWAP_64(L2ARC_LOG_BLK_MAGIC
))
10506 byteswap_uint64_array(this_lb
, sizeof (*this_lb
));
10507 if (this_lb
->lb_magic
!= L2ARC_LOG_BLK_MAGIC
) {
10508 err
= SET_ERROR(EINVAL
);
10512 /* Abort an in-flight fetch I/O in case of error */
10513 if (err
!= 0 && *next_io
!= NULL
) {
10514 l2arc_log_blk_fetch_abort(*next_io
);
10521 * Restores the payload of a log block to ARC. This creates empty ARC hdr
10522 * entries which only contain an l2arc hdr, essentially restoring the
10523 * buffers to their L2ARC evicted state. This function also updates space
10524 * usage on the L2ARC vdev to make sure it tracks restored buffers.
10527 l2arc_log_blk_restore(l2arc_dev_t
*dev
, const l2arc_log_blk_phys_t
*lb
,
10530 uint64_t size
= 0, asize
= 0;
10531 uint64_t log_entries
= dev
->l2ad_log_entries
;
10534 * Usually arc_adapt() is called only for data, not headers, but
10535 * since we may allocate significant amount of memory here, let ARC
10538 arc_adapt(log_entries
* HDR_L2ONLY_SIZE
);
10540 for (int i
= log_entries
- 1; i
>= 0; i
--) {
10542 * Restore goes in the reverse temporal direction to preserve
10543 * correct temporal ordering of buffers in the l2ad_buflist.
10544 * l2arc_hdr_restore also does a list_insert_tail instead of
10545 * list_insert_head on the l2ad_buflist:
10547 * LIST l2ad_buflist LIST
10548 * HEAD <------ (time) ------ TAIL
10549 * direction +-----+-----+-----+-----+-----+ direction
10550 * of l2arc <== | buf | buf | buf | buf | buf | ===> of rebuild
10551 * fill +-----+-----+-----+-----+-----+
10555 * l2arc_feed_thread l2arc_rebuild
10556 * will place new bufs here restores bufs here
10558 * During l2arc_rebuild() the device is not used by
10559 * l2arc_feed_thread() as dev->l2ad_rebuild is set to true.
10561 size
+= L2BLK_GET_LSIZE((&lb
->lb_entries
[i
])->le_prop
);
10562 asize
+= vdev_psize_to_asize(dev
->l2ad_vdev
,
10563 L2BLK_GET_PSIZE((&lb
->lb_entries
[i
])->le_prop
));
10564 l2arc_hdr_restore(&lb
->lb_entries
[i
], dev
);
10568 * Record rebuild stats:
10569 * size Logical size of restored buffers in the L2ARC
10570 * asize Aligned size of restored buffers in the L2ARC
10572 ARCSTAT_INCR(arcstat_l2_rebuild_size
, size
);
10573 ARCSTAT_INCR(arcstat_l2_rebuild_asize
, asize
);
10574 ARCSTAT_INCR(arcstat_l2_rebuild_bufs
, log_entries
);
10575 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize
, lb_asize
);
10576 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio
, asize
/ lb_asize
);
10577 ARCSTAT_BUMP(arcstat_l2_rebuild_log_blks
);
10581 * Restores a single ARC buf hdr from a log entry. The ARC buffer is put
10582 * into a state indicating that it has been evicted to L2ARC.
10585 l2arc_hdr_restore(const l2arc_log_ent_phys_t
*le
, l2arc_dev_t
*dev
)
10587 arc_buf_hdr_t
*hdr
, *exists
;
10588 kmutex_t
*hash_lock
;
10589 arc_buf_contents_t type
= L2BLK_GET_TYPE((le
)->le_prop
);
10590 uint64_t asize
= vdev_psize_to_asize(dev
->l2ad_vdev
,
10591 L2BLK_GET_PSIZE((le
)->le_prop
));
10594 * Do all the allocation before grabbing any locks, this lets us
10595 * sleep if memory is full and we don't have to deal with failed
10598 hdr
= arc_buf_alloc_l2only(L2BLK_GET_LSIZE((le
)->le_prop
), type
,
10599 dev
, le
->le_dva
, le
->le_daddr
,
10600 L2BLK_GET_PSIZE((le
)->le_prop
), asize
, le
->le_birth
,
10601 L2BLK_GET_COMPRESS((le
)->le_prop
), le
->le_complevel
,
10602 L2BLK_GET_PROTECTED((le
)->le_prop
),
10603 L2BLK_GET_PREFETCH((le
)->le_prop
),
10604 L2BLK_GET_STATE((le
)->le_prop
));
10607 * vdev_space_update() has to be called before arc_hdr_destroy() to
10608 * avoid underflow since the latter also calls vdev_space_update().
10610 l2arc_hdr_arcstats_increment(hdr
);
10611 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10613 mutex_enter(&dev
->l2ad_mtx
);
10614 list_insert_tail(&dev
->l2ad_buflist
, hdr
);
10615 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
), hdr
);
10616 mutex_exit(&dev
->l2ad_mtx
);
10618 exists
= buf_hash_insert(hdr
, &hash_lock
);
10620 /* Buffer was already cached, no need to restore it. */
10621 arc_hdr_destroy(hdr
);
10623 * If the buffer is already cached, check whether it has
10624 * L2ARC metadata. If not, enter them and update the flag.
10625 * This is important is case of onlining a cache device, since
10626 * we previously evicted all L2ARC metadata from ARC.
10628 if (!HDR_HAS_L2HDR(exists
)) {
10629 arc_hdr_set_flags(exists
, ARC_FLAG_HAS_L2HDR
);
10630 exists
->b_l2hdr
.b_dev
= dev
;
10631 exists
->b_l2hdr
.b_daddr
= le
->le_daddr
;
10632 exists
->b_l2hdr
.b_arcs_state
=
10633 L2BLK_GET_STATE((le
)->le_prop
);
10634 /* l2arc_hdr_arcstats_update() expects a valid asize */
10635 HDR_SET_L2SIZE(exists
, asize
);
10636 mutex_enter(&dev
->l2ad_mtx
);
10637 list_insert_tail(&dev
->l2ad_buflist
, exists
);
10638 (void) zfs_refcount_add_many(&dev
->l2ad_alloc
,
10639 arc_hdr_size(exists
), exists
);
10640 mutex_exit(&dev
->l2ad_mtx
);
10641 l2arc_hdr_arcstats_increment(exists
);
10642 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10644 ARCSTAT_BUMP(arcstat_l2_rebuild_bufs_precached
);
10647 mutex_exit(hash_lock
);
10651 * Starts an asynchronous read IO to read a log block. This is used in log
10652 * block reconstruction to start reading the next block before we are done
10653 * decoding and reconstructing the current block, to keep the l2arc device
10654 * nice and hot with read IO to process.
10655 * The returned zio will contain a newly allocated memory buffers for the IO
10656 * data which should then be freed by the caller once the zio is no longer
10657 * needed (i.e. due to it having completed). If you wish to abort this
10658 * zio, you should do so using l2arc_log_blk_fetch_abort, which takes
10659 * care of disposing of the allocated buffers correctly.
10662 l2arc_log_blk_fetch(vdev_t
*vd
, const l2arc_log_blkptr_t
*lbp
,
10663 l2arc_log_blk_phys_t
*lb
)
10667 l2arc_read_callback_t
*cb
;
10669 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
10670 asize
= L2BLK_GET_PSIZE((lbp
)->lbp_prop
);
10671 ASSERT(asize
<= sizeof (l2arc_log_blk_phys_t
));
10673 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
), KM_SLEEP
);
10674 cb
->l2rcb_abd
= abd_get_from_buf(lb
, asize
);
10675 pio
= zio_root(vd
->vdev_spa
, l2arc_blk_fetch_done
, cb
,
10676 ZIO_FLAG_CANFAIL
| ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
);
10677 (void) zio_nowait(zio_read_phys(pio
, vd
, lbp
->lbp_daddr
, asize
,
10678 cb
->l2rcb_abd
, ZIO_CHECKSUM_OFF
, NULL
, NULL
,
10679 ZIO_PRIORITY_ASYNC_READ
, ZIO_FLAG_CANFAIL
|
10680 ZIO_FLAG_DONT_PROPAGATE
| ZIO_FLAG_DONT_RETRY
, B_FALSE
));
10686 * Aborts a zio returned from l2arc_log_blk_fetch and frees the data
10687 * buffers allocated for it.
10690 l2arc_log_blk_fetch_abort(zio_t
*zio
)
10692 (void) zio_wait(zio
);
10696 * Creates a zio to update the device header on an l2arc device.
10699 l2arc_dev_hdr_update(l2arc_dev_t
*dev
)
10701 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10702 const uint64_t l2dhdr_asize
= dev
->l2ad_dev_hdr_asize
;
10706 VERIFY(spa_config_held(dev
->l2ad_spa
, SCL_STATE_ALL
, RW_READER
));
10708 l2dhdr
->dh_magic
= L2ARC_DEV_HDR_MAGIC
;
10709 l2dhdr
->dh_version
= L2ARC_PERSISTENT_VERSION
;
10710 l2dhdr
->dh_spa_guid
= spa_guid(dev
->l2ad_vdev
->vdev_spa
);
10711 l2dhdr
->dh_vdev_guid
= dev
->l2ad_vdev
->vdev_guid
;
10712 l2dhdr
->dh_log_entries
= dev
->l2ad_log_entries
;
10713 l2dhdr
->dh_evict
= dev
->l2ad_evict
;
10714 l2dhdr
->dh_start
= dev
->l2ad_start
;
10715 l2dhdr
->dh_end
= dev
->l2ad_end
;
10716 l2dhdr
->dh_lb_asize
= zfs_refcount_count(&dev
->l2ad_lb_asize
);
10717 l2dhdr
->dh_lb_count
= zfs_refcount_count(&dev
->l2ad_lb_count
);
10718 l2dhdr
->dh_flags
= 0;
10719 l2dhdr
->dh_trim_action_time
= dev
->l2ad_vdev
->vdev_trim_action_time
;
10720 l2dhdr
->dh_trim_state
= dev
->l2ad_vdev
->vdev_trim_state
;
10721 if (dev
->l2ad_first
)
10722 l2dhdr
->dh_flags
|= L2ARC_DEV_HDR_EVICT_FIRST
;
10724 abd
= abd_get_from_buf(l2dhdr
, l2dhdr_asize
);
10726 err
= zio_wait(zio_write_phys(NULL
, dev
->l2ad_vdev
,
10727 VDEV_LABEL_START_SIZE
, l2dhdr_asize
, abd
, ZIO_CHECKSUM_LABEL
, NULL
,
10728 NULL
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_CANFAIL
, B_FALSE
));
10733 zfs_dbgmsg("L2ARC IO error (%d) while writing device header, "
10734 "vdev guid: %llu", err
,
10735 (u_longlong_t
)dev
->l2ad_vdev
->vdev_guid
);
10740 * Commits a log block to the L2ARC device. This routine is invoked from
10741 * l2arc_write_buffers when the log block fills up.
10742 * This function allocates some memory to temporarily hold the serialized
10743 * buffer to be written. This is then released in l2arc_write_done.
10746 l2arc_log_blk_commit(l2arc_dev_t
*dev
, zio_t
*pio
, l2arc_write_callback_t
*cb
)
10748 l2arc_log_blk_phys_t
*lb
= &dev
->l2ad_log_blk
;
10749 l2arc_dev_hdr_phys_t
*l2dhdr
= dev
->l2ad_dev_hdr
;
10750 uint64_t psize
, asize
;
10752 l2arc_lb_abd_buf_t
*abd_buf
;
10754 l2arc_lb_ptr_buf_t
*lb_ptr_buf
;
10756 VERIFY3S(dev
->l2ad_log_ent_idx
, ==, dev
->l2ad_log_entries
);
10758 abd_buf
= zio_buf_alloc(sizeof (*abd_buf
));
10759 abd_buf
->abd
= abd_get_from_buf(lb
, sizeof (*lb
));
10760 lb_ptr_buf
= kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t
), KM_SLEEP
);
10761 lb_ptr_buf
->lb_ptr
= kmem_zalloc(sizeof (l2arc_log_blkptr_t
), KM_SLEEP
);
10763 /* link the buffer into the block chain */
10764 lb
->lb_prev_lbp
= l2dhdr
->dh_start_lbps
[1];
10765 lb
->lb_magic
= L2ARC_LOG_BLK_MAGIC
;
10768 * l2arc_log_blk_commit() may be called multiple times during a single
10769 * l2arc_write_buffers() call. Save the allocated abd buffers in a list
10770 * so we can free them in l2arc_write_done() later on.
10772 list_insert_tail(&cb
->l2wcb_abd_list
, abd_buf
);
10774 /* try to compress the buffer, at least one sector to save */
10775 psize
= zio_compress_data(ZIO_COMPRESS_LZ4
,
10776 abd_buf
->abd
, &abd
, sizeof (*lb
),
10777 zio_get_compression_max_size(ZIO_COMPRESS_LZ4
,
10778 dev
->l2ad_vdev
->vdev_ashift
,
10779 dev
->l2ad_vdev
->vdev_ashift
, sizeof (*lb
)), 0);
10781 /* a log block is never entirely zero */
10782 ASSERT(psize
!= 0);
10783 asize
= vdev_psize_to_asize(dev
->l2ad_vdev
, psize
);
10784 ASSERT(asize
<= sizeof (*lb
));
10787 * Update the start log block pointer in the device header to point
10788 * to the log block we're about to write.
10790 l2dhdr
->dh_start_lbps
[1] = l2dhdr
->dh_start_lbps
[0];
10791 l2dhdr
->dh_start_lbps
[0].lbp_daddr
= dev
->l2ad_hand
;
10792 l2dhdr
->dh_start_lbps
[0].lbp_payload_asize
=
10793 dev
->l2ad_log_blk_payload_asize
;
10794 l2dhdr
->dh_start_lbps
[0].lbp_payload_start
=
10795 dev
->l2ad_log_blk_payload_start
;
10797 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
, sizeof (*lb
));
10799 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
, asize
);
10800 L2BLK_SET_CHECKSUM(
10801 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10802 ZIO_CHECKSUM_FLETCHER_4
);
10803 if (asize
< sizeof (*lb
)) {
10804 /* compression succeeded */
10805 abd_zero_off(abd
, psize
, asize
- psize
);
10806 L2BLK_SET_COMPRESS(
10807 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10810 /* compression failed */
10811 abd_copy_from_buf_off(abd
, lb
, 0, sizeof (*lb
));
10812 L2BLK_SET_COMPRESS(
10813 (&l2dhdr
->dh_start_lbps
[0])->lbp_prop
,
10817 /* checksum what we're about to write */
10818 abd_fletcher_4_native(abd
, asize
, NULL
,
10819 &l2dhdr
->dh_start_lbps
[0].lbp_cksum
);
10821 abd_free(abd_buf
->abd
);
10823 /* perform the write itself */
10824 abd_buf
->abd
= abd
;
10825 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
, dev
->l2ad_hand
,
10826 asize
, abd_buf
->abd
, ZIO_CHECKSUM_OFF
, NULL
, NULL
,
10827 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_CANFAIL
, B_FALSE
);
10828 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
, zio_t
*, wzio
);
10829 (void) zio_nowait(wzio
);
10831 dev
->l2ad_hand
+= asize
;
10832 vdev_space_update(dev
->l2ad_vdev
, asize
, 0, 0);
10835 * Include the committed log block's pointer in the list of pointers
10836 * to log blocks present in the L2ARC device.
10838 memcpy(lb_ptr_buf
->lb_ptr
, &l2dhdr
->dh_start_lbps
[0],
10839 sizeof (l2arc_log_blkptr_t
));
10840 mutex_enter(&dev
->l2ad_mtx
);
10841 list_insert_head(&dev
->l2ad_lbptr_list
, lb_ptr_buf
);
10842 ARCSTAT_INCR(arcstat_l2_log_blk_asize
, asize
);
10843 ARCSTAT_BUMP(arcstat_l2_log_blk_count
);
10844 zfs_refcount_add_many(&dev
->l2ad_lb_asize
, asize
, lb_ptr_buf
);
10845 zfs_refcount_add(&dev
->l2ad_lb_count
, lb_ptr_buf
);
10846 mutex_exit(&dev
->l2ad_mtx
);
10848 /* bump the kstats */
10849 ARCSTAT_INCR(arcstat_l2_write_bytes
, asize
);
10850 ARCSTAT_BUMP(arcstat_l2_log_blk_writes
);
10851 ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize
, asize
);
10852 ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio
,
10853 dev
->l2ad_log_blk_payload_asize
/ asize
);
10855 /* start a new log block */
10856 dev
->l2ad_log_ent_idx
= 0;
10857 dev
->l2ad_log_blk_payload_asize
= 0;
10858 dev
->l2ad_log_blk_payload_start
= 0;
10864 * Validates an L2ARC log block address to make sure that it can be read
10865 * from the provided L2ARC device.
10868 l2arc_log_blkptr_valid(l2arc_dev_t
*dev
, const l2arc_log_blkptr_t
*lbp
)
10870 /* L2BLK_GET_PSIZE returns aligned size for log blocks */
10871 uint64_t asize
= L2BLK_GET_PSIZE((lbp
)->lbp_prop
);
10872 uint64_t end
= lbp
->lbp_daddr
+ asize
- 1;
10873 uint64_t start
= lbp
->lbp_payload_start
;
10874 boolean_t evicted
= B_FALSE
;
10877 * A log block is valid if all of the following conditions are true:
10878 * - it fits entirely (including its payload) between l2ad_start and
10880 * - it has a valid size
10881 * - neither the log block itself nor part of its payload was evicted
10882 * by l2arc_evict():
10884 * l2ad_hand l2ad_evict
10889 * l2ad_start ============================================ l2ad_end
10890 * --------------------------||||
10897 l2arc_range_check_overlap(start
, end
, dev
->l2ad_hand
) ||
10898 l2arc_range_check_overlap(start
, end
, dev
->l2ad_evict
) ||
10899 l2arc_range_check_overlap(dev
->l2ad_hand
, dev
->l2ad_evict
, start
) ||
10900 l2arc_range_check_overlap(dev
->l2ad_hand
, dev
->l2ad_evict
, end
);
10902 return (start
>= dev
->l2ad_start
&& end
<= dev
->l2ad_end
&&
10903 asize
> 0 && asize
<= sizeof (l2arc_log_blk_phys_t
) &&
10904 (!evicted
|| dev
->l2ad_first
));
10908 * Inserts ARC buffer header `hdr' into the current L2ARC log block on
10909 * the device. The buffer being inserted must be present in L2ARC.
10910 * Returns B_TRUE if the L2ARC log block is full and needs to be committed
10911 * to L2ARC, or B_FALSE if it still has room for more ARC buffers.
10914 l2arc_log_blk_insert(l2arc_dev_t
*dev
, const arc_buf_hdr_t
*hdr
)
10916 l2arc_log_blk_phys_t
*lb
= &dev
->l2ad_log_blk
;
10917 l2arc_log_ent_phys_t
*le
;
10919 if (dev
->l2ad_log_entries
== 0)
10922 int index
= dev
->l2ad_log_ent_idx
++;
10924 ASSERT3S(index
, <, dev
->l2ad_log_entries
);
10925 ASSERT(HDR_HAS_L2HDR(hdr
));
10927 le
= &lb
->lb_entries
[index
];
10928 memset(le
, 0, sizeof (*le
));
10929 le
->le_dva
= hdr
->b_dva
;
10930 le
->le_birth
= hdr
->b_birth
;
10931 le
->le_daddr
= hdr
->b_l2hdr
.b_daddr
;
10933 dev
->l2ad_log_blk_payload_start
= le
->le_daddr
;
10934 L2BLK_SET_LSIZE((le
)->le_prop
, HDR_GET_LSIZE(hdr
));
10935 L2BLK_SET_PSIZE((le
)->le_prop
, HDR_GET_PSIZE(hdr
));
10936 L2BLK_SET_COMPRESS((le
)->le_prop
, HDR_GET_COMPRESS(hdr
));
10937 le
->le_complevel
= hdr
->b_complevel
;
10938 L2BLK_SET_TYPE((le
)->le_prop
, hdr
->b_type
);
10939 L2BLK_SET_PROTECTED((le
)->le_prop
, !!(HDR_PROTECTED(hdr
)));
10940 L2BLK_SET_PREFETCH((le
)->le_prop
, !!(HDR_PREFETCH(hdr
)));
10941 L2BLK_SET_STATE((le
)->le_prop
, hdr
->b_l2hdr
.b_arcs_state
);
10943 dev
->l2ad_log_blk_payload_asize
+= vdev_psize_to_asize(dev
->l2ad_vdev
,
10944 HDR_GET_PSIZE(hdr
));
10946 return (dev
->l2ad_log_ent_idx
== dev
->l2ad_log_entries
);
10950 * Checks whether a given L2ARC device address sits in a time-sequential
10951 * range. The trick here is that the L2ARC is a rotary buffer, so we can't
10952 * just do a range comparison, we need to handle the situation in which the
10953 * range wraps around the end of the L2ARC device. Arguments:
10954 * bottom -- Lower end of the range to check (written to earlier).
10955 * top -- Upper end of the range to check (written to later).
10956 * check -- The address for which we want to determine if it sits in
10957 * between the top and bottom.
10959 * The 3-way conditional below represents the following cases:
10961 * bottom < top : Sequentially ordered case:
10962 * <check>--------+-------------------+
10963 * | (overlap here?) |
10965 * |---------------<bottom>============<top>--------------|
10967 * bottom > top: Looped-around case:
10968 * <check>--------+------------------+
10969 * | (overlap here?) |
10971 * |===============<top>---------------<bottom>===========|
10974 * +---------------+---------<check>
10976 * top == bottom : Just a single address comparison.
10979 l2arc_range_check_overlap(uint64_t bottom
, uint64_t top
, uint64_t check
)
10982 return (bottom
<= check
&& check
<= top
);
10983 else if (bottom
> top
)
10984 return (check
<= top
|| bottom
<= check
);
10986 return (check
== top
);
10989 EXPORT_SYMBOL(arc_buf_size
);
10990 EXPORT_SYMBOL(arc_write
);
10991 EXPORT_SYMBOL(arc_read
);
10992 EXPORT_SYMBOL(arc_buf_info
);
10993 EXPORT_SYMBOL(arc_getbuf_func
);
10994 EXPORT_SYMBOL(arc_add_prune_callback
);
10995 EXPORT_SYMBOL(arc_remove_prune_callback
);
10997 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min
, param_set_arc_min
,
10998 spl_param_get_u64
, ZMOD_RW
, "Minimum ARC size in bytes");
11000 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, max
, param_set_arc_max
,
11001 spl_param_get_u64
, ZMOD_RW
, "Maximum ARC size in bytes");
11003 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, meta_balance
, UINT
, ZMOD_RW
,
11004 "Balance between metadata and data on ghost hits.");
11006 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, grow_retry
, param_set_arc_int
,
11007 param_get_uint
, ZMOD_RW
, "Seconds before growing ARC size");
11009 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, shrink_shift
, param_set_arc_int
,
11010 param_get_uint
, ZMOD_RW
, "log2(fraction of ARC to reclaim)");
11012 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, pc_percent
, UINT
, ZMOD_RW
,
11013 "Percent of pagecache to reclaim ARC to");
11015 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, average_blocksize
, UINT
, ZMOD_RD
,
11016 "Target average block size");
11018 ZFS_MODULE_PARAM(zfs
, zfs_
, compressed_arc_enabled
, INT
, ZMOD_RW
,
11019 "Disable compressed ARC buffers");
11021 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min_prefetch_ms
, param_set_arc_int
,
11022 param_get_uint
, ZMOD_RW
, "Min life of prefetch block in ms");
11024 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, min_prescient_prefetch_ms
,
11025 param_set_arc_int
, param_get_uint
, ZMOD_RW
,
11026 "Min life of prescient prefetched block in ms");
11028 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, write_max
, U64
, ZMOD_RW
,
11029 "Max write bytes per interval");
11031 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, write_boost
, U64
, ZMOD_RW
,
11032 "Extra write bytes during device warmup");
11034 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, headroom
, U64
, ZMOD_RW
,
11035 "Number of max device writes to precache");
11037 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, headroom_boost
, U64
, ZMOD_RW
,
11038 "Compressed l2arc_headroom multiplier");
11040 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, trim_ahead
, U64
, ZMOD_RW
,
11041 "TRIM ahead L2ARC write size multiplier");
11043 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_secs
, U64
, ZMOD_RW
,
11044 "Seconds between L2ARC writing");
11046 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_min_ms
, U64
, ZMOD_RW
,
11047 "Min feed interval in milliseconds");
11049 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, noprefetch
, INT
, ZMOD_RW
,
11050 "Skip caching prefetched buffers");
11052 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, feed_again
, INT
, ZMOD_RW
,
11053 "Turbo L2ARC warmup");
11055 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, norw
, INT
, ZMOD_RW
,
11056 "No reads during writes");
11058 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, meta_percent
, UINT
, ZMOD_RW
,
11059 "Percent of ARC size allowed for L2ARC-only headers");
11061 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, rebuild_enabled
, INT
, ZMOD_RW
,
11062 "Rebuild the L2ARC when importing a pool");
11064 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, rebuild_blocks_min_l2size
, U64
, ZMOD_RW
,
11065 "Min size in bytes to write rebuild log blocks in L2ARC");
11067 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, mfuonly
, INT
, ZMOD_RW
,
11068 "Cache only MFU data from ARC into L2ARC");
11070 ZFS_MODULE_PARAM(zfs_l2arc
, l2arc_
, exclude_special
, INT
, ZMOD_RW
,
11071 "Exclude dbufs on special vdevs from being cached to L2ARC if set.");
11073 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, lotsfree_percent
, param_set_arc_int
,
11074 param_get_uint
, ZMOD_RW
, "System free memory I/O throttle in bytes");
11076 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, sys_free
, param_set_arc_u64
,
11077 spl_param_get_u64
, ZMOD_RW
, "System free memory target size in bytes");
11079 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, dnode_limit
, param_set_arc_u64
,
11080 spl_param_get_u64
, ZMOD_RW
, "Minimum bytes of dnodes in ARC");
11082 ZFS_MODULE_PARAM_CALL(zfs_arc
, zfs_arc_
, dnode_limit_percent
,
11083 param_set_arc_int
, param_get_uint
, ZMOD_RW
,
11084 "Percent of ARC meta buffers for dnodes");
11086 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, dnode_reduce_percent
, UINT
, ZMOD_RW
,
11087 "Percentage of excess dnodes to try to unpin");
11089 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, eviction_pct
, UINT
, ZMOD_RW
,
11090 "When full, ARC allocation waits for eviction of this % of alloc size");
11092 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, evict_batch_limit
, UINT
, ZMOD_RW
,
11093 "The number of headers to evict per sublist before moving to the next");
11095 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, prune_task_threads
, INT
, ZMOD_RW
,
11096 "Number of arc_prune threads");