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