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