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/vmsystm.h>
131 #include <sys/fs/swapnode.h>
132 #include <sys/dnlc.h>
134 #include <sys/callb.h>
135 #include <sys/kstat.h>
136 #include <zfs_fletcher.h>
138 static kmutex_t arc_reclaim_thr_lock
;
139 static kcondvar_t arc_reclaim_thr_cv
; /* used to signal reclaim thr */
140 static uint8_t arc_thread_exit
;
142 extern int zfs_write_limit_shift
;
143 extern uint64_t zfs_write_limit_max
;
144 extern kmutex_t zfs_write_limit_lock
;
146 #define ARC_REDUCE_DNLC_PERCENT 3
147 uint_t arc_reduce_dnlc_percent
= ARC_REDUCE_DNLC_PERCENT
;
149 typedef enum arc_reclaim_strategy
{
150 ARC_RECLAIM_AGGR
, /* Aggressive reclaim strategy */
151 ARC_RECLAIM_CONS
/* Conservative reclaim strategy */
152 } arc_reclaim_strategy_t
;
154 /* number of seconds before growing cache again */
155 static int arc_grow_retry
= 60;
157 /* shift of arc_c for calculating both min and max arc_p */
158 static int arc_p_min_shift
= 4;
160 /* log2(fraction of arc to reclaim) */
161 static int arc_shrink_shift
= 5;
164 * minimum lifespan of a prefetch block in clock ticks
165 * (initialized in arc_init())
167 static int arc_min_prefetch_lifespan
;
172 * The arc has filled available memory and has now warmed up.
174 static boolean_t arc_warm
;
177 * These tunables are for performance analysis.
179 uint64_t zfs_arc_max
;
180 uint64_t zfs_arc_min
;
181 uint64_t zfs_arc_meta_limit
= 0;
182 int zfs_arc_grow_retry
= 0;
183 int zfs_arc_shrink_shift
= 0;
184 int zfs_arc_p_min_shift
= 0;
187 * Note that buffers can be in one of 6 states:
188 * ARC_anon - anonymous (discussed below)
189 * ARC_mru - recently used, currently cached
190 * ARC_mru_ghost - recentely used, no longer in cache
191 * ARC_mfu - frequently used, currently cached
192 * ARC_mfu_ghost - frequently used, no longer in cache
193 * ARC_l2c_only - exists in L2ARC but not other states
194 * When there are no active references to the buffer, they are
195 * are linked onto a list in one of these arc states. These are
196 * the only buffers that can be evicted or deleted. Within each
197 * state there are multiple lists, one for meta-data and one for
198 * non-meta-data. Meta-data (indirect blocks, blocks of dnodes,
199 * etc.) is tracked separately so that it can be managed more
200 * explicitly: favored over data, limited explicitly.
202 * Anonymous buffers are buffers that are not associated with
203 * a DVA. These are buffers that hold dirty block copies
204 * before they are written to stable storage. By definition,
205 * they are "ref'd" and are considered part of arc_mru
206 * that cannot be freed. Generally, they will aquire a DVA
207 * as they are written and migrate onto the arc_mru list.
209 * The ARC_l2c_only state is for buffers that are in the second
210 * level ARC but no longer in any of the ARC_m* lists. The second
211 * level ARC itself may also contain buffers that are in any of
212 * the ARC_m* states - meaning that a buffer can exist in two
213 * places. The reason for the ARC_l2c_only state is to keep the
214 * buffer header in the hash table, so that reads that hit the
215 * second level ARC benefit from these fast lookups.
218 typedef struct arc_state
{
219 list_t arcs_list
[ARC_BUFC_NUMTYPES
]; /* list of evictable buffers */
220 uint64_t arcs_lsize
[ARC_BUFC_NUMTYPES
]; /* amount of evictable data */
221 uint64_t arcs_size
; /* total amount of data in this state */
226 static arc_state_t ARC_anon
;
227 static arc_state_t ARC_mru
;
228 static arc_state_t ARC_mru_ghost
;
229 static arc_state_t ARC_mfu
;
230 static arc_state_t ARC_mfu_ghost
;
231 static arc_state_t ARC_l2c_only
;
233 typedef struct arc_stats
{
234 kstat_named_t arcstat_hits
;
235 kstat_named_t arcstat_misses
;
236 kstat_named_t arcstat_demand_data_hits
;
237 kstat_named_t arcstat_demand_data_misses
;
238 kstat_named_t arcstat_demand_metadata_hits
;
239 kstat_named_t arcstat_demand_metadata_misses
;
240 kstat_named_t arcstat_prefetch_data_hits
;
241 kstat_named_t arcstat_prefetch_data_misses
;
242 kstat_named_t arcstat_prefetch_metadata_hits
;
243 kstat_named_t arcstat_prefetch_metadata_misses
;
244 kstat_named_t arcstat_mru_hits
;
245 kstat_named_t arcstat_mru_ghost_hits
;
246 kstat_named_t arcstat_mfu_hits
;
247 kstat_named_t arcstat_mfu_ghost_hits
;
248 kstat_named_t arcstat_deleted
;
249 kstat_named_t arcstat_recycle_miss
;
250 kstat_named_t arcstat_mutex_miss
;
251 kstat_named_t arcstat_evict_skip
;
252 kstat_named_t arcstat_evict_l2_cached
;
253 kstat_named_t arcstat_evict_l2_eligible
;
254 kstat_named_t arcstat_evict_l2_ineligible
;
255 kstat_named_t arcstat_hash_elements
;
256 kstat_named_t arcstat_hash_elements_max
;
257 kstat_named_t arcstat_hash_collisions
;
258 kstat_named_t arcstat_hash_chains
;
259 kstat_named_t arcstat_hash_chain_max
;
260 kstat_named_t arcstat_p
;
261 kstat_named_t arcstat_c
;
262 kstat_named_t arcstat_c_min
;
263 kstat_named_t arcstat_c_max
;
264 kstat_named_t arcstat_size
;
265 kstat_named_t arcstat_hdr_size
;
266 kstat_named_t arcstat_data_size
;
267 kstat_named_t arcstat_other_size
;
268 kstat_named_t arcstat_l2_hits
;
269 kstat_named_t arcstat_l2_misses
;
270 kstat_named_t arcstat_l2_feeds
;
271 kstat_named_t arcstat_l2_rw_clash
;
272 kstat_named_t arcstat_l2_read_bytes
;
273 kstat_named_t arcstat_l2_write_bytes
;
274 kstat_named_t arcstat_l2_writes_sent
;
275 kstat_named_t arcstat_l2_writes_done
;
276 kstat_named_t arcstat_l2_writes_error
;
277 kstat_named_t arcstat_l2_writes_hdr_miss
;
278 kstat_named_t arcstat_l2_evict_lock_retry
;
279 kstat_named_t arcstat_l2_evict_reading
;
280 kstat_named_t arcstat_l2_free_on_write
;
281 kstat_named_t arcstat_l2_abort_lowmem
;
282 kstat_named_t arcstat_l2_cksum_bad
;
283 kstat_named_t arcstat_l2_io_error
;
284 kstat_named_t arcstat_l2_size
;
285 kstat_named_t arcstat_l2_hdr_size
;
286 kstat_named_t arcstat_memory_throttle_count
;
289 static arc_stats_t arc_stats
= {
290 { "hits", KSTAT_DATA_UINT64
},
291 { "misses", KSTAT_DATA_UINT64
},
292 { "demand_data_hits", KSTAT_DATA_UINT64
},
293 { "demand_data_misses", KSTAT_DATA_UINT64
},
294 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
295 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
296 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
297 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
298 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
299 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
300 { "mru_hits", KSTAT_DATA_UINT64
},
301 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
302 { "mfu_hits", KSTAT_DATA_UINT64
},
303 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
304 { "deleted", KSTAT_DATA_UINT64
},
305 { "recycle_miss", KSTAT_DATA_UINT64
},
306 { "mutex_miss", KSTAT_DATA_UINT64
},
307 { "evict_skip", KSTAT_DATA_UINT64
},
308 { "evict_l2_cached", KSTAT_DATA_UINT64
},
309 { "evict_l2_eligible", KSTAT_DATA_UINT64
},
310 { "evict_l2_ineligible", KSTAT_DATA_UINT64
},
311 { "hash_elements", KSTAT_DATA_UINT64
},
312 { "hash_elements_max", KSTAT_DATA_UINT64
},
313 { "hash_collisions", KSTAT_DATA_UINT64
},
314 { "hash_chains", KSTAT_DATA_UINT64
},
315 { "hash_chain_max", KSTAT_DATA_UINT64
},
316 { "p", KSTAT_DATA_UINT64
},
317 { "c", KSTAT_DATA_UINT64
},
318 { "c_min", KSTAT_DATA_UINT64
},
319 { "c_max", KSTAT_DATA_UINT64
},
320 { "size", KSTAT_DATA_UINT64
},
321 { "hdr_size", KSTAT_DATA_UINT64
},
322 { "data_size", KSTAT_DATA_UINT64
},
323 { "other_size", KSTAT_DATA_UINT64
},
324 { "l2_hits", KSTAT_DATA_UINT64
},
325 { "l2_misses", KSTAT_DATA_UINT64
},
326 { "l2_feeds", KSTAT_DATA_UINT64
},
327 { "l2_rw_clash", KSTAT_DATA_UINT64
},
328 { "l2_read_bytes", KSTAT_DATA_UINT64
},
329 { "l2_write_bytes", KSTAT_DATA_UINT64
},
330 { "l2_writes_sent", KSTAT_DATA_UINT64
},
331 { "l2_writes_done", KSTAT_DATA_UINT64
},
332 { "l2_writes_error", KSTAT_DATA_UINT64
},
333 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64
},
334 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
335 { "l2_evict_reading", KSTAT_DATA_UINT64
},
336 { "l2_free_on_write", KSTAT_DATA_UINT64
},
337 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
338 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
339 { "l2_io_error", KSTAT_DATA_UINT64
},
340 { "l2_size", KSTAT_DATA_UINT64
},
341 { "l2_hdr_size", KSTAT_DATA_UINT64
},
342 { "memory_throttle_count", KSTAT_DATA_UINT64
}
345 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
347 #define ARCSTAT_INCR(stat, val) \
348 atomic_add_64(&arc_stats.stat.value.ui64, (val));
350 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
351 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
353 #define ARCSTAT_MAX(stat, val) { \
355 while ((val) > (m = arc_stats.stat.value.ui64) && \
356 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
360 #define ARCSTAT_MAXSTAT(stat) \
361 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
364 * We define a macro to allow ARC hits/misses to be easily broken down by
365 * two separate conditions, giving a total of four different subtypes for
366 * each of hits and misses (so eight statistics total).
368 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
371 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
373 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
377 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
379 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
384 static arc_state_t
*arc_anon
;
385 static arc_state_t
*arc_mru
;
386 static arc_state_t
*arc_mru_ghost
;
387 static arc_state_t
*arc_mfu
;
388 static arc_state_t
*arc_mfu_ghost
;
389 static arc_state_t
*arc_l2c_only
;
392 * There are several ARC variables that are critical to export as kstats --
393 * but we don't want to have to grovel around in the kstat whenever we wish to
394 * manipulate them. For these variables, we therefore define them to be in
395 * terms of the statistic variable. This assures that we are not introducing
396 * the possibility of inconsistency by having shadow copies of the variables,
397 * while still allowing the code to be readable.
399 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
400 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
401 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
402 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
403 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
405 static int arc_no_grow
; /* Don't try to grow cache size */
406 static uint64_t arc_tempreserve
;
407 static uint64_t arc_loaned_bytes
;
408 static uint64_t arc_meta_used
;
409 static uint64_t arc_meta_limit
;
410 static uint64_t arc_meta_max
= 0;
412 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t
;
414 typedef struct arc_callback arc_callback_t
;
416 struct arc_callback
{
418 arc_done_func_t
*acb_done
;
420 zio_t
*acb_zio_dummy
;
421 arc_callback_t
*acb_next
;
424 typedef struct arc_write_callback arc_write_callback_t
;
426 struct arc_write_callback
{
428 arc_done_func_t
*awcb_ready
;
429 arc_done_func_t
*awcb_done
;
434 /* protected by hash lock */
439 kmutex_t b_freeze_lock
;
440 zio_cksum_t
*b_freeze_cksum
;
443 arc_buf_hdr_t
*b_hash_next
;
448 arc_callback_t
*b_acb
;
452 arc_buf_contents_t b_type
;
456 /* protected by arc state mutex */
457 arc_state_t
*b_state
;
458 list_node_t b_arc_node
;
460 /* updated atomically */
461 clock_t b_arc_access
;
463 /* self protecting */
466 l2arc_buf_hdr_t
*b_l2hdr
;
467 list_node_t b_l2node
;
470 static arc_buf_t
*arc_eviction_list
;
471 static kmutex_t arc_eviction_mtx
;
472 static arc_buf_hdr_t arc_eviction_hdr
;
473 static void arc_get_data_buf(arc_buf_t
*buf
);
474 static void arc_access(arc_buf_hdr_t
*buf
, kmutex_t
*hash_lock
);
475 static int arc_evict_needed(arc_buf_contents_t type
);
476 static void arc_evict_ghost(arc_state_t
*state
, uint64_t spa
, int64_t bytes
);
478 static boolean_t
l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*ab
);
480 #define GHOST_STATE(state) \
481 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
482 (state) == arc_l2c_only)
485 * Private ARC flags. These flags are private ARC only flags that will show up
486 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
487 * be passed in as arc_flags in things like arc_read. However, these flags
488 * should never be passed and should only be set by ARC code. When adding new
489 * public flags, make sure not to smash the private ones.
492 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
493 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
494 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
495 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
496 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
497 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
498 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
499 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
500 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
501 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
503 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
504 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
505 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
506 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
507 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
508 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
509 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
510 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
511 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
512 (hdr)->b_l2hdr != NULL)
513 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
514 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
515 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
521 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
522 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
525 * Hash table routines
528 #define HT_LOCK_PAD 64
533 unsigned char pad
[(HT_LOCK_PAD
- sizeof (kmutex_t
))];
537 #define BUF_LOCKS 256
538 typedef struct buf_hash_table
{
540 arc_buf_hdr_t
**ht_table
;
541 struct ht_lock ht_locks
[BUF_LOCKS
];
544 static buf_hash_table_t buf_hash_table
;
546 #define BUF_HASH_INDEX(spa, dva, birth) \
547 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
548 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
549 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
550 #define HDR_LOCK(hdr) \
551 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
553 uint64_t zfs_crc64_table
[256];
559 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
560 #define L2ARC_HEADROOM 2 /* num of writes */
561 #define L2ARC_FEED_SECS 1 /* caching interval secs */
562 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
564 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
565 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
568 * L2ARC Performance Tunables
570 uint64_t l2arc_write_max
= L2ARC_WRITE_SIZE
; /* default max write size */
571 uint64_t l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra write during warmup */
572 uint64_t l2arc_headroom
= L2ARC_HEADROOM
; /* number of dev writes */
573 uint64_t l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
574 uint64_t l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval milliseconds */
575 boolean_t l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
576 boolean_t l2arc_feed_again
= B_TRUE
; /* turbo warmup */
577 boolean_t l2arc_norw
= B_TRUE
; /* no reads during writes */
582 typedef struct l2arc_dev
{
583 vdev_t
*l2ad_vdev
; /* vdev */
584 spa_t
*l2ad_spa
; /* spa */
585 uint64_t l2ad_hand
; /* next write location */
586 uint64_t l2ad_write
; /* desired write size, bytes */
587 uint64_t l2ad_boost
; /* warmup write boost, bytes */
588 uint64_t l2ad_start
; /* first addr on device */
589 uint64_t l2ad_end
; /* last addr on device */
590 uint64_t l2ad_evict
; /* last addr eviction reached */
591 boolean_t l2ad_first
; /* first sweep through */
592 boolean_t l2ad_writing
; /* currently writing */
593 list_t
*l2ad_buflist
; /* buffer list */
594 list_node_t l2ad_node
; /* device list node */
597 static list_t L2ARC_dev_list
; /* device list */
598 static list_t
*l2arc_dev_list
; /* device list pointer */
599 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
600 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
601 static kmutex_t l2arc_buflist_mtx
; /* mutex for all buflists */
602 static list_t L2ARC_free_on_write
; /* free after write buf list */
603 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
604 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
605 static uint64_t l2arc_ndev
; /* number of devices */
607 typedef struct l2arc_read_callback
{
608 arc_buf_t
*l2rcb_buf
; /* read buffer */
609 spa_t
*l2rcb_spa
; /* spa */
610 blkptr_t l2rcb_bp
; /* original blkptr */
611 zbookmark_t l2rcb_zb
; /* original bookmark */
612 int l2rcb_flags
; /* original flags */
613 } l2arc_read_callback_t
;
615 typedef struct l2arc_write_callback
{
616 l2arc_dev_t
*l2wcb_dev
; /* device info */
617 arc_buf_hdr_t
*l2wcb_head
; /* head of write buflist */
618 } l2arc_write_callback_t
;
620 struct l2arc_buf_hdr
{
621 /* protected by arc_buf_hdr mutex */
622 l2arc_dev_t
*b_dev
; /* L2ARC device */
623 uint64_t b_daddr
; /* disk address, offset byte */
626 typedef struct l2arc_data_free
{
627 /* protected by l2arc_free_on_write_mtx */
630 void (*l2df_func
)(void *, size_t);
631 list_node_t l2df_list_node
;
634 static kmutex_t l2arc_feed_thr_lock
;
635 static kcondvar_t l2arc_feed_thr_cv
;
636 static uint8_t l2arc_thread_exit
;
638 static void l2arc_read_done(zio_t
*zio
);
639 static void l2arc_hdr_stat_add(void);
640 static void l2arc_hdr_stat_remove(void);
643 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
645 uint8_t *vdva
= (uint8_t *)dva
;
646 uint64_t crc
= -1ULL;
649 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
651 for (i
= 0; i
< sizeof (dva_t
); i
++)
652 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ vdva
[i
]) & 0xFF];
654 crc
^= (spa
>>8) ^ birth
;
659 #define BUF_EMPTY(buf) \
660 ((buf)->b_dva.dva_word[0] == 0 && \
661 (buf)->b_dva.dva_word[1] == 0 && \
664 #define BUF_EQUAL(spa, dva, birth, buf) \
665 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
666 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
667 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
670 buf_discard_identity(arc_buf_hdr_t
*hdr
)
672 hdr
->b_dva
.dva_word
[0] = 0;
673 hdr
->b_dva
.dva_word
[1] = 0;
678 static arc_buf_hdr_t
*
679 buf_hash_find(uint64_t spa
, const dva_t
*dva
, uint64_t birth
, kmutex_t
**lockp
)
681 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
682 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
685 mutex_enter(hash_lock
);
686 for (buf
= buf_hash_table
.ht_table
[idx
]; buf
!= NULL
;
687 buf
= buf
->b_hash_next
) {
688 if (BUF_EQUAL(spa
, dva
, birth
, buf
)) {
693 mutex_exit(hash_lock
);
699 * Insert an entry into the hash table. If there is already an element
700 * equal to elem in the hash table, then the already existing element
701 * will be returned and the new element will not be inserted.
702 * Otherwise returns NULL.
704 static arc_buf_hdr_t
*
705 buf_hash_insert(arc_buf_hdr_t
*buf
, kmutex_t
**lockp
)
707 uint64_t idx
= BUF_HASH_INDEX(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
);
708 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
712 ASSERT(!HDR_IN_HASH_TABLE(buf
));
714 mutex_enter(hash_lock
);
715 for (fbuf
= buf_hash_table
.ht_table
[idx
], i
= 0; fbuf
!= NULL
;
716 fbuf
= fbuf
->b_hash_next
, i
++) {
717 if (BUF_EQUAL(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
, fbuf
))
721 buf
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
722 buf_hash_table
.ht_table
[idx
] = buf
;
723 buf
->b_flags
|= ARC_IN_HASH_TABLE
;
725 /* collect some hash table performance data */
727 ARCSTAT_BUMP(arcstat_hash_collisions
);
729 ARCSTAT_BUMP(arcstat_hash_chains
);
731 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
734 ARCSTAT_BUMP(arcstat_hash_elements
);
735 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
741 buf_hash_remove(arc_buf_hdr_t
*buf
)
743 arc_buf_hdr_t
*fbuf
, **bufp
;
744 uint64_t idx
= BUF_HASH_INDEX(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
);
746 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
747 ASSERT(HDR_IN_HASH_TABLE(buf
));
749 bufp
= &buf_hash_table
.ht_table
[idx
];
750 while ((fbuf
= *bufp
) != buf
) {
751 ASSERT(fbuf
!= NULL
);
752 bufp
= &fbuf
->b_hash_next
;
754 *bufp
= buf
->b_hash_next
;
755 buf
->b_hash_next
= NULL
;
756 buf
->b_flags
&= ~ARC_IN_HASH_TABLE
;
758 /* collect some hash table performance data */
759 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
761 if (buf_hash_table
.ht_table
[idx
] &&
762 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
763 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
767 * Global data structures and functions for the buf kmem cache.
769 static kmem_cache_t
*hdr_cache
;
770 static kmem_cache_t
*buf_cache
;
777 kmem_free(buf_hash_table
.ht_table
,
778 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
779 for (i
= 0; i
< BUF_LOCKS
; i
++)
780 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
781 kmem_cache_destroy(hdr_cache
);
782 kmem_cache_destroy(buf_cache
);
786 * Constructor callback - called when the cache is empty
787 * and a new buf is requested.
791 hdr_cons(void *vbuf
, void *unused
, int kmflag
)
793 arc_buf_hdr_t
*buf
= vbuf
;
795 bzero(buf
, sizeof (arc_buf_hdr_t
));
796 refcount_create(&buf
->b_refcnt
);
797 cv_init(&buf
->b_cv
, NULL
, CV_DEFAULT
, NULL
);
798 mutex_init(&buf
->b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
799 arc_space_consume(sizeof (arc_buf_hdr_t
), ARC_SPACE_HDRS
);
806 buf_cons(void *vbuf
, void *unused
, int kmflag
)
808 arc_buf_t
*buf
= vbuf
;
810 bzero(buf
, sizeof (arc_buf_t
));
811 mutex_init(&buf
->b_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
812 rw_init(&buf
->b_data_lock
, NULL
, RW_DEFAULT
, NULL
);
813 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
819 * Destructor callback - called when a cached buf is
820 * no longer required.
824 hdr_dest(void *vbuf
, void *unused
)
826 arc_buf_hdr_t
*buf
= vbuf
;
828 ASSERT(BUF_EMPTY(buf
));
829 refcount_destroy(&buf
->b_refcnt
);
830 cv_destroy(&buf
->b_cv
);
831 mutex_destroy(&buf
->b_freeze_lock
);
832 arc_space_return(sizeof (arc_buf_hdr_t
), ARC_SPACE_HDRS
);
837 buf_dest(void *vbuf
, void *unused
)
839 arc_buf_t
*buf
= vbuf
;
841 mutex_destroy(&buf
->b_evict_lock
);
842 rw_destroy(&buf
->b_data_lock
);
843 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
847 * Reclaim callback -- invoked when memory is low.
851 hdr_recl(void *unused
)
853 dprintf("hdr_recl called\n");
855 * umem calls the reclaim func when we destroy the buf cache,
856 * which is after we do arc_fini().
859 cv_signal(&arc_reclaim_thr_cv
);
866 uint64_t hsize
= 1ULL << 12;
870 * The hash table is big enough to fill all of physical memory
871 * with an average 64K block size. The table will take up
872 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
874 while (hsize
* 65536 < physmem
* PAGESIZE
)
877 buf_hash_table
.ht_mask
= hsize
- 1;
878 buf_hash_table
.ht_table
=
879 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
880 if (buf_hash_table
.ht_table
== NULL
) {
881 ASSERT(hsize
> (1ULL << 8));
886 hdr_cache
= kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t
),
887 0, hdr_cons
, hdr_dest
, hdr_recl
, NULL
, NULL
, 0);
888 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
889 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
891 for (i
= 0; i
< 256; i
++)
892 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
893 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
895 for (i
= 0; i
< BUF_LOCKS
; i
++) {
896 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
897 NULL
, MUTEX_DEFAULT
, NULL
);
901 #define ARC_MINTIME (hz>>4) /* 62 ms */
904 arc_cksum_verify(arc_buf_t
*buf
)
908 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
911 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
912 if (buf
->b_hdr
->b_freeze_cksum
== NULL
||
913 (buf
->b_hdr
->b_flags
& ARC_IO_ERROR
)) {
914 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
917 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
918 if (!ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
))
919 panic("buffer modified while frozen!");
920 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
924 arc_cksum_equal(arc_buf_t
*buf
)
929 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
930 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
931 equal
= ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
);
932 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
938 arc_cksum_compute(arc_buf_t
*buf
, boolean_t force
)
940 if (!force
&& !(zfs_flags
& ZFS_DEBUG_MODIFY
))
943 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
944 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
945 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
948 buf
->b_hdr
->b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
), KM_SLEEP
);
949 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
,
950 buf
->b_hdr
->b_freeze_cksum
);
951 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
955 arc_buf_thaw(arc_buf_t
*buf
)
957 if (zfs_flags
& ZFS_DEBUG_MODIFY
) {
958 if (buf
->b_hdr
->b_state
!= arc_anon
)
959 panic("modifying non-anon buffer!");
960 if (buf
->b_hdr
->b_flags
& ARC_IO_IN_PROGRESS
)
961 panic("modifying buffer while i/o in progress!");
962 arc_cksum_verify(buf
);
965 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
966 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
967 kmem_free(buf
->b_hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
968 buf
->b_hdr
->b_freeze_cksum
= NULL
;
971 if (zfs_flags
& ZFS_DEBUG_MODIFY
) {
972 if (buf
->b_hdr
->b_thawed
)
973 kmem_free(buf
->b_hdr
->b_thawed
, 1);
974 buf
->b_hdr
->b_thawed
= kmem_alloc(1, KM_SLEEP
);
977 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
981 arc_buf_freeze(arc_buf_t
*buf
)
985 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
988 hash_lock
= HDR_LOCK(buf
->b_hdr
);
989 mutex_enter(hash_lock
);
991 ASSERT(buf
->b_hdr
->b_freeze_cksum
!= NULL
||
992 buf
->b_hdr
->b_state
== arc_anon
);
993 arc_cksum_compute(buf
, B_FALSE
);
994 mutex_exit(hash_lock
);
998 add_reference(arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
, void *tag
)
1000 ASSERT(MUTEX_HELD(hash_lock
));
1002 if ((refcount_add(&ab
->b_refcnt
, tag
) == 1) &&
1003 (ab
->b_state
!= arc_anon
)) {
1004 uint64_t delta
= ab
->b_size
* ab
->b_datacnt
;
1005 list_t
*list
= &ab
->b_state
->arcs_list
[ab
->b_type
];
1006 uint64_t *size
= &ab
->b_state
->arcs_lsize
[ab
->b_type
];
1008 ASSERT(!MUTEX_HELD(&ab
->b_state
->arcs_mtx
));
1009 mutex_enter(&ab
->b_state
->arcs_mtx
);
1010 ASSERT(list_link_active(&ab
->b_arc_node
));
1011 list_remove(list
, ab
);
1012 if (GHOST_STATE(ab
->b_state
)) {
1013 ASSERT3U(ab
->b_datacnt
, ==, 0);
1014 ASSERT3P(ab
->b_buf
, ==, NULL
);
1018 ASSERT3U(*size
, >=, delta
);
1019 atomic_add_64(size
, -delta
);
1020 mutex_exit(&ab
->b_state
->arcs_mtx
);
1021 /* remove the prefetch flag if we get a reference */
1022 if (ab
->b_flags
& ARC_PREFETCH
)
1023 ab
->b_flags
&= ~ARC_PREFETCH
;
1028 remove_reference(arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
, void *tag
)
1031 arc_state_t
*state
= ab
->b_state
;
1033 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
1034 ASSERT(!GHOST_STATE(state
));
1036 if (((cnt
= refcount_remove(&ab
->b_refcnt
, tag
)) == 0) &&
1037 (state
!= arc_anon
)) {
1038 uint64_t *size
= &state
->arcs_lsize
[ab
->b_type
];
1040 ASSERT(!MUTEX_HELD(&state
->arcs_mtx
));
1041 mutex_enter(&state
->arcs_mtx
);
1042 ASSERT(!list_link_active(&ab
->b_arc_node
));
1043 list_insert_head(&state
->arcs_list
[ab
->b_type
], ab
);
1044 ASSERT(ab
->b_datacnt
> 0);
1045 atomic_add_64(size
, ab
->b_size
* ab
->b_datacnt
);
1046 mutex_exit(&state
->arcs_mtx
);
1052 * Move the supplied buffer to the indicated state. The mutex
1053 * for the buffer must be held by the caller.
1056 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
)
1058 arc_state_t
*old_state
= ab
->b_state
;
1059 int64_t refcnt
= refcount_count(&ab
->b_refcnt
);
1060 uint64_t from_delta
, to_delta
;
1062 ASSERT(MUTEX_HELD(hash_lock
));
1063 ASSERT(new_state
!= old_state
);
1064 ASSERT(refcnt
== 0 || ab
->b_datacnt
> 0);
1065 ASSERT(ab
->b_datacnt
== 0 || !GHOST_STATE(new_state
));
1066 ASSERT(ab
->b_datacnt
<= 1 || old_state
!= arc_anon
);
1068 from_delta
= to_delta
= ab
->b_datacnt
* ab
->b_size
;
1071 * If this buffer is evictable, transfer it from the
1072 * old state list to the new state list.
1075 if (old_state
!= arc_anon
) {
1076 int use_mutex
= !MUTEX_HELD(&old_state
->arcs_mtx
);
1077 uint64_t *size
= &old_state
->arcs_lsize
[ab
->b_type
];
1080 mutex_enter(&old_state
->arcs_mtx
);
1082 ASSERT(list_link_active(&ab
->b_arc_node
));
1083 list_remove(&old_state
->arcs_list
[ab
->b_type
], ab
);
1086 * If prefetching out of the ghost cache,
1087 * we will have a non-zero datacnt.
1089 if (GHOST_STATE(old_state
) && ab
->b_datacnt
== 0) {
1090 /* ghost elements have a ghost size */
1091 ASSERT(ab
->b_buf
== NULL
);
1092 from_delta
= ab
->b_size
;
1094 ASSERT3U(*size
, >=, from_delta
);
1095 atomic_add_64(size
, -from_delta
);
1098 mutex_exit(&old_state
->arcs_mtx
);
1100 if (new_state
!= arc_anon
) {
1101 int use_mutex
= !MUTEX_HELD(&new_state
->arcs_mtx
);
1102 uint64_t *size
= &new_state
->arcs_lsize
[ab
->b_type
];
1105 mutex_enter(&new_state
->arcs_mtx
);
1107 list_insert_head(&new_state
->arcs_list
[ab
->b_type
], ab
);
1109 /* ghost elements have a ghost size */
1110 if (GHOST_STATE(new_state
)) {
1111 ASSERT(ab
->b_datacnt
== 0);
1112 ASSERT(ab
->b_buf
== NULL
);
1113 to_delta
= ab
->b_size
;
1115 atomic_add_64(size
, to_delta
);
1118 mutex_exit(&new_state
->arcs_mtx
);
1122 ASSERT(!BUF_EMPTY(ab
));
1123 if (new_state
== arc_anon
&& HDR_IN_HASH_TABLE(ab
))
1124 buf_hash_remove(ab
);
1126 /* adjust state sizes */
1128 atomic_add_64(&new_state
->arcs_size
, to_delta
);
1130 ASSERT3U(old_state
->arcs_size
, >=, from_delta
);
1131 atomic_add_64(&old_state
->arcs_size
, -from_delta
);
1133 ab
->b_state
= new_state
;
1135 /* adjust l2arc hdr stats */
1136 if (new_state
== arc_l2c_only
)
1137 l2arc_hdr_stat_add();
1138 else if (old_state
== arc_l2c_only
)
1139 l2arc_hdr_stat_remove();
1143 arc_space_consume(uint64_t space
, arc_space_type_t type
)
1145 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1148 case ARC_SPACE_DATA
:
1149 ARCSTAT_INCR(arcstat_data_size
, space
);
1151 case ARC_SPACE_OTHER
:
1152 ARCSTAT_INCR(arcstat_other_size
, space
);
1154 case ARC_SPACE_HDRS
:
1155 ARCSTAT_INCR(arcstat_hdr_size
, space
);
1157 case ARC_SPACE_L2HDRS
:
1158 ARCSTAT_INCR(arcstat_l2_hdr_size
, space
);
1162 atomic_add_64(&arc_meta_used
, space
);
1163 atomic_add_64(&arc_size
, space
);
1167 arc_space_return(uint64_t space
, arc_space_type_t type
)
1169 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1172 case ARC_SPACE_DATA
:
1173 ARCSTAT_INCR(arcstat_data_size
, -space
);
1175 case ARC_SPACE_OTHER
:
1176 ARCSTAT_INCR(arcstat_other_size
, -space
);
1178 case ARC_SPACE_HDRS
:
1179 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
1181 case ARC_SPACE_L2HDRS
:
1182 ARCSTAT_INCR(arcstat_l2_hdr_size
, -space
);
1186 ASSERT(arc_meta_used
>= space
);
1187 if (arc_meta_max
< arc_meta_used
)
1188 arc_meta_max
= arc_meta_used
;
1189 atomic_add_64(&arc_meta_used
, -space
);
1190 ASSERT(arc_size
>= space
);
1191 atomic_add_64(&arc_size
, -space
);
1195 arc_data_buf_alloc(uint64_t size
)
1197 if (arc_evict_needed(ARC_BUFC_DATA
))
1198 cv_signal(&arc_reclaim_thr_cv
);
1199 atomic_add_64(&arc_size
, size
);
1200 return (zio_data_buf_alloc(size
));
1204 arc_data_buf_free(void *buf
, uint64_t size
)
1206 zio_data_buf_free(buf
, size
);
1207 ASSERT(arc_size
>= size
);
1208 atomic_add_64(&arc_size
, -size
);
1212 arc_buf_alloc(spa_t
*spa
, int size
, void *tag
, arc_buf_contents_t type
)
1217 ASSERT3U(size
, >, 0);
1218 hdr
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
1219 ASSERT(BUF_EMPTY(hdr
));
1222 hdr
->b_spa
= spa_load_guid(spa
);
1223 hdr
->b_state
= arc_anon
;
1224 hdr
->b_arc_access
= 0;
1225 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1228 buf
->b_efunc
= NULL
;
1229 buf
->b_private
= NULL
;
1232 arc_get_data_buf(buf
);
1235 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1236 (void) refcount_add(&hdr
->b_refcnt
, tag
);
1241 static char *arc_onloan_tag
= "onloan";
1244 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1245 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1246 * buffers must be returned to the arc before they can be used by the DMU or
1250 arc_loan_buf(spa_t
*spa
, int size
)
1254 buf
= arc_buf_alloc(spa
, size
, arc_onloan_tag
, ARC_BUFC_DATA
);
1256 atomic_add_64(&arc_loaned_bytes
, size
);
1261 * Return a loaned arc buffer to the arc.
1264 arc_return_buf(arc_buf_t
*buf
, void *tag
)
1266 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1268 ASSERT(buf
->b_data
!= NULL
);
1269 (void) refcount_add(&hdr
->b_refcnt
, tag
);
1270 (void) refcount_remove(&hdr
->b_refcnt
, arc_onloan_tag
);
1272 atomic_add_64(&arc_loaned_bytes
, -hdr
->b_size
);
1275 /* Detach an arc_buf from a dbuf (tag) */
1277 arc_loan_inuse_buf(arc_buf_t
*buf
, void *tag
)
1281 ASSERT(buf
->b_data
!= NULL
);
1283 (void) refcount_add(&hdr
->b_refcnt
, arc_onloan_tag
);
1284 (void) refcount_remove(&hdr
->b_refcnt
, tag
);
1285 buf
->b_efunc
= NULL
;
1286 buf
->b_private
= NULL
;
1288 atomic_add_64(&arc_loaned_bytes
, hdr
->b_size
);
1292 arc_buf_clone(arc_buf_t
*from
)
1295 arc_buf_hdr_t
*hdr
= from
->b_hdr
;
1296 uint64_t size
= hdr
->b_size
;
1298 ASSERT(hdr
->b_state
!= arc_anon
);
1300 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1303 buf
->b_efunc
= NULL
;
1304 buf
->b_private
= NULL
;
1305 buf
->b_next
= hdr
->b_buf
;
1307 arc_get_data_buf(buf
);
1308 bcopy(from
->b_data
, buf
->b_data
, size
);
1309 hdr
->b_datacnt
+= 1;
1314 arc_buf_add_ref(arc_buf_t
*buf
, void* tag
)
1317 kmutex_t
*hash_lock
;
1320 * Check to see if this buffer is evicted. Callers
1321 * must verify b_data != NULL to know if the add_ref
1324 mutex_enter(&buf
->b_evict_lock
);
1325 if (buf
->b_data
== NULL
) {
1326 mutex_exit(&buf
->b_evict_lock
);
1329 hash_lock
= HDR_LOCK(buf
->b_hdr
);
1330 mutex_enter(hash_lock
);
1332 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1333 mutex_exit(&buf
->b_evict_lock
);
1335 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
1336 add_reference(hdr
, hash_lock
, tag
);
1337 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
1338 arc_access(hdr
, hash_lock
);
1339 mutex_exit(hash_lock
);
1340 ARCSTAT_BUMP(arcstat_hits
);
1341 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
1342 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
1343 data
, metadata
, hits
);
1347 * Free the arc data buffer. If it is an l2arc write in progress,
1348 * the buffer is placed on l2arc_free_on_write to be freed later.
1351 arc_buf_data_free(arc_buf_hdr_t
*hdr
, void (*free_func
)(void *, size_t),
1352 void *data
, size_t size
)
1354 if (HDR_L2_WRITING(hdr
)) {
1355 l2arc_data_free_t
*df
;
1356 df
= kmem_alloc(sizeof (l2arc_data_free_t
), KM_SLEEP
);
1357 df
->l2df_data
= data
;
1358 df
->l2df_size
= size
;
1359 df
->l2df_func
= free_func
;
1360 mutex_enter(&l2arc_free_on_write_mtx
);
1361 list_insert_head(l2arc_free_on_write
, df
);
1362 mutex_exit(&l2arc_free_on_write_mtx
);
1363 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
1365 free_func(data
, size
);
1370 arc_buf_destroy(arc_buf_t
*buf
, boolean_t recycle
, boolean_t all
)
1374 /* free up data associated with the buf */
1376 arc_state_t
*state
= buf
->b_hdr
->b_state
;
1377 uint64_t size
= buf
->b_hdr
->b_size
;
1378 arc_buf_contents_t type
= buf
->b_hdr
->b_type
;
1380 arc_cksum_verify(buf
);
1383 if (type
== ARC_BUFC_METADATA
) {
1384 arc_buf_data_free(buf
->b_hdr
, zio_buf_free
,
1386 arc_space_return(size
, ARC_SPACE_DATA
);
1388 ASSERT(type
== ARC_BUFC_DATA
);
1389 arc_buf_data_free(buf
->b_hdr
,
1390 zio_data_buf_free
, buf
->b_data
, size
);
1391 ARCSTAT_INCR(arcstat_data_size
, -size
);
1392 atomic_add_64(&arc_size
, -size
);
1395 if (list_link_active(&buf
->b_hdr
->b_arc_node
)) {
1396 uint64_t *cnt
= &state
->arcs_lsize
[type
];
1398 ASSERT(refcount_is_zero(&buf
->b_hdr
->b_refcnt
));
1399 ASSERT(state
!= arc_anon
);
1401 ASSERT3U(*cnt
, >=, size
);
1402 atomic_add_64(cnt
, -size
);
1404 ASSERT3U(state
->arcs_size
, >=, size
);
1405 atomic_add_64(&state
->arcs_size
, -size
);
1407 ASSERT(buf
->b_hdr
->b_datacnt
> 0);
1408 buf
->b_hdr
->b_datacnt
-= 1;
1411 /* only remove the buf if requested */
1415 /* remove the buf from the hdr list */
1416 for (bufp
= &buf
->b_hdr
->b_buf
; *bufp
!= buf
; bufp
= &(*bufp
)->b_next
)
1418 *bufp
= buf
->b_next
;
1421 ASSERT(buf
->b_efunc
== NULL
);
1423 /* clean up the buf */
1425 kmem_cache_free(buf_cache
, buf
);
1429 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
1431 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1432 ASSERT3P(hdr
->b_state
, ==, arc_anon
);
1433 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1434 l2arc_buf_hdr_t
*l2hdr
= hdr
->b_l2hdr
;
1436 if (l2hdr
!= NULL
) {
1437 boolean_t buflist_held
= MUTEX_HELD(&l2arc_buflist_mtx
);
1439 * To prevent arc_free() and l2arc_evict() from
1440 * attempting to free the same buffer at the same time,
1441 * a FREE_IN_PROGRESS flag is given to arc_free() to
1442 * give it priority. l2arc_evict() can't destroy this
1443 * header while we are waiting on l2arc_buflist_mtx.
1445 * The hdr may be removed from l2ad_buflist before we
1446 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1448 if (!buflist_held
) {
1449 mutex_enter(&l2arc_buflist_mtx
);
1450 l2hdr
= hdr
->b_l2hdr
;
1453 if (l2hdr
!= NULL
) {
1454 list_remove(l2hdr
->b_dev
->l2ad_buflist
, hdr
);
1455 ARCSTAT_INCR(arcstat_l2_size
, -hdr
->b_size
);
1456 kmem_free(l2hdr
, sizeof (l2arc_buf_hdr_t
));
1457 if (hdr
->b_state
== arc_l2c_only
)
1458 l2arc_hdr_stat_remove();
1459 hdr
->b_l2hdr
= NULL
;
1463 mutex_exit(&l2arc_buflist_mtx
);
1466 if (!BUF_EMPTY(hdr
)) {
1467 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1468 buf_discard_identity(hdr
);
1470 while (hdr
->b_buf
) {
1471 arc_buf_t
*buf
= hdr
->b_buf
;
1474 mutex_enter(&arc_eviction_mtx
);
1475 mutex_enter(&buf
->b_evict_lock
);
1476 ASSERT(buf
->b_hdr
!= NULL
);
1477 arc_buf_destroy(hdr
->b_buf
, FALSE
, FALSE
);
1478 hdr
->b_buf
= buf
->b_next
;
1479 buf
->b_hdr
= &arc_eviction_hdr
;
1480 buf
->b_next
= arc_eviction_list
;
1481 arc_eviction_list
= buf
;
1482 mutex_exit(&buf
->b_evict_lock
);
1483 mutex_exit(&arc_eviction_mtx
);
1485 arc_buf_destroy(hdr
->b_buf
, FALSE
, TRUE
);
1488 if (hdr
->b_freeze_cksum
!= NULL
) {
1489 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
1490 hdr
->b_freeze_cksum
= NULL
;
1492 if (hdr
->b_thawed
) {
1493 kmem_free(hdr
->b_thawed
, 1);
1494 hdr
->b_thawed
= NULL
;
1497 ASSERT(!list_link_active(&hdr
->b_arc_node
));
1498 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
1499 ASSERT3P(hdr
->b_acb
, ==, NULL
);
1500 kmem_cache_free(hdr_cache
, hdr
);
1504 arc_buf_free(arc_buf_t
*buf
, void *tag
)
1506 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1507 int hashed
= hdr
->b_state
!= arc_anon
;
1509 ASSERT(buf
->b_efunc
== NULL
);
1510 ASSERT(buf
->b_data
!= NULL
);
1513 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
1515 mutex_enter(hash_lock
);
1517 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1519 (void) remove_reference(hdr
, hash_lock
, tag
);
1520 if (hdr
->b_datacnt
> 1) {
1521 arc_buf_destroy(buf
, FALSE
, TRUE
);
1523 ASSERT(buf
== hdr
->b_buf
);
1524 ASSERT(buf
->b_efunc
== NULL
);
1525 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
1527 mutex_exit(hash_lock
);
1528 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
1531 * We are in the middle of an async write. Don't destroy
1532 * this buffer unless the write completes before we finish
1533 * decrementing the reference count.
1535 mutex_enter(&arc_eviction_mtx
);
1536 (void) remove_reference(hdr
, NULL
, tag
);
1537 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1538 destroy_hdr
= !HDR_IO_IN_PROGRESS(hdr
);
1539 mutex_exit(&arc_eviction_mtx
);
1541 arc_hdr_destroy(hdr
);
1543 if (remove_reference(hdr
, NULL
, tag
) > 0)
1544 arc_buf_destroy(buf
, FALSE
, TRUE
);
1546 arc_hdr_destroy(hdr
);
1551 arc_buf_remove_ref(arc_buf_t
*buf
, void* tag
)
1553 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1554 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
1555 int no_callback
= (buf
->b_efunc
== NULL
);
1557 if (hdr
->b_state
== arc_anon
) {
1558 ASSERT(hdr
->b_datacnt
== 1);
1559 arc_buf_free(buf
, tag
);
1560 return (no_callback
);
1563 mutex_enter(hash_lock
);
1565 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
1566 ASSERT(hdr
->b_state
!= arc_anon
);
1567 ASSERT(buf
->b_data
!= NULL
);
1569 (void) remove_reference(hdr
, hash_lock
, tag
);
1570 if (hdr
->b_datacnt
> 1) {
1572 arc_buf_destroy(buf
, FALSE
, TRUE
);
1573 } else if (no_callback
) {
1574 ASSERT(hdr
->b_buf
== buf
&& buf
->b_next
== NULL
);
1575 ASSERT(buf
->b_efunc
== NULL
);
1576 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
1578 ASSERT(no_callback
|| hdr
->b_datacnt
> 1 ||
1579 refcount_is_zero(&hdr
->b_refcnt
));
1580 mutex_exit(hash_lock
);
1581 return (no_callback
);
1585 arc_buf_size(arc_buf_t
*buf
)
1587 return (buf
->b_hdr
->b_size
);
1591 * Evict buffers from list until we've removed the specified number of
1592 * bytes. Move the removed buffers to the appropriate evict state.
1593 * If the recycle flag is set, then attempt to "recycle" a buffer:
1594 * - look for a buffer to evict that is `bytes' long.
1595 * - return the data block from this buffer rather than freeing it.
1596 * This flag is used by callers that are trying to make space for a
1597 * new buffer in a full arc cache.
1599 * This function makes a "best effort". It skips over any buffers
1600 * it can't get a hash_lock on, and so may not catch all candidates.
1601 * It may also return without evicting as much space as requested.
1604 arc_evict(arc_state_t
*state
, uint64_t spa
, int64_t bytes
, boolean_t recycle
,
1605 arc_buf_contents_t type
)
1607 arc_state_t
*evicted_state
;
1608 uint64_t bytes_evicted
= 0, skipped
= 0, missed
= 0;
1609 arc_buf_hdr_t
*ab
, *ab_prev
= NULL
;
1610 list_t
*list
= &state
->arcs_list
[type
];
1611 kmutex_t
*hash_lock
;
1612 boolean_t have_lock
;
1613 void *stolen
= NULL
;
1615 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
1617 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
1619 mutex_enter(&state
->arcs_mtx
);
1620 mutex_enter(&evicted_state
->arcs_mtx
);
1622 for (ab
= list_tail(list
); ab
; ab
= ab_prev
) {
1623 ab_prev
= list_prev(list
, ab
);
1624 /* prefetch buffers have a minimum lifespan */
1625 if (HDR_IO_IN_PROGRESS(ab
) ||
1626 (spa
&& ab
->b_spa
!= spa
) ||
1627 (ab
->b_flags
& (ARC_PREFETCH
|ARC_INDIRECT
) &&
1628 ddi_get_lbolt() - ab
->b_arc_access
<
1629 arc_min_prefetch_lifespan
)) {
1633 /* "lookahead" for better eviction candidate */
1634 if (recycle
&& ab
->b_size
!= bytes
&&
1635 ab_prev
&& ab_prev
->b_size
== bytes
)
1637 hash_lock
= HDR_LOCK(ab
);
1638 have_lock
= MUTEX_HELD(hash_lock
);
1639 if (have_lock
|| mutex_tryenter(hash_lock
)) {
1640 ASSERT3U(refcount_count(&ab
->b_refcnt
), ==, 0);
1641 ASSERT(ab
->b_datacnt
> 0);
1643 arc_buf_t
*buf
= ab
->b_buf
;
1644 if (!mutex_tryenter(&buf
->b_evict_lock
)) {
1649 bytes_evicted
+= ab
->b_size
;
1650 if (recycle
&& ab
->b_type
== type
&&
1651 ab
->b_size
== bytes
&&
1652 !HDR_L2_WRITING(ab
)) {
1653 stolen
= buf
->b_data
;
1658 mutex_enter(&arc_eviction_mtx
);
1659 arc_buf_destroy(buf
,
1660 buf
->b_data
== stolen
, FALSE
);
1661 ab
->b_buf
= buf
->b_next
;
1662 buf
->b_hdr
= &arc_eviction_hdr
;
1663 buf
->b_next
= arc_eviction_list
;
1664 arc_eviction_list
= buf
;
1665 mutex_exit(&arc_eviction_mtx
);
1666 mutex_exit(&buf
->b_evict_lock
);
1668 mutex_exit(&buf
->b_evict_lock
);
1669 arc_buf_destroy(buf
,
1670 buf
->b_data
== stolen
, TRUE
);
1675 ARCSTAT_INCR(arcstat_evict_l2_cached
,
1678 if (l2arc_write_eligible(ab
->b_spa
, ab
)) {
1679 ARCSTAT_INCR(arcstat_evict_l2_eligible
,
1683 arcstat_evict_l2_ineligible
,
1688 if (ab
->b_datacnt
== 0) {
1689 arc_change_state(evicted_state
, ab
, hash_lock
);
1690 ASSERT(HDR_IN_HASH_TABLE(ab
));
1691 ab
->b_flags
|= ARC_IN_HASH_TABLE
;
1692 ab
->b_flags
&= ~ARC_BUF_AVAILABLE
;
1693 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, ab
);
1696 mutex_exit(hash_lock
);
1697 if (bytes
>= 0 && bytes_evicted
>= bytes
)
1704 mutex_exit(&evicted_state
->arcs_mtx
);
1705 mutex_exit(&state
->arcs_mtx
);
1707 if (bytes_evicted
< bytes
)
1708 dprintf("only evicted %lld bytes from %x",
1709 (longlong_t
)bytes_evicted
, state
);
1712 ARCSTAT_INCR(arcstat_evict_skip
, skipped
);
1715 ARCSTAT_INCR(arcstat_mutex_miss
, missed
);
1718 * We have just evicted some date into the ghost state, make
1719 * sure we also adjust the ghost state size if necessary.
1722 arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
> arc_c
) {
1723 int64_t mru_over
= arc_anon
->arcs_size
+ arc_mru
->arcs_size
+
1724 arc_mru_ghost
->arcs_size
- arc_c
;
1726 if (mru_over
> 0 && arc_mru_ghost
->arcs_lsize
[type
] > 0) {
1728 MIN(arc_mru_ghost
->arcs_lsize
[type
], mru_over
);
1729 arc_evict_ghost(arc_mru_ghost
, NULL
, todelete
);
1730 } else if (arc_mfu_ghost
->arcs_lsize
[type
] > 0) {
1731 int64_t todelete
= MIN(arc_mfu_ghost
->arcs_lsize
[type
],
1732 arc_mru_ghost
->arcs_size
+
1733 arc_mfu_ghost
->arcs_size
- arc_c
);
1734 arc_evict_ghost(arc_mfu_ghost
, NULL
, todelete
);
1742 * Remove buffers from list until we've removed the specified number of
1743 * bytes. Destroy the buffers that are removed.
1746 arc_evict_ghost(arc_state_t
*state
, uint64_t spa
, int64_t bytes
)
1748 arc_buf_hdr_t
*ab
, *ab_prev
;
1749 arc_buf_hdr_t marker
= { 0 };
1750 list_t
*list
= &state
->arcs_list
[ARC_BUFC_DATA
];
1751 kmutex_t
*hash_lock
;
1752 uint64_t bytes_deleted
= 0;
1753 uint64_t bufs_skipped
= 0;
1755 ASSERT(GHOST_STATE(state
));
1757 mutex_enter(&state
->arcs_mtx
);
1758 for (ab
= list_tail(list
); ab
; ab
= ab_prev
) {
1759 ab_prev
= list_prev(list
, ab
);
1760 if (spa
&& ab
->b_spa
!= spa
)
1763 /* ignore markers */
1767 hash_lock
= HDR_LOCK(ab
);
1768 /* caller may be trying to modify this buffer, skip it */
1769 if (MUTEX_HELD(hash_lock
))
1771 if (mutex_tryenter(hash_lock
)) {
1772 ASSERT(!HDR_IO_IN_PROGRESS(ab
));
1773 ASSERT(ab
->b_buf
== NULL
);
1774 ARCSTAT_BUMP(arcstat_deleted
);
1775 bytes_deleted
+= ab
->b_size
;
1777 if (ab
->b_l2hdr
!= NULL
) {
1779 * This buffer is cached on the 2nd Level ARC;
1780 * don't destroy the header.
1782 arc_change_state(arc_l2c_only
, ab
, hash_lock
);
1783 mutex_exit(hash_lock
);
1785 arc_change_state(arc_anon
, ab
, hash_lock
);
1786 mutex_exit(hash_lock
);
1787 arc_hdr_destroy(ab
);
1790 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, ab
);
1791 if (bytes
>= 0 && bytes_deleted
>= bytes
)
1793 } else if (bytes
< 0) {
1795 * Insert a list marker and then wait for the
1796 * hash lock to become available. Once its
1797 * available, restart from where we left off.
1799 list_insert_after(list
, ab
, &marker
);
1800 mutex_exit(&state
->arcs_mtx
);
1801 mutex_enter(hash_lock
);
1802 mutex_exit(hash_lock
);
1803 mutex_enter(&state
->arcs_mtx
);
1804 ab_prev
= list_prev(list
, &marker
);
1805 list_remove(list
, &marker
);
1809 mutex_exit(&state
->arcs_mtx
);
1811 if (list
== &state
->arcs_list
[ARC_BUFC_DATA
] &&
1812 (bytes
< 0 || bytes_deleted
< bytes
)) {
1813 list
= &state
->arcs_list
[ARC_BUFC_METADATA
];
1818 ARCSTAT_INCR(arcstat_mutex_miss
, bufs_skipped
);
1822 if (bytes_deleted
< bytes
)
1823 dprintf("only deleted %lld bytes from %p",
1824 (longlong_t
)bytes_deleted
, state
);
1830 int64_t adjustment
, delta
;
1836 adjustment
= MIN((int64_t)(arc_size
- arc_c
),
1837 (int64_t)(arc_anon
->arcs_size
+ arc_mru
->arcs_size
+ arc_meta_used
-
1840 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] > 0) {
1841 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_DATA
], adjustment
);
1842 (void) arc_evict(arc_mru
, NULL
, delta
, FALSE
, ARC_BUFC_DATA
);
1843 adjustment
-= delta
;
1846 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
1847 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
], adjustment
);
1848 (void) arc_evict(arc_mru
, NULL
, delta
, FALSE
,
1856 adjustment
= arc_size
- arc_c
;
1858 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] > 0) {
1859 delta
= MIN(adjustment
, arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
]);
1860 (void) arc_evict(arc_mfu
, NULL
, delta
, FALSE
, ARC_BUFC_DATA
);
1861 adjustment
-= delta
;
1864 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
1865 int64_t delta
= MIN(adjustment
,
1866 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
]);
1867 (void) arc_evict(arc_mfu
, NULL
, delta
, FALSE
,
1872 * Adjust ghost lists
1875 adjustment
= arc_mru
->arcs_size
+ arc_mru_ghost
->arcs_size
- arc_c
;
1877 if (adjustment
> 0 && arc_mru_ghost
->arcs_size
> 0) {
1878 delta
= MIN(arc_mru_ghost
->arcs_size
, adjustment
);
1879 arc_evict_ghost(arc_mru_ghost
, NULL
, delta
);
1883 arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
- arc_c
;
1885 if (adjustment
> 0 && arc_mfu_ghost
->arcs_size
> 0) {
1886 delta
= MIN(arc_mfu_ghost
->arcs_size
, adjustment
);
1887 arc_evict_ghost(arc_mfu_ghost
, NULL
, delta
);
1892 arc_do_user_evicts(void)
1894 mutex_enter(&arc_eviction_mtx
);
1895 while (arc_eviction_list
!= NULL
) {
1896 arc_buf_t
*buf
= arc_eviction_list
;
1897 arc_eviction_list
= buf
->b_next
;
1898 mutex_enter(&buf
->b_evict_lock
);
1900 mutex_exit(&buf
->b_evict_lock
);
1901 mutex_exit(&arc_eviction_mtx
);
1903 if (buf
->b_efunc
!= NULL
)
1904 VERIFY(buf
->b_efunc(buf
) == 0);
1906 buf
->b_efunc
= NULL
;
1907 buf
->b_private
= NULL
;
1908 kmem_cache_free(buf_cache
, buf
);
1909 mutex_enter(&arc_eviction_mtx
);
1911 mutex_exit(&arc_eviction_mtx
);
1915 * Flush all *evictable* data from the cache for the given spa.
1916 * NOTE: this will not touch "active" (i.e. referenced) data.
1919 arc_flush(spa_t
*spa
)
1924 guid
= spa_load_guid(spa
);
1926 while (list_head(&arc_mru
->arcs_list
[ARC_BUFC_DATA
])) {
1927 (void) arc_evict(arc_mru
, guid
, -1, FALSE
, ARC_BUFC_DATA
);
1931 while (list_head(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
])) {
1932 (void) arc_evict(arc_mru
, guid
, -1, FALSE
, ARC_BUFC_METADATA
);
1936 while (list_head(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
])) {
1937 (void) arc_evict(arc_mfu
, guid
, -1, FALSE
, ARC_BUFC_DATA
);
1941 while (list_head(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
])) {
1942 (void) arc_evict(arc_mfu
, guid
, -1, FALSE
, ARC_BUFC_METADATA
);
1947 arc_evict_ghost(arc_mru_ghost
, guid
, -1);
1948 arc_evict_ghost(arc_mfu_ghost
, guid
, -1);
1950 mutex_enter(&arc_reclaim_thr_lock
);
1951 arc_do_user_evicts();
1952 mutex_exit(&arc_reclaim_thr_lock
);
1953 ASSERT(spa
|| arc_eviction_list
== NULL
);
1959 if (arc_c
> arc_c_min
) {
1963 to_free
= MAX(arc_c
>> arc_shrink_shift
, ptob(needfree
));
1965 to_free
= arc_c
>> arc_shrink_shift
;
1967 if (arc_c
> arc_c_min
+ to_free
)
1968 atomic_add_64(&arc_c
, -to_free
);
1972 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
1973 if (arc_c
> arc_size
)
1974 arc_c
= MAX(arc_size
, arc_c_min
);
1976 arc_p
= (arc_c
>> 1);
1977 ASSERT(arc_c
>= arc_c_min
);
1978 ASSERT((int64_t)arc_p
>= 0);
1981 if (arc_size
> arc_c
)
1986 arc_reclaim_needed(void)
1996 * take 'desfree' extra pages, so we reclaim sooner, rather than later
2001 * check that we're out of range of the pageout scanner. It starts to
2002 * schedule paging if freemem is less than lotsfree and needfree.
2003 * lotsfree is the high-water mark for pageout, and needfree is the
2004 * number of needed free pages. We add extra pages here to make sure
2005 * the scanner doesn't start up while we're freeing memory.
2007 if (freemem
< lotsfree
+ needfree
+ extra
)
2011 * check to make sure that swapfs has enough space so that anon
2012 * reservations can still succeed. anon_resvmem() checks that the
2013 * availrmem is greater than swapfs_minfree, and the number of reserved
2014 * swap pages. We also add a bit of extra here just to prevent
2015 * circumstances from getting really dire.
2017 if (availrmem
< swapfs_minfree
+ swapfs_reserve
+ extra
)
2022 * If we're on an i386 platform, it's possible that we'll exhaust the
2023 * kernel heap space before we ever run out of available physical
2024 * memory. Most checks of the size of the heap_area compare against
2025 * tune.t_minarmem, which is the minimum available real memory that we
2026 * can have in the system. However, this is generally fixed at 25 pages
2027 * which is so low that it's useless. In this comparison, we seek to
2028 * calculate the total heap-size, and reclaim if more than 3/4ths of the
2029 * heap is allocated. (Or, in the calculation, if less than 1/4th is
2032 if (btop(vmem_size(heap_arena
, VMEM_FREE
)) <
2033 (btop(vmem_size(heap_arena
, VMEM_FREE
| VMEM_ALLOC
)) >> 2))
2038 if (spa_get_random(100) == 0)
2045 arc_kmem_reap_now(arc_reclaim_strategy_t strat
)
2048 kmem_cache_t
*prev_cache
= NULL
;
2049 kmem_cache_t
*prev_data_cache
= NULL
;
2050 extern kmem_cache_t
*zio_buf_cache
[];
2051 extern kmem_cache_t
*zio_data_buf_cache
[];
2054 if (arc_meta_used
>= arc_meta_limit
) {
2056 * We are exceeding our meta-data cache limit.
2057 * Purge some DNLC entries to release holds on meta-data.
2059 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent
);
2063 * Reclaim unused memory from all kmem caches.
2070 * An aggressive reclamation will shrink the cache size as well as
2071 * reap free buffers from the arc kmem caches.
2073 if (strat
== ARC_RECLAIM_AGGR
)
2076 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
2077 if (zio_buf_cache
[i
] != prev_cache
) {
2078 prev_cache
= zio_buf_cache
[i
];
2079 kmem_cache_reap_now(zio_buf_cache
[i
]);
2081 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
2082 prev_data_cache
= zio_data_buf_cache
[i
];
2083 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
2086 kmem_cache_reap_now(buf_cache
);
2087 kmem_cache_reap_now(hdr_cache
);
2091 arc_reclaim_thread(void)
2093 clock_t growtime
= 0;
2094 arc_reclaim_strategy_t last_reclaim
= ARC_RECLAIM_CONS
;
2097 CALLB_CPR_INIT(&cpr
, &arc_reclaim_thr_lock
, callb_generic_cpr
, FTAG
);
2099 mutex_enter(&arc_reclaim_thr_lock
);
2100 while (arc_thread_exit
== 0) {
2101 if (arc_reclaim_needed()) {
2104 if (last_reclaim
== ARC_RECLAIM_CONS
) {
2105 last_reclaim
= ARC_RECLAIM_AGGR
;
2107 last_reclaim
= ARC_RECLAIM_CONS
;
2111 last_reclaim
= ARC_RECLAIM_AGGR
;
2115 /* reset the growth delay for every reclaim */
2116 growtime
= ddi_get_lbolt() + (arc_grow_retry
* hz
);
2118 arc_kmem_reap_now(last_reclaim
);
2121 } else if (arc_no_grow
&& ddi_get_lbolt() >= growtime
) {
2122 arc_no_grow
= FALSE
;
2127 if (arc_eviction_list
!= NULL
)
2128 arc_do_user_evicts();
2130 /* block until needed, or one second, whichever is shorter */
2131 CALLB_CPR_SAFE_BEGIN(&cpr
);
2132 (void) cv_timedwait(&arc_reclaim_thr_cv
,
2133 &arc_reclaim_thr_lock
, (ddi_get_lbolt() + hz
));
2134 CALLB_CPR_SAFE_END(&cpr
, &arc_reclaim_thr_lock
);
2137 arc_thread_exit
= 0;
2138 cv_broadcast(&arc_reclaim_thr_cv
);
2139 CALLB_CPR_EXIT(&cpr
); /* drops arc_reclaim_thr_lock */
2144 * Adapt arc info given the number of bytes we are trying to add and
2145 * the state that we are comming from. This function is only called
2146 * when we are adding new content to the cache.
2149 arc_adapt(int bytes
, arc_state_t
*state
)
2152 uint64_t arc_p_min
= (arc_c
>> arc_p_min_shift
);
2154 if (state
== arc_l2c_only
)
2159 * Adapt the target size of the MRU list:
2160 * - if we just hit in the MRU ghost list, then increase
2161 * the target size of the MRU list.
2162 * - if we just hit in the MFU ghost list, then increase
2163 * the target size of the MFU list by decreasing the
2164 * target size of the MRU list.
2166 if (state
== arc_mru_ghost
) {
2167 mult
= ((arc_mru_ghost
->arcs_size
>= arc_mfu_ghost
->arcs_size
) ?
2168 1 : (arc_mfu_ghost
->arcs_size
/arc_mru_ghost
->arcs_size
));
2169 mult
= MIN(mult
, 10); /* avoid wild arc_p adjustment */
2171 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
2172 } else if (state
== arc_mfu_ghost
) {
2175 mult
= ((arc_mfu_ghost
->arcs_size
>= arc_mru_ghost
->arcs_size
) ?
2176 1 : (arc_mru_ghost
->arcs_size
/arc_mfu_ghost
->arcs_size
));
2177 mult
= MIN(mult
, 10);
2179 delta
= MIN(bytes
* mult
, arc_p
);
2180 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
2182 ASSERT((int64_t)arc_p
>= 0);
2184 if (arc_reclaim_needed()) {
2185 cv_signal(&arc_reclaim_thr_cv
);
2192 if (arc_c
>= arc_c_max
)
2196 * If we're within (2 * maxblocksize) bytes of the target
2197 * cache size, increment the target cache size
2199 if (arc_size
> arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
2200 atomic_add_64(&arc_c
, (int64_t)bytes
);
2201 if (arc_c
> arc_c_max
)
2203 else if (state
== arc_anon
)
2204 atomic_add_64(&arc_p
, (int64_t)bytes
);
2208 ASSERT((int64_t)arc_p
>= 0);
2212 * Check if the cache has reached its limits and eviction is required
2216 arc_evict_needed(arc_buf_contents_t type
)
2218 if (type
== ARC_BUFC_METADATA
&& arc_meta_used
>= arc_meta_limit
)
2223 * If zio data pages are being allocated out of a separate heap segment,
2224 * then enforce that the size of available vmem for this area remains
2225 * above about 1/32nd free.
2227 if (type
== ARC_BUFC_DATA
&& zio_arena
!= NULL
&&
2228 vmem_size(zio_arena
, VMEM_FREE
) <
2229 (vmem_size(zio_arena
, VMEM_ALLOC
) >> 5))
2233 if (arc_reclaim_needed())
2236 return (arc_size
> arc_c
);
2240 * The buffer, supplied as the first argument, needs a data block.
2241 * So, if we are at cache max, determine which cache should be victimized.
2242 * We have the following cases:
2244 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2245 * In this situation if we're out of space, but the resident size of the MFU is
2246 * under the limit, victimize the MFU cache to satisfy this insertion request.
2248 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2249 * Here, we've used up all of the available space for the MRU, so we need to
2250 * evict from our own cache instead. Evict from the set of resident MRU
2253 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2254 * c minus p represents the MFU space in the cache, since p is the size of the
2255 * cache that is dedicated to the MRU. In this situation there's still space on
2256 * the MFU side, so the MRU side needs to be victimized.
2258 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2259 * MFU's resident set is consuming more space than it has been allotted. In
2260 * this situation, we must victimize our own cache, the MFU, for this insertion.
2263 arc_get_data_buf(arc_buf_t
*buf
)
2265 arc_state_t
*state
= buf
->b_hdr
->b_state
;
2266 uint64_t size
= buf
->b_hdr
->b_size
;
2267 arc_buf_contents_t type
= buf
->b_hdr
->b_type
;
2269 arc_adapt(size
, state
);
2272 * We have not yet reached cache maximum size,
2273 * just allocate a new buffer.
2275 if (!arc_evict_needed(type
)) {
2276 if (type
== ARC_BUFC_METADATA
) {
2277 buf
->b_data
= zio_buf_alloc(size
);
2278 arc_space_consume(size
, ARC_SPACE_DATA
);
2280 ASSERT(type
== ARC_BUFC_DATA
);
2281 buf
->b_data
= zio_data_buf_alloc(size
);
2282 ARCSTAT_INCR(arcstat_data_size
, size
);
2283 atomic_add_64(&arc_size
, size
);
2289 * If we are prefetching from the mfu ghost list, this buffer
2290 * will end up on the mru list; so steal space from there.
2292 if (state
== arc_mfu_ghost
)
2293 state
= buf
->b_hdr
->b_flags
& ARC_PREFETCH
? arc_mru
: arc_mfu
;
2294 else if (state
== arc_mru_ghost
)
2297 if (state
== arc_mru
|| state
== arc_anon
) {
2298 uint64_t mru_used
= arc_anon
->arcs_size
+ arc_mru
->arcs_size
;
2299 state
= (arc_mfu
->arcs_lsize
[type
] >= size
&&
2300 arc_p
> mru_used
) ? arc_mfu
: arc_mru
;
2303 uint64_t mfu_space
= arc_c
- arc_p
;
2304 state
= (arc_mru
->arcs_lsize
[type
] >= size
&&
2305 mfu_space
> arc_mfu
->arcs_size
) ? arc_mru
: arc_mfu
;
2307 if ((buf
->b_data
= arc_evict(state
, NULL
, size
, TRUE
, type
)) == NULL
) {
2308 if (type
== ARC_BUFC_METADATA
) {
2309 buf
->b_data
= zio_buf_alloc(size
);
2310 arc_space_consume(size
, ARC_SPACE_DATA
);
2312 ASSERT(type
== ARC_BUFC_DATA
);
2313 buf
->b_data
= zio_data_buf_alloc(size
);
2314 ARCSTAT_INCR(arcstat_data_size
, size
);
2315 atomic_add_64(&arc_size
, size
);
2317 ARCSTAT_BUMP(arcstat_recycle_miss
);
2319 ASSERT(buf
->b_data
!= NULL
);
2322 * Update the state size. Note that ghost states have a
2323 * "ghost size" and so don't need to be updated.
2325 if (!GHOST_STATE(buf
->b_hdr
->b_state
)) {
2326 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2328 atomic_add_64(&hdr
->b_state
->arcs_size
, size
);
2329 if (list_link_active(&hdr
->b_arc_node
)) {
2330 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
2331 atomic_add_64(&hdr
->b_state
->arcs_lsize
[type
], size
);
2334 * If we are growing the cache, and we are adding anonymous
2335 * data, and we have outgrown arc_p, update arc_p
2337 if (arc_size
< arc_c
&& hdr
->b_state
== arc_anon
&&
2338 arc_anon
->arcs_size
+ arc_mru
->arcs_size
> arc_p
)
2339 arc_p
= MIN(arc_c
, arc_p
+ size
);
2344 * This routine is called whenever a buffer is accessed.
2345 * NOTE: the hash lock is dropped in this function.
2348 arc_access(arc_buf_hdr_t
*buf
, kmutex_t
*hash_lock
)
2352 ASSERT(MUTEX_HELD(hash_lock
));
2354 if (buf
->b_state
== arc_anon
) {
2356 * This buffer is not in the cache, and does not
2357 * appear in our "ghost" list. Add the new buffer
2361 ASSERT(buf
->b_arc_access
== 0);
2362 buf
->b_arc_access
= ddi_get_lbolt();
2363 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, buf
);
2364 arc_change_state(arc_mru
, buf
, hash_lock
);
2366 } else if (buf
->b_state
== arc_mru
) {
2367 now
= ddi_get_lbolt();
2370 * If this buffer is here because of a prefetch, then either:
2371 * - clear the flag if this is a "referencing" read
2372 * (any subsequent access will bump this into the MFU state).
2374 * - move the buffer to the head of the list if this is
2375 * another prefetch (to make it less likely to be evicted).
2377 if ((buf
->b_flags
& ARC_PREFETCH
) != 0) {
2378 if (refcount_count(&buf
->b_refcnt
) == 0) {
2379 ASSERT(list_link_active(&buf
->b_arc_node
));
2381 buf
->b_flags
&= ~ARC_PREFETCH
;
2382 ARCSTAT_BUMP(arcstat_mru_hits
);
2384 buf
->b_arc_access
= now
;
2389 * This buffer has been "accessed" only once so far,
2390 * but it is still in the cache. Move it to the MFU
2393 if (now
> buf
->b_arc_access
+ ARC_MINTIME
) {
2395 * More than 125ms have passed since we
2396 * instantiated this buffer. Move it to the
2397 * most frequently used state.
2399 buf
->b_arc_access
= now
;
2400 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2401 arc_change_state(arc_mfu
, buf
, hash_lock
);
2403 ARCSTAT_BUMP(arcstat_mru_hits
);
2404 } else if (buf
->b_state
== arc_mru_ghost
) {
2405 arc_state_t
*new_state
;
2407 * This buffer has been "accessed" recently, but
2408 * was evicted from the cache. Move it to the
2412 if (buf
->b_flags
& ARC_PREFETCH
) {
2413 new_state
= arc_mru
;
2414 if (refcount_count(&buf
->b_refcnt
) > 0)
2415 buf
->b_flags
&= ~ARC_PREFETCH
;
2416 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, buf
);
2418 new_state
= arc_mfu
;
2419 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2422 buf
->b_arc_access
= ddi_get_lbolt();
2423 arc_change_state(new_state
, buf
, hash_lock
);
2425 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
2426 } else if (buf
->b_state
== arc_mfu
) {
2428 * This buffer has been accessed more than once and is
2429 * still in the cache. Keep it in the MFU state.
2431 * NOTE: an add_reference() that occurred when we did
2432 * the arc_read() will have kicked this off the list.
2433 * If it was a prefetch, we will explicitly move it to
2434 * the head of the list now.
2436 if ((buf
->b_flags
& ARC_PREFETCH
) != 0) {
2437 ASSERT(refcount_count(&buf
->b_refcnt
) == 0);
2438 ASSERT(list_link_active(&buf
->b_arc_node
));
2440 ARCSTAT_BUMP(arcstat_mfu_hits
);
2441 buf
->b_arc_access
= ddi_get_lbolt();
2442 } else if (buf
->b_state
== arc_mfu_ghost
) {
2443 arc_state_t
*new_state
= arc_mfu
;
2445 * This buffer has been accessed more than once but has
2446 * been evicted from the cache. Move it back to the
2450 if (buf
->b_flags
& ARC_PREFETCH
) {
2452 * This is a prefetch access...
2453 * move this block back to the MRU state.
2455 ASSERT3U(refcount_count(&buf
->b_refcnt
), ==, 0);
2456 new_state
= arc_mru
;
2459 buf
->b_arc_access
= ddi_get_lbolt();
2460 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2461 arc_change_state(new_state
, buf
, hash_lock
);
2463 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
2464 } else if (buf
->b_state
== arc_l2c_only
) {
2466 * This buffer is on the 2nd Level ARC.
2469 buf
->b_arc_access
= ddi_get_lbolt();
2470 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2471 arc_change_state(arc_mfu
, buf
, hash_lock
);
2473 ASSERT(!"invalid arc state");
2477 /* a generic arc_done_func_t which you can use */
2480 arc_bcopy_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2482 if (zio
== NULL
|| zio
->io_error
== 0)
2483 bcopy(buf
->b_data
, arg
, buf
->b_hdr
->b_size
);
2484 VERIFY(arc_buf_remove_ref(buf
, arg
) == 1);
2487 /* a generic arc_done_func_t */
2489 arc_getbuf_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2491 arc_buf_t
**bufp
= arg
;
2492 if (zio
&& zio
->io_error
) {
2493 VERIFY(arc_buf_remove_ref(buf
, arg
) == 1);
2497 ASSERT(buf
->b_data
);
2502 arc_read_done(zio_t
*zio
)
2504 arc_buf_hdr_t
*hdr
, *found
;
2506 arc_buf_t
*abuf
; /* buffer we're assigning to callback */
2507 kmutex_t
*hash_lock
;
2508 arc_callback_t
*callback_list
, *acb
;
2509 int freeable
= FALSE
;
2511 buf
= zio
->io_private
;
2515 * The hdr was inserted into hash-table and removed from lists
2516 * prior to starting I/O. We should find this header, since
2517 * it's in the hash table, and it should be legit since it's
2518 * not possible to evict it during the I/O. The only possible
2519 * reason for it not to be found is if we were freed during the
2522 found
= buf_hash_find(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
,
2525 ASSERT((found
== NULL
&& HDR_FREED_IN_READ(hdr
) && hash_lock
== NULL
) ||
2526 (found
== hdr
&& DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
2527 (found
== hdr
&& HDR_L2_READING(hdr
)));
2529 hdr
->b_flags
&= ~ARC_L2_EVICTED
;
2530 if (l2arc_noprefetch
&& (hdr
->b_flags
& ARC_PREFETCH
))
2531 hdr
->b_flags
&= ~ARC_L2CACHE
;
2533 /* byteswap if necessary */
2534 callback_list
= hdr
->b_acb
;
2535 ASSERT(callback_list
!= NULL
);
2536 if (BP_SHOULD_BYTESWAP(zio
->io_bp
) && zio
->io_error
== 0) {
2537 arc_byteswap_func_t
*func
= BP_GET_LEVEL(zio
->io_bp
) > 0 ?
2538 byteswap_uint64_array
:
2539 dmu_ot
[BP_GET_TYPE(zio
->io_bp
)].ot_byteswap
;
2540 func(buf
->b_data
, hdr
->b_size
);
2543 arc_cksum_compute(buf
, B_FALSE
);
2545 if (hash_lock
&& zio
->io_error
== 0 && hdr
->b_state
== arc_anon
) {
2547 * Only call arc_access on anonymous buffers. This is because
2548 * if we've issued an I/O for an evicted buffer, we've already
2549 * called arc_access (to prevent any simultaneous readers from
2550 * getting confused).
2552 arc_access(hdr
, hash_lock
);
2555 /* create copies of the data buffer for the callers */
2557 for (acb
= callback_list
; acb
; acb
= acb
->acb_next
) {
2558 if (acb
->acb_done
) {
2560 abuf
= arc_buf_clone(buf
);
2561 acb
->acb_buf
= abuf
;
2566 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
2567 ASSERT(!HDR_BUF_AVAILABLE(hdr
));
2569 ASSERT(buf
->b_efunc
== NULL
);
2570 ASSERT(hdr
->b_datacnt
== 1);
2571 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
2574 ASSERT(refcount_is_zero(&hdr
->b_refcnt
) || callback_list
!= NULL
);
2576 if (zio
->io_error
!= 0) {
2577 hdr
->b_flags
|= ARC_IO_ERROR
;
2578 if (hdr
->b_state
!= arc_anon
)
2579 arc_change_state(arc_anon
, hdr
, hash_lock
);
2580 if (HDR_IN_HASH_TABLE(hdr
))
2581 buf_hash_remove(hdr
);
2582 freeable
= refcount_is_zero(&hdr
->b_refcnt
);
2586 * Broadcast before we drop the hash_lock to avoid the possibility
2587 * that the hdr (and hence the cv) might be freed before we get to
2588 * the cv_broadcast().
2590 cv_broadcast(&hdr
->b_cv
);
2593 mutex_exit(hash_lock
);
2596 * This block was freed while we waited for the read to
2597 * complete. It has been removed from the hash table and
2598 * moved to the anonymous state (so that it won't show up
2601 ASSERT3P(hdr
->b_state
, ==, arc_anon
);
2602 freeable
= refcount_is_zero(&hdr
->b_refcnt
);
2605 /* execute each callback and free its structure */
2606 while ((acb
= callback_list
) != NULL
) {
2608 acb
->acb_done(zio
, acb
->acb_buf
, acb
->acb_private
);
2610 if (acb
->acb_zio_dummy
!= NULL
) {
2611 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
2612 zio_nowait(acb
->acb_zio_dummy
);
2615 callback_list
= acb
->acb_next
;
2616 kmem_free(acb
, sizeof (arc_callback_t
));
2620 arc_hdr_destroy(hdr
);
2624 * "Read" the block block at the specified DVA (in bp) via the
2625 * cache. If the block is found in the cache, invoke the provided
2626 * callback immediately and return. Note that the `zio' parameter
2627 * in the callback will be NULL in this case, since no IO was
2628 * required. If the block is not in the cache pass the read request
2629 * on to the spa with a substitute callback function, so that the
2630 * requested block will be added to the cache.
2632 * If a read request arrives for a block that has a read in-progress,
2633 * either wait for the in-progress read to complete (and return the
2634 * results); or, if this is a read with a "done" func, add a record
2635 * to the read to invoke the "done" func when the read completes,
2636 * and return; or just return.
2638 * arc_read_done() will invoke all the requested "done" functions
2639 * for readers of this block.
2641 * Normal callers should use arc_read and pass the arc buffer and offset
2642 * for the bp. But if you know you don't need locking, you can use
2646 arc_read(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
, arc_buf_t
*pbuf
,
2647 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
2648 uint32_t *arc_flags
, const zbookmark_t
*zb
)
2654 * XXX This happens from traverse callback funcs, for
2655 * the objset_phys_t block.
2657 return (arc_read_nolock(pio
, spa
, bp
, done
, private, priority
,
2658 zio_flags
, arc_flags
, zb
));
2661 ASSERT(!refcount_is_zero(&pbuf
->b_hdr
->b_refcnt
));
2662 ASSERT3U((char *)bp
- (char *)pbuf
->b_data
, <, pbuf
->b_hdr
->b_size
);
2663 rw_enter(&pbuf
->b_data_lock
, RW_READER
);
2665 err
= arc_read_nolock(pio
, spa
, bp
, done
, private, priority
,
2666 zio_flags
, arc_flags
, zb
);
2667 rw_exit(&pbuf
->b_data_lock
);
2673 arc_read_nolock(zio_t
*pio
, spa_t
*spa
, const blkptr_t
*bp
,
2674 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
2675 uint32_t *arc_flags
, const zbookmark_t
*zb
)
2679 kmutex_t
*hash_lock
;
2681 uint64_t guid
= spa_load_guid(spa
);
2684 hdr
= buf_hash_find(guid
, BP_IDENTITY(bp
), BP_PHYSICAL_BIRTH(bp
),
2686 if (hdr
&& hdr
->b_datacnt
> 0) {
2688 *arc_flags
|= ARC_CACHED
;
2690 if (HDR_IO_IN_PROGRESS(hdr
)) {
2692 if (*arc_flags
& ARC_WAIT
) {
2693 cv_wait(&hdr
->b_cv
, hash_lock
);
2694 mutex_exit(hash_lock
);
2697 ASSERT(*arc_flags
& ARC_NOWAIT
);
2700 arc_callback_t
*acb
= NULL
;
2702 acb
= kmem_zalloc(sizeof (arc_callback_t
),
2704 acb
->acb_done
= done
;
2705 acb
->acb_private
= private;
2707 acb
->acb_zio_dummy
= zio_null(pio
,
2708 spa
, NULL
, NULL
, NULL
, zio_flags
);
2710 ASSERT(acb
->acb_done
!= NULL
);
2711 acb
->acb_next
= hdr
->b_acb
;
2713 add_reference(hdr
, hash_lock
, private);
2714 mutex_exit(hash_lock
);
2717 mutex_exit(hash_lock
);
2721 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
2724 add_reference(hdr
, hash_lock
, private);
2726 * If this block is already in use, create a new
2727 * copy of the data so that we will be guaranteed
2728 * that arc_release() will always succeed.
2732 ASSERT(buf
->b_data
);
2733 if (HDR_BUF_AVAILABLE(hdr
)) {
2734 ASSERT(buf
->b_efunc
== NULL
);
2735 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
2737 buf
= arc_buf_clone(buf
);
2740 } else if (*arc_flags
& ARC_PREFETCH
&&
2741 refcount_count(&hdr
->b_refcnt
) == 0) {
2742 hdr
->b_flags
|= ARC_PREFETCH
;
2744 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
2745 arc_access(hdr
, hash_lock
);
2746 if (*arc_flags
& ARC_L2CACHE
)
2747 hdr
->b_flags
|= ARC_L2CACHE
;
2748 mutex_exit(hash_lock
);
2749 ARCSTAT_BUMP(arcstat_hits
);
2750 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
2751 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
2752 data
, metadata
, hits
);
2755 done(NULL
, buf
, private);
2757 uint64_t size
= BP_GET_LSIZE(bp
);
2758 arc_callback_t
*acb
;
2761 boolean_t devw
= B_FALSE
;
2764 /* this block is not in the cache */
2765 arc_buf_hdr_t
*exists
;
2766 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
2767 buf
= arc_buf_alloc(spa
, size
, private, type
);
2769 hdr
->b_dva
= *BP_IDENTITY(bp
);
2770 hdr
->b_birth
= BP_PHYSICAL_BIRTH(bp
);
2771 hdr
->b_cksum0
= bp
->blk_cksum
.zc_word
[0];
2772 exists
= buf_hash_insert(hdr
, &hash_lock
);
2774 /* somebody beat us to the hash insert */
2775 mutex_exit(hash_lock
);
2776 buf_discard_identity(hdr
);
2777 (void) arc_buf_remove_ref(buf
, private);
2778 goto top
; /* restart the IO request */
2780 /* if this is a prefetch, we don't have a reference */
2781 if (*arc_flags
& ARC_PREFETCH
) {
2782 (void) remove_reference(hdr
, hash_lock
,
2784 hdr
->b_flags
|= ARC_PREFETCH
;
2786 if (*arc_flags
& ARC_L2CACHE
)
2787 hdr
->b_flags
|= ARC_L2CACHE
;
2788 if (BP_GET_LEVEL(bp
) > 0)
2789 hdr
->b_flags
|= ARC_INDIRECT
;
2791 /* this block is in the ghost cache */
2792 ASSERT(GHOST_STATE(hdr
->b_state
));
2793 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
2794 ASSERT3U(refcount_count(&hdr
->b_refcnt
), ==, 0);
2795 ASSERT(hdr
->b_buf
== NULL
);
2797 /* if this is a prefetch, we don't have a reference */
2798 if (*arc_flags
& ARC_PREFETCH
)
2799 hdr
->b_flags
|= ARC_PREFETCH
;
2801 add_reference(hdr
, hash_lock
, private);
2802 if (*arc_flags
& ARC_L2CACHE
)
2803 hdr
->b_flags
|= ARC_L2CACHE
;
2804 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
2807 buf
->b_efunc
= NULL
;
2808 buf
->b_private
= NULL
;
2811 ASSERT(hdr
->b_datacnt
== 0);
2813 arc_get_data_buf(buf
);
2814 arc_access(hdr
, hash_lock
);
2817 ASSERT(!GHOST_STATE(hdr
->b_state
));
2819 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
2820 acb
->acb_done
= done
;
2821 acb
->acb_private
= private;
2823 ASSERT(hdr
->b_acb
== NULL
);
2825 hdr
->b_flags
|= ARC_IO_IN_PROGRESS
;
2827 if (HDR_L2CACHE(hdr
) && hdr
->b_l2hdr
!= NULL
&&
2828 (vd
= hdr
->b_l2hdr
->b_dev
->l2ad_vdev
) != NULL
) {
2829 devw
= hdr
->b_l2hdr
->b_dev
->l2ad_writing
;
2830 addr
= hdr
->b_l2hdr
->b_daddr
;
2832 * Lock out device removal.
2834 if (vdev_is_dead(vd
) ||
2835 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
2839 mutex_exit(hash_lock
);
2841 ASSERT3U(hdr
->b_size
, ==, size
);
2842 DTRACE_PROBE4(arc__miss
, arc_buf_hdr_t
*, hdr
, blkptr_t
*, bp
,
2843 uint64_t, size
, zbookmark_t
*, zb
);
2844 ARCSTAT_BUMP(arcstat_misses
);
2845 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
2846 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
2847 data
, metadata
, misses
);
2849 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
2851 * Read from the L2ARC if the following are true:
2852 * 1. The L2ARC vdev was previously cached.
2853 * 2. This buffer still has L2ARC metadata.
2854 * 3. This buffer isn't currently writing to the L2ARC.
2855 * 4. The L2ARC entry wasn't evicted, which may
2856 * also have invalidated the vdev.
2857 * 5. This isn't prefetch and l2arc_noprefetch is set.
2859 if (hdr
->b_l2hdr
!= NULL
&&
2860 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
2861 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
2862 l2arc_read_callback_t
*cb
;
2864 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
2865 ARCSTAT_BUMP(arcstat_l2_hits
);
2867 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
2869 cb
->l2rcb_buf
= buf
;
2870 cb
->l2rcb_spa
= spa
;
2873 cb
->l2rcb_flags
= zio_flags
;
2876 * l2arc read. The SCL_L2ARC lock will be
2877 * released by l2arc_read_done().
2879 rzio
= zio_read_phys(pio
, vd
, addr
, size
,
2880 buf
->b_data
, ZIO_CHECKSUM_OFF
,
2881 l2arc_read_done
, cb
, priority
, zio_flags
|
2882 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_CANFAIL
|
2883 ZIO_FLAG_DONT_PROPAGATE
|
2884 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
2885 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
2887 ARCSTAT_INCR(arcstat_l2_read_bytes
, size
);
2889 if (*arc_flags
& ARC_NOWAIT
) {
2894 ASSERT(*arc_flags
& ARC_WAIT
);
2895 if (zio_wait(rzio
) == 0)
2898 /* l2arc read error; goto zio_read() */
2900 DTRACE_PROBE1(l2arc__miss
,
2901 arc_buf_hdr_t
*, hdr
);
2902 ARCSTAT_BUMP(arcstat_l2_misses
);
2903 if (HDR_L2_WRITING(hdr
))
2904 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
2905 spa_config_exit(spa
, SCL_L2ARC
, vd
);
2909 spa_config_exit(spa
, SCL_L2ARC
, vd
);
2910 if (l2arc_ndev
!= 0) {
2911 DTRACE_PROBE1(l2arc__miss
,
2912 arc_buf_hdr_t
*, hdr
);
2913 ARCSTAT_BUMP(arcstat_l2_misses
);
2917 rzio
= zio_read(pio
, spa
, bp
, buf
->b_data
, size
,
2918 arc_read_done
, buf
, priority
, zio_flags
, zb
);
2920 if (*arc_flags
& ARC_WAIT
)
2921 return (zio_wait(rzio
));
2923 ASSERT(*arc_flags
& ARC_NOWAIT
);
2930 arc_set_callback(arc_buf_t
*buf
, arc_evict_func_t
*func
, void *private)
2932 ASSERT(buf
->b_hdr
!= NULL
);
2933 ASSERT(buf
->b_hdr
->b_state
!= arc_anon
);
2934 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_refcnt
) || func
== NULL
);
2935 ASSERT(buf
->b_efunc
== NULL
);
2936 ASSERT(!HDR_BUF_AVAILABLE(buf
->b_hdr
));
2938 buf
->b_efunc
= func
;
2939 buf
->b_private
= private;
2943 * This is used by the DMU to let the ARC know that a buffer is
2944 * being evicted, so the ARC should clean up. If this arc buf
2945 * is not yet in the evicted state, it will be put there.
2948 arc_buf_evict(arc_buf_t
*buf
)
2951 kmutex_t
*hash_lock
;
2954 mutex_enter(&buf
->b_evict_lock
);
2958 * We are in arc_do_user_evicts().
2960 ASSERT(buf
->b_data
== NULL
);
2961 mutex_exit(&buf
->b_evict_lock
);
2963 } else if (buf
->b_data
== NULL
) {
2964 arc_buf_t copy
= *buf
; /* structure assignment */
2966 * We are on the eviction list; process this buffer now
2967 * but let arc_do_user_evicts() do the reaping.
2969 buf
->b_efunc
= NULL
;
2970 mutex_exit(&buf
->b_evict_lock
);
2971 VERIFY(copy
.b_efunc(©
) == 0);
2974 hash_lock
= HDR_LOCK(hdr
);
2975 mutex_enter(hash_lock
);
2977 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
2979 ASSERT3U(refcount_count(&hdr
->b_refcnt
), <, hdr
->b_datacnt
);
2980 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
2983 * Pull this buffer off of the hdr
2986 while (*bufp
!= buf
)
2987 bufp
= &(*bufp
)->b_next
;
2988 *bufp
= buf
->b_next
;
2990 ASSERT(buf
->b_data
!= NULL
);
2991 arc_buf_destroy(buf
, FALSE
, FALSE
);
2993 if (hdr
->b_datacnt
== 0) {
2994 arc_state_t
*old_state
= hdr
->b_state
;
2995 arc_state_t
*evicted_state
;
2997 ASSERT(hdr
->b_buf
== NULL
);
2998 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
3001 (old_state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
3003 mutex_enter(&old_state
->arcs_mtx
);
3004 mutex_enter(&evicted_state
->arcs_mtx
);
3006 arc_change_state(evicted_state
, hdr
, hash_lock
);
3007 ASSERT(HDR_IN_HASH_TABLE(hdr
));
3008 hdr
->b_flags
|= ARC_IN_HASH_TABLE
;
3009 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
3011 mutex_exit(&evicted_state
->arcs_mtx
);
3012 mutex_exit(&old_state
->arcs_mtx
);
3014 mutex_exit(hash_lock
);
3015 mutex_exit(&buf
->b_evict_lock
);
3017 VERIFY(buf
->b_efunc(buf
) == 0);
3018 buf
->b_efunc
= NULL
;
3019 buf
->b_private
= NULL
;
3022 kmem_cache_free(buf_cache
, buf
);
3027 * Release this buffer from the cache. This must be done
3028 * after a read and prior to modifying the buffer contents.
3029 * If the buffer has more than one reference, we must make
3030 * a new hdr for the buffer.
3033 arc_release(arc_buf_t
*buf
, void *tag
)
3036 kmutex_t
*hash_lock
= NULL
;
3037 l2arc_buf_hdr_t
*l2hdr
;
3041 * It would be nice to assert that if it's DMU metadata (level >
3042 * 0 || it's the dnode file), then it must be syncing context.
3043 * But we don't know that information at this level.
3046 mutex_enter(&buf
->b_evict_lock
);
3049 /* this buffer is not on any list */
3050 ASSERT(refcount_count(&hdr
->b_refcnt
) > 0);
3052 if (hdr
->b_state
== arc_anon
) {
3053 /* this buffer is already released */
3054 ASSERT(buf
->b_efunc
== NULL
);
3056 hash_lock
= HDR_LOCK(hdr
);
3057 mutex_enter(hash_lock
);
3059 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
3062 l2hdr
= hdr
->b_l2hdr
;
3064 mutex_enter(&l2arc_buflist_mtx
);
3065 hdr
->b_l2hdr
= NULL
;
3066 buf_size
= hdr
->b_size
;
3070 * Do we have more than one buf?
3072 if (hdr
->b_datacnt
> 1) {
3073 arc_buf_hdr_t
*nhdr
;
3075 uint64_t blksz
= hdr
->b_size
;
3076 uint64_t spa
= hdr
->b_spa
;
3077 arc_buf_contents_t type
= hdr
->b_type
;
3078 uint32_t flags
= hdr
->b_flags
;
3080 ASSERT(hdr
->b_buf
!= buf
|| buf
->b_next
!= NULL
);
3082 * Pull the data off of this hdr and attach it to
3083 * a new anonymous hdr.
3085 (void) remove_reference(hdr
, hash_lock
, tag
);
3087 while (*bufp
!= buf
)
3088 bufp
= &(*bufp
)->b_next
;
3089 *bufp
= buf
->b_next
;
3092 ASSERT3U(hdr
->b_state
->arcs_size
, >=, hdr
->b_size
);
3093 atomic_add_64(&hdr
->b_state
->arcs_size
, -hdr
->b_size
);
3094 if (refcount_is_zero(&hdr
->b_refcnt
)) {
3095 uint64_t *size
= &hdr
->b_state
->arcs_lsize
[hdr
->b_type
];
3096 ASSERT3U(*size
, >=, hdr
->b_size
);
3097 atomic_add_64(size
, -hdr
->b_size
);
3099 hdr
->b_datacnt
-= 1;
3100 arc_cksum_verify(buf
);
3102 mutex_exit(hash_lock
);
3104 nhdr
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
3105 nhdr
->b_size
= blksz
;
3107 nhdr
->b_type
= type
;
3109 nhdr
->b_state
= arc_anon
;
3110 nhdr
->b_arc_access
= 0;
3111 nhdr
->b_flags
= flags
& ARC_L2_WRITING
;
3112 nhdr
->b_l2hdr
= NULL
;
3113 nhdr
->b_datacnt
= 1;
3114 nhdr
->b_freeze_cksum
= NULL
;
3115 (void) refcount_add(&nhdr
->b_refcnt
, tag
);
3117 mutex_exit(&buf
->b_evict_lock
);
3118 atomic_add_64(&arc_anon
->arcs_size
, blksz
);
3120 mutex_exit(&buf
->b_evict_lock
);
3121 ASSERT(refcount_count(&hdr
->b_refcnt
) == 1);
3122 ASSERT(!list_link_active(&hdr
->b_arc_node
));
3123 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3124 if (hdr
->b_state
!= arc_anon
)
3125 arc_change_state(arc_anon
, hdr
, hash_lock
);
3126 hdr
->b_arc_access
= 0;
3128 mutex_exit(hash_lock
);
3130 buf_discard_identity(hdr
);
3133 buf
->b_efunc
= NULL
;
3134 buf
->b_private
= NULL
;
3137 list_remove(l2hdr
->b_dev
->l2ad_buflist
, hdr
);
3138 kmem_free(l2hdr
, sizeof (l2arc_buf_hdr_t
));
3139 ARCSTAT_INCR(arcstat_l2_size
, -buf_size
);
3140 mutex_exit(&l2arc_buflist_mtx
);
3145 * Release this buffer. If it does not match the provided BP, fill it
3146 * with that block's contents.
3150 arc_release_bp(arc_buf_t
*buf
, void *tag
, blkptr_t
*bp
, spa_t
*spa
,
3153 arc_release(buf
, tag
);
3158 arc_released(arc_buf_t
*buf
)
3162 mutex_enter(&buf
->b_evict_lock
);
3163 released
= (buf
->b_data
!= NULL
&& buf
->b_hdr
->b_state
== arc_anon
);
3164 mutex_exit(&buf
->b_evict_lock
);
3169 arc_has_callback(arc_buf_t
*buf
)
3173 mutex_enter(&buf
->b_evict_lock
);
3174 callback
= (buf
->b_efunc
!= NULL
);
3175 mutex_exit(&buf
->b_evict_lock
);
3181 arc_referenced(arc_buf_t
*buf
)
3185 mutex_enter(&buf
->b_evict_lock
);
3186 referenced
= (refcount_count(&buf
->b_hdr
->b_refcnt
));
3187 mutex_exit(&buf
->b_evict_lock
);
3188 return (referenced
);
3193 arc_write_ready(zio_t
*zio
)
3195 arc_write_callback_t
*callback
= zio
->io_private
;
3196 arc_buf_t
*buf
= callback
->awcb_buf
;
3197 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3199 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_refcnt
));
3200 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
3203 * If the IO is already in progress, then this is a re-write
3204 * attempt, so we need to thaw and re-compute the cksum.
3205 * It is the responsibility of the callback to handle the
3206 * accounting for any re-write attempt.
3208 if (HDR_IO_IN_PROGRESS(hdr
)) {
3209 mutex_enter(&hdr
->b_freeze_lock
);
3210 if (hdr
->b_freeze_cksum
!= NULL
) {
3211 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
3212 hdr
->b_freeze_cksum
= NULL
;
3214 mutex_exit(&hdr
->b_freeze_lock
);
3216 arc_cksum_compute(buf
, B_FALSE
);
3217 hdr
->b_flags
|= ARC_IO_IN_PROGRESS
;
3221 arc_write_done(zio_t
*zio
)
3223 arc_write_callback_t
*callback
= zio
->io_private
;
3224 arc_buf_t
*buf
= callback
->awcb_buf
;
3225 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3227 ASSERT(hdr
->b_acb
== NULL
);
3229 if (zio
->io_error
== 0) {
3230 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
3231 hdr
->b_birth
= BP_PHYSICAL_BIRTH(zio
->io_bp
);
3232 hdr
->b_cksum0
= zio
->io_bp
->blk_cksum
.zc_word
[0];
3234 ASSERT(BUF_EMPTY(hdr
));
3238 * If the block to be written was all-zero, we may have
3239 * compressed it away. In this case no write was performed
3240 * so there will be no dva/birth/checksum. The buffer must
3241 * therefore remain anonymous (and uncached).
3243 if (!BUF_EMPTY(hdr
)) {
3244 arc_buf_hdr_t
*exists
;
3245 kmutex_t
*hash_lock
;
3247 ASSERT(zio
->io_error
== 0);
3249 arc_cksum_verify(buf
);
3251 exists
= buf_hash_insert(hdr
, &hash_lock
);
3254 * This can only happen if we overwrite for
3255 * sync-to-convergence, because we remove
3256 * buffers from the hash table when we arc_free().
3258 if (zio
->io_flags
& ZIO_FLAG_IO_REWRITE
) {
3259 if (!BP_EQUAL(&zio
->io_bp_orig
, zio
->io_bp
))
3260 panic("bad overwrite, hdr=%p exists=%p",
3261 (void *)hdr
, (void *)exists
);
3262 ASSERT(refcount_is_zero(&exists
->b_refcnt
));
3263 arc_change_state(arc_anon
, exists
, hash_lock
);
3264 mutex_exit(hash_lock
);
3265 arc_hdr_destroy(exists
);
3266 exists
= buf_hash_insert(hdr
, &hash_lock
);
3267 ASSERT3P(exists
, ==, NULL
);
3270 ASSERT(hdr
->b_datacnt
== 1);
3271 ASSERT(hdr
->b_state
== arc_anon
);
3272 ASSERT(BP_GET_DEDUP(zio
->io_bp
));
3273 ASSERT(BP_GET_LEVEL(zio
->io_bp
) == 0);
3276 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3277 /* if it's not anon, we are doing a scrub */
3278 if (!exists
&& hdr
->b_state
== arc_anon
)
3279 arc_access(hdr
, hash_lock
);
3280 mutex_exit(hash_lock
);
3282 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3285 ASSERT(!refcount_is_zero(&hdr
->b_refcnt
));
3286 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
3288 kmem_free(callback
, sizeof (arc_write_callback_t
));
3292 arc_write(zio_t
*pio
, spa_t
*spa
, uint64_t txg
,
3293 blkptr_t
*bp
, arc_buf_t
*buf
, boolean_t l2arc
, const zio_prop_t
*zp
,
3294 arc_done_func_t
*ready
, arc_done_func_t
*done
, void *private,
3295 int priority
, int zio_flags
, const zbookmark_t
*zb
)
3297 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3298 arc_write_callback_t
*callback
;
3301 ASSERT(ready
!= NULL
);
3302 ASSERT(done
!= NULL
);
3303 ASSERT(!HDR_IO_ERROR(hdr
));
3304 ASSERT((hdr
->b_flags
& ARC_IO_IN_PROGRESS
) == 0);
3305 ASSERT(hdr
->b_acb
== NULL
);
3307 hdr
->b_flags
|= ARC_L2CACHE
;
3308 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
3309 callback
->awcb_ready
= ready
;
3310 callback
->awcb_done
= done
;
3311 callback
->awcb_private
= private;
3312 callback
->awcb_buf
= buf
;
3314 zio
= zio_write(pio
, spa
, txg
, bp
, buf
->b_data
, hdr
->b_size
, zp
,
3315 arc_write_ready
, arc_write_done
, callback
, priority
, zio_flags
, zb
);
3321 arc_memory_throttle(uint64_t reserve
, uint64_t inflight_data
, uint64_t txg
)
3324 uint64_t available_memory
= ptob(freemem
);
3325 static uint64_t page_load
= 0;
3326 static uint64_t last_txg
= 0;
3330 MIN(available_memory
, vmem_size(heap_arena
, VMEM_FREE
));
3332 if (available_memory
>= zfs_write_limit_max
)
3335 if (txg
> last_txg
) {
3340 * If we are in pageout, we know that memory is already tight,
3341 * the arc is already going to be evicting, so we just want to
3342 * continue to let page writes occur as quickly as possible.
3344 if (curproc
== proc_pageout
) {
3345 if (page_load
> MAX(ptob(minfree
), available_memory
) / 4)
3347 /* Note: reserve is inflated, so we deflate */
3348 page_load
+= reserve
/ 8;
3350 } else if (page_load
> 0 && arc_reclaim_needed()) {
3351 /* memory is low, delay before restarting */
3352 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
3357 if (arc_size
> arc_c_min
) {
3358 uint64_t evictable_memory
=
3359 arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] +
3360 arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] +
3361 arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] +
3362 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
];
3363 available_memory
+= MIN(evictable_memory
, arc_size
- arc_c_min
);
3366 if (inflight_data
> available_memory
/ 4) {
3367 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
3375 arc_tempreserve_clear(uint64_t reserve
)
3377 atomic_add_64(&arc_tempreserve
, -reserve
);
3378 ASSERT((int64_t)arc_tempreserve
>= 0);
3382 arc_tempreserve_space(uint64_t reserve
, uint64_t txg
)
3389 * Once in a while, fail for no reason. Everything should cope.
3391 if (spa_get_random(10000) == 0) {
3392 dprintf("forcing random failure\n");
3396 if (reserve
> arc_c
/4 && !arc_no_grow
)
3397 arc_c
= MIN(arc_c_max
, reserve
* 4);
3398 if (reserve
> arc_c
)
3402 * Don't count loaned bufs as in flight dirty data to prevent long
3403 * network delays from blocking transactions that are ready to be
3404 * assigned to a txg.
3406 anon_size
= MAX((int64_t)(arc_anon
->arcs_size
- arc_loaned_bytes
), 0);
3409 * Writes will, almost always, require additional memory allocations
3410 * in order to compress/encrypt/etc the data. We therefor need to
3411 * make sure that there is sufficient available memory for this.
3413 if (error
= arc_memory_throttle(reserve
, anon_size
, txg
))
3417 * Throttle writes when the amount of dirty data in the cache
3418 * gets too large. We try to keep the cache less than half full
3419 * of dirty blocks so that our sync times don't grow too large.
3420 * Note: if two requests come in concurrently, we might let them
3421 * both succeed, when one of them should fail. Not a huge deal.
3424 if (reserve
+ arc_tempreserve
+ anon_size
> arc_c
/ 2 &&
3425 anon_size
> arc_c
/ 4) {
3426 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3427 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3428 arc_tempreserve
>>10,
3429 arc_anon
->arcs_lsize
[ARC_BUFC_METADATA
]>>10,
3430 arc_anon
->arcs_lsize
[ARC_BUFC_DATA
]>>10,
3431 reserve
>>10, arc_c
>>10);
3434 atomic_add_64(&arc_tempreserve
, reserve
);
3441 mutex_init(&arc_reclaim_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3442 cv_init(&arc_reclaim_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
3444 /* Convert seconds to clock ticks */
3445 arc_min_prefetch_lifespan
= 1 * hz
;
3447 /* Start out with 1/8 of all memory */
3448 arc_c
= physmem
* PAGESIZE
/ 8;
3452 * On architectures where the physical memory can be larger
3453 * than the addressable space (intel in 32-bit mode), we may
3454 * need to limit the cache to 1/8 of VM size.
3456 arc_c
= MIN(arc_c
, vmem_size(heap_arena
, VMEM_ALLOC
| VMEM_FREE
) / 8);
3459 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3460 arc_c_min
= MAX(arc_c
/ 4, 64<<20);
3461 /* set max to 3/4 of all memory, or all but 1GB, whichever is more */
3462 if (arc_c
* 8 >= 1<<30)
3463 arc_c_max
= (arc_c
* 8) - (1<<30);
3465 arc_c_max
= arc_c_min
;
3466 arc_c_max
= MAX(arc_c
* 6, arc_c_max
);
3469 * Allow the tunables to override our calculations if they are
3470 * reasonable (ie. over 64MB)
3472 if (zfs_arc_max
> 64<<20 && zfs_arc_max
< physmem
* PAGESIZE
)
3473 arc_c_max
= zfs_arc_max
;
3474 if (zfs_arc_min
> 64<<20 && zfs_arc_min
<= arc_c_max
)
3475 arc_c_min
= zfs_arc_min
;
3478 arc_p
= (arc_c
>> 1);
3480 /* limit meta-data to 1/4 of the arc capacity */
3481 arc_meta_limit
= arc_c_max
/ 4;
3483 /* Allow the tunable to override if it is reasonable */
3484 if (zfs_arc_meta_limit
> 0 && zfs_arc_meta_limit
<= arc_c_max
)
3485 arc_meta_limit
= zfs_arc_meta_limit
;
3487 if (arc_c_min
< arc_meta_limit
/ 2 && zfs_arc_min
== 0)
3488 arc_c_min
= arc_meta_limit
/ 2;
3490 if (zfs_arc_grow_retry
> 0)
3491 arc_grow_retry
= zfs_arc_grow_retry
;
3493 if (zfs_arc_shrink_shift
> 0)
3494 arc_shrink_shift
= zfs_arc_shrink_shift
;
3496 if (zfs_arc_p_min_shift
> 0)
3497 arc_p_min_shift
= zfs_arc_p_min_shift
;
3499 /* if kmem_flags are set, lets try to use less memory */
3500 if (kmem_debugging())
3502 if (arc_c
< arc_c_min
)
3505 arc_anon
= &ARC_anon
;
3507 arc_mru_ghost
= &ARC_mru_ghost
;
3509 arc_mfu_ghost
= &ARC_mfu_ghost
;
3510 arc_l2c_only
= &ARC_l2c_only
;
3513 mutex_init(&arc_anon
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3514 mutex_init(&arc_mru
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3515 mutex_init(&arc_mru_ghost
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3516 mutex_init(&arc_mfu
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3517 mutex_init(&arc_mfu_ghost
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3518 mutex_init(&arc_l2c_only
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3520 list_create(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
3521 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3522 list_create(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
3523 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3524 list_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
3525 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3526 list_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
3527 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3528 list_create(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
3529 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3530 list_create(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
3531 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3532 list_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
3533 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3534 list_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
3535 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3536 list_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
3537 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3538 list_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
3539 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3543 arc_thread_exit
= 0;
3544 arc_eviction_list
= NULL
;
3545 mutex_init(&arc_eviction_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3546 bzero(&arc_eviction_hdr
, sizeof (arc_buf_hdr_t
));
3548 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
3549 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
3551 if (arc_ksp
!= NULL
) {
3552 arc_ksp
->ks_data
= &arc_stats
;
3553 kstat_install(arc_ksp
);
3556 (void) thread_create(NULL
, 0, arc_reclaim_thread
, NULL
, 0, &p0
,
3557 TS_RUN
, minclsyspri
);
3562 if (zfs_write_limit_max
== 0)
3563 zfs_write_limit_max
= ptob(physmem
) >> zfs_write_limit_shift
;
3565 zfs_write_limit_shift
= 0;
3566 mutex_init(&zfs_write_limit_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3572 mutex_enter(&arc_reclaim_thr_lock
);
3573 arc_thread_exit
= 1;
3574 while (arc_thread_exit
!= 0)
3575 cv_wait(&arc_reclaim_thr_cv
, &arc_reclaim_thr_lock
);
3576 mutex_exit(&arc_reclaim_thr_lock
);
3582 if (arc_ksp
!= NULL
) {
3583 kstat_delete(arc_ksp
);
3587 mutex_destroy(&arc_eviction_mtx
);
3588 mutex_destroy(&arc_reclaim_thr_lock
);
3589 cv_destroy(&arc_reclaim_thr_cv
);
3591 list_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
3592 list_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
3593 list_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
3594 list_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
3595 list_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
3596 list_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
3597 list_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
3598 list_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
3600 mutex_destroy(&arc_anon
->arcs_mtx
);
3601 mutex_destroy(&arc_mru
->arcs_mtx
);
3602 mutex_destroy(&arc_mru_ghost
->arcs_mtx
);
3603 mutex_destroy(&arc_mfu
->arcs_mtx
);
3604 mutex_destroy(&arc_mfu_ghost
->arcs_mtx
);
3605 mutex_destroy(&arc_l2c_only
->arcs_mtx
);
3607 mutex_destroy(&zfs_write_limit_lock
);
3611 ASSERT(arc_loaned_bytes
== 0);
3617 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3618 * It uses dedicated storage devices to hold cached data, which are populated
3619 * using large infrequent writes. The main role of this cache is to boost
3620 * the performance of random read workloads. The intended L2ARC devices
3621 * include short-stroked disks, solid state disks, and other media with
3622 * substantially faster read latency than disk.
3624 * +-----------------------+
3626 * +-----------------------+
3629 * l2arc_feed_thread() arc_read()
3633 * +---------------+ |
3635 * +---------------+ |
3640 * +-------+ +-------+
3642 * | cache | | cache |
3643 * +-------+ +-------+
3644 * +=========+ .-----.
3645 * : L2ARC : |-_____-|
3646 * : devices : | Disks |
3647 * +=========+ `-_____-'
3649 * Read requests are satisfied from the following sources, in order:
3652 * 2) vdev cache of L2ARC devices
3654 * 4) vdev cache of disks
3657 * Some L2ARC device types exhibit extremely slow write performance.
3658 * To accommodate for this there are some significant differences between
3659 * the L2ARC and traditional cache design:
3661 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
3662 * the ARC behave as usual, freeing buffers and placing headers on ghost
3663 * lists. The ARC does not send buffers to the L2ARC during eviction as
3664 * this would add inflated write latencies for all ARC memory pressure.
3666 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
3667 * It does this by periodically scanning buffers from the eviction-end of
3668 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
3669 * not already there. It scans until a headroom of buffers is satisfied,
3670 * which itself is a buffer for ARC eviction. The thread that does this is
3671 * l2arc_feed_thread(), illustrated below; example sizes are included to
3672 * provide a better sense of ratio than this diagram:
3675 * +---------------------+----------+
3676 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
3677 * +---------------------+----------+ | o L2ARC eligible
3678 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
3679 * +---------------------+----------+ |
3680 * 15.9 Gbytes ^ 32 Mbytes |
3682 * l2arc_feed_thread()
3684 * l2arc write hand <--[oooo]--'
3688 * +==============================+
3689 * L2ARC dev |####|#|###|###| |####| ... |
3690 * +==============================+
3693 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
3694 * evicted, then the L2ARC has cached a buffer much sooner than it probably
3695 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
3696 * safe to say that this is an uncommon case, since buffers at the end of
3697 * the ARC lists have moved there due to inactivity.
3699 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
3700 * then the L2ARC simply misses copying some buffers. This serves as a
3701 * pressure valve to prevent heavy read workloads from both stalling the ARC
3702 * with waits and clogging the L2ARC with writes. This also helps prevent
3703 * the potential for the L2ARC to churn if it attempts to cache content too
3704 * quickly, such as during backups of the entire pool.
3706 * 5. After system boot and before the ARC has filled main memory, there are
3707 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
3708 * lists can remain mostly static. Instead of searching from tail of these
3709 * lists as pictured, the l2arc_feed_thread() will search from the list heads
3710 * for eligible buffers, greatly increasing its chance of finding them.
3712 * The L2ARC device write speed is also boosted during this time so that
3713 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
3714 * there are no L2ARC reads, and no fear of degrading read performance
3715 * through increased writes.
3717 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
3718 * the vdev queue can aggregate them into larger and fewer writes. Each
3719 * device is written to in a rotor fashion, sweeping writes through
3720 * available space then repeating.
3722 * 7. The L2ARC does not store dirty content. It never needs to flush
3723 * write buffers back to disk based storage.
3725 * 8. If an ARC buffer is written (and dirtied) which also exists in the
3726 * L2ARC, the now stale L2ARC buffer is immediately dropped.
3728 * The performance of the L2ARC can be tweaked by a number of tunables, which
3729 * may be necessary for different workloads:
3731 * l2arc_write_max max write bytes per interval
3732 * l2arc_write_boost extra write bytes during device warmup
3733 * l2arc_noprefetch skip caching prefetched buffers
3734 * l2arc_headroom number of max device writes to precache
3735 * l2arc_feed_secs seconds between L2ARC writing
3737 * Tunables may be removed or added as future performance improvements are
3738 * integrated, and also may become zpool properties.
3740 * There are three key functions that control how the L2ARC warms up:
3742 * l2arc_write_eligible() check if a buffer is eligible to cache
3743 * l2arc_write_size() calculate how much to write
3744 * l2arc_write_interval() calculate sleep delay between writes
3746 * These three functions determine what to write, how much, and how quickly
3751 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*ab
)
3754 * A buffer is *not* eligible for the L2ARC if it:
3755 * 1. belongs to a different spa.
3756 * 2. is already cached on the L2ARC.
3757 * 3. has an I/O in progress (it may be an incomplete read).
3758 * 4. is flagged not eligible (zfs property).
3760 if (ab
->b_spa
!= spa_guid
|| ab
->b_l2hdr
!= NULL
||
3761 HDR_IO_IN_PROGRESS(ab
) || !HDR_L2CACHE(ab
))
3768 l2arc_write_size(l2arc_dev_t
*dev
)
3772 size
= dev
->l2ad_write
;
3774 if (arc_warm
== B_FALSE
)
3775 size
+= dev
->l2ad_boost
;
3782 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
3784 clock_t interval
, next
, now
;
3787 * If the ARC lists are busy, increase our write rate; if the
3788 * lists are stale, idle back. This is achieved by checking
3789 * how much we previously wrote - if it was more than half of
3790 * what we wanted, schedule the next write much sooner.
3792 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
3793 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
3795 interval
= hz
* l2arc_feed_secs
;
3797 now
= ddi_get_lbolt();
3798 next
= MAX(now
, MIN(now
+ interval
, began
+ interval
));
3804 l2arc_hdr_stat_add(void)
3806 ARCSTAT_INCR(arcstat_l2_hdr_size
, HDR_SIZE
+ L2HDR_SIZE
);
3807 ARCSTAT_INCR(arcstat_hdr_size
, -HDR_SIZE
);
3811 l2arc_hdr_stat_remove(void)
3813 ARCSTAT_INCR(arcstat_l2_hdr_size
, -(HDR_SIZE
+ L2HDR_SIZE
));
3814 ARCSTAT_INCR(arcstat_hdr_size
, HDR_SIZE
);
3818 * Cycle through L2ARC devices. This is how L2ARC load balances.
3819 * If a device is returned, this also returns holding the spa config lock.
3821 static l2arc_dev_t
*
3822 l2arc_dev_get_next(void)
3824 l2arc_dev_t
*first
, *next
= NULL
;
3827 * Lock out the removal of spas (spa_namespace_lock), then removal
3828 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
3829 * both locks will be dropped and a spa config lock held instead.
3831 mutex_enter(&spa_namespace_lock
);
3832 mutex_enter(&l2arc_dev_mtx
);
3834 /* if there are no vdevs, there is nothing to do */
3835 if (l2arc_ndev
== 0)
3839 next
= l2arc_dev_last
;
3841 /* loop around the list looking for a non-faulted vdev */
3843 next
= list_head(l2arc_dev_list
);
3845 next
= list_next(l2arc_dev_list
, next
);
3847 next
= list_head(l2arc_dev_list
);
3850 /* if we have come back to the start, bail out */
3853 else if (next
== first
)
3856 } while (vdev_is_dead(next
->l2ad_vdev
));
3858 /* if we were unable to find any usable vdevs, return NULL */
3859 if (vdev_is_dead(next
->l2ad_vdev
))
3862 l2arc_dev_last
= next
;
3865 mutex_exit(&l2arc_dev_mtx
);
3868 * Grab the config lock to prevent the 'next' device from being
3869 * removed while we are writing to it.
3872 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
3873 mutex_exit(&spa_namespace_lock
);
3879 * Free buffers that were tagged for destruction.
3882 l2arc_do_free_on_write()
3885 l2arc_data_free_t
*df
, *df_prev
;
3887 mutex_enter(&l2arc_free_on_write_mtx
);
3888 buflist
= l2arc_free_on_write
;
3890 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
3891 df_prev
= list_prev(buflist
, df
);
3892 ASSERT(df
->l2df_data
!= NULL
);
3893 ASSERT(df
->l2df_func
!= NULL
);
3894 df
->l2df_func(df
->l2df_data
, df
->l2df_size
);
3895 list_remove(buflist
, df
);
3896 kmem_free(df
, sizeof (l2arc_data_free_t
));
3899 mutex_exit(&l2arc_free_on_write_mtx
);
3903 * A write to a cache device has completed. Update all headers to allow
3904 * reads from these buffers to begin.
3907 l2arc_write_done(zio_t
*zio
)
3909 l2arc_write_callback_t
*cb
;
3912 arc_buf_hdr_t
*head
, *ab
, *ab_prev
;
3913 l2arc_buf_hdr_t
*abl2
;
3914 kmutex_t
*hash_lock
;
3916 cb
= zio
->io_private
;
3918 dev
= cb
->l2wcb_dev
;
3919 ASSERT(dev
!= NULL
);
3920 head
= cb
->l2wcb_head
;
3921 ASSERT(head
!= NULL
);
3922 buflist
= dev
->l2ad_buflist
;
3923 ASSERT(buflist
!= NULL
);
3924 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
3925 l2arc_write_callback_t
*, cb
);
3927 if (zio
->io_error
!= 0)
3928 ARCSTAT_BUMP(arcstat_l2_writes_error
);
3930 mutex_enter(&l2arc_buflist_mtx
);
3933 * All writes completed, or an error was hit.
3935 for (ab
= list_prev(buflist
, head
); ab
; ab
= ab_prev
) {
3936 ab_prev
= list_prev(buflist
, ab
);
3938 hash_lock
= HDR_LOCK(ab
);
3939 if (!mutex_tryenter(hash_lock
)) {
3941 * This buffer misses out. It may be in a stage
3942 * of eviction. Its ARC_L2_WRITING flag will be
3943 * left set, denying reads to this buffer.
3945 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss
);
3949 if (zio
->io_error
!= 0) {
3951 * Error - drop L2ARC entry.
3953 list_remove(buflist
, ab
);
3956 kmem_free(abl2
, sizeof (l2arc_buf_hdr_t
));
3957 ARCSTAT_INCR(arcstat_l2_size
, -ab
->b_size
);
3961 * Allow ARC to begin reads to this L2ARC entry.
3963 ab
->b_flags
&= ~ARC_L2_WRITING
;
3965 mutex_exit(hash_lock
);
3968 atomic_inc_64(&l2arc_writes_done
);
3969 list_remove(buflist
, head
);
3970 kmem_cache_free(hdr_cache
, head
);
3971 mutex_exit(&l2arc_buflist_mtx
);
3973 l2arc_do_free_on_write();
3975 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
3979 * A read to a cache device completed. Validate buffer contents before
3980 * handing over to the regular ARC routines.
3983 l2arc_read_done(zio_t
*zio
)
3985 l2arc_read_callback_t
*cb
;
3988 kmutex_t
*hash_lock
;
3991 ASSERT(zio
->io_vd
!= NULL
);
3992 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
3994 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
3996 cb
= zio
->io_private
;
3998 buf
= cb
->l2rcb_buf
;
3999 ASSERT(buf
!= NULL
);
4001 hash_lock
= HDR_LOCK(buf
->b_hdr
);
4002 mutex_enter(hash_lock
);
4004 ASSERT3P(hash_lock
, ==, HDR_LOCK(hdr
));
4007 * Check this survived the L2ARC journey.
4009 equal
= arc_cksum_equal(buf
);
4010 if (equal
&& zio
->io_error
== 0 && !HDR_L2_EVICTED(hdr
)) {
4011 mutex_exit(hash_lock
);
4012 zio
->io_private
= buf
;
4013 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
4014 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
4017 mutex_exit(hash_lock
);
4019 * Buffer didn't survive caching. Increment stats and
4020 * reissue to the original storage device.
4022 if (zio
->io_error
!= 0) {
4023 ARCSTAT_BUMP(arcstat_l2_io_error
);
4025 zio
->io_error
= EIO
;
4028 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
4031 * If there's no waiter, issue an async i/o to the primary
4032 * storage now. If there *is* a waiter, the caller must
4033 * issue the i/o in a context where it's OK to block.
4035 if (zio
->io_waiter
== NULL
) {
4036 zio_t
*pio
= zio_unique_parent(zio
);
4038 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
4040 zio_nowait(zio_read(pio
, cb
->l2rcb_spa
, &cb
->l2rcb_bp
,
4041 buf
->b_data
, zio
->io_size
, arc_read_done
, buf
,
4042 zio
->io_priority
, cb
->l2rcb_flags
, &cb
->l2rcb_zb
));
4046 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
4050 * This is the list priority from which the L2ARC will search for pages to
4051 * cache. This is used within loops (0..3) to cycle through lists in the
4052 * desired order. This order can have a significant effect on cache
4055 * Currently the metadata lists are hit first, MFU then MRU, followed by
4056 * the data lists. This function returns a locked list, and also returns
4060 l2arc_list_locked(int list_num
, kmutex_t
**lock
)
4064 ASSERT(list_num
>= 0 && list_num
<= 3);
4068 list
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
4069 *lock
= &arc_mfu
->arcs_mtx
;
4072 list
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
4073 *lock
= &arc_mru
->arcs_mtx
;
4076 list
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
4077 *lock
= &arc_mfu
->arcs_mtx
;
4080 list
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
4081 *lock
= &arc_mru
->arcs_mtx
;
4085 ASSERT(!(MUTEX_HELD(*lock
)));
4091 * Evict buffers from the device write hand to the distance specified in
4092 * bytes. This distance may span populated buffers, it may span nothing.
4093 * This is clearing a region on the L2ARC device ready for writing.
4094 * If the 'all' boolean is set, every buffer is evicted.
4097 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
4100 l2arc_buf_hdr_t
*abl2
;
4101 arc_buf_hdr_t
*ab
, *ab_prev
;
4102 kmutex_t
*hash_lock
;
4105 buflist
= dev
->l2ad_buflist
;
4107 if (buflist
== NULL
)
4110 if (!all
&& dev
->l2ad_first
) {
4112 * This is the first sweep through the device. There is
4118 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
4120 * When nearing the end of the device, evict to the end
4121 * before the device write hand jumps to the start.
4123 taddr
= dev
->l2ad_end
;
4125 taddr
= dev
->l2ad_hand
+ distance
;
4127 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
4128 uint64_t, taddr
, boolean_t
, all
);
4131 mutex_enter(&l2arc_buflist_mtx
);
4132 for (ab
= list_tail(buflist
); ab
; ab
= ab_prev
) {
4133 ab_prev
= list_prev(buflist
, ab
);
4135 hash_lock
= HDR_LOCK(ab
);
4136 if (!mutex_tryenter(hash_lock
)) {
4138 * Missed the hash lock. Retry.
4140 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
4141 mutex_exit(&l2arc_buflist_mtx
);
4142 mutex_enter(hash_lock
);
4143 mutex_exit(hash_lock
);
4147 if (HDR_L2_WRITE_HEAD(ab
)) {
4149 * We hit a write head node. Leave it for
4150 * l2arc_write_done().
4152 list_remove(buflist
, ab
);
4153 mutex_exit(hash_lock
);
4157 if (!all
&& ab
->b_l2hdr
!= NULL
&&
4158 (ab
->b_l2hdr
->b_daddr
> taddr
||
4159 ab
->b_l2hdr
->b_daddr
< dev
->l2ad_hand
)) {
4161 * We've evicted to the target address,
4162 * or the end of the device.
4164 mutex_exit(hash_lock
);
4168 if (HDR_FREE_IN_PROGRESS(ab
)) {
4170 * Already on the path to destruction.
4172 mutex_exit(hash_lock
);
4176 if (ab
->b_state
== arc_l2c_only
) {
4177 ASSERT(!HDR_L2_READING(ab
));
4179 * This doesn't exist in the ARC. Destroy.
4180 * arc_hdr_destroy() will call list_remove()
4181 * and decrement arcstat_l2_size.
4183 arc_change_state(arc_anon
, ab
, hash_lock
);
4184 arc_hdr_destroy(ab
);
4187 * Invalidate issued or about to be issued
4188 * reads, since we may be about to write
4189 * over this location.
4191 if (HDR_L2_READING(ab
)) {
4192 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
4193 ab
->b_flags
|= ARC_L2_EVICTED
;
4197 * Tell ARC this no longer exists in L2ARC.
4199 if (ab
->b_l2hdr
!= NULL
) {
4202 kmem_free(abl2
, sizeof (l2arc_buf_hdr_t
));
4203 ARCSTAT_INCR(arcstat_l2_size
, -ab
->b_size
);
4205 list_remove(buflist
, ab
);
4208 * This may have been leftover after a
4211 ab
->b_flags
&= ~ARC_L2_WRITING
;
4213 mutex_exit(hash_lock
);
4215 mutex_exit(&l2arc_buflist_mtx
);
4217 vdev_space_update(dev
->l2ad_vdev
, -(taddr
- dev
->l2ad_evict
), 0, 0);
4218 dev
->l2ad_evict
= taddr
;
4222 * Find and write ARC buffers to the L2ARC device.
4224 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4225 * for reading until they have completed writing.
4228 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
)
4230 arc_buf_hdr_t
*ab
, *ab_prev
, *head
;
4231 l2arc_buf_hdr_t
*hdrl2
;
4233 uint64_t passed_sz
, write_sz
, buf_sz
, headroom
;
4235 kmutex_t
*hash_lock
, *list_lock
;
4236 boolean_t have_lock
, full
;
4237 l2arc_write_callback_t
*cb
;
4239 uint64_t guid
= spa_load_guid(spa
);
4241 ASSERT(dev
->l2ad_vdev
!= NULL
);
4246 head
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
4247 head
->b_flags
|= ARC_L2_WRITE_HEAD
;
4250 * Copy buffers for L2ARC writing.
4252 mutex_enter(&l2arc_buflist_mtx
);
4253 for (int try = 0; try <= 3; try++) {
4254 list
= l2arc_list_locked(try, &list_lock
);
4258 * L2ARC fast warmup.
4260 * Until the ARC is warm and starts to evict, read from the
4261 * head of the ARC lists rather than the tail.
4263 headroom
= target_sz
* l2arc_headroom
;
4264 if (arc_warm
== B_FALSE
)
4265 ab
= list_head(list
);
4267 ab
= list_tail(list
);
4269 for (; ab
; ab
= ab_prev
) {
4270 if (arc_warm
== B_FALSE
)
4271 ab_prev
= list_next(list
, ab
);
4273 ab_prev
= list_prev(list
, ab
);
4275 hash_lock
= HDR_LOCK(ab
);
4276 have_lock
= MUTEX_HELD(hash_lock
);
4277 if (!have_lock
&& !mutex_tryenter(hash_lock
)) {
4279 * Skip this buffer rather than waiting.
4284 passed_sz
+= ab
->b_size
;
4285 if (passed_sz
> headroom
) {
4289 mutex_exit(hash_lock
);
4293 if (!l2arc_write_eligible(guid
, ab
)) {
4294 mutex_exit(hash_lock
);
4298 if ((write_sz
+ ab
->b_size
) > target_sz
) {
4300 mutex_exit(hash_lock
);
4306 * Insert a dummy header on the buflist so
4307 * l2arc_write_done() can find where the
4308 * write buffers begin without searching.
4310 list_insert_head(dev
->l2ad_buflist
, head
);
4313 sizeof (l2arc_write_callback_t
), KM_SLEEP
);
4314 cb
->l2wcb_dev
= dev
;
4315 cb
->l2wcb_head
= head
;
4316 pio
= zio_root(spa
, l2arc_write_done
, cb
,
4321 * Create and add a new L2ARC header.
4323 hdrl2
= kmem_zalloc(sizeof (l2arc_buf_hdr_t
), KM_SLEEP
);
4325 hdrl2
->b_daddr
= dev
->l2ad_hand
;
4327 ab
->b_flags
|= ARC_L2_WRITING
;
4328 ab
->b_l2hdr
= hdrl2
;
4329 list_insert_head(dev
->l2ad_buflist
, ab
);
4330 buf_data
= ab
->b_buf
->b_data
;
4331 buf_sz
= ab
->b_size
;
4334 * Compute and store the buffer cksum before
4335 * writing. On debug the cksum is verified first.
4337 arc_cksum_verify(ab
->b_buf
);
4338 arc_cksum_compute(ab
->b_buf
, B_TRUE
);
4340 mutex_exit(hash_lock
);
4342 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
4343 dev
->l2ad_hand
, buf_sz
, buf_data
, ZIO_CHECKSUM_OFF
,
4344 NULL
, NULL
, ZIO_PRIORITY_ASYNC_WRITE
,
4345 ZIO_FLAG_CANFAIL
, B_FALSE
);
4347 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
4349 (void) zio_nowait(wzio
);
4352 * Keep the clock hand suitably device-aligned.
4354 buf_sz
= vdev_psize_to_asize(dev
->l2ad_vdev
, buf_sz
);
4357 dev
->l2ad_hand
+= buf_sz
;
4360 mutex_exit(list_lock
);
4365 mutex_exit(&l2arc_buflist_mtx
);
4368 ASSERT3U(write_sz
, ==, 0);
4369 kmem_cache_free(hdr_cache
, head
);
4373 ASSERT3U(write_sz
, <=, target_sz
);
4374 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
4375 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_sz
);
4376 ARCSTAT_INCR(arcstat_l2_size
, write_sz
);
4377 vdev_space_update(dev
->l2ad_vdev
, write_sz
, 0, 0);
4380 * Bump device hand to the device start if it is approaching the end.
4381 * l2arc_evict() will already have evicted ahead for this case.
4383 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
4384 vdev_space_update(dev
->l2ad_vdev
,
4385 dev
->l2ad_end
- dev
->l2ad_hand
, 0, 0);
4386 dev
->l2ad_hand
= dev
->l2ad_start
;
4387 dev
->l2ad_evict
= dev
->l2ad_start
;
4388 dev
->l2ad_first
= B_FALSE
;
4391 dev
->l2ad_writing
= B_TRUE
;
4392 (void) zio_wait(pio
);
4393 dev
->l2ad_writing
= B_FALSE
;
4399 * This thread feeds the L2ARC at regular intervals. This is the beating
4400 * heart of the L2ARC.
4403 l2arc_feed_thread(void)
4408 uint64_t size
, wrote
;
4409 clock_t begin
, next
= ddi_get_lbolt();
4411 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
4413 mutex_enter(&l2arc_feed_thr_lock
);
4415 while (l2arc_thread_exit
== 0) {
4416 CALLB_CPR_SAFE_BEGIN(&cpr
);
4417 (void) cv_timedwait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
,
4419 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
4420 next
= ddi_get_lbolt() + hz
;
4423 * Quick check for L2ARC devices.
4425 mutex_enter(&l2arc_dev_mtx
);
4426 if (l2arc_ndev
== 0) {
4427 mutex_exit(&l2arc_dev_mtx
);
4430 mutex_exit(&l2arc_dev_mtx
);
4431 begin
= ddi_get_lbolt();
4434 * This selects the next l2arc device to write to, and in
4435 * doing so the next spa to feed from: dev->l2ad_spa. This
4436 * will return NULL if there are now no l2arc devices or if
4437 * they are all faulted.
4439 * If a device is returned, its spa's config lock is also
4440 * held to prevent device removal. l2arc_dev_get_next()
4441 * will grab and release l2arc_dev_mtx.
4443 if ((dev
= l2arc_dev_get_next()) == NULL
)
4446 spa
= dev
->l2ad_spa
;
4447 ASSERT(spa
!= NULL
);
4450 * If the pool is read-only then force the feed thread to
4451 * sleep a little longer.
4453 if (!spa_writeable(spa
)) {
4454 next
= ddi_get_lbolt() + 5 * l2arc_feed_secs
* hz
;
4455 spa_config_exit(spa
, SCL_L2ARC
, dev
);
4460 * Avoid contributing to memory pressure.
4462 if (arc_reclaim_needed()) {
4463 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
4464 spa_config_exit(spa
, SCL_L2ARC
, dev
);
4468 ARCSTAT_BUMP(arcstat_l2_feeds
);
4470 size
= l2arc_write_size(dev
);
4473 * Evict L2ARC buffers that will be overwritten.
4475 l2arc_evict(dev
, size
, B_FALSE
);
4478 * Write ARC buffers.
4480 wrote
= l2arc_write_buffers(spa
, dev
, size
);
4483 * Calculate interval between writes.
4485 next
= l2arc_write_interval(begin
, size
, wrote
);
4486 spa_config_exit(spa
, SCL_L2ARC
, dev
);
4489 l2arc_thread_exit
= 0;
4490 cv_broadcast(&l2arc_feed_thr_cv
);
4491 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
4496 l2arc_vdev_present(vdev_t
*vd
)
4500 mutex_enter(&l2arc_dev_mtx
);
4501 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
4502 dev
= list_next(l2arc_dev_list
, dev
)) {
4503 if (dev
->l2ad_vdev
== vd
)
4506 mutex_exit(&l2arc_dev_mtx
);
4508 return (dev
!= NULL
);
4512 * Add a vdev for use by the L2ARC. By this point the spa has already
4513 * validated the vdev and opened it.
4516 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
4518 l2arc_dev_t
*adddev
;
4520 ASSERT(!l2arc_vdev_present(vd
));
4523 * Create a new l2arc device entry.
4525 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
4526 adddev
->l2ad_spa
= spa
;
4527 adddev
->l2ad_vdev
= vd
;
4528 adddev
->l2ad_write
= l2arc_write_max
;
4529 adddev
->l2ad_boost
= l2arc_write_boost
;
4530 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
4531 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
4532 adddev
->l2ad_hand
= adddev
->l2ad_start
;
4533 adddev
->l2ad_evict
= adddev
->l2ad_start
;
4534 adddev
->l2ad_first
= B_TRUE
;
4535 adddev
->l2ad_writing
= B_FALSE
;
4536 ASSERT3U(adddev
->l2ad_write
, >, 0);
4539 * This is a list of all ARC buffers that are still valid on the
4542 adddev
->l2ad_buflist
= kmem_zalloc(sizeof (list_t
), KM_SLEEP
);
4543 list_create(adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
4544 offsetof(arc_buf_hdr_t
, b_l2node
));
4546 vdev_space_update(vd
, 0, 0, adddev
->l2ad_end
- adddev
->l2ad_hand
);
4549 * Add device to global list
4551 mutex_enter(&l2arc_dev_mtx
);
4552 list_insert_head(l2arc_dev_list
, adddev
);
4553 atomic_inc_64(&l2arc_ndev
);
4554 mutex_exit(&l2arc_dev_mtx
);
4558 * Remove a vdev from the L2ARC.
4561 l2arc_remove_vdev(vdev_t
*vd
)
4563 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
4566 * Find the device by vdev
4568 mutex_enter(&l2arc_dev_mtx
);
4569 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
4570 nextdev
= list_next(l2arc_dev_list
, dev
);
4571 if (vd
== dev
->l2ad_vdev
) {
4576 ASSERT(remdev
!= NULL
);
4579 * Remove device from global list
4581 list_remove(l2arc_dev_list
, remdev
);
4582 l2arc_dev_last
= NULL
; /* may have been invalidated */
4583 atomic_dec_64(&l2arc_ndev
);
4584 mutex_exit(&l2arc_dev_mtx
);
4587 * Clear all buflists and ARC references. L2ARC device flush.
4589 l2arc_evict(remdev
, 0, B_TRUE
);
4590 list_destroy(remdev
->l2ad_buflist
);
4591 kmem_free(remdev
->l2ad_buflist
, sizeof (list_t
));
4592 kmem_free(remdev
, sizeof (l2arc_dev_t
));
4598 l2arc_thread_exit
= 0;
4600 l2arc_writes_sent
= 0;
4601 l2arc_writes_done
= 0;
4603 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
4604 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
4605 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4606 mutex_init(&l2arc_buflist_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4607 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4609 l2arc_dev_list
= &L2ARC_dev_list
;
4610 l2arc_free_on_write
= &L2ARC_free_on_write
;
4611 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
4612 offsetof(l2arc_dev_t
, l2ad_node
));
4613 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
4614 offsetof(l2arc_data_free_t
, l2df_list_node
));
4621 * This is called from dmu_fini(), which is called from spa_fini();
4622 * Because of this, we can assume that all l2arc devices have
4623 * already been removed when the pools themselves were removed.
4626 l2arc_do_free_on_write();
4628 mutex_destroy(&l2arc_feed_thr_lock
);
4629 cv_destroy(&l2arc_feed_thr_cv
);
4630 mutex_destroy(&l2arc_dev_mtx
);
4631 mutex_destroy(&l2arc_buflist_mtx
);
4632 mutex_destroy(&l2arc_free_on_write_mtx
);
4634 list_destroy(l2arc_dev_list
);
4635 list_destroy(l2arc_free_on_write
);
4641 if (!(spa_mode_global
& FWRITE
))
4644 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
4645 TS_RUN
, minclsyspri
);
4651 if (!(spa_mode_global
& FWRITE
))
4654 mutex_enter(&l2arc_feed_thr_lock
);
4655 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
4656 l2arc_thread_exit
= 1;
4657 while (l2arc_thread_exit
!= 0)
4658 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
4659 mutex_exit(&l2arc_feed_thr_lock
);