4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2014 by Saso Kiselkov. All rights reserved.
26 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
30 * DVA-based Adjustable Replacement Cache
32 * While much of the theory of operation used here is
33 * based on the self-tuning, low overhead replacement cache
34 * presented by Megiddo and Modha at FAST 2003, there are some
35 * significant differences:
37 * 1. The Megiddo and Modha model assumes any page is evictable.
38 * Pages in its cache cannot be "locked" into memory. This makes
39 * the eviction algorithm simple: evict the last page in the list.
40 * This also make the performance characteristics easy to reason
41 * about. Our cache is not so simple. At any given moment, some
42 * subset of the blocks in the cache are un-evictable because we
43 * have handed out a reference to them. Blocks are only evictable
44 * when there are no external references active. This makes
45 * eviction far more problematic: we choose to evict the evictable
46 * blocks that are the "lowest" in the list.
48 * There are times when it is not possible to evict the requested
49 * space. In these circumstances we are unable to adjust the cache
50 * size. To prevent the cache growing unbounded at these times we
51 * implement a "cache throttle" that slows the flow of new data
52 * into the cache until we can make space available.
54 * 2. The Megiddo and Modha model assumes a fixed cache size.
55 * Pages are evicted when the cache is full and there is a cache
56 * miss. Our model has a variable sized cache. It grows with
57 * high use, but also tries to react to memory pressure from the
58 * operating system: decreasing its size when system memory is
61 * 3. The Megiddo and Modha model assumes a fixed page size. All
62 * elements of the cache are therefore exactly the same size. So
63 * when adjusting the cache size following a cache miss, its simply
64 * a matter of choosing a single page to evict. In our model, we
65 * have variable sized cache blocks (rangeing from 512 bytes to
66 * 128K bytes). We therefore choose a set of blocks to evict to make
67 * space for a cache miss that approximates as closely as possible
68 * the space used by the new block.
70 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
71 * by N. Megiddo & D. Modha, FAST 2003
77 * A new reference to a cache buffer can be obtained in two
78 * ways: 1) via a hash table lookup using the DVA as a key,
79 * or 2) via one of the ARC lists. The arc_read() interface
80 * uses method 1, while the internal arc algorithms for
81 * adjusting the cache use method 2. We therefore provide two
82 * types of locks: 1) the hash table lock array, and 2) the
85 * Buffers do not have their own mutexes, rather they rely on the
86 * hash table mutexes for the bulk of their protection (i.e. most
87 * fields in the arc_buf_hdr_t are protected by these mutexes).
89 * buf_hash_find() returns the appropriate mutex (held) when it
90 * locates the requested buffer in the hash table. It returns
91 * NULL for the mutex if the buffer was not in the table.
93 * buf_hash_remove() expects the appropriate hash mutex to be
94 * already held before it is invoked.
96 * Each arc state also has a mutex which is used to protect the
97 * buffer list associated with the state. When attempting to
98 * obtain a hash table lock while holding an arc list lock you
99 * must use: mutex_tryenter() to avoid deadlock. Also note that
100 * the active state mutex must be held before the ghost state mutex.
102 * Arc buffers may have an associated eviction callback function.
103 * This function will be invoked prior to removing the buffer (e.g.
104 * in arc_do_user_evicts()). Note however that the data associated
105 * with the buffer may be evicted prior to the callback. The callback
106 * must be made with *no locks held* (to prevent deadlock). Additionally,
107 * the users of callbacks must ensure that their private data is
108 * protected from simultaneous callbacks from arc_clear_callback()
109 * and arc_do_user_evicts().
111 * It as also possible to register a callback which is run when the
112 * arc_meta_limit is reached and no buffers can be safely evicted. In
113 * this case the arc user should drop a reference on some arc buffers so
114 * they can be reclaimed and the arc_meta_limit honored. For example,
115 * when using the ZPL each dentry holds a references on a znode. These
116 * dentries must be pruned before the arc buffer holding the znode can
119 * Note that the majority of the performance stats are manipulated
120 * with atomic operations.
122 * The L2ARC uses the l2ad_mtx on each vdev for the following:
124 * - L2ARC buflist creation
125 * - L2ARC buflist eviction
126 * - L2ARC write completion, which walks L2ARC buflists
127 * - ARC header destruction, as it removes from L2ARC buflists
128 * - ARC header release, as it removes from L2ARC buflists
133 #include <sys/zio_compress.h>
134 #include <sys/zfs_context.h>
136 #include <sys/refcount.h>
137 #include <sys/vdev.h>
138 #include <sys/vdev_impl.h>
139 #include <sys/dsl_pool.h>
140 #include <sys/multilist.h>
142 #include <sys/vmsystm.h>
144 #include <sys/fs/swapnode.h>
146 #include <linux/mm_compat.h>
148 #include <sys/callb.h>
149 #include <sys/kstat.h>
150 #include <sys/dmu_tx.h>
151 #include <zfs_fletcher.h>
152 #include <sys/arc_impl.h>
153 #include <sys/trace_arc.h>
156 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
157 boolean_t arc_watch
= B_FALSE
;
160 static kmutex_t arc_reclaim_lock
;
161 static kcondvar_t arc_reclaim_thread_cv
;
162 static boolean_t arc_reclaim_thread_exit
;
163 static kcondvar_t arc_reclaim_waiters_cv
;
165 static kmutex_t arc_user_evicts_lock
;
166 static kcondvar_t arc_user_evicts_cv
;
167 static boolean_t arc_user_evicts_thread_exit
;
170 * The number of headers to evict in arc_evict_state_impl() before
171 * dropping the sublist lock and evicting from another sublist. A lower
172 * value means we're more likely to evict the "correct" header (i.e. the
173 * oldest header in the arc state), but comes with higher overhead
174 * (i.e. more invocations of arc_evict_state_impl()).
176 int zfs_arc_evict_batch_limit
= 10;
179 * The number of sublists used for each of the arc state lists. If this
180 * is not set to a suitable value by the user, it will be configured to
181 * the number of CPUs on the system in arc_init().
183 int zfs_arc_num_sublists_per_state
= 0;
185 /* number of seconds before growing cache again */
186 static int arc_grow_retry
= 5;
188 /* shift of arc_c for calculating overflow limit in arc_get_data_buf */
189 int zfs_arc_overflow_shift
= 8;
191 /* shift of arc_c for calculating both min and max arc_p */
192 static int arc_p_min_shift
= 4;
194 /* log2(fraction of arc to reclaim) */
195 static int arc_shrink_shift
= 7;
198 * log2(fraction of ARC which must be free to allow growing).
199 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
200 * when reading a new block into the ARC, we will evict an equal-sized block
203 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
204 * we will still not allow it to grow.
206 int arc_no_grow_shift
= 5;
210 * minimum lifespan of a prefetch block in clock ticks
211 * (initialized in arc_init())
213 static int arc_min_prefetch_lifespan
;
216 * If this percent of memory is free, don't throttle.
218 int arc_lotsfree_percent
= 10;
223 * The arc has filled available memory and has now warmed up.
225 static boolean_t arc_warm
;
228 * These tunables are for performance analysis.
230 unsigned long zfs_arc_max
= 0;
231 unsigned long zfs_arc_min
= 0;
232 unsigned long zfs_arc_meta_limit
= 0;
233 unsigned long zfs_arc_meta_min
= 0;
234 int zfs_arc_grow_retry
= 0;
235 int zfs_arc_shrink_shift
= 0;
236 int zfs_arc_p_min_shift
= 0;
237 int zfs_disable_dup_eviction
= 0;
238 int zfs_arc_average_blocksize
= 8 * 1024; /* 8KB */
241 * These tunables are Linux specific
243 int zfs_arc_memory_throttle_disable
= 1;
244 int zfs_arc_min_prefetch_lifespan
= 0;
245 int zfs_arc_p_aggressive_disable
= 1;
246 int zfs_arc_p_dampener_disable
= 1;
247 int zfs_arc_meta_prune
= 10000;
248 int zfs_arc_meta_strategy
= ARC_STRATEGY_META_BALANCED
;
249 int zfs_arc_meta_adjust_restarts
= 4096;
252 static arc_state_t ARC_anon
;
253 static arc_state_t ARC_mru
;
254 static arc_state_t ARC_mru_ghost
;
255 static arc_state_t ARC_mfu
;
256 static arc_state_t ARC_mfu_ghost
;
257 static arc_state_t ARC_l2c_only
;
259 typedef struct arc_stats
{
260 kstat_named_t arcstat_hits
;
261 kstat_named_t arcstat_misses
;
262 kstat_named_t arcstat_demand_data_hits
;
263 kstat_named_t arcstat_demand_data_misses
;
264 kstat_named_t arcstat_demand_metadata_hits
;
265 kstat_named_t arcstat_demand_metadata_misses
;
266 kstat_named_t arcstat_prefetch_data_hits
;
267 kstat_named_t arcstat_prefetch_data_misses
;
268 kstat_named_t arcstat_prefetch_metadata_hits
;
269 kstat_named_t arcstat_prefetch_metadata_misses
;
270 kstat_named_t arcstat_mru_hits
;
271 kstat_named_t arcstat_mru_ghost_hits
;
272 kstat_named_t arcstat_mfu_hits
;
273 kstat_named_t arcstat_mfu_ghost_hits
;
274 kstat_named_t arcstat_deleted
;
276 * Number of buffers that could not be evicted because the hash lock
277 * was held by another thread. The lock may not necessarily be held
278 * by something using the same buffer, since hash locks are shared
279 * by multiple buffers.
281 kstat_named_t arcstat_mutex_miss
;
283 * Number of buffers skipped because they have I/O in progress, are
284 * indrect prefetch buffers that have not lived long enough, or are
285 * not from the spa we're trying to evict from.
287 kstat_named_t arcstat_evict_skip
;
289 * Number of times arc_evict_state() was unable to evict enough
290 * buffers to reach its target amount.
292 kstat_named_t arcstat_evict_not_enough
;
293 kstat_named_t arcstat_evict_l2_cached
;
294 kstat_named_t arcstat_evict_l2_eligible
;
295 kstat_named_t arcstat_evict_l2_ineligible
;
296 kstat_named_t arcstat_evict_l2_skip
;
297 kstat_named_t arcstat_hash_elements
;
298 kstat_named_t arcstat_hash_elements_max
;
299 kstat_named_t arcstat_hash_collisions
;
300 kstat_named_t arcstat_hash_chains
;
301 kstat_named_t arcstat_hash_chain_max
;
302 kstat_named_t arcstat_p
;
303 kstat_named_t arcstat_c
;
304 kstat_named_t arcstat_c_min
;
305 kstat_named_t arcstat_c_max
;
306 kstat_named_t arcstat_size
;
308 * Number of bytes consumed by internal ARC structures necessary
309 * for tracking purposes; these structures are not actually
310 * backed by ARC buffers. This includes arc_buf_hdr_t structures
311 * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only
312 * caches), and arc_buf_t structures (allocated via arc_buf_t
315 kstat_named_t arcstat_hdr_size
;
317 * Number of bytes consumed by ARC buffers of type equal to
318 * ARC_BUFC_DATA. This is generally consumed by buffers backing
319 * on disk user data (e.g. plain file contents).
321 kstat_named_t arcstat_data_size
;
323 * Number of bytes consumed by ARC buffers of type equal to
324 * ARC_BUFC_METADATA. This is generally consumed by buffers
325 * backing on disk data that is used for internal ZFS
326 * structures (e.g. ZAP, dnode, indirect blocks, etc).
328 kstat_named_t arcstat_metadata_size
;
330 * Number of bytes consumed by various buffers and structures
331 * not actually backed with ARC buffers. This includes bonus
332 * buffers (allocated directly via zio_buf_* functions),
333 * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t
334 * cache), and dnode_t structures (allocated via dnode_t cache).
336 kstat_named_t arcstat_other_size
;
338 * Total number of bytes consumed by ARC buffers residing in the
339 * arc_anon state. This includes *all* buffers in the arc_anon
340 * state; e.g. data, metadata, evictable, and unevictable buffers
341 * are all included in this value.
343 kstat_named_t arcstat_anon_size
;
345 * Number of bytes consumed by ARC buffers that meet the
346 * following criteria: backing buffers of type ARC_BUFC_DATA,
347 * residing in the arc_anon state, and are eligible for eviction
348 * (e.g. have no outstanding holds on the buffer).
350 kstat_named_t arcstat_anon_evictable_data
;
352 * Number of bytes consumed by ARC buffers that meet the
353 * following criteria: backing buffers of type ARC_BUFC_METADATA,
354 * residing in the arc_anon state, and are eligible for eviction
355 * (e.g. have no outstanding holds on the buffer).
357 kstat_named_t arcstat_anon_evictable_metadata
;
359 * Total number of bytes consumed by ARC buffers residing in the
360 * arc_mru state. This includes *all* buffers in the arc_mru
361 * state; e.g. data, metadata, evictable, and unevictable buffers
362 * are all included in this value.
364 kstat_named_t arcstat_mru_size
;
366 * Number of bytes consumed by ARC buffers that meet the
367 * following criteria: backing buffers of type ARC_BUFC_DATA,
368 * residing in the arc_mru state, and are eligible for eviction
369 * (e.g. have no outstanding holds on the buffer).
371 kstat_named_t arcstat_mru_evictable_data
;
373 * Number of bytes consumed by ARC buffers that meet the
374 * following criteria: backing buffers of type ARC_BUFC_METADATA,
375 * residing in the arc_mru state, and are eligible for eviction
376 * (e.g. have no outstanding holds on the buffer).
378 kstat_named_t arcstat_mru_evictable_metadata
;
380 * Total number of bytes that *would have been* consumed by ARC
381 * buffers in the arc_mru_ghost state. The key thing to note
382 * here, is the fact that this size doesn't actually indicate
383 * RAM consumption. The ghost lists only consist of headers and
384 * don't actually have ARC buffers linked off of these headers.
385 * Thus, *if* the headers had associated ARC buffers, these
386 * buffers *would have* consumed this number of bytes.
388 kstat_named_t arcstat_mru_ghost_size
;
390 * Number of bytes that *would have been* consumed by ARC
391 * buffers that are eligible for eviction, of type
392 * ARC_BUFC_DATA, and linked off the arc_mru_ghost state.
394 kstat_named_t arcstat_mru_ghost_evictable_data
;
396 * Number of bytes that *would have been* consumed by ARC
397 * buffers that are eligible for eviction, of type
398 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
400 kstat_named_t arcstat_mru_ghost_evictable_metadata
;
402 * Total number of bytes consumed by ARC buffers residing in the
403 * arc_mfu state. This includes *all* buffers in the arc_mfu
404 * state; e.g. data, metadata, evictable, and unevictable buffers
405 * are all included in this value.
407 kstat_named_t arcstat_mfu_size
;
409 * Number of bytes consumed by ARC buffers that are eligible for
410 * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu
413 kstat_named_t arcstat_mfu_evictable_data
;
415 * Number of bytes consumed by ARC buffers that are eligible for
416 * eviction, of type ARC_BUFC_METADATA, and reside in the
419 kstat_named_t arcstat_mfu_evictable_metadata
;
421 * Total number of bytes that *would have been* consumed by ARC
422 * buffers in the arc_mfu_ghost state. See the comment above
423 * arcstat_mru_ghost_size for more details.
425 kstat_named_t arcstat_mfu_ghost_size
;
427 * Number of bytes that *would have been* consumed by ARC
428 * buffers that are eligible for eviction, of type
429 * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state.
431 kstat_named_t arcstat_mfu_ghost_evictable_data
;
433 * Number of bytes that *would have been* consumed by ARC
434 * buffers that are eligible for eviction, of type
435 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
437 kstat_named_t arcstat_mfu_ghost_evictable_metadata
;
438 kstat_named_t arcstat_l2_hits
;
439 kstat_named_t arcstat_l2_misses
;
440 kstat_named_t arcstat_l2_feeds
;
441 kstat_named_t arcstat_l2_rw_clash
;
442 kstat_named_t arcstat_l2_read_bytes
;
443 kstat_named_t arcstat_l2_write_bytes
;
444 kstat_named_t arcstat_l2_writes_sent
;
445 kstat_named_t arcstat_l2_writes_done
;
446 kstat_named_t arcstat_l2_writes_error
;
447 kstat_named_t arcstat_l2_writes_lock_retry
;
448 kstat_named_t arcstat_l2_evict_lock_retry
;
449 kstat_named_t arcstat_l2_evict_reading
;
450 kstat_named_t arcstat_l2_evict_l1cached
;
451 kstat_named_t arcstat_l2_free_on_write
;
452 kstat_named_t arcstat_l2_cdata_free_on_write
;
453 kstat_named_t arcstat_l2_abort_lowmem
;
454 kstat_named_t arcstat_l2_cksum_bad
;
455 kstat_named_t arcstat_l2_io_error
;
456 kstat_named_t arcstat_l2_size
;
457 kstat_named_t arcstat_l2_asize
;
458 kstat_named_t arcstat_l2_hdr_size
;
459 kstat_named_t arcstat_l2_compress_successes
;
460 kstat_named_t arcstat_l2_compress_zeros
;
461 kstat_named_t arcstat_l2_compress_failures
;
462 kstat_named_t arcstat_memory_throttle_count
;
463 kstat_named_t arcstat_duplicate_buffers
;
464 kstat_named_t arcstat_duplicate_buffers_size
;
465 kstat_named_t arcstat_duplicate_reads
;
466 kstat_named_t arcstat_memory_direct_count
;
467 kstat_named_t arcstat_memory_indirect_count
;
468 kstat_named_t arcstat_no_grow
;
469 kstat_named_t arcstat_tempreserve
;
470 kstat_named_t arcstat_loaned_bytes
;
471 kstat_named_t arcstat_prune
;
472 kstat_named_t arcstat_meta_used
;
473 kstat_named_t arcstat_meta_limit
;
474 kstat_named_t arcstat_meta_max
;
475 kstat_named_t arcstat_meta_min
;
478 static arc_stats_t arc_stats
= {
479 { "hits", KSTAT_DATA_UINT64
},
480 { "misses", KSTAT_DATA_UINT64
},
481 { "demand_data_hits", KSTAT_DATA_UINT64
},
482 { "demand_data_misses", KSTAT_DATA_UINT64
},
483 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
484 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
485 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
486 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
487 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
488 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
489 { "mru_hits", KSTAT_DATA_UINT64
},
490 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
491 { "mfu_hits", KSTAT_DATA_UINT64
},
492 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
493 { "deleted", KSTAT_DATA_UINT64
},
494 { "mutex_miss", KSTAT_DATA_UINT64
},
495 { "evict_skip", KSTAT_DATA_UINT64
},
496 { "evict_not_enough", KSTAT_DATA_UINT64
},
497 { "evict_l2_cached", KSTAT_DATA_UINT64
},
498 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
499 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
500 { "evict_l2_skip", KSTAT_DATA_UINT64
},
501 { "hash_elements", KSTAT_DATA_UINT64
},
502 { "hash_elements_max", KSTAT_DATA_UINT64
},
503 { "hash_collisions", KSTAT_DATA_UINT64
},
504 { "hash_chains", KSTAT_DATA_UINT64
},
505 { "hash_chain_max", KSTAT_DATA_UINT64
},
506 { "p", KSTAT_DATA_UINT64
},
507 { "c", KSTAT_DATA_UINT64
},
508 { "c_min", KSTAT_DATA_UINT64
},
509 { "c_max", KSTAT_DATA_UINT64
},
510 { "size", KSTAT_DATA_UINT64
},
511 { "hdr_size", KSTAT_DATA_UINT64
},
512 { "data_size", KSTAT_DATA_UINT64
},
513 { "metadata_size", KSTAT_DATA_UINT64
},
514 { "other_size", KSTAT_DATA_UINT64
},
515 { "anon_size", KSTAT_DATA_UINT64
},
516 { "anon_evictable_data", KSTAT_DATA_UINT64
},
517 { "anon_evictable_metadata", KSTAT_DATA_UINT64
},
518 { "mru_size", KSTAT_DATA_UINT64
},
519 { "mru_evictable_data", KSTAT_DATA_UINT64
},
520 { "mru_evictable_metadata", KSTAT_DATA_UINT64
},
521 { "mru_ghost_size", KSTAT_DATA_UINT64
},
522 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64
},
523 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
524 { "mfu_size", KSTAT_DATA_UINT64
},
525 { "mfu_evictable_data", KSTAT_DATA_UINT64
},
526 { "mfu_evictable_metadata", KSTAT_DATA_UINT64
},
527 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
528 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64
},
529 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64
},
530 { "l2_hits", KSTAT_DATA_UINT64
},
531 { "l2_misses", KSTAT_DATA_UINT64
},
532 { "l2_feeds", KSTAT_DATA_UINT64
},
533 { "l2_rw_clash", KSTAT_DATA_UINT64
},
534 { "l2_read_bytes", KSTAT_DATA_UINT64
},
535 { "l2_write_bytes", KSTAT_DATA_UINT64
},
536 { "l2_writes_sent", KSTAT_DATA_UINT64
},
537 { "l2_writes_done", KSTAT_DATA_UINT64
},
538 { "l2_writes_error", KSTAT_DATA_UINT64
},
539 { "l2_writes_lock_retry", KSTAT_DATA_UINT64
},
540 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
541 { "l2_evict_reading", KSTAT_DATA_UINT64
},
542 { "l2_evict_l1cached", KSTAT_DATA_UINT64
},
543 { "l2_free_on_write", KSTAT_DATA_UINT64
},
544 { "l2_cdata_free_on_write", KSTAT_DATA_UINT64
},
545 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
546 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
547 { "l2_io_error", KSTAT_DATA_UINT64
},
548 { "l2_size", KSTAT_DATA_UINT64
},
549 { "l2_asize", KSTAT_DATA_UINT64
},
550 { "l2_hdr_size", KSTAT_DATA_UINT64
},
551 { "l2_compress_successes", KSTAT_DATA_UINT64
},
552 { "l2_compress_zeros", KSTAT_DATA_UINT64
},
553 { "l2_compress_failures", KSTAT_DATA_UINT64
},
554 { "memory_throttle_count", KSTAT_DATA_UINT64
},
555 { "duplicate_buffers", KSTAT_DATA_UINT64
},
556 { "duplicate_buffers_size", KSTAT_DATA_UINT64
},
557 { "duplicate_reads", KSTAT_DATA_UINT64
},
558 { "memory_direct_count", KSTAT_DATA_UINT64
},
559 { "memory_indirect_count", KSTAT_DATA_UINT64
},
560 { "arc_no_grow", KSTAT_DATA_UINT64
},
561 { "arc_tempreserve", KSTAT_DATA_UINT64
},
562 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
563 { "arc_prune", KSTAT_DATA_UINT64
},
564 { "arc_meta_used", KSTAT_DATA_UINT64
},
565 { "arc_meta_limit", KSTAT_DATA_UINT64
},
566 { "arc_meta_max", KSTAT_DATA_UINT64
},
567 { "arc_meta_min", KSTAT_DATA_UINT64
}
570 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
572 #define ARCSTAT_INCR(stat, val) \
573 atomic_add_64(&arc_stats.stat.value.ui64, (val))
575 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
576 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
578 #define ARCSTAT_MAX(stat, val) { \
580 while ((val) > (m = arc_stats.stat.value.ui64) && \
581 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
585 #define ARCSTAT_MAXSTAT(stat) \
586 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
589 * We define a macro to allow ARC hits/misses to be easily broken down by
590 * two separate conditions, giving a total of four different subtypes for
591 * each of hits and misses (so eight statistics total).
593 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
596 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
598 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
602 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
604 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
609 static arc_state_t
*arc_anon
;
610 static arc_state_t
*arc_mru
;
611 static arc_state_t
*arc_mru_ghost
;
612 static arc_state_t
*arc_mfu
;
613 static arc_state_t
*arc_mfu_ghost
;
614 static arc_state_t
*arc_l2c_only
;
617 * There are several ARC variables that are critical to export as kstats --
618 * but we don't want to have to grovel around in the kstat whenever we wish to
619 * manipulate them. For these variables, we therefore define them to be in
620 * terms of the statistic variable. This assures that we are not introducing
621 * the possibility of inconsistency by having shadow copies of the variables,
622 * while still allowing the code to be readable.
624 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
625 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
626 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
627 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
628 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
629 #define arc_no_grow ARCSTAT(arcstat_no_grow)
630 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
631 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
632 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
633 #define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */
634 #define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */
635 #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
637 #define L2ARC_IS_VALID_COMPRESS(_c_) \
638 ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY)
640 static list_t arc_prune_list
;
641 static kmutex_t arc_prune_mtx
;
642 static taskq_t
*arc_prune_taskq
;
643 static arc_buf_t
*arc_eviction_list
;
644 static arc_buf_hdr_t arc_eviction_hdr
;
646 #define GHOST_STATE(state) \
647 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
648 (state) == arc_l2c_only)
650 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
651 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
652 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
653 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
654 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ)
655 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE)
657 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
658 #define HDR_L2COMPRESS(hdr) ((hdr)->b_flags & ARC_FLAG_L2COMPRESS)
659 #define HDR_L2_READING(hdr) \
660 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
661 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
662 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
663 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
664 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
666 #define HDR_ISTYPE_METADATA(hdr) \
667 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
668 #define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
670 #define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
671 #define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
673 /* For storing compression mode in b_flags */
674 #define HDR_COMPRESS_OFFSET 24
675 #define HDR_COMPRESS_NBITS 7
677 #define HDR_GET_COMPRESS(hdr) ((enum zio_compress)BF32_GET(hdr->b_flags, \
678 HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS))
679 #define HDR_SET_COMPRESS(hdr, cmp) BF32_SET(hdr->b_flags, \
680 HDR_COMPRESS_OFFSET, HDR_COMPRESS_NBITS, (cmp))
686 #define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
687 #define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
690 * Hash table routines
693 #define HT_LOCK_ALIGN 64
694 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
699 unsigned char pad
[HT_LOCK_PAD
];
703 #define BUF_LOCKS 8192
704 typedef struct buf_hash_table
{
706 arc_buf_hdr_t
**ht_table
;
707 struct ht_lock ht_locks
[BUF_LOCKS
];
710 static buf_hash_table_t buf_hash_table
;
712 #define BUF_HASH_INDEX(spa, dva, birth) \
713 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
714 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
715 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
716 #define HDR_LOCK(hdr) \
717 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
719 uint64_t zfs_crc64_table
[256];
725 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
726 #define L2ARC_HEADROOM 2 /* num of writes */
728 * If we discover during ARC scan any buffers to be compressed, we boost
729 * our headroom for the next scanning cycle by this percentage multiple.
731 #define L2ARC_HEADROOM_BOOST 200
732 #define L2ARC_FEED_SECS 1 /* caching interval secs */
733 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
736 * Used to distinguish headers that are being process by
737 * l2arc_write_buffers(), but have yet to be assigned to a l2arc disk
738 * address. This can happen when the header is added to the l2arc's list
739 * of buffers to write in the first stage of l2arc_write_buffers(), but
740 * has not yet been written out which happens in the second stage of
741 * l2arc_write_buffers().
743 #define L2ARC_ADDR_UNSET ((uint64_t)(-1))
745 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
746 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
748 /* L2ARC Performance Tunables */
749 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
750 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
751 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
752 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
753 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
754 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
755 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
756 int l2arc_nocompress
= B_FALSE
; /* don't compress bufs */
757 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
758 int l2arc_norw
= B_FALSE
; /* no reads during writes */
763 static list_t L2ARC_dev_list
; /* device list */
764 static list_t
*l2arc_dev_list
; /* device list pointer */
765 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
766 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
767 static list_t L2ARC_free_on_write
; /* free after write buf list */
768 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
769 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
770 static uint64_t l2arc_ndev
; /* number of devices */
772 typedef struct l2arc_read_callback
{
773 arc_buf_t
*l2rcb_buf
; /* read buffer */
774 spa_t
*l2rcb_spa
; /* spa */
775 blkptr_t l2rcb_bp
; /* original blkptr */
776 zbookmark_phys_t l2rcb_zb
; /* original bookmark */
777 int l2rcb_flags
; /* original flags */
778 enum zio_compress l2rcb_compress
; /* applied compress */
779 } l2arc_read_callback_t
;
781 typedef struct l2arc_data_free
{
782 /* protected by l2arc_free_on_write_mtx */
785 void (*l2df_func
)(void *, size_t);
786 list_node_t l2df_list_node
;
789 static kmutex_t l2arc_feed_thr_lock
;
790 static kcondvar_t l2arc_feed_thr_cv
;
791 static uint8_t l2arc_thread_exit
;
793 static void arc_get_data_buf(arc_buf_t
*);
794 static void arc_access(arc_buf_hdr_t
*, kmutex_t
*);
795 static boolean_t
arc_is_overflowing(void);
796 static void arc_buf_watch(arc_buf_t
*);
797 static void arc_tuning_update(void);
799 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t
*);
800 static uint32_t arc_bufc_to_flags(arc_buf_contents_t
);
802 static boolean_t
l2arc_write_eligible(uint64_t, arc_buf_hdr_t
*);
803 static void l2arc_read_done(zio_t
*);
805 static boolean_t
l2arc_compress_buf(arc_buf_hdr_t
*);
806 static void l2arc_decompress_zio(zio_t
*, arc_buf_hdr_t
*, enum zio_compress
);
807 static void l2arc_release_cdata_buf(arc_buf_hdr_t
*);
810 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
812 uint8_t *vdva
= (uint8_t *)dva
;
813 uint64_t crc
= -1ULL;
816 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
818 for (i
= 0; i
< sizeof (dva_t
); i
++)
819 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ vdva
[i
]) & 0xFF];
821 crc
^= (spa
>>8) ^ birth
;
826 #define BUF_EMPTY(buf) \
827 ((buf)->b_dva.dva_word[0] == 0 && \
828 (buf)->b_dva.dva_word[1] == 0)
830 #define BUF_EQUAL(spa, dva, birth, buf) \
831 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
832 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
833 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
836 buf_discard_identity(arc_buf_hdr_t
*hdr
)
838 hdr
->b_dva
.dva_word
[0] = 0;
839 hdr
->b_dva
.dva_word
[1] = 0;
843 static arc_buf_hdr_t
*
844 buf_hash_find(uint64_t spa
, const blkptr_t
*bp
, kmutex_t
**lockp
)
846 const dva_t
*dva
= BP_IDENTITY(bp
);
847 uint64_t birth
= BP_PHYSICAL_BIRTH(bp
);
848 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
849 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
852 mutex_enter(hash_lock
);
853 for (hdr
= buf_hash_table
.ht_table
[idx
]; hdr
!= NULL
;
854 hdr
= hdr
->b_hash_next
) {
855 if (BUF_EQUAL(spa
, dva
, birth
, hdr
)) {
860 mutex_exit(hash_lock
);
866 * Insert an entry into the hash table. If there is already an element
867 * equal to elem in the hash table, then the already existing element
868 * will be returned and the new element will not be inserted.
869 * Otherwise returns NULL.
870 * If lockp == NULL, the caller is assumed to already hold the hash lock.
872 static arc_buf_hdr_t
*
873 buf_hash_insert(arc_buf_hdr_t
*hdr
, kmutex_t
**lockp
)
875 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
876 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
880 ASSERT(!DVA_IS_EMPTY(&hdr
->b_dva
));
881 ASSERT(hdr
->b_birth
!= 0);
882 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
886 mutex_enter(hash_lock
);
888 ASSERT(MUTEX_HELD(hash_lock
));
891 for (fhdr
= buf_hash_table
.ht_table
[idx
], i
= 0; fhdr
!= NULL
;
892 fhdr
= fhdr
->b_hash_next
, i
++) {
893 if (BUF_EQUAL(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
, fhdr
))
897 hdr
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
898 buf_hash_table
.ht_table
[idx
] = hdr
;
899 hdr
->b_flags
|= ARC_FLAG_IN_HASH_TABLE
;
901 /* collect some hash table performance data */
903 ARCSTAT_BUMP(arcstat_hash_collisions
);
905 ARCSTAT_BUMP(arcstat_hash_chains
);
907 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
910 ARCSTAT_BUMP(arcstat_hash_elements
);
911 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
917 buf_hash_remove(arc_buf_hdr_t
*hdr
)
919 arc_buf_hdr_t
*fhdr
, **hdrp
;
920 uint64_t idx
= BUF_HASH_INDEX(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
);
922 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
923 ASSERT(HDR_IN_HASH_TABLE(hdr
));
925 hdrp
= &buf_hash_table
.ht_table
[idx
];
926 while ((fhdr
= *hdrp
) != hdr
) {
927 ASSERT(fhdr
!= NULL
);
928 hdrp
= &fhdr
->b_hash_next
;
930 *hdrp
= hdr
->b_hash_next
;
931 hdr
->b_hash_next
= NULL
;
932 hdr
->b_flags
&= ~ARC_FLAG_IN_HASH_TABLE
;
934 /* collect some hash table performance data */
935 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
937 if (buf_hash_table
.ht_table
[idx
] &&
938 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
939 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
943 * Global data structures and functions for the buf kmem cache.
945 static kmem_cache_t
*hdr_full_cache
;
946 static kmem_cache_t
*hdr_l2only_cache
;
947 static kmem_cache_t
*buf_cache
;
954 #if defined(_KERNEL) && defined(HAVE_SPL)
956 * Large allocations which do not require contiguous pages
957 * should be using vmem_free() in the linux kernel\
959 vmem_free(buf_hash_table
.ht_table
,
960 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
962 kmem_free(buf_hash_table
.ht_table
,
963 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
965 for (i
= 0; i
< BUF_LOCKS
; i
++)
966 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
967 kmem_cache_destroy(hdr_full_cache
);
968 kmem_cache_destroy(hdr_l2only_cache
);
969 kmem_cache_destroy(buf_cache
);
973 * Constructor callback - called when the cache is empty
974 * and a new buf is requested.
978 hdr_full_cons(void *vbuf
, void *unused
, int kmflag
)
980 arc_buf_hdr_t
*hdr
= vbuf
;
982 bzero(hdr
, HDR_FULL_SIZE
);
983 cv_init(&hdr
->b_l1hdr
.b_cv
, NULL
, CV_DEFAULT
, NULL
);
984 refcount_create(&hdr
->b_l1hdr
.b_refcnt
);
985 mutex_init(&hdr
->b_l1hdr
.b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
986 list_link_init(&hdr
->b_l1hdr
.b_arc_node
);
987 list_link_init(&hdr
->b_l2hdr
.b_l2node
);
988 multilist_link_init(&hdr
->b_l1hdr
.b_arc_node
);
989 arc_space_consume(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
996 hdr_l2only_cons(void *vbuf
, void *unused
, int kmflag
)
998 arc_buf_hdr_t
*hdr
= vbuf
;
1000 bzero(hdr
, HDR_L2ONLY_SIZE
);
1001 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1008 buf_cons(void *vbuf
, void *unused
, int kmflag
)
1010 arc_buf_t
*buf
= vbuf
;
1012 bzero(buf
, sizeof (arc_buf_t
));
1013 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
1014 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1020 * Destructor callback - called when a cached buf is
1021 * no longer required.
1025 hdr_full_dest(void *vbuf
, void *unused
)
1027 arc_buf_hdr_t
*hdr
= vbuf
;
1029 ASSERT(BUF_EMPTY(hdr
));
1030 cv_destroy(&hdr
->b_l1hdr
.b_cv
);
1031 refcount_destroy(&hdr
->b_l1hdr
.b_refcnt
);
1032 mutex_destroy(&hdr
->b_l1hdr
.b_freeze_lock
);
1033 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1034 arc_space_return(HDR_FULL_SIZE
, ARC_SPACE_HDRS
);
1039 hdr_l2only_dest(void *vbuf
, void *unused
)
1041 ASSERTV(arc_buf_hdr_t
*hdr
= vbuf
);
1043 ASSERT(BUF_EMPTY(hdr
));
1044 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
1049 buf_dest(void *vbuf
, void *unused
)
1051 arc_buf_t
*buf
= vbuf
;
1053 mutex_destroy(&buf
->b_evict_lock
);
1054 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
1058 * Reclaim callback -- invoked when memory is low.
1062 hdr_recl(void *unused
)
1064 dprintf("hdr_recl called\n");
1066 * umem calls the reclaim func when we destroy the buf cache,
1067 * which is after we do arc_fini().
1070 cv_signal(&arc_reclaim_thread_cv
);
1077 uint64_t hsize
= 1ULL << 12;
1081 * The hash table is big enough to fill all of physical memory
1082 * with an average block size of zfs_arc_average_blocksize (default 8K).
1083 * By default, the table will take up
1084 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1086 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
1089 buf_hash_table
.ht_mask
= hsize
- 1;
1090 #if defined(_KERNEL) && defined(HAVE_SPL)
1092 * Large allocations which do not require contiguous pages
1093 * should be using vmem_alloc() in the linux kernel
1095 buf_hash_table
.ht_table
=
1096 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1098 buf_hash_table
.ht_table
=
1099 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1101 if (buf_hash_table
.ht_table
== NULL
) {
1102 ASSERT(hsize
> (1ULL << 8));
1107 hdr_full_cache
= kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE
,
1108 0, hdr_full_cons
, hdr_full_dest
, hdr_recl
, NULL
, NULL
, 0);
1109 hdr_l2only_cache
= kmem_cache_create("arc_buf_hdr_t_l2only",
1110 HDR_L2ONLY_SIZE
, 0, hdr_l2only_cons
, hdr_l2only_dest
, hdr_recl
,
1112 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1113 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1115 for (i
= 0; i
< 256; i
++)
1116 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1117 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1119 for (i
= 0; i
< BUF_LOCKS
; i
++) {
1120 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
1121 NULL
, MUTEX_DEFAULT
, NULL
);
1126 * Transition between the two allocation states for the arc_buf_hdr struct.
1127 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
1128 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
1129 * version is used when a cache buffer is only in the L2ARC in order to reduce
1132 static arc_buf_hdr_t
*
1133 arc_hdr_realloc(arc_buf_hdr_t
*hdr
, kmem_cache_t
*old
, kmem_cache_t
*new)
1135 arc_buf_hdr_t
*nhdr
;
1138 ASSERT(HDR_HAS_L2HDR(hdr
));
1139 ASSERT((old
== hdr_full_cache
&& new == hdr_l2only_cache
) ||
1140 (old
== hdr_l2only_cache
&& new == hdr_full_cache
));
1142 dev
= hdr
->b_l2hdr
.b_dev
;
1143 nhdr
= kmem_cache_alloc(new, KM_PUSHPAGE
);
1145 ASSERT(MUTEX_HELD(HDR_LOCK(hdr
)));
1146 buf_hash_remove(hdr
);
1148 bcopy(hdr
, nhdr
, HDR_L2ONLY_SIZE
);
1150 if (new == hdr_full_cache
) {
1151 nhdr
->b_flags
|= ARC_FLAG_HAS_L1HDR
;
1153 * arc_access and arc_change_state need to be aware that a
1154 * header has just come out of L2ARC, so we set its state to
1155 * l2c_only even though it's about to change.
1157 nhdr
->b_l1hdr
.b_state
= arc_l2c_only
;
1159 /* Verify previous threads set to NULL before freeing */
1160 ASSERT3P(nhdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
1162 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
);
1163 ASSERT0(hdr
->b_l1hdr
.b_datacnt
);
1166 * If we've reached here, We must have been called from
1167 * arc_evict_hdr(), as such we should have already been
1168 * removed from any ghost list we were previously on
1169 * (which protects us from racing with arc_evict_state),
1170 * thus no locking is needed during this check.
1172 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
1175 * A buffer must not be moved into the arc_l2c_only
1176 * state if it's not finished being written out to the
1177 * l2arc device. Otherwise, the b_l1hdr.b_tmp_cdata field
1178 * might try to be accessed, even though it was removed.
1180 VERIFY(!HDR_L2_WRITING(hdr
));
1181 VERIFY3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
1183 nhdr
->b_flags
&= ~ARC_FLAG_HAS_L1HDR
;
1186 * The header has been reallocated so we need to re-insert it into any
1189 (void) buf_hash_insert(nhdr
, NULL
);
1191 ASSERT(list_link_active(&hdr
->b_l2hdr
.b_l2node
));
1193 mutex_enter(&dev
->l2ad_mtx
);
1196 * We must place the realloc'ed header back into the list at
1197 * the same spot. Otherwise, if it's placed earlier in the list,
1198 * l2arc_write_buffers() could find it during the function's
1199 * write phase, and try to write it out to the l2arc.
1201 list_insert_after(&dev
->l2ad_buflist
, hdr
, nhdr
);
1202 list_remove(&dev
->l2ad_buflist
, hdr
);
1204 mutex_exit(&dev
->l2ad_mtx
);
1207 * Since we're using the pointer address as the tag when
1208 * incrementing and decrementing the l2ad_alloc refcount, we
1209 * must remove the old pointer (that we're about to destroy) and
1210 * add the new pointer to the refcount. Otherwise we'd remove
1211 * the wrong pointer address when calling arc_hdr_destroy() later.
1214 (void) refcount_remove_many(&dev
->l2ad_alloc
,
1215 hdr
->b_l2hdr
.b_asize
, hdr
);
1217 (void) refcount_add_many(&dev
->l2ad_alloc
,
1218 nhdr
->b_l2hdr
.b_asize
, nhdr
);
1220 buf_discard_identity(hdr
);
1221 hdr
->b_freeze_cksum
= NULL
;
1222 kmem_cache_free(old
, hdr
);
1228 #define ARC_MINTIME (hz>>4) /* 62 ms */
1231 arc_cksum_verify(arc_buf_t
*buf
)
1235 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1238 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1239 if (buf
->b_hdr
->b_freeze_cksum
== NULL
|| HDR_IO_ERROR(buf
->b_hdr
)) {
1240 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1243 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
1244 if (!ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
))
1245 panic("buffer modified while frozen!");
1246 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1250 arc_cksum_equal(arc_buf_t
*buf
)
1255 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1256 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
1257 equal
= ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
);
1258 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1264 arc_cksum_compute(arc_buf_t
*buf
, boolean_t force
)
1266 if (!force
&& !(zfs_flags
& ZFS_DEBUG_MODIFY
))
1269 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1270 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
1271 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1274 buf
->b_hdr
->b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1276 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
,
1277 buf
->b_hdr
->b_freeze_cksum
);
1278 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1284 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1286 panic("Got SIGSEGV at address: 0x%lx\n", (long) si
->si_addr
);
1292 arc_buf_unwatch(arc_buf_t
*buf
)
1296 ASSERT0(mprotect(buf
->b_data
, buf
->b_hdr
->b_size
,
1297 PROT_READ
| PROT_WRITE
));
1304 arc_buf_watch(arc_buf_t
*buf
)
1308 ASSERT0(mprotect(buf
->b_data
, buf
->b_hdr
->b_size
, PROT_READ
));
1312 static arc_buf_contents_t
1313 arc_buf_type(arc_buf_hdr_t
*hdr
)
1315 if (HDR_ISTYPE_METADATA(hdr
)) {
1316 return (ARC_BUFC_METADATA
);
1318 return (ARC_BUFC_DATA
);
1323 arc_bufc_to_flags(arc_buf_contents_t type
)
1327 /* metadata field is 0 if buffer contains normal data */
1329 case ARC_BUFC_METADATA
:
1330 return (ARC_FLAG_BUFC_METADATA
);
1334 panic("undefined ARC buffer type!");
1335 return ((uint32_t)-1);
1339 arc_buf_thaw(arc_buf_t
*buf
)
1341 if (zfs_flags
& ZFS_DEBUG_MODIFY
) {
1342 if (buf
->b_hdr
->b_l1hdr
.b_state
!= arc_anon
)
1343 panic("modifying non-anon buffer!");
1344 if (HDR_IO_IN_PROGRESS(buf
->b_hdr
))
1345 panic("modifying buffer while i/o in progress!");
1346 arc_cksum_verify(buf
);
1349 mutex_enter(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1350 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
1351 kmem_free(buf
->b_hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
1352 buf
->b_hdr
->b_freeze_cksum
= NULL
;
1355 mutex_exit(&buf
->b_hdr
->b_l1hdr
.b_freeze_lock
);
1357 arc_buf_unwatch(buf
);
1361 arc_buf_freeze(arc_buf_t
*buf
)
1363 kmutex_t
*hash_lock
;
1365 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1368 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1369 mutex_enter(hash_lock
);
1371 ASSERT(buf
->b_hdr
->b_freeze_cksum
!= NULL
||
1372 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
1373 arc_cksum_compute(buf
, B_FALSE
);
1374 mutex_exit(hash_lock
);
1379 add_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
1383 ASSERT(HDR_HAS_L1HDR(hdr
));
1384 ASSERT(MUTEX_HELD(hash_lock
));
1386 state
= hdr
->b_l1hdr
.b_state
;
1388 if ((refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
) == 1) &&
1389 (state
!= arc_anon
)) {
1390 /* We don't use the L2-only state list. */
1391 if (state
!= arc_l2c_only
) {
1392 arc_buf_contents_t type
= arc_buf_type(hdr
);
1393 uint64_t delta
= hdr
->b_size
* hdr
->b_l1hdr
.b_datacnt
;
1394 multilist_t
*list
= &state
->arcs_list
[type
];
1395 uint64_t *size
= &state
->arcs_lsize
[type
];
1397 multilist_remove(list
, hdr
);
1399 if (GHOST_STATE(state
)) {
1400 ASSERT0(hdr
->b_l1hdr
.b_datacnt
);
1401 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
1402 delta
= hdr
->b_size
;
1405 ASSERT3U(*size
, >=, delta
);
1406 atomic_add_64(size
, -delta
);
1408 /* remove the prefetch flag if we get a reference */
1409 hdr
->b_flags
&= ~ARC_FLAG_PREFETCH
;
1414 remove_reference(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
, void *tag
)
1417 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
1419 ASSERT(HDR_HAS_L1HDR(hdr
));
1420 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
1421 ASSERT(!GHOST_STATE(state
));
1424 * arc_l2c_only counts as a ghost state so we don't need to explicitly
1425 * check to prevent usage of the arc_l2c_only list.
1427 if (((cnt
= refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
)) == 0) &&
1428 (state
!= arc_anon
)) {
1429 arc_buf_contents_t type
= arc_buf_type(hdr
);
1430 multilist_t
*list
= &state
->arcs_list
[type
];
1431 uint64_t *size
= &state
->arcs_lsize
[type
];
1433 multilist_insert(list
, hdr
);
1435 ASSERT(hdr
->b_l1hdr
.b_datacnt
> 0);
1436 atomic_add_64(size
, hdr
->b_size
*
1437 hdr
->b_l1hdr
.b_datacnt
);
1443 * Returns detailed information about a specific arc buffer. When the
1444 * state_index argument is set the function will calculate the arc header
1445 * list position for its arc state. Since this requires a linear traversal
1446 * callers are strongly encourage not to do this. However, it can be helpful
1447 * for targeted analysis so the functionality is provided.
1450 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
1452 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
1453 l1arc_buf_hdr_t
*l1hdr
= NULL
;
1454 l2arc_buf_hdr_t
*l2hdr
= NULL
;
1455 arc_state_t
*state
= NULL
;
1457 if (HDR_HAS_L1HDR(hdr
)) {
1458 l1hdr
= &hdr
->b_l1hdr
;
1459 state
= l1hdr
->b_state
;
1461 if (HDR_HAS_L2HDR(hdr
))
1462 l2hdr
= &hdr
->b_l2hdr
;
1464 memset(abi
, 0, sizeof (arc_buf_info_t
));
1465 abi
->abi_flags
= hdr
->b_flags
;
1468 abi
->abi_datacnt
= l1hdr
->b_datacnt
;
1469 abi
->abi_access
= l1hdr
->b_arc_access
;
1470 abi
->abi_mru_hits
= l1hdr
->b_mru_hits
;
1471 abi
->abi_mru_ghost_hits
= l1hdr
->b_mru_ghost_hits
;
1472 abi
->abi_mfu_hits
= l1hdr
->b_mfu_hits
;
1473 abi
->abi_mfu_ghost_hits
= l1hdr
->b_mfu_ghost_hits
;
1474 abi
->abi_holds
= refcount_count(&l1hdr
->b_refcnt
);
1478 abi
->abi_l2arc_dattr
= l2hdr
->b_daddr
;
1479 abi
->abi_l2arc_asize
= l2hdr
->b_asize
;
1480 abi
->abi_l2arc_compress
= HDR_GET_COMPRESS(hdr
);
1481 abi
->abi_l2arc_hits
= l2hdr
->b_hits
;
1484 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
1485 abi
->abi_state_contents
= arc_buf_type(hdr
);
1486 abi
->abi_size
= hdr
->b_size
;
1490 * Move the supplied buffer to the indicated state. The hash lock
1491 * for the buffer must be held by the caller.
1494 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*hdr
,
1495 kmutex_t
*hash_lock
)
1497 arc_state_t
*old_state
;
1500 uint64_t from_delta
, to_delta
;
1501 arc_buf_contents_t buftype
= arc_buf_type(hdr
);
1504 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
1505 * in arc_read() when bringing a buffer out of the L2ARC. However, the
1506 * L1 hdr doesn't always exist when we change state to arc_anon before
1507 * destroying a header, in which case reallocating to add the L1 hdr is
1510 if (HDR_HAS_L1HDR(hdr
)) {
1511 old_state
= hdr
->b_l1hdr
.b_state
;
1512 refcnt
= refcount_count(&hdr
->b_l1hdr
.b_refcnt
);
1513 datacnt
= hdr
->b_l1hdr
.b_datacnt
;
1515 old_state
= arc_l2c_only
;
1520 ASSERT(MUTEX_HELD(hash_lock
));
1521 ASSERT3P(new_state
, !=, old_state
);
1522 ASSERT(refcnt
== 0 || datacnt
> 0);
1523 ASSERT(!GHOST_STATE(new_state
) || datacnt
== 0);
1524 ASSERT(old_state
!= arc_anon
|| datacnt
<= 1);
1526 from_delta
= to_delta
= datacnt
* hdr
->b_size
;
1529 * If this buffer is evictable, transfer it from the
1530 * old state list to the new state list.
1533 if (old_state
!= arc_anon
&& old_state
!= arc_l2c_only
) {
1534 uint64_t *size
= &old_state
->arcs_lsize
[buftype
];
1536 ASSERT(HDR_HAS_L1HDR(hdr
));
1537 multilist_remove(&old_state
->arcs_list
[buftype
], hdr
);
1540 * If prefetching out of the ghost cache,
1541 * we will have a non-zero datacnt.
1543 if (GHOST_STATE(old_state
) && datacnt
== 0) {
1544 /* ghost elements have a ghost size */
1545 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
);
1546 from_delta
= hdr
->b_size
;
1548 ASSERT3U(*size
, >=, from_delta
);
1549 atomic_add_64(size
, -from_delta
);
1551 if (new_state
!= arc_anon
&& new_state
!= arc_l2c_only
) {
1552 uint64_t *size
= &new_state
->arcs_lsize
[buftype
];
1555 * An L1 header always exists here, since if we're
1556 * moving to some L1-cached state (i.e. not l2c_only or
1557 * anonymous), we realloc the header to add an L1hdr
1560 ASSERT(HDR_HAS_L1HDR(hdr
));
1561 multilist_insert(&new_state
->arcs_list
[buftype
], hdr
);
1563 /* ghost elements have a ghost size */
1564 if (GHOST_STATE(new_state
)) {
1566 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
);
1567 to_delta
= hdr
->b_size
;
1569 atomic_add_64(size
, to_delta
);
1573 ASSERT(!BUF_EMPTY(hdr
));
1574 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(hdr
))
1575 buf_hash_remove(hdr
);
1577 /* adjust state sizes (ignore arc_l2c_only) */
1579 if (to_delta
&& new_state
!= arc_l2c_only
) {
1580 ASSERT(HDR_HAS_L1HDR(hdr
));
1581 if (GHOST_STATE(new_state
)) {
1585 * We moving a header to a ghost state, we first
1586 * remove all arc buffers. Thus, we'll have a
1587 * datacnt of zero, and no arc buffer to use for
1588 * the reference. As a result, we use the arc
1589 * header pointer for the reference.
1591 (void) refcount_add_many(&new_state
->arcs_size
,
1595 ASSERT3U(datacnt
, !=, 0);
1598 * Each individual buffer holds a unique reference,
1599 * thus we must remove each of these references one
1602 for (buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
1603 buf
= buf
->b_next
) {
1604 (void) refcount_add_many(&new_state
->arcs_size
,
1610 if (from_delta
&& old_state
!= arc_l2c_only
) {
1611 ASSERT(HDR_HAS_L1HDR(hdr
));
1612 if (GHOST_STATE(old_state
)) {
1614 * When moving a header off of a ghost state,
1615 * there's the possibility for datacnt to be
1616 * non-zero. This is because we first add the
1617 * arc buffer to the header prior to changing
1618 * the header's state. Since we used the header
1619 * for the reference when putting the header on
1620 * the ghost state, we must balance that and use
1621 * the header when removing off the ghost state
1622 * (even though datacnt is non zero).
1625 IMPLY(datacnt
== 0, new_state
== arc_anon
||
1626 new_state
== arc_l2c_only
);
1628 (void) refcount_remove_many(&old_state
->arcs_size
,
1632 ASSERT3U(datacnt
, !=, 0);
1635 * Each individual buffer holds a unique reference,
1636 * thus we must remove each of these references one
1639 for (buf
= hdr
->b_l1hdr
.b_buf
; buf
!= NULL
;
1640 buf
= buf
->b_next
) {
1641 (void) refcount_remove_many(
1642 &old_state
->arcs_size
, hdr
->b_size
, buf
);
1647 if (HDR_HAS_L1HDR(hdr
))
1648 hdr
->b_l1hdr
.b_state
= new_state
;
1651 * L2 headers should never be on the L2 state list since they don't
1652 * have L1 headers allocated.
1654 ASSERT(multilist_is_empty(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]) &&
1655 multilist_is_empty(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]));
1659 arc_space_consume(uint64_t space
, arc_space_type_t type
)
1661 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1666 case ARC_SPACE_DATA
:
1667 ARCSTAT_INCR(arcstat_data_size
, space
);
1669 case ARC_SPACE_META
:
1670 ARCSTAT_INCR(arcstat_metadata_size
, space
);
1672 case ARC_SPACE_OTHER
:
1673 ARCSTAT_INCR(arcstat_other_size
, space
);
1675 case ARC_SPACE_HDRS
:
1676 ARCSTAT_INCR(arcstat_hdr_size
, space
);
1678 case ARC_SPACE_L2HDRS
:
1679 ARCSTAT_INCR(arcstat_l2_hdr_size
, space
);
1683 if (type
!= ARC_SPACE_DATA
)
1684 ARCSTAT_INCR(arcstat_meta_used
, space
);
1686 atomic_add_64(&arc_size
, space
);
1690 arc_space_return(uint64_t space
, arc_space_type_t type
)
1692 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1697 case ARC_SPACE_DATA
:
1698 ARCSTAT_INCR(arcstat_data_size
, -space
);
1700 case ARC_SPACE_META
:
1701 ARCSTAT_INCR(arcstat_metadata_size
, -space
);
1703 case ARC_SPACE_OTHER
:
1704 ARCSTAT_INCR(arcstat_other_size
, -space
);
1706 case ARC_SPACE_HDRS
:
1707 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
1709 case ARC_SPACE_L2HDRS
:
1710 ARCSTAT_INCR(arcstat_l2_hdr_size
, -space
);
1714 if (type
!= ARC_SPACE_DATA
) {
1715 ASSERT(arc_meta_used
>= space
);
1716 if (arc_meta_max
< arc_meta_used
)
1717 arc_meta_max
= arc_meta_used
;
1718 ARCSTAT_INCR(arcstat_meta_used
, -space
);
1721 ASSERT(arc_size
>= space
);
1722 atomic_add_64(&arc_size
, -space
);
1726 arc_buf_alloc(spa_t
*spa
, uint64_t size
, void *tag
, arc_buf_contents_t type
)
1731 VERIFY3U(size
, <=, spa_maxblocksize(spa
));
1732 hdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
1733 ASSERT(BUF_EMPTY(hdr
));
1734 ASSERT3P(hdr
->b_freeze_cksum
, ==, NULL
);
1736 hdr
->b_spa
= spa_load_guid(spa
);
1737 hdr
->b_l1hdr
.b_mru_hits
= 0;
1738 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
1739 hdr
->b_l1hdr
.b_mfu_hits
= 0;
1740 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
1741 hdr
->b_l1hdr
.b_l2_hits
= 0;
1743 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1746 buf
->b_efunc
= NULL
;
1747 buf
->b_private
= NULL
;
1750 hdr
->b_flags
= arc_bufc_to_flags(type
);
1751 hdr
->b_flags
|= ARC_FLAG_HAS_L1HDR
;
1753 hdr
->b_l1hdr
.b_buf
= buf
;
1754 hdr
->b_l1hdr
.b_state
= arc_anon
;
1755 hdr
->b_l1hdr
.b_arc_access
= 0;
1756 hdr
->b_l1hdr
.b_datacnt
= 1;
1757 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
1759 arc_get_data_buf(buf
);
1761 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
1762 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
1767 static char *arc_onloan_tag
= "onloan";
1770 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1771 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1772 * buffers must be returned to the arc before they can be used by the DMU or
1776 arc_loan_buf(spa_t
*spa
, uint64_t size
)
1780 buf
= arc_buf_alloc(spa
, size
, arc_onloan_tag
, ARC_BUFC_DATA
);
1782 atomic_add_64(&arc_loaned_bytes
, size
);
1787 * Return a loaned arc buffer to the arc.
1790 arc_return_buf(arc_buf_t
*buf
, void *tag
)
1792 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1794 ASSERT(buf
->b_data
!= NULL
);
1795 ASSERT(HDR_HAS_L1HDR(hdr
));
1796 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, tag
);
1797 (void) refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
1799 atomic_add_64(&arc_loaned_bytes
, -hdr
->b_size
);
1802 /* Detach an arc_buf from a dbuf (tag) */
1804 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
1806 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1808 ASSERT(buf
->b_data
!= NULL
);
1809 ASSERT(HDR_HAS_L1HDR(hdr
));
1810 (void) refcount_add(&hdr
->b_l1hdr
.b_refcnt
, arc_onloan_tag
);
1811 (void) refcount_remove(&hdr
->b_l1hdr
.b_refcnt
, tag
);
1812 buf
->b_efunc
= NULL
;
1813 buf
->b_private
= NULL
;
1815 atomic_add_64(&arc_loaned_bytes
, hdr
->b_size
);
1819 arc_buf_clone(arc_buf_t
*from
)
1822 arc_buf_hdr_t
*hdr
= from
->b_hdr
;
1823 uint64_t size
= hdr
->b_size
;
1825 ASSERT(HDR_HAS_L1HDR(hdr
));
1826 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_anon
);
1828 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1831 buf
->b_efunc
= NULL
;
1832 buf
->b_private
= NULL
;
1833 buf
->b_next
= hdr
->b_l1hdr
.b_buf
;
1834 hdr
->b_l1hdr
.b_buf
= buf
;
1835 arc_get_data_buf(buf
);
1836 bcopy(from
->b_data
, buf
->b_data
, size
);
1839 * This buffer already exists in the arc so create a duplicate
1840 * copy for the caller. If the buffer is associated with user data
1841 * then track the size and number of duplicates. These stats will be
1842 * updated as duplicate buffers are created and destroyed.
1844 if (HDR_ISTYPE_DATA(hdr
)) {
1845 ARCSTAT_BUMP(arcstat_duplicate_buffers
);
1846 ARCSTAT_INCR(arcstat_duplicate_buffers_size
, size
);
1848 hdr
->b_l1hdr
.b_datacnt
+= 1;
1853 arc_buf_add_ref(arc_buf_t
*buf
, void* tag
)
1856 kmutex_t
*hash_lock
;
1859 * Check to see if this buffer is evicted. Callers
1860 * must verify b_data != NULL to know if the add_ref
1863 mutex_enter(&buf
->b_evict_lock
);
1864 if (buf
->b_data
== NULL
) {
1865 mutex_exit(&buf
->b_evict_lock
);
1868 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1869 mutex_enter(hash_lock
);
1871 ASSERT(HDR_HAS_L1HDR(hdr
));
1872 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1873 mutex_exit(&buf
->b_evict_lock
);
1875 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
1876 hdr
->b_l1hdr
.b_state
== arc_mfu
);
1878 add_reference(hdr
, hash_lock
, tag
);
1879 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
1880 arc_access(hdr
, hash_lock
);
1881 mutex_exit(hash_lock
);
1882 ARCSTAT_BUMP(arcstat_hits
);
1883 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
1884 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
1885 data
, metadata
, hits
);
1889 arc_buf_free_on_write(void *data
, size_t size
,
1890 void (*free_func
)(void *, size_t))
1892 l2arc_data_free_t
*df
;
1894 df
= kmem_alloc(sizeof (*df
), KM_SLEEP
);
1895 df
->l2df_data
= data
;
1896 df
->l2df_size
= size
;
1897 df
->l2df_func
= free_func
;
1898 mutex_enter(&l2arc_free_on_write_mtx
);
1899 list_insert_head(l2arc_free_on_write
, df
);
1900 mutex_exit(&l2arc_free_on_write_mtx
);
1904 * Free the arc data buffer. If it is an l2arc write in progress,
1905 * the buffer is placed on l2arc_free_on_write to be freed later.
1908 arc_buf_data_free(arc_buf_t
*buf
, void (*free_func
)(void *, size_t))
1910 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1912 if (HDR_L2_WRITING(hdr
)) {
1913 arc_buf_free_on_write(buf
->b_data
, hdr
->b_size
, free_func
);
1914 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
1916 free_func(buf
->b_data
, hdr
->b_size
);
1921 arc_buf_l2_cdata_free(arc_buf_hdr_t
*hdr
)
1923 ASSERT(HDR_HAS_L2HDR(hdr
));
1924 ASSERT(MUTEX_HELD(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
));
1927 * The b_tmp_cdata field is linked off of the b_l1hdr, so if
1928 * that doesn't exist, the header is in the arc_l2c_only state,
1929 * and there isn't anything to free (it's already been freed).
1931 if (!HDR_HAS_L1HDR(hdr
))
1935 * The header isn't being written to the l2arc device, thus it
1936 * shouldn't have a b_tmp_cdata to free.
1938 if (!HDR_L2_WRITING(hdr
)) {
1939 ASSERT3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
1944 * The header does not have compression enabled. This can be due
1945 * to the buffer not being compressible, or because we're
1946 * freeing the buffer before the second phase of
1947 * l2arc_write_buffer() has started (which does the compression
1948 * step). In either case, b_tmp_cdata does not point to a
1949 * separately compressed buffer, so there's nothing to free (it
1950 * points to the same buffer as the arc_buf_t's b_data field).
1952 if (HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
) {
1953 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
1958 * There's nothing to free since the buffer was all zero's and
1959 * compressed to a zero length buffer.
1961 if (HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_EMPTY
) {
1962 ASSERT3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
1966 ASSERT(L2ARC_IS_VALID_COMPRESS(HDR_GET_COMPRESS(hdr
)));
1968 arc_buf_free_on_write(hdr
->b_l1hdr
.b_tmp_cdata
,
1969 hdr
->b_size
, zio_data_buf_free
);
1971 ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write
);
1972 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
1976 * Free up buf->b_data and if 'remove' is set, then pull the
1977 * arc_buf_t off of the the arc_buf_hdr_t's list and free it.
1980 arc_buf_destroy(arc_buf_t
*buf
, boolean_t remove
)
1984 /* free up data associated with the buf */
1985 if (buf
->b_data
!= NULL
) {
1986 arc_state_t
*state
= buf
->b_hdr
->b_l1hdr
.b_state
;
1987 uint64_t size
= buf
->b_hdr
->b_size
;
1988 arc_buf_contents_t type
= arc_buf_type(buf
->b_hdr
);
1990 arc_cksum_verify(buf
);
1991 arc_buf_unwatch(buf
);
1993 if (type
== ARC_BUFC_METADATA
) {
1994 arc_buf_data_free(buf
, zio_buf_free
);
1995 arc_space_return(size
, ARC_SPACE_META
);
1997 ASSERT(type
== ARC_BUFC_DATA
);
1998 arc_buf_data_free(buf
, zio_data_buf_free
);
1999 arc_space_return(size
, ARC_SPACE_DATA
);
2002 /* protected by hash lock, if in the hash table */
2003 if (multilist_link_active(&buf
->b_hdr
->b_l1hdr
.b_arc_node
)) {
2004 uint64_t *cnt
= &state
->arcs_lsize
[type
];
2006 ASSERT(refcount_is_zero(
2007 &buf
->b_hdr
->b_l1hdr
.b_refcnt
));
2008 ASSERT(state
!= arc_anon
&& state
!= arc_l2c_only
);
2010 ASSERT3U(*cnt
, >=, size
);
2011 atomic_add_64(cnt
, -size
);
2014 (void) refcount_remove_many(&state
->arcs_size
, size
, buf
);
2018 * If we're destroying a duplicate buffer make sure
2019 * that the appropriate statistics are updated.
2021 if (buf
->b_hdr
->b_l1hdr
.b_datacnt
> 1 &&
2022 HDR_ISTYPE_DATA(buf
->b_hdr
)) {
2023 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers
);
2024 ARCSTAT_INCR(arcstat_duplicate_buffers_size
, -size
);
2026 ASSERT(buf
->b_hdr
->b_l1hdr
.b_datacnt
> 0);
2027 buf
->b_hdr
->b_l1hdr
.b_datacnt
-= 1;
2030 /* only remove the buf if requested */
2034 /* remove the buf from the hdr list */
2035 for (bufp
= &buf
->b_hdr
->b_l1hdr
.b_buf
; *bufp
!= buf
;
2036 bufp
= &(*bufp
)->b_next
)
2038 *bufp
= buf
->b_next
;
2041 ASSERT(buf
->b_efunc
== NULL
);
2043 /* clean up the buf */
2045 kmem_cache_free(buf_cache
, buf
);
2049 arc_hdr_l2hdr_destroy(arc_buf_hdr_t
*hdr
)
2051 l2arc_buf_hdr_t
*l2hdr
= &hdr
->b_l2hdr
;
2052 l2arc_dev_t
*dev
= l2hdr
->b_dev
;
2054 ASSERT(MUTEX_HELD(&dev
->l2ad_mtx
));
2055 ASSERT(HDR_HAS_L2HDR(hdr
));
2057 list_remove(&dev
->l2ad_buflist
, hdr
);
2059 arc_space_return(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
2062 * We don't want to leak the b_tmp_cdata buffer that was
2063 * allocated in l2arc_write_buffers()
2065 arc_buf_l2_cdata_free(hdr
);
2068 * If the l2hdr's b_daddr is equal to L2ARC_ADDR_UNSET, then
2069 * this header is being processed by l2arc_write_buffers() (i.e.
2070 * it's in the first stage of l2arc_write_buffers()).
2071 * Re-affirming that truth here, just to serve as a reminder. If
2072 * b_daddr does not equal L2ARC_ADDR_UNSET, then the header may or
2073 * may not have its HDR_L2_WRITING flag set. (the write may have
2074 * completed, in which case HDR_L2_WRITING will be false and the
2075 * b_daddr field will point to the address of the buffer on disk).
2077 IMPLY(l2hdr
->b_daddr
== L2ARC_ADDR_UNSET
, HDR_L2_WRITING(hdr
));
2080 * If b_daddr is equal to L2ARC_ADDR_UNSET, we're racing with
2081 * l2arc_write_buffers(). Since we've just removed this header
2082 * from the l2arc buffer list, this header will never reach the
2083 * second stage of l2arc_write_buffers(), which increments the
2084 * accounting stats for this header. Thus, we must be careful
2085 * not to decrement them for this header either.
2087 if (l2hdr
->b_daddr
!= L2ARC_ADDR_UNSET
) {
2088 ARCSTAT_INCR(arcstat_l2_asize
, -l2hdr
->b_asize
);
2089 ARCSTAT_INCR(arcstat_l2_size
, -hdr
->b_size
);
2091 vdev_space_update(dev
->l2ad_vdev
,
2092 -l2hdr
->b_asize
, 0, 0);
2094 (void) refcount_remove_many(&dev
->l2ad_alloc
,
2095 l2hdr
->b_asize
, hdr
);
2098 hdr
->b_flags
&= ~ARC_FLAG_HAS_L2HDR
;
2102 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
2104 if (HDR_HAS_L1HDR(hdr
)) {
2105 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
||
2106 hdr
->b_l1hdr
.b_datacnt
> 0);
2107 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2108 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
2110 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
2111 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
2113 if (HDR_HAS_L2HDR(hdr
)) {
2114 l2arc_dev_t
*dev
= hdr
->b_l2hdr
.b_dev
;
2115 boolean_t buflist_held
= MUTEX_HELD(&dev
->l2ad_mtx
);
2118 mutex_enter(&dev
->l2ad_mtx
);
2121 * Even though we checked this conditional above, we
2122 * need to check this again now that we have the
2123 * l2ad_mtx. This is because we could be racing with
2124 * another thread calling l2arc_evict() which might have
2125 * destroyed this header's L2 portion as we were waiting
2126 * to acquire the l2ad_mtx. If that happens, we don't
2127 * want to re-destroy the header's L2 portion.
2129 if (HDR_HAS_L2HDR(hdr
))
2130 arc_hdr_l2hdr_destroy(hdr
);
2133 mutex_exit(&dev
->l2ad_mtx
);
2136 if (!BUF_EMPTY(hdr
))
2137 buf_discard_identity(hdr
);
2139 if (hdr
->b_freeze_cksum
!= NULL
) {
2140 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
2141 hdr
->b_freeze_cksum
= NULL
;
2144 if (HDR_HAS_L1HDR(hdr
)) {
2145 while (hdr
->b_l1hdr
.b_buf
) {
2146 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
2148 if (buf
->b_efunc
!= NULL
) {
2149 mutex_enter(&arc_user_evicts_lock
);
2150 mutex_enter(&buf
->b_evict_lock
);
2151 ASSERT(buf
->b_hdr
!= NULL
);
2152 arc_buf_destroy(hdr
->b_l1hdr
.b_buf
, FALSE
);
2153 hdr
->b_l1hdr
.b_buf
= buf
->b_next
;
2154 buf
->b_hdr
= &arc_eviction_hdr
;
2155 buf
->b_next
= arc_eviction_list
;
2156 arc_eviction_list
= buf
;
2157 mutex_exit(&buf
->b_evict_lock
);
2158 cv_signal(&arc_user_evicts_cv
);
2159 mutex_exit(&arc_user_evicts_lock
);
2161 arc_buf_destroy(hdr
->b_l1hdr
.b_buf
, TRUE
);
2166 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
2167 if (HDR_HAS_L1HDR(hdr
)) {
2168 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
2169 ASSERT3P(hdr
->b_l1hdr
.b_acb
, ==, NULL
);
2170 kmem_cache_free(hdr_full_cache
, hdr
);
2172 kmem_cache_free(hdr_l2only_cache
, hdr
);
2177 arc_buf_free(arc_buf_t
*buf
, void *tag
)
2179 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2180 int hashed
= hdr
->b_l1hdr
.b_state
!= arc_anon
;
2182 ASSERT(buf
->b_efunc
== NULL
);
2183 ASSERT(buf
->b_data
!= NULL
);
2186 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
2188 mutex_enter(hash_lock
);
2190 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
2192 (void) remove_reference(hdr
, hash_lock
, tag
);
2193 if (hdr
->b_l1hdr
.b_datacnt
> 1) {
2194 arc_buf_destroy(buf
, TRUE
);
2196 ASSERT(buf
== hdr
->b_l1hdr
.b_buf
);
2197 ASSERT(buf
->b_efunc
== NULL
);
2198 hdr
->b_flags
|= ARC_FLAG_BUF_AVAILABLE
;
2200 mutex_exit(hash_lock
);
2201 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
2204 * We are in the middle of an async write. Don't destroy
2205 * this buffer unless the write completes before we finish
2206 * decrementing the reference count.
2208 mutex_enter(&arc_user_evicts_lock
);
2209 (void) remove_reference(hdr
, NULL
, tag
);
2210 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2211 destroy_hdr
= !HDR_IO_IN_PROGRESS(hdr
);
2212 mutex_exit(&arc_user_evicts_lock
);
2214 arc_hdr_destroy(hdr
);
2216 if (remove_reference(hdr
, NULL
, tag
) > 0)
2217 arc_buf_destroy(buf
, TRUE
);
2219 arc_hdr_destroy(hdr
);
2224 arc_buf_remove_ref(arc_buf_t
*buf
, void* tag
)
2226 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2227 kmutex_t
*hash_lock
= NULL
;
2228 boolean_t no_callback
= (buf
->b_efunc
== NULL
);
2230 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
2231 ASSERT(hdr
->b_l1hdr
.b_datacnt
== 1);
2232 arc_buf_free(buf
, tag
);
2233 return (no_callback
);
2236 hash_lock
= HDR_LOCK(hdr
);
2237 mutex_enter(hash_lock
);
2239 ASSERT(hdr
->b_l1hdr
.b_datacnt
> 0);
2240 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
2241 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_anon
);
2242 ASSERT(buf
->b_data
!= NULL
);
2244 (void) remove_reference(hdr
, hash_lock
, tag
);
2245 if (hdr
->b_l1hdr
.b_datacnt
> 1) {
2247 arc_buf_destroy(buf
, TRUE
);
2248 } else if (no_callback
) {
2249 ASSERT(hdr
->b_l1hdr
.b_buf
== buf
&& buf
->b_next
== NULL
);
2250 ASSERT(buf
->b_efunc
== NULL
);
2251 hdr
->b_flags
|= ARC_FLAG_BUF_AVAILABLE
;
2253 ASSERT(no_callback
|| hdr
->b_l1hdr
.b_datacnt
> 1 ||
2254 refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
2255 mutex_exit(hash_lock
);
2256 return (no_callback
);
2260 arc_buf_size(arc_buf_t
*buf
)
2262 return (buf
->b_hdr
->b_size
);
2266 * Called from the DMU to determine if the current buffer should be
2267 * evicted. In order to ensure proper locking, the eviction must be initiated
2268 * from the DMU. Return true if the buffer is associated with user data and
2269 * duplicate buffers still exist.
2272 arc_buf_eviction_needed(arc_buf_t
*buf
)
2275 boolean_t evict_needed
= B_FALSE
;
2277 if (zfs_disable_dup_eviction
)
2280 mutex_enter(&buf
->b_evict_lock
);
2284 * We are in arc_do_user_evicts(); let that function
2285 * perform the eviction.
2287 ASSERT(buf
->b_data
== NULL
);
2288 mutex_exit(&buf
->b_evict_lock
);
2290 } else if (buf
->b_data
== NULL
) {
2292 * We have already been added to the arc eviction list;
2293 * recommend eviction.
2295 ASSERT3P(hdr
, ==, &arc_eviction_hdr
);
2296 mutex_exit(&buf
->b_evict_lock
);
2300 if (hdr
->b_l1hdr
.b_datacnt
> 1 && HDR_ISTYPE_DATA(hdr
))
2301 evict_needed
= B_TRUE
;
2303 mutex_exit(&buf
->b_evict_lock
);
2304 return (evict_needed
);
2308 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
2309 * state of the header is dependent on its state prior to entering this
2310 * function. The following transitions are possible:
2312 * - arc_mru -> arc_mru_ghost
2313 * - arc_mfu -> arc_mfu_ghost
2314 * - arc_mru_ghost -> arc_l2c_only
2315 * - arc_mru_ghost -> deleted
2316 * - arc_mfu_ghost -> arc_l2c_only
2317 * - arc_mfu_ghost -> deleted
2320 arc_evict_hdr(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
2322 arc_state_t
*evicted_state
, *state
;
2323 int64_t bytes_evicted
= 0;
2325 ASSERT(MUTEX_HELD(hash_lock
));
2326 ASSERT(HDR_HAS_L1HDR(hdr
));
2328 state
= hdr
->b_l1hdr
.b_state
;
2329 if (GHOST_STATE(state
)) {
2330 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
2331 ASSERT(hdr
->b_l1hdr
.b_buf
== NULL
);
2334 * l2arc_write_buffers() relies on a header's L1 portion
2335 * (i.e. its b_tmp_cdata field) during its write phase.
2336 * Thus, we cannot push a header onto the arc_l2c_only
2337 * state (removing its L1 piece) until the header is
2338 * done being written to the l2arc.
2340 if (HDR_HAS_L2HDR(hdr
) && HDR_L2_WRITING(hdr
)) {
2341 ARCSTAT_BUMP(arcstat_evict_l2_skip
);
2342 return (bytes_evicted
);
2345 ARCSTAT_BUMP(arcstat_deleted
);
2346 bytes_evicted
+= hdr
->b_size
;
2348 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, hdr
);
2350 if (HDR_HAS_L2HDR(hdr
)) {
2352 * This buffer is cached on the 2nd Level ARC;
2353 * don't destroy the header.
2355 arc_change_state(arc_l2c_only
, hdr
, hash_lock
);
2357 * dropping from L1+L2 cached to L2-only,
2358 * realloc to remove the L1 header.
2360 hdr
= arc_hdr_realloc(hdr
, hdr_full_cache
,
2363 arc_change_state(arc_anon
, hdr
, hash_lock
);
2364 arc_hdr_destroy(hdr
);
2366 return (bytes_evicted
);
2369 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
2370 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
2372 /* prefetch buffers have a minimum lifespan */
2373 if (HDR_IO_IN_PROGRESS(hdr
) ||
2374 ((hdr
->b_flags
& (ARC_FLAG_PREFETCH
| ARC_FLAG_INDIRECT
)) &&
2375 ddi_get_lbolt() - hdr
->b_l1hdr
.b_arc_access
<
2376 arc_min_prefetch_lifespan
)) {
2377 ARCSTAT_BUMP(arcstat_evict_skip
);
2378 return (bytes_evicted
);
2381 ASSERT0(refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
2382 ASSERT3U(hdr
->b_l1hdr
.b_datacnt
, >, 0);
2383 while (hdr
->b_l1hdr
.b_buf
) {
2384 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
2385 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
2386 ARCSTAT_BUMP(arcstat_mutex_miss
);
2389 if (buf
->b_data
!= NULL
)
2390 bytes_evicted
+= hdr
->b_size
;
2391 if (buf
->b_efunc
!= NULL
) {
2392 mutex_enter(&arc_user_evicts_lock
);
2393 arc_buf_destroy(buf
, FALSE
);
2394 hdr
->b_l1hdr
.b_buf
= buf
->b_next
;
2395 buf
->b_hdr
= &arc_eviction_hdr
;
2396 buf
->b_next
= arc_eviction_list
;
2397 arc_eviction_list
= buf
;
2398 cv_signal(&arc_user_evicts_cv
);
2399 mutex_exit(&arc_user_evicts_lock
);
2400 mutex_exit(&buf
->b_evict_lock
);
2402 mutex_exit(&buf
->b_evict_lock
);
2403 arc_buf_destroy(buf
, TRUE
);
2407 if (HDR_HAS_L2HDR(hdr
)) {
2408 ARCSTAT_INCR(arcstat_evict_l2_cached
, hdr
->b_size
);
2410 if (l2arc_write_eligible(hdr
->b_spa
, hdr
))
2411 ARCSTAT_INCR(arcstat_evict_l2_eligible
, hdr
->b_size
);
2413 ARCSTAT_INCR(arcstat_evict_l2_ineligible
, hdr
->b_size
);
2416 if (hdr
->b_l1hdr
.b_datacnt
== 0) {
2417 arc_change_state(evicted_state
, hdr
, hash_lock
);
2418 ASSERT(HDR_IN_HASH_TABLE(hdr
));
2419 hdr
->b_flags
|= ARC_FLAG_IN_HASH_TABLE
;
2420 hdr
->b_flags
&= ~ARC_FLAG_BUF_AVAILABLE
;
2421 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, hdr
);
2424 return (bytes_evicted
);
2428 arc_evict_state_impl(multilist_t
*ml
, int idx
, arc_buf_hdr_t
*marker
,
2429 uint64_t spa
, int64_t bytes
)
2431 multilist_sublist_t
*mls
;
2432 uint64_t bytes_evicted
= 0;
2434 kmutex_t
*hash_lock
;
2435 int evict_count
= 0;
2437 ASSERT3P(marker
, !=, NULL
);
2438 ASSERTV(if (bytes
< 0) ASSERT(bytes
== ARC_EVICT_ALL
));
2440 mls
= multilist_sublist_lock(ml
, idx
);
2442 for (hdr
= multilist_sublist_prev(mls
, marker
); hdr
!= NULL
;
2443 hdr
= multilist_sublist_prev(mls
, marker
)) {
2444 if ((bytes
!= ARC_EVICT_ALL
&& bytes_evicted
>= bytes
) ||
2445 (evict_count
>= zfs_arc_evict_batch_limit
))
2449 * To keep our iteration location, move the marker
2450 * forward. Since we're not holding hdr's hash lock, we
2451 * must be very careful and not remove 'hdr' from the
2452 * sublist. Otherwise, other consumers might mistake the
2453 * 'hdr' as not being on a sublist when they call the
2454 * multilist_link_active() function (they all rely on
2455 * the hash lock protecting concurrent insertions and
2456 * removals). multilist_sublist_move_forward() was
2457 * specifically implemented to ensure this is the case
2458 * (only 'marker' will be removed and re-inserted).
2460 multilist_sublist_move_forward(mls
, marker
);
2463 * The only case where the b_spa field should ever be
2464 * zero, is the marker headers inserted by
2465 * arc_evict_state(). It's possible for multiple threads
2466 * to be calling arc_evict_state() concurrently (e.g.
2467 * dsl_pool_close() and zio_inject_fault()), so we must
2468 * skip any markers we see from these other threads.
2470 if (hdr
->b_spa
== 0)
2473 /* we're only interested in evicting buffers of a certain spa */
2474 if (spa
!= 0 && hdr
->b_spa
!= spa
) {
2475 ARCSTAT_BUMP(arcstat_evict_skip
);
2479 hash_lock
= HDR_LOCK(hdr
);
2482 * We aren't calling this function from any code path
2483 * that would already be holding a hash lock, so we're
2484 * asserting on this assumption to be defensive in case
2485 * this ever changes. Without this check, it would be
2486 * possible to incorrectly increment arcstat_mutex_miss
2487 * below (e.g. if the code changed such that we called
2488 * this function with a hash lock held).
2490 ASSERT(!MUTEX_HELD(hash_lock
));
2492 if (mutex_tryenter(hash_lock
)) {
2493 uint64_t evicted
= arc_evict_hdr(hdr
, hash_lock
);
2494 mutex_exit(hash_lock
);
2496 bytes_evicted
+= evicted
;
2499 * If evicted is zero, arc_evict_hdr() must have
2500 * decided to skip this header, don't increment
2501 * evict_count in this case.
2507 * If arc_size isn't overflowing, signal any
2508 * threads that might happen to be waiting.
2510 * For each header evicted, we wake up a single
2511 * thread. If we used cv_broadcast, we could
2512 * wake up "too many" threads causing arc_size
2513 * to significantly overflow arc_c; since
2514 * arc_get_data_buf() doesn't check for overflow
2515 * when it's woken up (it doesn't because it's
2516 * possible for the ARC to be overflowing while
2517 * full of un-evictable buffers, and the
2518 * function should proceed in this case).
2520 * If threads are left sleeping, due to not
2521 * using cv_broadcast, they will be woken up
2522 * just before arc_reclaim_thread() sleeps.
2524 mutex_enter(&arc_reclaim_lock
);
2525 if (!arc_is_overflowing())
2526 cv_signal(&arc_reclaim_waiters_cv
);
2527 mutex_exit(&arc_reclaim_lock
);
2529 ARCSTAT_BUMP(arcstat_mutex_miss
);
2533 multilist_sublist_unlock(mls
);
2535 return (bytes_evicted
);
2539 * Evict buffers from the given arc state, until we've removed the
2540 * specified number of bytes. Move the removed buffers to the
2541 * appropriate evict state.
2543 * This function makes a "best effort". It skips over any buffers
2544 * it can't get a hash_lock on, and so, may not catch all candidates.
2545 * It may also return without evicting as much space as requested.
2547 * If bytes is specified using the special value ARC_EVICT_ALL, this
2548 * will evict all available (i.e. unlocked and evictable) buffers from
2549 * the given arc state; which is used by arc_flush().
2552 arc_evict_state(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
2553 arc_buf_contents_t type
)
2555 uint64_t total_evicted
= 0;
2556 multilist_t
*ml
= &state
->arcs_list
[type
];
2558 arc_buf_hdr_t
**markers
;
2561 ASSERTV(if (bytes
< 0) ASSERT(bytes
== ARC_EVICT_ALL
));
2563 num_sublists
= multilist_get_num_sublists(ml
);
2566 * If we've tried to evict from each sublist, made some
2567 * progress, but still have not hit the target number of bytes
2568 * to evict, we want to keep trying. The markers allow us to
2569 * pick up where we left off for each individual sublist, rather
2570 * than starting from the tail each time.
2572 markers
= kmem_zalloc(sizeof (*markers
) * num_sublists
, KM_SLEEP
);
2573 for (i
= 0; i
< num_sublists
; i
++) {
2574 multilist_sublist_t
*mls
;
2576 markers
[i
] = kmem_cache_alloc(hdr_full_cache
, KM_SLEEP
);
2579 * A b_spa of 0 is used to indicate that this header is
2580 * a marker. This fact is used in arc_adjust_type() and
2581 * arc_evict_state_impl().
2583 markers
[i
]->b_spa
= 0;
2585 mls
= multilist_sublist_lock(ml
, i
);
2586 multilist_sublist_insert_tail(mls
, markers
[i
]);
2587 multilist_sublist_unlock(mls
);
2591 * While we haven't hit our target number of bytes to evict, or
2592 * we're evicting all available buffers.
2594 while (total_evicted
< bytes
|| bytes
== ARC_EVICT_ALL
) {
2596 * Start eviction using a randomly selected sublist,
2597 * this is to try and evenly balance eviction across all
2598 * sublists. Always starting at the same sublist
2599 * (e.g. index 0) would cause evictions to favor certain
2600 * sublists over others.
2602 int sublist_idx
= multilist_get_random_index(ml
);
2603 uint64_t scan_evicted
= 0;
2605 for (i
= 0; i
< num_sublists
; i
++) {
2606 uint64_t bytes_remaining
;
2607 uint64_t bytes_evicted
;
2609 if (bytes
== ARC_EVICT_ALL
)
2610 bytes_remaining
= ARC_EVICT_ALL
;
2611 else if (total_evicted
< bytes
)
2612 bytes_remaining
= bytes
- total_evicted
;
2616 bytes_evicted
= arc_evict_state_impl(ml
, sublist_idx
,
2617 markers
[sublist_idx
], spa
, bytes_remaining
);
2619 scan_evicted
+= bytes_evicted
;
2620 total_evicted
+= bytes_evicted
;
2622 /* we've reached the end, wrap to the beginning */
2623 if (++sublist_idx
>= num_sublists
)
2628 * If we didn't evict anything during this scan, we have
2629 * no reason to believe we'll evict more during another
2630 * scan, so break the loop.
2632 if (scan_evicted
== 0) {
2633 /* This isn't possible, let's make that obvious */
2634 ASSERT3S(bytes
, !=, 0);
2637 * When bytes is ARC_EVICT_ALL, the only way to
2638 * break the loop is when scan_evicted is zero.
2639 * In that case, we actually have evicted enough,
2640 * so we don't want to increment the kstat.
2642 if (bytes
!= ARC_EVICT_ALL
) {
2643 ASSERT3S(total_evicted
, <, bytes
);
2644 ARCSTAT_BUMP(arcstat_evict_not_enough
);
2651 for (i
= 0; i
< num_sublists
; i
++) {
2652 multilist_sublist_t
*mls
= multilist_sublist_lock(ml
, i
);
2653 multilist_sublist_remove(mls
, markers
[i
]);
2654 multilist_sublist_unlock(mls
);
2656 kmem_cache_free(hdr_full_cache
, markers
[i
]);
2658 kmem_free(markers
, sizeof (*markers
) * num_sublists
);
2660 return (total_evicted
);
2664 * Flush all "evictable" data of the given type from the arc state
2665 * specified. This will not evict any "active" buffers (i.e. referenced).
2667 * When 'retry' is set to FALSE, the function will make a single pass
2668 * over the state and evict any buffers that it can. Since it doesn't
2669 * continually retry the eviction, it might end up leaving some buffers
2670 * in the ARC due to lock misses.
2672 * When 'retry' is set to TRUE, the function will continually retry the
2673 * eviction until *all* evictable buffers have been removed from the
2674 * state. As a result, if concurrent insertions into the state are
2675 * allowed (e.g. if the ARC isn't shutting down), this function might
2676 * wind up in an infinite loop, continually trying to evict buffers.
2679 arc_flush_state(arc_state_t
*state
, uint64_t spa
, arc_buf_contents_t type
,
2682 uint64_t evicted
= 0;
2684 while (state
->arcs_lsize
[type
] != 0) {
2685 evicted
+= arc_evict_state(state
, spa
, ARC_EVICT_ALL
, type
);
2695 * Helper function for arc_prune() it is responsible for safely handling
2696 * the execution of a registered arc_prune_func_t.
2699 arc_prune_task(void *ptr
)
2701 arc_prune_t
*ap
= (arc_prune_t
*)ptr
;
2702 arc_prune_func_t
*func
= ap
->p_pfunc
;
2705 func(ap
->p_adjust
, ap
->p_private
);
2707 /* Callback unregistered concurrently with execution */
2708 if (refcount_remove(&ap
->p_refcnt
, func
) == 0) {
2709 ASSERT(!list_link_active(&ap
->p_node
));
2710 refcount_destroy(&ap
->p_refcnt
);
2711 kmem_free(ap
, sizeof (*ap
));
2716 * Notify registered consumers they must drop holds on a portion of the ARC
2717 * buffered they reference. This provides a mechanism to ensure the ARC can
2718 * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This
2719 * is analogous to dnlc_reduce_cache() but more generic.
2721 * This operation is performed asyncronously so it may be safely called
2722 * in the context of the arc_reclaim_thread(). A reference is taken here
2723 * for each registered arc_prune_t and the arc_prune_task() is responsible
2724 * for releasing it once the registered arc_prune_func_t has completed.
2727 arc_prune_async(int64_t adjust
)
2731 mutex_enter(&arc_prune_mtx
);
2732 for (ap
= list_head(&arc_prune_list
); ap
!= NULL
;
2733 ap
= list_next(&arc_prune_list
, ap
)) {
2735 if (refcount_count(&ap
->p_refcnt
) >= 2)
2738 refcount_add(&ap
->p_refcnt
, ap
->p_pfunc
);
2739 ap
->p_adjust
= adjust
;
2740 taskq_dispatch(arc_prune_taskq
, arc_prune_task
, ap
, TQ_SLEEP
);
2741 ARCSTAT_BUMP(arcstat_prune
);
2743 mutex_exit(&arc_prune_mtx
);
2747 arc_prune(int64_t adjust
)
2749 arc_prune_async(adjust
);
2750 taskq_wait_outstanding(arc_prune_taskq
, 0);
2754 * Evict the specified number of bytes from the state specified,
2755 * restricting eviction to the spa and type given. This function
2756 * prevents us from trying to evict more from a state's list than
2757 * is "evictable", and to skip evicting altogether when passed a
2758 * negative value for "bytes". In contrast, arc_evict_state() will
2759 * evict everything it can, when passed a negative value for "bytes".
2762 arc_adjust_impl(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
2763 arc_buf_contents_t type
)
2767 if (bytes
> 0 && state
->arcs_lsize
[type
] > 0) {
2768 delta
= MIN(state
->arcs_lsize
[type
], bytes
);
2769 return (arc_evict_state(state
, spa
, delta
, type
));
2776 * The goal of this function is to evict enough meta data buffers from the
2777 * ARC in order to enforce the arc_meta_limit. Achieving this is slightly
2778 * more complicated than it appears because it is common for data buffers
2779 * to have holds on meta data buffers. In addition, dnode meta data buffers
2780 * will be held by the dnodes in the block preventing them from being freed.
2781 * This means we can't simply traverse the ARC and expect to always find
2782 * enough unheld meta data buffer to release.
2784 * Therefore, this function has been updated to make alternating passes
2785 * over the ARC releasing data buffers and then newly unheld meta data
2786 * buffers. This ensures forward progress is maintained and arc_meta_used
2787 * will decrease. Normally this is sufficient, but if required the ARC
2788 * will call the registered prune callbacks causing dentry and inodes to
2789 * be dropped from the VFS cache. This will make dnode meta data buffers
2790 * available for reclaim.
2793 arc_adjust_meta_balanced(void)
2795 int64_t adjustmnt
, delta
, prune
= 0;
2796 uint64_t total_evicted
= 0;
2797 arc_buf_contents_t type
= ARC_BUFC_DATA
;
2798 int restarts
= MAX(zfs_arc_meta_adjust_restarts
, 0);
2802 * This slightly differs than the way we evict from the mru in
2803 * arc_adjust because we don't have a "target" value (i.e. no
2804 * "meta" arc_p). As a result, I think we can completely
2805 * cannibalize the metadata in the MRU before we evict the
2806 * metadata from the MFU. I think we probably need to implement a
2807 * "metadata arc_p" value to do this properly.
2809 adjustmnt
= arc_meta_used
- arc_meta_limit
;
2811 if (adjustmnt
> 0 && arc_mru
->arcs_lsize
[type
] > 0) {
2812 delta
= MIN(arc_mru
->arcs_lsize
[type
], adjustmnt
);
2813 total_evicted
+= arc_adjust_impl(arc_mru
, 0, delta
, type
);
2818 * We can't afford to recalculate adjustmnt here. If we do,
2819 * new metadata buffers can sneak into the MRU or ANON lists,
2820 * thus penalize the MFU metadata. Although the fudge factor is
2821 * small, it has been empirically shown to be significant for
2822 * certain workloads (e.g. creating many empty directories). As
2823 * such, we use the original calculation for adjustmnt, and
2824 * simply decrement the amount of data evicted from the MRU.
2827 if (adjustmnt
> 0 && arc_mfu
->arcs_lsize
[type
] > 0) {
2828 delta
= MIN(arc_mfu
->arcs_lsize
[type
], adjustmnt
);
2829 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, delta
, type
);
2832 adjustmnt
= arc_meta_used
- arc_meta_limit
;
2834 if (adjustmnt
> 0 && arc_mru_ghost
->arcs_lsize
[type
] > 0) {
2835 delta
= MIN(adjustmnt
,
2836 arc_mru_ghost
->arcs_lsize
[type
]);
2837 total_evicted
+= arc_adjust_impl(arc_mru_ghost
, 0, delta
, type
);
2841 if (adjustmnt
> 0 && arc_mfu_ghost
->arcs_lsize
[type
] > 0) {
2842 delta
= MIN(adjustmnt
,
2843 arc_mfu_ghost
->arcs_lsize
[type
]);
2844 total_evicted
+= arc_adjust_impl(arc_mfu_ghost
, 0, delta
, type
);
2848 * If after attempting to make the requested adjustment to the ARC
2849 * the meta limit is still being exceeded then request that the
2850 * higher layers drop some cached objects which have holds on ARC
2851 * meta buffers. Requests to the upper layers will be made with
2852 * increasingly large scan sizes until the ARC is below the limit.
2854 if (arc_meta_used
> arc_meta_limit
) {
2855 if (type
== ARC_BUFC_DATA
) {
2856 type
= ARC_BUFC_METADATA
;
2858 type
= ARC_BUFC_DATA
;
2860 if (zfs_arc_meta_prune
) {
2861 prune
+= zfs_arc_meta_prune
;
2862 arc_prune_async(prune
);
2871 return (total_evicted
);
2875 * Evict metadata buffers from the cache, such that arc_meta_used is
2876 * capped by the arc_meta_limit tunable.
2879 arc_adjust_meta_only(void)
2881 uint64_t total_evicted
= 0;
2885 * If we're over the meta limit, we want to evict enough
2886 * metadata to get back under the meta limit. We don't want to
2887 * evict so much that we drop the MRU below arc_p, though. If
2888 * we're over the meta limit more than we're over arc_p, we
2889 * evict some from the MRU here, and some from the MFU below.
2891 target
= MIN((int64_t)(arc_meta_used
- arc_meta_limit
),
2892 (int64_t)(refcount_count(&arc_anon
->arcs_size
) +
2893 refcount_count(&arc_mru
->arcs_size
) - arc_p
));
2895 total_evicted
+= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
2898 * Similar to the above, we want to evict enough bytes to get us
2899 * below the meta limit, but not so much as to drop us below the
2900 * space alloted to the MFU (which is defined as arc_c - arc_p).
2902 target
= MIN((int64_t)(arc_meta_used
- arc_meta_limit
),
2903 (int64_t)(refcount_count(&arc_mfu
->arcs_size
) - (arc_c
- arc_p
)));
2905 total_evicted
+= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
2907 return (total_evicted
);
2911 arc_adjust_meta(void)
2913 if (zfs_arc_meta_strategy
== ARC_STRATEGY_META_ONLY
)
2914 return (arc_adjust_meta_only());
2916 return (arc_adjust_meta_balanced());
2920 * Return the type of the oldest buffer in the given arc state
2922 * This function will select a random sublist of type ARC_BUFC_DATA and
2923 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist
2924 * is compared, and the type which contains the "older" buffer will be
2927 static arc_buf_contents_t
2928 arc_adjust_type(arc_state_t
*state
)
2930 multilist_t
*data_ml
= &state
->arcs_list
[ARC_BUFC_DATA
];
2931 multilist_t
*meta_ml
= &state
->arcs_list
[ARC_BUFC_METADATA
];
2932 int data_idx
= multilist_get_random_index(data_ml
);
2933 int meta_idx
= multilist_get_random_index(meta_ml
);
2934 multilist_sublist_t
*data_mls
;
2935 multilist_sublist_t
*meta_mls
;
2936 arc_buf_contents_t type
;
2937 arc_buf_hdr_t
*data_hdr
;
2938 arc_buf_hdr_t
*meta_hdr
;
2941 * We keep the sublist lock until we're finished, to prevent
2942 * the headers from being destroyed via arc_evict_state().
2944 data_mls
= multilist_sublist_lock(data_ml
, data_idx
);
2945 meta_mls
= multilist_sublist_lock(meta_ml
, meta_idx
);
2948 * These two loops are to ensure we skip any markers that
2949 * might be at the tail of the lists due to arc_evict_state().
2952 for (data_hdr
= multilist_sublist_tail(data_mls
); data_hdr
!= NULL
;
2953 data_hdr
= multilist_sublist_prev(data_mls
, data_hdr
)) {
2954 if (data_hdr
->b_spa
!= 0)
2958 for (meta_hdr
= multilist_sublist_tail(meta_mls
); meta_hdr
!= NULL
;
2959 meta_hdr
= multilist_sublist_prev(meta_mls
, meta_hdr
)) {
2960 if (meta_hdr
->b_spa
!= 0)
2964 if (data_hdr
== NULL
&& meta_hdr
== NULL
) {
2965 type
= ARC_BUFC_DATA
;
2966 } else if (data_hdr
== NULL
) {
2967 ASSERT3P(meta_hdr
, !=, NULL
);
2968 type
= ARC_BUFC_METADATA
;
2969 } else if (meta_hdr
== NULL
) {
2970 ASSERT3P(data_hdr
, !=, NULL
);
2971 type
= ARC_BUFC_DATA
;
2973 ASSERT3P(data_hdr
, !=, NULL
);
2974 ASSERT3P(meta_hdr
, !=, NULL
);
2976 /* The headers can't be on the sublist without an L1 header */
2977 ASSERT(HDR_HAS_L1HDR(data_hdr
));
2978 ASSERT(HDR_HAS_L1HDR(meta_hdr
));
2980 if (data_hdr
->b_l1hdr
.b_arc_access
<
2981 meta_hdr
->b_l1hdr
.b_arc_access
) {
2982 type
= ARC_BUFC_DATA
;
2984 type
= ARC_BUFC_METADATA
;
2988 multilist_sublist_unlock(meta_mls
);
2989 multilist_sublist_unlock(data_mls
);
2995 * Evict buffers from the cache, such that arc_size is capped by arc_c.
3000 uint64_t total_evicted
= 0;
3005 * If we're over arc_meta_limit, we want to correct that before
3006 * potentially evicting data buffers below.
3008 total_evicted
+= arc_adjust_meta();
3013 * If we're over the target cache size, we want to evict enough
3014 * from the list to get back to our target size. We don't want
3015 * to evict too much from the MRU, such that it drops below
3016 * arc_p. So, if we're over our target cache size more than
3017 * the MRU is over arc_p, we'll evict enough to get back to
3018 * arc_p here, and then evict more from the MFU below.
3020 target
= MIN((int64_t)(arc_size
- arc_c
),
3021 (int64_t)(refcount_count(&arc_anon
->arcs_size
) +
3022 refcount_count(&arc_mru
->arcs_size
) + arc_meta_used
- arc_p
));
3025 * If we're below arc_meta_min, always prefer to evict data.
3026 * Otherwise, try to satisfy the requested number of bytes to
3027 * evict from the type which contains older buffers; in an
3028 * effort to keep newer buffers in the cache regardless of their
3029 * type. If we cannot satisfy the number of bytes from this
3030 * type, spill over into the next type.
3032 if (arc_adjust_type(arc_mru
) == ARC_BUFC_METADATA
&&
3033 arc_meta_used
> arc_meta_min
) {
3034 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
3035 total_evicted
+= bytes
;
3038 * If we couldn't evict our target number of bytes from
3039 * metadata, we try to get the rest from data.
3044 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
3046 bytes
= arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_DATA
);
3047 total_evicted
+= bytes
;
3050 * If we couldn't evict our target number of bytes from
3051 * data, we try to get the rest from metadata.
3056 arc_adjust_impl(arc_mru
, 0, target
, ARC_BUFC_METADATA
);
3062 * Now that we've tried to evict enough from the MRU to get its
3063 * size back to arc_p, if we're still above the target cache
3064 * size, we evict the rest from the MFU.
3066 target
= arc_size
- arc_c
;
3068 if (arc_adjust_type(arc_mfu
) == ARC_BUFC_METADATA
&&
3069 arc_meta_used
> arc_meta_min
) {
3070 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
3071 total_evicted
+= bytes
;
3074 * If we couldn't evict our target number of bytes from
3075 * metadata, we try to get the rest from data.
3080 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
3082 bytes
= arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_DATA
);
3083 total_evicted
+= bytes
;
3086 * If we couldn't evict our target number of bytes from
3087 * data, we try to get the rest from data.
3092 arc_adjust_impl(arc_mfu
, 0, target
, ARC_BUFC_METADATA
);
3096 * Adjust ghost lists
3098 * In addition to the above, the ARC also defines target values
3099 * for the ghost lists. The sum of the mru list and mru ghost
3100 * list should never exceed the target size of the cache, and
3101 * the sum of the mru list, mfu list, mru ghost list, and mfu
3102 * ghost list should never exceed twice the target size of the
3103 * cache. The following logic enforces these limits on the ghost
3104 * caches, and evicts from them as needed.
3106 target
= refcount_count(&arc_mru
->arcs_size
) +
3107 refcount_count(&arc_mru_ghost
->arcs_size
) - arc_c
;
3109 bytes
= arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_DATA
);
3110 total_evicted
+= bytes
;
3115 arc_adjust_impl(arc_mru_ghost
, 0, target
, ARC_BUFC_METADATA
);
3118 * We assume the sum of the mru list and mfu list is less than
3119 * or equal to arc_c (we enforced this above), which means we
3120 * can use the simpler of the two equations below:
3122 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c
3123 * mru ghost + mfu ghost <= arc_c
3125 target
= refcount_count(&arc_mru_ghost
->arcs_size
) +
3126 refcount_count(&arc_mfu_ghost
->arcs_size
) - arc_c
;
3128 bytes
= arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_DATA
);
3129 total_evicted
+= bytes
;
3134 arc_adjust_impl(arc_mfu_ghost
, 0, target
, ARC_BUFC_METADATA
);
3136 return (total_evicted
);
3140 arc_do_user_evicts(void)
3142 mutex_enter(&arc_user_evicts_lock
);
3143 while (arc_eviction_list
!= NULL
) {
3144 arc_buf_t
*buf
= arc_eviction_list
;
3145 arc_eviction_list
= buf
->b_next
;
3146 mutex_enter(&buf
->b_evict_lock
);
3148 mutex_exit(&buf
->b_evict_lock
);
3149 mutex_exit(&arc_user_evicts_lock
);
3151 if (buf
->b_efunc
!= NULL
)
3152 VERIFY0(buf
->b_efunc(buf
->b_private
));
3154 buf
->b_efunc
= NULL
;
3155 buf
->b_private
= NULL
;
3156 kmem_cache_free(buf_cache
, buf
);
3157 mutex_enter(&arc_user_evicts_lock
);
3159 mutex_exit(&arc_user_evicts_lock
);
3163 arc_flush(spa_t
*spa
, boolean_t retry
)
3168 * If retry is TRUE, a spa must not be specified since we have
3169 * no good way to determine if all of a spa's buffers have been
3170 * evicted from an arc state.
3172 ASSERT(!retry
|| spa
== 0);
3175 guid
= spa_load_guid(spa
);
3177 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_DATA
, retry
);
3178 (void) arc_flush_state(arc_mru
, guid
, ARC_BUFC_METADATA
, retry
);
3180 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_DATA
, retry
);
3181 (void) arc_flush_state(arc_mfu
, guid
, ARC_BUFC_METADATA
, retry
);
3183 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_DATA
, retry
);
3184 (void) arc_flush_state(arc_mru_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
3186 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_DATA
, retry
);
3187 (void) arc_flush_state(arc_mfu_ghost
, guid
, ARC_BUFC_METADATA
, retry
);
3189 arc_do_user_evicts();
3190 ASSERT(spa
|| arc_eviction_list
== NULL
);
3194 arc_shrink(int64_t to_free
)
3196 if (arc_c
> arc_c_min
) {
3198 if (arc_c
> arc_c_min
+ to_free
)
3199 atomic_add_64(&arc_c
, -to_free
);
3203 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
3204 if (arc_c
> arc_size
)
3205 arc_c
= MAX(arc_size
, arc_c_min
);
3207 arc_p
= (arc_c
>> 1);
3208 ASSERT(arc_c
>= arc_c_min
);
3209 ASSERT((int64_t)arc_p
>= 0);
3212 if (arc_size
> arc_c
)
3213 (void) arc_adjust();
3216 typedef enum free_memory_reason_t
{
3221 FMR_PAGES_PP_MAXIMUM
,
3224 } free_memory_reason_t
;
3226 int64_t last_free_memory
;
3227 free_memory_reason_t last_free_reason
;
3232 * expiration time for arc_no_grow set by direct memory reclaim.
3234 static clock_t arc_grow_time
= 0;
3237 * Additional reserve of pages for pp_reserve.
3239 int64_t arc_pages_pp_reserve
= 64;
3242 * Additional reserve of pages for swapfs.
3244 int64_t arc_swapfs_reserve
= 64;
3246 #endif /* _KERNEL */
3249 * Return the amount of memory that can be consumed before reclaim will be
3250 * needed. Positive if there is sufficient free memory, negative indicates
3251 * the amount of memory that needs to be freed up.
3254 arc_available_memory(void)
3256 int64_t lowest
= INT64_MAX
;
3257 free_memory_reason_t r
= FMR_UNKNOWN
;
3262 * Under Linux we are not allowed to directly interrogate the global
3263 * memory state. Instead rely on observing that direct reclaim has
3264 * recently occurred therefore the system must be low on memory. The
3265 * exact values returned are not critical but should be small.
3267 if (ddi_time_after_eq(ddi_get_lbolt(), arc_grow_time
))
3270 lowest
= -PAGE_SIZE
;
3275 * Platforms like illumos have greater visibility in to the memory
3276 * subsystem and can return a more detailed analysis of memory.
3279 n
= PAGESIZE
* (-needfree
);
3287 * check that we're out of range of the pageout scanner. It starts to
3288 * schedule paging if freemem is less than lotsfree and needfree.
3289 * lotsfree is the high-water mark for pageout, and needfree is the
3290 * number of needed free pages. We add extra pages here to make sure
3291 * the scanner doesn't start up while we're freeing memory.
3293 n
= PAGESIZE
* (freemem
- lotsfree
- needfree
- desfree
);
3300 * check to make sure that swapfs has enough space so that anon
3301 * reservations can still succeed. anon_resvmem() checks that the
3302 * availrmem is greater than swapfs_minfree, and the number of reserved
3303 * swap pages. We also add a bit of extra here just to prevent
3304 * circumstances from getting really dire.
3306 n
= PAGESIZE
* (availrmem
- swapfs_minfree
- swapfs_reserve
-
3307 desfree
- arc_swapfs_reserve
);
3310 r
= FMR_SWAPFS_MINFREE
;
3315 * Check that we have enough availrmem that memory locking (e.g., via
3316 * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum
3317 * stores the number of pages that cannot be locked; when availrmem
3318 * drops below pages_pp_maximum, page locking mechanisms such as
3319 * page_pp_lock() will fail.)
3321 n
= PAGESIZE
* (availrmem
- pages_pp_maximum
-
3322 arc_pages_pp_reserve
);
3325 r
= FMR_PAGES_PP_MAXIMUM
;
3330 * If we're on an i386 platform, it's possible that we'll exhaust the
3331 * kernel heap space before we ever run out of available physical
3332 * memory. Most checks of the size of the heap_area compare against
3333 * tune.t_minarmem, which is the minimum available real memory that we
3334 * can have in the system. However, this is generally fixed at 25 pages
3335 * which is so low that it's useless. In this comparison, we seek to
3336 * calculate the total heap-size, and reclaim if more than 3/4ths of the
3337 * heap is allocated. (Or, in the calculation, if less than 1/4th is
3340 n
= vmem_size(heap_arena
, VMEM_FREE
) -
3341 (vmem_size(heap_arena
, VMEM_FREE
| VMEM_ALLOC
) >> 2);
3349 * If zio data pages are being allocated out of a separate heap segment,
3350 * then enforce that the size of available vmem for this arena remains
3351 * above about 1/16th free.
3353 * Note: The 1/16th arena free requirement was put in place
3354 * to aggressively evict memory from the arc in order to avoid
3355 * memory fragmentation issues.
3357 if (zio_arena
!= NULL
) {
3358 n
= vmem_size(zio_arena
, VMEM_FREE
) -
3359 (vmem_size(zio_arena
, VMEM_ALLOC
) >> 4);
3365 #endif /* __linux__ */
3367 /* Every 100 calls, free a small amount */
3368 if (spa_get_random(100) == 0)
3372 last_free_memory
= lowest
;
3373 last_free_reason
= r
;
3379 * Determine if the system is under memory pressure and is asking
3380 * to reclaim memory. A return value of TRUE indicates that the system
3381 * is under memory pressure and that the arc should adjust accordingly.
3384 arc_reclaim_needed(void)
3386 return (arc_available_memory() < 0);
3390 arc_kmem_reap_now(void)
3393 kmem_cache_t
*prev_cache
= NULL
;
3394 kmem_cache_t
*prev_data_cache
= NULL
;
3395 extern kmem_cache_t
*zio_buf_cache
[];
3396 extern kmem_cache_t
*zio_data_buf_cache
[];
3397 extern kmem_cache_t
*range_seg_cache
;
3399 if ((arc_meta_used
>= arc_meta_limit
) && zfs_arc_meta_prune
) {
3401 * We are exceeding our meta-data cache limit.
3402 * Prune some entries to release holds on meta-data.
3404 arc_prune(zfs_arc_meta_prune
);
3407 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
3408 if (zio_buf_cache
[i
] != prev_cache
) {
3409 prev_cache
= zio_buf_cache
[i
];
3410 kmem_cache_reap_now(zio_buf_cache
[i
]);
3412 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
3413 prev_data_cache
= zio_data_buf_cache
[i
];
3414 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
3417 kmem_cache_reap_now(buf_cache
);
3418 kmem_cache_reap_now(hdr_full_cache
);
3419 kmem_cache_reap_now(hdr_l2only_cache
);
3420 kmem_cache_reap_now(range_seg_cache
);
3422 if (zio_arena
!= NULL
) {
3424 * Ask the vmem arena to reclaim unused memory from its
3427 vmem_qcache_reap(zio_arena
);
3432 * Threads can block in arc_get_data_buf() waiting for this thread to evict
3433 * enough data and signal them to proceed. When this happens, the threads in
3434 * arc_get_data_buf() are sleeping while holding the hash lock for their
3435 * particular arc header. Thus, we must be careful to never sleep on a
3436 * hash lock in this thread. This is to prevent the following deadlock:
3438 * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L",
3439 * waiting for the reclaim thread to signal it.
3441 * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter,
3442 * fails, and goes to sleep forever.
3444 * This possible deadlock is avoided by always acquiring a hash lock
3445 * using mutex_tryenter() from arc_reclaim_thread().
3448 arc_reclaim_thread(void)
3450 fstrans_cookie_t cookie
= spl_fstrans_mark();
3451 clock_t growtime
= 0;
3454 CALLB_CPR_INIT(&cpr
, &arc_reclaim_lock
, callb_generic_cpr
, FTAG
);
3456 mutex_enter(&arc_reclaim_lock
);
3457 while (!arc_reclaim_thread_exit
) {
3459 int64_t free_memory
= arc_available_memory();
3460 uint64_t evicted
= 0;
3462 arc_tuning_update();
3464 mutex_exit(&arc_reclaim_lock
);
3466 if (free_memory
< 0) {
3468 arc_no_grow
= B_TRUE
;
3472 * Wait at least zfs_grow_retry (default 5) seconds
3473 * before considering growing.
3475 growtime
= ddi_get_lbolt() + (arc_grow_retry
* hz
);
3477 arc_kmem_reap_now();
3480 * If we are still low on memory, shrink the ARC
3481 * so that we have arc_shrink_min free space.
3483 free_memory
= arc_available_memory();
3485 to_free
= (arc_c
>> arc_shrink_shift
) - free_memory
;
3488 to_free
= MAX(to_free
, ptob(needfree
));
3490 arc_shrink(to_free
);
3492 } else if (free_memory
< arc_c
>> arc_no_grow_shift
) {
3493 arc_no_grow
= B_TRUE
;
3494 } else if (ddi_get_lbolt() >= growtime
) {
3495 arc_no_grow
= B_FALSE
;
3498 evicted
= arc_adjust();
3500 mutex_enter(&arc_reclaim_lock
);
3503 * If evicted is zero, we couldn't evict anything via
3504 * arc_adjust(). This could be due to hash lock
3505 * collisions, but more likely due to the majority of
3506 * arc buffers being unevictable. Therefore, even if
3507 * arc_size is above arc_c, another pass is unlikely to
3508 * be helpful and could potentially cause us to enter an
3511 if (arc_size
<= arc_c
|| evicted
== 0) {
3513 * We're either no longer overflowing, or we
3514 * can't evict anything more, so we should wake
3515 * up any threads before we go to sleep.
3517 cv_broadcast(&arc_reclaim_waiters_cv
);
3520 * Block until signaled, or after one second (we
3521 * might need to perform arc_kmem_reap_now()
3522 * even if we aren't being signalled)
3524 CALLB_CPR_SAFE_BEGIN(&cpr
);
3525 (void) cv_timedwait_sig(&arc_reclaim_thread_cv
,
3526 &arc_reclaim_lock
, ddi_get_lbolt() + hz
);
3527 CALLB_CPR_SAFE_END(&cpr
, &arc_reclaim_lock
);
3531 arc_reclaim_thread_exit
= FALSE
;
3532 cv_broadcast(&arc_reclaim_thread_cv
);
3533 CALLB_CPR_EXIT(&cpr
); /* drops arc_reclaim_lock */
3534 spl_fstrans_unmark(cookie
);
3539 arc_user_evicts_thread(void)
3541 fstrans_cookie_t cookie
= spl_fstrans_mark();
3544 CALLB_CPR_INIT(&cpr
, &arc_user_evicts_lock
, callb_generic_cpr
, FTAG
);
3546 mutex_enter(&arc_user_evicts_lock
);
3547 while (!arc_user_evicts_thread_exit
) {
3548 mutex_exit(&arc_user_evicts_lock
);
3550 arc_do_user_evicts();
3553 * This is necessary in order for the mdb ::arc dcmd to
3554 * show up to date information. Since the ::arc command
3555 * does not call the kstat's update function, without
3556 * this call, the command may show stale stats for the
3557 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
3558 * with this change, the data might be up to 1 second
3559 * out of date; but that should suffice. The arc_state_t
3560 * structures can be queried directly if more accurate
3561 * information is needed.
3563 if (arc_ksp
!= NULL
)
3564 arc_ksp
->ks_update(arc_ksp
, KSTAT_READ
);
3566 mutex_enter(&arc_user_evicts_lock
);
3569 * Block until signaled, or after one second (we need to
3570 * call the arc's kstat update function regularly).
3572 CALLB_CPR_SAFE_BEGIN(&cpr
);
3573 (void) cv_timedwait_sig(&arc_user_evicts_cv
,
3574 &arc_user_evicts_lock
, ddi_get_lbolt() + hz
);
3575 CALLB_CPR_SAFE_END(&cpr
, &arc_user_evicts_lock
);
3578 arc_user_evicts_thread_exit
= FALSE
;
3579 cv_broadcast(&arc_user_evicts_cv
);
3580 CALLB_CPR_EXIT(&cpr
); /* drops arc_user_evicts_lock */
3581 spl_fstrans_unmark(cookie
);
3587 * Determine the amount of memory eligible for eviction contained in the
3588 * ARC. All clean data reported by the ghost lists can always be safely
3589 * evicted. Due to arc_c_min, the same does not hold for all clean data
3590 * contained by the regular mru and mfu lists.
3592 * In the case of the regular mru and mfu lists, we need to report as
3593 * much clean data as possible, such that evicting that same reported
3594 * data will not bring arc_size below arc_c_min. Thus, in certain
3595 * circumstances, the total amount of clean data in the mru and mfu
3596 * lists might not actually be evictable.
3598 * The following two distinct cases are accounted for:
3600 * 1. The sum of the amount of dirty data contained by both the mru and
3601 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
3602 * is greater than or equal to arc_c_min.
3603 * (i.e. amount of dirty data >= arc_c_min)
3605 * This is the easy case; all clean data contained by the mru and mfu
3606 * lists is evictable. Evicting all clean data can only drop arc_size
3607 * to the amount of dirty data, which is greater than arc_c_min.
3609 * 2. The sum of the amount of dirty data contained by both the mru and
3610 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
3611 * is less than arc_c_min.
3612 * (i.e. arc_c_min > amount of dirty data)
3614 * 2.1. arc_size is greater than or equal arc_c_min.
3615 * (i.e. arc_size >= arc_c_min > amount of dirty data)
3617 * In this case, not all clean data from the regular mru and mfu
3618 * lists is actually evictable; we must leave enough clean data
3619 * to keep arc_size above arc_c_min. Thus, the maximum amount of
3620 * evictable data from the two lists combined, is exactly the
3621 * difference between arc_size and arc_c_min.
3623 * 2.2. arc_size is less than arc_c_min
3624 * (i.e. arc_c_min > arc_size > amount of dirty data)
3626 * In this case, none of the data contained in the mru and mfu
3627 * lists is evictable, even if it's clean. Since arc_size is
3628 * already below arc_c_min, evicting any more would only
3629 * increase this negative difference.
3632 arc_evictable_memory(void) {
3633 uint64_t arc_clean
=
3634 arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] +
3635 arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] +
3636 arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] +
3637 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
];
3638 uint64_t ghost_clean
=
3639 arc_mru_ghost
->arcs_lsize
[ARC_BUFC_DATA
] +
3640 arc_mru_ghost
->arcs_lsize
[ARC_BUFC_METADATA
] +
3641 arc_mfu_ghost
->arcs_lsize
[ARC_BUFC_DATA
] +
3642 arc_mfu_ghost
->arcs_lsize
[ARC_BUFC_METADATA
];
3643 uint64_t arc_dirty
= MAX((int64_t)arc_size
- (int64_t)arc_clean
, 0);
3645 if (arc_dirty
>= arc_c_min
)
3646 return (ghost_clean
+ arc_clean
);
3648 return (ghost_clean
+ MAX((int64_t)arc_size
- (int64_t)arc_c_min
, 0));
3652 * If sc->nr_to_scan is zero, the caller is requesting a query of the
3653 * number of objects which can potentially be freed. If it is nonzero,
3654 * the request is to free that many objects.
3656 * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks
3657 * in struct shrinker and also require the shrinker to return the number
3660 * Older kernels require the shrinker to return the number of freeable
3661 * objects following the freeing of nr_to_free.
3663 static spl_shrinker_t
3664 __arc_shrinker_func(struct shrinker
*shrink
, struct shrink_control
*sc
)
3668 /* The arc is considered warm once reclaim has occurred */
3669 if (unlikely(arc_warm
== B_FALSE
))
3672 /* Return the potential number of reclaimable pages */
3673 pages
= btop((int64_t)arc_evictable_memory());
3674 if (sc
->nr_to_scan
== 0)
3677 /* Not allowed to perform filesystem reclaim */
3678 if (!(sc
->gfp_mask
& __GFP_FS
))
3679 return (SHRINK_STOP
);
3681 /* Reclaim in progress */
3682 if (mutex_tryenter(&arc_reclaim_lock
) == 0)
3683 return (SHRINK_STOP
);
3685 mutex_exit(&arc_reclaim_lock
);
3688 * Evict the requested number of pages by shrinking arc_c the
3689 * requested amount. If there is nothing left to evict just
3690 * reap whatever we can from the various arc slabs.
3693 arc_shrink(ptob(sc
->nr_to_scan
));
3694 arc_kmem_reap_now();
3695 #ifdef HAVE_SPLIT_SHRINKER_CALLBACK
3696 pages
= MAX(pages
- btop(arc_evictable_memory()), 0);
3698 pages
= btop(arc_evictable_memory());
3701 arc_kmem_reap_now();
3702 pages
= SHRINK_STOP
;
3706 * We've reaped what we can, wake up threads.
3708 cv_broadcast(&arc_reclaim_waiters_cv
);
3711 * When direct reclaim is observed it usually indicates a rapid
3712 * increase in memory pressure. This occurs because the kswapd
3713 * threads were unable to asynchronously keep enough free memory
3714 * available. In this case set arc_no_grow to briefly pause arc
3715 * growth to avoid compounding the memory pressure.
3717 if (current_is_kswapd()) {
3718 ARCSTAT_BUMP(arcstat_memory_indirect_count
);
3720 arc_no_grow
= B_TRUE
;
3721 arc_grow_time
= ddi_get_lbolt() + (zfs_arc_grow_retry
* hz
);
3722 ARCSTAT_BUMP(arcstat_memory_direct_count
);
3727 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func
);
3729 SPL_SHRINKER_DECLARE(arc_shrinker
, arc_shrinker_func
, DEFAULT_SEEKS
);
3730 #endif /* _KERNEL */
3733 * Adapt arc info given the number of bytes we are trying to add and
3734 * the state that we are comming from. This function is only called
3735 * when we are adding new content to the cache.
3738 arc_adapt(int bytes
, arc_state_t
*state
)
3741 uint64_t arc_p_min
= (arc_c
>> arc_p_min_shift
);
3742 int64_t mrug_size
= refcount_count(&arc_mru_ghost
->arcs_size
);
3743 int64_t mfug_size
= refcount_count(&arc_mfu_ghost
->arcs_size
);
3745 if (state
== arc_l2c_only
)
3750 * Adapt the target size of the MRU list:
3751 * - if we just hit in the MRU ghost list, then increase
3752 * the target size of the MRU list.
3753 * - if we just hit in the MFU ghost list, then increase
3754 * the target size of the MFU list by decreasing the
3755 * target size of the MRU list.
3757 if (state
== arc_mru_ghost
) {
3758 mult
= (mrug_size
>= mfug_size
) ? 1 : (mfug_size
/ mrug_size
);
3759 if (!zfs_arc_p_dampener_disable
)
3760 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
3762 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
3763 } else if (state
== arc_mfu_ghost
) {
3766 mult
= (mfug_size
>= mrug_size
) ? 1 : (mrug_size
/ mfug_size
);
3767 if (!zfs_arc_p_dampener_disable
)
3768 mult
= MIN(mult
, 10);
3770 delta
= MIN(bytes
* mult
, arc_p
);
3771 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
3773 ASSERT((int64_t)arc_p
>= 0);
3775 if (arc_reclaim_needed()) {
3776 cv_signal(&arc_reclaim_thread_cv
);
3783 if (arc_c
>= arc_c_max
)
3787 * If we're within (2 * maxblocksize) bytes of the target
3788 * cache size, increment the target cache size
3790 VERIFY3U(arc_c
, >=, 2ULL << SPA_MAXBLOCKSHIFT
);
3791 if (arc_size
>= arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
3792 atomic_add_64(&arc_c
, (int64_t)bytes
);
3793 if (arc_c
> arc_c_max
)
3795 else if (state
== arc_anon
)
3796 atomic_add_64(&arc_p
, (int64_t)bytes
);
3800 ASSERT((int64_t)arc_p
>= 0);
3804 * Check if arc_size has grown past our upper threshold, determined by
3805 * zfs_arc_overflow_shift.
3808 arc_is_overflowing(void)
3810 /* Always allow at least one block of overflow */
3811 uint64_t overflow
= MAX(SPA_MAXBLOCKSIZE
,
3812 arc_c
>> zfs_arc_overflow_shift
);
3814 return (arc_size
>= arc_c
+ overflow
);
3818 * The buffer, supplied as the first argument, needs a data block. If we
3819 * are hitting the hard limit for the cache size, we must sleep, waiting
3820 * for the eviction thread to catch up. If we're past the target size
3821 * but below the hard limit, we'll only signal the reclaim thread and
3825 arc_get_data_buf(arc_buf_t
*buf
)
3827 arc_state_t
*state
= buf
->b_hdr
->b_l1hdr
.b_state
;
3828 uint64_t size
= buf
->b_hdr
->b_size
;
3829 arc_buf_contents_t type
= arc_buf_type(buf
->b_hdr
);
3831 arc_adapt(size
, state
);
3834 * If arc_size is currently overflowing, and has grown past our
3835 * upper limit, we must be adding data faster than the evict
3836 * thread can evict. Thus, to ensure we don't compound the
3837 * problem by adding more data and forcing arc_size to grow even
3838 * further past it's target size, we halt and wait for the
3839 * eviction thread to catch up.
3841 * It's also possible that the reclaim thread is unable to evict
3842 * enough buffers to get arc_size below the overflow limit (e.g.
3843 * due to buffers being un-evictable, or hash lock collisions).
3844 * In this case, we want to proceed regardless if we're
3845 * overflowing; thus we don't use a while loop here.
3847 if (arc_is_overflowing()) {
3848 mutex_enter(&arc_reclaim_lock
);
3851 * Now that we've acquired the lock, we may no longer be
3852 * over the overflow limit, lets check.
3854 * We're ignoring the case of spurious wake ups. If that
3855 * were to happen, it'd let this thread consume an ARC
3856 * buffer before it should have (i.e. before we're under
3857 * the overflow limit and were signalled by the reclaim
3858 * thread). As long as that is a rare occurrence, it
3859 * shouldn't cause any harm.
3861 if (arc_is_overflowing()) {
3862 cv_signal(&arc_reclaim_thread_cv
);
3863 cv_wait(&arc_reclaim_waiters_cv
, &arc_reclaim_lock
);
3866 mutex_exit(&arc_reclaim_lock
);
3869 if (type
== ARC_BUFC_METADATA
) {
3870 buf
->b_data
= zio_buf_alloc(size
);
3871 arc_space_consume(size
, ARC_SPACE_META
);
3873 ASSERT(type
== ARC_BUFC_DATA
);
3874 buf
->b_data
= zio_data_buf_alloc(size
);
3875 arc_space_consume(size
, ARC_SPACE_DATA
);
3879 * Update the state size. Note that ghost states have a
3880 * "ghost size" and so don't need to be updated.
3882 if (!GHOST_STATE(buf
->b_hdr
->b_l1hdr
.b_state
)) {
3883 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3884 arc_state_t
*state
= hdr
->b_l1hdr
.b_state
;
3886 (void) refcount_add_many(&state
->arcs_size
, size
, buf
);
3889 * If this is reached via arc_read, the link is
3890 * protected by the hash lock. If reached via
3891 * arc_buf_alloc, the header should not be accessed by
3892 * any other thread. And, if reached via arc_read_done,
3893 * the hash lock will protect it if it's found in the
3894 * hash table; otherwise no other thread should be
3895 * trying to [add|remove]_reference it.
3897 if (multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
)) {
3898 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
3899 atomic_add_64(&hdr
->b_l1hdr
.b_state
->arcs_lsize
[type
],
3903 * If we are growing the cache, and we are adding anonymous
3904 * data, and we have outgrown arc_p, update arc_p
3906 if (arc_size
< arc_c
&& hdr
->b_l1hdr
.b_state
== arc_anon
&&
3907 (refcount_count(&arc_anon
->arcs_size
) +
3908 refcount_count(&arc_mru
->arcs_size
) > arc_p
))
3909 arc_p
= MIN(arc_c
, arc_p
+ size
);
3914 * This routine is called whenever a buffer is accessed.
3915 * NOTE: the hash lock is dropped in this function.
3918 arc_access(arc_buf_hdr_t
*hdr
, kmutex_t
*hash_lock
)
3922 ASSERT(MUTEX_HELD(hash_lock
));
3923 ASSERT(HDR_HAS_L1HDR(hdr
));
3925 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
3927 * This buffer is not in the cache, and does not
3928 * appear in our "ghost" list. Add the new buffer
3932 ASSERT0(hdr
->b_l1hdr
.b_arc_access
);
3933 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
3934 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
3935 arc_change_state(arc_mru
, hdr
, hash_lock
);
3937 } else if (hdr
->b_l1hdr
.b_state
== arc_mru
) {
3938 now
= ddi_get_lbolt();
3941 * If this buffer is here because of a prefetch, then either:
3942 * - clear the flag if this is a "referencing" read
3943 * (any subsequent access will bump this into the MFU state).
3945 * - move the buffer to the head of the list if this is
3946 * another prefetch (to make it less likely to be evicted).
3948 if (HDR_PREFETCH(hdr
)) {
3949 if (refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
3950 /* link protected by hash lock */
3951 ASSERT(multilist_link_active(
3952 &hdr
->b_l1hdr
.b_arc_node
));
3954 hdr
->b_flags
&= ~ARC_FLAG_PREFETCH
;
3955 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
3956 ARCSTAT_BUMP(arcstat_mru_hits
);
3958 hdr
->b_l1hdr
.b_arc_access
= now
;
3963 * This buffer has been "accessed" only once so far,
3964 * but it is still in the cache. Move it to the MFU
3967 if (ddi_time_after(now
, hdr
->b_l1hdr
.b_arc_access
+
3970 * More than 125ms have passed since we
3971 * instantiated this buffer. Move it to the
3972 * most frequently used state.
3974 hdr
->b_l1hdr
.b_arc_access
= now
;
3975 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
3976 arc_change_state(arc_mfu
, hdr
, hash_lock
);
3978 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_hits
);
3979 ARCSTAT_BUMP(arcstat_mru_hits
);
3980 } else if (hdr
->b_l1hdr
.b_state
== arc_mru_ghost
) {
3981 arc_state_t
*new_state
;
3983 * This buffer has been "accessed" recently, but
3984 * was evicted from the cache. Move it to the
3988 if (HDR_PREFETCH(hdr
)) {
3989 new_state
= arc_mru
;
3990 if (refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0)
3991 hdr
->b_flags
&= ~ARC_FLAG_PREFETCH
;
3992 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, hdr
);
3994 new_state
= arc_mfu
;
3995 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
3998 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
3999 arc_change_state(new_state
, hdr
, hash_lock
);
4001 atomic_inc_32(&hdr
->b_l1hdr
.b_mru_ghost_hits
);
4002 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
4003 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu
) {
4005 * This buffer has been accessed more than once and is
4006 * still in the cache. Keep it in the MFU state.
4008 * NOTE: an add_reference() that occurred when we did
4009 * the arc_read() will have kicked this off the list.
4010 * If it was a prefetch, we will explicitly move it to
4011 * the head of the list now.
4013 if ((HDR_PREFETCH(hdr
)) != 0) {
4014 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
4015 /* link protected by hash_lock */
4016 ASSERT(multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
4018 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_hits
);
4019 ARCSTAT_BUMP(arcstat_mfu_hits
);
4020 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
4021 } else if (hdr
->b_l1hdr
.b_state
== arc_mfu_ghost
) {
4022 arc_state_t
*new_state
= arc_mfu
;
4024 * This buffer has been accessed more than once but has
4025 * been evicted from the cache. Move it back to the
4029 if (HDR_PREFETCH(hdr
)) {
4031 * This is a prefetch access...
4032 * move this block back to the MRU state.
4034 ASSERT0(refcount_count(&hdr
->b_l1hdr
.b_refcnt
));
4035 new_state
= arc_mru
;
4038 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
4039 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
4040 arc_change_state(new_state
, hdr
, hash_lock
);
4042 atomic_inc_32(&hdr
->b_l1hdr
.b_mfu_ghost_hits
);
4043 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
4044 } else if (hdr
->b_l1hdr
.b_state
== arc_l2c_only
) {
4046 * This buffer is on the 2nd Level ARC.
4049 hdr
->b_l1hdr
.b_arc_access
= ddi_get_lbolt();
4050 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, hdr
);
4051 arc_change_state(arc_mfu
, hdr
, hash_lock
);
4053 cmn_err(CE_PANIC
, "invalid arc state 0x%p",
4054 hdr
->b_l1hdr
.b_state
);
4058 /* a generic arc_done_func_t which you can use */
4061 arc_bcopy_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
4063 if (zio
== NULL
|| zio
->io_error
== 0)
4064 bcopy(buf
->b_data
, arg
, buf
->b_hdr
->b_size
);
4065 VERIFY(arc_buf_remove_ref(buf
, arg
));
4068 /* a generic arc_done_func_t */
4070 arc_getbuf_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
4072 arc_buf_t
**bufp
= arg
;
4073 if (zio
&& zio
->io_error
) {
4074 VERIFY(arc_buf_remove_ref(buf
, arg
));
4078 ASSERT(buf
->b_data
);
4083 arc_read_done(zio_t
*zio
)
4087 arc_buf_t
*abuf
; /* buffer we're assigning to callback */
4088 kmutex_t
*hash_lock
= NULL
;
4089 arc_callback_t
*callback_list
, *acb
;
4090 int freeable
= FALSE
;
4092 buf
= zio
->io_private
;
4096 * The hdr was inserted into hash-table and removed from lists
4097 * prior to starting I/O. We should find this header, since
4098 * it's in the hash table, and it should be legit since it's
4099 * not possible to evict it during the I/O. The only possible
4100 * reason for it not to be found is if we were freed during the
4103 if (HDR_IN_HASH_TABLE(hdr
)) {
4104 arc_buf_hdr_t
*found
;
4106 ASSERT3U(hdr
->b_birth
, ==, BP_PHYSICAL_BIRTH(zio
->io_bp
));
4107 ASSERT3U(hdr
->b_dva
.dva_word
[0], ==,
4108 BP_IDENTITY(zio
->io_bp
)->dva_word
[0]);
4109 ASSERT3U(hdr
->b_dva
.dva_word
[1], ==,
4110 BP_IDENTITY(zio
->io_bp
)->dva_word
[1]);
4112 found
= buf_hash_find(hdr
->b_spa
, zio
->io_bp
,
4115 ASSERT((found
== NULL
&& HDR_FREED_IN_READ(hdr
) &&
4116 hash_lock
== NULL
) ||
4118 DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
4119 (found
== hdr
&& HDR_L2_READING(hdr
)));
4122 hdr
->b_flags
&= ~ARC_FLAG_L2_EVICTED
;
4123 if (l2arc_noprefetch
&& HDR_PREFETCH(hdr
))
4124 hdr
->b_flags
&= ~ARC_FLAG_L2CACHE
;
4126 /* byteswap if necessary */
4127 callback_list
= hdr
->b_l1hdr
.b_acb
;
4128 ASSERT(callback_list
!= NULL
);
4129 if (BP_SHOULD_BYTESWAP(zio
->io_bp
) && zio
->io_error
== 0) {
4130 dmu_object_byteswap_t bswap
=
4131 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
4132 if (BP_GET_LEVEL(zio
->io_bp
) > 0)
4133 byteswap_uint64_array(buf
->b_data
, hdr
->b_size
);
4135 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, hdr
->b_size
);
4138 arc_cksum_compute(buf
, B_FALSE
);
4141 if (hash_lock
&& zio
->io_error
== 0 &&
4142 hdr
->b_l1hdr
.b_state
== arc_anon
) {
4144 * Only call arc_access on anonymous buffers. This is because
4145 * if we've issued an I/O for an evicted buffer, we've already
4146 * called arc_access (to prevent any simultaneous readers from
4147 * getting confused).
4149 arc_access(hdr
, hash_lock
);
4152 /* create copies of the data buffer for the callers */
4154 for (acb
= callback_list
; acb
; acb
= acb
->acb_next
) {
4155 if (acb
->acb_done
) {
4157 ARCSTAT_BUMP(arcstat_duplicate_reads
);
4158 abuf
= arc_buf_clone(buf
);
4160 acb
->acb_buf
= abuf
;
4164 hdr
->b_l1hdr
.b_acb
= NULL
;
4165 hdr
->b_flags
&= ~ARC_FLAG_IO_IN_PROGRESS
;
4166 ASSERT(!HDR_BUF_AVAILABLE(hdr
));
4168 ASSERT(buf
->b_efunc
== NULL
);
4169 ASSERT(hdr
->b_l1hdr
.b_datacnt
== 1);
4170 hdr
->b_flags
|= ARC_FLAG_BUF_AVAILABLE
;
4173 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
) ||
4174 callback_list
!= NULL
);
4176 if (zio
->io_error
!= 0) {
4177 hdr
->b_flags
|= ARC_FLAG_IO_ERROR
;
4178 if (hdr
->b_l1hdr
.b_state
!= arc_anon
)
4179 arc_change_state(arc_anon
, hdr
, hash_lock
);
4180 if (HDR_IN_HASH_TABLE(hdr
))
4181 buf_hash_remove(hdr
);
4182 freeable
= refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
4186 * Broadcast before we drop the hash_lock to avoid the possibility
4187 * that the hdr (and hence the cv) might be freed before we get to
4188 * the cv_broadcast().
4190 cv_broadcast(&hdr
->b_l1hdr
.b_cv
);
4192 if (hash_lock
!= NULL
) {
4193 mutex_exit(hash_lock
);
4196 * This block was freed while we waited for the read to
4197 * complete. It has been removed from the hash table and
4198 * moved to the anonymous state (so that it won't show up
4201 ASSERT3P(hdr
->b_l1hdr
.b_state
, ==, arc_anon
);
4202 freeable
= refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
);
4205 /* execute each callback and free its structure */
4206 while ((acb
= callback_list
) != NULL
) {
4208 acb
->acb_done(zio
, acb
->acb_buf
, acb
->acb_private
);
4210 if (acb
->acb_zio_dummy
!= NULL
) {
4211 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
4212 zio_nowait(acb
->acb_zio_dummy
);
4215 callback_list
= acb
->acb_next
;
4216 kmem_free(acb
, sizeof (arc_callback_t
));
4220 arc_hdr_destroy(hdr
);
4224 * "Read" the block at the specified DVA (in bp) via the
4225 * cache. If the block is found in the cache, invoke the provided
4226 * callback immediately and return. Note that the `zio' parameter
4227 * in the callback will be NULL in this case, since no IO was
4228 * required. If the block is not in the cache pass the read request
4229 * on to the spa with a substitute callback function, so that the
4230 * requested block will be added to the cache.
4232 * If a read request arrives for a block that has a read in-progress,
4233 * either wait for the in-progress read to complete (and return the
4234 * results); or, if this is a read with a "done" func, add a record
4235 * to the read to invoke the "done" func when the read completes,
4236 * and return; or just return.
4238 * arc_read_done() will invoke all the requested "done" functions
4239 * for readers of this block.
4242 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
, arc_done_func_t
*done
,
4243 void *private, zio_priority_t priority
, int zio_flags
,
4244 arc_flags_t
*arc_flags
, const zbookmark_phys_t
*zb
)
4246 arc_buf_hdr_t
*hdr
= NULL
;
4247 arc_buf_t
*buf
= NULL
;
4248 kmutex_t
*hash_lock
= NULL
;
4250 uint64_t guid
= spa_load_guid(spa
);
4253 ASSERT(!BP_IS_EMBEDDED(bp
) ||
4254 BPE_GET_ETYPE(bp
) == BP_EMBEDDED_TYPE_DATA
);
4257 if (!BP_IS_EMBEDDED(bp
)) {
4259 * Embedded BP's have no DVA and require no I/O to "read".
4260 * Create an anonymous arc buf to back it.
4262 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
4265 if (hdr
!= NULL
&& HDR_HAS_L1HDR(hdr
) && hdr
->b_l1hdr
.b_datacnt
> 0) {
4267 *arc_flags
|= ARC_FLAG_CACHED
;
4269 if (HDR_IO_IN_PROGRESS(hdr
)) {
4271 if (*arc_flags
& ARC_FLAG_WAIT
) {
4272 cv_wait(&hdr
->b_l1hdr
.b_cv
, hash_lock
);
4273 mutex_exit(hash_lock
);
4276 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
4279 arc_callback_t
*acb
= NULL
;
4281 acb
= kmem_zalloc(sizeof (arc_callback_t
),
4283 acb
->acb_done
= done
;
4284 acb
->acb_private
= private;
4286 acb
->acb_zio_dummy
= zio_null(pio
,
4287 spa
, NULL
, NULL
, NULL
, zio_flags
);
4289 ASSERT(acb
->acb_done
!= NULL
);
4290 acb
->acb_next
= hdr
->b_l1hdr
.b_acb
;
4291 hdr
->b_l1hdr
.b_acb
= acb
;
4292 add_reference(hdr
, hash_lock
, private);
4293 mutex_exit(hash_lock
);
4296 mutex_exit(hash_lock
);
4300 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
4301 hdr
->b_l1hdr
.b_state
== arc_mfu
);
4304 add_reference(hdr
, hash_lock
, private);
4306 * If this block is already in use, create a new
4307 * copy of the data so that we will be guaranteed
4308 * that arc_release() will always succeed.
4310 buf
= hdr
->b_l1hdr
.b_buf
;
4312 ASSERT(buf
->b_data
);
4313 if (HDR_BUF_AVAILABLE(hdr
)) {
4314 ASSERT(buf
->b_efunc
== NULL
);
4315 hdr
->b_flags
&= ~ARC_FLAG_BUF_AVAILABLE
;
4317 buf
= arc_buf_clone(buf
);
4320 } else if (*arc_flags
& ARC_FLAG_PREFETCH
&&
4321 refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 0) {
4322 hdr
->b_flags
|= ARC_FLAG_PREFETCH
;
4324 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
4325 arc_access(hdr
, hash_lock
);
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 mutex_exit(hash_lock
);
4331 ARCSTAT_BUMP(arcstat_hits
);
4332 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
4333 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
4334 data
, metadata
, hits
);
4337 done(NULL
, buf
, private);
4339 uint64_t size
= BP_GET_LSIZE(bp
);
4340 arc_callback_t
*acb
;
4343 boolean_t devw
= B_FALSE
;
4344 enum zio_compress b_compress
= ZIO_COMPRESS_OFF
;
4345 int32_t b_asize
= 0;
4348 * Gracefully handle a damaged logical block size as a
4349 * checksum error by passing a dummy zio to the done callback.
4351 if (size
> spa_maxblocksize(spa
)) {
4353 rzio
= zio_null(pio
, spa
, NULL
,
4354 NULL
, NULL
, zio_flags
);
4355 rzio
->io_error
= ECKSUM
;
4356 done(rzio
, buf
, private);
4364 /* this block is not in the cache */
4365 arc_buf_hdr_t
*exists
= NULL
;
4366 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
4367 buf
= arc_buf_alloc(spa
, size
, private, type
);
4369 if (!BP_IS_EMBEDDED(bp
)) {
4370 hdr
->b_dva
= *BP_IDENTITY(bp
);
4371 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
4372 exists
= buf_hash_insert(hdr
, &hash_lock
);
4374 if (exists
!= NULL
) {
4375 /* somebody beat us to the hash insert */
4376 mutex_exit(hash_lock
);
4377 buf_discard_identity(hdr
);
4378 (void) arc_buf_remove_ref(buf
, private);
4379 goto top
; /* restart the IO request */
4382 /* if this is a prefetch, we don't have a reference */
4383 if (*arc_flags
& ARC_FLAG_PREFETCH
) {
4384 (void) remove_reference(hdr
, hash_lock
,
4386 hdr
->b_flags
|= ARC_FLAG_PREFETCH
;
4388 if (*arc_flags
& ARC_FLAG_L2CACHE
)
4389 hdr
->b_flags
|= ARC_FLAG_L2CACHE
;
4390 if (*arc_flags
& ARC_FLAG_L2COMPRESS
)
4391 hdr
->b_flags
|= ARC_FLAG_L2COMPRESS
;
4392 if (BP_GET_LEVEL(bp
) > 0)
4393 hdr
->b_flags
|= ARC_FLAG_INDIRECT
;
4396 * This block is in the ghost cache. If it was L2-only
4397 * (and thus didn't have an L1 hdr), we realloc the
4398 * header to add an L1 hdr.
4400 if (!HDR_HAS_L1HDR(hdr
)) {
4401 hdr
= arc_hdr_realloc(hdr
, hdr_l2only_cache
,
4405 ASSERT(GHOST_STATE(hdr
->b_l1hdr
.b_state
));
4406 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
4407 ASSERT(refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
4408 ASSERT3P(hdr
->b_l1hdr
.b_buf
, ==, NULL
);
4410 /* if this is a prefetch, we don't have a reference */
4411 if (*arc_flags
& ARC_FLAG_PREFETCH
)
4412 hdr
->b_flags
|= ARC_FLAG_PREFETCH
;
4414 add_reference(hdr
, hash_lock
, private);
4415 if (*arc_flags
& ARC_FLAG_L2CACHE
)
4416 hdr
->b_flags
|= ARC_FLAG_L2CACHE
;
4417 if (*arc_flags
& ARC_FLAG_L2COMPRESS
)
4418 hdr
->b_flags
|= ARC_FLAG_L2COMPRESS
;
4419 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
4422 buf
->b_efunc
= NULL
;
4423 buf
->b_private
= NULL
;
4425 hdr
->b_l1hdr
.b_buf
= buf
;
4426 ASSERT0(hdr
->b_l1hdr
.b_datacnt
);
4427 hdr
->b_l1hdr
.b_datacnt
= 1;
4428 arc_get_data_buf(buf
);
4429 arc_access(hdr
, hash_lock
);
4432 ASSERT(!GHOST_STATE(hdr
->b_l1hdr
.b_state
));
4434 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
4435 acb
->acb_done
= done
;
4436 acb
->acb_private
= private;
4438 ASSERT(hdr
->b_l1hdr
.b_acb
== NULL
);
4439 hdr
->b_l1hdr
.b_acb
= acb
;
4440 hdr
->b_flags
|= ARC_FLAG_IO_IN_PROGRESS
;
4442 if (HDR_HAS_L2HDR(hdr
) &&
4443 (vd
= hdr
->b_l2hdr
.b_dev
->l2ad_vdev
) != NULL
) {
4444 devw
= hdr
->b_l2hdr
.b_dev
->l2ad_writing
;
4445 addr
= hdr
->b_l2hdr
.b_daddr
;
4446 b_compress
= HDR_GET_COMPRESS(hdr
);
4447 b_asize
= hdr
->b_l2hdr
.b_asize
;
4449 * Lock out device removal.
4451 if (vdev_is_dead(vd
) ||
4452 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
4456 if (hash_lock
!= NULL
)
4457 mutex_exit(hash_lock
);
4460 * At this point, we have a level 1 cache miss. Try again in
4461 * L2ARC if possible.
4463 ASSERT3U(hdr
->b_size
, ==, size
);
4464 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
, blkptr_t
*, bp
,
4465 uint64_t, size
, zbookmark_phys_t
*, zb
);
4466 ARCSTAT_BUMP(arcstat_misses
);
4467 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr
),
4468 demand
, prefetch
, !HDR_ISTYPE_METADATA(hdr
),
4469 data
, metadata
, misses
);
4471 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
4473 * Read from the L2ARC if the following are true:
4474 * 1. The L2ARC vdev was previously cached.
4475 * 2. This buffer still has L2ARC metadata.
4476 * 3. This buffer isn't currently writing to the L2ARC.
4477 * 4. The L2ARC entry wasn't evicted, which may
4478 * also have invalidated the vdev.
4479 * 5. This isn't prefetch and l2arc_noprefetch is set.
4481 if (HDR_HAS_L2HDR(hdr
) &&
4482 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
4483 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
4484 l2arc_read_callback_t
*cb
;
4486 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
4487 ARCSTAT_BUMP(arcstat_l2_hits
);
4488 atomic_inc_32(&hdr
->b_l2hdr
.b_hits
);
4490 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
4492 cb
->l2rcb_buf
= buf
;
4493 cb
->l2rcb_spa
= spa
;
4496 cb
->l2rcb_flags
= zio_flags
;
4497 cb
->l2rcb_compress
= b_compress
;
4499 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
4500 addr
+ size
< vd
->vdev_psize
-
4501 VDEV_LABEL_END_SIZE
);
4504 * l2arc read. The SCL_L2ARC lock will be
4505 * released by l2arc_read_done().
4506 * Issue a null zio if the underlying buffer
4507 * was squashed to zero size by compression.
4509 if (b_compress
== ZIO_COMPRESS_EMPTY
) {
4510 rzio
= zio_null(pio
, spa
, vd
,
4511 l2arc_read_done
, cb
,
4512 zio_flags
| ZIO_FLAG_DONT_CACHE
|
4514 ZIO_FLAG_DONT_PROPAGATE
|
4515 ZIO_FLAG_DONT_RETRY
);
4517 rzio
= zio_read_phys(pio
, vd
, addr
,
4518 b_asize
, buf
->b_data
,
4520 l2arc_read_done
, cb
, priority
,
4521 zio_flags
| ZIO_FLAG_DONT_CACHE
|
4523 ZIO_FLAG_DONT_PROPAGATE
|
4524 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
4526 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
4528 ARCSTAT_INCR(arcstat_l2_read_bytes
, b_asize
);
4530 if (*arc_flags
& ARC_FLAG_NOWAIT
) {
4535 ASSERT(*arc_flags
& ARC_FLAG_WAIT
);
4536 if (zio_wait(rzio
) == 0)
4539 /* l2arc read error; goto zio_read() */
4541 DTRACE_PROBE1(l2arc__miss
,
4542 arc_buf_hdr_t
*, hdr
);
4543 ARCSTAT_BUMP(arcstat_l2_misses
);
4544 if (HDR_L2_WRITING(hdr
))
4545 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
4546 spa_config_exit(spa
, SCL_L2ARC
, vd
);
4550 spa_config_exit(spa
, SCL_L2ARC
, vd
);
4551 if (l2arc_ndev
!= 0) {
4552 DTRACE_PROBE1(l2arc__miss
,
4553 arc_buf_hdr_t
*, hdr
);
4554 ARCSTAT_BUMP(arcstat_l2_misses
);
4558 rzio
= zio_read(pio
, spa
, bp
, buf
->b_data
, size
,
4559 arc_read_done
, buf
, priority
, zio_flags
, zb
);
4561 if (*arc_flags
& ARC_FLAG_WAIT
) {
4562 rc
= zio_wait(rzio
);
4566 ASSERT(*arc_flags
& ARC_FLAG_NOWAIT
);
4571 spa_read_history_add(spa
, zb
, *arc_flags
);
4576 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
4580 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
4582 p
->p_private
= private;
4583 list_link_init(&p
->p_node
);
4584 refcount_create(&p
->p_refcnt
);
4586 mutex_enter(&arc_prune_mtx
);
4587 refcount_add(&p
->p_refcnt
, &arc_prune_list
);
4588 list_insert_head(&arc_prune_list
, p
);
4589 mutex_exit(&arc_prune_mtx
);
4595 arc_remove_prune_callback(arc_prune_t
*p
)
4597 mutex_enter(&arc_prune_mtx
);
4598 list_remove(&arc_prune_list
, p
);
4599 if (refcount_remove(&p
->p_refcnt
, &arc_prune_list
) == 0) {
4600 refcount_destroy(&p
->p_refcnt
);
4601 kmem_free(p
, sizeof (*p
));
4603 mutex_exit(&arc_prune_mtx
);
4607 arc_set_callback(arc_buf_t
*buf
, arc_evict_func_t
*func
, void *private)
4609 ASSERT(buf
->b_hdr
!= NULL
);
4610 ASSERT(buf
->b_hdr
->b_l1hdr
.b_state
!= arc_anon
);
4611 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
) ||
4613 ASSERT(buf
->b_efunc
== NULL
);
4614 ASSERT(!HDR_BUF_AVAILABLE(buf
->b_hdr
));
4616 buf
->b_efunc
= func
;
4617 buf
->b_private
= private;
4621 * Notify the arc that a block was freed, and thus will never be used again.
4624 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
4627 kmutex_t
*hash_lock
;
4628 uint64_t guid
= spa_load_guid(spa
);
4630 ASSERT(!BP_IS_EMBEDDED(bp
));
4632 hdr
= buf_hash_find(guid
, bp
, &hash_lock
);
4635 if (HDR_BUF_AVAILABLE(hdr
)) {
4636 arc_buf_t
*buf
= hdr
->b_l1hdr
.b_buf
;
4637 add_reference(hdr
, hash_lock
, FTAG
);
4638 hdr
->b_flags
&= ~ARC_FLAG_BUF_AVAILABLE
;
4639 mutex_exit(hash_lock
);
4641 arc_release(buf
, FTAG
);
4642 (void) arc_buf_remove_ref(buf
, FTAG
);
4644 mutex_exit(hash_lock
);
4650 * Clear the user eviction callback set by arc_set_callback(), first calling
4651 * it if it exists. Because the presence of a callback keeps an arc_buf cached
4652 * clearing the callback may result in the arc_buf being destroyed. However,
4653 * it will not result in the *last* arc_buf being destroyed, hence the data
4654 * will remain cached in the ARC. We make a copy of the arc buffer here so
4655 * that we can process the callback without holding any locks.
4657 * It's possible that the callback is already in the process of being cleared
4658 * by another thread. In this case we can not clear the callback.
4660 * Returns B_TRUE if the callback was successfully called and cleared.
4663 arc_clear_callback(arc_buf_t
*buf
)
4666 kmutex_t
*hash_lock
;
4667 arc_evict_func_t
*efunc
= buf
->b_efunc
;
4668 void *private = buf
->b_private
;
4670 mutex_enter(&buf
->b_evict_lock
);
4674 * We are in arc_do_user_evicts().
4676 ASSERT(buf
->b_data
== NULL
);
4677 mutex_exit(&buf
->b_evict_lock
);
4679 } else if (buf
->b_data
== NULL
) {
4681 * We are on the eviction list; process this buffer now
4682 * but let arc_do_user_evicts() do the reaping.
4684 buf
->b_efunc
= NULL
;
4685 mutex_exit(&buf
->b_evict_lock
);
4686 VERIFY0(efunc(private));
4689 hash_lock
= HDR_LOCK(hdr
);
4690 mutex_enter(hash_lock
);
4692 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
4694 ASSERT3U(refcount_count(&hdr
->b_l1hdr
.b_refcnt
), <,
4695 hdr
->b_l1hdr
.b_datacnt
);
4696 ASSERT(hdr
->b_l1hdr
.b_state
== arc_mru
||
4697 hdr
->b_l1hdr
.b_state
== arc_mfu
);
4699 buf
->b_efunc
= NULL
;
4700 buf
->b_private
= NULL
;
4702 if (hdr
->b_l1hdr
.b_datacnt
> 1) {
4703 mutex_exit(&buf
->b_evict_lock
);
4704 arc_buf_destroy(buf
, TRUE
);
4706 ASSERT(buf
== hdr
->b_l1hdr
.b_buf
);
4707 hdr
->b_flags
|= ARC_FLAG_BUF_AVAILABLE
;
4708 mutex_exit(&buf
->b_evict_lock
);
4711 mutex_exit(hash_lock
);
4712 VERIFY0(efunc(private));
4717 * Release this buffer from the cache, making it an anonymous buffer. This
4718 * must be done after a read and prior to modifying the buffer contents.
4719 * If the buffer has more than one reference, we must make
4720 * a new hdr for the buffer.
4723 arc_release(arc_buf_t
*buf
, void *tag
)
4725 kmutex_t
*hash_lock
;
4727 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
4730 * It would be nice to assert that if its DMU metadata (level >
4731 * 0 || it's the dnode file), then it must be syncing context.
4732 * But we don't know that information at this level.
4735 mutex_enter(&buf
->b_evict_lock
);
4737 ASSERT(HDR_HAS_L1HDR(hdr
));
4740 * We don't grab the hash lock prior to this check, because if
4741 * the buffer's header is in the arc_anon state, it won't be
4742 * linked into the hash table.
4744 if (hdr
->b_l1hdr
.b_state
== arc_anon
) {
4745 mutex_exit(&buf
->b_evict_lock
);
4746 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
4747 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
4748 ASSERT(!HDR_HAS_L2HDR(hdr
));
4749 ASSERT(BUF_EMPTY(hdr
));
4751 ASSERT3U(hdr
->b_l1hdr
.b_datacnt
, ==, 1);
4752 ASSERT3S(refcount_count(&hdr
->b_l1hdr
.b_refcnt
), ==, 1);
4753 ASSERT(!list_link_active(&hdr
->b_l1hdr
.b_arc_node
));
4755 ASSERT3P(buf
->b_efunc
, ==, NULL
);
4756 ASSERT3P(buf
->b_private
, ==, NULL
);
4758 hdr
->b_l1hdr
.b_arc_access
= 0;
4764 hash_lock
= HDR_LOCK(hdr
);
4765 mutex_enter(hash_lock
);
4768 * This assignment is only valid as long as the hash_lock is
4769 * held, we must be careful not to reference state or the
4770 * b_state field after dropping the lock.
4772 state
= hdr
->b_l1hdr
.b_state
;
4773 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
4774 ASSERT3P(state
, !=, arc_anon
);
4776 /* this buffer is not on any list */
4777 ASSERT(refcount_count(&hdr
->b_l1hdr
.b_refcnt
) > 0);
4779 if (HDR_HAS_L2HDR(hdr
)) {
4780 mutex_enter(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
4783 * We have to recheck this conditional again now that
4784 * we're holding the l2ad_mtx to prevent a race with
4785 * another thread which might be concurrently calling
4786 * l2arc_evict(). In that case, l2arc_evict() might have
4787 * destroyed the header's L2 portion as we were waiting
4788 * to acquire the l2ad_mtx.
4790 if (HDR_HAS_L2HDR(hdr
))
4791 arc_hdr_l2hdr_destroy(hdr
);
4793 mutex_exit(&hdr
->b_l2hdr
.b_dev
->l2ad_mtx
);
4797 * Do we have more than one buf?
4799 if (hdr
->b_l1hdr
.b_datacnt
> 1) {
4800 arc_buf_hdr_t
*nhdr
;
4802 uint64_t blksz
= hdr
->b_size
;
4803 uint64_t spa
= hdr
->b_spa
;
4804 arc_buf_contents_t type
= arc_buf_type(hdr
);
4805 uint32_t flags
= hdr
->b_flags
;
4807 ASSERT(hdr
->b_l1hdr
.b_buf
!= buf
|| buf
->b_next
!= NULL
);
4809 * Pull the data off of this hdr and attach it to
4810 * a new anonymous hdr.
4812 (void) remove_reference(hdr
, hash_lock
, tag
);
4813 bufp
= &hdr
->b_l1hdr
.b_buf
;
4814 while (*bufp
!= buf
)
4815 bufp
= &(*bufp
)->b_next
;
4816 *bufp
= buf
->b_next
;
4819 ASSERT3P(state
, !=, arc_l2c_only
);
4821 (void) refcount_remove_many(
4822 &state
->arcs_size
, hdr
->b_size
, buf
);
4824 if (refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
)) {
4827 ASSERT3P(state
, !=, arc_l2c_only
);
4828 size
= &state
->arcs_lsize
[type
];
4829 ASSERT3U(*size
, >=, hdr
->b_size
);
4830 atomic_add_64(size
, -hdr
->b_size
);
4834 * We're releasing a duplicate user data buffer, update
4835 * our statistics accordingly.
4837 if (HDR_ISTYPE_DATA(hdr
)) {
4838 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers
);
4839 ARCSTAT_INCR(arcstat_duplicate_buffers_size
,
4842 hdr
->b_l1hdr
.b_datacnt
-= 1;
4843 arc_cksum_verify(buf
);
4844 arc_buf_unwatch(buf
);
4846 mutex_exit(hash_lock
);
4848 nhdr
= kmem_cache_alloc(hdr_full_cache
, KM_PUSHPAGE
);
4849 nhdr
->b_size
= blksz
;
4852 nhdr
->b_l1hdr
.b_mru_hits
= 0;
4853 nhdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
4854 nhdr
->b_l1hdr
.b_mfu_hits
= 0;
4855 nhdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
4856 nhdr
->b_l1hdr
.b_l2_hits
= 0;
4857 nhdr
->b_flags
= flags
& ARC_FLAG_L2_WRITING
;
4858 nhdr
->b_flags
|= arc_bufc_to_flags(type
);
4859 nhdr
->b_flags
|= ARC_FLAG_HAS_L1HDR
;
4861 nhdr
->b_l1hdr
.b_buf
= buf
;
4862 nhdr
->b_l1hdr
.b_datacnt
= 1;
4863 nhdr
->b_l1hdr
.b_state
= arc_anon
;
4864 nhdr
->b_l1hdr
.b_arc_access
= 0;
4865 nhdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
4866 nhdr
->b_freeze_cksum
= NULL
;
4868 (void) refcount_add(&nhdr
->b_l1hdr
.b_refcnt
, tag
);
4870 mutex_exit(&buf
->b_evict_lock
);
4871 (void) refcount_add_many(&arc_anon
->arcs_size
, blksz
, buf
);
4873 mutex_exit(&buf
->b_evict_lock
);
4874 ASSERT(refcount_count(&hdr
->b_l1hdr
.b_refcnt
) == 1);
4875 /* protected by hash lock, or hdr is on arc_anon */
4876 ASSERT(!multilist_link_active(&hdr
->b_l1hdr
.b_arc_node
));
4877 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
4878 hdr
->b_l1hdr
.b_mru_hits
= 0;
4879 hdr
->b_l1hdr
.b_mru_ghost_hits
= 0;
4880 hdr
->b_l1hdr
.b_mfu_hits
= 0;
4881 hdr
->b_l1hdr
.b_mfu_ghost_hits
= 0;
4882 hdr
->b_l1hdr
.b_l2_hits
= 0;
4883 arc_change_state(arc_anon
, hdr
, hash_lock
);
4884 hdr
->b_l1hdr
.b_arc_access
= 0;
4885 mutex_exit(hash_lock
);
4887 buf_discard_identity(hdr
);
4890 buf
->b_efunc
= NULL
;
4891 buf
->b_private
= NULL
;
4895 arc_released(arc_buf_t
*buf
)
4899 mutex_enter(&buf
->b_evict_lock
);
4900 released
= (buf
->b_data
!= NULL
&&
4901 buf
->b_hdr
->b_l1hdr
.b_state
== arc_anon
);
4902 mutex_exit(&buf
->b_evict_lock
);
4908 arc_referenced(arc_buf_t
*buf
)
4912 mutex_enter(&buf
->b_evict_lock
);
4913 referenced
= (refcount_count(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
4914 mutex_exit(&buf
->b_evict_lock
);
4915 return (referenced
);
4920 arc_write_ready(zio_t
*zio
)
4922 arc_write_callback_t
*callback
= zio
->io_private
;
4923 arc_buf_t
*buf
= callback
->awcb_buf
;
4924 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
4926 ASSERT(HDR_HAS_L1HDR(hdr
));
4927 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_l1hdr
.b_refcnt
));
4928 ASSERT(hdr
->b_l1hdr
.b_datacnt
> 0);
4929 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
4932 * If the IO is already in progress, then this is a re-write
4933 * attempt, so we need to thaw and re-compute the cksum.
4934 * It is the responsibility of the callback to handle the
4935 * accounting for any re-write attempt.
4937 if (HDR_IO_IN_PROGRESS(hdr
)) {
4938 mutex_enter(&hdr
->b_l1hdr
.b_freeze_lock
);
4939 if (hdr
->b_freeze_cksum
!= NULL
) {
4940 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
4941 hdr
->b_freeze_cksum
= NULL
;
4943 mutex_exit(&hdr
->b_l1hdr
.b_freeze_lock
);
4945 arc_cksum_compute(buf
, B_FALSE
);
4946 hdr
->b_flags
|= ARC_FLAG_IO_IN_PROGRESS
;
4950 * The SPA calls this callback for each physical write that happens on behalf
4951 * of a logical write. See the comment in dbuf_write_physdone() for details.
4954 arc_write_physdone(zio_t
*zio
)
4956 arc_write_callback_t
*cb
= zio
->io_private
;
4957 if (cb
->awcb_physdone
!= NULL
)
4958 cb
->awcb_physdone(zio
, cb
->awcb_buf
, cb
->awcb_private
);
4962 arc_write_done(zio_t
*zio
)
4964 arc_write_callback_t
*callback
= zio
->io_private
;
4965 arc_buf_t
*buf
= callback
->awcb_buf
;
4966 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
4968 ASSERT(hdr
->b_l1hdr
.b_acb
== NULL
);
4970 if (zio
->io_error
== 0) {
4971 if (BP_IS_HOLE(zio
->io_bp
) || BP_IS_EMBEDDED(zio
->io_bp
)) {
4972 buf_discard_identity(hdr
);
4974 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
4975 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
4978 ASSERT(BUF_EMPTY(hdr
));
4982 * If the block to be written was all-zero or compressed enough to be
4983 * embedded in the BP, no write was performed so there will be no
4984 * dva/birth/checksum. The buffer must therefore remain anonymous
4987 if (!BUF_EMPTY(hdr
)) {
4988 arc_buf_hdr_t
*exists
;
4989 kmutex_t
*hash_lock
;
4991 ASSERT(zio
->io_error
== 0);
4993 arc_cksum_verify(buf
);
4995 exists
= buf_hash_insert(hdr
, &hash_lock
);
4996 if (exists
!= NULL
) {
4998 * This can only happen if we overwrite for
4999 * sync-to-convergence, because we remove
5000 * buffers from the hash table when we arc_free().
5002 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
5003 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
5004 panic("bad overwrite, hdr=%p exists=%p",
5005 (void *)hdr
, (void *)exists
);
5006 ASSERT(refcount_is_zero(
5007 &exists
->b_l1hdr
.b_refcnt
));
5008 arc_change_state(arc_anon
, exists
, hash_lock
);
5009 mutex_exit(hash_lock
);
5010 arc_hdr_destroy(exists
);
5011 exists
= buf_hash_insert(hdr
, &hash_lock
);
5012 ASSERT3P(exists
, ==, NULL
);
5013 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
5015 ASSERT(zio
->io_prop
.zp_nopwrite
);
5016 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
5017 panic("bad nopwrite, hdr=%p exists=%p",
5018 (void *)hdr
, (void *)exists
);
5021 ASSERT(hdr
->b_l1hdr
.b_datacnt
== 1);
5022 ASSERT(hdr
->b_l1hdr
.b_state
== arc_anon
);
5023 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
5024 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
5027 hdr
->b_flags
&= ~ARC_FLAG_IO_IN_PROGRESS
;
5028 /* if it's not anon, we are doing a scrub */
5029 if (exists
== NULL
&& hdr
->b_l1hdr
.b_state
== arc_anon
)
5030 arc_access(hdr
, hash_lock
);
5031 mutex_exit(hash_lock
);
5033 hdr
->b_flags
&= ~ARC_FLAG_IO_IN_PROGRESS
;
5036 ASSERT(!refcount_is_zero(&hdr
->b_l1hdr
.b_refcnt
));
5037 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
5039 kmem_free(callback
, sizeof (arc_write_callback_t
));
5043 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
5044 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
, boolean_t l2arc_compress
,
5045 const zio_prop_t
*zp
, arc_done_func_t
*ready
, arc_done_func_t
*physdone
,
5046 arc_done_func_t
*done
, void *private, zio_priority_t priority
,
5047 int zio_flags
, const zbookmark_phys_t
*zb
)
5049 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
5050 arc_write_callback_t
*callback
;
5053 ASSERT(ready
!= NULL
);
5054 ASSERT(done
!= NULL
);
5055 ASSERT(!HDR_IO_ERROR(hdr
));
5056 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
5057 ASSERT(hdr
->b_l1hdr
.b_acb
== NULL
);
5058 ASSERT(hdr
->b_l1hdr
.b_datacnt
> 0);
5060 hdr
->b_flags
|= ARC_FLAG_L2CACHE
;
5062 hdr
->b_flags
|= ARC_FLAG_L2COMPRESS
;
5063 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
5064 callback
->awcb_ready
= ready
;
5065 callback
->awcb_physdone
= physdone
;
5066 callback
->awcb_done
= done
;
5067 callback
->awcb_private
= private;
5068 callback
->awcb_buf
= buf
;
5070 zio
= zio_write(pio
, spa
, txg
, bp
, buf
->b_data
, hdr
->b_size
, zp
,
5071 arc_write_ready
, arc_write_physdone
, arc_write_done
, callback
,
5072 priority
, zio_flags
, zb
);
5078 arc_memory_throttle(uint64_t reserve
, uint64_t txg
)
5081 if (zfs_arc_memory_throttle_disable
)
5084 if (freemem
> physmem
* arc_lotsfree_percent
/ 100)
5087 if (arc_reclaim_needed()) {
5088 /* memory is low, delay before restarting */
5089 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
5090 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
5091 return (SET_ERROR(EAGAIN
));
5098 arc_tempreserve_clear(uint64_t reserve
)
5100 atomic_add_64(&arc_tempreserve
, -reserve
);
5101 ASSERT((int64_t)arc_tempreserve
>= 0);
5105 arc_tempreserve_space(uint64_t reserve
, uint64_t txg
)
5110 if (reserve
> arc_c
/4 && !arc_no_grow
)
5111 arc_c
= MIN(arc_c_max
, reserve
* 4);
5114 * Throttle when the calculated memory footprint for the TXG
5115 * exceeds the target ARC size.
5117 if (reserve
> arc_c
) {
5118 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
5119 return (SET_ERROR(ERESTART
));
5123 * Don't count loaned bufs as in flight dirty data to prevent long
5124 * network delays from blocking transactions that are ready to be
5125 * assigned to a txg.
5127 anon_size
= MAX((int64_t)(refcount_count(&arc_anon
->arcs_size
) -
5128 arc_loaned_bytes
), 0);
5131 * Writes will, almost always, require additional memory allocations
5132 * in order to compress/encrypt/etc the data. We therefore need to
5133 * make sure that there is sufficient available memory for this.
5135 error
= arc_memory_throttle(reserve
, txg
);
5140 * Throttle writes when the amount of dirty data in the cache
5141 * gets too large. We try to keep the cache less than half full
5142 * of dirty blocks so that our sync times don't grow too large.
5143 * Note: if two requests come in concurrently, we might let them
5144 * both succeed, when one of them should fail. Not a huge deal.
5147 if (reserve
+ arc_tempreserve
+ anon_size
> arc_c
/ 2 &&
5148 anon_size
> arc_c
/ 4) {
5149 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
5150 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
5151 arc_tempreserve
>>10,
5152 arc_anon
->arcs_lsize
[ARC_BUFC_METADATA
]>>10,
5153 arc_anon
->arcs_lsize
[ARC_BUFC_DATA
]>>10,
5154 reserve
>>10, arc_c
>>10);
5155 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
5156 return (SET_ERROR(ERESTART
));
5158 atomic_add_64(&arc_tempreserve
, reserve
);
5163 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
5164 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
5166 size
->value
.ui64
= refcount_count(&state
->arcs_size
);
5167 evict_data
->value
.ui64
= state
->arcs_lsize
[ARC_BUFC_DATA
];
5168 evict_metadata
->value
.ui64
= state
->arcs_lsize
[ARC_BUFC_METADATA
];
5172 arc_kstat_update(kstat_t
*ksp
, int rw
)
5174 arc_stats_t
*as
= ksp
->ks_data
;
5176 if (rw
== KSTAT_WRITE
) {
5179 arc_kstat_update_state(arc_anon
,
5180 &as
->arcstat_anon_size
,
5181 &as
->arcstat_anon_evictable_data
,
5182 &as
->arcstat_anon_evictable_metadata
);
5183 arc_kstat_update_state(arc_mru
,
5184 &as
->arcstat_mru_size
,
5185 &as
->arcstat_mru_evictable_data
,
5186 &as
->arcstat_mru_evictable_metadata
);
5187 arc_kstat_update_state(arc_mru_ghost
,
5188 &as
->arcstat_mru_ghost_size
,
5189 &as
->arcstat_mru_ghost_evictable_data
,
5190 &as
->arcstat_mru_ghost_evictable_metadata
);
5191 arc_kstat_update_state(arc_mfu
,
5192 &as
->arcstat_mfu_size
,
5193 &as
->arcstat_mfu_evictable_data
,
5194 &as
->arcstat_mfu_evictable_metadata
);
5195 arc_kstat_update_state(arc_mfu_ghost
,
5196 &as
->arcstat_mfu_ghost_size
,
5197 &as
->arcstat_mfu_ghost_evictable_data
,
5198 &as
->arcstat_mfu_ghost_evictable_metadata
);
5205 * This function *must* return indices evenly distributed between all
5206 * sublists of the multilist. This is needed due to how the ARC eviction
5207 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
5208 * distributed between all sublists and uses this assumption when
5209 * deciding which sublist to evict from and how much to evict from it.
5212 arc_state_multilist_index_func(multilist_t
*ml
, void *obj
)
5214 arc_buf_hdr_t
*hdr
= obj
;
5217 * We rely on b_dva to generate evenly distributed index
5218 * numbers using buf_hash below. So, as an added precaution,
5219 * let's make sure we never add empty buffers to the arc lists.
5221 ASSERT(!BUF_EMPTY(hdr
));
5224 * The assumption here, is the hash value for a given
5225 * arc_buf_hdr_t will remain constant throughout its lifetime
5226 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
5227 * Thus, we don't need to store the header's sublist index
5228 * on insertion, as this index can be recalculated on removal.
5230 * Also, the low order bits of the hash value are thought to be
5231 * distributed evenly. Otherwise, in the case that the multilist
5232 * has a power of two number of sublists, each sublists' usage
5233 * would not be evenly distributed.
5235 return (buf_hash(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
) %
5236 multilist_get_num_sublists(ml
));
5240 * Called during module initialization and periodically thereafter to
5241 * apply reasonable changes to the exposed performance tunings. Non-zero
5242 * zfs_* values which differ from the currently set values will be applied.
5245 arc_tuning_update(void)
5247 /* Valid range: 64M - <all physical memory> */
5248 if ((zfs_arc_max
) && (zfs_arc_max
!= arc_c_max
) &&
5249 (zfs_arc_max
> 64 << 20) && (zfs_arc_max
< ptob(physmem
)) &&
5250 (zfs_arc_max
> arc_c_min
)) {
5251 arc_c_max
= zfs_arc_max
;
5253 arc_p
= (arc_c
>> 1);
5254 arc_meta_limit
= MIN(arc_meta_limit
, arc_c_max
);
5257 /* Valid range: 32M - <arc_c_max> */
5258 if ((zfs_arc_min
) && (zfs_arc_min
!= arc_c_min
) &&
5259 (zfs_arc_min
>= 2ULL << SPA_MAXBLOCKSHIFT
) &&
5260 (zfs_arc_min
<= arc_c_max
)) {
5261 arc_c_min
= zfs_arc_min
;
5262 arc_c
= MAX(arc_c
, arc_c_min
);
5265 /* Valid range: 16M - <arc_c_max> */
5266 if ((zfs_arc_meta_min
) && (zfs_arc_meta_min
!= arc_meta_min
) &&
5267 (zfs_arc_meta_min
>= 1ULL << SPA_MAXBLOCKSHIFT
) &&
5268 (zfs_arc_meta_min
<= arc_c_max
)) {
5269 arc_meta_min
= zfs_arc_meta_min
;
5270 arc_meta_limit
= MAX(arc_meta_limit
, arc_meta_min
);
5273 /* Valid range: <arc_meta_min> - <arc_c_max> */
5274 if ((zfs_arc_meta_limit
) && (zfs_arc_meta_limit
!= arc_meta_limit
) &&
5275 (zfs_arc_meta_limit
>= zfs_arc_meta_min
) &&
5276 (zfs_arc_meta_limit
<= arc_c_max
))
5277 arc_meta_limit
= zfs_arc_meta_limit
;
5279 /* Valid range: 1 - N */
5280 if (zfs_arc_grow_retry
)
5281 arc_grow_retry
= zfs_arc_grow_retry
;
5283 /* Valid range: 1 - N */
5284 if (zfs_arc_shrink_shift
) {
5285 arc_shrink_shift
= zfs_arc_shrink_shift
;
5286 arc_no_grow_shift
= MIN(arc_no_grow_shift
, arc_shrink_shift
-1);
5289 /* Valid range: 1 - N */
5290 if (zfs_arc_p_min_shift
)
5291 arc_p_min_shift
= zfs_arc_p_min_shift
;
5293 /* Valid range: 1 - N ticks */
5294 if (zfs_arc_min_prefetch_lifespan
)
5295 arc_min_prefetch_lifespan
= zfs_arc_min_prefetch_lifespan
;
5302 * allmem is "all memory that we could possibly use".
5305 uint64_t allmem
= ptob(physmem
);
5307 uint64_t allmem
= (physmem
* PAGESIZE
) / 2;
5310 mutex_init(&arc_reclaim_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5311 cv_init(&arc_reclaim_thread_cv
, NULL
, CV_DEFAULT
, NULL
);
5312 cv_init(&arc_reclaim_waiters_cv
, NULL
, CV_DEFAULT
, NULL
);
5314 mutex_init(&arc_user_evicts_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5315 cv_init(&arc_user_evicts_cv
, NULL
, CV_DEFAULT
, NULL
);
5317 /* Convert seconds to clock ticks */
5318 arc_min_prefetch_lifespan
= 1 * hz
;
5320 /* Start out with 1/8 of all memory */
5325 * On architectures where the physical memory can be larger
5326 * than the addressable space (intel in 32-bit mode), we may
5327 * need to limit the cache to 1/8 of VM size.
5329 arc_c
= MIN(arc_c
, vmem_size(heap_arena
, VMEM_ALLOC
| VMEM_FREE
) / 8);
5332 * Register a shrinker to support synchronous (direct) memory
5333 * reclaim from the arc. This is done to prevent kswapd from
5334 * swapping out pages when it is preferable to shrink the arc.
5336 spl_register_shrinker(&arc_shrinker
);
5339 /* Set min cache to allow safe operation of arc_adapt() */
5340 arc_c_min
= 2ULL << SPA_MAXBLOCKSHIFT
;
5341 /* Set max to 1/2 of all memory */
5342 arc_c_max
= allmem
/ 2;
5345 arc_p
= (arc_c
>> 1);
5347 /* Set min to 1/2 of arc_c_min */
5348 arc_meta_min
= 1ULL << SPA_MAXBLOCKSHIFT
;
5349 /* Initialize maximum observed usage to zero */
5351 /* Set limit to 3/4 of arc_c_max with a floor of arc_meta_min */
5352 arc_meta_limit
= MAX((3 * arc_c_max
) / 4, arc_meta_min
);
5354 /* Apply user specified tunings */
5355 arc_tuning_update();
5357 if (zfs_arc_num_sublists_per_state
< 1)
5358 zfs_arc_num_sublists_per_state
= MAX(boot_ncpus
, 1);
5360 /* if kmem_flags are set, lets try to use less memory */
5361 if (kmem_debugging())
5363 if (arc_c
< arc_c_min
)
5366 arc_anon
= &ARC_anon
;
5368 arc_mru_ghost
= &ARC_mru_ghost
;
5370 arc_mfu_ghost
= &ARC_mfu_ghost
;
5371 arc_l2c_only
= &ARC_l2c_only
;
5374 multilist_create(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
5375 sizeof (arc_buf_hdr_t
),
5376 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5377 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5378 multilist_create(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
5379 sizeof (arc_buf_hdr_t
),
5380 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5381 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5382 multilist_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
5383 sizeof (arc_buf_hdr_t
),
5384 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5385 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5386 multilist_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
5387 sizeof (arc_buf_hdr_t
),
5388 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5389 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5390 multilist_create(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
5391 sizeof (arc_buf_hdr_t
),
5392 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5393 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5394 multilist_create(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
5395 sizeof (arc_buf_hdr_t
),
5396 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5397 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5398 multilist_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
5399 sizeof (arc_buf_hdr_t
),
5400 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5401 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5402 multilist_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
5403 sizeof (arc_buf_hdr_t
),
5404 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5405 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5406 multilist_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
5407 sizeof (arc_buf_hdr_t
),
5408 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5409 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5410 multilist_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
5411 sizeof (arc_buf_hdr_t
),
5412 offsetof(arc_buf_hdr_t
, b_l1hdr
.b_arc_node
),
5413 zfs_arc_num_sublists_per_state
, arc_state_multilist_index_func
);
5415 arc_anon
->arcs_state
= ARC_STATE_ANON
;
5416 arc_mru
->arcs_state
= ARC_STATE_MRU
;
5417 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
5418 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
5419 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
5420 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
5422 refcount_create(&arc_anon
->arcs_size
);
5423 refcount_create(&arc_mru
->arcs_size
);
5424 refcount_create(&arc_mru_ghost
->arcs_size
);
5425 refcount_create(&arc_mfu
->arcs_size
);
5426 refcount_create(&arc_mfu_ghost
->arcs_size
);
5427 refcount_create(&arc_l2c_only
->arcs_size
);
5431 arc_reclaim_thread_exit
= FALSE
;
5432 arc_user_evicts_thread_exit
= FALSE
;
5433 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
5434 offsetof(arc_prune_t
, p_node
));
5435 arc_eviction_list
= NULL
;
5436 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
5437 bzero(&arc_eviction_hdr
, sizeof (arc_buf_hdr_t
));
5439 arc_prune_taskq
= taskq_create("arc_prune", max_ncpus
, minclsyspri
,
5440 max_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
| TASKQ_DYNAMIC
);
5442 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
5443 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
5445 if (arc_ksp
!= NULL
) {
5446 arc_ksp
->ks_data
= &arc_stats
;
5447 arc_ksp
->ks_update
= arc_kstat_update
;
5448 kstat_install(arc_ksp
);
5451 (void) thread_create(NULL
, 0, arc_reclaim_thread
, NULL
, 0, &p0
,
5452 TS_RUN
, minclsyspri
);
5454 (void) thread_create(NULL
, 0, arc_user_evicts_thread
, NULL
, 0, &p0
,
5455 TS_RUN
, minclsyspri
);
5461 * Calculate maximum amount of dirty data per pool.
5463 * If it has been set by a module parameter, take that.
5464 * Otherwise, use a percentage of physical memory defined by
5465 * zfs_dirty_data_max_percent (default 10%) with a cap at
5466 * zfs_dirty_data_max_max (default 25% of physical memory).
5468 if (zfs_dirty_data_max_max
== 0)
5469 zfs_dirty_data_max_max
= physmem
* PAGESIZE
*
5470 zfs_dirty_data_max_max_percent
/ 100;
5472 if (zfs_dirty_data_max
== 0) {
5473 zfs_dirty_data_max
= physmem
* PAGESIZE
*
5474 zfs_dirty_data_max_percent
/ 100;
5475 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
5476 zfs_dirty_data_max_max
);
5486 spl_unregister_shrinker(&arc_shrinker
);
5487 #endif /* _KERNEL */
5489 mutex_enter(&arc_reclaim_lock
);
5490 arc_reclaim_thread_exit
= TRUE
;
5492 * The reclaim thread will set arc_reclaim_thread_exit back to
5493 * FALSE when it is finished exiting; we're waiting for that.
5495 while (arc_reclaim_thread_exit
) {
5496 cv_signal(&arc_reclaim_thread_cv
);
5497 cv_wait(&arc_reclaim_thread_cv
, &arc_reclaim_lock
);
5499 mutex_exit(&arc_reclaim_lock
);
5501 mutex_enter(&arc_user_evicts_lock
);
5502 arc_user_evicts_thread_exit
= TRUE
;
5504 * The user evicts thread will set arc_user_evicts_thread_exit
5505 * to FALSE when it is finished exiting; we're waiting for that.
5507 while (arc_user_evicts_thread_exit
) {
5508 cv_signal(&arc_user_evicts_cv
);
5509 cv_wait(&arc_user_evicts_cv
, &arc_user_evicts_lock
);
5511 mutex_exit(&arc_user_evicts_lock
);
5513 /* Use TRUE to ensure *all* buffers are evicted */
5514 arc_flush(NULL
, TRUE
);
5518 if (arc_ksp
!= NULL
) {
5519 kstat_delete(arc_ksp
);
5523 taskq_wait(arc_prune_taskq
);
5524 taskq_destroy(arc_prune_taskq
);
5526 mutex_enter(&arc_prune_mtx
);
5527 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
5528 list_remove(&arc_prune_list
, p
);
5529 refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
5530 refcount_destroy(&p
->p_refcnt
);
5531 kmem_free(p
, sizeof (*p
));
5533 mutex_exit(&arc_prune_mtx
);
5535 list_destroy(&arc_prune_list
);
5536 mutex_destroy(&arc_prune_mtx
);
5537 mutex_destroy(&arc_reclaim_lock
);
5538 cv_destroy(&arc_reclaim_thread_cv
);
5539 cv_destroy(&arc_reclaim_waiters_cv
);
5541 mutex_destroy(&arc_user_evicts_lock
);
5542 cv_destroy(&arc_user_evicts_cv
);
5544 refcount_destroy(&arc_anon
->arcs_size
);
5545 refcount_destroy(&arc_mru
->arcs_size
);
5546 refcount_destroy(&arc_mru_ghost
->arcs_size
);
5547 refcount_destroy(&arc_mfu
->arcs_size
);
5548 refcount_destroy(&arc_mfu_ghost
->arcs_size
);
5549 refcount_destroy(&arc_l2c_only
->arcs_size
);
5551 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
5552 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
5553 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
5554 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
5555 multilist_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
5556 multilist_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
5557 multilist_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
5558 multilist_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
5559 multilist_destroy(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
]);
5560 multilist_destroy(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
]);
5564 ASSERT0(arc_loaned_bytes
);
5570 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
5571 * It uses dedicated storage devices to hold cached data, which are populated
5572 * using large infrequent writes. The main role of this cache is to boost
5573 * the performance of random read workloads. The intended L2ARC devices
5574 * include short-stroked disks, solid state disks, and other media with
5575 * substantially faster read latency than disk.
5577 * +-----------------------+
5579 * +-----------------------+
5582 * l2arc_feed_thread() arc_read()
5586 * +---------------+ |
5588 * +---------------+ |
5593 * +-------+ +-------+
5595 * | cache | | cache |
5596 * +-------+ +-------+
5597 * +=========+ .-----.
5598 * : L2ARC : |-_____-|
5599 * : devices : | Disks |
5600 * +=========+ `-_____-'
5602 * Read requests are satisfied from the following sources, in order:
5605 * 2) vdev cache of L2ARC devices
5607 * 4) vdev cache of disks
5610 * Some L2ARC device types exhibit extremely slow write performance.
5611 * To accommodate for this there are some significant differences between
5612 * the L2ARC and traditional cache design:
5614 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
5615 * the ARC behave as usual, freeing buffers and placing headers on ghost
5616 * lists. The ARC does not send buffers to the L2ARC during eviction as
5617 * this would add inflated write latencies for all ARC memory pressure.
5619 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
5620 * It does this by periodically scanning buffers from the eviction-end of
5621 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
5622 * not already there. It scans until a headroom of buffers is satisfied,
5623 * which itself is a buffer for ARC eviction. If a compressible buffer is
5624 * found during scanning and selected for writing to an L2ARC device, we
5625 * temporarily boost scanning headroom during the next scan cycle to make
5626 * sure we adapt to compression effects (which might significantly reduce
5627 * the data volume we write to L2ARC). The thread that does this is
5628 * l2arc_feed_thread(), illustrated below; example sizes are included to
5629 * provide a better sense of ratio than this diagram:
5632 * +---------------------+----------+
5633 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
5634 * +---------------------+----------+ | o L2ARC eligible
5635 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
5636 * +---------------------+----------+ |
5637 * 15.9 Gbytes ^ 32 Mbytes |
5639 * l2arc_feed_thread()
5641 * l2arc write hand <--[oooo]--'
5645 * +==============================+
5646 * L2ARC dev |####|#|###|###| |####| ... |
5647 * +==============================+
5650 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
5651 * evicted, then the L2ARC has cached a buffer much sooner than it probably
5652 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
5653 * safe to say that this is an uncommon case, since buffers at the end of
5654 * the ARC lists have moved there due to inactivity.
5656 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
5657 * then the L2ARC simply misses copying some buffers. This serves as a
5658 * pressure valve to prevent heavy read workloads from both stalling the ARC
5659 * with waits and clogging the L2ARC with writes. This also helps prevent
5660 * the potential for the L2ARC to churn if it attempts to cache content too
5661 * quickly, such as during backups of the entire pool.
5663 * 5. After system boot and before the ARC has filled main memory, there are
5664 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
5665 * lists can remain mostly static. Instead of searching from tail of these
5666 * lists as pictured, the l2arc_feed_thread() will search from the list heads
5667 * for eligible buffers, greatly increasing its chance of finding them.
5669 * The L2ARC device write speed is also boosted during this time so that
5670 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
5671 * there are no L2ARC reads, and no fear of degrading read performance
5672 * through increased writes.
5674 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
5675 * the vdev queue can aggregate them into larger and fewer writes. Each
5676 * device is written to in a rotor fashion, sweeping writes through
5677 * available space then repeating.
5679 * 7. The L2ARC does not store dirty content. It never needs to flush
5680 * write buffers back to disk based storage.
5682 * 8. If an ARC buffer is written (and dirtied) which also exists in the
5683 * L2ARC, the now stale L2ARC buffer is immediately dropped.
5685 * The performance of the L2ARC can be tweaked by a number of tunables, which
5686 * may be necessary for different workloads:
5688 * l2arc_write_max max write bytes per interval
5689 * l2arc_write_boost extra write bytes during device warmup
5690 * l2arc_noprefetch skip caching prefetched buffers
5691 * l2arc_nocompress skip compressing buffers
5692 * l2arc_headroom number of max device writes to precache
5693 * l2arc_headroom_boost when we find compressed buffers during ARC
5694 * scanning, we multiply headroom by this
5695 * percentage factor for the next scan cycle,
5696 * since more compressed buffers are likely to
5698 * l2arc_feed_secs seconds between L2ARC writing
5700 * Tunables may be removed or added as future performance improvements are
5701 * integrated, and also may become zpool properties.
5703 * There are three key functions that control how the L2ARC warms up:
5705 * l2arc_write_eligible() check if a buffer is eligible to cache
5706 * l2arc_write_size() calculate how much to write
5707 * l2arc_write_interval() calculate sleep delay between writes
5709 * These three functions determine what to write, how much, and how quickly
5714 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*hdr
)
5717 * A buffer is *not* eligible for the L2ARC if it:
5718 * 1. belongs to a different spa.
5719 * 2. is already cached on the L2ARC.
5720 * 3. has an I/O in progress (it may be an incomplete read).
5721 * 4. is flagged not eligible (zfs property).
5723 if (hdr
->b_spa
!= spa_guid
|| HDR_HAS_L2HDR(hdr
) ||
5724 HDR_IO_IN_PROGRESS(hdr
) || !HDR_L2CACHE(hdr
))
5731 l2arc_write_size(void)
5736 * Make sure our globals have meaningful values in case the user
5739 size
= l2arc_write_max
;
5741 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
5742 "be greater than zero, resetting it to the default (%d)",
5744 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
5747 if (arc_warm
== B_FALSE
)
5748 size
+= l2arc_write_boost
;
5755 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
5757 clock_t interval
, next
, now
;
5760 * If the ARC lists are busy, increase our write rate; if the
5761 * lists are stale, idle back. This is achieved by checking
5762 * how much we previously wrote - if it was more than half of
5763 * what we wanted, schedule the next write much sooner.
5765 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
5766 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
5768 interval
= hz
* l2arc_feed_secs
;
5770 now
= ddi_get_lbolt();
5771 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
5777 * Cycle through L2ARC devices. This is how L2ARC load balances.
5778 * If a device is returned, this also returns holding the spa config lock.
5780 static l2arc_dev_t
*
5781 l2arc_dev_get_next(void)
5783 l2arc_dev_t
*first
, *next
= NULL
;
5786 * Lock out the removal of spas (spa_namespace_lock), then removal
5787 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
5788 * both locks will be dropped and a spa config lock held instead.
5790 mutex_enter(&spa_namespace_lock
);
5791 mutex_enter(&l2arc_dev_mtx
);
5793 /* if there are no vdevs, there is nothing to do */
5794 if (l2arc_ndev
== 0)
5798 next
= l2arc_dev_last
;
5800 /* loop around the list looking for a non-faulted vdev */
5802 next
= list_head(l2arc_dev_list
);
5804 next
= list_next(l2arc_dev_list
, next
);
5806 next
= list_head(l2arc_dev_list
);
5809 /* if we have come back to the start, bail out */
5812 else if (next
== first
)
5815 } while (vdev_is_dead(next
->l2ad_vdev
));
5817 /* if we were unable to find any usable vdevs, return NULL */
5818 if (vdev_is_dead(next
->l2ad_vdev
))
5821 l2arc_dev_last
= next
;
5824 mutex_exit(&l2arc_dev_mtx
);
5827 * Grab the config lock to prevent the 'next' device from being
5828 * removed while we are writing to it.
5831 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
5832 mutex_exit(&spa_namespace_lock
);
5838 * Free buffers that were tagged for destruction.
5841 l2arc_do_free_on_write(void)
5844 l2arc_data_free_t
*df
, *df_prev
;
5846 mutex_enter(&l2arc_free_on_write_mtx
);
5847 buflist
= l2arc_free_on_write
;
5849 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
5850 df_prev
= list_prev(buflist
, df
);
5851 ASSERT(df
->l2df_data
!= NULL
);
5852 ASSERT(df
->l2df_func
!= NULL
);
5853 df
->l2df_func(df
->l2df_data
, df
->l2df_size
);
5854 list_remove(buflist
, df
);
5855 kmem_free(df
, sizeof (l2arc_data_free_t
));
5858 mutex_exit(&l2arc_free_on_write_mtx
);
5862 * A write to a cache device has completed. Update all headers to allow
5863 * reads from these buffers to begin.
5866 l2arc_write_done(zio_t
*zio
)
5868 l2arc_write_callback_t
*cb
;
5871 arc_buf_hdr_t
*head
, *hdr
, *hdr_prev
;
5872 kmutex_t
*hash_lock
;
5873 int64_t bytes_dropped
= 0;
5875 cb
= zio
->io_private
;
5877 dev
= cb
->l2wcb_dev
;
5878 ASSERT(dev
!= NULL
);
5879 head
= cb
->l2wcb_head
;
5880 ASSERT(head
!= NULL
);
5881 buflist
= &dev
->l2ad_buflist
;
5882 ASSERT(buflist
!= NULL
);
5883 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
5884 l2arc_write_callback_t
*, cb
);
5886 if (zio
->io_error
!= 0)
5887 ARCSTAT_BUMP(arcstat_l2_writes_error
);
5890 * All writes completed, or an error was hit.
5893 mutex_enter(&dev
->l2ad_mtx
);
5894 for (hdr
= list_prev(buflist
, head
); hdr
; hdr
= hdr_prev
) {
5895 hdr_prev
= list_prev(buflist
, hdr
);
5897 hash_lock
= HDR_LOCK(hdr
);
5900 * We cannot use mutex_enter or else we can deadlock
5901 * with l2arc_write_buffers (due to swapping the order
5902 * the hash lock and l2ad_mtx are taken).
5904 if (!mutex_tryenter(hash_lock
)) {
5906 * Missed the hash lock. We must retry so we
5907 * don't leave the ARC_FLAG_L2_WRITING bit set.
5909 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry
);
5912 * We don't want to rescan the headers we've
5913 * already marked as having been written out, so
5914 * we reinsert the head node so we can pick up
5915 * where we left off.
5917 list_remove(buflist
, head
);
5918 list_insert_after(buflist
, hdr
, head
);
5920 mutex_exit(&dev
->l2ad_mtx
);
5923 * We wait for the hash lock to become available
5924 * to try and prevent busy waiting, and increase
5925 * the chance we'll be able to acquire the lock
5926 * the next time around.
5928 mutex_enter(hash_lock
);
5929 mutex_exit(hash_lock
);
5934 * We could not have been moved into the arc_l2c_only
5935 * state while in-flight due to our ARC_FLAG_L2_WRITING
5936 * bit being set. Let's just ensure that's being enforced.
5938 ASSERT(HDR_HAS_L1HDR(hdr
));
5941 * We may have allocated a buffer for L2ARC compression,
5942 * we must release it to avoid leaking this data.
5944 l2arc_release_cdata_buf(hdr
);
5946 if (zio
->io_error
!= 0) {
5948 * Error - drop L2ARC entry.
5950 list_remove(buflist
, hdr
);
5951 hdr
->b_flags
&= ~ARC_FLAG_HAS_L2HDR
;
5953 ARCSTAT_INCR(arcstat_l2_asize
, -hdr
->b_l2hdr
.b_asize
);
5954 ARCSTAT_INCR(arcstat_l2_size
, -hdr
->b_size
);
5956 bytes_dropped
+= hdr
->b_l2hdr
.b_asize
;
5957 (void) refcount_remove_many(&dev
->l2ad_alloc
,
5958 hdr
->b_l2hdr
.b_asize
, hdr
);
5962 * Allow ARC to begin reads and ghost list evictions to
5965 hdr
->b_flags
&= ~ARC_FLAG_L2_WRITING
;
5967 mutex_exit(hash_lock
);
5970 atomic_inc_64(&l2arc_writes_done
);
5971 list_remove(buflist
, head
);
5972 ASSERT(!HDR_HAS_L1HDR(head
));
5973 kmem_cache_free(hdr_l2only_cache
, head
);
5974 mutex_exit(&dev
->l2ad_mtx
);
5976 vdev_space_update(dev
->l2ad_vdev
, -bytes_dropped
, 0, 0);
5978 l2arc_do_free_on_write();
5980 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
5984 * A read to a cache device completed. Validate buffer contents before
5985 * handing over to the regular ARC routines.
5988 l2arc_read_done(zio_t
*zio
)
5990 l2arc_read_callback_t
*cb
;
5993 kmutex_t
*hash_lock
;
5996 ASSERT(zio
->io_vd
!= NULL
);
5997 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
5999 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
6001 cb
= zio
->io_private
;
6003 buf
= cb
->l2rcb_buf
;
6004 ASSERT(buf
!= NULL
);
6006 hash_lock
= HDR_LOCK(buf
->b_hdr
);
6007 mutex_enter(hash_lock
);
6009 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
6012 * If the buffer was compressed, decompress it first.
6014 if (cb
->l2rcb_compress
!= ZIO_COMPRESS_OFF
)
6015 l2arc_decompress_zio(zio
, hdr
, cb
->l2rcb_compress
);
6016 ASSERT(zio
->io_data
!= NULL
);
6019 * Check this survived the L2ARC journey.
6021 equal
= arc_cksum_equal(buf
);
6022 if (equal
&& zio
->io_error
== 0 && !HDR_L2_EVICTED(hdr
)) {
6023 mutex_exit(hash_lock
);
6024 zio
->io_private
= buf
;
6025 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
6026 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
6029 mutex_exit(hash_lock
);
6031 * Buffer didn't survive caching. Increment stats and
6032 * reissue to the original storage device.
6034 if (zio
->io_error
!= 0) {
6035 ARCSTAT_BUMP(arcstat_l2_io_error
);
6037 zio
->io_error
= SET_ERROR(EIO
);
6040 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
6043 * If there's no waiter, issue an async i/o to the primary
6044 * storage now. If there *is* a waiter, the caller must
6045 * issue the i/o in a context where it's OK to block.
6047 if (zio
->io_waiter
== NULL
) {
6048 zio_t
*pio
= zio_unique_parent(zio
);
6050 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
6052 zio_nowait(zio_read(pio
, cb
->l2rcb_spa
, &cb
->l2rcb_bp
,
6053 buf
->b_data
, zio
->io_size
, arc_read_done
, buf
,
6054 zio
->io_priority
, cb
->l2rcb_flags
, &cb
->l2rcb_zb
));
6058 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
6062 * This is the list priority from which the L2ARC will search for pages to
6063 * cache. This is used within loops (0..3) to cycle through lists in the
6064 * desired order. This order can have a significant effect on cache
6067 * Currently the metadata lists are hit first, MFU then MRU, followed by
6068 * the data lists. This function returns a locked list, and also returns
6071 static multilist_sublist_t
*
6072 l2arc_sublist_lock(int list_num
)
6074 multilist_t
*ml
= NULL
;
6077 ASSERT(list_num
>= 0 && list_num
<= 3);
6081 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
6084 ml
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
6087 ml
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
6090 ml
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
6095 * Return a randomly-selected sublist. This is acceptable
6096 * because the caller feeds only a little bit of data for each
6097 * call (8MB). Subsequent calls will result in different
6098 * sublists being selected.
6100 idx
= multilist_get_random_index(ml
);
6101 return (multilist_sublist_lock(ml
, idx
));
6105 * Evict buffers from the device write hand to the distance specified in
6106 * bytes. This distance may span populated buffers, it may span nothing.
6107 * This is clearing a region on the L2ARC device ready for writing.
6108 * If the 'all' boolean is set, every buffer is evicted.
6111 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
6114 arc_buf_hdr_t
*hdr
, *hdr_prev
;
6115 kmutex_t
*hash_lock
;
6118 buflist
= &dev
->l2ad_buflist
;
6120 if (!all
&& dev
->l2ad_first
) {
6122 * This is the first sweep through the device. There is
6128 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
6130 * When nearing the end of the device, evict to the end
6131 * before the device write hand jumps to the start.
6133 taddr
= dev
->l2ad_end
;
6135 taddr
= dev
->l2ad_hand
+ distance
;
6137 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
6138 uint64_t, taddr
, boolean_t
, all
);
6141 mutex_enter(&dev
->l2ad_mtx
);
6142 for (hdr
= list_tail(buflist
); hdr
; hdr
= hdr_prev
) {
6143 hdr_prev
= list_prev(buflist
, hdr
);
6145 hash_lock
= HDR_LOCK(hdr
);
6148 * We cannot use mutex_enter or else we can deadlock
6149 * with l2arc_write_buffers (due to swapping the order
6150 * the hash lock and l2ad_mtx are taken).
6152 if (!mutex_tryenter(hash_lock
)) {
6154 * Missed the hash lock. Retry.
6156 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
6157 mutex_exit(&dev
->l2ad_mtx
);
6158 mutex_enter(hash_lock
);
6159 mutex_exit(hash_lock
);
6163 if (HDR_L2_WRITE_HEAD(hdr
)) {
6165 * We hit a write head node. Leave it for
6166 * l2arc_write_done().
6168 list_remove(buflist
, hdr
);
6169 mutex_exit(hash_lock
);
6173 if (!all
&& HDR_HAS_L2HDR(hdr
) &&
6174 (hdr
->b_l2hdr
.b_daddr
> taddr
||
6175 hdr
->b_l2hdr
.b_daddr
< dev
->l2ad_hand
)) {
6177 * We've evicted to the target address,
6178 * or the end of the device.
6180 mutex_exit(hash_lock
);
6184 ASSERT(HDR_HAS_L2HDR(hdr
));
6185 if (!HDR_HAS_L1HDR(hdr
)) {
6186 ASSERT(!HDR_L2_READING(hdr
));
6188 * This doesn't exist in the ARC. Destroy.
6189 * arc_hdr_destroy() will call list_remove()
6190 * and decrement arcstat_l2_size.
6192 arc_change_state(arc_anon
, hdr
, hash_lock
);
6193 arc_hdr_destroy(hdr
);
6195 ASSERT(hdr
->b_l1hdr
.b_state
!= arc_l2c_only
);
6196 ARCSTAT_BUMP(arcstat_l2_evict_l1cached
);
6198 * Invalidate issued or about to be issued
6199 * reads, since we may be about to write
6200 * over this location.
6202 if (HDR_L2_READING(hdr
)) {
6203 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
6204 hdr
->b_flags
|= ARC_FLAG_L2_EVICTED
;
6207 /* Ensure this header has finished being written */
6208 ASSERT(!HDR_L2_WRITING(hdr
));
6209 ASSERT3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
6211 arc_hdr_l2hdr_destroy(hdr
);
6213 mutex_exit(hash_lock
);
6215 mutex_exit(&dev
->l2ad_mtx
);
6219 * Find and write ARC buffers to the L2ARC device.
6221 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
6222 * for reading until they have completed writing.
6223 * The headroom_boost is an in-out parameter used to maintain headroom boost
6224 * state between calls to this function.
6226 * Returns the number of bytes actually written (which may be smaller than
6227 * the delta by which the device hand has changed due to alignment).
6230 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
,
6231 boolean_t
*headroom_boost
)
6233 arc_buf_hdr_t
*hdr
, *hdr_prev
, *head
;
6234 uint64_t write_asize
, write_sz
, headroom
, buf_compress_minsz
,
6238 l2arc_write_callback_t
*cb
;
6240 uint64_t guid
= spa_load_guid(spa
);
6242 const boolean_t do_headroom_boost
= *headroom_boost
;
6244 ASSERT(dev
->l2ad_vdev
!= NULL
);
6246 /* Lower the flag now, we might want to raise it again later. */
6247 *headroom_boost
= B_FALSE
;
6250 write_sz
= write_asize
= 0;
6252 head
= kmem_cache_alloc(hdr_l2only_cache
, KM_PUSHPAGE
);
6253 head
->b_flags
|= ARC_FLAG_L2_WRITE_HEAD
;
6254 head
->b_flags
|= ARC_FLAG_HAS_L2HDR
;
6257 * We will want to try to compress buffers that are at least 2x the
6258 * device sector size.
6260 buf_compress_minsz
= 2 << dev
->l2ad_vdev
->vdev_ashift
;
6263 * Copy buffers for L2ARC writing.
6265 for (try = 0; try <= 3; try++) {
6266 multilist_sublist_t
*mls
= l2arc_sublist_lock(try);
6267 uint64_t passed_sz
= 0;
6270 * L2ARC fast warmup.
6272 * Until the ARC is warm and starts to evict, read from the
6273 * head of the ARC lists rather than the tail.
6275 if (arc_warm
== B_FALSE
)
6276 hdr
= multilist_sublist_head(mls
);
6278 hdr
= multilist_sublist_tail(mls
);
6280 headroom
= target_sz
* l2arc_headroom
;
6281 if (do_headroom_boost
)
6282 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
6284 for (; hdr
; hdr
= hdr_prev
) {
6285 kmutex_t
*hash_lock
;
6289 if (arc_warm
== B_FALSE
)
6290 hdr_prev
= multilist_sublist_next(mls
, hdr
);
6292 hdr_prev
= multilist_sublist_prev(mls
, hdr
);
6294 hash_lock
= HDR_LOCK(hdr
);
6295 if (!mutex_tryenter(hash_lock
)) {
6297 * Skip this buffer rather than waiting.
6302 passed_sz
+= hdr
->b_size
;
6303 if (passed_sz
> headroom
) {
6307 mutex_exit(hash_lock
);
6311 if (!l2arc_write_eligible(guid
, hdr
)) {
6312 mutex_exit(hash_lock
);
6317 * Assume that the buffer is not going to be compressed
6318 * and could take more space on disk because of a larger
6321 buf_sz
= hdr
->b_size
;
6322 buf_a_sz
= vdev_psize_to_asize(dev
->l2ad_vdev
, buf_sz
);
6324 if ((write_asize
+ buf_a_sz
) > target_sz
) {
6326 mutex_exit(hash_lock
);
6332 * Insert a dummy header on the buflist so
6333 * l2arc_write_done() can find where the
6334 * write buffers begin without searching.
6336 mutex_enter(&dev
->l2ad_mtx
);
6337 list_insert_head(&dev
->l2ad_buflist
, head
);
6338 mutex_exit(&dev
->l2ad_mtx
);
6340 cb
= kmem_alloc(sizeof (l2arc_write_callback_t
),
6342 cb
->l2wcb_dev
= dev
;
6343 cb
->l2wcb_head
= head
;
6344 pio
= zio_root(spa
, l2arc_write_done
, cb
,
6349 * Create and add a new L2ARC header.
6351 hdr
->b_l2hdr
.b_dev
= dev
;
6352 arc_space_consume(HDR_L2ONLY_SIZE
, ARC_SPACE_L2HDRS
);
6353 hdr
->b_flags
|= ARC_FLAG_L2_WRITING
;
6355 * Temporarily stash the data buffer in b_tmp_cdata.
6356 * The subsequent write step will pick it up from
6357 * there. This is because can't access b_l1hdr.b_buf
6358 * without holding the hash_lock, which we in turn
6359 * can't access without holding the ARC list locks
6360 * (which we want to avoid during compression/writing)
6362 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_OFF
);
6363 hdr
->b_l2hdr
.b_asize
= hdr
->b_size
;
6364 hdr
->b_l2hdr
.b_hits
= 0;
6365 hdr
->b_l1hdr
.b_tmp_cdata
= hdr
->b_l1hdr
.b_buf
->b_data
;
6368 * Explicitly set the b_daddr field to a known
6369 * value which means "invalid address". This
6370 * enables us to differentiate which stage of
6371 * l2arc_write_buffers() the particular header
6372 * is in (e.g. this loop, or the one below).
6373 * ARC_FLAG_L2_WRITING is not enough to make
6374 * this distinction, and we need to know in
6375 * order to do proper l2arc vdev accounting in
6376 * arc_release() and arc_hdr_destroy().
6378 * Note, we can't use a new flag to distinguish
6379 * the two stages because we don't hold the
6380 * header's hash_lock below, in the second stage
6381 * of this function. Thus, we can't simply
6382 * change the b_flags field to denote that the
6383 * IO has been sent. We can change the b_daddr
6384 * field of the L2 portion, though, since we'll
6385 * be holding the l2ad_mtx; which is why we're
6386 * using it to denote the header's state change.
6388 hdr
->b_l2hdr
.b_daddr
= L2ARC_ADDR_UNSET
;
6389 hdr
->b_flags
|= ARC_FLAG_HAS_L2HDR
;
6391 mutex_enter(&dev
->l2ad_mtx
);
6392 list_insert_head(&dev
->l2ad_buflist
, hdr
);
6393 mutex_exit(&dev
->l2ad_mtx
);
6396 * Compute and store the buffer cksum before
6397 * writing. On debug the cksum is verified first.
6399 arc_cksum_verify(hdr
->b_l1hdr
.b_buf
);
6400 arc_cksum_compute(hdr
->b_l1hdr
.b_buf
, B_TRUE
);
6402 mutex_exit(hash_lock
);
6405 write_asize
+= buf_a_sz
;
6408 multilist_sublist_unlock(mls
);
6414 /* No buffers selected for writing? */
6417 ASSERT(!HDR_HAS_L1HDR(head
));
6418 kmem_cache_free(hdr_l2only_cache
, head
);
6422 mutex_enter(&dev
->l2ad_mtx
);
6425 * Note that elsewhere in this file arcstat_l2_asize
6426 * and the used space on l2ad_vdev are updated using b_asize,
6427 * which is not necessarily rounded up to the device block size.
6428 * Too keep accounting consistent we do the same here as well:
6429 * stats_size accumulates the sum of b_asize of the written buffers,
6430 * while write_asize accumulates the sum of b_asize rounded up
6431 * to the device block size.
6432 * The latter sum is used only to validate the corectness of the code.
6438 * Now start writing the buffers. We're starting at the write head
6439 * and work backwards, retracing the course of the buffer selector
6442 for (hdr
= list_prev(&dev
->l2ad_buflist
, head
); hdr
;
6443 hdr
= list_prev(&dev
->l2ad_buflist
, hdr
)) {
6447 * We rely on the L1 portion of the header below, so
6448 * it's invalid for this header to have been evicted out
6449 * of the ghost cache, prior to being written out. The
6450 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
6452 ASSERT(HDR_HAS_L1HDR(hdr
));
6455 * We shouldn't need to lock the buffer here, since we flagged
6456 * it as ARC_FLAG_L2_WRITING in the previous step, but we must
6457 * take care to only access its L2 cache parameters. In
6458 * particular, hdr->l1hdr.b_buf may be invalid by now due to
6461 hdr
->b_l2hdr
.b_daddr
= dev
->l2ad_hand
;
6463 if ((!l2arc_nocompress
&& HDR_L2COMPRESS(hdr
)) &&
6464 hdr
->b_l2hdr
.b_asize
>= buf_compress_minsz
) {
6465 if (l2arc_compress_buf(hdr
)) {
6467 * If compression succeeded, enable headroom
6468 * boost on the next scan cycle.
6470 *headroom_boost
= B_TRUE
;
6475 * Pick up the buffer data we had previously stashed away
6476 * (and now potentially also compressed).
6478 buf_data
= hdr
->b_l1hdr
.b_tmp_cdata
;
6479 buf_sz
= hdr
->b_l2hdr
.b_asize
;
6482 * We need to do this regardless if buf_sz is zero or
6483 * not, otherwise, when this l2hdr is evicted we'll
6484 * remove a reference that was never added.
6486 (void) refcount_add_many(&dev
->l2ad_alloc
, buf_sz
, hdr
);
6488 /* Compression may have squashed the buffer to zero length. */
6492 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
6493 dev
->l2ad_hand
, buf_sz
, buf_data
, ZIO_CHECKSUM_OFF
,
6494 NULL
, NULL
, ZIO_PRIORITY_ASYNC_WRITE
,
6495 ZIO_FLAG_CANFAIL
, B_FALSE
);
6497 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
6499 (void) zio_nowait(wzio
);
6501 stats_size
+= buf_sz
;
6504 * Keep the clock hand suitably device-aligned.
6506 buf_a_sz
= vdev_psize_to_asize(dev
->l2ad_vdev
, buf_sz
);
6507 write_asize
+= buf_a_sz
;
6508 dev
->l2ad_hand
+= buf_a_sz
;
6512 mutex_exit(&dev
->l2ad_mtx
);
6514 ASSERT3U(write_asize
, <=, target_sz
);
6515 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
6516 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_asize
);
6517 ARCSTAT_INCR(arcstat_l2_size
, write_sz
);
6518 ARCSTAT_INCR(arcstat_l2_asize
, stats_size
);
6519 vdev_space_update(dev
->l2ad_vdev
, stats_size
, 0, 0);
6522 * Bump device hand to the device start if it is approaching the end.
6523 * l2arc_evict() will already have evicted ahead for this case.
6525 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
6526 dev
->l2ad_hand
= dev
->l2ad_start
;
6527 dev
->l2ad_first
= B_FALSE
;
6530 dev
->l2ad_writing
= B_TRUE
;
6531 (void) zio_wait(pio
);
6532 dev
->l2ad_writing
= B_FALSE
;
6534 return (write_asize
);
6538 * Compresses an L2ARC buffer.
6539 * The data to be compressed must be prefilled in l1hdr.b_tmp_cdata and its
6540 * size in l2hdr->b_asize. This routine tries to compress the data and
6541 * depending on the compression result there are three possible outcomes:
6542 * *) The buffer was incompressible. The original l2hdr contents were left
6543 * untouched and are ready for writing to an L2 device.
6544 * *) The buffer was all-zeros, so there is no need to write it to an L2
6545 * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is
6546 * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY.
6547 * *) Compression succeeded and b_tmp_cdata was replaced with a temporary
6548 * data buffer which holds the compressed data to be written, and b_asize
6549 * tells us how much data there is. b_compress is set to the appropriate
6550 * compression algorithm. Once writing is done, invoke
6551 * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer.
6553 * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the
6554 * buffer was incompressible).
6557 l2arc_compress_buf(arc_buf_hdr_t
*hdr
)
6560 size_t csize
, len
, rounded
;
6561 l2arc_buf_hdr_t
*l2hdr
;
6563 ASSERT(HDR_HAS_L2HDR(hdr
));
6565 l2hdr
= &hdr
->b_l2hdr
;
6567 ASSERT(HDR_HAS_L1HDR(hdr
));
6568 ASSERT(HDR_GET_COMPRESS(hdr
) == ZIO_COMPRESS_OFF
);
6569 ASSERT(hdr
->b_l1hdr
.b_tmp_cdata
!= NULL
);
6571 len
= l2hdr
->b_asize
;
6572 cdata
= zio_data_buf_alloc(len
);
6573 ASSERT3P(cdata
, !=, NULL
);
6574 csize
= zio_compress_data(ZIO_COMPRESS_LZ4
, hdr
->b_l1hdr
.b_tmp_cdata
,
6575 cdata
, l2hdr
->b_asize
);
6577 rounded
= P2ROUNDUP(csize
, (size_t)SPA_MINBLOCKSIZE
);
6578 if (rounded
> csize
) {
6579 bzero((char *)cdata
+ csize
, rounded
- csize
);
6584 /* zero block, indicate that there's nothing to write */
6585 zio_data_buf_free(cdata
, len
);
6586 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_EMPTY
);
6588 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
6589 ARCSTAT_BUMP(arcstat_l2_compress_zeros
);
6591 } else if (csize
> 0 && csize
< len
) {
6593 * Compression succeeded, we'll keep the cdata around for
6594 * writing and release it afterwards.
6596 HDR_SET_COMPRESS(hdr
, ZIO_COMPRESS_LZ4
);
6597 l2hdr
->b_asize
= csize
;
6598 hdr
->b_l1hdr
.b_tmp_cdata
= cdata
;
6599 ARCSTAT_BUMP(arcstat_l2_compress_successes
);
6603 * Compression failed, release the compressed buffer.
6604 * l2hdr will be left unmodified.
6606 zio_data_buf_free(cdata
, len
);
6607 ARCSTAT_BUMP(arcstat_l2_compress_failures
);
6613 * Decompresses a zio read back from an l2arc device. On success, the
6614 * underlying zio's io_data buffer is overwritten by the uncompressed
6615 * version. On decompression error (corrupt compressed stream), the
6616 * zio->io_error value is set to signal an I/O error.
6618 * Please note that the compressed data stream is not checksummed, so
6619 * if the underlying device is experiencing data corruption, we may feed
6620 * corrupt data to the decompressor, so the decompressor needs to be
6621 * able to handle this situation (LZ4 does).
6624 l2arc_decompress_zio(zio_t
*zio
, arc_buf_hdr_t
*hdr
, enum zio_compress c
)
6629 ASSERT(L2ARC_IS_VALID_COMPRESS(c
));
6631 if (zio
->io_error
!= 0) {
6633 * An io error has occured, just restore the original io
6634 * size in preparation for a main pool read.
6636 zio
->io_orig_size
= zio
->io_size
= hdr
->b_size
;
6640 if (c
== ZIO_COMPRESS_EMPTY
) {
6642 * An empty buffer results in a null zio, which means we
6643 * need to fill its io_data after we're done restoring the
6644 * buffer's contents.
6646 ASSERT(hdr
->b_l1hdr
.b_buf
!= NULL
);
6647 bzero(hdr
->b_l1hdr
.b_buf
->b_data
, hdr
->b_size
);
6648 zio
->io_data
= zio
->io_orig_data
= hdr
->b_l1hdr
.b_buf
->b_data
;
6650 ASSERT(zio
->io_data
!= NULL
);
6652 * We copy the compressed data from the start of the arc buffer
6653 * (the zio_read will have pulled in only what we need, the
6654 * rest is garbage which we will overwrite at decompression)
6655 * and then decompress back to the ARC data buffer. This way we
6656 * can minimize copying by simply decompressing back over the
6657 * original compressed data (rather than decompressing to an
6658 * aux buffer and then copying back the uncompressed buffer,
6659 * which is likely to be much larger).
6661 csize
= zio
->io_size
;
6662 cdata
= zio_data_buf_alloc(csize
);
6663 bcopy(zio
->io_data
, cdata
, csize
);
6664 if (zio_decompress_data(c
, cdata
, zio
->io_data
, csize
,
6666 zio
->io_error
= SET_ERROR(EIO
);
6667 zio_data_buf_free(cdata
, csize
);
6670 /* Restore the expected uncompressed IO size. */
6671 zio
->io_orig_size
= zio
->io_size
= hdr
->b_size
;
6675 * Releases the temporary b_tmp_cdata buffer in an l2arc header structure.
6676 * This buffer serves as a temporary holder of compressed data while
6677 * the buffer entry is being written to an l2arc device. Once that is
6678 * done, we can dispose of it.
6681 l2arc_release_cdata_buf(arc_buf_hdr_t
*hdr
)
6683 enum zio_compress comp
= HDR_GET_COMPRESS(hdr
);
6685 ASSERT(HDR_HAS_L1HDR(hdr
));
6686 ASSERT(comp
== ZIO_COMPRESS_OFF
|| L2ARC_IS_VALID_COMPRESS(comp
));
6688 if (comp
== ZIO_COMPRESS_OFF
) {
6690 * In this case, b_tmp_cdata points to the same buffer
6691 * as the arc_buf_t's b_data field. We don't want to
6692 * free it, since the arc_buf_t will handle that.
6694 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
6695 } else if (comp
== ZIO_COMPRESS_EMPTY
) {
6697 * In this case, b_tmp_cdata was compressed to an empty
6698 * buffer, thus there's nothing to free and b_tmp_cdata
6699 * should have been set to NULL in l2arc_write_buffers().
6701 ASSERT3P(hdr
->b_l1hdr
.b_tmp_cdata
, ==, NULL
);
6704 * If the data was compressed, then we've allocated a
6705 * temporary buffer for it, so now we need to release it.
6707 ASSERT(hdr
->b_l1hdr
.b_tmp_cdata
!= NULL
);
6708 zio_data_buf_free(hdr
->b_l1hdr
.b_tmp_cdata
,
6710 hdr
->b_l1hdr
.b_tmp_cdata
= NULL
;
6716 * This thread feeds the L2ARC at regular intervals. This is the beating
6717 * heart of the L2ARC.
6720 l2arc_feed_thread(void)
6725 uint64_t size
, wrote
;
6726 clock_t begin
, next
= ddi_get_lbolt();
6727 boolean_t headroom_boost
= B_FALSE
;
6728 fstrans_cookie_t cookie
;
6730 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
6732 mutex_enter(&l2arc_feed_thr_lock
);
6734 cookie
= spl_fstrans_mark();
6735 while (l2arc_thread_exit
== 0) {
6736 CALLB_CPR_SAFE_BEGIN(&cpr
);
6737 (void) cv_timedwait_sig(&l2arc_feed_thr_cv
,
6738 &l2arc_feed_thr_lock
, next
);
6739 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
6740 next
= ddi_get_lbolt() + hz
;
6743 * Quick check for L2ARC devices.
6745 mutex_enter(&l2arc_dev_mtx
);
6746 if (l2arc_ndev
== 0) {
6747 mutex_exit(&l2arc_dev_mtx
);
6750 mutex_exit(&l2arc_dev_mtx
);
6751 begin
= ddi_get_lbolt();
6754 * This selects the next l2arc device to write to, and in
6755 * doing so the next spa to feed from: dev->l2ad_spa. This
6756 * will return NULL if there are now no l2arc devices or if
6757 * they are all faulted.
6759 * If a device is returned, its spa's config lock is also
6760 * held to prevent device removal. l2arc_dev_get_next()
6761 * will grab and release l2arc_dev_mtx.
6763 if ((dev
= l2arc_dev_get_next()) == NULL
)
6766 spa
= dev
->l2ad_spa
;
6767 ASSERT(spa
!= NULL
);
6770 * If the pool is read-only then force the feed thread to
6771 * sleep a little longer.
6773 if (!spa_writeable(spa
)) {
6774 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
6775 spa_config_exit(spa
, SCL_L2ARC
, dev
);
6780 * Avoid contributing to memory pressure.
6782 if (arc_reclaim_needed()) {
6783 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
6784 spa_config_exit(spa
, SCL_L2ARC
, dev
);
6788 ARCSTAT_BUMP(arcstat_l2_feeds
);
6790 size
= l2arc_write_size();
6793 * Evict L2ARC buffers that will be overwritten.
6795 l2arc_evict(dev
, size
, B_FALSE
);
6798 * Write ARC buffers.
6800 wrote
= l2arc_write_buffers(spa
, dev
, size
, &headroom_boost
);
6803 * Calculate interval between writes.
6805 next
= l2arc_write_interval(begin
, size
, wrote
);
6806 spa_config_exit(spa
, SCL_L2ARC
, dev
);
6808 spl_fstrans_unmark(cookie
);
6810 l2arc_thread_exit
= 0;
6811 cv_broadcast(&l2arc_feed_thr_cv
);
6812 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
6817 l2arc_vdev_present(vdev_t
*vd
)
6821 mutex_enter(&l2arc_dev_mtx
);
6822 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
6823 dev
= list_next(l2arc_dev_list
, dev
)) {
6824 if (dev
->l2ad_vdev
== vd
)
6827 mutex_exit(&l2arc_dev_mtx
);
6829 return (dev
!= NULL
);
6833 * Add a vdev for use by the L2ARC. By this point the spa has already
6834 * validated the vdev and opened it.
6837 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
6839 l2arc_dev_t
*adddev
;
6841 ASSERT(!l2arc_vdev_present(vd
));
6844 * Create a new l2arc device entry.
6846 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
6847 adddev
->l2ad_spa
= spa
;
6848 adddev
->l2ad_vdev
= vd
;
6849 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
6850 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
6851 adddev
->l2ad_hand
= adddev
->l2ad_start
;
6852 adddev
->l2ad_first
= B_TRUE
;
6853 adddev
->l2ad_writing
= B_FALSE
;
6854 list_link_init(&adddev
->l2ad_node
);
6856 mutex_init(&adddev
->l2ad_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
6858 * This is a list of all ARC buffers that are still valid on the
6861 list_create(&adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
6862 offsetof(arc_buf_hdr_t
, b_l2hdr
.b_l2node
));
6864 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
6865 refcount_create(&adddev
->l2ad_alloc
);
6868 * Add device to global list
6870 mutex_enter(&l2arc_dev_mtx
);
6871 list_insert_head(l2arc_dev_list
, adddev
);
6872 atomic_inc_64(&l2arc_ndev
);
6873 mutex_exit(&l2arc_dev_mtx
);
6877 * Remove a vdev from the L2ARC.
6880 l2arc_remove_vdev(vdev_t
*vd
)
6882 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
6885 * Find the device by vdev
6887 mutex_enter(&l2arc_dev_mtx
);
6888 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
6889 nextdev
= list_next(l2arc_dev_list
, dev
);
6890 if (vd
== dev
->l2ad_vdev
) {
6895 ASSERT(remdev
!= NULL
);
6898 * Remove device from global list
6900 list_remove(l2arc_dev_list
, remdev
);
6901 l2arc_dev_last
= NULL
; /* may have been invalidated */
6902 atomic_dec_64(&l2arc_ndev
);
6903 mutex_exit(&l2arc_dev_mtx
);
6906 * Clear all buflists and ARC references. L2ARC device flush.
6908 l2arc_evict(remdev
, 0, B_TRUE
);
6909 list_destroy(&remdev
->l2ad_buflist
);
6910 mutex_destroy(&remdev
->l2ad_mtx
);
6911 refcount_destroy(&remdev
->l2ad_alloc
);
6912 kmem_free(remdev
, sizeof (l2arc_dev_t
));
6918 l2arc_thread_exit
= 0;
6920 l2arc_writes_sent
= 0;
6921 l2arc_writes_done
= 0;
6923 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
6924 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
6925 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
6926 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
6928 l2arc_dev_list
= &L2ARC_dev_list
;
6929 l2arc_free_on_write
= &L2ARC_free_on_write
;
6930 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
6931 offsetof(l2arc_dev_t
, l2ad_node
));
6932 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
6933 offsetof(l2arc_data_free_t
, l2df_list_node
));
6940 * This is called from dmu_fini(), which is called from spa_fini();
6941 * Because of this, we can assume that all l2arc devices have
6942 * already been removed when the pools themselves were removed.
6945 l2arc_do_free_on_write();
6947 mutex_destroy(&l2arc_feed_thr_lock
);
6948 cv_destroy(&l2arc_feed_thr_cv
);
6949 mutex_destroy(&l2arc_dev_mtx
);
6950 mutex_destroy(&l2arc_free_on_write_mtx
);
6952 list_destroy(l2arc_dev_list
);
6953 list_destroy(l2arc_free_on_write
);
6959 if (!(spa_mode_global
& FWRITE
))
6962 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
6963 TS_RUN
, minclsyspri
);
6969 if (!(spa_mode_global
& FWRITE
))
6972 mutex_enter(&l2arc_feed_thr_lock
);
6973 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
6974 l2arc_thread_exit
= 1;
6975 while (l2arc_thread_exit
!= 0)
6976 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
6977 mutex_exit(&l2arc_feed_thr_lock
);
6980 #if defined(_KERNEL) && defined(HAVE_SPL)
6981 EXPORT_SYMBOL(arc_buf_size
);
6982 EXPORT_SYMBOL(arc_write
);
6983 EXPORT_SYMBOL(arc_read
);
6984 EXPORT_SYMBOL(arc_buf_remove_ref
);
6985 EXPORT_SYMBOL(arc_buf_info
);
6986 EXPORT_SYMBOL(arc_getbuf_func
);
6987 EXPORT_SYMBOL(arc_add_prune_callback
);
6988 EXPORT_SYMBOL(arc_remove_prune_callback
);
6990 module_param(zfs_arc_min
, ulong
, 0644);
6991 MODULE_PARM_DESC(zfs_arc_min
, "Min arc size");
6993 module_param(zfs_arc_max
, ulong
, 0644);
6994 MODULE_PARM_DESC(zfs_arc_max
, "Max arc size");
6996 module_param(zfs_arc_meta_limit
, ulong
, 0644);
6997 MODULE_PARM_DESC(zfs_arc_meta_limit
, "Meta limit for arc size");
6999 module_param(zfs_arc_meta_min
, ulong
, 0644);
7000 MODULE_PARM_DESC(zfs_arc_meta_min
, "Min arc metadata");
7002 module_param(zfs_arc_meta_prune
, int, 0644);
7003 MODULE_PARM_DESC(zfs_arc_meta_prune
, "Meta objects to scan for prune");
7005 module_param(zfs_arc_meta_adjust_restarts
, int, 0644);
7006 MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts
,
7007 "Limit number of restarts in arc_adjust_meta");
7009 module_param(zfs_arc_meta_strategy
, int, 0644);
7010 MODULE_PARM_DESC(zfs_arc_meta_strategy
, "Meta reclaim strategy");
7012 module_param(zfs_arc_grow_retry
, int, 0644);
7013 MODULE_PARM_DESC(zfs_arc_grow_retry
, "Seconds before growing arc size");
7015 module_param(zfs_arc_p_aggressive_disable
, int, 0644);
7016 MODULE_PARM_DESC(zfs_arc_p_aggressive_disable
, "disable aggressive arc_p grow");
7018 module_param(zfs_arc_p_dampener_disable
, int, 0644);
7019 MODULE_PARM_DESC(zfs_arc_p_dampener_disable
, "disable arc_p adapt dampener");
7021 module_param(zfs_arc_shrink_shift
, int, 0644);
7022 MODULE_PARM_DESC(zfs_arc_shrink_shift
, "log2(fraction of arc to reclaim)");
7024 module_param(zfs_arc_p_min_shift
, int, 0644);
7025 MODULE_PARM_DESC(zfs_arc_p_min_shift
, "arc_c shift to calc min/max arc_p");
7027 module_param(zfs_disable_dup_eviction
, int, 0644);
7028 MODULE_PARM_DESC(zfs_disable_dup_eviction
, "disable duplicate buffer eviction");
7030 module_param(zfs_arc_average_blocksize
, int, 0444);
7031 MODULE_PARM_DESC(zfs_arc_average_blocksize
, "Target average block size");
7033 module_param(zfs_arc_memory_throttle_disable
, int, 0644);
7034 MODULE_PARM_DESC(zfs_arc_memory_throttle_disable
, "disable memory throttle");
7036 module_param(zfs_arc_min_prefetch_lifespan
, int, 0644);
7037 MODULE_PARM_DESC(zfs_arc_min_prefetch_lifespan
, "Min life of prefetch block");
7039 module_param(zfs_arc_num_sublists_per_state
, int, 0644);
7040 MODULE_PARM_DESC(zfs_arc_num_sublists_per_state
,
7041 "Number of sublists used in each of the ARC state lists");
7043 module_param(l2arc_write_max
, ulong
, 0644);
7044 MODULE_PARM_DESC(l2arc_write_max
, "Max write bytes per interval");
7046 module_param(l2arc_write_boost
, ulong
, 0644);
7047 MODULE_PARM_DESC(l2arc_write_boost
, "Extra write bytes during device warmup");
7049 module_param(l2arc_headroom
, ulong
, 0644);
7050 MODULE_PARM_DESC(l2arc_headroom
, "Number of max device writes to precache");
7052 module_param(l2arc_headroom_boost
, ulong
, 0644);
7053 MODULE_PARM_DESC(l2arc_headroom_boost
, "Compressed l2arc_headroom multiplier");
7055 module_param(l2arc_feed_secs
, ulong
, 0644);
7056 MODULE_PARM_DESC(l2arc_feed_secs
, "Seconds between L2ARC writing");
7058 module_param(l2arc_feed_min_ms
, ulong
, 0644);
7059 MODULE_PARM_DESC(l2arc_feed_min_ms
, "Min feed interval in milliseconds");
7061 module_param(l2arc_noprefetch
, int, 0644);
7062 MODULE_PARM_DESC(l2arc_noprefetch
, "Skip caching prefetched buffers");
7064 module_param(l2arc_nocompress
, int, 0644);
7065 MODULE_PARM_DESC(l2arc_nocompress
, "Skip compressing L2ARC buffers");
7067 module_param(l2arc_feed_again
, int, 0644);
7068 MODULE_PARM_DESC(l2arc_feed_again
, "Turbo L2ARC warmup");
7070 module_param(l2arc_norw
, int, 0644);
7071 MODULE_PARM_DESC(l2arc_norw
, "No reads during writes");