xref: /illumos-gate/usr/src/uts/common/fs/zfs/arc.c (revision 1b912ec7100c10e7243bf0879af0fe580e08c73d)
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 2011 Nexenta Systems, Inc.  All rights reserved.
24  * Copyright (c) 2012 by Delphix. All rights reserved.
25  */
26 
27 /*
28  * DVA-based Adjustable Replacement Cache
29  *
30  * While much of the theory of operation used here is
31  * based on the self-tuning, low overhead replacement cache
32  * presented by Megiddo and Modha at FAST 2003, there are some
33  * significant differences:
34  *
35  * 1. The Megiddo and Modha model assumes any page is evictable.
36  * Pages in its cache cannot be "locked" into memory.  This makes
37  * the eviction algorithm simple: evict the last page in the list.
38  * This also make the performance characteristics easy to reason
39  * about.  Our cache is not so simple.  At any given moment, some
40  * subset of the blocks in the cache are un-evictable because we
41  * have handed out a reference to them.  Blocks are only evictable
42  * when there are no external references active.  This makes
43  * eviction far more problematic:  we choose to evict the evictable
44  * blocks that are the "lowest" in the list.
45  *
46  * There are times when it is not possible to evict the requested
47  * space.  In these circumstances we are unable to adjust the cache
48  * size.  To prevent the cache growing unbounded at these times we
49  * implement a "cache throttle" that slows the flow of new data
50  * into the cache until we can make space available.
51  *
52  * 2. The Megiddo and Modha model assumes a fixed cache size.
53  * Pages are evicted when the cache is full and there is a cache
54  * miss.  Our model has a variable sized cache.  It grows with
55  * high use, but also tries to react to memory pressure from the
56  * operating system: decreasing its size when system memory is
57  * tight.
58  *
59  * 3. The Megiddo and Modha model assumes a fixed page size. All
60  * elements of the cache are therefor exactly the same size.  So
61  * when adjusting the cache size following a cache miss, its simply
62  * a matter of choosing a single page to evict.  In our model, we
63  * have variable sized cache blocks (rangeing from 512 bytes to
64  * 128K bytes).  We therefor choose a set of blocks to evict to make
65  * space for a cache miss that approximates as closely as possible
66  * the space used by the new block.
67  *
68  * See also:  "ARC: A Self-Tuning, Low Overhead Replacement Cache"
69  * by N. Megiddo & D. Modha, FAST 2003
70  */
71 
72 /*
73  * The locking model:
74  *
75  * A new reference to a cache buffer can be obtained in two
76  * ways: 1) via a hash table lookup using the DVA as a key,
77  * or 2) via one of the ARC lists.  The arc_read() interface
78  * uses method 1, while the internal arc algorithms for
79  * adjusting the cache use method 2.  We therefor provide two
80  * types of locks: 1) the hash table lock array, and 2) the
81  * arc list locks.
82  *
83  * Buffers do not have their own mutexes, rather they rely on the
84  * hash table mutexes for the bulk of their protection (i.e. most
85  * fields in the arc_buf_hdr_t are protected by these mutexes).
86  *
87  * buf_hash_find() returns the appropriate mutex (held) when it
88  * locates the requested buffer in the hash table.  It returns
89  * NULL for the mutex if the buffer was not in the table.
90  *
91  * buf_hash_remove() expects the appropriate hash mutex to be
92  * already held before it is invoked.
93  *
94  * Each arc state also has a mutex which is used to protect the
95  * buffer list associated with the state.  When attempting to
96  * obtain a hash table lock while holding an arc list lock you
97  * must use: mutex_tryenter() to avoid deadlock.  Also note that
98  * the active state mutex must be held before the ghost state mutex.
99  *
100  * Arc buffers may have an associated eviction callback function.
101  * This function will be invoked prior to removing the buffer (e.g.
102  * in arc_do_user_evicts()).  Note however that the data associated
103  * with the buffer may be evicted prior to the callback.  The callback
104  * must be made with *no locks held* (to prevent deadlock).  Additionally,
105  * the users of callbacks must ensure that their private data is
106  * protected from simultaneous callbacks from arc_buf_evict()
107  * and arc_do_user_evicts().
108  *
109  * Note that the majority of the performance stats are manipulated
110  * with atomic operations.
111  *
112  * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
113  *
114  *	- L2ARC buflist creation
115  *	- L2ARC buflist eviction
116  *	- L2ARC write completion, which walks L2ARC buflists
117  *	- ARC header destruction, as it removes from L2ARC buflists
118  *	- ARC header release, as it removes from L2ARC buflists
119  */
120 
121 #include <sys/spa.h>
122 #include <sys/zio.h>
123 #include <sys/zfs_context.h>
124 #include <sys/arc.h>
125 #include <sys/refcount.h>
126 #include <sys/vdev.h>
127 #include <sys/vdev_impl.h>
128 #ifdef _KERNEL
129 #include <sys/vmsystm.h>
130 #include <vm/anon.h>
131 #include <sys/fs/swapnode.h>
132 #include <sys/dnlc.h>
133 #endif
134 #include <sys/callb.h>
135 #include <sys/kstat.h>
136 #include <zfs_fletcher.h>
137 
138 #ifndef _KERNEL
139 /* set with ZFS_DEBUG=watch, to enable watchpoints on frozen buffers */
140 boolean_t arc_watch = B_FALSE;
141 int arc_procfd;
142 #endif
143 
144 static kmutex_t		arc_reclaim_thr_lock;
145 static kcondvar_t	arc_reclaim_thr_cv;	/* used to signal reclaim thr */
146 static uint8_t		arc_thread_exit;
147 
148 extern int zfs_write_limit_shift;
149 extern uint64_t zfs_write_limit_max;
150 extern kmutex_t zfs_write_limit_lock;
151 
152 #define	ARC_REDUCE_DNLC_PERCENT	3
153 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
154 
155 typedef enum arc_reclaim_strategy {
156 	ARC_RECLAIM_AGGR,		/* Aggressive reclaim strategy */
157 	ARC_RECLAIM_CONS		/* Conservative reclaim strategy */
158 } arc_reclaim_strategy_t;
159 
160 /* number of seconds before growing cache again */
161 static int		arc_grow_retry = 60;
162 
163 /* shift of arc_c for calculating both min and max arc_p */
164 static int		arc_p_min_shift = 4;
165 
166 /* log2(fraction of arc to reclaim) */
167 static int		arc_shrink_shift = 5;
168 
169 /*
170  * minimum lifespan of a prefetch block in clock ticks
171  * (initialized in arc_init())
172  */
173 static int		arc_min_prefetch_lifespan;
174 
175 static int arc_dead;
176 
177 /*
178  * The arc has filled available memory and has now warmed up.
179  */
180 static boolean_t arc_warm;
181 
182 /*
183  * These tunables are for performance analysis.
184  */
185 uint64_t zfs_arc_max;
186 uint64_t zfs_arc_min;
187 uint64_t zfs_arc_meta_limit = 0;
188 int zfs_arc_grow_retry = 0;
189 int zfs_arc_shrink_shift = 0;
190 int zfs_arc_p_min_shift = 0;
191 int zfs_disable_dup_eviction = 0;
192 
193 /*
194  * Note that buffers can be in one of 6 states:
195  *	ARC_anon	- anonymous (discussed below)
196  *	ARC_mru		- recently used, currently cached
197  *	ARC_mru_ghost	- recentely used, no longer in cache
198  *	ARC_mfu		- frequently used, currently cached
199  *	ARC_mfu_ghost	- frequently used, no longer in cache
200  *	ARC_l2c_only	- exists in L2ARC but not other states
201  * When there are no active references to the buffer, they are
202  * are linked onto a list in one of these arc states.  These are
203  * the only buffers that can be evicted or deleted.  Within each
204  * state there are multiple lists, one for meta-data and one for
205  * non-meta-data.  Meta-data (indirect blocks, blocks of dnodes,
206  * etc.) is tracked separately so that it can be managed more
207  * explicitly: favored over data, limited explicitly.
208  *
209  * Anonymous buffers are buffers that are not associated with
210  * a DVA.  These are buffers that hold dirty block copies
211  * before they are written to stable storage.  By definition,
212  * they are "ref'd" and are considered part of arc_mru
213  * that cannot be freed.  Generally, they will aquire a DVA
214  * as they are written and migrate onto the arc_mru list.
215  *
216  * The ARC_l2c_only state is for buffers that are in the second
217  * level ARC but no longer in any of the ARC_m* lists.  The second
218  * level ARC itself may also contain buffers that are in any of
219  * the ARC_m* states - meaning that a buffer can exist in two
220  * places.  The reason for the ARC_l2c_only state is to keep the
221  * buffer header in the hash table, so that reads that hit the
222  * second level ARC benefit from these fast lookups.
223  */
224 
225 typedef struct arc_state {
226 	list_t	arcs_list[ARC_BUFC_NUMTYPES];	/* list of evictable buffers */
227 	uint64_t arcs_lsize[ARC_BUFC_NUMTYPES];	/* amount of evictable data */
228 	uint64_t arcs_size;	/* total amount of data in this state */
229 	kmutex_t arcs_mtx;
230 } arc_state_t;
231 
232 /* The 6 states: */
233 static arc_state_t ARC_anon;
234 static arc_state_t ARC_mru;
235 static arc_state_t ARC_mru_ghost;
236 static arc_state_t ARC_mfu;
237 static arc_state_t ARC_mfu_ghost;
238 static arc_state_t ARC_l2c_only;
239 
240 typedef struct arc_stats {
241 	kstat_named_t arcstat_hits;
242 	kstat_named_t arcstat_misses;
243 	kstat_named_t arcstat_demand_data_hits;
244 	kstat_named_t arcstat_demand_data_misses;
245 	kstat_named_t arcstat_demand_metadata_hits;
246 	kstat_named_t arcstat_demand_metadata_misses;
247 	kstat_named_t arcstat_prefetch_data_hits;
248 	kstat_named_t arcstat_prefetch_data_misses;
249 	kstat_named_t arcstat_prefetch_metadata_hits;
250 	kstat_named_t arcstat_prefetch_metadata_misses;
251 	kstat_named_t arcstat_mru_hits;
252 	kstat_named_t arcstat_mru_ghost_hits;
253 	kstat_named_t arcstat_mfu_hits;
254 	kstat_named_t arcstat_mfu_ghost_hits;
255 	kstat_named_t arcstat_deleted;
256 	kstat_named_t arcstat_recycle_miss;
257 	kstat_named_t arcstat_mutex_miss;
258 	kstat_named_t arcstat_evict_skip;
259 	kstat_named_t arcstat_evict_l2_cached;
260 	kstat_named_t arcstat_evict_l2_eligible;
261 	kstat_named_t arcstat_evict_l2_ineligible;
262 	kstat_named_t arcstat_hash_elements;
263 	kstat_named_t arcstat_hash_elements_max;
264 	kstat_named_t arcstat_hash_collisions;
265 	kstat_named_t arcstat_hash_chains;
266 	kstat_named_t arcstat_hash_chain_max;
267 	kstat_named_t arcstat_p;
268 	kstat_named_t arcstat_c;
269 	kstat_named_t arcstat_c_min;
270 	kstat_named_t arcstat_c_max;
271 	kstat_named_t arcstat_size;
272 	kstat_named_t arcstat_hdr_size;
273 	kstat_named_t arcstat_data_size;
274 	kstat_named_t arcstat_other_size;
275 	kstat_named_t arcstat_l2_hits;
276 	kstat_named_t arcstat_l2_misses;
277 	kstat_named_t arcstat_l2_feeds;
278 	kstat_named_t arcstat_l2_rw_clash;
279 	kstat_named_t arcstat_l2_read_bytes;
280 	kstat_named_t arcstat_l2_write_bytes;
281 	kstat_named_t arcstat_l2_writes_sent;
282 	kstat_named_t arcstat_l2_writes_done;
283 	kstat_named_t arcstat_l2_writes_error;
284 	kstat_named_t arcstat_l2_writes_hdr_miss;
285 	kstat_named_t arcstat_l2_evict_lock_retry;
286 	kstat_named_t arcstat_l2_evict_reading;
287 	kstat_named_t arcstat_l2_free_on_write;
288 	kstat_named_t arcstat_l2_abort_lowmem;
289 	kstat_named_t arcstat_l2_cksum_bad;
290 	kstat_named_t arcstat_l2_io_error;
291 	kstat_named_t arcstat_l2_size;
292 	kstat_named_t arcstat_l2_hdr_size;
293 	kstat_named_t arcstat_memory_throttle_count;
294 	kstat_named_t arcstat_duplicate_buffers;
295 	kstat_named_t arcstat_duplicate_buffers_size;
296 	kstat_named_t arcstat_duplicate_reads;
297 } arc_stats_t;
298 
299 static arc_stats_t arc_stats = {
300 	{ "hits",			KSTAT_DATA_UINT64 },
301 	{ "misses",			KSTAT_DATA_UINT64 },
302 	{ "demand_data_hits",		KSTAT_DATA_UINT64 },
303 	{ "demand_data_misses",		KSTAT_DATA_UINT64 },
304 	{ "demand_metadata_hits",	KSTAT_DATA_UINT64 },
305 	{ "demand_metadata_misses",	KSTAT_DATA_UINT64 },
306 	{ "prefetch_data_hits",		KSTAT_DATA_UINT64 },
307 	{ "prefetch_data_misses",	KSTAT_DATA_UINT64 },
308 	{ "prefetch_metadata_hits",	KSTAT_DATA_UINT64 },
309 	{ "prefetch_metadata_misses",	KSTAT_DATA_UINT64 },
310 	{ "mru_hits",			KSTAT_DATA_UINT64 },
311 	{ "mru_ghost_hits",		KSTAT_DATA_UINT64 },
312 	{ "mfu_hits",			KSTAT_DATA_UINT64 },
313 	{ "mfu_ghost_hits",		KSTAT_DATA_UINT64 },
314 	{ "deleted",			KSTAT_DATA_UINT64 },
315 	{ "recycle_miss",		KSTAT_DATA_UINT64 },
316 	{ "mutex_miss",			KSTAT_DATA_UINT64 },
317 	{ "evict_skip",			KSTAT_DATA_UINT64 },
318 	{ "evict_l2_cached",		KSTAT_DATA_UINT64 },
319 	{ "evict_l2_eligible",		KSTAT_DATA_UINT64 },
320 	{ "evict_l2_ineligible",	KSTAT_DATA_UINT64 },
321 	{ "hash_elements",		KSTAT_DATA_UINT64 },
322 	{ "hash_elements_max",		KSTAT_DATA_UINT64 },
323 	{ "hash_collisions",		KSTAT_DATA_UINT64 },
324 	{ "hash_chains",		KSTAT_DATA_UINT64 },
325 	{ "hash_chain_max",		KSTAT_DATA_UINT64 },
326 	{ "p",				KSTAT_DATA_UINT64 },
327 	{ "c",				KSTAT_DATA_UINT64 },
328 	{ "c_min",			KSTAT_DATA_UINT64 },
329 	{ "c_max",			KSTAT_DATA_UINT64 },
330 	{ "size",			KSTAT_DATA_UINT64 },
331 	{ "hdr_size",			KSTAT_DATA_UINT64 },
332 	{ "data_size",			KSTAT_DATA_UINT64 },
333 	{ "other_size",			KSTAT_DATA_UINT64 },
334 	{ "l2_hits",			KSTAT_DATA_UINT64 },
335 	{ "l2_misses",			KSTAT_DATA_UINT64 },
336 	{ "l2_feeds",			KSTAT_DATA_UINT64 },
337 	{ "l2_rw_clash",		KSTAT_DATA_UINT64 },
338 	{ "l2_read_bytes",		KSTAT_DATA_UINT64 },
339 	{ "l2_write_bytes",		KSTAT_DATA_UINT64 },
340 	{ "l2_writes_sent",		KSTAT_DATA_UINT64 },
341 	{ "l2_writes_done",		KSTAT_DATA_UINT64 },
342 	{ "l2_writes_error",		KSTAT_DATA_UINT64 },
343 	{ "l2_writes_hdr_miss",		KSTAT_DATA_UINT64 },
344 	{ "l2_evict_lock_retry",	KSTAT_DATA_UINT64 },
345 	{ "l2_evict_reading",		KSTAT_DATA_UINT64 },
346 	{ "l2_free_on_write",		KSTAT_DATA_UINT64 },
347 	{ "l2_abort_lowmem",		KSTAT_DATA_UINT64 },
348 	{ "l2_cksum_bad",		KSTAT_DATA_UINT64 },
349 	{ "l2_io_error",		KSTAT_DATA_UINT64 },
350 	{ "l2_size",			KSTAT_DATA_UINT64 },
351 	{ "l2_hdr_size",		KSTAT_DATA_UINT64 },
352 	{ "memory_throttle_count",	KSTAT_DATA_UINT64 },
353 	{ "duplicate_buffers",		KSTAT_DATA_UINT64 },
354 	{ "duplicate_buffers_size",	KSTAT_DATA_UINT64 },
355 	{ "duplicate_reads",		KSTAT_DATA_UINT64 }
356 };
357 
358 #define	ARCSTAT(stat)	(arc_stats.stat.value.ui64)
359 
360 #define	ARCSTAT_INCR(stat, val) \
361 	atomic_add_64(&arc_stats.stat.value.ui64, (val));
362 
363 #define	ARCSTAT_BUMP(stat)	ARCSTAT_INCR(stat, 1)
364 #define	ARCSTAT_BUMPDOWN(stat)	ARCSTAT_INCR(stat, -1)
365 
366 #define	ARCSTAT_MAX(stat, val) {					\
367 	uint64_t m;							\
368 	while ((val) > (m = arc_stats.stat.value.ui64) &&		\
369 	    (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val))))	\
370 		continue;						\
371 }
372 
373 #define	ARCSTAT_MAXSTAT(stat) \
374 	ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
375 
376 /*
377  * We define a macro to allow ARC hits/misses to be easily broken down by
378  * two separate conditions, giving a total of four different subtypes for
379  * each of hits and misses (so eight statistics total).
380  */
381 #define	ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
382 	if (cond1) {							\
383 		if (cond2) {						\
384 			ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
385 		} else {						\
386 			ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
387 		}							\
388 	} else {							\
389 		if (cond2) {						\
390 			ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
391 		} else {						\
392 			ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
393 		}							\
394 	}
395 
396 kstat_t			*arc_ksp;
397 static arc_state_t	*arc_anon;
398 static arc_state_t	*arc_mru;
399 static arc_state_t	*arc_mru_ghost;
400 static arc_state_t	*arc_mfu;
401 static arc_state_t	*arc_mfu_ghost;
402 static arc_state_t	*arc_l2c_only;
403 
404 /*
405  * There are several ARC variables that are critical to export as kstats --
406  * but we don't want to have to grovel around in the kstat whenever we wish to
407  * manipulate them.  For these variables, we therefore define them to be in
408  * terms of the statistic variable.  This assures that we are not introducing
409  * the possibility of inconsistency by having shadow copies of the variables,
410  * while still allowing the code to be readable.
411  */
412 #define	arc_size	ARCSTAT(arcstat_size)	/* actual total arc size */
413 #define	arc_p		ARCSTAT(arcstat_p)	/* target size of MRU */
414 #define	arc_c		ARCSTAT(arcstat_c)	/* target size of cache */
415 #define	arc_c_min	ARCSTAT(arcstat_c_min)	/* min target cache size */
416 #define	arc_c_max	ARCSTAT(arcstat_c_max)	/* max target cache size */
417 
418 static int		arc_no_grow;	/* Don't try to grow cache size */
419 static uint64_t		arc_tempreserve;
420 static uint64_t		arc_loaned_bytes;
421 static uint64_t		arc_meta_used;
422 static uint64_t		arc_meta_limit;
423 static uint64_t		arc_meta_max = 0;
424 
425 typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
426 
427 typedef struct arc_callback arc_callback_t;
428 
429 struct arc_callback {
430 	void			*acb_private;
431 	arc_done_func_t		*acb_done;
432 	arc_buf_t		*acb_buf;
433 	zio_t			*acb_zio_dummy;
434 	arc_callback_t		*acb_next;
435 };
436 
437 typedef struct arc_write_callback arc_write_callback_t;
438 
439 struct arc_write_callback {
440 	void		*awcb_private;
441 	arc_done_func_t	*awcb_ready;
442 	arc_done_func_t	*awcb_done;
443 	arc_buf_t	*awcb_buf;
444 };
445 
446 struct arc_buf_hdr {
447 	/* protected by hash lock */
448 	dva_t			b_dva;
449 	uint64_t		b_birth;
450 	uint64_t		b_cksum0;
451 
452 	kmutex_t		b_freeze_lock;
453 	zio_cksum_t		*b_freeze_cksum;
454 	void			*b_thawed;
455 
456 	arc_buf_hdr_t		*b_hash_next;
457 	arc_buf_t		*b_buf;
458 	uint32_t		b_flags;
459 	uint32_t		b_datacnt;
460 
461 	arc_callback_t		*b_acb;
462 	kcondvar_t		b_cv;
463 
464 	/* immutable */
465 	arc_buf_contents_t	b_type;
466 	uint64_t		b_size;
467 	uint64_t		b_spa;
468 
469 	/* protected by arc state mutex */
470 	arc_state_t		*b_state;
471 	list_node_t		b_arc_node;
472 
473 	/* updated atomically */
474 	clock_t			b_arc_access;
475 
476 	/* self protecting */
477 	refcount_t		b_refcnt;
478 
479 	l2arc_buf_hdr_t		*b_l2hdr;
480 	list_node_t		b_l2node;
481 };
482 
483 static arc_buf_t *arc_eviction_list;
484 static kmutex_t arc_eviction_mtx;
485 static arc_buf_hdr_t arc_eviction_hdr;
486 static void arc_get_data_buf(arc_buf_t *buf);
487 static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
488 static int arc_evict_needed(arc_buf_contents_t type);
489 static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
490 static void arc_buf_watch(arc_buf_t *buf);
491 
492 static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
493 
494 #define	GHOST_STATE(state)	\
495 	((state) == arc_mru_ghost || (state) == arc_mfu_ghost ||	\
496 	(state) == arc_l2c_only)
497 
498 /*
499  * Private ARC flags.  These flags are private ARC only flags that will show up
500  * in b_flags in the arc_hdr_buf_t.  Some flags are publicly declared, and can
501  * be passed in as arc_flags in things like arc_read.  However, these flags
502  * should never be passed and should only be set by ARC code.  When adding new
503  * public flags, make sure not to smash the private ones.
504  */
505 
506 #define	ARC_IN_HASH_TABLE	(1 << 9)	/* this buffer is hashed */
507 #define	ARC_IO_IN_PROGRESS	(1 << 10)	/* I/O in progress for buf */
508 #define	ARC_IO_ERROR		(1 << 11)	/* I/O failed for buf */
509 #define	ARC_FREED_IN_READ	(1 << 12)	/* buf freed while in read */
510 #define	ARC_BUF_AVAILABLE	(1 << 13)	/* block not in active use */
511 #define	ARC_INDIRECT		(1 << 14)	/* this is an indirect block */
512 #define	ARC_FREE_IN_PROGRESS	(1 << 15)	/* hdr about to be freed */
513 #define	ARC_L2_WRITING		(1 << 16)	/* L2ARC write in progress */
514 #define	ARC_L2_EVICTED		(1 << 17)	/* evicted during I/O */
515 #define	ARC_L2_WRITE_HEAD	(1 << 18)	/* head of write list */
516 
517 #define	HDR_IN_HASH_TABLE(hdr)	((hdr)->b_flags & ARC_IN_HASH_TABLE)
518 #define	HDR_IO_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS)
519 #define	HDR_IO_ERROR(hdr)	((hdr)->b_flags & ARC_IO_ERROR)
520 #define	HDR_PREFETCH(hdr)	((hdr)->b_flags & ARC_PREFETCH)
521 #define	HDR_FREED_IN_READ(hdr)	((hdr)->b_flags & ARC_FREED_IN_READ)
522 #define	HDR_BUF_AVAILABLE(hdr)	((hdr)->b_flags & ARC_BUF_AVAILABLE)
523 #define	HDR_FREE_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
524 #define	HDR_L2CACHE(hdr)	((hdr)->b_flags & ARC_L2CACHE)
525 #define	HDR_L2_READING(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS &&	\
526 				    (hdr)->b_l2hdr != NULL)
527 #define	HDR_L2_WRITING(hdr)	((hdr)->b_flags & ARC_L2_WRITING)
528 #define	HDR_L2_EVICTED(hdr)	((hdr)->b_flags & ARC_L2_EVICTED)
529 #define	HDR_L2_WRITE_HEAD(hdr)	((hdr)->b_flags & ARC_L2_WRITE_HEAD)
530 
531 /*
532  * Other sizes
533  */
534 
535 #define	HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
536 #define	L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
537 
538 /*
539  * Hash table routines
540  */
541 
542 #define	HT_LOCK_PAD	64
543 
544 struct ht_lock {
545 	kmutex_t	ht_lock;
546 #ifdef _KERNEL
547 	unsigned char	pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
548 #endif
549 };
550 
551 #define	BUF_LOCKS 256
552 typedef struct buf_hash_table {
553 	uint64_t ht_mask;
554 	arc_buf_hdr_t **ht_table;
555 	struct ht_lock ht_locks[BUF_LOCKS];
556 } buf_hash_table_t;
557 
558 static buf_hash_table_t buf_hash_table;
559 
560 #define	BUF_HASH_INDEX(spa, dva, birth) \
561 	(buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
562 #define	BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
563 #define	BUF_HASH_LOCK(idx)	(&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
564 #define	HDR_LOCK(hdr) \
565 	(BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
566 
567 uint64_t zfs_crc64_table[256];
568 
569 /*
570  * Level 2 ARC
571  */
572 
573 #define	L2ARC_WRITE_SIZE	(8 * 1024 * 1024)	/* initial write max */
574 #define	L2ARC_HEADROOM		2		/* num of writes */
575 #define	L2ARC_FEED_SECS		1		/* caching interval secs */
576 #define	L2ARC_FEED_MIN_MS	200		/* min caching interval ms */
577 
578 #define	l2arc_writes_sent	ARCSTAT(arcstat_l2_writes_sent)
579 #define	l2arc_writes_done	ARCSTAT(arcstat_l2_writes_done)
580 
581 /*
582  * L2ARC Performance Tunables
583  */
584 uint64_t l2arc_write_max = L2ARC_WRITE_SIZE;	/* default max write size */
585 uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE;	/* extra write during warmup */
586 uint64_t l2arc_headroom = L2ARC_HEADROOM;	/* number of dev writes */
587 uint64_t l2arc_feed_secs = L2ARC_FEED_SECS;	/* interval seconds */
588 uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS;	/* min interval milliseconds */
589 boolean_t l2arc_noprefetch = B_TRUE;		/* don't cache prefetch bufs */
590 boolean_t l2arc_feed_again = B_TRUE;		/* turbo warmup */
591 boolean_t l2arc_norw = B_TRUE;			/* no reads during writes */
592 
593 /*
594  * L2ARC Internals
595  */
596 typedef struct l2arc_dev {
597 	vdev_t			*l2ad_vdev;	/* vdev */
598 	spa_t			*l2ad_spa;	/* spa */
599 	uint64_t		l2ad_hand;	/* next write location */
600 	uint64_t		l2ad_write;	/* desired write size, bytes */
601 	uint64_t		l2ad_boost;	/* warmup write boost, bytes */
602 	uint64_t		l2ad_start;	/* first addr on device */
603 	uint64_t		l2ad_end;	/* last addr on device */
604 	uint64_t		l2ad_evict;	/* last addr eviction reached */
605 	boolean_t		l2ad_first;	/* first sweep through */
606 	boolean_t		l2ad_writing;	/* currently writing */
607 	list_t			*l2ad_buflist;	/* buffer list */
608 	list_node_t		l2ad_node;	/* device list node */
609 } l2arc_dev_t;
610 
611 static list_t L2ARC_dev_list;			/* device list */
612 static list_t *l2arc_dev_list;			/* device list pointer */
613 static kmutex_t l2arc_dev_mtx;			/* device list mutex */
614 static l2arc_dev_t *l2arc_dev_last;		/* last device used */
615 static kmutex_t l2arc_buflist_mtx;		/* mutex for all buflists */
616 static list_t L2ARC_free_on_write;		/* free after write buf list */
617 static list_t *l2arc_free_on_write;		/* free after write list ptr */
618 static kmutex_t l2arc_free_on_write_mtx;	/* mutex for list */
619 static uint64_t l2arc_ndev;			/* number of devices */
620 
621 typedef struct l2arc_read_callback {
622 	arc_buf_t	*l2rcb_buf;		/* read buffer */
623 	spa_t		*l2rcb_spa;		/* spa */
624 	blkptr_t	l2rcb_bp;		/* original blkptr */
625 	zbookmark_t	l2rcb_zb;		/* original bookmark */
626 	int		l2rcb_flags;		/* original flags */
627 } l2arc_read_callback_t;
628 
629 typedef struct l2arc_write_callback {
630 	l2arc_dev_t	*l2wcb_dev;		/* device info */
631 	arc_buf_hdr_t	*l2wcb_head;		/* head of write buflist */
632 } l2arc_write_callback_t;
633 
634 struct l2arc_buf_hdr {
635 	/* protected by arc_buf_hdr  mutex */
636 	l2arc_dev_t	*b_dev;			/* L2ARC device */
637 	uint64_t	b_daddr;		/* disk address, offset byte */
638 };
639 
640 typedef struct l2arc_data_free {
641 	/* protected by l2arc_free_on_write_mtx */
642 	void		*l2df_data;
643 	size_t		l2df_size;
644 	void		(*l2df_func)(void *, size_t);
645 	list_node_t	l2df_list_node;
646 } l2arc_data_free_t;
647 
648 static kmutex_t l2arc_feed_thr_lock;
649 static kcondvar_t l2arc_feed_thr_cv;
650 static uint8_t l2arc_thread_exit;
651 
652 static void l2arc_read_done(zio_t *zio);
653 static void l2arc_hdr_stat_add(void);
654 static void l2arc_hdr_stat_remove(void);
655 
656 static uint64_t
657 buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
658 {
659 	uint8_t *vdva = (uint8_t *)dva;
660 	uint64_t crc = -1ULL;
661 	int i;
662 
663 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
664 
665 	for (i = 0; i < sizeof (dva_t); i++)
666 		crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
667 
668 	crc ^= (spa>>8) ^ birth;
669 
670 	return (crc);
671 }
672 
673 #define	BUF_EMPTY(buf)						\
674 	((buf)->b_dva.dva_word[0] == 0 &&			\
675 	(buf)->b_dva.dva_word[1] == 0 &&			\
676 	(buf)->b_birth == 0)
677 
678 #define	BUF_EQUAL(spa, dva, birth, buf)				\
679 	((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) &&	\
680 	((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) &&	\
681 	((buf)->b_birth == birth) && ((buf)->b_spa == spa)
682 
683 static void
684 buf_discard_identity(arc_buf_hdr_t *hdr)
685 {
686 	hdr->b_dva.dva_word[0] = 0;
687 	hdr->b_dva.dva_word[1] = 0;
688 	hdr->b_birth = 0;
689 	hdr->b_cksum0 = 0;
690 }
691 
692 static arc_buf_hdr_t *
693 buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp)
694 {
695 	uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
696 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
697 	arc_buf_hdr_t *buf;
698 
699 	mutex_enter(hash_lock);
700 	for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
701 	    buf = buf->b_hash_next) {
702 		if (BUF_EQUAL(spa, dva, birth, buf)) {
703 			*lockp = hash_lock;
704 			return (buf);
705 		}
706 	}
707 	mutex_exit(hash_lock);
708 	*lockp = NULL;
709 	return (NULL);
710 }
711 
712 /*
713  * Insert an entry into the hash table.  If there is already an element
714  * equal to elem in the hash table, then the already existing element
715  * will be returned and the new element will not be inserted.
716  * Otherwise returns NULL.
717  */
718 static arc_buf_hdr_t *
719 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
720 {
721 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
722 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
723 	arc_buf_hdr_t *fbuf;
724 	uint32_t i;
725 
726 	ASSERT(!HDR_IN_HASH_TABLE(buf));
727 	*lockp = hash_lock;
728 	mutex_enter(hash_lock);
729 	for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
730 	    fbuf = fbuf->b_hash_next, i++) {
731 		if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
732 			return (fbuf);
733 	}
734 
735 	buf->b_hash_next = buf_hash_table.ht_table[idx];
736 	buf_hash_table.ht_table[idx] = buf;
737 	buf->b_flags |= ARC_IN_HASH_TABLE;
738 
739 	/* collect some hash table performance data */
740 	if (i > 0) {
741 		ARCSTAT_BUMP(arcstat_hash_collisions);
742 		if (i == 1)
743 			ARCSTAT_BUMP(arcstat_hash_chains);
744 
745 		ARCSTAT_MAX(arcstat_hash_chain_max, i);
746 	}
747 
748 	ARCSTAT_BUMP(arcstat_hash_elements);
749 	ARCSTAT_MAXSTAT(arcstat_hash_elements);
750 
751 	return (NULL);
752 }
753 
754 static void
755 buf_hash_remove(arc_buf_hdr_t *buf)
756 {
757 	arc_buf_hdr_t *fbuf, **bufp;
758 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
759 
760 	ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
761 	ASSERT(HDR_IN_HASH_TABLE(buf));
762 
763 	bufp = &buf_hash_table.ht_table[idx];
764 	while ((fbuf = *bufp) != buf) {
765 		ASSERT(fbuf != NULL);
766 		bufp = &fbuf->b_hash_next;
767 	}
768 	*bufp = buf->b_hash_next;
769 	buf->b_hash_next = NULL;
770 	buf->b_flags &= ~ARC_IN_HASH_TABLE;
771 
772 	/* collect some hash table performance data */
773 	ARCSTAT_BUMPDOWN(arcstat_hash_elements);
774 
775 	if (buf_hash_table.ht_table[idx] &&
776 	    buf_hash_table.ht_table[idx]->b_hash_next == NULL)
777 		ARCSTAT_BUMPDOWN(arcstat_hash_chains);
778 }
779 
780 /*
781  * Global data structures and functions for the buf kmem cache.
782  */
783 static kmem_cache_t *hdr_cache;
784 static kmem_cache_t *buf_cache;
785 
786 static void
787 buf_fini(void)
788 {
789 	int i;
790 
791 	kmem_free(buf_hash_table.ht_table,
792 	    (buf_hash_table.ht_mask + 1) * sizeof (void *));
793 	for (i = 0; i < BUF_LOCKS; i++)
794 		mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
795 	kmem_cache_destroy(hdr_cache);
796 	kmem_cache_destroy(buf_cache);
797 }
798 
799 /*
800  * Constructor callback - called when the cache is empty
801  * and a new buf is requested.
802  */
803 /* ARGSUSED */
804 static int
805 hdr_cons(void *vbuf, void *unused, int kmflag)
806 {
807 	arc_buf_hdr_t *buf = vbuf;
808 
809 	bzero(buf, sizeof (arc_buf_hdr_t));
810 	refcount_create(&buf->b_refcnt);
811 	cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
812 	mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
813 	arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
814 
815 	return (0);
816 }
817 
818 /* ARGSUSED */
819 static int
820 buf_cons(void *vbuf, void *unused, int kmflag)
821 {
822 	arc_buf_t *buf = vbuf;
823 
824 	bzero(buf, sizeof (arc_buf_t));
825 	mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
826 	arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
827 
828 	return (0);
829 }
830 
831 /*
832  * Destructor callback - called when a cached buf is
833  * no longer required.
834  */
835 /* ARGSUSED */
836 static void
837 hdr_dest(void *vbuf, void *unused)
838 {
839 	arc_buf_hdr_t *buf = vbuf;
840 
841 	ASSERT(BUF_EMPTY(buf));
842 	refcount_destroy(&buf->b_refcnt);
843 	cv_destroy(&buf->b_cv);
844 	mutex_destroy(&buf->b_freeze_lock);
845 	arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
846 }
847 
848 /* ARGSUSED */
849 static void
850 buf_dest(void *vbuf, void *unused)
851 {
852 	arc_buf_t *buf = vbuf;
853 
854 	mutex_destroy(&buf->b_evict_lock);
855 	arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
856 }
857 
858 /*
859  * Reclaim callback -- invoked when memory is low.
860  */
861 /* ARGSUSED */
862 static void
863 hdr_recl(void *unused)
864 {
865 	dprintf("hdr_recl called\n");
866 	/*
867 	 * umem calls the reclaim func when we destroy the buf cache,
868 	 * which is after we do arc_fini().
869 	 */
870 	if (!arc_dead)
871 		cv_signal(&arc_reclaim_thr_cv);
872 }
873 
874 static void
875 buf_init(void)
876 {
877 	uint64_t *ct;
878 	uint64_t hsize = 1ULL << 12;
879 	int i, j;
880 
881 	/*
882 	 * The hash table is big enough to fill all of physical memory
883 	 * with an average 64K block size.  The table will take up
884 	 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
885 	 */
886 	while (hsize * 65536 < physmem * PAGESIZE)
887 		hsize <<= 1;
888 retry:
889 	buf_hash_table.ht_mask = hsize - 1;
890 	buf_hash_table.ht_table =
891 	    kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
892 	if (buf_hash_table.ht_table == NULL) {
893 		ASSERT(hsize > (1ULL << 8));
894 		hsize >>= 1;
895 		goto retry;
896 	}
897 
898 	hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
899 	    0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
900 	buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
901 	    0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
902 
903 	for (i = 0; i < 256; i++)
904 		for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
905 			*ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
906 
907 	for (i = 0; i < BUF_LOCKS; i++) {
908 		mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
909 		    NULL, MUTEX_DEFAULT, NULL);
910 	}
911 }
912 
913 #define	ARC_MINTIME	(hz>>4) /* 62 ms */
914 
915 static void
916 arc_cksum_verify(arc_buf_t *buf)
917 {
918 	zio_cksum_t zc;
919 
920 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
921 		return;
922 
923 	mutex_enter(&buf->b_hdr->b_freeze_lock);
924 	if (buf->b_hdr->b_freeze_cksum == NULL ||
925 	    (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
926 		mutex_exit(&buf->b_hdr->b_freeze_lock);
927 		return;
928 	}
929 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
930 	if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
931 		panic("buffer modified while frozen!");
932 	mutex_exit(&buf->b_hdr->b_freeze_lock);
933 }
934 
935 static int
936 arc_cksum_equal(arc_buf_t *buf)
937 {
938 	zio_cksum_t zc;
939 	int equal;
940 
941 	mutex_enter(&buf->b_hdr->b_freeze_lock);
942 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
943 	equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
944 	mutex_exit(&buf->b_hdr->b_freeze_lock);
945 
946 	return (equal);
947 }
948 
949 static void
950 arc_cksum_compute(arc_buf_t *buf, boolean_t force)
951 {
952 	if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
953 		return;
954 
955 	mutex_enter(&buf->b_hdr->b_freeze_lock);
956 	if (buf->b_hdr->b_freeze_cksum != NULL) {
957 		mutex_exit(&buf->b_hdr->b_freeze_lock);
958 		return;
959 	}
960 	buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
961 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
962 	    buf->b_hdr->b_freeze_cksum);
963 	mutex_exit(&buf->b_hdr->b_freeze_lock);
964 	arc_buf_watch(buf);
965 }
966 
967 #ifndef _KERNEL
968 typedef struct procctl {
969 	long cmd;
970 	prwatch_t prwatch;
971 } procctl_t;
972 #endif
973 
974 /* ARGSUSED */
975 static void
976 arc_buf_unwatch(arc_buf_t *buf)
977 {
978 #ifndef _KERNEL
979 	if (arc_watch) {
980 		int result;
981 		procctl_t ctl;
982 		ctl.cmd = PCWATCH;
983 		ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data;
984 		ctl.prwatch.pr_size = 0;
985 		ctl.prwatch.pr_wflags = 0;
986 		result = write(arc_procfd, &ctl, sizeof (ctl));
987 		ASSERT3U(result, ==, sizeof (ctl));
988 	}
989 #endif
990 }
991 
992 /* ARGSUSED */
993 static void
994 arc_buf_watch(arc_buf_t *buf)
995 {
996 #ifndef _KERNEL
997 	if (arc_watch) {
998 		int result;
999 		procctl_t ctl;
1000 		ctl.cmd = PCWATCH;
1001 		ctl.prwatch.pr_vaddr = (uintptr_t)buf->b_data;
1002 		ctl.prwatch.pr_size = buf->b_hdr->b_size;
1003 		ctl.prwatch.pr_wflags = WA_WRITE;
1004 		result = write(arc_procfd, &ctl, sizeof (ctl));
1005 		ASSERT3U(result, ==, sizeof (ctl));
1006 	}
1007 #endif
1008 }
1009 
1010 void
1011 arc_buf_thaw(arc_buf_t *buf)
1012 {
1013 	if (zfs_flags & ZFS_DEBUG_MODIFY) {
1014 		if (buf->b_hdr->b_state != arc_anon)
1015 			panic("modifying non-anon buffer!");
1016 		if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
1017 			panic("modifying buffer while i/o in progress!");
1018 		arc_cksum_verify(buf);
1019 	}
1020 
1021 	mutex_enter(&buf->b_hdr->b_freeze_lock);
1022 	if (buf->b_hdr->b_freeze_cksum != NULL) {
1023 		kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1024 		buf->b_hdr->b_freeze_cksum = NULL;
1025 	}
1026 
1027 	if (zfs_flags & ZFS_DEBUG_MODIFY) {
1028 		if (buf->b_hdr->b_thawed)
1029 			kmem_free(buf->b_hdr->b_thawed, 1);
1030 		buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
1031 	}
1032 
1033 	mutex_exit(&buf->b_hdr->b_freeze_lock);
1034 
1035 	arc_buf_unwatch(buf);
1036 }
1037 
1038 void
1039 arc_buf_freeze(arc_buf_t *buf)
1040 {
1041 	kmutex_t *hash_lock;
1042 
1043 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
1044 		return;
1045 
1046 	hash_lock = HDR_LOCK(buf->b_hdr);
1047 	mutex_enter(hash_lock);
1048 
1049 	ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
1050 	    buf->b_hdr->b_state == arc_anon);
1051 	arc_cksum_compute(buf, B_FALSE);
1052 	mutex_exit(hash_lock);
1053 
1054 }
1055 
1056 static void
1057 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1058 {
1059 	ASSERT(MUTEX_HELD(hash_lock));
1060 
1061 	if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
1062 	    (ab->b_state != arc_anon)) {
1063 		uint64_t delta = ab->b_size * ab->b_datacnt;
1064 		list_t *list = &ab->b_state->arcs_list[ab->b_type];
1065 		uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1066 
1067 		ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
1068 		mutex_enter(&ab->b_state->arcs_mtx);
1069 		ASSERT(list_link_active(&ab->b_arc_node));
1070 		list_remove(list, ab);
1071 		if (GHOST_STATE(ab->b_state)) {
1072 			ASSERT0(ab->b_datacnt);
1073 			ASSERT3P(ab->b_buf, ==, NULL);
1074 			delta = ab->b_size;
1075 		}
1076 		ASSERT(delta > 0);
1077 		ASSERT3U(*size, >=, delta);
1078 		atomic_add_64(size, -delta);
1079 		mutex_exit(&ab->b_state->arcs_mtx);
1080 		/* remove the prefetch flag if we get a reference */
1081 		if (ab->b_flags & ARC_PREFETCH)
1082 			ab->b_flags &= ~ARC_PREFETCH;
1083 	}
1084 }
1085 
1086 static int
1087 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1088 {
1089 	int cnt;
1090 	arc_state_t *state = ab->b_state;
1091 
1092 	ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
1093 	ASSERT(!GHOST_STATE(state));
1094 
1095 	if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
1096 	    (state != arc_anon)) {
1097 		uint64_t *size = &state->arcs_lsize[ab->b_type];
1098 
1099 		ASSERT(!MUTEX_HELD(&state->arcs_mtx));
1100 		mutex_enter(&state->arcs_mtx);
1101 		ASSERT(!list_link_active(&ab->b_arc_node));
1102 		list_insert_head(&state->arcs_list[ab->b_type], ab);
1103 		ASSERT(ab->b_datacnt > 0);
1104 		atomic_add_64(size, ab->b_size * ab->b_datacnt);
1105 		mutex_exit(&state->arcs_mtx);
1106 	}
1107 	return (cnt);
1108 }
1109 
1110 /*
1111  * Move the supplied buffer to the indicated state.  The mutex
1112  * for the buffer must be held by the caller.
1113  */
1114 static void
1115 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1116 {
1117 	arc_state_t *old_state = ab->b_state;
1118 	int64_t refcnt = refcount_count(&ab->b_refcnt);
1119 	uint64_t from_delta, to_delta;
1120 
1121 	ASSERT(MUTEX_HELD(hash_lock));
1122 	ASSERT(new_state != old_state);
1123 	ASSERT(refcnt == 0 || ab->b_datacnt > 0);
1124 	ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
1125 	ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
1126 
1127 	from_delta = to_delta = ab->b_datacnt * ab->b_size;
1128 
1129 	/*
1130 	 * If this buffer is evictable, transfer it from the
1131 	 * old state list to the new state list.
1132 	 */
1133 	if (refcnt == 0) {
1134 		if (old_state != arc_anon) {
1135 			int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
1136 			uint64_t *size = &old_state->arcs_lsize[ab->b_type];
1137 
1138 			if (use_mutex)
1139 				mutex_enter(&old_state->arcs_mtx);
1140 
1141 			ASSERT(list_link_active(&ab->b_arc_node));
1142 			list_remove(&old_state->arcs_list[ab->b_type], ab);
1143 
1144 			/*
1145 			 * If prefetching out of the ghost cache,
1146 			 * we will have a non-zero datacnt.
1147 			 */
1148 			if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
1149 				/* ghost elements have a ghost size */
1150 				ASSERT(ab->b_buf == NULL);
1151 				from_delta = ab->b_size;
1152 			}
1153 			ASSERT3U(*size, >=, from_delta);
1154 			atomic_add_64(size, -from_delta);
1155 
1156 			if (use_mutex)
1157 				mutex_exit(&old_state->arcs_mtx);
1158 		}
1159 		if (new_state != arc_anon) {
1160 			int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
1161 			uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1162 
1163 			if (use_mutex)
1164 				mutex_enter(&new_state->arcs_mtx);
1165 
1166 			list_insert_head(&new_state->arcs_list[ab->b_type], ab);
1167 
1168 			/* ghost elements have a ghost size */
1169 			if (GHOST_STATE(new_state)) {
1170 				ASSERT(ab->b_datacnt == 0);
1171 				ASSERT(ab->b_buf == NULL);
1172 				to_delta = ab->b_size;
1173 			}
1174 			atomic_add_64(size, to_delta);
1175 
1176 			if (use_mutex)
1177 				mutex_exit(&new_state->arcs_mtx);
1178 		}
1179 	}
1180 
1181 	ASSERT(!BUF_EMPTY(ab));
1182 	if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1183 		buf_hash_remove(ab);
1184 
1185 	/* adjust state sizes */
1186 	if (to_delta)
1187 		atomic_add_64(&new_state->arcs_size, to_delta);
1188 	if (from_delta) {
1189 		ASSERT3U(old_state->arcs_size, >=, from_delta);
1190 		atomic_add_64(&old_state->arcs_size, -from_delta);
1191 	}
1192 	ab->b_state = new_state;
1193 
1194 	/* adjust l2arc hdr stats */
1195 	if (new_state == arc_l2c_only)
1196 		l2arc_hdr_stat_add();
1197 	else if (old_state == arc_l2c_only)
1198 		l2arc_hdr_stat_remove();
1199 }
1200 
1201 void
1202 arc_space_consume(uint64_t space, arc_space_type_t type)
1203 {
1204 	ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1205 
1206 	switch (type) {
1207 	case ARC_SPACE_DATA:
1208 		ARCSTAT_INCR(arcstat_data_size, space);
1209 		break;
1210 	case ARC_SPACE_OTHER:
1211 		ARCSTAT_INCR(arcstat_other_size, space);
1212 		break;
1213 	case ARC_SPACE_HDRS:
1214 		ARCSTAT_INCR(arcstat_hdr_size, space);
1215 		break;
1216 	case ARC_SPACE_L2HDRS:
1217 		ARCSTAT_INCR(arcstat_l2_hdr_size, space);
1218 		break;
1219 	}
1220 
1221 	atomic_add_64(&arc_meta_used, space);
1222 	atomic_add_64(&arc_size, space);
1223 }
1224 
1225 void
1226 arc_space_return(uint64_t space, arc_space_type_t type)
1227 {
1228 	ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
1229 
1230 	switch (type) {
1231 	case ARC_SPACE_DATA:
1232 		ARCSTAT_INCR(arcstat_data_size, -space);
1233 		break;
1234 	case ARC_SPACE_OTHER:
1235 		ARCSTAT_INCR(arcstat_other_size, -space);
1236 		break;
1237 	case ARC_SPACE_HDRS:
1238 		ARCSTAT_INCR(arcstat_hdr_size, -space);
1239 		break;
1240 	case ARC_SPACE_L2HDRS:
1241 		ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
1242 		break;
1243 	}
1244 
1245 	ASSERT(arc_meta_used >= space);
1246 	if (arc_meta_max < arc_meta_used)
1247 		arc_meta_max = arc_meta_used;
1248 	atomic_add_64(&arc_meta_used, -space);
1249 	ASSERT(arc_size >= space);
1250 	atomic_add_64(&arc_size, -space);
1251 }
1252 
1253 void *
1254 arc_data_buf_alloc(uint64_t size)
1255 {
1256 	if (arc_evict_needed(ARC_BUFC_DATA))
1257 		cv_signal(&arc_reclaim_thr_cv);
1258 	atomic_add_64(&arc_size, size);
1259 	return (zio_data_buf_alloc(size));
1260 }
1261 
1262 void
1263 arc_data_buf_free(void *buf, uint64_t size)
1264 {
1265 	zio_data_buf_free(buf, size);
1266 	ASSERT(arc_size >= size);
1267 	atomic_add_64(&arc_size, -size);
1268 }
1269 
1270 arc_buf_t *
1271 arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1272 {
1273 	arc_buf_hdr_t *hdr;
1274 	arc_buf_t *buf;
1275 
1276 	ASSERT3U(size, >, 0);
1277 	hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1278 	ASSERT(BUF_EMPTY(hdr));
1279 	hdr->b_size = size;
1280 	hdr->b_type = type;
1281 	hdr->b_spa = spa_load_guid(spa);
1282 	hdr->b_state = arc_anon;
1283 	hdr->b_arc_access = 0;
1284 	buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1285 	buf->b_hdr = hdr;
1286 	buf->b_data = NULL;
1287 	buf->b_efunc = NULL;
1288 	buf->b_private = NULL;
1289 	buf->b_next = NULL;
1290 	hdr->b_buf = buf;
1291 	arc_get_data_buf(buf);
1292 	hdr->b_datacnt = 1;
1293 	hdr->b_flags = 0;
1294 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
1295 	(void) refcount_add(&hdr->b_refcnt, tag);
1296 
1297 	return (buf);
1298 }
1299 
1300 static char *arc_onloan_tag = "onloan";
1301 
1302 /*
1303  * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
1304  * flight data by arc_tempreserve_space() until they are "returned". Loaned
1305  * buffers must be returned to the arc before they can be used by the DMU or
1306  * freed.
1307  */
1308 arc_buf_t *
1309 arc_loan_buf(spa_t *spa, int size)
1310 {
1311 	arc_buf_t *buf;
1312 
1313 	buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
1314 
1315 	atomic_add_64(&arc_loaned_bytes, size);
1316 	return (buf);
1317 }
1318 
1319 /*
1320  * Return a loaned arc buffer to the arc.
1321  */
1322 void
1323 arc_return_buf(arc_buf_t *buf, void *tag)
1324 {
1325 	arc_buf_hdr_t *hdr = buf->b_hdr;
1326 
1327 	ASSERT(buf->b_data != NULL);
1328 	(void) refcount_add(&hdr->b_refcnt, tag);
1329 	(void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
1330 
1331 	atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
1332 }
1333 
1334 /* Detach an arc_buf from a dbuf (tag) */
1335 void
1336 arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
1337 {
1338 	arc_buf_hdr_t *hdr;
1339 
1340 	ASSERT(buf->b_data != NULL);
1341 	hdr = buf->b_hdr;
1342 	(void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
1343 	(void) refcount_remove(&hdr->b_refcnt, tag);
1344 	buf->b_efunc = NULL;
1345 	buf->b_private = NULL;
1346 
1347 	atomic_add_64(&arc_loaned_bytes, hdr->b_size);
1348 }
1349 
1350 static arc_buf_t *
1351 arc_buf_clone(arc_buf_t *from)
1352 {
1353 	arc_buf_t *buf;
1354 	arc_buf_hdr_t *hdr = from->b_hdr;
1355 	uint64_t size = hdr->b_size;
1356 
1357 	ASSERT(hdr->b_state != arc_anon);
1358 
1359 	buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1360 	buf->b_hdr = hdr;
1361 	buf->b_data = NULL;
1362 	buf->b_efunc = NULL;
1363 	buf->b_private = NULL;
1364 	buf->b_next = hdr->b_buf;
1365 	hdr->b_buf = buf;
1366 	arc_get_data_buf(buf);
1367 	bcopy(from->b_data, buf->b_data, size);
1368 
1369 	/*
1370 	 * This buffer already exists in the arc so create a duplicate
1371 	 * copy for the caller.  If the buffer is associated with user data
1372 	 * then track the size and number of duplicates.  These stats will be
1373 	 * updated as duplicate buffers are created and destroyed.
1374 	 */
1375 	if (hdr->b_type == ARC_BUFC_DATA) {
1376 		ARCSTAT_BUMP(arcstat_duplicate_buffers);
1377 		ARCSTAT_INCR(arcstat_duplicate_buffers_size, size);
1378 	}
1379 	hdr->b_datacnt += 1;
1380 	return (buf);
1381 }
1382 
1383 void
1384 arc_buf_add_ref(arc_buf_t *buf, void* tag)
1385 {
1386 	arc_buf_hdr_t *hdr;
1387 	kmutex_t *hash_lock;
1388 
1389 	/*
1390 	 * Check to see if this buffer is evicted.  Callers
1391 	 * must verify b_data != NULL to know if the add_ref
1392 	 * was successful.
1393 	 */
1394 	mutex_enter(&buf->b_evict_lock);
1395 	if (buf->b_data == NULL) {
1396 		mutex_exit(&buf->b_evict_lock);
1397 		return;
1398 	}
1399 	hash_lock = HDR_LOCK(buf->b_hdr);
1400 	mutex_enter(hash_lock);
1401 	hdr = buf->b_hdr;
1402 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1403 	mutex_exit(&buf->b_evict_lock);
1404 
1405 	ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
1406 	add_reference(hdr, hash_lock, tag);
1407 	DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1408 	arc_access(hdr, hash_lock);
1409 	mutex_exit(hash_lock);
1410 	ARCSTAT_BUMP(arcstat_hits);
1411 	ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
1412 	    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
1413 	    data, metadata, hits);
1414 }
1415 
1416 /*
1417  * Free the arc data buffer.  If it is an l2arc write in progress,
1418  * the buffer is placed on l2arc_free_on_write to be freed later.
1419  */
1420 static void
1421 arc_buf_data_free(arc_buf_t *buf, void (*free_func)(void *, size_t))
1422 {
1423 	arc_buf_hdr_t *hdr = buf->b_hdr;
1424 
1425 	if (HDR_L2_WRITING(hdr)) {
1426 		l2arc_data_free_t *df;
1427 		df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
1428 		df->l2df_data = buf->b_data;
1429 		df->l2df_size = hdr->b_size;
1430 		df->l2df_func = free_func;
1431 		mutex_enter(&l2arc_free_on_write_mtx);
1432 		list_insert_head(l2arc_free_on_write, df);
1433 		mutex_exit(&l2arc_free_on_write_mtx);
1434 		ARCSTAT_BUMP(arcstat_l2_free_on_write);
1435 	} else {
1436 		free_func(buf->b_data, hdr->b_size);
1437 	}
1438 }
1439 
1440 static void
1441 arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
1442 {
1443 	arc_buf_t **bufp;
1444 
1445 	/* free up data associated with the buf */
1446 	if (buf->b_data) {
1447 		arc_state_t *state = buf->b_hdr->b_state;
1448 		uint64_t size = buf->b_hdr->b_size;
1449 		arc_buf_contents_t type = buf->b_hdr->b_type;
1450 
1451 		arc_cksum_verify(buf);
1452 		arc_buf_unwatch(buf);
1453 
1454 		if (!recycle) {
1455 			if (type == ARC_BUFC_METADATA) {
1456 				arc_buf_data_free(buf, zio_buf_free);
1457 				arc_space_return(size, ARC_SPACE_DATA);
1458 			} else {
1459 				ASSERT(type == ARC_BUFC_DATA);
1460 				arc_buf_data_free(buf, zio_data_buf_free);
1461 				ARCSTAT_INCR(arcstat_data_size, -size);
1462 				atomic_add_64(&arc_size, -size);
1463 			}
1464 		}
1465 		if (list_link_active(&buf->b_hdr->b_arc_node)) {
1466 			uint64_t *cnt = &state->arcs_lsize[type];
1467 
1468 			ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
1469 			ASSERT(state != arc_anon);
1470 
1471 			ASSERT3U(*cnt, >=, size);
1472 			atomic_add_64(cnt, -size);
1473 		}
1474 		ASSERT3U(state->arcs_size, >=, size);
1475 		atomic_add_64(&state->arcs_size, -size);
1476 		buf->b_data = NULL;
1477 
1478 		/*
1479 		 * If we're destroying a duplicate buffer make sure
1480 		 * that the appropriate statistics are updated.
1481 		 */
1482 		if (buf->b_hdr->b_datacnt > 1 &&
1483 		    buf->b_hdr->b_type == ARC_BUFC_DATA) {
1484 			ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
1485 			ARCSTAT_INCR(arcstat_duplicate_buffers_size, -size);
1486 		}
1487 		ASSERT(buf->b_hdr->b_datacnt > 0);
1488 		buf->b_hdr->b_datacnt -= 1;
1489 	}
1490 
1491 	/* only remove the buf if requested */
1492 	if (!all)
1493 		return;
1494 
1495 	/* remove the buf from the hdr list */
1496 	for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
1497 		continue;
1498 	*bufp = buf->b_next;
1499 	buf->b_next = NULL;
1500 
1501 	ASSERT(buf->b_efunc == NULL);
1502 
1503 	/* clean up the buf */
1504 	buf->b_hdr = NULL;
1505 	kmem_cache_free(buf_cache, buf);
1506 }
1507 
1508 static void
1509 arc_hdr_destroy(arc_buf_hdr_t *hdr)
1510 {
1511 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
1512 	ASSERT3P(hdr->b_state, ==, arc_anon);
1513 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1514 	l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
1515 
1516 	if (l2hdr != NULL) {
1517 		boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
1518 		/*
1519 		 * To prevent arc_free() and l2arc_evict() from
1520 		 * attempting to free the same buffer at the same time,
1521 		 * a FREE_IN_PROGRESS flag is given to arc_free() to
1522 		 * give it priority.  l2arc_evict() can't destroy this
1523 		 * header while we are waiting on l2arc_buflist_mtx.
1524 		 *
1525 		 * The hdr may be removed from l2ad_buflist before we
1526 		 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
1527 		 */
1528 		if (!buflist_held) {
1529 			mutex_enter(&l2arc_buflist_mtx);
1530 			l2hdr = hdr->b_l2hdr;
1531 		}
1532 
1533 		if (l2hdr != NULL) {
1534 			list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
1535 			ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
1536 			kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
1537 			if (hdr->b_state == arc_l2c_only)
1538 				l2arc_hdr_stat_remove();
1539 			hdr->b_l2hdr = NULL;
1540 		}
1541 
1542 		if (!buflist_held)
1543 			mutex_exit(&l2arc_buflist_mtx);
1544 	}
1545 
1546 	if (!BUF_EMPTY(hdr)) {
1547 		ASSERT(!HDR_IN_HASH_TABLE(hdr));
1548 		buf_discard_identity(hdr);
1549 	}
1550 	while (hdr->b_buf) {
1551 		arc_buf_t *buf = hdr->b_buf;
1552 
1553 		if (buf->b_efunc) {
1554 			mutex_enter(&arc_eviction_mtx);
1555 			mutex_enter(&buf->b_evict_lock);
1556 			ASSERT(buf->b_hdr != NULL);
1557 			arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
1558 			hdr->b_buf = buf->b_next;
1559 			buf->b_hdr = &arc_eviction_hdr;
1560 			buf->b_next = arc_eviction_list;
1561 			arc_eviction_list = buf;
1562 			mutex_exit(&buf->b_evict_lock);
1563 			mutex_exit(&arc_eviction_mtx);
1564 		} else {
1565 			arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
1566 		}
1567 	}
1568 	if (hdr->b_freeze_cksum != NULL) {
1569 		kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
1570 		hdr->b_freeze_cksum = NULL;
1571 	}
1572 	if (hdr->b_thawed) {
1573 		kmem_free(hdr->b_thawed, 1);
1574 		hdr->b_thawed = NULL;
1575 	}
1576 
1577 	ASSERT(!list_link_active(&hdr->b_arc_node));
1578 	ASSERT3P(hdr->b_hash_next, ==, NULL);
1579 	ASSERT3P(hdr->b_acb, ==, NULL);
1580 	kmem_cache_free(hdr_cache, hdr);
1581 }
1582 
1583 void
1584 arc_buf_free(arc_buf_t *buf, void *tag)
1585 {
1586 	arc_buf_hdr_t *hdr = buf->b_hdr;
1587 	int hashed = hdr->b_state != arc_anon;
1588 
1589 	ASSERT(buf->b_efunc == NULL);
1590 	ASSERT(buf->b_data != NULL);
1591 
1592 	if (hashed) {
1593 		kmutex_t *hash_lock = HDR_LOCK(hdr);
1594 
1595 		mutex_enter(hash_lock);
1596 		hdr = buf->b_hdr;
1597 		ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1598 
1599 		(void) remove_reference(hdr, hash_lock, tag);
1600 		if (hdr->b_datacnt > 1) {
1601 			arc_buf_destroy(buf, FALSE, TRUE);
1602 		} else {
1603 			ASSERT(buf == hdr->b_buf);
1604 			ASSERT(buf->b_efunc == NULL);
1605 			hdr->b_flags |= ARC_BUF_AVAILABLE;
1606 		}
1607 		mutex_exit(hash_lock);
1608 	} else if (HDR_IO_IN_PROGRESS(hdr)) {
1609 		int destroy_hdr;
1610 		/*
1611 		 * We are in the middle of an async write.  Don't destroy
1612 		 * this buffer unless the write completes before we finish
1613 		 * decrementing the reference count.
1614 		 */
1615 		mutex_enter(&arc_eviction_mtx);
1616 		(void) remove_reference(hdr, NULL, tag);
1617 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
1618 		destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
1619 		mutex_exit(&arc_eviction_mtx);
1620 		if (destroy_hdr)
1621 			arc_hdr_destroy(hdr);
1622 	} else {
1623 		if (remove_reference(hdr, NULL, tag) > 0)
1624 			arc_buf_destroy(buf, FALSE, TRUE);
1625 		else
1626 			arc_hdr_destroy(hdr);
1627 	}
1628 }
1629 
1630 int
1631 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
1632 {
1633 	arc_buf_hdr_t *hdr = buf->b_hdr;
1634 	kmutex_t *hash_lock = HDR_LOCK(hdr);
1635 	int no_callback = (buf->b_efunc == NULL);
1636 
1637 	if (hdr->b_state == arc_anon) {
1638 		ASSERT(hdr->b_datacnt == 1);
1639 		arc_buf_free(buf, tag);
1640 		return (no_callback);
1641 	}
1642 
1643 	mutex_enter(hash_lock);
1644 	hdr = buf->b_hdr;
1645 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
1646 	ASSERT(hdr->b_state != arc_anon);
1647 	ASSERT(buf->b_data != NULL);
1648 
1649 	(void) remove_reference(hdr, hash_lock, tag);
1650 	if (hdr->b_datacnt > 1) {
1651 		if (no_callback)
1652 			arc_buf_destroy(buf, FALSE, TRUE);
1653 	} else if (no_callback) {
1654 		ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
1655 		ASSERT(buf->b_efunc == NULL);
1656 		hdr->b_flags |= ARC_BUF_AVAILABLE;
1657 	}
1658 	ASSERT(no_callback || hdr->b_datacnt > 1 ||
1659 	    refcount_is_zero(&hdr->b_refcnt));
1660 	mutex_exit(hash_lock);
1661 	return (no_callback);
1662 }
1663 
1664 int
1665 arc_buf_size(arc_buf_t *buf)
1666 {
1667 	return (buf->b_hdr->b_size);
1668 }
1669 
1670 /*
1671  * Called from the DMU to determine if the current buffer should be
1672  * evicted. In order to ensure proper locking, the eviction must be initiated
1673  * from the DMU. Return true if the buffer is associated with user data and
1674  * duplicate buffers still exist.
1675  */
1676 boolean_t
1677 arc_buf_eviction_needed(arc_buf_t *buf)
1678 {
1679 	arc_buf_hdr_t *hdr;
1680 	boolean_t evict_needed = B_FALSE;
1681 
1682 	if (zfs_disable_dup_eviction)
1683 		return (B_FALSE);
1684 
1685 	mutex_enter(&buf->b_evict_lock);
1686 	hdr = buf->b_hdr;
1687 	if (hdr == NULL) {
1688 		/*
1689 		 * We are in arc_do_user_evicts(); let that function
1690 		 * perform the eviction.
1691 		 */
1692 		ASSERT(buf->b_data == NULL);
1693 		mutex_exit(&buf->b_evict_lock);
1694 		return (B_FALSE);
1695 	} else if (buf->b_data == NULL) {
1696 		/*
1697 		 * We have already been added to the arc eviction list;
1698 		 * recommend eviction.
1699 		 */
1700 		ASSERT3P(hdr, ==, &arc_eviction_hdr);
1701 		mutex_exit(&buf->b_evict_lock);
1702 		return (B_TRUE);
1703 	}
1704 
1705 	if (hdr->b_datacnt > 1 && hdr->b_type == ARC_BUFC_DATA)
1706 		evict_needed = B_TRUE;
1707 
1708 	mutex_exit(&buf->b_evict_lock);
1709 	return (evict_needed);
1710 }
1711 
1712 /*
1713  * Evict buffers from list until we've removed the specified number of
1714  * bytes.  Move the removed buffers to the appropriate evict state.
1715  * If the recycle flag is set, then attempt to "recycle" a buffer:
1716  * - look for a buffer to evict that is `bytes' long.
1717  * - return the data block from this buffer rather than freeing it.
1718  * This flag is used by callers that are trying to make space for a
1719  * new buffer in a full arc cache.
1720  *
1721  * This function makes a "best effort".  It skips over any buffers
1722  * it can't get a hash_lock on, and so may not catch all candidates.
1723  * It may also return without evicting as much space as requested.
1724  */
1725 static void *
1726 arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
1727     arc_buf_contents_t type)
1728 {
1729 	arc_state_t *evicted_state;
1730 	uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
1731 	arc_buf_hdr_t *ab, *ab_prev = NULL;
1732 	list_t *list = &state->arcs_list[type];
1733 	kmutex_t *hash_lock;
1734 	boolean_t have_lock;
1735 	void *stolen = NULL;
1736 
1737 	ASSERT(state == arc_mru || state == arc_mfu);
1738 
1739 	evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1740 
1741 	mutex_enter(&state->arcs_mtx);
1742 	mutex_enter(&evicted_state->arcs_mtx);
1743 
1744 	for (ab = list_tail(list); ab; ab = ab_prev) {
1745 		ab_prev = list_prev(list, ab);
1746 		/* prefetch buffers have a minimum lifespan */
1747 		if (HDR_IO_IN_PROGRESS(ab) ||
1748 		    (spa && ab->b_spa != spa) ||
1749 		    (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
1750 		    ddi_get_lbolt() - ab->b_arc_access <
1751 		    arc_min_prefetch_lifespan)) {
1752 			skipped++;
1753 			continue;
1754 		}
1755 		/* "lookahead" for better eviction candidate */
1756 		if (recycle && ab->b_size != bytes &&
1757 		    ab_prev && ab_prev->b_size == bytes)
1758 			continue;
1759 		hash_lock = HDR_LOCK(ab);
1760 		have_lock = MUTEX_HELD(hash_lock);
1761 		if (have_lock || mutex_tryenter(hash_lock)) {
1762 			ASSERT0(refcount_count(&ab->b_refcnt));
1763 			ASSERT(ab->b_datacnt > 0);
1764 			while (ab->b_buf) {
1765 				arc_buf_t *buf = ab->b_buf;
1766 				if (!mutex_tryenter(&buf->b_evict_lock)) {
1767 					missed += 1;
1768 					break;
1769 				}
1770 				if (buf->b_data) {
1771 					bytes_evicted += ab->b_size;
1772 					if (recycle && ab->b_type == type &&
1773 					    ab->b_size == bytes &&
1774 					    !HDR_L2_WRITING(ab)) {
1775 						stolen = buf->b_data;
1776 						recycle = FALSE;
1777 					}
1778 				}
1779 				if (buf->b_efunc) {
1780 					mutex_enter(&arc_eviction_mtx);
1781 					arc_buf_destroy(buf,
1782 					    buf->b_data == stolen, FALSE);
1783 					ab->b_buf = buf->b_next;
1784 					buf->b_hdr = &arc_eviction_hdr;
1785 					buf->b_next = arc_eviction_list;
1786 					arc_eviction_list = buf;
1787 					mutex_exit(&arc_eviction_mtx);
1788 					mutex_exit(&buf->b_evict_lock);
1789 				} else {
1790 					mutex_exit(&buf->b_evict_lock);
1791 					arc_buf_destroy(buf,
1792 					    buf->b_data == stolen, TRUE);
1793 				}
1794 			}
1795 
1796 			if (ab->b_l2hdr) {
1797 				ARCSTAT_INCR(arcstat_evict_l2_cached,
1798 				    ab->b_size);
1799 			} else {
1800 				if (l2arc_write_eligible(ab->b_spa, ab)) {
1801 					ARCSTAT_INCR(arcstat_evict_l2_eligible,
1802 					    ab->b_size);
1803 				} else {
1804 					ARCSTAT_INCR(
1805 					    arcstat_evict_l2_ineligible,
1806 					    ab->b_size);
1807 				}
1808 			}
1809 
1810 			if (ab->b_datacnt == 0) {
1811 				arc_change_state(evicted_state, ab, hash_lock);
1812 				ASSERT(HDR_IN_HASH_TABLE(ab));
1813 				ab->b_flags |= ARC_IN_HASH_TABLE;
1814 				ab->b_flags &= ~ARC_BUF_AVAILABLE;
1815 				DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
1816 			}
1817 			if (!have_lock)
1818 				mutex_exit(hash_lock);
1819 			if (bytes >= 0 && bytes_evicted >= bytes)
1820 				break;
1821 		} else {
1822 			missed += 1;
1823 		}
1824 	}
1825 
1826 	mutex_exit(&evicted_state->arcs_mtx);
1827 	mutex_exit(&state->arcs_mtx);
1828 
1829 	if (bytes_evicted < bytes)
1830 		dprintf("only evicted %lld bytes from %x",
1831 		    (longlong_t)bytes_evicted, state);
1832 
1833 	if (skipped)
1834 		ARCSTAT_INCR(arcstat_evict_skip, skipped);
1835 
1836 	if (missed)
1837 		ARCSTAT_INCR(arcstat_mutex_miss, missed);
1838 
1839 	/*
1840 	 * We have just evicted some date into the ghost state, make
1841 	 * sure we also adjust the ghost state size if necessary.
1842 	 */
1843 	if (arc_no_grow &&
1844 	    arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
1845 		int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
1846 		    arc_mru_ghost->arcs_size - arc_c;
1847 
1848 		if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
1849 			int64_t todelete =
1850 			    MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
1851 			arc_evict_ghost(arc_mru_ghost, NULL, todelete);
1852 		} else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
1853 			int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
1854 			    arc_mru_ghost->arcs_size +
1855 			    arc_mfu_ghost->arcs_size - arc_c);
1856 			arc_evict_ghost(arc_mfu_ghost, NULL, todelete);
1857 		}
1858 	}
1859 
1860 	return (stolen);
1861 }
1862 
1863 /*
1864  * Remove buffers from list until we've removed the specified number of
1865  * bytes.  Destroy the buffers that are removed.
1866  */
1867 static void
1868 arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
1869 {
1870 	arc_buf_hdr_t *ab, *ab_prev;
1871 	arc_buf_hdr_t marker = { 0 };
1872 	list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1873 	kmutex_t *hash_lock;
1874 	uint64_t bytes_deleted = 0;
1875 	uint64_t bufs_skipped = 0;
1876 
1877 	ASSERT(GHOST_STATE(state));
1878 top:
1879 	mutex_enter(&state->arcs_mtx);
1880 	for (ab = list_tail(list); ab; ab = ab_prev) {
1881 		ab_prev = list_prev(list, ab);
1882 		if (spa && ab->b_spa != spa)
1883 			continue;
1884 
1885 		/* ignore markers */
1886 		if (ab->b_spa == 0)
1887 			continue;
1888 
1889 		hash_lock = HDR_LOCK(ab);
1890 		/* caller may be trying to modify this buffer, skip it */
1891 		if (MUTEX_HELD(hash_lock))
1892 			continue;
1893 		if (mutex_tryenter(hash_lock)) {
1894 			ASSERT(!HDR_IO_IN_PROGRESS(ab));
1895 			ASSERT(ab->b_buf == NULL);
1896 			ARCSTAT_BUMP(arcstat_deleted);
1897 			bytes_deleted += ab->b_size;
1898 
1899 			if (ab->b_l2hdr != NULL) {
1900 				/*
1901 				 * This buffer is cached on the 2nd Level ARC;
1902 				 * don't destroy the header.
1903 				 */
1904 				arc_change_state(arc_l2c_only, ab, hash_lock);
1905 				mutex_exit(hash_lock);
1906 			} else {
1907 				arc_change_state(arc_anon, ab, hash_lock);
1908 				mutex_exit(hash_lock);
1909 				arc_hdr_destroy(ab);
1910 			}
1911 
1912 			DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1913 			if (bytes >= 0 && bytes_deleted >= bytes)
1914 				break;
1915 		} else if (bytes < 0) {
1916 			/*
1917 			 * Insert a list marker and then wait for the
1918 			 * hash lock to become available. Once its
1919 			 * available, restart from where we left off.
1920 			 */
1921 			list_insert_after(list, ab, &marker);
1922 			mutex_exit(&state->arcs_mtx);
1923 			mutex_enter(hash_lock);
1924 			mutex_exit(hash_lock);
1925 			mutex_enter(&state->arcs_mtx);
1926 			ab_prev = list_prev(list, &marker);
1927 			list_remove(list, &marker);
1928 		} else
1929 			bufs_skipped += 1;
1930 	}
1931 	mutex_exit(&state->arcs_mtx);
1932 
1933 	if (list == &state->arcs_list[ARC_BUFC_DATA] &&
1934 	    (bytes < 0 || bytes_deleted < bytes)) {
1935 		list = &state->arcs_list[ARC_BUFC_METADATA];
1936 		goto top;
1937 	}
1938 
1939 	if (bufs_skipped) {
1940 		ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
1941 		ASSERT(bytes >= 0);
1942 	}
1943 
1944 	if (bytes_deleted < bytes)
1945 		dprintf("only deleted %lld bytes from %p",
1946 		    (longlong_t)bytes_deleted, state);
1947 }
1948 
1949 static void
1950 arc_adjust(void)
1951 {
1952 	int64_t adjustment, delta;
1953 
1954 	/*
1955 	 * Adjust MRU size
1956 	 */
1957 
1958 	adjustment = MIN((int64_t)(arc_size - arc_c),
1959 	    (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
1960 	    arc_p));
1961 
1962 	if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
1963 		delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
1964 		(void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA);
1965 		adjustment -= delta;
1966 	}
1967 
1968 	if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1969 		delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
1970 		(void) arc_evict(arc_mru, NULL, delta, FALSE,
1971 		    ARC_BUFC_METADATA);
1972 	}
1973 
1974 	/*
1975 	 * Adjust MFU size
1976 	 */
1977 
1978 	adjustment = arc_size - arc_c;
1979 
1980 	if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
1981 		delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
1982 		(void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA);
1983 		adjustment -= delta;
1984 	}
1985 
1986 	if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
1987 		int64_t delta = MIN(adjustment,
1988 		    arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
1989 		(void) arc_evict(arc_mfu, NULL, delta, FALSE,
1990 		    ARC_BUFC_METADATA);
1991 	}
1992 
1993 	/*
1994 	 * Adjust ghost lists
1995 	 */
1996 
1997 	adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
1998 
1999 	if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
2000 		delta = MIN(arc_mru_ghost->arcs_size, adjustment);
2001 		arc_evict_ghost(arc_mru_ghost, NULL, delta);
2002 	}
2003 
2004 	adjustment =
2005 	    arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
2006 
2007 	if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
2008 		delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
2009 		arc_evict_ghost(arc_mfu_ghost, NULL, delta);
2010 	}
2011 }
2012 
2013 static void
2014 arc_do_user_evicts(void)
2015 {
2016 	mutex_enter(&arc_eviction_mtx);
2017 	while (arc_eviction_list != NULL) {
2018 		arc_buf_t *buf = arc_eviction_list;
2019 		arc_eviction_list = buf->b_next;
2020 		mutex_enter(&buf->b_evict_lock);
2021 		buf->b_hdr = NULL;
2022 		mutex_exit(&buf->b_evict_lock);
2023 		mutex_exit(&arc_eviction_mtx);
2024 
2025 		if (buf->b_efunc != NULL)
2026 			VERIFY(buf->b_efunc(buf) == 0);
2027 
2028 		buf->b_efunc = NULL;
2029 		buf->b_private = NULL;
2030 		kmem_cache_free(buf_cache, buf);
2031 		mutex_enter(&arc_eviction_mtx);
2032 	}
2033 	mutex_exit(&arc_eviction_mtx);
2034 }
2035 
2036 /*
2037  * Flush all *evictable* data from the cache for the given spa.
2038  * NOTE: this will not touch "active" (i.e. referenced) data.
2039  */
2040 void
2041 arc_flush(spa_t *spa)
2042 {
2043 	uint64_t guid = 0;
2044 
2045 	if (spa)
2046 		guid = spa_load_guid(spa);
2047 
2048 	while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
2049 		(void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
2050 		if (spa)
2051 			break;
2052 	}
2053 	while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
2054 		(void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
2055 		if (spa)
2056 			break;
2057 	}
2058 	while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
2059 		(void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
2060 		if (spa)
2061 			break;
2062 	}
2063 	while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
2064 		(void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
2065 		if (spa)
2066 			break;
2067 	}
2068 
2069 	arc_evict_ghost(arc_mru_ghost, guid, -1);
2070 	arc_evict_ghost(arc_mfu_ghost, guid, -1);
2071 
2072 	mutex_enter(&arc_reclaim_thr_lock);
2073 	arc_do_user_evicts();
2074 	mutex_exit(&arc_reclaim_thr_lock);
2075 	ASSERT(spa || arc_eviction_list == NULL);
2076 }
2077 
2078 void
2079 arc_shrink(void)
2080 {
2081 	if (arc_c > arc_c_min) {
2082 		uint64_t to_free;
2083 
2084 #ifdef _KERNEL
2085 		to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree));
2086 #else
2087 		to_free = arc_c >> arc_shrink_shift;
2088 #endif
2089 		if (arc_c > arc_c_min + to_free)
2090 			atomic_add_64(&arc_c, -to_free);
2091 		else
2092 			arc_c = arc_c_min;
2093 
2094 		atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
2095 		if (arc_c > arc_size)
2096 			arc_c = MAX(arc_size, arc_c_min);
2097 		if (arc_p > arc_c)
2098 			arc_p = (arc_c >> 1);
2099 		ASSERT(arc_c >= arc_c_min);
2100 		ASSERT((int64_t)arc_p >= 0);
2101 	}
2102 
2103 	if (arc_size > arc_c)
2104 		arc_adjust();
2105 }
2106 
2107 /*
2108  * Determine if the system is under memory pressure and is asking
2109  * to reclaim memory. A return value of 1 indicates that the system
2110  * is under memory pressure and that the arc should adjust accordingly.
2111  */
2112 static int
2113 arc_reclaim_needed(void)
2114 {
2115 	uint64_t extra;
2116 
2117 #ifdef _KERNEL
2118 
2119 	if (needfree)
2120 		return (1);
2121 
2122 	/*
2123 	 * take 'desfree' extra pages, so we reclaim sooner, rather than later
2124 	 */
2125 	extra = desfree;
2126 
2127 	/*
2128 	 * check that we're out of range of the pageout scanner.  It starts to
2129 	 * schedule paging if freemem is less than lotsfree and needfree.
2130 	 * lotsfree is the high-water mark for pageout, and needfree is the
2131 	 * number of needed free pages.  We add extra pages here to make sure
2132 	 * the scanner doesn't start up while we're freeing memory.
2133 	 */
2134 	if (freemem < lotsfree + needfree + extra)
2135 		return (1);
2136 
2137 	/*
2138 	 * check to make sure that swapfs has enough space so that anon
2139 	 * reservations can still succeed. anon_resvmem() checks that the
2140 	 * availrmem is greater than swapfs_minfree, and the number of reserved
2141 	 * swap pages.  We also add a bit of extra here just to prevent
2142 	 * circumstances from getting really dire.
2143 	 */
2144 	if (availrmem < swapfs_minfree + swapfs_reserve + extra)
2145 		return (1);
2146 
2147 #if defined(__i386)
2148 	/*
2149 	 * If we're on an i386 platform, it's possible that we'll exhaust the
2150 	 * kernel heap space before we ever run out of available physical
2151 	 * memory.  Most checks of the size of the heap_area compare against
2152 	 * tune.t_minarmem, which is the minimum available real memory that we
2153 	 * can have in the system.  However, this is generally fixed at 25 pages
2154 	 * which is so low that it's useless.  In this comparison, we seek to
2155 	 * calculate the total heap-size, and reclaim if more than 3/4ths of the
2156 	 * heap is allocated.  (Or, in the calculation, if less than 1/4th is
2157 	 * free)
2158 	 */
2159 	if (vmem_size(heap_arena, VMEM_FREE) <
2160 	    (vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC) >> 2))
2161 		return (1);
2162 #endif
2163 
2164 	/*
2165 	 * If zio data pages are being allocated out of a separate heap segment,
2166 	 * then enforce that the size of available vmem for this arena remains
2167 	 * above about 1/16th free.
2168 	 *
2169 	 * Note: The 1/16th arena free requirement was put in place
2170 	 * to aggressively evict memory from the arc in order to avoid
2171 	 * memory fragmentation issues.
2172 	 */
2173 	if (zio_arena != NULL &&
2174 	    vmem_size(zio_arena, VMEM_FREE) <
2175 	    (vmem_size(zio_arena, VMEM_ALLOC) >> 4))
2176 		return (1);
2177 #else
2178 	if (spa_get_random(100) == 0)
2179 		return (1);
2180 #endif
2181 	return (0);
2182 }
2183 
2184 static void
2185 arc_kmem_reap_now(arc_reclaim_strategy_t strat)
2186 {
2187 	size_t			i;
2188 	kmem_cache_t		*prev_cache = NULL;
2189 	kmem_cache_t		*prev_data_cache = NULL;
2190 	extern kmem_cache_t	*zio_buf_cache[];
2191 	extern kmem_cache_t	*zio_data_buf_cache[];
2192 
2193 #ifdef _KERNEL
2194 	if (arc_meta_used >= arc_meta_limit) {
2195 		/*
2196 		 * We are exceeding our meta-data cache limit.
2197 		 * Purge some DNLC entries to release holds on meta-data.
2198 		 */
2199 		dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
2200 	}
2201 #if defined(__i386)
2202 	/*
2203 	 * Reclaim unused memory from all kmem caches.
2204 	 */
2205 	kmem_reap();
2206 #endif
2207 #endif
2208 
2209 	/*
2210 	 * An aggressive reclamation will shrink the cache size as well as
2211 	 * reap free buffers from the arc kmem caches.
2212 	 */
2213 	if (strat == ARC_RECLAIM_AGGR)
2214 		arc_shrink();
2215 
2216 	for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2217 		if (zio_buf_cache[i] != prev_cache) {
2218 			prev_cache = zio_buf_cache[i];
2219 			kmem_cache_reap_now(zio_buf_cache[i]);
2220 		}
2221 		if (zio_data_buf_cache[i] != prev_data_cache) {
2222 			prev_data_cache = zio_data_buf_cache[i];
2223 			kmem_cache_reap_now(zio_data_buf_cache[i]);
2224 		}
2225 	}
2226 	kmem_cache_reap_now(buf_cache);
2227 	kmem_cache_reap_now(hdr_cache);
2228 
2229 	/*
2230 	 * Ask the vmem areana to reclaim unused memory from its
2231 	 * quantum caches.
2232 	 */
2233 	if (zio_arena != NULL && strat == ARC_RECLAIM_AGGR)
2234 		vmem_qcache_reap(zio_arena);
2235 }
2236 
2237 static void
2238 arc_reclaim_thread(void)
2239 {
2240 	clock_t			growtime = 0;
2241 	arc_reclaim_strategy_t	last_reclaim = ARC_RECLAIM_CONS;
2242 	callb_cpr_t		cpr;
2243 
2244 	CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2245 
2246 	mutex_enter(&arc_reclaim_thr_lock);
2247 	while (arc_thread_exit == 0) {
2248 		if (arc_reclaim_needed()) {
2249 
2250 			if (arc_no_grow) {
2251 				if (last_reclaim == ARC_RECLAIM_CONS) {
2252 					last_reclaim = ARC_RECLAIM_AGGR;
2253 				} else {
2254 					last_reclaim = ARC_RECLAIM_CONS;
2255 				}
2256 			} else {
2257 				arc_no_grow = TRUE;
2258 				last_reclaim = ARC_RECLAIM_AGGR;
2259 				membar_producer();
2260 			}
2261 
2262 			/* reset the growth delay for every reclaim */
2263 			growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
2264 
2265 			arc_kmem_reap_now(last_reclaim);
2266 			arc_warm = B_TRUE;
2267 
2268 		} else if (arc_no_grow && ddi_get_lbolt() >= growtime) {
2269 			arc_no_grow = FALSE;
2270 		}
2271 
2272 		arc_adjust();
2273 
2274 		if (arc_eviction_list != NULL)
2275 			arc_do_user_evicts();
2276 
2277 		/* block until needed, or one second, whichever is shorter */
2278 		CALLB_CPR_SAFE_BEGIN(&cpr);
2279 		(void) cv_timedwait(&arc_reclaim_thr_cv,
2280 		    &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2281 		CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2282 	}
2283 
2284 	arc_thread_exit = 0;
2285 	cv_broadcast(&arc_reclaim_thr_cv);
2286 	CALLB_CPR_EXIT(&cpr);		/* drops arc_reclaim_thr_lock */
2287 	thread_exit();
2288 }
2289 
2290 /*
2291  * Adapt arc info given the number of bytes we are trying to add and
2292  * the state that we are comming from.  This function is only called
2293  * when we are adding new content to the cache.
2294  */
2295 static void
2296 arc_adapt(int bytes, arc_state_t *state)
2297 {
2298 	int mult;
2299 	uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
2300 
2301 	if (state == arc_l2c_only)
2302 		return;
2303 
2304 	ASSERT(bytes > 0);
2305 	/*
2306 	 * Adapt the target size of the MRU list:
2307 	 *	- if we just hit in the MRU ghost list, then increase
2308 	 *	  the target size of the MRU list.
2309 	 *	- if we just hit in the MFU ghost list, then increase
2310 	 *	  the target size of the MFU list by decreasing the
2311 	 *	  target size of the MRU list.
2312 	 */
2313 	if (state == arc_mru_ghost) {
2314 		mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
2315 		    1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
2316 		mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
2317 
2318 		arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
2319 	} else if (state == arc_mfu_ghost) {
2320 		uint64_t delta;
2321 
2322 		mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
2323 		    1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
2324 		mult = MIN(mult, 10);
2325 
2326 		delta = MIN(bytes * mult, arc_p);
2327 		arc_p = MAX(arc_p_min, arc_p - delta);
2328 	}
2329 	ASSERT((int64_t)arc_p >= 0);
2330 
2331 	if (arc_reclaim_needed()) {
2332 		cv_signal(&arc_reclaim_thr_cv);
2333 		return;
2334 	}
2335 
2336 	if (arc_no_grow)
2337 		return;
2338 
2339 	if (arc_c >= arc_c_max)
2340 		return;
2341 
2342 	/*
2343 	 * If we're within (2 * maxblocksize) bytes of the target
2344 	 * cache size, increment the target cache size
2345 	 */
2346 	if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
2347 		atomic_add_64(&arc_c, (int64_t)bytes);
2348 		if (arc_c > arc_c_max)
2349 			arc_c = arc_c_max;
2350 		else if (state == arc_anon)
2351 			atomic_add_64(&arc_p, (int64_t)bytes);
2352 		if (arc_p > arc_c)
2353 			arc_p = arc_c;
2354 	}
2355 	ASSERT((int64_t)arc_p >= 0);
2356 }
2357 
2358 /*
2359  * Check if the cache has reached its limits and eviction is required
2360  * prior to insert.
2361  */
2362 static int
2363 arc_evict_needed(arc_buf_contents_t type)
2364 {
2365 	if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
2366 		return (1);
2367 
2368 	if (arc_reclaim_needed())
2369 		return (1);
2370 
2371 	return (arc_size > arc_c);
2372 }
2373 
2374 /*
2375  * The buffer, supplied as the first argument, needs a data block.
2376  * So, if we are at cache max, determine which cache should be victimized.
2377  * We have the following cases:
2378  *
2379  * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2380  * In this situation if we're out of space, but the resident size of the MFU is
2381  * under the limit, victimize the MFU cache to satisfy this insertion request.
2382  *
2383  * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2384  * Here, we've used up all of the available space for the MRU, so we need to
2385  * evict from our own cache instead.  Evict from the set of resident MRU
2386  * entries.
2387  *
2388  * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2389  * c minus p represents the MFU space in the cache, since p is the size of the
2390  * cache that is dedicated to the MRU.  In this situation there's still space on
2391  * the MFU side, so the MRU side needs to be victimized.
2392  *
2393  * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2394  * MFU's resident set is consuming more space than it has been allotted.  In
2395  * this situation, we must victimize our own cache, the MFU, for this insertion.
2396  */
2397 static void
2398 arc_get_data_buf(arc_buf_t *buf)
2399 {
2400 	arc_state_t		*state = buf->b_hdr->b_state;
2401 	uint64_t		size = buf->b_hdr->b_size;
2402 	arc_buf_contents_t	type = buf->b_hdr->b_type;
2403 
2404 	arc_adapt(size, state);
2405 
2406 	/*
2407 	 * We have not yet reached cache maximum size,
2408 	 * just allocate a new buffer.
2409 	 */
2410 	if (!arc_evict_needed(type)) {
2411 		if (type == ARC_BUFC_METADATA) {
2412 			buf->b_data = zio_buf_alloc(size);
2413 			arc_space_consume(size, ARC_SPACE_DATA);
2414 		} else {
2415 			ASSERT(type == ARC_BUFC_DATA);
2416 			buf->b_data = zio_data_buf_alloc(size);
2417 			ARCSTAT_INCR(arcstat_data_size, size);
2418 			atomic_add_64(&arc_size, size);
2419 		}
2420 		goto out;
2421 	}
2422 
2423 	/*
2424 	 * If we are prefetching from the mfu ghost list, this buffer
2425 	 * will end up on the mru list; so steal space from there.
2426 	 */
2427 	if (state == arc_mfu_ghost)
2428 		state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
2429 	else if (state == arc_mru_ghost)
2430 		state = arc_mru;
2431 
2432 	if (state == arc_mru || state == arc_anon) {
2433 		uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
2434 		state = (arc_mfu->arcs_lsize[type] >= size &&
2435 		    arc_p > mru_used) ? arc_mfu : arc_mru;
2436 	} else {
2437 		/* MFU cases */
2438 		uint64_t mfu_space = arc_c - arc_p;
2439 		state =  (arc_mru->arcs_lsize[type] >= size &&
2440 		    mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
2441 	}
2442 	if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) {
2443 		if (type == ARC_BUFC_METADATA) {
2444 			buf->b_data = zio_buf_alloc(size);
2445 			arc_space_consume(size, ARC_SPACE_DATA);
2446 		} else {
2447 			ASSERT(type == ARC_BUFC_DATA);
2448 			buf->b_data = zio_data_buf_alloc(size);
2449 			ARCSTAT_INCR(arcstat_data_size, size);
2450 			atomic_add_64(&arc_size, size);
2451 		}
2452 		ARCSTAT_BUMP(arcstat_recycle_miss);
2453 	}
2454 	ASSERT(buf->b_data != NULL);
2455 out:
2456 	/*
2457 	 * Update the state size.  Note that ghost states have a
2458 	 * "ghost size" and so don't need to be updated.
2459 	 */
2460 	if (!GHOST_STATE(buf->b_hdr->b_state)) {
2461 		arc_buf_hdr_t *hdr = buf->b_hdr;
2462 
2463 		atomic_add_64(&hdr->b_state->arcs_size, size);
2464 		if (list_link_active(&hdr->b_arc_node)) {
2465 			ASSERT(refcount_is_zero(&hdr->b_refcnt));
2466 			atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2467 		}
2468 		/*
2469 		 * If we are growing the cache, and we are adding anonymous
2470 		 * data, and we have outgrown arc_p, update arc_p
2471 		 */
2472 		if (arc_size < arc_c && hdr->b_state == arc_anon &&
2473 		    arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
2474 			arc_p = MIN(arc_c, arc_p + size);
2475 	}
2476 }
2477 
2478 /*
2479  * This routine is called whenever a buffer is accessed.
2480  * NOTE: the hash lock is dropped in this function.
2481  */
2482 static void
2483 arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2484 {
2485 	clock_t now;
2486 
2487 	ASSERT(MUTEX_HELD(hash_lock));
2488 
2489 	if (buf->b_state == arc_anon) {
2490 		/*
2491 		 * This buffer is not in the cache, and does not
2492 		 * appear in our "ghost" list.  Add the new buffer
2493 		 * to the MRU state.
2494 		 */
2495 
2496 		ASSERT(buf->b_arc_access == 0);
2497 		buf->b_arc_access = ddi_get_lbolt();
2498 		DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2499 		arc_change_state(arc_mru, buf, hash_lock);
2500 
2501 	} else if (buf->b_state == arc_mru) {
2502 		now = ddi_get_lbolt();
2503 
2504 		/*
2505 		 * If this buffer is here because of a prefetch, then either:
2506 		 * - clear the flag if this is a "referencing" read
2507 		 *   (any subsequent access will bump this into the MFU state).
2508 		 * or
2509 		 * - move the buffer to the head of the list if this is
2510 		 *   another prefetch (to make it less likely to be evicted).
2511 		 */
2512 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
2513 			if (refcount_count(&buf->b_refcnt) == 0) {
2514 				ASSERT(list_link_active(&buf->b_arc_node));
2515 			} else {
2516 				buf->b_flags &= ~ARC_PREFETCH;
2517 				ARCSTAT_BUMP(arcstat_mru_hits);
2518 			}
2519 			buf->b_arc_access = now;
2520 			return;
2521 		}
2522 
2523 		/*
2524 		 * This buffer has been "accessed" only once so far,
2525 		 * but it is still in the cache. Move it to the MFU
2526 		 * state.
2527 		 */
2528 		if (now > buf->b_arc_access + ARC_MINTIME) {
2529 			/*
2530 			 * More than 125ms have passed since we
2531 			 * instantiated this buffer.  Move it to the
2532 			 * most frequently used state.
2533 			 */
2534 			buf->b_arc_access = now;
2535 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2536 			arc_change_state(arc_mfu, buf, hash_lock);
2537 		}
2538 		ARCSTAT_BUMP(arcstat_mru_hits);
2539 	} else if (buf->b_state == arc_mru_ghost) {
2540 		arc_state_t	*new_state;
2541 		/*
2542 		 * This buffer has been "accessed" recently, but
2543 		 * was evicted from the cache.  Move it to the
2544 		 * MFU state.
2545 		 */
2546 
2547 		if (buf->b_flags & ARC_PREFETCH) {
2548 			new_state = arc_mru;
2549 			if (refcount_count(&buf->b_refcnt) > 0)
2550 				buf->b_flags &= ~ARC_PREFETCH;
2551 			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2552 		} else {
2553 			new_state = arc_mfu;
2554 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2555 		}
2556 
2557 		buf->b_arc_access = ddi_get_lbolt();
2558 		arc_change_state(new_state, buf, hash_lock);
2559 
2560 		ARCSTAT_BUMP(arcstat_mru_ghost_hits);
2561 	} else if (buf->b_state == arc_mfu) {
2562 		/*
2563 		 * This buffer has been accessed more than once and is
2564 		 * still in the cache.  Keep it in the MFU state.
2565 		 *
2566 		 * NOTE: an add_reference() that occurred when we did
2567 		 * the arc_read() will have kicked this off the list.
2568 		 * If it was a prefetch, we will explicitly move it to
2569 		 * the head of the list now.
2570 		 */
2571 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
2572 			ASSERT(refcount_count(&buf->b_refcnt) == 0);
2573 			ASSERT(list_link_active(&buf->b_arc_node));
2574 		}
2575 		ARCSTAT_BUMP(arcstat_mfu_hits);
2576 		buf->b_arc_access = ddi_get_lbolt();
2577 	} else if (buf->b_state == arc_mfu_ghost) {
2578 		arc_state_t	*new_state = arc_mfu;
2579 		/*
2580 		 * This buffer has been accessed more than once but has
2581 		 * been evicted from the cache.  Move it back to the
2582 		 * MFU state.
2583 		 */
2584 
2585 		if (buf->b_flags & ARC_PREFETCH) {
2586 			/*
2587 			 * This is a prefetch access...
2588 			 * move this block back to the MRU state.
2589 			 */
2590 			ASSERT0(refcount_count(&buf->b_refcnt));
2591 			new_state = arc_mru;
2592 		}
2593 
2594 		buf->b_arc_access = ddi_get_lbolt();
2595 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2596 		arc_change_state(new_state, buf, hash_lock);
2597 
2598 		ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
2599 	} else if (buf->b_state == arc_l2c_only) {
2600 		/*
2601 		 * This buffer is on the 2nd Level ARC.
2602 		 */
2603 
2604 		buf->b_arc_access = ddi_get_lbolt();
2605 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2606 		arc_change_state(arc_mfu, buf, hash_lock);
2607 	} else {
2608 		ASSERT(!"invalid arc state");
2609 	}
2610 }
2611 
2612 /* a generic arc_done_func_t which you can use */
2613 /* ARGSUSED */
2614 void
2615 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2616 {
2617 	if (zio == NULL || zio->io_error == 0)
2618 		bcopy(buf->b_data, arg, buf->b_hdr->b_size);
2619 	VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2620 }
2621 
2622 /* a generic arc_done_func_t */
2623 void
2624 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2625 {
2626 	arc_buf_t **bufp = arg;
2627 	if (zio && zio->io_error) {
2628 		VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2629 		*bufp = NULL;
2630 	} else {
2631 		*bufp = buf;
2632 		ASSERT(buf->b_data);
2633 	}
2634 }
2635 
2636 static void
2637 arc_read_done(zio_t *zio)
2638 {
2639 	arc_buf_hdr_t	*hdr, *found;
2640 	arc_buf_t	*buf;
2641 	arc_buf_t	*abuf;	/* buffer we're assigning to callback */
2642 	kmutex_t	*hash_lock;
2643 	arc_callback_t	*callback_list, *acb;
2644 	int		freeable = FALSE;
2645 
2646 	buf = zio->io_private;
2647 	hdr = buf->b_hdr;
2648 
2649 	/*
2650 	 * The hdr was inserted into hash-table and removed from lists
2651 	 * prior to starting I/O.  We should find this header, since
2652 	 * it's in the hash table, and it should be legit since it's
2653 	 * not possible to evict it during the I/O.  The only possible
2654 	 * reason for it not to be found is if we were freed during the
2655 	 * read.
2656 	 */
2657 	found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
2658 	    &hash_lock);
2659 
2660 	ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
2661 	    (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
2662 	    (found == hdr && HDR_L2_READING(hdr)));
2663 
2664 	hdr->b_flags &= ~ARC_L2_EVICTED;
2665 	if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
2666 		hdr->b_flags &= ~ARC_L2CACHE;
2667 
2668 	/* byteswap if necessary */
2669 	callback_list = hdr->b_acb;
2670 	ASSERT(callback_list != NULL);
2671 	if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
2672 		dmu_object_byteswap_t bswap =
2673 		    DMU_OT_BYTESWAP(BP_GET_TYPE(zio->io_bp));
2674 		arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
2675 		    byteswap_uint64_array :
2676 		    dmu_ot_byteswap[bswap].ob_func;
2677 		func(buf->b_data, hdr->b_size);
2678 	}
2679 
2680 	arc_cksum_compute(buf, B_FALSE);
2681 	arc_buf_watch(buf);
2682 
2683 	if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
2684 		/*
2685 		 * Only call arc_access on anonymous buffers.  This is because
2686 		 * if we've issued an I/O for an evicted buffer, we've already
2687 		 * called arc_access (to prevent any simultaneous readers from
2688 		 * getting confused).
2689 		 */
2690 		arc_access(hdr, hash_lock);
2691 	}
2692 
2693 	/* create copies of the data buffer for the callers */
2694 	abuf = buf;
2695 	for (acb = callback_list; acb; acb = acb->acb_next) {
2696 		if (acb->acb_done) {
2697 			if (abuf == NULL) {
2698 				ARCSTAT_BUMP(arcstat_duplicate_reads);
2699 				abuf = arc_buf_clone(buf);
2700 			}
2701 			acb->acb_buf = abuf;
2702 			abuf = NULL;
2703 		}
2704 	}
2705 	hdr->b_acb = NULL;
2706 	hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2707 	ASSERT(!HDR_BUF_AVAILABLE(hdr));
2708 	if (abuf == buf) {
2709 		ASSERT(buf->b_efunc == NULL);
2710 		ASSERT(hdr->b_datacnt == 1);
2711 		hdr->b_flags |= ARC_BUF_AVAILABLE;
2712 	}
2713 
2714 	ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2715 
2716 	if (zio->io_error != 0) {
2717 		hdr->b_flags |= ARC_IO_ERROR;
2718 		if (hdr->b_state != arc_anon)
2719 			arc_change_state(arc_anon, hdr, hash_lock);
2720 		if (HDR_IN_HASH_TABLE(hdr))
2721 			buf_hash_remove(hdr);
2722 		freeable = refcount_is_zero(&hdr->b_refcnt);
2723 	}
2724 
2725 	/*
2726 	 * Broadcast before we drop the hash_lock to avoid the possibility
2727 	 * that the hdr (and hence the cv) might be freed before we get to
2728 	 * the cv_broadcast().
2729 	 */
2730 	cv_broadcast(&hdr->b_cv);
2731 
2732 	if (hash_lock) {
2733 		mutex_exit(hash_lock);
2734 	} else {
2735 		/*
2736 		 * This block was freed while we waited for the read to
2737 		 * complete.  It has been removed from the hash table and
2738 		 * moved to the anonymous state (so that it won't show up
2739 		 * in the cache).
2740 		 */
2741 		ASSERT3P(hdr->b_state, ==, arc_anon);
2742 		freeable = refcount_is_zero(&hdr->b_refcnt);
2743 	}
2744 
2745 	/* execute each callback and free its structure */
2746 	while ((acb = callback_list) != NULL) {
2747 		if (acb->acb_done)
2748 			acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2749 
2750 		if (acb->acb_zio_dummy != NULL) {
2751 			acb->acb_zio_dummy->io_error = zio->io_error;
2752 			zio_nowait(acb->acb_zio_dummy);
2753 		}
2754 
2755 		callback_list = acb->acb_next;
2756 		kmem_free(acb, sizeof (arc_callback_t));
2757 	}
2758 
2759 	if (freeable)
2760 		arc_hdr_destroy(hdr);
2761 }
2762 
2763 /*
2764  * "Read" the block at the specified DVA (in bp) via the
2765  * cache.  If the block is found in the cache, invoke the provided
2766  * callback immediately and return.  Note that the `zio' parameter
2767  * in the callback will be NULL in this case, since no IO was
2768  * required.  If the block is not in the cache pass the read request
2769  * on to the spa with a substitute callback function, so that the
2770  * requested block will be added to the cache.
2771  *
2772  * If a read request arrives for a block that has a read in-progress,
2773  * either wait for the in-progress read to complete (and return the
2774  * results); or, if this is a read with a "done" func, add a record
2775  * to the read to invoke the "done" func when the read completes,
2776  * and return; or just return.
2777  *
2778  * arc_read_done() will invoke all the requested "done" functions
2779  * for readers of this block.
2780  */
2781 int
2782 arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_done_func_t *done,
2783     void *private, int priority, int zio_flags, uint32_t *arc_flags,
2784     const zbookmark_t *zb)
2785 {
2786 	arc_buf_hdr_t *hdr;
2787 	arc_buf_t *buf;
2788 	kmutex_t *hash_lock;
2789 	zio_t *rzio;
2790 	uint64_t guid = spa_load_guid(spa);
2791 
2792 top:
2793 	hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
2794 	    &hash_lock);
2795 	if (hdr && hdr->b_datacnt > 0) {
2796 
2797 		*arc_flags |= ARC_CACHED;
2798 
2799 		if (HDR_IO_IN_PROGRESS(hdr)) {
2800 
2801 			if (*arc_flags & ARC_WAIT) {
2802 				cv_wait(&hdr->b_cv, hash_lock);
2803 				mutex_exit(hash_lock);
2804 				goto top;
2805 			}
2806 			ASSERT(*arc_flags & ARC_NOWAIT);
2807 
2808 			if (done) {
2809 				arc_callback_t	*acb = NULL;
2810 
2811 				acb = kmem_zalloc(sizeof (arc_callback_t),
2812 				    KM_SLEEP);
2813 				acb->acb_done = done;
2814 				acb->acb_private = private;
2815 				if (pio != NULL)
2816 					acb->acb_zio_dummy = zio_null(pio,
2817 					    spa, NULL, NULL, NULL, zio_flags);
2818 
2819 				ASSERT(acb->acb_done != NULL);
2820 				acb->acb_next = hdr->b_acb;
2821 				hdr->b_acb = acb;
2822 				add_reference(hdr, hash_lock, private);
2823 				mutex_exit(hash_lock);
2824 				return (0);
2825 			}
2826 			mutex_exit(hash_lock);
2827 			return (0);
2828 		}
2829 
2830 		ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2831 
2832 		if (done) {
2833 			add_reference(hdr, hash_lock, private);
2834 			/*
2835 			 * If this block is already in use, create a new
2836 			 * copy of the data so that we will be guaranteed
2837 			 * that arc_release() will always succeed.
2838 			 */
2839 			buf = hdr->b_buf;
2840 			ASSERT(buf);
2841 			ASSERT(buf->b_data);
2842 			if (HDR_BUF_AVAILABLE(hdr)) {
2843 				ASSERT(buf->b_efunc == NULL);
2844 				hdr->b_flags &= ~ARC_BUF_AVAILABLE;
2845 			} else {
2846 				buf = arc_buf_clone(buf);
2847 			}
2848 
2849 		} else if (*arc_flags & ARC_PREFETCH &&
2850 		    refcount_count(&hdr->b_refcnt) == 0) {
2851 			hdr->b_flags |= ARC_PREFETCH;
2852 		}
2853 		DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
2854 		arc_access(hdr, hash_lock);
2855 		if (*arc_flags & ARC_L2CACHE)
2856 			hdr->b_flags |= ARC_L2CACHE;
2857 		mutex_exit(hash_lock);
2858 		ARCSTAT_BUMP(arcstat_hits);
2859 		ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2860 		    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
2861 		    data, metadata, hits);
2862 
2863 		if (done)
2864 			done(NULL, buf, private);
2865 	} else {
2866 		uint64_t size = BP_GET_LSIZE(bp);
2867 		arc_callback_t	*acb;
2868 		vdev_t *vd = NULL;
2869 		uint64_t addr;
2870 		boolean_t devw = B_FALSE;
2871 
2872 		if (hdr == NULL) {
2873 			/* this block is not in the cache */
2874 			arc_buf_hdr_t	*exists;
2875 			arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
2876 			buf = arc_buf_alloc(spa, size, private, type);
2877 			hdr = buf->b_hdr;
2878 			hdr->b_dva = *BP_IDENTITY(bp);
2879 			hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
2880 			hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
2881 			exists = buf_hash_insert(hdr, &hash_lock);
2882 			if (exists) {
2883 				/* somebody beat us to the hash insert */
2884 				mutex_exit(hash_lock);
2885 				buf_discard_identity(hdr);
2886 				(void) arc_buf_remove_ref(buf, private);
2887 				goto top; /* restart the IO request */
2888 			}
2889 			/* if this is a prefetch, we don't have a reference */
2890 			if (*arc_flags & ARC_PREFETCH) {
2891 				(void) remove_reference(hdr, hash_lock,
2892 				    private);
2893 				hdr->b_flags |= ARC_PREFETCH;
2894 			}
2895 			if (*arc_flags & ARC_L2CACHE)
2896 				hdr->b_flags |= ARC_L2CACHE;
2897 			if (BP_GET_LEVEL(bp) > 0)
2898 				hdr->b_flags |= ARC_INDIRECT;
2899 		} else {
2900 			/* this block is in the ghost cache */
2901 			ASSERT(GHOST_STATE(hdr->b_state));
2902 			ASSERT(!HDR_IO_IN_PROGRESS(hdr));
2903 			ASSERT0(refcount_count(&hdr->b_refcnt));
2904 			ASSERT(hdr->b_buf == NULL);
2905 
2906 			/* if this is a prefetch, we don't have a reference */
2907 			if (*arc_flags & ARC_PREFETCH)
2908 				hdr->b_flags |= ARC_PREFETCH;
2909 			else
2910 				add_reference(hdr, hash_lock, private);
2911 			if (*arc_flags & ARC_L2CACHE)
2912 				hdr->b_flags |= ARC_L2CACHE;
2913 			buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
2914 			buf->b_hdr = hdr;
2915 			buf->b_data = NULL;
2916 			buf->b_efunc = NULL;
2917 			buf->b_private = NULL;
2918 			buf->b_next = NULL;
2919 			hdr->b_buf = buf;
2920 			ASSERT(hdr->b_datacnt == 0);
2921 			hdr->b_datacnt = 1;
2922 			arc_get_data_buf(buf);
2923 			arc_access(hdr, hash_lock);
2924 		}
2925 
2926 		ASSERT(!GHOST_STATE(hdr->b_state));
2927 
2928 		acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
2929 		acb->acb_done = done;
2930 		acb->acb_private = private;
2931 
2932 		ASSERT(hdr->b_acb == NULL);
2933 		hdr->b_acb = acb;
2934 		hdr->b_flags |= ARC_IO_IN_PROGRESS;
2935 
2936 		if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
2937 		    (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
2938 			devw = hdr->b_l2hdr->b_dev->l2ad_writing;
2939 			addr = hdr->b_l2hdr->b_daddr;
2940 			/*
2941 			 * Lock out device removal.
2942 			 */
2943 			if (vdev_is_dead(vd) ||
2944 			    !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
2945 				vd = NULL;
2946 		}
2947 
2948 		mutex_exit(hash_lock);
2949 
2950 		ASSERT3U(hdr->b_size, ==, size);
2951 		DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
2952 		    uint64_t, size, zbookmark_t *, zb);
2953 		ARCSTAT_BUMP(arcstat_misses);
2954 		ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
2955 		    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
2956 		    data, metadata, misses);
2957 
2958 		if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
2959 			/*
2960 			 * Read from the L2ARC if the following are true:
2961 			 * 1. The L2ARC vdev was previously cached.
2962 			 * 2. This buffer still has L2ARC metadata.
2963 			 * 3. This buffer isn't currently writing to the L2ARC.
2964 			 * 4. The L2ARC entry wasn't evicted, which may
2965 			 *    also have invalidated the vdev.
2966 			 * 5. This isn't prefetch and l2arc_noprefetch is set.
2967 			 */
2968 			if (hdr->b_l2hdr != NULL &&
2969 			    !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
2970 			    !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
2971 				l2arc_read_callback_t *cb;
2972 
2973 				DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
2974 				ARCSTAT_BUMP(arcstat_l2_hits);
2975 
2976 				cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
2977 				    KM_SLEEP);
2978 				cb->l2rcb_buf = buf;
2979 				cb->l2rcb_spa = spa;
2980 				cb->l2rcb_bp = *bp;
2981 				cb->l2rcb_zb = *zb;
2982 				cb->l2rcb_flags = zio_flags;
2983 
2984 				/*
2985 				 * l2arc read.  The SCL_L2ARC lock will be
2986 				 * released by l2arc_read_done().
2987 				 */
2988 				rzio = zio_read_phys(pio, vd, addr, size,
2989 				    buf->b_data, ZIO_CHECKSUM_OFF,
2990 				    l2arc_read_done, cb, priority, zio_flags |
2991 				    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL |
2992 				    ZIO_FLAG_DONT_PROPAGATE |
2993 				    ZIO_FLAG_DONT_RETRY, B_FALSE);
2994 				DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
2995 				    zio_t *, rzio);
2996 				ARCSTAT_INCR(arcstat_l2_read_bytes, size);
2997 
2998 				if (*arc_flags & ARC_NOWAIT) {
2999 					zio_nowait(rzio);
3000 					return (0);
3001 				}
3002 
3003 				ASSERT(*arc_flags & ARC_WAIT);
3004 				if (zio_wait(rzio) == 0)
3005 					return (0);
3006 
3007 				/* l2arc read error; goto zio_read() */
3008 			} else {
3009 				DTRACE_PROBE1(l2arc__miss,
3010 				    arc_buf_hdr_t *, hdr);
3011 				ARCSTAT_BUMP(arcstat_l2_misses);
3012 				if (HDR_L2_WRITING(hdr))
3013 					ARCSTAT_BUMP(arcstat_l2_rw_clash);
3014 				spa_config_exit(spa, SCL_L2ARC, vd);
3015 			}
3016 		} else {
3017 			if (vd != NULL)
3018 				spa_config_exit(spa, SCL_L2ARC, vd);
3019 			if (l2arc_ndev != 0) {
3020 				DTRACE_PROBE1(l2arc__miss,
3021 				    arc_buf_hdr_t *, hdr);
3022 				ARCSTAT_BUMP(arcstat_l2_misses);
3023 			}
3024 		}
3025 
3026 		rzio = zio_read(pio, spa, bp, buf->b_data, size,
3027 		    arc_read_done, buf, priority, zio_flags, zb);
3028 
3029 		if (*arc_flags & ARC_WAIT)
3030 			return (zio_wait(rzio));
3031 
3032 		ASSERT(*arc_flags & ARC_NOWAIT);
3033 		zio_nowait(rzio);
3034 	}
3035 	return (0);
3036 }
3037 
3038 void
3039 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
3040 {
3041 	ASSERT(buf->b_hdr != NULL);
3042 	ASSERT(buf->b_hdr->b_state != arc_anon);
3043 	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
3044 	ASSERT(buf->b_efunc == NULL);
3045 	ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
3046 
3047 	buf->b_efunc = func;
3048 	buf->b_private = private;
3049 }
3050 
3051 /*
3052  * This is used by the DMU to let the ARC know that a buffer is
3053  * being evicted, so the ARC should clean up.  If this arc buf
3054  * is not yet in the evicted state, it will be put there.
3055  */
3056 int
3057 arc_buf_evict(arc_buf_t *buf)
3058 {
3059 	arc_buf_hdr_t *hdr;
3060 	kmutex_t *hash_lock;
3061 	arc_buf_t **bufp;
3062 
3063 	mutex_enter(&buf->b_evict_lock);
3064 	hdr = buf->b_hdr;
3065 	if (hdr == NULL) {
3066 		/*
3067 		 * We are in arc_do_user_evicts().
3068 		 */
3069 		ASSERT(buf->b_data == NULL);
3070 		mutex_exit(&buf->b_evict_lock);
3071 		return (0);
3072 	} else if (buf->b_data == NULL) {
3073 		arc_buf_t copy = *buf; /* structure assignment */
3074 		/*
3075 		 * We are on the eviction list; process this buffer now
3076 		 * but let arc_do_user_evicts() do the reaping.
3077 		 */
3078 		buf->b_efunc = NULL;
3079 		mutex_exit(&buf->b_evict_lock);
3080 		VERIFY(copy.b_efunc(&copy) == 0);
3081 		return (1);
3082 	}
3083 	hash_lock = HDR_LOCK(hdr);
3084 	mutex_enter(hash_lock);
3085 	hdr = buf->b_hdr;
3086 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3087 
3088 	ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
3089 	ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
3090 
3091 	/*
3092 	 * Pull this buffer off of the hdr
3093 	 */
3094 	bufp = &hdr->b_buf;
3095 	while (*bufp != buf)
3096 		bufp = &(*bufp)->b_next;
3097 	*bufp = buf->b_next;
3098 
3099 	ASSERT(buf->b_data != NULL);
3100 	arc_buf_destroy(buf, FALSE, FALSE);
3101 
3102 	if (hdr->b_datacnt == 0) {
3103 		arc_state_t *old_state = hdr->b_state;
3104 		arc_state_t *evicted_state;
3105 
3106 		ASSERT(hdr->b_buf == NULL);
3107 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
3108 
3109 		evicted_state =
3110 		    (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
3111 
3112 		mutex_enter(&old_state->arcs_mtx);
3113 		mutex_enter(&evicted_state->arcs_mtx);
3114 
3115 		arc_change_state(evicted_state, hdr, hash_lock);
3116 		ASSERT(HDR_IN_HASH_TABLE(hdr));
3117 		hdr->b_flags |= ARC_IN_HASH_TABLE;
3118 		hdr->b_flags &= ~ARC_BUF_AVAILABLE;
3119 
3120 		mutex_exit(&evicted_state->arcs_mtx);
3121 		mutex_exit(&old_state->arcs_mtx);
3122 	}
3123 	mutex_exit(hash_lock);
3124 	mutex_exit(&buf->b_evict_lock);
3125 
3126 	VERIFY(buf->b_efunc(buf) == 0);
3127 	buf->b_efunc = NULL;
3128 	buf->b_private = NULL;
3129 	buf->b_hdr = NULL;
3130 	buf->b_next = NULL;
3131 	kmem_cache_free(buf_cache, buf);
3132 	return (1);
3133 }
3134 
3135 /*
3136  * Release this buffer from the cache.  This must be done
3137  * after a read and prior to modifying the buffer contents.
3138  * If the buffer has more than one reference, we must make
3139  * a new hdr for the buffer.
3140  */
3141 void
3142 arc_release(arc_buf_t *buf, void *tag)
3143 {
3144 	arc_buf_hdr_t *hdr;
3145 	kmutex_t *hash_lock = NULL;
3146 	l2arc_buf_hdr_t *l2hdr;
3147 	uint64_t buf_size;
3148 
3149 	/*
3150 	 * It would be nice to assert that if it's DMU metadata (level >
3151 	 * 0 || it's the dnode file), then it must be syncing context.
3152 	 * But we don't know that information at this level.
3153 	 */
3154 
3155 	mutex_enter(&buf->b_evict_lock);
3156 	hdr = buf->b_hdr;
3157 
3158 	/* this buffer is not on any list */
3159 	ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3160 
3161 	if (hdr->b_state == arc_anon) {
3162 		/* this buffer is already released */
3163 		ASSERT(buf->b_efunc == NULL);
3164 	} else {
3165 		hash_lock = HDR_LOCK(hdr);
3166 		mutex_enter(hash_lock);
3167 		hdr = buf->b_hdr;
3168 		ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3169 	}
3170 
3171 	l2hdr = hdr->b_l2hdr;
3172 	if (l2hdr) {
3173 		mutex_enter(&l2arc_buflist_mtx);
3174 		hdr->b_l2hdr = NULL;
3175 		buf_size = hdr->b_size;
3176 	}
3177 
3178 	/*
3179 	 * Do we have more than one buf?
3180 	 */
3181 	if (hdr->b_datacnt > 1) {
3182 		arc_buf_hdr_t *nhdr;
3183 		arc_buf_t **bufp;
3184 		uint64_t blksz = hdr->b_size;
3185 		uint64_t spa = hdr->b_spa;
3186 		arc_buf_contents_t type = hdr->b_type;
3187 		uint32_t flags = hdr->b_flags;
3188 
3189 		ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3190 		/*
3191 		 * Pull the data off of this hdr and attach it to
3192 		 * a new anonymous hdr.
3193 		 */
3194 		(void) remove_reference(hdr, hash_lock, tag);
3195 		bufp = &hdr->b_buf;
3196 		while (*bufp != buf)
3197 			bufp = &(*bufp)->b_next;
3198 		*bufp = buf->b_next;
3199 		buf->b_next = NULL;
3200 
3201 		ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
3202 		atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
3203 		if (refcount_is_zero(&hdr->b_refcnt)) {
3204 			uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
3205 			ASSERT3U(*size, >=, hdr->b_size);
3206 			atomic_add_64(size, -hdr->b_size);
3207 		}
3208 
3209 		/*
3210 		 * We're releasing a duplicate user data buffer, update
3211 		 * our statistics accordingly.
3212 		 */
3213 		if (hdr->b_type == ARC_BUFC_DATA) {
3214 			ARCSTAT_BUMPDOWN(arcstat_duplicate_buffers);
3215 			ARCSTAT_INCR(arcstat_duplicate_buffers_size,
3216 			    -hdr->b_size);
3217 		}
3218 		hdr->b_datacnt -= 1;
3219 		arc_cksum_verify(buf);
3220 		arc_buf_unwatch(buf);
3221 
3222 		mutex_exit(hash_lock);
3223 
3224 		nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3225 		nhdr->b_size = blksz;
3226 		nhdr->b_spa = spa;
3227 		nhdr->b_type = type;
3228 		nhdr->b_buf = buf;
3229 		nhdr->b_state = arc_anon;
3230 		nhdr->b_arc_access = 0;
3231 		nhdr->b_flags = flags & ARC_L2_WRITING;
3232 		nhdr->b_l2hdr = NULL;
3233 		nhdr->b_datacnt = 1;
3234 		nhdr->b_freeze_cksum = NULL;
3235 		(void) refcount_add(&nhdr->b_refcnt, tag);
3236 		buf->b_hdr = nhdr;
3237 		mutex_exit(&buf->b_evict_lock);
3238 		atomic_add_64(&arc_anon->arcs_size, blksz);
3239 	} else {
3240 		mutex_exit(&buf->b_evict_lock);
3241 		ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3242 		ASSERT(!list_link_active(&hdr->b_arc_node));
3243 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
3244 		if (hdr->b_state != arc_anon)
3245 			arc_change_state(arc_anon, hdr, hash_lock);
3246 		hdr->b_arc_access = 0;
3247 		if (hash_lock)
3248 			mutex_exit(hash_lock);
3249 
3250 		buf_discard_identity(hdr);
3251 		arc_buf_thaw(buf);
3252 	}
3253 	buf->b_efunc = NULL;
3254 	buf->b_private = NULL;
3255 
3256 	if (l2hdr) {
3257 		list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
3258 		kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
3259 		ARCSTAT_INCR(arcstat_l2_size, -buf_size);
3260 		mutex_exit(&l2arc_buflist_mtx);
3261 	}
3262 }
3263 
3264 int
3265 arc_released(arc_buf_t *buf)
3266 {
3267 	int released;
3268 
3269 	mutex_enter(&buf->b_evict_lock);
3270 	released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
3271 	mutex_exit(&buf->b_evict_lock);
3272 	return (released);
3273 }
3274 
3275 int
3276 arc_has_callback(arc_buf_t *buf)
3277 {
3278 	int callback;
3279 
3280 	mutex_enter(&buf->b_evict_lock);
3281 	callback = (buf->b_efunc != NULL);
3282 	mutex_exit(&buf->b_evict_lock);
3283 	return (callback);
3284 }
3285 
3286 #ifdef ZFS_DEBUG
3287 int
3288 arc_referenced(arc_buf_t *buf)
3289 {
3290 	int referenced;
3291 
3292 	mutex_enter(&buf->b_evict_lock);
3293 	referenced = (refcount_count(&buf->b_hdr->b_refcnt));
3294 	mutex_exit(&buf->b_evict_lock);
3295 	return (referenced);
3296 }
3297 #endif
3298 
3299 static void
3300 arc_write_ready(zio_t *zio)
3301 {
3302 	arc_write_callback_t *callback = zio->io_private;
3303 	arc_buf_t *buf = callback->awcb_buf;
3304 	arc_buf_hdr_t *hdr = buf->b_hdr;
3305 
3306 	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
3307 	callback->awcb_ready(zio, buf, callback->awcb_private);
3308 
3309 	/*
3310 	 * If the IO is already in progress, then this is a re-write
3311 	 * attempt, so we need to thaw and re-compute the cksum.
3312 	 * It is the responsibility of the callback to handle the
3313 	 * accounting for any re-write attempt.
3314 	 */
3315 	if (HDR_IO_IN_PROGRESS(hdr)) {
3316 		mutex_enter(&hdr->b_freeze_lock);
3317 		if (hdr->b_freeze_cksum != NULL) {
3318 			kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
3319 			hdr->b_freeze_cksum = NULL;
3320 		}
3321 		mutex_exit(&hdr->b_freeze_lock);
3322 	}
3323 	arc_cksum_compute(buf, B_FALSE);
3324 	hdr->b_flags |= ARC_IO_IN_PROGRESS;
3325 }
3326 
3327 static void
3328 arc_write_done(zio_t *zio)
3329 {
3330 	arc_write_callback_t *callback = zio->io_private;
3331 	arc_buf_t *buf = callback->awcb_buf;
3332 	arc_buf_hdr_t *hdr = buf->b_hdr;
3333 
3334 	ASSERT(hdr->b_acb == NULL);
3335 
3336 	if (zio->io_error == 0) {
3337 		hdr->b_dva = *BP_IDENTITY(zio->io_bp);
3338 		hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
3339 		hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
3340 	} else {
3341 		ASSERT(BUF_EMPTY(hdr));
3342 	}
3343 
3344 	/*
3345 	 * If the block to be written was all-zero, we may have
3346 	 * compressed it away.  In this case no write was performed
3347 	 * so there will be no dva/birth/checksum.  The buffer must
3348 	 * therefore remain anonymous (and uncached).
3349 	 */
3350 	if (!BUF_EMPTY(hdr)) {
3351 		arc_buf_hdr_t *exists;
3352 		kmutex_t *hash_lock;
3353 
3354 		ASSERT(zio->io_error == 0);
3355 
3356 		arc_cksum_verify(buf);
3357 
3358 		exists = buf_hash_insert(hdr, &hash_lock);
3359 		if (exists) {
3360 			/*
3361 			 * This can only happen if we overwrite for
3362 			 * sync-to-convergence, because we remove
3363 			 * buffers from the hash table when we arc_free().
3364 			 */
3365 			if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
3366 				if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3367 					panic("bad overwrite, hdr=%p exists=%p",
3368 					    (void *)hdr, (void *)exists);
3369 				ASSERT(refcount_is_zero(&exists->b_refcnt));
3370 				arc_change_state(arc_anon, exists, hash_lock);
3371 				mutex_exit(hash_lock);
3372 				arc_hdr_destroy(exists);
3373 				exists = buf_hash_insert(hdr, &hash_lock);
3374 				ASSERT3P(exists, ==, NULL);
3375 			} else if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
3376 				/* nopwrite */
3377 				ASSERT(zio->io_prop.zp_nopwrite);
3378 				if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
3379 					panic("bad nopwrite, hdr=%p exists=%p",
3380 					    (void *)hdr, (void *)exists);
3381 			} else {
3382 				/* Dedup */
3383 				ASSERT(hdr->b_datacnt == 1);
3384 				ASSERT(hdr->b_state == arc_anon);
3385 				ASSERT(BP_GET_DEDUP(zio->io_bp));
3386 				ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
3387 			}
3388 		}
3389 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3390 		/* if it's not anon, we are doing a scrub */
3391 		if (!exists && hdr->b_state == arc_anon)
3392 			arc_access(hdr, hash_lock);
3393 		mutex_exit(hash_lock);
3394 	} else {
3395 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3396 	}
3397 
3398 	ASSERT(!refcount_is_zero(&hdr->b_refcnt));
3399 	callback->awcb_done(zio, buf, callback->awcb_private);
3400 
3401 	kmem_free(callback, sizeof (arc_write_callback_t));
3402 }
3403 
3404 zio_t *
3405 arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
3406     blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
3407     arc_done_func_t *ready, arc_done_func_t *done, void *private,
3408     int priority, int zio_flags, const zbookmark_t *zb)
3409 {
3410 	arc_buf_hdr_t *hdr = buf->b_hdr;
3411 	arc_write_callback_t *callback;
3412 	zio_t *zio;
3413 
3414 	ASSERT(ready != NULL);
3415 	ASSERT(done != NULL);
3416 	ASSERT(!HDR_IO_ERROR(hdr));
3417 	ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
3418 	ASSERT(hdr->b_acb == NULL);
3419 	if (l2arc)
3420 		hdr->b_flags |= ARC_L2CACHE;
3421 	callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
3422 	callback->awcb_ready = ready;
3423 	callback->awcb_done = done;
3424 	callback->awcb_private = private;
3425 	callback->awcb_buf = buf;
3426 
3427 	zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
3428 	    arc_write_ready, arc_write_done, callback, priority, zio_flags, zb);
3429 
3430 	return (zio);
3431 }
3432 
3433 static int
3434 arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg)
3435 {
3436 #ifdef _KERNEL
3437 	uint64_t available_memory = ptob(freemem);
3438 	static uint64_t page_load = 0;
3439 	static uint64_t last_txg = 0;
3440 
3441 #if defined(__i386)
3442 	available_memory =
3443 	    MIN(available_memory, vmem_size(heap_arena, VMEM_FREE));
3444 #endif
3445 	if (available_memory >= zfs_write_limit_max)
3446 		return (0);
3447 
3448 	if (txg > last_txg) {
3449 		last_txg = txg;
3450 		page_load = 0;
3451 	}
3452 	/*
3453 	 * If we are in pageout, we know that memory is already tight,
3454 	 * the arc is already going to be evicting, so we just want to
3455 	 * continue to let page writes occur as quickly as possible.
3456 	 */
3457 	if (curproc == proc_pageout) {
3458 		if (page_load > MAX(ptob(minfree), available_memory) / 4)
3459 			return (ERESTART);
3460 		/* Note: reserve is inflated, so we deflate */
3461 		page_load += reserve / 8;
3462 		return (0);
3463 	} else if (page_load > 0 && arc_reclaim_needed()) {
3464 		/* memory is low, delay before restarting */
3465 		ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3466 		return (EAGAIN);
3467 	}
3468 	page_load = 0;
3469 
3470 	if (arc_size > arc_c_min) {
3471 		uint64_t evictable_memory =
3472 		    arc_mru->arcs_lsize[ARC_BUFC_DATA] +
3473 		    arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
3474 		    arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
3475 		    arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
3476 		available_memory += MIN(evictable_memory, arc_size - arc_c_min);
3477 	}
3478 
3479 	if (inflight_data > available_memory / 4) {
3480 		ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
3481 		return (ERESTART);
3482 	}
3483 #endif
3484 	return (0);
3485 }
3486 
3487 void
3488 arc_tempreserve_clear(uint64_t reserve)
3489 {
3490 	atomic_add_64(&arc_tempreserve, -reserve);
3491 	ASSERT((int64_t)arc_tempreserve >= 0);
3492 }
3493 
3494 int
3495 arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3496 {
3497 	int error;
3498 	uint64_t anon_size;
3499 
3500 #ifdef ZFS_DEBUG
3501 	/*
3502 	 * Once in a while, fail for no reason.  Everything should cope.
3503 	 */
3504 	if (spa_get_random(10000) == 0) {
3505 		dprintf("forcing random failure\n");
3506 		return (ERESTART);
3507 	}
3508 #endif
3509 	if (reserve > arc_c/4 && !arc_no_grow)
3510 		arc_c = MIN(arc_c_max, reserve * 4);
3511 	if (reserve > arc_c)
3512 		return (ENOMEM);
3513 
3514 	/*
3515 	 * Don't count loaned bufs as in flight dirty data to prevent long
3516 	 * network delays from blocking transactions that are ready to be
3517 	 * assigned to a txg.
3518 	 */
3519 	anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
3520 
3521 	/*
3522 	 * Writes will, almost always, require additional memory allocations
3523 	 * in order to compress/encrypt/etc the data.  We therefor need to
3524 	 * make sure that there is sufficient available memory for this.
3525 	 */
3526 	if (error = arc_memory_throttle(reserve, anon_size, txg))
3527 		return (error);
3528 
3529 	/*
3530 	 * Throttle writes when the amount of dirty data in the cache
3531 	 * gets too large.  We try to keep the cache less than half full
3532 	 * of dirty blocks so that our sync times don't grow too large.
3533 	 * Note: if two requests come in concurrently, we might let them
3534 	 * both succeed, when one of them should fail.  Not a huge deal.
3535 	 */
3536 
3537 	if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
3538 	    anon_size > arc_c / 4) {
3539 		dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
3540 		    "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
3541 		    arc_tempreserve>>10,
3542 		    arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
3543 		    arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
3544 		    reserve>>10, arc_c>>10);
3545 		return (ERESTART);
3546 	}
3547 	atomic_add_64(&arc_tempreserve, reserve);
3548 	return (0);
3549 }
3550 
3551 void
3552 arc_init(void)
3553 {
3554 	mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3555 	cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3556 
3557 	/* Convert seconds to clock ticks */
3558 	arc_min_prefetch_lifespan = 1 * hz;
3559 
3560 	/* Start out with 1/8 of all memory */
3561 	arc_c = physmem * PAGESIZE / 8;
3562 
3563 #ifdef _KERNEL
3564 	/*
3565 	 * On architectures where the physical memory can be larger
3566 	 * than the addressable space (intel in 32-bit mode), we may
3567 	 * need to limit the cache to 1/8 of VM size.
3568 	 */
3569 	arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3570 #endif
3571 
3572 	/* set min cache to 1/32 of all memory, or 64MB, whichever is more */
3573 	arc_c_min = MAX(arc_c / 4, 64<<20);
3574 	/* set max to 3/4 of all memory, or all but 1GB, whichever is more */
3575 	if (arc_c * 8 >= 1<<30)
3576 		arc_c_max = (arc_c * 8) - (1<<30);
3577 	else
3578 		arc_c_max = arc_c_min;
3579 	arc_c_max = MAX(arc_c * 6, arc_c_max);
3580 
3581 	/*
3582 	 * Allow the tunables to override our calculations if they are
3583 	 * reasonable (ie. over 64MB)
3584 	 */
3585 	if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
3586 		arc_c_max = zfs_arc_max;
3587 	if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
3588 		arc_c_min = zfs_arc_min;
3589 
3590 	arc_c = arc_c_max;
3591 	arc_p = (arc_c >> 1);
3592 
3593 	/* limit meta-data to 1/4 of the arc capacity */
3594 	arc_meta_limit = arc_c_max / 4;
3595 
3596 	/* Allow the tunable to override if it is reasonable */
3597 	if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
3598 		arc_meta_limit = zfs_arc_meta_limit;
3599 
3600 	if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
3601 		arc_c_min = arc_meta_limit / 2;
3602 
3603 	if (zfs_arc_grow_retry > 0)
3604 		arc_grow_retry = zfs_arc_grow_retry;
3605 
3606 	if (zfs_arc_shrink_shift > 0)
3607 		arc_shrink_shift = zfs_arc_shrink_shift;
3608 
3609 	if (zfs_arc_p_min_shift > 0)
3610 		arc_p_min_shift = zfs_arc_p_min_shift;
3611 
3612 	/* if kmem_flags are set, lets try to use less memory */
3613 	if (kmem_debugging())
3614 		arc_c = arc_c / 2;
3615 	if (arc_c < arc_c_min)
3616 		arc_c = arc_c_min;
3617 
3618 	arc_anon = &ARC_anon;
3619 	arc_mru = &ARC_mru;
3620 	arc_mru_ghost = &ARC_mru_ghost;
3621 	arc_mfu = &ARC_mfu;
3622 	arc_mfu_ghost = &ARC_mfu_ghost;
3623 	arc_l2c_only = &ARC_l2c_only;
3624 	arc_size = 0;
3625 
3626 	mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3627 	mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3628 	mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3629 	mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3630 	mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3631 	mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
3632 
3633 	list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
3634 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3635 	list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
3636 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3637 	list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
3638 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3639 	list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
3640 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3641 	list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
3642 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3643 	list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
3644 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3645 	list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
3646 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3647 	list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
3648 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3649 	list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
3650 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3651 	list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
3652 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3653 
3654 	buf_init();
3655 
3656 	arc_thread_exit = 0;
3657 	arc_eviction_list = NULL;
3658 	mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
3659 	bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3660 
3661 	arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
3662 	    sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
3663 
3664 	if (arc_ksp != NULL) {
3665 		arc_ksp->ks_data = &arc_stats;
3666 		kstat_install(arc_ksp);
3667 	}
3668 
3669 	(void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
3670 	    TS_RUN, minclsyspri);
3671 
3672 	arc_dead = FALSE;
3673 	arc_warm = B_FALSE;
3674 
3675 	if (zfs_write_limit_max == 0)
3676 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
3677 	else
3678 		zfs_write_limit_shift = 0;
3679 	mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL);
3680 }
3681 
3682 void
3683 arc_fini(void)
3684 {
3685 	mutex_enter(&arc_reclaim_thr_lock);
3686 	arc_thread_exit = 1;
3687 	while (arc_thread_exit != 0)
3688 		cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3689 	mutex_exit(&arc_reclaim_thr_lock);
3690 
3691 	arc_flush(NULL);
3692 
3693 	arc_dead = TRUE;
3694 
3695 	if (arc_ksp != NULL) {
3696 		kstat_delete(arc_ksp);
3697 		arc_ksp = NULL;
3698 	}
3699 
3700 	mutex_destroy(&arc_eviction_mtx);
3701 	mutex_destroy(&arc_reclaim_thr_lock);
3702 	cv_destroy(&arc_reclaim_thr_cv);
3703 
3704 	list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
3705 	list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
3706 	list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
3707 	list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
3708 	list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
3709 	list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
3710 	list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
3711 	list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3712 
3713 	mutex_destroy(&arc_anon->arcs_mtx);
3714 	mutex_destroy(&arc_mru->arcs_mtx);
3715 	mutex_destroy(&arc_mru_ghost->arcs_mtx);
3716 	mutex_destroy(&arc_mfu->arcs_mtx);
3717 	mutex_destroy(&arc_mfu_ghost->arcs_mtx);
3718 	mutex_destroy(&arc_l2c_only->arcs_mtx);
3719 
3720 	mutex_destroy(&zfs_write_limit_lock);
3721 
3722 	buf_fini();
3723 
3724 	ASSERT(arc_loaned_bytes == 0);
3725 }
3726 
3727 /*
3728  * Level 2 ARC
3729  *
3730  * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
3731  * It uses dedicated storage devices to hold cached data, which are populated
3732  * using large infrequent writes.  The main role of this cache is to boost
3733  * the performance of random read workloads.  The intended L2ARC devices
3734  * include short-stroked disks, solid state disks, and other media with
3735  * substantially faster read latency than disk.
3736  *
3737  *                 +-----------------------+
3738  *                 |         ARC           |
3739  *                 +-----------------------+
3740  *                    |         ^     ^
3741  *                    |         |     |
3742  *      l2arc_feed_thread()    arc_read()
3743  *                    |         |     |
3744  *                    |  l2arc read   |
3745  *                    V         |     |
3746  *               +---------------+    |
3747  *               |     L2ARC     |    |
3748  *               +---------------+    |
3749  *                   |    ^           |
3750  *          l2arc_write() |           |
3751  *                   |    |           |
3752  *                   V    |           |
3753  *                 +-------+      +-------+
3754  *                 | vdev  |      | vdev  |
3755  *                 | cache |      | cache |
3756  *                 +-------+      +-------+
3757  *                 +=========+     .-----.
3758  *                 :  L2ARC  :    |-_____-|
3759  *                 : devices :    | Disks |
3760  *                 +=========+    `-_____-'
3761  *
3762  * Read requests are satisfied from the following sources, in order:
3763  *
3764  *	1) ARC
3765  *	2) vdev cache of L2ARC devices
3766  *	3) L2ARC devices
3767  *	4) vdev cache of disks
3768  *	5) disks
3769  *
3770  * Some L2ARC device types exhibit extremely slow write performance.
3771  * To accommodate for this there are some significant differences between
3772  * the L2ARC and traditional cache design:
3773  *
3774  * 1. There is no eviction path from the ARC to the L2ARC.  Evictions from
3775  * the ARC behave as usual, freeing buffers and placing headers on ghost
3776  * lists.  The ARC does not send buffers to the L2ARC during eviction as
3777  * this would add inflated write latencies for all ARC memory pressure.
3778  *
3779  * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
3780  * It does this by periodically scanning buffers from the eviction-end of
3781  * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
3782  * not already there.  It scans until a headroom of buffers is satisfied,
3783  * which itself is a buffer for ARC eviction.  The thread that does this is
3784  * l2arc_feed_thread(), illustrated below; example sizes are included to
3785  * provide a better sense of ratio than this diagram:
3786  *
3787  *	       head -->                        tail
3788  *	        +---------------------+----------+
3789  *	ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->.   # already on L2ARC
3790  *	        +---------------------+----------+   |   o L2ARC eligible
3791  *	ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->|   : ARC buffer
3792  *	        +---------------------+----------+   |
3793  *	             15.9 Gbytes      ^ 32 Mbytes    |
3794  *	                           headroom          |
3795  *	                                      l2arc_feed_thread()
3796  *	                                             |
3797  *	                 l2arc write hand <--[oooo]--'
3798  *	                         |           8 Mbyte
3799  *	                         |          write max
3800  *	                         V
3801  *		  +==============================+
3802  *	L2ARC dev |####|#|###|###|    |####| ... |
3803  *	          +==============================+
3804  *	                     32 Gbytes
3805  *
3806  * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
3807  * evicted, then the L2ARC has cached a buffer much sooner than it probably
3808  * needed to, potentially wasting L2ARC device bandwidth and storage.  It is
3809  * safe to say that this is an uncommon case, since buffers at the end of
3810  * the ARC lists have moved there due to inactivity.
3811  *
3812  * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
3813  * then the L2ARC simply misses copying some buffers.  This serves as a
3814  * pressure valve to prevent heavy read workloads from both stalling the ARC
3815  * with waits and clogging the L2ARC with writes.  This also helps prevent
3816  * the potential for the L2ARC to churn if it attempts to cache content too
3817  * quickly, such as during backups of the entire pool.
3818  *
3819  * 5. After system boot and before the ARC has filled main memory, there are
3820  * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
3821  * lists can remain mostly static.  Instead of searching from tail of these
3822  * lists as pictured, the l2arc_feed_thread() will search from the list heads
3823  * for eligible buffers, greatly increasing its chance of finding them.
3824  *
3825  * The L2ARC device write speed is also boosted during this time so that
3826  * the L2ARC warms up faster.  Since there have been no ARC evictions yet,
3827  * there are no L2ARC reads, and no fear of degrading read performance
3828  * through increased writes.
3829  *
3830  * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
3831  * the vdev queue can aggregate them into larger and fewer writes.  Each
3832  * device is written to in a rotor fashion, sweeping writes through
3833  * available space then repeating.
3834  *
3835  * 7. The L2ARC does not store dirty content.  It never needs to flush
3836  * write buffers back to disk based storage.
3837  *
3838  * 8. If an ARC buffer is written (and dirtied) which also exists in the
3839  * L2ARC, the now stale L2ARC buffer is immediately dropped.
3840  *
3841  * The performance of the L2ARC can be tweaked by a number of tunables, which
3842  * may be necessary for different workloads:
3843  *
3844  *	l2arc_write_max		max write bytes per interval
3845  *	l2arc_write_boost	extra write bytes during device warmup
3846  *	l2arc_noprefetch	skip caching prefetched buffers
3847  *	l2arc_headroom		number of max device writes to precache
3848  *	l2arc_feed_secs		seconds between L2ARC writing
3849  *
3850  * Tunables may be removed or added as future performance improvements are
3851  * integrated, and also may become zpool properties.
3852  *
3853  * There are three key functions that control how the L2ARC warms up:
3854  *
3855  *	l2arc_write_eligible()	check if a buffer is eligible to cache
3856  *	l2arc_write_size()	calculate how much to write
3857  *	l2arc_write_interval()	calculate sleep delay between writes
3858  *
3859  * These three functions determine what to write, how much, and how quickly
3860  * to send writes.
3861  */
3862 
3863 static boolean_t
3864 l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
3865 {
3866 	/*
3867 	 * A buffer is *not* eligible for the L2ARC if it:
3868 	 * 1. belongs to a different spa.
3869 	 * 2. is already cached on the L2ARC.
3870 	 * 3. has an I/O in progress (it may be an incomplete read).
3871 	 * 4. is flagged not eligible (zfs property).
3872 	 */
3873 	if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
3874 	    HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
3875 		return (B_FALSE);
3876 
3877 	return (B_TRUE);
3878 }
3879 
3880 static uint64_t
3881 l2arc_write_size(l2arc_dev_t *dev)
3882 {
3883 	uint64_t size;
3884 
3885 	size = dev->l2ad_write;
3886 
3887 	if (arc_warm == B_FALSE)
3888 		size += dev->l2ad_boost;
3889 
3890 	return (size);
3891 
3892 }
3893 
3894 static clock_t
3895 l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
3896 {
3897 	clock_t interval, next, now;
3898 
3899 	/*
3900 	 * If the ARC lists are busy, increase our write rate; if the
3901 	 * lists are stale, idle back.  This is achieved by checking
3902 	 * how much we previously wrote - if it was more than half of
3903 	 * what we wanted, schedule the next write much sooner.
3904 	 */
3905 	if (l2arc_feed_again && wrote > (wanted / 2))
3906 		interval = (hz * l2arc_feed_min_ms) / 1000;
3907 	else
3908 		interval = hz * l2arc_feed_secs;
3909 
3910 	now = ddi_get_lbolt();
3911 	next = MAX(now, MIN(now + interval, began + interval));
3912 
3913 	return (next);
3914 }
3915 
3916 static void
3917 l2arc_hdr_stat_add(void)
3918 {
3919 	ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
3920 	ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
3921 }
3922 
3923 static void
3924 l2arc_hdr_stat_remove(void)
3925 {
3926 	ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
3927 	ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
3928 }
3929 
3930 /*
3931  * Cycle through L2ARC devices.  This is how L2ARC load balances.
3932  * If a device is returned, this also returns holding the spa config lock.
3933  */
3934 static l2arc_dev_t *
3935 l2arc_dev_get_next(void)
3936 {
3937 	l2arc_dev_t *first, *next = NULL;
3938 
3939 	/*
3940 	 * Lock out the removal of spas (spa_namespace_lock), then removal
3941 	 * of cache devices (l2arc_dev_mtx).  Once a device has been selected,
3942 	 * both locks will be dropped and a spa config lock held instead.
3943 	 */
3944 	mutex_enter(&spa_namespace_lock);
3945 	mutex_enter(&l2arc_dev_mtx);
3946 
3947 	/* if there are no vdevs, there is nothing to do */
3948 	if (l2arc_ndev == 0)
3949 		goto out;
3950 
3951 	first = NULL;
3952 	next = l2arc_dev_last;
3953 	do {
3954 		/* loop around the list looking for a non-faulted vdev */
3955 		if (next == NULL) {
3956 			next = list_head(l2arc_dev_list);
3957 		} else {
3958 			next = list_next(l2arc_dev_list, next);
3959 			if (next == NULL)
3960 				next = list_head(l2arc_dev_list);
3961 		}
3962 
3963 		/* if we have come back to the start, bail out */
3964 		if (first == NULL)
3965 			first = next;
3966 		else if (next == first)
3967 			break;
3968 
3969 	} while (vdev_is_dead(next->l2ad_vdev));
3970 
3971 	/* if we were unable to find any usable vdevs, return NULL */
3972 	if (vdev_is_dead(next->l2ad_vdev))
3973 		next = NULL;
3974 
3975 	l2arc_dev_last = next;
3976 
3977 out:
3978 	mutex_exit(&l2arc_dev_mtx);
3979 
3980 	/*
3981 	 * Grab the config lock to prevent the 'next' device from being
3982 	 * removed while we are writing to it.
3983 	 */
3984 	if (next != NULL)
3985 		spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
3986 	mutex_exit(&spa_namespace_lock);
3987 
3988 	return (next);
3989 }
3990 
3991 /*
3992  * Free buffers that were tagged for destruction.
3993  */
3994 static void
3995 l2arc_do_free_on_write()
3996 {
3997 	list_t *buflist;
3998 	l2arc_data_free_t *df, *df_prev;
3999 
4000 	mutex_enter(&l2arc_free_on_write_mtx);
4001 	buflist = l2arc_free_on_write;
4002 
4003 	for (df = list_tail(buflist); df; df = df_prev) {
4004 		df_prev = list_prev(buflist, df);
4005 		ASSERT(df->l2df_data != NULL);
4006 		ASSERT(df->l2df_func != NULL);
4007 		df->l2df_func(df->l2df_data, df->l2df_size);
4008 		list_remove(buflist, df);
4009 		kmem_free(df, sizeof (l2arc_data_free_t));
4010 	}
4011 
4012 	mutex_exit(&l2arc_free_on_write_mtx);
4013 }
4014 
4015 /*
4016  * A write to a cache device has completed.  Update all headers to allow
4017  * reads from these buffers to begin.
4018  */
4019 static void
4020 l2arc_write_done(zio_t *zio)
4021 {
4022 	l2arc_write_callback_t *cb;
4023 	l2arc_dev_t *dev;
4024 	list_t *buflist;
4025 	arc_buf_hdr_t *head, *ab, *ab_prev;
4026 	l2arc_buf_hdr_t *abl2;
4027 	kmutex_t *hash_lock;
4028 
4029 	cb = zio->io_private;
4030 	ASSERT(cb != NULL);
4031 	dev = cb->l2wcb_dev;
4032 	ASSERT(dev != NULL);
4033 	head = cb->l2wcb_head;
4034 	ASSERT(head != NULL);
4035 	buflist = dev->l2ad_buflist;
4036 	ASSERT(buflist != NULL);
4037 	DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
4038 	    l2arc_write_callback_t *, cb);
4039 
4040 	if (zio->io_error != 0)
4041 		ARCSTAT_BUMP(arcstat_l2_writes_error);
4042 
4043 	mutex_enter(&l2arc_buflist_mtx);
4044 
4045 	/*
4046 	 * All writes completed, or an error was hit.
4047 	 */
4048 	for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
4049 		ab_prev = list_prev(buflist, ab);
4050 
4051 		hash_lock = HDR_LOCK(ab);
4052 		if (!mutex_tryenter(hash_lock)) {
4053 			/*
4054 			 * This buffer misses out.  It may be in a stage
4055 			 * of eviction.  Its ARC_L2_WRITING flag will be
4056 			 * left set, denying reads to this buffer.
4057 			 */
4058 			ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
4059 			continue;
4060 		}
4061 
4062 		if (zio->io_error != 0) {
4063 			/*
4064 			 * Error - drop L2ARC entry.
4065 			 */
4066 			list_remove(buflist, ab);
4067 			abl2 = ab->b_l2hdr;
4068 			ab->b_l2hdr = NULL;
4069 			kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4070 			ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4071 		}
4072 
4073 		/*
4074 		 * Allow ARC to begin reads to this L2ARC entry.
4075 		 */
4076 		ab->b_flags &= ~ARC_L2_WRITING;
4077 
4078 		mutex_exit(hash_lock);
4079 	}
4080 
4081 	atomic_inc_64(&l2arc_writes_done);
4082 	list_remove(buflist, head);
4083 	kmem_cache_free(hdr_cache, head);
4084 	mutex_exit(&l2arc_buflist_mtx);
4085 
4086 	l2arc_do_free_on_write();
4087 
4088 	kmem_free(cb, sizeof (l2arc_write_callback_t));
4089 }
4090 
4091 /*
4092  * A read to a cache device completed.  Validate buffer contents before
4093  * handing over to the regular ARC routines.
4094  */
4095 static void
4096 l2arc_read_done(zio_t *zio)
4097 {
4098 	l2arc_read_callback_t *cb;
4099 	arc_buf_hdr_t *hdr;
4100 	arc_buf_t *buf;
4101 	kmutex_t *hash_lock;
4102 	int equal;
4103 
4104 	ASSERT(zio->io_vd != NULL);
4105 	ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
4106 
4107 	spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
4108 
4109 	cb = zio->io_private;
4110 	ASSERT(cb != NULL);
4111 	buf = cb->l2rcb_buf;
4112 	ASSERT(buf != NULL);
4113 
4114 	hash_lock = HDR_LOCK(buf->b_hdr);
4115 	mutex_enter(hash_lock);
4116 	hdr = buf->b_hdr;
4117 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
4118 
4119 	/*
4120 	 * Check this survived the L2ARC journey.
4121 	 */
4122 	equal = arc_cksum_equal(buf);
4123 	if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
4124 		mutex_exit(hash_lock);
4125 		zio->io_private = buf;
4126 		zio->io_bp_copy = cb->l2rcb_bp;	/* XXX fix in L2ARC 2.0	*/
4127 		zio->io_bp = &zio->io_bp_copy;	/* XXX fix in L2ARC 2.0	*/
4128 		arc_read_done(zio);
4129 	} else {
4130 		mutex_exit(hash_lock);
4131 		/*
4132 		 * Buffer didn't survive caching.  Increment stats and
4133 		 * reissue to the original storage device.
4134 		 */
4135 		if (zio->io_error != 0) {
4136 			ARCSTAT_BUMP(arcstat_l2_io_error);
4137 		} else {
4138 			zio->io_error = EIO;
4139 		}
4140 		if (!equal)
4141 			ARCSTAT_BUMP(arcstat_l2_cksum_bad);
4142 
4143 		/*
4144 		 * If there's no waiter, issue an async i/o to the primary
4145 		 * storage now.  If there *is* a waiter, the caller must
4146 		 * issue the i/o in a context where it's OK to block.
4147 		 */
4148 		if (zio->io_waiter == NULL) {
4149 			zio_t *pio = zio_unique_parent(zio);
4150 
4151 			ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
4152 
4153 			zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
4154 			    buf->b_data, zio->io_size, arc_read_done, buf,
4155 			    zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
4156 		}
4157 	}
4158 
4159 	kmem_free(cb, sizeof (l2arc_read_callback_t));
4160 }
4161 
4162 /*
4163  * This is the list priority from which the L2ARC will search for pages to
4164  * cache.  This is used within loops (0..3) to cycle through lists in the
4165  * desired order.  This order can have a significant effect on cache
4166  * performance.
4167  *
4168  * Currently the metadata lists are hit first, MFU then MRU, followed by
4169  * the data lists.  This function returns a locked list, and also returns
4170  * the lock pointer.
4171  */
4172 static list_t *
4173 l2arc_list_locked(int list_num, kmutex_t **lock)
4174 {
4175 	list_t *list;
4176 
4177 	ASSERT(list_num >= 0 && list_num <= 3);
4178 
4179 	switch (list_num) {
4180 	case 0:
4181 		list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
4182 		*lock = &arc_mfu->arcs_mtx;
4183 		break;
4184 	case 1:
4185 		list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
4186 		*lock = &arc_mru->arcs_mtx;
4187 		break;
4188 	case 2:
4189 		list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
4190 		*lock = &arc_mfu->arcs_mtx;
4191 		break;
4192 	case 3:
4193 		list = &arc_mru->arcs_list[ARC_BUFC_DATA];
4194 		*lock = &arc_mru->arcs_mtx;
4195 		break;
4196 	}
4197 
4198 	ASSERT(!(MUTEX_HELD(*lock)));
4199 	mutex_enter(*lock);
4200 	return (list);
4201 }
4202 
4203 /*
4204  * Evict buffers from the device write hand to the distance specified in
4205  * bytes.  This distance may span populated buffers, it may span nothing.
4206  * This is clearing a region on the L2ARC device ready for writing.
4207  * If the 'all' boolean is set, every buffer is evicted.
4208  */
4209 static void
4210 l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
4211 {
4212 	list_t *buflist;
4213 	l2arc_buf_hdr_t *abl2;
4214 	arc_buf_hdr_t *ab, *ab_prev;
4215 	kmutex_t *hash_lock;
4216 	uint64_t taddr;
4217 
4218 	buflist = dev->l2ad_buflist;
4219 
4220 	if (buflist == NULL)
4221 		return;
4222 
4223 	if (!all && dev->l2ad_first) {
4224 		/*
4225 		 * This is the first sweep through the device.  There is
4226 		 * nothing to evict.
4227 		 */
4228 		return;
4229 	}
4230 
4231 	if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
4232 		/*
4233 		 * When nearing the end of the device, evict to the end
4234 		 * before the device write hand jumps to the start.
4235 		 */
4236 		taddr = dev->l2ad_end;
4237 	} else {
4238 		taddr = dev->l2ad_hand + distance;
4239 	}
4240 	DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
4241 	    uint64_t, taddr, boolean_t, all);
4242 
4243 top:
4244 	mutex_enter(&l2arc_buflist_mtx);
4245 	for (ab = list_tail(buflist); ab; ab = ab_prev) {
4246 		ab_prev = list_prev(buflist, ab);
4247 
4248 		hash_lock = HDR_LOCK(ab);
4249 		if (!mutex_tryenter(hash_lock)) {
4250 			/*
4251 			 * Missed the hash lock.  Retry.
4252 			 */
4253 			ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
4254 			mutex_exit(&l2arc_buflist_mtx);
4255 			mutex_enter(hash_lock);
4256 			mutex_exit(hash_lock);
4257 			goto top;
4258 		}
4259 
4260 		if (HDR_L2_WRITE_HEAD(ab)) {
4261 			/*
4262 			 * We hit a write head node.  Leave it for
4263 			 * l2arc_write_done().
4264 			 */
4265 			list_remove(buflist, ab);
4266 			mutex_exit(hash_lock);
4267 			continue;
4268 		}
4269 
4270 		if (!all && ab->b_l2hdr != NULL &&
4271 		    (ab->b_l2hdr->b_daddr > taddr ||
4272 		    ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
4273 			/*
4274 			 * We've evicted to the target address,
4275 			 * or the end of the device.
4276 			 */
4277 			mutex_exit(hash_lock);
4278 			break;
4279 		}
4280 
4281 		if (HDR_FREE_IN_PROGRESS(ab)) {
4282 			/*
4283 			 * Already on the path to destruction.
4284 			 */
4285 			mutex_exit(hash_lock);
4286 			continue;
4287 		}
4288 
4289 		if (ab->b_state == arc_l2c_only) {
4290 			ASSERT(!HDR_L2_READING(ab));
4291 			/*
4292 			 * This doesn't exist in the ARC.  Destroy.
4293 			 * arc_hdr_destroy() will call list_remove()
4294 			 * and decrement arcstat_l2_size.
4295 			 */
4296 			arc_change_state(arc_anon, ab, hash_lock);
4297 			arc_hdr_destroy(ab);
4298 		} else {
4299 			/*
4300 			 * Invalidate issued or about to be issued
4301 			 * reads, since we may be about to write
4302 			 * over this location.
4303 			 */
4304 			if (HDR_L2_READING(ab)) {
4305 				ARCSTAT_BUMP(arcstat_l2_evict_reading);
4306 				ab->b_flags |= ARC_L2_EVICTED;
4307 			}
4308 
4309 			/*
4310 			 * Tell ARC this no longer exists in L2ARC.
4311 			 */
4312 			if (ab->b_l2hdr != NULL) {
4313 				abl2 = ab->b_l2hdr;
4314 				ab->b_l2hdr = NULL;
4315 				kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
4316 				ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
4317 			}
4318 			list_remove(buflist, ab);
4319 
4320 			/*
4321 			 * This may have been leftover after a
4322 			 * failed write.
4323 			 */
4324 			ab->b_flags &= ~ARC_L2_WRITING;
4325 		}
4326 		mutex_exit(hash_lock);
4327 	}
4328 	mutex_exit(&l2arc_buflist_mtx);
4329 
4330 	vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
4331 	dev->l2ad_evict = taddr;
4332 }
4333 
4334 /*
4335  * Find and write ARC buffers to the L2ARC device.
4336  *
4337  * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
4338  * for reading until they have completed writing.
4339  */
4340 static uint64_t
4341 l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
4342 {
4343 	arc_buf_hdr_t *ab, *ab_prev, *head;
4344 	l2arc_buf_hdr_t *hdrl2;
4345 	list_t *list;
4346 	uint64_t passed_sz, write_sz, buf_sz, headroom;
4347 	void *buf_data;
4348 	kmutex_t *hash_lock, *list_lock;
4349 	boolean_t have_lock, full;
4350 	l2arc_write_callback_t *cb;
4351 	zio_t *pio, *wzio;
4352 	uint64_t guid = spa_load_guid(spa);
4353 
4354 	ASSERT(dev->l2ad_vdev != NULL);
4355 
4356 	pio = NULL;
4357 	write_sz = 0;
4358 	full = B_FALSE;
4359 	head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
4360 	head->b_flags |= ARC_L2_WRITE_HEAD;
4361 
4362 	/*
4363 	 * Copy buffers for L2ARC writing.
4364 	 */
4365 	mutex_enter(&l2arc_buflist_mtx);
4366 	for (int try = 0; try <= 3; try++) {
4367 		list = l2arc_list_locked(try, &list_lock);
4368 		passed_sz = 0;
4369 
4370 		/*
4371 		 * L2ARC fast warmup.
4372 		 *
4373 		 * Until the ARC is warm and starts to evict, read from the
4374 		 * head of the ARC lists rather than the tail.
4375 		 */
4376 		headroom = target_sz * l2arc_headroom;
4377 		if (arc_warm == B_FALSE)
4378 			ab = list_head(list);
4379 		else
4380 			ab = list_tail(list);
4381 
4382 		for (; ab; ab = ab_prev) {
4383 			if (arc_warm == B_FALSE)
4384 				ab_prev = list_next(list, ab);
4385 			else
4386 				ab_prev = list_prev(list, ab);
4387 
4388 			hash_lock = HDR_LOCK(ab);
4389 			have_lock = MUTEX_HELD(hash_lock);
4390 			if (!have_lock && !mutex_tryenter(hash_lock)) {
4391 				/*
4392 				 * Skip this buffer rather than waiting.
4393 				 */
4394 				continue;
4395 			}
4396 
4397 			passed_sz += ab->b_size;
4398 			if (passed_sz > headroom) {
4399 				/*
4400 				 * Searched too far.
4401 				 */
4402 				mutex_exit(hash_lock);
4403 				break;
4404 			}
4405 
4406 			if (!l2arc_write_eligible(guid, ab)) {
4407 				mutex_exit(hash_lock);
4408 				continue;
4409 			}
4410 
4411 			if ((write_sz + ab->b_size) > target_sz) {
4412 				full = B_TRUE;
4413 				mutex_exit(hash_lock);
4414 				break;
4415 			}
4416 
4417 			if (pio == NULL) {
4418 				/*
4419 				 * Insert a dummy header on the buflist so
4420 				 * l2arc_write_done() can find where the
4421 				 * write buffers begin without searching.
4422 				 */
4423 				list_insert_head(dev->l2ad_buflist, head);
4424 
4425 				cb = kmem_alloc(
4426 				    sizeof (l2arc_write_callback_t), KM_SLEEP);
4427 				cb->l2wcb_dev = dev;
4428 				cb->l2wcb_head = head;
4429 				pio = zio_root(spa, l2arc_write_done, cb,
4430 				    ZIO_FLAG_CANFAIL);
4431 			}
4432 
4433 			/*
4434 			 * Create and add a new L2ARC header.
4435 			 */
4436 			hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
4437 			hdrl2->b_dev = dev;
4438 			hdrl2->b_daddr = dev->l2ad_hand;
4439 
4440 			ab->b_flags |= ARC_L2_WRITING;
4441 			ab->b_l2hdr = hdrl2;
4442 			list_insert_head(dev->l2ad_buflist, ab);
4443 			buf_data = ab->b_buf->b_data;
4444 			buf_sz = ab->b_size;
4445 
4446 			/*
4447 			 * Compute and store the buffer cksum before
4448 			 * writing.  On debug the cksum is verified first.
4449 			 */
4450 			arc_cksum_verify(ab->b_buf);
4451 			arc_cksum_compute(ab->b_buf, B_TRUE);
4452 
4453 			mutex_exit(hash_lock);
4454 
4455 			wzio = zio_write_phys(pio, dev->l2ad_vdev,
4456 			    dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
4457 			    NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
4458 			    ZIO_FLAG_CANFAIL, B_FALSE);
4459 
4460 			DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
4461 			    zio_t *, wzio);
4462 			(void) zio_nowait(wzio);
4463 
4464 			/*
4465 			 * Keep the clock hand suitably device-aligned.
4466 			 */
4467 			buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
4468 
4469 			write_sz += buf_sz;
4470 			dev->l2ad_hand += buf_sz;
4471 		}
4472 
4473 		mutex_exit(list_lock);
4474 
4475 		if (full == B_TRUE)
4476 			break;
4477 	}
4478 	mutex_exit(&l2arc_buflist_mtx);
4479 
4480 	if (pio == NULL) {
4481 		ASSERT0(write_sz);
4482 		kmem_cache_free(hdr_cache, head);
4483 		return (0);
4484 	}
4485 
4486 	ASSERT3U(write_sz, <=, target_sz);
4487 	ARCSTAT_BUMP(arcstat_l2_writes_sent);
4488 	ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz);
4489 	ARCSTAT_INCR(arcstat_l2_size, write_sz);
4490 	vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0);
4491 
4492 	/*
4493 	 * Bump device hand to the device start if it is approaching the end.
4494 	 * l2arc_evict() will already have evicted ahead for this case.
4495 	 */
4496 	if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
4497 		vdev_space_update(dev->l2ad_vdev,
4498 		    dev->l2ad_end - dev->l2ad_hand, 0, 0);
4499 		dev->l2ad_hand = dev->l2ad_start;
4500 		dev->l2ad_evict = dev->l2ad_start;
4501 		dev->l2ad_first = B_FALSE;
4502 	}
4503 
4504 	dev->l2ad_writing = B_TRUE;
4505 	(void) zio_wait(pio);
4506 	dev->l2ad_writing = B_FALSE;
4507 
4508 	return (write_sz);
4509 }
4510 
4511 /*
4512  * This thread feeds the L2ARC at regular intervals.  This is the beating
4513  * heart of the L2ARC.
4514  */
4515 static void
4516 l2arc_feed_thread(void)
4517 {
4518 	callb_cpr_t cpr;
4519 	l2arc_dev_t *dev;
4520 	spa_t *spa;
4521 	uint64_t size, wrote;
4522 	clock_t begin, next = ddi_get_lbolt();
4523 
4524 	CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
4525 
4526 	mutex_enter(&l2arc_feed_thr_lock);
4527 
4528 	while (l2arc_thread_exit == 0) {
4529 		CALLB_CPR_SAFE_BEGIN(&cpr);
4530 		(void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock,
4531 		    next);
4532 		CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
4533 		next = ddi_get_lbolt() + hz;
4534 
4535 		/*
4536 		 * Quick check for L2ARC devices.
4537 		 */
4538 		mutex_enter(&l2arc_dev_mtx);
4539 		if (l2arc_ndev == 0) {
4540 			mutex_exit(&l2arc_dev_mtx);
4541 			continue;
4542 		}
4543 		mutex_exit(&l2arc_dev_mtx);
4544 		begin = ddi_get_lbolt();
4545 
4546 		/*
4547 		 * This selects the next l2arc device to write to, and in
4548 		 * doing so the next spa to feed from: dev->l2ad_spa.   This
4549 		 * will return NULL if there are now no l2arc devices or if
4550 		 * they are all faulted.
4551 		 *
4552 		 * If a device is returned, its spa's config lock is also
4553 		 * held to prevent device removal.  l2arc_dev_get_next()
4554 		 * will grab and release l2arc_dev_mtx.
4555 		 */
4556 		if ((dev = l2arc_dev_get_next()) == NULL)
4557 			continue;
4558 
4559 		spa = dev->l2ad_spa;
4560 		ASSERT(spa != NULL);
4561 
4562 		/*
4563 		 * If the pool is read-only then force the feed thread to
4564 		 * sleep a little longer.
4565 		 */
4566 		if (!spa_writeable(spa)) {
4567 			next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
4568 			spa_config_exit(spa, SCL_L2ARC, dev);
4569 			continue;
4570 		}
4571 
4572 		/*
4573 		 * Avoid contributing to memory pressure.
4574 		 */
4575 		if (arc_reclaim_needed()) {
4576 			ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
4577 			spa_config_exit(spa, SCL_L2ARC, dev);
4578 			continue;
4579 		}
4580 
4581 		ARCSTAT_BUMP(arcstat_l2_feeds);
4582 
4583 		size = l2arc_write_size(dev);
4584 
4585 		/*
4586 		 * Evict L2ARC buffers that will be overwritten.
4587 		 */
4588 		l2arc_evict(dev, size, B_FALSE);
4589 
4590 		/*
4591 		 * Write ARC buffers.
4592 		 */
4593 		wrote = l2arc_write_buffers(spa, dev, size);
4594 
4595 		/*
4596 		 * Calculate interval between writes.
4597 		 */
4598 		next = l2arc_write_interval(begin, size, wrote);
4599 		spa_config_exit(spa, SCL_L2ARC, dev);
4600 	}
4601 
4602 	l2arc_thread_exit = 0;
4603 	cv_broadcast(&l2arc_feed_thr_cv);
4604 	CALLB_CPR_EXIT(&cpr);		/* drops l2arc_feed_thr_lock */
4605 	thread_exit();
4606 }
4607 
4608 boolean_t
4609 l2arc_vdev_present(vdev_t *vd)
4610 {
4611 	l2arc_dev_t *dev;
4612 
4613 	mutex_enter(&l2arc_dev_mtx);
4614 	for (dev = list_head(l2arc_dev_list); dev != NULL;
4615 	    dev = list_next(l2arc_dev_list, dev)) {
4616 		if (dev->l2ad_vdev == vd)
4617 			break;
4618 	}
4619 	mutex_exit(&l2arc_dev_mtx);
4620 
4621 	return (dev != NULL);
4622 }
4623 
4624 /*
4625  * Add a vdev for use by the L2ARC.  By this point the spa has already
4626  * validated the vdev and opened it.
4627  */
4628 void
4629 l2arc_add_vdev(spa_t *spa, vdev_t *vd)
4630 {
4631 	l2arc_dev_t *adddev;
4632 
4633 	ASSERT(!l2arc_vdev_present(vd));
4634 
4635 	/*
4636 	 * Create a new l2arc device entry.
4637 	 */
4638 	adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
4639 	adddev->l2ad_spa = spa;
4640 	adddev->l2ad_vdev = vd;
4641 	adddev->l2ad_write = l2arc_write_max;
4642 	adddev->l2ad_boost = l2arc_write_boost;
4643 	adddev->l2ad_start = VDEV_LABEL_START_SIZE;
4644 	adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
4645 	adddev->l2ad_hand = adddev->l2ad_start;
4646 	adddev->l2ad_evict = adddev->l2ad_start;
4647 	adddev->l2ad_first = B_TRUE;
4648 	adddev->l2ad_writing = B_FALSE;
4649 	ASSERT3U(adddev->l2ad_write, >, 0);
4650 
4651 	/*
4652 	 * This is a list of all ARC buffers that are still valid on the
4653 	 * device.
4654 	 */
4655 	adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
4656 	list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
4657 	    offsetof(arc_buf_hdr_t, b_l2node));
4658 
4659 	vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
4660 
4661 	/*
4662 	 * Add device to global list
4663 	 */
4664 	mutex_enter(&l2arc_dev_mtx);
4665 	list_insert_head(l2arc_dev_list, adddev);
4666 	atomic_inc_64(&l2arc_ndev);
4667 	mutex_exit(&l2arc_dev_mtx);
4668 }
4669 
4670 /*
4671  * Remove a vdev from the L2ARC.
4672  */
4673 void
4674 l2arc_remove_vdev(vdev_t *vd)
4675 {
4676 	l2arc_dev_t *dev, *nextdev, *remdev = NULL;
4677 
4678 	/*
4679 	 * Find the device by vdev
4680 	 */
4681 	mutex_enter(&l2arc_dev_mtx);
4682 	for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
4683 		nextdev = list_next(l2arc_dev_list, dev);
4684 		if (vd == dev->l2ad_vdev) {
4685 			remdev = dev;
4686 			break;
4687 		}
4688 	}
4689 	ASSERT(remdev != NULL);
4690 
4691 	/*
4692 	 * Remove device from global list
4693 	 */
4694 	list_remove(l2arc_dev_list, remdev);
4695 	l2arc_dev_last = NULL;		/* may have been invalidated */
4696 	atomic_dec_64(&l2arc_ndev);
4697 	mutex_exit(&l2arc_dev_mtx);
4698 
4699 	/*
4700 	 * Clear all buflists and ARC references.  L2ARC device flush.
4701 	 */
4702 	l2arc_evict(remdev, 0, B_TRUE);
4703 	list_destroy(remdev->l2ad_buflist);
4704 	kmem_free(remdev->l2ad_buflist, sizeof (list_t));
4705 	kmem_free(remdev, sizeof (l2arc_dev_t));
4706 }
4707 
4708 void
4709 l2arc_init(void)
4710 {
4711 	l2arc_thread_exit = 0;
4712 	l2arc_ndev = 0;
4713 	l2arc_writes_sent = 0;
4714 	l2arc_writes_done = 0;
4715 
4716 	mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
4717 	cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
4718 	mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
4719 	mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
4720 	mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
4721 
4722 	l2arc_dev_list = &L2ARC_dev_list;
4723 	l2arc_free_on_write = &L2ARC_free_on_write;
4724 	list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
4725 	    offsetof(l2arc_dev_t, l2ad_node));
4726 	list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
4727 	    offsetof(l2arc_data_free_t, l2df_list_node));
4728 }
4729 
4730 void
4731 l2arc_fini(void)
4732 {
4733 	/*
4734 	 * This is called from dmu_fini(), which is called from spa_fini();
4735 	 * Because of this, we can assume that all l2arc devices have
4736 	 * already been removed when the pools themselves were removed.
4737 	 */
4738 
4739 	l2arc_do_free_on_write();
4740 
4741 	mutex_destroy(&l2arc_feed_thr_lock);
4742 	cv_destroy(&l2arc_feed_thr_cv);
4743 	mutex_destroy(&l2arc_dev_mtx);
4744 	mutex_destroy(&l2arc_buflist_mtx);
4745 	mutex_destroy(&l2arc_free_on_write_mtx);
4746 
4747 	list_destroy(l2arc_dev_list);
4748 	list_destroy(l2arc_free_on_write);
4749 }
4750 
4751 void
4752 l2arc_start(void)
4753 {
4754 	if (!(spa_mode_global & FWRITE))
4755 		return;
4756 
4757 	(void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
4758 	    TS_RUN, minclsyspri);
4759 }
4760 
4761 void
4762 l2arc_stop(void)
4763 {
4764 	if (!(spa_mode_global & FWRITE))
4765 		return;
4766 
4767 	mutex_enter(&l2arc_feed_thr_lock);
4768 	cv_signal(&l2arc_feed_thr_cv);	/* kick thread out of startup */
4769 	l2arc_thread_exit = 1;
4770 	while (l2arc_thread_exit != 0)
4771 		cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
4772 	mutex_exit(&l2arc_feed_thr_lock);
4773 }
4774