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) 2011 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 therefor 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 therefor 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 therefor 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>
138 #include <sys/vmsystm.h>
140 #include <sys/fs/swapnode.h>
143 #include <sys/callb.h>
144 #include <sys/kstat.h>
145 #include <sys/dmu_tx.h>
146 #include <zfs_fletcher.h>
148 static kmutex_t arc_reclaim_thr_lock
;
149 static kcondvar_t arc_reclaim_thr_cv
; /* used to signal reclaim thr */
150 static uint8_t arc_thread_exit
;
152 /* number of bytes to prune from caches when at arc_meta_limit is reached */
153 int zfs_arc_meta_prune
= 1048576;
155 typedef enum arc_reclaim_strategy
{
156 ARC_RECLAIM_AGGR
, /* Aggressive reclaim strategy */
157 ARC_RECLAIM_CONS
/* Conservative reclaim strategy */
158 } arc_reclaim_strategy_t
;
160 /* number of seconds before growing cache again */
161 int zfs_arc_grow_retry
= 5;
163 /* shift of arc_c for calculating both min and max arc_p */
164 int zfs_arc_p_min_shift
= 4;
166 /* log2(fraction of arc to reclaim) */
167 int zfs_arc_shrink_shift
= 5;
170 * minimum lifespan of a prefetch block in clock ticks
171 * (initialized in arc_init())
173 int zfs_arc_min_prefetch_lifespan
= HZ
;
175 /* disable arc proactive arc throttle due to low memory */
176 int zfs_arc_memory_throttle_disable
= 1;
178 /* disable duplicate buffer eviction */
179 int zfs_disable_dup_eviction
= 0;
183 /* expiration time for arc_no_grow */
184 static clock_t arc_grow_time
= 0;
187 * The arc has filled available memory and has now warmed up.
189 static boolean_t arc_warm
;
192 * These tunables are for performance analysis.
194 unsigned long zfs_arc_max
= 0;
195 unsigned long zfs_arc_min
= 0;
196 unsigned long zfs_arc_meta_limit
= 0;
199 * Note that buffers can be in one of 6 states:
200 * ARC_anon - anonymous (discussed below)
201 * ARC_mru - recently used, currently cached
202 * ARC_mru_ghost - recentely used, no longer in cache
203 * ARC_mfu - frequently used, currently cached
204 * ARC_mfu_ghost - frequently used, no longer in cache
205 * ARC_l2c_only - exists in L2ARC but not other states
206 * When there are no active references to the buffer, they are
207 * are linked onto a list in one of these arc states. These are
208 * the only buffers that can be evicted or deleted. Within each
209 * state there are multiple lists, one for meta-data and one for
210 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
211 * etc.) is tracked separately so that it can be managed more
212 * explicitly: favored over data, limited explicitly.
214 * Anonymous buffers are buffers that are not associated with
215 * a DVA. These are buffers that hold dirty block copies
216 * before they are written to stable storage. By definition,
217 * they are "ref'd" and are considered part of arc_mru
218 * that cannot be freed. Generally, they will aquire a DVA
219 * as they are written and migrate onto the arc_mru list.
221 * The ARC_l2c_only state is for buffers that are in the second
222 * level ARC but no longer in any of the ARC_m* lists. The second
223 * level ARC itself may also contain buffers that are in any of
224 * the ARC_m* states - meaning that a buffer can exist in two
225 * places. The reason for the ARC_l2c_only state is to keep the
226 * buffer header in the hash table, so that reads that hit the
227 * second level ARC benefit from these fast lookups.
230 typedef struct arc_state
{
231 list_t arcs_list
[ARC_BUFC_NUMTYPES
]; /* list of evictable buffers */
232 uint64_t arcs_lsize
[ARC_BUFC_NUMTYPES
]; /* amount of evictable data */
233 uint64_t arcs_size
; /* total amount of data in this state */
238 static arc_state_t ARC_anon
;
239 static arc_state_t ARC_mru
;
240 static arc_state_t ARC_mru_ghost
;
241 static arc_state_t ARC_mfu
;
242 static arc_state_t ARC_mfu_ghost
;
243 static arc_state_t ARC_l2c_only
;
245 typedef struct arc_stats
{
246 kstat_named_t arcstat_hits
;
247 kstat_named_t arcstat_misses
;
248 kstat_named_t arcstat_demand_data_hits
;
249 kstat_named_t arcstat_demand_data_misses
;
250 kstat_named_t arcstat_demand_metadata_hits
;
251 kstat_named_t arcstat_demand_metadata_misses
;
252 kstat_named_t arcstat_prefetch_data_hits
;
253 kstat_named_t arcstat_prefetch_data_misses
;
254 kstat_named_t arcstat_prefetch_metadata_hits
;
255 kstat_named_t arcstat_prefetch_metadata_misses
;
256 kstat_named_t arcstat_mru_hits
;
257 kstat_named_t arcstat_mru_ghost_hits
;
258 kstat_named_t arcstat_mfu_hits
;
259 kstat_named_t arcstat_mfu_ghost_hits
;
260 kstat_named_t arcstat_deleted
;
261 kstat_named_t arcstat_recycle_miss
;
262 kstat_named_t arcstat_mutex_miss
;
263 kstat_named_t arcstat_evict_skip
;
264 kstat_named_t arcstat_evict_l2_cached
;
265 kstat_named_t arcstat_evict_l2_eligible
;
266 kstat_named_t arcstat_evict_l2_ineligible
;
267 kstat_named_t arcstat_hash_elements
;
268 kstat_named_t arcstat_hash_elements_max
;
269 kstat_named_t arcstat_hash_collisions
;
270 kstat_named_t arcstat_hash_chains
;
271 kstat_named_t arcstat_hash_chain_max
;
272 kstat_named_t arcstat_p
;
273 kstat_named_t arcstat_c
;
274 kstat_named_t arcstat_c_min
;
275 kstat_named_t arcstat_c_max
;
276 kstat_named_t arcstat_size
;
277 kstat_named_t arcstat_hdr_size
;
278 kstat_named_t arcstat_data_size
;
279 kstat_named_t arcstat_other_size
;
280 kstat_named_t arcstat_anon_size
;
281 kstat_named_t arcstat_anon_evict_data
;
282 kstat_named_t arcstat_anon_evict_metadata
;
283 kstat_named_t arcstat_mru_size
;
284 kstat_named_t arcstat_mru_evict_data
;
285 kstat_named_t arcstat_mru_evict_metadata
;
286 kstat_named_t arcstat_mru_ghost_size
;
287 kstat_named_t arcstat_mru_ghost_evict_data
;
288 kstat_named_t arcstat_mru_ghost_evict_metadata
;
289 kstat_named_t arcstat_mfu_size
;
290 kstat_named_t arcstat_mfu_evict_data
;
291 kstat_named_t arcstat_mfu_evict_metadata
;
292 kstat_named_t arcstat_mfu_ghost_size
;
293 kstat_named_t arcstat_mfu_ghost_evict_data
;
294 kstat_named_t arcstat_mfu_ghost_evict_metadata
;
295 kstat_named_t arcstat_l2_hits
;
296 kstat_named_t arcstat_l2_misses
;
297 kstat_named_t arcstat_l2_feeds
;
298 kstat_named_t arcstat_l2_rw_clash
;
299 kstat_named_t arcstat_l2_read_bytes
;
300 kstat_named_t arcstat_l2_write_bytes
;
301 kstat_named_t arcstat_l2_writes_sent
;
302 kstat_named_t arcstat_l2_writes_done
;
303 kstat_named_t arcstat_l2_writes_error
;
304 kstat_named_t arcstat_l2_writes_hdr_miss
;
305 kstat_named_t arcstat_l2_evict_lock_retry
;
306 kstat_named_t arcstat_l2_evict_reading
;
307 kstat_named_t arcstat_l2_free_on_write
;
308 kstat_named_t arcstat_l2_abort_lowmem
;
309 kstat_named_t arcstat_l2_cksum_bad
;
310 kstat_named_t arcstat_l2_io_error
;
311 kstat_named_t arcstat_l2_size
;
312 kstat_named_t arcstat_l2_asize
;
313 kstat_named_t arcstat_l2_hdr_size
;
314 kstat_named_t arcstat_l2_compress_successes
;
315 kstat_named_t arcstat_l2_compress_zeros
;
316 kstat_named_t arcstat_l2_compress_failures
;
317 kstat_named_t arcstat_memory_throttle_count
;
318 kstat_named_t arcstat_duplicate_buffers
;
319 kstat_named_t arcstat_duplicate_buffers_size
;
320 kstat_named_t arcstat_duplicate_reads
;
321 kstat_named_t arcstat_memory_direct_count
;
322 kstat_named_t arcstat_memory_indirect_count
;
323 kstat_named_t arcstat_no_grow
;
324 kstat_named_t arcstat_tempreserve
;
325 kstat_named_t arcstat_loaned_bytes
;
326 kstat_named_t arcstat_prune
;
327 kstat_named_t arcstat_meta_used
;
328 kstat_named_t arcstat_meta_limit
;
329 kstat_named_t arcstat_meta_max
;
332 static arc_stats_t arc_stats
= {
333 { "hits", KSTAT_DATA_UINT64
},
334 { "misses", KSTAT_DATA_UINT64
},
335 { "demand_data_hits", KSTAT_DATA_UINT64
},
336 { "demand_data_misses", KSTAT_DATA_UINT64
},
337 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
338 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
339 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
340 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
341 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
342 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
343 { "mru_hits", KSTAT_DATA_UINT64
},
344 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
345 { "mfu_hits", KSTAT_DATA_UINT64
},
346 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
347 { "deleted", KSTAT_DATA_UINT64
},
348 { "recycle_miss", KSTAT_DATA_UINT64
},
349 { "mutex_miss", KSTAT_DATA_UINT64
},
350 { "evict_skip", KSTAT_DATA_UINT64
},
351 { "evict_l2_cached", KSTAT_DATA_UINT64
},
352 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
353 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
354 { "hash_elements", KSTAT_DATA_UINT64
},
355 { "hash_elements_max", KSTAT_DATA_UINT64
},
356 { "hash_collisions", KSTAT_DATA_UINT64
},
357 { "hash_chains", KSTAT_DATA_UINT64
},
358 { "hash_chain_max", KSTAT_DATA_UINT64
},
359 { "p", KSTAT_DATA_UINT64
},
360 { "c", KSTAT_DATA_UINT64
},
361 { "c_min", KSTAT_DATA_UINT64
},
362 { "c_max", KSTAT_DATA_UINT64
},
363 { "size", KSTAT_DATA_UINT64
},
364 { "hdr_size", KSTAT_DATA_UINT64
},
365 { "data_size", KSTAT_DATA_UINT64
},
366 { "other_size", KSTAT_DATA_UINT64
},
367 { "anon_size", KSTAT_DATA_UINT64
},
368 { "anon_evict_data", KSTAT_DATA_UINT64
},
369 { "anon_evict_metadata", KSTAT_DATA_UINT64
},
370 { "mru_size", KSTAT_DATA_UINT64
},
371 { "mru_evict_data", KSTAT_DATA_UINT64
},
372 { "mru_evict_metadata", KSTAT_DATA_UINT64
},
373 { "mru_ghost_size", KSTAT_DATA_UINT64
},
374 { "mru_ghost_evict_data", KSTAT_DATA_UINT64
},
375 { "mru_ghost_evict_metadata", KSTAT_DATA_UINT64
},
376 { "mfu_size", KSTAT_DATA_UINT64
},
377 { "mfu_evict_data", KSTAT_DATA_UINT64
},
378 { "mfu_evict_metadata", KSTAT_DATA_UINT64
},
379 { "mfu_ghost_size", KSTAT_DATA_UINT64
},
380 { "mfu_ghost_evict_data", KSTAT_DATA_UINT64
},
381 { "mfu_ghost_evict_metadata", KSTAT_DATA_UINT64
},
382 { "l2_hits", KSTAT_DATA_UINT64
},
383 { "l2_misses", KSTAT_DATA_UINT64
},
384 { "l2_feeds", KSTAT_DATA_UINT64
},
385 { "l2_rw_clash", KSTAT_DATA_UINT64
},
386 { "l2_read_bytes", KSTAT_DATA_UINT64
},
387 { "l2_write_bytes", KSTAT_DATA_UINT64
},
388 { "l2_writes_sent", KSTAT_DATA_UINT64
},
389 { "l2_writes_done", KSTAT_DATA_UINT64
},
390 { "l2_writes_error", KSTAT_DATA_UINT64
},
391 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64
},
392 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
393 { "l2_evict_reading", KSTAT_DATA_UINT64
},
394 { "l2_free_on_write", KSTAT_DATA_UINT64
},
395 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
396 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
397 { "l2_io_error", KSTAT_DATA_UINT64
},
398 { "l2_size", KSTAT_DATA_UINT64
},
399 { "l2_asize", KSTAT_DATA_UINT64
},
400 { "l2_hdr_size", KSTAT_DATA_UINT64
},
401 { "l2_compress_successes", KSTAT_DATA_UINT64
},
402 { "l2_compress_zeros", KSTAT_DATA_UINT64
},
403 { "l2_compress_failures", KSTAT_DATA_UINT64
},
404 { "memory_throttle_count", KSTAT_DATA_UINT64
},
405 { "duplicate_buffers", KSTAT_DATA_UINT64
},
406 { "duplicate_buffers_size", KSTAT_DATA_UINT64
},
407 { "duplicate_reads", KSTAT_DATA_UINT64
},
408 { "memory_direct_count", KSTAT_DATA_UINT64
},
409 { "memory_indirect_count", KSTAT_DATA_UINT64
},
410 { "arc_no_grow", KSTAT_DATA_UINT64
},
411 { "arc_tempreserve", KSTAT_DATA_UINT64
},
412 { "arc_loaned_bytes", KSTAT_DATA_UINT64
},
413 { "arc_prune", KSTAT_DATA_UINT64
},
414 { "arc_meta_used", KSTAT_DATA_UINT64
},
415 { "arc_meta_limit", KSTAT_DATA_UINT64
},
416 { "arc_meta_max", KSTAT_DATA_UINT64
},
419 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
421 #define ARCSTAT_INCR(stat, val) \
422 atomic_add_64(&arc_stats.stat.value.ui64, (val));
424 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
425 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
427 #define ARCSTAT_MAX(stat, val) { \
429 while ((val) > (m = arc_stats.stat.value.ui64) && \
430 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
434 #define ARCSTAT_MAXSTAT(stat) \
435 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
438 * We define a macro to allow ARC hits/misses to be easily broken down by
439 * two separate conditions, giving a total of four different subtypes for
440 * each of hits and misses (so eight statistics total).
442 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
445 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
447 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
451 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
453 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
458 static arc_state_t
*arc_anon
;
459 static arc_state_t
*arc_mru
;
460 static arc_state_t
*arc_mru_ghost
;
461 static arc_state_t
*arc_mfu
;
462 static arc_state_t
*arc_mfu_ghost
;
463 static arc_state_t
*arc_l2c_only
;
466 * There are several ARC variables that are critical to export as kstats --
467 * but we don't want to have to grovel around in the kstat whenever we wish to
468 * manipulate them. For these variables, we therefore define them to be in
469 * terms of the statistic variable. This assures that we are not introducing
470 * the possibility of inconsistency by having shadow copies of the variables,
471 * while still allowing the code to be readable.
473 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
474 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
475 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
476 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
477 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
478 #define arc_no_grow ARCSTAT(arcstat_no_grow)
479 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
480 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
481 #define arc_meta_used ARCSTAT(arcstat_meta_used)
482 #define arc_meta_limit ARCSTAT(arcstat_meta_limit)
483 #define arc_meta_max ARCSTAT(arcstat_meta_max)
485 #define L2ARC_IS_VALID_COMPRESS(_c_) \
486 ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY)
488 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t
;
490 typedef struct arc_callback arc_callback_t
;
492 struct arc_callback
{
494 arc_done_func_t
*acb_done
;
496 zio_t
*acb_zio_dummy
;
497 arc_callback_t
*acb_next
;
500 typedef struct arc_write_callback arc_write_callback_t
;
502 struct arc_write_callback
{
504 arc_done_func_t
*awcb_ready
;
505 arc_done_func_t
*awcb_done
;
510 /* protected by hash lock */
515 kmutex_t b_freeze_lock
;
516 zio_cksum_t
*b_freeze_cksum
;
518 arc_buf_hdr_t
*b_hash_next
;
523 arc_callback_t
*b_acb
;
527 arc_buf_contents_t b_type
;
531 /* protected by arc state mutex */
532 arc_state_t
*b_state
;
533 list_node_t b_arc_node
;
535 /* updated atomically */
536 clock_t b_arc_access
;
538 /* self protecting */
541 l2arc_buf_hdr_t
*b_l2hdr
;
542 list_node_t b_l2node
;
545 static list_t arc_prune_list
;
546 static kmutex_t arc_prune_mtx
;
547 static arc_buf_t
*arc_eviction_list
;
548 static kmutex_t arc_eviction_mtx
;
549 static arc_buf_hdr_t arc_eviction_hdr
;
550 static void arc_get_data_buf(arc_buf_t
*buf
);
551 static void arc_access(arc_buf_hdr_t
*buf
, kmutex_t
*hash_lock
);
552 static int arc_evict_needed(arc_buf_contents_t type
);
553 static void arc_evict_ghost(arc_state_t
*state
, uint64_t spa
, int64_t bytes
);
555 static boolean_t
l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*ab
);
557 #define GHOST_STATE(state) \
558 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
559 (state) == arc_l2c_only)
562 * Private ARC flags. These flags are private ARC only flags that will show up
563 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
564 * be passed in as arc_flags in things like arc_read. However, these flags
565 * should never be passed and should only be set by ARC code. When adding new
566 * public flags, make sure not to smash the private ones.
569 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
570 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
571 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
572 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
573 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
574 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
575 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
576 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
577 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
578 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
580 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
581 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
582 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
583 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
584 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
585 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
586 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
587 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
588 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
589 (hdr)->b_l2hdr != NULL)
590 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
591 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
592 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
598 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
599 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
602 * Hash table routines
605 #define HT_LOCK_ALIGN 64
606 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
611 unsigned char pad
[HT_LOCK_PAD
];
615 #define BUF_LOCKS 256
616 typedef struct buf_hash_table
{
618 arc_buf_hdr_t
**ht_table
;
619 struct ht_lock ht_locks
[BUF_LOCKS
];
622 static buf_hash_table_t buf_hash_table
;
624 #define BUF_HASH_INDEX(spa, dva, birth) \
625 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
626 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
627 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
628 #define HDR_LOCK(hdr) \
629 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
631 uint64_t zfs_crc64_table
[256];
637 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
638 #define L2ARC_HEADROOM 2 /* num of writes */
640 * If we discover during ARC scan any buffers to be compressed, we boost
641 * our headroom for the next scanning cycle by this percentage multiple.
643 #define L2ARC_HEADROOM_BOOST 200
644 #define L2ARC_FEED_SECS 1 /* caching interval secs */
645 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
647 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
648 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
651 * L2ARC Performance Tunables
653 unsigned long l2arc_write_max
= L2ARC_WRITE_SIZE
; /* def max write size */
654 unsigned long l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra warmup write */
655 unsigned long l2arc_headroom
= L2ARC_HEADROOM
; /* # of dev writes */
656 unsigned long l2arc_headroom_boost
= L2ARC_HEADROOM_BOOST
;
657 unsigned long l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
658 unsigned long l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval msecs */
659 int l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
660 int l2arc_nocompress
= B_FALSE
; /* don't compress bufs */
661 int l2arc_feed_again
= B_TRUE
; /* turbo warmup */
662 int l2arc_norw
= B_FALSE
; /* no reads during writes */
667 typedef struct l2arc_dev
{
668 vdev_t
*l2ad_vdev
; /* vdev */
669 spa_t
*l2ad_spa
; /* spa */
670 uint64_t l2ad_hand
; /* next write location */
671 uint64_t l2ad_start
; /* first addr on device */
672 uint64_t l2ad_end
; /* last addr on device */
673 uint64_t l2ad_evict
; /* last addr eviction reached */
674 boolean_t l2ad_first
; /* first sweep through */
675 boolean_t l2ad_writing
; /* currently writing */
676 list_t
*l2ad_buflist
; /* buffer list */
677 list_node_t l2ad_node
; /* device list node */
680 static list_t L2ARC_dev_list
; /* device list */
681 static list_t
*l2arc_dev_list
; /* device list pointer */
682 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
683 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
684 static kmutex_t l2arc_buflist_mtx
; /* mutex for all buflists */
685 static list_t L2ARC_free_on_write
; /* free after write buf list */
686 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
687 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
688 static uint64_t l2arc_ndev
; /* number of devices */
690 typedef struct l2arc_read_callback
{
691 arc_buf_t
*l2rcb_buf
; /* read buffer */
692 spa_t
*l2rcb_spa
; /* spa */
693 blkptr_t l2rcb_bp
; /* original blkptr */
694 zbookmark_t l2rcb_zb
; /* original bookmark */
695 int l2rcb_flags
; /* original flags */
696 enum zio_compress l2rcb_compress
; /* applied compress */
697 } l2arc_read_callback_t
;
699 typedef struct l2arc_write_callback
{
700 l2arc_dev_t
*l2wcb_dev
; /* device info */
701 arc_buf_hdr_t
*l2wcb_head
; /* head of write buflist */
702 } l2arc_write_callback_t
;
704 struct l2arc_buf_hdr
{
705 /* protected by arc_buf_hdr mutex */
706 l2arc_dev_t
*b_dev
; /* L2ARC device */
707 uint64_t b_daddr
; /* disk address, offset byte */
708 /* compression applied to buffer data */
709 enum zio_compress b_compress
;
710 /* real alloc'd buffer size depending on b_compress applied */
712 /* temporary buffer holder for in-flight compressed data */
716 typedef struct l2arc_data_free
{
717 /* protected by l2arc_free_on_write_mtx */
720 void (*l2df_func
)(void *, size_t);
721 list_node_t l2df_list_node
;
724 static kmutex_t l2arc_feed_thr_lock
;
725 static kcondvar_t l2arc_feed_thr_cv
;
726 static uint8_t l2arc_thread_exit
;
728 static void l2arc_read_done(zio_t
*zio
);
729 static void l2arc_hdr_stat_add(void);
730 static void l2arc_hdr_stat_remove(void);
732 static boolean_t
l2arc_compress_buf(l2arc_buf_hdr_t
*l2hdr
);
733 static void l2arc_decompress_zio(zio_t
*zio
, arc_buf_hdr_t
*hdr
,
734 enum zio_compress c
);
735 static void l2arc_release_cdata_buf(arc_buf_hdr_t
*ab
);
738 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
740 uint8_t *vdva
= (uint8_t *)dva
;
741 uint64_t crc
= -1ULL;
744 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
746 for (i
= 0; i
< sizeof (dva_t
); i
++)
747 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ vdva
[i
]) & 0xFF];
749 crc
^= (spa
>>8) ^ birth
;
754 #define BUF_EMPTY(buf) \
755 ((buf)->b_dva.dva_word[0] == 0 && \
756 (buf)->b_dva.dva_word[1] == 0 && \
759 #define BUF_EQUAL(spa, dva, birth, buf) \
760 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
761 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
762 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
765 buf_discard_identity(arc_buf_hdr_t
*hdr
)
767 hdr
->b_dva
.dva_word
[0] = 0;
768 hdr
->b_dva
.dva_word
[1] = 0;
773 static arc_buf_hdr_t
*
774 buf_hash_find(uint64_t spa
, const dva_t
*dva
, uint64_t birth
, kmutex_t
**lockp
)
776 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
777 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
780 mutex_enter(hash_lock
);
781 for (buf
= buf_hash_table
.ht_table
[idx
]; buf
!= NULL
;
782 buf
= buf
->b_hash_next
) {
783 if (BUF_EQUAL(spa
, dva
, birth
, buf
)) {
788 mutex_exit(hash_lock
);
794 * Insert an entry into the hash table. If there is already an element
795 * equal to elem in the hash table, then the already existing element
796 * will be returned and the new element will not be inserted.
797 * Otherwise returns NULL.
799 static arc_buf_hdr_t
*
800 buf_hash_insert(arc_buf_hdr_t
*buf
, kmutex_t
**lockp
)
802 uint64_t idx
= BUF_HASH_INDEX(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
);
803 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
807 ASSERT(!HDR_IN_HASH_TABLE(buf
));
809 mutex_enter(hash_lock
);
810 for (fbuf
= buf_hash_table
.ht_table
[idx
], i
= 0; fbuf
!= NULL
;
811 fbuf
= fbuf
->b_hash_next
, i
++) {
812 if (BUF_EQUAL(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
, fbuf
))
816 buf
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
817 buf_hash_table
.ht_table
[idx
] = buf
;
818 buf
->b_flags
|= ARC_IN_HASH_TABLE
;
820 /* collect some hash table performance data */
822 ARCSTAT_BUMP(arcstat_hash_collisions
);
824 ARCSTAT_BUMP(arcstat_hash_chains
);
826 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
829 ARCSTAT_BUMP(arcstat_hash_elements
);
830 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
836 buf_hash_remove(arc_buf_hdr_t
*buf
)
838 arc_buf_hdr_t
*fbuf
, **bufp
;
839 uint64_t idx
= BUF_HASH_INDEX(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
);
841 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
842 ASSERT(HDR_IN_HASH_TABLE(buf
));
844 bufp
= &buf_hash_table
.ht_table
[idx
];
845 while ((fbuf
= *bufp
) != buf
) {
846 ASSERT(fbuf
!= NULL
);
847 bufp
= &fbuf
->b_hash_next
;
849 *bufp
= buf
->b_hash_next
;
850 buf
->b_hash_next
= NULL
;
851 buf
->b_flags
&= ~ARC_IN_HASH_TABLE
;
853 /* collect some hash table performance data */
854 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
856 if (buf_hash_table
.ht_table
[idx
] &&
857 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
858 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
862 * Global data structures and functions for the buf kmem cache.
864 static kmem_cache_t
*hdr_cache
;
865 static kmem_cache_t
*buf_cache
;
872 #if defined(_KERNEL) && defined(HAVE_SPL)
873 /* Large allocations which do not require contiguous pages
874 * should be using vmem_free() in the linux kernel */
875 vmem_free(buf_hash_table
.ht_table
,
876 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
878 kmem_free(buf_hash_table
.ht_table
,
879 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
881 for (i
= 0; i
< BUF_LOCKS
; i
++)
882 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
883 kmem_cache_destroy(hdr_cache
);
884 kmem_cache_destroy(buf_cache
);
888 * Constructor callback - called when the cache is empty
889 * and a new buf is requested.
893 hdr_cons(void *vbuf
, void *unused
, int kmflag
)
895 arc_buf_hdr_t
*buf
= vbuf
;
897 bzero(buf
, sizeof (arc_buf_hdr_t
));
898 refcount_create(&buf
->b_refcnt
);
899 cv_init(&buf
->b_cv
, NULL
, CV_DEFAULT
, NULL
);
900 mutex_init(&buf
->b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
901 list_link_init(&buf
->b_arc_node
);
902 list_link_init(&buf
->b_l2node
);
903 arc_space_consume(sizeof (arc_buf_hdr_t
), ARC_SPACE_HDRS
);
910 buf_cons(void *vbuf
, void *unused
, int kmflag
)
912 arc_buf_t
*buf
= vbuf
;
914 bzero(buf
, sizeof (arc_buf_t
));
915 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
916 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
922 * Destructor callback - called when a cached buf is
923 * no longer required.
927 hdr_dest(void *vbuf
, void *unused
)
929 arc_buf_hdr_t
*buf
= vbuf
;
931 ASSERT(BUF_EMPTY(buf
));
932 refcount_destroy(&buf
->b_refcnt
);
933 cv_destroy(&buf
->b_cv
);
934 mutex_destroy(&buf
->b_freeze_lock
);
935 arc_space_return(sizeof (arc_buf_hdr_t
), ARC_SPACE_HDRS
);
940 buf_dest(void *vbuf
, void *unused
)
942 arc_buf_t
*buf
= vbuf
;
944 mutex_destroy(&buf
->b_evict_lock
);
945 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
952 uint64_t hsize
= 1ULL << 12;
956 * The hash table is big enough to fill all of physical memory
957 * with an average 64K block size. The table will take up
958 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
960 while (hsize
* 65536 < physmem
* PAGESIZE
)
963 buf_hash_table
.ht_mask
= hsize
- 1;
964 #if defined(_KERNEL) && defined(HAVE_SPL)
965 /* Large allocations which do not require contiguous pages
966 * should be using vmem_alloc() in the linux kernel */
967 buf_hash_table
.ht_table
=
968 vmem_zalloc(hsize
* sizeof (void*), KM_SLEEP
);
970 buf_hash_table
.ht_table
=
971 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
973 if (buf_hash_table
.ht_table
== NULL
) {
974 ASSERT(hsize
> (1ULL << 8));
979 hdr_cache
= kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t
),
980 0, hdr_cons
, hdr_dest
, NULL
, NULL
, NULL
, 0);
981 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
982 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
984 for (i
= 0; i
< 256; i
++)
985 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
986 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
988 for (i
= 0; i
< BUF_LOCKS
; i
++) {
989 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
990 NULL
, MUTEX_DEFAULT
, NULL
);
994 #define ARC_MINTIME (hz>>4) /* 62 ms */
997 arc_cksum_verify(arc_buf_t
*buf
)
1001 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1004 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
1005 if (buf
->b_hdr
->b_freeze_cksum
== NULL
||
1006 (buf
->b_hdr
->b_flags
& ARC_IO_ERROR
)) {
1007 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1010 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
1011 if (!ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
))
1012 panic("buffer modified while frozen!");
1013 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1017 arc_cksum_equal(arc_buf_t
*buf
)
1022 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
1023 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
1024 equal
= ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
);
1025 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1031 arc_cksum_compute(arc_buf_t
*buf
, boolean_t force
)
1033 if (!force
&& !(zfs_flags
& ZFS_DEBUG_MODIFY
))
1036 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
1037 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
1038 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1041 buf
->b_hdr
->b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
),
1043 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
,
1044 buf
->b_hdr
->b_freeze_cksum
);
1045 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1049 arc_buf_thaw(arc_buf_t
*buf
)
1051 if (zfs_flags
& ZFS_DEBUG_MODIFY
) {
1052 if (buf
->b_hdr
->b_state
!= arc_anon
)
1053 panic("modifying non-anon buffer!");
1054 if (buf
->b_hdr
->b_flags
& ARC_IO_IN_PROGRESS
)
1055 panic("modifying buffer while i/o in progress!");
1056 arc_cksum_verify(buf
);
1059 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
1060 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
1061 kmem_free(buf
->b_hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
1062 buf
->b_hdr
->b_freeze_cksum
= NULL
;
1065 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
1069 arc_buf_freeze(arc_buf_t
*buf
)
1071 kmutex_t
*hash_lock
;
1073 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
1076 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1077 mutex_enter(hash_lock
);
1079 ASSERT(buf
->b_hdr
->b_freeze_cksum
!= NULL
||
1080 buf
->b_hdr
->b_state
== arc_anon
);
1081 arc_cksum_compute(buf
, B_FALSE
);
1082 mutex_exit(hash_lock
);
1086 add_reference(arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
, void *tag
)
1088 ASSERT(MUTEX_HELD(hash_lock
));
1090 if ((refcount_add(&ab
->b_refcnt
, tag
) == 1) &&
1091 (ab
->b_state
!= arc_anon
)) {
1092 uint64_t delta
= ab
->b_size
* ab
->b_datacnt
;
1093 list_t
*list
= &ab
->b_state
->arcs_list
[ab
->b_type
];
1094 uint64_t *size
= &ab
->b_state
->arcs_lsize
[ab
->b_type
];
1096 ASSERT(!MUTEX_HELD(&ab
->b_state
->arcs_mtx
));
1097 mutex_enter(&ab
->b_state
->arcs_mtx
);
1098 ASSERT(list_link_active(&ab
->b_arc_node
));
1099 list_remove(list
, ab
);
1100 if (GHOST_STATE(ab
->b_state
)) {
1101 ASSERT0(ab
->b_datacnt
);
1102 ASSERT3P(ab
->b_buf
, ==, NULL
);
1106 ASSERT3U(*size
, >=, delta
);
1107 atomic_add_64(size
, -delta
);
1108 mutex_exit(&ab
->b_state
->arcs_mtx
);
1109 /* remove the prefetch flag if we get a reference */
1110 if (ab
->b_flags
& ARC_PREFETCH
)
1111 ab
->b_flags
&= ~ARC_PREFETCH
;
1116 remove_reference(arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
, void *tag
)
1119 arc_state_t
*state
= ab
->b_state
;
1121 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
1122 ASSERT(!GHOST_STATE(state
));
1124 if (((cnt
= refcount_remove(&ab
->b_refcnt
, tag
)) == 0) &&
1125 (state
!= arc_anon
)) {
1126 uint64_t *size
= &state
->arcs_lsize
[ab
->b_type
];
1128 ASSERT(!MUTEX_HELD(&state
->arcs_mtx
));
1129 mutex_enter(&state
->arcs_mtx
);
1130 ASSERT(!list_link_active(&ab
->b_arc_node
));
1131 list_insert_head(&state
->arcs_list
[ab
->b_type
], ab
);
1132 ASSERT(ab
->b_datacnt
> 0);
1133 atomic_add_64(size
, ab
->b_size
* ab
->b_datacnt
);
1134 mutex_exit(&state
->arcs_mtx
);
1140 * Move the supplied buffer to the indicated state. The mutex
1141 * for the buffer must be held by the caller.
1144 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
)
1146 arc_state_t
*old_state
= ab
->b_state
;
1147 int64_t refcnt
= refcount_count(&ab
->b_refcnt
);
1148 uint64_t from_delta
, to_delta
;
1150 ASSERT(MUTEX_HELD(hash_lock
));
1151 ASSERT(new_state
!= old_state
);
1152 ASSERT(refcnt
== 0 || ab
->b_datacnt
> 0);
1153 ASSERT(ab
->b_datacnt
== 0 || !GHOST_STATE(new_state
));
1154 ASSERT(ab
->b_datacnt
<= 1 || old_state
!= arc_anon
);
1156 from_delta
= to_delta
= ab
->b_datacnt
* ab
->b_size
;
1159 * If this buffer is evictable, transfer it from the
1160 * old state list to the new state list.
1163 if (old_state
!= arc_anon
) {
1164 int use_mutex
= !MUTEX_HELD(&old_state
->arcs_mtx
);
1165 uint64_t *size
= &old_state
->arcs_lsize
[ab
->b_type
];
1168 mutex_enter(&old_state
->arcs_mtx
);
1170 ASSERT(list_link_active(&ab
->b_arc_node
));
1171 list_remove(&old_state
->arcs_list
[ab
->b_type
], ab
);
1174 * If prefetching out of the ghost cache,
1175 * we will have a non-zero datacnt.
1177 if (GHOST_STATE(old_state
) && ab
->b_datacnt
== 0) {
1178 /* ghost elements have a ghost size */
1179 ASSERT(ab
->b_buf
== NULL
);
1180 from_delta
= ab
->b_size
;
1182 ASSERT3U(*size
, >=, from_delta
);
1183 atomic_add_64(size
, -from_delta
);
1186 mutex_exit(&old_state
->arcs_mtx
);
1188 if (new_state
!= arc_anon
) {
1189 int use_mutex
= !MUTEX_HELD(&new_state
->arcs_mtx
);
1190 uint64_t *size
= &new_state
->arcs_lsize
[ab
->b_type
];
1193 mutex_enter(&new_state
->arcs_mtx
);
1195 list_insert_head(&new_state
->arcs_list
[ab
->b_type
], ab
);
1197 /* ghost elements have a ghost size */
1198 if (GHOST_STATE(new_state
)) {
1199 ASSERT(ab
->b_datacnt
== 0);
1200 ASSERT(ab
->b_buf
== NULL
);
1201 to_delta
= ab
->b_size
;
1203 atomic_add_64(size
, to_delta
);
1206 mutex_exit(&new_state
->arcs_mtx
);
1210 ASSERT(!BUF_EMPTY(ab
));
1211 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(ab
))
1212 buf_hash_remove(ab
);
1214 /* adjust state sizes */
1216 atomic_add_64(&new_state
->arcs_size
, to_delta
);
1218 ASSERT3U(old_state
->arcs_size
, >=, from_delta
);
1219 atomic_add_64(&old_state
->arcs_size
, -from_delta
);
1221 ab
->b_state
= new_state
;
1223 /* adjust l2arc hdr stats */
1224 if (new_state
== arc_l2c_only
)
1225 l2arc_hdr_stat_add();
1226 else if (old_state
== arc_l2c_only
)
1227 l2arc_hdr_stat_remove();
1231 arc_space_consume(uint64_t space
, arc_space_type_t type
)
1233 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1238 case ARC_SPACE_DATA
:
1239 ARCSTAT_INCR(arcstat_data_size
, space
);
1241 case ARC_SPACE_OTHER
:
1242 ARCSTAT_INCR(arcstat_other_size
, space
);
1244 case ARC_SPACE_HDRS
:
1245 ARCSTAT_INCR(arcstat_hdr_size
, space
);
1247 case ARC_SPACE_L2HDRS
:
1248 ARCSTAT_INCR(arcstat_l2_hdr_size
, space
);
1252 atomic_add_64(&arc_meta_used
, space
);
1253 atomic_add_64(&arc_size
, space
);
1257 arc_space_return(uint64_t space
, arc_space_type_t type
)
1259 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1264 case ARC_SPACE_DATA
:
1265 ARCSTAT_INCR(arcstat_data_size
, -space
);
1267 case ARC_SPACE_OTHER
:
1268 ARCSTAT_INCR(arcstat_other_size
, -space
);
1270 case ARC_SPACE_HDRS
:
1271 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
1273 case ARC_SPACE_L2HDRS
:
1274 ARCSTAT_INCR(arcstat_l2_hdr_size
, -space
);
1278 ASSERT(arc_meta_used
>= space
);
1279 if (arc_meta_max
< arc_meta_used
)
1280 arc_meta_max
= arc_meta_used
;
1281 atomic_add_64(&arc_meta_used
, -space
);
1282 ASSERT(arc_size
>= space
);
1283 atomic_add_64(&arc_size
, -space
);
1287 arc_buf_alloc(spa_t
*spa
, int size
, void *tag
, arc_buf_contents_t type
)
1292 ASSERT3U(size
, >, 0);
1293 hdr
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
1294 ASSERT(BUF_EMPTY(hdr
));
1297 hdr
->b_spa
= spa_load_guid(spa
);
1298 hdr
->b_state
= arc_anon
;
1299 hdr
->b_arc_access
= 0;
1300 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1303 buf
->b_efunc
= NULL
;
1304 buf
->b_private
= NULL
;
1307 arc_get_data_buf(buf
);
1310 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1311 (void) refcount_add(&hdr
->b_refcnt
, tag
);
1316 static char *arc_onloan_tag
= "onloan";
1319 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1320 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1321 * buffers must be returned to the arc before they can be used by the DMU or
1325 arc_loan_buf(spa_t
*spa
, int size
)
1329 buf
= arc_buf_alloc(spa
, size
, arc_onloan_tag
, ARC_BUFC_DATA
);
1331 atomic_add_64(&arc_loaned_bytes
, size
);
1336 * Return a loaned arc buffer to the arc.
1339 arc_return_buf(arc_buf_t
*buf
, void *tag
)
1341 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1343 ASSERT(buf
->b_data
!= NULL
);
1344 (void) refcount_add(&hdr
->b_refcnt
, tag
);
1345 (void) refcount_remove(&hdr
->b_refcnt
, arc_onloan_tag
);
1347 atomic_add_64(&arc_loaned_bytes
, -hdr
->b_size
);
1350 /* Detach an arc_buf from a dbuf (tag) */
1352 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
1356 ASSERT(buf
->b_data
!= NULL
);
1358 (void) refcount_add(&hdr
->b_refcnt
, arc_onloan_tag
);
1359 (void) refcount_remove(&hdr
->b_refcnt
, tag
);
1360 buf
->b_efunc
= NULL
;
1361 buf
->b_private
= NULL
;
1363 atomic_add_64(&arc_loaned_bytes
, hdr
->b_size
);
1367 arc_buf_clone(arc_buf_t
*from
)
1370 arc_buf_hdr_t
*hdr
= from
->b_hdr
;
1371 uint64_t size
= hdr
->b_size
;
1373 ASSERT(hdr
->b_state
!= arc_anon
);
1375 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1378 buf
->b_efunc
= NULL
;
1379 buf
->b_private
= NULL
;
1380 buf
->b_next
= hdr
->b_buf
;
1382 arc_get_data_buf(buf
);
1383 bcopy(from
->b_data
, buf
->b_data
, size
);
1386 * This buffer already exists in the arc so create a duplicate
1387 * copy for the caller. If the buffer is associated with user data
1388 * then track the size and number of duplicates. These stats will be
1389 * updated as duplicate buffers are created and destroyed.
1391 if (hdr
->b_type
== ARC_BUFC_DATA
) {
1392 ARCSTAT_BUMP(arcstat_duplicate_buffers
);
1393 ARCSTAT_INCR(arcstat_duplicate_buffers_size
, size
);
1395 hdr
->b_datacnt
+= 1;
1400 arc_buf_add_ref(arc_buf_t
*buf
, void* tag
)
1403 kmutex_t
*hash_lock
;
1406 * Check to see if this buffer is evicted. Callers
1407 * must verify b_data != NULL to know if the add_ref
1410 mutex_enter(&buf
->b_evict_lock
);
1411 if (buf
->b_data
== NULL
) {
1412 mutex_exit(&buf
->b_evict_lock
);
1415 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1416 mutex_enter(hash_lock
);
1418 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1419 mutex_exit(&buf
->b_evict_lock
);
1421 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
1422 add_reference(hdr
, hash_lock
, tag
);
1423 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
1424 arc_access(hdr
, hash_lock
);
1425 mutex_exit(hash_lock
);
1426 ARCSTAT_BUMP(arcstat_hits
);
1427 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
1428 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
1429 data
, metadata
, hits
);
1433 * Free the arc data buffer. If it is an l2arc write in progress,
1434 * the buffer is placed on l2arc_free_on_write to be freed later.
1437 arc_buf_data_free(arc_buf_hdr_t
*hdr
, void (*free_func
)(void *, size_t),
1438 void *data
, size_t size
)
1440 if (HDR_L2_WRITING(hdr
)) {
1441 l2arc_data_free_t
*df
;
1442 df
= kmem_alloc(sizeof (l2arc_data_free_t
), KM_PUSHPAGE
);
1443 df
->l2df_data
= data
;
1444 df
->l2df_size
= size
;
1445 df
->l2df_func
= free_func
;
1446 mutex_enter(&l2arc_free_on_write_mtx
);
1447 list_insert_head(l2arc_free_on_write
, df
);
1448 mutex_exit(&l2arc_free_on_write_mtx
);
1449 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
1451 free_func(data
, size
);
1456 arc_buf_destroy(arc_buf_t
*buf
, boolean_t recycle
, boolean_t all
)
1460 /* free up data associated with the buf */
1462 arc_state_t
*state
= buf
->b_hdr
->b_state
;
1463 uint64_t size
= buf
->b_hdr
->b_size
;
1464 arc_buf_contents_t type
= buf
->b_hdr
->b_type
;
1466 arc_cksum_verify(buf
);
1469 if (type
== ARC_BUFC_METADATA
) {
1470 arc_buf_data_free(buf
->b_hdr
, zio_buf_free
,
1472 arc_space_return(size
, ARC_SPACE_DATA
);
1474 ASSERT(type
== ARC_BUFC_DATA
);
1475 arc_buf_data_free(buf
->b_hdr
,
1476 zio_data_buf_free
, buf
->b_data
, size
);
1477 ARCSTAT_INCR(arcstat_data_size
, -size
);
1478 atomic_add_64(&arc_size
, -size
);
1481 if (list_link_active(&buf
->b_hdr
->b_arc_node
)) {
1482 uint64_t *cnt
= &state
->arcs_lsize
[type
];
1484 ASSERT(refcount_is_zero(&buf
->b_hdr
->b_refcnt
));
1485 ASSERT(state
!= arc_anon
);
1487 ASSERT3U(*cnt
, >=, size
);
1488 atomic_add_64(cnt
, -size
);
1490 ASSERT3U(state
->arcs_size
, >=, size
);
1491 atomic_add_64(&state
->arcs_size
, -size
);
1495 * If we're destroying a duplicate buffer make sure
1496 * that the appropriate statistics are updated.
1498 if (buf
->b_hdr
->b_datacnt
> 1 &&
1499 buf
->b_hdr
->b_type
== ARC_BUFC_DATA
) {
1500 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers
);
1501 ARCSTAT_INCR(arcstat_duplicate_buffers_size
, -size
);
1503 ASSERT(buf
->b_hdr
->b_datacnt
> 0);
1504 buf
->b_hdr
->b_datacnt
-= 1;
1507 /* only remove the buf if requested */
1511 /* remove the buf from the hdr list */
1512 for (bufp
= &buf
->b_hdr
->b_buf
; *bufp
!= buf
; bufp
= &(*bufp
)->b_next
)
1514 *bufp
= buf
->b_next
;
1517 ASSERT(buf
->b_efunc
== NULL
);
1519 /* clean up the buf */
1521 kmem_cache_free(buf_cache
, buf
);
1525 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
1527 l2arc_buf_hdr_t
*l2hdr
= hdr
->b_l2hdr
;
1529 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1530 ASSERT3P(hdr
->b_state
, ==, arc_anon
);
1531 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1533 if (l2hdr
!= NULL
) {
1534 boolean_t buflist_held
= MUTEX_HELD(&l2arc_buflist_mtx
);
1536 * To prevent arc_free() and l2arc_evict() from
1537 * attempting to free the same buffer at the same time,
1538 * a FREE_IN_PROGRESS flag is given to arc_free() to
1539 * give it priority. l2arc_evict() can't destroy this
1540 * header while we are waiting on l2arc_buflist_mtx.
1542 * The hdr may be removed from l2ad_buflist before we
1543 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1545 if (!buflist_held
) {
1546 mutex_enter(&l2arc_buflist_mtx
);
1547 l2hdr
= hdr
->b_l2hdr
;
1550 if (l2hdr
!= NULL
) {
1551 list_remove(l2hdr
->b_dev
->l2ad_buflist
, hdr
);
1552 ARCSTAT_INCR(arcstat_l2_size
, -hdr
->b_size
);
1553 ARCSTAT_INCR(arcstat_l2_asize
, -l2hdr
->b_asize
);
1554 kmem_free(l2hdr
, sizeof (l2arc_buf_hdr_t
));
1555 arc_space_return(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
1556 if (hdr
->b_state
== arc_l2c_only
)
1557 l2arc_hdr_stat_remove();
1558 hdr
->b_l2hdr
= NULL
;
1562 mutex_exit(&l2arc_buflist_mtx
);
1565 if (!BUF_EMPTY(hdr
)) {
1566 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1567 buf_discard_identity(hdr
);
1569 while (hdr
->b_buf
) {
1570 arc_buf_t
*buf
= hdr
->b_buf
;
1573 mutex_enter(&arc_eviction_mtx
);
1574 mutex_enter(&buf
->b_evict_lock
);
1575 ASSERT(buf
->b_hdr
!= NULL
);
1576 arc_buf_destroy(hdr
->b_buf
, FALSE
, FALSE
);
1577 hdr
->b_buf
= buf
->b_next
;
1578 buf
->b_hdr
= &arc_eviction_hdr
;
1579 buf
->b_next
= arc_eviction_list
;
1580 arc_eviction_list
= buf
;
1581 mutex_exit(&buf
->b_evict_lock
);
1582 mutex_exit(&arc_eviction_mtx
);
1584 arc_buf_destroy(hdr
->b_buf
, FALSE
, TRUE
);
1587 if (hdr
->b_freeze_cksum
!= NULL
) {
1588 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
1589 hdr
->b_freeze_cksum
= NULL
;
1592 ASSERT(!list_link_active(&hdr
->b_arc_node
));
1593 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
1594 ASSERT3P(hdr
->b_acb
, ==, NULL
);
1595 kmem_cache_free(hdr_cache
, hdr
);
1599 arc_buf_free(arc_buf_t
*buf
, void *tag
)
1601 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1602 int hashed
= hdr
->b_state
!= arc_anon
;
1604 ASSERT(buf
->b_efunc
== NULL
);
1605 ASSERT(buf
->b_data
!= NULL
);
1608 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
1610 mutex_enter(hash_lock
);
1612 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1614 (void) remove_reference(hdr
, hash_lock
, tag
);
1615 if (hdr
->b_datacnt
> 1) {
1616 arc_buf_destroy(buf
, FALSE
, TRUE
);
1618 ASSERT(buf
== hdr
->b_buf
);
1619 ASSERT(buf
->b_efunc
== NULL
);
1620 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
1622 mutex_exit(hash_lock
);
1623 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
1626 * We are in the middle of an async write. Don't destroy
1627 * this buffer unless the write completes before we finish
1628 * decrementing the reference count.
1630 mutex_enter(&arc_eviction_mtx
);
1631 (void) remove_reference(hdr
, NULL
, tag
);
1632 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1633 destroy_hdr
= !HDR_IO_IN_PROGRESS(hdr
);
1634 mutex_exit(&arc_eviction_mtx
);
1636 arc_hdr_destroy(hdr
);
1638 if (remove_reference(hdr
, NULL
, tag
) > 0)
1639 arc_buf_destroy(buf
, FALSE
, TRUE
);
1641 arc_hdr_destroy(hdr
);
1646 arc_buf_remove_ref(arc_buf_t
*buf
, void* tag
)
1648 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1649 kmutex_t
*hash_lock
= NULL
;
1650 int no_callback
= (buf
->b_efunc
== NULL
);
1652 if (hdr
->b_state
== arc_anon
) {
1653 ASSERT(hdr
->b_datacnt
== 1);
1654 arc_buf_free(buf
, tag
);
1655 return (no_callback
);
1658 hash_lock
= HDR_LOCK(hdr
);
1659 mutex_enter(hash_lock
);
1661 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1662 ASSERT(hdr
->b_state
!= arc_anon
);
1663 ASSERT(buf
->b_data
!= NULL
);
1665 (void) remove_reference(hdr
, hash_lock
, tag
);
1666 if (hdr
->b_datacnt
> 1) {
1668 arc_buf_destroy(buf
, FALSE
, TRUE
);
1669 } else if (no_callback
) {
1670 ASSERT(hdr
->b_buf
== buf
&& buf
->b_next
== NULL
);
1671 ASSERT(buf
->b_efunc
== NULL
);
1672 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
1674 ASSERT(no_callback
|| hdr
->b_datacnt
> 1 ||
1675 refcount_is_zero(&hdr
->b_refcnt
));
1676 mutex_exit(hash_lock
);
1677 return (no_callback
);
1681 arc_buf_size(arc_buf_t
*buf
)
1683 return (buf
->b_hdr
->b_size
);
1687 * Called from the DMU to determine if the current buffer should be
1688 * evicted. In order to ensure proper locking, the eviction must be initiated
1689 * from the DMU. Return true if the buffer is associated with user data and
1690 * duplicate buffers still exist.
1693 arc_buf_eviction_needed(arc_buf_t
*buf
)
1696 boolean_t evict_needed
= B_FALSE
;
1698 if (zfs_disable_dup_eviction
)
1701 mutex_enter(&buf
->b_evict_lock
);
1705 * We are in arc_do_user_evicts(); let that function
1706 * perform the eviction.
1708 ASSERT(buf
->b_data
== NULL
);
1709 mutex_exit(&buf
->b_evict_lock
);
1711 } else if (buf
->b_data
== NULL
) {
1713 * We have already been added to the arc eviction list;
1714 * recommend eviction.
1716 ASSERT3P(hdr
, ==, &arc_eviction_hdr
);
1717 mutex_exit(&buf
->b_evict_lock
);
1721 if (hdr
->b_datacnt
> 1 && hdr
->b_type
== ARC_BUFC_DATA
)
1722 evict_needed
= B_TRUE
;
1724 mutex_exit(&buf
->b_evict_lock
);
1725 return (evict_needed
);
1729 * Evict buffers from list until we've removed the specified number of
1730 * bytes. Move the removed buffers to the appropriate evict state.
1731 * If the recycle flag is set, then attempt to "recycle" a buffer:
1732 * - look for a buffer to evict that is `bytes' long.
1733 * - return the data block from this buffer rather than freeing it.
1734 * This flag is used by callers that are trying to make space for a
1735 * new buffer in a full arc cache.
1737 * This function makes a "best effort". It skips over any buffers
1738 * it can't get a hash_lock on, and so may not catch all candidates.
1739 * It may also return without evicting as much space as requested.
1742 arc_evict(arc_state_t
*state
, uint64_t spa
, int64_t bytes
, boolean_t recycle
,
1743 arc_buf_contents_t type
)
1745 arc_state_t
*evicted_state
;
1746 uint64_t bytes_evicted
= 0, skipped
= 0, missed
= 0;
1747 arc_buf_hdr_t
*ab
, *ab_prev
= NULL
;
1748 list_t
*list
= &state
->arcs_list
[type
];
1749 kmutex_t
*hash_lock
;
1750 boolean_t have_lock
;
1751 void *stolen
= NULL
;
1753 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
1755 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
1757 mutex_enter(&state
->arcs_mtx
);
1758 mutex_enter(&evicted_state
->arcs_mtx
);
1760 for (ab
= list_tail(list
); ab
; ab
= ab_prev
) {
1761 ab_prev
= list_prev(list
, ab
);
1762 /* prefetch buffers have a minimum lifespan */
1763 if (HDR_IO_IN_PROGRESS(ab
) ||
1764 (spa
&& ab
->b_spa
!= spa
) ||
1765 (ab
->b_flags
& (ARC_PREFETCH
|ARC_INDIRECT
) &&
1766 ddi_get_lbolt() - ab
->b_arc_access
<
1767 zfs_arc_min_prefetch_lifespan
)) {
1771 /* "lookahead" for better eviction candidate */
1772 if (recycle
&& ab
->b_size
!= bytes
&&
1773 ab_prev
&& ab_prev
->b_size
== bytes
)
1775 hash_lock
= HDR_LOCK(ab
);
1776 have_lock
= MUTEX_HELD(hash_lock
);
1777 if (have_lock
|| mutex_tryenter(hash_lock
)) {
1778 ASSERT0(refcount_count(&ab
->b_refcnt
));
1779 ASSERT(ab
->b_datacnt
> 0);
1781 arc_buf_t
*buf
= ab
->b_buf
;
1782 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
1787 bytes_evicted
+= ab
->b_size
;
1788 if (recycle
&& ab
->b_type
== type
&&
1789 ab
->b_size
== bytes
&&
1790 !HDR_L2_WRITING(ab
)) {
1791 stolen
= buf
->b_data
;
1796 mutex_enter(&arc_eviction_mtx
);
1797 arc_buf_destroy(buf
,
1798 buf
->b_data
== stolen
, FALSE
);
1799 ab
->b_buf
= buf
->b_next
;
1800 buf
->b_hdr
= &arc_eviction_hdr
;
1801 buf
->b_next
= arc_eviction_list
;
1802 arc_eviction_list
= buf
;
1803 mutex_exit(&arc_eviction_mtx
);
1804 mutex_exit(&buf
->b_evict_lock
);
1806 mutex_exit(&buf
->b_evict_lock
);
1807 arc_buf_destroy(buf
,
1808 buf
->b_data
== stolen
, TRUE
);
1813 ARCSTAT_INCR(arcstat_evict_l2_cached
,
1816 if (l2arc_write_eligible(ab
->b_spa
, ab
)) {
1817 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
1821 arcstat_evict_l2_ineligible
,
1826 if (ab
->b_datacnt
== 0) {
1827 arc_change_state(evicted_state
, ab
, hash_lock
);
1828 ASSERT(HDR_IN_HASH_TABLE(ab
));
1829 ab
->b_flags
|= ARC_IN_HASH_TABLE
;
1830 ab
->b_flags
&= ~ARC_BUF_AVAILABLE
;
1831 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, ab
);
1834 mutex_exit(hash_lock
);
1835 if (bytes
>= 0 && bytes_evicted
>= bytes
)
1842 mutex_exit(&evicted_state
->arcs_mtx
);
1843 mutex_exit(&state
->arcs_mtx
);
1845 if (bytes_evicted
< bytes
)
1846 dprintf("only evicted %lld bytes from %x\n",
1847 (longlong_t
)bytes_evicted
, state
);
1850 ARCSTAT_INCR(arcstat_evict_skip
, skipped
);
1853 ARCSTAT_INCR(arcstat_mutex_miss
, missed
);
1856 * We have just evicted some date into the ghost state, make
1857 * sure we also adjust the ghost state size if necessary.
1860 arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
> arc_c
) {
1861 int64_t mru_over
= arc_anon
->arcs_size
+ arc_mru
->arcs_size
+
1862 arc_mru_ghost
->arcs_size
- arc_c
;
1864 if (mru_over
> 0 && arc_mru_ghost
->arcs_lsize
[type
] > 0) {
1866 MIN(arc_mru_ghost
->arcs_lsize
[type
], mru_over
);
1867 arc_evict_ghost(arc_mru_ghost
, 0, todelete
);
1868 } else if (arc_mfu_ghost
->arcs_lsize
[type
] > 0) {
1869 int64_t todelete
= MIN(arc_mfu_ghost
->arcs_lsize
[type
],
1870 arc_mru_ghost
->arcs_size
+
1871 arc_mfu_ghost
->arcs_size
- arc_c
);
1872 arc_evict_ghost(arc_mfu_ghost
, 0, todelete
);
1880 * Remove buffers from list until we've removed the specified number of
1881 * bytes. Destroy the buffers that are removed.
1884 arc_evict_ghost(arc_state_t
*state
, uint64_t spa
, int64_t bytes
)
1886 arc_buf_hdr_t
*ab
, *ab_prev
;
1887 arc_buf_hdr_t marker
;
1888 list_t
*list
= &state
->arcs_list
[ARC_BUFC_DATA
];
1889 kmutex_t
*hash_lock
;
1890 uint64_t bytes_deleted
= 0;
1891 uint64_t bufs_skipped
= 0;
1893 ASSERT(GHOST_STATE(state
));
1894 bzero(&marker
, sizeof(marker
));
1896 mutex_enter(&state
->arcs_mtx
);
1897 for (ab
= list_tail(list
); ab
; ab
= ab_prev
) {
1898 ab_prev
= list_prev(list
, ab
);
1899 if (spa
&& ab
->b_spa
!= spa
)
1902 /* ignore markers */
1906 hash_lock
= HDR_LOCK(ab
);
1907 /* caller may be trying to modify this buffer, skip it */
1908 if (MUTEX_HELD(hash_lock
))
1910 if (mutex_tryenter(hash_lock
)) {
1911 ASSERT(!HDR_IO_IN_PROGRESS(ab
));
1912 ASSERT(ab
->b_buf
== NULL
);
1913 ARCSTAT_BUMP(arcstat_deleted
);
1914 bytes_deleted
+= ab
->b_size
;
1916 if (ab
->b_l2hdr
!= NULL
) {
1918 * This buffer is cached on the 2nd Level ARC;
1919 * don't destroy the header.
1921 arc_change_state(arc_l2c_only
, ab
, hash_lock
);
1922 mutex_exit(hash_lock
);
1924 arc_change_state(arc_anon
, ab
, hash_lock
);
1925 mutex_exit(hash_lock
);
1926 arc_hdr_destroy(ab
);
1929 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, ab
);
1930 if (bytes
>= 0 && bytes_deleted
>= bytes
)
1932 } else if (bytes
< 0) {
1934 * Insert a list marker and then wait for the
1935 * hash lock to become available. Once its
1936 * available, restart from where we left off.
1938 list_insert_after(list
, ab
, &marker
);
1939 mutex_exit(&state
->arcs_mtx
);
1940 mutex_enter(hash_lock
);
1941 mutex_exit(hash_lock
);
1942 mutex_enter(&state
->arcs_mtx
);
1943 ab_prev
= list_prev(list
, &marker
);
1944 list_remove(list
, &marker
);
1948 mutex_exit(&state
->arcs_mtx
);
1950 if (list
== &state
->arcs_list
[ARC_BUFC_DATA
] &&
1951 (bytes
< 0 || bytes_deleted
< bytes
)) {
1952 list
= &state
->arcs_list
[ARC_BUFC_METADATA
];
1957 ARCSTAT_INCR(arcstat_mutex_miss
, bufs_skipped
);
1961 if (bytes_deleted
< bytes
)
1962 dprintf("only deleted %lld bytes from %p\n",
1963 (longlong_t
)bytes_deleted
, state
);
1969 int64_t adjustment
, delta
;
1975 adjustment
= MIN((int64_t)(arc_size
- arc_c
),
1976 (int64_t)(arc_anon
->arcs_size
+ arc_mru
->arcs_size
+ arc_meta_used
-
1979 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] > 0) {
1980 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_DATA
], adjustment
);
1981 (void) arc_evict(arc_mru
, 0, delta
, FALSE
, ARC_BUFC_DATA
);
1982 adjustment
-= delta
;
1985 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
1986 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
], adjustment
);
1987 (void) arc_evict(arc_mru
, 0, delta
, FALSE
,
1995 adjustment
= arc_size
- arc_c
;
1997 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] > 0) {
1998 delta
= MIN(adjustment
, arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
]);
1999 (void) arc_evict(arc_mfu
, 0, delta
, FALSE
, ARC_BUFC_DATA
);
2000 adjustment
-= delta
;
2003 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
2004 int64_t delta
= MIN(adjustment
,
2005 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
]);
2006 (void) arc_evict(arc_mfu
, 0, delta
, FALSE
,
2011 * Adjust ghost lists
2014 adjustment
= arc_mru
->arcs_size
+ arc_mru_ghost
->arcs_size
- arc_c
;
2016 if (adjustment
> 0 && arc_mru_ghost
->arcs_size
> 0) {
2017 delta
= MIN(arc_mru_ghost
->arcs_size
, adjustment
);
2018 arc_evict_ghost(arc_mru_ghost
, 0, delta
);
2022 arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
- arc_c
;
2024 if (adjustment
> 0 && arc_mfu_ghost
->arcs_size
> 0) {
2025 delta
= MIN(arc_mfu_ghost
->arcs_size
, adjustment
);
2026 arc_evict_ghost(arc_mfu_ghost
, 0, delta
);
2031 * Request that arc user drop references so that N bytes can be released
2032 * from the cache. This provides a mechanism to ensure the arc can honor
2033 * the arc_meta_limit and reclaim buffers which are pinned in the cache
2034 * by higher layers. (i.e. the zpl)
2037 arc_do_user_prune(int64_t adjustment
)
2039 arc_prune_func_t
*func
;
2041 arc_prune_t
*cp
, *np
;
2043 mutex_enter(&arc_prune_mtx
);
2045 cp
= list_head(&arc_prune_list
);
2046 while (cp
!= NULL
) {
2048 private = cp
->p_private
;
2049 np
= list_next(&arc_prune_list
, cp
);
2050 refcount_add(&cp
->p_refcnt
, func
);
2051 mutex_exit(&arc_prune_mtx
);
2054 func(adjustment
, private);
2056 mutex_enter(&arc_prune_mtx
);
2058 /* User removed prune callback concurrently with execution */
2059 if (refcount_remove(&cp
->p_refcnt
, func
) == 0) {
2060 ASSERT(!list_link_active(&cp
->p_node
));
2061 refcount_destroy(&cp
->p_refcnt
);
2062 kmem_free(cp
, sizeof (*cp
));
2068 ARCSTAT_BUMP(arcstat_prune
);
2069 mutex_exit(&arc_prune_mtx
);
2073 arc_do_user_evicts(void)
2075 mutex_enter(&arc_eviction_mtx
);
2076 while (arc_eviction_list
!= NULL
) {
2077 arc_buf_t
*buf
= arc_eviction_list
;
2078 arc_eviction_list
= buf
->b_next
;
2079 mutex_enter(&buf
->b_evict_lock
);
2081 mutex_exit(&buf
->b_evict_lock
);
2082 mutex_exit(&arc_eviction_mtx
);
2084 if (buf
->b_efunc
!= NULL
)
2085 VERIFY(buf
->b_efunc(buf
) == 0);
2087 buf
->b_efunc
= NULL
;
2088 buf
->b_private
= NULL
;
2089 kmem_cache_free(buf_cache
, buf
);
2090 mutex_enter(&arc_eviction_mtx
);
2092 mutex_exit(&arc_eviction_mtx
);
2096 * Evict only meta data objects from the cache leaving the data objects.
2097 * This is only used to enforce the tunable arc_meta_limit, if we are
2098 * unable to evict enough buffers notify the user via the prune callback.
2101 arc_adjust_meta(int64_t adjustment
, boolean_t may_prune
)
2105 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
2106 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
], adjustment
);
2107 arc_evict(arc_mru
, 0, delta
, FALSE
, ARC_BUFC_METADATA
);
2108 adjustment
-= delta
;
2111 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
2112 delta
= MIN(arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
], adjustment
);
2113 arc_evict(arc_mfu
, 0, delta
, FALSE
, ARC_BUFC_METADATA
);
2114 adjustment
-= delta
;
2117 if (may_prune
&& (adjustment
> 0) && (arc_meta_used
> arc_meta_limit
))
2118 arc_do_user_prune(zfs_arc_meta_prune
);
2122 * Flush all *evictable* data from the cache for the given spa.
2123 * NOTE: this will not touch "active" (i.e. referenced) data.
2126 arc_flush(spa_t
*spa
)
2131 guid
= spa_load_guid(spa
);
2133 while (list_head(&arc_mru
->arcs_list
[ARC_BUFC_DATA
])) {
2134 (void) arc_evict(arc_mru
, guid
, -1, FALSE
, ARC_BUFC_DATA
);
2138 while (list_head(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
])) {
2139 (void) arc_evict(arc_mru
, guid
, -1, FALSE
, ARC_BUFC_METADATA
);
2143 while (list_head(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
])) {
2144 (void) arc_evict(arc_mfu
, guid
, -1, FALSE
, ARC_BUFC_DATA
);
2148 while (list_head(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
])) {
2149 (void) arc_evict(arc_mfu
, guid
, -1, FALSE
, ARC_BUFC_METADATA
);
2154 arc_evict_ghost(arc_mru_ghost
, guid
, -1);
2155 arc_evict_ghost(arc_mfu_ghost
, guid
, -1);
2157 mutex_enter(&arc_reclaim_thr_lock
);
2158 arc_do_user_evicts();
2159 mutex_exit(&arc_reclaim_thr_lock
);
2160 ASSERT(spa
|| arc_eviction_list
== NULL
);
2164 arc_shrink(uint64_t bytes
)
2166 if (arc_c
> arc_c_min
) {
2169 to_free
= bytes
? bytes
: arc_c
>> zfs_arc_shrink_shift
;
2171 if (arc_c
> arc_c_min
+ to_free
)
2172 atomic_add_64(&arc_c
, -to_free
);
2176 atomic_add_64(&arc_p
, -(arc_p
>> zfs_arc_shrink_shift
));
2177 if (arc_c
> arc_size
)
2178 arc_c
= MAX(arc_size
, arc_c_min
);
2180 arc_p
= (arc_c
>> 1);
2181 ASSERT(arc_c
>= arc_c_min
);
2182 ASSERT((int64_t)arc_p
>= 0);
2185 if (arc_size
> arc_c
)
2190 arc_kmem_reap_now(arc_reclaim_strategy_t strat
, uint64_t bytes
)
2193 kmem_cache_t
*prev_cache
= NULL
;
2194 kmem_cache_t
*prev_data_cache
= NULL
;
2195 extern kmem_cache_t
*zio_buf_cache
[];
2196 extern kmem_cache_t
*zio_data_buf_cache
[];
2199 * An aggressive reclamation will shrink the cache size as well as
2200 * reap free buffers from the arc kmem caches.
2202 if (strat
== ARC_RECLAIM_AGGR
)
2205 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
2206 if (zio_buf_cache
[i
] != prev_cache
) {
2207 prev_cache
= zio_buf_cache
[i
];
2208 kmem_cache_reap_now(zio_buf_cache
[i
]);
2210 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
2211 prev_data_cache
= zio_data_buf_cache
[i
];
2212 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
2216 kmem_cache_reap_now(buf_cache
);
2217 kmem_cache_reap_now(hdr_cache
);
2221 * Unlike other ZFS implementations this thread is only responsible for
2222 * adapting the target ARC size on Linux. The responsibility for memory
2223 * reclamation has been entirely delegated to the arc_shrinker_func()
2224 * which is registered with the VM. To reflect this change in behavior
2225 * the arc_reclaim thread has been renamed to arc_adapt.
2228 arc_adapt_thread(void)
2233 CALLB_CPR_INIT(&cpr
, &arc_reclaim_thr_lock
, callb_generic_cpr
, FTAG
);
2235 mutex_enter(&arc_reclaim_thr_lock
);
2236 while (arc_thread_exit
== 0) {
2238 arc_reclaim_strategy_t last_reclaim
= ARC_RECLAIM_CONS
;
2240 if (spa_get_random(100) == 0) {
2243 if (last_reclaim
== ARC_RECLAIM_CONS
) {
2244 last_reclaim
= ARC_RECLAIM_AGGR
;
2246 last_reclaim
= ARC_RECLAIM_CONS
;
2250 last_reclaim
= ARC_RECLAIM_AGGR
;
2254 /* reset the growth delay for every reclaim */
2255 arc_grow_time
= ddi_get_lbolt()+(zfs_arc_grow_retry
* hz
);
2257 arc_kmem_reap_now(last_reclaim
, 0);
2260 #endif /* !_KERNEL */
2262 /* No recent memory pressure allow the ARC to grow. */
2263 if (arc_no_grow
&& ddi_get_lbolt() >= arc_grow_time
)
2264 arc_no_grow
= FALSE
;
2267 * Keep meta data usage within limits, arc_shrink() is not
2268 * used to avoid collapsing the arc_c value when only the
2269 * arc_meta_limit is being exceeded.
2271 prune
= (int64_t)arc_meta_used
- (int64_t)arc_meta_limit
;
2273 arc_adjust_meta(prune
, B_TRUE
);
2277 if (arc_eviction_list
!= NULL
)
2278 arc_do_user_evicts();
2280 /* block until needed, or one second, whichever is shorter */
2281 CALLB_CPR_SAFE_BEGIN(&cpr
);
2282 (void) cv_timedwait_interruptible(&arc_reclaim_thr_cv
,
2283 &arc_reclaim_thr_lock
, (ddi_get_lbolt() + hz
));
2284 CALLB_CPR_SAFE_END(&cpr
, &arc_reclaim_thr_lock
);
2287 /* Allow the module options to be changed */
2288 if (zfs_arc_max
> 64 << 20 &&
2289 zfs_arc_max
< physmem
* PAGESIZE
&&
2290 zfs_arc_max
!= arc_c_max
)
2291 arc_c_max
= zfs_arc_max
;
2293 if (zfs_arc_min
> 0 &&
2294 zfs_arc_min
< arc_c_max
&&
2295 zfs_arc_min
!= arc_c_min
)
2296 arc_c_min
= zfs_arc_min
;
2298 if (zfs_arc_meta_limit
> 0 &&
2299 zfs_arc_meta_limit
<= arc_c_max
&&
2300 zfs_arc_meta_limit
!= arc_meta_limit
)
2301 arc_meta_limit
= zfs_arc_meta_limit
;
2307 arc_thread_exit
= 0;
2308 cv_broadcast(&arc_reclaim_thr_cv
);
2309 CALLB_CPR_EXIT(&cpr
); /* drops arc_reclaim_thr_lock */
2315 * Determine the amount of memory eligible for eviction contained in the
2316 * ARC. All clean data reported by the ghost lists can always be safely
2317 * evicted. Due to arc_c_min, the same does not hold for all clean data
2318 * contained by the regular mru and mfu lists.
2320 * In the case of the regular mru and mfu lists, we need to report as
2321 * much clean data as possible, such that evicting that same reported
2322 * data will not bring arc_size below arc_c_min. Thus, in certain
2323 * circumstances, the total amount of clean data in the mru and mfu
2324 * lists might not actually be evictable.
2326 * The following two distinct cases are accounted for:
2328 * 1. The sum of the amount of dirty data contained by both the mru and
2329 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2330 * is greater than or equal to arc_c_min.
2331 * (i.e. amount of dirty data >= arc_c_min)
2333 * This is the easy case; all clean data contained by the mru and mfu
2334 * lists is evictable. Evicting all clean data can only drop arc_size
2335 * to the amount of dirty data, which is greater than arc_c_min.
2337 * 2. The sum of the amount of dirty data contained by both the mru and
2338 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2339 * is less than arc_c_min.
2340 * (i.e. arc_c_min > amount of dirty data)
2342 * 2.1. arc_size is greater than or equal arc_c_min.
2343 * (i.e. arc_size >= arc_c_min > amount of dirty data)
2345 * In this case, not all clean data from the regular mru and mfu
2346 * lists is actually evictable; we must leave enough clean data
2347 * to keep arc_size above arc_c_min. Thus, the maximum amount of
2348 * evictable data from the two lists combined, is exactly the
2349 * difference between arc_size and arc_c_min.
2351 * 2.2. arc_size is less than arc_c_min
2352 * (i.e. arc_c_min > arc_size > amount of dirty data)
2354 * In this case, none of the data contained in the mru and mfu
2355 * lists is evictable, even if it's clean. Since arc_size is
2356 * already below arc_c_min, evicting any more would only
2357 * increase this negative difference.
2360 arc_evictable_memory(void) {
2361 uint64_t arc_clean
=
2362 arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] +
2363 arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] +
2364 arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] +
2365 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
];
2366 uint64_t ghost_clean
=
2367 arc_mru_ghost
->arcs_lsize
[ARC_BUFC_DATA
] +
2368 arc_mru_ghost
->arcs_lsize
[ARC_BUFC_METADATA
] +
2369 arc_mfu_ghost
->arcs_lsize
[ARC_BUFC_DATA
] +
2370 arc_mfu_ghost
->arcs_lsize
[ARC_BUFC_METADATA
];
2371 uint64_t arc_dirty
= MAX((int64_t)arc_size
- (int64_t)arc_clean
, 0);
2373 if (arc_dirty
>= arc_c_min
)
2374 return (ghost_clean
+ arc_clean
);
2376 return (ghost_clean
+ MAX((int64_t)arc_size
- (int64_t)arc_c_min
, 0));
2380 __arc_shrinker_func(struct shrinker
*shrink
, struct shrink_control
*sc
)
2384 /* The arc is considered warm once reclaim has occurred */
2385 if (unlikely(arc_warm
== B_FALSE
))
2388 /* Return the potential number of reclaimable pages */
2389 pages
= btop(arc_evictable_memory());
2390 if (sc
->nr_to_scan
== 0)
2393 /* Not allowed to perform filesystem reclaim */
2394 if (!(sc
->gfp_mask
& __GFP_FS
))
2397 /* Reclaim in progress */
2398 if (mutex_tryenter(&arc_reclaim_thr_lock
) == 0)
2402 * Evict the requested number of pages by shrinking arc_c the
2403 * requested amount. If there is nothing left to evict just
2404 * reap whatever we can from the various arc slabs.
2407 arc_kmem_reap_now(ARC_RECLAIM_AGGR
, ptob(sc
->nr_to_scan
));
2409 arc_kmem_reap_now(ARC_RECLAIM_CONS
, ptob(sc
->nr_to_scan
));
2413 * When direct reclaim is observed it usually indicates a rapid
2414 * increase in memory pressure. This occurs because the kswapd
2415 * threads were unable to asynchronously keep enough free memory
2416 * available. In this case set arc_no_grow to briefly pause arc
2417 * growth to avoid compounding the memory pressure.
2419 if (current_is_kswapd()) {
2420 ARCSTAT_BUMP(arcstat_memory_indirect_count
);
2422 arc_no_grow
= B_TRUE
;
2423 arc_grow_time
= ddi_get_lbolt() + (zfs_arc_grow_retry
* hz
);
2424 ARCSTAT_BUMP(arcstat_memory_direct_count
);
2427 mutex_exit(&arc_reclaim_thr_lock
);
2431 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func
);
2433 SPL_SHRINKER_DECLARE(arc_shrinker
, arc_shrinker_func
, DEFAULT_SEEKS
);
2434 #endif /* _KERNEL */
2437 * Adapt arc info given the number of bytes we are trying to add and
2438 * the state that we are comming from. This function is only called
2439 * when we are adding new content to the cache.
2442 arc_adapt(int bytes
, arc_state_t
*state
)
2445 uint64_t arc_p_min
= (arc_c
>> zfs_arc_p_min_shift
);
2447 if (state
== arc_l2c_only
)
2452 * Adapt the target size of the MRU list:
2453 * - if we just hit in the MRU ghost list, then increase
2454 * the target size of the MRU list.
2455 * - if we just hit in the MFU ghost list, then increase
2456 * the target size of the MFU list by decreasing the
2457 * target size of the MRU list.
2459 if (state
== arc_mru_ghost
) {
2460 mult
= ((arc_mru_ghost
->arcs_size
>= arc_mfu_ghost
->arcs_size
) ?
2461 1 : (arc_mfu_ghost
->arcs_size
/arc_mru_ghost
->arcs_size
));
2462 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
2464 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
2465 } else if (state
== arc_mfu_ghost
) {
2468 mult
= ((arc_mfu_ghost
->arcs_size
>= arc_mru_ghost
->arcs_size
) ?
2469 1 : (arc_mru_ghost
->arcs_size
/arc_mfu_ghost
->arcs_size
));
2470 mult
= MIN(mult
, 10);
2472 delta
= MIN(bytes
* mult
, arc_p
);
2473 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
2475 ASSERT((int64_t)arc_p
>= 0);
2480 if (arc_c
>= arc_c_max
)
2484 * If we're within (2 * maxblocksize) bytes of the target
2485 * cache size, increment the target cache size
2487 if (arc_size
> arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
2488 atomic_add_64(&arc_c
, (int64_t)bytes
);
2489 if (arc_c
> arc_c_max
)
2491 else if (state
== arc_anon
)
2492 atomic_add_64(&arc_p
, (int64_t)bytes
);
2496 ASSERT((int64_t)arc_p
>= 0);
2500 * Check if the cache has reached its limits and eviction is required
2504 arc_evict_needed(arc_buf_contents_t type
)
2506 if (type
== ARC_BUFC_METADATA
&& arc_meta_used
>= arc_meta_limit
)
2512 return (arc_size
> arc_c
);
2516 * The buffer, supplied as the first argument, needs a data block.
2517 * So, if we are at cache max, determine which cache should be victimized.
2518 * We have the following cases:
2520 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2521 * In this situation if we're out of space, but the resident size of the MFU is
2522 * under the limit, victimize the MFU cache to satisfy this insertion request.
2524 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2525 * Here, we've used up all of the available space for the MRU, so we need to
2526 * evict from our own cache instead. Evict from the set of resident MRU
2529 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2530 * c minus p represents the MFU space in the cache, since p is the size of the
2531 * cache that is dedicated to the MRU. In this situation there's still space on
2532 * the MFU side, so the MRU side needs to be victimized.
2534 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2535 * MFU's resident set is consuming more space than it has been allotted. In
2536 * this situation, we must victimize our own cache, the MFU, for this insertion.
2539 arc_get_data_buf(arc_buf_t
*buf
)
2541 arc_state_t
*state
= buf
->b_hdr
->b_state
;
2542 uint64_t size
= buf
->b_hdr
->b_size
;
2543 arc_buf_contents_t type
= buf
->b_hdr
->b_type
;
2545 arc_adapt(size
, state
);
2548 * We have not yet reached cache maximum size,
2549 * just allocate a new buffer.
2551 if (!arc_evict_needed(type
)) {
2552 if (type
== ARC_BUFC_METADATA
) {
2553 buf
->b_data
= zio_buf_alloc(size
);
2554 arc_space_consume(size
, ARC_SPACE_DATA
);
2556 ASSERT(type
== ARC_BUFC_DATA
);
2557 buf
->b_data
= zio_data_buf_alloc(size
);
2558 ARCSTAT_INCR(arcstat_data_size
, size
);
2559 atomic_add_64(&arc_size
, size
);
2565 * If we are prefetching from the mfu ghost list, this buffer
2566 * will end up on the mru list; so steal space from there.
2568 if (state
== arc_mfu_ghost
)
2569 state
= buf
->b_hdr
->b_flags
& ARC_PREFETCH
? arc_mru
: arc_mfu
;
2570 else if (state
== arc_mru_ghost
)
2573 if (state
== arc_mru
|| state
== arc_anon
) {
2574 uint64_t mru_used
= arc_anon
->arcs_size
+ arc_mru
->arcs_size
;
2575 state
= (arc_mfu
->arcs_lsize
[type
] >= size
&&
2576 arc_p
> mru_used
) ? arc_mfu
: arc_mru
;
2579 uint64_t mfu_space
= arc_c
- arc_p
;
2580 state
= (arc_mru
->arcs_lsize
[type
] >= size
&&
2581 mfu_space
> arc_mfu
->arcs_size
) ? arc_mru
: arc_mfu
;
2584 if ((buf
->b_data
= arc_evict(state
, 0, size
, TRUE
, type
)) == NULL
) {
2585 if (type
== ARC_BUFC_METADATA
) {
2586 buf
->b_data
= zio_buf_alloc(size
);
2587 arc_space_consume(size
, ARC_SPACE_DATA
);
2590 * If we are unable to recycle an existing meta buffer
2591 * signal the reclaim thread. It will notify users
2592 * via the prune callback to drop references. The
2593 * prune callback in run in the context of the reclaim
2594 * thread to avoid deadlocking on the hash_lock.
2596 cv_signal(&arc_reclaim_thr_cv
);
2598 ASSERT(type
== ARC_BUFC_DATA
);
2599 buf
->b_data
= zio_data_buf_alloc(size
);
2600 ARCSTAT_INCR(arcstat_data_size
, size
);
2601 atomic_add_64(&arc_size
, size
);
2604 ARCSTAT_BUMP(arcstat_recycle_miss
);
2606 ASSERT(buf
->b_data
!= NULL
);
2609 * Update the state size. Note that ghost states have a
2610 * "ghost size" and so don't need to be updated.
2612 if (!GHOST_STATE(buf
->b_hdr
->b_state
)) {
2613 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2615 atomic_add_64(&hdr
->b_state
->arcs_size
, size
);
2616 if (list_link_active(&hdr
->b_arc_node
)) {
2617 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
2618 atomic_add_64(&hdr
->b_state
->arcs_lsize
[type
], size
);
2621 * If we are growing the cache, and we are adding anonymous
2622 * data, and we have outgrown arc_p, update arc_p
2624 if (arc_size
< arc_c
&& hdr
->b_state
== arc_anon
&&
2625 arc_anon
->arcs_size
+ arc_mru
->arcs_size
> arc_p
)
2626 arc_p
= MIN(arc_c
, arc_p
+ size
);
2631 * This routine is called whenever a buffer is accessed.
2632 * NOTE: the hash lock is dropped in this function.
2635 arc_access(arc_buf_hdr_t
*buf
, kmutex_t
*hash_lock
)
2639 ASSERT(MUTEX_HELD(hash_lock
));
2641 if (buf
->b_state
== arc_anon
) {
2643 * This buffer is not in the cache, and does not
2644 * appear in our "ghost" list. Add the new buffer
2648 ASSERT(buf
->b_arc_access
== 0);
2649 buf
->b_arc_access
= ddi_get_lbolt();
2650 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, buf
);
2651 arc_change_state(arc_mru
, buf
, hash_lock
);
2653 } else if (buf
->b_state
== arc_mru
) {
2654 now
= ddi_get_lbolt();
2657 * If this buffer is here because of a prefetch, then either:
2658 * - clear the flag if this is a "referencing" read
2659 * (any subsequent access will bump this into the MFU state).
2661 * - move the buffer to the head of the list if this is
2662 * another prefetch (to make it less likely to be evicted).
2664 if ((buf
->b_flags
& ARC_PREFETCH
) != 0) {
2665 if (refcount_count(&buf
->b_refcnt
) == 0) {
2666 ASSERT(list_link_active(&buf
->b_arc_node
));
2668 buf
->b_flags
&= ~ARC_PREFETCH
;
2669 ARCSTAT_BUMP(arcstat_mru_hits
);
2671 buf
->b_arc_access
= now
;
2676 * This buffer has been "accessed" only once so far,
2677 * but it is still in the cache. Move it to the MFU
2680 if (now
> buf
->b_arc_access
+ ARC_MINTIME
) {
2682 * More than 125ms have passed since we
2683 * instantiated this buffer. Move it to the
2684 * most frequently used state.
2686 buf
->b_arc_access
= now
;
2687 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2688 arc_change_state(arc_mfu
, buf
, hash_lock
);
2690 ARCSTAT_BUMP(arcstat_mru_hits
);
2691 } else if (buf
->b_state
== arc_mru_ghost
) {
2692 arc_state_t
*new_state
;
2694 * This buffer has been "accessed" recently, but
2695 * was evicted from the cache. Move it to the
2699 if (buf
->b_flags
& ARC_PREFETCH
) {
2700 new_state
= arc_mru
;
2701 if (refcount_count(&buf
->b_refcnt
) > 0)
2702 buf
->b_flags
&= ~ARC_PREFETCH
;
2703 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, buf
);
2705 new_state
= arc_mfu
;
2706 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2709 buf
->b_arc_access
= ddi_get_lbolt();
2710 arc_change_state(new_state
, buf
, hash_lock
);
2712 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
2713 } else if (buf
->b_state
== arc_mfu
) {
2715 * This buffer has been accessed more than once and is
2716 * still in the cache. Keep it in the MFU state.
2718 * NOTE: an add_reference() that occurred when we did
2719 * the arc_read() will have kicked this off the list.
2720 * If it was a prefetch, we will explicitly move it to
2721 * the head of the list now.
2723 if ((buf
->b_flags
& ARC_PREFETCH
) != 0) {
2724 ASSERT(refcount_count(&buf
->b_refcnt
) == 0);
2725 ASSERT(list_link_active(&buf
->b_arc_node
));
2727 ARCSTAT_BUMP(arcstat_mfu_hits
);
2728 buf
->b_arc_access
= ddi_get_lbolt();
2729 } else if (buf
->b_state
== arc_mfu_ghost
) {
2730 arc_state_t
*new_state
= arc_mfu
;
2732 * This buffer has been accessed more than once but has
2733 * been evicted from the cache. Move it back to the
2737 if (buf
->b_flags
& ARC_PREFETCH
) {
2739 * This is a prefetch access...
2740 * move this block back to the MRU state.
2742 ASSERT0(refcount_count(&buf
->b_refcnt
));
2743 new_state
= arc_mru
;
2746 buf
->b_arc_access
= ddi_get_lbolt();
2747 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2748 arc_change_state(new_state
, buf
, hash_lock
);
2750 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
2751 } else if (buf
->b_state
== arc_l2c_only
) {
2753 * This buffer is on the 2nd Level ARC.
2756 buf
->b_arc_access
= ddi_get_lbolt();
2757 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2758 arc_change_state(arc_mfu
, buf
, hash_lock
);
2760 ASSERT(!"invalid arc state");
2764 /* a generic arc_done_func_t which you can use */
2767 arc_bcopy_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2769 if (zio
== NULL
|| zio
->io_error
== 0)
2770 bcopy(buf
->b_data
, arg
, buf
->b_hdr
->b_size
);
2771 VERIFY(arc_buf_remove_ref(buf
, arg
) == 1);
2774 /* a generic arc_done_func_t */
2776 arc_getbuf_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2778 arc_buf_t
**bufp
= arg
;
2779 if (zio
&& zio
->io_error
) {
2780 VERIFY(arc_buf_remove_ref(buf
, arg
) == 1);
2784 ASSERT(buf
->b_data
);
2789 arc_read_done(zio_t
*zio
)
2791 arc_buf_hdr_t
*hdr
, *found
;
2793 arc_buf_t
*abuf
; /* buffer we're assigning to callback */
2794 kmutex_t
*hash_lock
;
2795 arc_callback_t
*callback_list
, *acb
;
2796 int freeable
= FALSE
;
2798 buf
= zio
->io_private
;
2802 * The hdr was inserted into hash-table and removed from lists
2803 * prior to starting I/O. We should find this header, since
2804 * it's in the hash table, and it should be legit since it's
2805 * not possible to evict it during the I/O. The only possible
2806 * reason for it not to be found is if we were freed during the
2809 found
= buf_hash_find(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
,
2812 ASSERT((found
== NULL
&& HDR_FREED_IN_READ(hdr
) && hash_lock
== NULL
) ||
2813 (found
== hdr
&& DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
2814 (found
== hdr
&& HDR_L2_READING(hdr
)));
2816 hdr
->b_flags
&= ~ARC_L2_EVICTED
;
2817 if (l2arc_noprefetch
&& (hdr
->b_flags
& ARC_PREFETCH
))
2818 hdr
->b_flags
&= ~ARC_L2CACHE
;
2820 /* byteswap if necessary */
2821 callback_list
= hdr
->b_acb
;
2822 ASSERT(callback_list
!= NULL
);
2823 if (BP_SHOULD_BYTESWAP(zio
->io_bp
) && zio
->io_error
== 0) {
2824 dmu_object_byteswap_t bswap
=
2825 DMU_OT_BYTESWAP(BP_GET_TYPE(zio
->io_bp
));
2826 if (BP_GET_LEVEL(zio
->io_bp
) > 0)
2827 byteswap_uint64_array(buf
->b_data
, hdr
->b_size
);
2829 dmu_ot_byteswap
[bswap
].ob_func(buf
->b_data
, hdr
->b_size
);
2832 arc_cksum_compute(buf
, B_FALSE
);
2834 if (hash_lock
&& zio
->io_error
== 0 && hdr
->b_state
== arc_anon
) {
2836 * Only call arc_access on anonymous buffers. This is because
2837 * if we've issued an I/O for an evicted buffer, we've already
2838 * called arc_access (to prevent any simultaneous readers from
2839 * getting confused).
2841 arc_access(hdr
, hash_lock
);
2844 /* create copies of the data buffer for the callers */
2846 for (acb
= callback_list
; acb
; acb
= acb
->acb_next
) {
2847 if (acb
->acb_done
) {
2849 ARCSTAT_BUMP(arcstat_duplicate_reads
);
2850 abuf
= arc_buf_clone(buf
);
2852 acb
->acb_buf
= abuf
;
2857 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
2858 ASSERT(!HDR_BUF_AVAILABLE(hdr
));
2860 ASSERT(buf
->b_efunc
== NULL
);
2861 ASSERT(hdr
->b_datacnt
== 1);
2862 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
2865 ASSERT(refcount_is_zero(&hdr
->b_refcnt
) || callback_list
!= NULL
);
2867 if (zio
->io_error
!= 0) {
2868 hdr
->b_flags
|= ARC_IO_ERROR
;
2869 if (hdr
->b_state
!= arc_anon
)
2870 arc_change_state(arc_anon
, hdr
, hash_lock
);
2871 if (HDR_IN_HASH_TABLE(hdr
))
2872 buf_hash_remove(hdr
);
2873 freeable
= refcount_is_zero(&hdr
->b_refcnt
);
2877 * Broadcast before we drop the hash_lock to avoid the possibility
2878 * that the hdr (and hence the cv) might be freed before we get to
2879 * the cv_broadcast().
2881 cv_broadcast(&hdr
->b_cv
);
2884 mutex_exit(hash_lock
);
2887 * This block was freed while we waited for the read to
2888 * complete. It has been removed from the hash table and
2889 * moved to the anonymous state (so that it won't show up
2892 ASSERT3P(hdr
->b_state
, ==, arc_anon
);
2893 freeable
= refcount_is_zero(&hdr
->b_refcnt
);
2896 /* execute each callback and free its structure */
2897 while ((acb
= callback_list
) != NULL
) {
2899 acb
->acb_done(zio
, acb
->acb_buf
, acb
->acb_private
);
2901 if (acb
->acb_zio_dummy
!= NULL
) {
2902 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
2903 zio_nowait(acb
->acb_zio_dummy
);
2906 callback_list
= acb
->acb_next
;
2907 kmem_free(acb
, sizeof (arc_callback_t
));
2911 arc_hdr_destroy(hdr
);
2915 * "Read" the block at the specified DVA (in bp) via the
2916 * cache. If the block is found in the cache, invoke the provided
2917 * callback immediately and return. Note that the `zio' parameter
2918 * in the callback will be NULL in this case, since no IO was
2919 * required. If the block is not in the cache pass the read request
2920 * on to the spa with a substitute callback function, so that the
2921 * requested block will be added to the cache.
2923 * If a read request arrives for a block that has a read in-progress,
2924 * either wait for the in-progress read to complete (and return the
2925 * results); or, if this is a read with a "done" func, add a record
2926 * to the read to invoke the "done" func when the read completes,
2927 * and return; or just return.
2929 * arc_read_done() will invoke all the requested "done" functions
2930 * for readers of this block.
2933 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
, arc_done_func_t
*done
,
2934 void *private, int priority
, int zio_flags
, uint32_t *arc_flags
,
2935 const zbookmark_t
*zb
)
2938 arc_buf_t
*buf
= NULL
;
2939 kmutex_t
*hash_lock
;
2941 uint64_t guid
= spa_load_guid(spa
);
2944 hdr
= buf_hash_find(guid
, BP_IDENTITY(bp
), BP_PHYSICAL_BIRTH(bp
),
2946 if (hdr
&& hdr
->b_datacnt
> 0) {
2948 *arc_flags
|= ARC_CACHED
;
2950 if (HDR_IO_IN_PROGRESS(hdr
)) {
2952 if (*arc_flags
& ARC_WAIT
) {
2953 cv_wait(&hdr
->b_cv
, hash_lock
);
2954 mutex_exit(hash_lock
);
2957 ASSERT(*arc_flags
& ARC_NOWAIT
);
2960 arc_callback_t
*acb
= NULL
;
2962 acb
= kmem_zalloc(sizeof (arc_callback_t
),
2964 acb
->acb_done
= done
;
2965 acb
->acb_private
= private;
2967 acb
->acb_zio_dummy
= zio_null(pio
,
2968 spa
, NULL
, NULL
, NULL
, zio_flags
);
2970 ASSERT(acb
->acb_done
!= NULL
);
2971 acb
->acb_next
= hdr
->b_acb
;
2973 add_reference(hdr
, hash_lock
, private);
2974 mutex_exit(hash_lock
);
2977 mutex_exit(hash_lock
);
2981 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
2984 add_reference(hdr
, hash_lock
, private);
2986 * If this block is already in use, create a new
2987 * copy of the data so that we will be guaranteed
2988 * that arc_release() will always succeed.
2992 ASSERT(buf
->b_data
);
2993 if (HDR_BUF_AVAILABLE(hdr
)) {
2994 ASSERT(buf
->b_efunc
== NULL
);
2995 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
2997 buf
= arc_buf_clone(buf
);
3000 } else if (*arc_flags
& ARC_PREFETCH
&&
3001 refcount_count(&hdr
->b_refcnt
) == 0) {
3002 hdr
->b_flags
|= ARC_PREFETCH
;
3004 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
3005 arc_access(hdr
, hash_lock
);
3006 if (*arc_flags
& ARC_L2CACHE
)
3007 hdr
->b_flags
|= ARC_L2CACHE
;
3008 if (*arc_flags
& ARC_L2COMPRESS
)
3009 hdr
->b_flags
|= ARC_L2COMPRESS
;
3010 mutex_exit(hash_lock
);
3011 ARCSTAT_BUMP(arcstat_hits
);
3012 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
3013 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
3014 data
, metadata
, hits
);
3017 done(NULL
, buf
, private);
3019 uint64_t size
= BP_GET_LSIZE(bp
);
3020 arc_callback_t
*acb
;
3023 boolean_t devw
= B_FALSE
;
3026 /* this block is not in the cache */
3027 arc_buf_hdr_t
*exists
;
3028 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
3029 buf
= arc_buf_alloc(spa
, size
, private, type
);
3031 hdr
->b_dva
= *BP_IDENTITY(bp
);
3032 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
3033 hdr
->b_cksum0
= bp
->blk_cksum
.zc_word
[0];
3034 exists
= buf_hash_insert(hdr
, &hash_lock
);
3036 /* somebody beat us to the hash insert */
3037 mutex_exit(hash_lock
);
3038 buf_discard_identity(hdr
);
3039 (void) arc_buf_remove_ref(buf
, private);
3040 goto top
; /* restart the IO request */
3042 /* if this is a prefetch, we don't have a reference */
3043 if (*arc_flags
& ARC_PREFETCH
) {
3044 (void) remove_reference(hdr
, hash_lock
,
3046 hdr
->b_flags
|= ARC_PREFETCH
;
3048 if (*arc_flags
& ARC_L2CACHE
)
3049 hdr
->b_flags
|= ARC_L2CACHE
;
3050 if (*arc_flags
& ARC_L2COMPRESS
)
3051 hdr
->b_flags
|= ARC_L2COMPRESS
;
3052 if (BP_GET_LEVEL(bp
) > 0)
3053 hdr
->b_flags
|= ARC_INDIRECT
;
3055 /* this block is in the ghost cache */
3056 ASSERT(GHOST_STATE(hdr
->b_state
));
3057 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3058 ASSERT0(refcount_count(&hdr
->b_refcnt
));
3059 ASSERT(hdr
->b_buf
== NULL
);
3061 /* if this is a prefetch, we don't have a reference */
3062 if (*arc_flags
& ARC_PREFETCH
)
3063 hdr
->b_flags
|= ARC_PREFETCH
;
3065 add_reference(hdr
, hash_lock
, private);
3066 if (*arc_flags
& ARC_L2CACHE
)
3067 hdr
->b_flags
|= ARC_L2CACHE
;
3068 if (*arc_flags
& ARC_L2COMPRESS
)
3069 hdr
->b_flags
|= ARC_L2COMPRESS
;
3070 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
3073 buf
->b_efunc
= NULL
;
3074 buf
->b_private
= NULL
;
3077 ASSERT(hdr
->b_datacnt
== 0);
3079 arc_get_data_buf(buf
);
3080 arc_access(hdr
, hash_lock
);
3083 ASSERT(!GHOST_STATE(hdr
->b_state
));
3085 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_PUSHPAGE
);
3086 acb
->acb_done
= done
;
3087 acb
->acb_private
= private;
3089 ASSERT(hdr
->b_acb
== NULL
);
3091 hdr
->b_flags
|= ARC_IO_IN_PROGRESS
;
3093 if (HDR_L2CACHE(hdr
) && hdr
->b_l2hdr
!= NULL
&&
3094 (vd
= hdr
->b_l2hdr
->b_dev
->l2ad_vdev
) != NULL
) {
3095 devw
= hdr
->b_l2hdr
->b_dev
->l2ad_writing
;
3096 addr
= hdr
->b_l2hdr
->b_daddr
;
3098 * Lock out device removal.
3100 if (vdev_is_dead(vd
) ||
3101 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
3105 mutex_exit(hash_lock
);
3107 ASSERT3U(hdr
->b_size
, ==, size
);
3108 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
, blkptr_t
*, bp
,
3109 uint64_t, size
, zbookmark_t
*, zb
);
3110 ARCSTAT_BUMP(arcstat_misses
);
3111 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
3112 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
3113 data
, metadata
, misses
);
3115 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
3117 * Read from the L2ARC if the following are true:
3118 * 1. The L2ARC vdev was previously cached.
3119 * 2. This buffer still has L2ARC metadata.
3120 * 3. This buffer isn't currently writing to the L2ARC.
3121 * 4. The L2ARC entry wasn't evicted, which may
3122 * also have invalidated the vdev.
3123 * 5. This isn't prefetch and l2arc_noprefetch is set.
3125 if (hdr
->b_l2hdr
!= NULL
&&
3126 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
3127 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
3128 l2arc_read_callback_t
*cb
;
3130 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
3131 ARCSTAT_BUMP(arcstat_l2_hits
);
3133 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
3135 cb
->l2rcb_buf
= buf
;
3136 cb
->l2rcb_spa
= spa
;
3139 cb
->l2rcb_flags
= zio_flags
;
3140 cb
->l2rcb_compress
= hdr
->b_l2hdr
->b_compress
;
3143 * l2arc read. The SCL_L2ARC lock will be
3144 * released by l2arc_read_done().
3145 * Issue a null zio if the underlying buffer
3146 * was squashed to zero size by compression.
3148 if (hdr
->b_l2hdr
->b_compress
==
3149 ZIO_COMPRESS_EMPTY
) {
3150 rzio
= zio_null(pio
, spa
, vd
,
3151 l2arc_read_done
, cb
,
3152 zio_flags
| ZIO_FLAG_DONT_CACHE
|
3154 ZIO_FLAG_DONT_PROPAGATE
|
3155 ZIO_FLAG_DONT_RETRY
);
3157 rzio
= zio_read_phys(pio
, vd
, addr
,
3158 hdr
->b_l2hdr
->b_asize
,
3159 buf
->b_data
, ZIO_CHECKSUM_OFF
,
3160 l2arc_read_done
, cb
, priority
,
3161 zio_flags
| ZIO_FLAG_DONT_CACHE
|
3163 ZIO_FLAG_DONT_PROPAGATE
|
3164 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
3166 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
3168 ARCSTAT_INCR(arcstat_l2_read_bytes
,
3169 hdr
->b_l2hdr
->b_asize
);
3171 if (*arc_flags
& ARC_NOWAIT
) {
3176 ASSERT(*arc_flags
& ARC_WAIT
);
3177 if (zio_wait(rzio
) == 0)
3180 /* l2arc read error; goto zio_read() */
3182 DTRACE_PROBE1(l2arc__miss
,
3183 arc_buf_hdr_t
*, hdr
);
3184 ARCSTAT_BUMP(arcstat_l2_misses
);
3185 if (HDR_L2_WRITING(hdr
))
3186 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
3187 spa_config_exit(spa
, SCL_L2ARC
, vd
);
3191 spa_config_exit(spa
, SCL_L2ARC
, vd
);
3192 if (l2arc_ndev
!= 0) {
3193 DTRACE_PROBE1(l2arc__miss
,
3194 arc_buf_hdr_t
*, hdr
);
3195 ARCSTAT_BUMP(arcstat_l2_misses
);
3199 rzio
= zio_read(pio
, spa
, bp
, buf
->b_data
, size
,
3200 arc_read_done
, buf
, priority
, zio_flags
, zb
);
3202 if (*arc_flags
& ARC_WAIT
)
3203 return (zio_wait(rzio
));
3205 ASSERT(*arc_flags
& ARC_NOWAIT
);
3212 arc_add_prune_callback(arc_prune_func_t
*func
, void *private)
3216 p
= kmem_alloc(sizeof(*p
), KM_SLEEP
);
3218 p
->p_private
= private;
3219 list_link_init(&p
->p_node
);
3220 refcount_create(&p
->p_refcnt
);
3222 mutex_enter(&arc_prune_mtx
);
3223 refcount_add(&p
->p_refcnt
, &arc_prune_list
);
3224 list_insert_head(&arc_prune_list
, p
);
3225 mutex_exit(&arc_prune_mtx
);
3231 arc_remove_prune_callback(arc_prune_t
*p
)
3233 mutex_enter(&arc_prune_mtx
);
3234 list_remove(&arc_prune_list
, p
);
3235 if (refcount_remove(&p
->p_refcnt
, &arc_prune_list
) == 0) {
3236 refcount_destroy(&p
->p_refcnt
);
3237 kmem_free(p
, sizeof (*p
));
3239 mutex_exit(&arc_prune_mtx
);
3243 arc_set_callback(arc_buf_t
*buf
, arc_evict_func_t
*func
, void *private)
3245 ASSERT(buf
->b_hdr
!= NULL
);
3246 ASSERT(buf
->b_hdr
->b_state
!= arc_anon
);
3247 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_refcnt
) || func
== NULL
);
3248 ASSERT(buf
->b_efunc
== NULL
);
3249 ASSERT(!HDR_BUF_AVAILABLE(buf
->b_hdr
));
3251 buf
->b_efunc
= func
;
3252 buf
->b_private
= private;
3256 * Notify the arc that a block was freed, and thus will never be used again.
3259 arc_freed(spa_t
*spa
, const blkptr_t
*bp
)
3262 kmutex_t
*hash_lock
;
3263 uint64_t guid
= spa_load_guid(spa
);
3265 hdr
= buf_hash_find(guid
, BP_IDENTITY(bp
), BP_PHYSICAL_BIRTH(bp
),
3269 if (HDR_BUF_AVAILABLE(hdr
)) {
3270 arc_buf_t
*buf
= hdr
->b_buf
;
3271 add_reference(hdr
, hash_lock
, FTAG
);
3272 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
3273 mutex_exit(hash_lock
);
3275 arc_release(buf
, FTAG
);
3276 (void) arc_buf_remove_ref(buf
, FTAG
);
3278 mutex_exit(hash_lock
);
3284 * This is used by the DMU to let the ARC know that a buffer is
3285 * being evicted, so the ARC should clean up. If this arc buf
3286 * is not yet in the evicted state, it will be put there.
3289 arc_buf_evict(arc_buf_t
*buf
)
3292 kmutex_t
*hash_lock
;
3295 mutex_enter(&buf
->b_evict_lock
);
3299 * We are in arc_do_user_evicts().
3301 ASSERT(buf
->b_data
== NULL
);
3302 mutex_exit(&buf
->b_evict_lock
);
3304 } else if (buf
->b_data
== NULL
) {
3305 arc_buf_t copy
= *buf
; /* structure assignment */
3307 * We are on the eviction list; process this buffer now
3308 * but let arc_do_user_evicts() do the reaping.
3310 buf
->b_efunc
= NULL
;
3311 mutex_exit(&buf
->b_evict_lock
);
3312 VERIFY(copy
.b_efunc(©
) == 0);
3315 hash_lock
= HDR_LOCK(hdr
);
3316 mutex_enter(hash_lock
);
3318 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3320 ASSERT3U(refcount_count(&hdr
->b_refcnt
), <, hdr
->b_datacnt
);
3321 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
3324 * Pull this buffer off of the hdr
3327 while (*bufp
!= buf
)
3328 bufp
= &(*bufp
)->b_next
;
3329 *bufp
= buf
->b_next
;
3331 ASSERT(buf
->b_data
!= NULL
);
3332 arc_buf_destroy(buf
, FALSE
, FALSE
);
3334 if (hdr
->b_datacnt
== 0) {
3335 arc_state_t
*old_state
= hdr
->b_state
;
3336 arc_state_t
*evicted_state
;
3338 ASSERT(hdr
->b_buf
== NULL
);
3339 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
3342 (old_state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
3344 mutex_enter(&old_state
->arcs_mtx
);
3345 mutex_enter(&evicted_state
->arcs_mtx
);
3347 arc_change_state(evicted_state
, hdr
, hash_lock
);
3348 ASSERT(HDR_IN_HASH_TABLE(hdr
));
3349 hdr
->b_flags
|= ARC_IN_HASH_TABLE
;
3350 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
3352 mutex_exit(&evicted_state
->arcs_mtx
);
3353 mutex_exit(&old_state
->arcs_mtx
);
3355 mutex_exit(hash_lock
);
3356 mutex_exit(&buf
->b_evict_lock
);
3358 VERIFY(buf
->b_efunc(buf
) == 0);
3359 buf
->b_efunc
= NULL
;
3360 buf
->b_private
= NULL
;
3363 kmem_cache_free(buf_cache
, buf
);
3368 * Release this buffer from the cache. This must be done
3369 * after a read and prior to modifying the buffer contents.
3370 * If the buffer has more than one reference, we must make
3371 * a new hdr for the buffer.
3374 arc_release(arc_buf_t
*buf
, void *tag
)
3377 kmutex_t
*hash_lock
= NULL
;
3378 l2arc_buf_hdr_t
*l2hdr
;
3379 uint64_t buf_size
= 0;
3382 * It would be nice to assert that if it's DMU metadata (level >
3383 * 0 || it's the dnode file), then it must be syncing context.
3384 * But we don't know that information at this level.
3387 mutex_enter(&buf
->b_evict_lock
);
3390 /* this buffer is not on any list */
3391 ASSERT(refcount_count(&hdr
->b_refcnt
) > 0);
3393 if (hdr
->b_state
== arc_anon
) {
3394 /* this buffer is already released */
3395 ASSERT(buf
->b_efunc
== NULL
);
3397 hash_lock
= HDR_LOCK(hdr
);
3398 mutex_enter(hash_lock
);
3400 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3403 l2hdr
= hdr
->b_l2hdr
;
3405 mutex_enter(&l2arc_buflist_mtx
);
3406 hdr
->b_l2hdr
= NULL
;
3407 buf_size
= hdr
->b_size
;
3411 * Do we have more than one buf?
3413 if (hdr
->b_datacnt
> 1) {
3414 arc_buf_hdr_t
*nhdr
;
3416 uint64_t blksz
= hdr
->b_size
;
3417 uint64_t spa
= hdr
->b_spa
;
3418 arc_buf_contents_t type
= hdr
->b_type
;
3419 uint32_t flags
= hdr
->b_flags
;
3421 ASSERT(hdr
->b_buf
!= buf
|| buf
->b_next
!= NULL
);
3423 * Pull the data off of this hdr and attach it to
3424 * a new anonymous hdr.
3426 (void) remove_reference(hdr
, hash_lock
, tag
);
3428 while (*bufp
!= buf
)
3429 bufp
= &(*bufp
)->b_next
;
3430 *bufp
= buf
->b_next
;
3433 ASSERT3U(hdr
->b_state
->arcs_size
, >=, hdr
->b_size
);
3434 atomic_add_64(&hdr
->b_state
->arcs_size
, -hdr
->b_size
);
3435 if (refcount_is_zero(&hdr
->b_refcnt
)) {
3436 uint64_t *size
= &hdr
->b_state
->arcs_lsize
[hdr
->b_type
];
3437 ASSERT3U(*size
, >=, hdr
->b_size
);
3438 atomic_add_64(size
, -hdr
->b_size
);
3442 * We're releasing a duplicate user data buffer, update
3443 * our statistics accordingly.
3445 if (hdr
->b_type
== ARC_BUFC_DATA
) {
3446 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers
);
3447 ARCSTAT_INCR(arcstat_duplicate_buffers_size
,
3450 hdr
->b_datacnt
-= 1;
3451 arc_cksum_verify(buf
);
3453 mutex_exit(hash_lock
);
3455 nhdr
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
3456 nhdr
->b_size
= blksz
;
3458 nhdr
->b_type
= type
;
3460 nhdr
->b_state
= arc_anon
;
3461 nhdr
->b_arc_access
= 0;
3462 nhdr
->b_flags
= flags
& ARC_L2_WRITING
;
3463 nhdr
->b_l2hdr
= NULL
;
3464 nhdr
->b_datacnt
= 1;
3465 nhdr
->b_freeze_cksum
= NULL
;
3466 (void) refcount_add(&nhdr
->b_refcnt
, tag
);
3468 mutex_exit(&buf
->b_evict_lock
);
3469 atomic_add_64(&arc_anon
->arcs_size
, blksz
);
3471 mutex_exit(&buf
->b_evict_lock
);
3472 ASSERT(refcount_count(&hdr
->b_refcnt
) == 1);
3473 ASSERT(!list_link_active(&hdr
->b_arc_node
));
3474 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3475 if (hdr
->b_state
!= arc_anon
)
3476 arc_change_state(arc_anon
, hdr
, hash_lock
);
3477 hdr
->b_arc_access
= 0;
3479 mutex_exit(hash_lock
);
3481 buf_discard_identity(hdr
);
3484 buf
->b_efunc
= NULL
;
3485 buf
->b_private
= NULL
;
3488 ARCSTAT_INCR(arcstat_l2_asize
, -l2hdr
->b_asize
);
3489 list_remove(l2hdr
->b_dev
->l2ad_buflist
, hdr
);
3490 kmem_free(l2hdr
, sizeof (l2arc_buf_hdr_t
));
3491 arc_space_return(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
3492 ARCSTAT_INCR(arcstat_l2_size
, -buf_size
);
3493 mutex_exit(&l2arc_buflist_mtx
);
3498 arc_released(arc_buf_t
*buf
)
3502 mutex_enter(&buf
->b_evict_lock
);
3503 released
= (buf
->b_data
!= NULL
&& buf
->b_hdr
->b_state
== arc_anon
);
3504 mutex_exit(&buf
->b_evict_lock
);
3509 arc_has_callback(arc_buf_t
*buf
)
3513 mutex_enter(&buf
->b_evict_lock
);
3514 callback
= (buf
->b_efunc
!= NULL
);
3515 mutex_exit(&buf
->b_evict_lock
);
3521 arc_referenced(arc_buf_t
*buf
)
3525 mutex_enter(&buf
->b_evict_lock
);
3526 referenced
= (refcount_count(&buf
->b_hdr
->b_refcnt
));
3527 mutex_exit(&buf
->b_evict_lock
);
3528 return (referenced
);
3533 arc_write_ready(zio_t
*zio
)
3535 arc_write_callback_t
*callback
= zio
->io_private
;
3536 arc_buf_t
*buf
= callback
->awcb_buf
;
3537 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3539 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_refcnt
));
3540 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
3543 * If the IO is already in progress, then this is a re-write
3544 * attempt, so we need to thaw and re-compute the cksum.
3545 * It is the responsibility of the callback to handle the
3546 * accounting for any re-write attempt.
3548 if (HDR_IO_IN_PROGRESS(hdr
)) {
3549 mutex_enter(&hdr
->b_freeze_lock
);
3550 if (hdr
->b_freeze_cksum
!= NULL
) {
3551 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
3552 hdr
->b_freeze_cksum
= NULL
;
3554 mutex_exit(&hdr
->b_freeze_lock
);
3556 arc_cksum_compute(buf
, B_FALSE
);
3557 hdr
->b_flags
|= ARC_IO_IN_PROGRESS
;
3561 arc_write_done(zio_t
*zio
)
3563 arc_write_callback_t
*callback
= zio
->io_private
;
3564 arc_buf_t
*buf
= callback
->awcb_buf
;
3565 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3567 ASSERT(hdr
->b_acb
== NULL
);
3569 if (zio
->io_error
== 0) {
3570 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
3571 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
3572 hdr
->b_cksum0
= zio
->io_bp
->blk_cksum
.zc_word
[0];
3574 ASSERT(BUF_EMPTY(hdr
));
3578 * If the block to be written was all-zero, we may have
3579 * compressed it away. In this case no write was performed
3580 * so there will be no dva/birth/checksum. The buffer must
3581 * therefore remain anonymous (and uncached).
3583 if (!BUF_EMPTY(hdr
)) {
3584 arc_buf_hdr_t
*exists
;
3585 kmutex_t
*hash_lock
;
3587 ASSERT(zio
->io_error
== 0);
3589 arc_cksum_verify(buf
);
3591 exists
= buf_hash_insert(hdr
, &hash_lock
);
3594 * This can only happen if we overwrite for
3595 * sync-to-convergence, because we remove
3596 * buffers from the hash table when we arc_free().
3598 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
3599 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
3600 panic("bad overwrite, hdr=%p exists=%p",
3601 (void *)hdr
, (void *)exists
);
3602 ASSERT(refcount_is_zero(&exists
->b_refcnt
));
3603 arc_change_state(arc_anon
, exists
, hash_lock
);
3604 mutex_exit(hash_lock
);
3605 arc_hdr_destroy(exists
);
3606 exists
= buf_hash_insert(hdr
, &hash_lock
);
3607 ASSERT3P(exists
, ==, NULL
);
3610 ASSERT(hdr
->b_datacnt
== 1);
3611 ASSERT(hdr
->b_state
== arc_anon
);
3612 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
3613 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
3616 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3617 /* if it's not anon, we are doing a scrub */
3618 if (!exists
&& hdr
->b_state
== arc_anon
)
3619 arc_access(hdr
, hash_lock
);
3620 mutex_exit(hash_lock
);
3622 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3625 ASSERT(!refcount_is_zero(&hdr
->b_refcnt
));
3626 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
3628 kmem_free(callback
, sizeof (arc_write_callback_t
));
3632 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
3633 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
, boolean_t l2arc_compress
,
3634 const zio_prop_t
*zp
, arc_done_func_t
*ready
, arc_done_func_t
*done
,
3635 void *private, int priority
, int zio_flags
, const zbookmark_t
*zb
)
3637 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3638 arc_write_callback_t
*callback
;
3641 ASSERT(ready
!= NULL
);
3642 ASSERT(done
!= NULL
);
3643 ASSERT(!HDR_IO_ERROR(hdr
));
3644 ASSERT((hdr
->b_flags
& ARC_IO_IN_PROGRESS
) == 0);
3645 ASSERT(hdr
->b_acb
== NULL
);
3647 hdr
->b_flags
|= ARC_L2CACHE
;
3649 hdr
->b_flags
|= ARC_L2COMPRESS
;
3650 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_PUSHPAGE
);
3651 callback
->awcb_ready
= ready
;
3652 callback
->awcb_done
= done
;
3653 callback
->awcb_private
= private;
3654 callback
->awcb_buf
= buf
;
3656 zio
= zio_write(pio
, spa
, txg
, bp
, buf
->b_data
, hdr
->b_size
, zp
,
3657 arc_write_ready
, arc_write_done
, callback
, priority
, zio_flags
, zb
);
3663 arc_memory_throttle(uint64_t reserve
, uint64_t inflight_data
, uint64_t txg
)
3666 uint64_t available_memory
;
3668 if (zfs_arc_memory_throttle_disable
)
3671 /* Easily reclaimable memory (free + inactive + arc-evictable) */
3672 available_memory
= ptob(spl_kmem_availrmem()) + arc_evictable_memory();
3674 if (available_memory
<= zfs_write_limit_max
) {
3675 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
3676 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
3680 if (inflight_data
> available_memory
/ 4) {
3681 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
3682 DMU_TX_STAT_BUMP(dmu_tx_memory_inflight
);
3690 arc_tempreserve_clear(uint64_t reserve
)
3692 atomic_add_64(&arc_tempreserve
, -reserve
);
3693 ASSERT((int64_t)arc_tempreserve
>= 0);
3697 arc_tempreserve_space(uint64_t reserve
, uint64_t txg
)
3704 * Once in a while, fail for no reason. Everything should cope.
3706 if (spa_get_random(10000) == 0) {
3707 dprintf("forcing random failure\n");
3711 if (reserve
> arc_c
/4 && !arc_no_grow
)
3712 arc_c
= MIN(arc_c_max
, reserve
* 4);
3713 if (reserve
> arc_c
) {
3714 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve
);
3719 * Don't count loaned bufs as in flight dirty data to prevent long
3720 * network delays from blocking transactions that are ready to be
3721 * assigned to a txg.
3723 anon_size
= MAX((int64_t)(arc_anon
->arcs_size
- arc_loaned_bytes
), 0);
3726 * Writes will, almost always, require additional memory allocations
3727 * in order to compress/encrypt/etc the data. We therefor need to
3728 * make sure that there is sufficient available memory for this.
3730 if ((error
= arc_memory_throttle(reserve
, anon_size
, txg
)))
3734 * Throttle writes when the amount of dirty data in the cache
3735 * gets too large. We try to keep the cache less than half full
3736 * of dirty blocks so that our sync times don't grow too large.
3737 * Note: if two requests come in concurrently, we might let them
3738 * both succeed, when one of them should fail. Not a huge deal.
3741 if (reserve
+ arc_tempreserve
+ anon_size
> arc_c
/ 2 &&
3742 anon_size
> arc_c
/ 4) {
3743 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3744 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3745 arc_tempreserve
>>10,
3746 arc_anon
->arcs_lsize
[ARC_BUFC_METADATA
]>>10,
3747 arc_anon
->arcs_lsize
[ARC_BUFC_DATA
]>>10,
3748 reserve
>>10, arc_c
>>10);
3749 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle
);
3752 atomic_add_64(&arc_tempreserve
, reserve
);
3757 arc_kstat_update_state(arc_state_t
*state
, kstat_named_t
*size
,
3758 kstat_named_t
*evict_data
, kstat_named_t
*evict_metadata
)
3760 size
->value
.ui64
= state
->arcs_size
;
3761 evict_data
->value
.ui64
= state
->arcs_lsize
[ARC_BUFC_DATA
];
3762 evict_metadata
->value
.ui64
= state
->arcs_lsize
[ARC_BUFC_METADATA
];
3766 arc_kstat_update(kstat_t
*ksp
, int rw
)
3768 arc_stats_t
*as
= ksp
->ks_data
;
3770 if (rw
== KSTAT_WRITE
) {
3773 arc_kstat_update_state(arc_anon
,
3774 &as
->arcstat_anon_size
,
3775 &as
->arcstat_anon_evict_data
,
3776 &as
->arcstat_anon_evict_metadata
);
3777 arc_kstat_update_state(arc_mru
,
3778 &as
->arcstat_mru_size
,
3779 &as
->arcstat_mru_evict_data
,
3780 &as
->arcstat_mru_evict_metadata
);
3781 arc_kstat_update_state(arc_mru_ghost
,
3782 &as
->arcstat_mru_ghost_size
,
3783 &as
->arcstat_mru_ghost_evict_data
,
3784 &as
->arcstat_mru_ghost_evict_metadata
);
3785 arc_kstat_update_state(arc_mfu
,
3786 &as
->arcstat_mfu_size
,
3787 &as
->arcstat_mfu_evict_data
,
3788 &as
->arcstat_mfu_evict_metadata
);
3789 arc_kstat_update_state(arc_mfu_ghost
,
3790 &as
->arcstat_mfu_ghost_size
,
3791 &as
->arcstat_mfu_ghost_evict_data
,
3792 &as
->arcstat_mfu_ghost_evict_metadata
);
3801 mutex_init(&arc_reclaim_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3802 cv_init(&arc_reclaim_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
3804 /* Convert seconds to clock ticks */
3805 zfs_arc_min_prefetch_lifespan
= 1 * hz
;
3807 /* Start out with 1/8 of all memory */
3808 arc_c
= physmem
* PAGESIZE
/ 8;
3812 * On architectures where the physical memory can be larger
3813 * than the addressable space (intel in 32-bit mode), we may
3814 * need to limit the cache to 1/8 of VM size.
3816 arc_c
= MIN(arc_c
, vmem_size(heap_arena
, VMEM_ALLOC
| VMEM_FREE
) / 8);
3818 * Register a shrinker to support synchronous (direct) memory
3819 * reclaim from the arc. This is done to prevent kswapd from
3820 * swapping out pages when it is preferable to shrink the arc.
3822 spl_register_shrinker(&arc_shrinker
);
3825 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3826 arc_c_min
= MAX(arc_c
/ 4, 64<<20);
3827 /* set max to 1/2 of all memory */
3828 arc_c_max
= MAX(arc_c
* 4, arc_c_max
);
3831 * Allow the tunables to override our calculations if they are
3832 * reasonable (ie. over 64MB)
3834 if (zfs_arc_max
> 64<<20 && zfs_arc_max
< physmem
* PAGESIZE
)
3835 arc_c_max
= zfs_arc_max
;
3836 if (zfs_arc_min
> 64<<20 && zfs_arc_min
<= arc_c_max
)
3837 arc_c_min
= zfs_arc_min
;
3840 arc_p
= (arc_c
>> 1);
3842 /* limit meta-data to 1/4 of the arc capacity */
3843 arc_meta_limit
= arc_c_max
/ 4;
3846 /* Allow the tunable to override if it is reasonable */
3847 if (zfs_arc_meta_limit
> 0 && zfs_arc_meta_limit
<= arc_c_max
)
3848 arc_meta_limit
= zfs_arc_meta_limit
;
3850 if (arc_c_min
< arc_meta_limit
/ 2 && zfs_arc_min
== 0)
3851 arc_c_min
= arc_meta_limit
/ 2;
3853 /* if kmem_flags are set, lets try to use less memory */
3854 if (kmem_debugging())
3856 if (arc_c
< arc_c_min
)
3859 arc_anon
= &ARC_anon
;
3861 arc_mru_ghost
= &ARC_mru_ghost
;
3863 arc_mfu_ghost
= &ARC_mfu_ghost
;
3864 arc_l2c_only
= &ARC_l2c_only
;
3867 mutex_init(&arc_anon
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3868 mutex_init(&arc_mru
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3869 mutex_init(&arc_mru_ghost
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3870 mutex_init(&arc_mfu
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3871 mutex_init(&arc_mfu_ghost
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3872 mutex_init(&arc_l2c_only
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3874 list_create(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
3875 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3876 list_create(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
3877 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3878 list_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
3879 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3880 list_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
3881 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3882 list_create(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
3883 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3884 list_create(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
3885 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3886 list_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
3887 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3888 list_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
3889 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3890 list_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
3891 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3892 list_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
3893 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3897 arc_thread_exit
= 0;
3898 list_create(&arc_prune_list
, sizeof (arc_prune_t
),
3899 offsetof(arc_prune_t
, p_node
));
3900 arc_eviction_list
= NULL
;
3901 mutex_init(&arc_prune_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3902 mutex_init(&arc_eviction_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3903 bzero(&arc_eviction_hdr
, sizeof (arc_buf_hdr_t
));
3905 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
3906 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
3908 if (arc_ksp
!= NULL
) {
3909 arc_ksp
->ks_data
= &arc_stats
;
3910 arc_ksp
->ks_update
= arc_kstat_update
;
3911 kstat_install(arc_ksp
);
3914 (void) thread_create(NULL
, 0, arc_adapt_thread
, NULL
, 0, &p0
,
3915 TS_RUN
, minclsyspri
);
3920 if (zfs_write_limit_max
== 0)
3921 zfs_write_limit_max
= ptob(physmem
) >> zfs_write_limit_shift
;
3923 zfs_write_limit_shift
= 0;
3924 mutex_init(&zfs_write_limit_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3932 mutex_enter(&arc_reclaim_thr_lock
);
3934 spl_unregister_shrinker(&arc_shrinker
);
3935 #endif /* _KERNEL */
3937 arc_thread_exit
= 1;
3938 while (arc_thread_exit
!= 0)
3939 cv_wait(&arc_reclaim_thr_cv
, &arc_reclaim_thr_lock
);
3940 mutex_exit(&arc_reclaim_thr_lock
);
3946 if (arc_ksp
!= NULL
) {
3947 kstat_delete(arc_ksp
);
3951 mutex_enter(&arc_prune_mtx
);
3952 while ((p
= list_head(&arc_prune_list
)) != NULL
) {
3953 list_remove(&arc_prune_list
, p
);
3954 refcount_remove(&p
->p_refcnt
, &arc_prune_list
);
3955 refcount_destroy(&p
->p_refcnt
);
3956 kmem_free(p
, sizeof (*p
));
3958 mutex_exit(&arc_prune_mtx
);
3960 list_destroy(&arc_prune_list
);
3961 mutex_destroy(&arc_prune_mtx
);
3962 mutex_destroy(&arc_eviction_mtx
);
3963 mutex_destroy(&arc_reclaim_thr_lock
);
3964 cv_destroy(&arc_reclaim_thr_cv
);
3966 list_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
3967 list_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
3968 list_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
3969 list_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
3970 list_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
3971 list_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
3972 list_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
3973 list_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
3975 mutex_destroy(&arc_anon
->arcs_mtx
);
3976 mutex_destroy(&arc_mru
->arcs_mtx
);
3977 mutex_destroy(&arc_mru_ghost
->arcs_mtx
);
3978 mutex_destroy(&arc_mfu
->arcs_mtx
);
3979 mutex_destroy(&arc_mfu_ghost
->arcs_mtx
);
3980 mutex_destroy(&arc_l2c_only
->arcs_mtx
);
3982 mutex_destroy(&zfs_write_limit_lock
);
3986 ASSERT(arc_loaned_bytes
== 0);
3992 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3993 * It uses dedicated storage devices to hold cached data, which are populated
3994 * using large infrequent writes. The main role of this cache is to boost
3995 * the performance of random read workloads. The intended L2ARC devices
3996 * include short-stroked disks, solid state disks, and other media with
3997 * substantially faster read latency than disk.
3999 * +-----------------------+
4001 * +-----------------------+
4004 * l2arc_feed_thread() arc_read()
4008 * +---------------+ |
4010 * +---------------+ |
4015 * +-------+ +-------+
4017 * | cache | | cache |
4018 * +-------+ +-------+
4019 * +=========+ .-----.
4020 * : L2ARC : |-_____-|
4021 * : devices : | Disks |
4022 * +=========+ `-_____-'
4024 * Read requests are satisfied from the following sources, in order:
4027 * 2) vdev cache of L2ARC devices
4029 * 4) vdev cache of disks
4032 * Some L2ARC device types exhibit extremely slow write performance.
4033 * To accommodate for this there are some significant differences between
4034 * the L2ARC and traditional cache design:
4036 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
4037 * the ARC behave as usual, freeing buffers and placing headers on ghost
4038 * lists. The ARC does not send buffers to the L2ARC during eviction as
4039 * this would add inflated write latencies for all ARC memory pressure.
4041 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
4042 * It does this by periodically scanning buffers from the eviction-end of
4043 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
4044 * not already there. It scans until a headroom of buffers is satisfied,
4045 * which itself is a buffer for ARC eviction. If a compressible buffer is
4046 * found during scanning and selected for writing to an L2ARC device, we
4047 * temporarily boost scanning headroom during the next scan cycle to make
4048 * sure we adapt to compression effects (which might significantly reduce
4049 * the data volume we write to L2ARC). The thread that does this is
4050 * l2arc_feed_thread(), illustrated below; example sizes are included to
4051 * provide a better sense of ratio than this diagram:
4054 * +---------------------+----------+
4055 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
4056 * +---------------------+----------+ | o L2ARC eligible
4057 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
4058 * +---------------------+----------+ |
4059 * 15.9 Gbytes ^ 32 Mbytes |
4061 * l2arc_feed_thread()
4063 * l2arc write hand <--[oooo]--'
4067 * +==============================+
4068 * L2ARC dev |####|#|###|###| |####| ... |
4069 * +==============================+
4072 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
4073 * evicted, then the L2ARC has cached a buffer much sooner than it probably
4074 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
4075 * safe to say that this is an uncommon case, since buffers at the end of
4076 * the ARC lists have moved there due to inactivity.
4078 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
4079 * then the L2ARC simply misses copying some buffers. This serves as a
4080 * pressure valve to prevent heavy read workloads from both stalling the ARC
4081 * with waits and clogging the L2ARC with writes. This also helps prevent
4082 * the potential for the L2ARC to churn if it attempts to cache content too
4083 * quickly, such as during backups of the entire pool.
4085 * 5. After system boot and before the ARC has filled main memory, there are
4086 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
4087 * lists can remain mostly static. Instead of searching from tail of these
4088 * lists as pictured, the l2arc_feed_thread() will search from the list heads
4089 * for eligible buffers, greatly increasing its chance of finding them.
4091 * The L2ARC device write speed is also boosted during this time so that
4092 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
4093 * there are no L2ARC reads, and no fear of degrading read performance
4094 * through increased writes.
4096 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
4097 * the vdev queue can aggregate them into larger and fewer writes. Each
4098 * device is written to in a rotor fashion, sweeping writes through
4099 * available space then repeating.
4101 * 7. The L2ARC does not store dirty content. It never needs to flush
4102 * write buffers back to disk based storage.
4104 * 8. If an ARC buffer is written (and dirtied) which also exists in the
4105 * L2ARC, the now stale L2ARC buffer is immediately dropped.
4107 * The performance of the L2ARC can be tweaked by a number of tunables, which
4108 * may be necessary for different workloads:
4110 * l2arc_write_max max write bytes per interval
4111 * l2arc_write_boost extra write bytes during device warmup
4112 * l2arc_noprefetch skip caching prefetched buffers
4113 * l2arc_nocompress skip compressing buffers
4114 * l2arc_headroom number of max device writes to precache
4115 * l2arc_headroom_boost when we find compressed buffers during ARC
4116 * scanning, we multiply headroom by this
4117 * percentage factor for the next scan cycle,
4118 * since more compressed buffers are likely to
4120 * l2arc_feed_secs seconds between L2ARC writing
4122 * Tunables may be removed or added as future performance improvements are
4123 * integrated, and also may become zpool properties.
4125 * There are three key functions that control how the L2ARC warms up:
4127 * l2arc_write_eligible() check if a buffer is eligible to cache
4128 * l2arc_write_size() calculate how much to write
4129 * l2arc_write_interval() calculate sleep delay between writes
4131 * These three functions determine what to write, how much, and how quickly
4136 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*ab
)
4139 * A buffer is *not* eligible for the L2ARC if it:
4140 * 1. belongs to a different spa.
4141 * 2. is already cached on the L2ARC.
4142 * 3. has an I/O in progress (it may be an incomplete read).
4143 * 4. is flagged not eligible (zfs property).
4145 if (ab
->b_spa
!= spa_guid
|| ab
->b_l2hdr
!= NULL
||
4146 HDR_IO_IN_PROGRESS(ab
) || !HDR_L2CACHE(ab
))
4153 l2arc_write_size(void)
4158 * Make sure our globals have meaningful values in case the user
4161 size
= l2arc_write_max
;
4163 cmn_err(CE_NOTE
, "Bad value for l2arc_write_max, value must "
4164 "be greater than zero, resetting it to the default (%d)",
4166 size
= l2arc_write_max
= L2ARC_WRITE_SIZE
;
4169 if (arc_warm
== B_FALSE
)
4170 size
+= l2arc_write_boost
;
4177 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
4179 clock_t interval
, next
, now
;
4182 * If the ARC lists are busy, increase our write rate; if the
4183 * lists are stale, idle back. This is achieved by checking
4184 * how much we previously wrote - if it was more than half of
4185 * what we wanted, schedule the next write much sooner.
4187 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
4188 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
4190 interval
= hz
* l2arc_feed_secs
;
4192 now
= ddi_get_lbolt();
4193 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
4199 l2arc_hdr_stat_add(void)
4201 ARCSTAT_INCR(arcstat_l2_hdr_size
, HDR_SIZE
);
4202 ARCSTAT_INCR(arcstat_hdr_size
, -HDR_SIZE
);
4206 l2arc_hdr_stat_remove(void)
4208 ARCSTAT_INCR(arcstat_l2_hdr_size
, -HDR_SIZE
);
4209 ARCSTAT_INCR(arcstat_hdr_size
, HDR_SIZE
);
4213 * Cycle through L2ARC devices. This is how L2ARC load balances.
4214 * If a device is returned, this also returns holding the spa config lock.
4216 static l2arc_dev_t
*
4217 l2arc_dev_get_next(void)
4219 l2arc_dev_t
*first
, *next
= NULL
;
4222 * Lock out the removal of spas (spa_namespace_lock), then removal
4223 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
4224 * both locks will be dropped and a spa config lock held instead.
4226 mutex_enter(&spa_namespace_lock
);
4227 mutex_enter(&l2arc_dev_mtx
);
4229 /* if there are no vdevs, there is nothing to do */
4230 if (l2arc_ndev
== 0)
4234 next
= l2arc_dev_last
;
4236 /* loop around the list looking for a non-faulted vdev */
4238 next
= list_head(l2arc_dev_list
);
4240 next
= list_next(l2arc_dev_list
, next
);
4242 next
= list_head(l2arc_dev_list
);
4245 /* if we have come back to the start, bail out */
4248 else if (next
== first
)
4251 } while (vdev_is_dead(next
->l2ad_vdev
));
4253 /* if we were unable to find any usable vdevs, return NULL */
4254 if (vdev_is_dead(next
->l2ad_vdev
))
4257 l2arc_dev_last
= next
;
4260 mutex_exit(&l2arc_dev_mtx
);
4263 * Grab the config lock to prevent the 'next' device from being
4264 * removed while we are writing to it.
4267 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
4268 mutex_exit(&spa_namespace_lock
);
4274 * Free buffers that were tagged for destruction.
4277 l2arc_do_free_on_write(void)
4280 l2arc_data_free_t
*df
, *df_prev
;
4282 mutex_enter(&l2arc_free_on_write_mtx
);
4283 buflist
= l2arc_free_on_write
;
4285 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
4286 df_prev
= list_prev(buflist
, df
);
4287 ASSERT(df
->l2df_data
!= NULL
);
4288 ASSERT(df
->l2df_func
!= NULL
);
4289 df
->l2df_func(df
->l2df_data
, df
->l2df_size
);
4290 list_remove(buflist
, df
);
4291 kmem_free(df
, sizeof (l2arc_data_free_t
));
4294 mutex_exit(&l2arc_free_on_write_mtx
);
4298 * A write to a cache device has completed. Update all headers to allow
4299 * reads from these buffers to begin.
4302 l2arc_write_done(zio_t
*zio
)
4304 l2arc_write_callback_t
*cb
;
4307 arc_buf_hdr_t
*head
, *ab
, *ab_prev
;
4308 l2arc_buf_hdr_t
*abl2
;
4309 kmutex_t
*hash_lock
;
4311 cb
= zio
->io_private
;
4313 dev
= cb
->l2wcb_dev
;
4314 ASSERT(dev
!= NULL
);
4315 head
= cb
->l2wcb_head
;
4316 ASSERT(head
!= NULL
);
4317 buflist
= dev
->l2ad_buflist
;
4318 ASSERT(buflist
!= NULL
);
4319 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
4320 l2arc_write_callback_t
*, cb
);
4322 if (zio
->io_error
!= 0)
4323 ARCSTAT_BUMP(arcstat_l2_writes_error
);
4325 mutex_enter(&l2arc_buflist_mtx
);
4328 * All writes completed, or an error was hit.
4330 for (ab
= list_prev(buflist
, head
); ab
; ab
= ab_prev
) {
4331 ab_prev
= list_prev(buflist
, ab
);
4333 hash_lock
= HDR_LOCK(ab
);
4334 if (!mutex_tryenter(hash_lock
)) {
4336 * This buffer misses out. It may be in a stage
4337 * of eviction. Its ARC_L2_WRITING flag will be
4338 * left set, denying reads to this buffer.
4340 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss
);
4347 * Release the temporary compressed buffer as soon as possible.
4349 if (abl2
->b_compress
!= ZIO_COMPRESS_OFF
)
4350 l2arc_release_cdata_buf(ab
);
4352 if (zio
->io_error
!= 0) {
4354 * Error - drop L2ARC entry.
4356 list_remove(buflist
, ab
);
4357 ARCSTAT_INCR(arcstat_l2_asize
, -abl2
->b_asize
);
4359 kmem_free(abl2
, sizeof (l2arc_buf_hdr_t
));
4360 arc_space_return(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
4361 ARCSTAT_INCR(arcstat_l2_size
, -ab
->b_size
);
4365 * Allow ARC to begin reads to this L2ARC entry.
4367 ab
->b_flags
&= ~ARC_L2_WRITING
;
4369 mutex_exit(hash_lock
);
4372 atomic_inc_64(&l2arc_writes_done
);
4373 list_remove(buflist
, head
);
4374 kmem_cache_free(hdr_cache
, head
);
4375 mutex_exit(&l2arc_buflist_mtx
);
4377 l2arc_do_free_on_write();
4379 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
4383 * A read to a cache device completed. Validate buffer contents before
4384 * handing over to the regular ARC routines.
4387 l2arc_read_done(zio_t
*zio
)
4389 l2arc_read_callback_t
*cb
;
4392 kmutex_t
*hash_lock
;
4395 ASSERT(zio
->io_vd
!= NULL
);
4396 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
4398 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
4400 cb
= zio
->io_private
;
4402 buf
= cb
->l2rcb_buf
;
4403 ASSERT(buf
!= NULL
);
4405 hash_lock
= HDR_LOCK(buf
->b_hdr
);
4406 mutex_enter(hash_lock
);
4408 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
4411 * If the buffer was compressed, decompress it first.
4413 if (cb
->l2rcb_compress
!= ZIO_COMPRESS_OFF
)
4414 l2arc_decompress_zio(zio
, hdr
, cb
->l2rcb_compress
);
4415 ASSERT(zio
->io_data
!= NULL
);
4418 * Check this survived the L2ARC journey.
4420 equal
= arc_cksum_equal(buf
);
4421 if (equal
&& zio
->io_error
== 0 && !HDR_L2_EVICTED(hdr
)) {
4422 mutex_exit(hash_lock
);
4423 zio
->io_private
= buf
;
4424 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
4425 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
4428 mutex_exit(hash_lock
);
4430 * Buffer didn't survive caching. Increment stats and
4431 * reissue to the original storage device.
4433 if (zio
->io_error
!= 0) {
4434 ARCSTAT_BUMP(arcstat_l2_io_error
);
4436 zio
->io_error
= EIO
;
4439 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
4442 * If there's no waiter, issue an async i/o to the primary
4443 * storage now. If there *is* a waiter, the caller must
4444 * issue the i/o in a context where it's OK to block.
4446 if (zio
->io_waiter
== NULL
) {
4447 zio_t
*pio
= zio_unique_parent(zio
);
4449 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
4451 zio_nowait(zio_read(pio
, cb
->l2rcb_spa
, &cb
->l2rcb_bp
,
4452 buf
->b_data
, zio
->io_size
, arc_read_done
, buf
,
4453 zio
->io_priority
, cb
->l2rcb_flags
, &cb
->l2rcb_zb
));
4457 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
4461 * This is the list priority from which the L2ARC will search for pages to
4462 * cache. This is used within loops (0..3) to cycle through lists in the
4463 * desired order. This order can have a significant effect on cache
4466 * Currently the metadata lists are hit first, MFU then MRU, followed by
4467 * the data lists. This function returns a locked list, and also returns
4471 l2arc_list_locked(int list_num
, kmutex_t
**lock
)
4473 list_t
*list
= NULL
;
4475 ASSERT(list_num
>= 0 && list_num
<= 3);
4479 list
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
4480 *lock
= &arc_mfu
->arcs_mtx
;
4483 list
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
4484 *lock
= &arc_mru
->arcs_mtx
;
4487 list
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
4488 *lock
= &arc_mfu
->arcs_mtx
;
4491 list
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
4492 *lock
= &arc_mru
->arcs_mtx
;
4496 ASSERT(!(MUTEX_HELD(*lock
)));
4502 * Evict buffers from the device write hand to the distance specified in
4503 * bytes. This distance may span populated buffers, it may span nothing.
4504 * This is clearing a region on the L2ARC device ready for writing.
4505 * If the 'all' boolean is set, every buffer is evicted.
4508 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
4511 l2arc_buf_hdr_t
*abl2
;
4512 arc_buf_hdr_t
*ab
, *ab_prev
;
4513 kmutex_t
*hash_lock
;
4516 buflist
= dev
->l2ad_buflist
;
4518 if (buflist
== NULL
)
4521 if (!all
&& dev
->l2ad_first
) {
4523 * This is the first sweep through the device. There is
4529 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
4531 * When nearing the end of the device, evict to the end
4532 * before the device write hand jumps to the start.
4534 taddr
= dev
->l2ad_end
;
4536 taddr
= dev
->l2ad_hand
+ distance
;
4538 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
4539 uint64_t, taddr
, boolean_t
, all
);
4542 mutex_enter(&l2arc_buflist_mtx
);
4543 for (ab
= list_tail(buflist
); ab
; ab
= ab_prev
) {
4544 ab_prev
= list_prev(buflist
, ab
);
4546 hash_lock
= HDR_LOCK(ab
);
4547 if (!mutex_tryenter(hash_lock
)) {
4549 * Missed the hash lock. Retry.
4551 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
4552 mutex_exit(&l2arc_buflist_mtx
);
4553 mutex_enter(hash_lock
);
4554 mutex_exit(hash_lock
);
4558 if (HDR_L2_WRITE_HEAD(ab
)) {
4560 * We hit a write head node. Leave it for
4561 * l2arc_write_done().
4563 list_remove(buflist
, ab
);
4564 mutex_exit(hash_lock
);
4568 if (!all
&& ab
->b_l2hdr
!= NULL
&&
4569 (ab
->b_l2hdr
->b_daddr
> taddr
||
4570 ab
->b_l2hdr
->b_daddr
< dev
->l2ad_hand
)) {
4572 * We've evicted to the target address,
4573 * or the end of the device.
4575 mutex_exit(hash_lock
);
4579 if (HDR_FREE_IN_PROGRESS(ab
)) {
4581 * Already on the path to destruction.
4583 mutex_exit(hash_lock
);
4587 if (ab
->b_state
== arc_l2c_only
) {
4588 ASSERT(!HDR_L2_READING(ab
));
4590 * This doesn't exist in the ARC. Destroy.
4591 * arc_hdr_destroy() will call list_remove()
4592 * and decrement arcstat_l2_size.
4594 arc_change_state(arc_anon
, ab
, hash_lock
);
4595 arc_hdr_destroy(ab
);
4598 * Invalidate issued or about to be issued
4599 * reads, since we may be about to write
4600 * over this location.
4602 if (HDR_L2_READING(ab
)) {
4603 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
4604 ab
->b_flags
|= ARC_L2_EVICTED
;
4608 * Tell ARC this no longer exists in L2ARC.
4610 if (ab
->b_l2hdr
!= NULL
) {
4612 ARCSTAT_INCR(arcstat_l2_asize
, -abl2
->b_asize
);
4614 kmem_free(abl2
, sizeof (l2arc_buf_hdr_t
));
4615 arc_space_return(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
4616 ARCSTAT_INCR(arcstat_l2_size
, -ab
->b_size
);
4618 list_remove(buflist
, ab
);
4621 * This may have been leftover after a
4624 ab
->b_flags
&= ~ARC_L2_WRITING
;
4626 mutex_exit(hash_lock
);
4628 mutex_exit(&l2arc_buflist_mtx
);
4630 vdev_space_update(dev
->l2ad_vdev
, -(taddr
- dev
->l2ad_evict
), 0, 0);
4631 dev
->l2ad_evict
= taddr
;
4635 * Find and write ARC buffers to the L2ARC device.
4637 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4638 * for reading until they have completed writing.
4639 * The headroom_boost is an in-out parameter used to maintain headroom boost
4640 * state between calls to this function.
4642 * Returns the number of bytes actually written (which may be smaller than
4643 * the delta by which the device hand has changed due to alignment).
4646 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
,
4647 boolean_t
*headroom_boost
)
4649 arc_buf_hdr_t
*ab
, *ab_prev
, *head
;
4651 uint64_t write_asize
, write_psize
, write_sz
, headroom
,
4654 kmutex_t
*list_lock
= NULL
;
4656 l2arc_write_callback_t
*cb
;
4658 uint64_t guid
= spa_load_guid(spa
);
4660 const boolean_t do_headroom_boost
= *headroom_boost
;
4662 ASSERT(dev
->l2ad_vdev
!= NULL
);
4664 /* Lower the flag now, we might want to raise it again later. */
4665 *headroom_boost
= B_FALSE
;
4668 write_sz
= write_asize
= write_psize
= 0;
4670 head
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
4671 head
->b_flags
|= ARC_L2_WRITE_HEAD
;
4674 * We will want to try to compress buffers that are at least 2x the
4675 * device sector size.
4677 buf_compress_minsz
= 2 << dev
->l2ad_vdev
->vdev_ashift
;
4680 * Copy buffers for L2ARC writing.
4682 mutex_enter(&l2arc_buflist_mtx
);
4683 for (try = 0; try <= 3; try++) {
4684 uint64_t passed_sz
= 0;
4686 list
= l2arc_list_locked(try, &list_lock
);
4689 * L2ARC fast warmup.
4691 * Until the ARC is warm and starts to evict, read from the
4692 * head of the ARC lists rather than the tail.
4694 if (arc_warm
== B_FALSE
)
4695 ab
= list_head(list
);
4697 ab
= list_tail(list
);
4699 headroom
= target_sz
* l2arc_headroom
;
4700 if (do_headroom_boost
)
4701 headroom
= (headroom
* l2arc_headroom_boost
) / 100;
4703 for (; ab
; ab
= ab_prev
) {
4704 l2arc_buf_hdr_t
*l2hdr
;
4705 kmutex_t
*hash_lock
;
4708 if (arc_warm
== B_FALSE
)
4709 ab_prev
= list_next(list
, ab
);
4711 ab_prev
= list_prev(list
, ab
);
4713 hash_lock
= HDR_LOCK(ab
);
4714 if (!mutex_tryenter(hash_lock
)) {
4716 * Skip this buffer rather than waiting.
4721 passed_sz
+= ab
->b_size
;
4722 if (passed_sz
> headroom
) {
4726 mutex_exit(hash_lock
);
4730 if (!l2arc_write_eligible(guid
, ab
)) {
4731 mutex_exit(hash_lock
);
4735 if ((write_sz
+ ab
->b_size
) > target_sz
) {
4737 mutex_exit(hash_lock
);
4743 * Insert a dummy header on the buflist so
4744 * l2arc_write_done() can find where the
4745 * write buffers begin without searching.
4747 list_insert_head(dev
->l2ad_buflist
, head
);
4749 cb
= kmem_alloc(sizeof (l2arc_write_callback_t
),
4751 cb
->l2wcb_dev
= dev
;
4752 cb
->l2wcb_head
= head
;
4753 pio
= zio_root(spa
, l2arc_write_done
, cb
,
4758 * Create and add a new L2ARC header.
4760 l2hdr
= kmem_zalloc(sizeof (l2arc_buf_hdr_t
),
4763 arc_space_consume(L2HDR_SIZE
, ARC_SPACE_L2HDRS
);
4765 ab
->b_flags
|= ARC_L2_WRITING
;
4768 * Temporarily stash the data buffer in b_tmp_cdata.
4769 * The subsequent write step will pick it up from
4770 * there. This is because can't access ab->b_buf
4771 * without holding the hash_lock, which we in turn
4772 * can't access without holding the ARC list locks
4773 * (which we want to avoid during compression/writing)
4775 l2hdr
->b_compress
= ZIO_COMPRESS_OFF
;
4776 l2hdr
->b_asize
= ab
->b_size
;
4777 l2hdr
->b_tmp_cdata
= ab
->b_buf
->b_data
;
4779 buf_sz
= ab
->b_size
;
4780 ab
->b_l2hdr
= l2hdr
;
4782 list_insert_head(dev
->l2ad_buflist
, ab
);
4785 * Compute and store the buffer cksum before
4786 * writing. On debug the cksum is verified first.
4788 arc_cksum_verify(ab
->b_buf
);
4789 arc_cksum_compute(ab
->b_buf
, B_TRUE
);
4791 mutex_exit(hash_lock
);
4796 mutex_exit(list_lock
);
4802 /* No buffers selected for writing? */
4805 mutex_exit(&l2arc_buflist_mtx
);
4806 kmem_cache_free(hdr_cache
, head
);
4811 * Now start writing the buffers. We're starting at the write head
4812 * and work backwards, retracing the course of the buffer selector
4815 for (ab
= list_prev(dev
->l2ad_buflist
, head
); ab
;
4816 ab
= list_prev(dev
->l2ad_buflist
, ab
)) {
4817 l2arc_buf_hdr_t
*l2hdr
;
4821 * We shouldn't need to lock the buffer here, since we flagged
4822 * it as ARC_L2_WRITING in the previous step, but we must take
4823 * care to only access its L2 cache parameters. In particular,
4824 * ab->b_buf may be invalid by now due to ARC eviction.
4826 l2hdr
= ab
->b_l2hdr
;
4827 l2hdr
->b_daddr
= dev
->l2ad_hand
;
4829 if (!l2arc_nocompress
&& (ab
->b_flags
& ARC_L2COMPRESS
) &&
4830 l2hdr
->b_asize
>= buf_compress_minsz
) {
4831 if (l2arc_compress_buf(l2hdr
)) {
4833 * If compression succeeded, enable headroom
4834 * boost on the next scan cycle.
4836 *headroom_boost
= B_TRUE
;
4841 * Pick up the buffer data we had previously stashed away
4842 * (and now potentially also compressed).
4844 buf_data
= l2hdr
->b_tmp_cdata
;
4845 buf_sz
= l2hdr
->b_asize
;
4847 /* Compression may have squashed the buffer to zero length. */
4851 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
4852 dev
->l2ad_hand
, buf_sz
, buf_data
, ZIO_CHECKSUM_OFF
,
4853 NULL
, NULL
, ZIO_PRIORITY_ASYNC_WRITE
,
4854 ZIO_FLAG_CANFAIL
, B_FALSE
);
4856 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
4858 (void) zio_nowait(wzio
);
4860 write_asize
+= buf_sz
;
4862 * Keep the clock hand suitably device-aligned.
4864 buf_p_sz
= vdev_psize_to_asize(dev
->l2ad_vdev
, buf_sz
);
4865 write_psize
+= buf_p_sz
;
4866 dev
->l2ad_hand
+= buf_p_sz
;
4870 mutex_exit(&l2arc_buflist_mtx
);
4872 ASSERT3U(write_asize
, <=, target_sz
);
4873 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
4874 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_asize
);
4875 ARCSTAT_INCR(arcstat_l2_size
, write_sz
);
4876 ARCSTAT_INCR(arcstat_l2_asize
, write_asize
);
4877 vdev_space_update(dev
->l2ad_vdev
, write_psize
, 0, 0);
4880 * Bump device hand to the device start if it is approaching the end.
4881 * l2arc_evict() will already have evicted ahead for this case.
4883 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
4884 vdev_space_update(dev
->l2ad_vdev
,
4885 dev
->l2ad_end
- dev
->l2ad_hand
, 0, 0);
4886 dev
->l2ad_hand
= dev
->l2ad_start
;
4887 dev
->l2ad_evict
= dev
->l2ad_start
;
4888 dev
->l2ad_first
= B_FALSE
;
4891 dev
->l2ad_writing
= B_TRUE
;
4892 (void) zio_wait(pio
);
4893 dev
->l2ad_writing
= B_FALSE
;
4895 return (write_asize
);
4899 * Compresses an L2ARC buffer.
4900 * The data to be compressed must be prefilled in l2hdr->b_tmp_cdata and its
4901 * size in l2hdr->b_asize. This routine tries to compress the data and
4902 * depending on the compression result there are three possible outcomes:
4903 * *) The buffer was incompressible. The original l2hdr contents were left
4904 * untouched and are ready for writing to an L2 device.
4905 * *) The buffer was all-zeros, so there is no need to write it to an L2
4906 * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is
4907 * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY.
4908 * *) Compression succeeded and b_tmp_cdata was replaced with a temporary
4909 * data buffer which holds the compressed data to be written, and b_asize
4910 * tells us how much data there is. b_compress is set to the appropriate
4911 * compression algorithm. Once writing is done, invoke
4912 * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer.
4914 * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the
4915 * buffer was incompressible).
4918 l2arc_compress_buf(l2arc_buf_hdr_t
*l2hdr
)
4923 ASSERT(l2hdr
->b_compress
== ZIO_COMPRESS_OFF
);
4924 ASSERT(l2hdr
->b_tmp_cdata
!= NULL
);
4926 len
= l2hdr
->b_asize
;
4927 cdata
= zio_data_buf_alloc(len
);
4928 csize
= zio_compress_data(ZIO_COMPRESS_LZ4
, l2hdr
->b_tmp_cdata
,
4929 cdata
, l2hdr
->b_asize
);
4932 /* zero block, indicate that there's nothing to write */
4933 zio_data_buf_free(cdata
, len
);
4934 l2hdr
->b_compress
= ZIO_COMPRESS_EMPTY
;
4936 l2hdr
->b_tmp_cdata
= NULL
;
4937 ARCSTAT_BUMP(arcstat_l2_compress_zeros
);
4939 } else if (csize
> 0 && csize
< len
) {
4941 * Compression succeeded, we'll keep the cdata around for
4942 * writing and release it afterwards.
4944 l2hdr
->b_compress
= ZIO_COMPRESS_LZ4
;
4945 l2hdr
->b_asize
= csize
;
4946 l2hdr
->b_tmp_cdata
= cdata
;
4947 ARCSTAT_BUMP(arcstat_l2_compress_successes
);
4951 * Compression failed, release the compressed buffer.
4952 * l2hdr will be left unmodified.
4954 zio_data_buf_free(cdata
, len
);
4955 ARCSTAT_BUMP(arcstat_l2_compress_failures
);
4961 * Decompresses a zio read back from an l2arc device. On success, the
4962 * underlying zio's io_data buffer is overwritten by the uncompressed
4963 * version. On decompression error (corrupt compressed stream), the
4964 * zio->io_error value is set to signal an I/O error.
4966 * Please note that the compressed data stream is not checksummed, so
4967 * if the underlying device is experiencing data corruption, we may feed
4968 * corrupt data to the decompressor, so the decompressor needs to be
4969 * able to handle this situation (LZ4 does).
4972 l2arc_decompress_zio(zio_t
*zio
, arc_buf_hdr_t
*hdr
, enum zio_compress c
)
4977 ASSERT(L2ARC_IS_VALID_COMPRESS(c
));
4979 if (zio
->io_error
!= 0) {
4981 * An io error has occured, just restore the original io
4982 * size in preparation for a main pool read.
4984 zio
->io_orig_size
= zio
->io_size
= hdr
->b_size
;
4988 if (c
== ZIO_COMPRESS_EMPTY
) {
4990 * An empty buffer results in a null zio, which means we
4991 * need to fill its io_data after we're done restoring the
4992 * buffer's contents.
4994 ASSERT(hdr
->b_buf
!= NULL
);
4995 bzero(hdr
->b_buf
->b_data
, hdr
->b_size
);
4996 zio
->io_data
= zio
->io_orig_data
= hdr
->b_buf
->b_data
;
4998 ASSERT(zio
->io_data
!= NULL
);
5000 * We copy the compressed data from the start of the arc buffer
5001 * (the zio_read will have pulled in only what we need, the
5002 * rest is garbage which we will overwrite at decompression)
5003 * and then decompress back to the ARC data buffer. This way we
5004 * can minimize copying by simply decompressing back over the
5005 * original compressed data (rather than decompressing to an
5006 * aux buffer and then copying back the uncompressed buffer,
5007 * which is likely to be much larger).
5009 csize
= zio
->io_size
;
5010 cdata
= zio_data_buf_alloc(csize
);
5011 bcopy(zio
->io_data
, cdata
, csize
);
5012 if (zio_decompress_data(c
, cdata
, zio
->io_data
, csize
,
5014 zio
->io_error
= EIO
;
5015 zio_data_buf_free(cdata
, csize
);
5018 /* Restore the expected uncompressed IO size. */
5019 zio
->io_orig_size
= zio
->io_size
= hdr
->b_size
;
5023 * Releases the temporary b_tmp_cdata buffer in an l2arc header structure.
5024 * This buffer serves as a temporary holder of compressed data while
5025 * the buffer entry is being written to an l2arc device. Once that is
5026 * done, we can dispose of it.
5029 l2arc_release_cdata_buf(arc_buf_hdr_t
*ab
)
5031 l2arc_buf_hdr_t
*l2hdr
= ab
->b_l2hdr
;
5033 if (l2hdr
->b_compress
== ZIO_COMPRESS_LZ4
) {
5035 * If the data was compressed, then we've allocated a
5036 * temporary buffer for it, so now we need to release it.
5038 ASSERT(l2hdr
->b_tmp_cdata
!= NULL
);
5039 zio_data_buf_free(l2hdr
->b_tmp_cdata
, ab
->b_size
);
5041 l2hdr
->b_tmp_cdata
= NULL
;
5045 * This thread feeds the L2ARC at regular intervals. This is the beating
5046 * heart of the L2ARC.
5049 l2arc_feed_thread(void)
5054 uint64_t size
, wrote
;
5055 clock_t begin
, next
= ddi_get_lbolt();
5056 boolean_t headroom_boost
= B_FALSE
;
5058 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
5060 mutex_enter(&l2arc_feed_thr_lock
);
5062 while (l2arc_thread_exit
== 0) {
5063 CALLB_CPR_SAFE_BEGIN(&cpr
);
5064 (void) cv_timedwait_interruptible(&l2arc_feed_thr_cv
,
5065 &l2arc_feed_thr_lock
, next
);
5066 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
5067 next
= ddi_get_lbolt() + hz
;
5070 * Quick check for L2ARC devices.
5072 mutex_enter(&l2arc_dev_mtx
);
5073 if (l2arc_ndev
== 0) {
5074 mutex_exit(&l2arc_dev_mtx
);
5077 mutex_exit(&l2arc_dev_mtx
);
5078 begin
= ddi_get_lbolt();
5081 * This selects the next l2arc device to write to, and in
5082 * doing so the next spa to feed from: dev->l2ad_spa. This
5083 * will return NULL if there are now no l2arc devices or if
5084 * they are all faulted.
5086 * If a device is returned, its spa's config lock is also
5087 * held to prevent device removal. l2arc_dev_get_next()
5088 * will grab and release l2arc_dev_mtx.
5090 if ((dev
= l2arc_dev_get_next()) == NULL
)
5093 spa
= dev
->l2ad_spa
;
5094 ASSERT(spa
!= NULL
);
5097 * If the pool is read-only then force the feed thread to
5098 * sleep a little longer.
5100 if (!spa_writeable(spa
)) {
5101 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
5102 spa_config_exit(spa
, SCL_L2ARC
, dev
);
5107 * Avoid contributing to memory pressure.
5110 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
5111 spa_config_exit(spa
, SCL_L2ARC
, dev
);
5115 ARCSTAT_BUMP(arcstat_l2_feeds
);
5117 size
= l2arc_write_size();
5120 * Evict L2ARC buffers that will be overwritten.
5122 l2arc_evict(dev
, size
, B_FALSE
);
5125 * Write ARC buffers.
5127 wrote
= l2arc_write_buffers(spa
, dev
, size
, &headroom_boost
);
5130 * Calculate interval between writes.
5132 next
= l2arc_write_interval(begin
, size
, wrote
);
5133 spa_config_exit(spa
, SCL_L2ARC
, dev
);
5136 l2arc_thread_exit
= 0;
5137 cv_broadcast(&l2arc_feed_thr_cv
);
5138 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
5143 l2arc_vdev_present(vdev_t
*vd
)
5147 mutex_enter(&l2arc_dev_mtx
);
5148 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
5149 dev
= list_next(l2arc_dev_list
, dev
)) {
5150 if (dev
->l2ad_vdev
== vd
)
5153 mutex_exit(&l2arc_dev_mtx
);
5155 return (dev
!= NULL
);
5159 * Add a vdev for use by the L2ARC. By this point the spa has already
5160 * validated the vdev and opened it.
5163 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
5165 l2arc_dev_t
*adddev
;
5167 ASSERT(!l2arc_vdev_present(vd
));
5170 * Create a new l2arc device entry.
5172 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
5173 adddev
->l2ad_spa
= spa
;
5174 adddev
->l2ad_vdev
= vd
;
5175 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
5176 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
5177 adddev
->l2ad_hand
= adddev
->l2ad_start
;
5178 adddev
->l2ad_evict
= adddev
->l2ad_start
;
5179 adddev
->l2ad_first
= B_TRUE
;
5180 adddev
->l2ad_writing
= B_FALSE
;
5181 list_link_init(&adddev
->l2ad_node
);
5184 * This is a list of all ARC buffers that are still valid on the
5187 adddev
->l2ad_buflist
= kmem_zalloc(sizeof (list_t
), KM_SLEEP
);
5188 list_create(adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
5189 offsetof(arc_buf_hdr_t
, b_l2node
));
5191 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
5194 * Add device to global list
5196 mutex_enter(&l2arc_dev_mtx
);
5197 list_insert_head(l2arc_dev_list
, adddev
);
5198 atomic_inc_64(&l2arc_ndev
);
5199 mutex_exit(&l2arc_dev_mtx
);
5203 * Remove a vdev from the L2ARC.
5206 l2arc_remove_vdev(vdev_t
*vd
)
5208 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
5211 * Find the device by vdev
5213 mutex_enter(&l2arc_dev_mtx
);
5214 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
5215 nextdev
= list_next(l2arc_dev_list
, dev
);
5216 if (vd
== dev
->l2ad_vdev
) {
5221 ASSERT(remdev
!= NULL
);
5224 * Remove device from global list
5226 list_remove(l2arc_dev_list
, remdev
);
5227 l2arc_dev_last
= NULL
; /* may have been invalidated */
5228 atomic_dec_64(&l2arc_ndev
);
5229 mutex_exit(&l2arc_dev_mtx
);
5232 * Clear all buflists and ARC references. L2ARC device flush.
5234 l2arc_evict(remdev
, 0, B_TRUE
);
5235 list_destroy(remdev
->l2ad_buflist
);
5236 kmem_free(remdev
->l2ad_buflist
, sizeof (list_t
));
5237 kmem_free(remdev
, sizeof (l2arc_dev_t
));
5243 l2arc_thread_exit
= 0;
5245 l2arc_writes_sent
= 0;
5246 l2arc_writes_done
= 0;
5248 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
5249 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
5250 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
5251 mutex_init(&l2arc_buflist_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
5252 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
5254 l2arc_dev_list
= &L2ARC_dev_list
;
5255 l2arc_free_on_write
= &L2ARC_free_on_write
;
5256 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
5257 offsetof(l2arc_dev_t
, l2ad_node
));
5258 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
5259 offsetof(l2arc_data_free_t
, l2df_list_node
));
5266 * This is called from dmu_fini(), which is called from spa_fini();
5267 * Because of this, we can assume that all l2arc devices have
5268 * already been removed when the pools themselves were removed.
5271 l2arc_do_free_on_write();
5273 mutex_destroy(&l2arc_feed_thr_lock
);
5274 cv_destroy(&l2arc_feed_thr_cv
);
5275 mutex_destroy(&l2arc_dev_mtx
);
5276 mutex_destroy(&l2arc_buflist_mtx
);
5277 mutex_destroy(&l2arc_free_on_write_mtx
);
5279 list_destroy(l2arc_dev_list
);
5280 list_destroy(l2arc_free_on_write
);
5286 if (!(spa_mode_global
& FWRITE
))
5289 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
5290 TS_RUN
, minclsyspri
);
5296 if (!(spa_mode_global
& FWRITE
))
5299 mutex_enter(&l2arc_feed_thr_lock
);
5300 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
5301 l2arc_thread_exit
= 1;
5302 while (l2arc_thread_exit
!= 0)
5303 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
5304 mutex_exit(&l2arc_feed_thr_lock
);
5307 #if defined(_KERNEL) && defined(HAVE_SPL)
5308 EXPORT_SYMBOL(arc_read
);
5309 EXPORT_SYMBOL(arc_buf_remove_ref
);
5310 EXPORT_SYMBOL(arc_getbuf_func
);
5311 EXPORT_SYMBOL(arc_add_prune_callback
);
5312 EXPORT_SYMBOL(arc_remove_prune_callback
);
5314 module_param(zfs_arc_min
, ulong
, 0644);
5315 MODULE_PARM_DESC(zfs_arc_min
, "Min arc size");
5317 module_param(zfs_arc_max
, ulong
, 0644);
5318 MODULE_PARM_DESC(zfs_arc_max
, "Max arc size");
5320 module_param(zfs_arc_meta_limit
, ulong
, 0644);
5321 MODULE_PARM_DESC(zfs_arc_meta_limit
, "Meta limit for arc size");
5323 module_param(zfs_arc_meta_prune
, int, 0644);
5324 MODULE_PARM_DESC(zfs_arc_meta_prune
, "Bytes of meta data to prune");
5326 module_param(zfs_arc_grow_retry
, int, 0644);
5327 MODULE_PARM_DESC(zfs_arc_grow_retry
, "Seconds before growing arc size");
5329 module_param(zfs_arc_shrink_shift
, int, 0644);
5330 MODULE_PARM_DESC(zfs_arc_shrink_shift
, "log2(fraction of arc to reclaim)");
5332 module_param(zfs_arc_p_min_shift
, int, 0644);
5333 MODULE_PARM_DESC(zfs_arc_p_min_shift
, "arc_c shift to calc min/max arc_p");
5335 module_param(zfs_disable_dup_eviction
, int, 0644);
5336 MODULE_PARM_DESC(zfs_disable_dup_eviction
, "disable duplicate buffer eviction");
5338 module_param(zfs_arc_memory_throttle_disable
, int, 0644);
5339 MODULE_PARM_DESC(zfs_arc_memory_throttle_disable
, "disable memory throttle");
5341 module_param(zfs_arc_min_prefetch_lifespan
, int, 0644);
5342 MODULE_PARM_DESC(zfs_arc_min_prefetch_lifespan
, "Min life of prefetch block");
5344 module_param(l2arc_write_max
, ulong
, 0644);
5345 MODULE_PARM_DESC(l2arc_write_max
, "Max write bytes per interval");
5347 module_param(l2arc_write_boost
, ulong
, 0644);
5348 MODULE_PARM_DESC(l2arc_write_boost
, "Extra write bytes during device warmup");
5350 module_param(l2arc_headroom
, ulong
, 0644);
5351 MODULE_PARM_DESC(l2arc_headroom
, "Number of max device writes to precache");
5353 module_param(l2arc_headroom_boost
, ulong
, 0644);
5354 MODULE_PARM_DESC(l2arc_headroom_boost
, "Compressed l2arc_headroom multiplier");
5356 module_param(l2arc_feed_secs
, ulong
, 0644);
5357 MODULE_PARM_DESC(l2arc_feed_secs
, "Seconds between L2ARC writing");
5359 module_param(l2arc_feed_min_ms
, ulong
, 0644);
5360 MODULE_PARM_DESC(l2arc_feed_min_ms
, "Min feed interval in milliseconds");
5362 module_param(l2arc_noprefetch
, int, 0644);
5363 MODULE_PARM_DESC(l2arc_noprefetch
, "Skip caching prefetched buffers");
5365 module_param(l2arc_nocompress
, int, 0644);
5366 MODULE_PARM_DESC(l2arc_nocompress
, "Skip compressing L2ARC buffers");
5368 module_param(l2arc_feed_again
, int, 0644);
5369 MODULE_PARM_DESC(l2arc_feed_again
, "Turbo L2ARC warmup");
5371 module_param(l2arc_norw
, int, 0644);
5372 MODULE_PARM_DESC(l2arc_norw
, "No reads during writes");