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