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 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29 * DVA-based Adjustable Replacement Cache
31 * While much of the theory of operation used here is
32 * based on the self-tuning, low overhead replacement cache
33 * presented by Megiddo and Modha at FAST 2003, there are some
34 * significant differences:
36 * 1. The Megiddo and Modha model assumes any page is evictable.
37 * Pages in its cache cannot be "locked" into memory. This makes
38 * the eviction algorithm simple: evict the last page in the list.
39 * This also make the performance characteristics easy to reason
40 * about. Our cache is not so simple. At any given moment, some
41 * subset of the blocks in the cache are un-evictable because we
42 * have handed out a reference to them. Blocks are only evictable
43 * when there are no external references active. This makes
44 * eviction far more problematic: we choose to evict the evictable
45 * blocks that are the "lowest" in the list.
47 * There are times when it is not possible to evict the requested
48 * space. In these circumstances we are unable to adjust the cache
49 * size. To prevent the cache growing unbounded at these times we
50 * implement a "cache throttle" that slows the flow of new data
51 * into the cache until we can make space available.
53 * 2. The Megiddo and Modha model assumes a fixed cache size.
54 * Pages are evicted when the cache is full and there is a cache
55 * miss. Our model has a variable sized cache. It grows with
56 * high use, but also tries to react to memory pressure from the
57 * operating system: decreasing its size when system memory is
60 * 3. The Megiddo and Modha model assumes a fixed page size. All
61 * elements of the cache are therefore exactly the same size. So
62 * when adjusting the cache size following a cache miss, its simply
63 * a matter of choosing a single page to evict. In our model, we
64 * have variable sized cache blocks (rangeing from 512 bytes to
65 * 128K bytes). We therefore choose a set of blocks to evict to make
66 * space for a cache miss that approximates as closely as possible
67 * the space used by the new block.
69 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
70 * by N. Megiddo & D. Modha, FAST 2003
76 * A new reference to a cache buffer can be obtained in two
77 * ways: 1) via a hash table lookup using the DVA as a key,
78 * or 2) via one of the ARC lists. The arc_read() interface
79 * uses method 1, while the internal arc algorithms for
80 * adjusting the cache use method 2. We therefore provide two
81 * types of locks: 1) the hash table lock array, and 2) the
84 * Buffers do not have their own mutexes, rather they rely on the
85 * hash table mutexes for the bulk of their protection (i.e. most
86 * fields in the arc_buf_hdr_t are protected by these mutexes).
88 * buf_hash_find() returns the appropriate mutex (held) when it
89 * locates the requested buffer in the hash table. It returns
90 * NULL for the mutex if the buffer was not in the table.
92 * buf_hash_remove() expects the appropriate hash mutex to be
93 * already held before it is invoked.
95 * Each arc state also has a mutex which is used to protect the
96 * buffer list associated with the state. When attempting to
97 * obtain a hash table lock while holding an arc list lock you
98 * must use: mutex_tryenter() to avoid deadlock. Also note that
99 * the active state mutex must be held before the ghost state mutex.
101 * Arc buffers may have an associated eviction callback function.
102 * This function will be invoked prior to removing the buffer (e.g.
103 * in arc_do_user_evicts()). Note however that the data associated
104 * with the buffer may be evicted prior to the callback. The callback
105 * must be made with *no locks held* (to prevent deadlock). Additionally,
106 * the users of callbacks must ensure that their private data is
107 * protected from simultaneous callbacks from arc_buf_evict()
108 * and arc_do_user_evicts().
110 * It as also possible to register a callback which is run when the
111 * arc_meta_limit is reached and no buffers can be safely evicted. In
112 * this case the arc user should drop a reference on some arc buffers so
113 * they can be reclaimed and the arc_meta_limit honored. For example,
114 * when using the ZPL each dentry holds a references on a znode. These
115 * dentries must be pruned before the arc buffer holding the znode can
118 * Note that the majority of the performance stats are manipulated
119 * with atomic operations.
121 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
123 * - L2ARC buflist creation
124 * - L2ARC buflist eviction
125 * - L2ARC write completion, which walks L2ARC buflists
126 * - ARC header destruction, as it removes from L2ARC buflists
127 * - ARC header release, as it removes from L2ARC buflists
132 #include <sys/zio_compress.h>
133 #include <sys/zfs_context.h>
135 #include <sys/vdev.h>
136 #include <sys/vdev_impl.h>
137 #include <sys/dsl_pool.h>
139 #include <sys/vmsystm.h>
141 #include <sys/fs/swapnode.h>
144 #include <sys/callb.h>
145 #include <sys/kstat.h>
146 #include <sys/dmu_tx.h>
147 #include <zfs_fletcher.h>
150 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
151 boolean_t arc_watch
= B_FALSE
;
154 static kmutex_t arc_reclaim_thr_lock
;
155 static kcondvar_t arc_reclaim_thr_cv
; /* used to signal reclaim thr */
156 static uint8_t arc_thread_exit
;
158 /* number of bytes to prune from caches when at arc_meta_limit is reached */
159 int zfs_arc_meta_prune
= 1048576;
161 typedef enum arc_reclaim_strategy
{
162 ARC_RECLAIM_AGGR
, /* Aggressive reclaim strategy */
163 ARC_RECLAIM_CONS
/* Conservative reclaim strategy */
164 } arc_reclaim_strategy_t
;
167 * The number of iterations through arc_evict_*() before we
168 * drop & reacquire the lock.
170 int arc_evict_iterations
= 100;
172 /* number of seconds before growing cache again */
173 int zfs_arc_grow_retry
= 5;
175 /* shift of arc_c for calculating both min and max arc_p */
176 int zfs_arc_p_min_shift
= 4;
178 /* disable anon data aggressively growing arc_p */
179 int zfs_arc_p_aggressive_disable
= 1;
181 /* log2(fraction of arc to reclaim) */
182 int zfs_arc_shrink_shift
= 5;
185 * minimum lifespan of a prefetch block in clock ticks
186 * (initialized in arc_init())
188 int zfs_arc_min_prefetch_lifespan
= HZ
;
190 /* disable arc proactive arc throttle due to low memory */
191 int zfs_arc_memory_throttle_disable
= 1;
193 /* disable duplicate buffer eviction */
194 int zfs_disable_dup_eviction
= 0;
197 * If this percent of memory is free, don't throttle.
199 int arc_lotsfree_percent
= 10;
203 /* expiration time for arc_no_grow */
204 static clock_t arc_grow_time
= 0;
207 * The arc has filled available memory and has now warmed up.
209 static boolean_t arc_warm
;
212 * These tunables are for performance analysis.
214 unsigned long zfs_arc_max
= 0;
215 unsigned long zfs_arc_min
= 0;
216 unsigned long zfs_arc_meta_limit
= 0;
219 * Note that buffers can be in one of 6 states:
220 * ARC_anon - anonymous (discussed below)
221 * ARC_mru - recently used, currently cached
222 * ARC_mru_ghost - recentely used, no longer in cache
223 * ARC_mfu - frequently used, currently cached
224 * ARC_mfu_ghost - frequently used, no longer in cache
225 * ARC_l2c_only - exists in L2ARC but not other states
226 * When there are no active references to the buffer, they are
227 * are linked onto a list in one of these arc states. These are
228 * the only buffers that can be evicted or deleted. Within each
229 * state there are multiple lists, one for meta-data and one for
230 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
231 * etc.) is tracked separately so that it can be managed more
232 * explicitly: favored over data, limited explicitly.
234 * Anonymous buffers are buffers that are not associated with
235 * a DVA. These are buffers that hold dirty block copies
236 * before they are written to stable storage. By definition,
237 * they are "ref'd" and are considered part of arc_mru
238 * that cannot be freed. Generally, they will aquire a DVA
239 * as they are written and migrate onto the arc_mru list.
241 * The ARC_l2c_only state is for buffers that are in the second
242 * level ARC but no longer in any of the ARC_m* lists. The second
243 * level ARC itself may also contain buffers that are in any of
244 * the ARC_m* states - meaning that a buffer can exist in two
245 * places. The reason for the ARC_l2c_only state is to keep the
246 * buffer header in the hash table, so that reads that hit the
247 * second level ARC benefit from these fast lookups.
250 typedef struct arc_state
{
251 list_t arcs_list
[ARC_BUFC_NUMTYPES
]; /* list of evictable buffers */
252 uint64_t arcs_lsize
[ARC_BUFC_NUMTYPES
]; /* amount of evictable data */
253 uint64_t arcs_size
; /* total amount of data in this state */
255 arc_state_type_t arcs_state
;
259 static arc_state_t ARC_anon
;
260 static arc_state_t ARC_mru
;
261 static arc_state_t ARC_mru_ghost
;
262 static arc_state_t ARC_mfu
;
263 static arc_state_t ARC_mfu_ghost
;
264 static arc_state_t ARC_l2c_only
;
266 typedef struct arc_stats
{
267 kstat_named_t arcstat_hits
;
268 kstat_named_t arcstat_misses
;
269 kstat_named_t arcstat_demand_data_hits
;
270 kstat_named_t arcstat_demand_data_misses
;
271 kstat_named_t arcstat_demand_metadata_hits
;
272 kstat_named_t arcstat_demand_metadata_misses
;
273 kstat_named_t arcstat_prefetch_data_hits
;
274 kstat_named_t arcstat_prefetch_data_misses
;
275 kstat_named_t arcstat_prefetch_metadata_hits
;
276 kstat_named_t arcstat_prefetch_metadata_misses
;
277 kstat_named_t arcstat_mru_hits
;
278 kstat_named_t arcstat_mru_ghost_hits
;
279 kstat_named_t arcstat_mfu_hits
;
280 kstat_named_t arcstat_mfu_ghost_hits
;
281 kstat_named_t arcstat_deleted
;
282 kstat_named_t arcstat_recycle_miss
;
284 * Number of buffers that could not be evicted because the hash lock
285 * was held by another thread. The lock may not necessarily be held
286 * by something using the same buffer, since hash locks are shared
287 * by multiple buffers.
289 kstat_named_t arcstat_mutex_miss
;
291 * Number of buffers skipped because they have I/O in progress, are
292 * indrect prefetch buffers that have not lived long enough, or are
293 * not from the spa we're trying to evict from.
295 kstat_named_t arcstat_evict_skip
;
296 kstat_named_t arcstat_evict_l2_cached
;
297 kstat_named_t arcstat_evict_l2_eligible
;
298 kstat_named_t arcstat_evict_l2_ineligible
;
299 kstat_named_t arcstat_hash_elements
;
300 kstat_named_t arcstat_hash_elements_max
;
301 kstat_named_t arcstat_hash_collisions
;
302 kstat_named_t arcstat_hash_chains
;
303 kstat_named_t arcstat_hash_chain_max
;
304 kstat_named_t arcstat_p
;
305 kstat_named_t arcstat_c
;
306 kstat_named_t arcstat_c_min
;
307 kstat_named_t arcstat_c_max
;
308 kstat_named_t arcstat_size
;
309 kstat_named_t arcstat_hdr_size
;
310 kstat_named_t arcstat_data_size
;
311 kstat_named_t arcstat_other_size
;
312 kstat_named_t arcstat_anon_size
;
313 kstat_named_t arcstat_anon_evict_data
;
314 kstat_named_t arcstat_anon_evict_metadata
;
315 kstat_named_t arcstat_mru_size
;
316 kstat_named_t arcstat_mru_evict_data
;
317 kstat_named_t arcstat_mru_evict_metadata
;
318 kstat_named_t arcstat_mru_ghost_size
;
319 kstat_named_t arcstat_mru_ghost_evict_data
;
320 kstat_named_t arcstat_mru_ghost_evict_metadata
;
321 kstat_named_t arcstat_mfu_size
;
322 kstat_named_t arcstat_mfu_evict_data
;
323 kstat_named_t arcstat_mfu_evict_metadata
;
324 kstat_named_t arcstat_mfu_ghost_size
;
325 kstat_named_t arcstat_mfu_ghost_evict_data
;
326 kstat_named_t arcstat_mfu_ghost_evict_metadata
;
327 kstat_named_t arcstat_l2_hits
;
328 kstat_named_t arcstat_l2_misses
;
329 kstat_named_t arcstat_l2_feeds
;
330 kstat_named_t arcstat_l2_rw_clash
;
331 kstat_named_t arcstat_l2_read_bytes
;
332 kstat_named_t arcstat_l2_write_bytes
;
333 kstat_named_t arcstat_l2_writes_sent
;
334 kstat_named_t arcstat_l2_writes_done
;
335 kstat_named_t arcstat_l2_writes_error
;
336 kstat_named_t arcstat_l2_writes_hdr_miss
;
337 kstat_named_t arcstat_l2_evict_lock_retry
;
338 kstat_named_t arcstat_l2_evict_reading
;
339 kstat_named_t arcstat_l2_free_on_write
;
340 kstat_named_t arcstat_l2_abort_lowmem
;
341 kstat_named_t arcstat_l2_cksum_bad
;
342 kstat_named_t arcstat_l2_io_error
;
343 kstat_named_t arcstat_l2_size
;
344 kstat_named_t arcstat_l2_asize
;
345 kstat_named_t arcstat_l2_hdr_size
;
346 kstat_named_t arcstat_l2_compress_successes
;
347 kstat_named_t arcstat_l2_compress_zeros
;
348 kstat_named_t arcstat_l2_compress_failures
;
349 kstat_named_t arcstat_memory_throttle_count
;
350 kstat_named_t arcstat_duplicate_buffers
;
351 kstat_named_t arcstat_duplicate_buffers_size
;
352 kstat_named_t arcstat_duplicate_reads
;
353 kstat_named_t arcstat_memory_direct_count
;
354 kstat_named_t arcstat_memory_indirect_count
;
355 kstat_named_t arcstat_no_grow
;
356 kstat_named_t arcstat_tempreserve
;
357 kstat_named_t arcstat_loaned_bytes
;
358 kstat_named_t arcstat_prune
;
359 kstat_named_t arcstat_meta_used
;
360 kstat_named_t arcstat_meta_limit
;
361 kstat_named_t arcstat_meta_max
;
364 static arc_stats_t arc_stats
= {
365 { "hits", KSTAT_DATA_UINT64
},
366 { "misses", KSTAT_DATA_UINT64
},
367 { "demand_data_hits", KSTAT_DATA_UINT64
},
368 { "demand_data_misses", KSTAT_DATA_UINT64
},
369 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
370 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
371 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
372 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
373 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
374 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
375 { "mru_hits", KSTAT_DATA_UINT64
},
376 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
377 { "mfu_hits", KSTAT_DATA_UINT64
},
378 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
379 { "deleted", KSTAT_DATA_UINT64
},
380 { "recycle_miss", KSTAT_DATA_UINT64
},
381 { "mutex_miss", KSTAT_DATA_UINT64
},
382 { "evict_skip", KSTAT_DATA_UINT64
},
383 { "evict_l2_cached", KSTAT_DATA_UINT64
},
384 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
385 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
386 { "hash_elements", KSTAT_DATA_UINT64
},
387 { "hash_elements_max", KSTAT_DATA_UINT64
},
388 { "hash_collisions", KSTAT_DATA_UINT64
},
389 { "hash_chains", KSTAT_DATA_UINT64
},
390 { "hash_chain_max", KSTAT_DATA_UINT64
},
391 { "p", KSTAT_DATA_UINT64
},
392 { "c", KSTAT_DATA_UINT64
},
393 { "c_min", KSTAT_DATA_UINT64
},
394 { "c_max", KSTAT_DATA_UINT64
},
395 { "size", KSTAT_DATA_UINT64
},
396 { "hdr_size", KSTAT_DATA_UINT64
},
397 { "data_size", KSTAT_DATA_UINT64
},
398 { "other_size", KSTAT_DATA_UINT64
},
399 { "anon_size", KSTAT_DATA_UINT64
},
400 { "anon_evict_data", KSTAT_DATA_UINT64
},
401 { "anon_evict_metadata", KSTAT_DATA_UINT64
},
402 { "mru_size", KSTAT_DATA_UINT64
},
403 { "mru_evict_data", KSTAT_DATA_UINT64
},
404 { "mru_evict_metadata", KSTAT_DATA_UINT64
},
405 { "mru_ghost_size", KSTAT_DATA_UINT64
},
406 { "mru_ghost_evict_data", KSTAT_DATA_UINT64
},
407 { "mru_ghost_evict_metadata", KSTAT_DATA_UINT64
},
408 { "mfu_size", KSTAT_DATA_UINT64
},
409 { "mfu_evict_data", KSTAT_DATA_UINT64
},
410 { "mfu_evict_metadata", KSTAT_DATA_UINT64
},
411 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
412 { "mfu_ghost_evict_data", KSTAT_DATA_UINT64
},
413 { "mfu_ghost_evict_metadata", KSTAT_DATA_UINT64
},
414 { "l2_hits", KSTAT_DATA_UINT64
},
415 { "l2_misses", KSTAT_DATA_UINT64
},
416 { "l2_feeds", KSTAT_DATA_UINT64
},
417 { "l2_rw_clash", KSTAT_DATA_UINT64
},
418 { "l2_read_bytes", KSTAT_DATA_UINT64
},
419 { "l2_write_bytes", KSTAT_DATA_UINT64
},
420 { "l2_writes_sent", KSTAT_DATA_UINT64
},
421 { "l2_writes_done", KSTAT_DATA_UINT64
},
422 { "l2_writes_error", KSTAT_DATA_UINT64
},
423 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64
},
424 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
425 { "l2_evict_reading", KSTAT_DATA_UINT64
},
426 { "l2_free_on_write", KSTAT_DATA_UINT64
},
427 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
428 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
429 { "l2_io_error", KSTAT_DATA_UINT64
},
430 { "l2_size", KSTAT_DATA_UINT64
},
431 { "l2_asize", KSTAT_DATA_UINT64
},
432 { "l2_hdr_size", KSTAT_DATA_UINT64
},
433 { "l2_compress_successes", KSTAT_DATA_UINT64
},
434 { "l2_compress_zeros", KSTAT_DATA_UINT64
},
435 { "l2_compress_failures", KSTAT_DATA_UINT64
},
436 { "memory_throttle_count", KSTAT_DATA_UINT64
},
437 { "duplicate_buffers", KSTAT_DATA_UINT64
},
438 { "duplicate_buffers_size", KSTAT_DATA_UINT64
},
439 { "duplicate_reads", KSTAT_DATA_UINT64
},
440 { "memory_direct_count", KSTAT_DATA_UINT64
},
441 { "memory_indirect_count", KSTAT_DATA_UINT64
},
442 { "arc_no_grow", KSTAT_DATA_UINT64
},
443 { "arc_tempreserve", KSTAT_DATA_UINT64
},
444 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
445 { "arc_prune", KSTAT_DATA_UINT64
},
446 { "arc_meta_used", KSTAT_DATA_UINT64
},
447 { "arc_meta_limit", KSTAT_DATA_UINT64
},
448 { "arc_meta_max", KSTAT_DATA_UINT64
},
451 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
453 #define ARCSTAT_INCR(stat, val) \
454 atomic_add_64(&arc_stats.stat.value.ui64, (val))
456 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
457 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
459 #define ARCSTAT_MAX(stat, val) { \
461 while ((val) > (m = arc_stats.stat.value.ui64) && \
462 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
466 #define ARCSTAT_MAXSTAT(stat) \
467 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
470 * We define a macro to allow ARC hits/misses to be easily broken down by
471 * two separate conditions, giving a total of four different subtypes for
472 * each of hits and misses (so eight statistics total).
474 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
477 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
479 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
483 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
485 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
490 static arc_state_t
*arc_anon
;
491 static arc_state_t
*arc_mru
;
492 static arc_state_t
*arc_mru_ghost
;
493 static arc_state_t
*arc_mfu
;
494 static arc_state_t
*arc_mfu_ghost
;
495 static arc_state_t
*arc_l2c_only
;
498 * There are several ARC variables that are critical to export as kstats --
499 * but we don't want to have to grovel around in the kstat whenever we wish to
500 * manipulate them. For these variables, we therefore define them to be in
501 * terms of the statistic variable. This assures that we are not introducing
502 * the possibility of inconsistency by having shadow copies of the variables,
503 * while still allowing the code to be readable.
505 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
506 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
507 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
508 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
509 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
510 #define arc_no_grow ARCSTAT(arcstat_no_grow)
511 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
512 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
513 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
514 #define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */
515 #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
517 #define L2ARC_IS_VALID_COMPRESS(_c_) \
518 ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY)
520 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t
;
522 typedef struct arc_callback arc_callback_t
;
524 struct arc_callback
{
526 arc_done_func_t
*acb_done
;
528 zio_t
*acb_zio_dummy
;
529 arc_callback_t
*acb_next
;
532 typedef struct arc_write_callback arc_write_callback_t
;
534 struct arc_write_callback
{
536 arc_done_func_t
*awcb_ready
;
537 arc_done_func_t
*awcb_physdone
;
538 arc_done_func_t
*awcb_done
;
543 /* protected by hash lock */
548 kmutex_t b_freeze_lock
;
549 zio_cksum_t
*b_freeze_cksum
;
551 arc_buf_hdr_t
*b_hash_next
;
556 arc_callback_t
*b_acb
;
560 arc_buf_contents_t b_type
;
564 /* protected by arc state mutex */
565 arc_state_t
*b_state
;
566 list_node_t b_arc_node
;
568 /* updated atomically */
569 clock_t b_arc_access
;
571 uint32_t b_mru_ghost_hits
;
573 uint32_t b_mfu_ghost_hits
;
576 /* self protecting */
579 l2arc_buf_hdr_t
*b_l2hdr
;
580 list_node_t b_l2node
;
583 static list_t arc_prune_list
;
584 static kmutex_t arc_prune_mtx
;
585 static arc_buf_t
*arc_eviction_list
;
586 static kmutex_t arc_eviction_mtx
;
587 static arc_buf_hdr_t arc_eviction_hdr
;
588 static void arc_get_data_buf(arc_buf_t
*buf
);
589 static void arc_access(arc_buf_hdr_t
*buf
, kmutex_t
*hash_lock
);
590 static int arc_evict_needed(arc_buf_contents_t type
);
591 static void arc_evict_ghost(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
592 arc_buf_contents_t type
);
593 static void arc_buf_watch(arc_buf_t
*buf
);
595 static boolean_t
l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*ab
);
597 #define GHOST_STATE(state) \
598 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
599 (state) == arc_l2c_only)
602 * Private ARC flags. These flags are private ARC only flags that will show up
603 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
604 * be passed in as arc_flags in things like arc_read. However, these flags
605 * should never be passed and should only be set by ARC code. When adding new
606 * public flags, make sure not to smash the private ones.
609 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
610 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
611 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
612 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
613 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
614 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
615 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
616 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
617 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
618 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
620 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
621 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
622 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
623 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
624 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
625 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
626 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
627 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
628 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
629 (hdr)->b_l2hdr != NULL)
630 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
631 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
632 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
638 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
639 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
642 * Hash table routines
645 #define HT_LOCK_ALIGN 64
646 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
651 unsigned char pad
[HT_LOCK_PAD
];
655 #define BUF_LOCKS 256
656 typedef struct buf_hash_table
{
658 arc_buf_hdr_t
**ht_table
;
659 struct ht_lock ht_locks
[BUF_LOCKS
];
662 static buf_hash_table_t buf_hash_table
;
664 #define BUF_HASH_INDEX(spa, dva, birth) \
665 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
666 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
667 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
668 #define HDR_LOCK(hdr) \
669 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
671 uint64_t zfs_crc64_table
[256];
677 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
678 #define L2ARC_HEADROOM 2 /* num of writes */
680 * If we discover during ARC scan any buffers to be compressed, we boost
681 * our headroom for the next scanning cycle by this percentage multiple.
683 #define L2ARC_HEADROOM_BOOST 200
684 #define L2ARC_FEED_SECS 1 /* caching interval secs */
685 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
687 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
688 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
690 /* L2ARC Performance Tunables */
691 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
692 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
693 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
694 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
695 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
696 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
697 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
698 int l2arc_nocompress
= B_FALSE
; /* don't compress bufs */
699 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
700 int l2arc_norw
= B_FALSE
; /* no reads during writes */
705 typedef struct l2arc_dev
{
706 vdev_t
*l2ad_vdev
; /* vdev */
707 spa_t
*l2ad_spa
; /* spa */
708 uint64_t l2ad_hand
; /* next write location */
709 uint64_t l2ad_start
; /* first addr on device */
710 uint64_t l2ad_end
; /* last addr on device */
711 uint64_t l2ad_evict
; /* last addr eviction reached */
712 boolean_t l2ad_first
; /* first sweep through */
713 boolean_t l2ad_writing
; /* currently writing */
714 list_t
*l2ad_buflist
; /* buffer list */
715 list_node_t l2ad_node
; /* device list node */
718 static list_t L2ARC_dev_list
; /* device list */
719 static list_t
*l2arc_dev_list
; /* device list pointer */
720 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
721 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
722 static kmutex_t l2arc_buflist_mtx
; /* mutex for all buflists */
723 static list_t L2ARC_free_on_write
; /* free after write buf list */
724 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
725 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
726 static uint64_t l2arc_ndev
; /* number of devices */
728 typedef struct l2arc_read_callback
{
729 arc_buf_t
*l2rcb_buf
; /* read buffer */
730 spa_t
*l2rcb_spa
; /* spa */
731 blkptr_t l2rcb_bp
; /* original blkptr */
732 zbookmark_t l2rcb_zb
; /* original bookmark */
733 int l2rcb_flags
; /* original flags */
734 enum zio_compress l2rcb_compress
; /* applied compress */
735 } l2arc_read_callback_t
;
737 typedef struct l2arc_write_callback
{
738 l2arc_dev_t
*l2wcb_dev
; /* device info */
739 arc_buf_hdr_t
*l2wcb_head
; /* head of write buflist */
740 } l2arc_write_callback_t
;
742 struct l2arc_buf_hdr
{
743 /* protected by arc_buf_hdr mutex */
744 l2arc_dev_t
*b_dev
; /* L2ARC device */
745 uint64_t b_daddr
; /* disk address, offset byte */
746 /* compression applied to buffer data */
747 enum zio_compress b_compress
;
748 /* real alloc'd buffer size depending on b_compress applied */
751 /* temporary buffer holder for in-flight compressed data */
755 typedef struct l2arc_data_free
{
756 /* protected by l2arc_free_on_write_mtx */
759 void (*l2df_func
)(void *, size_t);
760 list_node_t l2df_list_node
;
763 static kmutex_t l2arc_feed_thr_lock
;
764 static kcondvar_t l2arc_feed_thr_cv
;
765 static uint8_t l2arc_thread_exit
;
767 static void l2arc_read_done(zio_t
*zio
);
768 static void l2arc_hdr_stat_add(void);
769 static void l2arc_hdr_stat_remove(void);
771 static boolean_t
l2arc_compress_buf(l2arc_buf_hdr_t
*l2hdr
);
772 static void l2arc_decompress_zio(zio_t
*zio
, arc_buf_hdr_t
*hdr
,
773 enum zio_compress c
);
774 static void l2arc_release_cdata_buf(arc_buf_hdr_t
*ab
);
777 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
779 uint8_t *vdva
= (uint8_t *)dva
;
780 uint64_t crc
= -1ULL;
783 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
785 for (i
= 0; i
< sizeof (dva_t
); i
++)
786 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ vdva
[i
]) & 0xFF];
788 crc
^= (spa
>>8) ^ birth
;
793 #define BUF_EMPTY(buf) \
794 ((buf)->b_dva.dva_word[0] == 0 && \
795 (buf)->b_dva.dva_word[1] == 0 && \
798 #define BUF_EQUAL(spa, dva, birth, buf) \
799 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
800 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
801 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
804 buf_discard_identity(arc_buf_hdr_t
*hdr
)
806 hdr
->b_dva
.dva_word
[0] = 0;
807 hdr
->b_dva
.dva_word
[1] = 0;
812 static arc_buf_hdr_t
*
813 buf_hash_find(uint64_t spa
, const dva_t
*dva
, uint64_t birth
, kmutex_t
**lockp
)
815 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
816 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
819 mutex_enter(hash_lock
);
820 for (buf
= buf_hash_table
.ht_table
[idx
]; buf
!= NULL
;
821 buf
= buf
->b_hash_next
) {
822 if (BUF_EQUAL(spa
, dva
, birth
, buf
)) {
827 mutex_exit(hash_lock
);
833 * Insert an entry into the hash table. If there is already an element
834 * equal to elem in the hash table, then the already existing element
835 * will be returned and the new element will not be inserted.
836 * Otherwise returns NULL.
838 static arc_buf_hdr_t
*
839 buf_hash_insert(arc_buf_hdr_t
*buf
, kmutex_t
**lockp
)
841 uint64_t idx
= BUF_HASH_INDEX(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
);
842 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
846 ASSERT(!HDR_IN_HASH_TABLE(buf
));
848 mutex_enter(hash_lock
);
849 for (fbuf
= buf_hash_table
.ht_table
[idx
], i
= 0; fbuf
!= NULL
;
850 fbuf
= fbuf
->b_hash_next
, i
++) {
851 if (BUF_EQUAL(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
, fbuf
))
855 buf
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
856 buf_hash_table
.ht_table
[idx
] = buf
;
857 buf
->b_flags
|= ARC_IN_HASH_TABLE
;
859 /* collect some hash table performance data */
861 ARCSTAT_BUMP(arcstat_hash_collisions
);
863 ARCSTAT_BUMP(arcstat_hash_chains
);
865 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
868 ARCSTAT_BUMP(arcstat_hash_elements
);
869 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
875 buf_hash_remove(arc_buf_hdr_t
*buf
)
877 arc_buf_hdr_t
*fbuf
, **bufp
;
878 uint64_t idx
= BUF_HASH_INDEX(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
);
880 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
881 ASSERT(HDR_IN_HASH_TABLE(buf
));
883 bufp
= &buf_hash_table
.ht_table
[idx
];
884 while ((fbuf
= *bufp
) != buf
) {
885 ASSERT(fbuf
!= NULL
);
886 bufp
= &fbuf
->b_hash_next
;
888 *bufp
= buf
->b_hash_next
;
889 buf
->b_hash_next
= NULL
;
890 buf
->b_flags
&= ~ARC_IN_HASH_TABLE
;
892 /* collect some hash table performance data */
893 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
895 if (buf_hash_table
.ht_table
[idx
] &&
896 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
897 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
901 * Global data structures and functions for the buf kmem cache.
903 static kmem_cache_t
*hdr_cache
;
904 static kmem_cache_t
*buf_cache
;
905 static kmem_cache_t
*l2arc_hdr_cache
;
912 #if defined(_KERNEL) && defined(HAVE_SPL)
914 * Large allocations which do not require contiguous pages
915 * should be using vmem_free() in the linux kernel\
917 vmem_free(buf_hash_table
.ht_table
,
918 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
920 kmem_free(buf_hash_table
.ht_table
,
921 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
923 for (i
= 0; i
< BUF_LOCKS
; i
++)
924 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
925 kmem_cache_destroy(hdr_cache
);
926 kmem_cache_destroy(buf_cache
);
927 kmem_cache_destroy(l2arc_hdr_cache
);
931 * Constructor callback - called when the cache is empty
932 * and a new buf is requested.
936 hdr_cons(void *vbuf
, void *unused
, int kmflag
)
938 arc_buf_hdr_t
*buf
= vbuf
;
940 bzero(buf
, sizeof (arc_buf_hdr_t
));
941 refcount_create(&buf
->b_refcnt
);
942 cv_init(&buf
->b_cv
, NULL
, CV_DEFAULT
, NULL
);
943 mutex_init(&buf
->b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
944 list_link_init(&buf
->b_arc_node
);
945 list_link_init(&buf
->b_l2node
);
946 arc_space_consume(sizeof (arc_buf_hdr_t
), ARC_SPACE_HDRS
);
953 buf_cons(void *vbuf
, void *unused
, int kmflag
)
955 arc_buf_t
*buf
= vbuf
;
957 bzero(buf
, sizeof (arc_buf_t
));
958 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
959 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
965 * Destructor callback - called when a cached buf is
966 * no longer required.
970 hdr_dest(void *vbuf
, void *unused
)
972 arc_buf_hdr_t
*buf
= vbuf
;
974 ASSERT(BUF_EMPTY(buf
));
975 refcount_destroy(&buf
->b_refcnt
);
976 cv_destroy(&buf
->b_cv
);
977 mutex_destroy(&buf
->b_freeze_lock
);
978 arc_space_return(sizeof (arc_buf_hdr_t
), ARC_SPACE_HDRS
);
983 buf_dest(void *vbuf
, void *unused
)
985 arc_buf_t
*buf
= vbuf
;
987 mutex_destroy(&buf
->b_evict_lock
);
988 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
995 uint64_t hsize
= 1ULL << 12;
999 * The hash table is big enough to fill all of physical memory
1000 * with an average 64K block size. The table will take up
1001 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
1003 while (hsize
* 65536 < physmem
* PAGESIZE
)
1006 buf_hash_table
.ht_mask
= hsize
- 1;
1007 #if defined(_KERNEL) && defined(HAVE_SPL)
1009 * Large allocations which do not require contiguous pages
1010 * should be using vmem_alloc() in the linux kernel
1012 buf_hash_table
.ht_table
=
1013 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
1015 buf_hash_table
.ht_table
=
1016 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
1018 if (buf_hash_table
.ht_table
== NULL
) {
1019 ASSERT(hsize
> (1ULL << 8));
1024 hdr_cache
= kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t
),
1025 0, hdr_cons
, hdr_dest
, NULL
, NULL
, NULL
, 0);
1026 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
1027 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
1028 l2arc_hdr_cache
= kmem_cache_create("l2arc_buf_hdr_t", L2HDR_SIZE
,
1029 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
1031 for (i
= 0; i
< 256; i
++)
1032 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
1033 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
1035 for (i
= 0; i
< BUF_LOCKS
; i
++) {
1036 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
1037 NULL
, MUTEX_DEFAULT
, NULL
);
1041 #define ARC_MINTIME (hz>>4) /* 62 ms */
1044 arc_cksum_verify(arc_buf_t
*buf
)
1048 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1051 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
1052 if (buf
->b_hdr
->b_freeze_cksum
== NULL
||
1053 (buf
->b_hdr
->b_flags
& ARC_IO_ERROR
)) {
1054 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1057 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
1058 if (!ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
))
1059 panic("buffer modified while frozen!");
1060 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1064 arc_cksum_equal(arc_buf_t
*buf
)
1069 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
1070 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
1071 equal
= ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
);
1072 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1078 arc_cksum_compute(arc_buf_t
*buf
, boolean_t force
)
1080 if (!force
&& !(zfs_flags
& ZFS_DEBUG_MODIFY
))
1083 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
1084 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
1085 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1088 buf
->b_hdr
->b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1090 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
,
1091 buf
->b_hdr
->b_freeze_cksum
);
1092 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1098 arc_buf_sigsegv(int sig
, siginfo_t
*si
, void *unused
)
1100 panic("Got SIGSEGV at address: 0x%lx\n", (long) si
->si_addr
);
1106 arc_buf_unwatch(arc_buf_t
*buf
)
1110 ASSERT0(mprotect(buf
->b_data
, buf
->b_hdr
->b_size
,
1111 PROT_READ
| PROT_WRITE
));
1118 arc_buf_watch(arc_buf_t
*buf
)
1122 ASSERT0(mprotect(buf
->b_data
, buf
->b_hdr
->b_size
, PROT_READ
));
1127 arc_buf_thaw(arc_buf_t
*buf
)
1129 if (zfs_flags
& ZFS_DEBUG_MODIFY
) {
1130 if (buf
->b_hdr
->b_state
!= arc_anon
)
1131 panic("modifying non-anon buffer!");
1132 if (buf
->b_hdr
->b_flags
& ARC_IO_IN_PROGRESS
)
1133 panic("modifying buffer while i/o in progress!");
1134 arc_cksum_verify(buf
);
1137 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
1138 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
1139 kmem_free(buf
->b_hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
1140 buf
->b_hdr
->b_freeze_cksum
= NULL
;
1143 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1145 arc_buf_unwatch(buf
);
1149 arc_buf_freeze(arc_buf_t
*buf
)
1151 kmutex_t
*hash_lock
;
1153 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1156 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1157 mutex_enter(hash_lock
);
1159 ASSERT(buf
->b_hdr
->b_freeze_cksum
!= NULL
||
1160 buf
->b_hdr
->b_state
== arc_anon
);
1161 arc_cksum_compute(buf
, B_FALSE
);
1162 mutex_exit(hash_lock
);
1167 add_reference(arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
, void *tag
)
1169 ASSERT(MUTEX_HELD(hash_lock
));
1171 if ((refcount_add(&ab
->b_refcnt
, tag
) == 1) &&
1172 (ab
->b_state
!= arc_anon
)) {
1173 uint64_t delta
= ab
->b_size
* ab
->b_datacnt
;
1174 list_t
*list
= &ab
->b_state
->arcs_list
[ab
->b_type
];
1175 uint64_t *size
= &ab
->b_state
->arcs_lsize
[ab
->b_type
];
1177 ASSERT(!MUTEX_HELD(&ab
->b_state
->arcs_mtx
));
1178 mutex_enter(&ab
->b_state
->arcs_mtx
);
1179 ASSERT(list_link_active(&ab
->b_arc_node
));
1180 list_remove(list
, ab
);
1181 if (GHOST_STATE(ab
->b_state
)) {
1182 ASSERT0(ab
->b_datacnt
);
1183 ASSERT3P(ab
->b_buf
, ==, NULL
);
1187 ASSERT3U(*size
, >=, delta
);
1188 atomic_add_64(size
, -delta
);
1189 mutex_exit(&ab
->b_state
->arcs_mtx
);
1190 /* remove the prefetch flag if we get a reference */
1191 if (ab
->b_flags
& ARC_PREFETCH
)
1192 ab
->b_flags
&= ~ARC_PREFETCH
;
1197 remove_reference(arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
, void *tag
)
1200 arc_state_t
*state
= ab
->b_state
;
1202 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
1203 ASSERT(!GHOST_STATE(state
));
1205 if (((cnt
= refcount_remove(&ab
->b_refcnt
, tag
)) == 0) &&
1206 (state
!= arc_anon
)) {
1207 uint64_t *size
= &state
->arcs_lsize
[ab
->b_type
];
1209 ASSERT(!MUTEX_HELD(&state
->arcs_mtx
));
1210 mutex_enter(&state
->arcs_mtx
);
1211 ASSERT(!list_link_active(&ab
->b_arc_node
));
1212 list_insert_head(&state
->arcs_list
[ab
->b_type
], ab
);
1213 ASSERT(ab
->b_datacnt
> 0);
1214 atomic_add_64(size
, ab
->b_size
* ab
->b_datacnt
);
1215 mutex_exit(&state
->arcs_mtx
);
1221 * Returns detailed information about a specific arc buffer. When the
1222 * state_index argument is set the function will calculate the arc header
1223 * list position for its arc state. Since this requires a linear traversal
1224 * callers are strongly encourage not to do this. However, it can be helpful
1225 * for targeted analysis so the functionality is provided.
1228 arc_buf_info(arc_buf_t
*ab
, arc_buf_info_t
*abi
, int state_index
)
1230 arc_buf_hdr_t
*hdr
= ab
->b_hdr
;
1231 arc_state_t
*state
= hdr
->b_state
;
1233 memset(abi
, 0, sizeof (arc_buf_info_t
));
1234 abi
->abi_flags
= hdr
->b_flags
;
1235 abi
->abi_datacnt
= hdr
->b_datacnt
;
1236 abi
->abi_state_type
= state
? state
->arcs_state
: ARC_STATE_ANON
;
1237 abi
->abi_state_contents
= hdr
->b_type
;
1238 abi
->abi_state_index
= -1;
1239 abi
->abi_size
= hdr
->b_size
;
1240 abi
->abi_access
= hdr
->b_arc_access
;
1241 abi
->abi_mru_hits
= hdr
->b_mru_hits
;
1242 abi
->abi_mru_ghost_hits
= hdr
->b_mru_ghost_hits
;
1243 abi
->abi_mfu_hits
= hdr
->b_mfu_hits
;
1244 abi
->abi_mfu_ghost_hits
= hdr
->b_mfu_ghost_hits
;
1245 abi
->abi_holds
= refcount_count(&hdr
->b_refcnt
);
1248 abi
->abi_l2arc_dattr
= hdr
->b_l2hdr
->b_daddr
;
1249 abi
->abi_l2arc_asize
= hdr
->b_l2hdr
->b_asize
;
1250 abi
->abi_l2arc_compress
= hdr
->b_l2hdr
->b_compress
;
1251 abi
->abi_l2arc_hits
= hdr
->b_l2hdr
->b_hits
;
1254 if (state
&& state_index
&& list_link_active(&hdr
->b_arc_node
)) {
1255 list_t
*list
= &state
->arcs_list
[hdr
->b_type
];
1258 mutex_enter(&state
->arcs_mtx
);
1259 for (h
= list_head(list
); h
!= NULL
; h
= list_next(list
, h
)) {
1260 abi
->abi_state_index
++;
1264 mutex_exit(&state
->arcs_mtx
);
1269 * Move the supplied buffer to the indicated state. The mutex
1270 * for the buffer must be held by the caller.
1273 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
)
1275 arc_state_t
*old_state
= ab
->b_state
;
1276 int64_t refcnt
= refcount_count(&ab
->b_refcnt
);
1277 uint64_t from_delta
, to_delta
;
1279 ASSERT(MUTEX_HELD(hash_lock
));
1280 ASSERT3P(new_state
, !=, old_state
);
1281 ASSERT(refcnt
== 0 || ab
->b_datacnt
> 0);
1282 ASSERT(ab
->b_datacnt
== 0 || !GHOST_STATE(new_state
));
1283 ASSERT(ab
->b_datacnt
<= 1 || old_state
!= arc_anon
);
1285 from_delta
= to_delta
= ab
->b_datacnt
* ab
->b_size
;
1288 * If this buffer is evictable, transfer it from the
1289 * old state list to the new state list.
1292 if (old_state
!= arc_anon
) {
1293 int use_mutex
= !MUTEX_HELD(&old_state
->arcs_mtx
);
1294 uint64_t *size
= &old_state
->arcs_lsize
[ab
->b_type
];
1297 mutex_enter(&old_state
->arcs_mtx
);
1299 ASSERT(list_link_active(&ab
->b_arc_node
));
1300 list_remove(&old_state
->arcs_list
[ab
->b_type
], ab
);
1303 * If prefetching out of the ghost cache,
1304 * we will have a non-zero datacnt.
1306 if (GHOST_STATE(old_state
) && ab
->b_datacnt
== 0) {
1307 /* ghost elements have a ghost size */
1308 ASSERT(ab
->b_buf
== NULL
);
1309 from_delta
= ab
->b_size
;
1311 ASSERT3U(*size
, >=, from_delta
);
1312 atomic_add_64(size
, -from_delta
);
1315 mutex_exit(&old_state
->arcs_mtx
);
1317 if (new_state
!= arc_anon
) {
1318 int use_mutex
= !MUTEX_HELD(&new_state
->arcs_mtx
);
1319 uint64_t *size
= &new_state
->arcs_lsize
[ab
->b_type
];
1322 mutex_enter(&new_state
->arcs_mtx
);
1324 list_insert_head(&new_state
->arcs_list
[ab
->b_type
], ab
);
1326 /* ghost elements have a ghost size */
1327 if (GHOST_STATE(new_state
)) {
1328 ASSERT(ab
->b_datacnt
== 0);
1329 ASSERT(ab
->b_buf
== NULL
);
1330 to_delta
= ab
->b_size
;
1332 atomic_add_64(size
, to_delta
);
1335 mutex_exit(&new_state
->arcs_mtx
);
1339 ASSERT(!BUF_EMPTY(ab
));
1340 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(ab
))
1341 buf_hash_remove(ab
);
1343 /* adjust state sizes */
1345 atomic_add_64(&new_state
->arcs_size
, to_delta
);
1347 ASSERT3U(old_state
->arcs_size
, >=, from_delta
);
1348 atomic_add_64(&old_state
->arcs_size
, -from_delta
);
1350 ab
->b_state
= new_state
;
1352 /* adjust l2arc hdr stats */
1353 if (new_state
== arc_l2c_only
)
1354 l2arc_hdr_stat_add();
1355 else if (old_state
== arc_l2c_only
)
1356 l2arc_hdr_stat_remove();
1360 arc_space_consume(uint64_t space
, arc_space_type_t type
)
1362 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1367 case ARC_SPACE_DATA
:
1368 ARCSTAT_INCR(arcstat_data_size
, space
);
1370 case ARC_SPACE_OTHER
:
1371 ARCSTAT_INCR(arcstat_other_size
, space
);
1373 case ARC_SPACE_HDRS
:
1374 ARCSTAT_INCR(arcstat_hdr_size
, space
);
1376 case ARC_SPACE_L2HDRS
:
1377 ARCSTAT_INCR(arcstat_l2_hdr_size
, space
);
1381 ARCSTAT_INCR(arcstat_meta_used
, space
);
1382 atomic_add_64(&arc_size
, space
);
1386 arc_space_return(uint64_t space
, arc_space_type_t type
)
1388 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1393 case ARC_SPACE_DATA
:
1394 ARCSTAT_INCR(arcstat_data_size
, -space
);
1396 case ARC_SPACE_OTHER
:
1397 ARCSTAT_INCR(arcstat_other_size
, -space
);
1399 case ARC_SPACE_HDRS
:
1400 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
1402 case ARC_SPACE_L2HDRS
:
1403 ARCSTAT_INCR(arcstat_l2_hdr_size
, -space
);
1407 ASSERT(arc_meta_used
>= space
);
1408 if (arc_meta_max
< arc_meta_used
)
1409 arc_meta_max
= arc_meta_used
;
1410 ARCSTAT_INCR(arcstat_meta_used
, -space
);
1411 ASSERT(arc_size
>= space
);
1412 atomic_add_64(&arc_size
, -space
);
1416 arc_buf_alloc(spa_t
*spa
, int size
, void *tag
, arc_buf_contents_t type
)
1421 ASSERT3U(size
, >, 0);
1422 hdr
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
1423 ASSERT(BUF_EMPTY(hdr
));
1426 hdr
->b_spa
= spa_load_guid(spa
);
1427 hdr
->b_state
= arc_anon
;
1428 hdr
->b_arc_access
= 0;
1429 hdr
->b_mru_hits
= 0;
1430 hdr
->b_mru_ghost_hits
= 0;
1431 hdr
->b_mfu_hits
= 0;
1432 hdr
->b_mfu_ghost_hits
= 0;
1434 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1437 buf
->b_efunc
= NULL
;
1438 buf
->b_private
= NULL
;
1441 arc_get_data_buf(buf
);
1444 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1445 (void) refcount_add(&hdr
->b_refcnt
, tag
);
1450 static char *arc_onloan_tag
= "onloan";
1453 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1454 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1455 * buffers must be returned to the arc before they can be used by the DMU or
1459 arc_loan_buf(spa_t
*spa
, int size
)
1463 buf
= arc_buf_alloc(spa
, size
, arc_onloan_tag
, ARC_BUFC_DATA
);
1465 atomic_add_64(&arc_loaned_bytes
, size
);
1470 * Return a loaned arc buffer to the arc.
1473 arc_return_buf(arc_buf_t
*buf
, void *tag
)
1475 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1477 ASSERT(buf
->b_data
!= NULL
);
1478 (void) refcount_add(&hdr
->b_refcnt
, tag
);
1479 (void) refcount_remove(&hdr
->b_refcnt
, arc_onloan_tag
);
1481 atomic_add_64(&arc_loaned_bytes
, -hdr
->b_size
);
1484 /* Detach an arc_buf from a dbuf (tag) */
1486 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
1490 ASSERT(buf
->b_data
!= NULL
);
1492 (void) refcount_add(&hdr
->b_refcnt
, arc_onloan_tag
);
1493 (void) refcount_remove(&hdr
->b_refcnt
, tag
);
1494 buf
->b_efunc
= NULL
;
1495 buf
->b_private
= NULL
;
1497 atomic_add_64(&arc_loaned_bytes
, hdr
->b_size
);
1501 arc_buf_clone(arc_buf_t
*from
)
1504 arc_buf_hdr_t
*hdr
= from
->b_hdr
;
1505 uint64_t size
= hdr
->b_size
;
1507 ASSERT(hdr
->b_state
!= arc_anon
);
1509 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1512 buf
->b_efunc
= NULL
;
1513 buf
->b_private
= NULL
;
1514 buf
->b_next
= hdr
->b_buf
;
1516 arc_get_data_buf(buf
);
1517 bcopy(from
->b_data
, buf
->b_data
, size
);
1520 * This buffer already exists in the arc so create a duplicate
1521 * copy for the caller. If the buffer is associated with user data
1522 * then track the size and number of duplicates. These stats will be
1523 * updated as duplicate buffers are created and destroyed.
1525 if (hdr
->b_type
== ARC_BUFC_DATA
) {
1526 ARCSTAT_BUMP(arcstat_duplicate_buffers
);
1527 ARCSTAT_INCR(arcstat_duplicate_buffers_size
, size
);
1529 hdr
->b_datacnt
+= 1;
1534 arc_buf_add_ref(arc_buf_t
*buf
, void* tag
)
1537 kmutex_t
*hash_lock
;
1540 * Check to see if this buffer is evicted. Callers
1541 * must verify b_data != NULL to know if the add_ref
1544 mutex_enter(&buf
->b_evict_lock
);
1545 if (buf
->b_data
== NULL
) {
1546 mutex_exit(&buf
->b_evict_lock
);
1549 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1550 mutex_enter(hash_lock
);
1552 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1553 mutex_exit(&buf
->b_evict_lock
);
1555 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
1556 add_reference(hdr
, hash_lock
, tag
);
1557 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
1558 arc_access(hdr
, hash_lock
);
1559 mutex_exit(hash_lock
);
1560 ARCSTAT_BUMP(arcstat_hits
);
1561 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
1562 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
1563 data
, metadata
, hits
);
1567 * Free the arc data buffer. If it is an l2arc write in progress,
1568 * the buffer is placed on l2arc_free_on_write to be freed later.
1571 arc_buf_data_free(arc_buf_t
*buf
, void (*free_func
)(void *, size_t))
1573 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1575 if (HDR_L2_WRITING(hdr
)) {
1576 l2arc_data_free_t
*df
;
1577 df
= kmem_alloc(sizeof (l2arc_data_free_t
), KM_PUSHPAGE
);
1578 df
->l2df_data
= buf
->b_data
;
1579 df
->l2df_size
= hdr
->b_size
;
1580 df
->l2df_func
= free_func
;
1581 mutex_enter(&l2arc_free_on_write_mtx
);
1582 list_insert_head(l2arc_free_on_write
, df
);
1583 mutex_exit(&l2arc_free_on_write_mtx
);
1584 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
1586 free_func(buf
->b_data
, hdr
->b_size
);
1591 arc_buf_destroy(arc_buf_t
*buf
, boolean_t recycle
, boolean_t all
)
1595 /* free up data associated with the buf */
1597 arc_state_t
*state
= buf
->b_hdr
->b_state
;
1598 uint64_t size
= buf
->b_hdr
->b_size
;
1599 arc_buf_contents_t type
= buf
->b_hdr
->b_type
;
1601 arc_cksum_verify(buf
);
1602 arc_buf_unwatch(buf
);
1605 if (type
== ARC_BUFC_METADATA
) {
1606 arc_buf_data_free(buf
, zio_buf_free
);
1607 arc_space_return(size
, ARC_SPACE_DATA
);
1609 ASSERT(type
== ARC_BUFC_DATA
);
1610 arc_buf_data_free(buf
, zio_data_buf_free
);
1611 ARCSTAT_INCR(arcstat_data_size
, -size
);
1612 atomic_add_64(&arc_size
, -size
);
1615 if (list_link_active(&buf
->b_hdr
->b_arc_node
)) {
1616 uint64_t *cnt
= &state
->arcs_lsize
[type
];
1618 ASSERT(refcount_is_zero(&buf
->b_hdr
->b_refcnt
));
1619 ASSERT(state
!= arc_anon
);
1621 ASSERT3U(*cnt
, >=, size
);
1622 atomic_add_64(cnt
, -size
);
1624 ASSERT3U(state
->arcs_size
, >=, size
);
1625 atomic_add_64(&state
->arcs_size
, -size
);
1629 * If we're destroying a duplicate buffer make sure
1630 * that the appropriate statistics are updated.
1632 if (buf
->b_hdr
->b_datacnt
> 1 &&
1633 buf
->b_hdr
->b_type
== ARC_BUFC_DATA
) {
1634 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers
);
1635 ARCSTAT_INCR(arcstat_duplicate_buffers_size
, -size
);
1637 ASSERT(buf
->b_hdr
->b_datacnt
> 0);
1638 buf
->b_hdr
->b_datacnt
-= 1;
1641 /* only remove the buf if requested */
1645 /* remove the buf from the hdr list */
1646 for (bufp
= &buf
->b_hdr
->b_buf
; *bufp
!= buf
; bufp
= &(*bufp
)->b_next
)
1648 *bufp
= buf
->b_next
;
1651 ASSERT(buf
->b_efunc
== NULL
);
1653 /* clean up the buf */
1655 kmem_cache_free(buf_cache
, buf
);
1659 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
1661 l2arc_buf_hdr_t
*l2hdr
= hdr
->b_l2hdr
;
1663 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1664 ASSERT3P(hdr
->b_state
, ==, arc_anon
);
1665 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1667 if (l2hdr
!= NULL
) {
1668 boolean_t buflist_held
= MUTEX_HELD(&l2arc_buflist_mtx
);
1670 * To prevent arc_free() and l2arc_evict() from
1671 * attempting to free the same buffer at the same time,
1672 * a FREE_IN_PROGRESS flag is given to arc_free() to
1673 * give it priority. l2arc_evict() can't destroy this
1674 * header while we are waiting on l2arc_buflist_mtx.
1676 * The hdr may be removed from l2ad_buflist before we
1677 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1679 if (!buflist_held
) {
1680 mutex_enter(&l2arc_buflist_mtx
);
1681 l2hdr
= hdr
->b_l2hdr
;
1684 if (l2hdr
!= NULL
) {
1685 list_remove(l2hdr
->b_dev
->l2ad_buflist
, hdr
);
1686 ARCSTAT_INCR(arcstat_l2_size
, -hdr
->b_size
);
1687 ARCSTAT_INCR(arcstat_l2_asize
, -l2hdr
->b_asize
);
1688 kmem_cache_free(l2arc_hdr_cache
, l2hdr
);
1689 arc_space_return(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
1690 if (hdr
->b_state
== arc_l2c_only
)
1691 l2arc_hdr_stat_remove();
1692 hdr
->b_l2hdr
= NULL
;
1696 mutex_exit(&l2arc_buflist_mtx
);
1699 if (!BUF_EMPTY(hdr
)) {
1700 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1701 buf_discard_identity(hdr
);
1703 while (hdr
->b_buf
) {
1704 arc_buf_t
*buf
= hdr
->b_buf
;
1707 mutex_enter(&arc_eviction_mtx
);
1708 mutex_enter(&buf
->b_evict_lock
);
1709 ASSERT(buf
->b_hdr
!= NULL
);
1710 arc_buf_destroy(hdr
->b_buf
, FALSE
, FALSE
);
1711 hdr
->b_buf
= buf
->b_next
;
1712 buf
->b_hdr
= &arc_eviction_hdr
;
1713 buf
->b_next
= arc_eviction_list
;
1714 arc_eviction_list
= buf
;
1715 mutex_exit(&buf
->b_evict_lock
);
1716 mutex_exit(&arc_eviction_mtx
);
1718 arc_buf_destroy(hdr
->b_buf
, FALSE
, TRUE
);
1721 if (hdr
->b_freeze_cksum
!= NULL
) {
1722 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
1723 hdr
->b_freeze_cksum
= NULL
;
1726 ASSERT(!list_link_active(&hdr
->b_arc_node
));
1727 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
1728 ASSERT3P(hdr
->b_acb
, ==, NULL
);
1729 kmem_cache_free(hdr_cache
, hdr
);
1733 arc_buf_free(arc_buf_t
*buf
, void *tag
)
1735 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1736 int hashed
= hdr
->b_state
!= arc_anon
;
1738 ASSERT(buf
->b_efunc
== NULL
);
1739 ASSERT(buf
->b_data
!= NULL
);
1742 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
1744 mutex_enter(hash_lock
);
1746 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1748 (void) remove_reference(hdr
, hash_lock
, tag
);
1749 if (hdr
->b_datacnt
> 1) {
1750 arc_buf_destroy(buf
, FALSE
, TRUE
);
1752 ASSERT(buf
== hdr
->b_buf
);
1753 ASSERT(buf
->b_efunc
== NULL
);
1754 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
1756 mutex_exit(hash_lock
);
1757 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
1760 * We are in the middle of an async write. Don't destroy
1761 * this buffer unless the write completes before we finish
1762 * decrementing the reference count.
1764 mutex_enter(&arc_eviction_mtx
);
1765 (void) remove_reference(hdr
, NULL
, tag
);
1766 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1767 destroy_hdr
= !HDR_IO_IN_PROGRESS(hdr
);
1768 mutex_exit(&arc_eviction_mtx
);
1770 arc_hdr_destroy(hdr
);
1772 if (remove_reference(hdr
, NULL
, tag
) > 0)
1773 arc_buf_destroy(buf
, FALSE
, TRUE
);
1775 arc_hdr_destroy(hdr
);
1780 arc_buf_remove_ref(arc_buf_t
*buf
, void* tag
)
1782 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1783 kmutex_t
*hash_lock
= NULL
;
1784 boolean_t no_callback
= (buf
->b_efunc
== NULL
);
1786 if (hdr
->b_state
== arc_anon
) {
1787 ASSERT(hdr
->b_datacnt
== 1);
1788 arc_buf_free(buf
, tag
);
1789 return (no_callback
);
1792 hash_lock
= HDR_LOCK(hdr
);
1793 mutex_enter(hash_lock
);
1795 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1796 ASSERT(hdr
->b_state
!= arc_anon
);
1797 ASSERT(buf
->b_data
!= NULL
);
1799 (void) remove_reference(hdr
, hash_lock
, tag
);
1800 if (hdr
->b_datacnt
> 1) {
1802 arc_buf_destroy(buf
, FALSE
, TRUE
);
1803 } else if (no_callback
) {
1804 ASSERT(hdr
->b_buf
== buf
&& buf
->b_next
== NULL
);
1805 ASSERT(buf
->b_efunc
== NULL
);
1806 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
1808 ASSERT(no_callback
|| hdr
->b_datacnt
> 1 ||
1809 refcount_is_zero(&hdr
->b_refcnt
));
1810 mutex_exit(hash_lock
);
1811 return (no_callback
);
1815 arc_buf_size(arc_buf_t
*buf
)
1817 return (buf
->b_hdr
->b_size
);
1821 * Called from the DMU to determine if the current buffer should be
1822 * evicted. In order to ensure proper locking, the eviction must be initiated
1823 * from the DMU. Return true if the buffer is associated with user data and
1824 * duplicate buffers still exist.
1827 arc_buf_eviction_needed(arc_buf_t
*buf
)
1830 boolean_t evict_needed
= B_FALSE
;
1832 if (zfs_disable_dup_eviction
)
1835 mutex_enter(&buf
->b_evict_lock
);
1839 * We are in arc_do_user_evicts(); let that function
1840 * perform the eviction.
1842 ASSERT(buf
->b_data
== NULL
);
1843 mutex_exit(&buf
->b_evict_lock
);
1845 } else if (buf
->b_data
== NULL
) {
1847 * We have already been added to the arc eviction list;
1848 * recommend eviction.
1850 ASSERT3P(hdr
, ==, &arc_eviction_hdr
);
1851 mutex_exit(&buf
->b_evict_lock
);
1855 if (hdr
->b_datacnt
> 1 && hdr
->b_type
== ARC_BUFC_DATA
)
1856 evict_needed
= B_TRUE
;
1858 mutex_exit(&buf
->b_evict_lock
);
1859 return (evict_needed
);
1863 * Evict buffers from list until we've removed the specified number of
1864 * bytes. Move the removed buffers to the appropriate evict state.
1865 * If the recycle flag is set, then attempt to "recycle" a buffer:
1866 * - look for a buffer to evict that is `bytes' long.
1867 * - return the data block from this buffer rather than freeing it.
1868 * This flag is used by callers that are trying to make space for a
1869 * new buffer in a full arc cache.
1871 * This function makes a "best effort". It skips over any buffers
1872 * it can't get a hash_lock on, and so may not catch all candidates.
1873 * It may also return without evicting as much space as requested.
1876 arc_evict(arc_state_t
*state
, uint64_t spa
, int64_t bytes
, boolean_t recycle
,
1877 arc_buf_contents_t type
)
1879 arc_state_t
*evicted_state
;
1880 uint64_t bytes_evicted
= 0, skipped
= 0, missed
= 0;
1881 arc_buf_hdr_t
*ab
, *ab_prev
= NULL
;
1882 list_t
*list
= &state
->arcs_list
[type
];
1883 kmutex_t
*hash_lock
;
1884 boolean_t have_lock
;
1885 void *stolen
= NULL
;
1886 arc_buf_hdr_t marker
= {{{ 0 }}};
1889 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
1891 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
1893 mutex_enter(&state
->arcs_mtx
);
1894 mutex_enter(&evicted_state
->arcs_mtx
);
1896 for (ab
= list_tail(list
); ab
; ab
= ab_prev
) {
1897 ab_prev
= list_prev(list
, ab
);
1898 /* prefetch buffers have a minimum lifespan */
1899 if (HDR_IO_IN_PROGRESS(ab
) ||
1900 (spa
&& ab
->b_spa
!= spa
) ||
1901 (ab
->b_flags
& (ARC_PREFETCH
|ARC_INDIRECT
) &&
1902 ddi_get_lbolt() - ab
->b_arc_access
<
1903 zfs_arc_min_prefetch_lifespan
)) {
1907 /* "lookahead" for better eviction candidate */
1908 if (recycle
&& ab
->b_size
!= bytes
&&
1909 ab_prev
&& ab_prev
->b_size
== bytes
)
1912 /* ignore markers */
1917 * It may take a long time to evict all the bufs requested.
1918 * To avoid blocking all arc activity, periodically drop
1919 * the arcs_mtx and give other threads a chance to run
1920 * before reacquiring the lock.
1922 * If we are looking for a buffer to recycle, we are in
1923 * the hot code path, so don't sleep.
1925 if (!recycle
&& count
++ > arc_evict_iterations
) {
1926 list_insert_after(list
, ab
, &marker
);
1927 mutex_exit(&evicted_state
->arcs_mtx
);
1928 mutex_exit(&state
->arcs_mtx
);
1929 kpreempt(KPREEMPT_SYNC
);
1930 mutex_enter(&state
->arcs_mtx
);
1931 mutex_enter(&evicted_state
->arcs_mtx
);
1932 ab_prev
= list_prev(list
, &marker
);
1933 list_remove(list
, &marker
);
1938 hash_lock
= HDR_LOCK(ab
);
1939 have_lock
= MUTEX_HELD(hash_lock
);
1940 if (have_lock
|| mutex_tryenter(hash_lock
)) {
1941 ASSERT0(refcount_count(&ab
->b_refcnt
));
1942 ASSERT(ab
->b_datacnt
> 0);
1944 arc_buf_t
*buf
= ab
->b_buf
;
1945 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
1950 bytes_evicted
+= ab
->b_size
;
1951 if (recycle
&& ab
->b_type
== type
&&
1952 ab
->b_size
== bytes
&&
1953 !HDR_L2_WRITING(ab
)) {
1954 stolen
= buf
->b_data
;
1959 mutex_enter(&arc_eviction_mtx
);
1960 arc_buf_destroy(buf
,
1961 buf
->b_data
== stolen
, FALSE
);
1962 ab
->b_buf
= buf
->b_next
;
1963 buf
->b_hdr
= &arc_eviction_hdr
;
1964 buf
->b_next
= arc_eviction_list
;
1965 arc_eviction_list
= buf
;
1966 mutex_exit(&arc_eviction_mtx
);
1967 mutex_exit(&buf
->b_evict_lock
);
1969 mutex_exit(&buf
->b_evict_lock
);
1970 arc_buf_destroy(buf
,
1971 buf
->b_data
== stolen
, TRUE
);
1976 ARCSTAT_INCR(arcstat_evict_l2_cached
,
1979 if (l2arc_write_eligible(ab
->b_spa
, ab
)) {
1980 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
1984 arcstat_evict_l2_ineligible
,
1989 if (ab
->b_datacnt
== 0) {
1990 arc_change_state(evicted_state
, ab
, hash_lock
);
1991 ASSERT(HDR_IN_HASH_TABLE(ab
));
1992 ab
->b_flags
|= ARC_IN_HASH_TABLE
;
1993 ab
->b_flags
&= ~ARC_BUF_AVAILABLE
;
1994 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, ab
);
1997 mutex_exit(hash_lock
);
1998 if (bytes
>= 0 && bytes_evicted
>= bytes
)
2005 mutex_exit(&evicted_state
->arcs_mtx
);
2006 mutex_exit(&state
->arcs_mtx
);
2008 if (bytes_evicted
< bytes
)
2009 dprintf("only evicted %lld bytes from %x\n",
2010 (longlong_t
)bytes_evicted
, state
);
2013 ARCSTAT_INCR(arcstat_evict_skip
, skipped
);
2016 ARCSTAT_INCR(arcstat_mutex_miss
, missed
);
2019 * Note: we have just evicted some data into the ghost state,
2020 * potentially putting the ghost size over the desired size. Rather
2021 * that evicting from the ghost list in this hot code path, leave
2022 * this chore to the arc_reclaim_thread().
2029 * Remove buffers from list until we've removed the specified number of
2030 * bytes. Destroy the buffers that are removed.
2033 arc_evict_ghost(arc_state_t
*state
, uint64_t spa
, int64_t bytes
,
2034 arc_buf_contents_t type
)
2036 arc_buf_hdr_t
*ab
, *ab_prev
;
2037 arc_buf_hdr_t marker
;
2038 list_t
*list
= &state
->arcs_list
[type
];
2039 kmutex_t
*hash_lock
;
2040 uint64_t bytes_deleted
= 0;
2041 uint64_t bufs_skipped
= 0;
2044 ASSERT(GHOST_STATE(state
));
2045 bzero(&marker
, sizeof (marker
));
2047 mutex_enter(&state
->arcs_mtx
);
2048 for (ab
= list_tail(list
); ab
; ab
= ab_prev
) {
2049 ab_prev
= list_prev(list
, ab
);
2050 if (ab
->b_type
> ARC_BUFC_NUMTYPES
)
2051 panic("invalid ab=%p", (void *)ab
);
2052 if (spa
&& ab
->b_spa
!= spa
)
2055 /* ignore markers */
2059 hash_lock
= HDR_LOCK(ab
);
2060 /* caller may be trying to modify this buffer, skip it */
2061 if (MUTEX_HELD(hash_lock
))
2065 * It may take a long time to evict all the bufs requested.
2066 * To avoid blocking all arc activity, periodically drop
2067 * the arcs_mtx and give other threads a chance to run
2068 * before reacquiring the lock.
2070 if (count
++ > arc_evict_iterations
) {
2071 list_insert_after(list
, ab
, &marker
);
2072 mutex_exit(&state
->arcs_mtx
);
2073 kpreempt(KPREEMPT_SYNC
);
2074 mutex_enter(&state
->arcs_mtx
);
2075 ab_prev
= list_prev(list
, &marker
);
2076 list_remove(list
, &marker
);
2080 if (mutex_tryenter(hash_lock
)) {
2081 ASSERT(!HDR_IO_IN_PROGRESS(ab
));
2082 ASSERT(ab
->b_buf
== NULL
);
2083 ARCSTAT_BUMP(arcstat_deleted
);
2084 bytes_deleted
+= ab
->b_size
;
2086 if (ab
->b_l2hdr
!= NULL
) {
2088 * This buffer is cached on the 2nd Level ARC;
2089 * don't destroy the header.
2091 arc_change_state(arc_l2c_only
, ab
, hash_lock
);
2092 mutex_exit(hash_lock
);
2094 arc_change_state(arc_anon
, ab
, hash_lock
);
2095 mutex_exit(hash_lock
);
2096 arc_hdr_destroy(ab
);
2099 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, ab
);
2100 if (bytes
>= 0 && bytes_deleted
>= bytes
)
2102 } else if (bytes
< 0) {
2104 * Insert a list marker and then wait for the
2105 * hash lock to become available. Once its
2106 * available, restart from where we left off.
2108 list_insert_after(list
, ab
, &marker
);
2109 mutex_exit(&state
->arcs_mtx
);
2110 mutex_enter(hash_lock
);
2111 mutex_exit(hash_lock
);
2112 mutex_enter(&state
->arcs_mtx
);
2113 ab_prev
= list_prev(list
, &marker
);
2114 list_remove(list
, &marker
);
2119 mutex_exit(&state
->arcs_mtx
);
2121 if (list
== &state
->arcs_list
[ARC_BUFC_DATA
] &&
2122 (bytes
< 0 || bytes_deleted
< bytes
)) {
2123 list
= &state
->arcs_list
[ARC_BUFC_METADATA
];
2128 ARCSTAT_INCR(arcstat_mutex_miss
, bufs_skipped
);
2132 if (bytes_deleted
< bytes
)
2133 dprintf("only deleted %lld bytes from %p\n",
2134 (longlong_t
)bytes_deleted
, state
);
2140 int64_t adjustment
, delta
;
2146 adjustment
= MIN((int64_t)(arc_size
- arc_c
),
2147 (int64_t)(arc_anon
->arcs_size
+ arc_mru
->arcs_size
+ arc_meta_used
-
2150 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] > 0) {
2151 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_DATA
], adjustment
);
2152 (void) arc_evict(arc_mru
, 0, delta
, FALSE
, ARC_BUFC_DATA
);
2153 adjustment
-= delta
;
2156 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
2157 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
], adjustment
);
2158 (void) arc_evict(arc_mru
, 0, delta
, FALSE
,
2166 adjustment
= arc_size
- arc_c
;
2168 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] > 0) {
2169 delta
= MIN(adjustment
, arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
]);
2170 (void) arc_evict(arc_mfu
, 0, delta
, FALSE
, ARC_BUFC_DATA
);
2171 adjustment
-= delta
;
2174 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
2175 int64_t delta
= MIN(adjustment
,
2176 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
]);
2177 (void) arc_evict(arc_mfu
, 0, delta
, FALSE
,
2182 * Adjust ghost lists
2185 adjustment
= arc_mru
->arcs_size
+ arc_mru_ghost
->arcs_size
- arc_c
;
2187 if (adjustment
> 0 && arc_mru_ghost
->arcs_size
> 0) {
2188 delta
= MIN(arc_mru_ghost
->arcs_size
, adjustment
);
2189 arc_evict_ghost(arc_mru_ghost
, 0, delta
, ARC_BUFC_DATA
);
2193 arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
- arc_c
;
2195 if (adjustment
> 0 && arc_mfu_ghost
->arcs_size
> 0) {
2196 delta
= MIN(arc_mfu_ghost
->arcs_size
, adjustment
);
2197 arc_evict_ghost(arc_mfu_ghost
, 0, delta
, ARC_BUFC_DATA
);
2202 * Request that arc user drop references so that N bytes can be released
2203 * from the cache. This provides a mechanism to ensure the arc can honor
2204 * the arc_meta_limit and reclaim buffers which are pinned in the cache
2205 * by higher layers. (i.e. the zpl)
2208 arc_do_user_prune(int64_t adjustment
)
2210 arc_prune_func_t
*func
;
2212 arc_prune_t
*cp
, *np
;
2214 mutex_enter(&arc_prune_mtx
);
2216 cp
= list_head(&arc_prune_list
);
2217 while (cp
!= NULL
) {
2219 private = cp
->p_private
;
2220 np
= list_next(&arc_prune_list
, cp
);
2221 refcount_add(&cp
->p_refcnt
, func
);
2222 mutex_exit(&arc_prune_mtx
);
2225 func(adjustment
, private);
2227 mutex_enter(&arc_prune_mtx
);
2229 /* User removed prune callback concurrently with execution */
2230 if (refcount_remove(&cp
->p_refcnt
, func
) == 0) {
2231 ASSERT(!list_link_active(&cp
->p_node
));
2232 refcount_destroy(&cp
->p_refcnt
);
2233 kmem_free(cp
, sizeof (*cp
));
2239 ARCSTAT_BUMP(arcstat_prune
);
2240 mutex_exit(&arc_prune_mtx
);
2244 arc_do_user_evicts(void)
2246 mutex_enter(&arc_eviction_mtx
);
2247 while (arc_eviction_list
!= NULL
) {
2248 arc_buf_t
*buf
= arc_eviction_list
;
2249 arc_eviction_list
= buf
->b_next
;
2250 mutex_enter(&buf
->b_evict_lock
);
2252 mutex_exit(&buf
->b_evict_lock
);
2253 mutex_exit(&arc_eviction_mtx
);
2255 if (buf
->b_efunc
!= NULL
)
2256 VERIFY(buf
->b_efunc(buf
) == 0);
2258 buf
->b_efunc
= NULL
;
2259 buf
->b_private
= NULL
;
2260 kmem_cache_free(buf_cache
, buf
);
2261 mutex_enter(&arc_eviction_mtx
);
2263 mutex_exit(&arc_eviction_mtx
);
2267 * Evict only meta data objects from the cache leaving the data objects.
2268 * This is only used to enforce the tunable arc_meta_limit, if we are
2269 * unable to evict enough buffers notify the user via the prune callback.
2272 arc_adjust_meta(int64_t adjustment
, boolean_t may_prune
)
2276 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
2277 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
], adjustment
);
2278 arc_evict(arc_mru
, 0, delta
, FALSE
, ARC_BUFC_METADATA
);
2279 adjustment
-= delta
;
2282 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
2283 delta
= MIN(arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
], adjustment
);
2284 arc_evict(arc_mfu
, 0, delta
, FALSE
, ARC_BUFC_METADATA
);
2285 adjustment
-= delta
;
2288 if (may_prune
&& (adjustment
> 0) && (arc_meta_used
> arc_meta_limit
))
2289 arc_do_user_prune(zfs_arc_meta_prune
);
2293 * Flush all *evictable* data from the cache for the given spa.
2294 * NOTE: this will not touch "active" (i.e. referenced) data.
2297 arc_flush(spa_t
*spa
)
2302 guid
= spa_load_guid(spa
);
2304 while (list_head(&arc_mru
->arcs_list
[ARC_BUFC_DATA
])) {
2305 (void) arc_evict(arc_mru
, guid
, -1, FALSE
, ARC_BUFC_DATA
);
2309 while (list_head(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
])) {
2310 (void) arc_evict(arc_mru
, guid
, -1, FALSE
, ARC_BUFC_METADATA
);
2314 while (list_head(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
])) {
2315 (void) arc_evict(arc_mfu
, guid
, -1, FALSE
, ARC_BUFC_DATA
);
2319 while (list_head(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
])) {
2320 (void) arc_evict(arc_mfu
, guid
, -1, FALSE
, ARC_BUFC_METADATA
);
2325 arc_evict_ghost(arc_mru_ghost
, guid
, -1, ARC_BUFC_DATA
);
2326 arc_evict_ghost(arc_mfu_ghost
, guid
, -1, ARC_BUFC_DATA
);
2328 mutex_enter(&arc_reclaim_thr_lock
);
2329 arc_do_user_evicts();
2330 mutex_exit(&arc_reclaim_thr_lock
);
2331 ASSERT(spa
|| arc_eviction_list
== NULL
);
2335 arc_shrink(uint64_t bytes
)
2337 if (arc_c
> arc_c_min
) {
2341 to_free
= bytes
? bytes
: arc_c
>> zfs_arc_shrink_shift
;
2343 if (arc_c
> arc_c_min
+ to_free
)
2344 atomic_add_64(&arc_c
, -to_free
);
2348 arc_p_min
= (arc_c
>> zfs_arc_p_min_shift
);
2349 to_free
= bytes
? bytes
: arc_p
>> zfs_arc_shrink_shift
;
2351 if (arc_p
> arc_p_min
+ to_free
)
2352 atomic_add_64(&arc_p
, -to_free
);
2356 if (arc_c
> arc_size
)
2357 arc_c
= MAX(arc_size
, arc_c_min
);
2359 arc_p
= (arc_c
>> 1);
2360 ASSERT(arc_c
>= arc_c_min
);
2361 ASSERT((int64_t)arc_p
>= 0);
2364 if (arc_size
> arc_c
)
2369 arc_kmem_reap_now(arc_reclaim_strategy_t strat
, uint64_t bytes
)
2372 kmem_cache_t
*prev_cache
= NULL
;
2373 kmem_cache_t
*prev_data_cache
= NULL
;
2374 extern kmem_cache_t
*zio_buf_cache
[];
2375 extern kmem_cache_t
*zio_data_buf_cache
[];
2378 * An aggressive reclamation will shrink the cache size as well as
2379 * reap free buffers from the arc kmem caches.
2381 if (strat
== ARC_RECLAIM_AGGR
)
2384 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
2385 if (zio_buf_cache
[i
] != prev_cache
) {
2386 prev_cache
= zio_buf_cache
[i
];
2387 kmem_cache_reap_now(zio_buf_cache
[i
]);
2389 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
2390 prev_data_cache
= zio_data_buf_cache
[i
];
2391 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
2395 kmem_cache_reap_now(buf_cache
);
2396 kmem_cache_reap_now(hdr_cache
);
2400 * Unlike other ZFS implementations this thread is only responsible for
2401 * adapting the target ARC size on Linux. The responsibility for memory
2402 * reclamation has been entirely delegated to the arc_shrinker_func()
2403 * which is registered with the VM. To reflect this change in behavior
2404 * the arc_reclaim thread has been renamed to arc_adapt.
2407 arc_adapt_thread(void)
2412 CALLB_CPR_INIT(&cpr
, &arc_reclaim_thr_lock
, callb_generic_cpr
, FTAG
);
2414 mutex_enter(&arc_reclaim_thr_lock
);
2415 while (arc_thread_exit
== 0) {
2417 arc_reclaim_strategy_t last_reclaim
= ARC_RECLAIM_CONS
;
2419 if (spa_get_random(100) == 0) {
2422 if (last_reclaim
== ARC_RECLAIM_CONS
) {
2423 last_reclaim
= ARC_RECLAIM_AGGR
;
2425 last_reclaim
= ARC_RECLAIM_CONS
;
2429 last_reclaim
= ARC_RECLAIM_AGGR
;
2433 /* reset the growth delay for every reclaim */
2434 arc_grow_time
= ddi_get_lbolt() +
2435 (zfs_arc_grow_retry
* hz
);
2437 arc_kmem_reap_now(last_reclaim
, 0);
2440 #endif /* !_KERNEL */
2442 /* No recent memory pressure allow the ARC to grow. */
2443 if (arc_no_grow
&& ddi_get_lbolt() >= arc_grow_time
)
2444 arc_no_grow
= FALSE
;
2447 * Keep meta data usage within limits, arc_shrink() is not
2448 * used to avoid collapsing the arc_c value when only the
2449 * arc_meta_limit is being exceeded.
2451 prune
= (int64_t)arc_meta_used
- (int64_t)arc_meta_limit
;
2453 arc_adjust_meta(prune
, B_TRUE
);
2457 if (arc_eviction_list
!= NULL
)
2458 arc_do_user_evicts();
2460 /* block until needed, or one second, whichever is shorter */
2461 CALLB_CPR_SAFE_BEGIN(&cpr
);
2462 (void) cv_timedwait_interruptible(&arc_reclaim_thr_cv
,
2463 &arc_reclaim_thr_lock
, (ddi_get_lbolt() + hz
));
2464 CALLB_CPR_SAFE_END(&cpr
, &arc_reclaim_thr_lock
);
2467 /* Allow the module options to be changed */
2468 if (zfs_arc_max
> 64 << 20 &&
2469 zfs_arc_max
< physmem
* PAGESIZE
&&
2470 zfs_arc_max
!= arc_c_max
)
2471 arc_c_max
= zfs_arc_max
;
2473 if (zfs_arc_min
> 0 &&
2474 zfs_arc_min
< arc_c_max
&&
2475 zfs_arc_min
!= arc_c_min
)
2476 arc_c_min
= zfs_arc_min
;
2478 if (zfs_arc_meta_limit
> 0 &&
2479 zfs_arc_meta_limit
<= arc_c_max
&&
2480 zfs_arc_meta_limit
!= arc_meta_limit
)
2481 arc_meta_limit
= zfs_arc_meta_limit
;
2487 arc_thread_exit
= 0;
2488 cv_broadcast(&arc_reclaim_thr_cv
);
2489 CALLB_CPR_EXIT(&cpr
); /* drops arc_reclaim_thr_lock */
2495 * Determine the amount of memory eligible for eviction contained in the
2496 * ARC. All clean data reported by the ghost lists can always be safely
2497 * evicted. Due to arc_c_min, the same does not hold for all clean data
2498 * contained by the regular mru and mfu lists.
2500 * In the case of the regular mru and mfu lists, we need to report as
2501 * much clean data as possible, such that evicting that same reported
2502 * data will not bring arc_size below arc_c_min. Thus, in certain
2503 * circumstances, the total amount of clean data in the mru and mfu
2504 * lists might not actually be evictable.
2506 * The following two distinct cases are accounted for:
2508 * 1. The sum of the amount of dirty data contained by both the mru and
2509 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2510 * is greater than or equal to arc_c_min.
2511 * (i.e. amount of dirty data >= arc_c_min)
2513 * This is the easy case; all clean data contained by the mru and mfu
2514 * lists is evictable. Evicting all clean data can only drop arc_size
2515 * to the amount of dirty data, which is greater than arc_c_min.
2517 * 2. The sum of the amount of dirty data contained by both the mru and
2518 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2519 * is less than arc_c_min.
2520 * (i.e. arc_c_min > amount of dirty data)
2522 * 2.1. arc_size is greater than or equal arc_c_min.
2523 * (i.e. arc_size >= arc_c_min > amount of dirty data)
2525 * In this case, not all clean data from the regular mru and mfu
2526 * lists is actually evictable; we must leave enough clean data
2527 * to keep arc_size above arc_c_min. Thus, the maximum amount of
2528 * evictable data from the two lists combined, is exactly the
2529 * difference between arc_size and arc_c_min.
2531 * 2.2. arc_size is less than arc_c_min
2532 * (i.e. arc_c_min > arc_size > amount of dirty data)
2534 * In this case, none of the data contained in the mru and mfu
2535 * lists is evictable, even if it's clean. Since arc_size is
2536 * already below arc_c_min, evicting any more would only
2537 * increase this negative difference.
2540 arc_evictable_memory(void) {
2541 uint64_t arc_clean
=
2542 arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] +
2543 arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] +
2544 arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] +
2545 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
];
2546 uint64_t ghost_clean
=
2547 arc_mru_ghost
->arcs_lsize
[ARC_BUFC_DATA
] +
2548 arc_mru_ghost
->arcs_lsize
[ARC_BUFC_METADATA
] +
2549 arc_mfu_ghost
->arcs_lsize
[ARC_BUFC_DATA
] +
2550 arc_mfu_ghost
->arcs_lsize
[ARC_BUFC_METADATA
];
2551 uint64_t arc_dirty
= MAX((int64_t)arc_size
- (int64_t)arc_clean
, 0);
2553 if (arc_dirty
>= arc_c_min
)
2554 return (ghost_clean
+ arc_clean
);
2556 return (ghost_clean
+ MAX((int64_t)arc_size
- (int64_t)arc_c_min
, 0));
2560 __arc_shrinker_func(struct shrinker
*shrink
, struct shrink_control
*sc
)
2564 /* The arc is considered warm once reclaim has occurred */
2565 if (unlikely(arc_warm
== B_FALSE
))
2568 /* Return the potential number of reclaimable pages */
2569 pages
= btop(arc_evictable_memory());
2570 if (sc
->nr_to_scan
== 0)
2573 /* Not allowed to perform filesystem reclaim */
2574 if (!(sc
->gfp_mask
& __GFP_FS
))
2577 /* Reclaim in progress */
2578 if (mutex_tryenter(&arc_reclaim_thr_lock
) == 0)
2582 * Evict the requested number of pages by shrinking arc_c the
2583 * requested amount. If there is nothing left to evict just
2584 * reap whatever we can from the various arc slabs.
2587 arc_kmem_reap_now(ARC_RECLAIM_AGGR
, ptob(sc
->nr_to_scan
));
2589 arc_kmem_reap_now(ARC_RECLAIM_CONS
, ptob(sc
->nr_to_scan
));
2593 * When direct reclaim is observed it usually indicates a rapid
2594 * increase in memory pressure. This occurs because the kswapd
2595 * threads were unable to asynchronously keep enough free memory
2596 * available. In this case set arc_no_grow to briefly pause arc
2597 * growth to avoid compounding the memory pressure.
2599 if (current_is_kswapd()) {
2600 ARCSTAT_BUMP(arcstat_memory_indirect_count
);
2602 arc_no_grow
= B_TRUE
;
2603 arc_grow_time
= ddi_get_lbolt() + (zfs_arc_grow_retry
* hz
);
2604 ARCSTAT_BUMP(arcstat_memory_direct_count
);
2607 mutex_exit(&arc_reclaim_thr_lock
);
2611 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func
);
2613 SPL_SHRINKER_DECLARE(arc_shrinker
, arc_shrinker_func
, DEFAULT_SEEKS
);
2614 #endif /* _KERNEL */
2617 * Adapt arc info given the number of bytes we are trying to add and
2618 * the state that we are comming from. This function is only called
2619 * when we are adding new content to the cache.
2622 arc_adapt(int bytes
, arc_state_t
*state
)
2625 uint64_t arc_p_min
= (arc_c
>> zfs_arc_p_min_shift
);
2627 if (state
== arc_l2c_only
)
2632 * Adapt the target size of the MRU list:
2633 * - if we just hit in the MRU ghost list, then increase
2634 * the target size of the MRU list.
2635 * - if we just hit in the MFU ghost list, then increase
2636 * the target size of the MFU list by decreasing the
2637 * target size of the MRU list.
2639 if (state
== arc_mru_ghost
) {
2640 mult
= ((arc_mru_ghost
->arcs_size
>= arc_mfu_ghost
->arcs_size
) ?
2641 1 : (arc_mfu_ghost
->arcs_size
/arc_mru_ghost
->arcs_size
));
2642 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
2644 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
2645 } else if (state
== arc_mfu_ghost
) {
2648 mult
= ((arc_mfu_ghost
->arcs_size
>= arc_mru_ghost
->arcs_size
) ?
2649 1 : (arc_mru_ghost
->arcs_size
/arc_mfu_ghost
->arcs_size
));
2650 mult
= MIN(mult
, 10);
2652 delta
= MIN(bytes
* mult
, arc_p
);
2653 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
2655 ASSERT((int64_t)arc_p
>= 0);
2660 if (arc_c
>= arc_c_max
)
2664 * If we're within (2 * maxblocksize) bytes of the target
2665 * cache size, increment the target cache size
2667 if (arc_size
> arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
2668 atomic_add_64(&arc_c
, (int64_t)bytes
);
2669 if (arc_c
> arc_c_max
)
2671 else if (state
== arc_anon
)
2672 atomic_add_64(&arc_p
, (int64_t)bytes
);
2676 ASSERT((int64_t)arc_p
>= 0);
2680 * Check if the cache has reached its limits and eviction is required
2684 arc_evict_needed(arc_buf_contents_t type
)
2686 if (type
== ARC_BUFC_METADATA
&& arc_meta_used
>= arc_meta_limit
)
2692 return (arc_size
> arc_c
);
2696 * The buffer, supplied as the first argument, needs a data block.
2697 * So, if we are at cache max, determine which cache should be victimized.
2698 * We have the following cases:
2700 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2701 * In this situation if we're out of space, but the resident size of the MFU is
2702 * under the limit, victimize the MFU cache to satisfy this insertion request.
2704 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2705 * Here, we've used up all of the available space for the MRU, so we need to
2706 * evict from our own cache instead. Evict from the set of resident MRU
2709 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2710 * c minus p represents the MFU space in the cache, since p is the size of the
2711 * cache that is dedicated to the MRU. In this situation there's still space on
2712 * the MFU side, so the MRU side needs to be victimized.
2714 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2715 * MFU's resident set is consuming more space than it has been allotted. In
2716 * this situation, we must victimize our own cache, the MFU, for this insertion.
2719 arc_get_data_buf(arc_buf_t
*buf
)
2721 arc_state_t
*state
= buf
->b_hdr
->b_state
;
2722 uint64_t size
= buf
->b_hdr
->b_size
;
2723 arc_buf_contents_t type
= buf
->b_hdr
->b_type
;
2725 arc_adapt(size
, state
);
2728 * We have not yet reached cache maximum size,
2729 * just allocate a new buffer.
2731 if (!arc_evict_needed(type
)) {
2732 if (type
== ARC_BUFC_METADATA
) {
2733 buf
->b_data
= zio_buf_alloc(size
);
2734 arc_space_consume(size
, ARC_SPACE_DATA
);
2736 ASSERT(type
== ARC_BUFC_DATA
);
2737 buf
->b_data
= zio_data_buf_alloc(size
);
2738 ARCSTAT_INCR(arcstat_data_size
, size
);
2739 atomic_add_64(&arc_size
, size
);
2745 * If we are prefetching from the mfu ghost list, this buffer
2746 * will end up on the mru list; so steal space from there.
2748 if (state
== arc_mfu_ghost
)
2749 state
= buf
->b_hdr
->b_flags
& ARC_PREFETCH
? arc_mru
: arc_mfu
;
2750 else if (state
== arc_mru_ghost
)
2753 if (state
== arc_mru
|| state
== arc_anon
) {
2754 uint64_t mru_used
= arc_anon
->arcs_size
+ arc_mru
->arcs_size
;
2755 state
= (arc_mfu
->arcs_lsize
[type
] >= size
&&
2756 arc_p
> mru_used
) ? arc_mfu
: arc_mru
;
2759 uint64_t mfu_space
= arc_c
- arc_p
;
2760 state
= (arc_mru
->arcs_lsize
[type
] >= size
&&
2761 mfu_space
> arc_mfu
->arcs_size
) ? arc_mru
: arc_mfu
;
2764 if ((buf
->b_data
= arc_evict(state
, 0, size
, TRUE
, type
)) == NULL
) {
2765 if (type
== ARC_BUFC_METADATA
) {
2766 buf
->b_data
= zio_buf_alloc(size
);
2767 arc_space_consume(size
, ARC_SPACE_DATA
);
2770 * If we are unable to recycle an existing meta buffer
2771 * signal the reclaim thread. It will notify users
2772 * via the prune callback to drop references. The
2773 * prune callback in run in the context of the reclaim
2774 * thread to avoid deadlocking on the hash_lock.
2776 cv_signal(&arc_reclaim_thr_cv
);
2778 ASSERT(type
== ARC_BUFC_DATA
);
2779 buf
->b_data
= zio_data_buf_alloc(size
);
2780 ARCSTAT_INCR(arcstat_data_size
, size
);
2781 atomic_add_64(&arc_size
, size
);
2784 ARCSTAT_BUMP(arcstat_recycle_miss
);
2786 ASSERT(buf
->b_data
!= NULL
);
2789 * Update the state size. Note that ghost states have a
2790 * "ghost size" and so don't need to be updated.
2792 if (!GHOST_STATE(buf
->b_hdr
->b_state
)) {
2793 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2795 atomic_add_64(&hdr
->b_state
->arcs_size
, size
);
2796 if (list_link_active(&hdr
->b_arc_node
)) {
2797 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
2798 atomic_add_64(&hdr
->b_state
->arcs_lsize
[type
], size
);
2801 * If we are growing the cache, and we are adding anonymous
2802 * data, and we have outgrown arc_p, update arc_p
2804 if (!zfs_arc_p_aggressive_disable
&&
2805 arc_size
< arc_c
&& hdr
->b_state
== arc_anon
&&
2806 arc_anon
->arcs_size
+ arc_mru
->arcs_size
> arc_p
)
2807 arc_p
= MIN(arc_c
, arc_p
+ size
);
2812 * This routine is called whenever a buffer is accessed.
2813 * NOTE: the hash lock is dropped in this function.
2816 arc_access(arc_buf_hdr_t
*buf
, kmutex_t
*hash_lock
)
2820 ASSERT(MUTEX_HELD(hash_lock
));
2822 if (buf
->b_state
== arc_anon
) {
2824 * This buffer is not in the cache, and does not
2825 * appear in our "ghost" list. Add the new buffer
2829 ASSERT(buf
->b_arc_access
== 0);
2830 buf
->b_arc_access
= ddi_get_lbolt();
2831 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, buf
);
2832 arc_change_state(arc_mru
, buf
, hash_lock
);
2834 } else if (buf
->b_state
== arc_mru
) {
2835 now
= ddi_get_lbolt();
2838 * If this buffer is here because of a prefetch, then either:
2839 * - clear the flag if this is a "referencing" read
2840 * (any subsequent access will bump this into the MFU state).
2842 * - move the buffer to the head of the list if this is
2843 * another prefetch (to make it less likely to be evicted).
2845 if ((buf
->b_flags
& ARC_PREFETCH
) != 0) {
2846 if (refcount_count(&buf
->b_refcnt
) == 0) {
2847 ASSERT(list_link_active(&buf
->b_arc_node
));
2849 buf
->b_flags
&= ~ARC_PREFETCH
;
2850 atomic_inc_32(&buf
->b_mru_hits
);
2851 ARCSTAT_BUMP(arcstat_mru_hits
);
2853 buf
->b_arc_access
= now
;
2858 * This buffer has been "accessed" only once so far,
2859 * but it is still in the cache. Move it to the MFU
2862 if (now
> buf
->b_arc_access
+ ARC_MINTIME
) {
2864 * More than 125ms have passed since we
2865 * instantiated this buffer. Move it to the
2866 * most frequently used state.
2868 buf
->b_arc_access
= now
;
2869 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2870 arc_change_state(arc_mfu
, buf
, hash_lock
);
2872 atomic_inc_32(&buf
->b_mru_hits
);
2873 ARCSTAT_BUMP(arcstat_mru_hits
);
2874 } else if (buf
->b_state
== arc_mru_ghost
) {
2875 arc_state_t
*new_state
;
2877 * This buffer has been "accessed" recently, but
2878 * was evicted from the cache. Move it to the
2882 if (buf
->b_flags
& ARC_PREFETCH
) {
2883 new_state
= arc_mru
;
2884 if (refcount_count(&buf
->b_refcnt
) > 0)
2885 buf
->b_flags
&= ~ARC_PREFETCH
;
2886 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, buf
);
2888 new_state
= arc_mfu
;
2889 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2892 buf
->b_arc_access
= ddi_get_lbolt();
2893 arc_change_state(new_state
, buf
, hash_lock
);
2895 atomic_inc_32(&buf
->b_mru_ghost_hits
);
2896 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
2897 } else if (buf
->b_state
== arc_mfu
) {
2899 * This buffer has been accessed more than once and is
2900 * still in the cache. Keep it in the MFU state.
2902 * NOTE: an add_reference() that occurred when we did
2903 * the arc_read() will have kicked this off the list.
2904 * If it was a prefetch, we will explicitly move it to
2905 * the head of the list now.
2907 if ((buf
->b_flags
& ARC_PREFETCH
) != 0) {
2908 ASSERT(refcount_count(&buf
->b_refcnt
) == 0);
2909 ASSERT(list_link_active(&buf
->b_arc_node
));
2911 atomic_inc_32(&buf
->b_mfu_hits
);
2912 ARCSTAT_BUMP(arcstat_mfu_hits
);
2913 buf
->b_arc_access
= ddi_get_lbolt();
2914 } else if (buf
->b_state
== arc_mfu_ghost
) {
2915 arc_state_t
*new_state
= arc_mfu
;
2917 * This buffer has been accessed more than once but has
2918 * been evicted from the cache. Move it back to the
2922 if (buf
->b_flags
& ARC_PREFETCH
) {
2924 * This is a prefetch access...
2925 * move this block back to the MRU state.
2927 ASSERT0(refcount_count(&buf
->b_refcnt
));
2928 new_state
= arc_mru
;
2931 buf
->b_arc_access
= ddi_get_lbolt();
2932 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2933 arc_change_state(new_state
, buf
, hash_lock
);
2935 atomic_inc_32(&buf
->b_mfu_ghost_hits
);
2936 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
2937 } else if (buf
->b_state
== arc_l2c_only
) {
2939 * This buffer is on the 2nd Level ARC.
2942 buf
->b_arc_access
= ddi_get_lbolt();
2943 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2944 arc_change_state(arc_mfu
, buf
, hash_lock
);
2946 ASSERT(!"invalid arc state");
2950 /* a generic arc_done_func_t which you can use */
2953 arc_bcopy_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2955 if (zio
== NULL
|| zio
->io_error
== 0)
2956 bcopy(buf
->b_data
, arg
, buf
->b_hdr
->b_size
);
2957 VERIFY(arc_buf_remove_ref(buf
, arg
));
2960 /* a generic arc_done_func_t */
2962 arc_getbuf_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2964 arc_buf_t
**bufp
= arg
;
2965 if (zio
&& zio
->io_error
) {
2966 VERIFY(arc_buf_remove_ref(buf
, arg
));
2970 ASSERT(buf
->b_data
);
2975 arc_read_done(zio_t
*zio
)
2977 arc_buf_hdr_t
*hdr
, *found
;
2979 arc_buf_t
*abuf
; /* buffer we're assigning to callback */
2980 kmutex_t
*hash_lock
;
2981 arc_callback_t
*callback_list
, *acb
;
2982 int freeable
= FALSE
;
2984 buf
= zio
->io_private
;
2988 * The hdr was inserted into hash-table and removed from lists
2989 * prior to starting I/O. We should find this header, since
2990 * it's in the hash table, and it should be legit since it's
2991 * not possible to evict it during the I/O. The only possible
2992 * reason for it not to be found is if we were freed during the
2995 found
= buf_hash_find(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
,
2998 ASSERT((found
== NULL
&& HDR_FREED_IN_READ(hdr
) && hash_lock
== NULL
) ||
2999 (found
== hdr
&& DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
3000 (found
== hdr
&& HDR_L2_READING(hdr
)));
3002 hdr
->b_flags
&= ~ARC_L2_EVICTED
;
3003 if (l2arc_noprefetch
&& (hdr
->b_flags
& ARC_PREFETCH
))
3004 hdr
->b_flags
&= ~ARC_L2CACHE
;
3006 /* byteswap if necessary */
3007 callback_list
= hdr
->b_acb
;
3008 ASSERT(callback_list
!= NULL
);
3009 if (BP_SHOULD_BYTESWAP(zio
->io_bp
) && zio
->io_error
== 0) {
3010 dmu_object_byteswap_t bswap
=
3011 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
3012 if (BP_GET_LEVEL(zio
->io_bp
) > 0)
3013 byteswap_uint64_array(buf
->b_data
, hdr
->b_size
);
3015 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, hdr
->b_size
);
3018 arc_cksum_compute(buf
, B_FALSE
);
3021 if (hash_lock
&& zio
->io_error
== 0 && hdr
->b_state
== arc_anon
) {
3023 * Only call arc_access on anonymous buffers. This is because
3024 * if we've issued an I/O for an evicted buffer, we've already
3025 * called arc_access (to prevent any simultaneous readers from
3026 * getting confused).
3028 arc_access(hdr
, hash_lock
);
3031 /* create copies of the data buffer for the callers */
3033 for (acb
= callback_list
; acb
; acb
= acb
->acb_next
) {
3034 if (acb
->acb_done
) {
3036 ARCSTAT_BUMP(arcstat_duplicate_reads
);
3037 abuf
= arc_buf_clone(buf
);
3039 acb
->acb_buf
= abuf
;
3044 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3045 ASSERT(!HDR_BUF_AVAILABLE(hdr
));
3047 ASSERT(buf
->b_efunc
== NULL
);
3048 ASSERT(hdr
->b_datacnt
== 1);
3049 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
3052 ASSERT(refcount_is_zero(&hdr
->b_refcnt
) || callback_list
!= NULL
);
3054 if (zio
->io_error
!= 0) {
3055 hdr
->b_flags
|= ARC_IO_ERROR
;
3056 if (hdr
->b_state
!= arc_anon
)
3057 arc_change_state(arc_anon
, hdr
, hash_lock
);
3058 if (HDR_IN_HASH_TABLE(hdr
))
3059 buf_hash_remove(hdr
);
3060 freeable
= refcount_is_zero(&hdr
->b_refcnt
);
3064 * Broadcast before we drop the hash_lock to avoid the possibility
3065 * that the hdr (and hence the cv) might be freed before we get to
3066 * the cv_broadcast().
3068 cv_broadcast(&hdr
->b_cv
);
3071 mutex_exit(hash_lock
);
3074 * This block was freed while we waited for the read to
3075 * complete. It has been removed from the hash table and
3076 * moved to the anonymous state (so that it won't show up
3079 ASSERT3P(hdr
->b_state
, ==, arc_anon
);
3080 freeable
= refcount_is_zero(&hdr
->b_refcnt
);
3083 /* execute each callback and free its structure */
3084 while ((acb
= callback_list
) != NULL
) {
3086 acb
->acb_done(zio
, acb
->acb_buf
, acb
->acb_private
);
3088 if (acb
->acb_zio_dummy
!= NULL
) {
3089 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
3090 zio_nowait(acb
->acb_zio_dummy
);
3093 callback_list
= acb
->acb_next
;
3094 kmem_free(acb
, sizeof (arc_callback_t
));
3098 arc_hdr_destroy(hdr
);
3102 * "Read" the block at the specified DVA (in bp) via the
3103 * cache. If the block is found in the cache, invoke the provided
3104 * callback immediately and return. Note that the `zio' parameter
3105 * in the callback will be NULL in this case, since no IO was
3106 * required. If the block is not in the cache pass the read request
3107 * on to the spa with a substitute callback function, so that the
3108 * requested block will be added to the cache.
3110 * If a read request arrives for a block that has a read in-progress,
3111 * either wait for the in-progress read to complete (and return the
3112 * results); or, if this is a read with a "done" func, add a record
3113 * to the read to invoke the "done" func when the read completes,
3114 * and return; or just return.
3116 * arc_read_done() will invoke all the requested "done" functions
3117 * for readers of this block.
3120 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
, arc_done_func_t
*done
,
3121 void *private, zio_priority_t priority
, int zio_flags
, uint32_t *arc_flags
,
3122 const zbookmark_t
*zb
)
3125 arc_buf_t
*buf
= NULL
;
3126 kmutex_t
*hash_lock
;
3128 uint64_t guid
= spa_load_guid(spa
);
3132 hdr
= buf_hash_find(guid
, BP_IDENTITY(bp
), BP_PHYSICAL_BIRTH(bp
),
3134 if (hdr
&& hdr
->b_datacnt
> 0) {
3136 *arc_flags
|= ARC_CACHED
;
3138 if (HDR_IO_IN_PROGRESS(hdr
)) {
3140 if (*arc_flags
& ARC_WAIT
) {
3141 cv_wait(&hdr
->b_cv
, hash_lock
);
3142 mutex_exit(hash_lock
);
3145 ASSERT(*arc_flags
& ARC_NOWAIT
);
3148 arc_callback_t
*acb
= NULL
;
3150 acb
= kmem_zalloc(sizeof (arc_callback_t
),
3152 acb
->acb_done
= done
;
3153 acb
->acb_private
= private;
3155 acb
->acb_zio_dummy
= zio_null(pio
,
3156 spa
, NULL
, NULL
, NULL
, zio_flags
);
3158 ASSERT(acb
->acb_done
!= NULL
);
3159 acb
->acb_next
= hdr
->b_acb
;
3161 add_reference(hdr
, hash_lock
, private);
3162 mutex_exit(hash_lock
);
3165 mutex_exit(hash_lock
);
3169 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
3172 add_reference(hdr
, hash_lock
, private);
3174 * If this block is already in use, create a new
3175 * copy of the data so that we will be guaranteed
3176 * that arc_release() will always succeed.
3180 ASSERT(buf
->b_data
);
3181 if (HDR_BUF_AVAILABLE(hdr
)) {
3182 ASSERT(buf
->b_efunc
== NULL
);
3183 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
3185 buf
= arc_buf_clone(buf
);
3188 } else if (*arc_flags
& ARC_PREFETCH
&&
3189 refcount_count(&hdr
->b_refcnt
) == 0) {
3190 hdr
->b_flags
|= ARC_PREFETCH
;
3192 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
3193 arc_access(hdr
, hash_lock
);
3194 if (*arc_flags
& ARC_L2CACHE
)
3195 hdr
->b_flags
|= ARC_L2CACHE
;
3196 if (*arc_flags
& ARC_L2COMPRESS
)
3197 hdr
->b_flags
|= ARC_L2COMPRESS
;
3198 mutex_exit(hash_lock
);
3199 ARCSTAT_BUMP(arcstat_hits
);
3200 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
3201 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
3202 data
, metadata
, hits
);
3205 done(NULL
, buf
, private);
3207 uint64_t size
= BP_GET_LSIZE(bp
);
3208 arc_callback_t
*acb
;
3211 boolean_t devw
= B_FALSE
;
3214 /* this block is not in the cache */
3215 arc_buf_hdr_t
*exists
;
3216 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
3217 buf
= arc_buf_alloc(spa
, size
, private, type
);
3219 hdr
->b_dva
= *BP_IDENTITY(bp
);
3220 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
3221 hdr
->b_cksum0
= bp
->blk_cksum
.zc_word
[0];
3222 exists
= buf_hash_insert(hdr
, &hash_lock
);
3224 /* somebody beat us to the hash insert */
3225 mutex_exit(hash_lock
);
3226 buf_discard_identity(hdr
);
3227 (void) arc_buf_remove_ref(buf
, private);
3228 goto top
; /* restart the IO request */
3230 /* if this is a prefetch, we don't have a reference */
3231 if (*arc_flags
& ARC_PREFETCH
) {
3232 (void) remove_reference(hdr
, hash_lock
,
3234 hdr
->b_flags
|= ARC_PREFETCH
;
3236 if (*arc_flags
& ARC_L2CACHE
)
3237 hdr
->b_flags
|= ARC_L2CACHE
;
3238 if (*arc_flags
& ARC_L2COMPRESS
)
3239 hdr
->b_flags
|= ARC_L2COMPRESS
;
3240 if (BP_GET_LEVEL(bp
) > 0)
3241 hdr
->b_flags
|= ARC_INDIRECT
;
3243 /* this block is in the ghost cache */
3244 ASSERT(GHOST_STATE(hdr
->b_state
));
3245 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3246 ASSERT0(refcount_count(&hdr
->b_refcnt
));
3247 ASSERT(hdr
->b_buf
== NULL
);
3249 /* if this is a prefetch, we don't have a reference */
3250 if (*arc_flags
& ARC_PREFETCH
)
3251 hdr
->b_flags
|= ARC_PREFETCH
;
3253 add_reference(hdr
, hash_lock
, private);
3254 if (*arc_flags
& ARC_L2CACHE
)
3255 hdr
->b_flags
|= ARC_L2CACHE
;
3256 if (*arc_flags
& ARC_L2COMPRESS
)
3257 hdr
->b_flags
|= ARC_L2COMPRESS
;
3258 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
3261 buf
->b_efunc
= NULL
;
3262 buf
->b_private
= NULL
;
3265 ASSERT(hdr
->b_datacnt
== 0);
3267 arc_get_data_buf(buf
);
3268 arc_access(hdr
, hash_lock
);
3271 ASSERT(!GHOST_STATE(hdr
->b_state
));
3273 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_PUSHPAGE
);
3274 acb
->acb_done
= done
;
3275 acb
->acb_private
= private;
3277 ASSERT(hdr
->b_acb
== NULL
);
3279 hdr
->b_flags
|= ARC_IO_IN_PROGRESS
;
3281 if (HDR_L2CACHE(hdr
) && hdr
->b_l2hdr
!= NULL
&&
3282 (vd
= hdr
->b_l2hdr
->b_dev
->l2ad_vdev
) != NULL
) {
3283 devw
= hdr
->b_l2hdr
->b_dev
->l2ad_writing
;
3284 addr
= hdr
->b_l2hdr
->b_daddr
;
3286 * Lock out device removal.
3288 if (vdev_is_dead(vd
) ||
3289 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
3293 mutex_exit(hash_lock
);
3296 * At this point, we have a level 1 cache miss. Try again in
3297 * L2ARC if possible.
3299 ASSERT3U(hdr
->b_size
, ==, size
);
3300 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
, blkptr_t
*, bp
,
3301 uint64_t, size
, zbookmark_t
*, zb
);
3302 ARCSTAT_BUMP(arcstat_misses
);
3303 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
3304 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
3305 data
, metadata
, misses
);
3307 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
3309 * Read from the L2ARC if the following are true:
3310 * 1. The L2ARC vdev was previously cached.
3311 * 2. This buffer still has L2ARC metadata.
3312 * 3. This buffer isn't currently writing to the L2ARC.
3313 * 4. The L2ARC entry wasn't evicted, which may
3314 * also have invalidated the vdev.
3315 * 5. This isn't prefetch and l2arc_noprefetch is set.
3317 if (hdr
->b_l2hdr
!= NULL
&&
3318 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
3319 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
3320 l2arc_read_callback_t
*cb
;
3322 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
3323 ARCSTAT_BUMP(arcstat_l2_hits
);
3324 atomic_inc_32(&hdr
->b_l2hdr
->b_hits
);
3326 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
3328 cb
->l2rcb_buf
= buf
;
3329 cb
->l2rcb_spa
= spa
;
3332 cb
->l2rcb_flags
= zio_flags
;
3333 cb
->l2rcb_compress
= hdr
->b_l2hdr
->b_compress
;
3335 ASSERT(addr
>= VDEV_LABEL_START_SIZE
&&
3336 addr
+ size
< vd
->vdev_psize
-
3337 VDEV_LABEL_END_SIZE
);
3340 * l2arc read. The SCL_L2ARC lock will be
3341 * released by l2arc_read_done().
3342 * Issue a null zio if the underlying buffer
3343 * was squashed to zero size by compression.
3345 if (hdr
->b_l2hdr
->b_compress
==
3346 ZIO_COMPRESS_EMPTY
) {
3347 rzio
= zio_null(pio
, spa
, vd
,
3348 l2arc_read_done
, cb
,
3349 zio_flags
| ZIO_FLAG_DONT_CACHE
|
3351 ZIO_FLAG_DONT_PROPAGATE
|
3352 ZIO_FLAG_DONT_RETRY
);
3354 rzio
= zio_read_phys(pio
, vd
, addr
,
3355 hdr
->b_l2hdr
->b_asize
,
3356 buf
->b_data
, ZIO_CHECKSUM_OFF
,
3357 l2arc_read_done
, cb
, priority
,
3358 zio_flags
| ZIO_FLAG_DONT_CACHE
|
3360 ZIO_FLAG_DONT_PROPAGATE
|
3361 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
3363 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
3365 ARCSTAT_INCR(arcstat_l2_read_bytes
,
3366 hdr
->b_l2hdr
->b_asize
);
3368 if (*arc_flags
& ARC_NOWAIT
) {
3373 ASSERT(*arc_flags
& ARC_WAIT
);
3374 if (zio_wait(rzio
) == 0)
3377 /* l2arc read error; goto zio_read() */
3379 DTRACE_PROBE1(l2arc__miss
,
3380 arc_buf_hdr_t
*, hdr
);
3381 ARCSTAT_BUMP(arcstat_l2_misses
);
3382 if (HDR_L2_WRITING(hdr
))
3383 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
3384 spa_config_exit(spa
, SCL_L2ARC
, vd
);
3388 spa_config_exit(spa
, SCL_L2ARC
, vd
);
3389 if (l2arc_ndev
!= 0) {
3390 DTRACE_PROBE1(l2arc__miss
,
3391 arc_buf_hdr_t
*, hdr
);
3392 ARCSTAT_BUMP(arcstat_l2_misses
);
3396 rzio
= zio_read(pio
, spa
, bp
, buf
->b_data
, size
,
3397 arc_read_done
, buf
, priority
, zio_flags
, zb
);
3399 if (*arc_flags
& ARC_WAIT
) {
3400 rc
= zio_wait(rzio
);
3404 ASSERT(*arc_flags
& ARC_NOWAIT
);
3409 spa_read_history_add(spa
, zb
, *arc_flags
);
3414 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
3418 p
= kmem_alloc(sizeof (*p
), KM_SLEEP
);
3420 p
->p_private
= private;
3421 list_link_init(&p
->p_node
);
3422 refcount_create(&p
->p_refcnt
);
3424 mutex_enter(&arc_prune_mtx
);
3425 refcount_add(&p
->p_refcnt
, &arc_prune_list
);
3426 list_insert_head(&arc_prune_list
, p
);
3427 mutex_exit(&arc_prune_mtx
);
3433 arc_remove_prune_callback(arc_prune_t
*p
)
3435 mutex_enter(&arc_prune_mtx
);
3436 list_remove(&arc_prune_list
, p
);
3437 if (refcount_remove(&p
->p_refcnt
, &arc_prune_list
) == 0) {
3438 refcount_destroy(&p
->p_refcnt
);
3439 kmem_free(p
, sizeof (*p
));
3441 mutex_exit(&arc_prune_mtx
);
3445 arc_set_callback(arc_buf_t
*buf
, arc_evict_func_t
*func
, void *private)
3447 ASSERT(buf
->b_hdr
!= NULL
);
3448 ASSERT(buf
->b_hdr
->b_state
!= arc_anon
);
3449 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_refcnt
) || func
== NULL
);
3450 ASSERT(buf
->b_efunc
== NULL
);
3451 ASSERT(!HDR_BUF_AVAILABLE(buf
->b_hdr
));
3453 buf
->b_efunc
= func
;
3454 buf
->b_private
= private;
3458 * Notify the arc that a block was freed, and thus will never be used again.
3461 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
3464 kmutex_t
*hash_lock
;
3465 uint64_t guid
= spa_load_guid(spa
);
3467 hdr
= buf_hash_find(guid
, BP_IDENTITY(bp
), BP_PHYSICAL_BIRTH(bp
),
3471 if (HDR_BUF_AVAILABLE(hdr
)) {
3472 arc_buf_t
*buf
= hdr
->b_buf
;
3473 add_reference(hdr
, hash_lock
, FTAG
);
3474 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
3475 mutex_exit(hash_lock
);
3477 arc_release(buf
, FTAG
);
3478 (void) arc_buf_remove_ref(buf
, FTAG
);
3480 mutex_exit(hash_lock
);
3486 * This is used by the DMU to let the ARC know that a buffer is
3487 * being evicted, so the ARC should clean up. If this arc buf
3488 * is not yet in the evicted state, it will be put there.
3491 arc_buf_evict(arc_buf_t
*buf
)
3494 kmutex_t
*hash_lock
;
3497 mutex_enter(&buf
->b_evict_lock
);
3501 * We are in arc_do_user_evicts().
3503 ASSERT(buf
->b_data
== NULL
);
3504 mutex_exit(&buf
->b_evict_lock
);
3506 } else if (buf
->b_data
== NULL
) {
3507 arc_buf_t copy
= *buf
; /* structure assignment */
3509 * We are on the eviction list; process this buffer now
3510 * but let arc_do_user_evicts() do the reaping.
3512 buf
->b_efunc
= NULL
;
3513 mutex_exit(&buf
->b_evict_lock
);
3514 VERIFY(copy
.b_efunc(©
) == 0);
3517 hash_lock
= HDR_LOCK(hdr
);
3518 mutex_enter(hash_lock
);
3520 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3522 ASSERT3U(refcount_count(&hdr
->b_refcnt
), <, hdr
->b_datacnt
);
3523 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
3526 * Pull this buffer off of the hdr
3529 while (*bufp
!= buf
)
3530 bufp
= &(*bufp
)->b_next
;
3531 *bufp
= buf
->b_next
;
3533 ASSERT(buf
->b_data
!= NULL
);
3534 arc_buf_destroy(buf
, FALSE
, FALSE
);
3536 if (hdr
->b_datacnt
== 0) {
3537 arc_state_t
*old_state
= hdr
->b_state
;
3538 arc_state_t
*evicted_state
;
3540 ASSERT(hdr
->b_buf
== NULL
);
3541 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
3544 (old_state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
3546 mutex_enter(&old_state
->arcs_mtx
);
3547 mutex_enter(&evicted_state
->arcs_mtx
);
3549 arc_change_state(evicted_state
, hdr
, hash_lock
);
3550 ASSERT(HDR_IN_HASH_TABLE(hdr
));
3551 hdr
->b_flags
|= ARC_IN_HASH_TABLE
;
3552 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
3554 mutex_exit(&evicted_state
->arcs_mtx
);
3555 mutex_exit(&old_state
->arcs_mtx
);
3557 mutex_exit(hash_lock
);
3558 mutex_exit(&buf
->b_evict_lock
);
3560 VERIFY(buf
->b_efunc(buf
) == 0);
3561 buf
->b_efunc
= NULL
;
3562 buf
->b_private
= NULL
;
3565 kmem_cache_free(buf_cache
, buf
);
3570 * Release this buffer from the cache, making it an anonymous buffer. This
3571 * must be done after a read and prior to modifying the buffer contents.
3572 * If the buffer has more than one reference, we must make
3573 * a new hdr for the buffer.
3576 arc_release(arc_buf_t
*buf
, void *tag
)
3579 kmutex_t
*hash_lock
= NULL
;
3580 l2arc_buf_hdr_t
*l2hdr
;
3581 uint64_t buf_size
= 0;
3584 * It would be nice to assert that if it's DMU metadata (level >
3585 * 0 || it's the dnode file), then it must be syncing context.
3586 * But we don't know that information at this level.
3589 mutex_enter(&buf
->b_evict_lock
);
3592 /* this buffer is not on any list */
3593 ASSERT(refcount_count(&hdr
->b_refcnt
) > 0);
3595 if (hdr
->b_state
== arc_anon
) {
3596 /* this buffer is already released */
3597 ASSERT(buf
->b_efunc
== NULL
);
3599 hash_lock
= HDR_LOCK(hdr
);
3600 mutex_enter(hash_lock
);
3602 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3605 l2hdr
= hdr
->b_l2hdr
;
3607 mutex_enter(&l2arc_buflist_mtx
);
3608 hdr
->b_l2hdr
= NULL
;
3610 buf_size
= hdr
->b_size
;
3613 * Do we have more than one buf?
3615 if (hdr
->b_datacnt
> 1) {
3616 arc_buf_hdr_t
*nhdr
;
3618 uint64_t blksz
= hdr
->b_size
;
3619 uint64_t spa
= hdr
->b_spa
;
3620 arc_buf_contents_t type
= hdr
->b_type
;
3621 uint32_t flags
= hdr
->b_flags
;
3623 ASSERT(hdr
->b_buf
!= buf
|| buf
->b_next
!= NULL
);
3625 * Pull the data off of this hdr and attach it to
3626 * a new anonymous hdr.
3628 (void) remove_reference(hdr
, hash_lock
, tag
);
3630 while (*bufp
!= buf
)
3631 bufp
= &(*bufp
)->b_next
;
3632 *bufp
= buf
->b_next
;
3635 ASSERT3U(hdr
->b_state
->arcs_size
, >=, hdr
->b_size
);
3636 atomic_add_64(&hdr
->b_state
->arcs_size
, -hdr
->b_size
);
3637 if (refcount_is_zero(&hdr
->b_refcnt
)) {
3638 uint64_t *size
= &hdr
->b_state
->arcs_lsize
[hdr
->b_type
];
3639 ASSERT3U(*size
, >=, hdr
->b_size
);
3640 atomic_add_64(size
, -hdr
->b_size
);
3644 * We're releasing a duplicate user data buffer, update
3645 * our statistics accordingly.
3647 if (hdr
->b_type
== ARC_BUFC_DATA
) {
3648 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers
);
3649 ARCSTAT_INCR(arcstat_duplicate_buffers_size
,
3652 hdr
->b_datacnt
-= 1;
3653 arc_cksum_verify(buf
);
3654 arc_buf_unwatch(buf
);
3656 mutex_exit(hash_lock
);
3658 nhdr
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
3659 nhdr
->b_size
= blksz
;
3661 nhdr
->b_type
= type
;
3663 nhdr
->b_state
= arc_anon
;
3664 nhdr
->b_arc_access
= 0;
3665 nhdr
->b_mru_hits
= 0;
3666 nhdr
->b_mru_ghost_hits
= 0;
3667 nhdr
->b_mfu_hits
= 0;
3668 nhdr
->b_mfu_ghost_hits
= 0;
3669 nhdr
->b_l2_hits
= 0;
3670 nhdr
->b_flags
= flags
& ARC_L2_WRITING
;
3671 nhdr
->b_l2hdr
= NULL
;
3672 nhdr
->b_datacnt
= 1;
3673 nhdr
->b_freeze_cksum
= NULL
;
3674 (void) refcount_add(&nhdr
->b_refcnt
, tag
);
3676 mutex_exit(&buf
->b_evict_lock
);
3677 atomic_add_64(&arc_anon
->arcs_size
, blksz
);
3679 mutex_exit(&buf
->b_evict_lock
);
3680 ASSERT(refcount_count(&hdr
->b_refcnt
) == 1);
3681 ASSERT(!list_link_active(&hdr
->b_arc_node
));
3682 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3683 if (hdr
->b_state
!= arc_anon
)
3684 arc_change_state(arc_anon
, hdr
, hash_lock
);
3685 hdr
->b_arc_access
= 0;
3686 hdr
->b_mru_hits
= 0;
3687 hdr
->b_mru_ghost_hits
= 0;
3688 hdr
->b_mfu_hits
= 0;
3689 hdr
->b_mfu_ghost_hits
= 0;
3692 mutex_exit(hash_lock
);
3694 buf_discard_identity(hdr
);
3697 buf
->b_efunc
= NULL
;
3698 buf
->b_private
= NULL
;
3701 ARCSTAT_INCR(arcstat_l2_asize
, -l2hdr
->b_asize
);
3702 list_remove(l2hdr
->b_dev
->l2ad_buflist
, hdr
);
3703 kmem_cache_free(l2arc_hdr_cache
, l2hdr
);
3704 arc_space_return(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
3705 ARCSTAT_INCR(arcstat_l2_size
, -buf_size
);
3706 mutex_exit(&l2arc_buflist_mtx
);
3711 arc_released(arc_buf_t
*buf
)
3715 mutex_enter(&buf
->b_evict_lock
);
3716 released
= (buf
->b_data
!= NULL
&& buf
->b_hdr
->b_state
== arc_anon
);
3717 mutex_exit(&buf
->b_evict_lock
);
3722 arc_has_callback(arc_buf_t
*buf
)
3726 mutex_enter(&buf
->b_evict_lock
);
3727 callback
= (buf
->b_efunc
!= NULL
);
3728 mutex_exit(&buf
->b_evict_lock
);
3734 arc_referenced(arc_buf_t
*buf
)
3738 mutex_enter(&buf
->b_evict_lock
);
3739 referenced
= (refcount_count(&buf
->b_hdr
->b_refcnt
));
3740 mutex_exit(&buf
->b_evict_lock
);
3741 return (referenced
);
3746 arc_write_ready(zio_t
*zio
)
3748 arc_write_callback_t
*callback
= zio
->io_private
;
3749 arc_buf_t
*buf
= callback
->awcb_buf
;
3750 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3752 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_refcnt
));
3753 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
3756 * If the IO is already in progress, then this is a re-write
3757 * attempt, so we need to thaw and re-compute the cksum.
3758 * It is the responsibility of the callback to handle the
3759 * accounting for any re-write attempt.
3761 if (HDR_IO_IN_PROGRESS(hdr
)) {
3762 mutex_enter(&hdr
->b_freeze_lock
);
3763 if (hdr
->b_freeze_cksum
!= NULL
) {
3764 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
3765 hdr
->b_freeze_cksum
= NULL
;
3767 mutex_exit(&hdr
->b_freeze_lock
);
3769 arc_cksum_compute(buf
, B_FALSE
);
3770 hdr
->b_flags
|= ARC_IO_IN_PROGRESS
;
3774 * The SPA calls this callback for each physical write that happens on behalf
3775 * of a logical write. See the comment in dbuf_write_physdone() for details.
3778 arc_write_physdone(zio_t
*zio
)
3780 arc_write_callback_t
*cb
= zio
->io_private
;
3781 if (cb
->awcb_physdone
!= NULL
)
3782 cb
->awcb_physdone(zio
, cb
->awcb_buf
, cb
->awcb_private
);
3786 arc_write_done(zio_t
*zio
)
3788 arc_write_callback_t
*callback
= zio
->io_private
;
3789 arc_buf_t
*buf
= callback
->awcb_buf
;
3790 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3792 ASSERT(hdr
->b_acb
== NULL
);
3794 if (zio
->io_error
== 0) {
3795 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
3796 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
3797 hdr
->b_cksum0
= zio
->io_bp
->blk_cksum
.zc_word
[0];
3799 ASSERT(BUF_EMPTY(hdr
));
3803 * If the block to be written was all-zero, we may have
3804 * compressed it away. In this case no write was performed
3805 * so there will be no dva/birth/checksum. The buffer must
3806 * therefore remain anonymous (and uncached).
3808 if (!BUF_EMPTY(hdr
)) {
3809 arc_buf_hdr_t
*exists
;
3810 kmutex_t
*hash_lock
;
3812 ASSERT(zio
->io_error
== 0);
3814 arc_cksum_verify(buf
);
3816 exists
= buf_hash_insert(hdr
, &hash_lock
);
3819 * This can only happen if we overwrite for
3820 * sync-to-convergence, because we remove
3821 * buffers from the hash table when we arc_free().
3823 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
3824 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
3825 panic("bad overwrite, hdr=%p exists=%p",
3826 (void *)hdr
, (void *)exists
);
3827 ASSERT(refcount_is_zero(&exists
->b_refcnt
));
3828 arc_change_state(arc_anon
, exists
, hash_lock
);
3829 mutex_exit(hash_lock
);
3830 arc_hdr_destroy(exists
);
3831 exists
= buf_hash_insert(hdr
, &hash_lock
);
3832 ASSERT3P(exists
, ==, NULL
);
3833 } else if (zio
->io_flags
& ZIO_FLAG_NOPWRITE
) {
3835 ASSERT(zio
->io_prop
.zp_nopwrite
);
3836 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
3837 panic("bad nopwrite, hdr=%p exists=%p",
3838 (void *)hdr
, (void *)exists
);
3841 ASSERT(hdr
->b_datacnt
== 1);
3842 ASSERT(hdr
->b_state
== arc_anon
);
3843 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
3844 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
3847 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3848 /* if it's not anon, we are doing a scrub */
3849 if (!exists
&& hdr
->b_state
== arc_anon
)
3850 arc_access(hdr
, hash_lock
);
3851 mutex_exit(hash_lock
);
3853 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3856 ASSERT(!refcount_is_zero(&hdr
->b_refcnt
));
3857 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
3859 kmem_free(callback
, sizeof (arc_write_callback_t
));
3863 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
3864 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
, boolean_t l2arc_compress
,
3865 const zio_prop_t
*zp
, arc_done_func_t
*ready
, arc_done_func_t
*physdone
,
3866 arc_done_func_t
*done
, void *private, zio_priority_t priority
,
3867 int zio_flags
, const zbookmark_t
*zb
)
3869 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3870 arc_write_callback_t
*callback
;
3873 ASSERT(ready
!= NULL
);
3874 ASSERT(done
!= NULL
);
3875 ASSERT(!HDR_IO_ERROR(hdr
));
3876 ASSERT((hdr
->b_flags
& ARC_IO_IN_PROGRESS
) == 0);
3877 ASSERT(hdr
->b_acb
== NULL
);
3879 hdr
->b_flags
|= ARC_L2CACHE
;
3881 hdr
->b_flags
|= ARC_L2COMPRESS
;
3882 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_PUSHPAGE
);
3883 callback
->awcb_ready
= ready
;
3884 callback
->awcb_physdone
= physdone
;
3885 callback
->awcb_done
= done
;
3886 callback
->awcb_private
= private;
3887 callback
->awcb_buf
= buf
;
3889 zio
= zio_write(pio
, spa
, txg
, bp
, buf
->b_data
, hdr
->b_size
, zp
,
3890 arc_write_ready
, arc_write_physdone
, arc_write_done
, callback
,
3891 priority
, zio_flags
, zb
);
3897 arc_memory_throttle(uint64_t reserve
, uint64_t txg
)
3900 if (zfs_arc_memory_throttle_disable
)
3903 if (freemem
<= physmem
* arc_lotsfree_percent
/ 100) {
3904 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
3905 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
3906 return (SET_ERROR(EAGAIN
));
3913 arc_tempreserve_clear(uint64_t reserve
)
3915 atomic_add_64(&arc_tempreserve
, -reserve
);
3916 ASSERT((int64_t)arc_tempreserve
>= 0);
3920 arc_tempreserve_space(uint64_t reserve
, uint64_t txg
)
3925 if (reserve
> arc_c
/4 && !arc_no_grow
)
3926 arc_c
= MIN(arc_c_max
, reserve
* 4);
3927 if (reserve
> arc_c
) {
3928 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
3929 return (SET_ERROR(ENOMEM
));
3933 * Don't count loaned bufs as in flight dirty data to prevent long
3934 * network delays from blocking transactions that are ready to be
3935 * assigned to a txg.
3937 anon_size
= MAX((int64_t)(arc_anon
->arcs_size
- arc_loaned_bytes
), 0);
3940 * Writes will, almost always, require additional memory allocations
3941 * in order to compress/encrypt/etc the data. We therefore need to
3942 * make sure that there is sufficient available memory for this.
3944 error
= arc_memory_throttle(reserve
, txg
);
3949 * Throttle writes when the amount of dirty data in the cache
3950 * gets too large. We try to keep the cache less than half full
3951 * of dirty blocks so that our sync times don't grow too large.
3952 * Note: if two requests come in concurrently, we might let them
3953 * both succeed, when one of them should fail. Not a huge deal.
3956 if (reserve
+ arc_tempreserve
+ anon_size
> arc_c
/ 2 &&
3957 anon_size
> arc_c
/ 4) {
3958 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3959 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3960 arc_tempreserve
>>10,
3961 arc_anon
->arcs_lsize
[ARC_BUFC_METADATA
]>>10,
3962 arc_anon
->arcs_lsize
[ARC_BUFC_DATA
]>>10,
3963 reserve
>>10, arc_c
>>10);
3964 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
3965 return (SET_ERROR(ERESTART
));
3967 atomic_add_64(&arc_tempreserve
, reserve
);
3972 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
3973 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
3975 size
->value
.ui64
= state
->arcs_size
;
3976 evict_data
->value
.ui64
= state
->arcs_lsize
[ARC_BUFC_DATA
];
3977 evict_metadata
->value
.ui64
= state
->arcs_lsize
[ARC_BUFC_METADATA
];
3981 arc_kstat_update(kstat_t
*ksp
, int rw
)
3983 arc_stats_t
*as
= ksp
->ks_data
;
3985 if (rw
== KSTAT_WRITE
) {
3986 return (SET_ERROR(EACCES
));
3988 arc_kstat_update_state(arc_anon
,
3989 &as
->arcstat_anon_size
,
3990 &as
->arcstat_anon_evict_data
,
3991 &as
->arcstat_anon_evict_metadata
);
3992 arc_kstat_update_state(arc_mru
,
3993 &as
->arcstat_mru_size
,
3994 &as
->arcstat_mru_evict_data
,
3995 &as
->arcstat_mru_evict_metadata
);
3996 arc_kstat_update_state(arc_mru_ghost
,
3997 &as
->arcstat_mru_ghost_size
,
3998 &as
->arcstat_mru_ghost_evict_data
,
3999 &as
->arcstat_mru_ghost_evict_metadata
);
4000 arc_kstat_update_state(arc_mfu
,
4001 &as
->arcstat_mfu_size
,
4002 &as
->arcstat_mfu_evict_data
,
4003 &as
->arcstat_mfu_evict_metadata
);
4004 arc_kstat_update_state(arc_mfu_ghost
,
4005 &as
->arcstat_mfu_ghost_size
,
4006 &as
->arcstat_mfu_ghost_evict_data
,
4007 &as
->arcstat_mfu_ghost_evict_metadata
);
4016 mutex_init(&arc_reclaim_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
4017 cv_init(&arc_reclaim_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
4019 /* Convert seconds to clock ticks */
4020 zfs_arc_min_prefetch_lifespan
= 1 * hz
;
4022 /* Start out with 1/8 of all memory */
4023 arc_c
= physmem
* PAGESIZE
/ 8;
4027 * On architectures where the physical memory can be larger
4028 * than the addressable space (intel in 32-bit mode), we may
4029 * need to limit the cache to 1/8 of VM size.
4031 arc_c
= MIN(arc_c
, vmem_size(heap_arena
, VMEM_ALLOC
| VMEM_FREE
) / 8);
4033 * Register a shrinker to support synchronous (direct) memory
4034 * reclaim from the arc. This is done to prevent kswapd from
4035 * swapping out pages when it is preferable to shrink the arc.
4037 spl_register_shrinker(&arc_shrinker
);
4040 /* set min cache to zero */
4042 /* set max to 1/2 of all memory */
4043 arc_c_max
= arc_c
* 4;
4046 * Allow the tunables to override our calculations if they are
4047 * reasonable (ie. over 64MB)
4049 if (zfs_arc_max
> 64<<20 && zfs_arc_max
< physmem
* PAGESIZE
)
4050 arc_c_max
= zfs_arc_max
;
4051 if (zfs_arc_min
> 0 && zfs_arc_min
<= arc_c_max
)
4052 arc_c_min
= zfs_arc_min
;
4055 arc_p
= (arc_c
>> 1);
4057 /* limit meta-data to 1/4 of the arc capacity */
4058 arc_meta_limit
= arc_c_max
/ 4;
4061 /* Allow the tunable to override if it is reasonable */
4062 if (zfs_arc_meta_limit
> 0 && zfs_arc_meta_limit
<= arc_c_max
)
4063 arc_meta_limit
= zfs_arc_meta_limit
;
4065 /* if kmem_flags are set, lets try to use less memory */
4066 if (kmem_debugging())
4068 if (arc_c
< arc_c_min
)
4071 arc_anon
= &ARC_anon
;
4073 arc_mru_ghost
= &ARC_mru_ghost
;
4075 arc_mfu_ghost
= &ARC_mfu_ghost
;
4076 arc_l2c_only
= &ARC_l2c_only
;
4079 mutex_init(&arc_anon
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4080 mutex_init(&arc_mru
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4081 mutex_init(&arc_mru_ghost
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4082 mutex_init(&arc_mfu
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4083 mutex_init(&arc_mfu_ghost
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4084 mutex_init(&arc_l2c_only
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4086 list_create(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
4087 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4088 list_create(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
4089 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4090 list_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
4091 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4092 list_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
4093 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4094 list_create(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
4095 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4096 list_create(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
4097 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4098 list_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
4099 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4100 list_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
4101 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4102 list_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
4103 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4104 list_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
4105 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
4107 arc_anon
->arcs_state
= ARC_STATE_ANON
;
4108 arc_mru
->arcs_state
= ARC_STATE_MRU
;
4109 arc_mru_ghost
->arcs_state
= ARC_STATE_MRU_GHOST
;
4110 arc_mfu
->arcs_state
= ARC_STATE_MFU
;
4111 arc_mfu_ghost
->arcs_state
= ARC_STATE_MFU_GHOST
;
4112 arc_l2c_only
->arcs_state
= ARC_STATE_L2C_ONLY
;
4116 arc_thread_exit
= 0;
4117 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
4118 offsetof(arc_prune_t
, p_node
));
4119 arc_eviction_list
= NULL
;
4120 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4121 mutex_init(&arc_eviction_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4122 bzero(&arc_eviction_hdr
, sizeof (arc_buf_hdr_t
));
4124 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
4125 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
4127 if (arc_ksp
!= NULL
) {
4128 arc_ksp
->ks_data
= &arc_stats
;
4129 arc_ksp
->ks_update
= arc_kstat_update
;
4130 kstat_install(arc_ksp
);
4133 (void) thread_create(NULL
, 0, arc_adapt_thread
, NULL
, 0, &p0
,
4134 TS_RUN
, minclsyspri
);
4140 * Calculate maximum amount of dirty data per pool.
4142 * If it has been set by a module parameter, take that.
4143 * Otherwise, use a percentage of physical memory defined by
4144 * zfs_dirty_data_max_percent (default 10%) with a cap at
4145 * zfs_dirty_data_max_max (default 25% of physical memory).
4147 if (zfs_dirty_data_max_max
== 0)
4148 zfs_dirty_data_max_max
= physmem
* PAGESIZE
*
4149 zfs_dirty_data_max_max_percent
/ 100;
4151 if (zfs_dirty_data_max
== 0) {
4152 zfs_dirty_data_max
= physmem
* PAGESIZE
*
4153 zfs_dirty_data_max_percent
/ 100;
4154 zfs_dirty_data_max
= MIN(zfs_dirty_data_max
,
4155 zfs_dirty_data_max_max
);
4164 mutex_enter(&arc_reclaim_thr_lock
);
4166 spl_unregister_shrinker(&arc_shrinker
);
4167 #endif /* _KERNEL */
4169 arc_thread_exit
= 1;
4170 while (arc_thread_exit
!= 0)
4171 cv_wait(&arc_reclaim_thr_cv
, &arc_reclaim_thr_lock
);
4172 mutex_exit(&arc_reclaim_thr_lock
);
4178 if (arc_ksp
!= NULL
) {
4179 kstat_delete(arc_ksp
);
4183 mutex_enter(&arc_prune_mtx
);
4184 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
4185 list_remove(&arc_prune_list
, p
);
4186 refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
4187 refcount_destroy(&p
->p_refcnt
);
4188 kmem_free(p
, sizeof (*p
));
4190 mutex_exit(&arc_prune_mtx
);
4192 list_destroy(&arc_prune_list
);
4193 mutex_destroy(&arc_prune_mtx
);
4194 mutex_destroy(&arc_eviction_mtx
);
4195 mutex_destroy(&arc_reclaim_thr_lock
);
4196 cv_destroy(&arc_reclaim_thr_cv
);
4198 list_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
4199 list_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
4200 list_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
4201 list_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
4202 list_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
4203 list_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
4204 list_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
4205 list_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
4207 mutex_destroy(&arc_anon
->arcs_mtx
);
4208 mutex_destroy(&arc_mru
->arcs_mtx
);
4209 mutex_destroy(&arc_mru_ghost
->arcs_mtx
);
4210 mutex_destroy(&arc_mfu
->arcs_mtx
);
4211 mutex_destroy(&arc_mfu_ghost
->arcs_mtx
);
4212 mutex_destroy(&arc_l2c_only
->arcs_mtx
);
4216 ASSERT(arc_loaned_bytes
== 0);
4222 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
4223 * It uses dedicated storage devices to hold cached data, which are populated
4224 * using large infrequent writes. The main role of this cache is to boost
4225 * the performance of random read workloads. The intended L2ARC devices
4226 * include short-stroked disks, solid state disks, and other media with
4227 * substantially faster read latency than disk.
4229 * +-----------------------+
4231 * +-----------------------+
4234 * l2arc_feed_thread() arc_read()
4238 * +---------------+ |
4240 * +---------------+ |
4245 * +-------+ +-------+
4247 * | cache | | cache |
4248 * +-------+ +-------+
4249 * +=========+ .-----.
4250 * : L2ARC : |-_____-|
4251 * : devices : | Disks |
4252 * +=========+ `-_____-'
4254 * Read requests are satisfied from the following sources, in order:
4257 * 2) vdev cache of L2ARC devices
4259 * 4) vdev cache of disks
4262 * Some L2ARC device types exhibit extremely slow write performance.
4263 * To accommodate for this there are some significant differences between
4264 * the L2ARC and traditional cache design:
4266 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
4267 * the ARC behave as usual, freeing buffers and placing headers on ghost
4268 * lists. The ARC does not send buffers to the L2ARC during eviction as
4269 * this would add inflated write latencies for all ARC memory pressure.
4271 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
4272 * It does this by periodically scanning buffers from the eviction-end of
4273 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
4274 * not already there. It scans until a headroom of buffers is satisfied,
4275 * which itself is a buffer for ARC eviction. If a compressible buffer is
4276 * found during scanning and selected for writing to an L2ARC device, we
4277 * temporarily boost scanning headroom during the next scan cycle to make
4278 * sure we adapt to compression effects (which might significantly reduce
4279 * the data volume we write to L2ARC). The thread that does this is
4280 * l2arc_feed_thread(), illustrated below; example sizes are included to
4281 * provide a better sense of ratio than this diagram:
4284 * +---------------------+----------+
4285 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
4286 * +---------------------+----------+ | o L2ARC eligible
4287 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
4288 * +---------------------+----------+ |
4289 * 15.9 Gbytes ^ 32 Mbytes |
4291 * l2arc_feed_thread()
4293 * l2arc write hand <--[oooo]--'
4297 * +==============================+
4298 * L2ARC dev |####|#|###|###| |####| ... |
4299 * +==============================+
4302 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
4303 * evicted, then the L2ARC has cached a buffer much sooner than it probably
4304 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
4305 * safe to say that this is an uncommon case, since buffers at the end of
4306 * the ARC lists have moved there due to inactivity.
4308 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
4309 * then the L2ARC simply misses copying some buffers. This serves as a
4310 * pressure valve to prevent heavy read workloads from both stalling the ARC
4311 * with waits and clogging the L2ARC with writes. This also helps prevent
4312 * the potential for the L2ARC to churn if it attempts to cache content too
4313 * quickly, such as during backups of the entire pool.
4315 * 5. After system boot and before the ARC has filled main memory, there are
4316 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
4317 * lists can remain mostly static. Instead of searching from tail of these
4318 * lists as pictured, the l2arc_feed_thread() will search from the list heads
4319 * for eligible buffers, greatly increasing its chance of finding them.
4321 * The L2ARC device write speed is also boosted during this time so that
4322 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
4323 * there are no L2ARC reads, and no fear of degrading read performance
4324 * through increased writes.
4326 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
4327 * the vdev queue can aggregate them into larger and fewer writes. Each
4328 * device is written to in a rotor fashion, sweeping writes through
4329 * available space then repeating.
4331 * 7. The L2ARC does not store dirty content. It never needs to flush
4332 * write buffers back to disk based storage.
4334 * 8. If an ARC buffer is written (and dirtied) which also exists in the
4335 * L2ARC, the now stale L2ARC buffer is immediately dropped.
4337 * The performance of the L2ARC can be tweaked by a number of tunables, which
4338 * may be necessary for different workloads:
4340 * l2arc_write_max max write bytes per interval
4341 * l2arc_write_boost extra write bytes during device warmup
4342 * l2arc_noprefetch skip caching prefetched buffers
4343 * l2arc_nocompress skip compressing buffers
4344 * l2arc_headroom number of max device writes to precache
4345 * l2arc_headroom_boost when we find compressed buffers during ARC
4346 * scanning, we multiply headroom by this
4347 * percentage factor for the next scan cycle,
4348 * since more compressed buffers are likely to
4350 * l2arc_feed_secs seconds between L2ARC writing
4352 * Tunables may be removed or added as future performance improvements are
4353 * integrated, and also may become zpool properties.
4355 * There are three key functions that control how the L2ARC warms up:
4357 * l2arc_write_eligible() check if a buffer is eligible to cache
4358 * l2arc_write_size() calculate how much to write
4359 * l2arc_write_interval() calculate sleep delay between writes
4361 * These three functions determine what to write, how much, and how quickly
4366 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*ab
)
4369 * A buffer is *not* eligible for the L2ARC if it:
4370 * 1. belongs to a different spa.
4371 * 2. is already cached on the L2ARC.
4372 * 3. has an I/O in progress (it may be an incomplete read).
4373 * 4. is flagged not eligible (zfs property).
4375 if (ab
->b_spa
!= spa_guid
|| ab
->b_l2hdr
!= NULL
||
4376 HDR_IO_IN_PROGRESS(ab
) || !HDR_L2CACHE(ab
))
4383 l2arc_write_size(void)
4388 * Make sure our globals have meaningful values in case the user
4391 size
= l2arc_write_max
;
4393 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
4394 "be greater than zero, resetting it to the default (%d)",
4396 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
4399 if (arc_warm
== B_FALSE
)
4400 size
+= l2arc_write_boost
;
4407 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
4409 clock_t interval
, next
, now
;
4412 * If the ARC lists are busy, increase our write rate; if the
4413 * lists are stale, idle back. This is achieved by checking
4414 * how much we previously wrote - if it was more than half of
4415 * what we wanted, schedule the next write much sooner.
4417 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
4418 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
4420 interval
= hz
* l2arc_feed_secs
;
4422 now
= ddi_get_lbolt();
4423 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
4429 l2arc_hdr_stat_add(void)
4431 ARCSTAT_INCR(arcstat_l2_hdr_size
, HDR_SIZE
);
4432 ARCSTAT_INCR(arcstat_hdr_size
, -HDR_SIZE
);
4436 l2arc_hdr_stat_remove(void)
4438 ARCSTAT_INCR(arcstat_l2_hdr_size
, -HDR_SIZE
);
4439 ARCSTAT_INCR(arcstat_hdr_size
, HDR_SIZE
);
4443 * Cycle through L2ARC devices. This is how L2ARC load balances.
4444 * If a device is returned, this also returns holding the spa config lock.
4446 static l2arc_dev_t
*
4447 l2arc_dev_get_next(void)
4449 l2arc_dev_t
*first
, *next
= NULL
;
4452 * Lock out the removal of spas (spa_namespace_lock), then removal
4453 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
4454 * both locks will be dropped and a spa config lock held instead.
4456 mutex_enter(&spa_namespace_lock
);
4457 mutex_enter(&l2arc_dev_mtx
);
4459 /* if there are no vdevs, there is nothing to do */
4460 if (l2arc_ndev
== 0)
4464 next
= l2arc_dev_last
;
4466 /* loop around the list looking for a non-faulted vdev */
4468 next
= list_head(l2arc_dev_list
);
4470 next
= list_next(l2arc_dev_list
, next
);
4472 next
= list_head(l2arc_dev_list
);
4475 /* if we have come back to the start, bail out */
4478 else if (next
== first
)
4481 } while (vdev_is_dead(next
->l2ad_vdev
));
4483 /* if we were unable to find any usable vdevs, return NULL */
4484 if (vdev_is_dead(next
->l2ad_vdev
))
4487 l2arc_dev_last
= next
;
4490 mutex_exit(&l2arc_dev_mtx
);
4493 * Grab the config lock to prevent the 'next' device from being
4494 * removed while we are writing to it.
4497 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
4498 mutex_exit(&spa_namespace_lock
);
4504 * Free buffers that were tagged for destruction.
4507 l2arc_do_free_on_write(void)
4510 l2arc_data_free_t
*df
, *df_prev
;
4512 mutex_enter(&l2arc_free_on_write_mtx
);
4513 buflist
= l2arc_free_on_write
;
4515 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
4516 df_prev
= list_prev(buflist
, df
);
4517 ASSERT(df
->l2df_data
!= NULL
);
4518 ASSERT(df
->l2df_func
!= NULL
);
4519 df
->l2df_func(df
->l2df_data
, df
->l2df_size
);
4520 list_remove(buflist
, df
);
4521 kmem_free(df
, sizeof (l2arc_data_free_t
));
4524 mutex_exit(&l2arc_free_on_write_mtx
);
4528 * A write to a cache device has completed. Update all headers to allow
4529 * reads from these buffers to begin.
4532 l2arc_write_done(zio_t
*zio
)
4534 l2arc_write_callback_t
*cb
;
4537 arc_buf_hdr_t
*head
, *ab
, *ab_prev
;
4538 l2arc_buf_hdr_t
*abl2
;
4539 kmutex_t
*hash_lock
;
4541 cb
= zio
->io_private
;
4543 dev
= cb
->l2wcb_dev
;
4544 ASSERT(dev
!= NULL
);
4545 head
= cb
->l2wcb_head
;
4546 ASSERT(head
!= NULL
);
4547 buflist
= dev
->l2ad_buflist
;
4548 ASSERT(buflist
!= NULL
);
4549 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
4550 l2arc_write_callback_t
*, cb
);
4552 if (zio
->io_error
!= 0)
4553 ARCSTAT_BUMP(arcstat_l2_writes_error
);
4555 mutex_enter(&l2arc_buflist_mtx
);
4558 * All writes completed, or an error was hit.
4560 for (ab
= list_prev(buflist
, head
); ab
; ab
= ab_prev
) {
4561 ab_prev
= list_prev(buflist
, ab
);
4565 * Release the temporary compressed buffer as soon as possible.
4567 if (abl2
->b_compress
!= ZIO_COMPRESS_OFF
)
4568 l2arc_release_cdata_buf(ab
);
4570 hash_lock
= HDR_LOCK(ab
);
4571 if (!mutex_tryenter(hash_lock
)) {
4573 * This buffer misses out. It may be in a stage
4574 * of eviction. Its ARC_L2_WRITING flag will be
4575 * left set, denying reads to this buffer.
4577 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss
);
4581 if (zio
->io_error
!= 0) {
4583 * Error - drop L2ARC entry.
4585 list_remove(buflist
, ab
);
4586 ARCSTAT_INCR(arcstat_l2_asize
, -abl2
->b_asize
);
4588 kmem_cache_free(l2arc_hdr_cache
, abl2
);
4589 arc_space_return(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
4590 ARCSTAT_INCR(arcstat_l2_size
, -ab
->b_size
);
4594 * Allow ARC to begin reads to this L2ARC entry.
4596 ab
->b_flags
&= ~ARC_L2_WRITING
;
4598 mutex_exit(hash_lock
);
4601 atomic_inc_64(&l2arc_writes_done
);
4602 list_remove(buflist
, head
);
4603 kmem_cache_free(hdr_cache
, head
);
4604 mutex_exit(&l2arc_buflist_mtx
);
4606 l2arc_do_free_on_write();
4608 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
4612 * A read to a cache device completed. Validate buffer contents before
4613 * handing over to the regular ARC routines.
4616 l2arc_read_done(zio_t
*zio
)
4618 l2arc_read_callback_t
*cb
;
4621 kmutex_t
*hash_lock
;
4624 ASSERT(zio
->io_vd
!= NULL
);
4625 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
4627 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
4629 cb
= zio
->io_private
;
4631 buf
= cb
->l2rcb_buf
;
4632 ASSERT(buf
!= NULL
);
4634 hash_lock
= HDR_LOCK(buf
->b_hdr
);
4635 mutex_enter(hash_lock
);
4637 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
4640 * If the buffer was compressed, decompress it first.
4642 if (cb
->l2rcb_compress
!= ZIO_COMPRESS_OFF
)
4643 l2arc_decompress_zio(zio
, hdr
, cb
->l2rcb_compress
);
4644 ASSERT(zio
->io_data
!= NULL
);
4647 * Check this survived the L2ARC journey.
4649 equal
= arc_cksum_equal(buf
);
4650 if (equal
&& zio
->io_error
== 0 && !HDR_L2_EVICTED(hdr
)) {
4651 mutex_exit(hash_lock
);
4652 zio
->io_private
= buf
;
4653 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
4654 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
4657 mutex_exit(hash_lock
);
4659 * Buffer didn't survive caching. Increment stats and
4660 * reissue to the original storage device.
4662 if (zio
->io_error
!= 0) {
4663 ARCSTAT_BUMP(arcstat_l2_io_error
);
4665 zio
->io_error
= SET_ERROR(EIO
);
4668 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
4671 * If there's no waiter, issue an async i/o to the primary
4672 * storage now. If there *is* a waiter, the caller must
4673 * issue the i/o in a context where it's OK to block.
4675 if (zio
->io_waiter
== NULL
) {
4676 zio_t
*pio
= zio_unique_parent(zio
);
4678 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
4680 zio_nowait(zio_read(pio
, cb
->l2rcb_spa
, &cb
->l2rcb_bp
,
4681 buf
->b_data
, zio
->io_size
, arc_read_done
, buf
,
4682 zio
->io_priority
, cb
->l2rcb_flags
, &cb
->l2rcb_zb
));
4686 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
4690 * This is the list priority from which the L2ARC will search for pages to
4691 * cache. This is used within loops (0..3) to cycle through lists in the
4692 * desired order. This order can have a significant effect on cache
4695 * Currently the metadata lists are hit first, MFU then MRU, followed by
4696 * the data lists. This function returns a locked list, and also returns
4700 l2arc_list_locked(int list_num
, kmutex_t
**lock
)
4702 list_t
*list
= NULL
;
4704 ASSERT(list_num
>= 0 && list_num
<= 3);
4708 list
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
4709 *lock
= &arc_mfu
->arcs_mtx
;
4712 list
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
4713 *lock
= &arc_mru
->arcs_mtx
;
4716 list
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
4717 *lock
= &arc_mfu
->arcs_mtx
;
4720 list
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
4721 *lock
= &arc_mru
->arcs_mtx
;
4725 ASSERT(!(MUTEX_HELD(*lock
)));
4731 * Evict buffers from the device write hand to the distance specified in
4732 * bytes. This distance may span populated buffers, it may span nothing.
4733 * This is clearing a region on the L2ARC device ready for writing.
4734 * If the 'all' boolean is set, every buffer is evicted.
4737 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
4740 l2arc_buf_hdr_t
*abl2
;
4741 arc_buf_hdr_t
*ab
, *ab_prev
;
4742 kmutex_t
*hash_lock
;
4745 buflist
= dev
->l2ad_buflist
;
4747 if (buflist
== NULL
)
4750 if (!all
&& dev
->l2ad_first
) {
4752 * This is the first sweep through the device. There is
4758 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
4760 * When nearing the end of the device, evict to the end
4761 * before the device write hand jumps to the start.
4763 taddr
= dev
->l2ad_end
;
4765 taddr
= dev
->l2ad_hand
+ distance
;
4767 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
4768 uint64_t, taddr
, boolean_t
, all
);
4771 mutex_enter(&l2arc_buflist_mtx
);
4772 for (ab
= list_tail(buflist
); ab
; ab
= ab_prev
) {
4773 ab_prev
= list_prev(buflist
, ab
);
4775 hash_lock
= HDR_LOCK(ab
);
4776 if (!mutex_tryenter(hash_lock
)) {
4778 * Missed the hash lock. Retry.
4780 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
4781 mutex_exit(&l2arc_buflist_mtx
);
4782 mutex_enter(hash_lock
);
4783 mutex_exit(hash_lock
);
4787 if (HDR_L2_WRITE_HEAD(ab
)) {
4789 * We hit a write head node. Leave it for
4790 * l2arc_write_done().
4792 list_remove(buflist
, ab
);
4793 mutex_exit(hash_lock
);
4797 if (!all
&& ab
->b_l2hdr
!= NULL
&&
4798 (ab
->b_l2hdr
->b_daddr
> taddr
||
4799 ab
->b_l2hdr
->b_daddr
< dev
->l2ad_hand
)) {
4801 * We've evicted to the target address,
4802 * or the end of the device.
4804 mutex_exit(hash_lock
);
4808 if (HDR_FREE_IN_PROGRESS(ab
)) {
4810 * Already on the path to destruction.
4812 mutex_exit(hash_lock
);
4816 if (ab
->b_state
== arc_l2c_only
) {
4817 ASSERT(!HDR_L2_READING(ab
));
4819 * This doesn't exist in the ARC. Destroy.
4820 * arc_hdr_destroy() will call list_remove()
4821 * and decrement arcstat_l2_size.
4823 arc_change_state(arc_anon
, ab
, hash_lock
);
4824 arc_hdr_destroy(ab
);
4827 * Invalidate issued or about to be issued
4828 * reads, since we may be about to write
4829 * over this location.
4831 if (HDR_L2_READING(ab
)) {
4832 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
4833 ab
->b_flags
|= ARC_L2_EVICTED
;
4837 * Tell ARC this no longer exists in L2ARC.
4839 if (ab
->b_l2hdr
!= NULL
) {
4841 ARCSTAT_INCR(arcstat_l2_asize
, -abl2
->b_asize
);
4843 kmem_cache_free(l2arc_hdr_cache
, abl2
);
4844 arc_space_return(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
4845 ARCSTAT_INCR(arcstat_l2_size
, -ab
->b_size
);
4847 list_remove(buflist
, ab
);
4850 * This may have been leftover after a
4853 ab
->b_flags
&= ~ARC_L2_WRITING
;
4855 mutex_exit(hash_lock
);
4857 mutex_exit(&l2arc_buflist_mtx
);
4859 vdev_space_update(dev
->l2ad_vdev
, -(taddr
- dev
->l2ad_evict
), 0, 0);
4860 dev
->l2ad_evict
= taddr
;
4864 * Find and write ARC buffers to the L2ARC device.
4866 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4867 * for reading until they have completed writing.
4868 * The headroom_boost is an in-out parameter used to maintain headroom boost
4869 * state between calls to this function.
4871 * Returns the number of bytes actually written (which may be smaller than
4872 * the delta by which the device hand has changed due to alignment).
4875 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
,
4876 boolean_t
*headroom_boost
)
4878 arc_buf_hdr_t
*ab
, *ab_prev
, *head
;
4880 uint64_t write_asize
, write_psize
, write_sz
, headroom
,
4883 kmutex_t
*list_lock
= NULL
;
4885 l2arc_write_callback_t
*cb
;
4887 uint64_t guid
= spa_load_guid(spa
);
4889 const boolean_t do_headroom_boost
= *headroom_boost
;
4891 ASSERT(dev
->l2ad_vdev
!= NULL
);
4893 /* Lower the flag now, we might want to raise it again later. */
4894 *headroom_boost
= B_FALSE
;
4897 write_sz
= write_asize
= write_psize
= 0;
4899 head
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
4900 head
->b_flags
|= ARC_L2_WRITE_HEAD
;
4903 * We will want to try to compress buffers that are at least 2x the
4904 * device sector size.
4906 buf_compress_minsz
= 2 << dev
->l2ad_vdev
->vdev_ashift
;
4909 * Copy buffers for L2ARC writing.
4911 mutex_enter(&l2arc_buflist_mtx
);
4912 for (try = 0; try <= 3; try++) {
4913 uint64_t passed_sz
= 0;
4915 list
= l2arc_list_locked(try, &list_lock
);
4918 * L2ARC fast warmup.
4920 * Until the ARC is warm and starts to evict, read from the
4921 * head of the ARC lists rather than the tail.
4923 if (arc_warm
== B_FALSE
)
4924 ab
= list_head(list
);
4926 ab
= list_tail(list
);
4928 headroom
= target_sz
* l2arc_headroom
;
4929 if (do_headroom_boost
)
4930 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
4932 for (; ab
; ab
= ab_prev
) {
4933 l2arc_buf_hdr_t
*l2hdr
;
4934 kmutex_t
*hash_lock
;
4937 if (arc_warm
== B_FALSE
)
4938 ab_prev
= list_next(list
, ab
);
4940 ab_prev
= list_prev(list
, ab
);
4942 hash_lock
= HDR_LOCK(ab
);
4943 if (!mutex_tryenter(hash_lock
)) {
4945 * Skip this buffer rather than waiting.
4950 passed_sz
+= ab
->b_size
;
4951 if (passed_sz
> headroom
) {
4955 mutex_exit(hash_lock
);
4959 if (!l2arc_write_eligible(guid
, ab
)) {
4960 mutex_exit(hash_lock
);
4964 if ((write_sz
+ ab
->b_size
) > target_sz
) {
4966 mutex_exit(hash_lock
);
4972 * Insert a dummy header on the buflist so
4973 * l2arc_write_done() can find where the
4974 * write buffers begin without searching.
4976 list_insert_head(dev
->l2ad_buflist
, head
);
4978 cb
= kmem_alloc(sizeof (l2arc_write_callback_t
),
4980 cb
->l2wcb_dev
= dev
;
4981 cb
->l2wcb_head
= head
;
4982 pio
= zio_root(spa
, l2arc_write_done
, cb
,
4987 * Create and add a new L2ARC header.
4989 l2hdr
= kmem_cache_alloc(l2arc_hdr_cache
, KM_PUSHPAGE
);
4992 arc_space_consume(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
4994 ab
->b_flags
|= ARC_L2_WRITING
;
4997 * Temporarily stash the data buffer in b_tmp_cdata.
4998 * The subsequent write step will pick it up from
4999 * there. This is because can't access ab->b_buf
5000 * without holding the hash_lock, which we in turn
5001 * can't access without holding the ARC list locks
5002 * (which we want to avoid during compression/writing)
5004 l2hdr
->b_compress
= ZIO_COMPRESS_OFF
;
5005 l2hdr
->b_asize
= ab
->b_size
;
5006 l2hdr
->b_tmp_cdata
= ab
->b_buf
->b_data
;
5009 buf_sz
= ab
->b_size
;
5010 ab
->b_l2hdr
= l2hdr
;
5012 list_insert_head(dev
->l2ad_buflist
, ab
);
5015 * Compute and store the buffer cksum before
5016 * writing. On debug the cksum is verified first.
5018 arc_cksum_verify(ab
->b_buf
);
5019 arc_cksum_compute(ab
->b_buf
, B_TRUE
);
5021 mutex_exit(hash_lock
);
5026 mutex_exit(list_lock
);
5032 /* No buffers selected for writing? */
5035 mutex_exit(&l2arc_buflist_mtx
);
5036 kmem_cache_free(hdr_cache
, head
);
5041 * Now start writing the buffers. We're starting at the write head
5042 * and work backwards, retracing the course of the buffer selector
5045 for (ab
= list_prev(dev
->l2ad_buflist
, head
); ab
;
5046 ab
= list_prev(dev
->l2ad_buflist
, ab
)) {
5047 l2arc_buf_hdr_t
*l2hdr
;
5051 * We shouldn't need to lock the buffer here, since we flagged
5052 * it as ARC_L2_WRITING in the previous step, but we must take
5053 * care to only access its L2 cache parameters. In particular,
5054 * ab->b_buf may be invalid by now due to ARC eviction.
5056 l2hdr
= ab
->b_l2hdr
;
5057 l2hdr
->b_daddr
= dev
->l2ad_hand
;
5059 if (!l2arc_nocompress
&& (ab
->b_flags
& ARC_L2COMPRESS
) &&
5060 l2hdr
->b_asize
>= buf_compress_minsz
) {
5061 if (l2arc_compress_buf(l2hdr
)) {
5063 * If compression succeeded, enable headroom
5064 * boost on the next scan cycle.
5066 *headroom_boost
= B_TRUE
;
5071 * Pick up the buffer data we had previously stashed away
5072 * (and now potentially also compressed).
5074 buf_data
= l2hdr
->b_tmp_cdata
;
5075 buf_sz
= l2hdr
->b_asize
;
5077 /* Compression may have squashed the buffer to zero length. */
5081 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
5082 dev
->l2ad_hand
, buf_sz
, buf_data
, ZIO_CHECKSUM_OFF
,
5083 NULL
, NULL
, ZIO_PRIORITY_ASYNC_WRITE
,
5084 ZIO_FLAG_CANFAIL
, B_FALSE
);
5086 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
5088 (void) zio_nowait(wzio
);
5090 write_asize
+= buf_sz
;
5092 * Keep the clock hand suitably device-aligned.
5094 buf_p_sz
= vdev_psize_to_asize(dev
->l2ad_vdev
, buf_sz
);
5095 write_psize
+= buf_p_sz
;
5096 dev
->l2ad_hand
+= buf_p_sz
;
5100 mutex_exit(&l2arc_buflist_mtx
);
5102 ASSERT3U(write_asize
, <=, target_sz
);
5103 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
5104 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_asize
);
5105 ARCSTAT_INCR(arcstat_l2_size
, write_sz
);
5106 ARCSTAT_INCR(arcstat_l2_asize
, write_asize
);
5107 vdev_space_update(dev
->l2ad_vdev
, write_psize
, 0, 0);
5110 * Bump device hand to the device start if it is approaching the end.
5111 * l2arc_evict() will already have evicted ahead for this case.
5113 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
5114 vdev_space_update(dev
->l2ad_vdev
,
5115 dev
->l2ad_end
- dev
->l2ad_hand
, 0, 0);
5116 dev
->l2ad_hand
= dev
->l2ad_start
;
5117 dev
->l2ad_evict
= dev
->l2ad_start
;
5118 dev
->l2ad_first
= B_FALSE
;
5121 dev
->l2ad_writing
= B_TRUE
;
5122 (void) zio_wait(pio
);
5123 dev
->l2ad_writing
= B_FALSE
;
5125 return (write_asize
);
5129 * Compresses an L2ARC buffer.
5130 * The data to be compressed must be prefilled in l2hdr->b_tmp_cdata and its
5131 * size in l2hdr->b_asize. This routine tries to compress the data and
5132 * depending on the compression result there are three possible outcomes:
5133 * *) The buffer was incompressible. The original l2hdr contents were left
5134 * untouched and are ready for writing to an L2 device.
5135 * *) The buffer was all-zeros, so there is no need to write it to an L2
5136 * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is
5137 * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY.
5138 * *) Compression succeeded and b_tmp_cdata was replaced with a temporary
5139 * data buffer which holds the compressed data to be written, and b_asize
5140 * tells us how much data there is. b_compress is set to the appropriate
5141 * compression algorithm. Once writing is done, invoke
5142 * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer.
5144 * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the
5145 * buffer was incompressible).
5148 l2arc_compress_buf(l2arc_buf_hdr_t
*l2hdr
)
5153 ASSERT(l2hdr
->b_compress
== ZIO_COMPRESS_OFF
);
5154 ASSERT(l2hdr
->b_tmp_cdata
!= NULL
);
5156 len
= l2hdr
->b_asize
;
5157 cdata
= zio_data_buf_alloc(len
);
5158 csize
= zio_compress_data(ZIO_COMPRESS_LZ4
, l2hdr
->b_tmp_cdata
,
5159 cdata
, l2hdr
->b_asize
);
5162 /* zero block, indicate that there's nothing to write */
5163 zio_data_buf_free(cdata
, len
);
5164 l2hdr
->b_compress
= ZIO_COMPRESS_EMPTY
;
5166 l2hdr
->b_tmp_cdata
= NULL
;
5167 ARCSTAT_BUMP(arcstat_l2_compress_zeros
);
5169 } else if (csize
> 0 && csize
< len
) {
5171 * Compression succeeded, we'll keep the cdata around for
5172 * writing and release it afterwards.
5174 l2hdr
->b_compress
= ZIO_COMPRESS_LZ4
;
5175 l2hdr
->b_asize
= csize
;
5176 l2hdr
->b_tmp_cdata
= cdata
;
5177 ARCSTAT_BUMP(arcstat_l2_compress_successes
);
5181 * Compression failed, release the compressed buffer.
5182 * l2hdr will be left unmodified.
5184 zio_data_buf_free(cdata
, len
);
5185 ARCSTAT_BUMP(arcstat_l2_compress_failures
);
5191 * Decompresses a zio read back from an l2arc device. On success, the
5192 * underlying zio's io_data buffer is overwritten by the uncompressed
5193 * version. On decompression error (corrupt compressed stream), the
5194 * zio->io_error value is set to signal an I/O error.
5196 * Please note that the compressed data stream is not checksummed, so
5197 * if the underlying device is experiencing data corruption, we may feed
5198 * corrupt data to the decompressor, so the decompressor needs to be
5199 * able to handle this situation (LZ4 does).
5202 l2arc_decompress_zio(zio_t
*zio
, arc_buf_hdr_t
*hdr
, enum zio_compress c
)
5207 ASSERT(L2ARC_IS_VALID_COMPRESS(c
));
5209 if (zio
->io_error
!= 0) {
5211 * An io error has occured, just restore the original io
5212 * size in preparation for a main pool read.
5214 zio
->io_orig_size
= zio
->io_size
= hdr
->b_size
;
5218 if (c
== ZIO_COMPRESS_EMPTY
) {
5220 * An empty buffer results in a null zio, which means we
5221 * need to fill its io_data after we're done restoring the
5222 * buffer's contents.
5224 ASSERT(hdr
->b_buf
!= NULL
);
5225 bzero(hdr
->b_buf
->b_data
, hdr
->b_size
);
5226 zio
->io_data
= zio
->io_orig_data
= hdr
->b_buf
->b_data
;
5228 ASSERT(zio
->io_data
!= NULL
);
5230 * We copy the compressed data from the start of the arc buffer
5231 * (the zio_read will have pulled in only what we need, the
5232 * rest is garbage which we will overwrite at decompression)
5233 * and then decompress back to the ARC data buffer. This way we
5234 * can minimize copying by simply decompressing back over the
5235 * original compressed data (rather than decompressing to an
5236 * aux buffer and then copying back the uncompressed buffer,
5237 * which is likely to be much larger).
5239 csize
= zio
->io_size
;
5240 cdata
= zio_data_buf_alloc(csize
);
5241 bcopy(zio
->io_data
, cdata
, csize
);
5242 if (zio_decompress_data(c
, cdata
, zio
->io_data
, csize
,
5244 zio
->io_error
= SET_ERROR(EIO
);
5245 zio_data_buf_free(cdata
, csize
);
5248 /* Restore the expected uncompressed IO size. */
5249 zio
->io_orig_size
= zio
->io_size
= hdr
->b_size
;
5253 * Releases the temporary b_tmp_cdata buffer in an l2arc header structure.
5254 * This buffer serves as a temporary holder of compressed data while
5255 * the buffer entry is being written to an l2arc device. Once that is
5256 * done, we can dispose of it.
5259 l2arc_release_cdata_buf(arc_buf_hdr_t
*ab
)
5261 l2arc_buf_hdr_t
*l2hdr
= ab
->b_l2hdr
;
5263 if (l2hdr
->b_compress
== ZIO_COMPRESS_LZ4
) {
5265 * If the data was compressed, then we've allocated a
5266 * temporary buffer for it, so now we need to release it.
5268 ASSERT(l2hdr
->b_tmp_cdata
!= NULL
);
5269 zio_data_buf_free(l2hdr
->b_tmp_cdata
, ab
->b_size
);
5271 l2hdr
->b_tmp_cdata
= NULL
;
5275 * This thread feeds the L2ARC at regular intervals. This is the beating
5276 * heart of the L2ARC.
5279 l2arc_feed_thread(void)
5284 uint64_t size
, wrote
;
5285 clock_t begin
, next
= ddi_get_lbolt();
5286 boolean_t headroom_boost
= B_FALSE
;
5288 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
5290 mutex_enter(&l2arc_feed_thr_lock
);
5292 while (l2arc_thread_exit
== 0) {
5293 CALLB_CPR_SAFE_BEGIN(&cpr
);
5294 (void) cv_timedwait_interruptible(&l2arc_feed_thr_cv
,
5295 &l2arc_feed_thr_lock
, next
);
5296 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
5297 next
= ddi_get_lbolt() + hz
;
5300 * Quick check for L2ARC devices.
5302 mutex_enter(&l2arc_dev_mtx
);
5303 if (l2arc_ndev
== 0) {
5304 mutex_exit(&l2arc_dev_mtx
);
5307 mutex_exit(&l2arc_dev_mtx
);
5308 begin
= ddi_get_lbolt();
5311 * This selects the next l2arc device to write to, and in
5312 * doing so the next spa to feed from: dev->l2ad_spa. This
5313 * will return NULL if there are now no l2arc devices or if
5314 * they are all faulted.
5316 * If a device is returned, its spa's config lock is also
5317 * held to prevent device removal. l2arc_dev_get_next()
5318 * will grab and release l2arc_dev_mtx.
5320 if ((dev
= l2arc_dev_get_next()) == NULL
)
5323 spa
= dev
->l2ad_spa
;
5324 ASSERT(spa
!= NULL
);
5327 * If the pool is read-only then force the feed thread to
5328 * sleep a little longer.
5330 if (!spa_writeable(spa
)) {
5331 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
5332 spa_config_exit(spa
, SCL_L2ARC
, dev
);
5337 * Avoid contributing to memory pressure.
5340 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
5341 spa_config_exit(spa
, SCL_L2ARC
, dev
);
5345 ARCSTAT_BUMP(arcstat_l2_feeds
);
5347 size
= l2arc_write_size();
5350 * Evict L2ARC buffers that will be overwritten.
5352 l2arc_evict(dev
, size
, B_FALSE
);
5355 * Write ARC buffers.
5357 wrote
= l2arc_write_buffers(spa
, dev
, size
, &headroom_boost
);
5360 * Calculate interval between writes.
5362 next
= l2arc_write_interval(begin
, size
, wrote
);
5363 spa_config_exit(spa
, SCL_L2ARC
, dev
);
5366 l2arc_thread_exit
= 0;
5367 cv_broadcast(&l2arc_feed_thr_cv
);
5368 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
5373 l2arc_vdev_present(vdev_t
*vd
)
5377 mutex_enter(&l2arc_dev_mtx
);
5378 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
5379 dev
= list_next(l2arc_dev_list
, dev
)) {
5380 if (dev
->l2ad_vdev
== vd
)
5383 mutex_exit(&l2arc_dev_mtx
);
5385 return (dev
!= NULL
);
5389 * Add a vdev for use by the L2ARC. By this point the spa has already
5390 * validated the vdev and opened it.
5393 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
5395 l2arc_dev_t
*adddev
;
5397 ASSERT(!l2arc_vdev_present(vd
));
5400 * Create a new l2arc device entry.
5402 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
5403 adddev
->l2ad_spa
= spa
;
5404 adddev
->l2ad_vdev
= vd
;
5405 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
5406 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
5407 adddev
->l2ad_hand
= adddev
->l2ad_start
;
5408 adddev
->l2ad_evict
= adddev
->l2ad_start
;
5409 adddev
->l2ad_first
= B_TRUE
;
5410 adddev
->l2ad_writing
= B_FALSE
;
5411 list_link_init(&adddev
->l2ad_node
);
5414 * This is a list of all ARC buffers that are still valid on the
5417 adddev
->l2ad_buflist
= kmem_zalloc(sizeof (list_t
), KM_SLEEP
);
5418 list_create(adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
5419 offsetof(arc_buf_hdr_t
, b_l2node
));
5421 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
5424 * Add device to global list
5426 mutex_enter(&l2arc_dev_mtx
);
5427 list_insert_head(l2arc_dev_list
, adddev
);
5428 atomic_inc_64(&l2arc_ndev
);
5429 mutex_exit(&l2arc_dev_mtx
);
5433 * Remove a vdev from the L2ARC.
5436 l2arc_remove_vdev(vdev_t
*vd
)
5438 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
5441 * Find the device by vdev
5443 mutex_enter(&l2arc_dev_mtx
);
5444 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
5445 nextdev
= list_next(l2arc_dev_list
, dev
);
5446 if (vd
== dev
->l2ad_vdev
) {
5451 ASSERT(remdev
!= NULL
);
5454 * Remove device from global list
5456 list_remove(l2arc_dev_list
, remdev
);
5457 l2arc_dev_last
= NULL
; /* may have been invalidated */
5458 atomic_dec_64(&l2arc_ndev
);
5459 mutex_exit(&l2arc_dev_mtx
);
5462 * Clear all buflists and ARC references. L2ARC device flush.
5464 l2arc_evict(remdev
, 0, B_TRUE
);
5465 list_destroy(remdev
->l2ad_buflist
);
5466 kmem_free(remdev
->l2ad_buflist
, sizeof (list_t
));
5467 kmem_free(remdev
, sizeof (l2arc_dev_t
));
5473 l2arc_thread_exit
= 0;
5475 l2arc_writes_sent
= 0;
5476 l2arc_writes_done
= 0;
5478 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5479 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
5480 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
5481 mutex_init(&l2arc_buflist_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
5482 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
5484 l2arc_dev_list
= &L2ARC_dev_list
;
5485 l2arc_free_on_write
= &L2ARC_free_on_write
;
5486 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
5487 offsetof(l2arc_dev_t
, l2ad_node
));
5488 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
5489 offsetof(l2arc_data_free_t
, l2df_list_node
));
5496 * This is called from dmu_fini(), which is called from spa_fini();
5497 * Because of this, we can assume that all l2arc devices have
5498 * already been removed when the pools themselves were removed.
5501 l2arc_do_free_on_write();
5503 mutex_destroy(&l2arc_feed_thr_lock
);
5504 cv_destroy(&l2arc_feed_thr_cv
);
5505 mutex_destroy(&l2arc_dev_mtx
);
5506 mutex_destroy(&l2arc_buflist_mtx
);
5507 mutex_destroy(&l2arc_free_on_write_mtx
);
5509 list_destroy(l2arc_dev_list
);
5510 list_destroy(l2arc_free_on_write
);
5516 if (!(spa_mode_global
& FWRITE
))
5519 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
5520 TS_RUN
, minclsyspri
);
5526 if (!(spa_mode_global
& FWRITE
))
5529 mutex_enter(&l2arc_feed_thr_lock
);
5530 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
5531 l2arc_thread_exit
= 1;
5532 while (l2arc_thread_exit
!= 0)
5533 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
5534 mutex_exit(&l2arc_feed_thr_lock
);
5537 #if defined(_KERNEL) && defined(HAVE_SPL)
5538 EXPORT_SYMBOL(arc_read
);
5539 EXPORT_SYMBOL(arc_buf_remove_ref
);
5540 EXPORT_SYMBOL(arc_buf_info
);
5541 EXPORT_SYMBOL(arc_getbuf_func
);
5542 EXPORT_SYMBOL(arc_add_prune_callback
);
5543 EXPORT_SYMBOL(arc_remove_prune_callback
);
5545 module_param(zfs_arc_min
, ulong
, 0644);
5546 MODULE_PARM_DESC(zfs_arc_min
, "Min arc size");
5548 module_param(zfs_arc_max
, ulong
, 0644);
5549 MODULE_PARM_DESC(zfs_arc_max
, "Max arc size");
5551 module_param(zfs_arc_meta_limit
, ulong
, 0644);
5552 MODULE_PARM_DESC(zfs_arc_meta_limit
, "Meta limit for arc size");
5554 module_param(zfs_arc_meta_prune
, int, 0644);
5555 MODULE_PARM_DESC(zfs_arc_meta_prune
, "Bytes of meta data to prune");
5557 module_param(zfs_arc_grow_retry
, int, 0644);
5558 MODULE_PARM_DESC(zfs_arc_grow_retry
, "Seconds before growing arc size");
5560 module_param(zfs_arc_p_aggressive_disable
, int, 0644);
5561 MODULE_PARM_DESC(zfs_arc_p_aggressive_disable
, "disable aggressive arc_p grow");
5563 module_param(zfs_arc_shrink_shift
, int, 0644);
5564 MODULE_PARM_DESC(zfs_arc_shrink_shift
, "log2(fraction of arc to reclaim)");
5566 module_param(zfs_arc_p_min_shift
, int, 0644);
5567 MODULE_PARM_DESC(zfs_arc_p_min_shift
, "arc_c shift to calc min/max arc_p");
5569 module_param(zfs_disable_dup_eviction
, int, 0644);
5570 MODULE_PARM_DESC(zfs_disable_dup_eviction
, "disable duplicate buffer eviction");
5572 module_param(zfs_arc_memory_throttle_disable
, int, 0644);
5573 MODULE_PARM_DESC(zfs_arc_memory_throttle_disable
, "disable memory throttle");
5575 module_param(zfs_arc_min_prefetch_lifespan
, int, 0644);
5576 MODULE_PARM_DESC(zfs_arc_min_prefetch_lifespan
, "Min life of prefetch block");
5578 module_param(l2arc_write_max
, ulong
, 0644);
5579 MODULE_PARM_DESC(l2arc_write_max
, "Max write bytes per interval");
5581 module_param(l2arc_write_boost
, ulong
, 0644);
5582 MODULE_PARM_DESC(l2arc_write_boost
, "Extra write bytes during device warmup");
5584 module_param(l2arc_headroom
, ulong
, 0644);
5585 MODULE_PARM_DESC(l2arc_headroom
, "Number of max device writes to precache");
5587 module_param(l2arc_headroom_boost
, ulong
, 0644);
5588 MODULE_PARM_DESC(l2arc_headroom_boost
, "Compressed l2arc_headroom multiplier");
5590 module_param(l2arc_feed_secs
, ulong
, 0644);
5591 MODULE_PARM_DESC(l2arc_feed_secs
, "Seconds between L2ARC writing");
5593 module_param(l2arc_feed_min_ms
, ulong
, 0644);
5594 MODULE_PARM_DESC(l2arc_feed_min_ms
, "Min feed interval in milliseconds");
5596 module_param(l2arc_noprefetch
, int, 0644);
5597 MODULE_PARM_DESC(l2arc_noprefetch
, "Skip caching prefetched buffers");
5599 module_param(l2arc_nocompress
, int, 0644);
5600 MODULE_PARM_DESC(l2arc_nocompress
, "Skip compressing L2ARC buffers");
5602 module_param(l2arc_feed_again
, int, 0644);
5603 MODULE_PARM_DESC(l2arc_feed_again
, "Turbo L2ARC warmup");
5605 module_param(l2arc_norw
, int, 0644);
5606 MODULE_PARM_DESC(l2arc_norw
, "No reads during writes");