4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2011 by Delphix. All rights reserved.
28 * DVA-based Adjustable Replacement Cache
30 * While much of the theory of operation used here is
31 * based on the self-tuning, low overhead replacement cache
32 * presented by Megiddo and Modha at FAST 2003, there are some
33 * significant differences:
35 * 1. The Megiddo and Modha model assumes any page is evictable.
36 * Pages in its cache cannot be "locked" into memory. This makes
37 * the eviction algorithm simple: evict the last page in the list.
38 * This also make the performance characteristics easy to reason
39 * about. Our cache is not so simple. At any given moment, some
40 * subset of the blocks in the cache are un-evictable because we
41 * have handed out a reference to them. Blocks are only evictable
42 * when there are no external references active. This makes
43 * eviction far more problematic: we choose to evict the evictable
44 * blocks that are the "lowest" in the list.
46 * There are times when it is not possible to evict the requested
47 * space. In these circumstances we are unable to adjust the cache
48 * size. To prevent the cache growing unbounded at these times we
49 * implement a "cache throttle" that slows the flow of new data
50 * into the cache until we can make space available.
52 * 2. The Megiddo and Modha model assumes a fixed cache size.
53 * Pages are evicted when the cache is full and there is a cache
54 * miss. Our model has a variable sized cache. It grows with
55 * high use, but also tries to react to memory pressure from the
56 * operating system: decreasing its size when system memory is
59 * 3. The Megiddo and Modha model assumes a fixed page size. All
60 * elements of the cache are therefor exactly the same size. So
61 * when adjusting the cache size following a cache miss, its simply
62 * a matter of choosing a single page to evict. In our model, we
63 * have variable sized cache blocks (rangeing from 512 bytes to
64 * 128K bytes). We therefor choose a set of blocks to evict to make
65 * space for a cache miss that approximates as closely as possible
66 * the space used by the new block.
68 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
69 * by N. Megiddo & D. Modha, FAST 2003
75 * A new reference to a cache buffer can be obtained in two
76 * ways: 1) via a hash table lookup using the DVA as a key,
77 * or 2) via one of the ARC lists. The arc_read() interface
78 * uses method 1, while the internal arc algorithms for
79 * adjusting the cache use method 2. We therefor provide two
80 * types of locks: 1) the hash table lock array, and 2) the
83 * Buffers do not have their own mutexes, rather they rely on the
84 * hash table mutexes for the bulk of their protection (i.e. most
85 * fields in the arc_buf_hdr_t are protected by these mutexes).
87 * buf_hash_find() returns the appropriate mutex (held) when it
88 * locates the requested buffer in the hash table. It returns
89 * NULL for the mutex if the buffer was not in the table.
91 * buf_hash_remove() expects the appropriate hash mutex to be
92 * already held before it is invoked.
94 * Each arc state also has a mutex which is used to protect the
95 * buffer list associated with the state. When attempting to
96 * obtain a hash table lock while holding an arc list lock you
97 * must use: mutex_tryenter() to avoid deadlock. Also note that
98 * the active state mutex must be held before the ghost state mutex.
100 * Arc buffers may have an associated eviction callback function.
101 * This function will be invoked prior to removing the buffer (e.g.
102 * in arc_do_user_evicts()). Note however that the data associated
103 * with the buffer may be evicted prior to the callback. The callback
104 * must be made with *no locks held* (to prevent deadlock). Additionally,
105 * the users of callbacks must ensure that their private data is
106 * protected from simultaneous callbacks from arc_buf_evict()
107 * and arc_do_user_evicts().
109 * It as also possible to register a callback which is run when the
110 * arc_meta_limit is reached and no buffers can be safely evicted. In
111 * this case the arc user should drop a reference on some arc buffers so
112 * they can be reclaimed and the arc_meta_limit honored. For example,
113 * when using the ZPL each dentry holds a references on a znode. These
114 * dentries must be pruned before the arc buffer holding the znode can
117 * Note that the majority of the performance stats are manipulated
118 * with atomic operations.
120 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
122 * - L2ARC buflist creation
123 * - L2ARC buflist eviction
124 * - L2ARC write completion, which walks L2ARC buflists
125 * - ARC header destruction, as it removes from L2ARC buflists
126 * - ARC header release, as it removes from L2ARC buflists
131 #include <sys/zfs_context.h>
133 #include <sys/vdev.h>
134 #include <sys/vdev_impl.h>
136 #include <sys/vmsystm.h>
138 #include <sys/fs/swapnode.h>
141 #include <sys/callb.h>
142 #include <sys/kstat.h>
143 #include <sys/dmu_tx.h>
144 #include <zfs_fletcher.h>
146 static kmutex_t arc_reclaim_thr_lock;
147 static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */
148 static uint8_t arc_thread_exit;
150 /* number of bytes to prune from caches when at arc_meta_limit is reached */
151 uint_t arc_meta_prune = 1048576;
153 typedef enum arc_reclaim_strategy {
154 ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */
155 ARC_RECLAIM_CONS /* Conservative reclaim strategy */
156 } arc_reclaim_strategy_t;
158 /* number of seconds before growing cache again */
159 static int arc_grow_retry = 5;
161 /* expiration time for arc_no_grow */
162 static clock_t arc_grow_time = 0;
164 /* shift of arc_c for calculating both min and max arc_p */
165 static int arc_p_min_shift = 4;
167 /* log2(fraction of arc to reclaim) */
168 static int arc_shrink_shift = 5;
171 * minimum lifespan of a prefetch block in clock ticks
172 * (initialized in arc_init())
174 static int arc_min_prefetch_lifespan;
179 * The arc has filled available memory and has now warmed up.
181 static boolean_t arc_warm;
184 * These tunables are for performance analysis.
186 unsigned long zfs_arc_max = 0;
187 unsigned long zfs_arc_min = 0;
188 unsigned long zfs_arc_meta_limit = 0;
189 int zfs_arc_grow_retry = 0;
190 int zfs_arc_shrink_shift = 0;
191 int zfs_arc_p_min_shift = 0;
192 int zfs_disable_dup_eviction = 0;
193 int zfs_arc_meta_prune = 0;
196 * Note that buffers can be in one of 6 states:
197 * ARC_anon - anonymous (discussed below)
198 * ARC_mru - recently used, currently cached
199 * ARC_mru_ghost - recentely used, no longer in cache
200 * ARC_mfu - frequently used, currently cached
201 * ARC_mfu_ghost - frequently used, no longer in cache
202 * ARC_l2c_only - exists in L2ARC but not other states
203 * When there are no active references to the buffer, they are
204 * are linked onto a list in one of these arc states. These are
205 * the only buffers that can be evicted or deleted. Within each
206 * state there are multiple lists, one for meta-data and one for
207 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
208 * etc.) is tracked separately so that it can be managed more
209 * explicitly: favored over data, limited explicitly.
211 * Anonymous buffers are buffers that are not associated with
212 * a DVA. These are buffers that hold dirty block copies
213 * before they are written to stable storage. By definition,
214 * they are "ref'd" and are considered part of arc_mru
215 * that cannot be freed. Generally, they will aquire a DVA
216 * as they are written and migrate onto the arc_mru list.
218 * The ARC_l2c_only state is for buffers that are in the second
219 * level ARC but no longer in any of the ARC_m* lists. The second
220 * level ARC itself may also contain buffers that are in any of
221 * the ARC_m* states - meaning that a buffer can exist in two
222 * places. The reason for the ARC_l2c_only state is to keep the
223 * buffer header in the hash table, so that reads that hit the
224 * second level ARC benefit from these fast lookups.
227 typedef struct arc_state {
228 list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */
229 uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */
230 uint64_t arcs_size; /* total amount of data in this state */
235 static arc_state_t ARC_anon;
236 static arc_state_t ARC_mru;
237 static arc_state_t ARC_mru_ghost;
238 static arc_state_t ARC_mfu;
239 static arc_state_t ARC_mfu_ghost;
240 static arc_state_t ARC_l2c_only;
242 typedef struct arc_stats {
243 kstat_named_t arcstat_hits;
244 kstat_named_t arcstat_misses;
245 kstat_named_t arcstat_demand_data_hits;
246 kstat_named_t arcstat_demand_data_misses;
247 kstat_named_t arcstat_demand_metadata_hits;
248 kstat_named_t arcstat_demand_metadata_misses;
249 kstat_named_t arcstat_prefetch_data_hits;
250 kstat_named_t arcstat_prefetch_data_misses;
251 kstat_named_t arcstat_prefetch_metadata_hits;
252 kstat_named_t arcstat_prefetch_metadata_misses;
253 kstat_named_t arcstat_mru_hits;
254 kstat_named_t arcstat_mru_ghost_hits;
255 kstat_named_t arcstat_mfu_hits;
256 kstat_named_t arcstat_mfu_ghost_hits;
257 kstat_named_t arcstat_deleted;
258 kstat_named_t arcstat_recycle_miss;
259 kstat_named_t arcstat_mutex_miss;
260 kstat_named_t arcstat_evict_skip;
261 kstat_named_t arcstat_evict_l2_cached;
262 kstat_named_t arcstat_evict_l2_eligible;
263 kstat_named_t arcstat_evict_l2_ineligible;
264 kstat_named_t arcstat_hash_elements;
265 kstat_named_t arcstat_hash_elements_max;
266 kstat_named_t arcstat_hash_collisions;
267 kstat_named_t arcstat_hash_chains;
268 kstat_named_t arcstat_hash_chain_max;
269 kstat_named_t arcstat_p;
270 kstat_named_t arcstat_c;
271 kstat_named_t arcstat_c_min;
272 kstat_named_t arcstat_c_max;
273 kstat_named_t arcstat_size;
274 kstat_named_t arcstat_hdr_size;
275 kstat_named_t arcstat_data_size;
276 kstat_named_t arcstat_other_size;
277 kstat_named_t arcstat_anon_size;
278 kstat_named_t arcstat_anon_evict_data;
279 kstat_named_t arcstat_anon_evict_metadata;
280 kstat_named_t arcstat_mru_size;
281 kstat_named_t arcstat_mru_evict_data;
282 kstat_named_t arcstat_mru_evict_metadata;
283 kstat_named_t arcstat_mru_ghost_size;
284 kstat_named_t arcstat_mru_ghost_evict_data;
285 kstat_named_t arcstat_mru_ghost_evict_metadata;
286 kstat_named_t arcstat_mfu_size;
287 kstat_named_t arcstat_mfu_evict_data;
288 kstat_named_t arcstat_mfu_evict_metadata;
289 kstat_named_t arcstat_mfu_ghost_size;
290 kstat_named_t arcstat_mfu_ghost_evict_data;
291 kstat_named_t arcstat_mfu_ghost_evict_metadata;
292 kstat_named_t arcstat_l2_hits;
293 kstat_named_t arcstat_l2_misses;
294 kstat_named_t arcstat_l2_feeds;
295 kstat_named_t arcstat_l2_rw_clash;
296 kstat_named_t arcstat_l2_read_bytes;
297 kstat_named_t arcstat_l2_write_bytes;
298 kstat_named_t arcstat_l2_writes_sent;
299 kstat_named_t arcstat_l2_writes_done;
300 kstat_named_t arcstat_l2_writes_error;
301 kstat_named_t arcstat_l2_writes_hdr_miss;
302 kstat_named_t arcstat_l2_evict_lock_retry;
303 kstat_named_t arcstat_l2_evict_reading;
304 kstat_named_t arcstat_l2_free_on_write;
305 kstat_named_t arcstat_l2_abort_lowmem;
306 kstat_named_t arcstat_l2_cksum_bad;
307 kstat_named_t arcstat_l2_io_error;
308 kstat_named_t arcstat_l2_size;
309 kstat_named_t arcstat_l2_hdr_size;
310 kstat_named_t arcstat_memory_throttle_count;
311 kstat_named_t arcstat_duplicate_buffers;
312 kstat_named_t arcstat_duplicate_buffers_size;
313 kstat_named_t arcstat_duplicate_reads;
314 kstat_named_t arcstat_memory_direct_count;
315 kstat_named_t arcstat_memory_indirect_count;
316 kstat_named_t arcstat_no_grow;
317 kstat_named_t arcstat_tempreserve;
318 kstat_named_t arcstat_loaned_bytes;
319 kstat_named_t arcstat_prune;
320 kstat_named_t arcstat_meta_used;
321 kstat_named_t arcstat_meta_limit;
322 kstat_named_t arcstat_meta_max;
325 static arc_stats_t arc_stats = {
326 { "hits", KSTAT_DATA_UINT64 },
327 { "misses", KSTAT_DATA_UINT64 },
328 { "demand_data_hits", KSTAT_DATA_UINT64 },
329 { "demand_data_misses", KSTAT_DATA_UINT64 },
330 { "demand_metadata_hits", KSTAT_DATA_UINT64 },
331 { "demand_metadata_misses", KSTAT_DATA_UINT64 },
332 { "prefetch_data_hits", KSTAT_DATA_UINT64 },
333 { "prefetch_data_misses", KSTAT_DATA_UINT64 },
334 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
335 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
336 { "mru_hits", KSTAT_DATA_UINT64 },
337 { "mru_ghost_hits", KSTAT_DATA_UINT64 },
338 { "mfu_hits", KSTAT_DATA_UINT64 },
339 { "mfu_ghost_hits", KSTAT_DATA_UINT64 },
340 { "deleted", KSTAT_DATA_UINT64 },
341 { "recycle_miss", KSTAT_DATA_UINT64 },
342 { "mutex_miss", KSTAT_DATA_UINT64 },
343 { "evict_skip", KSTAT_DATA_UINT64 },
344 { "evict_l2_cached", KSTAT_DATA_UINT64 },
345 { "evict_l2_eligible", KSTAT_DATA_UINT64 },
346 { "evict_l2_ineligible", KSTAT_DATA_UINT64 },
347 { "hash_elements", KSTAT_DATA_UINT64 },
348 { "hash_elements_max", KSTAT_DATA_UINT64 },
349 { "hash_collisions", KSTAT_DATA_UINT64 },
350 { "hash_chains", KSTAT_DATA_UINT64 },
351 { "hash_chain_max", KSTAT_DATA_UINT64 },
352 { "p", KSTAT_DATA_UINT64 },
353 { "c", KSTAT_DATA_UINT64 },
354 { "c_min", KSTAT_DATA_UINT64 },
355 { "c_max", KSTAT_DATA_UINT64 },
356 { "size", KSTAT_DATA_UINT64 },
357 { "hdr_size", KSTAT_DATA_UINT64 },
358 { "data_size", KSTAT_DATA_UINT64 },
359 { "other_size", KSTAT_DATA_UINT64 },
360 { "anon_size", KSTAT_DATA_UINT64 },
361 { "anon_evict_data", KSTAT_DATA_UINT64 },
362 { "anon_evict_metadata", KSTAT_DATA_UINT64 },
363 { "mru_size", KSTAT_DATA_UINT64 },
364 { "mru_evict_data", KSTAT_DATA_UINT64 },
365 { "mru_evict_metadata", KSTAT_DATA_UINT64 },
366 { "mru_ghost_size", KSTAT_DATA_UINT64 },
367 { "mru_ghost_evict_data", KSTAT_DATA_UINT64 },
368 { "mru_ghost_evict_metadata", KSTAT_DATA_UINT64 },
369 { "mfu_size", KSTAT_DATA_UINT64 },
370 { "mfu_evict_data", KSTAT_DATA_UINT64 },
371 { "mfu_evict_metadata", KSTAT_DATA_UINT64 },
372 { "mfu_ghost_size", KSTAT_DATA_UINT64 },
373 { "mfu_ghost_evict_data", KSTAT_DATA_UINT64 },
374 { "mfu_ghost_evict_metadata", KSTAT_DATA_UINT64 },
375 { "l2_hits", KSTAT_DATA_UINT64 },
376 { "l2_misses", KSTAT_DATA_UINT64 },
377 { "l2_feeds", KSTAT_DATA_UINT64 },
378 { "l2_rw_clash", KSTAT_DATA_UINT64 },
379 { "l2_read_bytes", KSTAT_DATA_UINT64 },
380 { "l2_write_bytes", KSTAT_DATA_UINT64 },
381 { "l2_writes_sent", KSTAT_DATA_UINT64 },
382 { "l2_writes_done", KSTAT_DATA_UINT64 },
383 { "l2_writes_error", KSTAT_DATA_UINT64 },
384 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 },
385 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
386 { "l2_evict_reading", KSTAT_DATA_UINT64 },
387 { "l2_free_on_write", KSTAT_DATA_UINT64 },
388 { "l2_abort_lowmem", KSTAT_DATA_UINT64 },
389 { "l2_cksum_bad", KSTAT_DATA_UINT64 },
390 { "l2_io_error", KSTAT_DATA_UINT64 },
391 { "l2_size", KSTAT_DATA_UINT64 },
392 { "l2_hdr_size", KSTAT_DATA_UINT64 },
393 { "memory_throttle_count", KSTAT_DATA_UINT64 },
394 { "duplicate_buffers", KSTAT_DATA_UINT64 },
395 { "duplicate_buffers_size", KSTAT_DATA_UINT64 },
396 { "duplicate_reads", KSTAT_DATA_UINT64 },
397 { "memory_direct_count", KSTAT_DATA_UINT64 },
398 { "memory_indirect_count", KSTAT_DATA_UINT64 },
399 { "arc_no_grow", KSTAT_DATA_UINT64 },
400 { "arc_tempreserve", KSTAT_DATA_UINT64 },
401 { "arc_loaned_bytes", KSTAT_DATA_UINT64 },
402 { "arc_prune", KSTAT_DATA_UINT64 },
403 { "arc_meta_used", KSTAT_DATA_UINT64 },
404 { "arc_meta_limit", KSTAT_DATA_UINT64 },
405 { "arc_meta_max", KSTAT_DATA_UINT64 },
408 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
410 #define ARCSTAT_INCR(stat, val) \
411 atomic_add_64(&arc_stats.stat.value.ui64, (val));
413 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
414 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
416 #define ARCSTAT_MAX(stat, val) { \
418 while ((val) > (m = arc_stats.stat.value.ui64) && \
419 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
423 #define ARCSTAT_MAXSTAT(stat) \
424 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
427 * We define a macro to allow ARC hits/misses to be easily broken down by
428 * two separate conditions, giving a total of four different subtypes for
429 * each of hits and misses (so eight statistics total).
431 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
434 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
436 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
440 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
442 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
447 static arc_state_t *arc_anon;
448 static arc_state_t *arc_mru;
449 static arc_state_t *arc_mru_ghost;
450 static arc_state_t *arc_mfu;
451 static arc_state_t *arc_mfu_ghost;
452 static arc_state_t *arc_l2c_only;
455 * There are several ARC variables that are critical to export as kstats --
456 * but we don't want to have to grovel around in the kstat whenever we wish to
457 * manipulate them. For these variables, we therefore define them to be in
458 * terms of the statistic variable. This assures that we are not introducing
459 * the possibility of inconsistency by having shadow copies of the variables,
460 * while still allowing the code to be readable.
462 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
463 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
464 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
465 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
466 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
467 #define arc_no_grow ARCSTAT(arcstat_no_grow)
468 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
469 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
470 #define arc_meta_used ARCSTAT(arcstat_meta_used)
471 #define arc_meta_limit ARCSTAT(arcstat_meta_limit)
472 #define arc_meta_max ARCSTAT(arcstat_meta_max)
474 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
476 typedef struct arc_callback arc_callback_t;
478 struct arc_callback {
480 arc_done_func_t *acb_done;
482 zio_t *acb_zio_dummy;
483 arc_callback_t *acb_next;
486 typedef struct arc_write_callback arc_write_callback_t;
488 struct arc_write_callback {
490 arc_done_func_t *awcb_ready;
491 arc_done_func_t *awcb_done;
496 /* protected by hash lock */
501 kmutex_t b_freeze_lock;
502 zio_cksum_t *b_freeze_cksum;
505 arc_buf_hdr_t *b_hash_next;
510 arc_callback_t *b_acb;
514 arc_buf_contents_t b_type;
518 /* protected by arc state mutex */
519 arc_state_t *b_state;
520 list_node_t b_arc_node;
522 /* updated atomically */
523 clock_t b_arc_access;
525 /* self protecting */
528 l2arc_buf_hdr_t *b_l2hdr;
529 list_node_t b_l2node;
532 static list_t arc_prune_list;
533 static kmutex_t arc_prune_mtx;
534 static arc_buf_t *arc_eviction_list;
535 static kmutex_t arc_eviction_mtx;
536 static arc_buf_hdr_t arc_eviction_hdr;
537 static void arc_get_data_buf(arc_buf_t *buf);
538 static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
539 static int arc_evict_needed(arc_buf_contents_t type);
540 static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
542 static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
544 #define GHOST_STATE(state) \
545 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
546 (state) == arc_l2c_only)
549 * Private ARC flags. These flags are private ARC only flags that will show up
550 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
551 * be passed in as arc_flags in things like arc_read. However, these flags
552 * should never be passed and should only be set by ARC code. When adding new
553 * public flags, make sure not to smash the private ones.
556 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
557 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
558 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
559 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
560 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
561 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
562 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
563 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
564 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
565 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
567 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
568 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
569 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
570 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
571 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
572 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
573 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
574 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
575 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
576 (hdr)->b_l2hdr != NULL)
577 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
578 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
579 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
585 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
586 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
589 * Hash table routines
592 #define HT_LOCK_ALIGN 64
593 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
598 unsigned char pad[HT_LOCK_PAD];
602 #define BUF_LOCKS 256
603 typedef struct buf_hash_table {
605 arc_buf_hdr_t **ht_table;
606 struct ht_lock ht_locks[BUF_LOCKS];
609 static buf_hash_table_t buf_hash_table;
611 #define BUF_HASH_INDEX(spa, dva, birth) \
612 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
613 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
614 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
615 #define HDR_LOCK(hdr) \
616 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
618 uint64_t zfs_crc64_table[256];
624 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
625 #define L2ARC_HEADROOM 2 /* num of writes */
626 #define L2ARC_FEED_SECS 1 /* caching interval secs */
627 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
629 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
630 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
633 * L2ARC Performance Tunables
635 unsigned long l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */
636 unsigned long l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */
637 unsigned long l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */
638 unsigned long l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */
639 unsigned long l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */
640 int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */
641 int l2arc_feed_again = B_TRUE; /* turbo warmup */
642 int l2arc_norw = B_TRUE; /* no reads during writes */
647 typedef struct l2arc_dev {
648 vdev_t *l2ad_vdev; /* vdev */
649 spa_t *l2ad_spa; /* spa */
650 uint64_t l2ad_hand; /* next write location */
651 uint64_t l2ad_write; /* desired write size, bytes */
652 uint64_t l2ad_boost; /* warmup write boost, bytes */
653 uint64_t l2ad_start; /* first addr on device */
654 uint64_t l2ad_end; /* last addr on device */
655 uint64_t l2ad_evict; /* last addr eviction reached */
656 boolean_t l2ad_first; /* first sweep through */
657 boolean_t l2ad_writing; /* currently writing */
658 list_t *l2ad_buflist; /* buffer list */
659 list_node_t l2ad_node; /* device list node */
662 static list_t L2ARC_dev_list; /* device list */
663 static list_t *l2arc_dev_list; /* device list pointer */
664 static kmutex_t l2arc_dev_mtx; /* device list mutex */
665 static l2arc_dev_t *l2arc_dev_last; /* last device used */
666 static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */
667 static list_t L2ARC_free_on_write; /* free after write buf list */
668 static list_t *l2arc_free_on_write; /* free after write list ptr */
669 static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */
670 static uint64_t l2arc_ndev; /* number of devices */
672 typedef struct l2arc_read_callback {
673 arc_buf_t *l2rcb_buf; /* read buffer */
674 spa_t *l2rcb_spa; /* spa */
675 blkptr_t l2rcb_bp; /* original blkptr */
676 zbookmark_t l2rcb_zb; /* original bookmark */
677 int l2rcb_flags; /* original flags */
678 } l2arc_read_callback_t;
680 typedef struct l2arc_write_callback {
681 l2arc_dev_t *l2wcb_dev; /* device info */
682 arc_buf_hdr_t *l2wcb_head; /* head of write buflist */
683 } l2arc_write_callback_t;
685 struct l2arc_buf_hdr {
686 /* protected by arc_buf_hdr mutex */
687 l2arc_dev_t *b_dev; /* L2ARC device */
688 uint64_t b_daddr; /* disk address, offset byte */
691 typedef struct l2arc_data_free {
692 /* protected by l2arc_free_on_write_mtx */
695 void (*l2df_func)(void *, size_t);
696 list_node_t l2df_list_node;
699 static kmutex_t l2arc_feed_thr_lock;
700 static kcondvar_t l2arc_feed_thr_cv;
701 static uint8_t l2arc_thread_exit;
703 static void l2arc_read_done(zio_t *zio);
704 static void l2arc_hdr_stat_add(void);
705 static void l2arc_hdr_stat_remove(void);
708 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
710 uint8_t *vdva = (uint8_t *)dva;
711 uint64_t crc = -1ULL;
714 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
716 for (i = 0; i < sizeof (dva_t); i++)
717 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
719 crc ^= (spa>>8) ^ birth;
724 #define BUF_EMPTY(buf) \
725 ((buf)->b_dva.dva_word[0] == 0 && \
726 (buf)->b_dva.dva_word[1] == 0 && \
729 #define BUF_EQUAL(spa, dva, birth, buf) \
730 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
731 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
732 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
735 buf_discard_identity(arc_buf_hdr_t *hdr)
737 hdr->b_dva.dva_word[0] = 0;
738 hdr->b_dva.dva_word[1] = 0;
743 static arc_buf_hdr_t *
744 buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp)
746 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
747 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
750 mutex_enter(hash_lock);
751 for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
752 buf = buf->b_hash_next) {
753 if (BUF_EQUAL(spa, dva, birth, buf)) {
758 mutex_exit(hash_lock);
764 * Insert an entry into the hash table. If there is already an element
765 * equal to elem in the hash table, then the already existing element
766 * will be returned and the new element will not be inserted.
767 * Otherwise returns NULL.
769 static arc_buf_hdr_t *
770 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
772 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
773 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
777 ASSERT(!HDR_IN_HASH_TABLE(buf));
779 mutex_enter(hash_lock);
780 for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
781 fbuf = fbuf->b_hash_next, i++) {
782 if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
786 buf->b_hash_next = buf_hash_table.ht_table[idx];
787 buf_hash_table.ht_table[idx] = buf;
788 buf->b_flags |= ARC_IN_HASH_TABLE;
790 /* collect some hash table performance data */
792 ARCSTAT_BUMP(arcstat_hash_collisions);
794 ARCSTAT_BUMP(arcstat_hash_chains);
796 ARCSTAT_MAX(arcstat_hash_chain_max, i);
799 ARCSTAT_BUMP(arcstat_hash_elements);
800 ARCSTAT_MAXSTAT(arcstat_hash_elements);
806 buf_hash_remove(arc_buf_hdr_t *buf)
808 arc_buf_hdr_t *fbuf, **bufp;
809 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
811 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
812 ASSERT(HDR_IN_HASH_TABLE(buf));
814 bufp = &buf_hash_table.ht_table[idx];
815 while ((fbuf = *bufp) != buf) {
816 ASSERT(fbuf != NULL);
817 bufp = &fbuf->b_hash_next;
819 *bufp = buf->b_hash_next;
820 buf->b_hash_next = NULL;
821 buf->b_flags &= ~ARC_IN_HASH_TABLE;
823 /* collect some hash table performance data */
824 ARCSTAT_BUMPDOWN(arcstat_hash_elements);
826 if (buf_hash_table.ht_table[idx] &&
827 buf_hash_table.ht_table[idx]->b_hash_next == NULL)
828 ARCSTAT_BUMPDOWN(arcstat_hash_chains);
832 * Global data structures and functions for the buf kmem cache.
834 static kmem_cache_t *hdr_cache;
835 static kmem_cache_t *buf_cache;
842 #if defined(_KERNEL) && defined(HAVE_SPL)
843 /* Large allocations which do not require contiguous pages
844 * should be using vmem_free() in the linux kernel */
845 vmem_free(buf_hash_table.ht_table,
846 (buf_hash_table.ht_mask + 1) * sizeof (void *));
848 kmem_free(buf_hash_table.ht_table,
849 (buf_hash_table.ht_mask + 1) * sizeof (void *));
851 for (i = 0; i < BUF_LOCKS; i++)
852 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
853 kmem_cache_destroy(hdr_cache);
854 kmem_cache_destroy(buf_cache);
858 * Constructor callback - called when the cache is empty
859 * and a new buf is requested.
863 hdr_cons(void *vbuf, void *unused, int kmflag)
865 arc_buf_hdr_t *buf = vbuf;
867 bzero(buf, sizeof (arc_buf_hdr_t));
868 refcount_create(&buf->b_refcnt);
869 cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
870 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
871 list_link_init(&buf->b_arc_node);
872 list_link_init(&buf->b_l2node);
873 arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
880 buf_cons(void *vbuf, void *unused, int kmflag)
882 arc_buf_t *buf = vbuf;
884 bzero(buf, sizeof (arc_buf_t));
885 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
886 rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL);
887 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
893 * Destructor callback - called when a cached buf is
894 * no longer required.
898 hdr_dest(void *vbuf, void *unused)
900 arc_buf_hdr_t *buf = vbuf;
902 ASSERT(BUF_EMPTY(buf));
903 refcount_destroy(&buf->b_refcnt);
904 cv_destroy(&buf->b_cv);
905 mutex_destroy(&buf->b_freeze_lock);
906 arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
911 buf_dest(void *vbuf, void *unused)
913 arc_buf_t *buf = vbuf;
915 mutex_destroy(&buf->b_evict_lock);
916 rw_destroy(&buf->b_data_lock);
917 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
924 uint64_t hsize = 1ULL << 12;
928 * The hash table is big enough to fill all of physical memory
929 * with an average 64K block size. The table will take up
930 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
932 while (hsize * 65536 < physmem * PAGESIZE)
935 buf_hash_table.ht_mask = hsize - 1;
936 #if defined(_KERNEL) && defined(HAVE_SPL)
937 /* Large allocations which do not require contiguous pages
938 * should be using vmem_alloc() in the linux kernel */
939 buf_hash_table.ht_table =
940 vmem_zalloc(hsize * sizeof (void*), KM_SLEEP);
942 buf_hash_table.ht_table =
943 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
945 if (buf_hash_table.ht_table == NULL) {
946 ASSERT(hsize > (1ULL << 8));
951 hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
952 0, hdr_cons, hdr_dest, NULL, NULL, NULL, 0);
953 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
954 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
956 for (i = 0; i < 256; i++)
957 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
958 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
960 for (i = 0; i < BUF_LOCKS; i++) {
961 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
962 NULL, MUTEX_DEFAULT, NULL);
966 #define ARC_MINTIME (hz>>4) /* 62 ms */
969 arc_cksum_verify(arc_buf_t *buf)
973 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
976 mutex_enter(&buf->b_hdr->b_freeze_lock);
977 if (buf->b_hdr->b_freeze_cksum == NULL ||
978 (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
979 mutex_exit(&buf->b_hdr->b_freeze_lock);
982 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
983 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
984 panic("buffer modified while frozen!");
985 mutex_exit(&buf->b_hdr->b_freeze_lock);
989 arc_cksum_equal(arc_buf_t *buf)
994 mutex_enter(&buf->b_hdr->b_freeze_lock);
995 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
996 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
997 mutex_exit(&buf->b_hdr->b_freeze_lock);
1003 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
1005 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
1008 mutex_enter(&buf->b_hdr->b_freeze_lock);
1009 if (buf->b_hdr->b_freeze_cksum != NULL) {
1010 mutex_exit(&buf->b_hdr->b_freeze_lock);
1013 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t),
1015 fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
1016 buf->b_hdr->b_freeze_cksum);
1017 mutex_exit(&buf->b_hdr->b_freeze_lock);
1021 arc_buf_thaw(arc_buf_t *buf)
1023 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1024 if (buf->b_hdr->b_state != arc_anon)
1025 panic("modifying non-anon buffer!");
1026 if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
1027 panic("modifying buffer while i/o in progress!");
1028 arc_cksum_verify(buf);
1031 mutex_enter(&buf->b_hdr->b_freeze_lock);
1032 if (buf->b_hdr->b_freeze_cksum != NULL) {
1033 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1034 buf->b_hdr->b_freeze_cksum = NULL;
1037 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1038 if (buf->b_hdr->b_thawed)
1039 kmem_free(buf->b_hdr->b_thawed, 1);
1040 buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
1043 mutex_exit(&buf->b_hdr->b_freeze_lock);
1047 arc_buf_freeze(arc_buf_t *buf)
1049 kmutex_t *hash_lock;
1051 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1054 hash_lock = HDR_LOCK(buf->b_hdr);
1055 mutex_enter(hash_lock);
1057 ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
1058 buf->b_hdr->b_state == arc_anon);
1059 arc_cksum_compute(buf, B_FALSE);
1060 mutex_exit(hash_lock);
1064 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1066 ASSERT(MUTEX_HELD(hash_lock));
1068 if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1069 (ab->b_state != arc_anon)) {
1070 uint64_t delta = ab->b_size * ab->b_datacnt;
1071 list_t *list = &ab->b_state->arcs_list[ab->b_type];
1072 uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1074 ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
1075 mutex_enter(&ab->b_state->arcs_mtx);
1076 ASSERT(list_link_active(&ab->b_arc_node));
1077 list_remove(list, ab);
1078 if (GHOST_STATE(ab->b_state)) {
1079 ASSERT3U(ab->b_datacnt, ==, 0);
1080 ASSERT3P(ab->b_buf, ==, NULL);
1084 ASSERT3U(*size, >=, delta);
1085 atomic_add_64(size, -delta);
1086 mutex_exit(&ab->b_state->arcs_mtx);
1087 /* remove the prefetch flag if we get a reference */
1088 if (ab->b_flags & ARC_PREFETCH)
1089 ab->b_flags &= ~ARC_PREFETCH;
1094 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1097 arc_state_t *state = ab->b_state;
1099 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1100 ASSERT(!GHOST_STATE(state));
1102 if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1103 (state != arc_anon)) {
1104 uint64_t *size = &state->arcs_lsize[ab->b_type];
1106 ASSERT(!MUTEX_HELD(&state->arcs_mtx));
1107 mutex_enter(&state->arcs_mtx);
1108 ASSERT(!list_link_active(&ab->b_arc_node));
1109 list_insert_head(&state->arcs_list[ab->b_type], ab);
1110 ASSERT(ab->b_datacnt > 0);
1111 atomic_add_64(size, ab->b_size * ab->b_datacnt);
1112 mutex_exit(&state->arcs_mtx);
1118 * Move the supplied buffer to the indicated state. The mutex
1119 * for the buffer must be held by the caller.
1122 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1124 arc_state_t *old_state = ab->b_state;
1125 int64_t refcnt = refcount_count(&ab->b_refcnt);
1126 uint64_t from_delta, to_delta;
1128 ASSERT(MUTEX_HELD(hash_lock));
1129 ASSERT(new_state != old_state);
1130 ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1131 ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1132 ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1134 from_delta = to_delta = ab->b_datacnt * ab->b_size;
1137 * If this buffer is evictable, transfer it from the
1138 * old state list to the new state list.
1141 if (old_state != arc_anon) {
1142 int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
1143 uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1146 mutex_enter(&old_state->arcs_mtx);
1148 ASSERT(list_link_active(&ab->b_arc_node));
1149 list_remove(&old_state->arcs_list[ab->b_type], ab);
1152 * If prefetching out of the ghost cache,
1153 * we will have a non-zero datacnt.
1155 if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1156 /* ghost elements have a ghost size */
1157 ASSERT(ab->b_buf == NULL);
1158 from_delta = ab->b_size;
1160 ASSERT3U(*size, >=, from_delta);
1161 atomic_add_64(size, -from_delta);
1164 mutex_exit(&old_state->arcs_mtx);
1166 if (new_state != arc_anon) {
1167 int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
1168 uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1171 mutex_enter(&new_state->arcs_mtx);
1173 list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1175 /* ghost elements have a ghost size */
1176 if (GHOST_STATE(new_state)) {
1177 ASSERT(ab->b_datacnt == 0);
1178 ASSERT(ab->b_buf == NULL);
1179 to_delta = ab->b_size;
1181 atomic_add_64(size, to_delta);
1184 mutex_exit(&new_state->arcs_mtx);
1188 ASSERT(!BUF_EMPTY(ab));
1189 if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1190 buf_hash_remove(ab);
1192 /* adjust state sizes */
1194 atomic_add_64(&new_state->arcs_size, to_delta);
1196 ASSERT3U(old_state->arcs_size, >=, from_delta);
1197 atomic_add_64(&old_state->arcs_size, -from_delta);
1199 ab->b_state = new_state;
1201 /* adjust l2arc hdr stats */
1202 if (new_state == arc_l2c_only)
1203 l2arc_hdr_stat_add();
1204 else if (old_state == arc_l2c_only)
1205 l2arc_hdr_stat_remove();
1209 arc_space_consume(uint64_t space, arc_space_type_t type)
1211 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1216 case ARC_SPACE_DATA:
1217 ARCSTAT_INCR(arcstat_data_size, space);
1219 case ARC_SPACE_OTHER:
1220 ARCSTAT_INCR(arcstat_other_size, space);
1222 case ARC_SPACE_HDRS:
1223 ARCSTAT_INCR(arcstat_hdr_size, space);
1225 case ARC_SPACE_L2HDRS:
1226 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1230 atomic_add_64(&arc_meta_used, space);
1231 atomic_add_64(&arc_size, space);
1235 arc_space_return(uint64_t space, arc_space_type_t type)
1237 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1242 case ARC_SPACE_DATA:
1243 ARCSTAT_INCR(arcstat_data_size, -space);
1245 case ARC_SPACE_OTHER:
1246 ARCSTAT_INCR(arcstat_other_size, -space);
1248 case ARC_SPACE_HDRS:
1249 ARCSTAT_INCR(arcstat_hdr_size, -space);
1251 case ARC_SPACE_L2HDRS:
1252 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1256 ASSERT(arc_meta_used >= space);
1257 if (arc_meta_max < arc_meta_used)
1258 arc_meta_max = arc_meta_used;
1259 atomic_add_64(&arc_meta_used, -space);
1260 ASSERT(arc_size >= space);
1261 atomic_add_64(&arc_size, -space);
1265 arc_data_buf_alloc(uint64_t size)
1267 if (arc_evict_needed(ARC_BUFC_DATA))
1268 cv_signal(&arc_reclaim_thr_cv);
1269 atomic_add_64(&arc_size, size);
1270 return (zio_data_buf_alloc(size));
1274 arc_data_buf_free(void *buf, uint64_t size)
1276 zio_data_buf_free(buf, size);
1277 ASSERT(arc_size >= size);
1278 atomic_add_64(&arc_size, -size);
1282 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1287 ASSERT3U(size, >, 0);
1288 hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1289 ASSERT(BUF_EMPTY(hdr));
1292 hdr->b_spa = spa_load_guid(spa);
1293 hdr->b_state = arc_anon;
1294 hdr->b_arc_access = 0;
1295 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1298 buf->b_efunc = NULL;
1299 buf->b_private = NULL;
1302 arc_get_data_buf(buf);
1305 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1306 (void) refcount_add(&hdr->b_refcnt, tag);
1311 static char *arc_onloan_tag = "onloan";
1314 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1315 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1316 * buffers must be returned to the arc before they can be used by the DMU or
1320 arc_loan_buf(spa_t *spa, int size)
1324 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1326 atomic_add_64(&arc_loaned_bytes, size);
1331 * Return a loaned arc buffer to the arc.
1334 arc_return_buf(arc_buf_t *buf, void *tag)
1336 arc_buf_hdr_t *hdr = buf->b_hdr;
1338 ASSERT(buf->b_data != NULL);
1339 (void) refcount_add(&hdr->b_refcnt, tag);
1340 (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1342 atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1345 /* Detach an arc_buf from a dbuf (tag) */
1347 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1351 ASSERT(buf->b_data != NULL);
1353 (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1354 (void) refcount_remove(&hdr->b_refcnt, tag);
1355 buf->b_efunc = NULL;
1356 buf->b_private = NULL;
1358 atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1362 arc_buf_clone(arc_buf_t *from)
1365 arc_buf_hdr_t *hdr = from->b_hdr;
1366 uint64_t size = hdr->b_size;
1368 ASSERT(hdr->b_state != arc_anon);
1370 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1373 buf->b_efunc = NULL;
1374 buf->b_private = NULL;
1375 buf->b_next = hdr->b_buf;
1377 arc_get_data_buf(buf);
1378 bcopy(from->b_data, buf->b_data, size);
1381 * This buffer already exists in the arc so create a duplicate
1382 * copy for the caller. If the buffer is associated with user data
1383 * then track the size and number of duplicates. These stats will be
1384 * updated as duplicate buffers are created and destroyed.
1386 if (hdr->b_type == ARC_BUFC_DATA) {
1387 ARCSTAT_BUMP(arcstat_duplicate_buffers);
1388 ARCSTAT_INCR(arcstat_duplicate_buffers_size, size);
1390 hdr->b_datacnt += 1;
1395 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1398 kmutex_t *hash_lock;
1401 * Check to see if this buffer is evicted. Callers
1402 * must verify b_data != NULL to know if the add_ref
1405 mutex_enter(&buf->b_evict_lock);
1406 if (buf->b_data == NULL) {
1407 mutex_exit(&buf->b_evict_lock);
1410 hash_lock = HDR_LOCK(buf->b_hdr);
1411 mutex_enter(hash_lock);
1413 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1414 mutex_exit(&buf->b_evict_lock);
1416 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1417 add_reference(hdr, hash_lock, tag);
1418 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1419 arc_access(hdr, hash_lock);
1420 mutex_exit(hash_lock);
1421 ARCSTAT_BUMP(arcstat_hits);
1422 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1423 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1424 data, metadata, hits);
1428 * Free the arc data buffer. If it is an l2arc write in progress,
1429 * the buffer is placed on l2arc_free_on_write to be freed later.
1432 arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t),
1433 void *data, size_t size)
1435 if (HDR_L2_WRITING(hdr)) {
1436 l2arc_data_free_t *df;
1437 df = kmem_alloc(sizeof (l2arc_data_free_t), KM_PUSHPAGE);
1438 df->l2df_data = data;
1439 df->l2df_size = size;
1440 df->l2df_func = free_func;
1441 mutex_enter(&l2arc_free_on_write_mtx);
1442 list_insert_head(l2arc_free_on_write, df);
1443 mutex_exit(&l2arc_free_on_write_mtx);
1444 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1446 free_func(data, size);
1451 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1455 /* free up data associated with the buf */
1457 arc_state_t *state = buf->b_hdr->b_state;
1458 uint64_t size = buf->b_hdr->b_size;
1459 arc_buf_contents_t type = buf->b_hdr->b_type;
1461 arc_cksum_verify(buf);
1464 if (type == ARC_BUFC_METADATA) {
1465 arc_buf_data_free(buf->b_hdr, zio_buf_free,
1467 arc_space_return(size, ARC_SPACE_DATA);
1469 ASSERT(type == ARC_BUFC_DATA);
1470 arc_buf_data_free(buf->b_hdr,
1471 zio_data_buf_free, buf->b_data, size);
1472 ARCSTAT_INCR(arcstat_data_size, -size);
1473 atomic_add_64(&arc_size, -size);
1476 if (list_link_active(&buf->b_hdr->b_arc_node)) {
1477 uint64_t *cnt = &state->arcs_lsize[type];
1479 ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1480 ASSERT(state != arc_anon);
1482 ASSERT3U(*cnt, >=, size);
1483 atomic_add_64(cnt, -size);
1485 ASSERT3U(state->arcs_size, >=, size);
1486 atomic_add_64(&state->arcs_size, -size);
1490 * If we're destroying a duplicate buffer make sure
1491 * that the appropriate statistics are updated.
1493 if (buf->b_hdr->b_datacnt > 1 &&
1494 buf->b_hdr->b_type == ARC_BUFC_DATA) {
1495 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
1496 ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size);
1498 ASSERT(buf->b_hdr->b_datacnt > 0);
1499 buf->b_hdr->b_datacnt -= 1;
1502 /* only remove the buf if requested */
1506 /* remove the buf from the hdr list */
1507 for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1509 *bufp = buf->b_next;
1512 ASSERT(buf->b_efunc == NULL);
1514 /* clean up the buf */
1516 kmem_cache_free(buf_cache, buf);
1520 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1522 l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1524 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1525 ASSERT3P(hdr->b_state, ==, arc_anon);
1526 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1528 if (l2hdr != NULL) {
1529 boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1531 * To prevent arc_free() and l2arc_evict() from
1532 * attempting to free the same buffer at the same time,
1533 * a FREE_IN_PROGRESS flag is given to arc_free() to
1534 * give it priority. l2arc_evict() can't destroy this
1535 * header while we are waiting on l2arc_buflist_mtx.
1537 * The hdr may be removed from l2ad_buflist before we
1538 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1540 if (!buflist_held) {
1541 mutex_enter(&l2arc_buflist_mtx);
1542 l2hdr = hdr->b_l2hdr;
1545 if (l2hdr != NULL) {
1546 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1547 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1548 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1549 if (hdr->b_state == arc_l2c_only)
1550 l2arc_hdr_stat_remove();
1551 hdr->b_l2hdr = NULL;
1555 mutex_exit(&l2arc_buflist_mtx);
1558 if (!BUF_EMPTY(hdr)) {
1559 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1560 buf_discard_identity(hdr);
1562 while (hdr->b_buf) {
1563 arc_buf_t *buf = hdr->b_buf;
1566 mutex_enter(&arc_eviction_mtx);
1567 mutex_enter(&buf->b_evict_lock);
1568 ASSERT(buf->b_hdr != NULL);
1569 arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1570 hdr->b_buf = buf->b_next;
1571 buf->b_hdr = &arc_eviction_hdr;
1572 buf->b_next = arc_eviction_list;
1573 arc_eviction_list = buf;
1574 mutex_exit(&buf->b_evict_lock);
1575 mutex_exit(&arc_eviction_mtx);
1577 arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1580 if (hdr->b_freeze_cksum != NULL) {
1581 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1582 hdr->b_freeze_cksum = NULL;
1584 if (hdr->b_thawed) {
1585 kmem_free(hdr->b_thawed, 1);
1586 hdr->b_thawed = NULL;
1589 ASSERT(!list_link_active(&hdr->b_arc_node));
1590 ASSERT3P(hdr->b_hash_next, ==, NULL);
1591 ASSERT3P(hdr->b_acb, ==, NULL);
1592 kmem_cache_free(hdr_cache, hdr);
1596 arc_buf_free(arc_buf_t *buf, void *tag)
1598 arc_buf_hdr_t *hdr = buf->b_hdr;
1599 int hashed = hdr->b_state != arc_anon;
1601 ASSERT(buf->b_efunc == NULL);
1602 ASSERT(buf->b_data != NULL);
1605 kmutex_t *hash_lock = HDR_LOCK(hdr);
1607 mutex_enter(hash_lock);
1609 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1611 (void) remove_reference(hdr, hash_lock, tag);
1612 if (hdr->b_datacnt > 1) {
1613 arc_buf_destroy(buf, FALSE, TRUE);
1615 ASSERT(buf == hdr->b_buf);
1616 ASSERT(buf->b_efunc == NULL);
1617 hdr->b_flags |= ARC_BUF_AVAILABLE;
1619 mutex_exit(hash_lock);
1620 } else if (HDR_IO_IN_PROGRESS(hdr)) {
1623 * We are in the middle of an async write. Don't destroy
1624 * this buffer unless the write completes before we finish
1625 * decrementing the reference count.
1627 mutex_enter(&arc_eviction_mtx);
1628 (void) remove_reference(hdr, NULL, tag);
1629 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1630 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1631 mutex_exit(&arc_eviction_mtx);
1633 arc_hdr_destroy(hdr);
1635 if (remove_reference(hdr, NULL, tag) > 0)
1636 arc_buf_destroy(buf, FALSE, TRUE);
1638 arc_hdr_destroy(hdr);
1643 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1645 arc_buf_hdr_t *hdr = buf->b_hdr;
1646 kmutex_t *hash_lock = HDR_LOCK(hdr);
1647 int no_callback = (buf->b_efunc == NULL);
1649 if (hdr->b_state == arc_anon) {
1650 ASSERT(hdr->b_datacnt == 1);
1651 arc_buf_free(buf, tag);
1652 return (no_callback);
1655 mutex_enter(hash_lock);
1657 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1658 ASSERT(hdr->b_state != arc_anon);
1659 ASSERT(buf->b_data != NULL);
1661 (void) remove_reference(hdr, hash_lock, tag);
1662 if (hdr->b_datacnt > 1) {
1664 arc_buf_destroy(buf, FALSE, TRUE);
1665 } else if (no_callback) {
1666 ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1667 ASSERT(buf->b_efunc == NULL);
1668 hdr->b_flags |= ARC_BUF_AVAILABLE;
1670 ASSERT(no_callback || hdr->b_datacnt > 1 ||
1671 refcount_is_zero(&hdr->b_refcnt));
1672 mutex_exit(hash_lock);
1673 return (no_callback);
1677 arc_buf_size(arc_buf_t *buf)
1679 return (buf->b_hdr->b_size);
1683 * Called from the DMU to determine if the current buffer should be
1684 * evicted. In order to ensure proper locking, the eviction must be initiated
1685 * from the DMU. Return true if the buffer is associated with user data and
1686 * duplicate buffers still exist.
1689 arc_buf_eviction_needed(arc_buf_t *buf)
1692 boolean_t evict_needed = B_FALSE;
1694 if (zfs_disable_dup_eviction)
1697 mutex_enter(&buf->b_evict_lock);
1701 * We are in arc_do_user_evicts(); let that function
1702 * perform the eviction.
1704 ASSERT(buf->b_data == NULL);
1705 mutex_exit(&buf->b_evict_lock);
1707 } else if (buf->b_data == NULL) {
1709 * We have already been added to the arc eviction list;
1710 * recommend eviction.
1712 ASSERT3P(hdr, ==, &arc_eviction_hdr);
1713 mutex_exit(&buf->b_evict_lock);
1717 if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA)
1718 evict_needed = B_TRUE;
1720 mutex_exit(&buf->b_evict_lock);
1721 return (evict_needed);
1725 * Evict buffers from list until we've removed the specified number of
1726 * bytes. Move the removed buffers to the appropriate evict state.
1727 * If the recycle flag is set, then attempt to "recycle" a buffer:
1728 * - look for a buffer to evict that is `bytes' long.
1729 * - return the data block from this buffer rather than freeing it.
1730 * This flag is used by callers that are trying to make space for a
1731 * new buffer in a full arc cache.
1733 * This function makes a "best effort". It skips over any buffers
1734 * it can't get a hash_lock on, and so may not catch all candidates.
1735 * It may also return without evicting as much space as requested.
1738 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1739 arc_buf_contents_t type)
1741 arc_state_t *evicted_state;
1742 uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1743 arc_buf_hdr_t *ab, *ab_prev = NULL;
1744 list_t *list = &state->arcs_list[type];
1745 kmutex_t *hash_lock;
1746 boolean_t have_lock;
1747 void *stolen = NULL;
1749 ASSERT(state == arc_mru || state == arc_mfu);
1751 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1753 mutex_enter(&state->arcs_mtx);
1754 mutex_enter(&evicted_state->arcs_mtx);
1756 for (ab = list_tail(list); ab; ab = ab_prev) {
1757 ab_prev = list_prev(list, ab);
1758 /* prefetch buffers have a minimum lifespan */
1759 if (HDR_IO_IN_PROGRESS(ab) ||
1760 (spa && ab->b_spa != spa) ||
1761 (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1762 ddi_get_lbolt() - ab->b_arc_access <
1763 arc_min_prefetch_lifespan)) {
1767 /* "lookahead" for better eviction candidate */
1768 if (recycle && ab->b_size != bytes &&
1769 ab_prev && ab_prev->b_size == bytes)
1771 hash_lock = HDR_LOCK(ab);
1772 have_lock = MUTEX_HELD(hash_lock);
1773 if (have_lock || mutex_tryenter(hash_lock)) {
1774 ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0);
1775 ASSERT(ab->b_datacnt > 0);
1777 arc_buf_t *buf = ab->b_buf;
1778 if (!mutex_tryenter(&buf->b_evict_lock)) {
1783 bytes_evicted += ab->b_size;
1784 if (recycle && ab->b_type == type &&
1785 ab->b_size == bytes &&
1786 !HDR_L2_WRITING(ab)) {
1787 stolen = buf->b_data;
1792 mutex_enter(&arc_eviction_mtx);
1793 arc_buf_destroy(buf,
1794 buf->b_data == stolen, FALSE);
1795 ab->b_buf = buf->b_next;
1796 buf->b_hdr = &arc_eviction_hdr;
1797 buf->b_next = arc_eviction_list;
1798 arc_eviction_list = buf;
1799 mutex_exit(&arc_eviction_mtx);
1800 mutex_exit(&buf->b_evict_lock);
1802 mutex_exit(&buf->b_evict_lock);
1803 arc_buf_destroy(buf,
1804 buf->b_data == stolen, TRUE);
1809 ARCSTAT_INCR(arcstat_evict_l2_cached,
1812 if (l2arc_write_eligible(ab->b_spa, ab)) {
1813 ARCSTAT_INCR(arcstat_evict_l2_eligible,
1817 arcstat_evict_l2_ineligible,
1822 if (ab->b_datacnt == 0) {
1823 arc_change_state(evicted_state, ab, hash_lock);
1824 ASSERT(HDR_IN_HASH_TABLE(ab));
1825 ab->b_flags |= ARC_IN_HASH_TABLE;
1826 ab->b_flags &= ~ARC_BUF_AVAILABLE;
1827 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1830 mutex_exit(hash_lock);
1831 if (bytes >= 0 && bytes_evicted >= bytes)
1838 mutex_exit(&evicted_state->arcs_mtx);
1839 mutex_exit(&state->arcs_mtx);
1841 if (bytes_evicted < bytes)
1842 dprintf("only evicted %lld bytes from %x\n",
1843 (longlong_t)bytes_evicted, state);
1846 ARCSTAT_INCR(arcstat_evict_skip, skipped);
1849 ARCSTAT_INCR(arcstat_mutex_miss, missed);
1852 * We have just evicted some date into the ghost state, make
1853 * sure we also adjust the ghost state size if necessary.
1856 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
1857 int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
1858 arc_mru_ghost->arcs_size - arc_c;
1860 if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
1862 MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
1863 arc_evict_ghost(arc_mru_ghost, 0, todelete);
1864 } else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
1865 int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
1866 arc_mru_ghost->arcs_size +
1867 arc_mfu_ghost->arcs_size - arc_c);
1868 arc_evict_ghost(arc_mfu_ghost, 0, todelete);
1876 * Remove buffers from list until we've removed the specified number of
1877 * bytes. Destroy the buffers that are removed.
1880 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
1882 arc_buf_hdr_t *ab, *ab_prev;
1883 arc_buf_hdr_t marker;
1884 list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1885 kmutex_t *hash_lock;
1886 uint64_t bytes_deleted = 0;
1887 uint64_t bufs_skipped = 0;
1889 ASSERT(GHOST_STATE(state));
1890 bzero(&marker, sizeof(marker));
1892 mutex_enter(&state->arcs_mtx);
1893 for (ab = list_tail(list); ab; ab = ab_prev) {
1894 ab_prev = list_prev(list, ab);
1895 if (spa && ab->b_spa != spa)
1898 /* ignore markers */
1902 hash_lock = HDR_LOCK(ab);
1903 /* caller may be trying to modify this buffer, skip it */
1904 if (MUTEX_HELD(hash_lock))
1906 if (mutex_tryenter(hash_lock)) {
1907 ASSERT(!HDR_IO_IN_PROGRESS(ab));
1908 ASSERT(ab->b_buf == NULL);
1909 ARCSTAT_BUMP(arcstat_deleted);
1910 bytes_deleted += ab->b_size;
1912 if (ab->b_l2hdr != NULL) {
1914 * This buffer is cached on the 2nd Level ARC;
1915 * don't destroy the header.
1917 arc_change_state(arc_l2c_only, ab, hash_lock);
1918 mutex_exit(hash_lock);
1920 arc_change_state(arc_anon, ab, hash_lock);
1921 mutex_exit(hash_lock);
1922 arc_hdr_destroy(ab);
1925 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1926 if (bytes >= 0 && bytes_deleted >= bytes)
1928 } else if (bytes < 0) {
1930 * Insert a list marker and then wait for the
1931 * hash lock to become available. Once its
1932 * available, restart from where we left off.
1934 list_insert_after(list, ab, &marker);
1935 mutex_exit(&state->arcs_mtx);
1936 mutex_enter(hash_lock);
1937 mutex_exit(hash_lock);
1938 mutex_enter(&state->arcs_mtx);
1939 ab_prev = list_prev(list, &marker);
1940 list_remove(list, &marker);
1944 mutex_exit(&state->arcs_mtx);
1946 if (list == &state->arcs_list[ARC_BUFC_DATA] &&
1947 (bytes < 0 || bytes_deleted < bytes)) {
1948 list = &state->arcs_list[ARC_BUFC_METADATA];
1953 ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
1957 if (bytes_deleted < bytes)
1958 dprintf("only deleted %lld bytes from %p\n",
1959 (longlong_t)bytes_deleted, state);
1965 int64_t adjustment, delta;
1971 adjustment = MIN((int64_t)(arc_size - arc_c),
1972 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
1975 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
1976 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
1977 (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA);
1978 adjustment -= delta;
1981 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1982 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
1983 (void) arc_evict(arc_mru, 0, delta, FALSE,
1991 adjustment = arc_size - arc_c;
1993 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
1994 delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
1995 (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA);
1996 adjustment -= delta;
1999 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2000 int64_t delta = MIN(adjustment,
2001 arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
2002 (void) arc_evict(arc_mfu, 0, delta, FALSE,
2007 * Adjust ghost lists
2010 adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
2012 if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
2013 delta = MIN(arc_mru_ghost->arcs_size, adjustment);
2014 arc_evict_ghost(arc_mru_ghost, 0, delta);
2018 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
2020 if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
2021 delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
2022 arc_evict_ghost(arc_mfu_ghost, 0, delta);
2027 * Request that arc user drop references so that N bytes can be released
2028 * from the cache. This provides a mechanism to ensure the arc can honor
2029 * the arc_meta_limit and reclaim buffers which are pinned in the cache
2030 * by higher layers. (i.e. the zpl)
2033 arc_do_user_prune(int64_t adjustment)
2035 arc_prune_func_t *func;
2037 arc_prune_t *cp, *np;
2039 mutex_enter(&arc_prune_mtx);
2041 cp = list_head(&arc_prune_list);
2042 while (cp != NULL) {
2044 private = cp->p_private;
2045 np = list_next(&arc_prune_list, cp);
2046 refcount_add(&cp->p_refcnt, func);
2047 mutex_exit(&arc_prune_mtx);
2050 func(adjustment, private);
2052 mutex_enter(&arc_prune_mtx);
2054 /* User removed prune callback concurrently with execution */
2055 if (refcount_remove(&cp->p_refcnt, func) == 0) {
2056 ASSERT(!list_link_active(&cp->p_node));
2057 refcount_destroy(&cp->p_refcnt);
2058 kmem_free(cp, sizeof (*cp));
2064 ARCSTAT_BUMP(arcstat_prune);
2065 mutex_exit(&arc_prune_mtx);
2069 arc_do_user_evicts(void)
2071 mutex_enter(&arc_eviction_mtx);
2072 while (arc_eviction_list != NULL) {
2073 arc_buf_t *buf = arc_eviction_list;
2074 arc_eviction_list = buf->b_next;
2075 mutex_enter(&buf->b_evict_lock);
2077 mutex_exit(&buf->b_evict_lock);
2078 mutex_exit(&arc_eviction_mtx);
2080 if (buf->b_efunc != NULL)
2081 VERIFY(buf->b_efunc(buf) == 0);
2083 buf->b_efunc = NULL;
2084 buf->b_private = NULL;
2085 kmem_cache_free(buf_cache, buf);
2086 mutex_enter(&arc_eviction_mtx);
2088 mutex_exit(&arc_eviction_mtx);
2092 * Evict only meta data objects from the cache leaving the data objects.
2093 * This is only used to enforce the tunable arc_meta_limit, if we are
2094 * unable to evict enough buffers notify the user via the prune callback.
2097 arc_adjust_meta(int64_t adjustment, boolean_t may_prune)
2101 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2102 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2103 arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_METADATA);
2104 adjustment -= delta;
2107 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2108 delta = MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2109 arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_METADATA);
2110 adjustment -= delta;
2113 if (may_prune && (adjustment > 0) && (arc_meta_used > arc_meta_limit))
2114 arc_do_user_prune(arc_meta_prune);
2118 * Flush all *evictable* data from the cache for the given spa.
2119 * NOTE: this will not touch "active" (i.e. referenced) data.
2122 arc_flush(spa_t *spa)
2127 guid = spa_load_guid(spa);
2129 while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
2130 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
2134 while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
2135 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
2139 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
2140 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
2144 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
2145 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
2150 arc_evict_ghost(arc_mru_ghost, guid, -1);
2151 arc_evict_ghost(arc_mfu_ghost, guid, -1);
2153 mutex_enter(&arc_reclaim_thr_lock);
2154 arc_do_user_evicts();
2155 mutex_exit(&arc_reclaim_thr_lock);
2156 ASSERT(spa || arc_eviction_list == NULL);
2160 arc_shrink(uint64_t bytes)
2162 if (arc_c > arc_c_min) {
2165 to_free = bytes ? bytes : arc_c >> arc_shrink_shift;
2167 if (arc_c > arc_c_min + to_free)
2168 atomic_add_64(&arc_c, -to_free);
2172 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
2173 if (arc_c > arc_size)
2174 arc_c = MAX(arc_size, arc_c_min);
2176 arc_p = (arc_c >> 1);
2177 ASSERT(arc_c >= arc_c_min);
2178 ASSERT((int64_t)arc_p >= 0);
2181 if (arc_size > arc_c)
2186 arc_kmem_reap_now(arc_reclaim_strategy_t strat, uint64_t bytes)
2189 kmem_cache_t *prev_cache = NULL;
2190 kmem_cache_t *prev_data_cache = NULL;
2191 extern kmem_cache_t *zio_buf_cache[];
2192 extern kmem_cache_t *zio_data_buf_cache[];
2195 * An aggressive reclamation will shrink the cache size as well as
2196 * reap free buffers from the arc kmem caches.
2198 if (strat == ARC_RECLAIM_AGGR)
2201 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2202 if (zio_buf_cache[i] != prev_cache) {
2203 prev_cache = zio_buf_cache[i];
2204 kmem_cache_reap_now(zio_buf_cache[i]);
2206 if (zio_data_buf_cache[i] != prev_data_cache) {
2207 prev_data_cache = zio_data_buf_cache[i];
2208 kmem_cache_reap_now(zio_data_buf_cache[i]);
2212 kmem_cache_reap_now(buf_cache);
2213 kmem_cache_reap_now(hdr_cache);
2217 * Unlike other ZFS implementations this thread is only responsible for
2218 * adapting the target ARC size on Linux. The responsibility for memory
2219 * reclamation has been entirely delegated to the arc_shrinker_func()
2220 * which is registered with the VM. To reflect this change in behavior
2221 * the arc_reclaim thread has been renamed to arc_adapt.
2224 arc_adapt_thread(void)
2229 CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2231 mutex_enter(&arc_reclaim_thr_lock);
2232 while (arc_thread_exit == 0) {
2234 arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS;
2236 if (spa_get_random(100) == 0) {
2239 if (last_reclaim == ARC_RECLAIM_CONS) {
2240 last_reclaim = ARC_RECLAIM_AGGR;
2242 last_reclaim = ARC_RECLAIM_CONS;
2246 last_reclaim = ARC_RECLAIM_AGGR;
2250 /* reset the growth delay for every reclaim */
2251 arc_grow_time = ddi_get_lbolt()+(arc_grow_retry * hz);
2253 arc_kmem_reap_now(last_reclaim, 0);
2256 #endif /* !_KERNEL */
2258 /* No recent memory pressure allow the ARC to grow. */
2259 if (arc_no_grow && ddi_get_lbolt() >= arc_grow_time)
2260 arc_no_grow = FALSE;
2263 * Keep meta data usage within limits, arc_shrink() is not
2264 * used to avoid collapsing the arc_c value when only the
2265 * arc_meta_limit is being exceeded.
2267 prune = (int64_t)arc_meta_used - (int64_t)arc_meta_limit;
2269 arc_adjust_meta(prune, B_TRUE);
2273 if (arc_eviction_list != NULL)
2274 arc_do_user_evicts();
2276 /* block until needed, or one second, whichever is shorter */
2277 CALLB_CPR_SAFE_BEGIN(&cpr);
2278 (void) cv_timedwait_interruptible(&arc_reclaim_thr_cv,
2279 &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2280 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2283 arc_thread_exit = 0;
2284 cv_broadcast(&arc_reclaim_thr_cv);
2285 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */
2291 * Determine the amount of memory eligible for eviction contained in the
2292 * ARC. All clean data reported by the ghost lists can always be safely
2293 * evicted. Due to arc_c_min, the same does not hold for all clean data
2294 * contained by the regular mru and mfu lists.
2296 * In the case of the regular mru and mfu lists, we need to report as
2297 * much clean data as possible, such that evicting that same reported
2298 * data will not bring arc_size below arc_c_min. Thus, in certain
2299 * circumstances, the total amount of clean data in the mru and mfu
2300 * lists might not actually be evictable.
2302 * The following two distinct cases are accounted for:
2304 * 1. The sum of the amount of dirty data contained by both the mru and
2305 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2306 * is greater than or equal to arc_c_min.
2307 * (i.e. amount of dirty data >= arc_c_min)
2309 * This is the easy case; all clean data contained by the mru and mfu
2310 * lists is evictable. Evicting all clean data can only drop arc_size
2311 * to the amount of dirty data, which is greater than arc_c_min.
2313 * 2. The sum of the amount of dirty data contained by both the mru and
2314 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2315 * is less than arc_c_min.
2316 * (i.e. arc_c_min > amount of dirty data)
2318 * 2.1. arc_size is greater than or equal arc_c_min.
2319 * (i.e. arc_size >= arc_c_min > amount of dirty data)
2321 * In this case, not all clean data from the regular mru and mfu
2322 * lists is actually evictable; we must leave enough clean data
2323 * to keep arc_size above arc_c_min. Thus, the maximum amount of
2324 * evictable data from the two lists combined, is exactly the
2325 * difference between arc_size and arc_c_min.
2327 * 2.2. arc_size is less than arc_c_min
2328 * (i.e. arc_c_min > arc_size > amount of dirty data)
2330 * In this case, none of the data contained in the mru and mfu
2331 * lists is evictable, even if it's clean. Since arc_size is
2332 * already below arc_c_min, evicting any more would only
2333 * increase this negative difference.
2336 arc_evictable_memory(void) {
2337 uint64_t arc_clean =
2338 arc_mru->arcs_lsize[ARC_BUFC_DATA] +
2339 arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
2340 arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
2341 arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
2342 uint64_t ghost_clean =
2343 arc_mru_ghost->arcs_lsize[ARC_BUFC_DATA] +
2344 arc_mru_ghost->arcs_lsize[ARC_BUFC_METADATA] +
2345 arc_mfu_ghost->arcs_lsize[ARC_BUFC_DATA] +
2346 arc_mfu_ghost->arcs_lsize[ARC_BUFC_METADATA];
2347 uint64_t arc_dirty = MAX((int64_t)arc_size - (int64_t)arc_clean, 0);
2349 if (arc_dirty >= arc_c_min)
2350 return (ghost_clean + arc_clean);
2352 return (ghost_clean + MAX((int64_t)arc_size - (int64_t)arc_c_min, 0));
2356 __arc_shrinker_func(struct shrinker *shrink, struct shrink_control *sc)
2360 /* The arc is considered warm once reclaim has occurred */
2361 if (unlikely(arc_warm == B_FALSE))
2364 /* Return the potential number of reclaimable pages */
2365 pages = btop(arc_evictable_memory());
2366 if (sc->nr_to_scan == 0)
2369 /* Not allowed to perform filesystem reclaim */
2370 if (!(sc->gfp_mask & __GFP_FS))
2373 /* Reclaim in progress */
2374 if (mutex_tryenter(&arc_reclaim_thr_lock) == 0)
2378 * Evict the requested number of pages by shrinking arc_c the
2379 * requested amount. If there is nothing left to evict just
2380 * reap whatever we can from the various arc slabs.
2383 arc_kmem_reap_now(ARC_RECLAIM_AGGR, ptob(sc->nr_to_scan));
2384 pages = btop(arc_evictable_memory());
2386 arc_kmem_reap_now(ARC_RECLAIM_CONS, ptob(sc->nr_to_scan));
2391 * When direct reclaim is observed it usually indicates a rapid
2392 * increase in memory pressure. This occurs because the kswapd
2393 * threads were unable to asynchronously keep enough free memory
2394 * available. In this case set arc_no_grow to briefly pause arc
2395 * growth to avoid compounding the memory pressure.
2397 if (current_is_kswapd()) {
2398 ARCSTAT_BUMP(arcstat_memory_indirect_count);
2400 arc_no_grow = B_TRUE;
2401 arc_grow_time = ddi_get_lbolt() + (arc_grow_retry * hz);
2402 ARCSTAT_BUMP(arcstat_memory_direct_count);
2405 mutex_exit(&arc_reclaim_thr_lock);
2409 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func);
2411 SPL_SHRINKER_DECLARE(arc_shrinker, arc_shrinker_func, DEFAULT_SEEKS);
2412 #endif /* _KERNEL */
2415 * Adapt arc info given the number of bytes we are trying to add and
2416 * the state that we are comming from. This function is only called
2417 * when we are adding new content to the cache.
2420 arc_adapt(int bytes, arc_state_t *state)
2423 uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
2425 if (state == arc_l2c_only)
2430 * Adapt the target size of the MRU list:
2431 * - if we just hit in the MRU ghost list, then increase
2432 * the target size of the MRU list.
2433 * - if we just hit in the MFU ghost list, then increase
2434 * the target size of the MFU list by decreasing the
2435 * target size of the MRU list.
2437 if (state == arc_mru_ghost) {
2438 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2439 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2440 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2442 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2443 } else if (state == arc_mfu_ghost) {
2446 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2447 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2448 mult = MIN(mult, 10);
2450 delta = MIN(bytes * mult, arc_p);
2451 arc_p = MAX(arc_p_min, arc_p - delta);
2453 ASSERT((int64_t)arc_p >= 0);
2458 if (arc_c >= arc_c_max)
2462 * If we're within (2 * maxblocksize) bytes of the target
2463 * cache size, increment the target cache size
2465 if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2466 atomic_add_64(&arc_c, (int64_t)bytes);
2467 if (arc_c > arc_c_max)
2469 else if (state == arc_anon)
2470 atomic_add_64(&arc_p, (int64_t)bytes);
2474 ASSERT((int64_t)arc_p >= 0);
2478 * Check if the cache has reached its limits and eviction is required
2482 arc_evict_needed(arc_buf_contents_t type)
2484 if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2490 return (arc_size > arc_c);
2494 * The buffer, supplied as the first argument, needs a data block.
2495 * So, if we are at cache max, determine which cache should be victimized.
2496 * We have the following cases:
2498 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2499 * In this situation if we're out of space, but the resident size of the MFU is
2500 * under the limit, victimize the MFU cache to satisfy this insertion request.
2502 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2503 * Here, we've used up all of the available space for the MRU, so we need to
2504 * evict from our own cache instead. Evict from the set of resident MRU
2507 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2508 * c minus p represents the MFU space in the cache, since p is the size of the
2509 * cache that is dedicated to the MRU. In this situation there's still space on
2510 * the MFU side, so the MRU side needs to be victimized.
2512 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2513 * MFU's resident set is consuming more space than it has been allotted. In
2514 * this situation, we must victimize our own cache, the MFU, for this insertion.
2517 arc_get_data_buf(arc_buf_t *buf)
2519 arc_state_t *state = buf->b_hdr->b_state;
2520 uint64_t size = buf->b_hdr->b_size;
2521 arc_buf_contents_t type = buf->b_hdr->b_type;
2523 arc_adapt(size, state);
2526 * We have not yet reached cache maximum size,
2527 * just allocate a new buffer.
2529 if (!arc_evict_needed(type)) {
2530 if (type == ARC_BUFC_METADATA) {
2531 buf->b_data = zio_buf_alloc(size);
2532 arc_space_consume(size, ARC_SPACE_DATA);
2534 ASSERT(type == ARC_BUFC_DATA);
2535 buf->b_data = zio_data_buf_alloc(size);
2536 ARCSTAT_INCR(arcstat_data_size, size);
2537 atomic_add_64(&arc_size, size);
2543 * If we are prefetching from the mfu ghost list, this buffer
2544 * will end up on the mru list; so steal space from there.
2546 if (state == arc_mfu_ghost)
2547 state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2548 else if (state == arc_mru_ghost)
2551 if (state == arc_mru || state == arc_anon) {
2552 uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2553 state = (arc_mfu->arcs_lsize[type] >= size &&
2554 arc_p > mru_used) ? arc_mfu : arc_mru;
2557 uint64_t mfu_space = arc_c - arc_p;
2558 state = (arc_mru->arcs_lsize[type] >= size &&
2559 mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2562 if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) {
2563 if (type == ARC_BUFC_METADATA) {
2564 buf->b_data = zio_buf_alloc(size);
2565 arc_space_consume(size, ARC_SPACE_DATA);
2568 * If we are unable to recycle an existing meta buffer
2569 * signal the reclaim thread. It will notify users
2570 * via the prune callback to drop references. The
2571 * prune callback in run in the context of the reclaim
2572 * thread to avoid deadlocking on the hash_lock.
2574 cv_signal(&arc_reclaim_thr_cv);
2576 ASSERT(type == ARC_BUFC_DATA);
2577 buf->b_data = zio_data_buf_alloc(size);
2578 ARCSTAT_INCR(arcstat_data_size, size);
2579 atomic_add_64(&arc_size, size);
2582 ARCSTAT_BUMP(arcstat_recycle_miss);
2584 ASSERT(buf->b_data != NULL);
2587 * Update the state size. Note that ghost states have a
2588 * "ghost size" and so don't need to be updated.
2590 if (!GHOST_STATE(buf->b_hdr->b_state)) {
2591 arc_buf_hdr_t *hdr = buf->b_hdr;
2593 atomic_add_64(&hdr->b_state->arcs_size, size);
2594 if (list_link_active(&hdr->b_arc_node)) {
2595 ASSERT(refcount_is_zero(&hdr->b_refcnt));
2596 atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2599 * If we are growing the cache, and we are adding anonymous
2600 * data, and we have outgrown arc_p, update arc_p
2602 if (arc_size < arc_c && hdr->b_state == arc_anon &&
2603 arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2604 arc_p = MIN(arc_c, arc_p + size);
2609 * This routine is called whenever a buffer is accessed.
2610 * NOTE: the hash lock is dropped in this function.
2613 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2617 ASSERT(MUTEX_HELD(hash_lock));
2619 if (buf->b_state == arc_anon) {
2621 * This buffer is not in the cache, and does not
2622 * appear in our "ghost" list. Add the new buffer
2626 ASSERT(buf->b_arc_access == 0);
2627 buf->b_arc_access = ddi_get_lbolt();
2628 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2629 arc_change_state(arc_mru, buf, hash_lock);
2631 } else if (buf->b_state == arc_mru) {
2632 now = ddi_get_lbolt();
2635 * If this buffer is here because of a prefetch, then either:
2636 * - clear the flag if this is a "referencing" read
2637 * (any subsequent access will bump this into the MFU state).
2639 * - move the buffer to the head of the list if this is
2640 * another prefetch (to make it less likely to be evicted).
2642 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2643 if (refcount_count(&buf->b_refcnt) == 0) {
2644 ASSERT(list_link_active(&buf->b_arc_node));
2646 buf->b_flags &= ~ARC_PREFETCH;
2647 ARCSTAT_BUMP(arcstat_mru_hits);
2649 buf->b_arc_access = now;
2654 * This buffer has been "accessed" only once so far,
2655 * but it is still in the cache. Move it to the MFU
2658 if (now > buf->b_arc_access + ARC_MINTIME) {
2660 * More than 125ms have passed since we
2661 * instantiated this buffer. Move it to the
2662 * most frequently used state.
2664 buf->b_arc_access = now;
2665 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2666 arc_change_state(arc_mfu, buf, hash_lock);
2668 ARCSTAT_BUMP(arcstat_mru_hits);
2669 } else if (buf->b_state == arc_mru_ghost) {
2670 arc_state_t *new_state;
2672 * This buffer has been "accessed" recently, but
2673 * was evicted from the cache. Move it to the
2677 if (buf->b_flags & ARC_PREFETCH) {
2678 new_state = arc_mru;
2679 if (refcount_count(&buf->b_refcnt) > 0)
2680 buf->b_flags &= ~ARC_PREFETCH;
2681 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2683 new_state = arc_mfu;
2684 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2687 buf->b_arc_access = ddi_get_lbolt();
2688 arc_change_state(new_state, buf, hash_lock);
2690 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2691 } else if (buf->b_state == arc_mfu) {
2693 * This buffer has been accessed more than once and is
2694 * still in the cache. Keep it in the MFU state.
2696 * NOTE: an add_reference() that occurred when we did
2697 * the arc_read() will have kicked this off the list.
2698 * If it was a prefetch, we will explicitly move it to
2699 * the head of the list now.
2701 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2702 ASSERT(refcount_count(&buf->b_refcnt) == 0);
2703 ASSERT(list_link_active(&buf->b_arc_node));
2705 ARCSTAT_BUMP(arcstat_mfu_hits);
2706 buf->b_arc_access = ddi_get_lbolt();
2707 } else if (buf->b_state == arc_mfu_ghost) {
2708 arc_state_t *new_state = arc_mfu;
2710 * This buffer has been accessed more than once but has
2711 * been evicted from the cache. Move it back to the
2715 if (buf->b_flags & ARC_PREFETCH) {
2717 * This is a prefetch access...
2718 * move this block back to the MRU state.
2720 ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0);
2721 new_state = arc_mru;
2724 buf->b_arc_access = ddi_get_lbolt();
2725 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2726 arc_change_state(new_state, buf, hash_lock);
2728 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2729 } else if (buf->b_state == arc_l2c_only) {
2731 * This buffer is on the 2nd Level ARC.
2734 buf->b_arc_access = ddi_get_lbolt();
2735 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2736 arc_change_state(arc_mfu, buf, hash_lock);
2738 ASSERT(!"invalid arc state");
2742 /* a generic arc_done_func_t which you can use */
2745 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2747 if (zio == NULL || zio->io_error == 0)
2748 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2749 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2752 /* a generic arc_done_func_t */
2754 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2756 arc_buf_t **bufp = arg;
2757 if (zio && zio->io_error) {
2758 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2762 ASSERT(buf->b_data);
2767 arc_read_done(zio_t *zio)
2769 arc_buf_hdr_t *hdr, *found;
2771 arc_buf_t *abuf; /* buffer we're assigning to callback */
2772 kmutex_t *hash_lock;
2773 arc_callback_t *callback_list, *acb;
2774 int freeable = FALSE;
2776 buf = zio->io_private;
2780 * The hdr was inserted into hash-table and removed from lists
2781 * prior to starting I/O. We should find this header, since
2782 * it's in the hash table, and it should be legit since it's
2783 * not possible to evict it during the I/O. The only possible
2784 * reason for it not to be found is if we were freed during the
2787 found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
2790 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
2791 (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2792 (found == hdr && HDR_L2_READING(hdr)));
2794 hdr->b_flags &= ~ARC_L2_EVICTED;
2795 if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2796 hdr->b_flags &= ~ARC_L2CACHE;
2798 /* byteswap if necessary */
2799 callback_list = hdr->b_acb;
2800 ASSERT(callback_list != NULL);
2801 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2802 dmu_object_byteswap_t bswap =
2803 DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
2804 arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
2805 byteswap_uint64_array :
2806 dmu_ot_byteswap[bswap].ob_func;
2807 func(buf->b_data, hdr->b_size);
2810 arc_cksum_compute(buf, B_FALSE);
2812 if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
2814 * Only call arc_access on anonymous buffers. This is because
2815 * if we've issued an I/O for an evicted buffer, we've already
2816 * called arc_access (to prevent any simultaneous readers from
2817 * getting confused).
2819 arc_access(hdr, hash_lock);
2822 /* create copies of the data buffer for the callers */
2824 for (acb = callback_list; acb; acb = acb->acb_next) {
2825 if (acb->acb_done) {
2827 ARCSTAT_BUMP(arcstat_duplicate_reads);
2828 abuf = arc_buf_clone(buf);
2830 acb->acb_buf = abuf;
2835 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2836 ASSERT(!HDR_BUF_AVAILABLE(hdr));
2838 ASSERT(buf->b_efunc == NULL);
2839 ASSERT(hdr->b_datacnt == 1);
2840 hdr->b_flags |= ARC_BUF_AVAILABLE;
2843 ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2845 if (zio->io_error != 0) {
2846 hdr->b_flags |= ARC_IO_ERROR;
2847 if (hdr->b_state != arc_anon)
2848 arc_change_state(arc_anon, hdr, hash_lock);
2849 if (HDR_IN_HASH_TABLE(hdr))
2850 buf_hash_remove(hdr);
2851 freeable = refcount_is_zero(&hdr->b_refcnt);
2855 * Broadcast before we drop the hash_lock to avoid the possibility
2856 * that the hdr (and hence the cv) might be freed before we get to
2857 * the cv_broadcast().
2859 cv_broadcast(&hdr->b_cv);
2862 mutex_exit(hash_lock);
2865 * This block was freed while we waited for the read to
2866 * complete. It has been removed from the hash table and
2867 * moved to the anonymous state (so that it won't show up
2870 ASSERT3P(hdr->b_state, ==, arc_anon);
2871 freeable = refcount_is_zero(&hdr->b_refcnt);
2874 /* execute each callback and free its structure */
2875 while ((acb = callback_list) != NULL) {
2877 acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2879 if (acb->acb_zio_dummy != NULL) {
2880 acb->acb_zio_dummy->io_error = zio->io_error;
2881 zio_nowait(acb->acb_zio_dummy);
2884 callback_list = acb->acb_next;
2885 kmem_free(acb, sizeof (arc_callback_t));
2889 arc_hdr_destroy(hdr);
2893 * "Read" the block at the specified DVA (in bp) via the
2894 * cache. If the block is found in the cache, invoke the provided
2895 * callback immediately and return. Note that the `zio' parameter
2896 * in the callback will be NULL in this case, since no IO was
2897 * required. If the block is not in the cache pass the read request
2898 * on to the spa with a substitute callback function, so that the
2899 * requested block will be added to the cache.
2901 * If a read request arrives for a block that has a read in-progress,
2902 * either wait for the in-progress read to complete (and return the
2903 * results); or, if this is a read with a "done" func, add a record
2904 * to the read to invoke the "done" func when the read completes,
2905 * and return; or just return.
2907 * arc_read_done() will invoke all the requested "done" functions
2908 * for readers of this block.
2910 * Normal callers should use arc_read and pass the arc buffer and offset
2911 * for the bp. But if you know you don't need locking, you can use
2915 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf,
2916 arc_done_func_t *done, void *private, int priority, int zio_flags,
2917 uint32_t *arc_flags, const zbookmark_t *zb)
2923 * XXX This happens from traverse callback funcs, for
2924 * the objset_phys_t block.
2926 return (arc_read_nolock(pio, spa, bp, done, private, priority,
2927 zio_flags, arc_flags, zb));
2930 ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt));
2931 ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size);
2932 rw_enter(&pbuf->b_data_lock, RW_READER);
2934 err = arc_read_nolock(pio, spa, bp, done, private, priority,
2935 zio_flags, arc_flags, zb);
2936 rw_exit(&pbuf->b_data_lock);
2942 arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp,
2943 arc_done_func_t *done, void *private, int priority, int zio_flags,
2944 uint32_t *arc_flags, const zbookmark_t *zb)
2947 arc_buf_t *buf = NULL;
2948 kmutex_t *hash_lock;
2950 uint64_t guid = spa_load_guid(spa);
2953 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
2955 if (hdr && hdr->b_datacnt > 0) {
2957 *arc_flags |= ARC_CACHED;
2959 if (HDR_IO_IN_PROGRESS(hdr)) {
2961 if (*arc_flags & ARC_WAIT) {
2962 cv_wait(&hdr->b_cv, hash_lock);
2963 mutex_exit(hash_lock);
2966 ASSERT(*arc_flags & ARC_NOWAIT);
2969 arc_callback_t *acb = NULL;
2971 acb = kmem_zalloc(sizeof (arc_callback_t),
2973 acb->acb_done = done;
2974 acb->acb_private = private;
2976 acb->acb_zio_dummy = zio_null(pio,
2977 spa, NULL, NULL, NULL, zio_flags);
2979 ASSERT(acb->acb_done != NULL);
2980 acb->acb_next = hdr->b_acb;
2982 add_reference(hdr, hash_lock, private);
2983 mutex_exit(hash_lock);
2986 mutex_exit(hash_lock);
2990 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2993 add_reference(hdr, hash_lock, private);
2995 * If this block is already in use, create a new
2996 * copy of the data so that we will be guaranteed
2997 * that arc_release() will always succeed.
3001 ASSERT(buf->b_data);
3002 if (HDR_BUF_AVAILABLE(hdr)) {
3003 ASSERT(buf->b_efunc == NULL);
3004 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3006 buf = arc_buf_clone(buf);
3009 } else if (*arc_flags & ARC_PREFETCH &&
3010 refcount_count(&hdr->b_refcnt) == 0) {
3011 hdr->b_flags |= ARC_PREFETCH;
3013 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
3014 arc_access(hdr, hash_lock);
3015 if (*arc_flags & ARC_L2CACHE)
3016 hdr->b_flags |= ARC_L2CACHE;
3017 mutex_exit(hash_lock);
3018 ARCSTAT_BUMP(arcstat_hits);
3019 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3020 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3021 data, metadata, hits);
3024 done(NULL, buf, private);
3026 uint64_t size = BP_GET_LSIZE(bp);
3027 arc_callback_t *acb;
3030 boolean_t devw = B_FALSE;
3033 /* this block is not in the cache */
3034 arc_buf_hdr_t *exists;
3035 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
3036 buf = arc_buf_alloc(spa, size, private, type);
3038 hdr->b_dva = *BP_IDENTITY(bp);
3039 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
3040 hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
3041 exists = buf_hash_insert(hdr, &hash_lock);
3043 /* somebody beat us to the hash insert */
3044 mutex_exit(hash_lock);
3045 buf_discard_identity(hdr);
3046 (void) arc_buf_remove_ref(buf, private);
3047 goto top; /* restart the IO request */
3049 /* if this is a prefetch, we don't have a reference */
3050 if (*arc_flags & ARC_PREFETCH) {
3051 (void) remove_reference(hdr, hash_lock,
3053 hdr->b_flags |= ARC_PREFETCH;
3055 if (*arc_flags & ARC_L2CACHE)
3056 hdr->b_flags |= ARC_L2CACHE;
3057 if (BP_GET_LEVEL(bp) > 0)
3058 hdr->b_flags |= ARC_INDIRECT;
3060 /* this block is in the ghost cache */
3061 ASSERT(GHOST_STATE(hdr->b_state));
3062 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3063 ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0);
3064 ASSERT(hdr->b_buf == NULL);
3066 /* if this is a prefetch, we don't have a reference */
3067 if (*arc_flags & ARC_PREFETCH)
3068 hdr->b_flags |= ARC_PREFETCH;
3070 add_reference(hdr, hash_lock, private);
3071 if (*arc_flags & ARC_L2CACHE)
3072 hdr->b_flags |= ARC_L2CACHE;
3073 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
3076 buf->b_efunc = NULL;
3077 buf->b_private = NULL;
3080 ASSERT(hdr->b_datacnt == 0);
3082 arc_get_data_buf(buf);
3083 arc_access(hdr, hash_lock);
3086 ASSERT(!GHOST_STATE(hdr->b_state));
3088 acb = kmem_zalloc(sizeof (arc_callback_t), KM_PUSHPAGE);
3089 acb->acb_done = done;
3090 acb->acb_private = private;
3092 ASSERT(hdr->b_acb == NULL);
3094 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3096 if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
3097 (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
3098 devw = hdr->b_l2hdr->b_dev->l2ad_writing;
3099 addr = hdr->b_l2hdr->b_daddr;
3101 * Lock out device removal.
3103 if (vdev_is_dead(vd) ||
3104 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
3108 mutex_exit(hash_lock);
3110 ASSERT3U(hdr->b_size, ==, size);
3111 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
3112 uint64_t, size, zbookmark_t *, zb);
3113 ARCSTAT_BUMP(arcstat_misses);
3114 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3115 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3116 data, metadata, misses);
3118 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
3120 * Read from the L2ARC if the following are true:
3121 * 1. The L2ARC vdev was previously cached.
3122 * 2. This buffer still has L2ARC metadata.
3123 * 3. This buffer isn't currently writing to the L2ARC.
3124 * 4. The L2ARC entry wasn't evicted, which may
3125 * also have invalidated the vdev.
3126 * 5. This isn't prefetch and l2arc_noprefetch is set.
3128 if (hdr->b_l2hdr != NULL &&
3129 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
3130 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
3131 l2arc_read_callback_t *cb;
3133 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
3134 ARCSTAT_BUMP(arcstat_l2_hits);
3136 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
3138 cb->l2rcb_buf = buf;
3139 cb->l2rcb_spa = spa;
3142 cb->l2rcb_flags = zio_flags;
3145 * l2arc read. The SCL_L2ARC lock will be
3146 * released by l2arc_read_done().
3148 rzio = zio_read_phys(pio, vd, addr, size,
3149 buf->b_data, ZIO_CHECKSUM_OFF,
3150 l2arc_read_done, cb, priority, zio_flags |
3151 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL |
3152 ZIO_FLAG_DONT_PROPAGATE |
3153 ZIO_FLAG_DONT_RETRY, B_FALSE);
3154 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
3156 ARCSTAT_INCR(arcstat_l2_read_bytes, size);
3158 if (*arc_flags & ARC_NOWAIT) {
3163 ASSERT(*arc_flags & ARC_WAIT);
3164 if (zio_wait(rzio) == 0)
3167 /* l2arc read error; goto zio_read() */
3169 DTRACE_PROBE1(l2arc__miss,
3170 arc_buf_hdr_t *, hdr);
3171 ARCSTAT_BUMP(arcstat_l2_misses);
3172 if (HDR_L2_WRITING(hdr))
3173 ARCSTAT_BUMP(arcstat_l2_rw_clash);
3174 spa_config_exit(spa, SCL_L2ARC, vd);
3178 spa_config_exit(spa, SCL_L2ARC, vd);
3179 if (l2arc_ndev != 0) {
3180 DTRACE_PROBE1(l2arc__miss,
3181 arc_buf_hdr_t *, hdr);
3182 ARCSTAT_BUMP(arcstat_l2_misses);
3186 rzio = zio_read(pio, spa, bp, buf->b_data, size,
3187 arc_read_done, buf, priority, zio_flags, zb);
3189 if (*arc_flags & ARC_WAIT)
3190 return (zio_wait(rzio));
3192 ASSERT(*arc_flags & ARC_NOWAIT);
3199 arc_add_prune_callback(arc_prune_func_t *func, void *private)
3203 p = kmem_alloc(sizeof(*p), KM_SLEEP);
3205 p->p_private = private;
3206 list_link_init(&p->p_node);
3207 refcount_create(&p->p_refcnt);
3209 mutex_enter(&arc_prune_mtx);
3210 refcount_add(&p->p_refcnt, &arc_prune_list);
3211 list_insert_head(&arc_prune_list, p);
3212 mutex_exit(&arc_prune_mtx);
3218 arc_remove_prune_callback(arc_prune_t *p)
3220 mutex_enter(&arc_prune_mtx);
3221 list_remove(&arc_prune_list, p);
3222 if (refcount_remove(&p->p_refcnt, &arc_prune_list) == 0) {
3223 refcount_destroy(&p->p_refcnt);
3224 kmem_free(p, sizeof (*p));
3226 mutex_exit(&arc_prune_mtx);
3230 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
3232 ASSERT(buf->b_hdr != NULL);
3233 ASSERT(buf->b_hdr->b_state != arc_anon);
3234 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
3235 ASSERT(buf->b_efunc == NULL);
3236 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
3238 buf->b_efunc = func;
3239 buf->b_private = private;
3243 * This is used by the DMU to let the ARC know that a buffer is
3244 * being evicted, so the ARC should clean up. If this arc buf
3245 * is not yet in the evicted state, it will be put there.
3248 arc_buf_evict(arc_buf_t *buf)
3251 kmutex_t *hash_lock;
3254 mutex_enter(&buf->b_evict_lock);
3258 * We are in arc_do_user_evicts().
3260 ASSERT(buf->b_data == NULL);
3261 mutex_exit(&buf->b_evict_lock);
3263 } else if (buf->b_data == NULL) {
3264 arc_buf_t copy = *buf; /* structure assignment */
3266 * We are on the eviction list; process this buffer now
3267 * but let arc_do_user_evicts() do the reaping.
3269 buf->b_efunc = NULL;
3270 mutex_exit(&buf->b_evict_lock);
3271 VERIFY(copy.b_efunc(©) == 0);
3274 hash_lock = HDR_LOCK(hdr);
3275 mutex_enter(hash_lock);
3277 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3279 ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
3280 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3283 * Pull this buffer off of the hdr
3286 while (*bufp != buf)
3287 bufp = &(*bufp)->b_next;
3288 *bufp = buf->b_next;
3290 ASSERT(buf->b_data != NULL);
3291 arc_buf_destroy(buf, FALSE, FALSE);
3293 if (hdr->b_datacnt == 0) {
3294 arc_state_t *old_state = hdr->b_state;
3295 arc_state_t *evicted_state;
3297 ASSERT(hdr->b_buf == NULL);
3298 ASSERT(refcount_is_zero(&hdr->b_refcnt));
3301 (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
3303 mutex_enter(&old_state->arcs_mtx);
3304 mutex_enter(&evicted_state->arcs_mtx);
3306 arc_change_state(evicted_state, hdr, hash_lock);
3307 ASSERT(HDR_IN_HASH_TABLE(hdr));
3308 hdr->b_flags |= ARC_IN_HASH_TABLE;
3309 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3311 mutex_exit(&evicted_state->arcs_mtx);
3312 mutex_exit(&old_state->arcs_mtx);
3314 mutex_exit(hash_lock);
3315 mutex_exit(&buf->b_evict_lock);
3317 VERIFY(buf->b_efunc(buf) == 0);
3318 buf->b_efunc = NULL;
3319 buf->b_private = NULL;
3322 kmem_cache_free(buf_cache, buf);
3327 * Release this buffer from the cache. This must be done
3328 * after a read and prior to modifying the buffer contents.
3329 * If the buffer has more than one reference, we must make
3330 * a new hdr for the buffer.
3333 arc_release(arc_buf_t *buf, void *tag)
3336 kmutex_t *hash_lock = NULL;
3337 l2arc_buf_hdr_t *l2hdr;
3338 uint64_t buf_size = 0;
3341 * It would be nice to assert that if it's DMU metadata (level >
3342 * 0 || it's the dnode file), then it must be syncing context.
3343 * But we don't know that information at this level.
3346 mutex_enter(&buf->b_evict_lock);
3349 /* this buffer is not on any list */
3350 ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3352 if (hdr->b_state == arc_anon) {
3353 /* this buffer is already released */
3354 ASSERT(buf->b_efunc == NULL);
3356 hash_lock = HDR_LOCK(hdr);
3357 mutex_enter(hash_lock);
3359 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3362 l2hdr = hdr->b_l2hdr;
3364 mutex_enter(&l2arc_buflist_mtx);
3365 hdr->b_l2hdr = NULL;
3366 buf_size = hdr->b_size;
3370 * Do we have more than one buf?
3372 if (hdr->b_datacnt > 1) {
3373 arc_buf_hdr_t *nhdr;
3375 uint64_t blksz = hdr->b_size;
3376 uint64_t spa = hdr->b_spa;
3377 arc_buf_contents_t type = hdr->b_type;
3378 uint32_t flags = hdr->b_flags;
3380 ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3382 * Pull the data off of this hdr and attach it to
3383 * a new anonymous hdr.
3385 (void) remove_reference(hdr, hash_lock, tag);
3387 while (*bufp != buf)
3388 bufp = &(*bufp)->b_next;
3389 *bufp = buf->b_next;
3392 ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3393 atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3394 if (refcount_is_zero(&hdr->b_refcnt)) {
3395 uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3396 ASSERT3U(*size, >=, hdr->b_size);
3397 atomic_add_64(size, -hdr->b_size);
3401 * We're releasing a duplicate user data buffer, update
3402 * our statistics accordingly.
3404 if (hdr->b_type == ARC_BUFC_DATA) {
3405 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
3406 ARCSTAT_INCR(arcstat_duplicate_buffers_size,
3409 hdr->b_datacnt -= 1;
3410 arc_cksum_verify(buf);
3412 mutex_exit(hash_lock);
3414 nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3415 nhdr->b_size = blksz;
3417 nhdr->b_type = type;
3419 nhdr->b_state = arc_anon;
3420 nhdr->b_arc_access = 0;
3421 nhdr->b_flags = flags & ARC_L2_WRITING;
3422 nhdr->b_l2hdr = NULL;
3423 nhdr->b_datacnt = 1;
3424 nhdr->b_freeze_cksum = NULL;
3425 (void) refcount_add(&nhdr->b_refcnt, tag);
3427 mutex_exit(&buf->b_evict_lock);
3428 atomic_add_64(&arc_anon->arcs_size, blksz);
3430 mutex_exit(&buf->b_evict_lock);
3431 ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3432 ASSERT(!list_link_active(&hdr->b_arc_node));
3433 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3434 if (hdr->b_state != arc_anon)
3435 arc_change_state(arc_anon, hdr, hash_lock);
3436 hdr->b_arc_access = 0;
3438 mutex_exit(hash_lock);
3440 buf_discard_identity(hdr);
3443 buf->b_efunc = NULL;
3444 buf->b_private = NULL;
3447 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3448 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3449 ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3450 mutex_exit(&l2arc_buflist_mtx);
3455 * Release this buffer. If it does not match the provided BP, fill it
3456 * with that block's contents.
3460 arc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa,
3463 arc_release(buf, tag);
3468 arc_released(arc_buf_t *buf)
3472 mutex_enter(&buf->b_evict_lock);
3473 released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3474 mutex_exit(&buf->b_evict_lock);
3479 arc_has_callback(arc_buf_t *buf)
3483 mutex_enter(&buf->b_evict_lock);
3484 callback = (buf->b_efunc != NULL);
3485 mutex_exit(&buf->b_evict_lock);
3491 arc_referenced(arc_buf_t *buf)
3495 mutex_enter(&buf->b_evict_lock);
3496 referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3497 mutex_exit(&buf->b_evict_lock);
3498 return (referenced);
3503 arc_write_ready(zio_t *zio)
3505 arc_write_callback_t *callback = zio->io_private;
3506 arc_buf_t *buf = callback->awcb_buf;
3507 arc_buf_hdr_t *hdr = buf->b_hdr;
3509 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3510 callback->awcb_ready(zio, buf, callback->awcb_private);
3513 * If the IO is already in progress, then this is a re-write
3514 * attempt, so we need to thaw and re-compute the cksum.
3515 * It is the responsibility of the callback to handle the
3516 * accounting for any re-write attempt.
3518 if (HDR_IO_IN_PROGRESS(hdr)) {
3519 mutex_enter(&hdr->b_freeze_lock);
3520 if (hdr->b_freeze_cksum != NULL) {
3521 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3522 hdr->b_freeze_cksum = NULL;
3524 mutex_exit(&hdr->b_freeze_lock);
3526 arc_cksum_compute(buf, B_FALSE);
3527 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3531 arc_write_done(zio_t *zio)
3533 arc_write_callback_t *callback = zio->io_private;
3534 arc_buf_t *buf = callback->awcb_buf;
3535 arc_buf_hdr_t *hdr = buf->b_hdr;
3537 ASSERT(hdr->b_acb == NULL);
3539 if (zio->io_error == 0) {
3540 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3541 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3542 hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3544 ASSERT(BUF_EMPTY(hdr));
3548 * If the block to be written was all-zero, we may have
3549 * compressed it away. In this case no write was performed
3550 * so there will be no dva/birth/checksum. The buffer must
3551 * therefore remain anonymous (and uncached).
3553 if (!BUF_EMPTY(hdr)) {
3554 arc_buf_hdr_t *exists;
3555 kmutex_t *hash_lock;
3557 ASSERT(zio->io_error == 0);
3559 arc_cksum_verify(buf);
3561 exists = buf_hash_insert(hdr, &hash_lock);
3564 * This can only happen if we overwrite for
3565 * sync-to-convergence, because we remove
3566 * buffers from the hash table when we arc_free().
3568 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3569 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3570 panic("bad overwrite, hdr=%p exists=%p",
3571 (void *)hdr, (void *)exists);
3572 ASSERT(refcount_is_zero(&exists->b_refcnt));
3573 arc_change_state(arc_anon, exists, hash_lock);
3574 mutex_exit(hash_lock);
3575 arc_hdr_destroy(exists);
3576 exists = buf_hash_insert(hdr, &hash_lock);
3577 ASSERT3P(exists, ==, NULL);
3580 ASSERT(hdr->b_datacnt == 1);
3581 ASSERT(hdr->b_state == arc_anon);
3582 ASSERT(BP_GET_DEDUP(zio->io_bp));
3583 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3586 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3587 /* if it's not anon, we are doing a scrub */
3588 if (!exists && hdr->b_state == arc_anon)
3589 arc_access(hdr, hash_lock);
3590 mutex_exit(hash_lock);
3592 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3595 ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3596 callback->awcb_done(zio, buf, callback->awcb_private);
3598 kmem_free(callback, sizeof (arc_write_callback_t));
3602 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3603 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
3604 arc_done_func_t *ready, arc_done_func_t *done, void *private,
3605 int priority, int zio_flags, const zbookmark_t *zb)
3607 arc_buf_hdr_t *hdr = buf->b_hdr;
3608 arc_write_callback_t *callback;
3611 ASSERT(ready != NULL);
3612 ASSERT(done != NULL);
3613 ASSERT(!HDR_IO_ERROR(hdr));
3614 ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3615 ASSERT(hdr->b_acb == NULL);
3617 hdr->b_flags |= ARC_L2CACHE;
3618 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_PUSHPAGE);
3619 callback->awcb_ready = ready;
3620 callback->awcb_done = done;
3621 callback->awcb_private = private;
3622 callback->awcb_buf = buf;
3624 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3625 arc_write_ready, arc_write_done, callback, priority, zio_flags, zb);
3631 arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg)
3634 uint64_t available_memory;
3636 /* Easily reclaimable memory (free + inactive + arc-evictable) */
3637 available_memory = ptob(spl_kmem_availrmem()) + arc_evictable_memory();
3639 if (available_memory <= zfs_write_limit_max) {
3640 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3641 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim);
3645 if (inflight_data > available_memory / 4) {
3646 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3647 DMU_TX_STAT_BUMP(dmu_tx_memory_inflight);
3655 arc_tempreserve_clear(uint64_t reserve)
3657 atomic_add_64(&arc_tempreserve, -reserve);
3658 ASSERT((int64_t)arc_tempreserve >= 0);
3662 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3669 * Once in a while, fail for no reason. Everything should cope.
3671 if (spa_get_random(10000) == 0) {
3672 dprintf("forcing random failure\n");
3676 if (reserve > arc_c/4 && !arc_no_grow)
3677 arc_c = MIN(arc_c_max, reserve * 4);
3678 if (reserve > arc_c) {
3679 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve);
3684 * Don't count loaned bufs as in flight dirty data to prevent long
3685 * network delays from blocking transactions that are ready to be
3686 * assigned to a txg.
3688 anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3691 * Writes will, almost always, require additional memory allocations
3692 * in order to compress/encrypt/etc the data. We therefor need to
3693 * make sure that there is sufficient available memory for this.
3695 if ((error = arc_memory_throttle(reserve, anon_size, txg)))
3699 * Throttle writes when the amount of dirty data in the cache
3700 * gets too large. We try to keep the cache less than half full
3701 * of dirty blocks so that our sync times don't grow too large.
3702 * Note: if two requests come in concurrently, we might let them
3703 * both succeed, when one of them should fail. Not a huge deal.
3706 if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3707 anon_size > arc_c / 4) {
3708 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3709 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3710 arc_tempreserve>>10,
3711 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3712 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3713 reserve>>10, arc_c>>10);
3714 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle);
3717 atomic_add_64(&arc_tempreserve, reserve);
3722 arc_kstat_update_state(arc_state_t *state, kstat_named_t *size,
3723 kstat_named_t *evict_data, kstat_named_t *evict_metadata)
3725 size->value.ui64 = state->arcs_size;
3726 evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA];
3727 evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA];
3731 arc_kstat_update(kstat_t *ksp, int rw)
3733 arc_stats_t *as = ksp->ks_data;
3735 if (rw == KSTAT_WRITE) {
3738 arc_kstat_update_state(arc_anon,
3739 &as->arcstat_anon_size,
3740 &as->arcstat_anon_evict_data,
3741 &as->arcstat_anon_evict_metadata);
3742 arc_kstat_update_state(arc_mru,
3743 &as->arcstat_mru_size,
3744 &as->arcstat_mru_evict_data,
3745 &as->arcstat_mru_evict_metadata);
3746 arc_kstat_update_state(arc_mru_ghost,
3747 &as->arcstat_mru_ghost_size,
3748 &as->arcstat_mru_ghost_evict_data,
3749 &as->arcstat_mru_ghost_evict_metadata);
3750 arc_kstat_update_state(arc_mfu,
3751 &as->arcstat_mfu_size,
3752 &as->arcstat_mfu_evict_data,
3753 &as->arcstat_mfu_evict_metadata);
3754 arc_kstat_update_state(arc_mfu_ghost,
3755 &as->arcstat_mfu_ghost_size,
3756 &as->arcstat_mfu_ghost_evict_data,
3757 &as->arcstat_mfu_ghost_evict_metadata);
3766 mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3767 cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3769 /* Convert seconds to clock ticks */
3770 arc_min_prefetch_lifespan = 1 * hz;
3772 /* Start out with 1/8 of all memory */
3773 arc_c = physmem * PAGESIZE / 8;
3777 * On architectures where the physical memory can be larger
3778 * than the addressable space (intel in 32-bit mode), we may
3779 * need to limit the cache to 1/8 of VM size.
3781 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3783 * Register a shrinker to support synchronous (direct) memory
3784 * reclaim from the arc. This is done to prevent kswapd from
3785 * swapping out pages when it is preferable to shrink the arc.
3787 spl_register_shrinker(&arc_shrinker);
3790 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3791 arc_c_min = MAX(arc_c / 4, 64<<20);
3792 /* set max to 1/2 of all memory */
3793 arc_c_max = MAX(arc_c * 4, arc_c_max);
3796 * Allow the tunables to override our calculations if they are
3797 * reasonable (ie. over 64MB)
3799 if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
3800 arc_c_max = zfs_arc_max;
3801 if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
3802 arc_c_min = zfs_arc_min;
3805 arc_p = (arc_c >> 1);
3807 /* limit meta-data to 1/4 of the arc capacity */
3808 arc_meta_limit = arc_c_max / 4;
3811 /* Allow the tunable to override if it is reasonable */
3812 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
3813 arc_meta_limit = zfs_arc_meta_limit;
3815 if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3816 arc_c_min = arc_meta_limit / 2;
3818 if (zfs_arc_grow_retry > 0)
3819 arc_grow_retry = zfs_arc_grow_retry;
3821 if (zfs_arc_shrink_shift > 0)
3822 arc_shrink_shift = zfs_arc_shrink_shift;
3824 if (zfs_arc_p_min_shift > 0)
3825 arc_p_min_shift = zfs_arc_p_min_shift;
3827 if (zfs_arc_meta_prune > 0)
3828 arc_meta_prune = zfs_arc_meta_prune;
3830 /* if kmem_flags are set, lets try to use less memory */
3831 if (kmem_debugging())
3833 if (arc_c < arc_c_min)
3836 arc_anon = &ARC_anon;
3838 arc_mru_ghost = &ARC_mru_ghost;
3840 arc_mfu_ghost = &ARC_mfu_ghost;
3841 arc_l2c_only = &ARC_l2c_only;
3844 mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3845 mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3846 mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3847 mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3848 mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3849 mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3851 list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
3852 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3853 list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
3854 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3855 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
3856 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3857 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
3858 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3859 list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
3860 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3861 list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
3862 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3863 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
3864 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3865 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
3866 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3867 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
3868 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3869 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
3870 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3874 arc_thread_exit = 0;
3875 list_create(&arc_prune_list, sizeof (arc_prune_t),
3876 offsetof(arc_prune_t, p_node));
3877 arc_eviction_list = NULL;
3878 mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL);
3879 mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
3880 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3882 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
3883 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3885 if (arc_ksp != NULL) {
3886 arc_ksp->ks_data = &arc_stats;
3887 arc_ksp->ks_update = arc_kstat_update;
3888 kstat_install(arc_ksp);
3891 (void) thread_create(NULL, 0, arc_adapt_thread, NULL, 0, &p0,
3892 TS_RUN, minclsyspri);
3897 if (zfs_write_limit_max == 0)
3898 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
3900 zfs_write_limit_shift = 0;
3901 mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL);
3909 mutex_enter(&arc_reclaim_thr_lock);
3911 spl_unregister_shrinker(&arc_shrinker);
3912 #endif /* _KERNEL */
3914 arc_thread_exit = 1;
3915 while (arc_thread_exit != 0)
3916 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3917 mutex_exit(&arc_reclaim_thr_lock);
3923 if (arc_ksp != NULL) {
3924 kstat_delete(arc_ksp);
3928 mutex_enter(&arc_prune_mtx);
3929 while ((p = list_head(&arc_prune_list)) != NULL) {
3930 list_remove(&arc_prune_list, p);
3931 refcount_remove(&p->p_refcnt, &arc_prune_list);
3932 refcount_destroy(&p->p_refcnt);
3933 kmem_free(p, sizeof (*p));
3935 mutex_exit(&arc_prune_mtx);
3937 list_destroy(&arc_prune_list);
3938 mutex_destroy(&arc_prune_mtx);
3939 mutex_destroy(&arc_eviction_mtx);
3940 mutex_destroy(&arc_reclaim_thr_lock);
3941 cv_destroy(&arc_reclaim_thr_cv);
3943 list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
3944 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
3945 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
3946 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
3947 list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
3948 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
3949 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
3950 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3952 mutex_destroy(&arc_anon->arcs_mtx);
3953 mutex_destroy(&arc_mru->arcs_mtx);
3954 mutex_destroy(&arc_mru_ghost->arcs_mtx);
3955 mutex_destroy(&arc_mfu->arcs_mtx);
3956 mutex_destroy(&arc_mfu_ghost->arcs_mtx);
3957 mutex_destroy(&arc_l2c_only->arcs_mtx);
3959 mutex_destroy(&zfs_write_limit_lock);
3963 ASSERT(arc_loaned_bytes == 0);
3969 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3970 * It uses dedicated storage devices to hold cached data, which are populated
3971 * using large infrequent writes. The main role of this cache is to boost
3972 * the performance of random read workloads. The intended L2ARC devices
3973 * include short-stroked disks, solid state disks, and other media with
3974 * substantially faster read latency than disk.
3976 * +-----------------------+
3978 * +-----------------------+
3981 * l2arc_feed_thread() arc_read()
3985 * +---------------+ |
3987 * +---------------+ |
3992 * +-------+ +-------+
3994 * | cache | | cache |
3995 * +-------+ +-------+
3996 * +=========+ .-----.
3997 * : L2ARC : |-_____-|
3998 * : devices : | Disks |
3999 * +=========+ `-_____-'
4001 * Read requests are satisfied from the following sources, in order:
4004 * 2) vdev cache of L2ARC devices
4006 * 4) vdev cache of disks
4009 * Some L2ARC device types exhibit extremely slow write performance.
4010 * To accommodate for this there are some significant differences between
4011 * the L2ARC and traditional cache design:
4013 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
4014 * the ARC behave as usual, freeing buffers and placing headers on ghost
4015 * lists. The ARC does not send buffers to the L2ARC during eviction as
4016 * this would add inflated write latencies for all ARC memory pressure.
4018 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
4019 * It does this by periodically scanning buffers from the eviction-end of
4020 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
4021 * not already there. It scans until a headroom of buffers is satisfied,
4022 * which itself is a buffer for ARC eviction. The thread that does this is
4023 * l2arc_feed_thread(), illustrated below; example sizes are included to
4024 * provide a better sense of ratio than this diagram:
4027 * +---------------------+----------+
4028 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
4029 * +---------------------+----------+ | o L2ARC eligible
4030 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
4031 * +---------------------+----------+ |
4032 * 15.9 Gbytes ^ 32 Mbytes |
4034 * l2arc_feed_thread()
4036 * l2arc write hand <--[oooo]--'
4040 * +==============================+
4041 * L2ARC dev |####|#|###|###| |####| ... |
4042 * +==============================+
4045 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
4046 * evicted, then the L2ARC has cached a buffer much sooner than it probably
4047 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
4048 * safe to say that this is an uncommon case, since buffers at the end of
4049 * the ARC lists have moved there due to inactivity.
4051 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
4052 * then the L2ARC simply misses copying some buffers. This serves as a
4053 * pressure valve to prevent heavy read workloads from both stalling the ARC
4054 * with waits and clogging the L2ARC with writes. This also helps prevent
4055 * the potential for the L2ARC to churn if it attempts to cache content too
4056 * quickly, such as during backups of the entire pool.
4058 * 5. After system boot and before the ARC has filled main memory, there are
4059 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
4060 * lists can remain mostly static. Instead of searching from tail of these
4061 * lists as pictured, the l2arc_feed_thread() will search from the list heads
4062 * for eligible buffers, greatly increasing its chance of finding them.
4064 * The L2ARC device write speed is also boosted during this time so that
4065 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
4066 * there are no L2ARC reads, and no fear of degrading read performance
4067 * through increased writes.
4069 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
4070 * the vdev queue can aggregate them into larger and fewer writes. Each
4071 * device is written to in a rotor fashion, sweeping writes through
4072 * available space then repeating.
4074 * 7. The L2ARC does not store dirty content. It never needs to flush
4075 * write buffers back to disk based storage.
4077 * 8. If an ARC buffer is written (and dirtied) which also exists in the
4078 * L2ARC, the now stale L2ARC buffer is immediately dropped.
4080 * The performance of the L2ARC can be tweaked by a number of tunables, which
4081 * may be necessary for different workloads:
4083 * l2arc_write_max max write bytes per interval
4084 * l2arc_write_boost extra write bytes during device warmup
4085 * l2arc_noprefetch skip caching prefetched buffers
4086 * l2arc_headroom number of max device writes to precache
4087 * l2arc_feed_secs seconds between L2ARC writing
4089 * Tunables may be removed or added as future performance improvements are
4090 * integrated, and also may become zpool properties.
4092 * There are three key functions that control how the L2ARC warms up:
4094 * l2arc_write_eligible() check if a buffer is eligible to cache
4095 * l2arc_write_size() calculate how much to write
4096 * l2arc_write_interval() calculate sleep delay between writes
4098 * These three functions determine what to write, how much, and how quickly
4103 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
4106 * A buffer is *not* eligible for the L2ARC if it:
4107 * 1. belongs to a different spa.
4108 * 2. is already cached on the L2ARC.
4109 * 3. has an I/O in progress (it may be an incomplete read).
4110 * 4. is flagged not eligible (zfs property).
4112 if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
4113 HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
4120 l2arc_write_size(l2arc_dev_t *dev)
4124 size = dev->l2ad_write;
4126 if (arc_warm == B_FALSE)
4127 size += dev->l2ad_boost;
4134 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
4136 clock_t interval, next, now;
4139 * If the ARC lists are busy, increase our write rate; if the
4140 * lists are stale, idle back. This is achieved by checking
4141 * how much we previously wrote - if it was more than half of
4142 * what we wanted, schedule the next write much sooner.
4144 if (l2arc_feed_again && wrote > (wanted / 2))
4145 interval = (hz * l2arc_feed_min_ms) / 1000;
4147 interval = hz * l2arc_feed_secs;
4149 now = ddi_get_lbolt();
4150 next = MAX(now, MIN(now + interval, began + interval));
4156 l2arc_hdr_stat_add(void)
4158 ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
4159 ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
4163 l2arc_hdr_stat_remove(void)
4165 ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
4166 ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
4170 * Cycle through L2ARC devices. This is how L2ARC load balances.
4171 * If a device is returned, this also returns holding the spa config lock.
4173 static l2arc_dev_t *
4174 l2arc_dev_get_next(void)
4176 l2arc_dev_t *first, *next = NULL;
4179 * Lock out the removal of spas (spa_namespace_lock), then removal
4180 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
4181 * both locks will be dropped and a spa config lock held instead.
4183 mutex_enter(&spa_namespace_lock);
4184 mutex_enter(&l2arc_dev_mtx);
4186 /* if there are no vdevs, there is nothing to do */
4187 if (l2arc_ndev == 0)
4191 next = l2arc_dev_last;
4193 /* loop around the list looking for a non-faulted vdev */
4195 next = list_head(l2arc_dev_list);
4197 next = list_next(l2arc_dev_list, next);
4199 next = list_head(l2arc_dev_list);
4202 /* if we have come back to the start, bail out */
4205 else if (next == first)
4208 } while (vdev_is_dead(next->l2ad_vdev));
4210 /* if we were unable to find any usable vdevs, return NULL */
4211 if (vdev_is_dead(next->l2ad_vdev))
4214 l2arc_dev_last = next;
4217 mutex_exit(&l2arc_dev_mtx);
4220 * Grab the config lock to prevent the 'next' device from being
4221 * removed while we are writing to it.
4224 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
4225 mutex_exit(&spa_namespace_lock);
4231 * Free buffers that were tagged for destruction.
4234 l2arc_do_free_on_write(void)
4237 l2arc_data_free_t *df, *df_prev;
4239 mutex_enter(&l2arc_free_on_write_mtx);
4240 buflist = l2arc_free_on_write;
4242 for (df = list_tail(buflist); df; df = df_prev) {
4243 df_prev = list_prev(buflist, df);
4244 ASSERT(df->l2df_data != NULL);
4245 ASSERT(df->l2df_func != NULL);
4246 df->l2df_func(df->l2df_data, df->l2df_size);
4247 list_remove(buflist, df);
4248 kmem_free(df, sizeof (l2arc_data_free_t));
4251 mutex_exit(&l2arc_free_on_write_mtx);
4255 * A write to a cache device has completed. Update all headers to allow
4256 * reads from these buffers to begin.
4259 l2arc_write_done(zio_t *zio)
4261 l2arc_write_callback_t *cb;
4264 arc_buf_hdr_t *head, *ab, *ab_prev;
4265 l2arc_buf_hdr_t *abl2;
4266 kmutex_t *hash_lock;
4268 cb = zio->io_private;
4270 dev = cb->l2wcb_dev;
4271 ASSERT(dev != NULL);
4272 head = cb->l2wcb_head;
4273 ASSERT(head != NULL);
4274 buflist = dev->l2ad_buflist;
4275 ASSERT(buflist != NULL);
4276 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
4277 l2arc_write_callback_t *, cb);
4279 if (zio->io_error != 0)
4280 ARCSTAT_BUMP(arcstat_l2_writes_error);
4282 mutex_enter(&l2arc_buflist_mtx);
4285 * All writes completed, or an error was hit.
4287 for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
4288 ab_prev = list_prev(buflist, ab);
4290 hash_lock = HDR_LOCK(ab);
4291 if (!mutex_tryenter(hash_lock)) {
4293 * This buffer misses out. It may be in a stage
4294 * of eviction. Its ARC_L2_WRITING flag will be
4295 * left set, denying reads to this buffer.
4297 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
4301 if (zio->io_error != 0) {
4303 * Error - drop L2ARC entry.
4305 list_remove(buflist, ab);
4308 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4309 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4313 * Allow ARC to begin reads to this L2ARC entry.
4315 ab->b_flags &= ~ARC_L2_WRITING;
4317 mutex_exit(hash_lock);
4320 atomic_inc_64(&l2arc_writes_done);
4321 list_remove(buflist, head);
4322 kmem_cache_free(hdr_cache, head);
4323 mutex_exit(&l2arc_buflist_mtx);
4325 l2arc_do_free_on_write();
4327 kmem_free(cb, sizeof (l2arc_write_callback_t));
4331 * A read to a cache device completed. Validate buffer contents before
4332 * handing over to the regular ARC routines.
4335 l2arc_read_done(zio_t *zio)
4337 l2arc_read_callback_t *cb;
4340 kmutex_t *hash_lock;
4343 ASSERT(zio->io_vd != NULL);
4344 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
4346 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
4348 cb = zio->io_private;
4350 buf = cb->l2rcb_buf;
4351 ASSERT(buf != NULL);
4353 hash_lock = HDR_LOCK(buf->b_hdr);
4354 mutex_enter(hash_lock);
4356 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4359 * Check this survived the L2ARC journey.
4361 equal = arc_cksum_equal(buf);
4362 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4363 mutex_exit(hash_lock);
4364 zio->io_private = buf;
4365 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
4366 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
4369 mutex_exit(hash_lock);
4371 * Buffer didn't survive caching. Increment stats and
4372 * reissue to the original storage device.
4374 if (zio->io_error != 0) {
4375 ARCSTAT_BUMP(arcstat_l2_io_error);
4377 zio->io_error = EIO;
4380 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4383 * If there's no waiter, issue an async i/o to the primary
4384 * storage now. If there *is* a waiter, the caller must
4385 * issue the i/o in a context where it's OK to block.
4387 if (zio->io_waiter == NULL) {
4388 zio_t *pio = zio_unique_parent(zio);
4390 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4392 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4393 buf->b_data, zio->io_size, arc_read_done, buf,
4394 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4398 kmem_free(cb, sizeof (l2arc_read_callback_t));
4402 * This is the list priority from which the L2ARC will search for pages to
4403 * cache. This is used within loops (0..3) to cycle through lists in the
4404 * desired order. This order can have a significant effect on cache
4407 * Currently the metadata lists are hit first, MFU then MRU, followed by
4408 * the data lists. This function returns a locked list, and also returns
4412 l2arc_list_locked(int list_num, kmutex_t **lock)
4414 list_t *list = NULL;
4416 ASSERT(list_num >= 0 && list_num <= 3);
4420 list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
4421 *lock = &arc_mfu->arcs_mtx;
4424 list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
4425 *lock = &arc_mru->arcs_mtx;
4428 list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
4429 *lock = &arc_mfu->arcs_mtx;
4432 list = &arc_mru->arcs_list[ARC_BUFC_DATA];
4433 *lock = &arc_mru->arcs_mtx;
4437 ASSERT(!(MUTEX_HELD(*lock)));
4443 * Evict buffers from the device write hand to the distance specified in
4444 * bytes. This distance may span populated buffers, it may span nothing.
4445 * This is clearing a region on the L2ARC device ready for writing.
4446 * If the 'all' boolean is set, every buffer is evicted.
4449 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4452 l2arc_buf_hdr_t *abl2;
4453 arc_buf_hdr_t *ab, *ab_prev;
4454 kmutex_t *hash_lock;
4457 buflist = dev->l2ad_buflist;
4459 if (buflist == NULL)
4462 if (!all && dev->l2ad_first) {
4464 * This is the first sweep through the device. There is
4470 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4472 * When nearing the end of the device, evict to the end
4473 * before the device write hand jumps to the start.
4475 taddr = dev->l2ad_end;
4477 taddr = dev->l2ad_hand + distance;
4479 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4480 uint64_t, taddr, boolean_t, all);
4483 mutex_enter(&l2arc_buflist_mtx);
4484 for (ab = list_tail(buflist); ab; ab = ab_prev) {
4485 ab_prev = list_prev(buflist, ab);
4487 hash_lock = HDR_LOCK(ab);
4488 if (!mutex_tryenter(hash_lock)) {
4490 * Missed the hash lock. Retry.
4492 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4493 mutex_exit(&l2arc_buflist_mtx);
4494 mutex_enter(hash_lock);
4495 mutex_exit(hash_lock);
4499 if (HDR_L2_WRITE_HEAD(ab)) {
4501 * We hit a write head node. Leave it for
4502 * l2arc_write_done().
4504 list_remove(buflist, ab);
4505 mutex_exit(hash_lock);
4509 if (!all && ab->b_l2hdr != NULL &&
4510 (ab->b_l2hdr->b_daddr > taddr ||
4511 ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4513 * We've evicted to the target address,
4514 * or the end of the device.
4516 mutex_exit(hash_lock);
4520 if (HDR_FREE_IN_PROGRESS(ab)) {
4522 * Already on the path to destruction.
4524 mutex_exit(hash_lock);
4528 if (ab->b_state == arc_l2c_only) {
4529 ASSERT(!HDR_L2_READING(ab));
4531 * This doesn't exist in the ARC. Destroy.
4532 * arc_hdr_destroy() will call list_remove()
4533 * and decrement arcstat_l2_size.
4535 arc_change_state(arc_anon, ab, hash_lock);
4536 arc_hdr_destroy(ab);
4539 * Invalidate issued or about to be issued
4540 * reads, since we may be about to write
4541 * over this location.
4543 if (HDR_L2_READING(ab)) {
4544 ARCSTAT_BUMP(arcstat_l2_evict_reading);
4545 ab->b_flags |= ARC_L2_EVICTED;
4549 * Tell ARC this no longer exists in L2ARC.
4551 if (ab->b_l2hdr != NULL) {
4554 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4555 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4557 list_remove(buflist, ab);
4560 * This may have been leftover after a
4563 ab->b_flags &= ~ARC_L2_WRITING;
4565 mutex_exit(hash_lock);
4567 mutex_exit(&l2arc_buflist_mtx);
4569 vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
4570 dev->l2ad_evict = taddr;
4574 * Find and write ARC buffers to the L2ARC device.
4576 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4577 * for reading until they have completed writing.
4580 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
4582 arc_buf_hdr_t *ab, *ab_prev, *head;
4583 l2arc_buf_hdr_t *hdrl2;
4585 uint64_t passed_sz, write_sz, buf_sz, headroom;
4587 kmutex_t *hash_lock, *list_lock = NULL;
4588 boolean_t have_lock, full;
4589 l2arc_write_callback_t *cb;
4591 uint64_t guid = spa_load_guid(spa);
4594 ASSERT(dev->l2ad_vdev != NULL);
4599 head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4600 head->b_flags |= ARC_L2_WRITE_HEAD;
4603 * Copy buffers for L2ARC writing.
4605 mutex_enter(&l2arc_buflist_mtx);
4606 for (try = 0; try <= 3; try++) {
4607 list = l2arc_list_locked(try, &list_lock);
4611 * L2ARC fast warmup.
4613 * Until the ARC is warm and starts to evict, read from the
4614 * head of the ARC lists rather than the tail.
4616 headroom = target_sz * l2arc_headroom;
4617 if (arc_warm == B_FALSE)
4618 ab = list_head(list);
4620 ab = list_tail(list);
4622 for (; ab; ab = ab_prev) {
4623 if (arc_warm == B_FALSE)
4624 ab_prev = list_next(list, ab);
4626 ab_prev = list_prev(list, ab);
4628 hash_lock = HDR_LOCK(ab);
4629 have_lock = MUTEX_HELD(hash_lock);
4630 if (!have_lock && !mutex_tryenter(hash_lock)) {
4632 * Skip this buffer rather than waiting.
4637 passed_sz += ab->b_size;
4638 if (passed_sz > headroom) {
4642 mutex_exit(hash_lock);
4646 if (!l2arc_write_eligible(guid, ab)) {
4647 mutex_exit(hash_lock);
4651 if ((write_sz + ab->b_size) > target_sz) {
4653 mutex_exit(hash_lock);
4659 * Insert a dummy header on the buflist so
4660 * l2arc_write_done() can find where the
4661 * write buffers begin without searching.
4663 list_insert_head(dev->l2ad_buflist, head);
4665 cb = kmem_alloc(sizeof (l2arc_write_callback_t),
4667 cb->l2wcb_dev = dev;
4668 cb->l2wcb_head = head;
4669 pio = zio_root(spa, l2arc_write_done, cb,
4674 * Create and add a new L2ARC header.
4676 hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t),
4679 hdrl2->b_daddr = dev->l2ad_hand;
4681 ab->b_flags |= ARC_L2_WRITING;
4682 ab->b_l2hdr = hdrl2;
4683 list_insert_head(dev->l2ad_buflist, ab);
4684 buf_data = ab->b_buf->b_data;
4685 buf_sz = ab->b_size;
4688 * Compute and store the buffer cksum before
4689 * writing. On debug the cksum is verified first.
4691 arc_cksum_verify(ab->b_buf);
4692 arc_cksum_compute(ab->b_buf, B_TRUE);
4694 mutex_exit(hash_lock);
4696 wzio = zio_write_phys(pio, dev->l2ad_vdev,
4697 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
4698 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
4699 ZIO_FLAG_CANFAIL, B_FALSE);
4701 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
4703 (void) zio_nowait(wzio);
4706 * Keep the clock hand suitably device-aligned.
4708 buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
4711 dev->l2ad_hand += buf_sz;
4714 mutex_exit(list_lock);
4719 mutex_exit(&l2arc_buflist_mtx);
4722 ASSERT3U(write_sz, ==, 0);
4723 kmem_cache_free(hdr_cache, head);
4727 ASSERT3U(write_sz, <=, target_sz);
4728 ARCSTAT_BUMP(arcstat_l2_writes_sent);
4729 ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz);
4730 ARCSTAT_INCR(arcstat_l2_size, write_sz);
4731 vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0);
4734 * Bump device hand to the device start if it is approaching the end.
4735 * l2arc_evict() will already have evicted ahead for this case.
4737 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
4738 vdev_space_update(dev->l2ad_vdev,
4739 dev->l2ad_end - dev->l2ad_hand, 0, 0);
4740 dev->l2ad_hand = dev->l2ad_start;
4741 dev->l2ad_evict = dev->l2ad_start;
4742 dev->l2ad_first = B_FALSE;
4745 dev->l2ad_writing = B_TRUE;
4746 (void) zio_wait(pio);
4747 dev->l2ad_writing = B_FALSE;
4753 * This thread feeds the L2ARC at regular intervals. This is the beating
4754 * heart of the L2ARC.
4757 l2arc_feed_thread(void)
4762 uint64_t size, wrote;
4763 clock_t begin, next = ddi_get_lbolt();
4765 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
4767 mutex_enter(&l2arc_feed_thr_lock);
4769 while (l2arc_thread_exit == 0) {
4770 CALLB_CPR_SAFE_BEGIN(&cpr);
4771 (void) cv_timedwait_interruptible(&l2arc_feed_thr_cv,
4772 &l2arc_feed_thr_lock, next);
4773 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
4774 next = ddi_get_lbolt() + hz;
4777 * Quick check for L2ARC devices.
4779 mutex_enter(&l2arc_dev_mtx);
4780 if (l2arc_ndev == 0) {
4781 mutex_exit(&l2arc_dev_mtx);
4784 mutex_exit(&l2arc_dev_mtx);
4785 begin = ddi_get_lbolt();
4788 * This selects the next l2arc device to write to, and in
4789 * doing so the next spa to feed from: dev->l2ad_spa. This
4790 * will return NULL if there are now no l2arc devices or if
4791 * they are all faulted.
4793 * If a device is returned, its spa's config lock is also
4794 * held to prevent device removal. l2arc_dev_get_next()
4795 * will grab and release l2arc_dev_mtx.
4797 if ((dev = l2arc_dev_get_next()) == NULL)
4800 spa = dev->l2ad_spa;
4801 ASSERT(spa != NULL);
4804 * If the pool is read-only then force the feed thread to
4805 * sleep a little longer.
4807 if (!spa_writeable(spa)) {
4808 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
4809 spa_config_exit(spa, SCL_L2ARC, dev);
4814 * Avoid contributing to memory pressure.
4817 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
4818 spa_config_exit(spa, SCL_L2ARC, dev);
4822 ARCSTAT_BUMP(arcstat_l2_feeds);
4824 size = l2arc_write_size(dev);
4827 * Evict L2ARC buffers that will be overwritten.
4829 l2arc_evict(dev, size, B_FALSE);
4832 * Write ARC buffers.
4834 wrote = l2arc_write_buffers(spa, dev, size);
4837 * Calculate interval between writes.
4839 next = l2arc_write_interval(begin, size, wrote);
4840 spa_config_exit(spa, SCL_L2ARC, dev);
4843 l2arc_thread_exit = 0;
4844 cv_broadcast(&l2arc_feed_thr_cv);
4845 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
4850 l2arc_vdev_present(vdev_t *vd)
4854 mutex_enter(&l2arc_dev_mtx);
4855 for (dev = list_head(l2arc_dev_list); dev != NULL;
4856 dev = list_next(l2arc_dev_list, dev)) {
4857 if (dev->l2ad_vdev == vd)
4860 mutex_exit(&l2arc_dev_mtx);
4862 return (dev != NULL);
4866 * Add a vdev for use by the L2ARC. By this point the spa has already
4867 * validated the vdev and opened it.
4870 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
4872 l2arc_dev_t *adddev;
4874 ASSERT(!l2arc_vdev_present(vd));
4877 * Create a new l2arc device entry.
4879 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
4880 adddev->l2ad_spa = spa;
4881 adddev->l2ad_vdev = vd;
4882 adddev->l2ad_write = l2arc_write_max;
4883 adddev->l2ad_boost = l2arc_write_boost;
4884 adddev->l2ad_start = VDEV_LABEL_START_SIZE;
4885 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
4886 adddev->l2ad_hand = adddev->l2ad_start;
4887 adddev->l2ad_evict = adddev->l2ad_start;
4888 adddev->l2ad_first = B_TRUE;
4889 adddev->l2ad_writing = B_FALSE;
4890 list_link_init(&adddev->l2ad_node);
4891 ASSERT3U(adddev->l2ad_write, >, 0);
4894 * This is a list of all ARC buffers that are still valid on the
4897 adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
4898 list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
4899 offsetof(arc_buf_hdr_t, b_l2node));
4901 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
4904 * Add device to global list
4906 mutex_enter(&l2arc_dev_mtx);
4907 list_insert_head(l2arc_dev_list, adddev);
4908 atomic_inc_64(&l2arc_ndev);
4909 mutex_exit(&l2arc_dev_mtx);
4913 * Remove a vdev from the L2ARC.
4916 l2arc_remove_vdev(vdev_t *vd)
4918 l2arc_dev_t *dev, *nextdev, *remdev = NULL;
4921 * Find the device by vdev
4923 mutex_enter(&l2arc_dev_mtx);
4924 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
4925 nextdev = list_next(l2arc_dev_list, dev);
4926 if (vd == dev->l2ad_vdev) {
4931 ASSERT(remdev != NULL);
4934 * Remove device from global list
4936 list_remove(l2arc_dev_list, remdev);
4937 l2arc_dev_last = NULL; /* may have been invalidated */
4938 atomic_dec_64(&l2arc_ndev);
4939 mutex_exit(&l2arc_dev_mtx);
4942 * Clear all buflists and ARC references. L2ARC device flush.
4944 l2arc_evict(remdev, 0, B_TRUE);
4945 list_destroy(remdev->l2ad_buflist);
4946 kmem_free(remdev->l2ad_buflist, sizeof (list_t));
4947 kmem_free(remdev, sizeof (l2arc_dev_t));
4953 l2arc_thread_exit = 0;
4955 l2arc_writes_sent = 0;
4956 l2arc_writes_done = 0;
4958 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
4959 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
4960 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
4961 mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
4962 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
4964 l2arc_dev_list = &L2ARC_dev_list;
4965 l2arc_free_on_write = &L2ARC_free_on_write;
4966 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
4967 offsetof(l2arc_dev_t, l2ad_node));
4968 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
4969 offsetof(l2arc_data_free_t, l2df_list_node));
4976 * This is called from dmu_fini(), which is called from spa_fini();
4977 * Because of this, we can assume that all l2arc devices have
4978 * already been removed when the pools themselves were removed.
4981 l2arc_do_free_on_write();
4983 mutex_destroy(&l2arc_feed_thr_lock);
4984 cv_destroy(&l2arc_feed_thr_cv);
4985 mutex_destroy(&l2arc_dev_mtx);
4986 mutex_destroy(&l2arc_buflist_mtx);
4987 mutex_destroy(&l2arc_free_on_write_mtx);
4989 list_destroy(l2arc_dev_list);
4990 list_destroy(l2arc_free_on_write);
4996 if (!(spa_mode_global & FWRITE))
4999 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
5000 TS_RUN, minclsyspri);
5006 if (!(spa_mode_global & FWRITE))
5009 mutex_enter(&l2arc_feed_thr_lock);
5010 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
5011 l2arc_thread_exit = 1;
5012 while (l2arc_thread_exit != 0)
5013 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
5014 mutex_exit(&l2arc_feed_thr_lock);
5017 #if defined(_KERNEL) && defined(HAVE_SPL)
5018 EXPORT_SYMBOL(arc_read);
5019 EXPORT_SYMBOL(arc_buf_remove_ref);
5020 EXPORT_SYMBOL(arc_getbuf_func);
5021 EXPORT_SYMBOL(arc_add_prune_callback);
5022 EXPORT_SYMBOL(arc_remove_prune_callback);
5024 module_param(zfs_arc_min, ulong, 0444);
5025 MODULE_PARM_DESC(zfs_arc_min, "Min arc size");
5027 module_param(zfs_arc_max, ulong, 0444);
5028 MODULE_PARM_DESC(zfs_arc_max, "Max arc size");
5030 module_param(zfs_arc_meta_limit, ulong, 0444);
5031 MODULE_PARM_DESC(zfs_arc_meta_limit, "Meta limit for arc size");
5033 module_param(zfs_arc_meta_prune, int, 0444);
5034 MODULE_PARM_DESC(zfs_arc_meta_prune, "Bytes of meta data to prune");
5036 module_param(zfs_arc_grow_retry, int, 0444);
5037 MODULE_PARM_DESC(zfs_arc_grow_retry, "Seconds before growing arc size");
5039 module_param(zfs_arc_shrink_shift, int, 0444);
5040 MODULE_PARM_DESC(zfs_arc_shrink_shift, "log2(fraction of arc to reclaim)");
5042 module_param(zfs_arc_p_min_shift, int, 0444);
5043 MODULE_PARM_DESC(zfs_arc_p_min_shift, "arc_c shift to calc min/max arc_p");
5045 module_param(zfs_disable_dup_eviction, int, 0644);
5046 MODULE_PARM_DESC(zfs_disable_dup_eviction, "disable duplicate buffer eviction");
5048 module_param(l2arc_write_max, ulong, 0444);
5049 MODULE_PARM_DESC(l2arc_write_max, "Max write bytes per interval");
5051 module_param(l2arc_write_boost, ulong, 0444);
5052 MODULE_PARM_DESC(l2arc_write_boost, "Extra write bytes during device warmup");
5054 module_param(l2arc_headroom, ulong, 0444);
5055 MODULE_PARM_DESC(l2arc_headroom, "Number of max device writes to precache");
5057 module_param(l2arc_feed_secs, ulong, 0444);
5058 MODULE_PARM_DESC(l2arc_feed_secs, "Seconds between L2ARC writing");
5060 module_param(l2arc_feed_min_ms, ulong, 0444);
5061 MODULE_PARM_DESC(l2arc_feed_min_ms, "Min feed interval in milliseconds");
5063 module_param(l2arc_noprefetch, int, 0444);
5064 MODULE_PARM_DESC(l2arc_noprefetch, "Skip caching prefetched buffers");
5066 module_param(l2arc_feed_again, int, 0444);
5067 MODULE_PARM_DESC(l2arc_feed_again, "Turbo L2ARC warmup");
5069 module_param(l2arc_norw, int, 0444);
5070 MODULE_PARM_DESC(l2arc_norw, "No reads during writes");