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.
26 * DVA-based Adjustable Replacement Cache
28 * While much of the theory of operation used here is
29 * based on the self-tuning, low overhead replacement cache
30 * presented by Megiddo and Modha at FAST 2003, there are some
31 * significant differences:
33 * 1. The Megiddo and Modha model assumes any page is evictable.
34 * Pages in its cache cannot be "locked" into memory. This makes
35 * the eviction algorithm simple: evict the last page in the list.
36 * This also make the performance characteristics easy to reason
37 * about. Our cache is not so simple. At any given moment, some
38 * subset of the blocks in the cache are un-evictable because we
39 * have handed out a reference to them. Blocks are only evictable
40 * when there are no external references active. This makes
41 * eviction far more problematic: we choose to evict the evictable
42 * blocks that are the "lowest" in the list.
44 * There are times when it is not possible to evict the requested
45 * space. In these circumstances we are unable to adjust the cache
46 * size. To prevent the cache growing unbounded at these times we
47 * implement a "cache throttle" that slows the flow of new data
48 * into the cache until we can make space available.
50 * 2. The Megiddo and Modha model assumes a fixed cache size.
51 * Pages are evicted when the cache is full and there is a cache
52 * miss. Our model has a variable sized cache. It grows with
53 * high use, but also tries to react to memory pressure from the
54 * operating system: decreasing its size when system memory is
57 * 3. The Megiddo and Modha model assumes a fixed page size. All
58 * elements of the cache are therefor exactly the same size. So
59 * when adjusting the cache size following a cache miss, its simply
60 * a matter of choosing a single page to evict. In our model, we
61 * have variable sized cache blocks (rangeing from 512 bytes to
62 * 128K bytes). We therefor choose a set of blocks to evict to make
63 * space for a cache miss that approximates as closely as possible
64 * the space used by the new block.
66 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
67 * by N. Megiddo & D. Modha, FAST 2003
73 * A new reference to a cache buffer can be obtained in two
74 * ways: 1) via a hash table lookup using the DVA as a key,
75 * or 2) via one of the ARC lists. The arc_read() interface
76 * uses method 1, while the internal arc algorithms for
77 * adjusting the cache use method 2. We therefor provide two
78 * types of locks: 1) the hash table lock array, and 2) the
81 * Buffers do not have their own mutexs, rather they rely on the
82 * hash table mutexs for the bulk of their protection (i.e. most
83 * fields in the arc_buf_hdr_t are protected by these mutexs).
85 * buf_hash_find() returns the appropriate mutex (held) when it
86 * locates the requested buffer in the hash table. It returns
87 * NULL for the mutex if the buffer was not in the table.
89 * buf_hash_remove() expects the appropriate hash mutex to be
90 * already held before it is invoked.
92 * Each arc state also has a mutex which is used to protect the
93 * buffer list associated with the state. When attempting to
94 * obtain a hash table lock while holding an arc list lock you
95 * must use: mutex_tryenter() to avoid deadlock. Also note that
96 * the active state mutex must be held before the ghost state mutex.
98 * Arc buffers may have an associated eviction callback function.
99 * This function will be invoked prior to removing the buffer (e.g.
100 * in arc_do_user_evicts()). Note however that the data associated
101 * with the buffer may be evicted prior to the callback. The callback
102 * must be made with *no locks held* (to prevent deadlock). Additionally,
103 * the users of callbacks must ensure that their private data is
104 * protected from simultaneous callbacks from arc_buf_evict()
105 * and arc_do_user_evicts().
107 * Note that the majority of the performance stats are manipulated
108 * with atomic operations.
110 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
112 * - L2ARC buflist creation
113 * - L2ARC buflist eviction
114 * - L2ARC write completion, which walks L2ARC buflists
115 * - ARC header destruction, as it removes from L2ARC buflists
116 * - ARC header release, as it removes from L2ARC buflists
121 #include <sys/zfs_context.h>
123 #include <sys/refcount.h>
124 #include <sys/vdev.h>
125 #include <sys/vdev_impl.h>
127 #include <sys/vmsystm.h>
129 #include <sys/fs/swapnode.h>
130 #include <sys/dnlc.h>
132 #include <sys/callb.h>
133 #include <sys/kstat.h>
134 #include <zfs_fletcher.h>
136 static kmutex_t arc_reclaim_thr_lock;
137 static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */
138 static uint8_t arc_thread_exit;
140 extern int zfs_write_limit_shift;
141 extern uint64_t zfs_write_limit_max;
142 extern kmutex_t zfs_write_limit_lock;
144 #define ARC_REDUCE_DNLC_PERCENT 3
145 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
147 typedef enum arc_reclaim_strategy {
148 ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */
149 ARC_RECLAIM_CONS /* Conservative reclaim strategy */
150 } arc_reclaim_strategy_t;
152 /* number of seconds before growing cache again */
153 static int arc_grow_retry = 60;
155 /* shift of arc_c for calculating both min and max arc_p */
156 static int arc_p_min_shift = 4;
158 /* log2(fraction of arc to reclaim) */
159 static int arc_shrink_shift = 5;
162 * minimum lifespan of a prefetch block in clock ticks
163 * (initialized in arc_init())
165 static int arc_min_prefetch_lifespan;
170 * The arc has filled available memory and has now warmed up.
172 static boolean_t arc_warm;
175 * These tunables are for performance analysis.
177 uint64_t zfs_arc_max;
178 uint64_t zfs_arc_min;
179 uint64_t zfs_arc_meta_limit = 0;
180 int zfs_arc_grow_retry = 0;
181 int zfs_arc_shrink_shift = 0;
182 int zfs_arc_p_min_shift = 0;
185 * Note that buffers can be in one of 6 states:
186 * ARC_anon - anonymous (discussed below)
187 * ARC_mru - recently used, currently cached
188 * ARC_mru_ghost - recentely used, no longer in cache
189 * ARC_mfu - frequently used, currently cached
190 * ARC_mfu_ghost - frequently used, no longer in cache
191 * ARC_l2c_only - exists in L2ARC but not other states
192 * When there are no active references to the buffer, they are
193 * are linked onto a list in one of these arc states. These are
194 * the only buffers that can be evicted or deleted. Within each
195 * state there are multiple lists, one for meta-data and one for
196 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
197 * etc.) is tracked separately so that it can be managed more
198 * explicitly: favored over data, limited explicitly.
200 * Anonymous buffers are buffers that are not associated with
201 * a DVA. These are buffers that hold dirty block copies
202 * before they are written to stable storage. By definition,
203 * they are "ref'd" and are considered part of arc_mru
204 * that cannot be freed. Generally, they will aquire a DVA
205 * as they are written and migrate onto the arc_mru list.
207 * The ARC_l2c_only state is for buffers that are in the second
208 * level ARC but no longer in any of the ARC_m* lists. The second
209 * level ARC itself may also contain buffers that are in any of
210 * the ARC_m* states - meaning that a buffer can exist in two
211 * places. The reason for the ARC_l2c_only state is to keep the
212 * buffer header in the hash table, so that reads that hit the
213 * second level ARC benefit from these fast lookups.
216 typedef struct arc_state {
217 list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */
218 uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */
219 uint64_t arcs_size; /* total amount of data in this state */
224 static arc_state_t ARC_anon;
225 static arc_state_t ARC_mru;
226 static arc_state_t ARC_mru_ghost;
227 static arc_state_t ARC_mfu;
228 static arc_state_t ARC_mfu_ghost;
229 static arc_state_t ARC_l2c_only;
231 typedef struct arc_stats {
232 kstat_named_t arcstat_hits;
233 kstat_named_t arcstat_misses;
234 kstat_named_t arcstat_demand_data_hits;
235 kstat_named_t arcstat_demand_data_misses;
236 kstat_named_t arcstat_demand_metadata_hits;
237 kstat_named_t arcstat_demand_metadata_misses;
238 kstat_named_t arcstat_prefetch_data_hits;
239 kstat_named_t arcstat_prefetch_data_misses;
240 kstat_named_t arcstat_prefetch_metadata_hits;
241 kstat_named_t arcstat_prefetch_metadata_misses;
242 kstat_named_t arcstat_mru_hits;
243 kstat_named_t arcstat_mru_ghost_hits;
244 kstat_named_t arcstat_mfu_hits;
245 kstat_named_t arcstat_mfu_ghost_hits;
246 kstat_named_t arcstat_deleted;
247 kstat_named_t arcstat_recycle_miss;
248 kstat_named_t arcstat_mutex_miss;
249 kstat_named_t arcstat_evict_skip;
250 kstat_named_t arcstat_evict_l2_cached;
251 kstat_named_t arcstat_evict_l2_eligible;
252 kstat_named_t arcstat_evict_l2_ineligible;
253 kstat_named_t arcstat_hash_elements;
254 kstat_named_t arcstat_hash_elements_max;
255 kstat_named_t arcstat_hash_collisions;
256 kstat_named_t arcstat_hash_chains;
257 kstat_named_t arcstat_hash_chain_max;
258 kstat_named_t arcstat_p;
259 kstat_named_t arcstat_c;
260 kstat_named_t arcstat_c_min;
261 kstat_named_t arcstat_c_max;
262 kstat_named_t arcstat_size;
263 kstat_named_t arcstat_hdr_size;
264 kstat_named_t arcstat_data_size;
265 kstat_named_t arcstat_other_size;
266 kstat_named_t arcstat_l2_hits;
267 kstat_named_t arcstat_l2_misses;
268 kstat_named_t arcstat_l2_feeds;
269 kstat_named_t arcstat_l2_rw_clash;
270 kstat_named_t arcstat_l2_read_bytes;
271 kstat_named_t arcstat_l2_write_bytes;
272 kstat_named_t arcstat_l2_writes_sent;
273 kstat_named_t arcstat_l2_writes_done;
274 kstat_named_t arcstat_l2_writes_error;
275 kstat_named_t arcstat_l2_writes_hdr_miss;
276 kstat_named_t arcstat_l2_evict_lock_retry;
277 kstat_named_t arcstat_l2_evict_reading;
278 kstat_named_t arcstat_l2_free_on_write;
279 kstat_named_t arcstat_l2_abort_lowmem;
280 kstat_named_t arcstat_l2_cksum_bad;
281 kstat_named_t arcstat_l2_io_error;
282 kstat_named_t arcstat_l2_size;
283 kstat_named_t arcstat_l2_hdr_size;
284 kstat_named_t arcstat_memory_throttle_count;
287 static arc_stats_t arc_stats = {
288 { "hits", KSTAT_DATA_UINT64 },
289 { "misses", KSTAT_DATA_UINT64 },
290 { "demand_data_hits", KSTAT_DATA_UINT64 },
291 { "demand_data_misses", KSTAT_DATA_UINT64 },
292 { "demand_metadata_hits", KSTAT_DATA_UINT64 },
293 { "demand_metadata_misses", KSTAT_DATA_UINT64 },
294 { "prefetch_data_hits", KSTAT_DATA_UINT64 },
295 { "prefetch_data_misses", KSTAT_DATA_UINT64 },
296 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
297 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
298 { "mru_hits", KSTAT_DATA_UINT64 },
299 { "mru_ghost_hits", KSTAT_DATA_UINT64 },
300 { "mfu_hits", KSTAT_DATA_UINT64 },
301 { "mfu_ghost_hits", KSTAT_DATA_UINT64 },
302 { "deleted", KSTAT_DATA_UINT64 },
303 { "recycle_miss", KSTAT_DATA_UINT64 },
304 { "mutex_miss", KSTAT_DATA_UINT64 },
305 { "evict_skip", KSTAT_DATA_UINT64 },
306 { "evict_l2_cached", KSTAT_DATA_UINT64 },
307 { "evict_l2_eligible", KSTAT_DATA_UINT64 },
308 { "evict_l2_ineligible", KSTAT_DATA_UINT64 },
309 { "hash_elements", KSTAT_DATA_UINT64 },
310 { "hash_elements_max", KSTAT_DATA_UINT64 },
311 { "hash_collisions", KSTAT_DATA_UINT64 },
312 { "hash_chains", KSTAT_DATA_UINT64 },
313 { "hash_chain_max", KSTAT_DATA_UINT64 },
314 { "p", KSTAT_DATA_UINT64 },
315 { "c", KSTAT_DATA_UINT64 },
316 { "c_min", KSTAT_DATA_UINT64 },
317 { "c_max", KSTAT_DATA_UINT64 },
318 { "size", KSTAT_DATA_UINT64 },
319 { "hdr_size", KSTAT_DATA_UINT64 },
320 { "data_size", KSTAT_DATA_UINT64 },
321 { "other_size", KSTAT_DATA_UINT64 },
322 { "l2_hits", KSTAT_DATA_UINT64 },
323 { "l2_misses", KSTAT_DATA_UINT64 },
324 { "l2_feeds", KSTAT_DATA_UINT64 },
325 { "l2_rw_clash", KSTAT_DATA_UINT64 },
326 { "l2_read_bytes", KSTAT_DATA_UINT64 },
327 { "l2_write_bytes", KSTAT_DATA_UINT64 },
328 { "l2_writes_sent", KSTAT_DATA_UINT64 },
329 { "l2_writes_done", KSTAT_DATA_UINT64 },
330 { "l2_writes_error", KSTAT_DATA_UINT64 },
331 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 },
332 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
333 { "l2_evict_reading", KSTAT_DATA_UINT64 },
334 { "l2_free_on_write", KSTAT_DATA_UINT64 },
335 { "l2_abort_lowmem", KSTAT_DATA_UINT64 },
336 { "l2_cksum_bad", KSTAT_DATA_UINT64 },
337 { "l2_io_error", KSTAT_DATA_UINT64 },
338 { "l2_size", KSTAT_DATA_UINT64 },
339 { "l2_hdr_size", KSTAT_DATA_UINT64 },
340 { "memory_throttle_count", KSTAT_DATA_UINT64 }
343 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
345 #define ARCSTAT_INCR(stat, val) \
346 atomic_add_64(&arc_stats.stat.value.ui64, (val));
348 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
349 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
351 #define ARCSTAT_MAX(stat, val) { \
353 while ((val) > (m = arc_stats.stat.value.ui64) && \
354 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
358 #define ARCSTAT_MAXSTAT(stat) \
359 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
362 * We define a macro to allow ARC hits/misses to be easily broken down by
363 * two separate conditions, giving a total of four different subtypes for
364 * each of hits and misses (so eight statistics total).
366 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
369 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
371 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
375 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
377 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
382 static arc_state_t *arc_anon;
383 static arc_state_t *arc_mru;
384 static arc_state_t *arc_mru_ghost;
385 static arc_state_t *arc_mfu;
386 static arc_state_t *arc_mfu_ghost;
387 static arc_state_t *arc_l2c_only;
390 * There are several ARC variables that are critical to export as kstats --
391 * but we don't want to have to grovel around in the kstat whenever we wish to
392 * manipulate them. For these variables, we therefore define them to be in
393 * terms of the statistic variable. This assures that we are not introducing
394 * the possibility of inconsistency by having shadow copies of the variables,
395 * while still allowing the code to be readable.
397 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
398 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
399 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
400 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
401 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
403 static int arc_no_grow; /* Don't try to grow cache size */
404 static uint64_t arc_tempreserve;
405 static uint64_t arc_loaned_bytes;
406 static uint64_t arc_meta_used;
407 static uint64_t arc_meta_limit;
408 static uint64_t arc_meta_max = 0;
410 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
412 typedef struct arc_callback arc_callback_t;
414 struct arc_callback {
416 arc_done_func_t *acb_done;
418 zio_t *acb_zio_dummy;
419 arc_callback_t *acb_next;
422 typedef struct arc_write_callback arc_write_callback_t;
424 struct arc_write_callback {
426 arc_done_func_t *awcb_ready;
427 arc_done_func_t *awcb_done;
432 /* protected by hash lock */
437 kmutex_t b_freeze_lock;
438 zio_cksum_t *b_freeze_cksum;
441 arc_buf_hdr_t *b_hash_next;
446 arc_callback_t *b_acb;
450 arc_buf_contents_t b_type;
454 /* protected by arc state mutex */
455 arc_state_t *b_state;
456 list_node_t b_arc_node;
458 /* updated atomically */
459 clock_t b_arc_access;
461 /* self protecting */
464 l2arc_buf_hdr_t *b_l2hdr;
465 list_node_t b_l2node;
468 static arc_buf_t *arc_eviction_list;
469 static kmutex_t arc_eviction_mtx;
470 static arc_buf_hdr_t arc_eviction_hdr;
471 static void arc_get_data_buf(arc_buf_t *buf);
472 static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
473 static int arc_evict_needed(arc_buf_contents_t type);
474 static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
476 static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
478 #define GHOST_STATE(state) \
479 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
480 (state) == arc_l2c_only)
483 * Private ARC flags. These flags are private ARC only flags that will show up
484 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
485 * be passed in as arc_flags in things like arc_read. However, these flags
486 * should never be passed and should only be set by ARC code. When adding new
487 * public flags, make sure not to smash the private ones.
490 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
491 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
492 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
493 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
494 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
495 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
496 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
497 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
498 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
499 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
501 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
502 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
503 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
504 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
505 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
506 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
507 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
508 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
509 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
510 (hdr)->b_l2hdr != NULL)
511 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
512 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
513 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
519 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
520 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
523 * Hash table routines
526 #define HT_LOCK_PAD 64
531 unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
535 #define BUF_LOCKS 256
536 typedef struct buf_hash_table {
538 arc_buf_hdr_t **ht_table;
539 struct ht_lock ht_locks[BUF_LOCKS];
542 static buf_hash_table_t buf_hash_table;
544 #define BUF_HASH_INDEX(spa, dva, birth) \
545 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
546 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
547 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
548 #define HDR_LOCK(hdr) \
549 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
551 uint64_t zfs_crc64_table[256];
557 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
558 #define L2ARC_HEADROOM 2 /* num of writes */
559 #define L2ARC_FEED_SECS 1 /* caching interval secs */
560 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
562 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
563 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
566 * L2ARC Performance Tunables
568 uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */
569 uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */
570 uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */
571 uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */
572 uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */
573 boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */
574 boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */
575 boolean_t l2arc_norw = B_TRUE; /* no reads during writes */
580 typedef struct l2arc_dev {
581 vdev_t *l2ad_vdev; /* vdev */
582 spa_t *l2ad_spa; /* spa */
583 uint64_t l2ad_hand; /* next write location */
584 uint64_t l2ad_write; /* desired write size, bytes */
585 uint64_t l2ad_boost; /* warmup write boost, bytes */
586 uint64_t l2ad_start; /* first addr on device */
587 uint64_t l2ad_end; /* last addr on device */
588 uint64_t l2ad_evict; /* last addr eviction reached */
589 boolean_t l2ad_first; /* first sweep through */
590 boolean_t l2ad_writing; /* currently writing */
591 list_t *l2ad_buflist; /* buffer list */
592 list_node_t l2ad_node; /* device list node */
595 static list_t L2ARC_dev_list; /* device list */
596 static list_t *l2arc_dev_list; /* device list pointer */
597 static kmutex_t l2arc_dev_mtx; /* device list mutex */
598 static l2arc_dev_t *l2arc_dev_last; /* last device used */
599 static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */
600 static list_t L2ARC_free_on_write; /* free after write buf list */
601 static list_t *l2arc_free_on_write; /* free after write list ptr */
602 static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */
603 static uint64_t l2arc_ndev; /* number of devices */
605 typedef struct l2arc_read_callback {
606 arc_buf_t *l2rcb_buf; /* read buffer */
607 spa_t *l2rcb_spa; /* spa */
608 blkptr_t l2rcb_bp; /* original blkptr */
609 zbookmark_t l2rcb_zb; /* original bookmark */
610 int l2rcb_flags; /* original flags */
611 } l2arc_read_callback_t;
613 typedef struct l2arc_write_callback {
614 l2arc_dev_t *l2wcb_dev; /* device info */
615 arc_buf_hdr_t *l2wcb_head; /* head of write buflist */
616 } l2arc_write_callback_t;
618 struct l2arc_buf_hdr {
619 /* protected by arc_buf_hdr mutex */
620 l2arc_dev_t *b_dev; /* L2ARC device */
621 uint64_t b_daddr; /* disk address, offset byte */
624 typedef struct l2arc_data_free {
625 /* protected by l2arc_free_on_write_mtx */
628 void (*l2df_func)(void *, size_t);
629 list_node_t l2df_list_node;
632 static kmutex_t l2arc_feed_thr_lock;
633 static kcondvar_t l2arc_feed_thr_cv;
634 static uint8_t l2arc_thread_exit;
636 static void l2arc_read_done(zio_t *zio);
637 static void l2arc_hdr_stat_add(void);
638 static void l2arc_hdr_stat_remove(void);
641 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
643 uint8_t *vdva = (uint8_t *)dva;
644 uint64_t crc = -1ULL;
647 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
649 for (i = 0; i < sizeof (dva_t); i++)
650 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
652 crc ^= (spa>>8) ^ birth;
657 #define BUF_EMPTY(buf) \
658 ((buf)->b_dva.dva_word[0] == 0 && \
659 (buf)->b_dva.dva_word[1] == 0 && \
662 #define BUF_EQUAL(spa, dva, birth, buf) \
663 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
664 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
665 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
668 buf_discard_identity(arc_buf_hdr_t *hdr)
670 hdr->b_dva.dva_word[0] = 0;
671 hdr->b_dva.dva_word[1] = 0;
676 static arc_buf_hdr_t *
677 buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp)
679 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
680 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
683 mutex_enter(hash_lock);
684 for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
685 buf = buf->b_hash_next) {
686 if (BUF_EQUAL(spa, dva, birth, buf)) {
691 mutex_exit(hash_lock);
697 * Insert an entry into the hash table. If there is already an element
698 * equal to elem in the hash table, then the already existing element
699 * will be returned and the new element will not be inserted.
700 * Otherwise returns NULL.
702 static arc_buf_hdr_t *
703 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
705 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
706 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
710 ASSERT(!HDR_IN_HASH_TABLE(buf));
712 mutex_enter(hash_lock);
713 for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
714 fbuf = fbuf->b_hash_next, i++) {
715 if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
719 buf->b_hash_next = buf_hash_table.ht_table[idx];
720 buf_hash_table.ht_table[idx] = buf;
721 buf->b_flags |= ARC_IN_HASH_TABLE;
723 /* collect some hash table performance data */
725 ARCSTAT_BUMP(arcstat_hash_collisions);
727 ARCSTAT_BUMP(arcstat_hash_chains);
729 ARCSTAT_MAX(arcstat_hash_chain_max, i);
732 ARCSTAT_BUMP(arcstat_hash_elements);
733 ARCSTAT_MAXSTAT(arcstat_hash_elements);
739 buf_hash_remove(arc_buf_hdr_t *buf)
741 arc_buf_hdr_t *fbuf, **bufp;
742 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
744 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
745 ASSERT(HDR_IN_HASH_TABLE(buf));
747 bufp = &buf_hash_table.ht_table[idx];
748 while ((fbuf = *bufp) != buf) {
749 ASSERT(fbuf != NULL);
750 bufp = &fbuf->b_hash_next;
752 *bufp = buf->b_hash_next;
753 buf->b_hash_next = NULL;
754 buf->b_flags &= ~ARC_IN_HASH_TABLE;
756 /* collect some hash table performance data */
757 ARCSTAT_BUMPDOWN(arcstat_hash_elements);
759 if (buf_hash_table.ht_table[idx] &&
760 buf_hash_table.ht_table[idx]->b_hash_next == NULL)
761 ARCSTAT_BUMPDOWN(arcstat_hash_chains);
765 * Global data structures and functions for the buf kmem cache.
767 static kmem_cache_t *hdr_cache;
768 static kmem_cache_t *buf_cache;
775 kmem_free(buf_hash_table.ht_table,
776 (buf_hash_table.ht_mask + 1) * sizeof (void *));
777 for (i = 0; i < BUF_LOCKS; i++)
778 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
779 kmem_cache_destroy(hdr_cache);
780 kmem_cache_destroy(buf_cache);
784 * Constructor callback - called when the cache is empty
785 * and a new buf is requested.
789 hdr_cons(void *vbuf, void *unused, int kmflag)
791 arc_buf_hdr_t *buf = vbuf;
793 bzero(buf, sizeof (arc_buf_hdr_t));
794 refcount_create(&buf->b_refcnt);
795 cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
796 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
797 arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
804 buf_cons(void *vbuf, void *unused, int kmflag)
806 arc_buf_t *buf = vbuf;
808 bzero(buf, sizeof (arc_buf_t));
809 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
810 rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL);
811 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
817 * Destructor callback - called when a cached buf is
818 * no longer required.
822 hdr_dest(void *vbuf, void *unused)
824 arc_buf_hdr_t *buf = vbuf;
826 ASSERT(BUF_EMPTY(buf));
827 refcount_destroy(&buf->b_refcnt);
828 cv_destroy(&buf->b_cv);
829 mutex_destroy(&buf->b_freeze_lock);
830 arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
835 buf_dest(void *vbuf, void *unused)
837 arc_buf_t *buf = vbuf;
839 mutex_destroy(&buf->b_evict_lock);
840 rw_destroy(&buf->b_data_lock);
841 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
845 * Reclaim callback -- invoked when memory is low.
849 hdr_recl(void *unused)
851 dprintf("hdr_recl called\n");
853 * umem calls the reclaim func when we destroy the buf cache,
854 * which is after we do arc_fini().
857 cv_signal(&arc_reclaim_thr_cv);
864 uint64_t hsize = 1ULL << 12;
868 * The hash table is big enough to fill all of physical memory
869 * with an average 64K block size. The table will take up
870 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
872 while (hsize * 65536 < physmem * PAGESIZE)
875 buf_hash_table.ht_mask = hsize - 1;
876 buf_hash_table.ht_table =
877 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
878 if (buf_hash_table.ht_table == NULL) {
879 ASSERT(hsize > (1ULL << 8));
884 hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
885 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
886 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
887 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
889 for (i = 0; i < 256; i++)
890 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
891 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
893 for (i = 0; i < BUF_LOCKS; i++) {
894 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
895 NULL, MUTEX_DEFAULT, NULL);
899 #define ARC_MINTIME (hz>>4) /* 62 ms */
902 arc_cksum_verify(arc_buf_t *buf)
906 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
909 mutex_enter(&buf->b_hdr->b_freeze_lock);
910 if (buf->b_hdr->b_freeze_cksum == NULL ||
911 (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
912 mutex_exit(&buf->b_hdr->b_freeze_lock);
915 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
916 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
917 panic("buffer modified while frozen!");
918 mutex_exit(&buf->b_hdr->b_freeze_lock);
922 arc_cksum_equal(arc_buf_t *buf)
927 mutex_enter(&buf->b_hdr->b_freeze_lock);
928 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
929 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
930 mutex_exit(&buf->b_hdr->b_freeze_lock);
936 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
938 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
941 mutex_enter(&buf->b_hdr->b_freeze_lock);
942 if (buf->b_hdr->b_freeze_cksum != NULL) {
943 mutex_exit(&buf->b_hdr->b_freeze_lock);
946 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
947 fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
948 buf->b_hdr->b_freeze_cksum);
949 mutex_exit(&buf->b_hdr->b_freeze_lock);
953 arc_buf_thaw(arc_buf_t *buf)
955 if (zfs_flags & ZFS_DEBUG_MODIFY) {
956 if (buf->b_hdr->b_state != arc_anon)
957 panic("modifying non-anon buffer!");
958 if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
959 panic("modifying buffer while i/o in progress!");
960 arc_cksum_verify(buf);
963 mutex_enter(&buf->b_hdr->b_freeze_lock);
964 if (buf->b_hdr->b_freeze_cksum != NULL) {
965 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
966 buf->b_hdr->b_freeze_cksum = NULL;
969 if (zfs_flags & ZFS_DEBUG_MODIFY) {
970 if (buf->b_hdr->b_thawed)
971 kmem_free(buf->b_hdr->b_thawed, 1);
972 buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
975 mutex_exit(&buf->b_hdr->b_freeze_lock);
979 arc_buf_freeze(arc_buf_t *buf)
983 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
986 hash_lock = HDR_LOCK(buf->b_hdr);
987 mutex_enter(hash_lock);
989 ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
990 buf->b_hdr->b_state == arc_anon);
991 arc_cksum_compute(buf, B_FALSE);
992 mutex_exit(hash_lock);
996 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
998 ASSERT(MUTEX_HELD(hash_lock));
1000 if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1001 (ab->b_state != arc_anon)) {
1002 uint64_t delta = ab->b_size * ab->b_datacnt;
1003 list_t *list = &ab->b_state->arcs_list[ab->b_type];
1004 uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1006 ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
1007 mutex_enter(&ab->b_state->arcs_mtx);
1008 ASSERT(list_link_active(&ab->b_arc_node));
1009 list_remove(list, ab);
1010 if (GHOST_STATE(ab->b_state)) {
1011 ASSERT3U(ab->b_datacnt, ==, 0);
1012 ASSERT3P(ab->b_buf, ==, NULL);
1016 ASSERT3U(*size, >=, delta);
1017 atomic_add_64(size, -delta);
1018 mutex_exit(&ab->b_state->arcs_mtx);
1019 /* remove the prefetch flag if we get a reference */
1020 if (ab->b_flags & ARC_PREFETCH)
1021 ab->b_flags &= ~ARC_PREFETCH;
1026 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1029 arc_state_t *state = ab->b_state;
1031 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1032 ASSERT(!GHOST_STATE(state));
1034 if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1035 (state != arc_anon)) {
1036 uint64_t *size = &state->arcs_lsize[ab->b_type];
1038 ASSERT(!MUTEX_HELD(&state->arcs_mtx));
1039 mutex_enter(&state->arcs_mtx);
1040 ASSERT(!list_link_active(&ab->b_arc_node));
1041 list_insert_head(&state->arcs_list[ab->b_type], ab);
1042 ASSERT(ab->b_datacnt > 0);
1043 atomic_add_64(size, ab->b_size * ab->b_datacnt);
1044 mutex_exit(&state->arcs_mtx);
1050 * Move the supplied buffer to the indicated state. The mutex
1051 * for the buffer must be held by the caller.
1054 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1056 arc_state_t *old_state = ab->b_state;
1057 int64_t refcnt = refcount_count(&ab->b_refcnt);
1058 uint64_t from_delta, to_delta;
1060 ASSERT(MUTEX_HELD(hash_lock));
1061 ASSERT(new_state != old_state);
1062 ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1063 ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1064 ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1066 from_delta = to_delta = ab->b_datacnt * ab->b_size;
1069 * If this buffer is evictable, transfer it from the
1070 * old state list to the new state list.
1073 if (old_state != arc_anon) {
1074 int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
1075 uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1078 mutex_enter(&old_state->arcs_mtx);
1080 ASSERT(list_link_active(&ab->b_arc_node));
1081 list_remove(&old_state->arcs_list[ab->b_type], ab);
1084 * If prefetching out of the ghost cache,
1085 * we will have a non-zero datacnt.
1087 if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1088 /* ghost elements have a ghost size */
1089 ASSERT(ab->b_buf == NULL);
1090 from_delta = ab->b_size;
1092 ASSERT3U(*size, >=, from_delta);
1093 atomic_add_64(size, -from_delta);
1096 mutex_exit(&old_state->arcs_mtx);
1098 if (new_state != arc_anon) {
1099 int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
1100 uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1103 mutex_enter(&new_state->arcs_mtx);
1105 list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1107 /* ghost elements have a ghost size */
1108 if (GHOST_STATE(new_state)) {
1109 ASSERT(ab->b_datacnt == 0);
1110 ASSERT(ab->b_buf == NULL);
1111 to_delta = ab->b_size;
1113 atomic_add_64(size, to_delta);
1116 mutex_exit(&new_state->arcs_mtx);
1120 ASSERT(!BUF_EMPTY(ab));
1121 if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1122 buf_hash_remove(ab);
1124 /* adjust state sizes */
1126 atomic_add_64(&new_state->arcs_size, to_delta);
1128 ASSERT3U(old_state->arcs_size, >=, from_delta);
1129 atomic_add_64(&old_state->arcs_size, -from_delta);
1131 ab->b_state = new_state;
1133 /* adjust l2arc hdr stats */
1134 if (new_state == arc_l2c_only)
1135 l2arc_hdr_stat_add();
1136 else if (old_state == arc_l2c_only)
1137 l2arc_hdr_stat_remove();
1141 arc_space_consume(uint64_t space, arc_space_type_t type)
1143 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1146 case ARC_SPACE_DATA:
1147 ARCSTAT_INCR(arcstat_data_size, space);
1149 case ARC_SPACE_OTHER:
1150 ARCSTAT_INCR(arcstat_other_size, space);
1152 case ARC_SPACE_HDRS:
1153 ARCSTAT_INCR(arcstat_hdr_size, space);
1155 case ARC_SPACE_L2HDRS:
1156 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1160 atomic_add_64(&arc_meta_used, space);
1161 atomic_add_64(&arc_size, space);
1165 arc_space_return(uint64_t space, arc_space_type_t type)
1167 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1170 case ARC_SPACE_DATA:
1171 ARCSTAT_INCR(arcstat_data_size, -space);
1173 case ARC_SPACE_OTHER:
1174 ARCSTAT_INCR(arcstat_other_size, -space);
1176 case ARC_SPACE_HDRS:
1177 ARCSTAT_INCR(arcstat_hdr_size, -space);
1179 case ARC_SPACE_L2HDRS:
1180 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1184 ASSERT(arc_meta_used >= space);
1185 if (arc_meta_max < arc_meta_used)
1186 arc_meta_max = arc_meta_used;
1187 atomic_add_64(&arc_meta_used, -space);
1188 ASSERT(arc_size >= space);
1189 atomic_add_64(&arc_size, -space);
1193 arc_data_buf_alloc(uint64_t size)
1195 if (arc_evict_needed(ARC_BUFC_DATA))
1196 cv_signal(&arc_reclaim_thr_cv);
1197 atomic_add_64(&arc_size, size);
1198 return (zio_data_buf_alloc(size));
1202 arc_data_buf_free(void *buf, uint64_t size)
1204 zio_data_buf_free(buf, size);
1205 ASSERT(arc_size >= size);
1206 atomic_add_64(&arc_size, -size);
1210 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1215 ASSERT3U(size, >, 0);
1216 hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1217 ASSERT(BUF_EMPTY(hdr));
1220 hdr->b_spa = spa_guid(spa);
1221 hdr->b_state = arc_anon;
1222 hdr->b_arc_access = 0;
1223 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1226 buf->b_efunc = NULL;
1227 buf->b_private = NULL;
1230 arc_get_data_buf(buf);
1233 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1234 (void) refcount_add(&hdr->b_refcnt, tag);
1239 static char *arc_onloan_tag = "onloan";
1242 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1243 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1244 * buffers must be returned to the arc before they can be used by the DMU or
1248 arc_loan_buf(spa_t *spa, int size)
1252 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1254 atomic_add_64(&arc_loaned_bytes, size);
1259 * Return a loaned arc buffer to the arc.
1262 arc_return_buf(arc_buf_t *buf, void *tag)
1264 arc_buf_hdr_t *hdr = buf->b_hdr;
1266 ASSERT(buf->b_data != NULL);
1267 (void) refcount_add(&hdr->b_refcnt, tag);
1268 (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1270 atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1273 /* Detach an arc_buf from a dbuf (tag) */
1275 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1279 ASSERT(buf->b_data != NULL);
1281 (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1282 (void) refcount_remove(&hdr->b_refcnt, tag);
1283 buf->b_efunc = NULL;
1284 buf->b_private = NULL;
1286 atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1290 arc_buf_clone(arc_buf_t *from)
1293 arc_buf_hdr_t *hdr = from->b_hdr;
1294 uint64_t size = hdr->b_size;
1296 ASSERT(hdr->b_state != arc_anon);
1298 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1301 buf->b_efunc = NULL;
1302 buf->b_private = NULL;
1303 buf->b_next = hdr->b_buf;
1305 arc_get_data_buf(buf);
1306 bcopy(from->b_data, buf->b_data, size);
1307 hdr->b_datacnt += 1;
1312 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1315 kmutex_t *hash_lock;
1318 * Check to see if this buffer is evicted. Callers
1319 * must verify b_data != NULL to know if the add_ref
1322 mutex_enter(&buf->b_evict_lock);
1323 if (buf->b_data == NULL) {
1324 mutex_exit(&buf->b_evict_lock);
1327 hash_lock = HDR_LOCK(buf->b_hdr);
1328 mutex_enter(hash_lock);
1330 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1331 mutex_exit(&buf->b_evict_lock);
1333 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1334 add_reference(hdr, hash_lock, tag);
1335 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1336 arc_access(hdr, hash_lock);
1337 mutex_exit(hash_lock);
1338 ARCSTAT_BUMP(arcstat_hits);
1339 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1340 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1341 data, metadata, hits);
1345 * Free the arc data buffer. If it is an l2arc write in progress,
1346 * the buffer is placed on l2arc_free_on_write to be freed later.
1349 arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t),
1350 void *data, size_t size)
1352 if (HDR_L2_WRITING(hdr)) {
1353 l2arc_data_free_t *df;
1354 df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
1355 df->l2df_data = data;
1356 df->l2df_size = size;
1357 df->l2df_func = free_func;
1358 mutex_enter(&l2arc_free_on_write_mtx);
1359 list_insert_head(l2arc_free_on_write, df);
1360 mutex_exit(&l2arc_free_on_write_mtx);
1361 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1363 free_func(data, size);
1368 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1372 /* free up data associated with the buf */
1374 arc_state_t *state = buf->b_hdr->b_state;
1375 uint64_t size = buf->b_hdr->b_size;
1376 arc_buf_contents_t type = buf->b_hdr->b_type;
1378 arc_cksum_verify(buf);
1381 if (type == ARC_BUFC_METADATA) {
1382 arc_buf_data_free(buf->b_hdr, zio_buf_free,
1384 arc_space_return(size, ARC_SPACE_DATA);
1386 ASSERT(type == ARC_BUFC_DATA);
1387 arc_buf_data_free(buf->b_hdr,
1388 zio_data_buf_free, buf->b_data, size);
1389 ARCSTAT_INCR(arcstat_data_size, -size);
1390 atomic_add_64(&arc_size, -size);
1393 if (list_link_active(&buf->b_hdr->b_arc_node)) {
1394 uint64_t *cnt = &state->arcs_lsize[type];
1396 ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1397 ASSERT(state != arc_anon);
1399 ASSERT3U(*cnt, >=, size);
1400 atomic_add_64(cnt, -size);
1402 ASSERT3U(state->arcs_size, >=, size);
1403 atomic_add_64(&state->arcs_size, -size);
1405 ASSERT(buf->b_hdr->b_datacnt > 0);
1406 buf->b_hdr->b_datacnt -= 1;
1409 /* only remove the buf if requested */
1413 /* remove the buf from the hdr list */
1414 for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1416 *bufp = buf->b_next;
1419 ASSERT(buf->b_efunc == NULL);
1421 /* clean up the buf */
1423 kmem_cache_free(buf_cache, buf);
1427 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1429 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1430 ASSERT3P(hdr->b_state, ==, arc_anon);
1431 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1432 l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1434 if (l2hdr != NULL) {
1435 boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1437 * To prevent arc_free() and l2arc_evict() from
1438 * attempting to free the same buffer at the same time,
1439 * a FREE_IN_PROGRESS flag is given to arc_free() to
1440 * give it priority. l2arc_evict() can't destroy this
1441 * header while we are waiting on l2arc_buflist_mtx.
1443 * The hdr may be removed from l2ad_buflist before we
1444 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1446 if (!buflist_held) {
1447 mutex_enter(&l2arc_buflist_mtx);
1448 l2hdr = hdr->b_l2hdr;
1451 if (l2hdr != NULL) {
1452 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1453 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1454 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1455 if (hdr->b_state == arc_l2c_only)
1456 l2arc_hdr_stat_remove();
1457 hdr->b_l2hdr = NULL;
1461 mutex_exit(&l2arc_buflist_mtx);
1464 if (!BUF_EMPTY(hdr)) {
1465 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1466 buf_discard_identity(hdr);
1468 while (hdr->b_buf) {
1469 arc_buf_t *buf = hdr->b_buf;
1472 mutex_enter(&arc_eviction_mtx);
1473 mutex_enter(&buf->b_evict_lock);
1474 ASSERT(buf->b_hdr != NULL);
1475 arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1476 hdr->b_buf = buf->b_next;
1477 buf->b_hdr = &arc_eviction_hdr;
1478 buf->b_next = arc_eviction_list;
1479 arc_eviction_list = buf;
1480 mutex_exit(&buf->b_evict_lock);
1481 mutex_exit(&arc_eviction_mtx);
1483 arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1486 if (hdr->b_freeze_cksum != NULL) {
1487 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1488 hdr->b_freeze_cksum = NULL;
1490 if (hdr->b_thawed) {
1491 kmem_free(hdr->b_thawed, 1);
1492 hdr->b_thawed = NULL;
1495 ASSERT(!list_link_active(&hdr->b_arc_node));
1496 ASSERT3P(hdr->b_hash_next, ==, NULL);
1497 ASSERT3P(hdr->b_acb, ==, NULL);
1498 kmem_cache_free(hdr_cache, hdr);
1502 arc_buf_free(arc_buf_t *buf, void *tag)
1504 arc_buf_hdr_t *hdr = buf->b_hdr;
1505 int hashed = hdr->b_state != arc_anon;
1507 ASSERT(buf->b_efunc == NULL);
1508 ASSERT(buf->b_data != NULL);
1511 kmutex_t *hash_lock = HDR_LOCK(hdr);
1513 mutex_enter(hash_lock);
1515 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1517 (void) remove_reference(hdr, hash_lock, tag);
1518 if (hdr->b_datacnt > 1) {
1519 arc_buf_destroy(buf, FALSE, TRUE);
1521 ASSERT(buf == hdr->b_buf);
1522 ASSERT(buf->b_efunc == NULL);
1523 hdr->b_flags |= ARC_BUF_AVAILABLE;
1525 mutex_exit(hash_lock);
1526 } else if (HDR_IO_IN_PROGRESS(hdr)) {
1529 * We are in the middle of an async write. Don't destroy
1530 * this buffer unless the write completes before we finish
1531 * decrementing the reference count.
1533 mutex_enter(&arc_eviction_mtx);
1534 (void) remove_reference(hdr, NULL, tag);
1535 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1536 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1537 mutex_exit(&arc_eviction_mtx);
1539 arc_hdr_destroy(hdr);
1541 if (remove_reference(hdr, NULL, tag) > 0)
1542 arc_buf_destroy(buf, FALSE, TRUE);
1544 arc_hdr_destroy(hdr);
1549 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1551 arc_buf_hdr_t *hdr = buf->b_hdr;
1552 kmutex_t *hash_lock = HDR_LOCK(hdr);
1553 int no_callback = (buf->b_efunc == NULL);
1555 if (hdr->b_state == arc_anon) {
1556 ASSERT(hdr->b_datacnt == 1);
1557 arc_buf_free(buf, tag);
1558 return (no_callback);
1561 mutex_enter(hash_lock);
1563 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1564 ASSERT(hdr->b_state != arc_anon);
1565 ASSERT(buf->b_data != NULL);
1567 (void) remove_reference(hdr, hash_lock, tag);
1568 if (hdr->b_datacnt > 1) {
1570 arc_buf_destroy(buf, FALSE, TRUE);
1571 } else if (no_callback) {
1572 ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1573 ASSERT(buf->b_efunc == NULL);
1574 hdr->b_flags |= ARC_BUF_AVAILABLE;
1576 ASSERT(no_callback || hdr->b_datacnt > 1 ||
1577 refcount_is_zero(&hdr->b_refcnt));
1578 mutex_exit(hash_lock);
1579 return (no_callback);
1583 arc_buf_size(arc_buf_t *buf)
1585 return (buf->b_hdr->b_size);
1589 * Evict buffers from list until we've removed the specified number of
1590 * bytes. Move the removed buffers to the appropriate evict state.
1591 * If the recycle flag is set, then attempt to "recycle" a buffer:
1592 * - look for a buffer to evict that is `bytes' long.
1593 * - return the data block from this buffer rather than freeing it.
1594 * This flag is used by callers that are trying to make space for a
1595 * new buffer in a full arc cache.
1597 * This function makes a "best effort". It skips over any buffers
1598 * it can't get a hash_lock on, and so may not catch all candidates.
1599 * It may also return without evicting as much space as requested.
1602 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1603 arc_buf_contents_t type)
1605 arc_state_t *evicted_state;
1606 uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1607 arc_buf_hdr_t *ab, *ab_prev = NULL;
1608 list_t *list = &state->arcs_list[type];
1609 kmutex_t *hash_lock;
1610 boolean_t have_lock;
1611 void *stolen = NULL;
1613 ASSERT(state == arc_mru || state == arc_mfu);
1615 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1617 mutex_enter(&state->arcs_mtx);
1618 mutex_enter(&evicted_state->arcs_mtx);
1620 for (ab = list_tail(list); ab; ab = ab_prev) {
1621 ab_prev = list_prev(list, ab);
1622 /* prefetch buffers have a minimum lifespan */
1623 if (HDR_IO_IN_PROGRESS(ab) ||
1624 (spa && ab->b_spa != spa) ||
1625 (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1626 ddi_get_lbolt() - ab->b_arc_access <
1627 arc_min_prefetch_lifespan)) {
1631 /* "lookahead" for better eviction candidate */
1632 if (recycle && ab->b_size != bytes &&
1633 ab_prev && ab_prev->b_size == bytes)
1635 hash_lock = HDR_LOCK(ab);
1636 have_lock = MUTEX_HELD(hash_lock);
1637 if (have_lock || mutex_tryenter(hash_lock)) {
1638 ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0);
1639 ASSERT(ab->b_datacnt > 0);
1641 arc_buf_t *buf = ab->b_buf;
1642 if (!mutex_tryenter(&buf->b_evict_lock)) {
1647 bytes_evicted += ab->b_size;
1648 if (recycle && ab->b_type == type &&
1649 ab->b_size == bytes &&
1650 !HDR_L2_WRITING(ab)) {
1651 stolen = buf->b_data;
1656 mutex_enter(&arc_eviction_mtx);
1657 arc_buf_destroy(buf,
1658 buf->b_data == stolen, FALSE);
1659 ab->b_buf = buf->b_next;
1660 buf->b_hdr = &arc_eviction_hdr;
1661 buf->b_next = arc_eviction_list;
1662 arc_eviction_list = buf;
1663 mutex_exit(&arc_eviction_mtx);
1664 mutex_exit(&buf->b_evict_lock);
1666 mutex_exit(&buf->b_evict_lock);
1667 arc_buf_destroy(buf,
1668 buf->b_data == stolen, TRUE);
1673 ARCSTAT_INCR(arcstat_evict_l2_cached,
1676 if (l2arc_write_eligible(ab->b_spa, ab)) {
1677 ARCSTAT_INCR(arcstat_evict_l2_eligible,
1681 arcstat_evict_l2_ineligible,
1686 if (ab->b_datacnt == 0) {
1687 arc_change_state(evicted_state, ab, hash_lock);
1688 ASSERT(HDR_IN_HASH_TABLE(ab));
1689 ab->b_flags |= ARC_IN_HASH_TABLE;
1690 ab->b_flags &= ~ARC_BUF_AVAILABLE;
1691 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1694 mutex_exit(hash_lock);
1695 if (bytes >= 0 && bytes_evicted >= bytes)
1702 mutex_exit(&evicted_state->arcs_mtx);
1703 mutex_exit(&state->arcs_mtx);
1705 if (bytes_evicted < bytes)
1706 dprintf("only evicted %lld bytes from %x",
1707 (longlong_t)bytes_evicted, state);
1710 ARCSTAT_INCR(arcstat_evict_skip, skipped);
1713 ARCSTAT_INCR(arcstat_mutex_miss, missed);
1716 * We have just evicted some date into the ghost state, make
1717 * sure we also adjust the ghost state size if necessary.
1720 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
1721 int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
1722 arc_mru_ghost->arcs_size - arc_c;
1724 if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
1726 MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
1727 arc_evict_ghost(arc_mru_ghost, NULL, todelete);
1728 } else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
1729 int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
1730 arc_mru_ghost->arcs_size +
1731 arc_mfu_ghost->arcs_size - arc_c);
1732 arc_evict_ghost(arc_mfu_ghost, NULL, todelete);
1740 * Remove buffers from list until we've removed the specified number of
1741 * bytes. Destroy the buffers that are removed.
1744 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
1746 arc_buf_hdr_t *ab, *ab_prev;
1747 arc_buf_hdr_t marker = { 0 };
1748 list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1749 kmutex_t *hash_lock;
1750 uint64_t bytes_deleted = 0;
1751 uint64_t bufs_skipped = 0;
1753 ASSERT(GHOST_STATE(state));
1755 mutex_enter(&state->arcs_mtx);
1756 for (ab = list_tail(list); ab; ab = ab_prev) {
1757 ab_prev = list_prev(list, ab);
1758 if (spa && ab->b_spa != spa)
1761 /* ignore markers */
1765 hash_lock = HDR_LOCK(ab);
1766 /* caller may be trying to modify this buffer, skip it */
1767 if (MUTEX_HELD(hash_lock))
1769 if (mutex_tryenter(hash_lock)) {
1770 ASSERT(!HDR_IO_IN_PROGRESS(ab));
1771 ASSERT(ab->b_buf == NULL);
1772 ARCSTAT_BUMP(arcstat_deleted);
1773 bytes_deleted += ab->b_size;
1775 if (ab->b_l2hdr != NULL) {
1777 * This buffer is cached on the 2nd Level ARC;
1778 * don't destroy the header.
1780 arc_change_state(arc_l2c_only, ab, hash_lock);
1781 mutex_exit(hash_lock);
1783 arc_change_state(arc_anon, ab, hash_lock);
1784 mutex_exit(hash_lock);
1785 arc_hdr_destroy(ab);
1788 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1789 if (bytes >= 0 && bytes_deleted >= bytes)
1791 } else if (bytes < 0) {
1793 * Insert a list marker and then wait for the
1794 * hash lock to become available. Once its
1795 * available, restart from where we left off.
1797 list_insert_after(list, ab, &marker);
1798 mutex_exit(&state->arcs_mtx);
1799 mutex_enter(hash_lock);
1800 mutex_exit(hash_lock);
1801 mutex_enter(&state->arcs_mtx);
1802 ab_prev = list_prev(list, &marker);
1803 list_remove(list, &marker);
1807 mutex_exit(&state->arcs_mtx);
1809 if (list == &state->arcs_list[ARC_BUFC_DATA] &&
1810 (bytes < 0 || bytes_deleted < bytes)) {
1811 list = &state->arcs_list[ARC_BUFC_METADATA];
1816 ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
1820 if (bytes_deleted < bytes)
1821 dprintf("only deleted %lld bytes from %p",
1822 (longlong_t)bytes_deleted, state);
1828 int64_t adjustment, delta;
1834 adjustment = MIN((int64_t)(arc_size - arc_c),
1835 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
1838 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
1839 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
1840 (void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA);
1841 adjustment -= delta;
1844 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1845 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
1846 (void) arc_evict(arc_mru, NULL, delta, FALSE,
1854 adjustment = arc_size - arc_c;
1856 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
1857 delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
1858 (void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA);
1859 adjustment -= delta;
1862 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1863 int64_t delta = MIN(adjustment,
1864 arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
1865 (void) arc_evict(arc_mfu, NULL, delta, FALSE,
1870 * Adjust ghost lists
1873 adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
1875 if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
1876 delta = MIN(arc_mru_ghost->arcs_size, adjustment);
1877 arc_evict_ghost(arc_mru_ghost, NULL, delta);
1881 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
1883 if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
1884 delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
1885 arc_evict_ghost(arc_mfu_ghost, NULL, delta);
1890 arc_do_user_evicts(void)
1892 mutex_enter(&arc_eviction_mtx);
1893 while (arc_eviction_list != NULL) {
1894 arc_buf_t *buf = arc_eviction_list;
1895 arc_eviction_list = buf->b_next;
1896 mutex_enter(&buf->b_evict_lock);
1898 mutex_exit(&buf->b_evict_lock);
1899 mutex_exit(&arc_eviction_mtx);
1901 if (buf->b_efunc != NULL)
1902 VERIFY(buf->b_efunc(buf) == 0);
1904 buf->b_efunc = NULL;
1905 buf->b_private = NULL;
1906 kmem_cache_free(buf_cache, buf);
1907 mutex_enter(&arc_eviction_mtx);
1909 mutex_exit(&arc_eviction_mtx);
1913 * Flush all *evictable* data from the cache for the given spa.
1914 * NOTE: this will not touch "active" (i.e. referenced) data.
1917 arc_flush(spa_t *spa)
1922 guid = spa_guid(spa);
1924 while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
1925 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
1929 while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
1930 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
1934 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
1935 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
1939 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
1940 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
1945 arc_evict_ghost(arc_mru_ghost, guid, -1);
1946 arc_evict_ghost(arc_mfu_ghost, guid, -1);
1948 mutex_enter(&arc_reclaim_thr_lock);
1949 arc_do_user_evicts();
1950 mutex_exit(&arc_reclaim_thr_lock);
1951 ASSERT(spa || arc_eviction_list == NULL);
1957 if (arc_c > arc_c_min) {
1961 to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree));
1963 to_free = arc_c >> arc_shrink_shift;
1965 if (arc_c > arc_c_min + to_free)
1966 atomic_add_64(&arc_c, -to_free);
1970 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
1971 if (arc_c > arc_size)
1972 arc_c = MAX(arc_size, arc_c_min);
1974 arc_p = (arc_c >> 1);
1975 ASSERT(arc_c >= arc_c_min);
1976 ASSERT((int64_t)arc_p >= 0);
1979 if (arc_size > arc_c)
1984 arc_reclaim_needed(void)
1994 * take 'desfree' extra pages, so we reclaim sooner, rather than later
1999 * check that we're out of range of the pageout scanner. It starts to
2000 * schedule paging if freemem is less than lotsfree and needfree.
2001 * lotsfree is the high-water mark for pageout, and needfree is the
2002 * number of needed free pages. We add extra pages here to make sure
2003 * the scanner doesn't start up while we're freeing memory.
2005 if (freemem < lotsfree + needfree + extra)
2009 * check to make sure that swapfs has enough space so that anon
2010 * reservations can still succeed. anon_resvmem() checks that the
2011 * availrmem is greater than swapfs_minfree, and the number of reserved
2012 * swap pages. We also add a bit of extra here just to prevent
2013 * circumstances from getting really dire.
2015 if (availrmem < swapfs_minfree + swapfs_reserve + extra)
2020 * If we're on an i386 platform, it's possible that we'll exhaust the
2021 * kernel heap space before we ever run out of available physical
2022 * memory. Most checks of the size of the heap_area compare against
2023 * tune.t_minarmem, which is the minimum available real memory that we
2024 * can have in the system. However, this is generally fixed at 25 pages
2025 * which is so low that it's useless. In this comparison, we seek to
2026 * calculate the total heap-size, and reclaim if more than 3/4ths of the
2027 * heap is allocated. (Or, in the calculation, if less than 1/4th is
2030 if (btop(vmem_size(heap_arena, VMEM_FREE)) <
2031 (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
2036 if (spa_get_random(100) == 0)
2043 arc_kmem_reap_now(arc_reclaim_strategy_t strat)
2046 kmem_cache_t *prev_cache = NULL;
2047 kmem_cache_t *prev_data_cache = NULL;
2048 extern kmem_cache_t *zio_buf_cache[];
2049 extern kmem_cache_t *zio_data_buf_cache[];
2052 if (arc_meta_used >= arc_meta_limit) {
2054 * We are exceeding our meta-data cache limit.
2055 * Purge some DNLC entries to release holds on meta-data.
2057 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
2061 * Reclaim unused memory from all kmem caches.
2068 * An aggressive reclamation will shrink the cache size as well as
2069 * reap free buffers from the arc kmem caches.
2071 if (strat == ARC_RECLAIM_AGGR)
2074 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2075 if (zio_buf_cache[i] != prev_cache) {
2076 prev_cache = zio_buf_cache[i];
2077 kmem_cache_reap_now(zio_buf_cache[i]);
2079 if (zio_data_buf_cache[i] != prev_data_cache) {
2080 prev_data_cache = zio_data_buf_cache[i];
2081 kmem_cache_reap_now(zio_data_buf_cache[i]);
2084 kmem_cache_reap_now(buf_cache);
2085 kmem_cache_reap_now(hdr_cache);
2089 arc_reclaim_thread(void)
2091 clock_t growtime = 0;
2092 arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS;
2095 CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2097 mutex_enter(&arc_reclaim_thr_lock);
2098 while (arc_thread_exit == 0) {
2099 if (arc_reclaim_needed()) {
2102 if (last_reclaim == ARC_RECLAIM_CONS) {
2103 last_reclaim = ARC_RECLAIM_AGGR;
2105 last_reclaim = ARC_RECLAIM_CONS;
2109 last_reclaim = ARC_RECLAIM_AGGR;
2113 /* reset the growth delay for every reclaim */
2114 growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
2116 arc_kmem_reap_now(last_reclaim);
2119 } else if (arc_no_grow && ddi_get_lbolt() >= growtime) {
2120 arc_no_grow = FALSE;
2125 if (arc_eviction_list != NULL)
2126 arc_do_user_evicts();
2128 /* block until needed, or one second, whichever is shorter */
2129 CALLB_CPR_SAFE_BEGIN(&cpr);
2130 (void) cv_timedwait(&arc_reclaim_thr_cv,
2131 &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2132 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2135 arc_thread_exit = 0;
2136 cv_broadcast(&arc_reclaim_thr_cv);
2137 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */
2142 * Adapt arc info given the number of bytes we are trying to add and
2143 * the state that we are comming from. This function is only called
2144 * when we are adding new content to the cache.
2147 arc_adapt(int bytes, arc_state_t *state)
2150 uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
2152 if (state == arc_l2c_only)
2157 * Adapt the target size of the MRU list:
2158 * - if we just hit in the MRU ghost list, then increase
2159 * the target size of the MRU list.
2160 * - if we just hit in the MFU ghost list, then increase
2161 * the target size of the MFU list by decreasing the
2162 * target size of the MRU list.
2164 if (state == arc_mru_ghost) {
2165 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2166 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2167 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2169 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2170 } else if (state == arc_mfu_ghost) {
2173 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2174 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2175 mult = MIN(mult, 10);
2177 delta = MIN(bytes * mult, arc_p);
2178 arc_p = MAX(arc_p_min, arc_p - delta);
2180 ASSERT((int64_t)arc_p >= 0);
2182 if (arc_reclaim_needed()) {
2183 cv_signal(&arc_reclaim_thr_cv);
2190 if (arc_c >= arc_c_max)
2194 * If we're within (2 * maxblocksize) bytes of the target
2195 * cache size, increment the target cache size
2197 if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2198 atomic_add_64(&arc_c, (int64_t)bytes);
2199 if (arc_c > arc_c_max)
2201 else if (state == arc_anon)
2202 atomic_add_64(&arc_p, (int64_t)bytes);
2206 ASSERT((int64_t)arc_p >= 0);
2210 * Check if the cache has reached its limits and eviction is required
2214 arc_evict_needed(arc_buf_contents_t type)
2216 if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2221 * If zio data pages are being allocated out of a separate heap segment,
2222 * then enforce that the size of available vmem for this area remains
2223 * above about 1/32nd free.
2225 if (type == ARC_BUFC_DATA && zio_arena != NULL &&
2226 vmem_size(zio_arena, VMEM_FREE) <
2227 (vmem_size(zio_arena, VMEM_ALLOC) >> 5))
2231 if (arc_reclaim_needed())
2234 return (arc_size > arc_c);
2238 * The buffer, supplied as the first argument, needs a data block.
2239 * So, if we are at cache max, determine which cache should be victimized.
2240 * We have the following cases:
2242 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2243 * In this situation if we're out of space, but the resident size of the MFU is
2244 * under the limit, victimize the MFU cache to satisfy this insertion request.
2246 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2247 * Here, we've used up all of the available space for the MRU, so we need to
2248 * evict from our own cache instead. Evict from the set of resident MRU
2251 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2252 * c minus p represents the MFU space in the cache, since p is the size of the
2253 * cache that is dedicated to the MRU. In this situation there's still space on
2254 * the MFU side, so the MRU side needs to be victimized.
2256 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2257 * MFU's resident set is consuming more space than it has been allotted. In
2258 * this situation, we must victimize our own cache, the MFU, for this insertion.
2261 arc_get_data_buf(arc_buf_t *buf)
2263 arc_state_t *state = buf->b_hdr->b_state;
2264 uint64_t size = buf->b_hdr->b_size;
2265 arc_buf_contents_t type = buf->b_hdr->b_type;
2267 arc_adapt(size, state);
2270 * We have not yet reached cache maximum size,
2271 * just allocate a new buffer.
2273 if (!arc_evict_needed(type)) {
2274 if (type == ARC_BUFC_METADATA) {
2275 buf->b_data = zio_buf_alloc(size);
2276 arc_space_consume(size, ARC_SPACE_DATA);
2278 ASSERT(type == ARC_BUFC_DATA);
2279 buf->b_data = zio_data_buf_alloc(size);
2280 ARCSTAT_INCR(arcstat_data_size, size);
2281 atomic_add_64(&arc_size, size);
2287 * If we are prefetching from the mfu ghost list, this buffer
2288 * will end up on the mru list; so steal space from there.
2290 if (state == arc_mfu_ghost)
2291 state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2292 else if (state == arc_mru_ghost)
2295 if (state == arc_mru || state == arc_anon) {
2296 uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2297 state = (arc_mfu->arcs_lsize[type] >= size &&
2298 arc_p > mru_used) ? arc_mfu : arc_mru;
2301 uint64_t mfu_space = arc_c - arc_p;
2302 state = (arc_mru->arcs_lsize[type] >= size &&
2303 mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2305 if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) {
2306 if (type == ARC_BUFC_METADATA) {
2307 buf->b_data = zio_buf_alloc(size);
2308 arc_space_consume(size, ARC_SPACE_DATA);
2310 ASSERT(type == ARC_BUFC_DATA);
2311 buf->b_data = zio_data_buf_alloc(size);
2312 ARCSTAT_INCR(arcstat_data_size, size);
2313 atomic_add_64(&arc_size, size);
2315 ARCSTAT_BUMP(arcstat_recycle_miss);
2317 ASSERT(buf->b_data != NULL);
2320 * Update the state size. Note that ghost states have a
2321 * "ghost size" and so don't need to be updated.
2323 if (!GHOST_STATE(buf->b_hdr->b_state)) {
2324 arc_buf_hdr_t *hdr = buf->b_hdr;
2326 atomic_add_64(&hdr->b_state->arcs_size, size);
2327 if (list_link_active(&hdr->b_arc_node)) {
2328 ASSERT(refcount_is_zero(&hdr->b_refcnt));
2329 atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2332 * If we are growing the cache, and we are adding anonymous
2333 * data, and we have outgrown arc_p, update arc_p
2335 if (arc_size < arc_c && hdr->b_state == arc_anon &&
2336 arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2337 arc_p = MIN(arc_c, arc_p + size);
2342 * This routine is called whenever a buffer is accessed.
2343 * NOTE: the hash lock is dropped in this function.
2346 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2350 ASSERT(MUTEX_HELD(hash_lock));
2352 if (buf->b_state == arc_anon) {
2354 * This buffer is not in the cache, and does not
2355 * appear in our "ghost" list. Add the new buffer
2359 ASSERT(buf->b_arc_access == 0);
2360 buf->b_arc_access = ddi_get_lbolt();
2361 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2362 arc_change_state(arc_mru, buf, hash_lock);
2364 } else if (buf->b_state == arc_mru) {
2365 now = ddi_get_lbolt();
2368 * If this buffer is here because of a prefetch, then either:
2369 * - clear the flag if this is a "referencing" read
2370 * (any subsequent access will bump this into the MFU state).
2372 * - move the buffer to the head of the list if this is
2373 * another prefetch (to make it less likely to be evicted).
2375 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2376 if (refcount_count(&buf->b_refcnt) == 0) {
2377 ASSERT(list_link_active(&buf->b_arc_node));
2379 buf->b_flags &= ~ARC_PREFETCH;
2380 ARCSTAT_BUMP(arcstat_mru_hits);
2382 buf->b_arc_access = now;
2387 * This buffer has been "accessed" only once so far,
2388 * but it is still in the cache. Move it to the MFU
2391 if (now > buf->b_arc_access + ARC_MINTIME) {
2393 * More than 125ms have passed since we
2394 * instantiated this buffer. Move it to the
2395 * most frequently used state.
2397 buf->b_arc_access = now;
2398 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2399 arc_change_state(arc_mfu, buf, hash_lock);
2401 ARCSTAT_BUMP(arcstat_mru_hits);
2402 } else if (buf->b_state == arc_mru_ghost) {
2403 arc_state_t *new_state;
2405 * This buffer has been "accessed" recently, but
2406 * was evicted from the cache. Move it to the
2410 if (buf->b_flags & ARC_PREFETCH) {
2411 new_state = arc_mru;
2412 if (refcount_count(&buf->b_refcnt) > 0)
2413 buf->b_flags &= ~ARC_PREFETCH;
2414 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2416 new_state = arc_mfu;
2417 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2420 buf->b_arc_access = ddi_get_lbolt();
2421 arc_change_state(new_state, buf, hash_lock);
2423 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2424 } else if (buf->b_state == arc_mfu) {
2426 * This buffer has been accessed more than once and is
2427 * still in the cache. Keep it in the MFU state.
2429 * NOTE: an add_reference() that occurred when we did
2430 * the arc_read() will have kicked this off the list.
2431 * If it was a prefetch, we will explicitly move it to
2432 * the head of the list now.
2434 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2435 ASSERT(refcount_count(&buf->b_refcnt) == 0);
2436 ASSERT(list_link_active(&buf->b_arc_node));
2438 ARCSTAT_BUMP(arcstat_mfu_hits);
2439 buf->b_arc_access = ddi_get_lbolt();
2440 } else if (buf->b_state == arc_mfu_ghost) {
2441 arc_state_t *new_state = arc_mfu;
2443 * This buffer has been accessed more than once but has
2444 * been evicted from the cache. Move it back to the
2448 if (buf->b_flags & ARC_PREFETCH) {
2450 * This is a prefetch access...
2451 * move this block back to the MRU state.
2453 ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0);
2454 new_state = arc_mru;
2457 buf->b_arc_access = ddi_get_lbolt();
2458 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2459 arc_change_state(new_state, buf, hash_lock);
2461 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2462 } else if (buf->b_state == arc_l2c_only) {
2464 * This buffer is on the 2nd Level ARC.
2467 buf->b_arc_access = ddi_get_lbolt();
2468 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2469 arc_change_state(arc_mfu, buf, hash_lock);
2471 ASSERT(!"invalid arc state");
2475 /* a generic arc_done_func_t which you can use */
2478 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2480 if (zio == NULL || zio->io_error == 0)
2481 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2482 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2485 /* a generic arc_done_func_t */
2487 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2489 arc_buf_t **bufp = arg;
2490 if (zio && zio->io_error) {
2491 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2495 ASSERT(buf->b_data);
2500 arc_read_done(zio_t *zio)
2502 arc_buf_hdr_t *hdr, *found;
2504 arc_buf_t *abuf; /* buffer we're assigning to callback */
2505 kmutex_t *hash_lock;
2506 arc_callback_t *callback_list, *acb;
2507 int freeable = FALSE;
2509 buf = zio->io_private;
2513 * The hdr was inserted into hash-table and removed from lists
2514 * prior to starting I/O. We should find this header, since
2515 * it's in the hash table, and it should be legit since it's
2516 * not possible to evict it during the I/O. The only possible
2517 * reason for it not to be found is if we were freed during the
2520 found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
2523 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
2524 (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2525 (found == hdr && HDR_L2_READING(hdr)));
2527 hdr->b_flags &= ~ARC_L2_EVICTED;
2528 if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2529 hdr->b_flags &= ~ARC_L2CACHE;
2531 /* byteswap if necessary */
2532 callback_list = hdr->b_acb;
2533 ASSERT(callback_list != NULL);
2534 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2535 arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
2536 byteswap_uint64_array :
2537 dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap;
2538 func(buf->b_data, hdr->b_size);
2541 arc_cksum_compute(buf, B_FALSE);
2543 if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
2545 * Only call arc_access on anonymous buffers. This is because
2546 * if we've issued an I/O for an evicted buffer, we've already
2547 * called arc_access (to prevent any simultaneous readers from
2548 * getting confused).
2550 arc_access(hdr, hash_lock);
2553 /* create copies of the data buffer for the callers */
2555 for (acb = callback_list; acb; acb = acb->acb_next) {
2556 if (acb->acb_done) {
2558 abuf = arc_buf_clone(buf);
2559 acb->acb_buf = abuf;
2564 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2565 ASSERT(!HDR_BUF_AVAILABLE(hdr));
2567 ASSERT(buf->b_efunc == NULL);
2568 ASSERT(hdr->b_datacnt == 1);
2569 hdr->b_flags |= ARC_BUF_AVAILABLE;
2572 ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2574 if (zio->io_error != 0) {
2575 hdr->b_flags |= ARC_IO_ERROR;
2576 if (hdr->b_state != arc_anon)
2577 arc_change_state(arc_anon, hdr, hash_lock);
2578 if (HDR_IN_HASH_TABLE(hdr))
2579 buf_hash_remove(hdr);
2580 freeable = refcount_is_zero(&hdr->b_refcnt);
2584 * Broadcast before we drop the hash_lock to avoid the possibility
2585 * that the hdr (and hence the cv) might be freed before we get to
2586 * the cv_broadcast().
2588 cv_broadcast(&hdr->b_cv);
2591 mutex_exit(hash_lock);
2594 * This block was freed while we waited for the read to
2595 * complete. It has been removed from the hash table and
2596 * moved to the anonymous state (so that it won't show up
2599 ASSERT3P(hdr->b_state, ==, arc_anon);
2600 freeable = refcount_is_zero(&hdr->b_refcnt);
2603 /* execute each callback and free its structure */
2604 while ((acb = callback_list) != NULL) {
2606 acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2608 if (acb->acb_zio_dummy != NULL) {
2609 acb->acb_zio_dummy->io_error = zio->io_error;
2610 zio_nowait(acb->acb_zio_dummy);
2613 callback_list = acb->acb_next;
2614 kmem_free(acb, sizeof (arc_callback_t));
2618 arc_hdr_destroy(hdr);
2622 * "Read" the block block at the specified DVA (in bp) via the
2623 * cache. If the block is found in the cache, invoke the provided
2624 * callback immediately and return. Note that the `zio' parameter
2625 * in the callback will be NULL in this case, since no IO was
2626 * required. If the block is not in the cache pass the read request
2627 * on to the spa with a substitute callback function, so that the
2628 * requested block will be added to the cache.
2630 * If a read request arrives for a block that has a read in-progress,
2631 * either wait for the in-progress read to complete (and return the
2632 * results); or, if this is a read with a "done" func, add a record
2633 * to the read to invoke the "done" func when the read completes,
2634 * and return; or just return.
2636 * arc_read_done() will invoke all the requested "done" functions
2637 * for readers of this block.
2639 * Normal callers should use arc_read and pass the arc buffer and offset
2640 * for the bp. But if you know you don't need locking, you can use
2644 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf,
2645 arc_done_func_t *done, void *private, int priority, int zio_flags,
2646 uint32_t *arc_flags, const zbookmark_t *zb)
2652 * XXX This happens from traverse callback funcs, for
2653 * the objset_phys_t block.
2655 return (arc_read_nolock(pio, spa, bp, done, private, priority,
2656 zio_flags, arc_flags, zb));
2659 ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt));
2660 ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size);
2661 rw_enter(&pbuf->b_data_lock, RW_READER);
2663 err = arc_read_nolock(pio, spa, bp, done, private, priority,
2664 zio_flags, arc_flags, zb);
2665 rw_exit(&pbuf->b_data_lock);
2671 arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp,
2672 arc_done_func_t *done, void *private, int priority, int zio_flags,
2673 uint32_t *arc_flags, const zbookmark_t *zb)
2677 kmutex_t *hash_lock;
2679 uint64_t guid = spa_guid(spa);
2682 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
2684 if (hdr && hdr->b_datacnt > 0) {
2686 *arc_flags |= ARC_CACHED;
2688 if (HDR_IO_IN_PROGRESS(hdr)) {
2690 if (*arc_flags & ARC_WAIT) {
2691 cv_wait(&hdr->b_cv, hash_lock);
2692 mutex_exit(hash_lock);
2695 ASSERT(*arc_flags & ARC_NOWAIT);
2698 arc_callback_t *acb = NULL;
2700 acb = kmem_zalloc(sizeof (arc_callback_t),
2702 acb->acb_done = done;
2703 acb->acb_private = private;
2705 acb->acb_zio_dummy = zio_null(pio,
2706 spa, NULL, NULL, NULL, zio_flags);
2708 ASSERT(acb->acb_done != NULL);
2709 acb->acb_next = hdr->b_acb;
2711 add_reference(hdr, hash_lock, private);
2712 mutex_exit(hash_lock);
2715 mutex_exit(hash_lock);
2719 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2722 add_reference(hdr, hash_lock, private);
2724 * If this block is already in use, create a new
2725 * copy of the data so that we will be guaranteed
2726 * that arc_release() will always succeed.
2730 ASSERT(buf->b_data);
2731 if (HDR_BUF_AVAILABLE(hdr)) {
2732 ASSERT(buf->b_efunc == NULL);
2733 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
2735 buf = arc_buf_clone(buf);
2738 } else if (*arc_flags & ARC_PREFETCH &&
2739 refcount_count(&hdr->b_refcnt) == 0) {
2740 hdr->b_flags |= ARC_PREFETCH;
2742 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
2743 arc_access(hdr, hash_lock);
2744 if (*arc_flags & ARC_L2CACHE)
2745 hdr->b_flags |= ARC_L2CACHE;
2746 mutex_exit(hash_lock);
2747 ARCSTAT_BUMP(arcstat_hits);
2748 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2749 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
2750 data, metadata, hits);
2753 done(NULL, buf, private);
2755 uint64_t size = BP_GET_LSIZE(bp);
2756 arc_callback_t *acb;
2759 boolean_t devw = B_FALSE;
2762 /* this block is not in the cache */
2763 arc_buf_hdr_t *exists;
2764 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
2765 buf = arc_buf_alloc(spa, size, private, type);
2767 hdr->b_dva = *BP_IDENTITY(bp);
2768 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
2769 hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
2770 exists = buf_hash_insert(hdr, &hash_lock);
2772 /* somebody beat us to the hash insert */
2773 mutex_exit(hash_lock);
2774 buf_discard_identity(hdr);
2775 (void) arc_buf_remove_ref(buf, private);
2776 goto top; /* restart the IO request */
2778 /* if this is a prefetch, we don't have a reference */
2779 if (*arc_flags & ARC_PREFETCH) {
2780 (void) remove_reference(hdr, hash_lock,
2782 hdr->b_flags |= ARC_PREFETCH;
2784 if (*arc_flags & ARC_L2CACHE)
2785 hdr->b_flags |= ARC_L2CACHE;
2786 if (BP_GET_LEVEL(bp) > 0)
2787 hdr->b_flags |= ARC_INDIRECT;
2789 /* this block is in the ghost cache */
2790 ASSERT(GHOST_STATE(hdr->b_state));
2791 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
2792 ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0);
2793 ASSERT(hdr->b_buf == NULL);
2795 /* if this is a prefetch, we don't have a reference */
2796 if (*arc_flags & ARC_PREFETCH)
2797 hdr->b_flags |= ARC_PREFETCH;
2799 add_reference(hdr, hash_lock, private);
2800 if (*arc_flags & ARC_L2CACHE)
2801 hdr->b_flags |= ARC_L2CACHE;
2802 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
2805 buf->b_efunc = NULL;
2806 buf->b_private = NULL;
2809 ASSERT(hdr->b_datacnt == 0);
2811 arc_get_data_buf(buf);
2812 arc_access(hdr, hash_lock);
2815 ASSERT(!GHOST_STATE(hdr->b_state));
2817 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
2818 acb->acb_done = done;
2819 acb->acb_private = private;
2821 ASSERT(hdr->b_acb == NULL);
2823 hdr->b_flags |= ARC_IO_IN_PROGRESS;
2825 if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
2826 (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
2827 devw = hdr->b_l2hdr->b_dev->l2ad_writing;
2828 addr = hdr->b_l2hdr->b_daddr;
2830 * Lock out device removal.
2832 if (vdev_is_dead(vd) ||
2833 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
2837 mutex_exit(hash_lock);
2839 ASSERT3U(hdr->b_size, ==, size);
2840 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
2841 uint64_t, size, zbookmark_t *, zb);
2842 ARCSTAT_BUMP(arcstat_misses);
2843 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2844 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
2845 data, metadata, misses);
2847 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
2849 * Read from the L2ARC if the following are true:
2850 * 1. The L2ARC vdev was previously cached.
2851 * 2. This buffer still has L2ARC metadata.
2852 * 3. This buffer isn't currently writing to the L2ARC.
2853 * 4. The L2ARC entry wasn't evicted, which may
2854 * also have invalidated the vdev.
2855 * 5. This isn't prefetch and l2arc_noprefetch is set.
2857 if (hdr->b_l2hdr != NULL &&
2858 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
2859 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
2860 l2arc_read_callback_t *cb;
2862 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
2863 ARCSTAT_BUMP(arcstat_l2_hits);
2865 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
2867 cb->l2rcb_buf = buf;
2868 cb->l2rcb_spa = spa;
2871 cb->l2rcb_flags = zio_flags;
2874 * l2arc read. The SCL_L2ARC lock will be
2875 * released by l2arc_read_done().
2877 rzio = zio_read_phys(pio, vd, addr, size,
2878 buf->b_data, ZIO_CHECKSUM_OFF,
2879 l2arc_read_done, cb, priority, zio_flags |
2880 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL |
2881 ZIO_FLAG_DONT_PROPAGATE |
2882 ZIO_FLAG_DONT_RETRY, B_FALSE);
2883 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
2885 ARCSTAT_INCR(arcstat_l2_read_bytes, size);
2887 if (*arc_flags & ARC_NOWAIT) {
2892 ASSERT(*arc_flags & ARC_WAIT);
2893 if (zio_wait(rzio) == 0)
2896 /* l2arc read error; goto zio_read() */
2898 DTRACE_PROBE1(l2arc__miss,
2899 arc_buf_hdr_t *, hdr);
2900 ARCSTAT_BUMP(arcstat_l2_misses);
2901 if (HDR_L2_WRITING(hdr))
2902 ARCSTAT_BUMP(arcstat_l2_rw_clash);
2903 spa_config_exit(spa, SCL_L2ARC, vd);
2907 spa_config_exit(spa, SCL_L2ARC, vd);
2908 if (l2arc_ndev != 0) {
2909 DTRACE_PROBE1(l2arc__miss,
2910 arc_buf_hdr_t *, hdr);
2911 ARCSTAT_BUMP(arcstat_l2_misses);
2915 rzio = zio_read(pio, spa, bp, buf->b_data, size,
2916 arc_read_done, buf, priority, zio_flags, zb);
2918 if (*arc_flags & ARC_WAIT)
2919 return (zio_wait(rzio));
2921 ASSERT(*arc_flags & ARC_NOWAIT);
2928 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
2930 ASSERT(buf->b_hdr != NULL);
2931 ASSERT(buf->b_hdr->b_state != arc_anon);
2932 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
2933 ASSERT(buf->b_efunc == NULL);
2934 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
2936 buf->b_efunc = func;
2937 buf->b_private = private;
2941 * This is used by the DMU to let the ARC know that a buffer is
2942 * being evicted, so the ARC should clean up. If this arc buf
2943 * is not yet in the evicted state, it will be put there.
2946 arc_buf_evict(arc_buf_t *buf)
2949 kmutex_t *hash_lock;
2952 mutex_enter(&buf->b_evict_lock);
2956 * We are in arc_do_user_evicts().
2958 ASSERT(buf->b_data == NULL);
2959 mutex_exit(&buf->b_evict_lock);
2961 } else if (buf->b_data == NULL) {
2962 arc_buf_t copy = *buf; /* structure assignment */
2964 * We are on the eviction list; process this buffer now
2965 * but let arc_do_user_evicts() do the reaping.
2967 buf->b_efunc = NULL;
2968 mutex_exit(&buf->b_evict_lock);
2969 VERIFY(copy.b_efunc(©) == 0);
2972 hash_lock = HDR_LOCK(hdr);
2973 mutex_enter(hash_lock);
2975 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
2977 ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
2978 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2981 * Pull this buffer off of the hdr
2984 while (*bufp != buf)
2985 bufp = &(*bufp)->b_next;
2986 *bufp = buf->b_next;
2988 ASSERT(buf->b_data != NULL);
2989 arc_buf_destroy(buf, FALSE, FALSE);
2991 if (hdr->b_datacnt == 0) {
2992 arc_state_t *old_state = hdr->b_state;
2993 arc_state_t *evicted_state;
2995 ASSERT(hdr->b_buf == NULL);
2996 ASSERT(refcount_is_zero(&hdr->b_refcnt));
2999 (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
3001 mutex_enter(&old_state->arcs_mtx);
3002 mutex_enter(&evicted_state->arcs_mtx);
3004 arc_change_state(evicted_state, hdr, hash_lock);
3005 ASSERT(HDR_IN_HASH_TABLE(hdr));
3006 hdr->b_flags |= ARC_IN_HASH_TABLE;
3007 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3009 mutex_exit(&evicted_state->arcs_mtx);
3010 mutex_exit(&old_state->arcs_mtx);
3012 mutex_exit(hash_lock);
3013 mutex_exit(&buf->b_evict_lock);
3015 VERIFY(buf->b_efunc(buf) == 0);
3016 buf->b_efunc = NULL;
3017 buf->b_private = NULL;
3020 kmem_cache_free(buf_cache, buf);
3025 * Release this buffer from the cache. This must be done
3026 * after a read and prior to modifying the buffer contents.
3027 * If the buffer has more than one reference, we must make
3028 * a new hdr for the buffer.
3031 arc_release(arc_buf_t *buf, void *tag)
3034 kmutex_t *hash_lock = NULL;
3035 l2arc_buf_hdr_t *l2hdr;
3039 * It would be nice to assert that if it's DMU metadata (level >
3040 * 0 || it's the dnode file), then it must be syncing context.
3041 * But we don't know that information at this level.
3044 mutex_enter(&buf->b_evict_lock);
3047 /* this buffer is not on any list */
3048 ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3050 if (hdr->b_state == arc_anon) {
3051 /* this buffer is already released */
3052 ASSERT(buf->b_efunc == NULL);
3054 hash_lock = HDR_LOCK(hdr);
3055 mutex_enter(hash_lock);
3057 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3060 l2hdr = hdr->b_l2hdr;
3062 mutex_enter(&l2arc_buflist_mtx);
3063 hdr->b_l2hdr = NULL;
3064 buf_size = hdr->b_size;
3068 * Do we have more than one buf?
3070 if (hdr->b_datacnt > 1) {
3071 arc_buf_hdr_t *nhdr;
3073 uint64_t blksz = hdr->b_size;
3074 uint64_t spa = hdr->b_spa;
3075 arc_buf_contents_t type = hdr->b_type;
3076 uint32_t flags = hdr->b_flags;
3078 ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3080 * Pull the data off of this hdr and attach it to
3081 * a new anonymous hdr.
3083 (void) remove_reference(hdr, hash_lock, tag);
3085 while (*bufp != buf)
3086 bufp = &(*bufp)->b_next;
3087 *bufp = buf->b_next;
3090 ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3091 atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3092 if (refcount_is_zero(&hdr->b_refcnt)) {
3093 uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3094 ASSERT3U(*size, >=, hdr->b_size);
3095 atomic_add_64(size, -hdr->b_size);
3097 hdr->b_datacnt -= 1;
3098 arc_cksum_verify(buf);
3100 mutex_exit(hash_lock);
3102 nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3103 nhdr->b_size = blksz;
3105 nhdr->b_type = type;
3107 nhdr->b_state = arc_anon;
3108 nhdr->b_arc_access = 0;
3109 nhdr->b_flags = flags & ARC_L2_WRITING;
3110 nhdr->b_l2hdr = NULL;
3111 nhdr->b_datacnt = 1;
3112 nhdr->b_freeze_cksum = NULL;
3113 (void) refcount_add(&nhdr->b_refcnt, tag);
3115 mutex_exit(&buf->b_evict_lock);
3116 atomic_add_64(&arc_anon->arcs_size, blksz);
3118 mutex_exit(&buf->b_evict_lock);
3119 ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3120 ASSERT(!list_link_active(&hdr->b_arc_node));
3121 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3122 if (hdr->b_state != arc_anon)
3123 arc_change_state(arc_anon, hdr, hash_lock);
3124 hdr->b_arc_access = 0;
3126 mutex_exit(hash_lock);
3128 buf_discard_identity(hdr);
3131 buf->b_efunc = NULL;
3132 buf->b_private = NULL;
3135 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3136 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3137 ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3138 mutex_exit(&l2arc_buflist_mtx);
3143 * Release this buffer. If it does not match the provided BP, fill it
3144 * with that block's contents.
3148 arc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa,
3151 arc_release(buf, tag);
3156 arc_released(arc_buf_t *buf)
3160 mutex_enter(&buf->b_evict_lock);
3161 released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3162 mutex_exit(&buf->b_evict_lock);
3167 arc_has_callback(arc_buf_t *buf)
3171 mutex_enter(&buf->b_evict_lock);
3172 callback = (buf->b_efunc != NULL);
3173 mutex_exit(&buf->b_evict_lock);
3179 arc_referenced(arc_buf_t *buf)
3183 mutex_enter(&buf->b_evict_lock);
3184 referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3185 mutex_exit(&buf->b_evict_lock);
3186 return (referenced);
3191 arc_write_ready(zio_t *zio)
3193 arc_write_callback_t *callback = zio->io_private;
3194 arc_buf_t *buf = callback->awcb_buf;
3195 arc_buf_hdr_t *hdr = buf->b_hdr;
3197 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3198 callback->awcb_ready(zio, buf, callback->awcb_private);
3201 * If the IO is already in progress, then this is a re-write
3202 * attempt, so we need to thaw and re-compute the cksum.
3203 * It is the responsibility of the callback to handle the
3204 * accounting for any re-write attempt.
3206 if (HDR_IO_IN_PROGRESS(hdr)) {
3207 mutex_enter(&hdr->b_freeze_lock);
3208 if (hdr->b_freeze_cksum != NULL) {
3209 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3210 hdr->b_freeze_cksum = NULL;
3212 mutex_exit(&hdr->b_freeze_lock);
3214 arc_cksum_compute(buf, B_FALSE);
3215 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3219 arc_write_done(zio_t *zio)
3221 arc_write_callback_t *callback = zio->io_private;
3222 arc_buf_t *buf = callback->awcb_buf;
3223 arc_buf_hdr_t *hdr = buf->b_hdr;
3225 ASSERT(hdr->b_acb == NULL);
3227 if (zio->io_error == 0) {
3228 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3229 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3230 hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3232 ASSERT(BUF_EMPTY(hdr));
3236 * If the block to be written was all-zero, we may have
3237 * compressed it away. In this case no write was performed
3238 * so there will be no dva/birth/checksum. The buffer must
3239 * therefore remain anonymous (and uncached).
3241 if (!BUF_EMPTY(hdr)) {
3242 arc_buf_hdr_t *exists;
3243 kmutex_t *hash_lock;
3245 ASSERT(zio->io_error == 0);
3247 arc_cksum_verify(buf);
3249 exists = buf_hash_insert(hdr, &hash_lock);
3252 * This can only happen if we overwrite for
3253 * sync-to-convergence, because we remove
3254 * buffers from the hash table when we arc_free().
3256 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3257 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3258 panic("bad overwrite, hdr=%p exists=%p",
3259 (void *)hdr, (void *)exists);
3260 ASSERT(refcount_is_zero(&exists->b_refcnt));
3261 arc_change_state(arc_anon, exists, hash_lock);
3262 mutex_exit(hash_lock);
3263 arc_hdr_destroy(exists);
3264 exists = buf_hash_insert(hdr, &hash_lock);
3265 ASSERT3P(exists, ==, NULL);
3268 ASSERT(hdr->b_datacnt == 1);
3269 ASSERT(hdr->b_state == arc_anon);
3270 ASSERT(BP_GET_DEDUP(zio->io_bp));
3271 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3274 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3275 /* if it's not anon, we are doing a scrub */
3276 if (!exists && hdr->b_state == arc_anon)
3277 arc_access(hdr, hash_lock);
3278 mutex_exit(hash_lock);
3280 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3283 ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3284 callback->awcb_done(zio, buf, callback->awcb_private);
3286 kmem_free(callback, sizeof (arc_write_callback_t));
3290 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3291 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
3292 arc_done_func_t *ready, arc_done_func_t *done, void *private,
3293 int priority, int zio_flags, const zbookmark_t *zb)
3295 arc_buf_hdr_t *hdr = buf->b_hdr;
3296 arc_write_callback_t *callback;
3299 ASSERT(ready != NULL);
3300 ASSERT(done != NULL);
3301 ASSERT(!HDR_IO_ERROR(hdr));
3302 ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3303 ASSERT(hdr->b_acb == NULL);
3305 hdr->b_flags |= ARC_L2CACHE;
3306 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
3307 callback->awcb_ready = ready;
3308 callback->awcb_done = done;
3309 callback->awcb_private = private;
3310 callback->awcb_buf = buf;
3312 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3313 arc_write_ready, arc_write_done, callback, priority, zio_flags, zb);
3319 arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg)
3322 uint64_t available_memory = ptob(freemem);
3323 static uint64_t page_load = 0;
3324 static uint64_t last_txg = 0;
3328 MIN(available_memory, vmem_size(heap_arena, VMEM_FREE));
3330 if (available_memory >= zfs_write_limit_max)
3333 if (txg > last_txg) {
3338 * If we are in pageout, we know that memory is already tight,
3339 * the arc is already going to be evicting, so we just want to
3340 * continue to let page writes occur as quickly as possible.
3342 if (curproc == proc_pageout) {
3343 if (page_load > MAX(ptob(minfree), available_memory) / 4)
3345 /* Note: reserve is inflated, so we deflate */
3346 page_load += reserve / 8;
3348 } else if (page_load > 0 && arc_reclaim_needed()) {
3349 /* memory is low, delay before restarting */
3350 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3355 if (arc_size > arc_c_min) {
3356 uint64_t evictable_memory =
3357 arc_mru->arcs_lsize[ARC_BUFC_DATA] +
3358 arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
3359 arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
3360 arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
3361 available_memory += MIN(evictable_memory, arc_size - arc_c_min);
3364 if (inflight_data > available_memory / 4) {
3365 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3373 arc_tempreserve_clear(uint64_t reserve)
3375 atomic_add_64(&arc_tempreserve, -reserve);
3376 ASSERT((int64_t)arc_tempreserve >= 0);
3380 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3387 * Once in a while, fail for no reason. Everything should cope.
3389 if (spa_get_random(10000) == 0) {
3390 dprintf("forcing random failure\n");
3394 if (reserve > arc_c/4 && !arc_no_grow)
3395 arc_c = MIN(arc_c_max, reserve * 4);
3396 if (reserve > arc_c)
3400 * Don't count loaned bufs as in flight dirty data to prevent long
3401 * network delays from blocking transactions that are ready to be
3402 * assigned to a txg.
3404 anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3407 * Writes will, almost always, require additional memory allocations
3408 * in order to compress/encrypt/etc the data. We therefor need to
3409 * make sure that there is sufficient available memory for this.
3411 if (error = arc_memory_throttle(reserve, anon_size, txg))
3415 * Throttle writes when the amount of dirty data in the cache
3416 * gets too large. We try to keep the cache less than half full
3417 * of dirty blocks so that our sync times don't grow too large.
3418 * Note: if two requests come in concurrently, we might let them
3419 * both succeed, when one of them should fail. Not a huge deal.
3422 if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3423 anon_size > arc_c / 4) {
3424 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3425 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3426 arc_tempreserve>>10,
3427 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3428 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3429 reserve>>10, arc_c>>10);
3432 atomic_add_64(&arc_tempreserve, reserve);
3439 mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3440 cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3442 /* Convert seconds to clock ticks */
3443 arc_min_prefetch_lifespan = 1 * hz;
3445 /* Start out with 1/8 of all memory */
3446 arc_c = physmem * PAGESIZE / 8;
3450 * On architectures where the physical memory can be larger
3451 * than the addressable space (intel in 32-bit mode), we may
3452 * need to limit the cache to 1/8 of VM size.
3454 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3457 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3458 arc_c_min = MAX(arc_c / 4, 64<<20);
3459 /* set max to 3/4 of all memory, or all but 1GB, whichever is more */
3460 if (arc_c * 8 >= 1<<30)
3461 arc_c_max = (arc_c * 8) - (1<<30);
3463 arc_c_max = arc_c_min;
3464 arc_c_max = MAX(arc_c * 6, arc_c_max);
3467 * Allow the tunables to override our calculations if they are
3468 * reasonable (ie. over 64MB)
3470 if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
3471 arc_c_max = zfs_arc_max;
3472 if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
3473 arc_c_min = zfs_arc_min;
3476 arc_p = (arc_c >> 1);
3478 /* limit meta-data to 1/4 of the arc capacity */
3479 arc_meta_limit = arc_c_max / 4;
3481 /* Allow the tunable to override if it is reasonable */
3482 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
3483 arc_meta_limit = zfs_arc_meta_limit;
3485 if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3486 arc_c_min = arc_meta_limit / 2;
3488 if (zfs_arc_grow_retry > 0)
3489 arc_grow_retry = zfs_arc_grow_retry;
3491 if (zfs_arc_shrink_shift > 0)
3492 arc_shrink_shift = zfs_arc_shrink_shift;
3494 if (zfs_arc_p_min_shift > 0)
3495 arc_p_min_shift = zfs_arc_p_min_shift;
3497 /* if kmem_flags are set, lets try to use less memory */
3498 if (kmem_debugging())
3500 if (arc_c < arc_c_min)
3503 arc_anon = &ARC_anon;
3505 arc_mru_ghost = &ARC_mru_ghost;
3507 arc_mfu_ghost = &ARC_mfu_ghost;
3508 arc_l2c_only = &ARC_l2c_only;
3511 mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3512 mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3513 mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3514 mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3515 mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3516 mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3518 list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
3519 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3520 list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
3521 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3522 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
3523 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3524 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
3525 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3526 list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
3527 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3528 list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
3529 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3530 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
3531 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3532 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
3533 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3534 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
3535 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3536 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
3537 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3541 arc_thread_exit = 0;
3542 arc_eviction_list = NULL;
3543 mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
3544 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3546 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
3547 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3549 if (arc_ksp != NULL) {
3550 arc_ksp->ks_data = &arc_stats;
3551 kstat_install(arc_ksp);
3554 (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
3555 TS_RUN, minclsyspri);
3560 if (zfs_write_limit_max == 0)
3561 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
3563 zfs_write_limit_shift = 0;
3564 mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL);
3570 mutex_enter(&arc_reclaim_thr_lock);
3571 arc_thread_exit = 1;
3572 while (arc_thread_exit != 0)
3573 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3574 mutex_exit(&arc_reclaim_thr_lock);
3580 if (arc_ksp != NULL) {
3581 kstat_delete(arc_ksp);
3585 mutex_destroy(&arc_eviction_mtx);
3586 mutex_destroy(&arc_reclaim_thr_lock);
3587 cv_destroy(&arc_reclaim_thr_cv);
3589 list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
3590 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
3591 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
3592 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
3593 list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
3594 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
3595 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
3596 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3598 mutex_destroy(&arc_anon->arcs_mtx);
3599 mutex_destroy(&arc_mru->arcs_mtx);
3600 mutex_destroy(&arc_mru_ghost->arcs_mtx);
3601 mutex_destroy(&arc_mfu->arcs_mtx);
3602 mutex_destroy(&arc_mfu_ghost->arcs_mtx);
3603 mutex_destroy(&arc_l2c_only->arcs_mtx);
3605 mutex_destroy(&zfs_write_limit_lock);
3609 ASSERT(arc_loaned_bytes == 0);
3615 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3616 * It uses dedicated storage devices to hold cached data, which are populated
3617 * using large infrequent writes. The main role of this cache is to boost
3618 * the performance of random read workloads. The intended L2ARC devices
3619 * include short-stroked disks, solid state disks, and other media with
3620 * substantially faster read latency than disk.
3622 * +-----------------------+
3624 * +-----------------------+
3627 * l2arc_feed_thread() arc_read()
3631 * +---------------+ |
3633 * +---------------+ |
3638 * +-------+ +-------+
3640 * | cache | | cache |
3641 * +-------+ +-------+
3642 * +=========+ .-----.
3643 * : L2ARC : |-_____-|
3644 * : devices : | Disks |
3645 * +=========+ `-_____-'
3647 * Read requests are satisfied from the following sources, in order:
3650 * 2) vdev cache of L2ARC devices
3652 * 4) vdev cache of disks
3655 * Some L2ARC device types exhibit extremely slow write performance.
3656 * To accommodate for this there are some significant differences between
3657 * the L2ARC and traditional cache design:
3659 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
3660 * the ARC behave as usual, freeing buffers and placing headers on ghost
3661 * lists. The ARC does not send buffers to the L2ARC during eviction as
3662 * this would add inflated write latencies for all ARC memory pressure.
3664 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
3665 * It does this by periodically scanning buffers from the eviction-end of
3666 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
3667 * not already there. It scans until a headroom of buffers is satisfied,
3668 * which itself is a buffer for ARC eviction. The thread that does this is
3669 * l2arc_feed_thread(), illustrated below; example sizes are included to
3670 * provide a better sense of ratio than this diagram:
3673 * +---------------------+----------+
3674 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
3675 * +---------------------+----------+ | o L2ARC eligible
3676 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
3677 * +---------------------+----------+ |
3678 * 15.9 Gbytes ^ 32 Mbytes |
3680 * l2arc_feed_thread()
3682 * l2arc write hand <--[oooo]--'
3686 * +==============================+
3687 * L2ARC dev |####|#|###|###| |####| ... |
3688 * +==============================+
3691 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
3692 * evicted, then the L2ARC has cached a buffer much sooner than it probably
3693 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
3694 * safe to say that this is an uncommon case, since buffers at the end of
3695 * the ARC lists have moved there due to inactivity.
3697 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
3698 * then the L2ARC simply misses copying some buffers. This serves as a
3699 * pressure valve to prevent heavy read workloads from both stalling the ARC
3700 * with waits and clogging the L2ARC with writes. This also helps prevent
3701 * the potential for the L2ARC to churn if it attempts to cache content too
3702 * quickly, such as during backups of the entire pool.
3704 * 5. After system boot and before the ARC has filled main memory, there are
3705 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
3706 * lists can remain mostly static. Instead of searching from tail of these
3707 * lists as pictured, the l2arc_feed_thread() will search from the list heads
3708 * for eligible buffers, greatly increasing its chance of finding them.
3710 * The L2ARC device write speed is also boosted during this time so that
3711 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
3712 * there are no L2ARC reads, and no fear of degrading read performance
3713 * through increased writes.
3715 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
3716 * the vdev queue can aggregate them into larger and fewer writes. Each
3717 * device is written to in a rotor fashion, sweeping writes through
3718 * available space then repeating.
3720 * 7. The L2ARC does not store dirty content. It never needs to flush
3721 * write buffers back to disk based storage.
3723 * 8. If an ARC buffer is written (and dirtied) which also exists in the
3724 * L2ARC, the now stale L2ARC buffer is immediately dropped.
3726 * The performance of the L2ARC can be tweaked by a number of tunables, which
3727 * may be necessary for different workloads:
3729 * l2arc_write_max max write bytes per interval
3730 * l2arc_write_boost extra write bytes during device warmup
3731 * l2arc_noprefetch skip caching prefetched buffers
3732 * l2arc_headroom number of max device writes to precache
3733 * l2arc_feed_secs seconds between L2ARC writing
3735 * Tunables may be removed or added as future performance improvements are
3736 * integrated, and also may become zpool properties.
3738 * There are three key functions that control how the L2ARC warms up:
3740 * l2arc_write_eligible() check if a buffer is eligible to cache
3741 * l2arc_write_size() calculate how much to write
3742 * l2arc_write_interval() calculate sleep delay between writes
3744 * These three functions determine what to write, how much, and how quickly
3749 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
3752 * A buffer is *not* eligible for the L2ARC if it:
3753 * 1. belongs to a different spa.
3754 * 2. is already cached on the L2ARC.
3755 * 3. has an I/O in progress (it may be an incomplete read).
3756 * 4. is flagged not eligible (zfs property).
3758 if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
3759 HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
3766 l2arc_write_size(l2arc_dev_t *dev)
3770 size = dev->l2ad_write;
3772 if (arc_warm == B_FALSE)
3773 size += dev->l2ad_boost;
3780 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
3782 clock_t interval, next, now;
3785 * If the ARC lists are busy, increase our write rate; if the
3786 * lists are stale, idle back. This is achieved by checking
3787 * how much we previously wrote - if it was more than half of
3788 * what we wanted, schedule the next write much sooner.
3790 if (l2arc_feed_again && wrote > (wanted / 2))
3791 interval = (hz * l2arc_feed_min_ms) / 1000;
3793 interval = hz * l2arc_feed_secs;
3795 now = ddi_get_lbolt();
3796 next = MAX(now, MIN(now + interval, began + interval));
3802 l2arc_hdr_stat_add(void)
3804 ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
3805 ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
3809 l2arc_hdr_stat_remove(void)
3811 ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
3812 ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
3816 * Cycle through L2ARC devices. This is how L2ARC load balances.
3817 * If a device is returned, this also returns holding the spa config lock.
3819 static l2arc_dev_t *
3820 l2arc_dev_get_next(void)
3822 l2arc_dev_t *first, *next = NULL;
3825 * Lock out the removal of spas (spa_namespace_lock), then removal
3826 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
3827 * both locks will be dropped and a spa config lock held instead.
3829 mutex_enter(&spa_namespace_lock);
3830 mutex_enter(&l2arc_dev_mtx);
3832 /* if there are no vdevs, there is nothing to do */
3833 if (l2arc_ndev == 0)
3837 next = l2arc_dev_last;
3839 /* loop around the list looking for a non-faulted vdev */
3841 next = list_head(l2arc_dev_list);
3843 next = list_next(l2arc_dev_list, next);
3845 next = list_head(l2arc_dev_list);
3848 /* if we have come back to the start, bail out */
3851 else if (next == first)
3854 } while (vdev_is_dead(next->l2ad_vdev));
3856 /* if we were unable to find any usable vdevs, return NULL */
3857 if (vdev_is_dead(next->l2ad_vdev))
3860 l2arc_dev_last = next;
3863 mutex_exit(&l2arc_dev_mtx);
3866 * Grab the config lock to prevent the 'next' device from being
3867 * removed while we are writing to it.
3870 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
3871 mutex_exit(&spa_namespace_lock);
3877 * Free buffers that were tagged for destruction.
3880 l2arc_do_free_on_write()
3883 l2arc_data_free_t *df, *df_prev;
3885 mutex_enter(&l2arc_free_on_write_mtx);
3886 buflist = l2arc_free_on_write;
3888 for (df = list_tail(buflist); df; df = df_prev) {
3889 df_prev = list_prev(buflist, df);
3890 ASSERT(df->l2df_data != NULL);
3891 ASSERT(df->l2df_func != NULL);
3892 df->l2df_func(df->l2df_data, df->l2df_size);
3893 list_remove(buflist, df);
3894 kmem_free(df, sizeof (l2arc_data_free_t));
3897 mutex_exit(&l2arc_free_on_write_mtx);
3901 * A write to a cache device has completed. Update all headers to allow
3902 * reads from these buffers to begin.
3905 l2arc_write_done(zio_t *zio)
3907 l2arc_write_callback_t *cb;
3910 arc_buf_hdr_t *head, *ab, *ab_prev;
3911 l2arc_buf_hdr_t *abl2;
3912 kmutex_t *hash_lock;
3914 cb = zio->io_private;
3916 dev = cb->l2wcb_dev;
3917 ASSERT(dev != NULL);
3918 head = cb->l2wcb_head;
3919 ASSERT(head != NULL);
3920 buflist = dev->l2ad_buflist;
3921 ASSERT(buflist != NULL);
3922 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
3923 l2arc_write_callback_t *, cb);
3925 if (zio->io_error != 0)
3926 ARCSTAT_BUMP(arcstat_l2_writes_error);
3928 mutex_enter(&l2arc_buflist_mtx);
3931 * All writes completed, or an error was hit.
3933 for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
3934 ab_prev = list_prev(buflist, ab);
3936 hash_lock = HDR_LOCK(ab);
3937 if (!mutex_tryenter(hash_lock)) {
3939 * This buffer misses out. It may be in a stage
3940 * of eviction. Its ARC_L2_WRITING flag will be
3941 * left set, denying reads to this buffer.
3943 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
3947 if (zio->io_error != 0) {
3949 * Error - drop L2ARC entry.
3951 list_remove(buflist, ab);
3954 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
3955 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
3959 * Allow ARC to begin reads to this L2ARC entry.
3961 ab->b_flags &= ~ARC_L2_WRITING;
3963 mutex_exit(hash_lock);
3966 atomic_inc_64(&l2arc_writes_done);
3967 list_remove(buflist, head);
3968 kmem_cache_free(hdr_cache, head);
3969 mutex_exit(&l2arc_buflist_mtx);
3971 l2arc_do_free_on_write();
3973 kmem_free(cb, sizeof (l2arc_write_callback_t));
3977 * A read to a cache device completed. Validate buffer contents before
3978 * handing over to the regular ARC routines.
3981 l2arc_read_done(zio_t *zio)
3983 l2arc_read_callback_t *cb;
3986 kmutex_t *hash_lock;
3989 ASSERT(zio->io_vd != NULL);
3990 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
3992 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
3994 cb = zio->io_private;
3996 buf = cb->l2rcb_buf;
3997 ASSERT(buf != NULL);
3999 hash_lock = HDR_LOCK(buf->b_hdr);
4000 mutex_enter(hash_lock);
4002 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4005 * Check this survived the L2ARC journey.
4007 equal = arc_cksum_equal(buf);
4008 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4009 mutex_exit(hash_lock);
4010 zio->io_private = buf;
4011 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
4012 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
4015 mutex_exit(hash_lock);
4017 * Buffer didn't survive caching. Increment stats and
4018 * reissue to the original storage device.
4020 if (zio->io_error != 0) {
4021 ARCSTAT_BUMP(arcstat_l2_io_error);
4023 zio->io_error = EIO;
4026 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4029 * If there's no waiter, issue an async i/o to the primary
4030 * storage now. If there *is* a waiter, the caller must
4031 * issue the i/o in a context where it's OK to block.
4033 if (zio->io_waiter == NULL) {
4034 zio_t *pio = zio_unique_parent(zio);
4036 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4038 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4039 buf->b_data, zio->io_size, arc_read_done, buf,
4040 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4044 kmem_free(cb, sizeof (l2arc_read_callback_t));
4048 * This is the list priority from which the L2ARC will search for pages to
4049 * cache. This is used within loops (0..3) to cycle through lists in the
4050 * desired order. This order can have a significant effect on cache
4053 * Currently the metadata lists are hit first, MFU then MRU, followed by
4054 * the data lists. This function returns a locked list, and also returns
4058 l2arc_list_locked(int list_num, kmutex_t **lock)
4062 ASSERT(list_num >= 0 && list_num <= 3);
4066 list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
4067 *lock = &arc_mfu->arcs_mtx;
4070 list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
4071 *lock = &arc_mru->arcs_mtx;
4074 list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
4075 *lock = &arc_mfu->arcs_mtx;
4078 list = &arc_mru->arcs_list[ARC_BUFC_DATA];
4079 *lock = &arc_mru->arcs_mtx;
4083 ASSERT(!(MUTEX_HELD(*lock)));
4089 * Evict buffers from the device write hand to the distance specified in
4090 * bytes. This distance may span populated buffers, it may span nothing.
4091 * This is clearing a region on the L2ARC device ready for writing.
4092 * If the 'all' boolean is set, every buffer is evicted.
4095 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4098 l2arc_buf_hdr_t *abl2;
4099 arc_buf_hdr_t *ab, *ab_prev;
4100 kmutex_t *hash_lock;
4103 buflist = dev->l2ad_buflist;
4105 if (buflist == NULL)
4108 if (!all && dev->l2ad_first) {
4110 * This is the first sweep through the device. There is
4116 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4118 * When nearing the end of the device, evict to the end
4119 * before the device write hand jumps to the start.
4121 taddr = dev->l2ad_end;
4123 taddr = dev->l2ad_hand + distance;
4125 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4126 uint64_t, taddr, boolean_t, all);
4129 mutex_enter(&l2arc_buflist_mtx);
4130 for (ab = list_tail(buflist); ab; ab = ab_prev) {
4131 ab_prev = list_prev(buflist, ab);
4133 hash_lock = HDR_LOCK(ab);
4134 if (!mutex_tryenter(hash_lock)) {
4136 * Missed the hash lock. Retry.
4138 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4139 mutex_exit(&l2arc_buflist_mtx);
4140 mutex_enter(hash_lock);
4141 mutex_exit(hash_lock);
4145 if (HDR_L2_WRITE_HEAD(ab)) {
4147 * We hit a write head node. Leave it for
4148 * l2arc_write_done().
4150 list_remove(buflist, ab);
4151 mutex_exit(hash_lock);
4155 if (!all && ab->b_l2hdr != NULL &&
4156 (ab->b_l2hdr->b_daddr > taddr ||
4157 ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4159 * We've evicted to the target address,
4160 * or the end of the device.
4162 mutex_exit(hash_lock);
4166 if (HDR_FREE_IN_PROGRESS(ab)) {
4168 * Already on the path to destruction.
4170 mutex_exit(hash_lock);
4174 if (ab->b_state == arc_l2c_only) {
4175 ASSERT(!HDR_L2_READING(ab));
4177 * This doesn't exist in the ARC. Destroy.
4178 * arc_hdr_destroy() will call list_remove()
4179 * and decrement arcstat_l2_size.
4181 arc_change_state(arc_anon, ab, hash_lock);
4182 arc_hdr_destroy(ab);
4185 * Invalidate issued or about to be issued
4186 * reads, since we may be about to write
4187 * over this location.
4189 if (HDR_L2_READING(ab)) {
4190 ARCSTAT_BUMP(arcstat_l2_evict_reading);
4191 ab->b_flags |= ARC_L2_EVICTED;
4195 * Tell ARC this no longer exists in L2ARC.
4197 if (ab->b_l2hdr != NULL) {
4200 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4201 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4203 list_remove(buflist, ab);
4206 * This may have been leftover after a
4209 ab->b_flags &= ~ARC_L2_WRITING;
4211 mutex_exit(hash_lock);
4213 mutex_exit(&l2arc_buflist_mtx);
4215 vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
4216 dev->l2ad_evict = taddr;
4220 * Find and write ARC buffers to the L2ARC device.
4222 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4223 * for reading until they have completed writing.
4226 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
4228 arc_buf_hdr_t *ab, *ab_prev, *head;
4229 l2arc_buf_hdr_t *hdrl2;
4231 uint64_t passed_sz, write_sz, buf_sz, headroom;
4233 kmutex_t *hash_lock, *list_lock;
4234 boolean_t have_lock, full;
4235 l2arc_write_callback_t *cb;
4237 uint64_t guid = spa_guid(spa);
4239 ASSERT(dev->l2ad_vdev != NULL);
4244 head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4245 head->b_flags |= ARC_L2_WRITE_HEAD;
4248 * Copy buffers for L2ARC writing.
4250 mutex_enter(&l2arc_buflist_mtx);
4251 for (int try = 0; try <= 3; try++) {
4252 list = l2arc_list_locked(try, &list_lock);
4256 * L2ARC fast warmup.
4258 * Until the ARC is warm and starts to evict, read from the
4259 * head of the ARC lists rather than the tail.
4261 headroom = target_sz * l2arc_headroom;
4262 if (arc_warm == B_FALSE)
4263 ab = list_head(list);
4265 ab = list_tail(list);
4267 for (; ab; ab = ab_prev) {
4268 if (arc_warm == B_FALSE)
4269 ab_prev = list_next(list, ab);
4271 ab_prev = list_prev(list, ab);
4273 hash_lock = HDR_LOCK(ab);
4274 have_lock = MUTEX_HELD(hash_lock);
4275 if (!have_lock && !mutex_tryenter(hash_lock)) {
4277 * Skip this buffer rather than waiting.
4282 passed_sz += ab->b_size;
4283 if (passed_sz > headroom) {
4287 mutex_exit(hash_lock);
4291 if (!l2arc_write_eligible(guid, ab)) {
4292 mutex_exit(hash_lock);
4296 if ((write_sz + ab->b_size) > target_sz) {
4298 mutex_exit(hash_lock);
4304 * Insert a dummy header on the buflist so
4305 * l2arc_write_done() can find where the
4306 * write buffers begin without searching.
4308 list_insert_head(dev->l2ad_buflist, head);
4311 sizeof (l2arc_write_callback_t), KM_SLEEP);
4312 cb->l2wcb_dev = dev;
4313 cb->l2wcb_head = head;
4314 pio = zio_root(spa, l2arc_write_done, cb,
4319 * Create and add a new L2ARC header.
4321 hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
4323 hdrl2->b_daddr = dev->l2ad_hand;
4325 ab->b_flags |= ARC_L2_WRITING;
4326 ab->b_l2hdr = hdrl2;
4327 list_insert_head(dev->l2ad_buflist, ab);
4328 buf_data = ab->b_buf->b_data;
4329 buf_sz = ab->b_size;
4332 * Compute and store the buffer cksum before
4333 * writing. On debug the cksum is verified first.
4335 arc_cksum_verify(ab->b_buf);
4336 arc_cksum_compute(ab->b_buf, B_TRUE);
4338 mutex_exit(hash_lock);
4340 wzio = zio_write_phys(pio, dev->l2ad_vdev,
4341 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
4342 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
4343 ZIO_FLAG_CANFAIL, B_FALSE);
4345 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
4347 (void) zio_nowait(wzio);
4350 * Keep the clock hand suitably device-aligned.
4352 buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
4355 dev->l2ad_hand += buf_sz;
4358 mutex_exit(list_lock);
4363 mutex_exit(&l2arc_buflist_mtx);
4366 ASSERT3U(write_sz, ==, 0);
4367 kmem_cache_free(hdr_cache, head);
4371 ASSERT3U(write_sz, <=, target_sz);
4372 ARCSTAT_BUMP(arcstat_l2_writes_sent);
4373 ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz);
4374 ARCSTAT_INCR(arcstat_l2_size, write_sz);
4375 vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0);
4378 * Bump device hand to the device start if it is approaching the end.
4379 * l2arc_evict() will already have evicted ahead for this case.
4381 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
4382 vdev_space_update(dev->l2ad_vdev,
4383 dev->l2ad_end - dev->l2ad_hand, 0, 0);
4384 dev->l2ad_hand = dev->l2ad_start;
4385 dev->l2ad_evict = dev->l2ad_start;
4386 dev->l2ad_first = B_FALSE;
4389 dev->l2ad_writing = B_TRUE;
4390 (void) zio_wait(pio);
4391 dev->l2ad_writing = B_FALSE;
4397 * This thread feeds the L2ARC at regular intervals. This is the beating
4398 * heart of the L2ARC.
4401 l2arc_feed_thread(void)
4406 uint64_t size, wrote;
4407 clock_t begin, next = ddi_get_lbolt();
4409 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
4411 mutex_enter(&l2arc_feed_thr_lock);
4413 while (l2arc_thread_exit == 0) {
4414 CALLB_CPR_SAFE_BEGIN(&cpr);
4415 (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock,
4417 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
4418 next = ddi_get_lbolt() + hz;
4421 * Quick check for L2ARC devices.
4423 mutex_enter(&l2arc_dev_mtx);
4424 if (l2arc_ndev == 0) {
4425 mutex_exit(&l2arc_dev_mtx);
4428 mutex_exit(&l2arc_dev_mtx);
4429 begin = ddi_get_lbolt();
4432 * This selects the next l2arc device to write to, and in
4433 * doing so the next spa to feed from: dev->l2ad_spa. This
4434 * will return NULL if there are now no l2arc devices or if
4435 * they are all faulted.
4437 * If a device is returned, its spa's config lock is also
4438 * held to prevent device removal. l2arc_dev_get_next()
4439 * will grab and release l2arc_dev_mtx.
4441 if ((dev = l2arc_dev_get_next()) == NULL)
4444 spa = dev->l2ad_spa;
4445 ASSERT(spa != NULL);
4448 * If the pool is read-only then force the feed thread to
4449 * sleep a little longer.
4451 if (!spa_writeable(spa)) {
4452 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
4453 spa_config_exit(spa, SCL_L2ARC, dev);
4458 * Avoid contributing to memory pressure.
4460 if (arc_reclaim_needed()) {
4461 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
4462 spa_config_exit(spa, SCL_L2ARC, dev);
4466 ARCSTAT_BUMP(arcstat_l2_feeds);
4468 size = l2arc_write_size(dev);
4471 * Evict L2ARC buffers that will be overwritten.
4473 l2arc_evict(dev, size, B_FALSE);
4476 * Write ARC buffers.
4478 wrote = l2arc_write_buffers(spa, dev, size);
4481 * Calculate interval between writes.
4483 next = l2arc_write_interval(begin, size, wrote);
4484 spa_config_exit(spa, SCL_L2ARC, dev);
4487 l2arc_thread_exit = 0;
4488 cv_broadcast(&l2arc_feed_thr_cv);
4489 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
4494 l2arc_vdev_present(vdev_t *vd)
4498 mutex_enter(&l2arc_dev_mtx);
4499 for (dev = list_head(l2arc_dev_list); dev != NULL;
4500 dev = list_next(l2arc_dev_list, dev)) {
4501 if (dev->l2ad_vdev == vd)
4504 mutex_exit(&l2arc_dev_mtx);
4506 return (dev != NULL);
4510 * Add a vdev for use by the L2ARC. By this point the spa has already
4511 * validated the vdev and opened it.
4514 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
4516 l2arc_dev_t *adddev;
4518 ASSERT(!l2arc_vdev_present(vd));
4521 * Create a new l2arc device entry.
4523 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
4524 adddev->l2ad_spa = spa;
4525 adddev->l2ad_vdev = vd;
4526 adddev->l2ad_write = l2arc_write_max;
4527 adddev->l2ad_boost = l2arc_write_boost;
4528 adddev->l2ad_start = VDEV_LABEL_START_SIZE;
4529 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
4530 adddev->l2ad_hand = adddev->l2ad_start;
4531 adddev->l2ad_evict = adddev->l2ad_start;
4532 adddev->l2ad_first = B_TRUE;
4533 adddev->l2ad_writing = B_FALSE;
4534 ASSERT3U(adddev->l2ad_write, >, 0);
4537 * This is a list of all ARC buffers that are still valid on the
4540 adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
4541 list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
4542 offsetof(arc_buf_hdr_t, b_l2node));
4544 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
4547 * Add device to global list
4549 mutex_enter(&l2arc_dev_mtx);
4550 list_insert_head(l2arc_dev_list, adddev);
4551 atomic_inc_64(&l2arc_ndev);
4552 mutex_exit(&l2arc_dev_mtx);
4556 * Remove a vdev from the L2ARC.
4559 l2arc_remove_vdev(vdev_t *vd)
4561 l2arc_dev_t *dev, *nextdev, *remdev = NULL;
4564 * Find the device by vdev
4566 mutex_enter(&l2arc_dev_mtx);
4567 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
4568 nextdev = list_next(l2arc_dev_list, dev);
4569 if (vd == dev->l2ad_vdev) {
4574 ASSERT(remdev != NULL);
4577 * Remove device from global list
4579 list_remove(l2arc_dev_list, remdev);
4580 l2arc_dev_last = NULL; /* may have been invalidated */
4581 atomic_dec_64(&l2arc_ndev);
4582 mutex_exit(&l2arc_dev_mtx);
4585 * Clear all buflists and ARC references. L2ARC device flush.
4587 l2arc_evict(remdev, 0, B_TRUE);
4588 list_destroy(remdev->l2ad_buflist);
4589 kmem_free(remdev->l2ad_buflist, sizeof (list_t));
4590 kmem_free(remdev, sizeof (l2arc_dev_t));
4596 l2arc_thread_exit = 0;
4598 l2arc_writes_sent = 0;
4599 l2arc_writes_done = 0;
4601 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
4602 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
4603 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
4604 mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
4605 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
4607 l2arc_dev_list = &L2ARC_dev_list;
4608 l2arc_free_on_write = &L2ARC_free_on_write;
4609 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
4610 offsetof(l2arc_dev_t, l2ad_node));
4611 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
4612 offsetof(l2arc_data_free_t, l2df_list_node));
4619 * This is called from dmu_fini(), which is called from spa_fini();
4620 * Because of this, we can assume that all l2arc devices have
4621 * already been removed when the pools themselves were removed.
4624 l2arc_do_free_on_write();
4626 mutex_destroy(&l2arc_feed_thr_lock);
4627 cv_destroy(&l2arc_feed_thr_cv);
4628 mutex_destroy(&l2arc_dev_mtx);
4629 mutex_destroy(&l2arc_buflist_mtx);
4630 mutex_destroy(&l2arc_free_on_write_mtx);
4632 list_destroy(l2arc_dev_list);
4633 list_destroy(l2arc_free_on_write);
4639 if (!(spa_mode_global & FWRITE))
4642 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
4643 TS_RUN, minclsyspri);
4649 if (!(spa_mode_global & FWRITE))
4652 mutex_enter(&l2arc_feed_thr_lock);
4653 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
4654 l2arc_thread_exit = 1;
4655 while (l2arc_thread_exit != 0)
4656 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
4657 mutex_exit(&l2arc_feed_thr_lock);