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 list_link_init(&buf->b_arc_node);
798 list_link_init(&buf->b_l2node);
799 arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
806 buf_cons(void *vbuf, void *unused, int kmflag)
808 arc_buf_t *buf = vbuf;
810 bzero(buf, sizeof (arc_buf_t));
811 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
812 rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL);
813 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
819 * Destructor callback - called when a cached buf is
820 * no longer required.
824 hdr_dest(void *vbuf, void *unused)
826 arc_buf_hdr_t *buf = vbuf;
828 ASSERT(BUF_EMPTY(buf));
829 refcount_destroy(&buf->b_refcnt);
830 cv_destroy(&buf->b_cv);
831 mutex_destroy(&buf->b_freeze_lock);
832 arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
837 buf_dest(void *vbuf, void *unused)
839 arc_buf_t *buf = vbuf;
841 mutex_destroy(&buf->b_evict_lock);
842 rw_destroy(&buf->b_data_lock);
843 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
847 * Reclaim callback -- invoked when memory is low.
851 hdr_recl(void *unused)
853 dprintf("hdr_recl called\n");
855 * umem calls the reclaim func when we destroy the buf cache,
856 * which is after we do arc_fini().
859 cv_signal(&arc_reclaim_thr_cv);
866 uint64_t hsize = 1ULL << 12;
870 * The hash table is big enough to fill all of physical memory
871 * with an average 64K block size. The table will take up
872 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
874 while (hsize * 65536 < physmem * PAGESIZE)
877 buf_hash_table.ht_mask = hsize - 1;
878 buf_hash_table.ht_table =
879 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
880 if (buf_hash_table.ht_table == NULL) {
881 ASSERT(hsize > (1ULL << 8));
886 hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
887 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
888 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
889 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
891 for (i = 0; i < 256; i++)
892 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
893 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
895 for (i = 0; i < BUF_LOCKS; i++) {
896 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
897 NULL, MUTEX_DEFAULT, NULL);
901 #define ARC_MINTIME (hz>>4) /* 62 ms */
904 arc_cksum_verify(arc_buf_t *buf)
908 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
911 mutex_enter(&buf->b_hdr->b_freeze_lock);
912 if (buf->b_hdr->b_freeze_cksum == NULL ||
913 (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
914 mutex_exit(&buf->b_hdr->b_freeze_lock);
917 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
918 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
919 panic("buffer modified while frozen!");
920 mutex_exit(&buf->b_hdr->b_freeze_lock);
924 arc_cksum_equal(arc_buf_t *buf)
929 mutex_enter(&buf->b_hdr->b_freeze_lock);
930 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
931 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
932 mutex_exit(&buf->b_hdr->b_freeze_lock);
938 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
940 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
943 mutex_enter(&buf->b_hdr->b_freeze_lock);
944 if (buf->b_hdr->b_freeze_cksum != NULL) {
945 mutex_exit(&buf->b_hdr->b_freeze_lock);
948 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
949 fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
950 buf->b_hdr->b_freeze_cksum);
951 mutex_exit(&buf->b_hdr->b_freeze_lock);
955 arc_buf_thaw(arc_buf_t *buf)
957 if (zfs_flags & ZFS_DEBUG_MODIFY) {
958 if (buf->b_hdr->b_state != arc_anon)
959 panic("modifying non-anon buffer!");
960 if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
961 panic("modifying buffer while i/o in progress!");
962 arc_cksum_verify(buf);
965 mutex_enter(&buf->b_hdr->b_freeze_lock);
966 if (buf->b_hdr->b_freeze_cksum != NULL) {
967 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
968 buf->b_hdr->b_freeze_cksum = NULL;
971 if (zfs_flags & ZFS_DEBUG_MODIFY) {
972 if (buf->b_hdr->b_thawed)
973 kmem_free(buf->b_hdr->b_thawed, 1);
974 buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
977 mutex_exit(&buf->b_hdr->b_freeze_lock);
981 arc_buf_freeze(arc_buf_t *buf)
985 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
988 hash_lock = HDR_LOCK(buf->b_hdr);
989 mutex_enter(hash_lock);
991 ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
992 buf->b_hdr->b_state == arc_anon);
993 arc_cksum_compute(buf, B_FALSE);
994 mutex_exit(hash_lock);
998 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1000 ASSERT(MUTEX_HELD(hash_lock));
1002 if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1003 (ab->b_state != arc_anon)) {
1004 uint64_t delta = ab->b_size * ab->b_datacnt;
1005 list_t *list = &ab->b_state->arcs_list[ab->b_type];
1006 uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1008 ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
1009 mutex_enter(&ab->b_state->arcs_mtx);
1010 ASSERT(list_link_active(&ab->b_arc_node));
1011 list_remove(list, ab);
1012 if (GHOST_STATE(ab->b_state)) {
1013 ASSERT3U(ab->b_datacnt, ==, 0);
1014 ASSERT3P(ab->b_buf, ==, NULL);
1018 ASSERT3U(*size, >=, delta);
1019 atomic_add_64(size, -delta);
1020 mutex_exit(&ab->b_state->arcs_mtx);
1021 /* remove the prefetch flag if we get a reference */
1022 if (ab->b_flags & ARC_PREFETCH)
1023 ab->b_flags &= ~ARC_PREFETCH;
1028 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1031 arc_state_t *state = ab->b_state;
1033 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1034 ASSERT(!GHOST_STATE(state));
1036 if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1037 (state != arc_anon)) {
1038 uint64_t *size = &state->arcs_lsize[ab->b_type];
1040 ASSERT(!MUTEX_HELD(&state->arcs_mtx));
1041 mutex_enter(&state->arcs_mtx);
1042 ASSERT(!list_link_active(&ab->b_arc_node));
1043 list_insert_head(&state->arcs_list[ab->b_type], ab);
1044 ASSERT(ab->b_datacnt > 0);
1045 atomic_add_64(size, ab->b_size * ab->b_datacnt);
1046 mutex_exit(&state->arcs_mtx);
1052 * Move the supplied buffer to the indicated state. The mutex
1053 * for the buffer must be held by the caller.
1056 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1058 arc_state_t *old_state = ab->b_state;
1059 int64_t refcnt = refcount_count(&ab->b_refcnt);
1060 uint64_t from_delta, to_delta;
1062 ASSERT(MUTEX_HELD(hash_lock));
1063 ASSERT(new_state != old_state);
1064 ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1065 ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1066 ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1068 from_delta = to_delta = ab->b_datacnt * ab->b_size;
1071 * If this buffer is evictable, transfer it from the
1072 * old state list to the new state list.
1075 if (old_state != arc_anon) {
1076 int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
1077 uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1080 mutex_enter(&old_state->arcs_mtx);
1082 ASSERT(list_link_active(&ab->b_arc_node));
1083 list_remove(&old_state->arcs_list[ab->b_type], ab);
1086 * If prefetching out of the ghost cache,
1087 * we will have a non-zero datacnt.
1089 if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1090 /* ghost elements have a ghost size */
1091 ASSERT(ab->b_buf == NULL);
1092 from_delta = ab->b_size;
1094 ASSERT3U(*size, >=, from_delta);
1095 atomic_add_64(size, -from_delta);
1098 mutex_exit(&old_state->arcs_mtx);
1100 if (new_state != arc_anon) {
1101 int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
1102 uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1105 mutex_enter(&new_state->arcs_mtx);
1107 list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1109 /* ghost elements have a ghost size */
1110 if (GHOST_STATE(new_state)) {
1111 ASSERT(ab->b_datacnt == 0);
1112 ASSERT(ab->b_buf == NULL);
1113 to_delta = ab->b_size;
1115 atomic_add_64(size, to_delta);
1118 mutex_exit(&new_state->arcs_mtx);
1122 ASSERT(!BUF_EMPTY(ab));
1123 if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1124 buf_hash_remove(ab);
1126 /* adjust state sizes */
1128 atomic_add_64(&new_state->arcs_size, to_delta);
1130 ASSERT3U(old_state->arcs_size, >=, from_delta);
1131 atomic_add_64(&old_state->arcs_size, -from_delta);
1133 ab->b_state = new_state;
1135 /* adjust l2arc hdr stats */
1136 if (new_state == arc_l2c_only)
1137 l2arc_hdr_stat_add();
1138 else if (old_state == arc_l2c_only)
1139 l2arc_hdr_stat_remove();
1143 arc_space_consume(uint64_t space, arc_space_type_t type)
1145 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1150 case ARC_SPACE_DATA:
1151 ARCSTAT_INCR(arcstat_data_size, space);
1153 case ARC_SPACE_OTHER:
1154 ARCSTAT_INCR(arcstat_other_size, space);
1156 case ARC_SPACE_HDRS:
1157 ARCSTAT_INCR(arcstat_hdr_size, space);
1159 case ARC_SPACE_L2HDRS:
1160 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1164 atomic_add_64(&arc_meta_used, space);
1165 atomic_add_64(&arc_size, space);
1169 arc_space_return(uint64_t space, arc_space_type_t type)
1171 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1176 case ARC_SPACE_DATA:
1177 ARCSTAT_INCR(arcstat_data_size, -space);
1179 case ARC_SPACE_OTHER:
1180 ARCSTAT_INCR(arcstat_other_size, -space);
1182 case ARC_SPACE_HDRS:
1183 ARCSTAT_INCR(arcstat_hdr_size, -space);
1185 case ARC_SPACE_L2HDRS:
1186 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1190 ASSERT(arc_meta_used >= space);
1191 if (arc_meta_max < arc_meta_used)
1192 arc_meta_max = arc_meta_used;
1193 atomic_add_64(&arc_meta_used, -space);
1194 ASSERT(arc_size >= space);
1195 atomic_add_64(&arc_size, -space);
1199 arc_data_buf_alloc(uint64_t size)
1201 if (arc_evict_needed(ARC_BUFC_DATA))
1202 cv_signal(&arc_reclaim_thr_cv);
1203 atomic_add_64(&arc_size, size);
1204 return (zio_data_buf_alloc(size));
1208 arc_data_buf_free(void *buf, uint64_t size)
1210 zio_data_buf_free(buf, size);
1211 ASSERT(arc_size >= size);
1212 atomic_add_64(&arc_size, -size);
1216 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1221 ASSERT3U(size, >, 0);
1222 hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1223 ASSERT(BUF_EMPTY(hdr));
1226 hdr->b_spa = spa_guid(spa);
1227 hdr->b_state = arc_anon;
1228 hdr->b_arc_access = 0;
1229 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1232 buf->b_efunc = NULL;
1233 buf->b_private = NULL;
1236 arc_get_data_buf(buf);
1239 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1240 (void) refcount_add(&hdr->b_refcnt, tag);
1245 static char *arc_onloan_tag = "onloan";
1248 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1249 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1250 * buffers must be returned to the arc before they can be used by the DMU or
1254 arc_loan_buf(spa_t *spa, int size)
1258 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1260 atomic_add_64(&arc_loaned_bytes, size);
1265 * Return a loaned arc buffer to the arc.
1268 arc_return_buf(arc_buf_t *buf, void *tag)
1270 arc_buf_hdr_t *hdr = buf->b_hdr;
1272 ASSERT(buf->b_data != NULL);
1273 (void) refcount_add(&hdr->b_refcnt, tag);
1274 (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1276 atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1279 /* Detach an arc_buf from a dbuf (tag) */
1281 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1285 ASSERT(buf->b_data != NULL);
1287 (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1288 (void) refcount_remove(&hdr->b_refcnt, tag);
1289 buf->b_efunc = NULL;
1290 buf->b_private = NULL;
1292 atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1296 arc_buf_clone(arc_buf_t *from)
1299 arc_buf_hdr_t *hdr = from->b_hdr;
1300 uint64_t size = hdr->b_size;
1302 ASSERT(hdr->b_state != arc_anon);
1304 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1307 buf->b_efunc = NULL;
1308 buf->b_private = NULL;
1309 buf->b_next = hdr->b_buf;
1311 arc_get_data_buf(buf);
1312 bcopy(from->b_data, buf->b_data, size);
1313 hdr->b_datacnt += 1;
1318 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1321 kmutex_t *hash_lock;
1324 * Check to see if this buffer is evicted. Callers
1325 * must verify b_data != NULL to know if the add_ref
1328 mutex_enter(&buf->b_evict_lock);
1329 if (buf->b_data == NULL) {
1330 mutex_exit(&buf->b_evict_lock);
1333 hash_lock = HDR_LOCK(buf->b_hdr);
1334 mutex_enter(hash_lock);
1336 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1337 mutex_exit(&buf->b_evict_lock);
1339 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1340 add_reference(hdr, hash_lock, tag);
1341 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1342 arc_access(hdr, hash_lock);
1343 mutex_exit(hash_lock);
1344 ARCSTAT_BUMP(arcstat_hits);
1345 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1346 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1347 data, metadata, hits);
1351 * Free the arc data buffer. If it is an l2arc write in progress,
1352 * the buffer is placed on l2arc_free_on_write to be freed later.
1355 arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t),
1356 void *data, size_t size)
1358 if (HDR_L2_WRITING(hdr)) {
1359 l2arc_data_free_t *df;
1360 df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
1361 df->l2df_data = data;
1362 df->l2df_size = size;
1363 df->l2df_func = free_func;
1364 mutex_enter(&l2arc_free_on_write_mtx);
1365 list_insert_head(l2arc_free_on_write, df);
1366 mutex_exit(&l2arc_free_on_write_mtx);
1367 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1369 free_func(data, size);
1374 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1378 /* free up data associated with the buf */
1380 arc_state_t *state = buf->b_hdr->b_state;
1381 uint64_t size = buf->b_hdr->b_size;
1382 arc_buf_contents_t type = buf->b_hdr->b_type;
1384 arc_cksum_verify(buf);
1387 if (type == ARC_BUFC_METADATA) {
1388 arc_buf_data_free(buf->b_hdr, zio_buf_free,
1390 arc_space_return(size, ARC_SPACE_DATA);
1392 ASSERT(type == ARC_BUFC_DATA);
1393 arc_buf_data_free(buf->b_hdr,
1394 zio_data_buf_free, buf->b_data, size);
1395 ARCSTAT_INCR(arcstat_data_size, -size);
1396 atomic_add_64(&arc_size, -size);
1399 if (list_link_active(&buf->b_hdr->b_arc_node)) {
1400 uint64_t *cnt = &state->arcs_lsize[type];
1402 ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1403 ASSERT(state != arc_anon);
1405 ASSERT3U(*cnt, >=, size);
1406 atomic_add_64(cnt, -size);
1408 ASSERT3U(state->arcs_size, >=, size);
1409 atomic_add_64(&state->arcs_size, -size);
1411 ASSERT(buf->b_hdr->b_datacnt > 0);
1412 buf->b_hdr->b_datacnt -= 1;
1415 /* only remove the buf if requested */
1419 /* remove the buf from the hdr list */
1420 for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1422 *bufp = buf->b_next;
1425 ASSERT(buf->b_efunc == NULL);
1427 /* clean up the buf */
1429 kmem_cache_free(buf_cache, buf);
1433 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1435 l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1437 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1438 ASSERT3P(hdr->b_state, ==, arc_anon);
1439 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1441 if (l2hdr != NULL) {
1442 boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1444 * To prevent arc_free() and l2arc_evict() from
1445 * attempting to free the same buffer at the same time,
1446 * a FREE_IN_PROGRESS flag is given to arc_free() to
1447 * give it priority. l2arc_evict() can't destroy this
1448 * header while we are waiting on l2arc_buflist_mtx.
1450 * The hdr may be removed from l2ad_buflist before we
1451 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1453 if (!buflist_held) {
1454 mutex_enter(&l2arc_buflist_mtx);
1455 l2hdr = hdr->b_l2hdr;
1458 if (l2hdr != NULL) {
1459 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1460 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1461 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1462 if (hdr->b_state == arc_l2c_only)
1463 l2arc_hdr_stat_remove();
1464 hdr->b_l2hdr = NULL;
1468 mutex_exit(&l2arc_buflist_mtx);
1471 if (!BUF_EMPTY(hdr)) {
1472 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1473 buf_discard_identity(hdr);
1475 while (hdr->b_buf) {
1476 arc_buf_t *buf = hdr->b_buf;
1479 mutex_enter(&arc_eviction_mtx);
1480 mutex_enter(&buf->b_evict_lock);
1481 ASSERT(buf->b_hdr != NULL);
1482 arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1483 hdr->b_buf = buf->b_next;
1484 buf->b_hdr = &arc_eviction_hdr;
1485 buf->b_next = arc_eviction_list;
1486 arc_eviction_list = buf;
1487 mutex_exit(&buf->b_evict_lock);
1488 mutex_exit(&arc_eviction_mtx);
1490 arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1493 if (hdr->b_freeze_cksum != NULL) {
1494 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1495 hdr->b_freeze_cksum = NULL;
1497 if (hdr->b_thawed) {
1498 kmem_free(hdr->b_thawed, 1);
1499 hdr->b_thawed = NULL;
1502 ASSERT(!list_link_active(&hdr->b_arc_node));
1503 ASSERT3P(hdr->b_hash_next, ==, NULL);
1504 ASSERT3P(hdr->b_acb, ==, NULL);
1505 kmem_cache_free(hdr_cache, hdr);
1509 arc_buf_free(arc_buf_t *buf, void *tag)
1511 arc_buf_hdr_t *hdr = buf->b_hdr;
1512 int hashed = hdr->b_state != arc_anon;
1514 ASSERT(buf->b_efunc == NULL);
1515 ASSERT(buf->b_data != NULL);
1518 kmutex_t *hash_lock = HDR_LOCK(hdr);
1520 mutex_enter(hash_lock);
1522 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1524 (void) remove_reference(hdr, hash_lock, tag);
1525 if (hdr->b_datacnt > 1) {
1526 arc_buf_destroy(buf, FALSE, TRUE);
1528 ASSERT(buf == hdr->b_buf);
1529 ASSERT(buf->b_efunc == NULL);
1530 hdr->b_flags |= ARC_BUF_AVAILABLE;
1532 mutex_exit(hash_lock);
1533 } else if (HDR_IO_IN_PROGRESS(hdr)) {
1536 * We are in the middle of an async write. Don't destroy
1537 * this buffer unless the write completes before we finish
1538 * decrementing the reference count.
1540 mutex_enter(&arc_eviction_mtx);
1541 (void) remove_reference(hdr, NULL, tag);
1542 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1543 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1544 mutex_exit(&arc_eviction_mtx);
1546 arc_hdr_destroy(hdr);
1548 if (remove_reference(hdr, NULL, tag) > 0)
1549 arc_buf_destroy(buf, FALSE, TRUE);
1551 arc_hdr_destroy(hdr);
1556 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1558 arc_buf_hdr_t *hdr = buf->b_hdr;
1559 kmutex_t *hash_lock = HDR_LOCK(hdr);
1560 int no_callback = (buf->b_efunc == NULL);
1562 if (hdr->b_state == arc_anon) {
1563 ASSERT(hdr->b_datacnt == 1);
1564 arc_buf_free(buf, tag);
1565 return (no_callback);
1568 mutex_enter(hash_lock);
1570 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1571 ASSERT(hdr->b_state != arc_anon);
1572 ASSERT(buf->b_data != NULL);
1574 (void) remove_reference(hdr, hash_lock, tag);
1575 if (hdr->b_datacnt > 1) {
1577 arc_buf_destroy(buf, FALSE, TRUE);
1578 } else if (no_callback) {
1579 ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1580 ASSERT(buf->b_efunc == NULL);
1581 hdr->b_flags |= ARC_BUF_AVAILABLE;
1583 ASSERT(no_callback || hdr->b_datacnt > 1 ||
1584 refcount_is_zero(&hdr->b_refcnt));
1585 mutex_exit(hash_lock);
1586 return (no_callback);
1590 arc_buf_size(arc_buf_t *buf)
1592 return (buf->b_hdr->b_size);
1596 * Evict buffers from list until we've removed the specified number of
1597 * bytes. Move the removed buffers to the appropriate evict state.
1598 * If the recycle flag is set, then attempt to "recycle" a buffer:
1599 * - look for a buffer to evict that is `bytes' long.
1600 * - return the data block from this buffer rather than freeing it.
1601 * This flag is used by callers that are trying to make space for a
1602 * new buffer in a full arc cache.
1604 * This function makes a "best effort". It skips over any buffers
1605 * it can't get a hash_lock on, and so may not catch all candidates.
1606 * It may also return without evicting as much space as requested.
1609 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1610 arc_buf_contents_t type)
1612 arc_state_t *evicted_state;
1613 uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1614 arc_buf_hdr_t *ab, *ab_prev = NULL;
1615 list_t *list = &state->arcs_list[type];
1616 kmutex_t *hash_lock;
1617 boolean_t have_lock;
1618 void *stolen = NULL;
1620 ASSERT(state == arc_mru || state == arc_mfu);
1622 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1624 mutex_enter(&state->arcs_mtx);
1625 mutex_enter(&evicted_state->arcs_mtx);
1627 for (ab = list_tail(list); ab; ab = ab_prev) {
1628 ab_prev = list_prev(list, ab);
1629 /* prefetch buffers have a minimum lifespan */
1630 if (HDR_IO_IN_PROGRESS(ab) ||
1631 (spa && ab->b_spa != spa) ||
1632 (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1633 ddi_get_lbolt() - ab->b_arc_access <
1634 arc_min_prefetch_lifespan)) {
1638 /* "lookahead" for better eviction candidate */
1639 if (recycle && ab->b_size != bytes &&
1640 ab_prev && ab_prev->b_size == bytes)
1642 hash_lock = HDR_LOCK(ab);
1643 have_lock = MUTEX_HELD(hash_lock);
1644 if (have_lock || mutex_tryenter(hash_lock)) {
1645 ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0);
1646 ASSERT(ab->b_datacnt > 0);
1648 arc_buf_t *buf = ab->b_buf;
1649 if (!mutex_tryenter(&buf->b_evict_lock)) {
1654 bytes_evicted += ab->b_size;
1655 if (recycle && ab->b_type == type &&
1656 ab->b_size == bytes &&
1657 !HDR_L2_WRITING(ab)) {
1658 stolen = buf->b_data;
1663 mutex_enter(&arc_eviction_mtx);
1664 arc_buf_destroy(buf,
1665 buf->b_data == stolen, FALSE);
1666 ab->b_buf = buf->b_next;
1667 buf->b_hdr = &arc_eviction_hdr;
1668 buf->b_next = arc_eviction_list;
1669 arc_eviction_list = buf;
1670 mutex_exit(&arc_eviction_mtx);
1671 mutex_exit(&buf->b_evict_lock);
1673 mutex_exit(&buf->b_evict_lock);
1674 arc_buf_destroy(buf,
1675 buf->b_data == stolen, TRUE);
1680 ARCSTAT_INCR(arcstat_evict_l2_cached,
1683 if (l2arc_write_eligible(ab->b_spa, ab)) {
1684 ARCSTAT_INCR(arcstat_evict_l2_eligible,
1688 arcstat_evict_l2_ineligible,
1693 if (ab->b_datacnt == 0) {
1694 arc_change_state(evicted_state, ab, hash_lock);
1695 ASSERT(HDR_IN_HASH_TABLE(ab));
1696 ab->b_flags |= ARC_IN_HASH_TABLE;
1697 ab->b_flags &= ~ARC_BUF_AVAILABLE;
1698 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1701 mutex_exit(hash_lock);
1702 if (bytes >= 0 && bytes_evicted >= bytes)
1709 mutex_exit(&evicted_state->arcs_mtx);
1710 mutex_exit(&state->arcs_mtx);
1712 if (bytes_evicted < bytes)
1713 dprintf("only evicted %lld bytes from %x\n",
1714 (longlong_t)bytes_evicted, state);
1717 ARCSTAT_INCR(arcstat_evict_skip, skipped);
1720 ARCSTAT_INCR(arcstat_mutex_miss, missed);
1723 * We have just evicted some date into the ghost state, make
1724 * sure we also adjust the ghost state size if necessary.
1727 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
1728 int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
1729 arc_mru_ghost->arcs_size - arc_c;
1731 if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
1733 MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
1734 arc_evict_ghost(arc_mru_ghost, 0, todelete);
1735 } else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
1736 int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
1737 arc_mru_ghost->arcs_size +
1738 arc_mfu_ghost->arcs_size - arc_c);
1739 arc_evict_ghost(arc_mfu_ghost, 0, todelete);
1747 * Remove buffers from list until we've removed the specified number of
1748 * bytes. Destroy the buffers that are removed.
1751 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
1753 arc_buf_hdr_t *ab, *ab_prev;
1754 arc_buf_hdr_t marker;
1755 list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1756 kmutex_t *hash_lock;
1757 uint64_t bytes_deleted = 0;
1758 uint64_t bufs_skipped = 0;
1760 ASSERT(GHOST_STATE(state));
1761 bzero(&marker, sizeof(marker));
1763 mutex_enter(&state->arcs_mtx);
1764 for (ab = list_tail(list); ab; ab = ab_prev) {
1765 ab_prev = list_prev(list, ab);
1766 if (spa && ab->b_spa != spa)
1769 /* ignore markers */
1773 hash_lock = HDR_LOCK(ab);
1774 /* caller may be trying to modify this buffer, skip it */
1775 if (MUTEX_HELD(hash_lock))
1777 if (mutex_tryenter(hash_lock)) {
1778 ASSERT(!HDR_IO_IN_PROGRESS(ab));
1779 ASSERT(ab->b_buf == NULL);
1780 ARCSTAT_BUMP(arcstat_deleted);
1781 bytes_deleted += ab->b_size;
1783 if (ab->b_l2hdr != NULL) {
1785 * This buffer is cached on the 2nd Level ARC;
1786 * don't destroy the header.
1788 arc_change_state(arc_l2c_only, ab, hash_lock);
1789 mutex_exit(hash_lock);
1791 arc_change_state(arc_anon, ab, hash_lock);
1792 mutex_exit(hash_lock);
1793 arc_hdr_destroy(ab);
1796 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1797 if (bytes >= 0 && bytes_deleted >= bytes)
1799 } else if (bytes < 0) {
1801 * Insert a list marker and then wait for the
1802 * hash lock to become available. Once its
1803 * available, restart from where we left off.
1805 list_insert_after(list, ab, &marker);
1806 mutex_exit(&state->arcs_mtx);
1807 mutex_enter(hash_lock);
1808 mutex_exit(hash_lock);
1809 mutex_enter(&state->arcs_mtx);
1810 ab_prev = list_prev(list, &marker);
1811 list_remove(list, &marker);
1815 mutex_exit(&state->arcs_mtx);
1817 if (list == &state->arcs_list[ARC_BUFC_DATA] &&
1818 (bytes < 0 || bytes_deleted < bytes)) {
1819 list = &state->arcs_list[ARC_BUFC_METADATA];
1824 ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
1828 if (bytes_deleted < bytes)
1829 dprintf("only deleted %lld bytes from %p\n",
1830 (longlong_t)bytes_deleted, state);
1836 int64_t adjustment, delta;
1842 adjustment = MIN((int64_t)(arc_size - arc_c),
1843 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
1846 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
1847 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
1848 (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA);
1849 adjustment -= delta;
1852 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1853 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
1854 (void) arc_evict(arc_mru, 0, delta, FALSE,
1862 adjustment = arc_size - arc_c;
1864 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
1865 delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
1866 (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA);
1867 adjustment -= delta;
1870 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1871 int64_t delta = MIN(adjustment,
1872 arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
1873 (void) arc_evict(arc_mfu, 0, delta, FALSE,
1878 * Adjust ghost lists
1881 adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
1883 if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
1884 delta = MIN(arc_mru_ghost->arcs_size, adjustment);
1885 arc_evict_ghost(arc_mru_ghost, 0, delta);
1889 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
1891 if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
1892 delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
1893 arc_evict_ghost(arc_mfu_ghost, 0, delta);
1898 arc_do_user_evicts(void)
1900 mutex_enter(&arc_eviction_mtx);
1901 while (arc_eviction_list != NULL) {
1902 arc_buf_t *buf = arc_eviction_list;
1903 arc_eviction_list = buf->b_next;
1904 mutex_enter(&buf->b_evict_lock);
1906 mutex_exit(&buf->b_evict_lock);
1907 mutex_exit(&arc_eviction_mtx);
1909 if (buf->b_efunc != NULL)
1910 VERIFY(buf->b_efunc(buf) == 0);
1912 buf->b_efunc = NULL;
1913 buf->b_private = NULL;
1914 kmem_cache_free(buf_cache, buf);
1915 mutex_enter(&arc_eviction_mtx);
1917 mutex_exit(&arc_eviction_mtx);
1921 * Flush all *evictable* data from the cache for the given spa.
1922 * NOTE: this will not touch "active" (i.e. referenced) data.
1925 arc_flush(spa_t *spa)
1930 guid = spa_guid(spa);
1932 while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
1933 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
1937 while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
1938 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
1942 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
1943 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
1947 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
1948 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
1953 arc_evict_ghost(arc_mru_ghost, guid, -1);
1954 arc_evict_ghost(arc_mfu_ghost, guid, -1);
1956 mutex_enter(&arc_reclaim_thr_lock);
1957 arc_do_user_evicts();
1958 mutex_exit(&arc_reclaim_thr_lock);
1959 ASSERT(spa || arc_eviction_list == NULL);
1965 if (arc_c > arc_c_min) {
1969 to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree));
1971 to_free = arc_c >> arc_shrink_shift;
1973 if (arc_c > arc_c_min + to_free)
1974 atomic_add_64(&arc_c, -to_free);
1978 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
1979 if (arc_c > arc_size)
1980 arc_c = MAX(arc_size, arc_c_min);
1982 arc_p = (arc_c >> 1);
1983 ASSERT(arc_c >= arc_c_min);
1984 ASSERT((int64_t)arc_p >= 0);
1987 if (arc_size > arc_c)
1992 arc_reclaim_needed(void)
2001 * take 'desfree' extra pages, so we reclaim sooner, rather than later
2006 * check that we're out of range of the pageout scanner. It starts to
2007 * schedule paging if freemem is less than lotsfree and needfree.
2008 * lotsfree is the high-water mark for pageout, and needfree is the
2009 * number of needed free pages. We add extra pages here to make sure
2010 * the scanner doesn't start up while we're freeing memory.
2012 if (freemem < lotsfree + needfree + extra)
2016 * check to make sure that swapfs has enough space so that anon
2017 * reservations can still succeed. anon_resvmem() checks that the
2018 * availrmem is greater than swapfs_minfree, and the number of reserved
2019 * swap pages. We also add a bit of extra here just to prevent
2020 * circumstances from getting really dire.
2022 if (availrmem < swapfs_minfree + swapfs_reserve + extra)
2027 * If we're on an i386 platform, it's possible that we'll exhaust the
2028 * kernel heap space before we ever run out of available physical
2029 * memory. Most checks of the size of the heap_area compare against
2030 * tune.t_minarmem, which is the minimum available real memory that we
2031 * can have in the system. However, this is generally fixed at 25 pages
2032 * which is so low that it's useless. In this comparison, we seek to
2033 * calculate the total heap-size, and reclaim if more than 3/4ths of the
2034 * heap is allocated. (Or, in the calculation, if less than 1/4th is
2037 if (btop(vmem_size(heap_arena, VMEM_FREE)) <
2038 (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
2043 if (spa_get_random(100) == 0)
2050 arc_kmem_reap_now(arc_reclaim_strategy_t strat)
2053 kmem_cache_t *prev_cache = NULL;
2054 kmem_cache_t *prev_data_cache = NULL;
2055 extern kmem_cache_t *zio_buf_cache[];
2056 extern kmem_cache_t *zio_data_buf_cache[];
2059 if (arc_meta_used >= arc_meta_limit) {
2061 * We are exceeding our meta-data cache limit.
2062 * Purge some DNLC entries to release holds on meta-data.
2064 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
2068 * Reclaim unused memory from all kmem caches.
2075 * An aggressive reclamation will shrink the cache size as well as
2076 * reap free buffers from the arc kmem caches.
2078 if (strat == ARC_RECLAIM_AGGR)
2081 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2082 if (zio_buf_cache[i] != prev_cache) {
2083 prev_cache = zio_buf_cache[i];
2084 kmem_cache_reap_now(zio_buf_cache[i]);
2086 if (zio_data_buf_cache[i] != prev_data_cache) {
2087 prev_data_cache = zio_data_buf_cache[i];
2088 kmem_cache_reap_now(zio_data_buf_cache[i]);
2091 kmem_cache_reap_now(buf_cache);
2092 kmem_cache_reap_now(hdr_cache);
2096 arc_reclaim_thread(void)
2098 clock_t growtime = 0;
2099 arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS;
2102 CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2104 mutex_enter(&arc_reclaim_thr_lock);
2105 while (arc_thread_exit == 0) {
2106 if (arc_reclaim_needed()) {
2109 if (last_reclaim == ARC_RECLAIM_CONS) {
2110 last_reclaim = ARC_RECLAIM_AGGR;
2112 last_reclaim = ARC_RECLAIM_CONS;
2116 last_reclaim = ARC_RECLAIM_AGGR;
2120 /* reset the growth delay for every reclaim */
2121 growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
2123 arc_kmem_reap_now(last_reclaim);
2126 } else if (arc_no_grow && ddi_get_lbolt() >= growtime) {
2127 arc_no_grow = FALSE;
2132 if (arc_eviction_list != NULL)
2133 arc_do_user_evicts();
2135 /* block until needed, or one second, whichever is shorter */
2136 CALLB_CPR_SAFE_BEGIN(&cpr);
2137 (void) cv_timedwait(&arc_reclaim_thr_cv,
2138 &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2139 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2142 arc_thread_exit = 0;
2143 cv_broadcast(&arc_reclaim_thr_cv);
2144 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */
2149 * Adapt arc info given the number of bytes we are trying to add and
2150 * the state that we are comming from. This function is only called
2151 * when we are adding new content to the cache.
2154 arc_adapt(int bytes, arc_state_t *state)
2157 uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
2159 if (state == arc_l2c_only)
2164 * Adapt the target size of the MRU list:
2165 * - if we just hit in the MRU ghost list, then increase
2166 * the target size of the MRU list.
2167 * - if we just hit in the MFU ghost list, then increase
2168 * the target size of the MFU list by decreasing the
2169 * target size of the MRU list.
2171 if (state == arc_mru_ghost) {
2172 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2173 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2174 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2176 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2177 } else if (state == arc_mfu_ghost) {
2180 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2181 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2182 mult = MIN(mult, 10);
2184 delta = MIN(bytes * mult, arc_p);
2185 arc_p = MAX(arc_p_min, arc_p - delta);
2187 ASSERT((int64_t)arc_p >= 0);
2189 if (arc_reclaim_needed()) {
2190 cv_signal(&arc_reclaim_thr_cv);
2197 if (arc_c >= arc_c_max)
2201 * If we're within (2 * maxblocksize) bytes of the target
2202 * cache size, increment the target cache size
2204 if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2205 atomic_add_64(&arc_c, (int64_t)bytes);
2206 if (arc_c > arc_c_max)
2208 else if (state == arc_anon)
2209 atomic_add_64(&arc_p, (int64_t)bytes);
2213 ASSERT((int64_t)arc_p >= 0);
2217 * Check if the cache has reached its limits and eviction is required
2221 arc_evict_needed(arc_buf_contents_t type)
2223 if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2228 * If zio data pages are being allocated out of a separate heap segment,
2229 * then enforce that the size of available vmem for this area remains
2230 * above about 1/32nd free.
2232 if (type == ARC_BUFC_DATA && zio_arena != NULL &&
2233 vmem_size(zio_arena, VMEM_FREE) <
2234 (vmem_size(zio_arena, VMEM_ALLOC) >> 5))
2238 if (arc_reclaim_needed())
2241 return (arc_size > arc_c);
2245 * The buffer, supplied as the first argument, needs a data block.
2246 * So, if we are at cache max, determine which cache should be victimized.
2247 * We have the following cases:
2249 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2250 * In this situation if we're out of space, but the resident size of the MFU is
2251 * under the limit, victimize the MFU cache to satisfy this insertion request.
2253 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2254 * Here, we've used up all of the available space for the MRU, so we need to
2255 * evict from our own cache instead. Evict from the set of resident MRU
2258 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2259 * c minus p represents the MFU space in the cache, since p is the size of the
2260 * cache that is dedicated to the MRU. In this situation there's still space on
2261 * the MFU side, so the MRU side needs to be victimized.
2263 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2264 * MFU's resident set is consuming more space than it has been allotted. In
2265 * this situation, we must victimize our own cache, the MFU, for this insertion.
2268 arc_get_data_buf(arc_buf_t *buf)
2270 arc_state_t *state = buf->b_hdr->b_state;
2271 uint64_t size = buf->b_hdr->b_size;
2272 arc_buf_contents_t type = buf->b_hdr->b_type;
2274 arc_adapt(size, state);
2277 * We have not yet reached cache maximum size,
2278 * just allocate a new buffer.
2280 if (!arc_evict_needed(type)) {
2281 if (type == ARC_BUFC_METADATA) {
2282 buf->b_data = zio_buf_alloc(size);
2283 arc_space_consume(size, ARC_SPACE_DATA);
2285 ASSERT(type == ARC_BUFC_DATA);
2286 buf->b_data = zio_data_buf_alloc(size);
2287 ARCSTAT_INCR(arcstat_data_size, size);
2288 atomic_add_64(&arc_size, size);
2294 * If we are prefetching from the mfu ghost list, this buffer
2295 * will end up on the mru list; so steal space from there.
2297 if (state == arc_mfu_ghost)
2298 state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2299 else if (state == arc_mru_ghost)
2302 if (state == arc_mru || state == arc_anon) {
2303 uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2304 state = (arc_mfu->arcs_lsize[type] >= size &&
2305 arc_p > mru_used) ? arc_mfu : arc_mru;
2308 uint64_t mfu_space = arc_c - arc_p;
2309 state = (arc_mru->arcs_lsize[type] >= size &&
2310 mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2312 if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) {
2313 if (type == ARC_BUFC_METADATA) {
2314 buf->b_data = zio_buf_alloc(size);
2315 arc_space_consume(size, ARC_SPACE_DATA);
2317 ASSERT(type == ARC_BUFC_DATA);
2318 buf->b_data = zio_data_buf_alloc(size);
2319 ARCSTAT_INCR(arcstat_data_size, size);
2320 atomic_add_64(&arc_size, size);
2322 ARCSTAT_BUMP(arcstat_recycle_miss);
2324 ASSERT(buf->b_data != NULL);
2327 * Update the state size. Note that ghost states have a
2328 * "ghost size" and so don't need to be updated.
2330 if (!GHOST_STATE(buf->b_hdr->b_state)) {
2331 arc_buf_hdr_t *hdr = buf->b_hdr;
2333 atomic_add_64(&hdr->b_state->arcs_size, size);
2334 if (list_link_active(&hdr->b_arc_node)) {
2335 ASSERT(refcount_is_zero(&hdr->b_refcnt));
2336 atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2339 * If we are growing the cache, and we are adding anonymous
2340 * data, and we have outgrown arc_p, update arc_p
2342 if (arc_size < arc_c && hdr->b_state == arc_anon &&
2343 arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2344 arc_p = MIN(arc_c, arc_p + size);
2349 * This routine is called whenever a buffer is accessed.
2350 * NOTE: the hash lock is dropped in this function.
2353 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2357 ASSERT(MUTEX_HELD(hash_lock));
2359 if (buf->b_state == arc_anon) {
2361 * This buffer is not in the cache, and does not
2362 * appear in our "ghost" list. Add the new buffer
2366 ASSERT(buf->b_arc_access == 0);
2367 buf->b_arc_access = ddi_get_lbolt();
2368 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2369 arc_change_state(arc_mru, buf, hash_lock);
2371 } else if (buf->b_state == arc_mru) {
2372 now = ddi_get_lbolt();
2375 * If this buffer is here because of a prefetch, then either:
2376 * - clear the flag if this is a "referencing" read
2377 * (any subsequent access will bump this into the MFU state).
2379 * - move the buffer to the head of the list if this is
2380 * another prefetch (to make it less likely to be evicted).
2382 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2383 if (refcount_count(&buf->b_refcnt) == 0) {
2384 ASSERT(list_link_active(&buf->b_arc_node));
2386 buf->b_flags &= ~ARC_PREFETCH;
2387 ARCSTAT_BUMP(arcstat_mru_hits);
2389 buf->b_arc_access = now;
2394 * This buffer has been "accessed" only once so far,
2395 * but it is still in the cache. Move it to the MFU
2398 if (now > buf->b_arc_access + ARC_MINTIME) {
2400 * More than 125ms have passed since we
2401 * instantiated this buffer. Move it to the
2402 * most frequently used state.
2404 buf->b_arc_access = now;
2405 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2406 arc_change_state(arc_mfu, buf, hash_lock);
2408 ARCSTAT_BUMP(arcstat_mru_hits);
2409 } else if (buf->b_state == arc_mru_ghost) {
2410 arc_state_t *new_state;
2412 * This buffer has been "accessed" recently, but
2413 * was evicted from the cache. Move it to the
2417 if (buf->b_flags & ARC_PREFETCH) {
2418 new_state = arc_mru;
2419 if (refcount_count(&buf->b_refcnt) > 0)
2420 buf->b_flags &= ~ARC_PREFETCH;
2421 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2423 new_state = arc_mfu;
2424 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2427 buf->b_arc_access = ddi_get_lbolt();
2428 arc_change_state(new_state, buf, hash_lock);
2430 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2431 } else if (buf->b_state == arc_mfu) {
2433 * This buffer has been accessed more than once and is
2434 * still in the cache. Keep it in the MFU state.
2436 * NOTE: an add_reference() that occurred when we did
2437 * the arc_read() will have kicked this off the list.
2438 * If it was a prefetch, we will explicitly move it to
2439 * the head of the list now.
2441 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2442 ASSERT(refcount_count(&buf->b_refcnt) == 0);
2443 ASSERT(list_link_active(&buf->b_arc_node));
2445 ARCSTAT_BUMP(arcstat_mfu_hits);
2446 buf->b_arc_access = ddi_get_lbolt();
2447 } else if (buf->b_state == arc_mfu_ghost) {
2448 arc_state_t *new_state = arc_mfu;
2450 * This buffer has been accessed more than once but has
2451 * been evicted from the cache. Move it back to the
2455 if (buf->b_flags & ARC_PREFETCH) {
2457 * This is a prefetch access...
2458 * move this block back to the MRU state.
2460 ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0);
2461 new_state = arc_mru;
2464 buf->b_arc_access = ddi_get_lbolt();
2465 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2466 arc_change_state(new_state, buf, hash_lock);
2468 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2469 } else if (buf->b_state == arc_l2c_only) {
2471 * This buffer is on the 2nd Level ARC.
2474 buf->b_arc_access = ddi_get_lbolt();
2475 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2476 arc_change_state(arc_mfu, buf, hash_lock);
2478 ASSERT(!"invalid arc state");
2482 /* a generic arc_done_func_t which you can use */
2485 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2487 if (zio == NULL || zio->io_error == 0)
2488 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2489 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2492 /* a generic arc_done_func_t */
2494 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2496 arc_buf_t **bufp = arg;
2497 if (zio && zio->io_error) {
2498 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2502 ASSERT(buf->b_data);
2507 arc_read_done(zio_t *zio)
2509 arc_buf_hdr_t *hdr, *found;
2511 arc_buf_t *abuf; /* buffer we're assigning to callback */
2512 kmutex_t *hash_lock;
2513 arc_callback_t *callback_list, *acb;
2514 int freeable = FALSE;
2516 buf = zio->io_private;
2520 * The hdr was inserted into hash-table and removed from lists
2521 * prior to starting I/O. We should find this header, since
2522 * it's in the hash table, and it should be legit since it's
2523 * not possible to evict it during the I/O. The only possible
2524 * reason for it not to be found is if we were freed during the
2527 found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
2530 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
2531 (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2532 (found == hdr && HDR_L2_READING(hdr)));
2534 hdr->b_flags &= ~ARC_L2_EVICTED;
2535 if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2536 hdr->b_flags &= ~ARC_L2CACHE;
2538 /* byteswap if necessary */
2539 callback_list = hdr->b_acb;
2540 ASSERT(callback_list != NULL);
2541 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2542 arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
2543 byteswap_uint64_array :
2544 dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap;
2545 func(buf->b_data, hdr->b_size);
2548 arc_cksum_compute(buf, B_FALSE);
2550 if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
2552 * Only call arc_access on anonymous buffers. This is because
2553 * if we've issued an I/O for an evicted buffer, we've already
2554 * called arc_access (to prevent any simultaneous readers from
2555 * getting confused).
2557 arc_access(hdr, hash_lock);
2560 /* create copies of the data buffer for the callers */
2562 for (acb = callback_list; acb; acb = acb->acb_next) {
2563 if (acb->acb_done) {
2565 abuf = arc_buf_clone(buf);
2566 acb->acb_buf = abuf;
2571 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2572 ASSERT(!HDR_BUF_AVAILABLE(hdr));
2574 ASSERT(buf->b_efunc == NULL);
2575 ASSERT(hdr->b_datacnt == 1);
2576 hdr->b_flags |= ARC_BUF_AVAILABLE;
2579 ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2581 if (zio->io_error != 0) {
2582 hdr->b_flags |= ARC_IO_ERROR;
2583 if (hdr->b_state != arc_anon)
2584 arc_change_state(arc_anon, hdr, hash_lock);
2585 if (HDR_IN_HASH_TABLE(hdr))
2586 buf_hash_remove(hdr);
2587 freeable = refcount_is_zero(&hdr->b_refcnt);
2591 * Broadcast before we drop the hash_lock to avoid the possibility
2592 * that the hdr (and hence the cv) might be freed before we get to
2593 * the cv_broadcast().
2595 cv_broadcast(&hdr->b_cv);
2598 mutex_exit(hash_lock);
2601 * This block was freed while we waited for the read to
2602 * complete. It has been removed from the hash table and
2603 * moved to the anonymous state (so that it won't show up
2606 ASSERT3P(hdr->b_state, ==, arc_anon);
2607 freeable = refcount_is_zero(&hdr->b_refcnt);
2610 /* execute each callback and free its structure */
2611 while ((acb = callback_list) != NULL) {
2613 acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2615 if (acb->acb_zio_dummy != NULL) {
2616 acb->acb_zio_dummy->io_error = zio->io_error;
2617 zio_nowait(acb->acb_zio_dummy);
2620 callback_list = acb->acb_next;
2621 kmem_free(acb, sizeof (arc_callback_t));
2625 arc_hdr_destroy(hdr);
2629 * "Read" the block block at the specified DVA (in bp) via the
2630 * cache. If the block is found in the cache, invoke the provided
2631 * callback immediately and return. Note that the `zio' parameter
2632 * in the callback will be NULL in this case, since no IO was
2633 * required. If the block is not in the cache pass the read request
2634 * on to the spa with a substitute callback function, so that the
2635 * requested block will be added to the cache.
2637 * If a read request arrives for a block that has a read in-progress,
2638 * either wait for the in-progress read to complete (and return the
2639 * results); or, if this is a read with a "done" func, add a record
2640 * to the read to invoke the "done" func when the read completes,
2641 * and return; or just return.
2643 * arc_read_done() will invoke all the requested "done" functions
2644 * for readers of this block.
2646 * Normal callers should use arc_read and pass the arc buffer and offset
2647 * for the bp. But if you know you don't need locking, you can use
2651 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf,
2652 arc_done_func_t *done, void *private, int priority, int zio_flags,
2653 uint32_t *arc_flags, const zbookmark_t *zb)
2659 * XXX This happens from traverse callback funcs, for
2660 * the objset_phys_t block.
2662 return (arc_read_nolock(pio, spa, bp, done, private, priority,
2663 zio_flags, arc_flags, zb));
2666 ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt));
2667 ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size);
2668 rw_enter(&pbuf->b_data_lock, RW_READER);
2670 err = arc_read_nolock(pio, spa, bp, done, private, priority,
2671 zio_flags, arc_flags, zb);
2672 rw_exit(&pbuf->b_data_lock);
2678 arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp,
2679 arc_done_func_t *done, void *private, int priority, int zio_flags,
2680 uint32_t *arc_flags, const zbookmark_t *zb)
2683 arc_buf_t *buf = NULL;
2684 kmutex_t *hash_lock;
2686 uint64_t guid = spa_guid(spa);
2689 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
2691 if (hdr && hdr->b_datacnt > 0) {
2693 *arc_flags |= ARC_CACHED;
2695 if (HDR_IO_IN_PROGRESS(hdr)) {
2697 if (*arc_flags & ARC_WAIT) {
2698 cv_wait(&hdr->b_cv, hash_lock);
2699 mutex_exit(hash_lock);
2702 ASSERT(*arc_flags & ARC_NOWAIT);
2705 arc_callback_t *acb = NULL;
2707 acb = kmem_zalloc(sizeof (arc_callback_t),
2709 acb->acb_done = done;
2710 acb->acb_private = private;
2712 acb->acb_zio_dummy = zio_null(pio,
2713 spa, NULL, NULL, NULL, zio_flags);
2715 ASSERT(acb->acb_done != NULL);
2716 acb->acb_next = hdr->b_acb;
2718 add_reference(hdr, hash_lock, private);
2719 mutex_exit(hash_lock);
2722 mutex_exit(hash_lock);
2726 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2729 add_reference(hdr, hash_lock, private);
2731 * If this block is already in use, create a new
2732 * copy of the data so that we will be guaranteed
2733 * that arc_release() will always succeed.
2737 ASSERT(buf->b_data);
2738 if (HDR_BUF_AVAILABLE(hdr)) {
2739 ASSERT(buf->b_efunc == NULL);
2740 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
2742 buf = arc_buf_clone(buf);
2745 } else if (*arc_flags & ARC_PREFETCH &&
2746 refcount_count(&hdr->b_refcnt) == 0) {
2747 hdr->b_flags |= ARC_PREFETCH;
2749 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
2750 arc_access(hdr, hash_lock);
2751 if (*arc_flags & ARC_L2CACHE)
2752 hdr->b_flags |= ARC_L2CACHE;
2753 mutex_exit(hash_lock);
2754 ARCSTAT_BUMP(arcstat_hits);
2755 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2756 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
2757 data, metadata, hits);
2760 done(NULL, buf, private);
2762 uint64_t size = BP_GET_LSIZE(bp);
2763 arc_callback_t *acb;
2766 boolean_t devw = B_FALSE;
2769 /* this block is not in the cache */
2770 arc_buf_hdr_t *exists;
2771 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
2772 buf = arc_buf_alloc(spa, size, private, type);
2774 hdr->b_dva = *BP_IDENTITY(bp);
2775 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
2776 hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
2777 exists = buf_hash_insert(hdr, &hash_lock);
2779 /* somebody beat us to the hash insert */
2780 mutex_exit(hash_lock);
2781 buf_discard_identity(hdr);
2782 (void) arc_buf_remove_ref(buf, private);
2783 goto top; /* restart the IO request */
2785 /* if this is a prefetch, we don't have a reference */
2786 if (*arc_flags & ARC_PREFETCH) {
2787 (void) remove_reference(hdr, hash_lock,
2789 hdr->b_flags |= ARC_PREFETCH;
2791 if (*arc_flags & ARC_L2CACHE)
2792 hdr->b_flags |= ARC_L2CACHE;
2793 if (BP_GET_LEVEL(bp) > 0)
2794 hdr->b_flags |= ARC_INDIRECT;
2796 /* this block is in the ghost cache */
2797 ASSERT(GHOST_STATE(hdr->b_state));
2798 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
2799 ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0);
2800 ASSERT(hdr->b_buf == NULL);
2802 /* if this is a prefetch, we don't have a reference */
2803 if (*arc_flags & ARC_PREFETCH)
2804 hdr->b_flags |= ARC_PREFETCH;
2806 add_reference(hdr, hash_lock, private);
2807 if (*arc_flags & ARC_L2CACHE)
2808 hdr->b_flags |= ARC_L2CACHE;
2809 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
2812 buf->b_efunc = NULL;
2813 buf->b_private = NULL;
2816 ASSERT(hdr->b_datacnt == 0);
2818 arc_get_data_buf(buf);
2819 arc_access(hdr, hash_lock);
2822 ASSERT(!GHOST_STATE(hdr->b_state));
2824 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
2825 acb->acb_done = done;
2826 acb->acb_private = private;
2828 ASSERT(hdr->b_acb == NULL);
2830 hdr->b_flags |= ARC_IO_IN_PROGRESS;
2832 if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
2833 (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
2834 devw = hdr->b_l2hdr->b_dev->l2ad_writing;
2835 addr = hdr->b_l2hdr->b_daddr;
2837 * Lock out device removal.
2839 if (vdev_is_dead(vd) ||
2840 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
2844 mutex_exit(hash_lock);
2846 ASSERT3U(hdr->b_size, ==, size);
2847 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
2848 uint64_t, size, zbookmark_t *, zb);
2849 ARCSTAT_BUMP(arcstat_misses);
2850 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2851 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
2852 data, metadata, misses);
2854 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
2856 * Read from the L2ARC if the following are true:
2857 * 1. The L2ARC vdev was previously cached.
2858 * 2. This buffer still has L2ARC metadata.
2859 * 3. This buffer isn't currently writing to the L2ARC.
2860 * 4. The L2ARC entry wasn't evicted, which may
2861 * also have invalidated the vdev.
2862 * 5. This isn't prefetch and l2arc_noprefetch is set.
2864 if (hdr->b_l2hdr != NULL &&
2865 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
2866 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
2867 l2arc_read_callback_t *cb;
2869 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
2870 ARCSTAT_BUMP(arcstat_l2_hits);
2872 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
2874 cb->l2rcb_buf = buf;
2875 cb->l2rcb_spa = spa;
2878 cb->l2rcb_flags = zio_flags;
2881 * l2arc read. The SCL_L2ARC lock will be
2882 * released by l2arc_read_done().
2884 rzio = zio_read_phys(pio, vd, addr, size,
2885 buf->b_data, ZIO_CHECKSUM_OFF,
2886 l2arc_read_done, cb, priority, zio_flags |
2887 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL |
2888 ZIO_FLAG_DONT_PROPAGATE |
2889 ZIO_FLAG_DONT_RETRY, B_FALSE);
2890 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
2892 ARCSTAT_INCR(arcstat_l2_read_bytes, size);
2894 if (*arc_flags & ARC_NOWAIT) {
2899 ASSERT(*arc_flags & ARC_WAIT);
2900 if (zio_wait(rzio) == 0)
2903 /* l2arc read error; goto zio_read() */
2905 DTRACE_PROBE1(l2arc__miss,
2906 arc_buf_hdr_t *, hdr);
2907 ARCSTAT_BUMP(arcstat_l2_misses);
2908 if (HDR_L2_WRITING(hdr))
2909 ARCSTAT_BUMP(arcstat_l2_rw_clash);
2910 spa_config_exit(spa, SCL_L2ARC, vd);
2914 spa_config_exit(spa, SCL_L2ARC, vd);
2915 if (l2arc_ndev != 0) {
2916 DTRACE_PROBE1(l2arc__miss,
2917 arc_buf_hdr_t *, hdr);
2918 ARCSTAT_BUMP(arcstat_l2_misses);
2922 rzio = zio_read(pio, spa, bp, buf->b_data, size,
2923 arc_read_done, buf, priority, zio_flags, zb);
2925 if (*arc_flags & ARC_WAIT)
2926 return (zio_wait(rzio));
2928 ASSERT(*arc_flags & ARC_NOWAIT);
2935 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
2937 ASSERT(buf->b_hdr != NULL);
2938 ASSERT(buf->b_hdr->b_state != arc_anon);
2939 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
2940 ASSERT(buf->b_efunc == NULL);
2941 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
2943 buf->b_efunc = func;
2944 buf->b_private = private;
2948 * This is used by the DMU to let the ARC know that a buffer is
2949 * being evicted, so the ARC should clean up. If this arc buf
2950 * is not yet in the evicted state, it will be put there.
2953 arc_buf_evict(arc_buf_t *buf)
2956 kmutex_t *hash_lock;
2959 mutex_enter(&buf->b_evict_lock);
2963 * We are in arc_do_user_evicts().
2965 ASSERT(buf->b_data == NULL);
2966 mutex_exit(&buf->b_evict_lock);
2968 } else if (buf->b_data == NULL) {
2969 arc_buf_t copy = *buf; /* structure assignment */
2971 * We are on the eviction list; process this buffer now
2972 * but let arc_do_user_evicts() do the reaping.
2974 buf->b_efunc = NULL;
2975 mutex_exit(&buf->b_evict_lock);
2976 VERIFY(copy.b_efunc(©) == 0);
2979 hash_lock = HDR_LOCK(hdr);
2980 mutex_enter(hash_lock);
2982 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
2984 ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
2985 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2988 * Pull this buffer off of the hdr
2991 while (*bufp != buf)
2992 bufp = &(*bufp)->b_next;
2993 *bufp = buf->b_next;
2995 ASSERT(buf->b_data != NULL);
2996 arc_buf_destroy(buf, FALSE, FALSE);
2998 if (hdr->b_datacnt == 0) {
2999 arc_state_t *old_state = hdr->b_state;
3000 arc_state_t *evicted_state;
3002 ASSERT(hdr->b_buf == NULL);
3003 ASSERT(refcount_is_zero(&hdr->b_refcnt));
3006 (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
3008 mutex_enter(&old_state->arcs_mtx);
3009 mutex_enter(&evicted_state->arcs_mtx);
3011 arc_change_state(evicted_state, hdr, hash_lock);
3012 ASSERT(HDR_IN_HASH_TABLE(hdr));
3013 hdr->b_flags |= ARC_IN_HASH_TABLE;
3014 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3016 mutex_exit(&evicted_state->arcs_mtx);
3017 mutex_exit(&old_state->arcs_mtx);
3019 mutex_exit(hash_lock);
3020 mutex_exit(&buf->b_evict_lock);
3022 VERIFY(buf->b_efunc(buf) == 0);
3023 buf->b_efunc = NULL;
3024 buf->b_private = NULL;
3027 kmem_cache_free(buf_cache, buf);
3032 * Release this buffer from the cache. This must be done
3033 * after a read and prior to modifying the buffer contents.
3034 * If the buffer has more than one reference, we must make
3035 * a new hdr for the buffer.
3038 arc_release(arc_buf_t *buf, void *tag)
3041 kmutex_t *hash_lock = NULL;
3042 l2arc_buf_hdr_t *l2hdr;
3043 uint64_t buf_size = 0;
3046 * It would be nice to assert that if it's DMU metadata (level >
3047 * 0 || it's the dnode file), then it must be syncing context.
3048 * But we don't know that information at this level.
3051 mutex_enter(&buf->b_evict_lock);
3054 /* this buffer is not on any list */
3055 ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3057 if (hdr->b_state == arc_anon) {
3058 /* this buffer is already released */
3059 ASSERT(buf->b_efunc == NULL);
3061 hash_lock = HDR_LOCK(hdr);
3062 mutex_enter(hash_lock);
3064 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3067 l2hdr = hdr->b_l2hdr;
3069 mutex_enter(&l2arc_buflist_mtx);
3070 hdr->b_l2hdr = NULL;
3071 buf_size = hdr->b_size;
3075 * Do we have more than one buf?
3077 if (hdr->b_datacnt > 1) {
3078 arc_buf_hdr_t *nhdr;
3080 uint64_t blksz = hdr->b_size;
3081 uint64_t spa = hdr->b_spa;
3082 arc_buf_contents_t type = hdr->b_type;
3083 uint32_t flags = hdr->b_flags;
3085 ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3087 * Pull the data off of this hdr and attach it to
3088 * a new anonymous hdr.
3090 (void) remove_reference(hdr, hash_lock, tag);
3092 while (*bufp != buf)
3093 bufp = &(*bufp)->b_next;
3094 *bufp = buf->b_next;
3097 ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3098 atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3099 if (refcount_is_zero(&hdr->b_refcnt)) {
3100 uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3101 ASSERT3U(*size, >=, hdr->b_size);
3102 atomic_add_64(size, -hdr->b_size);
3104 hdr->b_datacnt -= 1;
3105 arc_cksum_verify(buf);
3107 mutex_exit(hash_lock);
3109 nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3110 nhdr->b_size = blksz;
3112 nhdr->b_type = type;
3114 nhdr->b_state = arc_anon;
3115 nhdr->b_arc_access = 0;
3116 nhdr->b_flags = flags & ARC_L2_WRITING;
3117 nhdr->b_l2hdr = NULL;
3118 nhdr->b_datacnt = 1;
3119 nhdr->b_freeze_cksum = NULL;
3120 (void) refcount_add(&nhdr->b_refcnt, tag);
3122 mutex_exit(&buf->b_evict_lock);
3123 atomic_add_64(&arc_anon->arcs_size, blksz);
3125 mutex_exit(&buf->b_evict_lock);
3126 ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3127 ASSERT(!list_link_active(&hdr->b_arc_node));
3128 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3129 if (hdr->b_state != arc_anon)
3130 arc_change_state(arc_anon, hdr, hash_lock);
3131 hdr->b_arc_access = 0;
3133 mutex_exit(hash_lock);
3135 buf_discard_identity(hdr);
3138 buf->b_efunc = NULL;
3139 buf->b_private = NULL;
3142 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3143 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3144 ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3145 mutex_exit(&l2arc_buflist_mtx);
3150 * Release this buffer. If it does not match the provided BP, fill it
3151 * with that block's contents.
3155 arc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa,
3158 arc_release(buf, tag);
3163 arc_released(arc_buf_t *buf)
3167 mutex_enter(&buf->b_evict_lock);
3168 released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3169 mutex_exit(&buf->b_evict_lock);
3174 arc_has_callback(arc_buf_t *buf)
3178 mutex_enter(&buf->b_evict_lock);
3179 callback = (buf->b_efunc != NULL);
3180 mutex_exit(&buf->b_evict_lock);
3186 arc_referenced(arc_buf_t *buf)
3190 mutex_enter(&buf->b_evict_lock);
3191 referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3192 mutex_exit(&buf->b_evict_lock);
3193 return (referenced);
3198 arc_write_ready(zio_t *zio)
3200 arc_write_callback_t *callback = zio->io_private;
3201 arc_buf_t *buf = callback->awcb_buf;
3202 arc_buf_hdr_t *hdr = buf->b_hdr;
3204 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3205 callback->awcb_ready(zio, buf, callback->awcb_private);
3208 * If the IO is already in progress, then this is a re-write
3209 * attempt, so we need to thaw and re-compute the cksum.
3210 * It is the responsibility of the callback to handle the
3211 * accounting for any re-write attempt.
3213 if (HDR_IO_IN_PROGRESS(hdr)) {
3214 mutex_enter(&hdr->b_freeze_lock);
3215 if (hdr->b_freeze_cksum != NULL) {
3216 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3217 hdr->b_freeze_cksum = NULL;
3219 mutex_exit(&hdr->b_freeze_lock);
3221 arc_cksum_compute(buf, B_FALSE);
3222 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3226 arc_write_done(zio_t *zio)
3228 arc_write_callback_t *callback = zio->io_private;
3229 arc_buf_t *buf = callback->awcb_buf;
3230 arc_buf_hdr_t *hdr = buf->b_hdr;
3232 ASSERT(hdr->b_acb == NULL);
3234 if (zio->io_error == 0) {
3235 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3236 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3237 hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3239 ASSERT(BUF_EMPTY(hdr));
3243 * If the block to be written was all-zero, we may have
3244 * compressed it away. In this case no write was performed
3245 * so there will be no dva/birth/checksum. The buffer must
3246 * therefore remain anonymous (and uncached).
3248 if (!BUF_EMPTY(hdr)) {
3249 arc_buf_hdr_t *exists;
3250 kmutex_t *hash_lock;
3252 ASSERT(zio->io_error == 0);
3254 arc_cksum_verify(buf);
3256 exists = buf_hash_insert(hdr, &hash_lock);
3259 * This can only happen if we overwrite for
3260 * sync-to-convergence, because we remove
3261 * buffers from the hash table when we arc_free().
3263 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3264 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3265 panic("bad overwrite, hdr=%p exists=%p",
3266 (void *)hdr, (void *)exists);
3267 ASSERT(refcount_is_zero(&exists->b_refcnt));
3268 arc_change_state(arc_anon, exists, hash_lock);
3269 mutex_exit(hash_lock);
3270 arc_hdr_destroy(exists);
3271 exists = buf_hash_insert(hdr, &hash_lock);
3272 ASSERT3P(exists, ==, NULL);
3275 ASSERT(hdr->b_datacnt == 1);
3276 ASSERT(hdr->b_state == arc_anon);
3277 ASSERT(BP_GET_DEDUP(zio->io_bp));
3278 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3281 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3282 /* if it's not anon, we are doing a scrub */
3283 if (!exists && hdr->b_state == arc_anon)
3284 arc_access(hdr, hash_lock);
3285 mutex_exit(hash_lock);
3287 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3290 ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3291 callback->awcb_done(zio, buf, callback->awcb_private);
3293 kmem_free(callback, sizeof (arc_write_callback_t));
3297 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3298 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
3299 arc_done_func_t *ready, arc_done_func_t *done, void *private,
3300 int priority, int zio_flags, const zbookmark_t *zb)
3302 arc_buf_hdr_t *hdr = buf->b_hdr;
3303 arc_write_callback_t *callback;
3306 ASSERT(ready != NULL);
3307 ASSERT(done != NULL);
3308 ASSERT(!HDR_IO_ERROR(hdr));
3309 ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3310 ASSERT(hdr->b_acb == NULL);
3312 hdr->b_flags |= ARC_L2CACHE;
3313 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
3314 callback->awcb_ready = ready;
3315 callback->awcb_done = done;
3316 callback->awcb_private = private;
3317 callback->awcb_buf = buf;
3319 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3320 arc_write_ready, arc_write_done, callback, priority, zio_flags, zb);
3326 arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg)
3329 uint64_t available_memory = ptob(freemem);
3330 static uint64_t page_load = 0;
3331 static uint64_t last_txg = 0;
3335 MIN(available_memory, vmem_size(heap_arena, VMEM_FREE));
3337 if (available_memory >= zfs_write_limit_max)
3340 if (txg > last_txg) {
3345 * If we are in pageout, we know that memory is already tight,
3346 * the arc is already going to be evicting, so we just want to
3347 * continue to let page writes occur as quickly as possible.
3349 if (curproc == proc_pageout) {
3350 if (page_load > MAX(ptob(minfree), available_memory) / 4)
3352 /* Note: reserve is inflated, so we deflate */
3353 page_load += reserve / 8;
3355 } else if (page_load > 0 && arc_reclaim_needed()) {
3356 /* memory is low, delay before restarting */
3357 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3362 if (arc_size > arc_c_min) {
3363 uint64_t evictable_memory =
3364 arc_mru->arcs_lsize[ARC_BUFC_DATA] +
3365 arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
3366 arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
3367 arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
3368 available_memory += MIN(evictable_memory, arc_size - arc_c_min);
3371 if (inflight_data > available_memory / 4) {
3372 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3380 arc_tempreserve_clear(uint64_t reserve)
3382 atomic_add_64(&arc_tempreserve, -reserve);
3383 ASSERT((int64_t)arc_tempreserve >= 0);
3387 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3394 * Once in a while, fail for no reason. Everything should cope.
3396 if (spa_get_random(10000) == 0) {
3397 dprintf("forcing random failure\n");
3401 if (reserve > arc_c/4 && !arc_no_grow)
3402 arc_c = MIN(arc_c_max, reserve * 4);
3403 if (reserve > arc_c)
3407 * Don't count loaned bufs as in flight dirty data to prevent long
3408 * network delays from blocking transactions that are ready to be
3409 * assigned to a txg.
3411 anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3414 * Writes will, almost always, require additional memory allocations
3415 * in order to compress/encrypt/etc the data. We therefor need to
3416 * make sure that there is sufficient available memory for this.
3418 if ((error = arc_memory_throttle(reserve, anon_size, txg)))
3422 * Throttle writes when the amount of dirty data in the cache
3423 * gets too large. We try to keep the cache less than half full
3424 * of dirty blocks so that our sync times don't grow too large.
3425 * Note: if two requests come in concurrently, we might let them
3426 * both succeed, when one of them should fail. Not a huge deal.
3429 if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3430 anon_size > arc_c / 4) {
3431 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3432 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3433 arc_tempreserve>>10,
3434 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3435 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3436 reserve>>10, arc_c>>10);
3439 atomic_add_64(&arc_tempreserve, reserve);
3446 mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3447 cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3449 /* Convert seconds to clock ticks */
3450 arc_min_prefetch_lifespan = 1 * hz;
3452 /* Start out with 1/8 of all memory */
3453 arc_c = physmem * PAGESIZE / 8;
3457 * On architectures where the physical memory can be larger
3458 * than the addressable space (intel in 32-bit mode), we may
3459 * need to limit the cache to 1/8 of VM size.
3461 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3464 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3465 arc_c_min = MAX(arc_c / 4, 64<<20);
3466 /* set max to 3/4 of all memory, or all but 1GB, whichever is more */
3467 if (arc_c * 8 >= 1<<30)
3468 arc_c_max = (arc_c * 8) - (1<<30);
3470 arc_c_max = arc_c_min;
3471 arc_c_max = MAX(arc_c * 6, arc_c_max);
3474 * Allow the tunables to override our calculations if they are
3475 * reasonable (ie. over 64MB)
3477 if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
3478 arc_c_max = zfs_arc_max;
3479 if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
3480 arc_c_min = zfs_arc_min;
3483 arc_p = (arc_c >> 1);
3485 /* limit meta-data to 1/4 of the arc capacity */
3486 arc_meta_limit = arc_c_max / 4;
3488 /* Allow the tunable to override if it is reasonable */
3489 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
3490 arc_meta_limit = zfs_arc_meta_limit;
3492 if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3493 arc_c_min = arc_meta_limit / 2;
3495 if (zfs_arc_grow_retry > 0)
3496 arc_grow_retry = zfs_arc_grow_retry;
3498 if (zfs_arc_shrink_shift > 0)
3499 arc_shrink_shift = zfs_arc_shrink_shift;
3501 if (zfs_arc_p_min_shift > 0)
3502 arc_p_min_shift = zfs_arc_p_min_shift;
3504 /* if kmem_flags are set, lets try to use less memory */
3505 if (kmem_debugging())
3507 if (arc_c < arc_c_min)
3510 arc_anon = &ARC_anon;
3512 arc_mru_ghost = &ARC_mru_ghost;
3514 arc_mfu_ghost = &ARC_mfu_ghost;
3515 arc_l2c_only = &ARC_l2c_only;
3518 mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3519 mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3520 mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3521 mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3522 mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3523 mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3525 list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
3526 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3527 list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
3528 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3529 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
3530 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3531 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
3532 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3533 list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
3534 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3535 list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
3536 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3537 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
3538 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3539 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
3540 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3541 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
3542 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3543 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
3544 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3548 arc_thread_exit = 0;
3549 arc_eviction_list = NULL;
3550 mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
3551 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3553 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
3554 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3556 if (arc_ksp != NULL) {
3557 arc_ksp->ks_data = &arc_stats;
3558 kstat_install(arc_ksp);
3561 (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
3562 TS_RUN, minclsyspri);
3567 if (zfs_write_limit_max == 0)
3568 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
3570 zfs_write_limit_shift = 0;
3571 mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL);
3577 mutex_enter(&arc_reclaim_thr_lock);
3578 arc_thread_exit = 1;
3579 while (arc_thread_exit != 0)
3580 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3581 mutex_exit(&arc_reclaim_thr_lock);
3587 if (arc_ksp != NULL) {
3588 kstat_delete(arc_ksp);
3592 mutex_destroy(&arc_eviction_mtx);
3593 mutex_destroy(&arc_reclaim_thr_lock);
3594 cv_destroy(&arc_reclaim_thr_cv);
3596 list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
3597 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
3598 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
3599 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
3600 list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
3601 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
3602 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
3603 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3605 mutex_destroy(&arc_anon->arcs_mtx);
3606 mutex_destroy(&arc_mru->arcs_mtx);
3607 mutex_destroy(&arc_mru_ghost->arcs_mtx);
3608 mutex_destroy(&arc_mfu->arcs_mtx);
3609 mutex_destroy(&arc_mfu_ghost->arcs_mtx);
3610 mutex_destroy(&arc_l2c_only->arcs_mtx);
3612 mutex_destroy(&zfs_write_limit_lock);
3616 ASSERT(arc_loaned_bytes == 0);
3622 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3623 * It uses dedicated storage devices to hold cached data, which are populated
3624 * using large infrequent writes. The main role of this cache is to boost
3625 * the performance of random read workloads. The intended L2ARC devices
3626 * include short-stroked disks, solid state disks, and other media with
3627 * substantially faster read latency than disk.
3629 * +-----------------------+
3631 * +-----------------------+
3634 * l2arc_feed_thread() arc_read()
3638 * +---------------+ |
3640 * +---------------+ |
3645 * +-------+ +-------+
3647 * | cache | | cache |
3648 * +-------+ +-------+
3649 * +=========+ .-----.
3650 * : L2ARC : |-_____-|
3651 * : devices : | Disks |
3652 * +=========+ `-_____-'
3654 * Read requests are satisfied from the following sources, in order:
3657 * 2) vdev cache of L2ARC devices
3659 * 4) vdev cache of disks
3662 * Some L2ARC device types exhibit extremely slow write performance.
3663 * To accommodate for this there are some significant differences between
3664 * the L2ARC and traditional cache design:
3666 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
3667 * the ARC behave as usual, freeing buffers and placing headers on ghost
3668 * lists. The ARC does not send buffers to the L2ARC during eviction as
3669 * this would add inflated write latencies for all ARC memory pressure.
3671 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
3672 * It does this by periodically scanning buffers from the eviction-end of
3673 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
3674 * not already there. It scans until a headroom of buffers is satisfied,
3675 * which itself is a buffer for ARC eviction. The thread that does this is
3676 * l2arc_feed_thread(), illustrated below; example sizes are included to
3677 * provide a better sense of ratio than this diagram:
3680 * +---------------------+----------+
3681 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
3682 * +---------------------+----------+ | o L2ARC eligible
3683 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
3684 * +---------------------+----------+ |
3685 * 15.9 Gbytes ^ 32 Mbytes |
3687 * l2arc_feed_thread()
3689 * l2arc write hand <--[oooo]--'
3693 * +==============================+
3694 * L2ARC dev |####|#|###|###| |####| ... |
3695 * +==============================+
3698 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
3699 * evicted, then the L2ARC has cached a buffer much sooner than it probably
3700 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
3701 * safe to say that this is an uncommon case, since buffers at the end of
3702 * the ARC lists have moved there due to inactivity.
3704 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
3705 * then the L2ARC simply misses copying some buffers. This serves as a
3706 * pressure valve to prevent heavy read workloads from both stalling the ARC
3707 * with waits and clogging the L2ARC with writes. This also helps prevent
3708 * the potential for the L2ARC to churn if it attempts to cache content too
3709 * quickly, such as during backups of the entire pool.
3711 * 5. After system boot and before the ARC has filled main memory, there are
3712 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
3713 * lists can remain mostly static. Instead of searching from tail of these
3714 * lists as pictured, the l2arc_feed_thread() will search from the list heads
3715 * for eligible buffers, greatly increasing its chance of finding them.
3717 * The L2ARC device write speed is also boosted during this time so that
3718 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
3719 * there are no L2ARC reads, and no fear of degrading read performance
3720 * through increased writes.
3722 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
3723 * the vdev queue can aggregate them into larger and fewer writes. Each
3724 * device is written to in a rotor fashion, sweeping writes through
3725 * available space then repeating.
3727 * 7. The L2ARC does not store dirty content. It never needs to flush
3728 * write buffers back to disk based storage.
3730 * 8. If an ARC buffer is written (and dirtied) which also exists in the
3731 * L2ARC, the now stale L2ARC buffer is immediately dropped.
3733 * The performance of the L2ARC can be tweaked by a number of tunables, which
3734 * may be necessary for different workloads:
3736 * l2arc_write_max max write bytes per interval
3737 * l2arc_write_boost extra write bytes during device warmup
3738 * l2arc_noprefetch skip caching prefetched buffers
3739 * l2arc_headroom number of max device writes to precache
3740 * l2arc_feed_secs seconds between L2ARC writing
3742 * Tunables may be removed or added as future performance improvements are
3743 * integrated, and also may become zpool properties.
3745 * There are three key functions that control how the L2ARC warms up:
3747 * l2arc_write_eligible() check if a buffer is eligible to cache
3748 * l2arc_write_size() calculate how much to write
3749 * l2arc_write_interval() calculate sleep delay between writes
3751 * These three functions determine what to write, how much, and how quickly
3756 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
3759 * A buffer is *not* eligible for the L2ARC if it:
3760 * 1. belongs to a different spa.
3761 * 2. is already cached on the L2ARC.
3762 * 3. has an I/O in progress (it may be an incomplete read).
3763 * 4. is flagged not eligible (zfs property).
3765 if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
3766 HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
3773 l2arc_write_size(l2arc_dev_t *dev)
3777 size = dev->l2ad_write;
3779 if (arc_warm == B_FALSE)
3780 size += dev->l2ad_boost;
3787 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
3789 clock_t interval, next, now;
3792 * If the ARC lists are busy, increase our write rate; if the
3793 * lists are stale, idle back. This is achieved by checking
3794 * how much we previously wrote - if it was more than half of
3795 * what we wanted, schedule the next write much sooner.
3797 if (l2arc_feed_again && wrote > (wanted / 2))
3798 interval = (hz * l2arc_feed_min_ms) / 1000;
3800 interval = hz * l2arc_feed_secs;
3802 now = ddi_get_lbolt();
3803 next = MAX(now, MIN(now + interval, began + interval));
3809 l2arc_hdr_stat_add(void)
3811 ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
3812 ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
3816 l2arc_hdr_stat_remove(void)
3818 ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
3819 ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
3823 * Cycle through L2ARC devices. This is how L2ARC load balances.
3824 * If a device is returned, this also returns holding the spa config lock.
3826 static l2arc_dev_t *
3827 l2arc_dev_get_next(void)
3829 l2arc_dev_t *first, *next = NULL;
3832 * Lock out the removal of spas (spa_namespace_lock), then removal
3833 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
3834 * both locks will be dropped and a spa config lock held instead.
3836 mutex_enter(&spa_namespace_lock);
3837 mutex_enter(&l2arc_dev_mtx);
3839 /* if there are no vdevs, there is nothing to do */
3840 if (l2arc_ndev == 0)
3844 next = l2arc_dev_last;
3846 /* loop around the list looking for a non-faulted vdev */
3848 next = list_head(l2arc_dev_list);
3850 next = list_next(l2arc_dev_list, next);
3852 next = list_head(l2arc_dev_list);
3855 /* if we have come back to the start, bail out */
3858 else if (next == first)
3861 } while (vdev_is_dead(next->l2ad_vdev));
3863 /* if we were unable to find any usable vdevs, return NULL */
3864 if (vdev_is_dead(next->l2ad_vdev))
3867 l2arc_dev_last = next;
3870 mutex_exit(&l2arc_dev_mtx);
3873 * Grab the config lock to prevent the 'next' device from being
3874 * removed while we are writing to it.
3877 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
3878 mutex_exit(&spa_namespace_lock);
3884 * Free buffers that were tagged for destruction.
3887 l2arc_do_free_on_write(void)
3890 l2arc_data_free_t *df, *df_prev;
3892 mutex_enter(&l2arc_free_on_write_mtx);
3893 buflist = l2arc_free_on_write;
3895 for (df = list_tail(buflist); df; df = df_prev) {
3896 df_prev = list_prev(buflist, df);
3897 ASSERT(df->l2df_data != NULL);
3898 ASSERT(df->l2df_func != NULL);
3899 df->l2df_func(df->l2df_data, df->l2df_size);
3900 list_remove(buflist, df);
3901 kmem_free(df, sizeof (l2arc_data_free_t));
3904 mutex_exit(&l2arc_free_on_write_mtx);
3908 * A write to a cache device has completed. Update all headers to allow
3909 * reads from these buffers to begin.
3912 l2arc_write_done(zio_t *zio)
3914 l2arc_write_callback_t *cb;
3917 arc_buf_hdr_t *head, *ab, *ab_prev;
3918 l2arc_buf_hdr_t *abl2;
3919 kmutex_t *hash_lock;
3921 cb = zio->io_private;
3923 dev = cb->l2wcb_dev;
3924 ASSERT(dev != NULL);
3925 head = cb->l2wcb_head;
3926 ASSERT(head != NULL);
3927 buflist = dev->l2ad_buflist;
3928 ASSERT(buflist != NULL);
3929 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
3930 l2arc_write_callback_t *, cb);
3932 if (zio->io_error != 0)
3933 ARCSTAT_BUMP(arcstat_l2_writes_error);
3935 mutex_enter(&l2arc_buflist_mtx);
3938 * All writes completed, or an error was hit.
3940 for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
3941 ab_prev = list_prev(buflist, ab);
3943 hash_lock = HDR_LOCK(ab);
3944 if (!mutex_tryenter(hash_lock)) {
3946 * This buffer misses out. It may be in a stage
3947 * of eviction. Its ARC_L2_WRITING flag will be
3948 * left set, denying reads to this buffer.
3950 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
3954 if (zio->io_error != 0) {
3956 * Error - drop L2ARC entry.
3958 list_remove(buflist, ab);
3961 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
3962 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
3966 * Allow ARC to begin reads to this L2ARC entry.
3968 ab->b_flags &= ~ARC_L2_WRITING;
3970 mutex_exit(hash_lock);
3973 atomic_inc_64(&l2arc_writes_done);
3974 list_remove(buflist, head);
3975 kmem_cache_free(hdr_cache, head);
3976 mutex_exit(&l2arc_buflist_mtx);
3978 l2arc_do_free_on_write();
3980 kmem_free(cb, sizeof (l2arc_write_callback_t));
3984 * A read to a cache device completed. Validate buffer contents before
3985 * handing over to the regular ARC routines.
3988 l2arc_read_done(zio_t *zio)
3990 l2arc_read_callback_t *cb;
3993 kmutex_t *hash_lock;
3996 ASSERT(zio->io_vd != NULL);
3997 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
3999 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
4001 cb = zio->io_private;
4003 buf = cb->l2rcb_buf;
4004 ASSERT(buf != NULL);
4006 hash_lock = HDR_LOCK(buf->b_hdr);
4007 mutex_enter(hash_lock);
4009 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4012 * Check this survived the L2ARC journey.
4014 equal = arc_cksum_equal(buf);
4015 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4016 mutex_exit(hash_lock);
4017 zio->io_private = buf;
4018 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
4019 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
4022 mutex_exit(hash_lock);
4024 * Buffer didn't survive caching. Increment stats and
4025 * reissue to the original storage device.
4027 if (zio->io_error != 0) {
4028 ARCSTAT_BUMP(arcstat_l2_io_error);
4030 zio->io_error = EIO;
4033 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4036 * If there's no waiter, issue an async i/o to the primary
4037 * storage now. If there *is* a waiter, the caller must
4038 * issue the i/o in a context where it's OK to block.
4040 if (zio->io_waiter == NULL) {
4041 zio_t *pio = zio_unique_parent(zio);
4043 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4045 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4046 buf->b_data, zio->io_size, arc_read_done, buf,
4047 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4051 kmem_free(cb, sizeof (l2arc_read_callback_t));
4055 * This is the list priority from which the L2ARC will search for pages to
4056 * cache. This is used within loops (0..3) to cycle through lists in the
4057 * desired order. This order can have a significant effect on cache
4060 * Currently the metadata lists are hit first, MFU then MRU, followed by
4061 * the data lists. This function returns a locked list, and also returns
4065 l2arc_list_locked(int list_num, kmutex_t **lock)
4067 list_t *list = NULL;
4069 ASSERT(list_num >= 0 && list_num <= 3);
4073 list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
4074 *lock = &arc_mfu->arcs_mtx;
4077 list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
4078 *lock = &arc_mru->arcs_mtx;
4081 list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
4082 *lock = &arc_mfu->arcs_mtx;
4085 list = &arc_mru->arcs_list[ARC_BUFC_DATA];
4086 *lock = &arc_mru->arcs_mtx;
4090 ASSERT(!(MUTEX_HELD(*lock)));
4096 * Evict buffers from the device write hand to the distance specified in
4097 * bytes. This distance may span populated buffers, it may span nothing.
4098 * This is clearing a region on the L2ARC device ready for writing.
4099 * If the 'all' boolean is set, every buffer is evicted.
4102 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4105 l2arc_buf_hdr_t *abl2;
4106 arc_buf_hdr_t *ab, *ab_prev;
4107 kmutex_t *hash_lock;
4110 buflist = dev->l2ad_buflist;
4112 if (buflist == NULL)
4115 if (!all && dev->l2ad_first) {
4117 * This is the first sweep through the device. There is
4123 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4125 * When nearing the end of the device, evict to the end
4126 * before the device write hand jumps to the start.
4128 taddr = dev->l2ad_end;
4130 taddr = dev->l2ad_hand + distance;
4132 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4133 uint64_t, taddr, boolean_t, all);
4136 mutex_enter(&l2arc_buflist_mtx);
4137 for (ab = list_tail(buflist); ab; ab = ab_prev) {
4138 ab_prev = list_prev(buflist, ab);
4140 hash_lock = HDR_LOCK(ab);
4141 if (!mutex_tryenter(hash_lock)) {
4143 * Missed the hash lock. Retry.
4145 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4146 mutex_exit(&l2arc_buflist_mtx);
4147 mutex_enter(hash_lock);
4148 mutex_exit(hash_lock);
4152 if (HDR_L2_WRITE_HEAD(ab)) {
4154 * We hit a write head node. Leave it for
4155 * l2arc_write_done().
4157 list_remove(buflist, ab);
4158 mutex_exit(hash_lock);
4162 if (!all && ab->b_l2hdr != NULL &&
4163 (ab->b_l2hdr->b_daddr > taddr ||
4164 ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4166 * We've evicted to the target address,
4167 * or the end of the device.
4169 mutex_exit(hash_lock);
4173 if (HDR_FREE_IN_PROGRESS(ab)) {
4175 * Already on the path to destruction.
4177 mutex_exit(hash_lock);
4181 if (ab->b_state == arc_l2c_only) {
4182 ASSERT(!HDR_L2_READING(ab));
4184 * This doesn't exist in the ARC. Destroy.
4185 * arc_hdr_destroy() will call list_remove()
4186 * and decrement arcstat_l2_size.
4188 arc_change_state(arc_anon, ab, hash_lock);
4189 arc_hdr_destroy(ab);
4192 * Invalidate issued or about to be issued
4193 * reads, since we may be about to write
4194 * over this location.
4196 if (HDR_L2_READING(ab)) {
4197 ARCSTAT_BUMP(arcstat_l2_evict_reading);
4198 ab->b_flags |= ARC_L2_EVICTED;
4202 * Tell ARC this no longer exists in L2ARC.
4204 if (ab->b_l2hdr != NULL) {
4207 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4208 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4210 list_remove(buflist, ab);
4213 * This may have been leftover after a
4216 ab->b_flags &= ~ARC_L2_WRITING;
4218 mutex_exit(hash_lock);
4220 mutex_exit(&l2arc_buflist_mtx);
4222 vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
4223 dev->l2ad_evict = taddr;
4227 * Find and write ARC buffers to the L2ARC device.
4229 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4230 * for reading until they have completed writing.
4233 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
4235 arc_buf_hdr_t *ab, *ab_prev, *head;
4236 l2arc_buf_hdr_t *hdrl2;
4238 uint64_t passed_sz, write_sz, buf_sz, headroom;
4240 kmutex_t *hash_lock, *list_lock = NULL;
4241 boolean_t have_lock, full;
4242 l2arc_write_callback_t *cb;
4244 uint64_t guid = spa_guid(spa);
4247 ASSERT(dev->l2ad_vdev != NULL);
4252 head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4253 head->b_flags |= ARC_L2_WRITE_HEAD;
4256 * Copy buffers for L2ARC writing.
4258 mutex_enter(&l2arc_buflist_mtx);
4259 for (try = 0; try <= 3; try++) {
4260 list = l2arc_list_locked(try, &list_lock);
4264 * L2ARC fast warmup.
4266 * Until the ARC is warm and starts to evict, read from the
4267 * head of the ARC lists rather than the tail.
4269 headroom = target_sz * l2arc_headroom;
4270 if (arc_warm == B_FALSE)
4271 ab = list_head(list);
4273 ab = list_tail(list);
4275 for (; ab; ab = ab_prev) {
4276 if (arc_warm == B_FALSE)
4277 ab_prev = list_next(list, ab);
4279 ab_prev = list_prev(list, ab);
4281 hash_lock = HDR_LOCK(ab);
4282 have_lock = MUTEX_HELD(hash_lock);
4283 if (!have_lock && !mutex_tryenter(hash_lock)) {
4285 * Skip this buffer rather than waiting.
4290 passed_sz += ab->b_size;
4291 if (passed_sz > headroom) {
4295 mutex_exit(hash_lock);
4299 if (!l2arc_write_eligible(guid, ab)) {
4300 mutex_exit(hash_lock);
4304 if ((write_sz + ab->b_size) > target_sz) {
4306 mutex_exit(hash_lock);
4312 * Insert a dummy header on the buflist so
4313 * l2arc_write_done() can find where the
4314 * write buffers begin without searching.
4316 list_insert_head(dev->l2ad_buflist, head);
4319 sizeof (l2arc_write_callback_t), KM_SLEEP);
4320 cb->l2wcb_dev = dev;
4321 cb->l2wcb_head = head;
4322 pio = zio_root(spa, l2arc_write_done, cb,
4327 * Create and add a new L2ARC header.
4329 hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
4331 hdrl2->b_daddr = dev->l2ad_hand;
4333 ab->b_flags |= ARC_L2_WRITING;
4334 ab->b_l2hdr = hdrl2;
4335 list_insert_head(dev->l2ad_buflist, ab);
4336 buf_data = ab->b_buf->b_data;
4337 buf_sz = ab->b_size;
4340 * Compute and store the buffer cksum before
4341 * writing. On debug the cksum is verified first.
4343 arc_cksum_verify(ab->b_buf);
4344 arc_cksum_compute(ab->b_buf, B_TRUE);
4346 mutex_exit(hash_lock);
4348 wzio = zio_write_phys(pio, dev->l2ad_vdev,
4349 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
4350 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
4351 ZIO_FLAG_CANFAIL, B_FALSE);
4353 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
4355 (void) zio_nowait(wzio);
4358 * Keep the clock hand suitably device-aligned.
4360 buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
4363 dev->l2ad_hand += buf_sz;
4366 mutex_exit(list_lock);
4371 mutex_exit(&l2arc_buflist_mtx);
4374 ASSERT3U(write_sz, ==, 0);
4375 kmem_cache_free(hdr_cache, head);
4379 ASSERT3U(write_sz, <=, target_sz);
4380 ARCSTAT_BUMP(arcstat_l2_writes_sent);
4381 ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz);
4382 ARCSTAT_INCR(arcstat_l2_size, write_sz);
4383 vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0);
4386 * Bump device hand to the device start if it is approaching the end.
4387 * l2arc_evict() will already have evicted ahead for this case.
4389 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
4390 vdev_space_update(dev->l2ad_vdev,
4391 dev->l2ad_end - dev->l2ad_hand, 0, 0);
4392 dev->l2ad_hand = dev->l2ad_start;
4393 dev->l2ad_evict = dev->l2ad_start;
4394 dev->l2ad_first = B_FALSE;
4397 dev->l2ad_writing = B_TRUE;
4398 (void) zio_wait(pio);
4399 dev->l2ad_writing = B_FALSE;
4405 * This thread feeds the L2ARC at regular intervals. This is the beating
4406 * heart of the L2ARC.
4409 l2arc_feed_thread(void)
4414 uint64_t size, wrote;
4415 clock_t begin, next = ddi_get_lbolt();
4417 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
4419 mutex_enter(&l2arc_feed_thr_lock);
4421 while (l2arc_thread_exit == 0) {
4422 CALLB_CPR_SAFE_BEGIN(&cpr);
4423 (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock,
4425 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
4426 next = ddi_get_lbolt() + hz;
4429 * Quick check for L2ARC devices.
4431 mutex_enter(&l2arc_dev_mtx);
4432 if (l2arc_ndev == 0) {
4433 mutex_exit(&l2arc_dev_mtx);
4436 mutex_exit(&l2arc_dev_mtx);
4437 begin = ddi_get_lbolt();
4440 * This selects the next l2arc device to write to, and in
4441 * doing so the next spa to feed from: dev->l2ad_spa. This
4442 * will return NULL if there are now no l2arc devices or if
4443 * they are all faulted.
4445 * If a device is returned, its spa's config lock is also
4446 * held to prevent device removal. l2arc_dev_get_next()
4447 * will grab and release l2arc_dev_mtx.
4449 if ((dev = l2arc_dev_get_next()) == NULL)
4452 spa = dev->l2ad_spa;
4453 ASSERT(spa != NULL);
4456 * If the pool is read-only then force the feed thread to
4457 * sleep a little longer.
4459 if (!spa_writeable(spa)) {
4460 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
4461 spa_config_exit(spa, SCL_L2ARC, dev);
4466 * Avoid contributing to memory pressure.
4468 if (arc_reclaim_needed()) {
4469 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
4470 spa_config_exit(spa, SCL_L2ARC, dev);
4474 ARCSTAT_BUMP(arcstat_l2_feeds);
4476 size = l2arc_write_size(dev);
4479 * Evict L2ARC buffers that will be overwritten.
4481 l2arc_evict(dev, size, B_FALSE);
4484 * Write ARC buffers.
4486 wrote = l2arc_write_buffers(spa, dev, size);
4489 * Calculate interval between writes.
4491 next = l2arc_write_interval(begin, size, wrote);
4492 spa_config_exit(spa, SCL_L2ARC, dev);
4495 l2arc_thread_exit = 0;
4496 cv_broadcast(&l2arc_feed_thr_cv);
4497 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
4502 l2arc_vdev_present(vdev_t *vd)
4506 mutex_enter(&l2arc_dev_mtx);
4507 for (dev = list_head(l2arc_dev_list); dev != NULL;
4508 dev = list_next(l2arc_dev_list, dev)) {
4509 if (dev->l2ad_vdev == vd)
4512 mutex_exit(&l2arc_dev_mtx);
4514 return (dev != NULL);
4518 * Add a vdev for use by the L2ARC. By this point the spa has already
4519 * validated the vdev and opened it.
4522 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
4524 l2arc_dev_t *adddev;
4526 ASSERT(!l2arc_vdev_present(vd));
4529 * Create a new l2arc device entry.
4531 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
4532 adddev->l2ad_spa = spa;
4533 adddev->l2ad_vdev = vd;
4534 adddev->l2ad_write = l2arc_write_max;
4535 adddev->l2ad_boost = l2arc_write_boost;
4536 adddev->l2ad_start = VDEV_LABEL_START_SIZE;
4537 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
4538 adddev->l2ad_hand = adddev->l2ad_start;
4539 adddev->l2ad_evict = adddev->l2ad_start;
4540 adddev->l2ad_first = B_TRUE;
4541 adddev->l2ad_writing = B_FALSE;
4542 list_link_init(&adddev->l2ad_node);
4543 ASSERT3U(adddev->l2ad_write, >, 0);
4546 * This is a list of all ARC buffers that are still valid on the
4549 adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
4550 list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
4551 offsetof(arc_buf_hdr_t, b_l2node));
4553 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
4556 * Add device to global list
4558 mutex_enter(&l2arc_dev_mtx);
4559 list_insert_head(l2arc_dev_list, adddev);
4560 atomic_inc_64(&l2arc_ndev);
4561 mutex_exit(&l2arc_dev_mtx);
4565 * Remove a vdev from the L2ARC.
4568 l2arc_remove_vdev(vdev_t *vd)
4570 l2arc_dev_t *dev, *nextdev, *remdev = NULL;
4573 * Find the device by vdev
4575 mutex_enter(&l2arc_dev_mtx);
4576 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
4577 nextdev = list_next(l2arc_dev_list, dev);
4578 if (vd == dev->l2ad_vdev) {
4583 ASSERT(remdev != NULL);
4586 * Remove device from global list
4588 list_remove(l2arc_dev_list, remdev);
4589 l2arc_dev_last = NULL; /* may have been invalidated */
4590 atomic_dec_64(&l2arc_ndev);
4591 mutex_exit(&l2arc_dev_mtx);
4594 * Clear all buflists and ARC references. L2ARC device flush.
4596 l2arc_evict(remdev, 0, B_TRUE);
4597 list_destroy(remdev->l2ad_buflist);
4598 kmem_free(remdev->l2ad_buflist, sizeof (list_t));
4599 kmem_free(remdev, sizeof (l2arc_dev_t));
4605 l2arc_thread_exit = 0;
4607 l2arc_writes_sent = 0;
4608 l2arc_writes_done = 0;
4610 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
4611 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
4612 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
4613 mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
4614 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
4616 l2arc_dev_list = &L2ARC_dev_list;
4617 l2arc_free_on_write = &L2ARC_free_on_write;
4618 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
4619 offsetof(l2arc_dev_t, l2ad_node));
4620 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
4621 offsetof(l2arc_data_free_t, l2df_list_node));
4628 * This is called from dmu_fini(), which is called from spa_fini();
4629 * Because of this, we can assume that all l2arc devices have
4630 * already been removed when the pools themselves were removed.
4633 l2arc_do_free_on_write();
4635 mutex_destroy(&l2arc_feed_thr_lock);
4636 cv_destroy(&l2arc_feed_thr_cv);
4637 mutex_destroy(&l2arc_dev_mtx);
4638 mutex_destroy(&l2arc_buflist_mtx);
4639 mutex_destroy(&l2arc_free_on_write_mtx);
4641 list_destroy(l2arc_dev_list);
4642 list_destroy(l2arc_free_on_write);
4648 if (!(spa_mode_global & FWRITE))
4651 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
4652 TS_RUN, minclsyspri);
4658 if (!(spa_mode_global & FWRITE))
4661 mutex_enter(&l2arc_feed_thr_lock);
4662 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
4663 l2arc_thread_exit = 1;
4664 while (l2arc_thread_exit != 0)
4665 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
4666 mutex_exit(&l2arc_feed_thr_lock);