4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2011 by Delphix. All rights reserved.
28 * DVA-based Adjustable Replacement Cache
30 * While much of the theory of operation used here is
31 * based on the self-tuning, low overhead replacement cache
32 * presented by Megiddo and Modha at FAST 2003, there are some
33 * significant differences:
35 * 1. The Megiddo and Modha model assumes any page is evictable.
36 * Pages in its cache cannot be "locked" into memory. This makes
37 * the eviction algorithm simple: evict the last page in the list.
38 * This also make the performance characteristics easy to reason
39 * about. Our cache is not so simple. At any given moment, some
40 * subset of the blocks in the cache are un-evictable because we
41 * have handed out a reference to them. Blocks are only evictable
42 * when there are no external references active. This makes
43 * eviction far more problematic: we choose to evict the evictable
44 * blocks that are the "lowest" in the list.
46 * There are times when it is not possible to evict the requested
47 * space. In these circumstances we are unable to adjust the cache
48 * size. To prevent the cache growing unbounded at these times we
49 * implement a "cache throttle" that slows the flow of new data
50 * into the cache until we can make space available.
52 * 2. The Megiddo and Modha model assumes a fixed cache size.
53 * Pages are evicted when the cache is full and there is a cache
54 * miss. Our model has a variable sized cache. It grows with
55 * high use, but also tries to react to memory pressure from the
56 * operating system: decreasing its size when system memory is
59 * 3. The Megiddo and Modha model assumes a fixed page size. All
60 * elements of the cache are therefor exactly the same size. So
61 * when adjusting the cache size following a cache miss, its simply
62 * a matter of choosing a single page to evict. In our model, we
63 * have variable sized cache blocks (rangeing from 512 bytes to
64 * 128K bytes). We therefor choose a set of blocks to evict to make
65 * space for a cache miss that approximates as closely as possible
66 * the space used by the new block.
68 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
69 * by N. Megiddo & D. Modha, FAST 2003
75 * A new reference to a cache buffer can be obtained in two
76 * ways: 1) via a hash table lookup using the DVA as a key,
77 * or 2) via one of the ARC lists. The arc_read() interface
78 * uses method 1, while the internal arc algorithms for
79 * adjusting the cache use method 2. We therefor provide two
80 * types of locks: 1) the hash table lock array, and 2) the
83 * Buffers do not have their own mutexs, rather they rely on the
84 * hash table mutexs for the bulk of their protection (i.e. most
85 * fields in the arc_buf_hdr_t are protected by these mutexs).
87 * buf_hash_find() returns the appropriate mutex (held) when it
88 * locates the requested buffer in the hash table. It returns
89 * NULL for the mutex if the buffer was not in the table.
91 * buf_hash_remove() expects the appropriate hash mutex to be
92 * already held before it is invoked.
94 * Each arc state also has a mutex which is used to protect the
95 * buffer list associated with the state. When attempting to
96 * obtain a hash table lock while holding an arc list lock you
97 * must use: mutex_tryenter() to avoid deadlock. Also note that
98 * the active state mutex must be held before the ghost state mutex.
100 * Arc buffers may have an associated eviction callback function.
101 * This function will be invoked prior to removing the buffer (e.g.
102 * in arc_do_user_evicts()). Note however that the data associated
103 * with the buffer may be evicted prior to the callback. The callback
104 * must be made with *no locks held* (to prevent deadlock). Additionally,
105 * the users of callbacks must ensure that their private data is
106 * protected from simultaneous callbacks from arc_buf_evict()
107 * and arc_do_user_evicts().
109 * Note that the majority of the performance stats are manipulated
110 * with atomic operations.
112 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
114 * - L2ARC buflist creation
115 * - L2ARC buflist eviction
116 * - L2ARC write completion, which walks L2ARC buflists
117 * - ARC header destruction, as it removes from L2ARC buflists
118 * - ARC header release, as it removes from L2ARC buflists
123 #include <sys/zfs_context.h>
125 #include <sys/refcount.h>
126 #include <sys/vdev.h>
127 #include <sys/vdev_impl.h>
129 #include <sys/dnlc.h>
131 #include <sys/callb.h>
132 #include <sys/kstat.h>
133 #include <zfs_fletcher.h>
136 #include <vm/vm_pageout.h>
140 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
141 boolean_t arc_watch = B_FALSE;
146 static kmutex_t arc_reclaim_thr_lock;
147 static kcondvar_t arc_reclaim_thr_cv; /* used to signal reclaim thr */
148 static uint8_t arc_thread_exit;
150 extern int zfs_write_limit_shift;
151 extern uint64_t zfs_write_limit_max;
152 extern kmutex_t zfs_write_limit_lock;
154 #define ARC_REDUCE_DNLC_PERCENT 3
155 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
157 typedef enum arc_reclaim_strategy {
158 ARC_RECLAIM_AGGR, /* Aggressive reclaim strategy */
159 ARC_RECLAIM_CONS /* Conservative reclaim strategy */
160 } arc_reclaim_strategy_t;
162 /* number of seconds before growing cache again */
163 static int arc_grow_retry = 60;
165 /* shift of arc_c for calculating both min and max arc_p */
166 static int arc_p_min_shift = 4;
168 /* log2(fraction of arc to reclaim) */
169 static int arc_shrink_shift = 5;
172 * minimum lifespan of a prefetch block in clock ticks
173 * (initialized in arc_init())
175 static int arc_min_prefetch_lifespan;
178 extern int zfs_prefetch_disable;
181 * The arc has filled available memory and has now warmed up.
183 static boolean_t arc_warm;
186 * These tunables are for performance analysis.
188 uint64_t zfs_arc_max;
189 uint64_t zfs_arc_min;
190 uint64_t zfs_arc_meta_limit = 0;
191 int zfs_arc_grow_retry = 0;
192 int zfs_arc_shrink_shift = 0;
193 int zfs_arc_p_min_shift = 0;
195 TUNABLE_QUAD("vfs.zfs.arc_max", &zfs_arc_max);
196 TUNABLE_QUAD("vfs.zfs.arc_min", &zfs_arc_min);
197 TUNABLE_QUAD("vfs.zfs.arc_meta_limit", &zfs_arc_meta_limit);
198 SYSCTL_DECL(_vfs_zfs);
199 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_max, CTLFLAG_RDTUN, &zfs_arc_max, 0,
201 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_min, CTLFLAG_RDTUN, &zfs_arc_min, 0,
205 * Note that buffers can be in one of 6 states:
206 * ARC_anon - anonymous (discussed below)
207 * ARC_mru - recently used, currently cached
208 * ARC_mru_ghost - recentely used, no longer in cache
209 * ARC_mfu - frequently used, currently cached
210 * ARC_mfu_ghost - frequently used, no longer in cache
211 * ARC_l2c_only - exists in L2ARC but not other states
212 * When there are no active references to the buffer, they are
213 * are linked onto a list in one of these arc states. These are
214 * the only buffers that can be evicted or deleted. Within each
215 * state there are multiple lists, one for meta-data and one for
216 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
217 * etc.) is tracked separately so that it can be managed more
218 * explicitly: favored over data, limited explicitly.
220 * Anonymous buffers are buffers that are not associated with
221 * a DVA. These are buffers that hold dirty block copies
222 * before they are written to stable storage. By definition,
223 * they are "ref'd" and are considered part of arc_mru
224 * that cannot be freed. Generally, they will aquire a DVA
225 * as they are written and migrate onto the arc_mru list.
227 * The ARC_l2c_only state is for buffers that are in the second
228 * level ARC but no longer in any of the ARC_m* lists. The second
229 * level ARC itself may also contain buffers that are in any of
230 * the ARC_m* states - meaning that a buffer can exist in two
231 * places. The reason for the ARC_l2c_only state is to keep the
232 * buffer header in the hash table, so that reads that hit the
233 * second level ARC benefit from these fast lookups.
236 #define ARCS_LOCK_PAD CACHE_LINE_SIZE
240 unsigned char pad[(ARCS_LOCK_PAD - sizeof (kmutex_t))];
245 * must be power of two for mask use to work
248 #define ARC_BUFC_NUMDATALISTS 16
249 #define ARC_BUFC_NUMMETADATALISTS 16
250 #define ARC_BUFC_NUMLISTS (ARC_BUFC_NUMMETADATALISTS + ARC_BUFC_NUMDATALISTS)
252 typedef struct arc_state {
253 uint64_t arcs_lsize[ARC_BUFC_NUMTYPES]; /* amount of evictable data */
254 uint64_t arcs_size; /* total amount of data in this state */
255 list_t arcs_lists[ARC_BUFC_NUMLISTS]; /* list of evictable buffers */
256 struct arcs_lock arcs_locks[ARC_BUFC_NUMLISTS] __aligned(CACHE_LINE_SIZE);
259 #define ARCS_LOCK(s, i) (&((s)->arcs_locks[(i)].arcs_lock))
262 static arc_state_t ARC_anon;
263 static arc_state_t ARC_mru;
264 static arc_state_t ARC_mru_ghost;
265 static arc_state_t ARC_mfu;
266 static arc_state_t ARC_mfu_ghost;
267 static arc_state_t ARC_l2c_only;
269 typedef struct arc_stats {
270 kstat_named_t arcstat_hits;
271 kstat_named_t arcstat_misses;
272 kstat_named_t arcstat_demand_data_hits;
273 kstat_named_t arcstat_demand_data_misses;
274 kstat_named_t arcstat_demand_metadata_hits;
275 kstat_named_t arcstat_demand_metadata_misses;
276 kstat_named_t arcstat_prefetch_data_hits;
277 kstat_named_t arcstat_prefetch_data_misses;
278 kstat_named_t arcstat_prefetch_metadata_hits;
279 kstat_named_t arcstat_prefetch_metadata_misses;
280 kstat_named_t arcstat_mru_hits;
281 kstat_named_t arcstat_mru_ghost_hits;
282 kstat_named_t arcstat_mfu_hits;
283 kstat_named_t arcstat_mfu_ghost_hits;
284 kstat_named_t arcstat_allocated;
285 kstat_named_t arcstat_deleted;
286 kstat_named_t arcstat_stolen;
287 kstat_named_t arcstat_recycle_miss;
288 kstat_named_t arcstat_mutex_miss;
289 kstat_named_t arcstat_evict_skip;
290 kstat_named_t arcstat_evict_l2_cached;
291 kstat_named_t arcstat_evict_l2_eligible;
292 kstat_named_t arcstat_evict_l2_ineligible;
293 kstat_named_t arcstat_hash_elements;
294 kstat_named_t arcstat_hash_elements_max;
295 kstat_named_t arcstat_hash_collisions;
296 kstat_named_t arcstat_hash_chains;
297 kstat_named_t arcstat_hash_chain_max;
298 kstat_named_t arcstat_p;
299 kstat_named_t arcstat_c;
300 kstat_named_t arcstat_c_min;
301 kstat_named_t arcstat_c_max;
302 kstat_named_t arcstat_size;
303 kstat_named_t arcstat_hdr_size;
304 kstat_named_t arcstat_data_size;
305 kstat_named_t arcstat_other_size;
306 kstat_named_t arcstat_l2_hits;
307 kstat_named_t arcstat_l2_misses;
308 kstat_named_t arcstat_l2_feeds;
309 kstat_named_t arcstat_l2_rw_clash;
310 kstat_named_t arcstat_l2_read_bytes;
311 kstat_named_t arcstat_l2_write_bytes;
312 kstat_named_t arcstat_l2_writes_sent;
313 kstat_named_t arcstat_l2_writes_done;
314 kstat_named_t arcstat_l2_writes_error;
315 kstat_named_t arcstat_l2_writes_hdr_miss;
316 kstat_named_t arcstat_l2_evict_lock_retry;
317 kstat_named_t arcstat_l2_evict_reading;
318 kstat_named_t arcstat_l2_free_on_write;
319 kstat_named_t arcstat_l2_abort_lowmem;
320 kstat_named_t arcstat_l2_cksum_bad;
321 kstat_named_t arcstat_l2_io_error;
322 kstat_named_t arcstat_l2_size;
323 kstat_named_t arcstat_l2_hdr_size;
324 kstat_named_t arcstat_memory_throttle_count;
325 kstat_named_t arcstat_l2_write_trylock_fail;
326 kstat_named_t arcstat_l2_write_passed_headroom;
327 kstat_named_t arcstat_l2_write_spa_mismatch;
328 kstat_named_t arcstat_l2_write_in_l2;
329 kstat_named_t arcstat_l2_write_hdr_io_in_progress;
330 kstat_named_t arcstat_l2_write_not_cacheable;
331 kstat_named_t arcstat_l2_write_full;
332 kstat_named_t arcstat_l2_write_buffer_iter;
333 kstat_named_t arcstat_l2_write_pios;
334 kstat_named_t arcstat_l2_write_buffer_bytes_scanned;
335 kstat_named_t arcstat_l2_write_buffer_list_iter;
336 kstat_named_t arcstat_l2_write_buffer_list_null_iter;
339 static arc_stats_t arc_stats = {
340 { "hits", KSTAT_DATA_UINT64 },
341 { "misses", KSTAT_DATA_UINT64 },
342 { "demand_data_hits", KSTAT_DATA_UINT64 },
343 { "demand_data_misses", KSTAT_DATA_UINT64 },
344 { "demand_metadata_hits", KSTAT_DATA_UINT64 },
345 { "demand_metadata_misses", KSTAT_DATA_UINT64 },
346 { "prefetch_data_hits", KSTAT_DATA_UINT64 },
347 { "prefetch_data_misses", KSTAT_DATA_UINT64 },
348 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
349 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
350 { "mru_hits", KSTAT_DATA_UINT64 },
351 { "mru_ghost_hits", KSTAT_DATA_UINT64 },
352 { "mfu_hits", KSTAT_DATA_UINT64 },
353 { "mfu_ghost_hits", KSTAT_DATA_UINT64 },
354 { "allocated", KSTAT_DATA_UINT64 },
355 { "deleted", KSTAT_DATA_UINT64 },
356 { "stolen", KSTAT_DATA_UINT64 },
357 { "recycle_miss", KSTAT_DATA_UINT64 },
358 { "mutex_miss", KSTAT_DATA_UINT64 },
359 { "evict_skip", KSTAT_DATA_UINT64 },
360 { "evict_l2_cached", KSTAT_DATA_UINT64 },
361 { "evict_l2_eligible", KSTAT_DATA_UINT64 },
362 { "evict_l2_ineligible", KSTAT_DATA_UINT64 },
363 { "hash_elements", KSTAT_DATA_UINT64 },
364 { "hash_elements_max", KSTAT_DATA_UINT64 },
365 { "hash_collisions", KSTAT_DATA_UINT64 },
366 { "hash_chains", KSTAT_DATA_UINT64 },
367 { "hash_chain_max", KSTAT_DATA_UINT64 },
368 { "p", KSTAT_DATA_UINT64 },
369 { "c", KSTAT_DATA_UINT64 },
370 { "c_min", KSTAT_DATA_UINT64 },
371 { "c_max", KSTAT_DATA_UINT64 },
372 { "size", KSTAT_DATA_UINT64 },
373 { "hdr_size", KSTAT_DATA_UINT64 },
374 { "data_size", KSTAT_DATA_UINT64 },
375 { "other_size", KSTAT_DATA_UINT64 },
376 { "l2_hits", KSTAT_DATA_UINT64 },
377 { "l2_misses", KSTAT_DATA_UINT64 },
378 { "l2_feeds", KSTAT_DATA_UINT64 },
379 { "l2_rw_clash", KSTAT_DATA_UINT64 },
380 { "l2_read_bytes", KSTAT_DATA_UINT64 },
381 { "l2_write_bytes", KSTAT_DATA_UINT64 },
382 { "l2_writes_sent", KSTAT_DATA_UINT64 },
383 { "l2_writes_done", KSTAT_DATA_UINT64 },
384 { "l2_writes_error", KSTAT_DATA_UINT64 },
385 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64 },
386 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
387 { "l2_evict_reading", KSTAT_DATA_UINT64 },
388 { "l2_free_on_write", KSTAT_DATA_UINT64 },
389 { "l2_abort_lowmem", KSTAT_DATA_UINT64 },
390 { "l2_cksum_bad", KSTAT_DATA_UINT64 },
391 { "l2_io_error", KSTAT_DATA_UINT64 },
392 { "l2_size", KSTAT_DATA_UINT64 },
393 { "l2_hdr_size", KSTAT_DATA_UINT64 },
394 { "memory_throttle_count", KSTAT_DATA_UINT64 },
395 { "l2_write_trylock_fail", KSTAT_DATA_UINT64 },
396 { "l2_write_passed_headroom", KSTAT_DATA_UINT64 },
397 { "l2_write_spa_mismatch", KSTAT_DATA_UINT64 },
398 { "l2_write_in_l2", KSTAT_DATA_UINT64 },
399 { "l2_write_io_in_progress", KSTAT_DATA_UINT64 },
400 { "l2_write_not_cacheable", KSTAT_DATA_UINT64 },
401 { "l2_write_full", KSTAT_DATA_UINT64 },
402 { "l2_write_buffer_iter", KSTAT_DATA_UINT64 },
403 { "l2_write_pios", KSTAT_DATA_UINT64 },
404 { "l2_write_buffer_bytes_scanned", KSTAT_DATA_UINT64 },
405 { "l2_write_buffer_list_iter", KSTAT_DATA_UINT64 },
406 { "l2_write_buffer_list_null_iter", KSTAT_DATA_UINT64 }
409 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
411 #define ARCSTAT_INCR(stat, val) \
412 atomic_add_64(&arc_stats.stat.value.ui64, (val));
414 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
415 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
417 #define ARCSTAT_MAX(stat, val) { \
419 while ((val) > (m = arc_stats.stat.value.ui64) && \
420 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
424 #define ARCSTAT_MAXSTAT(stat) \
425 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
428 * We define a macro to allow ARC hits/misses to be easily broken down by
429 * two separate conditions, giving a total of four different subtypes for
430 * each of hits and misses (so eight statistics total).
432 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
435 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
437 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
441 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
443 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
448 static arc_state_t *arc_anon;
449 static arc_state_t *arc_mru;
450 static arc_state_t *arc_mru_ghost;
451 static arc_state_t *arc_mfu;
452 static arc_state_t *arc_mfu_ghost;
453 static arc_state_t *arc_l2c_only;
456 * There are several ARC variables that are critical to export as kstats --
457 * but we don't want to have to grovel around in the kstat whenever we wish to
458 * manipulate them. For these variables, we therefore define them to be in
459 * terms of the statistic variable. This assures that we are not introducing
460 * the possibility of inconsistency by having shadow copies of the variables,
461 * while still allowing the code to be readable.
463 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
464 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
465 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
466 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
467 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
469 static int arc_no_grow; /* Don't try to grow cache size */
470 static uint64_t arc_tempreserve;
471 static uint64_t arc_loaned_bytes;
472 static uint64_t arc_meta_used;
473 static uint64_t arc_meta_limit;
474 static uint64_t arc_meta_max = 0;
475 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_used, CTLFLAG_RDTUN,
476 &arc_meta_used, 0, "ARC metadata used");
477 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, arc_meta_limit, CTLFLAG_RDTUN,
478 &arc_meta_limit, 0, "ARC metadata limit");
480 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
482 typedef struct arc_callback arc_callback_t;
484 struct arc_callback {
486 arc_done_func_t *acb_done;
488 zio_t *acb_zio_dummy;
489 arc_callback_t *acb_next;
492 typedef struct arc_write_callback arc_write_callback_t;
494 struct arc_write_callback {
496 arc_done_func_t *awcb_ready;
497 arc_done_func_t *awcb_done;
502 /* protected by hash lock */
507 kmutex_t b_freeze_lock;
508 zio_cksum_t *b_freeze_cksum;
511 arc_buf_hdr_t *b_hash_next;
516 arc_callback_t *b_acb;
520 arc_buf_contents_t b_type;
524 /* protected by arc state mutex */
525 arc_state_t *b_state;
526 list_node_t b_arc_node;
528 /* updated atomically */
529 clock_t b_arc_access;
531 /* self protecting */
534 l2arc_buf_hdr_t *b_l2hdr;
535 list_node_t b_l2node;
538 static arc_buf_t *arc_eviction_list;
539 static kmutex_t arc_eviction_mtx;
540 static arc_buf_hdr_t arc_eviction_hdr;
541 static void arc_get_data_buf(arc_buf_t *buf);
542 static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
543 static int arc_evict_needed(arc_buf_contents_t type);
544 static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
546 static void arc_buf_watch(arc_buf_t *buf);
549 static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
551 #define GHOST_STATE(state) \
552 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
553 (state) == arc_l2c_only)
556 * Private ARC flags. These flags are private ARC only flags that will show up
557 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
558 * be passed in as arc_flags in things like arc_read. However, these flags
559 * should never be passed and should only be set by ARC code. When adding new
560 * public flags, make sure not to smash the private ones.
563 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
564 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
565 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
566 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
567 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
568 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
569 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
570 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
571 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
572 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
574 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
575 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
576 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
577 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
578 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
579 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
580 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
581 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
582 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
583 (hdr)->b_l2hdr != NULL)
584 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
585 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
586 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
592 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
593 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
596 * Hash table routines
599 #define HT_LOCK_PAD CACHE_LINE_SIZE
604 unsigned char pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
608 #define BUF_LOCKS 256
609 typedef struct buf_hash_table {
611 arc_buf_hdr_t **ht_table;
612 struct ht_lock ht_locks[BUF_LOCKS] __aligned(CACHE_LINE_SIZE);
615 static buf_hash_table_t buf_hash_table;
617 #define BUF_HASH_INDEX(spa, dva, birth) \
618 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
619 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
620 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
621 #define HDR_LOCK(hdr) \
622 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
624 uint64_t zfs_crc64_table[256];
630 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
631 #define L2ARC_HEADROOM 2 /* num of writes */
632 #define L2ARC_FEED_SECS 1 /* caching interval secs */
633 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
635 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
636 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
639 * L2ARC Performance Tunables
641 uint64_t l2arc_write_max = L2ARC_WRITE_SIZE; /* default max write size */
642 uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra write during warmup */
643 uint64_t l2arc_headroom = L2ARC_HEADROOM; /* number of dev writes */
644 uint64_t l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */
645 uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval milliseconds */
646 boolean_t l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */
647 boolean_t l2arc_feed_again = B_TRUE; /* turbo warmup */
648 boolean_t l2arc_norw = B_TRUE; /* no reads during writes */
650 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_max, CTLFLAG_RW,
651 &l2arc_write_max, 0, "max write size");
652 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_write_boost, CTLFLAG_RW,
653 &l2arc_write_boost, 0, "extra write during warmup");
654 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_headroom, CTLFLAG_RW,
655 &l2arc_headroom, 0, "number of dev writes");
656 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_secs, CTLFLAG_RW,
657 &l2arc_feed_secs, 0, "interval seconds");
658 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2arc_feed_min_ms, CTLFLAG_RW,
659 &l2arc_feed_min_ms, 0, "min interval milliseconds");
661 SYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_noprefetch, CTLFLAG_RW,
662 &l2arc_noprefetch, 0, "don't cache prefetch bufs");
663 SYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_feed_again, CTLFLAG_RW,
664 &l2arc_feed_again, 0, "turbo warmup");
665 SYSCTL_INT(_vfs_zfs, OID_AUTO, l2arc_norw, CTLFLAG_RW,
666 &l2arc_norw, 0, "no reads during writes");
668 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_size, CTLFLAG_RD,
669 &ARC_anon.arcs_size, 0, "size of anonymous state");
670 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_metadata_lsize, CTLFLAG_RD,
671 &ARC_anon.arcs_lsize[ARC_BUFC_METADATA], 0, "size of anonymous state");
672 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, anon_data_lsize, CTLFLAG_RD,
673 &ARC_anon.arcs_lsize[ARC_BUFC_DATA], 0, "size of anonymous state");
675 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_size, CTLFLAG_RD,
676 &ARC_mru.arcs_size, 0, "size of mru state");
677 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_metadata_lsize, CTLFLAG_RD,
678 &ARC_mru.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mru state");
679 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_data_lsize, CTLFLAG_RD,
680 &ARC_mru.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mru state");
682 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_size, CTLFLAG_RD,
683 &ARC_mru_ghost.arcs_size, 0, "size of mru ghost state");
684 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_metadata_lsize, CTLFLAG_RD,
685 &ARC_mru_ghost.arcs_lsize[ARC_BUFC_METADATA], 0,
686 "size of metadata in mru ghost state");
687 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mru_ghost_data_lsize, CTLFLAG_RD,
688 &ARC_mru_ghost.arcs_lsize[ARC_BUFC_DATA], 0,
689 "size of data in mru ghost state");
691 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_size, CTLFLAG_RD,
692 &ARC_mfu.arcs_size, 0, "size of mfu state");
693 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_metadata_lsize, CTLFLAG_RD,
694 &ARC_mfu.arcs_lsize[ARC_BUFC_METADATA], 0, "size of metadata in mfu state");
695 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_data_lsize, CTLFLAG_RD,
696 &ARC_mfu.arcs_lsize[ARC_BUFC_DATA], 0, "size of data in mfu state");
698 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_size, CTLFLAG_RD,
699 &ARC_mfu_ghost.arcs_size, 0, "size of mfu ghost state");
700 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_metadata_lsize, CTLFLAG_RD,
701 &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_METADATA], 0,
702 "size of metadata in mfu ghost state");
703 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, mfu_ghost_data_lsize, CTLFLAG_RD,
704 &ARC_mfu_ghost.arcs_lsize[ARC_BUFC_DATA], 0,
705 "size of data in mfu ghost state");
707 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, l2c_only_size, CTLFLAG_RD,
708 &ARC_l2c_only.arcs_size, 0, "size of mru state");
713 typedef struct l2arc_dev {
714 vdev_t *l2ad_vdev; /* vdev */
715 spa_t *l2ad_spa; /* spa */
716 uint64_t l2ad_hand; /* next write location */
717 uint64_t l2ad_write; /* desired write size, bytes */
718 uint64_t l2ad_boost; /* warmup write boost, bytes */
719 uint64_t l2ad_start; /* first addr on device */
720 uint64_t l2ad_end; /* last addr on device */
721 uint64_t l2ad_evict; /* last addr eviction reached */
722 boolean_t l2ad_first; /* first sweep through */
723 boolean_t l2ad_writing; /* currently writing */
724 list_t *l2ad_buflist; /* buffer list */
725 list_node_t l2ad_node; /* device list node */
728 static list_t L2ARC_dev_list; /* device list */
729 static list_t *l2arc_dev_list; /* device list pointer */
730 static kmutex_t l2arc_dev_mtx; /* device list mutex */
731 static l2arc_dev_t *l2arc_dev_last; /* last device used */
732 static kmutex_t l2arc_buflist_mtx; /* mutex for all buflists */
733 static list_t L2ARC_free_on_write; /* free after write buf list */
734 static list_t *l2arc_free_on_write; /* free after write list ptr */
735 static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */
736 static uint64_t l2arc_ndev; /* number of devices */
738 typedef struct l2arc_read_callback {
739 arc_buf_t *l2rcb_buf; /* read buffer */
740 spa_t *l2rcb_spa; /* spa */
741 blkptr_t l2rcb_bp; /* original blkptr */
742 zbookmark_t l2rcb_zb; /* original bookmark */
743 int l2rcb_flags; /* original flags */
744 } l2arc_read_callback_t;
746 typedef struct l2arc_write_callback {
747 l2arc_dev_t *l2wcb_dev; /* device info */
748 arc_buf_hdr_t *l2wcb_head; /* head of write buflist */
749 } l2arc_write_callback_t;
751 struct l2arc_buf_hdr {
752 /* protected by arc_buf_hdr mutex */
753 l2arc_dev_t *b_dev; /* L2ARC device */
754 uint64_t b_daddr; /* disk address, offset byte */
757 typedef struct l2arc_data_free {
758 /* protected by l2arc_free_on_write_mtx */
761 void (*l2df_func)(void *, size_t);
762 list_node_t l2df_list_node;
765 static kmutex_t l2arc_feed_thr_lock;
766 static kcondvar_t l2arc_feed_thr_cv;
767 static uint8_t l2arc_thread_exit;
769 static void l2arc_read_done(zio_t *zio);
770 static void l2arc_hdr_stat_add(void);
771 static void l2arc_hdr_stat_remove(void);
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 kmem_free(buf_hash_table.ht_table,
909 (buf_hash_table.ht_mask + 1) * sizeof (void *));
910 for (i = 0; i < BUF_LOCKS; i++)
911 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
912 kmem_cache_destroy(hdr_cache);
913 kmem_cache_destroy(buf_cache);
917 * Constructor callback - called when the cache is empty
918 * and a new buf is requested.
922 hdr_cons(void *vbuf, void *unused, int kmflag)
924 arc_buf_hdr_t *buf = vbuf;
926 bzero(buf, sizeof (arc_buf_hdr_t));
927 refcount_create(&buf->b_refcnt);
928 cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
929 mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
930 arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
937 buf_cons(void *vbuf, void *unused, int kmflag)
939 arc_buf_t *buf = vbuf;
941 bzero(buf, sizeof (arc_buf_t));
942 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
943 rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL);
944 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
950 * Destructor callback - called when a cached buf is
951 * no longer required.
955 hdr_dest(void *vbuf, void *unused)
957 arc_buf_hdr_t *buf = vbuf;
959 ASSERT(BUF_EMPTY(buf));
960 refcount_destroy(&buf->b_refcnt);
961 cv_destroy(&buf->b_cv);
962 mutex_destroy(&buf->b_freeze_lock);
963 arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
968 buf_dest(void *vbuf, void *unused)
970 arc_buf_t *buf = vbuf;
972 mutex_destroy(&buf->b_evict_lock);
973 rw_destroy(&buf->b_data_lock);
974 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
978 * Reclaim callback -- invoked when memory is low.
982 hdr_recl(void *unused)
984 dprintf("hdr_recl called\n");
986 * umem calls the reclaim func when we destroy the buf cache,
987 * which is after we do arc_fini().
990 cv_signal(&arc_reclaim_thr_cv);
997 uint64_t hsize = 1ULL << 12;
1001 * The hash table is big enough to fill all of physical memory
1002 * with an average 64K block size. The table will take up
1003 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
1005 while (hsize * 65536 < (uint64_t)physmem * PAGESIZE)
1008 buf_hash_table.ht_mask = hsize - 1;
1009 buf_hash_table.ht_table =
1010 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
1011 if (buf_hash_table.ht_table == NULL) {
1012 ASSERT(hsize > (1ULL << 8));
1017 hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
1018 0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
1019 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
1020 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
1022 for (i = 0; i < 256; i++)
1023 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
1024 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
1026 for (i = 0; i < BUF_LOCKS; i++) {
1027 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
1028 NULL, MUTEX_DEFAULT, NULL);
1032 #define ARC_MINTIME (hz>>4) /* 62 ms */
1035 arc_cksum_verify(arc_buf_t *buf)
1039 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1042 mutex_enter(&buf->b_hdr->b_freeze_lock);
1043 if (buf->b_hdr->b_freeze_cksum == NULL ||
1044 (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
1045 mutex_exit(&buf->b_hdr->b_freeze_lock);
1048 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
1049 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
1050 panic("buffer modified while frozen!");
1051 mutex_exit(&buf->b_hdr->b_freeze_lock);
1055 arc_cksum_equal(arc_buf_t *buf)
1060 mutex_enter(&buf->b_hdr->b_freeze_lock);
1061 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
1062 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
1063 mutex_exit(&buf->b_hdr->b_freeze_lock);
1069 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
1071 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
1074 mutex_enter(&buf->b_hdr->b_freeze_lock);
1075 if (buf->b_hdr->b_freeze_cksum != NULL) {
1076 mutex_exit(&buf->b_hdr->b_freeze_lock);
1079 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
1080 fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
1081 buf->b_hdr->b_freeze_cksum);
1082 mutex_exit(&buf->b_hdr->b_freeze_lock);
1085 #endif /* illumos */
1090 typedef struct procctl {
1098 arc_buf_unwatch(arc_buf_t *buf)
1105 ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data;
1106 ctl.prwatch.pr_size = 0;
1107 ctl.prwatch.pr_wflags = 0;
1108 result = write(arc_procfd, &ctl, sizeof (ctl));
1109 ASSERT3U(result, ==, sizeof (ctl));
1116 arc_buf_watch(arc_buf_t *buf)
1123 ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data;
1124 ctl.prwatch.pr_size = buf->b_hdr->b_size;
1125 ctl.prwatch.pr_wflags = WA_WRITE;
1126 result = write(arc_procfd, &ctl, sizeof (ctl));
1127 ASSERT3U(result, ==, sizeof (ctl));
1131 #endif /* illumos */
1134 arc_buf_thaw(arc_buf_t *buf)
1136 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1137 if (buf->b_hdr->b_state != arc_anon)
1138 panic("modifying non-anon buffer!");
1139 if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
1140 panic("modifying buffer while i/o in progress!");
1141 arc_cksum_verify(buf);
1144 mutex_enter(&buf->b_hdr->b_freeze_lock);
1145 if (buf->b_hdr->b_freeze_cksum != NULL) {
1146 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1147 buf->b_hdr->b_freeze_cksum = NULL;
1150 if (zfs_flags & ZFS_DEBUG_MODIFY) {
1151 if (buf->b_hdr->b_thawed)
1152 kmem_free(buf->b_hdr->b_thawed, 1);
1153 buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
1156 mutex_exit(&buf->b_hdr->b_freeze_lock);
1159 arc_buf_unwatch(buf);
1160 #endif /* illumos */
1164 arc_buf_freeze(arc_buf_t *buf)
1166 kmutex_t *hash_lock;
1168 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1171 hash_lock = HDR_LOCK(buf->b_hdr);
1172 mutex_enter(hash_lock);
1174 ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
1175 buf->b_hdr->b_state == arc_anon);
1176 arc_cksum_compute(buf, B_FALSE);
1177 mutex_exit(hash_lock);
1182 get_buf_info(arc_buf_hdr_t *ab, arc_state_t *state, list_t **list, kmutex_t **lock)
1184 uint64_t buf_hashid = buf_hash(ab->b_spa, &ab->b_dva, ab->b_birth);
1186 if (ab->b_type == ARC_BUFC_METADATA)
1187 buf_hashid &= (ARC_BUFC_NUMMETADATALISTS - 1);
1189 buf_hashid &= (ARC_BUFC_NUMDATALISTS - 1);
1190 buf_hashid += ARC_BUFC_NUMMETADATALISTS;
1193 *list = &state->arcs_lists[buf_hashid];
1194 *lock = ARCS_LOCK(state, buf_hashid);
1199 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1201 ASSERT(MUTEX_HELD(hash_lock));
1203 if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1204 (ab->b_state != arc_anon)) {
1205 uint64_t delta = ab->b_size * ab->b_datacnt;
1206 uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1210 get_buf_info(ab, ab->b_state, &list, &lock);
1211 ASSERT(!MUTEX_HELD(lock));
1213 ASSERT(list_link_active(&ab->b_arc_node));
1214 list_remove(list, ab);
1215 if (GHOST_STATE(ab->b_state)) {
1216 ASSERT0(ab->b_datacnt);
1217 ASSERT3P(ab->b_buf, ==, NULL);
1221 ASSERT3U(*size, >=, delta);
1222 atomic_add_64(size, -delta);
1224 /* remove the prefetch flag if we get a reference */
1225 if (ab->b_flags & ARC_PREFETCH)
1226 ab->b_flags &= ~ARC_PREFETCH;
1231 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1234 arc_state_t *state = ab->b_state;
1236 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1237 ASSERT(!GHOST_STATE(state));
1239 if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1240 (state != arc_anon)) {
1241 uint64_t *size = &state->arcs_lsize[ab->b_type];
1245 get_buf_info(ab, state, &list, &lock);
1246 ASSERT(!MUTEX_HELD(lock));
1248 ASSERT(!list_link_active(&ab->b_arc_node));
1249 list_insert_head(list, ab);
1250 ASSERT(ab->b_datacnt > 0);
1251 atomic_add_64(size, ab->b_size * ab->b_datacnt);
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;
1270 ASSERT(MUTEX_HELD(hash_lock));
1271 ASSERT(new_state != old_state);
1272 ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1273 ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1274 ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1276 from_delta = to_delta = ab->b_datacnt * ab->b_size;
1279 * If this buffer is evictable, transfer it from the
1280 * old state list to the new state list.
1283 if (old_state != arc_anon) {
1285 uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1287 get_buf_info(ab, old_state, &list, &lock);
1288 use_mutex = !MUTEX_HELD(lock);
1292 ASSERT(list_link_active(&ab->b_arc_node));
1293 list_remove(list, ab);
1296 * If prefetching out of the ghost cache,
1297 * we will have a non-zero datacnt.
1299 if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1300 /* ghost elements have a ghost size */
1301 ASSERT(ab->b_buf == NULL);
1302 from_delta = ab->b_size;
1304 ASSERT3U(*size, >=, from_delta);
1305 atomic_add_64(size, -from_delta);
1310 if (new_state != arc_anon) {
1312 uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1314 get_buf_info(ab, new_state, &list, &lock);
1315 use_mutex = !MUTEX_HELD(lock);
1319 list_insert_head(list, ab);
1321 /* ghost elements have a ghost size */
1322 if (GHOST_STATE(new_state)) {
1323 ASSERT(ab->b_datacnt == 0);
1324 ASSERT(ab->b_buf == NULL);
1325 to_delta = ab->b_size;
1327 atomic_add_64(size, to_delta);
1334 ASSERT(!BUF_EMPTY(ab));
1335 if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1336 buf_hash_remove(ab);
1338 /* adjust state sizes */
1340 atomic_add_64(&new_state->arcs_size, to_delta);
1342 ASSERT3U(old_state->arcs_size, >=, from_delta);
1343 atomic_add_64(&old_state->arcs_size, -from_delta);
1345 ab->b_state = new_state;
1347 /* adjust l2arc hdr stats */
1348 if (new_state == arc_l2c_only)
1349 l2arc_hdr_stat_add();
1350 else if (old_state == arc_l2c_only)
1351 l2arc_hdr_stat_remove();
1355 arc_space_consume(uint64_t space, arc_space_type_t type)
1357 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1360 case ARC_SPACE_DATA:
1361 ARCSTAT_INCR(arcstat_data_size, space);
1363 case ARC_SPACE_OTHER:
1364 ARCSTAT_INCR(arcstat_other_size, space);
1366 case ARC_SPACE_HDRS:
1367 ARCSTAT_INCR(arcstat_hdr_size, space);
1369 case ARC_SPACE_L2HDRS:
1370 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1374 atomic_add_64(&arc_meta_used, space);
1375 atomic_add_64(&arc_size, space);
1379 arc_space_return(uint64_t space, arc_space_type_t type)
1381 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1384 case ARC_SPACE_DATA:
1385 ARCSTAT_INCR(arcstat_data_size, -space);
1387 case ARC_SPACE_OTHER:
1388 ARCSTAT_INCR(arcstat_other_size, -space);
1390 case ARC_SPACE_HDRS:
1391 ARCSTAT_INCR(arcstat_hdr_size, -space);
1393 case ARC_SPACE_L2HDRS:
1394 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1398 ASSERT(arc_meta_used >= space);
1399 if (arc_meta_max < arc_meta_used)
1400 arc_meta_max = arc_meta_used;
1401 atomic_add_64(&arc_meta_used, -space);
1402 ASSERT(arc_size >= space);
1403 atomic_add_64(&arc_size, -space);
1407 arc_data_buf_alloc(uint64_t size)
1409 if (arc_evict_needed(ARC_BUFC_DATA))
1410 cv_signal(&arc_reclaim_thr_cv);
1411 atomic_add_64(&arc_size, size);
1412 return (zio_data_buf_alloc(size));
1416 arc_data_buf_free(void *buf, uint64_t size)
1418 zio_data_buf_free(buf, size);
1419 ASSERT(arc_size >= size);
1420 atomic_add_64(&arc_size, -size);
1424 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1429 ASSERT3U(size, >, 0);
1430 hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1431 ASSERT(BUF_EMPTY(hdr));
1434 hdr->b_spa = spa_load_guid(spa);
1435 hdr->b_state = arc_anon;
1436 hdr->b_arc_access = 0;
1437 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1440 buf->b_efunc = NULL;
1441 buf->b_private = NULL;
1444 arc_get_data_buf(buf);
1447 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1448 (void) refcount_add(&hdr->b_refcnt, tag);
1453 static char *arc_onloan_tag = "onloan";
1456 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1457 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1458 * buffers must be returned to the arc before they can be used by the DMU or
1462 arc_loan_buf(spa_t *spa, int size)
1466 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1468 atomic_add_64(&arc_loaned_bytes, size);
1473 * Return a loaned arc buffer to the arc.
1476 arc_return_buf(arc_buf_t *buf, void *tag)
1478 arc_buf_hdr_t *hdr = buf->b_hdr;
1480 ASSERT(buf->b_data != NULL);
1481 (void) refcount_add(&hdr->b_refcnt, tag);
1482 (void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1484 atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1487 /* Detach an arc_buf from a dbuf (tag) */
1489 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1493 ASSERT(buf->b_data != NULL);
1495 (void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1496 (void) refcount_remove(&hdr->b_refcnt, tag);
1497 buf->b_efunc = NULL;
1498 buf->b_private = NULL;
1500 atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1504 arc_buf_clone(arc_buf_t *from)
1507 arc_buf_hdr_t *hdr = from->b_hdr;
1508 uint64_t size = hdr->b_size;
1510 ASSERT(hdr->b_state != arc_anon);
1512 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1515 buf->b_efunc = NULL;
1516 buf->b_private = NULL;
1517 buf->b_next = hdr->b_buf;
1519 arc_get_data_buf(buf);
1520 bcopy(from->b_data, buf->b_data, size);
1521 hdr->b_datacnt += 1;
1526 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1529 kmutex_t *hash_lock;
1532 * Check to see if this buffer is evicted. Callers
1533 * must verify b_data != NULL to know if the add_ref
1536 mutex_enter(&buf->b_evict_lock);
1537 if (buf->b_data == NULL) {
1538 mutex_exit(&buf->b_evict_lock);
1541 hash_lock = HDR_LOCK(buf->b_hdr);
1542 mutex_enter(hash_lock);
1544 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1545 mutex_exit(&buf->b_evict_lock);
1547 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1548 add_reference(hdr, hash_lock, tag);
1549 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1550 arc_access(hdr, hash_lock);
1551 mutex_exit(hash_lock);
1552 ARCSTAT_BUMP(arcstat_hits);
1553 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1554 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1555 data, metadata, hits);
1559 * Free the arc data buffer. If it is an l2arc write in progress,
1560 * the buffer is placed on l2arc_free_on_write to be freed later.
1563 arc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t))
1565 arc_buf_hdr_t *hdr = buf->b_hdr;
1567 if (HDR_L2_WRITING(hdr)) {
1568 l2arc_data_free_t *df;
1569 df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
1570 df->l2df_data = buf->b_data;
1571 df->l2df_size = hdr->b_size;
1572 df->l2df_func = free_func;
1573 mutex_enter(&l2arc_free_on_write_mtx);
1574 list_insert_head(l2arc_free_on_write, df);
1575 mutex_exit(&l2arc_free_on_write_mtx);
1576 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1578 free_func(buf->b_data, hdr->b_size);
1583 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1587 /* free up data associated with the buf */
1589 arc_state_t *state = buf->b_hdr->b_state;
1590 uint64_t size = buf->b_hdr->b_size;
1591 arc_buf_contents_t type = buf->b_hdr->b_type;
1593 arc_cksum_verify(buf);
1595 arc_buf_unwatch(buf);
1596 #endif /* illumos */
1599 if (type == ARC_BUFC_METADATA) {
1600 arc_buf_data_free(buf, zio_buf_free);
1601 arc_space_return(size, ARC_SPACE_DATA);
1603 ASSERT(type == ARC_BUFC_DATA);
1604 arc_buf_data_free(buf, zio_data_buf_free);
1605 ARCSTAT_INCR(arcstat_data_size, -size);
1606 atomic_add_64(&arc_size, -size);
1609 if (list_link_active(&buf->b_hdr->b_arc_node)) {
1610 uint64_t *cnt = &state->arcs_lsize[type];
1612 ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1613 ASSERT(state != arc_anon);
1615 ASSERT3U(*cnt, >=, size);
1616 atomic_add_64(cnt, -size);
1618 ASSERT3U(state->arcs_size, >=, size);
1619 atomic_add_64(&state->arcs_size, -size);
1621 ASSERT(buf->b_hdr->b_datacnt > 0);
1622 buf->b_hdr->b_datacnt -= 1;
1625 /* only remove the buf if requested */
1629 /* remove the buf from the hdr list */
1630 for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1632 *bufp = buf->b_next;
1635 ASSERT(buf->b_efunc == NULL);
1637 /* clean up the buf */
1639 kmem_cache_free(buf_cache, buf);
1643 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1645 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1646 ASSERT3P(hdr->b_state, ==, arc_anon);
1647 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1648 l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1650 if (l2hdr != NULL) {
1651 boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1653 * To prevent arc_free() and l2arc_evict() from
1654 * attempting to free the same buffer at the same time,
1655 * a FREE_IN_PROGRESS flag is given to arc_free() to
1656 * give it priority. l2arc_evict() can't destroy this
1657 * header while we are waiting on l2arc_buflist_mtx.
1659 * The hdr may be removed from l2ad_buflist before we
1660 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1662 if (!buflist_held) {
1663 mutex_enter(&l2arc_buflist_mtx);
1664 l2hdr = hdr->b_l2hdr;
1667 if (l2hdr != NULL) {
1668 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1669 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1670 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1671 if (hdr->b_state == arc_l2c_only)
1672 l2arc_hdr_stat_remove();
1673 hdr->b_l2hdr = NULL;
1677 mutex_exit(&l2arc_buflist_mtx);
1680 if (!BUF_EMPTY(hdr)) {
1681 ASSERT(!HDR_IN_HASH_TABLE(hdr));
1682 buf_discard_identity(hdr);
1684 while (hdr->b_buf) {
1685 arc_buf_t *buf = hdr->b_buf;
1688 mutex_enter(&arc_eviction_mtx);
1689 mutex_enter(&buf->b_evict_lock);
1690 ASSERT(buf->b_hdr != NULL);
1691 arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1692 hdr->b_buf = buf->b_next;
1693 buf->b_hdr = &arc_eviction_hdr;
1694 buf->b_next = arc_eviction_list;
1695 arc_eviction_list = buf;
1696 mutex_exit(&buf->b_evict_lock);
1697 mutex_exit(&arc_eviction_mtx);
1699 arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1702 if (hdr->b_freeze_cksum != NULL) {
1703 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1704 hdr->b_freeze_cksum = NULL;
1706 if (hdr->b_thawed) {
1707 kmem_free(hdr->b_thawed, 1);
1708 hdr->b_thawed = NULL;
1711 ASSERT(!list_link_active(&hdr->b_arc_node));
1712 ASSERT3P(hdr->b_hash_next, ==, NULL);
1713 ASSERT3P(hdr->b_acb, ==, NULL);
1714 kmem_cache_free(hdr_cache, hdr);
1718 arc_buf_free(arc_buf_t *buf, void *tag)
1720 arc_buf_hdr_t *hdr = buf->b_hdr;
1721 int hashed = hdr->b_state != arc_anon;
1723 ASSERT(buf->b_efunc == NULL);
1724 ASSERT(buf->b_data != NULL);
1727 kmutex_t *hash_lock = HDR_LOCK(hdr);
1729 mutex_enter(hash_lock);
1731 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1733 (void) remove_reference(hdr, hash_lock, tag);
1734 if (hdr->b_datacnt > 1) {
1735 arc_buf_destroy(buf, FALSE, TRUE);
1737 ASSERT(buf == hdr->b_buf);
1738 ASSERT(buf->b_efunc == NULL);
1739 hdr->b_flags |= ARC_BUF_AVAILABLE;
1741 mutex_exit(hash_lock);
1742 } else if (HDR_IO_IN_PROGRESS(hdr)) {
1745 * We are in the middle of an async write. Don't destroy
1746 * this buffer unless the write completes before we finish
1747 * decrementing the reference count.
1749 mutex_enter(&arc_eviction_mtx);
1750 (void) remove_reference(hdr, NULL, tag);
1751 ASSERT(refcount_is_zero(&hdr->b_refcnt));
1752 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1753 mutex_exit(&arc_eviction_mtx);
1755 arc_hdr_destroy(hdr);
1757 if (remove_reference(hdr, NULL, tag) > 0)
1758 arc_buf_destroy(buf, FALSE, TRUE);
1760 arc_hdr_destroy(hdr);
1765 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1767 arc_buf_hdr_t *hdr = buf->b_hdr;
1768 kmutex_t *hash_lock = HDR_LOCK(hdr);
1769 int no_callback = (buf->b_efunc == NULL);
1771 if (hdr->b_state == arc_anon) {
1772 ASSERT(hdr->b_datacnt == 1);
1773 arc_buf_free(buf, tag);
1774 return (no_callback);
1777 mutex_enter(hash_lock);
1779 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1780 ASSERT(hdr->b_state != arc_anon);
1781 ASSERT(buf->b_data != NULL);
1783 (void) remove_reference(hdr, hash_lock, tag);
1784 if (hdr->b_datacnt > 1) {
1786 arc_buf_destroy(buf, FALSE, TRUE);
1787 } else if (no_callback) {
1788 ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1789 ASSERT(buf->b_efunc == NULL);
1790 hdr->b_flags |= ARC_BUF_AVAILABLE;
1792 ASSERT(no_callback || hdr->b_datacnt > 1 ||
1793 refcount_is_zero(&hdr->b_refcnt));
1794 mutex_exit(hash_lock);
1795 return (no_callback);
1799 arc_buf_size(arc_buf_t *buf)
1801 return (buf->b_hdr->b_size);
1805 * Evict buffers from list until we've removed the specified number of
1806 * bytes. Move the removed buffers to the appropriate evict state.
1807 * If the recycle flag is set, then attempt to "recycle" a buffer:
1808 * - look for a buffer to evict that is `bytes' long.
1809 * - return the data block from this buffer rather than freeing it.
1810 * This flag is used by callers that are trying to make space for a
1811 * new buffer in a full arc cache.
1813 * This function makes a "best effort". It skips over any buffers
1814 * it can't get a hash_lock on, and so may not catch all candidates.
1815 * It may also return without evicting as much space as requested.
1818 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1819 arc_buf_contents_t type)
1821 arc_state_t *evicted_state;
1822 uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1823 int64_t bytes_remaining;
1824 arc_buf_hdr_t *ab, *ab_prev = NULL;
1825 list_t *evicted_list, *list, *evicted_list_start, *list_start;
1826 kmutex_t *lock, *evicted_lock;
1827 kmutex_t *hash_lock;
1828 boolean_t have_lock;
1829 void *stolen = NULL;
1830 static int evict_metadata_offset, evict_data_offset;
1831 int i, idx, offset, list_count, count;
1833 ASSERT(state == arc_mru || state == arc_mfu);
1835 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1837 if (type == ARC_BUFC_METADATA) {
1839 list_count = ARC_BUFC_NUMMETADATALISTS;
1840 list_start = &state->arcs_lists[0];
1841 evicted_list_start = &evicted_state->arcs_lists[0];
1842 idx = evict_metadata_offset;
1844 offset = ARC_BUFC_NUMMETADATALISTS;
1845 list_start = &state->arcs_lists[offset];
1846 evicted_list_start = &evicted_state->arcs_lists[offset];
1847 list_count = ARC_BUFC_NUMDATALISTS;
1848 idx = evict_data_offset;
1850 bytes_remaining = evicted_state->arcs_lsize[type];
1854 list = &list_start[idx];
1855 evicted_list = &evicted_list_start[idx];
1856 lock = ARCS_LOCK(state, (offset + idx));
1857 evicted_lock = ARCS_LOCK(evicted_state, (offset + idx));
1860 mutex_enter(evicted_lock);
1862 for (ab = list_tail(list); ab; ab = ab_prev) {
1863 ab_prev = list_prev(list, ab);
1864 bytes_remaining -= (ab->b_size * ab->b_datacnt);
1865 /* prefetch buffers have a minimum lifespan */
1866 if (HDR_IO_IN_PROGRESS(ab) ||
1867 (spa && ab->b_spa != spa) ||
1868 (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1869 ddi_get_lbolt() - ab->b_arc_access <
1870 arc_min_prefetch_lifespan)) {
1874 /* "lookahead" for better eviction candidate */
1875 if (recycle && ab->b_size != bytes &&
1876 ab_prev && ab_prev->b_size == bytes)
1878 hash_lock = HDR_LOCK(ab);
1879 have_lock = MUTEX_HELD(hash_lock);
1880 if (have_lock || mutex_tryenter(hash_lock)) {
1881 ASSERT0(refcount_count(&ab->b_refcnt));
1882 ASSERT(ab->b_datacnt > 0);
1884 arc_buf_t *buf = ab->b_buf;
1885 if (!mutex_tryenter(&buf->b_evict_lock)) {
1890 bytes_evicted += ab->b_size;
1891 if (recycle && ab->b_type == type &&
1892 ab->b_size == bytes &&
1893 !HDR_L2_WRITING(ab)) {
1894 stolen = buf->b_data;
1899 mutex_enter(&arc_eviction_mtx);
1900 arc_buf_destroy(buf,
1901 buf->b_data == stolen, FALSE);
1902 ab->b_buf = buf->b_next;
1903 buf->b_hdr = &arc_eviction_hdr;
1904 buf->b_next = arc_eviction_list;
1905 arc_eviction_list = buf;
1906 mutex_exit(&arc_eviction_mtx);
1907 mutex_exit(&buf->b_evict_lock);
1909 mutex_exit(&buf->b_evict_lock);
1910 arc_buf_destroy(buf,
1911 buf->b_data == stolen, TRUE);
1916 ARCSTAT_INCR(arcstat_evict_l2_cached,
1919 if (l2arc_write_eligible(ab->b_spa, ab)) {
1920 ARCSTAT_INCR(arcstat_evict_l2_eligible,
1924 arcstat_evict_l2_ineligible,
1929 if (ab->b_datacnt == 0) {
1930 arc_change_state(evicted_state, ab, hash_lock);
1931 ASSERT(HDR_IN_HASH_TABLE(ab));
1932 ab->b_flags |= ARC_IN_HASH_TABLE;
1933 ab->b_flags &= ~ARC_BUF_AVAILABLE;
1934 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1937 mutex_exit(hash_lock);
1938 if (bytes >= 0 && bytes_evicted >= bytes)
1940 if (bytes_remaining > 0) {
1941 mutex_exit(evicted_lock);
1943 idx = ((idx + 1) & (list_count - 1));
1952 mutex_exit(evicted_lock);
1955 idx = ((idx + 1) & (list_count - 1));
1958 if (bytes_evicted < bytes) {
1959 if (count < list_count)
1962 dprintf("only evicted %lld bytes from %x",
1963 (longlong_t)bytes_evicted, state);
1965 if (type == ARC_BUFC_METADATA)
1966 evict_metadata_offset = idx;
1968 evict_data_offset = idx;
1971 ARCSTAT_INCR(arcstat_evict_skip, skipped);
1974 ARCSTAT_INCR(arcstat_mutex_miss, missed);
1977 * We have just evicted some date into the ghost state, make
1978 * sure we also adjust the ghost state size if necessary.
1981 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
1982 int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
1983 arc_mru_ghost->arcs_size - arc_c;
1985 if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
1987 MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
1988 arc_evict_ghost(arc_mru_ghost, 0, todelete);
1989 } else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
1990 int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
1991 arc_mru_ghost->arcs_size +
1992 arc_mfu_ghost->arcs_size - arc_c);
1993 arc_evict_ghost(arc_mfu_ghost, 0, todelete);
1997 ARCSTAT_BUMP(arcstat_stolen);
2003 * Remove buffers from list until we've removed the specified number of
2004 * bytes. Destroy the buffers that are removed.
2007 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
2009 arc_buf_hdr_t *ab, *ab_prev;
2010 arc_buf_hdr_t marker = { 0 };
2011 list_t *list, *list_start;
2012 kmutex_t *hash_lock, *lock;
2013 uint64_t bytes_deleted = 0;
2014 uint64_t bufs_skipped = 0;
2015 static int evict_offset;
2016 int list_count, idx = evict_offset;
2017 int offset, count = 0;
2019 ASSERT(GHOST_STATE(state));
2022 * data lists come after metadata lists
2024 list_start = &state->arcs_lists[ARC_BUFC_NUMMETADATALISTS];
2025 list_count = ARC_BUFC_NUMDATALISTS;
2026 offset = ARC_BUFC_NUMMETADATALISTS;
2029 list = &list_start[idx];
2030 lock = ARCS_LOCK(state, idx + offset);
2033 for (ab = list_tail(list); ab; ab = ab_prev) {
2034 ab_prev = list_prev(list, ab);
2035 if (spa && ab->b_spa != spa)
2038 /* ignore markers */
2042 hash_lock = HDR_LOCK(ab);
2043 /* caller may be trying to modify this buffer, skip it */
2044 if (MUTEX_HELD(hash_lock))
2046 if (mutex_tryenter(hash_lock)) {
2047 ASSERT(!HDR_IO_IN_PROGRESS(ab));
2048 ASSERT(ab->b_buf == NULL);
2049 ARCSTAT_BUMP(arcstat_deleted);
2050 bytes_deleted += ab->b_size;
2052 if (ab->b_l2hdr != NULL) {
2054 * This buffer is cached on the 2nd Level ARC;
2055 * don't destroy the header.
2057 arc_change_state(arc_l2c_only, ab, hash_lock);
2058 mutex_exit(hash_lock);
2060 arc_change_state(arc_anon, ab, hash_lock);
2061 mutex_exit(hash_lock);
2062 arc_hdr_destroy(ab);
2065 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
2066 if (bytes >= 0 && bytes_deleted >= bytes)
2068 } else if (bytes < 0) {
2070 * Insert a list marker and then wait for the
2071 * hash lock to become available. Once its
2072 * available, restart from where we left off.
2074 list_insert_after(list, ab, &marker);
2076 mutex_enter(hash_lock);
2077 mutex_exit(hash_lock);
2079 ab_prev = list_prev(list, &marker);
2080 list_remove(list, &marker);
2085 idx = ((idx + 1) & (ARC_BUFC_NUMDATALISTS - 1));
2088 if (count < list_count)
2092 if ((uintptr_t)list > (uintptr_t)&state->arcs_lists[ARC_BUFC_NUMMETADATALISTS] &&
2093 (bytes < 0 || bytes_deleted < bytes)) {
2094 list_start = &state->arcs_lists[0];
2095 list_count = ARC_BUFC_NUMMETADATALISTS;
2101 ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
2105 if (bytes_deleted < bytes)
2106 dprintf("only deleted %lld bytes from %p",
2107 (longlong_t)bytes_deleted, state);
2113 int64_t adjustment, delta;
2119 adjustment = MIN((int64_t)(arc_size - arc_c),
2120 (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
2123 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
2124 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
2125 (void) arc_evict(arc_mru, 0, delta, FALSE, ARC_BUFC_DATA);
2126 adjustment -= delta;
2129 if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2130 delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
2131 (void) arc_evict(arc_mru, 0, delta, FALSE,
2139 adjustment = arc_size - arc_c;
2141 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
2142 delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
2143 (void) arc_evict(arc_mfu, 0, delta, FALSE, ARC_BUFC_DATA);
2144 adjustment -= delta;
2147 if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
2148 int64_t delta = MIN(adjustment,
2149 arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
2150 (void) arc_evict(arc_mfu, 0, delta, FALSE,
2155 * Adjust ghost lists
2158 adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
2160 if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
2161 delta = MIN(arc_mru_ghost->arcs_size, adjustment);
2162 arc_evict_ghost(arc_mru_ghost, 0, delta);
2166 arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
2168 if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
2169 delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
2170 arc_evict_ghost(arc_mfu_ghost, 0, delta);
2175 arc_do_user_evicts(void)
2177 static arc_buf_t *tmp_arc_eviction_list;
2180 * Move list over to avoid LOR
2183 mutex_enter(&arc_eviction_mtx);
2184 tmp_arc_eviction_list = arc_eviction_list;
2185 arc_eviction_list = NULL;
2186 mutex_exit(&arc_eviction_mtx);
2188 while (tmp_arc_eviction_list != NULL) {
2189 arc_buf_t *buf = tmp_arc_eviction_list;
2190 tmp_arc_eviction_list = buf->b_next;
2191 mutex_enter(&buf->b_evict_lock);
2193 mutex_exit(&buf->b_evict_lock);
2195 if (buf->b_efunc != NULL)
2196 VERIFY(buf->b_efunc(buf) == 0);
2198 buf->b_efunc = NULL;
2199 buf->b_private = NULL;
2200 kmem_cache_free(buf_cache, buf);
2203 if (arc_eviction_list != NULL)
2208 * Flush all *evictable* data from the cache for the given spa.
2209 * NOTE: this will not touch "active" (i.e. referenced) data.
2212 arc_flush(spa_t *spa)
2217 guid = spa_load_guid(spa);
2219 while (arc_mru->arcs_lsize[ARC_BUFC_DATA]) {
2220 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
2224 while (arc_mru->arcs_lsize[ARC_BUFC_METADATA]) {
2225 (void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
2229 while (arc_mfu->arcs_lsize[ARC_BUFC_DATA]) {
2230 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
2234 while (arc_mfu->arcs_lsize[ARC_BUFC_METADATA]) {
2235 (void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
2240 arc_evict_ghost(arc_mru_ghost, guid, -1);
2241 arc_evict_ghost(arc_mfu_ghost, guid, -1);
2243 mutex_enter(&arc_reclaim_thr_lock);
2244 arc_do_user_evicts();
2245 mutex_exit(&arc_reclaim_thr_lock);
2246 ASSERT(spa || arc_eviction_list == NULL);
2252 if (arc_c > arc_c_min) {
2256 to_free = arc_c >> arc_shrink_shift;
2258 to_free = arc_c >> arc_shrink_shift;
2260 if (arc_c > arc_c_min + to_free)
2261 atomic_add_64(&arc_c, -to_free);
2265 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
2266 if (arc_c > arc_size)
2267 arc_c = MAX(arc_size, arc_c_min);
2269 arc_p = (arc_c >> 1);
2270 ASSERT(arc_c >= arc_c_min);
2271 ASSERT((int64_t)arc_p >= 0);
2274 if (arc_size > arc_c)
2278 static int needfree = 0;
2281 arc_reclaim_needed(void)
2290 * Cooperate with pagedaemon when it's time for it to scan
2291 * and reclaim some pages.
2293 if (vm_paging_needed())
2298 * take 'desfree' extra pages, so we reclaim sooner, rather than later
2303 * check that we're out of range of the pageout scanner. It starts to
2304 * schedule paging if freemem is less than lotsfree and needfree.
2305 * lotsfree is the high-water mark for pageout, and needfree is the
2306 * number of needed free pages. We add extra pages here to make sure
2307 * the scanner doesn't start up while we're freeing memory.
2309 if (freemem < lotsfree + needfree + extra)
2313 * check to make sure that swapfs has enough space so that anon
2314 * reservations can still succeed. anon_resvmem() checks that the
2315 * availrmem is greater than swapfs_minfree, and the number of reserved
2316 * swap pages. We also add a bit of extra here just to prevent
2317 * circumstances from getting really dire.
2319 if (availrmem < swapfs_minfree + swapfs_reserve + extra)
2324 * If we're on an i386 platform, it's possible that we'll exhaust the
2325 * kernel heap space before we ever run out of available physical
2326 * memory. Most checks of the size of the heap_area compare against
2327 * tune.t_minarmem, which is the minimum available real memory that we
2328 * can have in the system. However, this is generally fixed at 25 pages
2329 * which is so low that it's useless. In this comparison, we seek to
2330 * calculate the total heap-size, and reclaim if more than 3/4ths of the
2331 * heap is allocated. (Or, in the calculation, if less than 1/4th is
2334 if (btop(vmem_size(heap_arena, VMEM_FREE)) <
2335 (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
2339 if (kmem_used() > (kmem_size() * 3) / 4)
2344 if (spa_get_random(100) == 0)
2350 extern kmem_cache_t *zio_buf_cache[];
2351 extern kmem_cache_t *zio_data_buf_cache[];
2354 arc_kmem_reap_now(arc_reclaim_strategy_t strat)
2357 kmem_cache_t *prev_cache = NULL;
2358 kmem_cache_t *prev_data_cache = NULL;
2361 if (arc_meta_used >= arc_meta_limit) {
2363 * We are exceeding our meta-data cache limit.
2364 * Purge some DNLC entries to release holds on meta-data.
2366 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
2370 * Reclaim unused memory from all kmem caches.
2377 * An aggressive reclamation will shrink the cache size as well as
2378 * reap free buffers from the arc kmem caches.
2380 if (strat == ARC_RECLAIM_AGGR)
2383 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2384 if (zio_buf_cache[i] != prev_cache) {
2385 prev_cache = zio_buf_cache[i];
2386 kmem_cache_reap_now(zio_buf_cache[i]);
2388 if (zio_data_buf_cache[i] != prev_data_cache) {
2389 prev_data_cache = zio_data_buf_cache[i];
2390 kmem_cache_reap_now(zio_data_buf_cache[i]);
2393 kmem_cache_reap_now(buf_cache);
2394 kmem_cache_reap_now(hdr_cache);
2398 arc_reclaim_thread(void *dummy __unused)
2400 clock_t growtime = 0;
2401 arc_reclaim_strategy_t last_reclaim = ARC_RECLAIM_CONS;
2404 CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2406 mutex_enter(&arc_reclaim_thr_lock);
2407 while (arc_thread_exit == 0) {
2408 if (arc_reclaim_needed()) {
2411 if (last_reclaim == ARC_RECLAIM_CONS) {
2412 last_reclaim = ARC_RECLAIM_AGGR;
2414 last_reclaim = ARC_RECLAIM_CONS;
2418 last_reclaim = ARC_RECLAIM_AGGR;
2422 /* reset the growth delay for every reclaim */
2423 growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
2425 if (needfree && last_reclaim == ARC_RECLAIM_CONS) {
2427 * If needfree is TRUE our vm_lowmem hook
2428 * was called and in that case we must free some
2429 * memory, so switch to aggressive mode.
2432 last_reclaim = ARC_RECLAIM_AGGR;
2434 arc_kmem_reap_now(last_reclaim);
2437 } else if (arc_no_grow && ddi_get_lbolt() >= growtime) {
2438 arc_no_grow = FALSE;
2443 if (arc_eviction_list != NULL)
2444 arc_do_user_evicts();
2453 /* block until needed, or one second, whichever is shorter */
2454 CALLB_CPR_SAFE_BEGIN(&cpr);
2455 (void) cv_timedwait(&arc_reclaim_thr_cv,
2456 &arc_reclaim_thr_lock, hz);
2457 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2460 arc_thread_exit = 0;
2461 cv_broadcast(&arc_reclaim_thr_cv);
2462 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_thr_lock */
2467 * Adapt arc info given the number of bytes we are trying to add and
2468 * the state that we are comming from. This function is only called
2469 * when we are adding new content to the cache.
2472 arc_adapt(int bytes, arc_state_t *state)
2475 uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
2477 if (state == arc_l2c_only)
2482 * Adapt the target size of the MRU list:
2483 * - if we just hit in the MRU ghost list, then increase
2484 * the target size of the MRU list.
2485 * - if we just hit in the MFU ghost list, then increase
2486 * the target size of the MFU list by decreasing the
2487 * target size of the MRU list.
2489 if (state == arc_mru_ghost) {
2490 mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2491 1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2492 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2494 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2495 } else if (state == arc_mfu_ghost) {
2498 mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2499 1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2500 mult = MIN(mult, 10);
2502 delta = MIN(bytes * mult, arc_p);
2503 arc_p = MAX(arc_p_min, arc_p - delta);
2505 ASSERT((int64_t)arc_p >= 0);
2507 if (arc_reclaim_needed()) {
2508 cv_signal(&arc_reclaim_thr_cv);
2515 if (arc_c >= arc_c_max)
2519 * If we're within (2 * maxblocksize) bytes of the target
2520 * cache size, increment the target cache size
2522 if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2523 atomic_add_64(&arc_c, (int64_t)bytes);
2524 if (arc_c > arc_c_max)
2526 else if (state == arc_anon)
2527 atomic_add_64(&arc_p, (int64_t)bytes);
2531 ASSERT((int64_t)arc_p >= 0);
2535 * Check if the cache has reached its limits and eviction is required
2539 arc_evict_needed(arc_buf_contents_t type)
2541 if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2547 * If zio data pages are being allocated out of a separate heap segment,
2548 * then enforce that the size of available vmem for this area remains
2549 * above about 1/32nd free.
2551 if (type == ARC_BUFC_DATA && zio_arena != NULL &&
2552 vmem_size(zio_arena, VMEM_FREE) <
2553 (vmem_size(zio_arena, VMEM_ALLOC) >> 5))
2558 if (arc_reclaim_needed())
2561 return (arc_size > arc_c);
2565 * The buffer, supplied as the first argument, needs a data block.
2566 * So, if we are at cache max, determine which cache should be victimized.
2567 * We have the following cases:
2569 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2570 * In this situation if we're out of space, but the resident size of the MFU is
2571 * under the limit, victimize the MFU cache to satisfy this insertion request.
2573 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2574 * Here, we've used up all of the available space for the MRU, so we need to
2575 * evict from our own cache instead. Evict from the set of resident MRU
2578 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2579 * c minus p represents the MFU space in the cache, since p is the size of the
2580 * cache that is dedicated to the MRU. In this situation there's still space on
2581 * the MFU side, so the MRU side needs to be victimized.
2583 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2584 * MFU's resident set is consuming more space than it has been allotted. In
2585 * this situation, we must victimize our own cache, the MFU, for this insertion.
2588 arc_get_data_buf(arc_buf_t *buf)
2590 arc_state_t *state = buf->b_hdr->b_state;
2591 uint64_t size = buf->b_hdr->b_size;
2592 arc_buf_contents_t type = buf->b_hdr->b_type;
2594 arc_adapt(size, state);
2597 * We have not yet reached cache maximum size,
2598 * just allocate a new buffer.
2600 if (!arc_evict_needed(type)) {
2601 if (type == ARC_BUFC_METADATA) {
2602 buf->b_data = zio_buf_alloc(size);
2603 arc_space_consume(size, ARC_SPACE_DATA);
2605 ASSERT(type == ARC_BUFC_DATA);
2606 buf->b_data = zio_data_buf_alloc(size);
2607 ARCSTAT_INCR(arcstat_data_size, size);
2608 atomic_add_64(&arc_size, size);
2614 * If we are prefetching from the mfu ghost list, this buffer
2615 * will end up on the mru list; so steal space from there.
2617 if (state == arc_mfu_ghost)
2618 state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2619 else if (state == arc_mru_ghost)
2622 if (state == arc_mru || state == arc_anon) {
2623 uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2624 state = (arc_mfu->arcs_lsize[type] >= size &&
2625 arc_p > mru_used) ? arc_mfu : arc_mru;
2628 uint64_t mfu_space = arc_c - arc_p;
2629 state = (arc_mru->arcs_lsize[type] >= size &&
2630 mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2632 if ((buf->b_data = arc_evict(state, 0, size, TRUE, type)) == NULL) {
2633 if (type == ARC_BUFC_METADATA) {
2634 buf->b_data = zio_buf_alloc(size);
2635 arc_space_consume(size, ARC_SPACE_DATA);
2637 ASSERT(type == ARC_BUFC_DATA);
2638 buf->b_data = zio_data_buf_alloc(size);
2639 ARCSTAT_INCR(arcstat_data_size, size);
2640 atomic_add_64(&arc_size, size);
2642 ARCSTAT_BUMP(arcstat_recycle_miss);
2644 ASSERT(buf->b_data != NULL);
2647 * Update the state size. Note that ghost states have a
2648 * "ghost size" and so don't need to be updated.
2650 if (!GHOST_STATE(buf->b_hdr->b_state)) {
2651 arc_buf_hdr_t *hdr = buf->b_hdr;
2653 atomic_add_64(&hdr->b_state->arcs_size, size);
2654 if (list_link_active(&hdr->b_arc_node)) {
2655 ASSERT(refcount_is_zero(&hdr->b_refcnt));
2656 atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2659 * If we are growing the cache, and we are adding anonymous
2660 * data, and we have outgrown arc_p, update arc_p
2662 if (arc_size < arc_c && hdr->b_state == arc_anon &&
2663 arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2664 arc_p = MIN(arc_c, arc_p + size);
2666 ARCSTAT_BUMP(arcstat_allocated);
2670 * This routine is called whenever a buffer is accessed.
2671 * NOTE: the hash lock is dropped in this function.
2674 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2678 ASSERT(MUTEX_HELD(hash_lock));
2680 if (buf->b_state == arc_anon) {
2682 * This buffer is not in the cache, and does not
2683 * appear in our "ghost" list. Add the new buffer
2687 ASSERT(buf->b_arc_access == 0);
2688 buf->b_arc_access = ddi_get_lbolt();
2689 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2690 arc_change_state(arc_mru, buf, hash_lock);
2692 } else if (buf->b_state == arc_mru) {
2693 now = ddi_get_lbolt();
2696 * If this buffer is here because of a prefetch, then either:
2697 * - clear the flag if this is a "referencing" read
2698 * (any subsequent access will bump this into the MFU state).
2700 * - move the buffer to the head of the list if this is
2701 * another prefetch (to make it less likely to be evicted).
2703 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2704 if (refcount_count(&buf->b_refcnt) == 0) {
2705 ASSERT(list_link_active(&buf->b_arc_node));
2707 buf->b_flags &= ~ARC_PREFETCH;
2708 ARCSTAT_BUMP(arcstat_mru_hits);
2710 buf->b_arc_access = now;
2715 * This buffer has been "accessed" only once so far,
2716 * but it is still in the cache. Move it to the MFU
2719 if (now > buf->b_arc_access + ARC_MINTIME) {
2721 * More than 125ms have passed since we
2722 * instantiated this buffer. Move it to the
2723 * most frequently used state.
2725 buf->b_arc_access = now;
2726 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2727 arc_change_state(arc_mfu, buf, hash_lock);
2729 ARCSTAT_BUMP(arcstat_mru_hits);
2730 } else if (buf->b_state == arc_mru_ghost) {
2731 arc_state_t *new_state;
2733 * This buffer has been "accessed" recently, but
2734 * was evicted from the cache. Move it to the
2738 if (buf->b_flags & ARC_PREFETCH) {
2739 new_state = arc_mru;
2740 if (refcount_count(&buf->b_refcnt) > 0)
2741 buf->b_flags &= ~ARC_PREFETCH;
2742 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2744 new_state = arc_mfu;
2745 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2748 buf->b_arc_access = ddi_get_lbolt();
2749 arc_change_state(new_state, buf, hash_lock);
2751 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2752 } else if (buf->b_state == arc_mfu) {
2754 * This buffer has been accessed more than once and is
2755 * still in the cache. Keep it in the MFU state.
2757 * NOTE: an add_reference() that occurred when we did
2758 * the arc_read() will have kicked this off the list.
2759 * If it was a prefetch, we will explicitly move it to
2760 * the head of the list now.
2762 if ((buf->b_flags & ARC_PREFETCH) != 0) {
2763 ASSERT(refcount_count(&buf->b_refcnt) == 0);
2764 ASSERT(list_link_active(&buf->b_arc_node));
2766 ARCSTAT_BUMP(arcstat_mfu_hits);
2767 buf->b_arc_access = ddi_get_lbolt();
2768 } else if (buf->b_state == arc_mfu_ghost) {
2769 arc_state_t *new_state = arc_mfu;
2771 * This buffer has been accessed more than once but has
2772 * been evicted from the cache. Move it back to the
2776 if (buf->b_flags & ARC_PREFETCH) {
2778 * This is a prefetch access...
2779 * move this block back to the MRU state.
2781 ASSERT0(refcount_count(&buf->b_refcnt));
2782 new_state = arc_mru;
2785 buf->b_arc_access = ddi_get_lbolt();
2786 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2787 arc_change_state(new_state, buf, hash_lock);
2789 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2790 } else if (buf->b_state == arc_l2c_only) {
2792 * This buffer is on the 2nd Level ARC.
2795 buf->b_arc_access = ddi_get_lbolt();
2796 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2797 arc_change_state(arc_mfu, buf, hash_lock);
2799 ASSERT(!"invalid arc state");
2803 /* a generic arc_done_func_t which you can use */
2806 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2808 if (zio == NULL || zio->io_error == 0)
2809 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2810 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2813 /* a generic arc_done_func_t */
2815 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2817 arc_buf_t **bufp = arg;
2818 if (zio && zio->io_error) {
2819 VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2823 ASSERT(buf->b_data);
2828 arc_read_done(zio_t *zio)
2830 arc_buf_hdr_t *hdr, *found;
2832 arc_buf_t *abuf; /* buffer we're assigning to callback */
2833 kmutex_t *hash_lock;
2834 arc_callback_t *callback_list, *acb;
2835 int freeable = FALSE;
2837 buf = zio->io_private;
2841 * The hdr was inserted into hash-table and removed from lists
2842 * prior to starting I/O. We should find this header, since
2843 * it's in the hash table, and it should be legit since it's
2844 * not possible to evict it during the I/O. The only possible
2845 * reason for it not to be found is if we were freed during the
2848 found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
2851 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
2852 (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2853 (found == hdr && HDR_L2_READING(hdr)));
2855 hdr->b_flags &= ~ARC_L2_EVICTED;
2856 if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2857 hdr->b_flags &= ~ARC_L2CACHE;
2859 /* byteswap if necessary */
2860 callback_list = hdr->b_acb;
2861 ASSERT(callback_list != NULL);
2862 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2863 dmu_object_byteswap_t bswap =
2864 DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
2865 arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
2866 byteswap_uint64_array :
2867 dmu_ot_byteswap[bswap].ob_func;
2868 func(buf->b_data, hdr->b_size);
2871 arc_cksum_compute(buf, B_FALSE);
2874 #endif /* illumos */
2876 if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
2878 * Only call arc_access on anonymous buffers. This is because
2879 * if we've issued an I/O for an evicted buffer, we've already
2880 * called arc_access (to prevent any simultaneous readers from
2881 * getting confused).
2883 arc_access(hdr, hash_lock);
2886 /* create copies of the data buffer for the callers */
2888 for (acb = callback_list; acb; acb = acb->acb_next) {
2889 if (acb->acb_done) {
2891 abuf = arc_buf_clone(buf);
2892 acb->acb_buf = abuf;
2897 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2898 ASSERT(!HDR_BUF_AVAILABLE(hdr));
2900 ASSERT(buf->b_efunc == NULL);
2901 ASSERT(hdr->b_datacnt == 1);
2902 hdr->b_flags |= ARC_BUF_AVAILABLE;
2905 ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2907 if (zio->io_error != 0) {
2908 hdr->b_flags |= ARC_IO_ERROR;
2909 if (hdr->b_state != arc_anon)
2910 arc_change_state(arc_anon, hdr, hash_lock);
2911 if (HDR_IN_HASH_TABLE(hdr))
2912 buf_hash_remove(hdr);
2913 freeable = refcount_is_zero(&hdr->b_refcnt);
2917 * Broadcast before we drop the hash_lock to avoid the possibility
2918 * that the hdr (and hence the cv) might be freed before we get to
2919 * the cv_broadcast().
2921 cv_broadcast(&hdr->b_cv);
2924 mutex_exit(hash_lock);
2927 * This block was freed while we waited for the read to
2928 * complete. It has been removed from the hash table and
2929 * moved to the anonymous state (so that it won't show up
2932 ASSERT3P(hdr->b_state, ==, arc_anon);
2933 freeable = refcount_is_zero(&hdr->b_refcnt);
2936 /* execute each callback and free its structure */
2937 while ((acb = callback_list) != NULL) {
2939 acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2941 if (acb->acb_zio_dummy != NULL) {
2942 acb->acb_zio_dummy->io_error = zio->io_error;
2943 zio_nowait(acb->acb_zio_dummy);
2946 callback_list = acb->acb_next;
2947 kmem_free(acb, sizeof (arc_callback_t));
2951 arc_hdr_destroy(hdr);
2955 * "Read" the block block at the specified DVA (in bp) via the
2956 * cache. If the block is found in the cache, invoke the provided
2957 * callback immediately and return. Note that the `zio' parameter
2958 * in the callback will be NULL in this case, since no IO was
2959 * required. If the block is not in the cache pass the read request
2960 * on to the spa with a substitute callback function, so that the
2961 * requested block will be added to the cache.
2963 * If a read request arrives for a block that has a read in-progress,
2964 * either wait for the in-progress read to complete (and return the
2965 * results); or, if this is a read with a "done" func, add a record
2966 * to the read to invoke the "done" func when the read completes,
2967 * and return; or just return.
2969 * arc_read_done() will invoke all the requested "done" functions
2970 * for readers of this block.
2972 * Normal callers should use arc_read and pass the arc buffer and offset
2973 * for the bp. But if you know you don't need locking, you can use
2977 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf,
2978 arc_done_func_t *done, void *private, int priority, int zio_flags,
2979 uint32_t *arc_flags, const zbookmark_t *zb)
2985 * XXX This happens from traverse callback funcs, for
2986 * the objset_phys_t block.
2988 return (arc_read_nolock(pio, spa, bp, done, private, priority,
2989 zio_flags, arc_flags, zb));
2992 ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt));
2993 ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size);
2994 rw_enter(&pbuf->b_data_lock, RW_READER);
2996 err = arc_read_nolock(pio, spa, bp, done, private, priority,
2997 zio_flags, arc_flags, zb);
2998 rw_exit(&pbuf->b_data_lock);
3004 arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp,
3005 arc_done_func_t *done, void *private, int priority, int zio_flags,
3006 uint32_t *arc_flags, const zbookmark_t *zb)
3010 kmutex_t *hash_lock;
3012 uint64_t guid = spa_load_guid(spa);
3015 hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
3017 if (hdr && hdr->b_datacnt > 0) {
3019 *arc_flags |= ARC_CACHED;
3021 if (HDR_IO_IN_PROGRESS(hdr)) {
3023 if (*arc_flags & ARC_WAIT) {
3024 cv_wait(&hdr->b_cv, hash_lock);
3025 mutex_exit(hash_lock);
3028 ASSERT(*arc_flags & ARC_NOWAIT);
3031 arc_callback_t *acb = NULL;
3033 acb = kmem_zalloc(sizeof (arc_callback_t),
3035 acb->acb_done = done;
3036 acb->acb_private = private;
3038 acb->acb_zio_dummy = zio_null(pio,
3039 spa, NULL, NULL, NULL, zio_flags);
3041 ASSERT(acb->acb_done != NULL);
3042 acb->acb_next = hdr->b_acb;
3044 add_reference(hdr, hash_lock, private);
3045 mutex_exit(hash_lock);
3048 mutex_exit(hash_lock);
3052 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3055 add_reference(hdr, hash_lock, private);
3057 * If this block is already in use, create a new
3058 * copy of the data so that we will be guaranteed
3059 * that arc_release() will always succeed.
3063 ASSERT(buf->b_data);
3064 if (HDR_BUF_AVAILABLE(hdr)) {
3065 ASSERT(buf->b_efunc == NULL);
3066 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3068 buf = arc_buf_clone(buf);
3071 } else if (*arc_flags & ARC_PREFETCH &&
3072 refcount_count(&hdr->b_refcnt) == 0) {
3073 hdr->b_flags |= ARC_PREFETCH;
3075 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
3076 arc_access(hdr, hash_lock);
3077 if (*arc_flags & ARC_L2CACHE)
3078 hdr->b_flags |= ARC_L2CACHE;
3079 mutex_exit(hash_lock);
3080 ARCSTAT_BUMP(arcstat_hits);
3081 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3082 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3083 data, metadata, hits);
3086 done(NULL, buf, private);
3088 uint64_t size = BP_GET_LSIZE(bp);
3089 arc_callback_t *acb;
3092 boolean_t devw = B_FALSE;
3095 /* this block is not in the cache */
3096 arc_buf_hdr_t *exists;
3097 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
3098 buf = arc_buf_alloc(spa, size, private, type);
3100 hdr->b_dva = *BP_IDENTITY(bp);
3101 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
3102 hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
3103 exists = buf_hash_insert(hdr, &hash_lock);
3105 /* somebody beat us to the hash insert */
3106 mutex_exit(hash_lock);
3107 buf_discard_identity(hdr);
3108 (void) arc_buf_remove_ref(buf, private);
3109 goto top; /* restart the IO request */
3111 /* if this is a prefetch, we don't have a reference */
3112 if (*arc_flags & ARC_PREFETCH) {
3113 (void) remove_reference(hdr, hash_lock,
3115 hdr->b_flags |= ARC_PREFETCH;
3117 if (*arc_flags & ARC_L2CACHE)
3118 hdr->b_flags |= ARC_L2CACHE;
3119 if (BP_GET_LEVEL(bp) > 0)
3120 hdr->b_flags |= ARC_INDIRECT;
3122 /* this block is in the ghost cache */
3123 ASSERT(GHOST_STATE(hdr->b_state));
3124 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3125 ASSERT0(refcount_count(&hdr->b_refcnt));
3126 ASSERT(hdr->b_buf == NULL);
3128 /* if this is a prefetch, we don't have a reference */
3129 if (*arc_flags & ARC_PREFETCH)
3130 hdr->b_flags |= ARC_PREFETCH;
3132 add_reference(hdr, hash_lock, private);
3133 if (*arc_flags & ARC_L2CACHE)
3134 hdr->b_flags |= ARC_L2CACHE;
3135 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
3138 buf->b_efunc = NULL;
3139 buf->b_private = NULL;
3142 ASSERT(hdr->b_datacnt == 0);
3144 arc_get_data_buf(buf);
3145 arc_access(hdr, hash_lock);
3148 ASSERT(!GHOST_STATE(hdr->b_state));
3150 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
3151 acb->acb_done = done;
3152 acb->acb_private = private;
3154 ASSERT(hdr->b_acb == NULL);
3156 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3158 if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
3159 (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
3160 devw = hdr->b_l2hdr->b_dev->l2ad_writing;
3161 addr = hdr->b_l2hdr->b_daddr;
3163 * Lock out device removal.
3165 if (vdev_is_dead(vd) ||
3166 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
3170 mutex_exit(hash_lock);
3172 ASSERT3U(hdr->b_size, ==, size);
3173 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
3174 uint64_t, size, zbookmark_t *, zb);
3175 ARCSTAT_BUMP(arcstat_misses);
3176 ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
3177 demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
3178 data, metadata, misses);
3180 curthread->td_ru.ru_inblock++;
3183 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
3185 * Read from the L2ARC if the following are true:
3186 * 1. The L2ARC vdev was previously cached.
3187 * 2. This buffer still has L2ARC metadata.
3188 * 3. This buffer isn't currently writing to the L2ARC.
3189 * 4. The L2ARC entry wasn't evicted, which may
3190 * also have invalidated the vdev.
3191 * 5. This isn't prefetch and l2arc_noprefetch is set.
3193 if (hdr->b_l2hdr != NULL &&
3194 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
3195 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
3196 l2arc_read_callback_t *cb;
3198 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
3199 ARCSTAT_BUMP(arcstat_l2_hits);
3201 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
3203 cb->l2rcb_buf = buf;
3204 cb->l2rcb_spa = spa;
3207 cb->l2rcb_flags = zio_flags;
3210 * l2arc read. The SCL_L2ARC lock will be
3211 * released by l2arc_read_done().
3213 rzio = zio_read_phys(pio, vd, addr, size,
3214 buf->b_data, ZIO_CHECKSUM_OFF,
3215 l2arc_read_done, cb, priority, zio_flags |
3216 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL |
3217 ZIO_FLAG_DONT_PROPAGATE |
3218 ZIO_FLAG_DONT_RETRY, B_FALSE);
3219 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
3221 ARCSTAT_INCR(arcstat_l2_read_bytes, size);
3223 if (*arc_flags & ARC_NOWAIT) {
3228 ASSERT(*arc_flags & ARC_WAIT);
3229 if (zio_wait(rzio) == 0)
3232 /* l2arc read error; goto zio_read() */
3234 DTRACE_PROBE1(l2arc__miss,
3235 arc_buf_hdr_t *, hdr);
3236 ARCSTAT_BUMP(arcstat_l2_misses);
3237 if (HDR_L2_WRITING(hdr))
3238 ARCSTAT_BUMP(arcstat_l2_rw_clash);
3239 spa_config_exit(spa, SCL_L2ARC, vd);
3243 spa_config_exit(spa, SCL_L2ARC, vd);
3244 if (l2arc_ndev != 0) {
3245 DTRACE_PROBE1(l2arc__miss,
3246 arc_buf_hdr_t *, hdr);
3247 ARCSTAT_BUMP(arcstat_l2_misses);
3251 rzio = zio_read(pio, spa, bp, buf->b_data, size,
3252 arc_read_done, buf, priority, zio_flags, zb);
3254 if (*arc_flags & ARC_WAIT)
3255 return (zio_wait(rzio));
3257 ASSERT(*arc_flags & ARC_NOWAIT);
3264 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
3266 ASSERT(buf->b_hdr != NULL);
3267 ASSERT(buf->b_hdr->b_state != arc_anon);
3268 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
3269 ASSERT(buf->b_efunc == NULL);
3270 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
3272 buf->b_efunc = func;
3273 buf->b_private = private;
3277 * This is used by the DMU to let the ARC know that a buffer is
3278 * being evicted, so the ARC should clean up. If this arc buf
3279 * is not yet in the evicted state, it will be put there.
3282 arc_buf_evict(arc_buf_t *buf)
3285 kmutex_t *hash_lock;
3287 list_t *list, *evicted_list;
3288 kmutex_t *lock, *evicted_lock;
3290 mutex_enter(&buf->b_evict_lock);
3294 * We are in arc_do_user_evicts().
3296 ASSERT(buf->b_data == NULL);
3297 mutex_exit(&buf->b_evict_lock);
3299 } else if (buf->b_data == NULL) {
3300 arc_buf_t copy = *buf; /* structure assignment */
3302 * We are on the eviction list; process this buffer now
3303 * but let arc_do_user_evicts() do the reaping.
3305 buf->b_efunc = NULL;
3306 mutex_exit(&buf->b_evict_lock);
3307 VERIFY(copy.b_efunc(©) == 0);
3310 hash_lock = HDR_LOCK(hdr);
3311 mutex_enter(hash_lock);
3313 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3315 ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
3316 ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3319 * Pull this buffer off of the hdr
3322 while (*bufp != buf)
3323 bufp = &(*bufp)->b_next;
3324 *bufp = buf->b_next;
3326 ASSERT(buf->b_data != NULL);
3327 arc_buf_destroy(buf, FALSE, FALSE);
3329 if (hdr->b_datacnt == 0) {
3330 arc_state_t *old_state = hdr->b_state;
3331 arc_state_t *evicted_state;
3333 ASSERT(hdr->b_buf == NULL);
3334 ASSERT(refcount_is_zero(&hdr->b_refcnt));
3337 (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
3339 get_buf_info(hdr, old_state, &list, &lock);
3340 get_buf_info(hdr, evicted_state, &evicted_list, &evicted_lock);
3342 mutex_enter(evicted_lock);
3344 arc_change_state(evicted_state, hdr, hash_lock);
3345 ASSERT(HDR_IN_HASH_TABLE(hdr));
3346 hdr->b_flags |= ARC_IN_HASH_TABLE;
3347 hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3349 mutex_exit(evicted_lock);
3352 mutex_exit(hash_lock);
3353 mutex_exit(&buf->b_evict_lock);
3355 VERIFY(buf->b_efunc(buf) == 0);
3356 buf->b_efunc = NULL;
3357 buf->b_private = NULL;
3360 kmem_cache_free(buf_cache, buf);
3365 * Release this buffer from the cache. This must be done
3366 * after a read and prior to modifying the buffer contents.
3367 * If the buffer has more than one reference, we must make
3368 * a new hdr for the buffer.
3371 arc_release(arc_buf_t *buf, void *tag)
3374 kmutex_t *hash_lock = NULL;
3375 l2arc_buf_hdr_t *l2hdr;
3379 * It would be nice to assert that if it's DMU metadata (level >
3380 * 0 || it's the dnode file), then it must be syncing context.
3381 * But we don't know that information at this level.
3384 mutex_enter(&buf->b_evict_lock);
3387 /* this buffer is not on any list */
3388 ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3390 if (hdr->b_state == arc_anon) {
3391 /* this buffer is already released */
3392 ASSERT(buf->b_efunc == NULL);
3394 hash_lock = HDR_LOCK(hdr);
3395 mutex_enter(hash_lock);
3397 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3400 l2hdr = hdr->b_l2hdr;
3402 mutex_enter(&l2arc_buflist_mtx);
3403 hdr->b_l2hdr = NULL;
3404 buf_size = hdr->b_size;
3408 * Do we have more than one buf?
3410 if (hdr->b_datacnt > 1) {
3411 arc_buf_hdr_t *nhdr;
3413 uint64_t blksz = hdr->b_size;
3414 uint64_t spa = hdr->b_spa;
3415 arc_buf_contents_t type = hdr->b_type;
3416 uint32_t flags = hdr->b_flags;
3418 ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3420 * Pull the data off of this hdr and attach it to
3421 * a new anonymous hdr.
3423 (void) remove_reference(hdr, hash_lock, tag);
3425 while (*bufp != buf)
3426 bufp = &(*bufp)->b_next;
3427 *bufp = buf->b_next;
3430 ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3431 atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3432 if (refcount_is_zero(&hdr->b_refcnt)) {
3433 uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3434 ASSERT3U(*size, >=, hdr->b_size);
3435 atomic_add_64(size, -hdr->b_size);
3437 hdr->b_datacnt -= 1;
3438 arc_cksum_verify(buf);
3440 arc_buf_unwatch(buf);
3441 #endif /* illumos */
3443 mutex_exit(hash_lock);
3445 nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3446 nhdr->b_size = blksz;
3448 nhdr->b_type = type;
3450 nhdr->b_state = arc_anon;
3451 nhdr->b_arc_access = 0;
3452 nhdr->b_flags = flags & ARC_L2_WRITING;
3453 nhdr->b_l2hdr = NULL;
3454 nhdr->b_datacnt = 1;
3455 nhdr->b_freeze_cksum = NULL;
3456 (void) refcount_add(&nhdr->b_refcnt, tag);
3458 mutex_exit(&buf->b_evict_lock);
3459 atomic_add_64(&arc_anon->arcs_size, blksz);
3461 mutex_exit(&buf->b_evict_lock);
3462 ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3463 ASSERT(!list_link_active(&hdr->b_arc_node));
3464 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3465 if (hdr->b_state != arc_anon)
3466 arc_change_state(arc_anon, hdr, hash_lock);
3467 hdr->b_arc_access = 0;
3469 mutex_exit(hash_lock);
3471 buf_discard_identity(hdr);
3474 buf->b_efunc = NULL;
3475 buf->b_private = NULL;
3478 list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3479 kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3480 ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3481 mutex_exit(&l2arc_buflist_mtx);
3486 * Release this buffer. If it does not match the provided BP, fill it
3487 * with that block's contents.
3491 arc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa,
3494 arc_release(buf, tag);
3499 arc_released(arc_buf_t *buf)
3503 mutex_enter(&buf->b_evict_lock);
3504 released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3505 mutex_exit(&buf->b_evict_lock);
3510 arc_has_callback(arc_buf_t *buf)
3514 mutex_enter(&buf->b_evict_lock);
3515 callback = (buf->b_efunc != NULL);
3516 mutex_exit(&buf->b_evict_lock);
3522 arc_referenced(arc_buf_t *buf)
3526 mutex_enter(&buf->b_evict_lock);
3527 referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3528 mutex_exit(&buf->b_evict_lock);
3529 return (referenced);
3534 arc_write_ready(zio_t *zio)
3536 arc_write_callback_t *callback = zio->io_private;
3537 arc_buf_t *buf = callback->awcb_buf;
3538 arc_buf_hdr_t *hdr = buf->b_hdr;
3540 ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3541 callback->awcb_ready(zio, buf, callback->awcb_private);
3544 * If the IO is already in progress, then this is a re-write
3545 * attempt, so we need to thaw and re-compute the cksum.
3546 * It is the responsibility of the callback to handle the
3547 * accounting for any re-write attempt.
3549 if (HDR_IO_IN_PROGRESS(hdr)) {
3550 mutex_enter(&hdr->b_freeze_lock);
3551 if (hdr->b_freeze_cksum != NULL) {
3552 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3553 hdr->b_freeze_cksum = NULL;
3555 mutex_exit(&hdr->b_freeze_lock);
3557 arc_cksum_compute(buf, B_FALSE);
3558 hdr->b_flags |= ARC_IO_IN_PROGRESS;
3562 arc_write_done(zio_t *zio)
3564 arc_write_callback_t *callback = zio->io_private;
3565 arc_buf_t *buf = callback->awcb_buf;
3566 arc_buf_hdr_t *hdr = buf->b_hdr;
3568 ASSERT(hdr->b_acb == NULL);
3570 if (zio->io_error == 0) {
3571 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3572 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3573 hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3575 ASSERT(BUF_EMPTY(hdr));
3579 * If the block to be written was all-zero, we may have
3580 * compressed it away. In this case no write was performed
3581 * so there will be no dva/birth/checksum. The buffer must
3582 * therefore remain anonymous (and uncached).
3584 if (!BUF_EMPTY(hdr)) {
3585 arc_buf_hdr_t *exists;
3586 kmutex_t *hash_lock;
3588 ASSERT(zio->io_error == 0);
3590 arc_cksum_verify(buf);
3592 exists = buf_hash_insert(hdr, &hash_lock);
3595 * This can only happen if we overwrite for
3596 * sync-to-convergence, because we remove
3597 * buffers from the hash table when we arc_free().
3599 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3600 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3601 panic("bad overwrite, hdr=%p exists=%p",
3602 (void *)hdr, (void *)exists);
3603 ASSERT(refcount_is_zero(&exists->b_refcnt));
3604 arc_change_state(arc_anon, exists, hash_lock);
3605 mutex_exit(hash_lock);
3606 arc_hdr_destroy(exists);
3607 exists = buf_hash_insert(hdr, &hash_lock);
3608 ASSERT3P(exists, ==, NULL);
3609 } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
3611 ASSERT(zio->io_prop.zp_nopwrite);
3612 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3613 panic("bad nopwrite, hdr=%p exists=%p",
3614 (void *)hdr, (void *)exists);
3617 ASSERT(hdr->b_datacnt == 1);
3618 ASSERT(hdr->b_state == arc_anon);
3619 ASSERT(BP_GET_DEDUP(zio->io_bp));
3620 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3623 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3624 /* if it's not anon, we are doing a scrub */
3625 if (!exists && hdr->b_state == arc_anon)
3626 arc_access(hdr, hash_lock);
3627 mutex_exit(hash_lock);
3629 hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3632 ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3633 callback->awcb_done(zio, buf, callback->awcb_private);
3635 kmem_free(callback, sizeof (arc_write_callback_t));
3639 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3640 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
3641 arc_done_func_t *ready, arc_done_func_t *done, void *private,
3642 int priority, int zio_flags, const zbookmark_t *zb)
3644 arc_buf_hdr_t *hdr = buf->b_hdr;
3645 arc_write_callback_t *callback;
3648 ASSERT(ready != NULL);
3649 ASSERT(done != NULL);
3650 ASSERT(!HDR_IO_ERROR(hdr));
3651 ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3652 ASSERT(hdr->b_acb == NULL);
3654 hdr->b_flags |= ARC_L2CACHE;
3655 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
3656 callback->awcb_ready = ready;
3657 callback->awcb_done = done;
3658 callback->awcb_private = private;
3659 callback->awcb_buf = buf;
3661 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3662 arc_write_ready, arc_write_done, callback, priority, zio_flags, zb);
3668 arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg)
3671 uint64_t available_memory =
3672 ptoa((uintmax_t)cnt.v_free_count + cnt.v_cache_count);
3673 static uint64_t page_load = 0;
3674 static uint64_t last_txg = 0;
3679 MIN(available_memory, vmem_size(heap_arena, VMEM_FREE));
3682 if (available_memory >= zfs_write_limit_max)
3685 if (txg > last_txg) {
3690 * If we are in pageout, we know that memory is already tight,
3691 * the arc is already going to be evicting, so we just want to
3692 * continue to let page writes occur as quickly as possible.
3694 if (curproc == pageproc) {
3695 if (page_load > available_memory / 4)
3697 /* Note: reserve is inflated, so we deflate */
3698 page_load += reserve / 8;
3700 } else if (page_load > 0 && arc_reclaim_needed()) {
3701 /* memory is low, delay before restarting */
3702 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3707 if (arc_size > arc_c_min) {
3708 uint64_t evictable_memory =
3709 arc_mru->arcs_lsize[ARC_BUFC_DATA] +
3710 arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
3711 arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
3712 arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
3713 available_memory += MIN(evictable_memory, arc_size - arc_c_min);
3716 if (inflight_data > available_memory / 4) {
3717 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3725 arc_tempreserve_clear(uint64_t reserve)
3727 atomic_add_64(&arc_tempreserve, -reserve);
3728 ASSERT((int64_t)arc_tempreserve >= 0);
3732 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3739 * Once in a while, fail for no reason. Everything should cope.
3741 if (spa_get_random(10000) == 0) {
3742 dprintf("forcing random failure\n");
3746 if (reserve > arc_c/4 && !arc_no_grow)
3747 arc_c = MIN(arc_c_max, reserve * 4);
3748 if (reserve > arc_c)
3752 * Don't count loaned bufs as in flight dirty data to prevent long
3753 * network delays from blocking transactions that are ready to be
3754 * assigned to a txg.
3756 anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3759 * Writes will, almost always, require additional memory allocations
3760 * in order to compress/encrypt/etc the data. We therefor need to
3761 * make sure that there is sufficient available memory for this.
3763 if (error = arc_memory_throttle(reserve, anon_size, txg))
3767 * Throttle writes when the amount of dirty data in the cache
3768 * gets too large. We try to keep the cache less than half full
3769 * of dirty blocks so that our sync times don't grow too large.
3770 * Note: if two requests come in concurrently, we might let them
3771 * both succeed, when one of them should fail. Not a huge deal.
3774 if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3775 anon_size > arc_c / 4) {
3776 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3777 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3778 arc_tempreserve>>10,
3779 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3780 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3781 reserve>>10, arc_c>>10);
3784 atomic_add_64(&arc_tempreserve, reserve);
3788 static kmutex_t arc_lowmem_lock;
3790 static eventhandler_tag arc_event_lowmem = NULL;
3793 arc_lowmem(void *arg __unused, int howto __unused)
3796 /* Serialize access via arc_lowmem_lock. */
3797 mutex_enter(&arc_lowmem_lock);
3798 mutex_enter(&arc_reclaim_thr_lock);
3800 cv_signal(&arc_reclaim_thr_cv);
3803 * It is unsafe to block here in arbitrary threads, because we can come
3804 * here from ARC itself and may hold ARC locks and thus risk a deadlock
3805 * with ARC reclaim thread.
3807 if (curproc == pageproc) {
3809 msleep(&needfree, &arc_reclaim_thr_lock, 0, "zfs:lowmem", 0);
3811 mutex_exit(&arc_reclaim_thr_lock);
3812 mutex_exit(&arc_lowmem_lock);
3819 int i, prefetch_tunable_set = 0;
3821 mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3822 cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3823 mutex_init(&arc_lowmem_lock, NULL, MUTEX_DEFAULT, NULL);
3825 /* Convert seconds to clock ticks */
3826 arc_min_prefetch_lifespan = 1 * hz;
3828 /* Start out with 1/8 of all memory */
3829 arc_c = kmem_size() / 8;
3834 * On architectures where the physical memory can be larger
3835 * than the addressable space (intel in 32-bit mode), we may
3836 * need to limit the cache to 1/8 of VM size.
3838 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3841 /* set min cache to 1/32 of all memory, or 16MB, whichever is more */
3842 arc_c_min = MAX(arc_c / 4, 64<<18);
3843 /* set max to 1/2 of all memory, or all but 1GB, whichever is more */
3844 if (arc_c * 8 >= 1<<30)
3845 arc_c_max = (arc_c * 8) - (1<<30);
3847 arc_c_max = arc_c_min;
3848 arc_c_max = MAX(arc_c * 5, arc_c_max);
3852 * Allow the tunables to override our calculations if they are
3853 * reasonable (ie. over 16MB)
3855 if (zfs_arc_max > 64<<18 && zfs_arc_max < kmem_size())
3856 arc_c_max = zfs_arc_max;
3857 if (zfs_arc_min > 64<<18 && zfs_arc_min <= arc_c_max)
3858 arc_c_min = zfs_arc_min;
3862 arc_p = (arc_c >> 1);
3864 /* limit meta-data to 1/4 of the arc capacity */
3865 arc_meta_limit = arc_c_max / 4;
3867 /* Allow the tunable to override if it is reasonable */
3868 if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
3869 arc_meta_limit = zfs_arc_meta_limit;
3871 if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3872 arc_c_min = arc_meta_limit / 2;
3874 if (zfs_arc_grow_retry > 0)
3875 arc_grow_retry = zfs_arc_grow_retry;
3877 if (zfs_arc_shrink_shift > 0)
3878 arc_shrink_shift = zfs_arc_shrink_shift;
3880 if (zfs_arc_p_min_shift > 0)
3881 arc_p_min_shift = zfs_arc_p_min_shift;
3883 /* if kmem_flags are set, lets try to use less memory */
3884 if (kmem_debugging())
3886 if (arc_c < arc_c_min)
3889 zfs_arc_min = arc_c_min;
3890 zfs_arc_max = arc_c_max;
3892 arc_anon = &ARC_anon;
3894 arc_mru_ghost = &ARC_mru_ghost;
3896 arc_mfu_ghost = &ARC_mfu_ghost;
3897 arc_l2c_only = &ARC_l2c_only;
3900 for (i = 0; i < ARC_BUFC_NUMLISTS; i++) {
3901 mutex_init(&arc_anon->arcs_locks[i].arcs_lock,
3902 NULL, MUTEX_DEFAULT, NULL);
3903 mutex_init(&arc_mru->arcs_locks[i].arcs_lock,
3904 NULL, MUTEX_DEFAULT, NULL);
3905 mutex_init(&arc_mru_ghost->arcs_locks[i].arcs_lock,
3906 NULL, MUTEX_DEFAULT, NULL);
3907 mutex_init(&arc_mfu->arcs_locks[i].arcs_lock,
3908 NULL, MUTEX_DEFAULT, NULL);
3909 mutex_init(&arc_mfu_ghost->arcs_locks[i].arcs_lock,
3910 NULL, MUTEX_DEFAULT, NULL);
3911 mutex_init(&arc_l2c_only->arcs_locks[i].arcs_lock,
3912 NULL, MUTEX_DEFAULT, NULL);
3914 list_create(&arc_mru->arcs_lists[i],
3915 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3916 list_create(&arc_mru_ghost->arcs_lists[i],
3917 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3918 list_create(&arc_mfu->arcs_lists[i],
3919 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3920 list_create(&arc_mfu_ghost->arcs_lists[i],
3921 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3922 list_create(&arc_mfu_ghost->arcs_lists[i],
3923 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3924 list_create(&arc_l2c_only->arcs_lists[i],
3925 sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3930 arc_thread_exit = 0;
3931 arc_eviction_list = NULL;
3932 mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
3933 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3935 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
3936 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3938 if (arc_ksp != NULL) {
3939 arc_ksp->ks_data = &arc_stats;
3940 kstat_install(arc_ksp);
3943 (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
3944 TS_RUN, minclsyspri);
3947 arc_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem, arc_lowmem, NULL,
3948 EVENTHANDLER_PRI_FIRST);
3954 if (zfs_write_limit_max == 0)
3955 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
3957 zfs_write_limit_shift = 0;
3958 mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL);
3961 if (TUNABLE_INT_FETCH("vfs.zfs.prefetch_disable", &zfs_prefetch_disable))
3962 prefetch_tunable_set = 1;
3965 if (prefetch_tunable_set == 0) {
3966 printf("ZFS NOTICE: Prefetch is disabled by default on i386 "
3968 printf(" add \"vfs.zfs.prefetch_disable=0\" "
3969 "to /boot/loader.conf.\n");
3970 zfs_prefetch_disable = 1;
3973 if ((((uint64_t)physmem * PAGESIZE) < (1ULL << 32)) &&
3974 prefetch_tunable_set == 0) {
3975 printf("ZFS NOTICE: Prefetch is disabled by default if less "
3976 "than 4GB of RAM is present;\n"
3977 " to enable, add \"vfs.zfs.prefetch_disable=0\" "
3978 "to /boot/loader.conf.\n");
3979 zfs_prefetch_disable = 1;
3982 /* Warn about ZFS memory and address space requirements. */
3983 if (((uint64_t)physmem * PAGESIZE) < (256 + 128 + 64) * (1 << 20)) {
3984 printf("ZFS WARNING: Recommended minimum RAM size is 512MB; "
3985 "expect unstable behavior.\n");
3987 if (kmem_size() < 512 * (1 << 20)) {
3988 printf("ZFS WARNING: Recommended minimum kmem_size is 512MB; "
3989 "expect unstable behavior.\n");
3990 printf(" Consider tuning vm.kmem_size and "
3991 "vm.kmem_size_max\n");
3992 printf(" in /boot/loader.conf.\n");
4002 mutex_enter(&arc_reclaim_thr_lock);
4003 arc_thread_exit = 1;
4004 cv_signal(&arc_reclaim_thr_cv);
4005 while (arc_thread_exit != 0)
4006 cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
4007 mutex_exit(&arc_reclaim_thr_lock);
4013 if (arc_ksp != NULL) {
4014 kstat_delete(arc_ksp);
4018 mutex_destroy(&arc_eviction_mtx);
4019 mutex_destroy(&arc_reclaim_thr_lock);
4020 cv_destroy(&arc_reclaim_thr_cv);
4022 for (i = 0; i < ARC_BUFC_NUMLISTS; i++) {
4023 list_destroy(&arc_mru->arcs_lists[i]);
4024 list_destroy(&arc_mru_ghost->arcs_lists[i]);
4025 list_destroy(&arc_mfu->arcs_lists[i]);
4026 list_destroy(&arc_mfu_ghost->arcs_lists[i]);
4027 list_destroy(&arc_l2c_only->arcs_lists[i]);
4029 mutex_destroy(&arc_anon->arcs_locks[i].arcs_lock);
4030 mutex_destroy(&arc_mru->arcs_locks[i].arcs_lock);
4031 mutex_destroy(&arc_mru_ghost->arcs_locks[i].arcs_lock);
4032 mutex_destroy(&arc_mfu->arcs_locks[i].arcs_lock);
4033 mutex_destroy(&arc_mfu_ghost->arcs_locks[i].arcs_lock);
4034 mutex_destroy(&arc_l2c_only->arcs_locks[i].arcs_lock);
4037 mutex_destroy(&zfs_write_limit_lock);
4041 ASSERT(arc_loaned_bytes == 0);
4043 mutex_destroy(&arc_lowmem_lock);
4045 if (arc_event_lowmem != NULL)
4046 EVENTHANDLER_DEREGISTER(vm_lowmem, arc_event_lowmem);
4053 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
4054 * It uses dedicated storage devices to hold cached data, which are populated
4055 * using large infrequent writes. The main role of this cache is to boost
4056 * the performance of random read workloads. The intended L2ARC devices
4057 * include short-stroked disks, solid state disks, and other media with
4058 * substantially faster read latency than disk.
4060 * +-----------------------+
4062 * +-----------------------+
4065 * l2arc_feed_thread() arc_read()
4069 * +---------------+ |
4071 * +---------------+ |
4076 * +-------+ +-------+
4078 * | cache | | cache |
4079 * +-------+ +-------+
4080 * +=========+ .-----.
4081 * : L2ARC : |-_____-|
4082 * : devices : | Disks |
4083 * +=========+ `-_____-'
4085 * Read requests are satisfied from the following sources, in order:
4088 * 2) vdev cache of L2ARC devices
4090 * 4) vdev cache of disks
4093 * Some L2ARC device types exhibit extremely slow write performance.
4094 * To accommodate for this there are some significant differences between
4095 * the L2ARC and traditional cache design:
4097 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
4098 * the ARC behave as usual, freeing buffers and placing headers on ghost
4099 * lists. The ARC does not send buffers to the L2ARC during eviction as
4100 * this would add inflated write latencies for all ARC memory pressure.
4102 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
4103 * It does this by periodically scanning buffers from the eviction-end of
4104 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
4105 * not already there. It scans until a headroom of buffers is satisfied,
4106 * which itself is a buffer for ARC eviction. The thread that does this is
4107 * l2arc_feed_thread(), illustrated below; example sizes are included to
4108 * provide a better sense of ratio than this diagram:
4111 * +---------------------+----------+
4112 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
4113 * +---------------------+----------+ | o L2ARC eligible
4114 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
4115 * +---------------------+----------+ |
4116 * 15.9 Gbytes ^ 32 Mbytes |
4118 * l2arc_feed_thread()
4120 * l2arc write hand <--[oooo]--'
4124 * +==============================+
4125 * L2ARC dev |####|#|###|###| |####| ... |
4126 * +==============================+
4129 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
4130 * evicted, then the L2ARC has cached a buffer much sooner than it probably
4131 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
4132 * safe to say that this is an uncommon case, since buffers at the end of
4133 * the ARC lists have moved there due to inactivity.
4135 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
4136 * then the L2ARC simply misses copying some buffers. This serves as a
4137 * pressure valve to prevent heavy read workloads from both stalling the ARC
4138 * with waits and clogging the L2ARC with writes. This also helps prevent
4139 * the potential for the L2ARC to churn if it attempts to cache content too
4140 * quickly, such as during backups of the entire pool.
4142 * 5. After system boot and before the ARC has filled main memory, there are
4143 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
4144 * lists can remain mostly static. Instead of searching from tail of these
4145 * lists as pictured, the l2arc_feed_thread() will search from the list heads
4146 * for eligible buffers, greatly increasing its chance of finding them.
4148 * The L2ARC device write speed is also boosted during this time so that
4149 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
4150 * there are no L2ARC reads, and no fear of degrading read performance
4151 * through increased writes.
4153 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
4154 * the vdev queue can aggregate them into larger and fewer writes. Each
4155 * device is written to in a rotor fashion, sweeping writes through
4156 * available space then repeating.
4158 * 7. The L2ARC does not store dirty content. It never needs to flush
4159 * write buffers back to disk based storage.
4161 * 8. If an ARC buffer is written (and dirtied) which also exists in the
4162 * L2ARC, the now stale L2ARC buffer is immediately dropped.
4164 * The performance of the L2ARC can be tweaked by a number of tunables, which
4165 * may be necessary for different workloads:
4167 * l2arc_write_max max write bytes per interval
4168 * l2arc_write_boost extra write bytes during device warmup
4169 * l2arc_noprefetch skip caching prefetched buffers
4170 * l2arc_headroom number of max device writes to precache
4171 * l2arc_feed_secs seconds between L2ARC writing
4173 * Tunables may be removed or added as future performance improvements are
4174 * integrated, and also may become zpool properties.
4176 * There are three key functions that control how the L2ARC warms up:
4178 * l2arc_write_eligible() check if a buffer is eligible to cache
4179 * l2arc_write_size() calculate how much to write
4180 * l2arc_write_interval() calculate sleep delay between writes
4182 * These three functions determine what to write, how much, and how quickly
4187 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
4190 * A buffer is *not* eligible for the L2ARC if it:
4191 * 1. belongs to a different spa.
4192 * 2. is already cached on the L2ARC.
4193 * 3. has an I/O in progress (it may be an incomplete read).
4194 * 4. is flagged not eligible (zfs property).
4196 if (ab->b_spa != spa_guid) {
4197 ARCSTAT_BUMP(arcstat_l2_write_spa_mismatch);
4200 if (ab->b_l2hdr != NULL) {
4201 ARCSTAT_BUMP(arcstat_l2_write_in_l2);
4204 if (HDR_IO_IN_PROGRESS(ab)) {
4205 ARCSTAT_BUMP(arcstat_l2_write_hdr_io_in_progress);
4208 if (!HDR_L2CACHE(ab)) {
4209 ARCSTAT_BUMP(arcstat_l2_write_not_cacheable);
4217 l2arc_write_size(l2arc_dev_t *dev)
4221 size = dev->l2ad_write;
4223 if (arc_warm == B_FALSE)
4224 size += dev->l2ad_boost;
4231 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
4233 clock_t interval, next, now;
4236 * If the ARC lists are busy, increase our write rate; if the
4237 * lists are stale, idle back. This is achieved by checking
4238 * how much we previously wrote - if it was more than half of
4239 * what we wanted, schedule the next write much sooner.
4241 if (l2arc_feed_again && wrote > (wanted / 2))
4242 interval = (hz * l2arc_feed_min_ms) / 1000;
4244 interval = hz * l2arc_feed_secs;
4246 now = ddi_get_lbolt();
4247 next = MAX(now, MIN(now + interval, began + interval));
4253 l2arc_hdr_stat_add(void)
4255 ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
4256 ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
4260 l2arc_hdr_stat_remove(void)
4262 ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
4263 ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
4267 * Cycle through L2ARC devices. This is how L2ARC load balances.
4268 * If a device is returned, this also returns holding the spa config lock.
4270 static l2arc_dev_t *
4271 l2arc_dev_get_next(void)
4273 l2arc_dev_t *first, *next = NULL;
4276 * Lock out the removal of spas (spa_namespace_lock), then removal
4277 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
4278 * both locks will be dropped and a spa config lock held instead.
4280 mutex_enter(&spa_namespace_lock);
4281 mutex_enter(&l2arc_dev_mtx);
4283 /* if there are no vdevs, there is nothing to do */
4284 if (l2arc_ndev == 0)
4288 next = l2arc_dev_last;
4290 /* loop around the list looking for a non-faulted vdev */
4292 next = list_head(l2arc_dev_list);
4294 next = list_next(l2arc_dev_list, next);
4296 next = list_head(l2arc_dev_list);
4299 /* if we have come back to the start, bail out */
4302 else if (next == first)
4305 } while (vdev_is_dead(next->l2ad_vdev));
4307 /* if we were unable to find any usable vdevs, return NULL */
4308 if (vdev_is_dead(next->l2ad_vdev))
4311 l2arc_dev_last = next;
4314 mutex_exit(&l2arc_dev_mtx);
4317 * Grab the config lock to prevent the 'next' device from being
4318 * removed while we are writing to it.
4321 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
4322 mutex_exit(&spa_namespace_lock);
4328 * Free buffers that were tagged for destruction.
4331 l2arc_do_free_on_write()
4334 l2arc_data_free_t *df, *df_prev;
4336 mutex_enter(&l2arc_free_on_write_mtx);
4337 buflist = l2arc_free_on_write;
4339 for (df = list_tail(buflist); df; df = df_prev) {
4340 df_prev = list_prev(buflist, df);
4341 ASSERT(df->l2df_data != NULL);
4342 ASSERT(df->l2df_func != NULL);
4343 df->l2df_func(df->l2df_data, df->l2df_size);
4344 list_remove(buflist, df);
4345 kmem_free(df, sizeof (l2arc_data_free_t));
4348 mutex_exit(&l2arc_free_on_write_mtx);
4352 * A write to a cache device has completed. Update all headers to allow
4353 * reads from these buffers to begin.
4356 l2arc_write_done(zio_t *zio)
4358 l2arc_write_callback_t *cb;
4361 arc_buf_hdr_t *head, *ab, *ab_prev;
4362 l2arc_buf_hdr_t *abl2;
4363 kmutex_t *hash_lock;
4365 cb = zio->io_private;
4367 dev = cb->l2wcb_dev;
4368 ASSERT(dev != NULL);
4369 head = cb->l2wcb_head;
4370 ASSERT(head != NULL);
4371 buflist = dev->l2ad_buflist;
4372 ASSERT(buflist != NULL);
4373 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
4374 l2arc_write_callback_t *, cb);
4376 if (zio->io_error != 0)
4377 ARCSTAT_BUMP(arcstat_l2_writes_error);
4379 mutex_enter(&l2arc_buflist_mtx);
4382 * All writes completed, or an error was hit.
4384 for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
4385 ab_prev = list_prev(buflist, ab);
4387 hash_lock = HDR_LOCK(ab);
4388 if (!mutex_tryenter(hash_lock)) {
4390 * This buffer misses out. It may be in a stage
4391 * of eviction. Its ARC_L2_WRITING flag will be
4392 * left set, denying reads to this buffer.
4394 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
4398 if (zio->io_error != 0) {
4400 * Error - drop L2ARC entry.
4402 list_remove(buflist, ab);
4405 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4406 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4410 * Allow ARC to begin reads to this L2ARC entry.
4412 ab->b_flags &= ~ARC_L2_WRITING;
4414 mutex_exit(hash_lock);
4417 atomic_inc_64(&l2arc_writes_done);
4418 list_remove(buflist, head);
4419 kmem_cache_free(hdr_cache, head);
4420 mutex_exit(&l2arc_buflist_mtx);
4422 l2arc_do_free_on_write();
4424 kmem_free(cb, sizeof (l2arc_write_callback_t));
4428 * A read to a cache device completed. Validate buffer contents before
4429 * handing over to the regular ARC routines.
4432 l2arc_read_done(zio_t *zio)
4434 l2arc_read_callback_t *cb;
4437 kmutex_t *hash_lock;
4440 ASSERT(zio->io_vd != NULL);
4441 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
4443 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
4445 cb = zio->io_private;
4447 buf = cb->l2rcb_buf;
4448 ASSERT(buf != NULL);
4450 hash_lock = HDR_LOCK(buf->b_hdr);
4451 mutex_enter(hash_lock);
4453 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4456 * Check this survived the L2ARC journey.
4458 equal = arc_cksum_equal(buf);
4459 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4460 mutex_exit(hash_lock);
4461 zio->io_private = buf;
4462 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
4463 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
4466 mutex_exit(hash_lock);
4468 * Buffer didn't survive caching. Increment stats and
4469 * reissue to the original storage device.
4471 if (zio->io_error != 0) {
4472 ARCSTAT_BUMP(arcstat_l2_io_error);
4474 zio->io_error = EIO;
4477 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4480 * If there's no waiter, issue an async i/o to the primary
4481 * storage now. If there *is* a waiter, the caller must
4482 * issue the i/o in a context where it's OK to block.
4484 if (zio->io_waiter == NULL) {
4485 zio_t *pio = zio_unique_parent(zio);
4487 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4489 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4490 buf->b_data, zio->io_size, arc_read_done, buf,
4491 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4495 kmem_free(cb, sizeof (l2arc_read_callback_t));
4499 * This is the list priority from which the L2ARC will search for pages to
4500 * cache. This is used within loops (0..3) to cycle through lists in the
4501 * desired order. This order can have a significant effect on cache
4504 * Currently the metadata lists are hit first, MFU then MRU, followed by
4505 * the data lists. This function returns a locked list, and also returns
4509 l2arc_list_locked(int list_num, kmutex_t **lock)
4514 ASSERT(list_num >= 0 && list_num < 2 * ARC_BUFC_NUMLISTS);
4516 if (list_num < ARC_BUFC_NUMMETADATALISTS) {
4518 list = &arc_mfu->arcs_lists[idx];
4519 *lock = ARCS_LOCK(arc_mfu, idx);
4520 } else if (list_num < ARC_BUFC_NUMMETADATALISTS * 2) {
4521 idx = list_num - ARC_BUFC_NUMMETADATALISTS;
4522 list = &arc_mru->arcs_lists[idx];
4523 *lock = ARCS_LOCK(arc_mru, idx);
4524 } else if (list_num < (ARC_BUFC_NUMMETADATALISTS * 2 +
4525 ARC_BUFC_NUMDATALISTS)) {
4526 idx = list_num - ARC_BUFC_NUMMETADATALISTS;
4527 list = &arc_mfu->arcs_lists[idx];
4528 *lock = ARCS_LOCK(arc_mfu, idx);
4530 idx = list_num - ARC_BUFC_NUMLISTS;
4531 list = &arc_mru->arcs_lists[idx];
4532 *lock = ARCS_LOCK(arc_mru, idx);
4535 ASSERT(!(MUTEX_HELD(*lock)));
4541 * Evict buffers from the device write hand to the distance specified in
4542 * bytes. This distance may span populated buffers, it may span nothing.
4543 * This is clearing a region on the L2ARC device ready for writing.
4544 * If the 'all' boolean is set, every buffer is evicted.
4547 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4550 l2arc_buf_hdr_t *abl2;
4551 arc_buf_hdr_t *ab, *ab_prev;
4552 kmutex_t *hash_lock;
4555 buflist = dev->l2ad_buflist;
4557 if (buflist == NULL)
4560 if (!all && dev->l2ad_first) {
4562 * This is the first sweep through the device. There is
4568 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4570 * When nearing the end of the device, evict to the end
4571 * before the device write hand jumps to the start.
4573 taddr = dev->l2ad_end;
4575 taddr = dev->l2ad_hand + distance;
4577 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4578 uint64_t, taddr, boolean_t, all);
4581 mutex_enter(&l2arc_buflist_mtx);
4582 for (ab = list_tail(buflist); ab; ab = ab_prev) {
4583 ab_prev = list_prev(buflist, ab);
4585 hash_lock = HDR_LOCK(ab);
4586 if (!mutex_tryenter(hash_lock)) {
4588 * Missed the hash lock. Retry.
4590 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4591 mutex_exit(&l2arc_buflist_mtx);
4592 mutex_enter(hash_lock);
4593 mutex_exit(hash_lock);
4597 if (HDR_L2_WRITE_HEAD(ab)) {
4599 * We hit a write head node. Leave it for
4600 * l2arc_write_done().
4602 list_remove(buflist, ab);
4603 mutex_exit(hash_lock);
4607 if (!all && ab->b_l2hdr != NULL &&
4608 (ab->b_l2hdr->b_daddr > taddr ||
4609 ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4611 * We've evicted to the target address,
4612 * or the end of the device.
4614 mutex_exit(hash_lock);
4618 if (HDR_FREE_IN_PROGRESS(ab)) {
4620 * Already on the path to destruction.
4622 mutex_exit(hash_lock);
4626 if (ab->b_state == arc_l2c_only) {
4627 ASSERT(!HDR_L2_READING(ab));
4629 * This doesn't exist in the ARC. Destroy.
4630 * arc_hdr_destroy() will call list_remove()
4631 * and decrement arcstat_l2_size.
4633 arc_change_state(arc_anon, ab, hash_lock);
4634 arc_hdr_destroy(ab);
4637 * Invalidate issued or about to be issued
4638 * reads, since we may be about to write
4639 * over this location.
4641 if (HDR_L2_READING(ab)) {
4642 ARCSTAT_BUMP(arcstat_l2_evict_reading);
4643 ab->b_flags |= ARC_L2_EVICTED;
4647 * Tell ARC this no longer exists in L2ARC.
4649 if (ab->b_l2hdr != NULL) {
4652 kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4653 ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4655 list_remove(buflist, ab);
4658 * This may have been leftover after a
4661 ab->b_flags &= ~ARC_L2_WRITING;
4663 mutex_exit(hash_lock);
4665 mutex_exit(&l2arc_buflist_mtx);
4667 vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
4668 dev->l2ad_evict = taddr;
4672 * Find and write ARC buffers to the L2ARC device.
4674 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4675 * for reading until they have completed writing.
4678 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
4680 arc_buf_hdr_t *ab, *ab_prev, *head;
4681 l2arc_buf_hdr_t *hdrl2;
4683 uint64_t passed_sz, write_sz, buf_sz, headroom;
4685 kmutex_t *hash_lock, *list_lock;
4686 boolean_t have_lock, full;
4687 l2arc_write_callback_t *cb;
4689 uint64_t guid = spa_load_guid(spa);
4692 ASSERT(dev->l2ad_vdev != NULL);
4697 head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4698 head->b_flags |= ARC_L2_WRITE_HEAD;
4700 ARCSTAT_BUMP(arcstat_l2_write_buffer_iter);
4702 * Copy buffers for L2ARC writing.
4704 mutex_enter(&l2arc_buflist_mtx);
4705 for (try = 0; try < 2 * ARC_BUFC_NUMLISTS; try++) {
4706 list = l2arc_list_locked(try, &list_lock);
4708 ARCSTAT_BUMP(arcstat_l2_write_buffer_list_iter);
4711 * L2ARC fast warmup.
4713 * Until the ARC is warm and starts to evict, read from the
4714 * head of the ARC lists rather than the tail.
4716 headroom = target_sz * l2arc_headroom;
4717 if (arc_warm == B_FALSE)
4718 ab = list_head(list);
4720 ab = list_tail(list);
4722 ARCSTAT_BUMP(arcstat_l2_write_buffer_list_null_iter);
4724 for (; ab; ab = ab_prev) {
4725 if (arc_warm == B_FALSE)
4726 ab_prev = list_next(list, ab);
4728 ab_prev = list_prev(list, ab);
4729 ARCSTAT_INCR(arcstat_l2_write_buffer_bytes_scanned, ab->b_size);
4731 hash_lock = HDR_LOCK(ab);
4732 have_lock = MUTEX_HELD(hash_lock);
4733 if (!have_lock && !mutex_tryenter(hash_lock)) {
4734 ARCSTAT_BUMP(arcstat_l2_write_trylock_fail);
4736 * Skip this buffer rather than waiting.
4741 passed_sz += ab->b_size;
4742 if (passed_sz > headroom) {
4746 mutex_exit(hash_lock);
4747 ARCSTAT_BUMP(arcstat_l2_write_passed_headroom);
4751 if (!l2arc_write_eligible(guid, ab)) {
4752 mutex_exit(hash_lock);
4756 if ((write_sz + ab->b_size) > target_sz) {
4758 mutex_exit(hash_lock);
4759 ARCSTAT_BUMP(arcstat_l2_write_full);
4765 * Insert a dummy header on the buflist so
4766 * l2arc_write_done() can find where the
4767 * write buffers begin without searching.
4769 list_insert_head(dev->l2ad_buflist, head);
4772 sizeof (l2arc_write_callback_t), KM_SLEEP);
4773 cb->l2wcb_dev = dev;
4774 cb->l2wcb_head = head;
4775 pio = zio_root(spa, l2arc_write_done, cb,
4777 ARCSTAT_BUMP(arcstat_l2_write_pios);
4781 * Create and add a new L2ARC header.
4783 hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
4785 hdrl2->b_daddr = dev->l2ad_hand;
4787 ab->b_flags |= ARC_L2_WRITING;
4788 ab->b_l2hdr = hdrl2;
4789 list_insert_head(dev->l2ad_buflist, ab);
4790 buf_data = ab->b_buf->b_data;
4791 buf_sz = ab->b_size;
4794 * Compute and store the buffer cksum before
4795 * writing. On debug the cksum is verified first.
4797 arc_cksum_verify(ab->b_buf);
4798 arc_cksum_compute(ab->b_buf, B_TRUE);
4800 mutex_exit(hash_lock);
4802 wzio = zio_write_phys(pio, dev->l2ad_vdev,
4803 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
4804 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
4805 ZIO_FLAG_CANFAIL, B_FALSE);
4807 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
4809 (void) zio_nowait(wzio);
4812 * Keep the clock hand suitably device-aligned.
4814 buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
4817 dev->l2ad_hand += buf_sz;
4820 mutex_exit(list_lock);
4825 mutex_exit(&l2arc_buflist_mtx);
4829 kmem_cache_free(hdr_cache, head);
4833 ASSERT3U(write_sz, <=, target_sz);
4834 ARCSTAT_BUMP(arcstat_l2_writes_sent);
4835 ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz);
4836 ARCSTAT_INCR(arcstat_l2_size, write_sz);
4837 vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0);
4840 * Bump device hand to the device start if it is approaching the end.
4841 * l2arc_evict() will already have evicted ahead for this case.
4843 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
4844 vdev_space_update(dev->l2ad_vdev,
4845 dev->l2ad_end - dev->l2ad_hand, 0, 0);
4846 dev->l2ad_hand = dev->l2ad_start;
4847 dev->l2ad_evict = dev->l2ad_start;
4848 dev->l2ad_first = B_FALSE;
4851 dev->l2ad_writing = B_TRUE;
4852 (void) zio_wait(pio);
4853 dev->l2ad_writing = B_FALSE;
4859 * This thread feeds the L2ARC at regular intervals. This is the beating
4860 * heart of the L2ARC.
4863 l2arc_feed_thread(void *dummy __unused)
4868 uint64_t size, wrote;
4869 clock_t begin, next = ddi_get_lbolt();
4871 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
4873 mutex_enter(&l2arc_feed_thr_lock);
4875 while (l2arc_thread_exit == 0) {
4876 CALLB_CPR_SAFE_BEGIN(&cpr);
4877 (void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock,
4878 next - ddi_get_lbolt());
4879 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
4880 next = ddi_get_lbolt() + hz;
4883 * Quick check for L2ARC devices.
4885 mutex_enter(&l2arc_dev_mtx);
4886 if (l2arc_ndev == 0) {
4887 mutex_exit(&l2arc_dev_mtx);
4890 mutex_exit(&l2arc_dev_mtx);
4891 begin = ddi_get_lbolt();
4894 * This selects the next l2arc device to write to, and in
4895 * doing so the next spa to feed from: dev->l2ad_spa. This
4896 * will return NULL if there are now no l2arc devices or if
4897 * they are all faulted.
4899 * If a device is returned, its spa's config lock is also
4900 * held to prevent device removal. l2arc_dev_get_next()
4901 * will grab and release l2arc_dev_mtx.
4903 if ((dev = l2arc_dev_get_next()) == NULL)
4906 spa = dev->l2ad_spa;
4907 ASSERT(spa != NULL);
4910 * If the pool is read-only then force the feed thread to
4911 * sleep a little longer.
4913 if (!spa_writeable(spa)) {
4914 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
4915 spa_config_exit(spa, SCL_L2ARC, dev);
4920 * Avoid contributing to memory pressure.
4922 if (arc_reclaim_needed()) {
4923 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
4924 spa_config_exit(spa, SCL_L2ARC, dev);
4928 ARCSTAT_BUMP(arcstat_l2_feeds);
4930 size = l2arc_write_size(dev);
4933 * Evict L2ARC buffers that will be overwritten.
4935 l2arc_evict(dev, size, B_FALSE);
4938 * Write ARC buffers.
4940 wrote = l2arc_write_buffers(spa, dev, size);
4943 * Calculate interval between writes.
4945 next = l2arc_write_interval(begin, size, wrote);
4946 spa_config_exit(spa, SCL_L2ARC, dev);
4949 l2arc_thread_exit = 0;
4950 cv_broadcast(&l2arc_feed_thr_cv);
4951 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
4956 l2arc_vdev_present(vdev_t *vd)
4960 mutex_enter(&l2arc_dev_mtx);
4961 for (dev = list_head(l2arc_dev_list); dev != NULL;
4962 dev = list_next(l2arc_dev_list, dev)) {
4963 if (dev->l2ad_vdev == vd)
4966 mutex_exit(&l2arc_dev_mtx);
4968 return (dev != NULL);
4972 * Add a vdev for use by the L2ARC. By this point the spa has already
4973 * validated the vdev and opened it.
4976 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
4978 l2arc_dev_t *adddev;
4980 ASSERT(!l2arc_vdev_present(vd));
4983 * Create a new l2arc device entry.
4985 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
4986 adddev->l2ad_spa = spa;
4987 adddev->l2ad_vdev = vd;
4988 adddev->l2ad_write = l2arc_write_max;
4989 adddev->l2ad_boost = l2arc_write_boost;
4990 adddev->l2ad_start = VDEV_LABEL_START_SIZE;
4991 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
4992 adddev->l2ad_hand = adddev->l2ad_start;
4993 adddev->l2ad_evict = adddev->l2ad_start;
4994 adddev->l2ad_first = B_TRUE;
4995 adddev->l2ad_writing = B_FALSE;
4996 ASSERT3U(adddev->l2ad_write, >, 0);
4999 * This is a list of all ARC buffers that are still valid on the
5002 adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
5003 list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
5004 offsetof(arc_buf_hdr_t, b_l2node));
5006 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
5009 * Add device to global list
5011 mutex_enter(&l2arc_dev_mtx);
5012 list_insert_head(l2arc_dev_list, adddev);
5013 atomic_inc_64(&l2arc_ndev);
5014 mutex_exit(&l2arc_dev_mtx);
5018 * Remove a vdev from the L2ARC.
5021 l2arc_remove_vdev(vdev_t *vd)
5023 l2arc_dev_t *dev, *nextdev, *remdev = NULL;
5026 * Find the device by vdev
5028 mutex_enter(&l2arc_dev_mtx);
5029 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
5030 nextdev = list_next(l2arc_dev_list, dev);
5031 if (vd == dev->l2ad_vdev) {
5036 ASSERT(remdev != NULL);
5039 * Remove device from global list
5041 list_remove(l2arc_dev_list, remdev);
5042 l2arc_dev_last = NULL; /* may have been invalidated */
5043 atomic_dec_64(&l2arc_ndev);
5044 mutex_exit(&l2arc_dev_mtx);
5047 * Clear all buflists and ARC references. L2ARC device flush.
5049 l2arc_evict(remdev, 0, B_TRUE);
5050 list_destroy(remdev->l2ad_buflist);
5051 kmem_free(remdev->l2ad_buflist, sizeof (list_t));
5052 kmem_free(remdev, sizeof (l2arc_dev_t));
5058 l2arc_thread_exit = 0;
5060 l2arc_writes_sent = 0;
5061 l2arc_writes_done = 0;
5063 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
5064 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
5065 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
5066 mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
5067 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
5069 l2arc_dev_list = &L2ARC_dev_list;
5070 l2arc_free_on_write = &L2ARC_free_on_write;
5071 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
5072 offsetof(l2arc_dev_t, l2ad_node));
5073 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
5074 offsetof(l2arc_data_free_t, l2df_list_node));
5081 * This is called from dmu_fini(), which is called from spa_fini();
5082 * Because of this, we can assume that all l2arc devices have
5083 * already been removed when the pools themselves were removed.
5086 l2arc_do_free_on_write();
5088 mutex_destroy(&l2arc_feed_thr_lock);
5089 cv_destroy(&l2arc_feed_thr_cv);
5090 mutex_destroy(&l2arc_dev_mtx);
5091 mutex_destroy(&l2arc_buflist_mtx);
5092 mutex_destroy(&l2arc_free_on_write_mtx);
5094 list_destroy(l2arc_dev_list);
5095 list_destroy(l2arc_free_on_write);
5101 if (!(spa_mode_global & FWRITE))
5104 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
5105 TS_RUN, minclsyspri);
5111 if (!(spa_mode_global & FWRITE))
5114 mutex_enter(&l2arc_feed_thr_lock);
5115 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
5116 l2arc_thread_exit = 1;
5117 while (l2arc_thread_exit != 0)
5118 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
5119 mutex_exit(&l2arc_feed_thr_lock);