4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 * DVA-based Adjustable Replacement Cache
28 * While much of the theory of operation used here is
29 * based on the self-tuning, low overhead replacement cache
30 * presented by Megiddo and Modha at FAST 2003, there are some
31 * significant differences:
33 * 1. The Megiddo and Modha model assumes any page is evictable.
34 * Pages in its cache cannot be "locked" into memory. This makes
35 * the eviction algorithm simple: evict the last page in the list.
36 * This also make the performance characteristics easy to reason
37 * about. Our cache is not so simple. At any given moment, some
38 * subset of the blocks in the cache are un-evictable because we
39 * have handed out a reference to them. Blocks are only evictable
40 * when there are no external references active. This makes
41 * eviction far more problematic: we choose to evict the evictable
42 * blocks that are the "lowest" in the list.
44 * There are times when it is not possible to evict the requested
45 * space. In these circumstances we are unable to adjust the cache
46 * size. To prevent the cache growing unbounded at these times we
47 * implement a "cache throttle" that slows the flow of new data
48 * into the cache until we can make space available.
50 * 2. The Megiddo and Modha model assumes a fixed cache size.
51 * Pages are evicted when the cache is full and there is a cache
52 * miss. Our model has a variable sized cache. It grows with
53 * high use, but also tries to react to memory pressure from the
54 * operating system: decreasing its size when system memory is
57 * 3. The Megiddo and Modha model assumes a fixed page size. All
58 * elements of the cache are therefor exactly the same size. So
59 * when adjusting the cache size following a cache miss, its simply
60 * a matter of choosing a single page to evict. In our model, we
61 * have variable sized cache blocks (rangeing from 512 bytes to
62 * 128K bytes). We therefor choose a set of blocks to evict to make
63 * space for a cache miss that approximates as closely as possible
64 * the space used by the new block.
66 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
67 * by N. Megiddo & D. Modha, FAST 2003
73 * A new reference to a cache buffer can be obtained in two
74 * ways: 1) via a hash table lookup using the DVA as a key,
75 * or 2) via one of the ARC lists. The arc_read() interface
76 * uses method 1, while the internal arc algorithms for
77 * adjusting the cache use method 2. We therefor provide two
78 * types of locks: 1) the hash table lock array, and 2) the
81 * Buffers do not have their own mutexs, rather they rely on the
82 * hash table mutexs for the bulk of their protection (i.e. most
83 * fields in the arc_buf_hdr_t are protected by these mutexs).
85 * buf_hash_find() returns the appropriate mutex (held) when it
86 * locates the requested buffer in the hash table. It returns
87 * NULL for the mutex if the buffer was not in the table.
89 * buf_hash_remove() expects the appropriate hash mutex to be
90 * already held before it is invoked.
92 * Each arc state also has a mutex which is used to protect the
93 * buffer list associated with the state. When attempting to
94 * obtain a hash table lock while holding an arc list lock you
95 * must use: mutex_tryenter() to avoid deadlock. Also note that
96 * the active state mutex must be held before the ghost state mutex.
98 * Arc buffers may have an associated eviction callback function.
99 * This function will be invoked prior to removing the buffer (e.g.
100 * in arc_do_user_evicts()). Note however that the data associated
101 * with the buffer may be evicted prior to the callback. The callback
102 * must be made with *no locks held* (to prevent deadlock). Additionally,
103 * the users of callbacks must ensure that their private data is
104 * protected from simultaneous callbacks from arc_buf_evict()
105 * and arc_do_user_evicts().
107 * It as also possible to register a callback which is run when the
108 * arc_meta_limit is reached and no buffers can be safely evicted. In
109 * this case the arc user should drop a reference on some arc buffers so
110 * they can be reclaimed and the arc_meta_limit honored. For example,
111 * when using the ZPL each dentry holds a references on a znode. These
112 * dentries must be pruned before the arc buffer holding the znode can
115 * Note that the majority of the performance stats are manipulated
116 * with atomic operations.
118 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
120 * - L2ARC buflist creation
121 * - L2ARC buflist eviction
122 * - L2ARC write completion, which walks L2ARC buflists
123 * - ARC header destruction, as it removes from L2ARC buflists
124 * - ARC header release, as it removes from L2ARC buflists
129 #include <sys/zfs_context.h>
131 #include <sys/vdev.h>
132 #include <sys/vdev_impl.h>
134 #include <sys/vmsystm.h>
136 #include <sys/fs/swapnode.h>
139 #include <sys/callb.h>
140 #include <sys/kstat.h>
141 #include <zfs_fletcher.h>
143 static kmutex_t arc_reclaim_thr_lock;
144 static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */
145 static uint8_t arc_thread_exit;
147 extern int zfs_write_limit_shift;
148 extern uint64_t zfs_write_limit_max;
149 extern kmutex_t zfs_write_limit_lock;
151 /* number of bytes to prune from caches when at arc_meta_limit is reached */
152 uint_t arc_meta_prune = 1048576;
154 typedef enum arc_reclaim_strategy {
155 ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */
156 ARC_RECLAIM_CONS /* Conservative reclaim strategy */
157 } arc_reclaim_strategy_t;
159 /* number of seconds before growing cache again */
160 static int arc_grow_retry = 60;
162 /* shift of arc_c for calculating both min and max arc_p */
163 static int arc_p_min_shift = 4;
165 /* log2(fraction of arc to reclaim) */
166 static int arc_shrink_shift = 5;
169 * minimum lifespan of a prefetch block in clock ticks
170 * (initialized in arc_init())
172 static int arc_min_prefetch_lifespan;
177 * The arc has filled available memory and has now warmed up.
179 static boolean_t arc_warm;
182 * These tunables are for performance analysis.
184 unsigned long zfs_arc_max = 0;
185 unsigned long zfs_arc_min = 0;
186 unsigned long zfs_arc_meta_limit = 0;
187 int zfs_arc_grow_retry = 0;
188 int zfs_arc_shrink_shift = 0;
189 int zfs_arc_p_min_shift = 0;
190 int zfs_arc_meta_prune = 0;
193 * Note that buffers can be in one of 6 states:
194 * ARC_anon - anonymous (discussed below)
195 * ARC_mru - recently used, currently cached
196 * ARC_mru_ghost - recentely used, no longer in cache
197 * ARC_mfu - frequently used, currently cached
198 * ARC_mfu_ghost - frequently used, no longer in cache
199 * ARC_l2c_only - exists in L2ARC but not other states
200 * When there are no active references to the buffer, they are
201 * are linked onto a list in one of these arc states. These are
202 * the only buffers that can be evicted or deleted. Within each
203 * state there are multiple lists, one for meta-data and one for
204 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
205 * etc.) is tracked separately so that it can be managed more
206 * explicitly: favored over data, limited explicitly.
208 * Anonymous buffers are buffers that are not associated with
209 * a DVA. These are buffers that hold dirty block copies
210 * before they are written to stable storage. By definition,
211 * they are "ref'd" and are considered part of arc_mru
212 * that cannot be freed. Generally, they will aquire a DVA
213 * as they are written and migrate onto the arc_mru list.
215 * The ARC_l2c_only state is for buffers that are in the second
216 * level ARC but no longer in any of the ARC_m* lists. The second
217 * level ARC itself may also contain buffers that are in any of
218 * the ARC_m* states - meaning that a buffer can exist in two
219 * places. The reason for the ARC_l2c_only state is to keep the
220 * buffer header in the hash table, so that reads that hit the
221 * second level ARC benefit from these fast lookups.
224 typedef struct arc_state {
225 list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */
226 uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */
227 uint64_t arcs_size; /* total amount of data in this state */
232 static arc_state_t ARC_anon;
233 static arc_state_t ARC_mru;
234 static arc_state_t ARC_mru_ghost;
235 static arc_state_t ARC_mfu;
236 static arc_state_t ARC_mfu_ghost;
237 static arc_state_t ARC_l2c_only;
239 typedef struct arc_stats {
240 kstat_named_t arcstat_hits;
241 kstat_named_t arcstat_misses;
242 kstat_named_t arcstat_demand_data_hits;
243 kstat_named_t arcstat_demand_data_misses;
244 kstat_named_t arcstat_demand_metadata_hits;
245 kstat_named_t arcstat_demand_metadata_misses;
246 kstat_named_t arcstat_prefetch_data_hits;
247 kstat_named_t arcstat_prefetch_data_misses;
248 kstat_named_t arcstat_prefetch_metadata_hits;
249 kstat_named_t arcstat_prefetch_metadata_misses;
250 kstat_named_t arcstat_mru_hits;
251 kstat_named_t arcstat_mru_ghost_hits;
252 kstat_named_t arcstat_mfu_hits;
253 kstat_named_t arcstat_mfu_ghost_hits;
254 kstat_named_t arcstat_deleted;
255 kstat_named_t arcstat_recycle_miss;
256 kstat_named_t arcstat_mutex_miss;
257 kstat_named_t arcstat_evict_skip;
258 kstat_named_t arcstat_evict_l2_cached;
259 kstat_named_t arcstat_evict_l2_eligible;
260 kstat_named_t arcstat_evict_l2_ineligible;
261 kstat_named_t arcstat_hash_elements;
262 kstat_named_t arcstat_hash_elements_max;
263 kstat_named_t arcstat_hash_collisions;
264 kstat_named_t arcstat_hash_chains;
265 kstat_named_t arcstat_hash_chain_max;
266 kstat_named_t arcstat_p;
267 kstat_named_t arcstat_c;
268 kstat_named_t arcstat_c_min;
269 kstat_named_t arcstat_c_max;
270 kstat_named_t arcstat_size;
271 kstat_named_t arcstat_hdr_size;
272 kstat_named_t arcstat_data_size;
273 kstat_named_t arcstat_other_size;
274 kstat_named_t arcstat_l2_hits;
275 kstat_named_t arcstat_l2_misses;
276 kstat_named_t arcstat_l2_feeds;
277 kstat_named_t arcstat_l2_rw_clash;
278 kstat_named_t arcstat_l2_read_bytes;
279 kstat_named_t arcstat_l2_write_bytes;
280 kstat_named_t arcstat_l2_writes_sent;
281 kstat_named_t arcstat_l2_writes_done;
282 kstat_named_t arcstat_l2_writes_error;
283 kstat_named_t arcstat_l2_writes_hdr_miss;
284 kstat_named_t arcstat_l2_evict_lock_retry;
285 kstat_named_t arcstat_l2_evict_reading;
286 kstat_named_t arcstat_l2_free_on_write;
287 kstat_named_t arcstat_l2_abort_lowmem;
288 kstat_named_t arcstat_l2_cksum_bad;
289 kstat_named_t arcstat_l2_io_error;
290 kstat_named_t arcstat_l2_size;
291 kstat_named_t arcstat_l2_hdr_size;
292 kstat_named_t arcstat_memory_throttle_count;
293 kstat_named_t arcstat_memory_direct_count;
294 kstat_named_t arcstat_memory_indirect_count;
295 kstat_named_t arcstat_no_grow;
296 kstat_named_t arcstat_tempreserve;
297 kstat_named_t arcstat_loaned_bytes;
298 kstat_named_t arcstat_prune;
299 kstat_named_t arcstat_meta_used;
300 kstat_named_t arcstat_meta_limit;
301 kstat_named_t arcstat_meta_max;
304 static arc_stats_t arc_stats = {
305 { "hits", KSTAT_DATA_UINT64 },
306 { "misses", KSTAT_DATA_UINT64 },
307 { "demand_data_hits", KSTAT_DATA_UINT64 },
308 { "demand_data_misses", KSTAT_DATA_UINT64 },
309 { "demand_metadata_hits", KSTAT_DATA_UINT64 },
310 { "demand_metadata_misses", KSTAT_DATA_UINT64 },
311 { "prefetch_data_hits", KSTAT_DATA_UINT64 },
312 { "prefetch_data_misses", KSTAT_DATA_UINT64 },
313 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
314 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
315 { "mru_hits", KSTAT_DATA_UINT64 },
316 { "mru_ghost_hits", KSTAT_DATA_UINT64 },
317 { "mfu_hits", KSTAT_DATA_UINT64 },
318 { "mfu_ghost_hits", KSTAT_DATA_UINT64 },
319 { "deleted", KSTAT_DATA_UINT64 },
320 { "recycle_miss", KSTAT_DATA_UINT64 },
321 { "mutex_miss", KSTAT_DATA_UINT64 },
322 { "evict_skip", KSTAT_DATA_UINT64 },
323 { "evict_l2_cached", KSTAT_DATA_UINT64 },
324 { "evict_l2_eligible", KSTAT_DATA_UINT64 },
325 { "evict_l2_ineligible", KSTAT_DATA_UINT64 },
326 { "hash_elements", KSTAT_DATA_UINT64 },
327 { "hash_elements_max", KSTAT_DATA_UINT64 },
328 { "hash_collisions", KSTAT_DATA_UINT64 },
329 { "hash_chains", KSTAT_DATA_UINT64 },
330 { "hash_chain_max", KSTAT_DATA_UINT64 },
331 { "p", KSTAT_DATA_UINT64 },
332 { "c", KSTAT_DATA_UINT64 },
333 { "c_min", KSTAT_DATA_UINT64 },
334 { "c_max", KSTAT_DATA_UINT64 },
335 { "size", KSTAT_DATA_UINT64 },
336 { "hdr_size", KSTAT_DATA_UINT64 },
337 { "data_size", KSTAT_DATA_UINT64 },
338 { "other_size", KSTAT_DATA_UINT64 },
339 { "l2_hits", KSTAT_DATA_UINT64 },
340 { "l2_misses", KSTAT_DATA_UINT64 },
341 { "l2_feeds", KSTAT_DATA_UINT64 },
342 { "l2_rw_clash", KSTAT_DATA_UINT64 },
343 { "l2_read_bytes", KSTAT_DATA_UINT64 },
344 { "l2_write_bytes", KSTAT_DATA_UINT64 },
345 { "l2_writes_sent", KSTAT_DATA_UINT64 },
346 { "l2_writes_done", KSTAT_DATA_UINT64 },
347 { "l2_writes_error", KSTAT_DATA_UINT64 },
348 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 },
349 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
350 { "l2_evict_reading", KSTAT_DATA_UINT64 },
351 { "l2_free_on_write", KSTAT_DATA_UINT64 },
352 { "l2_abort_lowmem", KSTAT_DATA_UINT64 },
353 { "l2_cksum_bad", KSTAT_DATA_UINT64 },
354 { "l2_io_error", KSTAT_DATA_UINT64 },
355 { "l2_size", KSTAT_DATA_UINT64 },
356 { "l2_hdr_size", KSTAT_DATA_UINT64 },
357 { "memory_throttle_count", KSTAT_DATA_UINT64 },
358 { "memory_direct_count", KSTAT_DATA_UINT64 },
359 { "memory_indirect_count", KSTAT_DATA_UINT64 },
360 { "arc_no_grow", KSTAT_DATA_UINT64 },
361 { "arc_tempreserve", KSTAT_DATA_UINT64 },
362 { "arc_loaned_bytes", KSTAT_DATA_UINT64 },
363 { "arc_prune", KSTAT_DATA_UINT64 },
364 { "arc_meta_used", KSTAT_DATA_UINT64 },
365 { "arc_meta_limit", KSTAT_DATA_UINT64 },
366 { "arc_meta_max", KSTAT_DATA_UINT64 },
369 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
371 #define ARCSTAT_INCR(stat, val) \
372 atomic_add_64(&arc_stats.stat.value.ui64, (val));
374 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
375 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
377 #define ARCSTAT_MAX(stat, val) { \
379 while ((val) > (m = arc_stats.stat.value.ui64) && \
380 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
384 #define ARCSTAT_MAXSTAT(stat) \
385 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
388 * We define a macro to allow ARC hits/misses to be easily broken down by
389 * two separate conditions, giving a total of four different subtypes for
390 * each of hits and misses (so eight statistics total).
392 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
395 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
397 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
401 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
403 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
408 static arc_state_t *arc_anon;
409 static arc_state_t *arc_mru;
410 static arc_state_t *arc_mru_ghost;
411 static arc_state_t *arc_mfu;
412 static arc_state_t *arc_mfu_ghost;
413 static arc_state_t *arc_l2c_only;
416 * There are several ARC variables that are critical to export as kstats --
417 * but we don't want to have to grovel around in the kstat whenever we wish to
418 * manipulate them. For these variables, we therefore define them to be in
419 * terms of the statistic variable. This assures that we are not introducing
420 * the possibility of inconsistency by having shadow copies of the variables,
421 * while still allowing the code to be readable.
423 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
424 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
425 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
426 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
427 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
428 #define arc_no_grow ARCSTAT(arcstat_no_grow)
429 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
430 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
431 #define arc_meta_used ARCSTAT(arcstat_meta_used)
432 #define arc_meta_limit ARCSTAT(arcstat_meta_limit)
433 #define arc_meta_max ARCSTAT(arcstat_meta_max)
435 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
437 typedef struct arc_callback arc_callback_t;
439 struct arc_callback {
441 arc_done_func_t *acb_done;
443 zio_t *acb_zio_dummy;
444 arc_callback_t *acb_next;
447 typedef struct arc_write_callback arc_write_callback_t;
449 struct arc_write_callback {
451 arc_done_func_t *awcb_ready;
452 arc_done_func_t *awcb_done;
457 /* protected by hash lock */
462 kmutex_t b_freeze_lock;
463 zio_cksum_t *b_freeze_cksum;
466 arc_buf_hdr_t *b_hash_next;
471 arc_callback_t *b_acb;
475 arc_buf_contents_t b_type;
479 /* protected by arc state mutex */
480 arc_state_t *b_state;
481 list_node_t b_arc_node;
483 /* updated atomically */
484 clock_t b_arc_access;
486 /* self protecting */
489 l2arc_buf_hdr_t *b_l2hdr;
490 list_node_t b_l2node;
493 static list_t arc_prune_list;
494 static kmutex_t arc_prune_mtx;
495 static arc_buf_t *arc_eviction_list;
496 static kmutex_t arc_eviction_mtx;
497 static arc_buf_hdr_t arc_eviction_hdr;
498 static void arc_get_data_buf(arc_buf_t *buf);
499 static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
500 static int arc_evict_needed(arc_buf_contents_t type);
501 static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
503 static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
505 #define GHOST_STATE(state) \
506 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
507 (state) == arc_l2c_only)
510 * Private ARC flags. These flags are private ARC only flags that will show up
511 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
512 * be passed in as arc_flags in things like arc_read. However, these flags
513 * should never be passed and should only be set by ARC code. When adding new
514 * public flags, make sure not to smash the private ones.
517 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
518 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
519 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
520 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
521 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
522 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
523 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
524 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
525 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
526 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
528 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
529 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
530 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
531 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
532 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
533 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
534 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
535 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
536 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
537 (hdr)->b_l2hdr != NULL)
538 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
539 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
540 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
546 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
547 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
550 * Hash table routines
553 #define HT_LOCK_ALIGN 64
554 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
559 unsigned char pad[HT_LOCK_PAD];
563 #define BUF_LOCKS 256
564 typedef struct buf_hash_table {
566 arc_buf_hdr_t **ht_table;
567 struct ht_lock ht_locks[BUF_LOCKS];
570 static buf_hash_table_t buf_hash_table;
572 #define BUF_HASH_INDEX(spa, dva, birth) \
573 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
574 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
575 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
576 #define HDR_LOCK(hdr) \
577 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
579 uint64_t zfs_crc64_table[256];
585 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
586 #define L2ARC_HEADROOM 2 /* num of writes */
587 #define L2ARC_FEED_SECS 1 /* caching interval secs */
588 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
590 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
591 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
594 * L2ARC Performance Tunables
596 unsigned long l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */
597 unsigned long l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */
598 unsigned long l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */
599 unsigned long l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */
600 unsigned long l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */
601 int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */
602 int l2arc_feed_again = B_TRUE; /* turbo warmup */
603 int l2arc_norw = B_TRUE; /* no reads during writes */
608 typedef struct l2arc_dev {
609 vdev_t *l2ad_vdev; /* vdev */
610 spa_t *l2ad_spa; /* spa */
611 uint64_t l2ad_hand; /* next write location */
612 uint64_t l2ad_write; /* desired write size, bytes */
613 uint64_t l2ad_boost; /* warmup write boost, bytes */
614 uint64_t l2ad_start; /* first addr on device */
615 uint64_t l2ad_end; /* last addr on device */
616 uint64_t l2ad_evict; /* last addr eviction reached */
617 boolean_t l2ad_first; /* first sweep through */
618 boolean_t l2ad_writing; /* currently writing */
619 list_t *l2ad_buflist; /* buffer list */
620 list_node_t l2ad_node; /* device list node */
623 static list_t L2ARC_dev_list; /* device list */
624 static list_t *l2arc_dev_list; /* device list pointer */
625 static kmutex_t l2arc_dev_mtx; /* device list mutex */
626 static l2arc_dev_t *l2arc_dev_last; /* last device used */
627 static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */
628 static list_t L2ARC_free_on_write; /* free after write buf list */
629 static list_t *l2arc_free_on_write; /* free after write list ptr */
630 static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */
631 static uint64_t l2arc_ndev; /* number of devices */
633 typedef struct l2arc_read_callback {
634 arc_buf_t *l2rcb_buf; /* read buffer */
635 spa_t *l2rcb_spa; /* spa */
636 blkptr_t l2rcb_bp; /* original blkptr */
637 zbookmark_t l2rcb_zb; /* original bookmark */
638 int l2rcb_flags; /* original flags */
639 } l2arc_read_callback_t;
641 typedef struct l2arc_write_callback {
642 l2arc_dev_t *l2wcb_dev; /* device info */
643 arc_buf_hdr_t *l2wcb_head; /* head of write buflist */
644 } l2arc_write_callback_t;
646 struct l2arc_buf_hdr {
647 /* protected by arc_buf_hdr mutex */
648 l2arc_dev_t *b_dev; /* L2ARC device */
649 uint64_t b_daddr; /* disk address, offset byte */
652 typedef struct l2arc_data_free {
653 /* protected by l2arc_free_on_write_mtx */
656 void (*l2df_func)(void *, size_t);
657 list_node_t l2df_list_node;
660 static kmutex_t l2arc_feed_thr_lock;
661 static kcondvar_t l2arc_feed_thr_cv;
662 static uint8_t l2arc_thread_exit;
664 static void l2arc_read_done(zio_t *zio);
665 static void l2arc_hdr_stat_add(void);
666 static void l2arc_hdr_stat_remove(void);
669 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
671 uint8_t *vdva = (uint8_t *)dva;
672 uint64_t crc = -1ULL;
675 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
677 for (i = 0; i < sizeof (dva_t); i++)
678 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
680 crc ^= (spa>>8) ^ birth;
685 #define BUF_EMPTY(buf) \
686 ((buf)->b_dva.dva_word[0] == 0 && \
687 (buf)->b_dva.dva_word[1] == 0 && \
690 #define BUF_EQUAL(spa, dva, birth, buf) \
691 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
692 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
693 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
696 buf_discard_identity(arc_buf_hdr_t *hdr)
698 hdr->b_dva.dva_word[0] = 0;
699 hdr->b_dva.dva_word[1] = 0;
704 static arc_buf_hdr_t *
705 buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp)
707 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
708 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
711 mutex_enter(hash_lock);
712 for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
713 buf = buf->b_hash_next) {
714 if (BUF_EQUAL(spa, dva, birth, buf)) {
719 mutex_exit(hash_lock);
725 * Insert an entry into the hash table. If there is already an element
726 * equal to elem in the hash table, then the already existing element
727 * will be returned and the new element will not be inserted.
728 * Otherwise returns NULL.
730 static arc_buf_hdr_t *
731 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
733 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
734 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
738 ASSERT(!HDR_IN_HASH_TABLE(buf));
740 mutex_enter(hash_lock);
741 for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
742 fbuf = fbuf->b_hash_next, i++) {
743 if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
747 buf->b_hash_next = buf_hash_table.ht_table[idx];
748 buf_hash_table.ht_table[idx] = buf;
749 buf->b_flags |= ARC_IN_HASH_TABLE;
751 /* collect some hash table performance data */
753 ARCSTAT_BUMP(arcstat_hash_collisions);
755 ARCSTAT_BUMP(arcstat_hash_chains);
757 ARCSTAT_MAX(arcstat_hash_chain_max, i);
760 ARCSTAT_BUMP(arcstat_hash_elements);
761 ARCSTAT_MAXSTAT(arcstat_hash_elements);
767 buf_hash_remove(arc_buf_hdr_t *buf)
769 arc_buf_hdr_t *fbuf, **bufp;
770 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
772 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
773 ASSERT(HDR_IN_HASH_TABLE(buf));
775 bufp = &buf_hash_table.ht_table[idx];
776 while ((fbuf = *bufp) != buf) {
777 ASSERT(fbuf != NULL);
778 bufp = &fbuf->b_hash_next;
780 *bufp = buf->b_hash_next;
781 buf->b_hash_next = NULL;
782 buf->b_flags &= ~ARC_IN_HASH_TABLE;
784 /* collect some hash table performance data */
785 ARCSTAT_BUMPDOWN(arcstat_hash_elements);
787 if (buf_hash_table.ht_table[idx] &&
788 buf_hash_table.ht_table[idx]->b_hash_next == NULL)
789 ARCSTAT_BUMPDOWN(arcstat_hash_chains);
793 * Global data structures and functions for the buf kmem cache.
795 static kmem_cache_t *hdr_cache;
796 static kmem_cache_t *buf_cache;
803 #if defined(_KERNEL) && defined(HAVE_SPL)
804 /* Large allocations which do not require contiguous pages
805 * should be using vmem_free() in the linux kernel */
806 vmem_free(buf_hash_table.ht_table,
807 (buf_hash_table.ht_mask + 1) * sizeof (void *));
809 kmem_free(buf_hash_table.ht_table,
810 (buf_hash_table.ht_mask + 1) * sizeof (void *));
812 for (i = 0; i < BUF_LOCKS; i++)
813 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
814 kmem_cache_destroy(hdr_cache);
815 kmem_cache_destroy(buf_cache);
819 * Constructor callback - called when the cache is empty
820 * and a new buf is requested.
824 hdr_cons(void *vbuf, void *unused, int kmflag)
826 arc_buf_hdr_t *buf = vbuf;
828 bzero(buf, sizeof (arc_buf_hdr_t));
829 refcount_create(&buf->b_refcnt);
830 cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
831 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
832 list_link_init(&buf->b_arc_node);
833 list_link_init(&buf->b_l2node);
834 arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
841 buf_cons(void *vbuf, void *unused, int kmflag)
843 arc_buf_t *buf = vbuf;
845 bzero(buf, sizeof (arc_buf_t));
846 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
847 rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL);
848 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
854 * Destructor callback - called when a cached buf is
855 * no longer required.
859 hdr_dest(void *vbuf, void *unused)
861 arc_buf_hdr_t *buf = vbuf;
863 ASSERT(BUF_EMPTY(buf));
864 refcount_destroy(&buf->b_refcnt);
865 cv_destroy(&buf->b_cv);
866 mutex_destroy(&buf->b_freeze_lock);
867 arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
872 buf_dest(void *vbuf, void *unused)
874 arc_buf_t *buf = vbuf;
876 mutex_destroy(&buf->b_evict_lock);
877 rw_destroy(&buf->b_data_lock);
878 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
882 * Reclaim callback -- invoked when memory is low.
886 hdr_recl(void *unused)
888 dprintf("hdr_recl called\n");
890 * umem calls the reclaim func when we destroy the buf cache,
891 * which is after we do arc_fini().
894 cv_signal(&arc_reclaim_thr_cv);
901 uint64_t hsize = 1ULL << 12;
905 * The hash table is big enough to fill all of physical memory
906 * with an average 64K block size. The table will take up
907 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
909 while (hsize * 65536 < physmem * PAGESIZE)
912 buf_hash_table.ht_mask = hsize - 1;
913 #if defined(_KERNEL) && defined(HAVE_SPL)
914 /* Large allocations which do not require contiguous pages
915 * should be using vmem_alloc() in the linux kernel */
916 buf_hash_table.ht_table =
917 vmem_zalloc(hsize * sizeof (void*), KM_SLEEP);
919 buf_hash_table.ht_table =
920 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
922 if (buf_hash_table.ht_table == NULL) {
923 ASSERT(hsize > (1ULL << 8));
928 hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
929 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
930 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
931 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
933 for (i = 0; i < 256; i++)
934 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
935 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
937 for (i = 0; i < BUF_LOCKS; i++) {
938 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
939 NULL, MUTEX_DEFAULT, NULL);
943 #define ARC_MINTIME (hz>>4) /* 62 ms */
946 arc_cksum_verify(arc_buf_t *buf)
950 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
953 mutex_enter(&buf->b_hdr->b_freeze_lock);
954 if (buf->b_hdr->b_freeze_cksum == NULL ||
955 (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
956 mutex_exit(&buf->b_hdr->b_freeze_lock);
959 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
960 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
961 panic("buffer modified while frozen!");
962 mutex_exit(&buf->b_hdr->b_freeze_lock);
966 arc_cksum_equal(arc_buf_t *buf)
971 mutex_enter(&buf->b_hdr->b_freeze_lock);
972 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
973 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
974 mutex_exit(&buf->b_hdr->b_freeze_lock);
980 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
982 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
985 mutex_enter(&buf->b_hdr->b_freeze_lock);
986 if (buf->b_hdr->b_freeze_cksum != NULL) {
987 mutex_exit(&buf->b_hdr->b_freeze_lock);
990 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
991 fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
992 buf->b_hdr->b_freeze_cksum);
993 mutex_exit(&buf->b_hdr->b_freeze_lock);
997 arc_buf_thaw(arc_buf_t *buf)
999 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1000 if (buf->b_hdr->b_state != arc_anon)
1001 panic("modifying non-anon buffer!");
1002 if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
1003 panic("modifying buffer while i/o in progress!");
1004 arc_cksum_verify(buf);
1007 mutex_enter(&buf->b_hdr->b_freeze_lock);
1008 if (buf->b_hdr->b_freeze_cksum != NULL) {
1009 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1010 buf->b_hdr->b_freeze_cksum = NULL;
1013 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1014 if (buf->b_hdr->b_thawed)
1015 kmem_free(buf->b_hdr->b_thawed, 1);
1016 buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
1019 mutex_exit(&buf->b_hdr->b_freeze_lock);
1023 arc_buf_freeze(arc_buf_t *buf)
1025 kmutex_t *hash_lock;
1027 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1030 hash_lock = HDR_LOCK(buf->b_hdr);
1031 mutex_enter(hash_lock);
1033 ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
1034 buf->b_hdr->b_state == arc_anon);
1035 arc_cksum_compute(buf, B_FALSE);
1036 mutex_exit(hash_lock);
1040 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1042 ASSERT(MUTEX_HELD(hash_lock));
1044 if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1045 (ab->b_state != arc_anon)) {
1046 uint64_t delta = ab->b_size * ab->b_datacnt;
1047 list_t *list = &ab->b_state->arcs_list[ab->b_type];
1048 uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1050 ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
1051 mutex_enter(&ab->b_state->arcs_mtx);
1052 ASSERT(list_link_active(&ab->b_arc_node));
1053 list_remove(list, ab);
1054 if (GHOST_STATE(ab->b_state)) {
1055 ASSERT3U(ab->b_datacnt, ==, 0);
1056 ASSERT3P(ab->b_buf, ==, NULL);
1060 ASSERT3U(*size, >=, delta);
1061 atomic_add_64(size, -delta);
1062 mutex_exit(&ab->b_state->arcs_mtx);
1063 /* remove the prefetch flag if we get a reference */
1064 if (ab->b_flags & ARC_PREFETCH)
1065 ab->b_flags &= ~ARC_PREFETCH;
1070 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1073 arc_state_t *state = ab->b_state;
1075 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1076 ASSERT(!GHOST_STATE(state));
1078 if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1079 (state != arc_anon)) {
1080 uint64_t *size = &state->arcs_lsize[ab->b_type];
1082 ASSERT(!MUTEX_HELD(&state->arcs_mtx));
1083 mutex_enter(&state->arcs_mtx);
1084 ASSERT(!list_link_active(&ab->b_arc_node));
1085 list_insert_head(&state->arcs_list[ab->b_type], ab);
1086 ASSERT(ab->b_datacnt > 0);
1087 atomic_add_64(size, ab->b_size * ab->b_datacnt);
1088 mutex_exit(&state->arcs_mtx);
1094 * Move the supplied buffer to the indicated state. The mutex
1095 * for the buffer must be held by the caller.
1098 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1100 arc_state_t *old_state = ab->b_state;
1101 int64_t refcnt = refcount_count(&ab->b_refcnt);
1102 uint64_t from_delta, to_delta;
1104 ASSERT(MUTEX_HELD(hash_lock));
1105 ASSERT(new_state != old_state);
1106 ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1107 ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1108 ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1110 from_delta = to_delta = ab->b_datacnt * ab->b_size;
1113 * If this buffer is evictable, transfer it from the
1114 * old state list to the new state list.
1117 if (old_state != arc_anon) {
1118 int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
1119 uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1122 mutex_enter(&old_state->arcs_mtx);
1124 ASSERT(list_link_active(&ab->b_arc_node));
1125 list_remove(&old_state->arcs_list[ab->b_type], ab);
1128 * If prefetching out of the ghost cache,
1129 * we will have a non-zero datacnt.
1131 if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1132 /* ghost elements have a ghost size */
1133 ASSERT(ab->b_buf == NULL);
1134 from_delta = ab->b_size;
1136 ASSERT3U(*size, >=, from_delta);
1137 atomic_add_64(size, -from_delta);
1140 mutex_exit(&old_state->arcs_mtx);
1142 if (new_state != arc_anon) {
1143 int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
1144 uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1147 mutex_enter(&new_state->arcs_mtx);
1149 list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1151 /* ghost elements have a ghost size */
1152 if (GHOST_STATE(new_state)) {
1153 ASSERT(ab->b_datacnt == 0);
1154 ASSERT(ab->b_buf == NULL);
1155 to_delta = ab->b_size;
1157 atomic_add_64(size, to_delta);
1160 mutex_exit(&new_state->arcs_mtx);
1164 ASSERT(!BUF_EMPTY(ab));
1165 if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1166 buf_hash_remove(ab);
1168 /* adjust state sizes */
1170 atomic_add_64(&new_state->arcs_size, to_delta);
1172 ASSERT3U(old_state->arcs_size, >=, from_delta);
1173 atomic_add_64(&old_state->arcs_size, -from_delta);
1175 ab->b_state = new_state;
1177 /* adjust l2arc hdr stats */
1178 if (new_state == arc_l2c_only)
1179 l2arc_hdr_stat_add();
1180 else if (old_state == arc_l2c_only)
1181 l2arc_hdr_stat_remove();
1185 arc_space_consume(uint64_t space, arc_space_type_t type)
1187 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1192 case ARC_SPACE_DATA:
1193 ARCSTAT_INCR(arcstat_data_size, space);
1195 case ARC_SPACE_OTHER:
1196 ARCSTAT_INCR(arcstat_other_size, space);
1198 case ARC_SPACE_HDRS:
1199 ARCSTAT_INCR(arcstat_hdr_size, space);
1201 case ARC_SPACE_L2HDRS:
1202 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1206 atomic_add_64(&arc_meta_used, space);
1207 atomic_add_64(&arc_size, space);
1211 arc_space_return(uint64_t space, arc_space_type_t type)
1213 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1218 case ARC_SPACE_DATA:
1219 ARCSTAT_INCR(arcstat_data_size, -space);
1221 case ARC_SPACE_OTHER:
1222 ARCSTAT_INCR(arcstat_other_size, -space);
1224 case ARC_SPACE_HDRS:
1225 ARCSTAT_INCR(arcstat_hdr_size, -space);
1227 case ARC_SPACE_L2HDRS:
1228 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1232 ASSERT(arc_meta_used >= space);
1233 if (arc_meta_max < arc_meta_used)
1234 arc_meta_max = arc_meta_used;
1235 atomic_add_64(&arc_meta_used, -space);
1236 ASSERT(arc_size >= space);
1237 atomic_add_64(&arc_size, -space);
1241 arc_data_buf_alloc(uint64_t size)
1243 if (arc_evict_needed(ARC_BUFC_DATA))
1244 cv_signal(&arc_reclaim_thr_cv);
1245 atomic_add_64(&arc_size, size);
1246 return (zio_data_buf_alloc(size));
1250 arc_data_buf_free(void *buf, uint64_t size)
1252 zio_data_buf_free(buf, size);
1253 ASSERT(arc_size >= size);
1254 atomic_add_64(&arc_size, -size);
1258 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1263 ASSERT3U(size, >, 0);
1264 hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1265 ASSERT(BUF_EMPTY(hdr));
1268 hdr->b_spa = spa_guid(spa);
1269 hdr->b_state = arc_anon;
1270 hdr->b_arc_access = 0;
1271 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1274 buf->b_efunc = NULL;
1275 buf->b_private = NULL;
1278 arc_get_data_buf(buf);
1281 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1282 (void) refcount_add(&hdr->b_refcnt, tag);
1287 static char *arc_onloan_tag = "onloan";
1290 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1291 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1292 * buffers must be returned to the arc before they can be used by the DMU or
1296 arc_loan_buf(spa_t *spa, int size)
1300 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1302 atomic_add_64(&arc_loaned_bytes, size);
1307 * Return a loaned arc buffer to the arc.
1310 arc_return_buf(arc_buf_t *buf, void *tag)
1312 arc_buf_hdr_t *hdr = buf->b_hdr;
1314 ASSERT(buf->b_data != NULL);
1315 (void) refcount_add(&hdr->b_refcnt, tag);
1316 (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1318 atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1321 /* Detach an arc_buf from a dbuf (tag) */
1323 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1327 ASSERT(buf->b_data != NULL);
1329 (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1330 (void) refcount_remove(&hdr->b_refcnt, tag);
1331 buf->b_efunc = NULL;
1332 buf->b_private = NULL;
1334 atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1338 arc_buf_clone(arc_buf_t *from)
1341 arc_buf_hdr_t *hdr = from->b_hdr;
1342 uint64_t size = hdr->b_size;
1344 ASSERT(hdr->b_state != arc_anon);
1346 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1349 buf->b_efunc = NULL;
1350 buf->b_private = NULL;
1351 buf->b_next = hdr->b_buf;
1353 arc_get_data_buf(buf);
1354 bcopy(from->b_data, buf->b_data, size);
1355 hdr->b_datacnt += 1;
1360 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1363 kmutex_t *hash_lock;
1366 * Check to see if this buffer is evicted. Callers
1367 * must verify b_data != NULL to know if the add_ref
1370 mutex_enter(&buf->b_evict_lock);
1371 if (buf->b_data == NULL) {
1372 mutex_exit(&buf->b_evict_lock);
1375 hash_lock = HDR_LOCK(buf->b_hdr);
1376 mutex_enter(hash_lock);
1378 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1379 mutex_exit(&buf->b_evict_lock);
1381 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1382 add_reference(hdr, hash_lock, tag);
1383 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1384 arc_access(hdr, hash_lock);
1385 mutex_exit(hash_lock);
1386 ARCSTAT_BUMP(arcstat_hits);
1387 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1388 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1389 data, metadata, hits);
1393 * Free the arc data buffer. If it is an l2arc write in progress,
1394 * the buffer is placed on l2arc_free_on_write to be freed later.
1397 arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t),
1398 void *data, size_t size)
1400 if (HDR_L2_WRITING(hdr)) {
1401 l2arc_data_free_t *df;
1402 df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
1403 df->l2df_data = data;
1404 df->l2df_size = size;
1405 df->l2df_func = free_func;
1406 mutex_enter(&l2arc_free_on_write_mtx);
1407 list_insert_head(l2arc_free_on_write, df);
1408 mutex_exit(&l2arc_free_on_write_mtx);
1409 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1411 free_func(data, size);
1416 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1420 /* free up data associated with the buf */
1422 arc_state_t *state = buf->b_hdr->b_state;
1423 uint64_t size = buf->b_hdr->b_size;
1424 arc_buf_contents_t type = buf->b_hdr->b_type;
1426 arc_cksum_verify(buf);
1429 if (type == ARC_BUFC_METADATA) {
1430 arc_buf_data_free(buf->b_hdr, zio_buf_free,
1432 arc_space_return(size, ARC_SPACE_DATA);
1434 ASSERT(type == ARC_BUFC_DATA);
1435 arc_buf_data_free(buf->b_hdr,
1436 zio_data_buf_free, buf->b_data, size);
1437 ARCSTAT_INCR(arcstat_data_size, -size);
1438 atomic_add_64(&arc_size, -size);
1441 if (list_link_active(&buf->b_hdr->b_arc_node)) {
1442 uint64_t *cnt = &state->arcs_lsize[type];
1444 ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1445 ASSERT(state != arc_anon);
1447 ASSERT3U(*cnt, >=, size);
1448 atomic_add_64(cnt, -size);
1450 ASSERT3U(state->arcs_size, >=, size);
1451 atomic_add_64(&state->arcs_size, -size);
1453 ASSERT(buf->b_hdr->b_datacnt > 0);
1454 buf->b_hdr->b_datacnt -= 1;
1457 /* only remove the buf if requested */
1461 /* remove the buf from the hdr list */
1462 for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1464 *bufp = buf->b_next;
1467 ASSERT(buf->b_efunc == NULL);
1469 /* clean up the buf */
1471 kmem_cache_free(buf_cache, buf);
1475 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1477 l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1479 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1480 ASSERT3P(hdr->b_state, ==, arc_anon);
1481 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1483 if (l2hdr != NULL) {
1484 boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1486 * To prevent arc_free() and l2arc_evict() from
1487 * attempting to free the same buffer at the same time,
1488 * a FREE_IN_PROGRESS flag is given to arc_free() to
1489 * give it priority. l2arc_evict() can't destroy this
1490 * header while we are waiting on l2arc_buflist_mtx.
1492 * The hdr may be removed from l2ad_buflist before we
1493 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1495 if (!buflist_held) {
1496 mutex_enter(&l2arc_buflist_mtx);
1497 l2hdr = hdr->b_l2hdr;
1500 if (l2hdr != NULL) {
1501 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1502 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1503 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1504 if (hdr->b_state == arc_l2c_only)
1505 l2arc_hdr_stat_remove();
1506 hdr->b_l2hdr = NULL;
1510 mutex_exit(&l2arc_buflist_mtx);
1513 if (!BUF_EMPTY(hdr)) {
1514 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1515 buf_discard_identity(hdr);
1517 while (hdr->b_buf) {
1518 arc_buf_t *buf = hdr->b_buf;
1521 mutex_enter(&arc_eviction_mtx);
1522 mutex_enter(&buf->b_evict_lock);
1523 ASSERT(buf->b_hdr != NULL);
1524 arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1525 hdr->b_buf = buf->b_next;
1526 buf->b_hdr = &arc_eviction_hdr;
1527 buf->b_next = arc_eviction_list;
1528 arc_eviction_list = buf;
1529 mutex_exit(&buf->b_evict_lock);
1530 mutex_exit(&arc_eviction_mtx);
1532 arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1535 if (hdr->b_freeze_cksum != NULL) {
1536 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1537 hdr->b_freeze_cksum = NULL;
1539 if (hdr->b_thawed) {
1540 kmem_free(hdr->b_thawed, 1);
1541 hdr->b_thawed = NULL;
1544 ASSERT(!list_link_active(&hdr->b_arc_node));
1545 ASSERT3P(hdr->b_hash_next, ==, NULL);
1546 ASSERT3P(hdr->b_acb, ==, NULL);
1547 kmem_cache_free(hdr_cache, hdr);
1551 arc_buf_free(arc_buf_t *buf, void *tag)
1553 arc_buf_hdr_t *hdr = buf->b_hdr;
1554 int hashed = hdr->b_state != arc_anon;
1556 ASSERT(buf->b_efunc == NULL);
1557 ASSERT(buf->b_data != NULL);
1560 kmutex_t *hash_lock = HDR_LOCK(hdr);
1562 mutex_enter(hash_lock);
1564 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1566 (void) remove_reference(hdr, hash_lock, tag);
1567 if (hdr->b_datacnt > 1) {
1568 arc_buf_destroy(buf, FALSE, TRUE);
1570 ASSERT(buf == hdr->b_buf);
1571 ASSERT(buf->b_efunc == NULL);
1572 hdr->b_flags |= ARC_BUF_AVAILABLE;
1574 mutex_exit(hash_lock);
1575 } else if (HDR_IO_IN_PROGRESS(hdr)) {
1578 * We are in the middle of an async write. Don't destroy
1579 * this buffer unless the write completes before we finish
1580 * decrementing the reference count.
1582 mutex_enter(&arc_eviction_mtx);
1583 (void) remove_reference(hdr, NULL, tag);
1584 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1585 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1586 mutex_exit(&arc_eviction_mtx);
1588 arc_hdr_destroy(hdr);
1590 if (remove_reference(hdr, NULL, tag) > 0)
1591 arc_buf_destroy(buf, FALSE, TRUE);
1593 arc_hdr_destroy(hdr);
1598 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1600 arc_buf_hdr_t *hdr = buf->b_hdr;
1601 kmutex_t *hash_lock = HDR_LOCK(hdr);
1602 int no_callback = (buf->b_efunc == NULL);
1604 if (hdr->b_state == arc_anon) {
1605 ASSERT(hdr->b_datacnt == 1);
1606 arc_buf_free(buf, tag);
1607 return (no_callback);
1610 mutex_enter(hash_lock);
1612 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1613 ASSERT(hdr->b_state != arc_anon);
1614 ASSERT(buf->b_data != NULL);
1616 (void) remove_reference(hdr, hash_lock, tag);
1617 if (hdr->b_datacnt > 1) {
1619 arc_buf_destroy(buf, FALSE, TRUE);
1620 } else if (no_callback) {
1621 ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1622 ASSERT(buf->b_efunc == NULL);
1623 hdr->b_flags |= ARC_BUF_AVAILABLE;
1625 ASSERT(no_callback || hdr->b_datacnt > 1 ||
1626 refcount_is_zero(&hdr->b_refcnt));
1627 mutex_exit(hash_lock);
1628 return (no_callback);
1632 arc_buf_size(arc_buf_t *buf)
1634 return (buf->b_hdr->b_size);
1638 * Evict buffers from list until we've removed the specified number of
1639 * bytes. Move the removed buffers to the appropriate evict state.
1640 * If the recycle flag is set, then attempt to "recycle" a buffer:
1641 * - look for a buffer to evict that is `bytes' long.
1642 * - return the data block from this buffer rather than freeing it.
1643 * This flag is used by callers that are trying to make space for a
1644 * new buffer in a full arc cache.
1646 * This function makes a "best effort". It skips over any buffers
1647 * it can't get a hash_lock on, and so may not catch all candidates.
1648 * It may also return without evicting as much space as requested.
1651 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1652 arc_buf_contents_t type)
1654 arc_state_t *evicted_state;
1655 uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1656 arc_buf_hdr_t *ab, *ab_prev = NULL;
1657 list_t *list = &state->arcs_list[type];
1658 kmutex_t *hash_lock;
1659 boolean_t have_lock;
1660 void *stolen = NULL;
1662 ASSERT(state == arc_mru || state == arc_mfu);
1664 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1666 mutex_enter(&state->arcs_mtx);
1667 mutex_enter(&evicted_state->arcs_mtx);
1669 for (ab = list_tail(list); ab; ab = ab_prev) {
1670 ab_prev = list_prev(list, ab);
1671 /* prefetch buffers have a minimum lifespan */
1672 if (HDR_IO_IN_PROGRESS(ab) ||
1673 (spa && ab->b_spa != spa) ||
1674 (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1675 ddi_get_lbolt() - ab->b_arc_access <
1676 arc_min_prefetch_lifespan)) {
1680 /* "lookahead" for better eviction candidate */
1681 if (recycle && ab->b_size != bytes &&
1682 ab_prev && ab_prev->b_size == bytes)
1684 hash_lock = HDR_LOCK(ab);
1685 have_lock = MUTEX_HELD(hash_lock);
1686 if (have_lock || mutex_tryenter(hash_lock)) {
1687 ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0);
1688 ASSERT(ab->b_datacnt > 0);
1690 arc_buf_t *buf = ab->b_buf;
1691 if (!mutex_tryenter(&buf->b_evict_lock)) {
1696 bytes_evicted += ab->b_size;
1697 if (recycle && ab->b_type == type &&
1698 ab->b_size == bytes &&
1699 !HDR_L2_WRITING(ab)) {
1700 stolen = buf->b_data;
1705 mutex_enter(&arc_eviction_mtx);
1706 arc_buf_destroy(buf,
1707 buf->b_data == stolen, FALSE);
1708 ab->b_buf = buf->b_next;
1709 buf->b_hdr = &arc_eviction_hdr;
1710 buf->b_next = arc_eviction_list;
1711 arc_eviction_list = buf;
1712 mutex_exit(&arc_eviction_mtx);
1713 mutex_exit(&buf->b_evict_lock);
1715 mutex_exit(&buf->b_evict_lock);
1716 arc_buf_destroy(buf,
1717 buf->b_data == stolen, TRUE);
1722 ARCSTAT_INCR(arcstat_evict_l2_cached,
1725 if (l2arc_write_eligible(ab->b_spa, ab)) {
1726 ARCSTAT_INCR(arcstat_evict_l2_eligible,
1730 arcstat_evict_l2_ineligible,
1735 if (ab->b_datacnt == 0) {
1736 arc_change_state(evicted_state, ab, hash_lock);
1737 ASSERT(HDR_IN_HASH_TABLE(ab));
1738 ab->b_flags |= ARC_IN_HASH_TABLE;
1739 ab->b_flags &= ~ARC_BUF_AVAILABLE;
1740 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1743 mutex_exit(hash_lock);
1744 if (bytes >= 0 && bytes_evicted >= bytes)
1751 mutex_exit(&evicted_state->arcs_mtx);
1752 mutex_exit(&state->arcs_mtx);
1754 if (bytes_evicted < bytes)
1755 dprintf("only evicted %lld bytes from %x\n",
1756 (longlong_t)bytes_evicted, state);
1759 ARCSTAT_INCR(arcstat_evict_skip, skipped);
1762 ARCSTAT_INCR(arcstat_mutex_miss, missed);
1765 * We have just evicted some date into the ghost state, make
1766 * sure we also adjust the ghost state size if necessary.
1769 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
1770 int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
1771 arc_mru_ghost->arcs_size - arc_c;
1773 if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
1775 MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
1776 arc_evict_ghost(arc_mru_ghost, 0, todelete);
1777 } else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
1778 int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
1779 arc_mru_ghost->arcs_size +
1780 arc_mfu_ghost->arcs_size - arc_c);
1781 arc_evict_ghost(arc_mfu_ghost, 0, todelete);
1789 * Remove buffers from list until we've removed the specified number of
1790 * bytes. Destroy the buffers that are removed.
1793 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
1795 arc_buf_hdr_t *ab, *ab_prev;
1796 arc_buf_hdr_t marker;
1797 list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1798 kmutex_t *hash_lock;
1799 uint64_t bytes_deleted = 0;
1800 uint64_t bufs_skipped = 0;
1802 ASSERT(GHOST_STATE(state));
1803 bzero(&marker, sizeof(marker));
1805 mutex_enter(&state->arcs_mtx);
1806 for (ab = list_tail(list); ab; ab = ab_prev) {
1807 ab_prev = list_prev(list, ab);
1808 if (spa && ab->b_spa != spa)
1811 /* ignore markers */
1815 hash_lock = HDR_LOCK(ab);
1816 /* caller may be trying to modify this buffer, skip it */
1817 if (MUTEX_HELD(hash_lock))
1819 if (mutex_tryenter(hash_lock)) {
1820 ASSERT(!HDR_IO_IN_PROGRESS(ab));
1821 ASSERT(ab->b_buf == NULL);
1822 ARCSTAT_BUMP(arcstat_deleted);
1823 bytes_deleted += ab->b_size;
1825 if (ab->b_l2hdr != NULL) {
1827 * This buffer is cached on the 2nd Level ARC;
1828 * don't destroy the header.
1830 arc_change_state(arc_l2c_only, ab, hash_lock);
1831 mutex_exit(hash_lock);
1833 arc_change_state(arc_anon, ab, hash_lock);
1834 mutex_exit(hash_lock);
1835 arc_hdr_destroy(ab);
1838 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1839 if (bytes >= 0 && bytes_deleted >= bytes)
1841 } else if (bytes < 0) {
1843 * Insert a list marker and then wait for the
1844 * hash lock to become available. Once its
1845 * available, restart from where we left off.
1847 list_insert_after(list, ab, &marker);
1848 mutex_exit(&state->arcs_mtx);
1849 mutex_enter(hash_lock);
1850 mutex_exit(hash_lock);
1851 mutex_enter(&state->arcs_mtx);
1852 ab_prev = list_prev(list, &marker);
1853 list_remove(list, &marker);
1857 mutex_exit(&state->arcs_mtx);
1859 if (list == &state->arcs_list[ARC_BUFC_DATA] &&
1860 (bytes < 0 || bytes_deleted < bytes)) {
1861 list = &state->arcs_list[ARC_BUFC_METADATA];
1866 ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
1870 if (bytes_deleted < bytes)
1871 dprintf("only deleted %lld bytes from %p\n",
1872 (longlong_t)bytes_deleted, state);
1878 int64_t adjustment, delta;
1884 adjustment = MIN((int64_t)(arc_size - arc_c),
1885 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
1888 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
1889 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
1890 (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA);
1891 adjustment -= delta;
1894 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1895 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
1896 (void) arc_evict(arc_mru, 0, delta, FALSE,
1904 adjustment = arc_size - arc_c;
1906 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
1907 delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
1908 (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA);
1909 adjustment -= delta;
1912 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1913 int64_t delta = MIN(adjustment,
1914 arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
1915 (void) arc_evict(arc_mfu, 0, delta, FALSE,
1920 * Adjust ghost lists
1923 adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
1925 if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
1926 delta = MIN(arc_mru_ghost->arcs_size, adjustment);
1927 arc_evict_ghost(arc_mru_ghost, 0, delta);
1931 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
1933 if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
1934 delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
1935 arc_evict_ghost(arc_mfu_ghost, 0, delta);
1940 * Request that arc user drop references so that N bytes can be released
1941 * from the cache. This provides a mechanism to ensure the arc can honor
1942 * the arc_meta_limit and reclaim buffers which are pinned in the cache
1943 * by higher layers. (i.e. the zpl)
1946 arc_do_user_prune(int64_t adjustment)
1948 arc_prune_func_t *func;
1950 arc_prune_t *cp, *np;
1952 mutex_enter(&arc_prune_mtx);
1954 cp = list_head(&arc_prune_list);
1955 while (cp != NULL) {
1957 private = cp->p_private;
1958 np = list_next(&arc_prune_list, cp);
1959 refcount_add(&cp->p_refcnt, func);
1960 mutex_exit(&arc_prune_mtx);
1963 func(adjustment, private);
1965 mutex_enter(&arc_prune_mtx);
1967 /* User removed prune callback concurrently with execution */
1968 if (refcount_remove(&cp->p_refcnt, func) == 0) {
1969 ASSERT(!list_link_active(&cp->p_node));
1970 refcount_destroy(&cp->p_refcnt);
1971 kmem_free(cp, sizeof (*cp));
1977 ARCSTAT_BUMP(arcstat_prune);
1978 mutex_exit(&arc_prune_mtx);
1982 arc_do_user_evicts(void)
1984 mutex_enter(&arc_eviction_mtx);
1985 while (arc_eviction_list != NULL) {
1986 arc_buf_t *buf = arc_eviction_list;
1987 arc_eviction_list = buf->b_next;
1988 mutex_enter(&buf->b_evict_lock);
1990 mutex_exit(&buf->b_evict_lock);
1991 mutex_exit(&arc_eviction_mtx);
1993 if (buf->b_efunc != NULL)
1994 VERIFY(buf->b_efunc(buf) == 0);
1996 buf->b_efunc = NULL;
1997 buf->b_private = NULL;
1998 kmem_cache_free(buf_cache, buf);
1999 mutex_enter(&arc_eviction_mtx);
2001 mutex_exit(&arc_eviction_mtx);
2005 * Evict only meta data objects from the cache leaving the data objects.
2006 * This is only used to enforce the tunable arc_meta_limit, if we are
2007 * unable to evict enough buffers notify the user via the prune callback.
2010 arc_adjust_meta(int64_t adjustment, boolean_t may_prune)
2014 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2015 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2016 arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_METADATA);
2017 adjustment -= delta;
2020 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2021 delta = MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2022 arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_METADATA);
2023 adjustment -= delta;
2026 if (may_prune && (adjustment > 0) && (arc_meta_used > arc_meta_limit))
2027 arc_do_user_prune(arc_meta_prune);
2031 * Flush all *evictable* data from the cache for the given spa.
2032 * NOTE: this will not touch "active" (i.e. referenced) data.
2035 arc_flush(spa_t *spa)
2040 guid = spa_guid(spa);
2042 while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
2043 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
2047 while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
2048 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
2052 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
2053 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
2057 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
2058 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
2063 arc_evict_ghost(arc_mru_ghost, guid, -1);
2064 arc_evict_ghost(arc_mfu_ghost, guid, -1);
2066 mutex_enter(&arc_reclaim_thr_lock);
2067 arc_do_user_evicts();
2068 mutex_exit(&arc_reclaim_thr_lock);
2069 ASSERT(spa || arc_eviction_list == NULL);
2075 if (arc_c > arc_c_min) {
2079 to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree));
2081 to_free = arc_c >> arc_shrink_shift;
2083 if (arc_c > arc_c_min + to_free)
2084 atomic_add_64(&arc_c, -to_free);
2088 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
2089 if (arc_c > arc_size)
2090 arc_c = MAX(arc_size, arc_c_min);
2092 arc_p = (arc_c >> 1);
2093 ASSERT(arc_c >= arc_c_min);
2094 ASSERT((int64_t)arc_p >= 0);
2097 if (arc_size > arc_c)
2102 arc_reclaim_needed(void)
2111 * take 'desfree' extra pages, so we reclaim sooner, rather than later
2116 * check that we're out of range of the pageout scanner. It starts to
2117 * schedule paging if freemem is less than lotsfree and needfree.
2118 * lotsfree is the high-water mark for pageout, and needfree is the
2119 * number of needed free pages. We add extra pages here to make sure
2120 * the scanner doesn't start up while we're freeing memory.
2122 if (freemem < lotsfree + needfree + extra)
2126 * check to make sure that swapfs has enough space so that anon
2127 * reservations can still succeed. anon_resvmem() checks that the
2128 * availrmem is greater than swapfs_minfree, and the number of reserved
2129 * swap pages. We also add a bit of extra here just to prevent
2130 * circumstances from getting really dire.
2132 if (availrmem < swapfs_minfree + swapfs_reserve + extra)
2137 * If we're on an i386 platform, it's possible that we'll exhaust the
2138 * kernel heap space before we ever run out of available physical
2139 * memory. Most checks of the size of the heap_area compare against
2140 * tune.t_minarmem, which is the minimum available real memory that we
2141 * can have in the system. However, this is generally fixed at 25 pages
2142 * which is so low that it's useless. In this comparison, we seek to
2143 * calculate the total heap-size, and reclaim if more than 3/4ths of the
2144 * heap is allocated. (Or, in the calculation, if less than 1/4th is
2147 if (btop(vmem_size(heap_arena, VMEM_FREE)) <
2148 (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
2153 if (spa_get_random(100) == 0)
2160 arc_kmem_reap_now(arc_reclaim_strategy_t strat)
2163 kmem_cache_t *prev_cache = NULL;
2164 kmem_cache_t *prev_data_cache = NULL;
2165 extern kmem_cache_t *zio_buf_cache[];
2166 extern kmem_cache_t *zio_data_buf_cache[];
2169 * An aggressive reclamation will shrink the cache size as well as
2170 * reap free buffers from the arc kmem caches.
2172 if (strat == ARC_RECLAIM_AGGR)
2175 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2176 if (zio_buf_cache[i] != prev_cache) {
2177 prev_cache = zio_buf_cache[i];
2178 kmem_cache_reap_now(zio_buf_cache[i]);
2180 if (zio_data_buf_cache[i] != prev_data_cache) {
2181 prev_data_cache = zio_data_buf_cache[i];
2182 kmem_cache_reap_now(zio_data_buf_cache[i]);
2186 kmem_cache_reap_now(buf_cache);
2187 kmem_cache_reap_now(hdr_cache);
2191 arc_reclaim_thread(void)
2193 clock_t growtime = 0;
2194 arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS;
2198 CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2200 mutex_enter(&arc_reclaim_thr_lock);
2201 while (arc_thread_exit == 0) {
2202 if (arc_reclaim_needed()) {
2205 if (last_reclaim == ARC_RECLAIM_CONS) {
2206 last_reclaim = ARC_RECLAIM_AGGR;
2208 last_reclaim = ARC_RECLAIM_CONS;
2212 last_reclaim = ARC_RECLAIM_AGGR;
2216 /* reset the growth delay for every reclaim */
2217 growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
2219 arc_kmem_reap_now(last_reclaim);
2222 } else if (arc_no_grow && ddi_get_lbolt() >= growtime) {
2223 arc_no_grow = FALSE;
2227 * Keep meta data usage within limits, arc_shrink() is not
2228 * used to avoid collapsing the arc_c value when only the
2229 * arc_meta_limit is being exceeded.
2231 prune = (int64_t)arc_meta_used - (int64_t)arc_meta_limit;
2233 arc_adjust_meta(prune, B_TRUE);
2237 if (arc_eviction_list != NULL)
2238 arc_do_user_evicts();
2240 /* block until needed, or one second, whichever is shorter */
2241 CALLB_CPR_SAFE_BEGIN(&cpr);
2242 (void) cv_timedwait_interruptible(&arc_reclaim_thr_cv,
2243 &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2244 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2247 arc_thread_exit = 0;
2248 cv_broadcast(&arc_reclaim_thr_cv);
2249 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */
2255 * Under Linux the arc shrinker may be called for synchronous (direct)
2256 * reclaim, or asynchronous (indirect) reclaim. When called by kswapd
2257 * for indirect reclaim we take a conservative approach and just reap
2258 * free slabs from the ARC caches. If this proves to be insufficient
2259 * direct reclaim will be trigger. In direct reclaim a more aggressive
2260 * strategy is used, data is evicted from the ARC and free slabs reaped.
2263 __arc_shrinker_func(struct shrinker *shrink, struct shrink_control *sc)
2265 arc_reclaim_strategy_t strategy;
2268 /* Return number of reclaimable pages based on arc_shrink_shift */
2269 arc_reclaim = MAX(btop(((int64_t)arc_size - (int64_t)arc_c_min))
2270 >> arc_shrink_shift, 0);
2271 if (sc->nr_to_scan == 0)
2272 return (arc_reclaim);
2274 /* Prevent reclaim below arc_c_min */
2275 if (arc_reclaim <= 0)
2278 /* Not allowed to perform filesystem reclaim */
2279 if (!(sc->gfp_mask & __GFP_FS))
2282 /* Reclaim in progress */
2283 if (mutex_tryenter(&arc_reclaim_thr_lock) == 0)
2286 if (current_is_kswapd()) {
2287 strategy = ARC_RECLAIM_CONS;
2288 ARCSTAT_INCR(arcstat_memory_indirect_count, 1);
2290 strategy = ARC_RECLAIM_AGGR;
2291 ARCSTAT_INCR(arcstat_memory_direct_count, 1);
2294 arc_kmem_reap_now(strategy);
2295 arc_reclaim = MAX(btop(((int64_t)arc_size - (int64_t)arc_c_min))
2296 >> arc_shrink_shift, 0);
2297 mutex_exit(&arc_reclaim_thr_lock);
2299 return (arc_reclaim);
2301 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func);
2303 SPL_SHRINKER_DECLARE(arc_shrinker, arc_shrinker_func, DEFAULT_SEEKS);
2304 #endif /* _KERNEL */
2307 * Adapt arc info given the number of bytes we are trying to add and
2308 * the state that we are comming from. This function is only called
2309 * when we are adding new content to the cache.
2312 arc_adapt(int bytes, arc_state_t *state)
2315 uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
2317 if (state == arc_l2c_only)
2322 * Adapt the target size of the MRU list:
2323 * - if we just hit in the MRU ghost list, then increase
2324 * the target size of the MRU list.
2325 * - if we just hit in the MFU ghost list, then increase
2326 * the target size of the MFU list by decreasing the
2327 * target size of the MRU list.
2329 if (state == arc_mru_ghost) {
2330 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2331 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2332 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2334 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2335 } else if (state == arc_mfu_ghost) {
2338 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2339 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2340 mult = MIN(mult, 10);
2342 delta = MIN(bytes * mult, arc_p);
2343 arc_p = MAX(arc_p_min, arc_p - delta);
2345 ASSERT((int64_t)arc_p >= 0);
2347 if (arc_reclaim_needed()) {
2348 cv_signal(&arc_reclaim_thr_cv);
2355 if (arc_c >= arc_c_max)
2359 * If we're within (2 * maxblocksize) bytes of the target
2360 * cache size, increment the target cache size
2362 if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2363 atomic_add_64(&arc_c, (int64_t)bytes);
2364 if (arc_c > arc_c_max)
2366 else if (state == arc_anon)
2367 atomic_add_64(&arc_p, (int64_t)bytes);
2371 ASSERT((int64_t)arc_p >= 0);
2375 * Check if the cache has reached its limits and eviction is required
2379 arc_evict_needed(arc_buf_contents_t type)
2381 if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2386 * If zio data pages are being allocated out of a separate heap segment,
2387 * then enforce that the size of available vmem for this area remains
2388 * above about 1/32nd free.
2390 if (type == ARC_BUFC_DATA && zio_arena != NULL &&
2391 vmem_size(zio_arena, VMEM_FREE) <
2392 (vmem_size(zio_arena, VMEM_ALLOC) >> 5))
2396 if (arc_reclaim_needed())
2399 return (arc_size > arc_c);
2403 * The buffer, supplied as the first argument, needs a data block.
2404 * So, if we are at cache max, determine which cache should be victimized.
2405 * We have the following cases:
2407 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2408 * In this situation if we're out of space, but the resident size of the MFU is
2409 * under the limit, victimize the MFU cache to satisfy this insertion request.
2411 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2412 * Here, we've used up all of the available space for the MRU, so we need to
2413 * evict from our own cache instead. Evict from the set of resident MRU
2416 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2417 * c minus p represents the MFU space in the cache, since p is the size of the
2418 * cache that is dedicated to the MRU. In this situation there's still space on
2419 * the MFU side, so the MRU side needs to be victimized.
2421 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2422 * MFU's resident set is consuming more space than it has been allotted. In
2423 * this situation, we must victimize our own cache, the MFU, for this insertion.
2426 arc_get_data_buf(arc_buf_t *buf)
2428 arc_state_t *state = buf->b_hdr->b_state;
2429 uint64_t size = buf->b_hdr->b_size;
2430 arc_buf_contents_t type = buf->b_hdr->b_type;
2432 arc_adapt(size, state);
2435 * We have not yet reached cache maximum size,
2436 * just allocate a new buffer.
2438 if (!arc_evict_needed(type)) {
2439 if (type == ARC_BUFC_METADATA) {
2440 buf->b_data = zio_buf_alloc(size);
2441 arc_space_consume(size, ARC_SPACE_DATA);
2443 ASSERT(type == ARC_BUFC_DATA);
2444 buf->b_data = zio_data_buf_alloc(size);
2445 ARCSTAT_INCR(arcstat_data_size, size);
2446 atomic_add_64(&arc_size, size);
2452 * If we are prefetching from the mfu ghost list, this buffer
2453 * will end up on the mru list; so steal space from there.
2455 if (state == arc_mfu_ghost)
2456 state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2457 else if (state == arc_mru_ghost)
2460 if (state == arc_mru || state == arc_anon) {
2461 uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2462 state = (arc_mfu->arcs_lsize[type] >= size &&
2463 arc_p > mru_used) ? arc_mfu : arc_mru;
2466 uint64_t mfu_space = arc_c - arc_p;
2467 state = (arc_mru->arcs_lsize[type] >= size &&
2468 mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2471 if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) {
2472 if (type == ARC_BUFC_METADATA) {
2473 buf->b_data = zio_buf_alloc(size);
2474 arc_space_consume(size, ARC_SPACE_DATA);
2477 * If we are unable to recycle an existing meta buffer
2478 * signal the reclaim thread. It will notify users
2479 * via the prune callback to drop references. The
2480 * prune callback in run in the context of the reclaim
2481 * thread to avoid deadlocking on the hash_lock.
2483 cv_signal(&arc_reclaim_thr_cv);
2485 ASSERT(type == ARC_BUFC_DATA);
2486 buf->b_data = zio_data_buf_alloc(size);
2487 ARCSTAT_INCR(arcstat_data_size, size);
2488 atomic_add_64(&arc_size, size);
2491 ARCSTAT_BUMP(arcstat_recycle_miss);
2493 ASSERT(buf->b_data != NULL);
2496 * Update the state size. Note that ghost states have a
2497 * "ghost size" and so don't need to be updated.
2499 if (!GHOST_STATE(buf->b_hdr->b_state)) {
2500 arc_buf_hdr_t *hdr = buf->b_hdr;
2502 atomic_add_64(&hdr->b_state->arcs_size, size);
2503 if (list_link_active(&hdr->b_arc_node)) {
2504 ASSERT(refcount_is_zero(&hdr->b_refcnt));
2505 atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2508 * If we are growing the cache, and we are adding anonymous
2509 * data, and we have outgrown arc_p, update arc_p
2511 if (arc_size < arc_c && hdr->b_state == arc_anon &&
2512 arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2513 arc_p = MIN(arc_c, arc_p + size);
2518 * This routine is called whenever a buffer is accessed.
2519 * NOTE: the hash lock is dropped in this function.
2522 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2526 ASSERT(MUTEX_HELD(hash_lock));
2528 if (buf->b_state == arc_anon) {
2530 * This buffer is not in the cache, and does not
2531 * appear in our "ghost" list. Add the new buffer
2535 ASSERT(buf->b_arc_access == 0);
2536 buf->b_arc_access = ddi_get_lbolt();
2537 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2538 arc_change_state(arc_mru, buf, hash_lock);
2540 } else if (buf->b_state == arc_mru) {
2541 now = ddi_get_lbolt();
2544 * If this buffer is here because of a prefetch, then either:
2545 * - clear the flag if this is a "referencing" read
2546 * (any subsequent access will bump this into the MFU state).
2548 * - move the buffer to the head of the list if this is
2549 * another prefetch (to make it less likely to be evicted).
2551 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2552 if (refcount_count(&buf->b_refcnt) == 0) {
2553 ASSERT(list_link_active(&buf->b_arc_node));
2555 buf->b_flags &= ~ARC_PREFETCH;
2556 ARCSTAT_BUMP(arcstat_mru_hits);
2558 buf->b_arc_access = now;
2563 * This buffer has been "accessed" only once so far,
2564 * but it is still in the cache. Move it to the MFU
2567 if (now > buf->b_arc_access + ARC_MINTIME) {
2569 * More than 125ms have passed since we
2570 * instantiated this buffer. Move it to the
2571 * most frequently used state.
2573 buf->b_arc_access = now;
2574 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2575 arc_change_state(arc_mfu, buf, hash_lock);
2577 ARCSTAT_BUMP(arcstat_mru_hits);
2578 } else if (buf->b_state == arc_mru_ghost) {
2579 arc_state_t *new_state;
2581 * This buffer has been "accessed" recently, but
2582 * was evicted from the cache. Move it to the
2586 if (buf->b_flags & ARC_PREFETCH) {
2587 new_state = arc_mru;
2588 if (refcount_count(&buf->b_refcnt) > 0)
2589 buf->b_flags &= ~ARC_PREFETCH;
2590 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2592 new_state = arc_mfu;
2593 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2596 buf->b_arc_access = ddi_get_lbolt();
2597 arc_change_state(new_state, buf, hash_lock);
2599 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2600 } else if (buf->b_state == arc_mfu) {
2602 * This buffer has been accessed more than once and is
2603 * still in the cache. Keep it in the MFU state.
2605 * NOTE: an add_reference() that occurred when we did
2606 * the arc_read() will have kicked this off the list.
2607 * If it was a prefetch, we will explicitly move it to
2608 * the head of the list now.
2610 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2611 ASSERT(refcount_count(&buf->b_refcnt) == 0);
2612 ASSERT(list_link_active(&buf->b_arc_node));
2614 ARCSTAT_BUMP(arcstat_mfu_hits);
2615 buf->b_arc_access = ddi_get_lbolt();
2616 } else if (buf->b_state == arc_mfu_ghost) {
2617 arc_state_t *new_state = arc_mfu;
2619 * This buffer has been accessed more than once but has
2620 * been evicted from the cache. Move it back to the
2624 if (buf->b_flags & ARC_PREFETCH) {
2626 * This is a prefetch access...
2627 * move this block back to the MRU state.
2629 ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0);
2630 new_state = arc_mru;
2633 buf->b_arc_access = ddi_get_lbolt();
2634 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2635 arc_change_state(new_state, buf, hash_lock);
2637 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2638 } else if (buf->b_state == arc_l2c_only) {
2640 * This buffer is on the 2nd Level ARC.
2643 buf->b_arc_access = ddi_get_lbolt();
2644 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2645 arc_change_state(arc_mfu, buf, hash_lock);
2647 ASSERT(!"invalid arc state");
2651 /* a generic arc_done_func_t which you can use */
2654 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2656 if (zio == NULL || zio->io_error == 0)
2657 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2658 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2661 /* a generic arc_done_func_t */
2663 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2665 arc_buf_t **bufp = arg;
2666 if (zio && zio->io_error) {
2667 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2671 ASSERT(buf->b_data);
2676 arc_read_done(zio_t *zio)
2678 arc_buf_hdr_t *hdr, *found;
2680 arc_buf_t *abuf; /* buffer we're assigning to callback */
2681 kmutex_t *hash_lock;
2682 arc_callback_t *callback_list, *acb;
2683 int freeable = FALSE;
2685 buf = zio->io_private;
2689 * The hdr was inserted into hash-table and removed from lists
2690 * prior to starting I/O. We should find this header, since
2691 * it's in the hash table, and it should be legit since it's
2692 * not possible to evict it during the I/O. The only possible
2693 * reason for it not to be found is if we were freed during the
2696 found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
2699 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
2700 (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2701 (found == hdr && HDR_L2_READING(hdr)));
2703 hdr->b_flags &= ~ARC_L2_EVICTED;
2704 if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2705 hdr->b_flags &= ~ARC_L2CACHE;
2707 /* byteswap if necessary */
2708 callback_list = hdr->b_acb;
2709 ASSERT(callback_list != NULL);
2710 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2711 arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
2712 byteswap_uint64_array :
2713 dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap;
2714 func(buf->b_data, hdr->b_size);
2717 arc_cksum_compute(buf, B_FALSE);
2719 if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
2721 * Only call arc_access on anonymous buffers. This is because
2722 * if we've issued an I/O for an evicted buffer, we've already
2723 * called arc_access (to prevent any simultaneous readers from
2724 * getting confused).
2726 arc_access(hdr, hash_lock);
2729 /* create copies of the data buffer for the callers */
2731 for (acb = callback_list; acb; acb = acb->acb_next) {
2732 if (acb->acb_done) {
2734 abuf = arc_buf_clone(buf);
2735 acb->acb_buf = abuf;
2740 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2741 ASSERT(!HDR_BUF_AVAILABLE(hdr));
2743 ASSERT(buf->b_efunc == NULL);
2744 ASSERT(hdr->b_datacnt == 1);
2745 hdr->b_flags |= ARC_BUF_AVAILABLE;
2748 ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2750 if (zio->io_error != 0) {
2751 hdr->b_flags |= ARC_IO_ERROR;
2752 if (hdr->b_state != arc_anon)
2753 arc_change_state(arc_anon, hdr, hash_lock);
2754 if (HDR_IN_HASH_TABLE(hdr))
2755 buf_hash_remove(hdr);
2756 freeable = refcount_is_zero(&hdr->b_refcnt);
2760 * Broadcast before we drop the hash_lock to avoid the possibility
2761 * that the hdr (and hence the cv) might be freed before we get to
2762 * the cv_broadcast().
2764 cv_broadcast(&hdr->b_cv);
2767 mutex_exit(hash_lock);
2770 * This block was freed while we waited for the read to
2771 * complete. It has been removed from the hash table and
2772 * moved to the anonymous state (so that it won't show up
2775 ASSERT3P(hdr->b_state, ==, arc_anon);
2776 freeable = refcount_is_zero(&hdr->b_refcnt);
2779 /* execute each callback and free its structure */
2780 while ((acb = callback_list) != NULL) {
2782 acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2784 if (acb->acb_zio_dummy != NULL) {
2785 acb->acb_zio_dummy->io_error = zio->io_error;
2786 zio_nowait(acb->acb_zio_dummy);
2789 callback_list = acb->acb_next;
2790 kmem_free(acb, sizeof (arc_callback_t));
2794 arc_hdr_destroy(hdr);
2798 * "Read" the block block at the specified DVA (in bp) via the
2799 * cache. If the block is found in the cache, invoke the provided
2800 * callback immediately and return. Note that the `zio' parameter
2801 * in the callback will be NULL in this case, since no IO was
2802 * required. If the block is not in the cache pass the read request
2803 * on to the spa with a substitute callback function, so that the
2804 * requested block will be added to the cache.
2806 * If a read request arrives for a block that has a read in-progress,
2807 * either wait for the in-progress read to complete (and return the
2808 * results); or, if this is a read with a "done" func, add a record
2809 * to the read to invoke the "done" func when the read completes,
2810 * and return; or just return.
2812 * arc_read_done() will invoke all the requested "done" functions
2813 * for readers of this block.
2815 * Normal callers should use arc_read and pass the arc buffer and offset
2816 * for the bp. But if you know you don't need locking, you can use
2820 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf,
2821 arc_done_func_t *done, void *private, int priority, int zio_flags,
2822 uint32_t *arc_flags, const zbookmark_t *zb)
2828 * XXX This happens from traverse callback funcs, for
2829 * the objset_phys_t block.
2831 return (arc_read_nolock(pio, spa, bp, done, private, priority,
2832 zio_flags, arc_flags, zb));
2835 ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt));
2836 ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size);
2837 rw_enter(&pbuf->b_data_lock, RW_READER);
2839 err = arc_read_nolock(pio, spa, bp, done, private, priority,
2840 zio_flags, arc_flags, zb);
2841 rw_exit(&pbuf->b_data_lock);
2847 arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp,
2848 arc_done_func_t *done, void *private, int priority, int zio_flags,
2849 uint32_t *arc_flags, const zbookmark_t *zb)
2852 arc_buf_t *buf = NULL;
2853 kmutex_t *hash_lock;
2855 uint64_t guid = spa_guid(spa);
2858 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
2860 if (hdr && hdr->b_datacnt > 0) {
2862 *arc_flags |= ARC_CACHED;
2864 if (HDR_IO_IN_PROGRESS(hdr)) {
2866 if (*arc_flags & ARC_WAIT) {
2867 cv_wait(&hdr->b_cv, hash_lock);
2868 mutex_exit(hash_lock);
2871 ASSERT(*arc_flags & ARC_NOWAIT);
2874 arc_callback_t *acb = NULL;
2876 acb = kmem_zalloc(sizeof (arc_callback_t),
2878 acb->acb_done = done;
2879 acb->acb_private = private;
2881 acb->acb_zio_dummy = zio_null(pio,
2882 spa, NULL, NULL, NULL, zio_flags);
2884 ASSERT(acb->acb_done != NULL);
2885 acb->acb_next = hdr->b_acb;
2887 add_reference(hdr, hash_lock, private);
2888 mutex_exit(hash_lock);
2891 mutex_exit(hash_lock);
2895 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2898 add_reference(hdr, hash_lock, private);
2900 * If this block is already in use, create a new
2901 * copy of the data so that we will be guaranteed
2902 * that arc_release() will always succeed.
2906 ASSERT(buf->b_data);
2907 if (HDR_BUF_AVAILABLE(hdr)) {
2908 ASSERT(buf->b_efunc == NULL);
2909 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
2911 buf = arc_buf_clone(buf);
2914 } else if (*arc_flags & ARC_PREFETCH &&
2915 refcount_count(&hdr->b_refcnt) == 0) {
2916 hdr->b_flags |= ARC_PREFETCH;
2918 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
2919 arc_access(hdr, hash_lock);
2920 if (*arc_flags & ARC_L2CACHE)
2921 hdr->b_flags |= ARC_L2CACHE;
2922 mutex_exit(hash_lock);
2923 ARCSTAT_BUMP(arcstat_hits);
2924 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2925 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
2926 data, metadata, hits);
2929 done(NULL, buf, private);
2931 uint64_t size = BP_GET_LSIZE(bp);
2932 arc_callback_t *acb;
2935 boolean_t devw = B_FALSE;
2938 /* this block is not in the cache */
2939 arc_buf_hdr_t *exists;
2940 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
2941 buf = arc_buf_alloc(spa, size, private, type);
2943 hdr->b_dva = *BP_IDENTITY(bp);
2944 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
2945 hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
2946 exists = buf_hash_insert(hdr, &hash_lock);
2948 /* somebody beat us to the hash insert */
2949 mutex_exit(hash_lock);
2950 buf_discard_identity(hdr);
2951 (void) arc_buf_remove_ref(buf, private);
2952 goto top; /* restart the IO request */
2954 /* if this is a prefetch, we don't have a reference */
2955 if (*arc_flags & ARC_PREFETCH) {
2956 (void) remove_reference(hdr, hash_lock,
2958 hdr->b_flags |= ARC_PREFETCH;
2960 if (*arc_flags & ARC_L2CACHE)
2961 hdr->b_flags |= ARC_L2CACHE;
2962 if (BP_GET_LEVEL(bp) > 0)
2963 hdr->b_flags |= ARC_INDIRECT;
2965 /* this block is in the ghost cache */
2966 ASSERT(GHOST_STATE(hdr->b_state));
2967 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
2968 ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0);
2969 ASSERT(hdr->b_buf == NULL);
2971 /* if this is a prefetch, we don't have a reference */
2972 if (*arc_flags & ARC_PREFETCH)
2973 hdr->b_flags |= ARC_PREFETCH;
2975 add_reference(hdr, hash_lock, private);
2976 if (*arc_flags & ARC_L2CACHE)
2977 hdr->b_flags |= ARC_L2CACHE;
2978 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
2981 buf->b_efunc = NULL;
2982 buf->b_private = NULL;
2985 ASSERT(hdr->b_datacnt == 0);
2987 arc_get_data_buf(buf);
2988 arc_access(hdr, hash_lock);
2991 ASSERT(!GHOST_STATE(hdr->b_state));
2993 acb = kmem_zalloc(sizeof (arc_callback_t), KM_PUSHPAGE);
2994 acb->acb_done = done;
2995 acb->acb_private = private;
2997 ASSERT(hdr->b_acb == NULL);
2999 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3001 if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
3002 (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
3003 devw = hdr->b_l2hdr->b_dev->l2ad_writing;
3004 addr = hdr->b_l2hdr->b_daddr;
3006 * Lock out device removal.
3008 if (vdev_is_dead(vd) ||
3009 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
3013 mutex_exit(hash_lock);
3015 ASSERT3U(hdr->b_size, ==, size);
3016 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
3017 uint64_t, size, zbookmark_t *, zb);
3018 ARCSTAT_BUMP(arcstat_misses);
3019 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3020 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3021 data, metadata, misses);
3023 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
3025 * Read from the L2ARC if the following are true:
3026 * 1. The L2ARC vdev was previously cached.
3027 * 2. This buffer still has L2ARC metadata.
3028 * 3. This buffer isn't currently writing to the L2ARC.
3029 * 4. The L2ARC entry wasn't evicted, which may
3030 * also have invalidated the vdev.
3031 * 5. This isn't prefetch and l2arc_noprefetch is set.
3033 if (hdr->b_l2hdr != NULL &&
3034 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
3035 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
3036 l2arc_read_callback_t *cb;
3038 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
3039 ARCSTAT_BUMP(arcstat_l2_hits);
3041 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
3043 cb->l2rcb_buf = buf;
3044 cb->l2rcb_spa = spa;
3047 cb->l2rcb_flags = zio_flags;
3050 * l2arc read. The SCL_L2ARC lock will be
3051 * released by l2arc_read_done().
3053 rzio = zio_read_phys(pio, vd, addr, size,
3054 buf->b_data, ZIO_CHECKSUM_OFF,
3055 l2arc_read_done, cb, priority, zio_flags |
3056 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL |
3057 ZIO_FLAG_DONT_PROPAGATE |
3058 ZIO_FLAG_DONT_RETRY, B_FALSE);
3059 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
3061 ARCSTAT_INCR(arcstat_l2_read_bytes, size);
3063 if (*arc_flags & ARC_NOWAIT) {
3068 ASSERT(*arc_flags & ARC_WAIT);
3069 if (zio_wait(rzio) == 0)
3072 /* l2arc read error; goto zio_read() */
3074 DTRACE_PROBE1(l2arc__miss,
3075 arc_buf_hdr_t *, hdr);
3076 ARCSTAT_BUMP(arcstat_l2_misses);
3077 if (HDR_L2_WRITING(hdr))
3078 ARCSTAT_BUMP(arcstat_l2_rw_clash);
3079 spa_config_exit(spa, SCL_L2ARC, vd);
3083 spa_config_exit(spa, SCL_L2ARC, vd);
3084 if (l2arc_ndev != 0) {
3085 DTRACE_PROBE1(l2arc__miss,
3086 arc_buf_hdr_t *, hdr);
3087 ARCSTAT_BUMP(arcstat_l2_misses);
3091 rzio = zio_read(pio, spa, bp, buf->b_data, size,
3092 arc_read_done, buf, priority, zio_flags, zb);
3094 if (*arc_flags & ARC_WAIT)
3095 return (zio_wait(rzio));
3097 ASSERT(*arc_flags & ARC_NOWAIT);
3104 arc_add_prune_callback(arc_prune_func_t *func, void *private)
3108 p = kmem_alloc(sizeof(*p), KM_SLEEP);
3110 p->p_private = private;
3111 list_link_init(&p->p_node);
3112 refcount_create(&p->p_refcnt);
3114 mutex_enter(&arc_prune_mtx);
3115 refcount_add(&p->p_refcnt, &arc_prune_list);
3116 list_insert_head(&arc_prune_list, p);
3117 mutex_exit(&arc_prune_mtx);
3123 arc_remove_prune_callback(arc_prune_t *p)
3125 mutex_enter(&arc_prune_mtx);
3126 list_remove(&arc_prune_list, p);
3127 if (refcount_remove(&p->p_refcnt, &arc_prune_list) == 0) {
3128 refcount_destroy(&p->p_refcnt);
3129 kmem_free(p, sizeof (*p));
3131 mutex_exit(&arc_prune_mtx);
3135 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
3137 ASSERT(buf->b_hdr != NULL);
3138 ASSERT(buf->b_hdr->b_state != arc_anon);
3139 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
3140 ASSERT(buf->b_efunc == NULL);
3141 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
3143 buf->b_efunc = func;
3144 buf->b_private = private;
3148 * This is used by the DMU to let the ARC know that a buffer is
3149 * being evicted, so the ARC should clean up. If this arc buf
3150 * is not yet in the evicted state, it will be put there.
3153 arc_buf_evict(arc_buf_t *buf)
3156 kmutex_t *hash_lock;
3159 mutex_enter(&buf->b_evict_lock);
3163 * We are in arc_do_user_evicts().
3165 ASSERT(buf->b_data == NULL);
3166 mutex_exit(&buf->b_evict_lock);
3168 } else if (buf->b_data == NULL) {
3169 arc_buf_t copy = *buf; /* structure assignment */
3171 * We are on the eviction list; process this buffer now
3172 * but let arc_do_user_evicts() do the reaping.
3174 buf->b_efunc = NULL;
3175 mutex_exit(&buf->b_evict_lock);
3176 VERIFY(copy.b_efunc(©) == 0);
3179 hash_lock = HDR_LOCK(hdr);
3180 mutex_enter(hash_lock);
3182 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3184 ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
3185 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3188 * Pull this buffer off of the hdr
3191 while (*bufp != buf)
3192 bufp = &(*bufp)->b_next;
3193 *bufp = buf->b_next;
3195 ASSERT(buf->b_data != NULL);
3196 arc_buf_destroy(buf, FALSE, FALSE);
3198 if (hdr->b_datacnt == 0) {
3199 arc_state_t *old_state = hdr->b_state;
3200 arc_state_t *evicted_state;
3202 ASSERT(hdr->b_buf == NULL);
3203 ASSERT(refcount_is_zero(&hdr->b_refcnt));
3206 (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
3208 mutex_enter(&old_state->arcs_mtx);
3209 mutex_enter(&evicted_state->arcs_mtx);
3211 arc_change_state(evicted_state, hdr, hash_lock);
3212 ASSERT(HDR_IN_HASH_TABLE(hdr));
3213 hdr->b_flags |= ARC_IN_HASH_TABLE;
3214 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3216 mutex_exit(&evicted_state->arcs_mtx);
3217 mutex_exit(&old_state->arcs_mtx);
3219 mutex_exit(hash_lock);
3220 mutex_exit(&buf->b_evict_lock);
3222 VERIFY(buf->b_efunc(buf) == 0);
3223 buf->b_efunc = NULL;
3224 buf->b_private = NULL;
3227 kmem_cache_free(buf_cache, buf);
3232 * Release this buffer from the cache. This must be done
3233 * after a read and prior to modifying the buffer contents.
3234 * If the buffer has more than one reference, we must make
3235 * a new hdr for the buffer.
3238 arc_release(arc_buf_t *buf, void *tag)
3241 kmutex_t *hash_lock = NULL;
3242 l2arc_buf_hdr_t *l2hdr;
3243 uint64_t buf_size = 0;
3246 * It would be nice to assert that if it's DMU metadata (level >
3247 * 0 || it's the dnode file), then it must be syncing context.
3248 * But we don't know that information at this level.
3251 mutex_enter(&buf->b_evict_lock);
3254 /* this buffer is not on any list */
3255 ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3257 if (hdr->b_state == arc_anon) {
3258 /* this buffer is already released */
3259 ASSERT(buf->b_efunc == NULL);
3261 hash_lock = HDR_LOCK(hdr);
3262 mutex_enter(hash_lock);
3264 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3267 l2hdr = hdr->b_l2hdr;
3269 mutex_enter(&l2arc_buflist_mtx);
3270 hdr->b_l2hdr = NULL;
3271 buf_size = hdr->b_size;
3275 * Do we have more than one buf?
3277 if (hdr->b_datacnt > 1) {
3278 arc_buf_hdr_t *nhdr;
3280 uint64_t blksz = hdr->b_size;
3281 uint64_t spa = hdr->b_spa;
3282 arc_buf_contents_t type = hdr->b_type;
3283 uint32_t flags = hdr->b_flags;
3285 ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3287 * Pull the data off of this hdr and attach it to
3288 * a new anonymous hdr.
3290 (void) remove_reference(hdr, hash_lock, tag);
3292 while (*bufp != buf)
3293 bufp = &(*bufp)->b_next;
3294 *bufp = buf->b_next;
3297 ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3298 atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3299 if (refcount_is_zero(&hdr->b_refcnt)) {
3300 uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3301 ASSERT3U(*size, >=, hdr->b_size);
3302 atomic_add_64(size, -hdr->b_size);
3304 hdr->b_datacnt -= 1;
3305 arc_cksum_verify(buf);
3307 mutex_exit(hash_lock);
3309 nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3310 nhdr->b_size = blksz;
3312 nhdr->b_type = type;
3314 nhdr->b_state = arc_anon;
3315 nhdr->b_arc_access = 0;
3316 nhdr->b_flags = flags & ARC_L2_WRITING;
3317 nhdr->b_l2hdr = NULL;
3318 nhdr->b_datacnt = 1;
3319 nhdr->b_freeze_cksum = NULL;
3320 (void) refcount_add(&nhdr->b_refcnt, tag);
3322 mutex_exit(&buf->b_evict_lock);
3323 atomic_add_64(&arc_anon->arcs_size, blksz);
3325 mutex_exit(&buf->b_evict_lock);
3326 ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3327 ASSERT(!list_link_active(&hdr->b_arc_node));
3328 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3329 if (hdr->b_state != arc_anon)
3330 arc_change_state(arc_anon, hdr, hash_lock);
3331 hdr->b_arc_access = 0;
3333 mutex_exit(hash_lock);
3335 buf_discard_identity(hdr);
3338 buf->b_efunc = NULL;
3339 buf->b_private = NULL;
3342 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3343 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3344 ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3345 mutex_exit(&l2arc_buflist_mtx);
3350 * Release this buffer. If it does not match the provided BP, fill it
3351 * with that block's contents.
3355 arc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa,
3358 arc_release(buf, tag);
3363 arc_released(arc_buf_t *buf)
3367 mutex_enter(&buf->b_evict_lock);
3368 released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3369 mutex_exit(&buf->b_evict_lock);
3374 arc_has_callback(arc_buf_t *buf)
3378 mutex_enter(&buf->b_evict_lock);
3379 callback = (buf->b_efunc != NULL);
3380 mutex_exit(&buf->b_evict_lock);
3386 arc_referenced(arc_buf_t *buf)
3390 mutex_enter(&buf->b_evict_lock);
3391 referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3392 mutex_exit(&buf->b_evict_lock);
3393 return (referenced);
3398 arc_write_ready(zio_t *zio)
3400 arc_write_callback_t *callback = zio->io_private;
3401 arc_buf_t *buf = callback->awcb_buf;
3402 arc_buf_hdr_t *hdr = buf->b_hdr;
3404 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3405 callback->awcb_ready(zio, buf, callback->awcb_private);
3408 * If the IO is already in progress, then this is a re-write
3409 * attempt, so we need to thaw and re-compute the cksum.
3410 * It is the responsibility of the callback to handle the
3411 * accounting for any re-write attempt.
3413 if (HDR_IO_IN_PROGRESS(hdr)) {
3414 mutex_enter(&hdr->b_freeze_lock);
3415 if (hdr->b_freeze_cksum != NULL) {
3416 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3417 hdr->b_freeze_cksum = NULL;
3419 mutex_exit(&hdr->b_freeze_lock);
3421 arc_cksum_compute(buf, B_FALSE);
3422 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3426 arc_write_done(zio_t *zio)
3428 arc_write_callback_t *callback = zio->io_private;
3429 arc_buf_t *buf = callback->awcb_buf;
3430 arc_buf_hdr_t *hdr = buf->b_hdr;
3432 ASSERT(hdr->b_acb == NULL);
3434 if (zio->io_error == 0) {
3435 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3436 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3437 hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3439 ASSERT(BUF_EMPTY(hdr));
3443 * If the block to be written was all-zero, we may have
3444 * compressed it away. In this case no write was performed
3445 * so there will be no dva/birth/checksum. The buffer must
3446 * therefore remain anonymous (and uncached).
3448 if (!BUF_EMPTY(hdr)) {
3449 arc_buf_hdr_t *exists;
3450 kmutex_t *hash_lock;
3452 ASSERT(zio->io_error == 0);
3454 arc_cksum_verify(buf);
3456 exists = buf_hash_insert(hdr, &hash_lock);
3459 * This can only happen if we overwrite for
3460 * sync-to-convergence, because we remove
3461 * buffers from the hash table when we arc_free().
3463 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3464 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3465 panic("bad overwrite, hdr=%p exists=%p",
3466 (void *)hdr, (void *)exists);
3467 ASSERT(refcount_is_zero(&exists->b_refcnt));
3468 arc_change_state(arc_anon, exists, hash_lock);
3469 mutex_exit(hash_lock);
3470 arc_hdr_destroy(exists);
3471 exists = buf_hash_insert(hdr, &hash_lock);
3472 ASSERT3P(exists, ==, NULL);
3475 ASSERT(hdr->b_datacnt == 1);
3476 ASSERT(hdr->b_state == arc_anon);
3477 ASSERT(BP_GET_DEDUP(zio->io_bp));
3478 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3481 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3482 /* if it's not anon, we are doing a scrub */
3483 if (!exists && hdr->b_state == arc_anon)
3484 arc_access(hdr, hash_lock);
3485 mutex_exit(hash_lock);
3487 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3490 ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3491 callback->awcb_done(zio, buf, callback->awcb_private);
3493 kmem_free(callback, sizeof (arc_write_callback_t));
3497 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3498 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
3499 arc_done_func_t *ready, arc_done_func_t *done, void *private,
3500 int priority, int zio_flags, const zbookmark_t *zb)
3502 arc_buf_hdr_t *hdr = buf->b_hdr;
3503 arc_write_callback_t *callback;
3506 ASSERT(ready != NULL);
3507 ASSERT(done != NULL);
3508 ASSERT(!HDR_IO_ERROR(hdr));
3509 ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3510 ASSERT(hdr->b_acb == NULL);
3512 hdr->b_flags |= ARC_L2CACHE;
3513 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
3514 callback->awcb_ready = ready;
3515 callback->awcb_done = done;
3516 callback->awcb_private = private;
3517 callback->awcb_buf = buf;
3519 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3520 arc_write_ready, arc_write_done, callback, priority, zio_flags, zb);
3526 arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg)
3529 uint64_t available_memory = ptob(freemem);
3530 static uint64_t page_load = 0;
3531 static uint64_t last_txg = 0;
3535 MIN(available_memory, vmem_size(heap_arena, VMEM_FREE));
3537 if (available_memory >= zfs_write_limit_max)
3540 if (txg > last_txg) {
3545 * If we are in pageout, we know that memory is already tight,
3546 * the arc is already going to be evicting, so we just want to
3547 * continue to let page writes occur as quickly as possible.
3549 if (curproc == proc_pageout) {
3550 if (page_load > MAX(ptob(minfree), available_memory) / 4)
3552 /* Note: reserve is inflated, so we deflate */
3553 page_load += reserve / 8;
3555 } else if (page_load > 0 && arc_reclaim_needed()) {
3556 /* memory is low, delay before restarting */
3557 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3562 if (arc_size > arc_c_min) {
3563 uint64_t evictable_memory =
3564 arc_mru->arcs_lsize[ARC_BUFC_DATA] +
3565 arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
3566 arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
3567 arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
3568 available_memory += MIN(evictable_memory, arc_size - arc_c_min);
3571 if (inflight_data > available_memory / 4) {
3572 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3580 arc_tempreserve_clear(uint64_t reserve)
3582 atomic_add_64(&arc_tempreserve, -reserve);
3583 ASSERT((int64_t)arc_tempreserve >= 0);
3587 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3594 * Once in a while, fail for no reason. Everything should cope.
3596 if (spa_get_random(10000) == 0) {
3597 dprintf("forcing random failure\n");
3601 if (reserve > arc_c/4 && !arc_no_grow)
3602 arc_c = MIN(arc_c_max, reserve * 4);
3603 if (reserve > arc_c)
3607 * Don't count loaned bufs as in flight dirty data to prevent long
3608 * network delays from blocking transactions that are ready to be
3609 * assigned to a txg.
3611 anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3614 * Writes will, almost always, require additional memory allocations
3615 * in order to compress/encrypt/etc the data. We therefor need to
3616 * make sure that there is sufficient available memory for this.
3618 if ((error = arc_memory_throttle(reserve, anon_size, txg)))
3622 * Throttle writes when the amount of dirty data in the cache
3623 * gets too large. We try to keep the cache less than half full
3624 * of dirty blocks so that our sync times don't grow too large.
3625 * Note: if two requests come in concurrently, we might let them
3626 * both succeed, when one of them should fail. Not a huge deal.
3629 if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3630 anon_size > arc_c / 4) {
3631 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3632 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3633 arc_tempreserve>>10,
3634 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3635 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3636 reserve>>10, arc_c>>10);
3639 atomic_add_64(&arc_tempreserve, reserve);
3646 mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3647 cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3649 /* Convert seconds to clock ticks */
3650 arc_min_prefetch_lifespan = 1 * hz;
3652 /* Start out with 1/8 of all memory */
3653 arc_c = physmem * PAGESIZE / 8;
3657 * On architectures where the physical memory can be larger
3658 * than the addressable space (intel in 32-bit mode), we may
3659 * need to limit the cache to 1/8 of VM size.
3661 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3663 * Register a shrinker to support synchronous (direct) memory
3664 * reclaim from the arc. This is done to prevent kswapd from
3665 * swapping out pages when it is preferable to shrink the arc.
3667 spl_register_shrinker(&arc_shrinker);
3670 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3671 arc_c_min = MAX(arc_c / 4, 64<<20);
3672 /* set max to 1/2 of all memory, or all but 4GB, whichever is more */
3673 if (arc_c * 8 >= ((uint64_t)4<<30))
3674 arc_c_max = (arc_c * 8) - ((uint64_t)4<<30);
3676 arc_c_max = arc_c_min;
3677 arc_c_max = MAX(arc_c * 4, arc_c_max);
3680 * Allow the tunables to override our calculations if they are
3681 * reasonable (ie. over 64MB)
3683 if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
3684 arc_c_max = zfs_arc_max;
3685 if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
3686 arc_c_min = zfs_arc_min;
3689 arc_p = (arc_c >> 1);
3691 /* limit meta-data to 1/4 of the arc capacity */
3692 arc_meta_limit = arc_c_max / 4;
3695 /* Allow the tunable to override if it is reasonable */
3696 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
3697 arc_meta_limit = zfs_arc_meta_limit;
3699 if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3700 arc_c_min = arc_meta_limit / 2;
3702 if (zfs_arc_grow_retry > 0)
3703 arc_grow_retry = zfs_arc_grow_retry;
3705 if (zfs_arc_shrink_shift > 0)
3706 arc_shrink_shift = zfs_arc_shrink_shift;
3708 if (zfs_arc_p_min_shift > 0)
3709 arc_p_min_shift = zfs_arc_p_min_shift;
3711 if (zfs_arc_meta_prune > 0)
3712 arc_meta_prune = zfs_arc_meta_prune;
3714 /* if kmem_flags are set, lets try to use less memory */
3715 if (kmem_debugging())
3717 if (arc_c < arc_c_min)
3720 arc_anon = &ARC_anon;
3722 arc_mru_ghost = &ARC_mru_ghost;
3724 arc_mfu_ghost = &ARC_mfu_ghost;
3725 arc_l2c_only = &ARC_l2c_only;
3728 mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3729 mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3730 mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3731 mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3732 mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3733 mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3735 list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
3736 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3737 list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
3738 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3739 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
3740 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3741 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
3742 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3743 list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
3744 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3745 list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
3746 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3747 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
3748 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3749 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
3750 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3751 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
3752 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3753 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
3754 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3758 arc_thread_exit = 0;
3759 list_create(&arc_prune_list, sizeof (arc_prune_t),
3760 offsetof(arc_prune_t, p_node));
3761 arc_eviction_list = NULL;
3762 mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL);
3763 mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
3764 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3766 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
3767 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3769 if (arc_ksp != NULL) {
3770 arc_ksp->ks_data = &arc_stats;
3771 kstat_install(arc_ksp);
3774 (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
3775 TS_RUN, minclsyspri);
3780 if (zfs_write_limit_max == 0)
3781 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
3783 zfs_write_limit_shift = 0;
3784 mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL);
3792 mutex_enter(&arc_reclaim_thr_lock);
3794 spl_unregister_shrinker(&arc_shrinker);
3795 #endif /* _KERNEL */
3797 arc_thread_exit = 1;
3798 while (arc_thread_exit != 0)
3799 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3800 mutex_exit(&arc_reclaim_thr_lock);
3806 if (arc_ksp != NULL) {
3807 kstat_delete(arc_ksp);
3811 mutex_enter(&arc_prune_mtx);
3812 while ((p = list_head(&arc_prune_list)) != NULL) {
3813 list_remove(&arc_prune_list, p);
3814 refcount_remove(&p->p_refcnt, &arc_prune_list);
3815 refcount_destroy(&p->p_refcnt);
3816 kmem_free(p, sizeof (*p));
3818 mutex_exit(&arc_prune_mtx);
3820 list_destroy(&arc_prune_list);
3821 mutex_destroy(&arc_prune_mtx);
3822 mutex_destroy(&arc_eviction_mtx);
3823 mutex_destroy(&arc_reclaim_thr_lock);
3824 cv_destroy(&arc_reclaim_thr_cv);
3826 list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
3827 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
3828 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
3829 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
3830 list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
3831 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
3832 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
3833 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3835 mutex_destroy(&arc_anon->arcs_mtx);
3836 mutex_destroy(&arc_mru->arcs_mtx);
3837 mutex_destroy(&arc_mru_ghost->arcs_mtx);
3838 mutex_destroy(&arc_mfu->arcs_mtx);
3839 mutex_destroy(&arc_mfu_ghost->arcs_mtx);
3840 mutex_destroy(&arc_l2c_only->arcs_mtx);
3842 mutex_destroy(&zfs_write_limit_lock);
3846 ASSERT(arc_loaned_bytes == 0);
3852 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3853 * It uses dedicated storage devices to hold cached data, which are populated
3854 * using large infrequent writes. The main role of this cache is to boost
3855 * the performance of random read workloads. The intended L2ARC devices
3856 * include short-stroked disks, solid state disks, and other media with
3857 * substantially faster read latency than disk.
3859 * +-----------------------+
3861 * +-----------------------+
3864 * l2arc_feed_thread() arc_read()
3868 * +---------------+ |
3870 * +---------------+ |
3875 * +-------+ +-------+
3877 * | cache | | cache |
3878 * +-------+ +-------+
3879 * +=========+ .-----.
3880 * : L2ARC : |-_____-|
3881 * : devices : | Disks |
3882 * +=========+ `-_____-'
3884 * Read requests are satisfied from the following sources, in order:
3887 * 2) vdev cache of L2ARC devices
3889 * 4) vdev cache of disks
3892 * Some L2ARC device types exhibit extremely slow write performance.
3893 * To accommodate for this there are some significant differences between
3894 * the L2ARC and traditional cache design:
3896 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
3897 * the ARC behave as usual, freeing buffers and placing headers on ghost
3898 * lists. The ARC does not send buffers to the L2ARC during eviction as
3899 * this would add inflated write latencies for all ARC memory pressure.
3901 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
3902 * It does this by periodically scanning buffers from the eviction-end of
3903 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
3904 * not already there. It scans until a headroom of buffers is satisfied,
3905 * which itself is a buffer for ARC eviction. The thread that does this is
3906 * l2arc_feed_thread(), illustrated below; example sizes are included to
3907 * provide a better sense of ratio than this diagram:
3910 * +---------------------+----------+
3911 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
3912 * +---------------------+----------+ | o L2ARC eligible
3913 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
3914 * +---------------------+----------+ |
3915 * 15.9 Gbytes ^ 32 Mbytes |
3917 * l2arc_feed_thread()
3919 * l2arc write hand <--[oooo]--'
3923 * +==============================+
3924 * L2ARC dev |####|#|###|###| |####| ... |
3925 * +==============================+
3928 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
3929 * evicted, then the L2ARC has cached a buffer much sooner than it probably
3930 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
3931 * safe to say that this is an uncommon case, since buffers at the end of
3932 * the ARC lists have moved there due to inactivity.
3934 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
3935 * then the L2ARC simply misses copying some buffers. This serves as a
3936 * pressure valve to prevent heavy read workloads from both stalling the ARC
3937 * with waits and clogging the L2ARC with writes. This also helps prevent
3938 * the potential for the L2ARC to churn if it attempts to cache content too
3939 * quickly, such as during backups of the entire pool.
3941 * 5. After system boot and before the ARC has filled main memory, there are
3942 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
3943 * lists can remain mostly static. Instead of searching from tail of these
3944 * lists as pictured, the l2arc_feed_thread() will search from the list heads
3945 * for eligible buffers, greatly increasing its chance of finding them.
3947 * The L2ARC device write speed is also boosted during this time so that
3948 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
3949 * there are no L2ARC reads, and no fear of degrading read performance
3950 * through increased writes.
3952 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
3953 * the vdev queue can aggregate them into larger and fewer writes. Each
3954 * device is written to in a rotor fashion, sweeping writes through
3955 * available space then repeating.
3957 * 7. The L2ARC does not store dirty content. It never needs to flush
3958 * write buffers back to disk based storage.
3960 * 8. If an ARC buffer is written (and dirtied) which also exists in the
3961 * L2ARC, the now stale L2ARC buffer is immediately dropped.
3963 * The performance of the L2ARC can be tweaked by a number of tunables, which
3964 * may be necessary for different workloads:
3966 * l2arc_write_max max write bytes per interval
3967 * l2arc_write_boost extra write bytes during device warmup
3968 * l2arc_noprefetch skip caching prefetched buffers
3969 * l2arc_headroom number of max device writes to precache
3970 * l2arc_feed_secs seconds between L2ARC writing
3972 * Tunables may be removed or added as future performance improvements are
3973 * integrated, and also may become zpool properties.
3975 * There are three key functions that control how the L2ARC warms up:
3977 * l2arc_write_eligible() check if a buffer is eligible to cache
3978 * l2arc_write_size() calculate how much to write
3979 * l2arc_write_interval() calculate sleep delay between writes
3981 * These three functions determine what to write, how much, and how quickly
3986 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
3989 * A buffer is *not* eligible for the L2ARC if it:
3990 * 1. belongs to a different spa.
3991 * 2. is already cached on the L2ARC.
3992 * 3. has an I/O in progress (it may be an incomplete read).
3993 * 4. is flagged not eligible (zfs property).
3995 if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
3996 HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
4003 l2arc_write_size(l2arc_dev_t *dev)
4007 size = dev->l2ad_write;
4009 if (arc_warm == B_FALSE)
4010 size += dev->l2ad_boost;
4017 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
4019 clock_t interval, next, now;
4022 * If the ARC lists are busy, increase our write rate; if the
4023 * lists are stale, idle back. This is achieved by checking
4024 * how much we previously wrote - if it was more than half of
4025 * what we wanted, schedule the next write much sooner.
4027 if (l2arc_feed_again && wrote > (wanted / 2))
4028 interval = (hz * l2arc_feed_min_ms) / 1000;
4030 interval = hz * l2arc_feed_secs;
4032 now = ddi_get_lbolt();
4033 next = MAX(now, MIN(now + interval, began + interval));
4039 l2arc_hdr_stat_add(void)
4041 ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
4042 ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
4046 l2arc_hdr_stat_remove(void)
4048 ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
4049 ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
4053 * Cycle through L2ARC devices. This is how L2ARC load balances.
4054 * If a device is returned, this also returns holding the spa config lock.
4056 static l2arc_dev_t *
4057 l2arc_dev_get_next(void)
4059 l2arc_dev_t *first, *next = NULL;
4062 * Lock out the removal of spas (spa_namespace_lock), then removal
4063 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
4064 * both locks will be dropped and a spa config lock held instead.
4066 mutex_enter(&spa_namespace_lock);
4067 mutex_enter(&l2arc_dev_mtx);
4069 /* if there are no vdevs, there is nothing to do */
4070 if (l2arc_ndev == 0)
4074 next = l2arc_dev_last;
4076 /* loop around the list looking for a non-faulted vdev */
4078 next = list_head(l2arc_dev_list);
4080 next = list_next(l2arc_dev_list, next);
4082 next = list_head(l2arc_dev_list);
4085 /* if we have come back to the start, bail out */
4088 else if (next == first)
4091 } while (vdev_is_dead(next->l2ad_vdev));
4093 /* if we were unable to find any usable vdevs, return NULL */
4094 if (vdev_is_dead(next->l2ad_vdev))
4097 l2arc_dev_last = next;
4100 mutex_exit(&l2arc_dev_mtx);
4103 * Grab the config lock to prevent the 'next' device from being
4104 * removed while we are writing to it.
4107 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
4108 mutex_exit(&spa_namespace_lock);
4114 * Free buffers that were tagged for destruction.
4117 l2arc_do_free_on_write(void)
4120 l2arc_data_free_t *df, *df_prev;
4122 mutex_enter(&l2arc_free_on_write_mtx);
4123 buflist = l2arc_free_on_write;
4125 for (df = list_tail(buflist); df; df = df_prev) {
4126 df_prev = list_prev(buflist, df);
4127 ASSERT(df->l2df_data != NULL);
4128 ASSERT(df->l2df_func != NULL);
4129 df->l2df_func(df->l2df_data, df->l2df_size);
4130 list_remove(buflist, df);
4131 kmem_free(df, sizeof (l2arc_data_free_t));
4134 mutex_exit(&l2arc_free_on_write_mtx);
4138 * A write to a cache device has completed. Update all headers to allow
4139 * reads from these buffers to begin.
4142 l2arc_write_done(zio_t *zio)
4144 l2arc_write_callback_t *cb;
4147 arc_buf_hdr_t *head, *ab, *ab_prev;
4148 l2arc_buf_hdr_t *abl2;
4149 kmutex_t *hash_lock;
4151 cb = zio->io_private;
4153 dev = cb->l2wcb_dev;
4154 ASSERT(dev != NULL);
4155 head = cb->l2wcb_head;
4156 ASSERT(head != NULL);
4157 buflist = dev->l2ad_buflist;
4158 ASSERT(buflist != NULL);
4159 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
4160 l2arc_write_callback_t *, cb);
4162 if (zio->io_error != 0)
4163 ARCSTAT_BUMP(arcstat_l2_writes_error);
4165 mutex_enter(&l2arc_buflist_mtx);
4168 * All writes completed, or an error was hit.
4170 for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
4171 ab_prev = list_prev(buflist, ab);
4173 hash_lock = HDR_LOCK(ab);
4174 if (!mutex_tryenter(hash_lock)) {
4176 * This buffer misses out. It may be in a stage
4177 * of eviction. Its ARC_L2_WRITING flag will be
4178 * left set, denying reads to this buffer.
4180 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
4184 if (zio->io_error != 0) {
4186 * Error - drop L2ARC entry.
4188 list_remove(buflist, ab);
4191 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4192 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4196 * Allow ARC to begin reads to this L2ARC entry.
4198 ab->b_flags &= ~ARC_L2_WRITING;
4200 mutex_exit(hash_lock);
4203 atomic_inc_64(&l2arc_writes_done);
4204 list_remove(buflist, head);
4205 kmem_cache_free(hdr_cache, head);
4206 mutex_exit(&l2arc_buflist_mtx);
4208 l2arc_do_free_on_write();
4210 kmem_free(cb, sizeof (l2arc_write_callback_t));
4214 * A read to a cache device completed. Validate buffer contents before
4215 * handing over to the regular ARC routines.
4218 l2arc_read_done(zio_t *zio)
4220 l2arc_read_callback_t *cb;
4223 kmutex_t *hash_lock;
4226 ASSERT(zio->io_vd != NULL);
4227 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
4229 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
4231 cb = zio->io_private;
4233 buf = cb->l2rcb_buf;
4234 ASSERT(buf != NULL);
4236 hash_lock = HDR_LOCK(buf->b_hdr);
4237 mutex_enter(hash_lock);
4239 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4242 * Check this survived the L2ARC journey.
4244 equal = arc_cksum_equal(buf);
4245 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4246 mutex_exit(hash_lock);
4247 zio->io_private = buf;
4248 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
4249 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
4252 mutex_exit(hash_lock);
4254 * Buffer didn't survive caching. Increment stats and
4255 * reissue to the original storage device.
4257 if (zio->io_error != 0) {
4258 ARCSTAT_BUMP(arcstat_l2_io_error);
4260 zio->io_error = EIO;
4263 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4266 * If there's no waiter, issue an async i/o to the primary
4267 * storage now. If there *is* a waiter, the caller must
4268 * issue the i/o in a context where it's OK to block.
4270 if (zio->io_waiter == NULL) {
4271 zio_t *pio = zio_unique_parent(zio);
4273 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4275 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4276 buf->b_data, zio->io_size, arc_read_done, buf,
4277 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4281 kmem_free(cb, sizeof (l2arc_read_callback_t));
4285 * This is the list priority from which the L2ARC will search for pages to
4286 * cache. This is used within loops (0..3) to cycle through lists in the
4287 * desired order. This order can have a significant effect on cache
4290 * Currently the metadata lists are hit first, MFU then MRU, followed by
4291 * the data lists. This function returns a locked list, and also returns
4295 l2arc_list_locked(int list_num, kmutex_t **lock)
4297 list_t *list = NULL;
4299 ASSERT(list_num >= 0 && list_num <= 3);
4303 list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
4304 *lock = &arc_mfu->arcs_mtx;
4307 list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
4308 *lock = &arc_mru->arcs_mtx;
4311 list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
4312 *lock = &arc_mfu->arcs_mtx;
4315 list = &arc_mru->arcs_list[ARC_BUFC_DATA];
4316 *lock = &arc_mru->arcs_mtx;
4320 ASSERT(!(MUTEX_HELD(*lock)));
4326 * Evict buffers from the device write hand to the distance specified in
4327 * bytes. This distance may span populated buffers, it may span nothing.
4328 * This is clearing a region on the L2ARC device ready for writing.
4329 * If the 'all' boolean is set, every buffer is evicted.
4332 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4335 l2arc_buf_hdr_t *abl2;
4336 arc_buf_hdr_t *ab, *ab_prev;
4337 kmutex_t *hash_lock;
4340 buflist = dev->l2ad_buflist;
4342 if (buflist == NULL)
4345 if (!all && dev->l2ad_first) {
4347 * This is the first sweep through the device. There is
4353 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4355 * When nearing the end of the device, evict to the end
4356 * before the device write hand jumps to the start.
4358 taddr = dev->l2ad_end;
4360 taddr = dev->l2ad_hand + distance;
4362 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4363 uint64_t, taddr, boolean_t, all);
4366 mutex_enter(&l2arc_buflist_mtx);
4367 for (ab = list_tail(buflist); ab; ab = ab_prev) {
4368 ab_prev = list_prev(buflist, ab);
4370 hash_lock = HDR_LOCK(ab);
4371 if (!mutex_tryenter(hash_lock)) {
4373 * Missed the hash lock. Retry.
4375 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4376 mutex_exit(&l2arc_buflist_mtx);
4377 mutex_enter(hash_lock);
4378 mutex_exit(hash_lock);
4382 if (HDR_L2_WRITE_HEAD(ab)) {
4384 * We hit a write head node. Leave it for
4385 * l2arc_write_done().
4387 list_remove(buflist, ab);
4388 mutex_exit(hash_lock);
4392 if (!all && ab->b_l2hdr != NULL &&
4393 (ab->b_l2hdr->b_daddr > taddr ||
4394 ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4396 * We've evicted to the target address,
4397 * or the end of the device.
4399 mutex_exit(hash_lock);
4403 if (HDR_FREE_IN_PROGRESS(ab)) {
4405 * Already on the path to destruction.
4407 mutex_exit(hash_lock);
4411 if (ab->b_state == arc_l2c_only) {
4412 ASSERT(!HDR_L2_READING(ab));
4414 * This doesn't exist in the ARC. Destroy.
4415 * arc_hdr_destroy() will call list_remove()
4416 * and decrement arcstat_l2_size.
4418 arc_change_state(arc_anon, ab, hash_lock);
4419 arc_hdr_destroy(ab);
4422 * Invalidate issued or about to be issued
4423 * reads, since we may be about to write
4424 * over this location.
4426 if (HDR_L2_READING(ab)) {
4427 ARCSTAT_BUMP(arcstat_l2_evict_reading);
4428 ab->b_flags |= ARC_L2_EVICTED;
4432 * Tell ARC this no longer exists in L2ARC.
4434 if (ab->b_l2hdr != NULL) {
4437 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4438 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4440 list_remove(buflist, ab);
4443 * This may have been leftover after a
4446 ab->b_flags &= ~ARC_L2_WRITING;
4448 mutex_exit(hash_lock);
4450 mutex_exit(&l2arc_buflist_mtx);
4452 vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
4453 dev->l2ad_evict = taddr;
4457 * Find and write ARC buffers to the L2ARC device.
4459 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4460 * for reading until they have completed writing.
4463 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
4465 arc_buf_hdr_t *ab, *ab_prev, *head;
4466 l2arc_buf_hdr_t *hdrl2;
4468 uint64_t passed_sz, write_sz, buf_sz, headroom;
4470 kmutex_t *hash_lock, *list_lock = NULL;
4471 boolean_t have_lock, full;
4472 l2arc_write_callback_t *cb;
4474 uint64_t guid = spa_guid(spa);
4477 ASSERT(dev->l2ad_vdev != NULL);
4482 head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4483 head->b_flags |= ARC_L2_WRITE_HEAD;
4486 * Copy buffers for L2ARC writing.
4488 mutex_enter(&l2arc_buflist_mtx);
4489 for (try = 0; try <= 3; try++) {
4490 list = l2arc_list_locked(try, &list_lock);
4494 * L2ARC fast warmup.
4496 * Until the ARC is warm and starts to evict, read from the
4497 * head of the ARC lists rather than the tail.
4499 headroom = target_sz * l2arc_headroom;
4500 if (arc_warm == B_FALSE)
4501 ab = list_head(list);
4503 ab = list_tail(list);
4505 for (; ab; ab = ab_prev) {
4506 if (arc_warm == B_FALSE)
4507 ab_prev = list_next(list, ab);
4509 ab_prev = list_prev(list, ab);
4511 hash_lock = HDR_LOCK(ab);
4512 have_lock = MUTEX_HELD(hash_lock);
4513 if (!have_lock && !mutex_tryenter(hash_lock)) {
4515 * Skip this buffer rather than waiting.
4520 passed_sz += ab->b_size;
4521 if (passed_sz > headroom) {
4525 mutex_exit(hash_lock);
4529 if (!l2arc_write_eligible(guid, ab)) {
4530 mutex_exit(hash_lock);
4534 if ((write_sz + ab->b_size) > target_sz) {
4536 mutex_exit(hash_lock);
4542 * Insert a dummy header on the buflist so
4543 * l2arc_write_done() can find where the
4544 * write buffers begin without searching.
4546 list_insert_head(dev->l2ad_buflist, head);
4549 sizeof (l2arc_write_callback_t), KM_SLEEP);
4550 cb->l2wcb_dev = dev;
4551 cb->l2wcb_head = head;
4552 pio = zio_root(spa, l2arc_write_done, cb,
4557 * Create and add a new L2ARC header.
4559 hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
4561 hdrl2->b_daddr = dev->l2ad_hand;
4563 ab->b_flags |= ARC_L2_WRITING;
4564 ab->b_l2hdr = hdrl2;
4565 list_insert_head(dev->l2ad_buflist, ab);
4566 buf_data = ab->b_buf->b_data;
4567 buf_sz = ab->b_size;
4570 * Compute and store the buffer cksum before
4571 * writing. On debug the cksum is verified first.
4573 arc_cksum_verify(ab->b_buf);
4574 arc_cksum_compute(ab->b_buf, B_TRUE);
4576 mutex_exit(hash_lock);
4578 wzio = zio_write_phys(pio, dev->l2ad_vdev,
4579 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
4580 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
4581 ZIO_FLAG_CANFAIL, B_FALSE);
4583 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
4585 (void) zio_nowait(wzio);
4588 * Keep the clock hand suitably device-aligned.
4590 buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
4593 dev->l2ad_hand += buf_sz;
4596 mutex_exit(list_lock);
4601 mutex_exit(&l2arc_buflist_mtx);
4604 ASSERT3U(write_sz, ==, 0);
4605 kmem_cache_free(hdr_cache, head);
4609 ASSERT3U(write_sz, <=, target_sz);
4610 ARCSTAT_BUMP(arcstat_l2_writes_sent);
4611 ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz);
4612 ARCSTAT_INCR(arcstat_l2_size, write_sz);
4613 vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0);
4616 * Bump device hand to the device start if it is approaching the end.
4617 * l2arc_evict() will already have evicted ahead for this case.
4619 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
4620 vdev_space_update(dev->l2ad_vdev,
4621 dev->l2ad_end - dev->l2ad_hand, 0, 0);
4622 dev->l2ad_hand = dev->l2ad_start;
4623 dev->l2ad_evict = dev->l2ad_start;
4624 dev->l2ad_first = B_FALSE;
4627 dev->l2ad_writing = B_TRUE;
4628 (void) zio_wait(pio);
4629 dev->l2ad_writing = B_FALSE;
4635 * This thread feeds the L2ARC at regular intervals. This is the beating
4636 * heart of the L2ARC.
4639 l2arc_feed_thread(void)
4644 uint64_t size, wrote;
4645 clock_t begin, next = ddi_get_lbolt();
4647 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
4649 mutex_enter(&l2arc_feed_thr_lock);
4651 while (l2arc_thread_exit == 0) {
4652 CALLB_CPR_SAFE_BEGIN(&cpr);
4653 (void) cv_timedwait_interruptible(&l2arc_feed_thr_cv,
4654 &l2arc_feed_thr_lock, next);
4655 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
4656 next = ddi_get_lbolt() + hz;
4659 * Quick check for L2ARC devices.
4661 mutex_enter(&l2arc_dev_mtx);
4662 if (l2arc_ndev == 0) {
4663 mutex_exit(&l2arc_dev_mtx);
4666 mutex_exit(&l2arc_dev_mtx);
4667 begin = ddi_get_lbolt();
4670 * This selects the next l2arc device to write to, and in
4671 * doing so the next spa to feed from: dev->l2ad_spa. This
4672 * will return NULL if there are now no l2arc devices or if
4673 * they are all faulted.
4675 * If a device is returned, its spa's config lock is also
4676 * held to prevent device removal. l2arc_dev_get_next()
4677 * will grab and release l2arc_dev_mtx.
4679 if ((dev = l2arc_dev_get_next()) == NULL)
4682 spa = dev->l2ad_spa;
4683 ASSERT(spa != NULL);
4686 * If the pool is read-only then force the feed thread to
4687 * sleep a little longer.
4689 if (!spa_writeable(spa)) {
4690 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
4691 spa_config_exit(spa, SCL_L2ARC, dev);
4696 * Avoid contributing to memory pressure.
4698 if (arc_reclaim_needed()) {
4699 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
4700 spa_config_exit(spa, SCL_L2ARC, dev);
4704 ARCSTAT_BUMP(arcstat_l2_feeds);
4706 size = l2arc_write_size(dev);
4709 * Evict L2ARC buffers that will be overwritten.
4711 l2arc_evict(dev, size, B_FALSE);
4714 * Write ARC buffers.
4716 wrote = l2arc_write_buffers(spa, dev, size);
4719 * Calculate interval between writes.
4721 next = l2arc_write_interval(begin, size, wrote);
4722 spa_config_exit(spa, SCL_L2ARC, dev);
4725 l2arc_thread_exit = 0;
4726 cv_broadcast(&l2arc_feed_thr_cv);
4727 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
4732 l2arc_vdev_present(vdev_t *vd)
4736 mutex_enter(&l2arc_dev_mtx);
4737 for (dev = list_head(l2arc_dev_list); dev != NULL;
4738 dev = list_next(l2arc_dev_list, dev)) {
4739 if (dev->l2ad_vdev == vd)
4742 mutex_exit(&l2arc_dev_mtx);
4744 return (dev != NULL);
4748 * Add a vdev for use by the L2ARC. By this point the spa has already
4749 * validated the vdev and opened it.
4752 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
4754 l2arc_dev_t *adddev;
4756 ASSERT(!l2arc_vdev_present(vd));
4759 * Create a new l2arc device entry.
4761 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
4762 adddev->l2ad_spa = spa;
4763 adddev->l2ad_vdev = vd;
4764 adddev->l2ad_write = l2arc_write_max;
4765 adddev->l2ad_boost = l2arc_write_boost;
4766 adddev->l2ad_start = VDEV_LABEL_START_SIZE;
4767 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
4768 adddev->l2ad_hand = adddev->l2ad_start;
4769 adddev->l2ad_evict = adddev->l2ad_start;
4770 adddev->l2ad_first = B_TRUE;
4771 adddev->l2ad_writing = B_FALSE;
4772 list_link_init(&adddev->l2ad_node);
4773 ASSERT3U(adddev->l2ad_write, >, 0);
4776 * This is a list of all ARC buffers that are still valid on the
4779 adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
4780 list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
4781 offsetof(arc_buf_hdr_t, b_l2node));
4783 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
4786 * Add device to global list
4788 mutex_enter(&l2arc_dev_mtx);
4789 list_insert_head(l2arc_dev_list, adddev);
4790 atomic_inc_64(&l2arc_ndev);
4791 mutex_exit(&l2arc_dev_mtx);
4795 * Remove a vdev from the L2ARC.
4798 l2arc_remove_vdev(vdev_t *vd)
4800 l2arc_dev_t *dev, *nextdev, *remdev = NULL;
4803 * Find the device by vdev
4805 mutex_enter(&l2arc_dev_mtx);
4806 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
4807 nextdev = list_next(l2arc_dev_list, dev);
4808 if (vd == dev->l2ad_vdev) {
4813 ASSERT(remdev != NULL);
4816 * Remove device from global list
4818 list_remove(l2arc_dev_list, remdev);
4819 l2arc_dev_last = NULL; /* may have been invalidated */
4820 atomic_dec_64(&l2arc_ndev);
4821 mutex_exit(&l2arc_dev_mtx);
4824 * Clear all buflists and ARC references. L2ARC device flush.
4826 l2arc_evict(remdev, 0, B_TRUE);
4827 list_destroy(remdev->l2ad_buflist);
4828 kmem_free(remdev->l2ad_buflist, sizeof (list_t));
4829 kmem_free(remdev, sizeof (l2arc_dev_t));
4835 l2arc_thread_exit = 0;
4837 l2arc_writes_sent = 0;
4838 l2arc_writes_done = 0;
4840 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
4841 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
4842 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
4843 mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
4844 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
4846 l2arc_dev_list = &L2ARC_dev_list;
4847 l2arc_free_on_write = &L2ARC_free_on_write;
4848 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
4849 offsetof(l2arc_dev_t, l2ad_node));
4850 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
4851 offsetof(l2arc_data_free_t, l2df_list_node));
4858 * This is called from dmu_fini(), which is called from spa_fini();
4859 * Because of this, we can assume that all l2arc devices have
4860 * already been removed when the pools themselves were removed.
4863 l2arc_do_free_on_write();
4865 mutex_destroy(&l2arc_feed_thr_lock);
4866 cv_destroy(&l2arc_feed_thr_cv);
4867 mutex_destroy(&l2arc_dev_mtx);
4868 mutex_destroy(&l2arc_buflist_mtx);
4869 mutex_destroy(&l2arc_free_on_write_mtx);
4871 list_destroy(l2arc_dev_list);
4872 list_destroy(l2arc_free_on_write);
4878 if (!(spa_mode_global & FWRITE))
4881 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
4882 TS_RUN, minclsyspri);
4888 if (!(spa_mode_global & FWRITE))
4891 mutex_enter(&l2arc_feed_thr_lock);
4892 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
4893 l2arc_thread_exit = 1;
4894 while (l2arc_thread_exit != 0)
4895 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
4896 mutex_exit(&l2arc_feed_thr_lock);
4899 #if defined(_KERNEL) && defined(HAVE_SPL)
4900 EXPORT_SYMBOL(arc_read);
4901 EXPORT_SYMBOL(arc_buf_remove_ref);
4902 EXPORT_SYMBOL(arc_getbuf_func);
4903 EXPORT_SYMBOL(arc_add_prune_callback);
4904 EXPORT_SYMBOL(arc_remove_prune_callback);
4906 module_param(zfs_arc_min, ulong, 0444);
4907 MODULE_PARM_DESC(zfs_arc_min, "Min arc size");
4909 module_param(zfs_arc_max, ulong, 0444);
4910 MODULE_PARM_DESC(zfs_arc_max, "Max arc size");
4912 module_param(zfs_arc_meta_limit, ulong, 0444);
4913 MODULE_PARM_DESC(zfs_arc_meta_limit, "Meta limit for arc size");
4915 module_param(zfs_arc_meta_prune, int, 0444);
4916 MODULE_PARM_DESC(zfs_arc_meta_prune, "Bytes of meta data to prune");
4918 module_param(zfs_arc_grow_retry, int, 0444);
4919 MODULE_PARM_DESC(zfs_arc_grow_retry, "Seconds before growing arc size");
4921 module_param(zfs_arc_shrink_shift, int, 0444);
4922 MODULE_PARM_DESC(zfs_arc_shrink_shift, "log2(fraction of arc to reclaim)");
4924 module_param(zfs_arc_p_min_shift, int, 0444);
4925 MODULE_PARM_DESC(zfs_arc_p_min_shift, "arc_c shift to calc min/max arc_p");
4927 module_param(l2arc_write_max, ulong, 0444);
4928 MODULE_PARM_DESC(l2arc_write_max, "Max write bytes per interval");
4930 module_param(l2arc_write_boost, ulong, 0444);
4931 MODULE_PARM_DESC(l2arc_write_boost, "Extra write bytes during device warmup");
4933 module_param(l2arc_headroom, ulong, 0444);
4934 MODULE_PARM_DESC(l2arc_headroom, "Number of max device writes to precache");
4936 module_param(l2arc_feed_secs, ulong, 0444);
4937 MODULE_PARM_DESC(l2arc_feed_secs, "Seconds between L2ARC writing");
4939 module_param(l2arc_feed_min_ms, ulong, 0444);
4940 MODULE_PARM_DESC(l2arc_feed_min_ms, "Min feed interval in milliseconds");
4942 module_param(l2arc_noprefetch, int, 0444);
4943 MODULE_PARM_DESC(l2arc_noprefetch, "Skip caching prefetched buffers");
4945 module_param(l2arc_feed_again, int, 0444);
4946 MODULE_PARM_DESC(l2arc_feed_again, "Turbo L2ARC warmup");
4948 module_param(l2arc_norw, int, 0444);
4949 MODULE_PARM_DESC(l2arc_norw, "No reads during writes");