xref: /onnv-gate/usr/src/uts/common/fs/zfs/arc.c (revision 2688)
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 /*
221484Sek110237  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens /*
29789Sahrens  * DVA-based Adjustable Relpacement Cache
30789Sahrens  *
311544Seschrock  * While much of the theory of operation used here is
321544Seschrock  * based on the self-tuning, low overhead replacement cache
33789Sahrens  * presented by Megiddo and Modha at FAST 2003, there are some
34789Sahrens  * significant differences:
35789Sahrens  *
36789Sahrens  * 1. The Megiddo and Modha model assumes any page is evictable.
37789Sahrens  * Pages in its cache cannot be "locked" into memory.  This makes
38789Sahrens  * the eviction algorithm simple: evict the last page in the list.
39789Sahrens  * This also make the performance characteristics easy to reason
40789Sahrens  * about.  Our cache is not so simple.  At any given moment, some
41789Sahrens  * subset of the blocks in the cache are un-evictable because we
42789Sahrens  * have handed out a reference to them.  Blocks are only evictable
43789Sahrens  * when there are no external references active.  This makes
44789Sahrens  * eviction far more problematic:  we choose to evict the evictable
45789Sahrens  * blocks that are the "lowest" in the list.
46789Sahrens  *
47789Sahrens  * There are times when it is not possible to evict the requested
48789Sahrens  * space.  In these circumstances we are unable to adjust the cache
49789Sahrens  * size.  To prevent the cache growing unbounded at these times we
50789Sahrens  * implement a "cache throttle" that slowes the flow of new data
51789Sahrens  * into the cache until we can make space avaiable.
52789Sahrens  *
53789Sahrens  * 2. The Megiddo and Modha model assumes a fixed cache size.
54789Sahrens  * Pages are evicted when the cache is full and there is a cache
55789Sahrens  * miss.  Our model has a variable sized cache.  It grows with
56789Sahrens  * high use, but also tries to react to memory preasure from the
57789Sahrens  * operating system: decreasing its size when system memory is
58789Sahrens  * tight.
59789Sahrens  *
60789Sahrens  * 3. The Megiddo and Modha model assumes a fixed page size. All
61789Sahrens  * elements of the cache are therefor exactly the same size.  So
62789Sahrens  * when adjusting the cache size following a cache miss, its simply
63789Sahrens  * a matter of choosing a single page to evict.  In our model, we
64789Sahrens  * have variable sized cache blocks (rangeing from 512 bytes to
65789Sahrens  * 128K bytes).  We therefor choose a set of blocks to evict to make
66789Sahrens  * space for a cache miss that approximates as closely as possible
67789Sahrens  * the space used by the new block.
68789Sahrens  *
69789Sahrens  * See also:  "ARC: A Self-Tuning, Low Overhead Replacement Cache"
70789Sahrens  * by N. Megiddo & D. Modha, FAST 2003
71789Sahrens  */
72789Sahrens 
73789Sahrens /*
74789Sahrens  * The locking model:
75789Sahrens  *
76789Sahrens  * A new reference to a cache buffer can be obtained in two
77789Sahrens  * ways: 1) via a hash table lookup using the DVA as a key,
78789Sahrens  * or 2) via one of the ARC lists.  The arc_read() inerface
79789Sahrens  * uses method 1, while the internal arc algorithms for
80789Sahrens  * adjusting the cache use method 2.  We therefor provide two
81789Sahrens  * types of locks: 1) the hash table lock array, and 2) the
82789Sahrens  * arc list locks.
83789Sahrens  *
84789Sahrens  * Buffers do not have their own mutexs, rather they rely on the
85789Sahrens  * hash table mutexs for the bulk of their protection (i.e. most
86789Sahrens  * fields in the arc_buf_hdr_t are protected by these mutexs).
87789Sahrens  *
88789Sahrens  * buf_hash_find() returns the appropriate mutex (held) when it
89789Sahrens  * locates the requested buffer in the hash table.  It returns
90789Sahrens  * NULL for the mutex if the buffer was not in the table.
91789Sahrens  *
92789Sahrens  * buf_hash_remove() expects the appropriate hash mutex to be
93789Sahrens  * already held before it is invoked.
94789Sahrens  *
95789Sahrens  * Each arc state also has a mutex which is used to protect the
96789Sahrens  * buffer list associated with the state.  When attempting to
97789Sahrens  * obtain a hash table lock while holding an arc list lock you
98789Sahrens  * must use: mutex_tryenter() to avoid deadlock.  Also note that
99*2688Smaybee  * the active state mutex must be held before the ghost state mutex.
100789Sahrens  *
1011544Seschrock  * Arc buffers may have an associated eviction callback function.
1021544Seschrock  * This function will be invoked prior to removing the buffer (e.g.
1031544Seschrock  * in arc_do_user_evicts()).  Note however that the data associated
1041544Seschrock  * with the buffer may be evicted prior to the callback.  The callback
1051544Seschrock  * must be made with *no locks held* (to prevent deadlock).  Additionally,
1061544Seschrock  * the users of callbacks must ensure that their private data is
1071544Seschrock  * protected from simultaneous callbacks from arc_buf_evict()
1081544Seschrock  * and arc_do_user_evicts().
1091544Seschrock  *
110789Sahrens  * Note that the majority of the performance stats are manipulated
111789Sahrens  * with atomic operations.
112789Sahrens  */
113789Sahrens 
114789Sahrens #include <sys/spa.h>
115789Sahrens #include <sys/zio.h>
116789Sahrens #include <sys/zfs_context.h>
117789Sahrens #include <sys/arc.h>
118789Sahrens #include <sys/refcount.h>
119789Sahrens #ifdef _KERNEL
120789Sahrens #include <sys/vmsystm.h>
121789Sahrens #include <vm/anon.h>
122789Sahrens #include <sys/fs/swapnode.h>
1231484Sek110237 #include <sys/dnlc.h>
124789Sahrens #endif
125789Sahrens #include <sys/callb.h>
126789Sahrens 
127789Sahrens static kmutex_t		arc_reclaim_thr_lock;
128789Sahrens static kcondvar_t	arc_reclaim_thr_cv;	/* used to signal reclaim thr */
129789Sahrens static uint8_t		arc_thread_exit;
130789Sahrens 
1311484Sek110237 #define	ARC_REDUCE_DNLC_PERCENT	3
1321484Sek110237 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
1331484Sek110237 
134789Sahrens typedef enum arc_reclaim_strategy {
135789Sahrens 	ARC_RECLAIM_AGGR,		/* Aggressive reclaim strategy */
136789Sahrens 	ARC_RECLAIM_CONS		/* Conservative reclaim strategy */
137789Sahrens } arc_reclaim_strategy_t;
138789Sahrens 
139789Sahrens /* number of seconds before growing cache again */
140789Sahrens static int		arc_grow_retry = 60;
141789Sahrens 
1422391Smaybee /*
1432638Sperrin  * minimum lifespan of a prefetch block in clock ticks
1442638Sperrin  * (initialized in arc_init())
1452391Smaybee  */
1462638Sperrin static int		arc_min_prefetch_lifespan;
1472391Smaybee 
148789Sahrens static kmutex_t arc_reclaim_lock;
149789Sahrens static int arc_dead;
150789Sahrens 
151789Sahrens /*
152789Sahrens  * Note that buffers can be on one of 5 states:
153789Sahrens  *	ARC_anon	- anonymous (discussed below)
1541544Seschrock  *	ARC_mru		- recently used, currently cached
1551544Seschrock  *	ARC_mru_ghost	- recentely used, no longer in cache
1561544Seschrock  *	ARC_mfu		- frequently used, currently cached
1571544Seschrock  *	ARC_mfu_ghost	- frequently used, no longer in cache
158789Sahrens  * When there are no active references to the buffer, they
159789Sahrens  * are linked onto one of the lists in arc.  These are the
160789Sahrens  * only buffers that can be evicted or deleted.
161789Sahrens  *
162789Sahrens  * Anonymous buffers are buffers that are not associated with
163789Sahrens  * a DVA.  These are buffers that hold dirty block copies
164789Sahrens  * before they are written to stable storage.  By definition,
1651544Seschrock  * they are "ref'd" and are considered part of arc_mru
166789Sahrens  * that cannot be freed.  Generally, they will aquire a DVA
1671544Seschrock  * as they are written and migrate onto the arc_mru list.
168789Sahrens  */
169789Sahrens 
170789Sahrens typedef struct arc_state {
171789Sahrens 	list_t	list;	/* linked list of evictable buffer in state */
172789Sahrens 	uint64_t lsize;	/* total size of buffers in the linked list */
173789Sahrens 	uint64_t size;	/* total size of all buffers in this state */
174789Sahrens 	uint64_t hits;
175789Sahrens 	kmutex_t mtx;
176789Sahrens } arc_state_t;
177789Sahrens 
178789Sahrens /* The 5 states: */
179789Sahrens static arc_state_t ARC_anon;
1801544Seschrock static arc_state_t ARC_mru;
1811544Seschrock static arc_state_t ARC_mru_ghost;
1821544Seschrock static arc_state_t ARC_mfu;
1831544Seschrock static arc_state_t ARC_mfu_ghost;
184789Sahrens 
185789Sahrens static struct arc {
186789Sahrens 	arc_state_t 	*anon;
1871544Seschrock 	arc_state_t	*mru;
1881544Seschrock 	arc_state_t	*mru_ghost;
1891544Seschrock 	arc_state_t	*mfu;
1901544Seschrock 	arc_state_t	*mfu_ghost;
191789Sahrens 	uint64_t	size;		/* Actual total arc size */
1921544Seschrock 	uint64_t	p;		/* Target size (in bytes) of mru */
193789Sahrens 	uint64_t	c;		/* Target size of cache (in bytes) */
194789Sahrens 	uint64_t	c_min;		/* Minimum target cache size */
195789Sahrens 	uint64_t	c_max;		/* Maximum target cache size */
196789Sahrens 
197789Sahrens 	/* performance stats */
198789Sahrens 	uint64_t	hits;
199789Sahrens 	uint64_t	misses;
200789Sahrens 	uint64_t	deleted;
201*2688Smaybee 	uint64_t	recycle_miss;
202*2688Smaybee 	uint64_t	mutex_miss;
203*2688Smaybee 	uint64_t	evict_skip;
204789Sahrens 	uint64_t	hash_elements;
205789Sahrens 	uint64_t	hash_elements_max;
206789Sahrens 	uint64_t	hash_collisions;
207789Sahrens 	uint64_t	hash_chains;
208789Sahrens 	uint32_t	hash_chain_max;
209789Sahrens 
210789Sahrens 	int		no_grow;	/* Don't try to grow cache size */
211789Sahrens } arc;
212789Sahrens 
213789Sahrens static uint64_t arc_tempreserve;
214789Sahrens 
215789Sahrens typedef struct arc_callback arc_callback_t;
216789Sahrens 
217789Sahrens struct arc_callback {
218789Sahrens 	arc_done_func_t		*acb_done;
219789Sahrens 	void			*acb_private;
220789Sahrens 	arc_byteswap_func_t	*acb_byteswap;
221789Sahrens 	arc_buf_t		*acb_buf;
222789Sahrens 	zio_t			*acb_zio_dummy;
223789Sahrens 	arc_callback_t		*acb_next;
224789Sahrens };
225789Sahrens 
226789Sahrens struct arc_buf_hdr {
227789Sahrens 	/* immutable */
228789Sahrens 	uint64_t		b_size;
229789Sahrens 	spa_t			*b_spa;
230789Sahrens 
231789Sahrens 	/* protected by hash lock */
232789Sahrens 	dva_t			b_dva;
233789Sahrens 	uint64_t		b_birth;
234789Sahrens 	uint64_t		b_cksum0;
235789Sahrens 
236789Sahrens 	arc_buf_hdr_t		*b_hash_next;
237789Sahrens 	arc_buf_t		*b_buf;
238789Sahrens 	uint32_t		b_flags;
2391544Seschrock 	uint32_t		b_datacnt;
240789Sahrens 
241789Sahrens 	kcondvar_t		b_cv;
242789Sahrens 	arc_callback_t		*b_acb;
243789Sahrens 
244789Sahrens 	/* protected by arc state mutex */
245789Sahrens 	arc_state_t		*b_state;
246789Sahrens 	list_node_t		b_arc_node;
247789Sahrens 
248789Sahrens 	/* updated atomically */
249789Sahrens 	clock_t			b_arc_access;
250789Sahrens 
251789Sahrens 	/* self protecting */
252789Sahrens 	refcount_t		b_refcnt;
253789Sahrens };
254789Sahrens 
2551544Seschrock static arc_buf_t *arc_eviction_list;
2561544Seschrock static kmutex_t arc_eviction_mtx;
257*2688Smaybee static void arc_get_data_buf(arc_buf_t *buf);
258*2688Smaybee static void arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
2591544Seschrock 
2601544Seschrock #define	GHOST_STATE(state)	\
2611544Seschrock 	((state) == arc.mru_ghost || (state) == arc.mfu_ghost)
2621544Seschrock 
263789Sahrens /*
264789Sahrens  * Private ARC flags.  These flags are private ARC only flags that will show up
265789Sahrens  * in b_flags in the arc_hdr_buf_t.  Some flags are publicly declared, and can
266789Sahrens  * be passed in as arc_flags in things like arc_read.  However, these flags
267789Sahrens  * should never be passed and should only be set by ARC code.  When adding new
268789Sahrens  * public flags, make sure not to smash the private ones.
269789Sahrens  */
270789Sahrens 
2711544Seschrock #define	ARC_IN_HASH_TABLE	(1 << 9)	/* this buffer is hashed */
272789Sahrens #define	ARC_IO_IN_PROGRESS	(1 << 10)	/* I/O in progress for buf */
273789Sahrens #define	ARC_IO_ERROR		(1 << 11)	/* I/O failed for buf */
274789Sahrens #define	ARC_FREED_IN_READ	(1 << 12)	/* buf freed while in read */
2751544Seschrock #define	ARC_BUF_AVAILABLE	(1 << 13)	/* block not in active use */
2762391Smaybee #define	ARC_INDIRECT		(1 << 14)	/* this is an indirect block */
277789Sahrens 
2781544Seschrock #define	HDR_IN_HASH_TABLE(hdr)	((hdr)->b_flags & ARC_IN_HASH_TABLE)
279789Sahrens #define	HDR_IO_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS)
280789Sahrens #define	HDR_IO_ERROR(hdr)	((hdr)->b_flags & ARC_IO_ERROR)
281789Sahrens #define	HDR_FREED_IN_READ(hdr)	((hdr)->b_flags & ARC_FREED_IN_READ)
2821544Seschrock #define	HDR_BUF_AVAILABLE(hdr)	((hdr)->b_flags & ARC_BUF_AVAILABLE)
283789Sahrens 
284789Sahrens /*
285789Sahrens  * Hash table routines
286789Sahrens  */
287789Sahrens 
288789Sahrens #define	HT_LOCK_PAD	64
289789Sahrens 
290789Sahrens struct ht_lock {
291789Sahrens 	kmutex_t	ht_lock;
292789Sahrens #ifdef _KERNEL
293789Sahrens 	unsigned char	pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
294789Sahrens #endif
295789Sahrens };
296789Sahrens 
297789Sahrens #define	BUF_LOCKS 256
298789Sahrens typedef struct buf_hash_table {
299789Sahrens 	uint64_t ht_mask;
300789Sahrens 	arc_buf_hdr_t **ht_table;
301789Sahrens 	struct ht_lock ht_locks[BUF_LOCKS];
302789Sahrens } buf_hash_table_t;
303789Sahrens 
304789Sahrens static buf_hash_table_t buf_hash_table;
305789Sahrens 
306789Sahrens #define	BUF_HASH_INDEX(spa, dva, birth) \
307789Sahrens 	(buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
308789Sahrens #define	BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
309789Sahrens #define	BUF_HASH_LOCK(idx)	(&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
310789Sahrens #define	HDR_LOCK(buf) \
311789Sahrens 	(BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth)))
312789Sahrens 
313789Sahrens uint64_t zfs_crc64_table[256];
314789Sahrens 
315789Sahrens static uint64_t
316789Sahrens buf_hash(spa_t *spa, dva_t *dva, uint64_t birth)
317789Sahrens {
318789Sahrens 	uintptr_t spav = (uintptr_t)spa;
319789Sahrens 	uint8_t *vdva = (uint8_t *)dva;
320789Sahrens 	uint64_t crc = -1ULL;
321789Sahrens 	int i;
322789Sahrens 
323789Sahrens 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
324789Sahrens 
325789Sahrens 	for (i = 0; i < sizeof (dva_t); i++)
326789Sahrens 		crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
327789Sahrens 
328789Sahrens 	crc ^= (spav>>8) ^ birth;
329789Sahrens 
330789Sahrens 	return (crc);
331789Sahrens }
332789Sahrens 
333789Sahrens #define	BUF_EMPTY(buf)						\
334789Sahrens 	((buf)->b_dva.dva_word[0] == 0 &&			\
335789Sahrens 	(buf)->b_dva.dva_word[1] == 0 &&			\
336789Sahrens 	(buf)->b_birth == 0)
337789Sahrens 
338789Sahrens #define	BUF_EQUAL(spa, dva, birth, buf)				\
339789Sahrens 	((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) &&	\
340789Sahrens 	((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) &&	\
341789Sahrens 	((buf)->b_birth == birth) && ((buf)->b_spa == spa)
342789Sahrens 
343789Sahrens static arc_buf_hdr_t *
344789Sahrens buf_hash_find(spa_t *spa, dva_t *dva, uint64_t birth, kmutex_t **lockp)
345789Sahrens {
346789Sahrens 	uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
347789Sahrens 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
348789Sahrens 	arc_buf_hdr_t *buf;
349789Sahrens 
350789Sahrens 	mutex_enter(hash_lock);
351789Sahrens 	for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
352789Sahrens 	    buf = buf->b_hash_next) {
353789Sahrens 		if (BUF_EQUAL(spa, dva, birth, buf)) {
354789Sahrens 			*lockp = hash_lock;
355789Sahrens 			return (buf);
356789Sahrens 		}
357789Sahrens 	}
358789Sahrens 	mutex_exit(hash_lock);
359789Sahrens 	*lockp = NULL;
360789Sahrens 	return (NULL);
361789Sahrens }
362789Sahrens 
363789Sahrens /*
364789Sahrens  * Insert an entry into the hash table.  If there is already an element
365789Sahrens  * equal to elem in the hash table, then the already existing element
366789Sahrens  * will be returned and the new element will not be inserted.
367789Sahrens  * Otherwise returns NULL.
368789Sahrens  */
369789Sahrens static arc_buf_hdr_t *
370789Sahrens buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
371789Sahrens {
372789Sahrens 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
373789Sahrens 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
374789Sahrens 	arc_buf_hdr_t *fbuf;
375789Sahrens 	uint32_t max, i;
376789Sahrens 
3771544Seschrock 	ASSERT(!HDR_IN_HASH_TABLE(buf));
378789Sahrens 	*lockp = hash_lock;
379789Sahrens 	mutex_enter(hash_lock);
380789Sahrens 	for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
381789Sahrens 	    fbuf = fbuf->b_hash_next, i++) {
382789Sahrens 		if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
383789Sahrens 			return (fbuf);
384789Sahrens 	}
385789Sahrens 
386789Sahrens 	buf->b_hash_next = buf_hash_table.ht_table[idx];
387789Sahrens 	buf_hash_table.ht_table[idx] = buf;
3881544Seschrock 	buf->b_flags |= ARC_IN_HASH_TABLE;
389789Sahrens 
390789Sahrens 	/* collect some hash table performance data */
391789Sahrens 	if (i > 0) {
392789Sahrens 		atomic_add_64(&arc.hash_collisions, 1);
393789Sahrens 		if (i == 1)
394789Sahrens 			atomic_add_64(&arc.hash_chains, 1);
395789Sahrens 	}
396789Sahrens 	while (i > (max = arc.hash_chain_max) &&
397789Sahrens 	    max != atomic_cas_32(&arc.hash_chain_max, max, i)) {
398789Sahrens 		continue;
399789Sahrens 	}
400789Sahrens 	atomic_add_64(&arc.hash_elements, 1);
401789Sahrens 	if (arc.hash_elements > arc.hash_elements_max)
402789Sahrens 		atomic_add_64(&arc.hash_elements_max, 1);
403789Sahrens 
404789Sahrens 	return (NULL);
405789Sahrens }
406789Sahrens 
407789Sahrens static void
408789Sahrens buf_hash_remove(arc_buf_hdr_t *buf)
409789Sahrens {
410789Sahrens 	arc_buf_hdr_t *fbuf, **bufp;
411789Sahrens 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
412789Sahrens 
413789Sahrens 	ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
4141544Seschrock 	ASSERT(HDR_IN_HASH_TABLE(buf));
415789Sahrens 
416789Sahrens 	bufp = &buf_hash_table.ht_table[idx];
417789Sahrens 	while ((fbuf = *bufp) != buf) {
418789Sahrens 		ASSERT(fbuf != NULL);
419789Sahrens 		bufp = &fbuf->b_hash_next;
420789Sahrens 	}
421789Sahrens 	*bufp = buf->b_hash_next;
422789Sahrens 	buf->b_hash_next = NULL;
4231544Seschrock 	buf->b_flags &= ~ARC_IN_HASH_TABLE;
424789Sahrens 
425789Sahrens 	/* collect some hash table performance data */
426789Sahrens 	atomic_add_64(&arc.hash_elements, -1);
427789Sahrens 	if (buf_hash_table.ht_table[idx] &&
428789Sahrens 	    buf_hash_table.ht_table[idx]->b_hash_next == NULL)
429789Sahrens 		atomic_add_64(&arc.hash_chains, -1);
430789Sahrens }
431789Sahrens 
432789Sahrens /*
433789Sahrens  * Global data structures and functions for the buf kmem cache.
434789Sahrens  */
435789Sahrens static kmem_cache_t *hdr_cache;
436789Sahrens static kmem_cache_t *buf_cache;
437789Sahrens 
438789Sahrens static void
439789Sahrens buf_fini(void)
440789Sahrens {
441789Sahrens 	int i;
442789Sahrens 
443789Sahrens 	kmem_free(buf_hash_table.ht_table,
444789Sahrens 	    (buf_hash_table.ht_mask + 1) * sizeof (void *));
445789Sahrens 	for (i = 0; i < BUF_LOCKS; i++)
446789Sahrens 		mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
447789Sahrens 	kmem_cache_destroy(hdr_cache);
448789Sahrens 	kmem_cache_destroy(buf_cache);
449789Sahrens }
450789Sahrens 
451789Sahrens /*
452789Sahrens  * Constructor callback - called when the cache is empty
453789Sahrens  * and a new buf is requested.
454789Sahrens  */
455789Sahrens /* ARGSUSED */
456789Sahrens static int
457789Sahrens hdr_cons(void *vbuf, void *unused, int kmflag)
458789Sahrens {
459789Sahrens 	arc_buf_hdr_t *buf = vbuf;
460789Sahrens 
461789Sahrens 	bzero(buf, sizeof (arc_buf_hdr_t));
462789Sahrens 	refcount_create(&buf->b_refcnt);
463789Sahrens 	cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
464789Sahrens 	return (0);
465789Sahrens }
466789Sahrens 
467789Sahrens /*
468789Sahrens  * Destructor callback - called when a cached buf is
469789Sahrens  * no longer required.
470789Sahrens  */
471789Sahrens /* ARGSUSED */
472789Sahrens static void
473789Sahrens hdr_dest(void *vbuf, void *unused)
474789Sahrens {
475789Sahrens 	arc_buf_hdr_t *buf = vbuf;
476789Sahrens 
477789Sahrens 	refcount_destroy(&buf->b_refcnt);
478789Sahrens 	cv_destroy(&buf->b_cv);
479789Sahrens }
480789Sahrens 
4811544Seschrock static int arc_reclaim_needed(void);
482789Sahrens void arc_kmem_reclaim(void);
483789Sahrens 
484789Sahrens /*
485789Sahrens  * Reclaim callback -- invoked when memory is low.
486789Sahrens  */
487789Sahrens /* ARGSUSED */
488789Sahrens static void
489789Sahrens hdr_recl(void *unused)
490789Sahrens {
491789Sahrens 	dprintf("hdr_recl called\n");
4921544Seschrock 	if (arc_reclaim_needed())
4931544Seschrock 		arc_kmem_reclaim();
494789Sahrens }
495789Sahrens 
496789Sahrens static void
497789Sahrens buf_init(void)
498789Sahrens {
499789Sahrens 	uint64_t *ct;
5001544Seschrock 	uint64_t hsize = 1ULL << 12;
501789Sahrens 	int i, j;
502789Sahrens 
503789Sahrens 	/*
504789Sahrens 	 * The hash table is big enough to fill all of physical memory
5051544Seschrock 	 * with an average 64K block size.  The table will take up
5061544Seschrock 	 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
507789Sahrens 	 */
5081544Seschrock 	while (hsize * 65536 < physmem * PAGESIZE)
509789Sahrens 		hsize <<= 1;
5101544Seschrock retry:
511789Sahrens 	buf_hash_table.ht_mask = hsize - 1;
5121544Seschrock 	buf_hash_table.ht_table =
5131544Seschrock 	    kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
5141544Seschrock 	if (buf_hash_table.ht_table == NULL) {
5151544Seschrock 		ASSERT(hsize > (1ULL << 8));
5161544Seschrock 		hsize >>= 1;
5171544Seschrock 		goto retry;
5181544Seschrock 	}
519789Sahrens 
520789Sahrens 	hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
521789Sahrens 	    0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
522789Sahrens 	buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
523789Sahrens 	    0, NULL, NULL, NULL, NULL, NULL, 0);
524789Sahrens 
525789Sahrens 	for (i = 0; i < 256; i++)
526789Sahrens 		for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
527789Sahrens 			*ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
528789Sahrens 
529789Sahrens 	for (i = 0; i < BUF_LOCKS; i++) {
530789Sahrens 		mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
531789Sahrens 		    NULL, MUTEX_DEFAULT, NULL);
532789Sahrens 	}
533789Sahrens }
534789Sahrens 
535789Sahrens #define	ARC_MINTIME	(hz>>4) /* 62 ms */
536789Sahrens 
537789Sahrens static void
538789Sahrens add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
539789Sahrens {
540789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
541789Sahrens 
542789Sahrens 	if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
543789Sahrens 	    (ab->b_state != arc.anon)) {
5441544Seschrock 		int delta = ab->b_size * ab->b_datacnt;
545789Sahrens 
546789Sahrens 		ASSERT(!MUTEX_HELD(&ab->b_state->mtx));
547789Sahrens 		mutex_enter(&ab->b_state->mtx);
548789Sahrens 		ASSERT(list_link_active(&ab->b_arc_node));
549789Sahrens 		list_remove(&ab->b_state->list, ab);
5501544Seschrock 		if (GHOST_STATE(ab->b_state)) {
5511544Seschrock 			ASSERT3U(ab->b_datacnt, ==, 0);
5521544Seschrock 			ASSERT3P(ab->b_buf, ==, NULL);
5531544Seschrock 			delta = ab->b_size;
5541544Seschrock 		}
5551544Seschrock 		ASSERT(delta > 0);
5561544Seschrock 		ASSERT3U(ab->b_state->lsize, >=, delta);
5571544Seschrock 		atomic_add_64(&ab->b_state->lsize, -delta);
558789Sahrens 		mutex_exit(&ab->b_state->mtx);
5592391Smaybee 		/* remove the prefetch flag is we get a reference */
5602391Smaybee 		if (ab->b_flags & ARC_PREFETCH)
5612391Smaybee 			ab->b_flags &= ~ARC_PREFETCH;
562789Sahrens 	}
563789Sahrens }
564789Sahrens 
565789Sahrens static int
566789Sahrens remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
567789Sahrens {
568789Sahrens 	int cnt;
569789Sahrens 
5701544Seschrock 	ASSERT(ab->b_state == arc.anon || MUTEX_HELD(hash_lock));
5711544Seschrock 	ASSERT(!GHOST_STATE(ab->b_state));
572789Sahrens 
573789Sahrens 	if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
574789Sahrens 	    (ab->b_state != arc.anon)) {
575789Sahrens 
576789Sahrens 		ASSERT(!MUTEX_HELD(&ab->b_state->mtx));
577789Sahrens 		mutex_enter(&ab->b_state->mtx);
578789Sahrens 		ASSERT(!list_link_active(&ab->b_arc_node));
579789Sahrens 		list_insert_head(&ab->b_state->list, ab);
5801544Seschrock 		ASSERT(ab->b_datacnt > 0);
5811544Seschrock 		atomic_add_64(&ab->b_state->lsize, ab->b_size * ab->b_datacnt);
5821544Seschrock 		ASSERT3U(ab->b_state->size, >=, ab->b_state->lsize);
583789Sahrens 		mutex_exit(&ab->b_state->mtx);
584789Sahrens 	}
585789Sahrens 	return (cnt);
586789Sahrens }
587789Sahrens 
588789Sahrens /*
589789Sahrens  * Move the supplied buffer to the indicated state.  The mutex
590789Sahrens  * for the buffer must be held by the caller.
591789Sahrens  */
592789Sahrens static void
5931544Seschrock arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
594789Sahrens {
5951544Seschrock 	arc_state_t *old_state = ab->b_state;
5961544Seschrock 	int refcnt = refcount_count(&ab->b_refcnt);
5971544Seschrock 	int from_delta, to_delta;
598789Sahrens 
599789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
6001544Seschrock 	ASSERT(new_state != old_state);
6011544Seschrock 	ASSERT(refcnt == 0 || ab->b_datacnt > 0);
6021544Seschrock 	ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
6031544Seschrock 
6041544Seschrock 	from_delta = to_delta = ab->b_datacnt * ab->b_size;
605789Sahrens 
606789Sahrens 	/*
607789Sahrens 	 * If this buffer is evictable, transfer it from the
608789Sahrens 	 * old state list to the new state list.
609789Sahrens 	 */
6101544Seschrock 	if (refcnt == 0) {
6111544Seschrock 		if (old_state != arc.anon) {
6121544Seschrock 			int use_mutex = !MUTEX_HELD(&old_state->mtx);
6131544Seschrock 
6141544Seschrock 			if (use_mutex)
6151544Seschrock 				mutex_enter(&old_state->mtx);
6161544Seschrock 
6171544Seschrock 			ASSERT(list_link_active(&ab->b_arc_node));
6181544Seschrock 			list_remove(&old_state->list, ab);
619789Sahrens 
6202391Smaybee 			/*
6212391Smaybee 			 * If prefetching out of the ghost cache,
6222391Smaybee 			 * we will have a non-null datacnt.
6232391Smaybee 			 */
6242391Smaybee 			if (GHOST_STATE(old_state) && ab->b_datacnt == 0) {
6252391Smaybee 				/* ghost elements have a ghost size */
6261544Seschrock 				ASSERT(ab->b_buf == NULL);
6271544Seschrock 				from_delta = ab->b_size;
628789Sahrens 			}
6291544Seschrock 			ASSERT3U(old_state->lsize, >=, from_delta);
6301544Seschrock 			atomic_add_64(&old_state->lsize, -from_delta);
6311544Seschrock 
6321544Seschrock 			if (use_mutex)
6331544Seschrock 				mutex_exit(&old_state->mtx);
634789Sahrens 		}
635789Sahrens 		if (new_state != arc.anon) {
6361544Seschrock 			int use_mutex = !MUTEX_HELD(&new_state->mtx);
637789Sahrens 
6381544Seschrock 			if (use_mutex)
639789Sahrens 				mutex_enter(&new_state->mtx);
6401544Seschrock 
641789Sahrens 			list_insert_head(&new_state->list, ab);
6421544Seschrock 
6431544Seschrock 			/* ghost elements have a ghost size */
6441544Seschrock 			if (GHOST_STATE(new_state)) {
6451544Seschrock 				ASSERT(ab->b_datacnt == 0);
6461544Seschrock 				ASSERT(ab->b_buf == NULL);
6471544Seschrock 				to_delta = ab->b_size;
6481544Seschrock 			}
6491544Seschrock 			atomic_add_64(&new_state->lsize, to_delta);
6501544Seschrock 			ASSERT3U(new_state->size + to_delta, >=,
6511544Seschrock 			    new_state->lsize);
6521544Seschrock 
6531544Seschrock 			if (use_mutex)
654789Sahrens 				mutex_exit(&new_state->mtx);
655789Sahrens 		}
656789Sahrens 	}
657789Sahrens 
658789Sahrens 	ASSERT(!BUF_EMPTY(ab));
6591544Seschrock 	if (new_state == arc.anon && old_state != arc.anon) {
660789Sahrens 		buf_hash_remove(ab);
661789Sahrens 	}
662789Sahrens 
6631544Seschrock 	/* adjust state sizes */
6641544Seschrock 	if (to_delta)
6651544Seschrock 		atomic_add_64(&new_state->size, to_delta);
6661544Seschrock 	if (from_delta) {
6671544Seschrock 		ASSERT3U(old_state->size, >=, from_delta);
6681544Seschrock 		atomic_add_64(&old_state->size, -from_delta);
669789Sahrens 	}
670789Sahrens 	ab->b_state = new_state;
671789Sahrens }
672789Sahrens 
673789Sahrens arc_buf_t *
674789Sahrens arc_buf_alloc(spa_t *spa, int size, void *tag)
675789Sahrens {
676789Sahrens 	arc_buf_hdr_t *hdr;
677789Sahrens 	arc_buf_t *buf;
678789Sahrens 
679789Sahrens 	ASSERT3U(size, >, 0);
680789Sahrens 	hdr = kmem_cache_alloc(hdr_cache, KM_SLEEP);
681789Sahrens 	ASSERT(BUF_EMPTY(hdr));
682789Sahrens 	hdr->b_size = size;
683789Sahrens 	hdr->b_spa = spa;
684789Sahrens 	hdr->b_state = arc.anon;
685789Sahrens 	hdr->b_arc_access = 0;
686789Sahrens 	buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
687789Sahrens 	buf->b_hdr = hdr;
688*2688Smaybee 	buf->b_data = NULL;
6891544Seschrock 	buf->b_efunc = NULL;
6901544Seschrock 	buf->b_private = NULL;
691789Sahrens 	buf->b_next = NULL;
692789Sahrens 	hdr->b_buf = buf;
693*2688Smaybee 	arc_get_data_buf(buf);
6941544Seschrock 	hdr->b_datacnt = 1;
695789Sahrens 	hdr->b_flags = 0;
696789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
697789Sahrens 	(void) refcount_add(&hdr->b_refcnt, tag);
698789Sahrens 
699789Sahrens 	return (buf);
700789Sahrens }
701789Sahrens 
702*2688Smaybee static arc_buf_t *
703*2688Smaybee arc_buf_clone(arc_buf_t *from)
7041544Seschrock {
705*2688Smaybee 	arc_buf_t *buf;
706*2688Smaybee 	arc_buf_hdr_t *hdr = from->b_hdr;
707*2688Smaybee 	uint64_t size = hdr->b_size;
7081544Seschrock 
709*2688Smaybee 	buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
710*2688Smaybee 	buf->b_hdr = hdr;
711*2688Smaybee 	buf->b_data = NULL;
712*2688Smaybee 	buf->b_efunc = NULL;
713*2688Smaybee 	buf->b_private = NULL;
714*2688Smaybee 	buf->b_next = hdr->b_buf;
715*2688Smaybee 	hdr->b_buf = buf;
716*2688Smaybee 	arc_get_data_buf(buf);
717*2688Smaybee 	bcopy(from->b_data, buf->b_data, size);
718*2688Smaybee 	hdr->b_datacnt += 1;
719*2688Smaybee 	return (buf);
7201544Seschrock }
7211544Seschrock 
7221544Seschrock void
7231544Seschrock arc_buf_add_ref(arc_buf_t *buf, void* tag)
7241544Seschrock {
7251544Seschrock 	arc_buf_hdr_t *hdr;
7261544Seschrock 	kmutex_t *hash_lock;
7271544Seschrock 
7281544Seschrock 	mutex_enter(&arc_eviction_mtx);
7291544Seschrock 	hdr = buf->b_hdr;
7301544Seschrock 	if (buf->b_data == NULL) {
7311544Seschrock 		/*
7321544Seschrock 		 * This buffer is evicted.
7331544Seschrock 		 */
7341544Seschrock 		mutex_exit(&arc_eviction_mtx);
7351544Seschrock 		return;
7361544Seschrock 	} else {
7371544Seschrock 		/*
7381544Seschrock 		 * Prevent this buffer from being evicted
7391544Seschrock 		 * while we add a reference.
7401544Seschrock 		 */
7411544Seschrock 		buf->b_hdr = NULL;
7421544Seschrock 	}
7431544Seschrock 	mutex_exit(&arc_eviction_mtx);
7441544Seschrock 
7451544Seschrock 	ASSERT(hdr->b_state != arc.anon);
7461544Seschrock 	hash_lock = HDR_LOCK(hdr);
7471544Seschrock 	mutex_enter(hash_lock);
7481544Seschrock 	ASSERT(!GHOST_STATE(hdr->b_state));
7491544Seschrock 	buf->b_hdr = hdr;
7501544Seschrock 	add_reference(hdr, hash_lock, tag);
751*2688Smaybee 	arc_access(hdr, hash_lock);
752*2688Smaybee 	mutex_exit(hash_lock);
7531544Seschrock 	atomic_add_64(&arc.hits, 1);
7541544Seschrock }
7551544Seschrock 
756789Sahrens static void
757*2688Smaybee arc_buf_destroy(arc_buf_t *buf, boolean_t recycle, boolean_t all)
7581544Seschrock {
7591544Seschrock 	arc_buf_t **bufp;
7601544Seschrock 
7611544Seschrock 	/* free up data associated with the buf */
7621544Seschrock 	if (buf->b_data) {
7631544Seschrock 		arc_state_t *state = buf->b_hdr->b_state;
7641544Seschrock 		uint64_t size = buf->b_hdr->b_size;
7651544Seschrock 
766*2688Smaybee 		if (!recycle) {
767*2688Smaybee 			zio_buf_free(buf->b_data, size);
768*2688Smaybee 			atomic_add_64(&arc.size, -size);
769*2688Smaybee 		}
7701544Seschrock 		if (list_link_active(&buf->b_hdr->b_arc_node)) {
7711544Seschrock 			ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
7721544Seschrock 			ASSERT(state != arc.anon);
7731544Seschrock 			ASSERT3U(state->lsize, >=, size);
7741544Seschrock 			atomic_add_64(&state->lsize, -size);
7751544Seschrock 		}
7761544Seschrock 		ASSERT3U(state->size, >=, size);
7771544Seschrock 		atomic_add_64(&state->size, -size);
7781544Seschrock 		buf->b_data = NULL;
7791544Seschrock 		ASSERT(buf->b_hdr->b_datacnt > 0);
7801544Seschrock 		buf->b_hdr->b_datacnt -= 1;
7811544Seschrock 	}
7821544Seschrock 
7831544Seschrock 	/* only remove the buf if requested */
7841544Seschrock 	if (!all)
7851544Seschrock 		return;
7861544Seschrock 
7871544Seschrock 	/* remove the buf from the hdr list */
7881544Seschrock 	for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
7891544Seschrock 		continue;
7901544Seschrock 	*bufp = buf->b_next;
7911544Seschrock 
7921544Seschrock 	ASSERT(buf->b_efunc == NULL);
7931544Seschrock 
7941544Seschrock 	/* clean up the buf */
7951544Seschrock 	buf->b_hdr = NULL;
7961544Seschrock 	kmem_cache_free(buf_cache, buf);
7971544Seschrock }
7981544Seschrock 
7991544Seschrock static void
8001544Seschrock arc_hdr_destroy(arc_buf_hdr_t *hdr)
801789Sahrens {
802789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
803789Sahrens 	ASSERT3P(hdr->b_state, ==, arc.anon);
8041544Seschrock 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
805789Sahrens 
806789Sahrens 	if (!BUF_EMPTY(hdr)) {
8071544Seschrock 		ASSERT(!HDR_IN_HASH_TABLE(hdr));
808789Sahrens 		bzero(&hdr->b_dva, sizeof (dva_t));
809789Sahrens 		hdr->b_birth = 0;
810789Sahrens 		hdr->b_cksum0 = 0;
811789Sahrens 	}
8121544Seschrock 	while (hdr->b_buf) {
813789Sahrens 		arc_buf_t *buf = hdr->b_buf;
814789Sahrens 
8151544Seschrock 		if (buf->b_efunc) {
8161544Seschrock 			mutex_enter(&arc_eviction_mtx);
8171544Seschrock 			ASSERT(buf->b_hdr != NULL);
818*2688Smaybee 			arc_buf_destroy(hdr->b_buf, FALSE, FALSE);
8191544Seschrock 			hdr->b_buf = buf->b_next;
8201544Seschrock 			buf->b_next = arc_eviction_list;
8211544Seschrock 			arc_eviction_list = buf;
8221544Seschrock 			mutex_exit(&arc_eviction_mtx);
8231544Seschrock 		} else {
824*2688Smaybee 			arc_buf_destroy(hdr->b_buf, FALSE, TRUE);
8251544Seschrock 		}
826789Sahrens 	}
8271544Seschrock 
828789Sahrens 	ASSERT(!list_link_active(&hdr->b_arc_node));
829789Sahrens 	ASSERT3P(hdr->b_hash_next, ==, NULL);
830789Sahrens 	ASSERT3P(hdr->b_acb, ==, NULL);
831789Sahrens 	kmem_cache_free(hdr_cache, hdr);
832789Sahrens }
833789Sahrens 
834789Sahrens void
835789Sahrens arc_buf_free(arc_buf_t *buf, void *tag)
836789Sahrens {
837789Sahrens 	arc_buf_hdr_t *hdr = buf->b_hdr;
8381544Seschrock 	int hashed = hdr->b_state != arc.anon;
8391544Seschrock 
8401544Seschrock 	ASSERT(buf->b_efunc == NULL);
8411544Seschrock 	ASSERT(buf->b_data != NULL);
8421544Seschrock 
8431544Seschrock 	if (hashed) {
8441544Seschrock 		kmutex_t *hash_lock = HDR_LOCK(hdr);
8451544Seschrock 
8461544Seschrock 		mutex_enter(hash_lock);
8471544Seschrock 		(void) remove_reference(hdr, hash_lock, tag);
8481544Seschrock 		if (hdr->b_datacnt > 1)
849*2688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
8501544Seschrock 		else
8511544Seschrock 			hdr->b_flags |= ARC_BUF_AVAILABLE;
8521544Seschrock 		mutex_exit(hash_lock);
8531544Seschrock 	} else if (HDR_IO_IN_PROGRESS(hdr)) {
8541544Seschrock 		int destroy_hdr;
8551544Seschrock 		/*
8561544Seschrock 		 * We are in the middle of an async write.  Don't destroy
8571544Seschrock 		 * this buffer unless the write completes before we finish
8581544Seschrock 		 * decrementing the reference count.
8591544Seschrock 		 */
8601544Seschrock 		mutex_enter(&arc_eviction_mtx);
8611544Seschrock 		(void) remove_reference(hdr, NULL, tag);
8621544Seschrock 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
8631544Seschrock 		destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
8641544Seschrock 		mutex_exit(&arc_eviction_mtx);
8651544Seschrock 		if (destroy_hdr)
8661544Seschrock 			arc_hdr_destroy(hdr);
8671544Seschrock 	} else {
8681544Seschrock 		if (remove_reference(hdr, NULL, tag) > 0) {
8691544Seschrock 			ASSERT(HDR_IO_ERROR(hdr));
870*2688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
8711544Seschrock 		} else {
8721544Seschrock 			arc_hdr_destroy(hdr);
8731544Seschrock 		}
8741544Seschrock 	}
8751544Seschrock }
8761544Seschrock 
8771544Seschrock int
8781544Seschrock arc_buf_remove_ref(arc_buf_t *buf, void* tag)
8791544Seschrock {
8801544Seschrock 	arc_buf_hdr_t *hdr = buf->b_hdr;
881789Sahrens 	kmutex_t *hash_lock = HDR_LOCK(hdr);
8821544Seschrock 	int no_callback = (buf->b_efunc == NULL);
8831544Seschrock 
8841544Seschrock 	if (hdr->b_state == arc.anon) {
8851544Seschrock 		arc_buf_free(buf, tag);
8861544Seschrock 		return (no_callback);
8871544Seschrock 	}
888789Sahrens 
889789Sahrens 	mutex_enter(hash_lock);
8901544Seschrock 	ASSERT(hdr->b_state != arc.anon);
8911544Seschrock 	ASSERT(buf->b_data != NULL);
892789Sahrens 
8931544Seschrock 	(void) remove_reference(hdr, hash_lock, tag);
8941544Seschrock 	if (hdr->b_datacnt > 1) {
8951544Seschrock 		if (no_callback)
896*2688Smaybee 			arc_buf_destroy(buf, FALSE, TRUE);
8971544Seschrock 	} else if (no_callback) {
8981544Seschrock 		ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
8991544Seschrock 		hdr->b_flags |= ARC_BUF_AVAILABLE;
900789Sahrens 	}
9011544Seschrock 	ASSERT(no_callback || hdr->b_datacnt > 1 ||
9021544Seschrock 	    refcount_is_zero(&hdr->b_refcnt));
903789Sahrens 	mutex_exit(hash_lock);
9041544Seschrock 	return (no_callback);
905789Sahrens }
906789Sahrens 
907789Sahrens int
908789Sahrens arc_buf_size(arc_buf_t *buf)
909789Sahrens {
910789Sahrens 	return (buf->b_hdr->b_size);
911789Sahrens }
912789Sahrens 
913789Sahrens /*
914789Sahrens  * Evict buffers from list until we've removed the specified number of
915789Sahrens  * bytes.  Move the removed buffers to the appropriate evict state.
916*2688Smaybee  * If the recycle flag is set, then attempt to "recycle" a buffer:
917*2688Smaybee  * - look for a buffer to evict that is `bytes' long.
918*2688Smaybee  * - return the data block from this buffer rather than freeing it.
919*2688Smaybee  * This flag is used by callers that are trying to make space for a
920*2688Smaybee  * new buffer in a full arc cache.
921789Sahrens  */
922*2688Smaybee static void *
923*2688Smaybee arc_evict(arc_state_t *state, int64_t bytes, boolean_t recycle)
924789Sahrens {
925789Sahrens 	arc_state_t *evicted_state;
926*2688Smaybee 	uint64_t bytes_evicted = 0, skipped = 0, missed = 0;
927789Sahrens 	arc_buf_hdr_t *ab, *ab_prev;
928789Sahrens 	kmutex_t *hash_lock;
929*2688Smaybee 	boolean_t have_lock;
930*2688Smaybee 	void *steal = NULL;
931789Sahrens 
9321544Seschrock 	ASSERT(state == arc.mru || state == arc.mfu);
933789Sahrens 
9341544Seschrock 	evicted_state = (state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost;
935789Sahrens 
936789Sahrens 	mutex_enter(&state->mtx);
937789Sahrens 	mutex_enter(&evicted_state->mtx);
938789Sahrens 
939789Sahrens 	for (ab = list_tail(&state->list); ab; ab = ab_prev) {
940789Sahrens 		ab_prev = list_prev(&state->list, ab);
9412391Smaybee 		/* prefetch buffers have a minimum lifespan */
942*2688Smaybee 		if (HDR_IO_IN_PROGRESS(ab) ||
943*2688Smaybee 		    (ab->b_flags & (ARC_PREFETCH|ARC_INDIRECT) &&
944*2688Smaybee 		    lbolt - ab->b_arc_access < arc_min_prefetch_lifespan)) {
9452391Smaybee 			skipped++;
9462391Smaybee 			continue;
9472391Smaybee 		}
948*2688Smaybee 		if (recycle && (ab->b_size != bytes || ab->b_datacnt > 1))
949*2688Smaybee 			continue;
950789Sahrens 		hash_lock = HDR_LOCK(ab);
951*2688Smaybee 		have_lock = MUTEX_HELD(hash_lock);
952*2688Smaybee 		if (have_lock || mutex_tryenter(hash_lock)) {
953789Sahrens 			ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0);
9541544Seschrock 			ASSERT(ab->b_datacnt > 0);
9551544Seschrock 			while (ab->b_buf) {
9561544Seschrock 				arc_buf_t *buf = ab->b_buf;
957*2688Smaybee 				if (buf->b_data) {
9581544Seschrock 					bytes_evicted += ab->b_size;
959*2688Smaybee 					if (recycle)
960*2688Smaybee 						steal = buf->b_data;
961*2688Smaybee 				}
9621544Seschrock 				if (buf->b_efunc) {
9631544Seschrock 					mutex_enter(&arc_eviction_mtx);
9641544Seschrock 					/*
9651544Seschrock 					 * arc_buf_add_ref() could derail
9661544Seschrock 					 * this eviction.
9671544Seschrock 					 */
9681544Seschrock 					if (buf->b_hdr == NULL) {
9691544Seschrock 						mutex_exit(&arc_eviction_mtx);
970*2688Smaybee 						bytes_evicted -= ab->b_size;
971*2688Smaybee 						if (recycle)
972*2688Smaybee 							steal = NULL;
973*2688Smaybee 						if (!have_lock)
974*2688Smaybee 							mutex_exit(hash_lock);
975*2688Smaybee 						goto derailed;
9761544Seschrock 					}
977*2688Smaybee 					arc_buf_destroy(buf, recycle, FALSE);
9781544Seschrock 					ab->b_buf = buf->b_next;
9791544Seschrock 					buf->b_next = arc_eviction_list;
9801544Seschrock 					arc_eviction_list = buf;
9811544Seschrock 					mutex_exit(&arc_eviction_mtx);
9821544Seschrock 				} else {
983*2688Smaybee 					arc_buf_destroy(buf, recycle, TRUE);
9841544Seschrock 				}
9851544Seschrock 			}
9861544Seschrock 			ASSERT(ab->b_datacnt == 0);
987789Sahrens 			arc_change_state(evicted_state, ab, hash_lock);
9881544Seschrock 			ASSERT(HDR_IN_HASH_TABLE(ab));
9891544Seschrock 			ab->b_flags = ARC_IN_HASH_TABLE;
990789Sahrens 			DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
991*2688Smaybee 			if (!have_lock)
992*2688Smaybee 				mutex_exit(hash_lock);
9931544Seschrock 			if (bytes >= 0 && bytes_evicted >= bytes)
994789Sahrens 				break;
995789Sahrens 		} else {
996*2688Smaybee 			missed += 1;
997789Sahrens 		}
998*2688Smaybee derailed:
999*2688Smaybee 		/* null statement */;
1000789Sahrens 	}
1001789Sahrens 	mutex_exit(&evicted_state->mtx);
1002789Sahrens 	mutex_exit(&state->mtx);
1003789Sahrens 
1004789Sahrens 	if (bytes_evicted < bytes)
1005789Sahrens 		dprintf("only evicted %lld bytes from %x",
1006789Sahrens 		    (longlong_t)bytes_evicted, state);
1007789Sahrens 
1008*2688Smaybee 	if (skipped)
1009*2688Smaybee 		atomic_add_64(&arc.evict_skip, skipped);
1010*2688Smaybee 	if (missed)
1011*2688Smaybee 		atomic_add_64(&arc.mutex_miss, missed);
1012*2688Smaybee 	return (steal);
1013789Sahrens }
1014789Sahrens 
1015789Sahrens /*
1016789Sahrens  * Remove buffers from list until we've removed the specified number of
1017789Sahrens  * bytes.  Destroy the buffers that are removed.
1018789Sahrens  */
1019789Sahrens static void
10201544Seschrock arc_evict_ghost(arc_state_t *state, int64_t bytes)
1021789Sahrens {
1022789Sahrens 	arc_buf_hdr_t *ab, *ab_prev;
1023789Sahrens 	kmutex_t *hash_lock;
10241544Seschrock 	uint64_t bytes_deleted = 0;
10251544Seschrock 	uint_t bufs_skipped = 0;
1026789Sahrens 
10271544Seschrock 	ASSERT(GHOST_STATE(state));
1028789Sahrens top:
1029789Sahrens 	mutex_enter(&state->mtx);
1030789Sahrens 	for (ab = list_tail(&state->list); ab; ab = ab_prev) {
1031789Sahrens 		ab_prev = list_prev(&state->list, ab);
1032789Sahrens 		hash_lock = HDR_LOCK(ab);
1033789Sahrens 		if (mutex_tryenter(hash_lock)) {
10342391Smaybee 			ASSERT(!HDR_IO_IN_PROGRESS(ab));
10351544Seschrock 			ASSERT(ab->b_buf == NULL);
1036789Sahrens 			arc_change_state(arc.anon, ab, hash_lock);
1037789Sahrens 			mutex_exit(hash_lock);
1038789Sahrens 			atomic_add_64(&arc.deleted, 1);
10391544Seschrock 			bytes_deleted += ab->b_size;
10401544Seschrock 			arc_hdr_destroy(ab);
1041789Sahrens 			DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1042789Sahrens 			if (bytes >= 0 && bytes_deleted >= bytes)
1043789Sahrens 				break;
1044789Sahrens 		} else {
1045789Sahrens 			if (bytes < 0) {
1046789Sahrens 				mutex_exit(&state->mtx);
1047789Sahrens 				mutex_enter(hash_lock);
1048789Sahrens 				mutex_exit(hash_lock);
1049789Sahrens 				goto top;
1050789Sahrens 			}
1051789Sahrens 			bufs_skipped += 1;
1052789Sahrens 		}
1053789Sahrens 	}
1054789Sahrens 	mutex_exit(&state->mtx);
1055789Sahrens 
1056789Sahrens 	if (bufs_skipped) {
1057*2688Smaybee 		atomic_add_64(&arc.mutex_miss, bufs_skipped);
1058789Sahrens 		ASSERT(bytes >= 0);
1059789Sahrens 	}
1060789Sahrens 
1061789Sahrens 	if (bytes_deleted < bytes)
1062789Sahrens 		dprintf("only deleted %lld bytes from %p",
1063789Sahrens 		    (longlong_t)bytes_deleted, state);
1064789Sahrens }
1065789Sahrens 
1066789Sahrens static void
1067789Sahrens arc_adjust(void)
1068789Sahrens {
1069789Sahrens 	int64_t top_sz, mru_over, arc_over;
1070789Sahrens 
10711544Seschrock 	top_sz = arc.anon->size + arc.mru->size;
1072789Sahrens 
10731544Seschrock 	if (top_sz > arc.p && arc.mru->lsize > 0) {
10741544Seschrock 		int64_t toevict = MIN(arc.mru->lsize, top_sz-arc.p);
1075*2688Smaybee 		(void) arc_evict(arc.mru, toevict, FALSE);
10761544Seschrock 		top_sz = arc.anon->size + arc.mru->size;
1077789Sahrens 	}
1078789Sahrens 
10791544Seschrock 	mru_over = top_sz + arc.mru_ghost->size - arc.c;
1080789Sahrens 
1081789Sahrens 	if (mru_over > 0) {
10821544Seschrock 		if (arc.mru_ghost->lsize > 0) {
10831544Seschrock 			int64_t todelete = MIN(arc.mru_ghost->lsize, mru_over);
10841544Seschrock 			arc_evict_ghost(arc.mru_ghost, todelete);
1085789Sahrens 		}
1086789Sahrens 	}
1087789Sahrens 
1088789Sahrens 	if ((arc_over = arc.size - arc.c) > 0) {
10891544Seschrock 		int64_t tbl_over;
1090789Sahrens 
10911544Seschrock 		if (arc.mfu->lsize > 0) {
10921544Seschrock 			int64_t toevict = MIN(arc.mfu->lsize, arc_over);
1093*2688Smaybee 			(void) arc_evict(arc.mfu, toevict, FALSE);
1094789Sahrens 		}
1095789Sahrens 
10961544Seschrock 		tbl_over = arc.size + arc.mru_ghost->lsize +
10971544Seschrock 		    arc.mfu_ghost->lsize - arc.c*2;
1098789Sahrens 
10991544Seschrock 		if (tbl_over > 0 && arc.mfu_ghost->lsize > 0) {
11001544Seschrock 			int64_t todelete = MIN(arc.mfu_ghost->lsize, tbl_over);
11011544Seschrock 			arc_evict_ghost(arc.mfu_ghost, todelete);
1102789Sahrens 		}
1103789Sahrens 	}
1104789Sahrens }
1105789Sahrens 
11061544Seschrock static void
11071544Seschrock arc_do_user_evicts(void)
11081544Seschrock {
11091544Seschrock 	mutex_enter(&arc_eviction_mtx);
11101544Seschrock 	while (arc_eviction_list != NULL) {
11111544Seschrock 		arc_buf_t *buf = arc_eviction_list;
11121544Seschrock 		arc_eviction_list = buf->b_next;
11131544Seschrock 		buf->b_hdr = NULL;
11141544Seschrock 		mutex_exit(&arc_eviction_mtx);
11151544Seschrock 
11161819Smaybee 		if (buf->b_efunc != NULL)
11171819Smaybee 			VERIFY(buf->b_efunc(buf) == 0);
11181544Seschrock 
11191544Seschrock 		buf->b_efunc = NULL;
11201544Seschrock 		buf->b_private = NULL;
11211544Seschrock 		kmem_cache_free(buf_cache, buf);
11221544Seschrock 		mutex_enter(&arc_eviction_mtx);
11231544Seschrock 	}
11241544Seschrock 	mutex_exit(&arc_eviction_mtx);
11251544Seschrock }
11261544Seschrock 
1127789Sahrens /*
1128789Sahrens  * Flush all *evictable* data from the cache.
1129789Sahrens  * NOTE: this will not touch "active" (i.e. referenced) data.
1130789Sahrens  */
1131789Sahrens void
1132789Sahrens arc_flush(void)
1133789Sahrens {
1134*2688Smaybee 	while (list_head(&arc.mru->list))
1135*2688Smaybee 		(void) arc_evict(arc.mru, -1, FALSE);
1136*2688Smaybee 	while (list_head(&arc.mfu->list))
1137*2688Smaybee 		(void) arc_evict(arc.mfu, -1, FALSE);
1138789Sahrens 
11391544Seschrock 	arc_evict_ghost(arc.mru_ghost, -1);
11401544Seschrock 	arc_evict_ghost(arc.mfu_ghost, -1);
11411544Seschrock 
11421544Seschrock 	mutex_enter(&arc_reclaim_thr_lock);
11431544Seschrock 	arc_do_user_evicts();
11441544Seschrock 	mutex_exit(&arc_reclaim_thr_lock);
11451544Seschrock 	ASSERT(arc_eviction_list == NULL);
1146789Sahrens }
1147789Sahrens 
11482391Smaybee int arc_kmem_reclaim_shift = 5;		/* log2(fraction of arc to reclaim) */
11492391Smaybee 
1150789Sahrens void
1151789Sahrens arc_kmem_reclaim(void)
1152789Sahrens {
11532048Sstans 	uint64_t to_free;
11542048Sstans 
1155789Sahrens 	/*
1156789Sahrens 	 * We need arc_reclaim_lock because we don't want multiple
1157789Sahrens 	 * threads trying to reclaim concurrently.
1158789Sahrens 	 */
1159789Sahrens 
1160789Sahrens 	/*
1161789Sahrens 	 * umem calls the reclaim func when we destroy the buf cache,
1162789Sahrens 	 * which is after we do arc_fini().  So we set a flag to prevent
1163789Sahrens 	 * accessing the destroyed mutexes and lists.
1164789Sahrens 	 */
1165789Sahrens 	if (arc_dead)
1166789Sahrens 		return;
1167789Sahrens 
11681544Seschrock 	if (arc.c <= arc.c_min)
11691544Seschrock 		return;
11701544Seschrock 
1171789Sahrens 	mutex_enter(&arc_reclaim_lock);
1172789Sahrens 
11732048Sstans #ifdef _KERNEL
11742391Smaybee 	to_free = MAX(arc.c >> arc_kmem_reclaim_shift, ptob(needfree));
11752048Sstans #else
11762391Smaybee 	to_free = arc.c >> arc_kmem_reclaim_shift;
11772048Sstans #endif
11782048Sstans 	if (arc.c > to_free)
11792048Sstans 		atomic_add_64(&arc.c, -to_free);
11802048Sstans 	else
11812048Sstans 		arc.c = arc.c_min;
11822048Sstans 
11832391Smaybee 	atomic_add_64(&arc.p, -(arc.p >> arc_kmem_reclaim_shift));
11841544Seschrock 	if (arc.c > arc.size)
11851544Seschrock 		arc.c = arc.size;
1186789Sahrens 	if (arc.c < arc.c_min)
1187789Sahrens 		arc.c = arc.c_min;
11881544Seschrock 	if (arc.p > arc.c)
11891544Seschrock 		arc.p = (arc.c >> 1);
11901544Seschrock 	ASSERT((int64_t)arc.p >= 0);
1191789Sahrens 
1192789Sahrens 	arc_adjust();
1193789Sahrens 
1194789Sahrens 	mutex_exit(&arc_reclaim_lock);
1195789Sahrens }
1196789Sahrens 
1197789Sahrens static int
1198789Sahrens arc_reclaim_needed(void)
1199789Sahrens {
1200789Sahrens 	uint64_t extra;
1201789Sahrens 
1202789Sahrens #ifdef _KERNEL
12032048Sstans 
12042048Sstans 	if (needfree)
12052048Sstans 		return (1);
12062048Sstans 
1207789Sahrens 	/*
1208789Sahrens 	 * take 'desfree' extra pages, so we reclaim sooner, rather than later
1209789Sahrens 	 */
1210789Sahrens 	extra = desfree;
1211789Sahrens 
1212789Sahrens 	/*
1213789Sahrens 	 * check that we're out of range of the pageout scanner.  It starts to
1214789Sahrens 	 * schedule paging if freemem is less than lotsfree and needfree.
1215789Sahrens 	 * lotsfree is the high-water mark for pageout, and needfree is the
1216789Sahrens 	 * number of needed free pages.  We add extra pages here to make sure
1217789Sahrens 	 * the scanner doesn't start up while we're freeing memory.
1218789Sahrens 	 */
1219789Sahrens 	if (freemem < lotsfree + needfree + extra)
1220789Sahrens 		return (1);
1221789Sahrens 
1222789Sahrens 	/*
1223789Sahrens 	 * check to make sure that swapfs has enough space so that anon
1224789Sahrens 	 * reservations can still succeeed. anon_resvmem() checks that the
1225789Sahrens 	 * availrmem is greater than swapfs_minfree, and the number of reserved
1226789Sahrens 	 * swap pages.  We also add a bit of extra here just to prevent
1227789Sahrens 	 * circumstances from getting really dire.
1228789Sahrens 	 */
1229789Sahrens 	if (availrmem < swapfs_minfree + swapfs_reserve + extra)
1230789Sahrens 		return (1);
1231789Sahrens 
12321936Smaybee #if defined(__i386)
1233789Sahrens 	/*
1234789Sahrens 	 * If we're on an i386 platform, it's possible that we'll exhaust the
1235789Sahrens 	 * kernel heap space before we ever run out of available physical
1236789Sahrens 	 * memory.  Most checks of the size of the heap_area compare against
1237789Sahrens 	 * tune.t_minarmem, which is the minimum available real memory that we
1238789Sahrens 	 * can have in the system.  However, this is generally fixed at 25 pages
1239789Sahrens 	 * which is so low that it's useless.  In this comparison, we seek to
1240789Sahrens 	 * calculate the total heap-size, and reclaim if more than 3/4ths of the
1241789Sahrens 	 * heap is allocated.  (Or, in the caclulation, if less than 1/4th is
1242789Sahrens 	 * free)
1243789Sahrens 	 */
1244789Sahrens 	if (btop(vmem_size(heap_arena, VMEM_FREE)) <
1245789Sahrens 	    (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
1246789Sahrens 		return (1);
1247789Sahrens #endif
1248789Sahrens 
1249789Sahrens #else
1250789Sahrens 	if (spa_get_random(100) == 0)
1251789Sahrens 		return (1);
1252789Sahrens #endif
1253789Sahrens 	return (0);
1254789Sahrens }
1255789Sahrens 
1256789Sahrens static void
1257789Sahrens arc_kmem_reap_now(arc_reclaim_strategy_t strat)
1258789Sahrens {
1259789Sahrens 	size_t			i;
1260789Sahrens 	kmem_cache_t		*prev_cache = NULL;
1261789Sahrens 	extern kmem_cache_t	*zio_buf_cache[];
1262789Sahrens 
12631484Sek110237 #ifdef _KERNEL
12641484Sek110237 	/*
12651484Sek110237 	 * First purge some DNLC entries, in case the DNLC is using
12661484Sek110237 	 * up too much memory.
12671484Sek110237 	 */
12681505Sek110237 	dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
12691936Smaybee 
12701936Smaybee #if defined(__i386)
12711936Smaybee 	/*
12721936Smaybee 	 * Reclaim unused memory from all kmem caches.
12731936Smaybee 	 */
12741936Smaybee 	kmem_reap();
12751936Smaybee #endif
12761484Sek110237 #endif
12771484Sek110237 
1278789Sahrens 	/*
12791544Seschrock 	 * An agressive reclamation will shrink the cache size as well as
12801544Seschrock 	 * reap free buffers from the arc kmem caches.
1281789Sahrens 	 */
1282789Sahrens 	if (strat == ARC_RECLAIM_AGGR)
12831544Seschrock 		arc_kmem_reclaim();
1284789Sahrens 
1285789Sahrens 	for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
1286789Sahrens 		if (zio_buf_cache[i] != prev_cache) {
1287789Sahrens 			prev_cache = zio_buf_cache[i];
1288789Sahrens 			kmem_cache_reap_now(zio_buf_cache[i]);
1289789Sahrens 		}
1290789Sahrens 	}
12911544Seschrock 	kmem_cache_reap_now(buf_cache);
12921544Seschrock 	kmem_cache_reap_now(hdr_cache);
1293789Sahrens }
1294789Sahrens 
1295789Sahrens static void
1296789Sahrens arc_reclaim_thread(void)
1297789Sahrens {
1298789Sahrens 	clock_t			growtime = 0;
1299789Sahrens 	arc_reclaim_strategy_t	last_reclaim = ARC_RECLAIM_CONS;
1300789Sahrens 	callb_cpr_t		cpr;
1301789Sahrens 
1302789Sahrens 	CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
1303789Sahrens 
1304789Sahrens 	mutex_enter(&arc_reclaim_thr_lock);
1305789Sahrens 	while (arc_thread_exit == 0) {
1306789Sahrens 		if (arc_reclaim_needed()) {
1307789Sahrens 
1308789Sahrens 			if (arc.no_grow) {
1309789Sahrens 				if (last_reclaim == ARC_RECLAIM_CONS) {
1310789Sahrens 					last_reclaim = ARC_RECLAIM_AGGR;
1311789Sahrens 				} else {
1312789Sahrens 					last_reclaim = ARC_RECLAIM_CONS;
1313789Sahrens 				}
1314789Sahrens 			} else {
1315789Sahrens 				arc.no_grow = TRUE;
1316789Sahrens 				last_reclaim = ARC_RECLAIM_AGGR;
1317789Sahrens 				membar_producer();
1318789Sahrens 			}
1319789Sahrens 
1320789Sahrens 			/* reset the growth delay for every reclaim */
1321789Sahrens 			growtime = lbolt + (arc_grow_retry * hz);
1322789Sahrens 
1323789Sahrens 			arc_kmem_reap_now(last_reclaim);
1324789Sahrens 
1325789Sahrens 		} else if ((growtime > 0) && ((growtime - lbolt) <= 0)) {
1326789Sahrens 			arc.no_grow = FALSE;
1327789Sahrens 		}
1328789Sahrens 
13291544Seschrock 		if (arc_eviction_list != NULL)
13301544Seschrock 			arc_do_user_evicts();
13311544Seschrock 
1332789Sahrens 		/* block until needed, or one second, whichever is shorter */
1333789Sahrens 		CALLB_CPR_SAFE_BEGIN(&cpr);
1334789Sahrens 		(void) cv_timedwait(&arc_reclaim_thr_cv,
1335789Sahrens 		    &arc_reclaim_thr_lock, (lbolt + hz));
1336789Sahrens 		CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
1337789Sahrens 	}
1338789Sahrens 
1339789Sahrens 	arc_thread_exit = 0;
1340789Sahrens 	cv_broadcast(&arc_reclaim_thr_cv);
1341789Sahrens 	CALLB_CPR_EXIT(&cpr);		/* drops arc_reclaim_thr_lock */
1342789Sahrens 	thread_exit();
1343789Sahrens }
1344789Sahrens 
13451544Seschrock /*
13461544Seschrock  * Adapt arc info given the number of bytes we are trying to add and
13471544Seschrock  * the state that we are comming from.  This function is only called
13481544Seschrock  * when we are adding new content to the cache.
13491544Seschrock  */
1350789Sahrens static void
13511544Seschrock arc_adapt(int bytes, arc_state_t *state)
1352789Sahrens {
13531544Seschrock 	int mult;
13541544Seschrock 
13551544Seschrock 	ASSERT(bytes > 0);
1356789Sahrens 	/*
13571544Seschrock 	 * Adapt the target size of the MRU list:
13581544Seschrock 	 *	- if we just hit in the MRU ghost list, then increase
13591544Seschrock 	 *	  the target size of the MRU list.
13601544Seschrock 	 *	- if we just hit in the MFU ghost list, then increase
13611544Seschrock 	 *	  the target size of the MFU list by decreasing the
13621544Seschrock 	 *	  target size of the MRU list.
1363789Sahrens 	 */
13641544Seschrock 	if (state == arc.mru_ghost) {
13651544Seschrock 		mult = ((arc.mru_ghost->size >= arc.mfu_ghost->size) ?
13661544Seschrock 		    1 : (arc.mfu_ghost->size/arc.mru_ghost->size));
13671544Seschrock 
13681544Seschrock 		arc.p = MIN(arc.c, arc.p + bytes * mult);
13691544Seschrock 	} else if (state == arc.mfu_ghost) {
13701544Seschrock 		mult = ((arc.mfu_ghost->size >= arc.mru_ghost->size) ?
13711544Seschrock 		    1 : (arc.mru_ghost->size/arc.mfu_ghost->size));
13721544Seschrock 
13731544Seschrock 		arc.p = MAX(0, (int64_t)arc.p - bytes * mult);
13741544Seschrock 	}
13751544Seschrock 	ASSERT((int64_t)arc.p >= 0);
1376789Sahrens 
1377789Sahrens 	if (arc_reclaim_needed()) {
1378789Sahrens 		cv_signal(&arc_reclaim_thr_cv);
1379789Sahrens 		return;
1380789Sahrens 	}
1381789Sahrens 
1382789Sahrens 	if (arc.no_grow)
1383789Sahrens 		return;
1384789Sahrens 
13851544Seschrock 	if (arc.c >= arc.c_max)
13861544Seschrock 		return;
13871544Seschrock 
1388789Sahrens 	/*
13891544Seschrock 	 * If we're within (2 * maxblocksize) bytes of the target
13901544Seschrock 	 * cache size, increment the target cache size
1391789Sahrens 	 */
13921544Seschrock 	if (arc.size > arc.c - (2ULL << SPA_MAXBLOCKSHIFT)) {
13931544Seschrock 		atomic_add_64(&arc.c, (int64_t)bytes);
1394789Sahrens 		if (arc.c > arc.c_max)
1395789Sahrens 			arc.c = arc.c_max;
13961544Seschrock 		else if (state == arc.anon)
13971544Seschrock 			atomic_add_64(&arc.p, (int64_t)bytes);
13981544Seschrock 		if (arc.p > arc.c)
13991544Seschrock 			arc.p = arc.c;
1400789Sahrens 	}
14011544Seschrock 	ASSERT((int64_t)arc.p >= 0);
1402789Sahrens }
1403789Sahrens 
1404789Sahrens /*
14051544Seschrock  * Check if the cache has reached its limits and eviction is required
14061544Seschrock  * prior to insert.
1407789Sahrens  */
1408789Sahrens static int
1409789Sahrens arc_evict_needed()
1410789Sahrens {
1411789Sahrens 	if (arc_reclaim_needed())
1412789Sahrens 		return (1);
1413789Sahrens 
14141544Seschrock 	return (arc.size > arc.c);
1415789Sahrens }
1416789Sahrens 
1417789Sahrens /*
1418*2688Smaybee  * The buffer, supplied as the first argument, needs a data block.
1419*2688Smaybee  * So, if we are at cache max, determine which cache should be victimized.
1420*2688Smaybee  * We have the following cases:
1421789Sahrens  *
14221544Seschrock  * 1. Insert for MRU, p > sizeof(arc.anon + arc.mru) ->
1423789Sahrens  * In this situation if we're out of space, but the resident size of the MFU is
1424789Sahrens  * under the limit, victimize the MFU cache to satisfy this insertion request.
1425789Sahrens  *
14261544Seschrock  * 2. Insert for MRU, p <= sizeof(arc.anon + arc.mru) ->
1427789Sahrens  * Here, we've used up all of the available space for the MRU, so we need to
1428789Sahrens  * evict from our own cache instead.  Evict from the set of resident MRU
1429789Sahrens  * entries.
1430789Sahrens  *
14311544Seschrock  * 3. Insert for MFU (c - p) > sizeof(arc.mfu) ->
1432789Sahrens  * c minus p represents the MFU space in the cache, since p is the size of the
1433789Sahrens  * cache that is dedicated to the MRU.  In this situation there's still space on
1434789Sahrens  * the MFU side, so the MRU side needs to be victimized.
1435789Sahrens  *
14361544Seschrock  * 4. Insert for MFU (c - p) < sizeof(arc.mfu) ->
1437789Sahrens  * MFU's resident set is consuming more space than it has been allotted.  In
1438789Sahrens  * this situation, we must victimize our own cache, the MFU, for this insertion.
1439789Sahrens  */
1440789Sahrens static void
1441*2688Smaybee arc_get_data_buf(arc_buf_t *buf)
1442789Sahrens {
1443*2688Smaybee 	arc_state_t	*state = buf->b_hdr->b_state;
1444*2688Smaybee 	uint64_t	size = buf->b_hdr->b_size;
1445*2688Smaybee 
1446*2688Smaybee 	arc_adapt(size, state);
1447789Sahrens 
1448*2688Smaybee 	/*
1449*2688Smaybee 	 * We have not yet reached cache maximum size,
1450*2688Smaybee 	 * just allocate a new buffer.
1451*2688Smaybee 	 */
1452*2688Smaybee 	if (!arc_evict_needed()) {
1453*2688Smaybee 		buf->b_data = zio_buf_alloc(size);
1454*2688Smaybee 		atomic_add_64(&arc.size, size);
1455*2688Smaybee 		goto out;
1456*2688Smaybee 	}
1457*2688Smaybee 
1458*2688Smaybee 	/*
1459*2688Smaybee 	 * If we are prefetching from the mfu ghost list, this buffer
1460*2688Smaybee 	 * will end up on the mru list; so steal space from there.
1461*2688Smaybee 	 */
1462*2688Smaybee 	if (state == arc.mfu_ghost)
1463*2688Smaybee 		state = buf->b_hdr->b_flags & ARC_PREFETCH ? arc.mru : arc.mfu;
1464*2688Smaybee 	else if (state == arc.mru_ghost)
1465*2688Smaybee 		state = arc.mru;
1466789Sahrens 
1467*2688Smaybee 	if (state == arc.mru || state == arc.anon) {
1468*2688Smaybee 		uint64_t mru_used = arc.anon->size + arc.mru->size;
1469*2688Smaybee 		state = (arc.p > mru_used) ? arc.mfu : arc.mru;
1470789Sahrens 	} else {
1471*2688Smaybee 		/* MFU cases */
1472*2688Smaybee 		uint64_t mfu_space = arc.c - arc.p;
1473*2688Smaybee 		state =  (mfu_space > arc.mfu->size) ? arc.mru : arc.mfu;
1474*2688Smaybee 	}
1475*2688Smaybee 	if ((buf->b_data = arc_evict(state, size, TRUE)) == NULL) {
1476*2688Smaybee 		(void) arc_evict(state, size, FALSE);
1477*2688Smaybee 		buf->b_data = zio_buf_alloc(size);
1478*2688Smaybee 		atomic_add_64(&arc.size, size);
1479*2688Smaybee 		atomic_add_64(&arc.recycle_miss, 1);
1480*2688Smaybee 		if (arc.size > arc.c)
1481*2688Smaybee 			arc_adjust();
1482*2688Smaybee 	}
1483*2688Smaybee 	ASSERT(buf->b_data != NULL);
1484*2688Smaybee out:
1485*2688Smaybee 	/*
1486*2688Smaybee 	 * Update the state size.  Note that ghost states have a
1487*2688Smaybee 	 * "ghost size" and so don't need to be updated.
1488*2688Smaybee 	 */
1489*2688Smaybee 	if (!GHOST_STATE(buf->b_hdr->b_state)) {
1490*2688Smaybee 		arc_buf_hdr_t *hdr = buf->b_hdr;
1491*2688Smaybee 
1492*2688Smaybee 		atomic_add_64(&hdr->b_state->size, size);
1493*2688Smaybee 		if (list_link_active(&hdr->b_arc_node)) {
1494*2688Smaybee 			ASSERT(refcount_is_zero(&hdr->b_refcnt));
1495*2688Smaybee 			atomic_add_64(&hdr->b_state->lsize, size);
1496789Sahrens 		}
1497789Sahrens 	}
1498789Sahrens }
1499789Sahrens 
1500789Sahrens /*
1501789Sahrens  * This routine is called whenever a buffer is accessed.
15021544Seschrock  * NOTE: the hash lock is dropped in this function.
1503789Sahrens  */
1504789Sahrens static void
1505*2688Smaybee arc_access(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
1506789Sahrens {
1507789Sahrens 	ASSERT(MUTEX_HELD(hash_lock));
1508789Sahrens 
1509789Sahrens 	if (buf->b_state == arc.anon) {
1510789Sahrens 		/*
1511789Sahrens 		 * This buffer is not in the cache, and does not
1512789Sahrens 		 * appear in our "ghost" list.  Add the new buffer
1513789Sahrens 		 * to the MRU state.
1514789Sahrens 		 */
1515789Sahrens 
1516789Sahrens 		ASSERT(buf->b_arc_access == 0);
1517789Sahrens 		buf->b_arc_access = lbolt;
15181544Seschrock 		DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
15191544Seschrock 		arc_change_state(arc.mru, buf, hash_lock);
1520789Sahrens 
15211544Seschrock 	} else if (buf->b_state == arc.mru) {
1522789Sahrens 		/*
15232391Smaybee 		 * If this buffer is here because of a prefetch, then either:
15242391Smaybee 		 * - clear the flag if this is a "referencing" read
15252391Smaybee 		 *   (any subsequent access will bump this into the MFU state).
15262391Smaybee 		 * or
15272391Smaybee 		 * - move the buffer to the head of the list if this is
15282391Smaybee 		 *   another prefetch (to make it less likely to be evicted).
1529789Sahrens 		 */
1530789Sahrens 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
15312391Smaybee 			if (refcount_count(&buf->b_refcnt) == 0) {
15322391Smaybee 				ASSERT(list_link_active(&buf->b_arc_node));
15332391Smaybee 				mutex_enter(&arc.mru->mtx);
15342391Smaybee 				list_remove(&arc.mru->list, buf);
15352391Smaybee 				list_insert_head(&arc.mru->list, buf);
15362391Smaybee 				mutex_exit(&arc.mru->mtx);
15372391Smaybee 			} else {
15382391Smaybee 				buf->b_flags &= ~ARC_PREFETCH;
15392391Smaybee 				atomic_add_64(&arc.mru->hits, 1);
15402391Smaybee 			}
15412391Smaybee 			buf->b_arc_access = lbolt;
1542789Sahrens 			return;
1543789Sahrens 		}
1544789Sahrens 
1545789Sahrens 		/*
1546789Sahrens 		 * This buffer has been "accessed" only once so far,
1547789Sahrens 		 * but it is still in the cache. Move it to the MFU
1548789Sahrens 		 * state.
1549789Sahrens 		 */
1550789Sahrens 		if (lbolt > buf->b_arc_access + ARC_MINTIME) {
1551789Sahrens 			/*
1552789Sahrens 			 * More than 125ms have passed since we
1553789Sahrens 			 * instantiated this buffer.  Move it to the
1554789Sahrens 			 * most frequently used state.
1555789Sahrens 			 */
1556789Sahrens 			buf->b_arc_access = lbolt;
15571544Seschrock 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
15581544Seschrock 			arc_change_state(arc.mfu, buf, hash_lock);
1559789Sahrens 		}
15601544Seschrock 		atomic_add_64(&arc.mru->hits, 1);
15611544Seschrock 	} else if (buf->b_state == arc.mru_ghost) {
1562789Sahrens 		arc_state_t	*new_state;
1563789Sahrens 		/*
1564789Sahrens 		 * This buffer has been "accessed" recently, but
1565789Sahrens 		 * was evicted from the cache.  Move it to the
1566789Sahrens 		 * MFU state.
1567789Sahrens 		 */
1568789Sahrens 
1569789Sahrens 		if (buf->b_flags & ARC_PREFETCH) {
15701544Seschrock 			new_state = arc.mru;
15712391Smaybee 			if (refcount_count(&buf->b_refcnt) > 0)
15722391Smaybee 				buf->b_flags &= ~ARC_PREFETCH;
15731544Seschrock 			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
1574789Sahrens 		} else {
15751544Seschrock 			new_state = arc.mfu;
15761544Seschrock 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
1577789Sahrens 		}
1578789Sahrens 
1579789Sahrens 		buf->b_arc_access = lbolt;
1580789Sahrens 		arc_change_state(new_state, buf, hash_lock);
1581789Sahrens 
15821544Seschrock 		atomic_add_64(&arc.mru_ghost->hits, 1);
15831544Seschrock 	} else if (buf->b_state == arc.mfu) {
1584789Sahrens 		/*
1585789Sahrens 		 * This buffer has been accessed more than once and is
1586789Sahrens 		 * still in the cache.  Keep it in the MFU state.
1587789Sahrens 		 *
15882391Smaybee 		 * NOTE: an add_reference() that occurred when we did
15892391Smaybee 		 * the arc_read() will have kicked this off the list.
15902391Smaybee 		 * If it was a prefetch, we will explicitly move it to
15912391Smaybee 		 * the head of the list now.
1592789Sahrens 		 */
15932391Smaybee 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
15942391Smaybee 			ASSERT(refcount_count(&buf->b_refcnt) == 0);
15952391Smaybee 			ASSERT(list_link_active(&buf->b_arc_node));
15962391Smaybee 			mutex_enter(&arc.mfu->mtx);
15972391Smaybee 			list_remove(&arc.mfu->list, buf);
15982391Smaybee 			list_insert_head(&arc.mfu->list, buf);
15992391Smaybee 			mutex_exit(&arc.mfu->mtx);
16002391Smaybee 		}
16011544Seschrock 		atomic_add_64(&arc.mfu->hits, 1);
16022391Smaybee 		buf->b_arc_access = lbolt;
16031544Seschrock 	} else if (buf->b_state == arc.mfu_ghost) {
16042391Smaybee 		arc_state_t	*new_state = arc.mfu;
1605789Sahrens 		/*
1606789Sahrens 		 * This buffer has been accessed more than once but has
1607789Sahrens 		 * been evicted from the cache.  Move it back to the
1608789Sahrens 		 * MFU state.
1609789Sahrens 		 */
1610789Sahrens 
16112391Smaybee 		if (buf->b_flags & ARC_PREFETCH) {
16122391Smaybee 			/*
16132391Smaybee 			 * This is a prefetch access...
16142391Smaybee 			 * move this block back to the MRU state.
16152391Smaybee 			 */
16162391Smaybee 			ASSERT3U(refcount_count(&buf->b_refcnt), ==, 0);
16172391Smaybee 			new_state = arc.mru;
16182391Smaybee 		}
16192391Smaybee 
1620789Sahrens 		buf->b_arc_access = lbolt;
16211544Seschrock 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
16222391Smaybee 		arc_change_state(new_state, buf, hash_lock);
1623789Sahrens 
16241544Seschrock 		atomic_add_64(&arc.mfu_ghost->hits, 1);
1625789Sahrens 	} else {
1626789Sahrens 		ASSERT(!"invalid arc state");
1627789Sahrens 	}
1628789Sahrens }
1629789Sahrens 
1630789Sahrens /* a generic arc_done_func_t which you can use */
1631789Sahrens /* ARGSUSED */
1632789Sahrens void
1633789Sahrens arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
1634789Sahrens {
1635789Sahrens 	bcopy(buf->b_data, arg, buf->b_hdr->b_size);
16361544Seschrock 	VERIFY(arc_buf_remove_ref(buf, arg) == 1);
1637789Sahrens }
1638789Sahrens 
1639789Sahrens /* a generic arc_done_func_t which you can use */
1640789Sahrens void
1641789Sahrens arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
1642789Sahrens {
1643789Sahrens 	arc_buf_t **bufp = arg;
1644789Sahrens 	if (zio && zio->io_error) {
16451544Seschrock 		VERIFY(arc_buf_remove_ref(buf, arg) == 1);
1646789Sahrens 		*bufp = NULL;
1647789Sahrens 	} else {
1648789Sahrens 		*bufp = buf;
1649789Sahrens 	}
1650789Sahrens }
1651789Sahrens 
1652789Sahrens static void
1653789Sahrens arc_read_done(zio_t *zio)
1654789Sahrens {
16551589Smaybee 	arc_buf_hdr_t	*hdr, *found;
1656789Sahrens 	arc_buf_t	*buf;
1657789Sahrens 	arc_buf_t	*abuf;	/* buffer we're assigning to callback */
1658789Sahrens 	kmutex_t	*hash_lock;
1659789Sahrens 	arc_callback_t	*callback_list, *acb;
1660789Sahrens 	int		freeable = FALSE;
1661789Sahrens 
1662789Sahrens 	buf = zio->io_private;
1663789Sahrens 	hdr = buf->b_hdr;
1664789Sahrens 
16651589Smaybee 	/*
16661589Smaybee 	 * The hdr was inserted into hash-table and removed from lists
16671589Smaybee 	 * prior to starting I/O.  We should find this header, since
16681589Smaybee 	 * it's in the hash table, and it should be legit since it's
16691589Smaybee 	 * not possible to evict it during the I/O.  The only possible
16701589Smaybee 	 * reason for it not to be found is if we were freed during the
16711589Smaybee 	 * read.
16721589Smaybee 	 */
16731589Smaybee 	found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth,
1674789Sahrens 		    &hash_lock);
1675789Sahrens 
16761589Smaybee 	ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
16771589Smaybee 	    (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))));
1678789Sahrens 
1679789Sahrens 	/* byteswap if necessary */
1680789Sahrens 	callback_list = hdr->b_acb;
1681789Sahrens 	ASSERT(callback_list != NULL);
1682789Sahrens 	if (BP_SHOULD_BYTESWAP(zio->io_bp) && callback_list->acb_byteswap)
1683789Sahrens 		callback_list->acb_byteswap(buf->b_data, hdr->b_size);
1684789Sahrens 
1685789Sahrens 	/* create copies of the data buffer for the callers */
1686789Sahrens 	abuf = buf;
1687789Sahrens 	for (acb = callback_list; acb; acb = acb->acb_next) {
1688789Sahrens 		if (acb->acb_done) {
1689*2688Smaybee 			if (abuf == NULL)
1690*2688Smaybee 				abuf = arc_buf_clone(buf);
1691789Sahrens 			acb->acb_buf = abuf;
1692789Sahrens 			abuf = NULL;
1693789Sahrens 		}
1694789Sahrens 	}
1695789Sahrens 	hdr->b_acb = NULL;
1696789Sahrens 	hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
16971544Seschrock 	ASSERT(!HDR_BUF_AVAILABLE(hdr));
16981544Seschrock 	if (abuf == buf)
16991544Seschrock 		hdr->b_flags |= ARC_BUF_AVAILABLE;
1700789Sahrens 
1701789Sahrens 	ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
1702789Sahrens 
1703789Sahrens 	if (zio->io_error != 0) {
1704789Sahrens 		hdr->b_flags |= ARC_IO_ERROR;
1705789Sahrens 		if (hdr->b_state != arc.anon)
1706789Sahrens 			arc_change_state(arc.anon, hdr, hash_lock);
17071544Seschrock 		if (HDR_IN_HASH_TABLE(hdr))
17081544Seschrock 			buf_hash_remove(hdr);
1709789Sahrens 		freeable = refcount_is_zero(&hdr->b_refcnt);
17102391Smaybee 		/* convert checksum errors into IO errors */
17111544Seschrock 		if (zio->io_error == ECKSUM)
17121544Seschrock 			zio->io_error = EIO;
1713789Sahrens 	}
1714789Sahrens 
17151544Seschrock 	/*
17162391Smaybee 	 * Broadcast before we drop the hash_lock to avoid the possibility
17172391Smaybee 	 * that the hdr (and hence the cv) might be freed before we get to
17182391Smaybee 	 * the cv_broadcast().
17191544Seschrock 	 */
17201544Seschrock 	cv_broadcast(&hdr->b_cv);
17211544Seschrock 
17221589Smaybee 	if (hash_lock) {
1723789Sahrens 		/*
1724789Sahrens 		 * Only call arc_access on anonymous buffers.  This is because
1725789Sahrens 		 * if we've issued an I/O for an evicted buffer, we've already
1726789Sahrens 		 * called arc_access (to prevent any simultaneous readers from
1727789Sahrens 		 * getting confused).
1728789Sahrens 		 */
1729789Sahrens 		if (zio->io_error == 0 && hdr->b_state == arc.anon)
1730*2688Smaybee 			arc_access(hdr, hash_lock);
1731*2688Smaybee 		mutex_exit(hash_lock);
1732789Sahrens 	} else {
1733789Sahrens 		/*
1734789Sahrens 		 * This block was freed while we waited for the read to
1735789Sahrens 		 * complete.  It has been removed from the hash table and
1736789Sahrens 		 * moved to the anonymous state (so that it won't show up
1737789Sahrens 		 * in the cache).
1738789Sahrens 		 */
1739789Sahrens 		ASSERT3P(hdr->b_state, ==, arc.anon);
1740789Sahrens 		freeable = refcount_is_zero(&hdr->b_refcnt);
1741789Sahrens 	}
1742789Sahrens 
1743789Sahrens 	/* execute each callback and free its structure */
1744789Sahrens 	while ((acb = callback_list) != NULL) {
1745789Sahrens 		if (acb->acb_done)
1746789Sahrens 			acb->acb_done(zio, acb->acb_buf, acb->acb_private);
1747789Sahrens 
1748789Sahrens 		if (acb->acb_zio_dummy != NULL) {
1749789Sahrens 			acb->acb_zio_dummy->io_error = zio->io_error;
1750789Sahrens 			zio_nowait(acb->acb_zio_dummy);
1751789Sahrens 		}
1752789Sahrens 
1753789Sahrens 		callback_list = acb->acb_next;
1754789Sahrens 		kmem_free(acb, sizeof (arc_callback_t));
1755789Sahrens 	}
1756789Sahrens 
1757789Sahrens 	if (freeable)
17581544Seschrock 		arc_hdr_destroy(hdr);
1759789Sahrens }
1760789Sahrens 
1761789Sahrens /*
1762789Sahrens  * "Read" the block block at the specified DVA (in bp) via the
1763789Sahrens  * cache.  If the block is found in the cache, invoke the provided
1764789Sahrens  * callback immediately and return.  Note that the `zio' parameter
1765789Sahrens  * in the callback will be NULL in this case, since no IO was
1766789Sahrens  * required.  If the block is not in the cache pass the read request
1767789Sahrens  * on to the spa with a substitute callback function, so that the
1768789Sahrens  * requested block will be added to the cache.
1769789Sahrens  *
1770789Sahrens  * If a read request arrives for a block that has a read in-progress,
1771789Sahrens  * either wait for the in-progress read to complete (and return the
1772789Sahrens  * results); or, if this is a read with a "done" func, add a record
1773789Sahrens  * to the read to invoke the "done" func when the read completes,
1774789Sahrens  * and return; or just return.
1775789Sahrens  *
1776789Sahrens  * arc_read_done() will invoke all the requested "done" functions
1777789Sahrens  * for readers of this block.
1778789Sahrens  */
1779789Sahrens int
1780789Sahrens arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_byteswap_func_t *swap,
1781789Sahrens     arc_done_func_t *done, void *private, int priority, int flags,
17822391Smaybee     uint32_t *arc_flags, zbookmark_t *zb)
1783789Sahrens {
1784789Sahrens 	arc_buf_hdr_t *hdr;
1785789Sahrens 	arc_buf_t *buf;
1786789Sahrens 	kmutex_t *hash_lock;
1787789Sahrens 	zio_t	*rzio;
1788789Sahrens 
1789789Sahrens top:
1790789Sahrens 	hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock);
17911544Seschrock 	if (hdr && hdr->b_datacnt > 0) {
1792789Sahrens 
17932391Smaybee 		*arc_flags |= ARC_CACHED;
17942391Smaybee 
1795789Sahrens 		if (HDR_IO_IN_PROGRESS(hdr)) {
17962391Smaybee 
17972391Smaybee 			if (*arc_flags & ARC_WAIT) {
17982391Smaybee 				cv_wait(&hdr->b_cv, hash_lock);
17992391Smaybee 				mutex_exit(hash_lock);
18002391Smaybee 				goto top;
18012391Smaybee 			}
18022391Smaybee 			ASSERT(*arc_flags & ARC_NOWAIT);
18032391Smaybee 
18042391Smaybee 			if (done) {
1805789Sahrens 				arc_callback_t	*acb = NULL;
1806789Sahrens 
1807789Sahrens 				acb = kmem_zalloc(sizeof (arc_callback_t),
1808789Sahrens 				    KM_SLEEP);
1809789Sahrens 				acb->acb_done = done;
1810789Sahrens 				acb->acb_private = private;
1811789Sahrens 				acb->acb_byteswap = swap;
1812789Sahrens 				if (pio != NULL)
1813789Sahrens 					acb->acb_zio_dummy = zio_null(pio,
1814789Sahrens 					    spa, NULL, NULL, flags);
1815789Sahrens 
1816789Sahrens 				ASSERT(acb->acb_done != NULL);
1817789Sahrens 				acb->acb_next = hdr->b_acb;
1818789Sahrens 				hdr->b_acb = acb;
1819789Sahrens 				add_reference(hdr, hash_lock, private);
1820789Sahrens 				mutex_exit(hash_lock);
1821789Sahrens 				return (0);
1822789Sahrens 			}
1823789Sahrens 			mutex_exit(hash_lock);
1824789Sahrens 			return (0);
1825789Sahrens 		}
1826789Sahrens 
18271544Seschrock 		ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu);
1828789Sahrens 
18291544Seschrock 		if (done) {
1830*2688Smaybee 			add_reference(hdr, hash_lock, private);
18311544Seschrock 			/*
18321544Seschrock 			 * If this block is already in use, create a new
18331544Seschrock 			 * copy of the data so that we will be guaranteed
18341544Seschrock 			 * that arc_release() will always succeed.
18351544Seschrock 			 */
18361544Seschrock 			buf = hdr->b_buf;
18371544Seschrock 			ASSERT(buf);
18381544Seschrock 			ASSERT(buf->b_data);
1839*2688Smaybee 			if (HDR_BUF_AVAILABLE(hdr)) {
18401544Seschrock 				ASSERT(buf->b_efunc == NULL);
18411544Seschrock 				hdr->b_flags &= ~ARC_BUF_AVAILABLE;
1842*2688Smaybee 			} else {
1843*2688Smaybee 				buf = arc_buf_clone(buf);
18441544Seschrock 			}
18452391Smaybee 		} else if (*arc_flags & ARC_PREFETCH &&
18462391Smaybee 		    refcount_count(&hdr->b_refcnt) == 0) {
18472391Smaybee 			hdr->b_flags |= ARC_PREFETCH;
1848789Sahrens 		}
1849789Sahrens 		DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1850*2688Smaybee 		arc_access(hdr, hash_lock);
1851*2688Smaybee 		mutex_exit(hash_lock);
1852789Sahrens 		atomic_add_64(&arc.hits, 1);
1853789Sahrens 		if (done)
1854789Sahrens 			done(NULL, buf, private);
1855789Sahrens 	} else {
1856789Sahrens 		uint64_t size = BP_GET_LSIZE(bp);
1857789Sahrens 		arc_callback_t	*acb;
1858789Sahrens 
1859789Sahrens 		if (hdr == NULL) {
1860789Sahrens 			/* this block is not in the cache */
1861789Sahrens 			arc_buf_hdr_t	*exists;
1862789Sahrens 
1863789Sahrens 			buf = arc_buf_alloc(spa, size, private);
1864789Sahrens 			hdr = buf->b_hdr;
1865789Sahrens 			hdr->b_dva = *BP_IDENTITY(bp);
1866789Sahrens 			hdr->b_birth = bp->blk_birth;
1867789Sahrens 			hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
1868789Sahrens 			exists = buf_hash_insert(hdr, &hash_lock);
1869789Sahrens 			if (exists) {
1870789Sahrens 				/* somebody beat us to the hash insert */
1871789Sahrens 				mutex_exit(hash_lock);
1872789Sahrens 				bzero(&hdr->b_dva, sizeof (dva_t));
1873789Sahrens 				hdr->b_birth = 0;
1874789Sahrens 				hdr->b_cksum0 = 0;
18751544Seschrock 				(void) arc_buf_remove_ref(buf, private);
1876789Sahrens 				goto top; /* restart the IO request */
1877789Sahrens 			}
18782391Smaybee 			/* if this is a prefetch, we don't have a reference */
18792391Smaybee 			if (*arc_flags & ARC_PREFETCH) {
18802391Smaybee 				(void) remove_reference(hdr, hash_lock,
18812391Smaybee 				    private);
18822391Smaybee 				hdr->b_flags |= ARC_PREFETCH;
18832391Smaybee 			}
18842391Smaybee 			if (BP_GET_LEVEL(bp) > 0)
18852391Smaybee 				hdr->b_flags |= ARC_INDIRECT;
1886789Sahrens 		} else {
1887789Sahrens 			/* this block is in the ghost cache */
18881544Seschrock 			ASSERT(GHOST_STATE(hdr->b_state));
18891544Seschrock 			ASSERT(!HDR_IO_IN_PROGRESS(hdr));
18902391Smaybee 			ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 0);
18912391Smaybee 			ASSERT(hdr->b_buf == NULL);
1892789Sahrens 
18932391Smaybee 			/* if this is a prefetch, we don't have a reference */
18942391Smaybee 			if (*arc_flags & ARC_PREFETCH)
18952391Smaybee 				hdr->b_flags |= ARC_PREFETCH;
18962391Smaybee 			else
18972391Smaybee 				add_reference(hdr, hash_lock, private);
1898789Sahrens 			buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
18991544Seschrock 			buf->b_hdr = hdr;
1900*2688Smaybee 			buf->b_data = NULL;
19011544Seschrock 			buf->b_efunc = NULL;
19021544Seschrock 			buf->b_private = NULL;
19031544Seschrock 			buf->b_next = NULL;
19041544Seschrock 			hdr->b_buf = buf;
1905*2688Smaybee 			arc_get_data_buf(buf);
19061544Seschrock 			ASSERT(hdr->b_datacnt == 0);
19071544Seschrock 			hdr->b_datacnt = 1;
19082391Smaybee 
1909789Sahrens 		}
1910789Sahrens 
1911789Sahrens 		acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
1912789Sahrens 		acb->acb_done = done;
1913789Sahrens 		acb->acb_private = private;
1914789Sahrens 		acb->acb_byteswap = swap;
1915789Sahrens 
1916789Sahrens 		ASSERT(hdr->b_acb == NULL);
1917789Sahrens 		hdr->b_acb = acb;
1918789Sahrens 		hdr->b_flags |= ARC_IO_IN_PROGRESS;
1919789Sahrens 
1920789Sahrens 		/*
1921789Sahrens 		 * If the buffer has been evicted, migrate it to a present state
1922789Sahrens 		 * before issuing the I/O.  Once we drop the hash-table lock,
1923789Sahrens 		 * the header will be marked as I/O in progress and have an
1924789Sahrens 		 * attached buffer.  At this point, anybody who finds this
1925789Sahrens 		 * buffer ought to notice that it's legit but has a pending I/O.
1926789Sahrens 		 */
1927789Sahrens 
19281544Seschrock 		if (GHOST_STATE(hdr->b_state))
1929*2688Smaybee 			arc_access(hdr, hash_lock);
1930*2688Smaybee 		mutex_exit(hash_lock);
1931789Sahrens 
1932789Sahrens 		ASSERT3U(hdr->b_size, ==, size);
19331596Sahrens 		DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size,
19341596Sahrens 		    zbookmark_t *, zb);
1935789Sahrens 		atomic_add_64(&arc.misses, 1);
19361544Seschrock 
1937789Sahrens 		rzio = zio_read(pio, spa, bp, buf->b_data, size,
19381544Seschrock 		    arc_read_done, buf, priority, flags, zb);
1939789Sahrens 
19402391Smaybee 		if (*arc_flags & ARC_WAIT)
1941789Sahrens 			return (zio_wait(rzio));
1942789Sahrens 
19432391Smaybee 		ASSERT(*arc_flags & ARC_NOWAIT);
1944789Sahrens 		zio_nowait(rzio);
1945789Sahrens 	}
1946789Sahrens 	return (0);
1947789Sahrens }
1948789Sahrens 
1949789Sahrens /*
1950789Sahrens  * arc_read() variant to support pool traversal.  If the block is already
1951789Sahrens  * in the ARC, make a copy of it; otherwise, the caller will do the I/O.
1952789Sahrens  * The idea is that we don't want pool traversal filling up memory, but
1953789Sahrens  * if the ARC already has the data anyway, we shouldn't pay for the I/O.
1954789Sahrens  */
1955789Sahrens int
1956789Sahrens arc_tryread(spa_t *spa, blkptr_t *bp, void *data)
1957789Sahrens {
1958789Sahrens 	arc_buf_hdr_t *hdr;
1959789Sahrens 	kmutex_t *hash_mtx;
1960789Sahrens 	int rc = 0;
1961789Sahrens 
1962789Sahrens 	hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx);
1963789Sahrens 
19641544Seschrock 	if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) {
19651544Seschrock 		arc_buf_t *buf = hdr->b_buf;
19661544Seschrock 
19671544Seschrock 		ASSERT(buf);
19681544Seschrock 		while (buf->b_data == NULL) {
19691544Seschrock 			buf = buf->b_next;
19701544Seschrock 			ASSERT(buf);
19711544Seschrock 		}
19721544Seschrock 		bcopy(buf->b_data, data, hdr->b_size);
19731544Seschrock 	} else {
1974789Sahrens 		rc = ENOENT;
19751544Seschrock 	}
1976789Sahrens 
1977789Sahrens 	if (hash_mtx)
1978789Sahrens 		mutex_exit(hash_mtx);
1979789Sahrens 
1980789Sahrens 	return (rc);
1981789Sahrens }
1982789Sahrens 
19831544Seschrock void
19841544Seschrock arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
19851544Seschrock {
19861544Seschrock 	ASSERT(buf->b_hdr != NULL);
19871544Seschrock 	ASSERT(buf->b_hdr->b_state != arc.anon);
19881544Seschrock 	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
19891544Seschrock 	buf->b_efunc = func;
19901544Seschrock 	buf->b_private = private;
19911544Seschrock }
19921544Seschrock 
19931544Seschrock /*
19941544Seschrock  * This is used by the DMU to let the ARC know that a buffer is
19951544Seschrock  * being evicted, so the ARC should clean up.  If this arc buf
19961544Seschrock  * is not yet in the evicted state, it will be put there.
19971544Seschrock  */
19981544Seschrock int
19991544Seschrock arc_buf_evict(arc_buf_t *buf)
20001544Seschrock {
20011544Seschrock 	arc_buf_hdr_t *hdr;
20021544Seschrock 	kmutex_t *hash_lock;
20031544Seschrock 	arc_buf_t **bufp;
20041544Seschrock 
20051544Seschrock 	mutex_enter(&arc_eviction_mtx);
20061544Seschrock 	hdr = buf->b_hdr;
20071544Seschrock 	if (hdr == NULL) {
20081544Seschrock 		/*
20091544Seschrock 		 * We are in arc_do_user_evicts().
20101544Seschrock 		 * NOTE: We can't be in arc_buf_add_ref() because
20111544Seschrock 		 * that would violate the interface rules.
20121544Seschrock 		 */
20131544Seschrock 		ASSERT(buf->b_data == NULL);
20141544Seschrock 		mutex_exit(&arc_eviction_mtx);
20151544Seschrock 		return (0);
20161544Seschrock 	} else if (buf->b_data == NULL) {
20171819Smaybee 		arc_buf_t copy = *buf; /* structure assignment */
20181544Seschrock 		/*
20191819Smaybee 		 * We are on the eviction list.  Process this buffer
20201819Smaybee 		 * now but let arc_do_user_evicts() do the reaping.
20211544Seschrock 		 */
20221819Smaybee 		buf->b_efunc = NULL;
20231819Smaybee 		buf->b_hdr = NULL;
20241544Seschrock 		mutex_exit(&arc_eviction_mtx);
20251819Smaybee 		VERIFY(copy.b_efunc(&copy) == 0);
20261819Smaybee 		return (1);
20271544Seschrock 	} else {
20281544Seschrock 		/*
20291544Seschrock 		 * Prevent a race with arc_evict()
20301544Seschrock 		 */
20311544Seschrock 		ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
20321544Seschrock 		buf->b_hdr = NULL;
20331544Seschrock 	}
20341544Seschrock 	mutex_exit(&arc_eviction_mtx);
20351544Seschrock 
20361544Seschrock 	hash_lock = HDR_LOCK(hdr);
20371544Seschrock 	mutex_enter(hash_lock);
20381544Seschrock 
20391544Seschrock 	ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu);
20401544Seschrock 
20411544Seschrock 	/*
20421544Seschrock 	 * Pull this buffer off of the hdr
20431544Seschrock 	 */
20441544Seschrock 	bufp = &hdr->b_buf;
20451544Seschrock 	while (*bufp != buf)
20461544Seschrock 		bufp = &(*bufp)->b_next;
20471544Seschrock 	*bufp = buf->b_next;
20481544Seschrock 
20491544Seschrock 	ASSERT(buf->b_data != NULL);
20501544Seschrock 	buf->b_hdr = hdr;
2051*2688Smaybee 	arc_buf_destroy(buf, FALSE, FALSE);
20521544Seschrock 
20531544Seschrock 	if (hdr->b_datacnt == 0) {
20541544Seschrock 		arc_state_t *old_state = hdr->b_state;
20551544Seschrock 		arc_state_t *evicted_state;
20561544Seschrock 
20571544Seschrock 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
20581544Seschrock 
20591544Seschrock 		evicted_state =
20601544Seschrock 		    (old_state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost;
20611544Seschrock 
20621544Seschrock 		mutex_enter(&old_state->mtx);
20631544Seschrock 		mutex_enter(&evicted_state->mtx);
20641544Seschrock 
20651544Seschrock 		arc_change_state(evicted_state, hdr, hash_lock);
20661544Seschrock 		ASSERT(HDR_IN_HASH_TABLE(hdr));
20671544Seschrock 		hdr->b_flags = ARC_IN_HASH_TABLE;
20681544Seschrock 
20691544Seschrock 		mutex_exit(&evicted_state->mtx);
20701544Seschrock 		mutex_exit(&old_state->mtx);
20711544Seschrock 	}
20721544Seschrock 	mutex_exit(hash_lock);
20731819Smaybee 
20741544Seschrock 	VERIFY(buf->b_efunc(buf) == 0);
20751544Seschrock 	buf->b_efunc = NULL;
20761544Seschrock 	buf->b_private = NULL;
20771544Seschrock 	buf->b_hdr = NULL;
20781544Seschrock 	kmem_cache_free(buf_cache, buf);
20791544Seschrock 	return (1);
20801544Seschrock }
20811544Seschrock 
2082789Sahrens /*
2083789Sahrens  * Release this buffer from the cache.  This must be done
2084789Sahrens  * after a read and prior to modifying the buffer contents.
2085789Sahrens  * If the buffer has more than one reference, we must make
2086789Sahrens  * make a new hdr for the buffer.
2087789Sahrens  */
2088789Sahrens void
2089789Sahrens arc_release(arc_buf_t *buf, void *tag)
2090789Sahrens {
2091789Sahrens 	arc_buf_hdr_t *hdr = buf->b_hdr;
2092789Sahrens 	kmutex_t *hash_lock = HDR_LOCK(hdr);
2093789Sahrens 
2094789Sahrens 	/* this buffer is not on any list */
2095789Sahrens 	ASSERT(refcount_count(&hdr->b_refcnt) > 0);
2096789Sahrens 
2097789Sahrens 	if (hdr->b_state == arc.anon) {
2098789Sahrens 		/* this buffer is already released */
2099789Sahrens 		ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1);
2100789Sahrens 		ASSERT(BUF_EMPTY(hdr));
21011544Seschrock 		ASSERT(buf->b_efunc == NULL);
2102789Sahrens 		return;
2103789Sahrens 	}
2104789Sahrens 
2105789Sahrens 	mutex_enter(hash_lock);
2106789Sahrens 
21071544Seschrock 	/*
21081544Seschrock 	 * Do we have more than one buf?
21091544Seschrock 	 */
21101544Seschrock 	if (hdr->b_buf != buf || buf->b_next != NULL) {
2111789Sahrens 		arc_buf_hdr_t *nhdr;
2112789Sahrens 		arc_buf_t **bufp;
2113789Sahrens 		uint64_t blksz = hdr->b_size;
2114789Sahrens 		spa_t *spa = hdr->b_spa;
2115789Sahrens 
21161544Seschrock 		ASSERT(hdr->b_datacnt > 1);
2117789Sahrens 		/*
2118789Sahrens 		 * Pull the data off of this buf and attach it to
2119789Sahrens 		 * a new anonymous buf.
2120789Sahrens 		 */
21211544Seschrock 		(void) remove_reference(hdr, hash_lock, tag);
2122789Sahrens 		bufp = &hdr->b_buf;
21231544Seschrock 		while (*bufp != buf)
2124789Sahrens 			bufp = &(*bufp)->b_next;
2125789Sahrens 		*bufp = (*bufp)->b_next;
21261544Seschrock 
2127789Sahrens 		ASSERT3U(hdr->b_state->size, >=, hdr->b_size);
2128789Sahrens 		atomic_add_64(&hdr->b_state->size, -hdr->b_size);
21291544Seschrock 		if (refcount_is_zero(&hdr->b_refcnt)) {
21301544Seschrock 			ASSERT3U(hdr->b_state->lsize, >=, hdr->b_size);
21311544Seschrock 			atomic_add_64(&hdr->b_state->lsize, -hdr->b_size);
21321544Seschrock 		}
21331544Seschrock 		hdr->b_datacnt -= 1;
21341544Seschrock 
2135789Sahrens 		mutex_exit(hash_lock);
2136789Sahrens 
2137789Sahrens 		nhdr = kmem_cache_alloc(hdr_cache, KM_SLEEP);
2138789Sahrens 		nhdr->b_size = blksz;
2139789Sahrens 		nhdr->b_spa = spa;
2140789Sahrens 		nhdr->b_buf = buf;
2141789Sahrens 		nhdr->b_state = arc.anon;
2142789Sahrens 		nhdr->b_arc_access = 0;
2143789Sahrens 		nhdr->b_flags = 0;
21441544Seschrock 		nhdr->b_datacnt = 1;
2145789Sahrens 		buf->b_hdr = nhdr;
2146789Sahrens 		buf->b_next = NULL;
2147789Sahrens 		(void) refcount_add(&nhdr->b_refcnt, tag);
2148789Sahrens 		atomic_add_64(&arc.anon->size, blksz);
2149789Sahrens 
2150789Sahrens 		hdr = nhdr;
2151789Sahrens 	} else {
21521544Seschrock 		ASSERT(refcount_count(&hdr->b_refcnt) == 1);
2153789Sahrens 		ASSERT(!list_link_active(&hdr->b_arc_node));
2154789Sahrens 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
2155789Sahrens 		arc_change_state(arc.anon, hdr, hash_lock);
2156789Sahrens 		hdr->b_arc_access = 0;
2157789Sahrens 		mutex_exit(hash_lock);
2158789Sahrens 		bzero(&hdr->b_dva, sizeof (dva_t));
2159789Sahrens 		hdr->b_birth = 0;
2160789Sahrens 		hdr->b_cksum0 = 0;
2161789Sahrens 	}
21621544Seschrock 	buf->b_efunc = NULL;
21631544Seschrock 	buf->b_private = NULL;
2164789Sahrens }
2165789Sahrens 
2166789Sahrens int
2167789Sahrens arc_released(arc_buf_t *buf)
2168789Sahrens {
21691544Seschrock 	return (buf->b_data != NULL && buf->b_hdr->b_state == arc.anon);
21701544Seschrock }
21711544Seschrock 
21721544Seschrock int
21731544Seschrock arc_has_callback(arc_buf_t *buf)
21741544Seschrock {
21751544Seschrock 	return (buf->b_efunc != NULL);
2176789Sahrens }
2177789Sahrens 
21781544Seschrock #ifdef ZFS_DEBUG
21791544Seschrock int
21801544Seschrock arc_referenced(arc_buf_t *buf)
21811544Seschrock {
21821544Seschrock 	return (refcount_count(&buf->b_hdr->b_refcnt));
21831544Seschrock }
21841544Seschrock #endif
21851544Seschrock 
2186789Sahrens static void
2187789Sahrens arc_write_done(zio_t *zio)
2188789Sahrens {
2189789Sahrens 	arc_buf_t *buf;
2190789Sahrens 	arc_buf_hdr_t *hdr;
2191789Sahrens 	arc_callback_t *acb;
2192789Sahrens 
2193789Sahrens 	buf = zio->io_private;
2194789Sahrens 	hdr = buf->b_hdr;
2195789Sahrens 	acb = hdr->b_acb;
2196789Sahrens 	hdr->b_acb = NULL;
21971544Seschrock 	ASSERT(acb != NULL);
2198789Sahrens 
2199789Sahrens 	/* this buffer is on no lists and is not in the hash table */
2200789Sahrens 	ASSERT3P(hdr->b_state, ==, arc.anon);
2201789Sahrens 
2202789Sahrens 	hdr->b_dva = *BP_IDENTITY(zio->io_bp);
2203789Sahrens 	hdr->b_birth = zio->io_bp->blk_birth;
2204789Sahrens 	hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
22051544Seschrock 	/*
22061544Seschrock 	 * If the block to be written was all-zero, we may have
22071544Seschrock 	 * compressed it away.  In this case no write was performed
22081544Seschrock 	 * so there will be no dva/birth-date/checksum.  The buffer
22091544Seschrock 	 * must therefor remain anonymous (and uncached).
22101544Seschrock 	 */
2211789Sahrens 	if (!BUF_EMPTY(hdr)) {
2212789Sahrens 		arc_buf_hdr_t *exists;
2213789Sahrens 		kmutex_t *hash_lock;
2214789Sahrens 
2215789Sahrens 		exists = buf_hash_insert(hdr, &hash_lock);
2216789Sahrens 		if (exists) {
2217789Sahrens 			/*
2218789Sahrens 			 * This can only happen if we overwrite for
2219789Sahrens 			 * sync-to-convergence, because we remove
2220789Sahrens 			 * buffers from the hash table when we arc_free().
2221789Sahrens 			 */
2222789Sahrens 			ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig),
2223789Sahrens 			    BP_IDENTITY(zio->io_bp)));
2224789Sahrens 			ASSERT3U(zio->io_bp_orig.blk_birth, ==,
2225789Sahrens 			    zio->io_bp->blk_birth);
2226789Sahrens 
2227789Sahrens 			ASSERT(refcount_is_zero(&exists->b_refcnt));
2228789Sahrens 			arc_change_state(arc.anon, exists, hash_lock);
2229789Sahrens 			mutex_exit(hash_lock);
22301544Seschrock 			arc_hdr_destroy(exists);
2231789Sahrens 			exists = buf_hash_insert(hdr, &hash_lock);
2232789Sahrens 			ASSERT3P(exists, ==, NULL);
2233789Sahrens 		}
22341544Seschrock 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2235*2688Smaybee 		arc_access(hdr, hash_lock);
2236*2688Smaybee 		mutex_exit(hash_lock);
22371544Seschrock 	} else if (acb->acb_done == NULL) {
22381544Seschrock 		int destroy_hdr;
22391544Seschrock 		/*
22401544Seschrock 		 * This is an anonymous buffer with no user callback,
22411544Seschrock 		 * destroy it if there are no active references.
22421544Seschrock 		 */
22431544Seschrock 		mutex_enter(&arc_eviction_mtx);
22441544Seschrock 		destroy_hdr = refcount_is_zero(&hdr->b_refcnt);
22451544Seschrock 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
22461544Seschrock 		mutex_exit(&arc_eviction_mtx);
22471544Seschrock 		if (destroy_hdr)
22481544Seschrock 			arc_hdr_destroy(hdr);
22491544Seschrock 	} else {
22501544Seschrock 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2251789Sahrens 	}
22521544Seschrock 
22531544Seschrock 	if (acb->acb_done) {
2254789Sahrens 		ASSERT(!refcount_is_zero(&hdr->b_refcnt));
2255789Sahrens 		acb->acb_done(zio, buf, acb->acb_private);
2256789Sahrens 	}
2257789Sahrens 
22581544Seschrock 	kmem_free(acb, sizeof (arc_callback_t));
2259789Sahrens }
2260789Sahrens 
2261789Sahrens int
22621775Sbillm arc_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies,
2263789Sahrens     uint64_t txg, blkptr_t *bp, arc_buf_t *buf,
2264789Sahrens     arc_done_func_t *done, void *private, int priority, int flags,
22651544Seschrock     uint32_t arc_flags, zbookmark_t *zb)
2266789Sahrens {
2267789Sahrens 	arc_buf_hdr_t *hdr = buf->b_hdr;
2268789Sahrens 	arc_callback_t	*acb;
2269789Sahrens 	zio_t	*rzio;
2270789Sahrens 
2271789Sahrens 	/* this is a private buffer - no locking required */
2272789Sahrens 	ASSERT3P(hdr->b_state, ==, arc.anon);
2273789Sahrens 	ASSERT(BUF_EMPTY(hdr));
2274789Sahrens 	ASSERT(!HDR_IO_ERROR(hdr));
22752237Smaybee 	ASSERT((hdr->b_flags & ARC_IO_IN_PROGRESS) == 0);
22762237Smaybee 	ASSERT(hdr->b_acb == 0);
2277789Sahrens 	acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
2278789Sahrens 	acb->acb_done = done;
2279789Sahrens 	acb->acb_private = private;
2280789Sahrens 	acb->acb_byteswap = (arc_byteswap_func_t *)-1;
2281789Sahrens 	hdr->b_acb = acb;
22821544Seschrock 	hdr->b_flags |= ARC_IO_IN_PROGRESS;
22831775Sbillm 	rzio = zio_write(pio, spa, checksum, compress, ncopies, txg, bp,
22841544Seschrock 	    buf->b_data, hdr->b_size, arc_write_done, buf, priority, flags, zb);
2285789Sahrens 
2286789Sahrens 	if (arc_flags & ARC_WAIT)
2287789Sahrens 		return (zio_wait(rzio));
2288789Sahrens 
2289789Sahrens 	ASSERT(arc_flags & ARC_NOWAIT);
2290789Sahrens 	zio_nowait(rzio);
2291789Sahrens 
2292789Sahrens 	return (0);
2293789Sahrens }
2294789Sahrens 
2295789Sahrens int
2296789Sahrens arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
2297789Sahrens     zio_done_func_t *done, void *private, uint32_t arc_flags)
2298789Sahrens {
2299789Sahrens 	arc_buf_hdr_t *ab;
2300789Sahrens 	kmutex_t *hash_lock;
2301789Sahrens 	zio_t	*zio;
2302789Sahrens 
2303789Sahrens 	/*
2304789Sahrens 	 * If this buffer is in the cache, release it, so it
2305789Sahrens 	 * can be re-used.
2306789Sahrens 	 */
2307789Sahrens 	ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock);
2308789Sahrens 	if (ab != NULL) {
2309789Sahrens 		/*
2310789Sahrens 		 * The checksum of blocks to free is not always
2311789Sahrens 		 * preserved (eg. on the deadlist).  However, if it is
2312789Sahrens 		 * nonzero, it should match what we have in the cache.
2313789Sahrens 		 */
2314789Sahrens 		ASSERT(bp->blk_cksum.zc_word[0] == 0 ||
2315789Sahrens 		    ab->b_cksum0 == bp->blk_cksum.zc_word[0]);
23161990Smaybee 		if (ab->b_state != arc.anon)
23171990Smaybee 			arc_change_state(arc.anon, ab, hash_lock);
23182391Smaybee 		if (HDR_IO_IN_PROGRESS(ab)) {
23192391Smaybee 			/*
23202391Smaybee 			 * This should only happen when we prefetch.
23212391Smaybee 			 */
23222391Smaybee 			ASSERT(ab->b_flags & ARC_PREFETCH);
23232391Smaybee 			ASSERT3U(ab->b_datacnt, ==, 1);
23242391Smaybee 			ab->b_flags |= ARC_FREED_IN_READ;
23252391Smaybee 			if (HDR_IN_HASH_TABLE(ab))
23262391Smaybee 				buf_hash_remove(ab);
23272391Smaybee 			ab->b_arc_access = 0;
23282391Smaybee 			bzero(&ab->b_dva, sizeof (dva_t));
23292391Smaybee 			ab->b_birth = 0;
23302391Smaybee 			ab->b_cksum0 = 0;
23312391Smaybee 			ab->b_buf->b_efunc = NULL;
23322391Smaybee 			ab->b_buf->b_private = NULL;
23332391Smaybee 			mutex_exit(hash_lock);
23342391Smaybee 		} else if (refcount_is_zero(&ab->b_refcnt)) {
2335789Sahrens 			mutex_exit(hash_lock);
23361544Seschrock 			arc_hdr_destroy(ab);
2337789Sahrens 			atomic_add_64(&arc.deleted, 1);
2338789Sahrens 		} else {
23391589Smaybee 			/*
23402391Smaybee 			 * We still have an active reference on this
23412391Smaybee 			 * buffer.  This can happen, e.g., from
23422391Smaybee 			 * dbuf_unoverride().
23431589Smaybee 			 */
23442391Smaybee 			ASSERT(!HDR_IN_HASH_TABLE(ab));
2345789Sahrens 			ab->b_arc_access = 0;
2346789Sahrens 			bzero(&ab->b_dva, sizeof (dva_t));
2347789Sahrens 			ab->b_birth = 0;
2348789Sahrens 			ab->b_cksum0 = 0;
23491544Seschrock 			ab->b_buf->b_efunc = NULL;
23501544Seschrock 			ab->b_buf->b_private = NULL;
2351789Sahrens 			mutex_exit(hash_lock);
2352789Sahrens 		}
2353789Sahrens 	}
2354789Sahrens 
2355789Sahrens 	zio = zio_free(pio, spa, txg, bp, done, private);
2356789Sahrens 
2357789Sahrens 	if (arc_flags & ARC_WAIT)
2358789Sahrens 		return (zio_wait(zio));
2359789Sahrens 
2360789Sahrens 	ASSERT(arc_flags & ARC_NOWAIT);
2361789Sahrens 	zio_nowait(zio);
2362789Sahrens 
2363789Sahrens 	return (0);
2364789Sahrens }
2365789Sahrens 
2366789Sahrens void
2367789Sahrens arc_tempreserve_clear(uint64_t tempreserve)
2368789Sahrens {
2369789Sahrens 	atomic_add_64(&arc_tempreserve, -tempreserve);
2370789Sahrens 	ASSERT((int64_t)arc_tempreserve >= 0);
2371789Sahrens }
2372789Sahrens 
2373789Sahrens int
2374789Sahrens arc_tempreserve_space(uint64_t tempreserve)
2375789Sahrens {
2376789Sahrens #ifdef ZFS_DEBUG
2377789Sahrens 	/*
2378789Sahrens 	 * Once in a while, fail for no reason.  Everything should cope.
2379789Sahrens 	 */
2380789Sahrens 	if (spa_get_random(10000) == 0) {
2381789Sahrens 		dprintf("forcing random failure\n");
2382789Sahrens 		return (ERESTART);
2383789Sahrens 	}
2384789Sahrens #endif
2385982Smaybee 	if (tempreserve > arc.c/4 && !arc.no_grow)
2386982Smaybee 		arc.c = MIN(arc.c_max, tempreserve * 4);
2387982Smaybee 	if (tempreserve > arc.c)
2388982Smaybee 		return (ENOMEM);
2389982Smaybee 
2390789Sahrens 	/*
2391982Smaybee 	 * Throttle writes when the amount of dirty data in the cache
2392982Smaybee 	 * gets too large.  We try to keep the cache less than half full
2393982Smaybee 	 * of dirty blocks so that our sync times don't grow too large.
2394982Smaybee 	 * Note: if two requests come in concurrently, we might let them
2395982Smaybee 	 * both succeed, when one of them should fail.  Not a huge deal.
2396982Smaybee 	 *
2397982Smaybee 	 * XXX The limit should be adjusted dynamically to keep the time
2398982Smaybee 	 * to sync a dataset fixed (around 1-5 seconds?).
2399789Sahrens 	 */
2400789Sahrens 
2401982Smaybee 	if (tempreserve + arc_tempreserve + arc.anon->size > arc.c / 2 &&
2402982Smaybee 	    arc_tempreserve + arc.anon->size > arc.c / 4) {
2403789Sahrens 		dprintf("failing, arc_tempreserve=%lluK anon=%lluK "
2404789Sahrens 		    "tempreserve=%lluK arc.c=%lluK\n",
2405789Sahrens 		    arc_tempreserve>>10, arc.anon->lsize>>10,
2406789Sahrens 		    tempreserve>>10, arc.c>>10);
2407789Sahrens 		return (ERESTART);
2408789Sahrens 	}
2409789Sahrens 	atomic_add_64(&arc_tempreserve, tempreserve);
2410789Sahrens 	return (0);
2411789Sahrens }
2412789Sahrens 
2413789Sahrens void
2414789Sahrens arc_init(void)
2415789Sahrens {
2416789Sahrens 	mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
2417789Sahrens 	mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
2418789Sahrens 	cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
2419789Sahrens 
24202391Smaybee 	/* Convert seconds to clock ticks */
24212638Sperrin 	arc_min_prefetch_lifespan = 1 * hz;
24222391Smaybee 
2423789Sahrens 	/* Start out with 1/8 of all memory */
2424789Sahrens 	arc.c = physmem * PAGESIZE / 8;
2425789Sahrens 
2426789Sahrens #ifdef _KERNEL
2427789Sahrens 	/*
2428789Sahrens 	 * On architectures where the physical memory can be larger
2429789Sahrens 	 * than the addressable space (intel in 32-bit mode), we may
2430789Sahrens 	 * need to limit the cache to 1/8 of VM size.
2431789Sahrens 	 */
2432789Sahrens 	arc.c = MIN(arc.c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
2433789Sahrens #endif
2434789Sahrens 
2435982Smaybee 	/* set min cache to 1/32 of all memory, or 64MB, whichever is more */
2436789Sahrens 	arc.c_min = MAX(arc.c / 4, 64<<20);
2437982Smaybee 	/* set max to 3/4 of all memory, or all but 1GB, whichever is more */
2438789Sahrens 	if (arc.c * 8 >= 1<<30)
2439789Sahrens 		arc.c_max = (arc.c * 8) - (1<<30);
2440789Sahrens 	else
2441789Sahrens 		arc.c_max = arc.c_min;
2442789Sahrens 	arc.c_max = MAX(arc.c * 6, arc.c_max);
2443789Sahrens 	arc.c = arc.c_max;
2444789Sahrens 	arc.p = (arc.c >> 1);
2445789Sahrens 
2446789Sahrens 	/* if kmem_flags are set, lets try to use less memory */
2447789Sahrens 	if (kmem_debugging())
2448789Sahrens 		arc.c = arc.c / 2;
2449789Sahrens 	if (arc.c < arc.c_min)
2450789Sahrens 		arc.c = arc.c_min;
2451789Sahrens 
2452789Sahrens 	arc.anon = &ARC_anon;
24531544Seschrock 	arc.mru = &ARC_mru;
24541544Seschrock 	arc.mru_ghost = &ARC_mru_ghost;
24551544Seschrock 	arc.mfu = &ARC_mfu;
24561544Seschrock 	arc.mfu_ghost = &ARC_mfu_ghost;
24571544Seschrock 	arc.size = 0;
2458789Sahrens 
2459*2688Smaybee 	arc.hits = 0;
2460*2688Smaybee 	arc.recycle_miss = 0;
2461*2688Smaybee 	arc.evict_skip = 0;
2462*2688Smaybee 	arc.mutex_miss = 0;
2463*2688Smaybee 
24641544Seschrock 	list_create(&arc.mru->list, sizeof (arc_buf_hdr_t),
2465789Sahrens 	    offsetof(arc_buf_hdr_t, b_arc_node));
24661544Seschrock 	list_create(&arc.mru_ghost->list, sizeof (arc_buf_hdr_t),
2467789Sahrens 	    offsetof(arc_buf_hdr_t, b_arc_node));
24681544Seschrock 	list_create(&arc.mfu->list, sizeof (arc_buf_hdr_t),
2469789Sahrens 	    offsetof(arc_buf_hdr_t, b_arc_node));
24701544Seschrock 	list_create(&arc.mfu_ghost->list, sizeof (arc_buf_hdr_t),
2471789Sahrens 	    offsetof(arc_buf_hdr_t, b_arc_node));
2472789Sahrens 
2473789Sahrens 	buf_init();
2474789Sahrens 
2475789Sahrens 	arc_thread_exit = 0;
24761544Seschrock 	arc_eviction_list = NULL;
24771544Seschrock 	mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
2478789Sahrens 
2479789Sahrens 	(void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
2480789Sahrens 	    TS_RUN, minclsyspri);
2481789Sahrens }
2482789Sahrens 
2483789Sahrens void
2484789Sahrens arc_fini(void)
2485789Sahrens {
2486789Sahrens 	mutex_enter(&arc_reclaim_thr_lock);
2487789Sahrens 	arc_thread_exit = 1;
2488789Sahrens 	while (arc_thread_exit != 0)
2489789Sahrens 		cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
2490789Sahrens 	mutex_exit(&arc_reclaim_thr_lock);
2491789Sahrens 
2492789Sahrens 	arc_flush();
2493789Sahrens 
2494789Sahrens 	arc_dead = TRUE;
2495789Sahrens 
24961544Seschrock 	mutex_destroy(&arc_eviction_mtx);
2497789Sahrens 	mutex_destroy(&arc_reclaim_lock);
2498789Sahrens 	mutex_destroy(&arc_reclaim_thr_lock);
2499789Sahrens 	cv_destroy(&arc_reclaim_thr_cv);
2500789Sahrens 
25011544Seschrock 	list_destroy(&arc.mru->list);
25021544Seschrock 	list_destroy(&arc.mru_ghost->list);
25031544Seschrock 	list_destroy(&arc.mfu->list);
25041544Seschrock 	list_destroy(&arc.mfu_ghost->list);
2505789Sahrens 
2506789Sahrens 	buf_fini();
2507789Sahrens }
2508