xref: /onnv-gate/usr/src/uts/common/fs/zfs/arc.c (revision 6018)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51484Sek110237  * Common Development and Distribution License (the "License").
61484Sek110237  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*6018Sbrendan  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens /*
293403Sbmc  * DVA-based Adjustable Replacement Cache
30789Sahrens  *
311544Seschrock  * While much of the theory of operation used here is
321544Seschrock  * based on the self-tuning, low overhead replacement cache
33789Sahrens  * presented by Megiddo and Modha at FAST 2003, there are some
34789Sahrens  * significant differences:
35789Sahrens  *
36789Sahrens  * 1. The Megiddo and Modha model assumes any page is evictable.
37789Sahrens  * Pages in its cache cannot be "locked" into memory.  This makes
38789Sahrens  * the eviction algorithm simple: evict the last page in the list.
39789Sahrens  * This also make the performance characteristics easy to reason
40789Sahrens  * about.  Our cache is not so simple.  At any given moment, some
41789Sahrens  * subset of the blocks in the cache are un-evictable because we
42789Sahrens  * have handed out a reference to them.  Blocks are only evictable
43789Sahrens  * when there are no external references active.  This makes
44789Sahrens  * eviction far more problematic:  we choose to evict the evictable
45789Sahrens  * blocks that are the "lowest" in the list.
46789Sahrens  *
47789Sahrens  * There are times when it is not possible to evict the requested
48789Sahrens  * space.  In these circumstances we are unable to adjust the cache
49789Sahrens  * size.  To prevent the cache growing unbounded at these times we
505450Sbrendan  * implement a "cache throttle" that slows the flow of new data
515450Sbrendan  * into the cache until we can make space available.
52789Sahrens  *
53789Sahrens  * 2. The Megiddo and Modha model assumes a fixed cache size.
54789Sahrens  * Pages are evicted when the cache is full and there is a cache
55789Sahrens  * miss.  Our model has a variable sized cache.  It grows with
565450Sbrendan  * high use, but also tries to react to memory pressure from the
57789Sahrens  * operating system: decreasing its size when system memory is
58789Sahrens  * tight.
59789Sahrens  *
60789Sahrens  * 3. The Megiddo and Modha model assumes a fixed page size. All
61789Sahrens  * elements of the cache are therefor exactly the same size.  So
62789Sahrens  * when adjusting the cache size following a cache miss, its simply
63789Sahrens  * a matter of choosing a single page to evict.  In our model, we
64789Sahrens  * have variable sized cache blocks (rangeing from 512 bytes to
65789Sahrens  * 128K bytes).  We therefor choose a set of blocks to evict to make
66789Sahrens  * space for a cache miss that approximates as closely as possible
67789Sahrens  * the space used by the new block.
68789Sahrens  *
69789Sahrens  * See also:  "ARC: A Self-Tuning, Low Overhead Replacement Cache"
70789Sahrens  * by N. Megiddo & D. Modha, FAST 2003
71789Sahrens  */
72789Sahrens 
73789Sahrens /*
74789Sahrens  * The locking model:
75789Sahrens  *
76789Sahrens  * A new reference to a cache buffer can be obtained in two
77789Sahrens  * ways: 1) via a hash table lookup using the DVA as a key,
785450Sbrendan  * or 2) via one of the ARC lists.  The arc_read() interface
79789Sahrens  * uses method 1, while the internal arc algorithms for
80789Sahrens  * adjusting the cache use method 2.  We therefor provide two
81789Sahrens  * types of locks: 1) the hash table lock array, and 2) the
82789Sahrens  * arc list locks.
83789Sahrens  *
84789Sahrens  * Buffers do not have their own mutexs, rather they rely on the
85789Sahrens  * hash table mutexs for the bulk of their protection (i.e. most
86789Sahrens  * fields in the arc_buf_hdr_t are protected by these mutexs).
87789Sahrens  *
88789Sahrens  * buf_hash_find() returns the appropriate mutex (held) when it
89789Sahrens  * locates the requested buffer in the hash table.  It returns
90789Sahrens  * NULL for the mutex if the buffer was not in the table.
91789Sahrens  *
92789Sahrens  * buf_hash_remove() expects the appropriate hash mutex to be
93789Sahrens  * already held before it is invoked.
94789Sahrens  *
95789Sahrens  * Each arc state also has a mutex which is used to protect the
96789Sahrens  * buffer list associated with the state.  When attempting to
97789Sahrens  * obtain a hash table lock while holding an arc list lock you
98789Sahrens  * must use: mutex_tryenter() to avoid deadlock.  Also note that
992688Smaybee  * the active state mutex must be held before the ghost state mutex.
100789Sahrens  *
1011544Seschrock  * Arc buffers may have an associated eviction callback function.
1021544Seschrock  * This function will be invoked prior to removing the buffer (e.g.
1031544Seschrock  * in arc_do_user_evicts()).  Note however that the data associated
1041544Seschrock  * with the buffer may be evicted prior to the callback.  The callback
1051544Seschrock  * must be made with *no locks held* (to prevent deadlock).  Additionally,
1061544Seschrock  * the users of callbacks must ensure that their private data is
1071544Seschrock  * protected from simultaneous callbacks from arc_buf_evict()
1081544Seschrock  * and arc_do_user_evicts().
1091544Seschrock  *
110789Sahrens  * Note that the majority of the performance stats are manipulated
111789Sahrens  * with atomic operations.
1125450Sbrendan  *
1135450Sbrendan  * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
1145450Sbrendan  *
1155450Sbrendan  *	- L2ARC buflist creation
1165450Sbrendan  *	- L2ARC buflist eviction
1175450Sbrendan  *	- L2ARC write completion, which walks L2ARC buflists
1185450Sbrendan  *	- ARC header destruction, as it removes from L2ARC buflists
1195450Sbrendan  *	- ARC header release, as it removes from L2ARC buflists
120789Sahrens  */
121789Sahrens 
122789Sahrens #include <sys/spa.h>
123789Sahrens #include <sys/zio.h>
1243093Sahrens #include <sys/zio_checksum.h>
125789Sahrens #include <sys/zfs_context.h>
126789Sahrens #include <sys/arc.h>
127789Sahrens #include <sys/refcount.h>
128789Sahrens #ifdef _KERNEL
129789Sahrens #include <sys/vmsystm.h>
130789Sahrens #include <vm/anon.h>
131789Sahrens #include <sys/fs/swapnode.h>
1321484Sek110237 #include <sys/dnlc.h>
133789Sahrens #endif
134789Sahrens #include <sys/callb.h>
1353403Sbmc #include <sys/kstat.h>
136789Sahrens 
137789Sahrens static kmutex_t		arc_reclaim_thr_lock;
138789Sahrens static kcondvar_t	arc_reclaim_thr_cv;	/* used to signal reclaim thr */
139789Sahrens static uint8_t		arc_thread_exit;
140789Sahrens 
1411484Sek110237 #define	ARC_REDUCE_DNLC_PERCENT	3
1421484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
1431484Sek110237 
144789Sahrens typedef enum arc_reclaim_strategy {
145789Sahrens 	ARC_RECLAIM_AGGR,		/* Aggressive reclaim strategy */
146789Sahrens 	ARC_RECLAIM_CONS		/* Conservative reclaim strategy */
147789Sahrens } arc_reclaim_strategy_t;
148789Sahrens 
149789Sahrens /* number of seconds before growing cache again */
150789Sahrens static int		arc_grow_retry = 60;
151789Sahrens 
1522391Smaybee /*
1532638Sperrin  * minimum lifespan of a prefetch block in clock ticks
1542638Sperrin  * (initialized in arc_init())
1552391Smaybee  */
1562638Sperrin static int		arc_min_prefetch_lifespan;
1572391Smaybee 
158789Sahrens static int arc_dead;
159789Sahrens 
160789Sahrens /*
1612885Sahrens  * These tunables are for performance analysis.
1622885Sahrens  */
1632885Sahrens uint64_t zfs_arc_max;
1642885Sahrens uint64_t zfs_arc_min;
1654645Sek110237 uint64_t zfs_arc_meta_limit = 0;
1662885Sahrens 
1672885Sahrens /*
1685450Sbrendan  * Note that buffers can be in one of 6 states:
169789Sahrens  *	ARC_anon	- anonymous (discussed below)
1701544Seschrock  *	ARC_mru		- recently used, currently cached
1711544Seschrock  *	ARC_mru_ghost	- recentely used, no longer in cache
1721544Seschrock  *	ARC_mfu		- frequently used, currently cached
1731544Seschrock  *	ARC_mfu_ghost	- frequently used, no longer in cache
1745450Sbrendan  *	ARC_l2c_only	- exists in L2ARC but not other states
1754309Smaybee  * When there are no active references to the buffer, they are
1764309Smaybee  * are linked onto a list in one of these arc states.  These are
1774309Smaybee  * the only buffers that can be evicted or deleted.  Within each
1784309Smaybee  * state there are multiple lists, one for meta-data and one for
1794309Smaybee  * non-meta-data.  Meta-data (indirect blocks, blocks of dnodes,
1804309Smaybee  * etc.) is tracked separately so that it can be managed more
1815450Sbrendan  * explicitly: favored over data, limited explicitly.
182789Sahrens  *
183789Sahrens  * Anonymous buffers are buffers that are not associated with
184789Sahrens  * a DVA.  These are buffers that hold dirty block copies
185789Sahrens  * before they are written to stable storage.  By definition,
1861544Seschrock  * they are "ref'd" and are considered part of arc_mru
187789Sahrens  * that cannot be freed.  Generally, they will aquire a DVA
1881544Seschrock  * as they are written and migrate onto the arc_mru list.
1895450Sbrendan  *
1905450Sbrendan  * The ARC_l2c_only state is for buffers that are in the second
1915450Sbrendan  * level ARC but no longer in any of the ARC_m* lists.  The second
1925450Sbrendan  * level ARC itself may also contain buffers that are in any of
1935450Sbrendan  * the ARC_m* states - meaning that a buffer can exist in two
1945450Sbrendan  * places.  The reason for the ARC_l2c_only state is to keep the
1955450Sbrendan  * buffer header in the hash table, so that reads that hit the
1965450Sbrendan  * second level ARC benefit from these fast lookups.
197789Sahrens  */
198789Sahrens 
199789Sahrens typedef struct arc_state {
2004309Smaybee 	list_t	arcs_list[ARC_BUFC_NUMTYPES];	/* list of evictable buffers */
2014309Smaybee 	uint64_t arcs_lsize[ARC_BUFC_NUMTYPES];	/* amount of evictable data */
2024309Smaybee 	uint64_t arcs_size;	/* total amount of data in this state */
2033403Sbmc 	kmutex_t arcs_mtx;
204789Sahrens } arc_state_t;
205789Sahrens 
2065450Sbrendan /* The 6 states: */
207789Sahrens static arc_state_t ARC_anon;
2081544Seschrock static arc_state_t ARC_mru;
2091544Seschrock static arc_state_t ARC_mru_ghost;
2101544Seschrock static arc_state_t ARC_mfu;
2111544Seschrock static arc_state_t ARC_mfu_ghost;
2125450Sbrendan static arc_state_t ARC_l2c_only;
213789Sahrens 
2143403Sbmc typedef struct arc_stats {
2153403Sbmc 	kstat_named_t arcstat_hits;
2163403Sbmc 	kstat_named_t arcstat_misses;
2173403Sbmc 	kstat_named_t arcstat_demand_data_hits;
2183403Sbmc 	kstat_named_t arcstat_demand_data_misses;
2193403Sbmc 	kstat_named_t arcstat_demand_metadata_hits;
2203403Sbmc 	kstat_named_t arcstat_demand_metadata_misses;
2213403Sbmc 	kstat_named_t arcstat_prefetch_data_hits;
2223403Sbmc 	kstat_named_t arcstat_prefetch_data_misses;
2233403Sbmc 	kstat_named_t arcstat_prefetch_metadata_hits;
2243403Sbmc 	kstat_named_t arcstat_prefetch_metadata_misses;
2253403Sbmc 	kstat_named_t arcstat_mru_hits;
2263403Sbmc 	kstat_named_t arcstat_mru_ghost_hits;
2273403Sbmc 	kstat_named_t arcstat_mfu_hits;
2283403Sbmc 	kstat_named_t arcstat_mfu_ghost_hits;
2293403Sbmc 	kstat_named_t arcstat_deleted;
2303403Sbmc 	kstat_named_t arcstat_recycle_miss;
2313403Sbmc 	kstat_named_t arcstat_mutex_miss;
2323403Sbmc 	kstat_named_t arcstat_evict_skip;
2333403Sbmc 	kstat_named_t arcstat_hash_elements;
2343403Sbmc 	kstat_named_t arcstat_hash_elements_max;
2353403Sbmc 	kstat_named_t arcstat_hash_collisions;
2363403Sbmc 	kstat_named_t arcstat_hash_chains;
2373403Sbmc 	kstat_named_t arcstat_hash_chain_max;
2383403Sbmc 	kstat_named_t arcstat_p;
2393403Sbmc 	kstat_named_t arcstat_c;
2403403Sbmc 	kstat_named_t arcstat_c_min;
2413403Sbmc 	kstat_named_t arcstat_c_max;
2423403Sbmc 	kstat_named_t arcstat_size;
2435450Sbrendan 	kstat_named_t arcstat_hdr_size;
2445450Sbrendan 	kstat_named_t arcstat_l2_hits;
2455450Sbrendan 	kstat_named_t arcstat_l2_misses;
2465450Sbrendan 	kstat_named_t arcstat_l2_feeds;
2475450Sbrendan 	kstat_named_t arcstat_l2_rw_clash;
2485450Sbrendan 	kstat_named_t arcstat_l2_writes_sent;
2495450Sbrendan 	kstat_named_t arcstat_l2_writes_done;
2505450Sbrendan 	kstat_named_t arcstat_l2_writes_error;
2515450Sbrendan 	kstat_named_t arcstat_l2_writes_hdr_miss;
2525450Sbrendan 	kstat_named_t arcstat_l2_evict_lock_retry;
2535450Sbrendan 	kstat_named_t arcstat_l2_evict_reading;
2545450Sbrendan 	kstat_named_t arcstat_l2_free_on_write;
2555450Sbrendan 	kstat_named_t arcstat_l2_abort_lowmem;
2565450Sbrendan 	kstat_named_t arcstat_l2_cksum_bad;
2575450Sbrendan 	kstat_named_t arcstat_l2_io_error;
2585450Sbrendan 	kstat_named_t arcstat_l2_size;
2595450Sbrendan 	kstat_named_t arcstat_l2_hdr_size;
2603403Sbmc } arc_stats_t;
2613403Sbmc 
2623403Sbmc static arc_stats_t arc_stats = {
2633403Sbmc 	{ "hits",			KSTAT_DATA_UINT64 },
2643403Sbmc 	{ "misses",			KSTAT_DATA_UINT64 },
2653403Sbmc 	{ "demand_data_hits",		KSTAT_DATA_UINT64 },
2663403Sbmc 	{ "demand_data_misses",		KSTAT_DATA_UINT64 },
2673403Sbmc 	{ "demand_metadata_hits",	KSTAT_DATA_UINT64 },
2683403Sbmc 	{ "demand_metadata_misses",	KSTAT_DATA_UINT64 },
2693403Sbmc 	{ "prefetch_data_hits",		KSTAT_DATA_UINT64 },
2703403Sbmc 	{ "prefetch_data_misses",	KSTAT_DATA_UINT64 },
2713403Sbmc 	{ "prefetch_metadata_hits",	KSTAT_DATA_UINT64 },
2723403Sbmc 	{ "prefetch_metadata_misses",	KSTAT_DATA_UINT64 },
2733403Sbmc 	{ "mru_hits",			KSTAT_DATA_UINT64 },
2743403Sbmc 	{ "mru_ghost_hits",		KSTAT_DATA_UINT64 },
2753403Sbmc 	{ "mfu_hits",			KSTAT_DATA_UINT64 },
2763403Sbmc 	{ "mfu_ghost_hits",		KSTAT_DATA_UINT64 },
2773403Sbmc 	{ "deleted",			KSTAT_DATA_UINT64 },
2783403Sbmc 	{ "recycle_miss",		KSTAT_DATA_UINT64 },
2793403Sbmc 	{ "mutex_miss",			KSTAT_DATA_UINT64 },
2803403Sbmc 	{ "evict_skip",			KSTAT_DATA_UINT64 },
2813403Sbmc 	{ "hash_elements",		KSTAT_DATA_UINT64 },
2823403Sbmc 	{ "hash_elements_max",		KSTAT_DATA_UINT64 },
2833403Sbmc 	{ "hash_collisions",		KSTAT_DATA_UINT64 },
2843403Sbmc 	{ "hash_chains",		KSTAT_DATA_UINT64 },
2853403Sbmc 	{ "hash_chain_max",		KSTAT_DATA_UINT64 },
2863403Sbmc 	{ "p",				KSTAT_DATA_UINT64 },
2873403Sbmc 	{ "c",				KSTAT_DATA_UINT64 },
2883403Sbmc 	{ "c_min",			KSTAT_DATA_UINT64 },
2893403Sbmc 	{ "c_max",			KSTAT_DATA_UINT64 },
2905450Sbrendan 	{ "size",			KSTAT_DATA_UINT64 },
2915450Sbrendan 	{ "hdr_size",			KSTAT_DATA_UINT64 },
2925450Sbrendan 	{ "l2_hits",			KSTAT_DATA_UINT64 },
2935450Sbrendan 	{ "l2_misses",			KSTAT_DATA_UINT64 },
2945450Sbrendan 	{ "l2_feeds",			KSTAT_DATA_UINT64 },
2955450Sbrendan 	{ "l2_rw_clash",		KSTAT_DATA_UINT64 },
2965450Sbrendan 	{ "l2_writes_sent",		KSTAT_DATA_UINT64 },
2975450Sbrendan 	{ "l2_writes_done",		KSTAT_DATA_UINT64 },
2985450Sbrendan 	{ "l2_writes_error",		KSTAT_DATA_UINT64 },
2995450Sbrendan 	{ "l2_writes_hdr_miss",		KSTAT_DATA_UINT64 },
3005450Sbrendan 	{ "l2_evict_lock_retry",	KSTAT_DATA_UINT64 },
3015450Sbrendan 	{ "l2_evict_reading",		KSTAT_DATA_UINT64 },
3025450Sbrendan 	{ "l2_free_on_write",		KSTAT_DATA_UINT64 },
3035450Sbrendan 	{ "l2_abort_lowmem",		KSTAT_DATA_UINT64 },
3045450Sbrendan 	{ "l2_cksum_bad",		KSTAT_DATA_UINT64 },
3055450Sbrendan 	{ "l2_io_error",		KSTAT_DATA_UINT64 },
3065450Sbrendan 	{ "l2_size",			KSTAT_DATA_UINT64 },
3075450Sbrendan 	{ "l2_hdr_size",		KSTAT_DATA_UINT64 }
3083403Sbmc };
309789Sahrens 
3103403Sbmc #define	ARCSTAT(stat)	(arc_stats.stat.value.ui64)
3113403Sbmc 
3123403Sbmc #define	ARCSTAT_INCR(stat, val) \
3133403Sbmc 	atomic_add_64(&arc_stats.stat.value.ui64, (val));
3143403Sbmc 
3153403Sbmc #define	ARCSTAT_BUMP(stat) 	ARCSTAT_INCR(stat, 1)
3163403Sbmc #define	ARCSTAT_BUMPDOWN(stat)	ARCSTAT_INCR(stat, -1)
3173403Sbmc 
3183403Sbmc #define	ARCSTAT_MAX(stat, val) {					\
3193403Sbmc 	uint64_t m;							\
3203403Sbmc 	while ((val) > (m = arc_stats.stat.value.ui64) &&		\
3213403Sbmc 	    (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val))))	\
3223403Sbmc 		continue;						\
3233403Sbmc }
3243403Sbmc 
3253403Sbmc #define	ARCSTAT_MAXSTAT(stat) \
3263403Sbmc 	ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
327789Sahrens 
3283403Sbmc /*
3293403Sbmc  * We define a macro to allow ARC hits/misses to be easily broken down by
3303403Sbmc  * two separate conditions, giving a total of four different subtypes for
3313403Sbmc  * each of hits and misses (so eight statistics total).
3323403Sbmc  */
3333403Sbmc #define	ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
3343403Sbmc 	if (cond1) {							\
3353403Sbmc 		if (cond2) {						\
3363403Sbmc 			ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
3373403Sbmc 		} else {						\
3383403Sbmc 			ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
3393403Sbmc 		}							\
3403403Sbmc 	} else {							\
3413403Sbmc 		if (cond2) {						\
3423403Sbmc 			ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
3433403Sbmc 		} else {						\
3443403Sbmc 			ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
3453403Sbmc 		}							\
3463403Sbmc 	}
347789Sahrens 
3483403Sbmc kstat_t			*arc_ksp;
3493403Sbmc static arc_state_t 	*arc_anon;
3503403Sbmc static arc_state_t	*arc_mru;
3513403Sbmc static arc_state_t	*arc_mru_ghost;
3523403Sbmc static arc_state_t	*arc_mfu;
3533403Sbmc static arc_state_t	*arc_mfu_ghost;
3545450Sbrendan static arc_state_t	*arc_l2c_only;
3553403Sbmc 
3563403Sbmc /*
3573403Sbmc  * There are several ARC variables that are critical to export as kstats --
3583403Sbmc  * but we don't want to have to grovel around in the kstat whenever we wish to
3593403Sbmc  * manipulate them.  For these variables, we therefore define them to be in
3603403Sbmc  * terms of the statistic variable.  This assures that we are not introducing
3613403Sbmc  * the possibility of inconsistency by having shadow copies of the variables,
3623403Sbmc  * while still allowing the code to be readable.
3633403Sbmc  */
3643403Sbmc #define	arc_size	ARCSTAT(arcstat_size)	/* actual total arc size */
3653403Sbmc #define	arc_p		ARCSTAT(arcstat_p)	/* target size of MRU */
3663403Sbmc #define	arc_c		ARCSTAT(arcstat_c)	/* target size of cache */
3673403Sbmc #define	arc_c_min	ARCSTAT(arcstat_c_min)	/* min target cache size */
3683403Sbmc #define	arc_c_max	ARCSTAT(arcstat_c_max)	/* max target cache size */
3693403Sbmc 
3703403Sbmc static int		arc_no_grow;	/* Don't try to grow cache size */
3713403Sbmc static uint64_t		arc_tempreserve;
3724309Smaybee static uint64_t		arc_meta_used;
3734309Smaybee static uint64_t		arc_meta_limit;
3744309Smaybee static uint64_t		arc_meta_max = 0;
375789Sahrens 
3765450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
3775450Sbrendan 
378789Sahrens typedef struct arc_callback arc_callback_t;
379789Sahrens 
380789Sahrens struct arc_callback {
3813547Smaybee 	void			*acb_private;
382789Sahrens 	arc_done_func_t		*acb_done;
383789Sahrens 	arc_byteswap_func_t	*acb_byteswap;
384789Sahrens 	arc_buf_t		*acb_buf;
385789Sahrens 	zio_t			*acb_zio_dummy;
386789Sahrens 	arc_callback_t		*acb_next;
387789Sahrens };
388789Sahrens 
3893547Smaybee typedef struct arc_write_callback arc_write_callback_t;
3903547Smaybee 
3913547Smaybee struct arc_write_callback {
3923547Smaybee 	void		*awcb_private;
3933547Smaybee 	arc_done_func_t	*awcb_ready;
3943547Smaybee 	arc_done_func_t	*awcb_done;
3953547Smaybee 	arc_buf_t	*awcb_buf;
3963547Smaybee };
3973547Smaybee 
398789Sahrens struct arc_buf_hdr {
399789Sahrens 	/* protected by hash lock */
400789Sahrens 	dva_t			b_dva;
401789Sahrens 	uint64_t		b_birth;
402789Sahrens 	uint64_t		b_cksum0;
403789Sahrens 
4043093Sahrens 	kmutex_t		b_freeze_lock;
4053093Sahrens 	zio_cksum_t		*b_freeze_cksum;
4063093Sahrens 
407789Sahrens 	arc_buf_hdr_t		*b_hash_next;
408789Sahrens 	arc_buf_t		*b_buf;
409789Sahrens 	uint32_t		b_flags;
4101544Seschrock 	uint32_t		b_datacnt;
411789Sahrens 
4123290Sjohansen 	arc_callback_t		*b_acb;
413789Sahrens 	kcondvar_t		b_cv;
4143290Sjohansen 
4153290Sjohansen 	/* immutable */
4163290Sjohansen 	arc_buf_contents_t	b_type;
4173290Sjohansen 	uint64_t		b_size;
4183290Sjohansen 	spa_t			*b_spa;
419789Sahrens 
420789Sahrens 	/* protected by arc state mutex */
421789Sahrens 	arc_state_t		*b_state;
422789Sahrens 	list_node_t		b_arc_node;
423789Sahrens 
424789Sahrens 	/* updated atomically */
425789Sahrens 	clock_t			b_arc_access;
426789Sahrens 
427789Sahrens 	/* self protecting */
428789Sahrens 	refcount_t		b_refcnt;
4295450Sbrendan 
4305450Sbrendan 	l2arc_buf_hdr_t		*b_l2hdr;
4315450Sbrendan 	list_node_t		b_l2node;
432789Sahrens };
433789Sahrens 
4341544Seschrock static arc_buf_t *arc_eviction_list;
4351544Seschrock static kmutex_t arc_eviction_mtx;
4362887Smaybee static arc_buf_hdr_t arc_eviction_hdr;
4372688Smaybee static void arc_get_data_buf(arc_buf_t *buf);
4382688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
4394309Smaybee static int arc_evict_needed(arc_buf_contents_t type);
4405642Smaybee static void arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes);
4411544Seschrock 
4421544Seschrock #define	GHOST_STATE(state)	\
4435450Sbrendan 	((state) == arc_mru_ghost || (state) == arc_mfu_ghost ||	\
4445450Sbrendan 	(state) == arc_l2c_only)
4451544Seschrock 
446789Sahrens /*
447789Sahrens  * Private ARC flags.  These flags are private ARC only flags that will show up
448789Sahrens  * in b_flags in the arc_hdr_buf_t.  Some flags are publicly declared, and can
449789Sahrens  * be passed in as arc_flags in things like arc_read.  However, these flags
450789Sahrens  * should never be passed and should only be set by ARC code.  When adding new
451789Sahrens  * public flags, make sure not to smash the private ones.
452789Sahrens  */
453789Sahrens 
4541544Seschrock #define	ARC_IN_HASH_TABLE	(1 << 9)	/* this buffer is hashed */
455789Sahrens #define	ARC_IO_IN_PROGRESS	(1 << 10)	/* I/O in progress for buf */
456789Sahrens #define	ARC_IO_ERROR		(1 << 11)	/* I/O failed for buf */
457789Sahrens #define	ARC_FREED_IN_READ	(1 << 12)	/* buf freed while in read */
4581544Seschrock #define	ARC_BUF_AVAILABLE	(1 << 13)	/* block not in active use */
4592391Smaybee #define	ARC_INDIRECT		(1 << 14)	/* this is an indirect block */
4605450Sbrendan #define	ARC_FREE_IN_PROGRESS	(1 << 15)	/* hdr about to be freed */
4615450Sbrendan #define	ARC_DONT_L2CACHE	(1 << 16)	/* originated by prefetch */
4625450Sbrendan #define	ARC_L2_READING		(1 << 17)	/* L2ARC read in progress */
4635450Sbrendan #define	ARC_L2_WRITING		(1 << 18)	/* L2ARC write in progress */
4645450Sbrendan #define	ARC_L2_EVICTED		(1 << 19)	/* evicted during I/O */
4655450Sbrendan #define	ARC_L2_WRITE_HEAD	(1 << 20)	/* head of write list */
466789Sahrens 
4671544Seschrock #define	HDR_IN_HASH_TABLE(hdr)	((hdr)->b_flags & ARC_IN_HASH_TABLE)
468789Sahrens #define	HDR_IO_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS)
469789Sahrens #define	HDR_IO_ERROR(hdr)	((hdr)->b_flags & ARC_IO_ERROR)
470789Sahrens #define	HDR_FREED_IN_READ(hdr)	((hdr)->b_flags & ARC_FREED_IN_READ)
4711544Seschrock #define	HDR_BUF_AVAILABLE(hdr)	((hdr)->b_flags & ARC_BUF_AVAILABLE)
4725450Sbrendan #define	HDR_FREE_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
4735450Sbrendan #define	HDR_DONT_L2CACHE(hdr)	((hdr)->b_flags & ARC_DONT_L2CACHE)
4745450Sbrendan #define	HDR_L2_READING(hdr)	((hdr)->b_flags & ARC_L2_READING)
4755450Sbrendan #define	HDR_L2_WRITING(hdr)	((hdr)->b_flags & ARC_L2_WRITING)
4765450Sbrendan #define	HDR_L2_EVICTED(hdr)	((hdr)->b_flags & ARC_L2_EVICTED)
4775450Sbrendan #define	HDR_L2_WRITE_HEAD(hdr)	((hdr)->b_flags & ARC_L2_WRITE_HEAD)
478789Sahrens 
479789Sahrens /*
480*6018Sbrendan  * Other sizes
481*6018Sbrendan  */
482*6018Sbrendan 
483*6018Sbrendan #define	HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
484*6018Sbrendan #define	L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
485*6018Sbrendan 
486*6018Sbrendan /*
487789Sahrens  * Hash table routines
488789Sahrens  */
489789Sahrens 
490789Sahrens #define	HT_LOCK_PAD	64
491789Sahrens 
492789Sahrens struct ht_lock {
493789Sahrens 	kmutex_t	ht_lock;
494789Sahrens #ifdef _KERNEL
495789Sahrens 	unsigned char	pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
496789Sahrens #endif
497789Sahrens };
498789Sahrens 
499789Sahrens #define	BUF_LOCKS 256
500789Sahrens typedef struct buf_hash_table {
501789Sahrens 	uint64_t ht_mask;
502789Sahrens 	arc_buf_hdr_t **ht_table;
503789Sahrens 	struct ht_lock ht_locks[BUF_LOCKS];
504789Sahrens } buf_hash_table_t;
505789Sahrens 
506789Sahrens static buf_hash_table_t buf_hash_table;
507789Sahrens 
508789Sahrens #define	BUF_HASH_INDEX(spa, dva, birth) \
509789Sahrens 	(buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
510789Sahrens #define	BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
511789Sahrens #define	BUF_HASH_LOCK(idx)	(&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
512789Sahrens #define	HDR_LOCK(buf) \
513789Sahrens 	(BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth)))
514789Sahrens 
515789Sahrens uint64_t zfs_crc64_table[256];
516789Sahrens 
5175450Sbrendan /*
5185450Sbrendan  * Level 2 ARC
5195450Sbrendan  */
5205450Sbrendan 
5215450Sbrendan #define	L2ARC_WRITE_SIZE	(8 * 1024 * 1024)	/* initial write max */
5225450Sbrendan #define	L2ARC_HEADROOM		4		/* num of writes */
5235450Sbrendan #define	L2ARC_FEED_DELAY	180		/* starting grace */
5245450Sbrendan #define	L2ARC_FEED_SECS		1		/* caching interval */
5255450Sbrendan 
5265450Sbrendan #define	l2arc_writes_sent	ARCSTAT(arcstat_l2_writes_sent)
5275450Sbrendan #define	l2arc_writes_done	ARCSTAT(arcstat_l2_writes_done)
5285450Sbrendan 
5295450Sbrendan /*
5305450Sbrendan  * L2ARC Performance Tunables
5315450Sbrendan  */
5325450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE;	/* default max write size */
5335450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM;	/* number of dev writes */
5345450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS;	/* interval seconds */
5355450Sbrendan boolean_t l2arc_noprefetch = B_TRUE;		/* don't cache prefetch bufs */
5365450Sbrendan 
5375450Sbrendan /*
5385450Sbrendan  * L2ARC Internals
5395450Sbrendan  */
5405450Sbrendan typedef struct l2arc_dev {
5415450Sbrendan 	vdev_t			*l2ad_vdev;	/* vdev */
5425450Sbrendan 	spa_t			*l2ad_spa;	/* spa */
5435450Sbrendan 	uint64_t		l2ad_hand;	/* next write location */
5445450Sbrendan 	uint64_t		l2ad_write;	/* desired write size, bytes */
5455450Sbrendan 	uint64_t		l2ad_start;	/* first addr on device */
5465450Sbrendan 	uint64_t		l2ad_end;	/* last addr on device */
5475450Sbrendan 	uint64_t		l2ad_evict;	/* last addr eviction reached */
5485450Sbrendan 	boolean_t		l2ad_first;	/* first sweep through */
5495450Sbrendan 	list_t			*l2ad_buflist;	/* buffer list */
5505450Sbrendan 	list_node_t		l2ad_node;	/* device list node */
5515450Sbrendan } l2arc_dev_t;
5525450Sbrendan 
5535450Sbrendan static list_t L2ARC_dev_list;			/* device list */
5545450Sbrendan static list_t *l2arc_dev_list;			/* device list pointer */
5555450Sbrendan static kmutex_t l2arc_dev_mtx;			/* device list mutex */
5565450Sbrendan static l2arc_dev_t *l2arc_dev_last;		/* last device used */
5575450Sbrendan static kmutex_t l2arc_buflist_mtx;		/* mutex for all buflists */
5585450Sbrendan static list_t L2ARC_free_on_write;		/* free after write buf list */
5595450Sbrendan static list_t *l2arc_free_on_write;		/* free after write list ptr */
5605450Sbrendan static kmutex_t l2arc_free_on_write_mtx;	/* mutex for list */
5615450Sbrendan static uint64_t l2arc_ndev;			/* number of devices */
5625450Sbrendan 
5635450Sbrendan typedef struct l2arc_read_callback {
5645450Sbrendan 	arc_buf_t	*l2rcb_buf;		/* read buffer */
5655450Sbrendan 	spa_t		*l2rcb_spa;		/* spa */
5665450Sbrendan 	blkptr_t	l2rcb_bp;		/* original blkptr */
5675450Sbrendan 	zbookmark_t	l2rcb_zb;		/* original bookmark */
5685450Sbrendan 	int		l2rcb_flags;		/* original flags */
5695450Sbrendan } l2arc_read_callback_t;
5705450Sbrendan 
5715450Sbrendan typedef struct l2arc_write_callback {
5725450Sbrendan 	l2arc_dev_t	*l2wcb_dev;		/* device info */
5735450Sbrendan 	arc_buf_hdr_t	*l2wcb_head;		/* head of write buflist */
5745450Sbrendan } l2arc_write_callback_t;
5755450Sbrendan 
5765450Sbrendan struct l2arc_buf_hdr {
5775450Sbrendan 	/* protected by arc_buf_hdr  mutex */
5785450Sbrendan 	l2arc_dev_t	*b_dev;			/* L2ARC device */
5795450Sbrendan 	daddr_t		b_daddr;		/* disk address, offset byte */
5805450Sbrendan };
5815450Sbrendan 
5825450Sbrendan typedef struct l2arc_data_free {
5835450Sbrendan 	/* protected by l2arc_free_on_write_mtx */
5845450Sbrendan 	void		*l2df_data;
5855450Sbrendan 	size_t		l2df_size;
5865450Sbrendan 	void		(*l2df_func)(void *, size_t);
5875450Sbrendan 	list_node_t	l2df_list_node;
5885450Sbrendan } l2arc_data_free_t;
5895450Sbrendan 
5905450Sbrendan static kmutex_t l2arc_feed_thr_lock;
5915450Sbrendan static kcondvar_t l2arc_feed_thr_cv;
5925450Sbrendan static uint8_t l2arc_thread_exit;
5935450Sbrendan 
5945450Sbrendan static void l2arc_read_done(zio_t *zio);
5955450Sbrendan static void l2arc_hdr_stat_add(void);
5965450Sbrendan static void l2arc_hdr_stat_remove(void);
5975450Sbrendan 
598789Sahrens static uint64_t
599789Sahrens buf_hash(spa_t *spa, dva_t *dva, uint64_t birth)
600789Sahrens {
601789Sahrens 	uintptr_t spav = (uintptr_t)spa;
602789Sahrens 	uint8_t *vdva = (uint8_t *)dva;
603789Sahrens 	uint64_t crc = -1ULL;
604789Sahrens 	int i;
605789Sahrens 
606789Sahrens 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
607789Sahrens 
608789Sahrens 	for (i = 0; i < sizeof (dva_t); i++)
609789Sahrens 		crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
610789Sahrens 
611789Sahrens 	crc ^= (spav>>8) ^ birth;
612789Sahrens 
613789Sahrens 	return (crc);
614789Sahrens }
615789Sahrens 
616789Sahrens #define	BUF_EMPTY(buf)						\
617789Sahrens 	((buf)->b_dva.dva_word[0] == 0 &&			\
618789Sahrens 	(buf)->b_dva.dva_word[1] == 0 &&			\
619789Sahrens 	(buf)->b_birth == 0)
620789Sahrens 
621789Sahrens #define	BUF_EQUAL(spa, dva, birth, buf)				\
622789Sahrens 	((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) &&	\
623789Sahrens 	((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) &&	\
624789Sahrens 	((buf)->b_birth == birth) && ((buf)->b_spa == spa)
625789Sahrens 
626789Sahrens static arc_buf_hdr_t *
627789Sahrens buf_hash_find(spa_t *spa, dva_t *dva, uint64_t birth, kmutex_t **lockp)
628789Sahrens {
629789Sahrens 	uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
630789Sahrens 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
631789Sahrens 	arc_buf_hdr_t *buf;
632789Sahrens 
633789Sahrens 	mutex_enter(hash_lock);
634789Sahrens 	for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
635789Sahrens 	    buf = buf->b_hash_next) {
636789Sahrens 		if (BUF_EQUAL(spa, dva, birth, buf)) {
637789Sahrens 			*lockp = hash_lock;
638789Sahrens 			return (buf);
639789Sahrens 		}
640789Sahrens 	}
641789Sahrens 	mutex_exit(hash_lock);
642789Sahrens 	*lockp = NULL;
643789Sahrens 	return (NULL);
644789Sahrens }
645789Sahrens 
646789Sahrens /*
647789Sahrens  * Insert an entry into the hash table.  If there is already an element
648789Sahrens  * equal to elem in the hash table, then the already existing element
649789Sahrens  * will be returned and the new element will not be inserted.
650789Sahrens  * Otherwise returns NULL.
651789Sahrens  */
652789Sahrens static arc_buf_hdr_t *
653789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
654789Sahrens {
655789Sahrens 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
656789Sahrens 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
657789Sahrens 	arc_buf_hdr_t *fbuf;
6583403Sbmc 	uint32_t i;
659789Sahrens 
6601544Seschrock 	ASSERT(!HDR_IN_HASH_TABLE(buf));
661789Sahrens 	*lockp = hash_lock;
662789Sahrens 	mutex_enter(hash_lock);
663789Sahrens 	for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
664789Sahrens 	    fbuf = fbuf->b_hash_next, i++) {
665789Sahrens 		if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
666789Sahrens 			return (fbuf);
667789Sahrens 	}
668789Sahrens 
669789Sahrens 	buf->b_hash_next = buf_hash_table.ht_table[idx];
670789Sahrens 	buf_hash_table.ht_table[idx] = buf;
6711544Seschrock 	buf->b_flags |= ARC_IN_HASH_TABLE;
672789Sahrens 
673789Sahrens 	/* collect some hash table performance data */
674789Sahrens 	if (i > 0) {
6753403Sbmc 		ARCSTAT_BUMP(arcstat_hash_collisions);
676789Sahrens 		if (i == 1)
6773403Sbmc 			ARCSTAT_BUMP(arcstat_hash_chains);
6783403Sbmc 
6793403Sbmc 		ARCSTAT_MAX(arcstat_hash_chain_max, i);
680789Sahrens 	}
6813403Sbmc 
6823403Sbmc 	ARCSTAT_BUMP(arcstat_hash_elements);
6833403Sbmc 	ARCSTAT_MAXSTAT(arcstat_hash_elements);
684789Sahrens 
685789Sahrens 	return (NULL);
686789Sahrens }
687789Sahrens 
688789Sahrens static void
689789Sahrens buf_hash_remove(arc_buf_hdr_t *buf)
690789Sahrens {
691789Sahrens 	arc_buf_hdr_t *fbuf, **bufp;
692789Sahrens 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
693789Sahrens 
694789Sahrens 	ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
6951544Seschrock 	ASSERT(HDR_IN_HASH_TABLE(buf));
696789Sahrens 
697789Sahrens 	bufp = &buf_hash_table.ht_table[idx];
698789Sahrens 	while ((fbuf = *bufp) != buf) {
699789Sahrens 		ASSERT(fbuf != NULL);
700789Sahrens 		bufp = &fbuf->b_hash_next;
701789Sahrens 	}
702789Sahrens 	*bufp = buf->b_hash_next;
703789Sahrens 	buf->b_hash_next = NULL;
7041544Seschrock 	buf->b_flags &= ~ARC_IN_HASH_TABLE;
705789Sahrens 
706789Sahrens 	/* collect some hash table performance data */
7073403Sbmc 	ARCSTAT_BUMPDOWN(arcstat_hash_elements);
7083403Sbmc 
709789Sahrens 	if (buf_hash_table.ht_table[idx] &&
710789Sahrens 	    buf_hash_table.ht_table[idx]->b_hash_next == NULL)
7113403Sbmc 		ARCSTAT_BUMPDOWN(arcstat_hash_chains);
712789Sahrens }
713789Sahrens 
714789Sahrens /*
715789Sahrens  * Global data structures and functions for the buf kmem cache.
716789Sahrens  */
717789Sahrens static kmem_cache_t *hdr_cache;
718789Sahrens static kmem_cache_t *buf_cache;
719789Sahrens 
720789Sahrens static void
721789Sahrens buf_fini(void)
722789Sahrens {
723789Sahrens 	int i;
724789Sahrens 
725789Sahrens 	kmem_free(buf_hash_table.ht_table,
726789Sahrens 	    (buf_hash_table.ht_mask + 1) * sizeof (void *));
727789Sahrens 	for (i = 0; i < BUF_LOCKS; i++)
728789Sahrens 		mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
729789Sahrens 	kmem_cache_destroy(hdr_cache);
730789Sahrens 	kmem_cache_destroy(buf_cache);
731789Sahrens }
732789Sahrens 
733789Sahrens /*
734789Sahrens  * Constructor callback - called when the cache is empty
735789Sahrens  * and a new buf is requested.
736789Sahrens  */
737789Sahrens /* ARGSUSED */
738789Sahrens static int
739789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag)
740789Sahrens {
741789Sahrens 	arc_buf_hdr_t *buf = vbuf;
742789Sahrens 
743789Sahrens 	bzero(buf, sizeof (arc_buf_hdr_t));
744789Sahrens 	refcount_create(&buf->b_refcnt);
745789Sahrens 	cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
7464831Sgw25295 	mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
7475450Sbrendan 
748*6018Sbrendan 	ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
749789Sahrens 	return (0);
750789Sahrens }
751789Sahrens 
752789Sahrens /*
753789Sahrens  * Destructor callback - called when a cached buf is
754789Sahrens  * no longer required.
755789Sahrens  */
756789Sahrens /* ARGSUSED */
757789Sahrens static void
758789Sahrens hdr_dest(void *vbuf, void *unused)
759789Sahrens {
760789Sahrens 	arc_buf_hdr_t *buf = vbuf;
761789Sahrens 
762789Sahrens 	refcount_destroy(&buf->b_refcnt);
763789Sahrens 	cv_destroy(&buf->b_cv);
7644831Sgw25295 	mutex_destroy(&buf->b_freeze_lock);
7655450Sbrendan 
766*6018Sbrendan 	ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
767789Sahrens }
768789Sahrens 
769789Sahrens /*
770789Sahrens  * Reclaim callback -- invoked when memory is low.
771789Sahrens  */
772789Sahrens /* ARGSUSED */
773789Sahrens static void
774789Sahrens hdr_recl(void *unused)
775789Sahrens {
776789Sahrens 	dprintf("hdr_recl called\n");
7773158Smaybee 	/*
7783158Smaybee 	 * umem calls the reclaim func when we destroy the buf cache,
7793158Smaybee 	 * which is after we do arc_fini().
7803158Smaybee 	 */
7813158Smaybee 	if (!arc_dead)
7823158Smaybee 		cv_signal(&arc_reclaim_thr_cv);
783789Sahrens }
784789Sahrens 
785789Sahrens static void
786789Sahrens buf_init(void)
787789Sahrens {
788789Sahrens 	uint64_t *ct;
7891544Seschrock 	uint64_t hsize = 1ULL << 12;
790789Sahrens 	int i, j;
791789Sahrens 
792789Sahrens 	/*
793789Sahrens 	 * The hash table is big enough to fill all of physical memory
7941544Seschrock 	 * with an average 64K block size.  The table will take up
7951544Seschrock 	 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
796789Sahrens 	 */
7971544Seschrock 	while (hsize * 65536 < physmem * PAGESIZE)
798789Sahrens 		hsize <<= 1;
7991544Seschrock retry:
800789Sahrens 	buf_hash_table.ht_mask = hsize - 1;
8011544Seschrock 	buf_hash_table.ht_table =
8021544Seschrock 	    kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
8031544Seschrock 	if (buf_hash_table.ht_table == NULL) {
8041544Seschrock 		ASSERT(hsize > (1ULL << 8));
8051544Seschrock 		hsize >>= 1;
8061544Seschrock 		goto retry;
8071544Seschrock 	}
808789Sahrens 
809789Sahrens 	hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
810789Sahrens 	    0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
811789Sahrens 	buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
812789Sahrens 	    0, NULL, NULL, NULL, NULL, NULL, 0);
813789Sahrens 
814789Sahrens 	for (i = 0; i < 256; i++)
815789Sahrens 		for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
816789Sahrens 			*ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
817789Sahrens 
818789Sahrens 	for (i = 0; i < BUF_LOCKS; i++) {
819789Sahrens 		mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
820789Sahrens 		    NULL, MUTEX_DEFAULT, NULL);
821789Sahrens 	}
822789Sahrens }
823789Sahrens 
824789Sahrens #define	ARC_MINTIME	(hz>>4) /* 62 ms */
825789Sahrens 
826789Sahrens static void
8273093Sahrens arc_cksum_verify(arc_buf_t *buf)
8283093Sahrens {
8293093Sahrens 	zio_cksum_t zc;
8303093Sahrens 
8313312Sahrens 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
8323093Sahrens 		return;
8333093Sahrens 
8343093Sahrens 	mutex_enter(&buf->b_hdr->b_freeze_lock);
8353265Sahrens 	if (buf->b_hdr->b_freeze_cksum == NULL ||
8363265Sahrens 	    (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
8373093Sahrens 		mutex_exit(&buf->b_hdr->b_freeze_lock);
8383093Sahrens 		return;
8393093Sahrens 	}
8403093Sahrens 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
8413093Sahrens 	if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
8423093Sahrens 		panic("buffer modified while frozen!");
8433093Sahrens 	mutex_exit(&buf->b_hdr->b_freeze_lock);
8443093Sahrens }
8453093Sahrens 
8465450Sbrendan static int
8475450Sbrendan arc_cksum_equal(arc_buf_t *buf)
8485450Sbrendan {
8495450Sbrendan 	zio_cksum_t zc;
8505450Sbrendan 	int equal;
8515450Sbrendan 
8525450Sbrendan 	mutex_enter(&buf->b_hdr->b_freeze_lock);
8535450Sbrendan 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
8545450Sbrendan 	equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
8555450Sbrendan 	mutex_exit(&buf->b_hdr->b_freeze_lock);
8565450Sbrendan 
8575450Sbrendan 	return (equal);
8585450Sbrendan }
8595450Sbrendan 
8603093Sahrens static void
8615450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force)
8623093Sahrens {
8635450Sbrendan 	if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
8643093Sahrens 		return;
8653093Sahrens 
8663093Sahrens 	mutex_enter(&buf->b_hdr->b_freeze_lock);
8673093Sahrens 	if (buf->b_hdr->b_freeze_cksum != NULL) {
8683093Sahrens 		mutex_exit(&buf->b_hdr->b_freeze_lock);
8693093Sahrens 		return;
8703093Sahrens 	}
8713093Sahrens 	buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
8723093Sahrens 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
8733093Sahrens 	    buf->b_hdr->b_freeze_cksum);
8743093Sahrens 	mutex_exit(&buf->b_hdr->b_freeze_lock);
8753093Sahrens }
8763093Sahrens 
8773093Sahrens void
8783093Sahrens arc_buf_thaw(arc_buf_t *buf)
8793093Sahrens {
8805450Sbrendan 	if (zfs_flags & ZFS_DEBUG_MODIFY) {
8815450Sbrendan 		if (buf->b_hdr->b_state != arc_anon)
8825450Sbrendan 			panic("modifying non-anon buffer!");
8835450Sbrendan 		if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
8845450Sbrendan 			panic("modifying buffer while i/o in progress!");
8855450Sbrendan 		arc_cksum_verify(buf);
8865450Sbrendan 	}
8875450Sbrendan 
8883093Sahrens 	mutex_enter(&buf->b_hdr->b_freeze_lock);
8893093Sahrens 	if (buf->b_hdr->b_freeze_cksum != NULL) {
8903093Sahrens 		kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
8913093Sahrens 		buf->b_hdr->b_freeze_cksum = NULL;
8923093Sahrens 	}
8933093Sahrens 	mutex_exit(&buf->b_hdr->b_freeze_lock);
8943093Sahrens }
8953093Sahrens 
8963093Sahrens void
8973093Sahrens arc_buf_freeze(arc_buf_t *buf)
8983093Sahrens {
8993312Sahrens 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
9003312Sahrens 		return;
9013312Sahrens 
9023093Sahrens 	ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
9033403Sbmc 	    buf->b_hdr->b_state == arc_anon);
9045450Sbrendan 	arc_cksum_compute(buf, B_FALSE);
9053093Sahrens }
9063093Sahrens 
9073093Sahrens static void
908789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
909789Sahrens {
910789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
911789Sahrens 
912789Sahrens 	if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
9133403Sbmc 	    (ab->b_state != arc_anon)) {
9143700Sek110237 		uint64_t delta = ab->b_size * ab->b_datacnt;
9154309Smaybee 		list_t *list = &ab->b_state->arcs_list[ab->b_type];
9164309Smaybee 		uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
917789Sahrens 
9183403Sbmc 		ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
9193403Sbmc 		mutex_enter(&ab->b_state->arcs_mtx);
920789Sahrens 		ASSERT(list_link_active(&ab->b_arc_node));
9214309Smaybee 		list_remove(list, ab);
9221544Seschrock 		if (GHOST_STATE(ab->b_state)) {
9231544Seschrock 			ASSERT3U(ab->b_datacnt, ==, 0);
9241544Seschrock 			ASSERT3P(ab->b_buf, ==, NULL);
9251544Seschrock 			delta = ab->b_size;
9261544Seschrock 		}
9271544Seschrock 		ASSERT(delta > 0);
9284309Smaybee 		ASSERT3U(*size, >=, delta);
9294309Smaybee 		atomic_add_64(size, -delta);
9303403Sbmc 		mutex_exit(&ab->b_state->arcs_mtx);
9312391Smaybee 		/* remove the prefetch flag is we get a reference */
9322391Smaybee 		if (ab->b_flags & ARC_PREFETCH)
9332391Smaybee 			ab->b_flags &= ~ARC_PREFETCH;
934789Sahrens 	}
935789Sahrens }
936789Sahrens 
937789Sahrens static int
938789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
939789Sahrens {
940789Sahrens 	int cnt;
9413403Sbmc 	arc_state_t *state = ab->b_state;
942789Sahrens 
9433403Sbmc 	ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
9443403Sbmc 	ASSERT(!GHOST_STATE(state));
945789Sahrens 
946789Sahrens 	if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
9473403Sbmc 	    (state != arc_anon)) {
9484309Smaybee 		uint64_t *size = &state->arcs_lsize[ab->b_type];
9494309Smaybee 
9503403Sbmc 		ASSERT(!MUTEX_HELD(&state->arcs_mtx));
9513403Sbmc 		mutex_enter(&state->arcs_mtx);
952789Sahrens 		ASSERT(!list_link_active(&ab->b_arc_node));
9534309Smaybee 		list_insert_head(&state->arcs_list[ab->b_type], ab);
9541544Seschrock 		ASSERT(ab->b_datacnt > 0);
9554309Smaybee 		atomic_add_64(size, ab->b_size * ab->b_datacnt);
9563403Sbmc 		mutex_exit(&state->arcs_mtx);
957789Sahrens 	}
958789Sahrens 	return (cnt);
959789Sahrens }
960789Sahrens 
961789Sahrens /*
962789Sahrens  * Move the supplied buffer to the indicated state.  The mutex
963789Sahrens  * for the buffer must be held by the caller.
964789Sahrens  */
965789Sahrens static void
9661544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
967789Sahrens {
9681544Seschrock 	arc_state_t *old_state = ab->b_state;
9693700Sek110237 	int64_t refcnt = refcount_count(&ab->b_refcnt);
9703700Sek110237 	uint64_t from_delta, to_delta;
971789Sahrens 
972789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
9731544Seschrock 	ASSERT(new_state != old_state);
9741544Seschrock 	ASSERT(refcnt == 0 || ab->b_datacnt > 0);
9751544Seschrock 	ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
9761544Seschrock 
9771544Seschrock 	from_delta = to_delta = ab->b_datacnt * ab->b_size;
978789Sahrens 
979789Sahrens 	/*
980789Sahrens 	 * If this buffer is evictable, transfer it from the
981789Sahrens 	 * old state list to the new state list.
982789Sahrens 	 */
9831544Seschrock 	if (refcnt == 0) {
9843403Sbmc 		if (old_state != arc_anon) {
9853403Sbmc 			int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
9864309Smaybee 			uint64_t *size = &old_state->arcs_lsize[ab->b_type];
9871544Seschrock 
9881544Seschrock 			if (use_mutex)
9893403Sbmc 				mutex_enter(&old_state->arcs_mtx);
9901544Seschrock 
9911544Seschrock 			ASSERT(list_link_active(&ab->b_arc_node));
9924309Smaybee 			list_remove(&old_state->arcs_list[ab->b_type], ab);
993789Sahrens 
9942391Smaybee 			/*
9952391Smaybee 			 * If prefetching out of the ghost cache,
9962391Smaybee 			 * we will have a non-null datacnt.
9972391Smaybee 			 */
9982391Smaybee 			if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
9992391Smaybee 				/* ghost elements have a ghost size */
10001544Seschrock 				ASSERT(ab->b_buf == NULL);
10011544Seschrock 				from_delta = ab->b_size;
1002789Sahrens 			}
10034309Smaybee 			ASSERT3U(*size, >=, from_delta);
10044309Smaybee 			atomic_add_64(size, -from_delta);
10051544Seschrock 
10061544Seschrock 			if (use_mutex)
10073403Sbmc 				mutex_exit(&old_state->arcs_mtx);
1008789Sahrens 		}
10093403Sbmc 		if (new_state != arc_anon) {
10103403Sbmc 			int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
10114309Smaybee 			uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1012789Sahrens 
10131544Seschrock 			if (use_mutex)
10143403Sbmc 				mutex_enter(&new_state->arcs_mtx);
10151544Seschrock 
10164309Smaybee 			list_insert_head(&new_state->arcs_list[ab->b_type], ab);
10171544Seschrock 
10181544Seschrock 			/* ghost elements have a ghost size */
10191544Seschrock 			if (GHOST_STATE(new_state)) {
10201544Seschrock 				ASSERT(ab->b_datacnt == 0);
10211544Seschrock 				ASSERT(ab->b_buf == NULL);
10221544Seschrock 				to_delta = ab->b_size;
10231544Seschrock 			}
10244309Smaybee 			atomic_add_64(size, to_delta);
10251544Seschrock 
10261544Seschrock 			if (use_mutex)
10273403Sbmc 				mutex_exit(&new_state->arcs_mtx);
1028789Sahrens 		}
1029789Sahrens 	}
1030789Sahrens 
1031789Sahrens 	ASSERT(!BUF_EMPTY(ab));
10325450Sbrendan 	if (new_state == arc_anon) {
1033789Sahrens 		buf_hash_remove(ab);
1034789Sahrens 	}
1035789Sahrens 
10361544Seschrock 	/* adjust state sizes */
10371544Seschrock 	if (to_delta)
10383403Sbmc 		atomic_add_64(&new_state->arcs_size, to_delta);
10391544Seschrock 	if (from_delta) {
10403403Sbmc 		ASSERT3U(old_state->arcs_size, >=, from_delta);
10413403Sbmc 		atomic_add_64(&old_state->arcs_size, -from_delta);
1042789Sahrens 	}
1043789Sahrens 	ab->b_state = new_state;
10445450Sbrendan 
10455450Sbrendan 	/* adjust l2arc hdr stats */
10465450Sbrendan 	if (new_state == arc_l2c_only)
10475450Sbrendan 		l2arc_hdr_stat_add();
10485450Sbrendan 	else if (old_state == arc_l2c_only)
10495450Sbrendan 		l2arc_hdr_stat_remove();
1050789Sahrens }
1051789Sahrens 
10524309Smaybee void
10534309Smaybee arc_space_consume(uint64_t space)
10544309Smaybee {
10554309Smaybee 	atomic_add_64(&arc_meta_used, space);
10564309Smaybee 	atomic_add_64(&arc_size, space);
10574309Smaybee }
10584309Smaybee 
10594309Smaybee void
10604309Smaybee arc_space_return(uint64_t space)
10614309Smaybee {
10624309Smaybee 	ASSERT(arc_meta_used >= space);
10634309Smaybee 	if (arc_meta_max < arc_meta_used)
10644309Smaybee 		arc_meta_max = arc_meta_used;
10654309Smaybee 	atomic_add_64(&arc_meta_used, -space);
10664309Smaybee 	ASSERT(arc_size >= space);
10674309Smaybee 	atomic_add_64(&arc_size, -space);
10684309Smaybee }
10694309Smaybee 
10704309Smaybee void *
10714309Smaybee arc_data_buf_alloc(uint64_t size)
10724309Smaybee {
10734309Smaybee 	if (arc_evict_needed(ARC_BUFC_DATA))
10744309Smaybee 		cv_signal(&arc_reclaim_thr_cv);
10754309Smaybee 	atomic_add_64(&arc_size, size);
10764309Smaybee 	return (zio_data_buf_alloc(size));
10774309Smaybee }
10784309Smaybee 
10794309Smaybee void
10804309Smaybee arc_data_buf_free(void *buf, uint64_t size)
10814309Smaybee {
10824309Smaybee 	zio_data_buf_free(buf, size);
10834309Smaybee 	ASSERT(arc_size >= size);
10844309Smaybee 	atomic_add_64(&arc_size, -size);
10854309Smaybee }
10864309Smaybee 
1087789Sahrens arc_buf_t *
10883290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1089789Sahrens {
1090789Sahrens 	arc_buf_hdr_t *hdr;
1091789Sahrens 	arc_buf_t *buf;
1092789Sahrens 
1093789Sahrens 	ASSERT3U(size, >, 0);
1094789Sahrens 	hdr = kmem_cache_alloc(hdr_cache, KM_SLEEP);
1095789Sahrens 	ASSERT(BUF_EMPTY(hdr));
1096789Sahrens 	hdr->b_size = size;
10973290Sjohansen 	hdr->b_type = type;
1098789Sahrens 	hdr->b_spa = spa;
10993403Sbmc 	hdr->b_state = arc_anon;
1100789Sahrens 	hdr->b_arc_access = 0;
1101789Sahrens 	buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
1102789Sahrens 	buf->b_hdr = hdr;
11032688Smaybee 	buf->b_data = NULL;
11041544Seschrock 	buf->b_efunc = NULL;
11051544Seschrock 	buf->b_private = NULL;
1106789Sahrens 	buf->b_next = NULL;
1107789Sahrens 	hdr->b_buf = buf;
11082688Smaybee 	arc_get_data_buf(buf);
11091544Seschrock 	hdr->b_datacnt = 1;
1110789Sahrens 	hdr->b_flags = 0;
1111789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
1112789Sahrens 	(void) refcount_add(&hdr->b_refcnt, tag);
1113789Sahrens 
1114789Sahrens 	return (buf);
1115789Sahrens }
1116789Sahrens 
11172688Smaybee static arc_buf_t *
11182688Smaybee arc_buf_clone(arc_buf_t *from)
11191544Seschrock {
11202688Smaybee 	arc_buf_t *buf;
11212688Smaybee 	arc_buf_hdr_t *hdr = from->b_hdr;
11222688Smaybee 	uint64_t size = hdr->b_size;
11231544Seschrock 
11242688Smaybee 	buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
11252688Smaybee 	buf->b_hdr = hdr;
11262688Smaybee 	buf->b_data = NULL;
11272688Smaybee 	buf->b_efunc = NULL;
11282688Smaybee 	buf->b_private = NULL;
11292688Smaybee 	buf->b_next = hdr->b_buf;
11302688Smaybee 	hdr->b_buf = buf;
11312688Smaybee 	arc_get_data_buf(buf);
11322688Smaybee 	bcopy(from->b_data, buf->b_data, size);
11332688Smaybee 	hdr->b_datacnt += 1;
11342688Smaybee 	return (buf);
11351544Seschrock }
11361544Seschrock 
11371544Seschrock void
11381544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag)
11391544Seschrock {
11402887Smaybee 	arc_buf_hdr_t *hdr;
11411544Seschrock 	kmutex_t *hash_lock;
11421544Seschrock 
11432724Smaybee 	/*
11442724Smaybee 	 * Check to see if this buffer is currently being evicted via
11452887Smaybee 	 * arc_do_user_evicts().
11462724Smaybee 	 */
11472887Smaybee 	mutex_enter(&arc_eviction_mtx);
11482887Smaybee 	hdr = buf->b_hdr;
11492887Smaybee 	if (hdr == NULL) {
11502887Smaybee 		mutex_exit(&arc_eviction_mtx);
11512724Smaybee 		return;
11522887Smaybee 	}
11532887Smaybee 	hash_lock = HDR_LOCK(hdr);
11542887Smaybee 	mutex_exit(&arc_eviction_mtx);
11552724Smaybee 
11562724Smaybee 	mutex_enter(hash_lock);
11571544Seschrock 	if (buf->b_data == NULL) {
11581544Seschrock 		/*
11591544Seschrock 		 * This buffer is evicted.
11601544Seschrock 		 */
11612724Smaybee 		mutex_exit(hash_lock);
11621544Seschrock 		return;
11631544Seschrock 	}
11641544Seschrock 
11652724Smaybee 	ASSERT(buf->b_hdr == hdr);
11663403Sbmc 	ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
11671544Seschrock 	add_reference(hdr, hash_lock, tag);
11682688Smaybee 	arc_access(hdr, hash_lock);
11692688Smaybee 	mutex_exit(hash_lock);
11703403Sbmc 	ARCSTAT_BUMP(arcstat_hits);
11713403Sbmc 	ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
11723403Sbmc 	    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
11733403Sbmc 	    data, metadata, hits);
11741544Seschrock }
11751544Seschrock 
11765450Sbrendan /*
11775450Sbrendan  * Free the arc data buffer.  If it is an l2arc write in progress,
11785450Sbrendan  * the buffer is placed on l2arc_free_on_write to be freed later.
11795450Sbrendan  */
11805450Sbrendan static void
11815450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t),
11825450Sbrendan     void *data, size_t size)
11835450Sbrendan {
11845450Sbrendan 	if (HDR_L2_WRITING(hdr)) {
11855450Sbrendan 		l2arc_data_free_t *df;
11865450Sbrendan 		df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
11875450Sbrendan 		df->l2df_data = data;
11885450Sbrendan 		df->l2df_size = size;
11895450Sbrendan 		df->l2df_func = free_func;
11905450Sbrendan 		mutex_enter(&l2arc_free_on_write_mtx);
11915450Sbrendan 		list_insert_head(l2arc_free_on_write, df);
11925450Sbrendan 		mutex_exit(&l2arc_free_on_write_mtx);
11935450Sbrendan 		ARCSTAT_BUMP(arcstat_l2_free_on_write);
11945450Sbrendan 	} else {
11955450Sbrendan 		free_func(data, size);
11965450Sbrendan 	}
11975450Sbrendan }
11985450Sbrendan 
1199789Sahrens static void
12002688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
12011544Seschrock {
12021544Seschrock 	arc_buf_t **bufp;
12031544Seschrock 
12041544Seschrock 	/* free up data associated with the buf */
12051544Seschrock 	if (buf->b_data) {
12061544Seschrock 		arc_state_t *state = buf->b_hdr->b_state;
12071544Seschrock 		uint64_t size = buf->b_hdr->b_size;
12083290Sjohansen 		arc_buf_contents_t type = buf->b_hdr->b_type;
12091544Seschrock 
12103093Sahrens 		arc_cksum_verify(buf);
12112688Smaybee 		if (!recycle) {
12123290Sjohansen 			if (type == ARC_BUFC_METADATA) {
12135450Sbrendan 				arc_buf_data_free(buf->b_hdr, zio_buf_free,
12145450Sbrendan 				    buf->b_data, size);
12154309Smaybee 				arc_space_return(size);
12163290Sjohansen 			} else {
12173290Sjohansen 				ASSERT(type == ARC_BUFC_DATA);
12185450Sbrendan 				arc_buf_data_free(buf->b_hdr,
12195450Sbrendan 				    zio_data_buf_free, buf->b_data, size);
12204309Smaybee 				atomic_add_64(&arc_size, -size);
12213290Sjohansen 			}
12222688Smaybee 		}
12231544Seschrock 		if (list_link_active(&buf->b_hdr->b_arc_node)) {
12244309Smaybee 			uint64_t *cnt = &state->arcs_lsize[type];
12254309Smaybee 
12261544Seschrock 			ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
12273403Sbmc 			ASSERT(state != arc_anon);
12284309Smaybee 
12294309Smaybee 			ASSERT3U(*cnt, >=, size);
12304309Smaybee 			atomic_add_64(cnt, -size);
12311544Seschrock 		}
12323403Sbmc 		ASSERT3U(state->arcs_size, >=, size);
12333403Sbmc 		atomic_add_64(&state->arcs_size, -size);
12341544Seschrock 		buf->b_data = NULL;
12351544Seschrock 		ASSERT(buf->b_hdr->b_datacnt > 0);
12361544Seschrock 		buf->b_hdr->b_datacnt -= 1;
12371544Seschrock 	}
12381544Seschrock 
12391544Seschrock 	/* only remove the buf if requested */
12401544Seschrock 	if (!all)
12411544Seschrock 		return;
12421544Seschrock 
12431544Seschrock 	/* remove the buf from the hdr list */
12441544Seschrock 	for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
12451544Seschrock 		continue;
12461544Seschrock 	*bufp = buf->b_next;
12471544Seschrock 
12481544Seschrock 	ASSERT(buf->b_efunc == NULL);
12491544Seschrock 
12501544Seschrock 	/* clean up the buf */
12511544Seschrock 	buf->b_hdr = NULL;
12521544Seschrock 	kmem_cache_free(buf_cache, buf);
12531544Seschrock }
12541544Seschrock 
12551544Seschrock static void
12561544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr)
1257789Sahrens {
1258789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
12593403Sbmc 	ASSERT3P(hdr->b_state, ==, arc_anon);
12601544Seschrock 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1261789Sahrens 
12625450Sbrendan 	if (hdr->b_l2hdr != NULL) {
12635450Sbrendan 		if (!MUTEX_HELD(&l2arc_buflist_mtx)) {
12645450Sbrendan 			/*
12655450Sbrendan 			 * To prevent arc_free() and l2arc_evict() from
12665450Sbrendan 			 * attempting to free the same buffer at the same time,
12675450Sbrendan 			 * a FREE_IN_PROGRESS flag is given to arc_free() to
12685450Sbrendan 			 * give it priority.  l2arc_evict() can't destroy this
12695450Sbrendan 			 * header while we are waiting on l2arc_buflist_mtx.
12705450Sbrendan 			 */
12715450Sbrendan 			mutex_enter(&l2arc_buflist_mtx);
12725450Sbrendan 			ASSERT(hdr->b_l2hdr != NULL);
12735450Sbrendan 
12745450Sbrendan 			list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr);
12755450Sbrendan 			mutex_exit(&l2arc_buflist_mtx);
12765450Sbrendan 		} else {
12775450Sbrendan 			list_remove(hdr->b_l2hdr->b_dev->l2ad_buflist, hdr);
12785450Sbrendan 		}
12795450Sbrendan 		ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
12805450Sbrendan 		kmem_free(hdr->b_l2hdr, sizeof (l2arc_buf_hdr_t));
12815450Sbrendan 		if (hdr->b_state == arc_l2c_only)
12825450Sbrendan 			l2arc_hdr_stat_remove();
12835450Sbrendan 		hdr->b_l2hdr = NULL;
12845450Sbrendan 	}
12855450Sbrendan 
1286789Sahrens 	if (!BUF_EMPTY(hdr)) {
12871544Seschrock 		ASSERT(!HDR_IN_HASH_TABLE(hdr));
1288789Sahrens 		bzero(&hdr->b_dva, sizeof (dva_t));
1289789Sahrens 		hdr->b_birth = 0;
1290789Sahrens 		hdr->b_cksum0 = 0;
1291789Sahrens 	}
12921544Seschrock 	while (hdr->b_buf) {
1293789Sahrens 		arc_buf_t *buf = hdr->b_buf;
1294789Sahrens 
12951544Seschrock 		if (buf->b_efunc) {
12961544Seschrock 			mutex_enter(&arc_eviction_mtx);
12971544Seschrock 			ASSERT(buf->b_hdr != NULL);
12982688Smaybee 			arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
12991544Seschrock 			hdr->b_buf = buf->b_next;
13002887Smaybee 			buf->b_hdr = &arc_eviction_hdr;
13011544Seschrock 			buf->b_next = arc_eviction_list;
13021544Seschrock 			arc_eviction_list = buf;
13031544Seschrock 			mutex_exit(&arc_eviction_mtx);
13041544Seschrock 		} else {
13052688Smaybee 			arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
13061544Seschrock 		}
1307789Sahrens 	}
13083093Sahrens 	if (hdr->b_freeze_cksum != NULL) {
13093093Sahrens 		kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
13103093Sahrens 		hdr->b_freeze_cksum = NULL;
13113093Sahrens 	}
13121544Seschrock 
1313789Sahrens 	ASSERT(!list_link_active(&hdr->b_arc_node));
1314789Sahrens 	ASSERT3P(hdr->b_hash_next, ==, NULL);
1315789Sahrens 	ASSERT3P(hdr->b_acb, ==, NULL);
1316789Sahrens 	kmem_cache_free(hdr_cache, hdr);
1317789Sahrens }
1318789Sahrens 
1319789Sahrens void
1320789Sahrens arc_buf_free(arc_buf_t *buf, void *tag)
1321789Sahrens {
1322789Sahrens 	arc_buf_hdr_t *hdr = buf->b_hdr;
13233403Sbmc 	int hashed = hdr->b_state != arc_anon;
13241544Seschrock 
13251544Seschrock 	ASSERT(buf->b_efunc == NULL);
13261544Seschrock 	ASSERT(buf->b_data != NULL);
13271544Seschrock 
13281544Seschrock 	if (hashed) {
13291544Seschrock 		kmutex_t *hash_lock = HDR_LOCK(hdr);
13301544Seschrock 
13311544Seschrock 		mutex_enter(hash_lock);
13321544Seschrock 		(void) remove_reference(hdr, hash_lock, tag);
13331544Seschrock 		if (hdr->b_datacnt > 1)
13342688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
13351544Seschrock 		else
13361544Seschrock 			hdr->b_flags |= ARC_BUF_AVAILABLE;
13371544Seschrock 		mutex_exit(hash_lock);
13381544Seschrock 	} else if (HDR_IO_IN_PROGRESS(hdr)) {
13391544Seschrock 		int destroy_hdr;
13401544Seschrock 		/*
13411544Seschrock 		 * We are in the middle of an async write.  Don't destroy
13421544Seschrock 		 * this buffer unless the write completes before we finish
13431544Seschrock 		 * decrementing the reference count.
13441544Seschrock 		 */
13451544Seschrock 		mutex_enter(&arc_eviction_mtx);
13461544Seschrock 		(void) remove_reference(hdr, NULL, tag);
13471544Seschrock 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
13481544Seschrock 		destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
13491544Seschrock 		mutex_exit(&arc_eviction_mtx);
13501544Seschrock 		if (destroy_hdr)
13511544Seschrock 			arc_hdr_destroy(hdr);
13521544Seschrock 	} else {
13531544Seschrock 		if (remove_reference(hdr, NULL, tag) > 0) {
13541544Seschrock 			ASSERT(HDR_IO_ERROR(hdr));
13552688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
13561544Seschrock 		} else {
13571544Seschrock 			arc_hdr_destroy(hdr);
13581544Seschrock 		}
13591544Seschrock 	}
13601544Seschrock }
13611544Seschrock 
13621544Seschrock int
13631544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag)
13641544Seschrock {
13651544Seschrock 	arc_buf_hdr_t *hdr = buf->b_hdr;
1366789Sahrens 	kmutex_t *hash_lock = HDR_LOCK(hdr);
13671544Seschrock 	int no_callback = (buf->b_efunc == NULL);
13681544Seschrock 
13693403Sbmc 	if (hdr->b_state == arc_anon) {
13701544Seschrock 		arc_buf_free(buf, tag);
13711544Seschrock 		return (no_callback);
13721544Seschrock 	}
1373789Sahrens 
1374789Sahrens 	mutex_enter(hash_lock);
13753403Sbmc 	ASSERT(hdr->b_state != arc_anon);
13761544Seschrock 	ASSERT(buf->b_data != NULL);
1377789Sahrens 
13781544Seschrock 	(void) remove_reference(hdr, hash_lock, tag);
13791544Seschrock 	if (hdr->b_datacnt > 1) {
13801544Seschrock 		if (no_callback)
13812688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
13821544Seschrock 	} else if (no_callback) {
13831544Seschrock 		ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
13841544Seschrock 		hdr->b_flags |= ARC_BUF_AVAILABLE;
1385789Sahrens 	}
13861544Seschrock 	ASSERT(no_callback || hdr->b_datacnt > 1 ||
13871544Seschrock 	    refcount_is_zero(&hdr->b_refcnt));
1388789Sahrens 	mutex_exit(hash_lock);
13891544Seschrock 	return (no_callback);
1390789Sahrens }
1391789Sahrens 
1392789Sahrens int
1393789Sahrens arc_buf_size(arc_buf_t *buf)
1394789Sahrens {
1395789Sahrens 	return (buf->b_hdr->b_size);
1396789Sahrens }
1397789Sahrens 
1398789Sahrens /*
1399789Sahrens  * Evict buffers from list until we've removed the specified number of
1400789Sahrens  * bytes.  Move the removed buffers to the appropriate evict state.
14012688Smaybee  * If the recycle flag is set, then attempt to "recycle" a buffer:
14022688Smaybee  * - look for a buffer to evict that is `bytes' long.
14032688Smaybee  * - return the data block from this buffer rather than freeing it.
14042688Smaybee  * This flag is used by callers that are trying to make space for a
14052688Smaybee  * new buffer in a full arc cache.
14065642Smaybee  *
14075642Smaybee  * This function makes a "best effort".  It skips over any buffers
14085642Smaybee  * it can't get a hash_lock on, and so may not catch all candidates.
14095642Smaybee  * It may also return without evicting as much space as requested.
1410789Sahrens  */
14112688Smaybee static void *
14125642Smaybee arc_evict(arc_state_t *state, spa_t *spa, int64_t bytes, boolean_t recycle,
14133290Sjohansen     arc_buf_contents_t type)
1414789Sahrens {
1415789Sahrens 	arc_state_t *evicted_state;
14162688Smaybee 	uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
14172918Smaybee 	arc_buf_hdr_t *ab, *ab_prev = NULL;
14184309Smaybee 	list_t *list = &state->arcs_list[type];
1419789Sahrens 	kmutex_t *hash_lock;
14202688Smaybee 	boolean_t have_lock;
14212918Smaybee 	void *stolen = NULL;
1422789Sahrens 
14233403Sbmc 	ASSERT(state == arc_mru || state == arc_mfu);
1424789Sahrens 
14253403Sbmc 	evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1426789Sahrens 
14273403Sbmc 	mutex_enter(&state->arcs_mtx);
14283403Sbmc 	mutex_enter(&evicted_state->arcs_mtx);
1429789Sahrens 
14304309Smaybee 	for (ab = list_tail(list); ab; ab = ab_prev) {
14314309Smaybee 		ab_prev = list_prev(list, ab);
14322391Smaybee 		/* prefetch buffers have a minimum lifespan */
14332688Smaybee 		if (HDR_IO_IN_PROGRESS(ab) ||
14345642Smaybee 		    (spa && ab->b_spa != spa) ||
14352688Smaybee 		    (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
14362688Smaybee 		    lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) {
14372391Smaybee 			skipped++;
14382391Smaybee 			continue;
14392391Smaybee 		}
14402918Smaybee 		/* "lookahead" for better eviction candidate */
14412918Smaybee 		if (recycle && ab->b_size != bytes &&
14422918Smaybee 		    ab_prev && ab_prev->b_size == bytes)
14432688Smaybee 			continue;
1444789Sahrens 		hash_lock = HDR_LOCK(ab);
14452688Smaybee 		have_lock = MUTEX_HELD(hash_lock);
14462688Smaybee 		if (have_lock || mutex_tryenter(hash_lock)) {
1447789Sahrens 			ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0);
14481544Seschrock 			ASSERT(ab->b_datacnt > 0);
14491544Seschrock 			while (ab->b_buf) {
14501544Seschrock 				arc_buf_t *buf = ab->b_buf;
14512688Smaybee 				if (buf->b_data) {
14521544Seschrock 					bytes_evicted += ab->b_size;
14533290Sjohansen 					if (recycle && ab->b_type == type &&
14545450Sbrendan 					    ab->b_size == bytes &&
14555450Sbrendan 					    !HDR_L2_WRITING(ab)) {
14562918Smaybee 						stolen = buf->b_data;
14572918Smaybee 						recycle = FALSE;
14582918Smaybee 					}
14592688Smaybee 				}
14601544Seschrock 				if (buf->b_efunc) {
14611544Seschrock 					mutex_enter(&arc_eviction_mtx);
14622918Smaybee 					arc_buf_destroy(buf,
14632918Smaybee 					    buf->b_data == stolen, FALSE);
14641544Seschrock 					ab->b_buf = buf->b_next;
14652887Smaybee 					buf->b_hdr = &arc_eviction_hdr;
14661544Seschrock 					buf->b_next = arc_eviction_list;
14671544Seschrock 					arc_eviction_list = buf;
14681544Seschrock 					mutex_exit(&arc_eviction_mtx);
14691544Seschrock 				} else {
14702918Smaybee 					arc_buf_destroy(buf,
14712918Smaybee 					    buf->b_data == stolen, TRUE);
14721544Seschrock 				}
14731544Seschrock 			}
14741544Seschrock 			ASSERT(ab->b_datacnt == 0);
1475789Sahrens 			arc_change_state(evicted_state, ab, hash_lock);
14761544Seschrock 			ASSERT(HDR_IN_HASH_TABLE(ab));
14775450Sbrendan 			ab->b_flags |= ARC_IN_HASH_TABLE;
14785450Sbrendan 			ab->b_flags &= ~ARC_BUF_AVAILABLE;
1479789Sahrens 			DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
14802688Smaybee 			if (!have_lock)
14812688Smaybee 				mutex_exit(hash_lock);
14821544Seschrock 			if (bytes >= 0 && bytes_evicted >= bytes)
1483789Sahrens 				break;
1484789Sahrens 		} else {
14852688Smaybee 			missed += 1;
1486789Sahrens 		}
1487789Sahrens 	}
14883403Sbmc 
14893403Sbmc 	mutex_exit(&evicted_state->arcs_mtx);
14903403Sbmc 	mutex_exit(&state->arcs_mtx);
1491789Sahrens 
1492789Sahrens 	if (bytes_evicted < bytes)
1493789Sahrens 		dprintf("only evicted %lld bytes from %x",
1494789Sahrens 		    (longlong_t)bytes_evicted, state);
1495789Sahrens 
14962688Smaybee 	if (skipped)
14973403Sbmc 		ARCSTAT_INCR(arcstat_evict_skip, skipped);
14983403Sbmc 
14992688Smaybee 	if (missed)
15003403Sbmc 		ARCSTAT_INCR(arcstat_mutex_miss, missed);
15013403Sbmc 
15024709Smaybee 	/*
15034709Smaybee 	 * We have just evicted some date into the ghost state, make
15044709Smaybee 	 * sure we also adjust the ghost state size if necessary.
15054709Smaybee 	 */
15064709Smaybee 	if (arc_no_grow &&
15074709Smaybee 	    arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
15084709Smaybee 		int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
15094709Smaybee 		    arc_mru_ghost->arcs_size - arc_c;
15104709Smaybee 
15114709Smaybee 		if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
15124709Smaybee 			int64_t todelete =
15134709Smaybee 			    MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
15145642Smaybee 			arc_evict_ghost(arc_mru_ghost, NULL, todelete);
15154709Smaybee 		} else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
15164709Smaybee 			int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
15174709Smaybee 			    arc_mru_ghost->arcs_size +
15184709Smaybee 			    arc_mfu_ghost->arcs_size - arc_c);
15195642Smaybee 			arc_evict_ghost(arc_mfu_ghost, NULL, todelete);
15204709Smaybee 		}
15214709Smaybee 	}
15224709Smaybee 
15232918Smaybee 	return (stolen);
1524789Sahrens }
1525789Sahrens 
1526789Sahrens /*
1527789Sahrens  * Remove buffers from list until we've removed the specified number of
1528789Sahrens  * bytes.  Destroy the buffers that are removed.
1529789Sahrens  */
1530789Sahrens static void
15315642Smaybee arc_evict_ghost(arc_state_t *state, spa_t *spa, int64_t bytes)
1532789Sahrens {
1533789Sahrens 	arc_buf_hdr_t *ab, *ab_prev;
15344309Smaybee 	list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1535789Sahrens 	kmutex_t *hash_lock;
15361544Seschrock 	uint64_t bytes_deleted = 0;
15373700Sek110237 	uint64_t bufs_skipped = 0;
1538789Sahrens 
15391544Seschrock 	ASSERT(GHOST_STATE(state));
1540789Sahrens top:
15413403Sbmc 	mutex_enter(&state->arcs_mtx);
15424309Smaybee 	for (ab = list_tail(list); ab; ab = ab_prev) {
15434309Smaybee 		ab_prev = list_prev(list, ab);
15445642Smaybee 		if (spa && ab->b_spa != spa)
15455642Smaybee 			continue;
1546789Sahrens 		hash_lock = HDR_LOCK(ab);
1547789Sahrens 		if (mutex_tryenter(hash_lock)) {
15482391Smaybee 			ASSERT(!HDR_IO_IN_PROGRESS(ab));
15491544Seschrock 			ASSERT(ab->b_buf == NULL);
15503403Sbmc 			ARCSTAT_BUMP(arcstat_deleted);
15511544Seschrock 			bytes_deleted += ab->b_size;
15525450Sbrendan 
15535450Sbrendan 			if (ab->b_l2hdr != NULL) {
15545450Sbrendan 				/*
15555450Sbrendan 				 * This buffer is cached on the 2nd Level ARC;
15565450Sbrendan 				 * don't destroy the header.
15575450Sbrendan 				 */
15585450Sbrendan 				arc_change_state(arc_l2c_only, ab, hash_lock);
15595450Sbrendan 				mutex_exit(hash_lock);
15605450Sbrendan 			} else {
15615450Sbrendan 				arc_change_state(arc_anon, ab, hash_lock);
15625450Sbrendan 				mutex_exit(hash_lock);
15635450Sbrendan 				arc_hdr_destroy(ab);
15645450Sbrendan 			}
15655450Sbrendan 
1566789Sahrens 			DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1567789Sahrens 			if (bytes >= 0 && bytes_deleted >= bytes)
1568789Sahrens 				break;
1569789Sahrens 		} else {
1570789Sahrens 			if (bytes < 0) {
15713403Sbmc 				mutex_exit(&state->arcs_mtx);
1572789Sahrens 				mutex_enter(hash_lock);
1573789Sahrens 				mutex_exit(hash_lock);
1574789Sahrens 				goto top;
1575789Sahrens 			}
1576789Sahrens 			bufs_skipped += 1;
1577789Sahrens 		}
1578789Sahrens 	}
15793403Sbmc 	mutex_exit(&state->arcs_mtx);
1580789Sahrens 
15814309Smaybee 	if (list == &state->arcs_list[ARC_BUFC_DATA] &&
15824309Smaybee 	    (bytes < 0 || bytes_deleted < bytes)) {
15834309Smaybee 		list = &state->arcs_list[ARC_BUFC_METADATA];
15844309Smaybee 		goto top;
15854309Smaybee 	}
15864309Smaybee 
1587789Sahrens 	if (bufs_skipped) {
15883403Sbmc 		ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
1589789Sahrens 		ASSERT(bytes >= 0);
1590789Sahrens 	}
1591789Sahrens 
1592789Sahrens 	if (bytes_deleted < bytes)
1593789Sahrens 		dprintf("only deleted %lld bytes from %p",
1594789Sahrens 		    (longlong_t)bytes_deleted, state);
1595789Sahrens }
1596789Sahrens 
1597789Sahrens static void
1598789Sahrens arc_adjust(void)
1599789Sahrens {
16003403Sbmc 	int64_t top_sz, mru_over, arc_over, todelete;
1601789Sahrens 
16025642Smaybee 	top_sz = arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used;
1603789Sahrens 
16044309Smaybee 	if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
16054309Smaybee 		int64_t toevict =
16064309Smaybee 		    MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], top_sz - arc_p);
16075642Smaybee 		(void) arc_evict(arc_mru, NULL, toevict, FALSE, ARC_BUFC_DATA);
16084309Smaybee 		top_sz = arc_anon->arcs_size + arc_mru->arcs_size;
16094309Smaybee 	}
16104309Smaybee 
16114309Smaybee 	if (top_sz > arc_p && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
16124309Smaybee 		int64_t toevict =
16134309Smaybee 		    MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], top_sz - arc_p);
16145642Smaybee 		(void) arc_evict(arc_mru, NULL, toevict, FALSE,
16155642Smaybee 		    ARC_BUFC_METADATA);
16163403Sbmc 		top_sz = arc_anon->arcs_size + arc_mru->arcs_size;
1617789Sahrens 	}
1618789Sahrens 
16193403Sbmc 	mru_over = top_sz + arc_mru_ghost->arcs_size - arc_c;
1620789Sahrens 
1621789Sahrens 	if (mru_over > 0) {
16224309Smaybee 		if (arc_mru_ghost->arcs_size > 0) {
16234309Smaybee 			todelete = MIN(arc_mru_ghost->arcs_size, mru_over);
16245642Smaybee 			arc_evict_ghost(arc_mru_ghost, NULL, todelete);
1625789Sahrens 		}
1626789Sahrens 	}
1627789Sahrens 
16283403Sbmc 	if ((arc_over = arc_size - arc_c) > 0) {
16291544Seschrock 		int64_t tbl_over;
1630789Sahrens 
16314309Smaybee 		if (arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
16324309Smaybee 			int64_t toevict =
16334309Smaybee 			    MIN(arc_mfu->arcs_lsize[ARC_BUFC_DATA], arc_over);
16345642Smaybee 			(void) arc_evict(arc_mfu, NULL, toevict, FALSE,
16354309Smaybee 			    ARC_BUFC_DATA);
16364309Smaybee 			arc_over = arc_size - arc_c;
1637789Sahrens 		}
1638789Sahrens 
16394309Smaybee 		if (arc_over > 0 &&
16404309Smaybee 		    arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
16414309Smaybee 			int64_t toevict =
16424309Smaybee 			    MIN(arc_mfu->arcs_lsize[ARC_BUFC_METADATA],
16434309Smaybee 			    arc_over);
16445642Smaybee 			(void) arc_evict(arc_mfu, NULL, toevict, FALSE,
16454309Smaybee 			    ARC_BUFC_METADATA);
16464309Smaybee 		}
16474309Smaybee 
16484309Smaybee 		tbl_over = arc_size + arc_mru_ghost->arcs_size +
16494309Smaybee 		    arc_mfu_ghost->arcs_size - arc_c * 2;
16504309Smaybee 
16514309Smaybee 		if (tbl_over > 0 && arc_mfu_ghost->arcs_size > 0) {
16524309Smaybee 			todelete = MIN(arc_mfu_ghost->arcs_size, tbl_over);
16535642Smaybee 			arc_evict_ghost(arc_mfu_ghost, NULL, todelete);
1654789Sahrens 		}
1655789Sahrens 	}
1656789Sahrens }
1657789Sahrens 
16581544Seschrock static void
16591544Seschrock arc_do_user_evicts(void)
16601544Seschrock {
16611544Seschrock 	mutex_enter(&arc_eviction_mtx);
16621544Seschrock 	while (arc_eviction_list != NULL) {
16631544Seschrock 		arc_buf_t *buf = arc_eviction_list;
16641544Seschrock 		arc_eviction_list = buf->b_next;
16651544Seschrock 		buf->b_hdr = NULL;
16661544Seschrock 		mutex_exit(&arc_eviction_mtx);
16671544Seschrock 
16681819Smaybee 		if (buf->b_efunc != NULL)
16691819Smaybee 			VERIFY(buf->b_efunc(buf) == 0);
16701544Seschrock 
16711544Seschrock 		buf->b_efunc = NULL;
16721544Seschrock 		buf->b_private = NULL;
16731544Seschrock 		kmem_cache_free(buf_cache, buf);
16741544Seschrock 		mutex_enter(&arc_eviction_mtx);
16751544Seschrock 	}
16761544Seschrock 	mutex_exit(&arc_eviction_mtx);
16771544Seschrock }
16781544Seschrock 
1679789Sahrens /*
16805642Smaybee  * Flush all *evictable* data from the cache for the given spa.
1681789Sahrens  * NOTE: this will not touch "active" (i.e. referenced) data.
1682789Sahrens  */
1683789Sahrens void
16845642Smaybee arc_flush(spa_t *spa)
1685789Sahrens {
16865642Smaybee 	while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
16875642Smaybee 		(void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_DATA);
16885642Smaybee 		if (spa)
16895642Smaybee 			break;
16905642Smaybee 	}
16915642Smaybee 	while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
16925642Smaybee 		(void) arc_evict(arc_mru, spa, -1, FALSE, ARC_BUFC_METADATA);
16935642Smaybee 		if (spa)
16945642Smaybee 			break;
16955642Smaybee 	}
16965642Smaybee 	while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
16975642Smaybee 		(void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_DATA);
16985642Smaybee 		if (spa)
16995642Smaybee 			break;
17005642Smaybee 	}
17015642Smaybee 	while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
17025642Smaybee 		(void) arc_evict(arc_mfu, spa, -1, FALSE, ARC_BUFC_METADATA);
17035642Smaybee 		if (spa)
17045642Smaybee 			break;
17055642Smaybee 	}
17065642Smaybee 
17075642Smaybee 	arc_evict_ghost(arc_mru_ghost, spa, -1);
17085642Smaybee 	arc_evict_ghost(arc_mfu_ghost, spa, -1);
17091544Seschrock 
17101544Seschrock 	mutex_enter(&arc_reclaim_thr_lock);
17111544Seschrock 	arc_do_user_evicts();
17121544Seschrock 	mutex_exit(&arc_reclaim_thr_lock);
17135642Smaybee 	ASSERT(spa || arc_eviction_list == NULL);
1714789Sahrens }
1715789Sahrens 
17163158Smaybee int arc_shrink_shift = 5;		/* log2(fraction of arc to reclaim) */
17172391Smaybee 
1718789Sahrens void
17193158Smaybee arc_shrink(void)
1720789Sahrens {
17213403Sbmc 	if (arc_c > arc_c_min) {
17223158Smaybee 		uint64_t to_free;
1723789Sahrens 
17242048Sstans #ifdef _KERNEL
17253403Sbmc 		to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree));
17262048Sstans #else
17273403Sbmc 		to_free = arc_c >> arc_shrink_shift;
17282048Sstans #endif
17293403Sbmc 		if (arc_c > arc_c_min + to_free)
17303403Sbmc 			atomic_add_64(&arc_c, -to_free);
17313158Smaybee 		else
17323403Sbmc 			arc_c = arc_c_min;
17332048Sstans 
17343403Sbmc 		atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
17353403Sbmc 		if (arc_c > arc_size)
17363403Sbmc 			arc_c = MAX(arc_size, arc_c_min);
17373403Sbmc 		if (arc_p > arc_c)
17383403Sbmc 			arc_p = (arc_c >> 1);
17393403Sbmc 		ASSERT(arc_c >= arc_c_min);
17403403Sbmc 		ASSERT((int64_t)arc_p >= 0);
17413158Smaybee 	}
1742789Sahrens 
17433403Sbmc 	if (arc_size > arc_c)
17443158Smaybee 		arc_adjust();
1745789Sahrens }
1746789Sahrens 
1747789Sahrens static int
1748789Sahrens arc_reclaim_needed(void)
1749789Sahrens {
1750789Sahrens 	uint64_t extra;
1751789Sahrens 
1752789Sahrens #ifdef _KERNEL
17532048Sstans 
17542048Sstans 	if (needfree)
17552048Sstans 		return (1);
17562048Sstans 
1757789Sahrens 	/*
1758789Sahrens 	 * take 'desfree' extra pages, so we reclaim sooner, rather than later
1759789Sahrens 	 */
1760789Sahrens 	extra = desfree;
1761789Sahrens 
1762789Sahrens 	/*
1763789Sahrens 	 * check that we're out of range of the pageout scanner.  It starts to
1764789Sahrens 	 * schedule paging if freemem is less than lotsfree and needfree.
1765789Sahrens 	 * lotsfree is the high-water mark for pageout, and needfree is the
1766789Sahrens 	 * number of needed free pages.  We add extra pages here to make sure
1767789Sahrens 	 * the scanner doesn't start up while we're freeing memory.
1768789Sahrens 	 */
1769789Sahrens 	if (freemem < lotsfree + needfree + extra)
1770789Sahrens 		return (1);
1771789Sahrens 
1772789Sahrens 	/*
1773789Sahrens 	 * check to make sure that swapfs has enough space so that anon
17745450Sbrendan 	 * reservations can still succeed. anon_resvmem() checks that the
1775789Sahrens 	 * availrmem is greater than swapfs_minfree, and the number of reserved
1776789Sahrens 	 * swap pages.  We also add a bit of extra here just to prevent
1777789Sahrens 	 * circumstances from getting really dire.
1778789Sahrens 	 */
1779789Sahrens 	if (availrmem < swapfs_minfree + swapfs_reserve + extra)
1780789Sahrens 		return (1);
1781789Sahrens 
17821936Smaybee #if defined(__i386)
1783789Sahrens 	/*
1784789Sahrens 	 * If we're on an i386 platform, it's possible that we'll exhaust the
1785789Sahrens 	 * kernel heap space before we ever run out of available physical
1786789Sahrens 	 * memory.  Most checks of the size of the heap_area compare against
1787789Sahrens 	 * tune.t_minarmem, which is the minimum available real memory that we
1788789Sahrens 	 * can have in the system.  However, this is generally fixed at 25 pages
1789789Sahrens 	 * which is so low that it's useless.  In this comparison, we seek to
1790789Sahrens 	 * calculate the total heap-size, and reclaim if more than 3/4ths of the
17915450Sbrendan 	 * heap is allocated.  (Or, in the calculation, if less than 1/4th is
1792789Sahrens 	 * free)
1793789Sahrens 	 */
1794789Sahrens 	if (btop(vmem_size(heap_arena, VMEM_FREE)) <
1795789Sahrens 	    (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
1796789Sahrens 		return (1);
1797789Sahrens #endif
1798789Sahrens 
1799789Sahrens #else
1800789Sahrens 	if (spa_get_random(100) == 0)
1801789Sahrens 		return (1);
1802789Sahrens #endif
1803789Sahrens 	return (0);
1804789Sahrens }
1805789Sahrens 
1806789Sahrens static void
1807789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat)
1808789Sahrens {
1809789Sahrens 	size_t			i;
1810789Sahrens 	kmem_cache_t		*prev_cache = NULL;
18113290Sjohansen 	kmem_cache_t		*prev_data_cache = NULL;
1812789Sahrens 	extern kmem_cache_t	*zio_buf_cache[];
18133290Sjohansen 	extern kmem_cache_t	*zio_data_buf_cache[];
1814789Sahrens 
18151484Sek110237 #ifdef _KERNEL
18164309Smaybee 	if (arc_meta_used >= arc_meta_limit) {
18174309Smaybee 		/*
18184309Smaybee 		 * We are exceeding our meta-data cache limit.
18194309Smaybee 		 * Purge some DNLC entries to release holds on meta-data.
18204309Smaybee 		 */
18214309Smaybee 		dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
18224309Smaybee 	}
18231936Smaybee #if defined(__i386)
18241936Smaybee 	/*
18251936Smaybee 	 * Reclaim unused memory from all kmem caches.
18261936Smaybee 	 */
18271936Smaybee 	kmem_reap();
18281936Smaybee #endif
18291484Sek110237 #endif
18301484Sek110237 
1831789Sahrens 	/*
18325450Sbrendan 	 * An aggressive reclamation will shrink the cache size as well as
18331544Seschrock 	 * reap free buffers from the arc kmem caches.
1834789Sahrens 	 */
1835789Sahrens 	if (strat == ARC_RECLAIM_AGGR)
18363158Smaybee 		arc_shrink();
1837789Sahrens 
1838789Sahrens 	for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
1839789Sahrens 		if (zio_buf_cache[i] != prev_cache) {
1840789Sahrens 			prev_cache = zio_buf_cache[i];
1841789Sahrens 			kmem_cache_reap_now(zio_buf_cache[i]);
1842789Sahrens 		}
18433290Sjohansen 		if (zio_data_buf_cache[i] != prev_data_cache) {
18443290Sjohansen 			prev_data_cache = zio_data_buf_cache[i];
18453290Sjohansen 			kmem_cache_reap_now(zio_data_buf_cache[i]);
18463290Sjohansen 		}
1847789Sahrens 	}
18481544Seschrock 	kmem_cache_reap_now(buf_cache);
18491544Seschrock 	kmem_cache_reap_now(hdr_cache);
1850789Sahrens }
1851789Sahrens 
1852789Sahrens static void
1853789Sahrens arc_reclaim_thread(void)
1854789Sahrens {
1855789Sahrens 	clock_t			growtime = 0;
1856789Sahrens 	arc_reclaim_strategy_t	last_reclaim = ARC_RECLAIM_CONS;
1857789Sahrens 	callb_cpr_t		cpr;
1858789Sahrens 
1859789Sahrens 	CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
1860789Sahrens 
1861789Sahrens 	mutex_enter(&arc_reclaim_thr_lock);
1862789Sahrens 	while (arc_thread_exit == 0) {
1863789Sahrens 		if (arc_reclaim_needed()) {
1864789Sahrens 
18653403Sbmc 			if (arc_no_grow) {
1866789Sahrens 				if (last_reclaim == ARC_RECLAIM_CONS) {
1867789Sahrens 					last_reclaim = ARC_RECLAIM_AGGR;
1868789Sahrens 				} else {
1869789Sahrens 					last_reclaim = ARC_RECLAIM_CONS;
1870789Sahrens 				}
1871789Sahrens 			} else {
18723403Sbmc 				arc_no_grow = TRUE;
1873789Sahrens 				last_reclaim = ARC_RECLAIM_AGGR;
1874789Sahrens 				membar_producer();
1875789Sahrens 			}
1876789Sahrens 
1877789Sahrens 			/* reset the growth delay for every reclaim */
1878789Sahrens 			growtime = lbolt + (arc_grow_retry * hz);
1879789Sahrens 
1880789Sahrens 			arc_kmem_reap_now(last_reclaim);
1881789Sahrens 
18824309Smaybee 		} else if (arc_no_grow && lbolt >= growtime) {
18833403Sbmc 			arc_no_grow = FALSE;
1884789Sahrens 		}
1885789Sahrens 
18863403Sbmc 		if (2 * arc_c < arc_size +
18873403Sbmc 		    arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size)
18883298Smaybee 			arc_adjust();
18893298Smaybee 
18901544Seschrock 		if (arc_eviction_list != NULL)
18911544Seschrock 			arc_do_user_evicts();
18921544Seschrock 
1893789Sahrens 		/* block until needed, or one second, whichever is shorter */
1894789Sahrens 		CALLB_CPR_SAFE_BEGIN(&cpr);
1895789Sahrens 		(void) cv_timedwait(&arc_reclaim_thr_cv,
1896789Sahrens 		    &arc_reclaim_thr_lock, (lbolt + hz));
1897789Sahrens 		CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
1898789Sahrens 	}
1899789Sahrens 
1900789Sahrens 	arc_thread_exit = 0;
1901789Sahrens 	cv_broadcast(&arc_reclaim_thr_cv);
1902789Sahrens 	CALLB_CPR_EXIT(&cpr);		/* drops arc_reclaim_thr_lock */
1903789Sahrens 	thread_exit();
1904789Sahrens }
1905789Sahrens 
19061544Seschrock /*
19071544Seschrock  * Adapt arc info given the number of bytes we are trying to add and
19081544Seschrock  * the state that we are comming from.  This function is only called
19091544Seschrock  * when we are adding new content to the cache.
19101544Seschrock  */
1911789Sahrens static void
19121544Seschrock arc_adapt(int bytes, arc_state_t *state)
1913789Sahrens {
19141544Seschrock 	int mult;
19151544Seschrock 
19165450Sbrendan 	if (state == arc_l2c_only)
19175450Sbrendan 		return;
19185450Sbrendan 
19191544Seschrock 	ASSERT(bytes > 0);
1920789Sahrens 	/*
19211544Seschrock 	 * Adapt the target size of the MRU list:
19221544Seschrock 	 *	- if we just hit in the MRU ghost list, then increase
19231544Seschrock 	 *	  the target size of the MRU list.
19241544Seschrock 	 *	- if we just hit in the MFU ghost list, then increase
19251544Seschrock 	 *	  the target size of the MFU list by decreasing the
19261544Seschrock 	 *	  target size of the MRU list.
1927789Sahrens 	 */
19283403Sbmc 	if (state == arc_mru_ghost) {
19293403Sbmc 		mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
19303403Sbmc 		    1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
19311544Seschrock 
19323403Sbmc 		arc_p = MIN(arc_c, arc_p + bytes * mult);
19333403Sbmc 	} else if (state == arc_mfu_ghost) {
19343403Sbmc 		mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
19353403Sbmc 		    1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
19361544Seschrock 
19373403Sbmc 		arc_p = MAX(0, (int64_t)arc_p - bytes * mult);
19381544Seschrock 	}
19393403Sbmc 	ASSERT((int64_t)arc_p >= 0);
1940789Sahrens 
1941789Sahrens 	if (arc_reclaim_needed()) {
1942789Sahrens 		cv_signal(&arc_reclaim_thr_cv);
1943789Sahrens 		return;
1944789Sahrens 	}
1945789Sahrens 
19463403Sbmc 	if (arc_no_grow)
1947789Sahrens 		return;
1948789Sahrens 
19493403Sbmc 	if (arc_c >= arc_c_max)
19501544Seschrock 		return;
19511544Seschrock 
1952789Sahrens 	/*
19531544Seschrock 	 * If we're within (2 * maxblocksize) bytes of the target
19541544Seschrock 	 * cache size, increment the target cache size
1955789Sahrens 	 */
19563403Sbmc 	if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
19573403Sbmc 		atomic_add_64(&arc_c, (int64_t)bytes);
19583403Sbmc 		if (arc_c > arc_c_max)
19593403Sbmc 			arc_c = arc_c_max;
19603403Sbmc 		else if (state == arc_anon)
19613403Sbmc 			atomic_add_64(&arc_p, (int64_t)bytes);
19623403Sbmc 		if (arc_p > arc_c)
19633403Sbmc 			arc_p = arc_c;
1964789Sahrens 	}
19653403Sbmc 	ASSERT((int64_t)arc_p >= 0);
1966789Sahrens }
1967789Sahrens 
1968789Sahrens /*
19691544Seschrock  * Check if the cache has reached its limits and eviction is required
19701544Seschrock  * prior to insert.
1971789Sahrens  */
1972789Sahrens static int
19734309Smaybee arc_evict_needed(arc_buf_contents_t type)
1974789Sahrens {
19754309Smaybee 	if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
19764309Smaybee 		return (1);
19774309Smaybee 
19784309Smaybee #ifdef _KERNEL
19794309Smaybee 	/*
19804309Smaybee 	 * If zio data pages are being allocated out of a separate heap segment,
19814309Smaybee 	 * then enforce that the size of available vmem for this area remains
19824309Smaybee 	 * above about 1/32nd free.
19834309Smaybee 	 */
19844309Smaybee 	if (type == ARC_BUFC_DATA && zio_arena != NULL &&
19854309Smaybee 	    vmem_size(zio_arena, VMEM_FREE) <
19864309Smaybee 	    (vmem_size(zio_arena, VMEM_ALLOC) >> 5))
19874309Smaybee 		return (1);
19884309Smaybee #endif
19894309Smaybee 
1990789Sahrens 	if (arc_reclaim_needed())
1991789Sahrens 		return (1);
1992789Sahrens 
19933403Sbmc 	return (arc_size > arc_c);
1994789Sahrens }
1995789Sahrens 
1996789Sahrens /*
19972688Smaybee  * The buffer, supplied as the first argument, needs a data block.
19982688Smaybee  * So, if we are at cache max, determine which cache should be victimized.
19992688Smaybee  * We have the following cases:
2000789Sahrens  *
20013403Sbmc  * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2002789Sahrens  * In this situation if we're out of space, but the resident size of the MFU is
2003789Sahrens  * under the limit, victimize the MFU cache to satisfy this insertion request.
2004789Sahrens  *
20053403Sbmc  * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2006789Sahrens  * Here, we've used up all of the available space for the MRU, so we need to
2007789Sahrens  * evict from our own cache instead.  Evict from the set of resident MRU
2008789Sahrens  * entries.
2009789Sahrens  *
20103403Sbmc  * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2011789Sahrens  * c minus p represents the MFU space in the cache, since p is the size of the
2012789Sahrens  * cache that is dedicated to the MRU.  In this situation there's still space on
2013789Sahrens  * the MFU side, so the MRU side needs to be victimized.
2014789Sahrens  *
20153403Sbmc  * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2016789Sahrens  * MFU's resident set is consuming more space than it has been allotted.  In
2017789Sahrens  * this situation, we must victimize our own cache, the MFU, for this insertion.
2018789Sahrens  */
2019789Sahrens static void
20202688Smaybee arc_get_data_buf(arc_buf_t *buf)
2021789Sahrens {
20223290Sjohansen 	arc_state_t		*state = buf->b_hdr->b_state;
20233290Sjohansen 	uint64_t		size = buf->b_hdr->b_size;
20243290Sjohansen 	arc_buf_contents_t	type = buf->b_hdr->b_type;
20252688Smaybee 
20262688Smaybee 	arc_adapt(size, state);
2027789Sahrens 
20282688Smaybee 	/*
20292688Smaybee 	 * We have not yet reached cache maximum size,
20302688Smaybee 	 * just allocate a new buffer.
20312688Smaybee 	 */
20324309Smaybee 	if (!arc_evict_needed(type)) {
20333290Sjohansen 		if (type == ARC_BUFC_METADATA) {
20343290Sjohansen 			buf->b_data = zio_buf_alloc(size);
20354309Smaybee 			arc_space_consume(size);
20363290Sjohansen 		} else {
20373290Sjohansen 			ASSERT(type == ARC_BUFC_DATA);
20383290Sjohansen 			buf->b_data = zio_data_buf_alloc(size);
20394309Smaybee 			atomic_add_64(&arc_size, size);
20403290Sjohansen 		}
20412688Smaybee 		goto out;
20422688Smaybee 	}
20432688Smaybee 
20442688Smaybee 	/*
20452688Smaybee 	 * If we are prefetching from the mfu ghost list, this buffer
20462688Smaybee 	 * will end up on the mru list; so steal space from there.
20472688Smaybee 	 */
20483403Sbmc 	if (state == arc_mfu_ghost)
20493403Sbmc 		state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
20503403Sbmc 	else if (state == arc_mru_ghost)
20513403Sbmc 		state = arc_mru;
2052789Sahrens 
20533403Sbmc 	if (state == arc_mru || state == arc_anon) {
20543403Sbmc 		uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
20554309Smaybee 		state = (arc_mfu->arcs_lsize[type] > 0 &&
20564309Smaybee 		    arc_p > mru_used) ? arc_mfu : arc_mru;
2057789Sahrens 	} else {
20582688Smaybee 		/* MFU cases */
20593403Sbmc 		uint64_t mfu_space = arc_c - arc_p;
20604309Smaybee 		state =  (arc_mru->arcs_lsize[type] > 0 &&
20614309Smaybee 		    mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
20622688Smaybee 	}
20635642Smaybee 	if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) {
20643290Sjohansen 		if (type == ARC_BUFC_METADATA) {
20653290Sjohansen 			buf->b_data = zio_buf_alloc(size);
20664309Smaybee 			arc_space_consume(size);
20673290Sjohansen 		} else {
20683290Sjohansen 			ASSERT(type == ARC_BUFC_DATA);
20693290Sjohansen 			buf->b_data = zio_data_buf_alloc(size);
20704309Smaybee 			atomic_add_64(&arc_size, size);
20713290Sjohansen 		}
20723403Sbmc 		ARCSTAT_BUMP(arcstat_recycle_miss);
20732688Smaybee 	}
20742688Smaybee 	ASSERT(buf->b_data != NULL);
20752688Smaybee out:
20762688Smaybee 	/*
20772688Smaybee 	 * Update the state size.  Note that ghost states have a
20782688Smaybee 	 * "ghost size" and so don't need to be updated.
20792688Smaybee 	 */
20802688Smaybee 	if (!GHOST_STATE(buf->b_hdr->b_state)) {
20812688Smaybee 		arc_buf_hdr_t *hdr = buf->b_hdr;
20822688Smaybee 
20833403Sbmc 		atomic_add_64(&hdr->b_state->arcs_size, size);
20842688Smaybee 		if (list_link_active(&hdr->b_arc_node)) {
20852688Smaybee 			ASSERT(refcount_is_zero(&hdr->b_refcnt));
20864309Smaybee 			atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2087789Sahrens 		}
20883298Smaybee 		/*
20893298Smaybee 		 * If we are growing the cache, and we are adding anonymous
20903403Sbmc 		 * data, and we have outgrown arc_p, update arc_p
20913298Smaybee 		 */
20923403Sbmc 		if (arc_size < arc_c && hdr->b_state == arc_anon &&
20933403Sbmc 		    arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
20943403Sbmc 			arc_p = MIN(arc_c, arc_p + size);
2095789Sahrens 	}
2096789Sahrens }
2097789Sahrens 
2098789Sahrens /*
2099789Sahrens  * This routine is called whenever a buffer is accessed.
21001544Seschrock  * NOTE: the hash lock is dropped in this function.
2101789Sahrens  */
2102789Sahrens static void
21032688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2104789Sahrens {
2105789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
2106789Sahrens 
21073403Sbmc 	if (buf->b_state == arc_anon) {
2108789Sahrens 		/*
2109789Sahrens 		 * This buffer is not in the cache, and does not
2110789Sahrens 		 * appear in our "ghost" list.  Add the new buffer
2111789Sahrens 		 * to the MRU state.
2112789Sahrens 		 */
2113789Sahrens 
2114789Sahrens 		ASSERT(buf->b_arc_access == 0);
2115789Sahrens 		buf->b_arc_access = lbolt;
21161544Seschrock 		DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
21173403Sbmc 		arc_change_state(arc_mru, buf, hash_lock);
2118789Sahrens 
21193403Sbmc 	} else if (buf->b_state == arc_mru) {
2120789Sahrens 		/*
21212391Smaybee 		 * If this buffer is here because of a prefetch, then either:
21222391Smaybee 		 * - clear the flag if this is a "referencing" read
21232391Smaybee 		 *   (any subsequent access will bump this into the MFU state).
21242391Smaybee 		 * or
21252391Smaybee 		 * - move the buffer to the head of the list if this is
21262391Smaybee 		 *   another prefetch (to make it less likely to be evicted).
2127789Sahrens 		 */
2128789Sahrens 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
21292391Smaybee 			if (refcount_count(&buf->b_refcnt) == 0) {
21302391Smaybee 				ASSERT(list_link_active(&buf->b_arc_node));
21312391Smaybee 			} else {
21322391Smaybee 				buf->b_flags &= ~ARC_PREFETCH;
21333403Sbmc 				ARCSTAT_BUMP(arcstat_mru_hits);
21342391Smaybee 			}
21352391Smaybee 			buf->b_arc_access = lbolt;
2136789Sahrens 			return;
2137789Sahrens 		}
2138789Sahrens 
2139789Sahrens 		/*
2140789Sahrens 		 * This buffer has been "accessed" only once so far,
2141789Sahrens 		 * but it is still in the cache. Move it to the MFU
2142789Sahrens 		 * state.
2143789Sahrens 		 */
2144789Sahrens 		if (lbolt > buf->b_arc_access + ARC_MINTIME) {
2145789Sahrens 			/*
2146789Sahrens 			 * More than 125ms have passed since we
2147789Sahrens 			 * instantiated this buffer.  Move it to the
2148789Sahrens 			 * most frequently used state.
2149789Sahrens 			 */
2150789Sahrens 			buf->b_arc_access = lbolt;
21511544Seschrock 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
21523403Sbmc 			arc_change_state(arc_mfu, buf, hash_lock);
2153789Sahrens 		}
21543403Sbmc 		ARCSTAT_BUMP(arcstat_mru_hits);
21553403Sbmc 	} else if (buf->b_state == arc_mru_ghost) {
2156789Sahrens 		arc_state_t	*new_state;
2157789Sahrens 		/*
2158789Sahrens 		 * This buffer has been "accessed" recently, but
2159789Sahrens 		 * was evicted from the cache.  Move it to the
2160789Sahrens 		 * MFU state.
2161789Sahrens 		 */
2162789Sahrens 
2163789Sahrens 		if (buf->b_flags & ARC_PREFETCH) {
21643403Sbmc 			new_state = arc_mru;
21652391Smaybee 			if (refcount_count(&buf->b_refcnt) > 0)
21662391Smaybee 				buf->b_flags &= ~ARC_PREFETCH;
21671544Seschrock 			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2168789Sahrens 		} else {
21693403Sbmc 			new_state = arc_mfu;
21701544Seschrock 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2171789Sahrens 		}
2172789Sahrens 
2173789Sahrens 		buf->b_arc_access = lbolt;
2174789Sahrens 		arc_change_state(new_state, buf, hash_lock);
2175789Sahrens 
21763403Sbmc 		ARCSTAT_BUMP(arcstat_mru_ghost_hits);
21773403Sbmc 	} else if (buf->b_state == arc_mfu) {
2178789Sahrens 		/*
2179789Sahrens 		 * This buffer has been accessed more than once and is
2180789Sahrens 		 * still in the cache.  Keep it in the MFU state.
2181789Sahrens 		 *
21822391Smaybee 		 * NOTE: an add_reference() that occurred when we did
21832391Smaybee 		 * the arc_read() will have kicked this off the list.
21842391Smaybee 		 * If it was a prefetch, we will explicitly move it to
21852391Smaybee 		 * the head of the list now.
2186789Sahrens 		 */
21872391Smaybee 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
21882391Smaybee 			ASSERT(refcount_count(&buf->b_refcnt) == 0);
21892391Smaybee 			ASSERT(list_link_active(&buf->b_arc_node));
21902391Smaybee 		}
21913403Sbmc 		ARCSTAT_BUMP(arcstat_mfu_hits);
21922391Smaybee 		buf->b_arc_access = lbolt;
21933403Sbmc 	} else if (buf->b_state == arc_mfu_ghost) {
21943403Sbmc 		arc_state_t	*new_state = arc_mfu;
2195789Sahrens 		/*
2196789Sahrens 		 * This buffer has been accessed more than once but has
2197789Sahrens 		 * been evicted from the cache.  Move it back to the
2198789Sahrens 		 * MFU state.
2199789Sahrens 		 */
2200789Sahrens 
22012391Smaybee 		if (buf->b_flags & ARC_PREFETCH) {
22022391Smaybee 			/*
22032391Smaybee 			 * This is a prefetch access...
22042391Smaybee 			 * move this block back to the MRU state.
22052391Smaybee 			 */
22062391Smaybee 			ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0);
22073403Sbmc 			new_state = arc_mru;
22082391Smaybee 		}
22092391Smaybee 
2210789Sahrens 		buf->b_arc_access = lbolt;
22111544Seschrock 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
22122391Smaybee 		arc_change_state(new_state, buf, hash_lock);
2213789Sahrens 
22143403Sbmc 		ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
22155450Sbrendan 	} else if (buf->b_state == arc_l2c_only) {
22165450Sbrendan 		/*
22175450Sbrendan 		 * This buffer is on the 2nd Level ARC.
22185450Sbrendan 		 */
22195450Sbrendan 
22205450Sbrendan 		buf->b_arc_access = lbolt;
22215450Sbrendan 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
22225450Sbrendan 		arc_change_state(arc_mfu, buf, hash_lock);
2223789Sahrens 	} else {
2224789Sahrens 		ASSERT(!"invalid arc state");
2225789Sahrens 	}
2226789Sahrens }
2227789Sahrens 
2228789Sahrens /* a generic arc_done_func_t which you can use */
2229789Sahrens /* ARGSUSED */
2230789Sahrens void
2231789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2232789Sahrens {
2233789Sahrens 	bcopy(buf->b_data, arg, buf->b_hdr->b_size);
22341544Seschrock 	VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2235789Sahrens }
2236789Sahrens 
22374309Smaybee /* a generic arc_done_func_t */
2238789Sahrens void
2239789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2240789Sahrens {
2241789Sahrens 	arc_buf_t **bufp = arg;
2242789Sahrens 	if (zio && zio->io_error) {
22431544Seschrock 		VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2244789Sahrens 		*bufp = NULL;
2245789Sahrens 	} else {
2246789Sahrens 		*bufp = buf;
2247789Sahrens 	}
2248789Sahrens }
2249789Sahrens 
2250789Sahrens static void
2251789Sahrens arc_read_done(zio_t *zio)
2252789Sahrens {
22531589Smaybee 	arc_buf_hdr_t	*hdr, *found;
2254789Sahrens 	arc_buf_t	*buf;
2255789Sahrens 	arc_buf_t	*abuf;	/* buffer we're assigning to callback */
2256789Sahrens 	kmutex_t	*hash_lock;
2257789Sahrens 	arc_callback_t	*callback_list, *acb;
2258789Sahrens 	int		freeable = FALSE;
2259789Sahrens 
2260789Sahrens 	buf = zio->io_private;
2261789Sahrens 	hdr = buf->b_hdr;
2262789Sahrens 
22631589Smaybee 	/*
22641589Smaybee 	 * The hdr was inserted into hash-table and removed from lists
22651589Smaybee 	 * prior to starting I/O.  We should find this header, since
22661589Smaybee 	 * it's in the hash table, and it should be legit since it's
22671589Smaybee 	 * not possible to evict it during the I/O.  The only possible
22681589Smaybee 	 * reason for it not to be found is if we were freed during the
22691589Smaybee 	 * read.
22701589Smaybee 	 */
22711589Smaybee 	found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth,
22723093Sahrens 	    &hash_lock);
2273789Sahrens 
22741589Smaybee 	ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
22755450Sbrendan 	    (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
22765450Sbrendan 	    (found == hdr && HDR_L2_READING(hdr)));
22775450Sbrendan 
22785450Sbrendan 	hdr->b_flags &= ~(ARC_L2_READING|ARC_L2_EVICTED);
22795450Sbrendan 	if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
22805450Sbrendan 		hdr->b_flags |= ARC_DONT_L2CACHE;
2281789Sahrens 
2282789Sahrens 	/* byteswap if necessary */
2283789Sahrens 	callback_list = hdr->b_acb;
2284789Sahrens 	ASSERT(callback_list != NULL);
2285789Sahrens 	if (BP_SHOULD_BYTESWAP(zio->io_bp) && callback_list->acb_byteswap)
2286789Sahrens 		callback_list->acb_byteswap(buf->b_data, hdr->b_size);
2287789Sahrens 
22885450Sbrendan 	arc_cksum_compute(buf, B_FALSE);
22893093Sahrens 
2290789Sahrens 	/* create copies of the data buffer for the callers */
2291789Sahrens 	abuf = buf;
2292789Sahrens 	for (acb = callback_list; acb; acb = acb->acb_next) {
2293789Sahrens 		if (acb->acb_done) {
22942688Smaybee 			if (abuf == NULL)
22952688Smaybee 				abuf = arc_buf_clone(buf);
2296789Sahrens 			acb->acb_buf = abuf;
2297789Sahrens 			abuf = NULL;
2298789Sahrens 		}
2299789Sahrens 	}
2300789Sahrens 	hdr->b_acb = NULL;
2301789Sahrens 	hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
23021544Seschrock 	ASSERT(!HDR_BUF_AVAILABLE(hdr));
23031544Seschrock 	if (abuf == buf)
23041544Seschrock 		hdr->b_flags |= ARC_BUF_AVAILABLE;
2305789Sahrens 
2306789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2307789Sahrens 
2308789Sahrens 	if (zio->io_error != 0) {
2309789Sahrens 		hdr->b_flags |= ARC_IO_ERROR;
23103403Sbmc 		if (hdr->b_state != arc_anon)
23113403Sbmc 			arc_change_state(arc_anon, hdr, hash_lock);
23121544Seschrock 		if (HDR_IN_HASH_TABLE(hdr))
23131544Seschrock 			buf_hash_remove(hdr);
2314789Sahrens 		freeable = refcount_is_zero(&hdr->b_refcnt);
23152391Smaybee 		/* convert checksum errors into IO errors */
23161544Seschrock 		if (zio->io_error == ECKSUM)
23171544Seschrock 			zio->io_error = EIO;
2318789Sahrens 	}
2319789Sahrens 
23201544Seschrock 	/*
23212391Smaybee 	 * Broadcast before we drop the hash_lock to avoid the possibility
23222391Smaybee 	 * that the hdr (and hence the cv) might be freed before we get to
23232391Smaybee 	 * the cv_broadcast().
23241544Seschrock 	 */
23251544Seschrock 	cv_broadcast(&hdr->b_cv);
23261544Seschrock 
23271589Smaybee 	if (hash_lock) {
2328789Sahrens 		/*
2329789Sahrens 		 * Only call arc_access on anonymous buffers.  This is because
2330789Sahrens 		 * if we've issued an I/O for an evicted buffer, we've already
2331789Sahrens 		 * called arc_access (to prevent any simultaneous readers from
2332789Sahrens 		 * getting confused).
2333789Sahrens 		 */
23343403Sbmc 		if (zio->io_error == 0 && hdr->b_state == arc_anon)
23352688Smaybee 			arc_access(hdr, hash_lock);
23362688Smaybee 		mutex_exit(hash_lock);
2337789Sahrens 	} else {
2338789Sahrens 		/*
2339789Sahrens 		 * This block was freed while we waited for the read to
2340789Sahrens 		 * complete.  It has been removed from the hash table and
2341789Sahrens 		 * moved to the anonymous state (so that it won't show up
2342789Sahrens 		 * in the cache).
2343789Sahrens 		 */
23443403Sbmc 		ASSERT3P(hdr->b_state, ==, arc_anon);
2345789Sahrens 		freeable = refcount_is_zero(&hdr->b_refcnt);
2346789Sahrens 	}
2347789Sahrens 
2348789Sahrens 	/* execute each callback and free its structure */
2349789Sahrens 	while ((acb = callback_list) != NULL) {
2350789Sahrens 		if (acb->acb_done)
2351789Sahrens 			acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2352789Sahrens 
2353789Sahrens 		if (acb->acb_zio_dummy != NULL) {
2354789Sahrens 			acb->acb_zio_dummy->io_error = zio->io_error;
2355789Sahrens 			zio_nowait(acb->acb_zio_dummy);
2356789Sahrens 		}
2357789Sahrens 
2358789Sahrens 		callback_list = acb->acb_next;
2359789Sahrens 		kmem_free(acb, sizeof (arc_callback_t));
2360789Sahrens 	}
2361789Sahrens 
2362789Sahrens 	if (freeable)
23631544Seschrock 		arc_hdr_destroy(hdr);
2364789Sahrens }
2365789Sahrens 
2366789Sahrens /*
2367789Sahrens  * "Read" the block block at the specified DVA (in bp) via the
2368789Sahrens  * cache.  If the block is found in the cache, invoke the provided
2369789Sahrens  * callback immediately and return.  Note that the `zio' parameter
2370789Sahrens  * in the callback will be NULL in this case, since no IO was
2371789Sahrens  * required.  If the block is not in the cache pass the read request
2372789Sahrens  * on to the spa with a substitute callback function, so that the
2373789Sahrens  * requested block will be added to the cache.
2374789Sahrens  *
2375789Sahrens  * If a read request arrives for a block that has a read in-progress,
2376789Sahrens  * either wait for the in-progress read to complete (and return the
2377789Sahrens  * results); or, if this is a read with a "done" func, add a record
2378789Sahrens  * to the read to invoke the "done" func when the read completes,
2379789Sahrens  * and return; or just return.
2380789Sahrens  *
2381789Sahrens  * arc_read_done() will invoke all the requested "done" functions
2382789Sahrens  * for readers of this block.
2383789Sahrens  */
2384789Sahrens int
2385789Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_byteswap_func_t *swap,
2386789Sahrens     arc_done_func_t *done, void *private, int priority, int flags,
23872391Smaybee     uint32_t *arc_flags, zbookmark_t *zb)
2388789Sahrens {
2389789Sahrens 	arc_buf_hdr_t *hdr;
2390789Sahrens 	arc_buf_t *buf;
2391789Sahrens 	kmutex_t *hash_lock;
23925450Sbrendan 	zio_t *rzio;
2393789Sahrens 
2394789Sahrens top:
2395789Sahrens 	hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock);
23961544Seschrock 	if (hdr && hdr->b_datacnt > 0) {
2397789Sahrens 
23982391Smaybee 		*arc_flags |= ARC_CACHED;
23992391Smaybee 
2400789Sahrens 		if (HDR_IO_IN_PROGRESS(hdr)) {
24012391Smaybee 
24022391Smaybee 			if (*arc_flags & ARC_WAIT) {
24032391Smaybee 				cv_wait(&hdr->b_cv, hash_lock);
24042391Smaybee 				mutex_exit(hash_lock);
24052391Smaybee 				goto top;
24062391Smaybee 			}
24072391Smaybee 			ASSERT(*arc_flags & ARC_NOWAIT);
24082391Smaybee 
24092391Smaybee 			if (done) {
2410789Sahrens 				arc_callback_t	*acb = NULL;
2411789Sahrens 
2412789Sahrens 				acb = kmem_zalloc(sizeof (arc_callback_t),
2413789Sahrens 				    KM_SLEEP);
2414789Sahrens 				acb->acb_done = done;
2415789Sahrens 				acb->acb_private = private;
2416789Sahrens 				acb->acb_byteswap = swap;
2417789Sahrens 				if (pio != NULL)
2418789Sahrens 					acb->acb_zio_dummy = zio_null(pio,
2419789Sahrens 					    spa, NULL, NULL, flags);
2420789Sahrens 
2421789Sahrens 				ASSERT(acb->acb_done != NULL);
2422789Sahrens 				acb->acb_next = hdr->b_acb;
2423789Sahrens 				hdr->b_acb = acb;
2424789Sahrens 				add_reference(hdr, hash_lock, private);
2425789Sahrens 				mutex_exit(hash_lock);
2426789Sahrens 				return (0);
2427789Sahrens 			}
2428789Sahrens 			mutex_exit(hash_lock);
2429789Sahrens 			return (0);
2430789Sahrens 		}
2431789Sahrens 
24323403Sbmc 		ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2433789Sahrens 
24341544Seschrock 		if (done) {
24352688Smaybee 			add_reference(hdr, hash_lock, private);
24361544Seschrock 			/*
24371544Seschrock 			 * If this block is already in use, create a new
24381544Seschrock 			 * copy of the data so that we will be guaranteed
24391544Seschrock 			 * that arc_release() will always succeed.
24401544Seschrock 			 */
24411544Seschrock 			buf = hdr->b_buf;
24421544Seschrock 			ASSERT(buf);
24431544Seschrock 			ASSERT(buf->b_data);
24442688Smaybee 			if (HDR_BUF_AVAILABLE(hdr)) {
24451544Seschrock 				ASSERT(buf->b_efunc == NULL);
24461544Seschrock 				hdr->b_flags &= ~ARC_BUF_AVAILABLE;
24472688Smaybee 			} else {
24482688Smaybee 				buf = arc_buf_clone(buf);
24491544Seschrock 			}
24502391Smaybee 		} else if (*arc_flags & ARC_PREFETCH &&
24512391Smaybee 		    refcount_count(&hdr->b_refcnt) == 0) {
24522391Smaybee 			hdr->b_flags |= ARC_PREFETCH;
2453789Sahrens 		}
2454789Sahrens 		DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
24552688Smaybee 		arc_access(hdr, hash_lock);
24562688Smaybee 		mutex_exit(hash_lock);
24573403Sbmc 		ARCSTAT_BUMP(arcstat_hits);
24583403Sbmc 		ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
24593403Sbmc 		    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
24603403Sbmc 		    data, metadata, hits);
24613403Sbmc 
2462789Sahrens 		if (done)
2463789Sahrens 			done(NULL, buf, private);
2464789Sahrens 	} else {
2465789Sahrens 		uint64_t size = BP_GET_LSIZE(bp);
2466789Sahrens 		arc_callback_t	*acb;
2467789Sahrens 
2468789Sahrens 		if (hdr == NULL) {
2469789Sahrens 			/* this block is not in the cache */
2470789Sahrens 			arc_buf_hdr_t	*exists;
24713290Sjohansen 			arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
24723290Sjohansen 			buf = arc_buf_alloc(spa, size, private, type);
2473789Sahrens 			hdr = buf->b_hdr;
2474789Sahrens 			hdr->b_dva = *BP_IDENTITY(bp);
2475789Sahrens 			hdr->b_birth = bp->blk_birth;
2476789Sahrens 			hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
2477789Sahrens 			exists = buf_hash_insert(hdr, &hash_lock);
2478789Sahrens 			if (exists) {
2479789Sahrens 				/* somebody beat us to the hash insert */
2480789Sahrens 				mutex_exit(hash_lock);
2481789Sahrens 				bzero(&hdr->b_dva, sizeof (dva_t));
2482789Sahrens 				hdr->b_birth = 0;
2483789Sahrens 				hdr->b_cksum0 = 0;
24841544Seschrock 				(void) arc_buf_remove_ref(buf, private);
2485789Sahrens 				goto top; /* restart the IO request */
2486789Sahrens 			}
24872391Smaybee 			/* if this is a prefetch, we don't have a reference */
24882391Smaybee 			if (*arc_flags & ARC_PREFETCH) {
24892391Smaybee 				(void) remove_reference(hdr, hash_lock,
24902391Smaybee 				    private);
24912391Smaybee 				hdr->b_flags |= ARC_PREFETCH;
24922391Smaybee 			}
24932391Smaybee 			if (BP_GET_LEVEL(bp) > 0)
24942391Smaybee 				hdr->b_flags |= ARC_INDIRECT;
2495789Sahrens 		} else {
2496789Sahrens 			/* this block is in the ghost cache */
24971544Seschrock 			ASSERT(GHOST_STATE(hdr->b_state));
24981544Seschrock 			ASSERT(!HDR_IO_IN_PROGRESS(hdr));
24992391Smaybee 			ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0);
25002391Smaybee 			ASSERT(hdr->b_buf == NULL);
2501789Sahrens 
25022391Smaybee 			/* if this is a prefetch, we don't have a reference */
25032391Smaybee 			if (*arc_flags & ARC_PREFETCH)
25042391Smaybee 				hdr->b_flags |= ARC_PREFETCH;
25052391Smaybee 			else
25062391Smaybee 				add_reference(hdr, hash_lock, private);
2507789Sahrens 			buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
25081544Seschrock 			buf->b_hdr = hdr;
25092688Smaybee 			buf->b_data = NULL;
25101544Seschrock 			buf->b_efunc = NULL;
25111544Seschrock 			buf->b_private = NULL;
25121544Seschrock 			buf->b_next = NULL;
25131544Seschrock 			hdr->b_buf = buf;
25142688Smaybee 			arc_get_data_buf(buf);
25151544Seschrock 			ASSERT(hdr->b_datacnt == 0);
25161544Seschrock 			hdr->b_datacnt = 1;
25172391Smaybee 
2518789Sahrens 		}
2519789Sahrens 
2520789Sahrens 		acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
2521789Sahrens 		acb->acb_done = done;
2522789Sahrens 		acb->acb_private = private;
2523789Sahrens 		acb->acb_byteswap = swap;
2524789Sahrens 
2525789Sahrens 		ASSERT(hdr->b_acb == NULL);
2526789Sahrens 		hdr->b_acb = acb;
2527789Sahrens 		hdr->b_flags |= ARC_IO_IN_PROGRESS;
2528789Sahrens 
2529789Sahrens 		/*
2530789Sahrens 		 * If the buffer has been evicted, migrate it to a present state
2531789Sahrens 		 * before issuing the I/O.  Once we drop the hash-table lock,
2532789Sahrens 		 * the header will be marked as I/O in progress and have an
2533789Sahrens 		 * attached buffer.  At this point, anybody who finds this
2534789Sahrens 		 * buffer ought to notice that it's legit but has a pending I/O.
2535789Sahrens 		 */
2536789Sahrens 
25371544Seschrock 		if (GHOST_STATE(hdr->b_state))
25382688Smaybee 			arc_access(hdr, hash_lock);
2539789Sahrens 
2540789Sahrens 		ASSERT3U(hdr->b_size, ==, size);
25411596Sahrens 		DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size,
25421596Sahrens 		    zbookmark_t *, zb);
25433403Sbmc 		ARCSTAT_BUMP(arcstat_misses);
25443403Sbmc 		ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
25453403Sbmc 		    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
25463403Sbmc 		    data, metadata, misses);
25471544Seschrock 
25485450Sbrendan 		if (l2arc_ndev != 0) {
25495450Sbrendan 			/*
25505450Sbrendan 			 * Read from the L2ARC if the following are true:
25515450Sbrendan 			 * 1. This buffer has L2ARC metadata.
25525450Sbrendan 			 * 2. This buffer isn't currently writing to the L2ARC.
25535450Sbrendan 			 */
25545450Sbrendan 			if (hdr->b_l2hdr != NULL && !HDR_L2_WRITING(hdr)) {
25555450Sbrendan 				vdev_t *vd = hdr->b_l2hdr->b_dev->l2ad_vdev;
25565450Sbrendan 				daddr_t addr = hdr->b_l2hdr->b_daddr;
25575450Sbrendan 				l2arc_read_callback_t *cb;
25585450Sbrendan 
25595450Sbrendan 				DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
25605450Sbrendan 				ARCSTAT_BUMP(arcstat_l2_hits);
25615450Sbrendan 
25625450Sbrendan 				hdr->b_flags |= ARC_L2_READING;
25635450Sbrendan 				mutex_exit(hash_lock);
25645450Sbrendan 
25655450Sbrendan 				cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
25665450Sbrendan 				    KM_SLEEP);
25675450Sbrendan 				cb->l2rcb_buf = buf;
25685450Sbrendan 				cb->l2rcb_spa = spa;
25695450Sbrendan 				cb->l2rcb_bp = *bp;
25705450Sbrendan 				cb->l2rcb_zb = *zb;
25715450Sbrendan 				cb->l2rcb_flags = flags;
25725450Sbrendan 
25735450Sbrendan 				/*
25745450Sbrendan 				 * l2arc read.
25755450Sbrendan 				 */
25765450Sbrendan 				rzio = zio_read_phys(pio, vd, addr, size,
25775450Sbrendan 				    buf->b_data, ZIO_CHECKSUM_OFF,
25785450Sbrendan 				    l2arc_read_done, cb, priority,
25795450Sbrendan 				    flags | ZIO_FLAG_DONT_CACHE, B_FALSE);
25805450Sbrendan 				DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
25815450Sbrendan 				    zio_t *, rzio);
25825450Sbrendan 
25835450Sbrendan 				if (*arc_flags & ARC_WAIT)
25845450Sbrendan 					return (zio_wait(rzio));
25855450Sbrendan 
25865450Sbrendan 				ASSERT(*arc_flags & ARC_NOWAIT);
25875450Sbrendan 				zio_nowait(rzio);
25885450Sbrendan 				return (0);
25895450Sbrendan 			} else {
25905450Sbrendan 				DTRACE_PROBE1(l2arc__miss,
25915450Sbrendan 				    arc_buf_hdr_t *, hdr);
25925450Sbrendan 				ARCSTAT_BUMP(arcstat_l2_misses);
25935450Sbrendan 				if (HDR_L2_WRITING(hdr))
25945450Sbrendan 					ARCSTAT_BUMP(arcstat_l2_rw_clash);
25955450Sbrendan 			}
25965450Sbrendan 		}
25975450Sbrendan 		mutex_exit(hash_lock);
25985450Sbrendan 
2599789Sahrens 		rzio = zio_read(pio, spa, bp, buf->b_data, size,
26001544Seschrock 		    arc_read_done, buf, priority, flags, zb);
2601789Sahrens 
26022391Smaybee 		if (*arc_flags & ARC_WAIT)
2603789Sahrens 			return (zio_wait(rzio));
2604789Sahrens 
26052391Smaybee 		ASSERT(*arc_flags & ARC_NOWAIT);
2606789Sahrens 		zio_nowait(rzio);
2607789Sahrens 	}
2608789Sahrens 	return (0);
2609789Sahrens }
2610789Sahrens 
2611789Sahrens /*
2612789Sahrens  * arc_read() variant to support pool traversal.  If the block is already
2613789Sahrens  * in the ARC, make a copy of it; otherwise, the caller will do the I/O.
2614789Sahrens  * The idea is that we don't want pool traversal filling up memory, but
2615789Sahrens  * if the ARC already has the data anyway, we shouldn't pay for the I/O.
2616789Sahrens  */
2617789Sahrens int
2618789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data)
2619789Sahrens {
2620789Sahrens 	arc_buf_hdr_t *hdr;
2621789Sahrens 	kmutex_t *hash_mtx;
2622789Sahrens 	int rc = 0;
2623789Sahrens 
2624789Sahrens 	hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx);
2625789Sahrens 
26261544Seschrock 	if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) {
26271544Seschrock 		arc_buf_t *buf = hdr->b_buf;
26281544Seschrock 
26291544Seschrock 		ASSERT(buf);
26301544Seschrock 		while (buf->b_data == NULL) {
26311544Seschrock 			buf = buf->b_next;
26321544Seschrock 			ASSERT(buf);
26331544Seschrock 		}
26341544Seschrock 		bcopy(buf->b_data, data, hdr->b_size);
26351544Seschrock 	} else {
2636789Sahrens 		rc = ENOENT;
26371544Seschrock 	}
2638789Sahrens 
2639789Sahrens 	if (hash_mtx)
2640789Sahrens 		mutex_exit(hash_mtx);
2641789Sahrens 
2642789Sahrens 	return (rc);
2643789Sahrens }
2644789Sahrens 
26451544Seschrock void
26461544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
26471544Seschrock {
26481544Seschrock 	ASSERT(buf->b_hdr != NULL);
26493403Sbmc 	ASSERT(buf->b_hdr->b_state != arc_anon);
26501544Seschrock 	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
26511544Seschrock 	buf->b_efunc = func;
26521544Seschrock 	buf->b_private = private;
26531544Seschrock }
26541544Seschrock 
26551544Seschrock /*
26561544Seschrock  * This is used by the DMU to let the ARC know that a buffer is
26571544Seschrock  * being evicted, so the ARC should clean up.  If this arc buf
26581544Seschrock  * is not yet in the evicted state, it will be put there.
26591544Seschrock  */
26601544Seschrock int
26611544Seschrock arc_buf_evict(arc_buf_t *buf)
26621544Seschrock {
26632887Smaybee 	arc_buf_hdr_t *hdr;
26641544Seschrock 	kmutex_t *hash_lock;
26651544Seschrock 	arc_buf_t **bufp;
26661544Seschrock 
26672887Smaybee 	mutex_enter(&arc_eviction_mtx);
26682887Smaybee 	hdr = buf->b_hdr;
26691544Seschrock 	if (hdr == NULL) {
26701544Seschrock 		/*
26711544Seschrock 		 * We are in arc_do_user_evicts().
26721544Seschrock 		 */
26731544Seschrock 		ASSERT(buf->b_data == NULL);
26742887Smaybee 		mutex_exit(&arc_eviction_mtx);
26751544Seschrock 		return (0);
26761544Seschrock 	}
26772887Smaybee 	hash_lock = HDR_LOCK(hdr);
26782887Smaybee 	mutex_exit(&arc_eviction_mtx);
26791544Seschrock 
26801544Seschrock 	mutex_enter(hash_lock);
26811544Seschrock 
26822724Smaybee 	if (buf->b_data == NULL) {
26832724Smaybee 		/*
26842724Smaybee 		 * We are on the eviction list.
26852724Smaybee 		 */
26862724Smaybee 		mutex_exit(hash_lock);
26872724Smaybee 		mutex_enter(&arc_eviction_mtx);
26882724Smaybee 		if (buf->b_hdr == NULL) {
26892724Smaybee 			/*
26902724Smaybee 			 * We are already in arc_do_user_evicts().
26912724Smaybee 			 */
26922724Smaybee 			mutex_exit(&arc_eviction_mtx);
26932724Smaybee 			return (0);
26942724Smaybee 		} else {
26952724Smaybee 			arc_buf_t copy = *buf; /* structure assignment */
26962724Smaybee 			/*
26972724Smaybee 			 * Process this buffer now
26982724Smaybee 			 * but let arc_do_user_evicts() do the reaping.
26992724Smaybee 			 */
27002724Smaybee 			buf->b_efunc = NULL;
27012724Smaybee 			mutex_exit(&arc_eviction_mtx);
27022724Smaybee 			VERIFY(copy.b_efunc(&copy) == 0);
27032724Smaybee 			return (1);
27042724Smaybee 		}
27052724Smaybee 	}
27062724Smaybee 
27072724Smaybee 	ASSERT(buf->b_hdr == hdr);
27082724Smaybee 	ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
27093403Sbmc 	ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
27101544Seschrock 
27111544Seschrock 	/*
27121544Seschrock 	 * Pull this buffer off of the hdr
27131544Seschrock 	 */
27141544Seschrock 	bufp = &hdr->b_buf;
27151544Seschrock 	while (*bufp != buf)
27161544Seschrock 		bufp = &(*bufp)->b_next;
27171544Seschrock 	*bufp = buf->b_next;
27181544Seschrock 
27191544Seschrock 	ASSERT(buf->b_data != NULL);
27202688Smaybee 	arc_buf_destroy(buf, FALSE, FALSE);
27211544Seschrock 
27221544Seschrock 	if (hdr->b_datacnt == 0) {
27231544Seschrock 		arc_state_t *old_state = hdr->b_state;
27241544Seschrock 		arc_state_t *evicted_state;
27251544Seschrock 
27261544Seschrock 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
27271544Seschrock 
27281544Seschrock 		evicted_state =
27293403Sbmc 		    (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
27301544Seschrock 
27313403Sbmc 		mutex_enter(&old_state->arcs_mtx);
27323403Sbmc 		mutex_enter(&evicted_state->arcs_mtx);
27331544Seschrock 
27341544Seschrock 		arc_change_state(evicted_state, hdr, hash_lock);
27351544Seschrock 		ASSERT(HDR_IN_HASH_TABLE(hdr));
27365450Sbrendan 		hdr->b_flags |= ARC_IN_HASH_TABLE;
27375450Sbrendan 		hdr->b_flags &= ~ARC_BUF_AVAILABLE;
27381544Seschrock 
27393403Sbmc 		mutex_exit(&evicted_state->arcs_mtx);
27403403Sbmc 		mutex_exit(&old_state->arcs_mtx);
27411544Seschrock 	}
27421544Seschrock 	mutex_exit(hash_lock);
27431819Smaybee 
27441544Seschrock 	VERIFY(buf->b_efunc(buf) == 0);
27451544Seschrock 	buf->b_efunc = NULL;
27461544Seschrock 	buf->b_private = NULL;
27471544Seschrock 	buf->b_hdr = NULL;
27481544Seschrock 	kmem_cache_free(buf_cache, buf);
27491544Seschrock 	return (1);
27501544Seschrock }
27511544Seschrock 
2752789Sahrens /*
2753789Sahrens  * Release this buffer from the cache.  This must be done
2754789Sahrens  * after a read and prior to modifying the buffer contents.
2755789Sahrens  * If the buffer has more than one reference, we must make
2756789Sahrens  * make a new hdr for the buffer.
2757789Sahrens  */
2758789Sahrens void
2759789Sahrens arc_release(arc_buf_t *buf, void *tag)
2760789Sahrens {
2761789Sahrens 	arc_buf_hdr_t *hdr = buf->b_hdr;
2762789Sahrens 	kmutex_t *hash_lock = HDR_LOCK(hdr);
27635450Sbrendan 	l2arc_buf_hdr_t *l2hdr = NULL;
27645450Sbrendan 	uint64_t buf_size;
2765789Sahrens 
2766789Sahrens 	/* this buffer is not on any list */
2767789Sahrens 	ASSERT(refcount_count(&hdr->b_refcnt) > 0);
2768789Sahrens 
27693403Sbmc 	if (hdr->b_state == arc_anon) {
2770789Sahrens 		/* this buffer is already released */
2771789Sahrens 		ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1);
2772789Sahrens 		ASSERT(BUF_EMPTY(hdr));
27731544Seschrock 		ASSERT(buf->b_efunc == NULL);
27743093Sahrens 		arc_buf_thaw(buf);
2775789Sahrens 		return;
2776789Sahrens 	}
2777789Sahrens 
2778789Sahrens 	mutex_enter(hash_lock);
2779789Sahrens 
27801544Seschrock 	/*
27811544Seschrock 	 * Do we have more than one buf?
27821544Seschrock 	 */
27831544Seschrock 	if (hdr->b_buf != buf || buf->b_next != NULL) {
2784789Sahrens 		arc_buf_hdr_t *nhdr;
2785789Sahrens 		arc_buf_t **bufp;
2786789Sahrens 		uint64_t blksz = hdr->b_size;
2787789Sahrens 		spa_t *spa = hdr->b_spa;
27883290Sjohansen 		arc_buf_contents_t type = hdr->b_type;
27895450Sbrendan 		uint32_t flags = hdr->b_flags;
2790789Sahrens 
27911544Seschrock 		ASSERT(hdr->b_datacnt > 1);
2792789Sahrens 		/*
2793789Sahrens 		 * Pull the data off of this buf and attach it to
2794789Sahrens 		 * a new anonymous buf.
2795789Sahrens 		 */
27961544Seschrock 		(void) remove_reference(hdr, hash_lock, tag);
2797789Sahrens 		bufp = &hdr->b_buf;
27981544Seschrock 		while (*bufp != buf)
2799789Sahrens 			bufp = &(*bufp)->b_next;
2800789Sahrens 		*bufp = (*bufp)->b_next;
28013897Smaybee 		buf->b_next = NULL;
28021544Seschrock 
28033403Sbmc 		ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
28043403Sbmc 		atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
28051544Seschrock 		if (refcount_is_zero(&hdr->b_refcnt)) {
28064309Smaybee 			uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
28074309Smaybee 			ASSERT3U(*size, >=, hdr->b_size);
28084309Smaybee 			atomic_add_64(size, -hdr->b_size);
28091544Seschrock 		}
28101544Seschrock 		hdr->b_datacnt -= 1;
28115450Sbrendan 		if (hdr->b_l2hdr != NULL) {
28125450Sbrendan 			mutex_enter(&l2arc_buflist_mtx);
28135450Sbrendan 			l2hdr = hdr->b_l2hdr;
28145450Sbrendan 			hdr->b_l2hdr = NULL;
28155450Sbrendan 			buf_size = hdr->b_size;
28165450Sbrendan 		}
28173547Smaybee 		arc_cksum_verify(buf);
28181544Seschrock 
2819789Sahrens 		mutex_exit(hash_lock);
2820789Sahrens 
2821789Sahrens 		nhdr = kmem_cache_alloc(hdr_cache, KM_SLEEP);
2822789Sahrens 		nhdr->b_size = blksz;
2823789Sahrens 		nhdr->b_spa = spa;
28243290Sjohansen 		nhdr->b_type = type;
2825789Sahrens 		nhdr->b_buf = buf;
28263403Sbmc 		nhdr->b_state = arc_anon;
2827789Sahrens 		nhdr->b_arc_access = 0;
28285450Sbrendan 		nhdr->b_flags = flags & ARC_L2_WRITING;
28295450Sbrendan 		nhdr->b_l2hdr = NULL;
28301544Seschrock 		nhdr->b_datacnt = 1;
28313547Smaybee 		nhdr->b_freeze_cksum = NULL;
28323897Smaybee 		(void) refcount_add(&nhdr->b_refcnt, tag);
2833789Sahrens 		buf->b_hdr = nhdr;
28343403Sbmc 		atomic_add_64(&arc_anon->arcs_size, blksz);
2835789Sahrens 	} else {
28361544Seschrock 		ASSERT(refcount_count(&hdr->b_refcnt) == 1);
2837789Sahrens 		ASSERT(!list_link_active(&hdr->b_arc_node));
2838789Sahrens 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
28393403Sbmc 		arc_change_state(arc_anon, hdr, hash_lock);
2840789Sahrens 		hdr->b_arc_access = 0;
28415450Sbrendan 		if (hdr->b_l2hdr != NULL) {
28425450Sbrendan 			mutex_enter(&l2arc_buflist_mtx);
28435450Sbrendan 			l2hdr = hdr->b_l2hdr;
28445450Sbrendan 			hdr->b_l2hdr = NULL;
28455450Sbrendan 			buf_size = hdr->b_size;
28465450Sbrendan 		}
2847789Sahrens 		mutex_exit(hash_lock);
28485450Sbrendan 
2849789Sahrens 		bzero(&hdr->b_dva, sizeof (dva_t));
2850789Sahrens 		hdr->b_birth = 0;
2851789Sahrens 		hdr->b_cksum0 = 0;
28523547Smaybee 		arc_buf_thaw(buf);
2853789Sahrens 	}
28541544Seschrock 	buf->b_efunc = NULL;
28551544Seschrock 	buf->b_private = NULL;
28565450Sbrendan 
28575450Sbrendan 	if (l2hdr) {
28585450Sbrendan 		list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
28595450Sbrendan 		kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
28605450Sbrendan 		ARCSTAT_INCR(arcstat_l2_size, -buf_size);
28615450Sbrendan 	}
28625450Sbrendan 	if (MUTEX_HELD(&l2arc_buflist_mtx))
28635450Sbrendan 		mutex_exit(&l2arc_buflist_mtx);
2864789Sahrens }
2865789Sahrens 
2866789Sahrens int
2867789Sahrens arc_released(arc_buf_t *buf)
2868789Sahrens {
28693403Sbmc 	return (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
28701544Seschrock }
28711544Seschrock 
28721544Seschrock int
28731544Seschrock arc_has_callback(arc_buf_t *buf)
28741544Seschrock {
28751544Seschrock 	return (buf->b_efunc != NULL);
2876789Sahrens }
2877789Sahrens 
28781544Seschrock #ifdef ZFS_DEBUG
28791544Seschrock int
28801544Seschrock arc_referenced(arc_buf_t *buf)
28811544Seschrock {
28821544Seschrock 	return (refcount_count(&buf->b_hdr->b_refcnt));
28831544Seschrock }
28841544Seschrock #endif
28851544Seschrock 
2886789Sahrens static void
28873547Smaybee arc_write_ready(zio_t *zio)
28883547Smaybee {
28893547Smaybee 	arc_write_callback_t *callback = zio->io_private;
28903547Smaybee 	arc_buf_t *buf = callback->awcb_buf;
28915329Sgw25295 	arc_buf_hdr_t *hdr = buf->b_hdr;
28925329Sgw25295 
28935329Sgw25295 	if (zio->io_error == 0 && callback->awcb_ready) {
28943547Smaybee 		ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
28953547Smaybee 		callback->awcb_ready(zio, buf, callback->awcb_private);
28963547Smaybee 	}
28975329Sgw25295 	/*
28985329Sgw25295 	 * If the IO is already in progress, then this is a re-write
28995329Sgw25295 	 * attempt, so we need to thaw and re-compute the cksum. It is
29005329Sgw25295 	 * the responsibility of the callback to handle the freeing
29015329Sgw25295 	 * and accounting for any re-write attempt. If we don't have a
29025329Sgw25295 	 * callback registered then simply free the block here.
29035329Sgw25295 	 */
29045329Sgw25295 	if (HDR_IO_IN_PROGRESS(hdr)) {
29055329Sgw25295 		if (!BP_IS_HOLE(&zio->io_bp_orig) &&
29065329Sgw25295 		    callback->awcb_ready == NULL) {
29075329Sgw25295 			zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg,
29085329Sgw25295 			    &zio->io_bp_orig, NULL, NULL));
29095329Sgw25295 		}
29105329Sgw25295 		mutex_enter(&hdr->b_freeze_lock);
29115329Sgw25295 		if (hdr->b_freeze_cksum != NULL) {
29125329Sgw25295 			kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
29135329Sgw25295 			hdr->b_freeze_cksum = NULL;
29145329Sgw25295 		}
29155329Sgw25295 		mutex_exit(&hdr->b_freeze_lock);
29165329Sgw25295 	}
29175450Sbrendan 	arc_cksum_compute(buf, B_FALSE);
29185329Sgw25295 	hdr->b_flags |= ARC_IO_IN_PROGRESS;
29193547Smaybee }
29203547Smaybee 
29213547Smaybee static void
2922789Sahrens arc_write_done(zio_t *zio)
2923789Sahrens {
29243547Smaybee 	arc_write_callback_t *callback = zio->io_private;
29253547Smaybee 	arc_buf_t *buf = callback->awcb_buf;
29263547Smaybee 	arc_buf_hdr_t *hdr = buf->b_hdr;
2927789Sahrens 
2928789Sahrens 	hdr->b_acb = NULL;
2929789Sahrens 
2930789Sahrens 	/* this buffer is on no lists and is not in the hash table */
29313403Sbmc 	ASSERT3P(hdr->b_state, ==, arc_anon);
2932789Sahrens 
2933789Sahrens 	hdr->b_dva = *BP_IDENTITY(zio->io_bp);
2934789Sahrens 	hdr->b_birth = zio->io_bp->blk_birth;
2935789Sahrens 	hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
29361544Seschrock 	/*
29371544Seschrock 	 * If the block to be written was all-zero, we may have
29381544Seschrock 	 * compressed it away.  In this case no write was performed
29391544Seschrock 	 * so there will be no dva/birth-date/checksum.  The buffer
29401544Seschrock 	 * must therefor remain anonymous (and uncached).
29411544Seschrock 	 */
2942789Sahrens 	if (!BUF_EMPTY(hdr)) {
2943789Sahrens 		arc_buf_hdr_t *exists;
2944789Sahrens 		kmutex_t *hash_lock;
2945789Sahrens 
29463093Sahrens 		arc_cksum_verify(buf);
29473093Sahrens 
2948789Sahrens 		exists = buf_hash_insert(hdr, &hash_lock);
2949789Sahrens 		if (exists) {
2950789Sahrens 			/*
2951789Sahrens 			 * This can only happen if we overwrite for
2952789Sahrens 			 * sync-to-convergence, because we remove
2953789Sahrens 			 * buffers from the hash table when we arc_free().
2954789Sahrens 			 */
2955789Sahrens 			ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig),
2956789Sahrens 			    BP_IDENTITY(zio->io_bp)));
2957789Sahrens 			ASSERT3U(zio->io_bp_orig.blk_birth, ==,
2958789Sahrens 			    zio->io_bp->blk_birth);
2959789Sahrens 
2960789Sahrens 			ASSERT(refcount_is_zero(&exists->b_refcnt));
29613403Sbmc 			arc_change_state(arc_anon, exists, hash_lock);
2962789Sahrens 			mutex_exit(hash_lock);
29631544Seschrock 			arc_hdr_destroy(exists);
2964789Sahrens 			exists = buf_hash_insert(hdr, &hash_lock);
2965789Sahrens 			ASSERT3P(exists, ==, NULL);
2966789Sahrens 		}
29671544Seschrock 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
29682688Smaybee 		arc_access(hdr, hash_lock);
29692688Smaybee 		mutex_exit(hash_lock);
29703547Smaybee 	} else if (callback->awcb_done == NULL) {
29711544Seschrock 		int destroy_hdr;
29721544Seschrock 		/*
29731544Seschrock 		 * This is an anonymous buffer with no user callback,
29741544Seschrock 		 * destroy it if there are no active references.
29751544Seschrock 		 */
29761544Seschrock 		mutex_enter(&arc_eviction_mtx);
29771544Seschrock 		destroy_hdr = refcount_is_zero(&hdr->b_refcnt);
29781544Seschrock 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
29791544Seschrock 		mutex_exit(&arc_eviction_mtx);
29801544Seschrock 		if (destroy_hdr)
29811544Seschrock 			arc_hdr_destroy(hdr);
29821544Seschrock 	} else {
29831544Seschrock 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2984789Sahrens 	}
29851544Seschrock 
29863547Smaybee 	if (callback->awcb_done) {
2987789Sahrens 		ASSERT(!refcount_is_zero(&hdr->b_refcnt));
29883547Smaybee 		callback->awcb_done(zio, buf, callback->awcb_private);
2989789Sahrens 	}
2990789Sahrens 
29913547Smaybee 	kmem_free(callback, sizeof (arc_write_callback_t));
2992789Sahrens }
2993789Sahrens 
29943547Smaybee zio_t *
29951775Sbillm arc_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies,
2996789Sahrens     uint64_t txg, blkptr_t *bp, arc_buf_t *buf,
29973547Smaybee     arc_done_func_t *ready, arc_done_func_t *done, void *private, int priority,
29983547Smaybee     int flags, zbookmark_t *zb)
2999789Sahrens {
3000789Sahrens 	arc_buf_hdr_t *hdr = buf->b_hdr;
30013547Smaybee 	arc_write_callback_t *callback;
30023547Smaybee 	zio_t	*zio;
3003789Sahrens 
3004789Sahrens 	/* this is a private buffer - no locking required */
30053403Sbmc 	ASSERT3P(hdr->b_state, ==, arc_anon);
3006789Sahrens 	ASSERT(BUF_EMPTY(hdr));
3007789Sahrens 	ASSERT(!HDR_IO_ERROR(hdr));
30082237Smaybee 	ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
30092237Smaybee 	ASSERT(hdr->b_acb == 0);
30103547Smaybee 	callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
30113547Smaybee 	callback->awcb_ready = ready;
30123547Smaybee 	callback->awcb_done = done;
30133547Smaybee 	callback->awcb_private = private;
30143547Smaybee 	callback->awcb_buf = buf;
30153547Smaybee 	zio = zio_write(pio, spa, checksum, compress, ncopies, txg, bp,
30163547Smaybee 	    buf->b_data, hdr->b_size, arc_write_ready, arc_write_done, callback,
30173547Smaybee 	    priority, flags, zb);
3018789Sahrens 
30193547Smaybee 	return (zio);
3020789Sahrens }
3021789Sahrens 
3022789Sahrens int
3023789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
3024789Sahrens     zio_done_func_t *done, void *private, uint32_t arc_flags)
3025789Sahrens {
3026789Sahrens 	arc_buf_hdr_t *ab;
3027789Sahrens 	kmutex_t *hash_lock;
3028789Sahrens 	zio_t	*zio;
3029789Sahrens 
3030789Sahrens 	/*
3031789Sahrens 	 * If this buffer is in the cache, release it, so it
3032789Sahrens 	 * can be re-used.
3033789Sahrens 	 */
3034789Sahrens 	ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock);
3035789Sahrens 	if (ab != NULL) {
3036789Sahrens 		/*
3037789Sahrens 		 * The checksum of blocks to free is not always
3038789Sahrens 		 * preserved (eg. on the deadlist).  However, if it is
3039789Sahrens 		 * nonzero, it should match what we have in the cache.
3040789Sahrens 		 */
3041789Sahrens 		ASSERT(bp->blk_cksum.zc_word[0] == 0 ||
3042789Sahrens 		    ab->b_cksum0 == bp->blk_cksum.zc_word[0]);
30433403Sbmc 		if (ab->b_state != arc_anon)
30443403Sbmc 			arc_change_state(arc_anon, ab, hash_lock);
30452391Smaybee 		if (HDR_IO_IN_PROGRESS(ab)) {
30462391Smaybee 			/*
30472391Smaybee 			 * This should only happen when we prefetch.
30482391Smaybee 			 */
30492391Smaybee 			ASSERT(ab->b_flags & ARC_PREFETCH);
30502391Smaybee 			ASSERT3U(ab->b_datacnt, ==, 1);
30512391Smaybee 			ab->b_flags |= ARC_FREED_IN_READ;
30522391Smaybee 			if (HDR_IN_HASH_TABLE(ab))
30532391Smaybee 				buf_hash_remove(ab);
30542391Smaybee 			ab->b_arc_access = 0;
30552391Smaybee 			bzero(&ab->b_dva, sizeof (dva_t));
30562391Smaybee 			ab->b_birth = 0;
30572391Smaybee 			ab->b_cksum0 = 0;
30582391Smaybee 			ab->b_buf->b_efunc = NULL;
30592391Smaybee 			ab->b_buf->b_private = NULL;
30602391Smaybee 			mutex_exit(hash_lock);
30612391Smaybee 		} else if (refcount_is_zero(&ab->b_refcnt)) {
30625450Sbrendan 			ab->b_flags |= ARC_FREE_IN_PROGRESS;
3063789Sahrens 			mutex_exit(hash_lock);
30641544Seschrock 			arc_hdr_destroy(ab);
30653403Sbmc 			ARCSTAT_BUMP(arcstat_deleted);
3066789Sahrens 		} else {
30671589Smaybee 			/*
30682391Smaybee 			 * We still have an active reference on this
30692391Smaybee 			 * buffer.  This can happen, e.g., from
30702391Smaybee 			 * dbuf_unoverride().
30711589Smaybee 			 */
30722391Smaybee 			ASSERT(!HDR_IN_HASH_TABLE(ab));
3073789Sahrens 			ab->b_arc_access = 0;
3074789Sahrens 			bzero(&ab->b_dva, sizeof (dva_t));
3075789Sahrens 			ab->b_birth = 0;
3076789Sahrens 			ab->b_cksum0 = 0;
30771544Seschrock 			ab->b_buf->b_efunc = NULL;
30781544Seschrock 			ab->b_buf->b_private = NULL;
3079789Sahrens 			mutex_exit(hash_lock);
3080789Sahrens 		}
3081789Sahrens 	}
3082789Sahrens 
3083789Sahrens 	zio = zio_free(pio, spa, txg, bp, done, private);
3084789Sahrens 
3085789Sahrens 	if (arc_flags & ARC_WAIT)
3086789Sahrens 		return (zio_wait(zio));
3087789Sahrens 
3088789Sahrens 	ASSERT(arc_flags & ARC_NOWAIT);
3089789Sahrens 	zio_nowait(zio);
3090789Sahrens 
3091789Sahrens 	return (0);
3092789Sahrens }
3093789Sahrens 
3094789Sahrens void
3095789Sahrens arc_tempreserve_clear(uint64_t tempreserve)
3096789Sahrens {
3097789Sahrens 	atomic_add_64(&arc_tempreserve, -tempreserve);
3098789Sahrens 	ASSERT((int64_t)arc_tempreserve >= 0);
3099789Sahrens }
3100789Sahrens 
3101789Sahrens int
3102789Sahrens arc_tempreserve_space(uint64_t tempreserve)
3103789Sahrens {
3104789Sahrens #ifdef ZFS_DEBUG
3105789Sahrens 	/*
3106789Sahrens 	 * Once in a while, fail for no reason.  Everything should cope.
3107789Sahrens 	 */
3108789Sahrens 	if (spa_get_random(10000) == 0) {
3109789Sahrens 		dprintf("forcing random failure\n");
3110789Sahrens 		return (ERESTART);
3111789Sahrens 	}
3112789Sahrens #endif
31133403Sbmc 	if (tempreserve > arc_c/4 && !arc_no_grow)
31143403Sbmc 		arc_c = MIN(arc_c_max, tempreserve * 4);
31153403Sbmc 	if (tempreserve > arc_c)
3116982Smaybee 		return (ENOMEM);
3117982Smaybee 
3118789Sahrens 	/*
3119982Smaybee 	 * Throttle writes when the amount of dirty data in the cache
3120982Smaybee 	 * gets too large.  We try to keep the cache less than half full
3121982Smaybee 	 * of dirty blocks so that our sync times don't grow too large.
3122982Smaybee 	 * Note: if two requests come in concurrently, we might let them
3123982Smaybee 	 * both succeed, when one of them should fail.  Not a huge deal.
3124982Smaybee 	 *
3125982Smaybee 	 * XXX The limit should be adjusted dynamically to keep the time
3126982Smaybee 	 * to sync a dataset fixed (around 1-5 seconds?).
3127789Sahrens 	 */
3128789Sahrens 
31293403Sbmc 	if (tempreserve + arc_tempreserve + arc_anon->arcs_size > arc_c / 2 &&
31303403Sbmc 	    arc_tempreserve + arc_anon->arcs_size > arc_c / 4) {
31314309Smaybee 		dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
31324309Smaybee 		    "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
31334309Smaybee 		    arc_tempreserve>>10,
31344309Smaybee 		    arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
31354309Smaybee 		    arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
31363403Sbmc 		    tempreserve>>10, arc_c>>10);
3137789Sahrens 		return (ERESTART);
3138789Sahrens 	}
3139789Sahrens 	atomic_add_64(&arc_tempreserve, tempreserve);
3140789Sahrens 	return (0);
3141789Sahrens }
3142789Sahrens 
3143789Sahrens void
3144789Sahrens arc_init(void)
3145789Sahrens {
3146789Sahrens 	mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3147789Sahrens 	cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3148789Sahrens 
31492391Smaybee 	/* Convert seconds to clock ticks */
31502638Sperrin 	arc_min_prefetch_lifespan = 1 * hz;
31512391Smaybee 
3152789Sahrens 	/* Start out with 1/8 of all memory */
31533403Sbmc 	arc_c = physmem * PAGESIZE / 8;
3154789Sahrens 
3155789Sahrens #ifdef _KERNEL
3156789Sahrens 	/*
3157789Sahrens 	 * On architectures where the physical memory can be larger
3158789Sahrens 	 * than the addressable space (intel in 32-bit mode), we may
3159789Sahrens 	 * need to limit the cache to 1/8 of VM size.
3160789Sahrens 	 */
31613403Sbmc 	arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3162789Sahrens #endif
3163789Sahrens 
3164982Smaybee 	/* set min cache to 1/32 of all memory, or 64MB, whichever is more */
31653403Sbmc 	arc_c_min = MAX(arc_c / 4, 64<<20);
3166982Smaybee 	/* set max to 3/4 of all memory, or all but 1GB, whichever is more */
31673403Sbmc 	if (arc_c * 8 >= 1<<30)
31683403Sbmc 		arc_c_max = (arc_c * 8) - (1<<30);
3169789Sahrens 	else
31703403Sbmc 		arc_c_max = arc_c_min;
31713403Sbmc 	arc_c_max = MAX(arc_c * 6, arc_c_max);
31722885Sahrens 
31732885Sahrens 	/*
31742885Sahrens 	 * Allow the tunables to override our calculations if they are
31752885Sahrens 	 * reasonable (ie. over 64MB)
31762885Sahrens 	 */
31772885Sahrens 	if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
31783403Sbmc 		arc_c_max = zfs_arc_max;
31793403Sbmc 	if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
31803403Sbmc 		arc_c_min = zfs_arc_min;
31812885Sahrens 
31823403Sbmc 	arc_c = arc_c_max;
31833403Sbmc 	arc_p = (arc_c >> 1);
3184789Sahrens 
31854309Smaybee 	/* limit meta-data to 1/4 of the arc capacity */
31864309Smaybee 	arc_meta_limit = arc_c_max / 4;
31874645Sek110237 
31884645Sek110237 	/* Allow the tunable to override if it is reasonable */
31894645Sek110237 	if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
31904645Sek110237 		arc_meta_limit = zfs_arc_meta_limit;
31914645Sek110237 
31924309Smaybee 	if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
31934309Smaybee 		arc_c_min = arc_meta_limit / 2;
31944309Smaybee 
3195789Sahrens 	/* if kmem_flags are set, lets try to use less memory */
3196789Sahrens 	if (kmem_debugging())
31973403Sbmc 		arc_c = arc_c / 2;
31983403Sbmc 	if (arc_c < arc_c_min)
31993403Sbmc 		arc_c = arc_c_min;
3200789Sahrens 
32013403Sbmc 	arc_anon = &ARC_anon;
32023403Sbmc 	arc_mru = &ARC_mru;
32033403Sbmc 	arc_mru_ghost = &ARC_mru_ghost;
32043403Sbmc 	arc_mfu = &ARC_mfu;
32053403Sbmc 	arc_mfu_ghost = &ARC_mfu_ghost;
32065450Sbrendan 	arc_l2c_only = &ARC_l2c_only;
32073403Sbmc 	arc_size = 0;
3208789Sahrens 
32093403Sbmc 	mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
32103403Sbmc 	mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
32113403Sbmc 	mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
32123403Sbmc 	mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
32133403Sbmc 	mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
32145450Sbrendan 	mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
32152688Smaybee 
32164309Smaybee 	list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
32174309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32184309Smaybee 	list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
32194309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32204309Smaybee 	list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
32214309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32224309Smaybee 	list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
32234309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32244309Smaybee 	list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
32254309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32264309Smaybee 	list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
32274309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32284309Smaybee 	list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
32294309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32304309Smaybee 	list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
32314309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32325450Sbrendan 	list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
32335450Sbrendan 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
32345450Sbrendan 	list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
32355450Sbrendan 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3236789Sahrens 
3237789Sahrens 	buf_init();
3238789Sahrens 
3239789Sahrens 	arc_thread_exit = 0;
32401544Seschrock 	arc_eviction_list = NULL;
32411544Seschrock 	mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
32422887Smaybee 	bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3243789Sahrens 
32443403Sbmc 	arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
32453403Sbmc 	    sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
32463403Sbmc 
32473403Sbmc 	if (arc_ksp != NULL) {
32483403Sbmc 		arc_ksp->ks_data = &arc_stats;
32493403Sbmc 		kstat_install(arc_ksp);
32503403Sbmc 	}
32513403Sbmc 
3252789Sahrens 	(void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
3253789Sahrens 	    TS_RUN, minclsyspri);
32543158Smaybee 
32553158Smaybee 	arc_dead = FALSE;
3256789Sahrens }
3257789Sahrens 
3258789Sahrens void
3259789Sahrens arc_fini(void)
3260789Sahrens {
3261789Sahrens 	mutex_enter(&arc_reclaim_thr_lock);
3262789Sahrens 	arc_thread_exit = 1;
3263789Sahrens 	while (arc_thread_exit != 0)
3264789Sahrens 		cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3265789Sahrens 	mutex_exit(&arc_reclaim_thr_lock);
3266789Sahrens 
32675642Smaybee 	arc_flush(NULL);
3268789Sahrens 
3269789Sahrens 	arc_dead = TRUE;
3270789Sahrens 
32713403Sbmc 	if (arc_ksp != NULL) {
32723403Sbmc 		kstat_delete(arc_ksp);
32733403Sbmc 		arc_ksp = NULL;
32743403Sbmc 	}
32753403Sbmc 
32761544Seschrock 	mutex_destroy(&arc_eviction_mtx);
3277789Sahrens 	mutex_destroy(&arc_reclaim_thr_lock);
3278789Sahrens 	cv_destroy(&arc_reclaim_thr_cv);
3279789Sahrens 
32804309Smaybee 	list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
32814309Smaybee 	list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
32824309Smaybee 	list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
32834309Smaybee 	list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
32844309Smaybee 	list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
32854309Smaybee 	list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
32864309Smaybee 	list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
32874309Smaybee 	list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3288789Sahrens 
32893403Sbmc 	mutex_destroy(&arc_anon->arcs_mtx);
32903403Sbmc 	mutex_destroy(&arc_mru->arcs_mtx);
32913403Sbmc 	mutex_destroy(&arc_mru_ghost->arcs_mtx);
32923403Sbmc 	mutex_destroy(&arc_mfu->arcs_mtx);
32933403Sbmc 	mutex_destroy(&arc_mfu_ghost->arcs_mtx);
32942856Snd150628 
3295789Sahrens 	buf_fini();
3296789Sahrens }
32975450Sbrendan 
32985450Sbrendan /*
32995450Sbrendan  * Level 2 ARC
33005450Sbrendan  *
33015450Sbrendan  * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
33025450Sbrendan  * It uses dedicated storage devices to hold cached data, which are populated
33035450Sbrendan  * using large infrequent writes.  The main role of this cache is to boost
33045450Sbrendan  * the performance of random read workloads.  The intended L2ARC devices
33055450Sbrendan  * include short-stroked disks, solid state disks, and other media with
33065450Sbrendan  * substantially faster read latency than disk.
33075450Sbrendan  *
33085450Sbrendan  *                 +-----------------------+
33095450Sbrendan  *                 |         ARC           |
33105450Sbrendan  *                 +-----------------------+
33115450Sbrendan  *                    |         ^     ^
33125450Sbrendan  *                    |         |     |
33135450Sbrendan  *      l2arc_feed_thread()    arc_read()
33145450Sbrendan  *                    |         |     |
33155450Sbrendan  *                    |  l2arc read   |
33165450Sbrendan  *                    V         |     |
33175450Sbrendan  *               +---------------+    |
33185450Sbrendan  *               |     L2ARC     |    |
33195450Sbrendan  *               +---------------+    |
33205450Sbrendan  *                   |    ^           |
33215450Sbrendan  *          l2arc_write() |           |
33225450Sbrendan  *                   |    |           |
33235450Sbrendan  *                   V    |           |
33245450Sbrendan  *                 +-------+      +-------+
33255450Sbrendan  *                 | vdev  |      | vdev  |
33265450Sbrendan  *                 | cache |      | cache |
33275450Sbrendan  *                 +-------+      +-------+
33285450Sbrendan  *                 +=========+     .-----.
33295450Sbrendan  *                 :  L2ARC  :    |-_____-|
33305450Sbrendan  *                 : devices :    | Disks |
33315450Sbrendan  *                 +=========+    `-_____-'
33325450Sbrendan  *
33335450Sbrendan  * Read requests are satisfied from the following sources, in order:
33345450Sbrendan  *
33355450Sbrendan  *	1) ARC
33365450Sbrendan  *	2) vdev cache of L2ARC devices
33375450Sbrendan  *	3) L2ARC devices
33385450Sbrendan  *	4) vdev cache of disks
33395450Sbrendan  *	5) disks
33405450Sbrendan  *
33415450Sbrendan  * Some L2ARC device types exhibit extremely slow write performance.
33425450Sbrendan  * To accommodate for this there are some significant differences between
33435450Sbrendan  * the L2ARC and traditional cache design:
33445450Sbrendan  *
33455450Sbrendan  * 1. There is no eviction path from the ARC to the L2ARC.  Evictions from
33465450Sbrendan  * the ARC behave as usual, freeing buffers and placing headers on ghost
33475450Sbrendan  * lists.  The ARC does not send buffers to the L2ARC during eviction as
33485450Sbrendan  * this would add inflated write latencies for all ARC memory pressure.
33495450Sbrendan  *
33505450Sbrendan  * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
33515450Sbrendan  * It does this by periodically scanning buffers from the eviction-end of
33525450Sbrendan  * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
33535450Sbrendan  * not already there.  It scans until a headroom of buffers is satisfied,
33545450Sbrendan  * which itself is a buffer for ARC eviction.  The thread that does this is
33555450Sbrendan  * l2arc_feed_thread(), illustrated below; example sizes are included to
33565450Sbrendan  * provide a better sense of ratio than this diagram:
33575450Sbrendan  *
33585450Sbrendan  *	       head -->                        tail
33595450Sbrendan  *	        +---------------------+----------+
33605450Sbrendan  *	ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->.   # already on L2ARC
33615450Sbrendan  *	        +---------------------+----------+   |   o L2ARC eligible
33625450Sbrendan  *	ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->|   : ARC buffer
33635450Sbrendan  *	        +---------------------+----------+   |
33645450Sbrendan  *	             15.9 Gbytes      ^ 32 Mbytes    |
33655450Sbrendan  *	                           headroom          |
33665450Sbrendan  *	                                      l2arc_feed_thread()
33675450Sbrendan  *	                                             |
33685450Sbrendan  *	                 l2arc write hand <--[oooo]--'
33695450Sbrendan  *	                         |           8 Mbyte
33705450Sbrendan  *	                         |          write max
33715450Sbrendan  *	                         V
33725450Sbrendan  *		  +==============================+
33735450Sbrendan  *	L2ARC dev |####|#|###|###|    |####| ... |
33745450Sbrendan  *	          +==============================+
33755450Sbrendan  *	                     32 Gbytes
33765450Sbrendan  *
33775450Sbrendan  * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
33785450Sbrendan  * evicted, then the L2ARC has cached a buffer much sooner than it probably
33795450Sbrendan  * needed to, potentially wasting L2ARC device bandwidth and storage.  It is
33805450Sbrendan  * safe to say that this is an uncommon case, since buffers at the end of
33815450Sbrendan  * the ARC lists have moved there due to inactivity.
33825450Sbrendan  *
33835450Sbrendan  * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
33845450Sbrendan  * then the L2ARC simply misses copying some buffers.  This serves as a
33855450Sbrendan  * pressure valve to prevent heavy read workloads from both stalling the ARC
33865450Sbrendan  * with waits and clogging the L2ARC with writes.  This also helps prevent
33875450Sbrendan  * the potential for the L2ARC to churn if it attempts to cache content too
33885450Sbrendan  * quickly, such as during backups of the entire pool.
33895450Sbrendan  *
33905450Sbrendan  * 5. Writes to the L2ARC devices are grouped and sent in-sequence, so that
33915450Sbrendan  * the vdev queue can aggregate them into larger and fewer writes.  Each
33925450Sbrendan  * device is written to in a rotor fashion, sweeping writes through
33935450Sbrendan  * available space then repeating.
33945450Sbrendan  *
33955450Sbrendan  * 6. The L2ARC does not store dirty content.  It never needs to flush
33965450Sbrendan  * write buffers back to disk based storage.
33975450Sbrendan  *
33985450Sbrendan  * 7. If an ARC buffer is written (and dirtied) which also exists in the
33995450Sbrendan  * L2ARC, the now stale L2ARC buffer is immediately dropped.
34005450Sbrendan  *
34015450Sbrendan  * The performance of the L2ARC can be tweaked by a number of tunables, which
34025450Sbrendan  * may be necessary for different workloads:
34035450Sbrendan  *
34045450Sbrendan  *	l2arc_write_max		max write bytes per interval
34055450Sbrendan  *	l2arc_noprefetch	skip caching prefetched buffers
34065450Sbrendan  *	l2arc_headroom		number of max device writes to precache
34075450Sbrendan  *	l2arc_feed_secs		seconds between L2ARC writing
34085450Sbrendan  *
34095450Sbrendan  * Tunables may be removed or added as future performance improvements are
34105450Sbrendan  * integrated, and also may become zpool properties.
34115450Sbrendan  */
34125450Sbrendan 
34135450Sbrendan static void
34145450Sbrendan l2arc_hdr_stat_add(void)
34155450Sbrendan {
3416*6018Sbrendan 	ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
3417*6018Sbrendan 	ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
34185450Sbrendan }
34195450Sbrendan 
34205450Sbrendan static void
34215450Sbrendan l2arc_hdr_stat_remove(void)
34225450Sbrendan {
3423*6018Sbrendan 	ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
3424*6018Sbrendan 	ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
34255450Sbrendan }
34265450Sbrendan 
34275450Sbrendan /*
34285450Sbrendan  * Cycle through L2ARC devices.  This is how L2ARC load balances.
34295450Sbrendan  * This is called with l2arc_dev_mtx held, which also locks out spa removal.
34305450Sbrendan  */
34315450Sbrendan static l2arc_dev_t *
34325450Sbrendan l2arc_dev_get_next(void)
34335450Sbrendan {
34345450Sbrendan 	l2arc_dev_t *next;
34355450Sbrendan 
34365450Sbrendan 	if (l2arc_dev_last == NULL) {
34375450Sbrendan 		next = list_head(l2arc_dev_list);
34385450Sbrendan 	} else {
34395450Sbrendan 		next = list_next(l2arc_dev_list, l2arc_dev_last);
34405450Sbrendan 		if (next == NULL)
34415450Sbrendan 			next = list_head(l2arc_dev_list);
34425450Sbrendan 	}
34435450Sbrendan 
34445450Sbrendan 	l2arc_dev_last = next;
34455450Sbrendan 
34465450Sbrendan 	return (next);
34475450Sbrendan }
34485450Sbrendan 
34495450Sbrendan /*
34505450Sbrendan  * A write to a cache device has completed.  Update all headers to allow
34515450Sbrendan  * reads from these buffers to begin.
34525450Sbrendan  */
34535450Sbrendan static void
34545450Sbrendan l2arc_write_done(zio_t *zio)
34555450Sbrendan {
34565450Sbrendan 	l2arc_write_callback_t *cb;
34575450Sbrendan 	l2arc_dev_t *dev;
34585450Sbrendan 	list_t *buflist;
34595450Sbrendan 	l2arc_data_free_t *df, *df_prev;
34605450Sbrendan 	arc_buf_hdr_t *head, *ab, *ab_prev;
34615450Sbrendan 	kmutex_t *hash_lock;
34625450Sbrendan 
34635450Sbrendan 	cb = zio->io_private;
34645450Sbrendan 	ASSERT(cb != NULL);
34655450Sbrendan 	dev = cb->l2wcb_dev;
34665450Sbrendan 	ASSERT(dev != NULL);
34675450Sbrendan 	head = cb->l2wcb_head;
34685450Sbrendan 	ASSERT(head != NULL);
34695450Sbrendan 	buflist = dev->l2ad_buflist;
34705450Sbrendan 	ASSERT(buflist != NULL);
34715450Sbrendan 	DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
34725450Sbrendan 	    l2arc_write_callback_t *, cb);
34735450Sbrendan 
34745450Sbrendan 	if (zio->io_error != 0)
34755450Sbrendan 		ARCSTAT_BUMP(arcstat_l2_writes_error);
34765450Sbrendan 
34775450Sbrendan 	mutex_enter(&l2arc_buflist_mtx);
34785450Sbrendan 
34795450Sbrendan 	/*
34805450Sbrendan 	 * All writes completed, or an error was hit.
34815450Sbrendan 	 */
34825450Sbrendan 	for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
34835450Sbrendan 		ab_prev = list_prev(buflist, ab);
34845450Sbrendan 
34855450Sbrendan 		hash_lock = HDR_LOCK(ab);
34865450Sbrendan 		if (!mutex_tryenter(hash_lock)) {
34875450Sbrendan 			/*
34885450Sbrendan 			 * This buffer misses out.  It may be in a stage
34895450Sbrendan 			 * of eviction.  Its ARC_L2_WRITING flag will be
34905450Sbrendan 			 * left set, denying reads to this buffer.
34915450Sbrendan 			 */
34925450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
34935450Sbrendan 			continue;
34945450Sbrendan 		}
34955450Sbrendan 
34965450Sbrendan 		if (zio->io_error != 0) {
34975450Sbrendan 			/*
34985450Sbrendan 			 * Error - invalidate L2ARC entry.
34995450Sbrendan 			 */
35005450Sbrendan 			ab->b_l2hdr = NULL;
35015450Sbrendan 		}
35025450Sbrendan 
35035450Sbrendan 		/*
35045450Sbrendan 		 * Allow ARC to begin reads to this L2ARC entry.
35055450Sbrendan 		 */
35065450Sbrendan 		ab->b_flags &= ~ARC_L2_WRITING;
35075450Sbrendan 
35085450Sbrendan 		mutex_exit(hash_lock);
35095450Sbrendan 	}
35105450Sbrendan 
35115450Sbrendan 	atomic_inc_64(&l2arc_writes_done);
35125450Sbrendan 	list_remove(buflist, head);
35135450Sbrendan 	kmem_cache_free(hdr_cache, head);
35145450Sbrendan 	mutex_exit(&l2arc_buflist_mtx);
35155450Sbrendan 
35165450Sbrendan 	/*
35175450Sbrendan 	 * Free buffers that were tagged for destruction.
35185450Sbrendan 	 */
35195450Sbrendan 	mutex_enter(&l2arc_free_on_write_mtx);
35205450Sbrendan 	buflist = l2arc_free_on_write;
35215450Sbrendan 	for (df = list_tail(buflist); df; df = df_prev) {
35225450Sbrendan 		df_prev = list_prev(buflist, df);
35235450Sbrendan 		ASSERT(df->l2df_data != NULL);
35245450Sbrendan 		ASSERT(df->l2df_func != NULL);
35255450Sbrendan 		df->l2df_func(df->l2df_data, df->l2df_size);
35265450Sbrendan 		list_remove(buflist, df);
35275450Sbrendan 		kmem_free(df, sizeof (l2arc_data_free_t));
35285450Sbrendan 	}
35295450Sbrendan 	mutex_exit(&l2arc_free_on_write_mtx);
35305450Sbrendan 
35315450Sbrendan 	kmem_free(cb, sizeof (l2arc_write_callback_t));
35325450Sbrendan }
35335450Sbrendan 
35345450Sbrendan /*
35355450Sbrendan  * A read to a cache device completed.  Validate buffer contents before
35365450Sbrendan  * handing over to the regular ARC routines.
35375450Sbrendan  */
35385450Sbrendan static void
35395450Sbrendan l2arc_read_done(zio_t *zio)
35405450Sbrendan {
35415450Sbrendan 	l2arc_read_callback_t *cb;
35425450Sbrendan 	arc_buf_hdr_t *hdr;
35435450Sbrendan 	arc_buf_t *buf;
35445450Sbrendan 	zio_t *rzio;
35455450Sbrendan 	kmutex_t *hash_lock;
35465450Sbrendan 	int equal, err = 0;
35475450Sbrendan 
35485450Sbrendan 	cb = zio->io_private;
35495450Sbrendan 	ASSERT(cb != NULL);
35505450Sbrendan 	buf = cb->l2rcb_buf;
35515450Sbrendan 	ASSERT(buf != NULL);
35525450Sbrendan 	hdr = buf->b_hdr;
35535450Sbrendan 	ASSERT(hdr != NULL);
35545450Sbrendan 
35555450Sbrendan 	hash_lock = HDR_LOCK(hdr);
35565450Sbrendan 	mutex_enter(hash_lock);
35575450Sbrendan 
35585450Sbrendan 	/*
35595450Sbrendan 	 * Check this survived the L2ARC journey.
35605450Sbrendan 	 */
35615450Sbrendan 	equal = arc_cksum_equal(buf);
35625450Sbrendan 	if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
35635450Sbrendan 		mutex_exit(hash_lock);
35645450Sbrendan 		zio->io_private = buf;
35655450Sbrendan 		arc_read_done(zio);
35665450Sbrendan 	} else {
35675450Sbrendan 		mutex_exit(hash_lock);
35685450Sbrendan 		/*
35695450Sbrendan 		 * Buffer didn't survive caching.  Increment stats and
35705450Sbrendan 		 * reissue to the original storage device.
35715450Sbrendan 		 */
35725450Sbrendan 		if (zio->io_error != 0)
35735450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_io_error);
35745450Sbrendan 		if (!equal)
35755450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_cksum_bad);
35765450Sbrendan 
35775450Sbrendan 		zio->io_flags &= ~ZIO_FLAG_DONT_CACHE;
35785450Sbrendan 		rzio = zio_read(NULL, cb->l2rcb_spa, &cb->l2rcb_bp,
35795450Sbrendan 		    buf->b_data, zio->io_size, arc_read_done, buf,
35805450Sbrendan 		    zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb);
35815450Sbrendan 
35825450Sbrendan 		/*
35835450Sbrendan 		 * Since this is a seperate thread, we can wait on this
35845450Sbrendan 		 * I/O whether there is an io_waiter or not.
35855450Sbrendan 		 */
35865450Sbrendan 		err = zio_wait(rzio);
35875450Sbrendan 
35885450Sbrendan 		/*
35895450Sbrendan 		 * Let the resent I/O call arc_read_done() instead.
35905450Sbrendan 		 * io_error is set to the reissued I/O error status.
35915450Sbrendan 		 */
35925450Sbrendan 		zio->io_done = NULL;
35935450Sbrendan 		zio->io_waiter = NULL;
35945450Sbrendan 		zio->io_error = err;
35955450Sbrendan 	}
35965450Sbrendan 
35975450Sbrendan 	kmem_free(cb, sizeof (l2arc_read_callback_t));
35985450Sbrendan }
35995450Sbrendan 
36005450Sbrendan /*
36015450Sbrendan  * This is the list priority from which the L2ARC will search for pages to
36025450Sbrendan  * cache.  This is used within loops (0..3) to cycle through lists in the
36035450Sbrendan  * desired order.  This order can have a significant effect on cache
36045450Sbrendan  * performance.
36055450Sbrendan  *
36065450Sbrendan  * Currently the metadata lists are hit first, MFU then MRU, followed by
36075450Sbrendan  * the data lists.  This function returns a locked list, and also returns
36085450Sbrendan  * the lock pointer.
36095450Sbrendan  */
36105450Sbrendan static list_t *
36115450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock)
36125450Sbrendan {
36135450Sbrendan 	list_t *list;
36145450Sbrendan 
36155450Sbrendan 	ASSERT(list_num >= 0 && list_num <= 3);
36165450Sbrendan 
36175450Sbrendan 	switch (list_num) {
36185450Sbrendan 	case 0:
36195450Sbrendan 		list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
36205450Sbrendan 		*lock = &arc_mfu->arcs_mtx;
36215450Sbrendan 		break;
36225450Sbrendan 	case 1:
36235450Sbrendan 		list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
36245450Sbrendan 		*lock = &arc_mru->arcs_mtx;
36255450Sbrendan 		break;
36265450Sbrendan 	case 2:
36275450Sbrendan 		list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
36285450Sbrendan 		*lock = &arc_mfu->arcs_mtx;
36295450Sbrendan 		break;
36305450Sbrendan 	case 3:
36315450Sbrendan 		list = &arc_mru->arcs_list[ARC_BUFC_DATA];
36325450Sbrendan 		*lock = &arc_mru->arcs_mtx;
36335450Sbrendan 		break;
36345450Sbrendan 	}
36355450Sbrendan 
36365450Sbrendan 	ASSERT(!(MUTEX_HELD(*lock)));
36375450Sbrendan 	mutex_enter(*lock);
36385450Sbrendan 	return (list);
36395450Sbrendan }
36405450Sbrendan 
36415450Sbrendan /*
36425450Sbrendan  * Evict buffers from the device write hand to the distance specified in
36435450Sbrendan  * bytes.  This distance may span populated buffers, it may span nothing.
36445450Sbrendan  * This is clearing a region on the L2ARC device ready for writing.
36455450Sbrendan  * If the 'all' boolean is set, every buffer is evicted.
36465450Sbrendan  */
36475450Sbrendan static void
36485450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
36495450Sbrendan {
36505450Sbrendan 	list_t *buflist;
36515450Sbrendan 	l2arc_buf_hdr_t *abl2;
36525450Sbrendan 	arc_buf_hdr_t *ab, *ab_prev;
36535450Sbrendan 	kmutex_t *hash_lock;
36545450Sbrendan 	uint64_t taddr;
36555450Sbrendan 
36565450Sbrendan 	ASSERT(MUTEX_HELD(&l2arc_dev_mtx));
36575450Sbrendan 
36585450Sbrendan 	buflist = dev->l2ad_buflist;
36595450Sbrendan 
36605450Sbrendan 	if (buflist == NULL)
36615450Sbrendan 		return;
36625450Sbrendan 
36635450Sbrendan 	if (!all && dev->l2ad_first) {
36645450Sbrendan 		/*
36655450Sbrendan 		 * This is the first sweep through the device.  There is
36665450Sbrendan 		 * nothing to evict.
36675450Sbrendan 		 */
36685450Sbrendan 		return;
36695450Sbrendan 	}
36705450Sbrendan 
36715450Sbrendan 	if (dev->l2ad_hand >= (dev->l2ad_end - (2 * dev->l2ad_write))) {
36725450Sbrendan 		/*
36735450Sbrendan 		 * When nearing the end of the device, evict to the end
36745450Sbrendan 		 * before the device write hand jumps to the start.
36755450Sbrendan 		 */
36765450Sbrendan 		taddr = dev->l2ad_end;
36775450Sbrendan 	} else {
36785450Sbrendan 		taddr = dev->l2ad_hand + distance;
36795450Sbrendan 	}
36805450Sbrendan 	DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
36815450Sbrendan 	    uint64_t, taddr, boolean_t, all);
36825450Sbrendan 
36835450Sbrendan top:
36845450Sbrendan 	mutex_enter(&l2arc_buflist_mtx);
36855450Sbrendan 	for (ab = list_tail(buflist); ab; ab = ab_prev) {
36865450Sbrendan 		ab_prev = list_prev(buflist, ab);
36875450Sbrendan 
36885450Sbrendan 		hash_lock = HDR_LOCK(ab);
36895450Sbrendan 		if (!mutex_tryenter(hash_lock)) {
36905450Sbrendan 			/*
36915450Sbrendan 			 * Missed the hash lock.  Retry.
36925450Sbrendan 			 */
36935450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
36945450Sbrendan 			mutex_exit(&l2arc_buflist_mtx);
36955450Sbrendan 			mutex_enter(hash_lock);
36965450Sbrendan 			mutex_exit(hash_lock);
36975450Sbrendan 			goto top;
36985450Sbrendan 		}
36995450Sbrendan 
37005450Sbrendan 		if (HDR_L2_WRITE_HEAD(ab)) {
37015450Sbrendan 			/*
37025450Sbrendan 			 * We hit a write head node.  Leave it for
37035450Sbrendan 			 * l2arc_write_done().
37045450Sbrendan 			 */
37055450Sbrendan 			list_remove(buflist, ab);
37065450Sbrendan 			mutex_exit(hash_lock);
37075450Sbrendan 			continue;
37085450Sbrendan 		}
37095450Sbrendan 
37105450Sbrendan 		if (!all && ab->b_l2hdr != NULL &&
37115450Sbrendan 		    (ab->b_l2hdr->b_daddr > taddr ||
37125450Sbrendan 		    ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
37135450Sbrendan 			/*
37145450Sbrendan 			 * We've evicted to the target address,
37155450Sbrendan 			 * or the end of the device.
37165450Sbrendan 			 */
37175450Sbrendan 			mutex_exit(hash_lock);
37185450Sbrendan 			break;
37195450Sbrendan 		}
37205450Sbrendan 
37215450Sbrendan 		if (HDR_FREE_IN_PROGRESS(ab)) {
37225450Sbrendan 			/*
37235450Sbrendan 			 * Already on the path to destruction.
37245450Sbrendan 			 */
37255450Sbrendan 			mutex_exit(hash_lock);
37265450Sbrendan 			continue;
37275450Sbrendan 		}
37285450Sbrendan 
37295450Sbrendan 		if (ab->b_state == arc_l2c_only) {
37305450Sbrendan 			ASSERT(!HDR_L2_READING(ab));
37315450Sbrendan 			/*
37325450Sbrendan 			 * This doesn't exist in the ARC.  Destroy.
37335450Sbrendan 			 * arc_hdr_destroy() will call list_remove()
37345450Sbrendan 			 * and decrement arcstat_l2_size.
37355450Sbrendan 			 */
37365450Sbrendan 			arc_change_state(arc_anon, ab, hash_lock);
37375450Sbrendan 			arc_hdr_destroy(ab);
37385450Sbrendan 		} else {
37395450Sbrendan 			/*
37405450Sbrendan 			 * Tell ARC this no longer exists in L2ARC.
37415450Sbrendan 			 */
37425450Sbrendan 			if (ab->b_l2hdr != NULL) {
37435450Sbrendan 				abl2 = ab->b_l2hdr;
37445450Sbrendan 				ab->b_l2hdr = NULL;
37455450Sbrendan 				kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
37465450Sbrendan 				ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
37475450Sbrendan 			}
37485450Sbrendan 			list_remove(buflist, ab);
37495450Sbrendan 
37505450Sbrendan 			/*
37515450Sbrendan 			 * This may have been leftover after a
37525450Sbrendan 			 * failed write.
37535450Sbrendan 			 */
37545450Sbrendan 			ab->b_flags &= ~ARC_L2_WRITING;
37555450Sbrendan 
37565450Sbrendan 			/*
37575450Sbrendan 			 * Invalidate issued or about to be issued
37585450Sbrendan 			 * reads, since we may be about to write
37595450Sbrendan 			 * over this location.
37605450Sbrendan 			 */
37615450Sbrendan 			if (HDR_L2_READING(ab)) {
37625450Sbrendan 				ARCSTAT_BUMP(arcstat_l2_evict_reading);
37635450Sbrendan 				ab->b_flags |= ARC_L2_EVICTED;
37645450Sbrendan 			}
37655450Sbrendan 		}
37665450Sbrendan 		mutex_exit(hash_lock);
37675450Sbrendan 	}
37685450Sbrendan 	mutex_exit(&l2arc_buflist_mtx);
37695450Sbrendan 
37705450Sbrendan 	spa_l2cache_space_update(dev->l2ad_vdev, 0, -(taddr - dev->l2ad_evict));
37715450Sbrendan 	dev->l2ad_evict = taddr;
37725450Sbrendan }
37735450Sbrendan 
37745450Sbrendan /*
37755450Sbrendan  * Find and write ARC buffers to the L2ARC device.
37765450Sbrendan  *
37775450Sbrendan  * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
37785450Sbrendan  * for reading until they have completed writing.
37795450Sbrendan  */
37805450Sbrendan static void
37815450Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev)
37825450Sbrendan {
37835450Sbrendan 	arc_buf_hdr_t *ab, *ab_prev, *head;
37845450Sbrendan 	l2arc_buf_hdr_t *hdrl2;
37855450Sbrendan 	list_t *list;
37865450Sbrendan 	uint64_t passed_sz, write_sz, buf_sz;
37875450Sbrendan 	uint64_t target_sz = dev->l2ad_write;
37885450Sbrendan 	uint64_t headroom = dev->l2ad_write * l2arc_headroom;
37895450Sbrendan 	void *buf_data;
37905450Sbrendan 	kmutex_t *hash_lock, *list_lock;
37915450Sbrendan 	boolean_t have_lock, full;
37925450Sbrendan 	l2arc_write_callback_t *cb;
37935450Sbrendan 	zio_t *pio, *wzio;
37945450Sbrendan 
37955450Sbrendan 	ASSERT(MUTEX_HELD(&l2arc_dev_mtx));
37965450Sbrendan 	ASSERT(dev->l2ad_vdev != NULL);
37975450Sbrendan 
37985450Sbrendan 	pio = NULL;
37995450Sbrendan 	write_sz = 0;
38005450Sbrendan 	full = B_FALSE;
38015450Sbrendan 	head = kmem_cache_alloc(hdr_cache, KM_SLEEP);
38025450Sbrendan 	head->b_flags |= ARC_L2_WRITE_HEAD;
38035450Sbrendan 
38045450Sbrendan 	/*
38055450Sbrendan 	 * Copy buffers for L2ARC writing.
38065450Sbrendan 	 */
38075450Sbrendan 	mutex_enter(&l2arc_buflist_mtx);
38085450Sbrendan 	for (int try = 0; try <= 3; try++) {
38095450Sbrendan 		list = l2arc_list_locked(try, &list_lock);
38105450Sbrendan 		passed_sz = 0;
38115450Sbrendan 
38125450Sbrendan 		for (ab = list_tail(list); ab; ab = ab_prev) {
38135450Sbrendan 			ab_prev = list_prev(list, ab);
38145450Sbrendan 
38155450Sbrendan 			hash_lock = HDR_LOCK(ab);
38165450Sbrendan 			have_lock = MUTEX_HELD(hash_lock);
38175450Sbrendan 			if (!have_lock && !mutex_tryenter(hash_lock)) {
38185450Sbrendan 				/*
38195450Sbrendan 				 * Skip this buffer rather than waiting.
38205450Sbrendan 				 */
38215450Sbrendan 				continue;
38225450Sbrendan 			}
38235450Sbrendan 
38245450Sbrendan 			passed_sz += ab->b_size;
38255450Sbrendan 			if (passed_sz > headroom) {
38265450Sbrendan 				/*
38275450Sbrendan 				 * Searched too far.
38285450Sbrendan 				 */
38295450Sbrendan 				mutex_exit(hash_lock);
38305450Sbrendan 				break;
38315450Sbrendan 			}
38325450Sbrendan 
38335450Sbrendan 			if (ab->b_spa != spa) {
38345450Sbrendan 				mutex_exit(hash_lock);
38355450Sbrendan 				continue;
38365450Sbrendan 			}
38375450Sbrendan 
38385450Sbrendan 			if (ab->b_l2hdr != NULL) {
38395450Sbrendan 				/*
38405450Sbrendan 				 * Already in L2ARC.
38415450Sbrendan 				 */
38425450Sbrendan 				mutex_exit(hash_lock);
38435450Sbrendan 				continue;
38445450Sbrendan 			}
38455450Sbrendan 
38465450Sbrendan 			if (HDR_IO_IN_PROGRESS(ab) || HDR_DONT_L2CACHE(ab)) {
38475450Sbrendan 				mutex_exit(hash_lock);
38485450Sbrendan 				continue;
38495450Sbrendan 			}
38505450Sbrendan 
38515450Sbrendan 			if ((write_sz + ab->b_size) > target_sz) {
38525450Sbrendan 				full = B_TRUE;
38535450Sbrendan 				mutex_exit(hash_lock);
38545450Sbrendan 				break;
38555450Sbrendan 			}
38565450Sbrendan 
38575450Sbrendan 			if (ab->b_buf == NULL) {
38585450Sbrendan 				DTRACE_PROBE1(l2arc__buf__null, void *, ab);
38595450Sbrendan 				mutex_exit(hash_lock);
38605450Sbrendan 				continue;
38615450Sbrendan 			}
38625450Sbrendan 
38635450Sbrendan 			if (pio == NULL) {
38645450Sbrendan 				/*
38655450Sbrendan 				 * Insert a dummy header on the buflist so
38665450Sbrendan 				 * l2arc_write_done() can find where the
38675450Sbrendan 				 * write buffers begin without searching.
38685450Sbrendan 				 */
38695450Sbrendan 				list_insert_head(dev->l2ad_buflist, head);
38705450Sbrendan 
38715450Sbrendan 				cb = kmem_alloc(
38725450Sbrendan 				    sizeof (l2arc_write_callback_t), KM_SLEEP);
38735450Sbrendan 				cb->l2wcb_dev = dev;
38745450Sbrendan 				cb->l2wcb_head = head;
38755450Sbrendan 				pio = zio_root(spa, l2arc_write_done, cb,
38765450Sbrendan 				    ZIO_FLAG_CANFAIL);
38775450Sbrendan 			}
38785450Sbrendan 
38795450Sbrendan 			/*
38805450Sbrendan 			 * Create and add a new L2ARC header.
38815450Sbrendan 			 */
38825450Sbrendan 			hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
38835450Sbrendan 			hdrl2->b_dev = dev;
38845450Sbrendan 			hdrl2->b_daddr = dev->l2ad_hand;
38855450Sbrendan 
38865450Sbrendan 			ab->b_flags |= ARC_L2_WRITING;
38875450Sbrendan 			ab->b_l2hdr = hdrl2;
38885450Sbrendan 			list_insert_head(dev->l2ad_buflist, ab);
38895450Sbrendan 			buf_data = ab->b_buf->b_data;
38905450Sbrendan 			buf_sz = ab->b_size;
38915450Sbrendan 
38925450Sbrendan 			/*
38935450Sbrendan 			 * Compute and store the buffer cksum before
38945450Sbrendan 			 * writing.  On debug the cksum is verified first.
38955450Sbrendan 			 */
38965450Sbrendan 			arc_cksum_verify(ab->b_buf);
38975450Sbrendan 			arc_cksum_compute(ab->b_buf, B_TRUE);
38985450Sbrendan 
38995450Sbrendan 			mutex_exit(hash_lock);
39005450Sbrendan 
39015450Sbrendan 			wzio = zio_write_phys(pio, dev->l2ad_vdev,
39025450Sbrendan 			    dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
39035450Sbrendan 			    NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
39045450Sbrendan 			    ZIO_FLAG_CANFAIL, B_FALSE);
39055450Sbrendan 
39065450Sbrendan 			DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
39075450Sbrendan 			    zio_t *, wzio);
39085450Sbrendan 			(void) zio_nowait(wzio);
39095450Sbrendan 
39105450Sbrendan 			write_sz += buf_sz;
39115450Sbrendan 			dev->l2ad_hand += buf_sz;
39125450Sbrendan 		}
39135450Sbrendan 
39145450Sbrendan 		mutex_exit(list_lock);
39155450Sbrendan 
39165450Sbrendan 		if (full == B_TRUE)
39175450Sbrendan 			break;
39185450Sbrendan 	}
39195450Sbrendan 	mutex_exit(&l2arc_buflist_mtx);
39205450Sbrendan 
39215450Sbrendan 	if (pio == NULL) {
39225450Sbrendan 		ASSERT3U(write_sz, ==, 0);
39235450Sbrendan 		kmem_cache_free(hdr_cache, head);
39245450Sbrendan 		return;
39255450Sbrendan 	}
39265450Sbrendan 
39275450Sbrendan 	ASSERT3U(write_sz, <=, target_sz);
39285450Sbrendan 	ARCSTAT_BUMP(arcstat_l2_writes_sent);
39295450Sbrendan 	ARCSTAT_INCR(arcstat_l2_size, write_sz);
39305450Sbrendan 	spa_l2cache_space_update(dev->l2ad_vdev, 0, write_sz);
39315450Sbrendan 
39325450Sbrendan 	/*
39335450Sbrendan 	 * Bump device hand to the device start if it is approaching the end.
39345450Sbrendan 	 * l2arc_evict() will already have evicted ahead for this case.
39355450Sbrendan 	 */
39365450Sbrendan 	if (dev->l2ad_hand >= (dev->l2ad_end - dev->l2ad_write)) {
39375450Sbrendan 		spa_l2cache_space_update(dev->l2ad_vdev, 0,
39385450Sbrendan 		    dev->l2ad_end - dev->l2ad_hand);
39395450Sbrendan 		dev->l2ad_hand = dev->l2ad_start;
39405450Sbrendan 		dev->l2ad_evict = dev->l2ad_start;
39415450Sbrendan 		dev->l2ad_first = B_FALSE;
39425450Sbrendan 	}
39435450Sbrendan 
39445450Sbrendan 	(void) zio_wait(pio);
39455450Sbrendan }
39465450Sbrendan 
39475450Sbrendan /*
39485450Sbrendan  * This thread feeds the L2ARC at regular intervals.  This is the beating
39495450Sbrendan  * heart of the L2ARC.
39505450Sbrendan  */
39515450Sbrendan static void
39525450Sbrendan l2arc_feed_thread(void)
39535450Sbrendan {
39545450Sbrendan 	callb_cpr_t cpr;
39555450Sbrendan 	l2arc_dev_t *dev;
39565450Sbrendan 	spa_t *spa;
39575450Sbrendan 	int interval;
39585450Sbrendan 	boolean_t startup = B_TRUE;
39595450Sbrendan 
39605450Sbrendan 	CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
39615450Sbrendan 
39625450Sbrendan 	mutex_enter(&l2arc_feed_thr_lock);
39635450Sbrendan 
39645450Sbrendan 	while (l2arc_thread_exit == 0) {
39655450Sbrendan 		/*
39665450Sbrendan 		 * Initially pause for L2ARC_FEED_DELAY seconds as a grace
39675450Sbrendan 		 * interval during boot, followed by l2arc_feed_secs seconds
39685450Sbrendan 		 * thereafter.
39695450Sbrendan 		 */
39705450Sbrendan 		CALLB_CPR_SAFE_BEGIN(&cpr);
39715450Sbrendan 		if (startup) {
39725450Sbrendan 			interval = L2ARC_FEED_DELAY;
39735450Sbrendan 			startup = B_FALSE;
39745450Sbrendan 		} else {
39755450Sbrendan 			interval = l2arc_feed_secs;
39765450Sbrendan 		}
39775450Sbrendan 		(void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock,
39785450Sbrendan 		    lbolt + (hz * interval));
39795450Sbrendan 		CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
39805450Sbrendan 
39815450Sbrendan 		/*
39825450Sbrendan 		 * Do nothing until L2ARC devices exist.
39835450Sbrendan 		 */
39845450Sbrendan 		mutex_enter(&l2arc_dev_mtx);
39855450Sbrendan 		if (l2arc_ndev == 0) {
39865450Sbrendan 			mutex_exit(&l2arc_dev_mtx);
39875450Sbrendan 			continue;
39885450Sbrendan 		}
39895450Sbrendan 
39905450Sbrendan 		/*
39915450Sbrendan 		 * Avoid contributing to memory pressure.
39925450Sbrendan 		 */
39935450Sbrendan 		if (arc_reclaim_needed()) {
39945450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
39955450Sbrendan 			mutex_exit(&l2arc_dev_mtx);
39965450Sbrendan 			continue;
39975450Sbrendan 		}
39985450Sbrendan 
39995450Sbrendan 		/*
40005450Sbrendan 		 * This selects the next l2arc device to write to, and in
40015450Sbrendan 		 * doing so the next spa to feed from: dev->l2ad_spa.
40025450Sbrendan 		 */
40035450Sbrendan 		if ((dev = l2arc_dev_get_next()) == NULL) {
40045450Sbrendan 			mutex_exit(&l2arc_dev_mtx);
40055450Sbrendan 			continue;
40065450Sbrendan 		}
40075450Sbrendan 		spa = dev->l2ad_spa;
40085450Sbrendan 		ASSERT(spa != NULL);
40095450Sbrendan 		ARCSTAT_BUMP(arcstat_l2_feeds);
40105450Sbrendan 
40115450Sbrendan 		/*
40125450Sbrendan 		 * Evict L2ARC buffers that will be overwritten.
40135450Sbrendan 		 */
40145450Sbrendan 		l2arc_evict(dev, dev->l2ad_write, B_FALSE);
40155450Sbrendan 
40165450Sbrendan 		/*
40175450Sbrendan 		 * Write ARC buffers.
40185450Sbrendan 		 */
40195450Sbrendan 		l2arc_write_buffers(spa, dev);
40205450Sbrendan 		mutex_exit(&l2arc_dev_mtx);
40215450Sbrendan 	}
40225450Sbrendan 
40235450Sbrendan 	l2arc_thread_exit = 0;
40245450Sbrendan 	cv_broadcast(&l2arc_feed_thr_cv);
40255450Sbrendan 	CALLB_CPR_EXIT(&cpr);		/* drops l2arc_feed_thr_lock */
40265450Sbrendan 	thread_exit();
40275450Sbrendan }
40285450Sbrendan 
40295450Sbrendan /*
40305450Sbrendan  * Add a vdev for use by the L2ARC.  By this point the spa has already
40315450Sbrendan  * validated the vdev and opened it.
40325450Sbrendan  */
40335450Sbrendan void
40345450Sbrendan l2arc_add_vdev(spa_t *spa, vdev_t *vd, uint64_t start, uint64_t end)
40355450Sbrendan {
40365450Sbrendan 	l2arc_dev_t *adddev;
40375450Sbrendan 
40385450Sbrendan 	/*
40395450Sbrendan 	 * Create a new l2arc device entry.
40405450Sbrendan 	 */
40415450Sbrendan 	adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
40425450Sbrendan 	adddev->l2ad_spa = spa;
40435450Sbrendan 	adddev->l2ad_vdev = vd;
40445450Sbrendan 	adddev->l2ad_write = l2arc_write_max;
40455450Sbrendan 	adddev->l2ad_start = start;
40465450Sbrendan 	adddev->l2ad_end = end;
40475450Sbrendan 	adddev->l2ad_hand = adddev->l2ad_start;
40485450Sbrendan 	adddev->l2ad_evict = adddev->l2ad_start;
40495450Sbrendan 	adddev->l2ad_first = B_TRUE;
40505450Sbrendan 	ASSERT3U(adddev->l2ad_write, >, 0);
40515450Sbrendan 
40525450Sbrendan 	/*
40535450Sbrendan 	 * This is a list of all ARC buffers that are still valid on the
40545450Sbrendan 	 * device.
40555450Sbrendan 	 */
40565450Sbrendan 	adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
40575450Sbrendan 	list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
40585450Sbrendan 	    offsetof(arc_buf_hdr_t, b_l2node));
40595450Sbrendan 
40605450Sbrendan 	spa_l2cache_space_update(vd, adddev->l2ad_end - adddev->l2ad_hand, 0);
40615450Sbrendan 
40625450Sbrendan 	/*
40635450Sbrendan 	 * Add device to global list
40645450Sbrendan 	 */
40655450Sbrendan 	mutex_enter(&l2arc_dev_mtx);
40665450Sbrendan 	list_insert_head(l2arc_dev_list, adddev);
40675450Sbrendan 	atomic_inc_64(&l2arc_ndev);
40685450Sbrendan 	mutex_exit(&l2arc_dev_mtx);
40695450Sbrendan }
40705450Sbrendan 
40715450Sbrendan /*
40725450Sbrendan  * Remove a vdev from the L2ARC.
40735450Sbrendan  */
40745450Sbrendan void
40755450Sbrendan l2arc_remove_vdev(vdev_t *vd)
40765450Sbrendan {
40775450Sbrendan 	l2arc_dev_t *dev, *nextdev, *remdev = NULL;
40785450Sbrendan 
40795450Sbrendan 	/*
40805450Sbrendan 	 * We can only grab the spa config lock when cache device writes
40815450Sbrendan 	 * complete.
40825450Sbrendan 	 */
40835450Sbrendan 	ASSERT3U(l2arc_writes_sent, ==, l2arc_writes_done);
40845450Sbrendan 
40855450Sbrendan 	/*
40865450Sbrendan 	 * Find the device by vdev
40875450Sbrendan 	 */
40885450Sbrendan 	mutex_enter(&l2arc_dev_mtx);
40895450Sbrendan 	for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
40905450Sbrendan 		nextdev = list_next(l2arc_dev_list, dev);
40915450Sbrendan 		if (vd == dev->l2ad_vdev) {
40925450Sbrendan 			remdev = dev;
40935450Sbrendan 			break;
40945450Sbrendan 		}
40955450Sbrendan 	}
40965450Sbrendan 	ASSERT(remdev != NULL);
40975450Sbrendan 
40985450Sbrendan 	/*
40995450Sbrendan 	 * Remove device from global list
41005450Sbrendan 	 */
41015450Sbrendan 	list_remove(l2arc_dev_list, remdev);
41025450Sbrendan 	l2arc_dev_last = NULL;		/* may have been invalidated */
41035450Sbrendan 
41045450Sbrendan 	/*
41055450Sbrendan 	 * Clear all buflists and ARC references.  L2ARC device flush.
41065450Sbrendan 	 */
41075450Sbrendan 	l2arc_evict(remdev, 0, B_TRUE);
41085450Sbrendan 	list_destroy(remdev->l2ad_buflist);
41095450Sbrendan 	kmem_free(remdev->l2ad_buflist, sizeof (list_t));
41105450Sbrendan 	kmem_free(remdev, sizeof (l2arc_dev_t));
41115450Sbrendan 
41125450Sbrendan 	atomic_dec_64(&l2arc_ndev);
41135450Sbrendan 	mutex_exit(&l2arc_dev_mtx);
41145450Sbrendan }
41155450Sbrendan 
41165450Sbrendan void
41175450Sbrendan l2arc_init()
41185450Sbrendan {
41195450Sbrendan 	l2arc_thread_exit = 0;
41205450Sbrendan 	l2arc_ndev = 0;
41215450Sbrendan 	l2arc_writes_sent = 0;
41225450Sbrendan 	l2arc_writes_done = 0;
41235450Sbrendan 
41245450Sbrendan 	mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
41255450Sbrendan 	cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
41265450Sbrendan 	mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
41275450Sbrendan 	mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
41285450Sbrendan 	mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
41295450Sbrendan 
41305450Sbrendan 	l2arc_dev_list = &L2ARC_dev_list;
41315450Sbrendan 	l2arc_free_on_write = &L2ARC_free_on_write;
41325450Sbrendan 	list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
41335450Sbrendan 	    offsetof(l2arc_dev_t, l2ad_node));
41345450Sbrendan 	list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
41355450Sbrendan 	    offsetof(l2arc_data_free_t, l2df_list_node));
41365450Sbrendan 
41375450Sbrendan 	(void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
41385450Sbrendan 	    TS_RUN, minclsyspri);
41395450Sbrendan }
41405450Sbrendan 
41415450Sbrendan void
41425450Sbrendan l2arc_fini()
41435450Sbrendan {
41445450Sbrendan 	mutex_enter(&l2arc_feed_thr_lock);
41455450Sbrendan 	cv_signal(&l2arc_feed_thr_cv);	/* kick thread out of startup */
41465450Sbrendan 	l2arc_thread_exit = 1;
41475450Sbrendan 	while (l2arc_thread_exit != 0)
41485450Sbrendan 		cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
41495450Sbrendan 	mutex_exit(&l2arc_feed_thr_lock);
41505450Sbrendan 
41515450Sbrendan 	mutex_destroy(&l2arc_feed_thr_lock);
41525450Sbrendan 	cv_destroy(&l2arc_feed_thr_cv);
41535450Sbrendan 	mutex_destroy(&l2arc_dev_mtx);
41545450Sbrendan 	mutex_destroy(&l2arc_buflist_mtx);
41555450Sbrendan 	mutex_destroy(&l2arc_free_on_write_mtx);
41565450Sbrendan 
41575450Sbrendan 	list_destroy(l2arc_dev_list);
41585450Sbrendan 	list_destroy(l2arc_free_on_write);
41595450Sbrendan }
4160