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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * DVA-based Adjustable Replacement Cache
29 * While much of the theory of operation used here is
30 * based on the self-tuning, low overhead replacement cache
31 * presented by Megiddo and Modha at FAST 2003, there are some
32 * significant differences:
34 * 1. The Megiddo and Modha model assumes any page is evictable.
35 * Pages in its cache cannot be "locked" into memory. This makes
36 * the eviction algorithm simple: evict the last page in the list.
37 * This also make the performance characteristics easy to reason
38 * about. Our cache is not so simple. At any given moment, some
39 * subset of the blocks in the cache are un-evictable because we
40 * have handed out a reference to them. Blocks are only evictable
41 * when there are no external references active. This makes
42 * eviction far more problematic: we choose to evict the evictable
43 * blocks that are the "lowest" in the list.
45 * There are times when it is not possible to evict the requested
46 * space. In these circumstances we are unable to adjust the cache
47 * size. To prevent the cache growing unbounded at these times we
48 * implement a "cache throttle" that slows the flow of new data
49 * into the cache until we can make space available.
51 * 2. The Megiddo and Modha model assumes a fixed cache size.
52 * Pages are evicted when the cache is full and there is a cache
53 * miss. Our model has a variable sized cache. It grows with
54 * high use, but also tries to react to memory pressure from the
55 * operating system: decreasing its size when system memory is
58 * 3. The Megiddo and Modha model assumes a fixed page size. All
59 * elements of the cache are therefor exactly the same size. So
60 * when adjusting the cache size following a cache miss, its simply
61 * a matter of choosing a single page to evict. In our model, we
62 * have variable sized cache blocks (rangeing from 512 bytes to
63 * 128K bytes). We therefor choose a set of blocks to evict to make
64 * space for a cache miss that approximates as closely as possible
65 * the space used by the new block.
67 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
68 * by N. Megiddo & D. Modha, FAST 2003
74 * A new reference to a cache buffer can be obtained in two
75 * ways: 1) via a hash table lookup using the DVA as a key,
76 * or 2) via one of the ARC lists. The arc_read() interface
77 * uses method 1, while the internal arc algorithms for
78 * adjusting the cache use method 2. We therefor provide two
79 * types of locks: 1) the hash table lock array, and 2) the
82 * Buffers do not have their own mutexs, rather they rely on the
83 * hash table mutexs for the bulk of their protection (i.e. most
84 * fields in the arc_buf_hdr_t are protected by these mutexs).
86 * buf_hash_find() returns the appropriate mutex (held) when it
87 * locates the requested buffer in the hash table. It returns
88 * NULL for the mutex if the buffer was not in the table.
90 * buf_hash_remove() expects the appropriate hash mutex to be
91 * already held before it is invoked.
93 * Each arc state also has a mutex which is used to protect the
94 * buffer list associated with the state. When attempting to
95 * obtain a hash table lock while holding an arc list lock you
96 * must use: mutex_tryenter() to avoid deadlock. Also note that
97 * the active state mutex must be held before the ghost state mutex.
99 * Arc buffers may have an associated eviction callback function.
100 * This function will be invoked prior to removing the buffer (e.g.
101 * in arc_do_user_evicts()). Note however that the data associated
102 * with the buffer may be evicted prior to the callback. The callback
103 * must be made with *no locks held* (to prevent deadlock). Additionally,
104 * the users of callbacks must ensure that their private data is
105 * protected from simultaneous callbacks from arc_buf_evict()
106 * and arc_do_user_evicts().
108 * Note that the majority of the performance stats are manipulated
109 * with atomic operations.
111 * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
113 * - L2ARC buflist creation
114 * - L2ARC buflist eviction
115 * - L2ARC write completion, which walks L2ARC buflists
116 * - ARC header destruction, as it removes from L2ARC buflists
117 * - ARC header release, as it removes from L2ARC buflists
122 #include <sys/zio_checksum.h>
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>
137 static kmutex_t arc_reclaim_thr_lock
;
138 static kcondvar_t arc_reclaim_thr_cv
; /* used to signal reclaim thr */
139 static uint8_t arc_thread_exit
;
141 extern int zfs_write_limit_shift
;
142 extern uint64_t zfs_write_limit_max
;
143 extern kmutex_t zfs_write_limit_lock
;
145 #define ARC_REDUCE_DNLC_PERCENT 3
146 uint_t arc_reduce_dnlc_percent
= ARC_REDUCE_DNLC_PERCENT
;
148 typedef enum arc_reclaim_strategy
{
149 ARC_RECLAIM_AGGR
, /* Aggressive reclaim strategy */
150 ARC_RECLAIM_CONS
/* Conservative reclaim strategy */
151 } arc_reclaim_strategy_t
;
153 /* number of seconds before growing cache again */
154 static int arc_grow_retry
= 60;
156 /* shift of arc_c for calculating both min and max arc_p */
157 static int arc_p_min_shift
= 4;
159 /* log2(fraction of arc to reclaim) */
160 static int arc_shrink_shift
= 5;
163 * minimum lifespan of a prefetch block in clock ticks
164 * (initialized in arc_init())
166 static int arc_min_prefetch_lifespan
;
171 * The arc has filled available memory and has now warmed up.
173 static boolean_t arc_warm
;
176 * These tunables are for performance analysis.
178 uint64_t zfs_arc_max
;
179 uint64_t zfs_arc_min
;
180 uint64_t zfs_arc_meta_limit
= 0;
181 int zfs_mdcomp_disable
= 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_hash_elements
;
253 kstat_named_t arcstat_hash_elements_max
;
254 kstat_named_t arcstat_hash_collisions
;
255 kstat_named_t arcstat_hash_chains
;
256 kstat_named_t arcstat_hash_chain_max
;
257 kstat_named_t arcstat_p
;
258 kstat_named_t arcstat_c
;
259 kstat_named_t arcstat_c_min
;
260 kstat_named_t arcstat_c_max
;
261 kstat_named_t arcstat_size
;
262 kstat_named_t arcstat_hdr_size
;
263 kstat_named_t arcstat_data_size
;
264 kstat_named_t arcstat_other_size
;
265 kstat_named_t arcstat_l2_hits
;
266 kstat_named_t arcstat_l2_misses
;
267 kstat_named_t arcstat_l2_feeds
;
268 kstat_named_t arcstat_l2_rw_clash
;
269 kstat_named_t arcstat_l2_read_bytes
;
270 kstat_named_t arcstat_l2_write_bytes
;
271 kstat_named_t arcstat_l2_writes_sent
;
272 kstat_named_t arcstat_l2_writes_done
;
273 kstat_named_t arcstat_l2_writes_error
;
274 kstat_named_t arcstat_l2_writes_hdr_miss
;
275 kstat_named_t arcstat_l2_evict_lock_retry
;
276 kstat_named_t arcstat_l2_evict_reading
;
277 kstat_named_t arcstat_l2_free_on_write
;
278 kstat_named_t arcstat_l2_abort_lowmem
;
279 kstat_named_t arcstat_l2_cksum_bad
;
280 kstat_named_t arcstat_l2_io_error
;
281 kstat_named_t arcstat_l2_size
;
282 kstat_named_t arcstat_l2_hdr_size
;
283 kstat_named_t arcstat_memory_throttle_count
;
286 static arc_stats_t arc_stats
= {
287 { "hits", KSTAT_DATA_UINT64
},
288 { "misses", KSTAT_DATA_UINT64
},
289 { "demand_data_hits", KSTAT_DATA_UINT64
},
290 { "demand_data_misses", KSTAT_DATA_UINT64
},
291 { "demand_metadata_hits", KSTAT_DATA_UINT64
},
292 { "demand_metadata_misses", KSTAT_DATA_UINT64
},
293 { "prefetch_data_hits", KSTAT_DATA_UINT64
},
294 { "prefetch_data_misses", KSTAT_DATA_UINT64
},
295 { "prefetch_metadata_hits", KSTAT_DATA_UINT64
},
296 { "prefetch_metadata_misses", KSTAT_DATA_UINT64
},
297 { "mru_hits", KSTAT_DATA_UINT64
},
298 { "mru_ghost_hits", KSTAT_DATA_UINT64
},
299 { "mfu_hits", KSTAT_DATA_UINT64
},
300 { "mfu_ghost_hits", KSTAT_DATA_UINT64
},
301 { "deleted", KSTAT_DATA_UINT64
},
302 { "recycle_miss", KSTAT_DATA_UINT64
},
303 { "mutex_miss", KSTAT_DATA_UINT64
},
304 { "evict_skip", KSTAT_DATA_UINT64
},
305 { "hash_elements", KSTAT_DATA_UINT64
},
306 { "hash_elements_max", KSTAT_DATA_UINT64
},
307 { "hash_collisions", KSTAT_DATA_UINT64
},
308 { "hash_chains", KSTAT_DATA_UINT64
},
309 { "hash_chain_max", KSTAT_DATA_UINT64
},
310 { "p", KSTAT_DATA_UINT64
},
311 { "c", KSTAT_DATA_UINT64
},
312 { "c_min", KSTAT_DATA_UINT64
},
313 { "c_max", KSTAT_DATA_UINT64
},
314 { "size", KSTAT_DATA_UINT64
},
315 { "hdr_size", KSTAT_DATA_UINT64
},
316 { "data_size", KSTAT_DATA_UINT64
},
317 { "other_size", KSTAT_DATA_UINT64
},
318 { "l2_hits", KSTAT_DATA_UINT64
},
319 { "l2_misses", KSTAT_DATA_UINT64
},
320 { "l2_feeds", KSTAT_DATA_UINT64
},
321 { "l2_rw_clash", KSTAT_DATA_UINT64
},
322 { "l2_read_bytes", KSTAT_DATA_UINT64
},
323 { "l2_write_bytes", KSTAT_DATA_UINT64
},
324 { "l2_writes_sent", KSTAT_DATA_UINT64
},
325 { "l2_writes_done", KSTAT_DATA_UINT64
},
326 { "l2_writes_error", KSTAT_DATA_UINT64
},
327 { "l2_writes_hdr_miss", KSTAT_DATA_UINT64
},
328 { "l2_evict_lock_retry", KSTAT_DATA_UINT64
},
329 { "l2_evict_reading", KSTAT_DATA_UINT64
},
330 { "l2_free_on_write", KSTAT_DATA_UINT64
},
331 { "l2_abort_lowmem", KSTAT_DATA_UINT64
},
332 { "l2_cksum_bad", KSTAT_DATA_UINT64
},
333 { "l2_io_error", KSTAT_DATA_UINT64
},
334 { "l2_size", KSTAT_DATA_UINT64
},
335 { "l2_hdr_size", KSTAT_DATA_UINT64
},
336 { "memory_throttle_count", KSTAT_DATA_UINT64
}
339 #define ARCSTAT(stat) (arc_stats.stat.value.ui64)
341 #define ARCSTAT_INCR(stat, val) \
342 atomic_add_64(&arc_stats.stat.value.ui64, (val));
344 #define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
345 #define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
347 #define ARCSTAT_MAX(stat, val) { \
349 while ((val) > (m = arc_stats.stat.value.ui64) && \
350 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
354 #define ARCSTAT_MAXSTAT(stat) \
355 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
358 * We define a macro to allow ARC hits/misses to be easily broken down by
359 * two separate conditions, giving a total of four different subtypes for
360 * each of hits and misses (so eight statistics total).
362 #define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
365 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
367 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
371 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
373 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
378 static arc_state_t
*arc_anon
;
379 static arc_state_t
*arc_mru
;
380 static arc_state_t
*arc_mru_ghost
;
381 static arc_state_t
*arc_mfu
;
382 static arc_state_t
*arc_mfu_ghost
;
383 static arc_state_t
*arc_l2c_only
;
386 * There are several ARC variables that are critical to export as kstats --
387 * but we don't want to have to grovel around in the kstat whenever we wish to
388 * manipulate them. For these variables, we therefore define them to be in
389 * terms of the statistic variable. This assures that we are not introducing
390 * the possibility of inconsistency by having shadow copies of the variables,
391 * while still allowing the code to be readable.
393 #define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
394 #define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
395 #define arc_c ARCSTAT(arcstat_c) /* target size of cache */
396 #define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
397 #define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
399 static int arc_no_grow
; /* Don't try to grow cache size */
400 static uint64_t arc_tempreserve
;
401 static uint64_t arc_loaned_bytes
;
402 static uint64_t arc_meta_used
;
403 static uint64_t arc_meta_limit
;
404 static uint64_t arc_meta_max
= 0;
406 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t
;
408 typedef struct arc_callback arc_callback_t
;
410 struct arc_callback
{
412 arc_done_func_t
*acb_done
;
414 zio_t
*acb_zio_dummy
;
415 arc_callback_t
*acb_next
;
418 typedef struct arc_write_callback arc_write_callback_t
;
420 struct arc_write_callback
{
422 arc_done_func_t
*awcb_ready
;
423 arc_done_func_t
*awcb_done
;
428 /* protected by hash lock */
433 kmutex_t b_freeze_lock
;
434 zio_cksum_t
*b_freeze_cksum
;
436 arc_buf_hdr_t
*b_hash_next
;
441 arc_callback_t
*b_acb
;
445 arc_buf_contents_t b_type
;
449 /* protected by arc state mutex */
450 arc_state_t
*b_state
;
451 list_node_t b_arc_node
;
453 /* updated atomically */
454 clock_t b_arc_access
;
456 /* self protecting */
459 l2arc_buf_hdr_t
*b_l2hdr
;
460 list_node_t b_l2node
;
463 static arc_buf_t
*arc_eviction_list
;
464 static kmutex_t arc_eviction_mtx
;
465 static arc_buf_hdr_t arc_eviction_hdr
;
466 static void arc_get_data_buf(arc_buf_t
*buf
);
467 static void arc_access(arc_buf_hdr_t
*buf
, kmutex_t
*hash_lock
);
468 static int arc_evict_needed(arc_buf_contents_t type
);
469 static void arc_evict_ghost(arc_state_t
*state
, uint64_t spa
, int64_t bytes
);
471 #define GHOST_STATE(state) \
472 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
473 (state) == arc_l2c_only)
476 * Private ARC flags. These flags are private ARC only flags that will show up
477 * in b_flags in the arc_hdr_buf_t. Some flags are publicly declared, and can
478 * be passed in as arc_flags in things like arc_read. However, these flags
479 * should never be passed and should only be set by ARC code. When adding new
480 * public flags, make sure not to smash the private ones.
483 #define ARC_IN_HASH_TABLE (1 << 9) /* this buffer is hashed */
484 #define ARC_IO_IN_PROGRESS (1 << 10) /* I/O in progress for buf */
485 #define ARC_IO_ERROR (1 << 11) /* I/O failed for buf */
486 #define ARC_FREED_IN_READ (1 << 12) /* buf freed while in read */
487 #define ARC_BUF_AVAILABLE (1 << 13) /* block not in active use */
488 #define ARC_INDIRECT (1 << 14) /* this is an indirect block */
489 #define ARC_FREE_IN_PROGRESS (1 << 15) /* hdr about to be freed */
490 #define ARC_L2_WRITING (1 << 16) /* L2ARC write in progress */
491 #define ARC_L2_EVICTED (1 << 17) /* evicted during I/O */
492 #define ARC_L2_WRITE_HEAD (1 << 18) /* head of write list */
493 #define ARC_STORED (1 << 19) /* has been store()d to */
495 #define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_IN_HASH_TABLE)
496 #define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS)
497 #define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_IO_ERROR)
498 #define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_PREFETCH)
499 #define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FREED_IN_READ)
500 #define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_BUF_AVAILABLE)
501 #define HDR_FREE_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
502 #define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_L2CACHE)
503 #define HDR_L2_READING(hdr) ((hdr)->b_flags & ARC_IO_IN_PROGRESS && \
504 (hdr)->b_l2hdr != NULL)
505 #define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_L2_WRITING)
506 #define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_L2_EVICTED)
507 #define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_L2_WRITE_HEAD)
513 #define HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
514 #define L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
517 * Hash table routines
520 #define HT_LOCK_PAD 64
525 unsigned char pad
[(HT_LOCK_PAD
- sizeof (kmutex_t
))];
529 #define BUF_LOCKS 256
530 typedef struct buf_hash_table
{
532 arc_buf_hdr_t
**ht_table
;
533 struct ht_lock ht_locks
[BUF_LOCKS
];
536 static buf_hash_table_t buf_hash_table
;
538 #define BUF_HASH_INDEX(spa, dva, birth) \
539 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
540 #define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
541 #define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
542 #define HDR_LOCK(buf) \
543 (BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth)))
545 uint64_t zfs_crc64_table
[256];
551 #define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
552 #define L2ARC_HEADROOM 2 /* num of writes */
553 #define L2ARC_FEED_SECS 1 /* caching interval secs */
554 #define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
556 #define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
557 #define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
560 * L2ARC Performance Tunables
562 uint64_t l2arc_write_max
= L2ARC_WRITE_SIZE
; /* default max write size */
563 uint64_t l2arc_write_boost
= L2ARC_WRITE_SIZE
; /* extra write during warmup */
564 uint64_t l2arc_headroom
= L2ARC_HEADROOM
; /* number of dev writes */
565 uint64_t l2arc_feed_secs
= L2ARC_FEED_SECS
; /* interval seconds */
566 uint64_t l2arc_feed_min_ms
= L2ARC_FEED_MIN_MS
; /* min interval milliseconds */
567 boolean_t l2arc_noprefetch
= B_TRUE
; /* don't cache prefetch bufs */
568 boolean_t l2arc_feed_again
= B_TRUE
; /* turbo warmup */
569 boolean_t l2arc_norw
= B_TRUE
; /* no reads during writes */
574 typedef struct l2arc_dev
{
575 vdev_t
*l2ad_vdev
; /* vdev */
576 spa_t
*l2ad_spa
; /* spa */
577 uint64_t l2ad_hand
; /* next write location */
578 uint64_t l2ad_write
; /* desired write size, bytes */
579 uint64_t l2ad_boost
; /* warmup write boost, bytes */
580 uint64_t l2ad_start
; /* first addr on device */
581 uint64_t l2ad_end
; /* last addr on device */
582 uint64_t l2ad_evict
; /* last addr eviction reached */
583 boolean_t l2ad_first
; /* first sweep through */
584 boolean_t l2ad_writing
; /* currently writing */
585 list_t
*l2ad_buflist
; /* buffer list */
586 list_node_t l2ad_node
; /* device list node */
589 static list_t L2ARC_dev_list
; /* device list */
590 static list_t
*l2arc_dev_list
; /* device list pointer */
591 static kmutex_t l2arc_dev_mtx
; /* device list mutex */
592 static l2arc_dev_t
*l2arc_dev_last
; /* last device used */
593 static kmutex_t l2arc_buflist_mtx
; /* mutex for all buflists */
594 static list_t L2ARC_free_on_write
; /* free after write buf list */
595 static list_t
*l2arc_free_on_write
; /* free after write list ptr */
596 static kmutex_t l2arc_free_on_write_mtx
; /* mutex for list */
597 static uint64_t l2arc_ndev
; /* number of devices */
599 typedef struct l2arc_read_callback
{
600 arc_buf_t
*l2rcb_buf
; /* read buffer */
601 spa_t
*l2rcb_spa
; /* spa */
602 blkptr_t l2rcb_bp
; /* original blkptr */
603 zbookmark_t l2rcb_zb
; /* original bookmark */
604 int l2rcb_flags
; /* original flags */
605 } l2arc_read_callback_t
;
607 typedef struct l2arc_write_callback
{
608 l2arc_dev_t
*l2wcb_dev
; /* device info */
609 arc_buf_hdr_t
*l2wcb_head
; /* head of write buflist */
610 } l2arc_write_callback_t
;
612 struct l2arc_buf_hdr
{
613 /* protected by arc_buf_hdr mutex */
614 l2arc_dev_t
*b_dev
; /* L2ARC device */
615 uint64_t b_daddr
; /* disk address, offset byte */
618 typedef struct l2arc_data_free
{
619 /* protected by l2arc_free_on_write_mtx */
622 void (*l2df_func
)(void *, size_t);
623 list_node_t l2df_list_node
;
626 static kmutex_t l2arc_feed_thr_lock
;
627 static kcondvar_t l2arc_feed_thr_cv
;
628 static uint8_t l2arc_thread_exit
;
630 static void l2arc_read_done(zio_t
*zio
);
631 static void l2arc_hdr_stat_add(void);
632 static void l2arc_hdr_stat_remove(void);
635 buf_hash(uint64_t spa
, const dva_t
*dva
, uint64_t birth
)
637 uint8_t *vdva
= (uint8_t *)dva
;
638 uint64_t crc
= -1ULL;
641 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
643 for (i
= 0; i
< sizeof (dva_t
); i
++)
644 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ vdva
[i
]) & 0xFF];
646 crc
^= (spa
>>8) ^ birth
;
651 #define BUF_EMPTY(buf) \
652 ((buf)->b_dva.dva_word[0] == 0 && \
653 (buf)->b_dva.dva_word[1] == 0 && \
656 #define BUF_EQUAL(spa, dva, birth, buf) \
657 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
658 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
659 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
661 static arc_buf_hdr_t
*
662 buf_hash_find(uint64_t spa
, const dva_t
*dva
, uint64_t birth
, kmutex_t
**lockp
)
664 uint64_t idx
= BUF_HASH_INDEX(spa
, dva
, birth
);
665 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
668 mutex_enter(hash_lock
);
669 for (buf
= buf_hash_table
.ht_table
[idx
]; buf
!= NULL
;
670 buf
= buf
->b_hash_next
) {
671 if (BUF_EQUAL(spa
, dva
, birth
, buf
)) {
676 mutex_exit(hash_lock
);
682 * Insert an entry into the hash table. If there is already an element
683 * equal to elem in the hash table, then the already existing element
684 * will be returned and the new element will not be inserted.
685 * Otherwise returns NULL.
687 static arc_buf_hdr_t
*
688 buf_hash_insert(arc_buf_hdr_t
*buf
, kmutex_t
**lockp
)
690 uint64_t idx
= BUF_HASH_INDEX(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
);
691 kmutex_t
*hash_lock
= BUF_HASH_LOCK(idx
);
695 ASSERT(!HDR_IN_HASH_TABLE(buf
));
697 mutex_enter(hash_lock
);
698 for (fbuf
= buf_hash_table
.ht_table
[idx
], i
= 0; fbuf
!= NULL
;
699 fbuf
= fbuf
->b_hash_next
, i
++) {
700 if (BUF_EQUAL(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
, fbuf
))
704 buf
->b_hash_next
= buf_hash_table
.ht_table
[idx
];
705 buf_hash_table
.ht_table
[idx
] = buf
;
706 buf
->b_flags
|= ARC_IN_HASH_TABLE
;
708 /* collect some hash table performance data */
710 ARCSTAT_BUMP(arcstat_hash_collisions
);
712 ARCSTAT_BUMP(arcstat_hash_chains
);
714 ARCSTAT_MAX(arcstat_hash_chain_max
, i
);
717 ARCSTAT_BUMP(arcstat_hash_elements
);
718 ARCSTAT_MAXSTAT(arcstat_hash_elements
);
724 buf_hash_remove(arc_buf_hdr_t
*buf
)
726 arc_buf_hdr_t
*fbuf
, **bufp
;
727 uint64_t idx
= BUF_HASH_INDEX(buf
->b_spa
, &buf
->b_dva
, buf
->b_birth
);
729 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx
)));
730 ASSERT(HDR_IN_HASH_TABLE(buf
));
732 bufp
= &buf_hash_table
.ht_table
[idx
];
733 while ((fbuf
= *bufp
) != buf
) {
734 ASSERT(fbuf
!= NULL
);
735 bufp
= &fbuf
->b_hash_next
;
737 *bufp
= buf
->b_hash_next
;
738 buf
->b_hash_next
= NULL
;
739 buf
->b_flags
&= ~ARC_IN_HASH_TABLE
;
741 /* collect some hash table performance data */
742 ARCSTAT_BUMPDOWN(arcstat_hash_elements
);
744 if (buf_hash_table
.ht_table
[idx
] &&
745 buf_hash_table
.ht_table
[idx
]->b_hash_next
== NULL
)
746 ARCSTAT_BUMPDOWN(arcstat_hash_chains
);
750 * Global data structures and functions for the buf kmem cache.
752 static kmem_cache_t
*hdr_cache
;
753 static kmem_cache_t
*buf_cache
;
760 kmem_free(buf_hash_table
.ht_table
,
761 (buf_hash_table
.ht_mask
+ 1) * sizeof (void *));
762 for (i
= 0; i
< BUF_LOCKS
; i
++)
763 mutex_destroy(&buf_hash_table
.ht_locks
[i
].ht_lock
);
764 kmem_cache_destroy(hdr_cache
);
765 kmem_cache_destroy(buf_cache
);
769 * Constructor callback - called when the cache is empty
770 * and a new buf is requested.
774 hdr_cons(void *vbuf
, void *unused
, int kmflag
)
776 arc_buf_hdr_t
*buf
= vbuf
;
778 bzero(buf
, sizeof (arc_buf_hdr_t
));
779 refcount_create(&buf
->b_refcnt
);
780 cv_init(&buf
->b_cv
, NULL
, CV_DEFAULT
, NULL
);
781 mutex_init(&buf
->b_freeze_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
782 arc_space_consume(sizeof (arc_buf_hdr_t
), ARC_SPACE_HDRS
);
789 buf_cons(void *vbuf
, void *unused
, int kmflag
)
791 arc_buf_t
*buf
= vbuf
;
793 bzero(buf
, sizeof (arc_buf_t
));
794 rw_init(&buf
->b_lock
, NULL
, RW_DEFAULT
, NULL
);
795 arc_space_consume(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
801 * Destructor callback - called when a cached buf is
802 * no longer required.
806 hdr_dest(void *vbuf
, void *unused
)
808 arc_buf_hdr_t
*buf
= vbuf
;
810 refcount_destroy(&buf
->b_refcnt
);
811 cv_destroy(&buf
->b_cv
);
812 mutex_destroy(&buf
->b_freeze_lock
);
813 arc_space_return(sizeof (arc_buf_hdr_t
), ARC_SPACE_HDRS
);
818 buf_dest(void *vbuf
, void *unused
)
820 arc_buf_t
*buf
= vbuf
;
822 rw_destroy(&buf
->b_lock
);
823 arc_space_return(sizeof (arc_buf_t
), ARC_SPACE_HDRS
);
827 * Reclaim callback -- invoked when memory is low.
831 hdr_recl(void *unused
)
833 dprintf("hdr_recl called\n");
835 * umem calls the reclaim func when we destroy the buf cache,
836 * which is after we do arc_fini().
839 cv_signal(&arc_reclaim_thr_cv
);
846 uint64_t hsize
= 1ULL << 12;
850 * The hash table is big enough to fill all of physical memory
851 * with an average 64K block size. The table will take up
852 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
854 while (hsize
* 65536 < physmem
* PAGESIZE
)
857 buf_hash_table
.ht_mask
= hsize
- 1;
858 buf_hash_table
.ht_table
=
859 kmem_zalloc(hsize
* sizeof (void*), KM_NOSLEEP
);
860 if (buf_hash_table
.ht_table
== NULL
) {
861 ASSERT(hsize
> (1ULL << 8));
866 hdr_cache
= kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t
),
867 0, hdr_cons
, hdr_dest
, hdr_recl
, NULL
, NULL
, 0);
868 buf_cache
= kmem_cache_create("arc_buf_t", sizeof (arc_buf_t
),
869 0, buf_cons
, buf_dest
, NULL
, NULL
, NULL
, 0);
871 for (i
= 0; i
< 256; i
++)
872 for (ct
= zfs_crc64_table
+ i
, *ct
= i
, j
= 8; j
> 0; j
--)
873 *ct
= (*ct
>> 1) ^ (-(*ct
& 1) & ZFS_CRC64_POLY
);
875 for (i
= 0; i
< BUF_LOCKS
; i
++) {
876 mutex_init(&buf_hash_table
.ht_locks
[i
].ht_lock
,
877 NULL
, MUTEX_DEFAULT
, NULL
);
881 #define ARC_MINTIME (hz>>4) /* 62 ms */
884 arc_cksum_verify(arc_buf_t
*buf
)
888 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
891 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
892 if (buf
->b_hdr
->b_freeze_cksum
== NULL
||
893 (buf
->b_hdr
->b_flags
& ARC_IO_ERROR
)) {
894 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
897 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
898 if (!ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
))
899 panic("buffer modified while frozen!");
900 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
904 arc_cksum_equal(arc_buf_t
*buf
)
909 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
910 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
, &zc
);
911 equal
= ZIO_CHECKSUM_EQUAL(*buf
->b_hdr
->b_freeze_cksum
, zc
);
912 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
918 arc_cksum_compute(arc_buf_t
*buf
, boolean_t force
)
920 if (!force
&& !(zfs_flags
& ZFS_DEBUG_MODIFY
))
923 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
924 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
925 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
928 buf
->b_hdr
->b_freeze_cksum
= kmem_alloc(sizeof (zio_cksum_t
), KM_SLEEP
);
929 fletcher_2_native(buf
->b_data
, buf
->b_hdr
->b_size
,
930 buf
->b_hdr
->b_freeze_cksum
);
931 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
935 arc_buf_thaw(arc_buf_t
*buf
)
937 if (zfs_flags
& ZFS_DEBUG_MODIFY
) {
938 if (buf
->b_hdr
->b_state
!= arc_anon
)
939 panic("modifying non-anon buffer!");
940 if (buf
->b_hdr
->b_flags
& ARC_IO_IN_PROGRESS
)
941 panic("modifying buffer while i/o in progress!");
942 arc_cksum_verify(buf
);
945 mutex_enter(&buf
->b_hdr
->b_freeze_lock
);
946 if (buf
->b_hdr
->b_freeze_cksum
!= NULL
) {
947 kmem_free(buf
->b_hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
948 buf
->b_hdr
->b_freeze_cksum
= NULL
;
950 mutex_exit(&buf
->b_hdr
->b_freeze_lock
);
954 arc_buf_freeze(arc_buf_t
*buf
)
956 if (!(zfs_flags
& ZFS_DEBUG_MODIFY
))
959 ASSERT(buf
->b_hdr
->b_freeze_cksum
!= NULL
||
960 buf
->b_hdr
->b_state
== arc_anon
);
961 arc_cksum_compute(buf
, B_FALSE
);
965 add_reference(arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
, void *tag
)
967 ASSERT(MUTEX_HELD(hash_lock
));
969 if ((refcount_add(&ab
->b_refcnt
, tag
) == 1) &&
970 (ab
->b_state
!= arc_anon
)) {
971 uint64_t delta
= ab
->b_size
* ab
->b_datacnt
;
972 list_t
*list
= &ab
->b_state
->arcs_list
[ab
->b_type
];
973 uint64_t *size
= &ab
->b_state
->arcs_lsize
[ab
->b_type
];
975 ASSERT(!MUTEX_HELD(&ab
->b_state
->arcs_mtx
));
976 mutex_enter(&ab
->b_state
->arcs_mtx
);
977 ASSERT(list_link_active(&ab
->b_arc_node
));
978 list_remove(list
, ab
);
979 if (GHOST_STATE(ab
->b_state
)) {
980 ASSERT3U(ab
->b_datacnt
, ==, 0);
981 ASSERT3P(ab
->b_buf
, ==, NULL
);
985 ASSERT3U(*size
, >=, delta
);
986 atomic_add_64(size
, -delta
);
987 mutex_exit(&ab
->b_state
->arcs_mtx
);
988 /* remove the prefetch flag if we get a reference */
989 if (ab
->b_flags
& ARC_PREFETCH
)
990 ab
->b_flags
&= ~ARC_PREFETCH
;
995 remove_reference(arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
, void *tag
)
998 arc_state_t
*state
= ab
->b_state
;
1000 ASSERT(state
== arc_anon
|| MUTEX_HELD(hash_lock
));
1001 ASSERT(!GHOST_STATE(state
));
1003 if (((cnt
= refcount_remove(&ab
->b_refcnt
, tag
)) == 0) &&
1004 (state
!= arc_anon
)) {
1005 uint64_t *size
= &state
->arcs_lsize
[ab
->b_type
];
1007 ASSERT(!MUTEX_HELD(&state
->arcs_mtx
));
1008 mutex_enter(&state
->arcs_mtx
);
1009 ASSERT(!list_link_active(&ab
->b_arc_node
));
1010 list_insert_head(&state
->arcs_list
[ab
->b_type
], ab
);
1011 ASSERT(ab
->b_datacnt
> 0);
1012 atomic_add_64(size
, ab
->b_size
* ab
->b_datacnt
);
1013 mutex_exit(&state
->arcs_mtx
);
1019 * Move the supplied buffer to the indicated state. The mutex
1020 * for the buffer must be held by the caller.
1023 arc_change_state(arc_state_t
*new_state
, arc_buf_hdr_t
*ab
, kmutex_t
*hash_lock
)
1025 arc_state_t
*old_state
= ab
->b_state
;
1026 int64_t refcnt
= refcount_count(&ab
->b_refcnt
);
1027 uint64_t from_delta
, to_delta
;
1029 ASSERT(MUTEX_HELD(hash_lock
));
1030 ASSERT(new_state
!= old_state
);
1031 ASSERT(refcnt
== 0 || ab
->b_datacnt
> 0);
1032 ASSERT(ab
->b_datacnt
== 0 || !GHOST_STATE(new_state
));
1034 from_delta
= to_delta
= ab
->b_datacnt
* ab
->b_size
;
1037 * If this buffer is evictable, transfer it from the
1038 * old state list to the new state list.
1041 if (old_state
!= arc_anon
) {
1042 int use_mutex
= !MUTEX_HELD(&old_state
->arcs_mtx
);
1043 uint64_t *size
= &old_state
->arcs_lsize
[ab
->b_type
];
1046 mutex_enter(&old_state
->arcs_mtx
);
1048 ASSERT(list_link_active(&ab
->b_arc_node
));
1049 list_remove(&old_state
->arcs_list
[ab
->b_type
], ab
);
1052 * If prefetching out of the ghost cache,
1053 * we will have a non-null datacnt.
1055 if (GHOST_STATE(old_state
) && ab
->b_datacnt
== 0) {
1056 /* ghost elements have a ghost size */
1057 ASSERT(ab
->b_buf
== NULL
);
1058 from_delta
= ab
->b_size
;
1060 ASSERT3U(*size
, >=, from_delta
);
1061 atomic_add_64(size
, -from_delta
);
1064 mutex_exit(&old_state
->arcs_mtx
);
1066 if (new_state
!= arc_anon
) {
1067 int use_mutex
= !MUTEX_HELD(&new_state
->arcs_mtx
);
1068 uint64_t *size
= &new_state
->arcs_lsize
[ab
->b_type
];
1071 mutex_enter(&new_state
->arcs_mtx
);
1073 list_insert_head(&new_state
->arcs_list
[ab
->b_type
], ab
);
1075 /* ghost elements have a ghost size */
1076 if (GHOST_STATE(new_state
)) {
1077 ASSERT(ab
->b_datacnt
== 0);
1078 ASSERT(ab
->b_buf
== NULL
);
1079 to_delta
= ab
->b_size
;
1081 atomic_add_64(size
, to_delta
);
1084 mutex_exit(&new_state
->arcs_mtx
);
1088 ASSERT(!BUF_EMPTY(ab
));
1089 if (new_state
== arc_anon
) {
1090 buf_hash_remove(ab
);
1093 /* adjust state sizes */
1095 atomic_add_64(&new_state
->arcs_size
, to_delta
);
1097 ASSERT3U(old_state
->arcs_size
, >=, from_delta
);
1098 atomic_add_64(&old_state
->arcs_size
, -from_delta
);
1100 ab
->b_state
= new_state
;
1102 /* adjust l2arc hdr stats */
1103 if (new_state
== arc_l2c_only
)
1104 l2arc_hdr_stat_add();
1105 else if (old_state
== arc_l2c_only
)
1106 l2arc_hdr_stat_remove();
1110 arc_space_consume(uint64_t space
, arc_space_type_t type
)
1112 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1115 case ARC_SPACE_DATA
:
1116 ARCSTAT_INCR(arcstat_data_size
, space
);
1118 case ARC_SPACE_OTHER
:
1119 ARCSTAT_INCR(arcstat_other_size
, space
);
1121 case ARC_SPACE_HDRS
:
1122 ARCSTAT_INCR(arcstat_hdr_size
, space
);
1124 case ARC_SPACE_L2HDRS
:
1125 ARCSTAT_INCR(arcstat_l2_hdr_size
, space
);
1129 atomic_add_64(&arc_meta_used
, space
);
1130 atomic_add_64(&arc_size
, space
);
1134 arc_space_return(uint64_t space
, arc_space_type_t type
)
1136 ASSERT(type
>= 0 && type
< ARC_SPACE_NUMTYPES
);
1139 case ARC_SPACE_DATA
:
1140 ARCSTAT_INCR(arcstat_data_size
, -space
);
1142 case ARC_SPACE_OTHER
:
1143 ARCSTAT_INCR(arcstat_other_size
, -space
);
1145 case ARC_SPACE_HDRS
:
1146 ARCSTAT_INCR(arcstat_hdr_size
, -space
);
1148 case ARC_SPACE_L2HDRS
:
1149 ARCSTAT_INCR(arcstat_l2_hdr_size
, -space
);
1153 ASSERT(arc_meta_used
>= space
);
1154 if (arc_meta_max
< arc_meta_used
)
1155 arc_meta_max
= arc_meta_used
;
1156 atomic_add_64(&arc_meta_used
, -space
);
1157 ASSERT(arc_size
>= space
);
1158 atomic_add_64(&arc_size
, -space
);
1162 arc_data_buf_alloc(uint64_t size
)
1164 if (arc_evict_needed(ARC_BUFC_DATA
))
1165 cv_signal(&arc_reclaim_thr_cv
);
1166 atomic_add_64(&arc_size
, size
);
1167 return (zio_data_buf_alloc(size
));
1171 arc_data_buf_free(void *buf
, uint64_t size
)
1173 zio_data_buf_free(buf
, size
);
1174 ASSERT(arc_size
>= size
);
1175 atomic_add_64(&arc_size
, -size
);
1179 arc_buf_alloc(spa_t
*spa
, int size
, void *tag
, arc_buf_contents_t type
)
1184 ASSERT3U(size
, >, 0);
1185 hdr
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
1186 ASSERT(BUF_EMPTY(hdr
));
1189 hdr
->b_spa
= spa_guid(spa
);
1190 hdr
->b_state
= arc_anon
;
1191 hdr
->b_arc_access
= 0;
1192 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1195 buf
->b_efunc
= NULL
;
1196 buf
->b_private
= NULL
;
1199 arc_get_data_buf(buf
);
1202 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1203 (void) refcount_add(&hdr
->b_refcnt
, tag
);
1208 static char *arc_onloan_tag
= "onloan";
1211 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1212 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1213 * buffers must be returned to the arc before they can be used by the DMU or
1217 arc_loan_buf(spa_t
*spa
, int size
)
1221 buf
= arc_buf_alloc(spa
, size
, arc_onloan_tag
, ARC_BUFC_DATA
);
1223 atomic_add_64(&arc_loaned_bytes
, size
);
1228 * Return a loaned arc buffer to the arc.
1231 arc_return_buf(arc_buf_t
*buf
, void *tag
)
1233 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1235 ASSERT(hdr
->b_state
== arc_anon
);
1236 ASSERT(buf
->b_data
!= NULL
);
1237 VERIFY(refcount_remove(&hdr
->b_refcnt
, arc_onloan_tag
) == 0);
1238 VERIFY(refcount_add(&hdr
->b_refcnt
, tag
) == 1);
1240 atomic_add_64(&arc_loaned_bytes
, -hdr
->b_size
);
1244 arc_buf_clone(arc_buf_t
*from
)
1247 arc_buf_hdr_t
*hdr
= from
->b_hdr
;
1248 uint64_t size
= hdr
->b_size
;
1250 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
1253 buf
->b_efunc
= NULL
;
1254 buf
->b_private
= NULL
;
1255 buf
->b_next
= hdr
->b_buf
;
1257 arc_get_data_buf(buf
);
1258 bcopy(from
->b_data
, buf
->b_data
, size
);
1259 hdr
->b_datacnt
+= 1;
1264 arc_buf_add_ref(arc_buf_t
*buf
, void* tag
)
1267 kmutex_t
*hash_lock
;
1270 * Check to see if this buffer is evicted. Callers
1271 * must verify b_data != NULL to know if the add_ref
1274 rw_enter(&buf
->b_lock
, RW_READER
);
1275 if (buf
->b_data
== NULL
) {
1276 rw_exit(&buf
->b_lock
);
1280 ASSERT(hdr
!= NULL
);
1281 hash_lock
= HDR_LOCK(hdr
);
1282 mutex_enter(hash_lock
);
1283 rw_exit(&buf
->b_lock
);
1285 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
1286 add_reference(hdr
, hash_lock
, tag
);
1287 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
1288 arc_access(hdr
, hash_lock
);
1289 mutex_exit(hash_lock
);
1290 ARCSTAT_BUMP(arcstat_hits
);
1291 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
1292 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
1293 data
, metadata
, hits
);
1297 * Free the arc data buffer. If it is an l2arc write in progress,
1298 * the buffer is placed on l2arc_free_on_write to be freed later.
1301 arc_buf_data_free(arc_buf_hdr_t
*hdr
, void (*free_func
)(void *, size_t),
1302 void *data
, size_t size
)
1304 if (HDR_L2_WRITING(hdr
)) {
1305 l2arc_data_free_t
*df
;
1306 df
= kmem_alloc(sizeof (l2arc_data_free_t
), KM_SLEEP
);
1307 df
->l2df_data
= data
;
1308 df
->l2df_size
= size
;
1309 df
->l2df_func
= free_func
;
1310 mutex_enter(&l2arc_free_on_write_mtx
);
1311 list_insert_head(l2arc_free_on_write
, df
);
1312 mutex_exit(&l2arc_free_on_write_mtx
);
1313 ARCSTAT_BUMP(arcstat_l2_free_on_write
);
1315 free_func(data
, size
);
1320 arc_buf_destroy(arc_buf_t
*buf
, boolean_t recycle
, boolean_t all
)
1324 /* free up data associated with the buf */
1326 arc_state_t
*state
= buf
->b_hdr
->b_state
;
1327 uint64_t size
= buf
->b_hdr
->b_size
;
1328 arc_buf_contents_t type
= buf
->b_hdr
->b_type
;
1330 arc_cksum_verify(buf
);
1332 if (type
== ARC_BUFC_METADATA
) {
1333 arc_buf_data_free(buf
->b_hdr
, zio_buf_free
,
1335 arc_space_return(size
, ARC_SPACE_DATA
);
1337 ASSERT(type
== ARC_BUFC_DATA
);
1338 arc_buf_data_free(buf
->b_hdr
,
1339 zio_data_buf_free
, buf
->b_data
, size
);
1340 ARCSTAT_INCR(arcstat_data_size
, -size
);
1341 atomic_add_64(&arc_size
, -size
);
1344 if (list_link_active(&buf
->b_hdr
->b_arc_node
)) {
1345 uint64_t *cnt
= &state
->arcs_lsize
[type
];
1347 ASSERT(refcount_is_zero(&buf
->b_hdr
->b_refcnt
));
1348 ASSERT(state
!= arc_anon
);
1350 ASSERT3U(*cnt
, >=, size
);
1351 atomic_add_64(cnt
, -size
);
1353 ASSERT3U(state
->arcs_size
, >=, size
);
1354 atomic_add_64(&state
->arcs_size
, -size
);
1356 ASSERT(buf
->b_hdr
->b_datacnt
> 0);
1357 buf
->b_hdr
->b_datacnt
-= 1;
1360 /* only remove the buf if requested */
1364 /* remove the buf from the hdr list */
1365 for (bufp
= &buf
->b_hdr
->b_buf
; *bufp
!= buf
; bufp
= &(*bufp
)->b_next
)
1367 *bufp
= buf
->b_next
;
1369 ASSERT(buf
->b_efunc
== NULL
);
1371 /* clean up the buf */
1373 kmem_cache_free(buf_cache
, buf
);
1377 arc_hdr_destroy(arc_buf_hdr_t
*hdr
)
1379 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1380 ASSERT3P(hdr
->b_state
, ==, arc_anon
);
1381 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
1382 ASSERT(!(hdr
->b_flags
& ARC_STORED
));
1384 if (hdr
->b_l2hdr
!= NULL
) {
1385 if (!MUTEX_HELD(&l2arc_buflist_mtx
)) {
1387 * To prevent arc_free() and l2arc_evict() from
1388 * attempting to free the same buffer at the same time,
1389 * a FREE_IN_PROGRESS flag is given to arc_free() to
1390 * give it priority. l2arc_evict() can't destroy this
1391 * header while we are waiting on l2arc_buflist_mtx.
1393 * The hdr may be removed from l2ad_buflist before we
1394 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1396 mutex_enter(&l2arc_buflist_mtx
);
1397 if (hdr
->b_l2hdr
!= NULL
) {
1398 list_remove(hdr
->b_l2hdr
->b_dev
->l2ad_buflist
,
1401 mutex_exit(&l2arc_buflist_mtx
);
1403 list_remove(hdr
->b_l2hdr
->b_dev
->l2ad_buflist
, hdr
);
1405 ARCSTAT_INCR(arcstat_l2_size
, -hdr
->b_size
);
1406 kmem_free(hdr
->b_l2hdr
, sizeof (l2arc_buf_hdr_t
));
1407 if (hdr
->b_state
== arc_l2c_only
)
1408 l2arc_hdr_stat_remove();
1409 hdr
->b_l2hdr
= NULL
;
1412 if (!BUF_EMPTY(hdr
)) {
1413 ASSERT(!HDR_IN_HASH_TABLE(hdr
));
1414 bzero(&hdr
->b_dva
, sizeof (dva_t
));
1418 while (hdr
->b_buf
) {
1419 arc_buf_t
*buf
= hdr
->b_buf
;
1422 mutex_enter(&arc_eviction_mtx
);
1423 rw_enter(&buf
->b_lock
, RW_WRITER
);
1424 ASSERT(buf
->b_hdr
!= NULL
);
1425 arc_buf_destroy(hdr
->b_buf
, FALSE
, FALSE
);
1426 hdr
->b_buf
= buf
->b_next
;
1427 buf
->b_hdr
= &arc_eviction_hdr
;
1428 buf
->b_next
= arc_eviction_list
;
1429 arc_eviction_list
= buf
;
1430 rw_exit(&buf
->b_lock
);
1431 mutex_exit(&arc_eviction_mtx
);
1433 arc_buf_destroy(hdr
->b_buf
, FALSE
, TRUE
);
1436 if (hdr
->b_freeze_cksum
!= NULL
) {
1437 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
1438 hdr
->b_freeze_cksum
= NULL
;
1441 ASSERT(!list_link_active(&hdr
->b_arc_node
));
1442 ASSERT3P(hdr
->b_hash_next
, ==, NULL
);
1443 ASSERT3P(hdr
->b_acb
, ==, NULL
);
1444 kmem_cache_free(hdr_cache
, hdr
);
1448 arc_buf_free(arc_buf_t
*buf
, void *tag
)
1450 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1451 int hashed
= hdr
->b_state
!= arc_anon
;
1453 ASSERT(buf
->b_efunc
== NULL
);
1454 ASSERT(buf
->b_data
!= NULL
);
1457 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
1459 mutex_enter(hash_lock
);
1460 (void) remove_reference(hdr
, hash_lock
, tag
);
1461 if (hdr
->b_datacnt
> 1)
1462 arc_buf_destroy(buf
, FALSE
, TRUE
);
1464 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
1465 mutex_exit(hash_lock
);
1466 } else if (HDR_IO_IN_PROGRESS(hdr
)) {
1469 * We are in the middle of an async write. Don't destroy
1470 * this buffer unless the write completes before we finish
1471 * decrementing the reference count.
1473 mutex_enter(&arc_eviction_mtx
);
1474 (void) remove_reference(hdr
, NULL
, tag
);
1475 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
1476 destroy_hdr
= !HDR_IO_IN_PROGRESS(hdr
);
1477 mutex_exit(&arc_eviction_mtx
);
1479 arc_hdr_destroy(hdr
);
1481 if (remove_reference(hdr
, NULL
, tag
) > 0) {
1482 ASSERT(HDR_IO_ERROR(hdr
));
1483 arc_buf_destroy(buf
, FALSE
, TRUE
);
1485 arc_hdr_destroy(hdr
);
1491 arc_buf_remove_ref(arc_buf_t
*buf
, void* tag
)
1493 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
1494 kmutex_t
*hash_lock
= HDR_LOCK(hdr
);
1495 int no_callback
= (buf
->b_efunc
== NULL
);
1497 if (hdr
->b_state
== arc_anon
) {
1498 arc_buf_free(buf
, tag
);
1499 return (no_callback
);
1502 mutex_enter(hash_lock
);
1503 ASSERT(hdr
->b_state
!= arc_anon
);
1504 ASSERT(buf
->b_data
!= NULL
);
1506 (void) remove_reference(hdr
, hash_lock
, tag
);
1507 if (hdr
->b_datacnt
> 1) {
1509 arc_buf_destroy(buf
, FALSE
, TRUE
);
1510 } else if (no_callback
) {
1511 ASSERT(hdr
->b_buf
== buf
&& buf
->b_next
== NULL
);
1512 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
1514 ASSERT(no_callback
|| hdr
->b_datacnt
> 1 ||
1515 refcount_is_zero(&hdr
->b_refcnt
));
1516 mutex_exit(hash_lock
);
1517 return (no_callback
);
1521 arc_buf_size(arc_buf_t
*buf
)
1523 return (buf
->b_hdr
->b_size
);
1527 * Evict buffers from list until we've removed the specified number of
1528 * bytes. Move the removed buffers to the appropriate evict state.
1529 * If the recycle flag is set, then attempt to "recycle" a buffer:
1530 * - look for a buffer to evict that is `bytes' long.
1531 * - return the data block from this buffer rather than freeing it.
1532 * This flag is used by callers that are trying to make space for a
1533 * new buffer in a full arc cache.
1535 * This function makes a "best effort". It skips over any buffers
1536 * it can't get a hash_lock on, and so may not catch all candidates.
1537 * It may also return without evicting as much space as requested.
1540 arc_evict(arc_state_t
*state
, uint64_t spa
, int64_t bytes
, boolean_t recycle
,
1541 arc_buf_contents_t type
)
1543 arc_state_t
*evicted_state
;
1544 uint64_t bytes_evicted
= 0, skipped
= 0, missed
= 0;
1545 arc_buf_hdr_t
*ab
, *ab_prev
= NULL
;
1546 list_t
*list
= &state
->arcs_list
[type
];
1547 kmutex_t
*hash_lock
;
1548 boolean_t have_lock
;
1549 void *stolen
= NULL
;
1551 ASSERT(state
== arc_mru
|| state
== arc_mfu
);
1553 evicted_state
= (state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
1555 mutex_enter(&state
->arcs_mtx
);
1556 mutex_enter(&evicted_state
->arcs_mtx
);
1558 for (ab
= list_tail(list
); ab
; ab
= ab_prev
) {
1559 ab_prev
= list_prev(list
, ab
);
1560 /* prefetch buffers have a minimum lifespan */
1561 if (HDR_IO_IN_PROGRESS(ab
) ||
1562 (spa
&& ab
->b_spa
!= spa
) ||
1563 (ab
->b_flags
& (ARC_PREFETCH
|ARC_INDIRECT
) &&
1564 lbolt
- ab
->b_arc_access
< arc_min_prefetch_lifespan
)) {
1568 /* "lookahead" for better eviction candidate */
1569 if (recycle
&& ab
->b_size
!= bytes
&&
1570 ab_prev
&& ab_prev
->b_size
== bytes
)
1572 hash_lock
= HDR_LOCK(ab
);
1573 have_lock
= MUTEX_HELD(hash_lock
);
1574 if (have_lock
|| mutex_tryenter(hash_lock
)) {
1575 ASSERT3U(refcount_count(&ab
->b_refcnt
), ==, 0);
1576 ASSERT(ab
->b_datacnt
> 0);
1578 arc_buf_t
*buf
= ab
->b_buf
;
1579 if (!rw_tryenter(&buf
->b_lock
, RW_WRITER
)) {
1584 bytes_evicted
+= ab
->b_size
;
1585 if (recycle
&& ab
->b_type
== type
&&
1586 ab
->b_size
== bytes
&&
1587 !HDR_L2_WRITING(ab
)) {
1588 stolen
= buf
->b_data
;
1593 mutex_enter(&arc_eviction_mtx
);
1594 arc_buf_destroy(buf
,
1595 buf
->b_data
== stolen
, FALSE
);
1596 ab
->b_buf
= buf
->b_next
;
1597 buf
->b_hdr
= &arc_eviction_hdr
;
1598 buf
->b_next
= arc_eviction_list
;
1599 arc_eviction_list
= buf
;
1600 mutex_exit(&arc_eviction_mtx
);
1601 rw_exit(&buf
->b_lock
);
1603 rw_exit(&buf
->b_lock
);
1604 arc_buf_destroy(buf
,
1605 buf
->b_data
== stolen
, TRUE
);
1608 if (ab
->b_datacnt
== 0) {
1609 arc_change_state(evicted_state
, ab
, hash_lock
);
1610 ASSERT(HDR_IN_HASH_TABLE(ab
));
1611 ab
->b_flags
|= ARC_IN_HASH_TABLE
;
1612 ab
->b_flags
&= ~ARC_BUF_AVAILABLE
;
1613 DTRACE_PROBE1(arc__evict
, arc_buf_hdr_t
*, ab
);
1616 mutex_exit(hash_lock
);
1617 if (bytes
>= 0 && bytes_evicted
>= bytes
)
1624 mutex_exit(&evicted_state
->arcs_mtx
);
1625 mutex_exit(&state
->arcs_mtx
);
1627 if (bytes_evicted
< bytes
)
1628 dprintf("only evicted %lld bytes from %x",
1629 (longlong_t
)bytes_evicted
, state
);
1632 ARCSTAT_INCR(arcstat_evict_skip
, skipped
);
1635 ARCSTAT_INCR(arcstat_mutex_miss
, missed
);
1638 * We have just evicted some date into the ghost state, make
1639 * sure we also adjust the ghost state size if necessary.
1642 arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
> arc_c
) {
1643 int64_t mru_over
= arc_anon
->arcs_size
+ arc_mru
->arcs_size
+
1644 arc_mru_ghost
->arcs_size
- arc_c
;
1646 if (mru_over
> 0 && arc_mru_ghost
->arcs_lsize
[type
] > 0) {
1648 MIN(arc_mru_ghost
->arcs_lsize
[type
], mru_over
);
1649 arc_evict_ghost(arc_mru_ghost
, NULL
, todelete
);
1650 } else if (arc_mfu_ghost
->arcs_lsize
[type
] > 0) {
1651 int64_t todelete
= MIN(arc_mfu_ghost
->arcs_lsize
[type
],
1652 arc_mru_ghost
->arcs_size
+
1653 arc_mfu_ghost
->arcs_size
- arc_c
);
1654 arc_evict_ghost(arc_mfu_ghost
, NULL
, todelete
);
1662 * Remove buffers from list until we've removed the specified number of
1663 * bytes. Destroy the buffers that are removed.
1666 arc_evict_ghost(arc_state_t
*state
, uint64_t spa
, int64_t bytes
)
1668 arc_buf_hdr_t
*ab
, *ab_prev
;
1669 list_t
*list
= &state
->arcs_list
[ARC_BUFC_DATA
];
1670 kmutex_t
*hash_lock
;
1671 uint64_t bytes_deleted
= 0;
1672 uint64_t bufs_skipped
= 0;
1674 ASSERT(GHOST_STATE(state
));
1676 mutex_enter(&state
->arcs_mtx
);
1677 for (ab
= list_tail(list
); ab
; ab
= ab_prev
) {
1678 ab_prev
= list_prev(list
, ab
);
1679 if (spa
&& ab
->b_spa
!= spa
)
1681 hash_lock
= HDR_LOCK(ab
);
1682 if (mutex_tryenter(hash_lock
)) {
1683 ASSERT(!HDR_IO_IN_PROGRESS(ab
));
1684 ASSERT(ab
->b_buf
== NULL
);
1685 ARCSTAT_BUMP(arcstat_deleted
);
1686 bytes_deleted
+= ab
->b_size
;
1688 if (ab
->b_l2hdr
!= NULL
) {
1690 * This buffer is cached on the 2nd Level ARC;
1691 * don't destroy the header.
1693 arc_change_state(arc_l2c_only
, ab
, hash_lock
);
1694 mutex_exit(hash_lock
);
1696 arc_change_state(arc_anon
, ab
, hash_lock
);
1697 mutex_exit(hash_lock
);
1698 arc_hdr_destroy(ab
);
1701 DTRACE_PROBE1(arc__delete
, arc_buf_hdr_t
*, ab
);
1702 if (bytes
>= 0 && bytes_deleted
>= bytes
)
1706 mutex_exit(&state
->arcs_mtx
);
1707 mutex_enter(hash_lock
);
1708 mutex_exit(hash_lock
);
1714 mutex_exit(&state
->arcs_mtx
);
1716 if (list
== &state
->arcs_list
[ARC_BUFC_DATA
] &&
1717 (bytes
< 0 || bytes_deleted
< bytes
)) {
1718 list
= &state
->arcs_list
[ARC_BUFC_METADATA
];
1723 ARCSTAT_INCR(arcstat_mutex_miss
, bufs_skipped
);
1727 if (bytes_deleted
< bytes
)
1728 dprintf("only deleted %lld bytes from %p",
1729 (longlong_t
)bytes_deleted
, state
);
1735 int64_t adjustment
, delta
;
1741 adjustment
= MIN(arc_size
- arc_c
,
1742 arc_anon
->arcs_size
+ arc_mru
->arcs_size
+ arc_meta_used
- arc_p
);
1744 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] > 0) {
1745 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_DATA
], adjustment
);
1746 (void) arc_evict(arc_mru
, NULL
, delta
, FALSE
, ARC_BUFC_DATA
);
1747 adjustment
-= delta
;
1750 if (adjustment
> 0 && arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
1751 delta
= MIN(arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
], adjustment
);
1752 (void) arc_evict(arc_mru
, NULL
, delta
, FALSE
,
1760 adjustment
= arc_size
- arc_c
;
1762 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] > 0) {
1763 delta
= MIN(adjustment
, arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
]);
1764 (void) arc_evict(arc_mfu
, NULL
, delta
, FALSE
, ARC_BUFC_DATA
);
1765 adjustment
-= delta
;
1768 if (adjustment
> 0 && arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
] > 0) {
1769 int64_t delta
= MIN(adjustment
,
1770 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
]);
1771 (void) arc_evict(arc_mfu
, NULL
, delta
, FALSE
,
1776 * Adjust ghost lists
1779 adjustment
= arc_mru
->arcs_size
+ arc_mru_ghost
->arcs_size
- arc_c
;
1781 if (adjustment
> 0 && arc_mru_ghost
->arcs_size
> 0) {
1782 delta
= MIN(arc_mru_ghost
->arcs_size
, adjustment
);
1783 arc_evict_ghost(arc_mru_ghost
, NULL
, delta
);
1787 arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
- arc_c
;
1789 if (adjustment
> 0 && arc_mfu_ghost
->arcs_size
> 0) {
1790 delta
= MIN(arc_mfu_ghost
->arcs_size
, adjustment
);
1791 arc_evict_ghost(arc_mfu_ghost
, NULL
, delta
);
1796 arc_do_user_evicts(void)
1798 mutex_enter(&arc_eviction_mtx
);
1799 while (arc_eviction_list
!= NULL
) {
1800 arc_buf_t
*buf
= arc_eviction_list
;
1801 arc_eviction_list
= buf
->b_next
;
1802 rw_enter(&buf
->b_lock
, RW_WRITER
);
1804 rw_exit(&buf
->b_lock
);
1805 mutex_exit(&arc_eviction_mtx
);
1807 if (buf
->b_efunc
!= NULL
)
1808 VERIFY(buf
->b_efunc(buf
) == 0);
1810 buf
->b_efunc
= NULL
;
1811 buf
->b_private
= NULL
;
1812 kmem_cache_free(buf_cache
, buf
);
1813 mutex_enter(&arc_eviction_mtx
);
1815 mutex_exit(&arc_eviction_mtx
);
1819 * Flush all *evictable* data from the cache for the given spa.
1820 * NOTE: this will not touch "active" (i.e. referenced) data.
1823 arc_flush(spa_t
*spa
)
1828 guid
= spa_guid(spa
);
1830 while (list_head(&arc_mru
->arcs_list
[ARC_BUFC_DATA
])) {
1831 (void) arc_evict(arc_mru
, guid
, -1, FALSE
, ARC_BUFC_DATA
);
1835 while (list_head(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
])) {
1836 (void) arc_evict(arc_mru
, guid
, -1, FALSE
, ARC_BUFC_METADATA
);
1840 while (list_head(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
])) {
1841 (void) arc_evict(arc_mfu
, guid
, -1, FALSE
, ARC_BUFC_DATA
);
1845 while (list_head(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
])) {
1846 (void) arc_evict(arc_mfu
, guid
, -1, FALSE
, ARC_BUFC_METADATA
);
1851 arc_evict_ghost(arc_mru_ghost
, guid
, -1);
1852 arc_evict_ghost(arc_mfu_ghost
, guid
, -1);
1854 mutex_enter(&arc_reclaim_thr_lock
);
1855 arc_do_user_evicts();
1856 mutex_exit(&arc_reclaim_thr_lock
);
1857 ASSERT(spa
|| arc_eviction_list
== NULL
);
1863 if (arc_c
> arc_c_min
) {
1867 to_free
= MAX(arc_c
>> arc_shrink_shift
, ptob(needfree
));
1869 to_free
= arc_c
>> arc_shrink_shift
;
1871 if (arc_c
> arc_c_min
+ to_free
)
1872 atomic_add_64(&arc_c
, -to_free
);
1876 atomic_add_64(&arc_p
, -(arc_p
>> arc_shrink_shift
));
1877 if (arc_c
> arc_size
)
1878 arc_c
= MAX(arc_size
, arc_c_min
);
1880 arc_p
= (arc_c
>> 1);
1881 ASSERT(arc_c
>= arc_c_min
);
1882 ASSERT((int64_t)arc_p
>= 0);
1885 if (arc_size
> arc_c
)
1890 arc_reclaim_needed(void)
1900 * take 'desfree' extra pages, so we reclaim sooner, rather than later
1905 * check that we're out of range of the pageout scanner. It starts to
1906 * schedule paging if freemem is less than lotsfree and needfree.
1907 * lotsfree is the high-water mark for pageout, and needfree is the
1908 * number of needed free pages. We add extra pages here to make sure
1909 * the scanner doesn't start up while we're freeing memory.
1911 if (freemem
< lotsfree
+ needfree
+ extra
)
1915 * check to make sure that swapfs has enough space so that anon
1916 * reservations can still succeed. anon_resvmem() checks that the
1917 * availrmem is greater than swapfs_minfree, and the number of reserved
1918 * swap pages. We also add a bit of extra here just to prevent
1919 * circumstances from getting really dire.
1921 if (availrmem
< swapfs_minfree
+ swapfs_reserve
+ extra
)
1926 * If we're on an i386 platform, it's possible that we'll exhaust the
1927 * kernel heap space before we ever run out of available physical
1928 * memory. Most checks of the size of the heap_area compare against
1929 * tune.t_minarmem, which is the minimum available real memory that we
1930 * can have in the system. However, this is generally fixed at 25 pages
1931 * which is so low that it's useless. In this comparison, we seek to
1932 * calculate the total heap-size, and reclaim if more than 3/4ths of the
1933 * heap is allocated. (Or, in the calculation, if less than 1/4th is
1936 if (btop(vmem_size(heap_arena
, VMEM_FREE
)) <
1937 (btop(vmem_size(heap_arena
, VMEM_FREE
| VMEM_ALLOC
)) >> 2))
1942 if (spa_get_random(100) == 0)
1949 arc_kmem_reap_now(arc_reclaim_strategy_t strat
)
1952 kmem_cache_t
*prev_cache
= NULL
;
1953 kmem_cache_t
*prev_data_cache
= NULL
;
1954 extern kmem_cache_t
*zio_buf_cache
[];
1955 extern kmem_cache_t
*zio_data_buf_cache
[];
1958 if (arc_meta_used
>= arc_meta_limit
) {
1960 * We are exceeding our meta-data cache limit.
1961 * Purge some DNLC entries to release holds on meta-data.
1963 dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent
);
1967 * Reclaim unused memory from all kmem caches.
1974 * An aggressive reclamation will shrink the cache size as well as
1975 * reap free buffers from the arc kmem caches.
1977 if (strat
== ARC_RECLAIM_AGGR
)
1980 for (i
= 0; i
< SPA_MAXBLOCKSIZE
>> SPA_MINBLOCKSHIFT
; i
++) {
1981 if (zio_buf_cache
[i
] != prev_cache
) {
1982 prev_cache
= zio_buf_cache
[i
];
1983 kmem_cache_reap_now(zio_buf_cache
[i
]);
1985 if (zio_data_buf_cache
[i
] != prev_data_cache
) {
1986 prev_data_cache
= zio_data_buf_cache
[i
];
1987 kmem_cache_reap_now(zio_data_buf_cache
[i
]);
1990 kmem_cache_reap_now(buf_cache
);
1991 kmem_cache_reap_now(hdr_cache
);
1995 arc_reclaim_thread(void)
1997 clock_t growtime
= 0;
1998 arc_reclaim_strategy_t last_reclaim
= ARC_RECLAIM_CONS
;
2001 CALLB_CPR_INIT(&cpr
, &arc_reclaim_thr_lock
, callb_generic_cpr
, FTAG
);
2003 mutex_enter(&arc_reclaim_thr_lock
);
2004 while (arc_thread_exit
== 0) {
2005 if (arc_reclaim_needed()) {
2008 if (last_reclaim
== ARC_RECLAIM_CONS
) {
2009 last_reclaim
= ARC_RECLAIM_AGGR
;
2011 last_reclaim
= ARC_RECLAIM_CONS
;
2015 last_reclaim
= ARC_RECLAIM_AGGR
;
2019 /* reset the growth delay for every reclaim */
2020 growtime
= lbolt
+ (arc_grow_retry
* hz
);
2022 arc_kmem_reap_now(last_reclaim
);
2025 } else if (arc_no_grow
&& lbolt
>= growtime
) {
2026 arc_no_grow
= FALSE
;
2029 if (2 * arc_c
< arc_size
+
2030 arc_mru_ghost
->arcs_size
+ arc_mfu_ghost
->arcs_size
)
2033 if (arc_eviction_list
!= NULL
)
2034 arc_do_user_evicts();
2036 /* block until needed, or one second, whichever is shorter */
2037 CALLB_CPR_SAFE_BEGIN(&cpr
);
2038 (void) cv_timedwait(&arc_reclaim_thr_cv
,
2039 &arc_reclaim_thr_lock
, (lbolt
+ hz
));
2040 CALLB_CPR_SAFE_END(&cpr
, &arc_reclaim_thr_lock
);
2043 arc_thread_exit
= 0;
2044 cv_broadcast(&arc_reclaim_thr_cv
);
2045 CALLB_CPR_EXIT(&cpr
); /* drops arc_reclaim_thr_lock */
2050 * Adapt arc info given the number of bytes we are trying to add and
2051 * the state that we are comming from. This function is only called
2052 * when we are adding new content to the cache.
2055 arc_adapt(int bytes
, arc_state_t
*state
)
2058 uint64_t arc_p_min
= (arc_c
>> arc_p_min_shift
);
2060 if (state
== arc_l2c_only
)
2065 * Adapt the target size of the MRU list:
2066 * - if we just hit in the MRU ghost list, then increase
2067 * the target size of the MRU list.
2068 * - if we just hit in the MFU ghost list, then increase
2069 * the target size of the MFU list by decreasing the
2070 * target size of the MRU list.
2072 if (state
== arc_mru_ghost
) {
2073 mult
= ((arc_mru_ghost
->arcs_size
>= arc_mfu_ghost
->arcs_size
) ?
2074 1 : (arc_mfu_ghost
->arcs_size
/arc_mru_ghost
->arcs_size
));
2076 arc_p
= MIN(arc_c
- arc_p_min
, arc_p
+ bytes
* mult
);
2077 } else if (state
== arc_mfu_ghost
) {
2080 mult
= ((arc_mfu_ghost
->arcs_size
>= arc_mru_ghost
->arcs_size
) ?
2081 1 : (arc_mru_ghost
->arcs_size
/arc_mfu_ghost
->arcs_size
));
2083 delta
= MIN(bytes
* mult
, arc_p
);
2084 arc_p
= MAX(arc_p_min
, arc_p
- delta
);
2086 ASSERT((int64_t)arc_p
>= 0);
2088 if (arc_reclaim_needed()) {
2089 cv_signal(&arc_reclaim_thr_cv
);
2096 if (arc_c
>= arc_c_max
)
2100 * If we're within (2 * maxblocksize) bytes of the target
2101 * cache size, increment the target cache size
2103 if (arc_size
> arc_c
- (2ULL << SPA_MAXBLOCKSHIFT
)) {
2104 atomic_add_64(&arc_c
, (int64_t)bytes
);
2105 if (arc_c
> arc_c_max
)
2107 else if (state
== arc_anon
)
2108 atomic_add_64(&arc_p
, (int64_t)bytes
);
2112 ASSERT((int64_t)arc_p
>= 0);
2116 * Check if the cache has reached its limits and eviction is required
2120 arc_evict_needed(arc_buf_contents_t type
)
2122 if (type
== ARC_BUFC_METADATA
&& arc_meta_used
>= arc_meta_limit
)
2127 * If zio data pages are being allocated out of a separate heap segment,
2128 * then enforce that the size of available vmem for this area remains
2129 * above about 1/32nd free.
2131 if (type
== ARC_BUFC_DATA
&& zio_arena
!= NULL
&&
2132 vmem_size(zio_arena
, VMEM_FREE
) <
2133 (vmem_size(zio_arena
, VMEM_ALLOC
) >> 5))
2137 if (arc_reclaim_needed())
2140 return (arc_size
> arc_c
);
2144 * The buffer, supplied as the first argument, needs a data block.
2145 * So, if we are at cache max, determine which cache should be victimized.
2146 * We have the following cases:
2148 * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2149 * In this situation if we're out of space, but the resident size of the MFU is
2150 * under the limit, victimize the MFU cache to satisfy this insertion request.
2152 * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2153 * Here, we've used up all of the available space for the MRU, so we need to
2154 * evict from our own cache instead. Evict from the set of resident MRU
2157 * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2158 * c minus p represents the MFU space in the cache, since p is the size of the
2159 * cache that is dedicated to the MRU. In this situation there's still space on
2160 * the MFU side, so the MRU side needs to be victimized.
2162 * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2163 * MFU's resident set is consuming more space than it has been allotted. In
2164 * this situation, we must victimize our own cache, the MFU, for this insertion.
2167 arc_get_data_buf(arc_buf_t
*buf
)
2169 arc_state_t
*state
= buf
->b_hdr
->b_state
;
2170 uint64_t size
= buf
->b_hdr
->b_size
;
2171 arc_buf_contents_t type
= buf
->b_hdr
->b_type
;
2173 arc_adapt(size
, state
);
2176 * We have not yet reached cache maximum size,
2177 * just allocate a new buffer.
2179 if (!arc_evict_needed(type
)) {
2180 if (type
== ARC_BUFC_METADATA
) {
2181 buf
->b_data
= zio_buf_alloc(size
);
2182 arc_space_consume(size
, ARC_SPACE_DATA
);
2184 ASSERT(type
== ARC_BUFC_DATA
);
2185 buf
->b_data
= zio_data_buf_alloc(size
);
2186 ARCSTAT_INCR(arcstat_data_size
, size
);
2187 atomic_add_64(&arc_size
, size
);
2193 * If we are prefetching from the mfu ghost list, this buffer
2194 * will end up on the mru list; so steal space from there.
2196 if (state
== arc_mfu_ghost
)
2197 state
= buf
->b_hdr
->b_flags
& ARC_PREFETCH
? arc_mru
: arc_mfu
;
2198 else if (state
== arc_mru_ghost
)
2201 if (state
== arc_mru
|| state
== arc_anon
) {
2202 uint64_t mru_used
= arc_anon
->arcs_size
+ arc_mru
->arcs_size
;
2203 state
= (arc_mfu
->arcs_lsize
[type
] >= size
&&
2204 arc_p
> mru_used
) ? arc_mfu
: arc_mru
;
2207 uint64_t mfu_space
= arc_c
- arc_p
;
2208 state
= (arc_mru
->arcs_lsize
[type
] >= size
&&
2209 mfu_space
> arc_mfu
->arcs_size
) ? arc_mru
: arc_mfu
;
2211 if ((buf
->b_data
= arc_evict(state
, NULL
, size
, TRUE
, type
)) == NULL
) {
2212 if (type
== ARC_BUFC_METADATA
) {
2213 buf
->b_data
= zio_buf_alloc(size
);
2214 arc_space_consume(size
, ARC_SPACE_DATA
);
2216 ASSERT(type
== ARC_BUFC_DATA
);
2217 buf
->b_data
= zio_data_buf_alloc(size
);
2218 ARCSTAT_INCR(arcstat_data_size
, size
);
2219 atomic_add_64(&arc_size
, size
);
2221 ARCSTAT_BUMP(arcstat_recycle_miss
);
2223 ASSERT(buf
->b_data
!= NULL
);
2226 * Update the state size. Note that ghost states have a
2227 * "ghost size" and so don't need to be updated.
2229 if (!GHOST_STATE(buf
->b_hdr
->b_state
)) {
2230 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
2232 atomic_add_64(&hdr
->b_state
->arcs_size
, size
);
2233 if (list_link_active(&hdr
->b_arc_node
)) {
2234 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
2235 atomic_add_64(&hdr
->b_state
->arcs_lsize
[type
], size
);
2238 * If we are growing the cache, and we are adding anonymous
2239 * data, and we have outgrown arc_p, update arc_p
2241 if (arc_size
< arc_c
&& hdr
->b_state
== arc_anon
&&
2242 arc_anon
->arcs_size
+ arc_mru
->arcs_size
> arc_p
)
2243 arc_p
= MIN(arc_c
, arc_p
+ size
);
2248 * This routine is called whenever a buffer is accessed.
2249 * NOTE: the hash lock is dropped in this function.
2252 arc_access(arc_buf_hdr_t
*buf
, kmutex_t
*hash_lock
)
2254 ASSERT(MUTEX_HELD(hash_lock
));
2256 if (buf
->b_state
== arc_anon
) {
2258 * This buffer is not in the cache, and does not
2259 * appear in our "ghost" list. Add the new buffer
2263 ASSERT(buf
->b_arc_access
== 0);
2264 buf
->b_arc_access
= lbolt
;
2265 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, buf
);
2266 arc_change_state(arc_mru
, buf
, hash_lock
);
2268 } else if (buf
->b_state
== arc_mru
) {
2270 * If this buffer is here because of a prefetch, then either:
2271 * - clear the flag if this is a "referencing" read
2272 * (any subsequent access will bump this into the MFU state).
2274 * - move the buffer to the head of the list if this is
2275 * another prefetch (to make it less likely to be evicted).
2277 if ((buf
->b_flags
& ARC_PREFETCH
) != 0) {
2278 if (refcount_count(&buf
->b_refcnt
) == 0) {
2279 ASSERT(list_link_active(&buf
->b_arc_node
));
2281 buf
->b_flags
&= ~ARC_PREFETCH
;
2282 ARCSTAT_BUMP(arcstat_mru_hits
);
2284 buf
->b_arc_access
= lbolt
;
2289 * This buffer has been "accessed" only once so far,
2290 * but it is still in the cache. Move it to the MFU
2293 if (lbolt
> buf
->b_arc_access
+ ARC_MINTIME
) {
2295 * More than 125ms have passed since we
2296 * instantiated this buffer. Move it to the
2297 * most frequently used state.
2299 buf
->b_arc_access
= lbolt
;
2300 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2301 arc_change_state(arc_mfu
, buf
, hash_lock
);
2303 ARCSTAT_BUMP(arcstat_mru_hits
);
2304 } else if (buf
->b_state
== arc_mru_ghost
) {
2305 arc_state_t
*new_state
;
2307 * This buffer has been "accessed" recently, but
2308 * was evicted from the cache. Move it to the
2312 if (buf
->b_flags
& ARC_PREFETCH
) {
2313 new_state
= arc_mru
;
2314 if (refcount_count(&buf
->b_refcnt
) > 0)
2315 buf
->b_flags
&= ~ARC_PREFETCH
;
2316 DTRACE_PROBE1(new_state__mru
, arc_buf_hdr_t
*, buf
);
2318 new_state
= arc_mfu
;
2319 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2322 buf
->b_arc_access
= lbolt
;
2323 arc_change_state(new_state
, buf
, hash_lock
);
2325 ARCSTAT_BUMP(arcstat_mru_ghost_hits
);
2326 } else if (buf
->b_state
== arc_mfu
) {
2328 * This buffer has been accessed more than once and is
2329 * still in the cache. Keep it in the MFU state.
2331 * NOTE: an add_reference() that occurred when we did
2332 * the arc_read() will have kicked this off the list.
2333 * If it was a prefetch, we will explicitly move it to
2334 * the head of the list now.
2336 if ((buf
->b_flags
& ARC_PREFETCH
) != 0) {
2337 ASSERT(refcount_count(&buf
->b_refcnt
) == 0);
2338 ASSERT(list_link_active(&buf
->b_arc_node
));
2340 ARCSTAT_BUMP(arcstat_mfu_hits
);
2341 buf
->b_arc_access
= lbolt
;
2342 } else if (buf
->b_state
== arc_mfu_ghost
) {
2343 arc_state_t
*new_state
= arc_mfu
;
2345 * This buffer has been accessed more than once but has
2346 * been evicted from the cache. Move it back to the
2350 if (buf
->b_flags
& ARC_PREFETCH
) {
2352 * This is a prefetch access...
2353 * move this block back to the MRU state.
2355 ASSERT3U(refcount_count(&buf
->b_refcnt
), ==, 0);
2356 new_state
= arc_mru
;
2359 buf
->b_arc_access
= lbolt
;
2360 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2361 arc_change_state(new_state
, buf
, hash_lock
);
2363 ARCSTAT_BUMP(arcstat_mfu_ghost_hits
);
2364 } else if (buf
->b_state
== arc_l2c_only
) {
2366 * This buffer is on the 2nd Level ARC.
2369 buf
->b_arc_access
= lbolt
;
2370 DTRACE_PROBE1(new_state__mfu
, arc_buf_hdr_t
*, buf
);
2371 arc_change_state(arc_mfu
, buf
, hash_lock
);
2373 ASSERT(!"invalid arc state");
2377 /* a generic arc_done_func_t which you can use */
2380 arc_bcopy_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2382 bcopy(buf
->b_data
, arg
, buf
->b_hdr
->b_size
);
2383 VERIFY(arc_buf_remove_ref(buf
, arg
) == 1);
2386 /* a generic arc_done_func_t */
2388 arc_getbuf_func(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
2390 arc_buf_t
**bufp
= arg
;
2391 if (zio
&& zio
->io_error
) {
2392 VERIFY(arc_buf_remove_ref(buf
, arg
) == 1);
2400 arc_read_done(zio_t
*zio
)
2402 arc_buf_hdr_t
*hdr
, *found
;
2404 arc_buf_t
*abuf
; /* buffer we're assigning to callback */
2405 kmutex_t
*hash_lock
;
2406 arc_callback_t
*callback_list
, *acb
;
2407 int freeable
= FALSE
;
2409 buf
= zio
->io_private
;
2413 * The hdr was inserted into hash-table and removed from lists
2414 * prior to starting I/O. We should find this header, since
2415 * it's in the hash table, and it should be legit since it's
2416 * not possible to evict it during the I/O. The only possible
2417 * reason for it not to be found is if we were freed during the
2420 found
= buf_hash_find(hdr
->b_spa
, &hdr
->b_dva
, hdr
->b_birth
,
2423 ASSERT((found
== NULL
&& HDR_FREED_IN_READ(hdr
) && hash_lock
== NULL
) ||
2424 (found
== hdr
&& DVA_EQUAL(&hdr
->b_dva
, BP_IDENTITY(zio
->io_bp
))) ||
2425 (found
== hdr
&& HDR_L2_READING(hdr
)));
2427 hdr
->b_flags
&= ~ARC_L2_EVICTED
;
2428 if (l2arc_noprefetch
&& (hdr
->b_flags
& ARC_PREFETCH
))
2429 hdr
->b_flags
&= ~ARC_L2CACHE
;
2431 /* byteswap if necessary */
2432 callback_list
= hdr
->b_acb
;
2433 ASSERT(callback_list
!= NULL
);
2434 if (BP_SHOULD_BYTESWAP(zio
->io_bp
)) {
2435 arc_byteswap_func_t
*func
= BP_GET_LEVEL(zio
->io_bp
) > 0 ?
2436 byteswap_uint64_array
:
2437 dmu_ot
[BP_GET_TYPE(zio
->io_bp
)].ot_byteswap
;
2438 func(buf
->b_data
, hdr
->b_size
);
2441 arc_cksum_compute(buf
, B_FALSE
);
2443 /* create copies of the data buffer for the callers */
2445 for (acb
= callback_list
; acb
; acb
= acb
->acb_next
) {
2446 if (acb
->acb_done
) {
2448 abuf
= arc_buf_clone(buf
);
2449 acb
->acb_buf
= abuf
;
2454 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
2455 ASSERT(!HDR_BUF_AVAILABLE(hdr
));
2457 hdr
->b_flags
|= ARC_BUF_AVAILABLE
;
2459 ASSERT(refcount_is_zero(&hdr
->b_refcnt
) || callback_list
!= NULL
);
2461 if (zio
->io_error
!= 0) {
2462 hdr
->b_flags
|= ARC_IO_ERROR
;
2463 if (hdr
->b_state
!= arc_anon
)
2464 arc_change_state(arc_anon
, hdr
, hash_lock
);
2465 if (HDR_IN_HASH_TABLE(hdr
))
2466 buf_hash_remove(hdr
);
2467 freeable
= refcount_is_zero(&hdr
->b_refcnt
);
2471 * Broadcast before we drop the hash_lock to avoid the possibility
2472 * that the hdr (and hence the cv) might be freed before we get to
2473 * the cv_broadcast().
2475 cv_broadcast(&hdr
->b_cv
);
2479 * Only call arc_access on anonymous buffers. This is because
2480 * if we've issued an I/O for an evicted buffer, we've already
2481 * called arc_access (to prevent any simultaneous readers from
2482 * getting confused).
2484 if (zio
->io_error
== 0 && hdr
->b_state
== arc_anon
)
2485 arc_access(hdr
, hash_lock
);
2486 mutex_exit(hash_lock
);
2489 * This block was freed while we waited for the read to
2490 * complete. It has been removed from the hash table and
2491 * moved to the anonymous state (so that it won't show up
2494 ASSERT3P(hdr
->b_state
, ==, arc_anon
);
2495 freeable
= refcount_is_zero(&hdr
->b_refcnt
);
2498 /* execute each callback and free its structure */
2499 while ((acb
= callback_list
) != NULL
) {
2501 acb
->acb_done(zio
, acb
->acb_buf
, acb
->acb_private
);
2503 if (acb
->acb_zio_dummy
!= NULL
) {
2504 acb
->acb_zio_dummy
->io_error
= zio
->io_error
;
2505 zio_nowait(acb
->acb_zio_dummy
);
2508 callback_list
= acb
->acb_next
;
2509 kmem_free(acb
, sizeof (arc_callback_t
));
2513 arc_hdr_destroy(hdr
);
2517 * "Read" the block block at the specified DVA (in bp) via the
2518 * cache. If the block is found in the cache, invoke the provided
2519 * callback immediately and return. Note that the `zio' parameter
2520 * in the callback will be NULL in this case, since no IO was
2521 * required. If the block is not in the cache pass the read request
2522 * on to the spa with a substitute callback function, so that the
2523 * requested block will be added to the cache.
2525 * If a read request arrives for a block that has a read in-progress,
2526 * either wait for the in-progress read to complete (and return the
2527 * results); or, if this is a read with a "done" func, add a record
2528 * to the read to invoke the "done" func when the read completes,
2529 * and return; or just return.
2531 * arc_read_done() will invoke all the requested "done" functions
2532 * for readers of this block.
2534 * Normal callers should use arc_read and pass the arc buffer and offset
2535 * for the bp. But if you know you don't need locking, you can use
2539 arc_read(zio_t
*pio
, spa_t
*spa
, blkptr_t
*bp
, arc_buf_t
*pbuf
,
2540 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
2541 uint32_t *arc_flags
, const zbookmark_t
*zb
)
2545 ASSERT(!refcount_is_zero(&pbuf
->b_hdr
->b_refcnt
));
2546 ASSERT3U((char *)bp
- (char *)pbuf
->b_data
, <, pbuf
->b_hdr
->b_size
);
2547 rw_enter(&pbuf
->b_lock
, RW_READER
);
2549 err
= arc_read_nolock(pio
, spa
, bp
, done
, private, priority
,
2550 zio_flags
, arc_flags
, zb
);
2551 rw_exit(&pbuf
->b_lock
);
2557 arc_read_nolock(zio_t
*pio
, spa_t
*spa
, blkptr_t
*bp
,
2558 arc_done_func_t
*done
, void *private, int priority
, int zio_flags
,
2559 uint32_t *arc_flags
, const zbookmark_t
*zb
)
2563 kmutex_t
*hash_lock
;
2565 uint64_t guid
= spa_guid(spa
);
2568 hdr
= buf_hash_find(guid
, BP_IDENTITY(bp
), bp
->blk_birth
, &hash_lock
);
2569 if (hdr
&& hdr
->b_datacnt
> 0) {
2571 *arc_flags
|= ARC_CACHED
;
2573 if (HDR_IO_IN_PROGRESS(hdr
)) {
2575 if (*arc_flags
& ARC_WAIT
) {
2576 cv_wait(&hdr
->b_cv
, hash_lock
);
2577 mutex_exit(hash_lock
);
2580 ASSERT(*arc_flags
& ARC_NOWAIT
);
2583 arc_callback_t
*acb
= NULL
;
2585 acb
= kmem_zalloc(sizeof (arc_callback_t
),
2587 acb
->acb_done
= done
;
2588 acb
->acb_private
= private;
2590 acb
->acb_zio_dummy
= zio_null(pio
,
2591 spa
, NULL
, NULL
, NULL
, zio_flags
);
2593 ASSERT(acb
->acb_done
!= NULL
);
2594 acb
->acb_next
= hdr
->b_acb
;
2596 add_reference(hdr
, hash_lock
, private);
2597 mutex_exit(hash_lock
);
2600 mutex_exit(hash_lock
);
2604 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
2607 add_reference(hdr
, hash_lock
, private);
2609 * If this block is already in use, create a new
2610 * copy of the data so that we will be guaranteed
2611 * that arc_release() will always succeed.
2615 ASSERT(buf
->b_data
);
2616 if (HDR_BUF_AVAILABLE(hdr
)) {
2617 ASSERT(buf
->b_efunc
== NULL
);
2618 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
2620 buf
= arc_buf_clone(buf
);
2622 } else if (*arc_flags
& ARC_PREFETCH
&&
2623 refcount_count(&hdr
->b_refcnt
) == 0) {
2624 hdr
->b_flags
|= ARC_PREFETCH
;
2626 DTRACE_PROBE1(arc__hit
, arc_buf_hdr_t
*, hdr
);
2627 arc_access(hdr
, hash_lock
);
2628 if (*arc_flags
& ARC_L2CACHE
)
2629 hdr
->b_flags
|= ARC_L2CACHE
;
2630 mutex_exit(hash_lock
);
2631 ARCSTAT_BUMP(arcstat_hits
);
2632 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
2633 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
2634 data
, metadata
, hits
);
2637 done(NULL
, buf
, private);
2639 uint64_t size
= BP_GET_LSIZE(bp
);
2640 arc_callback_t
*acb
;
2643 boolean_t devw
= B_FALSE
;
2646 /* this block is not in the cache */
2647 arc_buf_hdr_t
*exists
;
2648 arc_buf_contents_t type
= BP_GET_BUFC_TYPE(bp
);
2649 buf
= arc_buf_alloc(spa
, size
, private, type
);
2651 hdr
->b_dva
= *BP_IDENTITY(bp
);
2652 hdr
->b_birth
= bp
->blk_birth
;
2653 hdr
->b_cksum0
= bp
->blk_cksum
.zc_word
[0];
2654 exists
= buf_hash_insert(hdr
, &hash_lock
);
2656 /* somebody beat us to the hash insert */
2657 mutex_exit(hash_lock
);
2658 bzero(&hdr
->b_dva
, sizeof (dva_t
));
2661 (void) arc_buf_remove_ref(buf
, private);
2662 goto top
; /* restart the IO request */
2664 /* if this is a prefetch, we don't have a reference */
2665 if (*arc_flags
& ARC_PREFETCH
) {
2666 (void) remove_reference(hdr
, hash_lock
,
2668 hdr
->b_flags
|= ARC_PREFETCH
;
2670 if (*arc_flags
& ARC_L2CACHE
)
2671 hdr
->b_flags
|= ARC_L2CACHE
;
2672 if (BP_GET_LEVEL(bp
) > 0)
2673 hdr
->b_flags
|= ARC_INDIRECT
;
2675 /* this block is in the ghost cache */
2676 ASSERT(GHOST_STATE(hdr
->b_state
));
2677 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
2678 ASSERT3U(refcount_count(&hdr
->b_refcnt
), ==, 0);
2679 ASSERT(hdr
->b_buf
== NULL
);
2681 /* if this is a prefetch, we don't have a reference */
2682 if (*arc_flags
& ARC_PREFETCH
)
2683 hdr
->b_flags
|= ARC_PREFETCH
;
2685 add_reference(hdr
, hash_lock
, private);
2686 if (*arc_flags
& ARC_L2CACHE
)
2687 hdr
->b_flags
|= ARC_L2CACHE
;
2688 buf
= kmem_cache_alloc(buf_cache
, KM_PUSHPAGE
);
2691 buf
->b_efunc
= NULL
;
2692 buf
->b_private
= NULL
;
2695 arc_get_data_buf(buf
);
2696 ASSERT(hdr
->b_datacnt
== 0);
2701 acb
= kmem_zalloc(sizeof (arc_callback_t
), KM_SLEEP
);
2702 acb
->acb_done
= done
;
2703 acb
->acb_private
= private;
2705 ASSERT(hdr
->b_acb
== NULL
);
2707 hdr
->b_flags
|= ARC_IO_IN_PROGRESS
;
2710 * If the buffer has been evicted, migrate it to a present state
2711 * before issuing the I/O. Once we drop the hash-table lock,
2712 * the header will be marked as I/O in progress and have an
2713 * attached buffer. At this point, anybody who finds this
2714 * buffer ought to notice that it's legit but has a pending I/O.
2717 if (GHOST_STATE(hdr
->b_state
))
2718 arc_access(hdr
, hash_lock
);
2720 if (HDR_L2CACHE(hdr
) && hdr
->b_l2hdr
!= NULL
&&
2721 (vd
= hdr
->b_l2hdr
->b_dev
->l2ad_vdev
) != NULL
) {
2722 devw
= hdr
->b_l2hdr
->b_dev
->l2ad_writing
;
2723 addr
= hdr
->b_l2hdr
->b_daddr
;
2725 * Lock out device removal.
2727 if (vdev_is_dead(vd
) ||
2728 !spa_config_tryenter(spa
, SCL_L2ARC
, vd
, RW_READER
))
2732 mutex_exit(hash_lock
);
2734 ASSERT3U(hdr
->b_size
, ==, size
);
2735 DTRACE_PROBE3(arc__miss
, blkptr_t
*, bp
, uint64_t, size
,
2737 ARCSTAT_BUMP(arcstat_misses
);
2738 ARCSTAT_CONDSTAT(!(hdr
->b_flags
& ARC_PREFETCH
),
2739 demand
, prefetch
, hdr
->b_type
!= ARC_BUFC_METADATA
,
2740 data
, metadata
, misses
);
2742 if (vd
!= NULL
&& l2arc_ndev
!= 0 && !(l2arc_norw
&& devw
)) {
2744 * Read from the L2ARC if the following are true:
2745 * 1. The L2ARC vdev was previously cached.
2746 * 2. This buffer still has L2ARC metadata.
2747 * 3. This buffer isn't currently writing to the L2ARC.
2748 * 4. The L2ARC entry wasn't evicted, which may
2749 * also have invalidated the vdev.
2750 * 5. This isn't prefetch and l2arc_noprefetch is set.
2752 if (hdr
->b_l2hdr
!= NULL
&&
2753 !HDR_L2_WRITING(hdr
) && !HDR_L2_EVICTED(hdr
) &&
2754 !(l2arc_noprefetch
&& HDR_PREFETCH(hdr
))) {
2755 l2arc_read_callback_t
*cb
;
2757 DTRACE_PROBE1(l2arc__hit
, arc_buf_hdr_t
*, hdr
);
2758 ARCSTAT_BUMP(arcstat_l2_hits
);
2760 cb
= kmem_zalloc(sizeof (l2arc_read_callback_t
),
2762 cb
->l2rcb_buf
= buf
;
2763 cb
->l2rcb_spa
= spa
;
2766 cb
->l2rcb_flags
= zio_flags
;
2769 * l2arc read. The SCL_L2ARC lock will be
2770 * released by l2arc_read_done().
2772 rzio
= zio_read_phys(pio
, vd
, addr
, size
,
2773 buf
->b_data
, ZIO_CHECKSUM_OFF
,
2774 l2arc_read_done
, cb
, priority
, zio_flags
|
2775 ZIO_FLAG_DONT_CACHE
| ZIO_FLAG_CANFAIL
|
2776 ZIO_FLAG_DONT_PROPAGATE
|
2777 ZIO_FLAG_DONT_RETRY
, B_FALSE
);
2778 DTRACE_PROBE2(l2arc__read
, vdev_t
*, vd
,
2780 ARCSTAT_INCR(arcstat_l2_read_bytes
, size
);
2782 if (*arc_flags
& ARC_NOWAIT
) {
2787 ASSERT(*arc_flags
& ARC_WAIT
);
2788 if (zio_wait(rzio
) == 0)
2791 /* l2arc read error; goto zio_read() */
2793 DTRACE_PROBE1(l2arc__miss
,
2794 arc_buf_hdr_t
*, hdr
);
2795 ARCSTAT_BUMP(arcstat_l2_misses
);
2796 if (HDR_L2_WRITING(hdr
))
2797 ARCSTAT_BUMP(arcstat_l2_rw_clash
);
2798 spa_config_exit(spa
, SCL_L2ARC
, vd
);
2802 spa_config_exit(spa
, SCL_L2ARC
, vd
);
2803 if (l2arc_ndev
!= 0) {
2804 DTRACE_PROBE1(l2arc__miss
,
2805 arc_buf_hdr_t
*, hdr
);
2806 ARCSTAT_BUMP(arcstat_l2_misses
);
2810 rzio
= zio_read(pio
, spa
, bp
, buf
->b_data
, size
,
2811 arc_read_done
, buf
, priority
, zio_flags
, zb
);
2813 if (*arc_flags
& ARC_WAIT
)
2814 return (zio_wait(rzio
));
2816 ASSERT(*arc_flags
& ARC_NOWAIT
);
2823 * arc_read() variant to support pool traversal. If the block is already
2824 * in the ARC, make a copy of it; otherwise, the caller will do the I/O.
2825 * The idea is that we don't want pool traversal filling up memory, but
2826 * if the ARC already has the data anyway, we shouldn't pay for the I/O.
2829 arc_tryread(spa_t
*spa
, blkptr_t
*bp
, void *data
)
2833 uint64_t guid
= spa_guid(spa
);
2836 hdr
= buf_hash_find(guid
, BP_IDENTITY(bp
), bp
->blk_birth
, &hash_mtx
);
2838 if (hdr
&& hdr
->b_datacnt
> 0 && !HDR_IO_IN_PROGRESS(hdr
)) {
2839 arc_buf_t
*buf
= hdr
->b_buf
;
2842 while (buf
->b_data
== NULL
) {
2846 bcopy(buf
->b_data
, data
, hdr
->b_size
);
2852 mutex_exit(hash_mtx
);
2858 arc_set_callback(arc_buf_t
*buf
, arc_evict_func_t
*func
, void *private)
2860 ASSERT(buf
->b_hdr
!= NULL
);
2861 ASSERT(buf
->b_hdr
->b_state
!= arc_anon
);
2862 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_refcnt
) || func
== NULL
);
2863 buf
->b_efunc
= func
;
2864 buf
->b_private
= private;
2868 * This is used by the DMU to let the ARC know that a buffer is
2869 * being evicted, so the ARC should clean up. If this arc buf
2870 * is not yet in the evicted state, it will be put there.
2873 arc_buf_evict(arc_buf_t
*buf
)
2876 kmutex_t
*hash_lock
;
2879 rw_enter(&buf
->b_lock
, RW_WRITER
);
2883 * We are in arc_do_user_evicts().
2885 ASSERT(buf
->b_data
== NULL
);
2886 rw_exit(&buf
->b_lock
);
2888 } else if (buf
->b_data
== NULL
) {
2889 arc_buf_t copy
= *buf
; /* structure assignment */
2891 * We are on the eviction list; process this buffer now
2892 * but let arc_do_user_evicts() do the reaping.
2894 buf
->b_efunc
= NULL
;
2895 rw_exit(&buf
->b_lock
);
2896 VERIFY(copy
.b_efunc(©
) == 0);
2899 hash_lock
= HDR_LOCK(hdr
);
2900 mutex_enter(hash_lock
);
2902 ASSERT(buf
->b_hdr
== hdr
);
2903 ASSERT3U(refcount_count(&hdr
->b_refcnt
), <, hdr
->b_datacnt
);
2904 ASSERT(hdr
->b_state
== arc_mru
|| hdr
->b_state
== arc_mfu
);
2907 * Pull this buffer off of the hdr
2910 while (*bufp
!= buf
)
2911 bufp
= &(*bufp
)->b_next
;
2912 *bufp
= buf
->b_next
;
2914 ASSERT(buf
->b_data
!= NULL
);
2915 arc_buf_destroy(buf
, FALSE
, FALSE
);
2917 if (hdr
->b_datacnt
== 0) {
2918 arc_state_t
*old_state
= hdr
->b_state
;
2919 arc_state_t
*evicted_state
;
2921 ASSERT(refcount_is_zero(&hdr
->b_refcnt
));
2924 (old_state
== arc_mru
) ? arc_mru_ghost
: arc_mfu_ghost
;
2926 mutex_enter(&old_state
->arcs_mtx
);
2927 mutex_enter(&evicted_state
->arcs_mtx
);
2929 arc_change_state(evicted_state
, hdr
, hash_lock
);
2930 ASSERT(HDR_IN_HASH_TABLE(hdr
));
2931 hdr
->b_flags
|= ARC_IN_HASH_TABLE
;
2932 hdr
->b_flags
&= ~ARC_BUF_AVAILABLE
;
2934 mutex_exit(&evicted_state
->arcs_mtx
);
2935 mutex_exit(&old_state
->arcs_mtx
);
2937 mutex_exit(hash_lock
);
2938 rw_exit(&buf
->b_lock
);
2940 VERIFY(buf
->b_efunc(buf
) == 0);
2941 buf
->b_efunc
= NULL
;
2942 buf
->b_private
= NULL
;
2944 kmem_cache_free(buf_cache
, buf
);
2949 * Release this buffer from the cache. This must be done
2950 * after a read and prior to modifying the buffer contents.
2951 * If the buffer has more than one reference, we must make
2952 * a new hdr for the buffer.
2955 arc_release(arc_buf_t
*buf
, void *tag
)
2958 kmutex_t
*hash_lock
;
2959 l2arc_buf_hdr_t
*l2hdr
;
2961 boolean_t released
= B_FALSE
;
2963 rw_enter(&buf
->b_lock
, RW_WRITER
);
2966 /* this buffer is not on any list */
2967 ASSERT(refcount_count(&hdr
->b_refcnt
) > 0);
2968 ASSERT(!(hdr
->b_flags
& ARC_STORED
));
2970 if (hdr
->b_state
== arc_anon
) {
2971 /* this buffer is already released */
2972 ASSERT3U(refcount_count(&hdr
->b_refcnt
), ==, 1);
2973 ASSERT(BUF_EMPTY(hdr
));
2974 ASSERT(buf
->b_efunc
== NULL
);
2976 rw_exit(&buf
->b_lock
);
2979 hash_lock
= HDR_LOCK(hdr
);
2980 mutex_enter(hash_lock
);
2983 l2hdr
= hdr
->b_l2hdr
;
2985 mutex_enter(&l2arc_buflist_mtx
);
2986 hdr
->b_l2hdr
= NULL
;
2987 buf_size
= hdr
->b_size
;
2994 * Do we have more than one buf?
2996 if (hdr
->b_datacnt
> 1) {
2997 arc_buf_hdr_t
*nhdr
;
2999 uint64_t blksz
= hdr
->b_size
;
3000 uint64_t spa
= hdr
->b_spa
;
3001 arc_buf_contents_t type
= hdr
->b_type
;
3002 uint32_t flags
= hdr
->b_flags
;
3004 ASSERT(hdr
->b_buf
!= buf
|| buf
->b_next
!= NULL
);
3006 * Pull the data off of this buf and attach it to
3007 * a new anonymous buf.
3009 (void) remove_reference(hdr
, hash_lock
, tag
);
3011 while (*bufp
!= buf
)
3012 bufp
= &(*bufp
)->b_next
;
3013 *bufp
= (*bufp
)->b_next
;
3016 ASSERT3U(hdr
->b_state
->arcs_size
, >=, hdr
->b_size
);
3017 atomic_add_64(&hdr
->b_state
->arcs_size
, -hdr
->b_size
);
3018 if (refcount_is_zero(&hdr
->b_refcnt
)) {
3019 uint64_t *size
= &hdr
->b_state
->arcs_lsize
[hdr
->b_type
];
3020 ASSERT3U(*size
, >=, hdr
->b_size
);
3021 atomic_add_64(size
, -hdr
->b_size
);
3023 hdr
->b_datacnt
-= 1;
3024 arc_cksum_verify(buf
);
3026 mutex_exit(hash_lock
);
3028 nhdr
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
3029 nhdr
->b_size
= blksz
;
3031 nhdr
->b_type
= type
;
3033 nhdr
->b_state
= arc_anon
;
3034 nhdr
->b_arc_access
= 0;
3035 nhdr
->b_flags
= flags
& ARC_L2_WRITING
;
3036 nhdr
->b_l2hdr
= NULL
;
3037 nhdr
->b_datacnt
= 1;
3038 nhdr
->b_freeze_cksum
= NULL
;
3039 (void) refcount_add(&nhdr
->b_refcnt
, tag
);
3041 rw_exit(&buf
->b_lock
);
3042 atomic_add_64(&arc_anon
->arcs_size
, blksz
);
3044 rw_exit(&buf
->b_lock
);
3045 ASSERT(refcount_count(&hdr
->b_refcnt
) == 1);
3046 ASSERT(!list_link_active(&hdr
->b_arc_node
));
3047 ASSERT(!HDR_IO_IN_PROGRESS(hdr
));
3048 arc_change_state(arc_anon
, hdr
, hash_lock
);
3049 hdr
->b_arc_access
= 0;
3050 mutex_exit(hash_lock
);
3052 bzero(&hdr
->b_dva
, sizeof (dva_t
));
3057 buf
->b_efunc
= NULL
;
3058 buf
->b_private
= NULL
;
3062 list_remove(l2hdr
->b_dev
->l2ad_buflist
, hdr
);
3063 kmem_free(l2hdr
, sizeof (l2arc_buf_hdr_t
));
3064 ARCSTAT_INCR(arcstat_l2_size
, -buf_size
);
3065 mutex_exit(&l2arc_buflist_mtx
);
3070 arc_released(arc_buf_t
*buf
)
3074 rw_enter(&buf
->b_lock
, RW_READER
);
3075 released
= (buf
->b_data
!= NULL
&& buf
->b_hdr
->b_state
== arc_anon
);
3076 rw_exit(&buf
->b_lock
);
3081 arc_has_callback(arc_buf_t
*buf
)
3085 rw_enter(&buf
->b_lock
, RW_READER
);
3086 callback
= (buf
->b_efunc
!= NULL
);
3087 rw_exit(&buf
->b_lock
);
3093 arc_referenced(arc_buf_t
*buf
)
3097 rw_enter(&buf
->b_lock
, RW_READER
);
3098 referenced
= (refcount_count(&buf
->b_hdr
->b_refcnt
));
3099 rw_exit(&buf
->b_lock
);
3100 return (referenced
);
3105 arc_write_ready(zio_t
*zio
)
3107 arc_write_callback_t
*callback
= zio
->io_private
;
3108 arc_buf_t
*buf
= callback
->awcb_buf
;
3109 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3111 ASSERT(!refcount_is_zero(&buf
->b_hdr
->b_refcnt
));
3112 callback
->awcb_ready(zio
, buf
, callback
->awcb_private
);
3115 * If the IO is already in progress, then this is a re-write
3116 * attempt, so we need to thaw and re-compute the cksum.
3117 * It is the responsibility of the callback to handle the
3118 * accounting for any re-write attempt.
3120 if (HDR_IO_IN_PROGRESS(hdr
)) {
3121 mutex_enter(&hdr
->b_freeze_lock
);
3122 if (hdr
->b_freeze_cksum
!= NULL
) {
3123 kmem_free(hdr
->b_freeze_cksum
, sizeof (zio_cksum_t
));
3124 hdr
->b_freeze_cksum
= NULL
;
3126 mutex_exit(&hdr
->b_freeze_lock
);
3128 arc_cksum_compute(buf
, B_FALSE
);
3129 hdr
->b_flags
|= ARC_IO_IN_PROGRESS
;
3133 arc_write_done(zio_t
*zio
)
3135 arc_write_callback_t
*callback
= zio
->io_private
;
3136 arc_buf_t
*buf
= callback
->awcb_buf
;
3137 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3141 hdr
->b_dva
= *BP_IDENTITY(zio
->io_bp
);
3142 hdr
->b_birth
= zio
->io_bp
->blk_birth
;
3143 hdr
->b_cksum0
= zio
->io_bp
->blk_cksum
.zc_word
[0];
3145 * If the block to be written was all-zero, we may have
3146 * compressed it away. In this case no write was performed
3147 * so there will be no dva/birth-date/checksum. The buffer
3148 * must therefor remain anonymous (and uncached).
3150 if (!BUF_EMPTY(hdr
)) {
3151 arc_buf_hdr_t
*exists
;
3152 kmutex_t
*hash_lock
;
3154 arc_cksum_verify(buf
);
3156 exists
= buf_hash_insert(hdr
, &hash_lock
);
3159 * This can only happen if we overwrite for
3160 * sync-to-convergence, because we remove
3161 * buffers from the hash table when we arc_free().
3163 ASSERT(zio
->io_flags
& ZIO_FLAG_IO_REWRITE
);
3164 ASSERT(DVA_EQUAL(BP_IDENTITY(&zio
->io_bp_orig
),
3165 BP_IDENTITY(zio
->io_bp
)));
3166 ASSERT3U(zio
->io_bp_orig
.blk_birth
, ==,
3167 zio
->io_bp
->blk_birth
);
3169 ASSERT(refcount_is_zero(&exists
->b_refcnt
));
3170 arc_change_state(arc_anon
, exists
, hash_lock
);
3171 mutex_exit(hash_lock
);
3172 arc_hdr_destroy(exists
);
3173 exists
= buf_hash_insert(hdr
, &hash_lock
);
3174 ASSERT3P(exists
, ==, NULL
);
3176 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3177 /* if it's not anon, we are doing a scrub */
3178 if (hdr
->b_state
== arc_anon
)
3179 arc_access(hdr
, hash_lock
);
3180 mutex_exit(hash_lock
);
3181 } else if (callback
->awcb_done
== NULL
) {
3184 * This is an anonymous buffer with no user callback,
3185 * destroy it if there are no active references.
3187 mutex_enter(&arc_eviction_mtx
);
3188 destroy_hdr
= refcount_is_zero(&hdr
->b_refcnt
);
3189 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3190 mutex_exit(&arc_eviction_mtx
);
3192 arc_hdr_destroy(hdr
);
3194 hdr
->b_flags
&= ~ARC_IO_IN_PROGRESS
;
3196 hdr
->b_flags
&= ~ARC_STORED
;
3198 if (callback
->awcb_done
) {
3199 ASSERT(!refcount_is_zero(&hdr
->b_refcnt
));
3200 callback
->awcb_done(zio
, buf
, callback
->awcb_private
);
3203 kmem_free(callback
, sizeof (arc_write_callback_t
));
3207 write_policy(spa_t
*spa
, const writeprops_t
*wp
, zio_prop_t
*zp
)
3209 boolean_t ismd
= (wp
->wp_level
> 0 || dmu_ot
[wp
->wp_type
].ot_metadata
);
3211 /* Determine checksum setting */
3214 * Metadata always gets checksummed. If the data
3215 * checksum is multi-bit correctable, and it's not a
3216 * ZBT-style checksum, then it's suitable for metadata
3217 * as well. Otherwise, the metadata checksum defaults
3220 if (zio_checksum_table
[wp
->wp_oschecksum
].ci_correctable
&&
3221 !zio_checksum_table
[wp
->wp_oschecksum
].ci_zbt
)
3222 zp
->zp_checksum
= wp
->wp_oschecksum
;
3224 zp
->zp_checksum
= ZIO_CHECKSUM_FLETCHER_4
;
3226 zp
->zp_checksum
= zio_checksum_select(wp
->wp_dnchecksum
,
3230 /* Determine compression setting */
3233 * XXX -- we should design a compression algorithm
3234 * that specializes in arrays of bps.
3236 zp
->zp_compress
= zfs_mdcomp_disable
? ZIO_COMPRESS_EMPTY
:
3239 zp
->zp_compress
= zio_compress_select(wp
->wp_dncompress
,
3243 zp
->zp_type
= wp
->wp_type
;
3244 zp
->zp_level
= wp
->wp_level
;
3245 zp
->zp_ndvas
= MIN(wp
->wp_copies
+ ismd
, spa_max_replication(spa
));
3249 arc_write(zio_t
*pio
, spa_t
*spa
, const writeprops_t
*wp
,
3250 boolean_t l2arc
, uint64_t txg
, blkptr_t
*bp
, arc_buf_t
*buf
,
3251 arc_done_func_t
*ready
, arc_done_func_t
*done
, void *private, int priority
,
3252 int zio_flags
, const zbookmark_t
*zb
)
3254 arc_buf_hdr_t
*hdr
= buf
->b_hdr
;
3255 arc_write_callback_t
*callback
;
3259 ASSERT(ready
!= NULL
);
3260 ASSERT(!HDR_IO_ERROR(hdr
));
3261 ASSERT((hdr
->b_flags
& ARC_IO_IN_PROGRESS
) == 0);
3262 ASSERT(hdr
->b_acb
== 0);
3264 hdr
->b_flags
|= ARC_L2CACHE
;
3265 callback
= kmem_zalloc(sizeof (arc_write_callback_t
), KM_SLEEP
);
3266 callback
->awcb_ready
= ready
;
3267 callback
->awcb_done
= done
;
3268 callback
->awcb_private
= private;
3269 callback
->awcb_buf
= buf
;
3271 write_policy(spa
, wp
, &zp
);
3272 zio
= zio_write(pio
, spa
, txg
, bp
, buf
->b_data
, hdr
->b_size
, &zp
,
3273 arc_write_ready
, arc_write_done
, callback
, priority
, zio_flags
, zb
);
3279 arc_free(zio_t
*pio
, spa_t
*spa
, uint64_t txg
, blkptr_t
*bp
,
3280 zio_done_func_t
*done
, void *private, uint32_t arc_flags
)
3283 kmutex_t
*hash_lock
;
3285 uint64_t guid
= spa_guid(spa
);
3288 * If this buffer is in the cache, release it, so it
3291 ab
= buf_hash_find(guid
, BP_IDENTITY(bp
), bp
->blk_birth
, &hash_lock
);
3294 * The checksum of blocks to free is not always
3295 * preserved (eg. on the deadlist). However, if it is
3296 * nonzero, it should match what we have in the cache.
3298 ASSERT(bp
->blk_cksum
.zc_word
[0] == 0 ||
3299 bp
->blk_cksum
.zc_word
[0] == ab
->b_cksum0
||
3300 bp
->blk_fill
== BLK_FILL_ALREADY_FREED
);
3302 if (ab
->b_state
!= arc_anon
)
3303 arc_change_state(arc_anon
, ab
, hash_lock
);
3304 if (HDR_IO_IN_PROGRESS(ab
)) {
3306 * This should only happen when we prefetch.
3308 ASSERT(ab
->b_flags
& ARC_PREFETCH
);
3309 ASSERT3U(ab
->b_datacnt
, ==, 1);
3310 ab
->b_flags
|= ARC_FREED_IN_READ
;
3311 if (HDR_IN_HASH_TABLE(ab
))
3312 buf_hash_remove(ab
);
3313 ab
->b_arc_access
= 0;
3314 bzero(&ab
->b_dva
, sizeof (dva_t
));
3317 ab
->b_buf
->b_efunc
= NULL
;
3318 ab
->b_buf
->b_private
= NULL
;
3319 mutex_exit(hash_lock
);
3320 } else if (refcount_is_zero(&ab
->b_refcnt
)) {
3321 ab
->b_flags
|= ARC_FREE_IN_PROGRESS
;
3322 mutex_exit(hash_lock
);
3323 arc_hdr_destroy(ab
);
3324 ARCSTAT_BUMP(arcstat_deleted
);
3327 * We still have an active reference on this
3328 * buffer. This can happen, e.g., from
3329 * dbuf_unoverride().
3331 ASSERT(!HDR_IN_HASH_TABLE(ab
));
3332 ab
->b_arc_access
= 0;
3333 bzero(&ab
->b_dva
, sizeof (dva_t
));
3336 ab
->b_buf
->b_efunc
= NULL
;
3337 ab
->b_buf
->b_private
= NULL
;
3338 mutex_exit(hash_lock
);
3342 zio
= zio_free(pio
, spa
, txg
, bp
, done
, private, ZIO_FLAG_MUSTSUCCEED
);
3344 if (arc_flags
& ARC_WAIT
)
3345 return (zio_wait(zio
));
3347 ASSERT(arc_flags
& ARC_NOWAIT
);
3354 arc_memory_throttle(uint64_t reserve
, uint64_t inflight_data
, uint64_t txg
)
3357 uint64_t available_memory
= ptob(freemem
);
3358 static uint64_t page_load
= 0;
3359 static uint64_t last_txg
= 0;
3363 MIN(available_memory
, vmem_size(heap_arena
, VMEM_FREE
));
3365 if (available_memory
>= zfs_write_limit_max
)
3368 if (txg
> last_txg
) {
3373 * If we are in pageout, we know that memory is already tight,
3374 * the arc is already going to be evicting, so we just want to
3375 * continue to let page writes occur as quickly as possible.
3377 if (curproc
== proc_pageout
) {
3378 if (page_load
> MAX(ptob(minfree
), available_memory
) / 4)
3380 /* Note: reserve is inflated, so we deflate */
3381 page_load
+= reserve
/ 8;
3383 } else if (page_load
> 0 && arc_reclaim_needed()) {
3384 /* memory is low, delay before restarting */
3385 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
3390 if (arc_size
> arc_c_min
) {
3391 uint64_t evictable_memory
=
3392 arc_mru
->arcs_lsize
[ARC_BUFC_DATA
] +
3393 arc_mru
->arcs_lsize
[ARC_BUFC_METADATA
] +
3394 arc_mfu
->arcs_lsize
[ARC_BUFC_DATA
] +
3395 arc_mfu
->arcs_lsize
[ARC_BUFC_METADATA
];
3396 available_memory
+= MIN(evictable_memory
, arc_size
- arc_c_min
);
3399 if (inflight_data
> available_memory
/ 4) {
3400 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
3408 arc_tempreserve_clear(uint64_t reserve
)
3410 atomic_add_64(&arc_tempreserve
, -reserve
);
3411 ASSERT((int64_t)arc_tempreserve
>= 0);
3415 arc_tempreserve_space(uint64_t reserve
, uint64_t txg
)
3422 * Once in a while, fail for no reason. Everything should cope.
3424 if (spa_get_random(10000) == 0) {
3425 dprintf("forcing random failure\n");
3429 if (reserve
> arc_c
/4 && !arc_no_grow
)
3430 arc_c
= MIN(arc_c_max
, reserve
* 4);
3431 if (reserve
> arc_c
)
3435 * Don't count loaned bufs as in flight dirty data to prevent long
3436 * network delays from blocking transactions that are ready to be
3437 * assigned to a txg.
3439 anon_size
= MAX((int64_t)(arc_anon
->arcs_size
- arc_loaned_bytes
), 0);
3442 * Writes will, almost always, require additional memory allocations
3443 * in order to compress/encrypt/etc the data. We therefor need to
3444 * make sure that there is sufficient available memory for this.
3446 if (error
= arc_memory_throttle(reserve
, anon_size
, txg
))
3450 * Throttle writes when the amount of dirty data in the cache
3451 * gets too large. We try to keep the cache less than half full
3452 * of dirty blocks so that our sync times don't grow too large.
3453 * Note: if two requests come in concurrently, we might let them
3454 * both succeed, when one of them should fail. Not a huge deal.
3457 if (reserve
+ arc_tempreserve
+ anon_size
> arc_c
/ 2 &&
3458 anon_size
> arc_c
/ 4) {
3459 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3460 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3461 arc_tempreserve
>>10,
3462 arc_anon
->arcs_lsize
[ARC_BUFC_METADATA
]>>10,
3463 arc_anon
->arcs_lsize
[ARC_BUFC_DATA
]>>10,
3464 reserve
>>10, arc_c
>>10);
3467 atomic_add_64(&arc_tempreserve
, reserve
);
3474 mutex_init(&arc_reclaim_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3475 cv_init(&arc_reclaim_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
3477 /* Convert seconds to clock ticks */
3478 arc_min_prefetch_lifespan
= 1 * hz
;
3480 /* Start out with 1/8 of all memory */
3481 arc_c
= physmem
* PAGESIZE
/ 8;
3485 * On architectures where the physical memory can be larger
3486 * than the addressable space (intel in 32-bit mode), we may
3487 * need to limit the cache to 1/8 of VM size.
3489 arc_c
= MIN(arc_c
, vmem_size(heap_arena
, VMEM_ALLOC
| VMEM_FREE
) / 8);
3492 /* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3493 arc_c_min
= MAX(arc_c
/ 4, 64<<20);
3494 /* set max to 3/4 of all memory, or all but 1GB, whichever is more */
3495 if (arc_c
* 8 >= 1<<30)
3496 arc_c_max
= (arc_c
* 8) - (1<<30);
3498 arc_c_max
= arc_c_min
;
3499 arc_c_max
= MAX(arc_c
* 6, arc_c_max
);
3502 * Allow the tunables to override our calculations if they are
3503 * reasonable (ie. over 64MB)
3505 if (zfs_arc_max
> 64<<20 && zfs_arc_max
< physmem
* PAGESIZE
)
3506 arc_c_max
= zfs_arc_max
;
3507 if (zfs_arc_min
> 64<<20 && zfs_arc_min
<= arc_c_max
)
3508 arc_c_min
= zfs_arc_min
;
3511 arc_p
= (arc_c
>> 1);
3513 /* limit meta-data to 1/4 of the arc capacity */
3514 arc_meta_limit
= arc_c_max
/ 4;
3516 /* Allow the tunable to override if it is reasonable */
3517 if (zfs_arc_meta_limit
> 0 && zfs_arc_meta_limit
<= arc_c_max
)
3518 arc_meta_limit
= zfs_arc_meta_limit
;
3520 if (arc_c_min
< arc_meta_limit
/ 2 && zfs_arc_min
== 0)
3521 arc_c_min
= arc_meta_limit
/ 2;
3523 if (zfs_arc_grow_retry
> 0)
3524 arc_grow_retry
= zfs_arc_grow_retry
;
3526 if (zfs_arc_shrink_shift
> 0)
3527 arc_shrink_shift
= zfs_arc_shrink_shift
;
3529 if (zfs_arc_p_min_shift
> 0)
3530 arc_p_min_shift
= zfs_arc_p_min_shift
;
3532 /* if kmem_flags are set, lets try to use less memory */
3533 if (kmem_debugging())
3535 if (arc_c
< arc_c_min
)
3538 arc_anon
= &ARC_anon
;
3540 arc_mru_ghost
= &ARC_mru_ghost
;
3542 arc_mfu_ghost
= &ARC_mfu_ghost
;
3543 arc_l2c_only
= &ARC_l2c_only
;
3546 mutex_init(&arc_anon
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3547 mutex_init(&arc_mru
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3548 mutex_init(&arc_mru_ghost
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3549 mutex_init(&arc_mfu
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3550 mutex_init(&arc_mfu_ghost
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3551 mutex_init(&arc_l2c_only
->arcs_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3553 list_create(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
],
3554 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3555 list_create(&arc_mru
->arcs_list
[ARC_BUFC_DATA
],
3556 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3557 list_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
],
3558 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3559 list_create(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
],
3560 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3561 list_create(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
],
3562 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3563 list_create(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
],
3564 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3565 list_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
],
3566 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3567 list_create(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
],
3568 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3569 list_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_METADATA
],
3570 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3571 list_create(&arc_l2c_only
->arcs_list
[ARC_BUFC_DATA
],
3572 sizeof (arc_buf_hdr_t
), offsetof(arc_buf_hdr_t
, b_arc_node
));
3576 arc_thread_exit
= 0;
3577 arc_eviction_list
= NULL
;
3578 mutex_init(&arc_eviction_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3579 bzero(&arc_eviction_hdr
, sizeof (arc_buf_hdr_t
));
3581 arc_ksp
= kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED
,
3582 sizeof (arc_stats
) / sizeof (kstat_named_t
), KSTAT_FLAG_VIRTUAL
);
3584 if (arc_ksp
!= NULL
) {
3585 arc_ksp
->ks_data
= &arc_stats
;
3586 kstat_install(arc_ksp
);
3589 (void) thread_create(NULL
, 0, arc_reclaim_thread
, NULL
, 0, &p0
,
3590 TS_RUN
, minclsyspri
);
3595 if (zfs_write_limit_max
== 0)
3596 zfs_write_limit_max
= ptob(physmem
) >> zfs_write_limit_shift
;
3598 zfs_write_limit_shift
= 0;
3599 mutex_init(&zfs_write_limit_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
3605 mutex_enter(&arc_reclaim_thr_lock
);
3606 arc_thread_exit
= 1;
3607 while (arc_thread_exit
!= 0)
3608 cv_wait(&arc_reclaim_thr_cv
, &arc_reclaim_thr_lock
);
3609 mutex_exit(&arc_reclaim_thr_lock
);
3615 if (arc_ksp
!= NULL
) {
3616 kstat_delete(arc_ksp
);
3620 mutex_destroy(&arc_eviction_mtx
);
3621 mutex_destroy(&arc_reclaim_thr_lock
);
3622 cv_destroy(&arc_reclaim_thr_cv
);
3624 list_destroy(&arc_mru
->arcs_list
[ARC_BUFC_METADATA
]);
3625 list_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
3626 list_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_METADATA
]);
3627 list_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_METADATA
]);
3628 list_destroy(&arc_mru
->arcs_list
[ARC_BUFC_DATA
]);
3629 list_destroy(&arc_mru_ghost
->arcs_list
[ARC_BUFC_DATA
]);
3630 list_destroy(&arc_mfu
->arcs_list
[ARC_BUFC_DATA
]);
3631 list_destroy(&arc_mfu_ghost
->arcs_list
[ARC_BUFC_DATA
]);
3633 mutex_destroy(&arc_anon
->arcs_mtx
);
3634 mutex_destroy(&arc_mru
->arcs_mtx
);
3635 mutex_destroy(&arc_mru_ghost
->arcs_mtx
);
3636 mutex_destroy(&arc_mfu
->arcs_mtx
);
3637 mutex_destroy(&arc_mfu_ghost
->arcs_mtx
);
3638 mutex_destroy(&arc_l2c_only
->arcs_mtx
);
3640 mutex_destroy(&zfs_write_limit_lock
);
3644 ASSERT(arc_loaned_bytes
== 0);
3650 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3651 * It uses dedicated storage devices to hold cached data, which are populated
3652 * using large infrequent writes. The main role of this cache is to boost
3653 * the performance of random read workloads. The intended L2ARC devices
3654 * include short-stroked disks, solid state disks, and other media with
3655 * substantially faster read latency than disk.
3657 * +-----------------------+
3659 * +-----------------------+
3662 * l2arc_feed_thread() arc_read()
3666 * +---------------+ |
3668 * +---------------+ |
3673 * +-------+ +-------+
3675 * | cache | | cache |
3676 * +-------+ +-------+
3677 * +=========+ .-----.
3678 * : L2ARC : |-_____-|
3679 * : devices : | Disks |
3680 * +=========+ `-_____-'
3682 * Read requests are satisfied from the following sources, in order:
3685 * 2) vdev cache of L2ARC devices
3687 * 4) vdev cache of disks
3690 * Some L2ARC device types exhibit extremely slow write performance.
3691 * To accommodate for this there are some significant differences between
3692 * the L2ARC and traditional cache design:
3694 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
3695 * the ARC behave as usual, freeing buffers and placing headers on ghost
3696 * lists. The ARC does not send buffers to the L2ARC during eviction as
3697 * this would add inflated write latencies for all ARC memory pressure.
3699 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
3700 * It does this by periodically scanning buffers from the eviction-end of
3701 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
3702 * not already there. It scans until a headroom of buffers is satisfied,
3703 * which itself is a buffer for ARC eviction. The thread that does this is
3704 * l2arc_feed_thread(), illustrated below; example sizes are included to
3705 * provide a better sense of ratio than this diagram:
3708 * +---------------------+----------+
3709 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
3710 * +---------------------+----------+ | o L2ARC eligible
3711 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
3712 * +---------------------+----------+ |
3713 * 15.9 Gbytes ^ 32 Mbytes |
3715 * l2arc_feed_thread()
3717 * l2arc write hand <--[oooo]--'
3721 * +==============================+
3722 * L2ARC dev |####|#|###|###| |####| ... |
3723 * +==============================+
3726 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
3727 * evicted, then the L2ARC has cached a buffer much sooner than it probably
3728 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
3729 * safe to say that this is an uncommon case, since buffers at the end of
3730 * the ARC lists have moved there due to inactivity.
3732 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
3733 * then the L2ARC simply misses copying some buffers. This serves as a
3734 * pressure valve to prevent heavy read workloads from both stalling the ARC
3735 * with waits and clogging the L2ARC with writes. This also helps prevent
3736 * the potential for the L2ARC to churn if it attempts to cache content too
3737 * quickly, such as during backups of the entire pool.
3739 * 5. After system boot and before the ARC has filled main memory, there are
3740 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
3741 * lists can remain mostly static. Instead of searching from tail of these
3742 * lists as pictured, the l2arc_feed_thread() will search from the list heads
3743 * for eligible buffers, greatly increasing its chance of finding them.
3745 * The L2ARC device write speed is also boosted during this time so that
3746 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
3747 * there are no L2ARC reads, and no fear of degrading read performance
3748 * through increased writes.
3750 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
3751 * the vdev queue can aggregate them into larger and fewer writes. Each
3752 * device is written to in a rotor fashion, sweeping writes through
3753 * available space then repeating.
3755 * 7. The L2ARC does not store dirty content. It never needs to flush
3756 * write buffers back to disk based storage.
3758 * 8. If an ARC buffer is written (and dirtied) which also exists in the
3759 * L2ARC, the now stale L2ARC buffer is immediately dropped.
3761 * The performance of the L2ARC can be tweaked by a number of tunables, which
3762 * may be necessary for different workloads:
3764 * l2arc_write_max max write bytes per interval
3765 * l2arc_write_boost extra write bytes during device warmup
3766 * l2arc_noprefetch skip caching prefetched buffers
3767 * l2arc_headroom number of max device writes to precache
3768 * l2arc_feed_secs seconds between L2ARC writing
3770 * Tunables may be removed or added as future performance improvements are
3771 * integrated, and also may become zpool properties.
3773 * There are three key functions that control how the L2ARC warms up:
3775 * l2arc_write_eligible() check if a buffer is eligible to cache
3776 * l2arc_write_size() calculate how much to write
3777 * l2arc_write_interval() calculate sleep delay between writes
3779 * These three functions determine what to write, how much, and how quickly
3784 l2arc_write_eligible(uint64_t spa_guid
, arc_buf_hdr_t
*ab
)
3787 * A buffer is *not* eligible for the L2ARC if it:
3788 * 1. belongs to a different spa.
3789 * 2. has no attached buffer.
3790 * 3. is already cached on the L2ARC.
3791 * 4. has an I/O in progress (it may be an incomplete read).
3792 * 5. is flagged not eligible (zfs property).
3794 if (ab
->b_spa
!= spa_guid
|| ab
->b_buf
== NULL
|| ab
->b_l2hdr
!= NULL
||
3795 HDR_IO_IN_PROGRESS(ab
) || !HDR_L2CACHE(ab
))
3802 l2arc_write_size(l2arc_dev_t
*dev
)
3806 size
= dev
->l2ad_write
;
3808 if (arc_warm
== B_FALSE
)
3809 size
+= dev
->l2ad_boost
;
3816 l2arc_write_interval(clock_t began
, uint64_t wanted
, uint64_t wrote
)
3818 clock_t interval
, next
;
3821 * If the ARC lists are busy, increase our write rate; if the
3822 * lists are stale, idle back. This is achieved by checking
3823 * how much we previously wrote - if it was more than half of
3824 * what we wanted, schedule the next write much sooner.
3826 if (l2arc_feed_again
&& wrote
> (wanted
/ 2))
3827 interval
= (hz
* l2arc_feed_min_ms
) / 1000;
3829 interval
= hz
* l2arc_feed_secs
;
3831 next
= MAX(lbolt
, MIN(lbolt
+ interval
, began
+ interval
));
3837 l2arc_hdr_stat_add(void)
3839 ARCSTAT_INCR(arcstat_l2_hdr_size
, HDR_SIZE
+ L2HDR_SIZE
);
3840 ARCSTAT_INCR(arcstat_hdr_size
, -HDR_SIZE
);
3844 l2arc_hdr_stat_remove(void)
3846 ARCSTAT_INCR(arcstat_l2_hdr_size
, -(HDR_SIZE
+ L2HDR_SIZE
));
3847 ARCSTAT_INCR(arcstat_hdr_size
, HDR_SIZE
);
3851 * Cycle through L2ARC devices. This is how L2ARC load balances.
3852 * If a device is returned, this also returns holding the spa config lock.
3854 static l2arc_dev_t
*
3855 l2arc_dev_get_next(void)
3857 l2arc_dev_t
*first
, *next
= NULL
;
3860 * Lock out the removal of spas (spa_namespace_lock), then removal
3861 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
3862 * both locks will be dropped and a spa config lock held instead.
3864 mutex_enter(&spa_namespace_lock
);
3865 mutex_enter(&l2arc_dev_mtx
);
3867 /* if there are no vdevs, there is nothing to do */
3868 if (l2arc_ndev
== 0)
3872 next
= l2arc_dev_last
;
3874 /* loop around the list looking for a non-faulted vdev */
3876 next
= list_head(l2arc_dev_list
);
3878 next
= list_next(l2arc_dev_list
, next
);
3880 next
= list_head(l2arc_dev_list
);
3883 /* if we have come back to the start, bail out */
3886 else if (next
== first
)
3889 } while (vdev_is_dead(next
->l2ad_vdev
));
3891 /* if we were unable to find any usable vdevs, return NULL */
3892 if (vdev_is_dead(next
->l2ad_vdev
))
3895 l2arc_dev_last
= next
;
3898 mutex_exit(&l2arc_dev_mtx
);
3901 * Grab the config lock to prevent the 'next' device from being
3902 * removed while we are writing to it.
3905 spa_config_enter(next
->l2ad_spa
, SCL_L2ARC
, next
, RW_READER
);
3906 mutex_exit(&spa_namespace_lock
);
3912 * Free buffers that were tagged for destruction.
3915 l2arc_do_free_on_write()
3918 l2arc_data_free_t
*df
, *df_prev
;
3920 mutex_enter(&l2arc_free_on_write_mtx
);
3921 buflist
= l2arc_free_on_write
;
3923 for (df
= list_tail(buflist
); df
; df
= df_prev
) {
3924 df_prev
= list_prev(buflist
, df
);
3925 ASSERT(df
->l2df_data
!= NULL
);
3926 ASSERT(df
->l2df_func
!= NULL
);
3927 df
->l2df_func(df
->l2df_data
, df
->l2df_size
);
3928 list_remove(buflist
, df
);
3929 kmem_free(df
, sizeof (l2arc_data_free_t
));
3932 mutex_exit(&l2arc_free_on_write_mtx
);
3936 * A write to a cache device has completed. Update all headers to allow
3937 * reads from these buffers to begin.
3940 l2arc_write_done(zio_t
*zio
)
3942 l2arc_write_callback_t
*cb
;
3945 arc_buf_hdr_t
*head
, *ab
, *ab_prev
;
3946 l2arc_buf_hdr_t
*abl2
;
3947 kmutex_t
*hash_lock
;
3949 cb
= zio
->io_private
;
3951 dev
= cb
->l2wcb_dev
;
3952 ASSERT(dev
!= NULL
);
3953 head
= cb
->l2wcb_head
;
3954 ASSERT(head
!= NULL
);
3955 buflist
= dev
->l2ad_buflist
;
3956 ASSERT(buflist
!= NULL
);
3957 DTRACE_PROBE2(l2arc__iodone
, zio_t
*, zio
,
3958 l2arc_write_callback_t
*, cb
);
3960 if (zio
->io_error
!= 0)
3961 ARCSTAT_BUMP(arcstat_l2_writes_error
);
3963 mutex_enter(&l2arc_buflist_mtx
);
3966 * All writes completed, or an error was hit.
3968 for (ab
= list_prev(buflist
, head
); ab
; ab
= ab_prev
) {
3969 ab_prev
= list_prev(buflist
, ab
);
3971 hash_lock
= HDR_LOCK(ab
);
3972 if (!mutex_tryenter(hash_lock
)) {
3974 * This buffer misses out. It may be in a stage
3975 * of eviction. Its ARC_L2_WRITING flag will be
3976 * left set, denying reads to this buffer.
3978 ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss
);
3982 if (zio
->io_error
!= 0) {
3984 * Error - drop L2ARC entry.
3986 list_remove(buflist
, ab
);
3989 kmem_free(abl2
, sizeof (l2arc_buf_hdr_t
));
3990 ARCSTAT_INCR(arcstat_l2_size
, -ab
->b_size
);
3994 * Allow ARC to begin reads to this L2ARC entry.
3996 ab
->b_flags
&= ~ARC_L2_WRITING
;
3998 mutex_exit(hash_lock
);
4001 atomic_inc_64(&l2arc_writes_done
);
4002 list_remove(buflist
, head
);
4003 kmem_cache_free(hdr_cache
, head
);
4004 mutex_exit(&l2arc_buflist_mtx
);
4006 l2arc_do_free_on_write();
4008 kmem_free(cb
, sizeof (l2arc_write_callback_t
));
4012 * A read to a cache device completed. Validate buffer contents before
4013 * handing over to the regular ARC routines.
4016 l2arc_read_done(zio_t
*zio
)
4018 l2arc_read_callback_t
*cb
;
4021 kmutex_t
*hash_lock
;
4024 ASSERT(zio
->io_vd
!= NULL
);
4025 ASSERT(zio
->io_flags
& ZIO_FLAG_DONT_PROPAGATE
);
4027 spa_config_exit(zio
->io_spa
, SCL_L2ARC
, zio
->io_vd
);
4029 cb
= zio
->io_private
;
4031 buf
= cb
->l2rcb_buf
;
4032 ASSERT(buf
!= NULL
);
4034 ASSERT(hdr
!= NULL
);
4036 hash_lock
= HDR_LOCK(hdr
);
4037 mutex_enter(hash_lock
);
4040 * Check this survived the L2ARC journey.
4042 equal
= arc_cksum_equal(buf
);
4043 if (equal
&& zio
->io_error
== 0 && !HDR_L2_EVICTED(hdr
)) {
4044 mutex_exit(hash_lock
);
4045 zio
->io_private
= buf
;
4046 zio
->io_bp_copy
= cb
->l2rcb_bp
; /* XXX fix in L2ARC 2.0 */
4047 zio
->io_bp
= &zio
->io_bp_copy
; /* XXX fix in L2ARC 2.0 */
4050 mutex_exit(hash_lock
);
4052 * Buffer didn't survive caching. Increment stats and
4053 * reissue to the original storage device.
4055 if (zio
->io_error
!= 0) {
4056 ARCSTAT_BUMP(arcstat_l2_io_error
);
4058 zio
->io_error
= EIO
;
4061 ARCSTAT_BUMP(arcstat_l2_cksum_bad
);
4064 * If there's no waiter, issue an async i/o to the primary
4065 * storage now. If there *is* a waiter, the caller must
4066 * issue the i/o in a context where it's OK to block.
4068 if (zio
->io_waiter
== NULL
) {
4069 zio_t
*pio
= zio_unique_parent(zio
);
4071 ASSERT(!pio
|| pio
->io_child_type
== ZIO_CHILD_LOGICAL
);
4073 zio_nowait(zio_read(pio
, cb
->l2rcb_spa
, &cb
->l2rcb_bp
,
4074 buf
->b_data
, zio
->io_size
, arc_read_done
, buf
,
4075 zio
->io_priority
, cb
->l2rcb_flags
, &cb
->l2rcb_zb
));
4079 kmem_free(cb
, sizeof (l2arc_read_callback_t
));
4083 * This is the list priority from which the L2ARC will search for pages to
4084 * cache. This is used within loops (0..3) to cycle through lists in the
4085 * desired order. This order can have a significant effect on cache
4088 * Currently the metadata lists are hit first, MFU then MRU, followed by
4089 * the data lists. This function returns a locked list, and also returns
4093 l2arc_list_locked(int list_num
, kmutex_t
**lock
)
4097 ASSERT(list_num
>= 0 && list_num
<= 3);
4101 list
= &arc_mfu
->arcs_list
[ARC_BUFC_METADATA
];
4102 *lock
= &arc_mfu
->arcs_mtx
;
4105 list
= &arc_mru
->arcs_list
[ARC_BUFC_METADATA
];
4106 *lock
= &arc_mru
->arcs_mtx
;
4109 list
= &arc_mfu
->arcs_list
[ARC_BUFC_DATA
];
4110 *lock
= &arc_mfu
->arcs_mtx
;
4113 list
= &arc_mru
->arcs_list
[ARC_BUFC_DATA
];
4114 *lock
= &arc_mru
->arcs_mtx
;
4118 ASSERT(!(MUTEX_HELD(*lock
)));
4124 * Evict buffers from the device write hand to the distance specified in
4125 * bytes. This distance may span populated buffers, it may span nothing.
4126 * This is clearing a region on the L2ARC device ready for writing.
4127 * If the 'all' boolean is set, every buffer is evicted.
4130 l2arc_evict(l2arc_dev_t
*dev
, uint64_t distance
, boolean_t all
)
4133 l2arc_buf_hdr_t
*abl2
;
4134 arc_buf_hdr_t
*ab
, *ab_prev
;
4135 kmutex_t
*hash_lock
;
4138 buflist
= dev
->l2ad_buflist
;
4140 if (buflist
== NULL
)
4143 if (!all
&& dev
->l2ad_first
) {
4145 * This is the first sweep through the device. There is
4151 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- (2 * distance
))) {
4153 * When nearing the end of the device, evict to the end
4154 * before the device write hand jumps to the start.
4156 taddr
= dev
->l2ad_end
;
4158 taddr
= dev
->l2ad_hand
+ distance
;
4160 DTRACE_PROBE4(l2arc__evict
, l2arc_dev_t
*, dev
, list_t
*, buflist
,
4161 uint64_t, taddr
, boolean_t
, all
);
4164 mutex_enter(&l2arc_buflist_mtx
);
4165 for (ab
= list_tail(buflist
); ab
; ab
= ab_prev
) {
4166 ab_prev
= list_prev(buflist
, ab
);
4168 hash_lock
= HDR_LOCK(ab
);
4169 if (!mutex_tryenter(hash_lock
)) {
4171 * Missed the hash lock. Retry.
4173 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry
);
4174 mutex_exit(&l2arc_buflist_mtx
);
4175 mutex_enter(hash_lock
);
4176 mutex_exit(hash_lock
);
4180 if (HDR_L2_WRITE_HEAD(ab
)) {
4182 * We hit a write head node. Leave it for
4183 * l2arc_write_done().
4185 list_remove(buflist
, ab
);
4186 mutex_exit(hash_lock
);
4190 if (!all
&& ab
->b_l2hdr
!= NULL
&&
4191 (ab
->b_l2hdr
->b_daddr
> taddr
||
4192 ab
->b_l2hdr
->b_daddr
< dev
->l2ad_hand
)) {
4194 * We've evicted to the target address,
4195 * or the end of the device.
4197 mutex_exit(hash_lock
);
4201 if (HDR_FREE_IN_PROGRESS(ab
)) {
4203 * Already on the path to destruction.
4205 mutex_exit(hash_lock
);
4209 if (ab
->b_state
== arc_l2c_only
) {
4210 ASSERT(!HDR_L2_READING(ab
));
4212 * This doesn't exist in the ARC. Destroy.
4213 * arc_hdr_destroy() will call list_remove()
4214 * and decrement arcstat_l2_size.
4216 arc_change_state(arc_anon
, ab
, hash_lock
);
4217 arc_hdr_destroy(ab
);
4220 * Invalidate issued or about to be issued
4221 * reads, since we may be about to write
4222 * over this location.
4224 if (HDR_L2_READING(ab
)) {
4225 ARCSTAT_BUMP(arcstat_l2_evict_reading
);
4226 ab
->b_flags
|= ARC_L2_EVICTED
;
4230 * Tell ARC this no longer exists in L2ARC.
4232 if (ab
->b_l2hdr
!= NULL
) {
4235 kmem_free(abl2
, sizeof (l2arc_buf_hdr_t
));
4236 ARCSTAT_INCR(arcstat_l2_size
, -ab
->b_size
);
4238 list_remove(buflist
, ab
);
4241 * This may have been leftover after a
4244 ab
->b_flags
&= ~ARC_L2_WRITING
;
4246 mutex_exit(hash_lock
);
4248 mutex_exit(&l2arc_buflist_mtx
);
4250 spa_l2cache_space_update(dev
->l2ad_vdev
, 0, -(taddr
- dev
->l2ad_evict
));
4251 dev
->l2ad_evict
= taddr
;
4255 * Find and write ARC buffers to the L2ARC device.
4257 * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4258 * for reading until they have completed writing.
4261 l2arc_write_buffers(spa_t
*spa
, l2arc_dev_t
*dev
, uint64_t target_sz
)
4263 arc_buf_hdr_t
*ab
, *ab_prev
, *head
;
4264 l2arc_buf_hdr_t
*hdrl2
;
4266 uint64_t passed_sz
, write_sz
, buf_sz
, headroom
;
4268 kmutex_t
*hash_lock
, *list_lock
;
4269 boolean_t have_lock
, full
;
4270 l2arc_write_callback_t
*cb
;
4272 uint64_t guid
= spa_guid(spa
);
4274 ASSERT(dev
->l2ad_vdev
!= NULL
);
4279 head
= kmem_cache_alloc(hdr_cache
, KM_PUSHPAGE
);
4280 head
->b_flags
|= ARC_L2_WRITE_HEAD
;
4283 * Copy buffers for L2ARC writing.
4285 mutex_enter(&l2arc_buflist_mtx
);
4286 for (int try = 0; try <= 3; try++) {
4287 list
= l2arc_list_locked(try, &list_lock
);
4291 * L2ARC fast warmup.
4293 * Until the ARC is warm and starts to evict, read from the
4294 * head of the ARC lists rather than the tail.
4296 headroom
= target_sz
* l2arc_headroom
;
4297 if (arc_warm
== B_FALSE
)
4298 ab
= list_head(list
);
4300 ab
= list_tail(list
);
4302 for (; ab
; ab
= ab_prev
) {
4303 if (arc_warm
== B_FALSE
)
4304 ab_prev
= list_next(list
, ab
);
4306 ab_prev
= list_prev(list
, ab
);
4308 hash_lock
= HDR_LOCK(ab
);
4309 have_lock
= MUTEX_HELD(hash_lock
);
4310 if (!have_lock
&& !mutex_tryenter(hash_lock
)) {
4312 * Skip this buffer rather than waiting.
4317 passed_sz
+= ab
->b_size
;
4318 if (passed_sz
> headroom
) {
4322 mutex_exit(hash_lock
);
4326 if (!l2arc_write_eligible(guid
, ab
)) {
4327 mutex_exit(hash_lock
);
4331 if ((write_sz
+ ab
->b_size
) > target_sz
) {
4333 mutex_exit(hash_lock
);
4339 * Insert a dummy header on the buflist so
4340 * l2arc_write_done() can find where the
4341 * write buffers begin without searching.
4343 list_insert_head(dev
->l2ad_buflist
, head
);
4346 sizeof (l2arc_write_callback_t
), KM_SLEEP
);
4347 cb
->l2wcb_dev
= dev
;
4348 cb
->l2wcb_head
= head
;
4349 pio
= zio_root(spa
, l2arc_write_done
, cb
,
4354 * Create and add a new L2ARC header.
4356 hdrl2
= kmem_zalloc(sizeof (l2arc_buf_hdr_t
), KM_SLEEP
);
4358 hdrl2
->b_daddr
= dev
->l2ad_hand
;
4360 ab
->b_flags
|= ARC_L2_WRITING
;
4361 ab
->b_l2hdr
= hdrl2
;
4362 list_insert_head(dev
->l2ad_buflist
, ab
);
4363 buf_data
= ab
->b_buf
->b_data
;
4364 buf_sz
= ab
->b_size
;
4367 * Compute and store the buffer cksum before
4368 * writing. On debug the cksum is verified first.
4370 arc_cksum_verify(ab
->b_buf
);
4371 arc_cksum_compute(ab
->b_buf
, B_TRUE
);
4373 mutex_exit(hash_lock
);
4375 wzio
= zio_write_phys(pio
, dev
->l2ad_vdev
,
4376 dev
->l2ad_hand
, buf_sz
, buf_data
, ZIO_CHECKSUM_OFF
,
4377 NULL
, NULL
, ZIO_PRIORITY_ASYNC_WRITE
,
4378 ZIO_FLAG_CANFAIL
, B_FALSE
);
4380 DTRACE_PROBE2(l2arc__write
, vdev_t
*, dev
->l2ad_vdev
,
4382 (void) zio_nowait(wzio
);
4385 * Keep the clock hand suitably device-aligned.
4387 buf_sz
= vdev_psize_to_asize(dev
->l2ad_vdev
, buf_sz
);
4390 dev
->l2ad_hand
+= buf_sz
;
4393 mutex_exit(list_lock
);
4398 mutex_exit(&l2arc_buflist_mtx
);
4401 ASSERT3U(write_sz
, ==, 0);
4402 kmem_cache_free(hdr_cache
, head
);
4406 ASSERT3U(write_sz
, <=, target_sz
);
4407 ARCSTAT_BUMP(arcstat_l2_writes_sent
);
4408 ARCSTAT_INCR(arcstat_l2_write_bytes
, write_sz
);
4409 ARCSTAT_INCR(arcstat_l2_size
, write_sz
);
4410 spa_l2cache_space_update(dev
->l2ad_vdev
, 0, write_sz
);
4413 * Bump device hand to the device start if it is approaching the end.
4414 * l2arc_evict() will already have evicted ahead for this case.
4416 if (dev
->l2ad_hand
>= (dev
->l2ad_end
- target_sz
)) {
4417 spa_l2cache_space_update(dev
->l2ad_vdev
, 0,
4418 dev
->l2ad_end
- dev
->l2ad_hand
);
4419 dev
->l2ad_hand
= dev
->l2ad_start
;
4420 dev
->l2ad_evict
= dev
->l2ad_start
;
4421 dev
->l2ad_first
= B_FALSE
;
4424 dev
->l2ad_writing
= B_TRUE
;
4425 (void) zio_wait(pio
);
4426 dev
->l2ad_writing
= B_FALSE
;
4432 * This thread feeds the L2ARC at regular intervals. This is the beating
4433 * heart of the L2ARC.
4436 l2arc_feed_thread(void)
4441 uint64_t size
, wrote
;
4442 clock_t begin
, next
= lbolt
;
4444 CALLB_CPR_INIT(&cpr
, &l2arc_feed_thr_lock
, callb_generic_cpr
, FTAG
);
4446 mutex_enter(&l2arc_feed_thr_lock
);
4448 while (l2arc_thread_exit
== 0) {
4449 CALLB_CPR_SAFE_BEGIN(&cpr
);
4450 (void) cv_timedwait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
,
4452 CALLB_CPR_SAFE_END(&cpr
, &l2arc_feed_thr_lock
);
4456 * Quick check for L2ARC devices.
4458 mutex_enter(&l2arc_dev_mtx
);
4459 if (l2arc_ndev
== 0) {
4460 mutex_exit(&l2arc_dev_mtx
);
4463 mutex_exit(&l2arc_dev_mtx
);
4467 * This selects the next l2arc device to write to, and in
4468 * doing so the next spa to feed from: dev->l2ad_spa. This
4469 * will return NULL if there are now no l2arc devices or if
4470 * they are all faulted.
4472 * If a device is returned, its spa's config lock is also
4473 * held to prevent device removal. l2arc_dev_get_next()
4474 * will grab and release l2arc_dev_mtx.
4476 if ((dev
= l2arc_dev_get_next()) == NULL
)
4479 spa
= dev
->l2ad_spa
;
4480 ASSERT(spa
!= NULL
);
4483 * Avoid contributing to memory pressure.
4485 if (arc_reclaim_needed()) {
4486 ARCSTAT_BUMP(arcstat_l2_abort_lowmem
);
4487 spa_config_exit(spa
, SCL_L2ARC
, dev
);
4491 ARCSTAT_BUMP(arcstat_l2_feeds
);
4493 size
= l2arc_write_size(dev
);
4496 * Evict L2ARC buffers that will be overwritten.
4498 l2arc_evict(dev
, size
, B_FALSE
);
4501 * Write ARC buffers.
4503 wrote
= l2arc_write_buffers(spa
, dev
, size
);
4506 * Calculate interval between writes.
4508 next
= l2arc_write_interval(begin
, size
, wrote
);
4509 spa_config_exit(spa
, SCL_L2ARC
, dev
);
4512 l2arc_thread_exit
= 0;
4513 cv_broadcast(&l2arc_feed_thr_cv
);
4514 CALLB_CPR_EXIT(&cpr
); /* drops l2arc_feed_thr_lock */
4519 l2arc_vdev_present(vdev_t
*vd
)
4523 mutex_enter(&l2arc_dev_mtx
);
4524 for (dev
= list_head(l2arc_dev_list
); dev
!= NULL
;
4525 dev
= list_next(l2arc_dev_list
, dev
)) {
4526 if (dev
->l2ad_vdev
== vd
)
4529 mutex_exit(&l2arc_dev_mtx
);
4531 return (dev
!= NULL
);
4535 * Add a vdev for use by the L2ARC. By this point the spa has already
4536 * validated the vdev and opened it.
4539 l2arc_add_vdev(spa_t
*spa
, vdev_t
*vd
)
4541 l2arc_dev_t
*adddev
;
4543 ASSERT(!l2arc_vdev_present(vd
));
4546 * Create a new l2arc device entry.
4548 adddev
= kmem_zalloc(sizeof (l2arc_dev_t
), KM_SLEEP
);
4549 adddev
->l2ad_spa
= spa
;
4550 adddev
->l2ad_vdev
= vd
;
4551 adddev
->l2ad_write
= l2arc_write_max
;
4552 adddev
->l2ad_boost
= l2arc_write_boost
;
4553 adddev
->l2ad_start
= VDEV_LABEL_START_SIZE
;
4554 adddev
->l2ad_end
= VDEV_LABEL_START_SIZE
+ vdev_get_min_asize(vd
);
4555 adddev
->l2ad_hand
= adddev
->l2ad_start
;
4556 adddev
->l2ad_evict
= adddev
->l2ad_start
;
4557 adddev
->l2ad_first
= B_TRUE
;
4558 adddev
->l2ad_writing
= B_FALSE
;
4559 ASSERT3U(adddev
->l2ad_write
, >, 0);
4562 * This is a list of all ARC buffers that are still valid on the
4565 adddev
->l2ad_buflist
= kmem_zalloc(sizeof (list_t
), KM_SLEEP
);
4566 list_create(adddev
->l2ad_buflist
, sizeof (arc_buf_hdr_t
),
4567 offsetof(arc_buf_hdr_t
, b_l2node
));
4569 spa_l2cache_space_update(vd
, adddev
->l2ad_end
- adddev
->l2ad_hand
, 0);
4572 * Add device to global list
4574 mutex_enter(&l2arc_dev_mtx
);
4575 list_insert_head(l2arc_dev_list
, adddev
);
4576 atomic_inc_64(&l2arc_ndev
);
4577 mutex_exit(&l2arc_dev_mtx
);
4581 * Remove a vdev from the L2ARC.
4584 l2arc_remove_vdev(vdev_t
*vd
)
4586 l2arc_dev_t
*dev
, *nextdev
, *remdev
= NULL
;
4589 * Find the device by vdev
4591 mutex_enter(&l2arc_dev_mtx
);
4592 for (dev
= list_head(l2arc_dev_list
); dev
; dev
= nextdev
) {
4593 nextdev
= list_next(l2arc_dev_list
, dev
);
4594 if (vd
== dev
->l2ad_vdev
) {
4599 ASSERT(remdev
!= NULL
);
4602 * Remove device from global list
4604 list_remove(l2arc_dev_list
, remdev
);
4605 l2arc_dev_last
= NULL
; /* may have been invalidated */
4606 atomic_dec_64(&l2arc_ndev
);
4607 mutex_exit(&l2arc_dev_mtx
);
4610 * Clear all buflists and ARC references. L2ARC device flush.
4612 l2arc_evict(remdev
, 0, B_TRUE
);
4613 list_destroy(remdev
->l2ad_buflist
);
4614 kmem_free(remdev
->l2ad_buflist
, sizeof (list_t
));
4615 kmem_free(remdev
, sizeof (l2arc_dev_t
));
4621 l2arc_thread_exit
= 0;
4623 l2arc_writes_sent
= 0;
4624 l2arc_writes_done
= 0;
4626 mutex_init(&l2arc_feed_thr_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
4627 cv_init(&l2arc_feed_thr_cv
, NULL
, CV_DEFAULT
, NULL
);
4628 mutex_init(&l2arc_dev_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4629 mutex_init(&l2arc_buflist_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4630 mutex_init(&l2arc_free_on_write_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
4632 l2arc_dev_list
= &L2ARC_dev_list
;
4633 l2arc_free_on_write
= &L2ARC_free_on_write
;
4634 list_create(l2arc_dev_list
, sizeof (l2arc_dev_t
),
4635 offsetof(l2arc_dev_t
, l2ad_node
));
4636 list_create(l2arc_free_on_write
, sizeof (l2arc_data_free_t
),
4637 offsetof(l2arc_data_free_t
, l2df_list_node
));
4644 * This is called from dmu_fini(), which is called from spa_fini();
4645 * Because of this, we can assume that all l2arc devices have
4646 * already been removed when the pools themselves were removed.
4649 l2arc_do_free_on_write();
4651 mutex_destroy(&l2arc_feed_thr_lock
);
4652 cv_destroy(&l2arc_feed_thr_cv
);
4653 mutex_destroy(&l2arc_dev_mtx
);
4654 mutex_destroy(&l2arc_buflist_mtx
);
4655 mutex_destroy(&l2arc_free_on_write_mtx
);
4657 list_destroy(l2arc_dev_list
);
4658 list_destroy(l2arc_free_on_write
);
4664 if (!(spa_mode_global
& FWRITE
))
4667 (void) thread_create(NULL
, 0, l2arc_feed_thread
, NULL
, 0, &p0
,
4668 TS_RUN
, minclsyspri
);
4674 if (!(spa_mode_global
& FWRITE
))
4677 mutex_enter(&l2arc_feed_thr_lock
);
4678 cv_signal(&l2arc_feed_thr_cv
); /* kick thread out of startup */
4679 l2arc_thread_exit
= 1;
4680 while (l2arc_thread_exit
!= 0)
4681 cv_wait(&l2arc_feed_thr_cv
, &l2arc_feed_thr_lock
);
4682 mutex_exit(&l2arc_feed_thr_lock
);