]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/arc.c
Disable zpl_nr_cached_objects() callback
[mirror_zfs.git] / module / zfs / arc.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
36da08ef
PS
23 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2014 by Saso Kiselkov. All rights reserved.
3bec585e 26 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
34dc7c2f
BB
27 */
28
34dc7c2f
BB
29/*
30 * DVA-based Adjustable Replacement Cache
31 *
32 * While much of the theory of operation used here is
33 * based on the self-tuning, low overhead replacement cache
34 * presented by Megiddo and Modha at FAST 2003, there are some
35 * significant differences:
36 *
37 * 1. The Megiddo and Modha model assumes any page is evictable.
38 * Pages in its cache cannot be "locked" into memory. This makes
39 * the eviction algorithm simple: evict the last page in the list.
40 * This also make the performance characteristics easy to reason
41 * about. Our cache is not so simple. At any given moment, some
42 * subset of the blocks in the cache are un-evictable because we
43 * have handed out a reference to them. Blocks are only evictable
44 * when there are no external references active. This makes
45 * eviction far more problematic: we choose to evict the evictable
46 * blocks that are the "lowest" in the list.
47 *
48 * There are times when it is not possible to evict the requested
49 * space. In these circumstances we are unable to adjust the cache
50 * size. To prevent the cache growing unbounded at these times we
51 * implement a "cache throttle" that slows the flow of new data
52 * into the cache until we can make space available.
53 *
54 * 2. The Megiddo and Modha model assumes a fixed cache size.
55 * Pages are evicted when the cache is full and there is a cache
56 * miss. Our model has a variable sized cache. It grows with
57 * high use, but also tries to react to memory pressure from the
58 * operating system: decreasing its size when system memory is
59 * tight.
60 *
61 * 3. The Megiddo and Modha model assumes a fixed page size. All
d3cc8b15 62 * elements of the cache are therefore exactly the same size. So
34dc7c2f
BB
63 * when adjusting the cache size following a cache miss, its simply
64 * a matter of choosing a single page to evict. In our model, we
65 * have variable sized cache blocks (rangeing from 512 bytes to
d3cc8b15 66 * 128K bytes). We therefore choose a set of blocks to evict to make
34dc7c2f
BB
67 * space for a cache miss that approximates as closely as possible
68 * the space used by the new block.
69 *
70 * See also: "ARC: A Self-Tuning, Low Overhead Replacement Cache"
71 * by N. Megiddo & D. Modha, FAST 2003
72 */
73
74/*
75 * The locking model:
76 *
77 * A new reference to a cache buffer can be obtained in two
78 * ways: 1) via a hash table lookup using the DVA as a key,
79 * or 2) via one of the ARC lists. The arc_read() interface
80 * uses method 1, while the internal arc algorithms for
d3cc8b15 81 * adjusting the cache use method 2. We therefore provide two
34dc7c2f
BB
82 * types of locks: 1) the hash table lock array, and 2) the
83 * arc list locks.
84 *
5c839890
BC
85 * Buffers do not have their own mutexes, rather they rely on the
86 * hash table mutexes for the bulk of their protection (i.e. most
87 * fields in the arc_buf_hdr_t are protected by these mutexes).
34dc7c2f
BB
88 *
89 * buf_hash_find() returns the appropriate mutex (held) when it
90 * locates the requested buffer in the hash table. It returns
91 * NULL for the mutex if the buffer was not in the table.
92 *
93 * buf_hash_remove() expects the appropriate hash mutex to be
94 * already held before it is invoked.
95 *
96 * Each arc state also has a mutex which is used to protect the
97 * buffer list associated with the state. When attempting to
98 * obtain a hash table lock while holding an arc list lock you
99 * must use: mutex_tryenter() to avoid deadlock. Also note that
100 * the active state mutex must be held before the ghost state mutex.
101 *
102 * Arc buffers may have an associated eviction callback function.
103 * This function will be invoked prior to removing the buffer (e.g.
104 * in arc_do_user_evicts()). Note however that the data associated
105 * with the buffer may be evicted prior to the callback. The callback
106 * must be made with *no locks held* (to prevent deadlock). Additionally,
107 * the users of callbacks must ensure that their private data is
bd089c54 108 * protected from simultaneous callbacks from arc_clear_callback()
34dc7c2f
BB
109 * and arc_do_user_evicts().
110 *
ab26409d
BB
111 * It as also possible to register a callback which is run when the
112 * arc_meta_limit is reached and no buffers can be safely evicted. In
113 * this case the arc user should drop a reference on some arc buffers so
114 * they can be reclaimed and the arc_meta_limit honored. For example,
115 * when using the ZPL each dentry holds a references on a znode. These
116 * dentries must be pruned before the arc buffer holding the znode can
117 * be safely evicted.
118 *
34dc7c2f
BB
119 * Note that the majority of the performance stats are manipulated
120 * with atomic operations.
121 *
b9541d6b 122 * The L2ARC uses the l2ad_mtx on each vdev for the following:
34dc7c2f
BB
123 *
124 * - L2ARC buflist creation
125 * - L2ARC buflist eviction
126 * - L2ARC write completion, which walks L2ARC buflists
127 * - ARC header destruction, as it removes from L2ARC buflists
128 * - ARC header release, as it removes from L2ARC buflists
129 */
130
131#include <sys/spa.h>
132#include <sys/zio.h>
3a17a7a9 133#include <sys/zio_compress.h>
34dc7c2f
BB
134#include <sys/zfs_context.h>
135#include <sys/arc.h>
36da08ef 136#include <sys/refcount.h>
b128c09f 137#include <sys/vdev.h>
9babb374 138#include <sys/vdev_impl.h>
e8b96c60 139#include <sys/dsl_pool.h>
ca0bf58d 140#include <sys/multilist.h>
34dc7c2f
BB
141#ifdef _KERNEL
142#include <sys/vmsystm.h>
143#include <vm/anon.h>
144#include <sys/fs/swapnode.h>
ab26409d 145#include <sys/zpl.h>
aaed7c40 146#include <linux/mm_compat.h>
34dc7c2f
BB
147#endif
148#include <sys/callb.h>
149#include <sys/kstat.h>
570827e1 150#include <sys/dmu_tx.h>
428870ff 151#include <zfs_fletcher.h>
59ec819a 152#include <sys/arc_impl.h>
49ee64e5 153#include <sys/trace_arc.h>
34dc7c2f 154
498877ba
MA
155#ifndef _KERNEL
156/* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
157boolean_t arc_watch = B_FALSE;
158#endif
159
ca0bf58d
PS
160static kmutex_t arc_reclaim_lock;
161static kcondvar_t arc_reclaim_thread_cv;
162static boolean_t arc_reclaim_thread_exit;
163static kcondvar_t arc_reclaim_waiters_cv;
164
165static kmutex_t arc_user_evicts_lock;
166static kcondvar_t arc_user_evicts_cv;
167static boolean_t arc_user_evicts_thread_exit;
34dc7c2f 168
e8b96c60 169/*
ca0bf58d
PS
170 * The number of headers to evict in arc_evict_state_impl() before
171 * dropping the sublist lock and evicting from another sublist. A lower
172 * value means we're more likely to evict the "correct" header (i.e. the
173 * oldest header in the arc state), but comes with higher overhead
174 * (i.e. more invocations of arc_evict_state_impl()).
175 */
176int zfs_arc_evict_batch_limit = 10;
177
178/*
179 * The number of sublists used for each of the arc state lists. If this
180 * is not set to a suitable value by the user, it will be configured to
181 * the number of CPUs on the system in arc_init().
e8b96c60 182 */
ca0bf58d 183int zfs_arc_num_sublists_per_state = 0;
e8b96c60 184
34dc7c2f 185/* number of seconds before growing cache again */
ca67b33a 186static int arc_grow_retry = 5;
34dc7c2f 187
ca0bf58d 188/* shift of arc_c for calculating overflow limit in arc_get_data_buf */
ca67b33a 189int zfs_arc_overflow_shift = 8;
62422785 190
728d6ae9
BB
191/* shift of arc_c for calculating both min and max arc_p */
192static int arc_p_min_shift = 4;
193
d164b209 194/* log2(fraction of arc to reclaim) */
ca67b33a 195static int arc_shrink_shift = 7;
d164b209 196
34dc7c2f 197/*
ca67b33a
MA
198 * log2(fraction of ARC which must be free to allow growing).
199 * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
200 * when reading a new block into the ARC, we will evict an equal-sized block
201 * from the ARC.
202 *
203 * This must be less than arc_shrink_shift, so that when we shrink the ARC,
204 * we will still not allow it to grow.
34dc7c2f 205 */
ca67b33a 206int arc_no_grow_shift = 5;
bce45ec9 207
49ddb315 208
ca0bf58d
PS
209/*
210 * minimum lifespan of a prefetch block in clock ticks
211 * (initialized in arc_init())
212 */
ca67b33a 213static int arc_min_prefetch_lifespan;
ca0bf58d 214
e8b96c60
MA
215/*
216 * If this percent of memory is free, don't throttle.
217 */
218int arc_lotsfree_percent = 10;
219
34dc7c2f
BB
220static int arc_dead;
221
b128c09f
BB
222/*
223 * The arc has filled available memory and has now warmed up.
224 */
225static boolean_t arc_warm;
226
34dc7c2f
BB
227/*
228 * These tunables are for performance analysis.
229 */
c28b2279
BB
230unsigned long zfs_arc_max = 0;
231unsigned long zfs_arc_min = 0;
232unsigned long zfs_arc_meta_limit = 0;
ca0bf58d 233unsigned long zfs_arc_meta_min = 0;
ca67b33a
MA
234int zfs_arc_grow_retry = 0;
235int zfs_arc_shrink_shift = 0;
728d6ae9 236int zfs_arc_p_min_shift = 0;
ca67b33a
MA
237int zfs_disable_dup_eviction = 0;
238int zfs_arc_average_blocksize = 8 * 1024; /* 8KB */
34dc7c2f 239
bc888666 240/*
ca67b33a 241 * These tunables are Linux specific
bc888666 242 */
11f552fa 243unsigned long zfs_arc_sys_free = 0;
ca67b33a
MA
244int zfs_arc_min_prefetch_lifespan = 0;
245int zfs_arc_p_aggressive_disable = 1;
246int zfs_arc_p_dampener_disable = 1;
247int zfs_arc_meta_prune = 10000;
248int zfs_arc_meta_strategy = ARC_STRATEGY_META_BALANCED;
249int zfs_arc_meta_adjust_restarts = 4096;
7e8bddd0 250int zfs_arc_lotsfree_percent = 10;
bc888666 251
34dc7c2f
BB
252/* The 6 states: */
253static arc_state_t ARC_anon;
254static arc_state_t ARC_mru;
255static arc_state_t ARC_mru_ghost;
256static arc_state_t ARC_mfu;
257static arc_state_t ARC_mfu_ghost;
258static arc_state_t ARC_l2c_only;
259
260typedef struct arc_stats {
261 kstat_named_t arcstat_hits;
262 kstat_named_t arcstat_misses;
263 kstat_named_t arcstat_demand_data_hits;
264 kstat_named_t arcstat_demand_data_misses;
265 kstat_named_t arcstat_demand_metadata_hits;
266 kstat_named_t arcstat_demand_metadata_misses;
267 kstat_named_t arcstat_prefetch_data_hits;
268 kstat_named_t arcstat_prefetch_data_misses;
269 kstat_named_t arcstat_prefetch_metadata_hits;
270 kstat_named_t arcstat_prefetch_metadata_misses;
271 kstat_named_t arcstat_mru_hits;
272 kstat_named_t arcstat_mru_ghost_hits;
273 kstat_named_t arcstat_mfu_hits;
274 kstat_named_t arcstat_mfu_ghost_hits;
275 kstat_named_t arcstat_deleted;
e49f1e20
WA
276 /*
277 * Number of buffers that could not be evicted because the hash lock
278 * was held by another thread. The lock may not necessarily be held
279 * by something using the same buffer, since hash locks are shared
280 * by multiple buffers.
281 */
34dc7c2f 282 kstat_named_t arcstat_mutex_miss;
e49f1e20
WA
283 /*
284 * Number of buffers skipped because they have I/O in progress, are
285 * indrect prefetch buffers that have not lived long enough, or are
286 * not from the spa we're trying to evict from.
287 */
34dc7c2f 288 kstat_named_t arcstat_evict_skip;
ca0bf58d
PS
289 /*
290 * Number of times arc_evict_state() was unable to evict enough
291 * buffers to reach its target amount.
292 */
293 kstat_named_t arcstat_evict_not_enough;
428870ff
BB
294 kstat_named_t arcstat_evict_l2_cached;
295 kstat_named_t arcstat_evict_l2_eligible;
296 kstat_named_t arcstat_evict_l2_ineligible;
ca0bf58d 297 kstat_named_t arcstat_evict_l2_skip;
34dc7c2f
BB
298 kstat_named_t arcstat_hash_elements;
299 kstat_named_t arcstat_hash_elements_max;
300 kstat_named_t arcstat_hash_collisions;
301 kstat_named_t arcstat_hash_chains;
302 kstat_named_t arcstat_hash_chain_max;
303 kstat_named_t arcstat_p;
304 kstat_named_t arcstat_c;
305 kstat_named_t arcstat_c_min;
306 kstat_named_t arcstat_c_max;
307 kstat_named_t arcstat_size;
500445c0
PS
308 /*
309 * Number of bytes consumed by internal ARC structures necessary
310 * for tracking purposes; these structures are not actually
311 * backed by ARC buffers. This includes arc_buf_hdr_t structures
312 * (allocated via arc_buf_hdr_t_full and arc_buf_hdr_t_l2only
313 * caches), and arc_buf_t structures (allocated via arc_buf_t
314 * cache).
315 */
34dc7c2f 316 kstat_named_t arcstat_hdr_size;
500445c0
PS
317 /*
318 * Number of bytes consumed by ARC buffers of type equal to
319 * ARC_BUFC_DATA. This is generally consumed by buffers backing
320 * on disk user data (e.g. plain file contents).
321 */
d164b209 322 kstat_named_t arcstat_data_size;
500445c0
PS
323 /*
324 * Number of bytes consumed by ARC buffers of type equal to
325 * ARC_BUFC_METADATA. This is generally consumed by buffers
326 * backing on disk data that is used for internal ZFS
327 * structures (e.g. ZAP, dnode, indirect blocks, etc).
328 */
329 kstat_named_t arcstat_metadata_size;
330 /*
331 * Number of bytes consumed by various buffers and structures
332 * not actually backed with ARC buffers. This includes bonus
333 * buffers (allocated directly via zio_buf_* functions),
334 * dmu_buf_impl_t structures (allocated via dmu_buf_impl_t
335 * cache), and dnode_t structures (allocated via dnode_t cache).
336 */
d164b209 337 kstat_named_t arcstat_other_size;
500445c0
PS
338 /*
339 * Total number of bytes consumed by ARC buffers residing in the
340 * arc_anon state. This includes *all* buffers in the arc_anon
341 * state; e.g. data, metadata, evictable, and unevictable buffers
342 * are all included in this value.
343 */
13be560d 344 kstat_named_t arcstat_anon_size;
500445c0
PS
345 /*
346 * Number of bytes consumed by ARC buffers that meet the
347 * following criteria: backing buffers of type ARC_BUFC_DATA,
348 * residing in the arc_anon state, and are eligible for eviction
349 * (e.g. have no outstanding holds on the buffer).
350 */
351 kstat_named_t arcstat_anon_evictable_data;
352 /*
353 * Number of bytes consumed by ARC buffers that meet the
354 * following criteria: backing buffers of type ARC_BUFC_METADATA,
355 * residing in the arc_anon state, and are eligible for eviction
356 * (e.g. have no outstanding holds on the buffer).
357 */
358 kstat_named_t arcstat_anon_evictable_metadata;
359 /*
360 * Total number of bytes consumed by ARC buffers residing in the
361 * arc_mru state. This includes *all* buffers in the arc_mru
362 * state; e.g. data, metadata, evictable, and unevictable buffers
363 * are all included in this value.
364 */
13be560d 365 kstat_named_t arcstat_mru_size;
500445c0
PS
366 /*
367 * Number of bytes consumed by ARC buffers that meet the
368 * following criteria: backing buffers of type ARC_BUFC_DATA,
369 * residing in the arc_mru state, and are eligible for eviction
370 * (e.g. have no outstanding holds on the buffer).
371 */
372 kstat_named_t arcstat_mru_evictable_data;
373 /*
374 * Number of bytes consumed by ARC buffers that meet the
375 * following criteria: backing buffers of type ARC_BUFC_METADATA,
376 * residing in the arc_mru state, and are eligible for eviction
377 * (e.g. have no outstanding holds on the buffer).
378 */
379 kstat_named_t arcstat_mru_evictable_metadata;
380 /*
381 * Total number of bytes that *would have been* consumed by ARC
382 * buffers in the arc_mru_ghost state. The key thing to note
383 * here, is the fact that this size doesn't actually indicate
384 * RAM consumption. The ghost lists only consist of headers and
385 * don't actually have ARC buffers linked off of these headers.
386 * Thus, *if* the headers had associated ARC buffers, these
387 * buffers *would have* consumed this number of bytes.
388 */
13be560d 389 kstat_named_t arcstat_mru_ghost_size;
500445c0
PS
390 /*
391 * Number of bytes that *would have been* consumed by ARC
392 * buffers that are eligible for eviction, of type
393 * ARC_BUFC_DATA, and linked off the arc_mru_ghost state.
394 */
395 kstat_named_t arcstat_mru_ghost_evictable_data;
396 /*
397 * Number of bytes that *would have been* consumed by ARC
398 * buffers that are eligible for eviction, of type
399 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
400 */
401 kstat_named_t arcstat_mru_ghost_evictable_metadata;
402 /*
403 * Total number of bytes consumed by ARC buffers residing in the
404 * arc_mfu state. This includes *all* buffers in the arc_mfu
405 * state; e.g. data, metadata, evictable, and unevictable buffers
406 * are all included in this value.
407 */
13be560d 408 kstat_named_t arcstat_mfu_size;
500445c0
PS
409 /*
410 * Number of bytes consumed by ARC buffers that are eligible for
411 * eviction, of type ARC_BUFC_DATA, and reside in the arc_mfu
412 * state.
413 */
414 kstat_named_t arcstat_mfu_evictable_data;
415 /*
416 * Number of bytes consumed by ARC buffers that are eligible for
417 * eviction, of type ARC_BUFC_METADATA, and reside in the
418 * arc_mfu state.
419 */
420 kstat_named_t arcstat_mfu_evictable_metadata;
421 /*
422 * Total number of bytes that *would have been* consumed by ARC
423 * buffers in the arc_mfu_ghost state. See the comment above
424 * arcstat_mru_ghost_size for more details.
425 */
13be560d 426 kstat_named_t arcstat_mfu_ghost_size;
500445c0
PS
427 /*
428 * Number of bytes that *would have been* consumed by ARC
429 * buffers that are eligible for eviction, of type
430 * ARC_BUFC_DATA, and linked off the arc_mfu_ghost state.
431 */
432 kstat_named_t arcstat_mfu_ghost_evictable_data;
433 /*
434 * Number of bytes that *would have been* consumed by ARC
435 * buffers that are eligible for eviction, of type
436 * ARC_BUFC_METADATA, and linked off the arc_mru_ghost state.
437 */
438 kstat_named_t arcstat_mfu_ghost_evictable_metadata;
34dc7c2f
BB
439 kstat_named_t arcstat_l2_hits;
440 kstat_named_t arcstat_l2_misses;
441 kstat_named_t arcstat_l2_feeds;
442 kstat_named_t arcstat_l2_rw_clash;
d164b209
BB
443 kstat_named_t arcstat_l2_read_bytes;
444 kstat_named_t arcstat_l2_write_bytes;
34dc7c2f
BB
445 kstat_named_t arcstat_l2_writes_sent;
446 kstat_named_t arcstat_l2_writes_done;
447 kstat_named_t arcstat_l2_writes_error;
ca0bf58d 448 kstat_named_t arcstat_l2_writes_lock_retry;
34dc7c2f
BB
449 kstat_named_t arcstat_l2_evict_lock_retry;
450 kstat_named_t arcstat_l2_evict_reading;
b9541d6b 451 kstat_named_t arcstat_l2_evict_l1cached;
34dc7c2f 452 kstat_named_t arcstat_l2_free_on_write;
ca0bf58d 453 kstat_named_t arcstat_l2_cdata_free_on_write;
34dc7c2f
BB
454 kstat_named_t arcstat_l2_abort_lowmem;
455 kstat_named_t arcstat_l2_cksum_bad;
456 kstat_named_t arcstat_l2_io_error;
457 kstat_named_t arcstat_l2_size;
3a17a7a9 458 kstat_named_t arcstat_l2_asize;
34dc7c2f 459 kstat_named_t arcstat_l2_hdr_size;
3a17a7a9
SK
460 kstat_named_t arcstat_l2_compress_successes;
461 kstat_named_t arcstat_l2_compress_zeros;
462 kstat_named_t arcstat_l2_compress_failures;
34dc7c2f 463 kstat_named_t arcstat_memory_throttle_count;
1eb5bfa3
GW
464 kstat_named_t arcstat_duplicate_buffers;
465 kstat_named_t arcstat_duplicate_buffers_size;
466 kstat_named_t arcstat_duplicate_reads;
7cb67b45
BB
467 kstat_named_t arcstat_memory_direct_count;
468 kstat_named_t arcstat_memory_indirect_count;
1834f2d8
BB
469 kstat_named_t arcstat_no_grow;
470 kstat_named_t arcstat_tempreserve;
471 kstat_named_t arcstat_loaned_bytes;
ab26409d 472 kstat_named_t arcstat_prune;
1834f2d8
BB
473 kstat_named_t arcstat_meta_used;
474 kstat_named_t arcstat_meta_limit;
475 kstat_named_t arcstat_meta_max;
ca0bf58d 476 kstat_named_t arcstat_meta_min;
11f552fa
BB
477 kstat_named_t arcstat_need_free;
478 kstat_named_t arcstat_sys_free;
34dc7c2f
BB
479} arc_stats_t;
480
481static arc_stats_t arc_stats = {
482 { "hits", KSTAT_DATA_UINT64 },
483 { "misses", KSTAT_DATA_UINT64 },
484 { "demand_data_hits", KSTAT_DATA_UINT64 },
485 { "demand_data_misses", KSTAT_DATA_UINT64 },
486 { "demand_metadata_hits", KSTAT_DATA_UINT64 },
487 { "demand_metadata_misses", KSTAT_DATA_UINT64 },
488 { "prefetch_data_hits", KSTAT_DATA_UINT64 },
489 { "prefetch_data_misses", KSTAT_DATA_UINT64 },
490 { "prefetch_metadata_hits", KSTAT_DATA_UINT64 },
491 { "prefetch_metadata_misses", KSTAT_DATA_UINT64 },
492 { "mru_hits", KSTAT_DATA_UINT64 },
493 { "mru_ghost_hits", KSTAT_DATA_UINT64 },
494 { "mfu_hits", KSTAT_DATA_UINT64 },
495 { "mfu_ghost_hits", KSTAT_DATA_UINT64 },
496 { "deleted", KSTAT_DATA_UINT64 },
34dc7c2f
BB
497 { "mutex_miss", KSTAT_DATA_UINT64 },
498 { "evict_skip", KSTAT_DATA_UINT64 },
ca0bf58d 499 { "evict_not_enough", KSTAT_DATA_UINT64 },
428870ff
BB
500 { "evict_l2_cached", KSTAT_DATA_UINT64 },
501 { "evict_l2_eligible", KSTAT_DATA_UINT64 },
502 { "evict_l2_ineligible", KSTAT_DATA_UINT64 },
ca0bf58d 503 { "evict_l2_skip", KSTAT_DATA_UINT64 },
34dc7c2f
BB
504 { "hash_elements", KSTAT_DATA_UINT64 },
505 { "hash_elements_max", KSTAT_DATA_UINT64 },
506 { "hash_collisions", KSTAT_DATA_UINT64 },
507 { "hash_chains", KSTAT_DATA_UINT64 },
508 { "hash_chain_max", KSTAT_DATA_UINT64 },
509 { "p", KSTAT_DATA_UINT64 },
510 { "c", KSTAT_DATA_UINT64 },
511 { "c_min", KSTAT_DATA_UINT64 },
512 { "c_max", KSTAT_DATA_UINT64 },
513 { "size", KSTAT_DATA_UINT64 },
514 { "hdr_size", KSTAT_DATA_UINT64 },
d164b209 515 { "data_size", KSTAT_DATA_UINT64 },
500445c0 516 { "metadata_size", KSTAT_DATA_UINT64 },
d164b209 517 { "other_size", KSTAT_DATA_UINT64 },
13be560d 518 { "anon_size", KSTAT_DATA_UINT64 },
500445c0
PS
519 { "anon_evictable_data", KSTAT_DATA_UINT64 },
520 { "anon_evictable_metadata", KSTAT_DATA_UINT64 },
13be560d 521 { "mru_size", KSTAT_DATA_UINT64 },
500445c0
PS
522 { "mru_evictable_data", KSTAT_DATA_UINT64 },
523 { "mru_evictable_metadata", KSTAT_DATA_UINT64 },
13be560d 524 { "mru_ghost_size", KSTAT_DATA_UINT64 },
500445c0
PS
525 { "mru_ghost_evictable_data", KSTAT_DATA_UINT64 },
526 { "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 },
13be560d 527 { "mfu_size", KSTAT_DATA_UINT64 },
500445c0
PS
528 { "mfu_evictable_data", KSTAT_DATA_UINT64 },
529 { "mfu_evictable_metadata", KSTAT_DATA_UINT64 },
13be560d 530 { "mfu_ghost_size", KSTAT_DATA_UINT64 },
500445c0
PS
531 { "mfu_ghost_evictable_data", KSTAT_DATA_UINT64 },
532 { "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 },
34dc7c2f
BB
533 { "l2_hits", KSTAT_DATA_UINT64 },
534 { "l2_misses", KSTAT_DATA_UINT64 },
535 { "l2_feeds", KSTAT_DATA_UINT64 },
536 { "l2_rw_clash", KSTAT_DATA_UINT64 },
d164b209
BB
537 { "l2_read_bytes", KSTAT_DATA_UINT64 },
538 { "l2_write_bytes", KSTAT_DATA_UINT64 },
34dc7c2f
BB
539 { "l2_writes_sent", KSTAT_DATA_UINT64 },
540 { "l2_writes_done", KSTAT_DATA_UINT64 },
541 { "l2_writes_error", KSTAT_DATA_UINT64 },
ca0bf58d 542 { "l2_writes_lock_retry", KSTAT_DATA_UINT64 },
34dc7c2f
BB
543 { "l2_evict_lock_retry", KSTAT_DATA_UINT64 },
544 { "l2_evict_reading", KSTAT_DATA_UINT64 },
b9541d6b 545 { "l2_evict_l1cached", KSTAT_DATA_UINT64 },
34dc7c2f 546 { "l2_free_on_write", KSTAT_DATA_UINT64 },
ca0bf58d 547 { "l2_cdata_free_on_write", KSTAT_DATA_UINT64 },
34dc7c2f
BB
548 { "l2_abort_lowmem", KSTAT_DATA_UINT64 },
549 { "l2_cksum_bad", KSTAT_DATA_UINT64 },
550 { "l2_io_error", KSTAT_DATA_UINT64 },
551 { "l2_size", KSTAT_DATA_UINT64 },
3a17a7a9 552 { "l2_asize", KSTAT_DATA_UINT64 },
34dc7c2f 553 { "l2_hdr_size", KSTAT_DATA_UINT64 },
3a17a7a9
SK
554 { "l2_compress_successes", KSTAT_DATA_UINT64 },
555 { "l2_compress_zeros", KSTAT_DATA_UINT64 },
556 { "l2_compress_failures", KSTAT_DATA_UINT64 },
1834f2d8 557 { "memory_throttle_count", KSTAT_DATA_UINT64 },
1eb5bfa3
GW
558 { "duplicate_buffers", KSTAT_DATA_UINT64 },
559 { "duplicate_buffers_size", KSTAT_DATA_UINT64 },
560 { "duplicate_reads", KSTAT_DATA_UINT64 },
7cb67b45
BB
561 { "memory_direct_count", KSTAT_DATA_UINT64 },
562 { "memory_indirect_count", KSTAT_DATA_UINT64 },
1834f2d8
BB
563 { "arc_no_grow", KSTAT_DATA_UINT64 },
564 { "arc_tempreserve", KSTAT_DATA_UINT64 },
565 { "arc_loaned_bytes", KSTAT_DATA_UINT64 },
ab26409d 566 { "arc_prune", KSTAT_DATA_UINT64 },
1834f2d8
BB
567 { "arc_meta_used", KSTAT_DATA_UINT64 },
568 { "arc_meta_limit", KSTAT_DATA_UINT64 },
569 { "arc_meta_max", KSTAT_DATA_UINT64 },
11f552fa
BB
570 { "arc_meta_min", KSTAT_DATA_UINT64 },
571 { "arc_need_free", KSTAT_DATA_UINT64 },
572 { "arc_sys_free", KSTAT_DATA_UINT64 }
34dc7c2f
BB
573};
574
575#define ARCSTAT(stat) (arc_stats.stat.value.ui64)
576
577#define ARCSTAT_INCR(stat, val) \
d3cc8b15 578 atomic_add_64(&arc_stats.stat.value.ui64, (val))
34dc7c2f 579
428870ff 580#define ARCSTAT_BUMP(stat) ARCSTAT_INCR(stat, 1)
34dc7c2f
BB
581#define ARCSTAT_BUMPDOWN(stat) ARCSTAT_INCR(stat, -1)
582
583#define ARCSTAT_MAX(stat, val) { \
584 uint64_t m; \
585 while ((val) > (m = arc_stats.stat.value.ui64) && \
586 (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val)))) \
587 continue; \
588}
589
590#define ARCSTAT_MAXSTAT(stat) \
591 ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
592
593/*
594 * We define a macro to allow ARC hits/misses to be easily broken down by
595 * two separate conditions, giving a total of four different subtypes for
596 * each of hits and misses (so eight statistics total).
597 */
598#define ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
599 if (cond1) { \
600 if (cond2) { \
601 ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
602 } else { \
603 ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
604 } \
605 } else { \
606 if (cond2) { \
607 ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
608 } else { \
609 ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
610 } \
611 }
612
613kstat_t *arc_ksp;
428870ff 614static arc_state_t *arc_anon;
34dc7c2f
BB
615static arc_state_t *arc_mru;
616static arc_state_t *arc_mru_ghost;
617static arc_state_t *arc_mfu;
618static arc_state_t *arc_mfu_ghost;
619static arc_state_t *arc_l2c_only;
620
621/*
622 * There are several ARC variables that are critical to export as kstats --
623 * but we don't want to have to grovel around in the kstat whenever we wish to
624 * manipulate them. For these variables, we therefore define them to be in
625 * terms of the statistic variable. This assures that we are not introducing
626 * the possibility of inconsistency by having shadow copies of the variables,
627 * while still allowing the code to be readable.
628 */
629#define arc_size ARCSTAT(arcstat_size) /* actual total arc size */
630#define arc_p ARCSTAT(arcstat_p) /* target size of MRU */
631#define arc_c ARCSTAT(arcstat_c) /* target size of cache */
632#define arc_c_min ARCSTAT(arcstat_c_min) /* min target cache size */
633#define arc_c_max ARCSTAT(arcstat_c_max) /* max target cache size */
1834f2d8
BB
634#define arc_no_grow ARCSTAT(arcstat_no_grow)
635#define arc_tempreserve ARCSTAT(arcstat_tempreserve)
636#define arc_loaned_bytes ARCSTAT(arcstat_loaned_bytes)
23c0a133 637#define arc_meta_limit ARCSTAT(arcstat_meta_limit) /* max size for metadata */
ca0bf58d 638#define arc_meta_min ARCSTAT(arcstat_meta_min) /* min size for metadata */
23c0a133
GW
639#define arc_meta_used ARCSTAT(arcstat_meta_used) /* size of metadata */
640#define arc_meta_max ARCSTAT(arcstat_meta_max) /* max size of metadata */
11f552fa
BB
641#define arc_need_free ARCSTAT(arcstat_need_free) /* bytes to be freed */
642#define arc_sys_free ARCSTAT(arcstat_sys_free) /* target system free bytes */
34dc7c2f 643
3a17a7a9
SK
644#define L2ARC_IS_VALID_COMPRESS(_c_) \
645 ((_c_) == ZIO_COMPRESS_LZ4 || (_c_) == ZIO_COMPRESS_EMPTY)
646
ab26409d
BB
647static list_t arc_prune_list;
648static kmutex_t arc_prune_mtx;
f6046738 649static taskq_t *arc_prune_taskq;
34dc7c2f 650static arc_buf_t *arc_eviction_list;
34dc7c2f 651static arc_buf_hdr_t arc_eviction_hdr;
428870ff 652
34dc7c2f
BB
653#define GHOST_STATE(state) \
654 ((state) == arc_mru_ghost || (state) == arc_mfu_ghost || \
655 (state) == arc_l2c_only)
656
2a432414
GW
657#define HDR_IN_HASH_TABLE(hdr) ((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
658#define HDR_IO_IN_PROGRESS(hdr) ((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
659#define HDR_IO_ERROR(hdr) ((hdr)->b_flags & ARC_FLAG_IO_ERROR)
660#define HDR_PREFETCH(hdr) ((hdr)->b_flags & ARC_FLAG_PREFETCH)
661#define HDR_FREED_IN_READ(hdr) ((hdr)->b_flags & ARC_FLAG_FREED_IN_READ)
662#define HDR_BUF_AVAILABLE(hdr) ((hdr)->b_flags & ARC_FLAG_BUF_AVAILABLE)
b9541d6b 663
2a432414 664#define HDR_L2CACHE(hdr) ((hdr)->b_flags & ARC_FLAG_L2CACHE)
b9541d6b 665#define HDR_L2COMPRESS(hdr) ((hdr)->b_flags & ARC_FLAG_L2COMPRESS)
2a432414 666#define HDR_L2_READING(hdr) \
b9541d6b
CW
667 (((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) && \
668 ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
2a432414
GW
669#define HDR_L2_WRITING(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITING)
670#define HDR_L2_EVICTED(hdr) ((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
671#define HDR_L2_WRITE_HEAD(hdr) ((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
34dc7c2f 672
b9541d6b
CW
673#define HDR_ISTYPE_METADATA(hdr) \
674 ((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
675#define HDR_ISTYPE_DATA(hdr) (!HDR_ISTYPE_METADATA(hdr))
676
677#define HDR_HAS_L1HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
678#define HDR_HAS_L2HDR(hdr) ((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
679
34dc7c2f
BB
680/*
681 * Other sizes
682 */
683
b9541d6b
CW
684#define HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
685#define HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
34dc7c2f
BB
686
687/*
688 * Hash table routines
689 */
690
00b46022
BB
691#define HT_LOCK_ALIGN 64
692#define HT_LOCK_PAD (P2NPHASE(sizeof (kmutex_t), (HT_LOCK_ALIGN)))
34dc7c2f
BB
693
694struct ht_lock {
695 kmutex_t ht_lock;
696#ifdef _KERNEL
00b46022 697 unsigned char pad[HT_LOCK_PAD];
34dc7c2f
BB
698#endif
699};
700
b31d8ea7 701#define BUF_LOCKS 8192
34dc7c2f
BB
702typedef struct buf_hash_table {
703 uint64_t ht_mask;
704 arc_buf_hdr_t **ht_table;
705 struct ht_lock ht_locks[BUF_LOCKS];
706} buf_hash_table_t;
707
708static buf_hash_table_t buf_hash_table;
709
710#define BUF_HASH_INDEX(spa, dva, birth) \
711 (buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
712#define BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
713#define BUF_HASH_LOCK(idx) (&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
428870ff
BB
714#define HDR_LOCK(hdr) \
715 (BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
34dc7c2f
BB
716
717uint64_t zfs_crc64_table[256];
718
719/*
720 * Level 2 ARC
721 */
722
723#define L2ARC_WRITE_SIZE (8 * 1024 * 1024) /* initial write max */
3a17a7a9
SK
724#define L2ARC_HEADROOM 2 /* num of writes */
725/*
726 * If we discover during ARC scan any buffers to be compressed, we boost
727 * our headroom for the next scanning cycle by this percentage multiple.
728 */
729#define L2ARC_HEADROOM_BOOST 200
d164b209
BB
730#define L2ARC_FEED_SECS 1 /* caching interval secs */
731#define L2ARC_FEED_MIN_MS 200 /* min caching interval ms */
34dc7c2f 732
d962d5da
PS
733/*
734 * Used to distinguish headers that are being process by
735 * l2arc_write_buffers(), but have yet to be assigned to a l2arc disk
736 * address. This can happen when the header is added to the l2arc's list
737 * of buffers to write in the first stage of l2arc_write_buffers(), but
738 * has not yet been written out which happens in the second stage of
739 * l2arc_write_buffers().
740 */
741#define L2ARC_ADDR_UNSET ((uint64_t)(-1))
742
34dc7c2f
BB
743#define l2arc_writes_sent ARCSTAT(arcstat_l2_writes_sent)
744#define l2arc_writes_done ARCSTAT(arcstat_l2_writes_done)
745
d3cc8b15 746/* L2ARC Performance Tunables */
abd8610c
BB
747unsigned long l2arc_write_max = L2ARC_WRITE_SIZE; /* def max write size */
748unsigned long l2arc_write_boost = L2ARC_WRITE_SIZE; /* extra warmup write */
749unsigned long l2arc_headroom = L2ARC_HEADROOM; /* # of dev writes */
3a17a7a9 750unsigned long l2arc_headroom_boost = L2ARC_HEADROOM_BOOST;
abd8610c
BB
751unsigned long l2arc_feed_secs = L2ARC_FEED_SECS; /* interval seconds */
752unsigned long l2arc_feed_min_ms = L2ARC_FEED_MIN_MS; /* min interval msecs */
753int l2arc_noprefetch = B_TRUE; /* don't cache prefetch bufs */
3a17a7a9 754int l2arc_nocompress = B_FALSE; /* don't compress bufs */
abd8610c 755int l2arc_feed_again = B_TRUE; /* turbo warmup */
c93504f0 756int l2arc_norw = B_FALSE; /* no reads during writes */
34dc7c2f
BB
757
758/*
759 * L2ARC Internals
760 */
34dc7c2f
BB
761static list_t L2ARC_dev_list; /* device list */
762static list_t *l2arc_dev_list; /* device list pointer */
763static kmutex_t l2arc_dev_mtx; /* device list mutex */
764static l2arc_dev_t *l2arc_dev_last; /* last device used */
34dc7c2f
BB
765static list_t L2ARC_free_on_write; /* free after write buf list */
766static list_t *l2arc_free_on_write; /* free after write list ptr */
767static kmutex_t l2arc_free_on_write_mtx; /* mutex for list */
768static uint64_t l2arc_ndev; /* number of devices */
769
770typedef struct l2arc_read_callback {
3a17a7a9
SK
771 arc_buf_t *l2rcb_buf; /* read buffer */
772 spa_t *l2rcb_spa; /* spa */
773 blkptr_t l2rcb_bp; /* original blkptr */
5dbd68a3 774 zbookmark_phys_t l2rcb_zb; /* original bookmark */
3a17a7a9
SK
775 int l2rcb_flags; /* original flags */
776 enum zio_compress l2rcb_compress; /* applied compress */
34dc7c2f
BB
777} l2arc_read_callback_t;
778
34dc7c2f
BB
779typedef struct l2arc_data_free {
780 /* protected by l2arc_free_on_write_mtx */
781 void *l2df_data;
782 size_t l2df_size;
783 void (*l2df_func)(void *, size_t);
784 list_node_t l2df_list_node;
785} l2arc_data_free_t;
786
787static kmutex_t l2arc_feed_thr_lock;
788static kcondvar_t l2arc_feed_thr_cv;
789static uint8_t l2arc_thread_exit;
790
2a432414
GW
791static void arc_get_data_buf(arc_buf_t *);
792static void arc_access(arc_buf_hdr_t *, kmutex_t *);
ca0bf58d 793static boolean_t arc_is_overflowing(void);
2a432414 794static void arc_buf_watch(arc_buf_t *);
ca67b33a 795static void arc_tuning_update(void);
2a432414 796
b9541d6b
CW
797static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *);
798static uint32_t arc_bufc_to_flags(arc_buf_contents_t);
799
2a432414
GW
800static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *);
801static void l2arc_read_done(zio_t *);
34dc7c2f 802
b9541d6b 803static boolean_t l2arc_compress_buf(arc_buf_hdr_t *);
2a432414
GW
804static void l2arc_decompress_zio(zio_t *, arc_buf_hdr_t *, enum zio_compress);
805static void l2arc_release_cdata_buf(arc_buf_hdr_t *);
3a17a7a9 806
34dc7c2f 807static uint64_t
d164b209 808buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
34dc7c2f 809{
34dc7c2f
BB
810 uint8_t *vdva = (uint8_t *)dva;
811 uint64_t crc = -1ULL;
812 int i;
813
814 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
815
816 for (i = 0; i < sizeof (dva_t); i++)
817 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
818
d164b209 819 crc ^= (spa>>8) ^ birth;
34dc7c2f
BB
820
821 return (crc);
822}
823
824#define BUF_EMPTY(buf) \
825 ((buf)->b_dva.dva_word[0] == 0 && \
b9541d6b 826 (buf)->b_dva.dva_word[1] == 0)
34dc7c2f
BB
827
828#define BUF_EQUAL(spa, dva, birth, buf) \
829 ((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) && \
830 ((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) && \
831 ((buf)->b_birth == birth) && ((buf)->b_spa == spa)
832
428870ff
BB
833static void
834buf_discard_identity(arc_buf_hdr_t *hdr)
835{
836 hdr->b_dva.dva_word[0] = 0;
837 hdr->b_dva.dva_word[1] = 0;
838 hdr->b_birth = 0;
428870ff
BB
839}
840
34dc7c2f 841static arc_buf_hdr_t *
9b67f605 842buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp)
34dc7c2f 843{
9b67f605
MA
844 const dva_t *dva = BP_IDENTITY(bp);
845 uint64_t birth = BP_PHYSICAL_BIRTH(bp);
34dc7c2f
BB
846 uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
847 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
2a432414 848 arc_buf_hdr_t *hdr;
34dc7c2f
BB
849
850 mutex_enter(hash_lock);
2a432414
GW
851 for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL;
852 hdr = hdr->b_hash_next) {
853 if (BUF_EQUAL(spa, dva, birth, hdr)) {
34dc7c2f 854 *lockp = hash_lock;
2a432414 855 return (hdr);
34dc7c2f
BB
856 }
857 }
858 mutex_exit(hash_lock);
859 *lockp = NULL;
860 return (NULL);
861}
862
863/*
864 * Insert an entry into the hash table. If there is already an element
865 * equal to elem in the hash table, then the already existing element
866 * will be returned and the new element will not be inserted.
867 * Otherwise returns NULL.
b9541d6b 868 * If lockp == NULL, the caller is assumed to already hold the hash lock.
34dc7c2f
BB
869 */
870static arc_buf_hdr_t *
2a432414 871buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp)
34dc7c2f 872{
2a432414 873 uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth);
34dc7c2f 874 kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
2a432414 875 arc_buf_hdr_t *fhdr;
34dc7c2f
BB
876 uint32_t i;
877
2a432414
GW
878 ASSERT(!DVA_IS_EMPTY(&hdr->b_dva));
879 ASSERT(hdr->b_birth != 0);
880 ASSERT(!HDR_IN_HASH_TABLE(hdr));
b9541d6b
CW
881
882 if (lockp != NULL) {
883 *lockp = hash_lock;
884 mutex_enter(hash_lock);
885 } else {
886 ASSERT(MUTEX_HELD(hash_lock));
887 }
888
2a432414
GW
889 for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL;
890 fhdr = fhdr->b_hash_next, i++) {
891 if (BUF_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr))
892 return (fhdr);
34dc7c2f
BB
893 }
894
2a432414
GW
895 hdr->b_hash_next = buf_hash_table.ht_table[idx];
896 buf_hash_table.ht_table[idx] = hdr;
897 hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE;
34dc7c2f
BB
898
899 /* collect some hash table performance data */
900 if (i > 0) {
901 ARCSTAT_BUMP(arcstat_hash_collisions);
902 if (i == 1)
903 ARCSTAT_BUMP(arcstat_hash_chains);
904
905 ARCSTAT_MAX(arcstat_hash_chain_max, i);
906 }
907
908 ARCSTAT_BUMP(arcstat_hash_elements);
909 ARCSTAT_MAXSTAT(arcstat_hash_elements);
910
911 return (NULL);
912}
913
914static void
2a432414 915buf_hash_remove(arc_buf_hdr_t *hdr)
34dc7c2f 916{
2a432414
GW
917 arc_buf_hdr_t *fhdr, **hdrp;
918 uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth);
34dc7c2f
BB
919
920 ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
2a432414 921 ASSERT(HDR_IN_HASH_TABLE(hdr));
34dc7c2f 922
2a432414
GW
923 hdrp = &buf_hash_table.ht_table[idx];
924 while ((fhdr = *hdrp) != hdr) {
925 ASSERT(fhdr != NULL);
926 hdrp = &fhdr->b_hash_next;
34dc7c2f 927 }
2a432414
GW
928 *hdrp = hdr->b_hash_next;
929 hdr->b_hash_next = NULL;
930 hdr->b_flags &= ~ARC_FLAG_IN_HASH_TABLE;
34dc7c2f
BB
931
932 /* collect some hash table performance data */
933 ARCSTAT_BUMPDOWN(arcstat_hash_elements);
934
935 if (buf_hash_table.ht_table[idx] &&
936 buf_hash_table.ht_table[idx]->b_hash_next == NULL)
937 ARCSTAT_BUMPDOWN(arcstat_hash_chains);
938}
939
940/*
941 * Global data structures and functions for the buf kmem cache.
942 */
b9541d6b
CW
943static kmem_cache_t *hdr_full_cache;
944static kmem_cache_t *hdr_l2only_cache;
34dc7c2f
BB
945static kmem_cache_t *buf_cache;
946
947static void
948buf_fini(void)
949{
950 int i;
951
00b46022 952#if defined(_KERNEL) && defined(HAVE_SPL)
d1d7e268
MK
953 /*
954 * Large allocations which do not require contiguous pages
955 * should be using vmem_free() in the linux kernel\
956 */
00b46022
BB
957 vmem_free(buf_hash_table.ht_table,
958 (buf_hash_table.ht_mask + 1) * sizeof (void *));
959#else
34dc7c2f
BB
960 kmem_free(buf_hash_table.ht_table,
961 (buf_hash_table.ht_mask + 1) * sizeof (void *));
00b46022 962#endif
34dc7c2f
BB
963 for (i = 0; i < BUF_LOCKS; i++)
964 mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
b9541d6b
CW
965 kmem_cache_destroy(hdr_full_cache);
966 kmem_cache_destroy(hdr_l2only_cache);
34dc7c2f
BB
967 kmem_cache_destroy(buf_cache);
968}
969
970/*
971 * Constructor callback - called when the cache is empty
972 * and a new buf is requested.
973 */
974/* ARGSUSED */
975static int
b9541d6b
CW
976hdr_full_cons(void *vbuf, void *unused, int kmflag)
977{
978 arc_buf_hdr_t *hdr = vbuf;
979
980 bzero(hdr, HDR_FULL_SIZE);
981 cv_init(&hdr->b_l1hdr.b_cv, NULL, CV_DEFAULT, NULL);
982 refcount_create(&hdr->b_l1hdr.b_refcnt);
983 mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
984 list_link_init(&hdr->b_l1hdr.b_arc_node);
985 list_link_init(&hdr->b_l2hdr.b_l2node);
ca0bf58d 986 multilist_link_init(&hdr->b_l1hdr.b_arc_node);
b9541d6b
CW
987 arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS);
988
989 return (0);
990}
991
992/* ARGSUSED */
993static int
994hdr_l2only_cons(void *vbuf, void *unused, int kmflag)
34dc7c2f 995{
2a432414
GW
996 arc_buf_hdr_t *hdr = vbuf;
997
b9541d6b
CW
998 bzero(hdr, HDR_L2ONLY_SIZE);
999 arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS);
34dc7c2f 1000
34dc7c2f
BB
1001 return (0);
1002}
1003
b128c09f
BB
1004/* ARGSUSED */
1005static int
1006buf_cons(void *vbuf, void *unused, int kmflag)
1007{
1008 arc_buf_t *buf = vbuf;
1009
1010 bzero(buf, sizeof (arc_buf_t));
428870ff 1011 mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
d164b209
BB
1012 arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
1013
b128c09f
BB
1014 return (0);
1015}
1016
34dc7c2f
BB
1017/*
1018 * Destructor callback - called when a cached buf is
1019 * no longer required.
1020 */
1021/* ARGSUSED */
1022static void
b9541d6b 1023hdr_full_dest(void *vbuf, void *unused)
34dc7c2f 1024{
2a432414 1025 arc_buf_hdr_t *hdr = vbuf;
34dc7c2f 1026
2a432414 1027 ASSERT(BUF_EMPTY(hdr));
b9541d6b
CW
1028 cv_destroy(&hdr->b_l1hdr.b_cv);
1029 refcount_destroy(&hdr->b_l1hdr.b_refcnt);
1030 mutex_destroy(&hdr->b_l1hdr.b_freeze_lock);
ca0bf58d 1031 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
b9541d6b
CW
1032 arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS);
1033}
1034
1035/* ARGSUSED */
1036static void
1037hdr_l2only_dest(void *vbuf, void *unused)
1038{
1039 ASSERTV(arc_buf_hdr_t *hdr = vbuf);
1040
1041 ASSERT(BUF_EMPTY(hdr));
1042 arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS);
34dc7c2f
BB
1043}
1044
b128c09f
BB
1045/* ARGSUSED */
1046static void
1047buf_dest(void *vbuf, void *unused)
1048{
1049 arc_buf_t *buf = vbuf;
1050
428870ff 1051 mutex_destroy(&buf->b_evict_lock);
d164b209 1052 arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
b128c09f
BB
1053}
1054
8c8af9d8
BB
1055/*
1056 * Reclaim callback -- invoked when memory is low.
1057 */
1058/* ARGSUSED */
1059static void
1060hdr_recl(void *unused)
1061{
1062 dprintf("hdr_recl called\n");
1063 /*
1064 * umem calls the reclaim func when we destroy the buf cache,
1065 * which is after we do arc_fini().
1066 */
1067 if (!arc_dead)
1068 cv_signal(&arc_reclaim_thread_cv);
1069}
1070
34dc7c2f
BB
1071static void
1072buf_init(void)
1073{
1074 uint64_t *ct;
1075 uint64_t hsize = 1ULL << 12;
1076 int i, j;
1077
1078 /*
1079 * The hash table is big enough to fill all of physical memory
49ddb315
MA
1080 * with an average block size of zfs_arc_average_blocksize (default 8K).
1081 * By default, the table will take up
1082 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
34dc7c2f 1083 */
49ddb315 1084 while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE)
34dc7c2f
BB
1085 hsize <<= 1;
1086retry:
1087 buf_hash_table.ht_mask = hsize - 1;
00b46022 1088#if defined(_KERNEL) && defined(HAVE_SPL)
d1d7e268
MK
1089 /*
1090 * Large allocations which do not require contiguous pages
1091 * should be using vmem_alloc() in the linux kernel
1092 */
00b46022
BB
1093 buf_hash_table.ht_table =
1094 vmem_zalloc(hsize * sizeof (void*), KM_SLEEP);
1095#else
34dc7c2f
BB
1096 buf_hash_table.ht_table =
1097 kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
00b46022 1098#endif
34dc7c2f
BB
1099 if (buf_hash_table.ht_table == NULL) {
1100 ASSERT(hsize > (1ULL << 8));
1101 hsize >>= 1;
1102 goto retry;
1103 }
1104
b9541d6b 1105 hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE,
8c8af9d8 1106 0, hdr_full_cons, hdr_full_dest, hdr_recl, NULL, NULL, 0);
b9541d6b 1107 hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only",
8c8af9d8 1108 HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, hdr_recl,
b9541d6b 1109 NULL, NULL, 0);
34dc7c2f 1110 buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
b128c09f 1111 0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
34dc7c2f
BB
1112
1113 for (i = 0; i < 256; i++)
1114 for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
1115 *ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
1116
1117 for (i = 0; i < BUF_LOCKS; i++) {
1118 mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
40d06e3c 1119 NULL, MUTEX_DEFAULT, NULL);
34dc7c2f
BB
1120 }
1121}
1122
b9541d6b
CW
1123/*
1124 * Transition between the two allocation states for the arc_buf_hdr struct.
1125 * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
1126 * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
1127 * version is used when a cache buffer is only in the L2ARC in order to reduce
1128 * memory usage.
1129 */
1130static arc_buf_hdr_t *
1131arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new)
1132{
1133 arc_buf_hdr_t *nhdr;
1134 l2arc_dev_t *dev;
1135
1136 ASSERT(HDR_HAS_L2HDR(hdr));
1137 ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) ||
1138 (old == hdr_l2only_cache && new == hdr_full_cache));
1139
1140 dev = hdr->b_l2hdr.b_dev;
1141 nhdr = kmem_cache_alloc(new, KM_PUSHPAGE);
1142
1143 ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
1144 buf_hash_remove(hdr);
1145
1146 bcopy(hdr, nhdr, HDR_L2ONLY_SIZE);
d962d5da 1147
b9541d6b
CW
1148 if (new == hdr_full_cache) {
1149 nhdr->b_flags |= ARC_FLAG_HAS_L1HDR;
1150 /*
1151 * arc_access and arc_change_state need to be aware that a
1152 * header has just come out of L2ARC, so we set its state to
1153 * l2c_only even though it's about to change.
1154 */
1155 nhdr->b_l1hdr.b_state = arc_l2c_only;
ca0bf58d
PS
1156
1157 /* Verify previous threads set to NULL before freeing */
1158 ASSERT3P(nhdr->b_l1hdr.b_tmp_cdata, ==, NULL);
b9541d6b
CW
1159 } else {
1160 ASSERT(hdr->b_l1hdr.b_buf == NULL);
1161 ASSERT0(hdr->b_l1hdr.b_datacnt);
ca0bf58d
PS
1162
1163 /*
1164 * If we've reached here, We must have been called from
1165 * arc_evict_hdr(), as such we should have already been
1166 * removed from any ghost list we were previously on
1167 * (which protects us from racing with arc_evict_state),
1168 * thus no locking is needed during this check.
1169 */
1170 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
1171
b9541d6b 1172 /*
ca0bf58d
PS
1173 * A buffer must not be moved into the arc_l2c_only
1174 * state if it's not finished being written out to the
1175 * l2arc device. Otherwise, the b_l1hdr.b_tmp_cdata field
1176 * might try to be accessed, even though it was removed.
b9541d6b 1177 */
ca0bf58d
PS
1178 VERIFY(!HDR_L2_WRITING(hdr));
1179 VERIFY3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL);
1180
b9541d6b
CW
1181 nhdr->b_flags &= ~ARC_FLAG_HAS_L1HDR;
1182 }
1183 /*
1184 * The header has been reallocated so we need to re-insert it into any
1185 * lists it was on.
1186 */
1187 (void) buf_hash_insert(nhdr, NULL);
1188
1189 ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node));
1190
1191 mutex_enter(&dev->l2ad_mtx);
1192
1193 /*
1194 * We must place the realloc'ed header back into the list at
1195 * the same spot. Otherwise, if it's placed earlier in the list,
1196 * l2arc_write_buffers() could find it during the function's
1197 * write phase, and try to write it out to the l2arc.
1198 */
1199 list_insert_after(&dev->l2ad_buflist, hdr, nhdr);
1200 list_remove(&dev->l2ad_buflist, hdr);
1201
1202 mutex_exit(&dev->l2ad_mtx);
1203
d962d5da
PS
1204 /*
1205 * Since we're using the pointer address as the tag when
1206 * incrementing and decrementing the l2ad_alloc refcount, we
1207 * must remove the old pointer (that we're about to destroy) and
1208 * add the new pointer to the refcount. Otherwise we'd remove
1209 * the wrong pointer address when calling arc_hdr_destroy() later.
1210 */
1211
1212 (void) refcount_remove_many(&dev->l2ad_alloc,
1213 hdr->b_l2hdr.b_asize, hdr);
1214
1215 (void) refcount_add_many(&dev->l2ad_alloc,
1216 nhdr->b_l2hdr.b_asize, nhdr);
1217
b9541d6b
CW
1218 buf_discard_identity(hdr);
1219 hdr->b_freeze_cksum = NULL;
1220 kmem_cache_free(old, hdr);
1221
1222 return (nhdr);
1223}
1224
1225
34dc7c2f
BB
1226#define ARC_MINTIME (hz>>4) /* 62 ms */
1227
1228static void
1229arc_cksum_verify(arc_buf_t *buf)
1230{
1231 zio_cksum_t zc;
1232
1233 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1234 return;
1235
b9541d6b
CW
1236 mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock);
1237 if (buf->b_hdr->b_freeze_cksum == NULL || HDR_IO_ERROR(buf->b_hdr)) {
1238 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock);
34dc7c2f
BB
1239 return;
1240 }
1241 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
1242 if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
1243 panic("buffer modified while frozen!");
b9541d6b 1244 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock);
34dc7c2f
BB
1245}
1246
1247static int
1248arc_cksum_equal(arc_buf_t *buf)
1249{
1250 zio_cksum_t zc;
1251 int equal;
1252
b9541d6b 1253 mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock);
34dc7c2f
BB
1254 fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
1255 equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
b9541d6b 1256 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock);
34dc7c2f
BB
1257
1258 return (equal);
1259}
1260
1261static void
1262arc_cksum_compute(arc_buf_t *buf, boolean_t force)
1263{
1264 if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
1265 return;
1266
b9541d6b 1267 mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock);
34dc7c2f 1268 if (buf->b_hdr->b_freeze_cksum != NULL) {
b9541d6b 1269 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock);
34dc7c2f
BB
1270 return;
1271 }
96c080cb 1272 buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
34dc7c2f
BB
1273 fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
1274 buf->b_hdr->b_freeze_cksum);
b9541d6b 1275 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock);
498877ba
MA
1276 arc_buf_watch(buf);
1277}
1278
1279#ifndef _KERNEL
1280void
1281arc_buf_sigsegv(int sig, siginfo_t *si, void *unused)
1282{
1283 panic("Got SIGSEGV at address: 0x%lx\n", (long) si->si_addr);
1284}
1285#endif
1286
1287/* ARGSUSED */
1288static void
1289arc_buf_unwatch(arc_buf_t *buf)
1290{
1291#ifndef _KERNEL
1292 if (arc_watch) {
1293 ASSERT0(mprotect(buf->b_data, buf->b_hdr->b_size,
1294 PROT_READ | PROT_WRITE));
1295 }
1296#endif
1297}
1298
1299/* ARGSUSED */
1300static void
1301arc_buf_watch(arc_buf_t *buf)
1302{
1303#ifndef _KERNEL
1304 if (arc_watch)
1305 ASSERT0(mprotect(buf->b_data, buf->b_hdr->b_size, PROT_READ));
1306#endif
34dc7c2f
BB
1307}
1308
b9541d6b
CW
1309static arc_buf_contents_t
1310arc_buf_type(arc_buf_hdr_t *hdr)
1311{
1312 if (HDR_ISTYPE_METADATA(hdr)) {
1313 return (ARC_BUFC_METADATA);
1314 } else {
1315 return (ARC_BUFC_DATA);
1316 }
1317}
1318
1319static uint32_t
1320arc_bufc_to_flags(arc_buf_contents_t type)
1321{
1322 switch (type) {
1323 case ARC_BUFC_DATA:
1324 /* metadata field is 0 if buffer contains normal data */
1325 return (0);
1326 case ARC_BUFC_METADATA:
1327 return (ARC_FLAG_BUFC_METADATA);
1328 default:
1329 break;
1330 }
1331 panic("undefined ARC buffer type!");
1332 return ((uint32_t)-1);
1333}
1334
34dc7c2f
BB
1335void
1336arc_buf_thaw(arc_buf_t *buf)
1337{
1338 if (zfs_flags & ZFS_DEBUG_MODIFY) {
b9541d6b 1339 if (buf->b_hdr->b_l1hdr.b_state != arc_anon)
34dc7c2f 1340 panic("modifying non-anon buffer!");
b9541d6b 1341 if (HDR_IO_IN_PROGRESS(buf->b_hdr))
34dc7c2f
BB
1342 panic("modifying buffer while i/o in progress!");
1343 arc_cksum_verify(buf);
1344 }
1345
b9541d6b 1346 mutex_enter(&buf->b_hdr->b_l1hdr.b_freeze_lock);
34dc7c2f
BB
1347 if (buf->b_hdr->b_freeze_cksum != NULL) {
1348 kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1349 buf->b_hdr->b_freeze_cksum = NULL;
1350 }
428870ff 1351
b9541d6b 1352 mutex_exit(&buf->b_hdr->b_l1hdr.b_freeze_lock);
498877ba
MA
1353
1354 arc_buf_unwatch(buf);
34dc7c2f
BB
1355}
1356
1357void
1358arc_buf_freeze(arc_buf_t *buf)
1359{
428870ff
BB
1360 kmutex_t *hash_lock;
1361
34dc7c2f
BB
1362 if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1363 return;
1364
428870ff
BB
1365 hash_lock = HDR_LOCK(buf->b_hdr);
1366 mutex_enter(hash_lock);
1367
34dc7c2f 1368 ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
b9541d6b 1369 buf->b_hdr->b_l1hdr.b_state == arc_anon);
34dc7c2f 1370 arc_cksum_compute(buf, B_FALSE);
428870ff 1371 mutex_exit(hash_lock);
498877ba 1372
34dc7c2f
BB
1373}
1374
1375static void
2a432414 1376add_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag)
34dc7c2f 1377{
b9541d6b
CW
1378 arc_state_t *state;
1379
1380 ASSERT(HDR_HAS_L1HDR(hdr));
34dc7c2f
BB
1381 ASSERT(MUTEX_HELD(hash_lock));
1382
b9541d6b
CW
1383 state = hdr->b_l1hdr.b_state;
1384
1385 if ((refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) &&
1386 (state != arc_anon)) {
1387 /* We don't use the L2-only state list. */
1388 if (state != arc_l2c_only) {
ca0bf58d 1389 arc_buf_contents_t type = arc_buf_type(hdr);
b9541d6b 1390 uint64_t delta = hdr->b_size * hdr->b_l1hdr.b_datacnt;
ca0bf58d
PS
1391 multilist_t *list = &state->arcs_list[type];
1392 uint64_t *size = &state->arcs_lsize[type];
1393
1394 multilist_remove(list, hdr);
b9541d6b 1395
b9541d6b
CW
1396 if (GHOST_STATE(state)) {
1397 ASSERT0(hdr->b_l1hdr.b_datacnt);
1398 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
1399 delta = hdr->b_size;
1400 }
1401 ASSERT(delta > 0);
1402 ASSERT3U(*size, >=, delta);
1403 atomic_add_64(size, -delta);
34dc7c2f 1404 }
b128c09f 1405 /* remove the prefetch flag if we get a reference */
b9541d6b 1406 hdr->b_flags &= ~ARC_FLAG_PREFETCH;
34dc7c2f
BB
1407 }
1408}
1409
1410static int
2a432414 1411remove_reference(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, void *tag)
34dc7c2f
BB
1412{
1413 int cnt;
b9541d6b 1414 arc_state_t *state = hdr->b_l1hdr.b_state;
34dc7c2f 1415
b9541d6b 1416 ASSERT(HDR_HAS_L1HDR(hdr));
34dc7c2f
BB
1417 ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1418 ASSERT(!GHOST_STATE(state));
1419
b9541d6b
CW
1420 /*
1421 * arc_l2c_only counts as a ghost state so we don't need to explicitly
1422 * check to prevent usage of the arc_l2c_only list.
1423 */
1424 if (((cnt = refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) == 0) &&
34dc7c2f 1425 (state != arc_anon)) {
ca0bf58d
PS
1426 arc_buf_contents_t type = arc_buf_type(hdr);
1427 multilist_t *list = &state->arcs_list[type];
1428 uint64_t *size = &state->arcs_lsize[type];
1429
1430 multilist_insert(list, hdr);
34dc7c2f 1431
b9541d6b
CW
1432 ASSERT(hdr->b_l1hdr.b_datacnt > 0);
1433 atomic_add_64(size, hdr->b_size *
1434 hdr->b_l1hdr.b_datacnt);
34dc7c2f
BB
1435 }
1436 return (cnt);
1437}
1438
e0b0ca98
BB
1439/*
1440 * Returns detailed information about a specific arc buffer. When the
1441 * state_index argument is set the function will calculate the arc header
1442 * list position for its arc state. Since this requires a linear traversal
1443 * callers are strongly encourage not to do this. However, it can be helpful
1444 * for targeted analysis so the functionality is provided.
1445 */
1446void
1447arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index)
1448{
1449 arc_buf_hdr_t *hdr = ab->b_hdr;
b9541d6b
CW
1450 l1arc_buf_hdr_t *l1hdr = NULL;
1451 l2arc_buf_hdr_t *l2hdr = NULL;
1452 arc_state_t *state = NULL;
1453
1454 if (HDR_HAS_L1HDR(hdr)) {
1455 l1hdr = &hdr->b_l1hdr;
1456 state = l1hdr->b_state;
1457 }
1458 if (HDR_HAS_L2HDR(hdr))
1459 l2hdr = &hdr->b_l2hdr;
e0b0ca98 1460
d1d7e268 1461 memset(abi, 0, sizeof (arc_buf_info_t));
e0b0ca98 1462 abi->abi_flags = hdr->b_flags;
b9541d6b
CW
1463
1464 if (l1hdr) {
1465 abi->abi_datacnt = l1hdr->b_datacnt;
1466 abi->abi_access = l1hdr->b_arc_access;
1467 abi->abi_mru_hits = l1hdr->b_mru_hits;
1468 abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits;
1469 abi->abi_mfu_hits = l1hdr->b_mfu_hits;
1470 abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits;
1471 abi->abi_holds = refcount_count(&l1hdr->b_refcnt);
1472 }
1473
1474 if (l2hdr) {
1475 abi->abi_l2arc_dattr = l2hdr->b_daddr;
1476 abi->abi_l2arc_asize = l2hdr->b_asize;
4e0f33ff 1477 abi->abi_l2arc_compress = l2hdr->b_compress;
b9541d6b
CW
1478 abi->abi_l2arc_hits = l2hdr->b_hits;
1479 }
1480
e0b0ca98 1481 abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON;
b9541d6b 1482 abi->abi_state_contents = arc_buf_type(hdr);
e0b0ca98 1483 abi->abi_size = hdr->b_size;
e0b0ca98
BB
1484}
1485
34dc7c2f 1486/*
ca0bf58d 1487 * Move the supplied buffer to the indicated state. The hash lock
34dc7c2f
BB
1488 * for the buffer must be held by the caller.
1489 */
1490static void
2a432414
GW
1491arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr,
1492 kmutex_t *hash_lock)
34dc7c2f 1493{
b9541d6b
CW
1494 arc_state_t *old_state;
1495 int64_t refcnt;
1496 uint32_t datacnt;
34dc7c2f 1497 uint64_t from_delta, to_delta;
b9541d6b
CW
1498 arc_buf_contents_t buftype = arc_buf_type(hdr);
1499
1500 /*
1501 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
1502 * in arc_read() when bringing a buffer out of the L2ARC. However, the
1503 * L1 hdr doesn't always exist when we change state to arc_anon before
1504 * destroying a header, in which case reallocating to add the L1 hdr is
1505 * pointless.
1506 */
1507 if (HDR_HAS_L1HDR(hdr)) {
1508 old_state = hdr->b_l1hdr.b_state;
1509 refcnt = refcount_count(&hdr->b_l1hdr.b_refcnt);
1510 datacnt = hdr->b_l1hdr.b_datacnt;
1511 } else {
1512 old_state = arc_l2c_only;
1513 refcnt = 0;
1514 datacnt = 0;
1515 }
34dc7c2f
BB
1516
1517 ASSERT(MUTEX_HELD(hash_lock));
e8b96c60 1518 ASSERT3P(new_state, !=, old_state);
b9541d6b
CW
1519 ASSERT(refcnt == 0 || datacnt > 0);
1520 ASSERT(!GHOST_STATE(new_state) || datacnt == 0);
1521 ASSERT(old_state != arc_anon || datacnt <= 1);
34dc7c2f 1522
b9541d6b 1523 from_delta = to_delta = datacnt * hdr->b_size;
34dc7c2f
BB
1524
1525 /*
1526 * If this buffer is evictable, transfer it from the
1527 * old state list to the new state list.
1528 */
1529 if (refcnt == 0) {
b9541d6b 1530 if (old_state != arc_anon && old_state != arc_l2c_only) {
b9541d6b 1531 uint64_t *size = &old_state->arcs_lsize[buftype];
34dc7c2f 1532
b9541d6b 1533 ASSERT(HDR_HAS_L1HDR(hdr));
ca0bf58d 1534 multilist_remove(&old_state->arcs_list[buftype], hdr);
34dc7c2f
BB
1535
1536 /*
1537 * If prefetching out of the ghost cache,
428870ff 1538 * we will have a non-zero datacnt.
34dc7c2f 1539 */
b9541d6b 1540 if (GHOST_STATE(old_state) && datacnt == 0) {
34dc7c2f 1541 /* ghost elements have a ghost size */
b9541d6b 1542 ASSERT(hdr->b_l1hdr.b_buf == NULL);
2a432414 1543 from_delta = hdr->b_size;
34dc7c2f
BB
1544 }
1545 ASSERT3U(*size, >=, from_delta);
1546 atomic_add_64(size, -from_delta);
34dc7c2f 1547 }
b9541d6b 1548 if (new_state != arc_anon && new_state != arc_l2c_only) {
b9541d6b 1549 uint64_t *size = &new_state->arcs_lsize[buftype];
34dc7c2f 1550
b9541d6b
CW
1551 /*
1552 * An L1 header always exists here, since if we're
1553 * moving to some L1-cached state (i.e. not l2c_only or
1554 * anonymous), we realloc the header to add an L1hdr
1555 * beforehand.
1556 */
1557 ASSERT(HDR_HAS_L1HDR(hdr));
ca0bf58d 1558 multilist_insert(&new_state->arcs_list[buftype], hdr);
34dc7c2f
BB
1559
1560 /* ghost elements have a ghost size */
1561 if (GHOST_STATE(new_state)) {
b9541d6b
CW
1562 ASSERT0(datacnt);
1563 ASSERT(hdr->b_l1hdr.b_buf == NULL);
2a432414 1564 to_delta = hdr->b_size;
34dc7c2f
BB
1565 }
1566 atomic_add_64(size, to_delta);
34dc7c2f
BB
1567 }
1568 }
1569
2a432414
GW
1570 ASSERT(!BUF_EMPTY(hdr));
1571 if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr))
1572 buf_hash_remove(hdr);
34dc7c2f 1573
b9541d6b 1574 /* adjust state sizes (ignore arc_l2c_only) */
36da08ef
PS
1575
1576 if (to_delta && new_state != arc_l2c_only) {
1577 ASSERT(HDR_HAS_L1HDR(hdr));
1578 if (GHOST_STATE(new_state)) {
1579 ASSERT0(datacnt);
1580
1581 /*
1582 * We moving a header to a ghost state, we first
1583 * remove all arc buffers. Thus, we'll have a
1584 * datacnt of zero, and no arc buffer to use for
1585 * the reference. As a result, we use the arc
1586 * header pointer for the reference.
1587 */
1588 (void) refcount_add_many(&new_state->arcs_size,
1589 hdr->b_size, hdr);
1590 } else {
1591 arc_buf_t *buf;
1592 ASSERT3U(datacnt, !=, 0);
1593
1594 /*
1595 * Each individual buffer holds a unique reference,
1596 * thus we must remove each of these references one
1597 * at a time.
1598 */
1599 for (buf = hdr->b_l1hdr.b_buf; buf != NULL;
1600 buf = buf->b_next) {
1601 (void) refcount_add_many(&new_state->arcs_size,
1602 hdr->b_size, buf);
1603 }
1604 }
1605 }
1606
b9541d6b 1607 if (from_delta && old_state != arc_l2c_only) {
36da08ef
PS
1608 ASSERT(HDR_HAS_L1HDR(hdr));
1609 if (GHOST_STATE(old_state)) {
1610 /*
1611 * When moving a header off of a ghost state,
1612 * there's the possibility for datacnt to be
1613 * non-zero. This is because we first add the
1614 * arc buffer to the header prior to changing
1615 * the header's state. Since we used the header
1616 * for the reference when putting the header on
1617 * the ghost state, we must balance that and use
1618 * the header when removing off the ghost state
1619 * (even though datacnt is non zero).
1620 */
1621
1622 IMPLY(datacnt == 0, new_state == arc_anon ||
1623 new_state == arc_l2c_only);
1624
1625 (void) refcount_remove_many(&old_state->arcs_size,
1626 hdr->b_size, hdr);
1627 } else {
1628 arc_buf_t *buf;
1629 ASSERT3U(datacnt, !=, 0);
1630
1631 /*
1632 * Each individual buffer holds a unique reference,
1633 * thus we must remove each of these references one
1634 * at a time.
1635 */
1636 for (buf = hdr->b_l1hdr.b_buf; buf != NULL;
1637 buf = buf->b_next) {
1638 (void) refcount_remove_many(
1639 &old_state->arcs_size, hdr->b_size, buf);
1640 }
1641 }
34dc7c2f 1642 }
36da08ef 1643
b9541d6b
CW
1644 if (HDR_HAS_L1HDR(hdr))
1645 hdr->b_l1hdr.b_state = new_state;
34dc7c2f 1646
b9541d6b
CW
1647 /*
1648 * L2 headers should never be on the L2 state list since they don't
1649 * have L1 headers allocated.
1650 */
ca0bf58d
PS
1651 ASSERT(multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]) &&
1652 multilist_is_empty(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA]));
34dc7c2f
BB
1653}
1654
1655void
d164b209 1656arc_space_consume(uint64_t space, arc_space_type_t type)
34dc7c2f 1657{
d164b209
BB
1658 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1659
1660 switch (type) {
e75c13c3
BB
1661 default:
1662 break;
d164b209
BB
1663 case ARC_SPACE_DATA:
1664 ARCSTAT_INCR(arcstat_data_size, space);
1665 break;
cc7f677c 1666 case ARC_SPACE_META:
500445c0 1667 ARCSTAT_INCR(arcstat_metadata_size, space);
cc7f677c 1668 break;
d164b209
BB
1669 case ARC_SPACE_OTHER:
1670 ARCSTAT_INCR(arcstat_other_size, space);
1671 break;
1672 case ARC_SPACE_HDRS:
1673 ARCSTAT_INCR(arcstat_hdr_size, space);
1674 break;
1675 case ARC_SPACE_L2HDRS:
1676 ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1677 break;
1678 }
1679
500445c0 1680 if (type != ARC_SPACE_DATA)
cc7f677c
PS
1681 ARCSTAT_INCR(arcstat_meta_used, space);
1682
34dc7c2f
BB
1683 atomic_add_64(&arc_size, space);
1684}
1685
1686void
d164b209 1687arc_space_return(uint64_t space, arc_space_type_t type)
34dc7c2f 1688{
d164b209
BB
1689 ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1690
1691 switch (type) {
e75c13c3
BB
1692 default:
1693 break;
d164b209
BB
1694 case ARC_SPACE_DATA:
1695 ARCSTAT_INCR(arcstat_data_size, -space);
1696 break;
cc7f677c 1697 case ARC_SPACE_META:
500445c0 1698 ARCSTAT_INCR(arcstat_metadata_size, -space);
cc7f677c 1699 break;
d164b209
BB
1700 case ARC_SPACE_OTHER:
1701 ARCSTAT_INCR(arcstat_other_size, -space);
1702 break;
1703 case ARC_SPACE_HDRS:
1704 ARCSTAT_INCR(arcstat_hdr_size, -space);
1705 break;
1706 case ARC_SPACE_L2HDRS:
1707 ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1708 break;
1709 }
1710
cc7f677c
PS
1711 if (type != ARC_SPACE_DATA) {
1712 ASSERT(arc_meta_used >= space);
500445c0
PS
1713 if (arc_meta_max < arc_meta_used)
1714 arc_meta_max = arc_meta_used;
cc7f677c
PS
1715 ARCSTAT_INCR(arcstat_meta_used, -space);
1716 }
1717
34dc7c2f
BB
1718 ASSERT(arc_size >= space);
1719 atomic_add_64(&arc_size, -space);
1720}
1721
34dc7c2f 1722arc_buf_t *
5f6d0b6f 1723arc_buf_alloc(spa_t *spa, uint64_t size, void *tag, arc_buf_contents_t type)
34dc7c2f
BB
1724{
1725 arc_buf_hdr_t *hdr;
1726 arc_buf_t *buf;
1727
f1512ee6 1728 VERIFY3U(size, <=, spa_maxblocksize(spa));
b9541d6b 1729 hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE);
34dc7c2f 1730 ASSERT(BUF_EMPTY(hdr));
b9541d6b 1731 ASSERT3P(hdr->b_freeze_cksum, ==, NULL);
34dc7c2f 1732 hdr->b_size = size;
3541dc6d 1733 hdr->b_spa = spa_load_guid(spa);
b9541d6b
CW
1734 hdr->b_l1hdr.b_mru_hits = 0;
1735 hdr->b_l1hdr.b_mru_ghost_hits = 0;
1736 hdr->b_l1hdr.b_mfu_hits = 0;
1737 hdr->b_l1hdr.b_mfu_ghost_hits = 0;
1738 hdr->b_l1hdr.b_l2_hits = 0;
1739
34dc7c2f
BB
1740 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1741 buf->b_hdr = hdr;
1742 buf->b_data = NULL;
1743 buf->b_efunc = NULL;
1744 buf->b_private = NULL;
1745 buf->b_next = NULL;
b9541d6b
CW
1746
1747 hdr->b_flags = arc_bufc_to_flags(type);
1748 hdr->b_flags |= ARC_FLAG_HAS_L1HDR;
1749
1750 hdr->b_l1hdr.b_buf = buf;
1751 hdr->b_l1hdr.b_state = arc_anon;
1752 hdr->b_l1hdr.b_arc_access = 0;
1753 hdr->b_l1hdr.b_datacnt = 1;
ca0bf58d 1754 hdr->b_l1hdr.b_tmp_cdata = NULL;
b9541d6b 1755
34dc7c2f 1756 arc_get_data_buf(buf);
b9541d6b
CW
1757 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
1758 (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag);
34dc7c2f
BB
1759
1760 return (buf);
1761}
1762
9babb374
BB
1763static char *arc_onloan_tag = "onloan";
1764
1765/*
1766 * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1767 * flight data by arc_tempreserve_space() until they are "returned". Loaned
1768 * buffers must be returned to the arc before they can be used by the DMU or
1769 * freed.
1770 */
1771arc_buf_t *
5f6d0b6f 1772arc_loan_buf(spa_t *spa, uint64_t size)
9babb374
BB
1773{
1774 arc_buf_t *buf;
1775
1776 buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1777
1778 atomic_add_64(&arc_loaned_bytes, size);
1779 return (buf);
1780}
1781
1782/*
1783 * Return a loaned arc buffer to the arc.
1784 */
1785void
1786arc_return_buf(arc_buf_t *buf, void *tag)
1787{
1788 arc_buf_hdr_t *hdr = buf->b_hdr;
1789
9babb374 1790 ASSERT(buf->b_data != NULL);
b9541d6b
CW
1791 ASSERT(HDR_HAS_L1HDR(hdr));
1792 (void) refcount_add(&hdr->b_l1hdr.b_refcnt, tag);
1793 (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag);
9babb374
BB
1794
1795 atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1796}
1797
428870ff
BB
1798/* Detach an arc_buf from a dbuf (tag) */
1799void
1800arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1801{
b9541d6b 1802 arc_buf_hdr_t *hdr = buf->b_hdr;
428870ff
BB
1803
1804 ASSERT(buf->b_data != NULL);
b9541d6b
CW
1805 ASSERT(HDR_HAS_L1HDR(hdr));
1806 (void) refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag);
1807 (void) refcount_remove(&hdr->b_l1hdr.b_refcnt, tag);
428870ff
BB
1808 buf->b_efunc = NULL;
1809 buf->b_private = NULL;
1810
1811 atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1812}
1813
34dc7c2f
BB
1814static arc_buf_t *
1815arc_buf_clone(arc_buf_t *from)
1816{
1817 arc_buf_t *buf;
1818 arc_buf_hdr_t *hdr = from->b_hdr;
1819 uint64_t size = hdr->b_size;
1820
b9541d6b
CW
1821 ASSERT(HDR_HAS_L1HDR(hdr));
1822 ASSERT(hdr->b_l1hdr.b_state != arc_anon);
428870ff 1823
34dc7c2f
BB
1824 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1825 buf->b_hdr = hdr;
1826 buf->b_data = NULL;
1827 buf->b_efunc = NULL;
1828 buf->b_private = NULL;
b9541d6b
CW
1829 buf->b_next = hdr->b_l1hdr.b_buf;
1830 hdr->b_l1hdr.b_buf = buf;
34dc7c2f
BB
1831 arc_get_data_buf(buf);
1832 bcopy(from->b_data, buf->b_data, size);
1eb5bfa3
GW
1833
1834 /*
1835 * This buffer already exists in the arc so create a duplicate
1836 * copy for the caller. If the buffer is associated with user data
1837 * then track the size and number of duplicates. These stats will be
1838 * updated as duplicate buffers are created and destroyed.
1839 */
b9541d6b 1840 if (HDR_ISTYPE_DATA(hdr)) {
1eb5bfa3
GW
1841 ARCSTAT_BUMP(arcstat_duplicate_buffers);
1842 ARCSTAT_INCR(arcstat_duplicate_buffers_size, size);
1843 }
b9541d6b 1844 hdr->b_l1hdr.b_datacnt += 1;
34dc7c2f
BB
1845 return (buf);
1846}
1847
1848void
1849arc_buf_add_ref(arc_buf_t *buf, void* tag)
1850{
1851 arc_buf_hdr_t *hdr;
1852 kmutex_t *hash_lock;
1853
1854 /*
b128c09f
BB
1855 * Check to see if this buffer is evicted. Callers
1856 * must verify b_data != NULL to know if the add_ref
1857 * was successful.
34dc7c2f 1858 */
428870ff 1859 mutex_enter(&buf->b_evict_lock);
b128c09f 1860 if (buf->b_data == NULL) {
428870ff 1861 mutex_exit(&buf->b_evict_lock);
34dc7c2f
BB
1862 return;
1863 }
428870ff 1864 hash_lock = HDR_LOCK(buf->b_hdr);
34dc7c2f 1865 mutex_enter(hash_lock);
428870ff 1866 hdr = buf->b_hdr;
b9541d6b 1867 ASSERT(HDR_HAS_L1HDR(hdr));
428870ff
BB
1868 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1869 mutex_exit(&buf->b_evict_lock);
34dc7c2f 1870
b9541d6b
CW
1871 ASSERT(hdr->b_l1hdr.b_state == arc_mru ||
1872 hdr->b_l1hdr.b_state == arc_mfu);
1873
34dc7c2f 1874 add_reference(hdr, hash_lock, tag);
d164b209 1875 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
34dc7c2f
BB
1876 arc_access(hdr, hash_lock);
1877 mutex_exit(hash_lock);
1878 ARCSTAT_BUMP(arcstat_hits);
b9541d6b
CW
1879 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr),
1880 demand, prefetch, !HDR_ISTYPE_METADATA(hdr),
34dc7c2f
BB
1881 data, metadata, hits);
1882}
1883
ca0bf58d
PS
1884static void
1885arc_buf_free_on_write(void *data, size_t size,
1886 void (*free_func)(void *, size_t))
1887{
1888 l2arc_data_free_t *df;
1889
1890 df = kmem_alloc(sizeof (*df), KM_SLEEP);
1891 df->l2df_data = data;
1892 df->l2df_size = size;
1893 df->l2df_func = free_func;
1894 mutex_enter(&l2arc_free_on_write_mtx);
1895 list_insert_head(l2arc_free_on_write, df);
1896 mutex_exit(&l2arc_free_on_write_mtx);
1897}
1898
34dc7c2f
BB
1899/*
1900 * Free the arc data buffer. If it is an l2arc write in progress,
1901 * the buffer is placed on l2arc_free_on_write to be freed later.
1902 */
1903static void
498877ba 1904arc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t))
34dc7c2f 1905{
498877ba
MA
1906 arc_buf_hdr_t *hdr = buf->b_hdr;
1907
34dc7c2f 1908 if (HDR_L2_WRITING(hdr)) {
ca0bf58d 1909 arc_buf_free_on_write(buf->b_data, hdr->b_size, free_func);
34dc7c2f
BB
1910 ARCSTAT_BUMP(arcstat_l2_free_on_write);
1911 } else {
498877ba 1912 free_func(buf->b_data, hdr->b_size);
34dc7c2f
BB
1913 }
1914}
1915
ca0bf58d
PS
1916static void
1917arc_buf_l2_cdata_free(arc_buf_hdr_t *hdr)
1918{
1919 ASSERT(HDR_HAS_L2HDR(hdr));
1920 ASSERT(MUTEX_HELD(&hdr->b_l2hdr.b_dev->l2ad_mtx));
1921
1922 /*
1923 * The b_tmp_cdata field is linked off of the b_l1hdr, so if
1924 * that doesn't exist, the header is in the arc_l2c_only state,
1925 * and there isn't anything to free (it's already been freed).
1926 */
1927 if (!HDR_HAS_L1HDR(hdr))
1928 return;
1929
1930 /*
1931 * The header isn't being written to the l2arc device, thus it
1932 * shouldn't have a b_tmp_cdata to free.
1933 */
1934 if (!HDR_L2_WRITING(hdr)) {
1935 ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL);
1936 return;
1937 }
1938
1939 /*
1940 * The header does not have compression enabled. This can be due
1941 * to the buffer not being compressible, or because we're
1942 * freeing the buffer before the second phase of
1943 * l2arc_write_buffer() has started (which does the compression
1944 * step). In either case, b_tmp_cdata does not point to a
1945 * separately compressed buffer, so there's nothing to free (it
1946 * points to the same buffer as the arc_buf_t's b_data field).
1947 */
4e0f33ff 1948 if (hdr->b_l2hdr.b_compress == ZIO_COMPRESS_OFF) {
ca0bf58d
PS
1949 hdr->b_l1hdr.b_tmp_cdata = NULL;
1950 return;
1951 }
1952
1953 /*
1954 * There's nothing to free since the buffer was all zero's and
1955 * compressed to a zero length buffer.
1956 */
4e0f33ff 1957 if (hdr->b_l2hdr.b_compress == ZIO_COMPRESS_EMPTY) {
ca0bf58d
PS
1958 ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL);
1959 return;
1960 }
1961
4e0f33ff 1962 ASSERT(L2ARC_IS_VALID_COMPRESS(hdr->b_l2hdr.b_compress));
ca0bf58d
PS
1963
1964 arc_buf_free_on_write(hdr->b_l1hdr.b_tmp_cdata,
1965 hdr->b_size, zio_data_buf_free);
1966
1967 ARCSTAT_BUMP(arcstat_l2_cdata_free_on_write);
1968 hdr->b_l1hdr.b_tmp_cdata = NULL;
1969}
1970
bd089c54
MA
1971/*
1972 * Free up buf->b_data and if 'remove' is set, then pull the
1973 * arc_buf_t off of the the arc_buf_hdr_t's list and free it.
1974 */
34dc7c2f 1975static void
ca0bf58d 1976arc_buf_destroy(arc_buf_t *buf, boolean_t remove)
34dc7c2f
BB
1977{
1978 arc_buf_t **bufp;
1979
1980 /* free up data associated with the buf */
b9541d6b
CW
1981 if (buf->b_data != NULL) {
1982 arc_state_t *state = buf->b_hdr->b_l1hdr.b_state;
34dc7c2f 1983 uint64_t size = buf->b_hdr->b_size;
b9541d6b 1984 arc_buf_contents_t type = arc_buf_type(buf->b_hdr);
34dc7c2f
BB
1985
1986 arc_cksum_verify(buf);
498877ba 1987 arc_buf_unwatch(buf);
428870ff 1988
ca0bf58d
PS
1989 if (type == ARC_BUFC_METADATA) {
1990 arc_buf_data_free(buf, zio_buf_free);
1991 arc_space_return(size, ARC_SPACE_META);
1992 } else {
1993 ASSERT(type == ARC_BUFC_DATA);
1994 arc_buf_data_free(buf, zio_data_buf_free);
1995 arc_space_return(size, ARC_SPACE_DATA);
34dc7c2f 1996 }
ca0bf58d
PS
1997
1998 /* protected by hash lock, if in the hash table */
1999 if (multilist_link_active(&buf->b_hdr->b_l1hdr.b_arc_node)) {
34dc7c2f
BB
2000 uint64_t *cnt = &state->arcs_lsize[type];
2001
b9541d6b
CW
2002 ASSERT(refcount_is_zero(
2003 &buf->b_hdr->b_l1hdr.b_refcnt));
2004 ASSERT(state != arc_anon && state != arc_l2c_only);
34dc7c2f
BB
2005
2006 ASSERT3U(*cnt, >=, size);
2007 atomic_add_64(cnt, -size);
2008 }
36da08ef
PS
2009
2010 (void) refcount_remove_many(&state->arcs_size, size, buf);
34dc7c2f 2011 buf->b_data = NULL;
1eb5bfa3
GW
2012
2013 /*
2014 * If we're destroying a duplicate buffer make sure
2015 * that the appropriate statistics are updated.
2016 */
b9541d6b
CW
2017 if (buf->b_hdr->b_l1hdr.b_datacnt > 1 &&
2018 HDR_ISTYPE_DATA(buf->b_hdr)) {
1eb5bfa3
GW
2019 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
2020 ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size);
2021 }
b9541d6b
CW
2022 ASSERT(buf->b_hdr->b_l1hdr.b_datacnt > 0);
2023 buf->b_hdr->b_l1hdr.b_datacnt -= 1;
34dc7c2f
BB
2024 }
2025
2026 /* only remove the buf if requested */
bd089c54 2027 if (!remove)
34dc7c2f
BB
2028 return;
2029
2030 /* remove the buf from the hdr list */
b9541d6b
CW
2031 for (bufp = &buf->b_hdr->b_l1hdr.b_buf; *bufp != buf;
2032 bufp = &(*bufp)->b_next)
34dc7c2f
BB
2033 continue;
2034 *bufp = buf->b_next;
428870ff 2035 buf->b_next = NULL;
34dc7c2f
BB
2036
2037 ASSERT(buf->b_efunc == NULL);
2038
2039 /* clean up the buf */
2040 buf->b_hdr = NULL;
2041 kmem_cache_free(buf_cache, buf);
2042}
2043
d962d5da
PS
2044static void
2045arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr)
2046{
2047 l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr;
2048 l2arc_dev_t *dev = l2hdr->b_dev;
2049
2050 ASSERT(MUTEX_HELD(&dev->l2ad_mtx));
2051 ASSERT(HDR_HAS_L2HDR(hdr));
2052
2053 list_remove(&dev->l2ad_buflist, hdr);
2054
d962d5da
PS
2055 /*
2056 * We don't want to leak the b_tmp_cdata buffer that was
2057 * allocated in l2arc_write_buffers()
2058 */
2059 arc_buf_l2_cdata_free(hdr);
2060
2061 /*
2062 * If the l2hdr's b_daddr is equal to L2ARC_ADDR_UNSET, then
2063 * this header is being processed by l2arc_write_buffers() (i.e.
2064 * it's in the first stage of l2arc_write_buffers()).
2065 * Re-affirming that truth here, just to serve as a reminder. If
2066 * b_daddr does not equal L2ARC_ADDR_UNSET, then the header may or
2067 * may not have its HDR_L2_WRITING flag set. (the write may have
2068 * completed, in which case HDR_L2_WRITING will be false and the
2069 * b_daddr field will point to the address of the buffer on disk).
2070 */
2071 IMPLY(l2hdr->b_daddr == L2ARC_ADDR_UNSET, HDR_L2_WRITING(hdr));
2072
2073 /*
2074 * If b_daddr is equal to L2ARC_ADDR_UNSET, we're racing with
2075 * l2arc_write_buffers(). Since we've just removed this header
2076 * from the l2arc buffer list, this header will never reach the
2077 * second stage of l2arc_write_buffers(), which increments the
2078 * accounting stats for this header. Thus, we must be careful
2079 * not to decrement them for this header either.
2080 */
2081 if (l2hdr->b_daddr != L2ARC_ADDR_UNSET) {
2082 ARCSTAT_INCR(arcstat_l2_asize, -l2hdr->b_asize);
2083 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
2084
2085 vdev_space_update(dev->l2ad_vdev,
2086 -l2hdr->b_asize, 0, 0);
2087
2088 (void) refcount_remove_many(&dev->l2ad_alloc,
2089 l2hdr->b_asize, hdr);
2090 }
2091
2092 hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR;
2093}
2094
34dc7c2f
BB
2095static void
2096arc_hdr_destroy(arc_buf_hdr_t *hdr)
2097{
b9541d6b
CW
2098 if (HDR_HAS_L1HDR(hdr)) {
2099 ASSERT(hdr->b_l1hdr.b_buf == NULL ||
2100 hdr->b_l1hdr.b_datacnt > 0);
2101 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
2102 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
2103 }
34dc7c2f 2104 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
b9541d6b
CW
2105 ASSERT(!HDR_IN_HASH_TABLE(hdr));
2106
2107 if (HDR_HAS_L2HDR(hdr)) {
d962d5da
PS
2108 l2arc_dev_t *dev = hdr->b_l2hdr.b_dev;
2109 boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx);
428870ff 2110
d962d5da
PS
2111 if (!buflist_held)
2112 mutex_enter(&dev->l2ad_mtx);
b9541d6b 2113
ca0bf58d 2114 /*
d962d5da
PS
2115 * Even though we checked this conditional above, we
2116 * need to check this again now that we have the
2117 * l2ad_mtx. This is because we could be racing with
2118 * another thread calling l2arc_evict() which might have
2119 * destroyed this header's L2 portion as we were waiting
2120 * to acquire the l2ad_mtx. If that happens, we don't
2121 * want to re-destroy the header's L2 portion.
ca0bf58d 2122 */
d962d5da
PS
2123 if (HDR_HAS_L2HDR(hdr))
2124 arc_hdr_l2hdr_destroy(hdr);
428870ff
BB
2125
2126 if (!buflist_held)
d962d5da 2127 mutex_exit(&dev->l2ad_mtx);
34dc7c2f
BB
2128 }
2129
b9541d6b 2130 if (!BUF_EMPTY(hdr))
428870ff 2131 buf_discard_identity(hdr);
b9541d6b 2132
34dc7c2f
BB
2133 if (hdr->b_freeze_cksum != NULL) {
2134 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
2135 hdr->b_freeze_cksum = NULL;
2136 }
2137
b9541d6b
CW
2138 if (HDR_HAS_L1HDR(hdr)) {
2139 while (hdr->b_l1hdr.b_buf) {
2140 arc_buf_t *buf = hdr->b_l1hdr.b_buf;
2141
2142 if (buf->b_efunc != NULL) {
ca0bf58d 2143 mutex_enter(&arc_user_evicts_lock);
b9541d6b
CW
2144 mutex_enter(&buf->b_evict_lock);
2145 ASSERT(buf->b_hdr != NULL);
ca0bf58d 2146 arc_buf_destroy(hdr->b_l1hdr.b_buf, FALSE);
b9541d6b
CW
2147 hdr->b_l1hdr.b_buf = buf->b_next;
2148 buf->b_hdr = &arc_eviction_hdr;
2149 buf->b_next = arc_eviction_list;
2150 arc_eviction_list = buf;
2151 mutex_exit(&buf->b_evict_lock);
ca0bf58d
PS
2152 cv_signal(&arc_user_evicts_cv);
2153 mutex_exit(&arc_user_evicts_lock);
b9541d6b 2154 } else {
ca0bf58d 2155 arc_buf_destroy(hdr->b_l1hdr.b_buf, TRUE);
b9541d6b
CW
2156 }
2157 }
2158 }
2159
34dc7c2f 2160 ASSERT3P(hdr->b_hash_next, ==, NULL);
b9541d6b 2161 if (HDR_HAS_L1HDR(hdr)) {
ca0bf58d 2162 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
b9541d6b
CW
2163 ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
2164 kmem_cache_free(hdr_full_cache, hdr);
2165 } else {
2166 kmem_cache_free(hdr_l2only_cache, hdr);
2167 }
34dc7c2f
BB
2168}
2169
2170void
2171arc_buf_free(arc_buf_t *buf, void *tag)
2172{
2173 arc_buf_hdr_t *hdr = buf->b_hdr;
b9541d6b 2174 int hashed = hdr->b_l1hdr.b_state != arc_anon;
34dc7c2f
BB
2175
2176 ASSERT(buf->b_efunc == NULL);
2177 ASSERT(buf->b_data != NULL);
2178
2179 if (hashed) {
2180 kmutex_t *hash_lock = HDR_LOCK(hdr);
2181
2182 mutex_enter(hash_lock);
428870ff
BB
2183 hdr = buf->b_hdr;
2184 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
2185
34dc7c2f 2186 (void) remove_reference(hdr, hash_lock, tag);
b9541d6b 2187 if (hdr->b_l1hdr.b_datacnt > 1) {
ca0bf58d 2188 arc_buf_destroy(buf, TRUE);
428870ff 2189 } else {
b9541d6b 2190 ASSERT(buf == hdr->b_l1hdr.b_buf);
428870ff 2191 ASSERT(buf->b_efunc == NULL);
2a432414 2192 hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE;
428870ff 2193 }
34dc7c2f
BB
2194 mutex_exit(hash_lock);
2195 } else if (HDR_IO_IN_PROGRESS(hdr)) {
2196 int destroy_hdr;
2197 /*
2198 * We are in the middle of an async write. Don't destroy
2199 * this buffer unless the write completes before we finish
2200 * decrementing the reference count.
2201 */
ca0bf58d 2202 mutex_enter(&arc_user_evicts_lock);
34dc7c2f 2203 (void) remove_reference(hdr, NULL, tag);
b9541d6b 2204 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
34dc7c2f 2205 destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
ca0bf58d 2206 mutex_exit(&arc_user_evicts_lock);
34dc7c2f
BB
2207 if (destroy_hdr)
2208 arc_hdr_destroy(hdr);
2209 } else {
428870ff 2210 if (remove_reference(hdr, NULL, tag) > 0)
ca0bf58d 2211 arc_buf_destroy(buf, TRUE);
428870ff 2212 else
34dc7c2f 2213 arc_hdr_destroy(hdr);
34dc7c2f
BB
2214 }
2215}
2216
13fe0198 2217boolean_t
34dc7c2f
BB
2218arc_buf_remove_ref(arc_buf_t *buf, void* tag)
2219{
2220 arc_buf_hdr_t *hdr = buf->b_hdr;
96c080cb 2221 kmutex_t *hash_lock = HDR_LOCK(hdr);
13fe0198 2222 boolean_t no_callback = (buf->b_efunc == NULL);
34dc7c2f 2223
b9541d6b
CW
2224 if (hdr->b_l1hdr.b_state == arc_anon) {
2225 ASSERT(hdr->b_l1hdr.b_datacnt == 1);
34dc7c2f
BB
2226 arc_buf_free(buf, tag);
2227 return (no_callback);
2228 }
2229
2230 mutex_enter(hash_lock);
428870ff 2231 hdr = buf->b_hdr;
b9541d6b 2232 ASSERT(hdr->b_l1hdr.b_datacnt > 0);
428870ff 2233 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
b9541d6b 2234 ASSERT(hdr->b_l1hdr.b_state != arc_anon);
34dc7c2f
BB
2235 ASSERT(buf->b_data != NULL);
2236
2237 (void) remove_reference(hdr, hash_lock, tag);
b9541d6b 2238 if (hdr->b_l1hdr.b_datacnt > 1) {
34dc7c2f 2239 if (no_callback)
ca0bf58d 2240 arc_buf_destroy(buf, TRUE);
34dc7c2f 2241 } else if (no_callback) {
b9541d6b 2242 ASSERT(hdr->b_l1hdr.b_buf == buf && buf->b_next == NULL);
428870ff 2243 ASSERT(buf->b_efunc == NULL);
2a432414 2244 hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE;
34dc7c2f 2245 }
b9541d6b
CW
2246 ASSERT(no_callback || hdr->b_l1hdr.b_datacnt > 1 ||
2247 refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
34dc7c2f
BB
2248 mutex_exit(hash_lock);
2249 return (no_callback);
2250}
2251
5f6d0b6f 2252uint64_t
34dc7c2f
BB
2253arc_buf_size(arc_buf_t *buf)
2254{
2255 return (buf->b_hdr->b_size);
2256}
2257
1eb5bfa3
GW
2258/*
2259 * Called from the DMU to determine if the current buffer should be
2260 * evicted. In order to ensure proper locking, the eviction must be initiated
2261 * from the DMU. Return true if the buffer is associated with user data and
2262 * duplicate buffers still exist.
2263 */
2264boolean_t
2265arc_buf_eviction_needed(arc_buf_t *buf)
2266{
2267 arc_buf_hdr_t *hdr;
2268 boolean_t evict_needed = B_FALSE;
2269
2270 if (zfs_disable_dup_eviction)
2271 return (B_FALSE);
2272
2273 mutex_enter(&buf->b_evict_lock);
2274 hdr = buf->b_hdr;
2275 if (hdr == NULL) {
2276 /*
2277 * We are in arc_do_user_evicts(); let that function
2278 * perform the eviction.
2279 */
2280 ASSERT(buf->b_data == NULL);
2281 mutex_exit(&buf->b_evict_lock);
2282 return (B_FALSE);
2283 } else if (buf->b_data == NULL) {
2284 /*
2285 * We have already been added to the arc eviction list;
2286 * recommend eviction.
2287 */
2288 ASSERT3P(hdr, ==, &arc_eviction_hdr);
2289 mutex_exit(&buf->b_evict_lock);
2290 return (B_TRUE);
2291 }
2292
b9541d6b 2293 if (hdr->b_l1hdr.b_datacnt > 1 && HDR_ISTYPE_DATA(hdr))
1eb5bfa3
GW
2294 evict_needed = B_TRUE;
2295
2296 mutex_exit(&buf->b_evict_lock);
2297 return (evict_needed);
2298}
2299
34dc7c2f 2300/*
ca0bf58d
PS
2301 * Evict the arc_buf_hdr that is provided as a parameter. The resultant
2302 * state of the header is dependent on its state prior to entering this
2303 * function. The following transitions are possible:
34dc7c2f 2304 *
ca0bf58d
PS
2305 * - arc_mru -> arc_mru_ghost
2306 * - arc_mfu -> arc_mfu_ghost
2307 * - arc_mru_ghost -> arc_l2c_only
2308 * - arc_mru_ghost -> deleted
2309 * - arc_mfu_ghost -> arc_l2c_only
2310 * - arc_mfu_ghost -> deleted
34dc7c2f 2311 */
ca0bf58d
PS
2312static int64_t
2313arc_evict_hdr(arc_buf_hdr_t *hdr, kmutex_t *hash_lock)
34dc7c2f 2314{
ca0bf58d
PS
2315 arc_state_t *evicted_state, *state;
2316 int64_t bytes_evicted = 0;
34dc7c2f 2317
ca0bf58d
PS
2318 ASSERT(MUTEX_HELD(hash_lock));
2319 ASSERT(HDR_HAS_L1HDR(hdr));
e8b96c60 2320
ca0bf58d
PS
2321 state = hdr->b_l1hdr.b_state;
2322 if (GHOST_STATE(state)) {
2323 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
2324 ASSERT(hdr->b_l1hdr.b_buf == NULL);
e8b96c60
MA
2325
2326 /*
ca0bf58d
PS
2327 * l2arc_write_buffers() relies on a header's L1 portion
2328 * (i.e. its b_tmp_cdata field) during its write phase.
2329 * Thus, we cannot push a header onto the arc_l2c_only
2330 * state (removing its L1 piece) until the header is
2331 * done being written to the l2arc.
e8b96c60 2332 */
ca0bf58d
PS
2333 if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) {
2334 ARCSTAT_BUMP(arcstat_evict_l2_skip);
2335 return (bytes_evicted);
e8b96c60
MA
2336 }
2337
ca0bf58d
PS
2338 ARCSTAT_BUMP(arcstat_deleted);
2339 bytes_evicted += hdr->b_size;
428870ff 2340
ca0bf58d 2341 DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr);
428870ff 2342
ca0bf58d
PS
2343 if (HDR_HAS_L2HDR(hdr)) {
2344 /*
2345 * This buffer is cached on the 2nd Level ARC;
2346 * don't destroy the header.
2347 */
2348 arc_change_state(arc_l2c_only, hdr, hash_lock);
2349 /*
2350 * dropping from L1+L2 cached to L2-only,
2351 * realloc to remove the L1 header.
2352 */
2353 hdr = arc_hdr_realloc(hdr, hdr_full_cache,
2354 hdr_l2only_cache);
34dc7c2f 2355 } else {
ca0bf58d
PS
2356 arc_change_state(arc_anon, hdr, hash_lock);
2357 arc_hdr_destroy(hdr);
34dc7c2f 2358 }
ca0bf58d 2359 return (bytes_evicted);
34dc7c2f
BB
2360 }
2361
ca0bf58d
PS
2362 ASSERT(state == arc_mru || state == arc_mfu);
2363 evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
34dc7c2f 2364
ca0bf58d
PS
2365 /* prefetch buffers have a minimum lifespan */
2366 if (HDR_IO_IN_PROGRESS(hdr) ||
2367 ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) &&
2368 ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access <
2369 arc_min_prefetch_lifespan)) {
2370 ARCSTAT_BUMP(arcstat_evict_skip);
2371 return (bytes_evicted);
da8ccd0e
PS
2372 }
2373
ca0bf58d
PS
2374 ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt));
2375 ASSERT3U(hdr->b_l1hdr.b_datacnt, >, 0);
2376 while (hdr->b_l1hdr.b_buf) {
2377 arc_buf_t *buf = hdr->b_l1hdr.b_buf;
2378 if (!mutex_tryenter(&buf->b_evict_lock)) {
2379 ARCSTAT_BUMP(arcstat_mutex_miss);
2380 break;
2381 }
2382 if (buf->b_data != NULL)
2383 bytes_evicted += hdr->b_size;
2384 if (buf->b_efunc != NULL) {
2385 mutex_enter(&arc_user_evicts_lock);
2386 arc_buf_destroy(buf, FALSE);
2387 hdr->b_l1hdr.b_buf = buf->b_next;
2388 buf->b_hdr = &arc_eviction_hdr;
2389 buf->b_next = arc_eviction_list;
2390 arc_eviction_list = buf;
2391 cv_signal(&arc_user_evicts_cv);
2392 mutex_exit(&arc_user_evicts_lock);
2393 mutex_exit(&buf->b_evict_lock);
2394 } else {
2395 mutex_exit(&buf->b_evict_lock);
2396 arc_buf_destroy(buf, TRUE);
2397 }
2398 }
34dc7c2f 2399
ca0bf58d
PS
2400 if (HDR_HAS_L2HDR(hdr)) {
2401 ARCSTAT_INCR(arcstat_evict_l2_cached, hdr->b_size);
2402 } else {
2403 if (l2arc_write_eligible(hdr->b_spa, hdr))
2404 ARCSTAT_INCR(arcstat_evict_l2_eligible, hdr->b_size);
2405 else
2406 ARCSTAT_INCR(arcstat_evict_l2_ineligible, hdr->b_size);
2407 }
34dc7c2f 2408
ca0bf58d
PS
2409 if (hdr->b_l1hdr.b_datacnt == 0) {
2410 arc_change_state(evicted_state, hdr, hash_lock);
2411 ASSERT(HDR_IN_HASH_TABLE(hdr));
2412 hdr->b_flags |= ARC_FLAG_IN_HASH_TABLE;
2413 hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE;
2414 DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr);
2415 }
34dc7c2f 2416
ca0bf58d 2417 return (bytes_evicted);
34dc7c2f
BB
2418}
2419
ca0bf58d
PS
2420static uint64_t
2421arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker,
2422 uint64_t spa, int64_t bytes)
34dc7c2f 2423{
ca0bf58d
PS
2424 multilist_sublist_t *mls;
2425 uint64_t bytes_evicted = 0;
2426 arc_buf_hdr_t *hdr;
34dc7c2f 2427 kmutex_t *hash_lock;
ca0bf58d 2428 int evict_count = 0;
34dc7c2f 2429
ca0bf58d 2430 ASSERT3P(marker, !=, NULL);
96c080cb 2431 IMPLY(bytes < 0, bytes == ARC_EVICT_ALL);
ca0bf58d
PS
2432
2433 mls = multilist_sublist_lock(ml, idx);
572e2857 2434
ca0bf58d
PS
2435 for (hdr = multilist_sublist_prev(mls, marker); hdr != NULL;
2436 hdr = multilist_sublist_prev(mls, marker)) {
2437 if ((bytes != ARC_EVICT_ALL && bytes_evicted >= bytes) ||
2438 (evict_count >= zfs_arc_evict_batch_limit))
2439 break;
2440
2441 /*
2442 * To keep our iteration location, move the marker
2443 * forward. Since we're not holding hdr's hash lock, we
2444 * must be very careful and not remove 'hdr' from the
2445 * sublist. Otherwise, other consumers might mistake the
2446 * 'hdr' as not being on a sublist when they call the
2447 * multilist_link_active() function (they all rely on
2448 * the hash lock protecting concurrent insertions and
2449 * removals). multilist_sublist_move_forward() was
2450 * specifically implemented to ensure this is the case
2451 * (only 'marker' will be removed and re-inserted).
2452 */
2453 multilist_sublist_move_forward(mls, marker);
2454
2455 /*
2456 * The only case where the b_spa field should ever be
2457 * zero, is the marker headers inserted by
2458 * arc_evict_state(). It's possible for multiple threads
2459 * to be calling arc_evict_state() concurrently (e.g.
2460 * dsl_pool_close() and zio_inject_fault()), so we must
2461 * skip any markers we see from these other threads.
2462 */
2a432414 2463 if (hdr->b_spa == 0)
572e2857
BB
2464 continue;
2465
ca0bf58d
PS
2466 /* we're only interested in evicting buffers of a certain spa */
2467 if (spa != 0 && hdr->b_spa != spa) {
2468 ARCSTAT_BUMP(arcstat_evict_skip);
428870ff 2469 continue;
ca0bf58d
PS
2470 }
2471
2472 hash_lock = HDR_LOCK(hdr);
e8b96c60
MA
2473
2474 /*
ca0bf58d
PS
2475 * We aren't calling this function from any code path
2476 * that would already be holding a hash lock, so we're
2477 * asserting on this assumption to be defensive in case
2478 * this ever changes. Without this check, it would be
2479 * possible to incorrectly increment arcstat_mutex_miss
2480 * below (e.g. if the code changed such that we called
2481 * this function with a hash lock held).
e8b96c60 2482 */
ca0bf58d
PS
2483 ASSERT(!MUTEX_HELD(hash_lock));
2484
34dc7c2f 2485 if (mutex_tryenter(hash_lock)) {
ca0bf58d
PS
2486 uint64_t evicted = arc_evict_hdr(hdr, hash_lock);
2487 mutex_exit(hash_lock);
34dc7c2f 2488
ca0bf58d 2489 bytes_evicted += evicted;
34dc7c2f 2490
572e2857 2491 /*
ca0bf58d
PS
2492 * If evicted is zero, arc_evict_hdr() must have
2493 * decided to skip this header, don't increment
2494 * evict_count in this case.
572e2857 2495 */
ca0bf58d
PS
2496 if (evicted != 0)
2497 evict_count++;
2498
2499 /*
2500 * If arc_size isn't overflowing, signal any
2501 * threads that might happen to be waiting.
2502 *
2503 * For each header evicted, we wake up a single
2504 * thread. If we used cv_broadcast, we could
2505 * wake up "too many" threads causing arc_size
2506 * to significantly overflow arc_c; since
2507 * arc_get_data_buf() doesn't check for overflow
2508 * when it's woken up (it doesn't because it's
2509 * possible for the ARC to be overflowing while
2510 * full of un-evictable buffers, and the
2511 * function should proceed in this case).
2512 *
2513 * If threads are left sleeping, due to not
2514 * using cv_broadcast, they will be woken up
2515 * just before arc_reclaim_thread() sleeps.
2516 */
2517 mutex_enter(&arc_reclaim_lock);
2518 if (!arc_is_overflowing())
2519 cv_signal(&arc_reclaim_waiters_cv);
2520 mutex_exit(&arc_reclaim_lock);
e8b96c60 2521 } else {
ca0bf58d 2522 ARCSTAT_BUMP(arcstat_mutex_miss);
e8b96c60 2523 }
34dc7c2f 2524 }
34dc7c2f 2525
ca0bf58d 2526 multilist_sublist_unlock(mls);
34dc7c2f 2527
ca0bf58d 2528 return (bytes_evicted);
34dc7c2f
BB
2529}
2530
ca0bf58d
PS
2531/*
2532 * Evict buffers from the given arc state, until we've removed the
2533 * specified number of bytes. Move the removed buffers to the
2534 * appropriate evict state.
2535 *
2536 * This function makes a "best effort". It skips over any buffers
2537 * it can't get a hash_lock on, and so, may not catch all candidates.
2538 * It may also return without evicting as much space as requested.
2539 *
2540 * If bytes is specified using the special value ARC_EVICT_ALL, this
2541 * will evict all available (i.e. unlocked and evictable) buffers from
2542 * the given arc state; which is used by arc_flush().
2543 */
2544static uint64_t
2545arc_evict_state(arc_state_t *state, uint64_t spa, int64_t bytes,
2546 arc_buf_contents_t type)
34dc7c2f 2547{
ca0bf58d
PS
2548 uint64_t total_evicted = 0;
2549 multilist_t *ml = &state->arcs_list[type];
2550 int num_sublists;
2551 arc_buf_hdr_t **markers;
2552 int i;
2553
96c080cb 2554 IMPLY(bytes < 0, bytes == ARC_EVICT_ALL);
ca0bf58d
PS
2555
2556 num_sublists = multilist_get_num_sublists(ml);
d164b209
BB
2557
2558 /*
ca0bf58d
PS
2559 * If we've tried to evict from each sublist, made some
2560 * progress, but still have not hit the target number of bytes
2561 * to evict, we want to keep trying. The markers allow us to
2562 * pick up where we left off for each individual sublist, rather
2563 * than starting from the tail each time.
d164b209 2564 */
ca0bf58d
PS
2565 markers = kmem_zalloc(sizeof (*markers) * num_sublists, KM_SLEEP);
2566 for (i = 0; i < num_sublists; i++) {
2567 multilist_sublist_t *mls;
34dc7c2f 2568
ca0bf58d
PS
2569 markers[i] = kmem_cache_alloc(hdr_full_cache, KM_SLEEP);
2570
2571 /*
2572 * A b_spa of 0 is used to indicate that this header is
2573 * a marker. This fact is used in arc_adjust_type() and
2574 * arc_evict_state_impl().
2575 */
2576 markers[i]->b_spa = 0;
34dc7c2f 2577
ca0bf58d
PS
2578 mls = multilist_sublist_lock(ml, i);
2579 multilist_sublist_insert_tail(mls, markers[i]);
2580 multilist_sublist_unlock(mls);
34dc7c2f
BB
2581 }
2582
d164b209 2583 /*
ca0bf58d
PS
2584 * While we haven't hit our target number of bytes to evict, or
2585 * we're evicting all available buffers.
d164b209 2586 */
ca0bf58d
PS
2587 while (total_evicted < bytes || bytes == ARC_EVICT_ALL) {
2588 /*
2589 * Start eviction using a randomly selected sublist,
2590 * this is to try and evenly balance eviction across all
2591 * sublists. Always starting at the same sublist
2592 * (e.g. index 0) would cause evictions to favor certain
2593 * sublists over others.
2594 */
2595 int sublist_idx = multilist_get_random_index(ml);
2596 uint64_t scan_evicted = 0;
34dc7c2f 2597
ca0bf58d
PS
2598 for (i = 0; i < num_sublists; i++) {
2599 uint64_t bytes_remaining;
2600 uint64_t bytes_evicted;
d164b209 2601
ca0bf58d
PS
2602 if (bytes == ARC_EVICT_ALL)
2603 bytes_remaining = ARC_EVICT_ALL;
2604 else if (total_evicted < bytes)
2605 bytes_remaining = bytes - total_evicted;
2606 else
2607 break;
34dc7c2f 2608
ca0bf58d
PS
2609 bytes_evicted = arc_evict_state_impl(ml, sublist_idx,
2610 markers[sublist_idx], spa, bytes_remaining);
2611
2612 scan_evicted += bytes_evicted;
2613 total_evicted += bytes_evicted;
2614
2615 /* we've reached the end, wrap to the beginning */
2616 if (++sublist_idx >= num_sublists)
2617 sublist_idx = 0;
2618 }
2619
2620 /*
2621 * If we didn't evict anything during this scan, we have
2622 * no reason to believe we'll evict more during another
2623 * scan, so break the loop.
2624 */
2625 if (scan_evicted == 0) {
2626 /* This isn't possible, let's make that obvious */
2627 ASSERT3S(bytes, !=, 0);
34dc7c2f 2628
ca0bf58d
PS
2629 /*
2630 * When bytes is ARC_EVICT_ALL, the only way to
2631 * break the loop is when scan_evicted is zero.
2632 * In that case, we actually have evicted enough,
2633 * so we don't want to increment the kstat.
2634 */
2635 if (bytes != ARC_EVICT_ALL) {
2636 ASSERT3S(total_evicted, <, bytes);
2637 ARCSTAT_BUMP(arcstat_evict_not_enough);
2638 }
d164b209 2639
ca0bf58d
PS
2640 break;
2641 }
d164b209 2642 }
34dc7c2f 2643
ca0bf58d
PS
2644 for (i = 0; i < num_sublists; i++) {
2645 multilist_sublist_t *mls = multilist_sublist_lock(ml, i);
2646 multilist_sublist_remove(mls, markers[i]);
2647 multilist_sublist_unlock(mls);
34dc7c2f 2648
ca0bf58d 2649 kmem_cache_free(hdr_full_cache, markers[i]);
34dc7c2f 2650 }
ca0bf58d
PS
2651 kmem_free(markers, sizeof (*markers) * num_sublists);
2652
2653 return (total_evicted);
2654}
2655
2656/*
2657 * Flush all "evictable" data of the given type from the arc state
2658 * specified. This will not evict any "active" buffers (i.e. referenced).
2659 *
2660 * When 'retry' is set to FALSE, the function will make a single pass
2661 * over the state and evict any buffers that it can. Since it doesn't
2662 * continually retry the eviction, it might end up leaving some buffers
2663 * in the ARC due to lock misses.
2664 *
2665 * When 'retry' is set to TRUE, the function will continually retry the
2666 * eviction until *all* evictable buffers have been removed from the
2667 * state. As a result, if concurrent insertions into the state are
2668 * allowed (e.g. if the ARC isn't shutting down), this function might
2669 * wind up in an infinite loop, continually trying to evict buffers.
2670 */
2671static uint64_t
2672arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type,
2673 boolean_t retry)
2674{
2675 uint64_t evicted = 0;
2676
2677 while (state->arcs_lsize[type] != 0) {
2678 evicted += arc_evict_state(state, spa, ARC_EVICT_ALL, type);
2679
2680 if (!retry)
2681 break;
2682 }
2683
2684 return (evicted);
34dc7c2f
BB
2685}
2686
ab26409d 2687/*
f6046738
BB
2688 * Helper function for arc_prune() it is responsible for safely handling
2689 * the execution of a registered arc_prune_func_t.
ab26409d
BB
2690 */
2691static void
f6046738 2692arc_prune_task(void *ptr)
ab26409d 2693{
f6046738
BB
2694 arc_prune_t *ap = (arc_prune_t *)ptr;
2695 arc_prune_func_t *func = ap->p_pfunc;
ab26409d 2696
f6046738
BB
2697 if (func != NULL)
2698 func(ap->p_adjust, ap->p_private);
ab26409d 2699
f6046738
BB
2700 /* Callback unregistered concurrently with execution */
2701 if (refcount_remove(&ap->p_refcnt, func) == 0) {
2702 ASSERT(!list_link_active(&ap->p_node));
2703 refcount_destroy(&ap->p_refcnt);
2704 kmem_free(ap, sizeof (*ap));
2705 }
2706}
ab26409d 2707
f6046738
BB
2708/*
2709 * Notify registered consumers they must drop holds on a portion of the ARC
2710 * buffered they reference. This provides a mechanism to ensure the ARC can
2711 * honor the arc_meta_limit and reclaim otherwise pinned ARC buffers. This
2712 * is analogous to dnlc_reduce_cache() but more generic.
2713 *
2714 * This operation is performed asyncronously so it may be safely called
ca67b33a 2715 * in the context of the arc_reclaim_thread(). A reference is taken here
f6046738
BB
2716 * for each registered arc_prune_t and the arc_prune_task() is responsible
2717 * for releasing it once the registered arc_prune_func_t has completed.
2718 */
2719static void
2720arc_prune_async(int64_t adjust)
2721{
2722 arc_prune_t *ap;
ab26409d 2723
f6046738
BB
2724 mutex_enter(&arc_prune_mtx);
2725 for (ap = list_head(&arc_prune_list); ap != NULL;
2726 ap = list_next(&arc_prune_list, ap)) {
ab26409d 2727
f6046738
BB
2728 if (refcount_count(&ap->p_refcnt) >= 2)
2729 continue;
ab26409d 2730
f6046738
BB
2731 refcount_add(&ap->p_refcnt, ap->p_pfunc);
2732 ap->p_adjust = adjust;
2733 taskq_dispatch(arc_prune_taskq, arc_prune_task, ap, TQ_SLEEP);
2734 ARCSTAT_BUMP(arcstat_prune);
ab26409d 2735 }
ab26409d
BB
2736 mutex_exit(&arc_prune_mtx);
2737}
2738
f6046738
BB
2739static void
2740arc_prune(int64_t adjust)
2741{
2742 arc_prune_async(adjust);
2743 taskq_wait_outstanding(arc_prune_taskq, 0);
2744}
2745
ca0bf58d
PS
2746/*
2747 * Evict the specified number of bytes from the state specified,
2748 * restricting eviction to the spa and type given. This function
2749 * prevents us from trying to evict more from a state's list than
2750 * is "evictable", and to skip evicting altogether when passed a
2751 * negative value for "bytes". In contrast, arc_evict_state() will
2752 * evict everything it can, when passed a negative value for "bytes".
2753 */
2754static uint64_t
2755arc_adjust_impl(arc_state_t *state, uint64_t spa, int64_t bytes,
2756 arc_buf_contents_t type)
2757{
2758 int64_t delta;
2759
2760 if (bytes > 0 && state->arcs_lsize[type] > 0) {
2761 delta = MIN(state->arcs_lsize[type], bytes);
2762 return (arc_evict_state(state, spa, delta, type));
2763 }
2764
2765 return (0);
2766}
2767
2768/*
2769 * The goal of this function is to evict enough meta data buffers from the
2770 * ARC in order to enforce the arc_meta_limit. Achieving this is slightly
2771 * more complicated than it appears because it is common for data buffers
2772 * to have holds on meta data buffers. In addition, dnode meta data buffers
2773 * will be held by the dnodes in the block preventing them from being freed.
2774 * This means we can't simply traverse the ARC and expect to always find
2775 * enough unheld meta data buffer to release.
2776 *
2777 * Therefore, this function has been updated to make alternating passes
2778 * over the ARC releasing data buffers and then newly unheld meta data
2779 * buffers. This ensures forward progress is maintained and arc_meta_used
2780 * will decrease. Normally this is sufficient, but if required the ARC
2781 * will call the registered prune callbacks causing dentry and inodes to
2782 * be dropped from the VFS cache. This will make dnode meta data buffers
2783 * available for reclaim.
2784 */
2785static uint64_t
f6046738 2786arc_adjust_meta_balanced(void)
ca0bf58d
PS
2787{
2788 int64_t adjustmnt, delta, prune = 0;
2789 uint64_t total_evicted = 0;
2790 arc_buf_contents_t type = ARC_BUFC_DATA;
ca67b33a 2791 int restarts = MAX(zfs_arc_meta_adjust_restarts, 0);
ca0bf58d
PS
2792
2793restart:
2794 /*
2795 * This slightly differs than the way we evict from the mru in
2796 * arc_adjust because we don't have a "target" value (i.e. no
2797 * "meta" arc_p). As a result, I think we can completely
2798 * cannibalize the metadata in the MRU before we evict the
2799 * metadata from the MFU. I think we probably need to implement a
2800 * "metadata arc_p" value to do this properly.
2801 */
2802 adjustmnt = arc_meta_used - arc_meta_limit;
2803
2804 if (adjustmnt > 0 && arc_mru->arcs_lsize[type] > 0) {
2805 delta = MIN(arc_mru->arcs_lsize[type], adjustmnt);
2806 total_evicted += arc_adjust_impl(arc_mru, 0, delta, type);
2807 adjustmnt -= delta;
2808 }
2809
2810 /*
2811 * We can't afford to recalculate adjustmnt here. If we do,
2812 * new metadata buffers can sneak into the MRU or ANON lists,
2813 * thus penalize the MFU metadata. Although the fudge factor is
2814 * small, it has been empirically shown to be significant for
2815 * certain workloads (e.g. creating many empty directories). As
2816 * such, we use the original calculation for adjustmnt, and
2817 * simply decrement the amount of data evicted from the MRU.
2818 */
2819
2820 if (adjustmnt > 0 && arc_mfu->arcs_lsize[type] > 0) {
2821 delta = MIN(arc_mfu->arcs_lsize[type], adjustmnt);
2822 total_evicted += arc_adjust_impl(arc_mfu, 0, delta, type);
2823 }
2824
2825 adjustmnt = arc_meta_used - arc_meta_limit;
2826
2827 if (adjustmnt > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
2828 delta = MIN(adjustmnt,
2829 arc_mru_ghost->arcs_lsize[type]);
2830 total_evicted += arc_adjust_impl(arc_mru_ghost, 0, delta, type);
2831 adjustmnt -= delta;
2832 }
2833
2834 if (adjustmnt > 0 && arc_mfu_ghost->arcs_lsize[type] > 0) {
2835 delta = MIN(adjustmnt,
2836 arc_mfu_ghost->arcs_lsize[type]);
2837 total_evicted += arc_adjust_impl(arc_mfu_ghost, 0, delta, type);
2838 }
2839
2840 /*
2841 * If after attempting to make the requested adjustment to the ARC
2842 * the meta limit is still being exceeded then request that the
2843 * higher layers drop some cached objects which have holds on ARC
2844 * meta buffers. Requests to the upper layers will be made with
2845 * increasingly large scan sizes until the ARC is below the limit.
2846 */
2847 if (arc_meta_used > arc_meta_limit) {
2848 if (type == ARC_BUFC_DATA) {
2849 type = ARC_BUFC_METADATA;
2850 } else {
2851 type = ARC_BUFC_DATA;
2852
2853 if (zfs_arc_meta_prune) {
2854 prune += zfs_arc_meta_prune;
f6046738 2855 arc_prune_async(prune);
ca0bf58d
PS
2856 }
2857 }
2858
2859 if (restarts > 0) {
2860 restarts--;
2861 goto restart;
2862 }
2863 }
2864 return (total_evicted);
2865}
2866
f6046738
BB
2867/*
2868 * Evict metadata buffers from the cache, such that arc_meta_used is
2869 * capped by the arc_meta_limit tunable.
2870 */
2871static uint64_t
2872arc_adjust_meta_only(void)
2873{
2874 uint64_t total_evicted = 0;
2875 int64_t target;
2876
2877 /*
2878 * If we're over the meta limit, we want to evict enough
2879 * metadata to get back under the meta limit. We don't want to
2880 * evict so much that we drop the MRU below arc_p, though. If
2881 * we're over the meta limit more than we're over arc_p, we
2882 * evict some from the MRU here, and some from the MFU below.
2883 */
2884 target = MIN((int64_t)(arc_meta_used - arc_meta_limit),
36da08ef
PS
2885 (int64_t)(refcount_count(&arc_anon->arcs_size) +
2886 refcount_count(&arc_mru->arcs_size) - arc_p));
f6046738
BB
2887
2888 total_evicted += arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA);
2889
2890 /*
2891 * Similar to the above, we want to evict enough bytes to get us
2892 * below the meta limit, but not so much as to drop us below the
2893 * space alloted to the MFU (which is defined as arc_c - arc_p).
2894 */
2895 target = MIN((int64_t)(arc_meta_used - arc_meta_limit),
36da08ef 2896 (int64_t)(refcount_count(&arc_mfu->arcs_size) - (arc_c - arc_p)));
f6046738
BB
2897
2898 total_evicted += arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA);
2899
2900 return (total_evicted);
2901}
2902
2903static uint64_t
2904arc_adjust_meta(void)
2905{
2906 if (zfs_arc_meta_strategy == ARC_STRATEGY_META_ONLY)
2907 return (arc_adjust_meta_only());
2908 else
2909 return (arc_adjust_meta_balanced());
2910}
2911
ca0bf58d
PS
2912/*
2913 * Return the type of the oldest buffer in the given arc state
2914 *
2915 * This function will select a random sublist of type ARC_BUFC_DATA and
2916 * a random sublist of type ARC_BUFC_METADATA. The tail of each sublist
2917 * is compared, and the type which contains the "older" buffer will be
2918 * returned.
2919 */
2920static arc_buf_contents_t
2921arc_adjust_type(arc_state_t *state)
2922{
2923 multilist_t *data_ml = &state->arcs_list[ARC_BUFC_DATA];
2924 multilist_t *meta_ml = &state->arcs_list[ARC_BUFC_METADATA];
2925 int data_idx = multilist_get_random_index(data_ml);
2926 int meta_idx = multilist_get_random_index(meta_ml);
2927 multilist_sublist_t *data_mls;
2928 multilist_sublist_t *meta_mls;
2929 arc_buf_contents_t type;
2930 arc_buf_hdr_t *data_hdr;
2931 arc_buf_hdr_t *meta_hdr;
2932
2933 /*
2934 * We keep the sublist lock until we're finished, to prevent
2935 * the headers from being destroyed via arc_evict_state().
2936 */
2937 data_mls = multilist_sublist_lock(data_ml, data_idx);
2938 meta_mls = multilist_sublist_lock(meta_ml, meta_idx);
2939
2940 /*
2941 * These two loops are to ensure we skip any markers that
2942 * might be at the tail of the lists due to arc_evict_state().
2943 */
2944
2945 for (data_hdr = multilist_sublist_tail(data_mls); data_hdr != NULL;
2946 data_hdr = multilist_sublist_prev(data_mls, data_hdr)) {
2947 if (data_hdr->b_spa != 0)
2948 break;
2949 }
2950
2951 for (meta_hdr = multilist_sublist_tail(meta_mls); meta_hdr != NULL;
2952 meta_hdr = multilist_sublist_prev(meta_mls, meta_hdr)) {
2953 if (meta_hdr->b_spa != 0)
2954 break;
2955 }
2956
2957 if (data_hdr == NULL && meta_hdr == NULL) {
2958 type = ARC_BUFC_DATA;
2959 } else if (data_hdr == NULL) {
2960 ASSERT3P(meta_hdr, !=, NULL);
2961 type = ARC_BUFC_METADATA;
2962 } else if (meta_hdr == NULL) {
2963 ASSERT3P(data_hdr, !=, NULL);
2964 type = ARC_BUFC_DATA;
2965 } else {
2966 ASSERT3P(data_hdr, !=, NULL);
2967 ASSERT3P(meta_hdr, !=, NULL);
2968
2969 /* The headers can't be on the sublist without an L1 header */
2970 ASSERT(HDR_HAS_L1HDR(data_hdr));
2971 ASSERT(HDR_HAS_L1HDR(meta_hdr));
2972
2973 if (data_hdr->b_l1hdr.b_arc_access <
2974 meta_hdr->b_l1hdr.b_arc_access) {
2975 type = ARC_BUFC_DATA;
2976 } else {
2977 type = ARC_BUFC_METADATA;
2978 }
2979 }
2980
2981 multilist_sublist_unlock(meta_mls);
2982 multilist_sublist_unlock(data_mls);
2983
2984 return (type);
2985}
2986
2987/*
2988 * Evict buffers from the cache, such that arc_size is capped by arc_c.
2989 */
2990static uint64_t
2991arc_adjust(void)
2992{
2993 uint64_t total_evicted = 0;
2994 uint64_t bytes;
2995 int64_t target;
2996
2997 /*
2998 * If we're over arc_meta_limit, we want to correct that before
2999 * potentially evicting data buffers below.
3000 */
3001 total_evicted += arc_adjust_meta();
3002
3003 /*
3004 * Adjust MRU size
3005 *
3006 * If we're over the target cache size, we want to evict enough
3007 * from the list to get back to our target size. We don't want
3008 * to evict too much from the MRU, such that it drops below
3009 * arc_p. So, if we're over our target cache size more than
3010 * the MRU is over arc_p, we'll evict enough to get back to
3011 * arc_p here, and then evict more from the MFU below.
3012 */
3013 target = MIN((int64_t)(arc_size - arc_c),
36da08ef
PS
3014 (int64_t)(refcount_count(&arc_anon->arcs_size) +
3015 refcount_count(&arc_mru->arcs_size) + arc_meta_used - arc_p));
ca0bf58d
PS
3016
3017 /*
3018 * If we're below arc_meta_min, always prefer to evict data.
3019 * Otherwise, try to satisfy the requested number of bytes to
3020 * evict from the type which contains older buffers; in an
3021 * effort to keep newer buffers in the cache regardless of their
3022 * type. If we cannot satisfy the number of bytes from this
3023 * type, spill over into the next type.
3024 */
3025 if (arc_adjust_type(arc_mru) == ARC_BUFC_METADATA &&
3026 arc_meta_used > arc_meta_min) {
3027 bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA);
3028 total_evicted += bytes;
3029
3030 /*
3031 * If we couldn't evict our target number of bytes from
3032 * metadata, we try to get the rest from data.
3033 */
3034 target -= bytes;
3035
3036 total_evicted +=
3037 arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA);
3038 } else {
3039 bytes = arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_DATA);
3040 total_evicted += bytes;
3041
3042 /*
3043 * If we couldn't evict our target number of bytes from
3044 * data, we try to get the rest from metadata.
3045 */
3046 target -= bytes;
3047
3048 total_evicted +=
3049 arc_adjust_impl(arc_mru, 0, target, ARC_BUFC_METADATA);
3050 }
3051
3052 /*
3053 * Adjust MFU size
3054 *
3055 * Now that we've tried to evict enough from the MRU to get its
3056 * size back to arc_p, if we're still above the target cache
3057 * size, we evict the rest from the MFU.
3058 */
3059 target = arc_size - arc_c;
3060
a7b10a93 3061 if (arc_adjust_type(arc_mfu) == ARC_BUFC_METADATA &&
ca0bf58d
PS
3062 arc_meta_used > arc_meta_min) {
3063 bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA);
3064 total_evicted += bytes;
3065
3066 /*
3067 * If we couldn't evict our target number of bytes from
3068 * metadata, we try to get the rest from data.
3069 */
3070 target -= bytes;
3071
3072 total_evicted +=
3073 arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA);
3074 } else {
3075 bytes = arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_DATA);
3076 total_evicted += bytes;
3077
3078 /*
3079 * If we couldn't evict our target number of bytes from
3080 * data, we try to get the rest from data.
3081 */
3082 target -= bytes;
3083
3084 total_evicted +=
3085 arc_adjust_impl(arc_mfu, 0, target, ARC_BUFC_METADATA);
3086 }
3087
3088 /*
3089 * Adjust ghost lists
3090 *
3091 * In addition to the above, the ARC also defines target values
3092 * for the ghost lists. The sum of the mru list and mru ghost
3093 * list should never exceed the target size of the cache, and
3094 * the sum of the mru list, mfu list, mru ghost list, and mfu
3095 * ghost list should never exceed twice the target size of the
3096 * cache. The following logic enforces these limits on the ghost
3097 * caches, and evicts from them as needed.
3098 */
36da08ef
PS
3099 target = refcount_count(&arc_mru->arcs_size) +
3100 refcount_count(&arc_mru_ghost->arcs_size) - arc_c;
ca0bf58d
PS
3101
3102 bytes = arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_DATA);
3103 total_evicted += bytes;
3104
3105 target -= bytes;
3106
3107 total_evicted +=
3108 arc_adjust_impl(arc_mru_ghost, 0, target, ARC_BUFC_METADATA);
3109
3110 /*
3111 * We assume the sum of the mru list and mfu list is less than
3112 * or equal to arc_c (we enforced this above), which means we
3113 * can use the simpler of the two equations below:
3114 *
3115 * mru + mfu + mru ghost + mfu ghost <= 2 * arc_c
3116 * mru ghost + mfu ghost <= arc_c
3117 */
36da08ef
PS
3118 target = refcount_count(&arc_mru_ghost->arcs_size) +
3119 refcount_count(&arc_mfu_ghost->arcs_size) - arc_c;
ca0bf58d
PS
3120
3121 bytes = arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_DATA);
3122 total_evicted += bytes;
3123
3124 target -= bytes;
3125
3126 total_evicted +=
3127 arc_adjust_impl(arc_mfu_ghost, 0, target, ARC_BUFC_METADATA);
3128
3129 return (total_evicted);
3130}
3131
34dc7c2f
BB
3132static void
3133arc_do_user_evicts(void)
3134{
ca0bf58d 3135 mutex_enter(&arc_user_evicts_lock);
34dc7c2f
BB
3136 while (arc_eviction_list != NULL) {
3137 arc_buf_t *buf = arc_eviction_list;
3138 arc_eviction_list = buf->b_next;
428870ff 3139 mutex_enter(&buf->b_evict_lock);
34dc7c2f 3140 buf->b_hdr = NULL;
428870ff 3141 mutex_exit(&buf->b_evict_lock);
ca0bf58d 3142 mutex_exit(&arc_user_evicts_lock);
34dc7c2f
BB
3143
3144 if (buf->b_efunc != NULL)
bd089c54 3145 VERIFY0(buf->b_efunc(buf->b_private));
34dc7c2f
BB
3146
3147 buf->b_efunc = NULL;
3148 buf->b_private = NULL;
3149 kmem_cache_free(buf_cache, buf);
ca0bf58d 3150 mutex_enter(&arc_user_evicts_lock);
34dc7c2f 3151 }
ca0bf58d 3152 mutex_exit(&arc_user_evicts_lock);
34dc7c2f
BB
3153}
3154
ca0bf58d
PS
3155void
3156arc_flush(spa_t *spa, boolean_t retry)
ab26409d 3157{
ca0bf58d 3158 uint64_t guid = 0;
94520ca4 3159
bc888666 3160 /*
ca0bf58d
PS
3161 * If retry is TRUE, a spa must not be specified since we have
3162 * no good way to determine if all of a spa's buffers have been
3163 * evicted from an arc state.
bc888666 3164 */
ca0bf58d 3165 ASSERT(!retry || spa == 0);
d164b209 3166
b9541d6b 3167 if (spa != NULL)
3541dc6d 3168 guid = spa_load_guid(spa);
d164b209 3169
ca0bf58d
PS
3170 (void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry);
3171 (void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry);
3172
3173 (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry);
3174 (void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry);
3175
3176 (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry);
3177 (void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry);
34dc7c2f 3178
ca0bf58d
PS
3179 (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry);
3180 (void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry);
34dc7c2f 3181
34dc7c2f 3182 arc_do_user_evicts();
34dc7c2f
BB
3183 ASSERT(spa || arc_eviction_list == NULL);
3184}
3185
34dc7c2f 3186void
ca67b33a 3187arc_shrink(int64_t to_free)
34dc7c2f
BB
3188{
3189 if (arc_c > arc_c_min) {
302f753f 3190
34dc7c2f
BB
3191 if (arc_c > arc_c_min + to_free)
3192 atomic_add_64(&arc_c, -to_free);
3193 else
3194 arc_c = arc_c_min;
3195
ca67b33a 3196 atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
34dc7c2f
BB
3197 if (arc_c > arc_size)
3198 arc_c = MAX(arc_size, arc_c_min);
3199 if (arc_p > arc_c)
3200 arc_p = (arc_c >> 1);
3201 ASSERT(arc_c >= arc_c_min);
3202 ASSERT((int64_t)arc_p >= 0);
3203 }
3204
3205 if (arc_size > arc_c)
ca0bf58d 3206 (void) arc_adjust();
34dc7c2f
BB
3207}
3208
ca67b33a
MA
3209typedef enum free_memory_reason_t {
3210 FMR_UNKNOWN,
3211 FMR_NEEDFREE,
3212 FMR_LOTSFREE,
3213 FMR_SWAPFS_MINFREE,
3214 FMR_PAGES_PP_MAXIMUM,
3215 FMR_HEAP_ARENA,
3216 FMR_ZIO_ARENA,
3217} free_memory_reason_t;
3218
3219int64_t last_free_memory;
3220free_memory_reason_t last_free_reason;
3221
3222#ifdef _KERNEL
ca67b33a
MA
3223/*
3224 * Additional reserve of pages for pp_reserve.
3225 */
3226int64_t arc_pages_pp_reserve = 64;
3227
3228/*
3229 * Additional reserve of pages for swapfs.
3230 */
3231int64_t arc_swapfs_reserve = 64;
ca67b33a
MA
3232#endif /* _KERNEL */
3233
3234/*
3235 * Return the amount of memory that can be consumed before reclaim will be
3236 * needed. Positive if there is sufficient free memory, negative indicates
3237 * the amount of memory that needs to be freed up.
3238 */
3239static int64_t
3240arc_available_memory(void)
3241{
3242 int64_t lowest = INT64_MAX;
3243 free_memory_reason_t r = FMR_UNKNOWN;
ca67b33a 3244#ifdef _KERNEL
ca67b33a 3245 int64_t n;
11f552fa
BB
3246#ifdef __linux__
3247 pgcnt_t needfree = btop(arc_need_free);
3248 pgcnt_t lotsfree = btop(arc_sys_free);
3249 pgcnt_t desfree = 0;
3250#endif
ca67b33a 3251
ca67b33a
MA
3252 if (needfree > 0) {
3253 n = PAGESIZE * (-needfree);
3254 if (n < lowest) {
3255 lowest = n;
3256 r = FMR_NEEDFREE;
3257 }
3258 }
3259
3260 /*
3261 * check that we're out of range of the pageout scanner. It starts to
3262 * schedule paging if freemem is less than lotsfree and needfree.
3263 * lotsfree is the high-water mark for pageout, and needfree is the
3264 * number of needed free pages. We add extra pages here to make sure
3265 * the scanner doesn't start up while we're freeing memory.
3266 */
3267 n = PAGESIZE * (freemem - lotsfree - needfree - desfree);
3268 if (n < lowest) {
3269 lowest = n;
3270 r = FMR_LOTSFREE;
3271 }
3272
11f552fa 3273#ifndef __linux__
ca67b33a
MA
3274 /*
3275 * check to make sure that swapfs has enough space so that anon
3276 * reservations can still succeed. anon_resvmem() checks that the
3277 * availrmem is greater than swapfs_minfree, and the number of reserved
3278 * swap pages. We also add a bit of extra here just to prevent
3279 * circumstances from getting really dire.
3280 */
3281 n = PAGESIZE * (availrmem - swapfs_minfree - swapfs_reserve -
3282 desfree - arc_swapfs_reserve);
3283 if (n < lowest) {
3284 lowest = n;
3285 r = FMR_SWAPFS_MINFREE;
3286 }
3287
3288
3289 /*
3290 * Check that we have enough availrmem that memory locking (e.g., via
3291 * mlock(3C) or memcntl(2)) can still succeed. (pages_pp_maximum
3292 * stores the number of pages that cannot be locked; when availrmem
3293 * drops below pages_pp_maximum, page locking mechanisms such as
3294 * page_pp_lock() will fail.)
3295 */
3296 n = PAGESIZE * (availrmem - pages_pp_maximum -
3297 arc_pages_pp_reserve);
3298 if (n < lowest) {
3299 lowest = n;
3300 r = FMR_PAGES_PP_MAXIMUM;
3301 }
11f552fa 3302#endif
ca67b33a
MA
3303
3304#if defined(__i386)
3305 /*
3306 * If we're on an i386 platform, it's possible that we'll exhaust the
3307 * kernel heap space before we ever run out of available physical
3308 * memory. Most checks of the size of the heap_area compare against
3309 * tune.t_minarmem, which is the minimum available real memory that we
3310 * can have in the system. However, this is generally fixed at 25 pages
3311 * which is so low that it's useless. In this comparison, we seek to
3312 * calculate the total heap-size, and reclaim if more than 3/4ths of the
3313 * heap is allocated. (Or, in the calculation, if less than 1/4th is
3314 * free)
3315 */
3316 n = vmem_size(heap_arena, VMEM_FREE) -
3317 (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2);
3318 if (n < lowest) {
3319 lowest = n;
3320 r = FMR_HEAP_ARENA;
3321 }
3322#endif
3323
3324 /*
3325 * If zio data pages are being allocated out of a separate heap segment,
3326 * then enforce that the size of available vmem for this arena remains
3327 * above about 1/16th free.
3328 *
3329 * Note: The 1/16th arena free requirement was put in place
3330 * to aggressively evict memory from the arc in order to avoid
3331 * memory fragmentation issues.
3332 */
3333 if (zio_arena != NULL) {
3334 n = vmem_size(zio_arena, VMEM_FREE) -
3335 (vmem_size(zio_arena, VMEM_ALLOC) >> 4);
3336 if (n < lowest) {
3337 lowest = n;
3338 r = FMR_ZIO_ARENA;
3339 }
3340 }
11f552fa 3341#else /* _KERNEL */
ca67b33a
MA
3342 /* Every 100 calls, free a small amount */
3343 if (spa_get_random(100) == 0)
3344 lowest = -1024;
11f552fa 3345#endif /* _KERNEL */
ca67b33a
MA
3346
3347 last_free_memory = lowest;
3348 last_free_reason = r;
3349
3350 return (lowest);
3351}
3352
3353/*
3354 * Determine if the system is under memory pressure and is asking
3355 * to reclaim memory. A return value of TRUE indicates that the system
3356 * is under memory pressure and that the arc should adjust accordingly.
3357 */
3358static boolean_t
3359arc_reclaim_needed(void)
3360{
3361 return (arc_available_memory() < 0);
3362}
3363
34dc7c2f 3364static void
ca67b33a 3365arc_kmem_reap_now(void)
34dc7c2f
BB
3366{
3367 size_t i;
3368 kmem_cache_t *prev_cache = NULL;
3369 kmem_cache_t *prev_data_cache = NULL;
3370 extern kmem_cache_t *zio_buf_cache[];
3371 extern kmem_cache_t *zio_data_buf_cache[];
669dedb3 3372 extern kmem_cache_t *range_seg_cache;
34dc7c2f 3373
f6046738
BB
3374 if ((arc_meta_used >= arc_meta_limit) && zfs_arc_meta_prune) {
3375 /*
3376 * We are exceeding our meta-data cache limit.
3377 * Prune some entries to release holds on meta-data.
3378 */
3379 arc_prune(zfs_arc_meta_prune);
3380 }
3381
34dc7c2f
BB
3382 for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
3383 if (zio_buf_cache[i] != prev_cache) {
3384 prev_cache = zio_buf_cache[i];
3385 kmem_cache_reap_now(zio_buf_cache[i]);
3386 }
3387 if (zio_data_buf_cache[i] != prev_data_cache) {
3388 prev_data_cache = zio_data_buf_cache[i];
3389 kmem_cache_reap_now(zio_data_buf_cache[i]);
3390 }
3391 }
ca0bf58d 3392 kmem_cache_reap_now(buf_cache);
b9541d6b
CW
3393 kmem_cache_reap_now(hdr_full_cache);
3394 kmem_cache_reap_now(hdr_l2only_cache);
669dedb3 3395 kmem_cache_reap_now(range_seg_cache);
ca67b33a
MA
3396
3397 if (zio_arena != NULL) {
3398 /*
3399 * Ask the vmem arena to reclaim unused memory from its
3400 * quantum caches.
3401 */
3402 vmem_qcache_reap(zio_arena);
3403 }
34dc7c2f
BB
3404}
3405
302f753f 3406/*
ca0bf58d
PS
3407 * Threads can block in arc_get_data_buf() waiting for this thread to evict
3408 * enough data and signal them to proceed. When this happens, the threads in
3409 * arc_get_data_buf() are sleeping while holding the hash lock for their
3410 * particular arc header. Thus, we must be careful to never sleep on a
3411 * hash lock in this thread. This is to prevent the following deadlock:
3412 *
3413 * - Thread A sleeps on CV in arc_get_data_buf() holding hash lock "L",
3414 * waiting for the reclaim thread to signal it.
3415 *
3416 * - arc_reclaim_thread() tries to acquire hash lock "L" using mutex_enter,
3417 * fails, and goes to sleep forever.
3418 *
3419 * This possible deadlock is avoided by always acquiring a hash lock
3420 * using mutex_tryenter() from arc_reclaim_thread().
302f753f 3421 */
34dc7c2f 3422static void
ca67b33a 3423arc_reclaim_thread(void)
34dc7c2f 3424{
ca67b33a
MA
3425 fstrans_cookie_t cookie = spl_fstrans_mark();
3426 clock_t growtime = 0;
34dc7c2f
BB
3427 callb_cpr_t cpr;
3428
ca0bf58d 3429 CALLB_CPR_INIT(&cpr, &arc_reclaim_lock, callb_generic_cpr, FTAG);
34dc7c2f 3430
ca0bf58d 3431 mutex_enter(&arc_reclaim_lock);
ca67b33a
MA
3432 while (!arc_reclaim_thread_exit) {
3433 int64_t to_free;
3434 int64_t free_memory = arc_available_memory();
3435 uint64_t evicted = 0;
302f753f 3436
ca67b33a 3437 arc_tuning_update();
34dc7c2f 3438
ca67b33a 3439 mutex_exit(&arc_reclaim_lock);
34dc7c2f 3440
ca67b33a 3441 if (free_memory < 0) {
34dc7c2f 3442
ca67b33a 3443 arc_no_grow = B_TRUE;
b128c09f 3444 arc_warm = B_TRUE;
34dc7c2f 3445
ca67b33a
MA
3446 /*
3447 * Wait at least zfs_grow_retry (default 5) seconds
3448 * before considering growing.
3449 */
3450 growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
6a8f9b6b 3451
ca67b33a 3452 arc_kmem_reap_now();
34dc7c2f 3453
ca67b33a
MA
3454 /*
3455 * If we are still low on memory, shrink the ARC
3456 * so that we have arc_shrink_min free space.
3457 */
3458 free_memory = arc_available_memory();
34dc7c2f 3459
ca67b33a
MA
3460 to_free = (arc_c >> arc_shrink_shift) - free_memory;
3461 if (to_free > 0) {
3462#ifdef _KERNEL
11f552fa 3463 to_free = MAX(to_free, arc_need_free);
ca67b33a
MA
3464#endif
3465 arc_shrink(to_free);
3466 }
3467 } else if (free_memory < arc_c >> arc_no_grow_shift) {
3468 arc_no_grow = B_TRUE;
3469 } else if (ddi_get_lbolt() >= growtime) {
3470 arc_no_grow = B_FALSE;
3471 }
bce45ec9 3472
ca67b33a 3473 evicted = arc_adjust();
bce45ec9 3474
ca67b33a 3475 mutex_enter(&arc_reclaim_lock);
bce45ec9 3476
ca67b33a
MA
3477 /*
3478 * If evicted is zero, we couldn't evict anything via
3479 * arc_adjust(). This could be due to hash lock
3480 * collisions, but more likely due to the majority of
3481 * arc buffers being unevictable. Therefore, even if
3482 * arc_size is above arc_c, another pass is unlikely to
3483 * be helpful and could potentially cause us to enter an
3484 * infinite loop.
3485 */
3486 if (arc_size <= arc_c || evicted == 0) {
3487 /*
3488 * We're either no longer overflowing, or we
3489 * can't evict anything more, so we should wake
11f552fa
BB
3490 * up any threads before we go to sleep and clear
3491 * arc_need_free since nothing more can be done.
ca67b33a
MA
3492 */
3493 cv_broadcast(&arc_reclaim_waiters_cv);
11f552fa 3494 arc_need_free = 0;
bce45ec9 3495
ca67b33a
MA
3496 /*
3497 * Block until signaled, or after one second (we
3498 * might need to perform arc_kmem_reap_now()
3499 * even if we aren't being signalled)
3500 */
3501 CALLB_CPR_SAFE_BEGIN(&cpr);
3502 (void) cv_timedwait_sig(&arc_reclaim_thread_cv,
3503 &arc_reclaim_lock, ddi_get_lbolt() + hz);
3504 CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_lock);
3505 }
ca0bf58d 3506 }
bce45ec9 3507
ca67b33a 3508 arc_reclaim_thread_exit = FALSE;
ca0bf58d
PS
3509 cv_broadcast(&arc_reclaim_thread_cv);
3510 CALLB_CPR_EXIT(&cpr); /* drops arc_reclaim_lock */
3511 spl_fstrans_unmark(cookie);
3512 thread_exit();
3513}
3514
3515static void
3516arc_user_evicts_thread(void)
3517{
ca67b33a 3518 fstrans_cookie_t cookie = spl_fstrans_mark();
ca0bf58d 3519 callb_cpr_t cpr;
bce45ec9 3520
ca0bf58d 3521 CALLB_CPR_INIT(&cpr, &arc_user_evicts_lock, callb_generic_cpr, FTAG);
bce45ec9 3522
ca0bf58d
PS
3523 mutex_enter(&arc_user_evicts_lock);
3524 while (!arc_user_evicts_thread_exit) {
3525 mutex_exit(&arc_user_evicts_lock);
3526
3527 arc_do_user_evicts();
3528
3529 /*
3530 * This is necessary in order for the mdb ::arc dcmd to
3531 * show up to date information. Since the ::arc command
3532 * does not call the kstat's update function, without
3533 * this call, the command may show stale stats for the
3534 * anon, mru, mru_ghost, mfu, and mfu_ghost lists. Even
3535 * with this change, the data might be up to 1 second
3536 * out of date; but that should suffice. The arc_state_t
3537 * structures can be queried directly if more accurate
3538 * information is needed.
3539 */
3540 if (arc_ksp != NULL)
3541 arc_ksp->ks_update(arc_ksp, KSTAT_READ);
3542
3543 mutex_enter(&arc_user_evicts_lock);
3544
3545 /*
3546 * Block until signaled, or after one second (we need to
3547 * call the arc's kstat update function regularly).
3548 */
3549 CALLB_CPR_SAFE_BEGIN(&cpr);
b64ccd6c 3550 (void) cv_timedwait_sig(&arc_user_evicts_cv,
ca0bf58d
PS
3551 &arc_user_evicts_lock, ddi_get_lbolt() + hz);
3552 CALLB_CPR_SAFE_END(&cpr, &arc_user_evicts_lock);
34dc7c2f
BB
3553 }
3554
ca0bf58d
PS
3555 arc_user_evicts_thread_exit = FALSE;
3556 cv_broadcast(&arc_user_evicts_cv);
3557 CALLB_CPR_EXIT(&cpr); /* drops arc_user_evicts_lock */
40d06e3c 3558 spl_fstrans_unmark(cookie);
34dc7c2f
BB
3559 thread_exit();
3560}
3561
7cb67b45
BB
3562#ifdef _KERNEL
3563/*
302f753f
BB
3564 * Determine the amount of memory eligible for eviction contained in the
3565 * ARC. All clean data reported by the ghost lists can always be safely
3566 * evicted. Due to arc_c_min, the same does not hold for all clean data
3567 * contained by the regular mru and mfu lists.
3568 *
3569 * In the case of the regular mru and mfu lists, we need to report as
3570 * much clean data as possible, such that evicting that same reported
3571 * data will not bring arc_size below arc_c_min. Thus, in certain
3572 * circumstances, the total amount of clean data in the mru and mfu
3573 * lists might not actually be evictable.
3574 *
3575 * The following two distinct cases are accounted for:
3576 *
3577 * 1. The sum of the amount of dirty data contained by both the mru and
3578 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
3579 * is greater than or equal to arc_c_min.
3580 * (i.e. amount of dirty data >= arc_c_min)
3581 *
3582 * This is the easy case; all clean data contained by the mru and mfu
3583 * lists is evictable. Evicting all clean data can only drop arc_size
3584 * to the amount of dirty data, which is greater than arc_c_min.
3585 *
3586 * 2. The sum of the amount of dirty data contained by both the mru and
3587 * mfu lists, plus the ARC's other accounting (e.g. the anon list),
3588 * is less than arc_c_min.
3589 * (i.e. arc_c_min > amount of dirty data)
3590 *
3591 * 2.1. arc_size is greater than or equal arc_c_min.
3592 * (i.e. arc_size >= arc_c_min > amount of dirty data)
3593 *
3594 * In this case, not all clean data from the regular mru and mfu
3595 * lists is actually evictable; we must leave enough clean data
3596 * to keep arc_size above arc_c_min. Thus, the maximum amount of
3597 * evictable data from the two lists combined, is exactly the
3598 * difference between arc_size and arc_c_min.
3599 *
3600 * 2.2. arc_size is less than arc_c_min
3601 * (i.e. arc_c_min > arc_size > amount of dirty data)
3602 *
3603 * In this case, none of the data contained in the mru and mfu
3604 * lists is evictable, even if it's clean. Since arc_size is
3605 * already below arc_c_min, evicting any more would only
3606 * increase this negative difference.
7cb67b45 3607 */
302f753f
BB
3608static uint64_t
3609arc_evictable_memory(void) {
3610 uint64_t arc_clean =
3611 arc_mru->arcs_lsize[ARC_BUFC_DATA] +
3612 arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
3613 arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
3614 arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
3615 uint64_t ghost_clean =
3616 arc_mru_ghost->arcs_lsize[ARC_BUFC_DATA] +
3617 arc_mru_ghost->arcs_lsize[ARC_BUFC_METADATA] +
3618 arc_mfu_ghost->arcs_lsize[ARC_BUFC_DATA] +
3619 arc_mfu_ghost->arcs_lsize[ARC_BUFC_METADATA];
3620 uint64_t arc_dirty = MAX((int64_t)arc_size - (int64_t)arc_clean, 0);
3621
3622 if (arc_dirty >= arc_c_min)
3623 return (ghost_clean + arc_clean);
3624
3625 return (ghost_clean + MAX((int64_t)arc_size - (int64_t)arc_c_min, 0));
3626}
3627
ed6e9cc2
TC
3628/*
3629 * If sc->nr_to_scan is zero, the caller is requesting a query of the
3630 * number of objects which can potentially be freed. If it is nonzero,
3631 * the request is to free that many objects.
3632 *
3633 * Linux kernels >= 3.12 have the count_objects and scan_objects callbacks
3634 * in struct shrinker and also require the shrinker to return the number
3635 * of objects freed.
3636 *
3637 * Older kernels require the shrinker to return the number of freeable
3638 * objects following the freeing of nr_to_free.
3639 */
3640static spl_shrinker_t
7e7baeca 3641__arc_shrinker_func(struct shrinker *shrink, struct shrink_control *sc)
7cb67b45 3642{
ed6e9cc2 3643 int64_t pages;
7cb67b45 3644
302f753f
BB
3645 /* The arc is considered warm once reclaim has occurred */
3646 if (unlikely(arc_warm == B_FALSE))
3647 arc_warm = B_TRUE;
7cb67b45 3648
302f753f 3649 /* Return the potential number of reclaimable pages */
ed6e9cc2 3650 pages = btop((int64_t)arc_evictable_memory());
302f753f
BB
3651 if (sc->nr_to_scan == 0)
3652 return (pages);
3fd70ee6
BB
3653
3654 /* Not allowed to perform filesystem reclaim */
7e7baeca 3655 if (!(sc->gfp_mask & __GFP_FS))
ed6e9cc2 3656 return (SHRINK_STOP);
3fd70ee6 3657
7cb67b45 3658 /* Reclaim in progress */
ca0bf58d 3659 if (mutex_tryenter(&arc_reclaim_lock) == 0)
ed6e9cc2 3660 return (SHRINK_STOP);
7cb67b45 3661
ca0bf58d
PS
3662 mutex_exit(&arc_reclaim_lock);
3663
302f753f
BB
3664 /*
3665 * Evict the requested number of pages by shrinking arc_c the
3666 * requested amount. If there is nothing left to evict just
3667 * reap whatever we can from the various arc slabs.
3668 */
3669 if (pages > 0) {
ca67b33a
MA
3670 arc_shrink(ptob(sc->nr_to_scan));
3671 arc_kmem_reap_now();
ed6e9cc2
TC
3672#ifdef HAVE_SPLIT_SHRINKER_CALLBACK
3673 pages = MAX(pages - btop(arc_evictable_memory()), 0);
3674#else
1e3cb67b 3675 pages = btop(arc_evictable_memory());
ed6e9cc2 3676#endif
302f753f 3677 } else {
ca67b33a 3678 arc_kmem_reap_now();
ed6e9cc2 3679 pages = SHRINK_STOP;
302f753f
BB
3680 }
3681
ca0bf58d
PS
3682 /*
3683 * We've reaped what we can, wake up threads.
3684 */
3685 cv_broadcast(&arc_reclaim_waiters_cv);
3686
302f753f
BB
3687 /*
3688 * When direct reclaim is observed it usually indicates a rapid
3689 * increase in memory pressure. This occurs because the kswapd
3690 * threads were unable to asynchronously keep enough free memory
3691 * available. In this case set arc_no_grow to briefly pause arc
3692 * growth to avoid compounding the memory pressure.
3693 */
7cb67b45 3694 if (current_is_kswapd()) {
302f753f 3695 ARCSTAT_BUMP(arcstat_memory_indirect_count);
7cb67b45 3696 } else {
302f753f 3697 arc_no_grow = B_TRUE;
11f552fa 3698 arc_need_free = ptob(sc->nr_to_scan);
302f753f 3699 ARCSTAT_BUMP(arcstat_memory_direct_count);
7cb67b45
BB
3700 }
3701
1e3cb67b 3702 return (pages);
7cb67b45 3703}
7e7baeca 3704SPL_SHRINKER_CALLBACK_WRAPPER(arc_shrinker_func);
7cb67b45
BB
3705
3706SPL_SHRINKER_DECLARE(arc_shrinker, arc_shrinker_func, DEFAULT_SEEKS);
3707#endif /* _KERNEL */
3708
34dc7c2f
BB
3709/*
3710 * Adapt arc info given the number of bytes we are trying to add and
3711 * the state that we are comming from. This function is only called
3712 * when we are adding new content to the cache.
3713 */
3714static void
3715arc_adapt(int bytes, arc_state_t *state)
3716{
3717 int mult;
728d6ae9 3718 uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
36da08ef
PS
3719 int64_t mrug_size = refcount_count(&arc_mru_ghost->arcs_size);
3720 int64_t mfug_size = refcount_count(&arc_mfu_ghost->arcs_size);
34dc7c2f
BB
3721
3722 if (state == arc_l2c_only)
3723 return;
3724
3725 ASSERT(bytes > 0);
3726 /*
3727 * Adapt the target size of the MRU list:
3728 * - if we just hit in the MRU ghost list, then increase
3729 * the target size of the MRU list.
3730 * - if we just hit in the MFU ghost list, then increase
3731 * the target size of the MFU list by decreasing the
3732 * target size of the MRU list.
3733 */
3734 if (state == arc_mru_ghost) {
36da08ef 3735 mult = (mrug_size >= mfug_size) ? 1 : (mfug_size / mrug_size);
62422785
PS
3736 if (!zfs_arc_p_dampener_disable)
3737 mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
34dc7c2f 3738
728d6ae9 3739 arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
34dc7c2f 3740 } else if (state == arc_mfu_ghost) {
d164b209
BB
3741 uint64_t delta;
3742
36da08ef 3743 mult = (mfug_size >= mrug_size) ? 1 : (mrug_size / mfug_size);
62422785
PS
3744 if (!zfs_arc_p_dampener_disable)
3745 mult = MIN(mult, 10);
34dc7c2f 3746
d164b209 3747 delta = MIN(bytes * mult, arc_p);
728d6ae9 3748 arc_p = MAX(arc_p_min, arc_p - delta);
34dc7c2f
BB
3749 }
3750 ASSERT((int64_t)arc_p >= 0);
3751
ca67b33a
MA
3752 if (arc_reclaim_needed()) {
3753 cv_signal(&arc_reclaim_thread_cv);
3754 return;
3755 }
3756
34dc7c2f
BB
3757 if (arc_no_grow)
3758 return;
3759
3760 if (arc_c >= arc_c_max)
3761 return;
3762
3763 /*
3764 * If we're within (2 * maxblocksize) bytes of the target
3765 * cache size, increment the target cache size
3766 */
121b3cae
TC
3767 VERIFY3U(arc_c, >=, 2ULL << SPA_MAXBLOCKSHIFT);
3768 if (arc_size >= arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
34dc7c2f
BB
3769 atomic_add_64(&arc_c, (int64_t)bytes);
3770 if (arc_c > arc_c_max)
3771 arc_c = arc_c_max;
3772 else if (state == arc_anon)
3773 atomic_add_64(&arc_p, (int64_t)bytes);
3774 if (arc_p > arc_c)
3775 arc_p = arc_c;
3776 }
3777 ASSERT((int64_t)arc_p >= 0);
3778}
3779
3780/*
ca0bf58d
PS
3781 * Check if arc_size has grown past our upper threshold, determined by
3782 * zfs_arc_overflow_shift.
34dc7c2f 3783 */
ca0bf58d
PS
3784static boolean_t
3785arc_is_overflowing(void)
34dc7c2f 3786{
ca0bf58d
PS
3787 /* Always allow at least one block of overflow */
3788 uint64_t overflow = MAX(SPA_MAXBLOCKSIZE,
3789 arc_c >> zfs_arc_overflow_shift);
34dc7c2f 3790
ca0bf58d 3791 return (arc_size >= arc_c + overflow);
34dc7c2f
BB
3792}
3793
3794/*
ca0bf58d
PS
3795 * The buffer, supplied as the first argument, needs a data block. If we
3796 * are hitting the hard limit for the cache size, we must sleep, waiting
3797 * for the eviction thread to catch up. If we're past the target size
3798 * but below the hard limit, we'll only signal the reclaim thread and
3799 * continue on.
34dc7c2f
BB
3800 */
3801static void
3802arc_get_data_buf(arc_buf_t *buf)
3803{
b9541d6b 3804 arc_state_t *state = buf->b_hdr->b_l1hdr.b_state;
34dc7c2f 3805 uint64_t size = buf->b_hdr->b_size;
b9541d6b 3806 arc_buf_contents_t type = arc_buf_type(buf->b_hdr);
34dc7c2f
BB
3807
3808 arc_adapt(size, state);
3809
3810 /*
ca0bf58d
PS
3811 * If arc_size is currently overflowing, and has grown past our
3812 * upper limit, we must be adding data faster than the evict
3813 * thread can evict. Thus, to ensure we don't compound the
3814 * problem by adding more data and forcing arc_size to grow even
3815 * further past it's target size, we halt and wait for the
3816 * eviction thread to catch up.
3817 *
3818 * It's also possible that the reclaim thread is unable to evict
3819 * enough buffers to get arc_size below the overflow limit (e.g.
3820 * due to buffers being un-evictable, or hash lock collisions).
3821 * In this case, we want to proceed regardless if we're
3822 * overflowing; thus we don't use a while loop here.
34dc7c2f 3823 */
ca0bf58d
PS
3824 if (arc_is_overflowing()) {
3825 mutex_enter(&arc_reclaim_lock);
3826
3827 /*
3828 * Now that we've acquired the lock, we may no longer be
3829 * over the overflow limit, lets check.
3830 *
3831 * We're ignoring the case of spurious wake ups. If that
3832 * were to happen, it'd let this thread consume an ARC
3833 * buffer before it should have (i.e. before we're under
3834 * the overflow limit and were signalled by the reclaim
3835 * thread). As long as that is a rare occurrence, it
3836 * shouldn't cause any harm.
3837 */
3838 if (arc_is_overflowing()) {
3839 cv_signal(&arc_reclaim_thread_cv);
3840 cv_wait(&arc_reclaim_waiters_cv, &arc_reclaim_lock);
34dc7c2f 3841 }
34dc7c2f 3842
ca0bf58d 3843 mutex_exit(&arc_reclaim_lock);
34dc7c2f 3844 }
ab26409d 3845
da8ccd0e 3846 if (type == ARC_BUFC_METADATA) {
ca0bf58d
PS
3847 buf->b_data = zio_buf_alloc(size);
3848 arc_space_consume(size, ARC_SPACE_META);
3849 } else {
3850 ASSERT(type == ARC_BUFC_DATA);
3851 buf->b_data = zio_data_buf_alloc(size);
3852 arc_space_consume(size, ARC_SPACE_DATA);
da8ccd0e
PS
3853 }
3854
34dc7c2f
BB
3855 /*
3856 * Update the state size. Note that ghost states have a
3857 * "ghost size" and so don't need to be updated.
3858 */
b9541d6b 3859 if (!GHOST_STATE(buf->b_hdr->b_l1hdr.b_state)) {
34dc7c2f 3860 arc_buf_hdr_t *hdr = buf->b_hdr;
36da08ef 3861 arc_state_t *state = hdr->b_l1hdr.b_state;
34dc7c2f 3862
36da08ef 3863 (void) refcount_add_many(&state->arcs_size, size, buf);
ca0bf58d
PS
3864
3865 /*
3866 * If this is reached via arc_read, the link is
3867 * protected by the hash lock. If reached via
3868 * arc_buf_alloc, the header should not be accessed by
3869 * any other thread. And, if reached via arc_read_done,
3870 * the hash lock will protect it if it's found in the
3871 * hash table; otherwise no other thread should be
3872 * trying to [add|remove]_reference it.
3873 */
3874 if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
b9541d6b
CW
3875 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
3876 atomic_add_64(&hdr->b_l1hdr.b_state->arcs_lsize[type],
3877 size);
34dc7c2f
BB
3878 }
3879 /*
3880 * If we are growing the cache, and we are adding anonymous
3881 * data, and we have outgrown arc_p, update arc_p
3882 */
ca0bf58d 3883 if (arc_size < arc_c && hdr->b_l1hdr.b_state == arc_anon &&
36da08ef
PS
3884 (refcount_count(&arc_anon->arcs_size) +
3885 refcount_count(&arc_mru->arcs_size) > arc_p))
34dc7c2f
BB
3886 arc_p = MIN(arc_c, arc_p + size);
3887 }
3888}
3889
3890/*
3891 * This routine is called whenever a buffer is accessed.
3892 * NOTE: the hash lock is dropped in this function.
3893 */
3894static void
2a432414 3895arc_access(arc_buf_hdr_t *hdr, kmutex_t *hash_lock)
34dc7c2f 3896{
428870ff
BB
3897 clock_t now;
3898
34dc7c2f 3899 ASSERT(MUTEX_HELD(hash_lock));
b9541d6b 3900 ASSERT(HDR_HAS_L1HDR(hdr));
34dc7c2f 3901
b9541d6b 3902 if (hdr->b_l1hdr.b_state == arc_anon) {
34dc7c2f
BB
3903 /*
3904 * This buffer is not in the cache, and does not
3905 * appear in our "ghost" list. Add the new buffer
3906 * to the MRU state.
3907 */
3908
b9541d6b
CW
3909 ASSERT0(hdr->b_l1hdr.b_arc_access);
3910 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt();
2a432414
GW
3911 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr);
3912 arc_change_state(arc_mru, hdr, hash_lock);
34dc7c2f 3913
b9541d6b 3914 } else if (hdr->b_l1hdr.b_state == arc_mru) {
428870ff
BB
3915 now = ddi_get_lbolt();
3916
34dc7c2f
BB
3917 /*
3918 * If this buffer is here because of a prefetch, then either:
3919 * - clear the flag if this is a "referencing" read
3920 * (any subsequent access will bump this into the MFU state).
3921 * or
3922 * - move the buffer to the head of the list if this is
3923 * another prefetch (to make it less likely to be evicted).
3924 */
b9541d6b
CW
3925 if (HDR_PREFETCH(hdr)) {
3926 if (refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) {
ca0bf58d
PS
3927 /* link protected by hash lock */
3928 ASSERT(multilist_link_active(
b9541d6b 3929 &hdr->b_l1hdr.b_arc_node));
34dc7c2f 3930 } else {
2a432414 3931 hdr->b_flags &= ~ARC_FLAG_PREFETCH;
b9541d6b 3932 atomic_inc_32(&hdr->b_l1hdr.b_mru_hits);
34dc7c2f
BB
3933 ARCSTAT_BUMP(arcstat_mru_hits);
3934 }
b9541d6b 3935 hdr->b_l1hdr.b_arc_access = now;
34dc7c2f
BB
3936 return;
3937 }
3938
3939 /*
3940 * This buffer has been "accessed" only once so far,
3941 * but it is still in the cache. Move it to the MFU
3942 * state.
3943 */
b9541d6b
CW
3944 if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access +
3945 ARC_MINTIME)) {
34dc7c2f
BB
3946 /*
3947 * More than 125ms have passed since we
3948 * instantiated this buffer. Move it to the
3949 * most frequently used state.
3950 */
b9541d6b 3951 hdr->b_l1hdr.b_arc_access = now;
2a432414
GW
3952 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
3953 arc_change_state(arc_mfu, hdr, hash_lock);
34dc7c2f 3954 }
b9541d6b 3955 atomic_inc_32(&hdr->b_l1hdr.b_mru_hits);
34dc7c2f 3956 ARCSTAT_BUMP(arcstat_mru_hits);
b9541d6b 3957 } else if (hdr->b_l1hdr.b_state == arc_mru_ghost) {
34dc7c2f
BB
3958 arc_state_t *new_state;
3959 /*
3960 * This buffer has been "accessed" recently, but
3961 * was evicted from the cache. Move it to the
3962 * MFU state.
3963 */
3964
b9541d6b 3965 if (HDR_PREFETCH(hdr)) {
34dc7c2f 3966 new_state = arc_mru;
b9541d6b 3967 if (refcount_count(&hdr->b_l1hdr.b_refcnt) > 0)
2a432414
GW
3968 hdr->b_flags &= ~ARC_FLAG_PREFETCH;
3969 DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr);
34dc7c2f
BB
3970 } else {
3971 new_state = arc_mfu;
2a432414 3972 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
34dc7c2f
BB
3973 }
3974
b9541d6b 3975 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt();
2a432414 3976 arc_change_state(new_state, hdr, hash_lock);
34dc7c2f 3977
b9541d6b 3978 atomic_inc_32(&hdr->b_l1hdr.b_mru_ghost_hits);
34dc7c2f 3979 ARCSTAT_BUMP(arcstat_mru_ghost_hits);
b9541d6b 3980 } else if (hdr->b_l1hdr.b_state == arc_mfu) {
34dc7c2f
BB
3981 /*
3982 * This buffer has been accessed more than once and is
3983 * still in the cache. Keep it in the MFU state.
3984 *
3985 * NOTE: an add_reference() that occurred when we did
3986 * the arc_read() will have kicked this off the list.
3987 * If it was a prefetch, we will explicitly move it to
3988 * the head of the list now.
3989 */
b9541d6b
CW
3990 if ((HDR_PREFETCH(hdr)) != 0) {
3991 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
ca0bf58d
PS
3992 /* link protected by hash_lock */
3993 ASSERT(multilist_link_active(&hdr->b_l1hdr.b_arc_node));
34dc7c2f 3994 }
b9541d6b 3995 atomic_inc_32(&hdr->b_l1hdr.b_mfu_hits);
34dc7c2f 3996 ARCSTAT_BUMP(arcstat_mfu_hits);
b9541d6b
CW
3997 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt();
3998 } else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) {
34dc7c2f
BB
3999 arc_state_t *new_state = arc_mfu;
4000 /*
4001 * This buffer has been accessed more than once but has
4002 * been evicted from the cache. Move it back to the
4003 * MFU state.
4004 */
4005
b9541d6b 4006 if (HDR_PREFETCH(hdr)) {
34dc7c2f
BB
4007 /*
4008 * This is a prefetch access...
4009 * move this block back to the MRU state.
4010 */
b9541d6b 4011 ASSERT0(refcount_count(&hdr->b_l1hdr.b_refcnt));
34dc7c2f
BB
4012 new_state = arc_mru;
4013 }
4014
b9541d6b 4015 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt();
2a432414
GW
4016 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
4017 arc_change_state(new_state, hdr, hash_lock);
34dc7c2f 4018
b9541d6b 4019 atomic_inc_32(&hdr->b_l1hdr.b_mfu_ghost_hits);
34dc7c2f 4020 ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
b9541d6b 4021 } else if (hdr->b_l1hdr.b_state == arc_l2c_only) {
34dc7c2f
BB
4022 /*
4023 * This buffer is on the 2nd Level ARC.
4024 */
4025
b9541d6b 4026 hdr->b_l1hdr.b_arc_access = ddi_get_lbolt();
2a432414
GW
4027 DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
4028 arc_change_state(arc_mfu, hdr, hash_lock);
34dc7c2f 4029 } else {
b9541d6b
CW
4030 cmn_err(CE_PANIC, "invalid arc state 0x%p",
4031 hdr->b_l1hdr.b_state);
34dc7c2f
BB
4032 }
4033}
4034
4035/* a generic arc_done_func_t which you can use */
4036/* ARGSUSED */
4037void
4038arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
4039{
428870ff
BB
4040 if (zio == NULL || zio->io_error == 0)
4041 bcopy(buf->b_data, arg, buf->b_hdr->b_size);
13fe0198 4042 VERIFY(arc_buf_remove_ref(buf, arg));
34dc7c2f
BB
4043}
4044
4045/* a generic arc_done_func_t */
4046void
4047arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
4048{
4049 arc_buf_t **bufp = arg;
4050 if (zio && zio->io_error) {
13fe0198 4051 VERIFY(arc_buf_remove_ref(buf, arg));
34dc7c2f
BB
4052 *bufp = NULL;
4053 } else {
4054 *bufp = buf;
428870ff 4055 ASSERT(buf->b_data);
34dc7c2f
BB
4056 }
4057}
4058
4059static void
4060arc_read_done(zio_t *zio)
4061{
9b67f605 4062 arc_buf_hdr_t *hdr;
34dc7c2f
BB
4063 arc_buf_t *buf;
4064 arc_buf_t *abuf; /* buffer we're assigning to callback */
9b67f605 4065 kmutex_t *hash_lock = NULL;
34dc7c2f
BB
4066 arc_callback_t *callback_list, *acb;
4067 int freeable = FALSE;
4068
4069 buf = zio->io_private;
4070 hdr = buf->b_hdr;
4071
4072 /*
4073 * The hdr was inserted into hash-table and removed from lists
4074 * prior to starting I/O. We should find this header, since
4075 * it's in the hash table, and it should be legit since it's
4076 * not possible to evict it during the I/O. The only possible
4077 * reason for it not to be found is if we were freed during the
4078 * read.
4079 */
9b67f605
MA
4080 if (HDR_IN_HASH_TABLE(hdr)) {
4081 arc_buf_hdr_t *found;
4082
4083 ASSERT3U(hdr->b_birth, ==, BP_PHYSICAL_BIRTH(zio->io_bp));
4084 ASSERT3U(hdr->b_dva.dva_word[0], ==,
4085 BP_IDENTITY(zio->io_bp)->dva_word[0]);
4086 ASSERT3U(hdr->b_dva.dva_word[1], ==,
4087 BP_IDENTITY(zio->io_bp)->dva_word[1]);
4088
4089 found = buf_hash_find(hdr->b_spa, zio->io_bp,
4090 &hash_lock);
4091
4092 ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) &&
4093 hash_lock == NULL) ||
4094 (found == hdr &&
4095 DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
4096 (found == hdr && HDR_L2_READING(hdr)));
4097 }
34dc7c2f 4098
2a432414 4099 hdr->b_flags &= ~ARC_FLAG_L2_EVICTED;
b9541d6b 4100 if (l2arc_noprefetch && HDR_PREFETCH(hdr))
2a432414 4101 hdr->b_flags &= ~ARC_FLAG_L2CACHE;
34dc7c2f
BB
4102
4103 /* byteswap if necessary */
b9541d6b 4104 callback_list = hdr->b_l1hdr.b_acb;
34dc7c2f 4105 ASSERT(callback_list != NULL);
428870ff 4106 if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
9ae529ec
CS
4107 dmu_object_byteswap_t bswap =
4108 DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
b01615d5
RY
4109 if (BP_GET_LEVEL(zio->io_bp) > 0)
4110 byteswap_uint64_array(buf->b_data, hdr->b_size);
4111 else
4112 dmu_ot_byteswap[bswap].ob_func(buf->b_data, hdr->b_size);
b128c09f 4113 }
34dc7c2f
BB
4114
4115 arc_cksum_compute(buf, B_FALSE);
498877ba 4116 arc_buf_watch(buf);
34dc7c2f 4117
b9541d6b
CW
4118 if (hash_lock && zio->io_error == 0 &&
4119 hdr->b_l1hdr.b_state == arc_anon) {
428870ff
BB
4120 /*
4121 * Only call arc_access on anonymous buffers. This is because
4122 * if we've issued an I/O for an evicted buffer, we've already
4123 * called arc_access (to prevent any simultaneous readers from
4124 * getting confused).
4125 */
4126 arc_access(hdr, hash_lock);
4127 }
4128
34dc7c2f
BB
4129 /* create copies of the data buffer for the callers */
4130 abuf = buf;
4131 for (acb = callback_list; acb; acb = acb->acb_next) {
4132 if (acb->acb_done) {
1eb5bfa3
GW
4133 if (abuf == NULL) {
4134 ARCSTAT_BUMP(arcstat_duplicate_reads);
34dc7c2f 4135 abuf = arc_buf_clone(buf);
1eb5bfa3 4136 }
34dc7c2f
BB
4137 acb->acb_buf = abuf;
4138 abuf = NULL;
4139 }
4140 }
b9541d6b 4141 hdr->b_l1hdr.b_acb = NULL;
2a432414 4142 hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS;
34dc7c2f 4143 ASSERT(!HDR_BUF_AVAILABLE(hdr));
428870ff
BB
4144 if (abuf == buf) {
4145 ASSERT(buf->b_efunc == NULL);
b9541d6b 4146 ASSERT(hdr->b_l1hdr.b_datacnt == 1);
2a432414 4147 hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE;
428870ff 4148 }
34dc7c2f 4149
b9541d6b
CW
4150 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt) ||
4151 callback_list != NULL);
34dc7c2f
BB
4152
4153 if (zio->io_error != 0) {
2a432414 4154 hdr->b_flags |= ARC_FLAG_IO_ERROR;
b9541d6b 4155 if (hdr->b_l1hdr.b_state != arc_anon)
34dc7c2f
BB
4156 arc_change_state(arc_anon, hdr, hash_lock);
4157 if (HDR_IN_HASH_TABLE(hdr))
4158 buf_hash_remove(hdr);
b9541d6b 4159 freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt);
34dc7c2f
BB
4160 }
4161
4162 /*
4163 * Broadcast before we drop the hash_lock to avoid the possibility
4164 * that the hdr (and hence the cv) might be freed before we get to
4165 * the cv_broadcast().
4166 */
b9541d6b 4167 cv_broadcast(&hdr->b_l1hdr.b_cv);
34dc7c2f 4168
b9541d6b 4169 if (hash_lock != NULL) {
34dc7c2f
BB
4170 mutex_exit(hash_lock);
4171 } else {
4172 /*
4173 * This block was freed while we waited for the read to
4174 * complete. It has been removed from the hash table and
4175 * moved to the anonymous state (so that it won't show up
4176 * in the cache).
4177 */
b9541d6b
CW
4178 ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
4179 freeable = refcount_is_zero(&hdr->b_l1hdr.b_refcnt);
34dc7c2f
BB
4180 }
4181
4182 /* execute each callback and free its structure */
4183 while ((acb = callback_list) != NULL) {
4184 if (acb->acb_done)
4185 acb->acb_done(zio, acb->acb_buf, acb->acb_private);
4186
4187 if (acb->acb_zio_dummy != NULL) {
4188 acb->acb_zio_dummy->io_error = zio->io_error;
4189 zio_nowait(acb->acb_zio_dummy);
4190 }
4191
4192 callback_list = acb->acb_next;
4193 kmem_free(acb, sizeof (arc_callback_t));
4194 }
4195
4196 if (freeable)
4197 arc_hdr_destroy(hdr);
4198}
4199
4200/*
5c839890 4201 * "Read" the block at the specified DVA (in bp) via the
34dc7c2f
BB
4202 * cache. If the block is found in the cache, invoke the provided
4203 * callback immediately and return. Note that the `zio' parameter
4204 * in the callback will be NULL in this case, since no IO was
4205 * required. If the block is not in the cache pass the read request
4206 * on to the spa with a substitute callback function, so that the
4207 * requested block will be added to the cache.
4208 *
4209 * If a read request arrives for a block that has a read in-progress,
4210 * either wait for the in-progress read to complete (and return the
4211 * results); or, if this is a read with a "done" func, add a record
4212 * to the read to invoke the "done" func when the read completes,
4213 * and return; or just return.
4214 *
4215 * arc_read_done() will invoke all the requested "done" functions
4216 * for readers of this block.
4217 */
4218int
294f6806 4219arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done,
2a432414
GW
4220 void *private, zio_priority_t priority, int zio_flags,
4221 arc_flags_t *arc_flags, const zbookmark_phys_t *zb)
34dc7c2f 4222{
9b67f605 4223 arc_buf_hdr_t *hdr = NULL;
d4ed6673 4224 arc_buf_t *buf = NULL;
9b67f605 4225 kmutex_t *hash_lock = NULL;
34dc7c2f 4226 zio_t *rzio;
3541dc6d 4227 uint64_t guid = spa_load_guid(spa);
1421c891 4228 int rc = 0;
34dc7c2f 4229
9b67f605
MA
4230 ASSERT(!BP_IS_EMBEDDED(bp) ||
4231 BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
4232
34dc7c2f 4233top:
9b67f605
MA
4234 if (!BP_IS_EMBEDDED(bp)) {
4235 /*
4236 * Embedded BP's have no DVA and require no I/O to "read".
4237 * Create an anonymous arc buf to back it.
4238 */
4239 hdr = buf_hash_find(guid, bp, &hash_lock);
4240 }
4241
b9541d6b 4242 if (hdr != NULL && HDR_HAS_L1HDR(hdr) && hdr->b_l1hdr.b_datacnt > 0) {
34dc7c2f 4243
2a432414 4244 *arc_flags |= ARC_FLAG_CACHED;
34dc7c2f
BB
4245
4246 if (HDR_IO_IN_PROGRESS(hdr)) {
4247
2a432414 4248 if (*arc_flags & ARC_FLAG_WAIT) {
b9541d6b 4249 cv_wait(&hdr->b_l1hdr.b_cv, hash_lock);
34dc7c2f
BB
4250 mutex_exit(hash_lock);
4251 goto top;
4252 }
2a432414 4253 ASSERT(*arc_flags & ARC_FLAG_NOWAIT);
34dc7c2f
BB
4254
4255 if (done) {
4256 arc_callback_t *acb = NULL;
4257
4258 acb = kmem_zalloc(sizeof (arc_callback_t),
79c76d5b 4259 KM_SLEEP);
34dc7c2f
BB
4260 acb->acb_done = done;
4261 acb->acb_private = private;
34dc7c2f
BB
4262 if (pio != NULL)
4263 acb->acb_zio_dummy = zio_null(pio,
d164b209 4264 spa, NULL, NULL, NULL, zio_flags);
34dc7c2f
BB
4265
4266 ASSERT(acb->acb_done != NULL);
b9541d6b
CW
4267 acb->acb_next = hdr->b_l1hdr.b_acb;
4268 hdr->b_l1hdr.b_acb = acb;
34dc7c2f
BB
4269 add_reference(hdr, hash_lock, private);
4270 mutex_exit(hash_lock);
1421c891 4271 goto out;
34dc7c2f
BB
4272 }
4273 mutex_exit(hash_lock);
1421c891 4274 goto out;
34dc7c2f
BB
4275 }
4276
b9541d6b
CW
4277 ASSERT(hdr->b_l1hdr.b_state == arc_mru ||
4278 hdr->b_l1hdr.b_state == arc_mfu);
34dc7c2f
BB
4279
4280 if (done) {
4281 add_reference(hdr, hash_lock, private);
4282 /*
4283 * If this block is already in use, create a new
4284 * copy of the data so that we will be guaranteed
4285 * that arc_release() will always succeed.
4286 */
b9541d6b 4287 buf = hdr->b_l1hdr.b_buf;
34dc7c2f
BB
4288 ASSERT(buf);
4289 ASSERT(buf->b_data);
4290 if (HDR_BUF_AVAILABLE(hdr)) {
4291 ASSERT(buf->b_efunc == NULL);
2a432414 4292 hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE;
34dc7c2f
BB
4293 } else {
4294 buf = arc_buf_clone(buf);
4295 }
428870ff 4296
2a432414 4297 } else if (*arc_flags & ARC_FLAG_PREFETCH &&
b9541d6b 4298 refcount_count(&hdr->b_l1hdr.b_refcnt) == 0) {
2a432414 4299 hdr->b_flags |= ARC_FLAG_PREFETCH;
34dc7c2f
BB
4300 }
4301 DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
4302 arc_access(hdr, hash_lock);
2a432414
GW
4303 if (*arc_flags & ARC_FLAG_L2CACHE)
4304 hdr->b_flags |= ARC_FLAG_L2CACHE;
4305 if (*arc_flags & ARC_FLAG_L2COMPRESS)
4306 hdr->b_flags |= ARC_FLAG_L2COMPRESS;
34dc7c2f
BB
4307 mutex_exit(hash_lock);
4308 ARCSTAT_BUMP(arcstat_hits);
b9541d6b
CW
4309 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr),
4310 demand, prefetch, !HDR_ISTYPE_METADATA(hdr),
34dc7c2f
BB
4311 data, metadata, hits);
4312
4313 if (done)
4314 done(NULL, buf, private);
4315 } else {
4316 uint64_t size = BP_GET_LSIZE(bp);
9b67f605 4317 arc_callback_t *acb;
b128c09f 4318 vdev_t *vd = NULL;
a117a6d6 4319 uint64_t addr = 0;
d164b209 4320 boolean_t devw = B_FALSE;
0ed212dc 4321 enum zio_compress b_compress = ZIO_COMPRESS_OFF;
b9541d6b 4322 int32_t b_asize = 0;
34dc7c2f 4323
5f6d0b6f
BB
4324 /*
4325 * Gracefully handle a damaged logical block size as a
4326 * checksum error by passing a dummy zio to the done callback.
4327 */
f1512ee6 4328 if (size > spa_maxblocksize(spa)) {
5f6d0b6f
BB
4329 if (done) {
4330 rzio = zio_null(pio, spa, NULL,
4331 NULL, NULL, zio_flags);
4332 rzio->io_error = ECKSUM;
4333 done(rzio, buf, private);
4334 zio_nowait(rzio);
4335 }
4336 rc = ECKSUM;
4337 goto out;
4338 }
4339
34dc7c2f
BB
4340 if (hdr == NULL) {
4341 /* this block is not in the cache */
9b67f605 4342 arc_buf_hdr_t *exists = NULL;
34dc7c2f
BB
4343 arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
4344 buf = arc_buf_alloc(spa, size, private, type);
4345 hdr = buf->b_hdr;
9b67f605
MA
4346 if (!BP_IS_EMBEDDED(bp)) {
4347 hdr->b_dva = *BP_IDENTITY(bp);
4348 hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
9b67f605
MA
4349 exists = buf_hash_insert(hdr, &hash_lock);
4350 }
4351 if (exists != NULL) {
34dc7c2f
BB
4352 /* somebody beat us to the hash insert */
4353 mutex_exit(hash_lock);
428870ff 4354 buf_discard_identity(hdr);
34dc7c2f
BB
4355 (void) arc_buf_remove_ref(buf, private);
4356 goto top; /* restart the IO request */
4357 }
2a432414 4358
34dc7c2f 4359 /* if this is a prefetch, we don't have a reference */
2a432414 4360 if (*arc_flags & ARC_FLAG_PREFETCH) {
34dc7c2f
BB
4361 (void) remove_reference(hdr, hash_lock,
4362 private);
2a432414 4363 hdr->b_flags |= ARC_FLAG_PREFETCH;
34dc7c2f 4364 }
2a432414
GW
4365 if (*arc_flags & ARC_FLAG_L2CACHE)
4366 hdr->b_flags |= ARC_FLAG_L2CACHE;
4367 if (*arc_flags & ARC_FLAG_L2COMPRESS)
4368 hdr->b_flags |= ARC_FLAG_L2COMPRESS;
34dc7c2f 4369 if (BP_GET_LEVEL(bp) > 0)
2a432414 4370 hdr->b_flags |= ARC_FLAG_INDIRECT;
34dc7c2f 4371 } else {
b9541d6b
CW
4372 /*
4373 * This block is in the ghost cache. If it was L2-only
4374 * (and thus didn't have an L1 hdr), we realloc the
4375 * header to add an L1 hdr.
4376 */
4377 if (!HDR_HAS_L1HDR(hdr)) {
4378 hdr = arc_hdr_realloc(hdr, hdr_l2only_cache,
4379 hdr_full_cache);
4380 }
4381
4382 ASSERT(GHOST_STATE(hdr->b_l1hdr.b_state));
34dc7c2f 4383 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
b9541d6b 4384 ASSERT(refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
ca0bf58d 4385 ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
34dc7c2f
BB
4386
4387 /* if this is a prefetch, we don't have a reference */
2a432414
GW
4388 if (*arc_flags & ARC_FLAG_PREFETCH)
4389 hdr->b_flags |= ARC_FLAG_PREFETCH;
34dc7c2f
BB
4390 else
4391 add_reference(hdr, hash_lock, private);
2a432414
GW
4392 if (*arc_flags & ARC_FLAG_L2CACHE)
4393 hdr->b_flags |= ARC_FLAG_L2CACHE;
4394 if (*arc_flags & ARC_FLAG_L2COMPRESS)
4395 hdr->b_flags |= ARC_FLAG_L2COMPRESS;
34dc7c2f
BB
4396 buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
4397 buf->b_hdr = hdr;
4398 buf->b_data = NULL;
4399 buf->b_efunc = NULL;
4400 buf->b_private = NULL;
4401 buf->b_next = NULL;
b9541d6b
CW
4402 hdr->b_l1hdr.b_buf = buf;
4403 ASSERT0(hdr->b_l1hdr.b_datacnt);
4404 hdr->b_l1hdr.b_datacnt = 1;
428870ff
BB
4405 arc_get_data_buf(buf);
4406 arc_access(hdr, hash_lock);
34dc7c2f
BB
4407 }
4408
b9541d6b 4409 ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state));
428870ff 4410
79c76d5b 4411 acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
34dc7c2f
BB
4412 acb->acb_done = done;
4413 acb->acb_private = private;
34dc7c2f 4414
b9541d6b
CW
4415 ASSERT(hdr->b_l1hdr.b_acb == NULL);
4416 hdr->b_l1hdr.b_acb = acb;
2a432414 4417 hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS;
34dc7c2f 4418
b9541d6b
CW
4419 if (HDR_HAS_L2HDR(hdr) &&
4420 (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) {
4421 devw = hdr->b_l2hdr.b_dev->l2ad_writing;
4422 addr = hdr->b_l2hdr.b_daddr;
4e0f33ff 4423 b_compress = hdr->b_l2hdr.b_compress;
b9541d6b 4424 b_asize = hdr->b_l2hdr.b_asize;
b128c09f
BB
4425 /*
4426 * Lock out device removal.
4427 */
4428 if (vdev_is_dead(vd) ||
4429 !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
4430 vd = NULL;
4431 }
4432
9b67f605
MA
4433 if (hash_lock != NULL)
4434 mutex_exit(hash_lock);
b128c09f 4435
e49f1e20
WA
4436 /*
4437 * At this point, we have a level 1 cache miss. Try again in
4438 * L2ARC if possible.
4439 */
34dc7c2f 4440 ASSERT3U(hdr->b_size, ==, size);
428870ff 4441 DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
5dbd68a3 4442 uint64_t, size, zbookmark_phys_t *, zb);
34dc7c2f 4443 ARCSTAT_BUMP(arcstat_misses);
b9541d6b
CW
4444 ARCSTAT_CONDSTAT(!HDR_PREFETCH(hdr),
4445 demand, prefetch, !HDR_ISTYPE_METADATA(hdr),
34dc7c2f
BB
4446 data, metadata, misses);
4447
d164b209 4448 if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
34dc7c2f
BB
4449 /*
4450 * Read from the L2ARC if the following are true:
b128c09f
BB
4451 * 1. The L2ARC vdev was previously cached.
4452 * 2. This buffer still has L2ARC metadata.
4453 * 3. This buffer isn't currently writing to the L2ARC.
4454 * 4. The L2ARC entry wasn't evicted, which may
4455 * also have invalidated the vdev.
d164b209 4456 * 5. This isn't prefetch and l2arc_noprefetch is set.
34dc7c2f 4457 */
b9541d6b 4458 if (HDR_HAS_L2HDR(hdr) &&
d164b209
BB
4459 !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
4460 !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
34dc7c2f
BB
4461 l2arc_read_callback_t *cb;
4462
4463 DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
4464 ARCSTAT_BUMP(arcstat_l2_hits);
b9541d6b 4465 atomic_inc_32(&hdr->b_l2hdr.b_hits);
34dc7c2f 4466
34dc7c2f 4467 cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
79c76d5b 4468 KM_SLEEP);
34dc7c2f
BB
4469 cb->l2rcb_buf = buf;
4470 cb->l2rcb_spa = spa;
4471 cb->l2rcb_bp = *bp;
4472 cb->l2rcb_zb = *zb;
b128c09f 4473 cb->l2rcb_flags = zio_flags;
0ed212dc 4474 cb->l2rcb_compress = b_compress;
34dc7c2f 4475
a117a6d6
GW
4476 ASSERT(addr >= VDEV_LABEL_START_SIZE &&
4477 addr + size < vd->vdev_psize -
4478 VDEV_LABEL_END_SIZE);
4479
34dc7c2f 4480 /*
b128c09f
BB
4481 * l2arc read. The SCL_L2ARC lock will be
4482 * released by l2arc_read_done().
3a17a7a9
SK
4483 * Issue a null zio if the underlying buffer
4484 * was squashed to zero size by compression.
34dc7c2f 4485 */
0ed212dc 4486 if (b_compress == ZIO_COMPRESS_EMPTY) {
3a17a7a9
SK
4487 rzio = zio_null(pio, spa, vd,
4488 l2arc_read_done, cb,
4489 zio_flags | ZIO_FLAG_DONT_CACHE |
4490 ZIO_FLAG_CANFAIL |
4491 ZIO_FLAG_DONT_PROPAGATE |
4492 ZIO_FLAG_DONT_RETRY);
4493 } else {
4494 rzio = zio_read_phys(pio, vd, addr,
0ed212dc
BP
4495 b_asize, buf->b_data,
4496 ZIO_CHECKSUM_OFF,
3a17a7a9
SK
4497 l2arc_read_done, cb, priority,
4498 zio_flags | ZIO_FLAG_DONT_CACHE |
4499 ZIO_FLAG_CANFAIL |
4500 ZIO_FLAG_DONT_PROPAGATE |
4501 ZIO_FLAG_DONT_RETRY, B_FALSE);
4502 }
34dc7c2f
BB
4503 DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
4504 zio_t *, rzio);
0ed212dc 4505 ARCSTAT_INCR(arcstat_l2_read_bytes, b_asize);
34dc7c2f 4506
2a432414 4507 if (*arc_flags & ARC_FLAG_NOWAIT) {
b128c09f 4508 zio_nowait(rzio);
1421c891 4509 goto out;
b128c09f 4510 }
34dc7c2f 4511
2a432414 4512 ASSERT(*arc_flags & ARC_FLAG_WAIT);
b128c09f 4513 if (zio_wait(rzio) == 0)
1421c891 4514 goto out;
b128c09f
BB
4515
4516 /* l2arc read error; goto zio_read() */
34dc7c2f
BB
4517 } else {
4518 DTRACE_PROBE1(l2arc__miss,
4519 arc_buf_hdr_t *, hdr);
4520 ARCSTAT_BUMP(arcstat_l2_misses);
4521 if (HDR_L2_WRITING(hdr))
4522 ARCSTAT_BUMP(arcstat_l2_rw_clash);
b128c09f 4523 spa_config_exit(spa, SCL_L2ARC, vd);
34dc7c2f 4524 }
d164b209
BB
4525 } else {
4526 if (vd != NULL)
4527 spa_config_exit(spa, SCL_L2ARC, vd);
4528 if (l2arc_ndev != 0) {
4529 DTRACE_PROBE1(l2arc__miss,
4530 arc_buf_hdr_t *, hdr);
4531 ARCSTAT_BUMP(arcstat_l2_misses);
4532 }
34dc7c2f 4533 }
34dc7c2f
BB
4534
4535 rzio = zio_read(pio, spa, bp, buf->b_data, size,
b128c09f 4536 arc_read_done, buf, priority, zio_flags, zb);
34dc7c2f 4537
2a432414 4538 if (*arc_flags & ARC_FLAG_WAIT) {
1421c891
PS
4539 rc = zio_wait(rzio);
4540 goto out;
4541 }
34dc7c2f 4542
2a432414 4543 ASSERT(*arc_flags & ARC_FLAG_NOWAIT);
34dc7c2f
BB
4544 zio_nowait(rzio);
4545 }
1421c891
PS
4546
4547out:
4548 spa_read_history_add(spa, zb, *arc_flags);
4549 return (rc);
34dc7c2f
BB
4550}
4551
ab26409d
BB
4552arc_prune_t *
4553arc_add_prune_callback(arc_prune_func_t *func, void *private)
4554{
4555 arc_prune_t *p;
4556
d1d7e268 4557 p = kmem_alloc(sizeof (*p), KM_SLEEP);
ab26409d
BB
4558 p->p_pfunc = func;
4559 p->p_private = private;
4560 list_link_init(&p->p_node);
4561 refcount_create(&p->p_refcnt);
4562
4563 mutex_enter(&arc_prune_mtx);
4564 refcount_add(&p->p_refcnt, &arc_prune_list);
4565 list_insert_head(&arc_prune_list, p);
4566 mutex_exit(&arc_prune_mtx);
4567
4568 return (p);
4569}
4570
4571void
4572arc_remove_prune_callback(arc_prune_t *p)
4573{
4574 mutex_enter(&arc_prune_mtx);
4575 list_remove(&arc_prune_list, p);
4576 if (refcount_remove(&p->p_refcnt, &arc_prune_list) == 0) {
4577 refcount_destroy(&p->p_refcnt);
4578 kmem_free(p, sizeof (*p));
4579 }
4580 mutex_exit(&arc_prune_mtx);
4581}
4582
34dc7c2f
BB
4583void
4584arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
4585{
4586 ASSERT(buf->b_hdr != NULL);
b9541d6b
CW
4587 ASSERT(buf->b_hdr->b_l1hdr.b_state != arc_anon);
4588 ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt) ||
4589 func == NULL);
428870ff
BB
4590 ASSERT(buf->b_efunc == NULL);
4591 ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
4592
34dc7c2f
BB
4593 buf->b_efunc = func;
4594 buf->b_private = private;
4595}
4596
df4474f9
MA
4597/*
4598 * Notify the arc that a block was freed, and thus will never be used again.
4599 */
4600void
4601arc_freed(spa_t *spa, const blkptr_t *bp)
4602{
4603 arc_buf_hdr_t *hdr;
4604 kmutex_t *hash_lock;
4605 uint64_t guid = spa_load_guid(spa);
4606
9b67f605
MA
4607 ASSERT(!BP_IS_EMBEDDED(bp));
4608
4609 hdr = buf_hash_find(guid, bp, &hash_lock);
df4474f9
MA
4610 if (hdr == NULL)
4611 return;
4612 if (HDR_BUF_AVAILABLE(hdr)) {
b9541d6b 4613 arc_buf_t *buf = hdr->b_l1hdr.b_buf;
df4474f9 4614 add_reference(hdr, hash_lock, FTAG);
2a432414 4615 hdr->b_flags &= ~ARC_FLAG_BUF_AVAILABLE;
df4474f9
MA
4616 mutex_exit(hash_lock);
4617
4618 arc_release(buf, FTAG);
4619 (void) arc_buf_remove_ref(buf, FTAG);
4620 } else {
4621 mutex_exit(hash_lock);
4622 }
4623
4624}
4625
34dc7c2f 4626/*
bd089c54
MA
4627 * Clear the user eviction callback set by arc_set_callback(), first calling
4628 * it if it exists. Because the presence of a callback keeps an arc_buf cached
4629 * clearing the callback may result in the arc_buf being destroyed. However,
4630 * it will not result in the *last* arc_buf being destroyed, hence the data
4631 * will remain cached in the ARC. We make a copy of the arc buffer here so
4632 * that we can process the callback without holding any locks.
4633 *
4634 * It's possible that the callback is already in the process of being cleared
4635 * by another thread. In this case we can not clear the callback.
4636 *
4637 * Returns B_TRUE if the callback was successfully called and cleared.
34dc7c2f 4638 */
bd089c54
MA
4639boolean_t
4640arc_clear_callback(arc_buf_t *buf)
34dc7c2f
BB
4641{
4642 arc_buf_hdr_t *hdr;
4643 kmutex_t *hash_lock;
bd089c54
MA
4644 arc_evict_func_t *efunc = buf->b_efunc;
4645 void *private = buf->b_private;
34dc7c2f 4646
428870ff 4647 mutex_enter(&buf->b_evict_lock);
34dc7c2f
BB
4648 hdr = buf->b_hdr;
4649 if (hdr == NULL) {
4650 /*
4651 * We are in arc_do_user_evicts().
4652 */
4653 ASSERT(buf->b_data == NULL);
428870ff 4654 mutex_exit(&buf->b_evict_lock);
bd089c54 4655 return (B_FALSE);
b128c09f 4656 } else if (buf->b_data == NULL) {
34dc7c2f 4657 /*
b128c09f
BB
4658 * We are on the eviction list; process this buffer now
4659 * but let arc_do_user_evicts() do the reaping.
34dc7c2f 4660 */
b128c09f 4661 buf->b_efunc = NULL;
428870ff 4662 mutex_exit(&buf->b_evict_lock);
bd089c54
MA
4663 VERIFY0(efunc(private));
4664 return (B_TRUE);
34dc7c2f 4665 }
b128c09f
BB
4666 hash_lock = HDR_LOCK(hdr);
4667 mutex_enter(hash_lock);
428870ff
BB
4668 hdr = buf->b_hdr;
4669 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
34dc7c2f 4670
b9541d6b
CW
4671 ASSERT3U(refcount_count(&hdr->b_l1hdr.b_refcnt), <,
4672 hdr->b_l1hdr.b_datacnt);
4673 ASSERT(hdr->b_l1hdr.b_state == arc_mru ||
4674 hdr->b_l1hdr.b_state == arc_mfu);
34dc7c2f 4675
bd089c54
MA
4676 buf->b_efunc = NULL;
4677 buf->b_private = NULL;
34dc7c2f 4678
b9541d6b 4679 if (hdr->b_l1hdr.b_datacnt > 1) {
bd089c54 4680 mutex_exit(&buf->b_evict_lock);
ca0bf58d 4681 arc_buf_destroy(buf, TRUE);
bd089c54 4682 } else {
b9541d6b 4683 ASSERT(buf == hdr->b_l1hdr.b_buf);
2a432414 4684 hdr->b_flags |= ARC_FLAG_BUF_AVAILABLE;
bd089c54 4685 mutex_exit(&buf->b_evict_lock);
34dc7c2f 4686 }
34dc7c2f 4687
bd089c54
MA
4688 mutex_exit(hash_lock);
4689 VERIFY0(efunc(private));
4690 return (B_TRUE);
34dc7c2f
BB
4691}
4692
4693/*
e49f1e20
WA
4694 * Release this buffer from the cache, making it an anonymous buffer. This
4695 * must be done after a read and prior to modifying the buffer contents.
34dc7c2f 4696 * If the buffer has more than one reference, we must make
b128c09f 4697 * a new hdr for the buffer.
34dc7c2f
BB
4698 */
4699void
4700arc_release(arc_buf_t *buf, void *tag)
4701{
b9541d6b
CW
4702 kmutex_t *hash_lock;
4703 arc_state_t *state;
4704 arc_buf_hdr_t *hdr = buf->b_hdr;
34dc7c2f 4705
428870ff 4706 /*
ca0bf58d 4707 * It would be nice to assert that if its DMU metadata (level >
428870ff
BB
4708 * 0 || it's the dnode file), then it must be syncing context.
4709 * But we don't know that information at this level.
4710 */
4711
4712 mutex_enter(&buf->b_evict_lock);
b128c09f 4713
ca0bf58d
PS
4714 ASSERT(HDR_HAS_L1HDR(hdr));
4715
b9541d6b
CW
4716 /*
4717 * We don't grab the hash lock prior to this check, because if
4718 * the buffer's header is in the arc_anon state, it won't be
4719 * linked into the hash table.
4720 */
4721 if (hdr->b_l1hdr.b_state == arc_anon) {
4722 mutex_exit(&buf->b_evict_lock);
4723 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
4724 ASSERT(!HDR_IN_HASH_TABLE(hdr));
4725 ASSERT(!HDR_HAS_L2HDR(hdr));
4726 ASSERT(BUF_EMPTY(hdr));
34dc7c2f 4727
b9541d6b
CW
4728 ASSERT3U(hdr->b_l1hdr.b_datacnt, ==, 1);
4729 ASSERT3S(refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1);
4730 ASSERT(!list_link_active(&hdr->b_l1hdr.b_arc_node));
4731
4732 ASSERT3P(buf->b_efunc, ==, NULL);
4733 ASSERT3P(buf->b_private, ==, NULL);
4734
4735 hdr->b_l1hdr.b_arc_access = 0;
4736 arc_buf_thaw(buf);
4737
4738 return;
34dc7c2f
BB
4739 }
4740
b9541d6b
CW
4741 hash_lock = HDR_LOCK(hdr);
4742 mutex_enter(hash_lock);
4743
4744 /*
4745 * This assignment is only valid as long as the hash_lock is
4746 * held, we must be careful not to reference state or the
4747 * b_state field after dropping the lock.
4748 */
4749 state = hdr->b_l1hdr.b_state;
4750 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4751 ASSERT3P(state, !=, arc_anon);
4752
4753 /* this buffer is not on any list */
4754 ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) > 0);
4755
4756 if (HDR_HAS_L2HDR(hdr)) {
b9541d6b 4757 mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx);
ca0bf58d
PS
4758
4759 /*
d962d5da
PS
4760 * We have to recheck this conditional again now that
4761 * we're holding the l2ad_mtx to prevent a race with
4762 * another thread which might be concurrently calling
4763 * l2arc_evict(). In that case, l2arc_evict() might have
4764 * destroyed the header's L2 portion as we were waiting
4765 * to acquire the l2ad_mtx.
ca0bf58d 4766 */
d962d5da
PS
4767 if (HDR_HAS_L2HDR(hdr))
4768 arc_hdr_l2hdr_destroy(hdr);
ca0bf58d 4769
b9541d6b 4770 mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx);
b128c09f
BB
4771 }
4772
34dc7c2f
BB
4773 /*
4774 * Do we have more than one buf?
4775 */
b9541d6b 4776 if (hdr->b_l1hdr.b_datacnt > 1) {
34dc7c2f
BB
4777 arc_buf_hdr_t *nhdr;
4778 arc_buf_t **bufp;
4779 uint64_t blksz = hdr->b_size;
d164b209 4780 uint64_t spa = hdr->b_spa;
b9541d6b 4781 arc_buf_contents_t type = arc_buf_type(hdr);
34dc7c2f
BB
4782 uint32_t flags = hdr->b_flags;
4783
b9541d6b 4784 ASSERT(hdr->b_l1hdr.b_buf != buf || buf->b_next != NULL);
34dc7c2f 4785 /*
428870ff
BB
4786 * Pull the data off of this hdr and attach it to
4787 * a new anonymous hdr.
34dc7c2f
BB
4788 */
4789 (void) remove_reference(hdr, hash_lock, tag);
b9541d6b 4790 bufp = &hdr->b_l1hdr.b_buf;
34dc7c2f
BB
4791 while (*bufp != buf)
4792 bufp = &(*bufp)->b_next;
428870ff 4793 *bufp = buf->b_next;
34dc7c2f
BB
4794 buf->b_next = NULL;
4795
b9541d6b 4796 ASSERT3P(state, !=, arc_l2c_only);
36da08ef
PS
4797
4798 (void) refcount_remove_many(
4799 &state->arcs_size, hdr->b_size, buf);
4800
b9541d6b
CW
4801 if (refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) {
4802 uint64_t *size;
4803
4804 ASSERT3P(state, !=, arc_l2c_only);
4805 size = &state->arcs_lsize[type];
34dc7c2f
BB
4806 ASSERT3U(*size, >=, hdr->b_size);
4807 atomic_add_64(size, -hdr->b_size);
4808 }
1eb5bfa3
GW
4809
4810 /*
4811 * We're releasing a duplicate user data buffer, update
4812 * our statistics accordingly.
4813 */
b9541d6b 4814 if (HDR_ISTYPE_DATA(hdr)) {
1eb5bfa3
GW
4815 ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
4816 ARCSTAT_INCR(arcstat_duplicate_buffers_size,
4817 -hdr->b_size);
4818 }
b9541d6b 4819 hdr->b_l1hdr.b_datacnt -= 1;
34dc7c2f 4820 arc_cksum_verify(buf);
498877ba 4821 arc_buf_unwatch(buf);
34dc7c2f
BB
4822
4823 mutex_exit(hash_lock);
4824
b9541d6b 4825 nhdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE);
34dc7c2f
BB
4826 nhdr->b_size = blksz;
4827 nhdr->b_spa = spa;
b9541d6b
CW
4828
4829 nhdr->b_l1hdr.b_mru_hits = 0;
4830 nhdr->b_l1hdr.b_mru_ghost_hits = 0;
4831 nhdr->b_l1hdr.b_mfu_hits = 0;
4832 nhdr->b_l1hdr.b_mfu_ghost_hits = 0;
4833 nhdr->b_l1hdr.b_l2_hits = 0;
2a432414 4834 nhdr->b_flags = flags & ARC_FLAG_L2_WRITING;
b9541d6b
CW
4835 nhdr->b_flags |= arc_bufc_to_flags(type);
4836 nhdr->b_flags |= ARC_FLAG_HAS_L1HDR;
4837
4838 nhdr->b_l1hdr.b_buf = buf;
4839 nhdr->b_l1hdr.b_datacnt = 1;
4840 nhdr->b_l1hdr.b_state = arc_anon;
4841 nhdr->b_l1hdr.b_arc_access = 0;
ca0bf58d 4842 nhdr->b_l1hdr.b_tmp_cdata = NULL;
34dc7c2f 4843 nhdr->b_freeze_cksum = NULL;
b9541d6b
CW
4844
4845 (void) refcount_add(&nhdr->b_l1hdr.b_refcnt, tag);
34dc7c2f 4846 buf->b_hdr = nhdr;
428870ff 4847 mutex_exit(&buf->b_evict_lock);
36da08ef 4848 (void) refcount_add_many(&arc_anon->arcs_size, blksz, buf);
34dc7c2f 4849 } else {
428870ff 4850 mutex_exit(&buf->b_evict_lock);
b9541d6b 4851 ASSERT(refcount_count(&hdr->b_l1hdr.b_refcnt) == 1);
ca0bf58d
PS
4852 /* protected by hash lock, or hdr is on arc_anon */
4853 ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
34dc7c2f 4854 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
b9541d6b
CW
4855 hdr->b_l1hdr.b_mru_hits = 0;
4856 hdr->b_l1hdr.b_mru_ghost_hits = 0;
4857 hdr->b_l1hdr.b_mfu_hits = 0;
4858 hdr->b_l1hdr.b_mfu_ghost_hits = 0;
4859 hdr->b_l1hdr.b_l2_hits = 0;
4860 arc_change_state(arc_anon, hdr, hash_lock);
4861 hdr->b_l1hdr.b_arc_access = 0;
4862 mutex_exit(hash_lock);
34dc7c2f 4863
428870ff 4864 buf_discard_identity(hdr);
34dc7c2f
BB
4865 arc_buf_thaw(buf);
4866 }
4867 buf->b_efunc = NULL;
4868 buf->b_private = NULL;
34dc7c2f
BB
4869}
4870
4871int
4872arc_released(arc_buf_t *buf)
4873{
b128c09f
BB
4874 int released;
4875
428870ff 4876 mutex_enter(&buf->b_evict_lock);
b9541d6b
CW
4877 released = (buf->b_data != NULL &&
4878 buf->b_hdr->b_l1hdr.b_state == arc_anon);
428870ff 4879 mutex_exit(&buf->b_evict_lock);
b128c09f 4880 return (released);
34dc7c2f
BB
4881}
4882
34dc7c2f
BB
4883#ifdef ZFS_DEBUG
4884int
4885arc_referenced(arc_buf_t *buf)
4886{
b128c09f
BB
4887 int referenced;
4888
428870ff 4889 mutex_enter(&buf->b_evict_lock);
b9541d6b 4890 referenced = (refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt));
428870ff 4891 mutex_exit(&buf->b_evict_lock);
b128c09f 4892 return (referenced);
34dc7c2f
BB
4893}
4894#endif
4895
4896static void
4897arc_write_ready(zio_t *zio)
4898{
4899 arc_write_callback_t *callback = zio->io_private;
4900 arc_buf_t *buf = callback->awcb_buf;
4901 arc_buf_hdr_t *hdr = buf->b_hdr;
4902
b9541d6b
CW
4903 ASSERT(HDR_HAS_L1HDR(hdr));
4904 ASSERT(!refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt));
4905 ASSERT(hdr->b_l1hdr.b_datacnt > 0);
b128c09f
BB
4906 callback->awcb_ready(zio, buf, callback->awcb_private);
4907
34dc7c2f
BB
4908 /*
4909 * If the IO is already in progress, then this is a re-write
b128c09f
BB
4910 * attempt, so we need to thaw and re-compute the cksum.
4911 * It is the responsibility of the callback to handle the
4912 * accounting for any re-write attempt.
34dc7c2f
BB
4913 */
4914 if (HDR_IO_IN_PROGRESS(hdr)) {
b9541d6b 4915 mutex_enter(&hdr->b_l1hdr.b_freeze_lock);
34dc7c2f
BB
4916 if (hdr->b_freeze_cksum != NULL) {
4917 kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
4918 hdr->b_freeze_cksum = NULL;
4919 }
b9541d6b 4920 mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
34dc7c2f
BB
4921 }
4922 arc_cksum_compute(buf, B_FALSE);
2a432414 4923 hdr->b_flags |= ARC_FLAG_IO_IN_PROGRESS;
34dc7c2f
BB
4924}
4925
e8b96c60
MA
4926/*
4927 * The SPA calls this callback for each physical write that happens on behalf
4928 * of a logical write. See the comment in dbuf_write_physdone() for details.
4929 */
4930static void
4931arc_write_physdone(zio_t *zio)
4932{
4933 arc_write_callback_t *cb = zio->io_private;
4934 if (cb->awcb_physdone != NULL)
4935 cb->awcb_physdone(zio, cb->awcb_buf, cb->awcb_private);
4936}
4937
34dc7c2f
BB
4938static void
4939arc_write_done(zio_t *zio)
4940{
4941 arc_write_callback_t *callback = zio->io_private;
4942 arc_buf_t *buf = callback->awcb_buf;
4943 arc_buf_hdr_t *hdr = buf->b_hdr;
4944
b9541d6b 4945 ASSERT(hdr->b_l1hdr.b_acb == NULL);
428870ff
BB
4946
4947 if (zio->io_error == 0) {
9b67f605 4948 if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) {
b0bc7a84
MG
4949 buf_discard_identity(hdr);
4950 } else {
4951 hdr->b_dva = *BP_IDENTITY(zio->io_bp);
4952 hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
b0bc7a84 4953 }
428870ff
BB
4954 } else {
4955 ASSERT(BUF_EMPTY(hdr));
4956 }
34dc7c2f 4957
34dc7c2f 4958 /*
9b67f605
MA
4959 * If the block to be written was all-zero or compressed enough to be
4960 * embedded in the BP, no write was performed so there will be no
4961 * dva/birth/checksum. The buffer must therefore remain anonymous
4962 * (and uncached).
34dc7c2f
BB
4963 */
4964 if (!BUF_EMPTY(hdr)) {
4965 arc_buf_hdr_t *exists;
4966 kmutex_t *hash_lock;
4967
428870ff
BB
4968 ASSERT(zio->io_error == 0);
4969
34dc7c2f
BB
4970 arc_cksum_verify(buf);
4971
4972 exists = buf_hash_insert(hdr, &hash_lock);
b9541d6b 4973 if (exists != NULL) {
34dc7c2f
BB
4974 /*
4975 * This can only happen if we overwrite for
4976 * sync-to-convergence, because we remove
4977 * buffers from the hash table when we arc_free().
4978 */
428870ff
BB
4979 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
4980 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
4981 panic("bad overwrite, hdr=%p exists=%p",
4982 (void *)hdr, (void *)exists);
b9541d6b
CW
4983 ASSERT(refcount_is_zero(
4984 &exists->b_l1hdr.b_refcnt));
428870ff
BB
4985 arc_change_state(arc_anon, exists, hash_lock);
4986 mutex_exit(hash_lock);
4987 arc_hdr_destroy(exists);
4988 exists = buf_hash_insert(hdr, &hash_lock);
4989 ASSERT3P(exists, ==, NULL);
03c6040b
GW
4990 } else if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
4991 /* nopwrite */
4992 ASSERT(zio->io_prop.zp_nopwrite);
4993 if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
4994 panic("bad nopwrite, hdr=%p exists=%p",
4995 (void *)hdr, (void *)exists);
428870ff
BB
4996 } else {
4997 /* Dedup */
b9541d6b
CW
4998 ASSERT(hdr->b_l1hdr.b_datacnt == 1);
4999 ASSERT(hdr->b_l1hdr.b_state == arc_anon);
428870ff
BB
5000 ASSERT(BP_GET_DEDUP(zio->io_bp));
5001 ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
5002 }
34dc7c2f 5003 }
2a432414 5004 hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS;
b128c09f 5005 /* if it's not anon, we are doing a scrub */
b9541d6b 5006 if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon)
b128c09f 5007 arc_access(hdr, hash_lock);
34dc7c2f 5008 mutex_exit(hash_lock);
34dc7c2f 5009 } else {
2a432414 5010 hdr->b_flags &= ~ARC_FLAG_IO_IN_PROGRESS;
34dc7c2f
BB
5011 }
5012
b9541d6b 5013 ASSERT(!refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
428870ff 5014 callback->awcb_done(zio, buf, callback->awcb_private);
34dc7c2f
BB
5015
5016 kmem_free(callback, sizeof (arc_write_callback_t));
5017}
5018
5019zio_t *
428870ff 5020arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3a17a7a9 5021 blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, boolean_t l2arc_compress,
e8b96c60
MA
5022 const zio_prop_t *zp, arc_done_func_t *ready, arc_done_func_t *physdone,
5023 arc_done_func_t *done, void *private, zio_priority_t priority,
5dbd68a3 5024 int zio_flags, const zbookmark_phys_t *zb)
34dc7c2f
BB
5025{
5026 arc_buf_hdr_t *hdr = buf->b_hdr;
5027 arc_write_callback_t *callback;
b128c09f 5028 zio_t *zio;
34dc7c2f 5029
b128c09f 5030 ASSERT(ready != NULL);
428870ff 5031 ASSERT(done != NULL);
34dc7c2f 5032 ASSERT(!HDR_IO_ERROR(hdr));
b9541d6b
CW
5033 ASSERT(!HDR_IO_IN_PROGRESS(hdr));
5034 ASSERT(hdr->b_l1hdr.b_acb == NULL);
5035 ASSERT(hdr->b_l1hdr.b_datacnt > 0);
b128c09f 5036 if (l2arc)
2a432414 5037 hdr->b_flags |= ARC_FLAG_L2CACHE;
3a17a7a9 5038 if (l2arc_compress)
2a432414 5039 hdr->b_flags |= ARC_FLAG_L2COMPRESS;
79c76d5b 5040 callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
34dc7c2f 5041 callback->awcb_ready = ready;
e8b96c60 5042 callback->awcb_physdone = physdone;
34dc7c2f
BB
5043 callback->awcb_done = done;
5044 callback->awcb_private = private;
5045 callback->awcb_buf = buf;
b128c09f 5046
428870ff 5047 zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
e8b96c60
MA
5048 arc_write_ready, arc_write_physdone, arc_write_done, callback,
5049 priority, zio_flags, zb);
34dc7c2f
BB
5050
5051 return (zio);
5052}
5053
34dc7c2f 5054static int
e8b96c60 5055arc_memory_throttle(uint64_t reserve, uint64_t txg)
34dc7c2f
BB
5056{
5057#ifdef _KERNEL
7e8bddd0
BB
5058 uint64_t available_memory = ptob(freemem);
5059 static uint64_t page_load = 0;
5060 static uint64_t last_txg = 0;
5061#ifdef __linux__
5062 pgcnt_t minfree = btop(arc_sys_free / 4);
5063#endif
0c5493d4 5064
ca67b33a
MA
5065 if (freemem > physmem * arc_lotsfree_percent / 100)
5066 return (0);
5067
7e8bddd0
BB
5068 if (txg > last_txg) {
5069 last_txg = txg;
5070 page_load = 0;
5071 }
5072
5073 /*
5074 * If we are in pageout, we know that memory is already tight,
5075 * the arc is already going to be evicting, so we just want to
5076 * continue to let page writes occur as quickly as possible.
5077 */
5078 if (current_is_kswapd()) {
5079 if (page_load > MAX(ptob(minfree), available_memory) / 4) {
5080 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim);
5081 return (SET_ERROR(ERESTART));
5082 }
5083 /* Note: reserve is inflated, so we deflate */
5084 page_load += reserve / 8;
5085 return (0);
5086 } else if (page_load > 0 && arc_reclaim_needed()) {
ca67b33a 5087 /* memory is low, delay before restarting */
34dc7c2f 5088 ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
570827e1 5089 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim);
2e528b49 5090 return (SET_ERROR(EAGAIN));
34dc7c2f 5091 }
7e8bddd0 5092 page_load = 0;
34dc7c2f
BB
5093#endif
5094 return (0);
5095}
5096
5097void
5098arc_tempreserve_clear(uint64_t reserve)
5099{
5100 atomic_add_64(&arc_tempreserve, -reserve);
5101 ASSERT((int64_t)arc_tempreserve >= 0);
5102}
5103
5104int
5105arc_tempreserve_space(uint64_t reserve, uint64_t txg)
5106{
5107 int error;
9babb374 5108 uint64_t anon_size;
34dc7c2f 5109
34dc7c2f
BB
5110 if (reserve > arc_c/4 && !arc_no_grow)
5111 arc_c = MIN(arc_c_max, reserve * 4);
12f9a6a3
BB
5112
5113 /*
5114 * Throttle when the calculated memory footprint for the TXG
5115 * exceeds the target ARC size.
5116 */
570827e1
BB
5117 if (reserve > arc_c) {
5118 DMU_TX_STAT_BUMP(dmu_tx_memory_reserve);
12f9a6a3 5119 return (SET_ERROR(ERESTART));
570827e1 5120 }
34dc7c2f 5121
9babb374
BB
5122 /*
5123 * Don't count loaned bufs as in flight dirty data to prevent long
5124 * network delays from blocking transactions that are ready to be
5125 * assigned to a txg.
5126 */
36da08ef
PS
5127 anon_size = MAX((int64_t)(refcount_count(&arc_anon->arcs_size) -
5128 arc_loaned_bytes), 0);
9babb374 5129
34dc7c2f
BB
5130 /*
5131 * Writes will, almost always, require additional memory allocations
d3cc8b15 5132 * in order to compress/encrypt/etc the data. We therefore need to
34dc7c2f
BB
5133 * make sure that there is sufficient available memory for this.
5134 */
e8b96c60
MA
5135 error = arc_memory_throttle(reserve, txg);
5136 if (error != 0)
34dc7c2f
BB
5137 return (error);
5138
5139 /*
5140 * Throttle writes when the amount of dirty data in the cache
5141 * gets too large. We try to keep the cache less than half full
5142 * of dirty blocks so that our sync times don't grow too large.
5143 * Note: if two requests come in concurrently, we might let them
5144 * both succeed, when one of them should fail. Not a huge deal.
5145 */
9babb374
BB
5146
5147 if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
5148 anon_size > arc_c / 4) {
34dc7c2f
BB
5149 dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
5150 "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
5151 arc_tempreserve>>10,
5152 arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
5153 arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
5154 reserve>>10, arc_c>>10);
570827e1 5155 DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle);
2e528b49 5156 return (SET_ERROR(ERESTART));
34dc7c2f
BB
5157 }
5158 atomic_add_64(&arc_tempreserve, reserve);
5159 return (0);
5160}
5161
13be560d
BB
5162static void
5163arc_kstat_update_state(arc_state_t *state, kstat_named_t *size,
5164 kstat_named_t *evict_data, kstat_named_t *evict_metadata)
5165{
36da08ef 5166 size->value.ui64 = refcount_count(&state->arcs_size);
13be560d
BB
5167 evict_data->value.ui64 = state->arcs_lsize[ARC_BUFC_DATA];
5168 evict_metadata->value.ui64 = state->arcs_lsize[ARC_BUFC_METADATA];
5169}
5170
5171static int
5172arc_kstat_update(kstat_t *ksp, int rw)
5173{
5174 arc_stats_t *as = ksp->ks_data;
5175
5176 if (rw == KSTAT_WRITE) {
500445c0 5177 return (EACCES);
13be560d
BB
5178 } else {
5179 arc_kstat_update_state(arc_anon,
5180 &as->arcstat_anon_size,
500445c0
PS
5181 &as->arcstat_anon_evictable_data,
5182 &as->arcstat_anon_evictable_metadata);
13be560d
BB
5183 arc_kstat_update_state(arc_mru,
5184 &as->arcstat_mru_size,
500445c0
PS
5185 &as->arcstat_mru_evictable_data,
5186 &as->arcstat_mru_evictable_metadata);
13be560d
BB
5187 arc_kstat_update_state(arc_mru_ghost,
5188 &as->arcstat_mru_ghost_size,
500445c0
PS
5189 &as->arcstat_mru_ghost_evictable_data,
5190 &as->arcstat_mru_ghost_evictable_metadata);
13be560d
BB
5191 arc_kstat_update_state(arc_mfu,
5192 &as->arcstat_mfu_size,
500445c0
PS
5193 &as->arcstat_mfu_evictable_data,
5194 &as->arcstat_mfu_evictable_metadata);
fc41c640 5195 arc_kstat_update_state(arc_mfu_ghost,
13be560d 5196 &as->arcstat_mfu_ghost_size,
500445c0
PS
5197 &as->arcstat_mfu_ghost_evictable_data,
5198 &as->arcstat_mfu_ghost_evictable_metadata);
13be560d
BB
5199 }
5200
5201 return (0);
5202}
5203
ca0bf58d
PS
5204/*
5205 * This function *must* return indices evenly distributed between all
5206 * sublists of the multilist. This is needed due to how the ARC eviction
5207 * code is laid out; arc_evict_state() assumes ARC buffers are evenly
5208 * distributed between all sublists and uses this assumption when
5209 * deciding which sublist to evict from and how much to evict from it.
5210 */
5211unsigned int
5212arc_state_multilist_index_func(multilist_t *ml, void *obj)
5213{
5214 arc_buf_hdr_t *hdr = obj;
5215
5216 /*
5217 * We rely on b_dva to generate evenly distributed index
5218 * numbers using buf_hash below. So, as an added precaution,
5219 * let's make sure we never add empty buffers to the arc lists.
5220 */
5221 ASSERT(!BUF_EMPTY(hdr));
5222
5223 /*
5224 * The assumption here, is the hash value for a given
5225 * arc_buf_hdr_t will remain constant throughout its lifetime
5226 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
5227 * Thus, we don't need to store the header's sublist index
5228 * on insertion, as this index can be recalculated on removal.
5229 *
5230 * Also, the low order bits of the hash value are thought to be
5231 * distributed evenly. Otherwise, in the case that the multilist
5232 * has a power of two number of sublists, each sublists' usage
5233 * would not be evenly distributed.
5234 */
5235 return (buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) %
5236 multilist_get_num_sublists(ml));
5237}
5238
ca67b33a
MA
5239/*
5240 * Called during module initialization and periodically thereafter to
5241 * apply reasonable changes to the exposed performance tunings. Non-zero
5242 * zfs_* values which differ from the currently set values will be applied.
5243 */
5244static void
5245arc_tuning_update(void)
5246{
5247 /* Valid range: 64M - <all physical memory> */
5248 if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) &&
5249 (zfs_arc_max > 64 << 20) && (zfs_arc_max < ptob(physmem)) &&
5250 (zfs_arc_max > arc_c_min)) {
5251 arc_c_max = zfs_arc_max;
5252 arc_c = arc_c_max;
5253 arc_p = (arc_c >> 1);
5254 arc_meta_limit = MIN(arc_meta_limit, arc_c_max);
5255 }
5256
5257 /* Valid range: 32M - <arc_c_max> */
5258 if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) &&
5259 (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) &&
5260 (zfs_arc_min <= arc_c_max)) {
5261 arc_c_min = zfs_arc_min;
5262 arc_c = MAX(arc_c, arc_c_min);
5263 }
5264
5265 /* Valid range: 16M - <arc_c_max> */
5266 if ((zfs_arc_meta_min) && (zfs_arc_meta_min != arc_meta_min) &&
5267 (zfs_arc_meta_min >= 1ULL << SPA_MAXBLOCKSHIFT) &&
5268 (zfs_arc_meta_min <= arc_c_max)) {
5269 arc_meta_min = zfs_arc_meta_min;
5270 arc_meta_limit = MAX(arc_meta_limit, arc_meta_min);
5271 }
5272
5273 /* Valid range: <arc_meta_min> - <arc_c_max> */
5274 if ((zfs_arc_meta_limit) && (zfs_arc_meta_limit != arc_meta_limit) &&
5275 (zfs_arc_meta_limit >= zfs_arc_meta_min) &&
5276 (zfs_arc_meta_limit <= arc_c_max))
5277 arc_meta_limit = zfs_arc_meta_limit;
5278
5279 /* Valid range: 1 - N */
5280 if (zfs_arc_grow_retry)
5281 arc_grow_retry = zfs_arc_grow_retry;
5282
5283 /* Valid range: 1 - N */
5284 if (zfs_arc_shrink_shift) {
5285 arc_shrink_shift = zfs_arc_shrink_shift;
5286 arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1);
5287 }
5288
728d6ae9
BB
5289 /* Valid range: 1 - N */
5290 if (zfs_arc_p_min_shift)
5291 arc_p_min_shift = zfs_arc_p_min_shift;
5292
ca67b33a
MA
5293 /* Valid range: 1 - N ticks */
5294 if (zfs_arc_min_prefetch_lifespan)
5295 arc_min_prefetch_lifespan = zfs_arc_min_prefetch_lifespan;
11f552fa 5296
7e8bddd0
BB
5297 /* Valid range: 0 - 100 */
5298 if ((zfs_arc_lotsfree_percent >= 0) &&
5299 (zfs_arc_lotsfree_percent <= 100))
5300 arc_lotsfree_percent = zfs_arc_lotsfree_percent;
5301
11f552fa
BB
5302 /* Valid range: 0 - <all physical memory> */
5303 if ((zfs_arc_sys_free) && (zfs_arc_sys_free != arc_sys_free))
5304 arc_sys_free = MIN(MAX(zfs_arc_sys_free, 0), ptob(physmem));
7e8bddd0 5305
ca67b33a
MA
5306}
5307
34dc7c2f
BB
5308void
5309arc_init(void)
5310{
ca67b33a
MA
5311 /*
5312 * allmem is "all memory that we could possibly use".
5313 */
5314#ifdef _KERNEL
5315 uint64_t allmem = ptob(physmem);
5316#else
5317 uint64_t allmem = (physmem * PAGESIZE) / 2;
5318#endif
5319
ca0bf58d
PS
5320 mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
5321 cv_init(&arc_reclaim_thread_cv, NULL, CV_DEFAULT, NULL);
5322 cv_init(&arc_reclaim_waiters_cv, NULL, CV_DEFAULT, NULL);
5323
5324 mutex_init(&arc_user_evicts_lock, NULL, MUTEX_DEFAULT, NULL);
5325 cv_init(&arc_user_evicts_cv, NULL, CV_DEFAULT, NULL);
34dc7c2f
BB
5326
5327 /* Convert seconds to clock ticks */
ca67b33a 5328 arc_min_prefetch_lifespan = 1 * hz;
34dc7c2f
BB
5329
5330 /* Start out with 1/8 of all memory */
ca67b33a 5331 arc_c = allmem / 8;
34dc7c2f
BB
5332
5333#ifdef _KERNEL
5334 /*
5335 * On architectures where the physical memory can be larger
5336 * than the addressable space (intel in 32-bit mode), we may
5337 * need to limit the cache to 1/8 of VM size.
5338 */
5339 arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
ca67b33a 5340
7cb67b45
BB
5341 /*
5342 * Register a shrinker to support synchronous (direct) memory
5343 * reclaim from the arc. This is done to prevent kswapd from
5344 * swapping out pages when it is preferable to shrink the arc.
5345 */
5346 spl_register_shrinker(&arc_shrinker);
11f552fa
BB
5347
5348 /* Set to 1/64 of all memory or a minimum of 512K */
5349 arc_sys_free = MAX(ptob(physmem / 64), (512 * 1024));
5350 arc_need_free = 0;
34dc7c2f
BB
5351#endif
5352
ca67b33a 5353 /* Set min cache to allow safe operation of arc_adapt() */
121b3cae 5354 arc_c_min = 2ULL << SPA_MAXBLOCKSHIFT;
ca67b33a
MA
5355 /* Set max to 1/2 of all memory */
5356 arc_c_max = allmem / 2;
34dc7c2f
BB
5357
5358 arc_c = arc_c_max;
5359 arc_p = (arc_c >> 1);
5360
ca67b33a
MA
5361 /* Set min to 1/2 of arc_c_min */
5362 arc_meta_min = 1ULL << SPA_MAXBLOCKSHIFT;
5363 /* Initialize maximum observed usage to zero */
1834f2d8 5364 arc_meta_max = 0;
ca67b33a
MA
5365 /* Set limit to 3/4 of arc_c_max with a floor of arc_meta_min */
5366 arc_meta_limit = MAX((3 * arc_c_max) / 4, arc_meta_min);
34dc7c2f 5367
ca67b33a
MA
5368 /* Apply user specified tunings */
5369 arc_tuning_update();
c52fca13 5370
ca0bf58d 5371 if (zfs_arc_num_sublists_per_state < 1)
ca67b33a 5372 zfs_arc_num_sublists_per_state = MAX(boot_ncpus, 1);
ca0bf58d 5373
34dc7c2f
BB
5374 /* if kmem_flags are set, lets try to use less memory */
5375 if (kmem_debugging())
5376 arc_c = arc_c / 2;
5377 if (arc_c < arc_c_min)
5378 arc_c = arc_c_min;
5379
5380 arc_anon = &ARC_anon;
5381 arc_mru = &ARC_mru;
5382 arc_mru_ghost = &ARC_mru_ghost;
5383 arc_mfu = &ARC_mfu;
5384 arc_mfu_ghost = &ARC_mfu_ghost;
5385 arc_l2c_only = &ARC_l2c_only;
5386 arc_size = 0;
5387
ca0bf58d 5388 multilist_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
b9541d6b 5389 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5390 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5391 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5392 multilist_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
b9541d6b 5393 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5394 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5395 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5396 multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
b9541d6b 5397 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5398 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5399 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5400 multilist_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
b9541d6b 5401 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5402 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5403 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5404 multilist_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
b9541d6b 5405 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5406 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5407 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5408 multilist_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
b9541d6b 5409 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5410 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5411 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5412 multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
b9541d6b 5413 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5414 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5415 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5416 multilist_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
b9541d6b 5417 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5418 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5419 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5420 multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
b9541d6b 5421 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5422 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5423 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
5424 multilist_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
b9541d6b 5425 sizeof (arc_buf_hdr_t),
ca0bf58d
PS
5426 offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node),
5427 zfs_arc_num_sublists_per_state, arc_state_multilist_index_func);
34dc7c2f 5428
e0b0ca98
BB
5429 arc_anon->arcs_state = ARC_STATE_ANON;
5430 arc_mru->arcs_state = ARC_STATE_MRU;
5431 arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST;
5432 arc_mfu->arcs_state = ARC_STATE_MFU;
5433 arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST;
5434 arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY;
5435
36da08ef
PS
5436 refcount_create(&arc_anon->arcs_size);
5437 refcount_create(&arc_mru->arcs_size);
5438 refcount_create(&arc_mru_ghost->arcs_size);
5439 refcount_create(&arc_mfu->arcs_size);
5440 refcount_create(&arc_mfu_ghost->arcs_size);
5441 refcount_create(&arc_l2c_only->arcs_size);
5442
34dc7c2f
BB
5443 buf_init();
5444
ca0bf58d
PS
5445 arc_reclaim_thread_exit = FALSE;
5446 arc_user_evicts_thread_exit = FALSE;
ab26409d
BB
5447 list_create(&arc_prune_list, sizeof (arc_prune_t),
5448 offsetof(arc_prune_t, p_node));
34dc7c2f 5449 arc_eviction_list = NULL;
ab26409d 5450 mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f
BB
5451 bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
5452
1229323d 5453 arc_prune_taskq = taskq_create("arc_prune", max_ncpus, defclsyspri,
aa9af22c 5454 max_ncpus, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
f6046738 5455
34dc7c2f
BB
5456 arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
5457 sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
5458
5459 if (arc_ksp != NULL) {
5460 arc_ksp->ks_data = &arc_stats;
13be560d 5461 arc_ksp->ks_update = arc_kstat_update;
34dc7c2f
BB
5462 kstat_install(arc_ksp);
5463 }
5464
ca67b33a 5465 (void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
1229323d 5466 TS_RUN, defclsyspri);
34dc7c2f 5467
ca0bf58d 5468 (void) thread_create(NULL, 0, arc_user_evicts_thread, NULL, 0, &p0,
1229323d 5469 TS_RUN, defclsyspri);
ca0bf58d 5470
34dc7c2f 5471 arc_dead = FALSE;
b128c09f 5472 arc_warm = B_FALSE;
34dc7c2f 5473
e8b96c60
MA
5474 /*
5475 * Calculate maximum amount of dirty data per pool.
5476 *
5477 * If it has been set by a module parameter, take that.
5478 * Otherwise, use a percentage of physical memory defined by
5479 * zfs_dirty_data_max_percent (default 10%) with a cap at
5480 * zfs_dirty_data_max_max (default 25% of physical memory).
5481 */
5482 if (zfs_dirty_data_max_max == 0)
5483 zfs_dirty_data_max_max = physmem * PAGESIZE *
5484 zfs_dirty_data_max_max_percent / 100;
5485
5486 if (zfs_dirty_data_max == 0) {
5487 zfs_dirty_data_max = physmem * PAGESIZE *
5488 zfs_dirty_data_max_percent / 100;
5489 zfs_dirty_data_max = MIN(zfs_dirty_data_max,
5490 zfs_dirty_data_max_max);
5491 }
34dc7c2f
BB
5492}
5493
5494void
5495arc_fini(void)
5496{
ab26409d
BB
5497 arc_prune_t *p;
5498
7cb67b45
BB
5499#ifdef _KERNEL
5500 spl_unregister_shrinker(&arc_shrinker);
5501#endif /* _KERNEL */
5502
ca0bf58d
PS
5503 mutex_enter(&arc_reclaim_lock);
5504 arc_reclaim_thread_exit = TRUE;
5505 /*
5506 * The reclaim thread will set arc_reclaim_thread_exit back to
5507 * FALSE when it is finished exiting; we're waiting for that.
5508 */
5509 while (arc_reclaim_thread_exit) {
5510 cv_signal(&arc_reclaim_thread_cv);
5511 cv_wait(&arc_reclaim_thread_cv, &arc_reclaim_lock);
5512 }
5513 mutex_exit(&arc_reclaim_lock);
5514
5515 mutex_enter(&arc_user_evicts_lock);
5516 arc_user_evicts_thread_exit = TRUE;
5517 /*
5518 * The user evicts thread will set arc_user_evicts_thread_exit
5519 * to FALSE when it is finished exiting; we're waiting for that.
5520 */
5521 while (arc_user_evicts_thread_exit) {
5522 cv_signal(&arc_user_evicts_cv);
5523 cv_wait(&arc_user_evicts_cv, &arc_user_evicts_lock);
5524 }
5525 mutex_exit(&arc_user_evicts_lock);
34dc7c2f 5526
ca0bf58d
PS
5527 /* Use TRUE to ensure *all* buffers are evicted */
5528 arc_flush(NULL, TRUE);
34dc7c2f
BB
5529
5530 arc_dead = TRUE;
5531
5532 if (arc_ksp != NULL) {
5533 kstat_delete(arc_ksp);
5534 arc_ksp = NULL;
5535 }
5536
f6046738
BB
5537 taskq_wait(arc_prune_taskq);
5538 taskq_destroy(arc_prune_taskq);
5539
ab26409d
BB
5540 mutex_enter(&arc_prune_mtx);
5541 while ((p = list_head(&arc_prune_list)) != NULL) {
5542 list_remove(&arc_prune_list, p);
5543 refcount_remove(&p->p_refcnt, &arc_prune_list);
5544 refcount_destroy(&p->p_refcnt);
5545 kmem_free(p, sizeof (*p));
5546 }
5547 mutex_exit(&arc_prune_mtx);
5548
5549 list_destroy(&arc_prune_list);
5550 mutex_destroy(&arc_prune_mtx);
ca0bf58d
PS
5551 mutex_destroy(&arc_reclaim_lock);
5552 cv_destroy(&arc_reclaim_thread_cv);
5553 cv_destroy(&arc_reclaim_waiters_cv);
5554
5555 mutex_destroy(&arc_user_evicts_lock);
5556 cv_destroy(&arc_user_evicts_cv);
5557
36da08ef
PS
5558 refcount_destroy(&arc_anon->arcs_size);
5559 refcount_destroy(&arc_mru->arcs_size);
5560 refcount_destroy(&arc_mru_ghost->arcs_size);
5561 refcount_destroy(&arc_mfu->arcs_size);
5562 refcount_destroy(&arc_mfu_ghost->arcs_size);
5563 refcount_destroy(&arc_l2c_only->arcs_size);
5564
ca0bf58d
PS
5565 multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
5566 multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
5567 multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
5568 multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
5569 multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
5570 multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
5571 multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
5572 multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
5573 multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA]);
5574 multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]);
34dc7c2f
BB
5575
5576 buf_fini();
9babb374 5577
b9541d6b 5578 ASSERT0(arc_loaned_bytes);
34dc7c2f
BB
5579}
5580
5581/*
5582 * Level 2 ARC
5583 *
5584 * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
5585 * It uses dedicated storage devices to hold cached data, which are populated
5586 * using large infrequent writes. The main role of this cache is to boost
5587 * the performance of random read workloads. The intended L2ARC devices
5588 * include short-stroked disks, solid state disks, and other media with
5589 * substantially faster read latency than disk.
5590 *
5591 * +-----------------------+
5592 * | ARC |
5593 * +-----------------------+
5594 * | ^ ^
5595 * | | |
5596 * l2arc_feed_thread() arc_read()
5597 * | | |
5598 * | l2arc read |
5599 * V | |
5600 * +---------------+ |
5601 * | L2ARC | |
5602 * +---------------+ |
5603 * | ^ |
5604 * l2arc_write() | |
5605 * | | |
5606 * V | |
5607 * +-------+ +-------+
5608 * | vdev | | vdev |
5609 * | cache | | cache |
5610 * +-------+ +-------+
5611 * +=========+ .-----.
5612 * : L2ARC : |-_____-|
5613 * : devices : | Disks |
5614 * +=========+ `-_____-'
5615 *
5616 * Read requests are satisfied from the following sources, in order:
5617 *
5618 * 1) ARC
5619 * 2) vdev cache of L2ARC devices
5620 * 3) L2ARC devices
5621 * 4) vdev cache of disks
5622 * 5) disks
5623 *
5624 * Some L2ARC device types exhibit extremely slow write performance.
5625 * To accommodate for this there are some significant differences between
5626 * the L2ARC and traditional cache design:
5627 *
5628 * 1. There is no eviction path from the ARC to the L2ARC. Evictions from
5629 * the ARC behave as usual, freeing buffers and placing headers on ghost
5630 * lists. The ARC does not send buffers to the L2ARC during eviction as
5631 * this would add inflated write latencies for all ARC memory pressure.
5632 *
5633 * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
5634 * It does this by periodically scanning buffers from the eviction-end of
5635 * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
3a17a7a9
SK
5636 * not already there. It scans until a headroom of buffers is satisfied,
5637 * which itself is a buffer for ARC eviction. If a compressible buffer is
5638 * found during scanning and selected for writing to an L2ARC device, we
5639 * temporarily boost scanning headroom during the next scan cycle to make
5640 * sure we adapt to compression effects (which might significantly reduce
5641 * the data volume we write to L2ARC). The thread that does this is
34dc7c2f
BB
5642 * l2arc_feed_thread(), illustrated below; example sizes are included to
5643 * provide a better sense of ratio than this diagram:
5644 *
5645 * head --> tail
5646 * +---------------------+----------+
5647 * ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->. # already on L2ARC
5648 * +---------------------+----------+ | o L2ARC eligible
5649 * ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->| : ARC buffer
5650 * +---------------------+----------+ |
5651 * 15.9 Gbytes ^ 32 Mbytes |
5652 * headroom |
5653 * l2arc_feed_thread()
5654 * |
5655 * l2arc write hand <--[oooo]--'
5656 * | 8 Mbyte
5657 * | write max
5658 * V
5659 * +==============================+
5660 * L2ARC dev |####|#|###|###| |####| ... |
5661 * +==============================+
5662 * 32 Gbytes
5663 *
5664 * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
5665 * evicted, then the L2ARC has cached a buffer much sooner than it probably
5666 * needed to, potentially wasting L2ARC device bandwidth and storage. It is
5667 * safe to say that this is an uncommon case, since buffers at the end of
5668 * the ARC lists have moved there due to inactivity.
5669 *
5670 * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
5671 * then the L2ARC simply misses copying some buffers. This serves as a
5672 * pressure valve to prevent heavy read workloads from both stalling the ARC
5673 * with waits and clogging the L2ARC with writes. This also helps prevent
5674 * the potential for the L2ARC to churn if it attempts to cache content too
5675 * quickly, such as during backups of the entire pool.
5676 *
b128c09f
BB
5677 * 5. After system boot and before the ARC has filled main memory, there are
5678 * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
5679 * lists can remain mostly static. Instead of searching from tail of these
5680 * lists as pictured, the l2arc_feed_thread() will search from the list heads
5681 * for eligible buffers, greatly increasing its chance of finding them.
5682 *
5683 * The L2ARC device write speed is also boosted during this time so that
5684 * the L2ARC warms up faster. Since there have been no ARC evictions yet,
5685 * there are no L2ARC reads, and no fear of degrading read performance
5686 * through increased writes.
5687 *
5688 * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
34dc7c2f
BB
5689 * the vdev queue can aggregate them into larger and fewer writes. Each
5690 * device is written to in a rotor fashion, sweeping writes through
5691 * available space then repeating.
5692 *
b128c09f 5693 * 7. The L2ARC does not store dirty content. It never needs to flush
34dc7c2f
BB
5694 * write buffers back to disk based storage.
5695 *
b128c09f 5696 * 8. If an ARC buffer is written (and dirtied) which also exists in the
34dc7c2f
BB
5697 * L2ARC, the now stale L2ARC buffer is immediately dropped.
5698 *
5699 * The performance of the L2ARC can be tweaked by a number of tunables, which
5700 * may be necessary for different workloads:
5701 *
5702 * l2arc_write_max max write bytes per interval
b128c09f 5703 * l2arc_write_boost extra write bytes during device warmup
34dc7c2f 5704 * l2arc_noprefetch skip caching prefetched buffers
3a17a7a9 5705 * l2arc_nocompress skip compressing buffers
34dc7c2f 5706 * l2arc_headroom number of max device writes to precache
3a17a7a9
SK
5707 * l2arc_headroom_boost when we find compressed buffers during ARC
5708 * scanning, we multiply headroom by this
5709 * percentage factor for the next scan cycle,
5710 * since more compressed buffers are likely to
5711 * be present
34dc7c2f
BB
5712 * l2arc_feed_secs seconds between L2ARC writing
5713 *
5714 * Tunables may be removed or added as future performance improvements are
5715 * integrated, and also may become zpool properties.
d164b209
BB
5716 *
5717 * There are three key functions that control how the L2ARC warms up:
5718 *
5719 * l2arc_write_eligible() check if a buffer is eligible to cache
5720 * l2arc_write_size() calculate how much to write
5721 * l2arc_write_interval() calculate sleep delay between writes
5722 *
5723 * These three functions determine what to write, how much, and how quickly
5724 * to send writes.
34dc7c2f
BB
5725 */
5726
d164b209 5727static boolean_t
2a432414 5728l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr)
d164b209
BB
5729{
5730 /*
5731 * A buffer is *not* eligible for the L2ARC if it:
5732 * 1. belongs to a different spa.
428870ff
BB
5733 * 2. is already cached on the L2ARC.
5734 * 3. has an I/O in progress (it may be an incomplete read).
5735 * 4. is flagged not eligible (zfs property).
d164b209 5736 */
b9541d6b 5737 if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) ||
2a432414 5738 HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr))
d164b209
BB
5739 return (B_FALSE);
5740
5741 return (B_TRUE);
5742}
5743
5744static uint64_t
3a17a7a9 5745l2arc_write_size(void)
d164b209
BB
5746{
5747 uint64_t size;
5748
3a17a7a9
SK
5749 /*
5750 * Make sure our globals have meaningful values in case the user
5751 * altered them.
5752 */
5753 size = l2arc_write_max;
5754 if (size == 0) {
5755 cmn_err(CE_NOTE, "Bad value for l2arc_write_max, value must "
5756 "be greater than zero, resetting it to the default (%d)",
5757 L2ARC_WRITE_SIZE);
5758 size = l2arc_write_max = L2ARC_WRITE_SIZE;
5759 }
d164b209
BB
5760
5761 if (arc_warm == B_FALSE)
3a17a7a9 5762 size += l2arc_write_boost;
d164b209
BB
5763
5764 return (size);
5765
5766}
5767
5768static clock_t
5769l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
5770{
428870ff 5771 clock_t interval, next, now;
d164b209
BB
5772
5773 /*
5774 * If the ARC lists are busy, increase our write rate; if the
5775 * lists are stale, idle back. This is achieved by checking
5776 * how much we previously wrote - if it was more than half of
5777 * what we wanted, schedule the next write much sooner.
5778 */
5779 if (l2arc_feed_again && wrote > (wanted / 2))
5780 interval = (hz * l2arc_feed_min_ms) / 1000;
5781 else
5782 interval = hz * l2arc_feed_secs;
5783
428870ff
BB
5784 now = ddi_get_lbolt();
5785 next = MAX(now, MIN(now + interval, began + interval));
d164b209
BB
5786
5787 return (next);
5788}
5789
34dc7c2f
BB
5790/*
5791 * Cycle through L2ARC devices. This is how L2ARC load balances.
b128c09f 5792 * If a device is returned, this also returns holding the spa config lock.
34dc7c2f
BB
5793 */
5794static l2arc_dev_t *
5795l2arc_dev_get_next(void)
5796{
b128c09f 5797 l2arc_dev_t *first, *next = NULL;
34dc7c2f 5798
b128c09f
BB
5799 /*
5800 * Lock out the removal of spas (spa_namespace_lock), then removal
5801 * of cache devices (l2arc_dev_mtx). Once a device has been selected,
5802 * both locks will be dropped and a spa config lock held instead.
5803 */
5804 mutex_enter(&spa_namespace_lock);
5805 mutex_enter(&l2arc_dev_mtx);
5806
5807 /* if there are no vdevs, there is nothing to do */
5808 if (l2arc_ndev == 0)
5809 goto out;
5810
5811 first = NULL;
5812 next = l2arc_dev_last;
5813 do {
5814 /* loop around the list looking for a non-faulted vdev */
5815 if (next == NULL) {
34dc7c2f 5816 next = list_head(l2arc_dev_list);
b128c09f
BB
5817 } else {
5818 next = list_next(l2arc_dev_list, next);
5819 if (next == NULL)
5820 next = list_head(l2arc_dev_list);
5821 }
5822
5823 /* if we have come back to the start, bail out */
5824 if (first == NULL)
5825 first = next;
5826 else if (next == first)
5827 break;
5828
5829 } while (vdev_is_dead(next->l2ad_vdev));
5830
5831 /* if we were unable to find any usable vdevs, return NULL */
5832 if (vdev_is_dead(next->l2ad_vdev))
5833 next = NULL;
34dc7c2f
BB
5834
5835 l2arc_dev_last = next;
5836
b128c09f
BB
5837out:
5838 mutex_exit(&l2arc_dev_mtx);
5839
5840 /*
5841 * Grab the config lock to prevent the 'next' device from being
5842 * removed while we are writing to it.
5843 */
5844 if (next != NULL)
5845 spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
5846 mutex_exit(&spa_namespace_lock);
5847
34dc7c2f
BB
5848 return (next);
5849}
5850
b128c09f
BB
5851/*
5852 * Free buffers that were tagged for destruction.
5853 */
5854static void
0bc8fd78 5855l2arc_do_free_on_write(void)
b128c09f
BB
5856{
5857 list_t *buflist;
5858 l2arc_data_free_t *df, *df_prev;
5859
5860 mutex_enter(&l2arc_free_on_write_mtx);
5861 buflist = l2arc_free_on_write;
5862
5863 for (df = list_tail(buflist); df; df = df_prev) {
5864 df_prev = list_prev(buflist, df);
5865 ASSERT(df->l2df_data != NULL);
5866 ASSERT(df->l2df_func != NULL);
5867 df->l2df_func(df->l2df_data, df->l2df_size);
5868 list_remove(buflist, df);
5869 kmem_free(df, sizeof (l2arc_data_free_t));
5870 }
5871
5872 mutex_exit(&l2arc_free_on_write_mtx);
5873}
5874
34dc7c2f
BB
5875/*
5876 * A write to a cache device has completed. Update all headers to allow
5877 * reads from these buffers to begin.
5878 */
5879static void
5880l2arc_write_done(zio_t *zio)
5881{
5882 l2arc_write_callback_t *cb;
5883 l2arc_dev_t *dev;
5884 list_t *buflist;
2a432414 5885 arc_buf_hdr_t *head, *hdr, *hdr_prev;
34dc7c2f 5886 kmutex_t *hash_lock;
3bec585e 5887 int64_t bytes_dropped = 0;
34dc7c2f
BB
5888
5889 cb = zio->io_private;
5890 ASSERT(cb != NULL);
5891 dev = cb->l2wcb_dev;
5892 ASSERT(dev != NULL);
5893 head = cb->l2wcb_head;
5894 ASSERT(head != NULL);
b9541d6b 5895 buflist = &dev->l2ad_buflist;
34dc7c2f
BB
5896 ASSERT(buflist != NULL);
5897 DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
5898 l2arc_write_callback_t *, cb);
5899
5900 if (zio->io_error != 0)
5901 ARCSTAT_BUMP(arcstat_l2_writes_error);
5902
34dc7c2f
BB
5903 /*
5904 * All writes completed, or an error was hit.
5905 */
ca0bf58d
PS
5906top:
5907 mutex_enter(&dev->l2ad_mtx);
2a432414
GW
5908 for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) {
5909 hdr_prev = list_prev(buflist, hdr);
34dc7c2f 5910
2a432414 5911 hash_lock = HDR_LOCK(hdr);
ca0bf58d
PS
5912
5913 /*
5914 * We cannot use mutex_enter or else we can deadlock
5915 * with l2arc_write_buffers (due to swapping the order
5916 * the hash lock and l2ad_mtx are taken).
5917 */
34dc7c2f
BB
5918 if (!mutex_tryenter(hash_lock)) {
5919 /*
ca0bf58d
PS
5920 * Missed the hash lock. We must retry so we
5921 * don't leave the ARC_FLAG_L2_WRITING bit set.
34dc7c2f 5922 */
ca0bf58d
PS
5923 ARCSTAT_BUMP(arcstat_l2_writes_lock_retry);
5924
5925 /*
5926 * We don't want to rescan the headers we've
5927 * already marked as having been written out, so
5928 * we reinsert the head node so we can pick up
5929 * where we left off.
5930 */
5931 list_remove(buflist, head);
5932 list_insert_after(buflist, hdr, head);
5933
5934 mutex_exit(&dev->l2ad_mtx);
5935
5936 /*
5937 * We wait for the hash lock to become available
5938 * to try and prevent busy waiting, and increase
5939 * the chance we'll be able to acquire the lock
5940 * the next time around.
5941 */
5942 mutex_enter(hash_lock);
5943 mutex_exit(hash_lock);
5944 goto top;
34dc7c2f
BB
5945 }
5946
b9541d6b 5947 /*
ca0bf58d
PS
5948 * We could not have been moved into the arc_l2c_only
5949 * state while in-flight due to our ARC_FLAG_L2_WRITING
5950 * bit being set. Let's just ensure that's being enforced.
5951 */
5952 ASSERT(HDR_HAS_L1HDR(hdr));
5953
5954 /*
5955 * We may have allocated a buffer for L2ARC compression,
5956 * we must release it to avoid leaking this data.
b9541d6b 5957 */
ca0bf58d 5958 l2arc_release_cdata_buf(hdr);
b9541d6b 5959
34dc7c2f
BB
5960 if (zio->io_error != 0) {
5961 /*
b128c09f 5962 * Error - drop L2ARC entry.
34dc7c2f 5963 */
2a432414 5964 list_remove(buflist, hdr);
b9541d6b
CW
5965 hdr->b_flags &= ~ARC_FLAG_HAS_L2HDR;
5966
5967 ARCSTAT_INCR(arcstat_l2_asize, -hdr->b_l2hdr.b_asize);
2a432414 5968 ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
d962d5da
PS
5969
5970 bytes_dropped += hdr->b_l2hdr.b_asize;
5971 (void) refcount_remove_many(&dev->l2ad_alloc,
5972 hdr->b_l2hdr.b_asize, hdr);
34dc7c2f
BB
5973 }
5974
5975 /*
ca0bf58d
PS
5976 * Allow ARC to begin reads and ghost list evictions to
5977 * this L2ARC entry.
34dc7c2f 5978 */
2a432414 5979 hdr->b_flags &= ~ARC_FLAG_L2_WRITING;
34dc7c2f
BB
5980
5981 mutex_exit(hash_lock);
5982 }
5983
5984 atomic_inc_64(&l2arc_writes_done);
5985 list_remove(buflist, head);
b9541d6b
CW
5986 ASSERT(!HDR_HAS_L1HDR(head));
5987 kmem_cache_free(hdr_l2only_cache, head);
5988 mutex_exit(&dev->l2ad_mtx);
34dc7c2f 5989
3bec585e
SK
5990 vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0);
5991
b128c09f 5992 l2arc_do_free_on_write();
34dc7c2f
BB
5993
5994 kmem_free(cb, sizeof (l2arc_write_callback_t));
5995}
5996
5997/*
5998 * A read to a cache device completed. Validate buffer contents before
5999 * handing over to the regular ARC routines.
6000 */
6001static void
6002l2arc_read_done(zio_t *zio)
6003{
6004 l2arc_read_callback_t *cb;
6005 arc_buf_hdr_t *hdr;
6006 arc_buf_t *buf;
34dc7c2f 6007 kmutex_t *hash_lock;
b128c09f
BB
6008 int equal;
6009
6010 ASSERT(zio->io_vd != NULL);
6011 ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
6012
6013 spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
34dc7c2f
BB
6014
6015 cb = zio->io_private;
6016 ASSERT(cb != NULL);
6017 buf = cb->l2rcb_buf;
6018 ASSERT(buf != NULL);
34dc7c2f 6019
428870ff 6020 hash_lock = HDR_LOCK(buf->b_hdr);
34dc7c2f 6021 mutex_enter(hash_lock);
428870ff
BB
6022 hdr = buf->b_hdr;
6023 ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
34dc7c2f 6024
3a17a7a9
SK
6025 /*
6026 * If the buffer was compressed, decompress it first.
6027 */
6028 if (cb->l2rcb_compress != ZIO_COMPRESS_OFF)
6029 l2arc_decompress_zio(zio, hdr, cb->l2rcb_compress);
6030 ASSERT(zio->io_data != NULL);
4e0f33ff
AJ
6031 ASSERT3U(zio->io_size, ==, hdr->b_size);
6032 ASSERT3U(BP_GET_LSIZE(&cb->l2rcb_bp), ==, hdr->b_size);
3a17a7a9 6033
34dc7c2f
BB
6034 /*
6035 * Check this survived the L2ARC journey.
6036 */
6037 equal = arc_cksum_equal(buf);
6038 if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
6039 mutex_exit(hash_lock);
6040 zio->io_private = buf;
b128c09f
BB
6041 zio->io_bp_copy = cb->l2rcb_bp; /* XXX fix in L2ARC 2.0 */
6042 zio->io_bp = &zio->io_bp_copy; /* XXX fix in L2ARC 2.0 */
34dc7c2f
BB
6043 arc_read_done(zio);
6044 } else {
6045 mutex_exit(hash_lock);
6046 /*
6047 * Buffer didn't survive caching. Increment stats and
6048 * reissue to the original storage device.
6049 */
b128c09f 6050 if (zio->io_error != 0) {
34dc7c2f 6051 ARCSTAT_BUMP(arcstat_l2_io_error);
b128c09f 6052 } else {
2e528b49 6053 zio->io_error = SET_ERROR(EIO);
b128c09f 6054 }
34dc7c2f
BB
6055 if (!equal)
6056 ARCSTAT_BUMP(arcstat_l2_cksum_bad);
6057
34dc7c2f 6058 /*
b128c09f
BB
6059 * If there's no waiter, issue an async i/o to the primary
6060 * storage now. If there *is* a waiter, the caller must
6061 * issue the i/o in a context where it's OK to block.
34dc7c2f 6062 */
d164b209
BB
6063 if (zio->io_waiter == NULL) {
6064 zio_t *pio = zio_unique_parent(zio);
6065
6066 ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
6067
6068 zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4e0f33ff 6069 buf->b_data, hdr->b_size, arc_read_done, buf,
b128c09f 6070 zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
d164b209 6071 }
34dc7c2f
BB
6072 }
6073
6074 kmem_free(cb, sizeof (l2arc_read_callback_t));
6075}
6076
6077/*
6078 * This is the list priority from which the L2ARC will search for pages to
6079 * cache. This is used within loops (0..3) to cycle through lists in the
6080 * desired order. This order can have a significant effect on cache
6081 * performance.
6082 *
6083 * Currently the metadata lists are hit first, MFU then MRU, followed by
6084 * the data lists. This function returns a locked list, and also returns
6085 * the lock pointer.
6086 */
ca0bf58d
PS
6087static multilist_sublist_t *
6088l2arc_sublist_lock(int list_num)
34dc7c2f 6089{
ca0bf58d
PS
6090 multilist_t *ml = NULL;
6091 unsigned int idx;
34dc7c2f
BB
6092
6093 ASSERT(list_num >= 0 && list_num <= 3);
6094
6095 switch (list_num) {
6096 case 0:
ca0bf58d 6097 ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
34dc7c2f
BB
6098 break;
6099 case 1:
ca0bf58d 6100 ml = &arc_mru->arcs_list[ARC_BUFC_METADATA];
34dc7c2f
BB
6101 break;
6102 case 2:
ca0bf58d 6103 ml = &arc_mfu->arcs_list[ARC_BUFC_DATA];
34dc7c2f
BB
6104 break;
6105 case 3:
ca0bf58d 6106 ml = &arc_mru->arcs_list[ARC_BUFC_DATA];
34dc7c2f
BB
6107 break;
6108 }
6109
ca0bf58d
PS
6110 /*
6111 * Return a randomly-selected sublist. This is acceptable
6112 * because the caller feeds only a little bit of data for each
6113 * call (8MB). Subsequent calls will result in different
6114 * sublists being selected.
6115 */
6116 idx = multilist_get_random_index(ml);
6117 return (multilist_sublist_lock(ml, idx));
34dc7c2f
BB
6118}
6119
6120/*
6121 * Evict buffers from the device write hand to the distance specified in
6122 * bytes. This distance may span populated buffers, it may span nothing.
6123 * This is clearing a region on the L2ARC device ready for writing.
6124 * If the 'all' boolean is set, every buffer is evicted.
6125 */
6126static void
6127l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
6128{
6129 list_t *buflist;
2a432414 6130 arc_buf_hdr_t *hdr, *hdr_prev;
34dc7c2f
BB
6131 kmutex_t *hash_lock;
6132 uint64_t taddr;
6133
b9541d6b 6134 buflist = &dev->l2ad_buflist;
34dc7c2f
BB
6135
6136 if (!all && dev->l2ad_first) {
6137 /*
6138 * This is the first sweep through the device. There is
6139 * nothing to evict.
6140 */
6141 return;
6142 }
6143
b128c09f 6144 if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
34dc7c2f
BB
6145 /*
6146 * When nearing the end of the device, evict to the end
6147 * before the device write hand jumps to the start.
6148 */
6149 taddr = dev->l2ad_end;
6150 } else {
6151 taddr = dev->l2ad_hand + distance;
6152 }
6153 DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
6154 uint64_t, taddr, boolean_t, all);
6155
6156top:
b9541d6b 6157 mutex_enter(&dev->l2ad_mtx);
2a432414
GW
6158 for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) {
6159 hdr_prev = list_prev(buflist, hdr);
34dc7c2f 6160
2a432414 6161 hash_lock = HDR_LOCK(hdr);
ca0bf58d
PS
6162
6163 /*
6164 * We cannot use mutex_enter or else we can deadlock
6165 * with l2arc_write_buffers (due to swapping the order
6166 * the hash lock and l2ad_mtx are taken).
6167 */
34dc7c2f
BB
6168 if (!mutex_tryenter(hash_lock)) {
6169 /*
6170 * Missed the hash lock. Retry.
6171 */
6172 ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
b9541d6b 6173 mutex_exit(&dev->l2ad_mtx);
34dc7c2f
BB
6174 mutex_enter(hash_lock);
6175 mutex_exit(hash_lock);
6176 goto top;
6177 }
6178
2a432414 6179 if (HDR_L2_WRITE_HEAD(hdr)) {
34dc7c2f
BB
6180 /*
6181 * We hit a write head node. Leave it for
6182 * l2arc_write_done().
6183 */
2a432414 6184 list_remove(buflist, hdr);
34dc7c2f
BB
6185 mutex_exit(hash_lock);
6186 continue;
6187 }
6188
b9541d6b
CW
6189 if (!all && HDR_HAS_L2HDR(hdr) &&
6190 (hdr->b_l2hdr.b_daddr > taddr ||
6191 hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) {
34dc7c2f
BB
6192 /*
6193 * We've evicted to the target address,
6194 * or the end of the device.
6195 */
6196 mutex_exit(hash_lock);
6197 break;
6198 }
6199
b9541d6b
CW
6200 ASSERT(HDR_HAS_L2HDR(hdr));
6201 if (!HDR_HAS_L1HDR(hdr)) {
2a432414 6202 ASSERT(!HDR_L2_READING(hdr));
34dc7c2f
BB
6203 /*
6204 * This doesn't exist in the ARC. Destroy.
6205 * arc_hdr_destroy() will call list_remove()
6206 * and decrement arcstat_l2_size.
6207 */
2a432414
GW
6208 arc_change_state(arc_anon, hdr, hash_lock);
6209 arc_hdr_destroy(hdr);
34dc7c2f 6210 } else {
b9541d6b
CW
6211 ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only);
6212 ARCSTAT_BUMP(arcstat_l2_evict_l1cached);
b128c09f
BB
6213 /*
6214 * Invalidate issued or about to be issued
6215 * reads, since we may be about to write
6216 * over this location.
6217 */
2a432414 6218 if (HDR_L2_READING(hdr)) {
b128c09f 6219 ARCSTAT_BUMP(arcstat_l2_evict_reading);
2a432414 6220 hdr->b_flags |= ARC_FLAG_L2_EVICTED;
b128c09f
BB
6221 }
6222
ca0bf58d
PS
6223 /* Ensure this header has finished being written */
6224 ASSERT(!HDR_L2_WRITING(hdr));
6225 ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL);
d962d5da
PS
6226
6227 arc_hdr_l2hdr_destroy(hdr);
34dc7c2f
BB
6228 }
6229 mutex_exit(hash_lock);
6230 }
b9541d6b 6231 mutex_exit(&dev->l2ad_mtx);
34dc7c2f
BB
6232}
6233
6234/*
6235 * Find and write ARC buffers to the L2ARC device.
6236 *
2a432414 6237 * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
34dc7c2f 6238 * for reading until they have completed writing.
3a17a7a9
SK
6239 * The headroom_boost is an in-out parameter used to maintain headroom boost
6240 * state between calls to this function.
6241 *
6242 * Returns the number of bytes actually written (which may be smaller than
6243 * the delta by which the device hand has changed due to alignment).
34dc7c2f 6244 */
d164b209 6245static uint64_t
3a17a7a9
SK
6246l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz,
6247 boolean_t *headroom_boost)
34dc7c2f 6248{
2a432414 6249 arc_buf_hdr_t *hdr, *hdr_prev, *head;
ef56b078
AG
6250 uint64_t write_asize, write_sz, headroom, buf_compress_minsz,
6251 stats_size;
34dc7c2f 6252 void *buf_data;
3a17a7a9 6253 boolean_t full;
34dc7c2f
BB
6254 l2arc_write_callback_t *cb;
6255 zio_t *pio, *wzio;
3541dc6d 6256 uint64_t guid = spa_load_guid(spa);
d6320ddb 6257 int try;
3a17a7a9 6258 const boolean_t do_headroom_boost = *headroom_boost;
34dc7c2f 6259
34dc7c2f
BB
6260 ASSERT(dev->l2ad_vdev != NULL);
6261
3a17a7a9
SK
6262 /* Lower the flag now, we might want to raise it again later. */
6263 *headroom_boost = B_FALSE;
6264
34dc7c2f 6265 pio = NULL;
ef56b078 6266 write_sz = write_asize = 0;
34dc7c2f 6267 full = B_FALSE;
b9541d6b 6268 head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE);
2a432414 6269 head->b_flags |= ARC_FLAG_L2_WRITE_HEAD;
b9541d6b 6270 head->b_flags |= ARC_FLAG_HAS_L2HDR;
34dc7c2f 6271
3a17a7a9
SK
6272 /*
6273 * We will want to try to compress buffers that are at least 2x the
6274 * device sector size.
6275 */
6276 buf_compress_minsz = 2 << dev->l2ad_vdev->vdev_ashift;
6277
34dc7c2f
BB
6278 /*
6279 * Copy buffers for L2ARC writing.
6280 */
d6320ddb 6281 for (try = 0; try <= 3; try++) {
ca0bf58d 6282 multilist_sublist_t *mls = l2arc_sublist_lock(try);
3a17a7a9
SK
6283 uint64_t passed_sz = 0;
6284
b128c09f
BB
6285 /*
6286 * L2ARC fast warmup.
6287 *
6288 * Until the ARC is warm and starts to evict, read from the
6289 * head of the ARC lists rather than the tail.
6290 */
b128c09f 6291 if (arc_warm == B_FALSE)
ca0bf58d 6292 hdr = multilist_sublist_head(mls);
b128c09f 6293 else
ca0bf58d 6294 hdr = multilist_sublist_tail(mls);
b128c09f 6295
3a17a7a9
SK
6296 headroom = target_sz * l2arc_headroom;
6297 if (do_headroom_boost)
6298 headroom = (headroom * l2arc_headroom_boost) / 100;
6299
2a432414 6300 for (; hdr; hdr = hdr_prev) {
3a17a7a9
SK
6301 kmutex_t *hash_lock;
6302 uint64_t buf_sz;
ef56b078 6303 uint64_t buf_a_sz;
3a17a7a9 6304
b128c09f 6305 if (arc_warm == B_FALSE)
ca0bf58d 6306 hdr_prev = multilist_sublist_next(mls, hdr);
b128c09f 6307 else
ca0bf58d 6308 hdr_prev = multilist_sublist_prev(mls, hdr);
34dc7c2f 6309
2a432414 6310 hash_lock = HDR_LOCK(hdr);
3a17a7a9 6311 if (!mutex_tryenter(hash_lock)) {
34dc7c2f
BB
6312 /*
6313 * Skip this buffer rather than waiting.
6314 */
6315 continue;
6316 }
6317
2a432414 6318 passed_sz += hdr->b_size;
34dc7c2f
BB
6319 if (passed_sz > headroom) {
6320 /*
6321 * Searched too far.
6322 */
6323 mutex_exit(hash_lock);
6324 break;
6325 }
6326
2a432414 6327 if (!l2arc_write_eligible(guid, hdr)) {
34dc7c2f
BB
6328 mutex_exit(hash_lock);
6329 continue;
6330 }
6331
ef56b078
AG
6332 /*
6333 * Assume that the buffer is not going to be compressed
6334 * and could take more space on disk because of a larger
6335 * disk block size.
6336 */
6337 buf_sz = hdr->b_size;
6338 buf_a_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
6339
6340 if ((write_asize + buf_a_sz) > target_sz) {
34dc7c2f
BB
6341 full = B_TRUE;
6342 mutex_exit(hash_lock);
6343 break;
6344 }
6345
34dc7c2f
BB
6346 if (pio == NULL) {
6347 /*
6348 * Insert a dummy header on the buflist so
6349 * l2arc_write_done() can find where the
6350 * write buffers begin without searching.
6351 */
ca0bf58d 6352 mutex_enter(&dev->l2ad_mtx);
b9541d6b 6353 list_insert_head(&dev->l2ad_buflist, head);
ca0bf58d 6354 mutex_exit(&dev->l2ad_mtx);
34dc7c2f 6355
96c080cb
BB
6356 cb = kmem_alloc(
6357 sizeof (l2arc_write_callback_t), KM_SLEEP);
34dc7c2f
BB
6358 cb->l2wcb_dev = dev;
6359 cb->l2wcb_head = head;
6360 pio = zio_root(spa, l2arc_write_done, cb,
6361 ZIO_FLAG_CANFAIL);
6362 }
6363
6364 /*
6365 * Create and add a new L2ARC header.
6366 */
b9541d6b 6367 hdr->b_l2hdr.b_dev = dev;
2a432414 6368 hdr->b_flags |= ARC_FLAG_L2_WRITING;
3a17a7a9
SK
6369 /*
6370 * Temporarily stash the data buffer in b_tmp_cdata.
6371 * The subsequent write step will pick it up from
b9541d6b 6372 * there. This is because can't access b_l1hdr.b_buf
3a17a7a9
SK
6373 * without holding the hash_lock, which we in turn
6374 * can't access without holding the ARC list locks
6375 * (which we want to avoid during compression/writing)
6376 */
4e0f33ff 6377 hdr->b_l2hdr.b_compress = ZIO_COMPRESS_OFF;
b9541d6b
CW
6378 hdr->b_l2hdr.b_asize = hdr->b_size;
6379 hdr->b_l2hdr.b_hits = 0;
6380 hdr->b_l1hdr.b_tmp_cdata = hdr->b_l1hdr.b_buf->b_data;
3a17a7a9 6381
d962d5da
PS
6382 /*
6383 * Explicitly set the b_daddr field to a known
6384 * value which means "invalid address". This
6385 * enables us to differentiate which stage of
6386 * l2arc_write_buffers() the particular header
6387 * is in (e.g. this loop, or the one below).
6388 * ARC_FLAG_L2_WRITING is not enough to make
6389 * this distinction, and we need to know in
6390 * order to do proper l2arc vdev accounting in
6391 * arc_release() and arc_hdr_destroy().
6392 *
6393 * Note, we can't use a new flag to distinguish
6394 * the two stages because we don't hold the
6395 * header's hash_lock below, in the second stage
6396 * of this function. Thus, we can't simply
6397 * change the b_flags field to denote that the
6398 * IO has been sent. We can change the b_daddr
6399 * field of the L2 portion, though, since we'll
6400 * be holding the l2ad_mtx; which is why we're
6401 * using it to denote the header's state change.
6402 */
6403 hdr->b_l2hdr.b_daddr = L2ARC_ADDR_UNSET;
b9541d6b 6404 hdr->b_flags |= ARC_FLAG_HAS_L2HDR;
3a17a7a9 6405
ca0bf58d 6406 mutex_enter(&dev->l2ad_mtx);
b9541d6b 6407 list_insert_head(&dev->l2ad_buflist, hdr);
ca0bf58d 6408 mutex_exit(&dev->l2ad_mtx);
34dc7c2f
BB
6409
6410 /*
6411 * Compute and store the buffer cksum before
6412 * writing. On debug the cksum is verified first.
6413 */
b9541d6b
CW
6414 arc_cksum_verify(hdr->b_l1hdr.b_buf);
6415 arc_cksum_compute(hdr->b_l1hdr.b_buf, B_TRUE);
34dc7c2f
BB
6416
6417 mutex_exit(hash_lock);
6418
3a17a7a9 6419 write_sz += buf_sz;
ef56b078 6420 write_asize += buf_a_sz;
3a17a7a9
SK
6421 }
6422
ca0bf58d 6423 multilist_sublist_unlock(mls);
3a17a7a9
SK
6424
6425 if (full == B_TRUE)
6426 break;
6427 }
6428
6429 /* No buffers selected for writing? */
6430 if (pio == NULL) {
6431 ASSERT0(write_sz);
b9541d6b
CW
6432 ASSERT(!HDR_HAS_L1HDR(head));
6433 kmem_cache_free(hdr_l2only_cache, head);
3a17a7a9
SK
6434 return (0);
6435 }
6436
ca0bf58d
PS
6437 mutex_enter(&dev->l2ad_mtx);
6438
ef56b078
AG
6439 /*
6440 * Note that elsewhere in this file arcstat_l2_asize
6441 * and the used space on l2ad_vdev are updated using b_asize,
6442 * which is not necessarily rounded up to the device block size.
6443 * Too keep accounting consistent we do the same here as well:
6444 * stats_size accumulates the sum of b_asize of the written buffers,
6445 * while write_asize accumulates the sum of b_asize rounded up
6446 * to the device block size.
6447 * The latter sum is used only to validate the corectness of the code.
6448 */
6449 stats_size = 0;
6450 write_asize = 0;
6451
3a17a7a9
SK
6452 /*
6453 * Now start writing the buffers. We're starting at the write head
6454 * and work backwards, retracing the course of the buffer selector
6455 * loop above.
6456 */
b9541d6b
CW
6457 for (hdr = list_prev(&dev->l2ad_buflist, head); hdr;
6458 hdr = list_prev(&dev->l2ad_buflist, hdr)) {
3a17a7a9
SK
6459 uint64_t buf_sz;
6460
ca0bf58d
PS
6461 /*
6462 * We rely on the L1 portion of the header below, so
6463 * it's invalid for this header to have been evicted out
6464 * of the ghost cache, prior to being written out. The
6465 * ARC_FLAG_L2_WRITING bit ensures this won't happen.
6466 */
6467 ASSERT(HDR_HAS_L1HDR(hdr));
6468
3a17a7a9
SK
6469 /*
6470 * We shouldn't need to lock the buffer here, since we flagged
2a432414
GW
6471 * it as ARC_FLAG_L2_WRITING in the previous step, but we must
6472 * take care to only access its L2 cache parameters. In
b9541d6b 6473 * particular, hdr->l1hdr.b_buf may be invalid by now due to
2a432414 6474 * ARC eviction.
3a17a7a9 6475 */
b9541d6b 6476 hdr->b_l2hdr.b_daddr = dev->l2ad_hand;
3a17a7a9 6477
b9541d6b
CW
6478 if ((!l2arc_nocompress && HDR_L2COMPRESS(hdr)) &&
6479 hdr->b_l2hdr.b_asize >= buf_compress_minsz) {
6480 if (l2arc_compress_buf(hdr)) {
3a17a7a9
SK
6481 /*
6482 * If compression succeeded, enable headroom
6483 * boost on the next scan cycle.
6484 */
6485 *headroom_boost = B_TRUE;
6486 }
6487 }
6488
6489 /*
6490 * Pick up the buffer data we had previously stashed away
6491 * (and now potentially also compressed).
6492 */
b9541d6b
CW
6493 buf_data = hdr->b_l1hdr.b_tmp_cdata;
6494 buf_sz = hdr->b_l2hdr.b_asize;
3a17a7a9 6495
d962d5da
PS
6496 /*
6497 * We need to do this regardless if buf_sz is zero or
6498 * not, otherwise, when this l2hdr is evicted we'll
6499 * remove a reference that was never added.
6500 */
6501 (void) refcount_add_many(&dev->l2ad_alloc, buf_sz, hdr);
6502
3a17a7a9
SK
6503 /* Compression may have squashed the buffer to zero length. */
6504 if (buf_sz != 0) {
ef56b078 6505 uint64_t buf_a_sz;
3a17a7a9 6506
34dc7c2f
BB
6507 wzio = zio_write_phys(pio, dev->l2ad_vdev,
6508 dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
6509 NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
6510 ZIO_FLAG_CANFAIL, B_FALSE);
6511
6512 DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
6513 zio_t *, wzio);
6514 (void) zio_nowait(wzio);
6515
ef56b078 6516 stats_size += buf_sz;
d962d5da 6517
b128c09f
BB
6518 /*
6519 * Keep the clock hand suitably device-aligned.
6520 */
ef56b078
AG
6521 buf_a_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
6522 write_asize += buf_a_sz;
6523 dev->l2ad_hand += buf_a_sz;
34dc7c2f 6524 }
34dc7c2f 6525 }
34dc7c2f 6526
b9541d6b 6527 mutex_exit(&dev->l2ad_mtx);
34dc7c2f 6528
3a17a7a9 6529 ASSERT3U(write_asize, <=, target_sz);
34dc7c2f 6530 ARCSTAT_BUMP(arcstat_l2_writes_sent);
3a17a7a9 6531 ARCSTAT_INCR(arcstat_l2_write_bytes, write_asize);
34dc7c2f 6532 ARCSTAT_INCR(arcstat_l2_size, write_sz);
ef56b078
AG
6533 ARCSTAT_INCR(arcstat_l2_asize, stats_size);
6534 vdev_space_update(dev->l2ad_vdev, stats_size, 0, 0);
34dc7c2f
BB
6535
6536 /*
6537 * Bump device hand to the device start if it is approaching the end.
6538 * l2arc_evict() will already have evicted ahead for this case.
6539 */
b128c09f 6540 if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
34dc7c2f 6541 dev->l2ad_hand = dev->l2ad_start;
34dc7c2f
BB
6542 dev->l2ad_first = B_FALSE;
6543 }
6544
d164b209 6545 dev->l2ad_writing = B_TRUE;
34dc7c2f 6546 (void) zio_wait(pio);
d164b209
BB
6547 dev->l2ad_writing = B_FALSE;
6548
3a17a7a9
SK
6549 return (write_asize);
6550}
6551
6552/*
6553 * Compresses an L2ARC buffer.
b9541d6b 6554 * The data to be compressed must be prefilled in l1hdr.b_tmp_cdata and its
3a17a7a9
SK
6555 * size in l2hdr->b_asize. This routine tries to compress the data and
6556 * depending on the compression result there are three possible outcomes:
6557 * *) The buffer was incompressible. The original l2hdr contents were left
6558 * untouched and are ready for writing to an L2 device.
6559 * *) The buffer was all-zeros, so there is no need to write it to an L2
6560 * device. To indicate this situation b_tmp_cdata is NULL'ed, b_asize is
6561 * set to zero and b_compress is set to ZIO_COMPRESS_EMPTY.
6562 * *) Compression succeeded and b_tmp_cdata was replaced with a temporary
6563 * data buffer which holds the compressed data to be written, and b_asize
6564 * tells us how much data there is. b_compress is set to the appropriate
6565 * compression algorithm. Once writing is done, invoke
6566 * l2arc_release_cdata_buf on this l2hdr to free this temporary buffer.
6567 *
6568 * Returns B_TRUE if compression succeeded, or B_FALSE if it didn't (the
6569 * buffer was incompressible).
6570 */
6571static boolean_t
b9541d6b 6572l2arc_compress_buf(arc_buf_hdr_t *hdr)
3a17a7a9
SK
6573{
6574 void *cdata;
9b67f605 6575 size_t csize, len, rounded;
b9541d6b 6576 l2arc_buf_hdr_t *l2hdr;
3a17a7a9 6577
b9541d6b
CW
6578 ASSERT(HDR_HAS_L2HDR(hdr));
6579
6580 l2hdr = &hdr->b_l2hdr;
6581
6582 ASSERT(HDR_HAS_L1HDR(hdr));
4e0f33ff 6583 ASSERT3U(l2hdr->b_compress, ==, ZIO_COMPRESS_OFF);
b9541d6b 6584 ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL);
3a17a7a9
SK
6585
6586 len = l2hdr->b_asize;
6587 cdata = zio_data_buf_alloc(len);
b9541d6b
CW
6588 ASSERT3P(cdata, !=, NULL);
6589 csize = zio_compress_data(ZIO_COMPRESS_LZ4, hdr->b_l1hdr.b_tmp_cdata,
3a17a7a9
SK
6590 cdata, l2hdr->b_asize);
6591
9b67f605
MA
6592 rounded = P2ROUNDUP(csize, (size_t)SPA_MINBLOCKSIZE);
6593 if (rounded > csize) {
6594 bzero((char *)cdata + csize, rounded - csize);
6595 csize = rounded;
6596 }
6597
3a17a7a9
SK
6598 if (csize == 0) {
6599 /* zero block, indicate that there's nothing to write */
6600 zio_data_buf_free(cdata, len);
4e0f33ff 6601 l2hdr->b_compress = ZIO_COMPRESS_EMPTY;
3a17a7a9 6602 l2hdr->b_asize = 0;
b9541d6b 6603 hdr->b_l1hdr.b_tmp_cdata = NULL;
3a17a7a9
SK
6604 ARCSTAT_BUMP(arcstat_l2_compress_zeros);
6605 return (B_TRUE);
6606 } else if (csize > 0 && csize < len) {
6607 /*
6608 * Compression succeeded, we'll keep the cdata around for
6609 * writing and release it afterwards.
6610 */
4e0f33ff 6611 l2hdr->b_compress = ZIO_COMPRESS_LZ4;
3a17a7a9 6612 l2hdr->b_asize = csize;
b9541d6b 6613 hdr->b_l1hdr.b_tmp_cdata = cdata;
3a17a7a9
SK
6614 ARCSTAT_BUMP(arcstat_l2_compress_successes);
6615 return (B_TRUE);
6616 } else {
6617 /*
6618 * Compression failed, release the compressed buffer.
6619 * l2hdr will be left unmodified.
6620 */
6621 zio_data_buf_free(cdata, len);
6622 ARCSTAT_BUMP(arcstat_l2_compress_failures);
6623 return (B_FALSE);
6624 }
6625}
6626
6627/*
6628 * Decompresses a zio read back from an l2arc device. On success, the
6629 * underlying zio's io_data buffer is overwritten by the uncompressed
6630 * version. On decompression error (corrupt compressed stream), the
6631 * zio->io_error value is set to signal an I/O error.
6632 *
6633 * Please note that the compressed data stream is not checksummed, so
6634 * if the underlying device is experiencing data corruption, we may feed
6635 * corrupt data to the decompressor, so the decompressor needs to be
6636 * able to handle this situation (LZ4 does).
6637 */
6638static void
6639l2arc_decompress_zio(zio_t *zio, arc_buf_hdr_t *hdr, enum zio_compress c)
6640{
6641 uint64_t csize;
6642 void *cdata;
6643
6644 ASSERT(L2ARC_IS_VALID_COMPRESS(c));
6645
6646 if (zio->io_error != 0) {
6647 /*
6648 * An io error has occured, just restore the original io
6649 * size in preparation for a main pool read.
6650 */
6651 zio->io_orig_size = zio->io_size = hdr->b_size;
6652 return;
6653 }
6654
6655 if (c == ZIO_COMPRESS_EMPTY) {
6656 /*
6657 * An empty buffer results in a null zio, which means we
6658 * need to fill its io_data after we're done restoring the
6659 * buffer's contents.
6660 */
b9541d6b
CW
6661 ASSERT(hdr->b_l1hdr.b_buf != NULL);
6662 bzero(hdr->b_l1hdr.b_buf->b_data, hdr->b_size);
6663 zio->io_data = zio->io_orig_data = hdr->b_l1hdr.b_buf->b_data;
3a17a7a9
SK
6664 } else {
6665 ASSERT(zio->io_data != NULL);
6666 /*
6667 * We copy the compressed data from the start of the arc buffer
6668 * (the zio_read will have pulled in only what we need, the
6669 * rest is garbage which we will overwrite at decompression)
6670 * and then decompress back to the ARC data buffer. This way we
6671 * can minimize copying by simply decompressing back over the
6672 * original compressed data (rather than decompressing to an
6673 * aux buffer and then copying back the uncompressed buffer,
6674 * which is likely to be much larger).
6675 */
6676 csize = zio->io_size;
6677 cdata = zio_data_buf_alloc(csize);
6678 bcopy(zio->io_data, cdata, csize);
6679 if (zio_decompress_data(c, cdata, zio->io_data, csize,
6680 hdr->b_size) != 0)
96c080cb 6681 zio->io_error = EIO;
3a17a7a9
SK
6682 zio_data_buf_free(cdata, csize);
6683 }
6684
6685 /* Restore the expected uncompressed IO size. */
6686 zio->io_orig_size = zio->io_size = hdr->b_size;
6687}
6688
6689/*
6690 * Releases the temporary b_tmp_cdata buffer in an l2arc header structure.
6691 * This buffer serves as a temporary holder of compressed data while
6692 * the buffer entry is being written to an l2arc device. Once that is
6693 * done, we can dispose of it.
6694 */
6695static void
2a432414 6696l2arc_release_cdata_buf(arc_buf_hdr_t *hdr)
3a17a7a9 6697{
4e0f33ff 6698 enum zio_compress comp;
ca0bf58d 6699
b9541d6b 6700 ASSERT(HDR_HAS_L1HDR(hdr));
4e0f33ff
AJ
6701 ASSERT(HDR_HAS_L2HDR(hdr));
6702 comp = hdr->b_l2hdr.b_compress;
ca0bf58d
PS
6703 ASSERT(comp == ZIO_COMPRESS_OFF || L2ARC_IS_VALID_COMPRESS(comp));
6704
6705 if (comp == ZIO_COMPRESS_OFF) {
6706 /*
6707 * In this case, b_tmp_cdata points to the same buffer
6708 * as the arc_buf_t's b_data field. We don't want to
6709 * free it, since the arc_buf_t will handle that.
6710 */
6711 hdr->b_l1hdr.b_tmp_cdata = NULL;
6712 } else if (comp == ZIO_COMPRESS_EMPTY) {
6713 /*
6714 * In this case, b_tmp_cdata was compressed to an empty
6715 * buffer, thus there's nothing to free and b_tmp_cdata
6716 * should have been set to NULL in l2arc_write_buffers().
6717 */
6718 ASSERT3P(hdr->b_l1hdr.b_tmp_cdata, ==, NULL);
6719 } else {
3a17a7a9
SK
6720 /*
6721 * If the data was compressed, then we've allocated a
6722 * temporary buffer for it, so now we need to release it.
6723 */
b9541d6b
CW
6724 ASSERT(hdr->b_l1hdr.b_tmp_cdata != NULL);
6725 zio_data_buf_free(hdr->b_l1hdr.b_tmp_cdata,
6726 hdr->b_size);
ca0bf58d 6727 hdr->b_l1hdr.b_tmp_cdata = NULL;
3a17a7a9 6728 }
ca0bf58d 6729
34dc7c2f
BB
6730}
6731
6732/*
6733 * This thread feeds the L2ARC at regular intervals. This is the beating
6734 * heart of the L2ARC.
6735 */
6736static void
6737l2arc_feed_thread(void)
6738{
6739 callb_cpr_t cpr;
6740 l2arc_dev_t *dev;
6741 spa_t *spa;
d164b209 6742 uint64_t size, wrote;
428870ff 6743 clock_t begin, next = ddi_get_lbolt();
3a17a7a9 6744 boolean_t headroom_boost = B_FALSE;
40d06e3c 6745 fstrans_cookie_t cookie;
34dc7c2f
BB
6746
6747 CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
6748
6749 mutex_enter(&l2arc_feed_thr_lock);
6750
40d06e3c 6751 cookie = spl_fstrans_mark();
34dc7c2f 6752 while (l2arc_thread_exit == 0) {
34dc7c2f 6753 CALLB_CPR_SAFE_BEGIN(&cpr);
b64ccd6c 6754 (void) cv_timedwait_sig(&l2arc_feed_thr_cv,
5b63b3eb 6755 &l2arc_feed_thr_lock, next);
34dc7c2f 6756 CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
428870ff 6757 next = ddi_get_lbolt() + hz;
34dc7c2f
BB
6758
6759 /*
b128c09f 6760 * Quick check for L2ARC devices.
34dc7c2f
BB
6761 */
6762 mutex_enter(&l2arc_dev_mtx);
6763 if (l2arc_ndev == 0) {
6764 mutex_exit(&l2arc_dev_mtx);
6765 continue;
6766 }
b128c09f 6767 mutex_exit(&l2arc_dev_mtx);
428870ff 6768 begin = ddi_get_lbolt();
34dc7c2f
BB
6769
6770 /*
b128c09f
BB
6771 * This selects the next l2arc device to write to, and in
6772 * doing so the next spa to feed from: dev->l2ad_spa. This
6773 * will return NULL if there are now no l2arc devices or if
6774 * they are all faulted.
6775 *
6776 * If a device is returned, its spa's config lock is also
6777 * held to prevent device removal. l2arc_dev_get_next()
6778 * will grab and release l2arc_dev_mtx.
34dc7c2f 6779 */
b128c09f 6780 if ((dev = l2arc_dev_get_next()) == NULL)
34dc7c2f 6781 continue;
b128c09f
BB
6782
6783 spa = dev->l2ad_spa;
6784 ASSERT(spa != NULL);
34dc7c2f 6785
572e2857
BB
6786 /*
6787 * If the pool is read-only then force the feed thread to
6788 * sleep a little longer.
6789 */
6790 if (!spa_writeable(spa)) {
6791 next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
6792 spa_config_exit(spa, SCL_L2ARC, dev);
6793 continue;
6794 }
6795
34dc7c2f 6796 /*
b128c09f 6797 * Avoid contributing to memory pressure.
34dc7c2f 6798 */
ca67b33a 6799 if (arc_reclaim_needed()) {
b128c09f
BB
6800 ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
6801 spa_config_exit(spa, SCL_L2ARC, dev);
34dc7c2f
BB
6802 continue;
6803 }
b128c09f 6804
34dc7c2f
BB
6805 ARCSTAT_BUMP(arcstat_l2_feeds);
6806
3a17a7a9 6807 size = l2arc_write_size();
b128c09f 6808
34dc7c2f
BB
6809 /*
6810 * Evict L2ARC buffers that will be overwritten.
6811 */
b128c09f 6812 l2arc_evict(dev, size, B_FALSE);
34dc7c2f
BB
6813
6814 /*
6815 * Write ARC buffers.
6816 */
3a17a7a9 6817 wrote = l2arc_write_buffers(spa, dev, size, &headroom_boost);
d164b209
BB
6818
6819 /*
6820 * Calculate interval between writes.
6821 */
6822 next = l2arc_write_interval(begin, size, wrote);
b128c09f 6823 spa_config_exit(spa, SCL_L2ARC, dev);
34dc7c2f 6824 }
40d06e3c 6825 spl_fstrans_unmark(cookie);
34dc7c2f
BB
6826
6827 l2arc_thread_exit = 0;
6828 cv_broadcast(&l2arc_feed_thr_cv);
6829 CALLB_CPR_EXIT(&cpr); /* drops l2arc_feed_thr_lock */
6830 thread_exit();
6831}
6832
b128c09f
BB
6833boolean_t
6834l2arc_vdev_present(vdev_t *vd)
6835{
6836 l2arc_dev_t *dev;
6837
6838 mutex_enter(&l2arc_dev_mtx);
6839 for (dev = list_head(l2arc_dev_list); dev != NULL;
6840 dev = list_next(l2arc_dev_list, dev)) {
6841 if (dev->l2ad_vdev == vd)
6842 break;
6843 }
6844 mutex_exit(&l2arc_dev_mtx);
6845
6846 return (dev != NULL);
6847}
6848
34dc7c2f
BB
6849/*
6850 * Add a vdev for use by the L2ARC. By this point the spa has already
6851 * validated the vdev and opened it.
6852 */
6853void
9babb374 6854l2arc_add_vdev(spa_t *spa, vdev_t *vd)
34dc7c2f
BB
6855{
6856 l2arc_dev_t *adddev;
6857
b128c09f
BB
6858 ASSERT(!l2arc_vdev_present(vd));
6859
34dc7c2f
BB
6860 /*
6861 * Create a new l2arc device entry.
6862 */
6863 adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
6864 adddev->l2ad_spa = spa;
6865 adddev->l2ad_vdev = vd;
9babb374
BB
6866 adddev->l2ad_start = VDEV_LABEL_START_SIZE;
6867 adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
34dc7c2f 6868 adddev->l2ad_hand = adddev->l2ad_start;
34dc7c2f 6869 adddev->l2ad_first = B_TRUE;
d164b209 6870 adddev->l2ad_writing = B_FALSE;
98f72a53 6871 list_link_init(&adddev->l2ad_node);
34dc7c2f 6872
b9541d6b 6873 mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f
BB
6874 /*
6875 * This is a list of all ARC buffers that are still valid on the
6876 * device.
6877 */
b9541d6b
CW
6878 list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
6879 offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node));
34dc7c2f 6880
428870ff 6881 vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
d962d5da 6882 refcount_create(&adddev->l2ad_alloc);
34dc7c2f
BB
6883
6884 /*
6885 * Add device to global list
6886 */
6887 mutex_enter(&l2arc_dev_mtx);
6888 list_insert_head(l2arc_dev_list, adddev);
6889 atomic_inc_64(&l2arc_ndev);
6890 mutex_exit(&l2arc_dev_mtx);
6891}
6892
6893/*
6894 * Remove a vdev from the L2ARC.
6895 */
6896void
6897l2arc_remove_vdev(vdev_t *vd)
6898{
6899 l2arc_dev_t *dev, *nextdev, *remdev = NULL;
6900
34dc7c2f
BB
6901 /*
6902 * Find the device by vdev
6903 */
6904 mutex_enter(&l2arc_dev_mtx);
6905 for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
6906 nextdev = list_next(l2arc_dev_list, dev);
6907 if (vd == dev->l2ad_vdev) {
6908 remdev = dev;
6909 break;
6910 }
6911 }
6912 ASSERT(remdev != NULL);
6913
6914 /*
6915 * Remove device from global list
6916 */
6917 list_remove(l2arc_dev_list, remdev);
6918 l2arc_dev_last = NULL; /* may have been invalidated */
b128c09f
BB
6919 atomic_dec_64(&l2arc_ndev);
6920 mutex_exit(&l2arc_dev_mtx);
34dc7c2f
BB
6921
6922 /*
6923 * Clear all buflists and ARC references. L2ARC device flush.
6924 */
6925 l2arc_evict(remdev, 0, B_TRUE);
b9541d6b
CW
6926 list_destroy(&remdev->l2ad_buflist);
6927 mutex_destroy(&remdev->l2ad_mtx);
d962d5da 6928 refcount_destroy(&remdev->l2ad_alloc);
34dc7c2f 6929 kmem_free(remdev, sizeof (l2arc_dev_t));
34dc7c2f
BB
6930}
6931
6932void
b128c09f 6933l2arc_init(void)
34dc7c2f
BB
6934{
6935 l2arc_thread_exit = 0;
6936 l2arc_ndev = 0;
6937 l2arc_writes_sent = 0;
6938 l2arc_writes_done = 0;
6939
6940 mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
6941 cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
6942 mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
34dc7c2f
BB
6943 mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
6944
6945 l2arc_dev_list = &L2ARC_dev_list;
6946 l2arc_free_on_write = &L2ARC_free_on_write;
6947 list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
6948 offsetof(l2arc_dev_t, l2ad_node));
6949 list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
6950 offsetof(l2arc_data_free_t, l2df_list_node));
34dc7c2f
BB
6951}
6952
6953void
b128c09f 6954l2arc_fini(void)
34dc7c2f 6955{
b128c09f
BB
6956 /*
6957 * This is called from dmu_fini(), which is called from spa_fini();
6958 * Because of this, we can assume that all l2arc devices have
6959 * already been removed when the pools themselves were removed.
6960 */
6961
6962 l2arc_do_free_on_write();
34dc7c2f
BB
6963
6964 mutex_destroy(&l2arc_feed_thr_lock);
6965 cv_destroy(&l2arc_feed_thr_cv);
6966 mutex_destroy(&l2arc_dev_mtx);
34dc7c2f
BB
6967 mutex_destroy(&l2arc_free_on_write_mtx);
6968
6969 list_destroy(l2arc_dev_list);
6970 list_destroy(l2arc_free_on_write);
6971}
b128c09f
BB
6972
6973void
6974l2arc_start(void)
6975{
fb5f0bc8 6976 if (!(spa_mode_global & FWRITE))
b128c09f
BB
6977 return;
6978
6979 (void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
1229323d 6980 TS_RUN, defclsyspri);
b128c09f
BB
6981}
6982
6983void
6984l2arc_stop(void)
6985{
fb5f0bc8 6986 if (!(spa_mode_global & FWRITE))
b128c09f
BB
6987 return;
6988
6989 mutex_enter(&l2arc_feed_thr_lock);
6990 cv_signal(&l2arc_feed_thr_cv); /* kick thread out of startup */
6991 l2arc_thread_exit = 1;
6992 while (l2arc_thread_exit != 0)
6993 cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
6994 mutex_exit(&l2arc_feed_thr_lock);
6995}
c28b2279
BB
6996
6997#if defined(_KERNEL) && defined(HAVE_SPL)
0f699108
AZ
6998EXPORT_SYMBOL(arc_buf_size);
6999EXPORT_SYMBOL(arc_write);
c28b2279
BB
7000EXPORT_SYMBOL(arc_read);
7001EXPORT_SYMBOL(arc_buf_remove_ref);
e0b0ca98 7002EXPORT_SYMBOL(arc_buf_info);
c28b2279 7003EXPORT_SYMBOL(arc_getbuf_func);
ab26409d
BB
7004EXPORT_SYMBOL(arc_add_prune_callback);
7005EXPORT_SYMBOL(arc_remove_prune_callback);
c28b2279 7006
bce45ec9 7007module_param(zfs_arc_min, ulong, 0644);
c409e464 7008MODULE_PARM_DESC(zfs_arc_min, "Min arc size");
c28b2279 7009
bce45ec9 7010module_param(zfs_arc_max, ulong, 0644);
c409e464 7011MODULE_PARM_DESC(zfs_arc_max, "Max arc size");
c28b2279 7012
bce45ec9 7013module_param(zfs_arc_meta_limit, ulong, 0644);
c28b2279 7014MODULE_PARM_DESC(zfs_arc_meta_limit, "Meta limit for arc size");
6a8f9b6b 7015
ca0bf58d
PS
7016module_param(zfs_arc_meta_min, ulong, 0644);
7017MODULE_PARM_DESC(zfs_arc_meta_min, "Min arc metadata");
7018
bce45ec9 7019module_param(zfs_arc_meta_prune, int, 0644);
2cbb06b5 7020MODULE_PARM_DESC(zfs_arc_meta_prune, "Meta objects to scan for prune");
c409e464 7021
ca67b33a 7022module_param(zfs_arc_meta_adjust_restarts, int, 0644);
bc888666
BB
7023MODULE_PARM_DESC(zfs_arc_meta_adjust_restarts,
7024 "Limit number of restarts in arc_adjust_meta");
7025
f6046738
BB
7026module_param(zfs_arc_meta_strategy, int, 0644);
7027MODULE_PARM_DESC(zfs_arc_meta_strategy, "Meta reclaim strategy");
7028
bce45ec9 7029module_param(zfs_arc_grow_retry, int, 0644);
c409e464
BB
7030MODULE_PARM_DESC(zfs_arc_grow_retry, "Seconds before growing arc size");
7031
89c8cac4
PS
7032module_param(zfs_arc_p_aggressive_disable, int, 0644);
7033MODULE_PARM_DESC(zfs_arc_p_aggressive_disable, "disable aggressive arc_p grow");
7034
62422785
PS
7035module_param(zfs_arc_p_dampener_disable, int, 0644);
7036MODULE_PARM_DESC(zfs_arc_p_dampener_disable, "disable arc_p adapt dampener");
7037
bce45ec9 7038module_param(zfs_arc_shrink_shift, int, 0644);
c409e464
BB
7039MODULE_PARM_DESC(zfs_arc_shrink_shift, "log2(fraction of arc to reclaim)");
7040
728d6ae9
BB
7041module_param(zfs_arc_p_min_shift, int, 0644);
7042MODULE_PARM_DESC(zfs_arc_p_min_shift, "arc_c shift to calc min/max arc_p");
7043
1f7c30df
BB
7044module_param(zfs_disable_dup_eviction, int, 0644);
7045MODULE_PARM_DESC(zfs_disable_dup_eviction, "disable duplicate buffer eviction");
7046
49ddb315
MA
7047module_param(zfs_arc_average_blocksize, int, 0444);
7048MODULE_PARM_DESC(zfs_arc_average_blocksize, "Target average block size");
7049
bce45ec9
BB
7050module_param(zfs_arc_min_prefetch_lifespan, int, 0644);
7051MODULE_PARM_DESC(zfs_arc_min_prefetch_lifespan, "Min life of prefetch block");
7052
ca0bf58d
PS
7053module_param(zfs_arc_num_sublists_per_state, int, 0644);
7054MODULE_PARM_DESC(zfs_arc_num_sublists_per_state,
7055 "Number of sublists used in each of the ARC state lists");
7056
bce45ec9 7057module_param(l2arc_write_max, ulong, 0644);
abd8610c
BB
7058MODULE_PARM_DESC(l2arc_write_max, "Max write bytes per interval");
7059
bce45ec9 7060module_param(l2arc_write_boost, ulong, 0644);
abd8610c
BB
7061MODULE_PARM_DESC(l2arc_write_boost, "Extra write bytes during device warmup");
7062
bce45ec9 7063module_param(l2arc_headroom, ulong, 0644);
abd8610c
BB
7064MODULE_PARM_DESC(l2arc_headroom, "Number of max device writes to precache");
7065
3a17a7a9
SK
7066module_param(l2arc_headroom_boost, ulong, 0644);
7067MODULE_PARM_DESC(l2arc_headroom_boost, "Compressed l2arc_headroom multiplier");
7068
bce45ec9 7069module_param(l2arc_feed_secs, ulong, 0644);
abd8610c
BB
7070MODULE_PARM_DESC(l2arc_feed_secs, "Seconds between L2ARC writing");
7071
bce45ec9 7072module_param(l2arc_feed_min_ms, ulong, 0644);
abd8610c
BB
7073MODULE_PARM_DESC(l2arc_feed_min_ms, "Min feed interval in milliseconds");
7074
bce45ec9 7075module_param(l2arc_noprefetch, int, 0644);
abd8610c
BB
7076MODULE_PARM_DESC(l2arc_noprefetch, "Skip caching prefetched buffers");
7077
3a17a7a9
SK
7078module_param(l2arc_nocompress, int, 0644);
7079MODULE_PARM_DESC(l2arc_nocompress, "Skip compressing L2ARC buffers");
7080
bce45ec9 7081module_param(l2arc_feed_again, int, 0644);
abd8610c
BB
7082MODULE_PARM_DESC(l2arc_feed_again, "Turbo L2ARC warmup");
7083
bce45ec9 7084module_param(l2arc_norw, int, 0644);
abd8610c
BB
7085MODULE_PARM_DESC(l2arc_norw, "No reads during writes");
7086
7e8bddd0
BB
7087module_param(zfs_arc_lotsfree_percent, int, 0644);
7088MODULE_PARM_DESC(zfs_arc_lotsfree_percent,
7089 "System free memory I/O throttle in bytes");
7090
11f552fa
BB
7091module_param(zfs_arc_sys_free, ulong, 0644);
7092MODULE_PARM_DESC(zfs_arc_sys_free, "System free memory target size in bytes");
7093
c28b2279 7094#endif