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