xref: /onnv-gate/usr/src/uts/common/fs/zfs/arc.c (revision 13049:bda0decf867b)
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 /*
2212296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens /*
263403Sbmc  * DVA-based Adjustable Replacement Cache
27789Sahrens  *
281544Seschrock  * While much of the theory of operation used here is
291544Seschrock  * based on the self-tuning, low overhead replacement cache
30789Sahrens  * presented by Megiddo and Modha at FAST 2003, there are some
31789Sahrens  * significant differences:
32789Sahrens  *
33789Sahrens  * 1. The Megiddo and Modha model assumes any page is evictable.
34789Sahrens  * Pages in its cache cannot be "locked" into memory.  This makes
35789Sahrens  * the eviction algorithm simple: evict the last page in the list.
36789Sahrens  * This also make the performance characteristics easy to reason
37789Sahrens  * about.  Our cache is not so simple.  At any given moment, some
38789Sahrens  * subset of the blocks in the cache are un-evictable because we
39789Sahrens  * have handed out a reference to them.  Blocks are only evictable
40789Sahrens  * when there are no external references active.  This makes
41789Sahrens  * eviction far more problematic:  we choose to evict the evictable
42789Sahrens  * blocks that are the "lowest" in the list.
43789Sahrens  *
44789Sahrens  * There are times when it is not possible to evict the requested
45789Sahrens  * space.  In these circumstances we are unable to adjust the cache
46789Sahrens  * size.  To prevent the cache growing unbounded at these times we
475450Sbrendan  * implement a "cache throttle" that slows the flow of new data
485450Sbrendan  * into the cache until we can make space available.
49789Sahrens  *
50789Sahrens  * 2. The Megiddo and Modha model assumes a fixed cache size.
51789Sahrens  * Pages are evicted when the cache is full and there is a cache
52789Sahrens  * miss.  Our model has a variable sized cache.  It grows with
535450Sbrendan  * high use, but also tries to react to memory pressure from the
54789Sahrens  * operating system: decreasing its size when system memory is
55789Sahrens  * tight.
56789Sahrens  *
57789Sahrens  * 3. The Megiddo and Modha model assumes a fixed page size. All
58789Sahrens  * elements of the cache are therefor exactly the same size.  So
59789Sahrens  * when adjusting the cache size following a cache miss, its simply
60789Sahrens  * a matter of choosing a single page to evict.  In our model, we
61789Sahrens  * have variable sized cache blocks (rangeing from 512 bytes to
62789Sahrens  * 128K bytes).  We therefor choose a set of blocks to evict to make
63789Sahrens  * space for a cache miss that approximates as closely as possible
64789Sahrens  * the space used by the new block.
65789Sahrens  *
66789Sahrens  * See also:  "ARC: A Self-Tuning, Low Overhead Replacement Cache"
67789Sahrens  * by N. Megiddo & D. Modha, FAST 2003
68789Sahrens  */
69789Sahrens 
70789Sahrens /*
71789Sahrens  * The locking model:
72789Sahrens  *
73789Sahrens  * A new reference to a cache buffer can be obtained in two
74789Sahrens  * ways: 1) via a hash table lookup using the DVA as a key,
755450Sbrendan  * or 2) via one of the ARC lists.  The arc_read() interface
76789Sahrens  * uses method 1, while the internal arc algorithms for
77789Sahrens  * adjusting the cache use method 2.  We therefor provide two
78789Sahrens  * types of locks: 1) the hash table lock array, and 2) the
79789Sahrens  * arc list locks.
80789Sahrens  *
81789Sahrens  * Buffers do not have their own mutexs, rather they rely on the
82789Sahrens  * hash table mutexs for the bulk of their protection (i.e. most
83789Sahrens  * fields in the arc_buf_hdr_t are protected by these mutexs).
84789Sahrens  *
85789Sahrens  * buf_hash_find() returns the appropriate mutex (held) when it
86789Sahrens  * locates the requested buffer in the hash table.  It returns
87789Sahrens  * NULL for the mutex if the buffer was not in the table.
88789Sahrens  *
89789Sahrens  * buf_hash_remove() expects the appropriate hash mutex to be
90789Sahrens  * already held before it is invoked.
91789Sahrens  *
92789Sahrens  * Each arc state also has a mutex which is used to protect the
93789Sahrens  * buffer list associated with the state.  When attempting to
94789Sahrens  * obtain a hash table lock while holding an arc list lock you
95789Sahrens  * must use: mutex_tryenter() to avoid deadlock.  Also note that
962688Smaybee  * the active state mutex must be held before the ghost state mutex.
97789Sahrens  *
981544Seschrock  * Arc buffers may have an associated eviction callback function.
991544Seschrock  * This function will be invoked prior to removing the buffer (e.g.
1001544Seschrock  * in arc_do_user_evicts()).  Note however that the data associated
1011544Seschrock  * with the buffer may be evicted prior to the callback.  The callback
1021544Seschrock  * must be made with *no locks held* (to prevent deadlock).  Additionally,
1031544Seschrock  * the users of callbacks must ensure that their private data is
1041544Seschrock  * protected from simultaneous callbacks from arc_buf_evict()
1051544Seschrock  * and arc_do_user_evicts().
1061544Seschrock  *
107789Sahrens  * Note that the majority of the performance stats are manipulated
108789Sahrens  * with atomic operations.
1095450Sbrendan  *
1105450Sbrendan  * The L2ARC uses the l2arc_buflist_mtx global mutex for the following:
1115450Sbrendan  *
1125450Sbrendan  *	- L2ARC buflist creation
1135450Sbrendan  *	- L2ARC buflist eviction
1145450Sbrendan  *	- L2ARC write completion, which walks L2ARC buflists
1155450Sbrendan  *	- ARC header destruction, as it removes from L2ARC buflists
1165450Sbrendan  *	- ARC header release, as it removes from L2ARC buflists
117789Sahrens  */
118789Sahrens 
119789Sahrens #include <sys/spa.h>
120789Sahrens #include <sys/zio.h>
121789Sahrens #include <sys/zfs_context.h>
122789Sahrens #include <sys/arc.h>
123789Sahrens #include <sys/refcount.h>
1246643Seschrock #include <sys/vdev.h>
1259816SGeorge.Wilson@Sun.COM #include <sys/vdev_impl.h>
126789Sahrens #ifdef _KERNEL
127789Sahrens #include <sys/vmsystm.h>
128789Sahrens #include <vm/anon.h>
129789Sahrens #include <sys/fs/swapnode.h>
1301484Sek110237 #include <sys/dnlc.h>
131789Sahrens #endif
132789Sahrens #include <sys/callb.h>
1333403Sbmc #include <sys/kstat.h>
13410922SJeff.Bonwick@Sun.COM #include <zfs_fletcher.h>
135789Sahrens 
136789Sahrens static kmutex_t		arc_reclaim_thr_lock;
137789Sahrens static kcondvar_t	arc_reclaim_thr_cv;	/* used to signal reclaim thr */
138789Sahrens static uint8_t		arc_thread_exit;
139789Sahrens 
1406245Smaybee extern int zfs_write_limit_shift;
1416245Smaybee extern uint64_t zfs_write_limit_max;
1427468SMark.Maybee@Sun.COM extern kmutex_t zfs_write_limit_lock;
1436245Smaybee 
1441484Sek110237 #define	ARC_REDUCE_DNLC_PERCENT	3
1451484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
1461484Sek110237 
147789Sahrens typedef enum arc_reclaim_strategy {
148789Sahrens 	ARC_RECLAIM_AGGR,		/* Aggressive reclaim strategy */
149789Sahrens 	ARC_RECLAIM_CONS		/* Conservative reclaim strategy */
150789Sahrens } arc_reclaim_strategy_t;
151789Sahrens 
152789Sahrens /* number of seconds before growing cache again */
153789Sahrens static int		arc_grow_retry = 60;
154789Sahrens 
1558582SBrendan.Gregg@Sun.COM /* shift of arc_c for calculating both min and max arc_p */
1568582SBrendan.Gregg@Sun.COM static int		arc_p_min_shift = 4;
1578582SBrendan.Gregg@Sun.COM 
1588582SBrendan.Gregg@Sun.COM /* log2(fraction of arc to reclaim) */
1598582SBrendan.Gregg@Sun.COM static int		arc_shrink_shift = 5;
1608582SBrendan.Gregg@Sun.COM 
1612391Smaybee /*
1622638Sperrin  * minimum lifespan of a prefetch block in clock ticks
1632638Sperrin  * (initialized in arc_init())
1642391Smaybee  */
1652638Sperrin static int		arc_min_prefetch_lifespan;
1662391Smaybee 
167789Sahrens static int arc_dead;
168789Sahrens 
169789Sahrens /*
1706987Sbrendan  * The arc has filled available memory and has now warmed up.
1716987Sbrendan  */
1726987Sbrendan static boolean_t arc_warm;
1736987Sbrendan 
1746987Sbrendan /*
1752885Sahrens  * These tunables are for performance analysis.
1762885Sahrens  */
1772885Sahrens uint64_t zfs_arc_max;
1782885Sahrens uint64_t zfs_arc_min;
1794645Sek110237 uint64_t zfs_arc_meta_limit = 0;
1808582SBrendan.Gregg@Sun.COM int zfs_arc_grow_retry = 0;
1818582SBrendan.Gregg@Sun.COM int zfs_arc_shrink_shift = 0;
1828582SBrendan.Gregg@Sun.COM int zfs_arc_p_min_shift = 0;
1832885Sahrens 
1842885Sahrens /*
1855450Sbrendan  * Note that buffers can be in one of 6 states:
186789Sahrens  *	ARC_anon	- anonymous (discussed below)
1871544Seschrock  *	ARC_mru		- recently used, currently cached
1881544Seschrock  *	ARC_mru_ghost	- recentely used, no longer in cache
1891544Seschrock  *	ARC_mfu		- frequently used, currently cached
1901544Seschrock  *	ARC_mfu_ghost	- frequently used, no longer in cache
1915450Sbrendan  *	ARC_l2c_only	- exists in L2ARC but not other states
1924309Smaybee  * When there are no active references to the buffer, they are
1934309Smaybee  * are linked onto a list in one of these arc states.  These are
1944309Smaybee  * the only buffers that can be evicted or deleted.  Within each
1954309Smaybee  * state there are multiple lists, one for meta-data and one for
1964309Smaybee  * non-meta-data.  Meta-data (indirect blocks, blocks of dnodes,
1974309Smaybee  * etc.) is tracked separately so that it can be managed more
1985450Sbrendan  * explicitly: favored over data, limited explicitly.
199789Sahrens  *
200789Sahrens  * Anonymous buffers are buffers that are not associated with
201789Sahrens  * a DVA.  These are buffers that hold dirty block copies
202789Sahrens  * before they are written to stable storage.  By definition,
2031544Seschrock  * they are "ref'd" and are considered part of arc_mru
204789Sahrens  * that cannot be freed.  Generally, they will aquire a DVA
2051544Seschrock  * as they are written and migrate onto the arc_mru list.
2065450Sbrendan  *
2075450Sbrendan  * The ARC_l2c_only state is for buffers that are in the second
2085450Sbrendan  * level ARC but no longer in any of the ARC_m* lists.  The second
2095450Sbrendan  * level ARC itself may also contain buffers that are in any of
2105450Sbrendan  * the ARC_m* states - meaning that a buffer can exist in two
2115450Sbrendan  * places.  The reason for the ARC_l2c_only state is to keep the
2125450Sbrendan  * buffer header in the hash table, so that reads that hit the
2135450Sbrendan  * second level ARC benefit from these fast lookups.
214789Sahrens  */
215789Sahrens 
216789Sahrens typedef struct arc_state {
2174309Smaybee 	list_t	arcs_list[ARC_BUFC_NUMTYPES];	/* list of evictable buffers */
2184309Smaybee 	uint64_t arcs_lsize[ARC_BUFC_NUMTYPES];	/* amount of evictable data */
2194309Smaybee 	uint64_t arcs_size;	/* total amount of data in this state */
2203403Sbmc 	kmutex_t arcs_mtx;
221789Sahrens } arc_state_t;
222789Sahrens 
2235450Sbrendan /* The 6 states: */
224789Sahrens static arc_state_t ARC_anon;
2251544Seschrock static arc_state_t ARC_mru;
2261544Seschrock static arc_state_t ARC_mru_ghost;
2271544Seschrock static arc_state_t ARC_mfu;
2281544Seschrock static arc_state_t ARC_mfu_ghost;
2295450Sbrendan static arc_state_t ARC_l2c_only;
230789Sahrens 
2313403Sbmc typedef struct arc_stats {
2323403Sbmc 	kstat_named_t arcstat_hits;
2333403Sbmc 	kstat_named_t arcstat_misses;
2343403Sbmc 	kstat_named_t arcstat_demand_data_hits;
2353403Sbmc 	kstat_named_t arcstat_demand_data_misses;
2363403Sbmc 	kstat_named_t arcstat_demand_metadata_hits;
2373403Sbmc 	kstat_named_t arcstat_demand_metadata_misses;
2383403Sbmc 	kstat_named_t arcstat_prefetch_data_hits;
2393403Sbmc 	kstat_named_t arcstat_prefetch_data_misses;
2403403Sbmc 	kstat_named_t arcstat_prefetch_metadata_hits;
2413403Sbmc 	kstat_named_t arcstat_prefetch_metadata_misses;
2423403Sbmc 	kstat_named_t arcstat_mru_hits;
2433403Sbmc 	kstat_named_t arcstat_mru_ghost_hits;
2443403Sbmc 	kstat_named_t arcstat_mfu_hits;
2453403Sbmc 	kstat_named_t arcstat_mfu_ghost_hits;
2463403Sbmc 	kstat_named_t arcstat_deleted;
2473403Sbmc 	kstat_named_t arcstat_recycle_miss;
2483403Sbmc 	kstat_named_t arcstat_mutex_miss;
2493403Sbmc 	kstat_named_t arcstat_evict_skip;
25010357SBrendan.Gregg@Sun.COM 	kstat_named_t arcstat_evict_l2_cached;
25110357SBrendan.Gregg@Sun.COM 	kstat_named_t arcstat_evict_l2_eligible;
25210357SBrendan.Gregg@Sun.COM 	kstat_named_t arcstat_evict_l2_ineligible;
2533403Sbmc 	kstat_named_t arcstat_hash_elements;
2543403Sbmc 	kstat_named_t arcstat_hash_elements_max;
2553403Sbmc 	kstat_named_t arcstat_hash_collisions;
2563403Sbmc 	kstat_named_t arcstat_hash_chains;
2573403Sbmc 	kstat_named_t arcstat_hash_chain_max;
2583403Sbmc 	kstat_named_t arcstat_p;
2593403Sbmc 	kstat_named_t arcstat_c;
2603403Sbmc 	kstat_named_t arcstat_c_min;
2613403Sbmc 	kstat_named_t arcstat_c_max;
2623403Sbmc 	kstat_named_t arcstat_size;
2635450Sbrendan 	kstat_named_t arcstat_hdr_size;
2648582SBrendan.Gregg@Sun.COM 	kstat_named_t arcstat_data_size;
2658582SBrendan.Gregg@Sun.COM 	kstat_named_t arcstat_other_size;
2665450Sbrendan 	kstat_named_t arcstat_l2_hits;
2675450Sbrendan 	kstat_named_t arcstat_l2_misses;
2685450Sbrendan 	kstat_named_t arcstat_l2_feeds;
2695450Sbrendan 	kstat_named_t arcstat_l2_rw_clash;
2708582SBrendan.Gregg@Sun.COM 	kstat_named_t arcstat_l2_read_bytes;
2718582SBrendan.Gregg@Sun.COM 	kstat_named_t arcstat_l2_write_bytes;
2725450Sbrendan 	kstat_named_t arcstat_l2_writes_sent;
2735450Sbrendan 	kstat_named_t arcstat_l2_writes_done;
2745450Sbrendan 	kstat_named_t arcstat_l2_writes_error;
2755450Sbrendan 	kstat_named_t arcstat_l2_writes_hdr_miss;
2765450Sbrendan 	kstat_named_t arcstat_l2_evict_lock_retry;
2775450Sbrendan 	kstat_named_t arcstat_l2_evict_reading;
2785450Sbrendan 	kstat_named_t arcstat_l2_free_on_write;
2795450Sbrendan 	kstat_named_t arcstat_l2_abort_lowmem;
2805450Sbrendan 	kstat_named_t arcstat_l2_cksum_bad;
2815450Sbrendan 	kstat_named_t arcstat_l2_io_error;
2825450Sbrendan 	kstat_named_t arcstat_l2_size;
2835450Sbrendan 	kstat_named_t arcstat_l2_hdr_size;
2846245Smaybee 	kstat_named_t arcstat_memory_throttle_count;
2853403Sbmc } arc_stats_t;
2863403Sbmc 
2873403Sbmc static arc_stats_t arc_stats = {
2883403Sbmc 	{ "hits",			KSTAT_DATA_UINT64 },
2893403Sbmc 	{ "misses",			KSTAT_DATA_UINT64 },
2903403Sbmc 	{ "demand_data_hits",		KSTAT_DATA_UINT64 },
2913403Sbmc 	{ "demand_data_misses",		KSTAT_DATA_UINT64 },
2923403Sbmc 	{ "demand_metadata_hits",	KSTAT_DATA_UINT64 },
2933403Sbmc 	{ "demand_metadata_misses",	KSTAT_DATA_UINT64 },
2943403Sbmc 	{ "prefetch_data_hits",		KSTAT_DATA_UINT64 },
2953403Sbmc 	{ "prefetch_data_misses",	KSTAT_DATA_UINT64 },
2963403Sbmc 	{ "prefetch_metadata_hits",	KSTAT_DATA_UINT64 },
2973403Sbmc 	{ "prefetch_metadata_misses",	KSTAT_DATA_UINT64 },
2983403Sbmc 	{ "mru_hits",			KSTAT_DATA_UINT64 },
2993403Sbmc 	{ "mru_ghost_hits",		KSTAT_DATA_UINT64 },
3003403Sbmc 	{ "mfu_hits",			KSTAT_DATA_UINT64 },
3013403Sbmc 	{ "mfu_ghost_hits",		KSTAT_DATA_UINT64 },
3023403Sbmc 	{ "deleted",			KSTAT_DATA_UINT64 },
3033403Sbmc 	{ "recycle_miss",		KSTAT_DATA_UINT64 },
3043403Sbmc 	{ "mutex_miss",			KSTAT_DATA_UINT64 },
3053403Sbmc 	{ "evict_skip",			KSTAT_DATA_UINT64 },
30610357SBrendan.Gregg@Sun.COM 	{ "evict_l2_cached",		KSTAT_DATA_UINT64 },
30710357SBrendan.Gregg@Sun.COM 	{ "evict_l2_eligible",		KSTAT_DATA_UINT64 },
30810357SBrendan.Gregg@Sun.COM 	{ "evict_l2_ineligible",	KSTAT_DATA_UINT64 },
3093403Sbmc 	{ "hash_elements",		KSTAT_DATA_UINT64 },
3103403Sbmc 	{ "hash_elements_max",		KSTAT_DATA_UINT64 },
3113403Sbmc 	{ "hash_collisions",		KSTAT_DATA_UINT64 },
3123403Sbmc 	{ "hash_chains",		KSTAT_DATA_UINT64 },
3133403Sbmc 	{ "hash_chain_max",		KSTAT_DATA_UINT64 },
3143403Sbmc 	{ "p",				KSTAT_DATA_UINT64 },
3153403Sbmc 	{ "c",				KSTAT_DATA_UINT64 },
3163403Sbmc 	{ "c_min",			KSTAT_DATA_UINT64 },
3173403Sbmc 	{ "c_max",			KSTAT_DATA_UINT64 },
3185450Sbrendan 	{ "size",			KSTAT_DATA_UINT64 },
3195450Sbrendan 	{ "hdr_size",			KSTAT_DATA_UINT64 },
3208582SBrendan.Gregg@Sun.COM 	{ "data_size",			KSTAT_DATA_UINT64 },
3218582SBrendan.Gregg@Sun.COM 	{ "other_size",			KSTAT_DATA_UINT64 },
3225450Sbrendan 	{ "l2_hits",			KSTAT_DATA_UINT64 },
3235450Sbrendan 	{ "l2_misses",			KSTAT_DATA_UINT64 },
3245450Sbrendan 	{ "l2_feeds",			KSTAT_DATA_UINT64 },
3255450Sbrendan 	{ "l2_rw_clash",		KSTAT_DATA_UINT64 },
3268582SBrendan.Gregg@Sun.COM 	{ "l2_read_bytes",		KSTAT_DATA_UINT64 },
3278582SBrendan.Gregg@Sun.COM 	{ "l2_write_bytes",		KSTAT_DATA_UINT64 },
3285450Sbrendan 	{ "l2_writes_sent",		KSTAT_DATA_UINT64 },
3295450Sbrendan 	{ "l2_writes_done",		KSTAT_DATA_UINT64 },
3305450Sbrendan 	{ "l2_writes_error",		KSTAT_DATA_UINT64 },
3315450Sbrendan 	{ "l2_writes_hdr_miss",		KSTAT_DATA_UINT64 },
3325450Sbrendan 	{ "l2_evict_lock_retry",	KSTAT_DATA_UINT64 },
3335450Sbrendan 	{ "l2_evict_reading",		KSTAT_DATA_UINT64 },
3345450Sbrendan 	{ "l2_free_on_write",		KSTAT_DATA_UINT64 },
3355450Sbrendan 	{ "l2_abort_lowmem",		KSTAT_DATA_UINT64 },
3365450Sbrendan 	{ "l2_cksum_bad",		KSTAT_DATA_UINT64 },
3375450Sbrendan 	{ "l2_io_error",		KSTAT_DATA_UINT64 },
3385450Sbrendan 	{ "l2_size",			KSTAT_DATA_UINT64 },
3396245Smaybee 	{ "l2_hdr_size",		KSTAT_DATA_UINT64 },
3406245Smaybee 	{ "memory_throttle_count",	KSTAT_DATA_UINT64 }
3413403Sbmc };
342789Sahrens 
3433403Sbmc #define	ARCSTAT(stat)	(arc_stats.stat.value.ui64)
3443403Sbmc 
3453403Sbmc #define	ARCSTAT_INCR(stat, val) \
3463403Sbmc 	atomic_add_64(&arc_stats.stat.value.ui64, (val));
3473403Sbmc 
34810922SJeff.Bonwick@Sun.COM #define	ARCSTAT_BUMP(stat)	ARCSTAT_INCR(stat, 1)
3493403Sbmc #define	ARCSTAT_BUMPDOWN(stat)	ARCSTAT_INCR(stat, -1)
3503403Sbmc 
3513403Sbmc #define	ARCSTAT_MAX(stat, val) {					\
3523403Sbmc 	uint64_t m;							\
3533403Sbmc 	while ((val) > (m = arc_stats.stat.value.ui64) &&		\
3543403Sbmc 	    (m != atomic_cas_64(&arc_stats.stat.value.ui64, m, (val))))	\
3553403Sbmc 		continue;						\
3563403Sbmc }
3573403Sbmc 
3583403Sbmc #define	ARCSTAT_MAXSTAT(stat) \
3593403Sbmc 	ARCSTAT_MAX(stat##_max, arc_stats.stat.value.ui64)
360789Sahrens 
3613403Sbmc /*
3623403Sbmc  * We define a macro to allow ARC hits/misses to be easily broken down by
3633403Sbmc  * two separate conditions, giving a total of four different subtypes for
3643403Sbmc  * each of hits and misses (so eight statistics total).
3653403Sbmc  */
3663403Sbmc #define	ARCSTAT_CONDSTAT(cond1, stat1, notstat1, cond2, stat2, notstat2, stat) \
3673403Sbmc 	if (cond1) {							\
3683403Sbmc 		if (cond2) {						\
3693403Sbmc 			ARCSTAT_BUMP(arcstat_##stat1##_##stat2##_##stat); \
3703403Sbmc 		} else {						\
3713403Sbmc 			ARCSTAT_BUMP(arcstat_##stat1##_##notstat2##_##stat); \
3723403Sbmc 		}							\
3733403Sbmc 	} else {							\
3743403Sbmc 		if (cond2) {						\
3753403Sbmc 			ARCSTAT_BUMP(arcstat_##notstat1##_##stat2##_##stat); \
3763403Sbmc 		} else {						\
3773403Sbmc 			ARCSTAT_BUMP(arcstat_##notstat1##_##notstat2##_##stat);\
3783403Sbmc 		}							\
3793403Sbmc 	}
380789Sahrens 
3813403Sbmc kstat_t			*arc_ksp;
38210922SJeff.Bonwick@Sun.COM static arc_state_t	*arc_anon;
3833403Sbmc static arc_state_t	*arc_mru;
3843403Sbmc static arc_state_t	*arc_mru_ghost;
3853403Sbmc static arc_state_t	*arc_mfu;
3863403Sbmc static arc_state_t	*arc_mfu_ghost;
3875450Sbrendan static arc_state_t	*arc_l2c_only;
3883403Sbmc 
3893403Sbmc /*
3903403Sbmc  * There are several ARC variables that are critical to export as kstats --
3913403Sbmc  * but we don't want to have to grovel around in the kstat whenever we wish to
3923403Sbmc  * manipulate them.  For these variables, we therefore define them to be in
3933403Sbmc  * terms of the statistic variable.  This assures that we are not introducing
3943403Sbmc  * the possibility of inconsistency by having shadow copies of the variables,
3953403Sbmc  * while still allowing the code to be readable.
3963403Sbmc  */
3973403Sbmc #define	arc_size	ARCSTAT(arcstat_size)	/* actual total arc size */
3983403Sbmc #define	arc_p		ARCSTAT(arcstat_p)	/* target size of MRU */
3993403Sbmc #define	arc_c		ARCSTAT(arcstat_c)	/* target size of cache */
4003403Sbmc #define	arc_c_min	ARCSTAT(arcstat_c_min)	/* min target cache size */
4013403Sbmc #define	arc_c_max	ARCSTAT(arcstat_c_max)	/* max target cache size */
4023403Sbmc 
4033403Sbmc static int		arc_no_grow;	/* Don't try to grow cache size */
4043403Sbmc static uint64_t		arc_tempreserve;
4059412SAleksandr.Guzovskiy@Sun.COM static uint64_t		arc_loaned_bytes;
4064309Smaybee static uint64_t		arc_meta_used;
4074309Smaybee static uint64_t		arc_meta_limit;
4084309Smaybee static uint64_t		arc_meta_max = 0;
409789Sahrens 
4105450Sbrendan typedef struct l2arc_buf_hdr l2arc_buf_hdr_t;
4115450Sbrendan 
412789Sahrens typedef struct arc_callback arc_callback_t;
413789Sahrens 
414789Sahrens struct arc_callback {
4153547Smaybee 	void			*acb_private;
416789Sahrens 	arc_done_func_t		*acb_done;
417789Sahrens 	arc_buf_t		*acb_buf;
418789Sahrens 	zio_t			*acb_zio_dummy;
419789Sahrens 	arc_callback_t		*acb_next;
420789Sahrens };
421789Sahrens 
4223547Smaybee typedef struct arc_write_callback arc_write_callback_t;
4233547Smaybee 
4243547Smaybee struct arc_write_callback {
4253547Smaybee 	void		*awcb_private;
4263547Smaybee 	arc_done_func_t	*awcb_ready;
4273547Smaybee 	arc_done_func_t	*awcb_done;
4283547Smaybee 	arc_buf_t	*awcb_buf;
4293547Smaybee };
4303547Smaybee 
431789Sahrens struct arc_buf_hdr {
432789Sahrens 	/* protected by hash lock */
433789Sahrens 	dva_t			b_dva;
434789Sahrens 	uint64_t		b_birth;
435789Sahrens 	uint64_t		b_cksum0;
436789Sahrens 
4373093Sahrens 	kmutex_t		b_freeze_lock;
4383093Sahrens 	zio_cksum_t		*b_freeze_cksum;
43912296SLin.Ling@Sun.COM 	void			*b_thawed;
4403093Sahrens 
441789Sahrens 	arc_buf_hdr_t		*b_hash_next;
442789Sahrens 	arc_buf_t		*b_buf;
443789Sahrens 	uint32_t		b_flags;
4441544Seschrock 	uint32_t		b_datacnt;
445789Sahrens 
4463290Sjohansen 	arc_callback_t		*b_acb;
447789Sahrens 	kcondvar_t		b_cv;
4483290Sjohansen 
4493290Sjohansen 	/* immutable */
4503290Sjohansen 	arc_buf_contents_t	b_type;
4513290Sjohansen 	uint64_t		b_size;
4528636SMark.Maybee@Sun.COM 	uint64_t		b_spa;
453789Sahrens 
454789Sahrens 	/* protected by arc state mutex */
455789Sahrens 	arc_state_t		*b_state;
456789Sahrens 	list_node_t		b_arc_node;
457789Sahrens 
458789Sahrens 	/* updated atomically */
459789Sahrens 	clock_t			b_arc_access;
460789Sahrens 
461789Sahrens 	/* self protecting */
462789Sahrens 	refcount_t		b_refcnt;
4635450Sbrendan 
4645450Sbrendan 	l2arc_buf_hdr_t		*b_l2hdr;
4655450Sbrendan 	list_node_t		b_l2node;
466789Sahrens };
467789Sahrens 
4681544Seschrock static arc_buf_t *arc_eviction_list;
4691544Seschrock static kmutex_t arc_eviction_mtx;
4702887Smaybee static arc_buf_hdr_t arc_eviction_hdr;
4712688Smaybee static void arc_get_data_buf(arc_buf_t *buf);
4722688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
4734309Smaybee static int arc_evict_needed(arc_buf_contents_t type);
4748636SMark.Maybee@Sun.COM static void arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes);
4751544Seschrock 
47610357SBrendan.Gregg@Sun.COM static boolean_t l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab);
47710357SBrendan.Gregg@Sun.COM 
4781544Seschrock #define	GHOST_STATE(state)	\
4795450Sbrendan 	((state) == arc_mru_ghost || (state) == arc_mfu_ghost ||	\
4805450Sbrendan 	(state) == arc_l2c_only)
4811544Seschrock 
482789Sahrens /*
483789Sahrens  * Private ARC flags.  These flags are private ARC only flags that will show up
484789Sahrens  * in b_flags in the arc_hdr_buf_t.  Some flags are publicly declared, and can
485789Sahrens  * be passed in as arc_flags in things like arc_read.  However, these flags
486789Sahrens  * should never be passed and should only be set by ARC code.  When adding new
487789Sahrens  * public flags, make sure not to smash the private ones.
488789Sahrens  */
489789Sahrens 
4901544Seschrock #define	ARC_IN_HASH_TABLE	(1 << 9)	/* this buffer is hashed */
491789Sahrens #define	ARC_IO_IN_PROGRESS	(1 << 10)	/* I/O in progress for buf */
492789Sahrens #define	ARC_IO_ERROR		(1 << 11)	/* I/O failed for buf */
493789Sahrens #define	ARC_FREED_IN_READ	(1 << 12)	/* buf freed while in read */
4941544Seschrock #define	ARC_BUF_AVAILABLE	(1 << 13)	/* block not in active use */
4952391Smaybee #define	ARC_INDIRECT		(1 << 14)	/* this is an indirect block */
4965450Sbrendan #define	ARC_FREE_IN_PROGRESS	(1 << 15)	/* hdr about to be freed */
4977237Sek110237 #define	ARC_L2_WRITING		(1 << 16)	/* L2ARC write in progress */
4987237Sek110237 #define	ARC_L2_EVICTED		(1 << 17)	/* evicted during I/O */
4997237Sek110237 #define	ARC_L2_WRITE_HEAD	(1 << 18)	/* head of write list */
500789Sahrens 
5011544Seschrock #define	HDR_IN_HASH_TABLE(hdr)	((hdr)->b_flags & ARC_IN_HASH_TABLE)
502789Sahrens #define	HDR_IO_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS)
503789Sahrens #define	HDR_IO_ERROR(hdr)	((hdr)->b_flags & ARC_IO_ERROR)
5048582SBrendan.Gregg@Sun.COM #define	HDR_PREFETCH(hdr)	((hdr)->b_flags & ARC_PREFETCH)
505789Sahrens #define	HDR_FREED_IN_READ(hdr)	((hdr)->b_flags & ARC_FREED_IN_READ)
5061544Seschrock #define	HDR_BUF_AVAILABLE(hdr)	((hdr)->b_flags & ARC_BUF_AVAILABLE)
5075450Sbrendan #define	HDR_FREE_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_FREE_IN_PROGRESS)
5087237Sek110237 #define	HDR_L2CACHE(hdr)	((hdr)->b_flags & ARC_L2CACHE)
5096987Sbrendan #define	HDR_L2_READING(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS &&	\
5106987Sbrendan 				    (hdr)->b_l2hdr != NULL)
5115450Sbrendan #define	HDR_L2_WRITING(hdr)	((hdr)->b_flags & ARC_L2_WRITING)
5125450Sbrendan #define	HDR_L2_EVICTED(hdr)	((hdr)->b_flags & ARC_L2_EVICTED)
5135450Sbrendan #define	HDR_L2_WRITE_HEAD(hdr)	((hdr)->b_flags & ARC_L2_WRITE_HEAD)
514789Sahrens 
515789Sahrens /*
5166018Sbrendan  * Other sizes
5176018Sbrendan  */
5186018Sbrendan 
5196018Sbrendan #define	HDR_SIZE ((int64_t)sizeof (arc_buf_hdr_t))
5206018Sbrendan #define	L2HDR_SIZE ((int64_t)sizeof (l2arc_buf_hdr_t))
5216018Sbrendan 
5226018Sbrendan /*
523789Sahrens  * Hash table routines
524789Sahrens  */
525789Sahrens 
526789Sahrens #define	HT_LOCK_PAD	64
527789Sahrens 
528789Sahrens struct ht_lock {
529789Sahrens 	kmutex_t	ht_lock;
530789Sahrens #ifdef _KERNEL
531789Sahrens 	unsigned char	pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
532789Sahrens #endif
533789Sahrens };
534789Sahrens 
535789Sahrens #define	BUF_LOCKS 256
536789Sahrens typedef struct buf_hash_table {
537789Sahrens 	uint64_t ht_mask;
538789Sahrens 	arc_buf_hdr_t **ht_table;
539789Sahrens 	struct ht_lock ht_locks[BUF_LOCKS];
540789Sahrens } buf_hash_table_t;
541789Sahrens 
542789Sahrens static buf_hash_table_t buf_hash_table;
543789Sahrens 
544789Sahrens #define	BUF_HASH_INDEX(spa, dva, birth) \
545789Sahrens 	(buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
546789Sahrens #define	BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
547789Sahrens #define	BUF_HASH_LOCK(idx)	(&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
54812296SLin.Ling@Sun.COM #define	HDR_LOCK(hdr) \
54912296SLin.Ling@Sun.COM 	(BUF_HASH_LOCK(BUF_HASH_INDEX(hdr->b_spa, &hdr->b_dva, hdr->b_birth)))
550789Sahrens 
551789Sahrens uint64_t zfs_crc64_table[256];
552789Sahrens 
5535450Sbrendan /*
5545450Sbrendan  * Level 2 ARC
5555450Sbrendan  */
5565450Sbrendan 
5575450Sbrendan #define	L2ARC_WRITE_SIZE	(8 * 1024 * 1024)	/* initial write max */
5588582SBrendan.Gregg@Sun.COM #define	L2ARC_HEADROOM		2		/* num of writes */
5598582SBrendan.Gregg@Sun.COM #define	L2ARC_FEED_SECS		1		/* caching interval secs */
5608582SBrendan.Gregg@Sun.COM #define	L2ARC_FEED_MIN_MS	200		/* min caching interval ms */
5615450Sbrendan 
5625450Sbrendan #define	l2arc_writes_sent	ARCSTAT(arcstat_l2_writes_sent)
5635450Sbrendan #define	l2arc_writes_done	ARCSTAT(arcstat_l2_writes_done)
5645450Sbrendan 
5655450Sbrendan /*
5665450Sbrendan  * L2ARC Performance Tunables
5675450Sbrendan  */
5685450Sbrendan uint64_t l2arc_write_max = L2ARC_WRITE_SIZE;	/* default max write size */
5696987Sbrendan uint64_t l2arc_write_boost = L2ARC_WRITE_SIZE;	/* extra write during warmup */
5705450Sbrendan uint64_t l2arc_headroom = L2ARC_HEADROOM;	/* number of dev writes */
5715450Sbrendan uint64_t l2arc_feed_secs = L2ARC_FEED_SECS;	/* interval seconds */
5728582SBrendan.Gregg@Sun.COM uint64_t l2arc_feed_min_ms = L2ARC_FEED_MIN_MS;	/* min interval milliseconds */
5735450Sbrendan boolean_t l2arc_noprefetch = B_TRUE;		/* don't cache prefetch bufs */
5748582SBrendan.Gregg@Sun.COM boolean_t l2arc_feed_again = B_TRUE;		/* turbo warmup */
5758582SBrendan.Gregg@Sun.COM boolean_t l2arc_norw = B_TRUE;			/* no reads during writes */
5765450Sbrendan 
5775450Sbrendan /*
5785450Sbrendan  * L2ARC Internals
5795450Sbrendan  */
5805450Sbrendan typedef struct l2arc_dev {
5815450Sbrendan 	vdev_t			*l2ad_vdev;	/* vdev */
5825450Sbrendan 	spa_t			*l2ad_spa;	/* spa */
5835450Sbrendan 	uint64_t		l2ad_hand;	/* next write location */
5845450Sbrendan 	uint64_t		l2ad_write;	/* desired write size, bytes */
5856987Sbrendan 	uint64_t		l2ad_boost;	/* warmup write boost, bytes */
5865450Sbrendan 	uint64_t		l2ad_start;	/* first addr on device */
5875450Sbrendan 	uint64_t		l2ad_end;	/* last addr on device */
5885450Sbrendan 	uint64_t		l2ad_evict;	/* last addr eviction reached */
5895450Sbrendan 	boolean_t		l2ad_first;	/* first sweep through */
5908582SBrendan.Gregg@Sun.COM 	boolean_t		l2ad_writing;	/* currently writing */
5915450Sbrendan 	list_t			*l2ad_buflist;	/* buffer list */
5925450Sbrendan 	list_node_t		l2ad_node;	/* device list node */
5935450Sbrendan } l2arc_dev_t;
5945450Sbrendan 
5955450Sbrendan static list_t L2ARC_dev_list;			/* device list */
5965450Sbrendan static list_t *l2arc_dev_list;			/* device list pointer */
5975450Sbrendan static kmutex_t l2arc_dev_mtx;			/* device list mutex */
5985450Sbrendan static l2arc_dev_t *l2arc_dev_last;		/* last device used */
5995450Sbrendan static kmutex_t l2arc_buflist_mtx;		/* mutex for all buflists */
6005450Sbrendan static list_t L2ARC_free_on_write;		/* free after write buf list */
6015450Sbrendan static list_t *l2arc_free_on_write;		/* free after write list ptr */
6025450Sbrendan static kmutex_t l2arc_free_on_write_mtx;	/* mutex for list */
6035450Sbrendan static uint64_t l2arc_ndev;			/* number of devices */
6045450Sbrendan 
6055450Sbrendan typedef struct l2arc_read_callback {
6065450Sbrendan 	arc_buf_t	*l2rcb_buf;		/* read buffer */
6075450Sbrendan 	spa_t		*l2rcb_spa;		/* spa */
6085450Sbrendan 	blkptr_t	l2rcb_bp;		/* original blkptr */
6095450Sbrendan 	zbookmark_t	l2rcb_zb;		/* original bookmark */
6105450Sbrendan 	int		l2rcb_flags;		/* original flags */
6115450Sbrendan } l2arc_read_callback_t;
6125450Sbrendan 
6135450Sbrendan typedef struct l2arc_write_callback {
6145450Sbrendan 	l2arc_dev_t	*l2wcb_dev;		/* device info */
6155450Sbrendan 	arc_buf_hdr_t	*l2wcb_head;		/* head of write buflist */
6165450Sbrendan } l2arc_write_callback_t;
6175450Sbrendan 
6185450Sbrendan struct l2arc_buf_hdr {
6195450Sbrendan 	/* protected by arc_buf_hdr  mutex */
6205450Sbrendan 	l2arc_dev_t	*b_dev;			/* L2ARC device */
6219215SGeorge.Wilson@Sun.COM 	uint64_t	b_daddr;		/* disk address, offset byte */
6225450Sbrendan };
6235450Sbrendan 
6245450Sbrendan typedef struct l2arc_data_free {
6255450Sbrendan 	/* protected by l2arc_free_on_write_mtx */
6265450Sbrendan 	void		*l2df_data;
6275450Sbrendan 	size_t		l2df_size;
6285450Sbrendan 	void		(*l2df_func)(void *, size_t);
6295450Sbrendan 	list_node_t	l2df_list_node;
6305450Sbrendan } l2arc_data_free_t;
6315450Sbrendan 
6325450Sbrendan static kmutex_t l2arc_feed_thr_lock;
6335450Sbrendan static kcondvar_t l2arc_feed_thr_cv;
6345450Sbrendan static uint8_t l2arc_thread_exit;
6355450Sbrendan 
6365450Sbrendan static void l2arc_read_done(zio_t *zio);
6375450Sbrendan static void l2arc_hdr_stat_add(void);
6385450Sbrendan static void l2arc_hdr_stat_remove(void);
6395450Sbrendan 
640789Sahrens static uint64_t
buf_hash(uint64_t spa,const dva_t * dva,uint64_t birth)6418636SMark.Maybee@Sun.COM buf_hash(uint64_t spa, const dva_t *dva, uint64_t birth)
642789Sahrens {
643789Sahrens 	uint8_t *vdva = (uint8_t *)dva;
644789Sahrens 	uint64_t crc = -1ULL;
645789Sahrens 	int i;
646789Sahrens 
647789Sahrens 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
648789Sahrens 
649789Sahrens 	for (i = 0; i < sizeof (dva_t); i++)
650789Sahrens 		crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
651789Sahrens 
6528636SMark.Maybee@Sun.COM 	crc ^= (spa>>8) ^ birth;
653789Sahrens 
654789Sahrens 	return (crc);
655789Sahrens }
656789Sahrens 
657789Sahrens #define	BUF_EMPTY(buf)						\
658789Sahrens 	((buf)->b_dva.dva_word[0] == 0 &&			\
659789Sahrens 	(buf)->b_dva.dva_word[1] == 0 &&			\
660789Sahrens 	(buf)->b_birth == 0)
661789Sahrens 
662789Sahrens #define	BUF_EQUAL(spa, dva, birth, buf)				\
663789Sahrens 	((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) &&	\
664789Sahrens 	((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) &&	\
665789Sahrens 	((buf)->b_birth == birth) && ((buf)->b_spa == spa)
666789Sahrens 
66712296SLin.Ling@Sun.COM static void
buf_discard_identity(arc_buf_hdr_t * hdr)66812296SLin.Ling@Sun.COM buf_discard_identity(arc_buf_hdr_t *hdr)
66912296SLin.Ling@Sun.COM {
67012296SLin.Ling@Sun.COM 	hdr->b_dva.dva_word[0] = 0;
67112296SLin.Ling@Sun.COM 	hdr->b_dva.dva_word[1] = 0;
67212296SLin.Ling@Sun.COM 	hdr->b_birth = 0;
67312296SLin.Ling@Sun.COM 	hdr->b_cksum0 = 0;
67412296SLin.Ling@Sun.COM }
67512296SLin.Ling@Sun.COM 
676789Sahrens static arc_buf_hdr_t *
buf_hash_find(uint64_t spa,const dva_t * dva,uint64_t birth,kmutex_t ** lockp)6778636SMark.Maybee@Sun.COM buf_hash_find(uint64_t spa, const dva_t *dva, uint64_t birth, kmutex_t **lockp)
678789Sahrens {
679789Sahrens 	uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
680789Sahrens 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
681789Sahrens 	arc_buf_hdr_t *buf;
682789Sahrens 
683789Sahrens 	mutex_enter(hash_lock);
684789Sahrens 	for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
685789Sahrens 	    buf = buf->b_hash_next) {
686789Sahrens 		if (BUF_EQUAL(spa, dva, birth, buf)) {
687789Sahrens 			*lockp = hash_lock;
688789Sahrens 			return (buf);
689789Sahrens 		}
690789Sahrens 	}
691789Sahrens 	mutex_exit(hash_lock);
692789Sahrens 	*lockp = NULL;
693789Sahrens 	return (NULL);
694789Sahrens }
695789Sahrens 
696789Sahrens /*
697789Sahrens  * Insert an entry into the hash table.  If there is already an element
698789Sahrens  * equal to elem in the hash table, then the already existing element
699789Sahrens  * will be returned and the new element will not be inserted.
700789Sahrens  * Otherwise returns NULL.
701789Sahrens  */
702789Sahrens static arc_buf_hdr_t *
buf_hash_insert(arc_buf_hdr_t * buf,kmutex_t ** lockp)703789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
704789Sahrens {
705789Sahrens 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
706789Sahrens 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
707789Sahrens 	arc_buf_hdr_t *fbuf;
7083403Sbmc 	uint32_t i;
709789Sahrens 
7101544Seschrock 	ASSERT(!HDR_IN_HASH_TABLE(buf));
711789Sahrens 	*lockp = hash_lock;
712789Sahrens 	mutex_enter(hash_lock);
713789Sahrens 	for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
714789Sahrens 	    fbuf = fbuf->b_hash_next, i++) {
715789Sahrens 		if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
716789Sahrens 			return (fbuf);
717789Sahrens 	}
718789Sahrens 
719789Sahrens 	buf->b_hash_next = buf_hash_table.ht_table[idx];
720789Sahrens 	buf_hash_table.ht_table[idx] = buf;
7211544Seschrock 	buf->b_flags |= ARC_IN_HASH_TABLE;
722789Sahrens 
723789Sahrens 	/* collect some hash table performance data */
724789Sahrens 	if (i > 0) {
7253403Sbmc 		ARCSTAT_BUMP(arcstat_hash_collisions);
726789Sahrens 		if (i == 1)
7273403Sbmc 			ARCSTAT_BUMP(arcstat_hash_chains);
7283403Sbmc 
7293403Sbmc 		ARCSTAT_MAX(arcstat_hash_chain_max, i);
730789Sahrens 	}
7313403Sbmc 
7323403Sbmc 	ARCSTAT_BUMP(arcstat_hash_elements);
7333403Sbmc 	ARCSTAT_MAXSTAT(arcstat_hash_elements);
734789Sahrens 
735789Sahrens 	return (NULL);
736789Sahrens }
737789Sahrens 
738789Sahrens static void
buf_hash_remove(arc_buf_hdr_t * buf)739789Sahrens buf_hash_remove(arc_buf_hdr_t *buf)
740789Sahrens {
741789Sahrens 	arc_buf_hdr_t *fbuf, **bufp;
742789Sahrens 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
743789Sahrens 
744789Sahrens 	ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
7451544Seschrock 	ASSERT(HDR_IN_HASH_TABLE(buf));
746789Sahrens 
747789Sahrens 	bufp = &buf_hash_table.ht_table[idx];
748789Sahrens 	while ((fbuf = *bufp) != buf) {
749789Sahrens 		ASSERT(fbuf != NULL);
750789Sahrens 		bufp = &fbuf->b_hash_next;
751789Sahrens 	}
752789Sahrens 	*bufp = buf->b_hash_next;
753789Sahrens 	buf->b_hash_next = NULL;
7541544Seschrock 	buf->b_flags &= ~ARC_IN_HASH_TABLE;
755789Sahrens 
756789Sahrens 	/* collect some hash table performance data */
7573403Sbmc 	ARCSTAT_BUMPDOWN(arcstat_hash_elements);
7583403Sbmc 
759789Sahrens 	if (buf_hash_table.ht_table[idx] &&
760789Sahrens 	    buf_hash_table.ht_table[idx]->b_hash_next == NULL)
7613403Sbmc 		ARCSTAT_BUMPDOWN(arcstat_hash_chains);
762789Sahrens }
763789Sahrens 
764789Sahrens /*
765789Sahrens  * Global data structures and functions for the buf kmem cache.
766789Sahrens  */
767789Sahrens static kmem_cache_t *hdr_cache;
768789Sahrens static kmem_cache_t *buf_cache;
769789Sahrens 
770789Sahrens static void
buf_fini(void)771789Sahrens buf_fini(void)
772789Sahrens {
773789Sahrens 	int i;
774789Sahrens 
775789Sahrens 	kmem_free(buf_hash_table.ht_table,
776789Sahrens 	    (buf_hash_table.ht_mask + 1) * sizeof (void *));
777789Sahrens 	for (i = 0; i < BUF_LOCKS; i++)
778789Sahrens 		mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
779789Sahrens 	kmem_cache_destroy(hdr_cache);
780789Sahrens 	kmem_cache_destroy(buf_cache);
781789Sahrens }
782789Sahrens 
783789Sahrens /*
784789Sahrens  * Constructor callback - called when the cache is empty
785789Sahrens  * and a new buf is requested.
786789Sahrens  */
787789Sahrens /* ARGSUSED */
788789Sahrens static int
hdr_cons(void * vbuf,void * unused,int kmflag)789789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag)
790789Sahrens {
791789Sahrens 	arc_buf_hdr_t *buf = vbuf;
792789Sahrens 
793789Sahrens 	bzero(buf, sizeof (arc_buf_hdr_t));
794789Sahrens 	refcount_create(&buf->b_refcnt);
795789Sahrens 	cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
7964831Sgw25295 	mutex_init(&buf->b_freeze_lock, NULL, MUTEX_DEFAULT, NULL);
7978582SBrendan.Gregg@Sun.COM 	arc_space_consume(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
7988582SBrendan.Gregg@Sun.COM 
799789Sahrens 	return (0);
800789Sahrens }
801789Sahrens 
8027545SMark.Maybee@Sun.COM /* ARGSUSED */
8037545SMark.Maybee@Sun.COM static int
buf_cons(void * vbuf,void * unused,int kmflag)8047545SMark.Maybee@Sun.COM buf_cons(void *vbuf, void *unused, int kmflag)
8057545SMark.Maybee@Sun.COM {
8067545SMark.Maybee@Sun.COM 	arc_buf_t *buf = vbuf;
8077545SMark.Maybee@Sun.COM 
8087545SMark.Maybee@Sun.COM 	bzero(buf, sizeof (arc_buf_t));
80912296SLin.Ling@Sun.COM 	mutex_init(&buf->b_evict_lock, NULL, MUTEX_DEFAULT, NULL);
81012296SLin.Ling@Sun.COM 	rw_init(&buf->b_data_lock, NULL, RW_DEFAULT, NULL);
8118582SBrendan.Gregg@Sun.COM 	arc_space_consume(sizeof (arc_buf_t), ARC_SPACE_HDRS);
8128582SBrendan.Gregg@Sun.COM 
8137545SMark.Maybee@Sun.COM 	return (0);
8147545SMark.Maybee@Sun.COM }
8157545SMark.Maybee@Sun.COM 
816789Sahrens /*
817789Sahrens  * Destructor callback - called when a cached buf is
818789Sahrens  * no longer required.
819789Sahrens  */
820789Sahrens /* ARGSUSED */
821789Sahrens static void
hdr_dest(void * vbuf,void * unused)822789Sahrens hdr_dest(void *vbuf, void *unused)
823789Sahrens {
824789Sahrens 	arc_buf_hdr_t *buf = vbuf;
825789Sahrens 
82610922SJeff.Bonwick@Sun.COM 	ASSERT(BUF_EMPTY(buf));
827789Sahrens 	refcount_destroy(&buf->b_refcnt);
828789Sahrens 	cv_destroy(&buf->b_cv);
8294831Sgw25295 	mutex_destroy(&buf->b_freeze_lock);
8308582SBrendan.Gregg@Sun.COM 	arc_space_return(sizeof (arc_buf_hdr_t), ARC_SPACE_HDRS);
831789Sahrens }
832789Sahrens 
8337545SMark.Maybee@Sun.COM /* ARGSUSED */
8347545SMark.Maybee@Sun.COM static void
buf_dest(void * vbuf,void * unused)8357545SMark.Maybee@Sun.COM buf_dest(void *vbuf, void *unused)
8367545SMark.Maybee@Sun.COM {
8377545SMark.Maybee@Sun.COM 	arc_buf_t *buf = vbuf;
8387545SMark.Maybee@Sun.COM 
83912296SLin.Ling@Sun.COM 	mutex_destroy(&buf->b_evict_lock);
84012296SLin.Ling@Sun.COM 	rw_destroy(&buf->b_data_lock);
8418582SBrendan.Gregg@Sun.COM 	arc_space_return(sizeof (arc_buf_t), ARC_SPACE_HDRS);
8427545SMark.Maybee@Sun.COM }
8437545SMark.Maybee@Sun.COM 
844789Sahrens /*
845789Sahrens  * Reclaim callback -- invoked when memory is low.
846789Sahrens  */
847789Sahrens /* ARGSUSED */
848789Sahrens static void
hdr_recl(void * unused)849789Sahrens hdr_recl(void *unused)
850789Sahrens {
851789Sahrens 	dprintf("hdr_recl called\n");
8523158Smaybee 	/*
8533158Smaybee 	 * umem calls the reclaim func when we destroy the buf cache,
8543158Smaybee 	 * which is after we do arc_fini().
8553158Smaybee 	 */
8563158Smaybee 	if (!arc_dead)
8573158Smaybee 		cv_signal(&arc_reclaim_thr_cv);
858789Sahrens }
859789Sahrens 
860789Sahrens static void
buf_init(void)861789Sahrens buf_init(void)
862789Sahrens {
863789Sahrens 	uint64_t *ct;
8641544Seschrock 	uint64_t hsize = 1ULL << 12;
865789Sahrens 	int i, j;
866789Sahrens 
867789Sahrens 	/*
868789Sahrens 	 * The hash table is big enough to fill all of physical memory
8691544Seschrock 	 * with an average 64K block size.  The table will take up
8701544Seschrock 	 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
871789Sahrens 	 */
8721544Seschrock 	while (hsize * 65536 < physmem * PAGESIZE)
873789Sahrens 		hsize <<= 1;
8741544Seschrock retry:
875789Sahrens 	buf_hash_table.ht_mask = hsize - 1;
8761544Seschrock 	buf_hash_table.ht_table =
8771544Seschrock 	    kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
8781544Seschrock 	if (buf_hash_table.ht_table == NULL) {
8791544Seschrock 		ASSERT(hsize > (1ULL << 8));
8801544Seschrock 		hsize >>= 1;
8811544Seschrock 		goto retry;
8821544Seschrock 	}
883789Sahrens 
884789Sahrens 	hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
885789Sahrens 	    0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
886789Sahrens 	buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
8877545SMark.Maybee@Sun.COM 	    0, buf_cons, buf_dest, NULL, NULL, NULL, 0);
888789Sahrens 
889789Sahrens 	for (i = 0; i < 256; i++)
890789Sahrens 		for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
891789Sahrens 			*ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
892789Sahrens 
893789Sahrens 	for (i = 0; i < BUF_LOCKS; i++) {
894789Sahrens 		mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
895789Sahrens 		    NULL, MUTEX_DEFAULT, NULL);
896789Sahrens 	}
897789Sahrens }
898789Sahrens 
899789Sahrens #define	ARC_MINTIME	(hz>>4) /* 62 ms */
900789Sahrens 
901789Sahrens static void
arc_cksum_verify(arc_buf_t * buf)9023093Sahrens arc_cksum_verify(arc_buf_t *buf)
9033093Sahrens {
9043093Sahrens 	zio_cksum_t zc;
9053093Sahrens 
9063312Sahrens 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
9073093Sahrens 		return;
9083093Sahrens 
9093093Sahrens 	mutex_enter(&buf->b_hdr->b_freeze_lock);
9103265Sahrens 	if (buf->b_hdr->b_freeze_cksum == NULL ||
9113265Sahrens 	    (buf->b_hdr->b_flags & ARC_IO_ERROR)) {
9123093Sahrens 		mutex_exit(&buf->b_hdr->b_freeze_lock);
9133093Sahrens 		return;
9143093Sahrens 	}
9153093Sahrens 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
9163093Sahrens 	if (!ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc))
9173093Sahrens 		panic("buffer modified while frozen!");
9183093Sahrens 	mutex_exit(&buf->b_hdr->b_freeze_lock);
9193093Sahrens }
9203093Sahrens 
9215450Sbrendan static int
arc_cksum_equal(arc_buf_t * buf)9225450Sbrendan arc_cksum_equal(arc_buf_t *buf)
9235450Sbrendan {
9245450Sbrendan 	zio_cksum_t zc;
9255450Sbrendan 	int equal;
9265450Sbrendan 
9275450Sbrendan 	mutex_enter(&buf->b_hdr->b_freeze_lock);
9285450Sbrendan 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size, &zc);
9295450Sbrendan 	equal = ZIO_CHECKSUM_EQUAL(*buf->b_hdr->b_freeze_cksum, zc);
9305450Sbrendan 	mutex_exit(&buf->b_hdr->b_freeze_lock);
9315450Sbrendan 
9325450Sbrendan 	return (equal);
9335450Sbrendan }
9345450Sbrendan 
9353093Sahrens static void
arc_cksum_compute(arc_buf_t * buf,boolean_t force)9365450Sbrendan arc_cksum_compute(arc_buf_t *buf, boolean_t force)
9373093Sahrens {
9385450Sbrendan 	if (!force && !(zfs_flags & ZFS_DEBUG_MODIFY))
9393093Sahrens 		return;
9403093Sahrens 
9413093Sahrens 	mutex_enter(&buf->b_hdr->b_freeze_lock);
9423093Sahrens 	if (buf->b_hdr->b_freeze_cksum != NULL) {
9433093Sahrens 		mutex_exit(&buf->b_hdr->b_freeze_lock);
9443093Sahrens 		return;
9453093Sahrens 	}
9463093Sahrens 	buf->b_hdr->b_freeze_cksum = kmem_alloc(sizeof (zio_cksum_t), KM_SLEEP);
9473093Sahrens 	fletcher_2_native(buf->b_data, buf->b_hdr->b_size,
9483093Sahrens 	    buf->b_hdr->b_freeze_cksum);
9493093Sahrens 	mutex_exit(&buf->b_hdr->b_freeze_lock);
9503093Sahrens }
9513093Sahrens 
9523093Sahrens void
arc_buf_thaw(arc_buf_t * buf)9533093Sahrens arc_buf_thaw(arc_buf_t *buf)
9543093Sahrens {
9555450Sbrendan 	if (zfs_flags & ZFS_DEBUG_MODIFY) {
9565450Sbrendan 		if (buf->b_hdr->b_state != arc_anon)
9575450Sbrendan 			panic("modifying non-anon buffer!");
9585450Sbrendan 		if (buf->b_hdr->b_flags & ARC_IO_IN_PROGRESS)
9595450Sbrendan 			panic("modifying buffer while i/o in progress!");
9605450Sbrendan 		arc_cksum_verify(buf);
9615450Sbrendan 	}
9625450Sbrendan 
9633093Sahrens 	mutex_enter(&buf->b_hdr->b_freeze_lock);
9643093Sahrens 	if (buf->b_hdr->b_freeze_cksum != NULL) {
9653093Sahrens 		kmem_free(buf->b_hdr->b_freeze_cksum, sizeof (zio_cksum_t));
9663093Sahrens 		buf->b_hdr->b_freeze_cksum = NULL;
9673093Sahrens 	}
96812296SLin.Ling@Sun.COM 
96912296SLin.Ling@Sun.COM 	if (zfs_flags & ZFS_DEBUG_MODIFY) {
97012296SLin.Ling@Sun.COM 		if (buf->b_hdr->b_thawed)
97112296SLin.Ling@Sun.COM 			kmem_free(buf->b_hdr->b_thawed, 1);
97212296SLin.Ling@Sun.COM 		buf->b_hdr->b_thawed = kmem_alloc(1, KM_SLEEP);
97312296SLin.Ling@Sun.COM 	}
97412296SLin.Ling@Sun.COM 
9753093Sahrens 	mutex_exit(&buf->b_hdr->b_freeze_lock);
9763093Sahrens }
9773093Sahrens 
9783093Sahrens void
arc_buf_freeze(arc_buf_t * buf)9793093Sahrens arc_buf_freeze(arc_buf_t *buf)
9803093Sahrens {
98112296SLin.Ling@Sun.COM 	kmutex_t *hash_lock;
98212296SLin.Ling@Sun.COM 
9833312Sahrens 	if (!(zfs_flags & ZFS_DEBUG_MODIFY))
9843312Sahrens 		return;
9853312Sahrens 
98612296SLin.Ling@Sun.COM 	hash_lock = HDR_LOCK(buf->b_hdr);
98712296SLin.Ling@Sun.COM 	mutex_enter(hash_lock);
98812296SLin.Ling@Sun.COM 
9893093Sahrens 	ASSERT(buf->b_hdr->b_freeze_cksum != NULL ||
9903403Sbmc 	    buf->b_hdr->b_state == arc_anon);
9915450Sbrendan 	arc_cksum_compute(buf, B_FALSE);
99212296SLin.Ling@Sun.COM 	mutex_exit(hash_lock);
9933093Sahrens }
9943093Sahrens 
9953093Sahrens static void
add_reference(arc_buf_hdr_t * ab,kmutex_t * hash_lock,void * tag)996789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
997789Sahrens {
998789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
999789Sahrens 
1000789Sahrens 	if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
10013403Sbmc 	    (ab->b_state != arc_anon)) {
10023700Sek110237 		uint64_t delta = ab->b_size * ab->b_datacnt;
10034309Smaybee 		list_t *list = &ab->b_state->arcs_list[ab->b_type];
10044309Smaybee 		uint64_t *size = &ab->b_state->arcs_lsize[ab->b_type];
1005789Sahrens 
10063403Sbmc 		ASSERT(!MUTEX_HELD(&ab->b_state->arcs_mtx));
10073403Sbmc 		mutex_enter(&ab->b_state->arcs_mtx);
1008789Sahrens 		ASSERT(list_link_active(&ab->b_arc_node));
10094309Smaybee 		list_remove(list, ab);
10101544Seschrock 		if (GHOST_STATE(ab->b_state)) {
10111544Seschrock 			ASSERT3U(ab->b_datacnt, ==, 0);
10121544Seschrock 			ASSERT3P(ab->b_buf, ==, NULL);
10131544Seschrock 			delta = ab->b_size;
10141544Seschrock 		}
10151544Seschrock 		ASSERT(delta > 0);
10164309Smaybee 		ASSERT3U(*size, >=, delta);
10174309Smaybee 		atomic_add_64(size, -delta);
10183403Sbmc 		mutex_exit(&ab->b_state->arcs_mtx);
10197046Sahrens 		/* remove the prefetch flag if we get a reference */
10202391Smaybee 		if (ab->b_flags & ARC_PREFETCH)
10212391Smaybee 			ab->b_flags &= ~ARC_PREFETCH;
1022789Sahrens 	}
1023789Sahrens }
1024789Sahrens 
1025789Sahrens static int
remove_reference(arc_buf_hdr_t * ab,kmutex_t * hash_lock,void * tag)1026789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
1027789Sahrens {
1028789Sahrens 	int cnt;
10293403Sbmc 	arc_state_t *state = ab->b_state;
1030789Sahrens 
10313403Sbmc 	ASSERT(state == arc_anon || MUTEX_HELD(hash_lock));
10323403Sbmc 	ASSERT(!GHOST_STATE(state));
1033789Sahrens 
1034789Sahrens 	if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
10353403Sbmc 	    (state != arc_anon)) {
10364309Smaybee 		uint64_t *size = &state->arcs_lsize[ab->b_type];
10374309Smaybee 
10383403Sbmc 		ASSERT(!MUTEX_HELD(&state->arcs_mtx));
10393403Sbmc 		mutex_enter(&state->arcs_mtx);
1040789Sahrens 		ASSERT(!list_link_active(&ab->b_arc_node));
10414309Smaybee 		list_insert_head(&state->arcs_list[ab->b_type], ab);
10421544Seschrock 		ASSERT(ab->b_datacnt > 0);
10434309Smaybee 		atomic_add_64(size, ab->b_size * ab->b_datacnt);
10443403Sbmc 		mutex_exit(&state->arcs_mtx);
1045789Sahrens 	}
1046789Sahrens 	return (cnt);
1047789Sahrens }
1048789Sahrens 
1049789Sahrens /*
1050789Sahrens  * Move the supplied buffer to the indicated state.  The mutex
1051789Sahrens  * for the buffer must be held by the caller.
1052789Sahrens  */
1053789Sahrens static void
arc_change_state(arc_state_t * new_state,arc_buf_hdr_t * ab,kmutex_t * hash_lock)10541544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
1055789Sahrens {
10561544Seschrock 	arc_state_t *old_state = ab->b_state;
10573700Sek110237 	int64_t refcnt = refcount_count(&ab->b_refcnt);
10583700Sek110237 	uint64_t from_delta, to_delta;
1059789Sahrens 
1060789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
10611544Seschrock 	ASSERT(new_state != old_state);
10621544Seschrock 	ASSERT(refcnt == 0 || ab->b_datacnt > 0);
10631544Seschrock 	ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
106410922SJeff.Bonwick@Sun.COM 	ASSERT(ab->b_datacnt <= 1 || old_state != arc_anon);
10651544Seschrock 
10661544Seschrock 	from_delta = to_delta = ab->b_datacnt * ab->b_size;
1067789Sahrens 
1068789Sahrens 	/*
1069789Sahrens 	 * If this buffer is evictable, transfer it from the
1070789Sahrens 	 * old state list to the new state list.
1071789Sahrens 	 */
10721544Seschrock 	if (refcnt == 0) {
10733403Sbmc 		if (old_state != arc_anon) {
10743403Sbmc 			int use_mutex = !MUTEX_HELD(&old_state->arcs_mtx);
10754309Smaybee 			uint64_t *size = &old_state->arcs_lsize[ab->b_type];
10761544Seschrock 
10771544Seschrock 			if (use_mutex)
10783403Sbmc 				mutex_enter(&old_state->arcs_mtx);
10791544Seschrock 
10801544Seschrock 			ASSERT(list_link_active(&ab->b_arc_node));
10814309Smaybee 			list_remove(&old_state->arcs_list[ab->b_type], ab);
1082789Sahrens 
10832391Smaybee 			/*
10842391Smaybee 			 * If prefetching out of the ghost cache,
108512296SLin.Ling@Sun.COM 			 * we will have a non-zero datacnt.
10862391Smaybee 			 */
10872391Smaybee 			if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
10882391Smaybee 				/* ghost elements have a ghost size */
10891544Seschrock 				ASSERT(ab->b_buf == NULL);
10901544Seschrock 				from_delta = ab->b_size;
1091789Sahrens 			}
10924309Smaybee 			ASSERT3U(*size, >=, from_delta);
10934309Smaybee 			atomic_add_64(size, -from_delta);
10941544Seschrock 
10951544Seschrock 			if (use_mutex)
10963403Sbmc 				mutex_exit(&old_state->arcs_mtx);
1097789Sahrens 		}
10983403Sbmc 		if (new_state != arc_anon) {
10993403Sbmc 			int use_mutex = !MUTEX_HELD(&new_state->arcs_mtx);
11004309Smaybee 			uint64_t *size = &new_state->arcs_lsize[ab->b_type];
1101789Sahrens 
11021544Seschrock 			if (use_mutex)
11033403Sbmc 				mutex_enter(&new_state->arcs_mtx);
11041544Seschrock 
11054309Smaybee 			list_insert_head(&new_state->arcs_list[ab->b_type], ab);
11061544Seschrock 
11071544Seschrock 			/* ghost elements have a ghost size */
11081544Seschrock 			if (GHOST_STATE(new_state)) {
11091544Seschrock 				ASSERT(ab->b_datacnt == 0);
11101544Seschrock 				ASSERT(ab->b_buf == NULL);
11111544Seschrock 				to_delta = ab->b_size;
11121544Seschrock 			}
11134309Smaybee 			atomic_add_64(size, to_delta);
11141544Seschrock 
11151544Seschrock 			if (use_mutex)
11163403Sbmc 				mutex_exit(&new_state->arcs_mtx);
1117789Sahrens 		}
1118789Sahrens 	}
1119789Sahrens 
1120789Sahrens 	ASSERT(!BUF_EMPTY(ab));
112112296SLin.Ling@Sun.COM 	if (new_state == arc_anon && HDR_IN_HASH_TABLE(ab))
1122789Sahrens 		buf_hash_remove(ab);
1123789Sahrens 
11241544Seschrock 	/* adjust state sizes */
11251544Seschrock 	if (to_delta)
11263403Sbmc 		atomic_add_64(&new_state->arcs_size, to_delta);
11271544Seschrock 	if (from_delta) {
11283403Sbmc 		ASSERT3U(old_state->arcs_size, >=, from_delta);
11293403Sbmc 		atomic_add_64(&old_state->arcs_size, -from_delta);
1130789Sahrens 	}
1131789Sahrens 	ab->b_state = new_state;
11325450Sbrendan 
11335450Sbrendan 	/* adjust l2arc hdr stats */
11345450Sbrendan 	if (new_state == arc_l2c_only)
11355450Sbrendan 		l2arc_hdr_stat_add();
11365450Sbrendan 	else if (old_state == arc_l2c_only)
11375450Sbrendan 		l2arc_hdr_stat_remove();
1138789Sahrens }
1139789Sahrens 
11404309Smaybee void
arc_space_consume(uint64_t space,arc_space_type_t type)11418582SBrendan.Gregg@Sun.COM arc_space_consume(uint64_t space, arc_space_type_t type)
11424309Smaybee {
11438582SBrendan.Gregg@Sun.COM 	ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
11448582SBrendan.Gregg@Sun.COM 
11458582SBrendan.Gregg@Sun.COM 	switch (type) {
11468582SBrendan.Gregg@Sun.COM 	case ARC_SPACE_DATA:
11478582SBrendan.Gregg@Sun.COM 		ARCSTAT_INCR(arcstat_data_size, space);
11488582SBrendan.Gregg@Sun.COM 		break;
11498582SBrendan.Gregg@Sun.COM 	case ARC_SPACE_OTHER:
11508582SBrendan.Gregg@Sun.COM 		ARCSTAT_INCR(arcstat_other_size, space);
11518582SBrendan.Gregg@Sun.COM 		break;
11528582SBrendan.Gregg@Sun.COM 	case ARC_SPACE_HDRS:
11538582SBrendan.Gregg@Sun.COM 		ARCSTAT_INCR(arcstat_hdr_size, space);
11548582SBrendan.Gregg@Sun.COM 		break;
11558582SBrendan.Gregg@Sun.COM 	case ARC_SPACE_L2HDRS:
11568582SBrendan.Gregg@Sun.COM 		ARCSTAT_INCR(arcstat_l2_hdr_size, space);
11578582SBrendan.Gregg@Sun.COM 		break;
11588582SBrendan.Gregg@Sun.COM 	}
11598582SBrendan.Gregg@Sun.COM 
11604309Smaybee 	atomic_add_64(&arc_meta_used, space);
11614309Smaybee 	atomic_add_64(&arc_size, space);
11624309Smaybee }
11634309Smaybee 
11644309Smaybee void
arc_space_return(uint64_t space,arc_space_type_t type)11658582SBrendan.Gregg@Sun.COM arc_space_return(uint64_t space, arc_space_type_t type)
11664309Smaybee {
11678582SBrendan.Gregg@Sun.COM 	ASSERT(type >= 0 && type < ARC_SPACE_NUMTYPES);
11688582SBrendan.Gregg@Sun.COM 
11698582SBrendan.Gregg@Sun.COM 	switch (type) {
11708582SBrendan.Gregg@Sun.COM 	case ARC_SPACE_DATA:
11718582SBrendan.Gregg@Sun.COM 		ARCSTAT_INCR(arcstat_data_size, -space);
11728582SBrendan.Gregg@Sun.COM 		break;
11738582SBrendan.Gregg@Sun.COM 	case ARC_SPACE_OTHER:
11748582SBrendan.Gregg@Sun.COM 		ARCSTAT_INCR(arcstat_other_size, -space);
11758582SBrendan.Gregg@Sun.COM 		break;
11768582SBrendan.Gregg@Sun.COM 	case ARC_SPACE_HDRS:
11778582SBrendan.Gregg@Sun.COM 		ARCSTAT_INCR(arcstat_hdr_size, -space);
11788582SBrendan.Gregg@Sun.COM 		break;
11798582SBrendan.Gregg@Sun.COM 	case ARC_SPACE_L2HDRS:
11808582SBrendan.Gregg@Sun.COM 		ARCSTAT_INCR(arcstat_l2_hdr_size, -space);
11818582SBrendan.Gregg@Sun.COM 		break;
11828582SBrendan.Gregg@Sun.COM 	}
11838582SBrendan.Gregg@Sun.COM 
11844309Smaybee 	ASSERT(arc_meta_used >= space);
11854309Smaybee 	if (arc_meta_max < arc_meta_used)
11864309Smaybee 		arc_meta_max = arc_meta_used;
11874309Smaybee 	atomic_add_64(&arc_meta_used, -space);
11884309Smaybee 	ASSERT(arc_size >= space);
11894309Smaybee 	atomic_add_64(&arc_size, -space);
11904309Smaybee }
11914309Smaybee 
11924309Smaybee void *
arc_data_buf_alloc(uint64_t size)11934309Smaybee arc_data_buf_alloc(uint64_t size)
11944309Smaybee {
11954309Smaybee 	if (arc_evict_needed(ARC_BUFC_DATA))
11964309Smaybee 		cv_signal(&arc_reclaim_thr_cv);
11974309Smaybee 	atomic_add_64(&arc_size, size);
11984309Smaybee 	return (zio_data_buf_alloc(size));
11994309Smaybee }
12004309Smaybee 
12014309Smaybee void
arc_data_buf_free(void * buf,uint64_t size)12024309Smaybee arc_data_buf_free(void *buf, uint64_t size)
12034309Smaybee {
12044309Smaybee 	zio_data_buf_free(buf, size);
12054309Smaybee 	ASSERT(arc_size >= size);
12064309Smaybee 	atomic_add_64(&arc_size, -size);
12074309Smaybee }
12084309Smaybee 
1209789Sahrens arc_buf_t *
arc_buf_alloc(spa_t * spa,int size,void * tag,arc_buf_contents_t type)12103290Sjohansen arc_buf_alloc(spa_t *spa, int size, void *tag, arc_buf_contents_t type)
1211789Sahrens {
1212789Sahrens 	arc_buf_hdr_t *hdr;
1213789Sahrens 	arc_buf_t *buf;
1214789Sahrens 
1215789Sahrens 	ASSERT3U(size, >, 0);
12166245Smaybee 	hdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
1217789Sahrens 	ASSERT(BUF_EMPTY(hdr));
1218789Sahrens 	hdr->b_size = size;
12193290Sjohansen 	hdr->b_type = type;
12208636SMark.Maybee@Sun.COM 	hdr->b_spa = spa_guid(spa);
12213403Sbmc 	hdr->b_state = arc_anon;
1222789Sahrens 	hdr->b_arc_access = 0;
12236245Smaybee 	buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
1224789Sahrens 	buf->b_hdr = hdr;
12252688Smaybee 	buf->b_data = NULL;
12261544Seschrock 	buf->b_efunc = NULL;
12271544Seschrock 	buf->b_private = NULL;
1228789Sahrens 	buf->b_next = NULL;
1229789Sahrens 	hdr->b_buf = buf;
12302688Smaybee 	arc_get_data_buf(buf);
12311544Seschrock 	hdr->b_datacnt = 1;
1232789Sahrens 	hdr->b_flags = 0;
1233789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
1234789Sahrens 	(void) refcount_add(&hdr->b_refcnt, tag);
1235789Sahrens 
1236789Sahrens 	return (buf);
1237789Sahrens }
1238789Sahrens 
12399412SAleksandr.Guzovskiy@Sun.COM static char *arc_onloan_tag = "onloan";
12409412SAleksandr.Guzovskiy@Sun.COM 
12419412SAleksandr.Guzovskiy@Sun.COM /*
12429412SAleksandr.Guzovskiy@Sun.COM  * Loan out an anonymous arc buffer. Loaned buffers are not counted as in
12439412SAleksandr.Guzovskiy@Sun.COM  * flight data by arc_tempreserve_space() until they are "returned". Loaned
12449412SAleksandr.Guzovskiy@Sun.COM  * buffers must be returned to the arc before they can be used by the DMU or
12459412SAleksandr.Guzovskiy@Sun.COM  * freed.
12469412SAleksandr.Guzovskiy@Sun.COM  */
12479412SAleksandr.Guzovskiy@Sun.COM arc_buf_t *
arc_loan_buf(spa_t * spa,int size)12489412SAleksandr.Guzovskiy@Sun.COM arc_loan_buf(spa_t *spa, int size)
12499412SAleksandr.Guzovskiy@Sun.COM {
12509412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_t *buf;
12519412SAleksandr.Guzovskiy@Sun.COM 
12529412SAleksandr.Guzovskiy@Sun.COM 	buf = arc_buf_alloc(spa, size, arc_onloan_tag, ARC_BUFC_DATA);
12539412SAleksandr.Guzovskiy@Sun.COM 
12549412SAleksandr.Guzovskiy@Sun.COM 	atomic_add_64(&arc_loaned_bytes, size);
12559412SAleksandr.Guzovskiy@Sun.COM 	return (buf);
12569412SAleksandr.Guzovskiy@Sun.COM }
12579412SAleksandr.Guzovskiy@Sun.COM 
12589412SAleksandr.Guzovskiy@Sun.COM /*
12599412SAleksandr.Guzovskiy@Sun.COM  * Return a loaned arc buffer to the arc.
12609412SAleksandr.Guzovskiy@Sun.COM  */
12619412SAleksandr.Guzovskiy@Sun.COM void
arc_return_buf(arc_buf_t * buf,void * tag)12629412SAleksandr.Guzovskiy@Sun.COM arc_return_buf(arc_buf_t *buf, void *tag)
12639412SAleksandr.Guzovskiy@Sun.COM {
12649412SAleksandr.Guzovskiy@Sun.COM 	arc_buf_hdr_t *hdr = buf->b_hdr;
12659412SAleksandr.Guzovskiy@Sun.COM 
12669412SAleksandr.Guzovskiy@Sun.COM 	ASSERT(buf->b_data != NULL);
126711539SChunli.Zhang@Sun.COM 	(void) refcount_add(&hdr->b_refcnt, tag);
126811539SChunli.Zhang@Sun.COM 	(void) refcount_remove(&hdr->b_refcnt, arc_onloan_tag);
12699412SAleksandr.Guzovskiy@Sun.COM 
12709412SAleksandr.Guzovskiy@Sun.COM 	atomic_add_64(&arc_loaned_bytes, -hdr->b_size);
12719412SAleksandr.Guzovskiy@Sun.COM }
12729412SAleksandr.Guzovskiy@Sun.COM 
127311539SChunli.Zhang@Sun.COM /* Detach an arc_buf from a dbuf (tag) */
127411539SChunli.Zhang@Sun.COM void
arc_loan_inuse_buf(arc_buf_t * buf,void * tag)127511539SChunli.Zhang@Sun.COM arc_loan_inuse_buf(arc_buf_t *buf, void *tag)
127611539SChunli.Zhang@Sun.COM {
127711539SChunli.Zhang@Sun.COM 	arc_buf_hdr_t *hdr;
127811539SChunli.Zhang@Sun.COM 
127911539SChunli.Zhang@Sun.COM 	ASSERT(buf->b_data != NULL);
128011539SChunli.Zhang@Sun.COM 	hdr = buf->b_hdr;
128111539SChunli.Zhang@Sun.COM 	(void) refcount_add(&hdr->b_refcnt, arc_onloan_tag);
128211539SChunli.Zhang@Sun.COM 	(void) refcount_remove(&hdr->b_refcnt, tag);
128311539SChunli.Zhang@Sun.COM 	buf->b_efunc = NULL;
128411539SChunli.Zhang@Sun.COM 	buf->b_private = NULL;
128511539SChunli.Zhang@Sun.COM 
128611539SChunli.Zhang@Sun.COM 	atomic_add_64(&arc_loaned_bytes, hdr->b_size);
128711539SChunli.Zhang@Sun.COM }
128811539SChunli.Zhang@Sun.COM 
12892688Smaybee static arc_buf_t *
arc_buf_clone(arc_buf_t * from)12902688Smaybee arc_buf_clone(arc_buf_t *from)
12911544Seschrock {
12922688Smaybee 	arc_buf_t *buf;
12932688Smaybee 	arc_buf_hdr_t *hdr = from->b_hdr;
12942688Smaybee 	uint64_t size = hdr->b_size;
12951544Seschrock 
129610922SJeff.Bonwick@Sun.COM 	ASSERT(hdr->b_state != arc_anon);
129710922SJeff.Bonwick@Sun.COM 
12986245Smaybee 	buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
12992688Smaybee 	buf->b_hdr = hdr;
13002688Smaybee 	buf->b_data = NULL;
13012688Smaybee 	buf->b_efunc = NULL;
13022688Smaybee 	buf->b_private = NULL;
13032688Smaybee 	buf->b_next = hdr->b_buf;
13042688Smaybee 	hdr->b_buf = buf;
13052688Smaybee 	arc_get_data_buf(buf);
13062688Smaybee 	bcopy(from->b_data, buf->b_data, size);
13072688Smaybee 	hdr->b_datacnt += 1;
13082688Smaybee 	return (buf);
13091544Seschrock }
13101544Seschrock 
13111544Seschrock void
arc_buf_add_ref(arc_buf_t * buf,void * tag)13121544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag)
13131544Seschrock {
13142887Smaybee 	arc_buf_hdr_t *hdr;
13151544Seschrock 	kmutex_t *hash_lock;
13161544Seschrock 
13172724Smaybee 	/*
13187545SMark.Maybee@Sun.COM 	 * Check to see if this buffer is evicted.  Callers
13197545SMark.Maybee@Sun.COM 	 * must verify b_data != NULL to know if the add_ref
13207545SMark.Maybee@Sun.COM 	 * was successful.
13212724Smaybee 	 */
132212296SLin.Ling@Sun.COM 	mutex_enter(&buf->b_evict_lock);
13237545SMark.Maybee@Sun.COM 	if (buf->b_data == NULL) {
132412296SLin.Ling@Sun.COM 		mutex_exit(&buf->b_evict_lock);
13252724Smaybee 		return;
13262887Smaybee 	}
132712296SLin.Ling@Sun.COM 	hash_lock = HDR_LOCK(buf->b_hdr);
132812296SLin.Ling@Sun.COM 	mutex_enter(hash_lock);
13297545SMark.Maybee@Sun.COM 	hdr = buf->b_hdr;
133012296SLin.Ling@Sun.COM 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
133112296SLin.Ling@Sun.COM 	mutex_exit(&buf->b_evict_lock);
13327545SMark.Maybee@Sun.COM 
13333403Sbmc 	ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
13341544Seschrock 	add_reference(hdr, hash_lock, tag);
13358582SBrendan.Gregg@Sun.COM 	DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
13362688Smaybee 	arc_access(hdr, hash_lock);
13372688Smaybee 	mutex_exit(hash_lock);
13383403Sbmc 	ARCSTAT_BUMP(arcstat_hits);
13393403Sbmc 	ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
13403403Sbmc 	    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
13413403Sbmc 	    data, metadata, hits);
13421544Seschrock }
13431544Seschrock 
13445450Sbrendan /*
13455450Sbrendan  * Free the arc data buffer.  If it is an l2arc write in progress,
13465450Sbrendan  * the buffer is placed on l2arc_free_on_write to be freed later.
13475450Sbrendan  */
13485450Sbrendan static void
arc_buf_data_free(arc_buf_hdr_t * hdr,void (* free_func)(void *,size_t),void * data,size_t size)13495450Sbrendan arc_buf_data_free(arc_buf_hdr_t *hdr, void (*free_func)(void *, size_t),
13505450Sbrendan     void *data, size_t size)
13515450Sbrendan {
13525450Sbrendan 	if (HDR_L2_WRITING(hdr)) {
13535450Sbrendan 		l2arc_data_free_t *df;
13545450Sbrendan 		df = kmem_alloc(sizeof (l2arc_data_free_t), KM_SLEEP);
13555450Sbrendan 		df->l2df_data = data;
13565450Sbrendan 		df->l2df_size = size;
13575450Sbrendan 		df->l2df_func = free_func;
13585450Sbrendan 		mutex_enter(&l2arc_free_on_write_mtx);
13595450Sbrendan 		list_insert_head(l2arc_free_on_write, df);
13605450Sbrendan 		mutex_exit(&l2arc_free_on_write_mtx);
13615450Sbrendan 		ARCSTAT_BUMP(arcstat_l2_free_on_write);
13625450Sbrendan 	} else {
13635450Sbrendan 		free_func(data, size);
13645450Sbrendan 	}
13655450Sbrendan }
13665450Sbrendan 
1367789Sahrens static void
arc_buf_destroy(arc_buf_t * buf,boolean_t recycle,boolean_t all)13682688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
13691544Seschrock {
13701544Seschrock 	arc_buf_t **bufp;
13711544Seschrock 
13721544Seschrock 	/* free up data associated with the buf */
13731544Seschrock 	if (buf->b_data) {
13741544Seschrock 		arc_state_t *state = buf->b_hdr->b_state;
13751544Seschrock 		uint64_t size = buf->b_hdr->b_size;
13763290Sjohansen 		arc_buf_contents_t type = buf->b_hdr->b_type;
13771544Seschrock 
13783093Sahrens 		arc_cksum_verify(buf);
137910922SJeff.Bonwick@Sun.COM 
13802688Smaybee 		if (!recycle) {
13813290Sjohansen 			if (type == ARC_BUFC_METADATA) {
13825450Sbrendan 				arc_buf_data_free(buf->b_hdr, zio_buf_free,
13835450Sbrendan 				    buf->b_data, size);
13848582SBrendan.Gregg@Sun.COM 				arc_space_return(size, ARC_SPACE_DATA);
13853290Sjohansen 			} else {
13863290Sjohansen 				ASSERT(type == ARC_BUFC_DATA);
13875450Sbrendan 				arc_buf_data_free(buf->b_hdr,
13885450Sbrendan 				    zio_data_buf_free, buf->b_data, size);
13898582SBrendan.Gregg@Sun.COM 				ARCSTAT_INCR(arcstat_data_size, -size);
13904309Smaybee 				atomic_add_64(&arc_size, -size);
13913290Sjohansen 			}
13922688Smaybee 		}
13931544Seschrock 		if (list_link_active(&buf->b_hdr->b_arc_node)) {
13944309Smaybee 			uint64_t *cnt = &state->arcs_lsize[type];
13954309Smaybee 
13961544Seschrock 			ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
13973403Sbmc 			ASSERT(state != arc_anon);
13984309Smaybee 
13994309Smaybee 			ASSERT3U(*cnt, >=, size);
14004309Smaybee 			atomic_add_64(cnt, -size);
14011544Seschrock 		}
14023403Sbmc 		ASSERT3U(state->arcs_size, >=, size);
14033403Sbmc 		atomic_add_64(&state->arcs_size, -size);
14041544Seschrock 		buf->b_data = NULL;
14051544Seschrock 		ASSERT(buf->b_hdr->b_datacnt > 0);
14061544Seschrock 		buf->b_hdr->b_datacnt -= 1;
14071544Seschrock 	}
14081544Seschrock 
14091544Seschrock 	/* only remove the buf if requested */
14101544Seschrock 	if (!all)
14111544Seschrock 		return;
14121544Seschrock 
14131544Seschrock 	/* remove the buf from the hdr list */
14141544Seschrock 	for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
14151544Seschrock 		continue;
14161544Seschrock 	*bufp = buf->b_next;
141712296SLin.Ling@Sun.COM 	buf->b_next = NULL;
14181544Seschrock 
14191544Seschrock 	ASSERT(buf->b_efunc == NULL);
14201544Seschrock 
14211544Seschrock 	/* clean up the buf */
14221544Seschrock 	buf->b_hdr = NULL;
14231544Seschrock 	kmem_cache_free(buf_cache, buf);
14241544Seschrock }
14251544Seschrock 
14261544Seschrock static void
arc_hdr_destroy(arc_buf_hdr_t * hdr)14271544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr)
1428789Sahrens {
1429789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
14303403Sbmc 	ASSERT3P(hdr->b_state, ==, arc_anon);
14311544Seschrock 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
143210922SJeff.Bonwick@Sun.COM 	l2arc_buf_hdr_t *l2hdr = hdr->b_l2hdr;
143310922SJeff.Bonwick@Sun.COM 
143410922SJeff.Bonwick@Sun.COM 	if (l2hdr != NULL) {
143510922SJeff.Bonwick@Sun.COM 		boolean_t buflist_held = MUTEX_HELD(&l2arc_buflist_mtx);
143610922SJeff.Bonwick@Sun.COM 		/*
143710922SJeff.Bonwick@Sun.COM 		 * To prevent arc_free() and l2arc_evict() from
143810922SJeff.Bonwick@Sun.COM 		 * attempting to free the same buffer at the same time,
143910922SJeff.Bonwick@Sun.COM 		 * a FREE_IN_PROGRESS flag is given to arc_free() to
144010922SJeff.Bonwick@Sun.COM 		 * give it priority.  l2arc_evict() can't destroy this
144110922SJeff.Bonwick@Sun.COM 		 * header while we are waiting on l2arc_buflist_mtx.
144210922SJeff.Bonwick@Sun.COM 		 *
144310922SJeff.Bonwick@Sun.COM 		 * The hdr may be removed from l2ad_buflist before we
144410922SJeff.Bonwick@Sun.COM 		 * grab l2arc_buflist_mtx, so b_l2hdr is rechecked.
144510922SJeff.Bonwick@Sun.COM 		 */
144610922SJeff.Bonwick@Sun.COM 		if (!buflist_held) {
14475450Sbrendan 			mutex_enter(&l2arc_buflist_mtx);
144810922SJeff.Bonwick@Sun.COM 			l2hdr = hdr->b_l2hdr;
144910922SJeff.Bonwick@Sun.COM 		}
145010922SJeff.Bonwick@Sun.COM 
145110922SJeff.Bonwick@Sun.COM 		if (l2hdr != NULL) {
145210922SJeff.Bonwick@Sun.COM 			list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
145310922SJeff.Bonwick@Sun.COM 			ARCSTAT_INCR(arcstat_l2_size, -hdr->b_size);
145410922SJeff.Bonwick@Sun.COM 			kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
145510922SJeff.Bonwick@Sun.COM 			if (hdr->b_state == arc_l2c_only)
145610922SJeff.Bonwick@Sun.COM 				l2arc_hdr_stat_remove();
145710922SJeff.Bonwick@Sun.COM 			hdr->b_l2hdr = NULL;
145810922SJeff.Bonwick@Sun.COM 		}
145910922SJeff.Bonwick@Sun.COM 
146010922SJeff.Bonwick@Sun.COM 		if (!buflist_held)
14615450Sbrendan 			mutex_exit(&l2arc_buflist_mtx);
14625450Sbrendan 	}
14635450Sbrendan 
1464789Sahrens 	if (!BUF_EMPTY(hdr)) {
14651544Seschrock 		ASSERT(!HDR_IN_HASH_TABLE(hdr));
146612296SLin.Ling@Sun.COM 		buf_discard_identity(hdr);
1467789Sahrens 	}
14681544Seschrock 	while (hdr->b_buf) {
1469789Sahrens 		arc_buf_t *buf = hdr->b_buf;
1470789Sahrens 
14711544Seschrock 		if (buf->b_efunc) {
14721544Seschrock 			mutex_enter(&arc_eviction_mtx);
147312296SLin.Ling@Sun.COM 			mutex_enter(&buf->b_evict_lock);
14741544Seschrock 			ASSERT(buf->b_hdr != NULL);
14752688Smaybee 			arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
14761544Seschrock 			hdr->b_buf = buf->b_next;
14772887Smaybee 			buf->b_hdr = &arc_eviction_hdr;
14781544Seschrock 			buf->b_next = arc_eviction_list;
14791544Seschrock 			arc_eviction_list = buf;
148012296SLin.Ling@Sun.COM 			mutex_exit(&buf->b_evict_lock);
14811544Seschrock 			mutex_exit(&arc_eviction_mtx);
14821544Seschrock 		} else {
14832688Smaybee 			arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
14841544Seschrock 		}
1485789Sahrens 	}
14863093Sahrens 	if (hdr->b_freeze_cksum != NULL) {
14873093Sahrens 		kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
14883093Sahrens 		hdr->b_freeze_cksum = NULL;
14893093Sahrens 	}
149012296SLin.Ling@Sun.COM 	if (hdr->b_thawed) {
149112296SLin.Ling@Sun.COM 		kmem_free(hdr->b_thawed, 1);
149212296SLin.Ling@Sun.COM 		hdr->b_thawed = NULL;
149312296SLin.Ling@Sun.COM 	}
14941544Seschrock 
1495789Sahrens 	ASSERT(!list_link_active(&hdr->b_arc_node));
1496789Sahrens 	ASSERT3P(hdr->b_hash_next, ==, NULL);
1497789Sahrens 	ASSERT3P(hdr->b_acb, ==, NULL);
1498789Sahrens 	kmem_cache_free(hdr_cache, hdr);
1499789Sahrens }
1500789Sahrens 
1501789Sahrens void
arc_buf_free(arc_buf_t * buf,void * tag)1502789Sahrens arc_buf_free(arc_buf_t *buf, void *tag)
1503789Sahrens {
1504789Sahrens 	arc_buf_hdr_t *hdr = buf->b_hdr;
15053403Sbmc 	int hashed = hdr->b_state != arc_anon;
15061544Seschrock 
15071544Seschrock 	ASSERT(buf->b_efunc == NULL);
15081544Seschrock 	ASSERT(buf->b_data != NULL);
15091544Seschrock 
15101544Seschrock 	if (hashed) {
15111544Seschrock 		kmutex_t *hash_lock = HDR_LOCK(hdr);
15121544Seschrock 
15131544Seschrock 		mutex_enter(hash_lock);
151412296SLin.Ling@Sun.COM 		hdr = buf->b_hdr;
151512296SLin.Ling@Sun.COM 		ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
151612296SLin.Ling@Sun.COM 
15171544Seschrock 		(void) remove_reference(hdr, hash_lock, tag);
151810922SJeff.Bonwick@Sun.COM 		if (hdr->b_datacnt > 1) {
15192688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
152010922SJeff.Bonwick@Sun.COM 		} else {
152110922SJeff.Bonwick@Sun.COM 			ASSERT(buf == hdr->b_buf);
152210922SJeff.Bonwick@Sun.COM 			ASSERT(buf->b_efunc == NULL);
15231544Seschrock 			hdr->b_flags |= ARC_BUF_AVAILABLE;
152410922SJeff.Bonwick@Sun.COM 		}
15251544Seschrock 		mutex_exit(hash_lock);
15261544Seschrock 	} else if (HDR_IO_IN_PROGRESS(hdr)) {
15271544Seschrock 		int destroy_hdr;
15281544Seschrock 		/*
15291544Seschrock 		 * We are in the middle of an async write.  Don't destroy
15301544Seschrock 		 * this buffer unless the write completes before we finish
15311544Seschrock 		 * decrementing the reference count.
15321544Seschrock 		 */
15331544Seschrock 		mutex_enter(&arc_eviction_mtx);
15341544Seschrock 		(void) remove_reference(hdr, NULL, tag);
15351544Seschrock 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
15361544Seschrock 		destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
15371544Seschrock 		mutex_exit(&arc_eviction_mtx);
15381544Seschrock 		if (destroy_hdr)
15391544Seschrock 			arc_hdr_destroy(hdr);
15401544Seschrock 	} else {
154112296SLin.Ling@Sun.COM 		if (remove_reference(hdr, NULL, tag) > 0)
15422688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
154312296SLin.Ling@Sun.COM 		else
15441544Seschrock 			arc_hdr_destroy(hdr);
15451544Seschrock 	}
15461544Seschrock }
15471544Seschrock 
15481544Seschrock int
arc_buf_remove_ref(arc_buf_t * buf,void * tag)15491544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag)
15501544Seschrock {
15511544Seschrock 	arc_buf_hdr_t *hdr = buf->b_hdr;
1552789Sahrens 	kmutex_t *hash_lock = HDR_LOCK(hdr);
15531544Seschrock 	int no_callback = (buf->b_efunc == NULL);
15541544Seschrock 
15553403Sbmc 	if (hdr->b_state == arc_anon) {
155610922SJeff.Bonwick@Sun.COM 		ASSERT(hdr->b_datacnt == 1);
15571544Seschrock 		arc_buf_free(buf, tag);
15581544Seschrock 		return (no_callback);
15591544Seschrock 	}
1560789Sahrens 
1561789Sahrens 	mutex_enter(hash_lock);
156212296SLin.Ling@Sun.COM 	hdr = buf->b_hdr;
156312296SLin.Ling@Sun.COM 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
15643403Sbmc 	ASSERT(hdr->b_state != arc_anon);
15651544Seschrock 	ASSERT(buf->b_data != NULL);
1566789Sahrens 
15671544Seschrock 	(void) remove_reference(hdr, hash_lock, tag);
15681544Seschrock 	if (hdr->b_datacnt > 1) {
15691544Seschrock 		if (no_callback)
15702688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
15711544Seschrock 	} else if (no_callback) {
15721544Seschrock 		ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
157310922SJeff.Bonwick@Sun.COM 		ASSERT(buf->b_efunc == NULL);
15741544Seschrock 		hdr->b_flags |= ARC_BUF_AVAILABLE;
1575789Sahrens 	}
15761544Seschrock 	ASSERT(no_callback || hdr->b_datacnt > 1 ||
15771544Seschrock 	    refcount_is_zero(&hdr->b_refcnt));
1578789Sahrens 	mutex_exit(hash_lock);
15791544Seschrock 	return (no_callback);
1580789Sahrens }
1581789Sahrens 
1582789Sahrens int
arc_buf_size(arc_buf_t * buf)1583789Sahrens arc_buf_size(arc_buf_t *buf)
1584789Sahrens {
1585789Sahrens 	return (buf->b_hdr->b_size);
1586789Sahrens }
1587789Sahrens 
1588789Sahrens /*
1589789Sahrens  * Evict buffers from list until we've removed the specified number of
1590789Sahrens  * bytes.  Move the removed buffers to the appropriate evict state.
15912688Smaybee  * If the recycle flag is set, then attempt to "recycle" a buffer:
15922688Smaybee  * - look for a buffer to evict that is `bytes' long.
15932688Smaybee  * - return the data block from this buffer rather than freeing it.
15942688Smaybee  * This flag is used by callers that are trying to make space for a
15952688Smaybee  * new buffer in a full arc cache.
15965642Smaybee  *
15975642Smaybee  * This function makes a "best effort".  It skips over any buffers
15985642Smaybee  * it can't get a hash_lock on, and so may not catch all candidates.
15995642Smaybee  * It may also return without evicting as much space as requested.
1600789Sahrens  */
16012688Smaybee static void *
arc_evict(arc_state_t * state,uint64_t spa,int64_t bytes,boolean_t recycle,arc_buf_contents_t type)16028636SMark.Maybee@Sun.COM arc_evict(arc_state_t *state, uint64_t spa, int64_t bytes, boolean_t recycle,
16033290Sjohansen     arc_buf_contents_t type)
1604789Sahrens {
1605789Sahrens 	arc_state_t *evicted_state;
16062688Smaybee 	uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
16072918Smaybee 	arc_buf_hdr_t *ab, *ab_prev = NULL;
16084309Smaybee 	list_t *list = &state->arcs_list[type];
1609789Sahrens 	kmutex_t *hash_lock;
16102688Smaybee 	boolean_t have_lock;
16112918Smaybee 	void *stolen = NULL;
1612789Sahrens 
16133403Sbmc 	ASSERT(state == arc_mru || state == arc_mfu);
1614789Sahrens 
16153403Sbmc 	evicted_state = (state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
1616789Sahrens 
16173403Sbmc 	mutex_enter(&state->arcs_mtx);
16183403Sbmc 	mutex_enter(&evicted_state->arcs_mtx);
1619789Sahrens 
16204309Smaybee 	for (ab = list_tail(list); ab; ab = ab_prev) {
16214309Smaybee 		ab_prev = list_prev(list, ab);
16222391Smaybee 		/* prefetch buffers have a minimum lifespan */
16232688Smaybee 		if (HDR_IO_IN_PROGRESS(ab) ||
16245642Smaybee 		    (spa && ab->b_spa != spa) ||
16252688Smaybee 		    (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
162611066Srafael.vanoni@sun.com 		    ddi_get_lbolt() - ab->b_arc_access <
162711066Srafael.vanoni@sun.com 		    arc_min_prefetch_lifespan)) {
16282391Smaybee 			skipped++;
16292391Smaybee 			continue;
16302391Smaybee 		}
16312918Smaybee 		/* "lookahead" for better eviction candidate */
16322918Smaybee 		if (recycle && ab->b_size != bytes &&
16332918Smaybee 		    ab_prev && ab_prev->b_size == bytes)
16342688Smaybee 			continue;
1635789Sahrens 		hash_lock = HDR_LOCK(ab);
16362688Smaybee 		have_lock = MUTEX_HELD(hash_lock);
16372688Smaybee 		if (have_lock || mutex_tryenter(hash_lock)) {
1638789Sahrens 			ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0);
16391544Seschrock 			ASSERT(ab->b_datacnt > 0);
16401544Seschrock 			while (ab->b_buf) {
16411544Seschrock 				arc_buf_t *buf = ab->b_buf;
164212296SLin.Ling@Sun.COM 				if (!mutex_tryenter(&buf->b_evict_lock)) {
16437545SMark.Maybee@Sun.COM 					missed += 1;
16447545SMark.Maybee@Sun.COM 					break;
16457545SMark.Maybee@Sun.COM 				}
16462688Smaybee 				if (buf->b_data) {
16471544Seschrock 					bytes_evicted += ab->b_size;
16483290Sjohansen 					if (recycle && ab->b_type == type &&
16495450Sbrendan 					    ab->b_size == bytes &&
16505450Sbrendan 					    !HDR_L2_WRITING(ab)) {
16512918Smaybee 						stolen = buf->b_data;
16522918Smaybee 						recycle = FALSE;
16532918Smaybee 					}
16542688Smaybee 				}
16551544Seschrock 				if (buf->b_efunc) {
16561544Seschrock 					mutex_enter(&arc_eviction_mtx);
16572918Smaybee 					arc_buf_destroy(buf,
16582918Smaybee 					    buf->b_data == stolen, FALSE);
16591544Seschrock 					ab->b_buf = buf->b_next;
16602887Smaybee 					buf->b_hdr = &arc_eviction_hdr;
16611544Seschrock 					buf->b_next = arc_eviction_list;
16621544Seschrock 					arc_eviction_list = buf;
16631544Seschrock 					mutex_exit(&arc_eviction_mtx);
166412296SLin.Ling@Sun.COM 					mutex_exit(&buf->b_evict_lock);
16651544Seschrock 				} else {
166612296SLin.Ling@Sun.COM 					mutex_exit(&buf->b_evict_lock);
16672918Smaybee 					arc_buf_destroy(buf,
16682918Smaybee 					    buf->b_data == stolen, TRUE);
16691544Seschrock 				}
16701544Seschrock 			}
167110357SBrendan.Gregg@Sun.COM 
167210357SBrendan.Gregg@Sun.COM 			if (ab->b_l2hdr) {
167310357SBrendan.Gregg@Sun.COM 				ARCSTAT_INCR(arcstat_evict_l2_cached,
167410357SBrendan.Gregg@Sun.COM 				    ab->b_size);
167510357SBrendan.Gregg@Sun.COM 			} else {
167610357SBrendan.Gregg@Sun.COM 				if (l2arc_write_eligible(ab->b_spa, ab)) {
167710357SBrendan.Gregg@Sun.COM 					ARCSTAT_INCR(arcstat_evict_l2_eligible,
167810357SBrendan.Gregg@Sun.COM 					    ab->b_size);
167910357SBrendan.Gregg@Sun.COM 				} else {
168010357SBrendan.Gregg@Sun.COM 					ARCSTAT_INCR(
168110357SBrendan.Gregg@Sun.COM 					    arcstat_evict_l2_ineligible,
168210357SBrendan.Gregg@Sun.COM 					    ab->b_size);
168310357SBrendan.Gregg@Sun.COM 				}
168410357SBrendan.Gregg@Sun.COM 			}
168510357SBrendan.Gregg@Sun.COM 
16867545SMark.Maybee@Sun.COM 			if (ab->b_datacnt == 0) {
16877545SMark.Maybee@Sun.COM 				arc_change_state(evicted_state, ab, hash_lock);
16887545SMark.Maybee@Sun.COM 				ASSERT(HDR_IN_HASH_TABLE(ab));
16897545SMark.Maybee@Sun.COM 				ab->b_flags |= ARC_IN_HASH_TABLE;
16907545SMark.Maybee@Sun.COM 				ab->b_flags &= ~ARC_BUF_AVAILABLE;
16917545SMark.Maybee@Sun.COM 				DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
16927545SMark.Maybee@Sun.COM 			}
16932688Smaybee 			if (!have_lock)
16942688Smaybee 				mutex_exit(hash_lock);
16951544Seschrock 			if (bytes >= 0 && bytes_evicted >= bytes)
1696789Sahrens 				break;
1697789Sahrens 		} else {
16982688Smaybee 			missed += 1;
1699789Sahrens 		}
1700789Sahrens 	}
17013403Sbmc 
17023403Sbmc 	mutex_exit(&evicted_state->arcs_mtx);
17033403Sbmc 	mutex_exit(&state->arcs_mtx);
1704789Sahrens 
1705789Sahrens 	if (bytes_evicted < bytes)
1706789Sahrens 		dprintf("only evicted %lld bytes from %x",
1707789Sahrens 		    (longlong_t)bytes_evicted, state);
1708789Sahrens 
17092688Smaybee 	if (skipped)
17103403Sbmc 		ARCSTAT_INCR(arcstat_evict_skip, skipped);
17113403Sbmc 
17122688Smaybee 	if (missed)
17133403Sbmc 		ARCSTAT_INCR(arcstat_mutex_miss, missed);
17143403Sbmc 
17154709Smaybee 	/*
17164709Smaybee 	 * We have just evicted some date into the ghost state, make
17174709Smaybee 	 * sure we also adjust the ghost state size if necessary.
17184709Smaybee 	 */
17194709Smaybee 	if (arc_no_grow &&
17204709Smaybee 	    arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size > arc_c) {
17214709Smaybee 		int64_t mru_over = arc_anon->arcs_size + arc_mru->arcs_size +
17224709Smaybee 		    arc_mru_ghost->arcs_size - arc_c;
17234709Smaybee 
17244709Smaybee 		if (mru_over > 0 && arc_mru_ghost->arcs_lsize[type] > 0) {
17254709Smaybee 			int64_t todelete =
17264709Smaybee 			    MIN(arc_mru_ghost->arcs_lsize[type], mru_over);
17275642Smaybee 			arc_evict_ghost(arc_mru_ghost, NULL, todelete);
17284709Smaybee 		} else if (arc_mfu_ghost->arcs_lsize[type] > 0) {
17294709Smaybee 			int64_t todelete = MIN(arc_mfu_ghost->arcs_lsize[type],
17304709Smaybee 			    arc_mru_ghost->arcs_size +
17314709Smaybee 			    arc_mfu_ghost->arcs_size - arc_c);
17325642Smaybee 			arc_evict_ghost(arc_mfu_ghost, NULL, todelete);
17334709Smaybee 		}
17344709Smaybee 	}
17354709Smaybee 
17362918Smaybee 	return (stolen);
1737789Sahrens }
1738789Sahrens 
1739789Sahrens /*
1740789Sahrens  * Remove buffers from list until we've removed the specified number of
1741789Sahrens  * bytes.  Destroy the buffers that are removed.
1742789Sahrens  */
1743789Sahrens static void
arc_evict_ghost(arc_state_t * state,uint64_t spa,int64_t bytes)17448636SMark.Maybee@Sun.COM arc_evict_ghost(arc_state_t *state, uint64_t spa, int64_t bytes)
1745789Sahrens {
1746789Sahrens 	arc_buf_hdr_t *ab, *ab_prev;
174712674SSanjeev.Bagewadi@Sun.COM 	arc_buf_hdr_t marker = { 0 };
17484309Smaybee 	list_t *list = &state->arcs_list[ARC_BUFC_DATA];
1749789Sahrens 	kmutex_t *hash_lock;
17501544Seschrock 	uint64_t bytes_deleted = 0;
17513700Sek110237 	uint64_t bufs_skipped = 0;
1752789Sahrens 
17531544Seschrock 	ASSERT(GHOST_STATE(state));
1754789Sahrens top:
17553403Sbmc 	mutex_enter(&state->arcs_mtx);
17564309Smaybee 	for (ab = list_tail(list); ab; ab = ab_prev) {
17574309Smaybee 		ab_prev = list_prev(list, ab);
17585642Smaybee 		if (spa && ab->b_spa != spa)
17595642Smaybee 			continue;
176012674SSanjeev.Bagewadi@Sun.COM 
176112674SSanjeev.Bagewadi@Sun.COM 		/* ignore markers */
176212674SSanjeev.Bagewadi@Sun.COM 		if (ab->b_spa == 0)
176312674SSanjeev.Bagewadi@Sun.COM 			continue;
176412674SSanjeev.Bagewadi@Sun.COM 
1765789Sahrens 		hash_lock = HDR_LOCK(ab);
176612033Swilliam.gorrell@sun.com 		/* caller may be trying to modify this buffer, skip it */
176712033Swilliam.gorrell@sun.com 		if (MUTEX_HELD(hash_lock))
176812033Swilliam.gorrell@sun.com 			continue;
176912033Swilliam.gorrell@sun.com 		if (mutex_tryenter(hash_lock)) {
17702391Smaybee 			ASSERT(!HDR_IO_IN_PROGRESS(ab));
17711544Seschrock 			ASSERT(ab->b_buf == NULL);
17723403Sbmc 			ARCSTAT_BUMP(arcstat_deleted);
17731544Seschrock 			bytes_deleted += ab->b_size;
17745450Sbrendan 
17755450Sbrendan 			if (ab->b_l2hdr != NULL) {
17765450Sbrendan 				/*
17775450Sbrendan 				 * This buffer is cached on the 2nd Level ARC;
17785450Sbrendan 				 * don't destroy the header.
17795450Sbrendan 				 */
17805450Sbrendan 				arc_change_state(arc_l2c_only, ab, hash_lock);
178112033Swilliam.gorrell@sun.com 				mutex_exit(hash_lock);
17825450Sbrendan 			} else {
17835450Sbrendan 				arc_change_state(arc_anon, ab, hash_lock);
178412033Swilliam.gorrell@sun.com 				mutex_exit(hash_lock);
17855450Sbrendan 				arc_hdr_destroy(ab);
17865450Sbrendan 			}
17875450Sbrendan 
1788789Sahrens 			DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1789789Sahrens 			if (bytes >= 0 && bytes_deleted >= bytes)
1790789Sahrens 				break;
179112674SSanjeev.Bagewadi@Sun.COM 		} else if (bytes < 0) {
179212674SSanjeev.Bagewadi@Sun.COM 			/*
179312674SSanjeev.Bagewadi@Sun.COM 			 * Insert a list marker and then wait for the
179412674SSanjeev.Bagewadi@Sun.COM 			 * hash lock to become available. Once its
179512674SSanjeev.Bagewadi@Sun.COM 			 * available, restart from where we left off.
179612674SSanjeev.Bagewadi@Sun.COM 			 */
179712674SSanjeev.Bagewadi@Sun.COM 			list_insert_after(list, ab, &marker);
179812674SSanjeev.Bagewadi@Sun.COM 			mutex_exit(&state->arcs_mtx);
179912674SSanjeev.Bagewadi@Sun.COM 			mutex_enter(hash_lock);
180012674SSanjeev.Bagewadi@Sun.COM 			mutex_exit(hash_lock);
180112674SSanjeev.Bagewadi@Sun.COM 			mutex_enter(&state->arcs_mtx);
180212674SSanjeev.Bagewadi@Sun.COM 			ab_prev = list_prev(list, &marker);
180312674SSanjeev.Bagewadi@Sun.COM 			list_remove(list, &marker);
180412674SSanjeev.Bagewadi@Sun.COM 		} else
1805789Sahrens 			bufs_skipped += 1;
1806789Sahrens 	}
18073403Sbmc 	mutex_exit(&state->arcs_mtx);
1808789Sahrens 
18094309Smaybee 	if (list == &state->arcs_list[ARC_BUFC_DATA] &&
18104309Smaybee 	    (bytes < 0 || bytes_deleted < bytes)) {
18114309Smaybee 		list = &state->arcs_list[ARC_BUFC_METADATA];
18124309Smaybee 		goto top;
18134309Smaybee 	}
18144309Smaybee 
1815789Sahrens 	if (bufs_skipped) {
18163403Sbmc 		ARCSTAT_INCR(arcstat_mutex_miss, bufs_skipped);
1817789Sahrens 		ASSERT(bytes >= 0);
1818789Sahrens 	}
1819789Sahrens 
1820789Sahrens 	if (bytes_deleted < bytes)
1821789Sahrens 		dprintf("only deleted %lld bytes from %p",
1822789Sahrens 		    (longlong_t)bytes_deleted, state);
1823789Sahrens }
1824789Sahrens 
1825789Sahrens static void
arc_adjust(void)1826789Sahrens arc_adjust(void)
1827789Sahrens {
18288582SBrendan.Gregg@Sun.COM 	int64_t adjustment, delta;
18298582SBrendan.Gregg@Sun.COM 
18308582SBrendan.Gregg@Sun.COM 	/*
18318582SBrendan.Gregg@Sun.COM 	 * Adjust MRU size
18328582SBrendan.Gregg@Sun.COM 	 */
18338582SBrendan.Gregg@Sun.COM 
183412636STom.Erickson@Sun.COM 	adjustment = MIN((int64_t)(arc_size - arc_c),
183512636STom.Erickson@Sun.COM 	    (int64_t)(arc_anon->arcs_size + arc_mru->arcs_size + arc_meta_used -
183612636STom.Erickson@Sun.COM 	    arc_p));
18378582SBrendan.Gregg@Sun.COM 
18388582SBrendan.Gregg@Sun.COM 	if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_DATA] > 0) {
18398582SBrendan.Gregg@Sun.COM 		delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_DATA], adjustment);
18408582SBrendan.Gregg@Sun.COM 		(void) arc_evict(arc_mru, NULL, delta, FALSE, ARC_BUFC_DATA);
18418582SBrendan.Gregg@Sun.COM 		adjustment -= delta;
18424309Smaybee 	}
18434309Smaybee 
18448582SBrendan.Gregg@Sun.COM 	if (adjustment > 0 && arc_mru->arcs_lsize[ARC_BUFC_METADATA] > 0) {
18458582SBrendan.Gregg@Sun.COM 		delta = MIN(arc_mru->arcs_lsize[ARC_BUFC_METADATA], adjustment);
18468582SBrendan.Gregg@Sun.COM 		(void) arc_evict(arc_mru, NULL, delta, FALSE,
18475642Smaybee 		    ARC_BUFC_METADATA);
1848789Sahrens 	}
1849789Sahrens 
18508582SBrendan.Gregg@Sun.COM 	/*
18518582SBrendan.Gregg@Sun.COM 	 * Adjust MFU size
18528582SBrendan.Gregg@Sun.COM 	 */
18538582SBrendan.Gregg@Sun.COM 
18548582SBrendan.Gregg@Sun.COM 	adjustment = arc_size - arc_c;
18558582SBrendan.Gregg@Sun.COM 
18568582SBrendan.Gregg@Sun.COM 	if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_DATA] > 0) {
18578582SBrendan.Gregg@Sun.COM 		delta = MIN(adjustment, arc_mfu->arcs_lsize[ARC_BUFC_DATA]);
18588582SBrendan.Gregg@Sun.COM 		(void) arc_evict(arc_mfu, NULL, delta, FALSE, ARC_BUFC_DATA);
18598582SBrendan.Gregg@Sun.COM 		adjustment -= delta;
18608582SBrendan.Gregg@Sun.COM 	}
18618582SBrendan.Gregg@Sun.COM 
18628582SBrendan.Gregg@Sun.COM 	if (adjustment > 0 && arc_mfu->arcs_lsize[ARC_BUFC_METADATA] > 0) {
18638582SBrendan.Gregg@Sun.COM 		int64_t delta = MIN(adjustment,
18648582SBrendan.Gregg@Sun.COM 		    arc_mfu->arcs_lsize[ARC_BUFC_METADATA]);
18658582SBrendan.Gregg@Sun.COM 		(void) arc_evict(arc_mfu, NULL, delta, FALSE,
18668582SBrendan.Gregg@Sun.COM 		    ARC_BUFC_METADATA);
18678582SBrendan.Gregg@Sun.COM 	}
18688582SBrendan.Gregg@Sun.COM 
18698582SBrendan.Gregg@Sun.COM 	/*
18708582SBrendan.Gregg@Sun.COM 	 * Adjust ghost lists
18718582SBrendan.Gregg@Sun.COM 	 */
18728582SBrendan.Gregg@Sun.COM 
18738582SBrendan.Gregg@Sun.COM 	adjustment = arc_mru->arcs_size + arc_mru_ghost->arcs_size - arc_c;
18748582SBrendan.Gregg@Sun.COM 
18758582SBrendan.Gregg@Sun.COM 	if (adjustment > 0 && arc_mru_ghost->arcs_size > 0) {
18768582SBrendan.Gregg@Sun.COM 		delta = MIN(arc_mru_ghost->arcs_size, adjustment);
18778582SBrendan.Gregg@Sun.COM 		arc_evict_ghost(arc_mru_ghost, NULL, delta);
18788582SBrendan.Gregg@Sun.COM 	}
18798582SBrendan.Gregg@Sun.COM 
18808582SBrendan.Gregg@Sun.COM 	adjustment =
18818582SBrendan.Gregg@Sun.COM 	    arc_mru_ghost->arcs_size + arc_mfu_ghost->arcs_size - arc_c;
18828582SBrendan.Gregg@Sun.COM 
18838582SBrendan.Gregg@Sun.COM 	if (adjustment > 0 && arc_mfu_ghost->arcs_size > 0) {
18848582SBrendan.Gregg@Sun.COM 		delta = MIN(arc_mfu_ghost->arcs_size, adjustment);
18858582SBrendan.Gregg@Sun.COM 		arc_evict_ghost(arc_mfu_ghost, NULL, delta);
1886789Sahrens 	}
1887789Sahrens }
1888789Sahrens 
18891544Seschrock static void
arc_do_user_evicts(void)18901544Seschrock arc_do_user_evicts(void)
18911544Seschrock {
18921544Seschrock 	mutex_enter(&arc_eviction_mtx);
18931544Seschrock 	while (arc_eviction_list != NULL) {
18941544Seschrock 		arc_buf_t *buf = arc_eviction_list;
18951544Seschrock 		arc_eviction_list = buf->b_next;
189612296SLin.Ling@Sun.COM 		mutex_enter(&buf->b_evict_lock);
18971544Seschrock 		buf->b_hdr = NULL;
189812296SLin.Ling@Sun.COM 		mutex_exit(&buf->b_evict_lock);
18991544Seschrock 		mutex_exit(&arc_eviction_mtx);
19001544Seschrock 
19011819Smaybee 		if (buf->b_efunc != NULL)
19021819Smaybee 			VERIFY(buf->b_efunc(buf) == 0);
19031544Seschrock 
19041544Seschrock 		buf->b_efunc = NULL;
19051544Seschrock 		buf->b_private = NULL;
19061544Seschrock 		kmem_cache_free(buf_cache, buf);
19071544Seschrock 		mutex_enter(&arc_eviction_mtx);
19081544Seschrock 	}
19091544Seschrock 	mutex_exit(&arc_eviction_mtx);
19101544Seschrock }
19111544Seschrock 
1912789Sahrens /*
19135642Smaybee  * Flush all *evictable* data from the cache for the given spa.
1914789Sahrens  * NOTE: this will not touch "active" (i.e. referenced) data.
1915789Sahrens  */
1916789Sahrens void
arc_flush(spa_t * spa)19175642Smaybee arc_flush(spa_t *spa)
1918789Sahrens {
19198636SMark.Maybee@Sun.COM 	uint64_t guid = 0;
19208636SMark.Maybee@Sun.COM 
19218636SMark.Maybee@Sun.COM 	if (spa)
19228636SMark.Maybee@Sun.COM 		guid = spa_guid(spa);
19238636SMark.Maybee@Sun.COM 
19245642Smaybee 	while (list_head(&arc_mru->arcs_list[ARC_BUFC_DATA])) {
19258636SMark.Maybee@Sun.COM 		(void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_DATA);
19265642Smaybee 		if (spa)
19275642Smaybee 			break;
19285642Smaybee 	}
19295642Smaybee 	while (list_head(&arc_mru->arcs_list[ARC_BUFC_METADATA])) {
19308636SMark.Maybee@Sun.COM 		(void) arc_evict(arc_mru, guid, -1, FALSE, ARC_BUFC_METADATA);
19315642Smaybee 		if (spa)
19325642Smaybee 			break;
19335642Smaybee 	}
19345642Smaybee 	while (list_head(&arc_mfu->arcs_list[ARC_BUFC_DATA])) {
19358636SMark.Maybee@Sun.COM 		(void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_DATA);
19365642Smaybee 		if (spa)
19375642Smaybee 			break;
19385642Smaybee 	}
19395642Smaybee 	while (list_head(&arc_mfu->arcs_list[ARC_BUFC_METADATA])) {
19408636SMark.Maybee@Sun.COM 		(void) arc_evict(arc_mfu, guid, -1, FALSE, ARC_BUFC_METADATA);
19415642Smaybee 		if (spa)
19425642Smaybee 			break;
19435642Smaybee 	}
19445642Smaybee 
19458636SMark.Maybee@Sun.COM 	arc_evict_ghost(arc_mru_ghost, guid, -1);
19468636SMark.Maybee@Sun.COM 	arc_evict_ghost(arc_mfu_ghost, guid, -1);
19471544Seschrock 
19481544Seschrock 	mutex_enter(&arc_reclaim_thr_lock);
19491544Seschrock 	arc_do_user_evicts();
19501544Seschrock 	mutex_exit(&arc_reclaim_thr_lock);
19515642Smaybee 	ASSERT(spa || arc_eviction_list == NULL);
1952789Sahrens }
1953789Sahrens 
1954789Sahrens void
arc_shrink(void)19553158Smaybee arc_shrink(void)
1956789Sahrens {
19573403Sbmc 	if (arc_c > arc_c_min) {
19583158Smaybee 		uint64_t to_free;
1959789Sahrens 
19602048Sstans #ifdef _KERNEL
19613403Sbmc 		to_free = MAX(arc_c >> arc_shrink_shift, ptob(needfree));
19622048Sstans #else
19633403Sbmc 		to_free = arc_c >> arc_shrink_shift;
19642048Sstans #endif
19653403Sbmc 		if (arc_c > arc_c_min + to_free)
19663403Sbmc 			atomic_add_64(&arc_c, -to_free);
19673158Smaybee 		else
19683403Sbmc 			arc_c = arc_c_min;
19692048Sstans 
19703403Sbmc 		atomic_add_64(&arc_p, -(arc_p >> arc_shrink_shift));
19713403Sbmc 		if (arc_c > arc_size)
19723403Sbmc 			arc_c = MAX(arc_size, arc_c_min);
19733403Sbmc 		if (arc_p > arc_c)
19743403Sbmc 			arc_p = (arc_c >> 1);
19753403Sbmc 		ASSERT(arc_c >= arc_c_min);
19763403Sbmc 		ASSERT((int64_t)arc_p >= 0);
19773158Smaybee 	}
1978789Sahrens 
19793403Sbmc 	if (arc_size > arc_c)
19803158Smaybee 		arc_adjust();
1981789Sahrens }
1982789Sahrens 
1983789Sahrens static int
arc_reclaim_needed(void)1984789Sahrens arc_reclaim_needed(void)
1985789Sahrens {
1986789Sahrens 	uint64_t extra;
1987789Sahrens 
1988789Sahrens #ifdef _KERNEL
19892048Sstans 
19902048Sstans 	if (needfree)
19912048Sstans 		return (1);
19922048Sstans 
1993789Sahrens 	/*
1994789Sahrens 	 * take 'desfree' extra pages, so we reclaim sooner, rather than later
1995789Sahrens 	 */
1996789Sahrens 	extra = desfree;
1997789Sahrens 
1998789Sahrens 	/*
1999789Sahrens 	 * check that we're out of range of the pageout scanner.  It starts to
2000789Sahrens 	 * schedule paging if freemem is less than lotsfree and needfree.
2001789Sahrens 	 * lotsfree is the high-water mark for pageout, and needfree is the
2002789Sahrens 	 * number of needed free pages.  We add extra pages here to make sure
2003789Sahrens 	 * the scanner doesn't start up while we're freeing memory.
2004789Sahrens 	 */
2005789Sahrens 	if (freemem < lotsfree + needfree + extra)
2006789Sahrens 		return (1);
2007789Sahrens 
2008789Sahrens 	/*
2009789Sahrens 	 * check to make sure that swapfs has enough space so that anon
20105450Sbrendan 	 * reservations can still succeed. anon_resvmem() checks that the
2011789Sahrens 	 * availrmem is greater than swapfs_minfree, and the number of reserved
2012789Sahrens 	 * swap pages.  We also add a bit of extra here just to prevent
2013789Sahrens 	 * circumstances from getting really dire.
2014789Sahrens 	 */
2015789Sahrens 	if (availrmem < swapfs_minfree + swapfs_reserve + extra)
2016789Sahrens 		return (1);
2017789Sahrens 
20181936Smaybee #if defined(__i386)
2019789Sahrens 	/*
2020789Sahrens 	 * If we're on an i386 platform, it's possible that we'll exhaust the
2021789Sahrens 	 * kernel heap space before we ever run out of available physical
2022789Sahrens 	 * memory.  Most checks of the size of the heap_area compare against
2023789Sahrens 	 * tune.t_minarmem, which is the minimum available real memory that we
2024789Sahrens 	 * can have in the system.  However, this is generally fixed at 25 pages
2025789Sahrens 	 * which is so low that it's useless.  In this comparison, we seek to
2026789Sahrens 	 * calculate the total heap-size, and reclaim if more than 3/4ths of the
20275450Sbrendan 	 * heap is allocated.  (Or, in the calculation, if less than 1/4th is
2028789Sahrens 	 * free)
2029789Sahrens 	 */
2030789Sahrens 	if (btop(vmem_size(heap_arena, VMEM_FREE)) <
2031789Sahrens 	    (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
2032789Sahrens 		return (1);
2033789Sahrens #endif
2034789Sahrens 
2035789Sahrens #else
2036789Sahrens 	if (spa_get_random(100) == 0)
2037789Sahrens 		return (1);
2038789Sahrens #endif
2039789Sahrens 	return (0);
2040789Sahrens }
2041789Sahrens 
2042789Sahrens static void
arc_kmem_reap_now(arc_reclaim_strategy_t strat)2043789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat)
2044789Sahrens {
2045789Sahrens 	size_t			i;
2046789Sahrens 	kmem_cache_t		*prev_cache = NULL;
20473290Sjohansen 	kmem_cache_t		*prev_data_cache = NULL;
2048789Sahrens 	extern kmem_cache_t	*zio_buf_cache[];
20493290Sjohansen 	extern kmem_cache_t	*zio_data_buf_cache[];
2050789Sahrens 
20511484Sek110237 #ifdef _KERNEL
20524309Smaybee 	if (arc_meta_used >= arc_meta_limit) {
20534309Smaybee 		/*
20544309Smaybee 		 * We are exceeding our meta-data cache limit.
20554309Smaybee 		 * Purge some DNLC entries to release holds on meta-data.
20564309Smaybee 		 */
20574309Smaybee 		dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
20584309Smaybee 	}
20591936Smaybee #if defined(__i386)
20601936Smaybee 	/*
20611936Smaybee 	 * Reclaim unused memory from all kmem caches.
20621936Smaybee 	 */
20631936Smaybee 	kmem_reap();
20641936Smaybee #endif
20651484Sek110237 #endif
20661484Sek110237 
2067789Sahrens 	/*
20685450Sbrendan 	 * An aggressive reclamation will shrink the cache size as well as
20691544Seschrock 	 * reap free buffers from the arc kmem caches.
2070789Sahrens 	 */
2071789Sahrens 	if (strat == ARC_RECLAIM_AGGR)
20723158Smaybee 		arc_shrink();
2073789Sahrens 
2074789Sahrens 	for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
2075789Sahrens 		if (zio_buf_cache[i] != prev_cache) {
2076789Sahrens 			prev_cache = zio_buf_cache[i];
2077789Sahrens 			kmem_cache_reap_now(zio_buf_cache[i]);
2078789Sahrens 		}
20793290Sjohansen 		if (zio_data_buf_cache[i] != prev_data_cache) {
20803290Sjohansen 			prev_data_cache = zio_data_buf_cache[i];
20813290Sjohansen 			kmem_cache_reap_now(zio_data_buf_cache[i]);
20823290Sjohansen 		}
2083789Sahrens 	}
20841544Seschrock 	kmem_cache_reap_now(buf_cache);
20851544Seschrock 	kmem_cache_reap_now(hdr_cache);
2086789Sahrens }
2087789Sahrens 
2088789Sahrens static void
arc_reclaim_thread(void)2089789Sahrens arc_reclaim_thread(void)
2090789Sahrens {
2091789Sahrens 	clock_t			growtime = 0;
2092789Sahrens 	arc_reclaim_strategy_t	last_reclaim = ARC_RECLAIM_CONS;
2093789Sahrens 	callb_cpr_t		cpr;
2094789Sahrens 
2095789Sahrens 	CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
2096789Sahrens 
2097789Sahrens 	mutex_enter(&arc_reclaim_thr_lock);
2098789Sahrens 	while (arc_thread_exit == 0) {
2099789Sahrens 		if (arc_reclaim_needed()) {
2100789Sahrens 
21013403Sbmc 			if (arc_no_grow) {
2102789Sahrens 				if (last_reclaim == ARC_RECLAIM_CONS) {
2103789Sahrens 					last_reclaim = ARC_RECLAIM_AGGR;
2104789Sahrens 				} else {
2105789Sahrens 					last_reclaim = ARC_RECLAIM_CONS;
2106789Sahrens 				}
2107789Sahrens 			} else {
21083403Sbmc 				arc_no_grow = TRUE;
2109789Sahrens 				last_reclaim = ARC_RECLAIM_AGGR;
2110789Sahrens 				membar_producer();
2111789Sahrens 			}
2112789Sahrens 
2113789Sahrens 			/* reset the growth delay for every reclaim */
211411066Srafael.vanoni@sun.com 			growtime = ddi_get_lbolt() + (arc_grow_retry * hz);
2115789Sahrens 
2116789Sahrens 			arc_kmem_reap_now(last_reclaim);
21176987Sbrendan 			arc_warm = B_TRUE;
2118789Sahrens 
211911066Srafael.vanoni@sun.com 		} else if (arc_no_grow && ddi_get_lbolt() >= growtime) {
21203403Sbmc 			arc_no_grow = FALSE;
2121789Sahrens 		}
2122789Sahrens 
212312636STom.Erickson@Sun.COM 		arc_adjust();
21243298Smaybee 
21251544Seschrock 		if (arc_eviction_list != NULL)
21261544Seschrock 			arc_do_user_evicts();
21271544Seschrock 
2128789Sahrens 		/* block until needed, or one second, whichever is shorter */
2129789Sahrens 		CALLB_CPR_SAFE_BEGIN(&cpr);
2130789Sahrens 		(void) cv_timedwait(&arc_reclaim_thr_cv,
213111066Srafael.vanoni@sun.com 		    &arc_reclaim_thr_lock, (ddi_get_lbolt() + hz));
2132789Sahrens 		CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
2133789Sahrens 	}
2134789Sahrens 
2135789Sahrens 	arc_thread_exit = 0;
2136789Sahrens 	cv_broadcast(&arc_reclaim_thr_cv);
2137789Sahrens 	CALLB_CPR_EXIT(&cpr);		/* drops arc_reclaim_thr_lock */
2138789Sahrens 	thread_exit();
2139789Sahrens }
2140789Sahrens 
21411544Seschrock /*
21421544Seschrock  * Adapt arc info given the number of bytes we are trying to add and
21431544Seschrock  * the state that we are comming from.  This function is only called
21441544Seschrock  * when we are adding new content to the cache.
21451544Seschrock  */
2146789Sahrens static void
arc_adapt(int bytes,arc_state_t * state)21471544Seschrock arc_adapt(int bytes, arc_state_t *state)
2148789Sahrens {
21491544Seschrock 	int mult;
21508582SBrendan.Gregg@Sun.COM 	uint64_t arc_p_min = (arc_c >> arc_p_min_shift);
21511544Seschrock 
21525450Sbrendan 	if (state == arc_l2c_only)
21535450Sbrendan 		return;
21545450Sbrendan 
21551544Seschrock 	ASSERT(bytes > 0);
2156789Sahrens 	/*
21571544Seschrock 	 * Adapt the target size of the MRU list:
21581544Seschrock 	 *	- if we just hit in the MRU ghost list, then increase
21591544Seschrock 	 *	  the target size of the MRU list.
21601544Seschrock 	 *	- if we just hit in the MFU ghost list, then increase
21611544Seschrock 	 *	  the target size of the MFU list by decreasing the
21621544Seschrock 	 *	  target size of the MRU list.
2163789Sahrens 	 */
21643403Sbmc 	if (state == arc_mru_ghost) {
21653403Sbmc 		mult = ((arc_mru_ghost->arcs_size >= arc_mfu_ghost->arcs_size) ?
21663403Sbmc 		    1 : (arc_mfu_ghost->arcs_size/arc_mru_ghost->arcs_size));
216712636STom.Erickson@Sun.COM 		mult = MIN(mult, 10); /* avoid wild arc_p adjustment */
21681544Seschrock 
21698582SBrendan.Gregg@Sun.COM 		arc_p = MIN(arc_c - arc_p_min, arc_p + bytes * mult);
21703403Sbmc 	} else if (state == arc_mfu_ghost) {
21718582SBrendan.Gregg@Sun.COM 		uint64_t delta;
21728582SBrendan.Gregg@Sun.COM 
21733403Sbmc 		mult = ((arc_mfu_ghost->arcs_size >= arc_mru_ghost->arcs_size) ?
21743403Sbmc 		    1 : (arc_mru_ghost->arcs_size/arc_mfu_ghost->arcs_size));
217512636STom.Erickson@Sun.COM 		mult = MIN(mult, 10);
21761544Seschrock 
21778582SBrendan.Gregg@Sun.COM 		delta = MIN(bytes * mult, arc_p);
21788582SBrendan.Gregg@Sun.COM 		arc_p = MAX(arc_p_min, arc_p - delta);
21791544Seschrock 	}
21803403Sbmc 	ASSERT((int64_t)arc_p >= 0);
2181789Sahrens 
2182789Sahrens 	if (arc_reclaim_needed()) {
2183789Sahrens 		cv_signal(&arc_reclaim_thr_cv);
2184789Sahrens 		return;
2185789Sahrens 	}
2186789Sahrens 
21873403Sbmc 	if (arc_no_grow)
2188789Sahrens 		return;
2189789Sahrens 
21903403Sbmc 	if (arc_c >= arc_c_max)
21911544Seschrock 		return;
21921544Seschrock 
2193789Sahrens 	/*
21941544Seschrock 	 * If we're within (2 * maxblocksize) bytes of the target
21951544Seschrock 	 * cache size, increment the target cache size
2196789Sahrens 	 */
21973403Sbmc 	if (arc_size > arc_c - (2ULL << SPA_MAXBLOCKSHIFT)) {
21983403Sbmc 		atomic_add_64(&arc_c, (int64_t)bytes);
21993403Sbmc 		if (arc_c > arc_c_max)
22003403Sbmc 			arc_c = arc_c_max;
22013403Sbmc 		else if (state == arc_anon)
22023403Sbmc 			atomic_add_64(&arc_p, (int64_t)bytes);
22033403Sbmc 		if (arc_p > arc_c)
22043403Sbmc 			arc_p = arc_c;
2205789Sahrens 	}
22063403Sbmc 	ASSERT((int64_t)arc_p >= 0);
2207789Sahrens }
2208789Sahrens 
2209789Sahrens /*
22101544Seschrock  * Check if the cache has reached its limits and eviction is required
22111544Seschrock  * prior to insert.
2212789Sahrens  */
2213789Sahrens static int
arc_evict_needed(arc_buf_contents_t type)22144309Smaybee arc_evict_needed(arc_buf_contents_t type)
2215789Sahrens {
22164309Smaybee 	if (type == ARC_BUFC_METADATA && arc_meta_used >= arc_meta_limit)
22174309Smaybee 		return (1);
22184309Smaybee 
22194309Smaybee #ifdef _KERNEL
22204309Smaybee 	/*
22214309Smaybee 	 * If zio data pages are being allocated out of a separate heap segment,
22224309Smaybee 	 * then enforce that the size of available vmem for this area remains
22234309Smaybee 	 * above about 1/32nd free.
22244309Smaybee 	 */
22254309Smaybee 	if (type == ARC_BUFC_DATA && zio_arena != NULL &&
22264309Smaybee 	    vmem_size(zio_arena, VMEM_FREE) <
22274309Smaybee 	    (vmem_size(zio_arena, VMEM_ALLOC) >> 5))
22284309Smaybee 		return (1);
22294309Smaybee #endif
22304309Smaybee 
2231789Sahrens 	if (arc_reclaim_needed())
2232789Sahrens 		return (1);
2233789Sahrens 
22343403Sbmc 	return (arc_size > arc_c);
2235789Sahrens }
2236789Sahrens 
2237789Sahrens /*
22382688Smaybee  * The buffer, supplied as the first argument, needs a data block.
22392688Smaybee  * So, if we are at cache max, determine which cache should be victimized.
22402688Smaybee  * We have the following cases:
2241789Sahrens  *
22423403Sbmc  * 1. Insert for MRU, p > sizeof(arc_anon + arc_mru) ->
2243789Sahrens  * In this situation if we're out of space, but the resident size of the MFU is
2244789Sahrens  * under the limit, victimize the MFU cache to satisfy this insertion request.
2245789Sahrens  *
22463403Sbmc  * 2. Insert for MRU, p <= sizeof(arc_anon + arc_mru) ->
2247789Sahrens  * Here, we've used up all of the available space for the MRU, so we need to
2248789Sahrens  * evict from our own cache instead.  Evict from the set of resident MRU
2249789Sahrens  * entries.
2250789Sahrens  *
22513403Sbmc  * 3. Insert for MFU (c - p) > sizeof(arc_mfu) ->
2252789Sahrens  * c minus p represents the MFU space in the cache, since p is the size of the
2253789Sahrens  * cache that is dedicated to the MRU.  In this situation there's still space on
2254789Sahrens  * the MFU side, so the MRU side needs to be victimized.
2255789Sahrens  *
22563403Sbmc  * 4. Insert for MFU (c - p) < sizeof(arc_mfu) ->
2257789Sahrens  * MFU's resident set is consuming more space than it has been allotted.  In
2258789Sahrens  * this situation, we must victimize our own cache, the MFU, for this insertion.
2259789Sahrens  */
2260789Sahrens static void
arc_get_data_buf(arc_buf_t * buf)22612688Smaybee arc_get_data_buf(arc_buf_t *buf)
2262789Sahrens {
22633290Sjohansen 	arc_state_t		*state = buf->b_hdr->b_state;
22643290Sjohansen 	uint64_t		size = buf->b_hdr->b_size;
22653290Sjohansen 	arc_buf_contents_t	type = buf->b_hdr->b_type;
22662688Smaybee 
22672688Smaybee 	arc_adapt(size, state);
2268789Sahrens 
22692688Smaybee 	/*
22702688Smaybee 	 * We have not yet reached cache maximum size,
22712688Smaybee 	 * just allocate a new buffer.
22722688Smaybee 	 */
22734309Smaybee 	if (!arc_evict_needed(type)) {
22743290Sjohansen 		if (type == ARC_BUFC_METADATA) {
22753290Sjohansen 			buf->b_data = zio_buf_alloc(size);
22768582SBrendan.Gregg@Sun.COM 			arc_space_consume(size, ARC_SPACE_DATA);
22773290Sjohansen 		} else {
22783290Sjohansen 			ASSERT(type == ARC_BUFC_DATA);
22793290Sjohansen 			buf->b_data = zio_data_buf_alloc(size);
22808582SBrendan.Gregg@Sun.COM 			ARCSTAT_INCR(arcstat_data_size, size);
22814309Smaybee 			atomic_add_64(&arc_size, size);
22823290Sjohansen 		}
22832688Smaybee 		goto out;
22842688Smaybee 	}
22852688Smaybee 
22862688Smaybee 	/*
22872688Smaybee 	 * If we are prefetching from the mfu ghost list, this buffer
22882688Smaybee 	 * will end up on the mru list; so steal space from there.
22892688Smaybee 	 */
22903403Sbmc 	if (state == arc_mfu_ghost)
22913403Sbmc 		state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc_mru : arc_mfu;
22923403Sbmc 	else if (state == arc_mru_ghost)
22933403Sbmc 		state = arc_mru;
2294789Sahrens 
22953403Sbmc 	if (state == arc_mru || state == arc_anon) {
22963403Sbmc 		uint64_t mru_used = arc_anon->arcs_size + arc_mru->arcs_size;
22978582SBrendan.Gregg@Sun.COM 		state = (arc_mfu->arcs_lsize[type] >= size &&
22984309Smaybee 		    arc_p > mru_used) ? arc_mfu : arc_mru;
2299789Sahrens 	} else {
23002688Smaybee 		/* MFU cases */
23013403Sbmc 		uint64_t mfu_space = arc_c - arc_p;
23028582SBrendan.Gregg@Sun.COM 		state =  (arc_mru->arcs_lsize[type] >= size &&
23034309Smaybee 		    mfu_space > arc_mfu->arcs_size) ? arc_mru : arc_mfu;
23042688Smaybee 	}
23055642Smaybee 	if ((buf->b_data = arc_evict(state, NULL, size, TRUE, type)) == NULL) {
23063290Sjohansen 		if (type == ARC_BUFC_METADATA) {
23073290Sjohansen 			buf->b_data = zio_buf_alloc(size);
23088582SBrendan.Gregg@Sun.COM 			arc_space_consume(size, ARC_SPACE_DATA);
23093290Sjohansen 		} else {
23103290Sjohansen 			ASSERT(type == ARC_BUFC_DATA);
23113290Sjohansen 			buf->b_data = zio_data_buf_alloc(size);
23128582SBrendan.Gregg@Sun.COM 			ARCSTAT_INCR(arcstat_data_size, size);
23134309Smaybee 			atomic_add_64(&arc_size, size);
23143290Sjohansen 		}
23153403Sbmc 		ARCSTAT_BUMP(arcstat_recycle_miss);
23162688Smaybee 	}
23172688Smaybee 	ASSERT(buf->b_data != NULL);
23182688Smaybee out:
23192688Smaybee 	/*
23202688Smaybee 	 * Update the state size.  Note that ghost states have a
23212688Smaybee 	 * "ghost size" and so don't need to be updated.
23222688Smaybee 	 */
23232688Smaybee 	if (!GHOST_STATE(buf->b_hdr->b_state)) {
23242688Smaybee 		arc_buf_hdr_t *hdr = buf->b_hdr;
23252688Smaybee 
23263403Sbmc 		atomic_add_64(&hdr->b_state->arcs_size, size);
23272688Smaybee 		if (list_link_active(&hdr->b_arc_node)) {
23282688Smaybee 			ASSERT(refcount_is_zero(&hdr->b_refcnt));
23294309Smaybee 			atomic_add_64(&hdr->b_state->arcs_lsize[type], size);
2330789Sahrens 		}
23313298Smaybee 		/*
23323298Smaybee 		 * If we are growing the cache, and we are adding anonymous
23333403Sbmc 		 * data, and we have outgrown arc_p, update arc_p
23343298Smaybee 		 */
23353403Sbmc 		if (arc_size < arc_c && hdr->b_state == arc_anon &&
23363403Sbmc 		    arc_anon->arcs_size + arc_mru->arcs_size > arc_p)
23373403Sbmc 			arc_p = MIN(arc_c, arc_p + size);
2338789Sahrens 	}
2339789Sahrens }
2340789Sahrens 
2341789Sahrens /*
2342789Sahrens  * This routine is called whenever a buffer is accessed.
23431544Seschrock  * NOTE: the hash lock is dropped in this function.
2344789Sahrens  */
2345789Sahrens static void
arc_access(arc_buf_hdr_t * buf,kmutex_t * hash_lock)23462688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
2347789Sahrens {
234811066Srafael.vanoni@sun.com 	clock_t now;
234911066Srafael.vanoni@sun.com 
2350789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
2351789Sahrens 
23523403Sbmc 	if (buf->b_state == arc_anon) {
2353789Sahrens 		/*
2354789Sahrens 		 * This buffer is not in the cache, and does not
2355789Sahrens 		 * appear in our "ghost" list.  Add the new buffer
2356789Sahrens 		 * to the MRU state.
2357789Sahrens 		 */
2358789Sahrens 
2359789Sahrens 		ASSERT(buf->b_arc_access == 0);
236011066Srafael.vanoni@sun.com 		buf->b_arc_access = ddi_get_lbolt();
23611544Seschrock 		DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
23623403Sbmc 		arc_change_state(arc_mru, buf, hash_lock);
2363789Sahrens 
23643403Sbmc 	} else if (buf->b_state == arc_mru) {
236511066Srafael.vanoni@sun.com 		now = ddi_get_lbolt();
236611066Srafael.vanoni@sun.com 
2367789Sahrens 		/*
23682391Smaybee 		 * If this buffer is here because of a prefetch, then either:
23692391Smaybee 		 * - clear the flag if this is a "referencing" read
23702391Smaybee 		 *   (any subsequent access will bump this into the MFU state).
23712391Smaybee 		 * or
23722391Smaybee 		 * - move the buffer to the head of the list if this is
23732391Smaybee 		 *   another prefetch (to make it less likely to be evicted).
2374789Sahrens 		 */
2375789Sahrens 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
23762391Smaybee 			if (refcount_count(&buf->b_refcnt) == 0) {
23772391Smaybee 				ASSERT(list_link_active(&buf->b_arc_node));
23782391Smaybee 			} else {
23792391Smaybee 				buf->b_flags &= ~ARC_PREFETCH;
23803403Sbmc 				ARCSTAT_BUMP(arcstat_mru_hits);
23812391Smaybee 			}
238211066Srafael.vanoni@sun.com 			buf->b_arc_access = now;
2383789Sahrens 			return;
2384789Sahrens 		}
2385789Sahrens 
2386789Sahrens 		/*
2387789Sahrens 		 * This buffer has been "accessed" only once so far,
2388789Sahrens 		 * but it is still in the cache. Move it to the MFU
2389789Sahrens 		 * state.
2390789Sahrens 		 */
239111066Srafael.vanoni@sun.com 		if (now > buf->b_arc_access + ARC_MINTIME) {
2392789Sahrens 			/*
2393789Sahrens 			 * More than 125ms have passed since we
2394789Sahrens 			 * instantiated this buffer.  Move it to the
2395789Sahrens 			 * most frequently used state.
2396789Sahrens 			 */
239711066Srafael.vanoni@sun.com 			buf->b_arc_access = now;
23981544Seschrock 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
23993403Sbmc 			arc_change_state(arc_mfu, buf, hash_lock);
2400789Sahrens 		}
24013403Sbmc 		ARCSTAT_BUMP(arcstat_mru_hits);
24023403Sbmc 	} else if (buf->b_state == arc_mru_ghost) {
2403789Sahrens 		arc_state_t	*new_state;
2404789Sahrens 		/*
2405789Sahrens 		 * This buffer has been "accessed" recently, but
2406789Sahrens 		 * was evicted from the cache.  Move it to the
2407789Sahrens 		 * MFU state.
2408789Sahrens 		 */
2409789Sahrens 
2410789Sahrens 		if (buf->b_flags & ARC_PREFETCH) {
24113403Sbmc 			new_state = arc_mru;
24122391Smaybee 			if (refcount_count(&buf->b_refcnt) > 0)
24132391Smaybee 				buf->b_flags &= ~ARC_PREFETCH;
24141544Seschrock 			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
2415789Sahrens 		} else {
24163403Sbmc 			new_state = arc_mfu;
24171544Seschrock 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
2418789Sahrens 		}
2419789Sahrens 
242011066Srafael.vanoni@sun.com 		buf->b_arc_access = ddi_get_lbolt();
2421789Sahrens 		arc_change_state(new_state, buf, hash_lock);
2422789Sahrens 
24233403Sbmc 		ARCSTAT_BUMP(arcstat_mru_ghost_hits);
24243403Sbmc 	} else if (buf->b_state == arc_mfu) {
2425789Sahrens 		/*
2426789Sahrens 		 * This buffer has been accessed more than once and is
2427789Sahrens 		 * still in the cache.  Keep it in the MFU state.
2428789Sahrens 		 *
24292391Smaybee 		 * NOTE: an add_reference() that occurred when we did
24302391Smaybee 		 * the arc_read() will have kicked this off the list.
24312391Smaybee 		 * If it was a prefetch, we will explicitly move it to
24322391Smaybee 		 * the head of the list now.
2433789Sahrens 		 */
24342391Smaybee 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
24352391Smaybee 			ASSERT(refcount_count(&buf->b_refcnt) == 0);
24362391Smaybee 			ASSERT(list_link_active(&buf->b_arc_node));
24372391Smaybee 		}
24383403Sbmc 		ARCSTAT_BUMP(arcstat_mfu_hits);
243911066Srafael.vanoni@sun.com 		buf->b_arc_access = ddi_get_lbolt();
24403403Sbmc 	} else if (buf->b_state == arc_mfu_ghost) {
24413403Sbmc 		arc_state_t	*new_state = arc_mfu;
2442789Sahrens 		/*
2443789Sahrens 		 * This buffer has been accessed more than once but has
2444789Sahrens 		 * been evicted from the cache.  Move it back to the
2445789Sahrens 		 * MFU state.
2446789Sahrens 		 */
2447789Sahrens 
24482391Smaybee 		if (buf->b_flags & ARC_PREFETCH) {
24492391Smaybee 			/*
24502391Smaybee 			 * This is a prefetch access...
24512391Smaybee 			 * move this block back to the MRU state.
24522391Smaybee 			 */
24532391Smaybee 			ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0);
24543403Sbmc 			new_state = arc_mru;
24552391Smaybee 		}
24562391Smaybee 
245711066Srafael.vanoni@sun.com 		buf->b_arc_access = ddi_get_lbolt();
24581544Seschrock 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
24592391Smaybee 		arc_change_state(new_state, buf, hash_lock);
2460789Sahrens 
24613403Sbmc 		ARCSTAT_BUMP(arcstat_mfu_ghost_hits);
24625450Sbrendan 	} else if (buf->b_state == arc_l2c_only) {
24635450Sbrendan 		/*
24645450Sbrendan 		 * This buffer is on the 2nd Level ARC.
24655450Sbrendan 		 */
24665450Sbrendan 
246711066Srafael.vanoni@sun.com 		buf->b_arc_access = ddi_get_lbolt();
24685450Sbrendan 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
24695450Sbrendan 		arc_change_state(arc_mfu, buf, hash_lock);
2470789Sahrens 	} else {
2471789Sahrens 		ASSERT(!"invalid arc state");
2472789Sahrens 	}
2473789Sahrens }
2474789Sahrens 
2475789Sahrens /* a generic arc_done_func_t which you can use */
2476789Sahrens /* ARGSUSED */
2477789Sahrens void
arc_bcopy_func(zio_t * zio,arc_buf_t * buf,void * arg)2478789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
2479789Sahrens {
248012296SLin.Ling@Sun.COM 	if (zio == NULL || zio->io_error == 0)
248112296SLin.Ling@Sun.COM 		bcopy(buf->b_data, arg, buf->b_hdr->b_size);
24821544Seschrock 	VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2483789Sahrens }
2484789Sahrens 
24854309Smaybee /* a generic arc_done_func_t */
2486789Sahrens void
arc_getbuf_func(zio_t * zio,arc_buf_t * buf,void * arg)2487789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
2488789Sahrens {
2489789Sahrens 	arc_buf_t **bufp = arg;
2490789Sahrens 	if (zio && zio->io_error) {
24911544Seschrock 		VERIFY(arc_buf_remove_ref(buf, arg) == 1);
2492789Sahrens 		*bufp = NULL;
2493789Sahrens 	} else {
2494789Sahrens 		*bufp = buf;
249512296SLin.Ling@Sun.COM 		ASSERT(buf->b_data);
2496789Sahrens 	}
2497789Sahrens }
2498789Sahrens 
2499789Sahrens static void
arc_read_done(zio_t * zio)2500789Sahrens arc_read_done(zio_t *zio)
2501789Sahrens {
25021589Smaybee 	arc_buf_hdr_t	*hdr, *found;
2503789Sahrens 	arc_buf_t	*buf;
2504789Sahrens 	arc_buf_t	*abuf;	/* buffer we're assigning to callback */
2505789Sahrens 	kmutex_t	*hash_lock;
2506789Sahrens 	arc_callback_t	*callback_list, *acb;
2507789Sahrens 	int		freeable = FALSE;
2508789Sahrens 
2509789Sahrens 	buf = zio->io_private;
2510789Sahrens 	hdr = buf->b_hdr;
2511789Sahrens 
25121589Smaybee 	/*
25131589Smaybee 	 * The hdr was inserted into hash-table and removed from lists
25141589Smaybee 	 * prior to starting I/O.  We should find this header, since
25151589Smaybee 	 * it's in the hash table, and it should be legit since it's
25161589Smaybee 	 * not possible to evict it during the I/O.  The only possible
25171589Smaybee 	 * reason for it not to be found is if we were freed during the
25181589Smaybee 	 * read.
25191589Smaybee 	 */
25208636SMark.Maybee@Sun.COM 	found = buf_hash_find(hdr->b_spa, &hdr->b_dva, hdr->b_birth,
25213093Sahrens 	    &hash_lock);
2522789Sahrens 
25231589Smaybee 	ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
25245450Sbrendan 	    (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))) ||
25255450Sbrendan 	    (found == hdr && HDR_L2_READING(hdr)));
25265450Sbrendan 
25276987Sbrendan 	hdr->b_flags &= ~ARC_L2_EVICTED;
25285450Sbrendan 	if (l2arc_noprefetch && (hdr->b_flags & ARC_PREFETCH))
25297237Sek110237 		hdr->b_flags &= ~ARC_L2CACHE;
2530789Sahrens 
2531789Sahrens 	/* byteswap if necessary */
2532789Sahrens 	callback_list = hdr->b_acb;
2533789Sahrens 	ASSERT(callback_list != NULL);
253410839Swilliam.gorrell@sun.com 	if (BP_SHOULD_BYTESWAP(zio->io_bp) && zio->io_error == 0) {
25357046Sahrens 		arc_byteswap_func_t *func = BP_GET_LEVEL(zio->io_bp) > 0 ?
25367046Sahrens 		    byteswap_uint64_array :
25377046Sahrens 		    dmu_ot[BP_GET_TYPE(zio->io_bp)].ot_byteswap;
25387046Sahrens 		func(buf->b_data, hdr->b_size);
25397046Sahrens 	}
2540789Sahrens 
25415450Sbrendan 	arc_cksum_compute(buf, B_FALSE);
25423093Sahrens 
254310922SJeff.Bonwick@Sun.COM 	if (hash_lock && zio->io_error == 0 && hdr->b_state == arc_anon) {
254410922SJeff.Bonwick@Sun.COM 		/*
254510922SJeff.Bonwick@Sun.COM 		 * Only call arc_access on anonymous buffers.  This is because
254610922SJeff.Bonwick@Sun.COM 		 * if we've issued an I/O for an evicted buffer, we've already
254710922SJeff.Bonwick@Sun.COM 		 * called arc_access (to prevent any simultaneous readers from
254810922SJeff.Bonwick@Sun.COM 		 * getting confused).
254910922SJeff.Bonwick@Sun.COM 		 */
255010922SJeff.Bonwick@Sun.COM 		arc_access(hdr, hash_lock);
255110922SJeff.Bonwick@Sun.COM 	}
255210922SJeff.Bonwick@Sun.COM 
2553789Sahrens 	/* create copies of the data buffer for the callers */
2554789Sahrens 	abuf = buf;
2555789Sahrens 	for (acb = callback_list; acb; acb = acb->acb_next) {
2556789Sahrens 		if (acb->acb_done) {
25572688Smaybee 			if (abuf == NULL)
25582688Smaybee 				abuf = arc_buf_clone(buf);
2559789Sahrens 			acb->acb_buf = abuf;
2560789Sahrens 			abuf = NULL;
2561789Sahrens 		}
2562789Sahrens 	}
2563789Sahrens 	hdr->b_acb = NULL;
2564789Sahrens 	hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
25651544Seschrock 	ASSERT(!HDR_BUF_AVAILABLE(hdr));
256610922SJeff.Bonwick@Sun.COM 	if (abuf == buf) {
256710922SJeff.Bonwick@Sun.COM 		ASSERT(buf->b_efunc == NULL);
256810922SJeff.Bonwick@Sun.COM 		ASSERT(hdr->b_datacnt == 1);
25691544Seschrock 		hdr->b_flags |= ARC_BUF_AVAILABLE;
257010922SJeff.Bonwick@Sun.COM 	}
2571789Sahrens 
2572789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
2573789Sahrens 
2574789Sahrens 	if (zio->io_error != 0) {
2575789Sahrens 		hdr->b_flags |= ARC_IO_ERROR;
25763403Sbmc 		if (hdr->b_state != arc_anon)
25773403Sbmc 			arc_change_state(arc_anon, hdr, hash_lock);
25781544Seschrock 		if (HDR_IN_HASH_TABLE(hdr))
25791544Seschrock 			buf_hash_remove(hdr);
2580789Sahrens 		freeable = refcount_is_zero(&hdr->b_refcnt);
2581789Sahrens 	}
2582789Sahrens 
25831544Seschrock 	/*
25842391Smaybee 	 * Broadcast before we drop the hash_lock to avoid the possibility
25852391Smaybee 	 * that the hdr (and hence the cv) might be freed before we get to
25862391Smaybee 	 * the cv_broadcast().
25871544Seschrock 	 */
25881544Seschrock 	cv_broadcast(&hdr->b_cv);
25891544Seschrock 
25901589Smaybee 	if (hash_lock) {
25912688Smaybee 		mutex_exit(hash_lock);
2592789Sahrens 	} else {
2593789Sahrens 		/*
2594789Sahrens 		 * This block was freed while we waited for the read to
2595789Sahrens 		 * complete.  It has been removed from the hash table and
2596789Sahrens 		 * moved to the anonymous state (so that it won't show up
2597789Sahrens 		 * in the cache).
2598789Sahrens 		 */
25993403Sbmc 		ASSERT3P(hdr->b_state, ==, arc_anon);
2600789Sahrens 		freeable = refcount_is_zero(&hdr->b_refcnt);
2601789Sahrens 	}
2602789Sahrens 
2603789Sahrens 	/* execute each callback and free its structure */
2604789Sahrens 	while ((acb = callback_list) != NULL) {
2605789Sahrens 		if (acb->acb_done)
2606789Sahrens 			acb->acb_done(zio, acb->acb_buf, acb->acb_private);
2607789Sahrens 
2608789Sahrens 		if (acb->acb_zio_dummy != NULL) {
2609789Sahrens 			acb->acb_zio_dummy->io_error = zio->io_error;
2610789Sahrens 			zio_nowait(acb->acb_zio_dummy);
2611789Sahrens 		}
2612789Sahrens 
2613789Sahrens 		callback_list = acb->acb_next;
2614789Sahrens 		kmem_free(acb, sizeof (arc_callback_t));
2615789Sahrens 	}
2616789Sahrens 
2617789Sahrens 	if (freeable)
26181544Seschrock 		arc_hdr_destroy(hdr);
2619789Sahrens }
2620789Sahrens 
2621789Sahrens /*
2622789Sahrens  * "Read" the block block at the specified DVA (in bp) via the
2623789Sahrens  * cache.  If the block is found in the cache, invoke the provided
2624789Sahrens  * callback immediately and return.  Note that the `zio' parameter
2625789Sahrens  * in the callback will be NULL in this case, since no IO was
2626789Sahrens  * required.  If the block is not in the cache pass the read request
2627789Sahrens  * on to the spa with a substitute callback function, so that the
2628789Sahrens  * requested block will be added to the cache.
2629789Sahrens  *
2630789Sahrens  * If a read request arrives for a block that has a read in-progress,
2631789Sahrens  * either wait for the in-progress read to complete (and return the
2632789Sahrens  * results); or, if this is a read with a "done" func, add a record
2633789Sahrens  * to the read to invoke the "done" func when the read completes,
2634789Sahrens  * and return; or just return.
2635789Sahrens  *
2636789Sahrens  * arc_read_done() will invoke all the requested "done" functions
2637789Sahrens  * for readers of this block.
26387046Sahrens  *
26397046Sahrens  * Normal callers should use arc_read and pass the arc buffer and offset
26407046Sahrens  * for the bp.  But if you know you don't need locking, you can use
26418213SSuhasini.Peddada@Sun.COM  * arc_read_bp.
2642789Sahrens  */
2643789Sahrens int
arc_read(zio_t * pio,spa_t * spa,const blkptr_t * bp,arc_buf_t * pbuf,arc_done_func_t * done,void * private,int priority,int zio_flags,uint32_t * arc_flags,const zbookmark_t * zb)264410922SJeff.Bonwick@Sun.COM arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp, arc_buf_t *pbuf,
26457237Sek110237     arc_done_func_t *done, void *private, int priority, int zio_flags,
26467046Sahrens     uint32_t *arc_flags, const zbookmark_t *zb)
26477046Sahrens {
26487046Sahrens 	int err;
26497046Sahrens 
265012296SLin.Ling@Sun.COM 	if (pbuf == NULL) {
265112296SLin.Ling@Sun.COM 		/*
265212296SLin.Ling@Sun.COM 		 * XXX This happens from traverse callback funcs, for
265312296SLin.Ling@Sun.COM 		 * the objset_phys_t block.
265412296SLin.Ling@Sun.COM 		 */
265512296SLin.Ling@Sun.COM 		return (arc_read_nolock(pio, spa, bp, done, private, priority,
265612296SLin.Ling@Sun.COM 		    zio_flags, arc_flags, zb));
265712296SLin.Ling@Sun.COM 	}
265812296SLin.Ling@Sun.COM 
26597046Sahrens 	ASSERT(!refcount_is_zero(&pbuf->b_hdr->b_refcnt));
26607046Sahrens 	ASSERT3U((char *)bp - (char *)pbuf->b_data, <, pbuf->b_hdr->b_size);
266112296SLin.Ling@Sun.COM 	rw_enter(&pbuf->b_data_lock, RW_READER);
26627046Sahrens 
26637046Sahrens 	err = arc_read_nolock(pio, spa, bp, done, private, priority,
26647237Sek110237 	    zio_flags, arc_flags, zb);
266512296SLin.Ling@Sun.COM 	rw_exit(&pbuf->b_data_lock);
26669396SMatthew.Ahrens@Sun.COM 
26677046Sahrens 	return (err);
26687046Sahrens }
26697046Sahrens 
26707046Sahrens int
arc_read_nolock(zio_t * pio,spa_t * spa,const blkptr_t * bp,arc_done_func_t * done,void * private,int priority,int zio_flags,uint32_t * arc_flags,const zbookmark_t * zb)267110922SJeff.Bonwick@Sun.COM arc_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bp,
26727237Sek110237     arc_done_func_t *done, void *private, int priority, int zio_flags,
26737046Sahrens     uint32_t *arc_flags, const zbookmark_t *zb)
2674789Sahrens {
2675789Sahrens 	arc_buf_hdr_t *hdr;
2676789Sahrens 	arc_buf_t *buf;
2677789Sahrens 	kmutex_t *hash_lock;
26785450Sbrendan 	zio_t *rzio;
26798636SMark.Maybee@Sun.COM 	uint64_t guid = spa_guid(spa);
2680789Sahrens 
2681789Sahrens top:
268210922SJeff.Bonwick@Sun.COM 	hdr = buf_hash_find(guid, BP_IDENTITY(bp), BP_PHYSICAL_BIRTH(bp),
268310922SJeff.Bonwick@Sun.COM 	    &hash_lock);
26841544Seschrock 	if (hdr && hdr->b_datacnt > 0) {
2685789Sahrens 
26862391Smaybee 		*arc_flags |= ARC_CACHED;
26872391Smaybee 
2688789Sahrens 		if (HDR_IO_IN_PROGRESS(hdr)) {
26892391Smaybee 
26902391Smaybee 			if (*arc_flags & ARC_WAIT) {
26912391Smaybee 				cv_wait(&hdr->b_cv, hash_lock);
26922391Smaybee 				mutex_exit(hash_lock);
26932391Smaybee 				goto top;
26942391Smaybee 			}
26952391Smaybee 			ASSERT(*arc_flags & ARC_NOWAIT);
26962391Smaybee 
26972391Smaybee 			if (done) {
2698789Sahrens 				arc_callback_t	*acb = NULL;
2699789Sahrens 
2700789Sahrens 				acb = kmem_zalloc(sizeof (arc_callback_t),
2701789Sahrens 				    KM_SLEEP);
2702789Sahrens 				acb->acb_done = done;
2703789Sahrens 				acb->acb_private = private;
2704789Sahrens 				if (pio != NULL)
2705789Sahrens 					acb->acb_zio_dummy = zio_null(pio,
27068632SBill.Moore@Sun.COM 					    spa, NULL, NULL, NULL, zio_flags);
2707789Sahrens 
2708789Sahrens 				ASSERT(acb->acb_done != NULL);
2709789Sahrens 				acb->acb_next = hdr->b_acb;
2710789Sahrens 				hdr->b_acb = acb;
2711789Sahrens 				add_reference(hdr, hash_lock, private);
2712789Sahrens 				mutex_exit(hash_lock);
2713789Sahrens 				return (0);
2714789Sahrens 			}
2715789Sahrens 			mutex_exit(hash_lock);
2716789Sahrens 			return (0);
2717789Sahrens 		}
2718789Sahrens 
27193403Sbmc 		ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
2720789Sahrens 
27211544Seschrock 		if (done) {
27222688Smaybee 			add_reference(hdr, hash_lock, private);
27231544Seschrock 			/*
27241544Seschrock 			 * If this block is already in use, create a new
27251544Seschrock 			 * copy of the data so that we will be guaranteed
27261544Seschrock 			 * that arc_release() will always succeed.
27271544Seschrock 			 */
27281544Seschrock 			buf = hdr->b_buf;
27291544Seschrock 			ASSERT(buf);
27301544Seschrock 			ASSERT(buf->b_data);
27312688Smaybee 			if (HDR_BUF_AVAILABLE(hdr)) {
27321544Seschrock 				ASSERT(buf->b_efunc == NULL);
27331544Seschrock 				hdr->b_flags &= ~ARC_BUF_AVAILABLE;
27342688Smaybee 			} else {
27352688Smaybee 				buf = arc_buf_clone(buf);
27361544Seschrock 			}
273710922SJeff.Bonwick@Sun.COM 
27382391Smaybee 		} else if (*arc_flags & ARC_PREFETCH &&
27392391Smaybee 		    refcount_count(&hdr->b_refcnt) == 0) {
27402391Smaybee 			hdr->b_flags |= ARC_PREFETCH;
2741789Sahrens 		}
2742789Sahrens 		DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
27432688Smaybee 		arc_access(hdr, hash_lock);
27447237Sek110237 		if (*arc_flags & ARC_L2CACHE)
27457237Sek110237 			hdr->b_flags |= ARC_L2CACHE;
27462688Smaybee 		mutex_exit(hash_lock);
27473403Sbmc 		ARCSTAT_BUMP(arcstat_hits);
27483403Sbmc 		ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
27493403Sbmc 		    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
27503403Sbmc 		    data, metadata, hits);
27513403Sbmc 
2752789Sahrens 		if (done)
2753789Sahrens 			done(NULL, buf, private);
2754789Sahrens 	} else {
2755789Sahrens 		uint64_t size = BP_GET_LSIZE(bp);
2756789Sahrens 		arc_callback_t	*acb;
27576987Sbrendan 		vdev_t *vd = NULL;
27589215SGeorge.Wilson@Sun.COM 		uint64_t addr;
27598582SBrendan.Gregg@Sun.COM 		boolean_t devw = B_FALSE;
2760789Sahrens 
2761789Sahrens 		if (hdr == NULL) {
2762789Sahrens 			/* this block is not in the cache */
2763789Sahrens 			arc_buf_hdr_t	*exists;
27643290Sjohansen 			arc_buf_contents_t type = BP_GET_BUFC_TYPE(bp);
27653290Sjohansen 			buf = arc_buf_alloc(spa, size, private, type);
2766789Sahrens 			hdr = buf->b_hdr;
2767789Sahrens 			hdr->b_dva = *BP_IDENTITY(bp);
276810922SJeff.Bonwick@Sun.COM 			hdr->b_birth = BP_PHYSICAL_BIRTH(bp);
2769789Sahrens 			hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
2770789Sahrens 			exists = buf_hash_insert(hdr, &hash_lock);
2771789Sahrens 			if (exists) {
2772789Sahrens 				/* somebody beat us to the hash insert */
2773789Sahrens 				mutex_exit(hash_lock);
277412296SLin.Ling@Sun.COM 				buf_discard_identity(hdr);
27751544Seschrock 				(void) arc_buf_remove_ref(buf, private);
2776789Sahrens 				goto top; /* restart the IO request */
2777789Sahrens 			}
27782391Smaybee 			/* if this is a prefetch, we don't have a reference */
27792391Smaybee 			if (*arc_flags & ARC_PREFETCH) {
27802391Smaybee 				(void) remove_reference(hdr, hash_lock,
27812391Smaybee 				    private);
27822391Smaybee 				hdr->b_flags |= ARC_PREFETCH;
27832391Smaybee 			}
27847237Sek110237 			if (*arc_flags & ARC_L2CACHE)
27857237Sek110237 				hdr->b_flags |= ARC_L2CACHE;
27862391Smaybee 			if (BP_GET_LEVEL(bp) > 0)
27872391Smaybee 				hdr->b_flags |= ARC_INDIRECT;
2788789Sahrens 		} else {
2789789Sahrens 			/* this block is in the ghost cache */
27901544Seschrock 			ASSERT(GHOST_STATE(hdr->b_state));
27911544Seschrock 			ASSERT(!HDR_IO_IN_PROGRESS(hdr));
27922391Smaybee 			ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0);
27932391Smaybee 			ASSERT(hdr->b_buf == NULL);
2794789Sahrens 
27952391Smaybee 			/* if this is a prefetch, we don't have a reference */
27962391Smaybee 			if (*arc_flags & ARC_PREFETCH)
27972391Smaybee 				hdr->b_flags |= ARC_PREFETCH;
27982391Smaybee 			else
27992391Smaybee 				add_reference(hdr, hash_lock, private);
28007237Sek110237 			if (*arc_flags & ARC_L2CACHE)
28017237Sek110237 				hdr->b_flags |= ARC_L2CACHE;
28026245Smaybee 			buf = kmem_cache_alloc(buf_cache, KM_PUSHPAGE);
28031544Seschrock 			buf->b_hdr = hdr;
28042688Smaybee 			buf->b_data = NULL;
28051544Seschrock 			buf->b_efunc = NULL;
28061544Seschrock 			buf->b_private = NULL;
28071544Seschrock 			buf->b_next = NULL;
28081544Seschrock 			hdr->b_buf = buf;
28091544Seschrock 			ASSERT(hdr->b_datacnt == 0);
28101544Seschrock 			hdr->b_datacnt = 1;
281112033Swilliam.gorrell@sun.com 			arc_get_data_buf(buf);
281211805Swilliam.gorrell@sun.com 			arc_access(hdr, hash_lock);
2813789Sahrens 		}
2814789Sahrens 
281511805Swilliam.gorrell@sun.com 		ASSERT(!GHOST_STATE(hdr->b_state));
281611805Swilliam.gorrell@sun.com 
2817789Sahrens 		acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
2818789Sahrens 		acb->acb_done = done;
2819789Sahrens 		acb->acb_private = private;
2820789Sahrens 
2821789Sahrens 		ASSERT(hdr->b_acb == NULL);
2822789Sahrens 		hdr->b_acb = acb;
2823789Sahrens 		hdr->b_flags |= ARC_IO_IN_PROGRESS;
2824789Sahrens 
28257754SJeff.Bonwick@Sun.COM 		if (HDR_L2CACHE(hdr) && hdr->b_l2hdr != NULL &&
28267754SJeff.Bonwick@Sun.COM 		    (vd = hdr->b_l2hdr->b_dev->l2ad_vdev) != NULL) {
28278582SBrendan.Gregg@Sun.COM 			devw = hdr->b_l2hdr->b_dev->l2ad_writing;
28286987Sbrendan 			addr = hdr->b_l2hdr->b_daddr;
28297754SJeff.Bonwick@Sun.COM 			/*
28307754SJeff.Bonwick@Sun.COM 			 * Lock out device removal.
28317754SJeff.Bonwick@Sun.COM 			 */
28327754SJeff.Bonwick@Sun.COM 			if (vdev_is_dead(vd) ||
28337754SJeff.Bonwick@Sun.COM 			    !spa_config_tryenter(spa, SCL_L2ARC, vd, RW_READER))
28347754SJeff.Bonwick@Sun.COM 				vd = NULL;
28356987Sbrendan 		}
28366987Sbrendan 
28376987Sbrendan 		mutex_exit(hash_lock);
28386987Sbrendan 
2839789Sahrens 		ASSERT3U(hdr->b_size, ==, size);
284010409SBrendan.Gregg@Sun.COM 		DTRACE_PROBE4(arc__miss, arc_buf_hdr_t *, hdr, blkptr_t *, bp,
284110409SBrendan.Gregg@Sun.COM 		    uint64_t, size, zbookmark_t *, zb);
28423403Sbmc 		ARCSTAT_BUMP(arcstat_misses);
28433403Sbmc 		ARCSTAT_CONDSTAT(!(hdr->b_flags & ARC_PREFETCH),
28443403Sbmc 		    demand, prefetch, hdr->b_type != ARC_BUFC_METADATA,
28453403Sbmc 		    data, metadata, misses);
28461544Seschrock 
28478582SBrendan.Gregg@Sun.COM 		if (vd != NULL && l2arc_ndev != 0 && !(l2arc_norw && devw)) {
28486987Sbrendan 			/*
28496987Sbrendan 			 * Read from the L2ARC if the following are true:
28506987Sbrendan 			 * 1. The L2ARC vdev was previously cached.
28516987Sbrendan 			 * 2. This buffer still has L2ARC metadata.
28526987Sbrendan 			 * 3. This buffer isn't currently writing to the L2ARC.
28536987Sbrendan 			 * 4. The L2ARC entry wasn't evicted, which may
28546987Sbrendan 			 *    also have invalidated the vdev.
28558582SBrendan.Gregg@Sun.COM 			 * 5. This isn't prefetch and l2arc_noprefetch is set.
28566987Sbrendan 			 */
28577754SJeff.Bonwick@Sun.COM 			if (hdr->b_l2hdr != NULL &&
28588582SBrendan.Gregg@Sun.COM 			    !HDR_L2_WRITING(hdr) && !HDR_L2_EVICTED(hdr) &&
28598582SBrendan.Gregg@Sun.COM 			    !(l2arc_noprefetch && HDR_PREFETCH(hdr))) {
28605450Sbrendan 				l2arc_read_callback_t *cb;
28615450Sbrendan 
28626643Seschrock 				DTRACE_PROBE1(l2arc__hit, arc_buf_hdr_t *, hdr);
28636643Seschrock 				ARCSTAT_BUMP(arcstat_l2_hits);
28646643Seschrock 
28655450Sbrendan 				cb = kmem_zalloc(sizeof (l2arc_read_callback_t),
28665450Sbrendan 				    KM_SLEEP);
28675450Sbrendan 				cb->l2rcb_buf = buf;
28685450Sbrendan 				cb->l2rcb_spa = spa;
28695450Sbrendan 				cb->l2rcb_bp = *bp;
28705450Sbrendan 				cb->l2rcb_zb = *zb;
28717237Sek110237 				cb->l2rcb_flags = zio_flags;
28725450Sbrendan 
28735450Sbrendan 				/*
28747754SJeff.Bonwick@Sun.COM 				 * l2arc read.  The SCL_L2ARC lock will be
28757754SJeff.Bonwick@Sun.COM 				 * released by l2arc_read_done().
28765450Sbrendan 				 */
28775450Sbrendan 				rzio = zio_read_phys(pio, vd, addr, size,
28785450Sbrendan 				    buf->b_data, ZIO_CHECKSUM_OFF,
28797237Sek110237 				    l2arc_read_done, cb, priority, zio_flags |
28807361SBrendan.Gregg@Sun.COM 				    ZIO_FLAG_DONT_CACHE | ZIO_FLAG_CANFAIL |
28817754SJeff.Bonwick@Sun.COM 				    ZIO_FLAG_DONT_PROPAGATE |
28827754SJeff.Bonwick@Sun.COM 				    ZIO_FLAG_DONT_RETRY, B_FALSE);
28835450Sbrendan 				DTRACE_PROBE2(l2arc__read, vdev_t *, vd,
28845450Sbrendan 				    zio_t *, rzio);
28858582SBrendan.Gregg@Sun.COM 				ARCSTAT_INCR(arcstat_l2_read_bytes, size);
28866987Sbrendan 
28876987Sbrendan 				if (*arc_flags & ARC_NOWAIT) {
28886987Sbrendan 					zio_nowait(rzio);
28896987Sbrendan 					return (0);
28906987Sbrendan 				}
28916987Sbrendan 
28926987Sbrendan 				ASSERT(*arc_flags & ARC_WAIT);
28936987Sbrendan 				if (zio_wait(rzio) == 0)
28946987Sbrendan 					return (0);
28956987Sbrendan 
28966987Sbrendan 				/* l2arc read error; goto zio_read() */
28975450Sbrendan 			} else {
28985450Sbrendan 				DTRACE_PROBE1(l2arc__miss,
28995450Sbrendan 				    arc_buf_hdr_t *, hdr);
29005450Sbrendan 				ARCSTAT_BUMP(arcstat_l2_misses);
29015450Sbrendan 				if (HDR_L2_WRITING(hdr))
29025450Sbrendan 					ARCSTAT_BUMP(arcstat_l2_rw_clash);
29037754SJeff.Bonwick@Sun.COM 				spa_config_exit(spa, SCL_L2ARC, vd);
29045450Sbrendan 			}
29058582SBrendan.Gregg@Sun.COM 		} else {
29068628SBill.Moore@Sun.COM 			if (vd != NULL)
29078628SBill.Moore@Sun.COM 				spa_config_exit(spa, SCL_L2ARC, vd);
29088582SBrendan.Gregg@Sun.COM 			if (l2arc_ndev != 0) {
29098582SBrendan.Gregg@Sun.COM 				DTRACE_PROBE1(l2arc__miss,
29108582SBrendan.Gregg@Sun.COM 				    arc_buf_hdr_t *, hdr);
29118582SBrendan.Gregg@Sun.COM 				ARCSTAT_BUMP(arcstat_l2_misses);
29128582SBrendan.Gregg@Sun.COM 			}
29135450Sbrendan 		}
29146643Seschrock 
2915789Sahrens 		rzio = zio_read(pio, spa, bp, buf->b_data, size,
29167237Sek110237 		    arc_read_done, buf, priority, zio_flags, zb);
2917789Sahrens 
29182391Smaybee 		if (*arc_flags & ARC_WAIT)
2919789Sahrens 			return (zio_wait(rzio));
2920789Sahrens 
29212391Smaybee 		ASSERT(*arc_flags & ARC_NOWAIT);
2922789Sahrens 		zio_nowait(rzio);
2923789Sahrens 	}
2924789Sahrens 	return (0);
2925789Sahrens }
2926789Sahrens 
29271544Seschrock void
arc_set_callback(arc_buf_t * buf,arc_evict_func_t * func,void * private)29281544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
29291544Seschrock {
29301544Seschrock 	ASSERT(buf->b_hdr != NULL);
29313403Sbmc 	ASSERT(buf->b_hdr->b_state != arc_anon);
29321544Seschrock 	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
293310922SJeff.Bonwick@Sun.COM 	ASSERT(buf->b_efunc == NULL);
293410922SJeff.Bonwick@Sun.COM 	ASSERT(!HDR_BUF_AVAILABLE(buf->b_hdr));
293510922SJeff.Bonwick@Sun.COM 
29361544Seschrock 	buf->b_efunc = func;
29371544Seschrock 	buf->b_private = private;
29381544Seschrock }
29391544Seschrock 
29401544Seschrock /*
29411544Seschrock  * This is used by the DMU to let the ARC know that a buffer is
29421544Seschrock  * being evicted, so the ARC should clean up.  If this arc buf
29431544Seschrock  * is not yet in the evicted state, it will be put there.
29441544Seschrock  */
29451544Seschrock int
arc_buf_evict(arc_buf_t * buf)29461544Seschrock arc_buf_evict(arc_buf_t *buf)
29471544Seschrock {
29482887Smaybee 	arc_buf_hdr_t *hdr;
29491544Seschrock 	kmutex_t *hash_lock;
29501544Seschrock 	arc_buf_t **bufp;
29511544Seschrock 
295212296SLin.Ling@Sun.COM 	mutex_enter(&buf->b_evict_lock);
29532887Smaybee 	hdr = buf->b_hdr;
29541544Seschrock 	if (hdr == NULL) {
29551544Seschrock 		/*
29561544Seschrock 		 * We are in arc_do_user_evicts().
29571544Seschrock 		 */
29581544Seschrock 		ASSERT(buf->b_data == NULL);
295912296SLin.Ling@Sun.COM 		mutex_exit(&buf->b_evict_lock);
29601544Seschrock 		return (0);
29617545SMark.Maybee@Sun.COM 	} else if (buf->b_data == NULL) {
29627545SMark.Maybee@Sun.COM 		arc_buf_t copy = *buf; /* structure assignment */
29637545SMark.Maybee@Sun.COM 		/*
29647545SMark.Maybee@Sun.COM 		 * We are on the eviction list; process this buffer now
29657545SMark.Maybee@Sun.COM 		 * but let arc_do_user_evicts() do the reaping.
29667545SMark.Maybee@Sun.COM 		 */
29677545SMark.Maybee@Sun.COM 		buf->b_efunc = NULL;
296812296SLin.Ling@Sun.COM 		mutex_exit(&buf->b_evict_lock);
29697545SMark.Maybee@Sun.COM 		VERIFY(copy.b_efunc(&copy) == 0);
29707545SMark.Maybee@Sun.COM 		return (1);
29711544Seschrock 	}
29722887Smaybee 	hash_lock = HDR_LOCK(hdr);
29731544Seschrock 	mutex_enter(hash_lock);
297412296SLin.Ling@Sun.COM 	hdr = buf->b_hdr;
297512296SLin.Ling@Sun.COM 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
297612296SLin.Ling@Sun.COM 
29772724Smaybee 	ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
29783403Sbmc 	ASSERT(hdr->b_state == arc_mru || hdr->b_state == arc_mfu);
29791544Seschrock 
29801544Seschrock 	/*
29811544Seschrock 	 * Pull this buffer off of the hdr
29821544Seschrock 	 */
29831544Seschrock 	bufp = &hdr->b_buf;
29841544Seschrock 	while (*bufp != buf)
29851544Seschrock 		bufp = &(*bufp)->b_next;
29861544Seschrock 	*bufp = buf->b_next;
29871544Seschrock 
29881544Seschrock 	ASSERT(buf->b_data != NULL);
29892688Smaybee 	arc_buf_destroy(buf, FALSE, FALSE);
29901544Seschrock 
29911544Seschrock 	if (hdr->b_datacnt == 0) {
29921544Seschrock 		arc_state_t *old_state = hdr->b_state;
29931544Seschrock 		arc_state_t *evicted_state;
29941544Seschrock 
299512296SLin.Ling@Sun.COM 		ASSERT(hdr->b_buf == NULL);
29961544Seschrock 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
29971544Seschrock 
29981544Seschrock 		evicted_state =
29993403Sbmc 		    (old_state == arc_mru) ? arc_mru_ghost : arc_mfu_ghost;
30001544Seschrock 
30013403Sbmc 		mutex_enter(&old_state->arcs_mtx);
30023403Sbmc 		mutex_enter(&evicted_state->arcs_mtx);
30031544Seschrock 
30041544Seschrock 		arc_change_state(evicted_state, hdr, hash_lock);
30051544Seschrock 		ASSERT(HDR_IN_HASH_TABLE(hdr));
30065450Sbrendan 		hdr->b_flags |= ARC_IN_HASH_TABLE;
30075450Sbrendan 		hdr->b_flags &= ~ARC_BUF_AVAILABLE;
30081544Seschrock 
30093403Sbmc 		mutex_exit(&evicted_state->arcs_mtx);
30103403Sbmc 		mutex_exit(&old_state->arcs_mtx);
30111544Seschrock 	}
30121544Seschrock 	mutex_exit(hash_lock);
301312296SLin.Ling@Sun.COM 	mutex_exit(&buf->b_evict_lock);
30141819Smaybee 
30151544Seschrock 	VERIFY(buf->b_efunc(buf) == 0);
30161544Seschrock 	buf->b_efunc = NULL;
30171544Seschrock 	buf->b_private = NULL;
30181544Seschrock 	buf->b_hdr = NULL;
301912296SLin.Ling@Sun.COM 	buf->b_next = NULL;
30201544Seschrock 	kmem_cache_free(buf_cache, buf);
30211544Seschrock 	return (1);
30221544Seschrock }
30231544Seschrock 
3024789Sahrens /*
3025789Sahrens  * Release this buffer from the cache.  This must be done
3026789Sahrens  * after a read and prior to modifying the buffer contents.
3027789Sahrens  * If the buffer has more than one reference, we must make
30287046Sahrens  * a new hdr for the buffer.
3029789Sahrens  */
3030789Sahrens void
arc_release(arc_buf_t * buf,void * tag)3031789Sahrens arc_release(arc_buf_t *buf, void *tag)
3032789Sahrens {
30337545SMark.Maybee@Sun.COM 	arc_buf_hdr_t *hdr;
303412296SLin.Ling@Sun.COM 	kmutex_t *hash_lock = NULL;
30357545SMark.Maybee@Sun.COM 	l2arc_buf_hdr_t *l2hdr;
30365450Sbrendan 	uint64_t buf_size;
303712296SLin.Ling@Sun.COM 
303812296SLin.Ling@Sun.COM 	/*
303912296SLin.Ling@Sun.COM 	 * It would be nice to assert that if it's DMU metadata (level >
304012296SLin.Ling@Sun.COM 	 * 0 || it's the dnode file), then it must be syncing context.
304112296SLin.Ling@Sun.COM 	 * But we don't know that information at this level.
304212296SLin.Ling@Sun.COM 	 */
304312296SLin.Ling@Sun.COM 
304412296SLin.Ling@Sun.COM 	mutex_enter(&buf->b_evict_lock);
30457545SMark.Maybee@Sun.COM 	hdr = buf->b_hdr;
30467545SMark.Maybee@Sun.COM 
3047789Sahrens 	/* this buffer is not on any list */
3048789Sahrens 	ASSERT(refcount_count(&hdr->b_refcnt) > 0);
3049789Sahrens 
30503403Sbmc 	if (hdr->b_state == arc_anon) {
3051789Sahrens 		/* this buffer is already released */
30521544Seschrock 		ASSERT(buf->b_efunc == NULL);
30539274SBrendan.Gregg@Sun.COM 	} else {
30549274SBrendan.Gregg@Sun.COM 		hash_lock = HDR_LOCK(hdr);
30559274SBrendan.Gregg@Sun.COM 		mutex_enter(hash_lock);
305612296SLin.Ling@Sun.COM 		hdr = buf->b_hdr;
305712296SLin.Ling@Sun.COM 		ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
3058789Sahrens 	}
3059789Sahrens 
30607545SMark.Maybee@Sun.COM 	l2hdr = hdr->b_l2hdr;
30617545SMark.Maybee@Sun.COM 	if (l2hdr) {
30627545SMark.Maybee@Sun.COM 		mutex_enter(&l2arc_buflist_mtx);
30637545SMark.Maybee@Sun.COM 		hdr->b_l2hdr = NULL;
30647545SMark.Maybee@Sun.COM 		buf_size = hdr->b_size;
30657545SMark.Maybee@Sun.COM 	}
30667545SMark.Maybee@Sun.COM 
30671544Seschrock 	/*
30681544Seschrock 	 * Do we have more than one buf?
30691544Seschrock 	 */
30707545SMark.Maybee@Sun.COM 	if (hdr->b_datacnt > 1) {
3071789Sahrens 		arc_buf_hdr_t *nhdr;
3072789Sahrens 		arc_buf_t **bufp;
3073789Sahrens 		uint64_t blksz = hdr->b_size;
30748636SMark.Maybee@Sun.COM 		uint64_t spa = hdr->b_spa;
30753290Sjohansen 		arc_buf_contents_t type = hdr->b_type;
30765450Sbrendan 		uint32_t flags = hdr->b_flags;
3077789Sahrens 
30787545SMark.Maybee@Sun.COM 		ASSERT(hdr->b_buf != buf || buf->b_next != NULL);
3079789Sahrens 		/*
308012296SLin.Ling@Sun.COM 		 * Pull the data off of this hdr and attach it to
308112296SLin.Ling@Sun.COM 		 * a new anonymous hdr.
3082789Sahrens 		 */
30831544Seschrock 		(void) remove_reference(hdr, hash_lock, tag);
3084789Sahrens 		bufp = &hdr->b_buf;
30851544Seschrock 		while (*bufp != buf)
3086789Sahrens 			bufp = &(*bufp)->b_next;
308712296SLin.Ling@Sun.COM 		*bufp = buf->b_next;
30883897Smaybee 		buf->b_next = NULL;
30891544Seschrock 
30903403Sbmc 		ASSERT3U(hdr->b_state->arcs_size, >=, hdr->b_size);
30913403Sbmc 		atomic_add_64(&hdr->b_state->arcs_size, -hdr->b_size);
30921544Seschrock 		if (refcount_is_zero(&hdr->b_refcnt)) {
30934309Smaybee 			uint64_t *size = &hdr->b_state->arcs_lsize[hdr->b_type];
30944309Smaybee 			ASSERT3U(*size, >=, hdr->b_size);
30954309Smaybee 			atomic_add_64(size, -hdr->b_size);
30961544Seschrock 		}
30971544Seschrock 		hdr->b_datacnt -= 1;
30983547Smaybee 		arc_cksum_verify(buf);
30991544Seschrock 
3100789Sahrens 		mutex_exit(hash_lock);
3101789Sahrens 
31026245Smaybee 		nhdr = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
3103789Sahrens 		nhdr->b_size = blksz;
3104789Sahrens 		nhdr->b_spa = spa;
31053290Sjohansen 		nhdr->b_type = type;
3106789Sahrens 		nhdr->b_buf = buf;
31073403Sbmc 		nhdr->b_state = arc_anon;
3108789Sahrens 		nhdr->b_arc_access = 0;
31095450Sbrendan 		nhdr->b_flags = flags & ARC_L2_WRITING;
31105450Sbrendan 		nhdr->b_l2hdr = NULL;
31111544Seschrock 		nhdr->b_datacnt = 1;
31123547Smaybee 		nhdr->b_freeze_cksum = NULL;
31133897Smaybee 		(void) refcount_add(&nhdr->b_refcnt, tag);
3114789Sahrens 		buf->b_hdr = nhdr;
311512296SLin.Ling@Sun.COM 		mutex_exit(&buf->b_evict_lock);
31163403Sbmc 		atomic_add_64(&arc_anon->arcs_size, blksz);
3117789Sahrens 	} else {
311812296SLin.Ling@Sun.COM 		mutex_exit(&buf->b_evict_lock);
31191544Seschrock 		ASSERT(refcount_count(&hdr->b_refcnt) == 1);
3120789Sahrens 		ASSERT(!list_link_active(&hdr->b_arc_node));
3121789Sahrens 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
312212296SLin.Ling@Sun.COM 		if (hdr->b_state != arc_anon)
312312296SLin.Ling@Sun.COM 			arc_change_state(arc_anon, hdr, hash_lock);
3124789Sahrens 		hdr->b_arc_access = 0;
312512296SLin.Ling@Sun.COM 		if (hash_lock)
312612296SLin.Ling@Sun.COM 			mutex_exit(hash_lock);
312712296SLin.Ling@Sun.COM 
312812296SLin.Ling@Sun.COM 		buf_discard_identity(hdr);
31293547Smaybee 		arc_buf_thaw(buf);
3130789Sahrens 	}
31311544Seschrock 	buf->b_efunc = NULL;
31321544Seschrock 	buf->b_private = NULL;
31335450Sbrendan 
31345450Sbrendan 	if (l2hdr) {
31355450Sbrendan 		list_remove(l2hdr->b_dev->l2ad_buflist, hdr);
31365450Sbrendan 		kmem_free(l2hdr, sizeof (l2arc_buf_hdr_t));
31375450Sbrendan 		ARCSTAT_INCR(arcstat_l2_size, -buf_size);
31387545SMark.Maybee@Sun.COM 		mutex_exit(&l2arc_buflist_mtx);
31395450Sbrendan 	}
3140789Sahrens }
3141789Sahrens 
314212296SLin.Ling@Sun.COM /*
314312296SLin.Ling@Sun.COM  * Release this buffer.  If it does not match the provided BP, fill it
314412296SLin.Ling@Sun.COM  * with that block's contents.
314512296SLin.Ling@Sun.COM  */
314612296SLin.Ling@Sun.COM /* ARGSUSED */
314712296SLin.Ling@Sun.COM int
arc_release_bp(arc_buf_t * buf,void * tag,blkptr_t * bp,spa_t * spa,zbookmark_t * zb)314812296SLin.Ling@Sun.COM arc_release_bp(arc_buf_t *buf, void *tag, blkptr_t *bp, spa_t *spa,
314912296SLin.Ling@Sun.COM     zbookmark_t *zb)
315012296SLin.Ling@Sun.COM {
315112296SLin.Ling@Sun.COM 	arc_release(buf, tag);
315212296SLin.Ling@Sun.COM 	return (0);
315312296SLin.Ling@Sun.COM }
315412296SLin.Ling@Sun.COM 
3155789Sahrens int
arc_released(arc_buf_t * buf)3156789Sahrens arc_released(arc_buf_t *buf)
3157789Sahrens {
31587545SMark.Maybee@Sun.COM 	int released;
31597545SMark.Maybee@Sun.COM 
316012296SLin.Ling@Sun.COM 	mutex_enter(&buf->b_evict_lock);
31617545SMark.Maybee@Sun.COM 	released = (buf->b_data != NULL && buf->b_hdr->b_state == arc_anon);
316212296SLin.Ling@Sun.COM 	mutex_exit(&buf->b_evict_lock);
31637545SMark.Maybee@Sun.COM 	return (released);
31641544Seschrock }
31651544Seschrock 
31661544Seschrock int
arc_has_callback(arc_buf_t * buf)31671544Seschrock arc_has_callback(arc_buf_t *buf)
31681544Seschrock {
31697545SMark.Maybee@Sun.COM 	int callback;
31707545SMark.Maybee@Sun.COM 
317112296SLin.Ling@Sun.COM 	mutex_enter(&buf->b_evict_lock);
31727545SMark.Maybee@Sun.COM 	callback = (buf->b_efunc != NULL);
317312296SLin.Ling@Sun.COM 	mutex_exit(&buf->b_evict_lock);
31747545SMark.Maybee@Sun.COM 	return (callback);
3175789Sahrens }
3176789Sahrens 
31771544Seschrock #ifdef ZFS_DEBUG
31781544Seschrock int
arc_referenced(arc_buf_t * buf)31791544Seschrock arc_referenced(arc_buf_t *buf)
31801544Seschrock {
31817545SMark.Maybee@Sun.COM 	int referenced;
31827545SMark.Maybee@Sun.COM 
318312296SLin.Ling@Sun.COM 	mutex_enter(&buf->b_evict_lock);
31847545SMark.Maybee@Sun.COM 	referenced = (refcount_count(&buf->b_hdr->b_refcnt));
318512296SLin.Ling@Sun.COM 	mutex_exit(&buf->b_evict_lock);
31867545SMark.Maybee@Sun.COM 	return (referenced);
31871544Seschrock }
31881544Seschrock #endif
31891544Seschrock 
3190789Sahrens static void
arc_write_ready(zio_t * zio)31913547Smaybee arc_write_ready(zio_t *zio)
31923547Smaybee {
31933547Smaybee 	arc_write_callback_t *callback = zio->io_private;
31943547Smaybee 	arc_buf_t *buf = callback->awcb_buf;
31955329Sgw25295 	arc_buf_hdr_t *hdr = buf->b_hdr;
31965329Sgw25295 
31977754SJeff.Bonwick@Sun.COM 	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt));
31987754SJeff.Bonwick@Sun.COM 	callback->awcb_ready(zio, buf, callback->awcb_private);
31997754SJeff.Bonwick@Sun.COM 
32005329Sgw25295 	/*
32015329Sgw25295 	 * If the IO is already in progress, then this is a re-write
32027754SJeff.Bonwick@Sun.COM 	 * attempt, so we need to thaw and re-compute the cksum.
32037754SJeff.Bonwick@Sun.COM 	 * It is the responsibility of the callback to handle the
32047754SJeff.Bonwick@Sun.COM 	 * accounting for any re-write attempt.
32055329Sgw25295 	 */
32065329Sgw25295 	if (HDR_IO_IN_PROGRESS(hdr)) {
32075329Sgw25295 		mutex_enter(&hdr->b_freeze_lock);
32085329Sgw25295 		if (hdr->b_freeze_cksum != NULL) {
32095329Sgw25295 			kmem_free(hdr->b_freeze_cksum, sizeof (zio_cksum_t));
32105329Sgw25295 			hdr->b_freeze_cksum = NULL;
32115329Sgw25295 		}
32125329Sgw25295 		mutex_exit(&hdr->b_freeze_lock);
32135329Sgw25295 	}
32145450Sbrendan 	arc_cksum_compute(buf, B_FALSE);
32155329Sgw25295 	hdr->b_flags |= ARC_IO_IN_PROGRESS;
32163547Smaybee }
32173547Smaybee 
32183547Smaybee static void
arc_write_done(zio_t * zio)3219789Sahrens arc_write_done(zio_t *zio)
3220789Sahrens {
32213547Smaybee 	arc_write_callback_t *callback = zio->io_private;
32223547Smaybee 	arc_buf_t *buf = callback->awcb_buf;
32233547Smaybee 	arc_buf_hdr_t *hdr = buf->b_hdr;
3224789Sahrens 
322510922SJeff.Bonwick@Sun.COM 	ASSERT(hdr->b_acb == NULL);
322610922SJeff.Bonwick@Sun.COM 
322710922SJeff.Bonwick@Sun.COM 	if (zio->io_error == 0) {
322810922SJeff.Bonwick@Sun.COM 		hdr->b_dva = *BP_IDENTITY(zio->io_bp);
322910922SJeff.Bonwick@Sun.COM 		hdr->b_birth = BP_PHYSICAL_BIRTH(zio->io_bp);
323010922SJeff.Bonwick@Sun.COM 		hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
323110922SJeff.Bonwick@Sun.COM 	} else {
323210922SJeff.Bonwick@Sun.COM 		ASSERT(BUF_EMPTY(hdr));
323310922SJeff.Bonwick@Sun.COM 	}
323410922SJeff.Bonwick@Sun.COM 
32351544Seschrock 	/*
32361544Seschrock 	 * If the block to be written was all-zero, we may have
32371544Seschrock 	 * compressed it away.  In this case no write was performed
323812296SLin.Ling@Sun.COM 	 * so there will be no dva/birth/checksum.  The buffer must
323912296SLin.Ling@Sun.COM 	 * therefore remain anonymous (and uncached).
32401544Seschrock 	 */
3241789Sahrens 	if (!BUF_EMPTY(hdr)) {
3242789Sahrens 		arc_buf_hdr_t *exists;
3243789Sahrens 		kmutex_t *hash_lock;
3244789Sahrens 
324510922SJeff.Bonwick@Sun.COM 		ASSERT(zio->io_error == 0);
324610922SJeff.Bonwick@Sun.COM 
32473093Sahrens 		arc_cksum_verify(buf);
32483093Sahrens 
3249789Sahrens 		exists = buf_hash_insert(hdr, &hash_lock);
3250789Sahrens 		if (exists) {
3251789Sahrens 			/*
3252789Sahrens 			 * This can only happen if we overwrite for
3253789Sahrens 			 * sync-to-convergence, because we remove
3254789Sahrens 			 * buffers from the hash table when we arc_free().
3255789Sahrens 			 */
325610922SJeff.Bonwick@Sun.COM 			if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
325710922SJeff.Bonwick@Sun.COM 				if (!BP_EQUAL(&zio->io_bp_orig, zio->io_bp))
325810922SJeff.Bonwick@Sun.COM 					panic("bad overwrite, hdr=%p exists=%p",
325910922SJeff.Bonwick@Sun.COM 					    (void *)hdr, (void *)exists);
326010922SJeff.Bonwick@Sun.COM 				ASSERT(refcount_is_zero(&exists->b_refcnt));
326110922SJeff.Bonwick@Sun.COM 				arc_change_state(arc_anon, exists, hash_lock);
326210922SJeff.Bonwick@Sun.COM 				mutex_exit(hash_lock);
326310922SJeff.Bonwick@Sun.COM 				arc_hdr_destroy(exists);
326410922SJeff.Bonwick@Sun.COM 				exists = buf_hash_insert(hdr, &hash_lock);
326510922SJeff.Bonwick@Sun.COM 				ASSERT3P(exists, ==, NULL);
326610922SJeff.Bonwick@Sun.COM 			} else {
326710922SJeff.Bonwick@Sun.COM 				/* Dedup */
326810922SJeff.Bonwick@Sun.COM 				ASSERT(hdr->b_datacnt == 1);
326910922SJeff.Bonwick@Sun.COM 				ASSERT(hdr->b_state == arc_anon);
327010922SJeff.Bonwick@Sun.COM 				ASSERT(BP_GET_DEDUP(zio->io_bp));
327110922SJeff.Bonwick@Sun.COM 				ASSERT(BP_GET_LEVEL(zio->io_bp) == 0);
327210272SMatthew.Ahrens@Sun.COM 			}
3273789Sahrens 		}
32741544Seschrock 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
32757046Sahrens 		/* if it's not anon, we are doing a scrub */
327610922SJeff.Bonwick@Sun.COM 		if (!exists && hdr->b_state == arc_anon)
32777046Sahrens 			arc_access(hdr, hash_lock);
32782688Smaybee 		mutex_exit(hash_lock);
32791544Seschrock 	} else {
32801544Seschrock 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
3281789Sahrens 	}
328210922SJeff.Bonwick@Sun.COM 
328310922SJeff.Bonwick@Sun.COM 	ASSERT(!refcount_is_zero(&hdr->b_refcnt));
328410922SJeff.Bonwick@Sun.COM 	callback->awcb_done(zio, buf, callback->awcb_private);
3285789Sahrens 
32863547Smaybee 	kmem_free(callback, sizeof (arc_write_callback_t));
3287789Sahrens }
3288789Sahrens 
32893547Smaybee zio_t *
arc_write(zio_t * pio,spa_t * spa,uint64_t txg,blkptr_t * bp,arc_buf_t * buf,boolean_t l2arc,const zio_prop_t * zp,arc_done_func_t * ready,arc_done_func_t * done,void * private,int priority,int zio_flags,const zbookmark_t * zb)329010922SJeff.Bonwick@Sun.COM arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
329110922SJeff.Bonwick@Sun.COM     blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
329210922SJeff.Bonwick@Sun.COM     arc_done_func_t *ready, arc_done_func_t *done, void *private,
329310922SJeff.Bonwick@Sun.COM     int priority, int zio_flags, const zbookmark_t *zb)
3294789Sahrens {
3295789Sahrens 	arc_buf_hdr_t *hdr = buf->b_hdr;
32963547Smaybee 	arc_write_callback_t *callback;
32977754SJeff.Bonwick@Sun.COM 	zio_t *zio;
32987754SJeff.Bonwick@Sun.COM 
32997754SJeff.Bonwick@Sun.COM 	ASSERT(ready != NULL);
330010922SJeff.Bonwick@Sun.COM 	ASSERT(done != NULL);
3301789Sahrens 	ASSERT(!HDR_IO_ERROR(hdr));
33022237Smaybee 	ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
330310922SJeff.Bonwick@Sun.COM 	ASSERT(hdr->b_acb == NULL);
33047237Sek110237 	if (l2arc)
33057237Sek110237 		hdr->b_flags |= ARC_L2CACHE;
33063547Smaybee 	callback = kmem_zalloc(sizeof (arc_write_callback_t), KM_SLEEP);
33073547Smaybee 	callback->awcb_ready = ready;
33083547Smaybee 	callback->awcb_done = done;
33093547Smaybee 	callback->awcb_private = private;
33103547Smaybee 	callback->awcb_buf = buf;
33117046Sahrens 
331210922SJeff.Bonwick@Sun.COM 	zio = zio_write(pio, spa, txg, bp, buf->b_data, hdr->b_size, zp,
33137754SJeff.Bonwick@Sun.COM 	    arc_write_ready, arc_write_done, callback, priority, zio_flags, zb);
3314789Sahrens 
33153547Smaybee 	return (zio);
3316789Sahrens }
3317789Sahrens 
33186245Smaybee static int
arc_memory_throttle(uint64_t reserve,uint64_t inflight_data,uint64_t txg)33199412SAleksandr.Guzovskiy@Sun.COM arc_memory_throttle(uint64_t reserve, uint64_t inflight_data, uint64_t txg)
33206245Smaybee {
33216245Smaybee #ifdef _KERNEL
33226245Smaybee 	uint64_t available_memory = ptob(freemem);
33236245Smaybee 	static uint64_t page_load = 0;
33246245Smaybee 	static uint64_t last_txg = 0;
33256245Smaybee 
33266245Smaybee #if defined(__i386)
33276245Smaybee 	available_memory =
33286245Smaybee 	    MIN(available_memory, vmem_size(heap_arena, VMEM_FREE));
33296245Smaybee #endif
33306245Smaybee 	if (available_memory >= zfs_write_limit_max)
33316245Smaybee 		return (0);
33326245Smaybee 
33336245Smaybee 	if (txg > last_txg) {
33346245Smaybee 		last_txg = txg;
33356245Smaybee 		page_load = 0;
33366245Smaybee 	}
33376245Smaybee 	/*
33386245Smaybee 	 * If we are in pageout, we know that memory is already tight,
33396245Smaybee 	 * the arc is already going to be evicting, so we just want to
33406245Smaybee 	 * continue to let page writes occur as quickly as possible.
33416245Smaybee 	 */
33426245Smaybee 	if (curproc == proc_pageout) {
33436245Smaybee 		if (page_load > MAX(ptob(minfree), available_memory) / 4)
33446245Smaybee 			return (ERESTART);
33456245Smaybee 		/* Note: reserve is inflated, so we deflate */
33466245Smaybee 		page_load += reserve / 8;
33476245Smaybee 		return (0);
33486245Smaybee 	} else if (page_load > 0 && arc_reclaim_needed()) {
33496245Smaybee 		/* memory is low, delay before restarting */
33506245Smaybee 		ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
33516245Smaybee 		return (EAGAIN);
33526245Smaybee 	}
33536245Smaybee 	page_load = 0;
33546245Smaybee 
33556245Smaybee 	if (arc_size > arc_c_min) {
33566245Smaybee 		uint64_t evictable_memory =
33576245Smaybee 		    arc_mru->arcs_lsize[ARC_BUFC_DATA] +
33586245Smaybee 		    arc_mru->arcs_lsize[ARC_BUFC_METADATA] +
33596245Smaybee 		    arc_mfu->arcs_lsize[ARC_BUFC_DATA] +
33606245Smaybee 		    arc_mfu->arcs_lsize[ARC_BUFC_METADATA];
33616245Smaybee 		available_memory += MIN(evictable_memory, arc_size - arc_c_min);
33626245Smaybee 	}
33636245Smaybee 
33646245Smaybee 	if (inflight_data > available_memory / 4) {
33656245Smaybee 		ARCSTAT_INCR(arcstat_memory_throttle_count, 1);
33666245Smaybee 		return (ERESTART);
33676245Smaybee 	}
33686245Smaybee #endif
33696245Smaybee 	return (0);
33706245Smaybee }
33716245Smaybee 
3372789Sahrens void
arc_tempreserve_clear(uint64_t reserve)33736245Smaybee arc_tempreserve_clear(uint64_t reserve)
3374789Sahrens {
33756245Smaybee 	atomic_add_64(&arc_tempreserve, -reserve);
3376789Sahrens 	ASSERT((int64_t)arc_tempreserve >= 0);
3377789Sahrens }
3378789Sahrens 
3379789Sahrens int
arc_tempreserve_space(uint64_t reserve,uint64_t txg)33806245Smaybee arc_tempreserve_space(uint64_t reserve, uint64_t txg)
3381789Sahrens {
33826245Smaybee 	int error;
33839412SAleksandr.Guzovskiy@Sun.COM 	uint64_t anon_size;
33846245Smaybee 
3385789Sahrens #ifdef ZFS_DEBUG
3386789Sahrens 	/*
3387789Sahrens 	 * Once in a while, fail for no reason.  Everything should cope.
3388789Sahrens 	 */
3389789Sahrens 	if (spa_get_random(10000) == 0) {
3390789Sahrens 		dprintf("forcing random failure\n");
3391789Sahrens 		return (ERESTART);
3392789Sahrens 	}
3393789Sahrens #endif
33946245Smaybee 	if (reserve > arc_c/4 && !arc_no_grow)
33956245Smaybee 		arc_c = MIN(arc_c_max, reserve * 4);
33966245Smaybee 	if (reserve > arc_c)
3397982Smaybee 		return (ENOMEM);
3398982Smaybee 
3399789Sahrens 	/*
34009412SAleksandr.Guzovskiy@Sun.COM 	 * Don't count loaned bufs as in flight dirty data to prevent long
34019412SAleksandr.Guzovskiy@Sun.COM 	 * network delays from blocking transactions that are ready to be
34029412SAleksandr.Guzovskiy@Sun.COM 	 * assigned to a txg.
34039412SAleksandr.Guzovskiy@Sun.COM 	 */
34049412SAleksandr.Guzovskiy@Sun.COM 	anon_size = MAX((int64_t)(arc_anon->arcs_size - arc_loaned_bytes), 0);
34059412SAleksandr.Guzovskiy@Sun.COM 
34069412SAleksandr.Guzovskiy@Sun.COM 	/*
34076245Smaybee 	 * Writes will, almost always, require additional memory allocations
34086245Smaybee 	 * in order to compress/encrypt/etc the data.  We therefor need to
34096245Smaybee 	 * make sure that there is sufficient available memory for this.
34106245Smaybee 	 */
34119412SAleksandr.Guzovskiy@Sun.COM 	if (error = arc_memory_throttle(reserve, anon_size, txg))
34126245Smaybee 		return (error);
34136245Smaybee 
34146245Smaybee 	/*
3415982Smaybee 	 * Throttle writes when the amount of dirty data in the cache
3416982Smaybee 	 * gets too large.  We try to keep the cache less than half full
3417982Smaybee 	 * of dirty blocks so that our sync times don't grow too large.
3418982Smaybee 	 * Note: if two requests come in concurrently, we might let them
3419982Smaybee 	 * both succeed, when one of them should fail.  Not a huge deal.
3420789Sahrens 	 */
34219412SAleksandr.Guzovskiy@Sun.COM 
34229412SAleksandr.Guzovskiy@Sun.COM 	if (reserve + arc_tempreserve + anon_size > arc_c / 2 &&
34239412SAleksandr.Guzovskiy@Sun.COM 	    anon_size > arc_c / 4) {
34244309Smaybee 		dprintf("failing, arc_tempreserve=%lluK anon_meta=%lluK "
34254309Smaybee 		    "anon_data=%lluK tempreserve=%lluK arc_c=%lluK\n",
34264309Smaybee 		    arc_tempreserve>>10,
34274309Smaybee 		    arc_anon->arcs_lsize[ARC_BUFC_METADATA]>>10,
34284309Smaybee 		    arc_anon->arcs_lsize[ARC_BUFC_DATA]>>10,
34296245Smaybee 		    reserve>>10, arc_c>>10);
3430789Sahrens 		return (ERESTART);
3431789Sahrens 	}
34326245Smaybee 	atomic_add_64(&arc_tempreserve, reserve);
3433789Sahrens 	return (0);
3434789Sahrens }
3435789Sahrens 
3436789Sahrens void
arc_init(void)3437789Sahrens arc_init(void)
3438789Sahrens {
3439789Sahrens 	mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
3440789Sahrens 	cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
3441789Sahrens 
34422391Smaybee 	/* Convert seconds to clock ticks */
34432638Sperrin 	arc_min_prefetch_lifespan = 1 * hz;
34442391Smaybee 
3445789Sahrens 	/* Start out with 1/8 of all memory */
34463403Sbmc 	arc_c = physmem * PAGESIZE / 8;
3447789Sahrens 
3448789Sahrens #ifdef _KERNEL
3449789Sahrens 	/*
3450789Sahrens 	 * On architectures where the physical memory can be larger
3451789Sahrens 	 * than the addressable space (intel in 32-bit mode), we may
3452789Sahrens 	 * need to limit the cache to 1/8 of VM size.
3453789Sahrens 	 */
34543403Sbmc 	arc_c = MIN(arc_c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
3455789Sahrens #endif
3456789Sahrens 
3457982Smaybee 	/* set min cache to 1/32 of all memory, or 64MB, whichever is more */
34583403Sbmc 	arc_c_min = MAX(arc_c / 4, 64<<20);
3459982Smaybee 	/* set max to 3/4 of all memory, or all but 1GB, whichever is more */
34603403Sbmc 	if (arc_c * 8 >= 1<<30)
34613403Sbmc 		arc_c_max = (arc_c * 8) - (1<<30);
3462789Sahrens 	else
34633403Sbmc 		arc_c_max = arc_c_min;
34643403Sbmc 	arc_c_max = MAX(arc_c * 6, arc_c_max);
34652885Sahrens 
34662885Sahrens 	/*
34672885Sahrens 	 * Allow the tunables to override our calculations if they are
34682885Sahrens 	 * reasonable (ie. over 64MB)
34692885Sahrens 	 */
34702885Sahrens 	if (zfs_arc_max > 64<<20 && zfs_arc_max < physmem * PAGESIZE)
34713403Sbmc 		arc_c_max = zfs_arc_max;
34723403Sbmc 	if (zfs_arc_min > 64<<20 && zfs_arc_min <= arc_c_max)
34733403Sbmc 		arc_c_min = zfs_arc_min;
34742885Sahrens 
34753403Sbmc 	arc_c = arc_c_max;
34763403Sbmc 	arc_p = (arc_c >> 1);
3477789Sahrens 
34784309Smaybee 	/* limit meta-data to 1/4 of the arc capacity */
34794309Smaybee 	arc_meta_limit = arc_c_max / 4;
34804645Sek110237 
34814645Sek110237 	/* Allow the tunable to override if it is reasonable */
34824645Sek110237 	if (zfs_arc_meta_limit > 0 && zfs_arc_meta_limit <= arc_c_max)
34834645Sek110237 		arc_meta_limit = zfs_arc_meta_limit;
34844645Sek110237 
34854309Smaybee 	if (arc_c_min < arc_meta_limit / 2 && zfs_arc_min == 0)
34864309Smaybee 		arc_c_min = arc_meta_limit / 2;
34874309Smaybee 
34888582SBrendan.Gregg@Sun.COM 	if (zfs_arc_grow_retry > 0)
34898582SBrendan.Gregg@Sun.COM 		arc_grow_retry = zfs_arc_grow_retry;
34908582SBrendan.Gregg@Sun.COM 
34918582SBrendan.Gregg@Sun.COM 	if (zfs_arc_shrink_shift > 0)
34928582SBrendan.Gregg@Sun.COM 		arc_shrink_shift = zfs_arc_shrink_shift;
34938582SBrendan.Gregg@Sun.COM 
34948582SBrendan.Gregg@Sun.COM 	if (zfs_arc_p_min_shift > 0)
34958582SBrendan.Gregg@Sun.COM 		arc_p_min_shift = zfs_arc_p_min_shift;
34968582SBrendan.Gregg@Sun.COM 
3497789Sahrens 	/* if kmem_flags are set, lets try to use less memory */
3498789Sahrens 	if (kmem_debugging())
34993403Sbmc 		arc_c = arc_c / 2;
35003403Sbmc 	if (arc_c < arc_c_min)
35013403Sbmc 		arc_c = arc_c_min;
3502789Sahrens 
35033403Sbmc 	arc_anon = &ARC_anon;
35043403Sbmc 	arc_mru = &ARC_mru;
35053403Sbmc 	arc_mru_ghost = &ARC_mru_ghost;
35063403Sbmc 	arc_mfu = &ARC_mfu;
35073403Sbmc 	arc_mfu_ghost = &ARC_mfu_ghost;
35085450Sbrendan 	arc_l2c_only = &ARC_l2c_only;
35093403Sbmc 	arc_size = 0;
3510789Sahrens 
35113403Sbmc 	mutex_init(&arc_anon->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
35123403Sbmc 	mutex_init(&arc_mru->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
35133403Sbmc 	mutex_init(&arc_mru_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
35143403Sbmc 	mutex_init(&arc_mfu->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
35153403Sbmc 	mutex_init(&arc_mfu_ghost->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
35165450Sbrendan 	mutex_init(&arc_l2c_only->arcs_mtx, NULL, MUTEX_DEFAULT, NULL);
35172688Smaybee 
35184309Smaybee 	list_create(&arc_mru->arcs_list[ARC_BUFC_METADATA],
35194309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35204309Smaybee 	list_create(&arc_mru->arcs_list[ARC_BUFC_DATA],
35214309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35224309Smaybee 	list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA],
35234309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35244309Smaybee 	list_create(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA],
35254309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35264309Smaybee 	list_create(&arc_mfu->arcs_list[ARC_BUFC_METADATA],
35274309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35284309Smaybee 	list_create(&arc_mfu->arcs_list[ARC_BUFC_DATA],
35294309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35304309Smaybee 	list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA],
35314309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35324309Smaybee 	list_create(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA],
35334309Smaybee 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35345450Sbrendan 	list_create(&arc_l2c_only->arcs_list[ARC_BUFC_METADATA],
35355450Sbrendan 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
35365450Sbrendan 	list_create(&arc_l2c_only->arcs_list[ARC_BUFC_DATA],
35375450Sbrendan 	    sizeof (arc_buf_hdr_t), offsetof(arc_buf_hdr_t, b_arc_node));
3538789Sahrens 
3539789Sahrens 	buf_init();
3540789Sahrens 
3541789Sahrens 	arc_thread_exit = 0;
35421544Seschrock 	arc_eviction_list = NULL;
35431544Seschrock 	mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
35442887Smaybee 	bzero(&arc_eviction_hdr, sizeof (arc_buf_hdr_t));
3545789Sahrens 
35463403Sbmc 	arc_ksp = kstat_create("zfs", 0, "arcstats", "misc", KSTAT_TYPE_NAMED,
35473403Sbmc 	    sizeof (arc_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
35483403Sbmc 
35493403Sbmc 	if (arc_ksp != NULL) {
35503403Sbmc 		arc_ksp->ks_data = &arc_stats;
35513403Sbmc 		kstat_install(arc_ksp);
35523403Sbmc 	}
35533403Sbmc 
3554789Sahrens 	(void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
3555789Sahrens 	    TS_RUN, minclsyspri);
35563158Smaybee 
35573158Smaybee 	arc_dead = FALSE;
35586987Sbrendan 	arc_warm = B_FALSE;
35596245Smaybee 
35606245Smaybee 	if (zfs_write_limit_max == 0)
35617468SMark.Maybee@Sun.COM 		zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
35626245Smaybee 	else
35636245Smaybee 		zfs_write_limit_shift = 0;
35647468SMark.Maybee@Sun.COM 	mutex_init(&zfs_write_limit_lock, NULL, MUTEX_DEFAULT, NULL);
3565789Sahrens }
3566789Sahrens 
3567789Sahrens void
arc_fini(void)3568789Sahrens arc_fini(void)
3569789Sahrens {
3570789Sahrens 	mutex_enter(&arc_reclaim_thr_lock);
3571789Sahrens 	arc_thread_exit = 1;
3572789Sahrens 	while (arc_thread_exit != 0)
3573789Sahrens 		cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
3574789Sahrens 	mutex_exit(&arc_reclaim_thr_lock);
3575789Sahrens 
35765642Smaybee 	arc_flush(NULL);
3577789Sahrens 
3578789Sahrens 	arc_dead = TRUE;
3579789Sahrens 
35803403Sbmc 	if (arc_ksp != NULL) {
35813403Sbmc 		kstat_delete(arc_ksp);
35823403Sbmc 		arc_ksp = NULL;
35833403Sbmc 	}
35843403Sbmc 
35851544Seschrock 	mutex_destroy(&arc_eviction_mtx);
3586789Sahrens 	mutex_destroy(&arc_reclaim_thr_lock);
3587789Sahrens 	cv_destroy(&arc_reclaim_thr_cv);
3588789Sahrens 
35894309Smaybee 	list_destroy(&arc_mru->arcs_list[ARC_BUFC_METADATA]);
35904309Smaybee 	list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_METADATA]);
35914309Smaybee 	list_destroy(&arc_mfu->arcs_list[ARC_BUFC_METADATA]);
35924309Smaybee 	list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_METADATA]);
35934309Smaybee 	list_destroy(&arc_mru->arcs_list[ARC_BUFC_DATA]);
35944309Smaybee 	list_destroy(&arc_mru_ghost->arcs_list[ARC_BUFC_DATA]);
35954309Smaybee 	list_destroy(&arc_mfu->arcs_list[ARC_BUFC_DATA]);
35964309Smaybee 	list_destroy(&arc_mfu_ghost->arcs_list[ARC_BUFC_DATA]);
3597789Sahrens 
35983403Sbmc 	mutex_destroy(&arc_anon->arcs_mtx);
35993403Sbmc 	mutex_destroy(&arc_mru->arcs_mtx);
36003403Sbmc 	mutex_destroy(&arc_mru_ghost->arcs_mtx);
36013403Sbmc 	mutex_destroy(&arc_mfu->arcs_mtx);
36023403Sbmc 	mutex_destroy(&arc_mfu_ghost->arcs_mtx);
36038214SRicardo.M.Correia@Sun.COM 	mutex_destroy(&arc_l2c_only->arcs_mtx);
36042856Snd150628 
36057468SMark.Maybee@Sun.COM 	mutex_destroy(&zfs_write_limit_lock);
36067468SMark.Maybee@Sun.COM 
3607789Sahrens 	buf_fini();
36089412SAleksandr.Guzovskiy@Sun.COM 
36099412SAleksandr.Guzovskiy@Sun.COM 	ASSERT(arc_loaned_bytes == 0);
3610789Sahrens }
36115450Sbrendan 
36125450Sbrendan /*
36135450Sbrendan  * Level 2 ARC
36145450Sbrendan  *
36155450Sbrendan  * The level 2 ARC (L2ARC) is a cache layer in-between main memory and disk.
36165450Sbrendan  * It uses dedicated storage devices to hold cached data, which are populated
36175450Sbrendan  * using large infrequent writes.  The main role of this cache is to boost
36185450Sbrendan  * the performance of random read workloads.  The intended L2ARC devices
36195450Sbrendan  * include short-stroked disks, solid state disks, and other media with
36205450Sbrendan  * substantially faster read latency than disk.
36215450Sbrendan  *
36225450Sbrendan  *                 +-----------------------+
36235450Sbrendan  *                 |         ARC           |
36245450Sbrendan  *                 +-----------------------+
36255450Sbrendan  *                    |         ^     ^
36265450Sbrendan  *                    |         |     |
36275450Sbrendan  *      l2arc_feed_thread()    arc_read()
36285450Sbrendan  *                    |         |     |
36295450Sbrendan  *                    |  l2arc read   |
36305450Sbrendan  *                    V         |     |
36315450Sbrendan  *               +---------------+    |
36325450Sbrendan  *               |     L2ARC     |    |
36335450Sbrendan  *               +---------------+    |
36345450Sbrendan  *                   |    ^           |
36355450Sbrendan  *          l2arc_write() |           |
36365450Sbrendan  *                   |    |           |
36375450Sbrendan  *                   V    |           |
36385450Sbrendan  *                 +-------+      +-------+
36395450Sbrendan  *                 | vdev  |      | vdev  |
36405450Sbrendan  *                 | cache |      | cache |
36415450Sbrendan  *                 +-------+      +-------+
36425450Sbrendan  *                 +=========+     .-----.
36435450Sbrendan  *                 :  L2ARC  :    |-_____-|
36445450Sbrendan  *                 : devices :    | Disks |
36455450Sbrendan  *                 +=========+    `-_____-'
36465450Sbrendan  *
36475450Sbrendan  * Read requests are satisfied from the following sources, in order:
36485450Sbrendan  *
36495450Sbrendan  *	1) ARC
36505450Sbrendan  *	2) vdev cache of L2ARC devices
36515450Sbrendan  *	3) L2ARC devices
36525450Sbrendan  *	4) vdev cache of disks
36535450Sbrendan  *	5) disks
36545450Sbrendan  *
36555450Sbrendan  * Some L2ARC device types exhibit extremely slow write performance.
36565450Sbrendan  * To accommodate for this there are some significant differences between
36575450Sbrendan  * the L2ARC and traditional cache design:
36585450Sbrendan  *
36595450Sbrendan  * 1. There is no eviction path from the ARC to the L2ARC.  Evictions from
36605450Sbrendan  * the ARC behave as usual, freeing buffers and placing headers on ghost
36615450Sbrendan  * lists.  The ARC does not send buffers to the L2ARC during eviction as
36625450Sbrendan  * this would add inflated write latencies for all ARC memory pressure.
36635450Sbrendan  *
36645450Sbrendan  * 2. The L2ARC attempts to cache data from the ARC before it is evicted.
36655450Sbrendan  * It does this by periodically scanning buffers from the eviction-end of
36665450Sbrendan  * the MFU and MRU ARC lists, copying them to the L2ARC devices if they are
36675450Sbrendan  * not already there.  It scans until a headroom of buffers is satisfied,
36685450Sbrendan  * which itself is a buffer for ARC eviction.  The thread that does this is
36695450Sbrendan  * l2arc_feed_thread(), illustrated below; example sizes are included to
36705450Sbrendan  * provide a better sense of ratio than this diagram:
36715450Sbrendan  *
36725450Sbrendan  *	       head -->                        tail
36735450Sbrendan  *	        +---------------------+----------+
36745450Sbrendan  *	ARC_mfu |:::::#:::::::::::::::|o#o###o###|-->.   # already on L2ARC
36755450Sbrendan  *	        +---------------------+----------+   |   o L2ARC eligible
36765450Sbrendan  *	ARC_mru |:#:::::::::::::::::::|#o#ooo####|-->|   : ARC buffer
36775450Sbrendan  *	        +---------------------+----------+   |
36785450Sbrendan  *	             15.9 Gbytes      ^ 32 Mbytes    |
36795450Sbrendan  *	                           headroom          |
36805450Sbrendan  *	                                      l2arc_feed_thread()
36815450Sbrendan  *	                                             |
36825450Sbrendan  *	                 l2arc write hand <--[oooo]--'
36835450Sbrendan  *	                         |           8 Mbyte
36845450Sbrendan  *	                         |          write max
36855450Sbrendan  *	                         V
36865450Sbrendan  *		  +==============================+
36875450Sbrendan  *	L2ARC dev |####|#|###|###|    |####| ... |
36885450Sbrendan  *	          +==============================+
36895450Sbrendan  *	                     32 Gbytes
36905450Sbrendan  *
36915450Sbrendan  * 3. If an ARC buffer is copied to the L2ARC but then hit instead of
36925450Sbrendan  * evicted, then the L2ARC has cached a buffer much sooner than it probably
36935450Sbrendan  * needed to, potentially wasting L2ARC device bandwidth and storage.  It is
36945450Sbrendan  * safe to say that this is an uncommon case, since buffers at the end of
36955450Sbrendan  * the ARC lists have moved there due to inactivity.
36965450Sbrendan  *
36975450Sbrendan  * 4. If the ARC evicts faster than the L2ARC can maintain a headroom,
36985450Sbrendan  * then the L2ARC simply misses copying some buffers.  This serves as a
36995450Sbrendan  * pressure valve to prevent heavy read workloads from both stalling the ARC
37005450Sbrendan  * with waits and clogging the L2ARC with writes.  This also helps prevent
37015450Sbrendan  * the potential for the L2ARC to churn if it attempts to cache content too
37025450Sbrendan  * quickly, such as during backups of the entire pool.
37035450Sbrendan  *
37046987Sbrendan  * 5. After system boot and before the ARC has filled main memory, there are
37056987Sbrendan  * no evictions from the ARC and so the tails of the ARC_mfu and ARC_mru
37066987Sbrendan  * lists can remain mostly static.  Instead of searching from tail of these
37076987Sbrendan  * lists as pictured, the l2arc_feed_thread() will search from the list heads
37086987Sbrendan  * for eligible buffers, greatly increasing its chance of finding them.
37096987Sbrendan  *
37106987Sbrendan  * The L2ARC device write speed is also boosted during this time so that
37116987Sbrendan  * the L2ARC warms up faster.  Since there have been no ARC evictions yet,
37126987Sbrendan  * there are no L2ARC reads, and no fear of degrading read performance
37136987Sbrendan  * through increased writes.
37146987Sbrendan  *
37156987Sbrendan  * 6. Writes to the L2ARC devices are grouped and sent in-sequence, so that
37165450Sbrendan  * the vdev queue can aggregate them into larger and fewer writes.  Each
37175450Sbrendan  * device is written to in a rotor fashion, sweeping writes through
37185450Sbrendan  * available space then repeating.
37195450Sbrendan  *
37206987Sbrendan  * 7. The L2ARC does not store dirty content.  It never needs to flush
37215450Sbrendan  * write buffers back to disk based storage.
37225450Sbrendan  *
37236987Sbrendan  * 8. If an ARC buffer is written (and dirtied) which also exists in the
37245450Sbrendan  * L2ARC, the now stale L2ARC buffer is immediately dropped.
37255450Sbrendan  *
37265450Sbrendan  * The performance of the L2ARC can be tweaked by a number of tunables, which
37275450Sbrendan  * may be necessary for different workloads:
37285450Sbrendan  *
37295450Sbrendan  *	l2arc_write_max		max write bytes per interval
37306987Sbrendan  *	l2arc_write_boost	extra write bytes during device warmup
37315450Sbrendan  *	l2arc_noprefetch	skip caching prefetched buffers
37325450Sbrendan  *	l2arc_headroom		number of max device writes to precache
37335450Sbrendan  *	l2arc_feed_secs		seconds between L2ARC writing
37345450Sbrendan  *
37355450Sbrendan  * Tunables may be removed or added as future performance improvements are
37365450Sbrendan  * integrated, and also may become zpool properties.
37378582SBrendan.Gregg@Sun.COM  *
37388582SBrendan.Gregg@Sun.COM  * There are three key functions that control how the L2ARC warms up:
37398582SBrendan.Gregg@Sun.COM  *
37408582SBrendan.Gregg@Sun.COM  *	l2arc_write_eligible()	check if a buffer is eligible to cache
37418582SBrendan.Gregg@Sun.COM  *	l2arc_write_size()	calculate how much to write
37428582SBrendan.Gregg@Sun.COM  *	l2arc_write_interval()	calculate sleep delay between writes
37438582SBrendan.Gregg@Sun.COM  *
37448582SBrendan.Gregg@Sun.COM  * These three functions determine what to write, how much, and how quickly
37458582SBrendan.Gregg@Sun.COM  * to send writes.
37465450Sbrendan  */
37475450Sbrendan 
37488582SBrendan.Gregg@Sun.COM static boolean_t
l2arc_write_eligible(uint64_t spa_guid,arc_buf_hdr_t * ab)37498636SMark.Maybee@Sun.COM l2arc_write_eligible(uint64_t spa_guid, arc_buf_hdr_t *ab)
37508582SBrendan.Gregg@Sun.COM {
37518582SBrendan.Gregg@Sun.COM 	/*
37528582SBrendan.Gregg@Sun.COM 	 * A buffer is *not* eligible for the L2ARC if it:
37538582SBrendan.Gregg@Sun.COM 	 * 1. belongs to a different spa.
375410357SBrendan.Gregg@Sun.COM 	 * 2. is already cached on the L2ARC.
375510357SBrendan.Gregg@Sun.COM 	 * 3. has an I/O in progress (it may be an incomplete read).
375610357SBrendan.Gregg@Sun.COM 	 * 4. is flagged not eligible (zfs property).
37578582SBrendan.Gregg@Sun.COM 	 */
375810357SBrendan.Gregg@Sun.COM 	if (ab->b_spa != spa_guid || ab->b_l2hdr != NULL ||
37598582SBrendan.Gregg@Sun.COM 	    HDR_IO_IN_PROGRESS(ab) || !HDR_L2CACHE(ab))
37608582SBrendan.Gregg@Sun.COM 		return (B_FALSE);
37618582SBrendan.Gregg@Sun.COM 
37628582SBrendan.Gregg@Sun.COM 	return (B_TRUE);
37638582SBrendan.Gregg@Sun.COM }
37648582SBrendan.Gregg@Sun.COM 
37658582SBrendan.Gregg@Sun.COM static uint64_t
l2arc_write_size(l2arc_dev_t * dev)37668582SBrendan.Gregg@Sun.COM l2arc_write_size(l2arc_dev_t *dev)
37678582SBrendan.Gregg@Sun.COM {
37688582SBrendan.Gregg@Sun.COM 	uint64_t size;
37698582SBrendan.Gregg@Sun.COM 
37708582SBrendan.Gregg@Sun.COM 	size = dev->l2ad_write;
37718582SBrendan.Gregg@Sun.COM 
37728582SBrendan.Gregg@Sun.COM 	if (arc_warm == B_FALSE)
37738582SBrendan.Gregg@Sun.COM 		size += dev->l2ad_boost;
37748582SBrendan.Gregg@Sun.COM 
37758582SBrendan.Gregg@Sun.COM 	return (size);
37768582SBrendan.Gregg@Sun.COM 
37778582SBrendan.Gregg@Sun.COM }
37788582SBrendan.Gregg@Sun.COM 
37798582SBrendan.Gregg@Sun.COM static clock_t
l2arc_write_interval(clock_t began,uint64_t wanted,uint64_t wrote)37808582SBrendan.Gregg@Sun.COM l2arc_write_interval(clock_t began, uint64_t wanted, uint64_t wrote)
37818582SBrendan.Gregg@Sun.COM {
378211066Srafael.vanoni@sun.com 	clock_t interval, next, now;
37838582SBrendan.Gregg@Sun.COM 
37848582SBrendan.Gregg@Sun.COM 	/*
37858582SBrendan.Gregg@Sun.COM 	 * If the ARC lists are busy, increase our write rate; if the
37868582SBrendan.Gregg@Sun.COM 	 * lists are stale, idle back.  This is achieved by checking
37878582SBrendan.Gregg@Sun.COM 	 * how much we previously wrote - if it was more than half of
37888582SBrendan.Gregg@Sun.COM 	 * what we wanted, schedule the next write much sooner.
37898582SBrendan.Gregg@Sun.COM 	 */
37908582SBrendan.Gregg@Sun.COM 	if (l2arc_feed_again && wrote > (wanted / 2))
37918582SBrendan.Gregg@Sun.COM 		interval = (hz * l2arc_feed_min_ms) / 1000;
37928582SBrendan.Gregg@Sun.COM 	else
37938582SBrendan.Gregg@Sun.COM 		interval = hz * l2arc_feed_secs;
37948582SBrendan.Gregg@Sun.COM 
379511066Srafael.vanoni@sun.com 	now = ddi_get_lbolt();
379611066Srafael.vanoni@sun.com 	next = MAX(now, MIN(now + interval, began + interval));
37978582SBrendan.Gregg@Sun.COM 
37988582SBrendan.Gregg@Sun.COM 	return (next);
37998582SBrendan.Gregg@Sun.COM }
38008582SBrendan.Gregg@Sun.COM 
38015450Sbrendan static void
l2arc_hdr_stat_add(void)38025450Sbrendan l2arc_hdr_stat_add(void)
38035450Sbrendan {
38046018Sbrendan 	ARCSTAT_INCR(arcstat_l2_hdr_size, HDR_SIZE + L2HDR_SIZE);
38056018Sbrendan 	ARCSTAT_INCR(arcstat_hdr_size, -HDR_SIZE);
38065450Sbrendan }
38075450Sbrendan 
38085450Sbrendan static void
l2arc_hdr_stat_remove(void)38095450Sbrendan l2arc_hdr_stat_remove(void)
38105450Sbrendan {
38116018Sbrendan 	ARCSTAT_INCR(arcstat_l2_hdr_size, -(HDR_SIZE + L2HDR_SIZE));
38126018Sbrendan 	ARCSTAT_INCR(arcstat_hdr_size, HDR_SIZE);
38135450Sbrendan }
38145450Sbrendan 
38155450Sbrendan /*
38165450Sbrendan  * Cycle through L2ARC devices.  This is how L2ARC load balances.
38176987Sbrendan  * If a device is returned, this also returns holding the spa config lock.
38185450Sbrendan  */
38195450Sbrendan static l2arc_dev_t *
l2arc_dev_get_next(void)38205450Sbrendan l2arc_dev_get_next(void)
38215450Sbrendan {
38226987Sbrendan 	l2arc_dev_t *first, *next = NULL;
38236987Sbrendan 
38246987Sbrendan 	/*
38256987Sbrendan 	 * Lock out the removal of spas (spa_namespace_lock), then removal
38266987Sbrendan 	 * of cache devices (l2arc_dev_mtx).  Once a device has been selected,
38276987Sbrendan 	 * both locks will be dropped and a spa config lock held instead.
38286987Sbrendan 	 */
38296987Sbrendan 	mutex_enter(&spa_namespace_lock);
38306987Sbrendan 	mutex_enter(&l2arc_dev_mtx);
38316643Seschrock 
38326643Seschrock 	/* if there are no vdevs, there is nothing to do */
38336643Seschrock 	if (l2arc_ndev == 0)
38346987Sbrendan 		goto out;
38356643Seschrock 
38366643Seschrock 	first = NULL;
38376643Seschrock 	next = l2arc_dev_last;
38386643Seschrock 	do {
38396643Seschrock 		/* loop around the list looking for a non-faulted vdev */
38406643Seschrock 		if (next == NULL) {
38415450Sbrendan 			next = list_head(l2arc_dev_list);
38426643Seschrock 		} else {
38436643Seschrock 			next = list_next(l2arc_dev_list, next);
38446643Seschrock 			if (next == NULL)
38456643Seschrock 				next = list_head(l2arc_dev_list);
38466643Seschrock 		}
38476643Seschrock 
38486643Seschrock 		/* if we have come back to the start, bail out */
38496643Seschrock 		if (first == NULL)
38506643Seschrock 			first = next;
38516643Seschrock 		else if (next == first)
38526643Seschrock 			break;
38536643Seschrock 
38546643Seschrock 	} while (vdev_is_dead(next->l2ad_vdev));
38556643Seschrock 
38566643Seschrock 	/* if we were unable to find any usable vdevs, return NULL */
38576643Seschrock 	if (vdev_is_dead(next->l2ad_vdev))
38586987Sbrendan 		next = NULL;
38595450Sbrendan 
38605450Sbrendan 	l2arc_dev_last = next;
38615450Sbrendan 
38626987Sbrendan out:
38636987Sbrendan 	mutex_exit(&l2arc_dev_mtx);
38646987Sbrendan 
38656987Sbrendan 	/*
38666987Sbrendan 	 * Grab the config lock to prevent the 'next' device from being
38676987Sbrendan 	 * removed while we are writing to it.
38686987Sbrendan 	 */
38696987Sbrendan 	if (next != NULL)
38707754SJeff.Bonwick@Sun.COM 		spa_config_enter(next->l2ad_spa, SCL_L2ARC, next, RW_READER);
38716987Sbrendan 	mutex_exit(&spa_namespace_lock);
38726987Sbrendan 
38735450Sbrendan 	return (next);
38745450Sbrendan }
38755450Sbrendan 
38765450Sbrendan /*
38776987Sbrendan  * Free buffers that were tagged for destruction.
38786987Sbrendan  */
38796987Sbrendan static void
l2arc_do_free_on_write()38806987Sbrendan l2arc_do_free_on_write()
38816987Sbrendan {
38826987Sbrendan 	list_t *buflist;
38836987Sbrendan 	l2arc_data_free_t *df, *df_prev;
38846987Sbrendan 
38856987Sbrendan 	mutex_enter(&l2arc_free_on_write_mtx);
38866987Sbrendan 	buflist = l2arc_free_on_write;
38876987Sbrendan 
38886987Sbrendan 	for (df = list_tail(buflist); df; df = df_prev) {
38896987Sbrendan 		df_prev = list_prev(buflist, df);
38906987Sbrendan 		ASSERT(df->l2df_data != NULL);
38916987Sbrendan 		ASSERT(df->l2df_func != NULL);
38926987Sbrendan 		df->l2df_func(df->l2df_data, df->l2df_size);
38936987Sbrendan 		list_remove(buflist, df);
38946987Sbrendan 		kmem_free(df, sizeof (l2arc_data_free_t));
38956987Sbrendan 	}
38966987Sbrendan 
38976987Sbrendan 	mutex_exit(&l2arc_free_on_write_mtx);
38986987Sbrendan }
38996987Sbrendan 
39006987Sbrendan /*
39015450Sbrendan  * A write to a cache device has completed.  Update all headers to allow
39025450Sbrendan  * reads from these buffers to begin.
39035450Sbrendan  */
39045450Sbrendan static void
l2arc_write_done(zio_t * zio)39055450Sbrendan l2arc_write_done(zio_t *zio)
39065450Sbrendan {
39075450Sbrendan 	l2arc_write_callback_t *cb;
39085450Sbrendan 	l2arc_dev_t *dev;
39095450Sbrendan 	list_t *buflist;
39105450Sbrendan 	arc_buf_hdr_t *head, *ab, *ab_prev;
39116987Sbrendan 	l2arc_buf_hdr_t *abl2;
39125450Sbrendan 	kmutex_t *hash_lock;
39135450Sbrendan 
39145450Sbrendan 	cb = zio->io_private;
39155450Sbrendan 	ASSERT(cb != NULL);
39165450Sbrendan 	dev = cb->l2wcb_dev;
39175450Sbrendan 	ASSERT(dev != NULL);
39185450Sbrendan 	head = cb->l2wcb_head;
39195450Sbrendan 	ASSERT(head != NULL);
39205450Sbrendan 	buflist = dev->l2ad_buflist;
39215450Sbrendan 	ASSERT(buflist != NULL);
39225450Sbrendan 	DTRACE_PROBE2(l2arc__iodone, zio_t *, zio,
39235450Sbrendan 	    l2arc_write_callback_t *, cb);
39245450Sbrendan 
39255450Sbrendan 	if (zio->io_error != 0)
39265450Sbrendan 		ARCSTAT_BUMP(arcstat_l2_writes_error);
39275450Sbrendan 
39285450Sbrendan 	mutex_enter(&l2arc_buflist_mtx);
39295450Sbrendan 
39305450Sbrendan 	/*
39315450Sbrendan 	 * All writes completed, or an error was hit.
39325450Sbrendan 	 */
39335450Sbrendan 	for (ab = list_prev(buflist, head); ab; ab = ab_prev) {
39345450Sbrendan 		ab_prev = list_prev(buflist, ab);
39355450Sbrendan 
39365450Sbrendan 		hash_lock = HDR_LOCK(ab);
39375450Sbrendan 		if (!mutex_tryenter(hash_lock)) {
39385450Sbrendan 			/*
39395450Sbrendan 			 * This buffer misses out.  It may be in a stage
39405450Sbrendan 			 * of eviction.  Its ARC_L2_WRITING flag will be
39415450Sbrendan 			 * left set, denying reads to this buffer.
39425450Sbrendan 			 */
39435450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_writes_hdr_miss);
39445450Sbrendan 			continue;
39455450Sbrendan 		}
39465450Sbrendan 
39475450Sbrendan 		if (zio->io_error != 0) {
39485450Sbrendan 			/*
39496987Sbrendan 			 * Error - drop L2ARC entry.
39505450Sbrendan 			 */
39516987Sbrendan 			list_remove(buflist, ab);
39526987Sbrendan 			abl2 = ab->b_l2hdr;
39535450Sbrendan 			ab->b_l2hdr = NULL;
39546987Sbrendan 			kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
39556987Sbrendan 			ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
39565450Sbrendan 		}
39575450Sbrendan 
39585450Sbrendan 		/*
39595450Sbrendan 		 * Allow ARC to begin reads to this L2ARC entry.
39605450Sbrendan 		 */
39615450Sbrendan 		ab->b_flags &= ~ARC_L2_WRITING;
39625450Sbrendan 
39635450Sbrendan 		mutex_exit(hash_lock);
39645450Sbrendan 	}
39655450Sbrendan 
39665450Sbrendan 	atomic_inc_64(&l2arc_writes_done);
39675450Sbrendan 	list_remove(buflist, head);
39685450Sbrendan 	kmem_cache_free(hdr_cache, head);
39695450Sbrendan 	mutex_exit(&l2arc_buflist_mtx);
39705450Sbrendan 
39716987Sbrendan 	l2arc_do_free_on_write();
39725450Sbrendan 
39735450Sbrendan 	kmem_free(cb, sizeof (l2arc_write_callback_t));
39745450Sbrendan }
39755450Sbrendan 
39765450Sbrendan /*
39775450Sbrendan  * A read to a cache device completed.  Validate buffer contents before
39785450Sbrendan  * handing over to the regular ARC routines.
39795450Sbrendan  */
39805450Sbrendan static void
l2arc_read_done(zio_t * zio)39815450Sbrendan l2arc_read_done(zio_t *zio)
39825450Sbrendan {
39835450Sbrendan 	l2arc_read_callback_t *cb;
39845450Sbrendan 	arc_buf_hdr_t *hdr;
39855450Sbrendan 	arc_buf_t *buf;
39865450Sbrendan 	kmutex_t *hash_lock;
39876987Sbrendan 	int equal;
39885450Sbrendan 
39897754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_vd != NULL);
39907754SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE);
39917754SJeff.Bonwick@Sun.COM 
39927754SJeff.Bonwick@Sun.COM 	spa_config_exit(zio->io_spa, SCL_L2ARC, zio->io_vd);
39937754SJeff.Bonwick@Sun.COM 
39945450Sbrendan 	cb = zio->io_private;
39955450Sbrendan 	ASSERT(cb != NULL);
39965450Sbrendan 	buf = cb->l2rcb_buf;
39975450Sbrendan 	ASSERT(buf != NULL);
399812296SLin.Ling@Sun.COM 
399912296SLin.Ling@Sun.COM 	hash_lock = HDR_LOCK(buf->b_hdr);
400012296SLin.Ling@Sun.COM 	mutex_enter(hash_lock);
40015450Sbrendan 	hdr = buf->b_hdr;
400212296SLin.Ling@Sun.COM 	ASSERT3P(hash_lock, ==, HDR_LOCK(hdr));
40035450Sbrendan 
40045450Sbrendan 	/*
40055450Sbrendan 	 * Check this survived the L2ARC journey.
40065450Sbrendan 	 */
40075450Sbrendan 	equal = arc_cksum_equal(buf);
40085450Sbrendan 	if (equal && zio->io_error == 0 && !HDR_L2_EVICTED(hdr)) {
40095450Sbrendan 		mutex_exit(hash_lock);
40105450Sbrendan 		zio->io_private = buf;
40117754SJeff.Bonwick@Sun.COM 		zio->io_bp_copy = cb->l2rcb_bp;	/* XXX fix in L2ARC 2.0	*/
40127754SJeff.Bonwick@Sun.COM 		zio->io_bp = &zio->io_bp_copy;	/* XXX fix in L2ARC 2.0	*/
40135450Sbrendan 		arc_read_done(zio);
40145450Sbrendan 	} else {
40155450Sbrendan 		mutex_exit(hash_lock);
40165450Sbrendan 		/*
40175450Sbrendan 		 * Buffer didn't survive caching.  Increment stats and
40185450Sbrendan 		 * reissue to the original storage device.
40195450Sbrendan 		 */
40206987Sbrendan 		if (zio->io_error != 0) {
40215450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_io_error);
40226987Sbrendan 		} else {
40236987Sbrendan 			zio->io_error = EIO;
40246987Sbrendan 		}
40255450Sbrendan 		if (!equal)
40265450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_cksum_bad);
40275450Sbrendan 
40287754SJeff.Bonwick@Sun.COM 		/*
40297754SJeff.Bonwick@Sun.COM 		 * If there's no waiter, issue an async i/o to the primary
40307754SJeff.Bonwick@Sun.COM 		 * storage now.  If there *is* a waiter, the caller must
40317754SJeff.Bonwick@Sun.COM 		 * issue the i/o in a context where it's OK to block.
40327754SJeff.Bonwick@Sun.COM 		 */
40338632SBill.Moore@Sun.COM 		if (zio->io_waiter == NULL) {
40348632SBill.Moore@Sun.COM 			zio_t *pio = zio_unique_parent(zio);
40358632SBill.Moore@Sun.COM 
40368632SBill.Moore@Sun.COM 			ASSERT(!pio || pio->io_child_type == ZIO_CHILD_LOGICAL);
40378632SBill.Moore@Sun.COM 
40388632SBill.Moore@Sun.COM 			zio_nowait(zio_read(pio, cb->l2rcb_spa, &cb->l2rcb_bp,
40397754SJeff.Bonwick@Sun.COM 			    buf->b_data, zio->io_size, arc_read_done, buf,
40407754SJeff.Bonwick@Sun.COM 			    zio->io_priority, cb->l2rcb_flags, &cb->l2rcb_zb));
40418632SBill.Moore@Sun.COM 		}
40425450Sbrendan 	}
40435450Sbrendan 
40445450Sbrendan 	kmem_free(cb, sizeof (l2arc_read_callback_t));
40455450Sbrendan }
40465450Sbrendan 
40475450Sbrendan /*
40485450Sbrendan  * This is the list priority from which the L2ARC will search for pages to
40495450Sbrendan  * cache.  This is used within loops (0..3) to cycle through lists in the
40505450Sbrendan  * desired order.  This order can have a significant effect on cache
40515450Sbrendan  * performance.
40525450Sbrendan  *
40535450Sbrendan  * Currently the metadata lists are hit first, MFU then MRU, followed by
40545450Sbrendan  * the data lists.  This function returns a locked list, and also returns
40555450Sbrendan  * the lock pointer.
40565450Sbrendan  */
40575450Sbrendan static list_t *
l2arc_list_locked(int list_num,kmutex_t ** lock)40585450Sbrendan l2arc_list_locked(int list_num, kmutex_t **lock)
40595450Sbrendan {
40605450Sbrendan 	list_t *list;
40615450Sbrendan 
40625450Sbrendan 	ASSERT(list_num >= 0 && list_num <= 3);
40635450Sbrendan 
40645450Sbrendan 	switch (list_num) {
40655450Sbrendan 	case 0:
40665450Sbrendan 		list = &arc_mfu->arcs_list[ARC_BUFC_METADATA];
40675450Sbrendan 		*lock = &arc_mfu->arcs_mtx;
40685450Sbrendan 		break;
40695450Sbrendan 	case 1:
40705450Sbrendan 		list = &arc_mru->arcs_list[ARC_BUFC_METADATA];
40715450Sbrendan 		*lock = &arc_mru->arcs_mtx;
40725450Sbrendan 		break;
40735450Sbrendan 	case 2:
40745450Sbrendan 		list = &arc_mfu->arcs_list[ARC_BUFC_DATA];
40755450Sbrendan 		*lock = &arc_mfu->arcs_mtx;
40765450Sbrendan 		break;
40775450Sbrendan 	case 3:
40785450Sbrendan 		list = &arc_mru->arcs_list[ARC_BUFC_DATA];
40795450Sbrendan 		*lock = &arc_mru->arcs_mtx;
40805450Sbrendan 		break;
40815450Sbrendan 	}
40825450Sbrendan 
40835450Sbrendan 	ASSERT(!(MUTEX_HELD(*lock)));
40845450Sbrendan 	mutex_enter(*lock);
40855450Sbrendan 	return (list);
40865450Sbrendan }
40875450Sbrendan 
40885450Sbrendan /*
40895450Sbrendan  * Evict buffers from the device write hand to the distance specified in
40905450Sbrendan  * bytes.  This distance may span populated buffers, it may span nothing.
40915450Sbrendan  * This is clearing a region on the L2ARC device ready for writing.
40925450Sbrendan  * If the 'all' boolean is set, every buffer is evicted.
40935450Sbrendan  */
40945450Sbrendan static void
l2arc_evict(l2arc_dev_t * dev,uint64_t distance,boolean_t all)40955450Sbrendan l2arc_evict(l2arc_dev_t *dev, uint64_t distance, boolean_t all)
40965450Sbrendan {
40975450Sbrendan 	list_t *buflist;
40985450Sbrendan 	l2arc_buf_hdr_t *abl2;
40995450Sbrendan 	arc_buf_hdr_t *ab, *ab_prev;
41005450Sbrendan 	kmutex_t *hash_lock;
41015450Sbrendan 	uint64_t taddr;
41025450Sbrendan 
41035450Sbrendan 	buflist = dev->l2ad_buflist;
41045450Sbrendan 
41055450Sbrendan 	if (buflist == NULL)
41065450Sbrendan 		return;
41075450Sbrendan 
41085450Sbrendan 	if (!all && dev->l2ad_first) {
41095450Sbrendan 		/*
41105450Sbrendan 		 * This is the first sweep through the device.  There is
41115450Sbrendan 		 * nothing to evict.
41125450Sbrendan 		 */
41135450Sbrendan 		return;
41145450Sbrendan 	}
41155450Sbrendan 
41166987Sbrendan 	if (dev->l2ad_hand >= (dev->l2ad_end - (2 * distance))) {
41175450Sbrendan 		/*
41185450Sbrendan 		 * When nearing the end of the device, evict to the end
41195450Sbrendan 		 * before the device write hand jumps to the start.
41205450Sbrendan 		 */
41215450Sbrendan 		taddr = dev->l2ad_end;
41225450Sbrendan 	} else {
41235450Sbrendan 		taddr = dev->l2ad_hand + distance;
41245450Sbrendan 	}
41255450Sbrendan 	DTRACE_PROBE4(l2arc__evict, l2arc_dev_t *, dev, list_t *, buflist,
41265450Sbrendan 	    uint64_t, taddr, boolean_t, all);
41275450Sbrendan 
41285450Sbrendan top:
41295450Sbrendan 	mutex_enter(&l2arc_buflist_mtx);
41305450Sbrendan 	for (ab = list_tail(buflist); ab; ab = ab_prev) {
41315450Sbrendan 		ab_prev = list_prev(buflist, ab);
41325450Sbrendan 
41335450Sbrendan 		hash_lock = HDR_LOCK(ab);
41345450Sbrendan 		if (!mutex_tryenter(hash_lock)) {
41355450Sbrendan 			/*
41365450Sbrendan 			 * Missed the hash lock.  Retry.
41375450Sbrendan 			 */
41385450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_evict_lock_retry);
41395450Sbrendan 			mutex_exit(&l2arc_buflist_mtx);
41405450Sbrendan 			mutex_enter(hash_lock);
41415450Sbrendan 			mutex_exit(hash_lock);
41425450Sbrendan 			goto top;
41435450Sbrendan 		}
41445450Sbrendan 
41455450Sbrendan 		if (HDR_L2_WRITE_HEAD(ab)) {
41465450Sbrendan 			/*
41475450Sbrendan 			 * We hit a write head node.  Leave it for
41485450Sbrendan 			 * l2arc_write_done().
41495450Sbrendan 			 */
41505450Sbrendan 			list_remove(buflist, ab);
41515450Sbrendan 			mutex_exit(hash_lock);
41525450Sbrendan 			continue;
41535450Sbrendan 		}
41545450Sbrendan 
41555450Sbrendan 		if (!all && ab->b_l2hdr != NULL &&
41565450Sbrendan 		    (ab->b_l2hdr->b_daddr > taddr ||
41575450Sbrendan 		    ab->b_l2hdr->b_daddr < dev->l2ad_hand)) {
41585450Sbrendan 			/*
41595450Sbrendan 			 * We've evicted to the target address,
41605450Sbrendan 			 * or the end of the device.
41615450Sbrendan 			 */
41625450Sbrendan 			mutex_exit(hash_lock);
41635450Sbrendan 			break;
41645450Sbrendan 		}
41655450Sbrendan 
41665450Sbrendan 		if (HDR_FREE_IN_PROGRESS(ab)) {
41675450Sbrendan 			/*
41685450Sbrendan 			 * Already on the path to destruction.
41695450Sbrendan 			 */
41705450Sbrendan 			mutex_exit(hash_lock);
41715450Sbrendan 			continue;
41725450Sbrendan 		}
41735450Sbrendan 
41745450Sbrendan 		if (ab->b_state == arc_l2c_only) {
41755450Sbrendan 			ASSERT(!HDR_L2_READING(ab));
41765450Sbrendan 			/*
41775450Sbrendan 			 * This doesn't exist in the ARC.  Destroy.
41785450Sbrendan 			 * arc_hdr_destroy() will call list_remove()
41795450Sbrendan 			 * and decrement arcstat_l2_size.
41805450Sbrendan 			 */
41815450Sbrendan 			arc_change_state(arc_anon, ab, hash_lock);
41825450Sbrendan 			arc_hdr_destroy(ab);
41835450Sbrendan 		} else {
41845450Sbrendan 			/*
41856987Sbrendan 			 * Invalidate issued or about to be issued
41866987Sbrendan 			 * reads, since we may be about to write
41876987Sbrendan 			 * over this location.
41886987Sbrendan 			 */
41896987Sbrendan 			if (HDR_L2_READING(ab)) {
41906987Sbrendan 				ARCSTAT_BUMP(arcstat_l2_evict_reading);
41916987Sbrendan 				ab->b_flags |= ARC_L2_EVICTED;
41926987Sbrendan 			}
41936987Sbrendan 
41946987Sbrendan 			/*
41955450Sbrendan 			 * Tell ARC this no longer exists in L2ARC.
41965450Sbrendan 			 */
41975450Sbrendan 			if (ab->b_l2hdr != NULL) {
41985450Sbrendan 				abl2 = ab->b_l2hdr;
41995450Sbrendan 				ab->b_l2hdr = NULL;
42005450Sbrendan 				kmem_free(abl2, sizeof (l2arc_buf_hdr_t));
42015450Sbrendan 				ARCSTAT_INCR(arcstat_l2_size, -ab->b_size);
42025450Sbrendan 			}
42035450Sbrendan 			list_remove(buflist, ab);
42045450Sbrendan 
42055450Sbrendan 			/*
42065450Sbrendan 			 * This may have been leftover after a
42075450Sbrendan 			 * failed write.
42085450Sbrendan 			 */
42095450Sbrendan 			ab->b_flags &= ~ARC_L2_WRITING;
42105450Sbrendan 		}
42115450Sbrendan 		mutex_exit(hash_lock);
42125450Sbrendan 	}
42135450Sbrendan 	mutex_exit(&l2arc_buflist_mtx);
42145450Sbrendan 
421510922SJeff.Bonwick@Sun.COM 	vdev_space_update(dev->l2ad_vdev, -(taddr - dev->l2ad_evict), 0, 0);
42165450Sbrendan 	dev->l2ad_evict = taddr;
42175450Sbrendan }
42185450Sbrendan 
42195450Sbrendan /*
42205450Sbrendan  * Find and write ARC buffers to the L2ARC device.
42215450Sbrendan  *
42225450Sbrendan  * An ARC_L2_WRITING flag is set so that the L2ARC buffers are not valid
42235450Sbrendan  * for reading until they have completed writing.
42245450Sbrendan  */
42258582SBrendan.Gregg@Sun.COM static uint64_t
l2arc_write_buffers(spa_t * spa,l2arc_dev_t * dev,uint64_t target_sz)42266987Sbrendan l2arc_write_buffers(spa_t *spa, l2arc_dev_t *dev, uint64_t target_sz)
42275450Sbrendan {
42285450Sbrendan 	arc_buf_hdr_t *ab, *ab_prev, *head;
42295450Sbrendan 	l2arc_buf_hdr_t *hdrl2;
42305450Sbrendan 	list_t *list;
42316987Sbrendan 	uint64_t passed_sz, write_sz, buf_sz, headroom;
42325450Sbrendan 	void *buf_data;
42335450Sbrendan 	kmutex_t *hash_lock, *list_lock;
42345450Sbrendan 	boolean_t have_lock, full;
42355450Sbrendan 	l2arc_write_callback_t *cb;
42365450Sbrendan 	zio_t *pio, *wzio;
42378636SMark.Maybee@Sun.COM 	uint64_t guid = spa_guid(spa);
42385450Sbrendan 
42395450Sbrendan 	ASSERT(dev->l2ad_vdev != NULL);
42405450Sbrendan 
42415450Sbrendan 	pio = NULL;
42425450Sbrendan 	write_sz = 0;
42435450Sbrendan 	full = B_FALSE;
42446245Smaybee 	head = kmem_cache_alloc(hdr_cache, KM_PUSHPAGE);
42455450Sbrendan 	head->b_flags |= ARC_L2_WRITE_HEAD;
42465450Sbrendan 
42475450Sbrendan 	/*
42485450Sbrendan 	 * Copy buffers for L2ARC writing.
42495450Sbrendan 	 */
42505450Sbrendan 	mutex_enter(&l2arc_buflist_mtx);
42515450Sbrendan 	for (int try = 0; try <= 3; try++) {
42525450Sbrendan 		list = l2arc_list_locked(try, &list_lock);
42535450Sbrendan 		passed_sz = 0;
42545450Sbrendan 
42556987Sbrendan 		/*
42566987Sbrendan 		 * L2ARC fast warmup.
42576987Sbrendan 		 *
42586987Sbrendan 		 * Until the ARC is warm and starts to evict, read from the
42596987Sbrendan 		 * head of the ARC lists rather than the tail.
42606987Sbrendan 		 */
42616987Sbrendan 		headroom = target_sz * l2arc_headroom;
42626987Sbrendan 		if (arc_warm == B_FALSE)
42636987Sbrendan 			ab = list_head(list);
42646987Sbrendan 		else
42656987Sbrendan 			ab = list_tail(list);
42666987Sbrendan 
42676987Sbrendan 		for (; ab; ab = ab_prev) {
42686987Sbrendan 			if (arc_warm == B_FALSE)
42696987Sbrendan 				ab_prev = list_next(list, ab);
42706987Sbrendan 			else
42716987Sbrendan 				ab_prev = list_prev(list, ab);
42725450Sbrendan 
42735450Sbrendan 			hash_lock = HDR_LOCK(ab);
42745450Sbrendan 			have_lock = MUTEX_HELD(hash_lock);
42755450Sbrendan 			if (!have_lock && !mutex_tryenter(hash_lock)) {
42765450Sbrendan 				/*
42775450Sbrendan 				 * Skip this buffer rather than waiting.
42785450Sbrendan 				 */
42795450Sbrendan 				continue;
42805450Sbrendan 			}
42815450Sbrendan 
42825450Sbrendan 			passed_sz += ab->b_size;
42835450Sbrendan 			if (passed_sz > headroom) {
42845450Sbrendan 				/*
42855450Sbrendan 				 * Searched too far.
42865450Sbrendan 				 */
42875450Sbrendan 				mutex_exit(hash_lock);
42885450Sbrendan 				break;
42895450Sbrendan 			}
42905450Sbrendan 
42918636SMark.Maybee@Sun.COM 			if (!l2arc_write_eligible(guid, ab)) {
42925450Sbrendan 				mutex_exit(hash_lock);
42935450Sbrendan 				continue;
42945450Sbrendan 			}
42955450Sbrendan 
42965450Sbrendan 			if ((write_sz + ab->b_size) > target_sz) {
42975450Sbrendan 				full = B_TRUE;
42985450Sbrendan 				mutex_exit(hash_lock);
42995450Sbrendan 				break;
43005450Sbrendan 			}
43015450Sbrendan 
43025450Sbrendan 			if (pio == NULL) {
43035450Sbrendan 				/*
43045450Sbrendan 				 * Insert a dummy header on the buflist so
43055450Sbrendan 				 * l2arc_write_done() can find where the
43065450Sbrendan 				 * write buffers begin without searching.
43075450Sbrendan 				 */
43085450Sbrendan 				list_insert_head(dev->l2ad_buflist, head);
43095450Sbrendan 
43105450Sbrendan 				cb = kmem_alloc(
43115450Sbrendan 				    sizeof (l2arc_write_callback_t), KM_SLEEP);
43125450Sbrendan 				cb->l2wcb_dev = dev;
43135450Sbrendan 				cb->l2wcb_head = head;
43145450Sbrendan 				pio = zio_root(spa, l2arc_write_done, cb,
43155450Sbrendan 				    ZIO_FLAG_CANFAIL);
43165450Sbrendan 			}
43175450Sbrendan 
43185450Sbrendan 			/*
43195450Sbrendan 			 * Create and add a new L2ARC header.
43205450Sbrendan 			 */
43215450Sbrendan 			hdrl2 = kmem_zalloc(sizeof (l2arc_buf_hdr_t), KM_SLEEP);
43225450Sbrendan 			hdrl2->b_dev = dev;
43235450Sbrendan 			hdrl2->b_daddr = dev->l2ad_hand;
43245450Sbrendan 
43255450Sbrendan 			ab->b_flags |= ARC_L2_WRITING;
43265450Sbrendan 			ab->b_l2hdr = hdrl2;
43275450Sbrendan 			list_insert_head(dev->l2ad_buflist, ab);
43285450Sbrendan 			buf_data = ab->b_buf->b_data;
43295450Sbrendan 			buf_sz = ab->b_size;
43305450Sbrendan 
43315450Sbrendan 			/*
43325450Sbrendan 			 * Compute and store the buffer cksum before
43335450Sbrendan 			 * writing.  On debug the cksum is verified first.
43345450Sbrendan 			 */
43355450Sbrendan 			arc_cksum_verify(ab->b_buf);
43365450Sbrendan 			arc_cksum_compute(ab->b_buf, B_TRUE);
43375450Sbrendan 
43385450Sbrendan 			mutex_exit(hash_lock);
43395450Sbrendan 
43405450Sbrendan 			wzio = zio_write_phys(pio, dev->l2ad_vdev,
43415450Sbrendan 			    dev->l2ad_hand, buf_sz, buf_data, ZIO_CHECKSUM_OFF,
43425450Sbrendan 			    NULL, NULL, ZIO_PRIORITY_ASYNC_WRITE,
43435450Sbrendan 			    ZIO_FLAG_CANFAIL, B_FALSE);
43445450Sbrendan 
43455450Sbrendan 			DTRACE_PROBE2(l2arc__write, vdev_t *, dev->l2ad_vdev,
43465450Sbrendan 			    zio_t *, wzio);
43475450Sbrendan 			(void) zio_nowait(wzio);
43485450Sbrendan 
43497754SJeff.Bonwick@Sun.COM 			/*
43507754SJeff.Bonwick@Sun.COM 			 * Keep the clock hand suitably device-aligned.
43517754SJeff.Bonwick@Sun.COM 			 */
43527754SJeff.Bonwick@Sun.COM 			buf_sz = vdev_psize_to_asize(dev->l2ad_vdev, buf_sz);
43537754SJeff.Bonwick@Sun.COM 
43545450Sbrendan 			write_sz += buf_sz;
43555450Sbrendan 			dev->l2ad_hand += buf_sz;
43565450Sbrendan 		}
43575450Sbrendan 
43585450Sbrendan 		mutex_exit(list_lock);
43595450Sbrendan 
43605450Sbrendan 		if (full == B_TRUE)
43615450Sbrendan 			break;
43625450Sbrendan 	}
43635450Sbrendan 	mutex_exit(&l2arc_buflist_mtx);
43645450Sbrendan 
43655450Sbrendan 	if (pio == NULL) {
43665450Sbrendan 		ASSERT3U(write_sz, ==, 0);
43675450Sbrendan 		kmem_cache_free(hdr_cache, head);
43688582SBrendan.Gregg@Sun.COM 		return (0);
43695450Sbrendan 	}
43705450Sbrendan 
43715450Sbrendan 	ASSERT3U(write_sz, <=, target_sz);
43725450Sbrendan 	ARCSTAT_BUMP(arcstat_l2_writes_sent);
43738582SBrendan.Gregg@Sun.COM 	ARCSTAT_INCR(arcstat_l2_write_bytes, write_sz);
43745450Sbrendan 	ARCSTAT_INCR(arcstat_l2_size, write_sz);
437510922SJeff.Bonwick@Sun.COM 	vdev_space_update(dev->l2ad_vdev, write_sz, 0, 0);
43765450Sbrendan 
43775450Sbrendan 	/*
43785450Sbrendan 	 * Bump device hand to the device start if it is approaching the end.
43795450Sbrendan 	 * l2arc_evict() will already have evicted ahead for this case.
43805450Sbrendan 	 */
43816987Sbrendan 	if (dev->l2ad_hand >= (dev->l2ad_end - target_sz)) {
438210922SJeff.Bonwick@Sun.COM 		vdev_space_update(dev->l2ad_vdev,
438310922SJeff.Bonwick@Sun.COM 		    dev->l2ad_end - dev->l2ad_hand, 0, 0);
43845450Sbrendan 		dev->l2ad_hand = dev->l2ad_start;
43855450Sbrendan 		dev->l2ad_evict = dev->l2ad_start;
43865450Sbrendan 		dev->l2ad_first = B_FALSE;
43875450Sbrendan 	}
43885450Sbrendan 
43898582SBrendan.Gregg@Sun.COM 	dev->l2ad_writing = B_TRUE;
43905450Sbrendan 	(void) zio_wait(pio);
43918582SBrendan.Gregg@Sun.COM 	dev->l2ad_writing = B_FALSE;
43928582SBrendan.Gregg@Sun.COM 
43938582SBrendan.Gregg@Sun.COM 	return (write_sz);
43945450Sbrendan }
43955450Sbrendan 
43965450Sbrendan /*
43975450Sbrendan  * This thread feeds the L2ARC at regular intervals.  This is the beating
43985450Sbrendan  * heart of the L2ARC.
43995450Sbrendan  */
44005450Sbrendan static void
l2arc_feed_thread(void)44015450Sbrendan l2arc_feed_thread(void)
44025450Sbrendan {
44035450Sbrendan 	callb_cpr_t cpr;
44045450Sbrendan 	l2arc_dev_t *dev;
44055450Sbrendan 	spa_t *spa;
44068582SBrendan.Gregg@Sun.COM 	uint64_t size, wrote;
440711066Srafael.vanoni@sun.com 	clock_t begin, next = ddi_get_lbolt();
44085450Sbrendan 
44095450Sbrendan 	CALLB_CPR_INIT(&cpr, &l2arc_feed_thr_lock, callb_generic_cpr, FTAG);
44105450Sbrendan 
44115450Sbrendan 	mutex_enter(&l2arc_feed_thr_lock);
44125450Sbrendan 
44135450Sbrendan 	while (l2arc_thread_exit == 0) {
44145450Sbrendan 		CALLB_CPR_SAFE_BEGIN(&cpr);
44156987Sbrendan 		(void) cv_timedwait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock,
44168582SBrendan.Gregg@Sun.COM 		    next);
44176987Sbrendan 		CALLB_CPR_SAFE_END(&cpr, &l2arc_feed_thr_lock);
441811066Srafael.vanoni@sun.com 		next = ddi_get_lbolt() + hz;
44196987Sbrendan 
44206987Sbrendan 		/*
44216987Sbrendan 		 * Quick check for L2ARC devices.
44226987Sbrendan 		 */
44236987Sbrendan 		mutex_enter(&l2arc_dev_mtx);
44246987Sbrendan 		if (l2arc_ndev == 0) {
44256987Sbrendan 			mutex_exit(&l2arc_dev_mtx);
44266987Sbrendan 			continue;
44275450Sbrendan 		}
44286987Sbrendan 		mutex_exit(&l2arc_dev_mtx);
442911066Srafael.vanoni@sun.com 		begin = ddi_get_lbolt();
44306643Seschrock 
44315450Sbrendan 		/*
44326643Seschrock 		 * This selects the next l2arc device to write to, and in
44336643Seschrock 		 * doing so the next spa to feed from: dev->l2ad_spa.   This
44346987Sbrendan 		 * will return NULL if there are now no l2arc devices or if
44356987Sbrendan 		 * they are all faulted.
44366987Sbrendan 		 *
44376987Sbrendan 		 * If a device is returned, its spa's config lock is also
44386987Sbrendan 		 * held to prevent device removal.  l2arc_dev_get_next()
44396987Sbrendan 		 * will grab and release l2arc_dev_mtx.
44405450Sbrendan 		 */
44416987Sbrendan 		if ((dev = l2arc_dev_get_next()) == NULL)
44425450Sbrendan 			continue;
44436987Sbrendan 
44446987Sbrendan 		spa = dev->l2ad_spa;
44456987Sbrendan 		ASSERT(spa != NULL);
44465450Sbrendan 
44475450Sbrendan 		/*
4448*13049SGeorge.Wilson@Sun.COM 		 * If the pool is read-only then force the feed thread to
4449*13049SGeorge.Wilson@Sun.COM 		 * sleep a little longer.
4450*13049SGeorge.Wilson@Sun.COM 		 */
4451*13049SGeorge.Wilson@Sun.COM 		if (!spa_writeable(spa)) {
4452*13049SGeorge.Wilson@Sun.COM 			next = ddi_get_lbolt() + 5 * l2arc_feed_secs * hz;
4453*13049SGeorge.Wilson@Sun.COM 			spa_config_exit(spa, SCL_L2ARC, dev);
4454*13049SGeorge.Wilson@Sun.COM 			continue;
4455*13049SGeorge.Wilson@Sun.COM 		}
4456*13049SGeorge.Wilson@Sun.COM 
4457*13049SGeorge.Wilson@Sun.COM 		/*
44585450Sbrendan 		 * Avoid contributing to memory pressure.
44595450Sbrendan 		 */
44605450Sbrendan 		if (arc_reclaim_needed()) {
44615450Sbrendan 			ARCSTAT_BUMP(arcstat_l2_abort_lowmem);
44627754SJeff.Bonwick@Sun.COM 			spa_config_exit(spa, SCL_L2ARC, dev);
44635450Sbrendan 			continue;
44645450Sbrendan 		}
44655450Sbrendan 
44665450Sbrendan 		ARCSTAT_BUMP(arcstat_l2_feeds);
44675450Sbrendan 
44688582SBrendan.Gregg@Sun.COM 		size = l2arc_write_size(dev);
44696987Sbrendan 
44705450Sbrendan 		/*
44715450Sbrendan 		 * Evict L2ARC buffers that will be overwritten.
44725450Sbrendan 		 */
44736987Sbrendan 		l2arc_evict(dev, size, B_FALSE);
44745450Sbrendan 
44755450Sbrendan 		/*
44765450Sbrendan 		 * Write ARC buffers.
44775450Sbrendan 		 */
44788582SBrendan.Gregg@Sun.COM 		wrote = l2arc_write_buffers(spa, dev, size);
44798582SBrendan.Gregg@Sun.COM 
44808582SBrendan.Gregg@Sun.COM 		/*
44818582SBrendan.Gregg@Sun.COM 		 * Calculate interval between writes.
44828582SBrendan.Gregg@Sun.COM 		 */
44838582SBrendan.Gregg@Sun.COM 		next = l2arc_write_interval(begin, size, wrote);
44847754SJeff.Bonwick@Sun.COM 		spa_config_exit(spa, SCL_L2ARC, dev);
44855450Sbrendan 	}
44865450Sbrendan 
44875450Sbrendan 	l2arc_thread_exit = 0;
44885450Sbrendan 	cv_broadcast(&l2arc_feed_thr_cv);
44895450Sbrendan 	CALLB_CPR_EXIT(&cpr);		/* drops l2arc_feed_thr_lock */
44905450Sbrendan 	thread_exit();
44915450Sbrendan }
44925450Sbrendan 
44936643Seschrock boolean_t
l2arc_vdev_present(vdev_t * vd)44946643Seschrock l2arc_vdev_present(vdev_t *vd)
44956643Seschrock {
44966643Seschrock 	l2arc_dev_t *dev;
44976643Seschrock 
44986643Seschrock 	mutex_enter(&l2arc_dev_mtx);
44996643Seschrock 	for (dev = list_head(l2arc_dev_list); dev != NULL;
45006643Seschrock 	    dev = list_next(l2arc_dev_list, dev)) {
45016643Seschrock 		if (dev->l2ad_vdev == vd)
45026643Seschrock 			break;
45036643Seschrock 	}
45046643Seschrock 	mutex_exit(&l2arc_dev_mtx);
45056643Seschrock 
45066643Seschrock 	return (dev != NULL);
45076643Seschrock }
45086643Seschrock 
45095450Sbrendan /*
45105450Sbrendan  * Add a vdev for use by the L2ARC.  By this point the spa has already
45115450Sbrendan  * validated the vdev and opened it.
45125450Sbrendan  */
45135450Sbrendan void
l2arc_add_vdev(spa_t * spa,vdev_t * vd)45149816SGeorge.Wilson@Sun.COM l2arc_add_vdev(spa_t *spa, vdev_t *vd)
45155450Sbrendan {
45165450Sbrendan 	l2arc_dev_t *adddev;
45175450Sbrendan 
45186643Seschrock 	ASSERT(!l2arc_vdev_present(vd));
45196643Seschrock 
45205450Sbrendan 	/*
45215450Sbrendan 	 * Create a new l2arc device entry.
45225450Sbrendan 	 */
45235450Sbrendan 	adddev = kmem_zalloc(sizeof (l2arc_dev_t), KM_SLEEP);
45245450Sbrendan 	adddev->l2ad_spa = spa;
45255450Sbrendan 	adddev->l2ad_vdev = vd;
45265450Sbrendan 	adddev->l2ad_write = l2arc_write_max;
45276987Sbrendan 	adddev->l2ad_boost = l2arc_write_boost;
45289816SGeorge.Wilson@Sun.COM 	adddev->l2ad_start = VDEV_LABEL_START_SIZE;
45299816SGeorge.Wilson@Sun.COM 	adddev->l2ad_end = VDEV_LABEL_START_SIZE + vdev_get_min_asize(vd);
45305450Sbrendan 	adddev->l2ad_hand = adddev->l2ad_start;
45315450Sbrendan 	adddev->l2ad_evict = adddev->l2ad_start;
45325450Sbrendan 	adddev->l2ad_first = B_TRUE;
45338582SBrendan.Gregg@Sun.COM 	adddev->l2ad_writing = B_FALSE;
45345450Sbrendan 	ASSERT3U(adddev->l2ad_write, >, 0);
45355450Sbrendan 
45365450Sbrendan 	/*
45375450Sbrendan 	 * This is a list of all ARC buffers that are still valid on the
45385450Sbrendan 	 * device.
45395450Sbrendan 	 */
45405450Sbrendan 	adddev->l2ad_buflist = kmem_zalloc(sizeof (list_t), KM_SLEEP);
45415450Sbrendan 	list_create(adddev->l2ad_buflist, sizeof (arc_buf_hdr_t),
45425450Sbrendan 	    offsetof(arc_buf_hdr_t, b_l2node));
45435450Sbrendan 
454410922SJeff.Bonwick@Sun.COM 	vdev_space_update(vd, 0, 0, adddev->l2ad_end - adddev->l2ad_hand);
45455450Sbrendan 
45465450Sbrendan 	/*
45475450Sbrendan 	 * Add device to global list
45485450Sbrendan 	 */
45495450Sbrendan 	mutex_enter(&l2arc_dev_mtx);
45505450Sbrendan 	list_insert_head(l2arc_dev_list, adddev);
45515450Sbrendan 	atomic_inc_64(&l2arc_ndev);
45525450Sbrendan 	mutex_exit(&l2arc_dev_mtx);
45535450Sbrendan }
45545450Sbrendan 
45555450Sbrendan /*
45565450Sbrendan  * Remove a vdev from the L2ARC.
45575450Sbrendan  */
45585450Sbrendan void
l2arc_remove_vdev(vdev_t * vd)45595450Sbrendan l2arc_remove_vdev(vdev_t *vd)
45605450Sbrendan {
45615450Sbrendan 	l2arc_dev_t *dev, *nextdev, *remdev = NULL;
45625450Sbrendan 
45635450Sbrendan 	/*
45645450Sbrendan 	 * Find the device by vdev
45655450Sbrendan 	 */
45665450Sbrendan 	mutex_enter(&l2arc_dev_mtx);
45675450Sbrendan 	for (dev = list_head(l2arc_dev_list); dev; dev = nextdev) {
45685450Sbrendan 		nextdev = list_next(l2arc_dev_list, dev);
45695450Sbrendan 		if (vd == dev->l2ad_vdev) {
45705450Sbrendan 			remdev = dev;
45715450Sbrendan 			break;
45725450Sbrendan 		}
45735450Sbrendan 	}
45745450Sbrendan 	ASSERT(remdev != NULL);
45755450Sbrendan 
45765450Sbrendan 	/*
45775450Sbrendan 	 * Remove device from global list
45785450Sbrendan 	 */
45795450Sbrendan 	list_remove(l2arc_dev_list, remdev);
45805450Sbrendan 	l2arc_dev_last = NULL;		/* may have been invalidated */
45816987Sbrendan 	atomic_dec_64(&l2arc_ndev);
45826987Sbrendan 	mutex_exit(&l2arc_dev_mtx);
45835450Sbrendan 
45845450Sbrendan 	/*
45855450Sbrendan 	 * Clear all buflists and ARC references.  L2ARC device flush.
45865450Sbrendan 	 */
45875450Sbrendan 	l2arc_evict(remdev, 0, B_TRUE);
45885450Sbrendan 	list_destroy(remdev->l2ad_buflist);
45895450Sbrendan 	kmem_free(remdev->l2ad_buflist, sizeof (list_t));
45905450Sbrendan 	kmem_free(remdev, sizeof (l2arc_dev_t));
45915450Sbrendan }
45925450Sbrendan 
45935450Sbrendan void
l2arc_init(void)45947754SJeff.Bonwick@Sun.COM l2arc_init(void)
45955450Sbrendan {
45965450Sbrendan 	l2arc_thread_exit = 0;
45975450Sbrendan 	l2arc_ndev = 0;
45985450Sbrendan 	l2arc_writes_sent = 0;
45995450Sbrendan 	l2arc_writes_done = 0;
46005450Sbrendan 
46015450Sbrendan 	mutex_init(&l2arc_feed_thr_lock, NULL, MUTEX_DEFAULT, NULL);
46025450Sbrendan 	cv_init(&l2arc_feed_thr_cv, NULL, CV_DEFAULT, NULL);
46035450Sbrendan 	mutex_init(&l2arc_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
46045450Sbrendan 	mutex_init(&l2arc_buflist_mtx, NULL, MUTEX_DEFAULT, NULL);
46055450Sbrendan 	mutex_init(&l2arc_free_on_write_mtx, NULL, MUTEX_DEFAULT, NULL);
46065450Sbrendan 
46075450Sbrendan 	l2arc_dev_list = &L2ARC_dev_list;
46085450Sbrendan 	l2arc_free_on_write = &L2ARC_free_on_write;
46095450Sbrendan 	list_create(l2arc_dev_list, sizeof (l2arc_dev_t),
46105450Sbrendan 	    offsetof(l2arc_dev_t, l2ad_node));
46115450Sbrendan 	list_create(l2arc_free_on_write, sizeof (l2arc_data_free_t),
46125450Sbrendan 	    offsetof(l2arc_data_free_t, l2df_list_node));
46135450Sbrendan }
46145450Sbrendan 
46155450Sbrendan void
l2arc_fini(void)46167754SJeff.Bonwick@Sun.COM l2arc_fini(void)
46175450Sbrendan {
46186987Sbrendan 	/*
46196987Sbrendan 	 * This is called from dmu_fini(), which is called from spa_fini();
46206987Sbrendan 	 * Because of this, we can assume that all l2arc devices have
46216987Sbrendan 	 * already been removed when the pools themselves were removed.
46226987Sbrendan 	 */
46236987Sbrendan 
46246987Sbrendan 	l2arc_do_free_on_write();
46256987Sbrendan 
46265450Sbrendan 	mutex_destroy(&l2arc_feed_thr_lock);
46275450Sbrendan 	cv_destroy(&l2arc_feed_thr_cv);
46285450Sbrendan 	mutex_destroy(&l2arc_dev_mtx);
46295450Sbrendan 	mutex_destroy(&l2arc_buflist_mtx);
46305450Sbrendan 	mutex_destroy(&l2arc_free_on_write_mtx);
46315450Sbrendan 
46325450Sbrendan 	list_destroy(l2arc_dev_list);
46335450Sbrendan 	list_destroy(l2arc_free_on_write);
46345450Sbrendan }
46357754SJeff.Bonwick@Sun.COM 
46367754SJeff.Bonwick@Sun.COM void
l2arc_start(void)46377754SJeff.Bonwick@Sun.COM l2arc_start(void)
46387754SJeff.Bonwick@Sun.COM {
46398241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
46407754SJeff.Bonwick@Sun.COM 		return;
46417754SJeff.Bonwick@Sun.COM 
46427754SJeff.Bonwick@Sun.COM 	(void) thread_create(NULL, 0, l2arc_feed_thread, NULL, 0, &p0,
46437754SJeff.Bonwick@Sun.COM 	    TS_RUN, minclsyspri);
46447754SJeff.Bonwick@Sun.COM }
46457754SJeff.Bonwick@Sun.COM 
46467754SJeff.Bonwick@Sun.COM void
l2arc_stop(void)46477754SJeff.Bonwick@Sun.COM l2arc_stop(void)
46487754SJeff.Bonwick@Sun.COM {
46498241SJeff.Bonwick@Sun.COM 	if (!(spa_mode_global & FWRITE))
46507754SJeff.Bonwick@Sun.COM 		return;
46517754SJeff.Bonwick@Sun.COM 
46527754SJeff.Bonwick@Sun.COM 	mutex_enter(&l2arc_feed_thr_lock);
46537754SJeff.Bonwick@Sun.COM 	cv_signal(&l2arc_feed_thr_cv);	/* kick thread out of startup */
46547754SJeff.Bonwick@Sun.COM 	l2arc_thread_exit = 1;
46557754SJeff.Bonwick@Sun.COM 	while (l2arc_thread_exit != 0)
46567754SJeff.Bonwick@Sun.COM 		cv_wait(&l2arc_feed_thr_cv, &l2arc_feed_thr_lock);
46577754SJeff.Bonwick@Sun.COM 	mutex_exit(&l2arc_feed_thr_lock);
46587754SJeff.Bonwick@Sun.COM }
4659