xref: /illumos-gate/usr/src/uts/common/fs/zfs/arc.c (revision 0c44d0008f52b6a42b9c01d3b344661217520a68)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * DVA-based Adjustable Relpacement Cache
30  *
31  * While much of the theory of operation used here is
32  * based on the self-tuning, low overhead replacement cache
33  * presented by Megiddo and Modha at FAST 2003, there are some
34  * significant differences:
35  *
36  * 1. The Megiddo and Modha model assumes any page is evictable.
37  * Pages in its cache cannot be "locked" into memory.  This makes
38  * the eviction algorithm simple: evict the last page in the list.
39  * This also make the performance characteristics easy to reason
40  * about.  Our cache is not so simple.  At any given moment, some
41  * subset of the blocks in the cache are un-evictable because we
42  * have handed out a reference to them.  Blocks are only evictable
43  * when there are no external references active.  This makes
44  * eviction far more problematic:  we choose to evict the evictable
45  * blocks that are the "lowest" in the list.
46  *
47  * There are times when it is not possible to evict the requested
48  * space.  In these circumstances we are unable to adjust the cache
49  * size.  To prevent the cache growing unbounded at these times we
50  * implement a "cache throttle" that slowes the flow of new data
51  * into the cache until we can make space avaiable.
52  *
53  * 2. The Megiddo and Modha model assumes a fixed cache size.
54  * Pages are evicted when the cache is full and there is a cache
55  * miss.  Our model has a variable sized cache.  It grows with
56  * high use, but also tries to react to memory preasure from the
57  * operating system: decreasing its size when system memory is
58  * tight.
59  *
60  * 3. The Megiddo and Modha model assumes a fixed page size. All
61  * elements of the cache are therefor exactly the same size.  So
62  * when adjusting the cache size following a cache miss, its simply
63  * a matter of choosing a single page to evict.  In our model, we
64  * have variable sized cache blocks (rangeing from 512 bytes to
65  * 128K bytes).  We therefor choose a set of blocks to evict to make
66  * space for a cache miss that approximates as closely as possible
67  * the space used by the new block.
68  *
69  * See also:  "ARC: A Self-Tuning, Low Overhead Replacement Cache"
70  * by N. Megiddo & D. Modha, FAST 2003
71  */
72 
73 /*
74  * The locking model:
75  *
76  * A new reference to a cache buffer can be obtained in two
77  * ways: 1) via a hash table lookup using the DVA as a key,
78  * or 2) via one of the ARC lists.  The arc_read() inerface
79  * uses method 1, while the internal arc algorithms for
80  * adjusting the cache use method 2.  We therefor provide two
81  * types of locks: 1) the hash table lock array, and 2) the
82  * arc list locks.
83  *
84  * Buffers do not have their own mutexs, rather they rely on the
85  * hash table mutexs for the bulk of their protection (i.e. most
86  * fields in the arc_buf_hdr_t are protected by these mutexs).
87  *
88  * buf_hash_find() returns the appropriate mutex (held) when it
89  * locates the requested buffer in the hash table.  It returns
90  * NULL for the mutex if the buffer was not in the table.
91  *
92  * buf_hash_remove() expects the appropriate hash mutex to be
93  * already held before it is invoked.
94  *
95  * Each arc state also has a mutex which is used to protect the
96  * buffer list associated with the state.  When attempting to
97  * obtain a hash table lock while holding an arc list lock you
98  * must use: mutex_tryenter() to avoid deadlock.  Also note that
99  * the "top" state mutex must be held before the "bot" state mutex.
100  *
101  * Arc buffers may have an associated eviction callback function.
102  * This function will be invoked prior to removing the buffer (e.g.
103  * in arc_do_user_evicts()).  Note however that the data associated
104  * with the buffer may be evicted prior to the callback.  The callback
105  * must be made with *no locks held* (to prevent deadlock).  Additionally,
106  * the users of callbacks must ensure that their private data is
107  * protected from simultaneous callbacks from arc_buf_evict()
108  * and arc_do_user_evicts().
109  *
110  * Note that the majority of the performance stats are manipulated
111  * with atomic operations.
112  */
113 
114 #include <sys/spa.h>
115 #include <sys/zio.h>
116 #include <sys/zfs_context.h>
117 #include <sys/arc.h>
118 #include <sys/refcount.h>
119 #ifdef _KERNEL
120 #include <sys/vmsystm.h>
121 #include <vm/anon.h>
122 #include <sys/fs/swapnode.h>
123 #include <sys/dnlc.h>
124 #endif
125 #include <sys/callb.h>
126 
127 static kmutex_t		arc_reclaim_thr_lock;
128 static kcondvar_t	arc_reclaim_thr_cv;	/* used to signal reclaim thr */
129 static uint8_t		arc_thread_exit;
130 
131 #define	ARC_REDUCE_DNLC_PERCENT	3
132 uint_t arc_reduce_dnlc_percent = ARC_REDUCE_DNLC_PERCENT;
133 
134 typedef enum arc_reclaim_strategy {
135 	ARC_RECLAIM_AGGR,		/* Aggressive reclaim strategy */
136 	ARC_RECLAIM_CONS		/* Conservative reclaim strategy */
137 } arc_reclaim_strategy_t;
138 
139 /* number of seconds before growing cache again */
140 static int		arc_grow_retry = 60;
141 
142 static kmutex_t arc_reclaim_lock;
143 static int arc_dead;
144 
145 /*
146  * Note that buffers can be on one of 5 states:
147  *	ARC_anon	- anonymous (discussed below)
148  *	ARC_mru		- recently used, currently cached
149  *	ARC_mru_ghost	- recentely used, no longer in cache
150  *	ARC_mfu		- frequently used, currently cached
151  *	ARC_mfu_ghost	- frequently used, no longer in cache
152  * When there are no active references to the buffer, they
153  * are linked onto one of the lists in arc.  These are the
154  * only buffers that can be evicted or deleted.
155  *
156  * Anonymous buffers are buffers that are not associated with
157  * a DVA.  These are buffers that hold dirty block copies
158  * before they are written to stable storage.  By definition,
159  * they are "ref'd" and are considered part of arc_mru
160  * that cannot be freed.  Generally, they will aquire a DVA
161  * as they are written and migrate onto the arc_mru list.
162  */
163 
164 typedef struct arc_state {
165 	list_t	list;	/* linked list of evictable buffer in state */
166 	uint64_t lsize;	/* total size of buffers in the linked list */
167 	uint64_t size;	/* total size of all buffers in this state */
168 	uint64_t hits;
169 	kmutex_t mtx;
170 } arc_state_t;
171 
172 /* The 5 states: */
173 static arc_state_t ARC_anon;
174 static arc_state_t ARC_mru;
175 static arc_state_t ARC_mru_ghost;
176 static arc_state_t ARC_mfu;
177 static arc_state_t ARC_mfu_ghost;
178 
179 static struct arc {
180 	arc_state_t 	*anon;
181 	arc_state_t	*mru;
182 	arc_state_t	*mru_ghost;
183 	arc_state_t	*mfu;
184 	arc_state_t	*mfu_ghost;
185 	uint64_t	size;		/* Actual total arc size */
186 	uint64_t	p;		/* Target size (in bytes) of mru */
187 	uint64_t	c;		/* Target size of cache (in bytes) */
188 	uint64_t	c_min;		/* Minimum target cache size */
189 	uint64_t	c_max;		/* Maximum target cache size */
190 
191 	/* performance stats */
192 	uint64_t	hits;
193 	uint64_t	misses;
194 	uint64_t	deleted;
195 	uint64_t	skipped;
196 	uint64_t	hash_elements;
197 	uint64_t	hash_elements_max;
198 	uint64_t	hash_collisions;
199 	uint64_t	hash_chains;
200 	uint32_t	hash_chain_max;
201 
202 	int		no_grow;	/* Don't try to grow cache size */
203 } arc;
204 
205 static uint64_t arc_tempreserve;
206 
207 typedef struct arc_callback arc_callback_t;
208 
209 struct arc_callback {
210 	arc_done_func_t		*acb_done;
211 	void			*acb_private;
212 	arc_byteswap_func_t	*acb_byteswap;
213 	arc_buf_t		*acb_buf;
214 	zio_t			*acb_zio_dummy;
215 	arc_callback_t		*acb_next;
216 };
217 
218 struct arc_buf_hdr {
219 	/* immutable */
220 	uint64_t		b_size;
221 	spa_t			*b_spa;
222 
223 	/* protected by hash lock */
224 	dva_t			b_dva;
225 	uint64_t		b_birth;
226 	uint64_t		b_cksum0;
227 
228 	arc_buf_hdr_t		*b_hash_next;
229 	arc_buf_t		*b_buf;
230 	uint32_t		b_flags;
231 	uint32_t		b_datacnt;
232 
233 	kcondvar_t		b_cv;
234 	arc_callback_t		*b_acb;
235 
236 	/* protected by arc state mutex */
237 	arc_state_t		*b_state;
238 	list_node_t		b_arc_node;
239 
240 	/* updated atomically */
241 	clock_t			b_arc_access;
242 
243 	/* self protecting */
244 	refcount_t		b_refcnt;
245 };
246 
247 static arc_buf_t *arc_eviction_list;
248 static kmutex_t arc_eviction_mtx;
249 static void arc_access_and_exit(arc_buf_hdr_t *buf, kmutex_t *hash_lock);
250 
251 #define	GHOST_STATE(state)	\
252 	((state) == arc.mru_ghost || (state) == arc.mfu_ghost)
253 
254 /*
255  * Private ARC flags.  These flags are private ARC only flags that will show up
256  * in b_flags in the arc_hdr_buf_t.  Some flags are publicly declared, and can
257  * be passed in as arc_flags in things like arc_read.  However, these flags
258  * should never be passed and should only be set by ARC code.  When adding new
259  * public flags, make sure not to smash the private ones.
260  */
261 
262 #define	ARC_IN_HASH_TABLE	(1 << 9)	/* this buffer is hashed */
263 #define	ARC_IO_IN_PROGRESS	(1 << 10)	/* I/O in progress for buf */
264 #define	ARC_IO_ERROR		(1 << 11)	/* I/O failed for buf */
265 #define	ARC_FREED_IN_READ	(1 << 12)	/* buf freed while in read */
266 #define	ARC_BUF_AVAILABLE	(1 << 13)	/* block not in active use */
267 
268 #define	HDR_IN_HASH_TABLE(hdr)	((hdr)->b_flags & ARC_IN_HASH_TABLE)
269 #define	HDR_IO_IN_PROGRESS(hdr)	((hdr)->b_flags & ARC_IO_IN_PROGRESS)
270 #define	HDR_IO_ERROR(hdr)	((hdr)->b_flags & ARC_IO_ERROR)
271 #define	HDR_FREED_IN_READ(hdr)	((hdr)->b_flags & ARC_FREED_IN_READ)
272 #define	HDR_BUF_AVAILABLE(hdr)	((hdr)->b_flags & ARC_BUF_AVAILABLE)
273 
274 /*
275  * Hash table routines
276  */
277 
278 #define	HT_LOCK_PAD	64
279 
280 struct ht_lock {
281 	kmutex_t	ht_lock;
282 #ifdef _KERNEL
283 	unsigned char	pad[(HT_LOCK_PAD - sizeof (kmutex_t))];
284 #endif
285 };
286 
287 #define	BUF_LOCKS 256
288 typedef struct buf_hash_table {
289 	uint64_t ht_mask;
290 	arc_buf_hdr_t **ht_table;
291 	struct ht_lock ht_locks[BUF_LOCKS];
292 } buf_hash_table_t;
293 
294 static buf_hash_table_t buf_hash_table;
295 
296 #define	BUF_HASH_INDEX(spa, dva, birth) \
297 	(buf_hash(spa, dva, birth) & buf_hash_table.ht_mask)
298 #define	BUF_HASH_LOCK_NTRY(idx) (buf_hash_table.ht_locks[idx & (BUF_LOCKS-1)])
299 #define	BUF_HASH_LOCK(idx)	(&(BUF_HASH_LOCK_NTRY(idx).ht_lock))
300 #define	HDR_LOCK(buf) \
301 	(BUF_HASH_LOCK(BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth)))
302 
303 uint64_t zfs_crc64_table[256];
304 
305 static uint64_t
306 buf_hash(spa_t *spa, dva_t *dva, uint64_t birth)
307 {
308 	uintptr_t spav = (uintptr_t)spa;
309 	uint8_t *vdva = (uint8_t *)dva;
310 	uint64_t crc = -1ULL;
311 	int i;
312 
313 	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
314 
315 	for (i = 0; i < sizeof (dva_t); i++)
316 		crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ vdva[i]) & 0xFF];
317 
318 	crc ^= (spav>>8) ^ birth;
319 
320 	return (crc);
321 }
322 
323 #define	BUF_EMPTY(buf)						\
324 	((buf)->b_dva.dva_word[0] == 0 &&			\
325 	(buf)->b_dva.dva_word[1] == 0 &&			\
326 	(buf)->b_birth == 0)
327 
328 #define	BUF_EQUAL(spa, dva, birth, buf)				\
329 	((buf)->b_dva.dva_word[0] == (dva)->dva_word[0]) &&	\
330 	((buf)->b_dva.dva_word[1] == (dva)->dva_word[1]) &&	\
331 	((buf)->b_birth == birth) && ((buf)->b_spa == spa)
332 
333 static arc_buf_hdr_t *
334 buf_hash_find(spa_t *spa, dva_t *dva, uint64_t birth, kmutex_t **lockp)
335 {
336 	uint64_t idx = BUF_HASH_INDEX(spa, dva, birth);
337 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
338 	arc_buf_hdr_t *buf;
339 
340 	mutex_enter(hash_lock);
341 	for (buf = buf_hash_table.ht_table[idx]; buf != NULL;
342 	    buf = buf->b_hash_next) {
343 		if (BUF_EQUAL(spa, dva, birth, buf)) {
344 			*lockp = hash_lock;
345 			return (buf);
346 		}
347 	}
348 	mutex_exit(hash_lock);
349 	*lockp = NULL;
350 	return (NULL);
351 }
352 
353 /*
354  * Insert an entry into the hash table.  If there is already an element
355  * equal to elem in the hash table, then the already existing element
356  * will be returned and the new element will not be inserted.
357  * Otherwise returns NULL.
358  */
359 static arc_buf_hdr_t *
360 buf_hash_insert(arc_buf_hdr_t *buf, kmutex_t **lockp)
361 {
362 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
363 	kmutex_t *hash_lock = BUF_HASH_LOCK(idx);
364 	arc_buf_hdr_t *fbuf;
365 	uint32_t max, i;
366 
367 	ASSERT(!HDR_IN_HASH_TABLE(buf));
368 	*lockp = hash_lock;
369 	mutex_enter(hash_lock);
370 	for (fbuf = buf_hash_table.ht_table[idx], i = 0; fbuf != NULL;
371 	    fbuf = fbuf->b_hash_next, i++) {
372 		if (BUF_EQUAL(buf->b_spa, &buf->b_dva, buf->b_birth, fbuf))
373 			return (fbuf);
374 	}
375 
376 	buf->b_hash_next = buf_hash_table.ht_table[idx];
377 	buf_hash_table.ht_table[idx] = buf;
378 	buf->b_flags |= ARC_IN_HASH_TABLE;
379 
380 	/* collect some hash table performance data */
381 	if (i > 0) {
382 		atomic_add_64(&arc.hash_collisions, 1);
383 		if (i == 1)
384 			atomic_add_64(&arc.hash_chains, 1);
385 	}
386 	while (i > (max = arc.hash_chain_max) &&
387 	    max != atomic_cas_32(&arc.hash_chain_max, max, i)) {
388 		continue;
389 	}
390 	atomic_add_64(&arc.hash_elements, 1);
391 	if (arc.hash_elements > arc.hash_elements_max)
392 		atomic_add_64(&arc.hash_elements_max, 1);
393 
394 	return (NULL);
395 }
396 
397 static void
398 buf_hash_remove(arc_buf_hdr_t *buf)
399 {
400 	arc_buf_hdr_t *fbuf, **bufp;
401 	uint64_t idx = BUF_HASH_INDEX(buf->b_spa, &buf->b_dva, buf->b_birth);
402 
403 	ASSERT(MUTEX_HELD(BUF_HASH_LOCK(idx)));
404 	ASSERT(HDR_IN_HASH_TABLE(buf));
405 
406 	bufp = &buf_hash_table.ht_table[idx];
407 	while ((fbuf = *bufp) != buf) {
408 		ASSERT(fbuf != NULL);
409 		bufp = &fbuf->b_hash_next;
410 	}
411 	*bufp = buf->b_hash_next;
412 	buf->b_hash_next = NULL;
413 	buf->b_flags &= ~ARC_IN_HASH_TABLE;
414 
415 	/* collect some hash table performance data */
416 	atomic_add_64(&arc.hash_elements, -1);
417 	if (buf_hash_table.ht_table[idx] &&
418 	    buf_hash_table.ht_table[idx]->b_hash_next == NULL)
419 		atomic_add_64(&arc.hash_chains, -1);
420 }
421 
422 /*
423  * Global data structures and functions for the buf kmem cache.
424  */
425 static kmem_cache_t *hdr_cache;
426 static kmem_cache_t *buf_cache;
427 
428 static void
429 buf_fini(void)
430 {
431 	int i;
432 
433 	kmem_free(buf_hash_table.ht_table,
434 	    (buf_hash_table.ht_mask + 1) * sizeof (void *));
435 	for (i = 0; i < BUF_LOCKS; i++)
436 		mutex_destroy(&buf_hash_table.ht_locks[i].ht_lock);
437 	kmem_cache_destroy(hdr_cache);
438 	kmem_cache_destroy(buf_cache);
439 }
440 
441 /*
442  * Constructor callback - called when the cache is empty
443  * and a new buf is requested.
444  */
445 /* ARGSUSED */
446 static int
447 hdr_cons(void *vbuf, void *unused, int kmflag)
448 {
449 	arc_buf_hdr_t *buf = vbuf;
450 
451 	bzero(buf, sizeof (arc_buf_hdr_t));
452 	refcount_create(&buf->b_refcnt);
453 	cv_init(&buf->b_cv, NULL, CV_DEFAULT, NULL);
454 	return (0);
455 }
456 
457 /*
458  * Destructor callback - called when a cached buf is
459  * no longer required.
460  */
461 /* ARGSUSED */
462 static void
463 hdr_dest(void *vbuf, void *unused)
464 {
465 	arc_buf_hdr_t *buf = vbuf;
466 
467 	refcount_destroy(&buf->b_refcnt);
468 	cv_destroy(&buf->b_cv);
469 }
470 
471 static int arc_reclaim_needed(void);
472 void arc_kmem_reclaim(void);
473 
474 /*
475  * Reclaim callback -- invoked when memory is low.
476  */
477 /* ARGSUSED */
478 static void
479 hdr_recl(void *unused)
480 {
481 	dprintf("hdr_recl called\n");
482 	if (arc_reclaim_needed())
483 		arc_kmem_reclaim();
484 }
485 
486 static void
487 buf_init(void)
488 {
489 	uint64_t *ct;
490 	uint64_t hsize = 1ULL << 12;
491 	int i, j;
492 
493 	/*
494 	 * The hash table is big enough to fill all of physical memory
495 	 * with an average 64K block size.  The table will take up
496 	 * totalmem*sizeof(void*)/64K (eg. 128KB/GB with 8-byte pointers).
497 	 */
498 	while (hsize * 65536 < physmem * PAGESIZE)
499 		hsize <<= 1;
500 retry:
501 	buf_hash_table.ht_mask = hsize - 1;
502 	buf_hash_table.ht_table =
503 	    kmem_zalloc(hsize * sizeof (void*), KM_NOSLEEP);
504 	if (buf_hash_table.ht_table == NULL) {
505 		ASSERT(hsize > (1ULL << 8));
506 		hsize >>= 1;
507 		goto retry;
508 	}
509 
510 	hdr_cache = kmem_cache_create("arc_buf_hdr_t", sizeof (arc_buf_hdr_t),
511 	    0, hdr_cons, hdr_dest, hdr_recl, NULL, NULL, 0);
512 	buf_cache = kmem_cache_create("arc_buf_t", sizeof (arc_buf_t),
513 	    0, NULL, NULL, NULL, NULL, NULL, 0);
514 
515 	for (i = 0; i < 256; i++)
516 		for (ct = zfs_crc64_table + i, *ct = i, j = 8; j > 0; j--)
517 			*ct = (*ct >> 1) ^ (-(*ct & 1) & ZFS_CRC64_POLY);
518 
519 	for (i = 0; i < BUF_LOCKS; i++) {
520 		mutex_init(&buf_hash_table.ht_locks[i].ht_lock,
521 		    NULL, MUTEX_DEFAULT, NULL);
522 	}
523 }
524 
525 #define	ARC_MINTIME	(hz>>4) /* 62 ms */
526 
527 static void
528 add_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
529 {
530 	ASSERT(MUTEX_HELD(hash_lock));
531 
532 	if ((refcount_add(&ab->b_refcnt, tag) == 1) &&
533 	    (ab->b_state != arc.anon)) {
534 		int delta = ab->b_size * ab->b_datacnt;
535 
536 		ASSERT(!MUTEX_HELD(&ab->b_state->mtx));
537 		mutex_enter(&ab->b_state->mtx);
538 		ASSERT(refcount_count(&ab->b_refcnt) > 0);
539 		ASSERT(list_link_active(&ab->b_arc_node));
540 		list_remove(&ab->b_state->list, ab);
541 		if (GHOST_STATE(ab->b_state)) {
542 			ASSERT3U(ab->b_datacnt, ==, 0);
543 			ASSERT3P(ab->b_buf, ==, NULL);
544 			delta = ab->b_size;
545 		}
546 		ASSERT(delta > 0);
547 		ASSERT3U(ab->b_state->lsize, >=, delta);
548 		atomic_add_64(&ab->b_state->lsize, -delta);
549 		mutex_exit(&ab->b_state->mtx);
550 	}
551 }
552 
553 static int
554 remove_reference(arc_buf_hdr_t *ab, kmutex_t *hash_lock, void *tag)
555 {
556 	int cnt;
557 
558 	ASSERT(ab->b_state == arc.anon || MUTEX_HELD(hash_lock));
559 	ASSERT(!GHOST_STATE(ab->b_state));
560 
561 	if (((cnt = refcount_remove(&ab->b_refcnt, tag)) == 0) &&
562 	    (ab->b_state != arc.anon)) {
563 
564 		ASSERT(!MUTEX_HELD(&ab->b_state->mtx));
565 		mutex_enter(&ab->b_state->mtx);
566 		ASSERT(!list_link_active(&ab->b_arc_node));
567 		list_insert_head(&ab->b_state->list, ab);
568 		ASSERT(ab->b_datacnt > 0);
569 		atomic_add_64(&ab->b_state->lsize, ab->b_size * ab->b_datacnt);
570 		ASSERT3U(ab->b_state->size, >=, ab->b_state->lsize);
571 		mutex_exit(&ab->b_state->mtx);
572 	}
573 	return (cnt);
574 }
575 
576 /*
577  * Move the supplied buffer to the indicated state.  The mutex
578  * for the buffer must be held by the caller.
579  */
580 static void
581 arc_change_state(arc_state_t *new_state, arc_buf_hdr_t *ab, kmutex_t *hash_lock)
582 {
583 	arc_state_t *old_state = ab->b_state;
584 	int refcnt = refcount_count(&ab->b_refcnt);
585 	int from_delta, to_delta;
586 
587 	ASSERT(MUTEX_HELD(hash_lock));
588 	ASSERT(new_state != old_state);
589 	ASSERT(refcnt == 0 || ab->b_datacnt > 0);
590 	ASSERT(ab->b_datacnt == 0 || !GHOST_STATE(new_state));
591 
592 	from_delta = to_delta = ab->b_datacnt * ab->b_size;
593 
594 	/*
595 	 * If this buffer is evictable, transfer it from the
596 	 * old state list to the new state list.
597 	 */
598 	if (refcnt == 0) {
599 		if (old_state != arc.anon) {
600 			int use_mutex = !MUTEX_HELD(&old_state->mtx);
601 
602 			if (use_mutex)
603 				mutex_enter(&old_state->mtx);
604 
605 			ASSERT(list_link_active(&ab->b_arc_node));
606 			list_remove(&old_state->list, ab);
607 
608 			/* ghost elements have a ghost size */
609 			if (GHOST_STATE(old_state)) {
610 				ASSERT(ab->b_datacnt == 0);
611 				ASSERT(ab->b_buf == NULL);
612 				from_delta = ab->b_size;
613 			}
614 			ASSERT3U(old_state->lsize, >=, from_delta);
615 			atomic_add_64(&old_state->lsize, -from_delta);
616 
617 			if (use_mutex)
618 				mutex_exit(&old_state->mtx);
619 		}
620 		if (new_state != arc.anon) {
621 			int use_mutex = !MUTEX_HELD(&new_state->mtx);
622 
623 			if (use_mutex)
624 				mutex_enter(&new_state->mtx);
625 
626 			list_insert_head(&new_state->list, ab);
627 
628 			/* ghost elements have a ghost size */
629 			if (GHOST_STATE(new_state)) {
630 				ASSERT(ab->b_datacnt == 0);
631 				ASSERT(ab->b_buf == NULL);
632 				to_delta = ab->b_size;
633 			}
634 			atomic_add_64(&new_state->lsize, to_delta);
635 			ASSERT3U(new_state->size + to_delta, >=,
636 			    new_state->lsize);
637 
638 			if (use_mutex)
639 				mutex_exit(&new_state->mtx);
640 		}
641 	}
642 
643 	ASSERT(!BUF_EMPTY(ab));
644 	if (new_state == arc.anon && old_state != arc.anon) {
645 		buf_hash_remove(ab);
646 	}
647 
648 	/*
649 	 * If this buffer isn't being transferred to the MRU-top
650 	 * state, it's safe to clear its prefetch flag
651 	 */
652 	if ((new_state != arc.mru) && (new_state != arc.mru_ghost)) {
653 		ab->b_flags &= ~ARC_PREFETCH;
654 	}
655 
656 	/* adjust state sizes */
657 	if (to_delta)
658 		atomic_add_64(&new_state->size, to_delta);
659 	if (from_delta) {
660 		ASSERT3U(old_state->size, >=, from_delta);
661 		atomic_add_64(&old_state->size, -from_delta);
662 	}
663 	ab->b_state = new_state;
664 }
665 
666 arc_buf_t *
667 arc_buf_alloc(spa_t *spa, int size, void *tag)
668 {
669 	arc_buf_hdr_t *hdr;
670 	arc_buf_t *buf;
671 
672 	ASSERT3U(size, >, 0);
673 	hdr = kmem_cache_alloc(hdr_cache, KM_SLEEP);
674 	ASSERT(BUF_EMPTY(hdr));
675 	hdr->b_size = size;
676 	hdr->b_spa = spa;
677 	hdr->b_state = arc.anon;
678 	hdr->b_arc_access = 0;
679 	buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
680 	buf->b_hdr = hdr;
681 	buf->b_efunc = NULL;
682 	buf->b_private = NULL;
683 	buf->b_next = NULL;
684 	buf->b_data = zio_buf_alloc(size);
685 	hdr->b_buf = buf;
686 	hdr->b_datacnt = 1;
687 	hdr->b_flags = 0;
688 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
689 	(void) refcount_add(&hdr->b_refcnt, tag);
690 
691 	atomic_add_64(&arc.size, size);
692 	atomic_add_64(&arc.anon->size, size);
693 
694 	return (buf);
695 }
696 
697 static void *
698 arc_data_copy(arc_buf_hdr_t *hdr, void *old_data)
699 {
700 	void *new_data = zio_buf_alloc(hdr->b_size);
701 
702 	atomic_add_64(&arc.size, hdr->b_size);
703 	bcopy(old_data, new_data, hdr->b_size);
704 	atomic_add_64(&hdr->b_state->size, hdr->b_size);
705 	if (list_link_active(&hdr->b_arc_node)) {
706 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
707 		atomic_add_64(&hdr->b_state->lsize, hdr->b_size);
708 	}
709 	return (new_data);
710 }
711 
712 void
713 arc_buf_add_ref(arc_buf_t *buf, void* tag)
714 {
715 	arc_buf_hdr_t *hdr;
716 	kmutex_t *hash_lock;
717 
718 	mutex_enter(&arc_eviction_mtx);
719 	hdr = buf->b_hdr;
720 	if (buf->b_data == NULL) {
721 		/*
722 		 * This buffer is evicted.
723 		 */
724 		mutex_exit(&arc_eviction_mtx);
725 		return;
726 	} else {
727 		/*
728 		 * Prevent this buffer from being evicted
729 		 * while we add a reference.
730 		 */
731 		buf->b_hdr = NULL;
732 	}
733 	mutex_exit(&arc_eviction_mtx);
734 
735 	ASSERT(hdr->b_state != arc.anon);
736 	hash_lock = HDR_LOCK(hdr);
737 	mutex_enter(hash_lock);
738 	ASSERT(!GHOST_STATE(hdr->b_state));
739 	buf->b_hdr = hdr;
740 	add_reference(hdr, hash_lock, tag);
741 	arc_access_and_exit(hdr, hash_lock);
742 	atomic_add_64(&arc.hits, 1);
743 }
744 
745 static void
746 arc_buf_destroy(arc_buf_t *buf, boolean_t all)
747 {
748 	arc_buf_t **bufp;
749 
750 	/* free up data associated with the buf */
751 	if (buf->b_data) {
752 		arc_state_t *state = buf->b_hdr->b_state;
753 		uint64_t size = buf->b_hdr->b_size;
754 
755 		zio_buf_free(buf->b_data, size);
756 		atomic_add_64(&arc.size, -size);
757 		if (list_link_active(&buf->b_hdr->b_arc_node)) {
758 			ASSERT(refcount_is_zero(&buf->b_hdr->b_refcnt));
759 			ASSERT(state != arc.anon);
760 			ASSERT3U(state->lsize, >=, size);
761 			atomic_add_64(&state->lsize, -size);
762 		}
763 		ASSERT3U(state->size, >=, size);
764 		atomic_add_64(&state->size, -size);
765 		buf->b_data = NULL;
766 		ASSERT(buf->b_hdr->b_datacnt > 0);
767 		buf->b_hdr->b_datacnt -= 1;
768 	}
769 
770 	/* only remove the buf if requested */
771 	if (!all)
772 		return;
773 
774 	/* remove the buf from the hdr list */
775 	for (bufp = &buf->b_hdr->b_buf; *bufp != buf; bufp = &(*bufp)->b_next)
776 		continue;
777 	*bufp = buf->b_next;
778 
779 	ASSERT(buf->b_efunc == NULL);
780 
781 	/* clean up the buf */
782 	buf->b_hdr = NULL;
783 	kmem_cache_free(buf_cache, buf);
784 }
785 
786 static void
787 arc_hdr_destroy(arc_buf_hdr_t *hdr)
788 {
789 	ASSERT(refcount_is_zero(&hdr->b_refcnt));
790 	ASSERT3P(hdr->b_state, ==, arc.anon);
791 	ASSERT(!HDR_IO_IN_PROGRESS(hdr));
792 
793 	if (!BUF_EMPTY(hdr)) {
794 		ASSERT(!HDR_IN_HASH_TABLE(hdr));
795 		bzero(&hdr->b_dva, sizeof (dva_t));
796 		hdr->b_birth = 0;
797 		hdr->b_cksum0 = 0;
798 	}
799 	while (hdr->b_buf) {
800 		arc_buf_t *buf = hdr->b_buf;
801 
802 		if (buf->b_efunc) {
803 			mutex_enter(&arc_eviction_mtx);
804 			ASSERT(buf->b_hdr != NULL);
805 			arc_buf_destroy(hdr->b_buf, FALSE);
806 			hdr->b_buf = buf->b_next;
807 			buf->b_next = arc_eviction_list;
808 			arc_eviction_list = buf;
809 			mutex_exit(&arc_eviction_mtx);
810 		} else {
811 			arc_buf_destroy(hdr->b_buf, TRUE);
812 		}
813 	}
814 
815 	ASSERT(!list_link_active(&hdr->b_arc_node));
816 	ASSERT3P(hdr->b_hash_next, ==, NULL);
817 	ASSERT3P(hdr->b_acb, ==, NULL);
818 	kmem_cache_free(hdr_cache, hdr);
819 }
820 
821 void
822 arc_buf_free(arc_buf_t *buf, void *tag)
823 {
824 	arc_buf_hdr_t *hdr = buf->b_hdr;
825 	int hashed = hdr->b_state != arc.anon;
826 
827 	ASSERT(buf->b_efunc == NULL);
828 	ASSERT(buf->b_data != NULL);
829 
830 	if (hashed) {
831 		kmutex_t *hash_lock = HDR_LOCK(hdr);
832 
833 		mutex_enter(hash_lock);
834 		(void) remove_reference(hdr, hash_lock, tag);
835 		if (hdr->b_datacnt > 1)
836 			arc_buf_destroy(buf, TRUE);
837 		else
838 			hdr->b_flags |= ARC_BUF_AVAILABLE;
839 		mutex_exit(hash_lock);
840 	} else if (HDR_IO_IN_PROGRESS(hdr)) {
841 		int destroy_hdr;
842 		/*
843 		 * We are in the middle of an async write.  Don't destroy
844 		 * this buffer unless the write completes before we finish
845 		 * decrementing the reference count.
846 		 */
847 		mutex_enter(&arc_eviction_mtx);
848 		(void) remove_reference(hdr, NULL, tag);
849 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
850 		destroy_hdr = !HDR_IO_IN_PROGRESS(hdr);
851 		mutex_exit(&arc_eviction_mtx);
852 		if (destroy_hdr)
853 			arc_hdr_destroy(hdr);
854 	} else {
855 		if (remove_reference(hdr, NULL, tag) > 0) {
856 			ASSERT(HDR_IO_ERROR(hdr));
857 			arc_buf_destroy(buf, TRUE);
858 		} else {
859 			arc_hdr_destroy(hdr);
860 		}
861 	}
862 }
863 
864 int
865 arc_buf_remove_ref(arc_buf_t *buf, void* tag)
866 {
867 	arc_buf_hdr_t *hdr = buf->b_hdr;
868 	kmutex_t *hash_lock = HDR_LOCK(hdr);
869 	int no_callback = (buf->b_efunc == NULL);
870 
871 	if (hdr->b_state == arc.anon) {
872 		arc_buf_free(buf, tag);
873 		return (no_callback);
874 	}
875 
876 	mutex_enter(hash_lock);
877 	ASSERT(hdr->b_state != arc.anon);
878 	ASSERT(buf->b_data != NULL);
879 
880 	(void) remove_reference(hdr, hash_lock, tag);
881 	if (hdr->b_datacnt > 1) {
882 		if (no_callback)
883 			arc_buf_destroy(buf, TRUE);
884 	} else if (no_callback) {
885 		ASSERT(hdr->b_buf == buf && buf->b_next == NULL);
886 		hdr->b_flags |= ARC_BUF_AVAILABLE;
887 	}
888 	ASSERT(no_callback || hdr->b_datacnt > 1 ||
889 	    refcount_is_zero(&hdr->b_refcnt));
890 	mutex_exit(hash_lock);
891 	return (no_callback);
892 }
893 
894 int
895 arc_buf_size(arc_buf_t *buf)
896 {
897 	return (buf->b_hdr->b_size);
898 }
899 
900 /*
901  * Evict buffers from list until we've removed the specified number of
902  * bytes.  Move the removed buffers to the appropriate evict state.
903  */
904 static uint64_t
905 arc_evict(arc_state_t *state, int64_t bytes)
906 {
907 	arc_state_t *evicted_state;
908 	uint64_t bytes_evicted = 0, skipped = 0;
909 	arc_buf_hdr_t *ab, *ab_prev;
910 	kmutex_t *hash_lock;
911 
912 	ASSERT(state == arc.mru || state == arc.mfu);
913 
914 	evicted_state = (state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost;
915 
916 	mutex_enter(&state->mtx);
917 	mutex_enter(&evicted_state->mtx);
918 
919 	for (ab = list_tail(&state->list); ab; ab = ab_prev) {
920 		ab_prev = list_prev(&state->list, ab);
921 		hash_lock = HDR_LOCK(ab);
922 		if (mutex_tryenter(hash_lock)) {
923 			ASSERT3U(refcount_count(&ab->b_refcnt), ==, 0);
924 			ASSERT(ab->b_datacnt > 0);
925 			while (ab->b_buf) {
926 				arc_buf_t *buf = ab->b_buf;
927 				if (buf->b_data)
928 					bytes_evicted += ab->b_size;
929 				if (buf->b_efunc) {
930 					mutex_enter(&arc_eviction_mtx);
931 					/*
932 					 * arc_buf_add_ref() could derail
933 					 * this eviction.
934 					 */
935 					if (buf->b_hdr == NULL) {
936 						mutex_exit(&arc_eviction_mtx);
937 						mutex_exit(hash_lock);
938 						goto skip;
939 					}
940 					arc_buf_destroy(buf, FALSE);
941 					ab->b_buf = buf->b_next;
942 					buf->b_next = arc_eviction_list;
943 					arc_eviction_list = buf;
944 					mutex_exit(&arc_eviction_mtx);
945 				} else {
946 					arc_buf_destroy(buf, TRUE);
947 				}
948 			}
949 			ASSERT(ab->b_datacnt == 0);
950 			arc_change_state(evicted_state, ab, hash_lock);
951 			ASSERT(HDR_IN_HASH_TABLE(ab));
952 			ab->b_flags = ARC_IN_HASH_TABLE;
953 			DTRACE_PROBE1(arc__evict, arc_buf_hdr_t *, ab);
954 			mutex_exit(hash_lock);
955 			if (bytes >= 0 && bytes_evicted >= bytes)
956 				break;
957 		} else {
958 skip:
959 			skipped += 1;
960 		}
961 	}
962 	mutex_exit(&evicted_state->mtx);
963 	mutex_exit(&state->mtx);
964 
965 	if (bytes_evicted < bytes)
966 		dprintf("only evicted %lld bytes from %x",
967 		    (longlong_t)bytes_evicted, state);
968 
969 	atomic_add_64(&arc.skipped, skipped);
970 	if (bytes < 0)
971 		return (skipped);
972 	return (bytes_evicted);
973 }
974 
975 /*
976  * Remove buffers from list until we've removed the specified number of
977  * bytes.  Destroy the buffers that are removed.
978  */
979 static void
980 arc_evict_ghost(arc_state_t *state, int64_t bytes)
981 {
982 	arc_buf_hdr_t *ab, *ab_prev;
983 	kmutex_t *hash_lock;
984 	uint64_t bytes_deleted = 0;
985 	uint_t bufs_skipped = 0;
986 
987 	ASSERT(GHOST_STATE(state));
988 top:
989 	mutex_enter(&state->mtx);
990 	for (ab = list_tail(&state->list); ab; ab = ab_prev) {
991 		ab_prev = list_prev(&state->list, ab);
992 		hash_lock = HDR_LOCK(ab);
993 		if (mutex_tryenter(hash_lock)) {
994 			ASSERT(ab->b_buf == NULL);
995 			arc_change_state(arc.anon, ab, hash_lock);
996 			mutex_exit(hash_lock);
997 			atomic_add_64(&arc.deleted, 1);
998 			bytes_deleted += ab->b_size;
999 			arc_hdr_destroy(ab);
1000 			DTRACE_PROBE1(arc__delete, arc_buf_hdr_t *, ab);
1001 			if (bytes >= 0 && bytes_deleted >= bytes)
1002 				break;
1003 		} else {
1004 			if (bytes < 0) {
1005 				mutex_exit(&state->mtx);
1006 				mutex_enter(hash_lock);
1007 				mutex_exit(hash_lock);
1008 				goto top;
1009 			}
1010 			bufs_skipped += 1;
1011 		}
1012 	}
1013 	mutex_exit(&state->mtx);
1014 
1015 	if (bufs_skipped) {
1016 		atomic_add_64(&arc.skipped, bufs_skipped);
1017 		ASSERT(bytes >= 0);
1018 	}
1019 
1020 	if (bytes_deleted < bytes)
1021 		dprintf("only deleted %lld bytes from %p",
1022 		    (longlong_t)bytes_deleted, state);
1023 }
1024 
1025 static void
1026 arc_adjust(void)
1027 {
1028 	int64_t top_sz, mru_over, arc_over;
1029 
1030 	top_sz = arc.anon->size + arc.mru->size;
1031 
1032 	if (top_sz > arc.p && arc.mru->lsize > 0) {
1033 		int64_t toevict = MIN(arc.mru->lsize, top_sz-arc.p);
1034 		(void) arc_evict(arc.mru, toevict);
1035 		top_sz = arc.anon->size + arc.mru->size;
1036 	}
1037 
1038 	mru_over = top_sz + arc.mru_ghost->size - arc.c;
1039 
1040 	if (mru_over > 0) {
1041 		if (arc.mru_ghost->lsize > 0) {
1042 			int64_t todelete = MIN(arc.mru_ghost->lsize, mru_over);
1043 			arc_evict_ghost(arc.mru_ghost, todelete);
1044 		}
1045 	}
1046 
1047 	if ((arc_over = arc.size - arc.c) > 0) {
1048 		int64_t tbl_over;
1049 
1050 		if (arc.mfu->lsize > 0) {
1051 			int64_t toevict = MIN(arc.mfu->lsize, arc_over);
1052 			(void) arc_evict(arc.mfu, toevict);
1053 		}
1054 
1055 		tbl_over = arc.size + arc.mru_ghost->lsize +
1056 		    arc.mfu_ghost->lsize - arc.c*2;
1057 
1058 		if (tbl_over > 0 && arc.mfu_ghost->lsize > 0) {
1059 			int64_t todelete = MIN(arc.mfu_ghost->lsize, tbl_over);
1060 			arc_evict_ghost(arc.mfu_ghost, todelete);
1061 		}
1062 	}
1063 }
1064 
1065 static void
1066 arc_do_user_evicts(void)
1067 {
1068 	mutex_enter(&arc_eviction_mtx);
1069 	while (arc_eviction_list != NULL) {
1070 		arc_buf_t *buf = arc_eviction_list;
1071 		arc_eviction_list = buf->b_next;
1072 		buf->b_hdr = NULL;
1073 		mutex_exit(&arc_eviction_mtx);
1074 
1075 		if (buf->b_efunc != NULL)
1076 			VERIFY(buf->b_efunc(buf) == 0);
1077 
1078 		buf->b_efunc = NULL;
1079 		buf->b_private = NULL;
1080 		kmem_cache_free(buf_cache, buf);
1081 		mutex_enter(&arc_eviction_mtx);
1082 	}
1083 	mutex_exit(&arc_eviction_mtx);
1084 }
1085 
1086 /*
1087  * Flush all *evictable* data from the cache.
1088  * NOTE: this will not touch "active" (i.e. referenced) data.
1089  */
1090 void
1091 arc_flush(void)
1092 {
1093 	while (arc_evict(arc.mru, -1));
1094 	while (arc_evict(arc.mfu, -1));
1095 
1096 	arc_evict_ghost(arc.mru_ghost, -1);
1097 	arc_evict_ghost(arc.mfu_ghost, -1);
1098 
1099 	mutex_enter(&arc_reclaim_thr_lock);
1100 	arc_do_user_evicts();
1101 	mutex_exit(&arc_reclaim_thr_lock);
1102 	ASSERT(arc_eviction_list == NULL);
1103 }
1104 
1105 void
1106 arc_kmem_reclaim(void)
1107 {
1108 	uint64_t to_free;
1109 
1110 	/* Remove 12.5% */
1111 	/*
1112 	 * We need arc_reclaim_lock because we don't want multiple
1113 	 * threads trying to reclaim concurrently.
1114 	 */
1115 
1116 	/*
1117 	 * umem calls the reclaim func when we destroy the buf cache,
1118 	 * which is after we do arc_fini().  So we set a flag to prevent
1119 	 * accessing the destroyed mutexes and lists.
1120 	 */
1121 	if (arc_dead)
1122 		return;
1123 
1124 	if (arc.c <= arc.c_min)
1125 		return;
1126 
1127 	mutex_enter(&arc_reclaim_lock);
1128 
1129 #ifdef _KERNEL
1130 	to_free = MAX(arc.c >> 3, ptob(needfree));
1131 #else
1132 	to_free = arc.c >> 3;
1133 #endif
1134 	if (arc.c > to_free)
1135 		atomic_add_64(&arc.c, -to_free);
1136 	else
1137 		arc.c = arc.c_min;
1138 
1139 	atomic_add_64(&arc.p, -(arc.p >> 3));
1140 	if (arc.c > arc.size)
1141 		arc.c = arc.size;
1142 	if (arc.c < arc.c_min)
1143 		arc.c = arc.c_min;
1144 	if (arc.p > arc.c)
1145 		arc.p = (arc.c >> 1);
1146 	ASSERT((int64_t)arc.p >= 0);
1147 
1148 	arc_adjust();
1149 
1150 	mutex_exit(&arc_reclaim_lock);
1151 }
1152 
1153 static int
1154 arc_reclaim_needed(void)
1155 {
1156 	uint64_t extra;
1157 
1158 #ifdef _KERNEL
1159 
1160 	if (needfree)
1161 		return (1);
1162 
1163 	/*
1164 	 * take 'desfree' extra pages, so we reclaim sooner, rather than later
1165 	 */
1166 	extra = desfree;
1167 
1168 	/*
1169 	 * check that we're out of range of the pageout scanner.  It starts to
1170 	 * schedule paging if freemem is less than lotsfree and needfree.
1171 	 * lotsfree is the high-water mark for pageout, and needfree is the
1172 	 * number of needed free pages.  We add extra pages here to make sure
1173 	 * the scanner doesn't start up while we're freeing memory.
1174 	 */
1175 	if (freemem < lotsfree + needfree + extra)
1176 		return (1);
1177 
1178 	/*
1179 	 * check to make sure that swapfs has enough space so that anon
1180 	 * reservations can still succeeed. anon_resvmem() checks that the
1181 	 * availrmem is greater than swapfs_minfree, and the number of reserved
1182 	 * swap pages.  We also add a bit of extra here just to prevent
1183 	 * circumstances from getting really dire.
1184 	 */
1185 	if (availrmem < swapfs_minfree + swapfs_reserve + extra)
1186 		return (1);
1187 
1188 #if defined(__i386)
1189 	/*
1190 	 * If we're on an i386 platform, it's possible that we'll exhaust the
1191 	 * kernel heap space before we ever run out of available physical
1192 	 * memory.  Most checks of the size of the heap_area compare against
1193 	 * tune.t_minarmem, which is the minimum available real memory that we
1194 	 * can have in the system.  However, this is generally fixed at 25 pages
1195 	 * which is so low that it's useless.  In this comparison, we seek to
1196 	 * calculate the total heap-size, and reclaim if more than 3/4ths of the
1197 	 * heap is allocated.  (Or, in the caclulation, if less than 1/4th is
1198 	 * free)
1199 	 */
1200 	if (btop(vmem_size(heap_arena, VMEM_FREE)) <
1201 	    (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
1202 		return (1);
1203 #endif
1204 
1205 #else
1206 	if (spa_get_random(100) == 0)
1207 		return (1);
1208 #endif
1209 	return (0);
1210 }
1211 
1212 static void
1213 arc_kmem_reap_now(arc_reclaim_strategy_t strat)
1214 {
1215 	size_t			i;
1216 	kmem_cache_t		*prev_cache = NULL;
1217 	extern kmem_cache_t	*zio_buf_cache[];
1218 
1219 #ifdef _KERNEL
1220 	/*
1221 	 * First purge some DNLC entries, in case the DNLC is using
1222 	 * up too much memory.
1223 	 */
1224 	dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
1225 
1226 #if defined(__i386)
1227 	/*
1228 	 * Reclaim unused memory from all kmem caches.
1229 	 */
1230 	kmem_reap();
1231 #endif
1232 #endif
1233 
1234 	/*
1235 	 * An agressive reclamation will shrink the cache size as well as
1236 	 * reap free buffers from the arc kmem caches.
1237 	 */
1238 	if (strat == ARC_RECLAIM_AGGR)
1239 		arc_kmem_reclaim();
1240 
1241 	for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
1242 		if (zio_buf_cache[i] != prev_cache) {
1243 			prev_cache = zio_buf_cache[i];
1244 			kmem_cache_reap_now(zio_buf_cache[i]);
1245 		}
1246 	}
1247 	kmem_cache_reap_now(buf_cache);
1248 	kmem_cache_reap_now(hdr_cache);
1249 }
1250 
1251 static void
1252 arc_reclaim_thread(void)
1253 {
1254 	clock_t			growtime = 0;
1255 	arc_reclaim_strategy_t	last_reclaim = ARC_RECLAIM_CONS;
1256 	callb_cpr_t		cpr;
1257 
1258 	CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
1259 
1260 	mutex_enter(&arc_reclaim_thr_lock);
1261 	while (arc_thread_exit == 0) {
1262 		if (arc_reclaim_needed()) {
1263 
1264 			if (arc.no_grow) {
1265 				if (last_reclaim == ARC_RECLAIM_CONS) {
1266 					last_reclaim = ARC_RECLAIM_AGGR;
1267 				} else {
1268 					last_reclaim = ARC_RECLAIM_CONS;
1269 				}
1270 			} else {
1271 				arc.no_grow = TRUE;
1272 				last_reclaim = ARC_RECLAIM_AGGR;
1273 				membar_producer();
1274 			}
1275 
1276 			/* reset the growth delay for every reclaim */
1277 			growtime = lbolt + (arc_grow_retry * hz);
1278 
1279 			arc_kmem_reap_now(last_reclaim);
1280 
1281 		} else if ((growtime > 0) && ((growtime - lbolt) <= 0)) {
1282 			arc.no_grow = FALSE;
1283 		}
1284 
1285 		if (arc_eviction_list != NULL)
1286 			arc_do_user_evicts();
1287 
1288 		/* block until needed, or one second, whichever is shorter */
1289 		CALLB_CPR_SAFE_BEGIN(&cpr);
1290 		(void) cv_timedwait(&arc_reclaim_thr_cv,
1291 		    &arc_reclaim_thr_lock, (lbolt + hz));
1292 		CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
1293 	}
1294 
1295 	arc_thread_exit = 0;
1296 	cv_broadcast(&arc_reclaim_thr_cv);
1297 	CALLB_CPR_EXIT(&cpr);		/* drops arc_reclaim_thr_lock */
1298 	thread_exit();
1299 }
1300 
1301 /*
1302  * Adapt arc info given the number of bytes we are trying to add and
1303  * the state that we are comming from.  This function is only called
1304  * when we are adding new content to the cache.
1305  */
1306 static void
1307 arc_adapt(int bytes, arc_state_t *state)
1308 {
1309 	int mult;
1310 
1311 	ASSERT(bytes > 0);
1312 	/*
1313 	 * Adapt the target size of the MRU list:
1314 	 *	- if we just hit in the MRU ghost list, then increase
1315 	 *	  the target size of the MRU list.
1316 	 *	- if we just hit in the MFU ghost list, then increase
1317 	 *	  the target size of the MFU list by decreasing the
1318 	 *	  target size of the MRU list.
1319 	 */
1320 	if (state == arc.mru_ghost) {
1321 		mult = ((arc.mru_ghost->size >= arc.mfu_ghost->size) ?
1322 		    1 : (arc.mfu_ghost->size/arc.mru_ghost->size));
1323 
1324 		arc.p = MIN(arc.c, arc.p + bytes * mult);
1325 	} else if (state == arc.mfu_ghost) {
1326 		mult = ((arc.mfu_ghost->size >= arc.mru_ghost->size) ?
1327 		    1 : (arc.mru_ghost->size/arc.mfu_ghost->size));
1328 
1329 		arc.p = MAX(0, (int64_t)arc.p - bytes * mult);
1330 	}
1331 	ASSERT((int64_t)arc.p >= 0);
1332 
1333 	if (arc_reclaim_needed()) {
1334 		cv_signal(&arc_reclaim_thr_cv);
1335 		return;
1336 	}
1337 
1338 	if (arc.no_grow)
1339 		return;
1340 
1341 	if (arc.c >= arc.c_max)
1342 		return;
1343 
1344 	/*
1345 	 * If we're within (2 * maxblocksize) bytes of the target
1346 	 * cache size, increment the target cache size
1347 	 */
1348 	if (arc.size > arc.c - (2ULL << SPA_MAXBLOCKSHIFT)) {
1349 		atomic_add_64(&arc.c, (int64_t)bytes);
1350 		if (arc.c > arc.c_max)
1351 			arc.c = arc.c_max;
1352 		else if (state == arc.anon)
1353 			atomic_add_64(&arc.p, (int64_t)bytes);
1354 		if (arc.p > arc.c)
1355 			arc.p = arc.c;
1356 	}
1357 	ASSERT((int64_t)arc.p >= 0);
1358 }
1359 
1360 /*
1361  * Check if the cache has reached its limits and eviction is required
1362  * prior to insert.
1363  */
1364 static int
1365 arc_evict_needed()
1366 {
1367 	if (arc_reclaim_needed())
1368 		return (1);
1369 
1370 	return (arc.size > arc.c);
1371 }
1372 
1373 /*
1374  * The state, supplied as the first argument, is going to have something
1375  * inserted on its behalf. So, determine which cache must be victimized to
1376  * satisfy an insertion for this state.  We have the following cases:
1377  *
1378  * 1. Insert for MRU, p > sizeof(arc.anon + arc.mru) ->
1379  * In this situation if we're out of space, but the resident size of the MFU is
1380  * under the limit, victimize the MFU cache to satisfy this insertion request.
1381  *
1382  * 2. Insert for MRU, p <= sizeof(arc.anon + arc.mru) ->
1383  * Here, we've used up all of the available space for the MRU, so we need to
1384  * evict from our own cache instead.  Evict from the set of resident MRU
1385  * entries.
1386  *
1387  * 3. Insert for MFU (c - p) > sizeof(arc.mfu) ->
1388  * c minus p represents the MFU space in the cache, since p is the size of the
1389  * cache that is dedicated to the MRU.  In this situation there's still space on
1390  * the MFU side, so the MRU side needs to be victimized.
1391  *
1392  * 4. Insert for MFU (c - p) < sizeof(arc.mfu) ->
1393  * MFU's resident set is consuming more space than it has been allotted.  In
1394  * this situation, we must victimize our own cache, the MFU, for this insertion.
1395  */
1396 static void
1397 arc_evict_for_state(arc_state_t *state, uint64_t bytes)
1398 {
1399 	uint64_t	mru_used;
1400 	uint64_t	mfu_space;
1401 	uint64_t	evicted;
1402 
1403 	ASSERT(state == arc.mru || state == arc.mfu);
1404 
1405 	if (state == arc.mru) {
1406 		mru_used = arc.anon->size + arc.mru->size;
1407 		if (arc.p > mru_used) {
1408 			/* case 1 */
1409 			evicted = arc_evict(arc.mfu, bytes);
1410 			if (evicted < bytes) {
1411 				arc_adjust();
1412 			}
1413 		} else {
1414 			/* case 2 */
1415 			evicted = arc_evict(arc.mru, bytes);
1416 			if (evicted < bytes) {
1417 				arc_adjust();
1418 			}
1419 		}
1420 	} else {
1421 		/* MFU case */
1422 		mfu_space = arc.c - arc.p;
1423 		if (mfu_space > arc.mfu->size) {
1424 			/* case 3 */
1425 			evicted = arc_evict(arc.mru, bytes);
1426 			if (evicted < bytes) {
1427 				arc_adjust();
1428 			}
1429 		} else {
1430 			/* case 4 */
1431 			evicted = arc_evict(arc.mfu, bytes);
1432 			if (evicted < bytes) {
1433 				arc_adjust();
1434 			}
1435 		}
1436 	}
1437 }
1438 
1439 /*
1440  * This routine is called whenever a buffer is accessed.
1441  * NOTE: the hash lock is dropped in this function.
1442  */
1443 static void
1444 arc_access_and_exit(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
1445 {
1446 	arc_state_t	*evict_state = NULL;
1447 	int		blksz;
1448 
1449 	ASSERT(MUTEX_HELD(hash_lock));
1450 
1451 	blksz = buf->b_size;
1452 
1453 	if (buf->b_state == arc.anon) {
1454 		/*
1455 		 * This buffer is not in the cache, and does not
1456 		 * appear in our "ghost" list.  Add the new buffer
1457 		 * to the MRU state.
1458 		 */
1459 
1460 		arc_adapt(blksz, arc.anon);
1461 		if (arc_evict_needed())
1462 			evict_state = arc.mru;
1463 
1464 		ASSERT(buf->b_arc_access == 0);
1465 		buf->b_arc_access = lbolt;
1466 		DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
1467 		arc_change_state(arc.mru, buf, hash_lock);
1468 
1469 	} else if (buf->b_state == arc.mru) {
1470 		/*
1471 		 * If this buffer is in the MRU-top state and has the prefetch
1472 		 * flag, the first read was actually part of a prefetch.  In
1473 		 * this situation, we simply want to clear the flag and return.
1474 		 * A subsequent access should bump this into the MFU state.
1475 		 */
1476 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
1477 			buf->b_flags &= ~ARC_PREFETCH;
1478 			atomic_add_64(&arc.mru->hits, 1);
1479 			mutex_exit(hash_lock);
1480 			return;
1481 		}
1482 
1483 		/*
1484 		 * This buffer has been "accessed" only once so far,
1485 		 * but it is still in the cache. Move it to the MFU
1486 		 * state.
1487 		 */
1488 		if (lbolt > buf->b_arc_access + ARC_MINTIME) {
1489 			/*
1490 			 * More than 125ms have passed since we
1491 			 * instantiated this buffer.  Move it to the
1492 			 * most frequently used state.
1493 			 */
1494 			buf->b_arc_access = lbolt;
1495 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
1496 			arc_change_state(arc.mfu, buf, hash_lock);
1497 		}
1498 		atomic_add_64(&arc.mru->hits, 1);
1499 	} else if (buf->b_state == arc.mru_ghost) {
1500 		arc_state_t	*new_state;
1501 		/*
1502 		 * This buffer has been "accessed" recently, but
1503 		 * was evicted from the cache.  Move it to the
1504 		 * MFU state.
1505 		 */
1506 
1507 		if (buf->b_flags & ARC_PREFETCH) {
1508 			new_state = arc.mru;
1509 			buf->b_flags &= ~ARC_PREFETCH;
1510 			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
1511 		} else {
1512 			new_state = arc.mfu;
1513 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
1514 		}
1515 
1516 		arc_adapt(blksz, arc.mru_ghost);
1517 		if (arc_evict_needed())
1518 			evict_state = new_state;
1519 
1520 		buf->b_arc_access = lbolt;
1521 		arc_change_state(new_state, buf, hash_lock);
1522 
1523 		atomic_add_64(&arc.mru_ghost->hits, 1);
1524 	} else if (buf->b_state == arc.mfu) {
1525 		/*
1526 		 * This buffer has been accessed more than once and is
1527 		 * still in the cache.  Keep it in the MFU state.
1528 		 *
1529 		 * NOTE: the add_reference() that occurred when we did
1530 		 * the arc_read() should have kicked this off the list,
1531 		 * so even if it was a prefetch, it will be put back at
1532 		 * the head of the list when we remove_reference().
1533 		 */
1534 		atomic_add_64(&arc.mfu->hits, 1);
1535 	} else if (buf->b_state == arc.mfu_ghost) {
1536 		/*
1537 		 * This buffer has been accessed more than once but has
1538 		 * been evicted from the cache.  Move it back to the
1539 		 * MFU state.
1540 		 */
1541 
1542 		arc_adapt(blksz, arc.mfu_ghost);
1543 		if (arc_evict_needed())
1544 			evict_state = arc.mfu;
1545 
1546 		buf->b_arc_access = lbolt;
1547 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
1548 		arc_change_state(arc.mfu, buf, hash_lock);
1549 
1550 		atomic_add_64(&arc.mfu_ghost->hits, 1);
1551 	} else {
1552 		ASSERT(!"invalid arc state");
1553 	}
1554 
1555 	mutex_exit(hash_lock);
1556 	if (evict_state)
1557 		arc_evict_for_state(evict_state, blksz);
1558 }
1559 
1560 /* a generic arc_done_func_t which you can use */
1561 /* ARGSUSED */
1562 void
1563 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
1564 {
1565 	bcopy(buf->b_data, arg, buf->b_hdr->b_size);
1566 	VERIFY(arc_buf_remove_ref(buf, arg) == 1);
1567 }
1568 
1569 /* a generic arc_done_func_t which you can use */
1570 void
1571 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
1572 {
1573 	arc_buf_t **bufp = arg;
1574 	if (zio && zio->io_error) {
1575 		VERIFY(arc_buf_remove_ref(buf, arg) == 1);
1576 		*bufp = NULL;
1577 	} else {
1578 		*bufp = buf;
1579 	}
1580 }
1581 
1582 static void
1583 arc_read_done(zio_t *zio)
1584 {
1585 	arc_buf_hdr_t	*hdr, *found;
1586 	arc_buf_t	*buf;
1587 	arc_buf_t	*abuf;	/* buffer we're assigning to callback */
1588 	kmutex_t	*hash_lock;
1589 	arc_callback_t	*callback_list, *acb;
1590 	int		freeable = FALSE;
1591 
1592 	buf = zio->io_private;
1593 	hdr = buf->b_hdr;
1594 
1595 	/*
1596 	 * The hdr was inserted into hash-table and removed from lists
1597 	 * prior to starting I/O.  We should find this header, since
1598 	 * it's in the hash table, and it should be legit since it's
1599 	 * not possible to evict it during the I/O.  The only possible
1600 	 * reason for it not to be found is if we were freed during the
1601 	 * read.
1602 	 */
1603 	found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth,
1604 		    &hash_lock);
1605 
1606 	ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
1607 	    (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))));
1608 
1609 	/* byteswap if necessary */
1610 	callback_list = hdr->b_acb;
1611 	ASSERT(callback_list != NULL);
1612 	if (BP_SHOULD_BYTESWAP(zio->io_bp) && callback_list->acb_byteswap)
1613 		callback_list->acb_byteswap(buf->b_data, hdr->b_size);
1614 
1615 	/* create copies of the data buffer for the callers */
1616 	abuf = buf;
1617 	for (acb = callback_list; acb; acb = acb->acb_next) {
1618 		if (acb->acb_done) {
1619 			if (abuf == NULL) {
1620 				abuf = kmem_cache_alloc(buf_cache, KM_SLEEP);
1621 				abuf->b_data = arc_data_copy(hdr, buf->b_data);
1622 				abuf->b_hdr = hdr;
1623 				abuf->b_efunc = NULL;
1624 				abuf->b_private = NULL;
1625 				abuf->b_next = hdr->b_buf;
1626 				hdr->b_buf = abuf;
1627 				hdr->b_datacnt += 1;
1628 			}
1629 			acb->acb_buf = abuf;
1630 			abuf = NULL;
1631 		} else {
1632 			/*
1633 			 * The caller did not provide a callback function.
1634 			 * In this case, we should just remove the reference.
1635 			 */
1636 			if (HDR_FREED_IN_READ(hdr)) {
1637 				ASSERT3P(hdr->b_state, ==, arc.anon);
1638 				(void) refcount_remove(&hdr->b_refcnt,
1639 				    acb->acb_private);
1640 			} else {
1641 				(void) remove_reference(hdr, hash_lock,
1642 				    acb->acb_private);
1643 			}
1644 		}
1645 	}
1646 	hdr->b_acb = NULL;
1647 	hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
1648 	ASSERT(!HDR_BUF_AVAILABLE(hdr));
1649 	if (abuf == buf)
1650 		hdr->b_flags |= ARC_BUF_AVAILABLE;
1651 
1652 	ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
1653 
1654 	if (zio->io_error != 0) {
1655 		hdr->b_flags |= ARC_IO_ERROR;
1656 		if (hdr->b_state != arc.anon)
1657 			arc_change_state(arc.anon, hdr, hash_lock);
1658 		if (HDR_IN_HASH_TABLE(hdr))
1659 			buf_hash_remove(hdr);
1660 		freeable = refcount_is_zero(&hdr->b_refcnt);
1661 		/* translate checksum errors into IO errors */
1662 		if (zio->io_error == ECKSUM)
1663 			zio->io_error = EIO;
1664 	}
1665 
1666 	/*
1667 	 * Broadcast before we drop the hash_lock.  This is less efficient,
1668 	 * but avoids the possibility that the hdr (and hence the cv) might
1669 	 * be freed before we get to the cv_broadcast().
1670 	 */
1671 	cv_broadcast(&hdr->b_cv);
1672 
1673 	if (hash_lock) {
1674 		/*
1675 		 * Only call arc_access on anonymous buffers.  This is because
1676 		 * if we've issued an I/O for an evicted buffer, we've already
1677 		 * called arc_access (to prevent any simultaneous readers from
1678 		 * getting confused).
1679 		 */
1680 		if (zio->io_error == 0 && hdr->b_state == arc.anon)
1681 			arc_access_and_exit(hdr, hash_lock);
1682 		else
1683 			mutex_exit(hash_lock);
1684 	} else {
1685 		/*
1686 		 * This block was freed while we waited for the read to
1687 		 * complete.  It has been removed from the hash table and
1688 		 * moved to the anonymous state (so that it won't show up
1689 		 * in the cache).
1690 		 */
1691 		ASSERT3P(hdr->b_state, ==, arc.anon);
1692 		freeable = refcount_is_zero(&hdr->b_refcnt);
1693 	}
1694 
1695 	/* execute each callback and free its structure */
1696 	while ((acb = callback_list) != NULL) {
1697 		if (acb->acb_done)
1698 			acb->acb_done(zio, acb->acb_buf, acb->acb_private);
1699 
1700 		if (acb->acb_zio_dummy != NULL) {
1701 			acb->acb_zio_dummy->io_error = zio->io_error;
1702 			zio_nowait(acb->acb_zio_dummy);
1703 		}
1704 
1705 		callback_list = acb->acb_next;
1706 		kmem_free(acb, sizeof (arc_callback_t));
1707 	}
1708 
1709 	if (freeable)
1710 		arc_hdr_destroy(hdr);
1711 }
1712 
1713 /*
1714  * "Read" the block block at the specified DVA (in bp) via the
1715  * cache.  If the block is found in the cache, invoke the provided
1716  * callback immediately and return.  Note that the `zio' parameter
1717  * in the callback will be NULL in this case, since no IO was
1718  * required.  If the block is not in the cache pass the read request
1719  * on to the spa with a substitute callback function, so that the
1720  * requested block will be added to the cache.
1721  *
1722  * If a read request arrives for a block that has a read in-progress,
1723  * either wait for the in-progress read to complete (and return the
1724  * results); or, if this is a read with a "done" func, add a record
1725  * to the read to invoke the "done" func when the read completes,
1726  * and return; or just return.
1727  *
1728  * arc_read_done() will invoke all the requested "done" functions
1729  * for readers of this block.
1730  */
1731 int
1732 arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_byteswap_func_t *swap,
1733     arc_done_func_t *done, void *private, int priority, int flags,
1734     uint32_t arc_flags, zbookmark_t *zb)
1735 {
1736 	arc_buf_hdr_t *hdr;
1737 	arc_buf_t *buf;
1738 	kmutex_t *hash_lock;
1739 	zio_t	*rzio;
1740 
1741 top:
1742 	hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock);
1743 	if (hdr && hdr->b_datacnt > 0) {
1744 
1745 		if (HDR_IO_IN_PROGRESS(hdr)) {
1746 			if ((arc_flags & ARC_NOWAIT) && done) {
1747 				arc_callback_t	*acb = NULL;
1748 
1749 				acb = kmem_zalloc(sizeof (arc_callback_t),
1750 				    KM_SLEEP);
1751 				acb->acb_done = done;
1752 				acb->acb_private = private;
1753 				acb->acb_byteswap = swap;
1754 				if (pio != NULL)
1755 					acb->acb_zio_dummy = zio_null(pio,
1756 					    spa, NULL, NULL, flags);
1757 
1758 				ASSERT(acb->acb_done != NULL);
1759 				acb->acb_next = hdr->b_acb;
1760 				hdr->b_acb = acb;
1761 				add_reference(hdr, hash_lock, private);
1762 				mutex_exit(hash_lock);
1763 				return (0);
1764 			} else if (arc_flags & ARC_WAIT) {
1765 				cv_wait(&hdr->b_cv, hash_lock);
1766 				mutex_exit(hash_lock);
1767 				goto top;
1768 			}
1769 			mutex_exit(hash_lock);
1770 			return (0);
1771 		}
1772 
1773 		ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu);
1774 
1775 		if (done) {
1776 			/*
1777 			 * If this block is already in use, create a new
1778 			 * copy of the data so that we will be guaranteed
1779 			 * that arc_release() will always succeed.
1780 			 */
1781 			buf = hdr->b_buf;
1782 			ASSERT(buf);
1783 			ASSERT(buf->b_data);
1784 			if (!HDR_BUF_AVAILABLE(hdr)) {
1785 				void *data = arc_data_copy(hdr, buf->b_data);
1786 				buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
1787 				buf->b_hdr = hdr;
1788 				buf->b_data = data;
1789 				buf->b_efunc = NULL;
1790 				buf->b_private = NULL;
1791 				buf->b_next = hdr->b_buf;
1792 				hdr->b_buf = buf;
1793 				hdr->b_datacnt += 1;
1794 			} else {
1795 				ASSERT(buf->b_efunc == NULL);
1796 				hdr->b_flags &= ~ARC_BUF_AVAILABLE;
1797 			}
1798 			add_reference(hdr, hash_lock, private);
1799 		}
1800 		DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1801 		arc_access_and_exit(hdr, hash_lock);
1802 		atomic_add_64(&arc.hits, 1);
1803 		if (done)
1804 			done(NULL, buf, private);
1805 	} else {
1806 		uint64_t size = BP_GET_LSIZE(bp);
1807 		arc_callback_t	*acb;
1808 
1809 		if (hdr == NULL) {
1810 			/* this block is not in the cache */
1811 			arc_buf_hdr_t	*exists;
1812 
1813 			buf = arc_buf_alloc(spa, size, private);
1814 			hdr = buf->b_hdr;
1815 			hdr->b_dva = *BP_IDENTITY(bp);
1816 			hdr->b_birth = bp->blk_birth;
1817 			hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
1818 			exists = buf_hash_insert(hdr, &hash_lock);
1819 			if (exists) {
1820 				/* somebody beat us to the hash insert */
1821 				mutex_exit(hash_lock);
1822 				bzero(&hdr->b_dva, sizeof (dva_t));
1823 				hdr->b_birth = 0;
1824 				hdr->b_cksum0 = 0;
1825 				(void) arc_buf_remove_ref(buf, private);
1826 				goto top; /* restart the IO request */
1827 			}
1828 
1829 		} else {
1830 			/* this block is in the ghost cache */
1831 			ASSERT(GHOST_STATE(hdr->b_state));
1832 			ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1833 			add_reference(hdr, hash_lock, private);
1834 			ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1);
1835 
1836 			ASSERT(hdr->b_buf == NULL);
1837 			buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
1838 			buf->b_hdr = hdr;
1839 			buf->b_efunc = NULL;
1840 			buf->b_private = NULL;
1841 			buf->b_next = NULL;
1842 			hdr->b_buf = buf;
1843 			buf->b_data = zio_buf_alloc(hdr->b_size);
1844 			atomic_add_64(&arc.size, hdr->b_size);
1845 			ASSERT(hdr->b_datacnt == 0);
1846 			hdr->b_datacnt = 1;
1847 		}
1848 
1849 		acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
1850 		acb->acb_done = done;
1851 		acb->acb_private = private;
1852 		acb->acb_byteswap = swap;
1853 
1854 		ASSERT(hdr->b_acb == NULL);
1855 		hdr->b_acb = acb;
1856 
1857 		/*
1858 		 * If this DVA is part of a prefetch, mark the buf
1859 		 * header with the prefetch flag
1860 		 */
1861 		if (arc_flags & ARC_PREFETCH)
1862 			hdr->b_flags |= ARC_PREFETCH;
1863 		hdr->b_flags |= ARC_IO_IN_PROGRESS;
1864 
1865 		/*
1866 		 * If the buffer has been evicted, migrate it to a present state
1867 		 * before issuing the I/O.  Once we drop the hash-table lock,
1868 		 * the header will be marked as I/O in progress and have an
1869 		 * attached buffer.  At this point, anybody who finds this
1870 		 * buffer ought to notice that it's legit but has a pending I/O.
1871 		 */
1872 
1873 		if (GHOST_STATE(hdr->b_state))
1874 			arc_access_and_exit(hdr, hash_lock);
1875 		else
1876 			mutex_exit(hash_lock);
1877 
1878 		ASSERT3U(hdr->b_size, ==, size);
1879 		DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size,
1880 		    zbookmark_t *, zb);
1881 		atomic_add_64(&arc.misses, 1);
1882 
1883 		rzio = zio_read(pio, spa, bp, buf->b_data, size,
1884 		    arc_read_done, buf, priority, flags, zb);
1885 
1886 		if (arc_flags & ARC_WAIT)
1887 			return (zio_wait(rzio));
1888 
1889 		ASSERT(arc_flags & ARC_NOWAIT);
1890 		zio_nowait(rzio);
1891 	}
1892 	return (0);
1893 }
1894 
1895 /*
1896  * arc_read() variant to support pool traversal.  If the block is already
1897  * in the ARC, make a copy of it; otherwise, the caller will do the I/O.
1898  * The idea is that we don't want pool traversal filling up memory, but
1899  * if the ARC already has the data anyway, we shouldn't pay for the I/O.
1900  */
1901 int
1902 arc_tryread(spa_t *spa, blkptr_t *bp, void *data)
1903 {
1904 	arc_buf_hdr_t *hdr;
1905 	kmutex_t *hash_mtx;
1906 	int rc = 0;
1907 
1908 	hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx);
1909 
1910 	if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) {
1911 		arc_buf_t *buf = hdr->b_buf;
1912 
1913 		ASSERT(buf);
1914 		while (buf->b_data == NULL) {
1915 			buf = buf->b_next;
1916 			ASSERT(buf);
1917 		}
1918 		bcopy(buf->b_data, data, hdr->b_size);
1919 	} else {
1920 		rc = ENOENT;
1921 	}
1922 
1923 	if (hash_mtx)
1924 		mutex_exit(hash_mtx);
1925 
1926 	return (rc);
1927 }
1928 
1929 void
1930 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
1931 {
1932 	ASSERT(buf->b_hdr != NULL);
1933 	ASSERT(buf->b_hdr->b_state != arc.anon);
1934 	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
1935 	buf->b_efunc = func;
1936 	buf->b_private = private;
1937 }
1938 
1939 /*
1940  * This is used by the DMU to let the ARC know that a buffer is
1941  * being evicted, so the ARC should clean up.  If this arc buf
1942  * is not yet in the evicted state, it will be put there.
1943  */
1944 int
1945 arc_buf_evict(arc_buf_t *buf)
1946 {
1947 	arc_buf_hdr_t *hdr;
1948 	kmutex_t *hash_lock;
1949 	arc_buf_t **bufp;
1950 
1951 	mutex_enter(&arc_eviction_mtx);
1952 	hdr = buf->b_hdr;
1953 	if (hdr == NULL) {
1954 		/*
1955 		 * We are in arc_do_user_evicts().
1956 		 * NOTE: We can't be in arc_buf_add_ref() because
1957 		 * that would violate the interface rules.
1958 		 */
1959 		ASSERT(buf->b_data == NULL);
1960 		mutex_exit(&arc_eviction_mtx);
1961 		return (0);
1962 	} else if (buf->b_data == NULL) {
1963 		arc_buf_t copy = *buf; /* structure assignment */
1964 		/*
1965 		 * We are on the eviction list.  Process this buffer
1966 		 * now but let arc_do_user_evicts() do the reaping.
1967 		 */
1968 		buf->b_efunc = NULL;
1969 		buf->b_hdr = NULL;
1970 		mutex_exit(&arc_eviction_mtx);
1971 		VERIFY(copy.b_efunc(&copy) == 0);
1972 		return (1);
1973 	} else {
1974 		/*
1975 		 * Prevent a race with arc_evict()
1976 		 */
1977 		ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
1978 		buf->b_hdr = NULL;
1979 	}
1980 	mutex_exit(&arc_eviction_mtx);
1981 
1982 	hash_lock = HDR_LOCK(hdr);
1983 	mutex_enter(hash_lock);
1984 
1985 	ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu);
1986 
1987 	/*
1988 	 * Pull this buffer off of the hdr
1989 	 */
1990 	bufp = &hdr->b_buf;
1991 	while (*bufp != buf)
1992 		bufp = &(*bufp)->b_next;
1993 	*bufp = buf->b_next;
1994 
1995 	ASSERT(buf->b_data != NULL);
1996 	buf->b_hdr = hdr;
1997 	arc_buf_destroy(buf, FALSE);
1998 
1999 	if (hdr->b_datacnt == 0) {
2000 		arc_state_t *old_state = hdr->b_state;
2001 		arc_state_t *evicted_state;
2002 
2003 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
2004 
2005 		evicted_state =
2006 		    (old_state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost;
2007 
2008 		mutex_enter(&old_state->mtx);
2009 		mutex_enter(&evicted_state->mtx);
2010 
2011 		arc_change_state(evicted_state, hdr, hash_lock);
2012 		ASSERT(HDR_IN_HASH_TABLE(hdr));
2013 		hdr->b_flags = ARC_IN_HASH_TABLE;
2014 
2015 		mutex_exit(&evicted_state->mtx);
2016 		mutex_exit(&old_state->mtx);
2017 	}
2018 	mutex_exit(hash_lock);
2019 
2020 	VERIFY(buf->b_efunc(buf) == 0);
2021 	buf->b_efunc = NULL;
2022 	buf->b_private = NULL;
2023 	buf->b_hdr = NULL;
2024 	kmem_cache_free(buf_cache, buf);
2025 	return (1);
2026 }
2027 
2028 /*
2029  * Release this buffer from the cache.  This must be done
2030  * after a read and prior to modifying the buffer contents.
2031  * If the buffer has more than one reference, we must make
2032  * make a new hdr for the buffer.
2033  */
2034 void
2035 arc_release(arc_buf_t *buf, void *tag)
2036 {
2037 	arc_buf_hdr_t *hdr = buf->b_hdr;
2038 	kmutex_t *hash_lock = HDR_LOCK(hdr);
2039 
2040 	/* this buffer is not on any list */
2041 	ASSERT(refcount_count(&hdr->b_refcnt) > 0);
2042 
2043 	if (hdr->b_state == arc.anon) {
2044 		/* this buffer is already released */
2045 		ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1);
2046 		ASSERT(BUF_EMPTY(hdr));
2047 		ASSERT(buf->b_efunc == NULL);
2048 		return;
2049 	}
2050 
2051 	mutex_enter(hash_lock);
2052 
2053 	/*
2054 	 * Do we have more than one buf?
2055 	 */
2056 	if (hdr->b_buf != buf || buf->b_next != NULL) {
2057 		arc_buf_hdr_t *nhdr;
2058 		arc_buf_t **bufp;
2059 		uint64_t blksz = hdr->b_size;
2060 		spa_t *spa = hdr->b_spa;
2061 
2062 		ASSERT(hdr->b_datacnt > 1);
2063 		/*
2064 		 * Pull the data off of this buf and attach it to
2065 		 * a new anonymous buf.
2066 		 */
2067 		(void) remove_reference(hdr, hash_lock, tag);
2068 		bufp = &hdr->b_buf;
2069 		while (*bufp != buf)
2070 			bufp = &(*bufp)->b_next;
2071 		*bufp = (*bufp)->b_next;
2072 
2073 		ASSERT3U(hdr->b_state->size, >=, hdr->b_size);
2074 		atomic_add_64(&hdr->b_state->size, -hdr->b_size);
2075 		if (refcount_is_zero(&hdr->b_refcnt)) {
2076 			ASSERT3U(hdr->b_state->lsize, >=, hdr->b_size);
2077 			atomic_add_64(&hdr->b_state->lsize, -hdr->b_size);
2078 		}
2079 		hdr->b_datacnt -= 1;
2080 
2081 		mutex_exit(hash_lock);
2082 
2083 		nhdr = kmem_cache_alloc(hdr_cache, KM_SLEEP);
2084 		nhdr->b_size = blksz;
2085 		nhdr->b_spa = spa;
2086 		nhdr->b_buf = buf;
2087 		nhdr->b_state = arc.anon;
2088 		nhdr->b_arc_access = 0;
2089 		nhdr->b_flags = 0;
2090 		nhdr->b_datacnt = 1;
2091 		buf->b_hdr = nhdr;
2092 		buf->b_next = NULL;
2093 		(void) refcount_add(&nhdr->b_refcnt, tag);
2094 		atomic_add_64(&arc.anon->size, blksz);
2095 
2096 		hdr = nhdr;
2097 	} else {
2098 		ASSERT(refcount_count(&hdr->b_refcnt) == 1);
2099 		ASSERT(!list_link_active(&hdr->b_arc_node));
2100 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
2101 		arc_change_state(arc.anon, hdr, hash_lock);
2102 		hdr->b_arc_access = 0;
2103 		mutex_exit(hash_lock);
2104 		bzero(&hdr->b_dva, sizeof (dva_t));
2105 		hdr->b_birth = 0;
2106 		hdr->b_cksum0 = 0;
2107 	}
2108 	buf->b_efunc = NULL;
2109 	buf->b_private = NULL;
2110 }
2111 
2112 int
2113 arc_released(arc_buf_t *buf)
2114 {
2115 	return (buf->b_data != NULL && buf->b_hdr->b_state == arc.anon);
2116 }
2117 
2118 int
2119 arc_has_callback(arc_buf_t *buf)
2120 {
2121 	return (buf->b_efunc != NULL);
2122 }
2123 
2124 #ifdef ZFS_DEBUG
2125 int
2126 arc_referenced(arc_buf_t *buf)
2127 {
2128 	return (refcount_count(&buf->b_hdr->b_refcnt));
2129 }
2130 #endif
2131 
2132 static void
2133 arc_write_done(zio_t *zio)
2134 {
2135 	arc_buf_t *buf;
2136 	arc_buf_hdr_t *hdr;
2137 	arc_callback_t *acb;
2138 
2139 	buf = zio->io_private;
2140 	hdr = buf->b_hdr;
2141 	acb = hdr->b_acb;
2142 	hdr->b_acb = NULL;
2143 	ASSERT(acb != NULL);
2144 
2145 	/* this buffer is on no lists and is not in the hash table */
2146 	ASSERT3P(hdr->b_state, ==, arc.anon);
2147 
2148 	hdr->b_dva = *BP_IDENTITY(zio->io_bp);
2149 	hdr->b_birth = zio->io_bp->blk_birth;
2150 	hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
2151 	/*
2152 	 * If the block to be written was all-zero, we may have
2153 	 * compressed it away.  In this case no write was performed
2154 	 * so there will be no dva/birth-date/checksum.  The buffer
2155 	 * must therefor remain anonymous (and uncached).
2156 	 */
2157 	if (!BUF_EMPTY(hdr)) {
2158 		arc_buf_hdr_t *exists;
2159 		kmutex_t *hash_lock;
2160 
2161 		exists = buf_hash_insert(hdr, &hash_lock);
2162 		if (exists) {
2163 			/*
2164 			 * This can only happen if we overwrite for
2165 			 * sync-to-convergence, because we remove
2166 			 * buffers from the hash table when we arc_free().
2167 			 */
2168 			ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig),
2169 			    BP_IDENTITY(zio->io_bp)));
2170 			ASSERT3U(zio->io_bp_orig.blk_birth, ==,
2171 			    zio->io_bp->blk_birth);
2172 
2173 			ASSERT(refcount_is_zero(&exists->b_refcnt));
2174 			arc_change_state(arc.anon, exists, hash_lock);
2175 			mutex_exit(hash_lock);
2176 			arc_hdr_destroy(exists);
2177 			exists = buf_hash_insert(hdr, &hash_lock);
2178 			ASSERT3P(exists, ==, NULL);
2179 		}
2180 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2181 		arc_access_and_exit(hdr, hash_lock);
2182 	} else if (acb->acb_done == NULL) {
2183 		int destroy_hdr;
2184 		/*
2185 		 * This is an anonymous buffer with no user callback,
2186 		 * destroy it if there are no active references.
2187 		 */
2188 		mutex_enter(&arc_eviction_mtx);
2189 		destroy_hdr = refcount_is_zero(&hdr->b_refcnt);
2190 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2191 		mutex_exit(&arc_eviction_mtx);
2192 		if (destroy_hdr)
2193 			arc_hdr_destroy(hdr);
2194 	} else {
2195 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2196 	}
2197 
2198 	if (acb->acb_done) {
2199 		ASSERT(!refcount_is_zero(&hdr->b_refcnt));
2200 		acb->acb_done(zio, buf, acb->acb_private);
2201 	}
2202 
2203 	kmem_free(acb, sizeof (arc_callback_t));
2204 }
2205 
2206 int
2207 arc_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies,
2208     uint64_t txg, blkptr_t *bp, arc_buf_t *buf,
2209     arc_done_func_t *done, void *private, int priority, int flags,
2210     uint32_t arc_flags, zbookmark_t *zb)
2211 {
2212 	arc_buf_hdr_t *hdr = buf->b_hdr;
2213 	arc_callback_t	*acb;
2214 	zio_t	*rzio;
2215 
2216 	/* this is a private buffer - no locking required */
2217 	ASSERT3P(hdr->b_state, ==, arc.anon);
2218 	ASSERT(BUF_EMPTY(hdr));
2219 	ASSERT(!HDR_IO_ERROR(hdr));
2220 	acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
2221 	acb->acb_done = done;
2222 	acb->acb_private = private;
2223 	acb->acb_byteswap = (arc_byteswap_func_t *)-1;
2224 	hdr->b_acb = acb;
2225 	hdr->b_flags |= ARC_IO_IN_PROGRESS;
2226 	rzio = zio_write(pio, spa, checksum, compress, ncopies, txg, bp,
2227 	    buf->b_data, hdr->b_size, arc_write_done, buf, priority, flags, zb);
2228 
2229 	if (arc_flags & ARC_WAIT)
2230 		return (zio_wait(rzio));
2231 
2232 	ASSERT(arc_flags & ARC_NOWAIT);
2233 	zio_nowait(rzio);
2234 
2235 	return (0);
2236 }
2237 
2238 int
2239 arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
2240     zio_done_func_t *done, void *private, uint32_t arc_flags)
2241 {
2242 	arc_buf_hdr_t *ab;
2243 	kmutex_t *hash_lock;
2244 	zio_t	*zio;
2245 
2246 	/*
2247 	 * If this buffer is in the cache, release it, so it
2248 	 * can be re-used.
2249 	 */
2250 	ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock);
2251 	if (ab != NULL) {
2252 		/*
2253 		 * The checksum of blocks to free is not always
2254 		 * preserved (eg. on the deadlist).  However, if it is
2255 		 * nonzero, it should match what we have in the cache.
2256 		 */
2257 		ASSERT(bp->blk_cksum.zc_word[0] == 0 ||
2258 		    ab->b_cksum0 == bp->blk_cksum.zc_word[0]);
2259 		if (ab->b_state != arc.anon)
2260 			arc_change_state(arc.anon, ab, hash_lock);
2261 		if (refcount_is_zero(&ab->b_refcnt)) {
2262 			mutex_exit(hash_lock);
2263 			arc_hdr_destroy(ab);
2264 			atomic_add_64(&arc.deleted, 1);
2265 		} else {
2266 			/*
2267 			 * We could have an outstanding read on this
2268 			 * block, so multiple active references are
2269 			 * possible.  But we should only have a single
2270 			 * data buffer associated at this point.
2271 			 */
2272 			ASSERT3U(ab->b_datacnt, ==, 1);
2273 			if (HDR_IO_IN_PROGRESS(ab))
2274 				ab->b_flags |= ARC_FREED_IN_READ;
2275 			if (HDR_IN_HASH_TABLE(ab))
2276 				buf_hash_remove(ab);
2277 			ab->b_arc_access = 0;
2278 			bzero(&ab->b_dva, sizeof (dva_t));
2279 			ab->b_birth = 0;
2280 			ab->b_cksum0 = 0;
2281 			ab->b_buf->b_efunc = NULL;
2282 			ab->b_buf->b_private = NULL;
2283 			mutex_exit(hash_lock);
2284 		}
2285 	}
2286 
2287 	zio = zio_free(pio, spa, txg, bp, done, private);
2288 
2289 	if (arc_flags & ARC_WAIT)
2290 		return (zio_wait(zio));
2291 
2292 	ASSERT(arc_flags & ARC_NOWAIT);
2293 	zio_nowait(zio);
2294 
2295 	return (0);
2296 }
2297 
2298 void
2299 arc_tempreserve_clear(uint64_t tempreserve)
2300 {
2301 	atomic_add_64(&arc_tempreserve, -tempreserve);
2302 	ASSERT((int64_t)arc_tempreserve >= 0);
2303 }
2304 
2305 int
2306 arc_tempreserve_space(uint64_t tempreserve)
2307 {
2308 #ifdef ZFS_DEBUG
2309 	/*
2310 	 * Once in a while, fail for no reason.  Everything should cope.
2311 	 */
2312 	if (spa_get_random(10000) == 0) {
2313 		dprintf("forcing random failure\n");
2314 		return (ERESTART);
2315 	}
2316 #endif
2317 	if (tempreserve > arc.c/4 && !arc.no_grow)
2318 		arc.c = MIN(arc.c_max, tempreserve * 4);
2319 	if (tempreserve > arc.c)
2320 		return (ENOMEM);
2321 
2322 	/*
2323 	 * Throttle writes when the amount of dirty data in the cache
2324 	 * gets too large.  We try to keep the cache less than half full
2325 	 * of dirty blocks so that our sync times don't grow too large.
2326 	 * Note: if two requests come in concurrently, we might let them
2327 	 * both succeed, when one of them should fail.  Not a huge deal.
2328 	 *
2329 	 * XXX The limit should be adjusted dynamically to keep the time
2330 	 * to sync a dataset fixed (around 1-5 seconds?).
2331 	 */
2332 
2333 	if (tempreserve + arc_tempreserve + arc.anon->size > arc.c / 2 &&
2334 	    arc_tempreserve + arc.anon->size > arc.c / 4) {
2335 		dprintf("failing, arc_tempreserve=%lluK anon=%lluK "
2336 		    "tempreserve=%lluK arc.c=%lluK\n",
2337 		    arc_tempreserve>>10, arc.anon->lsize>>10,
2338 		    tempreserve>>10, arc.c>>10);
2339 		return (ERESTART);
2340 	}
2341 	atomic_add_64(&arc_tempreserve, tempreserve);
2342 	return (0);
2343 }
2344 
2345 void
2346 arc_init(void)
2347 {
2348 	mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
2349 	mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
2350 	cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
2351 
2352 	/* Start out with 1/8 of all memory */
2353 	arc.c = physmem * PAGESIZE / 8;
2354 
2355 #ifdef _KERNEL
2356 	/*
2357 	 * On architectures where the physical memory can be larger
2358 	 * than the addressable space (intel in 32-bit mode), we may
2359 	 * need to limit the cache to 1/8 of VM size.
2360 	 */
2361 	arc.c = MIN(arc.c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
2362 #endif
2363 
2364 	/* set min cache to 1/32 of all memory, or 64MB, whichever is more */
2365 	arc.c_min = MAX(arc.c / 4, 64<<20);
2366 	/* set max to 3/4 of all memory, or all but 1GB, whichever is more */
2367 	if (arc.c * 8 >= 1<<30)
2368 		arc.c_max = (arc.c * 8) - (1<<30);
2369 	else
2370 		arc.c_max = arc.c_min;
2371 	arc.c_max = MAX(arc.c * 6, arc.c_max);
2372 	arc.c = arc.c_max;
2373 	arc.p = (arc.c >> 1);
2374 
2375 	/* if kmem_flags are set, lets try to use less memory */
2376 	if (kmem_debugging())
2377 		arc.c = arc.c / 2;
2378 	if (arc.c < arc.c_min)
2379 		arc.c = arc.c_min;
2380 
2381 	arc.anon = &ARC_anon;
2382 	arc.mru = &ARC_mru;
2383 	arc.mru_ghost = &ARC_mru_ghost;
2384 	arc.mfu = &ARC_mfu;
2385 	arc.mfu_ghost = &ARC_mfu_ghost;
2386 	arc.size = 0;
2387 
2388 	list_create(&arc.mru->list, sizeof (arc_buf_hdr_t),
2389 	    offsetof(arc_buf_hdr_t, b_arc_node));
2390 	list_create(&arc.mru_ghost->list, sizeof (arc_buf_hdr_t),
2391 	    offsetof(arc_buf_hdr_t, b_arc_node));
2392 	list_create(&arc.mfu->list, sizeof (arc_buf_hdr_t),
2393 	    offsetof(arc_buf_hdr_t, b_arc_node));
2394 	list_create(&arc.mfu_ghost->list, sizeof (arc_buf_hdr_t),
2395 	    offsetof(arc_buf_hdr_t, b_arc_node));
2396 
2397 	buf_init();
2398 
2399 	arc_thread_exit = 0;
2400 	arc_eviction_list = NULL;
2401 	mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
2402 
2403 	(void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
2404 	    TS_RUN, minclsyspri);
2405 }
2406 
2407 void
2408 arc_fini(void)
2409 {
2410 	mutex_enter(&arc_reclaim_thr_lock);
2411 	arc_thread_exit = 1;
2412 	while (arc_thread_exit != 0)
2413 		cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
2414 	mutex_exit(&arc_reclaim_thr_lock);
2415 
2416 	arc_flush();
2417 
2418 	arc_dead = TRUE;
2419 
2420 	mutex_destroy(&arc_eviction_mtx);
2421 	mutex_destroy(&arc_reclaim_lock);
2422 	mutex_destroy(&arc_reclaim_thr_lock);
2423 	cv_destroy(&arc_reclaim_thr_cv);
2424 
2425 	list_destroy(&arc.mru->list);
2426 	list_destroy(&arc.mru_ghost->list);
2427 	list_destroy(&arc.mfu->list);
2428 	list_destroy(&arc.mfu_ghost->list);
2429 
2430 	buf_fini();
2431 }
2432