4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
29 * DVA-based Adjustable Replacement Cache
31 * While much of the theory of operation used here is
32 * based on the self-tuning, low overhead replacement cache
33 * presented by Megiddo and Modha at FAST 2003, there are some
34 * significant differences:
36 * 1. The Megiddo and Modha model assumes any page is evictable.
37 * Pages in its cache cannot be "locked" into memory. This makes
38 * the eviction algorithm simple: evict the last page in the list.
39 * This also make the performance characteristics easy to reason
40 * about. Our cache is not so simple. At any given moment, some
41 * subset of the blocks in the cache are un-evictable because we
42 * have handed out a reference to them. Blocks are only evictable
43 * when there are no external references active. This makes
44 * eviction far more problematic: we choose to evict the evictable
45 * blocks that are the "lowest" in the list.
47 * There are times when it is not possible to evict the requested
48 * space. In these circumstances we are unable to adjust the cache
49 * size. To prevent the cache growing unbounded at these times we
50 * implement a "cache throttle" that slows the flow of new data
51 * into the cache until we can make space available.
53 * 2. The Megiddo and Modha model assumes a fixed cache size.
54 * Pages are evicted when the cache is full and there is a cache
55 * miss. Our model has a variable sized cache. It grows with
56 * high use, but also tries to react to memory pressure from the
57 * operating system: decreasing its size when system memory is
60 * 3. The Megiddo and Modha model assumes a fixed page size. All
61 * elements of the cache are therefore exactly the same size. So
62 * when adjusting the cache size following a cache miss, its simply
63 * a matter of choosing a single page to evict. In our model, we
64 * have variable sized cache blocks (rangeing from 512 bytes to
65 * 128K bytes). We therefore choose a set of blocks to evict to make
66 * space for a cache miss that approximates as closely as possible
67 * the space used by the new block.
69 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
70 * by N. Megiddo & D. Modha, FAST 2003
76 * A new reference to a cache buffer can be obtained in two
77 * ways: 1) via a hash table lookup using the DVA as a key,
78 * or 2) via one of the ARC lists. The arc_read() interface
79 * uses method 1, while the internal arc algorithms for
80 * adjusting the cache use method 2. We therefore provide two
81 * types of locks: 1) the hash table lock array, and 2) the
84 * Buffers do not have their own mutexes, rather they rely on the
85 * hash table mutexes for the bulk of their protection (i.e. most
86 * fields in the arc_buf_hdr_t are protected by these mutexes).
88 * buf_hash_find() returns the appropriate mutex (held) when it
89 * locates the requested buffer in the hash table. It returns
90 * NULL for the mutex if the buffer was not in the table.
92 * buf_hash_remove() expects the appropriate hash mutex to be
93 * already held before it is invoked.
95 * Each arc state also has a mutex which is used to protect the
96 * buffer list associated with the state. When attempting to
97 * obtain a hash table lock while holding an arc list lock you
98 * must use: mutex_tryenter() to avoid deadlock. Also note that
99 * the active state mutex must be held before the ghost state mutex.
101 * Arc buffers may have an associated eviction callback function.
102 * This function will be invoked prior to removing the buffer (e.g.
103 * in arc_do_user_evicts()). Note however that the data associated
104 * with the buffer may be evicted prior to the callback. The callback
105 * must be made with *no locks held* (to prevent deadlock). Additionally,
106 * the users of callbacks must ensure that their private data is
107 * protected from simultaneous callbacks from arc_buf_evict()
108 * and arc_do_user_evicts().
110 * It as also possible to register a callback which is run when the
111 * arc_meta_limit is reached and no buffers can be safely evicted. In
112 * this case the arc user should drop a reference on some arc buffers so
113 * they can be reclaimed and the arc_meta_limit honored. For example,
114 * when using the ZPL each dentry holds a references on a znode. These
115 * dentries must be pruned before the arc buffer holding the znode can
118 * Note that the majority of the performance stats are manipulated
119 * with atomic operations.
121 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
123 * - L2ARC buflist creation
124 * - L2ARC buflist eviction
125 * - L2ARC write completion, which walks L2ARC buflists
126 * - ARC header destruction, as it removes from L2ARC buflists
127 * - ARC header release, as it removes from L2ARC buflists
132 #include <sys/zio_compress.h>
133 #include <sys/zfs_context.h>
135 #include <sys/vdev.h>
136 #include <sys/vdev_impl.h>
137 #include <sys/dsl_pool.h>
139 #include <sys/vmsystm.h>
141 #include <sys/fs/swapnode.h>
144 #include <sys/callb.h>
145 #include <sys/kstat.h>
146 #include <sys/dmu_tx.h>
147 #include <zfs_fletcher.h>
150 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
151 boolean_t arc_watch = B_FALSE;
154 static kmutex_t arc_reclaim_thr_lock;
155 static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */
156 static uint8_t arc_thread_exit;
158 /* number of bytes to prune from caches when at arc_meta_limit is reached */
159 int zfs_arc_meta_prune = 1048576;
161 typedef enum arc_reclaim_strategy {
162 ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */
163 ARC_RECLAIM_CONS /* Conservative reclaim strategy */
164 } arc_reclaim_strategy_t;
167 * The number of iterations through arc_evict_*() before we
168 * drop & reacquire the lock.
170 int arc_evict_iterations = 100;
172 /* number of seconds before growing cache again */
173 int zfs_arc_grow_retry = 5;
175 /* shift of arc_c for calculating both min and max arc_p */
176 int zfs_arc_p_min_shift = 4;
178 /* log2(fraction of arc to reclaim) */
179 int zfs_arc_shrink_shift = 5;
182 * minimum lifespan of a prefetch block in clock ticks
183 * (initialized in arc_init())
185 int zfs_arc_min_prefetch_lifespan = HZ;
187 /* disable arc proactive arc throttle due to low memory */
188 int zfs_arc_memory_throttle_disable = 1;
190 /* disable duplicate buffer eviction */
191 int zfs_disable_dup_eviction = 0;
194 * If this percent of memory is free, don't throttle.
196 int arc_lotsfree_percent = 10;
200 /* expiration time for arc_no_grow */
201 static clock_t arc_grow_time = 0;
204 * The arc has filled available memory and has now warmed up.
206 static boolean_t arc_warm;
209 * These tunables are for performance analysis.
211 unsigned long zfs_arc_max = 0;
212 unsigned long zfs_arc_min = 0;
213 unsigned long zfs_arc_meta_limit = 0;
216 * Note that buffers can be in one of 6 states:
217 * ARC_anon - anonymous (discussed below)
218 * ARC_mru - recently used, currently cached
219 * ARC_mru_ghost - recentely used, no longer in cache
220 * ARC_mfu - frequently used, currently cached
221 * ARC_mfu_ghost - frequently used, no longer in cache
222 * ARC_l2c_only - exists in L2ARC but not other states
223 * When there are no active references to the buffer, they are
224 * are linked onto a list in one of these arc states. These are
225 * the only buffers that can be evicted or deleted. Within each
226 * state there are multiple lists, one for meta-data and one for
227 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
228 * etc.) is tracked separately so that it can be managed more
229 * explicitly: favored over data, limited explicitly.
231 * Anonymous buffers are buffers that are not associated with
232 * a DVA. These are buffers that hold dirty block copies
233 * before they are written to stable storage. By definition,
234 * they are "ref'd" and are considered part of arc_mru
235 * that cannot be freed. Generally, they will aquire a DVA
236 * as they are written and migrate onto the arc_mru list.
238 * The ARC_l2c_only state is for buffers that are in the second
239 * level ARC but no longer in any of the ARC_m* lists. The second
240 * level ARC itself may also contain buffers that are in any of
241 * the ARC_m* states - meaning that a buffer can exist in two
242 * places. The reason for the ARC_l2c_only state is to keep the
243 * buffer header in the hash table, so that reads that hit the
244 * second level ARC benefit from these fast lookups.
247 typedef struct arc_state {
248 list_t arcs_list[ARC_BUFC_NUMTYPES]; /* list of evictable buffers */
249 uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */
250 uint64_t arcs_size; /* total amount of data in this state */
252 arc_state_type_t arcs_state;
256 static arc_state_t ARC_anon;
257 static arc_state_t ARC_mru;
258 static arc_state_t ARC_mru_ghost;
259 static arc_state_t ARC_mfu;
260 static arc_state_t ARC_mfu_ghost;
261 static arc_state_t ARC_l2c_only;
263 typedef struct arc_stats {
264 kstat_named_t arcstat_hits;
265 kstat_named_t arcstat_misses;
266 kstat_named_t arcstat_demand_data_hits;
267 kstat_named_t arcstat_demand_data_misses;
268 kstat_named_t arcstat_demand_metadata_hits;
269 kstat_named_t arcstat_demand_metadata_misses;
270 kstat_named_t arcstat_prefetch_data_hits;
271 kstat_named_t arcstat_prefetch_data_misses;
272 kstat_named_t arcstat_prefetch_metadata_hits;
273 kstat_named_t arcstat_prefetch_metadata_misses;
274 kstat_named_t arcstat_mru_hits;
275 kstat_named_t arcstat_mru_ghost_hits;
276 kstat_named_t arcstat_mfu_hits;
277 kstat_named_t arcstat_mfu_ghost_hits;
278 kstat_named_t arcstat_deleted;
279 kstat_named_t arcstat_recycle_miss;
281 * Number of buffers that could not be evicted because the hash lock
282 * was held by another thread. The lock may not necessarily be held
283 * by something using the same buffer, since hash locks are shared
284 * by multiple buffers.
286 kstat_named_t arcstat_mutex_miss;
288 * Number of buffers skipped because they have I/O in progress, are
289 * indrect prefetch buffers that have not lived long enough, or are
290 * not from the spa we're trying to evict from.
292 kstat_named_t arcstat_evict_skip;
293 kstat_named_t arcstat_evict_l2_cached;
294 kstat_named_t arcstat_evict_l2_eligible;
295 kstat_named_t arcstat_evict_l2_ineligible;
296 kstat_named_t arcstat_hash_elements;
297 kstat_named_t arcstat_hash_elements_max;
298 kstat_named_t arcstat_hash_collisions;
299 kstat_named_t arcstat_hash_chains;
300 kstat_named_t arcstat_hash_chain_max;
301 kstat_named_t arcstat_p;
302 kstat_named_t arcstat_c;
303 kstat_named_t arcstat_c_min;
304 kstat_named_t arcstat_c_max;
305 kstat_named_t arcstat_size;
306 kstat_named_t arcstat_hdr_size;
307 kstat_named_t arcstat_data_size;
308 kstat_named_t arcstat_other_size;
309 kstat_named_t arcstat_anon_size;
310 kstat_named_t arcstat_anon_evict_data;
311 kstat_named_t arcstat_anon_evict_metadata;
312 kstat_named_t arcstat_mru_size;
313 kstat_named_t arcstat_mru_evict_data;
314 kstat_named_t arcstat_mru_evict_metadata;
315 kstat_named_t arcstat_mru_ghost_size;
316 kstat_named_t arcstat_mru_ghost_evict_data;
317 kstat_named_t arcstat_mru_ghost_evict_metadata;
318 kstat_named_t arcstat_mfu_size;
319 kstat_named_t arcstat_mfu_evict_data;
320 kstat_named_t arcstat_mfu_evict_metadata;
321 kstat_named_t arcstat_mfu_ghost_size;
322 kstat_named_t arcstat_mfu_ghost_evict_data;
323 kstat_named_t arcstat_mfu_ghost_evict_metadata;
324 kstat_named_t arcstat_l2_hits;
325 kstat_named_t arcstat_l2_misses;
326 kstat_named_t arcstat_l2_feeds;
327 kstat_named_t arcstat_l2_rw_clash;
328 kstat_named_t arcstat_l2_read_bytes;
329 kstat_named_t arcstat_l2_write_bytes;
330 kstat_named_t arcstat_l2_writes_sent;
331 kstat_named_t arcstat_l2_writes_done;
332 kstat_named_t arcstat_l2_writes_error;
333 kstat_named_t arcstat_l2_writes_hdr_miss;
334 kstat_named_t arcstat_l2_evict_lock_retry;
335 kstat_named_t arcstat_l2_evict_reading;
336 kstat_named_t arcstat_l2_free_on_write;
337 kstat_named_t arcstat_l2_abort_lowmem;
338 kstat_named_t arcstat_l2_cksum_bad;
339 kstat_named_t arcstat_l2_io_error;
340 kstat_named_t arcstat_l2_size;
341 kstat_named_t arcstat_l2_asize;
342 kstat_named_t arcstat_l2_hdr_size;
343 kstat_named_t arcstat_l2_compress_successes;
344 kstat_named_t arcstat_l2_compress_zeros;
345 kstat_named_t arcstat_l2_compress_failures;
346 kstat_named_t arcstat_memory_throttle_count;
347 kstat_named_t arcstat_duplicate_buffers;
348 kstat_named_t arcstat_duplicate_buffers_size;
349 kstat_named_t arcstat_duplicate_reads;
350 kstat_named_t arcstat_memory_direct_count;
351 kstat_named_t arcstat_memory_indirect_count;
352 kstat_named_t arcstat_no_grow;
353 kstat_named_t arcstat_tempreserve;
354 kstat_named_t arcstat_loaned_bytes;
355 kstat_named_t arcstat_prune;
356 kstat_named_t arcstat_meta_used;
357 kstat_named_t arcstat_meta_limit;
358 kstat_named_t arcstat_meta_max;
361 static arc_stats_t arc_stats = {
362 { "hits", KSTAT_DATA_UINT64 },
363 { "misses", KSTAT_DATA_UINT64 },
364 { "demand_data_hits", KSTAT_DATA_UINT64 },
365 { "demand_data_misses", KSTAT_DATA_UINT64 },
366 { "demand_metadata_hits", KSTAT_DATA_UINT64 },
367 { "demand_metadata_misses", KSTAT_DATA_UINT64 },
368 { "prefetch_data_hits", KSTAT_DATA_UINT64 },
369 { "prefetch_data_misses", KSTAT_DATA_UINT64 },
370 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
371 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
372 { "mru_hits", KSTAT_DATA_UINT64 },
373 { "mru_ghost_hits", KSTAT_DATA_UINT64 },
374 { "mfu_hits", KSTAT_DATA_UINT64 },
375 { "mfu_ghost_hits", KSTAT_DATA_UINT64 },
376 { "deleted", KSTAT_DATA_UINT64 },
377 { "recycle_miss", KSTAT_DATA_UINT64 },
378 { "mutex_miss", KSTAT_DATA_UINT64 },
379 { "evict_skip", KSTAT_DATA_UINT64 },
380 { "evict_l2_cached", KSTAT_DATA_UINT64 },
381 { "evict_l2_eligible", KSTAT_DATA_UINT64 },
382 { "evict_l2_ineligible", KSTAT_DATA_UINT64 },
383 { "hash_elements", KSTAT_DATA_UINT64 },
384 { "hash_elements_max", KSTAT_DATA_UINT64 },
385 { "hash_collisions", KSTAT_DATA_UINT64 },
386 { "hash_chains", KSTAT_DATA_UINT64 },
387 { "hash_chain_max", KSTAT_DATA_UINT64 },
388 { "p", KSTAT_DATA_UINT64 },
389 { "c", KSTAT_DATA_UINT64 },
390 { "c_min", KSTAT_DATA_UINT64 },
391 { "c_max", KSTAT_DATA_UINT64 },
392 { "size", KSTAT_DATA_UINT64 },
393 { "hdr_size", KSTAT_DATA_UINT64 },
394 { "data_size", KSTAT_DATA_UINT64 },
395 { "other_size", KSTAT_DATA_UINT64 },
396 { "anon_size", KSTAT_DATA_UINT64 },
397 { "anon_evict_data", KSTAT_DATA_UINT64 },
398 { "anon_evict_metadata", KSTAT_DATA_UINT64 },
399 { "mru_size", KSTAT_DATA_UINT64 },
400 { "mru_evict_data", KSTAT_DATA_UINT64 },
401 { "mru_evict_metadata", KSTAT_DATA_UINT64 },
402 { "mru_ghost_size", KSTAT_DATA_UINT64 },
403 { "mru_ghost_evict_data", KSTAT_DATA_UINT64 },
404 { "mru_ghost_evict_metadata", KSTAT_DATA_UINT64 },
405 { "mfu_size", KSTAT_DATA_UINT64 },
406 { "mfu_evict_data", KSTAT_DATA_UINT64 },
407 { "mfu_evict_metadata", KSTAT_DATA_UINT64 },
408 { "mfu_ghost_size", KSTAT_DATA_UINT64 },
409 { "mfu_ghost_evict_data", KSTAT_DATA_UINT64 },
410 { "mfu_ghost_evict_metadata", KSTAT_DATA_UINT64 },
411 { "l2_hits", KSTAT_DATA_UINT64 },
412 { "l2_misses", KSTAT_DATA_UINT64 },
413 { "l2_feeds", KSTAT_DATA_UINT64 },
414 { "l2_rw_clash", KSTAT_DATA_UINT64 },
415 { "l2_read_bytes", KSTAT_DATA_UINT64 },
416 { "l2_write_bytes", KSTAT_DATA_UINT64 },
417 { "l2_writes_sent", KSTAT_DATA_UINT64 },
418 { "l2_writes_done", KSTAT_DATA_UINT64 },
419 { "l2_writes_error", KSTAT_DATA_UINT64 },
420 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 },
421 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
422 { "l2_evict_reading", KSTAT_DATA_UINT64 },
423 { "l2_free_on_write", KSTAT_DATA_UINT64 },
424 { "l2_abort_lowmem", KSTAT_DATA_UINT64 },
425 { "l2_cksum_bad", KSTAT_DATA_UINT64 },
426 { "l2_io_error", KSTAT_DATA_UINT64 },
427 { "l2_size", KSTAT_DATA_UINT64 },
428 { "l2_asize", KSTAT_DATA_UINT64 },
429 { "l2_hdr_size", KSTAT_DATA_UINT64 },
430 { "l2_compress_successes", KSTAT_DATA_UINT64 },
431 { "l2_compress_zeros", KSTAT_DATA_UINT64 },
432 { "l2_compress_failures", KSTAT_DATA_UINT64 },
433 { "memory_throttle_count", KSTAT_DATA_UINT64 },
434 { "duplicate_buffers", KSTAT_DATA_UINT64 },
435 { "duplicate_buffers_size", KSTAT_DATA_UINT64 },
436 { "duplicate_reads", KSTAT_DATA_UINT64 },
437 { "memory_direct_count", KSTAT_DATA_UINT64 },
438 { "memory_indirect_count", KSTAT_DATA_UINT64 },
439 { "arc_no_grow", KSTAT_DATA_UINT64 },
440 { "arc_tempreserve", KSTAT_DATA_UINT64 },
441 { "arc_loaned_bytes", KSTAT_DATA_UINT64 },
442 { "arc_prune", KSTAT_DATA_UINT64 },
443 { "arc_meta_used", KSTAT_DATA_UINT64 },
444 { "arc_meta_limit", KSTAT_DATA_UINT64 },
445 { "arc_meta_max", KSTAT_DATA_UINT64 },
448 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
450 #define ARCSTAT_INCR(stat, val) \
451 atomic_add_64(&arc_stats.stat.value.ui64, (val))
453 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
454 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
456 #define ARCSTAT_MAX(stat, val) { \
458 while ((val) > (m = arc_stats.stat.value.ui64) && \
459 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
463 #define ARCSTAT_MAXSTAT(stat) \
464 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
467 * We define a macro to allow ARC hits/misses to be easily broken down by
468 * two separate conditions, giving a total of four different subtypes for
469 * each of hits and misses (so eight statistics total).
471 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
474 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
476 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
480 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
482 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
487 static arc_state_t *arc_anon;
488 static arc_state_t *arc_mru;
489 static arc_state_t *arc_mru_ghost;
490 static arc_state_t *arc_mfu;
491 static arc_state_t *arc_mfu_ghost;
492 static arc_state_t *arc_l2c_only;
495 * There are several ARC variables that are critical to export as kstats --
496 * but we don't want to have to grovel around in the kstat whenever we wish to
497 * manipulate them. For these variables, we therefore define them to be in
498 * terms of the statistic variable. This assures that we are not introducing
499 * the possibility of inconsistency by having shadow copies of the variables,
500 * while still allowing the code to be readable.
502 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
503 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
504 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
505 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
506 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
507 #define arc_no_grow ARCSTAT(arcstat_no_grow)
508 #define arc_tempreserve ARCSTAT(arcstat_tempreserve)
509 #define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
510 #define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
511 #define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */
512 #define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
514 #define L2ARC_IS_VALID_COMPRESS(_c_) \
515 ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY)
517 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
519 typedef struct arc_callback arc_callback_t;
521 struct arc_callback {
523 arc_done_func_t *acb_done;
525 zio_t *acb_zio_dummy;
526 arc_callback_t *acb_next;
529 typedef struct arc_write_callback arc_write_callback_t;
531 struct arc_write_callback {
533 arc_done_func_t *awcb_ready;
534 arc_done_func_t *awcb_physdone;
535 arc_done_func_t *awcb_done;
540 /* protected by hash lock */
545 kmutex_t b_freeze_lock;
546 zio_cksum_t *b_freeze_cksum;
548 arc_buf_hdr_t *b_hash_next;
553 arc_callback_t *b_acb;
557 arc_buf_contents_t b_type;
561 /* protected by arc state mutex */
562 arc_state_t *b_state;
563 list_node_t b_arc_node;
565 /* updated atomically */
566 clock_t b_arc_access;
568 uint32_t b_mru_ghost_hits;
570 uint32_t b_mfu_ghost_hits;
573 /* self protecting */
576 l2arc_buf_hdr_t *b_l2hdr;
577 list_node_t b_l2node;
580 static list_t arc_prune_list;
581 static kmutex_t arc_prune_mtx;
582 static arc_buf_t *arc_eviction_list;
583 static kmutex_t arc_eviction_mtx;
584 static arc_buf_hdr_t arc_eviction_hdr;
585 static void arc_get_data_buf(arc_buf_t *buf);
586 static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
587 static int arc_evict_needed(arc_buf_contents_t type);
588 static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes,
589 arc_buf_contents_t type);
590 static void arc_buf_watch(arc_buf_t *buf);
592 static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
594 #define GHOST_STATE(state) \
595 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
596 (state) == arc_l2c_only)
599 * Private ARC flags. These flags are private ARC only flags that will show up
600 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
601 * be passed in as arc_flags in things like arc_read. However, these flags
602 * should never be passed and should only be set by ARC code. When adding new
603 * public flags, make sure not to smash the private ones.
606 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
607 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
608 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
609 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
610 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
611 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
612 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
613 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
614 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
615 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
617 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
618 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
619 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
620 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
621 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
622 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
623 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
624 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
625 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
626 (hdr)->b_l2hdr != NULL)
627 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
628 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
629 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
635 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
636 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
639 * Hash table routines
642 #define HT_LOCK_ALIGN 64
643 #define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
648 unsigned char pad[HT_LOCK_PAD];
652 #define BUF_LOCKS 256
653 typedef struct buf_hash_table {
655 arc_buf_hdr_t **ht_table;
656 struct ht_lock ht_locks[BUF_LOCKS];
659 static buf_hash_table_t buf_hash_table;
661 #define BUF_HASH_INDEX(spa, dva, birth) \
662 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
663 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
664 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
665 #define HDR_LOCK(hdr) \
666 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
668 uint64_t zfs_crc64_table[256];
674 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
675 #define L2ARC_HEADROOM 2 /* num of writes */
677 * If we discover during ARC scan any buffers to be compressed, we boost
678 * our headroom for the next scanning cycle by this percentage multiple.
680 #define L2ARC_HEADROOM_BOOST 200
681 #define L2ARC_FEED_SECS 1 /* caching interval secs */
682 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
684 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
685 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
687 /* L2ARC Performance Tunables */
688 unsigned long l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */
689 unsigned long l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */
690 unsigned long l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */
691 unsigned long l2arc_headroom_boost = L2ARC_HEADROOM_BOOST;
692 unsigned long l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */
693 unsigned long l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */
694 int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */
695 int l2arc_nocompress = B_FALSE; /* don't compress bufs */
696 int l2arc_feed_again = B_TRUE; /* turbo warmup */
697 int l2arc_norw = B_FALSE; /* no reads during writes */
702 typedef struct l2arc_dev {
703 vdev_t *l2ad_vdev; /* vdev */
704 spa_t *l2ad_spa; /* spa */
705 uint64_t l2ad_hand; /* next write location */
706 uint64_t l2ad_start; /* first addr on device */
707 uint64_t l2ad_end; /* last addr on device */
708 uint64_t l2ad_evict; /* last addr eviction reached */
709 boolean_t l2ad_first; /* first sweep through */
710 boolean_t l2ad_writing; /* currently writing */
711 list_t *l2ad_buflist; /* buffer list */
712 list_node_t l2ad_node; /* device list node */
715 static list_t L2ARC_dev_list; /* device list */
716 static list_t *l2arc_dev_list; /* device list pointer */
717 static kmutex_t l2arc_dev_mtx; /* device list mutex */
718 static l2arc_dev_t *l2arc_dev_last; /* last device used */
719 static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */
720 static list_t L2ARC_free_on_write; /* free after write buf list */
721 static list_t *l2arc_free_on_write; /* free after write list ptr */
722 static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */
723 static uint64_t l2arc_ndev; /* number of devices */
725 typedef struct l2arc_read_callback {
726 arc_buf_t *l2rcb_buf; /* read buffer */
727 spa_t *l2rcb_spa; /* spa */
728 blkptr_t l2rcb_bp; /* original blkptr */
729 zbookmark_t l2rcb_zb; /* original bookmark */
730 int l2rcb_flags; /* original flags */
731 enum zio_compress l2rcb_compress; /* applied compress */
732 } l2arc_read_callback_t;
734 typedef struct l2arc_write_callback {
735 l2arc_dev_t *l2wcb_dev; /* device info */
736 arc_buf_hdr_t *l2wcb_head; /* head of write buflist */
737 } l2arc_write_callback_t;
739 struct l2arc_buf_hdr {
740 /* protected by arc_buf_hdr mutex */
741 l2arc_dev_t *b_dev; /* L2ARC device */
742 uint64_t b_daddr; /* disk address, offset byte */
743 /* compression applied to buffer data */
744 enum zio_compress b_compress;
745 /* real alloc'd buffer size depending on b_compress applied */
748 /* temporary buffer holder for in-flight compressed data */
752 typedef struct l2arc_data_free {
753 /* protected by l2arc_free_on_write_mtx */
756 void (*l2df_func)(void *, size_t);
757 list_node_t l2df_list_node;
760 static kmutex_t l2arc_feed_thr_lock;
761 static kcondvar_t l2arc_feed_thr_cv;
762 static uint8_t l2arc_thread_exit;
764 static void l2arc_read_done(zio_t *zio);
765 static void l2arc_hdr_stat_add(void);
766 static void l2arc_hdr_stat_remove(void);
768 static boolean_t l2arc_compress_buf(l2arc_buf_hdr_t *l2hdr);
769 static void l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr,
770 enum zio_compress c);
771 static void l2arc_release_cdata_buf(arc_buf_hdr_t *ab);
774 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
776 uint8_t *vdva = (uint8_t *)dva;
777 uint64_t crc = -1ULL;
780 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
782 for (i = 0; i < sizeof (dva_t); i++)
783 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
785 crc ^= (spa>>8) ^ birth;
790 #define BUF_EMPTY(buf) \
791 ((buf)->b_dva.dva_word[0] == 0 && \
792 (buf)->b_dva.dva_word[1] == 0 && \
795 #define BUF_EQUAL(spa, dva, birth, buf) \
796 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
797 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
798 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
801 buf_discard_identity(arc_buf_hdr_t *hdr)
803 hdr->b_dva.dva_word[0] = 0;
804 hdr->b_dva.dva_word[1] = 0;
809 static arc_buf_hdr_t *
810 buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp)
812 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
813 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
816 mutex_enter(hash_lock);
817 for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
818 buf = buf->b_hash_next) {
819 if (BUF_EQUAL(spa, dva, birth, buf)) {
824 mutex_exit(hash_lock);
830 * Insert an entry into the hash table. If there is already an element
831 * equal to elem in the hash table, then the already existing element
832 * will be returned and the new element will not be inserted.
833 * Otherwise returns NULL.
835 static arc_buf_hdr_t *
836 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
838 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
839 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
843 ASSERT(!HDR_IN_HASH_TABLE(buf));
845 mutex_enter(hash_lock);
846 for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
847 fbuf = fbuf->b_hash_next, i++) {
848 if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
852 buf->b_hash_next = buf_hash_table.ht_table[idx];
853 buf_hash_table.ht_table[idx] = buf;
854 buf->b_flags |= ARC_IN_HASH_TABLE;
856 /* collect some hash table performance data */
858 ARCSTAT_BUMP(arcstat_hash_collisions);
860 ARCSTAT_BUMP(arcstat_hash_chains);
862 ARCSTAT_MAX(arcstat_hash_chain_max, i);
865 ARCSTAT_BUMP(arcstat_hash_elements);
866 ARCSTAT_MAXSTAT(arcstat_hash_elements);
872 buf_hash_remove(arc_buf_hdr_t *buf)
874 arc_buf_hdr_t *fbuf, **bufp;
875 uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
877 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
878 ASSERT(HDR_IN_HASH_TABLE(buf));
880 bufp = &buf_hash_table.ht_table[idx];
881 while ((fbuf = *bufp) != buf) {
882 ASSERT(fbuf != NULL);
883 bufp = &fbuf->b_hash_next;
885 *bufp = buf->b_hash_next;
886 buf->b_hash_next = NULL;
887 buf->b_flags &= ~ARC_IN_HASH_TABLE;
889 /* collect some hash table performance data */
890 ARCSTAT_BUMPDOWN(arcstat_hash_elements);
892 if (buf_hash_table.ht_table[idx] &&
893 buf_hash_table.ht_table[idx]->b_hash_next == NULL)
894 ARCSTAT_BUMPDOWN(arcstat_hash_chains);
898 * Global data structures and functions for the buf kmem cache.
900 static kmem_cache_t *hdr_cache;
901 static kmem_cache_t *buf_cache;
908 #if defined(_KERNEL) && defined(HAVE_SPL)
909 /* Large allocations which do not require contiguous pages
910 * should be using vmem_free() in the linux kernel */
911 vmem_free(buf_hash_table.ht_table,
912 (buf_hash_table.ht_mask + 1) * sizeof (void *));
914 kmem_free(buf_hash_table.ht_table,
915 (buf_hash_table.ht_mask + 1) * sizeof (void *));
917 for (i = 0; i < BUF_LOCKS; i++)
918 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
919 kmem_cache_destroy(hdr_cache);
920 kmem_cache_destroy(buf_cache);
924 * Constructor callback - called when the cache is empty
925 * and a new buf is requested.
929 hdr_cons(void *vbuf, void *unused, int kmflag)
931 arc_buf_hdr_t *buf = vbuf;
933 bzero(buf, sizeof (arc_buf_hdr_t));
934 refcount_create(&buf->b_refcnt);
935 cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
936 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
937 list_link_init(&buf->b_arc_node);
938 list_link_init(&buf->b_l2node);
939 arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
946 buf_cons(void *vbuf, void *unused, int kmflag)
948 arc_buf_t *buf = vbuf;
950 bzero(buf, sizeof (arc_buf_t));
951 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
952 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
958 * Destructor callback - called when a cached buf is
959 * no longer required.
963 hdr_dest(void *vbuf, void *unused)
965 arc_buf_hdr_t *buf = vbuf;
967 ASSERT(BUF_EMPTY(buf));
968 refcount_destroy(&buf->b_refcnt);
969 cv_destroy(&buf->b_cv);
970 mutex_destroy(&buf->b_freeze_lock);
971 arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
976 buf_dest(void *vbuf, void *unused)
978 arc_buf_t *buf = vbuf;
980 mutex_destroy(&buf->b_evict_lock);
981 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
988 uint64_t hsize = 1ULL << 12;
992 * The hash table is big enough to fill all of physical memory
993 * with an average 64K block size. The table will take up
994 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
996 while (hsize * 65536 < physmem * PAGESIZE)
999 buf_hash_table.ht_mask = hsize - 1;
1000 #if defined(_KERNEL) && defined(HAVE_SPL)
1001 /* Large allocations which do not require contiguous pages
1002 * should be using vmem_alloc() in the linux kernel */
1003 buf_hash_table.ht_table =
1004 vmem_zalloc(hsize * sizeof (void*), KM_SLEEP);
1006 buf_hash_table.ht_table =
1007 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
1009 if (buf_hash_table.ht_table == NULL) {
1010 ASSERT(hsize > (1ULL << 8));
1015 hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
1016 0, hdr_cons, hdr_dest, NULL, NULL, NULL, 0);
1017 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
1018 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
1020 for (i = 0; i < 256; i++)
1021 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
1022 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
1024 for (i = 0; i < BUF_LOCKS; i++) {
1025 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
1026 NULL, MUTEX_DEFAULT, NULL);
1030 #define ARC_MINTIME (hz>>4) /* 62 ms */
1033 arc_cksum_verify(arc_buf_t *buf)
1037 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1040 mutex_enter(&buf->b_hdr->b_freeze_lock);
1041 if (buf->b_hdr->b_freeze_cksum == NULL ||
1042 (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
1043 mutex_exit(&buf->b_hdr->b_freeze_lock);
1046 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
1047 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
1048 panic("buffer modified while frozen!");
1049 mutex_exit(&buf->b_hdr->b_freeze_lock);
1053 arc_cksum_equal(arc_buf_t *buf)
1058 mutex_enter(&buf->b_hdr->b_freeze_lock);
1059 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
1060 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
1061 mutex_exit(&buf->b_hdr->b_freeze_lock);
1067 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
1069 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
1072 mutex_enter(&buf->b_hdr->b_freeze_lock);
1073 if (buf->b_hdr->b_freeze_cksum != NULL) {
1074 mutex_exit(&buf->b_hdr->b_freeze_lock);
1077 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t),
1079 fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
1080 buf->b_hdr->b_freeze_cksum);
1081 mutex_exit(&buf->b_hdr->b_freeze_lock);
1087 arc_buf_sigsegv(int sig, siginfo_t *si, void *unused)
1089 panic("Got SIGSEGV at address: 0x%lx\n", (long) si->si_addr);
1095 arc_buf_unwatch(arc_buf_t *buf)
1099 ASSERT0(mprotect(buf->b_data, buf->b_hdr->b_size,
1100 PROT_READ | PROT_WRITE));
1107 arc_buf_watch(arc_buf_t *buf)
1111 ASSERT0(mprotect(buf->b_data, buf->b_hdr->b_size, PROT_READ));
1116 arc_buf_thaw(arc_buf_t *buf)
1118 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1119 if (buf->b_hdr->b_state != arc_anon)
1120 panic("modifying non-anon buffer!");
1121 if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
1122 panic("modifying buffer while i/o in progress!");
1123 arc_cksum_verify(buf);
1126 mutex_enter(&buf->b_hdr->b_freeze_lock);
1127 if (buf->b_hdr->b_freeze_cksum != NULL) {
1128 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1129 buf->b_hdr->b_freeze_cksum = NULL;
1132 mutex_exit(&buf->b_hdr->b_freeze_lock);
1134 arc_buf_unwatch(buf);
1138 arc_buf_freeze(arc_buf_t *buf)
1140 kmutex_t *hash_lock;
1142 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1145 hash_lock = HDR_LOCK(buf->b_hdr);
1146 mutex_enter(hash_lock);
1148 ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
1149 buf->b_hdr->b_state == arc_anon);
1150 arc_cksum_compute(buf, B_FALSE);
1151 mutex_exit(hash_lock);
1156 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1158 ASSERT(MUTEX_HELD(hash_lock));
1160 if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1161 (ab->b_state != arc_anon)) {
1162 uint64_t delta = ab->b_size * ab->b_datacnt;
1163 list_t *list = &ab->b_state->arcs_list[ab->b_type];
1164 uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1166 ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
1167 mutex_enter(&ab->b_state->arcs_mtx);
1168 ASSERT(list_link_active(&ab->b_arc_node));
1169 list_remove(list, ab);
1170 if (GHOST_STATE(ab->b_state)) {
1171 ASSERT0(ab->b_datacnt);
1172 ASSERT3P(ab->b_buf, ==, NULL);
1176 ASSERT3U(*size, >=, delta);
1177 atomic_add_64(size, -delta);
1178 mutex_exit(&ab->b_state->arcs_mtx);
1179 /* remove the prefetch flag if we get a reference */
1180 if (ab->b_flags & ARC_PREFETCH)
1181 ab->b_flags &= ~ARC_PREFETCH;
1186 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1189 arc_state_t *state = ab->b_state;
1191 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1192 ASSERT(!GHOST_STATE(state));
1194 if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1195 (state != arc_anon)) {
1196 uint64_t *size = &state->arcs_lsize[ab->b_type];
1198 ASSERT(!MUTEX_HELD(&state->arcs_mtx));
1199 mutex_enter(&state->arcs_mtx);
1200 ASSERT(!list_link_active(&ab->b_arc_node));
1201 list_insert_head(&state->arcs_list[ab->b_type], ab);
1202 ASSERT(ab->b_datacnt > 0);
1203 atomic_add_64(size, ab->b_size * ab->b_datacnt);
1204 mutex_exit(&state->arcs_mtx);
1210 * Returns detailed information about a specific arc buffer. When the
1211 * state_index argument is set the function will calculate the arc header
1212 * list position for its arc state. Since this requires a linear traversal
1213 * callers are strongly encourage not to do this. However, it can be helpful
1214 * for targeted analysis so the functionality is provided.
1217 arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index)
1219 arc_buf_hdr_t *hdr = ab->b_hdr;
1220 arc_state_t *state = hdr->b_state;
1222 memset(abi, 0, sizeof(arc_buf_info_t));
1223 abi->abi_flags = hdr->b_flags;
1224 abi->abi_datacnt = hdr->b_datacnt;
1225 abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON;
1226 abi->abi_state_contents = hdr->b_type;
1227 abi->abi_state_index = -1;
1228 abi->abi_size = hdr->b_size;
1229 abi->abi_access = hdr->b_arc_access;
1230 abi->abi_mru_hits = hdr->b_mru_hits;
1231 abi->abi_mru_ghost_hits = hdr->b_mru_ghost_hits;
1232 abi->abi_mfu_hits = hdr->b_mfu_hits;
1233 abi->abi_mfu_ghost_hits = hdr->b_mfu_ghost_hits;
1234 abi->abi_holds = refcount_count(&hdr->b_refcnt);
1237 abi->abi_l2arc_dattr = hdr->b_l2hdr->b_daddr;
1238 abi->abi_l2arc_asize = hdr->b_l2hdr->b_asize;
1239 abi->abi_l2arc_compress = hdr->b_l2hdr->b_compress;
1240 abi->abi_l2arc_hits = hdr->b_l2hdr->b_hits;
1243 if (state && state_index && list_link_active(&hdr->b_arc_node)) {
1244 list_t *list = &state->arcs_list[hdr->b_type];
1247 mutex_enter(&state->arcs_mtx);
1248 for (h = list_head(list); h != NULL; h = list_next(list, h)) {
1249 abi->abi_state_index++;
1253 mutex_exit(&state->arcs_mtx);
1258 * Move the supplied buffer to the indicated state. The mutex
1259 * for the buffer must be held by the caller.
1262 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1264 arc_state_t *old_state = ab->b_state;
1265 int64_t refcnt = refcount_count(&ab->b_refcnt);
1266 uint64_t from_delta, to_delta;
1268 ASSERT(MUTEX_HELD(hash_lock));
1269 ASSERT3P(new_state, !=, old_state);
1270 ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1271 ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1272 ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1274 from_delta = to_delta = ab->b_datacnt * ab->b_size;
1277 * If this buffer is evictable, transfer it from the
1278 * old state list to the new state list.
1281 if (old_state != arc_anon) {
1282 int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
1283 uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1286 mutex_enter(&old_state->arcs_mtx);
1288 ASSERT(list_link_active(&ab->b_arc_node));
1289 list_remove(&old_state->arcs_list[ab->b_type], ab);
1292 * If prefetching out of the ghost cache,
1293 * we will have a non-zero datacnt.
1295 if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1296 /* ghost elements have a ghost size */
1297 ASSERT(ab->b_buf == NULL);
1298 from_delta = ab->b_size;
1300 ASSERT3U(*size, >=, from_delta);
1301 atomic_add_64(size, -from_delta);
1304 mutex_exit(&old_state->arcs_mtx);
1306 if (new_state != arc_anon) {
1307 int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
1308 uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1311 mutex_enter(&new_state->arcs_mtx);
1313 list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1315 /* ghost elements have a ghost size */
1316 if (GHOST_STATE(new_state)) {
1317 ASSERT(ab->b_datacnt == 0);
1318 ASSERT(ab->b_buf == NULL);
1319 to_delta = ab->b_size;
1321 atomic_add_64(size, to_delta);
1324 mutex_exit(&new_state->arcs_mtx);
1328 ASSERT(!BUF_EMPTY(ab));
1329 if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1330 buf_hash_remove(ab);
1332 /* adjust state sizes */
1334 atomic_add_64(&new_state->arcs_size, to_delta);
1336 ASSERT3U(old_state->arcs_size, >=, from_delta);
1337 atomic_add_64(&old_state->arcs_size, -from_delta);
1339 ab->b_state = new_state;
1341 /* adjust l2arc hdr stats */
1342 if (new_state == arc_l2c_only)
1343 l2arc_hdr_stat_add();
1344 else if (old_state == arc_l2c_only)
1345 l2arc_hdr_stat_remove();
1349 arc_space_consume(uint64_t space, arc_space_type_t type)
1351 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1356 case ARC_SPACE_DATA:
1357 ARCSTAT_INCR(arcstat_data_size, space);
1359 case ARC_SPACE_OTHER:
1360 ARCSTAT_INCR(arcstat_other_size, space);
1362 case ARC_SPACE_HDRS:
1363 ARCSTAT_INCR(arcstat_hdr_size, space);
1365 case ARC_SPACE_L2HDRS:
1366 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1370 ARCSTAT_INCR(arcstat_meta_used, space);
1371 atomic_add_64(&arc_size, space);
1375 arc_space_return(uint64_t space, arc_space_type_t type)
1377 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1382 case ARC_SPACE_DATA:
1383 ARCSTAT_INCR(arcstat_data_size, -space);
1385 case ARC_SPACE_OTHER:
1386 ARCSTAT_INCR(arcstat_other_size, -space);
1388 case ARC_SPACE_HDRS:
1389 ARCSTAT_INCR(arcstat_hdr_size, -space);
1391 case ARC_SPACE_L2HDRS:
1392 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1396 ASSERT(arc_meta_used >= space);
1397 if (arc_meta_max < arc_meta_used)
1398 arc_meta_max = arc_meta_used;
1399 ARCSTAT_INCR(arcstat_meta_used, -space);
1400 ASSERT(arc_size >= space);
1401 atomic_add_64(&arc_size, -space);
1405 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1410 ASSERT3U(size, >, 0);
1411 hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1412 ASSERT(BUF_EMPTY(hdr));
1415 hdr->b_spa = spa_load_guid(spa);
1416 hdr->b_state = arc_anon;
1417 hdr->b_arc_access = 0;
1418 hdr->b_mru_hits = 0;
1419 hdr->b_mru_ghost_hits = 0;
1420 hdr->b_mfu_hits = 0;
1421 hdr->b_mfu_ghost_hits = 0;
1423 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1426 buf->b_efunc = NULL;
1427 buf->b_private = NULL;
1430 arc_get_data_buf(buf);
1433 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1434 (void) refcount_add(&hdr->b_refcnt, tag);
1439 static char *arc_onloan_tag = "onloan";
1442 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1443 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1444 * buffers must be returned to the arc before they can be used by the DMU or
1448 arc_loan_buf(spa_t *spa, int size)
1452 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1454 atomic_add_64(&arc_loaned_bytes, size);
1459 * Return a loaned arc buffer to the arc.
1462 arc_return_buf(arc_buf_t *buf, void *tag)
1464 arc_buf_hdr_t *hdr = buf->b_hdr;
1466 ASSERT(buf->b_data != NULL);
1467 (void) refcount_add(&hdr->b_refcnt, tag);
1468 (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1470 atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1473 /* Detach an arc_buf from a dbuf (tag) */
1475 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1479 ASSERT(buf->b_data != NULL);
1481 (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1482 (void) refcount_remove(&hdr->b_refcnt, tag);
1483 buf->b_efunc = NULL;
1484 buf->b_private = NULL;
1486 atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1490 arc_buf_clone(arc_buf_t *from)
1493 arc_buf_hdr_t *hdr = from->b_hdr;
1494 uint64_t size = hdr->b_size;
1496 ASSERT(hdr->b_state != arc_anon);
1498 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1501 buf->b_efunc = NULL;
1502 buf->b_private = NULL;
1503 buf->b_next = hdr->b_buf;
1505 arc_get_data_buf(buf);
1506 bcopy(from->b_data, buf->b_data, size);
1509 * This buffer already exists in the arc so create a duplicate
1510 * copy for the caller. If the buffer is associated with user data
1511 * then track the size and number of duplicates. These stats will be
1512 * updated as duplicate buffers are created and destroyed.
1514 if (hdr->b_type == ARC_BUFC_DATA) {
1515 ARCSTAT_BUMP(arcstat_duplicate_buffers);
1516 ARCSTAT_INCR(arcstat_duplicate_buffers_size, size);
1518 hdr->b_datacnt += 1;
1523 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1526 kmutex_t *hash_lock;
1529 * Check to see if this buffer is evicted. Callers
1530 * must verify b_data != NULL to know if the add_ref
1533 mutex_enter(&buf->b_evict_lock);
1534 if (buf->b_data == NULL) {
1535 mutex_exit(&buf->b_evict_lock);
1538 hash_lock = HDR_LOCK(buf->b_hdr);
1539 mutex_enter(hash_lock);
1541 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1542 mutex_exit(&buf->b_evict_lock);
1544 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1545 add_reference(hdr, hash_lock, tag);
1546 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1547 arc_access(hdr, hash_lock);
1548 mutex_exit(hash_lock);
1549 ARCSTAT_BUMP(arcstat_hits);
1550 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1551 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1552 data, metadata, hits);
1556 * Free the arc data buffer. If it is an l2arc write in progress,
1557 * the buffer is placed on l2arc_free_on_write to be freed later.
1560 arc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t))
1562 arc_buf_hdr_t *hdr = buf->b_hdr;
1564 if (HDR_L2_WRITING(hdr)) {
1565 l2arc_data_free_t *df;
1566 df = kmem_alloc(sizeof (l2arc_data_free_t), KM_PUSHPAGE);
1567 df->l2df_data = buf->b_data;
1568 df->l2df_size = hdr->b_size;
1569 df->l2df_func = free_func;
1570 mutex_enter(&l2arc_free_on_write_mtx);
1571 list_insert_head(l2arc_free_on_write, df);
1572 mutex_exit(&l2arc_free_on_write_mtx);
1573 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1575 free_func(buf->b_data, hdr->b_size);
1580 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1584 /* free up data associated with the buf */
1586 arc_state_t *state = buf->b_hdr->b_state;
1587 uint64_t size = buf->b_hdr->b_size;
1588 arc_buf_contents_t type = buf->b_hdr->b_type;
1590 arc_cksum_verify(buf);
1591 arc_buf_unwatch(buf);
1594 if (type == ARC_BUFC_METADATA) {
1595 arc_buf_data_free(buf, zio_buf_free);
1596 arc_space_return(size, ARC_SPACE_DATA);
1598 ASSERT(type == ARC_BUFC_DATA);
1599 arc_buf_data_free(buf, zio_data_buf_free);
1600 ARCSTAT_INCR(arcstat_data_size, -size);
1601 atomic_add_64(&arc_size, -size);
1604 if (list_link_active(&buf->b_hdr->b_arc_node)) {
1605 uint64_t *cnt = &state->arcs_lsize[type];
1607 ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1608 ASSERT(state != arc_anon);
1610 ASSERT3U(*cnt, >=, size);
1611 atomic_add_64(cnt, -size);
1613 ASSERT3U(state->arcs_size, >=, size);
1614 atomic_add_64(&state->arcs_size, -size);
1618 * If we're destroying a duplicate buffer make sure
1619 * that the appropriate statistics are updated.
1621 if (buf->b_hdr->b_datacnt > 1 &&
1622 buf->b_hdr->b_type == ARC_BUFC_DATA) {
1623 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
1624 ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size);
1626 ASSERT(buf->b_hdr->b_datacnt > 0);
1627 buf->b_hdr->b_datacnt -= 1;
1630 /* only remove the buf if requested */
1634 /* remove the buf from the hdr list */
1635 for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1637 *bufp = buf->b_next;
1640 ASSERT(buf->b_efunc == NULL);
1642 /* clean up the buf */
1644 kmem_cache_free(buf_cache, buf);
1648 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1650 l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1652 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1653 ASSERT3P(hdr->b_state, ==, arc_anon);
1654 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1656 if (l2hdr != NULL) {
1657 boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1659 * To prevent arc_free() and l2arc_evict() from
1660 * attempting to free the same buffer at the same time,
1661 * a FREE_IN_PROGRESS flag is given to arc_free() to
1662 * give it priority. l2arc_evict() can't destroy this
1663 * header while we are waiting on l2arc_buflist_mtx.
1665 * The hdr may be removed from l2ad_buflist before we
1666 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1668 if (!buflist_held) {
1669 mutex_enter(&l2arc_buflist_mtx);
1670 l2hdr = hdr->b_l2hdr;
1673 if (l2hdr != NULL) {
1674 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1675 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1676 ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize);
1677 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1678 arc_space_return(L2HDR_SIZE, ARC_SPACE_L2HDRS);
1679 if (hdr->b_state == arc_l2c_only)
1680 l2arc_hdr_stat_remove();
1681 hdr->b_l2hdr = NULL;
1685 mutex_exit(&l2arc_buflist_mtx);
1688 if (!BUF_EMPTY(hdr)) {
1689 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1690 buf_discard_identity(hdr);
1692 while (hdr->b_buf) {
1693 arc_buf_t *buf = hdr->b_buf;
1696 mutex_enter(&arc_eviction_mtx);
1697 mutex_enter(&buf->b_evict_lock);
1698 ASSERT(buf->b_hdr != NULL);
1699 arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1700 hdr->b_buf = buf->b_next;
1701 buf->b_hdr = &arc_eviction_hdr;
1702 buf->b_next = arc_eviction_list;
1703 arc_eviction_list = buf;
1704 mutex_exit(&buf->b_evict_lock);
1705 mutex_exit(&arc_eviction_mtx);
1707 arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1710 if (hdr->b_freeze_cksum != NULL) {
1711 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1712 hdr->b_freeze_cksum = NULL;
1715 ASSERT(!list_link_active(&hdr->b_arc_node));
1716 ASSERT3P(hdr->b_hash_next, ==, NULL);
1717 ASSERT3P(hdr->b_acb, ==, NULL);
1718 kmem_cache_free(hdr_cache, hdr);
1722 arc_buf_free(arc_buf_t *buf, void *tag)
1724 arc_buf_hdr_t *hdr = buf->b_hdr;
1725 int hashed = hdr->b_state != arc_anon;
1727 ASSERT(buf->b_efunc == NULL);
1728 ASSERT(buf->b_data != NULL);
1731 kmutex_t *hash_lock = HDR_LOCK(hdr);
1733 mutex_enter(hash_lock);
1735 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1737 (void) remove_reference(hdr, hash_lock, tag);
1738 if (hdr->b_datacnt > 1) {
1739 arc_buf_destroy(buf, FALSE, TRUE);
1741 ASSERT(buf == hdr->b_buf);
1742 ASSERT(buf->b_efunc == NULL);
1743 hdr->b_flags |= ARC_BUF_AVAILABLE;
1745 mutex_exit(hash_lock);
1746 } else if (HDR_IO_IN_PROGRESS(hdr)) {
1749 * We are in the middle of an async write. Don't destroy
1750 * this buffer unless the write completes before we finish
1751 * decrementing the reference count.
1753 mutex_enter(&arc_eviction_mtx);
1754 (void) remove_reference(hdr, NULL, tag);
1755 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1756 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1757 mutex_exit(&arc_eviction_mtx);
1759 arc_hdr_destroy(hdr);
1761 if (remove_reference(hdr, NULL, tag) > 0)
1762 arc_buf_destroy(buf, FALSE, TRUE);
1764 arc_hdr_destroy(hdr);
1769 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1771 arc_buf_hdr_t *hdr = buf->b_hdr;
1772 kmutex_t *hash_lock = NULL;
1773 boolean_t no_callback = (buf->b_efunc == NULL);
1775 if (hdr->b_state == arc_anon) {
1776 ASSERT(hdr->b_datacnt == 1);
1777 arc_buf_free(buf, tag);
1778 return (no_callback);
1781 hash_lock = HDR_LOCK(hdr);
1782 mutex_enter(hash_lock);
1784 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1785 ASSERT(hdr->b_state != arc_anon);
1786 ASSERT(buf->b_data != NULL);
1788 (void) remove_reference(hdr, hash_lock, tag);
1789 if (hdr->b_datacnt > 1) {
1791 arc_buf_destroy(buf, FALSE, TRUE);
1792 } else if (no_callback) {
1793 ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1794 ASSERT(buf->b_efunc == NULL);
1795 hdr->b_flags |= ARC_BUF_AVAILABLE;
1797 ASSERT(no_callback || hdr->b_datacnt > 1 ||
1798 refcount_is_zero(&hdr->b_refcnt));
1799 mutex_exit(hash_lock);
1800 return (no_callback);
1804 arc_buf_size(arc_buf_t *buf)
1806 return (buf->b_hdr->b_size);
1810 * Called from the DMU to determine if the current buffer should be
1811 * evicted. In order to ensure proper locking, the eviction must be initiated
1812 * from the DMU. Return true if the buffer is associated with user data and
1813 * duplicate buffers still exist.
1816 arc_buf_eviction_needed(arc_buf_t *buf)
1819 boolean_t evict_needed = B_FALSE;
1821 if (zfs_disable_dup_eviction)
1824 mutex_enter(&buf->b_evict_lock);
1828 * We are in arc_do_user_evicts(); let that function
1829 * perform the eviction.
1831 ASSERT(buf->b_data == NULL);
1832 mutex_exit(&buf->b_evict_lock);
1834 } else if (buf->b_data == NULL) {
1836 * We have already been added to the arc eviction list;
1837 * recommend eviction.
1839 ASSERT3P(hdr, ==, &arc_eviction_hdr);
1840 mutex_exit(&buf->b_evict_lock);
1844 if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA)
1845 evict_needed = B_TRUE;
1847 mutex_exit(&buf->b_evict_lock);
1848 return (evict_needed);
1852 * Evict buffers from list until we've removed the specified number of
1853 * bytes. Move the removed buffers to the appropriate evict state.
1854 * If the recycle flag is set, then attempt to "recycle" a buffer:
1855 * - look for a buffer to evict that is `bytes' long.
1856 * - return the data block from this buffer rather than freeing it.
1857 * This flag is used by callers that are trying to make space for a
1858 * new buffer in a full arc cache.
1860 * This function makes a "best effort". It skips over any buffers
1861 * it can't get a hash_lock on, and so may not catch all candidates.
1862 * It may also return without evicting as much space as requested.
1865 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1866 arc_buf_contents_t type)
1868 arc_state_t *evicted_state;
1869 uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1870 arc_buf_hdr_t *ab, *ab_prev = NULL;
1871 list_t *list = &state->arcs_list[type];
1872 kmutex_t *hash_lock;
1873 boolean_t have_lock;
1874 void *stolen = NULL;
1875 arc_buf_hdr_t marker = {{{ 0 }}};
1878 ASSERT(state == arc_mru || state == arc_mfu);
1880 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1882 mutex_enter(&state->arcs_mtx);
1883 mutex_enter(&evicted_state->arcs_mtx);
1885 for (ab = list_tail(list); ab; ab = ab_prev) {
1886 ab_prev = list_prev(list, ab);
1887 /* prefetch buffers have a minimum lifespan */
1888 if (HDR_IO_IN_PROGRESS(ab) ||
1889 (spa && ab->b_spa != spa) ||
1890 (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1891 ddi_get_lbolt() - ab->b_arc_access <
1892 zfs_arc_min_prefetch_lifespan)) {
1896 /* "lookahead" for better eviction candidate */
1897 if (recycle && ab->b_size != bytes &&
1898 ab_prev && ab_prev->b_size == bytes)
1901 /* ignore markers */
1906 * It may take a long time to evict all the bufs requested.
1907 * To avoid blocking all arc activity, periodically drop
1908 * the arcs_mtx and give other threads a chance to run
1909 * before reacquiring the lock.
1911 * If we are looking for a buffer to recycle, we are in
1912 * the hot code path, so don't sleep.
1914 if (!recycle && count++ > arc_evict_iterations) {
1915 list_insert_after(list, ab, &marker);
1916 mutex_exit(&evicted_state->arcs_mtx);
1917 mutex_exit(&state->arcs_mtx);
1918 kpreempt(KPREEMPT_SYNC);
1919 mutex_enter(&state->arcs_mtx);
1920 mutex_enter(&evicted_state->arcs_mtx);
1921 ab_prev = list_prev(list, &marker);
1922 list_remove(list, &marker);
1927 hash_lock = HDR_LOCK(ab);
1928 have_lock = MUTEX_HELD(hash_lock);
1929 if (have_lock || mutex_tryenter(hash_lock)) {
1930 ASSERT0(refcount_count(&ab->b_refcnt));
1931 ASSERT(ab->b_datacnt > 0);
1933 arc_buf_t *buf = ab->b_buf;
1934 if (!mutex_tryenter(&buf->b_evict_lock)) {
1939 bytes_evicted += ab->b_size;
1940 if (recycle && ab->b_type == type &&
1941 ab->b_size == bytes &&
1942 !HDR_L2_WRITING(ab)) {
1943 stolen = buf->b_data;
1948 mutex_enter(&arc_eviction_mtx);
1949 arc_buf_destroy(buf,
1950 buf->b_data == stolen, FALSE);
1951 ab->b_buf = buf->b_next;
1952 buf->b_hdr = &arc_eviction_hdr;
1953 buf->b_next = arc_eviction_list;
1954 arc_eviction_list = buf;
1955 mutex_exit(&arc_eviction_mtx);
1956 mutex_exit(&buf->b_evict_lock);
1958 mutex_exit(&buf->b_evict_lock);
1959 arc_buf_destroy(buf,
1960 buf->b_data == stolen, TRUE);
1965 ARCSTAT_INCR(arcstat_evict_l2_cached,
1968 if (l2arc_write_eligible(ab->b_spa, ab)) {
1969 ARCSTAT_INCR(arcstat_evict_l2_eligible,
1973 arcstat_evict_l2_ineligible,
1978 if (ab->b_datacnt == 0) {
1979 arc_change_state(evicted_state, ab, hash_lock);
1980 ASSERT(HDR_IN_HASH_TABLE(ab));
1981 ab->b_flags |= ARC_IN_HASH_TABLE;
1982 ab->b_flags &= ~ARC_BUF_AVAILABLE;
1983 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1986 mutex_exit(hash_lock);
1987 if (bytes >= 0 && bytes_evicted >= bytes)
1994 mutex_exit(&evicted_state->arcs_mtx);
1995 mutex_exit(&state->arcs_mtx);
1997 if (bytes_evicted < bytes)
1998 dprintf("only evicted %lld bytes from %x\n",
1999 (longlong_t)bytes_evicted, state);
2002 ARCSTAT_INCR(arcstat_evict_skip, skipped);
2005 ARCSTAT_INCR(arcstat_mutex_miss, missed);
2008 * Note: we have just evicted some data into the ghost state,
2009 * potentially putting the ghost size over the desired size. Rather
2010 * that evicting from the ghost list in this hot code path, leave
2011 * this chore to the arc_reclaim_thread().
2018 * Remove buffers from list until we've removed the specified number of
2019 * bytes. Destroy the buffers that are removed.
2022 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes,
2023 arc_buf_contents_t type)
2025 arc_buf_hdr_t *ab, *ab_prev;
2026 arc_buf_hdr_t marker;
2027 list_t *list = &state->arcs_list[type];
2028 kmutex_t *hash_lock;
2029 uint64_t bytes_deleted = 0;
2030 uint64_t bufs_skipped = 0;
2033 ASSERT(GHOST_STATE(state));
2034 bzero(&marker, sizeof(marker));
2036 mutex_enter(&state->arcs_mtx);
2037 for (ab = list_tail(list); ab; ab = ab_prev) {
2038 ab_prev = list_prev(list, ab);
2039 if (ab->b_type > ARC_BUFC_NUMTYPES)
2040 panic("invalid ab=%p", (void *)ab);
2041 if (spa && ab->b_spa != spa)
2044 /* ignore markers */
2048 hash_lock = HDR_LOCK(ab);
2049 /* caller may be trying to modify this buffer, skip it */
2050 if (MUTEX_HELD(hash_lock))
2054 * It may take a long time to evict all the bufs requested.
2055 * To avoid blocking all arc activity, periodically drop
2056 * the arcs_mtx and give other threads a chance to run
2057 * before reacquiring the lock.
2059 if (count++ > arc_evict_iterations) {
2060 list_insert_after(list, ab, &marker);
2061 mutex_exit(&state->arcs_mtx);
2062 kpreempt(KPREEMPT_SYNC);
2063 mutex_enter(&state->arcs_mtx);
2064 ab_prev = list_prev(list, &marker);
2065 list_remove(list, &marker);
2069 if (mutex_tryenter(hash_lock)) {
2070 ASSERT(!HDR_IO_IN_PROGRESS(ab));
2071 ASSERT(ab->b_buf == NULL);
2072 ARCSTAT_BUMP(arcstat_deleted);
2073 bytes_deleted += ab->b_size;
2075 if (ab->b_l2hdr != NULL) {
2077 * This buffer is cached on the 2nd Level ARC;
2078 * don't destroy the header.
2080 arc_change_state(arc_l2c_only, ab, hash_lock);
2081 mutex_exit(hash_lock);
2083 arc_change_state(arc_anon, ab, hash_lock);
2084 mutex_exit(hash_lock);
2085 arc_hdr_destroy(ab);
2088 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
2089 if (bytes >= 0 && bytes_deleted >= bytes)
2091 } else if (bytes < 0) {
2093 * Insert a list marker and then wait for the
2094 * hash lock to become available. Once its
2095 * available, restart from where we left off.
2097 list_insert_after(list, ab, &marker);
2098 mutex_exit(&state->arcs_mtx);
2099 mutex_enter(hash_lock);
2100 mutex_exit(hash_lock);
2101 mutex_enter(&state->arcs_mtx);
2102 ab_prev = list_prev(list, &marker);
2103 list_remove(list, &marker);
2108 mutex_exit(&state->arcs_mtx);
2110 if (list == &state->arcs_list[ARC_BUFC_DATA] &&
2111 (bytes < 0 || bytes_deleted < bytes)) {
2112 list = &state->arcs_list[ARC_BUFC_METADATA];
2117 ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
2121 if (bytes_deleted < bytes)
2122 dprintf("only deleted %lld bytes from %p\n",
2123 (longlong_t)bytes_deleted, state);
2129 int64_t adjustment, delta;
2135 adjustment = MIN((int64_t)(arc_size - arc_c),
2136 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
2139 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
2140 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
2141 (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA);
2142 adjustment -= delta;
2145 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2146 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2147 (void) arc_evict(arc_mru, 0, delta, FALSE,
2155 adjustment = arc_size - arc_c;
2157 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
2158 delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
2159 (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA);
2160 adjustment -= delta;
2163 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2164 int64_t delta = MIN(adjustment,
2165 arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
2166 (void) arc_evict(arc_mfu, 0, delta, FALSE,
2171 * Adjust ghost lists
2174 adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
2176 if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
2177 delta = MIN(arc_mru_ghost->arcs_size, adjustment);
2178 arc_evict_ghost(arc_mru_ghost, 0, delta, ARC_BUFC_DATA);
2182 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
2184 if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
2185 delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
2186 arc_evict_ghost(arc_mfu_ghost, 0, delta, ARC_BUFC_DATA);
2191 * Request that arc user drop references so that N bytes can be released
2192 * from the cache. This provides a mechanism to ensure the arc can honor
2193 * the arc_meta_limit and reclaim buffers which are pinned in the cache
2194 * by higher layers. (i.e. the zpl)
2197 arc_do_user_prune(int64_t adjustment)
2199 arc_prune_func_t *func;
2201 arc_prune_t *cp, *np;
2203 mutex_enter(&arc_prune_mtx);
2205 cp = list_head(&arc_prune_list);
2206 while (cp != NULL) {
2208 private = cp->p_private;
2209 np = list_next(&arc_prune_list, cp);
2210 refcount_add(&cp->p_refcnt, func);
2211 mutex_exit(&arc_prune_mtx);
2214 func(adjustment, private);
2216 mutex_enter(&arc_prune_mtx);
2218 /* User removed prune callback concurrently with execution */
2219 if (refcount_remove(&cp->p_refcnt, func) == 0) {
2220 ASSERT(!list_link_active(&cp->p_node));
2221 refcount_destroy(&cp->p_refcnt);
2222 kmem_free(cp, sizeof (*cp));
2228 ARCSTAT_BUMP(arcstat_prune);
2229 mutex_exit(&arc_prune_mtx);
2233 arc_do_user_evicts(void)
2235 mutex_enter(&arc_eviction_mtx);
2236 while (arc_eviction_list != NULL) {
2237 arc_buf_t *buf = arc_eviction_list;
2238 arc_eviction_list = buf->b_next;
2239 mutex_enter(&buf->b_evict_lock);
2241 mutex_exit(&buf->b_evict_lock);
2242 mutex_exit(&arc_eviction_mtx);
2244 if (buf->b_efunc != NULL)
2245 VERIFY(buf->b_efunc(buf) == 0);
2247 buf->b_efunc = NULL;
2248 buf->b_private = NULL;
2249 kmem_cache_free(buf_cache, buf);
2250 mutex_enter(&arc_eviction_mtx);
2252 mutex_exit(&arc_eviction_mtx);
2256 * Evict only meta data objects from the cache leaving the data objects.
2257 * This is only used to enforce the tunable arc_meta_limit, if we are
2258 * unable to evict enough buffers notify the user via the prune callback.
2261 arc_adjust_meta(int64_t adjustment, boolean_t may_prune)
2265 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2266 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2267 arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_METADATA);
2268 adjustment -= delta;
2271 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2272 delta = MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2273 arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_METADATA);
2274 adjustment -= delta;
2277 if (may_prune && (adjustment > 0) && (arc_meta_used > arc_meta_limit))
2278 arc_do_user_prune(zfs_arc_meta_prune);
2282 * Flush all *evictable* data from the cache for the given spa.
2283 * NOTE: this will not touch "active" (i.e. referenced) data.
2286 arc_flush(spa_t *spa)
2291 guid = spa_load_guid(spa);
2293 while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
2294 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
2298 while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
2299 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
2303 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
2304 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
2308 while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
2309 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
2314 arc_evict_ghost(arc_mru_ghost, guid, -1, ARC_BUFC_DATA);
2315 arc_evict_ghost(arc_mfu_ghost, guid, -1, ARC_BUFC_DATA);
2317 mutex_enter(&arc_reclaim_thr_lock);
2318 arc_do_user_evicts();
2319 mutex_exit(&arc_reclaim_thr_lock);
2320 ASSERT(spa || arc_eviction_list == NULL);
2324 arc_shrink(uint64_t bytes)
2326 if (arc_c > arc_c_min) {
2329 to_free = bytes ? bytes : arc_c >> zfs_arc_shrink_shift;
2331 if (arc_c > arc_c_min + to_free)
2332 atomic_add_64(&arc_c, -to_free);
2336 atomic_add_64(&arc_p, -(arc_p >> zfs_arc_shrink_shift));
2337 if (arc_c > arc_size)
2338 arc_c = MAX(arc_size, arc_c_min);
2340 arc_p = (arc_c >> 1);
2341 ASSERT(arc_c >= arc_c_min);
2342 ASSERT((int64_t)arc_p >= 0);
2345 if (arc_size > arc_c)
2350 arc_kmem_reap_now(arc_reclaim_strategy_t strat, uint64_t bytes)
2353 kmem_cache_t *prev_cache = NULL;
2354 kmem_cache_t *prev_data_cache = NULL;
2355 extern kmem_cache_t *zio_buf_cache[];
2356 extern kmem_cache_t *zio_data_buf_cache[];
2359 * An aggressive reclamation will shrink the cache size as well as
2360 * reap free buffers from the arc kmem caches.
2362 if (strat == ARC_RECLAIM_AGGR)
2365 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2366 if (zio_buf_cache[i] != prev_cache) {
2367 prev_cache = zio_buf_cache[i];
2368 kmem_cache_reap_now(zio_buf_cache[i]);
2370 if (zio_data_buf_cache[i] != prev_data_cache) {
2371 prev_data_cache = zio_data_buf_cache[i];
2372 kmem_cache_reap_now(zio_data_buf_cache[i]);
2376 kmem_cache_reap_now(buf_cache);
2377 kmem_cache_reap_now(hdr_cache);
2381 * Unlike other ZFS implementations this thread is only responsible for
2382 * adapting the target ARC size on Linux. The responsibility for memory
2383 * reclamation has been entirely delegated to the arc_shrinker_func()
2384 * which is registered with the VM. To reflect this change in behavior
2385 * the arc_reclaim thread has been renamed to arc_adapt.
2388 arc_adapt_thread(void)
2393 CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2395 mutex_enter(&arc_reclaim_thr_lock);
2396 while (arc_thread_exit == 0) {
2398 arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS;
2400 if (spa_get_random(100) == 0) {
2403 if (last_reclaim == ARC_RECLAIM_CONS) {
2404 last_reclaim = ARC_RECLAIM_AGGR;
2406 last_reclaim = ARC_RECLAIM_CONS;
2410 last_reclaim = ARC_RECLAIM_AGGR;
2414 /* reset the growth delay for every reclaim */
2415 arc_grow_time = ddi_get_lbolt()+(zfs_arc_grow_retry * hz);
2417 arc_kmem_reap_now(last_reclaim, 0);
2420 #endif /* !_KERNEL */
2422 /* No recent memory pressure allow the ARC to grow. */
2423 if (arc_no_grow && ddi_get_lbolt() >= arc_grow_time)
2424 arc_no_grow = FALSE;
2427 * Keep meta data usage within limits, arc_shrink() is not
2428 * used to avoid collapsing the arc_c value when only the
2429 * arc_meta_limit is being exceeded.
2431 prune = (int64_t)arc_meta_used - (int64_t)arc_meta_limit;
2433 arc_adjust_meta(prune, B_TRUE);
2437 if (arc_eviction_list != NULL)
2438 arc_do_user_evicts();
2440 /* block until needed, or one second, whichever is shorter */
2441 CALLB_CPR_SAFE_BEGIN(&cpr);
2442 (void) cv_timedwait_interruptible(&arc_reclaim_thr_cv,
2443 &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2444 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2447 /* Allow the module options to be changed */
2448 if (zfs_arc_max > 64 << 20 &&
2449 zfs_arc_max < physmem * PAGESIZE &&
2450 zfs_arc_max != arc_c_max)
2451 arc_c_max = zfs_arc_max;
2453 if (zfs_arc_min > 0 &&
2454 zfs_arc_min < arc_c_max &&
2455 zfs_arc_min != arc_c_min)
2456 arc_c_min = zfs_arc_min;
2458 if (zfs_arc_meta_limit > 0 &&
2459 zfs_arc_meta_limit <= arc_c_max &&
2460 zfs_arc_meta_limit != arc_meta_limit)
2461 arc_meta_limit = zfs_arc_meta_limit;
2467 arc_thread_exit = 0;
2468 cv_broadcast(&arc_reclaim_thr_cv);
2469 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */
2475 * Determine the amount of memory eligible for eviction contained in the
2476 * ARC. All clean data reported by the ghost lists can always be safely
2477 * evicted. Due to arc_c_min, the same does not hold for all clean data
2478 * contained by the regular mru and mfu lists.
2480 * In the case of the regular mru and mfu lists, we need to report as
2481 * much clean data as possible, such that evicting that same reported
2482 * data will not bring arc_size below arc_c_min. Thus, in certain
2483 * circumstances, the total amount of clean data in the mru and mfu
2484 * lists might not actually be evictable.
2486 * The following two distinct cases are accounted for:
2488 * 1. The sum of the amount of dirty data contained by both the mru and
2489 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2490 * is greater than or equal to arc_c_min.
2491 * (i.e. amount of dirty data >= arc_c_min)
2493 * This is the easy case; all clean data contained by the mru and mfu
2494 * lists is evictable. Evicting all clean data can only drop arc_size
2495 * to the amount of dirty data, which is greater than arc_c_min.
2497 * 2. The sum of the amount of dirty data contained by both the mru and
2498 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
2499 * is less than arc_c_min.
2500 * (i.e. arc_c_min > amount of dirty data)
2502 * 2.1. arc_size is greater than or equal arc_c_min.
2503 * (i.e. arc_size >= arc_c_min > amount of dirty data)
2505 * In this case, not all clean data from the regular mru and mfu
2506 * lists is actually evictable; we must leave enough clean data
2507 * to keep arc_size above arc_c_min. Thus, the maximum amount of
2508 * evictable data from the two lists combined, is exactly the
2509 * difference between arc_size and arc_c_min.
2511 * 2.2. arc_size is less than arc_c_min
2512 * (i.e. arc_c_min > arc_size > amount of dirty data)
2514 * In this case, none of the data contained in the mru and mfu
2515 * lists is evictable, even if it's clean. Since arc_size is
2516 * already below arc_c_min, evicting any more would only
2517 * increase this negative difference.
2520 arc_evictable_memory(void) {
2521 uint64_t arc_clean =
2522 arc_mru->arcs_lsize[ARC_BUFC_DATA] +
2523 arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
2524 arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
2525 arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
2526 uint64_t ghost_clean =
2527 arc_mru_ghost->arcs_lsize[ARC_BUFC_DATA] +
2528 arc_mru_ghost->arcs_lsize[ARC_BUFC_METADATA] +
2529 arc_mfu_ghost->arcs_lsize[ARC_BUFC_DATA] +
2530 arc_mfu_ghost->arcs_lsize[ARC_BUFC_METADATA];
2531 uint64_t arc_dirty = MAX((int64_t)arc_size - (int64_t)arc_clean, 0);
2533 if (arc_dirty >= arc_c_min)
2534 return (ghost_clean + arc_clean);
2536 return (ghost_clean + MAX((int64_t)arc_size - (int64_t)arc_c_min, 0));
2540 __arc_shrinker_func(struct shrinker *shrink, struct shrink_control *sc)
2544 /* The arc is considered warm once reclaim has occurred */
2545 if (unlikely(arc_warm == B_FALSE))
2548 /* Return the potential number of reclaimable pages */
2549 pages = btop(arc_evictable_memory());
2550 if (sc->nr_to_scan == 0)
2553 /* Not allowed to perform filesystem reclaim */
2554 if (!(sc->gfp_mask & __GFP_FS))
2557 /* Reclaim in progress */
2558 if (mutex_tryenter(&arc_reclaim_thr_lock) == 0)
2562 * Evict the requested number of pages by shrinking arc_c the
2563 * requested amount. If there is nothing left to evict just
2564 * reap whatever we can from the various arc slabs.
2567 arc_kmem_reap_now(ARC_RECLAIM_AGGR, ptob(sc->nr_to_scan));
2569 arc_kmem_reap_now(ARC_RECLAIM_CONS, ptob(sc->nr_to_scan));
2573 * When direct reclaim is observed it usually indicates a rapid
2574 * increase in memory pressure. This occurs because the kswapd
2575 * threads were unable to asynchronously keep enough free memory
2576 * available. In this case set arc_no_grow to briefly pause arc
2577 * growth to avoid compounding the memory pressure.
2579 if (current_is_kswapd()) {
2580 ARCSTAT_BUMP(arcstat_memory_indirect_count);
2582 arc_no_grow = B_TRUE;
2583 arc_grow_time = ddi_get_lbolt() + (zfs_arc_grow_retry * hz);
2584 ARCSTAT_BUMP(arcstat_memory_direct_count);
2587 mutex_exit(&arc_reclaim_thr_lock);
2591 SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func);
2593 SPL_SHRINKER_DECLARE(arc_shrinker, arc_shrinker_func, DEFAULT_SEEKS);
2594 #endif /* _KERNEL */
2597 * Adapt arc info given the number of bytes we are trying to add and
2598 * the state that we are comming from. This function is only called
2599 * when we are adding new content to the cache.
2602 arc_adapt(int bytes, arc_state_t *state)
2605 uint64_t arc_p_min = (arc_c >> zfs_arc_p_min_shift);
2607 if (state == arc_l2c_only)
2612 * Adapt the target size of the MRU list:
2613 * - if we just hit in the MRU ghost list, then increase
2614 * the target size of the MRU list.
2615 * - if we just hit in the MFU ghost list, then increase
2616 * the target size of the MFU list by decreasing the
2617 * target size of the MRU list.
2619 if (state == arc_mru_ghost) {
2620 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2621 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2622 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2624 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2625 } else if (state == arc_mfu_ghost) {
2628 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2629 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2630 mult = MIN(mult, 10);
2632 delta = MIN(bytes * mult, arc_p);
2633 arc_p = MAX(arc_p_min, arc_p - delta);
2635 ASSERT((int64_t)arc_p >= 0);
2640 if (arc_c >= arc_c_max)
2644 * If we're within (2 * maxblocksize) bytes of the target
2645 * cache size, increment the target cache size
2647 if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2648 atomic_add_64(&arc_c, (int64_t)bytes);
2649 if (arc_c > arc_c_max)
2651 else if (state == arc_anon)
2652 atomic_add_64(&arc_p, (int64_t)bytes);
2656 ASSERT((int64_t)arc_p >= 0);
2660 * Check if the cache has reached its limits and eviction is required
2664 arc_evict_needed(arc_buf_contents_t type)
2666 if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2672 return (arc_size > arc_c);
2676 * The buffer, supplied as the first argument, needs a data block.
2677 * So, if we are at cache max, determine which cache should be victimized.
2678 * We have the following cases:
2680 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2681 * In this situation if we're out of space, but the resident size of the MFU is
2682 * under the limit, victimize the MFU cache to satisfy this insertion request.
2684 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2685 * Here, we've used up all of the available space for the MRU, so we need to
2686 * evict from our own cache instead. Evict from the set of resident MRU
2689 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2690 * c minus p represents the MFU space in the cache, since p is the size of the
2691 * cache that is dedicated to the MRU. In this situation there's still space on
2692 * the MFU side, so the MRU side needs to be victimized.
2694 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2695 * MFU's resident set is consuming more space than it has been allotted. In
2696 * this situation, we must victimize our own cache, the MFU, for this insertion.
2699 arc_get_data_buf(arc_buf_t *buf)
2701 arc_state_t *state = buf->b_hdr->b_state;
2702 uint64_t size = buf->b_hdr->b_size;
2703 arc_buf_contents_t type = buf->b_hdr->b_type;
2705 arc_adapt(size, state);
2708 * We have not yet reached cache maximum size,
2709 * just allocate a new buffer.
2711 if (!arc_evict_needed(type)) {
2712 if (type == ARC_BUFC_METADATA) {
2713 buf->b_data = zio_buf_alloc(size);
2714 arc_space_consume(size, ARC_SPACE_DATA);
2716 ASSERT(type == ARC_BUFC_DATA);
2717 buf->b_data = zio_data_buf_alloc(size);
2718 ARCSTAT_INCR(arcstat_data_size, size);
2719 atomic_add_64(&arc_size, size);
2725 * If we are prefetching from the mfu ghost list, this buffer
2726 * will end up on the mru list; so steal space from there.
2728 if (state == arc_mfu_ghost)
2729 state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2730 else if (state == arc_mru_ghost)
2733 if (state == arc_mru || state == arc_anon) {
2734 uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2735 state = (arc_mfu->arcs_lsize[type] >= size &&
2736 arc_p > mru_used) ? arc_mfu : arc_mru;
2739 uint64_t mfu_space = arc_c - arc_p;
2740 state = (arc_mru->arcs_lsize[type] >= size &&
2741 mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2744 if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) {
2745 if (type == ARC_BUFC_METADATA) {
2746 buf->b_data = zio_buf_alloc(size);
2747 arc_space_consume(size, ARC_SPACE_DATA);
2750 * If we are unable to recycle an existing meta buffer
2751 * signal the reclaim thread. It will notify users
2752 * via the prune callback to drop references. The
2753 * prune callback in run in the context of the reclaim
2754 * thread to avoid deadlocking on the hash_lock.
2756 cv_signal(&arc_reclaim_thr_cv);
2758 ASSERT(type == ARC_BUFC_DATA);
2759 buf->b_data = zio_data_buf_alloc(size);
2760 ARCSTAT_INCR(arcstat_data_size, size);
2761 atomic_add_64(&arc_size, size);
2764 ARCSTAT_BUMP(arcstat_recycle_miss);
2766 ASSERT(buf->b_data != NULL);
2769 * Update the state size. Note that ghost states have a
2770 * "ghost size" and so don't need to be updated.
2772 if (!GHOST_STATE(buf->b_hdr->b_state)) {
2773 arc_buf_hdr_t *hdr = buf->b_hdr;
2775 atomic_add_64(&hdr->b_state->arcs_size, size);
2776 if (list_link_active(&hdr->b_arc_node)) {
2777 ASSERT(refcount_is_zero(&hdr->b_refcnt));
2778 atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2781 * If we are growing the cache, and we are adding anonymous
2782 * data, and we have outgrown arc_p, update arc_p
2784 if (arc_size < arc_c && hdr->b_state == arc_anon &&
2785 arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2786 arc_p = MIN(arc_c, arc_p + size);
2791 * This routine is called whenever a buffer is accessed.
2792 * NOTE: the hash lock is dropped in this function.
2795 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2799 ASSERT(MUTEX_HELD(hash_lock));
2801 if (buf->b_state == arc_anon) {
2803 * This buffer is not in the cache, and does not
2804 * appear in our "ghost" list. Add the new buffer
2808 ASSERT(buf->b_arc_access == 0);
2809 buf->b_arc_access = ddi_get_lbolt();
2810 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2811 arc_change_state(arc_mru, buf, hash_lock);
2813 } else if (buf->b_state == arc_mru) {
2814 now = ddi_get_lbolt();
2817 * If this buffer is here because of a prefetch, then either:
2818 * - clear the flag if this is a "referencing" read
2819 * (any subsequent access will bump this into the MFU state).
2821 * - move the buffer to the head of the list if this is
2822 * another prefetch (to make it less likely to be evicted).
2824 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2825 if (refcount_count(&buf->b_refcnt) == 0) {
2826 ASSERT(list_link_active(&buf->b_arc_node));
2828 buf->b_flags &= ~ARC_PREFETCH;
2829 atomic_inc_32(&buf->b_mru_hits);
2830 ARCSTAT_BUMP(arcstat_mru_hits);
2832 buf->b_arc_access = now;
2837 * This buffer has been "accessed" only once so far,
2838 * but it is still in the cache. Move it to the MFU
2841 if (now > buf->b_arc_access + ARC_MINTIME) {
2843 * More than 125ms have passed since we
2844 * instantiated this buffer. Move it to the
2845 * most frequently used state.
2847 buf->b_arc_access = now;
2848 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2849 arc_change_state(arc_mfu, buf, hash_lock);
2851 atomic_inc_32(&buf->b_mru_hits);
2852 ARCSTAT_BUMP(arcstat_mru_hits);
2853 } else if (buf->b_state == arc_mru_ghost) {
2854 arc_state_t *new_state;
2856 * This buffer has been "accessed" recently, but
2857 * was evicted from the cache. Move it to the
2861 if (buf->b_flags & ARC_PREFETCH) {
2862 new_state = arc_mru;
2863 if (refcount_count(&buf->b_refcnt) > 0)
2864 buf->b_flags &= ~ARC_PREFETCH;
2865 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2867 new_state = arc_mfu;
2868 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2871 buf->b_arc_access = ddi_get_lbolt();
2872 arc_change_state(new_state, buf, hash_lock);
2874 atomic_inc_32(&buf->b_mru_ghost_hits);
2875 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2876 } else if (buf->b_state == arc_mfu) {
2878 * This buffer has been accessed more than once and is
2879 * still in the cache. Keep it in the MFU state.
2881 * NOTE: an add_reference() that occurred when we did
2882 * the arc_read() will have kicked this off the list.
2883 * If it was a prefetch, we will explicitly move it to
2884 * the head of the list now.
2886 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2887 ASSERT(refcount_count(&buf->b_refcnt) == 0);
2888 ASSERT(list_link_active(&buf->b_arc_node));
2890 atomic_inc_32(&buf->b_mfu_hits);
2891 ARCSTAT_BUMP(arcstat_mfu_hits);
2892 buf->b_arc_access = ddi_get_lbolt();
2893 } else if (buf->b_state == arc_mfu_ghost) {
2894 arc_state_t *new_state = arc_mfu;
2896 * This buffer has been accessed more than once but has
2897 * been evicted from the cache. Move it back to the
2901 if (buf->b_flags & ARC_PREFETCH) {
2903 * This is a prefetch access...
2904 * move this block back to the MRU state.
2906 ASSERT0(refcount_count(&buf->b_refcnt));
2907 new_state = arc_mru;
2910 buf->b_arc_access = ddi_get_lbolt();
2911 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2912 arc_change_state(new_state, buf, hash_lock);
2914 atomic_inc_32(&buf->b_mfu_ghost_hits);
2915 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2916 } else if (buf->b_state == arc_l2c_only) {
2918 * This buffer is on the 2nd Level ARC.
2921 buf->b_arc_access = ddi_get_lbolt();
2922 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2923 arc_change_state(arc_mfu, buf, hash_lock);
2925 ASSERT(!"invalid arc state");
2929 /* a generic arc_done_func_t which you can use */
2932 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2934 if (zio == NULL || zio->io_error == 0)
2935 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2936 VERIFY(arc_buf_remove_ref(buf, arg));
2939 /* a generic arc_done_func_t */
2941 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2943 arc_buf_t **bufp = arg;
2944 if (zio && zio->io_error) {
2945 VERIFY(arc_buf_remove_ref(buf, arg));
2949 ASSERT(buf->b_data);
2954 arc_read_done(zio_t *zio)
2956 arc_buf_hdr_t *hdr, *found;
2958 arc_buf_t *abuf; /* buffer we're assigning to callback */
2959 kmutex_t *hash_lock;
2960 arc_callback_t *callback_list, *acb;
2961 int freeable = FALSE;
2963 buf = zio->io_private;
2967 * The hdr was inserted into hash-table and removed from lists
2968 * prior to starting I/O. We should find this header, since
2969 * it's in the hash table, and it should be legit since it's
2970 * not possible to evict it during the I/O. The only possible
2971 * reason for it not to be found is if we were freed during the
2974 found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
2977 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
2978 (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2979 (found == hdr && HDR_L2_READING(hdr)));
2981 hdr->b_flags &= ~ARC_L2_EVICTED;
2982 if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2983 hdr->b_flags &= ~ARC_L2CACHE;
2985 /* byteswap if necessary */
2986 callback_list = hdr->b_acb;
2987 ASSERT(callback_list != NULL);
2988 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2989 dmu_object_byteswap_t bswap =
2990 DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
2991 if (BP_GET_LEVEL(zio->io_bp) > 0)
2992 byteswap_uint64_array(buf->b_data, hdr->b_size);
2994 dmu_ot_byteswap[bswap].ob_func(buf->b_data, hdr->b_size);
2997 arc_cksum_compute(buf, B_FALSE);
3000 if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
3002 * Only call arc_access on anonymous buffers. This is because
3003 * if we've issued an I/O for an evicted buffer, we've already
3004 * called arc_access (to prevent any simultaneous readers from
3005 * getting confused).
3007 arc_access(hdr, hash_lock);
3010 /* create copies of the data buffer for the callers */
3012 for (acb = callback_list; acb; acb = acb->acb_next) {
3013 if (acb->acb_done) {
3015 ARCSTAT_BUMP(arcstat_duplicate_reads);
3016 abuf = arc_buf_clone(buf);
3018 acb->acb_buf = abuf;
3023 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3024 ASSERT(!HDR_BUF_AVAILABLE(hdr));
3026 ASSERT(buf->b_efunc == NULL);
3027 ASSERT(hdr->b_datacnt == 1);
3028 hdr->b_flags |= ARC_BUF_AVAILABLE;
3031 ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
3033 if (zio->io_error != 0) {
3034 hdr->b_flags |= ARC_IO_ERROR;
3035 if (hdr->b_state != arc_anon)
3036 arc_change_state(arc_anon, hdr, hash_lock);
3037 if (HDR_IN_HASH_TABLE(hdr))
3038 buf_hash_remove(hdr);
3039 freeable = refcount_is_zero(&hdr->b_refcnt);
3043 * Broadcast before we drop the hash_lock to avoid the possibility
3044 * that the hdr (and hence the cv) might be freed before we get to
3045 * the cv_broadcast().
3047 cv_broadcast(&hdr->b_cv);
3050 mutex_exit(hash_lock);
3053 * This block was freed while we waited for the read to
3054 * complete. It has been removed from the hash table and
3055 * moved to the anonymous state (so that it won't show up
3058 ASSERT3P(hdr->b_state, ==, arc_anon);
3059 freeable = refcount_is_zero(&hdr->b_refcnt);
3062 /* execute each callback and free its structure */
3063 while ((acb = callback_list) != NULL) {
3065 acb->acb_done(zio, acb->acb_buf, acb->acb_private);
3067 if (acb->acb_zio_dummy != NULL) {
3068 acb->acb_zio_dummy->io_error = zio->io_error;
3069 zio_nowait(acb->acb_zio_dummy);
3072 callback_list = acb->acb_next;
3073 kmem_free(acb, sizeof (arc_callback_t));
3077 arc_hdr_destroy(hdr);
3081 * "Read" the block at the specified DVA (in bp) via the
3082 * cache. If the block is found in the cache, invoke the provided
3083 * callback immediately and return. Note that the `zio' parameter
3084 * in the callback will be NULL in this case, since no IO was
3085 * required. If the block is not in the cache pass the read request
3086 * on to the spa with a substitute callback function, so that the
3087 * requested block will be added to the cache.
3089 * If a read request arrives for a block that has a read in-progress,
3090 * either wait for the in-progress read to complete (and return the
3091 * results); or, if this is a read with a "done" func, add a record
3092 * to the read to invoke the "done" func when the read completes,
3093 * and return; or just return.
3095 * arc_read_done() will invoke all the requested "done" functions
3096 * for readers of this block.
3099 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done,
3100 void *private, zio_priority_t priority, int zio_flags, uint32_t *arc_flags,
3101 const zbookmark_t *zb)
3104 arc_buf_t *buf = NULL;
3105 kmutex_t *hash_lock;
3107 uint64_t guid = spa_load_guid(spa);
3111 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
3113 if (hdr && hdr->b_datacnt > 0) {
3115 *arc_flags |= ARC_CACHED;
3117 if (HDR_IO_IN_PROGRESS(hdr)) {
3119 if (*arc_flags & ARC_WAIT) {
3120 cv_wait(&hdr->b_cv, hash_lock);
3121 mutex_exit(hash_lock);
3124 ASSERT(*arc_flags & ARC_NOWAIT);
3127 arc_callback_t *acb = NULL;
3129 acb = kmem_zalloc(sizeof (arc_callback_t),
3131 acb->acb_done = done;
3132 acb->acb_private = private;
3134 acb->acb_zio_dummy = zio_null(pio,
3135 spa, NULL, NULL, NULL, zio_flags);
3137 ASSERT(acb->acb_done != NULL);
3138 acb->acb_next = hdr->b_acb;
3140 add_reference(hdr, hash_lock, private);
3141 mutex_exit(hash_lock);
3144 mutex_exit(hash_lock);
3148 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3151 add_reference(hdr, hash_lock, private);
3153 * If this block is already in use, create a new
3154 * copy of the data so that we will be guaranteed
3155 * that arc_release() will always succeed.
3159 ASSERT(buf->b_data);
3160 if (HDR_BUF_AVAILABLE(hdr)) {
3161 ASSERT(buf->b_efunc == NULL);
3162 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3164 buf = arc_buf_clone(buf);
3167 } else if (*arc_flags & ARC_PREFETCH &&
3168 refcount_count(&hdr->b_refcnt) == 0) {
3169 hdr->b_flags |= ARC_PREFETCH;
3171 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
3172 arc_access(hdr, hash_lock);
3173 if (*arc_flags & ARC_L2CACHE)
3174 hdr->b_flags |= ARC_L2CACHE;
3175 if (*arc_flags & ARC_L2COMPRESS)
3176 hdr->b_flags |= ARC_L2COMPRESS;
3177 mutex_exit(hash_lock);
3178 ARCSTAT_BUMP(arcstat_hits);
3179 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3180 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3181 data, metadata, hits);
3184 done(NULL, buf, private);
3186 uint64_t size = BP_GET_LSIZE(bp);
3187 arc_callback_t *acb;
3190 boolean_t devw = B_FALSE;
3193 /* this block is not in the cache */
3194 arc_buf_hdr_t *exists;
3195 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
3196 buf = arc_buf_alloc(spa, size, private, type);
3198 hdr->b_dva = *BP_IDENTITY(bp);
3199 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
3200 hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
3201 exists = buf_hash_insert(hdr, &hash_lock);
3203 /* somebody beat us to the hash insert */
3204 mutex_exit(hash_lock);
3205 buf_discard_identity(hdr);
3206 (void) arc_buf_remove_ref(buf, private);
3207 goto top; /* restart the IO request */
3209 /* if this is a prefetch, we don't have a reference */
3210 if (*arc_flags & ARC_PREFETCH) {
3211 (void) remove_reference(hdr, hash_lock,
3213 hdr->b_flags |= ARC_PREFETCH;
3215 if (*arc_flags & ARC_L2CACHE)
3216 hdr->b_flags |= ARC_L2CACHE;
3217 if (*arc_flags & ARC_L2COMPRESS)
3218 hdr->b_flags |= ARC_L2COMPRESS;
3219 if (BP_GET_LEVEL(bp) > 0)
3220 hdr->b_flags |= ARC_INDIRECT;
3222 /* this block is in the ghost cache */
3223 ASSERT(GHOST_STATE(hdr->b_state));
3224 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3225 ASSERT0(refcount_count(&hdr->b_refcnt));
3226 ASSERT(hdr->b_buf == NULL);
3228 /* if this is a prefetch, we don't have a reference */
3229 if (*arc_flags & ARC_PREFETCH)
3230 hdr->b_flags |= ARC_PREFETCH;
3232 add_reference(hdr, hash_lock, private);
3233 if (*arc_flags & ARC_L2CACHE)
3234 hdr->b_flags |= ARC_L2CACHE;
3235 if (*arc_flags & ARC_L2COMPRESS)
3236 hdr->b_flags |= ARC_L2COMPRESS;
3237 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
3240 buf->b_efunc = NULL;
3241 buf->b_private = NULL;
3244 ASSERT(hdr->b_datacnt == 0);
3246 arc_get_data_buf(buf);
3247 arc_access(hdr, hash_lock);
3250 ASSERT(!GHOST_STATE(hdr->b_state));
3252 acb = kmem_zalloc(sizeof (arc_callback_t), KM_PUSHPAGE);
3253 acb->acb_done = done;
3254 acb->acb_private = private;
3256 ASSERT(hdr->b_acb == NULL);
3258 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3260 if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
3261 (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
3262 devw = hdr->b_l2hdr->b_dev->l2ad_writing;
3263 addr = hdr->b_l2hdr->b_daddr;
3265 * Lock out device removal.
3267 if (vdev_is_dead(vd) ||
3268 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
3272 mutex_exit(hash_lock);
3275 * At this point, we have a level 1 cache miss. Try again in
3276 * L2ARC if possible.
3278 ASSERT3U(hdr->b_size, ==, size);
3279 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
3280 uint64_t, size, zbookmark_t *, zb);
3281 ARCSTAT_BUMP(arcstat_misses);
3282 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3283 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3284 data, metadata, misses);
3286 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
3288 * Read from the L2ARC if the following are true:
3289 * 1. The L2ARC vdev was previously cached.
3290 * 2. This buffer still has L2ARC metadata.
3291 * 3. This buffer isn't currently writing to the L2ARC.
3292 * 4. The L2ARC entry wasn't evicted, which may
3293 * also have invalidated the vdev.
3294 * 5. This isn't prefetch and l2arc_noprefetch is set.
3296 if (hdr->b_l2hdr != NULL &&
3297 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
3298 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
3299 l2arc_read_callback_t *cb;
3301 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
3302 ARCSTAT_BUMP(arcstat_l2_hits);
3303 atomic_inc_32(&hdr->b_l2hdr->b_hits);
3305 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
3307 cb->l2rcb_buf = buf;
3308 cb->l2rcb_spa = spa;
3311 cb->l2rcb_flags = zio_flags;
3312 cb->l2rcb_compress = hdr->b_l2hdr->b_compress;
3314 ASSERT(addr >= VDEV_LABEL_START_SIZE &&
3315 addr + size < vd->vdev_psize -
3316 VDEV_LABEL_END_SIZE);
3319 * l2arc read. The SCL_L2ARC lock will be
3320 * released by l2arc_read_done().
3321 * Issue a null zio if the underlying buffer
3322 * was squashed to zero size by compression.
3324 if (hdr->b_l2hdr->b_compress ==
3325 ZIO_COMPRESS_EMPTY) {
3326 rzio = zio_null(pio, spa, vd,
3327 l2arc_read_done, cb,
3328 zio_flags | ZIO_FLAG_DONT_CACHE |
3330 ZIO_FLAG_DONT_PROPAGATE |
3331 ZIO_FLAG_DONT_RETRY);
3333 rzio = zio_read_phys(pio, vd, addr,
3334 hdr->b_l2hdr->b_asize,
3335 buf->b_data, ZIO_CHECKSUM_OFF,
3336 l2arc_read_done, cb, priority,
3337 zio_flags | ZIO_FLAG_DONT_CACHE |
3339 ZIO_FLAG_DONT_PROPAGATE |
3340 ZIO_FLAG_DONT_RETRY, B_FALSE);
3342 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
3344 ARCSTAT_INCR(arcstat_l2_read_bytes,
3345 hdr->b_l2hdr->b_asize);
3347 if (*arc_flags & ARC_NOWAIT) {
3352 ASSERT(*arc_flags & ARC_WAIT);
3353 if (zio_wait(rzio) == 0)
3356 /* l2arc read error; goto zio_read() */
3358 DTRACE_PROBE1(l2arc__miss,
3359 arc_buf_hdr_t *, hdr);
3360 ARCSTAT_BUMP(arcstat_l2_misses);
3361 if (HDR_L2_WRITING(hdr))
3362 ARCSTAT_BUMP(arcstat_l2_rw_clash);
3363 spa_config_exit(spa, SCL_L2ARC, vd);
3367 spa_config_exit(spa, SCL_L2ARC, vd);
3368 if (l2arc_ndev != 0) {
3369 DTRACE_PROBE1(l2arc__miss,
3370 arc_buf_hdr_t *, hdr);
3371 ARCSTAT_BUMP(arcstat_l2_misses);
3375 rzio = zio_read(pio, spa, bp, buf->b_data, size,
3376 arc_read_done, buf, priority, zio_flags, zb);
3378 if (*arc_flags & ARC_WAIT) {
3379 rc = zio_wait(rzio);
3383 ASSERT(*arc_flags & ARC_NOWAIT);
3388 spa_read_history_add(spa, zb, *arc_flags);
3393 arc_add_prune_callback(arc_prune_func_t *func, void *private)
3397 p = kmem_alloc(sizeof(*p), KM_SLEEP);
3399 p->p_private = private;
3400 list_link_init(&p->p_node);
3401 refcount_create(&p->p_refcnt);
3403 mutex_enter(&arc_prune_mtx);
3404 refcount_add(&p->p_refcnt, &arc_prune_list);
3405 list_insert_head(&arc_prune_list, p);
3406 mutex_exit(&arc_prune_mtx);
3412 arc_remove_prune_callback(arc_prune_t *p)
3414 mutex_enter(&arc_prune_mtx);
3415 list_remove(&arc_prune_list, p);
3416 if (refcount_remove(&p->p_refcnt, &arc_prune_list) == 0) {
3417 refcount_destroy(&p->p_refcnt);
3418 kmem_free(p, sizeof (*p));
3420 mutex_exit(&arc_prune_mtx);
3424 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
3426 ASSERT(buf->b_hdr != NULL);
3427 ASSERT(buf->b_hdr->b_state != arc_anon);
3428 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
3429 ASSERT(buf->b_efunc == NULL);
3430 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
3432 buf->b_efunc = func;
3433 buf->b_private = private;
3437 * Notify the arc that a block was freed, and thus will never be used again.
3440 arc_freed(spa_t *spa, const blkptr_t *bp)
3443 kmutex_t *hash_lock;
3444 uint64_t guid = spa_load_guid(spa);
3446 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
3450 if (HDR_BUF_AVAILABLE(hdr)) {
3451 arc_buf_t *buf = hdr->b_buf;
3452 add_reference(hdr, hash_lock, FTAG);
3453 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3454 mutex_exit(hash_lock);
3456 arc_release(buf, FTAG);
3457 (void) arc_buf_remove_ref(buf, FTAG);
3459 mutex_exit(hash_lock);
3465 * This is used by the DMU to let the ARC know that a buffer is
3466 * being evicted, so the ARC should clean up. If this arc buf
3467 * is not yet in the evicted state, it will be put there.
3470 arc_buf_evict(arc_buf_t *buf)
3473 kmutex_t *hash_lock;
3476 mutex_enter(&buf->b_evict_lock);
3480 * We are in arc_do_user_evicts().
3482 ASSERT(buf->b_data == NULL);
3483 mutex_exit(&buf->b_evict_lock);
3485 } else if (buf->b_data == NULL) {
3486 arc_buf_t copy = *buf; /* structure assignment */
3488 * We are on the eviction list; process this buffer now
3489 * but let arc_do_user_evicts() do the reaping.
3491 buf->b_efunc = NULL;
3492 mutex_exit(&buf->b_evict_lock);
3493 VERIFY(copy.b_efunc(©) == 0);
3496 hash_lock = HDR_LOCK(hdr);
3497 mutex_enter(hash_lock);
3499 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3501 ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
3502 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3505 * Pull this buffer off of the hdr
3508 while (*bufp != buf)
3509 bufp = &(*bufp)->b_next;
3510 *bufp = buf->b_next;
3512 ASSERT(buf->b_data != NULL);
3513 arc_buf_destroy(buf, FALSE, FALSE);
3515 if (hdr->b_datacnt == 0) {
3516 arc_state_t *old_state = hdr->b_state;
3517 arc_state_t *evicted_state;
3519 ASSERT(hdr->b_buf == NULL);
3520 ASSERT(refcount_is_zero(&hdr->b_refcnt));
3523 (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
3525 mutex_enter(&old_state->arcs_mtx);
3526 mutex_enter(&evicted_state->arcs_mtx);
3528 arc_change_state(evicted_state, hdr, hash_lock);
3529 ASSERT(HDR_IN_HASH_TABLE(hdr));
3530 hdr->b_flags |= ARC_IN_HASH_TABLE;
3531 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3533 mutex_exit(&evicted_state->arcs_mtx);
3534 mutex_exit(&old_state->arcs_mtx);
3536 mutex_exit(hash_lock);
3537 mutex_exit(&buf->b_evict_lock);
3539 VERIFY(buf->b_efunc(buf) == 0);
3540 buf->b_efunc = NULL;
3541 buf->b_private = NULL;
3544 kmem_cache_free(buf_cache, buf);
3549 * Release this buffer from the cache, making it an anonymous buffer. This
3550 * must be done after a read and prior to modifying the buffer contents.
3551 * If the buffer has more than one reference, we must make
3552 * a new hdr for the buffer.
3555 arc_release(arc_buf_t *buf, void *tag)
3558 kmutex_t *hash_lock = NULL;
3559 l2arc_buf_hdr_t *l2hdr;
3560 uint64_t buf_size = 0;
3563 * It would be nice to assert that if it's DMU metadata (level >
3564 * 0 || it's the dnode file), then it must be syncing context.
3565 * But we don't know that information at this level.
3568 mutex_enter(&buf->b_evict_lock);
3571 /* this buffer is not on any list */
3572 ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3574 if (hdr->b_state == arc_anon) {
3575 /* this buffer is already released */
3576 ASSERT(buf->b_efunc == NULL);
3578 hash_lock = HDR_LOCK(hdr);
3579 mutex_enter(hash_lock);
3581 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3584 l2hdr = hdr->b_l2hdr;
3586 mutex_enter(&l2arc_buflist_mtx);
3587 hdr->b_l2hdr = NULL;
3589 buf_size = hdr->b_size;
3592 * Do we have more than one buf?
3594 if (hdr->b_datacnt > 1) {
3595 arc_buf_hdr_t *nhdr;
3597 uint64_t blksz = hdr->b_size;
3598 uint64_t spa = hdr->b_spa;
3599 arc_buf_contents_t type = hdr->b_type;
3600 uint32_t flags = hdr->b_flags;
3602 ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3604 * Pull the data off of this hdr and attach it to
3605 * a new anonymous hdr.
3607 (void) remove_reference(hdr, hash_lock, tag);
3609 while (*bufp != buf)
3610 bufp = &(*bufp)->b_next;
3611 *bufp = buf->b_next;
3614 ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3615 atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3616 if (refcount_is_zero(&hdr->b_refcnt)) {
3617 uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3618 ASSERT3U(*size, >=, hdr->b_size);
3619 atomic_add_64(size, -hdr->b_size);
3623 * We're releasing a duplicate user data buffer, update
3624 * our statistics accordingly.
3626 if (hdr->b_type == ARC_BUFC_DATA) {
3627 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
3628 ARCSTAT_INCR(arcstat_duplicate_buffers_size,
3631 hdr->b_datacnt -= 1;
3632 arc_cksum_verify(buf);
3633 arc_buf_unwatch(buf);
3635 mutex_exit(hash_lock);
3637 nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3638 nhdr->b_size = blksz;
3640 nhdr->b_type = type;
3642 nhdr->b_state = arc_anon;
3643 nhdr->b_arc_access = 0;
3644 nhdr->b_mru_hits = 0;
3645 nhdr->b_mru_ghost_hits = 0;
3646 nhdr->b_mfu_hits = 0;
3647 nhdr->b_mfu_ghost_hits = 0;
3648 nhdr->b_l2_hits = 0;
3649 nhdr->b_flags = flags & ARC_L2_WRITING;
3650 nhdr->b_l2hdr = NULL;
3651 nhdr->b_datacnt = 1;
3652 nhdr->b_freeze_cksum = NULL;
3653 (void) refcount_add(&nhdr->b_refcnt, tag);
3655 mutex_exit(&buf->b_evict_lock);
3656 atomic_add_64(&arc_anon->arcs_size, blksz);
3658 mutex_exit(&buf->b_evict_lock);
3659 ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3660 ASSERT(!list_link_active(&hdr->b_arc_node));
3661 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3662 if (hdr->b_state != arc_anon)
3663 arc_change_state(arc_anon, hdr, hash_lock);
3664 hdr->b_arc_access = 0;
3665 hdr->b_mru_hits = 0;
3666 hdr->b_mru_ghost_hits = 0;
3667 hdr->b_mfu_hits = 0;
3668 hdr->b_mfu_ghost_hits = 0;
3671 mutex_exit(hash_lock);
3673 buf_discard_identity(hdr);
3676 buf->b_efunc = NULL;
3677 buf->b_private = NULL;
3680 ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize);
3681 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3682 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3683 arc_space_return(L2HDR_SIZE, ARC_SPACE_L2HDRS);
3684 ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3685 mutex_exit(&l2arc_buflist_mtx);
3690 arc_released(arc_buf_t *buf)
3694 mutex_enter(&buf->b_evict_lock);
3695 released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3696 mutex_exit(&buf->b_evict_lock);
3701 arc_has_callback(arc_buf_t *buf)
3705 mutex_enter(&buf->b_evict_lock);
3706 callback = (buf->b_efunc != NULL);
3707 mutex_exit(&buf->b_evict_lock);
3713 arc_referenced(arc_buf_t *buf)
3717 mutex_enter(&buf->b_evict_lock);
3718 referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3719 mutex_exit(&buf->b_evict_lock);
3720 return (referenced);
3725 arc_write_ready(zio_t *zio)
3727 arc_write_callback_t *callback = zio->io_private;
3728 arc_buf_t *buf = callback->awcb_buf;
3729 arc_buf_hdr_t *hdr = buf->b_hdr;
3731 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3732 callback->awcb_ready(zio, buf, callback->awcb_private);
3735 * If the IO is already in progress, then this is a re-write
3736 * attempt, so we need to thaw and re-compute the cksum.
3737 * It is the responsibility of the callback to handle the
3738 * accounting for any re-write attempt.
3740 if (HDR_IO_IN_PROGRESS(hdr)) {
3741 mutex_enter(&hdr->b_freeze_lock);
3742 if (hdr->b_freeze_cksum != NULL) {
3743 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3744 hdr->b_freeze_cksum = NULL;
3746 mutex_exit(&hdr->b_freeze_lock);
3748 arc_cksum_compute(buf, B_FALSE);
3749 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3753 * The SPA calls this callback for each physical write that happens on behalf
3754 * of a logical write. See the comment in dbuf_write_physdone() for details.
3757 arc_write_physdone(zio_t *zio)
3759 arc_write_callback_t *cb = zio->io_private;
3760 if (cb->awcb_physdone != NULL)
3761 cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private);
3765 arc_write_done(zio_t *zio)
3767 arc_write_callback_t *callback = zio->io_private;
3768 arc_buf_t *buf = callback->awcb_buf;
3769 arc_buf_hdr_t *hdr = buf->b_hdr;
3771 ASSERT(hdr->b_acb == NULL);
3773 if (zio->io_error == 0) {
3774 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3775 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3776 hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3778 ASSERT(BUF_EMPTY(hdr));
3782 * If the block to be written was all-zero, we may have
3783 * compressed it away. In this case no write was performed
3784 * so there will be no dva/birth/checksum. The buffer must
3785 * therefore remain anonymous (and uncached).
3787 if (!BUF_EMPTY(hdr)) {
3788 arc_buf_hdr_t *exists;
3789 kmutex_t *hash_lock;
3791 ASSERT(zio->io_error == 0);
3793 arc_cksum_verify(buf);
3795 exists = buf_hash_insert(hdr, &hash_lock);
3798 * This can only happen if we overwrite for
3799 * sync-to-convergence, because we remove
3800 * buffers from the hash table when we arc_free().
3802 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3803 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3804 panic("bad overwrite, hdr=%p exists=%p",
3805 (void *)hdr, (void *)exists);
3806 ASSERT(refcount_is_zero(&exists->b_refcnt));
3807 arc_change_state(arc_anon, exists, hash_lock);
3808 mutex_exit(hash_lock);
3809 arc_hdr_destroy(exists);
3810 exists = buf_hash_insert(hdr, &hash_lock);
3811 ASSERT3P(exists, ==, NULL);
3812 } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
3814 ASSERT(zio->io_prop.zp_nopwrite);
3815 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3816 panic("bad nopwrite, hdr=%p exists=%p",
3817 (void *)hdr, (void *)exists);
3820 ASSERT(hdr->b_datacnt == 1);
3821 ASSERT(hdr->b_state == arc_anon);
3822 ASSERT(BP_GET_DEDUP(zio->io_bp));
3823 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3826 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3827 /* if it's not anon, we are doing a scrub */
3828 if (!exists && hdr->b_state == arc_anon)
3829 arc_access(hdr, hash_lock);
3830 mutex_exit(hash_lock);
3832 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3835 ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3836 callback->awcb_done(zio, buf, callback->awcb_private);
3838 kmem_free(callback, sizeof (arc_write_callback_t));
3842 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3843 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress,
3844 const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone,
3845 arc_done_func_t *done, void *private, zio_priority_t priority,
3846 int zio_flags, const zbookmark_t *zb)
3848 arc_buf_hdr_t *hdr = buf->b_hdr;
3849 arc_write_callback_t *callback;
3852 ASSERT(ready != NULL);
3853 ASSERT(done != NULL);
3854 ASSERT(!HDR_IO_ERROR(hdr));
3855 ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3856 ASSERT(hdr->b_acb == NULL);
3858 hdr->b_flags |= ARC_L2CACHE;
3860 hdr->b_flags |= ARC_L2COMPRESS;
3861 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_PUSHPAGE);
3862 callback->awcb_ready = ready;
3863 callback->awcb_physdone = physdone;
3864 callback->awcb_done = done;
3865 callback->awcb_private = private;
3866 callback->awcb_buf = buf;
3868 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3869 arc_write_ready, arc_write_physdone, arc_write_done, callback,
3870 priority, zio_flags, zb);
3876 arc_memory_throttle(uint64_t reserve, uint64_t txg)
3879 if (zfs_arc_memory_throttle_disable)
3882 if (freemem <= physmem * arc_lotsfree_percent / 100) {
3883 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3884 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim);
3885 return (SET_ERROR(EAGAIN));
3892 arc_tempreserve_clear(uint64_t reserve)
3894 atomic_add_64(&arc_tempreserve, -reserve);
3895 ASSERT((int64_t)arc_tempreserve >= 0);
3899 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3904 if (reserve > arc_c/4 && !arc_no_grow)
3905 arc_c = MIN(arc_c_max, reserve * 4);
3906 if (reserve > arc_c) {
3907 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve);
3908 return (SET_ERROR(ENOMEM));
3912 * Don't count loaned bufs as in flight dirty data to prevent long
3913 * network delays from blocking transactions that are ready to be
3914 * assigned to a txg.
3916 anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3919 * Writes will, almost always, require additional memory allocations
3920 * in order to compress/encrypt/etc the data. We therefore need to
3921 * make sure that there is sufficient available memory for this.
3923 error = arc_memory_throttle(reserve, txg);
3928 * Throttle writes when the amount of dirty data in the cache
3929 * gets too large. We try to keep the cache less than half full
3930 * of dirty blocks so that our sync times don't grow too large.
3931 * Note: if two requests come in concurrently, we might let them
3932 * both succeed, when one of them should fail. Not a huge deal.
3935 if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3936 anon_size > arc_c / 4) {
3937 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3938 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3939 arc_tempreserve>>10,
3940 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3941 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3942 reserve>>10, arc_c>>10);
3943 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle);
3944 return (SET_ERROR(ERESTART));
3946 atomic_add_64(&arc_tempreserve, reserve);
3951 arc_kstat_update_state(arc_state_t *state, kstat_named_t *size,
3952 kstat_named_t *evict_data, kstat_named_t *evict_metadata)
3954 size->value.ui64 = state->arcs_size;
3955 evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA];
3956 evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA];
3960 arc_kstat_update(kstat_t *ksp, int rw)
3962 arc_stats_t *as = ksp->ks_data;
3964 if (rw == KSTAT_WRITE) {
3965 return (SET_ERROR(EACCES));
3967 arc_kstat_update_state(arc_anon,
3968 &as->arcstat_anon_size,
3969 &as->arcstat_anon_evict_data,
3970 &as->arcstat_anon_evict_metadata);
3971 arc_kstat_update_state(arc_mru,
3972 &as->arcstat_mru_size,
3973 &as->arcstat_mru_evict_data,
3974 &as->arcstat_mru_evict_metadata);
3975 arc_kstat_update_state(arc_mru_ghost,
3976 &as->arcstat_mru_ghost_size,
3977 &as->arcstat_mru_ghost_evict_data,
3978 &as->arcstat_mru_ghost_evict_metadata);
3979 arc_kstat_update_state(arc_mfu,
3980 &as->arcstat_mfu_size,
3981 &as->arcstat_mfu_evict_data,
3982 &as->arcstat_mfu_evict_metadata);
3983 arc_kstat_update_state(arc_mfu_ghost,
3984 &as->arcstat_mfu_ghost_size,
3985 &as->arcstat_mfu_ghost_evict_data,
3986 &as->arcstat_mfu_ghost_evict_metadata);
3995 mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3996 cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3998 /* Convert seconds to clock ticks */
3999 zfs_arc_min_prefetch_lifespan = 1 * hz;
4001 /* Start out with 1/8 of all memory */
4002 arc_c = physmem * PAGESIZE / 8;
4006 * On architectures where the physical memory can be larger
4007 * than the addressable space (intel in 32-bit mode), we may
4008 * need to limit the cache to 1/8 of VM size.
4010 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
4012 * Register a shrinker to support synchronous (direct) memory
4013 * reclaim from the arc. This is done to prevent kswapd from
4014 * swapping out pages when it is preferable to shrink the arc.
4016 spl_register_shrinker(&arc_shrinker);
4019 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
4020 arc_c_min = MAX(arc_c / 4, 64<<20);
4021 /* set max to 1/2 of all memory */
4022 arc_c_max = arc_c * 4;
4025 * Allow the tunables to override our calculations if they are
4026 * reasonable (ie. over 64MB)
4028 if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
4029 arc_c_max = zfs_arc_max;
4030 if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
4031 arc_c_min = zfs_arc_min;
4034 arc_p = (arc_c >> 1);
4036 /* limit meta-data to 1/4 of the arc capacity */
4037 arc_meta_limit = arc_c_max / 4;
4040 /* Allow the tunable to override if it is reasonable */
4041 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
4042 arc_meta_limit = zfs_arc_meta_limit;
4044 if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
4045 arc_c_min = arc_meta_limit / 2;
4047 /* if kmem_flags are set, lets try to use less memory */
4048 if (kmem_debugging())
4050 if (arc_c < arc_c_min)
4053 arc_anon = &ARC_anon;
4055 arc_mru_ghost = &ARC_mru_ghost;
4057 arc_mfu_ghost = &ARC_mfu_ghost;
4058 arc_l2c_only = &ARC_l2c_only;
4061 mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
4062 mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
4063 mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
4064 mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
4065 mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
4066 mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
4068 list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
4069 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4070 list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
4071 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4072 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
4073 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4074 list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
4075 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4076 list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
4077 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4078 list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
4079 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4080 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
4081 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4082 list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
4083 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4084 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
4085 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4086 list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
4087 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
4089 arc_anon->arcs_state = ARC_STATE_ANON;
4090 arc_mru->arcs_state = ARC_STATE_MRU;
4091 arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST;
4092 arc_mfu->arcs_state = ARC_STATE_MFU;
4093 arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST;
4094 arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY;
4098 arc_thread_exit = 0;
4099 list_create(&arc_prune_list, sizeof (arc_prune_t),
4100 offsetof(arc_prune_t, p_node));
4101 arc_eviction_list = NULL;
4102 mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL);
4103 mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
4104 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
4106 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
4107 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
4109 if (arc_ksp != NULL) {
4110 arc_ksp->ks_data = &arc_stats;
4111 arc_ksp->ks_update = arc_kstat_update;
4112 kstat_install(arc_ksp);
4115 (void) thread_create(NULL, 0, arc_adapt_thread, NULL, 0, &p0,
4116 TS_RUN, minclsyspri);
4122 * Calculate maximum amount of dirty data per pool.
4124 * If it has been set by a module parameter, take that.
4125 * Otherwise, use a percentage of physical memory defined by
4126 * zfs_dirty_data_max_percent (default 10%) with a cap at
4127 * zfs_dirty_data_max_max (default 25% of physical memory).
4129 if (zfs_dirty_data_max_max == 0)
4130 zfs_dirty_data_max_max = physmem * PAGESIZE *
4131 zfs_dirty_data_max_max_percent / 100;
4133 if (zfs_dirty_data_max == 0) {
4134 zfs_dirty_data_max = physmem * PAGESIZE *
4135 zfs_dirty_data_max_percent / 100;
4136 zfs_dirty_data_max = MIN(zfs_dirty_data_max,
4137 zfs_dirty_data_max_max);
4146 mutex_enter(&arc_reclaim_thr_lock);
4148 spl_unregister_shrinker(&arc_shrinker);
4149 #endif /* _KERNEL */
4151 arc_thread_exit = 1;
4152 while (arc_thread_exit != 0)
4153 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
4154 mutex_exit(&arc_reclaim_thr_lock);
4160 if (arc_ksp != NULL) {
4161 kstat_delete(arc_ksp);
4165 mutex_enter(&arc_prune_mtx);
4166 while ((p = list_head(&arc_prune_list)) != NULL) {
4167 list_remove(&arc_prune_list, p);
4168 refcount_remove(&p->p_refcnt, &arc_prune_list);
4169 refcount_destroy(&p->p_refcnt);
4170 kmem_free(p, sizeof (*p));
4172 mutex_exit(&arc_prune_mtx);
4174 list_destroy(&arc_prune_list);
4175 mutex_destroy(&arc_prune_mtx);
4176 mutex_destroy(&arc_eviction_mtx);
4177 mutex_destroy(&arc_reclaim_thr_lock);
4178 cv_destroy(&arc_reclaim_thr_cv);
4180 list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
4181 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
4182 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
4183 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
4184 list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
4185 list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
4186 list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
4187 list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
4189 mutex_destroy(&arc_anon->arcs_mtx);
4190 mutex_destroy(&arc_mru->arcs_mtx);
4191 mutex_destroy(&arc_mru_ghost->arcs_mtx);
4192 mutex_destroy(&arc_mfu->arcs_mtx);
4193 mutex_destroy(&arc_mfu_ghost->arcs_mtx);
4194 mutex_destroy(&arc_l2c_only->arcs_mtx);
4198 ASSERT(arc_loaned_bytes == 0);
4204 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
4205 * It uses dedicated storage devices to hold cached data, which are populated
4206 * using large infrequent writes. The main role of this cache is to boost
4207 * the performance of random read workloads. The intended L2ARC devices
4208 * include short-stroked disks, solid state disks, and other media with
4209 * substantially faster read latency than disk.
4211 * +-----------------------+
4213 * +-----------------------+
4216 * l2arc_feed_thread() arc_read()
4220 * +---------------+ |
4222 * +---------------+ |
4227 * +-------+ +-------+
4229 * | cache | | cache |
4230 * +-------+ +-------+
4231 * +=========+ .-----.
4232 * : L2ARC : |-_____-|
4233 * : devices : | Disks |
4234 * +=========+ `-_____-'
4236 * Read requests are satisfied from the following sources, in order:
4239 * 2) vdev cache of L2ARC devices
4241 * 4) vdev cache of disks
4244 * Some L2ARC device types exhibit extremely slow write performance.
4245 * To accommodate for this there are some significant differences between
4246 * the L2ARC and traditional cache design:
4248 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
4249 * the ARC behave as usual, freeing buffers and placing headers on ghost
4250 * lists. The ARC does not send buffers to the L2ARC during eviction as
4251 * this would add inflated write latencies for all ARC memory pressure.
4253 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
4254 * It does this by periodically scanning buffers from the eviction-end of
4255 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
4256 * not already there. It scans until a headroom of buffers is satisfied,
4257 * which itself is a buffer for ARC eviction. If a compressible buffer is
4258 * found during scanning and selected for writing to an L2ARC device, we
4259 * temporarily boost scanning headroom during the next scan cycle to make
4260 * sure we adapt to compression effects (which might significantly reduce
4261 * the data volume we write to L2ARC). The thread that does this is
4262 * l2arc_feed_thread(), illustrated below; example sizes are included to
4263 * provide a better sense of ratio than this diagram:
4266 * +---------------------+----------+
4267 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
4268 * +---------------------+----------+ | o L2ARC eligible
4269 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
4270 * +---------------------+----------+ |
4271 * 15.9 Gbytes ^ 32 Mbytes |
4273 * l2arc_feed_thread()
4275 * l2arc write hand <--[oooo]--'
4279 * +==============================+
4280 * L2ARC dev |####|#|###|###| |####| ... |
4281 * +==============================+
4284 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
4285 * evicted, then the L2ARC has cached a buffer much sooner than it probably
4286 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
4287 * safe to say that this is an uncommon case, since buffers at the end of
4288 * the ARC lists have moved there due to inactivity.
4290 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
4291 * then the L2ARC simply misses copying some buffers. This serves as a
4292 * pressure valve to prevent heavy read workloads from both stalling the ARC
4293 * with waits and clogging the L2ARC with writes. This also helps prevent
4294 * the potential for the L2ARC to churn if it attempts to cache content too
4295 * quickly, such as during backups of the entire pool.
4297 * 5. After system boot and before the ARC has filled main memory, there are
4298 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
4299 * lists can remain mostly static. Instead of searching from tail of these
4300 * lists as pictured, the l2arc_feed_thread() will search from the list heads
4301 * for eligible buffers, greatly increasing its chance of finding them.
4303 * The L2ARC device write speed is also boosted during this time so that
4304 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
4305 * there are no L2ARC reads, and no fear of degrading read performance
4306 * through increased writes.
4308 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
4309 * the vdev queue can aggregate them into larger and fewer writes. Each
4310 * device is written to in a rotor fashion, sweeping writes through
4311 * available space then repeating.
4313 * 7. The L2ARC does not store dirty content. It never needs to flush
4314 * write buffers back to disk based storage.
4316 * 8. If an ARC buffer is written (and dirtied) which also exists in the
4317 * L2ARC, the now stale L2ARC buffer is immediately dropped.
4319 * The performance of the L2ARC can be tweaked by a number of tunables, which
4320 * may be necessary for different workloads:
4322 * l2arc_write_max max write bytes per interval
4323 * l2arc_write_boost extra write bytes during device warmup
4324 * l2arc_noprefetch skip caching prefetched buffers
4325 * l2arc_nocompress skip compressing buffers
4326 * l2arc_headroom number of max device writes to precache
4327 * l2arc_headroom_boost when we find compressed buffers during ARC
4328 * scanning, we multiply headroom by this
4329 * percentage factor for the next scan cycle,
4330 * since more compressed buffers are likely to
4332 * l2arc_feed_secs seconds between L2ARC writing
4334 * Tunables may be removed or added as future performance improvements are
4335 * integrated, and also may become zpool properties.
4337 * There are three key functions that control how the L2ARC warms up:
4339 * l2arc_write_eligible() check if a buffer is eligible to cache
4340 * l2arc_write_size() calculate how much to write
4341 * l2arc_write_interval() calculate sleep delay between writes
4343 * These three functions determine what to write, how much, and how quickly
4348 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
4351 * A buffer is *not* eligible for the L2ARC if it:
4352 * 1. belongs to a different spa.
4353 * 2. is already cached on the L2ARC.
4354 * 3. has an I/O in progress (it may be an incomplete read).
4355 * 4. is flagged not eligible (zfs property).
4357 if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
4358 HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
4365 l2arc_write_size(void)
4370 * Make sure our globals have meaningful values in case the user
4373 size = l2arc_write_max;
4375 cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must "
4376 "be greater than zero, resetting it to the default (%d)",
4378 size = l2arc_write_max = L2ARC_WRITE_SIZE;
4381 if (arc_warm == B_FALSE)
4382 size += l2arc_write_boost;
4389 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
4391 clock_t interval, next, now;
4394 * If the ARC lists are busy, increase our write rate; if the
4395 * lists are stale, idle back. This is achieved by checking
4396 * how much we previously wrote - if it was more than half of
4397 * what we wanted, schedule the next write much sooner.
4399 if (l2arc_feed_again && wrote > (wanted / 2))
4400 interval = (hz * l2arc_feed_min_ms) / 1000;
4402 interval = hz * l2arc_feed_secs;
4404 now = ddi_get_lbolt();
4405 next = MAX(now, MIN(now + interval, began + interval));
4411 l2arc_hdr_stat_add(void)
4413 ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE);
4414 ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
4418 l2arc_hdr_stat_remove(void)
4420 ARCSTAT_INCR(arcstat_l2_hdr_size, -HDR_SIZE);
4421 ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
4425 * Cycle through L2ARC devices. This is how L2ARC load balances.
4426 * If a device is returned, this also returns holding the spa config lock.
4428 static l2arc_dev_t *
4429 l2arc_dev_get_next(void)
4431 l2arc_dev_t *first, *next = NULL;
4434 * Lock out the removal of spas (spa_namespace_lock), then removal
4435 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
4436 * both locks will be dropped and a spa config lock held instead.
4438 mutex_enter(&spa_namespace_lock);
4439 mutex_enter(&l2arc_dev_mtx);
4441 /* if there are no vdevs, there is nothing to do */
4442 if (l2arc_ndev == 0)
4446 next = l2arc_dev_last;
4448 /* loop around the list looking for a non-faulted vdev */
4450 next = list_head(l2arc_dev_list);
4452 next = list_next(l2arc_dev_list, next);
4454 next = list_head(l2arc_dev_list);
4457 /* if we have come back to the start, bail out */
4460 else if (next == first)
4463 } while (vdev_is_dead(next->l2ad_vdev));
4465 /* if we were unable to find any usable vdevs, return NULL */
4466 if (vdev_is_dead(next->l2ad_vdev))
4469 l2arc_dev_last = next;
4472 mutex_exit(&l2arc_dev_mtx);
4475 * Grab the config lock to prevent the 'next' device from being
4476 * removed while we are writing to it.
4479 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
4480 mutex_exit(&spa_namespace_lock);
4486 * Free buffers that were tagged for destruction.
4489 l2arc_do_free_on_write(void)
4492 l2arc_data_free_t *df, *df_prev;
4494 mutex_enter(&l2arc_free_on_write_mtx);
4495 buflist = l2arc_free_on_write;
4497 for (df = list_tail(buflist); df; df = df_prev) {
4498 df_prev = list_prev(buflist, df);
4499 ASSERT(df->l2df_data != NULL);
4500 ASSERT(df->l2df_func != NULL);
4501 df->l2df_func(df->l2df_data, df->l2df_size);
4502 list_remove(buflist, df);
4503 kmem_free(df, sizeof (l2arc_data_free_t));
4506 mutex_exit(&l2arc_free_on_write_mtx);
4510 * A write to a cache device has completed. Update all headers to allow
4511 * reads from these buffers to begin.
4514 l2arc_write_done(zio_t *zio)
4516 l2arc_write_callback_t *cb;
4519 arc_buf_hdr_t *head, *ab, *ab_prev;
4520 l2arc_buf_hdr_t *abl2;
4521 kmutex_t *hash_lock;
4523 cb = zio->io_private;
4525 dev = cb->l2wcb_dev;
4526 ASSERT(dev != NULL);
4527 head = cb->l2wcb_head;
4528 ASSERT(head != NULL);
4529 buflist = dev->l2ad_buflist;
4530 ASSERT(buflist != NULL);
4531 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
4532 l2arc_write_callback_t *, cb);
4534 if (zio->io_error != 0)
4535 ARCSTAT_BUMP(arcstat_l2_writes_error);
4537 mutex_enter(&l2arc_buflist_mtx);
4540 * All writes completed, or an error was hit.
4542 for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
4543 ab_prev = list_prev(buflist, ab);
4547 * Release the temporary compressed buffer as soon as possible.
4549 if (abl2->b_compress != ZIO_COMPRESS_OFF)
4550 l2arc_release_cdata_buf(ab);
4552 hash_lock = HDR_LOCK(ab);
4553 if (!mutex_tryenter(hash_lock)) {
4555 * This buffer misses out. It may be in a stage
4556 * of eviction. Its ARC_L2_WRITING flag will be
4557 * left set, denying reads to this buffer.
4559 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
4563 if (zio->io_error != 0) {
4565 * Error - drop L2ARC entry.
4567 list_remove(buflist, ab);
4568 ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize);
4570 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4571 arc_space_return(L2HDR_SIZE, ARC_SPACE_L2HDRS);
4572 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4576 * Allow ARC to begin reads to this L2ARC entry.
4578 ab->b_flags &= ~ARC_L2_WRITING;
4580 mutex_exit(hash_lock);
4583 atomic_inc_64(&l2arc_writes_done);
4584 list_remove(buflist, head);
4585 kmem_cache_free(hdr_cache, head);
4586 mutex_exit(&l2arc_buflist_mtx);
4588 l2arc_do_free_on_write();
4590 kmem_free(cb, sizeof (l2arc_write_callback_t));
4594 * A read to a cache device completed. Validate buffer contents before
4595 * handing over to the regular ARC routines.
4598 l2arc_read_done(zio_t *zio)
4600 l2arc_read_callback_t *cb;
4603 kmutex_t *hash_lock;
4606 ASSERT(zio->io_vd != NULL);
4607 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
4609 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
4611 cb = zio->io_private;
4613 buf = cb->l2rcb_buf;
4614 ASSERT(buf != NULL);
4616 hash_lock = HDR_LOCK(buf->b_hdr);
4617 mutex_enter(hash_lock);
4619 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4622 * If the buffer was compressed, decompress it first.
4624 if (cb->l2rcb_compress != ZIO_COMPRESS_OFF)
4625 l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress);
4626 ASSERT(zio->io_data != NULL);
4629 * Check this survived the L2ARC journey.
4631 equal = arc_cksum_equal(buf);
4632 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4633 mutex_exit(hash_lock);
4634 zio->io_private = buf;
4635 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
4636 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
4639 mutex_exit(hash_lock);
4641 * Buffer didn't survive caching. Increment stats and
4642 * reissue to the original storage device.
4644 if (zio->io_error != 0) {
4645 ARCSTAT_BUMP(arcstat_l2_io_error);
4647 zio->io_error = SET_ERROR(EIO);
4650 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4653 * If there's no waiter, issue an async i/o to the primary
4654 * storage now. If there *is* a waiter, the caller must
4655 * issue the i/o in a context where it's OK to block.
4657 if (zio->io_waiter == NULL) {
4658 zio_t *pio = zio_unique_parent(zio);
4660 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4662 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4663 buf->b_data, zio->io_size, arc_read_done, buf,
4664 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4668 kmem_free(cb, sizeof (l2arc_read_callback_t));
4672 * This is the list priority from which the L2ARC will search for pages to
4673 * cache. This is used within loops (0..3) to cycle through lists in the
4674 * desired order. This order can have a significant effect on cache
4677 * Currently the metadata lists are hit first, MFU then MRU, followed by
4678 * the data lists. This function returns a locked list, and also returns
4682 l2arc_list_locked(int list_num, kmutex_t **lock)
4684 list_t *list = NULL;
4686 ASSERT(list_num >= 0 && list_num <= 3);
4690 list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
4691 *lock = &arc_mfu->arcs_mtx;
4694 list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
4695 *lock = &arc_mru->arcs_mtx;
4698 list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
4699 *lock = &arc_mfu->arcs_mtx;
4702 list = &arc_mru->arcs_list[ARC_BUFC_DATA];
4703 *lock = &arc_mru->arcs_mtx;
4707 ASSERT(!(MUTEX_HELD(*lock)));
4713 * Evict buffers from the device write hand to the distance specified in
4714 * bytes. This distance may span populated buffers, it may span nothing.
4715 * This is clearing a region on the L2ARC device ready for writing.
4716 * If the 'all' boolean is set, every buffer is evicted.
4719 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4722 l2arc_buf_hdr_t *abl2;
4723 arc_buf_hdr_t *ab, *ab_prev;
4724 kmutex_t *hash_lock;
4727 buflist = dev->l2ad_buflist;
4729 if (buflist == NULL)
4732 if (!all && dev->l2ad_first) {
4734 * This is the first sweep through the device. There is
4740 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4742 * When nearing the end of the device, evict to the end
4743 * before the device write hand jumps to the start.
4745 taddr = dev->l2ad_end;
4747 taddr = dev->l2ad_hand + distance;
4749 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4750 uint64_t, taddr, boolean_t, all);
4753 mutex_enter(&l2arc_buflist_mtx);
4754 for (ab = list_tail(buflist); ab; ab = ab_prev) {
4755 ab_prev = list_prev(buflist, ab);
4757 hash_lock = HDR_LOCK(ab);
4758 if (!mutex_tryenter(hash_lock)) {
4760 * Missed the hash lock. Retry.
4762 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4763 mutex_exit(&l2arc_buflist_mtx);
4764 mutex_enter(hash_lock);
4765 mutex_exit(hash_lock);
4769 if (HDR_L2_WRITE_HEAD(ab)) {
4771 * We hit a write head node. Leave it for
4772 * l2arc_write_done().
4774 list_remove(buflist, ab);
4775 mutex_exit(hash_lock);
4779 if (!all && ab->b_l2hdr != NULL &&
4780 (ab->b_l2hdr->b_daddr > taddr ||
4781 ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4783 * We've evicted to the target address,
4784 * or the end of the device.
4786 mutex_exit(hash_lock);
4790 if (HDR_FREE_IN_PROGRESS(ab)) {
4792 * Already on the path to destruction.
4794 mutex_exit(hash_lock);
4798 if (ab->b_state == arc_l2c_only) {
4799 ASSERT(!HDR_L2_READING(ab));
4801 * This doesn't exist in the ARC. Destroy.
4802 * arc_hdr_destroy() will call list_remove()
4803 * and decrement arcstat_l2_size.
4805 arc_change_state(arc_anon, ab, hash_lock);
4806 arc_hdr_destroy(ab);
4809 * Invalidate issued or about to be issued
4810 * reads, since we may be about to write
4811 * over this location.
4813 if (HDR_L2_READING(ab)) {
4814 ARCSTAT_BUMP(arcstat_l2_evict_reading);
4815 ab->b_flags |= ARC_L2_EVICTED;
4819 * Tell ARC this no longer exists in L2ARC.
4821 if (ab->b_l2hdr != NULL) {
4823 ARCSTAT_INCR(arcstat_l2_asize, -abl2->b_asize);
4825 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4826 arc_space_return(L2HDR_SIZE, ARC_SPACE_L2HDRS);
4827 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4829 list_remove(buflist, ab);
4832 * This may have been leftover after a
4835 ab->b_flags &= ~ARC_L2_WRITING;
4837 mutex_exit(hash_lock);
4839 mutex_exit(&l2arc_buflist_mtx);
4841 vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
4842 dev->l2ad_evict = taddr;
4846 * Find and write ARC buffers to the L2ARC device.
4848 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4849 * for reading until they have completed writing.
4850 * The headroom_boost is an in-out parameter used to maintain headroom boost
4851 * state between calls to this function.
4853 * Returns the number of bytes actually written (which may be smaller than
4854 * the delta by which the device hand has changed due to alignment).
4857 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz,
4858 boolean_t *headroom_boost)
4860 arc_buf_hdr_t *ab, *ab_prev, *head;
4862 uint64_t write_asize, write_psize, write_sz, headroom,
4865 kmutex_t *list_lock = NULL;
4867 l2arc_write_callback_t *cb;
4869 uint64_t guid = spa_load_guid(spa);
4871 const boolean_t do_headroom_boost = *headroom_boost;
4873 ASSERT(dev->l2ad_vdev != NULL);
4875 /* Lower the flag now, we might want to raise it again later. */
4876 *headroom_boost = B_FALSE;
4879 write_sz = write_asize = write_psize = 0;
4881 head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4882 head->b_flags |= ARC_L2_WRITE_HEAD;
4885 * We will want to try to compress buffers that are at least 2x the
4886 * device sector size.
4888 buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift;
4891 * Copy buffers for L2ARC writing.
4893 mutex_enter(&l2arc_buflist_mtx);
4894 for (try = 0; try <= 3; try++) {
4895 uint64_t passed_sz = 0;
4897 list = l2arc_list_locked(try, &list_lock);
4900 * L2ARC fast warmup.
4902 * Until the ARC is warm and starts to evict, read from the
4903 * head of the ARC lists rather than the tail.
4905 if (arc_warm == B_FALSE)
4906 ab = list_head(list);
4908 ab = list_tail(list);
4910 headroom = target_sz * l2arc_headroom;
4911 if (do_headroom_boost)
4912 headroom = (headroom * l2arc_headroom_boost) / 100;
4914 for (; ab; ab = ab_prev) {
4915 l2arc_buf_hdr_t *l2hdr;
4916 kmutex_t *hash_lock;
4919 if (arc_warm == B_FALSE)
4920 ab_prev = list_next(list, ab);
4922 ab_prev = list_prev(list, ab);
4924 hash_lock = HDR_LOCK(ab);
4925 if (!mutex_tryenter(hash_lock)) {
4927 * Skip this buffer rather than waiting.
4932 passed_sz += ab->b_size;
4933 if (passed_sz > headroom) {
4937 mutex_exit(hash_lock);
4941 if (!l2arc_write_eligible(guid, ab)) {
4942 mutex_exit(hash_lock);
4946 if ((write_sz + ab->b_size) > target_sz) {
4948 mutex_exit(hash_lock);
4954 * Insert a dummy header on the buflist so
4955 * l2arc_write_done() can find where the
4956 * write buffers begin without searching.
4958 list_insert_head(dev->l2ad_buflist, head);
4960 cb = kmem_alloc(sizeof (l2arc_write_callback_t),
4962 cb->l2wcb_dev = dev;
4963 cb->l2wcb_head = head;
4964 pio = zio_root(spa, l2arc_write_done, cb,
4969 * Create and add a new L2ARC header.
4971 l2hdr = kmem_zalloc(sizeof (l2arc_buf_hdr_t),
4974 arc_space_consume(L2HDR_SIZE, ARC_SPACE_L2HDRS);
4976 ab->b_flags |= ARC_L2_WRITING;
4979 * Temporarily stash the data buffer in b_tmp_cdata.
4980 * The subsequent write step will pick it up from
4981 * there. This is because can't access ab->b_buf
4982 * without holding the hash_lock, which we in turn
4983 * can't access without holding the ARC list locks
4984 * (which we want to avoid during compression/writing)
4986 l2hdr->b_compress = ZIO_COMPRESS_OFF;
4987 l2hdr->b_asize = ab->b_size;
4988 l2hdr->b_tmp_cdata = ab->b_buf->b_data;
4991 buf_sz = ab->b_size;
4992 ab->b_l2hdr = l2hdr;
4994 list_insert_head(dev->l2ad_buflist, ab);
4997 * Compute and store the buffer cksum before
4998 * writing. On debug the cksum is verified first.
5000 arc_cksum_verify(ab->b_buf);
5001 arc_cksum_compute(ab->b_buf, B_TRUE);
5003 mutex_exit(hash_lock);
5008 mutex_exit(list_lock);
5014 /* No buffers selected for writing? */
5017 mutex_exit(&l2arc_buflist_mtx);
5018 kmem_cache_free(hdr_cache, head);
5023 * Now start writing the buffers. We're starting at the write head
5024 * and work backwards, retracing the course of the buffer selector
5027 for (ab = list_prev(dev->l2ad_buflist, head); ab;
5028 ab = list_prev(dev->l2ad_buflist, ab)) {
5029 l2arc_buf_hdr_t *l2hdr;
5033 * We shouldn't need to lock the buffer here, since we flagged
5034 * it as ARC_L2_WRITING in the previous step, but we must take
5035 * care to only access its L2 cache parameters. In particular,
5036 * ab->b_buf may be invalid by now due to ARC eviction.
5038 l2hdr = ab->b_l2hdr;
5039 l2hdr->b_daddr = dev->l2ad_hand;
5041 if (!l2arc_nocompress && (ab->b_flags & ARC_L2COMPRESS) &&
5042 l2hdr->b_asize >= buf_compress_minsz) {
5043 if (l2arc_compress_buf(l2hdr)) {
5045 * If compression succeeded, enable headroom
5046 * boost on the next scan cycle.
5048 *headroom_boost = B_TRUE;
5053 * Pick up the buffer data we had previously stashed away
5054 * (and now potentially also compressed).
5056 buf_data = l2hdr->b_tmp_cdata;
5057 buf_sz = l2hdr->b_asize;
5059 /* Compression may have squashed the buffer to zero length. */
5063 wzio = zio_write_phys(pio, dev->l2ad_vdev,
5064 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
5065 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
5066 ZIO_FLAG_CANFAIL, B_FALSE);
5068 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
5070 (void) zio_nowait(wzio);
5072 write_asize += buf_sz;
5074 * Keep the clock hand suitably device-aligned.
5076 buf_p_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
5077 write_psize += buf_p_sz;
5078 dev->l2ad_hand += buf_p_sz;
5082 mutex_exit(&l2arc_buflist_mtx);
5084 ASSERT3U(write_asize, <=, target_sz);
5085 ARCSTAT_BUMP(arcstat_l2_writes_sent);
5086 ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize);
5087 ARCSTAT_INCR(arcstat_l2_size, write_sz);
5088 ARCSTAT_INCR(arcstat_l2_asize, write_asize);
5089 vdev_space_update(dev->l2ad_vdev, write_psize, 0, 0);
5092 * Bump device hand to the device start if it is approaching the end.
5093 * l2arc_evict() will already have evicted ahead for this case.
5095 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
5096 vdev_space_update(dev->l2ad_vdev,
5097 dev->l2ad_end - dev->l2ad_hand, 0, 0);
5098 dev->l2ad_hand = dev->l2ad_start;
5099 dev->l2ad_evict = dev->l2ad_start;
5100 dev->l2ad_first = B_FALSE;
5103 dev->l2ad_writing = B_TRUE;
5104 (void) zio_wait(pio);
5105 dev->l2ad_writing = B_FALSE;
5107 return (write_asize);
5111 * Compresses an L2ARC buffer.
5112 * The data to be compressed must be prefilled in l2hdr->b_tmp_cdata and its
5113 * size in l2hdr->b_asize. This routine tries to compress the data and
5114 * depending on the compression result there are three possible outcomes:
5115 * *) The buffer was incompressible. The original l2hdr contents were left
5116 * untouched and are ready for writing to an L2 device.
5117 * *) The buffer was all-zeros, so there is no need to write it to an L2
5118 * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is
5119 * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY.
5120 * *) Compression succeeded and b_tmp_cdata was replaced with a temporary
5121 * data buffer which holds the compressed data to be written, and b_asize
5122 * tells us how much data there is. b_compress is set to the appropriate
5123 * compression algorithm. Once writing is done, invoke
5124 * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer.
5126 * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the
5127 * buffer was incompressible).
5130 l2arc_compress_buf(l2arc_buf_hdr_t *l2hdr)
5135 ASSERT(l2hdr->b_compress == ZIO_COMPRESS_OFF);
5136 ASSERT(l2hdr->b_tmp_cdata != NULL);
5138 len = l2hdr->b_asize;
5139 cdata = zio_data_buf_alloc(len);
5140 csize = zio_compress_data(ZIO_COMPRESS_LZ4, l2hdr->b_tmp_cdata,
5141 cdata, l2hdr->b_asize);
5144 /* zero block, indicate that there's nothing to write */
5145 zio_data_buf_free(cdata, len);
5146 l2hdr->b_compress = ZIO_COMPRESS_EMPTY;
5148 l2hdr->b_tmp_cdata = NULL;
5149 ARCSTAT_BUMP(arcstat_l2_compress_zeros);
5151 } else if (csize > 0 && csize < len) {
5153 * Compression succeeded, we'll keep the cdata around for
5154 * writing and release it afterwards.
5156 l2hdr->b_compress = ZIO_COMPRESS_LZ4;
5157 l2hdr->b_asize = csize;
5158 l2hdr->b_tmp_cdata = cdata;
5159 ARCSTAT_BUMP(arcstat_l2_compress_successes);
5163 * Compression failed, release the compressed buffer.
5164 * l2hdr will be left unmodified.
5166 zio_data_buf_free(cdata, len);
5167 ARCSTAT_BUMP(arcstat_l2_compress_failures);
5173 * Decompresses a zio read back from an l2arc device. On success, the
5174 * underlying zio's io_data buffer is overwritten by the uncompressed
5175 * version. On decompression error (corrupt compressed stream), the
5176 * zio->io_error value is set to signal an I/O error.
5178 * Please note that the compressed data stream is not checksummed, so
5179 * if the underlying device is experiencing data corruption, we may feed
5180 * corrupt data to the decompressor, so the decompressor needs to be
5181 * able to handle this situation (LZ4 does).
5184 l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c)
5189 ASSERT(L2ARC_IS_VALID_COMPRESS(c));
5191 if (zio->io_error != 0) {
5193 * An io error has occured, just restore the original io
5194 * size in preparation for a main pool read.
5196 zio->io_orig_size = zio->io_size = hdr->b_size;
5200 if (c == ZIO_COMPRESS_EMPTY) {
5202 * An empty buffer results in a null zio, which means we
5203 * need to fill its io_data after we're done restoring the
5204 * buffer's contents.
5206 ASSERT(hdr->b_buf != NULL);
5207 bzero(hdr->b_buf->b_data, hdr->b_size);
5208 zio->io_data = zio->io_orig_data = hdr->b_buf->b_data;
5210 ASSERT(zio->io_data != NULL);
5212 * We copy the compressed data from the start of the arc buffer
5213 * (the zio_read will have pulled in only what we need, the
5214 * rest is garbage which we will overwrite at decompression)
5215 * and then decompress back to the ARC data buffer. This way we
5216 * can minimize copying by simply decompressing back over the
5217 * original compressed data (rather than decompressing to an
5218 * aux buffer and then copying back the uncompressed buffer,
5219 * which is likely to be much larger).
5221 csize = zio->io_size;
5222 cdata = zio_data_buf_alloc(csize);
5223 bcopy(zio->io_data, cdata, csize);
5224 if (zio_decompress_data(c, cdata, zio->io_data, csize,
5226 zio->io_error = SET_ERROR(EIO);
5227 zio_data_buf_free(cdata, csize);
5230 /* Restore the expected uncompressed IO size. */
5231 zio->io_orig_size = zio->io_size = hdr->b_size;
5235 * Releases the temporary b_tmp_cdata buffer in an l2arc header structure.
5236 * This buffer serves as a temporary holder of compressed data while
5237 * the buffer entry is being written to an l2arc device. Once that is
5238 * done, we can dispose of it.
5241 l2arc_release_cdata_buf(arc_buf_hdr_t *ab)
5243 l2arc_buf_hdr_t *l2hdr = ab->b_l2hdr;
5245 if (l2hdr->b_compress == ZIO_COMPRESS_LZ4) {
5247 * If the data was compressed, then we've allocated a
5248 * temporary buffer for it, so now we need to release it.
5250 ASSERT(l2hdr->b_tmp_cdata != NULL);
5251 zio_data_buf_free(l2hdr->b_tmp_cdata, ab->b_size);
5253 l2hdr->b_tmp_cdata = NULL;
5257 * This thread feeds the L2ARC at regular intervals. This is the beating
5258 * heart of the L2ARC.
5261 l2arc_feed_thread(void)
5266 uint64_t size, wrote;
5267 clock_t begin, next = ddi_get_lbolt();
5268 boolean_t headroom_boost = B_FALSE;
5270 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
5272 mutex_enter(&l2arc_feed_thr_lock);
5274 while (l2arc_thread_exit == 0) {
5275 CALLB_CPR_SAFE_BEGIN(&cpr);
5276 (void) cv_timedwait_interruptible(&l2arc_feed_thr_cv,
5277 &l2arc_feed_thr_lock, next);
5278 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
5279 next = ddi_get_lbolt() + hz;
5282 * Quick check for L2ARC devices.
5284 mutex_enter(&l2arc_dev_mtx);
5285 if (l2arc_ndev == 0) {
5286 mutex_exit(&l2arc_dev_mtx);
5289 mutex_exit(&l2arc_dev_mtx);
5290 begin = ddi_get_lbolt();
5293 * This selects the next l2arc device to write to, and in
5294 * doing so the next spa to feed from: dev->l2ad_spa. This
5295 * will return NULL if there are now no l2arc devices or if
5296 * they are all faulted.
5298 * If a device is returned, its spa's config lock is also
5299 * held to prevent device removal. l2arc_dev_get_next()
5300 * will grab and release l2arc_dev_mtx.
5302 if ((dev = l2arc_dev_get_next()) == NULL)
5305 spa = dev->l2ad_spa;
5306 ASSERT(spa != NULL);
5309 * If the pool is read-only then force the feed thread to
5310 * sleep a little longer.
5312 if (!spa_writeable(spa)) {
5313 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
5314 spa_config_exit(spa, SCL_L2ARC, dev);
5319 * Avoid contributing to memory pressure.
5322 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
5323 spa_config_exit(spa, SCL_L2ARC, dev);
5327 ARCSTAT_BUMP(arcstat_l2_feeds);
5329 size = l2arc_write_size();
5332 * Evict L2ARC buffers that will be overwritten.
5334 l2arc_evict(dev, size, B_FALSE);
5337 * Write ARC buffers.
5339 wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost);
5342 * Calculate interval between writes.
5344 next = l2arc_write_interval(begin, size, wrote);
5345 spa_config_exit(spa, SCL_L2ARC, dev);
5348 l2arc_thread_exit = 0;
5349 cv_broadcast(&l2arc_feed_thr_cv);
5350 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
5355 l2arc_vdev_present(vdev_t *vd)
5359 mutex_enter(&l2arc_dev_mtx);
5360 for (dev = list_head(l2arc_dev_list); dev != NULL;
5361 dev = list_next(l2arc_dev_list, dev)) {
5362 if (dev->l2ad_vdev == vd)
5365 mutex_exit(&l2arc_dev_mtx);
5367 return (dev != NULL);
5371 * Add a vdev for use by the L2ARC. By this point the spa has already
5372 * validated the vdev and opened it.
5375 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
5377 l2arc_dev_t *adddev;
5379 ASSERT(!l2arc_vdev_present(vd));
5382 * Create a new l2arc device entry.
5384 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
5385 adddev->l2ad_spa = spa;
5386 adddev->l2ad_vdev = vd;
5387 adddev->l2ad_start = VDEV_LABEL_START_SIZE;
5388 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
5389 adddev->l2ad_hand = adddev->l2ad_start;
5390 adddev->l2ad_evict = adddev->l2ad_start;
5391 adddev->l2ad_first = B_TRUE;
5392 adddev->l2ad_writing = B_FALSE;
5393 list_link_init(&adddev->l2ad_node);
5396 * This is a list of all ARC buffers that are still valid on the
5399 adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
5400 list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
5401 offsetof(arc_buf_hdr_t, b_l2node));
5403 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
5406 * Add device to global list
5408 mutex_enter(&l2arc_dev_mtx);
5409 list_insert_head(l2arc_dev_list, adddev);
5410 atomic_inc_64(&l2arc_ndev);
5411 mutex_exit(&l2arc_dev_mtx);
5415 * Remove a vdev from the L2ARC.
5418 l2arc_remove_vdev(vdev_t *vd)
5420 l2arc_dev_t *dev, *nextdev, *remdev = NULL;
5423 * Find the device by vdev
5425 mutex_enter(&l2arc_dev_mtx);
5426 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
5427 nextdev = list_next(l2arc_dev_list, dev);
5428 if (vd == dev->l2ad_vdev) {
5433 ASSERT(remdev != NULL);
5436 * Remove device from global list
5438 list_remove(l2arc_dev_list, remdev);
5439 l2arc_dev_last = NULL; /* may have been invalidated */
5440 atomic_dec_64(&l2arc_ndev);
5441 mutex_exit(&l2arc_dev_mtx);
5444 * Clear all buflists and ARC references. L2ARC device flush.
5446 l2arc_evict(remdev, 0, B_TRUE);
5447 list_destroy(remdev->l2ad_buflist);
5448 kmem_free(remdev->l2ad_buflist, sizeof (list_t));
5449 kmem_free(remdev, sizeof (l2arc_dev_t));
5455 l2arc_thread_exit = 0;
5457 l2arc_writes_sent = 0;
5458 l2arc_writes_done = 0;
5460 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
5461 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
5462 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
5463 mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
5464 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
5466 l2arc_dev_list = &L2ARC_dev_list;
5467 l2arc_free_on_write = &L2ARC_free_on_write;
5468 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
5469 offsetof(l2arc_dev_t, l2ad_node));
5470 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
5471 offsetof(l2arc_data_free_t, l2df_list_node));
5478 * This is called from dmu_fini(), which is called from spa_fini();
5479 * Because of this, we can assume that all l2arc devices have
5480 * already been removed when the pools themselves were removed.
5483 l2arc_do_free_on_write();
5485 mutex_destroy(&l2arc_feed_thr_lock);
5486 cv_destroy(&l2arc_feed_thr_cv);
5487 mutex_destroy(&l2arc_dev_mtx);
5488 mutex_destroy(&l2arc_buflist_mtx);
5489 mutex_destroy(&l2arc_free_on_write_mtx);
5491 list_destroy(l2arc_dev_list);
5492 list_destroy(l2arc_free_on_write);
5498 if (!(spa_mode_global & FWRITE))
5501 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
5502 TS_RUN, minclsyspri);
5508 if (!(spa_mode_global & FWRITE))
5511 mutex_enter(&l2arc_feed_thr_lock);
5512 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
5513 l2arc_thread_exit = 1;
5514 while (l2arc_thread_exit != 0)
5515 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
5516 mutex_exit(&l2arc_feed_thr_lock);
5519 #if defined(_KERNEL) && defined(HAVE_SPL)
5520 EXPORT_SYMBOL(arc_read);
5521 EXPORT_SYMBOL(arc_buf_remove_ref);
5522 EXPORT_SYMBOL(arc_buf_info);
5523 EXPORT_SYMBOL(arc_getbuf_func);
5524 EXPORT_SYMBOL(arc_add_prune_callback);
5525 EXPORT_SYMBOL(arc_remove_prune_callback);
5527 module_param(zfs_arc_min, ulong, 0644);
5528 MODULE_PARM_DESC(zfs_arc_min, "Min arc size");
5530 module_param(zfs_arc_max, ulong, 0644);
5531 MODULE_PARM_DESC(zfs_arc_max, "Max arc size");
5533 module_param(zfs_arc_meta_limit, ulong, 0644);
5534 MODULE_PARM_DESC(zfs_arc_meta_limit, "Meta limit for arc size");
5536 module_param(zfs_arc_meta_prune, int, 0644);
5537 MODULE_PARM_DESC(zfs_arc_meta_prune, "Bytes of meta data to prune");
5539 module_param(zfs_arc_grow_retry, int, 0644);
5540 MODULE_PARM_DESC(zfs_arc_grow_retry, "Seconds before growing arc size");
5542 module_param(zfs_arc_shrink_shift, int, 0644);
5543 MODULE_PARM_DESC(zfs_arc_shrink_shift, "log2(fraction of arc to reclaim)");
5545 module_param(zfs_arc_p_min_shift, int, 0644);
5546 MODULE_PARM_DESC(zfs_arc_p_min_shift, "arc_c shift to calc min/max arc_p");
5548 module_param(zfs_disable_dup_eviction, int, 0644);
5549 MODULE_PARM_DESC(zfs_disable_dup_eviction, "disable duplicate buffer eviction");
5551 module_param(zfs_arc_memory_throttle_disable, int, 0644);
5552 MODULE_PARM_DESC(zfs_arc_memory_throttle_disable, "disable memory throttle");
5554 module_param(zfs_arc_min_prefetch_lifespan, int, 0644);
5555 MODULE_PARM_DESC(zfs_arc_min_prefetch_lifespan, "Min life of prefetch block");
5557 module_param(l2arc_write_max, ulong, 0644);
5558 MODULE_PARM_DESC(l2arc_write_max, "Max write bytes per interval");
5560 module_param(l2arc_write_boost, ulong, 0644);
5561 MODULE_PARM_DESC(l2arc_write_boost, "Extra write bytes during device warmup");
5563 module_param(l2arc_headroom, ulong, 0644);
5564 MODULE_PARM_DESC(l2arc_headroom, "Number of max device writes to precache");
5566 module_param(l2arc_headroom_boost, ulong, 0644);
5567 MODULE_PARM_DESC(l2arc_headroom_boost, "Compressed l2arc_headroom multiplier");
5569 module_param(l2arc_feed_secs, ulong, 0644);
5570 MODULE_PARM_DESC(l2arc_feed_secs, "Seconds between L2ARC writing");
5572 module_param(l2arc_feed_min_ms, ulong, 0644);
5573 MODULE_PARM_DESC(l2arc_feed_min_ms, "Min feed interval in milliseconds");
5575 module_param(l2arc_noprefetch, int, 0644);
5576 MODULE_PARM_DESC(l2arc_noprefetch, "Skip caching prefetched buffers");
5578 module_param(l2arc_nocompress, int, 0644);
5579 MODULE_PARM_DESC(l2arc_nocompress, "Skip compressing L2ARC buffers");
5581 module_param(l2arc_feed_again, int, 0644);
5582 MODULE_PARM_DESC(l2arc_feed_again, "Turbo L2ARC warmup");
5584 module_param(l2arc_norw, int, 0644);
5585 MODULE_PARM_DESC(l2arc_norw, "No reads during writes");