xref: /titanic_50/usr/src/uts/common/fs/zfs/arc.c (revision 921e7e07108d1e3f09fecb1805fa2c79bb584fed)
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 		if (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 	uint64_t to_free;
1114 
1115 	/* Remove 12.5% */
1116 	/*
1117 	 * We need arc_reclaim_lock because we don't want multiple
1118 	 * threads trying to reclaim concurrently.
1119 	 */
1120 
1121 	/*
1122 	 * umem calls the reclaim func when we destroy the buf cache,
1123 	 * which is after we do arc_fini().  So we set a flag to prevent
1124 	 * accessing the destroyed mutexes and lists.
1125 	 */
1126 	if (arc_dead)
1127 		return;
1128 
1129 	if (arc.c <= arc.c_min)
1130 		return;
1131 
1132 	mutex_enter(&arc_reclaim_lock);
1133 
1134 #ifdef _KERNEL
1135 	to_free = MAX(arc.c >> 3, ptob(needfree));
1136 #else
1137 	to_free = arc.c >> 3;
1138 #endif
1139 	if (arc.c > to_free)
1140 		atomic_add_64(&arc.c, -to_free);
1141 	else
1142 		arc.c = arc.c_min;
1143 
1144 	atomic_add_64(&arc.p, -(arc.p >> 3));
1145 	if (arc.c > arc.size)
1146 		arc.c = arc.size;
1147 	if (arc.c < arc.c_min)
1148 		arc.c = arc.c_min;
1149 	if (arc.p > arc.c)
1150 		arc.p = (arc.c >> 1);
1151 	ASSERT((int64_t)arc.p >= 0);
1152 
1153 	arc_adjust();
1154 
1155 	mutex_exit(&arc_reclaim_lock);
1156 }
1157 
1158 static int
1159 arc_reclaim_needed(void)
1160 {
1161 	uint64_t extra;
1162 
1163 #ifdef _KERNEL
1164 
1165 	if (needfree)
1166 		return (1);
1167 
1168 	/*
1169 	 * take 'desfree' extra pages, so we reclaim sooner, rather than later
1170 	 */
1171 	extra = desfree;
1172 
1173 	/*
1174 	 * check that we're out of range of the pageout scanner.  It starts to
1175 	 * schedule paging if freemem is less than lotsfree and needfree.
1176 	 * lotsfree is the high-water mark for pageout, and needfree is the
1177 	 * number of needed free pages.  We add extra pages here to make sure
1178 	 * the scanner doesn't start up while we're freeing memory.
1179 	 */
1180 	if (freemem < lotsfree + needfree + extra)
1181 		return (1);
1182 
1183 	/*
1184 	 * check to make sure that swapfs has enough space so that anon
1185 	 * reservations can still succeeed. anon_resvmem() checks that the
1186 	 * availrmem is greater than swapfs_minfree, and the number of reserved
1187 	 * swap pages.  We also add a bit of extra here just to prevent
1188 	 * circumstances from getting really dire.
1189 	 */
1190 	if (availrmem < swapfs_minfree + swapfs_reserve + extra)
1191 		return (1);
1192 
1193 #if defined(__i386)
1194 	/*
1195 	 * If we're on an i386 platform, it's possible that we'll exhaust the
1196 	 * kernel heap space before we ever run out of available physical
1197 	 * memory.  Most checks of the size of the heap_area compare against
1198 	 * tune.t_minarmem, which is the minimum available real memory that we
1199 	 * can have in the system.  However, this is generally fixed at 25 pages
1200 	 * which is so low that it's useless.  In this comparison, we seek to
1201 	 * calculate the total heap-size, and reclaim if more than 3/4ths of the
1202 	 * heap is allocated.  (Or, in the caclulation, if less than 1/4th is
1203 	 * free)
1204 	 */
1205 	if (btop(vmem_size(heap_arena, VMEM_FREE)) <
1206 	    (btop(vmem_size(heap_arena, VMEM_FREE | VMEM_ALLOC)) >> 2))
1207 		return (1);
1208 #endif
1209 
1210 #else
1211 	if (spa_get_random(100) == 0)
1212 		return (1);
1213 #endif
1214 	return (0);
1215 }
1216 
1217 static void
1218 arc_kmem_reap_now(arc_reclaim_strategy_t strat)
1219 {
1220 	size_t			i;
1221 	kmem_cache_t		*prev_cache = NULL;
1222 	extern kmem_cache_t	*zio_buf_cache[];
1223 
1224 #ifdef _KERNEL
1225 	/*
1226 	 * First purge some DNLC entries, in case the DNLC is using
1227 	 * up too much memory.
1228 	 */
1229 	dnlc_reduce_cache((void *)(uintptr_t)arc_reduce_dnlc_percent);
1230 
1231 #if defined(__i386)
1232 	/*
1233 	 * Reclaim unused memory from all kmem caches.
1234 	 */
1235 	kmem_reap();
1236 #endif
1237 #endif
1238 
1239 	/*
1240 	 * An agressive reclamation will shrink the cache size as well as
1241 	 * reap free buffers from the arc kmem caches.
1242 	 */
1243 	if (strat == ARC_RECLAIM_AGGR)
1244 		arc_kmem_reclaim();
1245 
1246 	for (i = 0; i < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; i++) {
1247 		if (zio_buf_cache[i] != prev_cache) {
1248 			prev_cache = zio_buf_cache[i];
1249 			kmem_cache_reap_now(zio_buf_cache[i]);
1250 		}
1251 	}
1252 	kmem_cache_reap_now(buf_cache);
1253 	kmem_cache_reap_now(hdr_cache);
1254 }
1255 
1256 static void
1257 arc_reclaim_thread(void)
1258 {
1259 	clock_t			growtime = 0;
1260 	arc_reclaim_strategy_t	last_reclaim = ARC_RECLAIM_CONS;
1261 	callb_cpr_t		cpr;
1262 
1263 	CALLB_CPR_INIT(&cpr, &arc_reclaim_thr_lock, callb_generic_cpr, FTAG);
1264 
1265 	mutex_enter(&arc_reclaim_thr_lock);
1266 	while (arc_thread_exit == 0) {
1267 		if (arc_reclaim_needed()) {
1268 
1269 			if (arc.no_grow) {
1270 				if (last_reclaim == ARC_RECLAIM_CONS) {
1271 					last_reclaim = ARC_RECLAIM_AGGR;
1272 				} else {
1273 					last_reclaim = ARC_RECLAIM_CONS;
1274 				}
1275 			} else {
1276 				arc.no_grow = TRUE;
1277 				last_reclaim = ARC_RECLAIM_AGGR;
1278 				membar_producer();
1279 			}
1280 
1281 			/* reset the growth delay for every reclaim */
1282 			growtime = lbolt + (arc_grow_retry * hz);
1283 
1284 			arc_kmem_reap_now(last_reclaim);
1285 
1286 		} else if ((growtime > 0) && ((growtime - lbolt) <= 0)) {
1287 			arc.no_grow = FALSE;
1288 		}
1289 
1290 		if (arc_eviction_list != NULL)
1291 			arc_do_user_evicts();
1292 
1293 		/* block until needed, or one second, whichever is shorter */
1294 		CALLB_CPR_SAFE_BEGIN(&cpr);
1295 		(void) cv_timedwait(&arc_reclaim_thr_cv,
1296 		    &arc_reclaim_thr_lock, (lbolt + hz));
1297 		CALLB_CPR_SAFE_END(&cpr, &arc_reclaim_thr_lock);
1298 	}
1299 
1300 	arc_thread_exit = 0;
1301 	cv_broadcast(&arc_reclaim_thr_cv);
1302 	CALLB_CPR_EXIT(&cpr);		/* drops arc_reclaim_thr_lock */
1303 	thread_exit();
1304 }
1305 
1306 /*
1307  * Adapt arc info given the number of bytes we are trying to add and
1308  * the state that we are comming from.  This function is only called
1309  * when we are adding new content to the cache.
1310  */
1311 static void
1312 arc_adapt(int bytes, arc_state_t *state)
1313 {
1314 	int mult;
1315 
1316 	ASSERT(bytes > 0);
1317 	/*
1318 	 * Adapt the target size of the MRU list:
1319 	 *	- if we just hit in the MRU ghost list, then increase
1320 	 *	  the target size of the MRU list.
1321 	 *	- if we just hit in the MFU ghost list, then increase
1322 	 *	  the target size of the MFU list by decreasing the
1323 	 *	  target size of the MRU list.
1324 	 */
1325 	if (state == arc.mru_ghost) {
1326 		mult = ((arc.mru_ghost->size >= arc.mfu_ghost->size) ?
1327 		    1 : (arc.mfu_ghost->size/arc.mru_ghost->size));
1328 
1329 		arc.p = MIN(arc.c, arc.p + bytes * mult);
1330 	} else if (state == arc.mfu_ghost) {
1331 		mult = ((arc.mfu_ghost->size >= arc.mru_ghost->size) ?
1332 		    1 : (arc.mru_ghost->size/arc.mfu_ghost->size));
1333 
1334 		arc.p = MAX(0, (int64_t)arc.p - bytes * mult);
1335 	}
1336 	ASSERT((int64_t)arc.p >= 0);
1337 
1338 	if (arc_reclaim_needed()) {
1339 		cv_signal(&arc_reclaim_thr_cv);
1340 		return;
1341 	}
1342 
1343 	if (arc.no_grow)
1344 		return;
1345 
1346 	if (arc.c >= arc.c_max)
1347 		return;
1348 
1349 	/*
1350 	 * If we're within (2 * maxblocksize) bytes of the target
1351 	 * cache size, increment the target cache size
1352 	 */
1353 	if (arc.size > arc.c - (2ULL << SPA_MAXBLOCKSHIFT)) {
1354 		atomic_add_64(&arc.c, (int64_t)bytes);
1355 		if (arc.c > arc.c_max)
1356 			arc.c = arc.c_max;
1357 		else if (state == arc.anon)
1358 			atomic_add_64(&arc.p, (int64_t)bytes);
1359 		if (arc.p > arc.c)
1360 			arc.p = arc.c;
1361 	}
1362 	ASSERT((int64_t)arc.p >= 0);
1363 }
1364 
1365 /*
1366  * Check if the cache has reached its limits and eviction is required
1367  * prior to insert.
1368  */
1369 static int
1370 arc_evict_needed()
1371 {
1372 	if (arc_reclaim_needed())
1373 		return (1);
1374 
1375 	return (arc.size > arc.c);
1376 }
1377 
1378 /*
1379  * The state, supplied as the first argument, is going to have something
1380  * inserted on its behalf. So, determine which cache must be victimized to
1381  * satisfy an insertion for this state.  We have the following cases:
1382  *
1383  * 1. Insert for MRU, p > sizeof(arc.anon + arc.mru) ->
1384  * In this situation if we're out of space, but the resident size of the MFU is
1385  * under the limit, victimize the MFU cache to satisfy this insertion request.
1386  *
1387  * 2. Insert for MRU, p <= sizeof(arc.anon + arc.mru) ->
1388  * Here, we've used up all of the available space for the MRU, so we need to
1389  * evict from our own cache instead.  Evict from the set of resident MRU
1390  * entries.
1391  *
1392  * 3. Insert for MFU (c - p) > sizeof(arc.mfu) ->
1393  * c minus p represents the MFU space in the cache, since p is the size of the
1394  * cache that is dedicated to the MRU.  In this situation there's still space on
1395  * the MFU side, so the MRU side needs to be victimized.
1396  *
1397  * 4. Insert for MFU (c - p) < sizeof(arc.mfu) ->
1398  * MFU's resident set is consuming more space than it has been allotted.  In
1399  * this situation, we must victimize our own cache, the MFU, for this insertion.
1400  */
1401 static void
1402 arc_evict_for_state(arc_state_t *state, uint64_t bytes)
1403 {
1404 	uint64_t	mru_used;
1405 	uint64_t	mfu_space;
1406 	uint64_t	evicted;
1407 
1408 	ASSERT(state == arc.mru || state == arc.mfu);
1409 
1410 	if (state == arc.mru) {
1411 		mru_used = arc.anon->size + arc.mru->size;
1412 		if (arc.p > mru_used) {
1413 			/* case 1 */
1414 			evicted = arc_evict(arc.mfu, bytes);
1415 			if (evicted < bytes) {
1416 				arc_adjust();
1417 			}
1418 		} else {
1419 			/* case 2 */
1420 			evicted = arc_evict(arc.mru, bytes);
1421 			if (evicted < bytes) {
1422 				arc_adjust();
1423 			}
1424 		}
1425 	} else {
1426 		/* MFU case */
1427 		mfu_space = arc.c - arc.p;
1428 		if (mfu_space > arc.mfu->size) {
1429 			/* case 3 */
1430 			evicted = arc_evict(arc.mru, bytes);
1431 			if (evicted < bytes) {
1432 				arc_adjust();
1433 			}
1434 		} else {
1435 			/* case 4 */
1436 			evicted = arc_evict(arc.mfu, bytes);
1437 			if (evicted < bytes) {
1438 				arc_adjust();
1439 			}
1440 		}
1441 	}
1442 }
1443 
1444 /*
1445  * This routine is called whenever a buffer is accessed.
1446  * NOTE: the hash lock is dropped in this function.
1447  */
1448 static void
1449 arc_access_and_exit(arc_buf_hdr_t *buf, kmutex_t *hash_lock)
1450 {
1451 	arc_state_t	*evict_state = NULL;
1452 	int		blksz;
1453 
1454 	ASSERT(MUTEX_HELD(hash_lock));
1455 
1456 	blksz = buf->b_size;
1457 
1458 	if (buf->b_state == arc.anon) {
1459 		/*
1460 		 * This buffer is not in the cache, and does not
1461 		 * appear in our "ghost" list.  Add the new buffer
1462 		 * to the MRU state.
1463 		 */
1464 
1465 		arc_adapt(blksz, arc.anon);
1466 		if (arc_evict_needed())
1467 			evict_state = arc.mru;
1468 
1469 		ASSERT(buf->b_arc_access == 0);
1470 		buf->b_arc_access = lbolt;
1471 		DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
1472 		arc_change_state(arc.mru, buf, hash_lock);
1473 
1474 	} else if (buf->b_state == arc.mru) {
1475 		/*
1476 		 * If this buffer is in the MRU-top state and has the prefetch
1477 		 * flag, the first read was actually part of a prefetch.  In
1478 		 * this situation, we simply want to clear the flag and return.
1479 		 * A subsequent access should bump this into the MFU state.
1480 		 */
1481 		if ((buf->b_flags & ARC_PREFETCH) != 0) {
1482 			buf->b_flags &= ~ARC_PREFETCH;
1483 			atomic_add_64(&arc.mru->hits, 1);
1484 			mutex_exit(hash_lock);
1485 			return;
1486 		}
1487 
1488 		/*
1489 		 * This buffer has been "accessed" only once so far,
1490 		 * but it is still in the cache. Move it to the MFU
1491 		 * state.
1492 		 */
1493 		if (lbolt > buf->b_arc_access + ARC_MINTIME) {
1494 			/*
1495 			 * More than 125ms have passed since we
1496 			 * instantiated this buffer.  Move it to the
1497 			 * most frequently used state.
1498 			 */
1499 			buf->b_arc_access = lbolt;
1500 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
1501 			arc_change_state(arc.mfu, buf, hash_lock);
1502 		}
1503 		atomic_add_64(&arc.mru->hits, 1);
1504 	} else if (buf->b_state == arc.mru_ghost) {
1505 		arc_state_t	*new_state;
1506 		/*
1507 		 * This buffer has been "accessed" recently, but
1508 		 * was evicted from the cache.  Move it to the
1509 		 * MFU state.
1510 		 */
1511 
1512 		if (buf->b_flags & ARC_PREFETCH) {
1513 			new_state = arc.mru;
1514 			buf->b_flags &= ~ARC_PREFETCH;
1515 			DTRACE_PROBE1(new_state__mru, arc_buf_hdr_t *, buf);
1516 		} else {
1517 			new_state = arc.mfu;
1518 			DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
1519 		}
1520 
1521 		arc_adapt(blksz, arc.mru_ghost);
1522 		if (arc_evict_needed())
1523 			evict_state = new_state;
1524 
1525 		buf->b_arc_access = lbolt;
1526 		arc_change_state(new_state, buf, hash_lock);
1527 
1528 		atomic_add_64(&arc.mru_ghost->hits, 1);
1529 	} else if (buf->b_state == arc.mfu) {
1530 		/*
1531 		 * This buffer has been accessed more than once and is
1532 		 * still in the cache.  Keep it in the MFU state.
1533 		 *
1534 		 * NOTE: the add_reference() that occurred when we did
1535 		 * the arc_read() should have kicked this off the list,
1536 		 * so even if it was a prefetch, it will be put back at
1537 		 * the head of the list when we remove_reference().
1538 		 */
1539 		atomic_add_64(&arc.mfu->hits, 1);
1540 	} else if (buf->b_state == arc.mfu_ghost) {
1541 		/*
1542 		 * This buffer has been accessed more than once but has
1543 		 * been evicted from the cache.  Move it back to the
1544 		 * MFU state.
1545 		 */
1546 
1547 		arc_adapt(blksz, arc.mfu_ghost);
1548 		if (arc_evict_needed())
1549 			evict_state = arc.mfu;
1550 
1551 		buf->b_arc_access = lbolt;
1552 		DTRACE_PROBE1(new_state__mfu, arc_buf_hdr_t *, buf);
1553 		arc_change_state(arc.mfu, buf, hash_lock);
1554 
1555 		atomic_add_64(&arc.mfu_ghost->hits, 1);
1556 	} else {
1557 		ASSERT(!"invalid arc state");
1558 	}
1559 
1560 	mutex_exit(hash_lock);
1561 	if (evict_state)
1562 		arc_evict_for_state(evict_state, blksz);
1563 }
1564 
1565 /* a generic arc_done_func_t which you can use */
1566 /* ARGSUSED */
1567 void
1568 arc_bcopy_func(zio_t *zio, arc_buf_t *buf, void *arg)
1569 {
1570 	bcopy(buf->b_data, arg, buf->b_hdr->b_size);
1571 	VERIFY(arc_buf_remove_ref(buf, arg) == 1);
1572 }
1573 
1574 /* a generic arc_done_func_t which you can use */
1575 void
1576 arc_getbuf_func(zio_t *zio, arc_buf_t *buf, void *arg)
1577 {
1578 	arc_buf_t **bufp = arg;
1579 	if (zio && zio->io_error) {
1580 		VERIFY(arc_buf_remove_ref(buf, arg) == 1);
1581 		*bufp = NULL;
1582 	} else {
1583 		*bufp = buf;
1584 	}
1585 }
1586 
1587 static void
1588 arc_read_done(zio_t *zio)
1589 {
1590 	arc_buf_hdr_t	*hdr, *found;
1591 	arc_buf_t	*buf;
1592 	arc_buf_t	*abuf;	/* buffer we're assigning to callback */
1593 	kmutex_t	*hash_lock;
1594 	arc_callback_t	*callback_list, *acb;
1595 	int		freeable = FALSE;
1596 
1597 	buf = zio->io_private;
1598 	hdr = buf->b_hdr;
1599 
1600 	/*
1601 	 * The hdr was inserted into hash-table and removed from lists
1602 	 * prior to starting I/O.  We should find this header, since
1603 	 * it's in the hash table, and it should be legit since it's
1604 	 * not possible to evict it during the I/O.  The only possible
1605 	 * reason for it not to be found is if we were freed during the
1606 	 * read.
1607 	 */
1608 	found = buf_hash_find(zio->io_spa, &hdr->b_dva, hdr->b_birth,
1609 		    &hash_lock);
1610 
1611 	ASSERT((found == NULL && HDR_FREED_IN_READ(hdr) && hash_lock == NULL) ||
1612 	    (found == hdr && DVA_EQUAL(&hdr->b_dva, BP_IDENTITY(zio->io_bp))));
1613 
1614 	/* byteswap if necessary */
1615 	callback_list = hdr->b_acb;
1616 	ASSERT(callback_list != NULL);
1617 	if (BP_SHOULD_BYTESWAP(zio->io_bp) && callback_list->acb_byteswap)
1618 		callback_list->acb_byteswap(buf->b_data, hdr->b_size);
1619 
1620 	/* create copies of the data buffer for the callers */
1621 	abuf = buf;
1622 	for (acb = callback_list; acb; acb = acb->acb_next) {
1623 		if (acb->acb_done) {
1624 			if (abuf == NULL) {
1625 				abuf = kmem_cache_alloc(buf_cache, KM_SLEEP);
1626 				abuf->b_data = arc_data_copy(hdr, buf->b_data);
1627 				abuf->b_hdr = hdr;
1628 				abuf->b_efunc = NULL;
1629 				abuf->b_private = NULL;
1630 				abuf->b_next = hdr->b_buf;
1631 				hdr->b_buf = abuf;
1632 				hdr->b_datacnt += 1;
1633 			}
1634 			acb->acb_buf = abuf;
1635 			abuf = NULL;
1636 		} else {
1637 			/*
1638 			 * The caller did not provide a callback function.
1639 			 * In this case, we should just remove the reference.
1640 			 */
1641 			if (HDR_FREED_IN_READ(hdr)) {
1642 				ASSERT3P(hdr->b_state, ==, arc.anon);
1643 				(void) refcount_remove(&hdr->b_refcnt,
1644 				    acb->acb_private);
1645 			} else {
1646 				(void) remove_reference(hdr, hash_lock,
1647 				    acb->acb_private);
1648 			}
1649 		}
1650 	}
1651 	hdr->b_acb = NULL;
1652 	hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
1653 	ASSERT(!HDR_BUF_AVAILABLE(hdr));
1654 	if (abuf == buf)
1655 		hdr->b_flags |= ARC_BUF_AVAILABLE;
1656 
1657 	ASSERT(refcount_is_zero(&hdr->b_refcnt) || callback_list != NULL);
1658 
1659 	if (zio->io_error != 0) {
1660 		hdr->b_flags |= ARC_IO_ERROR;
1661 		if (hdr->b_state != arc.anon)
1662 			arc_change_state(arc.anon, hdr, hash_lock);
1663 		if (HDR_IN_HASH_TABLE(hdr))
1664 			buf_hash_remove(hdr);
1665 		freeable = refcount_is_zero(&hdr->b_refcnt);
1666 		/* translate checksum errors into IO errors */
1667 		if (zio->io_error == ECKSUM)
1668 			zio->io_error = EIO;
1669 	}
1670 
1671 	/*
1672 	 * Broadcast before we drop the hash_lock.  This is less efficient,
1673 	 * but avoids the possibility that the hdr (and hence the cv) might
1674 	 * be freed before we get to the cv_broadcast().
1675 	 */
1676 	cv_broadcast(&hdr->b_cv);
1677 
1678 	if (hash_lock) {
1679 		/*
1680 		 * Only call arc_access on anonymous buffers.  This is because
1681 		 * if we've issued an I/O for an evicted buffer, we've already
1682 		 * called arc_access (to prevent any simultaneous readers from
1683 		 * getting confused).
1684 		 */
1685 		if (zio->io_error == 0 && hdr->b_state == arc.anon)
1686 			arc_access_and_exit(hdr, hash_lock);
1687 		else
1688 			mutex_exit(hash_lock);
1689 	} else {
1690 		/*
1691 		 * This block was freed while we waited for the read to
1692 		 * complete.  It has been removed from the hash table and
1693 		 * moved to the anonymous state (so that it won't show up
1694 		 * in the cache).
1695 		 */
1696 		ASSERT3P(hdr->b_state, ==, arc.anon);
1697 		freeable = refcount_is_zero(&hdr->b_refcnt);
1698 	}
1699 
1700 	/* execute each callback and free its structure */
1701 	while ((acb = callback_list) != NULL) {
1702 		if (acb->acb_done)
1703 			acb->acb_done(zio, acb->acb_buf, acb->acb_private);
1704 
1705 		if (acb->acb_zio_dummy != NULL) {
1706 			acb->acb_zio_dummy->io_error = zio->io_error;
1707 			zio_nowait(acb->acb_zio_dummy);
1708 		}
1709 
1710 		callback_list = acb->acb_next;
1711 		kmem_free(acb, sizeof (arc_callback_t));
1712 	}
1713 
1714 	if (freeable)
1715 		arc_hdr_destroy(hdr);
1716 }
1717 
1718 /*
1719  * "Read" the block block at the specified DVA (in bp) via the
1720  * cache.  If the block is found in the cache, invoke the provided
1721  * callback immediately and return.  Note that the `zio' parameter
1722  * in the callback will be NULL in this case, since no IO was
1723  * required.  If the block is not in the cache pass the read request
1724  * on to the spa with a substitute callback function, so that the
1725  * requested block will be added to the cache.
1726  *
1727  * If a read request arrives for a block that has a read in-progress,
1728  * either wait for the in-progress read to complete (and return the
1729  * results); or, if this is a read with a "done" func, add a record
1730  * to the read to invoke the "done" func when the read completes,
1731  * and return; or just return.
1732  *
1733  * arc_read_done() will invoke all the requested "done" functions
1734  * for readers of this block.
1735  */
1736 int
1737 arc_read(zio_t *pio, spa_t *spa, blkptr_t *bp, arc_byteswap_func_t *swap,
1738     arc_done_func_t *done, void *private, int priority, int flags,
1739     uint32_t arc_flags, zbookmark_t *zb)
1740 {
1741 	arc_buf_hdr_t *hdr;
1742 	arc_buf_t *buf;
1743 	kmutex_t *hash_lock;
1744 	zio_t	*rzio;
1745 
1746 top:
1747 	hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock);
1748 	if (hdr && hdr->b_datacnt > 0) {
1749 
1750 		if (HDR_IO_IN_PROGRESS(hdr)) {
1751 			if ((arc_flags & ARC_NOWAIT) && done) {
1752 				arc_callback_t	*acb = NULL;
1753 
1754 				acb = kmem_zalloc(sizeof (arc_callback_t),
1755 				    KM_SLEEP);
1756 				acb->acb_done = done;
1757 				acb->acb_private = private;
1758 				acb->acb_byteswap = swap;
1759 				if (pio != NULL)
1760 					acb->acb_zio_dummy = zio_null(pio,
1761 					    spa, NULL, NULL, flags);
1762 
1763 				ASSERT(acb->acb_done != NULL);
1764 				acb->acb_next = hdr->b_acb;
1765 				hdr->b_acb = acb;
1766 				add_reference(hdr, hash_lock, private);
1767 				mutex_exit(hash_lock);
1768 				return (0);
1769 			} else if (arc_flags & ARC_WAIT) {
1770 				cv_wait(&hdr->b_cv, hash_lock);
1771 				mutex_exit(hash_lock);
1772 				goto top;
1773 			}
1774 			mutex_exit(hash_lock);
1775 			return (0);
1776 		}
1777 
1778 		ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu);
1779 
1780 		if (done) {
1781 			/*
1782 			 * If this block is already in use, create a new
1783 			 * copy of the data so that we will be guaranteed
1784 			 * that arc_release() will always succeed.
1785 			 */
1786 			buf = hdr->b_buf;
1787 			ASSERT(buf);
1788 			ASSERT(buf->b_data);
1789 			if (!HDR_BUF_AVAILABLE(hdr)) {
1790 				void *data = arc_data_copy(hdr, buf->b_data);
1791 				buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
1792 				buf->b_hdr = hdr;
1793 				buf->b_data = data;
1794 				buf->b_efunc = NULL;
1795 				buf->b_private = NULL;
1796 				buf->b_next = hdr->b_buf;
1797 				hdr->b_buf = buf;
1798 				hdr->b_datacnt += 1;
1799 			} else {
1800 				ASSERT(buf->b_efunc == NULL);
1801 				hdr->b_flags &= ~ARC_BUF_AVAILABLE;
1802 			}
1803 			add_reference(hdr, hash_lock, private);
1804 		}
1805 		DTRACE_PROBE1(arc__hit, arc_buf_hdr_t *, hdr);
1806 		arc_access_and_exit(hdr, hash_lock);
1807 		atomic_add_64(&arc.hits, 1);
1808 		if (done)
1809 			done(NULL, buf, private);
1810 	} else {
1811 		uint64_t size = BP_GET_LSIZE(bp);
1812 		arc_callback_t	*acb;
1813 
1814 		if (hdr == NULL) {
1815 			/* this block is not in the cache */
1816 			arc_buf_hdr_t	*exists;
1817 
1818 			buf = arc_buf_alloc(spa, size, private);
1819 			hdr = buf->b_hdr;
1820 			hdr->b_dva = *BP_IDENTITY(bp);
1821 			hdr->b_birth = bp->blk_birth;
1822 			hdr->b_cksum0 = bp->blk_cksum.zc_word[0];
1823 			exists = buf_hash_insert(hdr, &hash_lock);
1824 			if (exists) {
1825 				/* somebody beat us to the hash insert */
1826 				mutex_exit(hash_lock);
1827 				bzero(&hdr->b_dva, sizeof (dva_t));
1828 				hdr->b_birth = 0;
1829 				hdr->b_cksum0 = 0;
1830 				(void) arc_buf_remove_ref(buf, private);
1831 				goto top; /* restart the IO request */
1832 			}
1833 
1834 		} else {
1835 			/* this block is in the ghost cache */
1836 			ASSERT(GHOST_STATE(hdr->b_state));
1837 			ASSERT(!HDR_IO_IN_PROGRESS(hdr));
1838 			add_reference(hdr, hash_lock, private);
1839 			ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1);
1840 
1841 			ASSERT(hdr->b_buf == NULL);
1842 			buf = kmem_cache_alloc(buf_cache, KM_SLEEP);
1843 			buf->b_hdr = hdr;
1844 			buf->b_efunc = NULL;
1845 			buf->b_private = NULL;
1846 			buf->b_next = NULL;
1847 			hdr->b_buf = buf;
1848 			buf->b_data = zio_buf_alloc(hdr->b_size);
1849 			atomic_add_64(&arc.size, hdr->b_size);
1850 			ASSERT(hdr->b_datacnt == 0);
1851 			hdr->b_datacnt = 1;
1852 		}
1853 
1854 		acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
1855 		acb->acb_done = done;
1856 		acb->acb_private = private;
1857 		acb->acb_byteswap = swap;
1858 
1859 		ASSERT(hdr->b_acb == NULL);
1860 		hdr->b_acb = acb;
1861 
1862 		/*
1863 		 * If this DVA is part of a prefetch, mark the buf
1864 		 * header with the prefetch flag
1865 		 */
1866 		if (arc_flags & ARC_PREFETCH)
1867 			hdr->b_flags |= ARC_PREFETCH;
1868 		hdr->b_flags |= ARC_IO_IN_PROGRESS;
1869 
1870 		/*
1871 		 * If the buffer has been evicted, migrate it to a present state
1872 		 * before issuing the I/O.  Once we drop the hash-table lock,
1873 		 * the header will be marked as I/O in progress and have an
1874 		 * attached buffer.  At this point, anybody who finds this
1875 		 * buffer ought to notice that it's legit but has a pending I/O.
1876 		 */
1877 
1878 		if (GHOST_STATE(hdr->b_state))
1879 			arc_access_and_exit(hdr, hash_lock);
1880 		else
1881 			mutex_exit(hash_lock);
1882 
1883 		ASSERT3U(hdr->b_size, ==, size);
1884 		DTRACE_PROBE3(arc__miss, blkptr_t *, bp, uint64_t, size,
1885 		    zbookmark_t *, zb);
1886 		atomic_add_64(&arc.misses, 1);
1887 
1888 		rzio = zio_read(pio, spa, bp, buf->b_data, size,
1889 		    arc_read_done, buf, priority, flags, zb);
1890 
1891 		if (arc_flags & ARC_WAIT)
1892 			return (zio_wait(rzio));
1893 
1894 		ASSERT(arc_flags & ARC_NOWAIT);
1895 		zio_nowait(rzio);
1896 	}
1897 	return (0);
1898 }
1899 
1900 /*
1901  * arc_read() variant to support pool traversal.  If the block is already
1902  * in the ARC, make a copy of it; otherwise, the caller will do the I/O.
1903  * The idea is that we don't want pool traversal filling up memory, but
1904  * if the ARC already has the data anyway, we shouldn't pay for the I/O.
1905  */
1906 int
1907 arc_tryread(spa_t *spa, blkptr_t *bp, void *data)
1908 {
1909 	arc_buf_hdr_t *hdr;
1910 	kmutex_t *hash_mtx;
1911 	int rc = 0;
1912 
1913 	hdr = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_mtx);
1914 
1915 	if (hdr && hdr->b_datacnt > 0 && !HDR_IO_IN_PROGRESS(hdr)) {
1916 		arc_buf_t *buf = hdr->b_buf;
1917 
1918 		ASSERT(buf);
1919 		while (buf->b_data == NULL) {
1920 			buf = buf->b_next;
1921 			ASSERT(buf);
1922 		}
1923 		bcopy(buf->b_data, data, hdr->b_size);
1924 	} else {
1925 		rc = ENOENT;
1926 	}
1927 
1928 	if (hash_mtx)
1929 		mutex_exit(hash_mtx);
1930 
1931 	return (rc);
1932 }
1933 
1934 void
1935 arc_set_callback(arc_buf_t *buf, arc_evict_func_t *func, void *private)
1936 {
1937 	ASSERT(buf->b_hdr != NULL);
1938 	ASSERT(buf->b_hdr->b_state != arc.anon);
1939 	ASSERT(!refcount_is_zero(&buf->b_hdr->b_refcnt) || func == NULL);
1940 	buf->b_efunc = func;
1941 	buf->b_private = private;
1942 }
1943 
1944 /*
1945  * This is used by the DMU to let the ARC know that a buffer is
1946  * being evicted, so the ARC should clean up.  If this arc buf
1947  * is not yet in the evicted state, it will be put there.
1948  */
1949 int
1950 arc_buf_evict(arc_buf_t *buf)
1951 {
1952 	arc_buf_hdr_t *hdr;
1953 	kmutex_t *hash_lock;
1954 	arc_buf_t **bufp;
1955 
1956 	mutex_enter(&arc_eviction_mtx);
1957 	hdr = buf->b_hdr;
1958 	if (hdr == NULL) {
1959 		/*
1960 		 * We are in arc_do_user_evicts().
1961 		 * NOTE: We can't be in arc_buf_add_ref() because
1962 		 * that would violate the interface rules.
1963 		 */
1964 		ASSERT(buf->b_data == NULL);
1965 		mutex_exit(&arc_eviction_mtx);
1966 		return (0);
1967 	} else if (buf->b_data == NULL) {
1968 		arc_buf_t copy = *buf; /* structure assignment */
1969 		/*
1970 		 * We are on the eviction list.  Process this buffer
1971 		 * now but let arc_do_user_evicts() do the reaping.
1972 		 */
1973 		buf->b_efunc = NULL;
1974 		buf->b_hdr = NULL;
1975 		mutex_exit(&arc_eviction_mtx);
1976 		VERIFY(copy.b_efunc(&copy) == 0);
1977 		return (1);
1978 	} else {
1979 		/*
1980 		 * Prevent a race with arc_evict()
1981 		 */
1982 		ASSERT3U(refcount_count(&hdr->b_refcnt), <, hdr->b_datacnt);
1983 		buf->b_hdr = NULL;
1984 	}
1985 	mutex_exit(&arc_eviction_mtx);
1986 
1987 	hash_lock = HDR_LOCK(hdr);
1988 	mutex_enter(hash_lock);
1989 
1990 	ASSERT(hdr->b_state == arc.mru || hdr->b_state == arc.mfu);
1991 
1992 	/*
1993 	 * Pull this buffer off of the hdr
1994 	 */
1995 	bufp = &hdr->b_buf;
1996 	while (*bufp != buf)
1997 		bufp = &(*bufp)->b_next;
1998 	*bufp = buf->b_next;
1999 
2000 	ASSERT(buf->b_data != NULL);
2001 	buf->b_hdr = hdr;
2002 	arc_buf_destroy(buf, FALSE);
2003 
2004 	if (hdr->b_datacnt == 0) {
2005 		arc_state_t *old_state = hdr->b_state;
2006 		arc_state_t *evicted_state;
2007 
2008 		ASSERT(refcount_is_zero(&hdr->b_refcnt));
2009 
2010 		evicted_state =
2011 		    (old_state == arc.mru) ? arc.mru_ghost : arc.mfu_ghost;
2012 
2013 		mutex_enter(&old_state->mtx);
2014 		mutex_enter(&evicted_state->mtx);
2015 
2016 		arc_change_state(evicted_state, hdr, hash_lock);
2017 		ASSERT(HDR_IN_HASH_TABLE(hdr));
2018 		hdr->b_flags = ARC_IN_HASH_TABLE;
2019 
2020 		mutex_exit(&evicted_state->mtx);
2021 		mutex_exit(&old_state->mtx);
2022 	}
2023 	mutex_exit(hash_lock);
2024 
2025 	VERIFY(buf->b_efunc(buf) == 0);
2026 	buf->b_efunc = NULL;
2027 	buf->b_private = NULL;
2028 	buf->b_hdr = NULL;
2029 	kmem_cache_free(buf_cache, buf);
2030 	return (1);
2031 }
2032 
2033 /*
2034  * Release this buffer from the cache.  This must be done
2035  * after a read and prior to modifying the buffer contents.
2036  * If the buffer has more than one reference, we must make
2037  * make a new hdr for the buffer.
2038  */
2039 void
2040 arc_release(arc_buf_t *buf, void *tag)
2041 {
2042 	arc_buf_hdr_t *hdr = buf->b_hdr;
2043 	kmutex_t *hash_lock = HDR_LOCK(hdr);
2044 
2045 	/* this buffer is not on any list */
2046 	ASSERT(refcount_count(&hdr->b_refcnt) > 0);
2047 
2048 	if (hdr->b_state == arc.anon) {
2049 		/* this buffer is already released */
2050 		ASSERT3U(refcount_count(&hdr->b_refcnt), ==, 1);
2051 		ASSERT(BUF_EMPTY(hdr));
2052 		ASSERT(buf->b_efunc == NULL);
2053 		return;
2054 	}
2055 
2056 	mutex_enter(hash_lock);
2057 
2058 	/*
2059 	 * Do we have more than one buf?
2060 	 */
2061 	if (hdr->b_buf != buf || buf->b_next != NULL) {
2062 		arc_buf_hdr_t *nhdr;
2063 		arc_buf_t **bufp;
2064 		uint64_t blksz = hdr->b_size;
2065 		spa_t *spa = hdr->b_spa;
2066 
2067 		ASSERT(hdr->b_datacnt > 1);
2068 		/*
2069 		 * Pull the data off of this buf and attach it to
2070 		 * a new anonymous buf.
2071 		 */
2072 		(void) remove_reference(hdr, hash_lock, tag);
2073 		bufp = &hdr->b_buf;
2074 		while (*bufp != buf)
2075 			bufp = &(*bufp)->b_next;
2076 		*bufp = (*bufp)->b_next;
2077 
2078 		ASSERT3U(hdr->b_state->size, >=, hdr->b_size);
2079 		atomic_add_64(&hdr->b_state->size, -hdr->b_size);
2080 		if (refcount_is_zero(&hdr->b_refcnt)) {
2081 			ASSERT3U(hdr->b_state->lsize, >=, hdr->b_size);
2082 			atomic_add_64(&hdr->b_state->lsize, -hdr->b_size);
2083 		}
2084 		hdr->b_datacnt -= 1;
2085 
2086 		mutex_exit(hash_lock);
2087 
2088 		nhdr = kmem_cache_alloc(hdr_cache, KM_SLEEP);
2089 		nhdr->b_size = blksz;
2090 		nhdr->b_spa = spa;
2091 		nhdr->b_buf = buf;
2092 		nhdr->b_state = arc.anon;
2093 		nhdr->b_arc_access = 0;
2094 		nhdr->b_flags = 0;
2095 		nhdr->b_datacnt = 1;
2096 		buf->b_hdr = nhdr;
2097 		buf->b_next = NULL;
2098 		(void) refcount_add(&nhdr->b_refcnt, tag);
2099 		atomic_add_64(&arc.anon->size, blksz);
2100 
2101 		hdr = nhdr;
2102 	} else {
2103 		ASSERT(refcount_count(&hdr->b_refcnt) == 1);
2104 		ASSERT(!list_link_active(&hdr->b_arc_node));
2105 		ASSERT(!HDR_IO_IN_PROGRESS(hdr));
2106 		arc_change_state(arc.anon, hdr, hash_lock);
2107 		hdr->b_arc_access = 0;
2108 		mutex_exit(hash_lock);
2109 		bzero(&hdr->b_dva, sizeof (dva_t));
2110 		hdr->b_birth = 0;
2111 		hdr->b_cksum0 = 0;
2112 	}
2113 	buf->b_efunc = NULL;
2114 	buf->b_private = NULL;
2115 }
2116 
2117 int
2118 arc_released(arc_buf_t *buf)
2119 {
2120 	return (buf->b_data != NULL && buf->b_hdr->b_state == arc.anon);
2121 }
2122 
2123 int
2124 arc_has_callback(arc_buf_t *buf)
2125 {
2126 	return (buf->b_efunc != NULL);
2127 }
2128 
2129 #ifdef ZFS_DEBUG
2130 int
2131 arc_referenced(arc_buf_t *buf)
2132 {
2133 	return (refcount_count(&buf->b_hdr->b_refcnt));
2134 }
2135 #endif
2136 
2137 static void
2138 arc_write_done(zio_t *zio)
2139 {
2140 	arc_buf_t *buf;
2141 	arc_buf_hdr_t *hdr;
2142 	arc_callback_t *acb;
2143 
2144 	buf = zio->io_private;
2145 	hdr = buf->b_hdr;
2146 	acb = hdr->b_acb;
2147 	hdr->b_acb = NULL;
2148 	ASSERT(acb != NULL);
2149 
2150 	/* this buffer is on no lists and is not in the hash table */
2151 	ASSERT3P(hdr->b_state, ==, arc.anon);
2152 
2153 	hdr->b_dva = *BP_IDENTITY(zio->io_bp);
2154 	hdr->b_birth = zio->io_bp->blk_birth;
2155 	hdr->b_cksum0 = zio->io_bp->blk_cksum.zc_word[0];
2156 	/*
2157 	 * If the block to be written was all-zero, we may have
2158 	 * compressed it away.  In this case no write was performed
2159 	 * so there will be no dva/birth-date/checksum.  The buffer
2160 	 * must therefor remain anonymous (and uncached).
2161 	 */
2162 	if (!BUF_EMPTY(hdr)) {
2163 		arc_buf_hdr_t *exists;
2164 		kmutex_t *hash_lock;
2165 
2166 		exists = buf_hash_insert(hdr, &hash_lock);
2167 		if (exists) {
2168 			/*
2169 			 * This can only happen if we overwrite for
2170 			 * sync-to-convergence, because we remove
2171 			 * buffers from the hash table when we arc_free().
2172 			 */
2173 			ASSERT(DVA_EQUAL(BP_IDENTITY(&zio->io_bp_orig),
2174 			    BP_IDENTITY(zio->io_bp)));
2175 			ASSERT3U(zio->io_bp_orig.blk_birth, ==,
2176 			    zio->io_bp->blk_birth);
2177 
2178 			ASSERT(refcount_is_zero(&exists->b_refcnt));
2179 			arc_change_state(arc.anon, exists, hash_lock);
2180 			mutex_exit(hash_lock);
2181 			arc_hdr_destroy(exists);
2182 			exists = buf_hash_insert(hdr, &hash_lock);
2183 			ASSERT3P(exists, ==, NULL);
2184 		}
2185 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2186 		arc_access_and_exit(hdr, hash_lock);
2187 	} else if (acb->acb_done == NULL) {
2188 		int destroy_hdr;
2189 		/*
2190 		 * This is an anonymous buffer with no user callback,
2191 		 * destroy it if there are no active references.
2192 		 */
2193 		mutex_enter(&arc_eviction_mtx);
2194 		destroy_hdr = refcount_is_zero(&hdr->b_refcnt);
2195 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2196 		mutex_exit(&arc_eviction_mtx);
2197 		if (destroy_hdr)
2198 			arc_hdr_destroy(hdr);
2199 	} else {
2200 		hdr->b_flags &= ~ARC_IO_IN_PROGRESS;
2201 	}
2202 
2203 	if (acb->acb_done) {
2204 		ASSERT(!refcount_is_zero(&hdr->b_refcnt));
2205 		acb->acb_done(zio, buf, acb->acb_private);
2206 	}
2207 
2208 	kmem_free(acb, sizeof (arc_callback_t));
2209 }
2210 
2211 int
2212 arc_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies,
2213     uint64_t txg, blkptr_t *bp, arc_buf_t *buf,
2214     arc_done_func_t *done, void *private, int priority, int flags,
2215     uint32_t arc_flags, zbookmark_t *zb)
2216 {
2217 	arc_buf_hdr_t *hdr = buf->b_hdr;
2218 	arc_callback_t	*acb;
2219 	zio_t	*rzio;
2220 
2221 	/* this is a private buffer - no locking required */
2222 	ASSERT3P(hdr->b_state, ==, arc.anon);
2223 	ASSERT(BUF_EMPTY(hdr));
2224 	ASSERT(!HDR_IO_ERROR(hdr));
2225 	acb = kmem_zalloc(sizeof (arc_callback_t), KM_SLEEP);
2226 	acb->acb_done = done;
2227 	acb->acb_private = private;
2228 	acb->acb_byteswap = (arc_byteswap_func_t *)-1;
2229 	hdr->b_acb = acb;
2230 	hdr->b_flags |= ARC_IO_IN_PROGRESS;
2231 	rzio = zio_write(pio, spa, checksum, compress, ncopies, txg, bp,
2232 	    buf->b_data, hdr->b_size, arc_write_done, buf, priority, flags, zb);
2233 
2234 	if (arc_flags & ARC_WAIT)
2235 		return (zio_wait(rzio));
2236 
2237 	ASSERT(arc_flags & ARC_NOWAIT);
2238 	zio_nowait(rzio);
2239 
2240 	return (0);
2241 }
2242 
2243 int
2244 arc_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
2245     zio_done_func_t *done, void *private, uint32_t arc_flags)
2246 {
2247 	arc_buf_hdr_t *ab;
2248 	kmutex_t *hash_lock;
2249 	zio_t	*zio;
2250 
2251 	/*
2252 	 * If this buffer is in the cache, release it, so it
2253 	 * can be re-used.
2254 	 */
2255 	ab = buf_hash_find(spa, BP_IDENTITY(bp), bp->blk_birth, &hash_lock);
2256 	if (ab != NULL) {
2257 		/*
2258 		 * The checksum of blocks to free is not always
2259 		 * preserved (eg. on the deadlist).  However, if it is
2260 		 * nonzero, it should match what we have in the cache.
2261 		 */
2262 		ASSERT(bp->blk_cksum.zc_word[0] == 0 ||
2263 		    ab->b_cksum0 == bp->blk_cksum.zc_word[0]);
2264 		if (ab->b_state != arc.anon)
2265 			arc_change_state(arc.anon, ab, hash_lock);
2266 		if (refcount_is_zero(&ab->b_refcnt)) {
2267 			mutex_exit(hash_lock);
2268 			arc_hdr_destroy(ab);
2269 			atomic_add_64(&arc.deleted, 1);
2270 		} else {
2271 			/*
2272 			 * We could have an outstanding read on this
2273 			 * block, so multiple active references are
2274 			 * possible.  But we should only have a single
2275 			 * data buffer associated at this point.
2276 			 */
2277 			ASSERT3U(ab->b_datacnt, ==, 1);
2278 			if (HDR_IO_IN_PROGRESS(ab))
2279 				ab->b_flags |= ARC_FREED_IN_READ;
2280 			if (HDR_IN_HASH_TABLE(ab))
2281 				buf_hash_remove(ab);
2282 			ab->b_arc_access = 0;
2283 			bzero(&ab->b_dva, sizeof (dva_t));
2284 			ab->b_birth = 0;
2285 			ab->b_cksum0 = 0;
2286 			ab->b_buf->b_efunc = NULL;
2287 			ab->b_buf->b_private = NULL;
2288 			mutex_exit(hash_lock);
2289 		}
2290 	}
2291 
2292 	zio = zio_free(pio, spa, txg, bp, done, private);
2293 
2294 	if (arc_flags & ARC_WAIT)
2295 		return (zio_wait(zio));
2296 
2297 	ASSERT(arc_flags & ARC_NOWAIT);
2298 	zio_nowait(zio);
2299 
2300 	return (0);
2301 }
2302 
2303 void
2304 arc_tempreserve_clear(uint64_t tempreserve)
2305 {
2306 	atomic_add_64(&arc_tempreserve, -tempreserve);
2307 	ASSERT((int64_t)arc_tempreserve >= 0);
2308 }
2309 
2310 int
2311 arc_tempreserve_space(uint64_t tempreserve)
2312 {
2313 #ifdef ZFS_DEBUG
2314 	/*
2315 	 * Once in a while, fail for no reason.  Everything should cope.
2316 	 */
2317 	if (spa_get_random(10000) == 0) {
2318 		dprintf("forcing random failure\n");
2319 		return (ERESTART);
2320 	}
2321 #endif
2322 	if (tempreserve > arc.c/4 && !arc.no_grow)
2323 		arc.c = MIN(arc.c_max, tempreserve * 4);
2324 	if (tempreserve > arc.c)
2325 		return (ENOMEM);
2326 
2327 	/*
2328 	 * Throttle writes when the amount of dirty data in the cache
2329 	 * gets too large.  We try to keep the cache less than half full
2330 	 * of dirty blocks so that our sync times don't grow too large.
2331 	 * Note: if two requests come in concurrently, we might let them
2332 	 * both succeed, when one of them should fail.  Not a huge deal.
2333 	 *
2334 	 * XXX The limit should be adjusted dynamically to keep the time
2335 	 * to sync a dataset fixed (around 1-5 seconds?).
2336 	 */
2337 
2338 	if (tempreserve + arc_tempreserve + arc.anon->size > arc.c / 2 &&
2339 	    arc_tempreserve + arc.anon->size > arc.c / 4) {
2340 		dprintf("failing, arc_tempreserve=%lluK anon=%lluK "
2341 		    "tempreserve=%lluK arc.c=%lluK\n",
2342 		    arc_tempreserve>>10, arc.anon->lsize>>10,
2343 		    tempreserve>>10, arc.c>>10);
2344 		return (ERESTART);
2345 	}
2346 	atomic_add_64(&arc_tempreserve, tempreserve);
2347 	return (0);
2348 }
2349 
2350 void
2351 arc_init(void)
2352 {
2353 	mutex_init(&arc_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
2354 	mutex_init(&arc_reclaim_thr_lock, NULL, MUTEX_DEFAULT, NULL);
2355 	cv_init(&arc_reclaim_thr_cv, NULL, CV_DEFAULT, NULL);
2356 
2357 	/* Start out with 1/8 of all memory */
2358 	arc.c = physmem * PAGESIZE / 8;
2359 
2360 #ifdef _KERNEL
2361 	/*
2362 	 * On architectures where the physical memory can be larger
2363 	 * than the addressable space (intel in 32-bit mode), we may
2364 	 * need to limit the cache to 1/8 of VM size.
2365 	 */
2366 	arc.c = MIN(arc.c, vmem_size(heap_arena, VMEM_ALLOC | VMEM_FREE) / 8);
2367 #endif
2368 
2369 	/* set min cache to 1/32 of all memory, or 64MB, whichever is more */
2370 	arc.c_min = MAX(arc.c / 4, 64<<20);
2371 	/* set max to 3/4 of all memory, or all but 1GB, whichever is more */
2372 	if (arc.c * 8 >= 1<<30)
2373 		arc.c_max = (arc.c * 8) - (1<<30);
2374 	else
2375 		arc.c_max = arc.c_min;
2376 	arc.c_max = MAX(arc.c * 6, arc.c_max);
2377 	arc.c = arc.c_max;
2378 	arc.p = (arc.c >> 1);
2379 
2380 	/* if kmem_flags are set, lets try to use less memory */
2381 	if (kmem_debugging())
2382 		arc.c = arc.c / 2;
2383 	if (arc.c < arc.c_min)
2384 		arc.c = arc.c_min;
2385 
2386 	arc.anon = &ARC_anon;
2387 	arc.mru = &ARC_mru;
2388 	arc.mru_ghost = &ARC_mru_ghost;
2389 	arc.mfu = &ARC_mfu;
2390 	arc.mfu_ghost = &ARC_mfu_ghost;
2391 	arc.size = 0;
2392 
2393 	list_create(&arc.mru->list, sizeof (arc_buf_hdr_t),
2394 	    offsetof(arc_buf_hdr_t, b_arc_node));
2395 	list_create(&arc.mru_ghost->list, sizeof (arc_buf_hdr_t),
2396 	    offsetof(arc_buf_hdr_t, b_arc_node));
2397 	list_create(&arc.mfu->list, sizeof (arc_buf_hdr_t),
2398 	    offsetof(arc_buf_hdr_t, b_arc_node));
2399 	list_create(&arc.mfu_ghost->list, sizeof (arc_buf_hdr_t),
2400 	    offsetof(arc_buf_hdr_t, b_arc_node));
2401 
2402 	buf_init();
2403 
2404 	arc_thread_exit = 0;
2405 	arc_eviction_list = NULL;
2406 	mutex_init(&arc_eviction_mtx, NULL, MUTEX_DEFAULT, NULL);
2407 
2408 	(void) thread_create(NULL, 0, arc_reclaim_thread, NULL, 0, &p0,
2409 	    TS_RUN, minclsyspri);
2410 }
2411 
2412 void
2413 arc_fini(void)
2414 {
2415 	mutex_enter(&arc_reclaim_thr_lock);
2416 	arc_thread_exit = 1;
2417 	while (arc_thread_exit != 0)
2418 		cv_wait(&arc_reclaim_thr_cv, &arc_reclaim_thr_lock);
2419 	mutex_exit(&arc_reclaim_thr_lock);
2420 
2421 	arc_flush();
2422 
2423 	arc_dead = TRUE;
2424 
2425 	mutex_destroy(&arc_eviction_mtx);
2426 	mutex_destroy(&arc_reclaim_lock);
2427 	mutex_destroy(&arc_reclaim_thr_lock);
2428 	cv_destroy(&arc_reclaim_thr_cv);
2429 
2430 	list_destroy(&arc.mru->list);
2431 	list_destroy(&arc.mru_ghost->list);
2432 	list_destroy(&arc.mfu->list);
2433 	list_destroy(&arc.mfu_ghost->list);
2434 
2435 	buf_fini();
2436 }
2437