xref: /freebsd/sys/contrib/openzfs/module/zfs/arc.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2018, Joyent, Inc.
25  * Copyright (c) 2011, 2020, Delphix. All rights reserved.
26  * Copyright (c) 2014, Saso Kiselkov. All rights reserved.
27  * Copyright (c) 2017, Nexenta Systems, Inc.  All rights reserved.
28  * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
29  * Copyright (c) 2020, George Amanakis. All rights reserved.
30  * Copyright (c) 2019, 2024, Klara Inc.
31  * Copyright (c) 2019, Allan Jude
32  * Copyright (c) 2020, The FreeBSD Foundation [1]
33  * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
34  *
35  * [1] Portions of this software were developed by Allan Jude
36  *     under sponsorship from the FreeBSD Foundation.
37  */
38 
39 /*
40  * DVA-based Adjustable Replacement Cache
41  *
42  * While much of the theory of operation used here is
43  * based on the self-tuning, low overhead replacement cache
44  * presented by Megiddo and Modha at FAST 2003, there are some
45  * significant differences:
46  *
47  * 1. The Megiddo and Modha model assumes any page is evictable.
48  * Pages in its cache cannot be "locked" into memory.  This makes
49  * the eviction algorithm simple: evict the last page in the list.
50  * This also make the performance characteristics easy to reason
51  * about.  Our cache is not so simple.  At any given moment, some
52  * subset of the blocks in the cache are un-evictable because we
53  * have handed out a reference to them.  Blocks are only evictable
54  * when there are no external references active.  This makes
55  * eviction far more problematic:  we choose to evict the evictable
56  * blocks that are the "lowest" in the list.
57  *
58  * There are times when it is not possible to evict the requested
59  * space.  In these circumstances we are unable to adjust the cache
60  * size.  To prevent the cache growing unbounded at these times we
61  * implement a "cache throttle" that slows the flow of new data
62  * into the cache until we can make space available.
63  *
64  * 2. The Megiddo and Modha model assumes a fixed cache size.
65  * Pages are evicted when the cache is full and there is a cache
66  * miss.  Our model has a variable sized cache.  It grows with
67  * high use, but also tries to react to memory pressure from the
68  * operating system: decreasing its size when system memory is
69  * tight.
70  *
71  * 3. The Megiddo and Modha model assumes a fixed page size. All
72  * elements of the cache are therefore exactly the same size.  So
73  * when adjusting the cache size following a cache miss, its simply
74  * a matter of choosing a single page to evict.  In our model, we
75  * have variable sized cache blocks (ranging from 512 bytes to
76  * 128K bytes).  We therefore choose a set of blocks to evict to make
77  * space for a cache miss that approximates as closely as possible
78  * the space used by the new block.
79  *
80  * See also:  "ARC: A Self-Tuning, Low Overhead Replacement Cache"
81  * by N. Megiddo & D. Modha, FAST 2003
82  */
83 
84 /*
85  * The locking model:
86  *
87  * A new reference to a cache buffer can be obtained in two
88  * ways: 1) via a hash table lookup using the DVA as a key,
89  * or 2) via one of the ARC lists.  The arc_read() interface
90  * uses method 1, while the internal ARC algorithms for
91  * adjusting the cache use method 2.  We therefore provide two
92  * types of locks: 1) the hash table lock array, and 2) the
93  * ARC list locks.
94  *
95  * Buffers do not have their own mutexes, rather they rely on the
96  * hash table mutexes for the bulk of their protection (i.e. most
97  * fields in the arc_buf_hdr_t are protected by these mutexes).
98  *
99  * buf_hash_find() returns the appropriate mutex (held) when it
100  * locates the requested buffer in the hash table.  It returns
101  * NULL for the mutex if the buffer was not in the table.
102  *
103  * buf_hash_remove() expects the appropriate hash mutex to be
104  * already held before it is invoked.
105  *
106  * Each ARC state also has a mutex which is used to protect the
107  * buffer list associated with the state.  When attempting to
108  * obtain a hash table lock while holding an ARC list lock you
109  * must use: mutex_tryenter() to avoid deadlock.  Also note that
110  * the active state mutex must be held before the ghost state mutex.
111  *
112  * It as also possible to register a callback which is run when the
113  * metadata limit is reached and no buffers can be safely evicted.  In
114  * this case the arc user should drop a reference on some arc buffers so
115  * they can be reclaimed.  For example, when using the ZPL each dentry
116  * holds a references on a znode.  These dentries must be pruned before
117  * the arc buffer holding the znode can be safely evicted.
118  *
119  * Note that the majority of the performance stats are manipulated
120  * with atomic operations.
121  *
122  * The L2ARC uses the l2ad_mtx on each vdev for the following:
123  *
124  *	- L2ARC buflist creation
125  *	- L2ARC buflist eviction
126  *	- L2ARC write completion, which walks L2ARC buflists
127  *	- ARC header destruction, as it removes from L2ARC buflists
128  *	- ARC header release, as it removes from L2ARC buflists
129  */
130 
131 /*
132  * ARC operation:
133  *
134  * Every block that is in the ARC is tracked by an arc_buf_hdr_t structure.
135  * This structure can point either to a block that is still in the cache or to
136  * one that is only accessible in an L2 ARC device, or it can provide
137  * information about a block that was recently evicted. If a block is
138  * only accessible in the L2ARC, then the arc_buf_hdr_t only has enough
139  * information to retrieve it from the L2ARC device. This information is
140  * stored in the l2arc_buf_hdr_t sub-structure of the arc_buf_hdr_t. A block
141  * that is in this state cannot access the data directly.
142  *
143  * Blocks that are actively being referenced or have not been evicted
144  * are cached in the L1ARC. The L1ARC (l1arc_buf_hdr_t) is a structure within
145  * the arc_buf_hdr_t that will point to the data block in memory. A block can
146  * only be read by a consumer if it has an l1arc_buf_hdr_t. The L1ARC
147  * caches data in two ways -- in a list of ARC buffers (arc_buf_t) and
148  * also in the arc_buf_hdr_t's private physical data block pointer (b_pabd).
149  *
150  * The L1ARC's data pointer may or may not be uncompressed. The ARC has the
151  * ability to store the physical data (b_pabd) associated with the DVA of the
152  * arc_buf_hdr_t. Since the b_pabd is a copy of the on-disk physical block,
153  * it will match its on-disk compression characteristics. This behavior can be
154  * disabled by setting 'zfs_compressed_arc_enabled' to B_FALSE. When the
155  * compressed ARC functionality is disabled, the b_pabd will point to an
156  * uncompressed version of the on-disk data.
157  *
158  * Data in the L1ARC is not accessed by consumers of the ARC directly. Each
159  * arc_buf_hdr_t can have multiple ARC buffers (arc_buf_t) which reference it.
160  * Each ARC buffer (arc_buf_t) is being actively accessed by a specific ARC
161  * consumer. The ARC will provide references to this data and will keep it
162  * cached until it is no longer in use. The ARC caches only the L1ARC's physical
163  * data block and will evict any arc_buf_t that is no longer referenced. The
164  * amount of memory consumed by the arc_buf_ts' data buffers can be seen via the
165  * "overhead_size" kstat.
166  *
167  * Depending on the consumer, an arc_buf_t can be requested in uncompressed or
168  * compressed form. The typical case is that consumers will want uncompressed
169  * data, and when that happens a new data buffer is allocated where the data is
170  * decompressed for them to use. Currently the only consumer who wants
171  * compressed arc_buf_t's is "zfs send", when it streams data exactly as it
172  * exists on disk. When this happens, the arc_buf_t's data buffer is shared
173  * with the arc_buf_hdr_t.
174  *
175  * Here is a diagram showing an arc_buf_hdr_t referenced by two arc_buf_t's. The
176  * first one is owned by a compressed send consumer (and therefore references
177  * the same compressed data buffer as the arc_buf_hdr_t) and the second could be
178  * used by any other consumer (and has its own uncompressed copy of the data
179  * buffer).
180  *
181  *   arc_buf_hdr_t
182  *   +-----------+
183  *   | fields    |
184  *   | common to |
185  *   | L1- and   |
186  *   | L2ARC     |
187  *   +-----------+
188  *   | l2arc_buf_hdr_t
189  *   |           |
190  *   +-----------+
191  *   | l1arc_buf_hdr_t
192  *   |           |              arc_buf_t
193  *   | b_buf     +------------>+-----------+      arc_buf_t
194  *   | b_pabd    +-+           |b_next     +---->+-----------+
195  *   +-----------+ |           |-----------|     |b_next     +-->NULL
196  *                 |           |b_comp = T |     +-----------+
197  *                 |           |b_data     +-+   |b_comp = F |
198  *                 |           +-----------+ |   |b_data     +-+
199  *                 +->+------+               |   +-----------+ |
200  *        compressed  |      |               |                 |
201  *           data     |      |<--------------+                 | uncompressed
202  *                    +------+          compressed,            |     data
203  *                                        shared               +-->+------+
204  *                                         data                    |      |
205  *                                                                 |      |
206  *                                                                 +------+
207  *
208  * When a consumer reads a block, the ARC must first look to see if the
209  * arc_buf_hdr_t is cached. If the hdr is cached then the ARC allocates a new
210  * arc_buf_t and either copies uncompressed data into a new data buffer from an
211  * existing uncompressed arc_buf_t, decompresses the hdr's b_pabd buffer into a
212  * new data buffer, or shares the hdr's b_pabd buffer, depending on whether the
213  * hdr is compressed and the desired compression characteristics of the
214  * arc_buf_t consumer. If the arc_buf_t ends up sharing data with the
215  * arc_buf_hdr_t and both of them are uncompressed then the arc_buf_t must be
216  * the last buffer in the hdr's b_buf list, however a shared compressed buf can
217  * be anywhere in the hdr's list.
218  *
219  * The diagram below shows an example of an uncompressed ARC hdr that is
220  * sharing its data with an arc_buf_t (note that the shared uncompressed buf is
221  * the last element in the buf list):
222  *
223  *                arc_buf_hdr_t
224  *                +-----------+
225  *                |           |
226  *                |           |
227  *                |           |
228  *                +-----------+
229  * l2arc_buf_hdr_t|           |
230  *                |           |
231  *                +-----------+
232  * l1arc_buf_hdr_t|           |
233  *                |           |                 arc_buf_t    (shared)
234  *                |    b_buf  +------------>+---------+      arc_buf_t
235  *                |           |             |b_next   +---->+---------+
236  *                |  b_pabd   +-+           |---------|     |b_next   +-->NULL
237  *                +-----------+ |           |         |     +---------+
238  *                              |           |b_data   +-+   |         |
239  *                              |           +---------+ |   |b_data   +-+
240  *                              +->+------+             |   +---------+ |
241  *                                 |      |             |               |
242  *                   uncompressed  |      |             |               |
243  *                        data     +------+             |               |
244  *                                    ^                 +->+------+     |
245  *                                    |       uncompressed |      |     |
246  *                                    |           data     |      |     |
247  *                                    |                    +------+     |
248  *                                    +---------------------------------+
249  *
250  * Writing to the ARC requires that the ARC first discard the hdr's b_pabd
251  * since the physical block is about to be rewritten. The new data contents
252  * will be contained in the arc_buf_t. As the I/O pipeline performs the write,
253  * it may compress the data before writing it to disk. The ARC will be called
254  * with the transformed data and will memcpy the transformed on-disk block into
255  * a newly allocated b_pabd. Writes are always done into buffers which have
256  * either been loaned (and hence are new and don't have other readers) or
257  * buffers which have been released (and hence have their own hdr, if there
258  * were originally other readers of the buf's original hdr). This ensures that
259  * the ARC only needs to update a single buf and its hdr after a write occurs.
260  *
261  * When the L2ARC is in use, it will also take advantage of the b_pabd. The
262  * L2ARC will always write the contents of b_pabd to the L2ARC. This means
263  * that when compressed ARC is enabled that the L2ARC blocks are identical
264  * to the on-disk block in the main data pool. This provides a significant
265  * advantage since the ARC can leverage the bp's checksum when reading from the
266  * L2ARC to determine if the contents are valid. However, if the compressed
267  * ARC is disabled, then the L2ARC's block must be transformed to look
268  * like the physical block in the main data pool before comparing the
269  * checksum and determining its validity.
270  *
271  * The L1ARC has a slightly different system for storing encrypted data.
272  * Raw (encrypted + possibly compressed) data has a few subtle differences from
273  * data that is just compressed. The biggest difference is that it is not
274  * possible to decrypt encrypted data (or vice-versa) if the keys aren't loaded.
275  * The other difference is that encryption cannot be treated as a suggestion.
276  * If a caller would prefer compressed data, but they actually wind up with
277  * uncompressed data the worst thing that could happen is there might be a
278  * performance hit. If the caller requests encrypted data, however, we must be
279  * sure they actually get it or else secret information could be leaked. Raw
280  * data is stored in hdr->b_crypt_hdr.b_rabd. An encrypted header, therefore,
281  * may have both an encrypted version and a decrypted version of its data at
282  * once. When a caller needs a raw arc_buf_t, it is allocated and the data is
283  * copied out of this header. To avoid complications with b_pabd, raw buffers
284  * cannot be shared.
285  */
286 
287 #include <sys/spa.h>
288 #include <sys/zio.h>
289 #include <sys/spa_impl.h>
290 #include <sys/zio_compress.h>
291 #include <sys/zio_checksum.h>
292 #include <sys/zfs_context.h>
293 #include <sys/arc.h>
294 #include <sys/zfs_refcount.h>
295 #include <sys/vdev.h>
296 #include <sys/vdev_impl.h>
297 #include <sys/dsl_pool.h>
298 #include <sys/multilist.h>
299 #include <sys/abd.h>
300 #include <sys/zil.h>
301 #include <sys/fm/fs/zfs.h>
302 #include <sys/callb.h>
303 #include <sys/kstat.h>
304 #include <sys/zthr.h>
305 #include <zfs_fletcher.h>
306 #include <sys/arc_impl.h>
307 #include <sys/trace_zfs.h>
308 #include <sys/aggsum.h>
309 #include <sys/wmsum.h>
310 #include <cityhash.h>
311 #include <sys/vdev_trim.h>
312 #include <sys/zfs_racct.h>
313 #include <sys/zstd/zstd.h>
314 
315 #ifndef _KERNEL
316 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
317 boolean_t arc_watch = B_FALSE;
318 #endif
319 
320 /*
321  * This thread's job is to keep enough free memory in the system, by
322  * calling arc_kmem_reap_soon() plus arc_reduce_target_size(), which improves
323  * arc_available_memory().
324  */
325 static zthr_t *arc_reap_zthr;
326 
327 /*
328  * This thread's job is to keep arc_size under arc_c, by calling
329  * arc_evict(), which improves arc_is_overflowing().
330  */
331 static zthr_t *arc_evict_zthr;
332 static arc_buf_hdr_t **arc_state_evict_markers;
333 static int arc_state_evict_marker_count;
334 
335 static kmutex_t arc_evict_lock;
336 static boolean_t arc_evict_needed = B_FALSE;
337 static clock_t arc_last_uncached_flush;
338 
339 /*
340  * Count of bytes evicted since boot.
341  */
342 static uint64_t arc_evict_count;
343 
344 /*
345  * List of arc_evict_waiter_t's, representing threads waiting for the
346  * arc_evict_count to reach specific values.
347  */
348 static list_t arc_evict_waiters;
349 
350 /*
351  * When arc_is_overflowing(), arc_get_data_impl() waits for this percent of
352  * the requested amount of data to be evicted.  For example, by default for
353  * every 2KB that's evicted, 1KB of it may be "reused" by a new allocation.
354  * Since this is above 100%, it ensures that progress is made towards getting
355  * arc_size under arc_c.  Since this is finite, it ensures that allocations
356  * can still happen, even during the potentially long time that arc_size is
357  * more than arc_c.
358  */
359 static uint_t zfs_arc_eviction_pct = 200;
360 
361 /*
362  * The number of headers to evict in arc_evict_state_impl() before
363  * dropping the sublist lock and evicting from another sublist. A lower
364  * value means we're more likely to evict the "correct" header (i.e. the
365  * oldest header in the arc state), but comes with higher overhead
366  * (i.e. more invocations of arc_evict_state_impl()).
367  */
368 static uint_t zfs_arc_evict_batch_limit = 10;
369 
370 /* number of seconds before growing cache again */
371 uint_t arc_grow_retry = 5;
372 
373 /*
374  * Minimum time between calls to arc_kmem_reap_soon().
375  */
376 static const int arc_kmem_cache_reap_retry_ms = 1000;
377 
378 /* shift of arc_c for calculating overflow limit in arc_get_data_impl */
379 static int zfs_arc_overflow_shift = 8;
380 
381 /* log2(fraction of arc to reclaim) */
382 uint_t arc_shrink_shift = 7;
383 
384 /* percent of pagecache to reclaim arc to */
385 #ifdef _KERNEL
386 uint_t zfs_arc_pc_percent = 0;
387 #endif
388 
389 /*
390  * log2(fraction of ARC which must be free to allow growing).
391  * I.e. If there is less than arc_c >> arc_no_grow_shift free memory,
392  * when reading a new block into the ARC, we will evict an equal-sized block
393  * from the ARC.
394  *
395  * This must be less than arc_shrink_shift, so that when we shrink the ARC,
396  * we will still not allow it to grow.
397  */
398 uint_t		arc_no_grow_shift = 5;
399 
400 
401 /*
402  * minimum lifespan of a prefetch block in clock ticks
403  * (initialized in arc_init())
404  */
405 static uint_t		arc_min_prefetch_ms;
406 static uint_t		arc_min_prescient_prefetch_ms;
407 
408 /*
409  * If this percent of memory is free, don't throttle.
410  */
411 uint_t arc_lotsfree_percent = 10;
412 
413 /*
414  * The arc has filled available memory and has now warmed up.
415  */
416 boolean_t arc_warm;
417 
418 /*
419  * These tunables are for performance analysis.
420  */
421 uint64_t zfs_arc_max = 0;
422 uint64_t zfs_arc_min = 0;
423 static uint64_t zfs_arc_dnode_limit = 0;
424 static uint_t zfs_arc_dnode_reduce_percent = 10;
425 static uint_t zfs_arc_grow_retry = 0;
426 static uint_t zfs_arc_shrink_shift = 0;
427 uint_t zfs_arc_average_blocksize = 8 * 1024; /* 8KB */
428 
429 /*
430  * ARC dirty data constraints for arc_tempreserve_space() throttle:
431  * * total dirty data limit
432  * * anon block dirty limit
433  * * each pool's anon allowance
434  */
435 static const unsigned long zfs_arc_dirty_limit_percent = 50;
436 static const unsigned long zfs_arc_anon_limit_percent = 25;
437 static const unsigned long zfs_arc_pool_dirty_percent = 20;
438 
439 /*
440  * Enable or disable compressed arc buffers.
441  */
442 int zfs_compressed_arc_enabled = B_TRUE;
443 
444 /*
445  * Balance between metadata and data on ghost hits.  Values above 100
446  * increase metadata caching by proportionally reducing effect of ghost
447  * data hits on target data/metadata rate.
448  */
449 static uint_t zfs_arc_meta_balance = 500;
450 
451 /*
452  * Percentage that can be consumed by dnodes of ARC meta buffers.
453  */
454 static uint_t zfs_arc_dnode_limit_percent = 10;
455 
456 /*
457  * These tunables are Linux-specific
458  */
459 static uint64_t zfs_arc_sys_free = 0;
460 static uint_t zfs_arc_min_prefetch_ms = 0;
461 static uint_t zfs_arc_min_prescient_prefetch_ms = 0;
462 static uint_t zfs_arc_lotsfree_percent = 10;
463 
464 /*
465  * Number of arc_prune threads
466  */
467 static int zfs_arc_prune_task_threads = 1;
468 
469 /* Used by spa_export/spa_destroy to flush the arc asynchronously */
470 static taskq_t *arc_flush_taskq;
471 
472 /* The 7 states: */
473 arc_state_t ARC_anon;
474 arc_state_t ARC_mru;
475 arc_state_t ARC_mru_ghost;
476 arc_state_t ARC_mfu;
477 arc_state_t ARC_mfu_ghost;
478 arc_state_t ARC_l2c_only;
479 arc_state_t ARC_uncached;
480 
481 arc_stats_t arc_stats = {
482 	{ "hits",			KSTAT_DATA_UINT64 },
483 	{ "iohits",			KSTAT_DATA_UINT64 },
484 	{ "misses",			KSTAT_DATA_UINT64 },
485 	{ "demand_data_hits",		KSTAT_DATA_UINT64 },
486 	{ "demand_data_iohits",		KSTAT_DATA_UINT64 },
487 	{ "demand_data_misses",		KSTAT_DATA_UINT64 },
488 	{ "demand_metadata_hits",	KSTAT_DATA_UINT64 },
489 	{ "demand_metadata_iohits",	KSTAT_DATA_UINT64 },
490 	{ "demand_metadata_misses",	KSTAT_DATA_UINT64 },
491 	{ "prefetch_data_hits",		KSTAT_DATA_UINT64 },
492 	{ "prefetch_data_iohits",	KSTAT_DATA_UINT64 },
493 	{ "prefetch_data_misses",	KSTAT_DATA_UINT64 },
494 	{ "prefetch_metadata_hits",	KSTAT_DATA_UINT64 },
495 	{ "prefetch_metadata_iohits",	KSTAT_DATA_UINT64 },
496 	{ "prefetch_metadata_misses",	KSTAT_DATA_UINT64 },
497 	{ "mru_hits",			KSTAT_DATA_UINT64 },
498 	{ "mru_ghost_hits",		KSTAT_DATA_UINT64 },
499 	{ "mfu_hits",			KSTAT_DATA_UINT64 },
500 	{ "mfu_ghost_hits",		KSTAT_DATA_UINT64 },
501 	{ "uncached_hits",		KSTAT_DATA_UINT64 },
502 	{ "deleted",			KSTAT_DATA_UINT64 },
503 	{ "mutex_miss",			KSTAT_DATA_UINT64 },
504 	{ "access_skip",		KSTAT_DATA_UINT64 },
505 	{ "evict_skip",			KSTAT_DATA_UINT64 },
506 	{ "evict_not_enough",		KSTAT_DATA_UINT64 },
507 	{ "evict_l2_cached",		KSTAT_DATA_UINT64 },
508 	{ "evict_l2_eligible",		KSTAT_DATA_UINT64 },
509 	{ "evict_l2_eligible_mfu",	KSTAT_DATA_UINT64 },
510 	{ "evict_l2_eligible_mru",	KSTAT_DATA_UINT64 },
511 	{ "evict_l2_ineligible",	KSTAT_DATA_UINT64 },
512 	{ "evict_l2_skip",		KSTAT_DATA_UINT64 },
513 	{ "hash_elements",		KSTAT_DATA_UINT64 },
514 	{ "hash_elements_max",		KSTAT_DATA_UINT64 },
515 	{ "hash_collisions",		KSTAT_DATA_UINT64 },
516 	{ "hash_chains",		KSTAT_DATA_UINT64 },
517 	{ "hash_chain_max",		KSTAT_DATA_UINT64 },
518 	{ "meta",			KSTAT_DATA_UINT64 },
519 	{ "pd",				KSTAT_DATA_UINT64 },
520 	{ "pm",				KSTAT_DATA_UINT64 },
521 	{ "c",				KSTAT_DATA_UINT64 },
522 	{ "c_min",			KSTAT_DATA_UINT64 },
523 	{ "c_max",			KSTAT_DATA_UINT64 },
524 	{ "size",			KSTAT_DATA_UINT64 },
525 	{ "compressed_size",		KSTAT_DATA_UINT64 },
526 	{ "uncompressed_size",		KSTAT_DATA_UINT64 },
527 	{ "overhead_size",		KSTAT_DATA_UINT64 },
528 	{ "hdr_size",			KSTAT_DATA_UINT64 },
529 	{ "data_size",			KSTAT_DATA_UINT64 },
530 	{ "metadata_size",		KSTAT_DATA_UINT64 },
531 	{ "dbuf_size",			KSTAT_DATA_UINT64 },
532 	{ "dnode_size",			KSTAT_DATA_UINT64 },
533 	{ "bonus_size",			KSTAT_DATA_UINT64 },
534 #if defined(COMPAT_FREEBSD11)
535 	{ "other_size",			KSTAT_DATA_UINT64 },
536 #endif
537 	{ "anon_size",			KSTAT_DATA_UINT64 },
538 	{ "anon_data",			KSTAT_DATA_UINT64 },
539 	{ "anon_metadata",		KSTAT_DATA_UINT64 },
540 	{ "anon_evictable_data",	KSTAT_DATA_UINT64 },
541 	{ "anon_evictable_metadata",	KSTAT_DATA_UINT64 },
542 	{ "mru_size",			KSTAT_DATA_UINT64 },
543 	{ "mru_data",			KSTAT_DATA_UINT64 },
544 	{ "mru_metadata",		KSTAT_DATA_UINT64 },
545 	{ "mru_evictable_data",		KSTAT_DATA_UINT64 },
546 	{ "mru_evictable_metadata",	KSTAT_DATA_UINT64 },
547 	{ "mru_ghost_size",		KSTAT_DATA_UINT64 },
548 	{ "mru_ghost_data",		KSTAT_DATA_UINT64 },
549 	{ "mru_ghost_metadata",		KSTAT_DATA_UINT64 },
550 	{ "mru_ghost_evictable_data",	KSTAT_DATA_UINT64 },
551 	{ "mru_ghost_evictable_metadata", KSTAT_DATA_UINT64 },
552 	{ "mfu_size",			KSTAT_DATA_UINT64 },
553 	{ "mfu_data",			KSTAT_DATA_UINT64 },
554 	{ "mfu_metadata",		KSTAT_DATA_UINT64 },
555 	{ "mfu_evictable_data",		KSTAT_DATA_UINT64 },
556 	{ "mfu_evictable_metadata",	KSTAT_DATA_UINT64 },
557 	{ "mfu_ghost_size",		KSTAT_DATA_UINT64 },
558 	{ "mfu_ghost_data",		KSTAT_DATA_UINT64 },
559 	{ "mfu_ghost_metadata",		KSTAT_DATA_UINT64 },
560 	{ "mfu_ghost_evictable_data",	KSTAT_DATA_UINT64 },
561 	{ "mfu_ghost_evictable_metadata", KSTAT_DATA_UINT64 },
562 	{ "uncached_size",		KSTAT_DATA_UINT64 },
563 	{ "uncached_data",		KSTAT_DATA_UINT64 },
564 	{ "uncached_metadata",		KSTAT_DATA_UINT64 },
565 	{ "uncached_evictable_data",	KSTAT_DATA_UINT64 },
566 	{ "uncached_evictable_metadata", KSTAT_DATA_UINT64 },
567 	{ "l2_hits",			KSTAT_DATA_UINT64 },
568 	{ "l2_misses",			KSTAT_DATA_UINT64 },
569 	{ "l2_prefetch_asize",		KSTAT_DATA_UINT64 },
570 	{ "l2_mru_asize",		KSTAT_DATA_UINT64 },
571 	{ "l2_mfu_asize",		KSTAT_DATA_UINT64 },
572 	{ "l2_bufc_data_asize",		KSTAT_DATA_UINT64 },
573 	{ "l2_bufc_metadata_asize",	KSTAT_DATA_UINT64 },
574 	{ "l2_feeds",			KSTAT_DATA_UINT64 },
575 	{ "l2_rw_clash",		KSTAT_DATA_UINT64 },
576 	{ "l2_read_bytes",		KSTAT_DATA_UINT64 },
577 	{ "l2_write_bytes",		KSTAT_DATA_UINT64 },
578 	{ "l2_writes_sent",		KSTAT_DATA_UINT64 },
579 	{ "l2_writes_done",		KSTAT_DATA_UINT64 },
580 	{ "l2_writes_error",		KSTAT_DATA_UINT64 },
581 	{ "l2_writes_lock_retry",	KSTAT_DATA_UINT64 },
582 	{ "l2_evict_lock_retry",	KSTAT_DATA_UINT64 },
583 	{ "l2_evict_reading",		KSTAT_DATA_UINT64 },
584 	{ "l2_evict_l1cached",		KSTAT_DATA_UINT64 },
585 	{ "l2_free_on_write",		KSTAT_DATA_UINT64 },
586 	{ "l2_abort_lowmem",		KSTAT_DATA_UINT64 },
587 	{ "l2_cksum_bad",		KSTAT_DATA_UINT64 },
588 	{ "l2_io_error",		KSTAT_DATA_UINT64 },
589 	{ "l2_size",			KSTAT_DATA_UINT64 },
590 	{ "l2_asize",			KSTAT_DATA_UINT64 },
591 	{ "l2_hdr_size",		KSTAT_DATA_UINT64 },
592 	{ "l2_log_blk_writes",		KSTAT_DATA_UINT64 },
593 	{ "l2_log_blk_avg_asize",	KSTAT_DATA_UINT64 },
594 	{ "l2_log_blk_asize",		KSTAT_DATA_UINT64 },
595 	{ "l2_log_blk_count",		KSTAT_DATA_UINT64 },
596 	{ "l2_data_to_meta_ratio",	KSTAT_DATA_UINT64 },
597 	{ "l2_rebuild_success",		KSTAT_DATA_UINT64 },
598 	{ "l2_rebuild_unsupported",	KSTAT_DATA_UINT64 },
599 	{ "l2_rebuild_io_errors",	KSTAT_DATA_UINT64 },
600 	{ "l2_rebuild_dh_errors",	KSTAT_DATA_UINT64 },
601 	{ "l2_rebuild_cksum_lb_errors",	KSTAT_DATA_UINT64 },
602 	{ "l2_rebuild_lowmem",		KSTAT_DATA_UINT64 },
603 	{ "l2_rebuild_size",		KSTAT_DATA_UINT64 },
604 	{ "l2_rebuild_asize",		KSTAT_DATA_UINT64 },
605 	{ "l2_rebuild_bufs",		KSTAT_DATA_UINT64 },
606 	{ "l2_rebuild_bufs_precached",	KSTAT_DATA_UINT64 },
607 	{ "l2_rebuild_log_blks",	KSTAT_DATA_UINT64 },
608 	{ "memory_throttle_count",	KSTAT_DATA_UINT64 },
609 	{ "memory_direct_count",	KSTAT_DATA_UINT64 },
610 	{ "memory_indirect_count",	KSTAT_DATA_UINT64 },
611 	{ "memory_all_bytes",		KSTAT_DATA_UINT64 },
612 	{ "memory_free_bytes",		KSTAT_DATA_UINT64 },
613 	{ "memory_available_bytes",	KSTAT_DATA_INT64 },
614 	{ "arc_no_grow",		KSTAT_DATA_UINT64 },
615 	{ "arc_tempreserve",		KSTAT_DATA_UINT64 },
616 	{ "arc_loaned_bytes",		KSTAT_DATA_UINT64 },
617 	{ "arc_prune",			KSTAT_DATA_UINT64 },
618 	{ "arc_meta_used",		KSTAT_DATA_UINT64 },
619 	{ "arc_dnode_limit",		KSTAT_DATA_UINT64 },
620 	{ "async_upgrade_sync",		KSTAT_DATA_UINT64 },
621 	{ "predictive_prefetch", KSTAT_DATA_UINT64 },
622 	{ "demand_hit_predictive_prefetch", KSTAT_DATA_UINT64 },
623 	{ "demand_iohit_predictive_prefetch", KSTAT_DATA_UINT64 },
624 	{ "prescient_prefetch", KSTAT_DATA_UINT64 },
625 	{ "demand_hit_prescient_prefetch", KSTAT_DATA_UINT64 },
626 	{ "demand_iohit_prescient_prefetch", KSTAT_DATA_UINT64 },
627 	{ "arc_need_free",		KSTAT_DATA_UINT64 },
628 	{ "arc_sys_free",		KSTAT_DATA_UINT64 },
629 	{ "arc_raw_size",		KSTAT_DATA_UINT64 },
630 	{ "cached_only_in_progress",	KSTAT_DATA_UINT64 },
631 	{ "abd_chunk_waste_size",	KSTAT_DATA_UINT64 },
632 };
633 
634 arc_sums_t arc_sums;
635 
636 #define	ARCSTAT_MAX(stat, val) {					\
637 	uint64_t m;							\
638 	while ((val) > (m = arc_stats.stat.value.ui64) &&		\
639 	    (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val))))	\
640 		continue;						\
641 }
642 
643 /*
644  * We define a macro to allow ARC hits/misses to be easily broken down by
645  * two separate conditions, giving a total of four different subtypes for
646  * each of hits and misses (so eight statistics total).
647  */
648 #define	ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
649 	if (cond1) {							\
650 		if (cond2) {						\
651 			ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
652 		} else {						\
653 			ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
654 		}							\
655 	} else {							\
656 		if (cond2) {						\
657 			ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
658 		} else {						\
659 			ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
660 		}							\
661 	}
662 
663 /*
664  * This macro allows us to use kstats as floating averages. Each time we
665  * update this kstat, we first factor it and the update value by
666  * ARCSTAT_AVG_FACTOR to shrink the new value's contribution to the overall
667  * average. This macro assumes that integer loads and stores are atomic, but
668  * is not safe for multiple writers updating the kstat in parallel (only the
669  * last writer's update will remain).
670  */
671 #define	ARCSTAT_F_AVG_FACTOR	3
672 #define	ARCSTAT_F_AVG(stat, value) \
673 	do { \
674 		uint64_t x = ARCSTAT(stat); \
675 		x = x - x / ARCSTAT_F_AVG_FACTOR + \
676 		    (value) / ARCSTAT_F_AVG_FACTOR; \
677 		ARCSTAT(stat) = x; \
678 	} while (0)
679 
680 static kstat_t			*arc_ksp;
681 
682 /*
683  * There are several ARC variables that are critical to export as kstats --
684  * but we don't want to have to grovel around in the kstat whenever we wish to
685  * manipulate them.  For these variables, we therefore define them to be in
686  * terms of the statistic variable.  This assures that we are not introducing
687  * the possibility of inconsistency by having shadow copies of the variables,
688  * while still allowing the code to be readable.
689  */
690 #define	arc_tempreserve	ARCSTAT(arcstat_tempreserve)
691 #define	arc_loaned_bytes	ARCSTAT(arcstat_loaned_bytes)
692 #define	arc_dnode_limit	ARCSTAT(arcstat_dnode_limit) /* max size for dnodes */
693 #define	arc_need_free	ARCSTAT(arcstat_need_free) /* waiting to be evicted */
694 
695 hrtime_t arc_growtime;
696 list_t arc_prune_list;
697 kmutex_t arc_prune_mtx;
698 taskq_t *arc_prune_taskq;
699 
700 #define	GHOST_STATE(state)	\
701 	((state) == arc_mru_ghost || (state) == arc_mfu_ghost ||	\
702 	(state) == arc_l2c_only)
703 
704 #define	HDR_IN_HASH_TABLE(hdr)	((hdr)->b_flags & ARC_FLAG_IN_HASH_TABLE)
705 #define	HDR_IO_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS)
706 #define	HDR_IO_ERROR(hdr)	((hdr)->b_flags & ARC_FLAG_IO_ERROR)
707 #define	HDR_PREFETCH(hdr)	((hdr)->b_flags & ARC_FLAG_PREFETCH)
708 #define	HDR_PRESCIENT_PREFETCH(hdr)	\
709 	((hdr)->b_flags & ARC_FLAG_PRESCIENT_PREFETCH)
710 #define	HDR_COMPRESSION_ENABLED(hdr)	\
711 	((hdr)->b_flags & ARC_FLAG_COMPRESSED_ARC)
712 
713 #define	HDR_L2CACHE(hdr)	((hdr)->b_flags & ARC_FLAG_L2CACHE)
714 #define	HDR_UNCACHED(hdr)	((hdr)->b_flags & ARC_FLAG_UNCACHED)
715 #define	HDR_L2_READING(hdr)	\
716 	(((hdr)->b_flags & ARC_FLAG_IO_IN_PROGRESS) &&	\
717 	((hdr)->b_flags & ARC_FLAG_HAS_L2HDR))
718 #define	HDR_L2_WRITING(hdr)	((hdr)->b_flags & ARC_FLAG_L2_WRITING)
719 #define	HDR_L2_EVICTED(hdr)	((hdr)->b_flags & ARC_FLAG_L2_EVICTED)
720 #define	HDR_L2_WRITE_HEAD(hdr)	((hdr)->b_flags & ARC_FLAG_L2_WRITE_HEAD)
721 #define	HDR_PROTECTED(hdr)	((hdr)->b_flags & ARC_FLAG_PROTECTED)
722 #define	HDR_NOAUTH(hdr)		((hdr)->b_flags & ARC_FLAG_NOAUTH)
723 #define	HDR_SHARED_DATA(hdr)	((hdr)->b_flags & ARC_FLAG_SHARED_DATA)
724 
725 #define	HDR_ISTYPE_METADATA(hdr)	\
726 	((hdr)->b_flags & ARC_FLAG_BUFC_METADATA)
727 #define	HDR_ISTYPE_DATA(hdr)	(!HDR_ISTYPE_METADATA(hdr))
728 
729 #define	HDR_HAS_L1HDR(hdr)	((hdr)->b_flags & ARC_FLAG_HAS_L1HDR)
730 #define	HDR_HAS_L2HDR(hdr)	((hdr)->b_flags & ARC_FLAG_HAS_L2HDR)
731 #define	HDR_HAS_RABD(hdr)	\
732 	(HDR_HAS_L1HDR(hdr) && HDR_PROTECTED(hdr) &&	\
733 	(hdr)->b_crypt_hdr.b_rabd != NULL)
734 #define	HDR_ENCRYPTED(hdr)	\
735 	(HDR_PROTECTED(hdr) && DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
736 #define	HDR_AUTHENTICATED(hdr)	\
737 	(HDR_PROTECTED(hdr) && !DMU_OT_IS_ENCRYPTED((hdr)->b_crypt_hdr.b_ot))
738 
739 /* For storing compression mode in b_flags */
740 #define	HDR_COMPRESS_OFFSET	(highbit64(ARC_FLAG_COMPRESS_0) - 1)
741 
742 #define	HDR_GET_COMPRESS(hdr)	((enum zio_compress)BF32_GET((hdr)->b_flags, \
743 	HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS))
744 #define	HDR_SET_COMPRESS(hdr, cmp) BF32_SET((hdr)->b_flags, \
745 	HDR_COMPRESS_OFFSET, SPA_COMPRESSBITS, (cmp));
746 
747 #define	ARC_BUF_LAST(buf)	((buf)->b_next == NULL)
748 #define	ARC_BUF_SHARED(buf)	((buf)->b_flags & ARC_BUF_FLAG_SHARED)
749 #define	ARC_BUF_COMPRESSED(buf)	((buf)->b_flags & ARC_BUF_FLAG_COMPRESSED)
750 #define	ARC_BUF_ENCRYPTED(buf)	((buf)->b_flags & ARC_BUF_FLAG_ENCRYPTED)
751 
752 /*
753  * Other sizes
754  */
755 
756 #define	HDR_FULL_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
757 #define	HDR_L2ONLY_SIZE ((int64_t)offsetof(arc_buf_hdr_t, b_l1hdr))
758 
759 /*
760  * Hash table routines
761  */
762 
763 #define	BUF_LOCKS 2048
764 typedef struct buf_hash_table {
765 	uint64_t ht_mask;
766 	arc_buf_hdr_t **ht_table;
767 	kmutex_t ht_locks[BUF_LOCKS] ____cacheline_aligned;
768 } buf_hash_table_t;
769 
770 static buf_hash_table_t buf_hash_table;
771 
772 #define	BUF_HASH_INDEX(spa, dva, birth) \
773 	(buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
774 #define	BUF_HASH_LOCK(idx)	(&buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
775 #define	HDR_LOCK(hdr) \
776 	(BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
777 
778 uint64_t zfs_crc64_table[256];
779 
780 /*
781  * Asynchronous ARC flush
782  *
783  * We track these in a list for arc_async_flush_guid_inuse().
784  * Used for both L1 and L2 async teardown.
785  */
786 static list_t arc_async_flush_list;
787 static kmutex_t	arc_async_flush_lock;
788 
789 typedef struct arc_async_flush {
790 	uint64_t	af_spa_guid;
791 	taskq_ent_t	af_tqent;
792 	uint_t		af_cache_level;	/* 1 or 2 to differentiate node */
793 	list_node_t	af_node;
794 } arc_async_flush_t;
795 
796 
797 /*
798  * Level 2 ARC
799  */
800 
801 #define	L2ARC_WRITE_SIZE	(32 * 1024 * 1024)	/* initial write max */
802 #define	L2ARC_HEADROOM		8			/* num of writes */
803 
804 /*
805  * If we discover during ARC scan any buffers to be compressed, we boost
806  * our headroom for the next scanning cycle by this percentage multiple.
807  */
808 #define	L2ARC_HEADROOM_BOOST	200
809 #define	L2ARC_FEED_SECS		1		/* caching interval secs */
810 #define	L2ARC_FEED_MIN_MS	200		/* min caching interval ms */
811 
812 /*
813  * We can feed L2ARC from two states of ARC buffers, mru and mfu,
814  * and each of the state has two types: data and metadata.
815  */
816 #define	L2ARC_FEED_TYPES	4
817 
818 /* L2ARC Performance Tunables */
819 uint64_t l2arc_write_max = L2ARC_WRITE_SIZE;	/* def max write size */
820 uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE;	/* extra warmup write */
821 uint64_t l2arc_headroom = L2ARC_HEADROOM;	/* # of dev writes */
822 uint64_t l2arc_headroom_boost = L2ARC_HEADROOM_BOOST;
823 uint64_t l2arc_feed_secs = L2ARC_FEED_SECS;	/* interval seconds */
824 uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS;	/* min interval msecs */
825 int l2arc_noprefetch = B_TRUE;			/* don't cache prefetch bufs */
826 int l2arc_feed_again = B_TRUE;			/* turbo warmup */
827 int l2arc_norw = B_FALSE;			/* no reads during writes */
828 static uint_t l2arc_meta_percent = 33;	/* limit on headers size */
829 
830 /*
831  * L2ARC Internals
832  */
833 static list_t L2ARC_dev_list;			/* device list */
834 static list_t *l2arc_dev_list;			/* device list pointer */
835 static kmutex_t l2arc_dev_mtx;			/* device list mutex */
836 static l2arc_dev_t *l2arc_dev_last;		/* last device used */
837 static list_t L2ARC_free_on_write;		/* free after write buf list */
838 static list_t *l2arc_free_on_write;		/* free after write list ptr */
839 static kmutex_t l2arc_free_on_write_mtx;	/* mutex for list */
840 static uint64_t l2arc_ndev;			/* number of devices */
841 
842 typedef struct l2arc_read_callback {
843 	arc_buf_hdr_t		*l2rcb_hdr;		/* read header */
844 	blkptr_t		l2rcb_bp;		/* original blkptr */
845 	zbookmark_phys_t	l2rcb_zb;		/* original bookmark */
846 	int			l2rcb_flags;		/* original flags */
847 	abd_t			*l2rcb_abd;		/* temporary buffer */
848 } l2arc_read_callback_t;
849 
850 typedef struct l2arc_data_free {
851 	/* protected by l2arc_free_on_write_mtx */
852 	abd_t		*l2df_abd;
853 	size_t		l2df_size;
854 	arc_buf_contents_t l2df_type;
855 	list_node_t	l2df_list_node;
856 } l2arc_data_free_t;
857 
858 typedef enum arc_fill_flags {
859 	ARC_FILL_LOCKED		= 1 << 0, /* hdr lock is held */
860 	ARC_FILL_COMPRESSED	= 1 << 1, /* fill with compressed data */
861 	ARC_FILL_ENCRYPTED	= 1 << 2, /* fill with encrypted data */
862 	ARC_FILL_NOAUTH		= 1 << 3, /* don't attempt to authenticate */
863 	ARC_FILL_IN_PLACE	= 1 << 4  /* fill in place (special case) */
864 } arc_fill_flags_t;
865 
866 typedef enum arc_ovf_level {
867 	ARC_OVF_NONE,			/* ARC within target size. */
868 	ARC_OVF_SOME,			/* ARC is slightly overflowed. */
869 	ARC_OVF_SEVERE			/* ARC is severely overflowed. */
870 } arc_ovf_level_t;
871 
872 static kmutex_t l2arc_feed_thr_lock;
873 static kcondvar_t l2arc_feed_thr_cv;
874 static uint8_t l2arc_thread_exit;
875 
876 static kmutex_t l2arc_rebuild_thr_lock;
877 static kcondvar_t l2arc_rebuild_thr_cv;
878 
879 enum arc_hdr_alloc_flags {
880 	ARC_HDR_ALLOC_RDATA = 0x1,
881 	ARC_HDR_USE_RESERVE = 0x4,
882 	ARC_HDR_ALLOC_LINEAR = 0x8,
883 };
884 
885 
886 static abd_t *arc_get_data_abd(arc_buf_hdr_t *, uint64_t, const void *, int);
887 static void *arc_get_data_buf(arc_buf_hdr_t *, uint64_t, const void *);
888 static void arc_get_data_impl(arc_buf_hdr_t *, uint64_t, const void *, int);
889 static void arc_free_data_abd(arc_buf_hdr_t *, abd_t *, uint64_t, const void *);
890 static void arc_free_data_buf(arc_buf_hdr_t *, void *, uint64_t, const void *);
891 static void arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size,
892     const void *tag);
893 static void arc_hdr_free_abd(arc_buf_hdr_t *, boolean_t);
894 static void arc_hdr_alloc_abd(arc_buf_hdr_t *, int);
895 static void arc_hdr_destroy(arc_buf_hdr_t *);
896 static void arc_access(arc_buf_hdr_t *, arc_flags_t, boolean_t);
897 static void arc_buf_watch(arc_buf_t *);
898 static void arc_change_state(arc_state_t *, arc_buf_hdr_t *);
899 
900 static arc_buf_contents_t arc_buf_type(arc_buf_hdr_t *);
901 static uint32_t arc_bufc_to_flags(arc_buf_contents_t);
902 static inline void arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags);
903 static inline void arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags);
904 
905 static boolean_t l2arc_write_eligible(uint64_t, arc_buf_hdr_t *);
906 static void l2arc_read_done(zio_t *);
907 static void l2arc_do_free_on_write(void);
908 static void l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr,
909     boolean_t state_only);
910 
911 static void arc_prune_async(uint64_t adjust);
912 
913 #define	l2arc_hdr_arcstats_increment(hdr) \
914 	l2arc_hdr_arcstats_update((hdr), B_TRUE, B_FALSE)
915 #define	l2arc_hdr_arcstats_decrement(hdr) \
916 	l2arc_hdr_arcstats_update((hdr), B_FALSE, B_FALSE)
917 #define	l2arc_hdr_arcstats_increment_state(hdr) \
918 	l2arc_hdr_arcstats_update((hdr), B_TRUE, B_TRUE)
919 #define	l2arc_hdr_arcstats_decrement_state(hdr) \
920 	l2arc_hdr_arcstats_update((hdr), B_FALSE, B_TRUE)
921 
922 /*
923  * l2arc_exclude_special : A zfs module parameter that controls whether buffers
924  * 		present on special vdevs are eligibile for caching in L2ARC. If
925  * 		set to 1, exclude dbufs on special vdevs from being cached to
926  * 		L2ARC.
927  */
928 int l2arc_exclude_special = 0;
929 
930 /*
931  * l2arc_mfuonly : A ZFS module parameter that controls whether only MFU
932  * 		metadata and data are cached from ARC into L2ARC.
933  */
934 static int l2arc_mfuonly = 0;
935 
936 /*
937  * L2ARC TRIM
938  * l2arc_trim_ahead : A ZFS module parameter that controls how much ahead of
939  * 		the current write size (l2arc_write_max) we should TRIM if we
940  * 		have filled the device. It is defined as a percentage of the
941  * 		write size. If set to 100 we trim twice the space required to
942  * 		accommodate upcoming writes. A minimum of 64MB will be trimmed.
943  * 		It also enables TRIM of the whole L2ARC device upon creation or
944  * 		addition to an existing pool or if the header of the device is
945  * 		invalid upon importing a pool or onlining a cache device. The
946  * 		default is 0, which disables TRIM on L2ARC altogether as it can
947  * 		put significant stress on the underlying storage devices. This
948  * 		will vary depending of how well the specific device handles
949  * 		these commands.
950  */
951 static uint64_t l2arc_trim_ahead = 0;
952 
953 /*
954  * Performance tuning of L2ARC persistence:
955  *
956  * l2arc_rebuild_enabled : A ZFS module parameter that controls whether adding
957  * 		an L2ARC device (either at pool import or later) will attempt
958  * 		to rebuild L2ARC buffer contents.
959  * l2arc_rebuild_blocks_min_l2size : A ZFS module parameter that controls
960  * 		whether log blocks are written to the L2ARC device. If the L2ARC
961  * 		device is less than 1GB, the amount of data l2arc_evict()
962  * 		evicts is significant compared to the amount of restored L2ARC
963  * 		data. In this case do not write log blocks in L2ARC in order
964  * 		not to waste space.
965  */
966 static int l2arc_rebuild_enabled = B_TRUE;
967 static uint64_t l2arc_rebuild_blocks_min_l2size = 1024 * 1024 * 1024;
968 
969 /* L2ARC persistence rebuild control routines. */
970 void l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen);
971 static __attribute__((noreturn)) void l2arc_dev_rebuild_thread(void *arg);
972 static int l2arc_rebuild(l2arc_dev_t *dev);
973 
974 /* L2ARC persistence read I/O routines. */
975 static int l2arc_dev_hdr_read(l2arc_dev_t *dev);
976 static int l2arc_log_blk_read(l2arc_dev_t *dev,
977     const l2arc_log_blkptr_t *this_lp, const l2arc_log_blkptr_t *next_lp,
978     l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb,
979     zio_t *this_io, zio_t **next_io);
980 static zio_t *l2arc_log_blk_fetch(vdev_t *vd,
981     const l2arc_log_blkptr_t *lp, l2arc_log_blk_phys_t *lb);
982 static void l2arc_log_blk_fetch_abort(zio_t *zio);
983 
984 /* L2ARC persistence block restoration routines. */
985 static void l2arc_log_blk_restore(l2arc_dev_t *dev,
986     const l2arc_log_blk_phys_t *lb, uint64_t lb_asize);
987 static void l2arc_hdr_restore(const l2arc_log_ent_phys_t *le,
988     l2arc_dev_t *dev);
989 
990 /* L2ARC persistence write I/O routines. */
991 static uint64_t l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio,
992     l2arc_write_callback_t *cb);
993 
994 /* L2ARC persistence auxiliary routines. */
995 boolean_t l2arc_log_blkptr_valid(l2arc_dev_t *dev,
996     const l2arc_log_blkptr_t *lbp);
997 static boolean_t l2arc_log_blk_insert(l2arc_dev_t *dev,
998     const arc_buf_hdr_t *ab);
999 boolean_t l2arc_range_check_overlap(uint64_t bottom,
1000     uint64_t top, uint64_t check);
1001 static void l2arc_blk_fetch_done(zio_t *zio);
1002 static inline uint64_t
1003     l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev);
1004 
1005 /*
1006  * We use Cityhash for this. It's fast, and has good hash properties without
1007  * requiring any large static buffers.
1008  */
1009 static uint64_t
buf_hash(uint64_t spa,const dva_t * dva,uint64_t birth)1010 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
1011 {
1012 	return (cityhash4(spa, dva->dva_word[0], dva->dva_word[1], birth));
1013 }
1014 
1015 #define	HDR_EMPTY(hdr)						\
1016 	((hdr)->b_dva.dva_word[0] == 0 &&			\
1017 	(hdr)->b_dva.dva_word[1] == 0)
1018 
1019 #define	HDR_EMPTY_OR_LOCKED(hdr)				\
1020 	(HDR_EMPTY(hdr) || MUTEX_HELD(HDR_LOCK(hdr)))
1021 
1022 #define	HDR_EQUAL(spa, dva, birth, hdr)				\
1023 	((hdr)->b_dva.dva_word[0] == (dva)->dva_word[0]) &&	\
1024 	((hdr)->b_dva.dva_word[1] == (dva)->dva_word[1]) &&	\
1025 	((hdr)->b_birth == birth) && ((hdr)->b_spa == spa)
1026 
1027 static void
buf_discard_identity(arc_buf_hdr_t * hdr)1028 buf_discard_identity(arc_buf_hdr_t *hdr)
1029 {
1030 	hdr->b_dva.dva_word[0] = 0;
1031 	hdr->b_dva.dva_word[1] = 0;
1032 	hdr->b_birth = 0;
1033 }
1034 
1035 static arc_buf_hdr_t *
buf_hash_find(uint64_t spa,const blkptr_t * bp,kmutex_t ** lockp)1036 buf_hash_find(uint64_t spa, const blkptr_t *bp, kmutex_t **lockp)
1037 {
1038 	const dva_t *dva = BP_IDENTITY(bp);
1039 	uint64_t birth = BP_GET_BIRTH(bp);
1040 	uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
1041 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
1042 	arc_buf_hdr_t *hdr;
1043 
1044 	mutex_enter(hash_lock);
1045 	for (hdr = buf_hash_table.ht_table[idx]; hdr != NULL;
1046 	    hdr = hdr->b_hash_next) {
1047 		if (HDR_EQUAL(spa, dva, birth, hdr)) {
1048 			*lockp = hash_lock;
1049 			return (hdr);
1050 		}
1051 	}
1052 	mutex_exit(hash_lock);
1053 	*lockp = NULL;
1054 	return (NULL);
1055 }
1056 
1057 /*
1058  * Insert an entry into the hash table.  If there is already an element
1059  * equal to elem in the hash table, then the already existing element
1060  * will be returned and the new element will not be inserted.
1061  * Otherwise returns NULL.
1062  * If lockp == NULL, the caller is assumed to already hold the hash lock.
1063  */
1064 static arc_buf_hdr_t *
buf_hash_insert(arc_buf_hdr_t * hdr,kmutex_t ** lockp)1065 buf_hash_insert(arc_buf_hdr_t *hdr, kmutex_t **lockp)
1066 {
1067 	uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth);
1068 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
1069 	arc_buf_hdr_t *fhdr;
1070 	uint32_t i;
1071 
1072 	ASSERT(!DVA_IS_EMPTY(&hdr->b_dva));
1073 	ASSERT(hdr->b_birth != 0);
1074 	ASSERT(!HDR_IN_HASH_TABLE(hdr));
1075 
1076 	if (lockp != NULL) {
1077 		*lockp = hash_lock;
1078 		mutex_enter(hash_lock);
1079 	} else {
1080 		ASSERT(MUTEX_HELD(hash_lock));
1081 	}
1082 
1083 	for (fhdr = buf_hash_table.ht_table[idx], i = 0; fhdr != NULL;
1084 	    fhdr = fhdr->b_hash_next, i++) {
1085 		if (HDR_EQUAL(hdr->b_spa, &hdr->b_dva, hdr->b_birth, fhdr))
1086 			return (fhdr);
1087 	}
1088 
1089 	hdr->b_hash_next = buf_hash_table.ht_table[idx];
1090 	buf_hash_table.ht_table[idx] = hdr;
1091 	arc_hdr_set_flags(hdr, ARC_FLAG_IN_HASH_TABLE);
1092 
1093 	/* collect some hash table performance data */
1094 	if (i > 0) {
1095 		ARCSTAT_BUMP(arcstat_hash_collisions);
1096 		if (i == 1)
1097 			ARCSTAT_BUMP(arcstat_hash_chains);
1098 		ARCSTAT_MAX(arcstat_hash_chain_max, i);
1099 	}
1100 	ARCSTAT_BUMP(arcstat_hash_elements);
1101 
1102 	return (NULL);
1103 }
1104 
1105 static void
buf_hash_remove(arc_buf_hdr_t * hdr)1106 buf_hash_remove(arc_buf_hdr_t *hdr)
1107 {
1108 	arc_buf_hdr_t *fhdr, **hdrp;
1109 	uint64_t idx = BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth);
1110 
1111 	ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
1112 	ASSERT(HDR_IN_HASH_TABLE(hdr));
1113 
1114 	hdrp = &buf_hash_table.ht_table[idx];
1115 	while ((fhdr = *hdrp) != hdr) {
1116 		ASSERT3P(fhdr, !=, NULL);
1117 		hdrp = &fhdr->b_hash_next;
1118 	}
1119 	*hdrp = hdr->b_hash_next;
1120 	hdr->b_hash_next = NULL;
1121 	arc_hdr_clear_flags(hdr, ARC_FLAG_IN_HASH_TABLE);
1122 
1123 	/* collect some hash table performance data */
1124 	ARCSTAT_BUMPDOWN(arcstat_hash_elements);
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  */
1133 
1134 static kmem_cache_t *hdr_full_cache;
1135 static kmem_cache_t *hdr_l2only_cache;
1136 static kmem_cache_t *buf_cache;
1137 
1138 static void
buf_fini(void)1139 buf_fini(void)
1140 {
1141 #if defined(_KERNEL)
1142 	/*
1143 	 * Large allocations which do not require contiguous pages
1144 	 * should be using vmem_free() in the linux kernel\
1145 	 */
1146 	vmem_free(buf_hash_table.ht_table,
1147 	    (buf_hash_table.ht_mask + 1) * sizeof (void *));
1148 #else
1149 	kmem_free(buf_hash_table.ht_table,
1150 	    (buf_hash_table.ht_mask + 1) * sizeof (void *));
1151 #endif
1152 	for (int i = 0; i < BUF_LOCKS; i++)
1153 		mutex_destroy(BUF_HASH_LOCK(i));
1154 	kmem_cache_destroy(hdr_full_cache);
1155 	kmem_cache_destroy(hdr_l2only_cache);
1156 	kmem_cache_destroy(buf_cache);
1157 }
1158 
1159 /*
1160  * Constructor callback - called when the cache is empty
1161  * and a new buf is requested.
1162  */
1163 static int
hdr_full_cons(void * vbuf,void * unused,int kmflag)1164 hdr_full_cons(void *vbuf, void *unused, int kmflag)
1165 {
1166 	(void) unused, (void) kmflag;
1167 	arc_buf_hdr_t *hdr = vbuf;
1168 
1169 	memset(hdr, 0, HDR_FULL_SIZE);
1170 	hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS;
1171 	zfs_refcount_create(&hdr->b_l1hdr.b_refcnt);
1172 #ifdef ZFS_DEBUG
1173 	mutex_init(&hdr->b_l1hdr.b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
1174 #endif
1175 	multilist_link_init(&hdr->b_l1hdr.b_arc_node);
1176 	list_link_init(&hdr->b_l2hdr.b_l2node);
1177 	arc_space_consume(HDR_FULL_SIZE, ARC_SPACE_HDRS);
1178 
1179 	return (0);
1180 }
1181 
1182 static int
hdr_l2only_cons(void * vbuf,void * unused,int kmflag)1183 hdr_l2only_cons(void *vbuf, void *unused, int kmflag)
1184 {
1185 	(void) unused, (void) kmflag;
1186 	arc_buf_hdr_t *hdr = vbuf;
1187 
1188 	memset(hdr, 0, HDR_L2ONLY_SIZE);
1189 	arc_space_consume(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS);
1190 
1191 	return (0);
1192 }
1193 
1194 static int
buf_cons(void * vbuf,void * unused,int kmflag)1195 buf_cons(void *vbuf, void *unused, int kmflag)
1196 {
1197 	(void) unused, (void) kmflag;
1198 	arc_buf_t *buf = vbuf;
1199 
1200 	memset(buf, 0, sizeof (arc_buf_t));
1201 	arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
1202 
1203 	return (0);
1204 }
1205 
1206 /*
1207  * Destructor callback - called when a cached buf is
1208  * no longer required.
1209  */
1210 static void
hdr_full_dest(void * vbuf,void * unused)1211 hdr_full_dest(void *vbuf, void *unused)
1212 {
1213 	(void) unused;
1214 	arc_buf_hdr_t *hdr = vbuf;
1215 
1216 	ASSERT(HDR_EMPTY(hdr));
1217 	zfs_refcount_destroy(&hdr->b_l1hdr.b_refcnt);
1218 #ifdef ZFS_DEBUG
1219 	mutex_destroy(&hdr->b_l1hdr.b_freeze_lock);
1220 #endif
1221 	ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
1222 	arc_space_return(HDR_FULL_SIZE, ARC_SPACE_HDRS);
1223 }
1224 
1225 static void
hdr_l2only_dest(void * vbuf,void * unused)1226 hdr_l2only_dest(void *vbuf, void *unused)
1227 {
1228 	(void) unused;
1229 	arc_buf_hdr_t *hdr = vbuf;
1230 
1231 	ASSERT(HDR_EMPTY(hdr));
1232 	arc_space_return(HDR_L2ONLY_SIZE, ARC_SPACE_L2HDRS);
1233 }
1234 
1235 static void
buf_dest(void * vbuf,void * unused)1236 buf_dest(void *vbuf, void *unused)
1237 {
1238 	(void) unused;
1239 	(void) vbuf;
1240 
1241 	arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
1242 }
1243 
1244 static void
buf_init(void)1245 buf_init(void)
1246 {
1247 	uint64_t *ct = NULL;
1248 	uint64_t hsize = 1ULL << 12;
1249 	int i, j;
1250 
1251 	/*
1252 	 * The hash table is big enough to fill all of physical memory
1253 	 * with an average block size of zfs_arc_average_blocksize (default 8K).
1254 	 * By default, the table will take up
1255 	 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
1256 	 */
1257 	while (hsize * zfs_arc_average_blocksize < arc_all_memory())
1258 		hsize <<= 1;
1259 retry:
1260 	buf_hash_table.ht_mask = hsize - 1;
1261 #if defined(_KERNEL)
1262 	/*
1263 	 * Large allocations which do not require contiguous pages
1264 	 * should be using vmem_alloc() in the linux kernel
1265 	 */
1266 	buf_hash_table.ht_table =
1267 	    vmem_zalloc(hsize * sizeof (void*), KM_SLEEP);
1268 #else
1269 	buf_hash_table.ht_table =
1270 	    kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
1271 #endif
1272 	if (buf_hash_table.ht_table == NULL) {
1273 		ASSERT(hsize > (1ULL << 8));
1274 		hsize >>= 1;
1275 		goto retry;
1276 	}
1277 
1278 	hdr_full_cache = kmem_cache_create("arc_buf_hdr_t_full", HDR_FULL_SIZE,
1279 	    0, hdr_full_cons, hdr_full_dest, NULL, NULL, NULL, KMC_RECLAIMABLE);
1280 	hdr_l2only_cache = kmem_cache_create("arc_buf_hdr_t_l2only",
1281 	    HDR_L2ONLY_SIZE, 0, hdr_l2only_cons, hdr_l2only_dest, NULL,
1282 	    NULL, NULL, 0);
1283 	buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
1284 	    0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
1285 
1286 	for (i = 0; i < 256; i++)
1287 		for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
1288 			*ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
1289 
1290 	for (i = 0; i < BUF_LOCKS; i++)
1291 		mutex_init(BUF_HASH_LOCK(i), NULL, MUTEX_DEFAULT, NULL);
1292 }
1293 
1294 #define	ARC_MINTIME	(hz>>4) /* 62 ms */
1295 
1296 /*
1297  * This is the size that the buf occupies in memory. If the buf is compressed,
1298  * it will correspond to the compressed size. You should use this method of
1299  * getting the buf size unless you explicitly need the logical size.
1300  */
1301 uint64_t
arc_buf_size(arc_buf_t * buf)1302 arc_buf_size(arc_buf_t *buf)
1303 {
1304 	return (ARC_BUF_COMPRESSED(buf) ?
1305 	    HDR_GET_PSIZE(buf->b_hdr) : HDR_GET_LSIZE(buf->b_hdr));
1306 }
1307 
1308 uint64_t
arc_buf_lsize(arc_buf_t * buf)1309 arc_buf_lsize(arc_buf_t *buf)
1310 {
1311 	return (HDR_GET_LSIZE(buf->b_hdr));
1312 }
1313 
1314 /*
1315  * This function will return B_TRUE if the buffer is encrypted in memory.
1316  * This buffer can be decrypted by calling arc_untransform().
1317  */
1318 boolean_t
arc_is_encrypted(arc_buf_t * buf)1319 arc_is_encrypted(arc_buf_t *buf)
1320 {
1321 	return (ARC_BUF_ENCRYPTED(buf) != 0);
1322 }
1323 
1324 /*
1325  * Returns B_TRUE if the buffer represents data that has not had its MAC
1326  * verified yet.
1327  */
1328 boolean_t
arc_is_unauthenticated(arc_buf_t * buf)1329 arc_is_unauthenticated(arc_buf_t *buf)
1330 {
1331 	return (HDR_NOAUTH(buf->b_hdr) != 0);
1332 }
1333 
1334 void
arc_get_raw_params(arc_buf_t * buf,boolean_t * byteorder,uint8_t * salt,uint8_t * iv,uint8_t * mac)1335 arc_get_raw_params(arc_buf_t *buf, boolean_t *byteorder, uint8_t *salt,
1336     uint8_t *iv, uint8_t *mac)
1337 {
1338 	arc_buf_hdr_t *hdr = buf->b_hdr;
1339 
1340 	ASSERT(HDR_PROTECTED(hdr));
1341 
1342 	memcpy(salt, hdr->b_crypt_hdr.b_salt, ZIO_DATA_SALT_LEN);
1343 	memcpy(iv, hdr->b_crypt_hdr.b_iv, ZIO_DATA_IV_LEN);
1344 	memcpy(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN);
1345 	*byteorder = (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ?
1346 	    ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER;
1347 }
1348 
1349 /*
1350  * Indicates how this buffer is compressed in memory. If it is not compressed
1351  * the value will be ZIO_COMPRESS_OFF. It can be made normally readable with
1352  * arc_untransform() as long as it is also unencrypted.
1353  */
1354 enum zio_compress
arc_get_compression(arc_buf_t * buf)1355 arc_get_compression(arc_buf_t *buf)
1356 {
1357 	return (ARC_BUF_COMPRESSED(buf) ?
1358 	    HDR_GET_COMPRESS(buf->b_hdr) : ZIO_COMPRESS_OFF);
1359 }
1360 
1361 /*
1362  * Return the compression algorithm used to store this data in the ARC. If ARC
1363  * compression is enabled or this is an encrypted block, this will be the same
1364  * as what's used to store it on-disk. Otherwise, this will be ZIO_COMPRESS_OFF.
1365  */
1366 static inline enum zio_compress
arc_hdr_get_compress(arc_buf_hdr_t * hdr)1367 arc_hdr_get_compress(arc_buf_hdr_t *hdr)
1368 {
1369 	return (HDR_COMPRESSION_ENABLED(hdr) ?
1370 	    HDR_GET_COMPRESS(hdr) : ZIO_COMPRESS_OFF);
1371 }
1372 
1373 uint8_t
arc_get_complevel(arc_buf_t * buf)1374 arc_get_complevel(arc_buf_t *buf)
1375 {
1376 	return (buf->b_hdr->b_complevel);
1377 }
1378 
1379 static inline boolean_t
arc_buf_is_shared(arc_buf_t * buf)1380 arc_buf_is_shared(arc_buf_t *buf)
1381 {
1382 	boolean_t shared = (buf->b_data != NULL &&
1383 	    buf->b_hdr->b_l1hdr.b_pabd != NULL &&
1384 	    abd_is_linear(buf->b_hdr->b_l1hdr.b_pabd) &&
1385 	    buf->b_data == abd_to_buf(buf->b_hdr->b_l1hdr.b_pabd));
1386 	IMPLY(shared, HDR_SHARED_DATA(buf->b_hdr));
1387 	EQUIV(shared, ARC_BUF_SHARED(buf));
1388 	IMPLY(shared, ARC_BUF_COMPRESSED(buf) || ARC_BUF_LAST(buf));
1389 
1390 	/*
1391 	 * It would be nice to assert arc_can_share() too, but the "hdr isn't
1392 	 * already being shared" requirement prevents us from doing that.
1393 	 */
1394 
1395 	return (shared);
1396 }
1397 
1398 /*
1399  * Free the checksum associated with this header. If there is no checksum, this
1400  * is a no-op.
1401  */
1402 static inline void
arc_cksum_free(arc_buf_hdr_t * hdr)1403 arc_cksum_free(arc_buf_hdr_t *hdr)
1404 {
1405 #ifdef ZFS_DEBUG
1406 	ASSERT(HDR_HAS_L1HDR(hdr));
1407 
1408 	mutex_enter(&hdr->b_l1hdr.b_freeze_lock);
1409 	if (hdr->b_l1hdr.b_freeze_cksum != NULL) {
1410 		kmem_free(hdr->b_l1hdr.b_freeze_cksum, sizeof (zio_cksum_t));
1411 		hdr->b_l1hdr.b_freeze_cksum = NULL;
1412 	}
1413 	mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1414 #endif
1415 }
1416 
1417 /*
1418  * Return true iff at least one of the bufs on hdr is not compressed.
1419  * Encrypted buffers count as compressed.
1420  */
1421 static boolean_t
arc_hdr_has_uncompressed_buf(arc_buf_hdr_t * hdr)1422 arc_hdr_has_uncompressed_buf(arc_buf_hdr_t *hdr)
1423 {
1424 	ASSERT(hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY_OR_LOCKED(hdr));
1425 
1426 	for (arc_buf_t *b = hdr->b_l1hdr.b_buf; b != NULL; b = b->b_next) {
1427 		if (!ARC_BUF_COMPRESSED(b)) {
1428 			return (B_TRUE);
1429 		}
1430 	}
1431 	return (B_FALSE);
1432 }
1433 
1434 
1435 /*
1436  * If we've turned on the ZFS_DEBUG_MODIFY flag, verify that the buf's data
1437  * matches the checksum that is stored in the hdr. If there is no checksum,
1438  * or if the buf is compressed, this is a no-op.
1439  */
1440 static void
arc_cksum_verify(arc_buf_t * buf)1441 arc_cksum_verify(arc_buf_t *buf)
1442 {
1443 #ifdef ZFS_DEBUG
1444 	arc_buf_hdr_t *hdr = buf->b_hdr;
1445 	zio_cksum_t zc;
1446 
1447 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1448 		return;
1449 
1450 	if (ARC_BUF_COMPRESSED(buf))
1451 		return;
1452 
1453 	ASSERT(HDR_HAS_L1HDR(hdr));
1454 
1455 	mutex_enter(&hdr->b_l1hdr.b_freeze_lock);
1456 
1457 	if (hdr->b_l1hdr.b_freeze_cksum == NULL || HDR_IO_ERROR(hdr)) {
1458 		mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1459 		return;
1460 	}
1461 
1462 	fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL, &zc);
1463 	if (!ZIO_CHECKSUM_EQUAL(*hdr->b_l1hdr.b_freeze_cksum, zc))
1464 		panic("buffer modified while frozen!");
1465 	mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1466 #endif
1467 }
1468 
1469 /*
1470  * This function makes the assumption that data stored in the L2ARC
1471  * will be transformed exactly as it is in the main pool. Because of
1472  * this we can verify the checksum against the reading process's bp.
1473  */
1474 static boolean_t
arc_cksum_is_equal(arc_buf_hdr_t * hdr,zio_t * zio)1475 arc_cksum_is_equal(arc_buf_hdr_t *hdr, zio_t *zio)
1476 {
1477 	ASSERT(!BP_IS_EMBEDDED(zio->io_bp));
1478 	VERIFY3U(BP_GET_PSIZE(zio->io_bp), ==, HDR_GET_PSIZE(hdr));
1479 
1480 	/*
1481 	 * Block pointers always store the checksum for the logical data.
1482 	 * If the block pointer has the gang bit set, then the checksum
1483 	 * it represents is for the reconstituted data and not for an
1484 	 * individual gang member. The zio pipeline, however, must be able to
1485 	 * determine the checksum of each of the gang constituents so it
1486 	 * treats the checksum comparison differently than what we need
1487 	 * for l2arc blocks. This prevents us from using the
1488 	 * zio_checksum_error() interface directly. Instead we must call the
1489 	 * zio_checksum_error_impl() so that we can ensure the checksum is
1490 	 * generated using the correct checksum algorithm and accounts for the
1491 	 * logical I/O size and not just a gang fragment.
1492 	 */
1493 	return (zio_checksum_error_impl(zio->io_spa, zio->io_bp,
1494 	    BP_GET_CHECKSUM(zio->io_bp), zio->io_abd, zio->io_size,
1495 	    zio->io_offset, NULL) == 0);
1496 }
1497 
1498 /*
1499  * Given a buf full of data, if ZFS_DEBUG_MODIFY is enabled this computes a
1500  * checksum and attaches it to the buf's hdr so that we can ensure that the buf
1501  * isn't modified later on. If buf is compressed or there is already a checksum
1502  * on the hdr, this is a no-op (we only checksum uncompressed bufs).
1503  */
1504 static void
arc_cksum_compute(arc_buf_t * buf)1505 arc_cksum_compute(arc_buf_t *buf)
1506 {
1507 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1508 		return;
1509 
1510 #ifdef ZFS_DEBUG
1511 	arc_buf_hdr_t *hdr = buf->b_hdr;
1512 	ASSERT(HDR_HAS_L1HDR(hdr));
1513 	mutex_enter(&hdr->b_l1hdr.b_freeze_lock);
1514 	if (hdr->b_l1hdr.b_freeze_cksum != NULL || ARC_BUF_COMPRESSED(buf)) {
1515 		mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1516 		return;
1517 	}
1518 
1519 	ASSERT(!ARC_BUF_ENCRYPTED(buf));
1520 	ASSERT(!ARC_BUF_COMPRESSED(buf));
1521 	hdr->b_l1hdr.b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t),
1522 	    KM_SLEEP);
1523 	fletcher_2_native(buf->b_data, arc_buf_size(buf), NULL,
1524 	    hdr->b_l1hdr.b_freeze_cksum);
1525 	mutex_exit(&hdr->b_l1hdr.b_freeze_lock);
1526 #endif
1527 	arc_buf_watch(buf);
1528 }
1529 
1530 #ifndef _KERNEL
1531 void
arc_buf_sigsegv(int sig,siginfo_t * si,void * unused)1532 arc_buf_sigsegv(int sig, siginfo_t *si, void *unused)
1533 {
1534 	(void) sig, (void) unused;
1535 	panic("Got SIGSEGV at address: 0x%lx\n", (long)si->si_addr);
1536 }
1537 #endif
1538 
1539 static void
arc_buf_unwatch(arc_buf_t * buf)1540 arc_buf_unwatch(arc_buf_t *buf)
1541 {
1542 #ifndef _KERNEL
1543 	if (arc_watch) {
1544 		ASSERT0(mprotect(buf->b_data, arc_buf_size(buf),
1545 		    PROT_READ | PROT_WRITE));
1546 	}
1547 #else
1548 	(void) buf;
1549 #endif
1550 }
1551 
1552 static void
arc_buf_watch(arc_buf_t * buf)1553 arc_buf_watch(arc_buf_t *buf)
1554 {
1555 #ifndef _KERNEL
1556 	if (arc_watch)
1557 		ASSERT0(mprotect(buf->b_data, arc_buf_size(buf),
1558 		    PROT_READ));
1559 #else
1560 	(void) buf;
1561 #endif
1562 }
1563 
1564 static arc_buf_contents_t
arc_buf_type(arc_buf_hdr_t * hdr)1565 arc_buf_type(arc_buf_hdr_t *hdr)
1566 {
1567 	arc_buf_contents_t type;
1568 	if (HDR_ISTYPE_METADATA(hdr)) {
1569 		type = ARC_BUFC_METADATA;
1570 	} else {
1571 		type = ARC_BUFC_DATA;
1572 	}
1573 	VERIFY3U(hdr->b_type, ==, type);
1574 	return (type);
1575 }
1576 
1577 boolean_t
arc_is_metadata(arc_buf_t * buf)1578 arc_is_metadata(arc_buf_t *buf)
1579 {
1580 	return (HDR_ISTYPE_METADATA(buf->b_hdr) != 0);
1581 }
1582 
1583 static uint32_t
arc_bufc_to_flags(arc_buf_contents_t type)1584 arc_bufc_to_flags(arc_buf_contents_t type)
1585 {
1586 	switch (type) {
1587 	case ARC_BUFC_DATA:
1588 		/* metadata field is 0 if buffer contains normal data */
1589 		return (0);
1590 	case ARC_BUFC_METADATA:
1591 		return (ARC_FLAG_BUFC_METADATA);
1592 	default:
1593 		break;
1594 	}
1595 	panic("undefined ARC buffer type!");
1596 	return ((uint32_t)-1);
1597 }
1598 
1599 void
arc_buf_thaw(arc_buf_t * buf)1600 arc_buf_thaw(arc_buf_t *buf)
1601 {
1602 	arc_buf_hdr_t *hdr = buf->b_hdr;
1603 
1604 	ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
1605 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1606 
1607 	arc_cksum_verify(buf);
1608 
1609 	/*
1610 	 * Compressed buffers do not manipulate the b_freeze_cksum.
1611 	 */
1612 	if (ARC_BUF_COMPRESSED(buf))
1613 		return;
1614 
1615 	ASSERT(HDR_HAS_L1HDR(hdr));
1616 	arc_cksum_free(hdr);
1617 	arc_buf_unwatch(buf);
1618 }
1619 
1620 void
arc_buf_freeze(arc_buf_t * buf)1621 arc_buf_freeze(arc_buf_t *buf)
1622 {
1623 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1624 		return;
1625 
1626 	if (ARC_BUF_COMPRESSED(buf))
1627 		return;
1628 
1629 	ASSERT(HDR_HAS_L1HDR(buf->b_hdr));
1630 	arc_cksum_compute(buf);
1631 }
1632 
1633 /*
1634  * The arc_buf_hdr_t's b_flags should never be modified directly. Instead,
1635  * the following functions should be used to ensure that the flags are
1636  * updated in a thread-safe way. When manipulating the flags either
1637  * the hash_lock must be held or the hdr must be undiscoverable. This
1638  * ensures that we're not racing with any other threads when updating
1639  * the flags.
1640  */
1641 static inline void
arc_hdr_set_flags(arc_buf_hdr_t * hdr,arc_flags_t flags)1642 arc_hdr_set_flags(arc_buf_hdr_t *hdr, arc_flags_t flags)
1643 {
1644 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1645 	hdr->b_flags |= flags;
1646 }
1647 
1648 static inline void
arc_hdr_clear_flags(arc_buf_hdr_t * hdr,arc_flags_t flags)1649 arc_hdr_clear_flags(arc_buf_hdr_t *hdr, arc_flags_t flags)
1650 {
1651 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1652 	hdr->b_flags &= ~flags;
1653 }
1654 
1655 /*
1656  * Setting the compression bits in the arc_buf_hdr_t's b_flags is
1657  * done in a special way since we have to clear and set bits
1658  * at the same time. Consumers that wish to set the compression bits
1659  * must use this function to ensure that the flags are updated in
1660  * thread-safe manner.
1661  */
1662 static void
arc_hdr_set_compress(arc_buf_hdr_t * hdr,enum zio_compress cmp)1663 arc_hdr_set_compress(arc_buf_hdr_t *hdr, enum zio_compress cmp)
1664 {
1665 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1666 
1667 	/*
1668 	 * Holes and embedded blocks will always have a psize = 0 so
1669 	 * we ignore the compression of the blkptr and set the
1670 	 * want to uncompress them. Mark them as uncompressed.
1671 	 */
1672 	if (!zfs_compressed_arc_enabled || HDR_GET_PSIZE(hdr) == 0) {
1673 		arc_hdr_clear_flags(hdr, ARC_FLAG_COMPRESSED_ARC);
1674 		ASSERT(!HDR_COMPRESSION_ENABLED(hdr));
1675 	} else {
1676 		arc_hdr_set_flags(hdr, ARC_FLAG_COMPRESSED_ARC);
1677 		ASSERT(HDR_COMPRESSION_ENABLED(hdr));
1678 	}
1679 
1680 	HDR_SET_COMPRESS(hdr, cmp);
1681 	ASSERT3U(HDR_GET_COMPRESS(hdr), ==, cmp);
1682 }
1683 
1684 /*
1685  * Looks for another buf on the same hdr which has the data decompressed, copies
1686  * from it, and returns true. If no such buf exists, returns false.
1687  */
1688 static boolean_t
arc_buf_try_copy_decompressed_data(arc_buf_t * buf)1689 arc_buf_try_copy_decompressed_data(arc_buf_t *buf)
1690 {
1691 	arc_buf_hdr_t *hdr = buf->b_hdr;
1692 	boolean_t copied = B_FALSE;
1693 
1694 	ASSERT(HDR_HAS_L1HDR(hdr));
1695 	ASSERT3P(buf->b_data, !=, NULL);
1696 	ASSERT(!ARC_BUF_COMPRESSED(buf));
1697 
1698 	for (arc_buf_t *from = hdr->b_l1hdr.b_buf; from != NULL;
1699 	    from = from->b_next) {
1700 		/* can't use our own data buffer */
1701 		if (from == buf) {
1702 			continue;
1703 		}
1704 
1705 		if (!ARC_BUF_COMPRESSED(from)) {
1706 			memcpy(buf->b_data, from->b_data, arc_buf_size(buf));
1707 			copied = B_TRUE;
1708 			break;
1709 		}
1710 	}
1711 
1712 #ifdef ZFS_DEBUG
1713 	/*
1714 	 * There were no decompressed bufs, so there should not be a
1715 	 * checksum on the hdr either.
1716 	 */
1717 	if (zfs_flags & ZFS_DEBUG_MODIFY)
1718 		EQUIV(!copied, hdr->b_l1hdr.b_freeze_cksum == NULL);
1719 #endif
1720 
1721 	return (copied);
1722 }
1723 
1724 /*
1725  * Allocates an ARC buf header that's in an evicted & L2-cached state.
1726  * This is used during l2arc reconstruction to make empty ARC buffers
1727  * which circumvent the regular disk->arc->l2arc path and instead come
1728  * into being in the reverse order, i.e. l2arc->arc.
1729  */
1730 static arc_buf_hdr_t *
arc_buf_alloc_l2only(size_t size,arc_buf_contents_t type,l2arc_dev_t * dev,dva_t dva,uint64_t daddr,int32_t psize,uint64_t asize,uint64_t birth,enum zio_compress compress,uint8_t complevel,boolean_t protected,boolean_t prefetch,arc_state_type_t arcs_state)1731 arc_buf_alloc_l2only(size_t size, arc_buf_contents_t type, l2arc_dev_t *dev,
1732     dva_t dva, uint64_t daddr, int32_t psize, uint64_t asize, uint64_t birth,
1733     enum zio_compress compress, uint8_t complevel, boolean_t protected,
1734     boolean_t prefetch, arc_state_type_t arcs_state)
1735 {
1736 	arc_buf_hdr_t	*hdr;
1737 
1738 	ASSERT(size != 0);
1739 	ASSERT(dev->l2ad_vdev != NULL);
1740 
1741 	hdr = kmem_cache_alloc(hdr_l2only_cache, KM_SLEEP);
1742 	hdr->b_birth = birth;
1743 	hdr->b_type = type;
1744 	hdr->b_flags = 0;
1745 	arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L2HDR);
1746 	HDR_SET_LSIZE(hdr, size);
1747 	HDR_SET_PSIZE(hdr, psize);
1748 	HDR_SET_L2SIZE(hdr, asize);
1749 	arc_hdr_set_compress(hdr, compress);
1750 	hdr->b_complevel = complevel;
1751 	if (protected)
1752 		arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED);
1753 	if (prefetch)
1754 		arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH);
1755 	hdr->b_spa = spa_load_guid(dev->l2ad_vdev->vdev_spa);
1756 
1757 	hdr->b_dva = dva;
1758 
1759 	hdr->b_l2hdr.b_dev = dev;
1760 	hdr->b_l2hdr.b_daddr = daddr;
1761 	hdr->b_l2hdr.b_arcs_state = arcs_state;
1762 
1763 	return (hdr);
1764 }
1765 
1766 /*
1767  * Return the size of the block, b_pabd, that is stored in the arc_buf_hdr_t.
1768  */
1769 static uint64_t
arc_hdr_size(arc_buf_hdr_t * hdr)1770 arc_hdr_size(arc_buf_hdr_t *hdr)
1771 {
1772 	uint64_t size;
1773 
1774 	if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF &&
1775 	    HDR_GET_PSIZE(hdr) > 0) {
1776 		size = HDR_GET_PSIZE(hdr);
1777 	} else {
1778 		ASSERT3U(HDR_GET_LSIZE(hdr), !=, 0);
1779 		size = HDR_GET_LSIZE(hdr);
1780 	}
1781 	return (size);
1782 }
1783 
1784 static int
arc_hdr_authenticate(arc_buf_hdr_t * hdr,spa_t * spa,uint64_t dsobj)1785 arc_hdr_authenticate(arc_buf_hdr_t *hdr, spa_t *spa, uint64_t dsobj)
1786 {
1787 	int ret;
1788 	uint64_t csize;
1789 	uint64_t lsize = HDR_GET_LSIZE(hdr);
1790 	uint64_t psize = HDR_GET_PSIZE(hdr);
1791 	abd_t *abd = hdr->b_l1hdr.b_pabd;
1792 	boolean_t free_abd = B_FALSE;
1793 
1794 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1795 	ASSERT(HDR_AUTHENTICATED(hdr));
1796 	ASSERT3P(abd, !=, NULL);
1797 
1798 	/*
1799 	 * The MAC is calculated on the compressed data that is stored on disk.
1800 	 * However, if compressed arc is disabled we will only have the
1801 	 * decompressed data available to us now. Compress it into a temporary
1802 	 * abd so we can verify the MAC. The performance overhead of this will
1803 	 * be relatively low, since most objects in an encrypted objset will
1804 	 * be encrypted (instead of authenticated) anyway.
1805 	 */
1806 	if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
1807 	    !HDR_COMPRESSION_ENABLED(hdr)) {
1808 		abd = NULL;
1809 		csize = zio_compress_data(HDR_GET_COMPRESS(hdr),
1810 		    hdr->b_l1hdr.b_pabd, &abd, lsize, MIN(lsize, psize),
1811 		    hdr->b_complevel);
1812 		if (csize >= lsize || csize > psize) {
1813 			ret = SET_ERROR(EIO);
1814 			return (ret);
1815 		}
1816 		ASSERT3P(abd, !=, NULL);
1817 		abd_zero_off(abd, csize, psize - csize);
1818 		free_abd = B_TRUE;
1819 	}
1820 
1821 	/*
1822 	 * Authentication is best effort. We authenticate whenever the key is
1823 	 * available. If we succeed we clear ARC_FLAG_NOAUTH.
1824 	 */
1825 	if (hdr->b_crypt_hdr.b_ot == DMU_OT_OBJSET) {
1826 		ASSERT3U(HDR_GET_COMPRESS(hdr), ==, ZIO_COMPRESS_OFF);
1827 		ASSERT3U(lsize, ==, psize);
1828 		ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa, dsobj, abd,
1829 		    psize, hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS);
1830 	} else {
1831 		ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj, abd, psize,
1832 		    hdr->b_crypt_hdr.b_mac);
1833 	}
1834 
1835 	if (ret == 0)
1836 		arc_hdr_clear_flags(hdr, ARC_FLAG_NOAUTH);
1837 	else if (ret == ENOENT)
1838 		ret = 0;
1839 
1840 	if (free_abd)
1841 		abd_free(abd);
1842 
1843 	return (ret);
1844 }
1845 
1846 /*
1847  * This function will take a header that only has raw encrypted data in
1848  * b_crypt_hdr.b_rabd and decrypt it into a new buffer which is stored in
1849  * b_l1hdr.b_pabd. If designated in the header flags, this function will
1850  * also decompress the data.
1851  */
1852 static int
arc_hdr_decrypt(arc_buf_hdr_t * hdr,spa_t * spa,const zbookmark_phys_t * zb)1853 arc_hdr_decrypt(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb)
1854 {
1855 	int ret;
1856 	abd_t *cabd = NULL;
1857 	boolean_t no_crypt = B_FALSE;
1858 	boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS);
1859 
1860 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1861 	ASSERT(HDR_ENCRYPTED(hdr));
1862 
1863 	arc_hdr_alloc_abd(hdr, 0);
1864 
1865 	ret = spa_do_crypt_abd(B_FALSE, spa, zb, hdr->b_crypt_hdr.b_ot,
1866 	    B_FALSE, bswap, hdr->b_crypt_hdr.b_salt, hdr->b_crypt_hdr.b_iv,
1867 	    hdr->b_crypt_hdr.b_mac, HDR_GET_PSIZE(hdr), hdr->b_l1hdr.b_pabd,
1868 	    hdr->b_crypt_hdr.b_rabd, &no_crypt);
1869 	if (ret != 0)
1870 		goto error;
1871 
1872 	if (no_crypt) {
1873 		abd_copy(hdr->b_l1hdr.b_pabd, hdr->b_crypt_hdr.b_rabd,
1874 		    HDR_GET_PSIZE(hdr));
1875 	}
1876 
1877 	/*
1878 	 * If this header has disabled arc compression but the b_pabd is
1879 	 * compressed after decrypting it, we need to decompress the newly
1880 	 * decrypted data.
1881 	 */
1882 	if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
1883 	    !HDR_COMPRESSION_ENABLED(hdr)) {
1884 		/*
1885 		 * We want to make sure that we are correctly honoring the
1886 		 * zfs_abd_scatter_enabled setting, so we allocate an abd here
1887 		 * and then loan a buffer from it, rather than allocating a
1888 		 * linear buffer and wrapping it in an abd later.
1889 		 */
1890 		cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr, 0);
1891 
1892 		ret = zio_decompress_data(HDR_GET_COMPRESS(hdr),
1893 		    hdr->b_l1hdr.b_pabd, cabd, HDR_GET_PSIZE(hdr),
1894 		    HDR_GET_LSIZE(hdr), &hdr->b_complevel);
1895 		if (ret != 0) {
1896 			goto error;
1897 		}
1898 
1899 		arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd,
1900 		    arc_hdr_size(hdr), hdr);
1901 		hdr->b_l1hdr.b_pabd = cabd;
1902 	}
1903 
1904 	return (0);
1905 
1906 error:
1907 	arc_hdr_free_abd(hdr, B_FALSE);
1908 	if (cabd != NULL)
1909 		arc_free_data_abd(hdr, cabd, arc_hdr_size(hdr), hdr);
1910 
1911 	return (ret);
1912 }
1913 
1914 /*
1915  * This function is called during arc_buf_fill() to prepare the header's
1916  * abd plaintext pointer for use. This involves authenticated protected
1917  * data and decrypting encrypted data into the plaintext abd.
1918  */
1919 static int
arc_fill_hdr_crypt(arc_buf_hdr_t * hdr,kmutex_t * hash_lock,spa_t * spa,const zbookmark_phys_t * zb,boolean_t noauth)1920 arc_fill_hdr_crypt(arc_buf_hdr_t *hdr, kmutex_t *hash_lock, spa_t *spa,
1921     const zbookmark_phys_t *zb, boolean_t noauth)
1922 {
1923 	int ret;
1924 
1925 	ASSERT(HDR_PROTECTED(hdr));
1926 
1927 	if (hash_lock != NULL)
1928 		mutex_enter(hash_lock);
1929 
1930 	if (HDR_NOAUTH(hdr) && !noauth) {
1931 		/*
1932 		 * The caller requested authenticated data but our data has
1933 		 * not been authenticated yet. Verify the MAC now if we can.
1934 		 */
1935 		ret = arc_hdr_authenticate(hdr, spa, zb->zb_objset);
1936 		if (ret != 0)
1937 			goto error;
1938 	} else if (HDR_HAS_RABD(hdr) && hdr->b_l1hdr.b_pabd == NULL) {
1939 		/*
1940 		 * If we only have the encrypted version of the data, but the
1941 		 * unencrypted version was requested we take this opportunity
1942 		 * to store the decrypted version in the header for future use.
1943 		 */
1944 		ret = arc_hdr_decrypt(hdr, spa, zb);
1945 		if (ret != 0)
1946 			goto error;
1947 	}
1948 
1949 	ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
1950 
1951 	if (hash_lock != NULL)
1952 		mutex_exit(hash_lock);
1953 
1954 	return (0);
1955 
1956 error:
1957 	if (hash_lock != NULL)
1958 		mutex_exit(hash_lock);
1959 
1960 	return (ret);
1961 }
1962 
1963 /*
1964  * This function is used by the dbuf code to decrypt bonus buffers in place.
1965  * The dbuf code itself doesn't have any locking for decrypting a shared dnode
1966  * block, so we use the hash lock here to protect against concurrent calls to
1967  * arc_buf_fill().
1968  */
1969 static void
arc_buf_untransform_in_place(arc_buf_t * buf)1970 arc_buf_untransform_in_place(arc_buf_t *buf)
1971 {
1972 	arc_buf_hdr_t *hdr = buf->b_hdr;
1973 
1974 	ASSERT(HDR_ENCRYPTED(hdr));
1975 	ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE);
1976 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
1977 	ASSERT3PF(hdr->b_l1hdr.b_pabd, !=, NULL, "hdr %px buf %px", hdr, buf);
1978 
1979 	zio_crypt_copy_dnode_bonus(hdr->b_l1hdr.b_pabd, buf->b_data,
1980 	    arc_buf_size(buf));
1981 	buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED;
1982 	buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED;
1983 }
1984 
1985 /*
1986  * Given a buf that has a data buffer attached to it, this function will
1987  * efficiently fill the buf with data of the specified compression setting from
1988  * the hdr and update the hdr's b_freeze_cksum if necessary. If the buf and hdr
1989  * are already sharing a data buf, no copy is performed.
1990  *
1991  * If the buf is marked as compressed but uncompressed data was requested, this
1992  * will allocate a new data buffer for the buf, remove that flag, and fill the
1993  * buf with uncompressed data. You can't request a compressed buf on a hdr with
1994  * uncompressed data, and (since we haven't added support for it yet) if you
1995  * want compressed data your buf must already be marked as compressed and have
1996  * the correct-sized data buffer.
1997  */
1998 static int
arc_buf_fill(arc_buf_t * buf,spa_t * spa,const zbookmark_phys_t * zb,arc_fill_flags_t flags)1999 arc_buf_fill(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb,
2000     arc_fill_flags_t flags)
2001 {
2002 	int error = 0;
2003 	arc_buf_hdr_t *hdr = buf->b_hdr;
2004 	boolean_t hdr_compressed =
2005 	    (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF);
2006 	boolean_t compressed = (flags & ARC_FILL_COMPRESSED) != 0;
2007 	boolean_t encrypted = (flags & ARC_FILL_ENCRYPTED) != 0;
2008 	dmu_object_byteswap_t bswap = hdr->b_l1hdr.b_byteswap;
2009 	kmutex_t *hash_lock = (flags & ARC_FILL_LOCKED) ? NULL : HDR_LOCK(hdr);
2010 
2011 	ASSERT3P(buf->b_data, !=, NULL);
2012 	IMPLY(compressed, hdr_compressed || ARC_BUF_ENCRYPTED(buf));
2013 	IMPLY(compressed, ARC_BUF_COMPRESSED(buf));
2014 	IMPLY(encrypted, HDR_ENCRYPTED(hdr));
2015 	IMPLY(encrypted, ARC_BUF_ENCRYPTED(buf));
2016 	IMPLY(encrypted, ARC_BUF_COMPRESSED(buf));
2017 	IMPLY(encrypted, !arc_buf_is_shared(buf));
2018 
2019 	/*
2020 	 * If the caller wanted encrypted data we just need to copy it from
2021 	 * b_rabd and potentially byteswap it. We won't be able to do any
2022 	 * further transforms on it.
2023 	 */
2024 	if (encrypted) {
2025 		ASSERT(HDR_HAS_RABD(hdr));
2026 		abd_copy_to_buf(buf->b_data, hdr->b_crypt_hdr.b_rabd,
2027 		    HDR_GET_PSIZE(hdr));
2028 		goto byteswap;
2029 	}
2030 
2031 	/*
2032 	 * Adjust encrypted and authenticated headers to accommodate
2033 	 * the request if needed. Dnode blocks (ARC_FILL_IN_PLACE) are
2034 	 * allowed to fail decryption due to keys not being loaded
2035 	 * without being marked as an IO error.
2036 	 */
2037 	if (HDR_PROTECTED(hdr)) {
2038 		error = arc_fill_hdr_crypt(hdr, hash_lock, spa,
2039 		    zb, !!(flags & ARC_FILL_NOAUTH));
2040 		if (error == EACCES && (flags & ARC_FILL_IN_PLACE) != 0) {
2041 			return (error);
2042 		} else if (error != 0) {
2043 			if (hash_lock != NULL)
2044 				mutex_enter(hash_lock);
2045 			arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR);
2046 			if (hash_lock != NULL)
2047 				mutex_exit(hash_lock);
2048 			return (error);
2049 		}
2050 	}
2051 
2052 	/*
2053 	 * There is a special case here for dnode blocks which are
2054 	 * decrypting their bonus buffers. These blocks may request to
2055 	 * be decrypted in-place. This is necessary because there may
2056 	 * be many dnodes pointing into this buffer and there is
2057 	 * currently no method to synchronize replacing the backing
2058 	 * b_data buffer and updating all of the pointers. Here we use
2059 	 * the hash lock to ensure there are no races. If the need
2060 	 * arises for other types to be decrypted in-place, they must
2061 	 * add handling here as well.
2062 	 */
2063 	if ((flags & ARC_FILL_IN_PLACE) != 0) {
2064 		ASSERT(!hdr_compressed);
2065 		ASSERT(!compressed);
2066 		ASSERT(!encrypted);
2067 
2068 		if (HDR_ENCRYPTED(hdr) && ARC_BUF_ENCRYPTED(buf)) {
2069 			ASSERT3U(hdr->b_crypt_hdr.b_ot, ==, DMU_OT_DNODE);
2070 
2071 			if (hash_lock != NULL)
2072 				mutex_enter(hash_lock);
2073 			arc_buf_untransform_in_place(buf);
2074 			if (hash_lock != NULL)
2075 				mutex_exit(hash_lock);
2076 
2077 			/* Compute the hdr's checksum if necessary */
2078 			arc_cksum_compute(buf);
2079 		}
2080 
2081 		return (0);
2082 	}
2083 
2084 	if (hdr_compressed == compressed) {
2085 		if (ARC_BUF_SHARED(buf)) {
2086 			ASSERT(arc_buf_is_shared(buf));
2087 		} else {
2088 			abd_copy_to_buf(buf->b_data, hdr->b_l1hdr.b_pabd,
2089 			    arc_buf_size(buf));
2090 		}
2091 	} else {
2092 		ASSERT(hdr_compressed);
2093 		ASSERT(!compressed);
2094 
2095 		/*
2096 		 * If the buf is sharing its data with the hdr, unlink it and
2097 		 * allocate a new data buffer for the buf.
2098 		 */
2099 		if (ARC_BUF_SHARED(buf)) {
2100 			ASSERTF(ARC_BUF_COMPRESSED(buf),
2101 			"buf %p was uncompressed", buf);
2102 
2103 			/* We need to give the buf its own b_data */
2104 			buf->b_flags &= ~ARC_BUF_FLAG_SHARED;
2105 			buf->b_data =
2106 			    arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf);
2107 			arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA);
2108 
2109 			/* Previously overhead was 0; just add new overhead */
2110 			ARCSTAT_INCR(arcstat_overhead_size, HDR_GET_LSIZE(hdr));
2111 		} else if (ARC_BUF_COMPRESSED(buf)) {
2112 			ASSERT(!arc_buf_is_shared(buf));
2113 
2114 			/* We need to reallocate the buf's b_data */
2115 			arc_free_data_buf(hdr, buf->b_data, HDR_GET_PSIZE(hdr),
2116 			    buf);
2117 			buf->b_data =
2118 			    arc_get_data_buf(hdr, HDR_GET_LSIZE(hdr), buf);
2119 
2120 			/* We increased the size of b_data; update overhead */
2121 			ARCSTAT_INCR(arcstat_overhead_size,
2122 			    HDR_GET_LSIZE(hdr) - HDR_GET_PSIZE(hdr));
2123 		}
2124 
2125 		/*
2126 		 * Regardless of the buf's previous compression settings, it
2127 		 * should not be compressed at the end of this function.
2128 		 */
2129 		buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED;
2130 
2131 		/*
2132 		 * Try copying the data from another buf which already has a
2133 		 * decompressed version. If that's not possible, it's time to
2134 		 * bite the bullet and decompress the data from the hdr.
2135 		 */
2136 		if (arc_buf_try_copy_decompressed_data(buf)) {
2137 			/* Skip byteswapping and checksumming (already done) */
2138 			return (0);
2139 		} else {
2140 			abd_t dabd;
2141 			abd_get_from_buf_struct(&dabd, buf->b_data,
2142 			    HDR_GET_LSIZE(hdr));
2143 			error = zio_decompress_data(HDR_GET_COMPRESS(hdr),
2144 			    hdr->b_l1hdr.b_pabd, &dabd,
2145 			    HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr),
2146 			    &hdr->b_complevel);
2147 			abd_free(&dabd);
2148 
2149 			/*
2150 			 * Absent hardware errors or software bugs, this should
2151 			 * be impossible, but log it anyway so we can debug it.
2152 			 */
2153 			if (error != 0) {
2154 				zfs_dbgmsg(
2155 				    "hdr %px, compress %d, psize %d, lsize %d",
2156 				    hdr, arc_hdr_get_compress(hdr),
2157 				    HDR_GET_PSIZE(hdr), HDR_GET_LSIZE(hdr));
2158 				if (hash_lock != NULL)
2159 					mutex_enter(hash_lock);
2160 				arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR);
2161 				if (hash_lock != NULL)
2162 					mutex_exit(hash_lock);
2163 				return (SET_ERROR(EIO));
2164 			}
2165 		}
2166 	}
2167 
2168 byteswap:
2169 	/* Byteswap the buf's data if necessary */
2170 	if (bswap != DMU_BSWAP_NUMFUNCS) {
2171 		ASSERT(!HDR_SHARED_DATA(hdr));
2172 		ASSERT3U(bswap, <, DMU_BSWAP_NUMFUNCS);
2173 		dmu_ot_byteswap[bswap].ob_func(buf->b_data, HDR_GET_LSIZE(hdr));
2174 	}
2175 
2176 	/* Compute the hdr's checksum if necessary */
2177 	arc_cksum_compute(buf);
2178 
2179 	return (0);
2180 }
2181 
2182 /*
2183  * If this function is being called to decrypt an encrypted buffer or verify an
2184  * authenticated one, the key must be loaded and a mapping must be made
2185  * available in the keystore via spa_keystore_create_mapping() or one of its
2186  * callers.
2187  */
2188 int
arc_untransform(arc_buf_t * buf,spa_t * spa,const zbookmark_phys_t * zb,boolean_t in_place)2189 arc_untransform(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb,
2190     boolean_t in_place)
2191 {
2192 	int ret;
2193 	arc_fill_flags_t flags = 0;
2194 
2195 	if (in_place)
2196 		flags |= ARC_FILL_IN_PLACE;
2197 
2198 	ret = arc_buf_fill(buf, spa, zb, flags);
2199 	if (ret == ECKSUM) {
2200 		/*
2201 		 * Convert authentication and decryption errors to EIO
2202 		 * (and generate an ereport) before leaving the ARC.
2203 		 */
2204 		ret = SET_ERROR(EIO);
2205 		spa_log_error(spa, zb, buf->b_hdr->b_birth);
2206 		(void) zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
2207 		    spa, NULL, zb, NULL, 0);
2208 	}
2209 
2210 	return (ret);
2211 }
2212 
2213 /*
2214  * Increment the amount of evictable space in the arc_state_t's refcount.
2215  * We account for the space used by the hdr and the arc buf individually
2216  * so that we can add and remove them from the refcount individually.
2217  */
2218 static void
arc_evictable_space_increment(arc_buf_hdr_t * hdr,arc_state_t * state)2219 arc_evictable_space_increment(arc_buf_hdr_t *hdr, arc_state_t *state)
2220 {
2221 	arc_buf_contents_t type = arc_buf_type(hdr);
2222 
2223 	ASSERT(HDR_HAS_L1HDR(hdr));
2224 
2225 	if (GHOST_STATE(state)) {
2226 		ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
2227 		ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2228 		ASSERT(!HDR_HAS_RABD(hdr));
2229 		(void) zfs_refcount_add_many(&state->arcs_esize[type],
2230 		    HDR_GET_LSIZE(hdr), hdr);
2231 		return;
2232 	}
2233 
2234 	if (hdr->b_l1hdr.b_pabd != NULL) {
2235 		(void) zfs_refcount_add_many(&state->arcs_esize[type],
2236 		    arc_hdr_size(hdr), hdr);
2237 	}
2238 	if (HDR_HAS_RABD(hdr)) {
2239 		(void) zfs_refcount_add_many(&state->arcs_esize[type],
2240 		    HDR_GET_PSIZE(hdr), hdr);
2241 	}
2242 
2243 	for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL;
2244 	    buf = buf->b_next) {
2245 		if (ARC_BUF_SHARED(buf))
2246 			continue;
2247 		(void) zfs_refcount_add_many(&state->arcs_esize[type],
2248 		    arc_buf_size(buf), buf);
2249 	}
2250 }
2251 
2252 /*
2253  * Decrement the amount of evictable space in the arc_state_t's refcount.
2254  * We account for the space used by the hdr and the arc buf individually
2255  * so that we can add and remove them from the refcount individually.
2256  */
2257 static void
arc_evictable_space_decrement(arc_buf_hdr_t * hdr,arc_state_t * state)2258 arc_evictable_space_decrement(arc_buf_hdr_t *hdr, arc_state_t *state)
2259 {
2260 	arc_buf_contents_t type = arc_buf_type(hdr);
2261 
2262 	ASSERT(HDR_HAS_L1HDR(hdr));
2263 
2264 	if (GHOST_STATE(state)) {
2265 		ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
2266 		ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2267 		ASSERT(!HDR_HAS_RABD(hdr));
2268 		(void) zfs_refcount_remove_many(&state->arcs_esize[type],
2269 		    HDR_GET_LSIZE(hdr), hdr);
2270 		return;
2271 	}
2272 
2273 	if (hdr->b_l1hdr.b_pabd != NULL) {
2274 		(void) zfs_refcount_remove_many(&state->arcs_esize[type],
2275 		    arc_hdr_size(hdr), hdr);
2276 	}
2277 	if (HDR_HAS_RABD(hdr)) {
2278 		(void) zfs_refcount_remove_many(&state->arcs_esize[type],
2279 		    HDR_GET_PSIZE(hdr), hdr);
2280 	}
2281 
2282 	for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL;
2283 	    buf = buf->b_next) {
2284 		if (ARC_BUF_SHARED(buf))
2285 			continue;
2286 		(void) zfs_refcount_remove_many(&state->arcs_esize[type],
2287 		    arc_buf_size(buf), buf);
2288 	}
2289 }
2290 
2291 /*
2292  * Add a reference to this hdr indicating that someone is actively
2293  * referencing that memory. When the refcount transitions from 0 to 1,
2294  * we remove it from the respective arc_state_t list to indicate that
2295  * it is not evictable.
2296  */
2297 static void
add_reference(arc_buf_hdr_t * hdr,const void * tag)2298 add_reference(arc_buf_hdr_t *hdr, const void *tag)
2299 {
2300 	arc_state_t *state = hdr->b_l1hdr.b_state;
2301 
2302 	ASSERT(HDR_HAS_L1HDR(hdr));
2303 	if (!HDR_EMPTY(hdr) && !MUTEX_HELD(HDR_LOCK(hdr))) {
2304 		ASSERT(state == arc_anon);
2305 		ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
2306 		ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
2307 	}
2308 
2309 	if ((zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag) == 1) &&
2310 	    state != arc_anon && state != arc_l2c_only) {
2311 		/* We don't use the L2-only state list. */
2312 		multilist_remove(&state->arcs_list[arc_buf_type(hdr)], hdr);
2313 		arc_evictable_space_decrement(hdr, state);
2314 	}
2315 }
2316 
2317 /*
2318  * Remove a reference from this hdr. When the reference transitions from
2319  * 1 to 0 and we're not anonymous, then we add this hdr to the arc_state_t's
2320  * list making it eligible for eviction.
2321  */
2322 static int
remove_reference(arc_buf_hdr_t * hdr,const void * tag)2323 remove_reference(arc_buf_hdr_t *hdr, const void *tag)
2324 {
2325 	int cnt;
2326 	arc_state_t *state = hdr->b_l1hdr.b_state;
2327 
2328 	ASSERT(HDR_HAS_L1HDR(hdr));
2329 	ASSERT(state == arc_anon || MUTEX_HELD(HDR_LOCK(hdr)));
2330 	ASSERT(!GHOST_STATE(state));	/* arc_l2c_only counts as a ghost. */
2331 
2332 	if ((cnt = zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag)) != 0)
2333 		return (cnt);
2334 
2335 	if (state == arc_anon) {
2336 		arc_hdr_destroy(hdr);
2337 		return (0);
2338 	}
2339 	if (state == arc_uncached && !HDR_PREFETCH(hdr)) {
2340 		arc_change_state(arc_anon, hdr);
2341 		arc_hdr_destroy(hdr);
2342 		return (0);
2343 	}
2344 	multilist_insert(&state->arcs_list[arc_buf_type(hdr)], hdr);
2345 	arc_evictable_space_increment(hdr, state);
2346 	return (0);
2347 }
2348 
2349 /*
2350  * Returns detailed information about a specific arc buffer.  When the
2351  * state_index argument is set the function will calculate the arc header
2352  * list position for its arc state.  Since this requires a linear traversal
2353  * callers are strongly encourage not to do this.  However, it can be helpful
2354  * for targeted analysis so the functionality is provided.
2355  */
2356 void
arc_buf_info(arc_buf_t * ab,arc_buf_info_t * abi,int state_index)2357 arc_buf_info(arc_buf_t *ab, arc_buf_info_t *abi, int state_index)
2358 {
2359 	(void) state_index;
2360 	arc_buf_hdr_t *hdr = ab->b_hdr;
2361 	l1arc_buf_hdr_t *l1hdr = NULL;
2362 	l2arc_buf_hdr_t *l2hdr = NULL;
2363 	arc_state_t *state = NULL;
2364 
2365 	memset(abi, 0, sizeof (arc_buf_info_t));
2366 
2367 	if (hdr == NULL)
2368 		return;
2369 
2370 	abi->abi_flags = hdr->b_flags;
2371 
2372 	if (HDR_HAS_L1HDR(hdr)) {
2373 		l1hdr = &hdr->b_l1hdr;
2374 		state = l1hdr->b_state;
2375 	}
2376 	if (HDR_HAS_L2HDR(hdr))
2377 		l2hdr = &hdr->b_l2hdr;
2378 
2379 	if (l1hdr) {
2380 		abi->abi_bufcnt = 0;
2381 		for (arc_buf_t *buf = l1hdr->b_buf; buf; buf = buf->b_next)
2382 			abi->abi_bufcnt++;
2383 		abi->abi_access = l1hdr->b_arc_access;
2384 		abi->abi_mru_hits = l1hdr->b_mru_hits;
2385 		abi->abi_mru_ghost_hits = l1hdr->b_mru_ghost_hits;
2386 		abi->abi_mfu_hits = l1hdr->b_mfu_hits;
2387 		abi->abi_mfu_ghost_hits = l1hdr->b_mfu_ghost_hits;
2388 		abi->abi_holds = zfs_refcount_count(&l1hdr->b_refcnt);
2389 	}
2390 
2391 	if (l2hdr) {
2392 		abi->abi_l2arc_dattr = l2hdr->b_daddr;
2393 		abi->abi_l2arc_hits = l2hdr->b_hits;
2394 	}
2395 
2396 	abi->abi_state_type = state ? state->arcs_state : ARC_STATE_ANON;
2397 	abi->abi_state_contents = arc_buf_type(hdr);
2398 	abi->abi_size = arc_hdr_size(hdr);
2399 }
2400 
2401 /*
2402  * Move the supplied buffer to the indicated state. The hash lock
2403  * for the buffer must be held by the caller.
2404  */
2405 static void
arc_change_state(arc_state_t * new_state,arc_buf_hdr_t * hdr)2406 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *hdr)
2407 {
2408 	arc_state_t *old_state;
2409 	int64_t refcnt;
2410 	boolean_t update_old, update_new;
2411 	arc_buf_contents_t type = arc_buf_type(hdr);
2412 
2413 	/*
2414 	 * We almost always have an L1 hdr here, since we call arc_hdr_realloc()
2415 	 * in arc_read() when bringing a buffer out of the L2ARC.  However, the
2416 	 * L1 hdr doesn't always exist when we change state to arc_anon before
2417 	 * destroying a header, in which case reallocating to add the L1 hdr is
2418 	 * pointless.
2419 	 */
2420 	if (HDR_HAS_L1HDR(hdr)) {
2421 		old_state = hdr->b_l1hdr.b_state;
2422 		refcnt = zfs_refcount_count(&hdr->b_l1hdr.b_refcnt);
2423 		update_old = (hdr->b_l1hdr.b_buf != NULL ||
2424 		    hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr));
2425 
2426 		IMPLY(GHOST_STATE(old_state), hdr->b_l1hdr.b_buf == NULL);
2427 		IMPLY(GHOST_STATE(new_state), hdr->b_l1hdr.b_buf == NULL);
2428 		IMPLY(old_state == arc_anon, hdr->b_l1hdr.b_buf == NULL ||
2429 		    ARC_BUF_LAST(hdr->b_l1hdr.b_buf));
2430 	} else {
2431 		old_state = arc_l2c_only;
2432 		refcnt = 0;
2433 		update_old = B_FALSE;
2434 	}
2435 	update_new = update_old;
2436 	if (GHOST_STATE(old_state))
2437 		update_old = B_TRUE;
2438 	if (GHOST_STATE(new_state))
2439 		update_new = B_TRUE;
2440 
2441 	ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
2442 	ASSERT3P(new_state, !=, old_state);
2443 
2444 	/*
2445 	 * If this buffer is evictable, transfer it from the
2446 	 * old state list to the new state list.
2447 	 */
2448 	if (refcnt == 0) {
2449 		if (old_state != arc_anon && old_state != arc_l2c_only) {
2450 			ASSERT(HDR_HAS_L1HDR(hdr));
2451 			/* remove_reference() saves on insert. */
2452 			if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
2453 				multilist_remove(&old_state->arcs_list[type],
2454 				    hdr);
2455 				arc_evictable_space_decrement(hdr, old_state);
2456 			}
2457 		}
2458 		if (new_state != arc_anon && new_state != arc_l2c_only) {
2459 			/*
2460 			 * An L1 header always exists here, since if we're
2461 			 * moving to some L1-cached state (i.e. not l2c_only or
2462 			 * anonymous), we realloc the header to add an L1hdr
2463 			 * beforehand.
2464 			 */
2465 			ASSERT(HDR_HAS_L1HDR(hdr));
2466 			multilist_insert(&new_state->arcs_list[type], hdr);
2467 			arc_evictable_space_increment(hdr, new_state);
2468 		}
2469 	}
2470 
2471 	ASSERT(!HDR_EMPTY(hdr));
2472 	if (new_state == arc_anon && HDR_IN_HASH_TABLE(hdr))
2473 		buf_hash_remove(hdr);
2474 
2475 	/* adjust state sizes (ignore arc_l2c_only) */
2476 
2477 	if (update_new && new_state != arc_l2c_only) {
2478 		ASSERT(HDR_HAS_L1HDR(hdr));
2479 		if (GHOST_STATE(new_state)) {
2480 
2481 			/*
2482 			 * When moving a header to a ghost state, we first
2483 			 * remove all arc buffers. Thus, we'll have no arc
2484 			 * buffer to use for the reference. As a result, we
2485 			 * use the arc header pointer for the reference.
2486 			 */
2487 			(void) zfs_refcount_add_many(
2488 			    &new_state->arcs_size[type],
2489 			    HDR_GET_LSIZE(hdr), hdr);
2490 			ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2491 			ASSERT(!HDR_HAS_RABD(hdr));
2492 		} else {
2493 
2494 			/*
2495 			 * Each individual buffer holds a unique reference,
2496 			 * thus we must remove each of these references one
2497 			 * at a time.
2498 			 */
2499 			for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL;
2500 			    buf = buf->b_next) {
2501 
2502 				/*
2503 				 * When the arc_buf_t is sharing the data
2504 				 * block with the hdr, the owner of the
2505 				 * reference belongs to the hdr. Only
2506 				 * add to the refcount if the arc_buf_t is
2507 				 * not shared.
2508 				 */
2509 				if (ARC_BUF_SHARED(buf))
2510 					continue;
2511 
2512 				(void) zfs_refcount_add_many(
2513 				    &new_state->arcs_size[type],
2514 				    arc_buf_size(buf), buf);
2515 			}
2516 
2517 			if (hdr->b_l1hdr.b_pabd != NULL) {
2518 				(void) zfs_refcount_add_many(
2519 				    &new_state->arcs_size[type],
2520 				    arc_hdr_size(hdr), hdr);
2521 			}
2522 
2523 			if (HDR_HAS_RABD(hdr)) {
2524 				(void) zfs_refcount_add_many(
2525 				    &new_state->arcs_size[type],
2526 				    HDR_GET_PSIZE(hdr), hdr);
2527 			}
2528 		}
2529 	}
2530 
2531 	if (update_old && old_state != arc_l2c_only) {
2532 		ASSERT(HDR_HAS_L1HDR(hdr));
2533 		if (GHOST_STATE(old_state)) {
2534 			ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2535 			ASSERT(!HDR_HAS_RABD(hdr));
2536 
2537 			/*
2538 			 * When moving a header off of a ghost state,
2539 			 * the header will not contain any arc buffers.
2540 			 * We use the arc header pointer for the reference
2541 			 * which is exactly what we did when we put the
2542 			 * header on the ghost state.
2543 			 */
2544 
2545 			(void) zfs_refcount_remove_many(
2546 			    &old_state->arcs_size[type],
2547 			    HDR_GET_LSIZE(hdr), hdr);
2548 		} else {
2549 
2550 			/*
2551 			 * Each individual buffer holds a unique reference,
2552 			 * thus we must remove each of these references one
2553 			 * at a time.
2554 			 */
2555 			for (arc_buf_t *buf = hdr->b_l1hdr.b_buf; buf != NULL;
2556 			    buf = buf->b_next) {
2557 
2558 				/*
2559 				 * When the arc_buf_t is sharing the data
2560 				 * block with the hdr, the owner of the
2561 				 * reference belongs to the hdr. Only
2562 				 * add to the refcount if the arc_buf_t is
2563 				 * not shared.
2564 				 */
2565 				if (ARC_BUF_SHARED(buf))
2566 					continue;
2567 
2568 				(void) zfs_refcount_remove_many(
2569 				    &old_state->arcs_size[type],
2570 				    arc_buf_size(buf), buf);
2571 			}
2572 			ASSERT(hdr->b_l1hdr.b_pabd != NULL ||
2573 			    HDR_HAS_RABD(hdr));
2574 
2575 			if (hdr->b_l1hdr.b_pabd != NULL) {
2576 				(void) zfs_refcount_remove_many(
2577 				    &old_state->arcs_size[type],
2578 				    arc_hdr_size(hdr), hdr);
2579 			}
2580 
2581 			if (HDR_HAS_RABD(hdr)) {
2582 				(void) zfs_refcount_remove_many(
2583 				    &old_state->arcs_size[type],
2584 				    HDR_GET_PSIZE(hdr), hdr);
2585 			}
2586 		}
2587 	}
2588 
2589 	if (HDR_HAS_L1HDR(hdr)) {
2590 		hdr->b_l1hdr.b_state = new_state;
2591 
2592 		if (HDR_HAS_L2HDR(hdr) && new_state != arc_l2c_only) {
2593 			l2arc_hdr_arcstats_decrement_state(hdr);
2594 			hdr->b_l2hdr.b_arcs_state = new_state->arcs_state;
2595 			l2arc_hdr_arcstats_increment_state(hdr);
2596 		}
2597 	}
2598 }
2599 
2600 void
arc_space_consume(uint64_t space,arc_space_type_t type)2601 arc_space_consume(uint64_t space, arc_space_type_t type)
2602 {
2603 	ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
2604 
2605 	switch (type) {
2606 	default:
2607 		break;
2608 	case ARC_SPACE_DATA:
2609 		ARCSTAT_INCR(arcstat_data_size, space);
2610 		break;
2611 	case ARC_SPACE_META:
2612 		ARCSTAT_INCR(arcstat_metadata_size, space);
2613 		break;
2614 	case ARC_SPACE_BONUS:
2615 		ARCSTAT_INCR(arcstat_bonus_size, space);
2616 		break;
2617 	case ARC_SPACE_DNODE:
2618 		ARCSTAT_INCR(arcstat_dnode_size, space);
2619 		break;
2620 	case ARC_SPACE_DBUF:
2621 		ARCSTAT_INCR(arcstat_dbuf_size, space);
2622 		break;
2623 	case ARC_SPACE_HDRS:
2624 		ARCSTAT_INCR(arcstat_hdr_size, space);
2625 		break;
2626 	case ARC_SPACE_L2HDRS:
2627 		aggsum_add(&arc_sums.arcstat_l2_hdr_size, space);
2628 		break;
2629 	case ARC_SPACE_ABD_CHUNK_WASTE:
2630 		/*
2631 		 * Note: this includes space wasted by all scatter ABD's, not
2632 		 * just those allocated by the ARC.  But the vast majority of
2633 		 * scatter ABD's come from the ARC, because other users are
2634 		 * very short-lived.
2635 		 */
2636 		ARCSTAT_INCR(arcstat_abd_chunk_waste_size, space);
2637 		break;
2638 	}
2639 
2640 	if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE)
2641 		ARCSTAT_INCR(arcstat_meta_used, space);
2642 
2643 	aggsum_add(&arc_sums.arcstat_size, space);
2644 }
2645 
2646 void
arc_space_return(uint64_t space,arc_space_type_t type)2647 arc_space_return(uint64_t space, arc_space_type_t type)
2648 {
2649 	ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
2650 
2651 	switch (type) {
2652 	default:
2653 		break;
2654 	case ARC_SPACE_DATA:
2655 		ARCSTAT_INCR(arcstat_data_size, -space);
2656 		break;
2657 	case ARC_SPACE_META:
2658 		ARCSTAT_INCR(arcstat_metadata_size, -space);
2659 		break;
2660 	case ARC_SPACE_BONUS:
2661 		ARCSTAT_INCR(arcstat_bonus_size, -space);
2662 		break;
2663 	case ARC_SPACE_DNODE:
2664 		ARCSTAT_INCR(arcstat_dnode_size, -space);
2665 		break;
2666 	case ARC_SPACE_DBUF:
2667 		ARCSTAT_INCR(arcstat_dbuf_size, -space);
2668 		break;
2669 	case ARC_SPACE_HDRS:
2670 		ARCSTAT_INCR(arcstat_hdr_size, -space);
2671 		break;
2672 	case ARC_SPACE_L2HDRS:
2673 		aggsum_add(&arc_sums.arcstat_l2_hdr_size, -space);
2674 		break;
2675 	case ARC_SPACE_ABD_CHUNK_WASTE:
2676 		ARCSTAT_INCR(arcstat_abd_chunk_waste_size, -space);
2677 		break;
2678 	}
2679 
2680 	if (type != ARC_SPACE_DATA && type != ARC_SPACE_ABD_CHUNK_WASTE)
2681 		ARCSTAT_INCR(arcstat_meta_used, -space);
2682 
2683 	ASSERT(aggsum_compare(&arc_sums.arcstat_size, space) >= 0);
2684 	aggsum_add(&arc_sums.arcstat_size, -space);
2685 }
2686 
2687 /*
2688  * Given a hdr and a buf, returns whether that buf can share its b_data buffer
2689  * with the hdr's b_pabd.
2690  */
2691 static boolean_t
arc_can_share(arc_buf_hdr_t * hdr,arc_buf_t * buf)2692 arc_can_share(arc_buf_hdr_t *hdr, arc_buf_t *buf)
2693 {
2694 	/*
2695 	 * The criteria for sharing a hdr's data are:
2696 	 * 1. the buffer is not encrypted
2697 	 * 2. the hdr's compression matches the buf's compression
2698 	 * 3. the hdr doesn't need to be byteswapped
2699 	 * 4. the hdr isn't already being shared
2700 	 * 5. the buf is either compressed or it is the last buf in the hdr list
2701 	 *
2702 	 * Criterion #5 maintains the invariant that shared uncompressed
2703 	 * bufs must be the final buf in the hdr's b_buf list. Reading this, you
2704 	 * might ask, "if a compressed buf is allocated first, won't that be the
2705 	 * last thing in the list?", but in that case it's impossible to create
2706 	 * a shared uncompressed buf anyway (because the hdr must be compressed
2707 	 * to have the compressed buf). You might also think that #3 is
2708 	 * sufficient to make this guarantee, however it's possible
2709 	 * (specifically in the rare L2ARC write race mentioned in
2710 	 * arc_buf_alloc_impl()) there will be an existing uncompressed buf that
2711 	 * is shareable, but wasn't at the time of its allocation. Rather than
2712 	 * allow a new shared uncompressed buf to be created and then shuffle
2713 	 * the list around to make it the last element, this simply disallows
2714 	 * sharing if the new buf isn't the first to be added.
2715 	 */
2716 	ASSERT3P(buf->b_hdr, ==, hdr);
2717 	boolean_t hdr_compressed =
2718 	    arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF;
2719 	boolean_t buf_compressed = ARC_BUF_COMPRESSED(buf) != 0;
2720 	return (!ARC_BUF_ENCRYPTED(buf) &&
2721 	    buf_compressed == hdr_compressed &&
2722 	    hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS &&
2723 	    !HDR_SHARED_DATA(hdr) &&
2724 	    (ARC_BUF_LAST(buf) || ARC_BUF_COMPRESSED(buf)));
2725 }
2726 
2727 /*
2728  * Allocate a buf for this hdr. If you care about the data that's in the hdr,
2729  * or if you want a compressed buffer, pass those flags in. Returns 0 if the
2730  * copy was made successfully, or an error code otherwise.
2731  */
2732 static int
arc_buf_alloc_impl(arc_buf_hdr_t * hdr,spa_t * spa,const zbookmark_phys_t * zb,const void * tag,boolean_t encrypted,boolean_t compressed,boolean_t noauth,boolean_t fill,arc_buf_t ** ret)2733 arc_buf_alloc_impl(arc_buf_hdr_t *hdr, spa_t *spa, const zbookmark_phys_t *zb,
2734     const void *tag, boolean_t encrypted, boolean_t compressed,
2735     boolean_t noauth, boolean_t fill, arc_buf_t **ret)
2736 {
2737 	arc_buf_t *buf;
2738 	arc_fill_flags_t flags = ARC_FILL_LOCKED;
2739 
2740 	ASSERT(HDR_HAS_L1HDR(hdr));
2741 	ASSERT3U(HDR_GET_LSIZE(hdr), >, 0);
2742 	VERIFY(hdr->b_type == ARC_BUFC_DATA ||
2743 	    hdr->b_type == ARC_BUFC_METADATA);
2744 	ASSERT3P(ret, !=, NULL);
2745 	ASSERT3P(*ret, ==, NULL);
2746 	IMPLY(encrypted, compressed);
2747 
2748 	buf = *ret = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
2749 	buf->b_hdr = hdr;
2750 	buf->b_data = NULL;
2751 	buf->b_next = hdr->b_l1hdr.b_buf;
2752 	buf->b_flags = 0;
2753 
2754 	add_reference(hdr, tag);
2755 
2756 	/*
2757 	 * We're about to change the hdr's b_flags. We must either
2758 	 * hold the hash_lock or be undiscoverable.
2759 	 */
2760 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
2761 
2762 	/*
2763 	 * Only honor requests for compressed bufs if the hdr is actually
2764 	 * compressed. This must be overridden if the buffer is encrypted since
2765 	 * encrypted buffers cannot be decompressed.
2766 	 */
2767 	if (encrypted) {
2768 		buf->b_flags |= ARC_BUF_FLAG_COMPRESSED;
2769 		buf->b_flags |= ARC_BUF_FLAG_ENCRYPTED;
2770 		flags |= ARC_FILL_COMPRESSED | ARC_FILL_ENCRYPTED;
2771 	} else if (compressed &&
2772 	    arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) {
2773 		buf->b_flags |= ARC_BUF_FLAG_COMPRESSED;
2774 		flags |= ARC_FILL_COMPRESSED;
2775 	}
2776 
2777 	if (noauth) {
2778 		ASSERT0(encrypted);
2779 		flags |= ARC_FILL_NOAUTH;
2780 	}
2781 
2782 	/*
2783 	 * If the hdr's data can be shared then we share the data buffer and
2784 	 * set the appropriate bit in the hdr's b_flags to indicate the hdr is
2785 	 * sharing it's b_pabd with the arc_buf_t. Otherwise, we allocate a new
2786 	 * buffer to store the buf's data.
2787 	 *
2788 	 * There are two additional restrictions here because we're sharing
2789 	 * hdr -> buf instead of the usual buf -> hdr. First, the hdr can't be
2790 	 * actively involved in an L2ARC write, because if this buf is used by
2791 	 * an arc_write() then the hdr's data buffer will be released when the
2792 	 * write completes, even though the L2ARC write might still be using it.
2793 	 * Second, the hdr's ABD must be linear so that the buf's user doesn't
2794 	 * need to be ABD-aware.  It must be allocated via
2795 	 * zio_[data_]buf_alloc(), not as a page, because we need to be able
2796 	 * to abd_release_ownership_of_buf(), which isn't allowed on "linear
2797 	 * page" buffers because the ABD code needs to handle freeing them
2798 	 * specially.
2799 	 */
2800 	boolean_t can_share = arc_can_share(hdr, buf) &&
2801 	    !HDR_L2_WRITING(hdr) &&
2802 	    hdr->b_l1hdr.b_pabd != NULL &&
2803 	    abd_is_linear(hdr->b_l1hdr.b_pabd) &&
2804 	    !abd_is_linear_page(hdr->b_l1hdr.b_pabd);
2805 
2806 	/* Set up b_data and sharing */
2807 	if (can_share) {
2808 		buf->b_data = abd_to_buf(hdr->b_l1hdr.b_pabd);
2809 		buf->b_flags |= ARC_BUF_FLAG_SHARED;
2810 		arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA);
2811 	} else {
2812 		buf->b_data =
2813 		    arc_get_data_buf(hdr, arc_buf_size(buf), buf);
2814 		ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf));
2815 	}
2816 	VERIFY3P(buf->b_data, !=, NULL);
2817 
2818 	hdr->b_l1hdr.b_buf = buf;
2819 
2820 	/*
2821 	 * If the user wants the data from the hdr, we need to either copy or
2822 	 * decompress the data.
2823 	 */
2824 	if (fill) {
2825 		ASSERT3P(zb, !=, NULL);
2826 		return (arc_buf_fill(buf, spa, zb, flags));
2827 	}
2828 
2829 	return (0);
2830 }
2831 
2832 static const char *arc_onloan_tag = "onloan";
2833 
2834 static inline void
arc_loaned_bytes_update(int64_t delta)2835 arc_loaned_bytes_update(int64_t delta)
2836 {
2837 	atomic_add_64(&arc_loaned_bytes, delta);
2838 
2839 	/* assert that it did not wrap around */
2840 	ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0);
2841 }
2842 
2843 /*
2844  * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
2845  * flight data by arc_tempreserve_space() until they are "returned". Loaned
2846  * buffers must be returned to the arc before they can be used by the DMU or
2847  * freed.
2848  */
2849 arc_buf_t *
arc_loan_buf(spa_t * spa,boolean_t is_metadata,int size)2850 arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size)
2851 {
2852 	arc_buf_t *buf = arc_alloc_buf(spa, arc_onloan_tag,
2853 	    is_metadata ? ARC_BUFC_METADATA : ARC_BUFC_DATA, size);
2854 
2855 	arc_loaned_bytes_update(arc_buf_size(buf));
2856 
2857 	return (buf);
2858 }
2859 
2860 arc_buf_t *
arc_loan_compressed_buf(spa_t * spa,uint64_t psize,uint64_t lsize,enum zio_compress compression_type,uint8_t complevel)2861 arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize,
2862     enum zio_compress compression_type, uint8_t complevel)
2863 {
2864 	arc_buf_t *buf = arc_alloc_compressed_buf(spa, arc_onloan_tag,
2865 	    psize, lsize, compression_type, complevel);
2866 
2867 	arc_loaned_bytes_update(arc_buf_size(buf));
2868 
2869 	return (buf);
2870 }
2871 
2872 arc_buf_t *
arc_loan_raw_buf(spa_t * spa,uint64_t dsobj,boolean_t byteorder,const uint8_t * salt,const uint8_t * iv,const uint8_t * mac,dmu_object_type_t ot,uint64_t psize,uint64_t lsize,enum zio_compress compression_type,uint8_t complevel)2873 arc_loan_raw_buf(spa_t *spa, uint64_t dsobj, boolean_t byteorder,
2874     const uint8_t *salt, const uint8_t *iv, const uint8_t *mac,
2875     dmu_object_type_t ot, uint64_t psize, uint64_t lsize,
2876     enum zio_compress compression_type, uint8_t complevel)
2877 {
2878 	arc_buf_t *buf = arc_alloc_raw_buf(spa, arc_onloan_tag, dsobj,
2879 	    byteorder, salt, iv, mac, ot, psize, lsize, compression_type,
2880 	    complevel);
2881 
2882 	atomic_add_64(&arc_loaned_bytes, psize);
2883 	return (buf);
2884 }
2885 
2886 
2887 /*
2888  * Return a loaned arc buffer to the arc.
2889  */
2890 void
arc_return_buf(arc_buf_t * buf,const void * tag)2891 arc_return_buf(arc_buf_t *buf, const void *tag)
2892 {
2893 	arc_buf_hdr_t *hdr = buf->b_hdr;
2894 
2895 	ASSERT3P(buf->b_data, !=, NULL);
2896 	ASSERT(HDR_HAS_L1HDR(hdr));
2897 	(void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, tag);
2898 	(void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag);
2899 
2900 	arc_loaned_bytes_update(-arc_buf_size(buf));
2901 }
2902 
2903 /* Detach an arc_buf from a dbuf (tag) */
2904 void
arc_loan_inuse_buf(arc_buf_t * buf,const void * tag)2905 arc_loan_inuse_buf(arc_buf_t *buf, const void *tag)
2906 {
2907 	arc_buf_hdr_t *hdr = buf->b_hdr;
2908 
2909 	ASSERT3P(buf->b_data, !=, NULL);
2910 	ASSERT(HDR_HAS_L1HDR(hdr));
2911 	(void) zfs_refcount_add(&hdr->b_l1hdr.b_refcnt, arc_onloan_tag);
2912 	(void) zfs_refcount_remove(&hdr->b_l1hdr.b_refcnt, tag);
2913 
2914 	arc_loaned_bytes_update(arc_buf_size(buf));
2915 }
2916 
2917 static void
l2arc_free_abd_on_write(abd_t * abd,size_t size,arc_buf_contents_t type)2918 l2arc_free_abd_on_write(abd_t *abd, size_t size, arc_buf_contents_t type)
2919 {
2920 	l2arc_data_free_t *df = kmem_alloc(sizeof (*df), KM_SLEEP);
2921 
2922 	df->l2df_abd = abd;
2923 	df->l2df_size = size;
2924 	df->l2df_type = type;
2925 	mutex_enter(&l2arc_free_on_write_mtx);
2926 	list_insert_head(l2arc_free_on_write, df);
2927 	mutex_exit(&l2arc_free_on_write_mtx);
2928 }
2929 
2930 static void
arc_hdr_free_on_write(arc_buf_hdr_t * hdr,boolean_t free_rdata)2931 arc_hdr_free_on_write(arc_buf_hdr_t *hdr, boolean_t free_rdata)
2932 {
2933 	arc_state_t *state = hdr->b_l1hdr.b_state;
2934 	arc_buf_contents_t type = arc_buf_type(hdr);
2935 	uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr);
2936 
2937 	/* protected by hash lock, if in the hash table */
2938 	if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
2939 		ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
2940 		ASSERT(state != arc_anon && state != arc_l2c_only);
2941 
2942 		(void) zfs_refcount_remove_many(&state->arcs_esize[type],
2943 		    size, hdr);
2944 	}
2945 	(void) zfs_refcount_remove_many(&state->arcs_size[type], size, hdr);
2946 	if (type == ARC_BUFC_METADATA) {
2947 		arc_space_return(size, ARC_SPACE_META);
2948 	} else {
2949 		ASSERT(type == ARC_BUFC_DATA);
2950 		arc_space_return(size, ARC_SPACE_DATA);
2951 	}
2952 
2953 	if (free_rdata) {
2954 		l2arc_free_abd_on_write(hdr->b_crypt_hdr.b_rabd, size, type);
2955 	} else {
2956 		l2arc_free_abd_on_write(hdr->b_l1hdr.b_pabd, size, type);
2957 	}
2958 }
2959 
2960 /*
2961  * Share the arc_buf_t's data with the hdr. Whenever we are sharing the
2962  * data buffer, we transfer the refcount ownership to the hdr and update
2963  * the appropriate kstats.
2964  */
2965 static void
arc_share_buf(arc_buf_hdr_t * hdr,arc_buf_t * buf)2966 arc_share_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf)
2967 {
2968 	ASSERT(arc_can_share(hdr, buf));
2969 	ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
2970 	ASSERT(!ARC_BUF_ENCRYPTED(buf));
2971 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
2972 
2973 	/*
2974 	 * Start sharing the data buffer. We transfer the
2975 	 * refcount ownership to the hdr since it always owns
2976 	 * the refcount whenever an arc_buf_t is shared.
2977 	 */
2978 	zfs_refcount_transfer_ownership_many(
2979 	    &hdr->b_l1hdr.b_state->arcs_size[arc_buf_type(hdr)],
2980 	    arc_hdr_size(hdr), buf, hdr);
2981 	hdr->b_l1hdr.b_pabd = abd_get_from_buf(buf->b_data, arc_buf_size(buf));
2982 	abd_take_ownership_of_buf(hdr->b_l1hdr.b_pabd,
2983 	    HDR_ISTYPE_METADATA(hdr));
2984 	arc_hdr_set_flags(hdr, ARC_FLAG_SHARED_DATA);
2985 	buf->b_flags |= ARC_BUF_FLAG_SHARED;
2986 
2987 	/*
2988 	 * Since we've transferred ownership to the hdr we need
2989 	 * to increment its compressed and uncompressed kstats and
2990 	 * decrement the overhead size.
2991 	 */
2992 	ARCSTAT_INCR(arcstat_compressed_size, arc_hdr_size(hdr));
2993 	ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr));
2994 	ARCSTAT_INCR(arcstat_overhead_size, -arc_buf_size(buf));
2995 }
2996 
2997 static void
arc_unshare_buf(arc_buf_hdr_t * hdr,arc_buf_t * buf)2998 arc_unshare_buf(arc_buf_hdr_t *hdr, arc_buf_t *buf)
2999 {
3000 	ASSERT(arc_buf_is_shared(buf));
3001 	ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
3002 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
3003 
3004 	/*
3005 	 * We are no longer sharing this buffer so we need
3006 	 * to transfer its ownership to the rightful owner.
3007 	 */
3008 	zfs_refcount_transfer_ownership_many(
3009 	    &hdr->b_l1hdr.b_state->arcs_size[arc_buf_type(hdr)],
3010 	    arc_hdr_size(hdr), hdr, buf);
3011 	arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA);
3012 	abd_release_ownership_of_buf(hdr->b_l1hdr.b_pabd);
3013 	abd_free(hdr->b_l1hdr.b_pabd);
3014 	hdr->b_l1hdr.b_pabd = NULL;
3015 	buf->b_flags &= ~ARC_BUF_FLAG_SHARED;
3016 
3017 	/*
3018 	 * Since the buffer is no longer shared between
3019 	 * the arc buf and the hdr, count it as overhead.
3020 	 */
3021 	ARCSTAT_INCR(arcstat_compressed_size, -arc_hdr_size(hdr));
3022 	ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr));
3023 	ARCSTAT_INCR(arcstat_overhead_size, arc_buf_size(buf));
3024 }
3025 
3026 /*
3027  * Remove an arc_buf_t from the hdr's buf list and return the last
3028  * arc_buf_t on the list. If no buffers remain on the list then return
3029  * NULL.
3030  */
3031 static arc_buf_t *
arc_buf_remove(arc_buf_hdr_t * hdr,arc_buf_t * buf)3032 arc_buf_remove(arc_buf_hdr_t *hdr, arc_buf_t *buf)
3033 {
3034 	ASSERT(HDR_HAS_L1HDR(hdr));
3035 	ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
3036 
3037 	arc_buf_t **bufp = &hdr->b_l1hdr.b_buf;
3038 	arc_buf_t *lastbuf = NULL;
3039 
3040 	/*
3041 	 * Remove the buf from the hdr list and locate the last
3042 	 * remaining buffer on the list.
3043 	 */
3044 	while (*bufp != NULL) {
3045 		if (*bufp == buf)
3046 			*bufp = buf->b_next;
3047 
3048 		/*
3049 		 * If we've removed a buffer in the middle of
3050 		 * the list then update the lastbuf and update
3051 		 * bufp.
3052 		 */
3053 		if (*bufp != NULL) {
3054 			lastbuf = *bufp;
3055 			bufp = &(*bufp)->b_next;
3056 		}
3057 	}
3058 	buf->b_next = NULL;
3059 	ASSERT3P(lastbuf, !=, buf);
3060 	IMPLY(lastbuf != NULL, ARC_BUF_LAST(lastbuf));
3061 
3062 	return (lastbuf);
3063 }
3064 
3065 /*
3066  * Free up buf->b_data and pull the arc_buf_t off of the arc_buf_hdr_t's
3067  * list and free it.
3068  */
3069 static void
arc_buf_destroy_impl(arc_buf_t * buf)3070 arc_buf_destroy_impl(arc_buf_t *buf)
3071 {
3072 	arc_buf_hdr_t *hdr = buf->b_hdr;
3073 
3074 	/*
3075 	 * Free up the data associated with the buf but only if we're not
3076 	 * sharing this with the hdr. If we are sharing it with the hdr, the
3077 	 * hdr is responsible for doing the free.
3078 	 */
3079 	if (buf->b_data != NULL) {
3080 		/*
3081 		 * We're about to change the hdr's b_flags. We must either
3082 		 * hold the hash_lock or be undiscoverable.
3083 		 */
3084 		ASSERT(HDR_EMPTY_OR_LOCKED(hdr));
3085 
3086 		arc_cksum_verify(buf);
3087 		arc_buf_unwatch(buf);
3088 
3089 		if (ARC_BUF_SHARED(buf)) {
3090 			arc_hdr_clear_flags(hdr, ARC_FLAG_SHARED_DATA);
3091 		} else {
3092 			ASSERT(!arc_buf_is_shared(buf));
3093 			uint64_t size = arc_buf_size(buf);
3094 			arc_free_data_buf(hdr, buf->b_data, size, buf);
3095 			ARCSTAT_INCR(arcstat_overhead_size, -size);
3096 		}
3097 		buf->b_data = NULL;
3098 
3099 		/*
3100 		 * If we have no more encrypted buffers and we've already
3101 		 * gotten a copy of the decrypted data we can free b_rabd
3102 		 * to save some space.
3103 		 */
3104 		if (ARC_BUF_ENCRYPTED(buf) && HDR_HAS_RABD(hdr) &&
3105 		    hdr->b_l1hdr.b_pabd != NULL && !HDR_IO_IN_PROGRESS(hdr)) {
3106 			arc_buf_t *b;
3107 			for (b = hdr->b_l1hdr.b_buf; b; b = b->b_next) {
3108 				if (b != buf && ARC_BUF_ENCRYPTED(b))
3109 					break;
3110 			}
3111 			if (b == NULL)
3112 				arc_hdr_free_abd(hdr, B_TRUE);
3113 		}
3114 	}
3115 
3116 	arc_buf_t *lastbuf = arc_buf_remove(hdr, buf);
3117 
3118 	if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) {
3119 		/*
3120 		 * If the current arc_buf_t is sharing its data buffer with the
3121 		 * hdr, then reassign the hdr's b_pabd to share it with the new
3122 		 * buffer at the end of the list. The shared buffer is always
3123 		 * the last one on the hdr's buffer list.
3124 		 *
3125 		 * There is an equivalent case for compressed bufs, but since
3126 		 * they aren't guaranteed to be the last buf in the list and
3127 		 * that is an exceedingly rare case, we just allow that space be
3128 		 * wasted temporarily. We must also be careful not to share
3129 		 * encrypted buffers, since they cannot be shared.
3130 		 */
3131 		if (lastbuf != NULL && !ARC_BUF_ENCRYPTED(lastbuf)) {
3132 			/* Only one buf can be shared at once */
3133 			ASSERT(!arc_buf_is_shared(lastbuf));
3134 			/* hdr is uncompressed so can't have compressed buf */
3135 			ASSERT(!ARC_BUF_COMPRESSED(lastbuf));
3136 
3137 			ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
3138 			arc_hdr_free_abd(hdr, B_FALSE);
3139 
3140 			/*
3141 			 * We must setup a new shared block between the
3142 			 * last buffer and the hdr. The data would have
3143 			 * been allocated by the arc buf so we need to transfer
3144 			 * ownership to the hdr since it's now being shared.
3145 			 */
3146 			arc_share_buf(hdr, lastbuf);
3147 		}
3148 	} else if (HDR_SHARED_DATA(hdr)) {
3149 		/*
3150 		 * Uncompressed shared buffers are always at the end
3151 		 * of the list. Compressed buffers don't have the
3152 		 * same requirements. This makes it hard to
3153 		 * simply assert that the lastbuf is shared so
3154 		 * we rely on the hdr's compression flags to determine
3155 		 * if we have a compressed, shared buffer.
3156 		 */
3157 		ASSERT3P(lastbuf, !=, NULL);
3158 		ASSERT(arc_buf_is_shared(lastbuf) ||
3159 		    arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF);
3160 	}
3161 
3162 	/*
3163 	 * Free the checksum if we're removing the last uncompressed buf from
3164 	 * this hdr.
3165 	 */
3166 	if (!arc_hdr_has_uncompressed_buf(hdr)) {
3167 		arc_cksum_free(hdr);
3168 	}
3169 
3170 	/* clean up the buf */
3171 	buf->b_hdr = NULL;
3172 	kmem_cache_free(buf_cache, buf);
3173 }
3174 
3175 static void
arc_hdr_alloc_abd(arc_buf_hdr_t * hdr,int alloc_flags)3176 arc_hdr_alloc_abd(arc_buf_hdr_t *hdr, int alloc_flags)
3177 {
3178 	uint64_t size;
3179 	boolean_t alloc_rdata = ((alloc_flags & ARC_HDR_ALLOC_RDATA) != 0);
3180 
3181 	ASSERT3U(HDR_GET_LSIZE(hdr), >, 0);
3182 	ASSERT(HDR_HAS_L1HDR(hdr));
3183 	ASSERT(!HDR_SHARED_DATA(hdr) || alloc_rdata);
3184 	IMPLY(alloc_rdata, HDR_PROTECTED(hdr));
3185 
3186 	if (alloc_rdata) {
3187 		size = HDR_GET_PSIZE(hdr);
3188 		ASSERT3P(hdr->b_crypt_hdr.b_rabd, ==, NULL);
3189 		hdr->b_crypt_hdr.b_rabd = arc_get_data_abd(hdr, size, hdr,
3190 		    alloc_flags);
3191 		ASSERT3P(hdr->b_crypt_hdr.b_rabd, !=, NULL);
3192 		ARCSTAT_INCR(arcstat_raw_size, size);
3193 	} else {
3194 		size = arc_hdr_size(hdr);
3195 		ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
3196 		hdr->b_l1hdr.b_pabd = arc_get_data_abd(hdr, size, hdr,
3197 		    alloc_flags);
3198 		ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
3199 	}
3200 
3201 	ARCSTAT_INCR(arcstat_compressed_size, size);
3202 	ARCSTAT_INCR(arcstat_uncompressed_size, HDR_GET_LSIZE(hdr));
3203 }
3204 
3205 static void
arc_hdr_free_abd(arc_buf_hdr_t * hdr,boolean_t free_rdata)3206 arc_hdr_free_abd(arc_buf_hdr_t *hdr, boolean_t free_rdata)
3207 {
3208 	uint64_t size = (free_rdata) ? HDR_GET_PSIZE(hdr) : arc_hdr_size(hdr);
3209 
3210 	ASSERT(HDR_HAS_L1HDR(hdr));
3211 	ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr));
3212 	IMPLY(free_rdata, HDR_HAS_RABD(hdr));
3213 
3214 	/*
3215 	 * If the hdr is currently being written to the l2arc then
3216 	 * we defer freeing the data by adding it to the l2arc_free_on_write
3217 	 * list. The l2arc will free the data once it's finished
3218 	 * writing it to the l2arc device.
3219 	 */
3220 	if (HDR_L2_WRITING(hdr)) {
3221 		arc_hdr_free_on_write(hdr, free_rdata);
3222 		ARCSTAT_BUMP(arcstat_l2_free_on_write);
3223 	} else if (free_rdata) {
3224 		arc_free_data_abd(hdr, hdr->b_crypt_hdr.b_rabd, size, hdr);
3225 	} else {
3226 		arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd, size, hdr);
3227 	}
3228 
3229 	if (free_rdata) {
3230 		hdr->b_crypt_hdr.b_rabd = NULL;
3231 		ARCSTAT_INCR(arcstat_raw_size, -size);
3232 	} else {
3233 		hdr->b_l1hdr.b_pabd = NULL;
3234 	}
3235 
3236 	if (hdr->b_l1hdr.b_pabd == NULL && !HDR_HAS_RABD(hdr))
3237 		hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS;
3238 
3239 	ARCSTAT_INCR(arcstat_compressed_size, -size);
3240 	ARCSTAT_INCR(arcstat_uncompressed_size, -HDR_GET_LSIZE(hdr));
3241 }
3242 
3243 /*
3244  * Allocate empty anonymous ARC header.  The header will get its identity
3245  * assigned and buffers attached later as part of read or write operations.
3246  *
3247  * In case of read arc_read() assigns header its identify (b_dva + b_birth),
3248  * inserts it into ARC hash to become globally visible and allocates physical
3249  * (b_pabd) or raw (b_rabd) ABD buffer to read into from disk.  On disk read
3250  * completion arc_read_done() allocates ARC buffer(s) as needed, potentially
3251  * sharing one of them with the physical ABD buffer.
3252  *
3253  * In case of write arc_alloc_buf() allocates ARC buffer to be filled with
3254  * data.  Then after compression and/or encryption arc_write_ready() allocates
3255  * and fills (or potentially shares) physical (b_pabd) or raw (b_rabd) ABD
3256  * buffer.  On disk write completion arc_write_done() assigns the header its
3257  * new identity (b_dva + b_birth) and inserts into ARC hash.
3258  *
3259  * In case of partial overwrite the old data is read first as described. Then
3260  * arc_release() either allocates new anonymous ARC header and moves the ARC
3261  * buffer to it, or reuses the old ARC header by discarding its identity and
3262  * removing it from ARC hash.  After buffer modification normal write process
3263  * follows as described.
3264  */
3265 static arc_buf_hdr_t *
arc_hdr_alloc(uint64_t spa,int32_t psize,int32_t lsize,boolean_t protected,enum zio_compress compression_type,uint8_t complevel,arc_buf_contents_t type)3266 arc_hdr_alloc(uint64_t spa, int32_t psize, int32_t lsize,
3267     boolean_t protected, enum zio_compress compression_type, uint8_t complevel,
3268     arc_buf_contents_t type)
3269 {
3270 	arc_buf_hdr_t *hdr;
3271 
3272 	VERIFY(type == ARC_BUFC_DATA || type == ARC_BUFC_METADATA);
3273 	hdr = kmem_cache_alloc(hdr_full_cache, KM_PUSHPAGE);
3274 
3275 	ASSERT(HDR_EMPTY(hdr));
3276 #ifdef ZFS_DEBUG
3277 	ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL);
3278 #endif
3279 	HDR_SET_PSIZE(hdr, psize);
3280 	HDR_SET_LSIZE(hdr, lsize);
3281 	hdr->b_spa = spa;
3282 	hdr->b_type = type;
3283 	hdr->b_flags = 0;
3284 	arc_hdr_set_flags(hdr, arc_bufc_to_flags(type) | ARC_FLAG_HAS_L1HDR);
3285 	arc_hdr_set_compress(hdr, compression_type);
3286 	hdr->b_complevel = complevel;
3287 	if (protected)
3288 		arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED);
3289 
3290 	hdr->b_l1hdr.b_state = arc_anon;
3291 	hdr->b_l1hdr.b_arc_access = 0;
3292 	hdr->b_l1hdr.b_mru_hits = 0;
3293 	hdr->b_l1hdr.b_mru_ghost_hits = 0;
3294 	hdr->b_l1hdr.b_mfu_hits = 0;
3295 	hdr->b_l1hdr.b_mfu_ghost_hits = 0;
3296 	hdr->b_l1hdr.b_buf = NULL;
3297 
3298 	ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
3299 
3300 	return (hdr);
3301 }
3302 
3303 /*
3304  * Transition between the two allocation states for the arc_buf_hdr struct.
3305  * The arc_buf_hdr struct can be allocated with (hdr_full_cache) or without
3306  * (hdr_l2only_cache) the fields necessary for the L1 cache - the smaller
3307  * version is used when a cache buffer is only in the L2ARC in order to reduce
3308  * memory usage.
3309  */
3310 static arc_buf_hdr_t *
arc_hdr_realloc(arc_buf_hdr_t * hdr,kmem_cache_t * old,kmem_cache_t * new)3311 arc_hdr_realloc(arc_buf_hdr_t *hdr, kmem_cache_t *old, kmem_cache_t *new)
3312 {
3313 	ASSERT(HDR_HAS_L2HDR(hdr));
3314 
3315 	arc_buf_hdr_t *nhdr;
3316 	l2arc_dev_t *dev = hdr->b_l2hdr.b_dev;
3317 
3318 	ASSERT((old == hdr_full_cache && new == hdr_l2only_cache) ||
3319 	    (old == hdr_l2only_cache && new == hdr_full_cache));
3320 
3321 	nhdr = kmem_cache_alloc(new, KM_PUSHPAGE);
3322 
3323 	ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
3324 	buf_hash_remove(hdr);
3325 
3326 	memcpy(nhdr, hdr, HDR_L2ONLY_SIZE);
3327 
3328 	if (new == hdr_full_cache) {
3329 		arc_hdr_set_flags(nhdr, ARC_FLAG_HAS_L1HDR);
3330 		/*
3331 		 * arc_access and arc_change_state need to be aware that a
3332 		 * header has just come out of L2ARC, so we set its state to
3333 		 * l2c_only even though it's about to change.
3334 		 */
3335 		nhdr->b_l1hdr.b_state = arc_l2c_only;
3336 
3337 		/* Verify previous threads set to NULL before freeing */
3338 		ASSERT3P(nhdr->b_l1hdr.b_pabd, ==, NULL);
3339 		ASSERT(!HDR_HAS_RABD(hdr));
3340 	} else {
3341 		ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
3342 #ifdef ZFS_DEBUG
3343 		ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL);
3344 #endif
3345 
3346 		/*
3347 		 * If we've reached here, We must have been called from
3348 		 * arc_evict_hdr(), as such we should have already been
3349 		 * removed from any ghost list we were previously on
3350 		 * (which protects us from racing with arc_evict_state),
3351 		 * thus no locking is needed during this check.
3352 		 */
3353 		ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
3354 
3355 		/*
3356 		 * A buffer must not be moved into the arc_l2c_only
3357 		 * state if it's not finished being written out to the
3358 		 * l2arc device. Otherwise, the b_l1hdr.b_pabd field
3359 		 * might try to be accessed, even though it was removed.
3360 		 */
3361 		VERIFY(!HDR_L2_WRITING(hdr));
3362 		VERIFY3P(hdr->b_l1hdr.b_pabd, ==, NULL);
3363 		ASSERT(!HDR_HAS_RABD(hdr));
3364 
3365 		arc_hdr_clear_flags(nhdr, ARC_FLAG_HAS_L1HDR);
3366 	}
3367 	/*
3368 	 * The header has been reallocated so we need to re-insert it into any
3369 	 * lists it was on.
3370 	 */
3371 	(void) buf_hash_insert(nhdr, NULL);
3372 
3373 	ASSERT(list_link_active(&hdr->b_l2hdr.b_l2node));
3374 
3375 	mutex_enter(&dev->l2ad_mtx);
3376 
3377 	/*
3378 	 * We must place the realloc'ed header back into the list at
3379 	 * the same spot. Otherwise, if it's placed earlier in the list,
3380 	 * l2arc_write_buffers() could find it during the function's
3381 	 * write phase, and try to write it out to the l2arc.
3382 	 */
3383 	list_insert_after(&dev->l2ad_buflist, hdr, nhdr);
3384 	list_remove(&dev->l2ad_buflist, hdr);
3385 
3386 	mutex_exit(&dev->l2ad_mtx);
3387 
3388 	/*
3389 	 * Since we're using the pointer address as the tag when
3390 	 * incrementing and decrementing the l2ad_alloc refcount, we
3391 	 * must remove the old pointer (that we're about to destroy) and
3392 	 * add the new pointer to the refcount. Otherwise we'd remove
3393 	 * the wrong pointer address when calling arc_hdr_destroy() later.
3394 	 */
3395 
3396 	(void) zfs_refcount_remove_many(&dev->l2ad_alloc,
3397 	    arc_hdr_size(hdr), hdr);
3398 	(void) zfs_refcount_add_many(&dev->l2ad_alloc,
3399 	    arc_hdr_size(nhdr), nhdr);
3400 
3401 	buf_discard_identity(hdr);
3402 	kmem_cache_free(old, hdr);
3403 
3404 	return (nhdr);
3405 }
3406 
3407 /*
3408  * This function is used by the send / receive code to convert a newly
3409  * allocated arc_buf_t to one that is suitable for a raw encrypted write. It
3410  * is also used to allow the root objset block to be updated without altering
3411  * its embedded MACs. Both block types will always be uncompressed so we do not
3412  * have to worry about compression type or psize.
3413  */
3414 void
arc_convert_to_raw(arc_buf_t * buf,uint64_t dsobj,boolean_t byteorder,dmu_object_type_t ot,const uint8_t * salt,const uint8_t * iv,const uint8_t * mac)3415 arc_convert_to_raw(arc_buf_t *buf, uint64_t dsobj, boolean_t byteorder,
3416     dmu_object_type_t ot, const uint8_t *salt, const uint8_t *iv,
3417     const uint8_t *mac)
3418 {
3419 	arc_buf_hdr_t *hdr = buf->b_hdr;
3420 
3421 	ASSERT(ot == DMU_OT_DNODE || ot == DMU_OT_OBJSET);
3422 	ASSERT(HDR_HAS_L1HDR(hdr));
3423 	ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
3424 
3425 	buf->b_flags |= (ARC_BUF_FLAG_COMPRESSED | ARC_BUF_FLAG_ENCRYPTED);
3426 	arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED);
3427 	hdr->b_crypt_hdr.b_dsobj = dsobj;
3428 	hdr->b_crypt_hdr.b_ot = ot;
3429 	hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ?
3430 	    DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot);
3431 	if (!arc_hdr_has_uncompressed_buf(hdr))
3432 		arc_cksum_free(hdr);
3433 
3434 	if (salt != NULL)
3435 		memcpy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN);
3436 	if (iv != NULL)
3437 		memcpy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN);
3438 	if (mac != NULL)
3439 		memcpy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN);
3440 }
3441 
3442 /*
3443  * Allocate a new arc_buf_hdr_t and arc_buf_t and return the buf to the caller.
3444  * The buf is returned thawed since we expect the consumer to modify it.
3445  */
3446 arc_buf_t *
arc_alloc_buf(spa_t * spa,const void * tag,arc_buf_contents_t type,int32_t size)3447 arc_alloc_buf(spa_t *spa, const void *tag, arc_buf_contents_t type,
3448     int32_t size)
3449 {
3450 	arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), size, size,
3451 	    B_FALSE, ZIO_COMPRESS_OFF, 0, type);
3452 
3453 	arc_buf_t *buf = NULL;
3454 	VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE, B_FALSE,
3455 	    B_FALSE, B_FALSE, &buf));
3456 	arc_buf_thaw(buf);
3457 
3458 	return (buf);
3459 }
3460 
3461 /*
3462  * Allocate a compressed buf in the same manner as arc_alloc_buf. Don't use this
3463  * for bufs containing metadata.
3464  */
3465 arc_buf_t *
arc_alloc_compressed_buf(spa_t * spa,const void * tag,uint64_t psize,uint64_t lsize,enum zio_compress compression_type,uint8_t complevel)3466 arc_alloc_compressed_buf(spa_t *spa, const void *tag, uint64_t psize,
3467     uint64_t lsize, enum zio_compress compression_type, uint8_t complevel)
3468 {
3469 	ASSERT3U(lsize, >, 0);
3470 	ASSERT3U(lsize, >=, psize);
3471 	ASSERT3U(compression_type, >, ZIO_COMPRESS_OFF);
3472 	ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS);
3473 
3474 	arc_buf_hdr_t *hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize,
3475 	    B_FALSE, compression_type, complevel, ARC_BUFC_DATA);
3476 
3477 	arc_buf_t *buf = NULL;
3478 	VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_FALSE,
3479 	    B_TRUE, B_FALSE, B_FALSE, &buf));
3480 	arc_buf_thaw(buf);
3481 
3482 	/*
3483 	 * To ensure that the hdr has the correct data in it if we call
3484 	 * arc_untransform() on this buf before it's been written to disk,
3485 	 * it's easiest if we just set up sharing between the buf and the hdr.
3486 	 */
3487 	arc_share_buf(hdr, buf);
3488 
3489 	return (buf);
3490 }
3491 
3492 arc_buf_t *
arc_alloc_raw_buf(spa_t * spa,const void * tag,uint64_t dsobj,boolean_t byteorder,const uint8_t * salt,const uint8_t * iv,const uint8_t * mac,dmu_object_type_t ot,uint64_t psize,uint64_t lsize,enum zio_compress compression_type,uint8_t complevel)3493 arc_alloc_raw_buf(spa_t *spa, const void *tag, uint64_t dsobj,
3494     boolean_t byteorder, const uint8_t *salt, const uint8_t *iv,
3495     const uint8_t *mac, dmu_object_type_t ot, uint64_t psize, uint64_t lsize,
3496     enum zio_compress compression_type, uint8_t complevel)
3497 {
3498 	arc_buf_hdr_t *hdr;
3499 	arc_buf_t *buf;
3500 	arc_buf_contents_t type = DMU_OT_IS_METADATA(ot) ?
3501 	    ARC_BUFC_METADATA : ARC_BUFC_DATA;
3502 
3503 	ASSERT3U(lsize, >, 0);
3504 	ASSERT3U(lsize, >=, psize);
3505 	ASSERT3U(compression_type, >=, ZIO_COMPRESS_OFF);
3506 	ASSERT3U(compression_type, <, ZIO_COMPRESS_FUNCTIONS);
3507 
3508 	hdr = arc_hdr_alloc(spa_load_guid(spa), psize, lsize, B_TRUE,
3509 	    compression_type, complevel, type);
3510 
3511 	hdr->b_crypt_hdr.b_dsobj = dsobj;
3512 	hdr->b_crypt_hdr.b_ot = ot;
3513 	hdr->b_l1hdr.b_byteswap = (byteorder == ZFS_HOST_BYTEORDER) ?
3514 	    DMU_BSWAP_NUMFUNCS : DMU_OT_BYTESWAP(ot);
3515 	memcpy(hdr->b_crypt_hdr.b_salt, salt, ZIO_DATA_SALT_LEN);
3516 	memcpy(hdr->b_crypt_hdr.b_iv, iv, ZIO_DATA_IV_LEN);
3517 	memcpy(hdr->b_crypt_hdr.b_mac, mac, ZIO_DATA_MAC_LEN);
3518 
3519 	/*
3520 	 * This buffer will be considered encrypted even if the ot is not an
3521 	 * encrypted type. It will become authenticated instead in
3522 	 * arc_write_ready().
3523 	 */
3524 	buf = NULL;
3525 	VERIFY0(arc_buf_alloc_impl(hdr, spa, NULL, tag, B_TRUE, B_TRUE,
3526 	    B_FALSE, B_FALSE, &buf));
3527 	arc_buf_thaw(buf);
3528 
3529 	return (buf);
3530 }
3531 
3532 static void
l2arc_hdr_arcstats_update(arc_buf_hdr_t * hdr,boolean_t incr,boolean_t state_only)3533 l2arc_hdr_arcstats_update(arc_buf_hdr_t *hdr, boolean_t incr,
3534     boolean_t state_only)
3535 {
3536 	uint64_t lsize = HDR_GET_LSIZE(hdr);
3537 	uint64_t psize = HDR_GET_PSIZE(hdr);
3538 	uint64_t asize = HDR_GET_L2SIZE(hdr);
3539 	arc_buf_contents_t type = hdr->b_type;
3540 	int64_t lsize_s;
3541 	int64_t psize_s;
3542 	int64_t asize_s;
3543 
3544 	/* For L2 we expect the header's b_l2size to be valid */
3545 	ASSERT3U(asize, >=, psize);
3546 
3547 	if (incr) {
3548 		lsize_s = lsize;
3549 		psize_s = psize;
3550 		asize_s = asize;
3551 	} else {
3552 		lsize_s = -lsize;
3553 		psize_s = -psize;
3554 		asize_s = -asize;
3555 	}
3556 
3557 	/* If the buffer is a prefetch, count it as such. */
3558 	if (HDR_PREFETCH(hdr)) {
3559 		ARCSTAT_INCR(arcstat_l2_prefetch_asize, asize_s);
3560 	} else {
3561 		/*
3562 		 * We use the value stored in the L2 header upon initial
3563 		 * caching in L2ARC. This value will be updated in case
3564 		 * an MRU/MRU_ghost buffer transitions to MFU but the L2ARC
3565 		 * metadata (log entry) cannot currently be updated. Having
3566 		 * the ARC state in the L2 header solves the problem of a
3567 		 * possibly absent L1 header (apparent in buffers restored
3568 		 * from persistent L2ARC).
3569 		 */
3570 		switch (hdr->b_l2hdr.b_arcs_state) {
3571 			case ARC_STATE_MRU_GHOST:
3572 			case ARC_STATE_MRU:
3573 				ARCSTAT_INCR(arcstat_l2_mru_asize, asize_s);
3574 				break;
3575 			case ARC_STATE_MFU_GHOST:
3576 			case ARC_STATE_MFU:
3577 				ARCSTAT_INCR(arcstat_l2_mfu_asize, asize_s);
3578 				break;
3579 			default:
3580 				break;
3581 		}
3582 	}
3583 
3584 	if (state_only)
3585 		return;
3586 
3587 	ARCSTAT_INCR(arcstat_l2_psize, psize_s);
3588 	ARCSTAT_INCR(arcstat_l2_lsize, lsize_s);
3589 
3590 	switch (type) {
3591 		case ARC_BUFC_DATA:
3592 			ARCSTAT_INCR(arcstat_l2_bufc_data_asize, asize_s);
3593 			break;
3594 		case ARC_BUFC_METADATA:
3595 			ARCSTAT_INCR(arcstat_l2_bufc_metadata_asize, asize_s);
3596 			break;
3597 		default:
3598 			break;
3599 	}
3600 }
3601 
3602 
3603 static void
arc_hdr_l2hdr_destroy(arc_buf_hdr_t * hdr)3604 arc_hdr_l2hdr_destroy(arc_buf_hdr_t *hdr)
3605 {
3606 	l2arc_buf_hdr_t *l2hdr = &hdr->b_l2hdr;
3607 	l2arc_dev_t *dev = l2hdr->b_dev;
3608 
3609 	ASSERT(MUTEX_HELD(&dev->l2ad_mtx));
3610 	ASSERT(HDR_HAS_L2HDR(hdr));
3611 
3612 	list_remove(&dev->l2ad_buflist, hdr);
3613 
3614 	l2arc_hdr_arcstats_decrement(hdr);
3615 	if (dev->l2ad_vdev != NULL) {
3616 		uint64_t asize = HDR_GET_L2SIZE(hdr);
3617 		vdev_space_update(dev->l2ad_vdev, -asize, 0, 0);
3618 	}
3619 
3620 	(void) zfs_refcount_remove_many(&dev->l2ad_alloc, arc_hdr_size(hdr),
3621 	    hdr);
3622 	arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR);
3623 }
3624 
3625 static void
arc_hdr_destroy(arc_buf_hdr_t * hdr)3626 arc_hdr_destroy(arc_buf_hdr_t *hdr)
3627 {
3628 	if (HDR_HAS_L1HDR(hdr)) {
3629 		ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
3630 		ASSERT3P(hdr->b_l1hdr.b_state, ==, arc_anon);
3631 	}
3632 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3633 	ASSERT(!HDR_IN_HASH_TABLE(hdr));
3634 
3635 	if (HDR_HAS_L2HDR(hdr)) {
3636 		l2arc_dev_t *dev = hdr->b_l2hdr.b_dev;
3637 		boolean_t buflist_held = MUTEX_HELD(&dev->l2ad_mtx);
3638 
3639 		if (!buflist_held)
3640 			mutex_enter(&dev->l2ad_mtx);
3641 
3642 		/*
3643 		 * Even though we checked this conditional above, we
3644 		 * need to check this again now that we have the
3645 		 * l2ad_mtx. This is because we could be racing with
3646 		 * another thread calling l2arc_evict() which might have
3647 		 * destroyed this header's L2 portion as we were waiting
3648 		 * to acquire the l2ad_mtx. If that happens, we don't
3649 		 * want to re-destroy the header's L2 portion.
3650 		 */
3651 		if (HDR_HAS_L2HDR(hdr)) {
3652 
3653 			if (!HDR_EMPTY(hdr))
3654 				buf_discard_identity(hdr);
3655 
3656 			arc_hdr_l2hdr_destroy(hdr);
3657 		}
3658 
3659 		if (!buflist_held)
3660 			mutex_exit(&dev->l2ad_mtx);
3661 	}
3662 
3663 	/*
3664 	 * The header's identify can only be safely discarded once it is no
3665 	 * longer discoverable.  This requires removing it from the hash table
3666 	 * and the l2arc header list.  After this point the hash lock can not
3667 	 * be used to protect the header.
3668 	 */
3669 	if (!HDR_EMPTY(hdr))
3670 		buf_discard_identity(hdr);
3671 
3672 	if (HDR_HAS_L1HDR(hdr)) {
3673 		arc_cksum_free(hdr);
3674 
3675 		while (hdr->b_l1hdr.b_buf != NULL)
3676 			arc_buf_destroy_impl(hdr->b_l1hdr.b_buf);
3677 
3678 		if (hdr->b_l1hdr.b_pabd != NULL)
3679 			arc_hdr_free_abd(hdr, B_FALSE);
3680 
3681 		if (HDR_HAS_RABD(hdr))
3682 			arc_hdr_free_abd(hdr, B_TRUE);
3683 	}
3684 
3685 	ASSERT3P(hdr->b_hash_next, ==, NULL);
3686 	if (HDR_HAS_L1HDR(hdr)) {
3687 		ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
3688 		ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
3689 #ifdef ZFS_DEBUG
3690 		ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL);
3691 #endif
3692 		kmem_cache_free(hdr_full_cache, hdr);
3693 	} else {
3694 		kmem_cache_free(hdr_l2only_cache, hdr);
3695 	}
3696 }
3697 
3698 void
arc_buf_destroy(arc_buf_t * buf,const void * tag)3699 arc_buf_destroy(arc_buf_t *buf, const void *tag)
3700 {
3701 	arc_buf_hdr_t *hdr = buf->b_hdr;
3702 
3703 	if (hdr->b_l1hdr.b_state == arc_anon) {
3704 		ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf);
3705 		ASSERT(ARC_BUF_LAST(buf));
3706 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3707 		VERIFY0(remove_reference(hdr, tag));
3708 		return;
3709 	}
3710 
3711 	kmutex_t *hash_lock = HDR_LOCK(hdr);
3712 	mutex_enter(hash_lock);
3713 
3714 	ASSERT3P(hdr, ==, buf->b_hdr);
3715 	ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL);
3716 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3717 	ASSERT3P(hdr->b_l1hdr.b_state, !=, arc_anon);
3718 	ASSERT3P(buf->b_data, !=, NULL);
3719 
3720 	arc_buf_destroy_impl(buf);
3721 	(void) remove_reference(hdr, tag);
3722 	mutex_exit(hash_lock);
3723 }
3724 
3725 /*
3726  * Evict the arc_buf_hdr that is provided as a parameter. The resultant
3727  * state of the header is dependent on its state prior to entering this
3728  * function. The following transitions are possible:
3729  *
3730  *    - arc_mru -> arc_mru_ghost
3731  *    - arc_mfu -> arc_mfu_ghost
3732  *    - arc_mru_ghost -> arc_l2c_only
3733  *    - arc_mru_ghost -> deleted
3734  *    - arc_mfu_ghost -> arc_l2c_only
3735  *    - arc_mfu_ghost -> deleted
3736  *    - arc_uncached -> deleted
3737  *
3738  * Return total size of evicted data buffers for eviction progress tracking.
3739  * When evicting from ghost states return logical buffer size to make eviction
3740  * progress at the same (or at least comparable) rate as from non-ghost states.
3741  *
3742  * Return *real_evicted for actual ARC size reduction to wake up threads
3743  * waiting for it.  For non-ghost states it includes size of evicted data
3744  * buffers (the headers are not freed there).  For ghost states it includes
3745  * only the evicted headers size.
3746  */
3747 static int64_t
arc_evict_hdr(arc_buf_hdr_t * hdr,uint64_t * real_evicted)3748 arc_evict_hdr(arc_buf_hdr_t *hdr, uint64_t *real_evicted)
3749 {
3750 	arc_state_t *evicted_state, *state;
3751 	int64_t bytes_evicted = 0;
3752 	uint_t min_lifetime = HDR_PRESCIENT_PREFETCH(hdr) ?
3753 	    arc_min_prescient_prefetch_ms : arc_min_prefetch_ms;
3754 
3755 	ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
3756 	ASSERT(HDR_HAS_L1HDR(hdr));
3757 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3758 	ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
3759 	ASSERT0(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt));
3760 
3761 	*real_evicted = 0;
3762 	state = hdr->b_l1hdr.b_state;
3763 	if (GHOST_STATE(state)) {
3764 
3765 		/*
3766 		 * l2arc_write_buffers() relies on a header's L1 portion
3767 		 * (i.e. its b_pabd field) during it's write phase.
3768 		 * Thus, we cannot push a header onto the arc_l2c_only
3769 		 * state (removing its L1 piece) until the header is
3770 		 * done being written to the l2arc.
3771 		 */
3772 		if (HDR_HAS_L2HDR(hdr) && HDR_L2_WRITING(hdr)) {
3773 			ARCSTAT_BUMP(arcstat_evict_l2_skip);
3774 			return (bytes_evicted);
3775 		}
3776 
3777 		ARCSTAT_BUMP(arcstat_deleted);
3778 		bytes_evicted += HDR_GET_LSIZE(hdr);
3779 
3780 		DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, hdr);
3781 
3782 		if (HDR_HAS_L2HDR(hdr)) {
3783 			ASSERT(hdr->b_l1hdr.b_pabd == NULL);
3784 			ASSERT(!HDR_HAS_RABD(hdr));
3785 			/*
3786 			 * This buffer is cached on the 2nd Level ARC;
3787 			 * don't destroy the header.
3788 			 */
3789 			arc_change_state(arc_l2c_only, hdr);
3790 			/*
3791 			 * dropping from L1+L2 cached to L2-only,
3792 			 * realloc to remove the L1 header.
3793 			 */
3794 			(void) arc_hdr_realloc(hdr, hdr_full_cache,
3795 			    hdr_l2only_cache);
3796 			*real_evicted += HDR_FULL_SIZE - HDR_L2ONLY_SIZE;
3797 		} else {
3798 			arc_change_state(arc_anon, hdr);
3799 			arc_hdr_destroy(hdr);
3800 			*real_evicted += HDR_FULL_SIZE;
3801 		}
3802 		return (bytes_evicted);
3803 	}
3804 
3805 	ASSERT(state == arc_mru || state == arc_mfu || state == arc_uncached);
3806 	evicted_state = (state == arc_uncached) ? arc_anon :
3807 	    ((state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost);
3808 
3809 	/* prefetch buffers have a minimum lifespan */
3810 	if ((hdr->b_flags & (ARC_FLAG_PREFETCH | ARC_FLAG_INDIRECT)) &&
3811 	    ddi_get_lbolt() - hdr->b_l1hdr.b_arc_access <
3812 	    MSEC_TO_TICK(min_lifetime)) {
3813 		ARCSTAT_BUMP(arcstat_evict_skip);
3814 		return (bytes_evicted);
3815 	}
3816 
3817 	if (HDR_HAS_L2HDR(hdr)) {
3818 		ARCSTAT_INCR(arcstat_evict_l2_cached, HDR_GET_LSIZE(hdr));
3819 	} else {
3820 		if (l2arc_write_eligible(hdr->b_spa, hdr)) {
3821 			ARCSTAT_INCR(arcstat_evict_l2_eligible,
3822 			    HDR_GET_LSIZE(hdr));
3823 
3824 			switch (state->arcs_state) {
3825 				case ARC_STATE_MRU:
3826 					ARCSTAT_INCR(
3827 					    arcstat_evict_l2_eligible_mru,
3828 					    HDR_GET_LSIZE(hdr));
3829 					break;
3830 				case ARC_STATE_MFU:
3831 					ARCSTAT_INCR(
3832 					    arcstat_evict_l2_eligible_mfu,
3833 					    HDR_GET_LSIZE(hdr));
3834 					break;
3835 				default:
3836 					break;
3837 			}
3838 		} else {
3839 			ARCSTAT_INCR(arcstat_evict_l2_ineligible,
3840 			    HDR_GET_LSIZE(hdr));
3841 		}
3842 	}
3843 
3844 	bytes_evicted += arc_hdr_size(hdr);
3845 	*real_evicted += arc_hdr_size(hdr);
3846 
3847 	/*
3848 	 * If this hdr is being evicted and has a compressed buffer then we
3849 	 * discard it here before we change states.  This ensures that the
3850 	 * accounting is updated correctly in arc_free_data_impl().
3851 	 */
3852 	if (hdr->b_l1hdr.b_pabd != NULL)
3853 		arc_hdr_free_abd(hdr, B_FALSE);
3854 
3855 	if (HDR_HAS_RABD(hdr))
3856 		arc_hdr_free_abd(hdr, B_TRUE);
3857 
3858 	arc_change_state(evicted_state, hdr);
3859 	DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, hdr);
3860 	if (evicted_state == arc_anon) {
3861 		arc_hdr_destroy(hdr);
3862 		*real_evicted += HDR_FULL_SIZE;
3863 	} else {
3864 		ASSERT(HDR_IN_HASH_TABLE(hdr));
3865 	}
3866 
3867 	return (bytes_evicted);
3868 }
3869 
3870 static void
arc_set_need_free(void)3871 arc_set_need_free(void)
3872 {
3873 	ASSERT(MUTEX_HELD(&arc_evict_lock));
3874 	int64_t remaining = arc_free_memory() - arc_sys_free / 2;
3875 	arc_evict_waiter_t *aw = list_tail(&arc_evict_waiters);
3876 	if (aw == NULL) {
3877 		arc_need_free = MAX(-remaining, 0);
3878 	} else {
3879 		arc_need_free =
3880 		    MAX(-remaining, (int64_t)(aw->aew_count - arc_evict_count));
3881 	}
3882 }
3883 
3884 static uint64_t
arc_evict_state_impl(multilist_t * ml,int idx,arc_buf_hdr_t * marker,uint64_t spa,uint64_t bytes)3885 arc_evict_state_impl(multilist_t *ml, int idx, arc_buf_hdr_t *marker,
3886     uint64_t spa, uint64_t bytes)
3887 {
3888 	multilist_sublist_t *mls;
3889 	uint64_t bytes_evicted = 0, real_evicted = 0;
3890 	arc_buf_hdr_t *hdr;
3891 	kmutex_t *hash_lock;
3892 	uint_t evict_count = zfs_arc_evict_batch_limit;
3893 
3894 	ASSERT3P(marker, !=, NULL);
3895 
3896 	mls = multilist_sublist_lock_idx(ml, idx);
3897 
3898 	for (hdr = multilist_sublist_prev(mls, marker); likely(hdr != NULL);
3899 	    hdr = multilist_sublist_prev(mls, marker)) {
3900 		if ((evict_count == 0) || (bytes_evicted >= bytes))
3901 			break;
3902 
3903 		/*
3904 		 * To keep our iteration location, move the marker
3905 		 * forward. Since we're not holding hdr's hash lock, we
3906 		 * must be very careful and not remove 'hdr' from the
3907 		 * sublist. Otherwise, other consumers might mistake the
3908 		 * 'hdr' as not being on a sublist when they call the
3909 		 * multilist_link_active() function (they all rely on
3910 		 * the hash lock protecting concurrent insertions and
3911 		 * removals). multilist_sublist_move_forward() was
3912 		 * specifically implemented to ensure this is the case
3913 		 * (only 'marker' will be removed and re-inserted).
3914 		 */
3915 		multilist_sublist_move_forward(mls, marker);
3916 
3917 		/*
3918 		 * The only case where the b_spa field should ever be
3919 		 * zero, is the marker headers inserted by
3920 		 * arc_evict_state(). It's possible for multiple threads
3921 		 * to be calling arc_evict_state() concurrently (e.g.
3922 		 * dsl_pool_close() and zio_inject_fault()), so we must
3923 		 * skip any markers we see from these other threads.
3924 		 */
3925 		if (hdr->b_spa == 0)
3926 			continue;
3927 
3928 		/* we're only interested in evicting buffers of a certain spa */
3929 		if (spa != 0 && hdr->b_spa != spa) {
3930 			ARCSTAT_BUMP(arcstat_evict_skip);
3931 			continue;
3932 		}
3933 
3934 		hash_lock = HDR_LOCK(hdr);
3935 
3936 		/*
3937 		 * We aren't calling this function from any code path
3938 		 * that would already be holding a hash lock, so we're
3939 		 * asserting on this assumption to be defensive in case
3940 		 * this ever changes. Without this check, it would be
3941 		 * possible to incorrectly increment arcstat_mutex_miss
3942 		 * below (e.g. if the code changed such that we called
3943 		 * this function with a hash lock held).
3944 		 */
3945 		ASSERT(!MUTEX_HELD(hash_lock));
3946 
3947 		if (mutex_tryenter(hash_lock)) {
3948 			uint64_t revicted;
3949 			uint64_t evicted = arc_evict_hdr(hdr, &revicted);
3950 			mutex_exit(hash_lock);
3951 
3952 			bytes_evicted += evicted;
3953 			real_evicted += revicted;
3954 
3955 			/*
3956 			 * If evicted is zero, arc_evict_hdr() must have
3957 			 * decided to skip this header, don't increment
3958 			 * evict_count in this case.
3959 			 */
3960 			if (evicted != 0)
3961 				evict_count--;
3962 
3963 		} else {
3964 			ARCSTAT_BUMP(arcstat_mutex_miss);
3965 		}
3966 	}
3967 
3968 	multilist_sublist_unlock(mls);
3969 
3970 	/*
3971 	 * Increment the count of evicted bytes, and wake up any threads that
3972 	 * are waiting for the count to reach this value.  Since the list is
3973 	 * ordered by ascending aew_count, we pop off the beginning of the
3974 	 * list until we reach the end, or a waiter that's past the current
3975 	 * "count".  Doing this outside the loop reduces the number of times
3976 	 * we need to acquire the global arc_evict_lock.
3977 	 *
3978 	 * Only wake when there's sufficient free memory in the system
3979 	 * (specifically, arc_sys_free/2, which by default is a bit more than
3980 	 * 1/64th of RAM).  See the comments in arc_wait_for_eviction().
3981 	 */
3982 	mutex_enter(&arc_evict_lock);
3983 	arc_evict_count += real_evicted;
3984 
3985 	if (arc_free_memory() > arc_sys_free / 2) {
3986 		arc_evict_waiter_t *aw;
3987 		while ((aw = list_head(&arc_evict_waiters)) != NULL &&
3988 		    aw->aew_count <= arc_evict_count) {
3989 			list_remove(&arc_evict_waiters, aw);
3990 			cv_broadcast(&aw->aew_cv);
3991 		}
3992 	}
3993 	arc_set_need_free();
3994 	mutex_exit(&arc_evict_lock);
3995 
3996 	/*
3997 	 * If the ARC size is reduced from arc_c_max to arc_c_min (especially
3998 	 * if the average cached block is small), eviction can be on-CPU for
3999 	 * many seconds.  To ensure that other threads that may be bound to
4000 	 * this CPU are able to make progress, make a voluntary preemption
4001 	 * call here.
4002 	 */
4003 	kpreempt(KPREEMPT_SYNC);
4004 
4005 	return (bytes_evicted);
4006 }
4007 
4008 static arc_buf_hdr_t *
arc_state_alloc_marker(void)4009 arc_state_alloc_marker(void)
4010 {
4011 	arc_buf_hdr_t *marker = kmem_cache_alloc(hdr_full_cache, KM_SLEEP);
4012 
4013 	/*
4014 	 * A b_spa of 0 is used to indicate that this header is
4015 	 * a marker. This fact is used in arc_evict_state_impl().
4016 	 */
4017 	marker->b_spa = 0;
4018 
4019 	return (marker);
4020 }
4021 
4022 static void
arc_state_free_marker(arc_buf_hdr_t * marker)4023 arc_state_free_marker(arc_buf_hdr_t *marker)
4024 {
4025 	kmem_cache_free(hdr_full_cache, marker);
4026 }
4027 
4028 /*
4029  * Allocate an array of buffer headers used as placeholders during arc state
4030  * eviction.
4031  */
4032 static arc_buf_hdr_t **
arc_state_alloc_markers(int count)4033 arc_state_alloc_markers(int count)
4034 {
4035 	arc_buf_hdr_t **markers;
4036 
4037 	markers = kmem_zalloc(sizeof (*markers) * count, KM_SLEEP);
4038 	for (int i = 0; i < count; i++)
4039 		markers[i] = arc_state_alloc_marker();
4040 	return (markers);
4041 }
4042 
4043 static void
arc_state_free_markers(arc_buf_hdr_t ** markers,int count)4044 arc_state_free_markers(arc_buf_hdr_t **markers, int count)
4045 {
4046 	for (int i = 0; i < count; i++)
4047 		arc_state_free_marker(markers[i]);
4048 	kmem_free(markers, sizeof (*markers) * count);
4049 }
4050 
4051 /*
4052  * Evict buffers from the given arc state, until we've removed the
4053  * specified number of bytes. Move the removed buffers to the
4054  * appropriate evict state.
4055  *
4056  * This function makes a "best effort". It skips over any buffers
4057  * it can't get a hash_lock on, and so, may not catch all candidates.
4058  * It may also return without evicting as much space as requested.
4059  *
4060  * If bytes is specified using the special value ARC_EVICT_ALL, this
4061  * will evict all available (i.e. unlocked and evictable) buffers from
4062  * the given arc state; which is used by arc_flush().
4063  */
4064 static uint64_t
arc_evict_state(arc_state_t * state,arc_buf_contents_t type,uint64_t spa,uint64_t bytes)4065 arc_evict_state(arc_state_t *state, arc_buf_contents_t type, uint64_t spa,
4066     uint64_t bytes)
4067 {
4068 	uint64_t total_evicted = 0;
4069 	multilist_t *ml = &state->arcs_list[type];
4070 	int num_sublists;
4071 	arc_buf_hdr_t **markers;
4072 
4073 	num_sublists = multilist_get_num_sublists(ml);
4074 
4075 	/*
4076 	 * If we've tried to evict from each sublist, made some
4077 	 * progress, but still have not hit the target number of bytes
4078 	 * to evict, we want to keep trying. The markers allow us to
4079 	 * pick up where we left off for each individual sublist, rather
4080 	 * than starting from the tail each time.
4081 	 */
4082 	if (zthr_iscurthread(arc_evict_zthr)) {
4083 		markers = arc_state_evict_markers;
4084 		ASSERT3S(num_sublists, <=, arc_state_evict_marker_count);
4085 	} else {
4086 		markers = arc_state_alloc_markers(num_sublists);
4087 	}
4088 	for (int i = 0; i < num_sublists; i++) {
4089 		multilist_sublist_t *mls;
4090 
4091 		mls = multilist_sublist_lock_idx(ml, i);
4092 		multilist_sublist_insert_tail(mls, markers[i]);
4093 		multilist_sublist_unlock(mls);
4094 	}
4095 
4096 	/*
4097 	 * While we haven't hit our target number of bytes to evict, or
4098 	 * we're evicting all available buffers.
4099 	 */
4100 	while (total_evicted < bytes) {
4101 		int sublist_idx = multilist_get_random_index(ml);
4102 		uint64_t scan_evicted = 0;
4103 
4104 		/*
4105 		 * Start eviction using a randomly selected sublist,
4106 		 * this is to try and evenly balance eviction across all
4107 		 * sublists. Always starting at the same sublist
4108 		 * (e.g. index 0) would cause evictions to favor certain
4109 		 * sublists over others.
4110 		 */
4111 		for (int i = 0; i < num_sublists; i++) {
4112 			uint64_t bytes_remaining;
4113 			uint64_t bytes_evicted;
4114 
4115 			if (total_evicted < bytes)
4116 				bytes_remaining = bytes - total_evicted;
4117 			else
4118 				break;
4119 
4120 			bytes_evicted = arc_evict_state_impl(ml, sublist_idx,
4121 			    markers[sublist_idx], spa, bytes_remaining);
4122 
4123 			scan_evicted += bytes_evicted;
4124 			total_evicted += bytes_evicted;
4125 
4126 			/* we've reached the end, wrap to the beginning */
4127 			if (++sublist_idx >= num_sublists)
4128 				sublist_idx = 0;
4129 		}
4130 
4131 		/*
4132 		 * If we didn't evict anything during this scan, we have
4133 		 * no reason to believe we'll evict more during another
4134 		 * scan, so break the loop.
4135 		 */
4136 		if (scan_evicted == 0) {
4137 			/* This isn't possible, let's make that obvious */
4138 			ASSERT3S(bytes, !=, 0);
4139 
4140 			/*
4141 			 * When bytes is ARC_EVICT_ALL, the only way to
4142 			 * break the loop is when scan_evicted is zero.
4143 			 * In that case, we actually have evicted enough,
4144 			 * so we don't want to increment the kstat.
4145 			 */
4146 			if (bytes != ARC_EVICT_ALL) {
4147 				ASSERT3S(total_evicted, <, bytes);
4148 				ARCSTAT_BUMP(arcstat_evict_not_enough);
4149 			}
4150 
4151 			break;
4152 		}
4153 	}
4154 
4155 	for (int i = 0; i < num_sublists; i++) {
4156 		multilist_sublist_t *mls = multilist_sublist_lock_idx(ml, i);
4157 		multilist_sublist_remove(mls, markers[i]);
4158 		multilist_sublist_unlock(mls);
4159 	}
4160 	if (markers != arc_state_evict_markers)
4161 		arc_state_free_markers(markers, num_sublists);
4162 
4163 	return (total_evicted);
4164 }
4165 
4166 /*
4167  * Flush all "evictable" data of the given type from the arc state
4168  * specified. This will not evict any "active" buffers (i.e. referenced).
4169  *
4170  * When 'retry' is set to B_FALSE, the function will make a single pass
4171  * over the state and evict any buffers that it can. Since it doesn't
4172  * continually retry the eviction, it might end up leaving some buffers
4173  * in the ARC due to lock misses.
4174  *
4175  * When 'retry' is set to B_TRUE, the function will continually retry the
4176  * eviction until *all* evictable buffers have been removed from the
4177  * state. As a result, if concurrent insertions into the state are
4178  * allowed (e.g. if the ARC isn't shutting down), this function might
4179  * wind up in an infinite loop, continually trying to evict buffers.
4180  */
4181 static uint64_t
arc_flush_state(arc_state_t * state,uint64_t spa,arc_buf_contents_t type,boolean_t retry)4182 arc_flush_state(arc_state_t *state, uint64_t spa, arc_buf_contents_t type,
4183     boolean_t retry)
4184 {
4185 	uint64_t evicted = 0;
4186 
4187 	while (zfs_refcount_count(&state->arcs_esize[type]) != 0) {
4188 		evicted += arc_evict_state(state, type, spa, ARC_EVICT_ALL);
4189 
4190 		if (!retry)
4191 			break;
4192 	}
4193 
4194 	return (evicted);
4195 }
4196 
4197 /*
4198  * Evict the specified number of bytes from the state specified. This
4199  * function prevents us from trying to evict more from a state's list
4200  * than is "evictable", and to skip evicting altogether when passed a
4201  * negative value for "bytes". In contrast, arc_evict_state() will
4202  * evict everything it can, when passed a negative value for "bytes".
4203  */
4204 static uint64_t
arc_evict_impl(arc_state_t * state,arc_buf_contents_t type,int64_t bytes)4205 arc_evict_impl(arc_state_t *state, arc_buf_contents_t type, int64_t bytes)
4206 {
4207 	uint64_t delta;
4208 
4209 	if (bytes > 0 && zfs_refcount_count(&state->arcs_esize[type]) > 0) {
4210 		delta = MIN(zfs_refcount_count(&state->arcs_esize[type]),
4211 		    bytes);
4212 		return (arc_evict_state(state, type, 0, delta));
4213 	}
4214 
4215 	return (0);
4216 }
4217 
4218 /*
4219  * Adjust specified fraction, taking into account initial ghost state(s) size,
4220  * ghost hit bytes towards increasing the fraction, ghost hit bytes towards
4221  * decreasing it, plus a balance factor, controlling the decrease rate, used
4222  * to balance metadata vs data.
4223  */
4224 static uint64_t
arc_evict_adj(uint64_t frac,uint64_t total,uint64_t up,uint64_t down,uint_t balance)4225 arc_evict_adj(uint64_t frac, uint64_t total, uint64_t up, uint64_t down,
4226     uint_t balance)
4227 {
4228 	if (total < 8 || up + down == 0)
4229 		return (frac);
4230 
4231 	/*
4232 	 * We should not have more ghost hits than ghost size, but they
4233 	 * may get close.  Restrict maximum adjustment in that case.
4234 	 */
4235 	if (up + down >= total / 4) {
4236 		uint64_t scale = (up + down) / (total / 8);
4237 		up /= scale;
4238 		down /= scale;
4239 	}
4240 
4241 	/* Get maximal dynamic range by choosing optimal shifts. */
4242 	int s = highbit64(total);
4243 	s = MIN(64 - s, 32);
4244 
4245 	uint64_t ofrac = (1ULL << 32) - frac;
4246 
4247 	if (frac >= 4 * ofrac)
4248 		up /= frac / (2 * ofrac + 1);
4249 	up = (up << s) / (total >> (32 - s));
4250 	if (ofrac >= 4 * frac)
4251 		down /= ofrac / (2 * frac + 1);
4252 	down = (down << s) / (total >> (32 - s));
4253 	down = down * 100 / balance;
4254 
4255 	return (frac + up - down);
4256 }
4257 
4258 /*
4259  * Calculate (x * multiplier / divisor) without unnecesary overflows.
4260  */
4261 static uint64_t
arc_mf(uint64_t x,uint64_t multiplier,uint64_t divisor)4262 arc_mf(uint64_t x, uint64_t multiplier, uint64_t divisor)
4263 {
4264 	uint64_t q = (x / divisor);
4265 	uint64_t r = (x % divisor);
4266 
4267 	return ((q * multiplier) + ((r * multiplier) / divisor));
4268 }
4269 
4270 /*
4271  * Evict buffers from the cache, such that arcstat_size is capped by arc_c.
4272  */
4273 static uint64_t
arc_evict(void)4274 arc_evict(void)
4275 {
4276 	uint64_t bytes, total_evicted = 0;
4277 	int64_t e, mrud, mrum, mfud, mfum, w;
4278 	static uint64_t ogrd, ogrm, ogfd, ogfm;
4279 	static uint64_t gsrd, gsrm, gsfd, gsfm;
4280 	uint64_t ngrd, ngrm, ngfd, ngfm;
4281 
4282 	/* Get current size of ARC states we can evict from. */
4283 	mrud = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_DATA]) +
4284 	    zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_DATA]);
4285 	mrum = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_METADATA]) +
4286 	    zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_METADATA]);
4287 	mfud = zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_DATA]);
4288 	mfum = zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_METADATA]);
4289 	uint64_t d = mrud + mfud;
4290 	uint64_t m = mrum + mfum;
4291 	uint64_t t = d + m;
4292 
4293 	/* Get ARC ghost hits since last eviction. */
4294 	ngrd = wmsum_value(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA]);
4295 	uint64_t grd = ngrd - ogrd;
4296 	ogrd = ngrd;
4297 	ngrm = wmsum_value(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA]);
4298 	uint64_t grm = ngrm - ogrm;
4299 	ogrm = ngrm;
4300 	ngfd = wmsum_value(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA]);
4301 	uint64_t gfd = ngfd - ogfd;
4302 	ogfd = ngfd;
4303 	ngfm = wmsum_value(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA]);
4304 	uint64_t gfm = ngfm - ogfm;
4305 	ogfm = ngfm;
4306 
4307 	/* Adjust ARC states balance based on ghost hits. */
4308 	arc_meta = arc_evict_adj(arc_meta, gsrd + gsrm + gsfd + gsfm,
4309 	    grm + gfm, grd + gfd, zfs_arc_meta_balance);
4310 	arc_pd = arc_evict_adj(arc_pd, gsrd + gsfd, grd, gfd, 100);
4311 	arc_pm = arc_evict_adj(arc_pm, gsrm + gsfm, grm, gfm, 100);
4312 
4313 	uint64_t asize = aggsum_value(&arc_sums.arcstat_size);
4314 	uint64_t ac = arc_c;
4315 	int64_t wt = t - (asize - ac);
4316 
4317 	/*
4318 	 * Try to reduce pinned dnodes if more than 3/4 of wanted metadata
4319 	 * target is not evictable or if they go over arc_dnode_limit.
4320 	 */
4321 	int64_t prune = 0;
4322 	int64_t dn = wmsum_value(&arc_sums.arcstat_dnode_size);
4323 	int64_t nem = zfs_refcount_count(&arc_mru->arcs_size[ARC_BUFC_METADATA])
4324 	    + zfs_refcount_count(&arc_mfu->arcs_size[ARC_BUFC_METADATA])
4325 	    - zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_METADATA])
4326 	    - zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]);
4327 	w = wt * (int64_t)(arc_meta >> 16) >> 16;
4328 	if (nem > w * 3 / 4) {
4329 		prune = dn / sizeof (dnode_t) *
4330 		    zfs_arc_dnode_reduce_percent / 100;
4331 		if (nem < w && w > 4)
4332 			prune = arc_mf(prune, nem - w * 3 / 4, w / 4);
4333 	}
4334 	if (dn > arc_dnode_limit) {
4335 		prune = MAX(prune, (dn - arc_dnode_limit) / sizeof (dnode_t) *
4336 		    zfs_arc_dnode_reduce_percent / 100);
4337 	}
4338 	if (prune > 0)
4339 		arc_prune_async(prune);
4340 
4341 	/* Evict MRU metadata. */
4342 	w = wt * (int64_t)(arc_meta * arc_pm >> 48) >> 16;
4343 	e = MIN((int64_t)(asize - ac), (int64_t)(mrum - w));
4344 	bytes = arc_evict_impl(arc_mru, ARC_BUFC_METADATA, e);
4345 	total_evicted += bytes;
4346 	mrum -= bytes;
4347 	asize -= bytes;
4348 
4349 	/* Evict MFU metadata. */
4350 	w = wt * (int64_t)(arc_meta >> 16) >> 16;
4351 	e = MIN((int64_t)(asize - ac), (int64_t)(m - bytes - w));
4352 	bytes = arc_evict_impl(arc_mfu, ARC_BUFC_METADATA, e);
4353 	total_evicted += bytes;
4354 	mfum -= bytes;
4355 	asize -= bytes;
4356 
4357 	/* Evict MRU data. */
4358 	wt -= m - total_evicted;
4359 	w = wt * (int64_t)(arc_pd >> 16) >> 16;
4360 	e = MIN((int64_t)(asize - ac), (int64_t)(mrud - w));
4361 	bytes = arc_evict_impl(arc_mru, ARC_BUFC_DATA, e);
4362 	total_evicted += bytes;
4363 	mrud -= bytes;
4364 	asize -= bytes;
4365 
4366 	/* Evict MFU data. */
4367 	e = asize - ac;
4368 	bytes = arc_evict_impl(arc_mfu, ARC_BUFC_DATA, e);
4369 	mfud -= bytes;
4370 	total_evicted += bytes;
4371 
4372 	/*
4373 	 * Evict ghost lists
4374 	 *
4375 	 * Size of each state's ghost list represents how much that state
4376 	 * may grow by shrinking the other states.  Would it need to shrink
4377 	 * other states to zero (that is unlikely), its ghost size would be
4378 	 * equal to sum of other three state sizes.  But excessive ghost
4379 	 * size may result in false ghost hits (too far back), that may
4380 	 * never result in real cache hits if several states are competing.
4381 	 * So choose some arbitraty point of 1/2 of other state sizes.
4382 	 */
4383 	gsrd = (mrum + mfud + mfum) / 2;
4384 	e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]) -
4385 	    gsrd;
4386 	(void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_DATA, e);
4387 
4388 	gsrm = (mrud + mfud + mfum) / 2;
4389 	e = zfs_refcount_count(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]) -
4390 	    gsrm;
4391 	(void) arc_evict_impl(arc_mru_ghost, ARC_BUFC_METADATA, e);
4392 
4393 	gsfd = (mrud + mrum + mfum) / 2;
4394 	e = zfs_refcount_count(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]) -
4395 	    gsfd;
4396 	(void) arc_evict_impl(arc_mfu_ghost, ARC_BUFC_DATA, e);
4397 
4398 	gsfm = (mrud + mrum + mfud) / 2;
4399 	e = zfs_refcount_count(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]) -
4400 	    gsfm;
4401 	(void) arc_evict_impl(arc_mfu_ghost, ARC_BUFC_METADATA, e);
4402 
4403 	return (total_evicted);
4404 }
4405 
4406 static void
arc_flush_impl(uint64_t guid,boolean_t retry)4407 arc_flush_impl(uint64_t guid, boolean_t retry)
4408 {
4409 	ASSERT(!retry || guid == 0);
4410 
4411 	(void) arc_flush_state(arc_mru, guid, ARC_BUFC_DATA, retry);
4412 	(void) arc_flush_state(arc_mru, guid, ARC_BUFC_METADATA, retry);
4413 
4414 	(void) arc_flush_state(arc_mfu, guid, ARC_BUFC_DATA, retry);
4415 	(void) arc_flush_state(arc_mfu, guid, ARC_BUFC_METADATA, retry);
4416 
4417 	(void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_DATA, retry);
4418 	(void) arc_flush_state(arc_mru_ghost, guid, ARC_BUFC_METADATA, retry);
4419 
4420 	(void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_DATA, retry);
4421 	(void) arc_flush_state(arc_mfu_ghost, guid, ARC_BUFC_METADATA, retry);
4422 
4423 	(void) arc_flush_state(arc_uncached, guid, ARC_BUFC_DATA, retry);
4424 	(void) arc_flush_state(arc_uncached, guid, ARC_BUFC_METADATA, retry);
4425 }
4426 
4427 void
arc_flush(spa_t * spa,boolean_t retry)4428 arc_flush(spa_t *spa, boolean_t retry)
4429 {
4430 	/*
4431 	 * If retry is B_TRUE, a spa must not be specified since we have
4432 	 * no good way to determine if all of a spa's buffers have been
4433 	 * evicted from an arc state.
4434 	 */
4435 	ASSERT(!retry || spa == NULL);
4436 
4437 	arc_flush_impl(spa != NULL ? spa_load_guid(spa) : 0, retry);
4438 }
4439 
4440 static arc_async_flush_t *
arc_async_flush_add(uint64_t spa_guid,uint_t level)4441 arc_async_flush_add(uint64_t spa_guid, uint_t level)
4442 {
4443 	arc_async_flush_t *af = kmem_alloc(sizeof (*af), KM_SLEEP);
4444 	af->af_spa_guid = spa_guid;
4445 	af->af_cache_level = level;
4446 	taskq_init_ent(&af->af_tqent);
4447 	list_link_init(&af->af_node);
4448 
4449 	mutex_enter(&arc_async_flush_lock);
4450 	list_insert_tail(&arc_async_flush_list, af);
4451 	mutex_exit(&arc_async_flush_lock);
4452 
4453 	return (af);
4454 }
4455 
4456 static void
arc_async_flush_remove(uint64_t spa_guid,uint_t level)4457 arc_async_flush_remove(uint64_t spa_guid, uint_t level)
4458 {
4459 	mutex_enter(&arc_async_flush_lock);
4460 	for (arc_async_flush_t *af = list_head(&arc_async_flush_list);
4461 	    af != NULL; af = list_next(&arc_async_flush_list, af)) {
4462 		if (af->af_spa_guid == spa_guid &&
4463 		    af->af_cache_level == level) {
4464 			list_remove(&arc_async_flush_list, af);
4465 			kmem_free(af, sizeof (*af));
4466 			break;
4467 		}
4468 	}
4469 	mutex_exit(&arc_async_flush_lock);
4470 }
4471 
4472 static void
arc_flush_task(void * arg)4473 arc_flush_task(void *arg)
4474 {
4475 	arc_async_flush_t *af = arg;
4476 	hrtime_t start_time = gethrtime();
4477 	uint64_t spa_guid = af->af_spa_guid;
4478 
4479 	arc_flush_impl(spa_guid, B_FALSE);
4480 	arc_async_flush_remove(spa_guid, af->af_cache_level);
4481 
4482 	uint64_t elaspsed = NSEC2MSEC(gethrtime() - start_time);
4483 	if (elaspsed > 0) {
4484 		zfs_dbgmsg("spa %llu arc flushed in %llu ms",
4485 		    (u_longlong_t)spa_guid, (u_longlong_t)elaspsed);
4486 	}
4487 }
4488 
4489 /*
4490  * ARC buffers use the spa's load guid and can continue to exist after
4491  * the spa_t is gone (exported). The blocks are orphaned since each
4492  * spa import has a different load guid.
4493  *
4494  * It's OK if the spa is re-imported while this asynchronous flush is
4495  * still in progress. The new spa_load_guid will be different.
4496  *
4497  * Also, arc_fini will wait for any arc_flush_task to finish.
4498  */
4499 void
arc_flush_async(spa_t * spa)4500 arc_flush_async(spa_t *spa)
4501 {
4502 	uint64_t spa_guid = spa_load_guid(spa);
4503 	arc_async_flush_t *af = arc_async_flush_add(spa_guid, 1);
4504 
4505 	taskq_dispatch_ent(arc_flush_taskq, arc_flush_task,
4506 	    af, TQ_SLEEP, &af->af_tqent);
4507 }
4508 
4509 /*
4510  * Check if a guid is still in-use as part of an async teardown task
4511  */
4512 boolean_t
arc_async_flush_guid_inuse(uint64_t spa_guid)4513 arc_async_flush_guid_inuse(uint64_t spa_guid)
4514 {
4515 	mutex_enter(&arc_async_flush_lock);
4516 	for (arc_async_flush_t *af = list_head(&arc_async_flush_list);
4517 	    af != NULL; af = list_next(&arc_async_flush_list, af)) {
4518 		if (af->af_spa_guid == spa_guid) {
4519 			mutex_exit(&arc_async_flush_lock);
4520 			return (B_TRUE);
4521 		}
4522 	}
4523 	mutex_exit(&arc_async_flush_lock);
4524 	return (B_FALSE);
4525 }
4526 
4527 uint64_t
arc_reduce_target_size(uint64_t to_free)4528 arc_reduce_target_size(uint64_t to_free)
4529 {
4530 	/*
4531 	 * Get the actual arc size.  Even if we don't need it, this updates
4532 	 * the aggsum lower bound estimate for arc_is_overflowing().
4533 	 */
4534 	uint64_t asize = aggsum_value(&arc_sums.arcstat_size);
4535 
4536 	/*
4537 	 * All callers want the ARC to actually evict (at least) this much
4538 	 * memory.  Therefore we reduce from the lower of the current size and
4539 	 * the target size.  This way, even if arc_c is much higher than
4540 	 * arc_size (as can be the case after many calls to arc_freed(), we will
4541 	 * immediately have arc_c < arc_size and therefore the arc_evict_zthr
4542 	 * will evict.
4543 	 */
4544 	uint64_t c = arc_c;
4545 	if (c > arc_c_min) {
4546 		c = MIN(c, MAX(asize, arc_c_min));
4547 		to_free = MIN(to_free, c - arc_c_min);
4548 		arc_c = c - to_free;
4549 	} else {
4550 		to_free = 0;
4551 	}
4552 
4553 	/*
4554 	 * Whether or not we reduced the target size, request eviction if the
4555 	 * current size is over it now, since caller obviously wants some RAM.
4556 	 */
4557 	if (asize > arc_c) {
4558 		/* See comment in arc_evict_cb_check() on why lock+flag */
4559 		mutex_enter(&arc_evict_lock);
4560 		arc_evict_needed = B_TRUE;
4561 		mutex_exit(&arc_evict_lock);
4562 		zthr_wakeup(arc_evict_zthr);
4563 	}
4564 
4565 	return (to_free);
4566 }
4567 
4568 /*
4569  * Determine if the system is under memory pressure and is asking
4570  * to reclaim memory. A return value of B_TRUE indicates that the system
4571  * is under memory pressure and that the arc should adjust accordingly.
4572  */
4573 boolean_t
arc_reclaim_needed(void)4574 arc_reclaim_needed(void)
4575 {
4576 	return (arc_available_memory() < 0);
4577 }
4578 
4579 void
arc_kmem_reap_soon(void)4580 arc_kmem_reap_soon(void)
4581 {
4582 	size_t			i;
4583 	kmem_cache_t		*prev_cache = NULL;
4584 	kmem_cache_t		*prev_data_cache = NULL;
4585 
4586 #ifdef _KERNEL
4587 #if defined(_ILP32)
4588 	/*
4589 	 * Reclaim unused memory from all kmem caches.
4590 	 */
4591 	kmem_reap();
4592 #endif
4593 #endif
4594 
4595 	for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
4596 #if defined(_ILP32)
4597 		/* reach upper limit of cache size on 32-bit */
4598 		if (zio_buf_cache[i] == NULL)
4599 			break;
4600 #endif
4601 		if (zio_buf_cache[i] != prev_cache) {
4602 			prev_cache = zio_buf_cache[i];
4603 			kmem_cache_reap_now(zio_buf_cache[i]);
4604 		}
4605 		if (zio_data_buf_cache[i] != prev_data_cache) {
4606 			prev_data_cache = zio_data_buf_cache[i];
4607 			kmem_cache_reap_now(zio_data_buf_cache[i]);
4608 		}
4609 	}
4610 	kmem_cache_reap_now(buf_cache);
4611 	kmem_cache_reap_now(hdr_full_cache);
4612 	kmem_cache_reap_now(hdr_l2only_cache);
4613 	kmem_cache_reap_now(zfs_btree_leaf_cache);
4614 	abd_cache_reap_now();
4615 }
4616 
4617 static boolean_t
arc_evict_cb_check(void * arg,zthr_t * zthr)4618 arc_evict_cb_check(void *arg, zthr_t *zthr)
4619 {
4620 	(void) arg, (void) zthr;
4621 
4622 #ifdef ZFS_DEBUG
4623 	/*
4624 	 * This is necessary in order to keep the kstat information
4625 	 * up to date for tools that display kstat data such as the
4626 	 * mdb ::arc dcmd and the Linux crash utility.  These tools
4627 	 * typically do not call kstat's update function, but simply
4628 	 * dump out stats from the most recent update.  Without
4629 	 * this call, these commands may show stale stats for the
4630 	 * anon, mru, mru_ghost, mfu, and mfu_ghost lists.  Even
4631 	 * with this call, the data might be out of date if the
4632 	 * evict thread hasn't been woken recently; but that should
4633 	 * suffice.  The arc_state_t structures can be queried
4634 	 * directly if more accurate information is needed.
4635 	 */
4636 	if (arc_ksp != NULL)
4637 		arc_ksp->ks_update(arc_ksp, KSTAT_READ);
4638 #endif
4639 
4640 	/*
4641 	 * We have to rely on arc_wait_for_eviction() to tell us when to
4642 	 * evict, rather than checking if we are overflowing here, so that we
4643 	 * are sure to not leave arc_wait_for_eviction() waiting on aew_cv.
4644 	 * If we have become "not overflowing" since arc_wait_for_eviction()
4645 	 * checked, we need to wake it up.  We could broadcast the CV here,
4646 	 * but arc_wait_for_eviction() may have not yet gone to sleep.  We
4647 	 * would need to use a mutex to ensure that this function doesn't
4648 	 * broadcast until arc_wait_for_eviction() has gone to sleep (e.g.
4649 	 * the arc_evict_lock).  However, the lock ordering of such a lock
4650 	 * would necessarily be incorrect with respect to the zthr_lock,
4651 	 * which is held before this function is called, and is held by
4652 	 * arc_wait_for_eviction() when it calls zthr_wakeup().
4653 	 */
4654 	if (arc_evict_needed)
4655 		return (B_TRUE);
4656 
4657 	/*
4658 	 * If we have buffers in uncached state, evict them periodically.
4659 	 */
4660 	return ((zfs_refcount_count(&arc_uncached->arcs_esize[ARC_BUFC_DATA]) +
4661 	    zfs_refcount_count(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]) &&
4662 	    ddi_get_lbolt() - arc_last_uncached_flush >
4663 	    MSEC_TO_TICK(arc_min_prefetch_ms / 2)));
4664 }
4665 
4666 /*
4667  * Keep arc_size under arc_c by running arc_evict which evicts data
4668  * from the ARC.
4669  */
4670 static void
arc_evict_cb(void * arg,zthr_t * zthr)4671 arc_evict_cb(void *arg, zthr_t *zthr)
4672 {
4673 	(void) arg;
4674 
4675 	uint64_t evicted = 0;
4676 	fstrans_cookie_t cookie = spl_fstrans_mark();
4677 
4678 	/* Always try to evict from uncached state. */
4679 	arc_last_uncached_flush = ddi_get_lbolt();
4680 	evicted += arc_flush_state(arc_uncached, 0, ARC_BUFC_DATA, B_FALSE);
4681 	evicted += arc_flush_state(arc_uncached, 0, ARC_BUFC_METADATA, B_FALSE);
4682 
4683 	/* Evict from other states only if told to. */
4684 	if (arc_evict_needed)
4685 		evicted += arc_evict();
4686 
4687 	/*
4688 	 * If evicted is zero, we couldn't evict anything
4689 	 * via arc_evict(). This could be due to hash lock
4690 	 * collisions, but more likely due to the majority of
4691 	 * arc buffers being unevictable. Therefore, even if
4692 	 * arc_size is above arc_c, another pass is unlikely to
4693 	 * be helpful and could potentially cause us to enter an
4694 	 * infinite loop.  Additionally, zthr_iscancelled() is
4695 	 * checked here so that if the arc is shutting down, the
4696 	 * broadcast will wake any remaining arc evict waiters.
4697 	 *
4698 	 * Note we cancel using zthr instead of arc_evict_zthr
4699 	 * because the latter may not yet be initializd when the
4700 	 * callback is first invoked.
4701 	 */
4702 	mutex_enter(&arc_evict_lock);
4703 	arc_evict_needed = !zthr_iscancelled(zthr) &&
4704 	    evicted > 0 && aggsum_compare(&arc_sums.arcstat_size, arc_c) > 0;
4705 	if (!arc_evict_needed) {
4706 		/*
4707 		 * We're either no longer overflowing, or we
4708 		 * can't evict anything more, so we should wake
4709 		 * arc_get_data_impl() sooner.
4710 		 */
4711 		arc_evict_waiter_t *aw;
4712 		while ((aw = list_remove_head(&arc_evict_waiters)) != NULL) {
4713 			cv_broadcast(&aw->aew_cv);
4714 		}
4715 		arc_set_need_free();
4716 	}
4717 	mutex_exit(&arc_evict_lock);
4718 	spl_fstrans_unmark(cookie);
4719 }
4720 
4721 static boolean_t
arc_reap_cb_check(void * arg,zthr_t * zthr)4722 arc_reap_cb_check(void *arg, zthr_t *zthr)
4723 {
4724 	(void) arg, (void) zthr;
4725 
4726 	int64_t free_memory = arc_available_memory();
4727 	static int reap_cb_check_counter = 0;
4728 
4729 	/*
4730 	 * If a kmem reap is already active, don't schedule more.  We must
4731 	 * check for this because kmem_cache_reap_soon() won't actually
4732 	 * block on the cache being reaped (this is to prevent callers from
4733 	 * becoming implicitly blocked by a system-wide kmem reap -- which,
4734 	 * on a system with many, many full magazines, can take minutes).
4735 	 */
4736 	if (!kmem_cache_reap_active() && free_memory < 0) {
4737 
4738 		arc_no_grow = B_TRUE;
4739 		arc_warm = B_TRUE;
4740 		/*
4741 		 * Wait at least zfs_grow_retry (default 5) seconds
4742 		 * before considering growing.
4743 		 */
4744 		arc_growtime = gethrtime() + SEC2NSEC(arc_grow_retry);
4745 		return (B_TRUE);
4746 	} else if (free_memory < arc_c >> arc_no_grow_shift) {
4747 		arc_no_grow = B_TRUE;
4748 	} else if (gethrtime() >= arc_growtime) {
4749 		arc_no_grow = B_FALSE;
4750 	}
4751 
4752 	/*
4753 	 * Called unconditionally every 60 seconds to reclaim unused
4754 	 * zstd compression and decompression context. This is done
4755 	 * here to avoid the need for an independent thread.
4756 	 */
4757 	if (!((reap_cb_check_counter++) % 60))
4758 		zfs_zstd_cache_reap_now();
4759 
4760 	return (B_FALSE);
4761 }
4762 
4763 /*
4764  * Keep enough free memory in the system by reaping the ARC's kmem
4765  * caches.  To cause more slabs to be reapable, we may reduce the
4766  * target size of the cache (arc_c), causing the arc_evict_cb()
4767  * to free more buffers.
4768  */
4769 static void
arc_reap_cb(void * arg,zthr_t * zthr)4770 arc_reap_cb(void *arg, zthr_t *zthr)
4771 {
4772 	int64_t can_free, free_memory, to_free;
4773 
4774 	(void) arg, (void) zthr;
4775 	fstrans_cookie_t cookie = spl_fstrans_mark();
4776 
4777 	/*
4778 	 * Kick off asynchronous kmem_reap()'s of all our caches.
4779 	 */
4780 	arc_kmem_reap_soon();
4781 
4782 	/*
4783 	 * Wait at least arc_kmem_cache_reap_retry_ms between
4784 	 * arc_kmem_reap_soon() calls. Without this check it is possible to
4785 	 * end up in a situation where we spend lots of time reaping
4786 	 * caches, while we're near arc_c_min.  Waiting here also gives the
4787 	 * subsequent free memory check a chance of finding that the
4788 	 * asynchronous reap has already freed enough memory, and we don't
4789 	 * need to call arc_reduce_target_size().
4790 	 */
4791 	delay((hz * arc_kmem_cache_reap_retry_ms + 999) / 1000);
4792 
4793 	/*
4794 	 * Reduce the target size as needed to maintain the amount of free
4795 	 * memory in the system at a fraction of the arc_size (1/128th by
4796 	 * default).  If oversubscribed (free_memory < 0) then reduce the
4797 	 * target arc_size by the deficit amount plus the fractional
4798 	 * amount.  If free memory is positive but less than the fractional
4799 	 * amount, reduce by what is needed to hit the fractional amount.
4800 	 */
4801 	free_memory = arc_available_memory();
4802 	can_free = arc_c - arc_c_min;
4803 	to_free = (MAX(can_free, 0) >> arc_shrink_shift) - free_memory;
4804 	if (to_free > 0)
4805 		arc_reduce_target_size(to_free);
4806 	spl_fstrans_unmark(cookie);
4807 }
4808 
4809 #ifdef _KERNEL
4810 /*
4811  * Determine the amount of memory eligible for eviction contained in the
4812  * ARC. All clean data reported by the ghost lists can always be safely
4813  * evicted. Due to arc_c_min, the same does not hold for all clean data
4814  * contained by the regular mru and mfu lists.
4815  *
4816  * In the case of the regular mru and mfu lists, we need to report as
4817  * much clean data as possible, such that evicting that same reported
4818  * data will not bring arc_size below arc_c_min. Thus, in certain
4819  * circumstances, the total amount of clean data in the mru and mfu
4820  * lists might not actually be evictable.
4821  *
4822  * The following two distinct cases are accounted for:
4823  *
4824  * 1. The sum of the amount of dirty data contained by both the mru and
4825  *    mfu lists, plus the ARC's other accounting (e.g. the anon list),
4826  *    is greater than or equal to arc_c_min.
4827  *    (i.e. amount of dirty data >= arc_c_min)
4828  *
4829  *    This is the easy case; all clean data contained by the mru and mfu
4830  *    lists is evictable. Evicting all clean data can only drop arc_size
4831  *    to the amount of dirty data, which is greater than arc_c_min.
4832  *
4833  * 2. The sum of the amount of dirty data contained by both the mru and
4834  *    mfu lists, plus the ARC's other accounting (e.g. the anon list),
4835  *    is less than arc_c_min.
4836  *    (i.e. arc_c_min > amount of dirty data)
4837  *
4838  *    2.1. arc_size is greater than or equal arc_c_min.
4839  *         (i.e. arc_size >= arc_c_min > amount of dirty data)
4840  *
4841  *         In this case, not all clean data from the regular mru and mfu
4842  *         lists is actually evictable; we must leave enough clean data
4843  *         to keep arc_size above arc_c_min. Thus, the maximum amount of
4844  *         evictable data from the two lists combined, is exactly the
4845  *         difference between arc_size and arc_c_min.
4846  *
4847  *    2.2. arc_size is less than arc_c_min
4848  *         (i.e. arc_c_min > arc_size > amount of dirty data)
4849  *
4850  *         In this case, none of the data contained in the mru and mfu
4851  *         lists is evictable, even if it's clean. Since arc_size is
4852  *         already below arc_c_min, evicting any more would only
4853  *         increase this negative difference.
4854  */
4855 
4856 #endif /* _KERNEL */
4857 
4858 /*
4859  * Adapt arc info given the number of bytes we are trying to add and
4860  * the state that we are coming from.  This function is only called
4861  * when we are adding new content to the cache.
4862  */
4863 static void
arc_adapt(uint64_t bytes)4864 arc_adapt(uint64_t bytes)
4865 {
4866 	/*
4867 	 * Wake reap thread if we do not have any available memory
4868 	 */
4869 	if (arc_reclaim_needed()) {
4870 		zthr_wakeup(arc_reap_zthr);
4871 		return;
4872 	}
4873 
4874 	if (arc_no_grow)
4875 		return;
4876 
4877 	if (arc_c >= arc_c_max)
4878 		return;
4879 
4880 	/*
4881 	 * If we're within (2 * maxblocksize) bytes of the target
4882 	 * cache size, increment the target cache size
4883 	 */
4884 	if (aggsum_upper_bound(&arc_sums.arcstat_size) +
4885 	    2 * SPA_MAXBLOCKSIZE >= arc_c) {
4886 		uint64_t dc = MAX(bytes, SPA_OLD_MAXBLOCKSIZE);
4887 		if (atomic_add_64_nv(&arc_c, dc) > arc_c_max)
4888 			arc_c = arc_c_max;
4889 	}
4890 }
4891 
4892 /*
4893  * Check if ARC current size has grown past our upper thresholds.
4894  */
4895 static arc_ovf_level_t
arc_is_overflowing(boolean_t lax,boolean_t use_reserve)4896 arc_is_overflowing(boolean_t lax, boolean_t use_reserve)
4897 {
4898 	/*
4899 	 * We just compare the lower bound here for performance reasons. Our
4900 	 * primary goals are to make sure that the arc never grows without
4901 	 * bound, and that it can reach its maximum size. This check
4902 	 * accomplishes both goals. The maximum amount we could run over by is
4903 	 * 2 * aggsum_borrow_multiplier * NUM_CPUS * the average size of a block
4904 	 * in the ARC. In practice, that's in the tens of MB, which is low
4905 	 * enough to be safe.
4906 	 */
4907 	int64_t over = aggsum_lower_bound(&arc_sums.arcstat_size) - arc_c -
4908 	    zfs_max_recordsize;
4909 
4910 	/* Always allow at least one block of overflow. */
4911 	if (over < 0)
4912 		return (ARC_OVF_NONE);
4913 
4914 	/* If we are under memory pressure, report severe overflow. */
4915 	if (!lax)
4916 		return (ARC_OVF_SEVERE);
4917 
4918 	/* We are not under pressure, so be more or less relaxed. */
4919 	int64_t overflow = (arc_c >> zfs_arc_overflow_shift) / 2;
4920 	if (use_reserve)
4921 		overflow *= 3;
4922 	return (over < overflow ? ARC_OVF_SOME : ARC_OVF_SEVERE);
4923 }
4924 
4925 static abd_t *
arc_get_data_abd(arc_buf_hdr_t * hdr,uint64_t size,const void * tag,int alloc_flags)4926 arc_get_data_abd(arc_buf_hdr_t *hdr, uint64_t size, const void *tag,
4927     int alloc_flags)
4928 {
4929 	arc_buf_contents_t type = arc_buf_type(hdr);
4930 
4931 	arc_get_data_impl(hdr, size, tag, alloc_flags);
4932 	if (alloc_flags & ARC_HDR_ALLOC_LINEAR)
4933 		return (abd_alloc_linear(size, type == ARC_BUFC_METADATA));
4934 	else
4935 		return (abd_alloc(size, type == ARC_BUFC_METADATA));
4936 }
4937 
4938 static void *
arc_get_data_buf(arc_buf_hdr_t * hdr,uint64_t size,const void * tag)4939 arc_get_data_buf(arc_buf_hdr_t *hdr, uint64_t size, const void *tag)
4940 {
4941 	arc_buf_contents_t type = arc_buf_type(hdr);
4942 
4943 	arc_get_data_impl(hdr, size, tag, 0);
4944 	if (type == ARC_BUFC_METADATA) {
4945 		return (zio_buf_alloc(size));
4946 	} else {
4947 		ASSERT(type == ARC_BUFC_DATA);
4948 		return (zio_data_buf_alloc(size));
4949 	}
4950 }
4951 
4952 /*
4953  * Wait for the specified amount of data (in bytes) to be evicted from the
4954  * ARC, and for there to be sufficient free memory in the system.
4955  * The lax argument specifies that caller does not have a specific reason
4956  * to wait, not aware of any memory pressure.  Low memory handlers though
4957  * should set it to B_FALSE to wait for all required evictions to complete.
4958  * The use_reserve argument allows some callers to wait less than others
4959  * to not block critical code paths, possibly blocking other resources.
4960  */
4961 void
arc_wait_for_eviction(uint64_t amount,boolean_t lax,boolean_t use_reserve)4962 arc_wait_for_eviction(uint64_t amount, boolean_t lax, boolean_t use_reserve)
4963 {
4964 	switch (arc_is_overflowing(lax, use_reserve)) {
4965 	case ARC_OVF_NONE:
4966 		return;
4967 	case ARC_OVF_SOME:
4968 		/*
4969 		 * This is a bit racy without taking arc_evict_lock, but the
4970 		 * worst that can happen is we either call zthr_wakeup() extra
4971 		 * time due to race with other thread here, or the set flag
4972 		 * get cleared by arc_evict_cb(), which is unlikely due to
4973 		 * big hysteresis, but also not important since at this level
4974 		 * of overflow the eviction is purely advisory.  Same time
4975 		 * taking the global lock here every time without waiting for
4976 		 * the actual eviction creates a significant lock contention.
4977 		 */
4978 		if (!arc_evict_needed) {
4979 			arc_evict_needed = B_TRUE;
4980 			zthr_wakeup(arc_evict_zthr);
4981 		}
4982 		return;
4983 	case ARC_OVF_SEVERE:
4984 	default:
4985 	{
4986 		arc_evict_waiter_t aw;
4987 		list_link_init(&aw.aew_node);
4988 		cv_init(&aw.aew_cv, NULL, CV_DEFAULT, NULL);
4989 
4990 		uint64_t last_count = 0;
4991 		mutex_enter(&arc_evict_lock);
4992 		if (!list_is_empty(&arc_evict_waiters)) {
4993 			arc_evict_waiter_t *last =
4994 			    list_tail(&arc_evict_waiters);
4995 			last_count = last->aew_count;
4996 		} else if (!arc_evict_needed) {
4997 			arc_evict_needed = B_TRUE;
4998 			zthr_wakeup(arc_evict_zthr);
4999 		}
5000 		/*
5001 		 * Note, the last waiter's count may be less than
5002 		 * arc_evict_count if we are low on memory in which
5003 		 * case arc_evict_state_impl() may have deferred
5004 		 * wakeups (but still incremented arc_evict_count).
5005 		 */
5006 		aw.aew_count = MAX(last_count, arc_evict_count) + amount;
5007 
5008 		list_insert_tail(&arc_evict_waiters, &aw);
5009 
5010 		arc_set_need_free();
5011 
5012 		DTRACE_PROBE3(arc__wait__for__eviction,
5013 		    uint64_t, amount,
5014 		    uint64_t, arc_evict_count,
5015 		    uint64_t, aw.aew_count);
5016 
5017 		/*
5018 		 * We will be woken up either when arc_evict_count reaches
5019 		 * aew_count, or when the ARC is no longer overflowing and
5020 		 * eviction completes.
5021 		 * In case of "false" wakeup, we will still be on the list.
5022 		 */
5023 		do {
5024 			cv_wait(&aw.aew_cv, &arc_evict_lock);
5025 		} while (list_link_active(&aw.aew_node));
5026 		mutex_exit(&arc_evict_lock);
5027 
5028 		cv_destroy(&aw.aew_cv);
5029 	}
5030 	}
5031 }
5032 
5033 /*
5034  * Allocate a block and return it to the caller. If we are hitting the
5035  * hard limit for the cache size, we must sleep, waiting for the eviction
5036  * thread to catch up. If we're past the target size but below the hard
5037  * limit, we'll only signal the reclaim thread and continue on.
5038  */
5039 static void
arc_get_data_impl(arc_buf_hdr_t * hdr,uint64_t size,const void * tag,int alloc_flags)5040 arc_get_data_impl(arc_buf_hdr_t *hdr, uint64_t size, const void *tag,
5041     int alloc_flags)
5042 {
5043 	arc_adapt(size);
5044 
5045 	/*
5046 	 * If arc_size is currently overflowing, we must be adding data
5047 	 * faster than we are evicting.  To ensure we don't compound the
5048 	 * problem by adding more data and forcing arc_size to grow even
5049 	 * further past it's target size, we wait for the eviction thread to
5050 	 * make some progress.  We also wait for there to be sufficient free
5051 	 * memory in the system, as measured by arc_free_memory().
5052 	 *
5053 	 * Specifically, we wait for zfs_arc_eviction_pct percent of the
5054 	 * requested size to be evicted.  This should be more than 100%, to
5055 	 * ensure that that progress is also made towards getting arc_size
5056 	 * under arc_c.  See the comment above zfs_arc_eviction_pct.
5057 	 */
5058 	arc_wait_for_eviction(size * zfs_arc_eviction_pct / 100,
5059 	    B_TRUE, alloc_flags & ARC_HDR_USE_RESERVE);
5060 
5061 	arc_buf_contents_t type = arc_buf_type(hdr);
5062 	if (type == ARC_BUFC_METADATA) {
5063 		arc_space_consume(size, ARC_SPACE_META);
5064 	} else {
5065 		arc_space_consume(size, ARC_SPACE_DATA);
5066 	}
5067 
5068 	/*
5069 	 * Update the state size.  Note that ghost states have a
5070 	 * "ghost size" and so don't need to be updated.
5071 	 */
5072 	arc_state_t *state = hdr->b_l1hdr.b_state;
5073 	if (!GHOST_STATE(state)) {
5074 
5075 		(void) zfs_refcount_add_many(&state->arcs_size[type], size,
5076 		    tag);
5077 
5078 		/*
5079 		 * If this is reached via arc_read, the link is
5080 		 * protected by the hash lock. If reached via
5081 		 * arc_buf_alloc, the header should not be accessed by
5082 		 * any other thread. And, if reached via arc_read_done,
5083 		 * the hash lock will protect it if it's found in the
5084 		 * hash table; otherwise no other thread should be
5085 		 * trying to [add|remove]_reference it.
5086 		 */
5087 		if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
5088 			ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
5089 			(void) zfs_refcount_add_many(&state->arcs_esize[type],
5090 			    size, tag);
5091 		}
5092 	}
5093 }
5094 
5095 static void
arc_free_data_abd(arc_buf_hdr_t * hdr,abd_t * abd,uint64_t size,const void * tag)5096 arc_free_data_abd(arc_buf_hdr_t *hdr, abd_t *abd, uint64_t size,
5097     const void *tag)
5098 {
5099 	arc_free_data_impl(hdr, size, tag);
5100 	abd_free(abd);
5101 }
5102 
5103 static void
arc_free_data_buf(arc_buf_hdr_t * hdr,void * buf,uint64_t size,const void * tag)5104 arc_free_data_buf(arc_buf_hdr_t *hdr, void *buf, uint64_t size, const void *tag)
5105 {
5106 	arc_buf_contents_t type = arc_buf_type(hdr);
5107 
5108 	arc_free_data_impl(hdr, size, tag);
5109 	if (type == ARC_BUFC_METADATA) {
5110 		zio_buf_free(buf, size);
5111 	} else {
5112 		ASSERT(type == ARC_BUFC_DATA);
5113 		zio_data_buf_free(buf, size);
5114 	}
5115 }
5116 
5117 /*
5118  * Free the arc data buffer.
5119  */
5120 static void
arc_free_data_impl(arc_buf_hdr_t * hdr,uint64_t size,const void * tag)5121 arc_free_data_impl(arc_buf_hdr_t *hdr, uint64_t size, const void *tag)
5122 {
5123 	arc_state_t *state = hdr->b_l1hdr.b_state;
5124 	arc_buf_contents_t type = arc_buf_type(hdr);
5125 
5126 	/* protected by hash lock, if in the hash table */
5127 	if (multilist_link_active(&hdr->b_l1hdr.b_arc_node)) {
5128 		ASSERT(zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt));
5129 		ASSERT(state != arc_anon && state != arc_l2c_only);
5130 
5131 		(void) zfs_refcount_remove_many(&state->arcs_esize[type],
5132 		    size, tag);
5133 	}
5134 	(void) zfs_refcount_remove_many(&state->arcs_size[type], size, tag);
5135 
5136 	VERIFY3U(hdr->b_type, ==, type);
5137 	if (type == ARC_BUFC_METADATA) {
5138 		arc_space_return(size, ARC_SPACE_META);
5139 	} else {
5140 		ASSERT(type == ARC_BUFC_DATA);
5141 		arc_space_return(size, ARC_SPACE_DATA);
5142 	}
5143 }
5144 
5145 /*
5146  * This routine is called whenever a buffer is accessed.
5147  */
5148 static void
arc_access(arc_buf_hdr_t * hdr,arc_flags_t arc_flags,boolean_t hit)5149 arc_access(arc_buf_hdr_t *hdr, arc_flags_t arc_flags, boolean_t hit)
5150 {
5151 	ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
5152 	ASSERT(HDR_HAS_L1HDR(hdr));
5153 
5154 	/*
5155 	 * Update buffer prefetch status.
5156 	 */
5157 	boolean_t was_prefetch = HDR_PREFETCH(hdr);
5158 	boolean_t now_prefetch = arc_flags & ARC_FLAG_PREFETCH;
5159 	if (was_prefetch != now_prefetch) {
5160 		if (was_prefetch) {
5161 			ARCSTAT_CONDSTAT(hit, demand_hit, demand_iohit,
5162 			    HDR_PRESCIENT_PREFETCH(hdr), prescient, predictive,
5163 			    prefetch);
5164 		}
5165 		if (HDR_HAS_L2HDR(hdr))
5166 			l2arc_hdr_arcstats_decrement_state(hdr);
5167 		if (was_prefetch) {
5168 			arc_hdr_clear_flags(hdr,
5169 			    ARC_FLAG_PREFETCH | ARC_FLAG_PRESCIENT_PREFETCH);
5170 		} else {
5171 			arc_hdr_set_flags(hdr, ARC_FLAG_PREFETCH);
5172 		}
5173 		if (HDR_HAS_L2HDR(hdr))
5174 			l2arc_hdr_arcstats_increment_state(hdr);
5175 	}
5176 	if (now_prefetch) {
5177 		if (arc_flags & ARC_FLAG_PRESCIENT_PREFETCH) {
5178 			arc_hdr_set_flags(hdr, ARC_FLAG_PRESCIENT_PREFETCH);
5179 			ARCSTAT_BUMP(arcstat_prescient_prefetch);
5180 		} else {
5181 			ARCSTAT_BUMP(arcstat_predictive_prefetch);
5182 		}
5183 	}
5184 	if (arc_flags & ARC_FLAG_L2CACHE)
5185 		arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE);
5186 
5187 	clock_t now = ddi_get_lbolt();
5188 	if (hdr->b_l1hdr.b_state == arc_anon) {
5189 		arc_state_t	*new_state;
5190 		/*
5191 		 * This buffer is not in the cache, and does not appear in
5192 		 * our "ghost" lists.  Add it to the MRU or uncached state.
5193 		 */
5194 		ASSERT0(hdr->b_l1hdr.b_arc_access);
5195 		hdr->b_l1hdr.b_arc_access = now;
5196 		if (HDR_UNCACHED(hdr)) {
5197 			new_state = arc_uncached;
5198 			DTRACE_PROBE1(new_state__uncached, arc_buf_hdr_t *,
5199 			    hdr);
5200 		} else {
5201 			new_state = arc_mru;
5202 			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr);
5203 		}
5204 		arc_change_state(new_state, hdr);
5205 	} else if (hdr->b_l1hdr.b_state == arc_mru) {
5206 		/*
5207 		 * This buffer has been accessed once recently and either
5208 		 * its read is still in progress or it is in the cache.
5209 		 */
5210 		if (HDR_IO_IN_PROGRESS(hdr)) {
5211 			hdr->b_l1hdr.b_arc_access = now;
5212 			return;
5213 		}
5214 		hdr->b_l1hdr.b_mru_hits++;
5215 		ARCSTAT_BUMP(arcstat_mru_hits);
5216 
5217 		/*
5218 		 * If the previous access was a prefetch, then it already
5219 		 * handled possible promotion, so nothing more to do for now.
5220 		 */
5221 		if (was_prefetch) {
5222 			hdr->b_l1hdr.b_arc_access = now;
5223 			return;
5224 		}
5225 
5226 		/*
5227 		 * If more than ARC_MINTIME have passed from the previous
5228 		 * hit, promote the buffer to the MFU state.
5229 		 */
5230 		if (ddi_time_after(now, hdr->b_l1hdr.b_arc_access +
5231 		    ARC_MINTIME)) {
5232 			hdr->b_l1hdr.b_arc_access = now;
5233 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
5234 			arc_change_state(arc_mfu, hdr);
5235 		}
5236 	} else if (hdr->b_l1hdr.b_state == arc_mru_ghost) {
5237 		arc_state_t	*new_state;
5238 		/*
5239 		 * This buffer has been accessed once recently, but was
5240 		 * evicted from the cache.  Would we have bigger MRU, it
5241 		 * would be an MRU hit, so handle it the same way, except
5242 		 * we don't need to check the previous access time.
5243 		 */
5244 		hdr->b_l1hdr.b_mru_ghost_hits++;
5245 		ARCSTAT_BUMP(arcstat_mru_ghost_hits);
5246 		hdr->b_l1hdr.b_arc_access = now;
5247 		wmsum_add(&arc_mru_ghost->arcs_hits[arc_buf_type(hdr)],
5248 		    arc_hdr_size(hdr));
5249 		if (was_prefetch) {
5250 			new_state = arc_mru;
5251 			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr);
5252 		} else {
5253 			new_state = arc_mfu;
5254 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
5255 		}
5256 		arc_change_state(new_state, hdr);
5257 	} else if (hdr->b_l1hdr.b_state == arc_mfu) {
5258 		/*
5259 		 * This buffer has been accessed more than once and either
5260 		 * still in the cache or being restored from one of ghosts.
5261 		 */
5262 		if (!HDR_IO_IN_PROGRESS(hdr)) {
5263 			hdr->b_l1hdr.b_mfu_hits++;
5264 			ARCSTAT_BUMP(arcstat_mfu_hits);
5265 		}
5266 		hdr->b_l1hdr.b_arc_access = now;
5267 	} else if (hdr->b_l1hdr.b_state == arc_mfu_ghost) {
5268 		/*
5269 		 * This buffer has been accessed more than once recently, but
5270 		 * has been evicted from the cache.  Would we have bigger MFU
5271 		 * it would stay in cache, so move it back to MFU state.
5272 		 */
5273 		hdr->b_l1hdr.b_mfu_ghost_hits++;
5274 		ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
5275 		hdr->b_l1hdr.b_arc_access = now;
5276 		wmsum_add(&arc_mfu_ghost->arcs_hits[arc_buf_type(hdr)],
5277 		    arc_hdr_size(hdr));
5278 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, hdr);
5279 		arc_change_state(arc_mfu, hdr);
5280 	} else if (hdr->b_l1hdr.b_state == arc_uncached) {
5281 		/*
5282 		 * This buffer is uncacheable, but we got a hit.  Probably
5283 		 * a demand read after prefetch.  Nothing more to do here.
5284 		 */
5285 		if (!HDR_IO_IN_PROGRESS(hdr))
5286 			ARCSTAT_BUMP(arcstat_uncached_hits);
5287 		hdr->b_l1hdr.b_arc_access = now;
5288 	} else if (hdr->b_l1hdr.b_state == arc_l2c_only) {
5289 		/*
5290 		 * This buffer is on the 2nd Level ARC and was not accessed
5291 		 * for a long time, so treat it as new and put into MRU.
5292 		 */
5293 		hdr->b_l1hdr.b_arc_access = now;
5294 		DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, hdr);
5295 		arc_change_state(arc_mru, hdr);
5296 	} else {
5297 		cmn_err(CE_PANIC, "invalid arc state 0x%p",
5298 		    hdr->b_l1hdr.b_state);
5299 	}
5300 }
5301 
5302 /*
5303  * This routine is called by dbuf_hold() to update the arc_access() state
5304  * which otherwise would be skipped for entries in the dbuf cache.
5305  */
5306 void
arc_buf_access(arc_buf_t * buf)5307 arc_buf_access(arc_buf_t *buf)
5308 {
5309 	arc_buf_hdr_t *hdr = buf->b_hdr;
5310 
5311 	/*
5312 	 * Avoid taking the hash_lock when possible as an optimization.
5313 	 * The header must be checked again under the hash_lock in order
5314 	 * to handle the case where it is concurrently being released.
5315 	 */
5316 	if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr))
5317 		return;
5318 
5319 	kmutex_t *hash_lock = HDR_LOCK(hdr);
5320 	mutex_enter(hash_lock);
5321 
5322 	if (hdr->b_l1hdr.b_state == arc_anon || HDR_EMPTY(hdr)) {
5323 		mutex_exit(hash_lock);
5324 		ARCSTAT_BUMP(arcstat_access_skip);
5325 		return;
5326 	}
5327 
5328 	ASSERT(hdr->b_l1hdr.b_state == arc_mru ||
5329 	    hdr->b_l1hdr.b_state == arc_mfu ||
5330 	    hdr->b_l1hdr.b_state == arc_uncached);
5331 
5332 	DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
5333 	arc_access(hdr, 0, B_TRUE);
5334 	mutex_exit(hash_lock);
5335 
5336 	ARCSTAT_BUMP(arcstat_hits);
5337 	ARCSTAT_CONDSTAT(B_TRUE /* demand */, demand, prefetch,
5338 	    !HDR_ISTYPE_METADATA(hdr), data, metadata, hits);
5339 }
5340 
5341 /* a generic arc_read_done_func_t which you can use */
5342 void
arc_bcopy_func(zio_t * zio,const zbookmark_phys_t * zb,const blkptr_t * bp,arc_buf_t * buf,void * arg)5343 arc_bcopy_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp,
5344     arc_buf_t *buf, void *arg)
5345 {
5346 	(void) zio, (void) zb, (void) bp;
5347 
5348 	if (buf == NULL)
5349 		return;
5350 
5351 	memcpy(arg, buf->b_data, arc_buf_size(buf));
5352 	arc_buf_destroy(buf, arg);
5353 }
5354 
5355 /* a generic arc_read_done_func_t */
5356 void
arc_getbuf_func(zio_t * zio,const zbookmark_phys_t * zb,const blkptr_t * bp,arc_buf_t * buf,void * arg)5357 arc_getbuf_func(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp,
5358     arc_buf_t *buf, void *arg)
5359 {
5360 	(void) zb, (void) bp;
5361 	arc_buf_t **bufp = arg;
5362 
5363 	if (buf == NULL) {
5364 		ASSERT(zio == NULL || zio->io_error != 0);
5365 		*bufp = NULL;
5366 	} else {
5367 		ASSERT(zio == NULL || zio->io_error == 0);
5368 		*bufp = buf;
5369 		ASSERT(buf->b_data != NULL);
5370 	}
5371 }
5372 
5373 static void
arc_hdr_verify(arc_buf_hdr_t * hdr,blkptr_t * bp)5374 arc_hdr_verify(arc_buf_hdr_t *hdr, blkptr_t *bp)
5375 {
5376 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) {
5377 		ASSERT3U(HDR_GET_PSIZE(hdr), ==, 0);
5378 		ASSERT3U(arc_hdr_get_compress(hdr), ==, ZIO_COMPRESS_OFF);
5379 	} else {
5380 		if (HDR_COMPRESSION_ENABLED(hdr)) {
5381 			ASSERT3U(arc_hdr_get_compress(hdr), ==,
5382 			    BP_GET_COMPRESS(bp));
5383 		}
5384 		ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp));
5385 		ASSERT3U(HDR_GET_PSIZE(hdr), ==, BP_GET_PSIZE(bp));
5386 		ASSERT3U(!!HDR_PROTECTED(hdr), ==, BP_IS_PROTECTED(bp));
5387 	}
5388 }
5389 
5390 static void
arc_read_done(zio_t * zio)5391 arc_read_done(zio_t *zio)
5392 {
5393 	blkptr_t 	*bp = zio->io_bp;
5394 	arc_buf_hdr_t	*hdr = zio->io_private;
5395 	kmutex_t	*hash_lock = NULL;
5396 	arc_callback_t	*callback_list;
5397 	arc_callback_t	*acb;
5398 
5399 	/*
5400 	 * The hdr was inserted into hash-table and removed from lists
5401 	 * prior to starting I/O.  We should find this header, since
5402 	 * it's in the hash table, and it should be legit since it's
5403 	 * not possible to evict it during the I/O.  The only possible
5404 	 * reason for it not to be found is if we were freed during the
5405 	 * read.
5406 	 */
5407 	if (HDR_IN_HASH_TABLE(hdr)) {
5408 		arc_buf_hdr_t *found;
5409 
5410 		ASSERT3U(hdr->b_birth, ==, BP_GET_BIRTH(zio->io_bp));
5411 		ASSERT3U(hdr->b_dva.dva_word[0], ==,
5412 		    BP_IDENTITY(zio->io_bp)->dva_word[0]);
5413 		ASSERT3U(hdr->b_dva.dva_word[1], ==,
5414 		    BP_IDENTITY(zio->io_bp)->dva_word[1]);
5415 
5416 		found = buf_hash_find(hdr->b_spa, zio->io_bp, &hash_lock);
5417 
5418 		ASSERT((found == hdr &&
5419 		    DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
5420 		    (found == hdr && HDR_L2_READING(hdr)));
5421 		ASSERT3P(hash_lock, !=, NULL);
5422 	}
5423 
5424 	if (BP_IS_PROTECTED(bp)) {
5425 		hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp);
5426 		hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset;
5427 		zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt,
5428 		    hdr->b_crypt_hdr.b_iv);
5429 
5430 		if (zio->io_error == 0) {
5431 			if (BP_GET_TYPE(bp) == DMU_OT_INTENT_LOG) {
5432 				void *tmpbuf;
5433 
5434 				tmpbuf = abd_borrow_buf_copy(zio->io_abd,
5435 				    sizeof (zil_chain_t));
5436 				zio_crypt_decode_mac_zil(tmpbuf,
5437 				    hdr->b_crypt_hdr.b_mac);
5438 				abd_return_buf(zio->io_abd, tmpbuf,
5439 				    sizeof (zil_chain_t));
5440 			} else {
5441 				zio_crypt_decode_mac_bp(bp,
5442 				    hdr->b_crypt_hdr.b_mac);
5443 			}
5444 		}
5445 	}
5446 
5447 	if (zio->io_error == 0) {
5448 		/* byteswap if necessary */
5449 		if (BP_SHOULD_BYTESWAP(zio->io_bp)) {
5450 			if (BP_GET_LEVEL(zio->io_bp) > 0) {
5451 				hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64;
5452 			} else {
5453 				hdr->b_l1hdr.b_byteswap =
5454 				    DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
5455 			}
5456 		} else {
5457 			hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS;
5458 		}
5459 		if (!HDR_L2_READING(hdr)) {
5460 			hdr->b_complevel = zio->io_prop.zp_complevel;
5461 		}
5462 	}
5463 
5464 	arc_hdr_clear_flags(hdr, ARC_FLAG_L2_EVICTED);
5465 	if (l2arc_noprefetch && HDR_PREFETCH(hdr))
5466 		arc_hdr_clear_flags(hdr, ARC_FLAG_L2CACHE);
5467 
5468 	callback_list = hdr->b_l1hdr.b_acb;
5469 	ASSERT3P(callback_list, !=, NULL);
5470 	hdr->b_l1hdr.b_acb = NULL;
5471 
5472 	/*
5473 	 * If a read request has a callback (i.e. acb_done is not NULL), then we
5474 	 * make a buf containing the data according to the parameters which were
5475 	 * passed in. The implementation of arc_buf_alloc_impl() ensures that we
5476 	 * aren't needlessly decompressing the data multiple times.
5477 	 */
5478 	int callback_cnt = 0;
5479 	for (acb = callback_list; acb != NULL; acb = acb->acb_next) {
5480 
5481 		/* We need the last one to call below in original order. */
5482 		callback_list = acb;
5483 
5484 		if (!acb->acb_done || acb->acb_nobuf)
5485 			continue;
5486 
5487 		callback_cnt++;
5488 
5489 		if (zio->io_error != 0)
5490 			continue;
5491 
5492 		int error = arc_buf_alloc_impl(hdr, zio->io_spa,
5493 		    &acb->acb_zb, acb->acb_private, acb->acb_encrypted,
5494 		    acb->acb_compressed, acb->acb_noauth, B_TRUE,
5495 		    &acb->acb_buf);
5496 
5497 		/*
5498 		 * Assert non-speculative zios didn't fail because an
5499 		 * encryption key wasn't loaded
5500 		 */
5501 		ASSERT((zio->io_flags & ZIO_FLAG_SPECULATIVE) ||
5502 		    error != EACCES);
5503 
5504 		/*
5505 		 * If we failed to decrypt, report an error now (as the zio
5506 		 * layer would have done if it had done the transforms).
5507 		 */
5508 		if (error == ECKSUM) {
5509 			ASSERT(BP_IS_PROTECTED(bp));
5510 			error = SET_ERROR(EIO);
5511 			if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
5512 				spa_log_error(zio->io_spa, &acb->acb_zb,
5513 				    BP_GET_LOGICAL_BIRTH(zio->io_bp));
5514 				(void) zfs_ereport_post(
5515 				    FM_EREPORT_ZFS_AUTHENTICATION,
5516 				    zio->io_spa, NULL, &acb->acb_zb, zio, 0);
5517 			}
5518 		}
5519 
5520 		if (error != 0) {
5521 			/*
5522 			 * Decompression or decryption failed.  Set
5523 			 * io_error so that when we call acb_done
5524 			 * (below), we will indicate that the read
5525 			 * failed. Note that in the unusual case
5526 			 * where one callback is compressed and another
5527 			 * uncompressed, we will mark all of them
5528 			 * as failed, even though the uncompressed
5529 			 * one can't actually fail.  In this case,
5530 			 * the hdr will not be anonymous, because
5531 			 * if there are multiple callbacks, it's
5532 			 * because multiple threads found the same
5533 			 * arc buf in the hash table.
5534 			 */
5535 			zio->io_error = error;
5536 		}
5537 	}
5538 
5539 	/*
5540 	 * If there are multiple callbacks, we must have the hash lock,
5541 	 * because the only way for multiple threads to find this hdr is
5542 	 * in the hash table.  This ensures that if there are multiple
5543 	 * callbacks, the hdr is not anonymous.  If it were anonymous,
5544 	 * we couldn't use arc_buf_destroy() in the error case below.
5545 	 */
5546 	ASSERT(callback_cnt < 2 || hash_lock != NULL);
5547 
5548 	if (zio->io_error == 0) {
5549 		arc_hdr_verify(hdr, zio->io_bp);
5550 	} else {
5551 		arc_hdr_set_flags(hdr, ARC_FLAG_IO_ERROR);
5552 		if (hdr->b_l1hdr.b_state != arc_anon)
5553 			arc_change_state(arc_anon, hdr);
5554 		if (HDR_IN_HASH_TABLE(hdr))
5555 			buf_hash_remove(hdr);
5556 	}
5557 
5558 	arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
5559 	(void) remove_reference(hdr, hdr);
5560 
5561 	if (hash_lock != NULL)
5562 		mutex_exit(hash_lock);
5563 
5564 	/* execute each callback and free its structure */
5565 	while ((acb = callback_list) != NULL) {
5566 		if (acb->acb_done != NULL) {
5567 			if (zio->io_error != 0 && acb->acb_buf != NULL) {
5568 				/*
5569 				 * If arc_buf_alloc_impl() fails during
5570 				 * decompression, the buf will still be
5571 				 * allocated, and needs to be freed here.
5572 				 */
5573 				arc_buf_destroy(acb->acb_buf,
5574 				    acb->acb_private);
5575 				acb->acb_buf = NULL;
5576 			}
5577 			acb->acb_done(zio, &zio->io_bookmark, zio->io_bp,
5578 			    acb->acb_buf, acb->acb_private);
5579 		}
5580 
5581 		if (acb->acb_zio_dummy != NULL) {
5582 			acb->acb_zio_dummy->io_error = zio->io_error;
5583 			zio_nowait(acb->acb_zio_dummy);
5584 		}
5585 
5586 		callback_list = acb->acb_prev;
5587 		if (acb->acb_wait) {
5588 			mutex_enter(&acb->acb_wait_lock);
5589 			acb->acb_wait_error = zio->io_error;
5590 			acb->acb_wait = B_FALSE;
5591 			cv_signal(&acb->acb_wait_cv);
5592 			mutex_exit(&acb->acb_wait_lock);
5593 			/* acb will be freed by the waiting thread. */
5594 		} else {
5595 			kmem_free(acb, sizeof (arc_callback_t));
5596 		}
5597 	}
5598 }
5599 
5600 /*
5601  * Lookup the block at the specified DVA (in bp), and return the manner in
5602  * which the block is cached. A zero return indicates not cached.
5603  */
5604 int
arc_cached(spa_t * spa,const blkptr_t * bp)5605 arc_cached(spa_t *spa, const blkptr_t *bp)
5606 {
5607 	arc_buf_hdr_t *hdr = NULL;
5608 	kmutex_t *hash_lock = NULL;
5609 	uint64_t guid = spa_load_guid(spa);
5610 	int flags = 0;
5611 
5612 	if (BP_IS_EMBEDDED(bp))
5613 		return (ARC_CACHED_EMBEDDED);
5614 
5615 	hdr = buf_hash_find(guid, bp, &hash_lock);
5616 	if (hdr == NULL)
5617 		return (0);
5618 
5619 	if (HDR_HAS_L1HDR(hdr)) {
5620 		arc_state_t *state = hdr->b_l1hdr.b_state;
5621 		/*
5622 		 * We switch to ensure that any future arc_state_type_t
5623 		 * changes are handled. This is just a shift to promote
5624 		 * more compile-time checking.
5625 		 */
5626 		switch (state->arcs_state) {
5627 		case ARC_STATE_ANON:
5628 			break;
5629 		case ARC_STATE_MRU:
5630 			flags |= ARC_CACHED_IN_MRU | ARC_CACHED_IN_L1;
5631 			break;
5632 		case ARC_STATE_MFU:
5633 			flags |= ARC_CACHED_IN_MFU | ARC_CACHED_IN_L1;
5634 			break;
5635 		case ARC_STATE_UNCACHED:
5636 			/* The header is still in L1, probably not for long */
5637 			flags |= ARC_CACHED_IN_L1;
5638 			break;
5639 		default:
5640 			break;
5641 		}
5642 	}
5643 	if (HDR_HAS_L2HDR(hdr))
5644 		flags |= ARC_CACHED_IN_L2;
5645 
5646 	mutex_exit(hash_lock);
5647 
5648 	return (flags);
5649 }
5650 
5651 /*
5652  * "Read" the block at the specified DVA (in bp) via the
5653  * cache.  If the block is found in the cache, invoke the provided
5654  * callback immediately and return.  Note that the `zio' parameter
5655  * in the callback will be NULL in this case, since no IO was
5656  * required.  If the block is not in the cache pass the read request
5657  * on to the spa with a substitute callback function, so that the
5658  * requested block will be added to the cache.
5659  *
5660  * If a read request arrives for a block that has a read in-progress,
5661  * either wait for the in-progress read to complete (and return the
5662  * results); or, if this is a read with a "done" func, add a record
5663  * to the read to invoke the "done" func when the read completes,
5664  * and return; or just return.
5665  *
5666  * arc_read_done() will invoke all the requested "done" functions
5667  * for readers of this block.
5668  */
5669 int
arc_read(zio_t * pio,spa_t * spa,const blkptr_t * bp,arc_read_done_func_t * done,void * private,zio_priority_t priority,int zio_flags,arc_flags_t * arc_flags,const zbookmark_phys_t * zb)5670 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
5671     arc_read_done_func_t *done, void *private, zio_priority_t priority,
5672     int zio_flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb)
5673 {
5674 	arc_buf_hdr_t *hdr = NULL;
5675 	kmutex_t *hash_lock = NULL;
5676 	zio_t *rzio;
5677 	uint64_t guid = spa_load_guid(spa);
5678 	boolean_t compressed_read = (zio_flags & ZIO_FLAG_RAW_COMPRESS) != 0;
5679 	boolean_t encrypted_read = BP_IS_ENCRYPTED(bp) &&
5680 	    (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0;
5681 	boolean_t noauth_read = BP_IS_AUTHENTICATED(bp) &&
5682 	    (zio_flags & ZIO_FLAG_RAW_ENCRYPT) != 0;
5683 	boolean_t embedded_bp = !!BP_IS_EMBEDDED(bp);
5684 	boolean_t no_buf = *arc_flags & ARC_FLAG_NO_BUF;
5685 	arc_buf_t *buf = NULL;
5686 	int rc = 0;
5687 	boolean_t bp_validation = B_FALSE;
5688 
5689 	ASSERT(!embedded_bp ||
5690 	    BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA);
5691 	ASSERT(!BP_IS_HOLE(bp));
5692 	ASSERT(!BP_IS_REDACTED(bp));
5693 
5694 	/*
5695 	 * Normally SPL_FSTRANS will already be set since kernel threads which
5696 	 * expect to call the DMU interfaces will set it when created.  System
5697 	 * calls are similarly handled by setting/cleaning the bit in the
5698 	 * registered callback (module/os/.../zfs/zpl_*).
5699 	 *
5700 	 * External consumers such as Lustre which call the exported DMU
5701 	 * interfaces may not have set SPL_FSTRANS.  To avoid a deadlock
5702 	 * on the hash_lock always set and clear the bit.
5703 	 */
5704 	fstrans_cookie_t cookie = spl_fstrans_mark();
5705 top:
5706 	if (!embedded_bp) {
5707 		/*
5708 		 * Embedded BP's have no DVA and require no I/O to "read".
5709 		 * Create an anonymous arc buf to back it.
5710 		 */
5711 		hdr = buf_hash_find(guid, bp, &hash_lock);
5712 	}
5713 
5714 	/*
5715 	 * Determine if we have an L1 cache hit or a cache miss. For simplicity
5716 	 * we maintain encrypted data separately from compressed / uncompressed
5717 	 * data. If the user is requesting raw encrypted data and we don't have
5718 	 * that in the header we will read from disk to guarantee that we can
5719 	 * get it even if the encryption keys aren't loaded.
5720 	 */
5721 	if (hdr != NULL && HDR_HAS_L1HDR(hdr) && (HDR_HAS_RABD(hdr) ||
5722 	    (hdr->b_l1hdr.b_pabd != NULL && !encrypted_read))) {
5723 		boolean_t is_data = !HDR_ISTYPE_METADATA(hdr);
5724 
5725 		/*
5726 		 * Verify the block pointer contents are reasonable.  This
5727 		 * should always be the case since the blkptr is protected by
5728 		 * a checksum.
5729 		 */
5730 		if (zfs_blkptr_verify(spa, bp, BLK_CONFIG_SKIP,
5731 		    BLK_VERIFY_LOG)) {
5732 			mutex_exit(hash_lock);
5733 			rc = SET_ERROR(ECKSUM);
5734 			goto done;
5735 		}
5736 
5737 		if (HDR_IO_IN_PROGRESS(hdr)) {
5738 			if (*arc_flags & ARC_FLAG_CACHED_ONLY) {
5739 				mutex_exit(hash_lock);
5740 				ARCSTAT_BUMP(arcstat_cached_only_in_progress);
5741 				rc = SET_ERROR(ENOENT);
5742 				goto done;
5743 			}
5744 
5745 			zio_t *head_zio = hdr->b_l1hdr.b_acb->acb_zio_head;
5746 			ASSERT3P(head_zio, !=, NULL);
5747 			if ((hdr->b_flags & ARC_FLAG_PRIO_ASYNC_READ) &&
5748 			    priority == ZIO_PRIORITY_SYNC_READ) {
5749 				/*
5750 				 * This is a sync read that needs to wait for
5751 				 * an in-flight async read. Request that the
5752 				 * zio have its priority upgraded.
5753 				 */
5754 				zio_change_priority(head_zio, priority);
5755 				DTRACE_PROBE1(arc__async__upgrade__sync,
5756 				    arc_buf_hdr_t *, hdr);
5757 				ARCSTAT_BUMP(arcstat_async_upgrade_sync);
5758 			}
5759 
5760 			DTRACE_PROBE1(arc__iohit, arc_buf_hdr_t *, hdr);
5761 			arc_access(hdr, *arc_flags, B_FALSE);
5762 
5763 			/*
5764 			 * If there are multiple threads reading the same block
5765 			 * and that block is not yet in the ARC, then only one
5766 			 * thread will do the physical I/O and all other
5767 			 * threads will wait until that I/O completes.
5768 			 * Synchronous reads use the acb_wait_cv whereas nowait
5769 			 * reads register a callback. Both are signalled/called
5770 			 * in arc_read_done.
5771 			 *
5772 			 * Errors of the physical I/O may need to be propagated.
5773 			 * Synchronous read errors are returned here from
5774 			 * arc_read_done via acb_wait_error.  Nowait reads
5775 			 * attach the acb_zio_dummy zio to pio and
5776 			 * arc_read_done propagates the physical I/O's io_error
5777 			 * to acb_zio_dummy, and thereby to pio.
5778 			 */
5779 			arc_callback_t *acb = NULL;
5780 			if (done || pio || *arc_flags & ARC_FLAG_WAIT) {
5781 				acb = kmem_zalloc(sizeof (arc_callback_t),
5782 				    KM_SLEEP);
5783 				acb->acb_done = done;
5784 				acb->acb_private = private;
5785 				acb->acb_compressed = compressed_read;
5786 				acb->acb_encrypted = encrypted_read;
5787 				acb->acb_noauth = noauth_read;
5788 				acb->acb_nobuf = no_buf;
5789 				if (*arc_flags & ARC_FLAG_WAIT) {
5790 					acb->acb_wait = B_TRUE;
5791 					mutex_init(&acb->acb_wait_lock, NULL,
5792 					    MUTEX_DEFAULT, NULL);
5793 					cv_init(&acb->acb_wait_cv, NULL,
5794 					    CV_DEFAULT, NULL);
5795 				}
5796 				acb->acb_zb = *zb;
5797 				if (pio != NULL) {
5798 					acb->acb_zio_dummy = zio_null(pio,
5799 					    spa, NULL, NULL, NULL, zio_flags);
5800 				}
5801 				acb->acb_zio_head = head_zio;
5802 				acb->acb_next = hdr->b_l1hdr.b_acb;
5803 				hdr->b_l1hdr.b_acb->acb_prev = acb;
5804 				hdr->b_l1hdr.b_acb = acb;
5805 			}
5806 			mutex_exit(hash_lock);
5807 
5808 			ARCSTAT_BUMP(arcstat_iohits);
5809 			ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH),
5810 			    demand, prefetch, is_data, data, metadata, iohits);
5811 
5812 			if (*arc_flags & ARC_FLAG_WAIT) {
5813 				mutex_enter(&acb->acb_wait_lock);
5814 				while (acb->acb_wait) {
5815 					cv_wait(&acb->acb_wait_cv,
5816 					    &acb->acb_wait_lock);
5817 				}
5818 				rc = acb->acb_wait_error;
5819 				mutex_exit(&acb->acb_wait_lock);
5820 				mutex_destroy(&acb->acb_wait_lock);
5821 				cv_destroy(&acb->acb_wait_cv);
5822 				kmem_free(acb, sizeof (arc_callback_t));
5823 			}
5824 			goto out;
5825 		}
5826 
5827 		ASSERT(hdr->b_l1hdr.b_state == arc_mru ||
5828 		    hdr->b_l1hdr.b_state == arc_mfu ||
5829 		    hdr->b_l1hdr.b_state == arc_uncached);
5830 
5831 		DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
5832 		arc_access(hdr, *arc_flags, B_TRUE);
5833 
5834 		if (done && !no_buf) {
5835 			ASSERT(!embedded_bp || !BP_IS_HOLE(bp));
5836 
5837 			/* Get a buf with the desired data in it. */
5838 			rc = arc_buf_alloc_impl(hdr, spa, zb, private,
5839 			    encrypted_read, compressed_read, noauth_read,
5840 			    B_TRUE, &buf);
5841 			if (rc == ECKSUM) {
5842 				/*
5843 				 * Convert authentication and decryption errors
5844 				 * to EIO (and generate an ereport if needed)
5845 				 * before leaving the ARC.
5846 				 */
5847 				rc = SET_ERROR(EIO);
5848 				if ((zio_flags & ZIO_FLAG_SPECULATIVE) == 0) {
5849 					spa_log_error(spa, zb, hdr->b_birth);
5850 					(void) zfs_ereport_post(
5851 					    FM_EREPORT_ZFS_AUTHENTICATION,
5852 					    spa, NULL, zb, NULL, 0);
5853 				}
5854 			}
5855 			if (rc != 0) {
5856 				arc_buf_destroy_impl(buf);
5857 				buf = NULL;
5858 				(void) remove_reference(hdr, private);
5859 			}
5860 
5861 			/* assert any errors weren't due to unloaded keys */
5862 			ASSERT((zio_flags & ZIO_FLAG_SPECULATIVE) ||
5863 			    rc != EACCES);
5864 		}
5865 		mutex_exit(hash_lock);
5866 		ARCSTAT_BUMP(arcstat_hits);
5867 		ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH),
5868 		    demand, prefetch, is_data, data, metadata, hits);
5869 		*arc_flags |= ARC_FLAG_CACHED;
5870 		goto done;
5871 	} else {
5872 		uint64_t lsize = BP_GET_LSIZE(bp);
5873 		uint64_t psize = BP_GET_PSIZE(bp);
5874 		arc_callback_t *acb;
5875 		vdev_t *vd = NULL;
5876 		uint64_t addr = 0;
5877 		boolean_t devw = B_FALSE;
5878 		uint64_t size;
5879 		abd_t *hdr_abd;
5880 		int alloc_flags = encrypted_read ? ARC_HDR_ALLOC_RDATA : 0;
5881 		arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
5882 		int config_lock;
5883 		int error;
5884 
5885 		if (*arc_flags & ARC_FLAG_CACHED_ONLY) {
5886 			if (hash_lock != NULL)
5887 				mutex_exit(hash_lock);
5888 			rc = SET_ERROR(ENOENT);
5889 			goto done;
5890 		}
5891 
5892 		if (zio_flags & ZIO_FLAG_CONFIG_WRITER) {
5893 			config_lock = BLK_CONFIG_HELD;
5894 		} else if (hash_lock != NULL) {
5895 			/*
5896 			 * Prevent lock order reversal
5897 			 */
5898 			config_lock = BLK_CONFIG_NEEDED_TRY;
5899 		} else {
5900 			config_lock = BLK_CONFIG_NEEDED;
5901 		}
5902 
5903 		/*
5904 		 * Verify the block pointer contents are reasonable.  This
5905 		 * should always be the case since the blkptr is protected by
5906 		 * a checksum.
5907 		 */
5908 		if (!bp_validation && (error = zfs_blkptr_verify(spa, bp,
5909 		    config_lock, BLK_VERIFY_LOG))) {
5910 			if (hash_lock != NULL)
5911 				mutex_exit(hash_lock);
5912 			if (error == EBUSY && !zfs_blkptr_verify(spa, bp,
5913 			    BLK_CONFIG_NEEDED, BLK_VERIFY_LOG)) {
5914 				bp_validation = B_TRUE;
5915 				goto top;
5916 			}
5917 			rc = SET_ERROR(ECKSUM);
5918 			goto done;
5919 		}
5920 
5921 		if (hdr == NULL) {
5922 			/*
5923 			 * This block is not in the cache or it has
5924 			 * embedded data.
5925 			 */
5926 			arc_buf_hdr_t *exists = NULL;
5927 			hdr = arc_hdr_alloc(guid, psize, lsize,
5928 			    BP_IS_PROTECTED(bp), BP_GET_COMPRESS(bp), 0, type);
5929 
5930 			if (!embedded_bp) {
5931 				hdr->b_dva = *BP_IDENTITY(bp);
5932 				hdr->b_birth = BP_GET_BIRTH(bp);
5933 				exists = buf_hash_insert(hdr, &hash_lock);
5934 			}
5935 			if (exists != NULL) {
5936 				/* somebody beat us to the hash insert */
5937 				mutex_exit(hash_lock);
5938 				buf_discard_identity(hdr);
5939 				arc_hdr_destroy(hdr);
5940 				goto top; /* restart the IO request */
5941 			}
5942 		} else {
5943 			/*
5944 			 * This block is in the ghost cache or encrypted data
5945 			 * was requested and we didn't have it. If it was
5946 			 * L2-only (and thus didn't have an L1 hdr),
5947 			 * we realloc the header to add an L1 hdr.
5948 			 */
5949 			if (!HDR_HAS_L1HDR(hdr)) {
5950 				hdr = arc_hdr_realloc(hdr, hdr_l2only_cache,
5951 				    hdr_full_cache);
5952 			}
5953 
5954 			if (GHOST_STATE(hdr->b_l1hdr.b_state)) {
5955 				ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
5956 				ASSERT(!HDR_HAS_RABD(hdr));
5957 				ASSERT(!HDR_IO_IN_PROGRESS(hdr));
5958 				ASSERT0(zfs_refcount_count(
5959 				    &hdr->b_l1hdr.b_refcnt));
5960 				ASSERT3P(hdr->b_l1hdr.b_buf, ==, NULL);
5961 #ifdef ZFS_DEBUG
5962 				ASSERT3P(hdr->b_l1hdr.b_freeze_cksum, ==, NULL);
5963 #endif
5964 			} else if (HDR_IO_IN_PROGRESS(hdr)) {
5965 				/*
5966 				 * If this header already had an IO in progress
5967 				 * and we are performing another IO to fetch
5968 				 * encrypted data we must wait until the first
5969 				 * IO completes so as not to confuse
5970 				 * arc_read_done(). This should be very rare
5971 				 * and so the performance impact shouldn't
5972 				 * matter.
5973 				 */
5974 				arc_callback_t *acb = kmem_zalloc(
5975 				    sizeof (arc_callback_t), KM_SLEEP);
5976 				acb->acb_wait = B_TRUE;
5977 				mutex_init(&acb->acb_wait_lock, NULL,
5978 				    MUTEX_DEFAULT, NULL);
5979 				cv_init(&acb->acb_wait_cv, NULL, CV_DEFAULT,
5980 				    NULL);
5981 				acb->acb_zio_head =
5982 				    hdr->b_l1hdr.b_acb->acb_zio_head;
5983 				acb->acb_next = hdr->b_l1hdr.b_acb;
5984 				hdr->b_l1hdr.b_acb->acb_prev = acb;
5985 				hdr->b_l1hdr.b_acb = acb;
5986 				mutex_exit(hash_lock);
5987 				mutex_enter(&acb->acb_wait_lock);
5988 				while (acb->acb_wait) {
5989 					cv_wait(&acb->acb_wait_cv,
5990 					    &acb->acb_wait_lock);
5991 				}
5992 				mutex_exit(&acb->acb_wait_lock);
5993 				mutex_destroy(&acb->acb_wait_lock);
5994 				cv_destroy(&acb->acb_wait_cv);
5995 				kmem_free(acb, sizeof (arc_callback_t));
5996 				goto top;
5997 			}
5998 		}
5999 		if (*arc_flags & ARC_FLAG_UNCACHED) {
6000 			arc_hdr_set_flags(hdr, ARC_FLAG_UNCACHED);
6001 			if (!encrypted_read)
6002 				alloc_flags |= ARC_HDR_ALLOC_LINEAR;
6003 		}
6004 
6005 		/*
6006 		 * Take additional reference for IO_IN_PROGRESS.  It stops
6007 		 * arc_access() from putting this header without any buffers
6008 		 * and so other references but obviously nonevictable onto
6009 		 * the evictable list of MRU or MFU state.
6010 		 */
6011 		add_reference(hdr, hdr);
6012 		if (!embedded_bp)
6013 			arc_access(hdr, *arc_flags, B_FALSE);
6014 		arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
6015 		arc_hdr_alloc_abd(hdr, alloc_flags);
6016 		if (encrypted_read) {
6017 			ASSERT(HDR_HAS_RABD(hdr));
6018 			size = HDR_GET_PSIZE(hdr);
6019 			hdr_abd = hdr->b_crypt_hdr.b_rabd;
6020 			zio_flags |= ZIO_FLAG_RAW;
6021 		} else {
6022 			ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
6023 			size = arc_hdr_size(hdr);
6024 			hdr_abd = hdr->b_l1hdr.b_pabd;
6025 
6026 			if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF) {
6027 				zio_flags |= ZIO_FLAG_RAW_COMPRESS;
6028 			}
6029 
6030 			/*
6031 			 * For authenticated bp's, we do not ask the ZIO layer
6032 			 * to authenticate them since this will cause the entire
6033 			 * IO to fail if the key isn't loaded. Instead, we
6034 			 * defer authentication until arc_buf_fill(), which will
6035 			 * verify the data when the key is available.
6036 			 */
6037 			if (BP_IS_AUTHENTICATED(bp))
6038 				zio_flags |= ZIO_FLAG_RAW_ENCRYPT;
6039 		}
6040 
6041 		if (BP_IS_AUTHENTICATED(bp))
6042 			arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH);
6043 		if (BP_GET_LEVEL(bp) > 0)
6044 			arc_hdr_set_flags(hdr, ARC_FLAG_INDIRECT);
6045 		ASSERT(!GHOST_STATE(hdr->b_l1hdr.b_state));
6046 
6047 		acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
6048 		acb->acb_done = done;
6049 		acb->acb_private = private;
6050 		acb->acb_compressed = compressed_read;
6051 		acb->acb_encrypted = encrypted_read;
6052 		acb->acb_noauth = noauth_read;
6053 		acb->acb_nobuf = no_buf;
6054 		acb->acb_zb = *zb;
6055 
6056 		ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
6057 		hdr->b_l1hdr.b_acb = acb;
6058 
6059 		if (HDR_HAS_L2HDR(hdr) &&
6060 		    (vd = hdr->b_l2hdr.b_dev->l2ad_vdev) != NULL) {
6061 			devw = hdr->b_l2hdr.b_dev->l2ad_writing;
6062 			addr = hdr->b_l2hdr.b_daddr;
6063 			/*
6064 			 * Lock out L2ARC device removal.
6065 			 */
6066 			if (vdev_is_dead(vd) ||
6067 			    !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
6068 				vd = NULL;
6069 		}
6070 
6071 		/*
6072 		 * We count both async reads and scrub IOs as asynchronous so
6073 		 * that both can be upgraded in the event of a cache hit while
6074 		 * the read IO is still in-flight.
6075 		 */
6076 		if (priority == ZIO_PRIORITY_ASYNC_READ ||
6077 		    priority == ZIO_PRIORITY_SCRUB)
6078 			arc_hdr_set_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ);
6079 		else
6080 			arc_hdr_clear_flags(hdr, ARC_FLAG_PRIO_ASYNC_READ);
6081 
6082 		/*
6083 		 * At this point, we have a level 1 cache miss or a blkptr
6084 		 * with embedded data.  Try again in L2ARC if possible.
6085 		 */
6086 		ASSERT3U(HDR_GET_LSIZE(hdr), ==, lsize);
6087 
6088 		/*
6089 		 * Skip ARC stat bump for block pointers with embedded
6090 		 * data. The data are read from the blkptr itself via
6091 		 * decode_embedded_bp_compressed().
6092 		 */
6093 		if (!embedded_bp) {
6094 			DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr,
6095 			    blkptr_t *, bp, uint64_t, lsize,
6096 			    zbookmark_phys_t *, zb);
6097 			ARCSTAT_BUMP(arcstat_misses);
6098 			ARCSTAT_CONDSTAT(!(*arc_flags & ARC_FLAG_PREFETCH),
6099 			    demand, prefetch, !HDR_ISTYPE_METADATA(hdr), data,
6100 			    metadata, misses);
6101 			zfs_racct_read(spa, size, 1, 0);
6102 		}
6103 
6104 		/* Check if the spa even has l2 configured */
6105 		const boolean_t spa_has_l2 = l2arc_ndev != 0 &&
6106 		    spa->spa_l2cache.sav_count > 0;
6107 
6108 		if (vd != NULL && spa_has_l2 && !(l2arc_norw && devw)) {
6109 			/*
6110 			 * Read from the L2ARC if the following are true:
6111 			 * 1. The L2ARC vdev was previously cached.
6112 			 * 2. This buffer still has L2ARC metadata.
6113 			 * 3. This buffer isn't currently writing to the L2ARC.
6114 			 * 4. The L2ARC entry wasn't evicted, which may
6115 			 *    also have invalidated the vdev.
6116 			 */
6117 			if (HDR_HAS_L2HDR(hdr) &&
6118 			    !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr)) {
6119 				l2arc_read_callback_t *cb;
6120 				abd_t *abd;
6121 				uint64_t asize;
6122 
6123 				DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
6124 				ARCSTAT_BUMP(arcstat_l2_hits);
6125 				hdr->b_l2hdr.b_hits++;
6126 
6127 				cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
6128 				    KM_SLEEP);
6129 				cb->l2rcb_hdr = hdr;
6130 				cb->l2rcb_bp = *bp;
6131 				cb->l2rcb_zb = *zb;
6132 				cb->l2rcb_flags = zio_flags;
6133 
6134 				/*
6135 				 * When Compressed ARC is disabled, but the
6136 				 * L2ARC block is compressed, arc_hdr_size()
6137 				 * will have returned LSIZE rather than PSIZE.
6138 				 */
6139 				if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
6140 				    !HDR_COMPRESSION_ENABLED(hdr) &&
6141 				    HDR_GET_PSIZE(hdr) != 0) {
6142 					size = HDR_GET_PSIZE(hdr);
6143 				}
6144 
6145 				asize = vdev_psize_to_asize(vd, size);
6146 				if (asize != size) {
6147 					abd = abd_alloc_for_io(asize,
6148 					    HDR_ISTYPE_METADATA(hdr));
6149 					cb->l2rcb_abd = abd;
6150 				} else {
6151 					abd = hdr_abd;
6152 				}
6153 
6154 				ASSERT(addr >= VDEV_LABEL_START_SIZE &&
6155 				    addr + asize <= vd->vdev_psize -
6156 				    VDEV_LABEL_END_SIZE);
6157 
6158 				/*
6159 				 * l2arc read.  The SCL_L2ARC lock will be
6160 				 * released by l2arc_read_done().
6161 				 * Issue a null zio if the underlying buffer
6162 				 * was squashed to zero size by compression.
6163 				 */
6164 				ASSERT3U(arc_hdr_get_compress(hdr), !=,
6165 				    ZIO_COMPRESS_EMPTY);
6166 				rzio = zio_read_phys(pio, vd, addr,
6167 				    asize, abd,
6168 				    ZIO_CHECKSUM_OFF,
6169 				    l2arc_read_done, cb, priority,
6170 				    zio_flags | ZIO_FLAG_CANFAIL |
6171 				    ZIO_FLAG_DONT_PROPAGATE |
6172 				    ZIO_FLAG_DONT_RETRY, B_FALSE);
6173 				acb->acb_zio_head = rzio;
6174 
6175 				if (hash_lock != NULL)
6176 					mutex_exit(hash_lock);
6177 
6178 				DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
6179 				    zio_t *, rzio);
6180 				ARCSTAT_INCR(arcstat_l2_read_bytes,
6181 				    HDR_GET_PSIZE(hdr));
6182 
6183 				if (*arc_flags & ARC_FLAG_NOWAIT) {
6184 					zio_nowait(rzio);
6185 					goto out;
6186 				}
6187 
6188 				ASSERT(*arc_flags & ARC_FLAG_WAIT);
6189 				if (zio_wait(rzio) == 0)
6190 					goto out;
6191 
6192 				/* l2arc read error; goto zio_read() */
6193 				if (hash_lock != NULL)
6194 					mutex_enter(hash_lock);
6195 			} else {
6196 				DTRACE_PROBE1(l2arc__miss,
6197 				    arc_buf_hdr_t *, hdr);
6198 				ARCSTAT_BUMP(arcstat_l2_misses);
6199 				if (HDR_L2_WRITING(hdr))
6200 					ARCSTAT_BUMP(arcstat_l2_rw_clash);
6201 				spa_config_exit(spa, SCL_L2ARC, vd);
6202 			}
6203 		} else {
6204 			if (vd != NULL)
6205 				spa_config_exit(spa, SCL_L2ARC, vd);
6206 
6207 			/*
6208 			 * Only a spa with l2 should contribute to l2
6209 			 * miss stats.  (Including the case of having a
6210 			 * faulted cache device - that's also a miss.)
6211 			 */
6212 			if (spa_has_l2) {
6213 				/*
6214 				 * Skip ARC stat bump for block pointers with
6215 				 * embedded data. The data are read from the
6216 				 * blkptr itself via
6217 				 * decode_embedded_bp_compressed().
6218 				 */
6219 				if (!embedded_bp) {
6220 					DTRACE_PROBE1(l2arc__miss,
6221 					    arc_buf_hdr_t *, hdr);
6222 					ARCSTAT_BUMP(arcstat_l2_misses);
6223 				}
6224 			}
6225 		}
6226 
6227 		rzio = zio_read(pio, spa, bp, hdr_abd, size,
6228 		    arc_read_done, hdr, priority, zio_flags, zb);
6229 		acb->acb_zio_head = rzio;
6230 
6231 		if (hash_lock != NULL)
6232 			mutex_exit(hash_lock);
6233 
6234 		if (*arc_flags & ARC_FLAG_WAIT) {
6235 			rc = zio_wait(rzio);
6236 			goto out;
6237 		}
6238 
6239 		ASSERT(*arc_flags & ARC_FLAG_NOWAIT);
6240 		zio_nowait(rzio);
6241 	}
6242 
6243 out:
6244 	/* embedded bps don't actually go to disk */
6245 	if (!embedded_bp)
6246 		spa_read_history_add(spa, zb, *arc_flags);
6247 	spl_fstrans_unmark(cookie);
6248 	return (rc);
6249 
6250 done:
6251 	if (done)
6252 		done(NULL, zb, bp, buf, private);
6253 	if (pio && rc != 0) {
6254 		zio_t *zio = zio_null(pio, spa, NULL, NULL, NULL, zio_flags);
6255 		zio->io_error = rc;
6256 		zio_nowait(zio);
6257 	}
6258 	goto out;
6259 }
6260 
6261 arc_prune_t *
arc_add_prune_callback(arc_prune_func_t * func,void * private)6262 arc_add_prune_callback(arc_prune_func_t *func, void *private)
6263 {
6264 	arc_prune_t *p;
6265 
6266 	p = kmem_alloc(sizeof (*p), KM_SLEEP);
6267 	p->p_pfunc = func;
6268 	p->p_private = private;
6269 	list_link_init(&p->p_node);
6270 	zfs_refcount_create(&p->p_refcnt);
6271 
6272 	mutex_enter(&arc_prune_mtx);
6273 	zfs_refcount_add(&p->p_refcnt, &arc_prune_list);
6274 	list_insert_head(&arc_prune_list, p);
6275 	mutex_exit(&arc_prune_mtx);
6276 
6277 	return (p);
6278 }
6279 
6280 void
arc_remove_prune_callback(arc_prune_t * p)6281 arc_remove_prune_callback(arc_prune_t *p)
6282 {
6283 	boolean_t wait = B_FALSE;
6284 	mutex_enter(&arc_prune_mtx);
6285 	list_remove(&arc_prune_list, p);
6286 	if (zfs_refcount_remove(&p->p_refcnt, &arc_prune_list) > 0)
6287 		wait = B_TRUE;
6288 	mutex_exit(&arc_prune_mtx);
6289 
6290 	/* wait for arc_prune_task to finish */
6291 	if (wait)
6292 		taskq_wait_outstanding(arc_prune_taskq, 0);
6293 	ASSERT0(zfs_refcount_count(&p->p_refcnt));
6294 	zfs_refcount_destroy(&p->p_refcnt);
6295 	kmem_free(p, sizeof (*p));
6296 }
6297 
6298 /*
6299  * Helper function for arc_prune_async() it is responsible for safely
6300  * handling the execution of a registered arc_prune_func_t.
6301  */
6302 static void
arc_prune_task(void * ptr)6303 arc_prune_task(void *ptr)
6304 {
6305 	arc_prune_t *ap = (arc_prune_t *)ptr;
6306 	arc_prune_func_t *func = ap->p_pfunc;
6307 
6308 	if (func != NULL)
6309 		func(ap->p_adjust, ap->p_private);
6310 
6311 	(void) zfs_refcount_remove(&ap->p_refcnt, func);
6312 }
6313 
6314 /*
6315  * Notify registered consumers they must drop holds on a portion of the ARC
6316  * buffers they reference.  This provides a mechanism to ensure the ARC can
6317  * honor the metadata limit and reclaim otherwise pinned ARC buffers.
6318  *
6319  * This operation is performed asynchronously so it may be safely called
6320  * in the context of the arc_reclaim_thread().  A reference is taken here
6321  * for each registered arc_prune_t and the arc_prune_task() is responsible
6322  * for releasing it once the registered arc_prune_func_t has completed.
6323  */
6324 static void
arc_prune_async(uint64_t adjust)6325 arc_prune_async(uint64_t adjust)
6326 {
6327 	arc_prune_t *ap;
6328 
6329 	mutex_enter(&arc_prune_mtx);
6330 	for (ap = list_head(&arc_prune_list); ap != NULL;
6331 	    ap = list_next(&arc_prune_list, ap)) {
6332 
6333 		if (zfs_refcount_count(&ap->p_refcnt) >= 2)
6334 			continue;
6335 
6336 		zfs_refcount_add(&ap->p_refcnt, ap->p_pfunc);
6337 		ap->p_adjust = adjust;
6338 		if (taskq_dispatch(arc_prune_taskq, arc_prune_task,
6339 		    ap, TQ_SLEEP) == TASKQID_INVALID) {
6340 			(void) zfs_refcount_remove(&ap->p_refcnt, ap->p_pfunc);
6341 			continue;
6342 		}
6343 		ARCSTAT_BUMP(arcstat_prune);
6344 	}
6345 	mutex_exit(&arc_prune_mtx);
6346 }
6347 
6348 /*
6349  * Notify the arc that a block was freed, and thus will never be used again.
6350  */
6351 void
arc_freed(spa_t * spa,const blkptr_t * bp)6352 arc_freed(spa_t *spa, const blkptr_t *bp)
6353 {
6354 	arc_buf_hdr_t *hdr;
6355 	kmutex_t *hash_lock;
6356 	uint64_t guid = spa_load_guid(spa);
6357 
6358 	ASSERT(!BP_IS_EMBEDDED(bp));
6359 
6360 	hdr = buf_hash_find(guid, bp, &hash_lock);
6361 	if (hdr == NULL)
6362 		return;
6363 
6364 	/*
6365 	 * We might be trying to free a block that is still doing I/O
6366 	 * (i.e. prefetch) or has some other reference (i.e. a dedup-ed,
6367 	 * dmu_sync-ed block). A block may also have a reference if it is
6368 	 * part of a dedup-ed, dmu_synced write. The dmu_sync() function would
6369 	 * have written the new block to its final resting place on disk but
6370 	 * without the dedup flag set. This would have left the hdr in the MRU
6371 	 * state and discoverable. When the txg finally syncs it detects that
6372 	 * the block was overridden in open context and issues an override I/O.
6373 	 * Since this is a dedup block, the override I/O will determine if the
6374 	 * block is already in the DDT. If so, then it will replace the io_bp
6375 	 * with the bp from the DDT and allow the I/O to finish. When the I/O
6376 	 * reaches the done callback, dbuf_write_override_done, it will
6377 	 * check to see if the io_bp and io_bp_override are identical.
6378 	 * If they are not, then it indicates that the bp was replaced with
6379 	 * the bp in the DDT and the override bp is freed. This allows
6380 	 * us to arrive here with a reference on a block that is being
6381 	 * freed. So if we have an I/O in progress, or a reference to
6382 	 * this hdr, then we don't destroy the hdr.
6383 	 */
6384 	if (!HDR_HAS_L1HDR(hdr) ||
6385 	    zfs_refcount_is_zero(&hdr->b_l1hdr.b_refcnt)) {
6386 		arc_change_state(arc_anon, hdr);
6387 		arc_hdr_destroy(hdr);
6388 		mutex_exit(hash_lock);
6389 	} else {
6390 		mutex_exit(hash_lock);
6391 	}
6392 
6393 }
6394 
6395 /*
6396  * Release this buffer from the cache, making it an anonymous buffer.  This
6397  * must be done after a read and prior to modifying the buffer contents.
6398  * If the buffer has more than one reference, we must make
6399  * a new hdr for the buffer.
6400  */
6401 void
arc_release(arc_buf_t * buf,const void * tag)6402 arc_release(arc_buf_t *buf, const void *tag)
6403 {
6404 	arc_buf_hdr_t *hdr = buf->b_hdr;
6405 
6406 	/*
6407 	 * It would be nice to assert that if its DMU metadata (level >
6408 	 * 0 || it's the dnode file), then it must be syncing context.
6409 	 * But we don't know that information at this level.
6410 	 */
6411 
6412 	ASSERT(HDR_HAS_L1HDR(hdr));
6413 
6414 	/*
6415 	 * We don't grab the hash lock prior to this check, because if
6416 	 * the buffer's header is in the arc_anon state, it won't be
6417 	 * linked into the hash table.
6418 	 */
6419 	if (hdr->b_l1hdr.b_state == arc_anon) {
6420 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
6421 		ASSERT(!HDR_IN_HASH_TABLE(hdr));
6422 		ASSERT(!HDR_HAS_L2HDR(hdr));
6423 
6424 		ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf);
6425 		ASSERT(ARC_BUF_LAST(buf));
6426 		ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), ==, 1);
6427 		ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
6428 
6429 		hdr->b_l1hdr.b_arc_access = 0;
6430 
6431 		/*
6432 		 * If the buf is being overridden then it may already
6433 		 * have a hdr that is not empty.
6434 		 */
6435 		buf_discard_identity(hdr);
6436 		arc_buf_thaw(buf);
6437 
6438 		return;
6439 	}
6440 
6441 	kmutex_t *hash_lock = HDR_LOCK(hdr);
6442 	mutex_enter(hash_lock);
6443 
6444 	/*
6445 	 * This assignment is only valid as long as the hash_lock is
6446 	 * held, we must be careful not to reference state or the
6447 	 * b_state field after dropping the lock.
6448 	 */
6449 	arc_state_t *state = hdr->b_l1hdr.b_state;
6450 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
6451 	ASSERT3P(state, !=, arc_anon);
6452 	ASSERT3P(state, !=, arc_l2c_only);
6453 
6454 	/* this buffer is not on any list */
6455 	ASSERT3S(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt), >, 0);
6456 
6457 	/*
6458 	 * Do we have more than one buf?
6459 	 */
6460 	if (hdr->b_l1hdr.b_buf != buf || !ARC_BUF_LAST(buf)) {
6461 		arc_buf_hdr_t *nhdr;
6462 		uint64_t spa = hdr->b_spa;
6463 		uint64_t psize = HDR_GET_PSIZE(hdr);
6464 		uint64_t lsize = HDR_GET_LSIZE(hdr);
6465 		boolean_t protected = HDR_PROTECTED(hdr);
6466 		enum zio_compress compress = arc_hdr_get_compress(hdr);
6467 		arc_buf_contents_t type = arc_buf_type(hdr);
6468 
6469 		if (ARC_BUF_SHARED(buf) && !ARC_BUF_COMPRESSED(buf)) {
6470 			ASSERT3P(hdr->b_l1hdr.b_buf, !=, buf);
6471 			ASSERT(ARC_BUF_LAST(buf));
6472 		}
6473 
6474 		/*
6475 		 * Pull the buffer off of this hdr and find the last buffer
6476 		 * in the hdr's buffer list.
6477 		 */
6478 		VERIFY3S(remove_reference(hdr, tag), >, 0);
6479 		arc_buf_t *lastbuf = arc_buf_remove(hdr, buf);
6480 		ASSERT3P(lastbuf, !=, NULL);
6481 
6482 		/*
6483 		 * If the current arc_buf_t and the hdr are sharing their data
6484 		 * buffer, then we must stop sharing that block.
6485 		 */
6486 		if (ARC_BUF_SHARED(buf)) {
6487 			ASSERT(!arc_buf_is_shared(lastbuf));
6488 
6489 			/*
6490 			 * First, sever the block sharing relationship between
6491 			 * buf and the arc_buf_hdr_t.
6492 			 */
6493 			arc_unshare_buf(hdr, buf);
6494 
6495 			/*
6496 			 * Now we need to recreate the hdr's b_pabd. Since we
6497 			 * have lastbuf handy, we try to share with it, but if
6498 			 * we can't then we allocate a new b_pabd and copy the
6499 			 * data from buf into it.
6500 			 */
6501 			if (arc_can_share(hdr, lastbuf)) {
6502 				arc_share_buf(hdr, lastbuf);
6503 			} else {
6504 				arc_hdr_alloc_abd(hdr, 0);
6505 				abd_copy_from_buf(hdr->b_l1hdr.b_pabd,
6506 				    buf->b_data, psize);
6507 			}
6508 		} else if (HDR_SHARED_DATA(hdr)) {
6509 			/*
6510 			 * Uncompressed shared buffers are always at the end
6511 			 * of the list. Compressed buffers don't have the
6512 			 * same requirements. This makes it hard to
6513 			 * simply assert that the lastbuf is shared so
6514 			 * we rely on the hdr's compression flags to determine
6515 			 * if we have a compressed, shared buffer.
6516 			 */
6517 			ASSERT(arc_buf_is_shared(lastbuf) ||
6518 			    arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF);
6519 			ASSERT(!arc_buf_is_shared(buf));
6520 		}
6521 
6522 		ASSERT(hdr->b_l1hdr.b_pabd != NULL || HDR_HAS_RABD(hdr));
6523 
6524 		(void) zfs_refcount_remove_many(&state->arcs_size[type],
6525 		    arc_buf_size(buf), buf);
6526 
6527 		arc_cksum_verify(buf);
6528 		arc_buf_unwatch(buf);
6529 
6530 		/* if this is the last uncompressed buf free the checksum */
6531 		if (!arc_hdr_has_uncompressed_buf(hdr))
6532 			arc_cksum_free(hdr);
6533 
6534 		mutex_exit(hash_lock);
6535 
6536 		nhdr = arc_hdr_alloc(spa, psize, lsize, protected,
6537 		    compress, hdr->b_complevel, type);
6538 		ASSERT3P(nhdr->b_l1hdr.b_buf, ==, NULL);
6539 		ASSERT0(zfs_refcount_count(&nhdr->b_l1hdr.b_refcnt));
6540 		VERIFY3U(nhdr->b_type, ==, type);
6541 		ASSERT(!HDR_SHARED_DATA(nhdr));
6542 
6543 		nhdr->b_l1hdr.b_buf = buf;
6544 		(void) zfs_refcount_add(&nhdr->b_l1hdr.b_refcnt, tag);
6545 		buf->b_hdr = nhdr;
6546 
6547 		(void) zfs_refcount_add_many(&arc_anon->arcs_size[type],
6548 		    arc_buf_size(buf), buf);
6549 	} else {
6550 		ASSERT(zfs_refcount_count(&hdr->b_l1hdr.b_refcnt) == 1);
6551 		/* protected by hash lock, or hdr is on arc_anon */
6552 		ASSERT(!multilist_link_active(&hdr->b_l1hdr.b_arc_node));
6553 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
6554 
6555 		if (HDR_HAS_L2HDR(hdr)) {
6556 			mutex_enter(&hdr->b_l2hdr.b_dev->l2ad_mtx);
6557 			/* Recheck to prevent race with l2arc_evict(). */
6558 			if (HDR_HAS_L2HDR(hdr))
6559 				arc_hdr_l2hdr_destroy(hdr);
6560 			mutex_exit(&hdr->b_l2hdr.b_dev->l2ad_mtx);
6561 		}
6562 
6563 		hdr->b_l1hdr.b_mru_hits = 0;
6564 		hdr->b_l1hdr.b_mru_ghost_hits = 0;
6565 		hdr->b_l1hdr.b_mfu_hits = 0;
6566 		hdr->b_l1hdr.b_mfu_ghost_hits = 0;
6567 		arc_change_state(arc_anon, hdr);
6568 		hdr->b_l1hdr.b_arc_access = 0;
6569 
6570 		mutex_exit(hash_lock);
6571 		buf_discard_identity(hdr);
6572 		arc_buf_thaw(buf);
6573 	}
6574 }
6575 
6576 int
arc_released(arc_buf_t * buf)6577 arc_released(arc_buf_t *buf)
6578 {
6579 	return (buf->b_data != NULL &&
6580 	    buf->b_hdr->b_l1hdr.b_state == arc_anon);
6581 }
6582 
6583 #ifdef ZFS_DEBUG
6584 int
arc_referenced(arc_buf_t * buf)6585 arc_referenced(arc_buf_t *buf)
6586 {
6587 	return (zfs_refcount_count(&buf->b_hdr->b_l1hdr.b_refcnt));
6588 }
6589 #endif
6590 
6591 static void
arc_write_ready(zio_t * zio)6592 arc_write_ready(zio_t *zio)
6593 {
6594 	arc_write_callback_t *callback = zio->io_private;
6595 	arc_buf_t *buf = callback->awcb_buf;
6596 	arc_buf_hdr_t *hdr = buf->b_hdr;
6597 	blkptr_t *bp = zio->io_bp;
6598 	uint64_t psize = BP_IS_HOLE(bp) ? 0 : BP_GET_PSIZE(bp);
6599 	fstrans_cookie_t cookie = spl_fstrans_mark();
6600 
6601 	ASSERT(HDR_HAS_L1HDR(hdr));
6602 	ASSERT(!zfs_refcount_is_zero(&buf->b_hdr->b_l1hdr.b_refcnt));
6603 	ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL);
6604 
6605 	/*
6606 	 * If we're reexecuting this zio because the pool suspended, then
6607 	 * cleanup any state that was previously set the first time the
6608 	 * callback was invoked.
6609 	 */
6610 	if (zio->io_flags & ZIO_FLAG_REEXECUTED) {
6611 		arc_cksum_free(hdr);
6612 		arc_buf_unwatch(buf);
6613 		if (hdr->b_l1hdr.b_pabd != NULL) {
6614 			if (ARC_BUF_SHARED(buf)) {
6615 				arc_unshare_buf(hdr, buf);
6616 			} else {
6617 				ASSERT(!arc_buf_is_shared(buf));
6618 				arc_hdr_free_abd(hdr, B_FALSE);
6619 			}
6620 		}
6621 
6622 		if (HDR_HAS_RABD(hdr))
6623 			arc_hdr_free_abd(hdr, B_TRUE);
6624 	}
6625 	ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
6626 	ASSERT(!HDR_HAS_RABD(hdr));
6627 	ASSERT(!HDR_SHARED_DATA(hdr));
6628 	ASSERT(!arc_buf_is_shared(buf));
6629 
6630 	callback->awcb_ready(zio, buf, callback->awcb_private);
6631 
6632 	if (HDR_IO_IN_PROGRESS(hdr)) {
6633 		ASSERT(zio->io_flags & ZIO_FLAG_REEXECUTED);
6634 	} else {
6635 		arc_hdr_set_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
6636 		add_reference(hdr, hdr); /* For IO_IN_PROGRESS. */
6637 	}
6638 
6639 	if (BP_IS_PROTECTED(bp)) {
6640 		/* ZIL blocks are written through zio_rewrite */
6641 		ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG);
6642 
6643 		if (BP_SHOULD_BYTESWAP(bp)) {
6644 			if (BP_GET_LEVEL(bp) > 0) {
6645 				hdr->b_l1hdr.b_byteswap = DMU_BSWAP_UINT64;
6646 			} else {
6647 				hdr->b_l1hdr.b_byteswap =
6648 				    DMU_OT_BYTESWAP(BP_GET_TYPE(bp));
6649 			}
6650 		} else {
6651 			hdr->b_l1hdr.b_byteswap = DMU_BSWAP_NUMFUNCS;
6652 		}
6653 
6654 		arc_hdr_set_flags(hdr, ARC_FLAG_PROTECTED);
6655 		hdr->b_crypt_hdr.b_ot = BP_GET_TYPE(bp);
6656 		hdr->b_crypt_hdr.b_dsobj = zio->io_bookmark.zb_objset;
6657 		zio_crypt_decode_params_bp(bp, hdr->b_crypt_hdr.b_salt,
6658 		    hdr->b_crypt_hdr.b_iv);
6659 		zio_crypt_decode_mac_bp(bp, hdr->b_crypt_hdr.b_mac);
6660 	} else {
6661 		arc_hdr_clear_flags(hdr, ARC_FLAG_PROTECTED);
6662 	}
6663 
6664 	/*
6665 	 * If this block was written for raw encryption but the zio layer
6666 	 * ended up only authenticating it, adjust the buffer flags now.
6667 	 */
6668 	if (BP_IS_AUTHENTICATED(bp) && ARC_BUF_ENCRYPTED(buf)) {
6669 		arc_hdr_set_flags(hdr, ARC_FLAG_NOAUTH);
6670 		buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED;
6671 		if (BP_GET_COMPRESS(bp) == ZIO_COMPRESS_OFF)
6672 			buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED;
6673 	} else if (BP_IS_HOLE(bp) && ARC_BUF_ENCRYPTED(buf)) {
6674 		buf->b_flags &= ~ARC_BUF_FLAG_ENCRYPTED;
6675 		buf->b_flags &= ~ARC_BUF_FLAG_COMPRESSED;
6676 	}
6677 
6678 	/* this must be done after the buffer flags are adjusted */
6679 	arc_cksum_compute(buf);
6680 
6681 	enum zio_compress compress;
6682 	if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) {
6683 		compress = ZIO_COMPRESS_OFF;
6684 	} else {
6685 		ASSERT3U(HDR_GET_LSIZE(hdr), ==, BP_GET_LSIZE(bp));
6686 		compress = BP_GET_COMPRESS(bp);
6687 	}
6688 	HDR_SET_PSIZE(hdr, psize);
6689 	arc_hdr_set_compress(hdr, compress);
6690 	hdr->b_complevel = zio->io_prop.zp_complevel;
6691 
6692 	if (zio->io_error != 0 || psize == 0)
6693 		goto out;
6694 
6695 	/*
6696 	 * Fill the hdr with data. If the buffer is encrypted we have no choice
6697 	 * but to copy the data into b_radb. If the hdr is compressed, the data
6698 	 * we want is available from the zio, otherwise we can take it from
6699 	 * the buf.
6700 	 *
6701 	 * We might be able to share the buf's data with the hdr here. However,
6702 	 * doing so would cause the ARC to be full of linear ABDs if we write a
6703 	 * lot of shareable data. As a compromise, we check whether scattered
6704 	 * ABDs are allowed, and assume that if they are then the user wants
6705 	 * the ARC to be primarily filled with them regardless of the data being
6706 	 * written. Therefore, if they're allowed then we allocate one and copy
6707 	 * the data into it; otherwise, we share the data directly if we can.
6708 	 */
6709 	if (ARC_BUF_ENCRYPTED(buf)) {
6710 		ASSERT3U(psize, >, 0);
6711 		ASSERT(ARC_BUF_COMPRESSED(buf));
6712 		arc_hdr_alloc_abd(hdr, ARC_HDR_ALLOC_RDATA |
6713 		    ARC_HDR_USE_RESERVE);
6714 		abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize);
6715 	} else if (!(HDR_UNCACHED(hdr) ||
6716 	    abd_size_alloc_linear(arc_buf_size(buf))) ||
6717 	    !arc_can_share(hdr, buf)) {
6718 		/*
6719 		 * Ideally, we would always copy the io_abd into b_pabd, but the
6720 		 * user may have disabled compressed ARC, thus we must check the
6721 		 * hdr's compression setting rather than the io_bp's.
6722 		 */
6723 		if (BP_IS_ENCRYPTED(bp)) {
6724 			ASSERT3U(psize, >, 0);
6725 			arc_hdr_alloc_abd(hdr, ARC_HDR_ALLOC_RDATA |
6726 			    ARC_HDR_USE_RESERVE);
6727 			abd_copy(hdr->b_crypt_hdr.b_rabd, zio->io_abd, psize);
6728 		} else if (arc_hdr_get_compress(hdr) != ZIO_COMPRESS_OFF &&
6729 		    !ARC_BUF_COMPRESSED(buf)) {
6730 			ASSERT3U(psize, >, 0);
6731 			arc_hdr_alloc_abd(hdr, ARC_HDR_USE_RESERVE);
6732 			abd_copy(hdr->b_l1hdr.b_pabd, zio->io_abd, psize);
6733 		} else {
6734 			ASSERT3U(zio->io_orig_size, ==, arc_hdr_size(hdr));
6735 			arc_hdr_alloc_abd(hdr, ARC_HDR_USE_RESERVE);
6736 			abd_copy_from_buf(hdr->b_l1hdr.b_pabd, buf->b_data,
6737 			    arc_buf_size(buf));
6738 		}
6739 	} else {
6740 		ASSERT3P(buf->b_data, ==, abd_to_buf(zio->io_orig_abd));
6741 		ASSERT3U(zio->io_orig_size, ==, arc_buf_size(buf));
6742 		ASSERT3P(hdr->b_l1hdr.b_buf, ==, buf);
6743 		ASSERT(ARC_BUF_LAST(buf));
6744 
6745 		arc_share_buf(hdr, buf);
6746 	}
6747 
6748 out:
6749 	arc_hdr_verify(hdr, bp);
6750 	spl_fstrans_unmark(cookie);
6751 }
6752 
6753 static void
arc_write_children_ready(zio_t * zio)6754 arc_write_children_ready(zio_t *zio)
6755 {
6756 	arc_write_callback_t *callback = zio->io_private;
6757 	arc_buf_t *buf = callback->awcb_buf;
6758 
6759 	callback->awcb_children_ready(zio, buf, callback->awcb_private);
6760 }
6761 
6762 static void
arc_write_done(zio_t * zio)6763 arc_write_done(zio_t *zio)
6764 {
6765 	arc_write_callback_t *callback = zio->io_private;
6766 	arc_buf_t *buf = callback->awcb_buf;
6767 	arc_buf_hdr_t *hdr = buf->b_hdr;
6768 
6769 	ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
6770 
6771 	if (zio->io_error == 0) {
6772 		arc_hdr_verify(hdr, zio->io_bp);
6773 
6774 		if (BP_IS_HOLE(zio->io_bp) || BP_IS_EMBEDDED(zio->io_bp)) {
6775 			buf_discard_identity(hdr);
6776 		} else {
6777 			hdr->b_dva = *BP_IDENTITY(zio->io_bp);
6778 			hdr->b_birth = BP_GET_BIRTH(zio->io_bp);
6779 		}
6780 	} else {
6781 		ASSERT(HDR_EMPTY(hdr));
6782 	}
6783 
6784 	/*
6785 	 * If the block to be written was all-zero or compressed enough to be
6786 	 * embedded in the BP, no write was performed so there will be no
6787 	 * dva/birth/checksum.  The buffer must therefore remain anonymous
6788 	 * (and uncached).
6789 	 */
6790 	if (!HDR_EMPTY(hdr)) {
6791 		arc_buf_hdr_t *exists;
6792 		kmutex_t *hash_lock;
6793 
6794 		ASSERT3U(zio->io_error, ==, 0);
6795 
6796 		arc_cksum_verify(buf);
6797 
6798 		exists = buf_hash_insert(hdr, &hash_lock);
6799 		if (exists != NULL) {
6800 			/*
6801 			 * This can only happen if we overwrite for
6802 			 * sync-to-convergence, because we remove
6803 			 * buffers from the hash table when we arc_free().
6804 			 */
6805 			if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
6806 				if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
6807 					panic("bad overwrite, hdr=%p exists=%p",
6808 					    (void *)hdr, (void *)exists);
6809 				ASSERT(zfs_refcount_is_zero(
6810 				    &exists->b_l1hdr.b_refcnt));
6811 				arc_change_state(arc_anon, exists);
6812 				arc_hdr_destroy(exists);
6813 				mutex_exit(hash_lock);
6814 				exists = buf_hash_insert(hdr, &hash_lock);
6815 				ASSERT3P(exists, ==, NULL);
6816 			} else if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
6817 				/* nopwrite */
6818 				ASSERT(zio->io_prop.zp_nopwrite);
6819 				if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
6820 					panic("bad nopwrite, hdr=%p exists=%p",
6821 					    (void *)hdr, (void *)exists);
6822 			} else {
6823 				/* Dedup */
6824 				ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL);
6825 				ASSERT(ARC_BUF_LAST(hdr->b_l1hdr.b_buf));
6826 				ASSERT(hdr->b_l1hdr.b_state == arc_anon);
6827 				ASSERT(BP_GET_DEDUP(zio->io_bp));
6828 				ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
6829 			}
6830 		}
6831 		arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
6832 		VERIFY3S(remove_reference(hdr, hdr), >, 0);
6833 		/* if it's not anon, we are doing a scrub */
6834 		if (exists == NULL && hdr->b_l1hdr.b_state == arc_anon)
6835 			arc_access(hdr, 0, B_FALSE);
6836 		mutex_exit(hash_lock);
6837 	} else {
6838 		arc_hdr_clear_flags(hdr, ARC_FLAG_IO_IN_PROGRESS);
6839 		VERIFY3S(remove_reference(hdr, hdr), >, 0);
6840 	}
6841 
6842 	callback->awcb_done(zio, buf, callback->awcb_private);
6843 
6844 	abd_free(zio->io_abd);
6845 	kmem_free(callback, sizeof (arc_write_callback_t));
6846 }
6847 
6848 zio_t *
arc_write(zio_t * pio,spa_t * spa,uint64_t txg,blkptr_t * bp,arc_buf_t * buf,boolean_t uncached,boolean_t l2arc,const zio_prop_t * zp,arc_write_done_func_t * ready,arc_write_done_func_t * children_ready,arc_write_done_func_t * done,void * private,zio_priority_t priority,int zio_flags,const zbookmark_phys_t * zb)6849 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
6850     blkptr_t *bp, arc_buf_t *buf, boolean_t uncached, boolean_t l2arc,
6851     const zio_prop_t *zp, arc_write_done_func_t *ready,
6852     arc_write_done_func_t *children_ready, arc_write_done_func_t *done,
6853     void *private, zio_priority_t priority, int zio_flags,
6854     const zbookmark_phys_t *zb)
6855 {
6856 	arc_buf_hdr_t *hdr = buf->b_hdr;
6857 	arc_write_callback_t *callback;
6858 	zio_t *zio;
6859 	zio_prop_t localprop = *zp;
6860 
6861 	ASSERT3P(ready, !=, NULL);
6862 	ASSERT3P(done, !=, NULL);
6863 	ASSERT(!HDR_IO_ERROR(hdr));
6864 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
6865 	ASSERT3P(hdr->b_l1hdr.b_acb, ==, NULL);
6866 	ASSERT3P(hdr->b_l1hdr.b_buf, !=, NULL);
6867 	if (uncached)
6868 		arc_hdr_set_flags(hdr, ARC_FLAG_UNCACHED);
6869 	else if (l2arc)
6870 		arc_hdr_set_flags(hdr, ARC_FLAG_L2CACHE);
6871 
6872 	if (ARC_BUF_ENCRYPTED(buf)) {
6873 		ASSERT(ARC_BUF_COMPRESSED(buf));
6874 		localprop.zp_encrypt = B_TRUE;
6875 		localprop.zp_compress = HDR_GET_COMPRESS(hdr);
6876 		localprop.zp_complevel = hdr->b_complevel;
6877 		localprop.zp_byteorder =
6878 		    (hdr->b_l1hdr.b_byteswap == DMU_BSWAP_NUMFUNCS) ?
6879 		    ZFS_HOST_BYTEORDER : !ZFS_HOST_BYTEORDER;
6880 		memcpy(localprop.zp_salt, hdr->b_crypt_hdr.b_salt,
6881 		    ZIO_DATA_SALT_LEN);
6882 		memcpy(localprop.zp_iv, hdr->b_crypt_hdr.b_iv,
6883 		    ZIO_DATA_IV_LEN);
6884 		memcpy(localprop.zp_mac, hdr->b_crypt_hdr.b_mac,
6885 		    ZIO_DATA_MAC_LEN);
6886 		if (DMU_OT_IS_ENCRYPTED(localprop.zp_type)) {
6887 			localprop.zp_nopwrite = B_FALSE;
6888 			localprop.zp_copies =
6889 			    MIN(localprop.zp_copies, SPA_DVAS_PER_BP - 1);
6890 			localprop.zp_gang_copies =
6891 			    MIN(localprop.zp_gang_copies, SPA_DVAS_PER_BP - 1);
6892 		}
6893 		zio_flags |= ZIO_FLAG_RAW;
6894 	} else if (ARC_BUF_COMPRESSED(buf)) {
6895 		ASSERT3U(HDR_GET_LSIZE(hdr), !=, arc_buf_size(buf));
6896 		localprop.zp_compress = HDR_GET_COMPRESS(hdr);
6897 		localprop.zp_complevel = hdr->b_complevel;
6898 		zio_flags |= ZIO_FLAG_RAW_COMPRESS;
6899 	}
6900 	callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
6901 	callback->awcb_ready = ready;
6902 	callback->awcb_children_ready = children_ready;
6903 	callback->awcb_done = done;
6904 	callback->awcb_private = private;
6905 	callback->awcb_buf = buf;
6906 
6907 	/*
6908 	 * The hdr's b_pabd is now stale, free it now. A new data block
6909 	 * will be allocated when the zio pipeline calls arc_write_ready().
6910 	 */
6911 	if (hdr->b_l1hdr.b_pabd != NULL) {
6912 		/*
6913 		 * If the buf is currently sharing the data block with
6914 		 * the hdr then we need to break that relationship here.
6915 		 * The hdr will remain with a NULL data pointer and the
6916 		 * buf will take sole ownership of the block.
6917 		 */
6918 		if (ARC_BUF_SHARED(buf)) {
6919 			arc_unshare_buf(hdr, buf);
6920 		} else {
6921 			ASSERT(!arc_buf_is_shared(buf));
6922 			arc_hdr_free_abd(hdr, B_FALSE);
6923 		}
6924 		VERIFY3P(buf->b_data, !=, NULL);
6925 	}
6926 
6927 	if (HDR_HAS_RABD(hdr))
6928 		arc_hdr_free_abd(hdr, B_TRUE);
6929 
6930 	if (!(zio_flags & ZIO_FLAG_RAW))
6931 		arc_hdr_set_compress(hdr, ZIO_COMPRESS_OFF);
6932 
6933 	ASSERT(!arc_buf_is_shared(buf));
6934 	ASSERT3P(hdr->b_l1hdr.b_pabd, ==, NULL);
6935 
6936 	zio = zio_write(pio, spa, txg, bp,
6937 	    abd_get_from_buf(buf->b_data, HDR_GET_LSIZE(hdr)),
6938 	    HDR_GET_LSIZE(hdr), arc_buf_size(buf), &localprop, arc_write_ready,
6939 	    (children_ready != NULL) ? arc_write_children_ready : NULL,
6940 	    arc_write_done, callback, priority, zio_flags, zb);
6941 
6942 	return (zio);
6943 }
6944 
6945 void
arc_tempreserve_clear(uint64_t reserve)6946 arc_tempreserve_clear(uint64_t reserve)
6947 {
6948 	atomic_add_64(&arc_tempreserve, -reserve);
6949 	ASSERT((int64_t)arc_tempreserve >= 0);
6950 }
6951 
6952 int
arc_tempreserve_space(spa_t * spa,uint64_t reserve,uint64_t txg)6953 arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg)
6954 {
6955 	int error;
6956 	uint64_t anon_size;
6957 
6958 	if (!arc_no_grow &&
6959 	    reserve > arc_c/4 &&
6960 	    reserve * 4 > (2ULL << SPA_MAXBLOCKSHIFT))
6961 		arc_c = MIN(arc_c_max, reserve * 4);
6962 
6963 	/*
6964 	 * Throttle when the calculated memory footprint for the TXG
6965 	 * exceeds the target ARC size.
6966 	 */
6967 	if (reserve > arc_c) {
6968 		DMU_TX_STAT_BUMP(dmu_tx_memory_reserve);
6969 		return (SET_ERROR(ERESTART));
6970 	}
6971 
6972 	/*
6973 	 * Don't count loaned bufs as in flight dirty data to prevent long
6974 	 * network delays from blocking transactions that are ready to be
6975 	 * assigned to a txg.
6976 	 */
6977 
6978 	/* assert that it has not wrapped around */
6979 	ASSERT3S(atomic_add_64_nv(&arc_loaned_bytes, 0), >=, 0);
6980 
6981 	anon_size = MAX((int64_t)
6982 	    (zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_DATA]) +
6983 	    zfs_refcount_count(&arc_anon->arcs_size[ARC_BUFC_METADATA]) -
6984 	    arc_loaned_bytes), 0);
6985 
6986 	/*
6987 	 * Writes will, almost always, require additional memory allocations
6988 	 * in order to compress/encrypt/etc the data.  We therefore need to
6989 	 * make sure that there is sufficient available memory for this.
6990 	 */
6991 	error = arc_memory_throttle(spa, reserve, txg);
6992 	if (error != 0)
6993 		return (error);
6994 
6995 	/*
6996 	 * Throttle writes when the amount of dirty data in the cache
6997 	 * gets too large.  We try to keep the cache less than half full
6998 	 * of dirty blocks so that our sync times don't grow too large.
6999 	 *
7000 	 * In the case of one pool being built on another pool, we want
7001 	 * to make sure we don't end up throttling the lower (backing)
7002 	 * pool when the upper pool is the majority contributor to dirty
7003 	 * data. To insure we make forward progress during throttling, we
7004 	 * also check the current pool's net dirty data and only throttle
7005 	 * if it exceeds zfs_arc_pool_dirty_percent of the anonymous dirty
7006 	 * data in the cache.
7007 	 *
7008 	 * Note: if two requests come in concurrently, we might let them
7009 	 * both succeed, when one of them should fail.  Not a huge deal.
7010 	 */
7011 	uint64_t total_dirty = reserve + arc_tempreserve + anon_size;
7012 	uint64_t spa_dirty_anon = spa_dirty_data(spa);
7013 	uint64_t rarc_c = arc_warm ? arc_c : arc_c_max;
7014 	if (total_dirty > rarc_c * zfs_arc_dirty_limit_percent / 100 &&
7015 	    anon_size > rarc_c * zfs_arc_anon_limit_percent / 100 &&
7016 	    spa_dirty_anon > anon_size * zfs_arc_pool_dirty_percent / 100) {
7017 #ifdef ZFS_DEBUG
7018 		uint64_t meta_esize = zfs_refcount_count(
7019 		    &arc_anon->arcs_esize[ARC_BUFC_METADATA]);
7020 		uint64_t data_esize =
7021 		    zfs_refcount_count(&arc_anon->arcs_esize[ARC_BUFC_DATA]);
7022 		dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
7023 		    "anon_data=%lluK tempreserve=%lluK rarc_c=%lluK\n",
7024 		    (u_longlong_t)arc_tempreserve >> 10,
7025 		    (u_longlong_t)meta_esize >> 10,
7026 		    (u_longlong_t)data_esize >> 10,
7027 		    (u_longlong_t)reserve >> 10,
7028 		    (u_longlong_t)rarc_c >> 10);
7029 #endif
7030 		DMU_TX_STAT_BUMP(dmu_tx_dirty_throttle);
7031 		return (SET_ERROR(ERESTART));
7032 	}
7033 	atomic_add_64(&arc_tempreserve, reserve);
7034 	return (0);
7035 }
7036 
7037 static void
arc_kstat_update_state(arc_state_t * state,kstat_named_t * size,kstat_named_t * data,kstat_named_t * metadata,kstat_named_t * evict_data,kstat_named_t * evict_metadata)7038 arc_kstat_update_state(arc_state_t *state, kstat_named_t *size,
7039     kstat_named_t *data, kstat_named_t *metadata,
7040     kstat_named_t *evict_data, kstat_named_t *evict_metadata)
7041 {
7042 	data->value.ui64 =
7043 	    zfs_refcount_count(&state->arcs_size[ARC_BUFC_DATA]);
7044 	metadata->value.ui64 =
7045 	    zfs_refcount_count(&state->arcs_size[ARC_BUFC_METADATA]);
7046 	size->value.ui64 = data->value.ui64 + metadata->value.ui64;
7047 	evict_data->value.ui64 =
7048 	    zfs_refcount_count(&state->arcs_esize[ARC_BUFC_DATA]);
7049 	evict_metadata->value.ui64 =
7050 	    zfs_refcount_count(&state->arcs_esize[ARC_BUFC_METADATA]);
7051 }
7052 
7053 static int
arc_kstat_update(kstat_t * ksp,int rw)7054 arc_kstat_update(kstat_t *ksp, int rw)
7055 {
7056 	arc_stats_t *as = ksp->ks_data;
7057 
7058 	if (rw == KSTAT_WRITE)
7059 		return (SET_ERROR(EACCES));
7060 
7061 	as->arcstat_hits.value.ui64 =
7062 	    wmsum_value(&arc_sums.arcstat_hits);
7063 	as->arcstat_iohits.value.ui64 =
7064 	    wmsum_value(&arc_sums.arcstat_iohits);
7065 	as->arcstat_misses.value.ui64 =
7066 	    wmsum_value(&arc_sums.arcstat_misses);
7067 	as->arcstat_demand_data_hits.value.ui64 =
7068 	    wmsum_value(&arc_sums.arcstat_demand_data_hits);
7069 	as->arcstat_demand_data_iohits.value.ui64 =
7070 	    wmsum_value(&arc_sums.arcstat_demand_data_iohits);
7071 	as->arcstat_demand_data_misses.value.ui64 =
7072 	    wmsum_value(&arc_sums.arcstat_demand_data_misses);
7073 	as->arcstat_demand_metadata_hits.value.ui64 =
7074 	    wmsum_value(&arc_sums.arcstat_demand_metadata_hits);
7075 	as->arcstat_demand_metadata_iohits.value.ui64 =
7076 	    wmsum_value(&arc_sums.arcstat_demand_metadata_iohits);
7077 	as->arcstat_demand_metadata_misses.value.ui64 =
7078 	    wmsum_value(&arc_sums.arcstat_demand_metadata_misses);
7079 	as->arcstat_prefetch_data_hits.value.ui64 =
7080 	    wmsum_value(&arc_sums.arcstat_prefetch_data_hits);
7081 	as->arcstat_prefetch_data_iohits.value.ui64 =
7082 	    wmsum_value(&arc_sums.arcstat_prefetch_data_iohits);
7083 	as->arcstat_prefetch_data_misses.value.ui64 =
7084 	    wmsum_value(&arc_sums.arcstat_prefetch_data_misses);
7085 	as->arcstat_prefetch_metadata_hits.value.ui64 =
7086 	    wmsum_value(&arc_sums.arcstat_prefetch_metadata_hits);
7087 	as->arcstat_prefetch_metadata_iohits.value.ui64 =
7088 	    wmsum_value(&arc_sums.arcstat_prefetch_metadata_iohits);
7089 	as->arcstat_prefetch_metadata_misses.value.ui64 =
7090 	    wmsum_value(&arc_sums.arcstat_prefetch_metadata_misses);
7091 	as->arcstat_mru_hits.value.ui64 =
7092 	    wmsum_value(&arc_sums.arcstat_mru_hits);
7093 	as->arcstat_mru_ghost_hits.value.ui64 =
7094 	    wmsum_value(&arc_sums.arcstat_mru_ghost_hits);
7095 	as->arcstat_mfu_hits.value.ui64 =
7096 	    wmsum_value(&arc_sums.arcstat_mfu_hits);
7097 	as->arcstat_mfu_ghost_hits.value.ui64 =
7098 	    wmsum_value(&arc_sums.arcstat_mfu_ghost_hits);
7099 	as->arcstat_uncached_hits.value.ui64 =
7100 	    wmsum_value(&arc_sums.arcstat_uncached_hits);
7101 	as->arcstat_deleted.value.ui64 =
7102 	    wmsum_value(&arc_sums.arcstat_deleted);
7103 	as->arcstat_mutex_miss.value.ui64 =
7104 	    wmsum_value(&arc_sums.arcstat_mutex_miss);
7105 	as->arcstat_access_skip.value.ui64 =
7106 	    wmsum_value(&arc_sums.arcstat_access_skip);
7107 	as->arcstat_evict_skip.value.ui64 =
7108 	    wmsum_value(&arc_sums.arcstat_evict_skip);
7109 	as->arcstat_evict_not_enough.value.ui64 =
7110 	    wmsum_value(&arc_sums.arcstat_evict_not_enough);
7111 	as->arcstat_evict_l2_cached.value.ui64 =
7112 	    wmsum_value(&arc_sums.arcstat_evict_l2_cached);
7113 	as->arcstat_evict_l2_eligible.value.ui64 =
7114 	    wmsum_value(&arc_sums.arcstat_evict_l2_eligible);
7115 	as->arcstat_evict_l2_eligible_mfu.value.ui64 =
7116 	    wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mfu);
7117 	as->arcstat_evict_l2_eligible_mru.value.ui64 =
7118 	    wmsum_value(&arc_sums.arcstat_evict_l2_eligible_mru);
7119 	as->arcstat_evict_l2_ineligible.value.ui64 =
7120 	    wmsum_value(&arc_sums.arcstat_evict_l2_ineligible);
7121 	as->arcstat_evict_l2_skip.value.ui64 =
7122 	    wmsum_value(&arc_sums.arcstat_evict_l2_skip);
7123 	as->arcstat_hash_elements.value.ui64 =
7124 	    as->arcstat_hash_elements_max.value.ui64 =
7125 	    wmsum_value(&arc_sums.arcstat_hash_elements);
7126 	as->arcstat_hash_collisions.value.ui64 =
7127 	    wmsum_value(&arc_sums.arcstat_hash_collisions);
7128 	as->arcstat_hash_chains.value.ui64 =
7129 	    wmsum_value(&arc_sums.arcstat_hash_chains);
7130 	as->arcstat_size.value.ui64 =
7131 	    aggsum_value(&arc_sums.arcstat_size);
7132 	as->arcstat_compressed_size.value.ui64 =
7133 	    wmsum_value(&arc_sums.arcstat_compressed_size);
7134 	as->arcstat_uncompressed_size.value.ui64 =
7135 	    wmsum_value(&arc_sums.arcstat_uncompressed_size);
7136 	as->arcstat_overhead_size.value.ui64 =
7137 	    wmsum_value(&arc_sums.arcstat_overhead_size);
7138 	as->arcstat_hdr_size.value.ui64 =
7139 	    wmsum_value(&arc_sums.arcstat_hdr_size);
7140 	as->arcstat_data_size.value.ui64 =
7141 	    wmsum_value(&arc_sums.arcstat_data_size);
7142 	as->arcstat_metadata_size.value.ui64 =
7143 	    wmsum_value(&arc_sums.arcstat_metadata_size);
7144 	as->arcstat_dbuf_size.value.ui64 =
7145 	    wmsum_value(&arc_sums.arcstat_dbuf_size);
7146 #if defined(COMPAT_FREEBSD11)
7147 	as->arcstat_other_size.value.ui64 =
7148 	    wmsum_value(&arc_sums.arcstat_bonus_size) +
7149 	    wmsum_value(&arc_sums.arcstat_dnode_size) +
7150 	    wmsum_value(&arc_sums.arcstat_dbuf_size);
7151 #endif
7152 
7153 	arc_kstat_update_state(arc_anon,
7154 	    &as->arcstat_anon_size,
7155 	    &as->arcstat_anon_data,
7156 	    &as->arcstat_anon_metadata,
7157 	    &as->arcstat_anon_evictable_data,
7158 	    &as->arcstat_anon_evictable_metadata);
7159 	arc_kstat_update_state(arc_mru,
7160 	    &as->arcstat_mru_size,
7161 	    &as->arcstat_mru_data,
7162 	    &as->arcstat_mru_metadata,
7163 	    &as->arcstat_mru_evictable_data,
7164 	    &as->arcstat_mru_evictable_metadata);
7165 	arc_kstat_update_state(arc_mru_ghost,
7166 	    &as->arcstat_mru_ghost_size,
7167 	    &as->arcstat_mru_ghost_data,
7168 	    &as->arcstat_mru_ghost_metadata,
7169 	    &as->arcstat_mru_ghost_evictable_data,
7170 	    &as->arcstat_mru_ghost_evictable_metadata);
7171 	arc_kstat_update_state(arc_mfu,
7172 	    &as->arcstat_mfu_size,
7173 	    &as->arcstat_mfu_data,
7174 	    &as->arcstat_mfu_metadata,
7175 	    &as->arcstat_mfu_evictable_data,
7176 	    &as->arcstat_mfu_evictable_metadata);
7177 	arc_kstat_update_state(arc_mfu_ghost,
7178 	    &as->arcstat_mfu_ghost_size,
7179 	    &as->arcstat_mfu_ghost_data,
7180 	    &as->arcstat_mfu_ghost_metadata,
7181 	    &as->arcstat_mfu_ghost_evictable_data,
7182 	    &as->arcstat_mfu_ghost_evictable_metadata);
7183 	arc_kstat_update_state(arc_uncached,
7184 	    &as->arcstat_uncached_size,
7185 	    &as->arcstat_uncached_data,
7186 	    &as->arcstat_uncached_metadata,
7187 	    &as->arcstat_uncached_evictable_data,
7188 	    &as->arcstat_uncached_evictable_metadata);
7189 
7190 	as->arcstat_dnode_size.value.ui64 =
7191 	    wmsum_value(&arc_sums.arcstat_dnode_size);
7192 	as->arcstat_bonus_size.value.ui64 =
7193 	    wmsum_value(&arc_sums.arcstat_bonus_size);
7194 	as->arcstat_l2_hits.value.ui64 =
7195 	    wmsum_value(&arc_sums.arcstat_l2_hits);
7196 	as->arcstat_l2_misses.value.ui64 =
7197 	    wmsum_value(&arc_sums.arcstat_l2_misses);
7198 	as->arcstat_l2_prefetch_asize.value.ui64 =
7199 	    wmsum_value(&arc_sums.arcstat_l2_prefetch_asize);
7200 	as->arcstat_l2_mru_asize.value.ui64 =
7201 	    wmsum_value(&arc_sums.arcstat_l2_mru_asize);
7202 	as->arcstat_l2_mfu_asize.value.ui64 =
7203 	    wmsum_value(&arc_sums.arcstat_l2_mfu_asize);
7204 	as->arcstat_l2_bufc_data_asize.value.ui64 =
7205 	    wmsum_value(&arc_sums.arcstat_l2_bufc_data_asize);
7206 	as->arcstat_l2_bufc_metadata_asize.value.ui64 =
7207 	    wmsum_value(&arc_sums.arcstat_l2_bufc_metadata_asize);
7208 	as->arcstat_l2_feeds.value.ui64 =
7209 	    wmsum_value(&arc_sums.arcstat_l2_feeds);
7210 	as->arcstat_l2_rw_clash.value.ui64 =
7211 	    wmsum_value(&arc_sums.arcstat_l2_rw_clash);
7212 	as->arcstat_l2_read_bytes.value.ui64 =
7213 	    wmsum_value(&arc_sums.arcstat_l2_read_bytes);
7214 	as->arcstat_l2_write_bytes.value.ui64 =
7215 	    wmsum_value(&arc_sums.arcstat_l2_write_bytes);
7216 	as->arcstat_l2_writes_sent.value.ui64 =
7217 	    wmsum_value(&arc_sums.arcstat_l2_writes_sent);
7218 	as->arcstat_l2_writes_done.value.ui64 =
7219 	    wmsum_value(&arc_sums.arcstat_l2_writes_done);
7220 	as->arcstat_l2_writes_error.value.ui64 =
7221 	    wmsum_value(&arc_sums.arcstat_l2_writes_error);
7222 	as->arcstat_l2_writes_lock_retry.value.ui64 =
7223 	    wmsum_value(&arc_sums.arcstat_l2_writes_lock_retry);
7224 	as->arcstat_l2_evict_lock_retry.value.ui64 =
7225 	    wmsum_value(&arc_sums.arcstat_l2_evict_lock_retry);
7226 	as->arcstat_l2_evict_reading.value.ui64 =
7227 	    wmsum_value(&arc_sums.arcstat_l2_evict_reading);
7228 	as->arcstat_l2_evict_l1cached.value.ui64 =
7229 	    wmsum_value(&arc_sums.arcstat_l2_evict_l1cached);
7230 	as->arcstat_l2_free_on_write.value.ui64 =
7231 	    wmsum_value(&arc_sums.arcstat_l2_free_on_write);
7232 	as->arcstat_l2_abort_lowmem.value.ui64 =
7233 	    wmsum_value(&arc_sums.arcstat_l2_abort_lowmem);
7234 	as->arcstat_l2_cksum_bad.value.ui64 =
7235 	    wmsum_value(&arc_sums.arcstat_l2_cksum_bad);
7236 	as->arcstat_l2_io_error.value.ui64 =
7237 	    wmsum_value(&arc_sums.arcstat_l2_io_error);
7238 	as->arcstat_l2_lsize.value.ui64 =
7239 	    wmsum_value(&arc_sums.arcstat_l2_lsize);
7240 	as->arcstat_l2_psize.value.ui64 =
7241 	    wmsum_value(&arc_sums.arcstat_l2_psize);
7242 	as->arcstat_l2_hdr_size.value.ui64 =
7243 	    aggsum_value(&arc_sums.arcstat_l2_hdr_size);
7244 	as->arcstat_l2_log_blk_writes.value.ui64 =
7245 	    wmsum_value(&arc_sums.arcstat_l2_log_blk_writes);
7246 	as->arcstat_l2_log_blk_asize.value.ui64 =
7247 	    wmsum_value(&arc_sums.arcstat_l2_log_blk_asize);
7248 	as->arcstat_l2_log_blk_count.value.ui64 =
7249 	    wmsum_value(&arc_sums.arcstat_l2_log_blk_count);
7250 	as->arcstat_l2_rebuild_success.value.ui64 =
7251 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_success);
7252 	as->arcstat_l2_rebuild_abort_unsupported.value.ui64 =
7253 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_unsupported);
7254 	as->arcstat_l2_rebuild_abort_io_errors.value.ui64 =
7255 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_io_errors);
7256 	as->arcstat_l2_rebuild_abort_dh_errors.value.ui64 =
7257 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_dh_errors);
7258 	as->arcstat_l2_rebuild_abort_cksum_lb_errors.value.ui64 =
7259 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors);
7260 	as->arcstat_l2_rebuild_abort_lowmem.value.ui64 =
7261 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_abort_lowmem);
7262 	as->arcstat_l2_rebuild_size.value.ui64 =
7263 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_size);
7264 	as->arcstat_l2_rebuild_asize.value.ui64 =
7265 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_asize);
7266 	as->arcstat_l2_rebuild_bufs.value.ui64 =
7267 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs);
7268 	as->arcstat_l2_rebuild_bufs_precached.value.ui64 =
7269 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_bufs_precached);
7270 	as->arcstat_l2_rebuild_log_blks.value.ui64 =
7271 	    wmsum_value(&arc_sums.arcstat_l2_rebuild_log_blks);
7272 	as->arcstat_memory_throttle_count.value.ui64 =
7273 	    wmsum_value(&arc_sums.arcstat_memory_throttle_count);
7274 	as->arcstat_memory_direct_count.value.ui64 =
7275 	    wmsum_value(&arc_sums.arcstat_memory_direct_count);
7276 	as->arcstat_memory_indirect_count.value.ui64 =
7277 	    wmsum_value(&arc_sums.arcstat_memory_indirect_count);
7278 
7279 	as->arcstat_memory_all_bytes.value.ui64 =
7280 	    arc_all_memory();
7281 	as->arcstat_memory_free_bytes.value.ui64 =
7282 	    arc_free_memory();
7283 	as->arcstat_memory_available_bytes.value.i64 =
7284 	    arc_available_memory();
7285 
7286 	as->arcstat_prune.value.ui64 =
7287 	    wmsum_value(&arc_sums.arcstat_prune);
7288 	as->arcstat_meta_used.value.ui64 =
7289 	    wmsum_value(&arc_sums.arcstat_meta_used);
7290 	as->arcstat_async_upgrade_sync.value.ui64 =
7291 	    wmsum_value(&arc_sums.arcstat_async_upgrade_sync);
7292 	as->arcstat_predictive_prefetch.value.ui64 =
7293 	    wmsum_value(&arc_sums.arcstat_predictive_prefetch);
7294 	as->arcstat_demand_hit_predictive_prefetch.value.ui64 =
7295 	    wmsum_value(&arc_sums.arcstat_demand_hit_predictive_prefetch);
7296 	as->arcstat_demand_iohit_predictive_prefetch.value.ui64 =
7297 	    wmsum_value(&arc_sums.arcstat_demand_iohit_predictive_prefetch);
7298 	as->arcstat_prescient_prefetch.value.ui64 =
7299 	    wmsum_value(&arc_sums.arcstat_prescient_prefetch);
7300 	as->arcstat_demand_hit_prescient_prefetch.value.ui64 =
7301 	    wmsum_value(&arc_sums.arcstat_demand_hit_prescient_prefetch);
7302 	as->arcstat_demand_iohit_prescient_prefetch.value.ui64 =
7303 	    wmsum_value(&arc_sums.arcstat_demand_iohit_prescient_prefetch);
7304 	as->arcstat_raw_size.value.ui64 =
7305 	    wmsum_value(&arc_sums.arcstat_raw_size);
7306 	as->arcstat_cached_only_in_progress.value.ui64 =
7307 	    wmsum_value(&arc_sums.arcstat_cached_only_in_progress);
7308 	as->arcstat_abd_chunk_waste_size.value.ui64 =
7309 	    wmsum_value(&arc_sums.arcstat_abd_chunk_waste_size);
7310 
7311 	return (0);
7312 }
7313 
7314 /*
7315  * This function *must* return indices evenly distributed between all
7316  * sublists of the multilist. This is needed due to how the ARC eviction
7317  * code is laid out; arc_evict_state() assumes ARC buffers are evenly
7318  * distributed between all sublists and uses this assumption when
7319  * deciding which sublist to evict from and how much to evict from it.
7320  */
7321 static unsigned int
arc_state_multilist_index_func(multilist_t * ml,void * obj)7322 arc_state_multilist_index_func(multilist_t *ml, void *obj)
7323 {
7324 	arc_buf_hdr_t *hdr = obj;
7325 
7326 	/*
7327 	 * We rely on b_dva to generate evenly distributed index
7328 	 * numbers using buf_hash below. So, as an added precaution,
7329 	 * let's make sure we never add empty buffers to the arc lists.
7330 	 */
7331 	ASSERT(!HDR_EMPTY(hdr));
7332 
7333 	/*
7334 	 * The assumption here, is the hash value for a given
7335 	 * arc_buf_hdr_t will remain constant throughout its lifetime
7336 	 * (i.e. its b_spa, b_dva, and b_birth fields don't change).
7337 	 * Thus, we don't need to store the header's sublist index
7338 	 * on insertion, as this index can be recalculated on removal.
7339 	 *
7340 	 * Also, the low order bits of the hash value are thought to be
7341 	 * distributed evenly. Otherwise, in the case that the multilist
7342 	 * has a power of two number of sublists, each sublists' usage
7343 	 * would not be evenly distributed. In this context full 64bit
7344 	 * division would be a waste of time, so limit it to 32 bits.
7345 	 */
7346 	return ((unsigned int)buf_hash(hdr->b_spa, &hdr->b_dva, hdr->b_birth) %
7347 	    multilist_get_num_sublists(ml));
7348 }
7349 
7350 static unsigned int
arc_state_l2c_multilist_index_func(multilist_t * ml,void * obj)7351 arc_state_l2c_multilist_index_func(multilist_t *ml, void *obj)
7352 {
7353 	panic("Header %p insert into arc_l2c_only %p", obj, ml);
7354 }
7355 
7356 #define	WARN_IF_TUNING_IGNORED(tuning, value, do_warn) do {	\
7357 	if ((do_warn) && (tuning) && ((tuning) != (value))) {	\
7358 		cmn_err(CE_WARN,				\
7359 		    "ignoring tunable %s (using %llu instead)",	\
7360 		    (#tuning), (u_longlong_t)(value));	\
7361 	}							\
7362 } while (0)
7363 
7364 /*
7365  * Called during module initialization and periodically thereafter to
7366  * apply reasonable changes to the exposed performance tunings.  Can also be
7367  * called explicitly by param_set_arc_*() functions when ARC tunables are
7368  * updated manually.  Non-zero zfs_* values which differ from the currently set
7369  * values will be applied.
7370  */
7371 void
arc_tuning_update(boolean_t verbose)7372 arc_tuning_update(boolean_t verbose)
7373 {
7374 	uint64_t allmem = arc_all_memory();
7375 
7376 	/* Valid range: 32M - <arc_c_max> */
7377 	if ((zfs_arc_min) && (zfs_arc_min != arc_c_min) &&
7378 	    (zfs_arc_min >= 2ULL << SPA_MAXBLOCKSHIFT) &&
7379 	    (zfs_arc_min <= arc_c_max)) {
7380 		arc_c_min = zfs_arc_min;
7381 		arc_c = MAX(arc_c, arc_c_min);
7382 	}
7383 	WARN_IF_TUNING_IGNORED(zfs_arc_min, arc_c_min, verbose);
7384 
7385 	/* Valid range: 64M - <all physical memory> */
7386 	if ((zfs_arc_max) && (zfs_arc_max != arc_c_max) &&
7387 	    (zfs_arc_max >= MIN_ARC_MAX) && (zfs_arc_max < allmem) &&
7388 	    (zfs_arc_max > arc_c_min)) {
7389 		arc_c_max = zfs_arc_max;
7390 		arc_c = MIN(arc_c, arc_c_max);
7391 		if (arc_dnode_limit > arc_c_max)
7392 			arc_dnode_limit = arc_c_max;
7393 	}
7394 	WARN_IF_TUNING_IGNORED(zfs_arc_max, arc_c_max, verbose);
7395 
7396 	/* Valid range: 0 - <all physical memory> */
7397 	arc_dnode_limit = zfs_arc_dnode_limit ? zfs_arc_dnode_limit :
7398 	    MIN(zfs_arc_dnode_limit_percent, 100) * arc_c_max / 100;
7399 	WARN_IF_TUNING_IGNORED(zfs_arc_dnode_limit, arc_dnode_limit, verbose);
7400 
7401 	/* Valid range: 1 - N */
7402 	if (zfs_arc_grow_retry)
7403 		arc_grow_retry = zfs_arc_grow_retry;
7404 
7405 	/* Valid range: 1 - N */
7406 	if (zfs_arc_shrink_shift) {
7407 		arc_shrink_shift = zfs_arc_shrink_shift;
7408 		arc_no_grow_shift = MIN(arc_no_grow_shift, arc_shrink_shift -1);
7409 	}
7410 
7411 	/* Valid range: 1 - N ms */
7412 	if (zfs_arc_min_prefetch_ms)
7413 		arc_min_prefetch_ms = zfs_arc_min_prefetch_ms;
7414 
7415 	/* Valid range: 1 - N ms */
7416 	if (zfs_arc_min_prescient_prefetch_ms) {
7417 		arc_min_prescient_prefetch_ms =
7418 		    zfs_arc_min_prescient_prefetch_ms;
7419 	}
7420 
7421 	/* Valid range: 0 - 100 */
7422 	if (zfs_arc_lotsfree_percent <= 100)
7423 		arc_lotsfree_percent = zfs_arc_lotsfree_percent;
7424 	WARN_IF_TUNING_IGNORED(zfs_arc_lotsfree_percent, arc_lotsfree_percent,
7425 	    verbose);
7426 
7427 	/* Valid range: 0 - <all physical memory> */
7428 	if ((zfs_arc_sys_free) && (zfs_arc_sys_free != arc_sys_free))
7429 		arc_sys_free = MIN(zfs_arc_sys_free, allmem);
7430 	WARN_IF_TUNING_IGNORED(zfs_arc_sys_free, arc_sys_free, verbose);
7431 }
7432 
7433 static void
arc_state_multilist_init(multilist_t * ml,multilist_sublist_index_func_t * index_func,int * maxcountp)7434 arc_state_multilist_init(multilist_t *ml,
7435     multilist_sublist_index_func_t *index_func, int *maxcountp)
7436 {
7437 	multilist_create(ml, sizeof (arc_buf_hdr_t),
7438 	    offsetof(arc_buf_hdr_t, b_l1hdr.b_arc_node), index_func);
7439 	*maxcountp = MAX(*maxcountp, multilist_get_num_sublists(ml));
7440 }
7441 
7442 static void
arc_state_init(void)7443 arc_state_init(void)
7444 {
7445 	int num_sublists = 0;
7446 
7447 	arc_state_multilist_init(&arc_mru->arcs_list[ARC_BUFC_METADATA],
7448 	    arc_state_multilist_index_func, &num_sublists);
7449 	arc_state_multilist_init(&arc_mru->arcs_list[ARC_BUFC_DATA],
7450 	    arc_state_multilist_index_func, &num_sublists);
7451 	arc_state_multilist_init(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
7452 	    arc_state_multilist_index_func, &num_sublists);
7453 	arc_state_multilist_init(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
7454 	    arc_state_multilist_index_func, &num_sublists);
7455 	arc_state_multilist_init(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
7456 	    arc_state_multilist_index_func, &num_sublists);
7457 	arc_state_multilist_init(&arc_mfu->arcs_list[ARC_BUFC_DATA],
7458 	    arc_state_multilist_index_func, &num_sublists);
7459 	arc_state_multilist_init(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
7460 	    arc_state_multilist_index_func, &num_sublists);
7461 	arc_state_multilist_init(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
7462 	    arc_state_multilist_index_func, &num_sublists);
7463 	arc_state_multilist_init(&arc_uncached->arcs_list[ARC_BUFC_METADATA],
7464 	    arc_state_multilist_index_func, &num_sublists);
7465 	arc_state_multilist_init(&arc_uncached->arcs_list[ARC_BUFC_DATA],
7466 	    arc_state_multilist_index_func, &num_sublists);
7467 
7468 	/*
7469 	 * L2 headers should never be on the L2 state list since they don't
7470 	 * have L1 headers allocated.  Special index function asserts that.
7471 	 */
7472 	arc_state_multilist_init(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
7473 	    arc_state_l2c_multilist_index_func, &num_sublists);
7474 	arc_state_multilist_init(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
7475 	    arc_state_l2c_multilist_index_func, &num_sublists);
7476 
7477 	/*
7478 	 * Keep track of the number of markers needed to reclaim buffers from
7479 	 * any ARC state.  The markers will be pre-allocated so as to minimize
7480 	 * the number of memory allocations performed by the eviction thread.
7481 	 */
7482 	arc_state_evict_marker_count = num_sublists;
7483 
7484 	zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_METADATA]);
7485 	zfs_refcount_create(&arc_anon->arcs_esize[ARC_BUFC_DATA]);
7486 	zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_METADATA]);
7487 	zfs_refcount_create(&arc_mru->arcs_esize[ARC_BUFC_DATA]);
7488 	zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]);
7489 	zfs_refcount_create(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]);
7490 	zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]);
7491 	zfs_refcount_create(&arc_mfu->arcs_esize[ARC_BUFC_DATA]);
7492 	zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]);
7493 	zfs_refcount_create(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]);
7494 	zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]);
7495 	zfs_refcount_create(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]);
7496 	zfs_refcount_create(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]);
7497 	zfs_refcount_create(&arc_uncached->arcs_esize[ARC_BUFC_DATA]);
7498 
7499 	zfs_refcount_create(&arc_anon->arcs_size[ARC_BUFC_DATA]);
7500 	zfs_refcount_create(&arc_anon->arcs_size[ARC_BUFC_METADATA]);
7501 	zfs_refcount_create(&arc_mru->arcs_size[ARC_BUFC_DATA]);
7502 	zfs_refcount_create(&arc_mru->arcs_size[ARC_BUFC_METADATA]);
7503 	zfs_refcount_create(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]);
7504 	zfs_refcount_create(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]);
7505 	zfs_refcount_create(&arc_mfu->arcs_size[ARC_BUFC_DATA]);
7506 	zfs_refcount_create(&arc_mfu->arcs_size[ARC_BUFC_METADATA]);
7507 	zfs_refcount_create(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]);
7508 	zfs_refcount_create(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]);
7509 	zfs_refcount_create(&arc_l2c_only->arcs_size[ARC_BUFC_DATA]);
7510 	zfs_refcount_create(&arc_l2c_only->arcs_size[ARC_BUFC_METADATA]);
7511 	zfs_refcount_create(&arc_uncached->arcs_size[ARC_BUFC_DATA]);
7512 	zfs_refcount_create(&arc_uncached->arcs_size[ARC_BUFC_METADATA]);
7513 
7514 	wmsum_init(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA], 0);
7515 	wmsum_init(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA], 0);
7516 	wmsum_init(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA], 0);
7517 	wmsum_init(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA], 0);
7518 
7519 	wmsum_init(&arc_sums.arcstat_hits, 0);
7520 	wmsum_init(&arc_sums.arcstat_iohits, 0);
7521 	wmsum_init(&arc_sums.arcstat_misses, 0);
7522 	wmsum_init(&arc_sums.arcstat_demand_data_hits, 0);
7523 	wmsum_init(&arc_sums.arcstat_demand_data_iohits, 0);
7524 	wmsum_init(&arc_sums.arcstat_demand_data_misses, 0);
7525 	wmsum_init(&arc_sums.arcstat_demand_metadata_hits, 0);
7526 	wmsum_init(&arc_sums.arcstat_demand_metadata_iohits, 0);
7527 	wmsum_init(&arc_sums.arcstat_demand_metadata_misses, 0);
7528 	wmsum_init(&arc_sums.arcstat_prefetch_data_hits, 0);
7529 	wmsum_init(&arc_sums.arcstat_prefetch_data_iohits, 0);
7530 	wmsum_init(&arc_sums.arcstat_prefetch_data_misses, 0);
7531 	wmsum_init(&arc_sums.arcstat_prefetch_metadata_hits, 0);
7532 	wmsum_init(&arc_sums.arcstat_prefetch_metadata_iohits, 0);
7533 	wmsum_init(&arc_sums.arcstat_prefetch_metadata_misses, 0);
7534 	wmsum_init(&arc_sums.arcstat_mru_hits, 0);
7535 	wmsum_init(&arc_sums.arcstat_mru_ghost_hits, 0);
7536 	wmsum_init(&arc_sums.arcstat_mfu_hits, 0);
7537 	wmsum_init(&arc_sums.arcstat_mfu_ghost_hits, 0);
7538 	wmsum_init(&arc_sums.arcstat_uncached_hits, 0);
7539 	wmsum_init(&arc_sums.arcstat_deleted, 0);
7540 	wmsum_init(&arc_sums.arcstat_mutex_miss, 0);
7541 	wmsum_init(&arc_sums.arcstat_access_skip, 0);
7542 	wmsum_init(&arc_sums.arcstat_evict_skip, 0);
7543 	wmsum_init(&arc_sums.arcstat_evict_not_enough, 0);
7544 	wmsum_init(&arc_sums.arcstat_evict_l2_cached, 0);
7545 	wmsum_init(&arc_sums.arcstat_evict_l2_eligible, 0);
7546 	wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mfu, 0);
7547 	wmsum_init(&arc_sums.arcstat_evict_l2_eligible_mru, 0);
7548 	wmsum_init(&arc_sums.arcstat_evict_l2_ineligible, 0);
7549 	wmsum_init(&arc_sums.arcstat_evict_l2_skip, 0);
7550 	wmsum_init(&arc_sums.arcstat_hash_elements, 0);
7551 	wmsum_init(&arc_sums.arcstat_hash_collisions, 0);
7552 	wmsum_init(&arc_sums.arcstat_hash_chains, 0);
7553 	aggsum_init(&arc_sums.arcstat_size, 0);
7554 	wmsum_init(&arc_sums.arcstat_compressed_size, 0);
7555 	wmsum_init(&arc_sums.arcstat_uncompressed_size, 0);
7556 	wmsum_init(&arc_sums.arcstat_overhead_size, 0);
7557 	wmsum_init(&arc_sums.arcstat_hdr_size, 0);
7558 	wmsum_init(&arc_sums.arcstat_data_size, 0);
7559 	wmsum_init(&arc_sums.arcstat_metadata_size, 0);
7560 	wmsum_init(&arc_sums.arcstat_dbuf_size, 0);
7561 	wmsum_init(&arc_sums.arcstat_dnode_size, 0);
7562 	wmsum_init(&arc_sums.arcstat_bonus_size, 0);
7563 	wmsum_init(&arc_sums.arcstat_l2_hits, 0);
7564 	wmsum_init(&arc_sums.arcstat_l2_misses, 0);
7565 	wmsum_init(&arc_sums.arcstat_l2_prefetch_asize, 0);
7566 	wmsum_init(&arc_sums.arcstat_l2_mru_asize, 0);
7567 	wmsum_init(&arc_sums.arcstat_l2_mfu_asize, 0);
7568 	wmsum_init(&arc_sums.arcstat_l2_bufc_data_asize, 0);
7569 	wmsum_init(&arc_sums.arcstat_l2_bufc_metadata_asize, 0);
7570 	wmsum_init(&arc_sums.arcstat_l2_feeds, 0);
7571 	wmsum_init(&arc_sums.arcstat_l2_rw_clash, 0);
7572 	wmsum_init(&arc_sums.arcstat_l2_read_bytes, 0);
7573 	wmsum_init(&arc_sums.arcstat_l2_write_bytes, 0);
7574 	wmsum_init(&arc_sums.arcstat_l2_writes_sent, 0);
7575 	wmsum_init(&arc_sums.arcstat_l2_writes_done, 0);
7576 	wmsum_init(&arc_sums.arcstat_l2_writes_error, 0);
7577 	wmsum_init(&arc_sums.arcstat_l2_writes_lock_retry, 0);
7578 	wmsum_init(&arc_sums.arcstat_l2_evict_lock_retry, 0);
7579 	wmsum_init(&arc_sums.arcstat_l2_evict_reading, 0);
7580 	wmsum_init(&arc_sums.arcstat_l2_evict_l1cached, 0);
7581 	wmsum_init(&arc_sums.arcstat_l2_free_on_write, 0);
7582 	wmsum_init(&arc_sums.arcstat_l2_abort_lowmem, 0);
7583 	wmsum_init(&arc_sums.arcstat_l2_cksum_bad, 0);
7584 	wmsum_init(&arc_sums.arcstat_l2_io_error, 0);
7585 	wmsum_init(&arc_sums.arcstat_l2_lsize, 0);
7586 	wmsum_init(&arc_sums.arcstat_l2_psize, 0);
7587 	aggsum_init(&arc_sums.arcstat_l2_hdr_size, 0);
7588 	wmsum_init(&arc_sums.arcstat_l2_log_blk_writes, 0);
7589 	wmsum_init(&arc_sums.arcstat_l2_log_blk_asize, 0);
7590 	wmsum_init(&arc_sums.arcstat_l2_log_blk_count, 0);
7591 	wmsum_init(&arc_sums.arcstat_l2_rebuild_success, 0);
7592 	wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_unsupported, 0);
7593 	wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_io_errors, 0);
7594 	wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_dh_errors, 0);
7595 	wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors, 0);
7596 	wmsum_init(&arc_sums.arcstat_l2_rebuild_abort_lowmem, 0);
7597 	wmsum_init(&arc_sums.arcstat_l2_rebuild_size, 0);
7598 	wmsum_init(&arc_sums.arcstat_l2_rebuild_asize, 0);
7599 	wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs, 0);
7600 	wmsum_init(&arc_sums.arcstat_l2_rebuild_bufs_precached, 0);
7601 	wmsum_init(&arc_sums.arcstat_l2_rebuild_log_blks, 0);
7602 	wmsum_init(&arc_sums.arcstat_memory_throttle_count, 0);
7603 	wmsum_init(&arc_sums.arcstat_memory_direct_count, 0);
7604 	wmsum_init(&arc_sums.arcstat_memory_indirect_count, 0);
7605 	wmsum_init(&arc_sums.arcstat_prune, 0);
7606 	wmsum_init(&arc_sums.arcstat_meta_used, 0);
7607 	wmsum_init(&arc_sums.arcstat_async_upgrade_sync, 0);
7608 	wmsum_init(&arc_sums.arcstat_predictive_prefetch, 0);
7609 	wmsum_init(&arc_sums.arcstat_demand_hit_predictive_prefetch, 0);
7610 	wmsum_init(&arc_sums.arcstat_demand_iohit_predictive_prefetch, 0);
7611 	wmsum_init(&arc_sums.arcstat_prescient_prefetch, 0);
7612 	wmsum_init(&arc_sums.arcstat_demand_hit_prescient_prefetch, 0);
7613 	wmsum_init(&arc_sums.arcstat_demand_iohit_prescient_prefetch, 0);
7614 	wmsum_init(&arc_sums.arcstat_raw_size, 0);
7615 	wmsum_init(&arc_sums.arcstat_cached_only_in_progress, 0);
7616 	wmsum_init(&arc_sums.arcstat_abd_chunk_waste_size, 0);
7617 
7618 	arc_anon->arcs_state = ARC_STATE_ANON;
7619 	arc_mru->arcs_state = ARC_STATE_MRU;
7620 	arc_mru_ghost->arcs_state = ARC_STATE_MRU_GHOST;
7621 	arc_mfu->arcs_state = ARC_STATE_MFU;
7622 	arc_mfu_ghost->arcs_state = ARC_STATE_MFU_GHOST;
7623 	arc_l2c_only->arcs_state = ARC_STATE_L2C_ONLY;
7624 	arc_uncached->arcs_state = ARC_STATE_UNCACHED;
7625 }
7626 
7627 static void
arc_state_fini(void)7628 arc_state_fini(void)
7629 {
7630 	zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_METADATA]);
7631 	zfs_refcount_destroy(&arc_anon->arcs_esize[ARC_BUFC_DATA]);
7632 	zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_METADATA]);
7633 	zfs_refcount_destroy(&arc_mru->arcs_esize[ARC_BUFC_DATA]);
7634 	zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_METADATA]);
7635 	zfs_refcount_destroy(&arc_mru_ghost->arcs_esize[ARC_BUFC_DATA]);
7636 	zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]);
7637 	zfs_refcount_destroy(&arc_mfu->arcs_esize[ARC_BUFC_DATA]);
7638 	zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_METADATA]);
7639 	zfs_refcount_destroy(&arc_mfu_ghost->arcs_esize[ARC_BUFC_DATA]);
7640 	zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_METADATA]);
7641 	zfs_refcount_destroy(&arc_l2c_only->arcs_esize[ARC_BUFC_DATA]);
7642 	zfs_refcount_destroy(&arc_uncached->arcs_esize[ARC_BUFC_METADATA]);
7643 	zfs_refcount_destroy(&arc_uncached->arcs_esize[ARC_BUFC_DATA]);
7644 
7645 	zfs_refcount_destroy(&arc_anon->arcs_size[ARC_BUFC_DATA]);
7646 	zfs_refcount_destroy(&arc_anon->arcs_size[ARC_BUFC_METADATA]);
7647 	zfs_refcount_destroy(&arc_mru->arcs_size[ARC_BUFC_DATA]);
7648 	zfs_refcount_destroy(&arc_mru->arcs_size[ARC_BUFC_METADATA]);
7649 	zfs_refcount_destroy(&arc_mru_ghost->arcs_size[ARC_BUFC_DATA]);
7650 	zfs_refcount_destroy(&arc_mru_ghost->arcs_size[ARC_BUFC_METADATA]);
7651 	zfs_refcount_destroy(&arc_mfu->arcs_size[ARC_BUFC_DATA]);
7652 	zfs_refcount_destroy(&arc_mfu->arcs_size[ARC_BUFC_METADATA]);
7653 	zfs_refcount_destroy(&arc_mfu_ghost->arcs_size[ARC_BUFC_DATA]);
7654 	zfs_refcount_destroy(&arc_mfu_ghost->arcs_size[ARC_BUFC_METADATA]);
7655 	zfs_refcount_destroy(&arc_l2c_only->arcs_size[ARC_BUFC_DATA]);
7656 	zfs_refcount_destroy(&arc_l2c_only->arcs_size[ARC_BUFC_METADATA]);
7657 	zfs_refcount_destroy(&arc_uncached->arcs_size[ARC_BUFC_DATA]);
7658 	zfs_refcount_destroy(&arc_uncached->arcs_size[ARC_BUFC_METADATA]);
7659 
7660 	multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
7661 	multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
7662 	multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
7663 	multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
7664 	multilist_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
7665 	multilist_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
7666 	multilist_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
7667 	multilist_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
7668 	multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA]);
7669 	multilist_destroy(&arc_l2c_only->arcs_list[ARC_BUFC_DATA]);
7670 	multilist_destroy(&arc_uncached->arcs_list[ARC_BUFC_METADATA]);
7671 	multilist_destroy(&arc_uncached->arcs_list[ARC_BUFC_DATA]);
7672 
7673 	wmsum_fini(&arc_mru_ghost->arcs_hits[ARC_BUFC_DATA]);
7674 	wmsum_fini(&arc_mru_ghost->arcs_hits[ARC_BUFC_METADATA]);
7675 	wmsum_fini(&arc_mfu_ghost->arcs_hits[ARC_BUFC_DATA]);
7676 	wmsum_fini(&arc_mfu_ghost->arcs_hits[ARC_BUFC_METADATA]);
7677 
7678 	wmsum_fini(&arc_sums.arcstat_hits);
7679 	wmsum_fini(&arc_sums.arcstat_iohits);
7680 	wmsum_fini(&arc_sums.arcstat_misses);
7681 	wmsum_fini(&arc_sums.arcstat_demand_data_hits);
7682 	wmsum_fini(&arc_sums.arcstat_demand_data_iohits);
7683 	wmsum_fini(&arc_sums.arcstat_demand_data_misses);
7684 	wmsum_fini(&arc_sums.arcstat_demand_metadata_hits);
7685 	wmsum_fini(&arc_sums.arcstat_demand_metadata_iohits);
7686 	wmsum_fini(&arc_sums.arcstat_demand_metadata_misses);
7687 	wmsum_fini(&arc_sums.arcstat_prefetch_data_hits);
7688 	wmsum_fini(&arc_sums.arcstat_prefetch_data_iohits);
7689 	wmsum_fini(&arc_sums.arcstat_prefetch_data_misses);
7690 	wmsum_fini(&arc_sums.arcstat_prefetch_metadata_hits);
7691 	wmsum_fini(&arc_sums.arcstat_prefetch_metadata_iohits);
7692 	wmsum_fini(&arc_sums.arcstat_prefetch_metadata_misses);
7693 	wmsum_fini(&arc_sums.arcstat_mru_hits);
7694 	wmsum_fini(&arc_sums.arcstat_mru_ghost_hits);
7695 	wmsum_fini(&arc_sums.arcstat_mfu_hits);
7696 	wmsum_fini(&arc_sums.arcstat_mfu_ghost_hits);
7697 	wmsum_fini(&arc_sums.arcstat_uncached_hits);
7698 	wmsum_fini(&arc_sums.arcstat_deleted);
7699 	wmsum_fini(&arc_sums.arcstat_mutex_miss);
7700 	wmsum_fini(&arc_sums.arcstat_access_skip);
7701 	wmsum_fini(&arc_sums.arcstat_evict_skip);
7702 	wmsum_fini(&arc_sums.arcstat_evict_not_enough);
7703 	wmsum_fini(&arc_sums.arcstat_evict_l2_cached);
7704 	wmsum_fini(&arc_sums.arcstat_evict_l2_eligible);
7705 	wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mfu);
7706 	wmsum_fini(&arc_sums.arcstat_evict_l2_eligible_mru);
7707 	wmsum_fini(&arc_sums.arcstat_evict_l2_ineligible);
7708 	wmsum_fini(&arc_sums.arcstat_evict_l2_skip);
7709 	wmsum_fini(&arc_sums.arcstat_hash_elements);
7710 	wmsum_fini(&arc_sums.arcstat_hash_collisions);
7711 	wmsum_fini(&arc_sums.arcstat_hash_chains);
7712 	aggsum_fini(&arc_sums.arcstat_size);
7713 	wmsum_fini(&arc_sums.arcstat_compressed_size);
7714 	wmsum_fini(&arc_sums.arcstat_uncompressed_size);
7715 	wmsum_fini(&arc_sums.arcstat_overhead_size);
7716 	wmsum_fini(&arc_sums.arcstat_hdr_size);
7717 	wmsum_fini(&arc_sums.arcstat_data_size);
7718 	wmsum_fini(&arc_sums.arcstat_metadata_size);
7719 	wmsum_fini(&arc_sums.arcstat_dbuf_size);
7720 	wmsum_fini(&arc_sums.arcstat_dnode_size);
7721 	wmsum_fini(&arc_sums.arcstat_bonus_size);
7722 	wmsum_fini(&arc_sums.arcstat_l2_hits);
7723 	wmsum_fini(&arc_sums.arcstat_l2_misses);
7724 	wmsum_fini(&arc_sums.arcstat_l2_prefetch_asize);
7725 	wmsum_fini(&arc_sums.arcstat_l2_mru_asize);
7726 	wmsum_fini(&arc_sums.arcstat_l2_mfu_asize);
7727 	wmsum_fini(&arc_sums.arcstat_l2_bufc_data_asize);
7728 	wmsum_fini(&arc_sums.arcstat_l2_bufc_metadata_asize);
7729 	wmsum_fini(&arc_sums.arcstat_l2_feeds);
7730 	wmsum_fini(&arc_sums.arcstat_l2_rw_clash);
7731 	wmsum_fini(&arc_sums.arcstat_l2_read_bytes);
7732 	wmsum_fini(&arc_sums.arcstat_l2_write_bytes);
7733 	wmsum_fini(&arc_sums.arcstat_l2_writes_sent);
7734 	wmsum_fini(&arc_sums.arcstat_l2_writes_done);
7735 	wmsum_fini(&arc_sums.arcstat_l2_writes_error);
7736 	wmsum_fini(&arc_sums.arcstat_l2_writes_lock_retry);
7737 	wmsum_fini(&arc_sums.arcstat_l2_evict_lock_retry);
7738 	wmsum_fini(&arc_sums.arcstat_l2_evict_reading);
7739 	wmsum_fini(&arc_sums.arcstat_l2_evict_l1cached);
7740 	wmsum_fini(&arc_sums.arcstat_l2_free_on_write);
7741 	wmsum_fini(&arc_sums.arcstat_l2_abort_lowmem);
7742 	wmsum_fini(&arc_sums.arcstat_l2_cksum_bad);
7743 	wmsum_fini(&arc_sums.arcstat_l2_io_error);
7744 	wmsum_fini(&arc_sums.arcstat_l2_lsize);
7745 	wmsum_fini(&arc_sums.arcstat_l2_psize);
7746 	aggsum_fini(&arc_sums.arcstat_l2_hdr_size);
7747 	wmsum_fini(&arc_sums.arcstat_l2_log_blk_writes);
7748 	wmsum_fini(&arc_sums.arcstat_l2_log_blk_asize);
7749 	wmsum_fini(&arc_sums.arcstat_l2_log_blk_count);
7750 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_success);
7751 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_unsupported);
7752 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_io_errors);
7753 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_dh_errors);
7754 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_cksum_lb_errors);
7755 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_abort_lowmem);
7756 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_size);
7757 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_asize);
7758 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs);
7759 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_bufs_precached);
7760 	wmsum_fini(&arc_sums.arcstat_l2_rebuild_log_blks);
7761 	wmsum_fini(&arc_sums.arcstat_memory_throttle_count);
7762 	wmsum_fini(&arc_sums.arcstat_memory_direct_count);
7763 	wmsum_fini(&arc_sums.arcstat_memory_indirect_count);
7764 	wmsum_fini(&arc_sums.arcstat_prune);
7765 	wmsum_fini(&arc_sums.arcstat_meta_used);
7766 	wmsum_fini(&arc_sums.arcstat_async_upgrade_sync);
7767 	wmsum_fini(&arc_sums.arcstat_predictive_prefetch);
7768 	wmsum_fini(&arc_sums.arcstat_demand_hit_predictive_prefetch);
7769 	wmsum_fini(&arc_sums.arcstat_demand_iohit_predictive_prefetch);
7770 	wmsum_fini(&arc_sums.arcstat_prescient_prefetch);
7771 	wmsum_fini(&arc_sums.arcstat_demand_hit_prescient_prefetch);
7772 	wmsum_fini(&arc_sums.arcstat_demand_iohit_prescient_prefetch);
7773 	wmsum_fini(&arc_sums.arcstat_raw_size);
7774 	wmsum_fini(&arc_sums.arcstat_cached_only_in_progress);
7775 	wmsum_fini(&arc_sums.arcstat_abd_chunk_waste_size);
7776 }
7777 
7778 uint64_t
arc_target_bytes(void)7779 arc_target_bytes(void)
7780 {
7781 	return (arc_c);
7782 }
7783 
7784 void
arc_set_limits(uint64_t allmem)7785 arc_set_limits(uint64_t allmem)
7786 {
7787 	/* Set min cache to 1/32 of all memory, or 32MB, whichever is more. */
7788 	arc_c_min = MAX(allmem / 32, 2ULL << SPA_MAXBLOCKSHIFT);
7789 
7790 	/* How to set default max varies by platform. */
7791 	arc_c_max = arc_default_max(arc_c_min, allmem);
7792 }
7793 void
arc_init(void)7794 arc_init(void)
7795 {
7796 	uint64_t percent, allmem = arc_all_memory();
7797 	mutex_init(&arc_evict_lock, NULL, MUTEX_DEFAULT, NULL);
7798 	list_create(&arc_evict_waiters, sizeof (arc_evict_waiter_t),
7799 	    offsetof(arc_evict_waiter_t, aew_node));
7800 
7801 	arc_min_prefetch_ms = 1000;
7802 	arc_min_prescient_prefetch_ms = 6000;
7803 
7804 #if defined(_KERNEL)
7805 	arc_lowmem_init();
7806 #endif
7807 
7808 	arc_set_limits(allmem);
7809 
7810 #ifdef _KERNEL
7811 	/*
7812 	 * If zfs_arc_max is non-zero at init, meaning it was set in the kernel
7813 	 * environment before the module was loaded, don't block setting the
7814 	 * maximum because it is less than arc_c_min, instead, reset arc_c_min
7815 	 * to a lower value.
7816 	 * zfs_arc_min will be handled by arc_tuning_update().
7817 	 */
7818 	if (zfs_arc_max != 0 && zfs_arc_max >= MIN_ARC_MAX &&
7819 	    zfs_arc_max < allmem) {
7820 		arc_c_max = zfs_arc_max;
7821 		if (arc_c_min >= arc_c_max) {
7822 			arc_c_min = MAX(zfs_arc_max / 2,
7823 			    2ULL << SPA_MAXBLOCKSHIFT);
7824 		}
7825 	}
7826 #else
7827 	/*
7828 	 * In userland, there's only the memory pressure that we artificially
7829 	 * create (see arc_available_memory()).  Don't let arc_c get too
7830 	 * small, because it can cause transactions to be larger than
7831 	 * arc_c, causing arc_tempreserve_space() to fail.
7832 	 */
7833 	arc_c_min = MAX(arc_c_max / 2, 2ULL << SPA_MAXBLOCKSHIFT);
7834 #endif
7835 
7836 	arc_c = arc_c_min;
7837 	/*
7838 	 * 32-bit fixed point fractions of metadata from total ARC size,
7839 	 * MRU data from all data and MRU metadata from all metadata.
7840 	 */
7841 	arc_meta = (1ULL << 32) / 4;	/* Metadata is 25% of arc_c. */
7842 	arc_pd = (1ULL << 32) / 2;	/* Data MRU is 50% of data. */
7843 	arc_pm = (1ULL << 32) / 2;	/* Metadata MRU is 50% of metadata. */
7844 
7845 	percent = MIN(zfs_arc_dnode_limit_percent, 100);
7846 	arc_dnode_limit = arc_c_max * percent / 100;
7847 
7848 	/* Apply user specified tunings */
7849 	arc_tuning_update(B_TRUE);
7850 
7851 	/* if kmem_flags are set, lets try to use less memory */
7852 	if (kmem_debugging())
7853 		arc_c = arc_c / 2;
7854 	if (arc_c < arc_c_min)
7855 		arc_c = arc_c_min;
7856 
7857 	arc_register_hotplug();
7858 
7859 	arc_state_init();
7860 
7861 	buf_init();
7862 
7863 	list_create(&arc_prune_list, sizeof (arc_prune_t),
7864 	    offsetof(arc_prune_t, p_node));
7865 	mutex_init(&arc_prune_mtx, NULL, MUTEX_DEFAULT, NULL);
7866 
7867 	arc_prune_taskq = taskq_create("arc_prune", zfs_arc_prune_task_threads,
7868 	    defclsyspri, 100, INT_MAX, TASKQ_PREPOPULATE | TASKQ_DYNAMIC);
7869 
7870 	list_create(&arc_async_flush_list, sizeof (arc_async_flush_t),
7871 	    offsetof(arc_async_flush_t, af_node));
7872 	mutex_init(&arc_async_flush_lock, NULL, MUTEX_DEFAULT, NULL);
7873 	arc_flush_taskq = taskq_create("arc_flush", MIN(boot_ncpus, 4),
7874 	    defclsyspri, 1, INT_MAX, TASKQ_DYNAMIC);
7875 
7876 	arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
7877 	    sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
7878 
7879 	if (arc_ksp != NULL) {
7880 		arc_ksp->ks_data = &arc_stats;
7881 		arc_ksp->ks_update = arc_kstat_update;
7882 		kstat_install(arc_ksp);
7883 	}
7884 
7885 	arc_state_evict_markers =
7886 	    arc_state_alloc_markers(arc_state_evict_marker_count);
7887 	arc_evict_zthr = zthr_create_timer("arc_evict",
7888 	    arc_evict_cb_check, arc_evict_cb, NULL, SEC2NSEC(1), defclsyspri);
7889 	arc_reap_zthr = zthr_create_timer("arc_reap",
7890 	    arc_reap_cb_check, arc_reap_cb, NULL, SEC2NSEC(1), minclsyspri);
7891 
7892 	arc_warm = B_FALSE;
7893 
7894 	/*
7895 	 * Calculate maximum amount of dirty data per pool.
7896 	 *
7897 	 * If it has been set by a module parameter, take that.
7898 	 * Otherwise, use a percentage of physical memory defined by
7899 	 * zfs_dirty_data_max_percent (default 10%) with a cap at
7900 	 * zfs_dirty_data_max_max (default 4G or 25% of physical memory).
7901 	 */
7902 #ifdef __LP64__
7903 	if (zfs_dirty_data_max_max == 0)
7904 		zfs_dirty_data_max_max = MIN(4ULL * 1024 * 1024 * 1024,
7905 		    allmem * zfs_dirty_data_max_max_percent / 100);
7906 #else
7907 	if (zfs_dirty_data_max_max == 0)
7908 		zfs_dirty_data_max_max = MIN(1ULL * 1024 * 1024 * 1024,
7909 		    allmem * zfs_dirty_data_max_max_percent / 100);
7910 #endif
7911 
7912 	if (zfs_dirty_data_max == 0) {
7913 		zfs_dirty_data_max = allmem *
7914 		    zfs_dirty_data_max_percent / 100;
7915 		zfs_dirty_data_max = MIN(zfs_dirty_data_max,
7916 		    zfs_dirty_data_max_max);
7917 	}
7918 
7919 	if (zfs_wrlog_data_max == 0) {
7920 
7921 		/*
7922 		 * dp_wrlog_total is reduced for each txg at the end of
7923 		 * spa_sync(). However, dp_dirty_total is reduced every time
7924 		 * a block is written out. Thus under normal operation,
7925 		 * dp_wrlog_total could grow 2 times as big as
7926 		 * zfs_dirty_data_max.
7927 		 */
7928 		zfs_wrlog_data_max = zfs_dirty_data_max * 2;
7929 	}
7930 }
7931 
7932 void
arc_fini(void)7933 arc_fini(void)
7934 {
7935 	arc_prune_t *p;
7936 
7937 #ifdef _KERNEL
7938 	arc_lowmem_fini();
7939 #endif /* _KERNEL */
7940 
7941 	/* Wait for any background flushes */
7942 	taskq_wait(arc_flush_taskq);
7943 	taskq_destroy(arc_flush_taskq);
7944 
7945 	/* Use B_TRUE to ensure *all* buffers are evicted */
7946 	arc_flush(NULL, B_TRUE);
7947 
7948 	if (arc_ksp != NULL) {
7949 		kstat_delete(arc_ksp);
7950 		arc_ksp = NULL;
7951 	}
7952 
7953 	taskq_wait(arc_prune_taskq);
7954 	taskq_destroy(arc_prune_taskq);
7955 
7956 	list_destroy(&arc_async_flush_list);
7957 	mutex_destroy(&arc_async_flush_lock);
7958 
7959 	mutex_enter(&arc_prune_mtx);
7960 	while ((p = list_remove_head(&arc_prune_list)) != NULL) {
7961 		(void) zfs_refcount_remove(&p->p_refcnt, &arc_prune_list);
7962 		zfs_refcount_destroy(&p->p_refcnt);
7963 		kmem_free(p, sizeof (*p));
7964 	}
7965 	mutex_exit(&arc_prune_mtx);
7966 
7967 	list_destroy(&arc_prune_list);
7968 	mutex_destroy(&arc_prune_mtx);
7969 
7970 	(void) zthr_cancel(arc_evict_zthr);
7971 	(void) zthr_cancel(arc_reap_zthr);
7972 	arc_state_free_markers(arc_state_evict_markers,
7973 	    arc_state_evict_marker_count);
7974 
7975 	mutex_destroy(&arc_evict_lock);
7976 	list_destroy(&arc_evict_waiters);
7977 
7978 	/*
7979 	 * Free any buffers that were tagged for destruction.  This needs
7980 	 * to occur before arc_state_fini() runs and destroys the aggsum
7981 	 * values which are updated when freeing scatter ABDs.
7982 	 */
7983 	l2arc_do_free_on_write();
7984 
7985 	/*
7986 	 * buf_fini() must proceed arc_state_fini() because buf_fin() may
7987 	 * trigger the release of kmem magazines, which can callback to
7988 	 * arc_space_return() which accesses aggsums freed in act_state_fini().
7989 	 */
7990 	buf_fini();
7991 	arc_state_fini();
7992 
7993 	arc_unregister_hotplug();
7994 
7995 	/*
7996 	 * We destroy the zthrs after all the ARC state has been
7997 	 * torn down to avoid the case of them receiving any
7998 	 * wakeup() signals after they are destroyed.
7999 	 */
8000 	zthr_destroy(arc_evict_zthr);
8001 	zthr_destroy(arc_reap_zthr);
8002 
8003 	ASSERT0(arc_loaned_bytes);
8004 }
8005 
8006 /*
8007  * Level 2 ARC
8008  *
8009  * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
8010  * It uses dedicated storage devices to hold cached data, which are populated
8011  * using large infrequent writes.  The main role of this cache is to boost
8012  * the performance of random read workloads.  The intended L2ARC devices
8013  * include short-stroked disks, solid state disks, and other media with
8014  * substantially faster read latency than disk.
8015  *
8016  *                 +-----------------------+
8017  *                 |         ARC           |
8018  *                 +-----------------------+
8019  *                    |         ^     ^
8020  *                    |         |     |
8021  *      l2arc_feed_thread()    arc_read()
8022  *                    |         |     |
8023  *                    |  l2arc read   |
8024  *                    V         |     |
8025  *               +---------------+    |
8026  *               |     L2ARC     |    |
8027  *               +---------------+    |
8028  *                   |    ^           |
8029  *          l2arc_write() |           |
8030  *                   |    |           |
8031  *                   V    |           |
8032  *                 +-------+      +-------+
8033  *                 | vdev  |      | vdev  |
8034  *                 | cache |      | cache |
8035  *                 +-------+      +-------+
8036  *                 +=========+     .-----.
8037  *                 :  L2ARC  :    |-_____-|
8038  *                 : devices :    | Disks |
8039  *                 +=========+    `-_____-'
8040  *
8041  * Read requests are satisfied from the following sources, in order:
8042  *
8043  *	1) ARC
8044  *	2) vdev cache of L2ARC devices
8045  *	3) L2ARC devices
8046  *	4) vdev cache of disks
8047  *	5) disks
8048  *
8049  * Some L2ARC device types exhibit extremely slow write performance.
8050  * To accommodate for this there are some significant differences between
8051  * the L2ARC and traditional cache design:
8052  *
8053  * 1. There is no eviction path from the ARC to the L2ARC.  Evictions from
8054  * the ARC behave as usual, freeing buffers and placing headers on ghost
8055  * lists.  The ARC does not send buffers to the L2ARC during eviction as
8056  * this would add inflated write latencies for all ARC memory pressure.
8057  *
8058  * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
8059  * It does this by periodically scanning buffers from the eviction-end of
8060  * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
8061  * not already there. It scans until a headroom of buffers is satisfied,
8062  * which itself is a buffer for ARC eviction. If a compressible buffer is
8063  * found during scanning and selected for writing to an L2ARC device, we
8064  * temporarily boost scanning headroom during the next scan cycle to make
8065  * sure we adapt to compression effects (which might significantly reduce
8066  * the data volume we write to L2ARC). The thread that does this is
8067  * l2arc_feed_thread(), illustrated below; example sizes are included to
8068  * provide a better sense of ratio than this diagram:
8069  *
8070  *	       head -->                        tail
8071  *	        +---------------------+----------+
8072  *	ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->.   # already on L2ARC
8073  *	        +---------------------+----------+   |   o L2ARC eligible
8074  *	ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->|   : ARC buffer
8075  *	        +---------------------+----------+   |
8076  *	             15.9 Gbytes      ^ 32 Mbytes    |
8077  *	                           headroom          |
8078  *	                                      l2arc_feed_thread()
8079  *	                                             |
8080  *	                 l2arc write hand <--[oooo]--'
8081  *	                         |           8 Mbyte
8082  *	                         |          write max
8083  *	                         V
8084  *		  +==============================+
8085  *	L2ARC dev |####|#|###|###|    |####| ... |
8086  *	          +==============================+
8087  *	                     32 Gbytes
8088  *
8089  * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
8090  * evicted, then the L2ARC has cached a buffer much sooner than it probably
8091  * needed to, potentially wasting L2ARC device bandwidth and storage.  It is
8092  * safe to say that this is an uncommon case, since buffers at the end of
8093  * the ARC lists have moved there due to inactivity.
8094  *
8095  * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
8096  * then the L2ARC simply misses copying some buffers.  This serves as a
8097  * pressure valve to prevent heavy read workloads from both stalling the ARC
8098  * with waits and clogging the L2ARC with writes.  This also helps prevent
8099  * the potential for the L2ARC to churn if it attempts to cache content too
8100  * quickly, such as during backups of the entire pool.
8101  *
8102  * 5. After system boot and before the ARC has filled main memory, there are
8103  * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
8104  * lists can remain mostly static.  Instead of searching from tail of these
8105  * lists as pictured, the l2arc_feed_thread() will search from the list heads
8106  * for eligible buffers, greatly increasing its chance of finding them.
8107  *
8108  * The L2ARC device write speed is also boosted during this time so that
8109  * the L2ARC warms up faster.  Since there have been no ARC evictions yet,
8110  * there are no L2ARC reads, and no fear of degrading read performance
8111  * through increased writes.
8112  *
8113  * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
8114  * the vdev queue can aggregate them into larger and fewer writes.  Each
8115  * device is written to in a rotor fashion, sweeping writes through
8116  * available space then repeating.
8117  *
8118  * 7. The L2ARC does not store dirty content.  It never needs to flush
8119  * write buffers back to disk based storage.
8120  *
8121  * 8. If an ARC buffer is written (and dirtied) which also exists in the
8122  * L2ARC, the now stale L2ARC buffer is immediately dropped.
8123  *
8124  * The performance of the L2ARC can be tweaked by a number of tunables, which
8125  * may be necessary for different workloads:
8126  *
8127  *	l2arc_write_max		max write bytes per interval
8128  *	l2arc_write_boost	extra write bytes during device warmup
8129  *	l2arc_noprefetch	skip caching prefetched buffers
8130  *	l2arc_headroom		number of max device writes to precache
8131  *	l2arc_headroom_boost	when we find compressed buffers during ARC
8132  *				scanning, we multiply headroom by this
8133  *				percentage factor for the next scan cycle,
8134  *				since more compressed buffers are likely to
8135  *				be present
8136  *	l2arc_feed_secs		seconds between L2ARC writing
8137  *
8138  * Tunables may be removed or added as future performance improvements are
8139  * integrated, and also may become zpool properties.
8140  *
8141  * There are three key functions that control how the L2ARC warms up:
8142  *
8143  *	l2arc_write_eligible()	check if a buffer is eligible to cache
8144  *	l2arc_write_size()	calculate how much to write
8145  *	l2arc_write_interval()	calculate sleep delay between writes
8146  *
8147  * These three functions determine what to write, how much, and how quickly
8148  * to send writes.
8149  *
8150  * L2ARC persistence:
8151  *
8152  * When writing buffers to L2ARC, we periodically add some metadata to
8153  * make sure we can pick them up after reboot, thus dramatically reducing
8154  * the impact that any downtime has on the performance of storage systems
8155  * with large caches.
8156  *
8157  * The implementation works fairly simply by integrating the following two
8158  * modifications:
8159  *
8160  * *) When writing to the L2ARC, we occasionally write a "l2arc log block",
8161  *    which is an additional piece of metadata which describes what's been
8162  *    written. This allows us to rebuild the arc_buf_hdr_t structures of the
8163  *    main ARC buffers. There are 2 linked-lists of log blocks headed by
8164  *    dh_start_lbps[2]. We alternate which chain we append to, so they are
8165  *    time-wise and offset-wise interleaved, but that is an optimization rather
8166  *    than for correctness. The log block also includes a pointer to the
8167  *    previous block in its chain.
8168  *
8169  * *) We reserve SPA_MINBLOCKSIZE of space at the start of each L2ARC device
8170  *    for our header bookkeeping purposes. This contains a device header,
8171  *    which contains our top-level reference structures. We update it each
8172  *    time we write a new log block, so that we're able to locate it in the
8173  *    L2ARC device. If this write results in an inconsistent device header
8174  *    (e.g. due to power failure), we detect this by verifying the header's
8175  *    checksum and simply fail to reconstruct the L2ARC after reboot.
8176  *
8177  * Implementation diagram:
8178  *
8179  * +=== L2ARC device (not to scale) ======================================+
8180  * |       ___two newest log block pointers__.__________                  |
8181  * |      /                                   \dh_start_lbps[1]           |
8182  * |	 /				       \         \dh_start_lbps[0]|
8183  * |.___/__.                                    V         V               |
8184  * ||L2 dev|....|lb |bufs |lb |bufs |lb |bufs |lb |bufs |lb |---(empty)---|
8185  * ||   hdr|      ^         /^       /^        /         /                |
8186  * |+------+  ...--\-------/  \-----/--\------/         /                 |
8187  * |                \--------------/    \--------------/                  |
8188  * +======================================================================+
8189  *
8190  * As can be seen on the diagram, rather than using a simple linked list,
8191  * we use a pair of linked lists with alternating elements. This is a
8192  * performance enhancement due to the fact that we only find out the
8193  * address of the next log block access once the current block has been
8194  * completely read in. Obviously, this hurts performance, because we'd be
8195  * keeping the device's I/O queue at only a 1 operation deep, thus
8196  * incurring a large amount of I/O round-trip latency. Having two lists
8197  * allows us to fetch two log blocks ahead of where we are currently
8198  * rebuilding L2ARC buffers.
8199  *
8200  * On-device data structures:
8201  *
8202  * L2ARC device header:	l2arc_dev_hdr_phys_t
8203  * L2ARC log block:	l2arc_log_blk_phys_t
8204  *
8205  * L2ARC reconstruction:
8206  *
8207  * When writing data, we simply write in the standard rotary fashion,
8208  * evicting buffers as we go and simply writing new data over them (writing
8209  * a new log block every now and then). This obviously means that once we
8210  * loop around the end of the device, we will start cutting into an already
8211  * committed log block (and its referenced data buffers), like so:
8212  *
8213  *    current write head__       __old tail
8214  *                        \     /
8215  *                        V    V
8216  * <--|bufs |lb |bufs |lb |    |bufs |lb |bufs |lb |-->
8217  *                         ^    ^^^^^^^^^___________________________________
8218  *                         |                                                \
8219  *                   <<nextwrite>> may overwrite this blk and/or its bufs --'
8220  *
8221  * When importing the pool, we detect this situation and use it to stop
8222  * our scanning process (see l2arc_rebuild).
8223  *
8224  * There is one significant caveat to consider when rebuilding ARC contents
8225  * from an L2ARC device: what about invalidated buffers? Given the above
8226  * construction, we cannot update blocks which we've already written to amend
8227  * them to remove buffers which were invalidated. Thus, during reconstruction,
8228  * we might be populating the cache with buffers for data that's not on the
8229  * main pool anymore, or may have been overwritten!
8230  *
8231  * As it turns out, this isn't a problem. Every arc_read request includes
8232  * both the DVA and, crucially, the birth TXG of the BP the caller is
8233  * looking for. So even if the cache were populated by completely rotten
8234  * blocks for data that had been long deleted and/or overwritten, we'll
8235  * never actually return bad data from the cache, since the DVA with the
8236  * birth TXG uniquely identify a block in space and time - once created,
8237  * a block is immutable on disk. The worst thing we have done is wasted
8238  * some time and memory at l2arc rebuild to reconstruct outdated ARC
8239  * entries that will get dropped from the l2arc as it is being updated
8240  * with new blocks.
8241  *
8242  * L2ARC buffers that have been evicted by l2arc_evict() ahead of the write
8243  * hand are not restored. This is done by saving the offset (in bytes)
8244  * l2arc_evict() has evicted to in the L2ARC device header and taking it
8245  * into account when restoring buffers.
8246  */
8247 
8248 static boolean_t
l2arc_write_eligible(uint64_t spa_guid,arc_buf_hdr_t * hdr)8249 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *hdr)
8250 {
8251 	/*
8252 	 * A buffer is *not* eligible for the L2ARC if it:
8253 	 * 1. belongs to a different spa.
8254 	 * 2. is already cached on the L2ARC.
8255 	 * 3. has an I/O in progress (it may be an incomplete read).
8256 	 * 4. is flagged not eligible (zfs property).
8257 	 */
8258 	if (hdr->b_spa != spa_guid || HDR_HAS_L2HDR(hdr) ||
8259 	    HDR_IO_IN_PROGRESS(hdr) || !HDR_L2CACHE(hdr))
8260 		return (B_FALSE);
8261 
8262 	return (B_TRUE);
8263 }
8264 
8265 static uint64_t
l2arc_write_size(l2arc_dev_t * dev)8266 l2arc_write_size(l2arc_dev_t *dev)
8267 {
8268 	uint64_t size;
8269 
8270 	/*
8271 	 * Make sure our globals have meaningful values in case the user
8272 	 * altered them.
8273 	 */
8274 	size = l2arc_write_max;
8275 	if (size == 0) {
8276 		cmn_err(CE_NOTE, "l2arc_write_max must be greater than zero, "
8277 		    "resetting it to the default (%d)", L2ARC_WRITE_SIZE);
8278 		size = l2arc_write_max = L2ARC_WRITE_SIZE;
8279 	}
8280 
8281 	if (arc_warm == B_FALSE)
8282 		size += l2arc_write_boost;
8283 
8284 	/* We need to add in the worst case scenario of log block overhead. */
8285 	size += l2arc_log_blk_overhead(size, dev);
8286 	if (dev->l2ad_vdev->vdev_has_trim && l2arc_trim_ahead > 0) {
8287 		/*
8288 		 * Trim ahead of the write size 64MB or (l2arc_trim_ahead/100)
8289 		 * times the writesize, whichever is greater.
8290 		 */
8291 		size += MAX(64 * 1024 * 1024,
8292 		    (size * l2arc_trim_ahead) / 100);
8293 	}
8294 
8295 	/*
8296 	 * Make sure the write size does not exceed the size of the cache
8297 	 * device. This is important in l2arc_evict(), otherwise infinite
8298 	 * iteration can occur.
8299 	 */
8300 	size = MIN(size, (dev->l2ad_end - dev->l2ad_start) / 4);
8301 
8302 	size = P2ROUNDUP(size, 1ULL << dev->l2ad_vdev->vdev_ashift);
8303 
8304 	return (size);
8305 
8306 }
8307 
8308 static clock_t
l2arc_write_interval(clock_t began,uint64_t wanted,uint64_t wrote)8309 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
8310 {
8311 	clock_t interval, next, now;
8312 
8313 	/*
8314 	 * If the ARC lists are busy, increase our write rate; if the
8315 	 * lists are stale, idle back.  This is achieved by checking
8316 	 * how much we previously wrote - if it was more than half of
8317 	 * what we wanted, schedule the next write much sooner.
8318 	 */
8319 	if (l2arc_feed_again && wrote > (wanted / 2))
8320 		interval = (hz * l2arc_feed_min_ms) / 1000;
8321 	else
8322 		interval = hz * l2arc_feed_secs;
8323 
8324 	now = ddi_get_lbolt();
8325 	next = MAX(now, MIN(now + interval, began + interval));
8326 
8327 	return (next);
8328 }
8329 
8330 static boolean_t
l2arc_dev_invalid(const l2arc_dev_t * dev)8331 l2arc_dev_invalid(const l2arc_dev_t *dev)
8332 {
8333 	/*
8334 	 * We want to skip devices that are being rebuilt, trimmed,
8335 	 * removed, or belong to a spa that is being exported.
8336 	 */
8337 	return (dev->l2ad_vdev == NULL || vdev_is_dead(dev->l2ad_vdev) ||
8338 	    dev->l2ad_rebuild || dev->l2ad_trim_all ||
8339 	    dev->l2ad_spa == NULL || dev->l2ad_spa->spa_is_exporting);
8340 }
8341 
8342 /*
8343  * Cycle through L2ARC devices.  This is how L2ARC load balances.
8344  * If a device is returned, this also returns holding the spa config lock.
8345  */
8346 static l2arc_dev_t *
l2arc_dev_get_next(void)8347 l2arc_dev_get_next(void)
8348 {
8349 	l2arc_dev_t *first, *next = NULL;
8350 
8351 	/*
8352 	 * Lock out the removal of spas (spa_namespace_lock), then removal
8353 	 * of cache devices (l2arc_dev_mtx).  Once a device has been selected,
8354 	 * both locks will be dropped and a spa config lock held instead.
8355 	 */
8356 	mutex_enter(&spa_namespace_lock);
8357 	mutex_enter(&l2arc_dev_mtx);
8358 
8359 	/* if there are no vdevs, there is nothing to do */
8360 	if (l2arc_ndev == 0)
8361 		goto out;
8362 
8363 	first = NULL;
8364 	next = l2arc_dev_last;
8365 	do {
8366 		/* loop around the list looking for a non-faulted vdev */
8367 		if (next == NULL) {
8368 			next = list_head(l2arc_dev_list);
8369 		} else {
8370 			next = list_next(l2arc_dev_list, next);
8371 			if (next == NULL)
8372 				next = list_head(l2arc_dev_list);
8373 		}
8374 
8375 		/* if we have come back to the start, bail out */
8376 		if (first == NULL)
8377 			first = next;
8378 		else if (next == first)
8379 			break;
8380 
8381 		ASSERT3P(next, !=, NULL);
8382 	} while (l2arc_dev_invalid(next));
8383 
8384 	/* if we were unable to find any usable vdevs, return NULL */
8385 	if (l2arc_dev_invalid(next))
8386 		next = NULL;
8387 
8388 	l2arc_dev_last = next;
8389 
8390 out:
8391 	mutex_exit(&l2arc_dev_mtx);
8392 
8393 	/*
8394 	 * Grab the config lock to prevent the 'next' device from being
8395 	 * removed while we are writing to it.
8396 	 */
8397 	if (next != NULL)
8398 		spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
8399 	mutex_exit(&spa_namespace_lock);
8400 
8401 	return (next);
8402 }
8403 
8404 /*
8405  * Free buffers that were tagged for destruction.
8406  */
8407 static void
l2arc_do_free_on_write(void)8408 l2arc_do_free_on_write(void)
8409 {
8410 	l2arc_data_free_t *df;
8411 
8412 	mutex_enter(&l2arc_free_on_write_mtx);
8413 	while ((df = list_remove_head(l2arc_free_on_write)) != NULL) {
8414 		ASSERT3P(df->l2df_abd, !=, NULL);
8415 		abd_free(df->l2df_abd);
8416 		kmem_free(df, sizeof (l2arc_data_free_t));
8417 	}
8418 	mutex_exit(&l2arc_free_on_write_mtx);
8419 }
8420 
8421 /*
8422  * A write to a cache device has completed.  Update all headers to allow
8423  * reads from these buffers to begin.
8424  */
8425 static void
l2arc_write_done(zio_t * zio)8426 l2arc_write_done(zio_t *zio)
8427 {
8428 	l2arc_write_callback_t	*cb;
8429 	l2arc_lb_abd_buf_t	*abd_buf;
8430 	l2arc_lb_ptr_buf_t	*lb_ptr_buf;
8431 	l2arc_dev_t		*dev;
8432 	l2arc_dev_hdr_phys_t	*l2dhdr;
8433 	list_t			*buflist;
8434 	arc_buf_hdr_t		*head, *hdr, *hdr_prev;
8435 	kmutex_t		*hash_lock;
8436 	int64_t			bytes_dropped = 0;
8437 
8438 	cb = zio->io_private;
8439 	ASSERT3P(cb, !=, NULL);
8440 	dev = cb->l2wcb_dev;
8441 	l2dhdr = dev->l2ad_dev_hdr;
8442 	ASSERT3P(dev, !=, NULL);
8443 	head = cb->l2wcb_head;
8444 	ASSERT3P(head, !=, NULL);
8445 	buflist = &dev->l2ad_buflist;
8446 	ASSERT3P(buflist, !=, NULL);
8447 	DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
8448 	    l2arc_write_callback_t *, cb);
8449 
8450 	/*
8451 	 * All writes completed, or an error was hit.
8452 	 */
8453 top:
8454 	mutex_enter(&dev->l2ad_mtx);
8455 	for (hdr = list_prev(buflist, head); hdr; hdr = hdr_prev) {
8456 		hdr_prev = list_prev(buflist, hdr);
8457 
8458 		hash_lock = HDR_LOCK(hdr);
8459 
8460 		/*
8461 		 * We cannot use mutex_enter or else we can deadlock
8462 		 * with l2arc_write_buffers (due to swapping the order
8463 		 * the hash lock and l2ad_mtx are taken).
8464 		 */
8465 		if (!mutex_tryenter(hash_lock)) {
8466 			/*
8467 			 * Missed the hash lock. We must retry so we
8468 			 * don't leave the ARC_FLAG_L2_WRITING bit set.
8469 			 */
8470 			ARCSTAT_BUMP(arcstat_l2_writes_lock_retry);
8471 
8472 			/*
8473 			 * We don't want to rescan the headers we've
8474 			 * already marked as having been written out, so
8475 			 * we reinsert the head node so we can pick up
8476 			 * where we left off.
8477 			 */
8478 			list_remove(buflist, head);
8479 			list_insert_after(buflist, hdr, head);
8480 
8481 			mutex_exit(&dev->l2ad_mtx);
8482 
8483 			/*
8484 			 * We wait for the hash lock to become available
8485 			 * to try and prevent busy waiting, and increase
8486 			 * the chance we'll be able to acquire the lock
8487 			 * the next time around.
8488 			 */
8489 			mutex_enter(hash_lock);
8490 			mutex_exit(hash_lock);
8491 			goto top;
8492 		}
8493 
8494 		/*
8495 		 * We could not have been moved into the arc_l2c_only
8496 		 * state while in-flight due to our ARC_FLAG_L2_WRITING
8497 		 * bit being set. Let's just ensure that's being enforced.
8498 		 */
8499 		ASSERT(HDR_HAS_L1HDR(hdr));
8500 
8501 		/*
8502 		 * Skipped - drop L2ARC entry and mark the header as no
8503 		 * longer L2 eligibile.
8504 		 */
8505 		if (zio->io_error != 0) {
8506 			/*
8507 			 * Error - drop L2ARC entry.
8508 			 */
8509 			list_remove(buflist, hdr);
8510 			arc_hdr_clear_flags(hdr, ARC_FLAG_HAS_L2HDR);
8511 
8512 			uint64_t psize = HDR_GET_PSIZE(hdr);
8513 			l2arc_hdr_arcstats_decrement(hdr);
8514 
8515 			ASSERT(dev->l2ad_vdev != NULL);
8516 
8517 			bytes_dropped +=
8518 			    vdev_psize_to_asize(dev->l2ad_vdev, psize);
8519 			(void) zfs_refcount_remove_many(&dev->l2ad_alloc,
8520 			    arc_hdr_size(hdr), hdr);
8521 		}
8522 
8523 		/*
8524 		 * Allow ARC to begin reads and ghost list evictions to
8525 		 * this L2ARC entry.
8526 		 */
8527 		arc_hdr_clear_flags(hdr, ARC_FLAG_L2_WRITING);
8528 
8529 		mutex_exit(hash_lock);
8530 	}
8531 
8532 	/*
8533 	 * Free the allocated abd buffers for writing the log blocks.
8534 	 * If the zio failed reclaim the allocated space and remove the
8535 	 * pointers to these log blocks from the log block pointer list
8536 	 * of the L2ARC device.
8537 	 */
8538 	while ((abd_buf = list_remove_tail(&cb->l2wcb_abd_list)) != NULL) {
8539 		abd_free(abd_buf->abd);
8540 		zio_buf_free(abd_buf, sizeof (*abd_buf));
8541 		if (zio->io_error != 0) {
8542 			lb_ptr_buf = list_remove_head(&dev->l2ad_lbptr_list);
8543 			/*
8544 			 * L2BLK_GET_PSIZE returns aligned size for log
8545 			 * blocks.
8546 			 */
8547 			uint64_t asize =
8548 			    L2BLK_GET_PSIZE((lb_ptr_buf->lb_ptr)->lbp_prop);
8549 			bytes_dropped += asize;
8550 			ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize);
8551 			ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count);
8552 			zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize,
8553 			    lb_ptr_buf);
8554 			(void) zfs_refcount_remove(&dev->l2ad_lb_count,
8555 			    lb_ptr_buf);
8556 			kmem_free(lb_ptr_buf->lb_ptr,
8557 			    sizeof (l2arc_log_blkptr_t));
8558 			kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t));
8559 		}
8560 	}
8561 	list_destroy(&cb->l2wcb_abd_list);
8562 
8563 	if (zio->io_error != 0) {
8564 		ARCSTAT_BUMP(arcstat_l2_writes_error);
8565 
8566 		/*
8567 		 * Restore the lbps array in the header to its previous state.
8568 		 * If the list of log block pointers is empty, zero out the
8569 		 * log block pointers in the device header.
8570 		 */
8571 		lb_ptr_buf = list_head(&dev->l2ad_lbptr_list);
8572 		for (int i = 0; i < 2; i++) {
8573 			if (lb_ptr_buf == NULL) {
8574 				/*
8575 				 * If the list is empty zero out the device
8576 				 * header. Otherwise zero out the second log
8577 				 * block pointer in the header.
8578 				 */
8579 				if (i == 0) {
8580 					memset(l2dhdr, 0,
8581 					    dev->l2ad_dev_hdr_asize);
8582 				} else {
8583 					memset(&l2dhdr->dh_start_lbps[i], 0,
8584 					    sizeof (l2arc_log_blkptr_t));
8585 				}
8586 				break;
8587 			}
8588 			memcpy(&l2dhdr->dh_start_lbps[i], lb_ptr_buf->lb_ptr,
8589 			    sizeof (l2arc_log_blkptr_t));
8590 			lb_ptr_buf = list_next(&dev->l2ad_lbptr_list,
8591 			    lb_ptr_buf);
8592 		}
8593 	}
8594 
8595 	ARCSTAT_BUMP(arcstat_l2_writes_done);
8596 	list_remove(buflist, head);
8597 	ASSERT(!HDR_HAS_L1HDR(head));
8598 	kmem_cache_free(hdr_l2only_cache, head);
8599 	mutex_exit(&dev->l2ad_mtx);
8600 
8601 	ASSERT(dev->l2ad_vdev != NULL);
8602 	vdev_space_update(dev->l2ad_vdev, -bytes_dropped, 0, 0);
8603 
8604 	l2arc_do_free_on_write();
8605 
8606 	kmem_free(cb, sizeof (l2arc_write_callback_t));
8607 }
8608 
8609 static int
l2arc_untransform(zio_t * zio,l2arc_read_callback_t * cb)8610 l2arc_untransform(zio_t *zio, l2arc_read_callback_t *cb)
8611 {
8612 	int ret;
8613 	spa_t *spa = zio->io_spa;
8614 	arc_buf_hdr_t *hdr = cb->l2rcb_hdr;
8615 	blkptr_t *bp = zio->io_bp;
8616 	uint8_t salt[ZIO_DATA_SALT_LEN];
8617 	uint8_t iv[ZIO_DATA_IV_LEN];
8618 	uint8_t mac[ZIO_DATA_MAC_LEN];
8619 	boolean_t no_crypt = B_FALSE;
8620 
8621 	/*
8622 	 * ZIL data is never be written to the L2ARC, so we don't need
8623 	 * special handling for its unique MAC storage.
8624 	 */
8625 	ASSERT3U(BP_GET_TYPE(bp), !=, DMU_OT_INTENT_LOG);
8626 	ASSERT(MUTEX_HELD(HDR_LOCK(hdr)));
8627 	ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
8628 
8629 	/*
8630 	 * If the data was encrypted, decrypt it now. Note that
8631 	 * we must check the bp here and not the hdr, since the
8632 	 * hdr does not have its encryption parameters updated
8633 	 * until arc_read_done().
8634 	 */
8635 	if (BP_IS_ENCRYPTED(bp)) {
8636 		abd_t *eabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr,
8637 		    ARC_HDR_USE_RESERVE);
8638 
8639 		zio_crypt_decode_params_bp(bp, salt, iv);
8640 		zio_crypt_decode_mac_bp(bp, mac);
8641 
8642 		ret = spa_do_crypt_abd(B_FALSE, spa, &cb->l2rcb_zb,
8643 		    BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
8644 		    salt, iv, mac, HDR_GET_PSIZE(hdr), eabd,
8645 		    hdr->b_l1hdr.b_pabd, &no_crypt);
8646 		if (ret != 0) {
8647 			arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr);
8648 			goto error;
8649 		}
8650 
8651 		/*
8652 		 * If we actually performed decryption, replace b_pabd
8653 		 * with the decrypted data. Otherwise we can just throw
8654 		 * our decryption buffer away.
8655 		 */
8656 		if (!no_crypt) {
8657 			arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd,
8658 			    arc_hdr_size(hdr), hdr);
8659 			hdr->b_l1hdr.b_pabd = eabd;
8660 			zio->io_abd = eabd;
8661 		} else {
8662 			arc_free_data_abd(hdr, eabd, arc_hdr_size(hdr), hdr);
8663 		}
8664 	}
8665 
8666 	/*
8667 	 * If the L2ARC block was compressed, but ARC compression
8668 	 * is disabled we decompress the data into a new buffer and
8669 	 * replace the existing data.
8670 	 */
8671 	if (HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
8672 	    !HDR_COMPRESSION_ENABLED(hdr)) {
8673 		abd_t *cabd = arc_get_data_abd(hdr, arc_hdr_size(hdr), hdr,
8674 		    ARC_HDR_USE_RESERVE);
8675 
8676 		ret = zio_decompress_data(HDR_GET_COMPRESS(hdr),
8677 		    hdr->b_l1hdr.b_pabd, cabd, HDR_GET_PSIZE(hdr),
8678 		    HDR_GET_LSIZE(hdr), &hdr->b_complevel);
8679 		if (ret != 0) {
8680 			arc_free_data_abd(hdr, cabd, arc_hdr_size(hdr), hdr);
8681 			goto error;
8682 		}
8683 
8684 		arc_free_data_abd(hdr, hdr->b_l1hdr.b_pabd,
8685 		    arc_hdr_size(hdr), hdr);
8686 		hdr->b_l1hdr.b_pabd = cabd;
8687 		zio->io_abd = cabd;
8688 		zio->io_size = HDR_GET_LSIZE(hdr);
8689 	}
8690 
8691 	return (0);
8692 
8693 error:
8694 	return (ret);
8695 }
8696 
8697 
8698 /*
8699  * A read to a cache device completed.  Validate buffer contents before
8700  * handing over to the regular ARC routines.
8701  */
8702 static void
l2arc_read_done(zio_t * zio)8703 l2arc_read_done(zio_t *zio)
8704 {
8705 	int tfm_error = 0;
8706 	l2arc_read_callback_t *cb = zio->io_private;
8707 	arc_buf_hdr_t *hdr;
8708 	kmutex_t *hash_lock;
8709 	boolean_t valid_cksum;
8710 	boolean_t using_rdata = (BP_IS_ENCRYPTED(&cb->l2rcb_bp) &&
8711 	    (cb->l2rcb_flags & ZIO_FLAG_RAW_ENCRYPT));
8712 
8713 	ASSERT3P(zio->io_vd, !=, NULL);
8714 	ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
8715 
8716 	spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
8717 
8718 	ASSERT3P(cb, !=, NULL);
8719 	hdr = cb->l2rcb_hdr;
8720 	ASSERT3P(hdr, !=, NULL);
8721 
8722 	hash_lock = HDR_LOCK(hdr);
8723 	mutex_enter(hash_lock);
8724 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
8725 
8726 	/*
8727 	 * If the data was read into a temporary buffer,
8728 	 * move it and free the buffer.
8729 	 */
8730 	if (cb->l2rcb_abd != NULL) {
8731 		ASSERT3U(arc_hdr_size(hdr), <, zio->io_size);
8732 		if (zio->io_error == 0) {
8733 			if (using_rdata) {
8734 				abd_copy(hdr->b_crypt_hdr.b_rabd,
8735 				    cb->l2rcb_abd, arc_hdr_size(hdr));
8736 			} else {
8737 				abd_copy(hdr->b_l1hdr.b_pabd,
8738 				    cb->l2rcb_abd, arc_hdr_size(hdr));
8739 			}
8740 		}
8741 
8742 		/*
8743 		 * The following must be done regardless of whether
8744 		 * there was an error:
8745 		 * - free the temporary buffer
8746 		 * - point zio to the real ARC buffer
8747 		 * - set zio size accordingly
8748 		 * These are required because zio is either re-used for
8749 		 * an I/O of the block in the case of the error
8750 		 * or the zio is passed to arc_read_done() and it
8751 		 * needs real data.
8752 		 */
8753 		abd_free(cb->l2rcb_abd);
8754 		zio->io_size = zio->io_orig_size = arc_hdr_size(hdr);
8755 
8756 		if (using_rdata) {
8757 			ASSERT(HDR_HAS_RABD(hdr));
8758 			zio->io_abd = zio->io_orig_abd =
8759 			    hdr->b_crypt_hdr.b_rabd;
8760 		} else {
8761 			ASSERT3P(hdr->b_l1hdr.b_pabd, !=, NULL);
8762 			zio->io_abd = zio->io_orig_abd = hdr->b_l1hdr.b_pabd;
8763 		}
8764 	}
8765 
8766 	ASSERT3P(zio->io_abd, !=, NULL);
8767 
8768 	/*
8769 	 * Check this survived the L2ARC journey.
8770 	 */
8771 	ASSERT(zio->io_abd == hdr->b_l1hdr.b_pabd ||
8772 	    (HDR_HAS_RABD(hdr) && zio->io_abd == hdr->b_crypt_hdr.b_rabd));
8773 	zio->io_bp_copy = cb->l2rcb_bp;	/* XXX fix in L2ARC 2.0	*/
8774 	zio->io_bp = &zio->io_bp_copy;	/* XXX fix in L2ARC 2.0	*/
8775 	zio->io_prop.zp_complevel = hdr->b_complevel;
8776 
8777 	valid_cksum = arc_cksum_is_equal(hdr, zio);
8778 
8779 	/*
8780 	 * b_rabd will always match the data as it exists on disk if it is
8781 	 * being used. Therefore if we are reading into b_rabd we do not
8782 	 * attempt to untransform the data.
8783 	 */
8784 	if (valid_cksum && !using_rdata)
8785 		tfm_error = l2arc_untransform(zio, cb);
8786 
8787 	if (valid_cksum && tfm_error == 0 && zio->io_error == 0 &&
8788 	    !HDR_L2_EVICTED(hdr)) {
8789 		mutex_exit(hash_lock);
8790 		zio->io_private = hdr;
8791 		arc_read_done(zio);
8792 	} else {
8793 		/*
8794 		 * Buffer didn't survive caching.  Increment stats and
8795 		 * reissue to the original storage device.
8796 		 */
8797 		if (zio->io_error != 0) {
8798 			ARCSTAT_BUMP(arcstat_l2_io_error);
8799 		} else {
8800 			zio->io_error = SET_ERROR(EIO);
8801 		}
8802 		if (!valid_cksum || tfm_error != 0)
8803 			ARCSTAT_BUMP(arcstat_l2_cksum_bad);
8804 
8805 		/*
8806 		 * If there's no waiter, issue an async i/o to the primary
8807 		 * storage now.  If there *is* a waiter, the caller must
8808 		 * issue the i/o in a context where it's OK to block.
8809 		 */
8810 		if (zio->io_waiter == NULL) {
8811 			zio_t *pio = zio_unique_parent(zio);
8812 			void *abd = (using_rdata) ?
8813 			    hdr->b_crypt_hdr.b_rabd : hdr->b_l1hdr.b_pabd;
8814 
8815 			ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
8816 
8817 			zio = zio_read(pio, zio->io_spa, zio->io_bp,
8818 			    abd, zio->io_size, arc_read_done,
8819 			    hdr, zio->io_priority, cb->l2rcb_flags,
8820 			    &cb->l2rcb_zb);
8821 
8822 			/*
8823 			 * Original ZIO will be freed, so we need to update
8824 			 * ARC header with the new ZIO pointer to be used
8825 			 * by zio_change_priority() in arc_read().
8826 			 */
8827 			for (struct arc_callback *acb = hdr->b_l1hdr.b_acb;
8828 			    acb != NULL; acb = acb->acb_next)
8829 				acb->acb_zio_head = zio;
8830 
8831 			mutex_exit(hash_lock);
8832 			zio_nowait(zio);
8833 		} else {
8834 			mutex_exit(hash_lock);
8835 		}
8836 	}
8837 
8838 	kmem_free(cb, sizeof (l2arc_read_callback_t));
8839 }
8840 
8841 /*
8842  * This is the list priority from which the L2ARC will search for pages to
8843  * cache.  This is used within loops (0..3) to cycle through lists in the
8844  * desired order.  This order can have a significant effect on cache
8845  * performance.
8846  *
8847  * Currently the metadata lists are hit first, MFU then MRU, followed by
8848  * the data lists.  This function returns a locked list, and also returns
8849  * the lock pointer.
8850  */
8851 static multilist_sublist_t *
l2arc_sublist_lock(int list_num)8852 l2arc_sublist_lock(int list_num)
8853 {
8854 	multilist_t *ml = NULL;
8855 	unsigned int idx;
8856 
8857 	ASSERT(list_num >= 0 && list_num < L2ARC_FEED_TYPES);
8858 
8859 	switch (list_num) {
8860 	case 0:
8861 		ml = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
8862 		break;
8863 	case 1:
8864 		ml = &arc_mru->arcs_list[ARC_BUFC_METADATA];
8865 		break;
8866 	case 2:
8867 		ml = &arc_mfu->arcs_list[ARC_BUFC_DATA];
8868 		break;
8869 	case 3:
8870 		ml = &arc_mru->arcs_list[ARC_BUFC_DATA];
8871 		break;
8872 	default:
8873 		return (NULL);
8874 	}
8875 
8876 	/*
8877 	 * Return a randomly-selected sublist. This is acceptable
8878 	 * because the caller feeds only a little bit of data for each
8879 	 * call (8MB). Subsequent calls will result in different
8880 	 * sublists being selected.
8881 	 */
8882 	idx = multilist_get_random_index(ml);
8883 	return (multilist_sublist_lock_idx(ml, idx));
8884 }
8885 
8886 /*
8887  * Calculates the maximum overhead of L2ARC metadata log blocks for a given
8888  * L2ARC write size. l2arc_evict and l2arc_write_size need to include this
8889  * overhead in processing to make sure there is enough headroom available
8890  * when writing buffers.
8891  */
8892 static inline uint64_t
l2arc_log_blk_overhead(uint64_t write_sz,l2arc_dev_t * dev)8893 l2arc_log_blk_overhead(uint64_t write_sz, l2arc_dev_t *dev)
8894 {
8895 	if (dev->l2ad_log_entries == 0) {
8896 		return (0);
8897 	} else {
8898 		ASSERT(dev->l2ad_vdev != NULL);
8899 
8900 		uint64_t log_entries = write_sz >> SPA_MINBLOCKSHIFT;
8901 
8902 		uint64_t log_blocks = (log_entries +
8903 		    dev->l2ad_log_entries - 1) /
8904 		    dev->l2ad_log_entries;
8905 
8906 		return (vdev_psize_to_asize(dev->l2ad_vdev,
8907 		    sizeof (l2arc_log_blk_phys_t)) * log_blocks);
8908 	}
8909 }
8910 
8911 /*
8912  * Evict buffers from the device write hand to the distance specified in
8913  * bytes. This distance may span populated buffers, it may span nothing.
8914  * This is clearing a region on the L2ARC device ready for writing.
8915  * If the 'all' boolean is set, every buffer is evicted.
8916  */
8917 static void
l2arc_evict(l2arc_dev_t * dev,uint64_t distance,boolean_t all)8918 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
8919 {
8920 	list_t *buflist;
8921 	arc_buf_hdr_t *hdr, *hdr_prev;
8922 	kmutex_t *hash_lock;
8923 	uint64_t taddr;
8924 	l2arc_lb_ptr_buf_t *lb_ptr_buf, *lb_ptr_buf_prev;
8925 	vdev_t *vd = dev->l2ad_vdev;
8926 	boolean_t rerun;
8927 
8928 	ASSERT(vd != NULL || all);
8929 	ASSERT(dev->l2ad_spa != NULL || all);
8930 
8931 	buflist = &dev->l2ad_buflist;
8932 
8933 top:
8934 	rerun = B_FALSE;
8935 	if (dev->l2ad_hand + distance > dev->l2ad_end) {
8936 		/*
8937 		 * When there is no space to accommodate upcoming writes,
8938 		 * evict to the end. Then bump the write and evict hands
8939 		 * to the start and iterate. This iteration does not
8940 		 * happen indefinitely as we make sure in
8941 		 * l2arc_write_size() that when the write hand is reset,
8942 		 * the write size does not exceed the end of the device.
8943 		 */
8944 		rerun = B_TRUE;
8945 		taddr = dev->l2ad_end;
8946 	} else {
8947 		taddr = dev->l2ad_hand + distance;
8948 	}
8949 	DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
8950 	    uint64_t, taddr, boolean_t, all);
8951 
8952 	if (!all) {
8953 		/*
8954 		 * This check has to be placed after deciding whether to
8955 		 * iterate (rerun).
8956 		 */
8957 		if (dev->l2ad_first) {
8958 			/*
8959 			 * This is the first sweep through the device. There is
8960 			 * nothing to evict. We have already trimmmed the
8961 			 * whole device.
8962 			 */
8963 			goto out;
8964 		} else {
8965 			/*
8966 			 * Trim the space to be evicted.
8967 			 */
8968 			if (vd->vdev_has_trim && dev->l2ad_evict < taddr &&
8969 			    l2arc_trim_ahead > 0) {
8970 				/*
8971 				 * We have to drop the spa_config lock because
8972 				 * vdev_trim_range() will acquire it.
8973 				 * l2ad_evict already accounts for the label
8974 				 * size. To prevent vdev_trim_ranges() from
8975 				 * adding it again, we subtract it from
8976 				 * l2ad_evict.
8977 				 */
8978 				spa_config_exit(dev->l2ad_spa, SCL_L2ARC, dev);
8979 				vdev_trim_simple(vd,
8980 				    dev->l2ad_evict - VDEV_LABEL_START_SIZE,
8981 				    taddr - dev->l2ad_evict);
8982 				spa_config_enter(dev->l2ad_spa, SCL_L2ARC, dev,
8983 				    RW_READER);
8984 			}
8985 
8986 			/*
8987 			 * When rebuilding L2ARC we retrieve the evict hand
8988 			 * from the header of the device. Of note, l2arc_evict()
8989 			 * does not actually delete buffers from the cache
8990 			 * device, but trimming may do so depending on the
8991 			 * hardware implementation. Thus keeping track of the
8992 			 * evict hand is useful.
8993 			 */
8994 			dev->l2ad_evict = MAX(dev->l2ad_evict, taddr);
8995 		}
8996 	}
8997 
8998 retry:
8999 	mutex_enter(&dev->l2ad_mtx);
9000 	/*
9001 	 * We have to account for evicted log blocks. Run vdev_space_update()
9002 	 * on log blocks whose offset (in bytes) is before the evicted offset
9003 	 * (in bytes) by searching in the list of pointers to log blocks
9004 	 * present in the L2ARC device.
9005 	 */
9006 	for (lb_ptr_buf = list_tail(&dev->l2ad_lbptr_list); lb_ptr_buf;
9007 	    lb_ptr_buf = lb_ptr_buf_prev) {
9008 
9009 		lb_ptr_buf_prev = list_prev(&dev->l2ad_lbptr_list, lb_ptr_buf);
9010 
9011 		/* L2BLK_GET_PSIZE returns aligned size for log blocks */
9012 		uint64_t asize = L2BLK_GET_PSIZE(
9013 		    (lb_ptr_buf->lb_ptr)->lbp_prop);
9014 
9015 		/*
9016 		 * We don't worry about log blocks left behind (ie
9017 		 * lbp_payload_start < l2ad_hand) because l2arc_write_buffers()
9018 		 * will never write more than l2arc_evict() evicts.
9019 		 */
9020 		if (!all && l2arc_log_blkptr_valid(dev, lb_ptr_buf->lb_ptr)) {
9021 			break;
9022 		} else {
9023 			if (vd != NULL)
9024 				vdev_space_update(vd, -asize, 0, 0);
9025 			ARCSTAT_INCR(arcstat_l2_log_blk_asize, -asize);
9026 			ARCSTAT_BUMPDOWN(arcstat_l2_log_blk_count);
9027 			zfs_refcount_remove_many(&dev->l2ad_lb_asize, asize,
9028 			    lb_ptr_buf);
9029 			(void) zfs_refcount_remove(&dev->l2ad_lb_count,
9030 			    lb_ptr_buf);
9031 			list_remove(&dev->l2ad_lbptr_list, lb_ptr_buf);
9032 			kmem_free(lb_ptr_buf->lb_ptr,
9033 			    sizeof (l2arc_log_blkptr_t));
9034 			kmem_free(lb_ptr_buf, sizeof (l2arc_lb_ptr_buf_t));
9035 		}
9036 	}
9037 
9038 	for (hdr = list_tail(buflist); hdr; hdr = hdr_prev) {
9039 		hdr_prev = list_prev(buflist, hdr);
9040 
9041 		ASSERT(!HDR_EMPTY(hdr));
9042 		hash_lock = HDR_LOCK(hdr);
9043 
9044 		/*
9045 		 * We cannot use mutex_enter or else we can deadlock
9046 		 * with l2arc_write_buffers (due to swapping the order
9047 		 * the hash lock and l2ad_mtx are taken).
9048 		 */
9049 		if (!mutex_tryenter(hash_lock)) {
9050 			/*
9051 			 * Missed the hash lock.  Retry.
9052 			 */
9053 			ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
9054 			mutex_exit(&dev->l2ad_mtx);
9055 			mutex_enter(hash_lock);
9056 			mutex_exit(hash_lock);
9057 			goto retry;
9058 		}
9059 
9060 		/*
9061 		 * A header can't be on this list if it doesn't have L2 header.
9062 		 */
9063 		ASSERT(HDR_HAS_L2HDR(hdr));
9064 
9065 		/* Ensure this header has finished being written. */
9066 		ASSERT(!HDR_L2_WRITING(hdr));
9067 		ASSERT(!HDR_L2_WRITE_HEAD(hdr));
9068 
9069 		if (!all && (hdr->b_l2hdr.b_daddr >= dev->l2ad_evict ||
9070 		    hdr->b_l2hdr.b_daddr < dev->l2ad_hand)) {
9071 			/*
9072 			 * We've evicted to the target address,
9073 			 * or the end of the device.
9074 			 */
9075 			mutex_exit(hash_lock);
9076 			break;
9077 		}
9078 
9079 		if (!HDR_HAS_L1HDR(hdr)) {
9080 			ASSERT(!HDR_L2_READING(hdr));
9081 			/*
9082 			 * This doesn't exist in the ARC.  Destroy.
9083 			 * arc_hdr_destroy() will call list_remove()
9084 			 * and decrement arcstat_l2_lsize.
9085 			 */
9086 			arc_change_state(arc_anon, hdr);
9087 			arc_hdr_destroy(hdr);
9088 		} else {
9089 			ASSERT(hdr->b_l1hdr.b_state != arc_l2c_only);
9090 			ARCSTAT_BUMP(arcstat_l2_evict_l1cached);
9091 			/*
9092 			 * Invalidate issued or about to be issued
9093 			 * reads, since we may be about to write
9094 			 * over this location.
9095 			 */
9096 			if (HDR_L2_READING(hdr)) {
9097 				ARCSTAT_BUMP(arcstat_l2_evict_reading);
9098 				arc_hdr_set_flags(hdr, ARC_FLAG_L2_EVICTED);
9099 			}
9100 
9101 			arc_hdr_l2hdr_destroy(hdr);
9102 		}
9103 		mutex_exit(hash_lock);
9104 	}
9105 	mutex_exit(&dev->l2ad_mtx);
9106 
9107 out:
9108 	/*
9109 	 * We need to check if we evict all buffers, otherwise we may iterate
9110 	 * unnecessarily.
9111 	 */
9112 	if (!all && rerun) {
9113 		/*
9114 		 * Bump device hand to the device start if it is approaching the
9115 		 * end. l2arc_evict() has already evicted ahead for this case.
9116 		 */
9117 		dev->l2ad_hand = dev->l2ad_start;
9118 		dev->l2ad_evict = dev->l2ad_start;
9119 		dev->l2ad_first = B_FALSE;
9120 		goto top;
9121 	}
9122 
9123 	if (!all) {
9124 		/*
9125 		 * In case of cache device removal (all) the following
9126 		 * assertions may be violated without functional consequences
9127 		 * as the device is about to be removed.
9128 		 */
9129 		ASSERT3U(dev->l2ad_hand + distance, <=, dev->l2ad_end);
9130 		if (!dev->l2ad_first)
9131 			ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict);
9132 	}
9133 }
9134 
9135 /*
9136  * Handle any abd transforms that might be required for writing to the L2ARC.
9137  * If successful, this function will always return an abd with the data
9138  * transformed as it is on disk in a new abd of asize bytes.
9139  */
9140 static int
l2arc_apply_transforms(spa_t * spa,arc_buf_hdr_t * hdr,uint64_t asize,abd_t ** abd_out)9141 l2arc_apply_transforms(spa_t *spa, arc_buf_hdr_t *hdr, uint64_t asize,
9142     abd_t **abd_out)
9143 {
9144 	int ret;
9145 	abd_t *cabd = NULL, *eabd = NULL, *to_write = hdr->b_l1hdr.b_pabd;
9146 	enum zio_compress compress = HDR_GET_COMPRESS(hdr);
9147 	uint64_t psize = HDR_GET_PSIZE(hdr);
9148 	uint64_t size = arc_hdr_size(hdr);
9149 	boolean_t ismd = HDR_ISTYPE_METADATA(hdr);
9150 	boolean_t bswap = (hdr->b_l1hdr.b_byteswap != DMU_BSWAP_NUMFUNCS);
9151 	dsl_crypto_key_t *dck = NULL;
9152 	uint8_t mac[ZIO_DATA_MAC_LEN] = { 0 };
9153 	boolean_t no_crypt = B_FALSE;
9154 
9155 	ASSERT((HDR_GET_COMPRESS(hdr) != ZIO_COMPRESS_OFF &&
9156 	    !HDR_COMPRESSION_ENABLED(hdr)) ||
9157 	    HDR_ENCRYPTED(hdr) || HDR_SHARED_DATA(hdr) || psize != asize);
9158 	ASSERT3U(psize, <=, asize);
9159 
9160 	/*
9161 	 * If this data simply needs its own buffer, we simply allocate it
9162 	 * and copy the data. This may be done to eliminate a dependency on a
9163 	 * shared buffer or to reallocate the buffer to match asize.
9164 	 */
9165 	if (HDR_HAS_RABD(hdr)) {
9166 		ASSERT3U(asize, >, psize);
9167 		to_write = abd_alloc_for_io(asize, ismd);
9168 		abd_copy(to_write, hdr->b_crypt_hdr.b_rabd, psize);
9169 		abd_zero_off(to_write, psize, asize - psize);
9170 		goto out;
9171 	}
9172 
9173 	if ((compress == ZIO_COMPRESS_OFF || HDR_COMPRESSION_ENABLED(hdr)) &&
9174 	    !HDR_ENCRYPTED(hdr)) {
9175 		ASSERT3U(size, ==, psize);
9176 		to_write = abd_alloc_for_io(asize, ismd);
9177 		abd_copy(to_write, hdr->b_l1hdr.b_pabd, size);
9178 		if (asize > size)
9179 			abd_zero_off(to_write, size, asize - size);
9180 		goto out;
9181 	}
9182 
9183 	if (compress != ZIO_COMPRESS_OFF && !HDR_COMPRESSION_ENABLED(hdr)) {
9184 		cabd = abd_alloc_for_io(MAX(size, asize), ismd);
9185 		uint64_t csize = zio_compress_data(compress, to_write, &cabd,
9186 		    size, MIN(size, psize), hdr->b_complevel);
9187 		if (csize >= size || csize > psize) {
9188 			/*
9189 			 * We can't re-compress the block into the original
9190 			 * psize.  Even if it fits into asize, it does not
9191 			 * matter, since checksum will never match on read.
9192 			 */
9193 			abd_free(cabd);
9194 			return (SET_ERROR(EIO));
9195 		}
9196 		if (asize > csize)
9197 			abd_zero_off(cabd, csize, asize - csize);
9198 		to_write = cabd;
9199 	}
9200 
9201 	if (HDR_ENCRYPTED(hdr)) {
9202 		eabd = abd_alloc_for_io(asize, ismd);
9203 
9204 		/*
9205 		 * If the dataset was disowned before the buffer
9206 		 * made it to this point, the key to re-encrypt
9207 		 * it won't be available. In this case we simply
9208 		 * won't write the buffer to the L2ARC.
9209 		 */
9210 		ret = spa_keystore_lookup_key(spa, hdr->b_crypt_hdr.b_dsobj,
9211 		    FTAG, &dck);
9212 		if (ret != 0)
9213 			goto error;
9214 
9215 		ret = zio_do_crypt_abd(B_TRUE, &dck->dck_key,
9216 		    hdr->b_crypt_hdr.b_ot, bswap, hdr->b_crypt_hdr.b_salt,
9217 		    hdr->b_crypt_hdr.b_iv, mac, psize, to_write, eabd,
9218 		    &no_crypt);
9219 		if (ret != 0)
9220 			goto error;
9221 
9222 		if (no_crypt)
9223 			abd_copy(eabd, to_write, psize);
9224 
9225 		if (psize != asize)
9226 			abd_zero_off(eabd, psize, asize - psize);
9227 
9228 		/* assert that the MAC we got here matches the one we saved */
9229 		ASSERT0(memcmp(mac, hdr->b_crypt_hdr.b_mac, ZIO_DATA_MAC_LEN));
9230 		spa_keystore_dsl_key_rele(spa, dck, FTAG);
9231 
9232 		if (to_write == cabd)
9233 			abd_free(cabd);
9234 
9235 		to_write = eabd;
9236 	}
9237 
9238 out:
9239 	ASSERT3P(to_write, !=, hdr->b_l1hdr.b_pabd);
9240 	*abd_out = to_write;
9241 	return (0);
9242 
9243 error:
9244 	if (dck != NULL)
9245 		spa_keystore_dsl_key_rele(spa, dck, FTAG);
9246 	if (cabd != NULL)
9247 		abd_free(cabd);
9248 	if (eabd != NULL)
9249 		abd_free(eabd);
9250 
9251 	*abd_out = NULL;
9252 	return (ret);
9253 }
9254 
9255 static void
l2arc_blk_fetch_done(zio_t * zio)9256 l2arc_blk_fetch_done(zio_t *zio)
9257 {
9258 	l2arc_read_callback_t *cb;
9259 
9260 	cb = zio->io_private;
9261 	if (cb->l2rcb_abd != NULL)
9262 		abd_free(cb->l2rcb_abd);
9263 	kmem_free(cb, sizeof (l2arc_read_callback_t));
9264 }
9265 
9266 /*
9267  * Find and write ARC buffers to the L2ARC device.
9268  *
9269  * An ARC_FLAG_L2_WRITING flag is set so that the L2ARC buffers are not valid
9270  * for reading until they have completed writing.
9271  * The headroom_boost is an in-out parameter used to maintain headroom boost
9272  * state between calls to this function.
9273  *
9274  * Returns the number of bytes actually written (which may be smaller than
9275  * the delta by which the device hand has changed due to alignment and the
9276  * writing of log blocks).
9277  */
9278 static uint64_t
l2arc_write_buffers(spa_t * spa,l2arc_dev_t * dev,uint64_t target_sz)9279 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
9280 {
9281 	arc_buf_hdr_t 		*hdr, *head, *marker;
9282 	uint64_t 		write_asize, write_psize, headroom;
9283 	boolean_t		full, from_head = !arc_warm;
9284 	l2arc_write_callback_t	*cb = NULL;
9285 	zio_t 			*pio, *wzio;
9286 	uint64_t 		guid = spa_load_guid(spa);
9287 	l2arc_dev_hdr_phys_t	*l2dhdr = dev->l2ad_dev_hdr;
9288 
9289 	ASSERT3P(dev->l2ad_vdev, !=, NULL);
9290 
9291 	pio = NULL;
9292 	write_asize = write_psize = 0;
9293 	full = B_FALSE;
9294 	head = kmem_cache_alloc(hdr_l2only_cache, KM_PUSHPAGE);
9295 	arc_hdr_set_flags(head, ARC_FLAG_L2_WRITE_HEAD | ARC_FLAG_HAS_L2HDR);
9296 	marker = arc_state_alloc_marker();
9297 
9298 	/*
9299 	 * Copy buffers for L2ARC writing.
9300 	 */
9301 	for (int pass = 0; pass < L2ARC_FEED_TYPES; pass++) {
9302 		/*
9303 		 * pass == 0: MFU meta
9304 		 * pass == 1: MRU meta
9305 		 * pass == 2: MFU data
9306 		 * pass == 3: MRU data
9307 		 */
9308 		if (l2arc_mfuonly == 1) {
9309 			if (pass == 1 || pass == 3)
9310 				continue;
9311 		} else if (l2arc_mfuonly > 1) {
9312 			if (pass == 3)
9313 				continue;
9314 		}
9315 
9316 		uint64_t passed_sz = 0;
9317 		headroom = target_sz * l2arc_headroom;
9318 		if (zfs_compressed_arc_enabled)
9319 			headroom = (headroom * l2arc_headroom_boost) / 100;
9320 
9321 		/*
9322 		 * Until the ARC is warm and starts to evict, read from the
9323 		 * head of the ARC lists rather than the tail.
9324 		 */
9325 		multilist_sublist_t *mls = l2arc_sublist_lock(pass);
9326 		ASSERT3P(mls, !=, NULL);
9327 		if (from_head)
9328 			hdr = multilist_sublist_head(mls);
9329 		else
9330 			hdr = multilist_sublist_tail(mls);
9331 
9332 		while (hdr != NULL) {
9333 			kmutex_t *hash_lock;
9334 			abd_t *to_write = NULL;
9335 
9336 			hash_lock = HDR_LOCK(hdr);
9337 			if (!mutex_tryenter(hash_lock)) {
9338 skip:
9339 				/* Skip this buffer rather than waiting. */
9340 				if (from_head)
9341 					hdr = multilist_sublist_next(mls, hdr);
9342 				else
9343 					hdr = multilist_sublist_prev(mls, hdr);
9344 				continue;
9345 			}
9346 
9347 			passed_sz += HDR_GET_LSIZE(hdr);
9348 			if (l2arc_headroom != 0 && passed_sz > headroom) {
9349 				/*
9350 				 * Searched too far.
9351 				 */
9352 				mutex_exit(hash_lock);
9353 				break;
9354 			}
9355 
9356 			if (!l2arc_write_eligible(guid, hdr)) {
9357 				mutex_exit(hash_lock);
9358 				goto skip;
9359 			}
9360 
9361 			ASSERT(HDR_HAS_L1HDR(hdr));
9362 			ASSERT3U(HDR_GET_PSIZE(hdr), >, 0);
9363 			ASSERT3U(arc_hdr_size(hdr), >, 0);
9364 			ASSERT(hdr->b_l1hdr.b_pabd != NULL ||
9365 			    HDR_HAS_RABD(hdr));
9366 			uint64_t psize = HDR_GET_PSIZE(hdr);
9367 			uint64_t asize = vdev_psize_to_asize(dev->l2ad_vdev,
9368 			    psize);
9369 
9370 			/*
9371 			 * If the allocated size of this buffer plus the max
9372 			 * size for the pending log block exceeds the evicted
9373 			 * target size, terminate writing buffers for this run.
9374 			 */
9375 			if (write_asize + asize +
9376 			    sizeof (l2arc_log_blk_phys_t) > target_sz) {
9377 				full = B_TRUE;
9378 				mutex_exit(hash_lock);
9379 				break;
9380 			}
9381 
9382 			/*
9383 			 * We should not sleep with sublist lock held or it
9384 			 * may block ARC eviction.  Insert a marker to save
9385 			 * the position and drop the lock.
9386 			 */
9387 			if (from_head) {
9388 				multilist_sublist_insert_after(mls, hdr,
9389 				    marker);
9390 			} else {
9391 				multilist_sublist_insert_before(mls, hdr,
9392 				    marker);
9393 			}
9394 			multilist_sublist_unlock(mls);
9395 
9396 			/*
9397 			 * If this header has b_rabd, we can use this since it
9398 			 * must always match the data exactly as it exists on
9399 			 * disk. Otherwise, the L2ARC can normally use the
9400 			 * hdr's data, but if we're sharing data between the
9401 			 * hdr and one of its bufs, L2ARC needs its own copy of
9402 			 * the data so that the ZIO below can't race with the
9403 			 * buf consumer. To ensure that this copy will be
9404 			 * available for the lifetime of the ZIO and be cleaned
9405 			 * up afterwards, we add it to the l2arc_free_on_write
9406 			 * queue. If we need to apply any transforms to the
9407 			 * data (compression, encryption) we will also need the
9408 			 * extra buffer.
9409 			 */
9410 			if (HDR_HAS_RABD(hdr) && psize == asize) {
9411 				to_write = hdr->b_crypt_hdr.b_rabd;
9412 			} else if ((HDR_COMPRESSION_ENABLED(hdr) ||
9413 			    HDR_GET_COMPRESS(hdr) == ZIO_COMPRESS_OFF) &&
9414 			    !HDR_ENCRYPTED(hdr) && !HDR_SHARED_DATA(hdr) &&
9415 			    psize == asize) {
9416 				to_write = hdr->b_l1hdr.b_pabd;
9417 			} else {
9418 				int ret;
9419 				arc_buf_contents_t type = arc_buf_type(hdr);
9420 
9421 				ret = l2arc_apply_transforms(spa, hdr, asize,
9422 				    &to_write);
9423 				if (ret != 0) {
9424 					arc_hdr_clear_flags(hdr,
9425 					    ARC_FLAG_L2CACHE);
9426 					mutex_exit(hash_lock);
9427 					goto next;
9428 				}
9429 
9430 				l2arc_free_abd_on_write(to_write, asize, type);
9431 			}
9432 
9433 			hdr->b_l2hdr.b_dev = dev;
9434 			hdr->b_l2hdr.b_daddr = dev->l2ad_hand;
9435 			hdr->b_l2hdr.b_hits = 0;
9436 			hdr->b_l2hdr.b_arcs_state =
9437 			    hdr->b_l1hdr.b_state->arcs_state;
9438 			/* l2arc_hdr_arcstats_update() expects a valid asize */
9439 			HDR_SET_L2SIZE(hdr, asize);
9440 			arc_hdr_set_flags(hdr, ARC_FLAG_HAS_L2HDR |
9441 			    ARC_FLAG_L2_WRITING);
9442 
9443 			(void) zfs_refcount_add_many(&dev->l2ad_alloc,
9444 			    arc_hdr_size(hdr), hdr);
9445 			l2arc_hdr_arcstats_increment(hdr);
9446 			vdev_space_update(dev->l2ad_vdev, asize, 0, 0);
9447 
9448 			mutex_enter(&dev->l2ad_mtx);
9449 			if (pio == NULL) {
9450 				/*
9451 				 * Insert a dummy header on the buflist so
9452 				 * l2arc_write_done() can find where the
9453 				 * write buffers begin without searching.
9454 				 */
9455 				list_insert_head(&dev->l2ad_buflist, head);
9456 			}
9457 			list_insert_head(&dev->l2ad_buflist, hdr);
9458 			mutex_exit(&dev->l2ad_mtx);
9459 
9460 			boolean_t commit = l2arc_log_blk_insert(dev, hdr);
9461 			mutex_exit(hash_lock);
9462 
9463 			if (pio == NULL) {
9464 				cb = kmem_alloc(
9465 				    sizeof (l2arc_write_callback_t), KM_SLEEP);
9466 				cb->l2wcb_dev = dev;
9467 				cb->l2wcb_head = head;
9468 				list_create(&cb->l2wcb_abd_list,
9469 				    sizeof (l2arc_lb_abd_buf_t),
9470 				    offsetof(l2arc_lb_abd_buf_t, node));
9471 				pio = zio_root(spa, l2arc_write_done, cb,
9472 				    ZIO_FLAG_CANFAIL);
9473 			}
9474 
9475 			wzio = zio_write_phys(pio, dev->l2ad_vdev,
9476 			    dev->l2ad_hand, asize, to_write,
9477 			    ZIO_CHECKSUM_OFF, NULL, hdr,
9478 			    ZIO_PRIORITY_ASYNC_WRITE,
9479 			    ZIO_FLAG_CANFAIL, B_FALSE);
9480 
9481 			DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
9482 			    zio_t *, wzio);
9483 			zio_nowait(wzio);
9484 
9485 			write_psize += psize;
9486 			write_asize += asize;
9487 			dev->l2ad_hand += asize;
9488 
9489 			if (commit) {
9490 				/* l2ad_hand will be adjusted inside. */
9491 				write_asize +=
9492 				    l2arc_log_blk_commit(dev, pio, cb);
9493 			}
9494 
9495 next:
9496 			multilist_sublist_lock(mls);
9497 			if (from_head)
9498 				hdr = multilist_sublist_next(mls, marker);
9499 			else
9500 				hdr = multilist_sublist_prev(mls, marker);
9501 			multilist_sublist_remove(mls, marker);
9502 		}
9503 
9504 		multilist_sublist_unlock(mls);
9505 
9506 		if (full == B_TRUE)
9507 			break;
9508 	}
9509 
9510 	arc_state_free_marker(marker);
9511 
9512 	/* No buffers selected for writing? */
9513 	if (pio == NULL) {
9514 		ASSERT0(write_psize);
9515 		ASSERT(!HDR_HAS_L1HDR(head));
9516 		kmem_cache_free(hdr_l2only_cache, head);
9517 
9518 		/*
9519 		 * Although we did not write any buffers l2ad_evict may
9520 		 * have advanced.
9521 		 */
9522 		if (dev->l2ad_evict != l2dhdr->dh_evict)
9523 			l2arc_dev_hdr_update(dev);
9524 
9525 		return (0);
9526 	}
9527 
9528 	if (!dev->l2ad_first)
9529 		ASSERT3U(dev->l2ad_hand, <=, dev->l2ad_evict);
9530 
9531 	ASSERT3U(write_asize, <=, target_sz);
9532 	ARCSTAT_BUMP(arcstat_l2_writes_sent);
9533 	ARCSTAT_INCR(arcstat_l2_write_bytes, write_psize);
9534 
9535 	dev->l2ad_writing = B_TRUE;
9536 	(void) zio_wait(pio);
9537 	dev->l2ad_writing = B_FALSE;
9538 
9539 	/*
9540 	 * Update the device header after the zio completes as
9541 	 * l2arc_write_done() may have updated the memory holding the log block
9542 	 * pointers in the device header.
9543 	 */
9544 	l2arc_dev_hdr_update(dev);
9545 
9546 	return (write_asize);
9547 }
9548 
9549 static boolean_t
l2arc_hdr_limit_reached(void)9550 l2arc_hdr_limit_reached(void)
9551 {
9552 	int64_t s = aggsum_upper_bound(&arc_sums.arcstat_l2_hdr_size);
9553 
9554 	return (arc_reclaim_needed() ||
9555 	    (s > (arc_warm ? arc_c : arc_c_max) * l2arc_meta_percent / 100));
9556 }
9557 
9558 /*
9559  * This thread feeds the L2ARC at regular intervals.  This is the beating
9560  * heart of the L2ARC.
9561  */
9562 static  __attribute__((noreturn)) void
l2arc_feed_thread(void * unused)9563 l2arc_feed_thread(void *unused)
9564 {
9565 	(void) unused;
9566 	callb_cpr_t cpr;
9567 	l2arc_dev_t *dev;
9568 	spa_t *spa;
9569 	uint64_t size, wrote;
9570 	clock_t begin, next = ddi_get_lbolt();
9571 	fstrans_cookie_t cookie;
9572 
9573 	CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
9574 
9575 	mutex_enter(&l2arc_feed_thr_lock);
9576 
9577 	cookie = spl_fstrans_mark();
9578 	while (l2arc_thread_exit == 0) {
9579 		CALLB_CPR_SAFE_BEGIN(&cpr);
9580 		(void) cv_timedwait_idle(&l2arc_feed_thr_cv,
9581 		    &l2arc_feed_thr_lock, next);
9582 		CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
9583 		next = ddi_get_lbolt() + hz;
9584 
9585 		/*
9586 		 * Quick check for L2ARC devices.
9587 		 */
9588 		mutex_enter(&l2arc_dev_mtx);
9589 		if (l2arc_ndev == 0) {
9590 			mutex_exit(&l2arc_dev_mtx);
9591 			continue;
9592 		}
9593 		mutex_exit(&l2arc_dev_mtx);
9594 		begin = ddi_get_lbolt();
9595 
9596 		/*
9597 		 * This selects the next l2arc device to write to, and in
9598 		 * doing so the next spa to feed from: dev->l2ad_spa.   This
9599 		 * will return NULL if there are now no l2arc devices or if
9600 		 * they are all faulted.
9601 		 *
9602 		 * If a device is returned, its spa's config lock is also
9603 		 * held to prevent device removal.  l2arc_dev_get_next()
9604 		 * will grab and release l2arc_dev_mtx.
9605 		 */
9606 		if ((dev = l2arc_dev_get_next()) == NULL)
9607 			continue;
9608 
9609 		spa = dev->l2ad_spa;
9610 		ASSERT3P(spa, !=, NULL);
9611 
9612 		/*
9613 		 * If the pool is read-only then force the feed thread to
9614 		 * sleep a little longer.
9615 		 */
9616 		if (!spa_writeable(spa)) {
9617 			next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
9618 			spa_config_exit(spa, SCL_L2ARC, dev);
9619 			continue;
9620 		}
9621 
9622 		/*
9623 		 * Avoid contributing to memory pressure.
9624 		 */
9625 		if (l2arc_hdr_limit_reached()) {
9626 			ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
9627 			spa_config_exit(spa, SCL_L2ARC, dev);
9628 			continue;
9629 		}
9630 
9631 		ARCSTAT_BUMP(arcstat_l2_feeds);
9632 
9633 		size = l2arc_write_size(dev);
9634 
9635 		/*
9636 		 * Evict L2ARC buffers that will be overwritten.
9637 		 */
9638 		l2arc_evict(dev, size, B_FALSE);
9639 
9640 		/*
9641 		 * Write ARC buffers.
9642 		 */
9643 		wrote = l2arc_write_buffers(spa, dev, size);
9644 
9645 		/*
9646 		 * Calculate interval between writes.
9647 		 */
9648 		next = l2arc_write_interval(begin, size, wrote);
9649 		spa_config_exit(spa, SCL_L2ARC, dev);
9650 	}
9651 	spl_fstrans_unmark(cookie);
9652 
9653 	l2arc_thread_exit = 0;
9654 	cv_broadcast(&l2arc_feed_thr_cv);
9655 	CALLB_CPR_EXIT(&cpr);		/* drops l2arc_feed_thr_lock */
9656 	thread_exit();
9657 }
9658 
9659 boolean_t
l2arc_vdev_present(vdev_t * vd)9660 l2arc_vdev_present(vdev_t *vd)
9661 {
9662 	return (l2arc_vdev_get(vd) != NULL);
9663 }
9664 
9665 /*
9666  * Returns the l2arc_dev_t associated with a particular vdev_t or NULL if
9667  * the vdev_t isn't an L2ARC device.
9668  */
9669 l2arc_dev_t *
l2arc_vdev_get(vdev_t * vd)9670 l2arc_vdev_get(vdev_t *vd)
9671 {
9672 	l2arc_dev_t	*dev;
9673 
9674 	mutex_enter(&l2arc_dev_mtx);
9675 	for (dev = list_head(l2arc_dev_list); dev != NULL;
9676 	    dev = list_next(l2arc_dev_list, dev)) {
9677 		if (dev->l2ad_vdev == vd)
9678 			break;
9679 	}
9680 	mutex_exit(&l2arc_dev_mtx);
9681 
9682 	return (dev);
9683 }
9684 
9685 static void
l2arc_rebuild_dev(l2arc_dev_t * dev,boolean_t reopen)9686 l2arc_rebuild_dev(l2arc_dev_t *dev, boolean_t reopen)
9687 {
9688 	l2arc_dev_hdr_phys_t *l2dhdr = dev->l2ad_dev_hdr;
9689 	uint64_t l2dhdr_asize = dev->l2ad_dev_hdr_asize;
9690 	spa_t *spa = dev->l2ad_spa;
9691 
9692 	/*
9693 	 * After a l2arc_remove_vdev(), the spa_t will no longer be valid
9694 	 */
9695 	if (spa == NULL)
9696 		return;
9697 
9698 	/*
9699 	 * The L2ARC has to hold at least the payload of one log block for
9700 	 * them to be restored (persistent L2ARC). The payload of a log block
9701 	 * depends on the amount of its log entries. We always write log blocks
9702 	 * with 1022 entries. How many of them are committed or restored depends
9703 	 * on the size of the L2ARC device. Thus the maximum payload of
9704 	 * one log block is 1022 * SPA_MAXBLOCKSIZE = 16GB. If the L2ARC device
9705 	 * is less than that, we reduce the amount of committed and restored
9706 	 * log entries per block so as to enable persistence.
9707 	 */
9708 	if (dev->l2ad_end < l2arc_rebuild_blocks_min_l2size) {
9709 		dev->l2ad_log_entries = 0;
9710 	} else {
9711 		dev->l2ad_log_entries = MIN((dev->l2ad_end -
9712 		    dev->l2ad_start) >> SPA_MAXBLOCKSHIFT,
9713 		    L2ARC_LOG_BLK_MAX_ENTRIES);
9714 	}
9715 
9716 	/*
9717 	 * Read the device header, if an error is returned do not rebuild L2ARC.
9718 	 */
9719 	if (l2arc_dev_hdr_read(dev) == 0 && dev->l2ad_log_entries > 0) {
9720 		/*
9721 		 * If we are onlining a cache device (vdev_reopen) that was
9722 		 * still present (l2arc_vdev_present()) and rebuild is enabled,
9723 		 * we should evict all ARC buffers and pointers to log blocks
9724 		 * and reclaim their space before restoring its contents to
9725 		 * L2ARC.
9726 		 */
9727 		if (reopen) {
9728 			if (!l2arc_rebuild_enabled) {
9729 				return;
9730 			} else {
9731 				l2arc_evict(dev, 0, B_TRUE);
9732 				/* start a new log block */
9733 				dev->l2ad_log_ent_idx = 0;
9734 				dev->l2ad_log_blk_payload_asize = 0;
9735 				dev->l2ad_log_blk_payload_start = 0;
9736 			}
9737 		}
9738 		/*
9739 		 * Just mark the device as pending for a rebuild. We won't
9740 		 * be starting a rebuild in line here as it would block pool
9741 		 * import. Instead spa_load_impl will hand that off to an
9742 		 * async task which will call l2arc_spa_rebuild_start.
9743 		 */
9744 		dev->l2ad_rebuild = B_TRUE;
9745 	} else if (spa_writeable(spa)) {
9746 		/*
9747 		 * In this case TRIM the whole device if l2arc_trim_ahead > 0,
9748 		 * otherwise create a new header. We zero out the memory holding
9749 		 * the header to reset dh_start_lbps. If we TRIM the whole
9750 		 * device the new header will be written by
9751 		 * vdev_trim_l2arc_thread() at the end of the TRIM to update the
9752 		 * trim_state in the header too. When reading the header, if
9753 		 * trim_state is not VDEV_TRIM_COMPLETE and l2arc_trim_ahead > 0
9754 		 * we opt to TRIM the whole device again.
9755 		 */
9756 		if (l2arc_trim_ahead > 0) {
9757 			dev->l2ad_trim_all = B_TRUE;
9758 		} else {
9759 			memset(l2dhdr, 0, l2dhdr_asize);
9760 			l2arc_dev_hdr_update(dev);
9761 		}
9762 	}
9763 }
9764 
9765 /*
9766  * Add a vdev for use by the L2ARC.  By this point the spa has already
9767  * validated the vdev and opened it.
9768  */
9769 void
l2arc_add_vdev(spa_t * spa,vdev_t * vd)9770 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
9771 {
9772 	l2arc_dev_t		*adddev;
9773 	uint64_t		l2dhdr_asize;
9774 
9775 	ASSERT(!l2arc_vdev_present(vd));
9776 
9777 	/*
9778 	 * Create a new l2arc device entry.
9779 	 */
9780 	adddev = vmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
9781 	adddev->l2ad_spa = spa;
9782 	adddev->l2ad_vdev = vd;
9783 	/* leave extra size for an l2arc device header */
9784 	l2dhdr_asize = adddev->l2ad_dev_hdr_asize =
9785 	    MAX(sizeof (*adddev->l2ad_dev_hdr), 1 << vd->vdev_ashift);
9786 	adddev->l2ad_start = VDEV_LABEL_START_SIZE + l2dhdr_asize;
9787 	adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
9788 	ASSERT3U(adddev->l2ad_start, <, adddev->l2ad_end);
9789 	adddev->l2ad_hand = adddev->l2ad_start;
9790 	adddev->l2ad_evict = adddev->l2ad_start;
9791 	adddev->l2ad_first = B_TRUE;
9792 	adddev->l2ad_writing = B_FALSE;
9793 	adddev->l2ad_trim_all = B_FALSE;
9794 	list_link_init(&adddev->l2ad_node);
9795 	adddev->l2ad_dev_hdr = kmem_zalloc(l2dhdr_asize, KM_SLEEP);
9796 
9797 	mutex_init(&adddev->l2ad_mtx, NULL, MUTEX_DEFAULT, NULL);
9798 	/*
9799 	 * This is a list of all ARC buffers that are still valid on the
9800 	 * device.
9801 	 */
9802 	list_create(&adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
9803 	    offsetof(arc_buf_hdr_t, b_l2hdr.b_l2node));
9804 
9805 	/*
9806 	 * This is a list of pointers to log blocks that are still present
9807 	 * on the device.
9808 	 */
9809 	list_create(&adddev->l2ad_lbptr_list, sizeof (l2arc_lb_ptr_buf_t),
9810 	    offsetof(l2arc_lb_ptr_buf_t, node));
9811 
9812 	vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
9813 	zfs_refcount_create(&adddev->l2ad_alloc);
9814 	zfs_refcount_create(&adddev->l2ad_lb_asize);
9815 	zfs_refcount_create(&adddev->l2ad_lb_count);
9816 
9817 	/*
9818 	 * Decide if dev is eligible for L2ARC rebuild or whole device
9819 	 * trimming. This has to happen before the device is added in the
9820 	 * cache device list and l2arc_dev_mtx is released. Otherwise
9821 	 * l2arc_feed_thread() might already start writing on the
9822 	 * device.
9823 	 */
9824 	l2arc_rebuild_dev(adddev, B_FALSE);
9825 
9826 	/*
9827 	 * Add device to global list
9828 	 */
9829 	mutex_enter(&l2arc_dev_mtx);
9830 	list_insert_head(l2arc_dev_list, adddev);
9831 	atomic_inc_64(&l2arc_ndev);
9832 	mutex_exit(&l2arc_dev_mtx);
9833 }
9834 
9835 /*
9836  * Decide if a vdev is eligible for L2ARC rebuild, called from vdev_reopen()
9837  * in case of onlining a cache device.
9838  */
9839 void
l2arc_rebuild_vdev(vdev_t * vd,boolean_t reopen)9840 l2arc_rebuild_vdev(vdev_t *vd, boolean_t reopen)
9841 {
9842 	l2arc_dev_t		*dev = NULL;
9843 
9844 	dev = l2arc_vdev_get(vd);
9845 	ASSERT3P(dev, !=, NULL);
9846 
9847 	/*
9848 	 * In contrast to l2arc_add_vdev() we do not have to worry about
9849 	 * l2arc_feed_thread() invalidating previous content when onlining a
9850 	 * cache device. The device parameters (l2ad*) are not cleared when
9851 	 * offlining the device and writing new buffers will not invalidate
9852 	 * all previous content. In worst case only buffers that have not had
9853 	 * their log block written to the device will be lost.
9854 	 * When onlining the cache device (ie offline->online without exporting
9855 	 * the pool in between) this happens:
9856 	 * vdev_reopen() -> vdev_open() -> l2arc_rebuild_vdev()
9857 	 * 			|			|
9858 	 * 		vdev_is_dead() = B_FALSE	l2ad_rebuild = B_TRUE
9859 	 * During the time where vdev_is_dead = B_FALSE and until l2ad_rebuild
9860 	 * is set to B_TRUE we might write additional buffers to the device.
9861 	 */
9862 	l2arc_rebuild_dev(dev, reopen);
9863 }
9864 
9865 typedef struct {
9866 	l2arc_dev_t	*rva_l2arc_dev;
9867 	uint64_t	rva_spa_gid;
9868 	uint64_t	rva_vdev_gid;
9869 	boolean_t	rva_async;
9870 
9871 } remove_vdev_args_t;
9872 
9873 static void
l2arc_device_teardown(void * arg)9874 l2arc_device_teardown(void *arg)
9875 {
9876 	remove_vdev_args_t *rva = arg;
9877 	l2arc_dev_t *remdev = rva->rva_l2arc_dev;
9878 	hrtime_t start_time = gethrtime();
9879 
9880 	/*
9881 	 * Clear all buflists and ARC references.  L2ARC device flush.
9882 	 */
9883 	l2arc_evict(remdev, 0, B_TRUE);
9884 	list_destroy(&remdev->l2ad_buflist);
9885 	ASSERT(list_is_empty(&remdev->l2ad_lbptr_list));
9886 	list_destroy(&remdev->l2ad_lbptr_list);
9887 	mutex_destroy(&remdev->l2ad_mtx);
9888 	zfs_refcount_destroy(&remdev->l2ad_alloc);
9889 	zfs_refcount_destroy(&remdev->l2ad_lb_asize);
9890 	zfs_refcount_destroy(&remdev->l2ad_lb_count);
9891 	kmem_free(remdev->l2ad_dev_hdr, remdev->l2ad_dev_hdr_asize);
9892 	vmem_free(remdev, sizeof (l2arc_dev_t));
9893 
9894 	uint64_t elaspsed = NSEC2MSEC(gethrtime() - start_time);
9895 	if (elaspsed > 0) {
9896 		zfs_dbgmsg("spa %llu, vdev %llu removed in %llu ms",
9897 		    (u_longlong_t)rva->rva_spa_gid,
9898 		    (u_longlong_t)rva->rva_vdev_gid,
9899 		    (u_longlong_t)elaspsed);
9900 	}
9901 
9902 	if (rva->rva_async)
9903 		arc_async_flush_remove(rva->rva_spa_gid, 2);
9904 	kmem_free(rva, sizeof (remove_vdev_args_t));
9905 }
9906 
9907 /*
9908  * Remove a vdev from the L2ARC.
9909  */
9910 void
l2arc_remove_vdev(vdev_t * vd)9911 l2arc_remove_vdev(vdev_t *vd)
9912 {
9913 	spa_t *spa = vd->vdev_spa;
9914 	boolean_t asynchronous = spa->spa_state == POOL_STATE_EXPORTED ||
9915 	    spa->spa_state == POOL_STATE_DESTROYED;
9916 
9917 	/*
9918 	 * Find the device by vdev
9919 	 */
9920 	l2arc_dev_t *remdev = l2arc_vdev_get(vd);
9921 	ASSERT3P(remdev, !=, NULL);
9922 
9923 	/*
9924 	 * Save info for final teardown
9925 	 */
9926 	remove_vdev_args_t *rva = kmem_alloc(sizeof (remove_vdev_args_t),
9927 	    KM_SLEEP);
9928 	rva->rva_l2arc_dev = remdev;
9929 	rva->rva_spa_gid = spa_load_guid(spa);
9930 	rva->rva_vdev_gid = remdev->l2ad_vdev->vdev_guid;
9931 
9932 	/*
9933 	 * Cancel any ongoing or scheduled rebuild.
9934 	 */
9935 	mutex_enter(&l2arc_rebuild_thr_lock);
9936 	remdev->l2ad_rebuild_cancel = B_TRUE;
9937 	if (remdev->l2ad_rebuild_began == B_TRUE) {
9938 		while (remdev->l2ad_rebuild == B_TRUE)
9939 			cv_wait(&l2arc_rebuild_thr_cv, &l2arc_rebuild_thr_lock);
9940 	}
9941 	mutex_exit(&l2arc_rebuild_thr_lock);
9942 	rva->rva_async = asynchronous;
9943 
9944 	/*
9945 	 * Remove device from global list
9946 	 */
9947 	ASSERT(spa_config_held(spa, SCL_L2ARC, RW_WRITER) & SCL_L2ARC);
9948 	mutex_enter(&l2arc_dev_mtx);
9949 	list_remove(l2arc_dev_list, remdev);
9950 	l2arc_dev_last = NULL;		/* may have been invalidated */
9951 	atomic_dec_64(&l2arc_ndev);
9952 
9953 	/* During a pool export spa & vdev will no longer be valid */
9954 	if (asynchronous) {
9955 		remdev->l2ad_spa = NULL;
9956 		remdev->l2ad_vdev = NULL;
9957 	}
9958 	mutex_exit(&l2arc_dev_mtx);
9959 
9960 	if (!asynchronous) {
9961 		l2arc_device_teardown(rva);
9962 		return;
9963 	}
9964 
9965 	arc_async_flush_t *af = arc_async_flush_add(rva->rva_spa_gid, 2);
9966 
9967 	taskq_dispatch_ent(arc_flush_taskq, l2arc_device_teardown, rva,
9968 	    TQ_SLEEP, &af->af_tqent);
9969 }
9970 
9971 void
l2arc_init(void)9972 l2arc_init(void)
9973 {
9974 	l2arc_thread_exit = 0;
9975 	l2arc_ndev = 0;
9976 
9977 	mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
9978 	cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
9979 	mutex_init(&l2arc_rebuild_thr_lock, NULL, MUTEX_DEFAULT, NULL);
9980 	cv_init(&l2arc_rebuild_thr_cv, NULL, CV_DEFAULT, NULL);
9981 	mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
9982 	mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
9983 
9984 	l2arc_dev_list = &L2ARC_dev_list;
9985 	l2arc_free_on_write = &L2ARC_free_on_write;
9986 	list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
9987 	    offsetof(l2arc_dev_t, l2ad_node));
9988 	list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
9989 	    offsetof(l2arc_data_free_t, l2df_list_node));
9990 }
9991 
9992 void
l2arc_fini(void)9993 l2arc_fini(void)
9994 {
9995 	mutex_destroy(&l2arc_feed_thr_lock);
9996 	cv_destroy(&l2arc_feed_thr_cv);
9997 	mutex_destroy(&l2arc_rebuild_thr_lock);
9998 	cv_destroy(&l2arc_rebuild_thr_cv);
9999 	mutex_destroy(&l2arc_dev_mtx);
10000 	mutex_destroy(&l2arc_free_on_write_mtx);
10001 
10002 	list_destroy(l2arc_dev_list);
10003 	list_destroy(l2arc_free_on_write);
10004 }
10005 
10006 void
l2arc_start(void)10007 l2arc_start(void)
10008 {
10009 	if (!(spa_mode_global & SPA_MODE_WRITE))
10010 		return;
10011 
10012 	(void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
10013 	    TS_RUN, defclsyspri);
10014 }
10015 
10016 void
l2arc_stop(void)10017 l2arc_stop(void)
10018 {
10019 	if (!(spa_mode_global & SPA_MODE_WRITE))
10020 		return;
10021 
10022 	mutex_enter(&l2arc_feed_thr_lock);
10023 	cv_signal(&l2arc_feed_thr_cv);	/* kick thread out of startup */
10024 	l2arc_thread_exit = 1;
10025 	while (l2arc_thread_exit != 0)
10026 		cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
10027 	mutex_exit(&l2arc_feed_thr_lock);
10028 }
10029 
10030 /*
10031  * Punches out rebuild threads for the L2ARC devices in a spa. This should
10032  * be called after pool import from the spa async thread, since starting
10033  * these threads directly from spa_import() will make them part of the
10034  * "zpool import" context and delay process exit (and thus pool import).
10035  */
10036 void
l2arc_spa_rebuild_start(spa_t * spa)10037 l2arc_spa_rebuild_start(spa_t *spa)
10038 {
10039 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
10040 
10041 	/*
10042 	 * Locate the spa's l2arc devices and kick off rebuild threads.
10043 	 */
10044 	for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
10045 		l2arc_dev_t *dev =
10046 		    l2arc_vdev_get(spa->spa_l2cache.sav_vdevs[i]);
10047 		if (dev == NULL) {
10048 			/* Don't attempt a rebuild if the vdev is UNAVAIL */
10049 			continue;
10050 		}
10051 		mutex_enter(&l2arc_rebuild_thr_lock);
10052 		if (dev->l2ad_rebuild && !dev->l2ad_rebuild_cancel) {
10053 			dev->l2ad_rebuild_began = B_TRUE;
10054 			(void) thread_create(NULL, 0, l2arc_dev_rebuild_thread,
10055 			    dev, 0, &p0, TS_RUN, minclsyspri);
10056 		}
10057 		mutex_exit(&l2arc_rebuild_thr_lock);
10058 	}
10059 }
10060 
10061 void
l2arc_spa_rebuild_stop(spa_t * spa)10062 l2arc_spa_rebuild_stop(spa_t *spa)
10063 {
10064 	ASSERT(MUTEX_HELD(&spa_namespace_lock) ||
10065 	    spa->spa_export_thread == curthread);
10066 
10067 	for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
10068 		l2arc_dev_t *dev =
10069 		    l2arc_vdev_get(spa->spa_l2cache.sav_vdevs[i]);
10070 		if (dev == NULL)
10071 			continue;
10072 		mutex_enter(&l2arc_rebuild_thr_lock);
10073 		dev->l2ad_rebuild_cancel = B_TRUE;
10074 		mutex_exit(&l2arc_rebuild_thr_lock);
10075 	}
10076 	for (int i = 0; i < spa->spa_l2cache.sav_count; i++) {
10077 		l2arc_dev_t *dev =
10078 		    l2arc_vdev_get(spa->spa_l2cache.sav_vdevs[i]);
10079 		if (dev == NULL)
10080 			continue;
10081 		mutex_enter(&l2arc_rebuild_thr_lock);
10082 		if (dev->l2ad_rebuild_began == B_TRUE) {
10083 			while (dev->l2ad_rebuild == B_TRUE) {
10084 				cv_wait(&l2arc_rebuild_thr_cv,
10085 				    &l2arc_rebuild_thr_lock);
10086 			}
10087 		}
10088 		mutex_exit(&l2arc_rebuild_thr_lock);
10089 	}
10090 }
10091 
10092 /*
10093  * Main entry point for L2ARC rebuilding.
10094  */
10095 static __attribute__((noreturn)) void
l2arc_dev_rebuild_thread(void * arg)10096 l2arc_dev_rebuild_thread(void *arg)
10097 {
10098 	l2arc_dev_t *dev = arg;
10099 
10100 	VERIFY(dev->l2ad_rebuild);
10101 	(void) l2arc_rebuild(dev);
10102 	mutex_enter(&l2arc_rebuild_thr_lock);
10103 	dev->l2ad_rebuild_began = B_FALSE;
10104 	dev->l2ad_rebuild = B_FALSE;
10105 	cv_signal(&l2arc_rebuild_thr_cv);
10106 	mutex_exit(&l2arc_rebuild_thr_lock);
10107 
10108 	thread_exit();
10109 }
10110 
10111 /*
10112  * This function implements the actual L2ARC metadata rebuild. It:
10113  * starts reading the log block chain and restores each block's contents
10114  * to memory (reconstructing arc_buf_hdr_t's).
10115  *
10116  * Operation stops under any of the following conditions:
10117  *
10118  * 1) We reach the end of the log block chain.
10119  * 2) We encounter *any* error condition (cksum errors, io errors)
10120  */
10121 static int
l2arc_rebuild(l2arc_dev_t * dev)10122 l2arc_rebuild(l2arc_dev_t *dev)
10123 {
10124 	vdev_t			*vd = dev->l2ad_vdev;
10125 	spa_t			*spa = vd->vdev_spa;
10126 	int			err = 0;
10127 	l2arc_dev_hdr_phys_t	*l2dhdr = dev->l2ad_dev_hdr;
10128 	l2arc_log_blk_phys_t	*this_lb, *next_lb;
10129 	zio_t			*this_io = NULL, *next_io = NULL;
10130 	l2arc_log_blkptr_t	lbps[2];
10131 	l2arc_lb_ptr_buf_t	*lb_ptr_buf;
10132 	boolean_t		lock_held;
10133 
10134 	this_lb = vmem_zalloc(sizeof (*this_lb), KM_SLEEP);
10135 	next_lb = vmem_zalloc(sizeof (*next_lb), KM_SLEEP);
10136 
10137 	/*
10138 	 * We prevent device removal while issuing reads to the device,
10139 	 * then during the rebuilding phases we drop this lock again so
10140 	 * that a spa_unload or device remove can be initiated - this is
10141 	 * safe, because the spa will signal us to stop before removing
10142 	 * our device and wait for us to stop.
10143 	 */
10144 	spa_config_enter(spa, SCL_L2ARC, vd, RW_READER);
10145 	lock_held = B_TRUE;
10146 
10147 	/*
10148 	 * Retrieve the persistent L2ARC device state.
10149 	 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10150 	 */
10151 	dev->l2ad_evict = MAX(l2dhdr->dh_evict, dev->l2ad_start);
10152 	dev->l2ad_hand = MAX(l2dhdr->dh_start_lbps[0].lbp_daddr +
10153 	    L2BLK_GET_PSIZE((&l2dhdr->dh_start_lbps[0])->lbp_prop),
10154 	    dev->l2ad_start);
10155 	dev->l2ad_first = !!(l2dhdr->dh_flags & L2ARC_DEV_HDR_EVICT_FIRST);
10156 
10157 	vd->vdev_trim_action_time = l2dhdr->dh_trim_action_time;
10158 	vd->vdev_trim_state = l2dhdr->dh_trim_state;
10159 
10160 	/*
10161 	 * In case the zfs module parameter l2arc_rebuild_enabled is false
10162 	 * we do not start the rebuild process.
10163 	 */
10164 	if (!l2arc_rebuild_enabled)
10165 		goto out;
10166 
10167 	/* Prepare the rebuild process */
10168 	memcpy(lbps, l2dhdr->dh_start_lbps, sizeof (lbps));
10169 
10170 	/* Start the rebuild process */
10171 	for (;;) {
10172 		if (!l2arc_log_blkptr_valid(dev, &lbps[0]))
10173 			break;
10174 
10175 		if ((err = l2arc_log_blk_read(dev, &lbps[0], &lbps[1],
10176 		    this_lb, next_lb, this_io, &next_io)) != 0)
10177 			goto out;
10178 
10179 		/*
10180 		 * Our memory pressure valve. If the system is running low
10181 		 * on memory, rather than swamping memory with new ARC buf
10182 		 * hdrs, we opt not to rebuild the L2ARC. At this point,
10183 		 * however, we have already set up our L2ARC dev to chain in
10184 		 * new metadata log blocks, so the user may choose to offline/
10185 		 * online the L2ARC dev at a later time (or re-import the pool)
10186 		 * to reconstruct it (when there's less memory pressure).
10187 		 */
10188 		if (l2arc_hdr_limit_reached()) {
10189 			ARCSTAT_BUMP(arcstat_l2_rebuild_abort_lowmem);
10190 			cmn_err(CE_NOTE, "System running low on memory, "
10191 			    "aborting L2ARC rebuild.");
10192 			err = SET_ERROR(ENOMEM);
10193 			goto out;
10194 		}
10195 
10196 		spa_config_exit(spa, SCL_L2ARC, vd);
10197 		lock_held = B_FALSE;
10198 
10199 		/*
10200 		 * Now that we know that the next_lb checks out alright, we
10201 		 * can start reconstruction from this log block.
10202 		 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10203 		 */
10204 		uint64_t asize = L2BLK_GET_PSIZE((&lbps[0])->lbp_prop);
10205 		l2arc_log_blk_restore(dev, this_lb, asize);
10206 
10207 		/*
10208 		 * log block restored, include its pointer in the list of
10209 		 * pointers to log blocks present in the L2ARC device.
10210 		 */
10211 		lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP);
10212 		lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t),
10213 		    KM_SLEEP);
10214 		memcpy(lb_ptr_buf->lb_ptr, &lbps[0],
10215 		    sizeof (l2arc_log_blkptr_t));
10216 		mutex_enter(&dev->l2ad_mtx);
10217 		list_insert_tail(&dev->l2ad_lbptr_list, lb_ptr_buf);
10218 		ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize);
10219 		ARCSTAT_BUMP(arcstat_l2_log_blk_count);
10220 		zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf);
10221 		zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf);
10222 		mutex_exit(&dev->l2ad_mtx);
10223 		vdev_space_update(vd, asize, 0, 0);
10224 
10225 		/*
10226 		 * Protection against loops of log blocks:
10227 		 *
10228 		 *				       l2ad_hand  l2ad_evict
10229 		 *                                         V	      V
10230 		 * l2ad_start |=======================================| l2ad_end
10231 		 *             -----|||----|||---|||----|||
10232 		 *                  (3)    (2)   (1)    (0)
10233 		 *             ---|||---|||----|||---|||
10234 		 *		  (7)   (6)    (5)   (4)
10235 		 *
10236 		 * In this situation the pointer of log block (4) passes
10237 		 * l2arc_log_blkptr_valid() but the log block should not be
10238 		 * restored as it is overwritten by the payload of log block
10239 		 * (0). Only log blocks (0)-(3) should be restored. We check
10240 		 * whether l2ad_evict lies in between the payload starting
10241 		 * offset of the next log block (lbps[1].lbp_payload_start)
10242 		 * and the payload starting offset of the present log block
10243 		 * (lbps[0].lbp_payload_start). If true and this isn't the
10244 		 * first pass, we are looping from the beginning and we should
10245 		 * stop.
10246 		 */
10247 		if (l2arc_range_check_overlap(lbps[1].lbp_payload_start,
10248 		    lbps[0].lbp_payload_start, dev->l2ad_evict) &&
10249 		    !dev->l2ad_first)
10250 			goto out;
10251 
10252 		kpreempt(KPREEMPT_SYNC);
10253 		for (;;) {
10254 			mutex_enter(&l2arc_rebuild_thr_lock);
10255 			if (dev->l2ad_rebuild_cancel) {
10256 				mutex_exit(&l2arc_rebuild_thr_lock);
10257 				err = SET_ERROR(ECANCELED);
10258 				goto out;
10259 			}
10260 			mutex_exit(&l2arc_rebuild_thr_lock);
10261 			if (spa_config_tryenter(spa, SCL_L2ARC, vd,
10262 			    RW_READER)) {
10263 				lock_held = B_TRUE;
10264 				break;
10265 			}
10266 			/*
10267 			 * L2ARC config lock held by somebody in writer,
10268 			 * possibly due to them trying to remove us. They'll
10269 			 * likely to want us to shut down, so after a little
10270 			 * delay, we check l2ad_rebuild_cancel and retry
10271 			 * the lock again.
10272 			 */
10273 			delay(1);
10274 		}
10275 
10276 		/*
10277 		 * Continue with the next log block.
10278 		 */
10279 		lbps[0] = lbps[1];
10280 		lbps[1] = this_lb->lb_prev_lbp;
10281 		PTR_SWAP(this_lb, next_lb);
10282 		this_io = next_io;
10283 		next_io = NULL;
10284 	}
10285 
10286 	if (this_io != NULL)
10287 		l2arc_log_blk_fetch_abort(this_io);
10288 out:
10289 	if (next_io != NULL)
10290 		l2arc_log_blk_fetch_abort(next_io);
10291 	vmem_free(this_lb, sizeof (*this_lb));
10292 	vmem_free(next_lb, sizeof (*next_lb));
10293 
10294 	if (err == ECANCELED) {
10295 		/*
10296 		 * In case the rebuild was canceled do not log to spa history
10297 		 * log as the pool may be in the process of being removed.
10298 		 */
10299 		zfs_dbgmsg("L2ARC rebuild aborted, restored %llu blocks",
10300 		    (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count));
10301 		return (err);
10302 	} else if (!l2arc_rebuild_enabled) {
10303 		spa_history_log_internal(spa, "L2ARC rebuild", NULL,
10304 		    "disabled");
10305 	} else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) > 0) {
10306 		ARCSTAT_BUMP(arcstat_l2_rebuild_success);
10307 		spa_history_log_internal(spa, "L2ARC rebuild", NULL,
10308 		    "successful, restored %llu blocks",
10309 		    (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count));
10310 	} else if (err == 0 && zfs_refcount_count(&dev->l2ad_lb_count) == 0) {
10311 		/*
10312 		 * No error but also nothing restored, meaning the lbps array
10313 		 * in the device header points to invalid/non-present log
10314 		 * blocks. Reset the header.
10315 		 */
10316 		spa_history_log_internal(spa, "L2ARC rebuild", NULL,
10317 		    "no valid log blocks");
10318 		memset(l2dhdr, 0, dev->l2ad_dev_hdr_asize);
10319 		l2arc_dev_hdr_update(dev);
10320 	} else if (err != 0) {
10321 		spa_history_log_internal(spa, "L2ARC rebuild", NULL,
10322 		    "aborted, restored %llu blocks",
10323 		    (u_longlong_t)zfs_refcount_count(&dev->l2ad_lb_count));
10324 	}
10325 
10326 	if (lock_held)
10327 		spa_config_exit(spa, SCL_L2ARC, vd);
10328 
10329 	return (err);
10330 }
10331 
10332 /*
10333  * Attempts to read the device header on the provided L2ARC device and writes
10334  * it to `hdr'. On success, this function returns 0, otherwise the appropriate
10335  * error code is returned.
10336  */
10337 static int
l2arc_dev_hdr_read(l2arc_dev_t * dev)10338 l2arc_dev_hdr_read(l2arc_dev_t *dev)
10339 {
10340 	int			err;
10341 	uint64_t		guid;
10342 	l2arc_dev_hdr_phys_t	*l2dhdr = dev->l2ad_dev_hdr;
10343 	const uint64_t		l2dhdr_asize = dev->l2ad_dev_hdr_asize;
10344 	abd_t 			*abd;
10345 
10346 	guid = spa_guid(dev->l2ad_vdev->vdev_spa);
10347 
10348 	abd = abd_get_from_buf(l2dhdr, l2dhdr_asize);
10349 
10350 	err = zio_wait(zio_read_phys(NULL, dev->l2ad_vdev,
10351 	    VDEV_LABEL_START_SIZE, l2dhdr_asize, abd,
10352 	    ZIO_CHECKSUM_LABEL, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
10353 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY |
10354 	    ZIO_FLAG_SPECULATIVE, B_FALSE));
10355 
10356 	abd_free(abd);
10357 
10358 	if (err != 0) {
10359 		ARCSTAT_BUMP(arcstat_l2_rebuild_abort_dh_errors);
10360 		zfs_dbgmsg("L2ARC IO error (%d) while reading device header, "
10361 		    "vdev guid: %llu", err,
10362 		    (u_longlong_t)dev->l2ad_vdev->vdev_guid);
10363 		return (err);
10364 	}
10365 
10366 	if (l2dhdr->dh_magic == BSWAP_64(L2ARC_DEV_HDR_MAGIC))
10367 		byteswap_uint64_array(l2dhdr, sizeof (*l2dhdr));
10368 
10369 	if (l2dhdr->dh_magic != L2ARC_DEV_HDR_MAGIC ||
10370 	    l2dhdr->dh_spa_guid != guid ||
10371 	    l2dhdr->dh_vdev_guid != dev->l2ad_vdev->vdev_guid ||
10372 	    l2dhdr->dh_version != L2ARC_PERSISTENT_VERSION ||
10373 	    l2dhdr->dh_log_entries != dev->l2ad_log_entries ||
10374 	    l2dhdr->dh_end != dev->l2ad_end ||
10375 	    !l2arc_range_check_overlap(dev->l2ad_start, dev->l2ad_end,
10376 	    l2dhdr->dh_evict) ||
10377 	    (l2dhdr->dh_trim_state != VDEV_TRIM_COMPLETE &&
10378 	    l2arc_trim_ahead > 0)) {
10379 		/*
10380 		 * Attempt to rebuild a device containing no actual dev hdr
10381 		 * or containing a header from some other pool or from another
10382 		 * version of persistent L2ARC.
10383 		 */
10384 		ARCSTAT_BUMP(arcstat_l2_rebuild_abort_unsupported);
10385 		return (SET_ERROR(ENOTSUP));
10386 	}
10387 
10388 	return (0);
10389 }
10390 
10391 /*
10392  * Reads L2ARC log blocks from storage and validates their contents.
10393  *
10394  * This function implements a simple fetcher to make sure that while
10395  * we're processing one buffer the L2ARC is already fetching the next
10396  * one in the chain.
10397  *
10398  * The arguments this_lp and next_lp point to the current and next log block
10399  * address in the block chain. Similarly, this_lb and next_lb hold the
10400  * l2arc_log_blk_phys_t's of the current and next L2ARC blk.
10401  *
10402  * The `this_io' and `next_io' arguments are used for block fetching.
10403  * When issuing the first blk IO during rebuild, you should pass NULL for
10404  * `this_io'. This function will then issue a sync IO to read the block and
10405  * also issue an async IO to fetch the next block in the block chain. The
10406  * fetched IO is returned in `next_io'. On subsequent calls to this
10407  * function, pass the value returned in `next_io' from the previous call
10408  * as `this_io' and a fresh `next_io' pointer to hold the next fetch IO.
10409  * Prior to the call, you should initialize your `next_io' pointer to be
10410  * NULL. If no fetch IO was issued, the pointer is left set at NULL.
10411  *
10412  * On success, this function returns 0, otherwise it returns an appropriate
10413  * error code. On error the fetching IO is aborted and cleared before
10414  * returning from this function. Therefore, if we return `success', the
10415  * caller can assume that we have taken care of cleanup of fetch IOs.
10416  */
10417 static int
l2arc_log_blk_read(l2arc_dev_t * dev,const l2arc_log_blkptr_t * this_lbp,const l2arc_log_blkptr_t * next_lbp,l2arc_log_blk_phys_t * this_lb,l2arc_log_blk_phys_t * next_lb,zio_t * this_io,zio_t ** next_io)10418 l2arc_log_blk_read(l2arc_dev_t *dev,
10419     const l2arc_log_blkptr_t *this_lbp, const l2arc_log_blkptr_t *next_lbp,
10420     l2arc_log_blk_phys_t *this_lb, l2arc_log_blk_phys_t *next_lb,
10421     zio_t *this_io, zio_t **next_io)
10422 {
10423 	int		err = 0;
10424 	zio_cksum_t	cksum;
10425 	uint64_t	asize;
10426 
10427 	ASSERT(this_lbp != NULL && next_lbp != NULL);
10428 	ASSERT(this_lb != NULL && next_lb != NULL);
10429 	ASSERT(next_io != NULL && *next_io == NULL);
10430 	ASSERT(l2arc_log_blkptr_valid(dev, this_lbp));
10431 
10432 	/*
10433 	 * Check to see if we have issued the IO for this log block in a
10434 	 * previous run. If not, this is the first call, so issue it now.
10435 	 */
10436 	if (this_io == NULL) {
10437 		this_io = l2arc_log_blk_fetch(dev->l2ad_vdev, this_lbp,
10438 		    this_lb);
10439 	}
10440 
10441 	/*
10442 	 * Peek to see if we can start issuing the next IO immediately.
10443 	 */
10444 	if (l2arc_log_blkptr_valid(dev, next_lbp)) {
10445 		/*
10446 		 * Start issuing IO for the next log block early - this
10447 		 * should help keep the L2ARC device busy while we
10448 		 * decompress and restore this log block.
10449 		 */
10450 		*next_io = l2arc_log_blk_fetch(dev->l2ad_vdev, next_lbp,
10451 		    next_lb);
10452 	}
10453 
10454 	/* Wait for the IO to read this log block to complete */
10455 	if ((err = zio_wait(this_io)) != 0) {
10456 		ARCSTAT_BUMP(arcstat_l2_rebuild_abort_io_errors);
10457 		zfs_dbgmsg("L2ARC IO error (%d) while reading log block, "
10458 		    "offset: %llu, vdev guid: %llu", err,
10459 		    (u_longlong_t)this_lbp->lbp_daddr,
10460 		    (u_longlong_t)dev->l2ad_vdev->vdev_guid);
10461 		goto cleanup;
10462 	}
10463 
10464 	/*
10465 	 * Make sure the buffer checks out.
10466 	 * L2BLK_GET_PSIZE returns aligned size for log blocks.
10467 	 */
10468 	asize = L2BLK_GET_PSIZE((this_lbp)->lbp_prop);
10469 	fletcher_4_native(this_lb, asize, NULL, &cksum);
10470 	if (!ZIO_CHECKSUM_EQUAL(cksum, this_lbp->lbp_cksum)) {
10471 		ARCSTAT_BUMP(arcstat_l2_rebuild_abort_cksum_lb_errors);
10472 		zfs_dbgmsg("L2ARC log block cksum failed, offset: %llu, "
10473 		    "vdev guid: %llu, l2ad_hand: %llu, l2ad_evict: %llu",
10474 		    (u_longlong_t)this_lbp->lbp_daddr,
10475 		    (u_longlong_t)dev->l2ad_vdev->vdev_guid,
10476 		    (u_longlong_t)dev->l2ad_hand,
10477 		    (u_longlong_t)dev->l2ad_evict);
10478 		err = SET_ERROR(ECKSUM);
10479 		goto cleanup;
10480 	}
10481 
10482 	/* Now we can take our time decoding this buffer */
10483 	switch (L2BLK_GET_COMPRESS((this_lbp)->lbp_prop)) {
10484 	case ZIO_COMPRESS_OFF:
10485 		break;
10486 	case ZIO_COMPRESS_LZ4: {
10487 		abd_t *abd = abd_alloc_linear(asize, B_TRUE);
10488 		abd_copy_from_buf_off(abd, this_lb, 0, asize);
10489 		abd_t dabd;
10490 		abd_get_from_buf_struct(&dabd, this_lb, sizeof (*this_lb));
10491 		err = zio_decompress_data(
10492 		    L2BLK_GET_COMPRESS((this_lbp)->lbp_prop),
10493 		    abd, &dabd, asize, sizeof (*this_lb), NULL);
10494 		abd_free(&dabd);
10495 		abd_free(abd);
10496 		if (err != 0) {
10497 			err = SET_ERROR(EINVAL);
10498 			goto cleanup;
10499 		}
10500 		break;
10501 	}
10502 	default:
10503 		err = SET_ERROR(EINVAL);
10504 		goto cleanup;
10505 	}
10506 	if (this_lb->lb_magic == BSWAP_64(L2ARC_LOG_BLK_MAGIC))
10507 		byteswap_uint64_array(this_lb, sizeof (*this_lb));
10508 	if (this_lb->lb_magic != L2ARC_LOG_BLK_MAGIC) {
10509 		err = SET_ERROR(EINVAL);
10510 		goto cleanup;
10511 	}
10512 cleanup:
10513 	/* Abort an in-flight fetch I/O in case of error */
10514 	if (err != 0 && *next_io != NULL) {
10515 		l2arc_log_blk_fetch_abort(*next_io);
10516 		*next_io = NULL;
10517 	}
10518 	return (err);
10519 }
10520 
10521 /*
10522  * Restores the payload of a log block to ARC. This creates empty ARC hdr
10523  * entries which only contain an l2arc hdr, essentially restoring the
10524  * buffers to their L2ARC evicted state. This function also updates space
10525  * usage on the L2ARC vdev to make sure it tracks restored buffers.
10526  */
10527 static void
l2arc_log_blk_restore(l2arc_dev_t * dev,const l2arc_log_blk_phys_t * lb,uint64_t lb_asize)10528 l2arc_log_blk_restore(l2arc_dev_t *dev, const l2arc_log_blk_phys_t *lb,
10529     uint64_t lb_asize)
10530 {
10531 	uint64_t	size = 0, asize = 0;
10532 	uint64_t	log_entries = dev->l2ad_log_entries;
10533 
10534 	/*
10535 	 * Usually arc_adapt() is called only for data, not headers, but
10536 	 * since we may allocate significant amount of memory here, let ARC
10537 	 * grow its arc_c.
10538 	 */
10539 	arc_adapt(log_entries * HDR_L2ONLY_SIZE);
10540 
10541 	for (int i = log_entries - 1; i >= 0; i--) {
10542 		/*
10543 		 * Restore goes in the reverse temporal direction to preserve
10544 		 * correct temporal ordering of buffers in the l2ad_buflist.
10545 		 * l2arc_hdr_restore also does a list_insert_tail instead of
10546 		 * list_insert_head on the l2ad_buflist:
10547 		 *
10548 		 *		LIST	l2ad_buflist		LIST
10549 		 *		HEAD  <------ (time) ------	TAIL
10550 		 * direction	+-----+-----+-----+-----+-----+    direction
10551 		 * of l2arc <== | buf | buf | buf | buf | buf | ===> of rebuild
10552 		 * fill		+-----+-----+-----+-----+-----+
10553 		 *		^				^
10554 		 *		|				|
10555 		 *		|				|
10556 		 *	l2arc_feed_thread		l2arc_rebuild
10557 		 *	will place new bufs here	restores bufs here
10558 		 *
10559 		 * During l2arc_rebuild() the device is not used by
10560 		 * l2arc_feed_thread() as dev->l2ad_rebuild is set to true.
10561 		 */
10562 		size += L2BLK_GET_LSIZE((&lb->lb_entries[i])->le_prop);
10563 		asize += vdev_psize_to_asize(dev->l2ad_vdev,
10564 		    L2BLK_GET_PSIZE((&lb->lb_entries[i])->le_prop));
10565 		l2arc_hdr_restore(&lb->lb_entries[i], dev);
10566 	}
10567 
10568 	/*
10569 	 * Record rebuild stats:
10570 	 *	size		Logical size of restored buffers in the L2ARC
10571 	 *	asize		Aligned size of restored buffers in the L2ARC
10572 	 */
10573 	ARCSTAT_INCR(arcstat_l2_rebuild_size, size);
10574 	ARCSTAT_INCR(arcstat_l2_rebuild_asize, asize);
10575 	ARCSTAT_INCR(arcstat_l2_rebuild_bufs, log_entries);
10576 	ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, lb_asize);
10577 	ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio, asize / lb_asize);
10578 	ARCSTAT_BUMP(arcstat_l2_rebuild_log_blks);
10579 }
10580 
10581 /*
10582  * Restores a single ARC buf hdr from a log entry. The ARC buffer is put
10583  * into a state indicating that it has been evicted to L2ARC.
10584  */
10585 static void
l2arc_hdr_restore(const l2arc_log_ent_phys_t * le,l2arc_dev_t * dev)10586 l2arc_hdr_restore(const l2arc_log_ent_phys_t *le, l2arc_dev_t *dev)
10587 {
10588 	arc_buf_hdr_t		*hdr, *exists;
10589 	kmutex_t		*hash_lock;
10590 	arc_buf_contents_t	type = L2BLK_GET_TYPE((le)->le_prop);
10591 	uint64_t		asize = vdev_psize_to_asize(dev->l2ad_vdev,
10592 	    L2BLK_GET_PSIZE((le)->le_prop));
10593 
10594 	/*
10595 	 * Do all the allocation before grabbing any locks, this lets us
10596 	 * sleep if memory is full and we don't have to deal with failed
10597 	 * allocations.
10598 	 */
10599 	hdr = arc_buf_alloc_l2only(L2BLK_GET_LSIZE((le)->le_prop), type,
10600 	    dev, le->le_dva, le->le_daddr,
10601 	    L2BLK_GET_PSIZE((le)->le_prop), asize, le->le_birth,
10602 	    L2BLK_GET_COMPRESS((le)->le_prop), le->le_complevel,
10603 	    L2BLK_GET_PROTECTED((le)->le_prop),
10604 	    L2BLK_GET_PREFETCH((le)->le_prop),
10605 	    L2BLK_GET_STATE((le)->le_prop));
10606 
10607 	/*
10608 	 * vdev_space_update() has to be called before arc_hdr_destroy() to
10609 	 * avoid underflow since the latter also calls vdev_space_update().
10610 	 */
10611 	l2arc_hdr_arcstats_increment(hdr);
10612 	vdev_space_update(dev->l2ad_vdev, asize, 0, 0);
10613 
10614 	mutex_enter(&dev->l2ad_mtx);
10615 	list_insert_tail(&dev->l2ad_buflist, hdr);
10616 	(void) zfs_refcount_add_many(&dev->l2ad_alloc, arc_hdr_size(hdr), hdr);
10617 	mutex_exit(&dev->l2ad_mtx);
10618 
10619 	exists = buf_hash_insert(hdr, &hash_lock);
10620 	if (exists) {
10621 		/* Buffer was already cached, no need to restore it. */
10622 		arc_hdr_destroy(hdr);
10623 		/*
10624 		 * If the buffer is already cached, check whether it has
10625 		 * L2ARC metadata. If not, enter them and update the flag.
10626 		 * This is important is case of onlining a cache device, since
10627 		 * we previously evicted all L2ARC metadata from ARC.
10628 		 */
10629 		if (!HDR_HAS_L2HDR(exists)) {
10630 			arc_hdr_set_flags(exists, ARC_FLAG_HAS_L2HDR);
10631 			exists->b_l2hdr.b_dev = dev;
10632 			exists->b_l2hdr.b_daddr = le->le_daddr;
10633 			exists->b_l2hdr.b_arcs_state =
10634 			    L2BLK_GET_STATE((le)->le_prop);
10635 			/* l2arc_hdr_arcstats_update() expects a valid asize */
10636 			HDR_SET_L2SIZE(exists, asize);
10637 			mutex_enter(&dev->l2ad_mtx);
10638 			list_insert_tail(&dev->l2ad_buflist, exists);
10639 			(void) zfs_refcount_add_many(&dev->l2ad_alloc,
10640 			    arc_hdr_size(exists), exists);
10641 			mutex_exit(&dev->l2ad_mtx);
10642 			l2arc_hdr_arcstats_increment(exists);
10643 			vdev_space_update(dev->l2ad_vdev, asize, 0, 0);
10644 		}
10645 		ARCSTAT_BUMP(arcstat_l2_rebuild_bufs_precached);
10646 	}
10647 
10648 	mutex_exit(hash_lock);
10649 }
10650 
10651 /*
10652  * Starts an asynchronous read IO to read a log block. This is used in log
10653  * block reconstruction to start reading the next block before we are done
10654  * decoding and reconstructing the current block, to keep the l2arc device
10655  * nice and hot with read IO to process.
10656  * The returned zio will contain a newly allocated memory buffers for the IO
10657  * data which should then be freed by the caller once the zio is no longer
10658  * needed (i.e. due to it having completed). If you wish to abort this
10659  * zio, you should do so using l2arc_log_blk_fetch_abort, which takes
10660  * care of disposing of the allocated buffers correctly.
10661  */
10662 static zio_t *
l2arc_log_blk_fetch(vdev_t * vd,const l2arc_log_blkptr_t * lbp,l2arc_log_blk_phys_t * lb)10663 l2arc_log_blk_fetch(vdev_t *vd, const l2arc_log_blkptr_t *lbp,
10664     l2arc_log_blk_phys_t *lb)
10665 {
10666 	uint32_t		asize;
10667 	zio_t			*pio;
10668 	l2arc_read_callback_t	*cb;
10669 
10670 	/* L2BLK_GET_PSIZE returns aligned size for log blocks */
10671 	asize = L2BLK_GET_PSIZE((lbp)->lbp_prop);
10672 	ASSERT(asize <= sizeof (l2arc_log_blk_phys_t));
10673 
10674 	cb = kmem_zalloc(sizeof (l2arc_read_callback_t), KM_SLEEP);
10675 	cb->l2rcb_abd = abd_get_from_buf(lb, asize);
10676 	pio = zio_root(vd->vdev_spa, l2arc_blk_fetch_done, cb,
10677 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY);
10678 	(void) zio_nowait(zio_read_phys(pio, vd, lbp->lbp_daddr, asize,
10679 	    cb->l2rcb_abd, ZIO_CHECKSUM_OFF, NULL, NULL,
10680 	    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL |
10681 	    ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY, B_FALSE));
10682 
10683 	return (pio);
10684 }
10685 
10686 /*
10687  * Aborts a zio returned from l2arc_log_blk_fetch and frees the data
10688  * buffers allocated for it.
10689  */
10690 static void
l2arc_log_blk_fetch_abort(zio_t * zio)10691 l2arc_log_blk_fetch_abort(zio_t *zio)
10692 {
10693 	(void) zio_wait(zio);
10694 }
10695 
10696 /*
10697  * Creates a zio to update the device header on an l2arc device.
10698  */
10699 void
l2arc_dev_hdr_update(l2arc_dev_t * dev)10700 l2arc_dev_hdr_update(l2arc_dev_t *dev)
10701 {
10702 	l2arc_dev_hdr_phys_t	*l2dhdr = dev->l2ad_dev_hdr;
10703 	const uint64_t		l2dhdr_asize = dev->l2ad_dev_hdr_asize;
10704 	abd_t			*abd;
10705 	int			err;
10706 
10707 	VERIFY(spa_config_held(dev->l2ad_spa, SCL_STATE_ALL, RW_READER));
10708 
10709 	l2dhdr->dh_magic = L2ARC_DEV_HDR_MAGIC;
10710 	l2dhdr->dh_version = L2ARC_PERSISTENT_VERSION;
10711 	l2dhdr->dh_spa_guid = spa_guid(dev->l2ad_vdev->vdev_spa);
10712 	l2dhdr->dh_vdev_guid = dev->l2ad_vdev->vdev_guid;
10713 	l2dhdr->dh_log_entries = dev->l2ad_log_entries;
10714 	l2dhdr->dh_evict = dev->l2ad_evict;
10715 	l2dhdr->dh_start = dev->l2ad_start;
10716 	l2dhdr->dh_end = dev->l2ad_end;
10717 	l2dhdr->dh_lb_asize = zfs_refcount_count(&dev->l2ad_lb_asize);
10718 	l2dhdr->dh_lb_count = zfs_refcount_count(&dev->l2ad_lb_count);
10719 	l2dhdr->dh_flags = 0;
10720 	l2dhdr->dh_trim_action_time = dev->l2ad_vdev->vdev_trim_action_time;
10721 	l2dhdr->dh_trim_state = dev->l2ad_vdev->vdev_trim_state;
10722 	if (dev->l2ad_first)
10723 		l2dhdr->dh_flags |= L2ARC_DEV_HDR_EVICT_FIRST;
10724 
10725 	abd = abd_get_from_buf(l2dhdr, l2dhdr_asize);
10726 
10727 	err = zio_wait(zio_write_phys(NULL, dev->l2ad_vdev,
10728 	    VDEV_LABEL_START_SIZE, l2dhdr_asize, abd, ZIO_CHECKSUM_LABEL, NULL,
10729 	    NULL, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE));
10730 
10731 	abd_free(abd);
10732 
10733 	if (err != 0) {
10734 		zfs_dbgmsg("L2ARC IO error (%d) while writing device header, "
10735 		    "vdev guid: %llu", err,
10736 		    (u_longlong_t)dev->l2ad_vdev->vdev_guid);
10737 	}
10738 }
10739 
10740 /*
10741  * Commits a log block to the L2ARC device. This routine is invoked from
10742  * l2arc_write_buffers when the log block fills up.
10743  * This function allocates some memory to temporarily hold the serialized
10744  * buffer to be written. This is then released in l2arc_write_done.
10745  */
10746 static uint64_t
l2arc_log_blk_commit(l2arc_dev_t * dev,zio_t * pio,l2arc_write_callback_t * cb)10747 l2arc_log_blk_commit(l2arc_dev_t *dev, zio_t *pio, l2arc_write_callback_t *cb)
10748 {
10749 	l2arc_log_blk_phys_t	*lb = &dev->l2ad_log_blk;
10750 	l2arc_dev_hdr_phys_t	*l2dhdr = dev->l2ad_dev_hdr;
10751 	uint64_t		psize, asize;
10752 	zio_t			*wzio;
10753 	l2arc_lb_abd_buf_t	*abd_buf;
10754 	abd_t			*abd = NULL;
10755 	l2arc_lb_ptr_buf_t	*lb_ptr_buf;
10756 
10757 	VERIFY3S(dev->l2ad_log_ent_idx, ==, dev->l2ad_log_entries);
10758 
10759 	abd_buf = zio_buf_alloc(sizeof (*abd_buf));
10760 	abd_buf->abd = abd_get_from_buf(lb, sizeof (*lb));
10761 	lb_ptr_buf = kmem_zalloc(sizeof (l2arc_lb_ptr_buf_t), KM_SLEEP);
10762 	lb_ptr_buf->lb_ptr = kmem_zalloc(sizeof (l2arc_log_blkptr_t), KM_SLEEP);
10763 
10764 	/* link the buffer into the block chain */
10765 	lb->lb_prev_lbp = l2dhdr->dh_start_lbps[1];
10766 	lb->lb_magic = L2ARC_LOG_BLK_MAGIC;
10767 
10768 	/*
10769 	 * l2arc_log_blk_commit() may be called multiple times during a single
10770 	 * l2arc_write_buffers() call. Save the allocated abd buffers in a list
10771 	 * so we can free them in l2arc_write_done() later on.
10772 	 */
10773 	list_insert_tail(&cb->l2wcb_abd_list, abd_buf);
10774 
10775 	/* try to compress the buffer, at least one sector to save */
10776 	psize = zio_compress_data(ZIO_COMPRESS_LZ4,
10777 	    abd_buf->abd, &abd, sizeof (*lb),
10778 	    zio_get_compression_max_size(ZIO_COMPRESS_LZ4,
10779 	    dev->l2ad_vdev->vdev_ashift,
10780 	    dev->l2ad_vdev->vdev_ashift, sizeof (*lb)), 0);
10781 
10782 	/* a log block is never entirely zero */
10783 	ASSERT(psize != 0);
10784 	asize = vdev_psize_to_asize(dev->l2ad_vdev, psize);
10785 	ASSERT(asize <= sizeof (*lb));
10786 
10787 	/*
10788 	 * Update the start log block pointer in the device header to point
10789 	 * to the log block we're about to write.
10790 	 */
10791 	l2dhdr->dh_start_lbps[1] = l2dhdr->dh_start_lbps[0];
10792 	l2dhdr->dh_start_lbps[0].lbp_daddr = dev->l2ad_hand;
10793 	l2dhdr->dh_start_lbps[0].lbp_payload_asize =
10794 	    dev->l2ad_log_blk_payload_asize;
10795 	l2dhdr->dh_start_lbps[0].lbp_payload_start =
10796 	    dev->l2ad_log_blk_payload_start;
10797 	L2BLK_SET_LSIZE(
10798 	    (&l2dhdr->dh_start_lbps[0])->lbp_prop, sizeof (*lb));
10799 	L2BLK_SET_PSIZE(
10800 	    (&l2dhdr->dh_start_lbps[0])->lbp_prop, asize);
10801 	L2BLK_SET_CHECKSUM(
10802 	    (&l2dhdr->dh_start_lbps[0])->lbp_prop,
10803 	    ZIO_CHECKSUM_FLETCHER_4);
10804 	if (asize < sizeof (*lb)) {
10805 		/* compression succeeded */
10806 		abd_zero_off(abd, psize, asize - psize);
10807 		L2BLK_SET_COMPRESS(
10808 		    (&l2dhdr->dh_start_lbps[0])->lbp_prop,
10809 		    ZIO_COMPRESS_LZ4);
10810 	} else {
10811 		/* compression failed */
10812 		abd_copy_from_buf_off(abd, lb, 0, sizeof (*lb));
10813 		L2BLK_SET_COMPRESS(
10814 		    (&l2dhdr->dh_start_lbps[0])->lbp_prop,
10815 		    ZIO_COMPRESS_OFF);
10816 	}
10817 
10818 	/* checksum what we're about to write */
10819 	abd_fletcher_4_native(abd, asize, NULL,
10820 	    &l2dhdr->dh_start_lbps[0].lbp_cksum);
10821 
10822 	abd_free(abd_buf->abd);
10823 
10824 	/* perform the write itself */
10825 	abd_buf->abd = abd;
10826 	wzio = zio_write_phys(pio, dev->l2ad_vdev, dev->l2ad_hand,
10827 	    asize, abd_buf->abd, ZIO_CHECKSUM_OFF, NULL, NULL,
10828 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_CANFAIL, B_FALSE);
10829 	DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev, zio_t *, wzio);
10830 	(void) zio_nowait(wzio);
10831 
10832 	dev->l2ad_hand += asize;
10833 	vdev_space_update(dev->l2ad_vdev, asize, 0, 0);
10834 
10835 	/*
10836 	 * Include the committed log block's pointer  in the list of pointers
10837 	 * to log blocks present in the L2ARC device.
10838 	 */
10839 	memcpy(lb_ptr_buf->lb_ptr, &l2dhdr->dh_start_lbps[0],
10840 	    sizeof (l2arc_log_blkptr_t));
10841 	mutex_enter(&dev->l2ad_mtx);
10842 	list_insert_head(&dev->l2ad_lbptr_list, lb_ptr_buf);
10843 	ARCSTAT_INCR(arcstat_l2_log_blk_asize, asize);
10844 	ARCSTAT_BUMP(arcstat_l2_log_blk_count);
10845 	zfs_refcount_add_many(&dev->l2ad_lb_asize, asize, lb_ptr_buf);
10846 	zfs_refcount_add(&dev->l2ad_lb_count, lb_ptr_buf);
10847 	mutex_exit(&dev->l2ad_mtx);
10848 
10849 	/* bump the kstats */
10850 	ARCSTAT_INCR(arcstat_l2_write_bytes, asize);
10851 	ARCSTAT_BUMP(arcstat_l2_log_blk_writes);
10852 	ARCSTAT_F_AVG(arcstat_l2_log_blk_avg_asize, asize);
10853 	ARCSTAT_F_AVG(arcstat_l2_data_to_meta_ratio,
10854 	    dev->l2ad_log_blk_payload_asize / asize);
10855 
10856 	/* start a new log block */
10857 	dev->l2ad_log_ent_idx = 0;
10858 	dev->l2ad_log_blk_payload_asize = 0;
10859 	dev->l2ad_log_blk_payload_start = 0;
10860 
10861 	return (asize);
10862 }
10863 
10864 /*
10865  * Validates an L2ARC log block address to make sure that it can be read
10866  * from the provided L2ARC device.
10867  */
10868 boolean_t
l2arc_log_blkptr_valid(l2arc_dev_t * dev,const l2arc_log_blkptr_t * lbp)10869 l2arc_log_blkptr_valid(l2arc_dev_t *dev, const l2arc_log_blkptr_t *lbp)
10870 {
10871 	/* L2BLK_GET_PSIZE returns aligned size for log blocks */
10872 	uint64_t asize = L2BLK_GET_PSIZE((lbp)->lbp_prop);
10873 	uint64_t end = lbp->lbp_daddr + asize - 1;
10874 	uint64_t start = lbp->lbp_payload_start;
10875 	boolean_t evicted = B_FALSE;
10876 
10877 	/*
10878 	 * A log block is valid if all of the following conditions are true:
10879 	 * - it fits entirely (including its payload) between l2ad_start and
10880 	 *   l2ad_end
10881 	 * - it has a valid size
10882 	 * - neither the log block itself nor part of its payload was evicted
10883 	 *   by l2arc_evict():
10884 	 *
10885 	 *		l2ad_hand          l2ad_evict
10886 	 *		|			 |	lbp_daddr
10887 	 *		|     start		 |	|  end
10888 	 *		|     |			 |	|  |
10889 	 *		V     V		         V	V  V
10890 	 *   l2ad_start ============================================ l2ad_end
10891 	 *                    --------------------------||||
10892 	 *				^		 ^
10893 	 *				|		log block
10894 	 *				payload
10895 	 */
10896 
10897 	evicted =
10898 	    l2arc_range_check_overlap(start, end, dev->l2ad_hand) ||
10899 	    l2arc_range_check_overlap(start, end, dev->l2ad_evict) ||
10900 	    l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, start) ||
10901 	    l2arc_range_check_overlap(dev->l2ad_hand, dev->l2ad_evict, end);
10902 
10903 	return (start >= dev->l2ad_start && end <= dev->l2ad_end &&
10904 	    asize > 0 && asize <= sizeof (l2arc_log_blk_phys_t) &&
10905 	    (!evicted || dev->l2ad_first));
10906 }
10907 
10908 /*
10909  * Inserts ARC buffer header `hdr' into the current L2ARC log block on
10910  * the device. The buffer being inserted must be present in L2ARC.
10911  * Returns B_TRUE if the L2ARC log block is full and needs to be committed
10912  * to L2ARC, or B_FALSE if it still has room for more ARC buffers.
10913  */
10914 static boolean_t
l2arc_log_blk_insert(l2arc_dev_t * dev,const arc_buf_hdr_t * hdr)10915 l2arc_log_blk_insert(l2arc_dev_t *dev, const arc_buf_hdr_t *hdr)
10916 {
10917 	l2arc_log_blk_phys_t	*lb = &dev->l2ad_log_blk;
10918 	l2arc_log_ent_phys_t	*le;
10919 
10920 	if (dev->l2ad_log_entries == 0)
10921 		return (B_FALSE);
10922 
10923 	int index = dev->l2ad_log_ent_idx++;
10924 
10925 	ASSERT3S(index, <, dev->l2ad_log_entries);
10926 	ASSERT(HDR_HAS_L2HDR(hdr));
10927 
10928 	le = &lb->lb_entries[index];
10929 	memset(le, 0, sizeof (*le));
10930 	le->le_dva = hdr->b_dva;
10931 	le->le_birth = hdr->b_birth;
10932 	le->le_daddr = hdr->b_l2hdr.b_daddr;
10933 	if (index == 0)
10934 		dev->l2ad_log_blk_payload_start = le->le_daddr;
10935 	L2BLK_SET_LSIZE((le)->le_prop, HDR_GET_LSIZE(hdr));
10936 	L2BLK_SET_PSIZE((le)->le_prop, HDR_GET_PSIZE(hdr));
10937 	L2BLK_SET_COMPRESS((le)->le_prop, HDR_GET_COMPRESS(hdr));
10938 	le->le_complevel = hdr->b_complevel;
10939 	L2BLK_SET_TYPE((le)->le_prop, hdr->b_type);
10940 	L2BLK_SET_PROTECTED((le)->le_prop, !!(HDR_PROTECTED(hdr)));
10941 	L2BLK_SET_PREFETCH((le)->le_prop, !!(HDR_PREFETCH(hdr)));
10942 	L2BLK_SET_STATE((le)->le_prop, hdr->b_l2hdr.b_arcs_state);
10943 
10944 	dev->l2ad_log_blk_payload_asize += vdev_psize_to_asize(dev->l2ad_vdev,
10945 	    HDR_GET_PSIZE(hdr));
10946 
10947 	return (dev->l2ad_log_ent_idx == dev->l2ad_log_entries);
10948 }
10949 
10950 /*
10951  * Checks whether a given L2ARC device address sits in a time-sequential
10952  * range. The trick here is that the L2ARC is a rotary buffer, so we can't
10953  * just do a range comparison, we need to handle the situation in which the
10954  * range wraps around the end of the L2ARC device. Arguments:
10955  *	bottom -- Lower end of the range to check (written to earlier).
10956  *	top    -- Upper end of the range to check (written to later).
10957  *	check  -- The address for which we want to determine if it sits in
10958  *		  between the top and bottom.
10959  *
10960  * The 3-way conditional below represents the following cases:
10961  *
10962  *	bottom < top : Sequentially ordered case:
10963  *	  <check>--------+-------------------+
10964  *	                 |  (overlap here?)  |
10965  *	 L2ARC dev       V                   V
10966  *	 |---------------<bottom>============<top>--------------|
10967  *
10968  *	bottom > top: Looped-around case:
10969  *	                      <check>--------+------------------+
10970  *	                                     |  (overlap here?) |
10971  *	 L2ARC dev                           V                  V
10972  *	 |===============<top>---------------<bottom>===========|
10973  *	 ^               ^
10974  *	 |  (or here?)   |
10975  *	 +---------------+---------<check>
10976  *
10977  *	top == bottom : Just a single address comparison.
10978  */
10979 boolean_t
l2arc_range_check_overlap(uint64_t bottom,uint64_t top,uint64_t check)10980 l2arc_range_check_overlap(uint64_t bottom, uint64_t top, uint64_t check)
10981 {
10982 	if (bottom < top)
10983 		return (bottom <= check && check <= top);
10984 	else if (bottom > top)
10985 		return (check <= top || bottom <= check);
10986 	else
10987 		return (check == top);
10988 }
10989 
10990 EXPORT_SYMBOL(arc_buf_size);
10991 EXPORT_SYMBOL(arc_write);
10992 EXPORT_SYMBOL(arc_read);
10993 EXPORT_SYMBOL(arc_buf_info);
10994 EXPORT_SYMBOL(arc_getbuf_func);
10995 EXPORT_SYMBOL(arc_add_prune_callback);
10996 EXPORT_SYMBOL(arc_remove_prune_callback);
10997 
10998 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min, param_set_arc_min,
10999 	spl_param_get_u64, ZMOD_RW, "Minimum ARC size in bytes");
11000 
11001 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, max, param_set_arc_max,
11002 	spl_param_get_u64, ZMOD_RW, "Maximum ARC size in bytes");
11003 
11004 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, meta_balance, UINT, ZMOD_RW,
11005 	"Balance between metadata and data on ghost hits.");
11006 
11007 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, grow_retry, param_set_arc_int,
11008 	param_get_uint, ZMOD_RW, "Seconds before growing ARC size");
11009 
11010 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, shrink_shift, param_set_arc_int,
11011 	param_get_uint, ZMOD_RW, "log2(fraction of ARC to reclaim)");
11012 
11013 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, pc_percent, UINT, ZMOD_RW,
11014 	"Percent of pagecache to reclaim ARC to");
11015 
11016 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, average_blocksize, UINT, ZMOD_RD,
11017 	"Target average block size");
11018 
11019 ZFS_MODULE_PARAM(zfs, zfs_, compressed_arc_enabled, INT, ZMOD_RW,
11020 	"Disable compressed ARC buffers");
11021 
11022 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prefetch_ms, param_set_arc_int,
11023 	param_get_uint, ZMOD_RW, "Min life of prefetch block in ms");
11024 
11025 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, min_prescient_prefetch_ms,
11026     param_set_arc_int, param_get_uint, ZMOD_RW,
11027 	"Min life of prescient prefetched block in ms");
11028 
11029 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_max, U64, ZMOD_RW,
11030 	"Max write bytes per interval");
11031 
11032 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, write_boost, U64, ZMOD_RW,
11033 	"Extra write bytes during device warmup");
11034 
11035 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom, U64, ZMOD_RW,
11036 	"Number of max device writes to precache");
11037 
11038 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, headroom_boost, U64, ZMOD_RW,
11039 	"Compressed l2arc_headroom multiplier");
11040 
11041 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, trim_ahead, U64, ZMOD_RW,
11042 	"TRIM ahead L2ARC write size multiplier");
11043 
11044 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_secs, U64, ZMOD_RW,
11045 	"Seconds between L2ARC writing");
11046 
11047 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_min_ms, U64, ZMOD_RW,
11048 	"Min feed interval in milliseconds");
11049 
11050 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, noprefetch, INT, ZMOD_RW,
11051 	"Skip caching prefetched buffers");
11052 
11053 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, feed_again, INT, ZMOD_RW,
11054 	"Turbo L2ARC warmup");
11055 
11056 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, norw, INT, ZMOD_RW,
11057 	"No reads during writes");
11058 
11059 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, meta_percent, UINT, ZMOD_RW,
11060 	"Percent of ARC size allowed for L2ARC-only headers");
11061 
11062 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_enabled, INT, ZMOD_RW,
11063 	"Rebuild the L2ARC when importing a pool");
11064 
11065 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, rebuild_blocks_min_l2size, U64, ZMOD_RW,
11066 	"Min size in bytes to write rebuild log blocks in L2ARC");
11067 
11068 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, mfuonly, INT, ZMOD_RW,
11069 	"Cache only MFU data from ARC into L2ARC");
11070 
11071 ZFS_MODULE_PARAM(zfs_l2arc, l2arc_, exclude_special, INT, ZMOD_RW,
11072 	"Exclude dbufs on special vdevs from being cached to L2ARC if set.");
11073 
11074 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, lotsfree_percent, param_set_arc_int,
11075 	param_get_uint, ZMOD_RW, "System free memory I/O throttle in bytes");
11076 
11077 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, sys_free, param_set_arc_u64,
11078 	spl_param_get_u64, ZMOD_RW, "System free memory target size in bytes");
11079 
11080 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit, param_set_arc_u64,
11081 	spl_param_get_u64, ZMOD_RW, "Minimum bytes of dnodes in ARC");
11082 
11083 ZFS_MODULE_PARAM_CALL(zfs_arc, zfs_arc_, dnode_limit_percent,
11084     param_set_arc_int, param_get_uint, ZMOD_RW,
11085 	"Percent of ARC meta buffers for dnodes");
11086 
11087 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, dnode_reduce_percent, UINT, ZMOD_RW,
11088 	"Percentage of excess dnodes to try to unpin");
11089 
11090 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, eviction_pct, UINT, ZMOD_RW,
11091 	"When full, ARC allocation waits for eviction of this % of alloc size");
11092 
11093 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, evict_batch_limit, UINT, ZMOD_RW,
11094 	"The number of headers to evict per sublist before moving to the next");
11095 
11096 ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, prune_task_threads, INT, ZMOD_RW,
11097 	"Number of arc_prune threads");
11098