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) 2011, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
29 * DVA-based Adjustable Replacement Cache
31 * While much of the theory of operation used here is
32 * based on the self-tuning, low overhead replacement cache
33 * presented by Megiddo and Modha at FAST 2003, there are some
34 * significant differences:
36 * 1. The Megiddo and Modha model assumes any page is evictable.
37 * Pages in its cache cannot be "locked" into memory. This makes
38 * the eviction algorithm simple: evict the last page in the list.
39 * This also make the performance characteristics easy to reason
40 * about. Our cache is not so simple. At any given moment, some
41 * subset of the blocks in the cache are un-evictable because we
42 * have handed out a reference to them. Blocks are only evictable
43 * when there are no external references active. This makes
44 * eviction far more problematic: we choose to evict the evictable
45 * blocks that are the "lowest" in the list.
47 * There are times when it is not possible to evict the requested
48 * space. In these circumstances we are unable to adjust the cache
49 * size. To prevent the cache growing unbounded at these times we
50 * implement a "cache throttle" that slows the flow of new data
51 * into the cache until we can make space available.
53 * 2. The Megiddo and Modha model assumes a fixed cache size.
54 * Pages are evicted when the cache is full and there is a cache
55 * miss. Our model has a variable sized cache. It grows with
56 * high use, but also tries to react to memory pressure from the
57 * operating system: decreasing its size when system memory is
60 * 3. The Megiddo and Modha model assumes a fixed page size. All
61 * elements of the cache are therefore exactly the same size. So
62 * when adjusting the cache size following a cache miss, its simply
63 * a matter of choosing a single page to evict. In our model, we
64 * have variable sized cache blocks (rangeing from 512 bytes to
65 * 128K bytes). We therefore choose a set of blocks to evict to make
66 * space for a cache miss that approximates as closely as possible
67 * the space used by the new block.
69 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
70 * by N. Megiddo & D. Modha, FAST 2003
76 * A new reference to a cache buffer can be obtained in two
77 * ways: 1) via a hash table lookup using the DVA as a key,
78 * or 2) via one of the ARC lists. The arc_read() interface
79 * uses method 1, while the internal arc algorithms for
80 * adjusting the cache use method 2. We therefore provide two
81 * types of locks: 1) the hash table lock array, and 2) the
84 * Buffers do not have their own mutexes, rather they rely on the
85 * hash table mutexes for the bulk of their protection (i.e. most
86 * fields in the arc_buf_hdr_t are protected by these mutexes).
88 * buf_hash_find() returns the appropriate mutex (held) when it
89 * locates the requested buffer in the hash table. It returns
90 * NULL for the mutex if the buffer was not in the table.
92 * buf_hash_remove() expects the appropriate hash mutex to be
93 * already held before it is invoked.
95 * Each arc state also has a mutex which is used to protect the
96 * buffer list associated with the state. When attempting to
97 * obtain a hash table lock while holding an arc list lock you
98 * must use: mutex_tryenter() to avoid deadlock. Also note that
99 * the active state mutex must be held before the ghost state mutex.
101 * Arc buffers may have an associated eviction callback function.
102 * This function will be invoked prior to removing the buffer (e.g.
103 * in arc_do_user_evicts()). Note however that the data associated
104 * with the buffer may be evicted prior to the callback. The callback
105 * must be made with *no locks held* (to prevent deadlock). Additionally,
106 * the users of callbacks must ensure that their private data is
107 * protected from simultaneous callbacks from arc_clear_callback()
108 * and arc_do_user_evicts().
110 * It as also possible to register a callback which is run when the
111 * arc_meta_limit is reached and no buffers can be safely evicted. In
112 * this case the arc user should drop a reference on some arc buffers so
113 * they can be reclaimed and the arc_meta_limit honored. For example,
114 * when using the ZPL each dentry holds a references on a znode. These
115 * dentries must be pruned before the arc buffer holding the znode can
118 * Note that the majority of the performance stats are manipulated
119 * with atomic operations.
121 * The L2ARC uses the l2ad_mtx on each vdev for the following:
123 * - L2ARC buflist creation
124 * - L2ARC buflist eviction
125 * - L2ARC write completion, which walks L2ARC buflists
126 * - ARC header destruction, as it removes from L2ARC buflists
127 * - ARC header release, as it removes from L2ARC buflists
132 #include <sys/zio_compress.h>
133 #include <sys/zfs_context.h>
135 #include <sys/vdev.h>
136 #include <sys/vdev_impl.h>
137 #include <sys/dsl_pool.h>
138 #include <sys/multilist.h>
140 #include <sys/vmsystm.h>
142 #include <sys/fs/swapnode.h>
144 #include <linux/mm_compat.h>
146 #include <sys/callb.h>
147 #include <sys/kstat.h>
148 #include <sys/dmu_tx.h>
149 #include <zfs_fletcher.h>
150 #include <sys/arc_impl.h>
151 #include <sys/trace_arc.h>
154 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
155 boolean_t arc_watch
= B_FALSE
;
158 static kmutex_t arc_reclaim_lock
;
159 static kcondvar_t arc_reclaim_thread_cv
;
160 static boolean_t arc_reclaim_thread_exit
;
161 static kcondvar_t arc_reclaim_waiters_cv
;
163 static kmutex_t arc_user_evicts_lock
;
164 static kcondvar_t arc_user_evicts_cv
;
165 static boolean_t arc_user_evicts_thread_exit
;
168 * The number of headers to evict in arc_evict_state_impl() before
169 * dropping the sublist lock and evicting from another sublist. A lower
170 * value means we're more likely to evict the "correct" header (i.e. the
171 * oldest header in the arc state), but comes with higher overhead
172 * (i.e. more invocations of arc_evict_state_impl()).
174 int zfs_arc_evict_batch_limit
= 10;
177 * The number of sublists used for each of the arc state lists. If this
178 * is not set to a suitable value by the user, it will be configured to
179 * the number of CPUs on the system in arc_init().
181 int zfs_arc_num_sublists_per_state
= 0;
183 /* number of seconds before growing cache again */
184 static int arc_grow_retry
= 5;
186 /* shift of arc_c for calculating overflow limit in arc_get_data_buf */
187 int zfs_arc_overflow_shift
= 8;
189 /* log2(fraction of arc to reclaim) */
190 static int arc_shrink_shift
= 7;
193 * log2(fraction of ARC which must be free to allow growing).
194 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
195 * when reading a new block into the ARC, we will evict an equal-sized block
198 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
199 * we will still not allow it to grow.
201 int arc_no_grow_shift
= 5;
205 * minimum lifespan of a prefetch block in clock ticks
206 * (initialized in arc_init())
208 static int arc_min_prefetch_lifespan
;
211 * If this percent of memory is free, don't throttle.
213 int arc_lotsfree_percent
= 10;
218 * The arc has filled available memory and has now warmed up.
220 static boolean_t arc_warm
;
223 * These tunables are for performance analysis.
225 unsigned long zfs_arc_max
= 0;
226 unsigned long zfs_arc_min
= 0;
227 unsigned long zfs_arc_meta_limit
= 0;
228 unsigned long zfs_arc_meta_min
= 0;
229 int zfs_arc_grow_retry
= 0;
230 int zfs_arc_shrink_shift
= 0;
231 int zfs_disable_dup_eviction
= 0;
232 int zfs_arc_average_blocksize
= 8 * 1024; /* 8KB */
235 * These tunables are Linux specific
237 int zfs_arc_memory_throttle_disable
= 1;
238 int zfs_arc_min_prefetch_lifespan
= 0;
239 int zfs_arc_p_aggressive_disable
= 1;
240 int zfs_arc_p_dampener_disable
= 1;
241 int zfs_arc_meta_prune
= 10000;
242 int zfs_arc_meta_strategy
= ARC_STRATEGY_META_BALANCED
;
243 int zfs_arc_meta_adjust_restarts
= 4096;
246 static arc_state_t ARC_anon
;
247 static arc_state_t ARC_mru
;
248 static arc_state_t ARC_mru_ghost
;
249 static arc_state_t ARC_mfu
;
250 static arc_state_t ARC_mfu_ghost
;
251 static arc_state_t ARC_l2c_only
;
253 typedef struct arc_stats
{
254 kstat_named_t arcstat_hits
;
255 kstat_named_t arcstat_misses
;
256 kstat_named_t arcstat_demand_data_hits
;
257 kstat_named_t arcstat_demand_data_misses
;
258 kstat_named_t arcstat_demand_metadata_hits
;
259 kstat_named_t arcstat_demand_metadata_misses
;
260 kstat_named_t arcstat_prefetch_data_hits
;
261 kstat_named_t arcstat_prefetch_data_misses
;
262 kstat_named_t arcstat_prefetch_metadata_hits
;
263 kstat_named_t arcstat_prefetch_metadata_misses
;
264 kstat_named_t arcstat_mru_hits
;
265 kstat_named_t arcstat_mru_ghost_hits
;
266 kstat_named_t arcstat_mfu_hits
;
267 kstat_named_t arcstat_mfu_ghost_hits
;
268 kstat_named_t arcstat_deleted
;
270 * Number of buffers that could not be evicted because the hash lock
271 * was held by another thread. The lock may not necessarily be held
272 * by something using the same buffer, since hash locks are shared
273 * by multiple buffers.
275 kstat_named_t arcstat_mutex_miss
;
277 * Number of buffers skipped because they have I/O in progress, are
278 * indrect prefetch buffers that have not lived long enough, or are
279 * not from the spa we're trying to evict from.
281 kstat_named_t arcstat_evict_skip
;
283 * Number of times arc_evict_state() was unable to evict enough
284 * buffers to reach its target amount.
286 kstat_named_t arcstat_evict_not_enough
;
287 kstat_named_t arcstat_evict_l2_cached
;
288 kstat_named_t arcstat_evict_l2_eligible
;
289 kstat_named_t arcstat_evict_l2_ineligible
;
290 kstat_named_t arcstat_evict_l2_skip
;
291 kstat_named_t arcstat_hash_elements
;
292 kstat_named_t arcstat_hash_elements_max
;
293 kstat_named_t arcstat_hash_collisions
;
294 kstat_named_t arcstat_hash_chains
;
295 kstat_named_t arcstat_hash_chain_max
;
296 kstat_named_t arcstat_p
;
297 kstat_named_t arcstat_c
;
298 kstat_named_t arcstat_c_min
;
299 kstat_named_t arcstat_c_max
;
300 kstat_named_t arcstat_size
;
302 * Number of bytes consumed by internal ARC structures necessary
303 * for tracking purposes; these structures are not actually
304 * backed by ARC buffers. This includes arc_buf_hdr_t structures
305 * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only
306 * caches), and arc_buf_t structures (allocated via arc_buf_t
309 kstat_named_t arcstat_hdr_size
;
311 * Number of bytes consumed by ARC buffers of type equal to
312 * ARC_BUFC_DATA. This is generally consumed by buffers backing
313 * on disk user data (e.g. plain file contents).
315 kstat_named_t arcstat_data_size
;
317 * Number of bytes consumed by ARC buffers of type equal to
318 * ARC_BUFC_METADATA. This is generally consumed by buffers
319 * backing on disk data that is used for internal ZFS
320 * structures (e.g. ZAP, dnode, indirect blocks, etc).
322 kstat_named_t arcstat_metadata_size
;
324 * Number of bytes consumed by various buffers and structures
325 * not actually backed with ARC buffers. This includes bonus
326 * buffers (allocated directly via zio_buf_* functions),
327 * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t
328 * cache), and dnode_t structures (allocated via dnode_t cache).
330 kstat_named_t arcstat_other_size
;
332 * Total number of bytes consumed by ARC buffers residing in the
333 * arc_anon state. This includes *all* buffers in the arc_anon
334 * state; e.g. data, metadata, evictable, and unevictable buffers
335 * are all included in this value.
337 kstat_named_t arcstat_anon_size
;
339 * Number of bytes consumed by ARC buffers that meet the
340 * following criteria: backing buffers of type ARC_BUFC_DATA,
341 * residing in the arc_anon state, and are eligible for eviction
342 * (e.g. have no outstanding holds on the buffer).
344 kstat_named_t arcstat_anon_evictable_data
;
346 * Number of bytes consumed by ARC buffers that meet the
347 * following criteria: backing buffers of type ARC_BUFC_METADATA,
348 * residing in the arc_anon state, and are eligible for eviction
349 * (e.g. have no outstanding holds on the buffer).
351 kstat_named_t arcstat_anon_evictable_metadata
;
353 * Total number of bytes consumed by ARC buffers residing in the
354 * arc_mru state. This includes *all* buffers in the arc_mru
355 * state; e.g. data, metadata, evictable, and unevictable buffers
356 * are all included in this value.
358 kstat_named_t arcstat_mru_size
;
360 * Number of bytes consumed by ARC buffers that meet the
361 * following criteria: backing buffers of type ARC_BUFC_DATA,
362 * residing in the arc_mru state, and are eligible for eviction
363 * (e.g. have no outstanding holds on the buffer).
365 kstat_named_t arcstat_mru_evictable_data
;
367 * Number of bytes consumed by ARC buffers that meet the
368 * following criteria: backing buffers of type ARC_BUFC_METADATA,
369 * residing in the arc_mru state, and are eligible for eviction
370 * (e.g. have no outstanding holds on the buffer).
372 kstat_named_t arcstat_mru_evictable_metadata
;
374 * Total number of bytes that *would have been* consumed by ARC
375 * buffers in the arc_mru_ghost state. The key thing to note
376 * here, is the fact that this size doesn't actually indicate
377 * RAM consumption. The ghost lists only consist of headers and
378 * don't actually have ARC buffers linked off of these headers.
379 * Thus, *if* the headers had associated ARC buffers, these
380 * buffers *would have* consumed this number of bytes.
382 kstat_named_t arcstat_mru_ghost_size
;
384 * Number of bytes that *would have been* consumed by ARC
385 * buffers that are eligible for eviction, of type
386 * ARC_BUFC_DATA, and linked off the arc_mru_ghost state.
388 kstat_named_t arcstat_mru_ghost_evictable_data
;
390 * Number of bytes that *would have been* consumed by ARC
391 * buffers that are eligible for eviction, of type
392 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
394 kstat_named_t arcstat_mru_ghost_evictable_metadata
;
396 * Total number of bytes consumed by ARC buffers residing in the
397 * arc_mfu state. This includes *all* buffers in the arc_mfu
398 * state; e.g. data, metadata, evictable, and unevictable buffers
399 * are all included in this value.
401 kstat_named_t arcstat_mfu_size
;
403 * Number of bytes consumed by ARC buffers that are eligible for
404 * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu
407 kstat_named_t arcstat_mfu_evictable_data
;
409 * Number of bytes consumed by ARC buffers that are eligible for
410 * eviction, of type ARC_BUFC_METADATA, and reside in the
413 kstat_named_t arcstat_mfu_evictable_metadata
;
415 * Total number of bytes that *would have been* consumed by ARC
416 * buffers in the arc_mfu_ghost state. See the comment above
417 * arcstat_mru_ghost_size for more details.
419 kstat_named_t arcstat_mfu_ghost_size
;
421 * Number of bytes that *would have been* consumed by ARC
422 * buffers that are eligible for eviction, of type
423 * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state.
425 kstat_named_t arcstat_mfu_ghost_evictable_data
;
427 * Number of bytes that *would have been* consumed by ARC
428 * buffers that are eligible for eviction, of type
429 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
431 kstat_named_t arcstat_mfu_ghost_evictable_metadata
;
432 kstat_named_t arcstat_l2_hits
;
433 kstat_named_t arcstat_l2_misses
;
434 kstat_named_t arcstat_l2_feeds
;
435 kstat_named_t arcstat_l2_rw_clash
;
436 kstat_named_t arcstat_l2_read_bytes
;
437 kstat_named_t arcstat_l2_write_bytes
;
438 kstat_named_t arcstat_l2_writes_sent
;
439 kstat_named_t arcstat_l2_writes_done
;
440 kstat_named_t arcstat_l2_writes_error
;
441 kstat_named_t arcstat_l2_writes_lock_retry
;
442 kstat_named_t arcstat_l2_evict_lock_retry
;
443 kstat_named_t arcstat_l2_evict_reading
;
444 kstat_named_t arcstat_l2_evict_l1cached
;
445 kstat_named_t arcstat_l2_free_on_write
;
446 kstat_named_t arcstat_l2_cdata_free_on_write
;
447 kstat_named_t arcstat_l2_abort_lowmem
;
448 kstat_named_t arcstat_l2_cksum_bad
;
449 kstat_named_t arcstat_l2_io_error
;
450 kstat_named_t arcstat_l2_size
;
451 kstat_named_t arcstat_l2_asize
;
452 kstat_named_t arcstat_l2_hdr_size
;
453 kstat_named_t arcstat_l2_compress_successes
;
454 kstat_named_t arcstat_l2_compress_zeros
;
455 kstat_named_t arcstat_l2_compress_failures
;
456 kstat_named_t arcstat_memory_throttle_count
;
457 kstat_named_t arcstat_duplicate_buffers
;
458 kstat_named_t arcstat_duplicate_buffers_size
;
459 kstat_named_t arcstat_duplicate_reads
;
460 kstat_named_t arcstat_memory_direct_count
;
461 kstat_named_t arcstat_memory_indirect_count
;
462 kstat_named_t arcstat_no_grow
;
463 kstat_named_t arcstat_tempreserve
;
464 kstat_named_t arcstat_loaned_bytes
;
465 kstat_named_t arcstat_prune
;
466 kstat_named_t arcstat_meta_used
;
467 kstat_named_t arcstat_meta_limit
;
468 kstat_named_t arcstat_meta_max
;
469 kstat_named_t arcstat_meta_min
;
472 static arc_stats_t arc_stats
= {
473 { "hits", KSTAT_DATA_UINT64
},
474 { "misses", KSTAT_DATA_UINT64
},
475 { "demand_data_hits", KSTAT_DATA_UINT64
},
476 { "demand_data_misses", KSTAT_DATA_UINT64
},
477 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
478 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
479 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
480 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
481 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
482 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
483 { "mru_hits", KSTAT_DATA_UINT64
},
484 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
485 { "mfu_hits", KSTAT_DATA_UINT64
},
486 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
487 { "deleted", KSTAT_DATA_UINT64
},
488 { "mutex_miss", KSTAT_DATA_UINT64
},
489 { "evict_skip", KSTAT_DATA_UINT64
},
490 { "evict_not_enough", KSTAT_DATA_UINT64
},
491 { "evict_l2_cached", KSTAT_DATA_UINT64
},
492 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
493 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
494 { "evict_l2_skip", KSTAT_DATA_UINT64
},
495 { "hash_elements", KSTAT_DATA_UINT64
},
496 { "hash_elements_max", KSTAT_DATA_UINT64
},
497 { "hash_collisions", KSTAT_DATA_UINT64
},
498 { "hash_chains", KSTAT_DATA_UINT64
},
499 { "hash_chain_max", KSTAT_DATA_UINT64
},
500 { "p", KSTAT_DATA_UINT64
},
501 { "c", KSTAT_DATA_UINT64
},
502 { "c_min", KSTAT_DATA_UINT64
},
503 { "c_max", KSTAT_DATA_UINT64
},
504 { "size", KSTAT_DATA_UINT64
},
505 { "hdr_size", KSTAT_DATA_UINT64
},
506 { "data_size", KSTAT_DATA_UINT64
},
507 { "metadata_size", KSTAT_DATA_UINT64
},
508 { "other_size", KSTAT_DATA_UINT64
},
509 { "anon_size", KSTAT_DATA_UINT64
},
510 { "anon_evictable_data", KSTAT_DATA_UINT64
},
511 { "anon_evictable_metadata", KSTAT_DATA_UINT64
},
512 { "mru_size", KSTAT_DATA_UINT64
},
513 { "mru_evictable_data", KSTAT_DATA_UINT64
},
514 { "mru_evictable_metadata", KSTAT_DATA_UINT64
},
515 { "mru_ghost_size", KSTAT_DATA_UINT64
},
516 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64
},
517 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
518 { "mfu_size", KSTAT_DATA_UINT64
},
519 { "mfu_evictable_data", KSTAT_DATA_UINT64
},
520 { "mfu_evictable_metadata", KSTAT_DATA_UINT64
},
521 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
522 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64
},
523 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
524 { "l2_hits", KSTAT_DATA_UINT64
},
525 { "l2_misses", KSTAT_DATA_UINT64
},
526 { "l2_feeds", KSTAT_DATA_UINT64
},
527 { "l2_rw_clash", KSTAT_DATA_UINT64
},
528 { "l2_read_bytes", KSTAT_DATA_UINT64
},
529 { "l2_write_bytes", KSTAT_DATA_UINT64
},
530 { "l2_writes_sent", KSTAT_DATA_UINT64
},
531 { "l2_writes_done", KSTAT_DATA_UINT64
},
532 { "l2_writes_error", KSTAT_DATA_UINT64
},
533 { "l2_writes_lock_retry", KSTAT_DATA_UINT64
},
534 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
535 { "l2_evict_reading", KSTAT_DATA_UINT64
},
536 { "l2_evict_l1cached", KSTAT_DATA_UINT64
},
537 { "l2_free_on_write", KSTAT_DATA_UINT64
},
538 { "l2_cdata_free_on_write", KSTAT_DATA_UINT64
},
539 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
540 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
541 { "l2_io_error", KSTAT_DATA_UINT64
},
542 { "l2_size", KSTAT_DATA_UINT64
},
543 { "l2_asize", KSTAT_DATA_UINT64
},
544 { "l2_hdr_size", KSTAT_DATA_UINT64
},
545 { "l2_compress_successes", KSTAT_DATA_UINT64
},
546 { "l2_compress_zeros", KSTAT_DATA_UINT64
},
547 { "l2_compress_failures", KSTAT_DATA_UINT64
},
548 { "memory_throttle_count", KSTAT_DATA_UINT64
},
549 { "duplicate_buffers", KSTAT_DATA_UINT64
},
550 { "duplicate_buffers_size", KSTAT_DATA_UINT64
},
551 { "duplicate_reads", KSTAT_DATA_UINT64
},
552 { "memory_direct_count", KSTAT_DATA_UINT64
},
553 { "memory_indirect_count", KSTAT_DATA_UINT64
},
554 { "arc_no_grow", KSTAT_DATA_UINT64
},
555 { "arc_tempreserve", KSTAT_DATA_UINT64
},
556 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
557 { "arc_prune", KSTAT_DATA_UINT64
},
558 { "arc_meta_used", KSTAT_DATA_UINT64
},
559 { "arc_meta_limit", KSTAT_DATA_UINT64
},
560 { "arc_meta_max", KSTAT_DATA_UINT64
},
561 { "arc_meta_min", KSTAT_DATA_UINT64
}
564 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
566 #define ARCSTAT_INCR(stat, val) \
567 atomic_add_64(&arc_stats.stat.value.ui64, (val))
569 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
570 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
572 #define ARCSTAT_MAX(stat, val) { \
574 while ((val) > (m = arc_stats.stat.value.ui64) && \
575 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
579 #define ARCSTAT_MAXSTAT(stat) \
580 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
583 * We define a macro to allow ARC hits/misses to be easily broken down by
584 * two separate conditions, giving a total of four different subtypes for
585 * each of hits and misses (so eight statistics total).
587 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
590 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
592 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
596 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
598 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
603 static arc_state_t
*arc_anon
;
604 static arc_state_t
*arc_mru
;
605 static arc_state_t
*arc_mru_ghost
;
606 static arc_state_t
*arc_mfu
;
607 static arc_state_t
*arc_mfu_ghost
;
608 static arc_state_t
*arc_l2c_only
;
611 * There are several ARC variables that are critical to export as kstats --
612 * but we don't want to have to grovel around in the kstat whenever we wish to
613 * manipulate them. For these variables, we therefore define them to be in
614 * terms of the statistic variable. This assures that we are not introducing
615 * the possibility of inconsistency by having shadow copies of the variables,
616 * while still allowing the code to be readable.
618 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
619 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
620 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
621 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
622 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
623 #define arc_no_grow ARCSTAT(arcstat_no_grow)
624 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
625 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
626 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
627 #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */
628 #define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */
629 #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
631 #define L2ARC_IS_VALID_COMPRESS(_c_) \
632 ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY)
634 static list_t arc_prune_list
;
635 static kmutex_t arc_prune_mtx
;
636 static taskq_t
*arc_prune_taskq
;
637 static arc_buf_t
*arc_eviction_list
;
638 static arc_buf_hdr_t arc_eviction_hdr
;
640 #define GHOST_STATE(state) \
641 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
642 (state) == arc_l2c_only)
644 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
645 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
646 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
647 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
648 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ)
649 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE)
651 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
652 #define HDR_L2COMPRESS(hdr) ((hdr)->b_flags & ARC_FLAG_L2COMPRESS)
653 #define HDR_L2_READING(hdr) \
654 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
655 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
656 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
657 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
658 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
660 #define HDR_ISTYPE_METADATA(hdr) \
661 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
662 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
664 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
665 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
667 /* For storing compression mode in b_flags */
668 #define HDR_COMPRESS_OFFSET 24
669 #define HDR_COMPRESS_NBITS 7
671 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET(hdr->b_flags, \
672 HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS))
673 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET(hdr->b_flags, \
674 HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS, (cmp))
680 #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
681 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
684 * Hash table routines
687 #define HT_LOCK_ALIGN 64
688 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
693 unsigned char pad
[HT_LOCK_PAD
];
697 #define BUF_LOCKS 8192
698 typedef struct buf_hash_table
{
700 arc_buf_hdr_t
**ht_table
;
701 struct ht_lock ht_locks
[BUF_LOCKS
];
704 static buf_hash_table_t buf_hash_table
;
706 #define BUF_HASH_INDEX(spa, dva, birth) \
707 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
708 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
709 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
710 #define HDR_LOCK(hdr) \
711 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
713 uint64_t zfs_crc64_table
[256];
719 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
720 #define L2ARC_HEADROOM 2 /* num of writes */
722 * If we discover during ARC scan any buffers to be compressed, we boost
723 * our headroom for the next scanning cycle by this percentage multiple.
725 #define L2ARC_HEADROOM_BOOST 200
726 #define L2ARC_FEED_SECS 1 /* caching interval secs */
727 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
730 * Used to distinguish headers that are being process by
731 * l2arc_write_buffers(), but have yet to be assigned to a l2arc disk
732 * address. This can happen when the header is added to the l2arc's list
733 * of buffers to write in the first stage of l2arc_write_buffers(), but
734 * has not yet been written out which happens in the second stage of
735 * l2arc_write_buffers().
737 #define L2ARC_ADDR_UNSET ((uint64_t)(-1))
739 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
740 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
742 /* L2ARC Performance Tunables */
743 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
744 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
745 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
746 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
747 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
748 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
749 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
750 int l2arc_nocompress
= B_FALSE
; /* don't compress bufs */
751 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
752 int l2arc_norw
= B_FALSE
; /* no reads during writes */
757 static list_t L2ARC_dev_list
; /* device list */
758 static list_t
*l2arc_dev_list
; /* device list pointer */
759 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
760 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
761 static list_t L2ARC_free_on_write
; /* free after write buf list */
762 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
763 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
764 static uint64_t l2arc_ndev
; /* number of devices */
766 typedef struct l2arc_read_callback
{
767 arc_buf_t
*l2rcb_buf
; /* read buffer */
768 spa_t
*l2rcb_spa
; /* spa */
769 blkptr_t l2rcb_bp
; /* original blkptr */
770 zbookmark_phys_t l2rcb_zb
; /* original bookmark */
771 int l2rcb_flags
; /* original flags */
772 enum zio_compress l2rcb_compress
; /* applied compress */
773 } l2arc_read_callback_t
;
775 typedef struct l2arc_data_free
{
776 /* protected by l2arc_free_on_write_mtx */
779 void (*l2df_func
)(void *, size_t);
780 list_node_t l2df_list_node
;
783 static kmutex_t l2arc_feed_thr_lock
;
784 static kcondvar_t l2arc_feed_thr_cv
;
785 static uint8_t l2arc_thread_exit
;
787 static void arc_get_data_buf(arc_buf_t
*);
788 static void arc_access(arc_buf_hdr_t
*, kmutex_t
*);
789 static boolean_t
arc_is_overflowing(void);
790 static void arc_buf_watch(arc_buf_t
*);
791 static void arc_tuning_update(void);
793 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t
*);
794 static uint32_t arc_bufc_to_flags(arc_buf_contents_t
);
796 static boolean_t
l2arc_write_eligible(uint64_t, arc_buf_hdr_t
*);
797 static void l2arc_read_done(zio_t
*);
799 static boolean_t
l2arc_compress_buf(arc_buf_hdr_t
*);
800 static void l2arc_decompress_zio(zio_t
*, arc_buf_hdr_t
*, enum zio_compress
);
801 static void l2arc_release_cdata_buf(arc_buf_hdr_t
*);
804 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
806 uint8_t *vdva
= (uint8_t *)dva
;
807 uint64_t crc
= -1ULL;
810 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
812 for (i
= 0; i
< sizeof (dva_t
); i
++)
813 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ vdva
[i
]) & 0xFF];
815 crc
^= (spa
>>8) ^ birth
;
820 #define BUF_EMPTY(buf) \
821 ((buf)->b_dva.dva_word[0] == 0 && \
822 (buf)->b_dva.dva_word[1] == 0)
824 #define BUF_EQUAL(spa, dva, birth, buf) \
825 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
826 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
827 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
830 buf_discard_identity(arc_buf_hdr_t
*hdr
)
832 hdr
->b_dva
.dva_word
[0] = 0;
833 hdr
->b_dva
.dva_word
[1] = 0;
837 static arc_buf_hdr_t
*
838 buf_hash_find(uint64_t spa
, const blkptr_t
*bp
, kmutex_t
**lockp
)
840 const dva_t
*dva
= BP_IDENTITY(bp
);
841 uint64_t birth
= BP_PHYSICAL_BIRTH(bp
);
842 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
843 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
846 mutex_enter(hash_lock
);
847 for (hdr
= buf_hash_table
.ht_table
[idx
]; hdr
!= NULL
;
848 hdr
= hdr
->b_hash_next
) {
849 if (BUF_EQUAL(spa
, dva
, birth
, hdr
)) {
854 mutex_exit(hash_lock
);
860 * Insert an entry into the hash table. If there is already an element
861 * equal to elem in the hash table, then the already existing element
862 * will be returned and the new element will not be inserted.
863 * Otherwise returns NULL.
864 * If lockp == NULL, the caller is assumed to already hold the hash lock.
866 static arc_buf_hdr_t
*
867 buf_hash_insert(arc_buf_hdr_t
*hdr
, kmutex_t
**lockp
)
869 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
870 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
874 ASSERT(!DVA_IS_EMPTY(&hdr
->b_dva
));
875 ASSERT(hdr
->b_birth
!= 0);
876 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
880 mutex_enter(hash_lock
);
882 ASSERT(MUTEX_HELD(hash_lock
));
885 for (fhdr
= buf_hash_table
.ht_table
[idx
], i
= 0; fhdr
!= NULL
;
886 fhdr
= fhdr
->b_hash_next
, i
++) {
887 if (BUF_EQUAL(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
, fhdr
))
891 hdr
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
892 buf_hash_table
.ht_table
[idx
] = hdr
;
893 hdr
->b_flags
|= ARC_FLAG_IN_HASH_TABLE
;
895 /* collect some hash table performance data */
897 ARCSTAT_BUMP(arcstat_hash_collisions
);
899 ARCSTAT_BUMP(arcstat_hash_chains
);
901 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
904 ARCSTAT_BUMP(arcstat_hash_elements
);
905 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
911 buf_hash_remove(arc_buf_hdr_t
*hdr
)
913 arc_buf_hdr_t
*fhdr
, **hdrp
;
914 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
916 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
917 ASSERT(HDR_IN_HASH_TABLE(hdr
));
919 hdrp
= &buf_hash_table
.ht_table
[idx
];
920 while ((fhdr
= *hdrp
) != hdr
) {
921 ASSERT(fhdr
!= NULL
);
922 hdrp
= &fhdr
->b_hash_next
;
924 *hdrp
= hdr
->b_hash_next
;
925 hdr
->b_hash_next
= NULL
;
926 hdr
->b_flags
&= ~ARC_FLAG_IN_HASH_TABLE
;
928 /* collect some hash table performance data */
929 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
931 if (buf_hash_table
.ht_table
[idx
] &&
932 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
933 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
937 * Global data structures and functions for the buf kmem cache.
939 static kmem_cache_t
*hdr_full_cache
;
940 static kmem_cache_t
*hdr_l2only_cache
;
941 static kmem_cache_t
*buf_cache
;
948 #if defined(_KERNEL) && defined(HAVE_SPL)
950 * Large allocations which do not require contiguous pages
951 * should be using vmem_free() in the linux kernel\
953 vmem_free(buf_hash_table
.ht_table
,
954 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
956 kmem_free(buf_hash_table
.ht_table
,
957 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
959 for (i
= 0; i
< BUF_LOCKS
; i
++)
960 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
961 kmem_cache_destroy(hdr_full_cache
);
962 kmem_cache_destroy(hdr_l2only_cache
);
963 kmem_cache_destroy(buf_cache
);
967 * Constructor callback - called when the cache is empty
968 * and a new buf is requested.
972 hdr_full_cons(void *vbuf
, void *unused
, int kmflag
)
974 arc_buf_hdr_t
*hdr
= vbuf
;
976 bzero(hdr
, HDR_FULL_SIZE
);
977 cv_init(&hdr
->b_l1hdr
.b_cv
, NULL
, CV_DEFAULT
, NULL
);
978 refcount_create(&hdr
->b_l1hdr
.b_refcnt
);
979 mutex_init(&hdr
->b_l1hdr
.b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
980 list_link_init(&hdr
->b_l1hdr
.b_arc_node
);
981 list_link_init(&hdr
->b_l2hdr
.b_l2node
);
982 multilist_link_init(&hdr
->b_l1hdr
.b_arc_node
);
983 arc_space_consume(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
990 hdr_l2only_cons(void *vbuf
, void *unused
, int kmflag
)
992 arc_buf_hdr_t
*hdr
= vbuf
;
994 bzero(hdr
, HDR_L2ONLY_SIZE
);
995 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1002 buf_cons(void *vbuf
, void *unused
, int kmflag
)
1004 arc_buf_t
*buf
= vbuf
;
1006 bzero(buf
, sizeof (arc_buf_t
));
1007 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1008 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1014 * Destructor callback - called when a cached buf is
1015 * no longer required.
1019 hdr_full_dest(void *vbuf
, void *unused
)
1021 arc_buf_hdr_t
*hdr
= vbuf
;
1023 ASSERT(BUF_EMPTY(hdr
));
1024 cv_destroy(&hdr
->b_l1hdr
.b_cv
);
1025 refcount_destroy(&hdr
->b_l1hdr
.b_refcnt
);
1026 mutex_destroy(&hdr
->b_l1hdr
.b_freeze_lock
);
1027 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1028 arc_space_return(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1033 hdr_l2only_dest(void *vbuf
, void *unused
)
1035 ASSERTV(arc_buf_hdr_t
*hdr
= vbuf
);
1037 ASSERT(BUF_EMPTY(hdr
));
1038 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1043 buf_dest(void *vbuf
, void *unused
)
1045 arc_buf_t
*buf
= vbuf
;
1047 mutex_destroy(&buf
->b_evict_lock
);
1048 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1055 uint64_t hsize
= 1ULL << 12;
1059 * The hash table is big enough to fill all of physical memory
1060 * with an average block size of zfs_arc_average_blocksize (default 8K).
1061 * By default, the table will take up
1062 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1064 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
1067 buf_hash_table
.ht_mask
= hsize
- 1;
1068 #if defined(_KERNEL) && defined(HAVE_SPL)
1070 * Large allocations which do not require contiguous pages
1071 * should be using vmem_alloc() in the linux kernel
1073 buf_hash_table
.ht_table
=
1074 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1076 buf_hash_table
.ht_table
=
1077 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1079 if (buf_hash_table
.ht_table
== NULL
) {
1080 ASSERT(hsize
> (1ULL << 8));
1085 hdr_full_cache
= kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE
,
1086 0, hdr_full_cons
, hdr_full_dest
, NULL
, NULL
, NULL
, 0);
1087 hdr_l2only_cache
= kmem_cache_create("arc_buf_hdr_t_l2only",
1088 HDR_L2ONLY_SIZE
, 0, hdr_l2only_cons
, hdr_l2only_dest
, NULL
,
1090 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1091 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1093 for (i
= 0; i
< 256; i
++)
1094 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1095 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1097 for (i
= 0; i
< BUF_LOCKS
; i
++) {
1098 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
1099 NULL
, MUTEX_DEFAULT
, NULL
);
1104 * Transition between the two allocation states for the arc_buf_hdr struct.
1105 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
1106 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
1107 * version is used when a cache buffer is only in the L2ARC in order to reduce
1110 static arc_buf_hdr_t
*
1111 arc_hdr_realloc(arc_buf_hdr_t
*hdr
, kmem_cache_t
*old
, kmem_cache_t
*new)
1113 arc_buf_hdr_t
*nhdr
;
1116 ASSERT(HDR_HAS_L2HDR(hdr
));
1117 ASSERT((old
== hdr_full_cache
&& new == hdr_l2only_cache
) ||
1118 (old
== hdr_l2only_cache
&& new == hdr_full_cache
));
1120 dev
= hdr
->b_l2hdr
.b_dev
;
1121 nhdr
= kmem_cache_alloc(new, KM_PUSHPAGE
);
1123 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
1124 buf_hash_remove(hdr
);
1126 bcopy(hdr
, nhdr
, HDR_L2ONLY_SIZE
);
1128 if (new == hdr_full_cache
) {
1129 nhdr
->b_flags
|= ARC_FLAG_HAS_L1HDR
;
1131 * arc_access and arc_change_state need to be aware that a
1132 * header has just come out of L2ARC, so we set its state to
1133 * l2c_only even though it's about to change.
1135 nhdr
->b_l1hdr
.b_state
= arc_l2c_only
;
1137 /* Verify previous threads set to NULL before freeing */
1138 ASSERT3P(nhdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
1140 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
);
1141 ASSERT0(hdr
->b_l1hdr
.b_datacnt
);
1144 * If we've reached here, We must have been called from
1145 * arc_evict_hdr(), as such we should have already been
1146 * removed from any ghost list we were previously on
1147 * (which protects us from racing with arc_evict_state),
1148 * thus no locking is needed during this check.
1150 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1153 * A buffer must not be moved into the arc_l2c_only
1154 * state if it's not finished being written out to the
1155 * l2arc device. Otherwise, the b_l1hdr.b_tmp_cdata field
1156 * might try to be accessed, even though it was removed.
1158 VERIFY(!HDR_L2_WRITING(hdr
));
1159 VERIFY3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
1161 nhdr
->b_flags
&= ~ARC_FLAG_HAS_L1HDR
;
1164 * The header has been reallocated so we need to re-insert it into any
1167 (void) buf_hash_insert(nhdr
, NULL
);
1169 ASSERT(list_link_active(&hdr
->b_l2hdr
.b_l2node
));
1171 mutex_enter(&dev
->l2ad_mtx
);
1174 * We must place the realloc'ed header back into the list at
1175 * the same spot. Otherwise, if it's placed earlier in the list,
1176 * l2arc_write_buffers() could find it during the function's
1177 * write phase, and try to write it out to the l2arc.
1179 list_insert_after(&dev
->l2ad_buflist
, hdr
, nhdr
);
1180 list_remove(&dev
->l2ad_buflist
, hdr
);
1182 mutex_exit(&dev
->l2ad_mtx
);
1185 * Since we're using the pointer address as the tag when
1186 * incrementing and decrementing the l2ad_alloc refcount, we
1187 * must remove the old pointer (that we're about to destroy) and
1188 * add the new pointer to the refcount. Otherwise we'd remove
1189 * the wrong pointer address when calling arc_hdr_destroy() later.
1192 (void) refcount_remove_many(&dev
->l2ad_alloc
,
1193 hdr
->b_l2hdr
.b_asize
, hdr
);
1195 (void) refcount_add_many(&dev
->l2ad_alloc
,
1196 nhdr
->b_l2hdr
.b_asize
, nhdr
);
1198 buf_discard_identity(hdr
);
1199 hdr
->b_freeze_cksum
= NULL
;
1200 kmem_cache_free(old
, hdr
);
1206 #define ARC_MINTIME (hz>>4) /* 62 ms */
1209 arc_cksum_verify(arc_buf_t
*buf
)
1213 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1216 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1217 if (buf
->b_hdr
->b_freeze_cksum
== NULL
|| HDR_IO_ERROR(buf
->b_hdr
)) {
1218 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1221 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
1222 if (!ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
))
1223 panic("buffer modified while frozen!");
1224 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1228 arc_cksum_equal(arc_buf_t
*buf
)
1233 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1234 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
1235 equal
= ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
);
1236 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1242 arc_cksum_compute(arc_buf_t
*buf
, boolean_t force
)
1244 if (!force
&& !(zfs_flags
& ZFS_DEBUG_MODIFY
))
1247 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1248 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
1249 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1252 buf
->b_hdr
->b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1254 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
,
1255 buf
->b_hdr
->b_freeze_cksum
);
1256 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1262 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1264 panic("Got SIGSEGV at address: 0x%lx\n", (long) si
->si_addr
);
1270 arc_buf_unwatch(arc_buf_t
*buf
)
1274 ASSERT0(mprotect(buf
->b_data
, buf
->b_hdr
->b_size
,
1275 PROT_READ
| PROT_WRITE
));
1282 arc_buf_watch(arc_buf_t
*buf
)
1286 ASSERT0(mprotect(buf
->b_data
, buf
->b_hdr
->b_size
, PROT_READ
));
1290 static arc_buf_contents_t
1291 arc_buf_type(arc_buf_hdr_t
*hdr
)
1293 if (HDR_ISTYPE_METADATA(hdr
)) {
1294 return (ARC_BUFC_METADATA
);
1296 return (ARC_BUFC_DATA
);
1301 arc_bufc_to_flags(arc_buf_contents_t type
)
1305 /* metadata field is 0 if buffer contains normal data */
1307 case ARC_BUFC_METADATA
:
1308 return (ARC_FLAG_BUFC_METADATA
);
1312 panic("undefined ARC buffer type!");
1313 return ((uint32_t)-1);
1317 arc_buf_thaw(arc_buf_t
*buf
)
1319 if (zfs_flags
& ZFS_DEBUG_MODIFY
) {
1320 if (buf
->b_hdr
->b_l1hdr
.b_state
!= arc_anon
)
1321 panic("modifying non-anon buffer!");
1322 if (HDR_IO_IN_PROGRESS(buf
->b_hdr
))
1323 panic("modifying buffer while i/o in progress!");
1324 arc_cksum_verify(buf
);
1327 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1328 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
1329 kmem_free(buf
->b_hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
1330 buf
->b_hdr
->b_freeze_cksum
= NULL
;
1333 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1335 arc_buf_unwatch(buf
);
1339 arc_buf_freeze(arc_buf_t
*buf
)
1341 kmutex_t
*hash_lock
;
1343 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1346 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1347 mutex_enter(hash_lock
);
1349 ASSERT(buf
->b_hdr
->b_freeze_cksum
!= NULL
||
1350 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
1351 arc_cksum_compute(buf
, B_FALSE
);
1352 mutex_exit(hash_lock
);
1357 add_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
1361 ASSERT(HDR_HAS_L1HDR(hdr
));
1362 ASSERT(MUTEX_HELD(hash_lock
));
1364 state
= hdr
->b_l1hdr
.b_state
;
1366 if ((refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
) == 1) &&
1367 (state
!= arc_anon
)) {
1368 /* We don't use the L2-only state list. */
1369 if (state
!= arc_l2c_only
) {
1370 arc_buf_contents_t type
= arc_buf_type(hdr
);
1371 uint64_t delta
= hdr
->b_size
* hdr
->b_l1hdr
.b_datacnt
;
1372 multilist_t
*list
= &state
->arcs_list
[type
];
1373 uint64_t *size
= &state
->arcs_lsize
[type
];
1375 multilist_remove(list
, hdr
);
1377 if (GHOST_STATE(state
)) {
1378 ASSERT0(hdr
->b_l1hdr
.b_datacnt
);
1379 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
1380 delta
= hdr
->b_size
;
1383 ASSERT3U(*size
, >=, delta
);
1384 atomic_add_64(size
, -delta
);
1386 /* remove the prefetch flag if we get a reference */
1387 hdr
->b_flags
&= ~ARC_FLAG_PREFETCH
;
1392 remove_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
1395 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
1397 ASSERT(HDR_HAS_L1HDR(hdr
));
1398 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
1399 ASSERT(!GHOST_STATE(state
));
1402 * arc_l2c_only counts as a ghost state so we don't need to explicitly
1403 * check to prevent usage of the arc_l2c_only list.
1405 if (((cnt
= refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
)) == 0) &&
1406 (state
!= arc_anon
)) {
1407 arc_buf_contents_t type
= arc_buf_type(hdr
);
1408 multilist_t
*list
= &state
->arcs_list
[type
];
1409 uint64_t *size
= &state
->arcs_lsize
[type
];
1411 multilist_insert(list
, hdr
);
1413 ASSERT(hdr
->b_l1hdr
.b_datacnt
> 0);
1414 atomic_add_64(size
, hdr
->b_size
*
1415 hdr
->b_l1hdr
.b_datacnt
);
1421 * Returns detailed information about a specific arc buffer. When the
1422 * state_index argument is set the function will calculate the arc header
1423 * list position for its arc state. Since this requires a linear traversal
1424 * callers are strongly encourage not to do this. However, it can be helpful
1425 * for targeted analysis so the functionality is provided.
1428 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
1430 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
1431 l1arc_buf_hdr_t
*l1hdr
= NULL
;
1432 l2arc_buf_hdr_t
*l2hdr
= NULL
;
1433 arc_state_t
*state
= NULL
;
1435 if (HDR_HAS_L1HDR(hdr
)) {
1436 l1hdr
= &hdr
->b_l1hdr
;
1437 state
= l1hdr
->b_state
;
1439 if (HDR_HAS_L2HDR(hdr
))
1440 l2hdr
= &hdr
->b_l2hdr
;
1442 memset(abi
, 0, sizeof (arc_buf_info_t
));
1443 abi
->abi_flags
= hdr
->b_flags
;
1446 abi
->abi_datacnt
= l1hdr
->b_datacnt
;
1447 abi
->abi_access
= l1hdr
->b_arc_access
;
1448 abi
->abi_mru_hits
= l1hdr
->b_mru_hits
;
1449 abi
->abi_mru_ghost_hits
= l1hdr
->b_mru_ghost_hits
;
1450 abi
->abi_mfu_hits
= l1hdr
->b_mfu_hits
;
1451 abi
->abi_mfu_ghost_hits
= l1hdr
->b_mfu_ghost_hits
;
1452 abi
->abi_holds
= refcount_count(&l1hdr
->b_refcnt
);
1456 abi
->abi_l2arc_dattr
= l2hdr
->b_daddr
;
1457 abi
->abi_l2arc_asize
= l2hdr
->b_asize
;
1458 abi
->abi_l2arc_compress
= HDR_GET_COMPRESS(hdr
);
1459 abi
->abi_l2arc_hits
= l2hdr
->b_hits
;
1462 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
1463 abi
->abi_state_contents
= arc_buf_type(hdr
);
1464 abi
->abi_size
= hdr
->b_size
;
1468 * Move the supplied buffer to the indicated state. The hash lock
1469 * for the buffer must be held by the caller.
1472 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*hdr
,
1473 kmutex_t
*hash_lock
)
1475 arc_state_t
*old_state
;
1478 uint64_t from_delta
, to_delta
;
1479 arc_buf_contents_t buftype
= arc_buf_type(hdr
);
1482 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
1483 * in arc_read() when bringing a buffer out of the L2ARC. However, the
1484 * L1 hdr doesn't always exist when we change state to arc_anon before
1485 * destroying a header, in which case reallocating to add the L1 hdr is
1488 if (HDR_HAS_L1HDR(hdr
)) {
1489 old_state
= hdr
->b_l1hdr
.b_state
;
1490 refcnt
= refcount_count(&hdr
->b_l1hdr
.b_refcnt
);
1491 datacnt
= hdr
->b_l1hdr
.b_datacnt
;
1493 old_state
= arc_l2c_only
;
1498 ASSERT(MUTEX_HELD(hash_lock
));
1499 ASSERT3P(new_state
, !=, old_state
);
1500 ASSERT(refcnt
== 0 || datacnt
> 0);
1501 ASSERT(!GHOST_STATE(new_state
) || datacnt
== 0);
1502 ASSERT(old_state
!= arc_anon
|| datacnt
<= 1);
1504 from_delta
= to_delta
= datacnt
* hdr
->b_size
;
1507 * If this buffer is evictable, transfer it from the
1508 * old state list to the new state list.
1511 if (old_state
!= arc_anon
&& old_state
!= arc_l2c_only
) {
1512 uint64_t *size
= &old_state
->arcs_lsize
[buftype
];
1514 ASSERT(HDR_HAS_L1HDR(hdr
));
1515 multilist_remove(&old_state
->arcs_list
[buftype
], hdr
);
1518 * If prefetching out of the ghost cache,
1519 * we will have a non-zero datacnt.
1521 if (GHOST_STATE(old_state
) && datacnt
== 0) {
1522 /* ghost elements have a ghost size */
1523 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
);
1524 from_delta
= hdr
->b_size
;
1526 ASSERT3U(*size
, >=, from_delta
);
1527 atomic_add_64(size
, -from_delta
);
1529 if (new_state
!= arc_anon
&& new_state
!= arc_l2c_only
) {
1530 uint64_t *size
= &new_state
->arcs_lsize
[buftype
];
1533 * An L1 header always exists here, since if we're
1534 * moving to some L1-cached state (i.e. not l2c_only or
1535 * anonymous), we realloc the header to add an L1hdr
1538 ASSERT(HDR_HAS_L1HDR(hdr
));
1539 multilist_insert(&new_state
->arcs_list
[buftype
], hdr
);
1541 /* ghost elements have a ghost size */
1542 if (GHOST_STATE(new_state
)) {
1544 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
);
1545 to_delta
= hdr
->b_size
;
1547 atomic_add_64(size
, to_delta
);
1551 ASSERT(!BUF_EMPTY(hdr
));
1552 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(hdr
))
1553 buf_hash_remove(hdr
);
1555 /* adjust state sizes (ignore arc_l2c_only) */
1556 if (to_delta
&& new_state
!= arc_l2c_only
)
1557 atomic_add_64(&new_state
->arcs_size
, to_delta
);
1558 if (from_delta
&& old_state
!= arc_l2c_only
) {
1559 ASSERT3U(old_state
->arcs_size
, >=, from_delta
);
1560 atomic_add_64(&old_state
->arcs_size
, -from_delta
);
1562 if (HDR_HAS_L1HDR(hdr
))
1563 hdr
->b_l1hdr
.b_state
= new_state
;
1566 * L2 headers should never be on the L2 state list since they don't
1567 * have L1 headers allocated.
1569 ASSERT(multilist_is_empty(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]) &&
1570 multilist_is_empty(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]));
1574 arc_space_consume(uint64_t space
, arc_space_type_t type
)
1576 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1581 case ARC_SPACE_DATA
:
1582 ARCSTAT_INCR(arcstat_data_size
, space
);
1584 case ARC_SPACE_META
:
1585 ARCSTAT_INCR(arcstat_metadata_size
, space
);
1587 case ARC_SPACE_OTHER
:
1588 ARCSTAT_INCR(arcstat_other_size
, space
);
1590 case ARC_SPACE_HDRS
:
1591 ARCSTAT_INCR(arcstat_hdr_size
, space
);
1593 case ARC_SPACE_L2HDRS
:
1594 ARCSTAT_INCR(arcstat_l2_hdr_size
, space
);
1598 if (type
!= ARC_SPACE_DATA
)
1599 ARCSTAT_INCR(arcstat_meta_used
, space
);
1601 atomic_add_64(&arc_size
, space
);
1605 arc_space_return(uint64_t space
, arc_space_type_t type
)
1607 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1612 case ARC_SPACE_DATA
:
1613 ARCSTAT_INCR(arcstat_data_size
, -space
);
1615 case ARC_SPACE_META
:
1616 ARCSTAT_INCR(arcstat_metadata_size
, -space
);
1618 case ARC_SPACE_OTHER
:
1619 ARCSTAT_INCR(arcstat_other_size
, -space
);
1621 case ARC_SPACE_HDRS
:
1622 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
1624 case ARC_SPACE_L2HDRS
:
1625 ARCSTAT_INCR(arcstat_l2_hdr_size
, -space
);
1629 if (type
!= ARC_SPACE_DATA
) {
1630 ASSERT(arc_meta_used
>= space
);
1631 if (arc_meta_max
< arc_meta_used
)
1632 arc_meta_max
= arc_meta_used
;
1633 ARCSTAT_INCR(arcstat_meta_used
, -space
);
1636 ASSERT(arc_size
>= space
);
1637 atomic_add_64(&arc_size
, -space
);
1641 arc_buf_alloc(spa_t
*spa
, uint64_t size
, void *tag
, arc_buf_contents_t type
)
1646 VERIFY3U(size
, <=, spa_maxblocksize(spa
));
1647 hdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
1648 ASSERT(BUF_EMPTY(hdr
));
1649 ASSERT3P(hdr
->b_freeze_cksum
, ==, NULL
);
1651 hdr
->b_spa
= spa_load_guid(spa
);
1652 hdr
->b_l1hdr
.b_mru_hits
= 0;
1653 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
1654 hdr
->b_l1hdr
.b_mfu_hits
= 0;
1655 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
1656 hdr
->b_l1hdr
.b_l2_hits
= 0;
1658 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1661 buf
->b_efunc
= NULL
;
1662 buf
->b_private
= NULL
;
1665 hdr
->b_flags
= arc_bufc_to_flags(type
);
1666 hdr
->b_flags
|= ARC_FLAG_HAS_L1HDR
;
1668 hdr
->b_l1hdr
.b_buf
= buf
;
1669 hdr
->b_l1hdr
.b_state
= arc_anon
;
1670 hdr
->b_l1hdr
.b_arc_access
= 0;
1671 hdr
->b_l1hdr
.b_datacnt
= 1;
1672 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
1674 arc_get_data_buf(buf
);
1676 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
1677 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
1682 static char *arc_onloan_tag
= "onloan";
1685 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1686 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1687 * buffers must be returned to the arc before they can be used by the DMU or
1691 arc_loan_buf(spa_t
*spa
, uint64_t size
)
1695 buf
= arc_buf_alloc(spa
, size
, arc_onloan_tag
, ARC_BUFC_DATA
);
1697 atomic_add_64(&arc_loaned_bytes
, size
);
1702 * Return a loaned arc buffer to the arc.
1705 arc_return_buf(arc_buf_t
*buf
, void *tag
)
1707 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1709 ASSERT(buf
->b_data
!= NULL
);
1710 ASSERT(HDR_HAS_L1HDR(hdr
));
1711 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
1712 (void) refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
1714 atomic_add_64(&arc_loaned_bytes
, -hdr
->b_size
);
1717 /* Detach an arc_buf from a dbuf (tag) */
1719 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
1721 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1723 ASSERT(buf
->b_data
!= NULL
);
1724 ASSERT(HDR_HAS_L1HDR(hdr
));
1725 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
1726 (void) refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
);
1727 buf
->b_efunc
= NULL
;
1728 buf
->b_private
= NULL
;
1730 atomic_add_64(&arc_loaned_bytes
, hdr
->b_size
);
1734 arc_buf_clone(arc_buf_t
*from
)
1737 arc_buf_hdr_t
*hdr
= from
->b_hdr
;
1738 uint64_t size
= hdr
->b_size
;
1740 ASSERT(HDR_HAS_L1HDR(hdr
));
1741 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_anon
);
1743 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1746 buf
->b_efunc
= NULL
;
1747 buf
->b_private
= NULL
;
1748 buf
->b_next
= hdr
->b_l1hdr
.b_buf
;
1749 hdr
->b_l1hdr
.b_buf
= buf
;
1750 arc_get_data_buf(buf
);
1751 bcopy(from
->b_data
, buf
->b_data
, size
);
1754 * This buffer already exists in the arc so create a duplicate
1755 * copy for the caller. If the buffer is associated with user data
1756 * then track the size and number of duplicates. These stats will be
1757 * updated as duplicate buffers are created and destroyed.
1759 if (HDR_ISTYPE_DATA(hdr
)) {
1760 ARCSTAT_BUMP(arcstat_duplicate_buffers
);
1761 ARCSTAT_INCR(arcstat_duplicate_buffers_size
, size
);
1763 hdr
->b_l1hdr
.b_datacnt
+= 1;
1768 arc_buf_add_ref(arc_buf_t
*buf
, void* tag
)
1771 kmutex_t
*hash_lock
;
1774 * Check to see if this buffer is evicted. Callers
1775 * must verify b_data != NULL to know if the add_ref
1778 mutex_enter(&buf
->b_evict_lock
);
1779 if (buf
->b_data
== NULL
) {
1780 mutex_exit(&buf
->b_evict_lock
);
1783 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1784 mutex_enter(hash_lock
);
1786 ASSERT(HDR_HAS_L1HDR(hdr
));
1787 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1788 mutex_exit(&buf
->b_evict_lock
);
1790 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
1791 hdr
->b_l1hdr
.b_state
== arc_mfu
);
1793 add_reference(hdr
, hash_lock
, tag
);
1794 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
1795 arc_access(hdr
, hash_lock
);
1796 mutex_exit(hash_lock
);
1797 ARCSTAT_BUMP(arcstat_hits
);
1798 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
1799 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
1800 data
, metadata
, hits
);
1804 arc_buf_free_on_write(void *data
, size_t size
,
1805 void (*free_func
)(void *, size_t))
1807 l2arc_data_free_t
*df
;
1809 df
= kmem_alloc(sizeof (*df
), KM_SLEEP
);
1810 df
->l2df_data
= data
;
1811 df
->l2df_size
= size
;
1812 df
->l2df_func
= free_func
;
1813 mutex_enter(&l2arc_free_on_write_mtx
);
1814 list_insert_head(l2arc_free_on_write
, df
);
1815 mutex_exit(&l2arc_free_on_write_mtx
);
1819 * Free the arc data buffer. If it is an l2arc write in progress,
1820 * the buffer is placed on l2arc_free_on_write to be freed later.
1823 arc_buf_data_free(arc_buf_t
*buf
, void (*free_func
)(void *, size_t))
1825 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1827 if (HDR_L2_WRITING(hdr
)) {
1828 arc_buf_free_on_write(buf
->b_data
, hdr
->b_size
, free_func
);
1829 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
1831 free_func(buf
->b_data
, hdr
->b_size
);
1836 arc_buf_l2_cdata_free(arc_buf_hdr_t
*hdr
)
1838 ASSERT(HDR_HAS_L2HDR(hdr
));
1839 ASSERT(MUTEX_HELD(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
));
1842 * The b_tmp_cdata field is linked off of the b_l1hdr, so if
1843 * that doesn't exist, the header is in the arc_l2c_only state,
1844 * and there isn't anything to free (it's already been freed).
1846 if (!HDR_HAS_L1HDR(hdr
))
1850 * The header isn't being written to the l2arc device, thus it
1851 * shouldn't have a b_tmp_cdata to free.
1853 if (!HDR_L2_WRITING(hdr
)) {
1854 ASSERT3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
1859 * The header does not have compression enabled. This can be due
1860 * to the buffer not being compressible, or because we're
1861 * freeing the buffer before the second phase of
1862 * l2arc_write_buffer() has started (which does the compression
1863 * step). In either case, b_tmp_cdata does not point to a
1864 * separately compressed buffer, so there's nothing to free (it
1865 * points to the same buffer as the arc_buf_t's b_data field).
1867 if (HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
) {
1868 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
1873 * There's nothing to free since the buffer was all zero's and
1874 * compressed to a zero length buffer.
1876 if (HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_EMPTY
) {
1877 ASSERT3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
1881 ASSERT(L2ARC_IS_VALID_COMPRESS(HDR_GET_COMPRESS(hdr
)));
1883 arc_buf_free_on_write(hdr
->b_l1hdr
.b_tmp_cdata
,
1884 hdr
->b_size
, zio_data_buf_free
);
1886 ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write
);
1887 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
1891 * Free up buf->b_data and if 'remove' is set, then pull the
1892 * arc_buf_t off of the the arc_buf_hdr_t's list and free it.
1895 arc_buf_destroy(arc_buf_t
*buf
, boolean_t remove
)
1899 /* free up data associated with the buf */
1900 if (buf
->b_data
!= NULL
) {
1901 arc_state_t
*state
= buf
->b_hdr
->b_l1hdr
.b_state
;
1902 uint64_t size
= buf
->b_hdr
->b_size
;
1903 arc_buf_contents_t type
= arc_buf_type(buf
->b_hdr
);
1905 arc_cksum_verify(buf
);
1906 arc_buf_unwatch(buf
);
1908 if (type
== ARC_BUFC_METADATA
) {
1909 arc_buf_data_free(buf
, zio_buf_free
);
1910 arc_space_return(size
, ARC_SPACE_META
);
1912 ASSERT(type
== ARC_BUFC_DATA
);
1913 arc_buf_data_free(buf
, zio_data_buf_free
);
1914 arc_space_return(size
, ARC_SPACE_DATA
);
1917 /* protected by hash lock, if in the hash table */
1918 if (multilist_link_active(&buf
->b_hdr
->b_l1hdr
.b_arc_node
)) {
1919 uint64_t *cnt
= &state
->arcs_lsize
[type
];
1921 ASSERT(refcount_is_zero(
1922 &buf
->b_hdr
->b_l1hdr
.b_refcnt
));
1923 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
1925 ASSERT3U(*cnt
, >=, size
);
1926 atomic_add_64(cnt
, -size
);
1928 ASSERT3U(state
->arcs_size
, >=, size
);
1929 atomic_add_64(&state
->arcs_size
, -size
);
1933 * If we're destroying a duplicate buffer make sure
1934 * that the appropriate statistics are updated.
1936 if (buf
->b_hdr
->b_l1hdr
.b_datacnt
> 1 &&
1937 HDR_ISTYPE_DATA(buf
->b_hdr
)) {
1938 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers
);
1939 ARCSTAT_INCR(arcstat_duplicate_buffers_size
, -size
);
1941 ASSERT(buf
->b_hdr
->b_l1hdr
.b_datacnt
> 0);
1942 buf
->b_hdr
->b_l1hdr
.b_datacnt
-= 1;
1945 /* only remove the buf if requested */
1949 /* remove the buf from the hdr list */
1950 for (bufp
= &buf
->b_hdr
->b_l1hdr
.b_buf
; *bufp
!= buf
;
1951 bufp
= &(*bufp
)->b_next
)
1953 *bufp
= buf
->b_next
;
1956 ASSERT(buf
->b_efunc
== NULL
);
1958 /* clean up the buf */
1960 kmem_cache_free(buf_cache
, buf
);
1964 arc_hdr_l2hdr_destroy(arc_buf_hdr_t
*hdr
)
1966 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
1967 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
1969 ASSERT(MUTEX_HELD(&dev
->l2ad_mtx
));
1970 ASSERT(HDR_HAS_L2HDR(hdr
));
1972 list_remove(&dev
->l2ad_buflist
, hdr
);
1974 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1977 * We don't want to leak the b_tmp_cdata buffer that was
1978 * allocated in l2arc_write_buffers()
1980 arc_buf_l2_cdata_free(hdr
);
1983 * If the l2hdr's b_daddr is equal to L2ARC_ADDR_UNSET, then
1984 * this header is being processed by l2arc_write_buffers() (i.e.
1985 * it's in the first stage of l2arc_write_buffers()).
1986 * Re-affirming that truth here, just to serve as a reminder. If
1987 * b_daddr does not equal L2ARC_ADDR_UNSET, then the header may or
1988 * may not have its HDR_L2_WRITING flag set. (the write may have
1989 * completed, in which case HDR_L2_WRITING will be false and the
1990 * b_daddr field will point to the address of the buffer on disk).
1992 IMPLY(l2hdr
->b_daddr
== L2ARC_ADDR_UNSET
, HDR_L2_WRITING(hdr
));
1995 * If b_daddr is equal to L2ARC_ADDR_UNSET, we're racing with
1996 * l2arc_write_buffers(). Since we've just removed this header
1997 * from the l2arc buffer list, this header will never reach the
1998 * second stage of l2arc_write_buffers(), which increments the
1999 * accounting stats for this header. Thus, we must be careful
2000 * not to decrement them for this header either.
2002 if (l2hdr
->b_daddr
!= L2ARC_ADDR_UNSET
) {
2003 ARCSTAT_INCR(arcstat_l2_asize
, -l2hdr
->b_asize
);
2004 ARCSTAT_INCR(arcstat_l2_size
, -hdr
->b_size
);
2006 vdev_space_update(dev
->l2ad_vdev
,
2007 -l2hdr
->b_asize
, 0, 0);
2009 (void) refcount_remove_many(&dev
->l2ad_alloc
,
2010 l2hdr
->b_asize
, hdr
);
2013 hdr
->b_flags
&= ~ARC_FLAG_HAS_L2HDR
;
2017 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
2019 if (HDR_HAS_L1HDR(hdr
)) {
2020 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
||
2021 hdr
->b_l1hdr
.b_datacnt
> 0);
2022 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2023 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
2025 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
2026 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
2028 if (HDR_HAS_L2HDR(hdr
)) {
2029 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
2030 boolean_t buflist_held
= MUTEX_HELD(&dev
->l2ad_mtx
);
2033 mutex_enter(&dev
->l2ad_mtx
);
2036 * Even though we checked this conditional above, we
2037 * need to check this again now that we have the
2038 * l2ad_mtx. This is because we could be racing with
2039 * another thread calling l2arc_evict() which might have
2040 * destroyed this header's L2 portion as we were waiting
2041 * to acquire the l2ad_mtx. If that happens, we don't
2042 * want to re-destroy the header's L2 portion.
2044 if (HDR_HAS_L2HDR(hdr
))
2045 arc_hdr_l2hdr_destroy(hdr
);
2048 mutex_exit(&dev
->l2ad_mtx
);
2051 if (!BUF_EMPTY(hdr
))
2052 buf_discard_identity(hdr
);
2054 if (hdr
->b_freeze_cksum
!= NULL
) {
2055 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
2056 hdr
->b_freeze_cksum
= NULL
;
2059 if (HDR_HAS_L1HDR(hdr
)) {
2060 while (hdr
->b_l1hdr
.b_buf
) {
2061 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
2063 if (buf
->b_efunc
!= NULL
) {
2064 mutex_enter(&arc_user_evicts_lock
);
2065 mutex_enter(&buf
->b_evict_lock
);
2066 ASSERT(buf
->b_hdr
!= NULL
);
2067 arc_buf_destroy(hdr
->b_l1hdr
.b_buf
, FALSE
);
2068 hdr
->b_l1hdr
.b_buf
= buf
->b_next
;
2069 buf
->b_hdr
= &arc_eviction_hdr
;
2070 buf
->b_next
= arc_eviction_list
;
2071 arc_eviction_list
= buf
;
2072 mutex_exit(&buf
->b_evict_lock
);
2073 cv_signal(&arc_user_evicts_cv
);
2074 mutex_exit(&arc_user_evicts_lock
);
2076 arc_buf_destroy(hdr
->b_l1hdr
.b_buf
, TRUE
);
2081 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
2082 if (HDR_HAS_L1HDR(hdr
)) {
2083 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
2084 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
2085 kmem_cache_free(hdr_full_cache
, hdr
);
2087 kmem_cache_free(hdr_l2only_cache
, hdr
);
2092 arc_buf_free(arc_buf_t
*buf
, void *tag
)
2094 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2095 int hashed
= hdr
->b_l1hdr
.b_state
!= arc_anon
;
2097 ASSERT(buf
->b_efunc
== NULL
);
2098 ASSERT(buf
->b_data
!= NULL
);
2101 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
2103 mutex_enter(hash_lock
);
2105 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
2107 (void) remove_reference(hdr
, hash_lock
, tag
);
2108 if (hdr
->b_l1hdr
.b_datacnt
> 1) {
2109 arc_buf_destroy(buf
, TRUE
);
2111 ASSERT(buf
== hdr
->b_l1hdr
.b_buf
);
2112 ASSERT(buf
->b_efunc
== NULL
);
2113 hdr
->b_flags
|= ARC_FLAG_BUF_AVAILABLE
;
2115 mutex_exit(hash_lock
);
2116 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
2119 * We are in the middle of an async write. Don't destroy
2120 * this buffer unless the write completes before we finish
2121 * decrementing the reference count.
2123 mutex_enter(&arc_user_evicts_lock
);
2124 (void) remove_reference(hdr
, NULL
, tag
);
2125 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2126 destroy_hdr
= !HDR_IO_IN_PROGRESS(hdr
);
2127 mutex_exit(&arc_user_evicts_lock
);
2129 arc_hdr_destroy(hdr
);
2131 if (remove_reference(hdr
, NULL
, tag
) > 0)
2132 arc_buf_destroy(buf
, TRUE
);
2134 arc_hdr_destroy(hdr
);
2139 arc_buf_remove_ref(arc_buf_t
*buf
, void* tag
)
2141 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2142 kmutex_t
*hash_lock
= NULL
;
2143 boolean_t no_callback
= (buf
->b_efunc
== NULL
);
2145 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
2146 ASSERT(hdr
->b_l1hdr
.b_datacnt
== 1);
2147 arc_buf_free(buf
, tag
);
2148 return (no_callback
);
2151 hash_lock
= HDR_LOCK(hdr
);
2152 mutex_enter(hash_lock
);
2154 ASSERT(hdr
->b_l1hdr
.b_datacnt
> 0);
2155 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
2156 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_anon
);
2157 ASSERT(buf
->b_data
!= NULL
);
2159 (void) remove_reference(hdr
, hash_lock
, tag
);
2160 if (hdr
->b_l1hdr
.b_datacnt
> 1) {
2162 arc_buf_destroy(buf
, TRUE
);
2163 } else if (no_callback
) {
2164 ASSERT(hdr
->b_l1hdr
.b_buf
== buf
&& buf
->b_next
== NULL
);
2165 ASSERT(buf
->b_efunc
== NULL
);
2166 hdr
->b_flags
|= ARC_FLAG_BUF_AVAILABLE
;
2168 ASSERT(no_callback
|| hdr
->b_l1hdr
.b_datacnt
> 1 ||
2169 refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2170 mutex_exit(hash_lock
);
2171 return (no_callback
);
2175 arc_buf_size(arc_buf_t
*buf
)
2177 return (buf
->b_hdr
->b_size
);
2181 * Called from the DMU to determine if the current buffer should be
2182 * evicted. In order to ensure proper locking, the eviction must be initiated
2183 * from the DMU. Return true if the buffer is associated with user data and
2184 * duplicate buffers still exist.
2187 arc_buf_eviction_needed(arc_buf_t
*buf
)
2190 boolean_t evict_needed
= B_FALSE
;
2192 if (zfs_disable_dup_eviction
)
2195 mutex_enter(&buf
->b_evict_lock
);
2199 * We are in arc_do_user_evicts(); let that function
2200 * perform the eviction.
2202 ASSERT(buf
->b_data
== NULL
);
2203 mutex_exit(&buf
->b_evict_lock
);
2205 } else if (buf
->b_data
== NULL
) {
2207 * We have already been added to the arc eviction list;
2208 * recommend eviction.
2210 ASSERT3P(hdr
, ==, &arc_eviction_hdr
);
2211 mutex_exit(&buf
->b_evict_lock
);
2215 if (hdr
->b_l1hdr
.b_datacnt
> 1 && HDR_ISTYPE_DATA(hdr
))
2216 evict_needed
= B_TRUE
;
2218 mutex_exit(&buf
->b_evict_lock
);
2219 return (evict_needed
);
2223 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
2224 * state of the header is dependent on its state prior to entering this
2225 * function. The following transitions are possible:
2227 * - arc_mru -> arc_mru_ghost
2228 * - arc_mfu -> arc_mfu_ghost
2229 * - arc_mru_ghost -> arc_l2c_only
2230 * - arc_mru_ghost -> deleted
2231 * - arc_mfu_ghost -> arc_l2c_only
2232 * - arc_mfu_ghost -> deleted
2235 arc_evict_hdr(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
2237 arc_state_t
*evicted_state
, *state
;
2238 int64_t bytes_evicted
= 0;
2240 ASSERT(MUTEX_HELD(hash_lock
));
2241 ASSERT(HDR_HAS_L1HDR(hdr
));
2243 state
= hdr
->b_l1hdr
.b_state
;
2244 if (GHOST_STATE(state
)) {
2245 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
2246 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
);
2249 * l2arc_write_buffers() relies on a header's L1 portion
2250 * (i.e. its b_tmp_cdata field) during its write phase.
2251 * Thus, we cannot push a header onto the arc_l2c_only
2252 * state (removing its L1 piece) until the header is
2253 * done being written to the l2arc.
2255 if (HDR_HAS_L2HDR(hdr
) && HDR_L2_WRITING(hdr
)) {
2256 ARCSTAT_BUMP(arcstat_evict_l2_skip
);
2257 return (bytes_evicted
);
2260 ARCSTAT_BUMP(arcstat_deleted
);
2261 bytes_evicted
+= hdr
->b_size
;
2263 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, hdr
);
2265 if (HDR_HAS_L2HDR(hdr
)) {
2267 * This buffer is cached on the 2nd Level ARC;
2268 * don't destroy the header.
2270 arc_change_state(arc_l2c_only
, hdr
, hash_lock
);
2272 * dropping from L1+L2 cached to L2-only,
2273 * realloc to remove the L1 header.
2275 hdr
= arc_hdr_realloc(hdr
, hdr_full_cache
,
2278 arc_change_state(arc_anon
, hdr
, hash_lock
);
2279 arc_hdr_destroy(hdr
);
2281 return (bytes_evicted
);
2284 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
2285 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
2287 /* prefetch buffers have a minimum lifespan */
2288 if (HDR_IO_IN_PROGRESS(hdr
) ||
2289 ((hdr
->b_flags
& (ARC_FLAG_PREFETCH
| ARC_FLAG_INDIRECT
)) &&
2290 ddi_get_lbolt() - hdr
->b_l1hdr
.b_arc_access
<
2291 arc_min_prefetch_lifespan
)) {
2292 ARCSTAT_BUMP(arcstat_evict_skip
);
2293 return (bytes_evicted
);
2296 ASSERT0(refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
2297 ASSERT3U(hdr
->b_l1hdr
.b_datacnt
, >, 0);
2298 while (hdr
->b_l1hdr
.b_buf
) {
2299 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
2300 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
2301 ARCSTAT_BUMP(arcstat_mutex_miss
);
2304 if (buf
->b_data
!= NULL
)
2305 bytes_evicted
+= hdr
->b_size
;
2306 if (buf
->b_efunc
!= NULL
) {
2307 mutex_enter(&arc_user_evicts_lock
);
2308 arc_buf_destroy(buf
, FALSE
);
2309 hdr
->b_l1hdr
.b_buf
= buf
->b_next
;
2310 buf
->b_hdr
= &arc_eviction_hdr
;
2311 buf
->b_next
= arc_eviction_list
;
2312 arc_eviction_list
= buf
;
2313 cv_signal(&arc_user_evicts_cv
);
2314 mutex_exit(&arc_user_evicts_lock
);
2315 mutex_exit(&buf
->b_evict_lock
);
2317 mutex_exit(&buf
->b_evict_lock
);
2318 arc_buf_destroy(buf
, TRUE
);
2322 if (HDR_HAS_L2HDR(hdr
)) {
2323 ARCSTAT_INCR(arcstat_evict_l2_cached
, hdr
->b_size
);
2325 if (l2arc_write_eligible(hdr
->b_spa
, hdr
))
2326 ARCSTAT_INCR(arcstat_evict_l2_eligible
, hdr
->b_size
);
2328 ARCSTAT_INCR(arcstat_evict_l2_ineligible
, hdr
->b_size
);
2331 if (hdr
->b_l1hdr
.b_datacnt
== 0) {
2332 arc_change_state(evicted_state
, hdr
, hash_lock
);
2333 ASSERT(HDR_IN_HASH_TABLE(hdr
));
2334 hdr
->b_flags
|= ARC_FLAG_IN_HASH_TABLE
;
2335 hdr
->b_flags
&= ~ARC_FLAG_BUF_AVAILABLE
;
2336 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, hdr
);
2339 return (bytes_evicted
);
2343 arc_evict_state_impl(multilist_t
*ml
, int idx
, arc_buf_hdr_t
*marker
,
2344 uint64_t spa
, int64_t bytes
)
2346 multilist_sublist_t
*mls
;
2347 uint64_t bytes_evicted
= 0;
2349 kmutex_t
*hash_lock
;
2350 int evict_count
= 0;
2352 ASSERT3P(marker
, !=, NULL
);
2353 ASSERTV(if (bytes
< 0) ASSERT(bytes
== ARC_EVICT_ALL
));
2355 mls
= multilist_sublist_lock(ml
, idx
);
2357 for (hdr
= multilist_sublist_prev(mls
, marker
); hdr
!= NULL
;
2358 hdr
= multilist_sublist_prev(mls
, marker
)) {
2359 if ((bytes
!= ARC_EVICT_ALL
&& bytes_evicted
>= bytes
) ||
2360 (evict_count
>= zfs_arc_evict_batch_limit
))
2364 * To keep our iteration location, move the marker
2365 * forward. Since we're not holding hdr's hash lock, we
2366 * must be very careful and not remove 'hdr' from the
2367 * sublist. Otherwise, other consumers might mistake the
2368 * 'hdr' as not being on a sublist when they call the
2369 * multilist_link_active() function (they all rely on
2370 * the hash lock protecting concurrent insertions and
2371 * removals). multilist_sublist_move_forward() was
2372 * specifically implemented to ensure this is the case
2373 * (only 'marker' will be removed and re-inserted).
2375 multilist_sublist_move_forward(mls
, marker
);
2378 * The only case where the b_spa field should ever be
2379 * zero, is the marker headers inserted by
2380 * arc_evict_state(). It's possible for multiple threads
2381 * to be calling arc_evict_state() concurrently (e.g.
2382 * dsl_pool_close() and zio_inject_fault()), so we must
2383 * skip any markers we see from these other threads.
2385 if (hdr
->b_spa
== 0)
2388 /* we're only interested in evicting buffers of a certain spa */
2389 if (spa
!= 0 && hdr
->b_spa
!= spa
) {
2390 ARCSTAT_BUMP(arcstat_evict_skip
);
2394 hash_lock
= HDR_LOCK(hdr
);
2397 * We aren't calling this function from any code path
2398 * that would already be holding a hash lock, so we're
2399 * asserting on this assumption to be defensive in case
2400 * this ever changes. Without this check, it would be
2401 * possible to incorrectly increment arcstat_mutex_miss
2402 * below (e.g. if the code changed such that we called
2403 * this function with a hash lock held).
2405 ASSERT(!MUTEX_HELD(hash_lock
));
2407 if (mutex_tryenter(hash_lock
)) {
2408 uint64_t evicted
= arc_evict_hdr(hdr
, hash_lock
);
2409 mutex_exit(hash_lock
);
2411 bytes_evicted
+= evicted
;
2414 * If evicted is zero, arc_evict_hdr() must have
2415 * decided to skip this header, don't increment
2416 * evict_count in this case.
2422 * If arc_size isn't overflowing, signal any
2423 * threads that might happen to be waiting.
2425 * For each header evicted, we wake up a single
2426 * thread. If we used cv_broadcast, we could
2427 * wake up "too many" threads causing arc_size
2428 * to significantly overflow arc_c; since
2429 * arc_get_data_buf() doesn't check for overflow
2430 * when it's woken up (it doesn't because it's
2431 * possible for the ARC to be overflowing while
2432 * full of un-evictable buffers, and the
2433 * function should proceed in this case).
2435 * If threads are left sleeping, due to not
2436 * using cv_broadcast, they will be woken up
2437 * just before arc_reclaim_thread() sleeps.
2439 mutex_enter(&arc_reclaim_lock
);
2440 if (!arc_is_overflowing())
2441 cv_signal(&arc_reclaim_waiters_cv
);
2442 mutex_exit(&arc_reclaim_lock
);
2444 ARCSTAT_BUMP(arcstat_mutex_miss
);
2448 multilist_sublist_unlock(mls
);
2450 return (bytes_evicted
);
2454 * Evict buffers from the given arc state, until we've removed the
2455 * specified number of bytes. Move the removed buffers to the
2456 * appropriate evict state.
2458 * This function makes a "best effort". It skips over any buffers
2459 * it can't get a hash_lock on, and so, may not catch all candidates.
2460 * It may also return without evicting as much space as requested.
2462 * If bytes is specified using the special value ARC_EVICT_ALL, this
2463 * will evict all available (i.e. unlocked and evictable) buffers from
2464 * the given arc state; which is used by arc_flush().
2467 arc_evict_state(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
2468 arc_buf_contents_t type
)
2470 uint64_t total_evicted
= 0;
2471 multilist_t
*ml
= &state
->arcs_list
[type
];
2473 arc_buf_hdr_t
**markers
;
2476 ASSERTV(if (bytes
< 0) ASSERT(bytes
== ARC_EVICT_ALL
));
2478 num_sublists
= multilist_get_num_sublists(ml
);
2481 * If we've tried to evict from each sublist, made some
2482 * progress, but still have not hit the target number of bytes
2483 * to evict, we want to keep trying. The markers allow us to
2484 * pick up where we left off for each individual sublist, rather
2485 * than starting from the tail each time.
2487 markers
= kmem_zalloc(sizeof (*markers
) * num_sublists
, KM_SLEEP
);
2488 for (i
= 0; i
< num_sublists
; i
++) {
2489 multilist_sublist_t
*mls
;
2491 markers
[i
] = kmem_cache_alloc(hdr_full_cache
, KM_SLEEP
);
2494 * A b_spa of 0 is used to indicate that this header is
2495 * a marker. This fact is used in arc_adjust_type() and
2496 * arc_evict_state_impl().
2498 markers
[i
]->b_spa
= 0;
2500 mls
= multilist_sublist_lock(ml
, i
);
2501 multilist_sublist_insert_tail(mls
, markers
[i
]);
2502 multilist_sublist_unlock(mls
);
2506 * While we haven't hit our target number of bytes to evict, or
2507 * we're evicting all available buffers.
2509 while (total_evicted
< bytes
|| bytes
== ARC_EVICT_ALL
) {
2511 * Start eviction using a randomly selected sublist,
2512 * this is to try and evenly balance eviction across all
2513 * sublists. Always starting at the same sublist
2514 * (e.g. index 0) would cause evictions to favor certain
2515 * sublists over others.
2517 int sublist_idx
= multilist_get_random_index(ml
);
2518 uint64_t scan_evicted
= 0;
2520 for (i
= 0; i
< num_sublists
; i
++) {
2521 uint64_t bytes_remaining
;
2522 uint64_t bytes_evicted
;
2524 if (bytes
== ARC_EVICT_ALL
)
2525 bytes_remaining
= ARC_EVICT_ALL
;
2526 else if (total_evicted
< bytes
)
2527 bytes_remaining
= bytes
- total_evicted
;
2531 bytes_evicted
= arc_evict_state_impl(ml
, sublist_idx
,
2532 markers
[sublist_idx
], spa
, bytes_remaining
);
2534 scan_evicted
+= bytes_evicted
;
2535 total_evicted
+= bytes_evicted
;
2537 /* we've reached the end, wrap to the beginning */
2538 if (++sublist_idx
>= num_sublists
)
2543 * If we didn't evict anything during this scan, we have
2544 * no reason to believe we'll evict more during another
2545 * scan, so break the loop.
2547 if (scan_evicted
== 0) {
2548 /* This isn't possible, let's make that obvious */
2549 ASSERT3S(bytes
, !=, 0);
2552 * When bytes is ARC_EVICT_ALL, the only way to
2553 * break the loop is when scan_evicted is zero.
2554 * In that case, we actually have evicted enough,
2555 * so we don't want to increment the kstat.
2557 if (bytes
!= ARC_EVICT_ALL
) {
2558 ASSERT3S(total_evicted
, <, bytes
);
2559 ARCSTAT_BUMP(arcstat_evict_not_enough
);
2566 for (i
= 0; i
< num_sublists
; i
++) {
2567 multilist_sublist_t
*mls
= multilist_sublist_lock(ml
, i
);
2568 multilist_sublist_remove(mls
, markers
[i
]);
2569 multilist_sublist_unlock(mls
);
2571 kmem_cache_free(hdr_full_cache
, markers
[i
]);
2573 kmem_free(markers
, sizeof (*markers
) * num_sublists
);
2575 return (total_evicted
);
2579 * Flush all "evictable" data of the given type from the arc state
2580 * specified. This will not evict any "active" buffers (i.e. referenced).
2582 * When 'retry' is set to FALSE, the function will make a single pass
2583 * over the state and evict any buffers that it can. Since it doesn't
2584 * continually retry the eviction, it might end up leaving some buffers
2585 * in the ARC due to lock misses.
2587 * When 'retry' is set to TRUE, the function will continually retry the
2588 * eviction until *all* evictable buffers have been removed from the
2589 * state. As a result, if concurrent insertions into the state are
2590 * allowed (e.g. if the ARC isn't shutting down), this function might
2591 * wind up in an infinite loop, continually trying to evict buffers.
2594 arc_flush_state(arc_state_t
*state
, uint64_t spa
, arc_buf_contents_t type
,
2597 uint64_t evicted
= 0;
2599 while (state
->arcs_lsize
[type
] != 0) {
2600 evicted
+= arc_evict_state(state
, spa
, ARC_EVICT_ALL
, type
);
2610 * Helper function for arc_prune() it is responsible for safely handling
2611 * the execution of a registered arc_prune_func_t.
2614 arc_prune_task(void *ptr
)
2616 arc_prune_t
*ap
= (arc_prune_t
*)ptr
;
2617 arc_prune_func_t
*func
= ap
->p_pfunc
;
2620 func(ap
->p_adjust
, ap
->p_private
);
2622 /* Callback unregistered concurrently with execution */
2623 if (refcount_remove(&ap
->p_refcnt
, func
) == 0) {
2624 ASSERT(!list_link_active(&ap
->p_node
));
2625 refcount_destroy(&ap
->p_refcnt
);
2626 kmem_free(ap
, sizeof (*ap
));
2631 * Notify registered consumers they must drop holds on a portion of the ARC
2632 * buffered they reference. This provides a mechanism to ensure the ARC can
2633 * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This
2634 * is analogous to dnlc_reduce_cache() but more generic.
2636 * This operation is performed asyncronously so it may be safely called
2637 * in the context of the arc_reclaim_thread(). A reference is taken here
2638 * for each registered arc_prune_t and the arc_prune_task() is responsible
2639 * for releasing it once the registered arc_prune_func_t has completed.
2642 arc_prune_async(int64_t adjust
)
2646 mutex_enter(&arc_prune_mtx
);
2647 for (ap
= list_head(&arc_prune_list
); ap
!= NULL
;
2648 ap
= list_next(&arc_prune_list
, ap
)) {
2650 if (refcount_count(&ap
->p_refcnt
) >= 2)
2653 refcount_add(&ap
->p_refcnt
, ap
->p_pfunc
);
2654 ap
->p_adjust
= adjust
;
2655 taskq_dispatch(arc_prune_taskq
, arc_prune_task
, ap
, TQ_SLEEP
);
2656 ARCSTAT_BUMP(arcstat_prune
);
2658 mutex_exit(&arc_prune_mtx
);
2662 arc_prune(int64_t adjust
)
2664 arc_prune_async(adjust
);
2665 taskq_wait_outstanding(arc_prune_taskq
, 0);
2669 * Evict the specified number of bytes from the state specified,
2670 * restricting eviction to the spa and type given. This function
2671 * prevents us from trying to evict more from a state's list than
2672 * is "evictable", and to skip evicting altogether when passed a
2673 * negative value for "bytes". In contrast, arc_evict_state() will
2674 * evict everything it can, when passed a negative value for "bytes".
2677 arc_adjust_impl(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
2678 arc_buf_contents_t type
)
2682 if (bytes
> 0 && state
->arcs_lsize
[type
] > 0) {
2683 delta
= MIN(state
->arcs_lsize
[type
], bytes
);
2684 return (arc_evict_state(state
, spa
, delta
, type
));
2691 * The goal of this function is to evict enough meta data buffers from the
2692 * ARC in order to enforce the arc_meta_limit. Achieving this is slightly
2693 * more complicated than it appears because it is common for data buffers
2694 * to have holds on meta data buffers. In addition, dnode meta data buffers
2695 * will be held by the dnodes in the block preventing them from being freed.
2696 * This means we can't simply traverse the ARC and expect to always find
2697 * enough unheld meta data buffer to release.
2699 * Therefore, this function has been updated to make alternating passes
2700 * over the ARC releasing data buffers and then newly unheld meta data
2701 * buffers. This ensures forward progress is maintained and arc_meta_used
2702 * will decrease. Normally this is sufficient, but if required the ARC
2703 * will call the registered prune callbacks causing dentry and inodes to
2704 * be dropped from the VFS cache. This will make dnode meta data buffers
2705 * available for reclaim.
2708 arc_adjust_meta_balanced(void)
2710 int64_t adjustmnt
, delta
, prune
= 0;
2711 uint64_t total_evicted
= 0;
2712 arc_buf_contents_t type
= ARC_BUFC_DATA
;
2713 int restarts
= MAX(zfs_arc_meta_adjust_restarts
, 0);
2717 * This slightly differs than the way we evict from the mru in
2718 * arc_adjust because we don't have a "target" value (i.e. no
2719 * "meta" arc_p). As a result, I think we can completely
2720 * cannibalize the metadata in the MRU before we evict the
2721 * metadata from the MFU. I think we probably need to implement a
2722 * "metadata arc_p" value to do this properly.
2724 adjustmnt
= arc_meta_used
- arc_meta_limit
;
2726 if (adjustmnt
> 0 && arc_mru
->arcs_lsize
[type
] > 0) {
2727 delta
= MIN(arc_mru
->arcs_lsize
[type
], adjustmnt
);
2728 total_evicted
+= arc_adjust_impl(arc_mru
, 0, delta
, type
);
2733 * We can't afford to recalculate adjustmnt here. If we do,
2734 * new metadata buffers can sneak into the MRU or ANON lists,
2735 * thus penalize the MFU metadata. Although the fudge factor is
2736 * small, it has been empirically shown to be significant for
2737 * certain workloads (e.g. creating many empty directories). As
2738 * such, we use the original calculation for adjustmnt, and
2739 * simply decrement the amount of data evicted from the MRU.
2742 if (adjustmnt
> 0 && arc_mfu
->arcs_lsize
[type
] > 0) {
2743 delta
= MIN(arc_mfu
->arcs_lsize
[type
], adjustmnt
);
2744 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, delta
, type
);
2747 adjustmnt
= arc_meta_used
- arc_meta_limit
;
2749 if (adjustmnt
> 0 && arc_mru_ghost
->arcs_lsize
[type
] > 0) {
2750 delta
= MIN(adjustmnt
,
2751 arc_mru_ghost
->arcs_lsize
[type
]);
2752 total_evicted
+= arc_adjust_impl(arc_mru_ghost
, 0, delta
, type
);
2756 if (adjustmnt
> 0 && arc_mfu_ghost
->arcs_lsize
[type
] > 0) {
2757 delta
= MIN(adjustmnt
,
2758 arc_mfu_ghost
->arcs_lsize
[type
]);
2759 total_evicted
+= arc_adjust_impl(arc_mfu_ghost
, 0, delta
, type
);
2763 * If after attempting to make the requested adjustment to the ARC
2764 * the meta limit is still being exceeded then request that the
2765 * higher layers drop some cached objects which have holds on ARC
2766 * meta buffers. Requests to the upper layers will be made with
2767 * increasingly large scan sizes until the ARC is below the limit.
2769 if (arc_meta_used
> arc_meta_limit
) {
2770 if (type
== ARC_BUFC_DATA
) {
2771 type
= ARC_BUFC_METADATA
;
2773 type
= ARC_BUFC_DATA
;
2775 if (zfs_arc_meta_prune
) {
2776 prune
+= zfs_arc_meta_prune
;
2777 arc_prune_async(prune
);
2786 return (total_evicted
);
2790 * Evict metadata buffers from the cache, such that arc_meta_used is
2791 * capped by the arc_meta_limit tunable.
2794 arc_adjust_meta_only(void)
2796 uint64_t total_evicted
= 0;
2800 * If we're over the meta limit, we want to evict enough
2801 * metadata to get back under the meta limit. We don't want to
2802 * evict so much that we drop the MRU below arc_p, though. If
2803 * we're over the meta limit more than we're over arc_p, we
2804 * evict some from the MRU here, and some from the MFU below.
2806 target
= MIN((int64_t)(arc_meta_used
- arc_meta_limit
),
2807 (int64_t)(arc_anon
->arcs_size
+ arc_mru
->arcs_size
- arc_p
));
2809 total_evicted
+= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
2812 * Similar to the above, we want to evict enough bytes to get us
2813 * below the meta limit, but not so much as to drop us below the
2814 * space alloted to the MFU (which is defined as arc_c - arc_p).
2816 target
= MIN((int64_t)(arc_meta_used
- arc_meta_limit
),
2817 (int64_t)(arc_mfu
->arcs_size
- (arc_c
- arc_p
)));
2819 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
2821 return (total_evicted
);
2825 arc_adjust_meta(void)
2827 if (zfs_arc_meta_strategy
== ARC_STRATEGY_META_ONLY
)
2828 return (arc_adjust_meta_only());
2830 return (arc_adjust_meta_balanced());
2834 * Return the type of the oldest buffer in the given arc state
2836 * This function will select a random sublist of type ARC_BUFC_DATA and
2837 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist
2838 * is compared, and the type which contains the "older" buffer will be
2841 static arc_buf_contents_t
2842 arc_adjust_type(arc_state_t
*state
)
2844 multilist_t
*data_ml
= &state
->arcs_list
[ARC_BUFC_DATA
];
2845 multilist_t
*meta_ml
= &state
->arcs_list
[ARC_BUFC_METADATA
];
2846 int data_idx
= multilist_get_random_index(data_ml
);
2847 int meta_idx
= multilist_get_random_index(meta_ml
);
2848 multilist_sublist_t
*data_mls
;
2849 multilist_sublist_t
*meta_mls
;
2850 arc_buf_contents_t type
;
2851 arc_buf_hdr_t
*data_hdr
;
2852 arc_buf_hdr_t
*meta_hdr
;
2855 * We keep the sublist lock until we're finished, to prevent
2856 * the headers from being destroyed via arc_evict_state().
2858 data_mls
= multilist_sublist_lock(data_ml
, data_idx
);
2859 meta_mls
= multilist_sublist_lock(meta_ml
, meta_idx
);
2862 * These two loops are to ensure we skip any markers that
2863 * might be at the tail of the lists due to arc_evict_state().
2866 for (data_hdr
= multilist_sublist_tail(data_mls
); data_hdr
!= NULL
;
2867 data_hdr
= multilist_sublist_prev(data_mls
, data_hdr
)) {
2868 if (data_hdr
->b_spa
!= 0)
2872 for (meta_hdr
= multilist_sublist_tail(meta_mls
); meta_hdr
!= NULL
;
2873 meta_hdr
= multilist_sublist_prev(meta_mls
, meta_hdr
)) {
2874 if (meta_hdr
->b_spa
!= 0)
2878 if (data_hdr
== NULL
&& meta_hdr
== NULL
) {
2879 type
= ARC_BUFC_DATA
;
2880 } else if (data_hdr
== NULL
) {
2881 ASSERT3P(meta_hdr
, !=, NULL
);
2882 type
= ARC_BUFC_METADATA
;
2883 } else if (meta_hdr
== NULL
) {
2884 ASSERT3P(data_hdr
, !=, NULL
);
2885 type
= ARC_BUFC_DATA
;
2887 ASSERT3P(data_hdr
, !=, NULL
);
2888 ASSERT3P(meta_hdr
, !=, NULL
);
2890 /* The headers can't be on the sublist without an L1 header */
2891 ASSERT(HDR_HAS_L1HDR(data_hdr
));
2892 ASSERT(HDR_HAS_L1HDR(meta_hdr
));
2894 if (data_hdr
->b_l1hdr
.b_arc_access
<
2895 meta_hdr
->b_l1hdr
.b_arc_access
) {
2896 type
= ARC_BUFC_DATA
;
2898 type
= ARC_BUFC_METADATA
;
2902 multilist_sublist_unlock(meta_mls
);
2903 multilist_sublist_unlock(data_mls
);
2909 * Evict buffers from the cache, such that arc_size is capped by arc_c.
2914 uint64_t total_evicted
= 0;
2919 * If we're over arc_meta_limit, we want to correct that before
2920 * potentially evicting data buffers below.
2922 total_evicted
+= arc_adjust_meta();
2927 * If we're over the target cache size, we want to evict enough
2928 * from the list to get back to our target size. We don't want
2929 * to evict too much from the MRU, such that it drops below
2930 * arc_p. So, if we're over our target cache size more than
2931 * the MRU is over arc_p, we'll evict enough to get back to
2932 * arc_p here, and then evict more from the MFU below.
2934 target
= MIN((int64_t)(arc_size
- arc_c
),
2935 (int64_t)(arc_anon
->arcs_size
+ arc_mru
->arcs_size
+ arc_meta_used
-
2939 * If we're below arc_meta_min, always prefer to evict data.
2940 * Otherwise, try to satisfy the requested number of bytes to
2941 * evict from the type which contains older buffers; in an
2942 * effort to keep newer buffers in the cache regardless of their
2943 * type. If we cannot satisfy the number of bytes from this
2944 * type, spill over into the next type.
2946 if (arc_adjust_type(arc_mru
) == ARC_BUFC_METADATA
&&
2947 arc_meta_used
> arc_meta_min
) {
2948 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
2949 total_evicted
+= bytes
;
2952 * If we couldn't evict our target number of bytes from
2953 * metadata, we try to get the rest from data.
2958 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
2960 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
2961 total_evicted
+= bytes
;
2964 * If we couldn't evict our target number of bytes from
2965 * data, we try to get the rest from metadata.
2970 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
2976 * Now that we've tried to evict enough from the MRU to get its
2977 * size back to arc_p, if we're still above the target cache
2978 * size, we evict the rest from the MFU.
2980 target
= arc_size
- arc_c
;
2982 if (arc_adjust_type(arc_mfu
) == ARC_BUFC_METADATA
&&
2983 arc_meta_used
> arc_meta_min
) {
2984 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
2985 total_evicted
+= bytes
;
2988 * If we couldn't evict our target number of bytes from
2989 * metadata, we try to get the rest from data.
2994 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
2996 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
2997 total_evicted
+= bytes
;
3000 * If we couldn't evict our target number of bytes from
3001 * data, we try to get the rest from data.
3006 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
3010 * Adjust ghost lists
3012 * In addition to the above, the ARC also defines target values
3013 * for the ghost lists. The sum of the mru list and mru ghost
3014 * list should never exceed the target size of the cache, and
3015 * the sum of the mru list, mfu list, mru ghost list, and mfu
3016 * ghost list should never exceed twice the target size of the
3017 * cache. The following logic enforces these limits on the ghost
3018 * caches, and evicts from them as needed.
3020 target
= arc_mru
->arcs_size
+ arc_mru_ghost
->arcs_size
- arc_c
;
3022 bytes
= arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_DATA
);
3023 total_evicted
+= bytes
;
3028 arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_METADATA
);
3031 * We assume the sum of the mru list and mfu list is less than
3032 * or equal to arc_c (we enforced this above), which means we
3033 * can use the simpler of the two equations below:
3035 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c
3036 * mru ghost + mfu ghost <= arc_c
3038 target
= arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
- arc_c
;
3040 bytes
= arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_DATA
);
3041 total_evicted
+= bytes
;
3046 arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_METADATA
);
3048 return (total_evicted
);
3052 arc_do_user_evicts(void)
3054 mutex_enter(&arc_user_evicts_lock
);
3055 while (arc_eviction_list
!= NULL
) {
3056 arc_buf_t
*buf
= arc_eviction_list
;
3057 arc_eviction_list
= buf
->b_next
;
3058 mutex_enter(&buf
->b_evict_lock
);
3060 mutex_exit(&buf
->b_evict_lock
);
3061 mutex_exit(&arc_user_evicts_lock
);
3063 if (buf
->b_efunc
!= NULL
)
3064 VERIFY0(buf
->b_efunc(buf
->b_private
));
3066 buf
->b_efunc
= NULL
;
3067 buf
->b_private
= NULL
;
3068 kmem_cache_free(buf_cache
, buf
);
3069 mutex_enter(&arc_user_evicts_lock
);
3071 mutex_exit(&arc_user_evicts_lock
);
3075 arc_flush(spa_t
*spa
, boolean_t retry
)
3080 * If retry is TRUE, a spa must not be specified since we have
3081 * no good way to determine if all of a spa's buffers have been
3082 * evicted from an arc state.
3084 ASSERT(!retry
|| spa
== 0);
3087 guid
= spa_load_guid(spa
);
3089 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_DATA
, retry
);
3090 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_METADATA
, retry
);
3092 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_DATA
, retry
);
3093 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_METADATA
, retry
);
3095 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_DATA
, retry
);
3096 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
3098 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_DATA
, retry
);
3099 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
3101 arc_do_user_evicts();
3102 ASSERT(spa
|| arc_eviction_list
== NULL
);
3106 arc_shrink(int64_t to_free
)
3108 if (arc_c
> arc_c_min
) {
3110 if (arc_c
> arc_c_min
+ to_free
)
3111 atomic_add_64(&arc_c
, -to_free
);
3115 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
3116 if (arc_c
> arc_size
)
3117 arc_c
= MAX(arc_size
, arc_c_min
);
3119 arc_p
= (arc_c
>> 1);
3120 ASSERT(arc_c
>= arc_c_min
);
3121 ASSERT((int64_t)arc_p
>= 0);
3124 if (arc_size
> arc_c
)
3125 (void) arc_adjust();
3128 typedef enum free_memory_reason_t
{
3133 FMR_PAGES_PP_MAXIMUM
,
3136 } free_memory_reason_t
;
3138 int64_t last_free_memory
;
3139 free_memory_reason_t last_free_reason
;
3144 * expiration time for arc_no_grow set by direct memory reclaim.
3146 static clock_t arc_grow_time
= 0;
3149 * Additional reserve of pages for pp_reserve.
3151 int64_t arc_pages_pp_reserve
= 64;
3154 * Additional reserve of pages for swapfs.
3156 int64_t arc_swapfs_reserve
= 64;
3158 #endif /* _KERNEL */
3161 * Return the amount of memory that can be consumed before reclaim will be
3162 * needed. Positive if there is sufficient free memory, negative indicates
3163 * the amount of memory that needs to be freed up.
3166 arc_available_memory(void)
3168 int64_t lowest
= INT64_MAX
;
3169 free_memory_reason_t r
= FMR_UNKNOWN
;
3174 * Under Linux we are not allowed to directly interrogate the global
3175 * memory state. Instead rely on observing that direct reclaim has
3176 * recently occurred therefore the system must be low on memory. The
3177 * exact values returned are not critical but should be small.
3179 if (ddi_time_after_eq(ddi_get_lbolt(), arc_grow_time
))
3182 lowest
= -PAGE_SIZE
;
3187 * Platforms like illumos have greater visibility in to the memory
3188 * subsystem and can return a more detailed analysis of memory.
3191 n
= PAGESIZE
* (-needfree
);
3199 * check that we're out of range of the pageout scanner. It starts to
3200 * schedule paging if freemem is less than lotsfree and needfree.
3201 * lotsfree is the high-water mark for pageout, and needfree is the
3202 * number of needed free pages. We add extra pages here to make sure
3203 * the scanner doesn't start up while we're freeing memory.
3205 n
= PAGESIZE
* (freemem
- lotsfree
- needfree
- desfree
);
3212 * check to make sure that swapfs has enough space so that anon
3213 * reservations can still succeed. anon_resvmem() checks that the
3214 * availrmem is greater than swapfs_minfree, and the number of reserved
3215 * swap pages. We also add a bit of extra here just to prevent
3216 * circumstances from getting really dire.
3218 n
= PAGESIZE
* (availrmem
- swapfs_minfree
- swapfs_reserve
-
3219 desfree
- arc_swapfs_reserve
);
3222 r
= FMR_SWAPFS_MINFREE
;
3227 * Check that we have enough availrmem that memory locking (e.g., via
3228 * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum
3229 * stores the number of pages that cannot be locked; when availrmem
3230 * drops below pages_pp_maximum, page locking mechanisms such as
3231 * page_pp_lock() will fail.)
3233 n
= PAGESIZE
* (availrmem
- pages_pp_maximum
-
3234 arc_pages_pp_reserve
);
3237 r
= FMR_PAGES_PP_MAXIMUM
;
3242 * If we're on an i386 platform, it's possible that we'll exhaust the
3243 * kernel heap space before we ever run out of available physical
3244 * memory. Most checks of the size of the heap_area compare against
3245 * tune.t_minarmem, which is the minimum available real memory that we
3246 * can have in the system. However, this is generally fixed at 25 pages
3247 * which is so low that it's useless. In this comparison, we seek to
3248 * calculate the total heap-size, and reclaim if more than 3/4ths of the
3249 * heap is allocated. (Or, in the calculation, if less than 1/4th is
3252 n
= vmem_size(heap_arena
, VMEM_FREE
) -
3253 (vmem_size(heap_arena
, VMEM_FREE
| VMEM_ALLOC
) >> 2);
3261 * If zio data pages are being allocated out of a separate heap segment,
3262 * then enforce that the size of available vmem for this arena remains
3263 * above about 1/16th free.
3265 * Note: The 1/16th arena free requirement was put in place
3266 * to aggressively evict memory from the arc in order to avoid
3267 * memory fragmentation issues.
3269 if (zio_arena
!= NULL
) {
3270 n
= vmem_size(zio_arena
, VMEM_FREE
) -
3271 (vmem_size(zio_arena
, VMEM_ALLOC
) >> 4);
3277 #endif /* __linux__ */
3279 /* Every 100 calls, free a small amount */
3280 if (spa_get_random(100) == 0)
3284 last_free_memory
= lowest
;
3285 last_free_reason
= r
;
3291 * Determine if the system is under memory pressure and is asking
3292 * to reclaim memory. A return value of TRUE indicates that the system
3293 * is under memory pressure and that the arc should adjust accordingly.
3296 arc_reclaim_needed(void)
3298 return (arc_available_memory() < 0);
3302 arc_kmem_reap_now(void)
3305 kmem_cache_t
*prev_cache
= NULL
;
3306 kmem_cache_t
*prev_data_cache
= NULL
;
3307 extern kmem_cache_t
*zio_buf_cache
[];
3308 extern kmem_cache_t
*zio_data_buf_cache
[];
3309 extern kmem_cache_t
*range_seg_cache
;
3311 if ((arc_meta_used
>= arc_meta_limit
) && zfs_arc_meta_prune
) {
3313 * We are exceeding our meta-data cache limit.
3314 * Prune some entries to release holds on meta-data.
3316 arc_prune(zfs_arc_meta_prune
);
3319 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
3320 if (zio_buf_cache
[i
] != prev_cache
) {
3321 prev_cache
= zio_buf_cache
[i
];
3322 kmem_cache_reap_now(zio_buf_cache
[i
]);
3324 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
3325 prev_data_cache
= zio_data_buf_cache
[i
];
3326 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
3329 kmem_cache_reap_now(buf_cache
);
3330 kmem_cache_reap_now(hdr_full_cache
);
3331 kmem_cache_reap_now(hdr_l2only_cache
);
3332 kmem_cache_reap_now(range_seg_cache
);
3334 if (zio_arena
!= NULL
) {
3336 * Ask the vmem arena to reclaim unused memory from its
3339 vmem_qcache_reap(zio_arena
);
3344 * Threads can block in arc_get_data_buf() waiting for this thread to evict
3345 * enough data and signal them to proceed. When this happens, the threads in
3346 * arc_get_data_buf() are sleeping while holding the hash lock for their
3347 * particular arc header. Thus, we must be careful to never sleep on a
3348 * hash lock in this thread. This is to prevent the following deadlock:
3350 * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L",
3351 * waiting for the reclaim thread to signal it.
3353 * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter,
3354 * fails, and goes to sleep forever.
3356 * This possible deadlock is avoided by always acquiring a hash lock
3357 * using mutex_tryenter() from arc_reclaim_thread().
3360 arc_reclaim_thread(void)
3362 fstrans_cookie_t cookie
= spl_fstrans_mark();
3363 clock_t growtime
= 0;
3366 CALLB_CPR_INIT(&cpr
, &arc_reclaim_lock
, callb_generic_cpr
, FTAG
);
3368 mutex_enter(&arc_reclaim_lock
);
3369 while (!arc_reclaim_thread_exit
) {
3371 int64_t free_memory
= arc_available_memory();
3372 uint64_t evicted
= 0;
3374 arc_tuning_update();
3376 mutex_exit(&arc_reclaim_lock
);
3378 if (free_memory
< 0) {
3380 arc_no_grow
= B_TRUE
;
3384 * Wait at least zfs_grow_retry (default 5) seconds
3385 * before considering growing.
3387 growtime
= ddi_get_lbolt() + (arc_grow_retry
* hz
);
3389 arc_kmem_reap_now();
3392 * If we are still low on memory, shrink the ARC
3393 * so that we have arc_shrink_min free space.
3395 free_memory
= arc_available_memory();
3397 to_free
= (arc_c
>> arc_shrink_shift
) - free_memory
;
3400 to_free
= MAX(to_free
, ptob(needfree
));
3402 arc_shrink(to_free
);
3404 } else if (free_memory
< arc_c
>> arc_no_grow_shift
) {
3405 arc_no_grow
= B_TRUE
;
3406 } else if (ddi_get_lbolt() >= growtime
) {
3407 arc_no_grow
= B_FALSE
;
3410 evicted
= arc_adjust();
3412 mutex_enter(&arc_reclaim_lock
);
3415 * If evicted is zero, we couldn't evict anything via
3416 * arc_adjust(). This could be due to hash lock
3417 * collisions, but more likely due to the majority of
3418 * arc buffers being unevictable. Therefore, even if
3419 * arc_size is above arc_c, another pass is unlikely to
3420 * be helpful and could potentially cause us to enter an
3423 if (arc_size
<= arc_c
|| evicted
== 0) {
3425 * We're either no longer overflowing, or we
3426 * can't evict anything more, so we should wake
3427 * up any threads before we go to sleep.
3429 cv_broadcast(&arc_reclaim_waiters_cv
);
3432 * Block until signaled, or after one second (we
3433 * might need to perform arc_kmem_reap_now()
3434 * even if we aren't being signalled)
3436 CALLB_CPR_SAFE_BEGIN(&cpr
);
3437 (void) cv_timedwait_sig(&arc_reclaim_thread_cv
,
3438 &arc_reclaim_lock
, ddi_get_lbolt() + hz
);
3439 CALLB_CPR_SAFE_END(&cpr
, &arc_reclaim_lock
);
3443 arc_reclaim_thread_exit
= FALSE
;
3444 cv_broadcast(&arc_reclaim_thread_cv
);
3445 CALLB_CPR_EXIT(&cpr
); /* drops arc_reclaim_lock */
3446 spl_fstrans_unmark(cookie
);
3451 arc_user_evicts_thread(void)
3453 fstrans_cookie_t cookie
= spl_fstrans_mark();
3456 CALLB_CPR_INIT(&cpr
, &arc_user_evicts_lock
, callb_generic_cpr
, FTAG
);
3458 mutex_enter(&arc_user_evicts_lock
);
3459 while (!arc_user_evicts_thread_exit
) {
3460 mutex_exit(&arc_user_evicts_lock
);
3462 arc_do_user_evicts();
3465 * This is necessary in order for the mdb ::arc dcmd to
3466 * show up to date information. Since the ::arc command
3467 * does not call the kstat's update function, without
3468 * this call, the command may show stale stats for the
3469 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
3470 * with this change, the data might be up to 1 second
3471 * out of date; but that should suffice. The arc_state_t
3472 * structures can be queried directly if more accurate
3473 * information is needed.
3475 if (arc_ksp
!= NULL
)
3476 arc_ksp
->ks_update(arc_ksp
, KSTAT_READ
);
3478 mutex_enter(&arc_user_evicts_lock
);
3481 * Block until signaled, or after one second (we need to
3482 * call the arc's kstat update function regularly).
3484 CALLB_CPR_SAFE_BEGIN(&cpr
);
3485 (void) cv_timedwait_sig(&arc_user_evicts_cv
,
3486 &arc_user_evicts_lock
, ddi_get_lbolt() + hz
);
3487 CALLB_CPR_SAFE_END(&cpr
, &arc_user_evicts_lock
);
3490 arc_user_evicts_thread_exit
= FALSE
;
3491 cv_broadcast(&arc_user_evicts_cv
);
3492 CALLB_CPR_EXIT(&cpr
); /* drops arc_user_evicts_lock */
3493 spl_fstrans_unmark(cookie
);
3499 * Determine the amount of memory eligible for eviction contained in the
3500 * ARC. All clean data reported by the ghost lists can always be safely
3501 * evicted. Due to arc_c_min, the same does not hold for all clean data
3502 * contained by the regular mru and mfu lists.
3504 * In the case of the regular mru and mfu lists, we need to report as
3505 * much clean data as possible, such that evicting that same reported
3506 * data will not bring arc_size below arc_c_min. Thus, in certain
3507 * circumstances, the total amount of clean data in the mru and mfu
3508 * lists might not actually be evictable.
3510 * The following two distinct cases are accounted for:
3512 * 1. The sum of the amount of dirty data contained by both the mru and
3513 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
3514 * is greater than or equal to arc_c_min.
3515 * (i.e. amount of dirty data >= arc_c_min)
3517 * This is the easy case; all clean data contained by the mru and mfu
3518 * lists is evictable. Evicting all clean data can only drop arc_size
3519 * to the amount of dirty data, which is greater than arc_c_min.
3521 * 2. The sum of the amount of dirty data contained by both the mru and
3522 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
3523 * is less than arc_c_min.
3524 * (i.e. arc_c_min > amount of dirty data)
3526 * 2.1. arc_size is greater than or equal arc_c_min.
3527 * (i.e. arc_size >= arc_c_min > amount of dirty data)
3529 * In this case, not all clean data from the regular mru and mfu
3530 * lists is actually evictable; we must leave enough clean data
3531 * to keep arc_size above arc_c_min. Thus, the maximum amount of
3532 * evictable data from the two lists combined, is exactly the
3533 * difference between arc_size and arc_c_min.
3535 * 2.2. arc_size is less than arc_c_min
3536 * (i.e. arc_c_min > arc_size > amount of dirty data)
3538 * In this case, none of the data contained in the mru and mfu
3539 * lists is evictable, even if it's clean. Since arc_size is
3540 * already below arc_c_min, evicting any more would only
3541 * increase this negative difference.
3544 arc_evictable_memory(void) {
3545 uint64_t arc_clean
=
3546 arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] +
3547 arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] +
3548 arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] +
3549 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
];
3550 uint64_t ghost_clean
=
3551 arc_mru_ghost
->arcs_lsize
[ARC_BUFC_DATA
] +
3552 arc_mru_ghost
->arcs_lsize
[ARC_BUFC_METADATA
] +
3553 arc_mfu_ghost
->arcs_lsize
[ARC_BUFC_DATA
] +
3554 arc_mfu_ghost
->arcs_lsize
[ARC_BUFC_METADATA
];
3555 uint64_t arc_dirty
= MAX((int64_t)arc_size
- (int64_t)arc_clean
, 0);
3557 if (arc_dirty
>= arc_c_min
)
3558 return (ghost_clean
+ arc_clean
);
3560 return (ghost_clean
+ MAX((int64_t)arc_size
- (int64_t)arc_c_min
, 0));
3564 * If sc->nr_to_scan is zero, the caller is requesting a query of the
3565 * number of objects which can potentially be freed. If it is nonzero,
3566 * the request is to free that many objects.
3568 * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks
3569 * in struct shrinker and also require the shrinker to return the number
3572 * Older kernels require the shrinker to return the number of freeable
3573 * objects following the freeing of nr_to_free.
3575 static spl_shrinker_t
3576 __arc_shrinker_func(struct shrinker
*shrink
, struct shrink_control
*sc
)
3580 /* The arc is considered warm once reclaim has occurred */
3581 if (unlikely(arc_warm
== B_FALSE
))
3584 /* Return the potential number of reclaimable pages */
3585 pages
= btop((int64_t)arc_evictable_memory());
3586 if (sc
->nr_to_scan
== 0)
3589 /* Not allowed to perform filesystem reclaim */
3590 if (!(sc
->gfp_mask
& __GFP_FS
))
3591 return (SHRINK_STOP
);
3593 /* Reclaim in progress */
3594 if (mutex_tryenter(&arc_reclaim_lock
) == 0)
3595 return (SHRINK_STOP
);
3597 mutex_exit(&arc_reclaim_lock
);
3600 * Evict the requested number of pages by shrinking arc_c the
3601 * requested amount. If there is nothing left to evict just
3602 * reap whatever we can from the various arc slabs.
3605 arc_shrink(ptob(sc
->nr_to_scan
));
3606 arc_kmem_reap_now();
3607 #ifdef HAVE_SPLIT_SHRINKER_CALLBACK
3608 pages
= MAX(pages
- btop(arc_evictable_memory()), 0);
3610 pages
= btop(arc_evictable_memory());
3613 arc_kmem_reap_now();
3614 pages
= SHRINK_STOP
;
3618 * We've reaped what we can, wake up threads.
3620 cv_broadcast(&arc_reclaim_waiters_cv
);
3623 * When direct reclaim is observed it usually indicates a rapid
3624 * increase in memory pressure. This occurs because the kswapd
3625 * threads were unable to asynchronously keep enough free memory
3626 * available. In this case set arc_no_grow to briefly pause arc
3627 * growth to avoid compounding the memory pressure.
3629 if (current_is_kswapd()) {
3630 ARCSTAT_BUMP(arcstat_memory_indirect_count
);
3632 arc_no_grow
= B_TRUE
;
3633 arc_grow_time
= ddi_get_lbolt() + (zfs_arc_grow_retry
* hz
);
3634 ARCSTAT_BUMP(arcstat_memory_direct_count
);
3639 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func
);
3641 SPL_SHRINKER_DECLARE(arc_shrinker
, arc_shrinker_func
, DEFAULT_SEEKS
);
3642 #endif /* _KERNEL */
3645 * Adapt arc info given the number of bytes we are trying to add and
3646 * the state that we are comming from. This function is only called
3647 * when we are adding new content to the cache.
3650 arc_adapt(int bytes
, arc_state_t
*state
)
3654 if (state
== arc_l2c_only
)
3659 * Adapt the target size of the MRU list:
3660 * - if we just hit in the MRU ghost list, then increase
3661 * the target size of the MRU list.
3662 * - if we just hit in the MFU ghost list, then increase
3663 * the target size of the MFU list by decreasing the
3664 * target size of the MRU list.
3666 if (state
== arc_mru_ghost
) {
3667 mult
= ((arc_mru_ghost
->arcs_size
>= arc_mfu_ghost
->arcs_size
) ?
3668 1 : (arc_mfu_ghost
->arcs_size
/arc_mru_ghost
->arcs_size
));
3670 if (!zfs_arc_p_dampener_disable
)
3671 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
3673 arc_p
= MIN(arc_c
, arc_p
+ bytes
* mult
);
3674 } else if (state
== arc_mfu_ghost
) {
3677 mult
= ((arc_mfu_ghost
->arcs_size
>= arc_mru_ghost
->arcs_size
) ?
3678 1 : (arc_mru_ghost
->arcs_size
/arc_mfu_ghost
->arcs_size
));
3680 if (!zfs_arc_p_dampener_disable
)
3681 mult
= MIN(mult
, 10);
3683 delta
= MIN(bytes
* mult
, arc_p
);
3684 arc_p
= MAX(0, arc_p
- delta
);
3686 ASSERT((int64_t)arc_p
>= 0);
3688 if (arc_reclaim_needed()) {
3689 cv_signal(&arc_reclaim_thread_cv
);
3696 if (arc_c
>= arc_c_max
)
3700 * If we're within (2 * maxblocksize) bytes of the target
3701 * cache size, increment the target cache size
3703 VERIFY3U(arc_c
, >=, 2ULL << SPA_MAXBLOCKSHIFT
);
3704 if (arc_size
>= arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
3705 atomic_add_64(&arc_c
, (int64_t)bytes
);
3706 if (arc_c
> arc_c_max
)
3708 else if (state
== arc_anon
)
3709 atomic_add_64(&arc_p
, (int64_t)bytes
);
3713 ASSERT((int64_t)arc_p
>= 0);
3717 * Check if arc_size has grown past our upper threshold, determined by
3718 * zfs_arc_overflow_shift.
3721 arc_is_overflowing(void)
3723 /* Always allow at least one block of overflow */
3724 uint64_t overflow
= MAX(SPA_MAXBLOCKSIZE
,
3725 arc_c
>> zfs_arc_overflow_shift
);
3727 return (arc_size
>= arc_c
+ overflow
);
3731 * The buffer, supplied as the first argument, needs a data block. If we
3732 * are hitting the hard limit for the cache size, we must sleep, waiting
3733 * for the eviction thread to catch up. If we're past the target size
3734 * but below the hard limit, we'll only signal the reclaim thread and
3738 arc_get_data_buf(arc_buf_t
*buf
)
3740 arc_state_t
*state
= buf
->b_hdr
->b_l1hdr
.b_state
;
3741 uint64_t size
= buf
->b_hdr
->b_size
;
3742 arc_buf_contents_t type
= arc_buf_type(buf
->b_hdr
);
3744 arc_adapt(size
, state
);
3747 * If arc_size is currently overflowing, and has grown past our
3748 * upper limit, we must be adding data faster than the evict
3749 * thread can evict. Thus, to ensure we don't compound the
3750 * problem by adding more data and forcing arc_size to grow even
3751 * further past it's target size, we halt and wait for the
3752 * eviction thread to catch up.
3754 * It's also possible that the reclaim thread is unable to evict
3755 * enough buffers to get arc_size below the overflow limit (e.g.
3756 * due to buffers being un-evictable, or hash lock collisions).
3757 * In this case, we want to proceed regardless if we're
3758 * overflowing; thus we don't use a while loop here.
3760 if (arc_is_overflowing()) {
3761 mutex_enter(&arc_reclaim_lock
);
3764 * Now that we've acquired the lock, we may no longer be
3765 * over the overflow limit, lets check.
3767 * We're ignoring the case of spurious wake ups. If that
3768 * were to happen, it'd let this thread consume an ARC
3769 * buffer before it should have (i.e. before we're under
3770 * the overflow limit and were signalled by the reclaim
3771 * thread). As long as that is a rare occurrence, it
3772 * shouldn't cause any harm.
3774 if (arc_is_overflowing()) {
3775 cv_signal(&arc_reclaim_thread_cv
);
3776 cv_wait(&arc_reclaim_waiters_cv
, &arc_reclaim_lock
);
3779 mutex_exit(&arc_reclaim_lock
);
3782 if (type
== ARC_BUFC_METADATA
) {
3783 buf
->b_data
= zio_buf_alloc(size
);
3784 arc_space_consume(size
, ARC_SPACE_META
);
3786 ASSERT(type
== ARC_BUFC_DATA
);
3787 buf
->b_data
= zio_data_buf_alloc(size
);
3788 arc_space_consume(size
, ARC_SPACE_DATA
);
3792 * Update the state size. Note that ghost states have a
3793 * "ghost size" and so don't need to be updated.
3795 if (!GHOST_STATE(buf
->b_hdr
->b_l1hdr
.b_state
)) {
3796 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3798 atomic_add_64(&hdr
->b_l1hdr
.b_state
->arcs_size
, size
);
3801 * If this is reached via arc_read, the link is
3802 * protected by the hash lock. If reached via
3803 * arc_buf_alloc, the header should not be accessed by
3804 * any other thread. And, if reached via arc_read_done,
3805 * the hash lock will protect it if it's found in the
3806 * hash table; otherwise no other thread should be
3807 * trying to [add|remove]_reference it.
3809 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
3810 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3811 atomic_add_64(&hdr
->b_l1hdr
.b_state
->arcs_lsize
[type
],
3815 * If we are growing the cache, and we are adding anonymous
3816 * data, and we have outgrown arc_p, update arc_p
3818 if (arc_size
< arc_c
&& hdr
->b_l1hdr
.b_state
== arc_anon
&&
3819 arc_anon
->arcs_size
+ arc_mru
->arcs_size
> arc_p
)
3820 arc_p
= MIN(arc_c
, arc_p
+ size
);
3825 * This routine is called whenever a buffer is accessed.
3826 * NOTE: the hash lock is dropped in this function.
3829 arc_access(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
3833 ASSERT(MUTEX_HELD(hash_lock
));
3834 ASSERT(HDR_HAS_L1HDR(hdr
));
3836 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
3838 * This buffer is not in the cache, and does not
3839 * appear in our "ghost" list. Add the new buffer
3843 ASSERT0(hdr
->b_l1hdr
.b_arc_access
);
3844 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
3845 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
3846 arc_change_state(arc_mru
, hdr
, hash_lock
);
3848 } else if (hdr
->b_l1hdr
.b_state
== arc_mru
) {
3849 now
= ddi_get_lbolt();
3852 * If this buffer is here because of a prefetch, then either:
3853 * - clear the flag if this is a "referencing" read
3854 * (any subsequent access will bump this into the MFU state).
3856 * - move the buffer to the head of the list if this is
3857 * another prefetch (to make it less likely to be evicted).
3859 if (HDR_PREFETCH(hdr
)) {
3860 if (refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
3861 /* link protected by hash lock */
3862 ASSERT(multilist_link_active(
3863 &hdr
->b_l1hdr
.b_arc_node
));
3865 hdr
->b_flags
&= ~ARC_FLAG_PREFETCH
;
3866 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
3867 ARCSTAT_BUMP(arcstat_mru_hits
);
3869 hdr
->b_l1hdr
.b_arc_access
= now
;
3874 * This buffer has been "accessed" only once so far,
3875 * but it is still in the cache. Move it to the MFU
3878 if (ddi_time_after(now
, hdr
->b_l1hdr
.b_arc_access
+
3881 * More than 125ms have passed since we
3882 * instantiated this buffer. Move it to the
3883 * most frequently used state.
3885 hdr
->b_l1hdr
.b_arc_access
= now
;
3886 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
3887 arc_change_state(arc_mfu
, hdr
, hash_lock
);
3889 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
3890 ARCSTAT_BUMP(arcstat_mru_hits
);
3891 } else if (hdr
->b_l1hdr
.b_state
== arc_mru_ghost
) {
3892 arc_state_t
*new_state
;
3894 * This buffer has been "accessed" recently, but
3895 * was evicted from the cache. Move it to the
3899 if (HDR_PREFETCH(hdr
)) {
3900 new_state
= arc_mru
;
3901 if (refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0)
3902 hdr
->b_flags
&= ~ARC_FLAG_PREFETCH
;
3903 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
3905 new_state
= arc_mfu
;
3906 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
3909 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
3910 arc_change_state(new_state
, hdr
, hash_lock
);
3912 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_ghost_hits
);
3913 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
3914 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu
) {
3916 * This buffer has been accessed more than once and is
3917 * still in the cache. Keep it in the MFU state.
3919 * NOTE: an add_reference() that occurred when we did
3920 * the arc_read() will have kicked this off the list.
3921 * If it was a prefetch, we will explicitly move it to
3922 * the head of the list now.
3924 if ((HDR_PREFETCH(hdr
)) != 0) {
3925 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3926 /* link protected by hash_lock */
3927 ASSERT(multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
3929 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_hits
);
3930 ARCSTAT_BUMP(arcstat_mfu_hits
);
3931 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
3932 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu_ghost
) {
3933 arc_state_t
*new_state
= arc_mfu
;
3935 * This buffer has been accessed more than once but has
3936 * been evicted from the cache. Move it back to the
3940 if (HDR_PREFETCH(hdr
)) {
3942 * This is a prefetch access...
3943 * move this block back to the MRU state.
3945 ASSERT0(refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
3946 new_state
= arc_mru
;
3949 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
3950 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
3951 arc_change_state(new_state
, hdr
, hash_lock
);
3953 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_ghost_hits
);
3954 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
3955 } else if (hdr
->b_l1hdr
.b_state
== arc_l2c_only
) {
3957 * This buffer is on the 2nd Level ARC.
3960 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
3961 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
3962 arc_change_state(arc_mfu
, hdr
, hash_lock
);
3964 cmn_err(CE_PANIC
, "invalid arc state 0x%p",
3965 hdr
->b_l1hdr
.b_state
);
3969 /* a generic arc_done_func_t which you can use */
3972 arc_bcopy_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3974 if (zio
== NULL
|| zio
->io_error
== 0)
3975 bcopy(buf
->b_data
, arg
, buf
->b_hdr
->b_size
);
3976 VERIFY(arc_buf_remove_ref(buf
, arg
));
3979 /* a generic arc_done_func_t */
3981 arc_getbuf_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3983 arc_buf_t
**bufp
= arg
;
3984 if (zio
&& zio
->io_error
) {
3985 VERIFY(arc_buf_remove_ref(buf
, arg
));
3989 ASSERT(buf
->b_data
);
3994 arc_read_done(zio_t
*zio
)
3998 arc_buf_t
*abuf
; /* buffer we're assigning to callback */
3999 kmutex_t
*hash_lock
= NULL
;
4000 arc_callback_t
*callback_list
, *acb
;
4001 int freeable
= FALSE
;
4003 buf
= zio
->io_private
;
4007 * The hdr was inserted into hash-table and removed from lists
4008 * prior to starting I/O. We should find this header, since
4009 * it's in the hash table, and it should be legit since it's
4010 * not possible to evict it during the I/O. The only possible
4011 * reason for it not to be found is if we were freed during the
4014 if (HDR_IN_HASH_TABLE(hdr
)) {
4015 arc_buf_hdr_t
*found
;
4017 ASSERT3U(hdr
->b_birth
, ==, BP_PHYSICAL_BIRTH(zio
->io_bp
));
4018 ASSERT3U(hdr
->b_dva
.dva_word
[0], ==,
4019 BP_IDENTITY(zio
->io_bp
)->dva_word
[0]);
4020 ASSERT3U(hdr
->b_dva
.dva_word
[1], ==,
4021 BP_IDENTITY(zio
->io_bp
)->dva_word
[1]);
4023 found
= buf_hash_find(hdr
->b_spa
, zio
->io_bp
,
4026 ASSERT((found
== NULL
&& HDR_FREED_IN_READ(hdr
) &&
4027 hash_lock
== NULL
) ||
4029 DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
4030 (found
== hdr
&& HDR_L2_READING(hdr
)));
4033 hdr
->b_flags
&= ~ARC_FLAG_L2_EVICTED
;
4034 if (l2arc_noprefetch
&& HDR_PREFETCH(hdr
))
4035 hdr
->b_flags
&= ~ARC_FLAG_L2CACHE
;
4037 /* byteswap if necessary */
4038 callback_list
= hdr
->b_l1hdr
.b_acb
;
4039 ASSERT(callback_list
!= NULL
);
4040 if (BP_SHOULD_BYTESWAP(zio
->io_bp
) && zio
->io_error
== 0) {
4041 dmu_object_byteswap_t bswap
=
4042 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
4043 if (BP_GET_LEVEL(zio
->io_bp
) > 0)
4044 byteswap_uint64_array(buf
->b_data
, hdr
->b_size
);
4046 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, hdr
->b_size
);
4049 arc_cksum_compute(buf
, B_FALSE
);
4052 if (hash_lock
&& zio
->io_error
== 0 &&
4053 hdr
->b_l1hdr
.b_state
== arc_anon
) {
4055 * Only call arc_access on anonymous buffers. This is because
4056 * if we've issued an I/O for an evicted buffer, we've already
4057 * called arc_access (to prevent any simultaneous readers from
4058 * getting confused).
4060 arc_access(hdr
, hash_lock
);
4063 /* create copies of the data buffer for the callers */
4065 for (acb
= callback_list
; acb
; acb
= acb
->acb_next
) {
4066 if (acb
->acb_done
) {
4068 ARCSTAT_BUMP(arcstat_duplicate_reads
);
4069 abuf
= arc_buf_clone(buf
);
4071 acb
->acb_buf
= abuf
;
4075 hdr
->b_l1hdr
.b_acb
= NULL
;
4076 hdr
->b_flags
&= ~ARC_FLAG_IO_IN_PROGRESS
;
4077 ASSERT(!HDR_BUF_AVAILABLE(hdr
));
4079 ASSERT(buf
->b_efunc
== NULL
);
4080 ASSERT(hdr
->b_l1hdr
.b_datacnt
== 1);
4081 hdr
->b_flags
|= ARC_FLAG_BUF_AVAILABLE
;
4084 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
) ||
4085 callback_list
!= NULL
);
4087 if (zio
->io_error
!= 0) {
4088 hdr
->b_flags
|= ARC_FLAG_IO_ERROR
;
4089 if (hdr
->b_l1hdr
.b_state
!= arc_anon
)
4090 arc_change_state(arc_anon
, hdr
, hash_lock
);
4091 if (HDR_IN_HASH_TABLE(hdr
))
4092 buf_hash_remove(hdr
);
4093 freeable
= refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
4097 * Broadcast before we drop the hash_lock to avoid the possibility
4098 * that the hdr (and hence the cv) might be freed before we get to
4099 * the cv_broadcast().
4101 cv_broadcast(&hdr
->b_l1hdr
.b_cv
);
4103 if (hash_lock
!= NULL
) {
4104 mutex_exit(hash_lock
);
4107 * This block was freed while we waited for the read to
4108 * complete. It has been removed from the hash table and
4109 * moved to the anonymous state (so that it won't show up
4112 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
4113 freeable
= refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
4116 /* execute each callback and free its structure */
4117 while ((acb
= callback_list
) != NULL
) {
4119 acb
->acb_done(zio
, acb
->acb_buf
, acb
->acb_private
);
4121 if (acb
->acb_zio_dummy
!= NULL
) {
4122 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
4123 zio_nowait(acb
->acb_zio_dummy
);
4126 callback_list
= acb
->acb_next
;
4127 kmem_free(acb
, sizeof (arc_callback_t
));
4131 arc_hdr_destroy(hdr
);
4135 * "Read" the block at the specified DVA (in bp) via the
4136 * cache. If the block is found in the cache, invoke the provided
4137 * callback immediately and return. Note that the `zio' parameter
4138 * in the callback will be NULL in this case, since no IO was
4139 * required. If the block is not in the cache pass the read request
4140 * on to the spa with a substitute callback function, so that the
4141 * requested block will be added to the cache.
4143 * If a read request arrives for a block that has a read in-progress,
4144 * either wait for the in-progress read to complete (and return the
4145 * results); or, if this is a read with a "done" func, add a record
4146 * to the read to invoke the "done" func when the read completes,
4147 * and return; or just return.
4149 * arc_read_done() will invoke all the requested "done" functions
4150 * for readers of this block.
4153 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
, arc_done_func_t
*done
,
4154 void *private, zio_priority_t priority
, int zio_flags
,
4155 arc_flags_t
*arc_flags
, const zbookmark_phys_t
*zb
)
4157 arc_buf_hdr_t
*hdr
= NULL
;
4158 arc_buf_t
*buf
= NULL
;
4159 kmutex_t
*hash_lock
= NULL
;
4161 uint64_t guid
= spa_load_guid(spa
);
4164 ASSERT(!BP_IS_EMBEDDED(bp
) ||
4165 BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
);
4168 if (!BP_IS_EMBEDDED(bp
)) {
4170 * Embedded BP's have no DVA and require no I/O to "read".
4171 * Create an anonymous arc buf to back it.
4173 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
4176 if (hdr
!= NULL
&& HDR_HAS_L1HDR(hdr
) && hdr
->b_l1hdr
.b_datacnt
> 0) {
4178 *arc_flags
|= ARC_FLAG_CACHED
;
4180 if (HDR_IO_IN_PROGRESS(hdr
)) {
4182 if (*arc_flags
& ARC_FLAG_WAIT
) {
4183 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
4184 mutex_exit(hash_lock
);
4187 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
4190 arc_callback_t
*acb
= NULL
;
4192 acb
= kmem_zalloc(sizeof (arc_callback_t
),
4194 acb
->acb_done
= done
;
4195 acb
->acb_private
= private;
4197 acb
->acb_zio_dummy
= zio_null(pio
,
4198 spa
, NULL
, NULL
, NULL
, zio_flags
);
4200 ASSERT(acb
->acb_done
!= NULL
);
4201 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
4202 hdr
->b_l1hdr
.b_acb
= acb
;
4203 add_reference(hdr
, hash_lock
, private);
4204 mutex_exit(hash_lock
);
4207 mutex_exit(hash_lock
);
4211 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
4212 hdr
->b_l1hdr
.b_state
== arc_mfu
);
4215 add_reference(hdr
, hash_lock
, private);
4217 * If this block is already in use, create a new
4218 * copy of the data so that we will be guaranteed
4219 * that arc_release() will always succeed.
4221 buf
= hdr
->b_l1hdr
.b_buf
;
4223 ASSERT(buf
->b_data
);
4224 if (HDR_BUF_AVAILABLE(hdr
)) {
4225 ASSERT(buf
->b_efunc
== NULL
);
4226 hdr
->b_flags
&= ~ARC_FLAG_BUF_AVAILABLE
;
4228 buf
= arc_buf_clone(buf
);
4231 } else if (*arc_flags
& ARC_FLAG_PREFETCH
&&
4232 refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
4233 hdr
->b_flags
|= ARC_FLAG_PREFETCH
;
4235 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
4236 arc_access(hdr
, hash_lock
);
4237 if (*arc_flags
& ARC_FLAG_L2CACHE
)
4238 hdr
->b_flags
|= ARC_FLAG_L2CACHE
;
4239 if (*arc_flags
& ARC_FLAG_L2COMPRESS
)
4240 hdr
->b_flags
|= ARC_FLAG_L2COMPRESS
;
4241 mutex_exit(hash_lock
);
4242 ARCSTAT_BUMP(arcstat_hits
);
4243 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
4244 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
4245 data
, metadata
, hits
);
4248 done(NULL
, buf
, private);
4250 uint64_t size
= BP_GET_LSIZE(bp
);
4251 arc_callback_t
*acb
;
4254 boolean_t devw
= B_FALSE
;
4255 enum zio_compress b_compress
= ZIO_COMPRESS_OFF
;
4256 int32_t b_asize
= 0;
4259 * Gracefully handle a damaged logical block size as a
4260 * checksum error by passing a dummy zio to the done callback.
4262 if (size
> spa_maxblocksize(spa
)) {
4264 rzio
= zio_null(pio
, spa
, NULL
,
4265 NULL
, NULL
, zio_flags
);
4266 rzio
->io_error
= ECKSUM
;
4267 done(rzio
, buf
, private);
4275 /* this block is not in the cache */
4276 arc_buf_hdr_t
*exists
= NULL
;
4277 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
4278 buf
= arc_buf_alloc(spa
, size
, private, type
);
4280 if (!BP_IS_EMBEDDED(bp
)) {
4281 hdr
->b_dva
= *BP_IDENTITY(bp
);
4282 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
4283 exists
= buf_hash_insert(hdr
, &hash_lock
);
4285 if (exists
!= NULL
) {
4286 /* somebody beat us to the hash insert */
4287 mutex_exit(hash_lock
);
4288 buf_discard_identity(hdr
);
4289 (void) arc_buf_remove_ref(buf
, private);
4290 goto top
; /* restart the IO request */
4293 /* if this is a prefetch, we don't have a reference */
4294 if (*arc_flags
& ARC_FLAG_PREFETCH
) {
4295 (void) remove_reference(hdr
, hash_lock
,
4297 hdr
->b_flags
|= ARC_FLAG_PREFETCH
;
4299 if (*arc_flags
& ARC_FLAG_L2CACHE
)
4300 hdr
->b_flags
|= ARC_FLAG_L2CACHE
;
4301 if (*arc_flags
& ARC_FLAG_L2COMPRESS
)
4302 hdr
->b_flags
|= ARC_FLAG_L2COMPRESS
;
4303 if (BP_GET_LEVEL(bp
) > 0)
4304 hdr
->b_flags
|= ARC_FLAG_INDIRECT
;
4307 * This block is in the ghost cache. If it was L2-only
4308 * (and thus didn't have an L1 hdr), we realloc the
4309 * header to add an L1 hdr.
4311 if (!HDR_HAS_L1HDR(hdr
)) {
4312 hdr
= arc_hdr_realloc(hdr
, hdr_l2only_cache
,
4316 ASSERT(GHOST_STATE(hdr
->b_l1hdr
.b_state
));
4317 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
4318 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
4319 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
4321 /* if this is a prefetch, we don't have a reference */
4322 if (*arc_flags
& ARC_FLAG_PREFETCH
)
4323 hdr
->b_flags
|= ARC_FLAG_PREFETCH
;
4325 add_reference(hdr
, hash_lock
, private);
4326 if (*arc_flags
& ARC_FLAG_L2CACHE
)
4327 hdr
->b_flags
|= ARC_FLAG_L2CACHE
;
4328 if (*arc_flags
& ARC_FLAG_L2COMPRESS
)
4329 hdr
->b_flags
|= ARC_FLAG_L2COMPRESS
;
4330 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
4333 buf
->b_efunc
= NULL
;
4334 buf
->b_private
= NULL
;
4336 hdr
->b_l1hdr
.b_buf
= buf
;
4337 ASSERT0(hdr
->b_l1hdr
.b_datacnt
);
4338 hdr
->b_l1hdr
.b_datacnt
= 1;
4339 arc_get_data_buf(buf
);
4340 arc_access(hdr
, hash_lock
);
4343 ASSERT(!GHOST_STATE(hdr
->b_l1hdr
.b_state
));
4345 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
4346 acb
->acb_done
= done
;
4347 acb
->acb_private
= private;
4349 ASSERT(hdr
->b_l1hdr
.b_acb
== NULL
);
4350 hdr
->b_l1hdr
.b_acb
= acb
;
4351 hdr
->b_flags
|= ARC_FLAG_IO_IN_PROGRESS
;
4353 if (HDR_HAS_L2HDR(hdr
) &&
4354 (vd
= hdr
->b_l2hdr
.b_dev
->l2ad_vdev
) != NULL
) {
4355 devw
= hdr
->b_l2hdr
.b_dev
->l2ad_writing
;
4356 addr
= hdr
->b_l2hdr
.b_daddr
;
4357 b_compress
= HDR_GET_COMPRESS(hdr
);
4358 b_asize
= hdr
->b_l2hdr
.b_asize
;
4360 * Lock out device removal.
4362 if (vdev_is_dead(vd
) ||
4363 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
4367 if (hash_lock
!= NULL
)
4368 mutex_exit(hash_lock
);
4371 * At this point, we have a level 1 cache miss. Try again in
4372 * L2ARC if possible.
4374 ASSERT3U(hdr
->b_size
, ==, size
);
4375 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
, blkptr_t
*, bp
,
4376 uint64_t, size
, zbookmark_phys_t
*, zb
);
4377 ARCSTAT_BUMP(arcstat_misses
);
4378 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
4379 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
4380 data
, metadata
, misses
);
4382 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
4384 * Read from the L2ARC if the following are true:
4385 * 1. The L2ARC vdev was previously cached.
4386 * 2. This buffer still has L2ARC metadata.
4387 * 3. This buffer isn't currently writing to the L2ARC.
4388 * 4. The L2ARC entry wasn't evicted, which may
4389 * also have invalidated the vdev.
4390 * 5. This isn't prefetch and l2arc_noprefetch is set.
4392 if (HDR_HAS_L2HDR(hdr
) &&
4393 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
4394 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
4395 l2arc_read_callback_t
*cb
;
4397 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
4398 ARCSTAT_BUMP(arcstat_l2_hits
);
4399 atomic_inc_32(&hdr
->b_l2hdr
.b_hits
);
4401 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
4403 cb
->l2rcb_buf
= buf
;
4404 cb
->l2rcb_spa
= spa
;
4407 cb
->l2rcb_flags
= zio_flags
;
4408 cb
->l2rcb_compress
= b_compress
;
4410 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
4411 addr
+ size
< vd
->vdev_psize
-
4412 VDEV_LABEL_END_SIZE
);
4415 * l2arc read. The SCL_L2ARC lock will be
4416 * released by l2arc_read_done().
4417 * Issue a null zio if the underlying buffer
4418 * was squashed to zero size by compression.
4420 if (b_compress
== ZIO_COMPRESS_EMPTY
) {
4421 rzio
= zio_null(pio
, spa
, vd
,
4422 l2arc_read_done
, cb
,
4423 zio_flags
| ZIO_FLAG_DONT_CACHE
|
4425 ZIO_FLAG_DONT_PROPAGATE
|
4426 ZIO_FLAG_DONT_RETRY
);
4428 rzio
= zio_read_phys(pio
, vd
, addr
,
4429 b_asize
, buf
->b_data
,
4431 l2arc_read_done
, cb
, priority
,
4432 zio_flags
| ZIO_FLAG_DONT_CACHE
|
4434 ZIO_FLAG_DONT_PROPAGATE
|
4435 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
4437 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
4439 ARCSTAT_INCR(arcstat_l2_read_bytes
, b_asize
);
4441 if (*arc_flags
& ARC_FLAG_NOWAIT
) {
4446 ASSERT(*arc_flags
& ARC_FLAG_WAIT
);
4447 if (zio_wait(rzio
) == 0)
4450 /* l2arc read error; goto zio_read() */
4452 DTRACE_PROBE1(l2arc__miss
,
4453 arc_buf_hdr_t
*, hdr
);
4454 ARCSTAT_BUMP(arcstat_l2_misses
);
4455 if (HDR_L2_WRITING(hdr
))
4456 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
4457 spa_config_exit(spa
, SCL_L2ARC
, vd
);
4461 spa_config_exit(spa
, SCL_L2ARC
, vd
);
4462 if (l2arc_ndev
!= 0) {
4463 DTRACE_PROBE1(l2arc__miss
,
4464 arc_buf_hdr_t
*, hdr
);
4465 ARCSTAT_BUMP(arcstat_l2_misses
);
4469 rzio
= zio_read(pio
, spa
, bp
, buf
->b_data
, size
,
4470 arc_read_done
, buf
, priority
, zio_flags
, zb
);
4472 if (*arc_flags
& ARC_FLAG_WAIT
) {
4473 rc
= zio_wait(rzio
);
4477 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
4482 spa_read_history_add(spa
, zb
, *arc_flags
);
4487 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
4491 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
4493 p
->p_private
= private;
4494 list_link_init(&p
->p_node
);
4495 refcount_create(&p
->p_refcnt
);
4497 mutex_enter(&arc_prune_mtx
);
4498 refcount_add(&p
->p_refcnt
, &arc_prune_list
);
4499 list_insert_head(&arc_prune_list
, p
);
4500 mutex_exit(&arc_prune_mtx
);
4506 arc_remove_prune_callback(arc_prune_t
*p
)
4508 mutex_enter(&arc_prune_mtx
);
4509 list_remove(&arc_prune_list
, p
);
4510 if (refcount_remove(&p
->p_refcnt
, &arc_prune_list
) == 0) {
4511 refcount_destroy(&p
->p_refcnt
);
4512 kmem_free(p
, sizeof (*p
));
4514 mutex_exit(&arc_prune_mtx
);
4518 arc_set_callback(arc_buf_t
*buf
, arc_evict_func_t
*func
, void *private)
4520 ASSERT(buf
->b_hdr
!= NULL
);
4521 ASSERT(buf
->b_hdr
->b_l1hdr
.b_state
!= arc_anon
);
4522 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
) ||
4524 ASSERT(buf
->b_efunc
== NULL
);
4525 ASSERT(!HDR_BUF_AVAILABLE(buf
->b_hdr
));
4527 buf
->b_efunc
= func
;
4528 buf
->b_private
= private;
4532 * Notify the arc that a block was freed, and thus will never be used again.
4535 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
4538 kmutex_t
*hash_lock
;
4539 uint64_t guid
= spa_load_guid(spa
);
4541 ASSERT(!BP_IS_EMBEDDED(bp
));
4543 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
4546 if (HDR_BUF_AVAILABLE(hdr
)) {
4547 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
4548 add_reference(hdr
, hash_lock
, FTAG
);
4549 hdr
->b_flags
&= ~ARC_FLAG_BUF_AVAILABLE
;
4550 mutex_exit(hash_lock
);
4552 arc_release(buf
, FTAG
);
4553 (void) arc_buf_remove_ref(buf
, FTAG
);
4555 mutex_exit(hash_lock
);
4561 * Clear the user eviction callback set by arc_set_callback(), first calling
4562 * it if it exists. Because the presence of a callback keeps an arc_buf cached
4563 * clearing the callback may result in the arc_buf being destroyed. However,
4564 * it will not result in the *last* arc_buf being destroyed, hence the data
4565 * will remain cached in the ARC. We make a copy of the arc buffer here so
4566 * that we can process the callback without holding any locks.
4568 * It's possible that the callback is already in the process of being cleared
4569 * by another thread. In this case we can not clear the callback.
4571 * Returns B_TRUE if the callback was successfully called and cleared.
4574 arc_clear_callback(arc_buf_t
*buf
)
4577 kmutex_t
*hash_lock
;
4578 arc_evict_func_t
*efunc
= buf
->b_efunc
;
4579 void *private = buf
->b_private
;
4581 mutex_enter(&buf
->b_evict_lock
);
4585 * We are in arc_do_user_evicts().
4587 ASSERT(buf
->b_data
== NULL
);
4588 mutex_exit(&buf
->b_evict_lock
);
4590 } else if (buf
->b_data
== NULL
) {
4592 * We are on the eviction list; process this buffer now
4593 * but let arc_do_user_evicts() do the reaping.
4595 buf
->b_efunc
= NULL
;
4596 mutex_exit(&buf
->b_evict_lock
);
4597 VERIFY0(efunc(private));
4600 hash_lock
= HDR_LOCK(hdr
);
4601 mutex_enter(hash_lock
);
4603 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
4605 ASSERT3U(refcount_count(&hdr
->b_l1hdr
.b_refcnt
), <,
4606 hdr
->b_l1hdr
.b_datacnt
);
4607 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
4608 hdr
->b_l1hdr
.b_state
== arc_mfu
);
4610 buf
->b_efunc
= NULL
;
4611 buf
->b_private
= NULL
;
4613 if (hdr
->b_l1hdr
.b_datacnt
> 1) {
4614 mutex_exit(&buf
->b_evict_lock
);
4615 arc_buf_destroy(buf
, TRUE
);
4617 ASSERT(buf
== hdr
->b_l1hdr
.b_buf
);
4618 hdr
->b_flags
|= ARC_FLAG_BUF_AVAILABLE
;
4619 mutex_exit(&buf
->b_evict_lock
);
4622 mutex_exit(hash_lock
);
4623 VERIFY0(efunc(private));
4628 * Release this buffer from the cache, making it an anonymous buffer. This
4629 * must be done after a read and prior to modifying the buffer contents.
4630 * If the buffer has more than one reference, we must make
4631 * a new hdr for the buffer.
4634 arc_release(arc_buf_t
*buf
, void *tag
)
4636 kmutex_t
*hash_lock
;
4638 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
4641 * It would be nice to assert that if its DMU metadata (level >
4642 * 0 || it's the dnode file), then it must be syncing context.
4643 * But we don't know that information at this level.
4646 mutex_enter(&buf
->b_evict_lock
);
4648 ASSERT(HDR_HAS_L1HDR(hdr
));
4651 * We don't grab the hash lock prior to this check, because if
4652 * the buffer's header is in the arc_anon state, it won't be
4653 * linked into the hash table.
4655 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
4656 mutex_exit(&buf
->b_evict_lock
);
4657 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
4658 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
4659 ASSERT(!HDR_HAS_L2HDR(hdr
));
4660 ASSERT(BUF_EMPTY(hdr
));
4662 ASSERT3U(hdr
->b_l1hdr
.b_datacnt
, ==, 1);
4663 ASSERT3S(refcount_count(&hdr
->b_l1hdr
.b_refcnt
), ==, 1);
4664 ASSERT(!list_link_active(&hdr
->b_l1hdr
.b_arc_node
));
4666 ASSERT3P(buf
->b_efunc
, ==, NULL
);
4667 ASSERT3P(buf
->b_private
, ==, NULL
);
4669 hdr
->b_l1hdr
.b_arc_access
= 0;
4675 hash_lock
= HDR_LOCK(hdr
);
4676 mutex_enter(hash_lock
);
4679 * This assignment is only valid as long as the hash_lock is
4680 * held, we must be careful not to reference state or the
4681 * b_state field after dropping the lock.
4683 state
= hdr
->b_l1hdr
.b_state
;
4684 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
4685 ASSERT3P(state
, !=, arc_anon
);
4687 /* this buffer is not on any list */
4688 ASSERT(refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0);
4690 if (HDR_HAS_L2HDR(hdr
)) {
4691 mutex_enter(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
4694 * We have to recheck this conditional again now that
4695 * we're holding the l2ad_mtx to prevent a race with
4696 * another thread which might be concurrently calling
4697 * l2arc_evict(). In that case, l2arc_evict() might have
4698 * destroyed the header's L2 portion as we were waiting
4699 * to acquire the l2ad_mtx.
4701 if (HDR_HAS_L2HDR(hdr
))
4702 arc_hdr_l2hdr_destroy(hdr
);
4704 mutex_exit(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
4708 * Do we have more than one buf?
4710 if (hdr
->b_l1hdr
.b_datacnt
> 1) {
4711 arc_buf_hdr_t
*nhdr
;
4713 uint64_t blksz
= hdr
->b_size
;
4714 uint64_t spa
= hdr
->b_spa
;
4715 arc_buf_contents_t type
= arc_buf_type(hdr
);
4716 uint32_t flags
= hdr
->b_flags
;
4718 ASSERT(hdr
->b_l1hdr
.b_buf
!= buf
|| buf
->b_next
!= NULL
);
4720 * Pull the data off of this hdr and attach it to
4721 * a new anonymous hdr.
4723 (void) remove_reference(hdr
, hash_lock
, tag
);
4724 bufp
= &hdr
->b_l1hdr
.b_buf
;
4725 while (*bufp
!= buf
)
4726 bufp
= &(*bufp
)->b_next
;
4727 *bufp
= buf
->b_next
;
4730 ASSERT3P(state
, !=, arc_l2c_only
);
4731 ASSERT3U(state
->arcs_size
, >=, hdr
->b_size
);
4732 atomic_add_64(&state
->arcs_size
, -hdr
->b_size
);
4733 if (refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
4736 ASSERT3P(state
, !=, arc_l2c_only
);
4737 size
= &state
->arcs_lsize
[type
];
4738 ASSERT3U(*size
, >=, hdr
->b_size
);
4739 atomic_add_64(size
, -hdr
->b_size
);
4743 * We're releasing a duplicate user data buffer, update
4744 * our statistics accordingly.
4746 if (HDR_ISTYPE_DATA(hdr
)) {
4747 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers
);
4748 ARCSTAT_INCR(arcstat_duplicate_buffers_size
,
4751 hdr
->b_l1hdr
.b_datacnt
-= 1;
4752 arc_cksum_verify(buf
);
4753 arc_buf_unwatch(buf
);
4755 mutex_exit(hash_lock
);
4757 nhdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
4758 nhdr
->b_size
= blksz
;
4761 nhdr
->b_l1hdr
.b_mru_hits
= 0;
4762 nhdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
4763 nhdr
->b_l1hdr
.b_mfu_hits
= 0;
4764 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
4765 nhdr
->b_l1hdr
.b_l2_hits
= 0;
4766 nhdr
->b_flags
= flags
& ARC_FLAG_L2_WRITING
;
4767 nhdr
->b_flags
|= arc_bufc_to_flags(type
);
4768 nhdr
->b_flags
|= ARC_FLAG_HAS_L1HDR
;
4770 nhdr
->b_l1hdr
.b_buf
= buf
;
4771 nhdr
->b_l1hdr
.b_datacnt
= 1;
4772 nhdr
->b_l1hdr
.b_state
= arc_anon
;
4773 nhdr
->b_l1hdr
.b_arc_access
= 0;
4774 nhdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
4775 nhdr
->b_freeze_cksum
= NULL
;
4777 (void) refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, tag
);
4779 mutex_exit(&buf
->b_evict_lock
);
4780 atomic_add_64(&arc_anon
->arcs_size
, blksz
);
4782 mutex_exit(&buf
->b_evict_lock
);
4783 ASSERT(refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 1);
4784 /* protected by hash lock, or hdr is on arc_anon */
4785 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
4786 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
4787 hdr
->b_l1hdr
.b_mru_hits
= 0;
4788 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
4789 hdr
->b_l1hdr
.b_mfu_hits
= 0;
4790 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
4791 hdr
->b_l1hdr
.b_l2_hits
= 0;
4792 arc_change_state(arc_anon
, hdr
, hash_lock
);
4793 hdr
->b_l1hdr
.b_arc_access
= 0;
4794 mutex_exit(hash_lock
);
4796 buf_discard_identity(hdr
);
4799 buf
->b_efunc
= NULL
;
4800 buf
->b_private
= NULL
;
4804 arc_released(arc_buf_t
*buf
)
4808 mutex_enter(&buf
->b_evict_lock
);
4809 released
= (buf
->b_data
!= NULL
&&
4810 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
4811 mutex_exit(&buf
->b_evict_lock
);
4817 arc_referenced(arc_buf_t
*buf
)
4821 mutex_enter(&buf
->b_evict_lock
);
4822 referenced
= (refcount_count(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
4823 mutex_exit(&buf
->b_evict_lock
);
4824 return (referenced
);
4829 arc_write_ready(zio_t
*zio
)
4831 arc_write_callback_t
*callback
= zio
->io_private
;
4832 arc_buf_t
*buf
= callback
->awcb_buf
;
4833 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
4835 ASSERT(HDR_HAS_L1HDR(hdr
));
4836 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
4837 ASSERT(hdr
->b_l1hdr
.b_datacnt
> 0);
4838 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
4841 * If the IO is already in progress, then this is a re-write
4842 * attempt, so we need to thaw and re-compute the cksum.
4843 * It is the responsibility of the callback to handle the
4844 * accounting for any re-write attempt.
4846 if (HDR_IO_IN_PROGRESS(hdr
)) {
4847 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
4848 if (hdr
->b_freeze_cksum
!= NULL
) {
4849 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
4850 hdr
->b_freeze_cksum
= NULL
;
4852 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
4854 arc_cksum_compute(buf
, B_FALSE
);
4855 hdr
->b_flags
|= ARC_FLAG_IO_IN_PROGRESS
;
4859 * The SPA calls this callback for each physical write that happens on behalf
4860 * of a logical write. See the comment in dbuf_write_physdone() for details.
4863 arc_write_physdone(zio_t
*zio
)
4865 arc_write_callback_t
*cb
= zio
->io_private
;
4866 if (cb
->awcb_physdone
!= NULL
)
4867 cb
->awcb_physdone(zio
, cb
->awcb_buf
, cb
->awcb_private
);
4871 arc_write_done(zio_t
*zio
)
4873 arc_write_callback_t
*callback
= zio
->io_private
;
4874 arc_buf_t
*buf
= callback
->awcb_buf
;
4875 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
4877 ASSERT(hdr
->b_l1hdr
.b_acb
== NULL
);
4879 if (zio
->io_error
== 0) {
4880 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
4881 buf_discard_identity(hdr
);
4883 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
4884 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
4887 ASSERT(BUF_EMPTY(hdr
));
4891 * If the block to be written was all-zero or compressed enough to be
4892 * embedded in the BP, no write was performed so there will be no
4893 * dva/birth/checksum. The buffer must therefore remain anonymous
4896 if (!BUF_EMPTY(hdr
)) {
4897 arc_buf_hdr_t
*exists
;
4898 kmutex_t
*hash_lock
;
4900 ASSERT(zio
->io_error
== 0);
4902 arc_cksum_verify(buf
);
4904 exists
= buf_hash_insert(hdr
, &hash_lock
);
4905 if (exists
!= NULL
) {
4907 * This can only happen if we overwrite for
4908 * sync-to-convergence, because we remove
4909 * buffers from the hash table when we arc_free().
4911 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
4912 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
4913 panic("bad overwrite, hdr=%p exists=%p",
4914 (void *)hdr
, (void *)exists
);
4915 ASSERT(refcount_is_zero(
4916 &exists
->b_l1hdr
.b_refcnt
));
4917 arc_change_state(arc_anon
, exists
, hash_lock
);
4918 mutex_exit(hash_lock
);
4919 arc_hdr_destroy(exists
);
4920 exists
= buf_hash_insert(hdr
, &hash_lock
);
4921 ASSERT3P(exists
, ==, NULL
);
4922 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
4924 ASSERT(zio
->io_prop
.zp_nopwrite
);
4925 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
4926 panic("bad nopwrite, hdr=%p exists=%p",
4927 (void *)hdr
, (void *)exists
);
4930 ASSERT(hdr
->b_l1hdr
.b_datacnt
== 1);
4931 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
4932 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
4933 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
4936 hdr
->b_flags
&= ~ARC_FLAG_IO_IN_PROGRESS
;
4937 /* if it's not anon, we are doing a scrub */
4938 if (exists
== NULL
&& hdr
->b_l1hdr
.b_state
== arc_anon
)
4939 arc_access(hdr
, hash_lock
);
4940 mutex_exit(hash_lock
);
4942 hdr
->b_flags
&= ~ARC_FLAG_IO_IN_PROGRESS
;
4945 ASSERT(!refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
4946 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
4948 kmem_free(callback
, sizeof (arc_write_callback_t
));
4952 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
4953 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
, boolean_t l2arc_compress
,
4954 const zio_prop_t
*zp
, arc_done_func_t
*ready
, arc_done_func_t
*physdone
,
4955 arc_done_func_t
*done
, void *private, zio_priority_t priority
,
4956 int zio_flags
, const zbookmark_phys_t
*zb
)
4958 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
4959 arc_write_callback_t
*callback
;
4962 ASSERT(ready
!= NULL
);
4963 ASSERT(done
!= NULL
);
4964 ASSERT(!HDR_IO_ERROR(hdr
));
4965 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
4966 ASSERT(hdr
->b_l1hdr
.b_acb
== NULL
);
4967 ASSERT(hdr
->b_l1hdr
.b_datacnt
> 0);
4969 hdr
->b_flags
|= ARC_FLAG_L2CACHE
;
4971 hdr
->b_flags
|= ARC_FLAG_L2COMPRESS
;
4972 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
4973 callback
->awcb_ready
= ready
;
4974 callback
->awcb_physdone
= physdone
;
4975 callback
->awcb_done
= done
;
4976 callback
->awcb_private
= private;
4977 callback
->awcb_buf
= buf
;
4979 zio
= zio_write(pio
, spa
, txg
, bp
, buf
->b_data
, hdr
->b_size
, zp
,
4980 arc_write_ready
, arc_write_physdone
, arc_write_done
, callback
,
4981 priority
, zio_flags
, zb
);
4987 arc_memory_throttle(uint64_t reserve
, uint64_t txg
)
4990 if (zfs_arc_memory_throttle_disable
)
4993 if (freemem
> physmem
* arc_lotsfree_percent
/ 100)
4996 if (arc_reclaim_needed()) {
4997 /* memory is low, delay before restarting */
4998 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
4999 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
5000 return (SET_ERROR(EAGAIN
));
5007 arc_tempreserve_clear(uint64_t reserve
)
5009 atomic_add_64(&arc_tempreserve
, -reserve
);
5010 ASSERT((int64_t)arc_tempreserve
>= 0);
5014 arc_tempreserve_space(uint64_t reserve
, uint64_t txg
)
5019 if (reserve
> arc_c
/4 && !arc_no_grow
)
5020 arc_c
= MIN(arc_c_max
, reserve
* 4);
5023 * Throttle when the calculated memory footprint for the TXG
5024 * exceeds the target ARC size.
5026 if (reserve
> arc_c
) {
5027 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
5028 return (SET_ERROR(ERESTART
));
5032 * Don't count loaned bufs as in flight dirty data to prevent long
5033 * network delays from blocking transactions that are ready to be
5034 * assigned to a txg.
5036 anon_size
= MAX((int64_t)(arc_anon
->arcs_size
- arc_loaned_bytes
), 0);
5039 * Writes will, almost always, require additional memory allocations
5040 * in order to compress/encrypt/etc the data. We therefore need to
5041 * make sure that there is sufficient available memory for this.
5043 error
= arc_memory_throttle(reserve
, txg
);
5048 * Throttle writes when the amount of dirty data in the cache
5049 * gets too large. We try to keep the cache less than half full
5050 * of dirty blocks so that our sync times don't grow too large.
5051 * Note: if two requests come in concurrently, we might let them
5052 * both succeed, when one of them should fail. Not a huge deal.
5055 if (reserve
+ arc_tempreserve
+ anon_size
> arc_c
/ 2 &&
5056 anon_size
> arc_c
/ 4) {
5057 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
5058 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
5059 arc_tempreserve
>>10,
5060 arc_anon
->arcs_lsize
[ARC_BUFC_METADATA
]>>10,
5061 arc_anon
->arcs_lsize
[ARC_BUFC_DATA
]>>10,
5062 reserve
>>10, arc_c
>>10);
5063 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
5064 return (SET_ERROR(ERESTART
));
5066 atomic_add_64(&arc_tempreserve
, reserve
);
5071 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
5072 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
5074 size
->value
.ui64
= state
->arcs_size
;
5075 evict_data
->value
.ui64
= state
->arcs_lsize
[ARC_BUFC_DATA
];
5076 evict_metadata
->value
.ui64
= state
->arcs_lsize
[ARC_BUFC_METADATA
];
5080 arc_kstat_update(kstat_t
*ksp
, int rw
)
5082 arc_stats_t
*as
= ksp
->ks_data
;
5084 if (rw
== KSTAT_WRITE
) {
5087 arc_kstat_update_state(arc_anon
,
5088 &as
->arcstat_anon_size
,
5089 &as
->arcstat_anon_evictable_data
,
5090 &as
->arcstat_anon_evictable_metadata
);
5091 arc_kstat_update_state(arc_mru
,
5092 &as
->arcstat_mru_size
,
5093 &as
->arcstat_mru_evictable_data
,
5094 &as
->arcstat_mru_evictable_metadata
);
5095 arc_kstat_update_state(arc_mru_ghost
,
5096 &as
->arcstat_mru_ghost_size
,
5097 &as
->arcstat_mru_ghost_evictable_data
,
5098 &as
->arcstat_mru_ghost_evictable_metadata
);
5099 arc_kstat_update_state(arc_mfu
,
5100 &as
->arcstat_mfu_size
,
5101 &as
->arcstat_mfu_evictable_data
,
5102 &as
->arcstat_mfu_evictable_metadata
);
5103 arc_kstat_update_state(arc_mfu_ghost
,
5104 &as
->arcstat_mfu_ghost_size
,
5105 &as
->arcstat_mfu_ghost_evictable_data
,
5106 &as
->arcstat_mfu_ghost_evictable_metadata
);
5113 * This function *must* return indices evenly distributed between all
5114 * sublists of the multilist. This is needed due to how the ARC eviction
5115 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
5116 * distributed between all sublists and uses this assumption when
5117 * deciding which sublist to evict from and how much to evict from it.
5120 arc_state_multilist_index_func(multilist_t
*ml
, void *obj
)
5122 arc_buf_hdr_t
*hdr
= obj
;
5125 * We rely on b_dva to generate evenly distributed index
5126 * numbers using buf_hash below. So, as an added precaution,
5127 * let's make sure we never add empty buffers to the arc lists.
5129 ASSERT(!BUF_EMPTY(hdr
));
5132 * The assumption here, is the hash value for a given
5133 * arc_buf_hdr_t will remain constant throughout its lifetime
5134 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
5135 * Thus, we don't need to store the header's sublist index
5136 * on insertion, as this index can be recalculated on removal.
5138 * Also, the low order bits of the hash value are thought to be
5139 * distributed evenly. Otherwise, in the case that the multilist
5140 * has a power of two number of sublists, each sublists' usage
5141 * would not be evenly distributed.
5143 return (buf_hash(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
) %
5144 multilist_get_num_sublists(ml
));
5148 * Called during module initialization and periodically thereafter to
5149 * apply reasonable changes to the exposed performance tunings. Non-zero
5150 * zfs_* values which differ from the currently set values will be applied.
5153 arc_tuning_update(void)
5155 /* Valid range: 64M - <all physical memory> */
5156 if ((zfs_arc_max
) && (zfs_arc_max
!= arc_c_max
) &&
5157 (zfs_arc_max
> 64 << 20) && (zfs_arc_max
< ptob(physmem
)) &&
5158 (zfs_arc_max
> arc_c_min
)) {
5159 arc_c_max
= zfs_arc_max
;
5161 arc_p
= (arc_c
>> 1);
5162 arc_meta_limit
= MIN(arc_meta_limit
, arc_c_max
);
5165 /* Valid range: 32M - <arc_c_max> */
5166 if ((zfs_arc_min
) && (zfs_arc_min
!= arc_c_min
) &&
5167 (zfs_arc_min
>= 2ULL << SPA_MAXBLOCKSHIFT
) &&
5168 (zfs_arc_min
<= arc_c_max
)) {
5169 arc_c_min
= zfs_arc_min
;
5170 arc_c
= MAX(arc_c
, arc_c_min
);
5173 /* Valid range: 16M - <arc_c_max> */
5174 if ((zfs_arc_meta_min
) && (zfs_arc_meta_min
!= arc_meta_min
) &&
5175 (zfs_arc_meta_min
>= 1ULL << SPA_MAXBLOCKSHIFT
) &&
5176 (zfs_arc_meta_min
<= arc_c_max
)) {
5177 arc_meta_min
= zfs_arc_meta_min
;
5178 arc_meta_limit
= MAX(arc_meta_limit
, arc_meta_min
);
5181 /* Valid range: <arc_meta_min> - <arc_c_max> */
5182 if ((zfs_arc_meta_limit
) && (zfs_arc_meta_limit
!= arc_meta_limit
) &&
5183 (zfs_arc_meta_limit
>= zfs_arc_meta_min
) &&
5184 (zfs_arc_meta_limit
<= arc_c_max
))
5185 arc_meta_limit
= zfs_arc_meta_limit
;
5187 /* Valid range: 1 - N */
5188 if (zfs_arc_grow_retry
)
5189 arc_grow_retry
= zfs_arc_grow_retry
;
5191 /* Valid range: 1 - N */
5192 if (zfs_arc_shrink_shift
) {
5193 arc_shrink_shift
= zfs_arc_shrink_shift
;
5194 arc_no_grow_shift
= MIN(arc_no_grow_shift
, arc_shrink_shift
-1);
5197 /* Valid range: 1 - N ticks */
5198 if (zfs_arc_min_prefetch_lifespan
)
5199 arc_min_prefetch_lifespan
= zfs_arc_min_prefetch_lifespan
;
5206 * allmem is "all memory that we could possibly use".
5209 uint64_t allmem
= ptob(physmem
);
5211 uint64_t allmem
= (physmem
* PAGESIZE
) / 2;
5214 mutex_init(&arc_reclaim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5215 cv_init(&arc_reclaim_thread_cv
, NULL
, CV_DEFAULT
, NULL
);
5216 cv_init(&arc_reclaim_waiters_cv
, NULL
, CV_DEFAULT
, NULL
);
5218 mutex_init(&arc_user_evicts_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5219 cv_init(&arc_user_evicts_cv
, NULL
, CV_DEFAULT
, NULL
);
5221 /* Convert seconds to clock ticks */
5222 arc_min_prefetch_lifespan
= 1 * hz
;
5224 /* Start out with 1/8 of all memory */
5229 * On architectures where the physical memory can be larger
5230 * than the addressable space (intel in 32-bit mode), we may
5231 * need to limit the cache to 1/8 of VM size.
5233 arc_c
= MIN(arc_c
, vmem_size(heap_arena
, VMEM_ALLOC
| VMEM_FREE
) / 8);
5236 * Register a shrinker to support synchronous (direct) memory
5237 * reclaim from the arc. This is done to prevent kswapd from
5238 * swapping out pages when it is preferable to shrink the arc.
5240 spl_register_shrinker(&arc_shrinker
);
5243 /* Set min cache to allow safe operation of arc_adapt() */
5244 arc_c_min
= 2ULL << SPA_MAXBLOCKSHIFT
;
5245 /* Set max to 1/2 of all memory */
5246 arc_c_max
= allmem
/ 2;
5249 arc_p
= (arc_c
>> 1);
5251 /* Set min to 1/2 of arc_c_min */
5252 arc_meta_min
= 1ULL << SPA_MAXBLOCKSHIFT
;
5253 /* Initialize maximum observed usage to zero */
5255 /* Set limit to 3/4 of arc_c_max with a floor of arc_meta_min */
5256 arc_meta_limit
= MAX((3 * arc_c_max
) / 4, arc_meta_min
);
5258 /* Apply user specified tunings */
5259 arc_tuning_update();
5261 if (zfs_arc_num_sublists_per_state
< 1)
5262 zfs_arc_num_sublists_per_state
= MAX(boot_ncpus
, 1);
5264 /* if kmem_flags are set, lets try to use less memory */
5265 if (kmem_debugging())
5267 if (arc_c
< arc_c_min
)
5270 arc_anon
= &ARC_anon
;
5272 arc_mru_ghost
= &ARC_mru_ghost
;
5274 arc_mfu_ghost
= &ARC_mfu_ghost
;
5275 arc_l2c_only
= &ARC_l2c_only
;
5278 multilist_create(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
5279 sizeof (arc_buf_hdr_t
),
5280 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5281 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5282 multilist_create(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
5283 sizeof (arc_buf_hdr_t
),
5284 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5285 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5286 multilist_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
5287 sizeof (arc_buf_hdr_t
),
5288 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5289 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5290 multilist_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
5291 sizeof (arc_buf_hdr_t
),
5292 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5293 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5294 multilist_create(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
5295 sizeof (arc_buf_hdr_t
),
5296 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5297 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5298 multilist_create(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
5299 sizeof (arc_buf_hdr_t
),
5300 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5301 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5302 multilist_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
5303 sizeof (arc_buf_hdr_t
),
5304 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5305 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5306 multilist_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
5307 sizeof (arc_buf_hdr_t
),
5308 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5309 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5310 multilist_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
5311 sizeof (arc_buf_hdr_t
),
5312 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5313 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5314 multilist_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
5315 sizeof (arc_buf_hdr_t
),
5316 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5317 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5319 arc_anon
->arcs_state
= ARC_STATE_ANON
;
5320 arc_mru
->arcs_state
= ARC_STATE_MRU
;
5321 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
5322 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
5323 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
5324 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
5328 arc_reclaim_thread_exit
= FALSE
;
5329 arc_user_evicts_thread_exit
= FALSE
;
5330 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
5331 offsetof(arc_prune_t
, p_node
));
5332 arc_eviction_list
= NULL
;
5333 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
5334 bzero(&arc_eviction_hdr
, sizeof (arc_buf_hdr_t
));
5336 arc_prune_taskq
= taskq_create("arc_prune", max_ncpus
, minclsyspri
,
5337 max_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
5339 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
5340 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
5342 if (arc_ksp
!= NULL
) {
5343 arc_ksp
->ks_data
= &arc_stats
;
5344 arc_ksp
->ks_update
= arc_kstat_update
;
5345 kstat_install(arc_ksp
);
5348 (void) thread_create(NULL
, 0, arc_reclaim_thread
, NULL
, 0, &p0
,
5349 TS_RUN
, minclsyspri
);
5351 (void) thread_create(NULL
, 0, arc_user_evicts_thread
, NULL
, 0, &p0
,
5352 TS_RUN
, minclsyspri
);
5358 * Calculate maximum amount of dirty data per pool.
5360 * If it has been set by a module parameter, take that.
5361 * Otherwise, use a percentage of physical memory defined by
5362 * zfs_dirty_data_max_percent (default 10%) with a cap at
5363 * zfs_dirty_data_max_max (default 25% of physical memory).
5365 if (zfs_dirty_data_max_max
== 0)
5366 zfs_dirty_data_max_max
= physmem
* PAGESIZE
*
5367 zfs_dirty_data_max_max_percent
/ 100;
5369 if (zfs_dirty_data_max
== 0) {
5370 zfs_dirty_data_max
= physmem
* PAGESIZE
*
5371 zfs_dirty_data_max_percent
/ 100;
5372 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
5373 zfs_dirty_data_max_max
);
5383 spl_unregister_shrinker(&arc_shrinker
);
5384 #endif /* _KERNEL */
5386 mutex_enter(&arc_reclaim_lock
);
5387 arc_reclaim_thread_exit
= TRUE
;
5389 * The reclaim thread will set arc_reclaim_thread_exit back to
5390 * FALSE when it is finished exiting; we're waiting for that.
5392 while (arc_reclaim_thread_exit
) {
5393 cv_signal(&arc_reclaim_thread_cv
);
5394 cv_wait(&arc_reclaim_thread_cv
, &arc_reclaim_lock
);
5396 mutex_exit(&arc_reclaim_lock
);
5398 mutex_enter(&arc_user_evicts_lock
);
5399 arc_user_evicts_thread_exit
= TRUE
;
5401 * The user evicts thread will set arc_user_evicts_thread_exit
5402 * to FALSE when it is finished exiting; we're waiting for that.
5404 while (arc_user_evicts_thread_exit
) {
5405 cv_signal(&arc_user_evicts_cv
);
5406 cv_wait(&arc_user_evicts_cv
, &arc_user_evicts_lock
);
5408 mutex_exit(&arc_user_evicts_lock
);
5410 /* Use TRUE to ensure *all* buffers are evicted */
5411 arc_flush(NULL
, TRUE
);
5415 if (arc_ksp
!= NULL
) {
5416 kstat_delete(arc_ksp
);
5420 taskq_wait(arc_prune_taskq
);
5421 taskq_destroy(arc_prune_taskq
);
5423 mutex_enter(&arc_prune_mtx
);
5424 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
5425 list_remove(&arc_prune_list
, p
);
5426 refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
5427 refcount_destroy(&p
->p_refcnt
);
5428 kmem_free(p
, sizeof (*p
));
5430 mutex_exit(&arc_prune_mtx
);
5432 list_destroy(&arc_prune_list
);
5433 mutex_destroy(&arc_prune_mtx
);
5434 mutex_destroy(&arc_reclaim_lock
);
5435 cv_destroy(&arc_reclaim_thread_cv
);
5436 cv_destroy(&arc_reclaim_waiters_cv
);
5438 mutex_destroy(&arc_user_evicts_lock
);
5439 cv_destroy(&arc_user_evicts_cv
);
5441 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
5442 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
5443 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
5444 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
5445 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
5446 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
5447 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
5448 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
5449 multilist_destroy(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]);
5450 multilist_destroy(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]);
5454 ASSERT0(arc_loaned_bytes
);
5460 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
5461 * It uses dedicated storage devices to hold cached data, which are populated
5462 * using large infrequent writes. The main role of this cache is to boost
5463 * the performance of random read workloads. The intended L2ARC devices
5464 * include short-stroked disks, solid state disks, and other media with
5465 * substantially faster read latency than disk.
5467 * +-----------------------+
5469 * +-----------------------+
5472 * l2arc_feed_thread() arc_read()
5476 * +---------------+ |
5478 * +---------------+ |
5483 * +-------+ +-------+
5485 * | cache | | cache |
5486 * +-------+ +-------+
5487 * +=========+ .-----.
5488 * : L2ARC : |-_____-|
5489 * : devices : | Disks |
5490 * +=========+ `-_____-'
5492 * Read requests are satisfied from the following sources, in order:
5495 * 2) vdev cache of L2ARC devices
5497 * 4) vdev cache of disks
5500 * Some L2ARC device types exhibit extremely slow write performance.
5501 * To accommodate for this there are some significant differences between
5502 * the L2ARC and traditional cache design:
5504 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
5505 * the ARC behave as usual, freeing buffers and placing headers on ghost
5506 * lists. The ARC does not send buffers to the L2ARC during eviction as
5507 * this would add inflated write latencies for all ARC memory pressure.
5509 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
5510 * It does this by periodically scanning buffers from the eviction-end of
5511 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
5512 * not already there. It scans until a headroom of buffers is satisfied,
5513 * which itself is a buffer for ARC eviction. If a compressible buffer is
5514 * found during scanning and selected for writing to an L2ARC device, we
5515 * temporarily boost scanning headroom during the next scan cycle to make
5516 * sure we adapt to compression effects (which might significantly reduce
5517 * the data volume we write to L2ARC). The thread that does this is
5518 * l2arc_feed_thread(), illustrated below; example sizes are included to
5519 * provide a better sense of ratio than this diagram:
5522 * +---------------------+----------+
5523 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
5524 * +---------------------+----------+ | o L2ARC eligible
5525 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
5526 * +---------------------+----------+ |
5527 * 15.9 Gbytes ^ 32 Mbytes |
5529 * l2arc_feed_thread()
5531 * l2arc write hand <--[oooo]--'
5535 * +==============================+
5536 * L2ARC dev |####|#|###|###| |####| ... |
5537 * +==============================+
5540 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
5541 * evicted, then the L2ARC has cached a buffer much sooner than it probably
5542 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
5543 * safe to say that this is an uncommon case, since buffers at the end of
5544 * the ARC lists have moved there due to inactivity.
5546 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
5547 * then the L2ARC simply misses copying some buffers. This serves as a
5548 * pressure valve to prevent heavy read workloads from both stalling the ARC
5549 * with waits and clogging the L2ARC with writes. This also helps prevent
5550 * the potential for the L2ARC to churn if it attempts to cache content too
5551 * quickly, such as during backups of the entire pool.
5553 * 5. After system boot and before the ARC has filled main memory, there are
5554 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
5555 * lists can remain mostly static. Instead of searching from tail of these
5556 * lists as pictured, the l2arc_feed_thread() will search from the list heads
5557 * for eligible buffers, greatly increasing its chance of finding them.
5559 * The L2ARC device write speed is also boosted during this time so that
5560 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
5561 * there are no L2ARC reads, and no fear of degrading read performance
5562 * through increased writes.
5564 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
5565 * the vdev queue can aggregate them into larger and fewer writes. Each
5566 * device is written to in a rotor fashion, sweeping writes through
5567 * available space then repeating.
5569 * 7. The L2ARC does not store dirty content. It never needs to flush
5570 * write buffers back to disk based storage.
5572 * 8. If an ARC buffer is written (and dirtied) which also exists in the
5573 * L2ARC, the now stale L2ARC buffer is immediately dropped.
5575 * The performance of the L2ARC can be tweaked by a number of tunables, which
5576 * may be necessary for different workloads:
5578 * l2arc_write_max max write bytes per interval
5579 * l2arc_write_boost extra write bytes during device warmup
5580 * l2arc_noprefetch skip caching prefetched buffers
5581 * l2arc_nocompress skip compressing buffers
5582 * l2arc_headroom number of max device writes to precache
5583 * l2arc_headroom_boost when we find compressed buffers during ARC
5584 * scanning, we multiply headroom by this
5585 * percentage factor for the next scan cycle,
5586 * since more compressed buffers are likely to
5588 * l2arc_feed_secs seconds between L2ARC writing
5590 * Tunables may be removed or added as future performance improvements are
5591 * integrated, and also may become zpool properties.
5593 * There are three key functions that control how the L2ARC warms up:
5595 * l2arc_write_eligible() check if a buffer is eligible to cache
5596 * l2arc_write_size() calculate how much to write
5597 * l2arc_write_interval() calculate sleep delay between writes
5599 * These three functions determine what to write, how much, and how quickly
5604 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*hdr
)
5607 * A buffer is *not* eligible for the L2ARC if it:
5608 * 1. belongs to a different spa.
5609 * 2. is already cached on the L2ARC.
5610 * 3. has an I/O in progress (it may be an incomplete read).
5611 * 4. is flagged not eligible (zfs property).
5613 if (hdr
->b_spa
!= spa_guid
|| HDR_HAS_L2HDR(hdr
) ||
5614 HDR_IO_IN_PROGRESS(hdr
) || !HDR_L2CACHE(hdr
))
5621 l2arc_write_size(void)
5626 * Make sure our globals have meaningful values in case the user
5629 size
= l2arc_write_max
;
5631 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
5632 "be greater than zero, resetting it to the default (%d)",
5634 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
5637 if (arc_warm
== B_FALSE
)
5638 size
+= l2arc_write_boost
;
5645 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
5647 clock_t interval
, next
, now
;
5650 * If the ARC lists are busy, increase our write rate; if the
5651 * lists are stale, idle back. This is achieved by checking
5652 * how much we previously wrote - if it was more than half of
5653 * what we wanted, schedule the next write much sooner.
5655 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
5656 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
5658 interval
= hz
* l2arc_feed_secs
;
5660 now
= ddi_get_lbolt();
5661 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
5667 * Cycle through L2ARC devices. This is how L2ARC load balances.
5668 * If a device is returned, this also returns holding the spa config lock.
5670 static l2arc_dev_t
*
5671 l2arc_dev_get_next(void)
5673 l2arc_dev_t
*first
, *next
= NULL
;
5676 * Lock out the removal of spas (spa_namespace_lock), then removal
5677 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
5678 * both locks will be dropped and a spa config lock held instead.
5680 mutex_enter(&spa_namespace_lock
);
5681 mutex_enter(&l2arc_dev_mtx
);
5683 /* if there are no vdevs, there is nothing to do */
5684 if (l2arc_ndev
== 0)
5688 next
= l2arc_dev_last
;
5690 /* loop around the list looking for a non-faulted vdev */
5692 next
= list_head(l2arc_dev_list
);
5694 next
= list_next(l2arc_dev_list
, next
);
5696 next
= list_head(l2arc_dev_list
);
5699 /* if we have come back to the start, bail out */
5702 else if (next
== first
)
5705 } while (vdev_is_dead(next
->l2ad_vdev
));
5707 /* if we were unable to find any usable vdevs, return NULL */
5708 if (vdev_is_dead(next
->l2ad_vdev
))
5711 l2arc_dev_last
= next
;
5714 mutex_exit(&l2arc_dev_mtx
);
5717 * Grab the config lock to prevent the 'next' device from being
5718 * removed while we are writing to it.
5721 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
5722 mutex_exit(&spa_namespace_lock
);
5728 * Free buffers that were tagged for destruction.
5731 l2arc_do_free_on_write(void)
5734 l2arc_data_free_t
*df
, *df_prev
;
5736 mutex_enter(&l2arc_free_on_write_mtx
);
5737 buflist
= l2arc_free_on_write
;
5739 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
5740 df_prev
= list_prev(buflist
, df
);
5741 ASSERT(df
->l2df_data
!= NULL
);
5742 ASSERT(df
->l2df_func
!= NULL
);
5743 df
->l2df_func(df
->l2df_data
, df
->l2df_size
);
5744 list_remove(buflist
, df
);
5745 kmem_free(df
, sizeof (l2arc_data_free_t
));
5748 mutex_exit(&l2arc_free_on_write_mtx
);
5752 * A write to a cache device has completed. Update all headers to allow
5753 * reads from these buffers to begin.
5756 l2arc_write_done(zio_t
*zio
)
5758 l2arc_write_callback_t
*cb
;
5761 arc_buf_hdr_t
*head
, *hdr
, *hdr_prev
;
5762 kmutex_t
*hash_lock
;
5763 int64_t bytes_dropped
= 0;
5765 cb
= zio
->io_private
;
5767 dev
= cb
->l2wcb_dev
;
5768 ASSERT(dev
!= NULL
);
5769 head
= cb
->l2wcb_head
;
5770 ASSERT(head
!= NULL
);
5771 buflist
= &dev
->l2ad_buflist
;
5772 ASSERT(buflist
!= NULL
);
5773 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
5774 l2arc_write_callback_t
*, cb
);
5776 if (zio
->io_error
!= 0)
5777 ARCSTAT_BUMP(arcstat_l2_writes_error
);
5780 * All writes completed, or an error was hit.
5783 mutex_enter(&dev
->l2ad_mtx
);
5784 for (hdr
= list_prev(buflist
, head
); hdr
; hdr
= hdr_prev
) {
5785 hdr_prev
= list_prev(buflist
, hdr
);
5787 hash_lock
= HDR_LOCK(hdr
);
5790 * We cannot use mutex_enter or else we can deadlock
5791 * with l2arc_write_buffers (due to swapping the order
5792 * the hash lock and l2ad_mtx are taken).
5794 if (!mutex_tryenter(hash_lock
)) {
5796 * Missed the hash lock. We must retry so we
5797 * don't leave the ARC_FLAG_L2_WRITING bit set.
5799 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry
);
5802 * We don't want to rescan the headers we've
5803 * already marked as having been written out, so
5804 * we reinsert the head node so we can pick up
5805 * where we left off.
5807 list_remove(buflist
, head
);
5808 list_insert_after(buflist
, hdr
, head
);
5810 mutex_exit(&dev
->l2ad_mtx
);
5813 * We wait for the hash lock to become available
5814 * to try and prevent busy waiting, and increase
5815 * the chance we'll be able to acquire the lock
5816 * the next time around.
5818 mutex_enter(hash_lock
);
5819 mutex_exit(hash_lock
);
5824 * We could not have been moved into the arc_l2c_only
5825 * state while in-flight due to our ARC_FLAG_L2_WRITING
5826 * bit being set. Let's just ensure that's being enforced.
5828 ASSERT(HDR_HAS_L1HDR(hdr
));
5831 * We may have allocated a buffer for L2ARC compression,
5832 * we must release it to avoid leaking this data.
5834 l2arc_release_cdata_buf(hdr
);
5836 if (zio
->io_error
!= 0) {
5838 * Error - drop L2ARC entry.
5840 list_remove(buflist
, hdr
);
5841 hdr
->b_flags
&= ~ARC_FLAG_HAS_L2HDR
;
5843 ARCSTAT_INCR(arcstat_l2_asize
, -hdr
->b_l2hdr
.b_asize
);
5844 ARCSTAT_INCR(arcstat_l2_size
, -hdr
->b_size
);
5846 bytes_dropped
+= hdr
->b_l2hdr
.b_asize
;
5847 (void) refcount_remove_many(&dev
->l2ad_alloc
,
5848 hdr
->b_l2hdr
.b_asize
, hdr
);
5852 * Allow ARC to begin reads and ghost list evictions to
5855 hdr
->b_flags
&= ~ARC_FLAG_L2_WRITING
;
5857 mutex_exit(hash_lock
);
5860 atomic_inc_64(&l2arc_writes_done
);
5861 list_remove(buflist
, head
);
5862 ASSERT(!HDR_HAS_L1HDR(head
));
5863 kmem_cache_free(hdr_l2only_cache
, head
);
5864 mutex_exit(&dev
->l2ad_mtx
);
5866 vdev_space_update(dev
->l2ad_vdev
, -bytes_dropped
, 0, 0);
5868 l2arc_do_free_on_write();
5870 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
5874 * A read to a cache device completed. Validate buffer contents before
5875 * handing over to the regular ARC routines.
5878 l2arc_read_done(zio_t
*zio
)
5880 l2arc_read_callback_t
*cb
;
5883 kmutex_t
*hash_lock
;
5886 ASSERT(zio
->io_vd
!= NULL
);
5887 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
5889 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
5891 cb
= zio
->io_private
;
5893 buf
= cb
->l2rcb_buf
;
5894 ASSERT(buf
!= NULL
);
5896 hash_lock
= HDR_LOCK(buf
->b_hdr
);
5897 mutex_enter(hash_lock
);
5899 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
5902 * If the buffer was compressed, decompress it first.
5904 if (cb
->l2rcb_compress
!= ZIO_COMPRESS_OFF
)
5905 l2arc_decompress_zio(zio
, hdr
, cb
->l2rcb_compress
);
5906 ASSERT(zio
->io_data
!= NULL
);
5909 * Check this survived the L2ARC journey.
5911 equal
= arc_cksum_equal(buf
);
5912 if (equal
&& zio
->io_error
== 0 && !HDR_L2_EVICTED(hdr
)) {
5913 mutex_exit(hash_lock
);
5914 zio
->io_private
= buf
;
5915 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
5916 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
5919 mutex_exit(hash_lock
);
5921 * Buffer didn't survive caching. Increment stats and
5922 * reissue to the original storage device.
5924 if (zio
->io_error
!= 0) {
5925 ARCSTAT_BUMP(arcstat_l2_io_error
);
5927 zio
->io_error
= SET_ERROR(EIO
);
5930 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
5933 * If there's no waiter, issue an async i/o to the primary
5934 * storage now. If there *is* a waiter, the caller must
5935 * issue the i/o in a context where it's OK to block.
5937 if (zio
->io_waiter
== NULL
) {
5938 zio_t
*pio
= zio_unique_parent(zio
);
5940 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
5942 zio_nowait(zio_read(pio
, cb
->l2rcb_spa
, &cb
->l2rcb_bp
,
5943 buf
->b_data
, zio
->io_size
, arc_read_done
, buf
,
5944 zio
->io_priority
, cb
->l2rcb_flags
, &cb
->l2rcb_zb
));
5948 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
5952 * This is the list priority from which the L2ARC will search for pages to
5953 * cache. This is used within loops (0..3) to cycle through lists in the
5954 * desired order. This order can have a significant effect on cache
5957 * Currently the metadata lists are hit first, MFU then MRU, followed by
5958 * the data lists. This function returns a locked list, and also returns
5961 static multilist_sublist_t
*
5962 l2arc_sublist_lock(int list_num
)
5964 multilist_t
*ml
= NULL
;
5967 ASSERT(list_num
>= 0 && list_num
<= 3);
5971 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
5974 ml
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
5977 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
5980 ml
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
5985 * Return a randomly-selected sublist. This is acceptable
5986 * because the caller feeds only a little bit of data for each
5987 * call (8MB). Subsequent calls will result in different
5988 * sublists being selected.
5990 idx
= multilist_get_random_index(ml
);
5991 return (multilist_sublist_lock(ml
, idx
));
5995 * Evict buffers from the device write hand to the distance specified in
5996 * bytes. This distance may span populated buffers, it may span nothing.
5997 * This is clearing a region on the L2ARC device ready for writing.
5998 * If the 'all' boolean is set, every buffer is evicted.
6001 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
6004 arc_buf_hdr_t
*hdr
, *hdr_prev
;
6005 kmutex_t
*hash_lock
;
6008 buflist
= &dev
->l2ad_buflist
;
6010 if (!all
&& dev
->l2ad_first
) {
6012 * This is the first sweep through the device. There is
6018 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
6020 * When nearing the end of the device, evict to the end
6021 * before the device write hand jumps to the start.
6023 taddr
= dev
->l2ad_end
;
6025 taddr
= dev
->l2ad_hand
+ distance
;
6027 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
6028 uint64_t, taddr
, boolean_t
, all
);
6031 mutex_enter(&dev
->l2ad_mtx
);
6032 for (hdr
= list_tail(buflist
); hdr
; hdr
= hdr_prev
) {
6033 hdr_prev
= list_prev(buflist
, hdr
);
6035 hash_lock
= HDR_LOCK(hdr
);
6038 * We cannot use mutex_enter or else we can deadlock
6039 * with l2arc_write_buffers (due to swapping the order
6040 * the hash lock and l2ad_mtx are taken).
6042 if (!mutex_tryenter(hash_lock
)) {
6044 * Missed the hash lock. Retry.
6046 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
6047 mutex_exit(&dev
->l2ad_mtx
);
6048 mutex_enter(hash_lock
);
6049 mutex_exit(hash_lock
);
6053 if (HDR_L2_WRITE_HEAD(hdr
)) {
6055 * We hit a write head node. Leave it for
6056 * l2arc_write_done().
6058 list_remove(buflist
, hdr
);
6059 mutex_exit(hash_lock
);
6063 if (!all
&& HDR_HAS_L2HDR(hdr
) &&
6064 (hdr
->b_l2hdr
.b_daddr
> taddr
||
6065 hdr
->b_l2hdr
.b_daddr
< dev
->l2ad_hand
)) {
6067 * We've evicted to the target address,
6068 * or the end of the device.
6070 mutex_exit(hash_lock
);
6074 ASSERT(HDR_HAS_L2HDR(hdr
));
6075 if (!HDR_HAS_L1HDR(hdr
)) {
6076 ASSERT(!HDR_L2_READING(hdr
));
6078 * This doesn't exist in the ARC. Destroy.
6079 * arc_hdr_destroy() will call list_remove()
6080 * and decrement arcstat_l2_size.
6082 arc_change_state(arc_anon
, hdr
, hash_lock
);
6083 arc_hdr_destroy(hdr
);
6085 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_l2c_only
);
6086 ARCSTAT_BUMP(arcstat_l2_evict_l1cached
);
6088 * Invalidate issued or about to be issued
6089 * reads, since we may be about to write
6090 * over this location.
6092 if (HDR_L2_READING(hdr
)) {
6093 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
6094 hdr
->b_flags
|= ARC_FLAG_L2_EVICTED
;
6097 /* Ensure this header has finished being written */
6098 ASSERT(!HDR_L2_WRITING(hdr
));
6099 ASSERT3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
6101 arc_hdr_l2hdr_destroy(hdr
);
6103 mutex_exit(hash_lock
);
6105 mutex_exit(&dev
->l2ad_mtx
);
6109 * Find and write ARC buffers to the L2ARC device.
6111 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
6112 * for reading until they have completed writing.
6113 * The headroom_boost is an in-out parameter used to maintain headroom boost
6114 * state between calls to this function.
6116 * Returns the number of bytes actually written (which may be smaller than
6117 * the delta by which the device hand has changed due to alignment).
6120 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
,
6121 boolean_t
*headroom_boost
)
6123 arc_buf_hdr_t
*hdr
, *hdr_prev
, *head
;
6124 uint64_t write_asize
, write_sz
, headroom
, buf_compress_minsz
,
6128 l2arc_write_callback_t
*cb
;
6130 uint64_t guid
= spa_load_guid(spa
);
6132 const boolean_t do_headroom_boost
= *headroom_boost
;
6134 ASSERT(dev
->l2ad_vdev
!= NULL
);
6136 /* Lower the flag now, we might want to raise it again later. */
6137 *headroom_boost
= B_FALSE
;
6140 write_sz
= write_asize
= 0;
6142 head
= kmem_cache_alloc(hdr_l2only_cache
, KM_PUSHPAGE
);
6143 head
->b_flags
|= ARC_FLAG_L2_WRITE_HEAD
;
6144 head
->b_flags
|= ARC_FLAG_HAS_L2HDR
;
6147 * We will want to try to compress buffers that are at least 2x the
6148 * device sector size.
6150 buf_compress_minsz
= 2 << dev
->l2ad_vdev
->vdev_ashift
;
6153 * Copy buffers for L2ARC writing.
6155 for (try = 0; try <= 3; try++) {
6156 multilist_sublist_t
*mls
= l2arc_sublist_lock(try);
6157 uint64_t passed_sz
= 0;
6160 * L2ARC fast warmup.
6162 * Until the ARC is warm and starts to evict, read from the
6163 * head of the ARC lists rather than the tail.
6165 if (arc_warm
== B_FALSE
)
6166 hdr
= multilist_sublist_head(mls
);
6168 hdr
= multilist_sublist_tail(mls
);
6170 headroom
= target_sz
* l2arc_headroom
;
6171 if (do_headroom_boost
)
6172 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
6174 for (; hdr
; hdr
= hdr_prev
) {
6175 kmutex_t
*hash_lock
;
6179 if (arc_warm
== B_FALSE
)
6180 hdr_prev
= multilist_sublist_next(mls
, hdr
);
6182 hdr_prev
= multilist_sublist_prev(mls
, hdr
);
6184 hash_lock
= HDR_LOCK(hdr
);
6185 if (!mutex_tryenter(hash_lock
)) {
6187 * Skip this buffer rather than waiting.
6192 passed_sz
+= hdr
->b_size
;
6193 if (passed_sz
> headroom
) {
6197 mutex_exit(hash_lock
);
6201 if (!l2arc_write_eligible(guid
, hdr
)) {
6202 mutex_exit(hash_lock
);
6207 * Assume that the buffer is not going to be compressed
6208 * and could take more space on disk because of a larger
6211 buf_sz
= hdr
->b_size
;
6212 buf_a_sz
= vdev_psize_to_asize(dev
->l2ad_vdev
, buf_sz
);
6214 if ((write_asize
+ buf_a_sz
) > target_sz
) {
6216 mutex_exit(hash_lock
);
6222 * Insert a dummy header on the buflist so
6223 * l2arc_write_done() can find where the
6224 * write buffers begin without searching.
6226 mutex_enter(&dev
->l2ad_mtx
);
6227 list_insert_head(&dev
->l2ad_buflist
, head
);
6228 mutex_exit(&dev
->l2ad_mtx
);
6230 cb
= kmem_alloc(sizeof (l2arc_write_callback_t
),
6232 cb
->l2wcb_dev
= dev
;
6233 cb
->l2wcb_head
= head
;
6234 pio
= zio_root(spa
, l2arc_write_done
, cb
,
6239 * Create and add a new L2ARC header.
6241 hdr
->b_l2hdr
.b_dev
= dev
;
6242 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
6243 hdr
->b_flags
|= ARC_FLAG_L2_WRITING
;
6245 * Temporarily stash the data buffer in b_tmp_cdata.
6246 * The subsequent write step will pick it up from
6247 * there. This is because can't access b_l1hdr.b_buf
6248 * without holding the hash_lock, which we in turn
6249 * can't access without holding the ARC list locks
6250 * (which we want to avoid during compression/writing)
6252 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_OFF
);
6253 hdr
->b_l2hdr
.b_asize
= hdr
->b_size
;
6254 hdr
->b_l2hdr
.b_hits
= 0;
6255 hdr
->b_l1hdr
.b_tmp_cdata
= hdr
->b_l1hdr
.b_buf
->b_data
;
6258 * Explicitly set the b_daddr field to a known
6259 * value which means "invalid address". This
6260 * enables us to differentiate which stage of
6261 * l2arc_write_buffers() the particular header
6262 * is in (e.g. this loop, or the one below).
6263 * ARC_FLAG_L2_WRITING is not enough to make
6264 * this distinction, and we need to know in
6265 * order to do proper l2arc vdev accounting in
6266 * arc_release() and arc_hdr_destroy().
6268 * Note, we can't use a new flag to distinguish
6269 * the two stages because we don't hold the
6270 * header's hash_lock below, in the second stage
6271 * of this function. Thus, we can't simply
6272 * change the b_flags field to denote that the
6273 * IO has been sent. We can change the b_daddr
6274 * field of the L2 portion, though, since we'll
6275 * be holding the l2ad_mtx; which is why we're
6276 * using it to denote the header's state change.
6278 hdr
->b_l2hdr
.b_daddr
= L2ARC_ADDR_UNSET
;
6279 hdr
->b_flags
|= ARC_FLAG_HAS_L2HDR
;
6281 mutex_enter(&dev
->l2ad_mtx
);
6282 list_insert_head(&dev
->l2ad_buflist
, hdr
);
6283 mutex_exit(&dev
->l2ad_mtx
);
6286 * Compute and store the buffer cksum before
6287 * writing. On debug the cksum is verified first.
6289 arc_cksum_verify(hdr
->b_l1hdr
.b_buf
);
6290 arc_cksum_compute(hdr
->b_l1hdr
.b_buf
, B_TRUE
);
6292 mutex_exit(hash_lock
);
6295 write_asize
+= buf_a_sz
;
6298 multilist_sublist_unlock(mls
);
6304 /* No buffers selected for writing? */
6307 ASSERT(!HDR_HAS_L1HDR(head
));
6308 kmem_cache_free(hdr_l2only_cache
, head
);
6312 mutex_enter(&dev
->l2ad_mtx
);
6315 * Note that elsewhere in this file arcstat_l2_asize
6316 * and the used space on l2ad_vdev are updated using b_asize,
6317 * which is not necessarily rounded up to the device block size.
6318 * Too keep accounting consistent we do the same here as well:
6319 * stats_size accumulates the sum of b_asize of the written buffers,
6320 * while write_asize accumulates the sum of b_asize rounded up
6321 * to the device block size.
6322 * The latter sum is used only to validate the corectness of the code.
6328 * Now start writing the buffers. We're starting at the write head
6329 * and work backwards, retracing the course of the buffer selector
6332 for (hdr
= list_prev(&dev
->l2ad_buflist
, head
); hdr
;
6333 hdr
= list_prev(&dev
->l2ad_buflist
, hdr
)) {
6337 * We rely on the L1 portion of the header below, so
6338 * it's invalid for this header to have been evicted out
6339 * of the ghost cache, prior to being written out. The
6340 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
6342 ASSERT(HDR_HAS_L1HDR(hdr
));
6345 * We shouldn't need to lock the buffer here, since we flagged
6346 * it as ARC_FLAG_L2_WRITING in the previous step, but we must
6347 * take care to only access its L2 cache parameters. In
6348 * particular, hdr->l1hdr.b_buf may be invalid by now due to
6351 hdr
->b_l2hdr
.b_daddr
= dev
->l2ad_hand
;
6353 if ((!l2arc_nocompress
&& HDR_L2COMPRESS(hdr
)) &&
6354 hdr
->b_l2hdr
.b_asize
>= buf_compress_minsz
) {
6355 if (l2arc_compress_buf(hdr
)) {
6357 * If compression succeeded, enable headroom
6358 * boost on the next scan cycle.
6360 *headroom_boost
= B_TRUE
;
6365 * Pick up the buffer data we had previously stashed away
6366 * (and now potentially also compressed).
6368 buf_data
= hdr
->b_l1hdr
.b_tmp_cdata
;
6369 buf_sz
= hdr
->b_l2hdr
.b_asize
;
6372 * We need to do this regardless if buf_sz is zero or
6373 * not, otherwise, when this l2hdr is evicted we'll
6374 * remove a reference that was never added.
6376 (void) refcount_add_many(&dev
->l2ad_alloc
, buf_sz
, hdr
);
6378 /* Compression may have squashed the buffer to zero length. */
6382 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
6383 dev
->l2ad_hand
, buf_sz
, buf_data
, ZIO_CHECKSUM_OFF
,
6384 NULL
, NULL
, ZIO_PRIORITY_ASYNC_WRITE
,
6385 ZIO_FLAG_CANFAIL
, B_FALSE
);
6387 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
6389 (void) zio_nowait(wzio
);
6391 stats_size
+= buf_sz
;
6394 * Keep the clock hand suitably device-aligned.
6396 buf_a_sz
= vdev_psize_to_asize(dev
->l2ad_vdev
, buf_sz
);
6397 write_asize
+= buf_a_sz
;
6398 dev
->l2ad_hand
+= buf_a_sz
;
6402 mutex_exit(&dev
->l2ad_mtx
);
6404 ASSERT3U(write_asize
, <=, target_sz
);
6405 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
6406 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_asize
);
6407 ARCSTAT_INCR(arcstat_l2_size
, write_sz
);
6408 ARCSTAT_INCR(arcstat_l2_asize
, stats_size
);
6409 vdev_space_update(dev
->l2ad_vdev
, stats_size
, 0, 0);
6412 * Bump device hand to the device start if it is approaching the end.
6413 * l2arc_evict() will already have evicted ahead for this case.
6415 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
6416 dev
->l2ad_hand
= dev
->l2ad_start
;
6417 dev
->l2ad_first
= B_FALSE
;
6420 dev
->l2ad_writing
= B_TRUE
;
6421 (void) zio_wait(pio
);
6422 dev
->l2ad_writing
= B_FALSE
;
6424 return (write_asize
);
6428 * Compresses an L2ARC buffer.
6429 * The data to be compressed must be prefilled in l1hdr.b_tmp_cdata and its
6430 * size in l2hdr->b_asize. This routine tries to compress the data and
6431 * depending on the compression result there are three possible outcomes:
6432 * *) The buffer was incompressible. The original l2hdr contents were left
6433 * untouched and are ready for writing to an L2 device.
6434 * *) The buffer was all-zeros, so there is no need to write it to an L2
6435 * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is
6436 * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY.
6437 * *) Compression succeeded and b_tmp_cdata was replaced with a temporary
6438 * data buffer which holds the compressed data to be written, and b_asize
6439 * tells us how much data there is. b_compress is set to the appropriate
6440 * compression algorithm. Once writing is done, invoke
6441 * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer.
6443 * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the
6444 * buffer was incompressible).
6447 l2arc_compress_buf(arc_buf_hdr_t
*hdr
)
6450 size_t csize
, len
, rounded
;
6451 l2arc_buf_hdr_t
*l2hdr
;
6453 ASSERT(HDR_HAS_L2HDR(hdr
));
6455 l2hdr
= &hdr
->b_l2hdr
;
6457 ASSERT(HDR_HAS_L1HDR(hdr
));
6458 ASSERT(HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
);
6459 ASSERT(hdr
->b_l1hdr
.b_tmp_cdata
!= NULL
);
6461 len
= l2hdr
->b_asize
;
6462 cdata
= zio_data_buf_alloc(len
);
6463 ASSERT3P(cdata
, !=, NULL
);
6464 csize
= zio_compress_data(ZIO_COMPRESS_LZ4
, hdr
->b_l1hdr
.b_tmp_cdata
,
6465 cdata
, l2hdr
->b_asize
);
6467 rounded
= P2ROUNDUP(csize
, (size_t)SPA_MINBLOCKSIZE
);
6468 if (rounded
> csize
) {
6469 bzero((char *)cdata
+ csize
, rounded
- csize
);
6474 /* zero block, indicate that there's nothing to write */
6475 zio_data_buf_free(cdata
, len
);
6476 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_EMPTY
);
6478 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
6479 ARCSTAT_BUMP(arcstat_l2_compress_zeros
);
6481 } else if (csize
> 0 && csize
< len
) {
6483 * Compression succeeded, we'll keep the cdata around for
6484 * writing and release it afterwards.
6486 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_LZ4
);
6487 l2hdr
->b_asize
= csize
;
6488 hdr
->b_l1hdr
.b_tmp_cdata
= cdata
;
6489 ARCSTAT_BUMP(arcstat_l2_compress_successes
);
6493 * Compression failed, release the compressed buffer.
6494 * l2hdr will be left unmodified.
6496 zio_data_buf_free(cdata
, len
);
6497 ARCSTAT_BUMP(arcstat_l2_compress_failures
);
6503 * Decompresses a zio read back from an l2arc device. On success, the
6504 * underlying zio's io_data buffer is overwritten by the uncompressed
6505 * version. On decompression error (corrupt compressed stream), the
6506 * zio->io_error value is set to signal an I/O error.
6508 * Please note that the compressed data stream is not checksummed, so
6509 * if the underlying device is experiencing data corruption, we may feed
6510 * corrupt data to the decompressor, so the decompressor needs to be
6511 * able to handle this situation (LZ4 does).
6514 l2arc_decompress_zio(zio_t
*zio
, arc_buf_hdr_t
*hdr
, enum zio_compress c
)
6519 ASSERT(L2ARC_IS_VALID_COMPRESS(c
));
6521 if (zio
->io_error
!= 0) {
6523 * An io error has occured, just restore the original io
6524 * size in preparation for a main pool read.
6526 zio
->io_orig_size
= zio
->io_size
= hdr
->b_size
;
6530 if (c
== ZIO_COMPRESS_EMPTY
) {
6532 * An empty buffer results in a null zio, which means we
6533 * need to fill its io_data after we're done restoring the
6534 * buffer's contents.
6536 ASSERT(hdr
->b_l1hdr
.b_buf
!= NULL
);
6537 bzero(hdr
->b_l1hdr
.b_buf
->b_data
, hdr
->b_size
);
6538 zio
->io_data
= zio
->io_orig_data
= hdr
->b_l1hdr
.b_buf
->b_data
;
6540 ASSERT(zio
->io_data
!= NULL
);
6542 * We copy the compressed data from the start of the arc buffer
6543 * (the zio_read will have pulled in only what we need, the
6544 * rest is garbage which we will overwrite at decompression)
6545 * and then decompress back to the ARC data buffer. This way we
6546 * can minimize copying by simply decompressing back over the
6547 * original compressed data (rather than decompressing to an
6548 * aux buffer and then copying back the uncompressed buffer,
6549 * which is likely to be much larger).
6551 csize
= zio
->io_size
;
6552 cdata
= zio_data_buf_alloc(csize
);
6553 bcopy(zio
->io_data
, cdata
, csize
);
6554 if (zio_decompress_data(c
, cdata
, zio
->io_data
, csize
,
6556 zio
->io_error
= SET_ERROR(EIO
);
6557 zio_data_buf_free(cdata
, csize
);
6560 /* Restore the expected uncompressed IO size. */
6561 zio
->io_orig_size
= zio
->io_size
= hdr
->b_size
;
6565 * Releases the temporary b_tmp_cdata buffer in an l2arc header structure.
6566 * This buffer serves as a temporary holder of compressed data while
6567 * the buffer entry is being written to an l2arc device. Once that is
6568 * done, we can dispose of it.
6571 l2arc_release_cdata_buf(arc_buf_hdr_t
*hdr
)
6573 enum zio_compress comp
= HDR_GET_COMPRESS(hdr
);
6575 ASSERT(HDR_HAS_L1HDR(hdr
));
6576 ASSERT(comp
== ZIO_COMPRESS_OFF
|| L2ARC_IS_VALID_COMPRESS(comp
));
6578 if (comp
== ZIO_COMPRESS_OFF
) {
6580 * In this case, b_tmp_cdata points to the same buffer
6581 * as the arc_buf_t's b_data field. We don't want to
6582 * free it, since the arc_buf_t will handle that.
6584 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
6585 } else if (comp
== ZIO_COMPRESS_EMPTY
) {
6587 * In this case, b_tmp_cdata was compressed to an empty
6588 * buffer, thus there's nothing to free and b_tmp_cdata
6589 * should have been set to NULL in l2arc_write_buffers().
6591 ASSERT3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
6594 * If the data was compressed, then we've allocated a
6595 * temporary buffer for it, so now we need to release it.
6597 ASSERT(hdr
->b_l1hdr
.b_tmp_cdata
!= NULL
);
6598 zio_data_buf_free(hdr
->b_l1hdr
.b_tmp_cdata
,
6600 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
6606 * This thread feeds the L2ARC at regular intervals. This is the beating
6607 * heart of the L2ARC.
6610 l2arc_feed_thread(void)
6615 uint64_t size
, wrote
;
6616 clock_t begin
, next
= ddi_get_lbolt();
6617 boolean_t headroom_boost
= B_FALSE
;
6618 fstrans_cookie_t cookie
;
6620 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
6622 mutex_enter(&l2arc_feed_thr_lock
);
6624 cookie
= spl_fstrans_mark();
6625 while (l2arc_thread_exit
== 0) {
6626 CALLB_CPR_SAFE_BEGIN(&cpr
);
6627 (void) cv_timedwait_sig(&l2arc_feed_thr_cv
,
6628 &l2arc_feed_thr_lock
, next
);
6629 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
6630 next
= ddi_get_lbolt() + hz
;
6633 * Quick check for L2ARC devices.
6635 mutex_enter(&l2arc_dev_mtx
);
6636 if (l2arc_ndev
== 0) {
6637 mutex_exit(&l2arc_dev_mtx
);
6640 mutex_exit(&l2arc_dev_mtx
);
6641 begin
= ddi_get_lbolt();
6644 * This selects the next l2arc device to write to, and in
6645 * doing so the next spa to feed from: dev->l2ad_spa. This
6646 * will return NULL if there are now no l2arc devices or if
6647 * they are all faulted.
6649 * If a device is returned, its spa's config lock is also
6650 * held to prevent device removal. l2arc_dev_get_next()
6651 * will grab and release l2arc_dev_mtx.
6653 if ((dev
= l2arc_dev_get_next()) == NULL
)
6656 spa
= dev
->l2ad_spa
;
6657 ASSERT(spa
!= NULL
);
6660 * If the pool is read-only then force the feed thread to
6661 * sleep a little longer.
6663 if (!spa_writeable(spa
)) {
6664 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
6665 spa_config_exit(spa
, SCL_L2ARC
, dev
);
6670 * Avoid contributing to memory pressure.
6672 if (arc_reclaim_needed()) {
6673 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
6674 spa_config_exit(spa
, SCL_L2ARC
, dev
);
6678 ARCSTAT_BUMP(arcstat_l2_feeds
);
6680 size
= l2arc_write_size();
6683 * Evict L2ARC buffers that will be overwritten.
6685 l2arc_evict(dev
, size
, B_FALSE
);
6688 * Write ARC buffers.
6690 wrote
= l2arc_write_buffers(spa
, dev
, size
, &headroom_boost
);
6693 * Calculate interval between writes.
6695 next
= l2arc_write_interval(begin
, size
, wrote
);
6696 spa_config_exit(spa
, SCL_L2ARC
, dev
);
6698 spl_fstrans_unmark(cookie
);
6700 l2arc_thread_exit
= 0;
6701 cv_broadcast(&l2arc_feed_thr_cv
);
6702 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
6707 l2arc_vdev_present(vdev_t
*vd
)
6711 mutex_enter(&l2arc_dev_mtx
);
6712 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
6713 dev
= list_next(l2arc_dev_list
, dev
)) {
6714 if (dev
->l2ad_vdev
== vd
)
6717 mutex_exit(&l2arc_dev_mtx
);
6719 return (dev
!= NULL
);
6723 * Add a vdev for use by the L2ARC. By this point the spa has already
6724 * validated the vdev and opened it.
6727 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
6729 l2arc_dev_t
*adddev
;
6731 ASSERT(!l2arc_vdev_present(vd
));
6734 * Create a new l2arc device entry.
6736 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
6737 adddev
->l2ad_spa
= spa
;
6738 adddev
->l2ad_vdev
= vd
;
6739 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
6740 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
6741 adddev
->l2ad_hand
= adddev
->l2ad_start
;
6742 adddev
->l2ad_first
= B_TRUE
;
6743 adddev
->l2ad_writing
= B_FALSE
;
6744 list_link_init(&adddev
->l2ad_node
);
6746 mutex_init(&adddev
->l2ad_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
6748 * This is a list of all ARC buffers that are still valid on the
6751 list_create(&adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
6752 offsetof(arc_buf_hdr_t
, b_l2hdr
.b_l2node
));
6754 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
6755 refcount_create(&adddev
->l2ad_alloc
);
6758 * Add device to global list
6760 mutex_enter(&l2arc_dev_mtx
);
6761 list_insert_head(l2arc_dev_list
, adddev
);
6762 atomic_inc_64(&l2arc_ndev
);
6763 mutex_exit(&l2arc_dev_mtx
);
6767 * Remove a vdev from the L2ARC.
6770 l2arc_remove_vdev(vdev_t
*vd
)
6772 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
6775 * Find the device by vdev
6777 mutex_enter(&l2arc_dev_mtx
);
6778 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
6779 nextdev
= list_next(l2arc_dev_list
, dev
);
6780 if (vd
== dev
->l2ad_vdev
) {
6785 ASSERT(remdev
!= NULL
);
6788 * Remove device from global list
6790 list_remove(l2arc_dev_list
, remdev
);
6791 l2arc_dev_last
= NULL
; /* may have been invalidated */
6792 atomic_dec_64(&l2arc_ndev
);
6793 mutex_exit(&l2arc_dev_mtx
);
6796 * Clear all buflists and ARC references. L2ARC device flush.
6798 l2arc_evict(remdev
, 0, B_TRUE
);
6799 list_destroy(&remdev
->l2ad_buflist
);
6800 mutex_destroy(&remdev
->l2ad_mtx
);
6801 refcount_destroy(&remdev
->l2ad_alloc
);
6802 kmem_free(remdev
, sizeof (l2arc_dev_t
));
6808 l2arc_thread_exit
= 0;
6810 l2arc_writes_sent
= 0;
6811 l2arc_writes_done
= 0;
6813 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6814 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
6815 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
6816 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
6818 l2arc_dev_list
= &L2ARC_dev_list
;
6819 l2arc_free_on_write
= &L2ARC_free_on_write
;
6820 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
6821 offsetof(l2arc_dev_t
, l2ad_node
));
6822 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
6823 offsetof(l2arc_data_free_t
, l2df_list_node
));
6830 * This is called from dmu_fini(), which is called from spa_fini();
6831 * Because of this, we can assume that all l2arc devices have
6832 * already been removed when the pools themselves were removed.
6835 l2arc_do_free_on_write();
6837 mutex_destroy(&l2arc_feed_thr_lock
);
6838 cv_destroy(&l2arc_feed_thr_cv
);
6839 mutex_destroy(&l2arc_dev_mtx
);
6840 mutex_destroy(&l2arc_free_on_write_mtx
);
6842 list_destroy(l2arc_dev_list
);
6843 list_destroy(l2arc_free_on_write
);
6849 if (!(spa_mode_global
& FWRITE
))
6852 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
6853 TS_RUN
, minclsyspri
);
6859 if (!(spa_mode_global
& FWRITE
))
6862 mutex_enter(&l2arc_feed_thr_lock
);
6863 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
6864 l2arc_thread_exit
= 1;
6865 while (l2arc_thread_exit
!= 0)
6866 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
6867 mutex_exit(&l2arc_feed_thr_lock
);
6870 #if defined(_KERNEL) && defined(HAVE_SPL)
6871 EXPORT_SYMBOL(arc_buf_size
);
6872 EXPORT_SYMBOL(arc_write
);
6873 EXPORT_SYMBOL(arc_read
);
6874 EXPORT_SYMBOL(arc_buf_remove_ref
);
6875 EXPORT_SYMBOL(arc_buf_info
);
6876 EXPORT_SYMBOL(arc_getbuf_func
);
6877 EXPORT_SYMBOL(arc_add_prune_callback
);
6878 EXPORT_SYMBOL(arc_remove_prune_callback
);
6880 module_param(zfs_arc_min
, ulong
, 0644);
6881 MODULE_PARM_DESC(zfs_arc_min
, "Min arc size");
6883 module_param(zfs_arc_max
, ulong
, 0644);
6884 MODULE_PARM_DESC(zfs_arc_max
, "Max arc size");
6886 module_param(zfs_arc_meta_limit
, ulong
, 0644);
6887 MODULE_PARM_DESC(zfs_arc_meta_limit
, "Meta limit for arc size");
6889 module_param(zfs_arc_meta_min
, ulong
, 0644);
6890 MODULE_PARM_DESC(zfs_arc_meta_min
, "Min arc metadata");
6892 module_param(zfs_arc_meta_prune
, int, 0644);
6893 MODULE_PARM_DESC(zfs_arc_meta_prune
, "Meta objects to scan for prune");
6895 module_param(zfs_arc_meta_adjust_restarts
, int, 0644);
6896 MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts
,
6897 "Limit number of restarts in arc_adjust_meta");
6899 module_param(zfs_arc_meta_strategy
, int, 0644);
6900 MODULE_PARM_DESC(zfs_arc_meta_strategy
, "Meta reclaim strategy");
6902 module_param(zfs_arc_grow_retry
, int, 0644);
6903 MODULE_PARM_DESC(zfs_arc_grow_retry
, "Seconds before growing arc size");
6905 module_param(zfs_arc_p_aggressive_disable
, int, 0644);
6906 MODULE_PARM_DESC(zfs_arc_p_aggressive_disable
, "disable aggressive arc_p grow");
6908 module_param(zfs_arc_p_dampener_disable
, int, 0644);
6909 MODULE_PARM_DESC(zfs_arc_p_dampener_disable
, "disable arc_p adapt dampener");
6911 module_param(zfs_arc_shrink_shift
, int, 0644);
6912 MODULE_PARM_DESC(zfs_arc_shrink_shift
, "log2(fraction of arc to reclaim)");
6914 module_param(zfs_disable_dup_eviction
, int, 0644);
6915 MODULE_PARM_DESC(zfs_disable_dup_eviction
, "disable duplicate buffer eviction");
6917 module_param(zfs_arc_average_blocksize
, int, 0444);
6918 MODULE_PARM_DESC(zfs_arc_average_blocksize
, "Target average block size");
6920 module_param(zfs_arc_memory_throttle_disable
, int, 0644);
6921 MODULE_PARM_DESC(zfs_arc_memory_throttle_disable
, "disable memory throttle");
6923 module_param(zfs_arc_min_prefetch_lifespan
, int, 0644);
6924 MODULE_PARM_DESC(zfs_arc_min_prefetch_lifespan
, "Min life of prefetch block");
6926 module_param(zfs_arc_num_sublists_per_state
, int, 0644);
6927 MODULE_PARM_DESC(zfs_arc_num_sublists_per_state
,
6928 "Number of sublists used in each of the ARC state lists");
6930 module_param(l2arc_write_max
, ulong
, 0644);
6931 MODULE_PARM_DESC(l2arc_write_max
, "Max write bytes per interval");
6933 module_param(l2arc_write_boost
, ulong
, 0644);
6934 MODULE_PARM_DESC(l2arc_write_boost
, "Extra write bytes during device warmup");
6936 module_param(l2arc_headroom
, ulong
, 0644);
6937 MODULE_PARM_DESC(l2arc_headroom
, "Number of max device writes to precache");
6939 module_param(l2arc_headroom_boost
, ulong
, 0644);
6940 MODULE_PARM_DESC(l2arc_headroom_boost
, "Compressed l2arc_headroom multiplier");
6942 module_param(l2arc_feed_secs
, ulong
, 0644);
6943 MODULE_PARM_DESC(l2arc_feed_secs
, "Seconds between L2ARC writing");
6945 module_param(l2arc_feed_min_ms
, ulong
, 0644);
6946 MODULE_PARM_DESC(l2arc_feed_min_ms
, "Min feed interval in milliseconds");
6948 module_param(l2arc_noprefetch
, int, 0644);
6949 MODULE_PARM_DESC(l2arc_noprefetch
, "Skip caching prefetched buffers");
6951 module_param(l2arc_nocompress
, int, 0644);
6952 MODULE_PARM_DESC(l2arc_nocompress
, "Skip compressing L2ARC buffers");
6954 module_param(l2arc_feed_again
, int, 0644);
6955 MODULE_PARM_DESC(l2arc_feed_again
, "Turbo L2ARC warmup");
6957 module_param(l2arc_norw
, int, 0644);
6958 MODULE_PARM_DESC(l2arc_norw
, "No reads during writes");