4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright (c) 2014 by Saso Kiselkov. All rights reserved.
26 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
30 * DVA-based Adjustable Replacement Cache
32 * While much of the theory of operation used here is
33 * based on the self-tuning, low overhead replacement cache
34 * presented by Megiddo and Modha at FAST 2003, there are some
35 * significant differences:
37 * 1. The Megiddo and Modha model assumes any page is evictable.
38 * Pages in its cache cannot be "locked" into memory. This makes
39 * the eviction algorithm simple: evict the last page in the list.
40 * This also make the performance characteristics easy to reason
41 * about. Our cache is not so simple. At any given moment, some
42 * subset of the blocks in the cache are un-evictable because we
43 * have handed out a reference to them. Blocks are only evictable
44 * when there are no external references active. This makes
45 * eviction far more problematic: we choose to evict the evictable
46 * blocks that are the "lowest" in the list.
48 * There are times when it is not possible to evict the requested
49 * space. In these circumstances we are unable to adjust the cache
50 * size. To prevent the cache growing unbounded at these times we
51 * implement a "cache throttle" that slows the flow of new data
52 * into the cache until we can make space available.
54 * 2. The Megiddo and Modha model assumes a fixed cache size.
55 * Pages are evicted when the cache is full and there is a cache
56 * miss. Our model has a variable sized cache. It grows with
57 * high use, but also tries to react to memory pressure from the
58 * operating system: decreasing its size when system memory is
61 * 3. The Megiddo and Modha model assumes a fixed page size. All
62 * elements of the cache are therefore exactly the same size. So
63 * when adjusting the cache size following a cache miss, its simply
64 * a matter of choosing a single page to evict. In our model, we
65 * have variable sized cache blocks (rangeing from 512 bytes to
66 * 128K bytes). We therefore choose a set of blocks to evict to make
67 * space for a cache miss that approximates as closely as possible
68 * the space used by the new block.
70 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
71 * by N. Megiddo & D. Modha, FAST 2003
77 * A new reference to a cache buffer can be obtained in two
78 * ways: 1) via a hash table lookup using the DVA as a key,
79 * or 2) via one of the ARC lists. The arc_read() interface
80 * uses method 1, while the internal ARC algorithms for
81 * adjusting the cache use method 2. We therefore provide two
82 * types of locks: 1) the hash table lock array, and 2) the
85 * Buffers do not have their own mutexes, rather they rely on the
86 * hash table mutexes for the bulk of their protection (i.e. most
87 * fields in the arc_buf_hdr_t are protected by these mutexes).
89 * buf_hash_find() returns the appropriate mutex (held) when it
90 * locates the requested buffer in the hash table. It returns
91 * NULL for the mutex if the buffer was not in the table.
93 * buf_hash_remove() expects the appropriate hash mutex to be
94 * already held before it is invoked.
96 * Each ARC state also has a mutex which is used to protect the
97 * buffer list associated with the state. When attempting to
98 * obtain a hash table lock while holding an ARC list lock you
99 * must use: mutex_tryenter() to avoid deadlock. Also note that
100 * the active state mutex must be held before the ghost state mutex.
102 * It as also possible to register a callback which is run when the
103 * arc_meta_limit is reached and no buffers can be safely evicted. In
104 * this case the arc user should drop a reference on some arc buffers so
105 * they can be reclaimed and the arc_meta_limit honored. For example,
106 * when using the ZPL each dentry holds a references on a znode. These
107 * dentries must be pruned before the arc buffer holding the znode can
110 * Note that the majority of the performance stats are manipulated
111 * with atomic operations.
113 * The L2ARC uses the l2ad_mtx on each vdev for the following:
115 * - L2ARC buflist creation
116 * - L2ARC buflist eviction
117 * - L2ARC write completion, which walks L2ARC buflists
118 * - ARC header destruction, as it removes from L2ARC buflists
119 * - ARC header release, as it removes from L2ARC buflists
125 * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure.
126 * This structure can point either to a block that is still in the cache or to
127 * one that is only accessible in an L2 ARC device, or it can provide
128 * information about a block that was recently evicted. If a block is
129 * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough
130 * information to retrieve it from the L2ARC device. This information is
131 * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block
132 * that is in this state cannot access the data directly.
134 * Blocks that are actively being referenced or have not been evicted
135 * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within
136 * the arc_buf_hdr_t that will point to the data block in memory. A block can
137 * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC
138 * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and
139 * also in the arc_buf_hdr_t's private physical data block pointer (b_pdata).
141 * The L1ARC's data pointer may or may not be uncompressed. The ARC has the
142 * ability to store the physical data (b_pdata) associated with the DVA of the
143 * arc_buf_hdr_t. Since the b_pdata is a copy of the on-disk physical block,
144 * it will match its on-disk compression characteristics. This behavior can be
145 * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the
146 * compressed ARC functionality is disabled, the b_pdata will point to an
147 * uncompressed version of the on-disk data.
149 * Data in the L1ARC is not accessed by consumers of the ARC directly. Each
150 * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it.
151 * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC
152 * consumer. The ARC will provide references to this data and will keep it
153 * cached until it is no longer in use. The ARC caches only the L1ARC's physical
154 * data block and will evict any arc_buf_t that is no longer referenced. The
155 * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the
156 * "overhead_size" kstat.
158 * Depending on the consumer, an arc_buf_t can be requested in uncompressed or
159 * compressed form. The typical case is that consumers will want uncompressed
160 * data, and when that happens a new data buffer is allocated where the data is
161 * decompressed for them to use. Currently the only consumer who wants
162 * compressed arc_buf_t's is "zfs send", when it streams data exactly as it
163 * exists on disk. When this happens, the arc_buf_t's data buffer is shared
164 * with the arc_buf_hdr_t.
166 * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The
167 * first one is owned by a compressed send consumer (and therefore references
168 * the same compressed data buffer as the arc_buf_hdr_t) and the second could be
169 * used by any other consumer (and has its own uncompressed copy of the data
184 * | b_buf +------------>+-----------+ arc_buf_t
185 * | b_pdata +-+ |b_next +---->+-----------+
186 * +-----------+ | |-----------| |b_next +-->NULL
187 * | |b_comp = T | +-----------+
188 * | |b_data +-+ |b_comp = F |
189 * | +-----------+ | |b_data +-+
190 * +->+------+ | +-----------+ |
192 * data | |<--------------+ | uncompressed
193 * +------+ compressed, | data
194 * shared +-->+------+
199 * When a consumer reads a block, the ARC must first look to see if the
200 * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new
201 * arc_buf_t and either copies uncompressed data into a new data buffer from an
202 * existing uncompressed arc_buf_t, decompresses the hdr's b_pdata buffer into a
203 * new data buffer, or shares the hdr's b_pdata buffer, depending on whether the
204 * hdr is compressed and the desired compression characteristics of the
205 * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the
206 * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be
207 * the last buffer in the hdr's b_buf list, however a shared compressed buf can
208 * be anywhere in the hdr's list.
210 * The diagram below shows an example of an uncompressed ARC hdr that is
211 * sharing its data with an arc_buf_t (note that the shared uncompressed buf is
212 * the last element in the buf list):
224 * | | arc_buf_t (shared)
225 * | b_buf +------------>+---------+ arc_buf_t
226 * | | |b_next +---->+---------+
227 * | b_pdata +-+ |---------| |b_next +-->NULL
228 * +-----------+ | | | +---------+
230 * | +---------+ | |b_data +-+
231 * +->+------+ | +---------+ |
233 * uncompressed | | | |
236 * | uncompressed | | |
239 * +---------------------------------+
241 * Writing to the ARC requires that the ARC first discard the hdr's b_pdata
242 * since the physical block is about to be rewritten. The new data contents
243 * will be contained in the arc_buf_t. As the I/O pipeline performs the write,
244 * it may compress the data before writing it to disk. The ARC will be called
245 * with the transformed data and will bcopy the transformed on-disk block into
246 * a newly allocated b_pdata. Writes are always done into buffers which have
247 * either been loaned (and hence are new and don't have other readers) or
248 * buffers which have been released (and hence have their own hdr, if there
249 * were originally other readers of the buf's original hdr). This ensures that
250 * the ARC only needs to update a single buf and its hdr after a write occurs.
252 * When the L2ARC is in use, it will also take advantage of the b_pdata. The
253 * L2ARC will always write the contents of b_pdata to the L2ARC. This means
254 * that when compressed ARC is enabled that the L2ARC blocks are identical
255 * to the on-disk block in the main data pool. This provides a significant
256 * advantage since the ARC can leverage the bp's checksum when reading from the
257 * L2ARC to determine if the contents are valid. However, if the compressed
258 * ARC is disabled, then the L2ARC's block must be transformed to look
259 * like the physical block in the main data pool before comparing the
260 * checksum and determining its validity.
265 #include <sys/spa_impl.h>
266 #include <sys/zio_compress.h>
267 #include <sys/zio_checksum.h>
268 #include <sys/zfs_context.h>
270 #include <sys/refcount.h>
271 #include <sys/vdev.h>
272 #include <sys/vdev_impl.h>
273 #include <sys/dsl_pool.h>
274 #include <sys/multilist.h>
276 #include <sys/vmsystm.h>
278 #include <sys/fs/swapnode.h>
280 #include <linux/mm_compat.h>
282 #include <sys/callb.h>
283 #include <sys/kstat.h>
284 #include <sys/dmu_tx.h>
285 #include <zfs_fletcher.h>
286 #include <sys/arc_impl.h>
287 #include <sys/trace_arc.h>
290 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
291 boolean_t arc_watch
= B_FALSE
;
294 static kmutex_t arc_reclaim_lock
;
295 static kcondvar_t arc_reclaim_thread_cv
;
296 static boolean_t arc_reclaim_thread_exit
;
297 static kcondvar_t arc_reclaim_waiters_cv
;
300 * The number of headers to evict in arc_evict_state_impl() before
301 * dropping the sublist lock and evicting from another sublist. A lower
302 * value means we're more likely to evict the "correct" header (i.e. the
303 * oldest header in the arc state), but comes with higher overhead
304 * (i.e. more invocations of arc_evict_state_impl()).
306 int zfs_arc_evict_batch_limit
= 10;
309 * The number of sublists used for each of the arc state lists. If this
310 * is not set to a suitable value by the user, it will be configured to
311 * the number of CPUs on the system in arc_init().
313 int zfs_arc_num_sublists_per_state
= 0;
315 /* number of seconds before growing cache again */
316 static int arc_grow_retry
= 5;
318 /* shift of arc_c for calculating overflow limit in arc_get_data_buf */
319 int zfs_arc_overflow_shift
= 8;
321 /* shift of arc_c for calculating both min and max arc_p */
322 static int arc_p_min_shift
= 4;
324 /* log2(fraction of arc to reclaim) */
325 static int arc_shrink_shift
= 7;
328 * log2(fraction of ARC which must be free to allow growing).
329 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
330 * when reading a new block into the ARC, we will evict an equal-sized block
333 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
334 * we will still not allow it to grow.
336 int arc_no_grow_shift
= 5;
340 * minimum lifespan of a prefetch block in clock ticks
341 * (initialized in arc_init())
343 static int arc_min_prefetch_lifespan
;
346 * If this percent of memory is free, don't throttle.
348 int arc_lotsfree_percent
= 10;
353 * The arc has filled available memory and has now warmed up.
355 static boolean_t arc_warm
;
358 * log2 fraction of the zio arena to keep free.
360 int arc_zio_arena_free_shift
= 2;
363 * These tunables are for performance analysis.
365 unsigned long zfs_arc_max
= 0;
366 unsigned long zfs_arc_min
= 0;
367 unsigned long zfs_arc_meta_limit
= 0;
368 unsigned long zfs_arc_meta_min
= 0;
369 unsigned long zfs_arc_dnode_limit
= 0;
370 unsigned long zfs_arc_dnode_reduce_percent
= 10;
371 int zfs_arc_grow_retry
= 0;
372 int zfs_arc_shrink_shift
= 0;
373 int zfs_arc_p_min_shift
= 0;
374 int zfs_arc_average_blocksize
= 8 * 1024; /* 8KB */
376 int zfs_compressed_arc_enabled
= B_TRUE
;
379 * ARC will evict meta buffers that exceed arc_meta_limit. This
380 * tunable make arc_meta_limit adjustable for different workloads.
382 unsigned long zfs_arc_meta_limit_percent
= 75;
385 * Percentage that can be consumed by dnodes of ARC meta buffers.
387 unsigned long zfs_arc_dnode_limit_percent
= 10;
390 * These tunables are Linux specific
392 unsigned long zfs_arc_sys_free
= 0;
393 int zfs_arc_min_prefetch_lifespan
= 0;
394 int zfs_arc_p_aggressive_disable
= 1;
395 int zfs_arc_p_dampener_disable
= 1;
396 int zfs_arc_meta_prune
= 10000;
397 int zfs_arc_meta_strategy
= ARC_STRATEGY_META_BALANCED
;
398 int zfs_arc_meta_adjust_restarts
= 4096;
399 int zfs_arc_lotsfree_percent
= 10;
402 static arc_state_t ARC_anon
;
403 static arc_state_t ARC_mru
;
404 static arc_state_t ARC_mru_ghost
;
405 static arc_state_t ARC_mfu
;
406 static arc_state_t ARC_mfu_ghost
;
407 static arc_state_t ARC_l2c_only
;
409 typedef struct arc_stats
{
410 kstat_named_t arcstat_hits
;
411 kstat_named_t arcstat_misses
;
412 kstat_named_t arcstat_demand_data_hits
;
413 kstat_named_t arcstat_demand_data_misses
;
414 kstat_named_t arcstat_demand_metadata_hits
;
415 kstat_named_t arcstat_demand_metadata_misses
;
416 kstat_named_t arcstat_prefetch_data_hits
;
417 kstat_named_t arcstat_prefetch_data_misses
;
418 kstat_named_t arcstat_prefetch_metadata_hits
;
419 kstat_named_t arcstat_prefetch_metadata_misses
;
420 kstat_named_t arcstat_mru_hits
;
421 kstat_named_t arcstat_mru_ghost_hits
;
422 kstat_named_t arcstat_mfu_hits
;
423 kstat_named_t arcstat_mfu_ghost_hits
;
424 kstat_named_t arcstat_deleted
;
426 * Number of buffers that could not be evicted because the hash lock
427 * was held by another thread. The lock may not necessarily be held
428 * by something using the same buffer, since hash locks are shared
429 * by multiple buffers.
431 kstat_named_t arcstat_mutex_miss
;
433 * Number of buffers skipped because they have I/O in progress, are
434 * indrect prefetch buffers that have not lived long enough, or are
435 * not from the spa we're trying to evict from.
437 kstat_named_t arcstat_evict_skip
;
439 * Number of times arc_evict_state() was unable to evict enough
440 * buffers to reach its target amount.
442 kstat_named_t arcstat_evict_not_enough
;
443 kstat_named_t arcstat_evict_l2_cached
;
444 kstat_named_t arcstat_evict_l2_eligible
;
445 kstat_named_t arcstat_evict_l2_ineligible
;
446 kstat_named_t arcstat_evict_l2_skip
;
447 kstat_named_t arcstat_hash_elements
;
448 kstat_named_t arcstat_hash_elements_max
;
449 kstat_named_t arcstat_hash_collisions
;
450 kstat_named_t arcstat_hash_chains
;
451 kstat_named_t arcstat_hash_chain_max
;
452 kstat_named_t arcstat_p
;
453 kstat_named_t arcstat_c
;
454 kstat_named_t arcstat_c_min
;
455 kstat_named_t arcstat_c_max
;
456 kstat_named_t arcstat_size
;
458 * Number of compressed bytes stored in the arc_buf_hdr_t's b_pdata.
459 * Note that the compressed bytes may match the uncompressed bytes
460 * if the block is either not compressed or compressed arc is disabled.
462 kstat_named_t arcstat_compressed_size
;
464 * Uncompressed size of the data stored in b_pdata. If compressed
465 * arc is disabled then this value will be identical to the stat
468 kstat_named_t arcstat_uncompressed_size
;
470 * Number of bytes stored in all the arc_buf_t's. This is classified
471 * as "overhead" since this data is typically short-lived and will
472 * be evicted from the arc when it becomes unreferenced unless the
473 * zfs_keep_uncompressed_metadata or zfs_keep_uncompressed_level
474 * values have been set (see comment in dbuf.c for more information).
476 kstat_named_t arcstat_overhead_size
;
478 * Number of bytes consumed by internal ARC structures necessary
479 * for tracking purposes; these structures are not actually
480 * backed by ARC buffers. This includes arc_buf_hdr_t structures
481 * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only
482 * caches), and arc_buf_t structures (allocated via arc_buf_t
485 kstat_named_t arcstat_hdr_size
;
487 * Number of bytes consumed by ARC buffers of type equal to
488 * ARC_BUFC_DATA. This is generally consumed by buffers backing
489 * on disk user data (e.g. plain file contents).
491 kstat_named_t arcstat_data_size
;
493 * Number of bytes consumed by ARC buffers of type equal to
494 * ARC_BUFC_METADATA. This is generally consumed by buffers
495 * backing on disk data that is used for internal ZFS
496 * structures (e.g. ZAP, dnode, indirect blocks, etc).
498 kstat_named_t arcstat_metadata_size
;
500 * Number of bytes consumed by dmu_buf_impl_t objects.
502 kstat_named_t arcstat_dbuf_size
;
504 * Number of bytes consumed by dnode_t objects.
506 kstat_named_t arcstat_dnode_size
;
508 * Number of bytes consumed by bonus buffers.
510 kstat_named_t arcstat_bonus_size
;
512 * Total number of bytes consumed by ARC buffers residing in the
513 * arc_anon state. This includes *all* buffers in the arc_anon
514 * state; e.g. data, metadata, evictable, and unevictable buffers
515 * are all included in this value.
517 kstat_named_t arcstat_anon_size
;
519 * Number of bytes consumed by ARC buffers that meet the
520 * following criteria: backing buffers of type ARC_BUFC_DATA,
521 * residing in the arc_anon state, and are eligible for eviction
522 * (e.g. have no outstanding holds on the buffer).
524 kstat_named_t arcstat_anon_evictable_data
;
526 * Number of bytes consumed by ARC buffers that meet the
527 * following criteria: backing buffers of type ARC_BUFC_METADATA,
528 * residing in the arc_anon state, and are eligible for eviction
529 * (e.g. have no outstanding holds on the buffer).
531 kstat_named_t arcstat_anon_evictable_metadata
;
533 * Total number of bytes consumed by ARC buffers residing in the
534 * arc_mru state. This includes *all* buffers in the arc_mru
535 * state; e.g. data, metadata, evictable, and unevictable buffers
536 * are all included in this value.
538 kstat_named_t arcstat_mru_size
;
540 * Number of bytes consumed by ARC buffers that meet the
541 * following criteria: backing buffers of type ARC_BUFC_DATA,
542 * residing in the arc_mru state, and are eligible for eviction
543 * (e.g. have no outstanding holds on the buffer).
545 kstat_named_t arcstat_mru_evictable_data
;
547 * Number of bytes consumed by ARC buffers that meet the
548 * following criteria: backing buffers of type ARC_BUFC_METADATA,
549 * residing in the arc_mru state, and are eligible for eviction
550 * (e.g. have no outstanding holds on the buffer).
552 kstat_named_t arcstat_mru_evictable_metadata
;
554 * Total number of bytes that *would have been* consumed by ARC
555 * buffers in the arc_mru_ghost state. The key thing to note
556 * here, is the fact that this size doesn't actually indicate
557 * RAM consumption. The ghost lists only consist of headers and
558 * don't actually have ARC buffers linked off of these headers.
559 * Thus, *if* the headers had associated ARC buffers, these
560 * buffers *would have* consumed this number of bytes.
562 kstat_named_t arcstat_mru_ghost_size
;
564 * Number of bytes that *would have been* consumed by ARC
565 * buffers that are eligible for eviction, of type
566 * ARC_BUFC_DATA, and linked off the arc_mru_ghost state.
568 kstat_named_t arcstat_mru_ghost_evictable_data
;
570 * Number of bytes that *would have been* consumed by ARC
571 * buffers that are eligible for eviction, of type
572 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
574 kstat_named_t arcstat_mru_ghost_evictable_metadata
;
576 * Total number of bytes consumed by ARC buffers residing in the
577 * arc_mfu state. This includes *all* buffers in the arc_mfu
578 * state; e.g. data, metadata, evictable, and unevictable buffers
579 * are all included in this value.
581 kstat_named_t arcstat_mfu_size
;
583 * Number of bytes consumed by ARC buffers that are eligible for
584 * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu
587 kstat_named_t arcstat_mfu_evictable_data
;
589 * Number of bytes consumed by ARC buffers that are eligible for
590 * eviction, of type ARC_BUFC_METADATA, and reside in the
593 kstat_named_t arcstat_mfu_evictable_metadata
;
595 * Total number of bytes that *would have been* consumed by ARC
596 * buffers in the arc_mfu_ghost state. See the comment above
597 * arcstat_mru_ghost_size for more details.
599 kstat_named_t arcstat_mfu_ghost_size
;
601 * Number of bytes that *would have been* consumed by ARC
602 * buffers that are eligible for eviction, of type
603 * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state.
605 kstat_named_t arcstat_mfu_ghost_evictable_data
;
607 * Number of bytes that *would have been* consumed by ARC
608 * buffers that are eligible for eviction, of type
609 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
611 kstat_named_t arcstat_mfu_ghost_evictable_metadata
;
612 kstat_named_t arcstat_l2_hits
;
613 kstat_named_t arcstat_l2_misses
;
614 kstat_named_t arcstat_l2_feeds
;
615 kstat_named_t arcstat_l2_rw_clash
;
616 kstat_named_t arcstat_l2_read_bytes
;
617 kstat_named_t arcstat_l2_write_bytes
;
618 kstat_named_t arcstat_l2_writes_sent
;
619 kstat_named_t arcstat_l2_writes_done
;
620 kstat_named_t arcstat_l2_writes_error
;
621 kstat_named_t arcstat_l2_writes_lock_retry
;
622 kstat_named_t arcstat_l2_evict_lock_retry
;
623 kstat_named_t arcstat_l2_evict_reading
;
624 kstat_named_t arcstat_l2_evict_l1cached
;
625 kstat_named_t arcstat_l2_free_on_write
;
626 kstat_named_t arcstat_l2_abort_lowmem
;
627 kstat_named_t arcstat_l2_cksum_bad
;
628 kstat_named_t arcstat_l2_io_error
;
629 kstat_named_t arcstat_l2_size
;
630 kstat_named_t arcstat_l2_asize
;
631 kstat_named_t arcstat_l2_hdr_size
;
632 kstat_named_t arcstat_memory_throttle_count
;
633 kstat_named_t arcstat_memory_direct_count
;
634 kstat_named_t arcstat_memory_indirect_count
;
635 kstat_named_t arcstat_no_grow
;
636 kstat_named_t arcstat_tempreserve
;
637 kstat_named_t arcstat_loaned_bytes
;
638 kstat_named_t arcstat_prune
;
639 kstat_named_t arcstat_meta_used
;
640 kstat_named_t arcstat_meta_limit
;
641 kstat_named_t arcstat_dnode_limit
;
642 kstat_named_t arcstat_meta_max
;
643 kstat_named_t arcstat_meta_min
;
644 kstat_named_t arcstat_sync_wait_for_async
;
645 kstat_named_t arcstat_demand_hit_predictive_prefetch
;
646 kstat_named_t arcstat_need_free
;
647 kstat_named_t arcstat_sys_free
;
650 static arc_stats_t arc_stats
= {
651 { "hits", KSTAT_DATA_UINT64
},
652 { "misses", KSTAT_DATA_UINT64
},
653 { "demand_data_hits", KSTAT_DATA_UINT64
},
654 { "demand_data_misses", KSTAT_DATA_UINT64
},
655 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
656 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
657 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
658 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
659 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
660 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
661 { "mru_hits", KSTAT_DATA_UINT64
},
662 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
663 { "mfu_hits", KSTAT_DATA_UINT64
},
664 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
665 { "deleted", KSTAT_DATA_UINT64
},
666 { "mutex_miss", KSTAT_DATA_UINT64
},
667 { "evict_skip", KSTAT_DATA_UINT64
},
668 { "evict_not_enough", KSTAT_DATA_UINT64
},
669 { "evict_l2_cached", KSTAT_DATA_UINT64
},
670 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
671 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
672 { "evict_l2_skip", KSTAT_DATA_UINT64
},
673 { "hash_elements", KSTAT_DATA_UINT64
},
674 { "hash_elements_max", KSTAT_DATA_UINT64
},
675 { "hash_collisions", KSTAT_DATA_UINT64
},
676 { "hash_chains", KSTAT_DATA_UINT64
},
677 { "hash_chain_max", KSTAT_DATA_UINT64
},
678 { "p", KSTAT_DATA_UINT64
},
679 { "c", KSTAT_DATA_UINT64
},
680 { "c_min", KSTAT_DATA_UINT64
},
681 { "c_max", KSTAT_DATA_UINT64
},
682 { "size", KSTAT_DATA_UINT64
},
683 { "compressed_size", KSTAT_DATA_UINT64
},
684 { "uncompressed_size", KSTAT_DATA_UINT64
},
685 { "overhead_size", KSTAT_DATA_UINT64
},
686 { "hdr_size", KSTAT_DATA_UINT64
},
687 { "data_size", KSTAT_DATA_UINT64
},
688 { "metadata_size", KSTAT_DATA_UINT64
},
689 { "dbuf_size", KSTAT_DATA_UINT64
},
690 { "dnode_size", KSTAT_DATA_UINT64
},
691 { "bonus_size", KSTAT_DATA_UINT64
},
692 { "anon_size", KSTAT_DATA_UINT64
},
693 { "anon_evictable_data", KSTAT_DATA_UINT64
},
694 { "anon_evictable_metadata", KSTAT_DATA_UINT64
},
695 { "mru_size", KSTAT_DATA_UINT64
},
696 { "mru_evictable_data", KSTAT_DATA_UINT64
},
697 { "mru_evictable_metadata", KSTAT_DATA_UINT64
},
698 { "mru_ghost_size", KSTAT_DATA_UINT64
},
699 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64
},
700 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
701 { "mfu_size", KSTAT_DATA_UINT64
},
702 { "mfu_evictable_data", KSTAT_DATA_UINT64
},
703 { "mfu_evictable_metadata", KSTAT_DATA_UINT64
},
704 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
705 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64
},
706 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
707 { "l2_hits", KSTAT_DATA_UINT64
},
708 { "l2_misses", KSTAT_DATA_UINT64
},
709 { "l2_feeds", KSTAT_DATA_UINT64
},
710 { "l2_rw_clash", KSTAT_DATA_UINT64
},
711 { "l2_read_bytes", KSTAT_DATA_UINT64
},
712 { "l2_write_bytes", KSTAT_DATA_UINT64
},
713 { "l2_writes_sent", KSTAT_DATA_UINT64
},
714 { "l2_writes_done", KSTAT_DATA_UINT64
},
715 { "l2_writes_error", KSTAT_DATA_UINT64
},
716 { "l2_writes_lock_retry", KSTAT_DATA_UINT64
},
717 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
718 { "l2_evict_reading", KSTAT_DATA_UINT64
},
719 { "l2_evict_l1cached", KSTAT_DATA_UINT64
},
720 { "l2_free_on_write", KSTAT_DATA_UINT64
},
721 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
722 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
723 { "l2_io_error", KSTAT_DATA_UINT64
},
724 { "l2_size", KSTAT_DATA_UINT64
},
725 { "l2_asize", KSTAT_DATA_UINT64
},
726 { "l2_hdr_size", KSTAT_DATA_UINT64
},
727 { "memory_throttle_count", KSTAT_DATA_UINT64
},
728 { "memory_direct_count", KSTAT_DATA_UINT64
},
729 { "memory_indirect_count", KSTAT_DATA_UINT64
},
730 { "arc_no_grow", KSTAT_DATA_UINT64
},
731 { "arc_tempreserve", KSTAT_DATA_UINT64
},
732 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
733 { "arc_prune", KSTAT_DATA_UINT64
},
734 { "arc_meta_used", KSTAT_DATA_UINT64
},
735 { "arc_meta_limit", KSTAT_DATA_UINT64
},
736 { "arc_dnode_limit", KSTAT_DATA_UINT64
},
737 { "arc_meta_max", KSTAT_DATA_UINT64
},
738 { "arc_meta_min", KSTAT_DATA_UINT64
},
739 { "sync_wait_for_async", KSTAT_DATA_UINT64
},
740 { "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64
},
741 { "arc_need_free", KSTAT_DATA_UINT64
},
742 { "arc_sys_free", KSTAT_DATA_UINT64
}
745 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
747 #define ARCSTAT_INCR(stat, val) \
748 atomic_add_64(&arc_stats.stat.value.ui64, (val))
750 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
751 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
753 #define ARCSTAT_MAX(stat, val) { \
755 while ((val) > (m = arc_stats.stat.value.ui64) && \
756 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
760 #define ARCSTAT_MAXSTAT(stat) \
761 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
764 * We define a macro to allow ARC hits/misses to be easily broken down by
765 * two separate conditions, giving a total of four different subtypes for
766 * each of hits and misses (so eight statistics total).
768 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
771 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
773 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
777 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
779 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
784 static arc_state_t
*arc_anon
;
785 static arc_state_t
*arc_mru
;
786 static arc_state_t
*arc_mru_ghost
;
787 static arc_state_t
*arc_mfu
;
788 static arc_state_t
*arc_mfu_ghost
;
789 static arc_state_t
*arc_l2c_only
;
792 * There are several ARC variables that are critical to export as kstats --
793 * but we don't want to have to grovel around in the kstat whenever we wish to
794 * manipulate them. For these variables, we therefore define them to be in
795 * terms of the statistic variable. This assures that we are not introducing
796 * the possibility of inconsistency by having shadow copies of the variables,
797 * while still allowing the code to be readable.
799 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
800 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
801 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
802 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
803 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
804 #define arc_no_grow ARCSTAT(arcstat_no_grow) /* do not grow cache size */
805 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
806 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
807 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
808 #define arc_dnode_limit ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */
809 #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */
810 #define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */
811 #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
812 #define arc_dbuf_size ARCSTAT(arcstat_dbuf_size) /* dbuf metadata */
813 #define arc_dnode_size ARCSTAT(arcstat_dnode_size) /* dnode metadata */
814 #define arc_bonus_size ARCSTAT(arcstat_bonus_size) /* bonus buffer metadata */
815 #define arc_need_free ARCSTAT(arcstat_need_free) /* bytes to be freed */
816 #define arc_sys_free ARCSTAT(arcstat_sys_free) /* target system free bytes */
818 /* compressed size of entire arc */
819 #define arc_compressed_size ARCSTAT(arcstat_compressed_size)
820 /* uncompressed size of entire arc */
821 #define arc_uncompressed_size ARCSTAT(arcstat_uncompressed_size)
822 /* number of bytes in the arc from arc_buf_t's */
823 #define arc_overhead_size ARCSTAT(arcstat_overhead_size)
825 static list_t arc_prune_list
;
826 static kmutex_t arc_prune_mtx
;
827 static taskq_t
*arc_prune_taskq
;
829 #define GHOST_STATE(state) \
830 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
831 (state) == arc_l2c_only)
833 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
834 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
835 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
836 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
837 #define HDR_COMPRESSION_ENABLED(hdr) \
838 ((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC)
840 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
841 #define HDR_L2_READING(hdr) \
842 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
843 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
844 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
845 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
846 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
847 #define HDR_SHARED_DATA(hdr) ((hdr)->b_flags & ARC_FLAG_SHARED_DATA)
849 #define HDR_ISTYPE_METADATA(hdr) \
850 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
851 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
853 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
854 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
856 /* For storing compression mode in b_flags */
857 #define HDR_COMPRESS_OFFSET (highbit64(ARC_FLAG_COMPRESS_0) - 1)
859 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET((hdr)->b_flags, \
860 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS))
861 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \
862 HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp));
864 #define ARC_BUF_LAST(buf) ((buf)->b_next == NULL)
865 #define ARC_BUF_SHARED(buf) ((buf)->b_flags & ARC_BUF_FLAG_SHARED)
866 #define ARC_BUF_COMPRESSED(buf) ((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED)
872 #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
873 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
876 * Hash table routines
879 #define HT_LOCK_ALIGN 64
880 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
885 unsigned char pad
[HT_LOCK_PAD
];
889 #define BUF_LOCKS 8192
890 typedef struct buf_hash_table
{
892 arc_buf_hdr_t
**ht_table
;
893 struct ht_lock ht_locks
[BUF_LOCKS
];
896 static buf_hash_table_t buf_hash_table
;
898 #define BUF_HASH_INDEX(spa, dva, birth) \
899 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
900 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
901 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
902 #define HDR_LOCK(hdr) \
903 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
905 uint64_t zfs_crc64_table
[256];
911 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
912 #define L2ARC_HEADROOM 2 /* num of writes */
915 * If we discover during ARC scan any buffers to be compressed, we boost
916 * our headroom for the next scanning cycle by this percentage multiple.
918 #define L2ARC_HEADROOM_BOOST 200
919 #define L2ARC_FEED_SECS 1 /* caching interval secs */
920 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
922 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
923 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
925 /* L2ARC Performance Tunables */
926 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
927 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
928 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
929 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
930 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
931 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
932 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
933 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
934 int l2arc_norw
= B_FALSE
; /* no reads during writes */
939 static list_t L2ARC_dev_list
; /* device list */
940 static list_t
*l2arc_dev_list
; /* device list pointer */
941 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
942 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
943 static list_t L2ARC_free_on_write
; /* free after write buf list */
944 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
945 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
946 static uint64_t l2arc_ndev
; /* number of devices */
948 typedef struct l2arc_read_callback
{
949 arc_buf_hdr_t
*l2rcb_hdr
; /* read header */
950 blkptr_t l2rcb_bp
; /* original blkptr */
951 zbookmark_phys_t l2rcb_zb
; /* original bookmark */
952 int l2rcb_flags
; /* original flags */
953 } l2arc_read_callback_t
;
955 typedef struct l2arc_data_free
{
956 /* protected by l2arc_free_on_write_mtx */
959 arc_buf_contents_t l2df_type
;
960 list_node_t l2df_list_node
;
963 static kmutex_t l2arc_feed_thr_lock
;
964 static kcondvar_t l2arc_feed_thr_cv
;
965 static uint8_t l2arc_thread_exit
;
967 static void *arc_get_data_buf(arc_buf_hdr_t
*, uint64_t, void *);
968 static void arc_free_data_buf(arc_buf_hdr_t
*, void *, uint64_t, void *);
969 static void arc_hdr_free_pdata(arc_buf_hdr_t
*hdr
);
970 static void arc_hdr_alloc_pdata(arc_buf_hdr_t
*);
971 static void arc_access(arc_buf_hdr_t
*, kmutex_t
*);
972 static boolean_t
arc_is_overflowing(void);
973 static void arc_buf_watch(arc_buf_t
*);
974 static void arc_tuning_update(void);
975 static void arc_prune_async(int64_t);
977 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t
*);
978 static uint32_t arc_bufc_to_flags(arc_buf_contents_t
);
979 static inline void arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
980 static inline void arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
);
982 static boolean_t
l2arc_write_eligible(uint64_t, arc_buf_hdr_t
*);
983 static void l2arc_read_done(zio_t
*);
986 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
988 uint8_t *vdva
= (uint8_t *)dva
;
989 uint64_t crc
= -1ULL;
992 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
994 for (i
= 0; i
< sizeof (dva_t
); i
++)
995 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ vdva
[i
]) & 0xFF];
997 crc
^= (spa
>>8) ^ birth
;
1002 #define HDR_EMPTY(hdr) \
1003 ((hdr)->b_dva.dva_word[0] == 0 && \
1004 (hdr)->b_dva.dva_word[1] == 0)
1006 #define HDR_EQUAL(spa, dva, birth, hdr) \
1007 ((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
1008 ((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
1009 ((hdr)->b_birth == birth) && ((hdr)->b_spa == spa)
1012 buf_discard_identity(arc_buf_hdr_t
*hdr
)
1014 hdr
->b_dva
.dva_word
[0] = 0;
1015 hdr
->b_dva
.dva_word
[1] = 0;
1019 static arc_buf_hdr_t
*
1020 buf_hash_find(uint64_t spa
, const blkptr_t
*bp
, kmutex_t
**lockp
)
1022 const dva_t
*dva
= BP_IDENTITY(bp
);
1023 uint64_t birth
= BP_PHYSICAL_BIRTH(bp
);
1024 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
1025 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1028 mutex_enter(hash_lock
);
1029 for (hdr
= buf_hash_table
.ht_table
[idx
]; hdr
!= NULL
;
1030 hdr
= hdr
->b_hash_next
) {
1031 if (HDR_EQUAL(spa
, dva
, birth
, hdr
)) {
1036 mutex_exit(hash_lock
);
1042 * Insert an entry into the hash table. If there is already an element
1043 * equal to elem in the hash table, then the already existing element
1044 * will be returned and the new element will not be inserted.
1045 * Otherwise returns NULL.
1046 * If lockp == NULL, the caller is assumed to already hold the hash lock.
1048 static arc_buf_hdr_t
*
1049 buf_hash_insert(arc_buf_hdr_t
*hdr
, kmutex_t
**lockp
)
1051 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1052 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
1053 arc_buf_hdr_t
*fhdr
;
1056 ASSERT(!DVA_IS_EMPTY(&hdr
->b_dva
));
1057 ASSERT(hdr
->b_birth
!= 0);
1058 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1060 if (lockp
!= NULL
) {
1062 mutex_enter(hash_lock
);
1064 ASSERT(MUTEX_HELD(hash_lock
));
1067 for (fhdr
= buf_hash_table
.ht_table
[idx
], i
= 0; fhdr
!= NULL
;
1068 fhdr
= fhdr
->b_hash_next
, i
++) {
1069 if (HDR_EQUAL(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
, fhdr
))
1073 hdr
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
1074 buf_hash_table
.ht_table
[idx
] = hdr
;
1075 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1077 /* collect some hash table performance data */
1079 ARCSTAT_BUMP(arcstat_hash_collisions
);
1081 ARCSTAT_BUMP(arcstat_hash_chains
);
1083 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
1086 ARCSTAT_BUMP(arcstat_hash_elements
);
1087 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
1093 buf_hash_remove(arc_buf_hdr_t
*hdr
)
1095 arc_buf_hdr_t
*fhdr
, **hdrp
;
1096 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
1098 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
1099 ASSERT(HDR_IN_HASH_TABLE(hdr
));
1101 hdrp
= &buf_hash_table
.ht_table
[idx
];
1102 while ((fhdr
= *hdrp
) != hdr
) {
1103 ASSERT3P(fhdr
, !=, NULL
);
1104 hdrp
= &fhdr
->b_hash_next
;
1106 *hdrp
= hdr
->b_hash_next
;
1107 hdr
->b_hash_next
= NULL
;
1108 arc_hdr_clear_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
1110 /* collect some hash table performance data */
1111 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
1113 if (buf_hash_table
.ht_table
[idx
] &&
1114 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
1115 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
1119 * Global data structures and functions for the buf kmem cache.
1121 static kmem_cache_t
*hdr_full_cache
;
1122 static kmem_cache_t
*hdr_l2only_cache
;
1123 static kmem_cache_t
*buf_cache
;
1130 #if defined(_KERNEL) && defined(HAVE_SPL)
1132 * Large allocations which do not require contiguous pages
1133 * should be using vmem_free() in the linux kernel\
1135 vmem_free(buf_hash_table
.ht_table
,
1136 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1138 kmem_free(buf_hash_table
.ht_table
,
1139 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
1141 for (i
= 0; i
< BUF_LOCKS
; i
++)
1142 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
1143 kmem_cache_destroy(hdr_full_cache
);
1144 kmem_cache_destroy(hdr_l2only_cache
);
1145 kmem_cache_destroy(buf_cache
);
1149 * Constructor callback - called when the cache is empty
1150 * and a new buf is requested.
1154 hdr_full_cons(void *vbuf
, void *unused
, int kmflag
)
1156 arc_buf_hdr_t
*hdr
= vbuf
;
1158 bzero(hdr
, HDR_FULL_SIZE
);
1159 cv_init(&hdr
->b_l1hdr
.b_cv
, NULL
, CV_DEFAULT
, NULL
);
1160 refcount_create(&hdr
->b_l1hdr
.b_refcnt
);
1161 mutex_init(&hdr
->b_l1hdr
.b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1162 list_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1163 list_link_init(&hdr
->b_l2hdr
.b_l2node
);
1164 multilist_link_init(&hdr
->b_l1hdr
.b_arc_node
);
1165 arc_space_consume(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1172 hdr_l2only_cons(void *vbuf
, void *unused
, int kmflag
)
1174 arc_buf_hdr_t
*hdr
= vbuf
;
1176 bzero(hdr
, HDR_L2ONLY_SIZE
);
1177 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1184 buf_cons(void *vbuf
, void *unused
, int kmflag
)
1186 arc_buf_t
*buf
= vbuf
;
1188 bzero(buf
, sizeof (arc_buf_t
));
1189 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1190 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1196 * Destructor callback - called when a cached buf is
1197 * no longer required.
1201 hdr_full_dest(void *vbuf
, void *unused
)
1203 arc_buf_hdr_t
*hdr
= vbuf
;
1205 ASSERT(HDR_EMPTY(hdr
));
1206 cv_destroy(&hdr
->b_l1hdr
.b_cv
);
1207 refcount_destroy(&hdr
->b_l1hdr
.b_refcnt
);
1208 mutex_destroy(&hdr
->b_l1hdr
.b_freeze_lock
);
1209 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1210 arc_space_return(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1215 hdr_l2only_dest(void *vbuf
, void *unused
)
1217 ASSERTV(arc_buf_hdr_t
*hdr
= vbuf
);
1219 ASSERT(HDR_EMPTY(hdr
));
1220 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1225 buf_dest(void *vbuf
, void *unused
)
1227 arc_buf_t
*buf
= vbuf
;
1229 mutex_destroy(&buf
->b_evict_lock
);
1230 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1234 * Reclaim callback -- invoked when memory is low.
1238 hdr_recl(void *unused
)
1240 dprintf("hdr_recl called\n");
1242 * umem calls the reclaim func when we destroy the buf cache,
1243 * which is after we do arc_fini().
1246 cv_signal(&arc_reclaim_thread_cv
);
1253 uint64_t hsize
= 1ULL << 12;
1257 * The hash table is big enough to fill all of physical memory
1258 * with an average block size of zfs_arc_average_blocksize (default 8K).
1259 * By default, the table will take up
1260 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1262 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
1265 buf_hash_table
.ht_mask
= hsize
- 1;
1266 #if defined(_KERNEL) && defined(HAVE_SPL)
1268 * Large allocations which do not require contiguous pages
1269 * should be using vmem_alloc() in the linux kernel
1271 buf_hash_table
.ht_table
=
1272 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1274 buf_hash_table
.ht_table
=
1275 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1277 if (buf_hash_table
.ht_table
== NULL
) {
1278 ASSERT(hsize
> (1ULL << 8));
1283 hdr_full_cache
= kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE
,
1284 0, hdr_full_cons
, hdr_full_dest
, hdr_recl
, NULL
, NULL
, 0);
1285 hdr_l2only_cache
= kmem_cache_create("arc_buf_hdr_t_l2only",
1286 HDR_L2ONLY_SIZE
, 0, hdr_l2only_cons
, hdr_l2only_dest
, hdr_recl
,
1288 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1289 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1291 for (i
= 0; i
< 256; i
++)
1292 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1293 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1295 for (i
= 0; i
< BUF_LOCKS
; i
++) {
1296 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
1297 NULL
, MUTEX_DEFAULT
, NULL
);
1301 #define ARC_MINTIME (hz>>4) /* 62 ms */
1304 * This is the size that the buf occupies in memory. If the buf is compressed,
1305 * it will correspond to the compressed size. You should use this method of
1306 * getting the buf size unless you explicitly need the logical size.
1309 arc_buf_size(arc_buf_t
*buf
)
1311 return (ARC_BUF_COMPRESSED(buf
) ?
1312 HDR_GET_PSIZE(buf
->b_hdr
) : HDR_GET_LSIZE(buf
->b_hdr
));
1316 arc_buf_lsize(arc_buf_t
*buf
)
1318 return (HDR_GET_LSIZE(buf
->b_hdr
));
1322 arc_get_compression(arc_buf_t
*buf
)
1324 return (ARC_BUF_COMPRESSED(buf
) ?
1325 HDR_GET_COMPRESS(buf
->b_hdr
) : ZIO_COMPRESS_OFF
);
1328 static inline boolean_t
1329 arc_buf_is_shared(arc_buf_t
*buf
)
1331 boolean_t shared
= (buf
->b_data
!= NULL
&&
1332 buf
->b_data
== buf
->b_hdr
->b_l1hdr
.b_pdata
);
1333 IMPLY(shared
, HDR_SHARED_DATA(buf
->b_hdr
));
1334 IMPLY(shared
, ARC_BUF_SHARED(buf
));
1335 IMPLY(shared
, ARC_BUF_COMPRESSED(buf
) || ARC_BUF_LAST(buf
));
1338 * It would be nice to assert arc_can_share() too, but the "hdr isn't
1339 * already being shared" requirement prevents us from doing that.
1346 arc_cksum_free(arc_buf_hdr_t
*hdr
)
1348 ASSERT(HDR_HAS_L1HDR(hdr
));
1349 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1350 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
) {
1351 kmem_free(hdr
->b_l1hdr
.b_freeze_cksum
, sizeof (zio_cksum_t
));
1352 hdr
->b_l1hdr
.b_freeze_cksum
= NULL
;
1354 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1358 * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data
1359 * matches the checksum that is stored in the hdr. If there is no checksum,
1360 * or if the buf is compressed, this is a no-op.
1363 arc_cksum_verify(arc_buf_t
*buf
)
1365 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1368 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1371 if (ARC_BUF_COMPRESSED(buf
)) {
1372 ASSERT(hdr
->b_l1hdr
.b_freeze_cksum
== NULL
||
1373 hdr
->b_l1hdr
.b_bufcnt
> 1);
1377 ASSERT(HDR_HAS_L1HDR(hdr
));
1379 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
1380 if (hdr
->b_l1hdr
.b_freeze_cksum
== NULL
|| HDR_IO_ERROR(hdr
)) {
1381 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1385 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
), &zc
);
1386 if (!ZIO_CHECKSUM_EQUAL(*hdr
->b_l1hdr
.b_freeze_cksum
, zc
))
1387 panic("buffer modified while frozen!");
1388 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1392 arc_cksum_is_equal(arc_buf_hdr_t
*hdr
, zio_t
*zio
)
1394 enum zio_compress compress
= BP_GET_COMPRESS(zio
->io_bp
);
1395 boolean_t valid_cksum
;
1397 ASSERT(!BP_IS_EMBEDDED(zio
->io_bp
));
1398 VERIFY3U(BP_GET_PSIZE(zio
->io_bp
), ==, HDR_GET_PSIZE(hdr
));
1401 * We rely on the blkptr's checksum to determine if the block
1402 * is valid or not. When compressed arc is enabled, the l2arc
1403 * writes the block to the l2arc just as it appears in the pool.
1404 * This allows us to use the blkptr's checksum to validate the
1405 * data that we just read off of the l2arc without having to store
1406 * a separate checksum in the arc_buf_hdr_t. However, if compressed
1407 * arc is disabled, then the data written to the l2arc is always
1408 * uncompressed and won't match the block as it exists in the main
1409 * pool. When this is the case, we must first compress it if it is
1410 * compressed on the main pool before we can validate the checksum.
1412 if (!HDR_COMPRESSION_ENABLED(hdr
) && compress
!= ZIO_COMPRESS_OFF
) {
1416 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, ZIO_COMPRESS_OFF
);
1418 cbuf
= zio_buf_alloc(HDR_GET_PSIZE(hdr
));
1419 lsize
= HDR_GET_LSIZE(hdr
);
1420 csize
= zio_compress_data(compress
, zio
->io_data
, cbuf
, lsize
);
1421 ASSERT3U(csize
, <=, HDR_GET_PSIZE(hdr
));
1422 if (csize
< HDR_GET_PSIZE(hdr
)) {
1424 * Compressed blocks are always a multiple of the
1425 * smallest ashift in the pool. Ideally, we would
1426 * like to round up the csize to the next
1427 * spa_min_ashift but that value may have changed
1428 * since the block was last written. Instead,
1429 * we rely on the fact that the hdr's psize
1430 * was set to the psize of the block when it was
1431 * last written. We set the csize to that value
1432 * and zero out any part that should not contain
1435 bzero((char *)cbuf
+ csize
, HDR_GET_PSIZE(hdr
) - csize
);
1436 csize
= HDR_GET_PSIZE(hdr
);
1438 zio_push_transform(zio
, cbuf
, csize
, HDR_GET_PSIZE(hdr
), NULL
);
1442 * Block pointers always store the checksum for the logical data.
1443 * If the block pointer has the gang bit set, then the checksum
1444 * it represents is for the reconstituted data and not for an
1445 * individual gang member. The zio pipeline, however, must be able to
1446 * determine the checksum of each of the gang constituents so it
1447 * treats the checksum comparison differently than what we need
1448 * for l2arc blocks. This prevents us from using the
1449 * zio_checksum_error() interface directly. Instead we must call the
1450 * zio_checksum_error_impl() so that we can ensure the checksum is
1451 * generated using the correct checksum algorithm and accounts for the
1452 * logical I/O size and not just a gang fragment.
1454 valid_cksum
= (zio_checksum_error_impl(zio
->io_spa
, zio
->io_bp
,
1455 BP_GET_CHECKSUM(zio
->io_bp
), zio
->io_data
, zio
->io_size
,
1456 zio
->io_offset
, NULL
) == 0);
1457 zio_pop_transforms(zio
);
1458 return (valid_cksum
);
1462 * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a
1463 * checksum and attaches it to the buf's hdr so that we can ensure that the buf
1464 * isn't modified later on. If buf is compressed or there is already a checksum
1465 * on the hdr, this is a no-op (we only checksum uncompressed bufs).
1468 arc_cksum_compute(arc_buf_t
*buf
)
1470 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1472 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1475 ASSERT(HDR_HAS_L1HDR(hdr
));
1477 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1478 if (hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
) {
1479 ASSERT(!ARC_BUF_COMPRESSED(buf
) || hdr
->b_l1hdr
.b_bufcnt
> 1);
1480 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1482 } else if (ARC_BUF_COMPRESSED(buf
)) {
1484 * Since the checksum doesn't apply to compressed buffers, we
1485 * only keep a checksum if there are uncompressed buffers.
1486 * Therefore there must be another buffer, which is
1489 IMPLY(hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
,
1490 hdr
->b_l1hdr
.b_bufcnt
> 1);
1491 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1495 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1496 hdr
->b_l1hdr
.b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1498 fletcher_2_native(buf
->b_data
, arc_buf_size(buf
),
1499 hdr
->b_l1hdr
.b_freeze_cksum
);
1500 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
1506 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1508 panic("Got SIGSEGV at address: 0x%lx\n", (long) si
->si_addr
);
1514 arc_buf_unwatch(arc_buf_t
*buf
)
1518 ASSERT0(mprotect(buf
->b_data
, HDR_GET_LSIZE(buf
->b_hdr
),
1519 PROT_READ
| PROT_WRITE
));
1526 arc_buf_watch(arc_buf_t
*buf
)
1530 ASSERT0(mprotect(buf
->b_data
, arc_buf_size(buf
),
1535 static arc_buf_contents_t
1536 arc_buf_type(arc_buf_hdr_t
*hdr
)
1538 arc_buf_contents_t type
;
1539 if (HDR_ISTYPE_METADATA(hdr
)) {
1540 type
= ARC_BUFC_METADATA
;
1542 type
= ARC_BUFC_DATA
;
1544 VERIFY3U(hdr
->b_type
, ==, type
);
1549 arc_is_metadata(arc_buf_t
*buf
)
1551 return (HDR_ISTYPE_METADATA(buf
->b_hdr
) != 0);
1555 arc_bufc_to_flags(arc_buf_contents_t type
)
1559 /* metadata field is 0 if buffer contains normal data */
1561 case ARC_BUFC_METADATA
:
1562 return (ARC_FLAG_BUFC_METADATA
);
1566 panic("undefined ARC buffer type!");
1567 return ((uint32_t)-1);
1571 arc_buf_thaw(arc_buf_t
*buf
)
1573 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1575 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
1576 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1578 arc_cksum_verify(buf
);
1581 * Compressed buffers do not manipulate the b_freeze_cksum or
1582 * allocate b_thawed.
1584 if (ARC_BUF_COMPRESSED(buf
)) {
1585 ASSERT(hdr
->b_l1hdr
.b_freeze_cksum
== NULL
||
1586 hdr
->b_l1hdr
.b_bufcnt
> 1);
1590 ASSERT(HDR_HAS_L1HDR(hdr
));
1591 arc_cksum_free(hdr
);
1592 arc_buf_unwatch(buf
);
1596 arc_buf_freeze(arc_buf_t
*buf
)
1598 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1599 kmutex_t
*hash_lock
;
1601 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1604 if (ARC_BUF_COMPRESSED(buf
)) {
1605 ASSERT(hdr
->b_l1hdr
.b_freeze_cksum
== NULL
||
1606 hdr
->b_l1hdr
.b_bufcnt
> 1);
1610 hash_lock
= HDR_LOCK(hdr
);
1611 mutex_enter(hash_lock
);
1613 ASSERT(HDR_HAS_L1HDR(hdr
));
1614 ASSERT(hdr
->b_l1hdr
.b_freeze_cksum
!= NULL
||
1615 hdr
->b_l1hdr
.b_state
== arc_anon
);
1616 arc_cksum_compute(buf
);
1617 mutex_exit(hash_lock
);
1621 * The arc_buf_hdr_t's b_flags should never be modified directly. Instead,
1622 * the following functions should be used to ensure that the flags are
1623 * updated in a thread-safe way. When manipulating the flags either
1624 * the hash_lock must be held or the hdr must be undiscoverable. This
1625 * ensures that we're not racing with any other threads when updating
1629 arc_hdr_set_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1631 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
1632 hdr
->b_flags
|= flags
;
1636 arc_hdr_clear_flags(arc_buf_hdr_t
*hdr
, arc_flags_t flags
)
1638 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
1639 hdr
->b_flags
&= ~flags
;
1643 * Setting the compression bits in the arc_buf_hdr_t's b_flags is
1644 * done in a special way since we have to clear and set bits
1645 * at the same time. Consumers that wish to set the compression bits
1646 * must use this function to ensure that the flags are updated in
1647 * thread-safe manner.
1650 arc_hdr_set_compress(arc_buf_hdr_t
*hdr
, enum zio_compress cmp
)
1652 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
1655 * Holes and embedded blocks will always have a psize = 0 so
1656 * we ignore the compression of the blkptr and set the
1657 * want to uncompress them. Mark them as uncompressed.
1659 if (!zfs_compressed_arc_enabled
|| HDR_GET_PSIZE(hdr
) == 0) {
1660 arc_hdr_clear_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1661 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_OFF
);
1662 ASSERT(!HDR_COMPRESSION_ENABLED(hdr
));
1663 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, ZIO_COMPRESS_OFF
);
1665 arc_hdr_set_flags(hdr
, ARC_FLAG_COMPRESSED_ARC
);
1666 HDR_SET_COMPRESS(hdr
, cmp
);
1667 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, cmp
);
1668 ASSERT(HDR_COMPRESSION_ENABLED(hdr
));
1673 * Looks for another buf on the same hdr which has the data decompressed, copies
1674 * from it, and returns true. If no such buf exists, returns false.
1677 arc_buf_try_copy_decompressed_data(arc_buf_t
*buf
)
1679 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1681 boolean_t copied
= B_FALSE
;
1683 ASSERT(HDR_HAS_L1HDR(hdr
));
1684 ASSERT3P(buf
->b_data
, !=, NULL
);
1685 ASSERT(!ARC_BUF_COMPRESSED(buf
));
1687 for (from
= hdr
->b_l1hdr
.b_buf
; from
!= NULL
;
1688 from
= from
->b_next
) {
1689 /* can't use our own data buffer */
1694 if (!ARC_BUF_COMPRESSED(from
)) {
1695 bcopy(from
->b_data
, buf
->b_data
, arc_buf_size(buf
));
1702 * There were no decompressed bufs, so there should not be a
1703 * checksum on the hdr either.
1705 EQUIV(!copied
, hdr
->b_l1hdr
.b_freeze_cksum
== NULL
);
1711 * Given a buf that has a data buffer attached to it, this function will
1712 * efficiently fill the buf with data of the specified compression setting from
1713 * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr
1714 * are already sharing a data buf, no copy is performed.
1716 * If the buf is marked as compressed but uncompressed data was requested, this
1717 * will allocate a new data buffer for the buf, remove that flag, and fill the
1718 * buf with uncompressed data. You can't request a compressed buf on a hdr with
1719 * uncompressed data, and (since we haven't added support for it yet) if you
1720 * want compressed data your buf must already be marked as compressed and have
1721 * the correct-sized data buffer.
1724 arc_buf_fill(arc_buf_t
*buf
, boolean_t compressed
)
1726 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1727 boolean_t hdr_compressed
= (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
);
1728 dmu_object_byteswap_t bswap
= hdr
->b_l1hdr
.b_byteswap
;
1730 ASSERT3P(buf
->b_data
, !=, NULL
);
1731 IMPLY(compressed
, hdr_compressed
);
1732 IMPLY(compressed
, ARC_BUF_COMPRESSED(buf
));
1734 if (hdr_compressed
== compressed
) {
1735 if (!arc_buf_is_shared(buf
)) {
1736 bcopy(hdr
->b_l1hdr
.b_pdata
, buf
->b_data
,
1740 ASSERT(hdr_compressed
);
1741 ASSERT(!compressed
);
1742 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, HDR_GET_PSIZE(hdr
));
1745 * If the buf is sharing its data with the hdr, unlink it and
1746 * allocate a new data buffer for the buf.
1748 if (arc_buf_is_shared(buf
)) {
1749 ASSERT(ARC_BUF_COMPRESSED(buf
));
1751 /* We need to give the buf it's own b_data */
1752 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
1754 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
1755 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
1757 /* Previously overhead was 0; just add new overhead */
1758 ARCSTAT_INCR(arcstat_overhead_size
, HDR_GET_LSIZE(hdr
));
1759 } else if (ARC_BUF_COMPRESSED(buf
)) {
1760 /* We need to reallocate the buf's b_data */
1761 arc_free_data_buf(hdr
, buf
->b_data
, HDR_GET_PSIZE(hdr
),
1764 arc_get_data_buf(hdr
, HDR_GET_LSIZE(hdr
), buf
);
1766 /* We increased the size of b_data; update overhead */
1767 ARCSTAT_INCR(arcstat_overhead_size
,
1768 HDR_GET_LSIZE(hdr
) - HDR_GET_PSIZE(hdr
));
1772 * Regardless of the buf's previous compression settings, it
1773 * should not be compressed at the end of this function.
1775 buf
->b_flags
&= ~ARC_BUF_FLAG_COMPRESSED
;
1778 * Try copying the data from another buf which already has a
1779 * decompressed version. If that's not possible, it's time to
1780 * bite the bullet and decompress the data from the hdr.
1782 if (arc_buf_try_copy_decompressed_data(buf
)) {
1783 /* Skip byteswapping and checksumming (already done) */
1784 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, !=, NULL
);
1787 int error
= zio_decompress_data(HDR_GET_COMPRESS(hdr
),
1788 hdr
->b_l1hdr
.b_pdata
, buf
->b_data
,
1789 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
1792 * Absent hardware errors or software bugs, this should
1793 * be impossible, but log it anyway so we can debug it.
1797 "hdr %p, compress %d, psize %d, lsize %d",
1798 hdr
, HDR_GET_COMPRESS(hdr
),
1799 HDR_GET_PSIZE(hdr
), HDR_GET_LSIZE(hdr
));
1800 return (SET_ERROR(EIO
));
1805 /* Byteswap the buf's data if necessary */
1806 if (bswap
!= DMU_BSWAP_NUMFUNCS
) {
1807 ASSERT(!HDR_SHARED_DATA(hdr
));
1808 ASSERT3U(bswap
, <, DMU_BSWAP_NUMFUNCS
);
1809 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, HDR_GET_LSIZE(hdr
));
1812 /* Compute the hdr's checksum if necessary */
1813 arc_cksum_compute(buf
);
1819 arc_decompress(arc_buf_t
*buf
)
1821 return (arc_buf_fill(buf
, B_FALSE
));
1825 * Return the size of the block, b_pdata, that is stored in the arc_buf_hdr_t.
1828 arc_hdr_size(arc_buf_hdr_t
*hdr
)
1832 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
1833 HDR_GET_PSIZE(hdr
) > 0) {
1834 size
= HDR_GET_PSIZE(hdr
);
1836 ASSERT3U(HDR_GET_LSIZE(hdr
), !=, 0);
1837 size
= HDR_GET_LSIZE(hdr
);
1843 * Increment the amount of evictable space in the arc_state_t's refcount.
1844 * We account for the space used by the hdr and the arc buf individually
1845 * so that we can add and remove them from the refcount individually.
1848 arc_evictable_space_increment(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
1850 arc_buf_contents_t type
= arc_buf_type(hdr
);
1853 ASSERT(HDR_HAS_L1HDR(hdr
));
1855 if (GHOST_STATE(state
)) {
1856 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
1857 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
1858 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
1859 (void) refcount_add_many(&state
->arcs_esize
[type
],
1860 HDR_GET_LSIZE(hdr
), hdr
);
1864 ASSERT(!GHOST_STATE(state
));
1865 if (hdr
->b_l1hdr
.b_pdata
!= NULL
) {
1866 (void) refcount_add_many(&state
->arcs_esize
[type
],
1867 arc_hdr_size(hdr
), hdr
);
1869 for (buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
; buf
= buf
->b_next
) {
1870 if (arc_buf_is_shared(buf
))
1872 (void) refcount_add_many(&state
->arcs_esize
[type
],
1873 arc_buf_size(buf
), buf
);
1878 * Decrement the amount of evictable space in the arc_state_t's refcount.
1879 * We account for the space used by the hdr and the arc buf individually
1880 * so that we can add and remove them from the refcount individually.
1883 arc_evictable_space_decrement(arc_buf_hdr_t
*hdr
, arc_state_t
*state
)
1885 arc_buf_contents_t type
= arc_buf_type(hdr
);
1888 ASSERT(HDR_HAS_L1HDR(hdr
));
1890 if (GHOST_STATE(state
)) {
1891 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
1892 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
1893 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
1894 (void) refcount_remove_many(&state
->arcs_esize
[type
],
1895 HDR_GET_LSIZE(hdr
), hdr
);
1899 ASSERT(!GHOST_STATE(state
));
1900 if (hdr
->b_l1hdr
.b_pdata
!= NULL
) {
1901 (void) refcount_remove_many(&state
->arcs_esize
[type
],
1902 arc_hdr_size(hdr
), hdr
);
1904 for (buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
; buf
= buf
->b_next
) {
1905 if (arc_buf_is_shared(buf
))
1907 (void) refcount_remove_many(&state
->arcs_esize
[type
],
1908 arc_buf_size(buf
), buf
);
1913 * Add a reference to this hdr indicating that someone is actively
1914 * referencing that memory. When the refcount transitions from 0 to 1,
1915 * we remove it from the respective arc_state_t list to indicate that
1916 * it is not evictable.
1919 add_reference(arc_buf_hdr_t
*hdr
, void *tag
)
1923 ASSERT(HDR_HAS_L1HDR(hdr
));
1924 if (!MUTEX_HELD(HDR_LOCK(hdr
))) {
1925 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
1926 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
1927 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
1930 state
= hdr
->b_l1hdr
.b_state
;
1932 if ((refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
) == 1) &&
1933 (state
!= arc_anon
)) {
1934 /* We don't use the L2-only state list. */
1935 if (state
!= arc_l2c_only
) {
1936 multilist_remove(&state
->arcs_list
[arc_buf_type(hdr
)],
1938 arc_evictable_space_decrement(hdr
, state
);
1940 /* remove the prefetch flag if we get a reference */
1941 arc_hdr_clear_flags(hdr
, ARC_FLAG_PREFETCH
);
1946 * Remove a reference from this hdr. When the reference transitions from
1947 * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's
1948 * list making it eligible for eviction.
1951 remove_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
1954 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
1956 ASSERT(HDR_HAS_L1HDR(hdr
));
1957 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
1958 ASSERT(!GHOST_STATE(state
));
1961 * arc_l2c_only counts as a ghost state so we don't need to explicitly
1962 * check to prevent usage of the arc_l2c_only list.
1964 if (((cnt
= refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
)) == 0) &&
1965 (state
!= arc_anon
)) {
1966 multilist_insert(&state
->arcs_list
[arc_buf_type(hdr
)], hdr
);
1967 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
1968 arc_evictable_space_increment(hdr
, state
);
1974 * Returns detailed information about a specific arc buffer. When the
1975 * state_index argument is set the function will calculate the arc header
1976 * list position for its arc state. Since this requires a linear traversal
1977 * callers are strongly encourage not to do this. However, it can be helpful
1978 * for targeted analysis so the functionality is provided.
1981 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
1983 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
1984 l1arc_buf_hdr_t
*l1hdr
= NULL
;
1985 l2arc_buf_hdr_t
*l2hdr
= NULL
;
1986 arc_state_t
*state
= NULL
;
1988 memset(abi
, 0, sizeof (arc_buf_info_t
));
1993 abi
->abi_flags
= hdr
->b_flags
;
1995 if (HDR_HAS_L1HDR(hdr
)) {
1996 l1hdr
= &hdr
->b_l1hdr
;
1997 state
= l1hdr
->b_state
;
1999 if (HDR_HAS_L2HDR(hdr
))
2000 l2hdr
= &hdr
->b_l2hdr
;
2003 abi
->abi_bufcnt
= l1hdr
->b_bufcnt
;
2004 abi
->abi_access
= l1hdr
->b_arc_access
;
2005 abi
->abi_mru_hits
= l1hdr
->b_mru_hits
;
2006 abi
->abi_mru_ghost_hits
= l1hdr
->b_mru_ghost_hits
;
2007 abi
->abi_mfu_hits
= l1hdr
->b_mfu_hits
;
2008 abi
->abi_mfu_ghost_hits
= l1hdr
->b_mfu_ghost_hits
;
2009 abi
->abi_holds
= refcount_count(&l1hdr
->b_refcnt
);
2013 abi
->abi_l2arc_dattr
= l2hdr
->b_daddr
;
2014 abi
->abi_l2arc_hits
= l2hdr
->b_hits
;
2017 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
2018 abi
->abi_state_contents
= arc_buf_type(hdr
);
2019 abi
->abi_size
= arc_hdr_size(hdr
);
2023 * Move the supplied buffer to the indicated state. The hash lock
2024 * for the buffer must be held by the caller.
2027 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*hdr
,
2028 kmutex_t
*hash_lock
)
2030 arc_state_t
*old_state
;
2033 boolean_t update_old
, update_new
;
2034 arc_buf_contents_t buftype
= arc_buf_type(hdr
);
2037 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
2038 * in arc_read() when bringing a buffer out of the L2ARC. However, the
2039 * L1 hdr doesn't always exist when we change state to arc_anon before
2040 * destroying a header, in which case reallocating to add the L1 hdr is
2043 if (HDR_HAS_L1HDR(hdr
)) {
2044 old_state
= hdr
->b_l1hdr
.b_state
;
2045 refcnt
= refcount_count(&hdr
->b_l1hdr
.b_refcnt
);
2046 bufcnt
= hdr
->b_l1hdr
.b_bufcnt
;
2047 update_old
= (bufcnt
> 0 || hdr
->b_l1hdr
.b_pdata
!= NULL
);
2049 old_state
= arc_l2c_only
;
2052 update_old
= B_FALSE
;
2054 update_new
= update_old
;
2056 ASSERT(MUTEX_HELD(hash_lock
));
2057 ASSERT3P(new_state
, !=, old_state
);
2058 ASSERT(!GHOST_STATE(new_state
) || bufcnt
== 0);
2059 ASSERT(old_state
!= arc_anon
|| bufcnt
<= 1);
2062 * If this buffer is evictable, transfer it from the
2063 * old state list to the new state list.
2066 if (old_state
!= arc_anon
&& old_state
!= arc_l2c_only
) {
2067 ASSERT(HDR_HAS_L1HDR(hdr
));
2068 multilist_remove(&old_state
->arcs_list
[buftype
], hdr
);
2070 if (GHOST_STATE(old_state
)) {
2072 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2073 update_old
= B_TRUE
;
2075 arc_evictable_space_decrement(hdr
, old_state
);
2077 if (new_state
!= arc_anon
&& new_state
!= arc_l2c_only
) {
2079 * An L1 header always exists here, since if we're
2080 * moving to some L1-cached state (i.e. not l2c_only or
2081 * anonymous), we realloc the header to add an L1hdr
2084 ASSERT(HDR_HAS_L1HDR(hdr
));
2085 multilist_insert(&new_state
->arcs_list
[buftype
], hdr
);
2087 if (GHOST_STATE(new_state
)) {
2089 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2090 update_new
= B_TRUE
;
2092 arc_evictable_space_increment(hdr
, new_state
);
2096 ASSERT(!HDR_EMPTY(hdr
));
2097 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(hdr
))
2098 buf_hash_remove(hdr
);
2100 /* adjust state sizes (ignore arc_l2c_only) */
2102 if (update_new
&& new_state
!= arc_l2c_only
) {
2103 ASSERT(HDR_HAS_L1HDR(hdr
));
2104 if (GHOST_STATE(new_state
)) {
2108 * When moving a header to a ghost state, we first
2109 * remove all arc buffers. Thus, we'll have a
2110 * bufcnt of zero, and no arc buffer to use for
2111 * the reference. As a result, we use the arc
2112 * header pointer for the reference.
2114 (void) refcount_add_many(&new_state
->arcs_size
,
2115 HDR_GET_LSIZE(hdr
), hdr
);
2116 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
2119 uint32_t buffers
= 0;
2122 * Each individual buffer holds a unique reference,
2123 * thus we must remove each of these references one
2126 for (buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2127 buf
= buf
->b_next
) {
2128 ASSERT3U(bufcnt
, !=, 0);
2132 * When the arc_buf_t is sharing the data
2133 * block with the hdr, the owner of the
2134 * reference belongs to the hdr. Only
2135 * add to the refcount if the arc_buf_t is
2138 if (arc_buf_is_shared(buf
))
2141 (void) refcount_add_many(&new_state
->arcs_size
,
2142 arc_buf_size(buf
), buf
);
2144 ASSERT3U(bufcnt
, ==, buffers
);
2146 if (hdr
->b_l1hdr
.b_pdata
!= NULL
) {
2147 (void) refcount_add_many(&new_state
->arcs_size
,
2148 arc_hdr_size(hdr
), hdr
);
2150 ASSERT(GHOST_STATE(old_state
));
2155 if (update_old
&& old_state
!= arc_l2c_only
) {
2156 ASSERT(HDR_HAS_L1HDR(hdr
));
2157 if (GHOST_STATE(old_state
)) {
2159 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
2162 * When moving a header off of a ghost state,
2163 * the header will not contain any arc buffers.
2164 * We use the arc header pointer for the reference
2165 * which is exactly what we did when we put the
2166 * header on the ghost state.
2169 (void) refcount_remove_many(&old_state
->arcs_size
,
2170 HDR_GET_LSIZE(hdr
), hdr
);
2173 uint32_t buffers
= 0;
2176 * Each individual buffer holds a unique reference,
2177 * thus we must remove each of these references one
2180 for (buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
2181 buf
= buf
->b_next
) {
2182 ASSERT3U(bufcnt
, !=, 0);
2186 * When the arc_buf_t is sharing the data
2187 * block with the hdr, the owner of the
2188 * reference belongs to the hdr. Only
2189 * add to the refcount if the arc_buf_t is
2192 if (arc_buf_is_shared(buf
))
2195 (void) refcount_remove_many(
2196 &old_state
->arcs_size
, arc_buf_size(buf
),
2199 ASSERT3U(bufcnt
, ==, buffers
);
2200 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
2201 (void) refcount_remove_many(
2202 &old_state
->arcs_size
, arc_hdr_size(hdr
), hdr
);
2206 if (HDR_HAS_L1HDR(hdr
))
2207 hdr
->b_l1hdr
.b_state
= new_state
;
2210 * L2 headers should never be on the L2 state list since they don't
2211 * have L1 headers allocated.
2213 ASSERT(multilist_is_empty(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]) &&
2214 multilist_is_empty(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]));
2218 arc_space_consume(uint64_t space
, arc_space_type_t type
)
2220 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2225 case ARC_SPACE_DATA
:
2226 ARCSTAT_INCR(arcstat_data_size
, space
);
2228 case ARC_SPACE_META
:
2229 ARCSTAT_INCR(arcstat_metadata_size
, space
);
2231 case ARC_SPACE_BONUS
:
2232 ARCSTAT_INCR(arcstat_bonus_size
, space
);
2234 case ARC_SPACE_DNODE
:
2235 ARCSTAT_INCR(arcstat_dnode_size
, space
);
2237 case ARC_SPACE_DBUF
:
2238 ARCSTAT_INCR(arcstat_dbuf_size
, space
);
2240 case ARC_SPACE_HDRS
:
2241 ARCSTAT_INCR(arcstat_hdr_size
, space
);
2243 case ARC_SPACE_L2HDRS
:
2244 ARCSTAT_INCR(arcstat_l2_hdr_size
, space
);
2248 if (type
!= ARC_SPACE_DATA
)
2249 ARCSTAT_INCR(arcstat_meta_used
, space
);
2251 atomic_add_64(&arc_size
, space
);
2255 arc_space_return(uint64_t space
, arc_space_type_t type
)
2257 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
2262 case ARC_SPACE_DATA
:
2263 ARCSTAT_INCR(arcstat_data_size
, -space
);
2265 case ARC_SPACE_META
:
2266 ARCSTAT_INCR(arcstat_metadata_size
, -space
);
2268 case ARC_SPACE_BONUS
:
2269 ARCSTAT_INCR(arcstat_bonus_size
, -space
);
2271 case ARC_SPACE_DNODE
:
2272 ARCSTAT_INCR(arcstat_dnode_size
, -space
);
2274 case ARC_SPACE_DBUF
:
2275 ARCSTAT_INCR(arcstat_dbuf_size
, -space
);
2277 case ARC_SPACE_HDRS
:
2278 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
2280 case ARC_SPACE_L2HDRS
:
2281 ARCSTAT_INCR(arcstat_l2_hdr_size
, -space
);
2285 if (type
!= ARC_SPACE_DATA
) {
2286 ASSERT(arc_meta_used
>= space
);
2287 if (arc_meta_max
< arc_meta_used
)
2288 arc_meta_max
= arc_meta_used
;
2289 ARCSTAT_INCR(arcstat_meta_used
, -space
);
2292 ASSERT(arc_size
>= space
);
2293 atomic_add_64(&arc_size
, -space
);
2297 * Given a hdr and a buf, returns whether that buf can share its b_data buffer
2298 * with the hdr's b_pdata.
2301 arc_can_share(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2303 boolean_t hdr_compressed
, buf_compressed
;
2305 * The criteria for sharing a hdr's data are:
2306 * 1. the hdr's compression matches the buf's compression
2307 * 2. the hdr doesn't need to be byteswapped
2308 * 3. the hdr isn't already being shared
2309 * 4. the buf is either compressed or it is the last buf in the hdr list
2311 * Criterion #4 maintains the invariant that shared uncompressed
2312 * bufs must be the final buf in the hdr's b_buf list. Reading this, you
2313 * might ask, "if a compressed buf is allocated first, won't that be the
2314 * last thing in the list?", but in that case it's impossible to create
2315 * a shared uncompressed buf anyway (because the hdr must be compressed
2316 * to have the compressed buf). You might also think that #3 is
2317 * sufficient to make this guarantee, however it's possible
2318 * (specifically in the rare L2ARC write race mentioned in
2319 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that
2320 * is sharable, but wasn't at the time of its allocation. Rather than
2321 * allow a new shared uncompressed buf to be created and then shuffle
2322 * the list around to make it the last element, this simply disallows
2323 * sharing if the new buf isn't the first to be added.
2325 ASSERT3P(buf
->b_hdr
, ==, hdr
);
2326 hdr_compressed
= HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
;
2327 buf_compressed
= ARC_BUF_COMPRESSED(buf
) != 0;
2328 return (buf_compressed
== hdr_compressed
&&
2329 hdr
->b_l1hdr
.b_byteswap
== DMU_BSWAP_NUMFUNCS
&&
2330 !HDR_SHARED_DATA(hdr
) &&
2331 (ARC_BUF_LAST(buf
) || ARC_BUF_COMPRESSED(buf
)));
2335 * Allocate a buf for this hdr. If you care about the data that's in the hdr,
2336 * or if you want a compressed buffer, pass those flags in. Returns 0 if the
2337 * copy was made successfully, or an error code otherwise.
2340 arc_buf_alloc_impl(arc_buf_hdr_t
*hdr
, void *tag
, boolean_t compressed
,
2341 boolean_t fill
, arc_buf_t
**ret
)
2344 boolean_t can_share
;
2346 ASSERT(HDR_HAS_L1HDR(hdr
));
2347 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
2348 VERIFY(hdr
->b_type
== ARC_BUFC_DATA
||
2349 hdr
->b_type
== ARC_BUFC_METADATA
);
2350 ASSERT3P(ret
, !=, NULL
);
2351 ASSERT3P(*ret
, ==, NULL
);
2353 hdr
->b_l1hdr
.b_mru_hits
= 0;
2354 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
2355 hdr
->b_l1hdr
.b_mfu_hits
= 0;
2356 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
2357 hdr
->b_l1hdr
.b_l2_hits
= 0;
2359 buf
= *ret
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
2362 buf
->b_next
= hdr
->b_l1hdr
.b_buf
;
2365 add_reference(hdr
, tag
);
2368 * We're about to change the hdr's b_flags. We must either
2369 * hold the hash_lock or be undiscoverable.
2371 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
2374 * Only honor requests for compressed bufs if the hdr is actually
2377 if (compressed
&& HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
)
2378 buf
->b_flags
|= ARC_BUF_FLAG_COMPRESSED
;
2381 * Although the ARC should handle it correctly, levels above the ARC
2382 * should prevent us from having multiple compressed bufs off the same
2383 * hdr. To ensure we notice it if this behavior changes, we assert this
2384 * here the best we can.
2386 IMPLY(ARC_BUF_COMPRESSED(buf
), !HDR_SHARED_DATA(hdr
));
2389 * If the hdr's data can be shared then we share the data buffer and
2390 * set the appropriate bit in the hdr's b_flags to indicate the hdr is
2391 * allocate a new buffer to store the buf's data.
2393 * There is one additional restriction here because we're sharing
2394 * hdr -> buf instead of the usual buf -> hdr: the hdr can't be actively
2395 * involved in an L2ARC write, because if this buf is used by an
2396 * arc_write() then the hdr's data buffer will be released when the
2397 * write completes, even though the L2ARC write might still be using it.
2399 can_share
= arc_can_share(hdr
, buf
) && !HDR_L2_WRITING(hdr
);
2401 /* Set up b_data and sharing */
2403 buf
->b_data
= hdr
->b_l1hdr
.b_pdata
;
2404 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2405 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2408 arc_get_data_buf(hdr
, arc_buf_size(buf
), buf
);
2409 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
2411 VERIFY3P(buf
->b_data
, !=, NULL
);
2413 hdr
->b_l1hdr
.b_buf
= buf
;
2414 hdr
->b_l1hdr
.b_bufcnt
+= 1;
2417 * If the user wants the data from the hdr, we need to either copy or
2418 * decompress the data.
2421 return (arc_buf_fill(buf
, ARC_BUF_COMPRESSED(buf
) != 0));
2427 static char *arc_onloan_tag
= "onloan";
2430 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
2431 * flight data by arc_tempreserve_space() until they are "returned". Loaned
2432 * buffers must be returned to the arc before they can be used by the DMU or
2436 arc_loan_buf(spa_t
*spa
, boolean_t is_metadata
, int size
)
2438 arc_buf_t
*buf
= arc_alloc_buf(spa
, arc_onloan_tag
,
2439 is_metadata
? ARC_BUFC_METADATA
: ARC_BUFC_DATA
, size
);
2441 atomic_add_64(&arc_loaned_bytes
, size
);
2446 arc_loan_compressed_buf(spa_t
*spa
, uint64_t psize
, uint64_t lsize
,
2447 enum zio_compress compression_type
)
2449 arc_buf_t
*buf
= arc_alloc_compressed_buf(spa
, arc_onloan_tag
,
2450 psize
, lsize
, compression_type
);
2452 atomic_add_64(&arc_loaned_bytes
, psize
);
2458 * Return a loaned arc buffer to the arc.
2461 arc_return_buf(arc_buf_t
*buf
, void *tag
)
2463 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2465 ASSERT3P(buf
->b_data
, !=, NULL
);
2466 ASSERT(HDR_HAS_L1HDR(hdr
));
2467 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2468 (void) refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2470 atomic_add_64(&arc_loaned_bytes
, -arc_buf_size(buf
));
2473 /* Detach an arc_buf from a dbuf (tag) */
2475 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
2477 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2479 ASSERT3P(buf
->b_data
, !=, NULL
);
2480 ASSERT(HDR_HAS_L1HDR(hdr
));
2481 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
2482 (void) refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
);
2484 atomic_add_64(&arc_loaned_bytes
, -arc_buf_size(buf
));
2488 l2arc_free_data_on_write(void *data
, size_t size
, arc_buf_contents_t type
)
2490 l2arc_data_free_t
*df
= kmem_alloc(sizeof (*df
), KM_SLEEP
);
2492 df
->l2df_data
= data
;
2493 df
->l2df_size
= size
;
2494 df
->l2df_type
= type
;
2495 mutex_enter(&l2arc_free_on_write_mtx
);
2496 list_insert_head(l2arc_free_on_write
, df
);
2497 mutex_exit(&l2arc_free_on_write_mtx
);
2501 arc_hdr_free_on_write(arc_buf_hdr_t
*hdr
)
2503 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
2504 arc_buf_contents_t type
= arc_buf_type(hdr
);
2505 uint64_t size
= arc_hdr_size(hdr
);
2507 /* protected by hash lock, if in the hash table */
2508 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
2509 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2510 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
2512 (void) refcount_remove_many(&state
->arcs_esize
[type
],
2515 (void) refcount_remove_many(&state
->arcs_size
, size
, hdr
);
2517 l2arc_free_data_on_write(hdr
->b_l1hdr
.b_pdata
, size
, type
);
2521 * Share the arc_buf_t's data with the hdr. Whenever we are sharing the
2522 * data buffer, we transfer the refcount ownership to the hdr and update
2523 * the appropriate kstats.
2526 arc_share_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2528 ASSERT(arc_can_share(hdr
, buf
));
2529 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
2530 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
2533 * Start sharing the data buffer. We transfer the
2534 * refcount ownership to the hdr since it always owns
2535 * the refcount whenever an arc_buf_t is shared.
2537 refcount_transfer_ownership(&hdr
->b_l1hdr
.b_state
->arcs_size
, buf
, hdr
);
2538 hdr
->b_l1hdr
.b_pdata
= buf
->b_data
;
2539 arc_hdr_set_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2540 buf
->b_flags
|= ARC_BUF_FLAG_SHARED
;
2543 * Since we've transferred ownership to the hdr we need
2544 * to increment its compressed and uncompressed kstats and
2545 * decrement the overhead size.
2547 ARCSTAT_INCR(arcstat_compressed_size
, arc_hdr_size(hdr
));
2548 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
2549 ARCSTAT_INCR(arcstat_overhead_size
, -arc_buf_size(buf
));
2553 arc_unshare_buf(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2555 ASSERT(arc_buf_is_shared(buf
));
2556 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
2557 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
2560 * We are no longer sharing this buffer so we need
2561 * to transfer its ownership to the rightful owner.
2563 refcount_transfer_ownership(&hdr
->b_l1hdr
.b_state
->arcs_size
, hdr
, buf
);
2564 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2565 hdr
->b_l1hdr
.b_pdata
= NULL
;
2566 buf
->b_flags
&= ~ARC_BUF_FLAG_SHARED
;
2569 * Since the buffer is no longer shared between
2570 * the arc buf and the hdr, count it as overhead.
2572 ARCSTAT_INCR(arcstat_compressed_size
, -arc_hdr_size(hdr
));
2573 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
2574 ARCSTAT_INCR(arcstat_overhead_size
, arc_buf_size(buf
));
2578 * Remove an arc_buf_t from the hdr's buf list and return the last
2579 * arc_buf_t on the list. If no buffers remain on the list then return
2583 arc_buf_remove(arc_buf_hdr_t
*hdr
, arc_buf_t
*buf
)
2585 arc_buf_t
**bufp
= &hdr
->b_l1hdr
.b_buf
;
2586 arc_buf_t
*lastbuf
= NULL
;
2588 ASSERT(HDR_HAS_L1HDR(hdr
));
2589 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
2592 * Remove the buf from the hdr list and locate the last
2593 * remaining buffer on the list.
2595 while (*bufp
!= NULL
) {
2597 *bufp
= buf
->b_next
;
2600 * If we've removed a buffer in the middle of
2601 * the list then update the lastbuf and update
2604 if (*bufp
!= NULL
) {
2606 bufp
= &(*bufp
)->b_next
;
2610 ASSERT3P(lastbuf
, !=, buf
);
2611 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, lastbuf
!= NULL
);
2612 IMPLY(hdr
->b_l1hdr
.b_bufcnt
> 0, hdr
->b_l1hdr
.b_buf
!= NULL
);
2613 IMPLY(lastbuf
!= NULL
, ARC_BUF_LAST(lastbuf
));
2619 * Free up buf->b_data and pull the arc_buf_t off of the the arc_buf_hdr_t's
2623 arc_buf_destroy_impl(arc_buf_t
*buf
)
2626 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2629 * Free up the data associated with the buf but only if we're not
2630 * sharing this with the hdr. If we are sharing it with the hdr, the
2631 * hdr is responsible for doing the free.
2633 if (buf
->b_data
!= NULL
) {
2635 * We're about to change the hdr's b_flags. We must either
2636 * hold the hash_lock or be undiscoverable.
2638 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)) || HDR_EMPTY(hdr
));
2640 arc_cksum_verify(buf
);
2641 arc_buf_unwatch(buf
);
2643 if (arc_buf_is_shared(buf
)) {
2644 arc_hdr_clear_flags(hdr
, ARC_FLAG_SHARED_DATA
);
2646 uint64_t size
= arc_buf_size(buf
);
2647 arc_free_data_buf(hdr
, buf
->b_data
, size
, buf
);
2648 ARCSTAT_INCR(arcstat_overhead_size
, -size
);
2652 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
2653 hdr
->b_l1hdr
.b_bufcnt
-= 1;
2656 lastbuf
= arc_buf_remove(hdr
, buf
);
2658 if (ARC_BUF_SHARED(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
2660 * If the current arc_buf_t is sharing its data buffer with the
2661 * hdr, then reassign the hdr's b_pdata to share it with the new
2662 * buffer at the end of the list. The shared buffer is always
2663 * the last one on the hdr's buffer list.
2665 * There is an equivalent case for compressed bufs, but since
2666 * they aren't guaranteed to be the last buf in the list and
2667 * that is an exceedingly rare case, we just allow that space be
2668 * wasted temporarily.
2670 if (lastbuf
!= NULL
) {
2671 /* Only one buf can be shared at once */
2672 VERIFY(!arc_buf_is_shared(lastbuf
));
2673 /* hdr is uncompressed so can't have compressed buf */
2674 VERIFY(!ARC_BUF_COMPRESSED(lastbuf
));
2676 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
2677 arc_hdr_free_pdata(hdr
);
2680 * We must setup a new shared block between the
2681 * last buffer and the hdr. The data would have
2682 * been allocated by the arc buf so we need to transfer
2683 * ownership to the hdr since it's now being shared.
2685 arc_share_buf(hdr
, lastbuf
);
2687 } else if (HDR_SHARED_DATA(hdr
)) {
2689 * Uncompressed shared buffers are always at the end
2690 * of the list. Compressed buffers don't have the
2691 * same requirements. This makes it hard to
2692 * simply assert that the lastbuf is shared so
2693 * we rely on the hdr's compression flags to determine
2694 * if we have a compressed, shared buffer.
2696 ASSERT3P(lastbuf
, !=, NULL
);
2697 ASSERT(arc_buf_is_shared(lastbuf
) ||
2698 HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
);
2701 if (hdr
->b_l1hdr
.b_bufcnt
== 0)
2702 arc_cksum_free(hdr
);
2704 /* clean up the buf */
2706 kmem_cache_free(buf_cache
, buf
);
2710 arc_hdr_alloc_pdata(arc_buf_hdr_t
*hdr
)
2712 ASSERT3U(HDR_GET_LSIZE(hdr
), >, 0);
2713 ASSERT(HDR_HAS_L1HDR(hdr
));
2714 ASSERT(!HDR_SHARED_DATA(hdr
));
2716 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
2717 hdr
->b_l1hdr
.b_pdata
= arc_get_data_buf(hdr
, arc_hdr_size(hdr
), hdr
);
2718 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
2719 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
2721 ARCSTAT_INCR(arcstat_compressed_size
, arc_hdr_size(hdr
));
2722 ARCSTAT_INCR(arcstat_uncompressed_size
, HDR_GET_LSIZE(hdr
));
2726 arc_hdr_free_pdata(arc_buf_hdr_t
*hdr
)
2728 ASSERT(HDR_HAS_L1HDR(hdr
));
2729 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
2732 * If the hdr is currently being written to the l2arc then
2733 * we defer freeing the data by adding it to the l2arc_free_on_write
2734 * list. The l2arc will free the data once it's finished
2735 * writing it to the l2arc device.
2737 if (HDR_L2_WRITING(hdr
)) {
2738 arc_hdr_free_on_write(hdr
);
2739 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
2741 arc_free_data_buf(hdr
, hdr
->b_l1hdr
.b_pdata
,
2742 arc_hdr_size(hdr
), hdr
);
2744 hdr
->b_l1hdr
.b_pdata
= NULL
;
2745 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
2747 ARCSTAT_INCR(arcstat_compressed_size
, -arc_hdr_size(hdr
));
2748 ARCSTAT_INCR(arcstat_uncompressed_size
, -HDR_GET_LSIZE(hdr
));
2751 static arc_buf_hdr_t
*
2752 arc_hdr_alloc(uint64_t spa
, int32_t psize
, int32_t lsize
,
2753 enum zio_compress compression_type
, arc_buf_contents_t type
)
2757 VERIFY(type
== ARC_BUFC_DATA
|| type
== ARC_BUFC_METADATA
);
2759 hdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
2760 ASSERT(HDR_EMPTY(hdr
));
2761 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
2762 HDR_SET_PSIZE(hdr
, psize
);
2763 HDR_SET_LSIZE(hdr
, lsize
);
2767 arc_hdr_set_flags(hdr
, arc_bufc_to_flags(type
) | ARC_FLAG_HAS_L1HDR
);
2768 arc_hdr_set_compress(hdr
, compression_type
);
2770 hdr
->b_l1hdr
.b_state
= arc_anon
;
2771 hdr
->b_l1hdr
.b_arc_access
= 0;
2772 hdr
->b_l1hdr
.b_bufcnt
= 0;
2773 hdr
->b_l1hdr
.b_buf
= NULL
;
2776 * Allocate the hdr's buffer. This will contain either
2777 * the compressed or uncompressed data depending on the block
2778 * it references and compressed arc enablement.
2780 arc_hdr_alloc_pdata(hdr
);
2781 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2787 * Transition between the two allocation states for the arc_buf_hdr struct.
2788 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
2789 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
2790 * version is used when a cache buffer is only in the L2ARC in order to reduce
2793 static arc_buf_hdr_t
*
2794 arc_hdr_realloc(arc_buf_hdr_t
*hdr
, kmem_cache_t
*old
, kmem_cache_t
*new)
2796 arc_buf_hdr_t
*nhdr
;
2797 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
2799 ASSERT(HDR_HAS_L2HDR(hdr
));
2800 ASSERT((old
== hdr_full_cache
&& new == hdr_l2only_cache
) ||
2801 (old
== hdr_l2only_cache
&& new == hdr_full_cache
));
2803 nhdr
= kmem_cache_alloc(new, KM_PUSHPAGE
);
2805 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
2806 buf_hash_remove(hdr
);
2808 bcopy(hdr
, nhdr
, HDR_L2ONLY_SIZE
);
2810 if (new == hdr_full_cache
) {
2811 arc_hdr_set_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
2813 * arc_access and arc_change_state need to be aware that a
2814 * header has just come out of L2ARC, so we set its state to
2815 * l2c_only even though it's about to change.
2817 nhdr
->b_l1hdr
.b_state
= arc_l2c_only
;
2819 /* Verify previous threads set to NULL before freeing */
2820 ASSERT3P(nhdr
->b_l1hdr
.b_pdata
, ==, NULL
);
2822 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
2823 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
2824 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
2827 * If we've reached here, We must have been called from
2828 * arc_evict_hdr(), as such we should have already been
2829 * removed from any ghost list we were previously on
2830 * (which protects us from racing with arc_evict_state),
2831 * thus no locking is needed during this check.
2833 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
2836 * A buffer must not be moved into the arc_l2c_only
2837 * state if it's not finished being written out to the
2838 * l2arc device. Otherwise, the b_l1hdr.b_pdata field
2839 * might try to be accessed, even though it was removed.
2841 VERIFY(!HDR_L2_WRITING(hdr
));
2842 VERIFY3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
2844 arc_hdr_clear_flags(nhdr
, ARC_FLAG_HAS_L1HDR
);
2847 * The header has been reallocated so we need to re-insert it into any
2850 (void) buf_hash_insert(nhdr
, NULL
);
2852 ASSERT(list_link_active(&hdr
->b_l2hdr
.b_l2node
));
2854 mutex_enter(&dev
->l2ad_mtx
);
2857 * We must place the realloc'ed header back into the list at
2858 * the same spot. Otherwise, if it's placed earlier in the list,
2859 * l2arc_write_buffers() could find it during the function's
2860 * write phase, and try to write it out to the l2arc.
2862 list_insert_after(&dev
->l2ad_buflist
, hdr
, nhdr
);
2863 list_remove(&dev
->l2ad_buflist
, hdr
);
2865 mutex_exit(&dev
->l2ad_mtx
);
2868 * Since we're using the pointer address as the tag when
2869 * incrementing and decrementing the l2ad_alloc refcount, we
2870 * must remove the old pointer (that we're about to destroy) and
2871 * add the new pointer to the refcount. Otherwise we'd remove
2872 * the wrong pointer address when calling arc_hdr_destroy() later.
2875 (void) refcount_remove_many(&dev
->l2ad_alloc
, arc_hdr_size(hdr
), hdr
);
2876 (void) refcount_add_many(&dev
->l2ad_alloc
, arc_hdr_size(nhdr
), nhdr
);
2878 buf_discard_identity(hdr
);
2879 kmem_cache_free(old
, hdr
);
2885 * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller.
2886 * The buf is returned thawed since we expect the consumer to modify it.
2889 arc_alloc_buf(spa_t
*spa
, void *tag
, arc_buf_contents_t type
, int32_t size
)
2892 arc_buf_hdr_t
*hdr
= arc_hdr_alloc(spa_load_guid(spa
), size
, size
,
2893 ZIO_COMPRESS_OFF
, type
);
2894 ASSERT(!MUTEX_HELD(HDR_LOCK(hdr
)));
2897 VERIFY0(arc_buf_alloc_impl(hdr
, tag
, B_FALSE
, B_FALSE
, &buf
));
2904 * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this
2905 * for bufs containing metadata.
2908 arc_alloc_compressed_buf(spa_t
*spa
, void *tag
, uint64_t psize
, uint64_t lsize
,
2909 enum zio_compress compression_type
)
2913 ASSERT3U(lsize
, >, 0);
2914 ASSERT3U(lsize
, >=, psize
);
2915 ASSERT(compression_type
> ZIO_COMPRESS_OFF
);
2916 ASSERT(compression_type
< ZIO_COMPRESS_FUNCTIONS
);
2918 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
2919 compression_type
, ARC_BUFC_DATA
);
2920 ASSERT(!MUTEX_HELD(HDR_LOCK(hdr
)));
2923 VERIFY0(arc_buf_alloc_impl(hdr
, tag
, B_TRUE
, B_FALSE
, &buf
));
2925 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
2931 arc_hdr_l2hdr_destroy(arc_buf_hdr_t
*hdr
)
2933 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
2934 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
2935 uint64_t asize
= arc_hdr_size(hdr
);
2937 ASSERT(MUTEX_HELD(&dev
->l2ad_mtx
));
2938 ASSERT(HDR_HAS_L2HDR(hdr
));
2940 list_remove(&dev
->l2ad_buflist
, hdr
);
2942 ARCSTAT_INCR(arcstat_l2_asize
, -asize
);
2943 ARCSTAT_INCR(arcstat_l2_size
, -HDR_GET_LSIZE(hdr
));
2945 vdev_space_update(dev
->l2ad_vdev
, -asize
, 0, 0);
2947 (void) refcount_remove_many(&dev
->l2ad_alloc
, asize
, hdr
);
2948 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
2952 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
2954 if (HDR_HAS_L1HDR(hdr
)) {
2955 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
||
2956 hdr
->b_l1hdr
.b_bufcnt
> 0);
2957 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2958 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
2960 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
2961 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
2963 if (!HDR_EMPTY(hdr
))
2964 buf_discard_identity(hdr
);
2966 if (HDR_HAS_L2HDR(hdr
)) {
2967 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
2968 boolean_t buflist_held
= MUTEX_HELD(&dev
->l2ad_mtx
);
2971 mutex_enter(&dev
->l2ad_mtx
);
2974 * Even though we checked this conditional above, we
2975 * need to check this again now that we have the
2976 * l2ad_mtx. This is because we could be racing with
2977 * another thread calling l2arc_evict() which might have
2978 * destroyed this header's L2 portion as we were waiting
2979 * to acquire the l2ad_mtx. If that happens, we don't
2980 * want to re-destroy the header's L2 portion.
2982 if (HDR_HAS_L2HDR(hdr
))
2983 arc_hdr_l2hdr_destroy(hdr
);
2986 mutex_exit(&dev
->l2ad_mtx
);
2989 if (HDR_HAS_L1HDR(hdr
)) {
2990 arc_cksum_free(hdr
);
2992 while (hdr
->b_l1hdr
.b_buf
!= NULL
)
2993 arc_buf_destroy_impl(hdr
->b_l1hdr
.b_buf
);
2995 if (hdr
->b_l1hdr
.b_pdata
!= NULL
) {
2996 arc_hdr_free_pdata(hdr
);
3000 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
3001 if (HDR_HAS_L1HDR(hdr
)) {
3002 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3003 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
3004 kmem_cache_free(hdr_full_cache
, hdr
);
3006 kmem_cache_free(hdr_l2only_cache
, hdr
);
3011 arc_buf_destroy(arc_buf_t
*buf
, void* tag
)
3013 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3014 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
3016 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
3017 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
3018 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3019 VERIFY0(remove_reference(hdr
, NULL
, tag
));
3020 arc_hdr_destroy(hdr
);
3024 mutex_enter(hash_lock
);
3025 ASSERT3P(hdr
, ==, buf
->b_hdr
);
3026 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
3027 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3028 ASSERT3P(hdr
->b_l1hdr
.b_state
, !=, arc_anon
);
3029 ASSERT3P(buf
->b_data
, !=, NULL
);
3031 (void) remove_reference(hdr
, hash_lock
, tag
);
3032 arc_buf_destroy_impl(buf
);
3033 mutex_exit(hash_lock
);
3037 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
3038 * state of the header is dependent on its state prior to entering this
3039 * function. The following transitions are possible:
3041 * - arc_mru -> arc_mru_ghost
3042 * - arc_mfu -> arc_mfu_ghost
3043 * - arc_mru_ghost -> arc_l2c_only
3044 * - arc_mru_ghost -> deleted
3045 * - arc_mfu_ghost -> arc_l2c_only
3046 * - arc_mfu_ghost -> deleted
3049 arc_evict_hdr(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
3051 arc_state_t
*evicted_state
, *state
;
3052 int64_t bytes_evicted
= 0;
3054 ASSERT(MUTEX_HELD(hash_lock
));
3055 ASSERT(HDR_HAS_L1HDR(hdr
));
3057 state
= hdr
->b_l1hdr
.b_state
;
3058 if (GHOST_STATE(state
)) {
3059 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3060 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
3063 * l2arc_write_buffers() relies on a header's L1 portion
3064 * (i.e. its b_pdata field) during its write phase.
3065 * Thus, we cannot push a header onto the arc_l2c_only
3066 * state (removing its L1 piece) until the header is
3067 * done being written to the l2arc.
3069 if (HDR_HAS_L2HDR(hdr
) && HDR_L2_WRITING(hdr
)) {
3070 ARCSTAT_BUMP(arcstat_evict_l2_skip
);
3071 return (bytes_evicted
);
3074 ARCSTAT_BUMP(arcstat_deleted
);
3075 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3077 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, hdr
);
3079 if (HDR_HAS_L2HDR(hdr
)) {
3080 ASSERT(hdr
->b_l1hdr
.b_pdata
== NULL
);
3082 * This buffer is cached on the 2nd Level ARC;
3083 * don't destroy the header.
3085 arc_change_state(arc_l2c_only
, hdr
, hash_lock
);
3087 * dropping from L1+L2 cached to L2-only,
3088 * realloc to remove the L1 header.
3090 hdr
= arc_hdr_realloc(hdr
, hdr_full_cache
,
3093 arc_change_state(arc_anon
, hdr
, hash_lock
);
3094 arc_hdr_destroy(hdr
);
3096 return (bytes_evicted
);
3099 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
3100 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
3102 /* prefetch buffers have a minimum lifespan */
3103 if (HDR_IO_IN_PROGRESS(hdr
) ||
3104 ((hdr
->b_flags
& (ARC_FLAG_PREFETCH
| ARC_FLAG_INDIRECT
)) &&
3105 ddi_get_lbolt() - hdr
->b_l1hdr
.b_arc_access
<
3106 arc_min_prefetch_lifespan
)) {
3107 ARCSTAT_BUMP(arcstat_evict_skip
);
3108 return (bytes_evicted
);
3111 ASSERT0(refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3112 while (hdr
->b_l1hdr
.b_buf
) {
3113 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
3114 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
3115 ARCSTAT_BUMP(arcstat_mutex_miss
);
3118 if (buf
->b_data
!= NULL
)
3119 bytes_evicted
+= HDR_GET_LSIZE(hdr
);
3120 mutex_exit(&buf
->b_evict_lock
);
3121 arc_buf_destroy_impl(buf
);
3124 if (HDR_HAS_L2HDR(hdr
)) {
3125 ARCSTAT_INCR(arcstat_evict_l2_cached
, HDR_GET_LSIZE(hdr
));
3127 if (l2arc_write_eligible(hdr
->b_spa
, hdr
)) {
3128 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
3129 HDR_GET_LSIZE(hdr
));
3131 ARCSTAT_INCR(arcstat_evict_l2_ineligible
,
3132 HDR_GET_LSIZE(hdr
));
3136 if (hdr
->b_l1hdr
.b_bufcnt
== 0) {
3137 arc_cksum_free(hdr
);
3139 bytes_evicted
+= arc_hdr_size(hdr
);
3142 * If this hdr is being evicted and has a compressed
3143 * buffer then we discard it here before we change states.
3144 * This ensures that the accounting is updated correctly
3145 * in arc_free_data_buf().
3147 arc_hdr_free_pdata(hdr
);
3149 arc_change_state(evicted_state
, hdr
, hash_lock
);
3150 ASSERT(HDR_IN_HASH_TABLE(hdr
));
3151 arc_hdr_set_flags(hdr
, ARC_FLAG_IN_HASH_TABLE
);
3152 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, hdr
);
3155 return (bytes_evicted
);
3159 arc_evict_state_impl(multilist_t
*ml
, int idx
, arc_buf_hdr_t
*marker
,
3160 uint64_t spa
, int64_t bytes
)
3162 multilist_sublist_t
*mls
;
3163 uint64_t bytes_evicted
= 0;
3165 kmutex_t
*hash_lock
;
3166 int evict_count
= 0;
3168 ASSERT3P(marker
, !=, NULL
);
3169 IMPLY(bytes
< 0, bytes
== ARC_EVICT_ALL
);
3171 mls
= multilist_sublist_lock(ml
, idx
);
3173 for (hdr
= multilist_sublist_prev(mls
, marker
); hdr
!= NULL
;
3174 hdr
= multilist_sublist_prev(mls
, marker
)) {
3175 if ((bytes
!= ARC_EVICT_ALL
&& bytes_evicted
>= bytes
) ||
3176 (evict_count
>= zfs_arc_evict_batch_limit
))
3180 * To keep our iteration location, move the marker
3181 * forward. Since we're not holding hdr's hash lock, we
3182 * must be very careful and not remove 'hdr' from the
3183 * sublist. Otherwise, other consumers might mistake the
3184 * 'hdr' as not being on a sublist when they call the
3185 * multilist_link_active() function (they all rely on
3186 * the hash lock protecting concurrent insertions and
3187 * removals). multilist_sublist_move_forward() was
3188 * specifically implemented to ensure this is the case
3189 * (only 'marker' will be removed and re-inserted).
3191 multilist_sublist_move_forward(mls
, marker
);
3194 * The only case where the b_spa field should ever be
3195 * zero, is the marker headers inserted by
3196 * arc_evict_state(). It's possible for multiple threads
3197 * to be calling arc_evict_state() concurrently (e.g.
3198 * dsl_pool_close() and zio_inject_fault()), so we must
3199 * skip any markers we see from these other threads.
3201 if (hdr
->b_spa
== 0)
3204 /* we're only interested in evicting buffers of a certain spa */
3205 if (spa
!= 0 && hdr
->b_spa
!= spa
) {
3206 ARCSTAT_BUMP(arcstat_evict_skip
);
3210 hash_lock
= HDR_LOCK(hdr
);
3213 * We aren't calling this function from any code path
3214 * that would already be holding a hash lock, so we're
3215 * asserting on this assumption to be defensive in case
3216 * this ever changes. Without this check, it would be
3217 * possible to incorrectly increment arcstat_mutex_miss
3218 * below (e.g. if the code changed such that we called
3219 * this function with a hash lock held).
3221 ASSERT(!MUTEX_HELD(hash_lock
));
3223 if (mutex_tryenter(hash_lock
)) {
3224 uint64_t evicted
= arc_evict_hdr(hdr
, hash_lock
);
3225 mutex_exit(hash_lock
);
3227 bytes_evicted
+= evicted
;
3230 * If evicted is zero, arc_evict_hdr() must have
3231 * decided to skip this header, don't increment
3232 * evict_count in this case.
3238 * If arc_size isn't overflowing, signal any
3239 * threads that might happen to be waiting.
3241 * For each header evicted, we wake up a single
3242 * thread. If we used cv_broadcast, we could
3243 * wake up "too many" threads causing arc_size
3244 * to significantly overflow arc_c; since
3245 * arc_get_data_buf() doesn't check for overflow
3246 * when it's woken up (it doesn't because it's
3247 * possible for the ARC to be overflowing while
3248 * full of un-evictable buffers, and the
3249 * function should proceed in this case).
3251 * If threads are left sleeping, due to not
3252 * using cv_broadcast, they will be woken up
3253 * just before arc_reclaim_thread() sleeps.
3255 mutex_enter(&arc_reclaim_lock
);
3256 if (!arc_is_overflowing())
3257 cv_signal(&arc_reclaim_waiters_cv
);
3258 mutex_exit(&arc_reclaim_lock
);
3260 ARCSTAT_BUMP(arcstat_mutex_miss
);
3264 multilist_sublist_unlock(mls
);
3266 return (bytes_evicted
);
3270 * Evict buffers from the given arc state, until we've removed the
3271 * specified number of bytes. Move the removed buffers to the
3272 * appropriate evict state.
3274 * This function makes a "best effort". It skips over any buffers
3275 * it can't get a hash_lock on, and so, may not catch all candidates.
3276 * It may also return without evicting as much space as requested.
3278 * If bytes is specified using the special value ARC_EVICT_ALL, this
3279 * will evict all available (i.e. unlocked and evictable) buffers from
3280 * the given arc state; which is used by arc_flush().
3283 arc_evict_state(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
3284 arc_buf_contents_t type
)
3286 uint64_t total_evicted
= 0;
3287 multilist_t
*ml
= &state
->arcs_list
[type
];
3289 arc_buf_hdr_t
**markers
;
3292 IMPLY(bytes
< 0, bytes
== ARC_EVICT_ALL
);
3294 num_sublists
= multilist_get_num_sublists(ml
);
3297 * If we've tried to evict from each sublist, made some
3298 * progress, but still have not hit the target number of bytes
3299 * to evict, we want to keep trying. The markers allow us to
3300 * pick up where we left off for each individual sublist, rather
3301 * than starting from the tail each time.
3303 markers
= kmem_zalloc(sizeof (*markers
) * num_sublists
, KM_SLEEP
);
3304 for (i
= 0; i
< num_sublists
; i
++) {
3305 multilist_sublist_t
*mls
;
3307 markers
[i
] = kmem_cache_alloc(hdr_full_cache
, KM_SLEEP
);
3310 * A b_spa of 0 is used to indicate that this header is
3311 * a marker. This fact is used in arc_adjust_type() and
3312 * arc_evict_state_impl().
3314 markers
[i
]->b_spa
= 0;
3316 mls
= multilist_sublist_lock(ml
, i
);
3317 multilist_sublist_insert_tail(mls
, markers
[i
]);
3318 multilist_sublist_unlock(mls
);
3322 * While we haven't hit our target number of bytes to evict, or
3323 * we're evicting all available buffers.
3325 while (total_evicted
< bytes
|| bytes
== ARC_EVICT_ALL
) {
3326 int sublist_idx
= multilist_get_random_index(ml
);
3327 uint64_t scan_evicted
= 0;
3330 * Try to reduce pinned dnodes with a floor of arc_dnode_limit.
3331 * Request that 10% of the LRUs be scanned by the superblock
3334 if (type
== ARC_BUFC_DATA
&& arc_dnode_size
> arc_dnode_limit
)
3335 arc_prune_async((arc_dnode_size
- arc_dnode_limit
) /
3336 sizeof (dnode_t
) / zfs_arc_dnode_reduce_percent
);
3339 * Start eviction using a randomly selected sublist,
3340 * this is to try and evenly balance eviction across all
3341 * sublists. Always starting at the same sublist
3342 * (e.g. index 0) would cause evictions to favor certain
3343 * sublists over others.
3345 for (i
= 0; i
< num_sublists
; i
++) {
3346 uint64_t bytes_remaining
;
3347 uint64_t bytes_evicted
;
3349 if (bytes
== ARC_EVICT_ALL
)
3350 bytes_remaining
= ARC_EVICT_ALL
;
3351 else if (total_evicted
< bytes
)
3352 bytes_remaining
= bytes
- total_evicted
;
3356 bytes_evicted
= arc_evict_state_impl(ml
, sublist_idx
,
3357 markers
[sublist_idx
], spa
, bytes_remaining
);
3359 scan_evicted
+= bytes_evicted
;
3360 total_evicted
+= bytes_evicted
;
3362 /* we've reached the end, wrap to the beginning */
3363 if (++sublist_idx
>= num_sublists
)
3368 * If we didn't evict anything during this scan, we have
3369 * no reason to believe we'll evict more during another
3370 * scan, so break the loop.
3372 if (scan_evicted
== 0) {
3373 /* This isn't possible, let's make that obvious */
3374 ASSERT3S(bytes
, !=, 0);
3377 * When bytes is ARC_EVICT_ALL, the only way to
3378 * break the loop is when scan_evicted is zero.
3379 * In that case, we actually have evicted enough,
3380 * so we don't want to increment the kstat.
3382 if (bytes
!= ARC_EVICT_ALL
) {
3383 ASSERT3S(total_evicted
, <, bytes
);
3384 ARCSTAT_BUMP(arcstat_evict_not_enough
);
3391 for (i
= 0; i
< num_sublists
; i
++) {
3392 multilist_sublist_t
*mls
= multilist_sublist_lock(ml
, i
);
3393 multilist_sublist_remove(mls
, markers
[i
]);
3394 multilist_sublist_unlock(mls
);
3396 kmem_cache_free(hdr_full_cache
, markers
[i
]);
3398 kmem_free(markers
, sizeof (*markers
) * num_sublists
);
3400 return (total_evicted
);
3404 * Flush all "evictable" data of the given type from the arc state
3405 * specified. This will not evict any "active" buffers (i.e. referenced).
3407 * When 'retry' is set to B_FALSE, the function will make a single pass
3408 * over the state and evict any buffers that it can. Since it doesn't
3409 * continually retry the eviction, it might end up leaving some buffers
3410 * in the ARC due to lock misses.
3412 * When 'retry' is set to B_TRUE, the function will continually retry the
3413 * eviction until *all* evictable buffers have been removed from the
3414 * state. As a result, if concurrent insertions into the state are
3415 * allowed (e.g. if the ARC isn't shutting down), this function might
3416 * wind up in an infinite loop, continually trying to evict buffers.
3419 arc_flush_state(arc_state_t
*state
, uint64_t spa
, arc_buf_contents_t type
,
3422 uint64_t evicted
= 0;
3424 while (refcount_count(&state
->arcs_esize
[type
]) != 0) {
3425 evicted
+= arc_evict_state(state
, spa
, ARC_EVICT_ALL
, type
);
3435 * Helper function for arc_prune_async() it is responsible for safely
3436 * handling the execution of a registered arc_prune_func_t.
3439 arc_prune_task(void *ptr
)
3441 arc_prune_t
*ap
= (arc_prune_t
*)ptr
;
3442 arc_prune_func_t
*func
= ap
->p_pfunc
;
3445 func(ap
->p_adjust
, ap
->p_private
);
3447 refcount_remove(&ap
->p_refcnt
, func
);
3451 * Notify registered consumers they must drop holds on a portion of the ARC
3452 * buffered they reference. This provides a mechanism to ensure the ARC can
3453 * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This
3454 * is analogous to dnlc_reduce_cache() but more generic.
3456 * This operation is performed asynchronously so it may be safely called
3457 * in the context of the arc_reclaim_thread(). A reference is taken here
3458 * for each registered arc_prune_t and the arc_prune_task() is responsible
3459 * for releasing it once the registered arc_prune_func_t has completed.
3462 arc_prune_async(int64_t adjust
)
3466 mutex_enter(&arc_prune_mtx
);
3467 for (ap
= list_head(&arc_prune_list
); ap
!= NULL
;
3468 ap
= list_next(&arc_prune_list
, ap
)) {
3470 if (refcount_count(&ap
->p_refcnt
) >= 2)
3473 refcount_add(&ap
->p_refcnt
, ap
->p_pfunc
);
3474 ap
->p_adjust
= adjust
;
3475 taskq_dispatch(arc_prune_taskq
, arc_prune_task
, ap
, TQ_SLEEP
);
3476 ARCSTAT_BUMP(arcstat_prune
);
3478 mutex_exit(&arc_prune_mtx
);
3482 * Evict the specified number of bytes from the state specified,
3483 * restricting eviction to the spa and type given. This function
3484 * prevents us from trying to evict more from a state's list than
3485 * is "evictable", and to skip evicting altogether when passed a
3486 * negative value for "bytes". In contrast, arc_evict_state() will
3487 * evict everything it can, when passed a negative value for "bytes".
3490 arc_adjust_impl(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
3491 arc_buf_contents_t type
)
3495 if (bytes
> 0 && refcount_count(&state
->arcs_esize
[type
]) > 0) {
3496 delta
= MIN(refcount_count(&state
->arcs_esize
[type
]), bytes
);
3497 return (arc_evict_state(state
, spa
, delta
, type
));
3504 * The goal of this function is to evict enough meta data buffers from the
3505 * ARC in order to enforce the arc_meta_limit. Achieving this is slightly
3506 * more complicated than it appears because it is common for data buffers
3507 * to have holds on meta data buffers. In addition, dnode meta data buffers
3508 * will be held by the dnodes in the block preventing them from being freed.
3509 * This means we can't simply traverse the ARC and expect to always find
3510 * enough unheld meta data buffer to release.
3512 * Therefore, this function has been updated to make alternating passes
3513 * over the ARC releasing data buffers and then newly unheld meta data
3514 * buffers. This ensures forward progress is maintained and arc_meta_used
3515 * will decrease. Normally this is sufficient, but if required the ARC
3516 * will call the registered prune callbacks causing dentry and inodes to
3517 * be dropped from the VFS cache. This will make dnode meta data buffers
3518 * available for reclaim.
3521 arc_adjust_meta_balanced(void)
3523 int64_t delta
, prune
= 0;
3524 uint64_t adjustmnt
, total_evicted
= 0;
3525 arc_buf_contents_t type
= ARC_BUFC_DATA
;
3526 int restarts
= MAX(zfs_arc_meta_adjust_restarts
, 0);
3530 * This slightly differs than the way we evict from the mru in
3531 * arc_adjust because we don't have a "target" value (i.e. no
3532 * "meta" arc_p). As a result, I think we can completely
3533 * cannibalize the metadata in the MRU before we evict the
3534 * metadata from the MFU. I think we probably need to implement a
3535 * "metadata arc_p" value to do this properly.
3537 adjustmnt
= arc_meta_used
- arc_meta_limit
;
3539 if (adjustmnt
> 0 && refcount_count(&arc_mru
->arcs_esize
[type
]) > 0) {
3540 delta
= MIN(refcount_count(&arc_mru
->arcs_esize
[type
]),
3542 total_evicted
+= arc_adjust_impl(arc_mru
, 0, delta
, type
);
3547 * We can't afford to recalculate adjustmnt here. If we do,
3548 * new metadata buffers can sneak into the MRU or ANON lists,
3549 * thus penalize the MFU metadata. Although the fudge factor is
3550 * small, it has been empirically shown to be significant for
3551 * certain workloads (e.g. creating many empty directories). As
3552 * such, we use the original calculation for adjustmnt, and
3553 * simply decrement the amount of data evicted from the MRU.
3556 if (adjustmnt
> 0 && refcount_count(&arc_mfu
->arcs_esize
[type
]) > 0) {
3557 delta
= MIN(refcount_count(&arc_mfu
->arcs_esize
[type
]),
3559 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, delta
, type
);
3562 adjustmnt
= arc_meta_used
- arc_meta_limit
;
3564 if (adjustmnt
> 0 &&
3565 refcount_count(&arc_mru_ghost
->arcs_esize
[type
]) > 0) {
3566 delta
= MIN(adjustmnt
,
3567 refcount_count(&arc_mru_ghost
->arcs_esize
[type
]));
3568 total_evicted
+= arc_adjust_impl(arc_mru_ghost
, 0, delta
, type
);
3572 if (adjustmnt
> 0 &&
3573 refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]) > 0) {
3574 delta
= MIN(adjustmnt
,
3575 refcount_count(&arc_mfu_ghost
->arcs_esize
[type
]));
3576 total_evicted
+= arc_adjust_impl(arc_mfu_ghost
, 0, delta
, type
);
3580 * If after attempting to make the requested adjustment to the ARC
3581 * the meta limit is still being exceeded then request that the
3582 * higher layers drop some cached objects which have holds on ARC
3583 * meta buffers. Requests to the upper layers will be made with
3584 * increasingly large scan sizes until the ARC is below the limit.
3586 if (arc_meta_used
> arc_meta_limit
) {
3587 if (type
== ARC_BUFC_DATA
) {
3588 type
= ARC_BUFC_METADATA
;
3590 type
= ARC_BUFC_DATA
;
3592 if (zfs_arc_meta_prune
) {
3593 prune
+= zfs_arc_meta_prune
;
3594 arc_prune_async(prune
);
3603 return (total_evicted
);
3607 * Evict metadata buffers from the cache, such that arc_meta_used is
3608 * capped by the arc_meta_limit tunable.
3611 arc_adjust_meta_only(void)
3613 uint64_t total_evicted
= 0;
3617 * If we're over the meta limit, we want to evict enough
3618 * metadata to get back under the meta limit. We don't want to
3619 * evict so much that we drop the MRU below arc_p, though. If
3620 * we're over the meta limit more than we're over arc_p, we
3621 * evict some from the MRU here, and some from the MFU below.
3623 target
= MIN((int64_t)(arc_meta_used
- arc_meta_limit
),
3624 (int64_t)(refcount_count(&arc_anon
->arcs_size
) +
3625 refcount_count(&arc_mru
->arcs_size
) - arc_p
));
3627 total_evicted
+= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
3630 * Similar to the above, we want to evict enough bytes to get us
3631 * below the meta limit, but not so much as to drop us below the
3632 * space allotted to the MFU (which is defined as arc_c - arc_p).
3634 target
= MIN((int64_t)(arc_meta_used
- arc_meta_limit
),
3635 (int64_t)(refcount_count(&arc_mfu
->arcs_size
) - (arc_c
- arc_p
)));
3637 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
3639 return (total_evicted
);
3643 arc_adjust_meta(void)
3645 if (zfs_arc_meta_strategy
== ARC_STRATEGY_META_ONLY
)
3646 return (arc_adjust_meta_only());
3648 return (arc_adjust_meta_balanced());
3652 * Return the type of the oldest buffer in the given arc state
3654 * This function will select a random sublist of type ARC_BUFC_DATA and
3655 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist
3656 * is compared, and the type which contains the "older" buffer will be
3659 static arc_buf_contents_t
3660 arc_adjust_type(arc_state_t
*state
)
3662 multilist_t
*data_ml
= &state
->arcs_list
[ARC_BUFC_DATA
];
3663 multilist_t
*meta_ml
= &state
->arcs_list
[ARC_BUFC_METADATA
];
3664 int data_idx
= multilist_get_random_index(data_ml
);
3665 int meta_idx
= multilist_get_random_index(meta_ml
);
3666 multilist_sublist_t
*data_mls
;
3667 multilist_sublist_t
*meta_mls
;
3668 arc_buf_contents_t type
;
3669 arc_buf_hdr_t
*data_hdr
;
3670 arc_buf_hdr_t
*meta_hdr
;
3673 * We keep the sublist lock until we're finished, to prevent
3674 * the headers from being destroyed via arc_evict_state().
3676 data_mls
= multilist_sublist_lock(data_ml
, data_idx
);
3677 meta_mls
= multilist_sublist_lock(meta_ml
, meta_idx
);
3680 * These two loops are to ensure we skip any markers that
3681 * might be at the tail of the lists due to arc_evict_state().
3684 for (data_hdr
= multilist_sublist_tail(data_mls
); data_hdr
!= NULL
;
3685 data_hdr
= multilist_sublist_prev(data_mls
, data_hdr
)) {
3686 if (data_hdr
->b_spa
!= 0)
3690 for (meta_hdr
= multilist_sublist_tail(meta_mls
); meta_hdr
!= NULL
;
3691 meta_hdr
= multilist_sublist_prev(meta_mls
, meta_hdr
)) {
3692 if (meta_hdr
->b_spa
!= 0)
3696 if (data_hdr
== NULL
&& meta_hdr
== NULL
) {
3697 type
= ARC_BUFC_DATA
;
3698 } else if (data_hdr
== NULL
) {
3699 ASSERT3P(meta_hdr
, !=, NULL
);
3700 type
= ARC_BUFC_METADATA
;
3701 } else if (meta_hdr
== NULL
) {
3702 ASSERT3P(data_hdr
, !=, NULL
);
3703 type
= ARC_BUFC_DATA
;
3705 ASSERT3P(data_hdr
, !=, NULL
);
3706 ASSERT3P(meta_hdr
, !=, NULL
);
3708 /* The headers can't be on the sublist without an L1 header */
3709 ASSERT(HDR_HAS_L1HDR(data_hdr
));
3710 ASSERT(HDR_HAS_L1HDR(meta_hdr
));
3712 if (data_hdr
->b_l1hdr
.b_arc_access
<
3713 meta_hdr
->b_l1hdr
.b_arc_access
) {
3714 type
= ARC_BUFC_DATA
;
3716 type
= ARC_BUFC_METADATA
;
3720 multilist_sublist_unlock(meta_mls
);
3721 multilist_sublist_unlock(data_mls
);
3727 * Evict buffers from the cache, such that arc_size is capped by arc_c.
3732 uint64_t total_evicted
= 0;
3737 * If we're over arc_meta_limit, we want to correct that before
3738 * potentially evicting data buffers below.
3740 total_evicted
+= arc_adjust_meta();
3745 * If we're over the target cache size, we want to evict enough
3746 * from the list to get back to our target size. We don't want
3747 * to evict too much from the MRU, such that it drops below
3748 * arc_p. So, if we're over our target cache size more than
3749 * the MRU is over arc_p, we'll evict enough to get back to
3750 * arc_p here, and then evict more from the MFU below.
3752 target
= MIN((int64_t)(arc_size
- arc_c
),
3753 (int64_t)(refcount_count(&arc_anon
->arcs_size
) +
3754 refcount_count(&arc_mru
->arcs_size
) + arc_meta_used
- arc_p
));
3757 * If we're below arc_meta_min, always prefer to evict data.
3758 * Otherwise, try to satisfy the requested number of bytes to
3759 * evict from the type which contains older buffers; in an
3760 * effort to keep newer buffers in the cache regardless of their
3761 * type. If we cannot satisfy the number of bytes from this
3762 * type, spill over into the next type.
3764 if (arc_adjust_type(arc_mru
) == ARC_BUFC_METADATA
&&
3765 arc_meta_used
> arc_meta_min
) {
3766 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
3767 total_evicted
+= bytes
;
3770 * If we couldn't evict our target number of bytes from
3771 * metadata, we try to get the rest from data.
3776 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
3778 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
3779 total_evicted
+= bytes
;
3782 * If we couldn't evict our target number of bytes from
3783 * data, we try to get the rest from metadata.
3788 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
3794 * Now that we've tried to evict enough from the MRU to get its
3795 * size back to arc_p, if we're still above the target cache
3796 * size, we evict the rest from the MFU.
3798 target
= arc_size
- arc_c
;
3800 if (arc_adjust_type(arc_mfu
) == ARC_BUFC_METADATA
&&
3801 arc_meta_used
> arc_meta_min
) {
3802 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
3803 total_evicted
+= bytes
;
3806 * If we couldn't evict our target number of bytes from
3807 * metadata, we try to get the rest from data.
3812 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
3814 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
3815 total_evicted
+= bytes
;
3818 * If we couldn't evict our target number of bytes from
3819 * data, we try to get the rest from data.
3824 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
3828 * Adjust ghost lists
3830 * In addition to the above, the ARC also defines target values
3831 * for the ghost lists. The sum of the mru list and mru ghost
3832 * list should never exceed the target size of the cache, and
3833 * the sum of the mru list, mfu list, mru ghost list, and mfu
3834 * ghost list should never exceed twice the target size of the
3835 * cache. The following logic enforces these limits on the ghost
3836 * caches, and evicts from them as needed.
3838 target
= refcount_count(&arc_mru
->arcs_size
) +
3839 refcount_count(&arc_mru_ghost
->arcs_size
) - arc_c
;
3841 bytes
= arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_DATA
);
3842 total_evicted
+= bytes
;
3847 arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_METADATA
);
3850 * We assume the sum of the mru list and mfu list is less than
3851 * or equal to arc_c (we enforced this above), which means we
3852 * can use the simpler of the two equations below:
3854 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c
3855 * mru ghost + mfu ghost <= arc_c
3857 target
= refcount_count(&arc_mru_ghost
->arcs_size
) +
3858 refcount_count(&arc_mfu_ghost
->arcs_size
) - arc_c
;
3860 bytes
= arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_DATA
);
3861 total_evicted
+= bytes
;
3866 arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_METADATA
);
3868 return (total_evicted
);
3872 arc_flush(spa_t
*spa
, boolean_t retry
)
3877 * If retry is B_TRUE, a spa must not be specified since we have
3878 * no good way to determine if all of a spa's buffers have been
3879 * evicted from an arc state.
3881 ASSERT(!retry
|| spa
== 0);
3884 guid
= spa_load_guid(spa
);
3886 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_DATA
, retry
);
3887 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_METADATA
, retry
);
3889 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_DATA
, retry
);
3890 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_METADATA
, retry
);
3892 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_DATA
, retry
);
3893 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
3895 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_DATA
, retry
);
3896 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
3900 arc_shrink(int64_t to_free
)
3904 if (c
> to_free
&& c
- to_free
> arc_c_min
) {
3905 arc_c
= c
- to_free
;
3906 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
3907 if (arc_c
> arc_size
)
3908 arc_c
= MAX(arc_size
, arc_c_min
);
3910 arc_p
= (arc_c
>> 1);
3911 ASSERT(arc_c
>= arc_c_min
);
3912 ASSERT((int64_t)arc_p
>= 0);
3917 if (arc_size
> arc_c
)
3918 (void) arc_adjust();
3921 typedef enum free_memory_reason_t
{
3926 FMR_PAGES_PP_MAXIMUM
,
3929 } free_memory_reason_t
;
3931 int64_t last_free_memory
;
3932 free_memory_reason_t last_free_reason
;
3936 * Additional reserve of pages for pp_reserve.
3938 int64_t arc_pages_pp_reserve
= 64;
3941 * Additional reserve of pages for swapfs.
3943 int64_t arc_swapfs_reserve
= 64;
3944 #endif /* _KERNEL */
3947 * Return the amount of memory that can be consumed before reclaim will be
3948 * needed. Positive if there is sufficient free memory, negative indicates
3949 * the amount of memory that needs to be freed up.
3952 arc_available_memory(void)
3954 int64_t lowest
= INT64_MAX
;
3955 free_memory_reason_t r
= FMR_UNKNOWN
;
3959 pgcnt_t needfree
= btop(arc_need_free
);
3960 pgcnt_t lotsfree
= btop(arc_sys_free
);
3961 pgcnt_t desfree
= 0;
3965 n
= PAGESIZE
* (-needfree
);
3973 * check that we're out of range of the pageout scanner. It starts to
3974 * schedule paging if freemem is less than lotsfree and needfree.
3975 * lotsfree is the high-water mark for pageout, and needfree is the
3976 * number of needed free pages. We add extra pages here to make sure
3977 * the scanner doesn't start up while we're freeing memory.
3979 n
= PAGESIZE
* (freemem
- lotsfree
- needfree
- desfree
);
3987 * check to make sure that swapfs has enough space so that anon
3988 * reservations can still succeed. anon_resvmem() checks that the
3989 * availrmem is greater than swapfs_minfree, and the number of reserved
3990 * swap pages. We also add a bit of extra here just to prevent
3991 * circumstances from getting really dire.
3993 n
= PAGESIZE
* (availrmem
- swapfs_minfree
- swapfs_reserve
-
3994 desfree
- arc_swapfs_reserve
);
3997 r
= FMR_SWAPFS_MINFREE
;
4002 * Check that we have enough availrmem that memory locking (e.g., via
4003 * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum
4004 * stores the number of pages that cannot be locked; when availrmem
4005 * drops below pages_pp_maximum, page locking mechanisms such as
4006 * page_pp_lock() will fail.)
4008 n
= PAGESIZE
* (availrmem
- pages_pp_maximum
-
4009 arc_pages_pp_reserve
);
4012 r
= FMR_PAGES_PP_MAXIMUM
;
4018 * If we're on an i386 platform, it's possible that we'll exhaust the
4019 * kernel heap space before we ever run out of available physical
4020 * memory. Most checks of the size of the heap_area compare against
4021 * tune.t_minarmem, which is the minimum available real memory that we
4022 * can have in the system. However, this is generally fixed at 25 pages
4023 * which is so low that it's useless. In this comparison, we seek to
4024 * calculate the total heap-size, and reclaim if more than 3/4ths of the
4025 * heap is allocated. (Or, in the calculation, if less than 1/4th is
4028 n
= vmem_size(heap_arena
, VMEM_FREE
) -
4029 (vmem_size(heap_arena
, VMEM_FREE
| VMEM_ALLOC
) >> 2);
4037 * If zio data pages are being allocated out of a separate heap segment,
4038 * then enforce that the size of available vmem for this arena remains
4039 * above about 1/4th (1/(2^arc_zio_arena_free_shift)) free.
4041 * Note that reducing the arc_zio_arena_free_shift keeps more virtual
4042 * memory (in the zio_arena) free, which can avoid memory
4043 * fragmentation issues.
4045 if (zio_arena
!= NULL
) {
4046 n
= vmem_size(zio_arena
, VMEM_FREE
) - (vmem_size(zio_arena
,
4047 VMEM_ALLOC
) >> arc_zio_arena_free_shift
);
4054 /* Every 100 calls, free a small amount */
4055 if (spa_get_random(100) == 0)
4057 #endif /* _KERNEL */
4059 last_free_memory
= lowest
;
4060 last_free_reason
= r
;
4066 * Determine if the system is under memory pressure and is asking
4067 * to reclaim memory. A return value of B_TRUE indicates that the system
4068 * is under memory pressure and that the arc should adjust accordingly.
4071 arc_reclaim_needed(void)
4073 return (arc_available_memory() < 0);
4077 arc_kmem_reap_now(void)
4080 kmem_cache_t
*prev_cache
= NULL
;
4081 kmem_cache_t
*prev_data_cache
= NULL
;
4082 extern kmem_cache_t
*zio_buf_cache
[];
4083 extern kmem_cache_t
*zio_data_buf_cache
[];
4084 extern kmem_cache_t
*range_seg_cache
;
4086 if ((arc_meta_used
>= arc_meta_limit
) && zfs_arc_meta_prune
) {
4088 * We are exceeding our meta-data cache limit.
4089 * Prune some entries to release holds on meta-data.
4091 arc_prune_async(zfs_arc_meta_prune
);
4094 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
4096 /* reach upper limit of cache size on 32-bit */
4097 if (zio_buf_cache
[i
] == NULL
)
4100 if (zio_buf_cache
[i
] != prev_cache
) {
4101 prev_cache
= zio_buf_cache
[i
];
4102 kmem_cache_reap_now(zio_buf_cache
[i
]);
4104 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
4105 prev_data_cache
= zio_data_buf_cache
[i
];
4106 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
4109 kmem_cache_reap_now(buf_cache
);
4110 kmem_cache_reap_now(hdr_full_cache
);
4111 kmem_cache_reap_now(hdr_l2only_cache
);
4112 kmem_cache_reap_now(range_seg_cache
);
4114 if (zio_arena
!= NULL
) {
4116 * Ask the vmem arena to reclaim unused memory from its
4119 vmem_qcache_reap(zio_arena
);
4124 * Threads can block in arc_get_data_buf() waiting for this thread to evict
4125 * enough data and signal them to proceed. When this happens, the threads in
4126 * arc_get_data_buf() are sleeping while holding the hash lock for their
4127 * particular arc header. Thus, we must be careful to never sleep on a
4128 * hash lock in this thread. This is to prevent the following deadlock:
4130 * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L",
4131 * waiting for the reclaim thread to signal it.
4133 * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter,
4134 * fails, and goes to sleep forever.
4136 * This possible deadlock is avoided by always acquiring a hash lock
4137 * using mutex_tryenter() from arc_reclaim_thread().
4140 arc_reclaim_thread(void)
4142 fstrans_cookie_t cookie
= spl_fstrans_mark();
4143 hrtime_t growtime
= 0;
4146 CALLB_CPR_INIT(&cpr
, &arc_reclaim_lock
, callb_generic_cpr
, FTAG
);
4148 mutex_enter(&arc_reclaim_lock
);
4149 while (!arc_reclaim_thread_exit
) {
4151 int64_t free_memory
= arc_available_memory();
4152 uint64_t evicted
= 0;
4154 arc_tuning_update();
4157 * This is necessary in order for the mdb ::arc dcmd to
4158 * show up to date information. Since the ::arc command
4159 * does not call the kstat's update function, without
4160 * this call, the command may show stale stats for the
4161 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
4162 * with this change, the data might be up to 1 second
4163 * out of date; but that should suffice. The arc_state_t
4164 * structures can be queried directly if more accurate
4165 * information is needed.
4168 if (arc_ksp
!= NULL
)
4169 arc_ksp
->ks_update(arc_ksp
, KSTAT_READ
);
4171 mutex_exit(&arc_reclaim_lock
);
4173 if (free_memory
< 0) {
4175 arc_no_grow
= B_TRUE
;
4179 * Wait at least zfs_grow_retry (default 5) seconds
4180 * before considering growing.
4182 growtime
= gethrtime() + SEC2NSEC(arc_grow_retry
);
4184 arc_kmem_reap_now();
4187 * If we are still low on memory, shrink the ARC
4188 * so that we have arc_shrink_min free space.
4190 free_memory
= arc_available_memory();
4192 to_free
= (arc_c
>> arc_shrink_shift
) - free_memory
;
4195 to_free
= MAX(to_free
, arc_need_free
);
4197 arc_shrink(to_free
);
4199 } else if (free_memory
< arc_c
>> arc_no_grow_shift
) {
4200 arc_no_grow
= B_TRUE
;
4201 } else if (gethrtime() >= growtime
) {
4202 arc_no_grow
= B_FALSE
;
4205 evicted
= arc_adjust();
4207 mutex_enter(&arc_reclaim_lock
);
4210 * If evicted is zero, we couldn't evict anything via
4211 * arc_adjust(). This could be due to hash lock
4212 * collisions, but more likely due to the majority of
4213 * arc buffers being unevictable. Therefore, even if
4214 * arc_size is above arc_c, another pass is unlikely to
4215 * be helpful and could potentially cause us to enter an
4218 if (arc_size
<= arc_c
|| evicted
== 0) {
4220 * We're either no longer overflowing, or we
4221 * can't evict anything more, so we should wake
4222 * up any threads before we go to sleep and clear
4223 * arc_need_free since nothing more can be done.
4225 cv_broadcast(&arc_reclaim_waiters_cv
);
4229 * Block until signaled, or after one second (we
4230 * might need to perform arc_kmem_reap_now()
4231 * even if we aren't being signalled)
4233 CALLB_CPR_SAFE_BEGIN(&cpr
);
4234 (void) cv_timedwait_sig_hires(&arc_reclaim_thread_cv
,
4235 &arc_reclaim_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
4236 CALLB_CPR_SAFE_END(&cpr
, &arc_reclaim_lock
);
4240 arc_reclaim_thread_exit
= B_FALSE
;
4241 cv_broadcast(&arc_reclaim_thread_cv
);
4242 CALLB_CPR_EXIT(&cpr
); /* drops arc_reclaim_lock */
4243 spl_fstrans_unmark(cookie
);
4249 * Determine the amount of memory eligible for eviction contained in the
4250 * ARC. All clean data reported by the ghost lists can always be safely
4251 * evicted. Due to arc_c_min, the same does not hold for all clean data
4252 * contained by the regular mru and mfu lists.
4254 * In the case of the regular mru and mfu lists, we need to report as
4255 * much clean data as possible, such that evicting that same reported
4256 * data will not bring arc_size below arc_c_min. Thus, in certain
4257 * circumstances, the total amount of clean data in the mru and mfu
4258 * lists might not actually be evictable.
4260 * The following two distinct cases are accounted for:
4262 * 1. The sum of the amount of dirty data contained by both the mru and
4263 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
4264 * is greater than or equal to arc_c_min.
4265 * (i.e. amount of dirty data >= arc_c_min)
4267 * This is the easy case; all clean data contained by the mru and mfu
4268 * lists is evictable. Evicting all clean data can only drop arc_size
4269 * to the amount of dirty data, which is greater than arc_c_min.
4271 * 2. The sum of the amount of dirty data contained by both the mru and
4272 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
4273 * is less than arc_c_min.
4274 * (i.e. arc_c_min > amount of dirty data)
4276 * 2.1. arc_size is greater than or equal arc_c_min.
4277 * (i.e. arc_size >= arc_c_min > amount of dirty data)
4279 * In this case, not all clean data from the regular mru and mfu
4280 * lists is actually evictable; we must leave enough clean data
4281 * to keep arc_size above arc_c_min. Thus, the maximum amount of
4282 * evictable data from the two lists combined, is exactly the
4283 * difference between arc_size and arc_c_min.
4285 * 2.2. arc_size is less than arc_c_min
4286 * (i.e. arc_c_min > arc_size > amount of dirty data)
4288 * In this case, none of the data contained in the mru and mfu
4289 * lists is evictable, even if it's clean. Since arc_size is
4290 * already below arc_c_min, evicting any more would only
4291 * increase this negative difference.
4294 arc_evictable_memory(void) {
4295 uint64_t arc_clean
=
4296 refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]) +
4297 refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]) +
4298 refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]) +
4299 refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
4300 uint64_t ghost_clean
=
4301 refcount_count(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]) +
4302 refcount_count(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]) +
4303 refcount_count(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]) +
4304 refcount_count(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
4305 uint64_t arc_dirty
= MAX((int64_t)arc_size
- (int64_t)arc_clean
, 0);
4307 if (arc_dirty
>= arc_c_min
)
4308 return (ghost_clean
+ arc_clean
);
4310 return (ghost_clean
+ MAX((int64_t)arc_size
- (int64_t)arc_c_min
, 0));
4314 * If sc->nr_to_scan is zero, the caller is requesting a query of the
4315 * number of objects which can potentially be freed. If it is nonzero,
4316 * the request is to free that many objects.
4318 * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks
4319 * in struct shrinker and also require the shrinker to return the number
4322 * Older kernels require the shrinker to return the number of freeable
4323 * objects following the freeing of nr_to_free.
4325 static spl_shrinker_t
4326 __arc_shrinker_func(struct shrinker
*shrink
, struct shrink_control
*sc
)
4330 /* The arc is considered warm once reclaim has occurred */
4331 if (unlikely(arc_warm
== B_FALSE
))
4334 /* Return the potential number of reclaimable pages */
4335 pages
= btop((int64_t)arc_evictable_memory());
4336 if (sc
->nr_to_scan
== 0)
4339 /* Not allowed to perform filesystem reclaim */
4340 if (!(sc
->gfp_mask
& __GFP_FS
))
4341 return (SHRINK_STOP
);
4343 /* Reclaim in progress */
4344 if (mutex_tryenter(&arc_reclaim_lock
) == 0)
4345 return (SHRINK_STOP
);
4347 mutex_exit(&arc_reclaim_lock
);
4350 * Evict the requested number of pages by shrinking arc_c the
4351 * requested amount. If there is nothing left to evict just
4352 * reap whatever we can from the various arc slabs.
4355 arc_shrink(ptob(sc
->nr_to_scan
));
4356 arc_kmem_reap_now();
4357 #ifdef HAVE_SPLIT_SHRINKER_CALLBACK
4358 pages
= MAX(pages
- btop(arc_evictable_memory()), 0);
4360 pages
= btop(arc_evictable_memory());
4363 arc_kmem_reap_now();
4364 pages
= SHRINK_STOP
;
4368 * We've reaped what we can, wake up threads.
4370 cv_broadcast(&arc_reclaim_waiters_cv
);
4373 * When direct reclaim is observed it usually indicates a rapid
4374 * increase in memory pressure. This occurs because the kswapd
4375 * threads were unable to asynchronously keep enough free memory
4376 * available. In this case set arc_no_grow to briefly pause arc
4377 * growth to avoid compounding the memory pressure.
4379 if (current_is_kswapd()) {
4380 ARCSTAT_BUMP(arcstat_memory_indirect_count
);
4382 arc_no_grow
= B_TRUE
;
4383 arc_need_free
= ptob(sc
->nr_to_scan
);
4384 ARCSTAT_BUMP(arcstat_memory_direct_count
);
4389 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func
);
4391 SPL_SHRINKER_DECLARE(arc_shrinker
, arc_shrinker_func
, DEFAULT_SEEKS
);
4392 #endif /* _KERNEL */
4395 * Adapt arc info given the number of bytes we are trying to add and
4396 * the state that we are comming from. This function is only called
4397 * when we are adding new content to the cache.
4400 arc_adapt(int bytes
, arc_state_t
*state
)
4403 uint64_t arc_p_min
= (arc_c
>> arc_p_min_shift
);
4404 int64_t mrug_size
= refcount_count(&arc_mru_ghost
->arcs_size
);
4405 int64_t mfug_size
= refcount_count(&arc_mfu_ghost
->arcs_size
);
4407 if (state
== arc_l2c_only
)
4412 * Adapt the target size of the MRU list:
4413 * - if we just hit in the MRU ghost list, then increase
4414 * the target size of the MRU list.
4415 * - if we just hit in the MFU ghost list, then increase
4416 * the target size of the MFU list by decreasing the
4417 * target size of the MRU list.
4419 if (state
== arc_mru_ghost
) {
4420 mult
= (mrug_size
>= mfug_size
) ? 1 : (mfug_size
/ mrug_size
);
4421 if (!zfs_arc_p_dampener_disable
)
4422 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
4424 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
4425 } else if (state
== arc_mfu_ghost
) {
4428 mult
= (mfug_size
>= mrug_size
) ? 1 : (mrug_size
/ mfug_size
);
4429 if (!zfs_arc_p_dampener_disable
)
4430 mult
= MIN(mult
, 10);
4432 delta
= MIN(bytes
* mult
, arc_p
);
4433 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
4435 ASSERT((int64_t)arc_p
>= 0);
4437 if (arc_reclaim_needed()) {
4438 cv_signal(&arc_reclaim_thread_cv
);
4445 if (arc_c
>= arc_c_max
)
4449 * If we're within (2 * maxblocksize) bytes of the target
4450 * cache size, increment the target cache size
4452 ASSERT3U(arc_c
, >=, 2ULL << SPA_MAXBLOCKSHIFT
);
4453 if (arc_size
>= arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
4454 atomic_add_64(&arc_c
, (int64_t)bytes
);
4455 if (arc_c
> arc_c_max
)
4457 else if (state
== arc_anon
)
4458 atomic_add_64(&arc_p
, (int64_t)bytes
);
4462 ASSERT((int64_t)arc_p
>= 0);
4466 * Check if arc_size has grown past our upper threshold, determined by
4467 * zfs_arc_overflow_shift.
4470 arc_is_overflowing(void)
4472 /* Always allow at least one block of overflow */
4473 uint64_t overflow
= MAX(SPA_MAXBLOCKSIZE
,
4474 arc_c
>> zfs_arc_overflow_shift
);
4476 return (arc_size
>= arc_c
+ overflow
);
4480 * Allocate a block and return it to the caller. If we are hitting the
4481 * hard limit for the cache size, we must sleep, waiting for the eviction
4482 * thread to catch up. If we're past the target size but below the hard
4483 * limit, we'll only signal the reclaim thread and continue on.
4486 arc_get_data_buf(arc_buf_hdr_t
*hdr
, uint64_t size
, void *tag
)
4489 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
4490 arc_buf_contents_t type
= arc_buf_type(hdr
);
4492 arc_adapt(size
, state
);
4495 * If arc_size is currently overflowing, and has grown past our
4496 * upper limit, we must be adding data faster than the evict
4497 * thread can evict. Thus, to ensure we don't compound the
4498 * problem by adding more data and forcing arc_size to grow even
4499 * further past it's target size, we halt and wait for the
4500 * eviction thread to catch up.
4502 * It's also possible that the reclaim thread is unable to evict
4503 * enough buffers to get arc_size below the overflow limit (e.g.
4504 * due to buffers being un-evictable, or hash lock collisions).
4505 * In this case, we want to proceed regardless if we're
4506 * overflowing; thus we don't use a while loop here.
4508 if (arc_is_overflowing()) {
4509 mutex_enter(&arc_reclaim_lock
);
4512 * Now that we've acquired the lock, we may no longer be
4513 * over the overflow limit, lets check.
4515 * We're ignoring the case of spurious wake ups. If that
4516 * were to happen, it'd let this thread consume an ARC
4517 * buffer before it should have (i.e. before we're under
4518 * the overflow limit and were signalled by the reclaim
4519 * thread). As long as that is a rare occurrence, it
4520 * shouldn't cause any harm.
4522 if (arc_is_overflowing()) {
4523 cv_signal(&arc_reclaim_thread_cv
);
4524 cv_wait(&arc_reclaim_waiters_cv
, &arc_reclaim_lock
);
4527 mutex_exit(&arc_reclaim_lock
);
4530 VERIFY3U(hdr
->b_type
, ==, type
);
4531 if (type
== ARC_BUFC_METADATA
) {
4532 datap
= zio_buf_alloc(size
);
4533 arc_space_consume(size
, ARC_SPACE_META
);
4535 ASSERT(type
== ARC_BUFC_DATA
);
4536 datap
= zio_data_buf_alloc(size
);
4537 arc_space_consume(size
, ARC_SPACE_DATA
);
4541 * Update the state size. Note that ghost states have a
4542 * "ghost size" and so don't need to be updated.
4544 if (!GHOST_STATE(state
)) {
4546 (void) refcount_add_many(&state
->arcs_size
, size
, tag
);
4549 * If this is reached via arc_read, the link is
4550 * protected by the hash lock. If reached via
4551 * arc_buf_alloc, the header should not be accessed by
4552 * any other thread. And, if reached via arc_read_done,
4553 * the hash lock will protect it if it's found in the
4554 * hash table; otherwise no other thread should be
4555 * trying to [add|remove]_reference it.
4557 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
4558 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
4559 (void) refcount_add_many(&state
->arcs_esize
[type
],
4564 * If we are growing the cache, and we are adding anonymous
4565 * data, and we have outgrown arc_p, update arc_p
4567 if (arc_size
< arc_c
&& hdr
->b_l1hdr
.b_state
== arc_anon
&&
4568 (refcount_count(&arc_anon
->arcs_size
) +
4569 refcount_count(&arc_mru
->arcs_size
) > arc_p
))
4570 arc_p
= MIN(arc_c
, arc_p
+ size
);
4576 * Free the arc data buffer.
4579 arc_free_data_buf(arc_buf_hdr_t
*hdr
, void *data
, uint64_t size
, void *tag
)
4581 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
4582 arc_buf_contents_t type
= arc_buf_type(hdr
);
4584 /* protected by hash lock, if in the hash table */
4585 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
4586 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
4587 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
4589 (void) refcount_remove_many(&state
->arcs_esize
[type
],
4592 (void) refcount_remove_many(&state
->arcs_size
, size
, tag
);
4594 VERIFY3U(hdr
->b_type
, ==, type
);
4595 if (type
== ARC_BUFC_METADATA
) {
4596 zio_buf_free(data
, size
);
4597 arc_space_return(size
, ARC_SPACE_META
);
4599 ASSERT(type
== ARC_BUFC_DATA
);
4600 zio_data_buf_free(data
, size
);
4601 arc_space_return(size
, ARC_SPACE_DATA
);
4606 * This routine is called whenever a buffer is accessed.
4607 * NOTE: the hash lock is dropped in this function.
4610 arc_access(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
4614 ASSERT(MUTEX_HELD(hash_lock
));
4615 ASSERT(HDR_HAS_L1HDR(hdr
));
4617 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
4619 * This buffer is not in the cache, and does not
4620 * appear in our "ghost" list. Add the new buffer
4624 ASSERT0(hdr
->b_l1hdr
.b_arc_access
);
4625 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
4626 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
4627 arc_change_state(arc_mru
, hdr
, hash_lock
);
4629 } else if (hdr
->b_l1hdr
.b_state
== arc_mru
) {
4630 now
= ddi_get_lbolt();
4633 * If this buffer is here because of a prefetch, then either:
4634 * - clear the flag if this is a "referencing" read
4635 * (any subsequent access will bump this into the MFU state).
4637 * - move the buffer to the head of the list if this is
4638 * another prefetch (to make it less likely to be evicted).
4640 if (HDR_PREFETCH(hdr
)) {
4641 if (refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
4642 /* link protected by hash lock */
4643 ASSERT(multilist_link_active(
4644 &hdr
->b_l1hdr
.b_arc_node
));
4646 arc_hdr_clear_flags(hdr
, ARC_FLAG_PREFETCH
);
4647 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
4648 ARCSTAT_BUMP(arcstat_mru_hits
);
4650 hdr
->b_l1hdr
.b_arc_access
= now
;
4655 * This buffer has been "accessed" only once so far,
4656 * but it is still in the cache. Move it to the MFU
4659 if (ddi_time_after(now
, hdr
->b_l1hdr
.b_arc_access
+
4662 * More than 125ms have passed since we
4663 * instantiated this buffer. Move it to the
4664 * most frequently used state.
4666 hdr
->b_l1hdr
.b_arc_access
= now
;
4667 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
4668 arc_change_state(arc_mfu
, hdr
, hash_lock
);
4670 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
4671 ARCSTAT_BUMP(arcstat_mru_hits
);
4672 } else if (hdr
->b_l1hdr
.b_state
== arc_mru_ghost
) {
4673 arc_state_t
*new_state
;
4675 * This buffer has been "accessed" recently, but
4676 * was evicted from the cache. Move it to the
4680 if (HDR_PREFETCH(hdr
)) {
4681 new_state
= arc_mru
;
4682 if (refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0)
4683 arc_hdr_clear_flags(hdr
, ARC_FLAG_PREFETCH
);
4684 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
4686 new_state
= arc_mfu
;
4687 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
4690 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
4691 arc_change_state(new_state
, hdr
, hash_lock
);
4693 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_ghost_hits
);
4694 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
4695 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu
) {
4697 * This buffer has been accessed more than once and is
4698 * still in the cache. Keep it in the MFU state.
4700 * NOTE: an add_reference() that occurred when we did
4701 * the arc_read() will have kicked this off the list.
4702 * If it was a prefetch, we will explicitly move it to
4703 * the head of the list now.
4705 if ((HDR_PREFETCH(hdr
)) != 0) {
4706 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
4707 /* link protected by hash_lock */
4708 ASSERT(multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
4710 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_hits
);
4711 ARCSTAT_BUMP(arcstat_mfu_hits
);
4712 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
4713 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu_ghost
) {
4714 arc_state_t
*new_state
= arc_mfu
;
4716 * This buffer has been accessed more than once but has
4717 * been evicted from the cache. Move it back to the
4721 if (HDR_PREFETCH(hdr
)) {
4723 * This is a prefetch access...
4724 * move this block back to the MRU state.
4726 ASSERT0(refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
4727 new_state
= arc_mru
;
4730 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
4731 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
4732 arc_change_state(new_state
, hdr
, hash_lock
);
4734 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_ghost_hits
);
4735 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
4736 } else if (hdr
->b_l1hdr
.b_state
== arc_l2c_only
) {
4738 * This buffer is on the 2nd Level ARC.
4741 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
4742 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
4743 arc_change_state(arc_mfu
, hdr
, hash_lock
);
4745 cmn_err(CE_PANIC
, "invalid arc state 0x%p",
4746 hdr
->b_l1hdr
.b_state
);
4750 /* a generic arc_done_func_t which you can use */
4753 arc_bcopy_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
4755 if (zio
== NULL
|| zio
->io_error
== 0)
4756 bcopy(buf
->b_data
, arg
, arc_buf_size(buf
));
4757 arc_buf_destroy(buf
, arg
);
4760 /* a generic arc_done_func_t */
4762 arc_getbuf_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
4764 arc_buf_t
**bufp
= arg
;
4765 if (zio
&& zio
->io_error
) {
4766 arc_buf_destroy(buf
, arg
);
4770 ASSERT(buf
->b_data
);
4775 arc_hdr_verify(arc_buf_hdr_t
*hdr
, blkptr_t
*bp
)
4777 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
)) {
4778 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, 0);
4779 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==, ZIO_COMPRESS_OFF
);
4781 if (HDR_COMPRESSION_ENABLED(hdr
)) {
4782 ASSERT3U(HDR_GET_COMPRESS(hdr
), ==,
4783 BP_GET_COMPRESS(bp
));
4785 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(bp
));
4786 ASSERT3U(HDR_GET_PSIZE(hdr
), ==, BP_GET_PSIZE(bp
));
4791 arc_read_done(zio_t
*zio
)
4793 arc_buf_hdr_t
*hdr
= zio
->io_private
;
4794 kmutex_t
*hash_lock
= NULL
;
4795 arc_callback_t
*callback_list
;
4796 arc_callback_t
*acb
;
4797 boolean_t freeable
= B_FALSE
;
4798 boolean_t no_zio_error
= (zio
->io_error
== 0);
4799 int callback_cnt
= 0;
4801 * The hdr was inserted into hash-table and removed from lists
4802 * prior to starting I/O. We should find this header, since
4803 * it's in the hash table, and it should be legit since it's
4804 * not possible to evict it during the I/O. The only possible
4805 * reason for it not to be found is if we were freed during the
4808 if (HDR_IN_HASH_TABLE(hdr
)) {
4809 arc_buf_hdr_t
*found
;
4811 ASSERT3U(hdr
->b_birth
, ==, BP_PHYSICAL_BIRTH(zio
->io_bp
));
4812 ASSERT3U(hdr
->b_dva
.dva_word
[0], ==,
4813 BP_IDENTITY(zio
->io_bp
)->dva_word
[0]);
4814 ASSERT3U(hdr
->b_dva
.dva_word
[1], ==,
4815 BP_IDENTITY(zio
->io_bp
)->dva_word
[1]);
4817 found
= buf_hash_find(hdr
->b_spa
, zio
->io_bp
, &hash_lock
);
4819 ASSERT((found
== hdr
&&
4820 DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
4821 (found
== hdr
&& HDR_L2_READING(hdr
)));
4822 ASSERT3P(hash_lock
, !=, NULL
);
4826 /* byteswap if necessary */
4827 if (BP_SHOULD_BYTESWAP(zio
->io_bp
)) {
4828 if (BP_GET_LEVEL(zio
->io_bp
) > 0) {
4829 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_UINT64
;
4831 hdr
->b_l1hdr
.b_byteswap
=
4832 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
4835 hdr
->b_l1hdr
.b_byteswap
= DMU_BSWAP_NUMFUNCS
;
4839 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_EVICTED
);
4840 if (l2arc_noprefetch
&& HDR_PREFETCH(hdr
))
4841 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2CACHE
);
4843 callback_list
= hdr
->b_l1hdr
.b_acb
;
4844 ASSERT3P(callback_list
, !=, NULL
);
4846 if (hash_lock
&& no_zio_error
&& hdr
->b_l1hdr
.b_state
== arc_anon
) {
4848 * Only call arc_access on anonymous buffers. This is because
4849 * if we've issued an I/O for an evicted buffer, we've already
4850 * called arc_access (to prevent any simultaneous readers from
4851 * getting confused).
4853 arc_access(hdr
, hash_lock
);
4857 * If a read request has a callback (i.e. acb_done is not NULL), then we
4858 * make a buf containing the data according to the parameters which were
4859 * passed in. The implementation of arc_buf_alloc_impl() ensures that we
4860 * aren't needlessly decompressing the data multiple times.
4862 for (acb
= callback_list
; acb
!= NULL
; acb
= acb
->acb_next
) {
4867 /* This is a demand read since prefetches don't use callbacks */
4871 error
= arc_buf_alloc_impl(hdr
, acb
->acb_private
,
4872 acb
->acb_compressed
, no_zio_error
, &acb
->acb_buf
);
4874 zio
->io_error
= error
;
4877 hdr
->b_l1hdr
.b_acb
= NULL
;
4878 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
4879 if (callback_cnt
== 0) {
4880 ASSERT(HDR_PREFETCH(hdr
));
4881 ASSERT0(hdr
->b_l1hdr
.b_bufcnt
);
4882 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
4885 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
) ||
4886 callback_list
!= NULL
);
4889 arc_hdr_verify(hdr
, zio
->io_bp
);
4891 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_ERROR
);
4892 if (hdr
->b_l1hdr
.b_state
!= arc_anon
)
4893 arc_change_state(arc_anon
, hdr
, hash_lock
);
4894 if (HDR_IN_HASH_TABLE(hdr
))
4895 buf_hash_remove(hdr
);
4896 freeable
= refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
4900 * Broadcast before we drop the hash_lock to avoid the possibility
4901 * that the hdr (and hence the cv) might be freed before we get to
4902 * the cv_broadcast().
4904 cv_broadcast(&hdr
->b_l1hdr
.b_cv
);
4906 if (hash_lock
!= NULL
) {
4907 mutex_exit(hash_lock
);
4910 * This block was freed while we waited for the read to
4911 * complete. It has been removed from the hash table and
4912 * moved to the anonymous state (so that it won't show up
4915 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
4916 freeable
= refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
4919 /* execute each callback and free its structure */
4920 while ((acb
= callback_list
) != NULL
) {
4922 acb
->acb_done(zio
, acb
->acb_buf
, acb
->acb_private
);
4924 if (acb
->acb_zio_dummy
!= NULL
) {
4925 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
4926 zio_nowait(acb
->acb_zio_dummy
);
4929 callback_list
= acb
->acb_next
;
4930 kmem_free(acb
, sizeof (arc_callback_t
));
4934 arc_hdr_destroy(hdr
);
4938 * "Read" the block at the specified DVA (in bp) via the
4939 * cache. If the block is found in the cache, invoke the provided
4940 * callback immediately and return. Note that the `zio' parameter
4941 * in the callback will be NULL in this case, since no IO was
4942 * required. If the block is not in the cache pass the read request
4943 * on to the spa with a substitute callback function, so that the
4944 * requested block will be added to the cache.
4946 * If a read request arrives for a block that has a read in-progress,
4947 * either wait for the in-progress read to complete (and return the
4948 * results); or, if this is a read with a "done" func, add a record
4949 * to the read to invoke the "done" func when the read completes,
4950 * and return; or just return.
4952 * arc_read_done() will invoke all the requested "done" functions
4953 * for readers of this block.
4956 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
, arc_done_func_t
*done
,
4957 void *private, zio_priority_t priority
, int zio_flags
,
4958 arc_flags_t
*arc_flags
, const zbookmark_phys_t
*zb
)
4960 arc_buf_hdr_t
*hdr
= NULL
;
4961 kmutex_t
*hash_lock
= NULL
;
4963 uint64_t guid
= spa_load_guid(spa
);
4964 boolean_t compressed_read
= (zio_flags
& ZIO_FLAG_RAW
) != 0;
4967 ASSERT(!BP_IS_EMBEDDED(bp
) ||
4968 BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
);
4971 if (!BP_IS_EMBEDDED(bp
)) {
4973 * Embedded BP's have no DVA and require no I/O to "read".
4974 * Create an anonymous arc buf to back it.
4976 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
4979 if (hdr
!= NULL
&& HDR_HAS_L1HDR(hdr
) && hdr
->b_l1hdr
.b_pdata
!= NULL
) {
4980 arc_buf_t
*buf
= NULL
;
4981 *arc_flags
|= ARC_FLAG_CACHED
;
4983 if (HDR_IO_IN_PROGRESS(hdr
)) {
4985 if ((hdr
->b_flags
& ARC_FLAG_PRIO_ASYNC_READ
) &&
4986 priority
== ZIO_PRIORITY_SYNC_READ
) {
4988 * This sync read must wait for an
4989 * in-progress async read (e.g. a predictive
4990 * prefetch). Async reads are queued
4991 * separately at the vdev_queue layer, so
4992 * this is a form of priority inversion.
4993 * Ideally, we would "inherit" the demand
4994 * i/o's priority by moving the i/o from
4995 * the async queue to the synchronous queue,
4996 * but there is currently no mechanism to do
4997 * so. Track this so that we can evaluate
4998 * the magnitude of this potential performance
5001 * Note that if the prefetch i/o is already
5002 * active (has been issued to the device),
5003 * the prefetch improved performance, because
5004 * we issued it sooner than we would have
5005 * without the prefetch.
5007 DTRACE_PROBE1(arc__sync__wait__for__async
,
5008 arc_buf_hdr_t
*, hdr
);
5009 ARCSTAT_BUMP(arcstat_sync_wait_for_async
);
5011 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
5012 arc_hdr_clear_flags(hdr
,
5013 ARC_FLAG_PREDICTIVE_PREFETCH
);
5016 if (*arc_flags
& ARC_FLAG_WAIT
) {
5017 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
5018 mutex_exit(hash_lock
);
5021 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
5024 arc_callback_t
*acb
= NULL
;
5026 acb
= kmem_zalloc(sizeof (arc_callback_t
),
5028 acb
->acb_done
= done
;
5029 acb
->acb_private
= private;
5031 acb
->acb_zio_dummy
= zio_null(pio
,
5032 spa
, NULL
, NULL
, NULL
, zio_flags
);
5034 ASSERT3P(acb
->acb_done
, !=, NULL
);
5035 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
5036 hdr
->b_l1hdr
.b_acb
= acb
;
5037 mutex_exit(hash_lock
);
5040 mutex_exit(hash_lock
);
5044 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
5045 hdr
->b_l1hdr
.b_state
== arc_mfu
);
5048 if (hdr
->b_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
) {
5050 * This is a demand read which does not have to
5051 * wait for i/o because we did a predictive
5052 * prefetch i/o for it, which has completed.
5055 arc__demand__hit__predictive__prefetch
,
5056 arc_buf_hdr_t
*, hdr
);
5058 arcstat_demand_hit_predictive_prefetch
);
5059 arc_hdr_clear_flags(hdr
,
5060 ARC_FLAG_PREDICTIVE_PREFETCH
);
5062 ASSERT(!BP_IS_EMBEDDED(bp
) || !BP_IS_HOLE(bp
));
5064 /* Get a buf with the desired data in it. */
5065 VERIFY0(arc_buf_alloc_impl(hdr
, private,
5066 compressed_read
, B_TRUE
, &buf
));
5067 } else if (*arc_flags
& ARC_FLAG_PREFETCH
&&
5068 refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
5069 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
5071 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
5072 arc_access(hdr
, hash_lock
);
5073 if (*arc_flags
& ARC_FLAG_L2CACHE
)
5074 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
5075 mutex_exit(hash_lock
);
5076 ARCSTAT_BUMP(arcstat_hits
);
5077 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
5078 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
5079 data
, metadata
, hits
);
5082 done(NULL
, buf
, private);
5084 uint64_t lsize
= BP_GET_LSIZE(bp
);
5085 uint64_t psize
= BP_GET_PSIZE(bp
);
5086 arc_callback_t
*acb
;
5089 boolean_t devw
= B_FALSE
;
5093 * Gracefully handle a damaged logical block size as a
5096 if (lsize
> spa_maxblocksize(spa
)) {
5097 rc
= SET_ERROR(ECKSUM
);
5102 /* this block is not in the cache */
5103 arc_buf_hdr_t
*exists
= NULL
;
5104 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
5105 hdr
= arc_hdr_alloc(spa_load_guid(spa
), psize
, lsize
,
5106 BP_GET_COMPRESS(bp
), type
);
5108 if (!BP_IS_EMBEDDED(bp
)) {
5109 hdr
->b_dva
= *BP_IDENTITY(bp
);
5110 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
5111 exists
= buf_hash_insert(hdr
, &hash_lock
);
5113 if (exists
!= NULL
) {
5114 /* somebody beat us to the hash insert */
5115 mutex_exit(hash_lock
);
5116 buf_discard_identity(hdr
);
5117 arc_hdr_destroy(hdr
);
5118 goto top
; /* restart the IO request */
5122 * This block is in the ghost cache. If it was L2-only
5123 * (and thus didn't have an L1 hdr), we realloc the
5124 * header to add an L1 hdr.
5126 if (!HDR_HAS_L1HDR(hdr
)) {
5127 hdr
= arc_hdr_realloc(hdr
, hdr_l2only_cache
,
5131 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
5132 ASSERT(GHOST_STATE(hdr
->b_l1hdr
.b_state
));
5133 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
5134 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5135 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
5136 ASSERT3P(hdr
->b_l1hdr
.b_freeze_cksum
, ==, NULL
);
5139 * This is a delicate dance that we play here.
5140 * This hdr is in the ghost list so we access it
5141 * to move it out of the ghost list before we
5142 * initiate the read. If it's a prefetch then
5143 * it won't have a callback so we'll remove the
5144 * reference that arc_buf_alloc_impl() created. We
5145 * do this after we've called arc_access() to
5146 * avoid hitting an assert in remove_reference().
5148 arc_access(hdr
, hash_lock
);
5149 arc_hdr_alloc_pdata(hdr
);
5151 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
5152 size
= arc_hdr_size(hdr
);
5155 * If compression is enabled on the hdr, then will do
5156 * RAW I/O and will store the compressed data in the hdr's
5157 * data block. Otherwise, the hdr's data block will contain
5158 * the uncompressed data.
5160 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
) {
5161 zio_flags
|= ZIO_FLAG_RAW
;
5164 if (*arc_flags
& ARC_FLAG_PREFETCH
)
5165 arc_hdr_set_flags(hdr
, ARC_FLAG_PREFETCH
);
5166 if (*arc_flags
& ARC_FLAG_L2CACHE
)
5167 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
5168 if (BP_GET_LEVEL(bp
) > 0)
5169 arc_hdr_set_flags(hdr
, ARC_FLAG_INDIRECT
);
5170 if (*arc_flags
& ARC_FLAG_PREDICTIVE_PREFETCH
)
5171 arc_hdr_set_flags(hdr
, ARC_FLAG_PREDICTIVE_PREFETCH
);
5172 ASSERT(!GHOST_STATE(hdr
->b_l1hdr
.b_state
));
5174 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
5175 acb
->acb_done
= done
;
5176 acb
->acb_private
= private;
5177 acb
->acb_compressed
= compressed_read
;
5179 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
5180 hdr
->b_l1hdr
.b_acb
= acb
;
5181 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5183 if (HDR_HAS_L2HDR(hdr
) &&
5184 (vd
= hdr
->b_l2hdr
.b_dev
->l2ad_vdev
) != NULL
) {
5185 devw
= hdr
->b_l2hdr
.b_dev
->l2ad_writing
;
5186 addr
= hdr
->b_l2hdr
.b_daddr
;
5188 * Lock out device removal.
5190 if (vdev_is_dead(vd
) ||
5191 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
5195 if (priority
== ZIO_PRIORITY_ASYNC_READ
)
5196 arc_hdr_set_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
5198 arc_hdr_clear_flags(hdr
, ARC_FLAG_PRIO_ASYNC_READ
);
5200 if (hash_lock
!= NULL
)
5201 mutex_exit(hash_lock
);
5204 * At this point, we have a level 1 cache miss. Try again in
5205 * L2ARC if possible.
5207 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, lsize
);
5209 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
, blkptr_t
*, bp
,
5210 uint64_t, lsize
, zbookmark_phys_t
*, zb
);
5211 ARCSTAT_BUMP(arcstat_misses
);
5212 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
5213 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
5214 data
, metadata
, misses
);
5216 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
5218 * Read from the L2ARC if the following are true:
5219 * 1. The L2ARC vdev was previously cached.
5220 * 2. This buffer still has L2ARC metadata.
5221 * 3. This buffer isn't currently writing to the L2ARC.
5222 * 4. The L2ARC entry wasn't evicted, which may
5223 * also have invalidated the vdev.
5224 * 5. This isn't prefetch and l2arc_noprefetch is set.
5226 if (HDR_HAS_L2HDR(hdr
) &&
5227 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
5228 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
5229 l2arc_read_callback_t
*cb
;
5231 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
5232 ARCSTAT_BUMP(arcstat_l2_hits
);
5233 atomic_inc_32(&hdr
->b_l2hdr
.b_hits
);
5235 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
5237 cb
->l2rcb_hdr
= hdr
;
5240 cb
->l2rcb_flags
= zio_flags
;
5242 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
5243 addr
+ lsize
< vd
->vdev_psize
-
5244 VDEV_LABEL_END_SIZE
);
5247 * l2arc read. The SCL_L2ARC lock will be
5248 * released by l2arc_read_done().
5249 * Issue a null zio if the underlying buffer
5250 * was squashed to zero size by compression.
5252 ASSERT3U(HDR_GET_COMPRESS(hdr
), !=,
5253 ZIO_COMPRESS_EMPTY
);
5254 rzio
= zio_read_phys(pio
, vd
, addr
,
5255 size
, hdr
->b_l1hdr
.b_pdata
,
5257 l2arc_read_done
, cb
, priority
,
5258 zio_flags
| ZIO_FLAG_DONT_CACHE
|
5260 ZIO_FLAG_DONT_PROPAGATE
|
5261 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
5263 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
5265 ARCSTAT_INCR(arcstat_l2_read_bytes
, size
);
5267 if (*arc_flags
& ARC_FLAG_NOWAIT
) {
5272 ASSERT(*arc_flags
& ARC_FLAG_WAIT
);
5273 if (zio_wait(rzio
) == 0)
5276 /* l2arc read error; goto zio_read() */
5278 DTRACE_PROBE1(l2arc__miss
,
5279 arc_buf_hdr_t
*, hdr
);
5280 ARCSTAT_BUMP(arcstat_l2_misses
);
5281 if (HDR_L2_WRITING(hdr
))
5282 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
5283 spa_config_exit(spa
, SCL_L2ARC
, vd
);
5287 spa_config_exit(spa
, SCL_L2ARC
, vd
);
5288 if (l2arc_ndev
!= 0) {
5289 DTRACE_PROBE1(l2arc__miss
,
5290 arc_buf_hdr_t
*, hdr
);
5291 ARCSTAT_BUMP(arcstat_l2_misses
);
5295 rzio
= zio_read(pio
, spa
, bp
, hdr
->b_l1hdr
.b_pdata
, size
,
5296 arc_read_done
, hdr
, priority
, zio_flags
, zb
);
5298 if (*arc_flags
& ARC_FLAG_WAIT
) {
5299 rc
= zio_wait(rzio
);
5303 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
5308 spa_read_history_add(spa
, zb
, *arc_flags
);
5313 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
5317 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
5319 p
->p_private
= private;
5320 list_link_init(&p
->p_node
);
5321 refcount_create(&p
->p_refcnt
);
5323 mutex_enter(&arc_prune_mtx
);
5324 refcount_add(&p
->p_refcnt
, &arc_prune_list
);
5325 list_insert_head(&arc_prune_list
, p
);
5326 mutex_exit(&arc_prune_mtx
);
5332 arc_remove_prune_callback(arc_prune_t
*p
)
5334 boolean_t wait
= B_FALSE
;
5335 mutex_enter(&arc_prune_mtx
);
5336 list_remove(&arc_prune_list
, p
);
5337 if (refcount_remove(&p
->p_refcnt
, &arc_prune_list
) > 0)
5339 mutex_exit(&arc_prune_mtx
);
5341 /* wait for arc_prune_task to finish */
5343 taskq_wait_outstanding(arc_prune_taskq
, 0);
5344 ASSERT0(refcount_count(&p
->p_refcnt
));
5345 refcount_destroy(&p
->p_refcnt
);
5346 kmem_free(p
, sizeof (*p
));
5350 * Notify the arc that a block was freed, and thus will never be used again.
5353 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
5356 kmutex_t
*hash_lock
;
5357 uint64_t guid
= spa_load_guid(spa
);
5359 ASSERT(!BP_IS_EMBEDDED(bp
));
5361 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
5366 * We might be trying to free a block that is still doing I/O
5367 * (i.e. prefetch) or has a reference (i.e. a dedup-ed,
5368 * dmu_sync-ed block). If this block is being prefetched, then it
5369 * would still have the ARC_FLAG_IO_IN_PROGRESS flag set on the hdr
5370 * until the I/O completes. A block may also have a reference if it is
5371 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would
5372 * have written the new block to its final resting place on disk but
5373 * without the dedup flag set. This would have left the hdr in the MRU
5374 * state and discoverable. When the txg finally syncs it detects that
5375 * the block was overridden in open context and issues an override I/O.
5376 * Since this is a dedup block, the override I/O will determine if the
5377 * block is already in the DDT. If so, then it will replace the io_bp
5378 * with the bp from the DDT and allow the I/O to finish. When the I/O
5379 * reaches the done callback, dbuf_write_override_done, it will
5380 * check to see if the io_bp and io_bp_override are identical.
5381 * If they are not, then it indicates that the bp was replaced with
5382 * the bp in the DDT and the override bp is freed. This allows
5383 * us to arrive here with a reference on a block that is being
5384 * freed. So if we have an I/O in progress, or a reference to
5385 * this hdr, then we don't destroy the hdr.
5387 if (!HDR_HAS_L1HDR(hdr
) || (!HDR_IO_IN_PROGRESS(hdr
) &&
5388 refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
))) {
5389 arc_change_state(arc_anon
, hdr
, hash_lock
);
5390 arc_hdr_destroy(hdr
);
5391 mutex_exit(hash_lock
);
5393 mutex_exit(hash_lock
);
5399 * Release this buffer from the cache, making it an anonymous buffer. This
5400 * must be done after a read and prior to modifying the buffer contents.
5401 * If the buffer has more than one reference, we must make
5402 * a new hdr for the buffer.
5405 arc_release(arc_buf_t
*buf
, void *tag
)
5407 kmutex_t
*hash_lock
;
5409 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5412 * It would be nice to assert that if its DMU metadata (level >
5413 * 0 || it's the dnode file), then it must be syncing context.
5414 * But we don't know that information at this level.
5417 mutex_enter(&buf
->b_evict_lock
);
5419 ASSERT(HDR_HAS_L1HDR(hdr
));
5422 * We don't grab the hash lock prior to this check, because if
5423 * the buffer's header is in the arc_anon state, it won't be
5424 * linked into the hash table.
5426 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
5427 mutex_exit(&buf
->b_evict_lock
);
5428 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
5429 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
5430 ASSERT(!HDR_HAS_L2HDR(hdr
));
5431 ASSERT(HDR_EMPTY(hdr
));
5433 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
5434 ASSERT3S(refcount_count(&hdr
->b_l1hdr
.b_refcnt
), ==, 1);
5435 ASSERT(!list_link_active(&hdr
->b_l1hdr
.b_arc_node
));
5437 hdr
->b_l1hdr
.b_arc_access
= 0;
5440 * If the buf is being overridden then it may already
5441 * have a hdr that is not empty.
5443 buf_discard_identity(hdr
);
5449 hash_lock
= HDR_LOCK(hdr
);
5450 mutex_enter(hash_lock
);
5453 * This assignment is only valid as long as the hash_lock is
5454 * held, we must be careful not to reference state or the
5455 * b_state field after dropping the lock.
5457 state
= hdr
->b_l1hdr
.b_state
;
5458 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
5459 ASSERT3P(state
, !=, arc_anon
);
5461 /* this buffer is not on any list */
5462 ASSERT3S(refcount_count(&hdr
->b_l1hdr
.b_refcnt
), >, 0);
5464 if (HDR_HAS_L2HDR(hdr
)) {
5465 mutex_enter(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
5468 * We have to recheck this conditional again now that
5469 * we're holding the l2ad_mtx to prevent a race with
5470 * another thread which might be concurrently calling
5471 * l2arc_evict(). In that case, l2arc_evict() might have
5472 * destroyed the header's L2 portion as we were waiting
5473 * to acquire the l2ad_mtx.
5475 if (HDR_HAS_L2HDR(hdr
))
5476 arc_hdr_l2hdr_destroy(hdr
);
5478 mutex_exit(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
5482 * Do we have more than one buf?
5484 if (hdr
->b_l1hdr
.b_bufcnt
> 1) {
5485 arc_buf_hdr_t
*nhdr
;
5486 uint64_t spa
= hdr
->b_spa
;
5487 uint64_t psize
= HDR_GET_PSIZE(hdr
);
5488 uint64_t lsize
= HDR_GET_LSIZE(hdr
);
5489 enum zio_compress compress
= HDR_GET_COMPRESS(hdr
);
5490 arc_buf_contents_t type
= arc_buf_type(hdr
);
5491 arc_buf_t
*lastbuf
= NULL
;
5492 VERIFY3U(hdr
->b_type
, ==, type
);
5494 ASSERT(hdr
->b_l1hdr
.b_buf
!= buf
|| buf
->b_next
!= NULL
);
5495 (void) remove_reference(hdr
, hash_lock
, tag
);
5497 if (arc_buf_is_shared(buf
) && !ARC_BUF_COMPRESSED(buf
)) {
5498 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
5499 ASSERT(ARC_BUF_LAST(buf
));
5503 * Pull the data off of this hdr and attach it to
5504 * a new anonymous hdr. Also find the last buffer
5505 * in the hdr's buffer list.
5507 lastbuf
= arc_buf_remove(hdr
, buf
);
5508 ASSERT3P(lastbuf
, !=, NULL
);
5511 * If the current arc_buf_t and the hdr are sharing their data
5512 * buffer, then we must stop sharing that block.
5514 if (arc_buf_is_shared(buf
)) {
5515 ASSERT3P(hdr
->b_l1hdr
.b_buf
, !=, buf
);
5516 VERIFY(!arc_buf_is_shared(lastbuf
));
5519 * First, sever the block sharing relationship between
5520 * buf and the arc_buf_hdr_t. Then, setup a new
5521 * block sharing relationship with the last buffer
5522 * on the arc_buf_t list.
5524 arc_unshare_buf(hdr
, buf
);
5527 * Now we need to recreate the hdr's b_pdata. Since we
5528 * have lastbuf handy, we try to share with it, but if
5529 * we can't then we allocate a new b_pdata and copy the
5530 * data from buf into it.
5532 if (arc_can_share(hdr
, lastbuf
)) {
5533 arc_share_buf(hdr
, lastbuf
);
5535 arc_hdr_alloc_pdata(hdr
);
5536 bcopy(buf
->b_data
, hdr
->b_l1hdr
.b_pdata
, psize
);
5538 VERIFY3P(lastbuf
->b_data
, !=, NULL
);
5539 } else if (HDR_SHARED_DATA(hdr
)) {
5541 * Uncompressed shared buffers are always at the end
5542 * of the list. Compressed buffers don't have the
5543 * same requirements. This makes it hard to
5544 * simply assert that the lastbuf is shared so
5545 * we rely on the hdr's compression flags to determine
5546 * if we have a compressed, shared buffer.
5548 ASSERT(arc_buf_is_shared(lastbuf
) ||
5549 HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
);
5550 ASSERT(!ARC_BUF_SHARED(buf
));
5552 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
5553 ASSERT3P(state
, !=, arc_l2c_only
);
5555 (void) refcount_remove_many(&state
->arcs_size
,
5556 arc_buf_size(buf
), buf
);
5558 if (refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
5559 ASSERT3P(state
, !=, arc_l2c_only
);
5560 (void) refcount_remove_many(&state
->arcs_esize
[type
],
5561 arc_buf_size(buf
), buf
);
5564 hdr
->b_l1hdr
.b_bufcnt
-= 1;
5565 arc_cksum_verify(buf
);
5566 arc_buf_unwatch(buf
);
5568 mutex_exit(hash_lock
);
5571 * Allocate a new hdr. The new hdr will contain a b_pdata
5572 * buffer which will be freed in arc_write().
5574 nhdr
= arc_hdr_alloc(spa
, psize
, lsize
, compress
, type
);
5575 ASSERT3P(nhdr
->b_l1hdr
.b_buf
, ==, NULL
);
5576 ASSERT0(nhdr
->b_l1hdr
.b_bufcnt
);
5577 ASSERT0(refcount_count(&nhdr
->b_l1hdr
.b_refcnt
));
5578 VERIFY3U(nhdr
->b_type
, ==, type
);
5579 ASSERT(!HDR_SHARED_DATA(nhdr
));
5581 nhdr
->b_l1hdr
.b_buf
= buf
;
5582 nhdr
->b_l1hdr
.b_bufcnt
= 1;
5583 nhdr
->b_l1hdr
.b_mru_hits
= 0;
5584 nhdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
5585 nhdr
->b_l1hdr
.b_mfu_hits
= 0;
5586 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
5587 nhdr
->b_l1hdr
.b_l2_hits
= 0;
5588 (void) refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, tag
);
5591 mutex_exit(&buf
->b_evict_lock
);
5592 (void) refcount_add_many(&arc_anon
->arcs_size
,
5593 HDR_GET_LSIZE(nhdr
), buf
);
5595 mutex_exit(&buf
->b_evict_lock
);
5596 ASSERT(refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 1);
5597 /* protected by hash lock, or hdr is on arc_anon */
5598 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
5599 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
5600 hdr
->b_l1hdr
.b_mru_hits
= 0;
5601 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
5602 hdr
->b_l1hdr
.b_mfu_hits
= 0;
5603 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
5604 hdr
->b_l1hdr
.b_l2_hits
= 0;
5605 arc_change_state(arc_anon
, hdr
, hash_lock
);
5606 hdr
->b_l1hdr
.b_arc_access
= 0;
5607 mutex_exit(hash_lock
);
5609 buf_discard_identity(hdr
);
5615 arc_released(arc_buf_t
*buf
)
5619 mutex_enter(&buf
->b_evict_lock
);
5620 released
= (buf
->b_data
!= NULL
&&
5621 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
5622 mutex_exit(&buf
->b_evict_lock
);
5628 arc_referenced(arc_buf_t
*buf
)
5632 mutex_enter(&buf
->b_evict_lock
);
5633 referenced
= (refcount_count(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
5634 mutex_exit(&buf
->b_evict_lock
);
5635 return (referenced
);
5640 arc_write_ready(zio_t
*zio
)
5642 arc_write_callback_t
*callback
= zio
->io_private
;
5643 arc_buf_t
*buf
= callback
->awcb_buf
;
5644 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5645 uint64_t psize
= BP_IS_HOLE(zio
->io_bp
) ? 0 : BP_GET_PSIZE(zio
->io_bp
);
5646 enum zio_compress compress
;
5648 ASSERT(HDR_HAS_L1HDR(hdr
));
5649 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
5650 ASSERT(hdr
->b_l1hdr
.b_bufcnt
> 0);
5653 * If we're reexecuting this zio because the pool suspended, then
5654 * cleanup any state that was previously set the first time the
5655 * callback was invoked.
5657 if (zio
->io_flags
& ZIO_FLAG_REEXECUTED
) {
5658 arc_cksum_free(hdr
);
5659 arc_buf_unwatch(buf
);
5660 if (hdr
->b_l1hdr
.b_pdata
!= NULL
) {
5661 if (arc_buf_is_shared(buf
)) {
5662 arc_unshare_buf(hdr
, buf
);
5664 arc_hdr_free_pdata(hdr
);
5668 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
5669 ASSERT(!HDR_SHARED_DATA(hdr
));
5670 ASSERT(!arc_buf_is_shared(buf
));
5672 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
5674 if (HDR_IO_IN_PROGRESS(hdr
))
5675 ASSERT(zio
->io_flags
& ZIO_FLAG_REEXECUTED
);
5677 arc_cksum_compute(buf
);
5678 arc_hdr_set_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5680 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
5681 compress
= ZIO_COMPRESS_OFF
;
5683 ASSERT3U(HDR_GET_LSIZE(hdr
), ==, BP_GET_LSIZE(zio
->io_bp
));
5684 compress
= BP_GET_COMPRESS(zio
->io_bp
);
5686 HDR_SET_PSIZE(hdr
, psize
);
5687 arc_hdr_set_compress(hdr
, compress
);
5690 * If the hdr is compressed, then copy the compressed
5691 * zio contents into arc_buf_hdr_t. Otherwise, copy the original
5692 * data buf into the hdr. Ideally, we would like to always copy the
5693 * io_data into b_pdata but the user may have disabled compressed
5694 * arc thus the on-disk block may or may not match what we maintain
5695 * in the hdr's b_pdata field.
5697 if (HDR_GET_COMPRESS(hdr
) != ZIO_COMPRESS_OFF
&&
5698 !ARC_BUF_COMPRESSED(buf
)) {
5699 ASSERT3U(BP_GET_COMPRESS(zio
->io_bp
), !=, ZIO_COMPRESS_OFF
);
5700 ASSERT3U(psize
, >, 0);
5701 arc_hdr_alloc_pdata(hdr
);
5702 bcopy(zio
->io_data
, hdr
->b_l1hdr
.b_pdata
, psize
);
5704 ASSERT3P(buf
->b_data
, ==, zio
->io_orig_data
);
5705 ASSERT3U(zio
->io_orig_size
, ==, arc_buf_size(buf
));
5706 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, ==, 1);
5709 * This hdr is not compressed so we're able to share
5710 * the arc_buf_t data buffer with the hdr.
5712 arc_share_buf(hdr
, buf
);
5713 ASSERT0(bcmp(zio
->io_orig_data
, hdr
->b_l1hdr
.b_pdata
,
5714 HDR_GET_LSIZE(hdr
)));
5716 arc_hdr_verify(hdr
, zio
->io_bp
);
5720 arc_write_children_ready(zio_t
*zio
)
5722 arc_write_callback_t
*callback
= zio
->io_private
;
5723 arc_buf_t
*buf
= callback
->awcb_buf
;
5725 callback
->awcb_children_ready(zio
, buf
, callback
->awcb_private
);
5729 * The SPA calls this callback for each physical write that happens on behalf
5730 * of a logical write. See the comment in dbuf_write_physdone() for details.
5733 arc_write_physdone(zio_t
*zio
)
5735 arc_write_callback_t
*cb
= zio
->io_private
;
5736 if (cb
->awcb_physdone
!= NULL
)
5737 cb
->awcb_physdone(zio
, cb
->awcb_buf
, cb
->awcb_private
);
5741 arc_write_done(zio_t
*zio
)
5743 arc_write_callback_t
*callback
= zio
->io_private
;
5744 arc_buf_t
*buf
= callback
->awcb_buf
;
5745 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5747 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
5749 if (zio
->io_error
== 0) {
5750 arc_hdr_verify(hdr
, zio
->io_bp
);
5752 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
5753 buf_discard_identity(hdr
);
5755 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
5756 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
5759 ASSERT(HDR_EMPTY(hdr
));
5763 * If the block to be written was all-zero or compressed enough to be
5764 * embedded in the BP, no write was performed so there will be no
5765 * dva/birth/checksum. The buffer must therefore remain anonymous
5768 if (!HDR_EMPTY(hdr
)) {
5769 arc_buf_hdr_t
*exists
;
5770 kmutex_t
*hash_lock
;
5772 ASSERT3U(zio
->io_error
, ==, 0);
5774 arc_cksum_verify(buf
);
5776 exists
= buf_hash_insert(hdr
, &hash_lock
);
5777 if (exists
!= NULL
) {
5779 * This can only happen if we overwrite for
5780 * sync-to-convergence, because we remove
5781 * buffers from the hash table when we arc_free().
5783 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
5784 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
5785 panic("bad overwrite, hdr=%p exists=%p",
5786 (void *)hdr
, (void *)exists
);
5787 ASSERT(refcount_is_zero(
5788 &exists
->b_l1hdr
.b_refcnt
));
5789 arc_change_state(arc_anon
, exists
, hash_lock
);
5790 mutex_exit(hash_lock
);
5791 arc_hdr_destroy(exists
);
5792 exists
= buf_hash_insert(hdr
, &hash_lock
);
5793 ASSERT3P(exists
, ==, NULL
);
5794 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
5796 ASSERT(zio
->io_prop
.zp_nopwrite
);
5797 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
5798 panic("bad nopwrite, hdr=%p exists=%p",
5799 (void *)hdr
, (void *)exists
);
5802 ASSERT(hdr
->b_l1hdr
.b_bufcnt
== 1);
5803 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
5804 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
5805 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
5808 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5809 /* if it's not anon, we are doing a scrub */
5810 if (exists
== NULL
&& hdr
->b_l1hdr
.b_state
== arc_anon
)
5811 arc_access(hdr
, hash_lock
);
5812 mutex_exit(hash_lock
);
5814 arc_hdr_clear_flags(hdr
, ARC_FLAG_IO_IN_PROGRESS
);
5817 ASSERT(!refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5818 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
5820 kmem_free(callback
, sizeof (arc_write_callback_t
));
5824 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
5825 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
,
5826 const zio_prop_t
*zp
, arc_done_func_t
*ready
,
5827 arc_done_func_t
*children_ready
, arc_done_func_t
*physdone
,
5828 arc_done_func_t
*done
, void *private, zio_priority_t priority
,
5829 int zio_flags
, const zbookmark_phys_t
*zb
)
5831 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5832 arc_write_callback_t
*callback
;
5835 ASSERT3P(ready
, !=, NULL
);
5836 ASSERT3P(done
, !=, NULL
);
5837 ASSERT(!HDR_IO_ERROR(hdr
));
5838 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
5839 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
5840 ASSERT3U(hdr
->b_l1hdr
.b_bufcnt
, >, 0);
5842 arc_hdr_set_flags(hdr
, ARC_FLAG_L2CACHE
);
5843 if (ARC_BUF_COMPRESSED(buf
)) {
5844 ASSERT3U(zp
->zp_compress
, !=, ZIO_COMPRESS_OFF
);
5845 zio_flags
|= ZIO_FLAG_RAW
;
5847 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
5848 callback
->awcb_ready
= ready
;
5849 callback
->awcb_children_ready
= children_ready
;
5850 callback
->awcb_physdone
= physdone
;
5851 callback
->awcb_done
= done
;
5852 callback
->awcb_private
= private;
5853 callback
->awcb_buf
= buf
;
5856 * The hdr's b_pdata is now stale, free it now. A new data block
5857 * will be allocated when the zio pipeline calls arc_write_ready().
5859 if (hdr
->b_l1hdr
.b_pdata
!= NULL
) {
5861 * If the buf is currently sharing the data block with
5862 * the hdr then we need to break that relationship here.
5863 * The hdr will remain with a NULL data pointer and the
5864 * buf will take sole ownership of the block.
5866 if (arc_buf_is_shared(buf
)) {
5867 arc_unshare_buf(hdr
, buf
);
5869 arc_hdr_free_pdata(hdr
);
5871 VERIFY3P(buf
->b_data
, !=, NULL
);
5872 arc_hdr_set_compress(hdr
, ZIO_COMPRESS_OFF
);
5874 ASSERT(!arc_buf_is_shared(buf
));
5875 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, ==, NULL
);
5877 zio
= zio_write(pio
, spa
, txg
, bp
, buf
->b_data
,
5878 HDR_GET_LSIZE(hdr
), arc_buf_size(buf
), zp
,
5880 (children_ready
!= NULL
) ? arc_write_children_ready
: NULL
,
5881 arc_write_physdone
, arc_write_done
, callback
,
5882 priority
, zio_flags
, zb
);
5888 arc_memory_throttle(uint64_t reserve
, uint64_t txg
)
5891 uint64_t available_memory
= ptob(freemem
);
5892 static uint64_t page_load
= 0;
5893 static uint64_t last_txg
= 0;
5895 pgcnt_t minfree
= btop(arc_sys_free
/ 4);
5898 if (freemem
> physmem
* arc_lotsfree_percent
/ 100)
5901 if (txg
> last_txg
) {
5906 * If we are in pageout, we know that memory is already tight,
5907 * the arc is already going to be evicting, so we just want to
5908 * continue to let page writes occur as quickly as possible.
5910 if (current_is_kswapd()) {
5911 if (page_load
> MAX(ptob(minfree
), available_memory
) / 4) {
5912 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
5913 return (SET_ERROR(ERESTART
));
5915 /* Note: reserve is inflated, so we deflate */
5916 page_load
+= reserve
/ 8;
5918 } else if (page_load
> 0 && arc_reclaim_needed()) {
5919 /* memory is low, delay before restarting */
5920 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
5921 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
5922 return (SET_ERROR(EAGAIN
));
5930 arc_tempreserve_clear(uint64_t reserve
)
5932 atomic_add_64(&arc_tempreserve
, -reserve
);
5933 ASSERT((int64_t)arc_tempreserve
>= 0);
5937 arc_tempreserve_space(uint64_t reserve
, uint64_t txg
)
5943 reserve
> arc_c
/4 &&
5944 reserve
* 4 > (2ULL << SPA_MAXBLOCKSHIFT
))
5945 arc_c
= MIN(arc_c_max
, reserve
* 4);
5948 * Throttle when the calculated memory footprint for the TXG
5949 * exceeds the target ARC size.
5951 if (reserve
> arc_c
) {
5952 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
5953 return (SET_ERROR(ERESTART
));
5957 * Don't count loaned bufs as in flight dirty data to prevent long
5958 * network delays from blocking transactions that are ready to be
5959 * assigned to a txg.
5961 anon_size
= MAX((int64_t)(refcount_count(&arc_anon
->arcs_size
) -
5962 arc_loaned_bytes
), 0);
5965 * Writes will, almost always, require additional memory allocations
5966 * in order to compress/encrypt/etc the data. We therefore need to
5967 * make sure that there is sufficient available memory for this.
5969 error
= arc_memory_throttle(reserve
, txg
);
5974 * Throttle writes when the amount of dirty data in the cache
5975 * gets too large. We try to keep the cache less than half full
5976 * of dirty blocks so that our sync times don't grow too large.
5977 * Note: if two requests come in concurrently, we might let them
5978 * both succeed, when one of them should fail. Not a huge deal.
5981 if (reserve
+ arc_tempreserve
+ anon_size
> arc_c
/ 2 &&
5982 anon_size
> arc_c
/ 4) {
5983 uint64_t meta_esize
=
5984 refcount_count(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
5985 uint64_t data_esize
=
5986 refcount_count(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
5987 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
5988 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
5989 arc_tempreserve
>> 10, meta_esize
>> 10,
5990 data_esize
>> 10, reserve
>> 10, arc_c
>> 10);
5991 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
5992 return (SET_ERROR(ERESTART
));
5994 atomic_add_64(&arc_tempreserve
, reserve
);
5999 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
6000 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
6002 size
->value
.ui64
= refcount_count(&state
->arcs_size
);
6003 evict_data
->value
.ui64
=
6004 refcount_count(&state
->arcs_esize
[ARC_BUFC_DATA
]);
6005 evict_metadata
->value
.ui64
=
6006 refcount_count(&state
->arcs_esize
[ARC_BUFC_METADATA
]);
6010 arc_kstat_update(kstat_t
*ksp
, int rw
)
6012 arc_stats_t
*as
= ksp
->ks_data
;
6014 if (rw
== KSTAT_WRITE
) {
6017 arc_kstat_update_state(arc_anon
,
6018 &as
->arcstat_anon_size
,
6019 &as
->arcstat_anon_evictable_data
,
6020 &as
->arcstat_anon_evictable_metadata
);
6021 arc_kstat_update_state(arc_mru
,
6022 &as
->arcstat_mru_size
,
6023 &as
->arcstat_mru_evictable_data
,
6024 &as
->arcstat_mru_evictable_metadata
);
6025 arc_kstat_update_state(arc_mru_ghost
,
6026 &as
->arcstat_mru_ghost_size
,
6027 &as
->arcstat_mru_ghost_evictable_data
,
6028 &as
->arcstat_mru_ghost_evictable_metadata
);
6029 arc_kstat_update_state(arc_mfu
,
6030 &as
->arcstat_mfu_size
,
6031 &as
->arcstat_mfu_evictable_data
,
6032 &as
->arcstat_mfu_evictable_metadata
);
6033 arc_kstat_update_state(arc_mfu_ghost
,
6034 &as
->arcstat_mfu_ghost_size
,
6035 &as
->arcstat_mfu_ghost_evictable_data
,
6036 &as
->arcstat_mfu_ghost_evictable_metadata
);
6043 * This function *must* return indices evenly distributed between all
6044 * sublists of the multilist. This is needed due to how the ARC eviction
6045 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
6046 * distributed between all sublists and uses this assumption when
6047 * deciding which sublist to evict from and how much to evict from it.
6050 arc_state_multilist_index_func(multilist_t
*ml
, void *obj
)
6052 arc_buf_hdr_t
*hdr
= obj
;
6055 * We rely on b_dva to generate evenly distributed index
6056 * numbers using buf_hash below. So, as an added precaution,
6057 * let's make sure we never add empty buffers to the arc lists.
6059 ASSERT(!HDR_EMPTY(hdr
));
6062 * The assumption here, is the hash value for a given
6063 * arc_buf_hdr_t will remain constant throughout its lifetime
6064 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
6065 * Thus, we don't need to store the header's sublist index
6066 * on insertion, as this index can be recalculated on removal.
6068 * Also, the low order bits of the hash value are thought to be
6069 * distributed evenly. Otherwise, in the case that the multilist
6070 * has a power of two number of sublists, each sublists' usage
6071 * would not be evenly distributed.
6073 return (buf_hash(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
) %
6074 multilist_get_num_sublists(ml
));
6078 * Called during module initialization and periodically thereafter to
6079 * apply reasonable changes to the exposed performance tunings. Non-zero
6080 * zfs_* values which differ from the currently set values will be applied.
6083 arc_tuning_update(void)
6086 /* Valid range: 64M - <all physical memory> */
6087 if ((zfs_arc_max
) && (zfs_arc_max
!= arc_c_max
) &&
6088 (zfs_arc_max
> 64 << 20) && (zfs_arc_max
< ptob(physmem
)) &&
6089 (zfs_arc_max
> arc_c_min
)) {
6090 arc_c_max
= zfs_arc_max
;
6092 arc_p
= (arc_c
>> 1);
6093 /* Valid range of arc_meta_limit: arc_meta_min - arc_c_max */
6094 percent
= MIN(zfs_arc_meta_limit_percent
, 100);
6095 arc_meta_limit
= MAX(arc_meta_min
, (percent
* arc_c_max
) / 100);
6096 percent
= MIN(zfs_arc_dnode_limit_percent
, 100);
6097 arc_dnode_limit
= (percent
* arc_meta_limit
) / 100;
6100 /* Valid range: 32M - <arc_c_max> */
6101 if ((zfs_arc_min
) && (zfs_arc_min
!= arc_c_min
) &&
6102 (zfs_arc_min
>= 2ULL << SPA_MAXBLOCKSHIFT
) &&
6103 (zfs_arc_min
<= arc_c_max
)) {
6104 arc_c_min
= zfs_arc_min
;
6105 arc_c
= MAX(arc_c
, arc_c_min
);
6108 /* Valid range: 16M - <arc_c_max> */
6109 if ((zfs_arc_meta_min
) && (zfs_arc_meta_min
!= arc_meta_min
) &&
6110 (zfs_arc_meta_min
>= 1ULL << SPA_MAXBLOCKSHIFT
) &&
6111 (zfs_arc_meta_min
<= arc_c_max
)) {
6112 arc_meta_min
= zfs_arc_meta_min
;
6113 arc_meta_limit
= MAX(arc_meta_limit
, arc_meta_min
);
6114 arc_dnode_limit
= arc_meta_limit
/ 10;
6117 /* Valid range: <arc_meta_min> - <arc_c_max> */
6118 if ((zfs_arc_meta_limit
) && (zfs_arc_meta_limit
!= arc_meta_limit
) &&
6119 (zfs_arc_meta_limit
>= zfs_arc_meta_min
) &&
6120 (zfs_arc_meta_limit
<= arc_c_max
))
6121 arc_meta_limit
= zfs_arc_meta_limit
;
6123 /* Valid range: <arc_meta_min> - <arc_c_max> */
6124 if ((zfs_arc_dnode_limit
) && (zfs_arc_dnode_limit
!= arc_dnode_limit
) &&
6125 (zfs_arc_dnode_limit
>= zfs_arc_meta_min
) &&
6126 (zfs_arc_dnode_limit
<= arc_c_max
))
6127 arc_dnode_limit
= zfs_arc_dnode_limit
;
6129 /* Valid range: 1 - N */
6130 if (zfs_arc_grow_retry
)
6131 arc_grow_retry
= zfs_arc_grow_retry
;
6133 /* Valid range: 1 - N */
6134 if (zfs_arc_shrink_shift
) {
6135 arc_shrink_shift
= zfs_arc_shrink_shift
;
6136 arc_no_grow_shift
= MIN(arc_no_grow_shift
, arc_shrink_shift
-1);
6139 /* Valid range: 1 - N */
6140 if (zfs_arc_p_min_shift
)
6141 arc_p_min_shift
= zfs_arc_p_min_shift
;
6143 /* Valid range: 1 - N ticks */
6144 if (zfs_arc_min_prefetch_lifespan
)
6145 arc_min_prefetch_lifespan
= zfs_arc_min_prefetch_lifespan
;
6147 /* Valid range: 0 - 100 */
6148 if ((zfs_arc_lotsfree_percent
>= 0) &&
6149 (zfs_arc_lotsfree_percent
<= 100))
6150 arc_lotsfree_percent
= zfs_arc_lotsfree_percent
;
6152 /* Valid range: 0 - <all physical memory> */
6153 if ((zfs_arc_sys_free
) && (zfs_arc_sys_free
!= arc_sys_free
))
6154 arc_sys_free
= MIN(MAX(zfs_arc_sys_free
, 0), ptob(physmem
));
6159 arc_state_init(void)
6161 arc_anon
= &ARC_anon
;
6163 arc_mru_ghost
= &ARC_mru_ghost
;
6165 arc_mfu_ghost
= &ARC_mfu_ghost
;
6166 arc_l2c_only
= &ARC_l2c_only
;
6168 multilist_create(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
6169 sizeof (arc_buf_hdr_t
),
6170 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6171 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6172 multilist_create(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
6173 sizeof (arc_buf_hdr_t
),
6174 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6175 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6176 multilist_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
6177 sizeof (arc_buf_hdr_t
),
6178 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6179 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6180 multilist_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
6181 sizeof (arc_buf_hdr_t
),
6182 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6183 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6184 multilist_create(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
6185 sizeof (arc_buf_hdr_t
),
6186 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6187 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6188 multilist_create(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
6189 sizeof (arc_buf_hdr_t
),
6190 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6191 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6192 multilist_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
6193 sizeof (arc_buf_hdr_t
),
6194 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6195 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6196 multilist_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
6197 sizeof (arc_buf_hdr_t
),
6198 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6199 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6200 multilist_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
6201 sizeof (arc_buf_hdr_t
),
6202 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6203 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6204 multilist_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
6205 sizeof (arc_buf_hdr_t
),
6206 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
6207 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
6209 refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
6210 refcount_create(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
6211 refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
6212 refcount_create(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
6213 refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
6214 refcount_create(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
6215 refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
6216 refcount_create(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
6217 refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
6218 refcount_create(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
6219 refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
6220 refcount_create(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
6222 refcount_create(&arc_anon
->arcs_size
);
6223 refcount_create(&arc_mru
->arcs_size
);
6224 refcount_create(&arc_mru_ghost
->arcs_size
);
6225 refcount_create(&arc_mfu
->arcs_size
);
6226 refcount_create(&arc_mfu_ghost
->arcs_size
);
6227 refcount_create(&arc_l2c_only
->arcs_size
);
6229 arc_anon
->arcs_state
= ARC_STATE_ANON
;
6230 arc_mru
->arcs_state
= ARC_STATE_MRU
;
6231 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
6232 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
6233 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
6234 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
6238 arc_state_fini(void)
6240 refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_METADATA
]);
6241 refcount_destroy(&arc_anon
->arcs_esize
[ARC_BUFC_DATA
]);
6242 refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]);
6243 refcount_destroy(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]);
6244 refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
6245 refcount_destroy(&arc_mru_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
6246 refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
6247 refcount_destroy(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]);
6248 refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_METADATA
]);
6249 refcount_destroy(&arc_mfu_ghost
->arcs_esize
[ARC_BUFC_DATA
]);
6250 refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_METADATA
]);
6251 refcount_destroy(&arc_l2c_only
->arcs_esize
[ARC_BUFC_DATA
]);
6253 refcount_destroy(&arc_anon
->arcs_size
);
6254 refcount_destroy(&arc_mru
->arcs_size
);
6255 refcount_destroy(&arc_mru_ghost
->arcs_size
);
6256 refcount_destroy(&arc_mfu
->arcs_size
);
6257 refcount_destroy(&arc_mfu_ghost
->arcs_size
);
6258 refcount_destroy(&arc_l2c_only
->arcs_size
);
6260 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
6261 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
6262 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
6263 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
6264 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
6265 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
6266 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
6267 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
6280 * allmem is "all memory that we could possibly use".
6283 uint64_t allmem
= ptob(physmem
);
6285 uint64_t allmem
= (physmem
* PAGESIZE
) / 2;
6289 mutex_init(&arc_reclaim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6290 cv_init(&arc_reclaim_thread_cv
, NULL
, CV_DEFAULT
, NULL
);
6291 cv_init(&arc_reclaim_waiters_cv
, NULL
, CV_DEFAULT
, NULL
);
6293 /* Convert seconds to clock ticks */
6294 arc_min_prefetch_lifespan
= 1 * hz
;
6298 * Register a shrinker to support synchronous (direct) memory
6299 * reclaim from the arc. This is done to prevent kswapd from
6300 * swapping out pages when it is preferable to shrink the arc.
6302 spl_register_shrinker(&arc_shrinker
);
6304 /* Set to 1/64 of all memory or a minimum of 512K */
6305 arc_sys_free
= MAX(ptob(physmem
/ 64), (512 * 1024));
6309 /* Set max to 1/2 of all memory */
6310 arc_c_max
= allmem
/ 2;
6313 * In userland, there's only the memory pressure that we artificially
6314 * create (see arc_available_memory()). Don't let arc_c get too
6315 * small, because it can cause transactions to be larger than
6316 * arc_c, causing arc_tempreserve_space() to fail.
6319 arc_c_min
= MAX(arc_c_max
/ 2, 2ULL << SPA_MAXBLOCKSHIFT
);
6321 arc_c_min
= 2ULL << SPA_MAXBLOCKSHIFT
;
6325 arc_p
= (arc_c
>> 1);
6328 /* Set min to 1/2 of arc_c_min */
6329 arc_meta_min
= 1ULL << SPA_MAXBLOCKSHIFT
;
6330 /* Initialize maximum observed usage to zero */
6333 * Set arc_meta_limit to a percent of arc_c_max with a floor of
6334 * arc_meta_min, and a ceiling of arc_c_max.
6336 percent
= MIN(zfs_arc_meta_limit_percent
, 100);
6337 arc_meta_limit
= MAX(arc_meta_min
, (percent
* arc_c_max
) / 100);
6338 percent
= MIN(zfs_arc_dnode_limit_percent
, 100);
6339 arc_dnode_limit
= (percent
* arc_meta_limit
) / 100;
6341 /* Apply user specified tunings */
6342 arc_tuning_update();
6344 if (zfs_arc_num_sublists_per_state
< 1)
6345 zfs_arc_num_sublists_per_state
= MAX(boot_ncpus
, 1);
6347 /* if kmem_flags are set, lets try to use less memory */
6348 if (kmem_debugging())
6350 if (arc_c
< arc_c_min
)
6356 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
6357 offsetof(arc_prune_t
, p_node
));
6358 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
6360 arc_prune_taskq
= taskq_create("arc_prune", max_ncpus
, defclsyspri
,
6361 max_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
6363 arc_reclaim_thread_exit
= B_FALSE
;
6365 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
6366 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
6368 if (arc_ksp
!= NULL
) {
6369 arc_ksp
->ks_data
= &arc_stats
;
6370 arc_ksp
->ks_update
= arc_kstat_update
;
6371 kstat_install(arc_ksp
);
6374 (void) thread_create(NULL
, 0, arc_reclaim_thread
, NULL
, 0, &p0
,
6375 TS_RUN
, defclsyspri
);
6381 * Calculate maximum amount of dirty data per pool.
6383 * If it has been set by a module parameter, take that.
6384 * Otherwise, use a percentage of physical memory defined by
6385 * zfs_dirty_data_max_percent (default 10%) with a cap at
6386 * zfs_dirty_data_max_max (default 25% of physical memory).
6388 if (zfs_dirty_data_max_max
== 0)
6389 zfs_dirty_data_max_max
= (uint64_t)physmem
* PAGESIZE
*
6390 zfs_dirty_data_max_max_percent
/ 100;
6392 if (zfs_dirty_data_max
== 0) {
6393 zfs_dirty_data_max
= (uint64_t)physmem
* PAGESIZE
*
6394 zfs_dirty_data_max_percent
/ 100;
6395 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
6396 zfs_dirty_data_max_max
);
6406 spl_unregister_shrinker(&arc_shrinker
);
6407 #endif /* _KERNEL */
6409 mutex_enter(&arc_reclaim_lock
);
6410 arc_reclaim_thread_exit
= B_TRUE
;
6412 * The reclaim thread will set arc_reclaim_thread_exit back to
6413 * B_FALSE when it is finished exiting; we're waiting for that.
6415 while (arc_reclaim_thread_exit
) {
6416 cv_signal(&arc_reclaim_thread_cv
);
6417 cv_wait(&arc_reclaim_thread_cv
, &arc_reclaim_lock
);
6419 mutex_exit(&arc_reclaim_lock
);
6421 /* Use B_TRUE to ensure *all* buffers are evicted */
6422 arc_flush(NULL
, B_TRUE
);
6426 if (arc_ksp
!= NULL
) {
6427 kstat_delete(arc_ksp
);
6431 taskq_wait(arc_prune_taskq
);
6432 taskq_destroy(arc_prune_taskq
);
6434 mutex_enter(&arc_prune_mtx
);
6435 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
6436 list_remove(&arc_prune_list
, p
);
6437 refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
6438 refcount_destroy(&p
->p_refcnt
);
6439 kmem_free(p
, sizeof (*p
));
6441 mutex_exit(&arc_prune_mtx
);
6443 list_destroy(&arc_prune_list
);
6444 mutex_destroy(&arc_prune_mtx
);
6445 mutex_destroy(&arc_reclaim_lock
);
6446 cv_destroy(&arc_reclaim_thread_cv
);
6447 cv_destroy(&arc_reclaim_waiters_cv
);
6452 ASSERT0(arc_loaned_bytes
);
6458 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
6459 * It uses dedicated storage devices to hold cached data, which are populated
6460 * using large infrequent writes. The main role of this cache is to boost
6461 * the performance of random read workloads. The intended L2ARC devices
6462 * include short-stroked disks, solid state disks, and other media with
6463 * substantially faster read latency than disk.
6465 * +-----------------------+
6467 * +-----------------------+
6470 * l2arc_feed_thread() arc_read()
6474 * +---------------+ |
6476 * +---------------+ |
6481 * +-------+ +-------+
6483 * | cache | | cache |
6484 * +-------+ +-------+
6485 * +=========+ .-----.
6486 * : L2ARC : |-_____-|
6487 * : devices : | Disks |
6488 * +=========+ `-_____-'
6490 * Read requests are satisfied from the following sources, in order:
6493 * 2) vdev cache of L2ARC devices
6495 * 4) vdev cache of disks
6498 * Some L2ARC device types exhibit extremely slow write performance.
6499 * To accommodate for this there are some significant differences between
6500 * the L2ARC and traditional cache design:
6502 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
6503 * the ARC behave as usual, freeing buffers and placing headers on ghost
6504 * lists. The ARC does not send buffers to the L2ARC during eviction as
6505 * this would add inflated write latencies for all ARC memory pressure.
6507 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
6508 * It does this by periodically scanning buffers from the eviction-end of
6509 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
6510 * not already there. It scans until a headroom of buffers is satisfied,
6511 * which itself is a buffer for ARC eviction. If a compressible buffer is
6512 * found during scanning and selected for writing to an L2ARC device, we
6513 * temporarily boost scanning headroom during the next scan cycle to make
6514 * sure we adapt to compression effects (which might significantly reduce
6515 * the data volume we write to L2ARC). The thread that does this is
6516 * l2arc_feed_thread(), illustrated below; example sizes are included to
6517 * provide a better sense of ratio than this diagram:
6520 * +---------------------+----------+
6521 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
6522 * +---------------------+----------+ | o L2ARC eligible
6523 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
6524 * +---------------------+----------+ |
6525 * 15.9 Gbytes ^ 32 Mbytes |
6527 * l2arc_feed_thread()
6529 * l2arc write hand <--[oooo]--'
6533 * +==============================+
6534 * L2ARC dev |####|#|###|###| |####| ... |
6535 * +==============================+
6538 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
6539 * evicted, then the L2ARC has cached a buffer much sooner than it probably
6540 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
6541 * safe to say that this is an uncommon case, since buffers at the end of
6542 * the ARC lists have moved there due to inactivity.
6544 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
6545 * then the L2ARC simply misses copying some buffers. This serves as a
6546 * pressure valve to prevent heavy read workloads from both stalling the ARC
6547 * with waits and clogging the L2ARC with writes. This also helps prevent
6548 * the potential for the L2ARC to churn if it attempts to cache content too
6549 * quickly, such as during backups of the entire pool.
6551 * 5. After system boot and before the ARC has filled main memory, there are
6552 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
6553 * lists can remain mostly static. Instead of searching from tail of these
6554 * lists as pictured, the l2arc_feed_thread() will search from the list heads
6555 * for eligible buffers, greatly increasing its chance of finding them.
6557 * The L2ARC device write speed is also boosted during this time so that
6558 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
6559 * there are no L2ARC reads, and no fear of degrading read performance
6560 * through increased writes.
6562 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
6563 * the vdev queue can aggregate them into larger and fewer writes. Each
6564 * device is written to in a rotor fashion, sweeping writes through
6565 * available space then repeating.
6567 * 7. The L2ARC does not store dirty content. It never needs to flush
6568 * write buffers back to disk based storage.
6570 * 8. If an ARC buffer is written (and dirtied) which also exists in the
6571 * L2ARC, the now stale L2ARC buffer is immediately dropped.
6573 * The performance of the L2ARC can be tweaked by a number of tunables, which
6574 * may be necessary for different workloads:
6576 * l2arc_write_max max write bytes per interval
6577 * l2arc_write_boost extra write bytes during device warmup
6578 * l2arc_noprefetch skip caching prefetched buffers
6579 * l2arc_headroom number of max device writes to precache
6580 * l2arc_headroom_boost when we find compressed buffers during ARC
6581 * scanning, we multiply headroom by this
6582 * percentage factor for the next scan cycle,
6583 * since more compressed buffers are likely to
6585 * l2arc_feed_secs seconds between L2ARC writing
6587 * Tunables may be removed or added as future performance improvements are
6588 * integrated, and also may become zpool properties.
6590 * There are three key functions that control how the L2ARC warms up:
6592 * l2arc_write_eligible() check if a buffer is eligible to cache
6593 * l2arc_write_size() calculate how much to write
6594 * l2arc_write_interval() calculate sleep delay between writes
6596 * These three functions determine what to write, how much, and how quickly
6601 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*hdr
)
6604 * A buffer is *not* eligible for the L2ARC if it:
6605 * 1. belongs to a different spa.
6606 * 2. is already cached on the L2ARC.
6607 * 3. has an I/O in progress (it may be an incomplete read).
6608 * 4. is flagged not eligible (zfs property).
6610 if (hdr
->b_spa
!= spa_guid
|| HDR_HAS_L2HDR(hdr
) ||
6611 HDR_IO_IN_PROGRESS(hdr
) || !HDR_L2CACHE(hdr
))
6618 l2arc_write_size(void)
6623 * Make sure our globals have meaningful values in case the user
6626 size
= l2arc_write_max
;
6628 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
6629 "be greater than zero, resetting it to the default (%d)",
6631 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
6634 if (arc_warm
== B_FALSE
)
6635 size
+= l2arc_write_boost
;
6642 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
6644 clock_t interval
, next
, now
;
6647 * If the ARC lists are busy, increase our write rate; if the
6648 * lists are stale, idle back. This is achieved by checking
6649 * how much we previously wrote - if it was more than half of
6650 * what we wanted, schedule the next write much sooner.
6652 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
6653 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
6655 interval
= hz
* l2arc_feed_secs
;
6657 now
= ddi_get_lbolt();
6658 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
6664 * Cycle through L2ARC devices. This is how L2ARC load balances.
6665 * If a device is returned, this also returns holding the spa config lock.
6667 static l2arc_dev_t
*
6668 l2arc_dev_get_next(void)
6670 l2arc_dev_t
*first
, *next
= NULL
;
6673 * Lock out the removal of spas (spa_namespace_lock), then removal
6674 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
6675 * both locks will be dropped and a spa config lock held instead.
6677 mutex_enter(&spa_namespace_lock
);
6678 mutex_enter(&l2arc_dev_mtx
);
6680 /* if there are no vdevs, there is nothing to do */
6681 if (l2arc_ndev
== 0)
6685 next
= l2arc_dev_last
;
6687 /* loop around the list looking for a non-faulted vdev */
6689 next
= list_head(l2arc_dev_list
);
6691 next
= list_next(l2arc_dev_list
, next
);
6693 next
= list_head(l2arc_dev_list
);
6696 /* if we have come back to the start, bail out */
6699 else if (next
== first
)
6702 } while (vdev_is_dead(next
->l2ad_vdev
));
6704 /* if we were unable to find any usable vdevs, return NULL */
6705 if (vdev_is_dead(next
->l2ad_vdev
))
6708 l2arc_dev_last
= next
;
6711 mutex_exit(&l2arc_dev_mtx
);
6714 * Grab the config lock to prevent the 'next' device from being
6715 * removed while we are writing to it.
6718 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
6719 mutex_exit(&spa_namespace_lock
);
6725 * Free buffers that were tagged for destruction.
6728 l2arc_do_free_on_write(void)
6731 l2arc_data_free_t
*df
, *df_prev
;
6733 mutex_enter(&l2arc_free_on_write_mtx
);
6734 buflist
= l2arc_free_on_write
;
6736 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
6737 df_prev
= list_prev(buflist
, df
);
6738 ASSERT3P(df
->l2df_data
, !=, NULL
);
6739 if (df
->l2df_type
== ARC_BUFC_METADATA
) {
6740 zio_buf_free(df
->l2df_data
, df
->l2df_size
);
6742 ASSERT(df
->l2df_type
== ARC_BUFC_DATA
);
6743 zio_data_buf_free(df
->l2df_data
, df
->l2df_size
);
6745 list_remove(buflist
, df
);
6746 kmem_free(df
, sizeof (l2arc_data_free_t
));
6749 mutex_exit(&l2arc_free_on_write_mtx
);
6753 * A write to a cache device has completed. Update all headers to allow
6754 * reads from these buffers to begin.
6757 l2arc_write_done(zio_t
*zio
)
6759 l2arc_write_callback_t
*cb
;
6762 arc_buf_hdr_t
*head
, *hdr
, *hdr_prev
;
6763 kmutex_t
*hash_lock
;
6764 int64_t bytes_dropped
= 0;
6766 cb
= zio
->io_private
;
6767 ASSERT3P(cb
, !=, NULL
);
6768 dev
= cb
->l2wcb_dev
;
6769 ASSERT3P(dev
, !=, NULL
);
6770 head
= cb
->l2wcb_head
;
6771 ASSERT3P(head
, !=, NULL
);
6772 buflist
= &dev
->l2ad_buflist
;
6773 ASSERT3P(buflist
, !=, NULL
);
6774 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
6775 l2arc_write_callback_t
*, cb
);
6777 if (zio
->io_error
!= 0)
6778 ARCSTAT_BUMP(arcstat_l2_writes_error
);
6781 * All writes completed, or an error was hit.
6784 mutex_enter(&dev
->l2ad_mtx
);
6785 for (hdr
= list_prev(buflist
, head
); hdr
; hdr
= hdr_prev
) {
6786 hdr_prev
= list_prev(buflist
, hdr
);
6788 hash_lock
= HDR_LOCK(hdr
);
6791 * We cannot use mutex_enter or else we can deadlock
6792 * with l2arc_write_buffers (due to swapping the order
6793 * the hash lock and l2ad_mtx are taken).
6795 if (!mutex_tryenter(hash_lock
)) {
6797 * Missed the hash lock. We must retry so we
6798 * don't leave the ARC_FLAG_L2_WRITING bit set.
6800 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry
);
6803 * We don't want to rescan the headers we've
6804 * already marked as having been written out, so
6805 * we reinsert the head node so we can pick up
6806 * where we left off.
6808 list_remove(buflist
, head
);
6809 list_insert_after(buflist
, hdr
, head
);
6811 mutex_exit(&dev
->l2ad_mtx
);
6814 * We wait for the hash lock to become available
6815 * to try and prevent busy waiting, and increase
6816 * the chance we'll be able to acquire the lock
6817 * the next time around.
6819 mutex_enter(hash_lock
);
6820 mutex_exit(hash_lock
);
6825 * We could not have been moved into the arc_l2c_only
6826 * state while in-flight due to our ARC_FLAG_L2_WRITING
6827 * bit being set. Let's just ensure that's being enforced.
6829 ASSERT(HDR_HAS_L1HDR(hdr
));
6832 * Skipped - drop L2ARC entry and mark the header as no
6833 * longer L2 eligibile.
6835 if (zio
->io_error
!= 0) {
6837 * Error - drop L2ARC entry.
6839 list_remove(buflist
, hdr
);
6840 arc_hdr_clear_flags(hdr
, ARC_FLAG_HAS_L2HDR
);
6842 ARCSTAT_INCR(arcstat_l2_asize
, -arc_hdr_size(hdr
));
6843 ARCSTAT_INCR(arcstat_l2_size
, -HDR_GET_LSIZE(hdr
));
6845 bytes_dropped
+= arc_hdr_size(hdr
);
6846 (void) refcount_remove_many(&dev
->l2ad_alloc
,
6847 arc_hdr_size(hdr
), hdr
);
6851 * Allow ARC to begin reads and ghost list evictions to
6854 arc_hdr_clear_flags(hdr
, ARC_FLAG_L2_WRITING
);
6856 mutex_exit(hash_lock
);
6859 atomic_inc_64(&l2arc_writes_done
);
6860 list_remove(buflist
, head
);
6861 ASSERT(!HDR_HAS_L1HDR(head
));
6862 kmem_cache_free(hdr_l2only_cache
, head
);
6863 mutex_exit(&dev
->l2ad_mtx
);
6865 vdev_space_update(dev
->l2ad_vdev
, -bytes_dropped
, 0, 0);
6867 l2arc_do_free_on_write();
6869 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
6873 * A read to a cache device completed. Validate buffer contents before
6874 * handing over to the regular ARC routines.
6877 l2arc_read_done(zio_t
*zio
)
6879 l2arc_read_callback_t
*cb
;
6881 kmutex_t
*hash_lock
;
6882 boolean_t valid_cksum
;
6884 ASSERT3P(zio
->io_vd
, !=, NULL
);
6885 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
6887 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
6889 cb
= zio
->io_private
;
6890 ASSERT3P(cb
, !=, NULL
);
6891 hdr
= cb
->l2rcb_hdr
;
6892 ASSERT3P(hdr
, !=, NULL
);
6894 hash_lock
= HDR_LOCK(hdr
);
6895 mutex_enter(hash_lock
);
6896 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
6898 ASSERT3P(zio
->io_data
, !=, NULL
);
6901 * Check this survived the L2ARC journey.
6903 ASSERT3P(zio
->io_data
, ==, hdr
->b_l1hdr
.b_pdata
);
6904 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
6905 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
6907 valid_cksum
= arc_cksum_is_equal(hdr
, zio
);
6908 if (valid_cksum
&& zio
->io_error
== 0 && !HDR_L2_EVICTED(hdr
)) {
6909 mutex_exit(hash_lock
);
6910 zio
->io_private
= hdr
;
6913 mutex_exit(hash_lock
);
6915 * Buffer didn't survive caching. Increment stats and
6916 * reissue to the original storage device.
6918 if (zio
->io_error
!= 0) {
6919 ARCSTAT_BUMP(arcstat_l2_io_error
);
6921 zio
->io_error
= SET_ERROR(EIO
);
6924 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
6927 * If there's no waiter, issue an async i/o to the primary
6928 * storage now. If there *is* a waiter, the caller must
6929 * issue the i/o in a context where it's OK to block.
6931 if (zio
->io_waiter
== NULL
) {
6932 zio_t
*pio
= zio_unique_parent(zio
);
6934 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
6936 zio_nowait(zio_read(pio
, zio
->io_spa
, zio
->io_bp
,
6937 hdr
->b_l1hdr
.b_pdata
, zio
->io_size
, arc_read_done
,
6938 hdr
, zio
->io_priority
, cb
->l2rcb_flags
,
6943 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
6947 * This is the list priority from which the L2ARC will search for pages to
6948 * cache. This is used within loops (0..3) to cycle through lists in the
6949 * desired order. This order can have a significant effect on cache
6952 * Currently the metadata lists are hit first, MFU then MRU, followed by
6953 * the data lists. This function returns a locked list, and also returns
6956 static multilist_sublist_t
*
6957 l2arc_sublist_lock(int list_num
)
6959 multilist_t
*ml
= NULL
;
6962 ASSERT(list_num
>= 0 && list_num
<= 3);
6966 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
6969 ml
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
6972 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
6975 ml
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
6980 * Return a randomly-selected sublist. This is acceptable
6981 * because the caller feeds only a little bit of data for each
6982 * call (8MB). Subsequent calls will result in different
6983 * sublists being selected.
6985 idx
= multilist_get_random_index(ml
);
6986 return (multilist_sublist_lock(ml
, idx
));
6990 * Evict buffers from the device write hand to the distance specified in
6991 * bytes. This distance may span populated buffers, it may span nothing.
6992 * This is clearing a region on the L2ARC device ready for writing.
6993 * If the 'all' boolean is set, every buffer is evicted.
6996 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
6999 arc_buf_hdr_t
*hdr
, *hdr_prev
;
7000 kmutex_t
*hash_lock
;
7003 buflist
= &dev
->l2ad_buflist
;
7005 if (!all
&& dev
->l2ad_first
) {
7007 * This is the first sweep through the device. There is
7013 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
7015 * When nearing the end of the device, evict to the end
7016 * before the device write hand jumps to the start.
7018 taddr
= dev
->l2ad_end
;
7020 taddr
= dev
->l2ad_hand
+ distance
;
7022 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
7023 uint64_t, taddr
, boolean_t
, all
);
7026 mutex_enter(&dev
->l2ad_mtx
);
7027 for (hdr
= list_tail(buflist
); hdr
; hdr
= hdr_prev
) {
7028 hdr_prev
= list_prev(buflist
, hdr
);
7030 hash_lock
= HDR_LOCK(hdr
);
7033 * We cannot use mutex_enter or else we can deadlock
7034 * with l2arc_write_buffers (due to swapping the order
7035 * the hash lock and l2ad_mtx are taken).
7037 if (!mutex_tryenter(hash_lock
)) {
7039 * Missed the hash lock. Retry.
7041 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
7042 mutex_exit(&dev
->l2ad_mtx
);
7043 mutex_enter(hash_lock
);
7044 mutex_exit(hash_lock
);
7048 if (HDR_L2_WRITE_HEAD(hdr
)) {
7050 * We hit a write head node. Leave it for
7051 * l2arc_write_done().
7053 list_remove(buflist
, hdr
);
7054 mutex_exit(hash_lock
);
7058 if (!all
&& HDR_HAS_L2HDR(hdr
) &&
7059 (hdr
->b_l2hdr
.b_daddr
> taddr
||
7060 hdr
->b_l2hdr
.b_daddr
< dev
->l2ad_hand
)) {
7062 * We've evicted to the target address,
7063 * or the end of the device.
7065 mutex_exit(hash_lock
);
7069 ASSERT(HDR_HAS_L2HDR(hdr
));
7070 if (!HDR_HAS_L1HDR(hdr
)) {
7071 ASSERT(!HDR_L2_READING(hdr
));
7073 * This doesn't exist in the ARC. Destroy.
7074 * arc_hdr_destroy() will call list_remove()
7075 * and decrement arcstat_l2_size.
7077 arc_change_state(arc_anon
, hdr
, hash_lock
);
7078 arc_hdr_destroy(hdr
);
7080 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_l2c_only
);
7081 ARCSTAT_BUMP(arcstat_l2_evict_l1cached
);
7083 * Invalidate issued or about to be issued
7084 * reads, since we may be about to write
7085 * over this location.
7087 if (HDR_L2_READING(hdr
)) {
7088 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
7089 arc_hdr_set_flags(hdr
, ARC_FLAG_L2_EVICTED
);
7092 /* Ensure this header has finished being written */
7093 ASSERT(!HDR_L2_WRITING(hdr
));
7095 arc_hdr_l2hdr_destroy(hdr
);
7097 mutex_exit(hash_lock
);
7099 mutex_exit(&dev
->l2ad_mtx
);
7103 * Find and write ARC buffers to the L2ARC device.
7105 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
7106 * for reading until they have completed writing.
7107 * The headroom_boost is an in-out parameter used to maintain headroom boost
7108 * state between calls to this function.
7110 * Returns the number of bytes actually written (which may be smaller than
7111 * the delta by which the device hand has changed due to alignment).
7114 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
)
7116 arc_buf_hdr_t
*hdr
, *hdr_prev
, *head
;
7117 uint64_t write_asize
, write_psize
, write_sz
, headroom
;
7119 l2arc_write_callback_t
*cb
;
7121 uint64_t guid
= spa_load_guid(spa
);
7124 ASSERT3P(dev
->l2ad_vdev
, !=, NULL
);
7127 write_sz
= write_asize
= write_psize
= 0;
7129 head
= kmem_cache_alloc(hdr_l2only_cache
, KM_PUSHPAGE
);
7130 arc_hdr_set_flags(head
, ARC_FLAG_L2_WRITE_HEAD
| ARC_FLAG_HAS_L2HDR
);
7133 * Copy buffers for L2ARC writing.
7135 for (try = 0; try <= 3; try++) {
7136 multilist_sublist_t
*mls
= l2arc_sublist_lock(try);
7137 uint64_t passed_sz
= 0;
7140 * L2ARC fast warmup.
7142 * Until the ARC is warm and starts to evict, read from the
7143 * head of the ARC lists rather than the tail.
7145 if (arc_warm
== B_FALSE
)
7146 hdr
= multilist_sublist_head(mls
);
7148 hdr
= multilist_sublist_tail(mls
);
7150 headroom
= target_sz
* l2arc_headroom
;
7151 if (zfs_compressed_arc_enabled
)
7152 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
7154 for (; hdr
; hdr
= hdr_prev
) {
7155 kmutex_t
*hash_lock
;
7156 uint64_t asize
, size
;
7159 if (arc_warm
== B_FALSE
)
7160 hdr_prev
= multilist_sublist_next(mls
, hdr
);
7162 hdr_prev
= multilist_sublist_prev(mls
, hdr
);
7164 hash_lock
= HDR_LOCK(hdr
);
7165 if (!mutex_tryenter(hash_lock
)) {
7167 * Skip this buffer rather than waiting.
7172 passed_sz
+= HDR_GET_LSIZE(hdr
);
7173 if (passed_sz
> headroom
) {
7177 mutex_exit(hash_lock
);
7181 if (!l2arc_write_eligible(guid
, hdr
)) {
7182 mutex_exit(hash_lock
);
7186 if ((write_asize
+ HDR_GET_LSIZE(hdr
)) > target_sz
) {
7188 mutex_exit(hash_lock
);
7194 * Insert a dummy header on the buflist so
7195 * l2arc_write_done() can find where the
7196 * write buffers begin without searching.
7198 mutex_enter(&dev
->l2ad_mtx
);
7199 list_insert_head(&dev
->l2ad_buflist
, head
);
7200 mutex_exit(&dev
->l2ad_mtx
);
7203 sizeof (l2arc_write_callback_t
), KM_SLEEP
);
7204 cb
->l2wcb_dev
= dev
;
7205 cb
->l2wcb_head
= head
;
7206 pio
= zio_root(spa
, l2arc_write_done
, cb
,
7210 hdr
->b_l2hdr
.b_dev
= dev
;
7211 hdr
->b_l2hdr
.b_hits
= 0;
7213 hdr
->b_l2hdr
.b_daddr
= dev
->l2ad_hand
;
7214 arc_hdr_set_flags(hdr
,
7215 ARC_FLAG_L2_WRITING
| ARC_FLAG_HAS_L2HDR
);
7217 mutex_enter(&dev
->l2ad_mtx
);
7218 list_insert_head(&dev
->l2ad_buflist
, hdr
);
7219 mutex_exit(&dev
->l2ad_mtx
);
7222 * We rely on the L1 portion of the header below, so
7223 * it's invalid for this header to have been evicted out
7224 * of the ghost cache, prior to being written out. The
7225 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
7227 ASSERT(HDR_HAS_L1HDR(hdr
));
7229 ASSERT3U(HDR_GET_PSIZE(hdr
), >, 0);
7230 ASSERT3P(hdr
->b_l1hdr
.b_pdata
, !=, NULL
);
7231 ASSERT3U(arc_hdr_size(hdr
), >, 0);
7232 size
= arc_hdr_size(hdr
);
7234 (void) refcount_add_many(&dev
->l2ad_alloc
, size
, hdr
);
7237 * Normally the L2ARC can use the hdr's data, but if
7238 * we're sharing data between the hdr and one of its
7239 * bufs, L2ARC needs its own copy of the data so that
7240 * the ZIO below can't race with the buf consumer. To
7241 * ensure that this copy will be available for the
7242 * lifetime of the ZIO and be cleaned up afterwards, we
7243 * add it to the l2arc_free_on_write queue.
7245 if (!HDR_SHARED_DATA(hdr
)) {
7246 to_write
= hdr
->b_l1hdr
.b_pdata
;
7248 arc_buf_contents_t type
= arc_buf_type(hdr
);
7249 if (type
== ARC_BUFC_METADATA
) {
7250 to_write
= zio_buf_alloc(size
);
7252 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
7253 to_write
= zio_data_buf_alloc(size
);
7256 bcopy(hdr
->b_l1hdr
.b_pdata
, to_write
, size
);
7257 l2arc_free_data_on_write(to_write
, size
, type
);
7259 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
7260 hdr
->b_l2hdr
.b_daddr
, size
, to_write
,
7261 ZIO_CHECKSUM_OFF
, NULL
, hdr
,
7262 ZIO_PRIORITY_ASYNC_WRITE
,
7263 ZIO_FLAG_CANFAIL
, B_FALSE
);
7265 write_sz
+= HDR_GET_LSIZE(hdr
);
7266 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
7269 write_asize
+= size
;
7271 * Keep the clock hand suitably device-aligned.
7273 asize
= vdev_psize_to_asize(dev
->l2ad_vdev
, size
);
7274 write_psize
+= asize
;
7275 dev
->l2ad_hand
+= asize
;
7277 mutex_exit(hash_lock
);
7279 (void) zio_nowait(wzio
);
7282 multilist_sublist_unlock(mls
);
7288 /* No buffers selected for writing? */
7291 ASSERT(!HDR_HAS_L1HDR(head
));
7292 kmem_cache_free(hdr_l2only_cache
, head
);
7296 ASSERT3U(write_asize
, <=, target_sz
);
7297 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
7298 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_asize
);
7299 ARCSTAT_INCR(arcstat_l2_size
, write_sz
);
7300 ARCSTAT_INCR(arcstat_l2_asize
, write_asize
);
7301 vdev_space_update(dev
->l2ad_vdev
, write_asize
, 0, 0);
7304 * Bump device hand to the device start if it is approaching the end.
7305 * l2arc_evict() will already have evicted ahead for this case.
7307 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
7308 dev
->l2ad_hand
= dev
->l2ad_start
;
7309 dev
->l2ad_first
= B_FALSE
;
7312 dev
->l2ad_writing
= B_TRUE
;
7313 (void) zio_wait(pio
);
7314 dev
->l2ad_writing
= B_FALSE
;
7316 return (write_asize
);
7320 * This thread feeds the L2ARC at regular intervals. This is the beating
7321 * heart of the L2ARC.
7324 l2arc_feed_thread(void)
7329 uint64_t size
, wrote
;
7330 clock_t begin
, next
= ddi_get_lbolt();
7331 fstrans_cookie_t cookie
;
7333 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
7335 mutex_enter(&l2arc_feed_thr_lock
);
7337 cookie
= spl_fstrans_mark();
7338 while (l2arc_thread_exit
== 0) {
7339 CALLB_CPR_SAFE_BEGIN(&cpr
);
7340 (void) cv_timedwait_sig(&l2arc_feed_thr_cv
,
7341 &l2arc_feed_thr_lock
, next
);
7342 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
7343 next
= ddi_get_lbolt() + hz
;
7346 * Quick check for L2ARC devices.
7348 mutex_enter(&l2arc_dev_mtx
);
7349 if (l2arc_ndev
== 0) {
7350 mutex_exit(&l2arc_dev_mtx
);
7353 mutex_exit(&l2arc_dev_mtx
);
7354 begin
= ddi_get_lbolt();
7357 * This selects the next l2arc device to write to, and in
7358 * doing so the next spa to feed from: dev->l2ad_spa. This
7359 * will return NULL if there are now no l2arc devices or if
7360 * they are all faulted.
7362 * If a device is returned, its spa's config lock is also
7363 * held to prevent device removal. l2arc_dev_get_next()
7364 * will grab and release l2arc_dev_mtx.
7366 if ((dev
= l2arc_dev_get_next()) == NULL
)
7369 spa
= dev
->l2ad_spa
;
7370 ASSERT3P(spa
, !=, NULL
);
7373 * If the pool is read-only then force the feed thread to
7374 * sleep a little longer.
7376 if (!spa_writeable(spa
)) {
7377 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
7378 spa_config_exit(spa
, SCL_L2ARC
, dev
);
7383 * Avoid contributing to memory pressure.
7385 if (arc_reclaim_needed()) {
7386 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
7387 spa_config_exit(spa
, SCL_L2ARC
, dev
);
7391 ARCSTAT_BUMP(arcstat_l2_feeds
);
7393 size
= l2arc_write_size();
7396 * Evict L2ARC buffers that will be overwritten.
7398 l2arc_evict(dev
, size
, B_FALSE
);
7401 * Write ARC buffers.
7403 wrote
= l2arc_write_buffers(spa
, dev
, size
);
7406 * Calculate interval between writes.
7408 next
= l2arc_write_interval(begin
, size
, wrote
);
7409 spa_config_exit(spa
, SCL_L2ARC
, dev
);
7411 spl_fstrans_unmark(cookie
);
7413 l2arc_thread_exit
= 0;
7414 cv_broadcast(&l2arc_feed_thr_cv
);
7415 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
7420 l2arc_vdev_present(vdev_t
*vd
)
7424 mutex_enter(&l2arc_dev_mtx
);
7425 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
7426 dev
= list_next(l2arc_dev_list
, dev
)) {
7427 if (dev
->l2ad_vdev
== vd
)
7430 mutex_exit(&l2arc_dev_mtx
);
7432 return (dev
!= NULL
);
7436 * Add a vdev for use by the L2ARC. By this point the spa has already
7437 * validated the vdev and opened it.
7440 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
7442 l2arc_dev_t
*adddev
;
7444 ASSERT(!l2arc_vdev_present(vd
));
7447 * Create a new l2arc device entry.
7449 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
7450 adddev
->l2ad_spa
= spa
;
7451 adddev
->l2ad_vdev
= vd
;
7452 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
7453 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
7454 adddev
->l2ad_hand
= adddev
->l2ad_start
;
7455 adddev
->l2ad_first
= B_TRUE
;
7456 adddev
->l2ad_writing
= B_FALSE
;
7457 list_link_init(&adddev
->l2ad_node
);
7459 mutex_init(&adddev
->l2ad_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
7461 * This is a list of all ARC buffers that are still valid on the
7464 list_create(&adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
7465 offsetof(arc_buf_hdr_t
, b_l2hdr
.b_l2node
));
7467 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
7468 refcount_create(&adddev
->l2ad_alloc
);
7471 * Add device to global list
7473 mutex_enter(&l2arc_dev_mtx
);
7474 list_insert_head(l2arc_dev_list
, adddev
);
7475 atomic_inc_64(&l2arc_ndev
);
7476 mutex_exit(&l2arc_dev_mtx
);
7480 * Remove a vdev from the L2ARC.
7483 l2arc_remove_vdev(vdev_t
*vd
)
7485 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
7488 * Find the device by vdev
7490 mutex_enter(&l2arc_dev_mtx
);
7491 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
7492 nextdev
= list_next(l2arc_dev_list
, dev
);
7493 if (vd
== dev
->l2ad_vdev
) {
7498 ASSERT3P(remdev
, !=, NULL
);
7501 * Remove device from global list
7503 list_remove(l2arc_dev_list
, remdev
);
7504 l2arc_dev_last
= NULL
; /* may have been invalidated */
7505 atomic_dec_64(&l2arc_ndev
);
7506 mutex_exit(&l2arc_dev_mtx
);
7509 * Clear all buflists and ARC references. L2ARC device flush.
7511 l2arc_evict(remdev
, 0, B_TRUE
);
7512 list_destroy(&remdev
->l2ad_buflist
);
7513 mutex_destroy(&remdev
->l2ad_mtx
);
7514 refcount_destroy(&remdev
->l2ad_alloc
);
7515 kmem_free(remdev
, sizeof (l2arc_dev_t
));
7521 l2arc_thread_exit
= 0;
7523 l2arc_writes_sent
= 0;
7524 l2arc_writes_done
= 0;
7526 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
7527 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
7528 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
7529 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
7531 l2arc_dev_list
= &L2ARC_dev_list
;
7532 l2arc_free_on_write
= &L2ARC_free_on_write
;
7533 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
7534 offsetof(l2arc_dev_t
, l2ad_node
));
7535 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
7536 offsetof(l2arc_data_free_t
, l2df_list_node
));
7543 * This is called from dmu_fini(), which is called from spa_fini();
7544 * Because of this, we can assume that all l2arc devices have
7545 * already been removed when the pools themselves were removed.
7548 l2arc_do_free_on_write();
7550 mutex_destroy(&l2arc_feed_thr_lock
);
7551 cv_destroy(&l2arc_feed_thr_cv
);
7552 mutex_destroy(&l2arc_dev_mtx
);
7553 mutex_destroy(&l2arc_free_on_write_mtx
);
7555 list_destroy(l2arc_dev_list
);
7556 list_destroy(l2arc_free_on_write
);
7562 if (!(spa_mode_global
& FWRITE
))
7565 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
7566 TS_RUN
, defclsyspri
);
7572 if (!(spa_mode_global
& FWRITE
))
7575 mutex_enter(&l2arc_feed_thr_lock
);
7576 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
7577 l2arc_thread_exit
= 1;
7578 while (l2arc_thread_exit
!= 0)
7579 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
7580 mutex_exit(&l2arc_feed_thr_lock
);
7583 #if defined(_KERNEL) && defined(HAVE_SPL)
7584 EXPORT_SYMBOL(arc_buf_size
);
7585 EXPORT_SYMBOL(arc_write
);
7586 EXPORT_SYMBOL(arc_read
);
7587 EXPORT_SYMBOL(arc_buf_info
);
7588 EXPORT_SYMBOL(arc_getbuf_func
);
7589 EXPORT_SYMBOL(arc_add_prune_callback
);
7590 EXPORT_SYMBOL(arc_remove_prune_callback
);
7592 module_param(zfs_arc_min
, ulong
, 0644);
7593 MODULE_PARM_DESC(zfs_arc_min
, "Min arc size");
7595 module_param(zfs_arc_max
, ulong
, 0644);
7596 MODULE_PARM_DESC(zfs_arc_max
, "Max arc size");
7598 module_param(zfs_arc_meta_limit
, ulong
, 0644);
7599 MODULE_PARM_DESC(zfs_arc_meta_limit
, "Meta limit for arc size");
7601 module_param(zfs_arc_meta_limit_percent
, ulong
, 0644);
7602 MODULE_PARM_DESC(zfs_arc_meta_limit_percent
,
7603 "Percent of arc size for arc meta limit");
7605 module_param(zfs_arc_meta_min
, ulong
, 0644);
7606 MODULE_PARM_DESC(zfs_arc_meta_min
, "Min arc metadata");
7608 module_param(zfs_arc_meta_prune
, int, 0644);
7609 MODULE_PARM_DESC(zfs_arc_meta_prune
, "Meta objects to scan for prune");
7611 module_param(zfs_arc_meta_adjust_restarts
, int, 0644);
7612 MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts
,
7613 "Limit number of restarts in arc_adjust_meta");
7615 module_param(zfs_arc_meta_strategy
, int, 0644);
7616 MODULE_PARM_DESC(zfs_arc_meta_strategy
, "Meta reclaim strategy");
7618 module_param(zfs_arc_grow_retry
, int, 0644);
7619 MODULE_PARM_DESC(zfs_arc_grow_retry
, "Seconds before growing arc size");
7621 module_param(zfs_arc_p_aggressive_disable
, int, 0644);
7622 MODULE_PARM_DESC(zfs_arc_p_aggressive_disable
, "disable aggressive arc_p grow");
7624 module_param(zfs_arc_p_dampener_disable
, int, 0644);
7625 MODULE_PARM_DESC(zfs_arc_p_dampener_disable
, "disable arc_p adapt dampener");
7627 module_param(zfs_arc_shrink_shift
, int, 0644);
7628 MODULE_PARM_DESC(zfs_arc_shrink_shift
, "log2(fraction of arc to reclaim)");
7630 module_param(zfs_arc_p_min_shift
, int, 0644);
7631 MODULE_PARM_DESC(zfs_arc_p_min_shift
, "arc_c shift to calc min/max arc_p");
7633 module_param(zfs_arc_average_blocksize
, int, 0444);
7634 MODULE_PARM_DESC(zfs_arc_average_blocksize
, "Target average block size");
7636 module_param(zfs_compressed_arc_enabled
, int, 0644);
7637 MODULE_PARM_DESC(zfs_arc_average_blocksize
, "Disable compressed arc buffers");
7639 module_param(zfs_arc_min_prefetch_lifespan
, int, 0644);
7640 MODULE_PARM_DESC(zfs_arc_min_prefetch_lifespan
, "Min life of prefetch block");
7642 module_param(zfs_arc_num_sublists_per_state
, int, 0644);
7643 MODULE_PARM_DESC(zfs_arc_num_sublists_per_state
,
7644 "Number of sublists used in each of the ARC state lists");
7646 module_param(l2arc_write_max
, ulong
, 0644);
7647 MODULE_PARM_DESC(l2arc_write_max
, "Max write bytes per interval");
7649 module_param(l2arc_write_boost
, ulong
, 0644);
7650 MODULE_PARM_DESC(l2arc_write_boost
, "Extra write bytes during device warmup");
7652 module_param(l2arc_headroom
, ulong
, 0644);
7653 MODULE_PARM_DESC(l2arc_headroom
, "Number of max device writes to precache");
7655 module_param(l2arc_headroom_boost
, ulong
, 0644);
7656 MODULE_PARM_DESC(l2arc_headroom_boost
, "Compressed l2arc_headroom multiplier");
7658 module_param(l2arc_feed_secs
, ulong
, 0644);
7659 MODULE_PARM_DESC(l2arc_feed_secs
, "Seconds between L2ARC writing");
7661 module_param(l2arc_feed_min_ms
, ulong
, 0644);
7662 MODULE_PARM_DESC(l2arc_feed_min_ms
, "Min feed interval in milliseconds");
7664 module_param(l2arc_noprefetch
, int, 0644);
7665 MODULE_PARM_DESC(l2arc_noprefetch
, "Skip caching prefetched buffers");
7667 module_param(l2arc_feed_again
, int, 0644);
7668 MODULE_PARM_DESC(l2arc_feed_again
, "Turbo L2ARC warmup");
7670 module_param(l2arc_norw
, int, 0644);
7671 MODULE_PARM_DESC(l2arc_norw
, "No reads during writes");
7673 module_param(zfs_arc_lotsfree_percent
, int, 0644);
7674 MODULE_PARM_DESC(zfs_arc_lotsfree_percent
,
7675 "System free memory I/O throttle in bytes");
7677 module_param(zfs_arc_sys_free
, ulong
, 0644);
7678 MODULE_PARM_DESC(zfs_arc_sys_free
, "System free memory target size in bytes");
7680 module_param(zfs_arc_dnode_limit
, ulong
, 0644);
7681 MODULE_PARM_DESC(zfs_arc_dnode_limit
, "Minimum bytes of dnodes in arc");
7683 module_param(zfs_arc_dnode_limit_percent
, ulong
, 0644);
7684 MODULE_PARM_DESC(zfs_arc_dnode_limit_percent
,
7685 "Percent of ARC meta buffers for dnodes");
7687 module_param(zfs_arc_dnode_reduce_percent
, ulong
, 0644);
7688 MODULE_PARM_DESC(zfs_arc_dnode_reduce_percent
,
7689 "Percentage of excess dnodes to try to unpin");