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.
28 * DVA-based Adjustable Replacement Cache
30 * While much of the theory of operation used here is
31 * based on the self-tuning, low overhead replacement cache
32 * presented by Megiddo and Modha at FAST 2003, there are some
33 * significant differences:
35 * 1. The Megiddo and Modha model assumes any page is evictable.
36 * Pages in its cache cannot be "locked" into memory. This makes
37 * the eviction algorithm simple: evict the last page in the list.
38 * This also make the performance characteristics easy to reason
39 * about. Our cache is not so simple. At any given moment, some
40 * subset of the blocks in the cache are un-evictable because we
41 * have handed out a reference to them. Blocks are only evictable
42 * when there are no external references active. This makes
43 * eviction far more problematic: we choose to evict the evictable
44 * blocks that are the "lowest" in the list.
46 * There are times when it is not possible to evict the requested
47 * space. In these circumstances we are unable to adjust the cache
48 * size. To prevent the cache growing unbounded at these times we
49 * implement a "cache throttle" that slows the flow of new data
50 * into the cache until we can make space available.
52 * 2. The Megiddo and Modha model assumes a fixed cache size.
53 * Pages are evicted when the cache is full and there is a cache
54 * miss. Our model has a variable sized cache. It grows with
55 * high use, but also tries to react to memory pressure from the
56 * operating system: decreasing its size when system memory is
59 * 3. The Megiddo and Modha model assumes a fixed page size. All
60 * elements of the cache are therefor exactly the same size. So
61 * when adjusting the cache size following a cache miss, its simply
62 * a matter of choosing a single page to evict. In our model, we
63 * have variable sized cache blocks (rangeing from 512 bytes to
64 * 128K bytes). We therefor choose a set of blocks to evict to make
65 * space for a cache miss that approximates as closely as possible
66 * the space used by the new block.
68 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
69 * by N. Megiddo & D. Modha, FAST 2003
75 * A new reference to a cache buffer can be obtained in two
76 * ways: 1) via a hash table lookup using the DVA as a key,
77 * or 2) via one of the ARC lists. The arc_read() interface
78 * uses method 1, while the internal arc algorithms for
79 * adjusting the cache use method 2. We therefor provide two
80 * types of locks: 1) the hash table lock array, and 2) the
83 * Buffers do not have their own mutexes, rather they rely on the
84 * hash table mutexes for the bulk of their protection (i.e. most
85 * fields in the arc_buf_hdr_t are protected by these mutexes).
87 * buf_hash_find() returns the appropriate mutex (held) when it
88 * locates the requested buffer in the hash table. It returns
89 * NULL for the mutex if the buffer was not in the table.
91 * buf_hash_remove() expects the appropriate hash mutex to be
92 * already held before it is invoked.
94 * Each arc state also has a mutex which is used to protect the
95 * buffer list associated with the state. When attempting to
96 * obtain a hash table lock while holding an arc list lock you
97 * must use: mutex_tryenter() to avoid deadlock. Also note that
98 * the active state mutex must be held before the ghost state mutex.
100 * Arc buffers may have an associated eviction callback function.
101 * This function will be invoked prior to removing the buffer (e.g.
102 * in arc_do_user_evicts()). Note however that the data associated
103 * with the buffer may be evicted prior to the callback. The callback
104 * must be made with *no locks held* (to prevent deadlock). Additionally,
105 * the users of callbacks must ensure that their private data is
106 * protected from simultaneous callbacks from arc_buf_evict()
107 * and arc_do_user_evicts().
109 * It as also possible to register a callback which is run when the
110 * arc_meta_limit is reached and no buffers can be safely evicted. In
111 * this case the arc user should drop a reference on some arc buffers so
112 * they can be reclaimed and the arc_meta_limit honored. For example,
113 * when using the ZPL each dentry holds a references on a znode. These
114 * dentries must be pruned before the arc buffer holding the znode can
117 * Note that the majority of the performance stats are manipulated
118 * with atomic operations.
120 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
122 * - L2ARC buflist creation
123 * - L2ARC buflist eviction
124 * - L2ARC write completion, which walks L2ARC buflists
125 * - ARC header destruction, as it removes from L2ARC buflists
126 * - ARC header release, as it removes from L2ARC buflists
131 #include <sys/zfs_context.h>
133 #include <sys/vdev.h>
134 #include <sys/vdev_impl.h>
136 #include <sys/vmsystm.h>
138 #include <sys/fs/swapnode.h>
141 #include <sys/callb.h>
142 #include <sys/kstat.h>
143 #include <sys/dmu_tx.h>
144 #include <zfs_fletcher.h>
146 static kmutex_t arc_reclaim_thr_lock;
147 static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */
148 static uint8_t arc_thread_exit;
150 /* number of bytes to prune from caches when at arc_meta_limit is reached */
151 uint_t arc_meta_prune = 1048576;
153 typedef enum arc_reclaim_strategy {
154 ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */
155 ARC_RECLAIM_CONS /* Conservative reclaim strategy */
156 } arc_reclaim_strategy_t;
158 /* number of seconds before growing cache again */
159 static int arc_grow_retry = 5;
161 /* expiration time for arc_no_grow */
162 static clock_t arc_grow_time = 0;
164 /* shift of arc_c for calculating both min and max arc_p */
165 static int arc_p_min_shift = 4;
167 /* log2(fraction of arc to reclaim) */
168 static int arc_shrink_shift = 5;
171 * minimum lifespan of a prefetch block in clock ticks
172 * (initialized in arc_init())
174 static int arc_min_prefetch_lifespan;
179 * The arc has filled available memory and has now warmed up.
181 static boolean_t arc_warm;
184 * These tunables are for performance analysis.
186 unsigned long zfs_arc_max = 0;
187 unsigned long zfs_arc_min = 0;
188 unsigned long zfs_arc_meta_limit = 0;
189 int zfs_arc_grow_retry = 0;
190 int zfs_arc_shrink_shift = 0;
191 int zfs_arc_p_min_shift = 0;
192 int zfs_arc_memory_throttle_disable = 1;
193 int zfs_disable_dup_eviction = 0;
194 int zfs_arc_meta_prune = 0;
197 * Note that buffers can be in one of 6 states:
198 * ARC_anon - anonymous (discussed below)
199 * ARC_mru - recently used, currently cached
200 * ARC_mru_ghost - recentely used, no longer in cache
201 * ARC_mfu - frequently used, currently cached
202 * ARC_mfu_ghost - frequently used, no longer in cache
203 * ARC_l2c_only - exists in L2ARC but not other states
204 * When there are no active references to the buffer, they are
205 * are linked onto a list in one of these arc states. These are
206 * the only buffers that can be evicted or deleted. Within each
207 * state there are multiple lists, one for meta-data and one for
208 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
209 * etc.) is tracked separately so that it can be managed more
210 * explicitly: favored over data, limited explicitly.
212 * Anonymous buffers are buffers that are not associated with
213 * a DVA. These are buffers that hold dirty block copies
214 * before they are written to stable storage. By definition,
215 * they are "ref'd" and are considered part of arc_mru
216 * that cannot be freed. Generally, they will aquire a DVA
217 * as they are written and migrate onto the arc_mru list.
219 * The ARC_l2c_only state is for buffers that are in the second
220 * level ARC but no longer in any of the ARC_m* lists. The second
221 * level ARC itself may also contain buffers that are in any of
222 * the ARC_m* states - meaning that a buffer can exist in two
223 * places. The reason for the ARC_l2c_only state is to keep the
224 * buffer header in the hash table, so that reads that hit the
225 * second level ARC benefit from these fast lookups.
228 typedef struct arc_state {
229 list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */
230 uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */
231 uint64_t arcs_size; /* total amount of data in this state */
236 static arc_state_t ARC_anon;
237 static arc_state_t ARC_mru;
238 static arc_state_t ARC_mru_ghost;
239 static arc_state_t ARC_mfu;
240 static arc_state_t ARC_mfu_ghost;
241 static arc_state_t ARC_l2c_only;
243 typedef struct arc_stats {
244 kstat_named_t arcstat_hits;
245 kstat_named_t arcstat_misses;
246 kstat_named_t arcstat_demand_data_hits;
247 kstat_named_t arcstat_demand_data_misses;
248 kstat_named_t arcstat_demand_metadata_hits;
249 kstat_named_t arcstat_demand_metadata_misses;
250 kstat_named_t arcstat_prefetch_data_hits;
251 kstat_named_t arcstat_prefetch_data_misses;
252 kstat_named_t arcstat_prefetch_metadata_hits;
253 kstat_named_t arcstat_prefetch_metadata_misses;
254 kstat_named_t arcstat_mru_hits;
255 kstat_named_t arcstat_mru_ghost_hits;
256 kstat_named_t arcstat_mfu_hits;
257 kstat_named_t arcstat_mfu_ghost_hits;
258 kstat_named_t arcstat_deleted;
259 kstat_named_t arcstat_recycle_miss;
260 kstat_named_t arcstat_mutex_miss;
261 kstat_named_t arcstat_evict_skip;
262 kstat_named_t arcstat_evict_l2_cached;
263 kstat_named_t arcstat_evict_l2_eligible;
264 kstat_named_t arcstat_evict_l2_ineligible;
265 kstat_named_t arcstat_hash_elements;
266 kstat_named_t arcstat_hash_elements_max;
267 kstat_named_t arcstat_hash_collisions;
268 kstat_named_t arcstat_hash_chains;
269 kstat_named_t arcstat_hash_chain_max;
270 kstat_named_t arcstat_p;
271 kstat_named_t arcstat_c;
272 kstat_named_t arcstat_c_min;
273 kstat_named_t arcstat_c_max;
274 kstat_named_t arcstat_size;
275 kstat_named_t arcstat_hdr_size;
276 kstat_named_t arcstat_data_size;
277 kstat_named_t arcstat_other_size;
278 kstat_named_t arcstat_anon_size;
279 kstat_named_t arcstat_anon_evict_data;
280 kstat_named_t arcstat_anon_evict_metadata;
281 kstat_named_t arcstat_mru_size;
282 kstat_named_t arcstat_mru_evict_data;
283 kstat_named_t arcstat_mru_evict_metadata;
284 kstat_named_t arcstat_mru_ghost_size;
285 kstat_named_t arcstat_mru_ghost_evict_data;
286 kstat_named_t arcstat_mru_ghost_evict_metadata;
287 kstat_named_t arcstat_mfu_size;
288 kstat_named_t arcstat_mfu_evict_data;
289 kstat_named_t arcstat_mfu_evict_metadata;
290 kstat_named_t arcstat_mfu_ghost_size;
291 kstat_named_t arcstat_mfu_ghost_evict_data;
292 kstat_named_t arcstat_mfu_ghost_evict_metadata;
293 kstat_named_t arcstat_l2_hits;
294 kstat_named_t arcstat_l2_misses;
295 kstat_named_t arcstat_l2_feeds;
296 kstat_named_t arcstat_l2_rw_clash;
297 kstat_named_t arcstat_l2_read_bytes;
298 kstat_named_t arcstat_l2_write_bytes;
299 kstat_named_t arcstat_l2_writes_sent;
300 kstat_named_t arcstat_l2_writes_done;
301 kstat_named_t arcstat_l2_writes_error;
302 kstat_named_t arcstat_l2_writes_hdr_miss;
303 kstat_named_t arcstat_l2_evict_lock_retry;
304 kstat_named_t arcstat_l2_evict_reading;
305 kstat_named_t arcstat_l2_free_on_write;
306 kstat_named_t arcstat_l2_abort_lowmem;
307 kstat_named_t arcstat_l2_cksum_bad;
308 kstat_named_t arcstat_l2_io_error;
309 kstat_named_t arcstat_l2_size;
310 kstat_named_t arcstat_l2_hdr_size;
311 kstat_named_t arcstat_memory_throttle_count;
312 kstat_named_t arcstat_duplicate_buffers;
313 kstat_named_t arcstat_duplicate_buffers_size;
314 kstat_named_t arcstat_duplicate_reads;
315 kstat_named_t arcstat_memory_direct_count;
316 kstat_named_t arcstat_memory_indirect_count;
317 kstat_named_t arcstat_no_grow;
318 kstat_named_t arcstat_tempreserve;
319 kstat_named_t arcstat_loaned_bytes;
320 kstat_named_t arcstat_prune;
321 kstat_named_t arcstat_meta_used;
322 kstat_named_t arcstat_meta_limit;
323 kstat_named_t arcstat_meta_max;
326 static arc_stats_t arc_stats = {
327 { "hits", KSTAT_DATA_UINT64 },
328 { "misses", KSTAT_DATA_UINT64 },
329 { "demand_data_hits", KSTAT_DATA_UINT64 },
330 { "demand_data_misses", KSTAT_DATA_UINT64 },
331 { "demand_metadata_hits", KSTAT_DATA_UINT64 },
332 { "demand_metadata_misses", KSTAT_DATA_UINT64 },
333 { "prefetch_data_hits", KSTAT_DATA_UINT64 },
334 { "prefetch_data_misses", KSTAT_DATA_UINT64 },
335 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
336 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
337 { "mru_hits", KSTAT_DATA_UINT64 },
338 { "mru_ghost_hits", KSTAT_DATA_UINT64 },
339 { "mfu_hits", KSTAT_DATA_UINT64 },
340 { "mfu_ghost_hits", KSTAT_DATA_UINT64 },
341 { "deleted", KSTAT_DATA_UINT64 },
342 { "recycle_miss", KSTAT_DATA_UINT64 },
343 { "mutex_miss", KSTAT_DATA_UINT64 },
344 { "evict_skip", KSTAT_DATA_UINT64 },
345 { "evict_l2_cached", KSTAT_DATA_UINT64 },
346 { "evict_l2_eligible", KSTAT_DATA_UINT64 },
347 { "evict_l2_ineligible", KSTAT_DATA_UINT64 },
348 { "hash_elements", KSTAT_DATA_UINT64 },
349 { "hash_elements_max", KSTAT_DATA_UINT64 },
350 { "hash_collisions", KSTAT_DATA_UINT64 },
351 { "hash_chains", KSTAT_DATA_UINT64 },
352 { "hash_chain_max", KSTAT_DATA_UINT64 },
353 { "p", KSTAT_DATA_UINT64 },
354 { "c", KSTAT_DATA_UINT64 },
355 { "c_min", KSTAT_DATA_UINT64 },
356 { "c_max", KSTAT_DATA_UINT64 },
357 { "size", KSTAT_DATA_UINT64 },
358 { "hdr_size", KSTAT_DATA_UINT64 },
359 { "data_size", KSTAT_DATA_UINT64 },
360 { "other_size", KSTAT_DATA_UINT64 },
361 { "anon_size", KSTAT_DATA_UINT64 },
362 { "anon_evict_data", KSTAT_DATA_UINT64 },
363 { "anon_evict_metadata", KSTAT_DATA_UINT64 },
364 { "mru_size", KSTAT_DATA_UINT64 },
365 { "mru_evict_data", KSTAT_DATA_UINT64 },
366 { "mru_evict_metadata", KSTAT_DATA_UINT64 },
367 { "mru_ghost_size", KSTAT_DATA_UINT64 },
368 { "mru_ghost_evict_data", KSTAT_DATA_UINT64 },
369 { "mru_ghost_evict_metadata", KSTAT_DATA_UINT64 },
370 { "mfu_size", KSTAT_DATA_UINT64 },
371 { "mfu_evict_data", KSTAT_DATA_UINT64 },
372 { "mfu_evict_metadata", KSTAT_DATA_UINT64 },
373 { "mfu_ghost_size", KSTAT_DATA_UINT64 },
374 { "mfu_ghost_evict_data", KSTAT_DATA_UINT64 },
375 { "mfu_ghost_evict_metadata", KSTAT_DATA_UINT64 },
376 { "l2_hits", KSTAT_DATA_UINT64 },
377 { "l2_misses", KSTAT_DATA_UINT64 },
378 { "l2_feeds", KSTAT_DATA_UINT64 },
379 { "l2_rw_clash", KSTAT_DATA_UINT64 },
380 { "l2_read_bytes", KSTAT_DATA_UINT64 },
381 { "l2_write_bytes", KSTAT_DATA_UINT64 },
382 { "l2_writes_sent", KSTAT_DATA_UINT64 },
383 { "l2_writes_done", KSTAT_DATA_UINT64 },
384 { "l2_writes_error", KSTAT_DATA_UINT64 },
385 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 },
386 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
387 { "l2_evict_reading", KSTAT_DATA_UINT64 },
388 { "l2_free_on_write", KSTAT_DATA_UINT64 },
389 { "l2_abort_lowmem", KSTAT_DATA_UINT64 },
390 { "l2_cksum_bad", KSTAT_DATA_UINT64 },
391 { "l2_io_error", KSTAT_DATA_UINT64 },
392 { "l2_size", KSTAT_DATA_UINT64 },
393 { "l2_hdr_size", KSTAT_DATA_UINT64 },
394 { "memory_throttle_count", KSTAT_DATA_UINT64 },
395 { "duplicate_buffers", KSTAT_DATA_UINT64 },
396 { "duplicate_buffers_size", KSTAT_DATA_UINT64 },
397 { "duplicate_reads", KSTAT_DATA_UINT64 },
398 { "memory_direct_count", KSTAT_DATA_UINT64 },
399 { "memory_indirect_count", KSTAT_DATA_UINT64 },
400 { "arc_no_grow", KSTAT_DATA_UINT64 },
401 { "arc_tempreserve", KSTAT_DATA_UINT64 },
402 { "arc_loaned_bytes", KSTAT_DATA_UINT64 },
403 { "arc_prune", KSTAT_DATA_UINT64 },
404 { "arc_meta_used", KSTAT_DATA_UINT64 },
405 { "arc_meta_limit", KSTAT_DATA_UINT64 },
406 { "arc_meta_max", KSTAT_DATA_UINT64 },
409 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
411 #define ARCSTAT_INCR(stat, val) \
412 atomic_add_64(&arc_stats.stat.value.ui64, (val));
414 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
415 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
417 #define ARCSTAT_MAX(stat, val) { \
419 while ((val) > (m = arc_stats.stat.value.ui64) && \
420 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
424 #define ARCSTAT_MAXSTAT(stat) \
425 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
428 * We define a macro to allow ARC hits/misses to be easily broken down by
429 * two separate conditions, giving a total of four different subtypes for
430 * each of hits and misses (so eight statistics total).
432 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
435 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
437 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
441 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
443 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
448 static arc_state_t *arc_anon;
449 static arc_state_t *arc_mru;
450 static arc_state_t *arc_mru_ghost;
451 static arc_state_t *arc_mfu;
452 static arc_state_t *arc_mfu_ghost;
453 static arc_state_t *arc_l2c_only;
456 * There are several ARC variables that are critical to export as kstats --
457 * but we don't want to have to grovel around in the kstat whenever we wish to
458 * manipulate them. For these variables, we therefore define them to be in
459 * terms of the statistic variable. This assures that we are not introducing
460 * the possibility of inconsistency by having shadow copies of the variables,
461 * while still allowing the code to be readable.
463 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
464 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
465 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
466 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
467 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
468 #define arc_no_grow ARCSTAT(arcstat_no_grow)
469 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
470 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
471 #define arc_meta_used ARCSTAT(arcstat_meta_used)
472 #define arc_meta_limit ARCSTAT(arcstat_meta_limit)
473 #define arc_meta_max ARCSTAT(arcstat_meta_max)
475 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
477 typedef struct arc_callback arc_callback_t;
479 struct arc_callback {
481 arc_done_func_t *acb_done;
483 zio_t *acb_zio_dummy;
484 arc_callback_t *acb_next;
487 typedef struct arc_write_callback arc_write_callback_t;
489 struct arc_write_callback {
491 arc_done_func_t *awcb_ready;
492 arc_done_func_t *awcb_done;
497 /* protected by hash lock */
502 kmutex_t b_freeze_lock;
503 zio_cksum_t *b_freeze_cksum;
506 arc_buf_hdr_t *b_hash_next;
511 arc_callback_t *b_acb;
515 arc_buf_contents_t b_type;
519 /* protected by arc state mutex */
520 arc_state_t *b_state;
521 list_node_t b_arc_node;
523 /* updated atomically */
524 clock_t b_arc_access;
526 /* self protecting */
529 l2arc_buf_hdr_t *b_l2hdr;
530 list_node_t b_l2node;
533 static list_t arc_prune_list;
534 static kmutex_t arc_prune_mtx;
535 static arc_buf_t *arc_eviction_list;
536 static kmutex_t arc_eviction_mtx;
537 static arc_buf_hdr_t arc_eviction_hdr;
538 static void arc_get_data_buf(arc_buf_t *buf);
539 static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
540 static int arc_evict_needed(arc_buf_contents_t type);
541 static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
543 static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
545 #define GHOST_STATE(state) \
546 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
547 (state) == arc_l2c_only)
550 * Private ARC flags. These flags are private ARC only flags that will show up
551 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
552 * be passed in as arc_flags in things like arc_read. However, these flags
553 * should never be passed and should only be set by ARC code. When adding new
554 * public flags, make sure not to smash the private ones.
557 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
558 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
559 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
560 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
561 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
562 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
563 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
564 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
565 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
566 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
568 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
569 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
570 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
571 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
572 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
573 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
574 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
575 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
576 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
577 (hdr)->b_l2hdr != NULL)
578 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
579 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
580 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
586 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
587 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
590 * Hash table routines
593 #define HT_LOCK_ALIGN 64
594 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
599 unsigned char pad[HT_LOCK_PAD];
603 #define BUF_LOCKS 256
604 typedef struct buf_hash_table {
606 arc_buf_hdr_t **ht_table;
607 struct ht_lock ht_locks[BUF_LOCKS];
610 static buf_hash_table_t buf_hash_table;
612 #define BUF_HASH_INDEX(spa, dva, birth) \
613 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
614 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
615 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
616 #define HDR_LOCK(hdr) \
617 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
619 uint64_t zfs_crc64_table[256];
625 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
626 #define L2ARC_HEADROOM 2 /* num of writes */
627 #define L2ARC_FEED_SECS 1 /* caching interval secs */
628 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
630 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
631 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
634 * L2ARC Performance Tunables
636 unsigned long l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */
637 unsigned long l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */
638 unsigned long l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */
639 unsigned long l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */
640 unsigned long l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */
641 int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */
642 int l2arc_feed_again = B_TRUE; /* turbo warmup */
643 int l2arc_norw = B_TRUE; /* no reads during writes */
648 typedef struct l2arc_dev {
649 vdev_t *l2ad_vdev; /* vdev */
650 spa_t *l2ad_spa; /* spa */
651 uint64_t l2ad_hand; /* next write location */
652 uint64_t l2ad_write; /* desired write size, bytes */
653 uint64_t l2ad_boost; /* warmup write boost, bytes */
654 uint64_t l2ad_start; /* first addr on device */
655 uint64_t l2ad_end; /* last addr on device */
656 uint64_t l2ad_evict; /* last addr eviction reached */
657 boolean_t l2ad_first; /* first sweep through */
658 boolean_t l2ad_writing; /* currently writing */
659 list_t *l2ad_buflist; /* buffer list */
660 list_node_t l2ad_node; /* device list node */
663 static list_t L2ARC_dev_list; /* device list */
664 static list_t *l2arc_dev_list; /* device list pointer */
665 static kmutex_t l2arc_dev_mtx; /* device list mutex */
666 static l2arc_dev_t *l2arc_dev_last; /* last device used */
667 static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */
668 static list_t L2ARC_free_on_write; /* free after write buf list */
669 static list_t *l2arc_free_on_write; /* free after write list ptr */
670 static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */
671 static uint64_t l2arc_ndev; /* number of devices */
673 typedef struct l2arc_read_callback {
674 arc_buf_t *l2rcb_buf; /* read buffer */
675 spa_t *l2rcb_spa; /* spa */
676 blkptr_t l2rcb_bp; /* original blkptr */
677 zbookmark_t l2rcb_zb; /* original bookmark */
678 int l2rcb_flags; /* original flags */
679 } l2arc_read_callback_t;
681 typedef struct l2arc_write_callback {
682 l2arc_dev_t *l2wcb_dev; /* device info */
683 arc_buf_hdr_t *l2wcb_head; /* head of write buflist */
684 } l2arc_write_callback_t;
686 struct l2arc_buf_hdr {
687 /* protected by arc_buf_hdr mutex */
688 l2arc_dev_t *b_dev; /* L2ARC device */
689 uint64_t b_daddr; /* disk address, offset byte */
692 typedef struct l2arc_data_free {
693 /* protected by l2arc_free_on_write_mtx */
696 void (*l2df_func)(void *, size_t);
697 list_node_t l2df_list_node;
700 static kmutex_t l2arc_feed_thr_lock;
701 static kcondvar_t l2arc_feed_thr_cv;
702 static uint8_t l2arc_thread_exit;
704 static void l2arc_read_done(zio_t *zio);
705 static void l2arc_hdr_stat_add(void);
706 static void l2arc_hdr_stat_remove(void);
709 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
711 uint8_t *vdva = (uint8_t *)dva;
712 uint64_t crc = -1ULL;
715 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
717 for (i = 0; i < sizeof (dva_t); i++)
718 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
720 crc ^= (spa>>8) ^ birth;
725 #define BUF_EMPTY(buf) \
726 ((buf)->b_dva.dva_word[0] == 0 && \
727 (buf)->b_dva.dva_word[1] == 0 && \
730 #define BUF_EQUAL(spa, dva, birth, buf) \
731 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
732 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
733 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
736 buf_discard_identity(arc_buf_hdr_t *hdr)
738 hdr->b_dva.dva_word[0] = 0;
739 hdr->b_dva.dva_word[1] = 0;
744 static arc_buf_hdr_t *
745 buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp)
747 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
748 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
751 mutex_enter(hash_lock);
752 for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
753 buf = buf->b_hash_next) {
754 if (BUF_EQUAL(spa, dva, birth, buf)) {
759 mutex_exit(hash_lock);
765 * Insert an entry into the hash table. If there is already an element
766 * equal to elem in the hash table, then the already existing element
767 * will be returned and the new element will not be inserted.
768 * Otherwise returns NULL.
770 static arc_buf_hdr_t *
771 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
773 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
774 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
778 ASSERT(!HDR_IN_HASH_TABLE(buf));
780 mutex_enter(hash_lock);
781 for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
782 fbuf = fbuf->b_hash_next, i++) {
783 if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
787 buf->b_hash_next = buf_hash_table.ht_table[idx];
788 buf_hash_table.ht_table[idx] = buf;
789 buf->b_flags |= ARC_IN_HASH_TABLE;
791 /* collect some hash table performance data */
793 ARCSTAT_BUMP(arcstat_hash_collisions);
795 ARCSTAT_BUMP(arcstat_hash_chains);
797 ARCSTAT_MAX(arcstat_hash_chain_max, i);
800 ARCSTAT_BUMP(arcstat_hash_elements);
801 ARCSTAT_MAXSTAT(arcstat_hash_elements);
807 buf_hash_remove(arc_buf_hdr_t *buf)
809 arc_buf_hdr_t *fbuf, **bufp;
810 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
812 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
813 ASSERT(HDR_IN_HASH_TABLE(buf));
815 bufp = &buf_hash_table.ht_table[idx];
816 while ((fbuf = *bufp) != buf) {
817 ASSERT(fbuf != NULL);
818 bufp = &fbuf->b_hash_next;
820 *bufp = buf->b_hash_next;
821 buf->b_hash_next = NULL;
822 buf->b_flags &= ~ARC_IN_HASH_TABLE;
824 /* collect some hash table performance data */
825 ARCSTAT_BUMPDOWN(arcstat_hash_elements);
827 if (buf_hash_table.ht_table[idx] &&
828 buf_hash_table.ht_table[idx]->b_hash_next == NULL)
829 ARCSTAT_BUMPDOWN(arcstat_hash_chains);
833 * Global data structures and functions for the buf kmem cache.
835 static kmem_cache_t *hdr_cache;
836 static kmem_cache_t *buf_cache;
843 #if defined(_KERNEL) && defined(HAVE_SPL)
844 /* Large allocations which do not require contiguous pages
845 * should be using vmem_free() in the linux kernel */
846 vmem_free(buf_hash_table.ht_table,
847 (buf_hash_table.ht_mask + 1) * sizeof (void *));
849 kmem_free(buf_hash_table.ht_table,
850 (buf_hash_table.ht_mask + 1) * sizeof (void *));
852 for (i = 0; i < BUF_LOCKS; i++)
853 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
854 kmem_cache_destroy(hdr_cache);
855 kmem_cache_destroy(buf_cache);
859 * Constructor callback - called when the cache is empty
860 * and a new buf is requested.
864 hdr_cons(void *vbuf, void *unused, int kmflag)
866 arc_buf_hdr_t *buf = vbuf;
868 bzero(buf, sizeof (arc_buf_hdr_t));
869 refcount_create(&buf->b_refcnt);
870 cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
871 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
872 list_link_init(&buf->b_arc_node);
873 list_link_init(&buf->b_l2node);
874 arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
881 buf_cons(void *vbuf, void *unused, int kmflag)
883 arc_buf_t *buf = vbuf;
885 bzero(buf, sizeof (arc_buf_t));
886 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
887 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
893 * Destructor callback - called when a cached buf is
894 * no longer required.
898 hdr_dest(void *vbuf, void *unused)
900 arc_buf_hdr_t *buf = vbuf;
902 ASSERT(BUF_EMPTY(buf));
903 refcount_destroy(&buf->b_refcnt);
904 cv_destroy(&buf->b_cv);
905 mutex_destroy(&buf->b_freeze_lock);
906 arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
911 buf_dest(void *vbuf, void *unused)
913 arc_buf_t *buf = vbuf;
915 mutex_destroy(&buf->b_evict_lock);
916 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
923 uint64_t hsize = 1ULL << 12;
927 * The hash table is big enough to fill all of physical memory
928 * with an average 64K block size. The table will take up
929 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
931 while (hsize * 65536 < physmem * PAGESIZE)
934 buf_hash_table.ht_mask = hsize - 1;
935 #if defined(_KERNEL) && defined(HAVE_SPL)
936 /* Large allocations which do not require contiguous pages
937 * should be using vmem_alloc() in the linux kernel */
938 buf_hash_table.ht_table =
939 vmem_zalloc(hsize * sizeof (void*), KM_SLEEP);
941 buf_hash_table.ht_table =
942 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
944 if (buf_hash_table.ht_table == NULL) {
945 ASSERT(hsize > (1ULL << 8));
950 hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
951 0, hdr_cons, hdr_dest, NULL, NULL, NULL, 0);
952 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
953 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
955 for (i = 0; i < 256; i++)
956 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
957 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
959 for (i = 0; i < BUF_LOCKS; i++) {
960 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
961 NULL, MUTEX_DEFAULT, NULL);
965 #define ARC_MINTIME (hz>>4) /* 62 ms */
968 arc_cksum_verify(arc_buf_t *buf)
972 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
975 mutex_enter(&buf->b_hdr->b_freeze_lock);
976 if (buf->b_hdr->b_freeze_cksum == NULL ||
977 (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
978 mutex_exit(&buf->b_hdr->b_freeze_lock);
981 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
982 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
983 panic("buffer modified while frozen!");
984 mutex_exit(&buf->b_hdr->b_freeze_lock);
988 arc_cksum_equal(arc_buf_t *buf)
993 mutex_enter(&buf->b_hdr->b_freeze_lock);
994 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
995 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
996 mutex_exit(&buf->b_hdr->b_freeze_lock);
1002 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
1004 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
1007 mutex_enter(&buf->b_hdr->b_freeze_lock);
1008 if (buf->b_hdr->b_freeze_cksum != NULL) {
1009 mutex_exit(&buf->b_hdr->b_freeze_lock);
1012 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t),
1014 fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
1015 buf->b_hdr->b_freeze_cksum);
1016 mutex_exit(&buf->b_hdr->b_freeze_lock);
1020 arc_buf_thaw(arc_buf_t *buf)
1022 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1023 if (buf->b_hdr->b_state != arc_anon)
1024 panic("modifying non-anon buffer!");
1025 if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
1026 panic("modifying buffer while i/o in progress!");
1027 arc_cksum_verify(buf);
1030 mutex_enter(&buf->b_hdr->b_freeze_lock);
1031 if (buf->b_hdr->b_freeze_cksum != NULL) {
1032 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1033 buf->b_hdr->b_freeze_cksum = NULL;
1036 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1037 if (buf->b_hdr->b_thawed)
1038 kmem_free(buf->b_hdr->b_thawed, 1);
1039 buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
1042 mutex_exit(&buf->b_hdr->b_freeze_lock);
1046 arc_buf_freeze(arc_buf_t *buf)
1048 kmutex_t *hash_lock;
1050 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1053 hash_lock = HDR_LOCK(buf->b_hdr);
1054 mutex_enter(hash_lock);
1056 ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
1057 buf->b_hdr->b_state == arc_anon);
1058 arc_cksum_compute(buf, B_FALSE);
1059 mutex_exit(hash_lock);
1063 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1065 ASSERT(MUTEX_HELD(hash_lock));
1067 if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1068 (ab->b_state != arc_anon)) {
1069 uint64_t delta = ab->b_size * ab->b_datacnt;
1070 list_t *list = &ab->b_state->arcs_list[ab->b_type];
1071 uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1073 ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
1074 mutex_enter(&ab->b_state->arcs_mtx);
1075 ASSERT(list_link_active(&ab->b_arc_node));
1076 list_remove(list, ab);
1077 if (GHOST_STATE(ab->b_state)) {
1078 ASSERT0(ab->b_datacnt);
1079 ASSERT3P(ab->b_buf, ==, NULL);
1083 ASSERT3U(*size, >=, delta);
1084 atomic_add_64(size, -delta);
1085 mutex_exit(&ab->b_state->arcs_mtx);
1086 /* remove the prefetch flag if we get a reference */
1087 if (ab->b_flags & ARC_PREFETCH)
1088 ab->b_flags &= ~ARC_PREFETCH;
1093 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1096 arc_state_t *state = ab->b_state;
1098 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1099 ASSERT(!GHOST_STATE(state));
1101 if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1102 (state != arc_anon)) {
1103 uint64_t *size = &state->arcs_lsize[ab->b_type];
1105 ASSERT(!MUTEX_HELD(&state->arcs_mtx));
1106 mutex_enter(&state->arcs_mtx);
1107 ASSERT(!list_link_active(&ab->b_arc_node));
1108 list_insert_head(&state->arcs_list[ab->b_type], ab);
1109 ASSERT(ab->b_datacnt > 0);
1110 atomic_add_64(size, ab->b_size * ab->b_datacnt);
1111 mutex_exit(&state->arcs_mtx);
1117 * Move the supplied buffer to the indicated state. The mutex
1118 * for the buffer must be held by the caller.
1121 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1123 arc_state_t *old_state = ab->b_state;
1124 int64_t refcnt = refcount_count(&ab->b_refcnt);
1125 uint64_t from_delta, to_delta;
1127 ASSERT(MUTEX_HELD(hash_lock));
1128 ASSERT(new_state != old_state);
1129 ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1130 ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1131 ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1133 from_delta = to_delta = ab->b_datacnt * ab->b_size;
1136 * If this buffer is evictable, transfer it from the
1137 * old state list to the new state list.
1140 if (old_state != arc_anon) {
1141 int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
1142 uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1145 mutex_enter(&old_state->arcs_mtx);
1147 ASSERT(list_link_active(&ab->b_arc_node));
1148 list_remove(&old_state->arcs_list[ab->b_type], ab);
1151 * If prefetching out of the ghost cache,
1152 * we will have a non-zero datacnt.
1154 if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1155 /* ghost elements have a ghost size */
1156 ASSERT(ab->b_buf == NULL);
1157 from_delta = ab->b_size;
1159 ASSERT3U(*size, >=, from_delta);
1160 atomic_add_64(size, -from_delta);
1163 mutex_exit(&old_state->arcs_mtx);
1165 if (new_state != arc_anon) {
1166 int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
1167 uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1170 mutex_enter(&new_state->arcs_mtx);
1172 list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1174 /* ghost elements have a ghost size */
1175 if (GHOST_STATE(new_state)) {
1176 ASSERT(ab->b_datacnt == 0);
1177 ASSERT(ab->b_buf == NULL);
1178 to_delta = ab->b_size;
1180 atomic_add_64(size, to_delta);
1183 mutex_exit(&new_state->arcs_mtx);
1187 ASSERT(!BUF_EMPTY(ab));
1188 if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1189 buf_hash_remove(ab);
1191 /* adjust state sizes */
1193 atomic_add_64(&new_state->arcs_size, to_delta);
1195 ASSERT3U(old_state->arcs_size, >=, from_delta);
1196 atomic_add_64(&old_state->arcs_size, -from_delta);
1198 ab->b_state = new_state;
1200 /* adjust l2arc hdr stats */
1201 if (new_state == arc_l2c_only)
1202 l2arc_hdr_stat_add();
1203 else if (old_state == arc_l2c_only)
1204 l2arc_hdr_stat_remove();
1208 arc_space_consume(uint64_t space, arc_space_type_t type)
1210 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1215 case ARC_SPACE_DATA:
1216 ARCSTAT_INCR(arcstat_data_size, space);
1218 case ARC_SPACE_OTHER:
1219 ARCSTAT_INCR(arcstat_other_size, space);
1221 case ARC_SPACE_HDRS:
1222 ARCSTAT_INCR(arcstat_hdr_size, space);
1224 case ARC_SPACE_L2HDRS:
1225 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1229 atomic_add_64(&arc_meta_used, space);
1230 atomic_add_64(&arc_size, space);
1234 arc_space_return(uint64_t space, arc_space_type_t type)
1236 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1241 case ARC_SPACE_DATA:
1242 ARCSTAT_INCR(arcstat_data_size, -space);
1244 case ARC_SPACE_OTHER:
1245 ARCSTAT_INCR(arcstat_other_size, -space);
1247 case ARC_SPACE_HDRS:
1248 ARCSTAT_INCR(arcstat_hdr_size, -space);
1250 case ARC_SPACE_L2HDRS:
1251 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1255 ASSERT(arc_meta_used >= space);
1256 if (arc_meta_max < arc_meta_used)
1257 arc_meta_max = arc_meta_used;
1258 atomic_add_64(&arc_meta_used, -space);
1259 ASSERT(arc_size >= space);
1260 atomic_add_64(&arc_size, -space);
1264 arc_data_buf_alloc(uint64_t size)
1266 if (arc_evict_needed(ARC_BUFC_DATA))
1267 cv_signal(&arc_reclaim_thr_cv);
1268 atomic_add_64(&arc_size, size);
1269 return (zio_data_buf_alloc(size));
1273 arc_data_buf_free(void *buf, uint64_t size)
1275 zio_data_buf_free(buf, size);
1276 ASSERT(arc_size >= size);
1277 atomic_add_64(&arc_size, -size);
1281 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1286 ASSERT3U(size, >, 0);
1287 hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1288 ASSERT(BUF_EMPTY(hdr));
1291 hdr->b_spa = spa_load_guid(spa);
1292 hdr->b_state = arc_anon;
1293 hdr->b_arc_access = 0;
1294 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1297 buf->b_efunc = NULL;
1298 buf->b_private = NULL;
1301 arc_get_data_buf(buf);
1304 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1305 (void) refcount_add(&hdr->b_refcnt, tag);
1310 static char *arc_onloan_tag = "onloan";
1313 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1314 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1315 * buffers must be returned to the arc before they can be used by the DMU or
1319 arc_loan_buf(spa_t *spa, int size)
1323 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1325 atomic_add_64(&arc_loaned_bytes, size);
1330 * Return a loaned arc buffer to the arc.
1333 arc_return_buf(arc_buf_t *buf, void *tag)
1335 arc_buf_hdr_t *hdr = buf->b_hdr;
1337 ASSERT(buf->b_data != NULL);
1338 (void) refcount_add(&hdr->b_refcnt, tag);
1339 (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1341 atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1344 /* Detach an arc_buf from a dbuf (tag) */
1346 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1350 ASSERT(buf->b_data != NULL);
1352 (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1353 (void) refcount_remove(&hdr->b_refcnt, tag);
1354 buf->b_efunc = NULL;
1355 buf->b_private = NULL;
1357 atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1361 arc_buf_clone(arc_buf_t *from)
1364 arc_buf_hdr_t *hdr = from->b_hdr;
1365 uint64_t size = hdr->b_size;
1367 ASSERT(hdr->b_state != arc_anon);
1369 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1372 buf->b_efunc = NULL;
1373 buf->b_private = NULL;
1374 buf->b_next = hdr->b_buf;
1376 arc_get_data_buf(buf);
1377 bcopy(from->b_data, buf->b_data, size);
1380 * This buffer already exists in the arc so create a duplicate
1381 * copy for the caller. If the buffer is associated with user data
1382 * then track the size and number of duplicates. These stats will be
1383 * updated as duplicate buffers are created and destroyed.
1385 if (hdr->b_type == ARC_BUFC_DATA) {
1386 ARCSTAT_BUMP(arcstat_duplicate_buffers);
1387 ARCSTAT_INCR(arcstat_duplicate_buffers_size, size);
1389 hdr->b_datacnt += 1;
1394 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1397 kmutex_t *hash_lock;
1400 * Check to see if this buffer is evicted. Callers
1401 * must verify b_data != NULL to know if the add_ref
1404 mutex_enter(&buf->b_evict_lock);
1405 if (buf->b_data == NULL) {
1406 mutex_exit(&buf->b_evict_lock);
1409 hash_lock = HDR_LOCK(buf->b_hdr);
1410 mutex_enter(hash_lock);
1412 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1413 mutex_exit(&buf->b_evict_lock);
1415 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1416 add_reference(hdr, hash_lock, tag);
1417 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1418 arc_access(hdr, hash_lock);
1419 mutex_exit(hash_lock);
1420 ARCSTAT_BUMP(arcstat_hits);
1421 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1422 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1423 data, metadata, hits);
1427 * Free the arc data buffer. If it is an l2arc write in progress,
1428 * the buffer is placed on l2arc_free_on_write to be freed later.
1431 arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t),
1432 void *data, size_t size)
1434 if (HDR_L2_WRITING(hdr)) {
1435 l2arc_data_free_t *df;
1436 df = kmem_alloc(sizeof (l2arc_data_free_t), KM_PUSHPAGE);
1437 df->l2df_data = data;
1438 df->l2df_size = size;
1439 df->l2df_func = free_func;
1440 mutex_enter(&l2arc_free_on_write_mtx);
1441 list_insert_head(l2arc_free_on_write, df);
1442 mutex_exit(&l2arc_free_on_write_mtx);
1443 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1445 free_func(data, size);
1450 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1454 /* free up data associated with the buf */
1456 arc_state_t *state = buf->b_hdr->b_state;
1457 uint64_t size = buf->b_hdr->b_size;
1458 arc_buf_contents_t type = buf->b_hdr->b_type;
1460 arc_cksum_verify(buf);
1463 if (type == ARC_BUFC_METADATA) {
1464 arc_buf_data_free(buf->b_hdr, zio_buf_free,
1466 arc_space_return(size, ARC_SPACE_DATA);
1468 ASSERT(type == ARC_BUFC_DATA);
1469 arc_buf_data_free(buf->b_hdr,
1470 zio_data_buf_free, buf->b_data, size);
1471 ARCSTAT_INCR(arcstat_data_size, -size);
1472 atomic_add_64(&arc_size, -size);
1475 if (list_link_active(&buf->b_hdr->b_arc_node)) {
1476 uint64_t *cnt = &state->arcs_lsize[type];
1478 ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1479 ASSERT(state != arc_anon);
1481 ASSERT3U(*cnt, >=, size);
1482 atomic_add_64(cnt, -size);
1484 ASSERT3U(state->arcs_size, >=, size);
1485 atomic_add_64(&state->arcs_size, -size);
1489 * If we're destroying a duplicate buffer make sure
1490 * that the appropriate statistics are updated.
1492 if (buf->b_hdr->b_datacnt > 1 &&
1493 buf->b_hdr->b_type == ARC_BUFC_DATA) {
1494 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
1495 ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size);
1497 ASSERT(buf->b_hdr->b_datacnt > 0);
1498 buf->b_hdr->b_datacnt -= 1;
1501 /* only remove the buf if requested */
1505 /* remove the buf from the hdr list */
1506 for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1508 *bufp = buf->b_next;
1511 ASSERT(buf->b_efunc == NULL);
1513 /* clean up the buf */
1515 kmem_cache_free(buf_cache, buf);
1519 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1521 l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1523 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1524 ASSERT3P(hdr->b_state, ==, arc_anon);
1525 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1527 if (l2hdr != NULL) {
1528 boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1530 * To prevent arc_free() and l2arc_evict() from
1531 * attempting to free the same buffer at the same time,
1532 * a FREE_IN_PROGRESS flag is given to arc_free() to
1533 * give it priority. l2arc_evict() can't destroy this
1534 * header while we are waiting on l2arc_buflist_mtx.
1536 * The hdr may be removed from l2ad_buflist before we
1537 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1539 if (!buflist_held) {
1540 mutex_enter(&l2arc_buflist_mtx);
1541 l2hdr = hdr->b_l2hdr;
1544 if (l2hdr != NULL) {
1545 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1546 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1547 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1548 if (hdr->b_state == arc_l2c_only)
1549 l2arc_hdr_stat_remove();
1550 hdr->b_l2hdr = NULL;
1554 mutex_exit(&l2arc_buflist_mtx);
1557 if (!BUF_EMPTY(hdr)) {
1558 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1559 buf_discard_identity(hdr);
1561 while (hdr->b_buf) {
1562 arc_buf_t *buf = hdr->b_buf;
1565 mutex_enter(&arc_eviction_mtx);
1566 mutex_enter(&buf->b_evict_lock);
1567 ASSERT(buf->b_hdr != NULL);
1568 arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1569 hdr->b_buf = buf->b_next;
1570 buf->b_hdr = &arc_eviction_hdr;
1571 buf->b_next = arc_eviction_list;
1572 arc_eviction_list = buf;
1573 mutex_exit(&buf->b_evict_lock);
1574 mutex_exit(&arc_eviction_mtx);
1576 arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1579 if (hdr->b_freeze_cksum != NULL) {
1580 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1581 hdr->b_freeze_cksum = NULL;
1583 if (hdr->b_thawed) {
1584 kmem_free(hdr->b_thawed, 1);
1585 hdr->b_thawed = NULL;
1588 ASSERT(!list_link_active(&hdr->b_arc_node));
1589 ASSERT3P(hdr->b_hash_next, ==, NULL);
1590 ASSERT3P(hdr->b_acb, ==, NULL);
1591 kmem_cache_free(hdr_cache, hdr);
1595 arc_buf_free(arc_buf_t *buf, void *tag)
1597 arc_buf_hdr_t *hdr = buf->b_hdr;
1598 int hashed = hdr->b_state != arc_anon;
1600 ASSERT(buf->b_efunc == NULL);
1601 ASSERT(buf->b_data != NULL);
1604 kmutex_t *hash_lock = HDR_LOCK(hdr);
1606 mutex_enter(hash_lock);
1608 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1610 (void) remove_reference(hdr, hash_lock, tag);
1611 if (hdr->b_datacnt > 1) {
1612 arc_buf_destroy(buf, FALSE, TRUE);
1614 ASSERT(buf == hdr->b_buf);
1615 ASSERT(buf->b_efunc == NULL);
1616 hdr->b_flags |= ARC_BUF_AVAILABLE;
1618 mutex_exit(hash_lock);
1619 } else if (HDR_IO_IN_PROGRESS(hdr)) {
1622 * We are in the middle of an async write. Don't destroy
1623 * this buffer unless the write completes before we finish
1624 * decrementing the reference count.
1626 mutex_enter(&arc_eviction_mtx);
1627 (void) remove_reference(hdr, NULL, tag);
1628 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1629 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1630 mutex_exit(&arc_eviction_mtx);
1632 arc_hdr_destroy(hdr);
1634 if (remove_reference(hdr, NULL, tag) > 0)
1635 arc_buf_destroy(buf, FALSE, TRUE);
1637 arc_hdr_destroy(hdr);
1642 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1644 arc_buf_hdr_t *hdr = buf->b_hdr;
1645 kmutex_t *hash_lock = HDR_LOCK(hdr);
1646 int no_callback = (buf->b_efunc == NULL);
1648 if (hdr->b_state == arc_anon) {
1649 ASSERT(hdr->b_datacnt == 1);
1650 arc_buf_free(buf, tag);
1651 return (no_callback);
1654 mutex_enter(hash_lock);
1656 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1657 ASSERT(hdr->b_state != arc_anon);
1658 ASSERT(buf->b_data != NULL);
1660 (void) remove_reference(hdr, hash_lock, tag);
1661 if (hdr->b_datacnt > 1) {
1663 arc_buf_destroy(buf, FALSE, TRUE);
1664 } else if (no_callback) {
1665 ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1666 ASSERT(buf->b_efunc == NULL);
1667 hdr->b_flags |= ARC_BUF_AVAILABLE;
1669 ASSERT(no_callback || hdr->b_datacnt > 1 ||
1670 refcount_is_zero(&hdr->b_refcnt));
1671 mutex_exit(hash_lock);
1672 return (no_callback);
1676 arc_buf_size(arc_buf_t *buf)
1678 return (buf->b_hdr->b_size);
1682 * Called from the DMU to determine if the current buffer should be
1683 * evicted. In order to ensure proper locking, the eviction must be initiated
1684 * from the DMU. Return true if the buffer is associated with user data and
1685 * duplicate buffers still exist.
1688 arc_buf_eviction_needed(arc_buf_t *buf)
1691 boolean_t evict_needed = B_FALSE;
1693 if (zfs_disable_dup_eviction)
1696 mutex_enter(&buf->b_evict_lock);
1700 * We are in arc_do_user_evicts(); let that function
1701 * perform the eviction.
1703 ASSERT(buf->b_data == NULL);
1704 mutex_exit(&buf->b_evict_lock);
1706 } else if (buf->b_data == NULL) {
1708 * We have already been added to the arc eviction list;
1709 * recommend eviction.
1711 ASSERT3P(hdr, ==, &arc_eviction_hdr);
1712 mutex_exit(&buf->b_evict_lock);
1716 if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA)
1717 evict_needed = B_TRUE;
1719 mutex_exit(&buf->b_evict_lock);
1720 return (evict_needed);
1724 * Evict buffers from list until we've removed the specified number of
1725 * bytes. Move the removed buffers to the appropriate evict state.
1726 * If the recycle flag is set, then attempt to "recycle" a buffer:
1727 * - look for a buffer to evict that is `bytes' long.
1728 * - return the data block from this buffer rather than freeing it.
1729 * This flag is used by callers that are trying to make space for a
1730 * new buffer in a full arc cache.
1732 * This function makes a "best effort". It skips over any buffers
1733 * it can't get a hash_lock on, and so may not catch all candidates.
1734 * It may also return without evicting as much space as requested.
1737 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1738 arc_buf_contents_t type)
1740 arc_state_t *evicted_state;
1741 uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1742 arc_buf_hdr_t *ab, *ab_prev = NULL;
1743 list_t *list = &state->arcs_list[type];
1744 kmutex_t *hash_lock;
1745 boolean_t have_lock;
1746 void *stolen = NULL;
1748 ASSERT(state == arc_mru || state == arc_mfu);
1750 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1752 mutex_enter(&state->arcs_mtx);
1753 mutex_enter(&evicted_state->arcs_mtx);
1755 for (ab = list_tail(list); ab; ab = ab_prev) {
1756 ab_prev = list_prev(list, ab);
1757 /* prefetch buffers have a minimum lifespan */
1758 if (HDR_IO_IN_PROGRESS(ab) ||
1759 (spa && ab->b_spa != spa) ||
1760 (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1761 ddi_get_lbolt() - ab->b_arc_access <
1762 arc_min_prefetch_lifespan)) {
1766 /* "lookahead" for better eviction candidate */
1767 if (recycle && ab->b_size != bytes &&
1768 ab_prev && ab_prev->b_size == bytes)
1770 hash_lock = HDR_LOCK(ab);
1771 have_lock = MUTEX_HELD(hash_lock);
1772 if (have_lock || mutex_tryenter(hash_lock)) {
1773 ASSERT0(refcount_count(&ab->b_refcnt));
1774 ASSERT(ab->b_datacnt > 0);
1776 arc_buf_t *buf = ab->b_buf;
1777 if (!mutex_tryenter(&buf->b_evict_lock)) {
1782 bytes_evicted += ab->b_size;
1783 if (recycle && ab->b_type == type &&
1784 ab->b_size == bytes &&
1785 !HDR_L2_WRITING(ab)) {
1786 stolen = buf->b_data;
1791 mutex_enter(&arc_eviction_mtx);
1792 arc_buf_destroy(buf,
1793 buf->b_data == stolen, FALSE);
1794 ab->b_buf = buf->b_next;
1795 buf->b_hdr = &arc_eviction_hdr;
1796 buf->b_next = arc_eviction_list;
1797 arc_eviction_list = buf;
1798 mutex_exit(&arc_eviction_mtx);
1799 mutex_exit(&buf->b_evict_lock);
1801 mutex_exit(&buf->b_evict_lock);
1802 arc_buf_destroy(buf,
1803 buf->b_data == stolen, TRUE);
1808 ARCSTAT_INCR(arcstat_evict_l2_cached,
1811 if (l2arc_write_eligible(ab->b_spa, ab)) {
1812 ARCSTAT_INCR(arcstat_evict_l2_eligible,
1816 arcstat_evict_l2_ineligible,
1821 if (ab->b_datacnt == 0) {
1822 arc_change_state(evicted_state, ab, hash_lock);
1823 ASSERT(HDR_IN_HASH_TABLE(ab));
1824 ab->b_flags |= ARC_IN_HASH_TABLE;
1825 ab->b_flags &= ~ARC_BUF_AVAILABLE;
1826 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1829 mutex_exit(hash_lock);
1830 if (bytes >= 0 && bytes_evicted >= bytes)
1837 mutex_exit(&evicted_state->arcs_mtx);
1838 mutex_exit(&state->arcs_mtx);
1840 if (bytes_evicted < bytes)
1841 dprintf("only evicted %lld bytes from %x\n",
1842 (longlong_t)bytes_evicted, state);
1845 ARCSTAT_INCR(arcstat_evict_skip, skipped);
1848 ARCSTAT_INCR(arcstat_mutex_miss, missed);
1851 * We have just evicted some date into the ghost state, make
1852 * sure we also adjust the ghost state size if necessary.
1855 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
1856 int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
1857 arc_mru_ghost->arcs_size - arc_c;
1859 if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
1861 MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
1862 arc_evict_ghost(arc_mru_ghost, 0, todelete);
1863 } else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
1864 int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
1865 arc_mru_ghost->arcs_size +
1866 arc_mfu_ghost->arcs_size - arc_c);
1867 arc_evict_ghost(arc_mfu_ghost, 0, todelete);
1875 * Remove buffers from list until we've removed the specified number of
1876 * bytes. Destroy the buffers that are removed.
1879 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
1881 arc_buf_hdr_t *ab, *ab_prev;
1882 arc_buf_hdr_t marker;
1883 list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1884 kmutex_t *hash_lock;
1885 uint64_t bytes_deleted = 0;
1886 uint64_t bufs_skipped = 0;
1888 ASSERT(GHOST_STATE(state));
1889 bzero(&marker, sizeof(marker));
1891 mutex_enter(&state->arcs_mtx);
1892 for (ab = list_tail(list); ab; ab = ab_prev) {
1893 ab_prev = list_prev(list, ab);
1894 if (spa && ab->b_spa != spa)
1897 /* ignore markers */
1901 hash_lock = HDR_LOCK(ab);
1902 /* caller may be trying to modify this buffer, skip it */
1903 if (MUTEX_HELD(hash_lock))
1905 if (mutex_tryenter(hash_lock)) {
1906 ASSERT(!HDR_IO_IN_PROGRESS(ab));
1907 ASSERT(ab->b_buf == NULL);
1908 ARCSTAT_BUMP(arcstat_deleted);
1909 bytes_deleted += ab->b_size;
1911 if (ab->b_l2hdr != NULL) {
1913 * This buffer is cached on the 2nd Level ARC;
1914 * don't destroy the header.
1916 arc_change_state(arc_l2c_only, ab, hash_lock);
1917 mutex_exit(hash_lock);
1919 arc_change_state(arc_anon, ab, hash_lock);
1920 mutex_exit(hash_lock);
1921 arc_hdr_destroy(ab);
1924 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1925 if (bytes >= 0 && bytes_deleted >= bytes)
1927 } else if (bytes < 0) {
1929 * Insert a list marker and then wait for the
1930 * hash lock to become available. Once its
1931 * available, restart from where we left off.
1933 list_insert_after(list, ab, &marker);
1934 mutex_exit(&state->arcs_mtx);
1935 mutex_enter(hash_lock);
1936 mutex_exit(hash_lock);
1937 mutex_enter(&state->arcs_mtx);
1938 ab_prev = list_prev(list, &marker);
1939 list_remove(list, &marker);
1943 mutex_exit(&state->arcs_mtx);
1945 if (list == &state->arcs_list[ARC_BUFC_DATA] &&
1946 (bytes < 0 || bytes_deleted < bytes)) {
1947 list = &state->arcs_list[ARC_BUFC_METADATA];
1952 ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
1956 if (bytes_deleted < bytes)
1957 dprintf("only deleted %lld bytes from %p\n",
1958 (longlong_t)bytes_deleted, state);
1964 int64_t adjustment, delta;
1970 adjustment = MIN((int64_t)(arc_size - arc_c),
1971 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
1974 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
1975 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
1976 (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA);
1977 adjustment -= delta;
1980 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1981 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
1982 (void) arc_evict(arc_mru, 0, delta, FALSE,
1990 adjustment = arc_size - arc_c;
1992 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
1993 delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
1994 (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA);
1995 adjustment -= delta;
1998 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1999 int64_t delta = MIN(adjustment,
2000 arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
2001 (void) arc_evict(arc_mfu, 0, delta, FALSE,
2006 * Adjust ghost lists
2009 adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
2011 if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
2012 delta = MIN(arc_mru_ghost->arcs_size, adjustment);
2013 arc_evict_ghost(arc_mru_ghost, 0, delta);
2017 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
2019 if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
2020 delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
2021 arc_evict_ghost(arc_mfu_ghost, 0, delta);
2026 * Request that arc user drop references so that N bytes can be released
2027 * from the cache. This provides a mechanism to ensure the arc can honor
2028 * the arc_meta_limit and reclaim buffers which are pinned in the cache
2029 * by higher layers. (i.e. the zpl)
2032 arc_do_user_prune(int64_t adjustment)
2034 arc_prune_func_t *func;
2036 arc_prune_t *cp, *np;
2038 mutex_enter(&arc_prune_mtx);
2040 cp = list_head(&arc_prune_list);
2041 while (cp != NULL) {
2043 private = cp->p_private;
2044 np = list_next(&arc_prune_list, cp);
2045 refcount_add(&cp->p_refcnt, func);
2046 mutex_exit(&arc_prune_mtx);
2049 func(adjustment, private);
2051 mutex_enter(&arc_prune_mtx);
2053 /* User removed prune callback concurrently with execution */
2054 if (refcount_remove(&cp->p_refcnt, func) == 0) {
2055 ASSERT(!list_link_active(&cp->p_node));
2056 refcount_destroy(&cp->p_refcnt);
2057 kmem_free(cp, sizeof (*cp));
2063 ARCSTAT_BUMP(arcstat_prune);
2064 mutex_exit(&arc_prune_mtx);
2068 arc_do_user_evicts(void)
2070 mutex_enter(&arc_eviction_mtx);
2071 while (arc_eviction_list != NULL) {
2072 arc_buf_t *buf = arc_eviction_list;
2073 arc_eviction_list = buf->b_next;
2074 mutex_enter(&buf->b_evict_lock);
2076 mutex_exit(&buf->b_evict_lock);
2077 mutex_exit(&arc_eviction_mtx);
2079 if (buf->b_efunc != NULL)
2080 VERIFY(buf->b_efunc(buf) == 0);
2082 buf->b_efunc = NULL;
2083 buf->b_private = NULL;
2084 kmem_cache_free(buf_cache, buf);
2085 mutex_enter(&arc_eviction_mtx);
2087 mutex_exit(&arc_eviction_mtx);
2091 * Evict only meta data objects from the cache leaving the data objects.
2092 * This is only used to enforce the tunable arc_meta_limit, if we are
2093 * unable to evict enough buffers notify the user via the prune callback.
2096 arc_adjust_meta(int64_t adjustment, boolean_t may_prune)
2100 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2101 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2102 arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_METADATA);
2103 adjustment -= delta;
2106 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2107 delta = MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2108 arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_METADATA);
2109 adjustment -= delta;
2112 if (may_prune && (adjustment > 0) && (arc_meta_used > arc_meta_limit))
2113 arc_do_user_prune(arc_meta_prune);
2117 * Flush all *evictable* data from the cache for the given spa.
2118 * NOTE: this will not touch "active" (i.e. referenced) data.
2121 arc_flush(spa_t *spa)
2126 guid = spa_load_guid(spa);
2128 while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
2129 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
2133 while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
2134 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
2138 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
2139 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
2143 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
2144 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
2149 arc_evict_ghost(arc_mru_ghost, guid, -1);
2150 arc_evict_ghost(arc_mfu_ghost, guid, -1);
2152 mutex_enter(&arc_reclaim_thr_lock);
2153 arc_do_user_evicts();
2154 mutex_exit(&arc_reclaim_thr_lock);
2155 ASSERT(spa || arc_eviction_list == NULL);
2159 arc_shrink(uint64_t bytes)
2161 if (arc_c > arc_c_min) {
2164 to_free = bytes ? bytes : arc_c >> arc_shrink_shift;
2166 if (arc_c > arc_c_min + to_free)
2167 atomic_add_64(&arc_c, -to_free);
2171 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
2172 if (arc_c > arc_size)
2173 arc_c = MAX(arc_size, arc_c_min);
2175 arc_p = (arc_c >> 1);
2176 ASSERT(arc_c >= arc_c_min);
2177 ASSERT((int64_t)arc_p >= 0);
2180 if (arc_size > arc_c)
2185 arc_kmem_reap_now(arc_reclaim_strategy_t strat, uint64_t bytes)
2188 kmem_cache_t *prev_cache = NULL;
2189 kmem_cache_t *prev_data_cache = NULL;
2190 extern kmem_cache_t *zio_buf_cache[];
2191 extern kmem_cache_t *zio_data_buf_cache[];
2194 * An aggressive reclamation will shrink the cache size as well as
2195 * reap free buffers from the arc kmem caches.
2197 if (strat == ARC_RECLAIM_AGGR)
2200 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2201 if (zio_buf_cache[i] != prev_cache) {
2202 prev_cache = zio_buf_cache[i];
2203 kmem_cache_reap_now(zio_buf_cache[i]);
2205 if (zio_data_buf_cache[i] != prev_data_cache) {
2206 prev_data_cache = zio_data_buf_cache[i];
2207 kmem_cache_reap_now(zio_data_buf_cache[i]);
2211 kmem_cache_reap_now(buf_cache);
2212 kmem_cache_reap_now(hdr_cache);
2216 * Unlike other ZFS implementations this thread is only responsible for
2217 * adapting the target ARC size on Linux. The responsibility for memory
2218 * reclamation has been entirely delegated to the arc_shrinker_func()
2219 * which is registered with the VM. To reflect this change in behavior
2220 * the arc_reclaim thread has been renamed to arc_adapt.
2223 arc_adapt_thread(void)
2228 CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2230 mutex_enter(&arc_reclaim_thr_lock);
2231 while (arc_thread_exit == 0) {
2233 arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS;
2235 if (spa_get_random(100) == 0) {
2238 if (last_reclaim == ARC_RECLAIM_CONS) {
2239 last_reclaim = ARC_RECLAIM_AGGR;
2241 last_reclaim = ARC_RECLAIM_CONS;
2245 last_reclaim = ARC_RECLAIM_AGGR;
2249 /* reset the growth delay for every reclaim */
2250 arc_grow_time = ddi_get_lbolt()+(arc_grow_retry * hz);
2252 arc_kmem_reap_now(last_reclaim, 0);
2255 #endif /* !_KERNEL */
2257 /* No recent memory pressure allow the ARC to grow. */
2258 if (arc_no_grow && ddi_get_lbolt() >= arc_grow_time)
2259 arc_no_grow = FALSE;
2262 * Keep meta data usage within limits, arc_shrink() is not
2263 * used to avoid collapsing the arc_c value when only the
2264 * arc_meta_limit is being exceeded.
2266 prune = (int64_t)arc_meta_used - (int64_t)arc_meta_limit;
2268 arc_adjust_meta(prune, B_TRUE);
2272 if (arc_eviction_list != NULL)
2273 arc_do_user_evicts();
2275 /* block until needed, or one second, whichever is shorter */
2276 CALLB_CPR_SAFE_BEGIN(&cpr);
2277 (void) cv_timedwait_interruptible(&arc_reclaim_thr_cv,
2278 &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2279 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2282 arc_thread_exit = 0;
2283 cv_broadcast(&arc_reclaim_thr_cv);
2284 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */
2290 * Determine the amount of memory eligible for eviction contained in the
2291 * ARC. All clean data reported by the ghost lists can always be safely
2292 * evicted. Due to arc_c_min, the same does not hold for all clean data
2293 * contained by the regular mru and mfu lists.
2295 * In the case of the regular mru and mfu lists, we need to report as
2296 * much clean data as possible, such that evicting that same reported
2297 * data will not bring arc_size below arc_c_min. Thus, in certain
2298 * circumstances, the total amount of clean data in the mru and mfu
2299 * lists might not actually be evictable.
2301 * The following two distinct cases are accounted for:
2303 * 1. The sum of the amount of dirty data contained by both the mru and
2304 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2305 * is greater than or equal to arc_c_min.
2306 * (i.e. amount of dirty data >= arc_c_min)
2308 * This is the easy case; all clean data contained by the mru and mfu
2309 * lists is evictable. Evicting all clean data can only drop arc_size
2310 * to the amount of dirty data, which is greater than arc_c_min.
2312 * 2. The sum of the amount of dirty data contained by both the mru and
2313 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2314 * is less than arc_c_min.
2315 * (i.e. arc_c_min > amount of dirty data)
2317 * 2.1. arc_size is greater than or equal arc_c_min.
2318 * (i.e. arc_size >= arc_c_min > amount of dirty data)
2320 * In this case, not all clean data from the regular mru and mfu
2321 * lists is actually evictable; we must leave enough clean data
2322 * to keep arc_size above arc_c_min. Thus, the maximum amount of
2323 * evictable data from the two lists combined, is exactly the
2324 * difference between arc_size and arc_c_min.
2326 * 2.2. arc_size is less than arc_c_min
2327 * (i.e. arc_c_min > arc_size > amount of dirty data)
2329 * In this case, none of the data contained in the mru and mfu
2330 * lists is evictable, even if it's clean. Since arc_size is
2331 * already below arc_c_min, evicting any more would only
2332 * increase this negative difference.
2335 arc_evictable_memory(void) {
2336 uint64_t arc_clean =
2337 arc_mru->arcs_lsize[ARC_BUFC_DATA] +
2338 arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
2339 arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
2340 arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
2341 uint64_t ghost_clean =
2342 arc_mru_ghost->arcs_lsize[ARC_BUFC_DATA] +
2343 arc_mru_ghost->arcs_lsize[ARC_BUFC_METADATA] +
2344 arc_mfu_ghost->arcs_lsize[ARC_BUFC_DATA] +
2345 arc_mfu_ghost->arcs_lsize[ARC_BUFC_METADATA];
2346 uint64_t arc_dirty = MAX((int64_t)arc_size - (int64_t)arc_clean, 0);
2348 if (arc_dirty >= arc_c_min)
2349 return (ghost_clean + arc_clean);
2351 return (ghost_clean + MAX((int64_t)arc_size - (int64_t)arc_c_min, 0));
2355 __arc_shrinker_func(struct shrinker *shrink, struct shrink_control *sc)
2359 /* The arc is considered warm once reclaim has occurred */
2360 if (unlikely(arc_warm == B_FALSE))
2363 /* Return the potential number of reclaimable pages */
2364 pages = btop(arc_evictable_memory());
2365 if (sc->nr_to_scan == 0)
2368 /* Not allowed to perform filesystem reclaim */
2369 if (!(sc->gfp_mask & __GFP_FS))
2372 /* Reclaim in progress */
2373 if (mutex_tryenter(&arc_reclaim_thr_lock) == 0)
2377 * Evict the requested number of pages by shrinking arc_c the
2378 * requested amount. If there is nothing left to evict just
2379 * reap whatever we can from the various arc slabs.
2382 arc_kmem_reap_now(ARC_RECLAIM_AGGR, ptob(sc->nr_to_scan));
2383 pages = btop(arc_evictable_memory());
2385 arc_kmem_reap_now(ARC_RECLAIM_CONS, ptob(sc->nr_to_scan));
2390 * When direct reclaim is observed it usually indicates a rapid
2391 * increase in memory pressure. This occurs because the kswapd
2392 * threads were unable to asynchronously keep enough free memory
2393 * available. In this case set arc_no_grow to briefly pause arc
2394 * growth to avoid compounding the memory pressure.
2396 if (current_is_kswapd()) {
2397 ARCSTAT_BUMP(arcstat_memory_indirect_count);
2399 arc_no_grow = B_TRUE;
2400 arc_grow_time = ddi_get_lbolt() + (arc_grow_retry * hz);
2401 ARCSTAT_BUMP(arcstat_memory_direct_count);
2404 mutex_exit(&arc_reclaim_thr_lock);
2408 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func);
2410 SPL_SHRINKER_DECLARE(arc_shrinker, arc_shrinker_func, DEFAULT_SEEKS);
2411 #endif /* _KERNEL */
2414 * Adapt arc info given the number of bytes we are trying to add and
2415 * the state that we are comming from. This function is only called
2416 * when we are adding new content to the cache.
2419 arc_adapt(int bytes, arc_state_t *state)
2422 uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
2424 if (state == arc_l2c_only)
2429 * Adapt the target size of the MRU list:
2430 * - if we just hit in the MRU ghost list, then increase
2431 * the target size of the MRU list.
2432 * - if we just hit in the MFU ghost list, then increase
2433 * the target size of the MFU list by decreasing the
2434 * target size of the MRU list.
2436 if (state == arc_mru_ghost) {
2437 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2438 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2439 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2441 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2442 } else if (state == arc_mfu_ghost) {
2445 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2446 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2447 mult = MIN(mult, 10);
2449 delta = MIN(bytes * mult, arc_p);
2450 arc_p = MAX(arc_p_min, arc_p - delta);
2452 ASSERT((int64_t)arc_p >= 0);
2457 if (arc_c >= arc_c_max)
2461 * If we're within (2 * maxblocksize) bytes of the target
2462 * cache size, increment the target cache size
2464 if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2465 atomic_add_64(&arc_c, (int64_t)bytes);
2466 if (arc_c > arc_c_max)
2468 else if (state == arc_anon)
2469 atomic_add_64(&arc_p, (int64_t)bytes);
2473 ASSERT((int64_t)arc_p >= 0);
2477 * Check if the cache has reached its limits and eviction is required
2481 arc_evict_needed(arc_buf_contents_t type)
2483 if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2489 return (arc_size > arc_c);
2493 * The buffer, supplied as the first argument, needs a data block.
2494 * So, if we are at cache max, determine which cache should be victimized.
2495 * We have the following cases:
2497 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2498 * In this situation if we're out of space, but the resident size of the MFU is
2499 * under the limit, victimize the MFU cache to satisfy this insertion request.
2501 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2502 * Here, we've used up all of the available space for the MRU, so we need to
2503 * evict from our own cache instead. Evict from the set of resident MRU
2506 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2507 * c minus p represents the MFU space in the cache, since p is the size of the
2508 * cache that is dedicated to the MRU. In this situation there's still space on
2509 * the MFU side, so the MRU side needs to be victimized.
2511 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2512 * MFU's resident set is consuming more space than it has been allotted. In
2513 * this situation, we must victimize our own cache, the MFU, for this insertion.
2516 arc_get_data_buf(arc_buf_t *buf)
2518 arc_state_t *state = buf->b_hdr->b_state;
2519 uint64_t size = buf->b_hdr->b_size;
2520 arc_buf_contents_t type = buf->b_hdr->b_type;
2522 arc_adapt(size, state);
2525 * We have not yet reached cache maximum size,
2526 * just allocate a new buffer.
2528 if (!arc_evict_needed(type)) {
2529 if (type == ARC_BUFC_METADATA) {
2530 buf->b_data = zio_buf_alloc(size);
2531 arc_space_consume(size, ARC_SPACE_DATA);
2533 ASSERT(type == ARC_BUFC_DATA);
2534 buf->b_data = zio_data_buf_alloc(size);
2535 ARCSTAT_INCR(arcstat_data_size, size);
2536 atomic_add_64(&arc_size, size);
2542 * If we are prefetching from the mfu ghost list, this buffer
2543 * will end up on the mru list; so steal space from there.
2545 if (state == arc_mfu_ghost)
2546 state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2547 else if (state == arc_mru_ghost)
2550 if (state == arc_mru || state == arc_anon) {
2551 uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2552 state = (arc_mfu->arcs_lsize[type] >= size &&
2553 arc_p > mru_used) ? arc_mfu : arc_mru;
2556 uint64_t mfu_space = arc_c - arc_p;
2557 state = (arc_mru->arcs_lsize[type] >= size &&
2558 mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2561 if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) {
2562 if (type == ARC_BUFC_METADATA) {
2563 buf->b_data = zio_buf_alloc(size);
2564 arc_space_consume(size, ARC_SPACE_DATA);
2567 * If we are unable to recycle an existing meta buffer
2568 * signal the reclaim thread. It will notify users
2569 * via the prune callback to drop references. The
2570 * prune callback in run in the context of the reclaim
2571 * thread to avoid deadlocking on the hash_lock.
2573 cv_signal(&arc_reclaim_thr_cv);
2575 ASSERT(type == ARC_BUFC_DATA);
2576 buf->b_data = zio_data_buf_alloc(size);
2577 ARCSTAT_INCR(arcstat_data_size, size);
2578 atomic_add_64(&arc_size, size);
2581 ARCSTAT_BUMP(arcstat_recycle_miss);
2583 ASSERT(buf->b_data != NULL);
2586 * Update the state size. Note that ghost states have a
2587 * "ghost size" and so don't need to be updated.
2589 if (!GHOST_STATE(buf->b_hdr->b_state)) {
2590 arc_buf_hdr_t *hdr = buf->b_hdr;
2592 atomic_add_64(&hdr->b_state->arcs_size, size);
2593 if (list_link_active(&hdr->b_arc_node)) {
2594 ASSERT(refcount_is_zero(&hdr->b_refcnt));
2595 atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2598 * If we are growing the cache, and we are adding anonymous
2599 * data, and we have outgrown arc_p, update arc_p
2601 if (arc_size < arc_c && hdr->b_state == arc_anon &&
2602 arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2603 arc_p = MIN(arc_c, arc_p + size);
2608 * This routine is called whenever a buffer is accessed.
2609 * NOTE: the hash lock is dropped in this function.
2612 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2616 ASSERT(MUTEX_HELD(hash_lock));
2618 if (buf->b_state == arc_anon) {
2620 * This buffer is not in the cache, and does not
2621 * appear in our "ghost" list. Add the new buffer
2625 ASSERT(buf->b_arc_access == 0);
2626 buf->b_arc_access = ddi_get_lbolt();
2627 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2628 arc_change_state(arc_mru, buf, hash_lock);
2630 } else if (buf->b_state == arc_mru) {
2631 now = ddi_get_lbolt();
2634 * If this buffer is here because of a prefetch, then either:
2635 * - clear the flag if this is a "referencing" read
2636 * (any subsequent access will bump this into the MFU state).
2638 * - move the buffer to the head of the list if this is
2639 * another prefetch (to make it less likely to be evicted).
2641 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2642 if (refcount_count(&buf->b_refcnt) == 0) {
2643 ASSERT(list_link_active(&buf->b_arc_node));
2645 buf->b_flags &= ~ARC_PREFETCH;
2646 ARCSTAT_BUMP(arcstat_mru_hits);
2648 buf->b_arc_access = now;
2653 * This buffer has been "accessed" only once so far,
2654 * but it is still in the cache. Move it to the MFU
2657 if (now > buf->b_arc_access + ARC_MINTIME) {
2659 * More than 125ms have passed since we
2660 * instantiated this buffer. Move it to the
2661 * most frequently used state.
2663 buf->b_arc_access = now;
2664 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2665 arc_change_state(arc_mfu, buf, hash_lock);
2667 ARCSTAT_BUMP(arcstat_mru_hits);
2668 } else if (buf->b_state == arc_mru_ghost) {
2669 arc_state_t *new_state;
2671 * This buffer has been "accessed" recently, but
2672 * was evicted from the cache. Move it to the
2676 if (buf->b_flags & ARC_PREFETCH) {
2677 new_state = arc_mru;
2678 if (refcount_count(&buf->b_refcnt) > 0)
2679 buf->b_flags &= ~ARC_PREFETCH;
2680 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2682 new_state = arc_mfu;
2683 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2686 buf->b_arc_access = ddi_get_lbolt();
2687 arc_change_state(new_state, buf, hash_lock);
2689 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2690 } else if (buf->b_state == arc_mfu) {
2692 * This buffer has been accessed more than once and is
2693 * still in the cache. Keep it in the MFU state.
2695 * NOTE: an add_reference() that occurred when we did
2696 * the arc_read() will have kicked this off the list.
2697 * If it was a prefetch, we will explicitly move it to
2698 * the head of the list now.
2700 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2701 ASSERT(refcount_count(&buf->b_refcnt) == 0);
2702 ASSERT(list_link_active(&buf->b_arc_node));
2704 ARCSTAT_BUMP(arcstat_mfu_hits);
2705 buf->b_arc_access = ddi_get_lbolt();
2706 } else if (buf->b_state == arc_mfu_ghost) {
2707 arc_state_t *new_state = arc_mfu;
2709 * This buffer has been accessed more than once but has
2710 * been evicted from the cache. Move it back to the
2714 if (buf->b_flags & ARC_PREFETCH) {
2716 * This is a prefetch access...
2717 * move this block back to the MRU state.
2719 ASSERT0(refcount_count(&buf->b_refcnt));
2720 new_state = arc_mru;
2723 buf->b_arc_access = ddi_get_lbolt();
2724 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2725 arc_change_state(new_state, buf, hash_lock);
2727 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2728 } else if (buf->b_state == arc_l2c_only) {
2730 * This buffer is on the 2nd Level ARC.
2733 buf->b_arc_access = ddi_get_lbolt();
2734 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2735 arc_change_state(arc_mfu, buf, hash_lock);
2737 ASSERT(!"invalid arc state");
2741 /* a generic arc_done_func_t which you can use */
2744 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2746 if (zio == NULL || zio->io_error == 0)
2747 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2748 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2751 /* a generic arc_done_func_t */
2753 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2755 arc_buf_t **bufp = arg;
2756 if (zio && zio->io_error) {
2757 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2761 ASSERT(buf->b_data);
2766 arc_read_done(zio_t *zio)
2768 arc_buf_hdr_t *hdr, *found;
2770 arc_buf_t *abuf; /* buffer we're assigning to callback */
2771 kmutex_t *hash_lock;
2772 arc_callback_t *callback_list, *acb;
2773 int freeable = FALSE;
2775 buf = zio->io_private;
2779 * The hdr was inserted into hash-table and removed from lists
2780 * prior to starting I/O. We should find this header, since
2781 * it's in the hash table, and it should be legit since it's
2782 * not possible to evict it during the I/O. The only possible
2783 * reason for it not to be found is if we were freed during the
2786 found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
2789 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
2790 (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2791 (found == hdr && HDR_L2_READING(hdr)));
2793 hdr->b_flags &= ~ARC_L2_EVICTED;
2794 if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2795 hdr->b_flags &= ~ARC_L2CACHE;
2797 /* byteswap if necessary */
2798 callback_list = hdr->b_acb;
2799 ASSERT(callback_list != NULL);
2800 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2801 dmu_object_byteswap_t bswap =
2802 DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
2803 if (BP_GET_LEVEL(zio->io_bp) > 0)
2804 byteswap_uint64_array(buf->b_data, hdr->b_size);
2806 dmu_ot_byteswap[bswap].ob_func(buf->b_data, hdr->b_size);
2809 arc_cksum_compute(buf, B_FALSE);
2811 if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
2813 * Only call arc_access on anonymous buffers. This is because
2814 * if we've issued an I/O for an evicted buffer, we've already
2815 * called arc_access (to prevent any simultaneous readers from
2816 * getting confused).
2818 arc_access(hdr, hash_lock);
2821 /* create copies of the data buffer for the callers */
2823 for (acb = callback_list; acb; acb = acb->acb_next) {
2824 if (acb->acb_done) {
2826 ARCSTAT_BUMP(arcstat_duplicate_reads);
2827 abuf = arc_buf_clone(buf);
2829 acb->acb_buf = abuf;
2834 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2835 ASSERT(!HDR_BUF_AVAILABLE(hdr));
2837 ASSERT(buf->b_efunc == NULL);
2838 ASSERT(hdr->b_datacnt == 1);
2839 hdr->b_flags |= ARC_BUF_AVAILABLE;
2842 ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2844 if (zio->io_error != 0) {
2845 hdr->b_flags |= ARC_IO_ERROR;
2846 if (hdr->b_state != arc_anon)
2847 arc_change_state(arc_anon, hdr, hash_lock);
2848 if (HDR_IN_HASH_TABLE(hdr))
2849 buf_hash_remove(hdr);
2850 freeable = refcount_is_zero(&hdr->b_refcnt);
2854 * Broadcast before we drop the hash_lock to avoid the possibility
2855 * that the hdr (and hence the cv) might be freed before we get to
2856 * the cv_broadcast().
2858 cv_broadcast(&hdr->b_cv);
2861 mutex_exit(hash_lock);
2864 * This block was freed while we waited for the read to
2865 * complete. It has been removed from the hash table and
2866 * moved to the anonymous state (so that it won't show up
2869 ASSERT3P(hdr->b_state, ==, arc_anon);
2870 freeable = refcount_is_zero(&hdr->b_refcnt);
2873 /* execute each callback and free its structure */
2874 while ((acb = callback_list) != NULL) {
2876 acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2878 if (acb->acb_zio_dummy != NULL) {
2879 acb->acb_zio_dummy->io_error = zio->io_error;
2880 zio_nowait(acb->acb_zio_dummy);
2883 callback_list = acb->acb_next;
2884 kmem_free(acb, sizeof (arc_callback_t));
2888 arc_hdr_destroy(hdr);
2892 * "Read" the block at the specified DVA (in bp) via the
2893 * cache. If the block is found in the cache, invoke the provided
2894 * callback immediately and return. Note that the `zio' parameter
2895 * in the callback will be NULL in this case, since no IO was
2896 * required. If the block is not in the cache pass the read request
2897 * on to the spa with a substitute callback function, so that the
2898 * requested block will be added to the cache.
2900 * If a read request arrives for a block that has a read in-progress,
2901 * either wait for the in-progress read to complete (and return the
2902 * results); or, if this is a read with a "done" func, add a record
2903 * to the read to invoke the "done" func when the read completes,
2904 * and return; or just return.
2906 * arc_read_done() will invoke all the requested "done" functions
2907 * for readers of this block.
2910 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done,
2911 void *private, int priority, int zio_flags, uint32_t *arc_flags,
2912 const zbookmark_t *zb)
2915 arc_buf_t *buf = NULL;
2916 kmutex_t *hash_lock;
2918 uint64_t guid = spa_load_guid(spa);
2921 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
2923 if (hdr && hdr->b_datacnt > 0) {
2925 *arc_flags |= ARC_CACHED;
2927 if (HDR_IO_IN_PROGRESS(hdr)) {
2929 if (*arc_flags & ARC_WAIT) {
2930 cv_wait(&hdr->b_cv, hash_lock);
2931 mutex_exit(hash_lock);
2934 ASSERT(*arc_flags & ARC_NOWAIT);
2937 arc_callback_t *acb = NULL;
2939 acb = kmem_zalloc(sizeof (arc_callback_t),
2941 acb->acb_done = done;
2942 acb->acb_private = private;
2944 acb->acb_zio_dummy = zio_null(pio,
2945 spa, NULL, NULL, NULL, zio_flags);
2947 ASSERT(acb->acb_done != NULL);
2948 acb->acb_next = hdr->b_acb;
2950 add_reference(hdr, hash_lock, private);
2951 mutex_exit(hash_lock);
2954 mutex_exit(hash_lock);
2958 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2961 add_reference(hdr, hash_lock, private);
2963 * If this block is already in use, create a new
2964 * copy of the data so that we will be guaranteed
2965 * that arc_release() will always succeed.
2969 ASSERT(buf->b_data);
2970 if (HDR_BUF_AVAILABLE(hdr)) {
2971 ASSERT(buf->b_efunc == NULL);
2972 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
2974 buf = arc_buf_clone(buf);
2977 } else if (*arc_flags & ARC_PREFETCH &&
2978 refcount_count(&hdr->b_refcnt) == 0) {
2979 hdr->b_flags |= ARC_PREFETCH;
2981 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
2982 arc_access(hdr, hash_lock);
2983 if (*arc_flags & ARC_L2CACHE)
2984 hdr->b_flags |= ARC_L2CACHE;
2985 mutex_exit(hash_lock);
2986 ARCSTAT_BUMP(arcstat_hits);
2987 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2988 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
2989 data, metadata, hits);
2992 done(NULL, buf, private);
2994 uint64_t size = BP_GET_LSIZE(bp);
2995 arc_callback_t *acb;
2998 boolean_t devw = B_FALSE;
3001 /* this block is not in the cache */
3002 arc_buf_hdr_t *exists;
3003 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
3004 buf = arc_buf_alloc(spa, size, private, type);
3006 hdr->b_dva = *BP_IDENTITY(bp);
3007 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
3008 hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
3009 exists = buf_hash_insert(hdr, &hash_lock);
3011 /* somebody beat us to the hash insert */
3012 mutex_exit(hash_lock);
3013 buf_discard_identity(hdr);
3014 (void) arc_buf_remove_ref(buf, private);
3015 goto top; /* restart the IO request */
3017 /* if this is a prefetch, we don't have a reference */
3018 if (*arc_flags & ARC_PREFETCH) {
3019 (void) remove_reference(hdr, hash_lock,
3021 hdr->b_flags |= ARC_PREFETCH;
3023 if (*arc_flags & ARC_L2CACHE)
3024 hdr->b_flags |= ARC_L2CACHE;
3025 if (BP_GET_LEVEL(bp) > 0)
3026 hdr->b_flags |= ARC_INDIRECT;
3028 /* this block is in the ghost cache */
3029 ASSERT(GHOST_STATE(hdr->b_state));
3030 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3031 ASSERT0(refcount_count(&hdr->b_refcnt));
3032 ASSERT(hdr->b_buf == NULL);
3034 /* if this is a prefetch, we don't have a reference */
3035 if (*arc_flags & ARC_PREFETCH)
3036 hdr->b_flags |= ARC_PREFETCH;
3038 add_reference(hdr, hash_lock, private);
3039 if (*arc_flags & ARC_L2CACHE)
3040 hdr->b_flags |= ARC_L2CACHE;
3041 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
3044 buf->b_efunc = NULL;
3045 buf->b_private = NULL;
3048 ASSERT(hdr->b_datacnt == 0);
3050 arc_get_data_buf(buf);
3051 arc_access(hdr, hash_lock);
3054 ASSERT(!GHOST_STATE(hdr->b_state));
3056 acb = kmem_zalloc(sizeof (arc_callback_t), KM_PUSHPAGE);
3057 acb->acb_done = done;
3058 acb->acb_private = private;
3060 ASSERT(hdr->b_acb == NULL);
3062 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3064 if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
3065 (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
3066 devw = hdr->b_l2hdr->b_dev->l2ad_writing;
3067 addr = hdr->b_l2hdr->b_daddr;
3069 * Lock out device removal.
3071 if (vdev_is_dead(vd) ||
3072 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
3076 mutex_exit(hash_lock);
3078 ASSERT3U(hdr->b_size, ==, size);
3079 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
3080 uint64_t, size, zbookmark_t *, zb);
3081 ARCSTAT_BUMP(arcstat_misses);
3082 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3083 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3084 data, metadata, misses);
3086 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
3088 * Read from the L2ARC if the following are true:
3089 * 1. The L2ARC vdev was previously cached.
3090 * 2. This buffer still has L2ARC metadata.
3091 * 3. This buffer isn't currently writing to the L2ARC.
3092 * 4. The L2ARC entry wasn't evicted, which may
3093 * also have invalidated the vdev.
3094 * 5. This isn't prefetch and l2arc_noprefetch is set.
3096 if (hdr->b_l2hdr != NULL &&
3097 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
3098 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
3099 l2arc_read_callback_t *cb;
3101 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
3102 ARCSTAT_BUMP(arcstat_l2_hits);
3104 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
3106 cb->l2rcb_buf = buf;
3107 cb->l2rcb_spa = spa;
3110 cb->l2rcb_flags = zio_flags;
3113 * l2arc read. The SCL_L2ARC lock will be
3114 * released by l2arc_read_done().
3116 rzio = zio_read_phys(pio, vd, addr, size,
3117 buf->b_data, ZIO_CHECKSUM_OFF,
3118 l2arc_read_done, cb, priority, zio_flags |
3119 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL |
3120 ZIO_FLAG_DONT_PROPAGATE |
3121 ZIO_FLAG_DONT_RETRY, B_FALSE);
3122 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
3124 ARCSTAT_INCR(arcstat_l2_read_bytes, size);
3126 if (*arc_flags & ARC_NOWAIT) {
3131 ASSERT(*arc_flags & ARC_WAIT);
3132 if (zio_wait(rzio) == 0)
3135 /* l2arc read error; goto zio_read() */
3137 DTRACE_PROBE1(l2arc__miss,
3138 arc_buf_hdr_t *, hdr);
3139 ARCSTAT_BUMP(arcstat_l2_misses);
3140 if (HDR_L2_WRITING(hdr))
3141 ARCSTAT_BUMP(arcstat_l2_rw_clash);
3142 spa_config_exit(spa, SCL_L2ARC, vd);
3146 spa_config_exit(spa, SCL_L2ARC, vd);
3147 if (l2arc_ndev != 0) {
3148 DTRACE_PROBE1(l2arc__miss,
3149 arc_buf_hdr_t *, hdr);
3150 ARCSTAT_BUMP(arcstat_l2_misses);
3154 rzio = zio_read(pio, spa, bp, buf->b_data, size,
3155 arc_read_done, buf, priority, zio_flags, zb);
3157 if (*arc_flags & ARC_WAIT)
3158 return (zio_wait(rzio));
3160 ASSERT(*arc_flags & ARC_NOWAIT);
3167 arc_add_prune_callback(arc_prune_func_t *func, void *private)
3171 p = kmem_alloc(sizeof(*p), KM_SLEEP);
3173 p->p_private = private;
3174 list_link_init(&p->p_node);
3175 refcount_create(&p->p_refcnt);
3177 mutex_enter(&arc_prune_mtx);
3178 refcount_add(&p->p_refcnt, &arc_prune_list);
3179 list_insert_head(&arc_prune_list, p);
3180 mutex_exit(&arc_prune_mtx);
3186 arc_remove_prune_callback(arc_prune_t *p)
3188 mutex_enter(&arc_prune_mtx);
3189 list_remove(&arc_prune_list, p);
3190 if (refcount_remove(&p->p_refcnt, &arc_prune_list) == 0) {
3191 refcount_destroy(&p->p_refcnt);
3192 kmem_free(p, sizeof (*p));
3194 mutex_exit(&arc_prune_mtx);
3198 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
3200 ASSERT(buf->b_hdr != NULL);
3201 ASSERT(buf->b_hdr->b_state != arc_anon);
3202 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
3203 ASSERT(buf->b_efunc == NULL);
3204 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
3206 buf->b_efunc = func;
3207 buf->b_private = private;
3211 * Notify the arc that a block was freed, and thus will never be used again.
3214 arc_freed(spa_t *spa, const blkptr_t *bp)
3217 kmutex_t *hash_lock;
3218 uint64_t guid = spa_load_guid(spa);
3220 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
3224 if (HDR_BUF_AVAILABLE(hdr)) {
3225 arc_buf_t *buf = hdr->b_buf;
3226 add_reference(hdr, hash_lock, FTAG);
3227 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3228 mutex_exit(hash_lock);
3230 arc_release(buf, FTAG);
3231 (void) arc_buf_remove_ref(buf, FTAG);
3233 mutex_exit(hash_lock);
3239 * This is used by the DMU to let the ARC know that a buffer is
3240 * being evicted, so the ARC should clean up. If this arc buf
3241 * is not yet in the evicted state, it will be put there.
3244 arc_buf_evict(arc_buf_t *buf)
3247 kmutex_t *hash_lock;
3250 mutex_enter(&buf->b_evict_lock);
3254 * We are in arc_do_user_evicts().
3256 ASSERT(buf->b_data == NULL);
3257 mutex_exit(&buf->b_evict_lock);
3259 } else if (buf->b_data == NULL) {
3260 arc_buf_t copy = *buf; /* structure assignment */
3262 * We are on the eviction list; process this buffer now
3263 * but let arc_do_user_evicts() do the reaping.
3265 buf->b_efunc = NULL;
3266 mutex_exit(&buf->b_evict_lock);
3267 VERIFY(copy.b_efunc(©) == 0);
3270 hash_lock = HDR_LOCK(hdr);
3271 mutex_enter(hash_lock);
3273 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3275 ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
3276 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3279 * Pull this buffer off of the hdr
3282 while (*bufp != buf)
3283 bufp = &(*bufp)->b_next;
3284 *bufp = buf->b_next;
3286 ASSERT(buf->b_data != NULL);
3287 arc_buf_destroy(buf, FALSE, FALSE);
3289 if (hdr->b_datacnt == 0) {
3290 arc_state_t *old_state = hdr->b_state;
3291 arc_state_t *evicted_state;
3293 ASSERT(hdr->b_buf == NULL);
3294 ASSERT(refcount_is_zero(&hdr->b_refcnt));
3297 (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
3299 mutex_enter(&old_state->arcs_mtx);
3300 mutex_enter(&evicted_state->arcs_mtx);
3302 arc_change_state(evicted_state, hdr, hash_lock);
3303 ASSERT(HDR_IN_HASH_TABLE(hdr));
3304 hdr->b_flags |= ARC_IN_HASH_TABLE;
3305 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3307 mutex_exit(&evicted_state->arcs_mtx);
3308 mutex_exit(&old_state->arcs_mtx);
3310 mutex_exit(hash_lock);
3311 mutex_exit(&buf->b_evict_lock);
3313 VERIFY(buf->b_efunc(buf) == 0);
3314 buf->b_efunc = NULL;
3315 buf->b_private = NULL;
3318 kmem_cache_free(buf_cache, buf);
3323 * Release this buffer from the cache. This must be done
3324 * after a read and prior to modifying the buffer contents.
3325 * If the buffer has more than one reference, we must make
3326 * a new hdr for the buffer.
3329 arc_release(arc_buf_t *buf, void *tag)
3332 kmutex_t *hash_lock = NULL;
3333 l2arc_buf_hdr_t *l2hdr;
3334 uint64_t buf_size = 0;
3337 * It would be nice to assert that if it's DMU metadata (level >
3338 * 0 || it's the dnode file), then it must be syncing context.
3339 * But we don't know that information at this level.
3342 mutex_enter(&buf->b_evict_lock);
3345 /* this buffer is not on any list */
3346 ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3348 if (hdr->b_state == arc_anon) {
3349 /* this buffer is already released */
3350 ASSERT(buf->b_efunc == NULL);
3352 hash_lock = HDR_LOCK(hdr);
3353 mutex_enter(hash_lock);
3355 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3358 l2hdr = hdr->b_l2hdr;
3360 mutex_enter(&l2arc_buflist_mtx);
3361 hdr->b_l2hdr = NULL;
3362 buf_size = hdr->b_size;
3366 * Do we have more than one buf?
3368 if (hdr->b_datacnt > 1) {
3369 arc_buf_hdr_t *nhdr;
3371 uint64_t blksz = hdr->b_size;
3372 uint64_t spa = hdr->b_spa;
3373 arc_buf_contents_t type = hdr->b_type;
3374 uint32_t flags = hdr->b_flags;
3376 ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3378 * Pull the data off of this hdr and attach it to
3379 * a new anonymous hdr.
3381 (void) remove_reference(hdr, hash_lock, tag);
3383 while (*bufp != buf)
3384 bufp = &(*bufp)->b_next;
3385 *bufp = buf->b_next;
3388 ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3389 atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3390 if (refcount_is_zero(&hdr->b_refcnt)) {
3391 uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3392 ASSERT3U(*size, >=, hdr->b_size);
3393 atomic_add_64(size, -hdr->b_size);
3397 * We're releasing a duplicate user data buffer, update
3398 * our statistics accordingly.
3400 if (hdr->b_type == ARC_BUFC_DATA) {
3401 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
3402 ARCSTAT_INCR(arcstat_duplicate_buffers_size,
3405 hdr->b_datacnt -= 1;
3406 arc_cksum_verify(buf);
3408 mutex_exit(hash_lock);
3410 nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3411 nhdr->b_size = blksz;
3413 nhdr->b_type = type;
3415 nhdr->b_state = arc_anon;
3416 nhdr->b_arc_access = 0;
3417 nhdr->b_flags = flags & ARC_L2_WRITING;
3418 nhdr->b_l2hdr = NULL;
3419 nhdr->b_datacnt = 1;
3420 nhdr->b_freeze_cksum = NULL;
3421 (void) refcount_add(&nhdr->b_refcnt, tag);
3423 mutex_exit(&buf->b_evict_lock);
3424 atomic_add_64(&arc_anon->arcs_size, blksz);
3426 mutex_exit(&buf->b_evict_lock);
3427 ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3428 ASSERT(!list_link_active(&hdr->b_arc_node));
3429 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3430 if (hdr->b_state != arc_anon)
3431 arc_change_state(arc_anon, hdr, hash_lock);
3432 hdr->b_arc_access = 0;
3434 mutex_exit(hash_lock);
3436 buf_discard_identity(hdr);
3439 buf->b_efunc = NULL;
3440 buf->b_private = NULL;
3443 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3444 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3445 ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3446 mutex_exit(&l2arc_buflist_mtx);
3451 arc_released(arc_buf_t *buf)
3455 mutex_enter(&buf->b_evict_lock);
3456 released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3457 mutex_exit(&buf->b_evict_lock);
3462 arc_has_callback(arc_buf_t *buf)
3466 mutex_enter(&buf->b_evict_lock);
3467 callback = (buf->b_efunc != NULL);
3468 mutex_exit(&buf->b_evict_lock);
3474 arc_referenced(arc_buf_t *buf)
3478 mutex_enter(&buf->b_evict_lock);
3479 referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3480 mutex_exit(&buf->b_evict_lock);
3481 return (referenced);
3486 arc_write_ready(zio_t *zio)
3488 arc_write_callback_t *callback = zio->io_private;
3489 arc_buf_t *buf = callback->awcb_buf;
3490 arc_buf_hdr_t *hdr = buf->b_hdr;
3492 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3493 callback->awcb_ready(zio, buf, callback->awcb_private);
3496 * If the IO is already in progress, then this is a re-write
3497 * attempt, so we need to thaw and re-compute the cksum.
3498 * It is the responsibility of the callback to handle the
3499 * accounting for any re-write attempt.
3501 if (HDR_IO_IN_PROGRESS(hdr)) {
3502 mutex_enter(&hdr->b_freeze_lock);
3503 if (hdr->b_freeze_cksum != NULL) {
3504 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3505 hdr->b_freeze_cksum = NULL;
3507 mutex_exit(&hdr->b_freeze_lock);
3509 arc_cksum_compute(buf, B_FALSE);
3510 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3514 arc_write_done(zio_t *zio)
3516 arc_write_callback_t *callback = zio->io_private;
3517 arc_buf_t *buf = callback->awcb_buf;
3518 arc_buf_hdr_t *hdr = buf->b_hdr;
3520 ASSERT(hdr->b_acb == NULL);
3522 if (zio->io_error == 0) {
3523 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3524 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3525 hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3527 ASSERT(BUF_EMPTY(hdr));
3531 * If the block to be written was all-zero, we may have
3532 * compressed it away. In this case no write was performed
3533 * so there will be no dva/birth/checksum. The buffer must
3534 * therefore remain anonymous (and uncached).
3536 if (!BUF_EMPTY(hdr)) {
3537 arc_buf_hdr_t *exists;
3538 kmutex_t *hash_lock;
3540 ASSERT(zio->io_error == 0);
3542 arc_cksum_verify(buf);
3544 exists = buf_hash_insert(hdr, &hash_lock);
3547 * This can only happen if we overwrite for
3548 * sync-to-convergence, because we remove
3549 * buffers from the hash table when we arc_free().
3551 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3552 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3553 panic("bad overwrite, hdr=%p exists=%p",
3554 (void *)hdr, (void *)exists);
3555 ASSERT(refcount_is_zero(&exists->b_refcnt));
3556 arc_change_state(arc_anon, exists, hash_lock);
3557 mutex_exit(hash_lock);
3558 arc_hdr_destroy(exists);
3559 exists = buf_hash_insert(hdr, &hash_lock);
3560 ASSERT3P(exists, ==, NULL);
3563 ASSERT(hdr->b_datacnt == 1);
3564 ASSERT(hdr->b_state == arc_anon);
3565 ASSERT(BP_GET_DEDUP(zio->io_bp));
3566 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3569 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3570 /* if it's not anon, we are doing a scrub */
3571 if (!exists && hdr->b_state == arc_anon)
3572 arc_access(hdr, hash_lock);
3573 mutex_exit(hash_lock);
3575 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3578 ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3579 callback->awcb_done(zio, buf, callback->awcb_private);
3581 kmem_free(callback, sizeof (arc_write_callback_t));
3585 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3586 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
3587 arc_done_func_t *ready, arc_done_func_t *done, void *private,
3588 int priority, int zio_flags, const zbookmark_t *zb)
3590 arc_buf_hdr_t *hdr = buf->b_hdr;
3591 arc_write_callback_t *callback;
3594 ASSERT(ready != NULL);
3595 ASSERT(done != NULL);
3596 ASSERT(!HDR_IO_ERROR(hdr));
3597 ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3598 ASSERT(hdr->b_acb == NULL);
3600 hdr->b_flags |= ARC_L2CACHE;
3601 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_PUSHPAGE);
3602 callback->awcb_ready = ready;
3603 callback->awcb_done = done;
3604 callback->awcb_private = private;
3605 callback->awcb_buf = buf;
3607 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3608 arc_write_ready, arc_write_done, callback, priority, zio_flags, zb);
3614 arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg)
3617 uint64_t available_memory;
3619 if (zfs_arc_memory_throttle_disable)
3622 /* Easily reclaimable memory (free + inactive + arc-evictable) */
3623 available_memory = ptob(spl_kmem_availrmem()) + arc_evictable_memory();
3625 if (available_memory <= zfs_write_limit_max) {
3626 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3627 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim);
3631 if (inflight_data > available_memory / 4) {
3632 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3633 DMU_TX_STAT_BUMP(dmu_tx_memory_inflight);
3641 arc_tempreserve_clear(uint64_t reserve)
3643 atomic_add_64(&arc_tempreserve, -reserve);
3644 ASSERT((int64_t)arc_tempreserve >= 0);
3648 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3655 * Once in a while, fail for no reason. Everything should cope.
3657 if (spa_get_random(10000) == 0) {
3658 dprintf("forcing random failure\n");
3662 if (reserve > arc_c/4 && !arc_no_grow)
3663 arc_c = MIN(arc_c_max, reserve * 4);
3664 if (reserve > arc_c) {
3665 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve);
3670 * Don't count loaned bufs as in flight dirty data to prevent long
3671 * network delays from blocking transactions that are ready to be
3672 * assigned to a txg.
3674 anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3677 * Writes will, almost always, require additional memory allocations
3678 * in order to compress/encrypt/etc the data. We therefor need to
3679 * make sure that there is sufficient available memory for this.
3681 if ((error = arc_memory_throttle(reserve, anon_size, txg)))
3685 * Throttle writes when the amount of dirty data in the cache
3686 * gets too large. We try to keep the cache less than half full
3687 * of dirty blocks so that our sync times don't grow too large.
3688 * Note: if two requests come in concurrently, we might let them
3689 * both succeed, when one of them should fail. Not a huge deal.
3692 if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3693 anon_size > arc_c / 4) {
3694 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3695 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3696 arc_tempreserve>>10,
3697 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3698 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3699 reserve>>10, arc_c>>10);
3700 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle);
3703 atomic_add_64(&arc_tempreserve, reserve);
3708 arc_kstat_update_state(arc_state_t *state, kstat_named_t *size,
3709 kstat_named_t *evict_data, kstat_named_t *evict_metadata)
3711 size->value.ui64 = state->arcs_size;
3712 evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA];
3713 evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA];
3717 arc_kstat_update(kstat_t *ksp, int rw)
3719 arc_stats_t *as = ksp->ks_data;
3721 if (rw == KSTAT_WRITE) {
3724 arc_kstat_update_state(arc_anon,
3725 &as->arcstat_anon_size,
3726 &as->arcstat_anon_evict_data,
3727 &as->arcstat_anon_evict_metadata);
3728 arc_kstat_update_state(arc_mru,
3729 &as->arcstat_mru_size,
3730 &as->arcstat_mru_evict_data,
3731 &as->arcstat_mru_evict_metadata);
3732 arc_kstat_update_state(arc_mru_ghost,
3733 &as->arcstat_mru_ghost_size,
3734 &as->arcstat_mru_ghost_evict_data,
3735 &as->arcstat_mru_ghost_evict_metadata);
3736 arc_kstat_update_state(arc_mfu,
3737 &as->arcstat_mfu_size,
3738 &as->arcstat_mfu_evict_data,
3739 &as->arcstat_mfu_evict_metadata);
3740 arc_kstat_update_state(arc_mfu_ghost,
3741 &as->arcstat_mfu_ghost_size,
3742 &as->arcstat_mfu_ghost_evict_data,
3743 &as->arcstat_mfu_ghost_evict_metadata);
3752 mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3753 cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3755 /* Convert seconds to clock ticks */
3756 arc_min_prefetch_lifespan = 1 * hz;
3758 /* Start out with 1/8 of all memory */
3759 arc_c = physmem * PAGESIZE / 8;
3763 * On architectures where the physical memory can be larger
3764 * than the addressable space (intel in 32-bit mode), we may
3765 * need to limit the cache to 1/8 of VM size.
3767 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3769 * Register a shrinker to support synchronous (direct) memory
3770 * reclaim from the arc. This is done to prevent kswapd from
3771 * swapping out pages when it is preferable to shrink the arc.
3773 spl_register_shrinker(&arc_shrinker);
3776 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3777 arc_c_min = MAX(arc_c / 4, 64<<20);
3778 /* set max to 1/2 of all memory */
3779 arc_c_max = MAX(arc_c * 4, arc_c_max);
3782 * Allow the tunables to override our calculations if they are
3783 * reasonable (ie. over 64MB)
3785 if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
3786 arc_c_max = zfs_arc_max;
3787 if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
3788 arc_c_min = zfs_arc_min;
3791 arc_p = (arc_c >> 1);
3793 /* limit meta-data to 1/4 of the arc capacity */
3794 arc_meta_limit = arc_c_max / 4;
3797 /* Allow the tunable to override if it is reasonable */
3798 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
3799 arc_meta_limit = zfs_arc_meta_limit;
3801 if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3802 arc_c_min = arc_meta_limit / 2;
3804 if (zfs_arc_grow_retry > 0)
3805 arc_grow_retry = zfs_arc_grow_retry;
3807 if (zfs_arc_shrink_shift > 0)
3808 arc_shrink_shift = zfs_arc_shrink_shift;
3810 if (zfs_arc_p_min_shift > 0)
3811 arc_p_min_shift = zfs_arc_p_min_shift;
3813 if (zfs_arc_meta_prune > 0)
3814 arc_meta_prune = zfs_arc_meta_prune;
3816 /* if kmem_flags are set, lets try to use less memory */
3817 if (kmem_debugging())
3819 if (arc_c < arc_c_min)
3822 arc_anon = &ARC_anon;
3824 arc_mru_ghost = &ARC_mru_ghost;
3826 arc_mfu_ghost = &ARC_mfu_ghost;
3827 arc_l2c_only = &ARC_l2c_only;
3830 mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3831 mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3832 mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3833 mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3834 mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3835 mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3837 list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
3838 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3839 list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
3840 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3841 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
3842 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3843 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
3844 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3845 list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
3846 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3847 list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
3848 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3849 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
3850 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3851 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
3852 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3853 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
3854 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3855 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
3856 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3860 arc_thread_exit = 0;
3861 list_create(&arc_prune_list, sizeof (arc_prune_t),
3862 offsetof(arc_prune_t, p_node));
3863 arc_eviction_list = NULL;
3864 mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL);
3865 mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
3866 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3868 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
3869 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3871 if (arc_ksp != NULL) {
3872 arc_ksp->ks_data = &arc_stats;
3873 arc_ksp->ks_update = arc_kstat_update;
3874 kstat_install(arc_ksp);
3877 (void) thread_create(NULL, 0, arc_adapt_thread, NULL, 0, &p0,
3878 TS_RUN, minclsyspri);
3883 if (zfs_write_limit_max == 0)
3884 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
3886 zfs_write_limit_shift = 0;
3887 mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL);
3895 mutex_enter(&arc_reclaim_thr_lock);
3897 spl_unregister_shrinker(&arc_shrinker);
3898 #endif /* _KERNEL */
3900 arc_thread_exit = 1;
3901 while (arc_thread_exit != 0)
3902 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3903 mutex_exit(&arc_reclaim_thr_lock);
3909 if (arc_ksp != NULL) {
3910 kstat_delete(arc_ksp);
3914 mutex_enter(&arc_prune_mtx);
3915 while ((p = list_head(&arc_prune_list)) != NULL) {
3916 list_remove(&arc_prune_list, p);
3917 refcount_remove(&p->p_refcnt, &arc_prune_list);
3918 refcount_destroy(&p->p_refcnt);
3919 kmem_free(p, sizeof (*p));
3921 mutex_exit(&arc_prune_mtx);
3923 list_destroy(&arc_prune_list);
3924 mutex_destroy(&arc_prune_mtx);
3925 mutex_destroy(&arc_eviction_mtx);
3926 mutex_destroy(&arc_reclaim_thr_lock);
3927 cv_destroy(&arc_reclaim_thr_cv);
3929 list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
3930 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
3931 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
3932 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
3933 list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
3934 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
3935 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
3936 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3938 mutex_destroy(&arc_anon->arcs_mtx);
3939 mutex_destroy(&arc_mru->arcs_mtx);
3940 mutex_destroy(&arc_mru_ghost->arcs_mtx);
3941 mutex_destroy(&arc_mfu->arcs_mtx);
3942 mutex_destroy(&arc_mfu_ghost->arcs_mtx);
3943 mutex_destroy(&arc_l2c_only->arcs_mtx);
3945 mutex_destroy(&zfs_write_limit_lock);
3949 ASSERT(arc_loaned_bytes == 0);
3955 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3956 * It uses dedicated storage devices to hold cached data, which are populated
3957 * using large infrequent writes. The main role of this cache is to boost
3958 * the performance of random read workloads. The intended L2ARC devices
3959 * include short-stroked disks, solid state disks, and other media with
3960 * substantially faster read latency than disk.
3962 * +-----------------------+
3964 * +-----------------------+
3967 * l2arc_feed_thread() arc_read()
3971 * +---------------+ |
3973 * +---------------+ |
3978 * +-------+ +-------+
3980 * | cache | | cache |
3981 * +-------+ +-------+
3982 * +=========+ .-----.
3983 * : L2ARC : |-_____-|
3984 * : devices : | Disks |
3985 * +=========+ `-_____-'
3987 * Read requests are satisfied from the following sources, in order:
3990 * 2) vdev cache of L2ARC devices
3992 * 4) vdev cache of disks
3995 * Some L2ARC device types exhibit extremely slow write performance.
3996 * To accommodate for this there are some significant differences between
3997 * the L2ARC and traditional cache design:
3999 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
4000 * the ARC behave as usual, freeing buffers and placing headers on ghost
4001 * lists. The ARC does not send buffers to the L2ARC during eviction as
4002 * this would add inflated write latencies for all ARC memory pressure.
4004 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
4005 * It does this by periodically scanning buffers from the eviction-end of
4006 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
4007 * not already there. It scans until a headroom of buffers is satisfied,
4008 * which itself is a buffer for ARC eviction. The thread that does this is
4009 * l2arc_feed_thread(), illustrated below; example sizes are included to
4010 * provide a better sense of ratio than this diagram:
4013 * +---------------------+----------+
4014 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
4015 * +---------------------+----------+ | o L2ARC eligible
4016 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
4017 * +---------------------+----------+ |
4018 * 15.9 Gbytes ^ 32 Mbytes |
4020 * l2arc_feed_thread()
4022 * l2arc write hand <--[oooo]--'
4026 * +==============================+
4027 * L2ARC dev |####|#|###|###| |####| ... |
4028 * +==============================+
4031 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
4032 * evicted, then the L2ARC has cached a buffer much sooner than it probably
4033 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
4034 * safe to say that this is an uncommon case, since buffers at the end of
4035 * the ARC lists have moved there due to inactivity.
4037 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
4038 * then the L2ARC simply misses copying some buffers. This serves as a
4039 * pressure valve to prevent heavy read workloads from both stalling the ARC
4040 * with waits and clogging the L2ARC with writes. This also helps prevent
4041 * the potential for the L2ARC to churn if it attempts to cache content too
4042 * quickly, such as during backups of the entire pool.
4044 * 5. After system boot and before the ARC has filled main memory, there are
4045 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
4046 * lists can remain mostly static. Instead of searching from tail of these
4047 * lists as pictured, the l2arc_feed_thread() will search from the list heads
4048 * for eligible buffers, greatly increasing its chance of finding them.
4050 * The L2ARC device write speed is also boosted during this time so that
4051 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
4052 * there are no L2ARC reads, and no fear of degrading read performance
4053 * through increased writes.
4055 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
4056 * the vdev queue can aggregate them into larger and fewer writes. Each
4057 * device is written to in a rotor fashion, sweeping writes through
4058 * available space then repeating.
4060 * 7. The L2ARC does not store dirty content. It never needs to flush
4061 * write buffers back to disk based storage.
4063 * 8. If an ARC buffer is written (and dirtied) which also exists in the
4064 * L2ARC, the now stale L2ARC buffer is immediately dropped.
4066 * The performance of the L2ARC can be tweaked by a number of tunables, which
4067 * may be necessary for different workloads:
4069 * l2arc_write_max max write bytes per interval
4070 * l2arc_write_boost extra write bytes during device warmup
4071 * l2arc_noprefetch skip caching prefetched buffers
4072 * l2arc_headroom number of max device writes to precache
4073 * l2arc_feed_secs seconds between L2ARC writing
4075 * Tunables may be removed or added as future performance improvements are
4076 * integrated, and also may become zpool properties.
4078 * There are three key functions that control how the L2ARC warms up:
4080 * l2arc_write_eligible() check if a buffer is eligible to cache
4081 * l2arc_write_size() calculate how much to write
4082 * l2arc_write_interval() calculate sleep delay between writes
4084 * These three functions determine what to write, how much, and how quickly
4089 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
4092 * A buffer is *not* eligible for the L2ARC if it:
4093 * 1. belongs to a different spa.
4094 * 2. is already cached on the L2ARC.
4095 * 3. has an I/O in progress (it may be an incomplete read).
4096 * 4. is flagged not eligible (zfs property).
4098 if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
4099 HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
4106 l2arc_write_size(l2arc_dev_t *dev)
4110 size = dev->l2ad_write;
4112 if (arc_warm == B_FALSE)
4113 size += dev->l2ad_boost;
4120 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
4122 clock_t interval, next, now;
4125 * If the ARC lists are busy, increase our write rate; if the
4126 * lists are stale, idle back. This is achieved by checking
4127 * how much we previously wrote - if it was more than half of
4128 * what we wanted, schedule the next write much sooner.
4130 if (l2arc_feed_again && wrote > (wanted / 2))
4131 interval = (hz * l2arc_feed_min_ms) / 1000;
4133 interval = hz * l2arc_feed_secs;
4135 now = ddi_get_lbolt();
4136 next = MAX(now, MIN(now + interval, began + interval));
4142 l2arc_hdr_stat_add(void)
4144 ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
4145 ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
4149 l2arc_hdr_stat_remove(void)
4151 ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
4152 ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
4156 * Cycle through L2ARC devices. This is how L2ARC load balances.
4157 * If a device is returned, this also returns holding the spa config lock.
4159 static l2arc_dev_t *
4160 l2arc_dev_get_next(void)
4162 l2arc_dev_t *first, *next = NULL;
4165 * Lock out the removal of spas (spa_namespace_lock), then removal
4166 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
4167 * both locks will be dropped and a spa config lock held instead.
4169 mutex_enter(&spa_namespace_lock);
4170 mutex_enter(&l2arc_dev_mtx);
4172 /* if there are no vdevs, there is nothing to do */
4173 if (l2arc_ndev == 0)
4177 next = l2arc_dev_last;
4179 /* loop around the list looking for a non-faulted vdev */
4181 next = list_head(l2arc_dev_list);
4183 next = list_next(l2arc_dev_list, next);
4185 next = list_head(l2arc_dev_list);
4188 /* if we have come back to the start, bail out */
4191 else if (next == first)
4194 } while (vdev_is_dead(next->l2ad_vdev));
4196 /* if we were unable to find any usable vdevs, return NULL */
4197 if (vdev_is_dead(next->l2ad_vdev))
4200 l2arc_dev_last = next;
4203 mutex_exit(&l2arc_dev_mtx);
4206 * Grab the config lock to prevent the 'next' device from being
4207 * removed while we are writing to it.
4210 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
4211 mutex_exit(&spa_namespace_lock);
4217 * Free buffers that were tagged for destruction.
4220 l2arc_do_free_on_write(void)
4223 l2arc_data_free_t *df, *df_prev;
4225 mutex_enter(&l2arc_free_on_write_mtx);
4226 buflist = l2arc_free_on_write;
4228 for (df = list_tail(buflist); df; df = df_prev) {
4229 df_prev = list_prev(buflist, df);
4230 ASSERT(df->l2df_data != NULL);
4231 ASSERT(df->l2df_func != NULL);
4232 df->l2df_func(df->l2df_data, df->l2df_size);
4233 list_remove(buflist, df);
4234 kmem_free(df, sizeof (l2arc_data_free_t));
4237 mutex_exit(&l2arc_free_on_write_mtx);
4241 * A write to a cache device has completed. Update all headers to allow
4242 * reads from these buffers to begin.
4245 l2arc_write_done(zio_t *zio)
4247 l2arc_write_callback_t *cb;
4250 arc_buf_hdr_t *head, *ab, *ab_prev;
4251 l2arc_buf_hdr_t *abl2;
4252 kmutex_t *hash_lock;
4254 cb = zio->io_private;
4256 dev = cb->l2wcb_dev;
4257 ASSERT(dev != NULL);
4258 head = cb->l2wcb_head;
4259 ASSERT(head != NULL);
4260 buflist = dev->l2ad_buflist;
4261 ASSERT(buflist != NULL);
4262 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
4263 l2arc_write_callback_t *, cb);
4265 if (zio->io_error != 0)
4266 ARCSTAT_BUMP(arcstat_l2_writes_error);
4268 mutex_enter(&l2arc_buflist_mtx);
4271 * All writes completed, or an error was hit.
4273 for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
4274 ab_prev = list_prev(buflist, ab);
4276 hash_lock = HDR_LOCK(ab);
4277 if (!mutex_tryenter(hash_lock)) {
4279 * This buffer misses out. It may be in a stage
4280 * of eviction. Its ARC_L2_WRITING flag will be
4281 * left set, denying reads to this buffer.
4283 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
4287 if (zio->io_error != 0) {
4289 * Error - drop L2ARC entry.
4291 list_remove(buflist, ab);
4294 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4295 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4299 * Allow ARC to begin reads to this L2ARC entry.
4301 ab->b_flags &= ~ARC_L2_WRITING;
4303 mutex_exit(hash_lock);
4306 atomic_inc_64(&l2arc_writes_done);
4307 list_remove(buflist, head);
4308 kmem_cache_free(hdr_cache, head);
4309 mutex_exit(&l2arc_buflist_mtx);
4311 l2arc_do_free_on_write();
4313 kmem_free(cb, sizeof (l2arc_write_callback_t));
4317 * A read to a cache device completed. Validate buffer contents before
4318 * handing over to the regular ARC routines.
4321 l2arc_read_done(zio_t *zio)
4323 l2arc_read_callback_t *cb;
4326 kmutex_t *hash_lock;
4329 ASSERT(zio->io_vd != NULL);
4330 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
4332 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
4334 cb = zio->io_private;
4336 buf = cb->l2rcb_buf;
4337 ASSERT(buf != NULL);
4339 hash_lock = HDR_LOCK(buf->b_hdr);
4340 mutex_enter(hash_lock);
4342 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4345 * Check this survived the L2ARC journey.
4347 equal = arc_cksum_equal(buf);
4348 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4349 mutex_exit(hash_lock);
4350 zio->io_private = buf;
4351 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
4352 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
4355 mutex_exit(hash_lock);
4357 * Buffer didn't survive caching. Increment stats and
4358 * reissue to the original storage device.
4360 if (zio->io_error != 0) {
4361 ARCSTAT_BUMP(arcstat_l2_io_error);
4363 zio->io_error = EIO;
4366 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4369 * If there's no waiter, issue an async i/o to the primary
4370 * storage now. If there *is* a waiter, the caller must
4371 * issue the i/o in a context where it's OK to block.
4373 if (zio->io_waiter == NULL) {
4374 zio_t *pio = zio_unique_parent(zio);
4376 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4378 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4379 buf->b_data, zio->io_size, arc_read_done, buf,
4380 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4384 kmem_free(cb, sizeof (l2arc_read_callback_t));
4388 * This is the list priority from which the L2ARC will search for pages to
4389 * cache. This is used within loops (0..3) to cycle through lists in the
4390 * desired order. This order can have a significant effect on cache
4393 * Currently the metadata lists are hit first, MFU then MRU, followed by
4394 * the data lists. This function returns a locked list, and also returns
4398 l2arc_list_locked(int list_num, kmutex_t **lock)
4400 list_t *list = NULL;
4402 ASSERT(list_num >= 0 && list_num <= 3);
4406 list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
4407 *lock = &arc_mfu->arcs_mtx;
4410 list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
4411 *lock = &arc_mru->arcs_mtx;
4414 list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
4415 *lock = &arc_mfu->arcs_mtx;
4418 list = &arc_mru->arcs_list[ARC_BUFC_DATA];
4419 *lock = &arc_mru->arcs_mtx;
4423 ASSERT(!(MUTEX_HELD(*lock)));
4429 * Evict buffers from the device write hand to the distance specified in
4430 * bytes. This distance may span populated buffers, it may span nothing.
4431 * This is clearing a region on the L2ARC device ready for writing.
4432 * If the 'all' boolean is set, every buffer is evicted.
4435 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4438 l2arc_buf_hdr_t *abl2;
4439 arc_buf_hdr_t *ab, *ab_prev;
4440 kmutex_t *hash_lock;
4443 buflist = dev->l2ad_buflist;
4445 if (buflist == NULL)
4448 if (!all && dev->l2ad_first) {
4450 * This is the first sweep through the device. There is
4456 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4458 * When nearing the end of the device, evict to the end
4459 * before the device write hand jumps to the start.
4461 taddr = dev->l2ad_end;
4463 taddr = dev->l2ad_hand + distance;
4465 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4466 uint64_t, taddr, boolean_t, all);
4469 mutex_enter(&l2arc_buflist_mtx);
4470 for (ab = list_tail(buflist); ab; ab = ab_prev) {
4471 ab_prev = list_prev(buflist, ab);
4473 hash_lock = HDR_LOCK(ab);
4474 if (!mutex_tryenter(hash_lock)) {
4476 * Missed the hash lock. Retry.
4478 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4479 mutex_exit(&l2arc_buflist_mtx);
4480 mutex_enter(hash_lock);
4481 mutex_exit(hash_lock);
4485 if (HDR_L2_WRITE_HEAD(ab)) {
4487 * We hit a write head node. Leave it for
4488 * l2arc_write_done().
4490 list_remove(buflist, ab);
4491 mutex_exit(hash_lock);
4495 if (!all && ab->b_l2hdr != NULL &&
4496 (ab->b_l2hdr->b_daddr > taddr ||
4497 ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4499 * We've evicted to the target address,
4500 * or the end of the device.
4502 mutex_exit(hash_lock);
4506 if (HDR_FREE_IN_PROGRESS(ab)) {
4508 * Already on the path to destruction.
4510 mutex_exit(hash_lock);
4514 if (ab->b_state == arc_l2c_only) {
4515 ASSERT(!HDR_L2_READING(ab));
4517 * This doesn't exist in the ARC. Destroy.
4518 * arc_hdr_destroy() will call list_remove()
4519 * and decrement arcstat_l2_size.
4521 arc_change_state(arc_anon, ab, hash_lock);
4522 arc_hdr_destroy(ab);
4525 * Invalidate issued or about to be issued
4526 * reads, since we may be about to write
4527 * over this location.
4529 if (HDR_L2_READING(ab)) {
4530 ARCSTAT_BUMP(arcstat_l2_evict_reading);
4531 ab->b_flags |= ARC_L2_EVICTED;
4535 * Tell ARC this no longer exists in L2ARC.
4537 if (ab->b_l2hdr != NULL) {
4540 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4541 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4543 list_remove(buflist, ab);
4546 * This may have been leftover after a
4549 ab->b_flags &= ~ARC_L2_WRITING;
4551 mutex_exit(hash_lock);
4553 mutex_exit(&l2arc_buflist_mtx);
4555 vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
4556 dev->l2ad_evict = taddr;
4560 * Find and write ARC buffers to the L2ARC device.
4562 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4563 * for reading until they have completed writing.
4566 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
4568 arc_buf_hdr_t *ab, *ab_prev, *head;
4569 l2arc_buf_hdr_t *hdrl2;
4571 uint64_t passed_sz, write_sz, buf_sz, headroom;
4573 kmutex_t *hash_lock, *list_lock = NULL;
4574 boolean_t have_lock, full;
4575 l2arc_write_callback_t *cb;
4577 uint64_t guid = spa_load_guid(spa);
4580 ASSERT(dev->l2ad_vdev != NULL);
4585 head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4586 head->b_flags |= ARC_L2_WRITE_HEAD;
4589 * Copy buffers for L2ARC writing.
4591 mutex_enter(&l2arc_buflist_mtx);
4592 for (try = 0; try <= 3; try++) {
4593 list = l2arc_list_locked(try, &list_lock);
4597 * L2ARC fast warmup.
4599 * Until the ARC is warm and starts to evict, read from the
4600 * head of the ARC lists rather than the tail.
4602 headroom = target_sz * l2arc_headroom;
4603 if (arc_warm == B_FALSE)
4604 ab = list_head(list);
4606 ab = list_tail(list);
4608 for (; ab; ab = ab_prev) {
4609 if (arc_warm == B_FALSE)
4610 ab_prev = list_next(list, ab);
4612 ab_prev = list_prev(list, ab);
4614 hash_lock = HDR_LOCK(ab);
4615 have_lock = MUTEX_HELD(hash_lock);
4616 if (!have_lock && !mutex_tryenter(hash_lock)) {
4618 * Skip this buffer rather than waiting.
4623 passed_sz += ab->b_size;
4624 if (passed_sz > headroom) {
4628 mutex_exit(hash_lock);
4632 if (!l2arc_write_eligible(guid, ab)) {
4633 mutex_exit(hash_lock);
4637 if ((write_sz + ab->b_size) > target_sz) {
4639 mutex_exit(hash_lock);
4645 * Insert a dummy header on the buflist so
4646 * l2arc_write_done() can find where the
4647 * write buffers begin without searching.
4649 list_insert_head(dev->l2ad_buflist, head);
4651 cb = kmem_alloc(sizeof (l2arc_write_callback_t),
4653 cb->l2wcb_dev = dev;
4654 cb->l2wcb_head = head;
4655 pio = zio_root(spa, l2arc_write_done, cb,
4660 * Create and add a new L2ARC header.
4662 hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t),
4665 hdrl2->b_daddr = dev->l2ad_hand;
4667 ab->b_flags |= ARC_L2_WRITING;
4668 ab->b_l2hdr = hdrl2;
4669 list_insert_head(dev->l2ad_buflist, ab);
4670 buf_data = ab->b_buf->b_data;
4671 buf_sz = ab->b_size;
4674 * Compute and store the buffer cksum before
4675 * writing. On debug the cksum is verified first.
4677 arc_cksum_verify(ab->b_buf);
4678 arc_cksum_compute(ab->b_buf, B_TRUE);
4680 mutex_exit(hash_lock);
4682 wzio = zio_write_phys(pio, dev->l2ad_vdev,
4683 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
4684 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
4685 ZIO_FLAG_CANFAIL, B_FALSE);
4687 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
4689 (void) zio_nowait(wzio);
4692 * Keep the clock hand suitably device-aligned.
4694 buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
4697 dev->l2ad_hand += buf_sz;
4700 mutex_exit(list_lock);
4705 mutex_exit(&l2arc_buflist_mtx);
4709 kmem_cache_free(hdr_cache, head);
4713 ASSERT3U(write_sz, <=, target_sz);
4714 ARCSTAT_BUMP(arcstat_l2_writes_sent);
4715 ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz);
4716 ARCSTAT_INCR(arcstat_l2_size, write_sz);
4717 vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0);
4720 * Bump device hand to the device start if it is approaching the end.
4721 * l2arc_evict() will already have evicted ahead for this case.
4723 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
4724 vdev_space_update(dev->l2ad_vdev,
4725 dev->l2ad_end - dev->l2ad_hand, 0, 0);
4726 dev->l2ad_hand = dev->l2ad_start;
4727 dev->l2ad_evict = dev->l2ad_start;
4728 dev->l2ad_first = B_FALSE;
4731 dev->l2ad_writing = B_TRUE;
4732 (void) zio_wait(pio);
4733 dev->l2ad_writing = B_FALSE;
4739 * This thread feeds the L2ARC at regular intervals. This is the beating
4740 * heart of the L2ARC.
4743 l2arc_feed_thread(void)
4748 uint64_t size, wrote;
4749 clock_t begin, next = ddi_get_lbolt();
4751 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
4753 mutex_enter(&l2arc_feed_thr_lock);
4755 while (l2arc_thread_exit == 0) {
4756 CALLB_CPR_SAFE_BEGIN(&cpr);
4757 (void) cv_timedwait_interruptible(&l2arc_feed_thr_cv,
4758 &l2arc_feed_thr_lock, next);
4759 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
4760 next = ddi_get_lbolt() + hz;
4763 * Quick check for L2ARC devices.
4765 mutex_enter(&l2arc_dev_mtx);
4766 if (l2arc_ndev == 0) {
4767 mutex_exit(&l2arc_dev_mtx);
4770 mutex_exit(&l2arc_dev_mtx);
4771 begin = ddi_get_lbolt();
4774 * This selects the next l2arc device to write to, and in
4775 * doing so the next spa to feed from: dev->l2ad_spa. This
4776 * will return NULL if there are now no l2arc devices or if
4777 * they are all faulted.
4779 * If a device is returned, its spa's config lock is also
4780 * held to prevent device removal. l2arc_dev_get_next()
4781 * will grab and release l2arc_dev_mtx.
4783 if ((dev = l2arc_dev_get_next()) == NULL)
4786 spa = dev->l2ad_spa;
4787 ASSERT(spa != NULL);
4790 * If the pool is read-only then force the feed thread to
4791 * sleep a little longer.
4793 if (!spa_writeable(spa)) {
4794 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
4795 spa_config_exit(spa, SCL_L2ARC, dev);
4800 * Avoid contributing to memory pressure.
4803 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
4804 spa_config_exit(spa, SCL_L2ARC, dev);
4808 ARCSTAT_BUMP(arcstat_l2_feeds);
4810 size = l2arc_write_size(dev);
4813 * Evict L2ARC buffers that will be overwritten.
4815 l2arc_evict(dev, size, B_FALSE);
4818 * Write ARC buffers.
4820 wrote = l2arc_write_buffers(spa, dev, size);
4823 * Calculate interval between writes.
4825 next = l2arc_write_interval(begin, size, wrote);
4826 spa_config_exit(spa, SCL_L2ARC, dev);
4829 l2arc_thread_exit = 0;
4830 cv_broadcast(&l2arc_feed_thr_cv);
4831 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
4836 l2arc_vdev_present(vdev_t *vd)
4840 mutex_enter(&l2arc_dev_mtx);
4841 for (dev = list_head(l2arc_dev_list); dev != NULL;
4842 dev = list_next(l2arc_dev_list, dev)) {
4843 if (dev->l2ad_vdev == vd)
4846 mutex_exit(&l2arc_dev_mtx);
4848 return (dev != NULL);
4852 * Add a vdev for use by the L2ARC. By this point the spa has already
4853 * validated the vdev and opened it.
4856 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
4858 l2arc_dev_t *adddev;
4860 ASSERT(!l2arc_vdev_present(vd));
4863 * Create a new l2arc device entry.
4865 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
4866 adddev->l2ad_spa = spa;
4867 adddev->l2ad_vdev = vd;
4868 adddev->l2ad_write = l2arc_write_max;
4869 adddev->l2ad_boost = l2arc_write_boost;
4870 adddev->l2ad_start = VDEV_LABEL_START_SIZE;
4871 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
4872 adddev->l2ad_hand = adddev->l2ad_start;
4873 adddev->l2ad_evict = adddev->l2ad_start;
4874 adddev->l2ad_first = B_TRUE;
4875 adddev->l2ad_writing = B_FALSE;
4876 list_link_init(&adddev->l2ad_node);
4877 ASSERT3U(adddev->l2ad_write, >, 0);
4880 * This is a list of all ARC buffers that are still valid on the
4883 adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
4884 list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
4885 offsetof(arc_buf_hdr_t, b_l2node));
4887 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
4890 * Add device to global list
4892 mutex_enter(&l2arc_dev_mtx);
4893 list_insert_head(l2arc_dev_list, adddev);
4894 atomic_inc_64(&l2arc_ndev);
4895 mutex_exit(&l2arc_dev_mtx);
4899 * Remove a vdev from the L2ARC.
4902 l2arc_remove_vdev(vdev_t *vd)
4904 l2arc_dev_t *dev, *nextdev, *remdev = NULL;
4907 * Find the device by vdev
4909 mutex_enter(&l2arc_dev_mtx);
4910 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
4911 nextdev = list_next(l2arc_dev_list, dev);
4912 if (vd == dev->l2ad_vdev) {
4917 ASSERT(remdev != NULL);
4920 * Remove device from global list
4922 list_remove(l2arc_dev_list, remdev);
4923 l2arc_dev_last = NULL; /* may have been invalidated */
4924 atomic_dec_64(&l2arc_ndev);
4925 mutex_exit(&l2arc_dev_mtx);
4928 * Clear all buflists and ARC references. L2ARC device flush.
4930 l2arc_evict(remdev, 0, B_TRUE);
4931 list_destroy(remdev->l2ad_buflist);
4932 kmem_free(remdev->l2ad_buflist, sizeof (list_t));
4933 kmem_free(remdev, sizeof (l2arc_dev_t));
4939 l2arc_thread_exit = 0;
4941 l2arc_writes_sent = 0;
4942 l2arc_writes_done = 0;
4944 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
4945 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
4946 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
4947 mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
4948 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
4950 l2arc_dev_list = &L2ARC_dev_list;
4951 l2arc_free_on_write = &L2ARC_free_on_write;
4952 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
4953 offsetof(l2arc_dev_t, l2ad_node));
4954 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
4955 offsetof(l2arc_data_free_t, l2df_list_node));
4962 * This is called from dmu_fini(), which is called from spa_fini();
4963 * Because of this, we can assume that all l2arc devices have
4964 * already been removed when the pools themselves were removed.
4967 l2arc_do_free_on_write();
4969 mutex_destroy(&l2arc_feed_thr_lock);
4970 cv_destroy(&l2arc_feed_thr_cv);
4971 mutex_destroy(&l2arc_dev_mtx);
4972 mutex_destroy(&l2arc_buflist_mtx);
4973 mutex_destroy(&l2arc_free_on_write_mtx);
4975 list_destroy(l2arc_dev_list);
4976 list_destroy(l2arc_free_on_write);
4982 if (!(spa_mode_global & FWRITE))
4985 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
4986 TS_RUN, minclsyspri);
4992 if (!(spa_mode_global & FWRITE))
4995 mutex_enter(&l2arc_feed_thr_lock);
4996 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
4997 l2arc_thread_exit = 1;
4998 while (l2arc_thread_exit != 0)
4999 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
5000 mutex_exit(&l2arc_feed_thr_lock);
5003 #if defined(_KERNEL) && defined(HAVE_SPL)
5004 EXPORT_SYMBOL(arc_read);
5005 EXPORT_SYMBOL(arc_buf_remove_ref);
5006 EXPORT_SYMBOL(arc_getbuf_func);
5007 EXPORT_SYMBOL(arc_add_prune_callback);
5008 EXPORT_SYMBOL(arc_remove_prune_callback);
5010 module_param(zfs_arc_min, ulong, 0444);
5011 MODULE_PARM_DESC(zfs_arc_min, "Min arc size");
5013 module_param(zfs_arc_max, ulong, 0444);
5014 MODULE_PARM_DESC(zfs_arc_max, "Max arc size");
5016 module_param(zfs_arc_meta_limit, ulong, 0444);
5017 MODULE_PARM_DESC(zfs_arc_meta_limit, "Meta limit for arc size");
5019 module_param(zfs_arc_meta_prune, int, 0444);
5020 MODULE_PARM_DESC(zfs_arc_meta_prune, "Bytes of meta data to prune");
5022 module_param(zfs_arc_grow_retry, int, 0444);
5023 MODULE_PARM_DESC(zfs_arc_grow_retry, "Seconds before growing arc size");
5025 module_param(zfs_arc_shrink_shift, int, 0444);
5026 MODULE_PARM_DESC(zfs_arc_shrink_shift, "log2(fraction of arc to reclaim)");
5028 module_param(zfs_arc_p_min_shift, int, 0444);
5029 MODULE_PARM_DESC(zfs_arc_p_min_shift, "arc_c shift to calc min/max arc_p");
5031 module_param(zfs_disable_dup_eviction, int, 0644);
5032 MODULE_PARM_DESC(zfs_disable_dup_eviction, "disable duplicate buffer eviction");
5034 module_param(zfs_arc_memory_throttle_disable, int, 0644);
5035 MODULE_PARM_DESC(zfs_arc_memory_throttle_disable, "disable memory throttle");
5037 module_param(l2arc_write_max, ulong, 0444);
5038 MODULE_PARM_DESC(l2arc_write_max, "Max write bytes per interval");
5040 module_param(l2arc_write_boost, ulong, 0444);
5041 MODULE_PARM_DESC(l2arc_write_boost, "Extra write bytes during device warmup");
5043 module_param(l2arc_headroom, ulong, 0444);
5044 MODULE_PARM_DESC(l2arc_headroom, "Number of max device writes to precache");
5046 module_param(l2arc_feed_secs, ulong, 0444);
5047 MODULE_PARM_DESC(l2arc_feed_secs, "Seconds between L2ARC writing");
5049 module_param(l2arc_feed_min_ms, ulong, 0444);
5050 MODULE_PARM_DESC(l2arc_feed_min_ms, "Min feed interval in milliseconds");
5052 module_param(l2arc_noprefetch, int, 0444);
5053 MODULE_PARM_DESC(l2arc_noprefetch, "Skip caching prefetched buffers");
5055 module_param(l2arc_feed_again, int, 0444);
5056 MODULE_PARM_DESC(l2arc_feed_again, "Turbo L2ARC warmup");
5058 module_param(l2arc_norw, int, 0444);
5059 MODULE_PARM_DESC(l2arc_norw, "No reads during writes");