xref: /titanic_51/usr/src/uts/common/fs/zfs/vdev_cache.c (revision 1a5e258f5471356ca102c7176637cdce45bac147)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22a3f829aeSBill Moore  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25be6fd75aSMatthew Ahrens /*
26be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
27be6fd75aSMatthew Ahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/zfs_context.h>
30fa9e4066Sahrens #include <sys/spa.h>
31fa9e4066Sahrens #include <sys/vdev_impl.h>
32fa9e4066Sahrens #include <sys/zio.h>
3387db74c1Sek110237 #include <sys/kstat.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens /*
36fa9e4066Sahrens  * Virtual device read-ahead caching.
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * This file implements a simple LRU read-ahead cache.  When the DMU reads
39fa9e4066Sahrens  * a given block, it will often want other, nearby blocks soon thereafter.
40fa9e4066Sahrens  * We take advantage of this by reading a larger disk region and caching
4187db74c1Sek110237  * the result.  In the best case, this can turn 128 back-to-back 512-byte
4287db74c1Sek110237  * reads into a single 64k read followed by 127 cache hits; this reduces
43fa9e4066Sahrens  * latency dramatically.  In the worst case, it can turn an isolated 512-byte
4487db74c1Sek110237  * read into a 64k read, which doesn't affect latency all that much but is
45fa9e4066Sahrens  * terribly wasteful of bandwidth.  A more intelligent version of the cache
46fa9e4066Sahrens  * could keep track of access patterns and not do read-ahead unless it sees
47fdb2e906Sek110237  * at least two temporally close I/Os to the same region.  Currently, only
48fdb2e906Sek110237  * metadata I/O is inflated.  A futher enhancement could take advantage of
49fdb2e906Sek110237  * more semantic information about the I/O.  And it could use something
50fdb2e906Sek110237  * faster than an AVL tree; that was chosen solely for convenience.
51fa9e4066Sahrens  *
52fa9e4066Sahrens  * There are five cache operations: allocate, fill, read, write, evict.
53fa9e4066Sahrens  *
54fa9e4066Sahrens  * (1) Allocate.  This reserves a cache entry for the specified region.
55fa9e4066Sahrens  *     We separate the allocate and fill operations so that multiple threads
56fa9e4066Sahrens  *     don't generate I/O for the same cache miss.
57fa9e4066Sahrens  *
58fa9e4066Sahrens  * (2) Fill.  When the I/O for a cache miss completes, the fill routine
59fa9e4066Sahrens  *     places the data in the previously allocated cache entry.
60fa9e4066Sahrens  *
61fa9e4066Sahrens  * (3) Read.  Read data from the cache.
62fa9e4066Sahrens  *
63fa9e4066Sahrens  * (4) Write.  Update cache contents after write completion.
64fa9e4066Sahrens  *
65fa9e4066Sahrens  * (5) Evict.  When allocating a new entry, we evict the oldest (LRU) entry
66614409b5Sahrens  *     if the total cache size exceeds zfs_vdev_cache_size.
67fa9e4066Sahrens  */
68fa9e4066Sahrens 
69614409b5Sahrens /*
70614409b5Sahrens  * These tunables are for performance analysis.
71614409b5Sahrens  */
72614409b5Sahrens /*
73614409b5Sahrens  * All i/os smaller than zfs_vdev_cache_max will be turned into
74614409b5Sahrens  * 1<<zfs_vdev_cache_bshift byte reads by the vdev_cache (aka software
7587db74c1Sek110237  * track buffer).  At most zfs_vdev_cache_size bytes will be kept in each
76614409b5Sahrens  * vdev's vdev_cache.
77b68a40a8SGarrett D'Amore  *
78b68a40a8SGarrett D'Amore  * TODO: Note that with the current ZFS code, it turns out that the
79b68a40a8SGarrett D'Amore  * vdev cache is not helpful, and in some cases actually harmful.  It
80b68a40a8SGarrett D'Amore  * is better if we disable this.  Once some time has passed, we should
81b68a40a8SGarrett D'Amore  * actually remove this to simplify the code.  For now we just disable
82b68a40a8SGarrett D'Amore  * it by setting the zfs_vdev_cache_size to zero.  Note that Solaris 11
83b68a40a8SGarrett D'Amore  * has made these same changes.
84614409b5Sahrens  */
8587db74c1Sek110237 int zfs_vdev_cache_max = 1<<14;			/* 16KB */
86b68a40a8SGarrett D'Amore int zfs_vdev_cache_size = 0;
87614409b5Sahrens int zfs_vdev_cache_bshift = 16;
88614409b5Sahrens 
8987db74c1Sek110237 #define	VCBS (1 << zfs_vdev_cache_bshift)	/* 64KB */
9087db74c1Sek110237 
9187db74c1Sek110237 kstat_t	*vdc_ksp = NULL;
9287db74c1Sek110237 
9387db74c1Sek110237 typedef struct vdc_stats {
9487db74c1Sek110237 	kstat_named_t vdc_stat_delegations;
9587db74c1Sek110237 	kstat_named_t vdc_stat_hits;
9687db74c1Sek110237 	kstat_named_t vdc_stat_misses;
9787db74c1Sek110237 } vdc_stats_t;
9887db74c1Sek110237 
9987db74c1Sek110237 static vdc_stats_t vdc_stats = {
10087db74c1Sek110237 	{ "delegations",	KSTAT_DATA_UINT64 },
10187db74c1Sek110237 	{ "hits",		KSTAT_DATA_UINT64 },
10287db74c1Sek110237 	{ "misses",		KSTAT_DATA_UINT64 }
10387db74c1Sek110237 };
10487db74c1Sek110237 
105*1a5e258fSJosef 'Jeff' Sipek #define	VDCSTAT_BUMP(stat)	atomic_inc_64(&vdc_stats.stat.value.ui64);
106614409b5Sahrens 
107fa9e4066Sahrens static int
108fa9e4066Sahrens vdev_cache_offset_compare(const void *a1, const void *a2)
109fa9e4066Sahrens {
110fa9e4066Sahrens 	const vdev_cache_entry_t *ve1 = a1;
111fa9e4066Sahrens 	const vdev_cache_entry_t *ve2 = a2;
112fa9e4066Sahrens 
113fa9e4066Sahrens 	if (ve1->ve_offset < ve2->ve_offset)
114fa9e4066Sahrens 		return (-1);
115fa9e4066Sahrens 	if (ve1->ve_offset > ve2->ve_offset)
116fa9e4066Sahrens 		return (1);
117fa9e4066Sahrens 	return (0);
118fa9e4066Sahrens }
119fa9e4066Sahrens 
120fa9e4066Sahrens static int
121fa9e4066Sahrens vdev_cache_lastused_compare(const void *a1, const void *a2)
122fa9e4066Sahrens {
123fa9e4066Sahrens 	const vdev_cache_entry_t *ve1 = a1;
124fa9e4066Sahrens 	const vdev_cache_entry_t *ve2 = a2;
125fa9e4066Sahrens 
126fa9e4066Sahrens 	if (ve1->ve_lastused < ve2->ve_lastused)
127fa9e4066Sahrens 		return (-1);
128fa9e4066Sahrens 	if (ve1->ve_lastused > ve2->ve_lastused)
129fa9e4066Sahrens 		return (1);
130fa9e4066Sahrens 
131fa9e4066Sahrens 	/*
132fa9e4066Sahrens 	 * Among equally old entries, sort by offset to ensure uniqueness.
133fa9e4066Sahrens 	 */
134fa9e4066Sahrens 	return (vdev_cache_offset_compare(a1, a2));
135fa9e4066Sahrens }
136fa9e4066Sahrens 
137fa9e4066Sahrens /*
138fa9e4066Sahrens  * Evict the specified entry from the cache.
139fa9e4066Sahrens  */
140fa9e4066Sahrens static void
141fa9e4066Sahrens vdev_cache_evict(vdev_cache_t *vc, vdev_cache_entry_t *ve)
142fa9e4066Sahrens {
143fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&vc->vc_lock));
144fa9e4066Sahrens 	ASSERT(ve->ve_fill_io == NULL);
145fa9e4066Sahrens 	ASSERT(ve->ve_data != NULL);
146fa9e4066Sahrens 
147fa9e4066Sahrens 	avl_remove(&vc->vc_lastused_tree, ve);
148fa9e4066Sahrens 	avl_remove(&vc->vc_offset_tree, ve);
149614409b5Sahrens 	zio_buf_free(ve->ve_data, VCBS);
150fa9e4066Sahrens 	kmem_free(ve, sizeof (vdev_cache_entry_t));
151fa9e4066Sahrens }
152fa9e4066Sahrens 
153fa9e4066Sahrens /*
154fa9e4066Sahrens  * Allocate an entry in the cache.  At the point we don't have the data,
155fa9e4066Sahrens  * we're just creating a placeholder so that multiple threads don't all
156fa9e4066Sahrens  * go off and read the same blocks.
157fa9e4066Sahrens  */
158fa9e4066Sahrens static vdev_cache_entry_t *
159fa9e4066Sahrens vdev_cache_allocate(zio_t *zio)
160fa9e4066Sahrens {
161fa9e4066Sahrens 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
162614409b5Sahrens 	uint64_t offset = P2ALIGN(zio->io_offset, VCBS);
163fa9e4066Sahrens 	vdev_cache_entry_t *ve;
164fa9e4066Sahrens 
165fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&vc->vc_lock));
166fa9e4066Sahrens 
167614409b5Sahrens 	if (zfs_vdev_cache_size == 0)
168fa9e4066Sahrens 		return (NULL);
169fa9e4066Sahrens 
170fa9e4066Sahrens 	/*
171fa9e4066Sahrens 	 * If adding a new entry would exceed the cache size,
172fa9e4066Sahrens 	 * evict the oldest entry (LRU).
173fa9e4066Sahrens 	 */
174614409b5Sahrens 	if ((avl_numnodes(&vc->vc_lastused_tree) << zfs_vdev_cache_bshift) >
175614409b5Sahrens 	    zfs_vdev_cache_size) {
176fa9e4066Sahrens 		ve = avl_first(&vc->vc_lastused_tree);
177e14bb325SJeff Bonwick 		if (ve->ve_fill_io != NULL)
178fa9e4066Sahrens 			return (NULL);
179fa9e4066Sahrens 		ASSERT(ve->ve_hits != 0);
180fa9e4066Sahrens 		vdev_cache_evict(vc, ve);
181fa9e4066Sahrens 	}
182fa9e4066Sahrens 
183fa9e4066Sahrens 	ve = kmem_zalloc(sizeof (vdev_cache_entry_t), KM_SLEEP);
184fa9e4066Sahrens 	ve->ve_offset = offset;
185d3d50737SRafael Vanoni 	ve->ve_lastused = ddi_get_lbolt();
186614409b5Sahrens 	ve->ve_data = zio_buf_alloc(VCBS);
187fa9e4066Sahrens 
188fa9e4066Sahrens 	avl_add(&vc->vc_offset_tree, ve);
189fa9e4066Sahrens 	avl_add(&vc->vc_lastused_tree, ve);
190fa9e4066Sahrens 
191fa9e4066Sahrens 	return (ve);
192fa9e4066Sahrens }
193fa9e4066Sahrens 
194fa9e4066Sahrens static void
195fa9e4066Sahrens vdev_cache_hit(vdev_cache_t *vc, vdev_cache_entry_t *ve, zio_t *zio)
196fa9e4066Sahrens {
197614409b5Sahrens 	uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
198fa9e4066Sahrens 
199fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&vc->vc_lock));
200fa9e4066Sahrens 	ASSERT(ve->ve_fill_io == NULL);
201fa9e4066Sahrens 
202d3d50737SRafael Vanoni 	if (ve->ve_lastused != ddi_get_lbolt()) {
203fa9e4066Sahrens 		avl_remove(&vc->vc_lastused_tree, ve);
204d3d50737SRafael Vanoni 		ve->ve_lastused = ddi_get_lbolt();
205fa9e4066Sahrens 		avl_add(&vc->vc_lastused_tree, ve);
206fa9e4066Sahrens 	}
207fa9e4066Sahrens 
208fa9e4066Sahrens 	ve->ve_hits++;
209fa9e4066Sahrens 	bcopy(ve->ve_data + cache_phase, zio->io_data, zio->io_size);
210fa9e4066Sahrens }
211fa9e4066Sahrens 
212fa9e4066Sahrens /*
213fa9e4066Sahrens  * Fill a previously allocated cache entry with data.
214fa9e4066Sahrens  */
215fa9e4066Sahrens static void
216a3f829aeSBill Moore vdev_cache_fill(zio_t *fio)
217fa9e4066Sahrens {
218a3f829aeSBill Moore 	vdev_t *vd = fio->io_vd;
219fa9e4066Sahrens 	vdev_cache_t *vc = &vd->vdev_cache;
220a3f829aeSBill Moore 	vdev_cache_entry_t *ve = fio->io_private;
221a3f829aeSBill Moore 	zio_t *pio;
222fa9e4066Sahrens 
223a3f829aeSBill Moore 	ASSERT(fio->io_size == VCBS);
224fa9e4066Sahrens 
225fa9e4066Sahrens 	/*
226fa9e4066Sahrens 	 * Add data to the cache.
227fa9e4066Sahrens 	 */
228fa9e4066Sahrens 	mutex_enter(&vc->vc_lock);
229fa9e4066Sahrens 
230a3f829aeSBill Moore 	ASSERT(ve->ve_fill_io == fio);
231a3f829aeSBill Moore 	ASSERT(ve->ve_offset == fio->io_offset);
232a3f829aeSBill Moore 	ASSERT(ve->ve_data == fio->io_data);
233fa9e4066Sahrens 
234fa9e4066Sahrens 	ve->ve_fill_io = NULL;
235fa9e4066Sahrens 
236fa9e4066Sahrens 	/*
237fa9e4066Sahrens 	 * Even if this cache line was invalidated by a missed write update,
238fa9e4066Sahrens 	 * any reads that were queued up before the missed update are still
239fa9e4066Sahrens 	 * valid, so we can satisfy them from this line before we evict it.
240fa9e4066Sahrens 	 */
241a3f829aeSBill Moore 	while ((pio = zio_walk_parents(fio)) != NULL)
242a3f829aeSBill Moore 		vdev_cache_hit(vc, ve, pio);
243fa9e4066Sahrens 
244a3f829aeSBill Moore 	if (fio->io_error || ve->ve_missed_update)
245fa9e4066Sahrens 		vdev_cache_evict(vc, ve);
246fa9e4066Sahrens 
247fa9e4066Sahrens 	mutex_exit(&vc->vc_lock);
248fa9e4066Sahrens }
249fa9e4066Sahrens 
250fa9e4066Sahrens /*
25143466aaeSMax Grossman  * Read data from the cache.  Returns B_TRUE cache hit, B_FALSE on miss.
252fa9e4066Sahrens  */
25343466aaeSMax Grossman boolean_t
254fa9e4066Sahrens vdev_cache_read(zio_t *zio)
255fa9e4066Sahrens {
256fa9e4066Sahrens 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
257fa9e4066Sahrens 	vdev_cache_entry_t *ve, ve_search;
258614409b5Sahrens 	uint64_t cache_offset = P2ALIGN(zio->io_offset, VCBS);
259614409b5Sahrens 	uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
260fa9e4066Sahrens 	zio_t *fio;
261fa9e4066Sahrens 
262fa9e4066Sahrens 	ASSERT(zio->io_type == ZIO_TYPE_READ);
263fa9e4066Sahrens 
264fa9e4066Sahrens 	if (zio->io_flags & ZIO_FLAG_DONT_CACHE)
26543466aaeSMax Grossman 		return (B_FALSE);
266fa9e4066Sahrens 
267614409b5Sahrens 	if (zio->io_size > zfs_vdev_cache_max)
26843466aaeSMax Grossman 		return (B_FALSE);
269fa9e4066Sahrens 
270fa9e4066Sahrens 	/*
271fa9e4066Sahrens 	 * If the I/O straddles two or more cache blocks, don't cache it.
272fa9e4066Sahrens 	 */
27388b7b0f2SMatthew Ahrens 	if (P2BOUNDARY(zio->io_offset, zio->io_size, VCBS))
27443466aaeSMax Grossman 		return (B_FALSE);
275fa9e4066Sahrens 
276614409b5Sahrens 	ASSERT(cache_phase + zio->io_size <= VCBS);
277fa9e4066Sahrens 
278fa9e4066Sahrens 	mutex_enter(&vc->vc_lock);
279fa9e4066Sahrens 
280fa9e4066Sahrens 	ve_search.ve_offset = cache_offset;
281fa9e4066Sahrens 	ve = avl_find(&vc->vc_offset_tree, &ve_search, NULL);
282fa9e4066Sahrens 
283fa9e4066Sahrens 	if (ve != NULL) {
284fa9e4066Sahrens 		if (ve->ve_missed_update) {
285fa9e4066Sahrens 			mutex_exit(&vc->vc_lock);
28643466aaeSMax Grossman 			return (B_FALSE);
287fa9e4066Sahrens 		}
288fa9e4066Sahrens 
289fa9e4066Sahrens 		if ((fio = ve->ve_fill_io) != NULL) {
290fa9e4066Sahrens 			zio_vdev_io_bypass(zio);
291a3f829aeSBill Moore 			zio_add_child(zio, fio);
292fa9e4066Sahrens 			mutex_exit(&vc->vc_lock);
29387db74c1Sek110237 			VDCSTAT_BUMP(vdc_stat_delegations);
29443466aaeSMax Grossman 			return (B_TRUE);
295fa9e4066Sahrens 		}
296fa9e4066Sahrens 
297fa9e4066Sahrens 		vdev_cache_hit(vc, ve, zio);
298fa9e4066Sahrens 		zio_vdev_io_bypass(zio);
299fa9e4066Sahrens 
300fa9e4066Sahrens 		mutex_exit(&vc->vc_lock);
30187db74c1Sek110237 		VDCSTAT_BUMP(vdc_stat_hits);
30243466aaeSMax Grossman 		return (B_TRUE);
303fa9e4066Sahrens 	}
304fa9e4066Sahrens 
305fa9e4066Sahrens 	ve = vdev_cache_allocate(zio);
306fa9e4066Sahrens 
307fa9e4066Sahrens 	if (ve == NULL) {
308fa9e4066Sahrens 		mutex_exit(&vc->vc_lock);
30943466aaeSMax Grossman 		return (B_FALSE);
310fa9e4066Sahrens 	}
311fa9e4066Sahrens 
312e14bb325SJeff Bonwick 	fio = zio_vdev_delegated_io(zio->io_vd, cache_offset,
31369962b56SMatthew Ahrens 	    ve->ve_data, VCBS, ZIO_TYPE_READ, ZIO_PRIORITY_NOW,
314e14bb325SJeff Bonwick 	    ZIO_FLAG_DONT_CACHE, vdev_cache_fill, ve);
315fa9e4066Sahrens 
316fa9e4066Sahrens 	ve->ve_fill_io = fio;
317fa9e4066Sahrens 	zio_vdev_io_bypass(zio);
318a3f829aeSBill Moore 	zio_add_child(zio, fio);
319fa9e4066Sahrens 
320fa9e4066Sahrens 	mutex_exit(&vc->vc_lock);
321fa9e4066Sahrens 	zio_nowait(fio);
32287db74c1Sek110237 	VDCSTAT_BUMP(vdc_stat_misses);
323fa9e4066Sahrens 
32443466aaeSMax Grossman 	return (B_TRUE);
325fa9e4066Sahrens }
326fa9e4066Sahrens 
327fa9e4066Sahrens /*
328fa9e4066Sahrens  * Update cache contents upon write completion.
329fa9e4066Sahrens  */
330fa9e4066Sahrens void
331fa9e4066Sahrens vdev_cache_write(zio_t *zio)
332fa9e4066Sahrens {
333fa9e4066Sahrens 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
334fa9e4066Sahrens 	vdev_cache_entry_t *ve, ve_search;
335fa9e4066Sahrens 	uint64_t io_start = zio->io_offset;
336fa9e4066Sahrens 	uint64_t io_end = io_start + zio->io_size;
337614409b5Sahrens 	uint64_t min_offset = P2ALIGN(io_start, VCBS);
338614409b5Sahrens 	uint64_t max_offset = P2ROUNDUP(io_end, VCBS);
339fa9e4066Sahrens 	avl_index_t where;
340fa9e4066Sahrens 
341fa9e4066Sahrens 	ASSERT(zio->io_type == ZIO_TYPE_WRITE);
342fa9e4066Sahrens 
343fa9e4066Sahrens 	mutex_enter(&vc->vc_lock);
344fa9e4066Sahrens 
345fa9e4066Sahrens 	ve_search.ve_offset = min_offset;
346fa9e4066Sahrens 	ve = avl_find(&vc->vc_offset_tree, &ve_search, &where);
347fa9e4066Sahrens 
348fa9e4066Sahrens 	if (ve == NULL)
349fa9e4066Sahrens 		ve = avl_nearest(&vc->vc_offset_tree, where, AVL_AFTER);
350fa9e4066Sahrens 
351fa9e4066Sahrens 	while (ve != NULL && ve->ve_offset < max_offset) {
352fa9e4066Sahrens 		uint64_t start = MAX(ve->ve_offset, io_start);
353614409b5Sahrens 		uint64_t end = MIN(ve->ve_offset + VCBS, io_end);
354fa9e4066Sahrens 
355fa9e4066Sahrens 		if (ve->ve_fill_io != NULL) {
356fa9e4066Sahrens 			ve->ve_missed_update = 1;
357fa9e4066Sahrens 		} else {
358fa9e4066Sahrens 			bcopy((char *)zio->io_data + start - io_start,
359fa9e4066Sahrens 			    ve->ve_data + start - ve->ve_offset, end - start);
360fa9e4066Sahrens 		}
361fa9e4066Sahrens 		ve = AVL_NEXT(&vc->vc_offset_tree, ve);
362fa9e4066Sahrens 	}
363fa9e4066Sahrens 	mutex_exit(&vc->vc_lock);
364fa9e4066Sahrens }
365fa9e4066Sahrens 
366fa9e4066Sahrens void
3673d7072f8Seschrock vdev_cache_purge(vdev_t *vd)
3683d7072f8Seschrock {
3693d7072f8Seschrock 	vdev_cache_t *vc = &vd->vdev_cache;
3703d7072f8Seschrock 	vdev_cache_entry_t *ve;
3713d7072f8Seschrock 
3723d7072f8Seschrock 	mutex_enter(&vc->vc_lock);
3733d7072f8Seschrock 	while ((ve = avl_first(&vc->vc_offset_tree)) != NULL)
3743d7072f8Seschrock 		vdev_cache_evict(vc, ve);
3753d7072f8Seschrock 	mutex_exit(&vc->vc_lock);
3763d7072f8Seschrock }
3773d7072f8Seschrock 
3783d7072f8Seschrock void
379fa9e4066Sahrens vdev_cache_init(vdev_t *vd)
380fa9e4066Sahrens {
381fa9e4066Sahrens 	vdev_cache_t *vc = &vd->vdev_cache;
382fa9e4066Sahrens 
383fa9e4066Sahrens 	mutex_init(&vc->vc_lock, NULL, MUTEX_DEFAULT, NULL);
384fa9e4066Sahrens 
385fa9e4066Sahrens 	avl_create(&vc->vc_offset_tree, vdev_cache_offset_compare,
386fa9e4066Sahrens 	    sizeof (vdev_cache_entry_t),
387fa9e4066Sahrens 	    offsetof(struct vdev_cache_entry, ve_offset_node));
388fa9e4066Sahrens 
389fa9e4066Sahrens 	avl_create(&vc->vc_lastused_tree, vdev_cache_lastused_compare,
390fa9e4066Sahrens 	    sizeof (vdev_cache_entry_t),
391fa9e4066Sahrens 	    offsetof(struct vdev_cache_entry, ve_lastused_node));
392fa9e4066Sahrens }
393fa9e4066Sahrens 
394fa9e4066Sahrens void
395fa9e4066Sahrens vdev_cache_fini(vdev_t *vd)
396fa9e4066Sahrens {
397fa9e4066Sahrens 	vdev_cache_t *vc = &vd->vdev_cache;
398fa9e4066Sahrens 
3993d7072f8Seschrock 	vdev_cache_purge(vd);
400fa9e4066Sahrens 
401fa9e4066Sahrens 	avl_destroy(&vc->vc_offset_tree);
402fa9e4066Sahrens 	avl_destroy(&vc->vc_lastused_tree);
403fa9e4066Sahrens 
404fa9e4066Sahrens 	mutex_destroy(&vc->vc_lock);
405fa9e4066Sahrens }
40687db74c1Sek110237 
40787db74c1Sek110237 void
40887db74c1Sek110237 vdev_cache_stat_init(void)
40987db74c1Sek110237 {
41087db74c1Sek110237 	vdc_ksp = kstat_create("zfs", 0, "vdev_cache_stats", "misc",
41187db74c1Sek110237 	    KSTAT_TYPE_NAMED, sizeof (vdc_stats) / sizeof (kstat_named_t),
41287db74c1Sek110237 	    KSTAT_FLAG_VIRTUAL);
41387db74c1Sek110237 	if (vdc_ksp != NULL) {
41487db74c1Sek110237 		vdc_ksp->ks_data = &vdc_stats;
41587db74c1Sek110237 		kstat_install(vdc_ksp);
41687db74c1Sek110237 	}
41787db74c1Sek110237 }
41887db74c1Sek110237 
41987db74c1Sek110237 void
42087db74c1Sek110237 vdev_cache_stat_fini(void)
42187db74c1Sek110237 {
42287db74c1Sek110237 	if (vdc_ksp != NULL) {
42387db74c1Sek110237 		kstat_delete(vdc_ksp);
42487db74c1Sek110237 		vdc_ksp = NULL;
42587db74c1Sek110237 	}
42687db74c1Sek110237 }
427