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