xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_cache.c (revision 4d7988d6)
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 /*
26770499e1SDan Kimmel  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
27be6fd75aSMatthew Ahrens  */
29fa9e4066Sahrens #include <sys/zfs_context.h>
30fa9e4066Sahrens #include <sys/spa.h>
31fa9e4066Sahrens #include <sys/vdev_impl.h>
32fa9e4066Sahrens #include <sys/zio.h>
3387db74c1Sek #include <sys/kstat.h>
34770499e1SDan Kimmel #include <sys/abd.h>
36fa9e4066Sahrens /*
37fa9e4066Sahrens  * Virtual device read-ahead caching.
38fa9e4066Sahrens  *
39fa9e4066Sahrens  * This file implements a simple LRU read-ahead cache.  When the DMU reads
40fa9e4066Sahrens  * a given block, it will often want other, nearby blocks soon thereafter.
41fa9e4066Sahrens  * We take advantage of this by reading a larger disk region and caching
4287db74c1Sek  * the result.  In the best case, this can turn 128 back-to-back 512-byte
4387db74c1Sek  * reads into a single 64k read followed by 127 cache hits; this reduces
44fa9e4066Sahrens  * latency dramatically.  In the worst case, it can turn an isolated 512-byte
4587db74c1Sek  * read into a 64k read, which doesn't affect latency all that much but is
46fa9e4066Sahrens  * terribly wasteful of bandwidth.  A more intelligent version of the cache
47fa9e4066Sahrens  * could keep track of access patterns and not do read-ahead unless it sees
48fdb2e906Sek  * at least two temporally close I/Os to the same region.  Currently, only
49fdb2e906Sek  * metadata I/O is inflated.  A futher enhancement could take advantage of
50fdb2e906Sek  * more semantic information about the I/O.  And it could use something
51fdb2e906Sek  * faster than an AVL tree; that was chosen solely for convenience.
52fa9e4066Sahrens  *
53fa9e4066Sahrens  * There are five cache operations: allocate, fill, read, write, evict.
54fa9e4066Sahrens  *
55fa9e4066Sahrens  * (1) Allocate.  This reserves a cache entry for the specified region.
56fa9e4066Sahrens  *     We separate the allocate and fill operations so that multiple threads
57fa9e4066Sahrens  *     don't generate I/O for the same cache miss.
58fa9e4066Sahrens  *
59fa9e4066Sahrens  * (2) Fill.  When the I/O for a cache miss completes, the fill routine
60fa9e4066Sahrens  *     places the data in the previously allocated cache entry.
61fa9e4066Sahrens  *
62fa9e4066Sahrens  * (3) Read.  Read data from the cache.
63fa9e4066Sahrens  *
64fa9e4066Sahrens  * (4) Write.  Update cache contents after write completion.
65fa9e4066Sahrens  *
66fa9e4066Sahrens  * (5) Evict.  When allocating a new entry, we evict the oldest (LRU) entry
67614409b5Sahrens  *     if the total cache size exceeds zfs_vdev_cache_size.
68fa9e4066Sahrens  */
70614409b5Sahrens /*
71614409b5Sahrens  * These tunables are for performance analysis.
72614409b5Sahrens  */
73614409b5Sahrens /*
74614409b5Sahrens  * All i/os smaller than zfs_vdev_cache_max will be turned into
75614409b5Sahrens  * 1<<zfs_vdev_cache_bshift byte reads by the vdev_cache (aka software
7687db74c1Sek  * track buffer).  At most zfs_vdev_cache_size bytes will be kept in each
77614409b5Sahrens  * vdev's vdev_cache.
78b68a40a8SGarrett D'Amore  *
79b68a40a8SGarrett D'Amore  * TODO: Note that with the current ZFS code, it turns out that the
80b68a40a8SGarrett D'Amore  * vdev cache is not helpful, and in some cases actually harmful.  It
81b68a40a8SGarrett D'Amore  * is better if we disable this.  Once some time has passed, we should
82b68a40a8SGarrett D'Amore  * actually remove this to simplify the code.  For now we just disable
83b68a40a8SGarrett D'Amore  * it by setting the zfs_vdev_cache_size to zero.  Note that Solaris 11
84b68a40a8SGarrett D'Amore  * has made these same changes.
85614409b5Sahrens  */
8687db74c1Sek int zfs_vdev_cache_max = 1<<14;			/* 16KB */
87b68a40a8SGarrett D'Amore int zfs_vdev_cache_size = 0;
88614409b5Sahrens int zfs_vdev_cache_bshift = 16;
9087db74c1Sek #define	VCBS (1 << zfs_vdev_cache_bshift)	/* 64KB */
9287db74c1Sek kstat_t	*vdc_ksp = NULL;
9487db74c1Sek typedef struct vdc_stats {
9587db74c1Sek 	kstat_named_t vdc_stat_delegations;
9687db74c1Sek 	kstat_named_t vdc_stat_hits;
9787db74c1Sek 	kstat_named_t vdc_stat_misses;
9887db74c1Sek } vdc_stats_t;
10087db74c1Sek static vdc_stats_t vdc_stats = {
10187db74c1Sek 	{ "delegations",	KSTAT_DATA_UINT64 },
10287db74c1Sek 	{ "hits",		KSTAT_DATA_UINT64 },
10387db74c1Sek 	{ "misses",		KSTAT_DATA_UINT64 }
10487db74c1Sek };
1061a5e258fSJosef 'Jeff' Sipek #define	VDCSTAT_BUMP(stat)	atomic_inc_64(&vdc_stats.stat.value.ui64);
108c4ab0d3fSGvozden Neskovic static inline int
vdev_cache_offset_compare(const void * a1,const void * a2)109fa9e4066Sahrens vdev_cache_offset_compare(const void *a1, const void *a2)
110fa9e4066Sahrens {
111c4ab0d3fSGvozden Neskovic 	const vdev_cache_entry_t *ve1 = (const vdev_cache_entry_t *)a1;
112c4ab0d3fSGvozden Neskovic 	const vdev_cache_entry_t *ve2 = (const vdev_cache_entry_t *)a2;
113c4ab0d3fSGvozden Neskovic 
114*4d7988d6SPaul Dagnelie 	return (TREE_CMP(ve1->ve_offset, ve2->ve_offset));
115fa9e4066Sahrens }
117fa9e4066Sahrens static int
vdev_cache_lastused_compare(const void * a1,const void * a2)118fa9e4066Sahrens vdev_cache_lastused_compare(const void *a1, const void *a2)
119fa9e4066Sahrens {
120c4ab0d3fSGvozden Neskovic 	const vdev_cache_entry_t *ve1 = (const vdev_cache_entry_t *)a1;
121c4ab0d3fSGvozden Neskovic 	const vdev_cache_entry_t *ve2 = (const vdev_cache_entry_t *)a2;
123*4d7988d6SPaul Dagnelie 	int cmp = TREE_CMP(ve1->ve_lastused, ve2->ve_lastused);
124c4ab0d3fSGvozden Neskovic 	if (likely(cmp))
125c4ab0d3fSGvozden Neskovic 		return (cmp);
127fa9e4066Sahrens 	/*
128fa9e4066Sahrens 	 * Among equally old entries, sort by offset to ensure uniqueness.
129fa9e4066Sahrens 	 */
130fa9e4066Sahrens 	return (vdev_cache_offset_compare(a1, a2));
131fa9e4066Sahrens }
133fa9e4066Sahrens /*
134fa9e4066Sahrens  * Evict the specified entry from the cache.
135fa9e4066Sahrens  */
136fa9e4066Sahrens static void
vdev_cache_evict(vdev_cache_t * vc,vdev_cache_entry_t * ve)137fa9e4066Sahrens vdev_cache_evict(vdev_cache_t *vc, vdev_cache_entry_t *ve)
138fa9e4066Sahrens {
139fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&vc->vc_lock));
140770499e1SDan Kimmel 	ASSERT3P(ve->ve_fill_io, ==, NULL);
141770499e1SDan Kimmel 	ASSERT3P(ve->ve_abd, !=, NULL);
143fa9e4066Sahrens 	avl_remove(&vc->vc_lastused_tree, ve);
144fa9e4066Sahrens 	avl_remove(&vc->vc_offset_tree, ve);
145770499e1SDan Kimmel 	abd_free(ve->ve_abd);
146fa9e4066Sahrens 	kmem_free(ve, sizeof (vdev_cache_entry_t));
147fa9e4066Sahrens }
149fa9e4066Sahrens /*
150fa9e4066Sahrens  * Allocate an entry in the cache.  At the point we don't have the data,
151fa9e4066Sahrens  * we're just creating a placeholder so that multiple threads don't all
152fa9e4066Sahrens  * go off and read the same blocks.
153fa9e4066Sahrens  */
154fa9e4066Sahrens static vdev_cache_entry_t *
vdev_cache_allocate(zio_t * zio)155fa9e4066Sahrens vdev_cache_allocate(zio_t *zio)
156fa9e4066Sahrens {
157fa9e4066Sahrens 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
158614409b5Sahrens 	uint64_t offset = P2ALIGN(zio->io_offset, VCBS);
159fa9e4066Sahrens 	vdev_cache_entry_t *ve;
161fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&vc->vc_lock));
163614409b5Sahrens 	if (zfs_vdev_cache_size == 0)
164fa9e4066Sahrens 		return (NULL);
166fa9e4066Sahrens 	/*
167fa9e4066Sahrens 	 * If adding a new entry would exceed the cache size,
168fa9e4066Sahrens 	 * evict the oldest entry (LRU).
169fa9e4066Sahrens 	 */
170614409b5Sahrens 	if ((avl_numnodes(&vc->vc_lastused_tree) << zfs_vdev_cache_bshift) >
171614409b5Sahrens 	    zfs_vdev_cache_size) {
172fa9e4066Sahrens 		ve = avl_first(&vc->vc_lastused_tree);
173e14bb325SJeff Bonwick 		if (ve->ve_fill_io != NULL)
174fa9e4066Sahrens 			return (NULL);
175770499e1SDan Kimmel 		ASSERT3U(ve->ve_hits, !=, 0);
176fa9e4066Sahrens 		vdev_cache_evict(vc, ve);
177fa9e4066Sahrens 	}
179fa9e4066Sahrens 	ve = kmem_zalloc(sizeof (vdev_cache_entry_t), KM_SLEEP);
180fa9e4066Sahrens 	ve->ve_offset = offset;
181d3d50737SRafael Vanoni 	ve->ve_lastused = ddi_get_lbolt();
182770499e1SDan Kimmel 	ve->ve_abd = abd_alloc_for_io(VCBS, B_TRUE);
184fa9e4066Sahrens 	avl_add(&vc->vc_offset_tree, ve);
185fa9e4066Sahrens 	avl_add(&vc->vc_lastused_tree, ve);
187fa9e4066Sahrens 	return (ve);
188fa9e4066Sahrens }
190fa9e4066Sahrens static void
vdev_cache_hit(vdev_cache_t * vc,vdev_cache_entry_t * ve,zio_t * zio)191fa9e4066Sahrens vdev_cache_hit(vdev_cache_t *vc, vdev_cache_entry_t *ve, zio_t *zio)
192fa9e4066Sahrens {
193614409b5Sahrens 	uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
195fa9e4066Sahrens 	ASSERT(MUTEX_HELD(&vc->vc_lock));
196770499e1SDan Kimmel 	ASSERT3P(ve->ve_fill_io, ==, NULL);
198d3d50737SRafael Vanoni 	if (ve->ve_lastused != ddi_get_lbolt()) {
199fa9e4066Sahrens 		avl_remove(&vc->vc_lastused_tree, ve);
200d3d50737SRafael Vanoni 		ve->ve_lastused = ddi_get_lbolt();
201fa9e4066Sahrens 		avl_add(&vc->vc_lastused_tree, ve);
202fa9e4066Sahrens 	}
204fa9e4066Sahrens 	ve->ve_hits++;
205770499e1SDan Kimmel 	abd_copy_off(zio->io_abd, ve->ve_abd, 0, cache_phase, zio->io_size);
206fa9e4066Sahrens }
208fa9e4066Sahrens /*
209fa9e4066Sahrens  * Fill a previously allocated cache entry with data.
210fa9e4066Sahrens  */
211fa9e4066Sahrens static void
vdev_cache_fill(zio_t * fio)212a3f829aeSBill Moore vdev_cache_fill(zio_t *fio)
213fa9e4066Sahrens {
214a3f829aeSBill Moore 	vdev_t *vd = fio->io_vd;
215fa9e4066Sahrens 	vdev_cache_t *vc = &vd->vdev_cache;
216a3f829aeSBill Moore 	vdev_cache_entry_t *ve = fio->io_private;
217a3f829aeSBill Moore 	zio_t *pio;
219770499e1SDan Kimmel 	ASSERT3U(fio->io_size, ==, VCBS);
221fa9e4066Sahrens 	/*
222fa9e4066Sahrens 	 * Add data to the cache.
223fa9e4066Sahrens 	 */
224fa9e4066Sahrens 	mutex_enter(&vc->vc_lock);
226770499e1SDan Kimmel 	ASSERT3P(ve->ve_fill_io, ==, fio);
227770499e1SDan Kimmel 	ASSERT3U(ve->ve_offset, ==, fio->io_offset);
228770499e1SDan Kimmel 	ASSERT3P(ve->ve_abd, ==, fio->io_abd);
230fa9e4066Sahrens 	ve->ve_fill_io = NULL;
232fa9e4066Sahrens 	/*
233fa9e4066Sahrens 	 * Even if this cache line was invalidated by a missed write update,
234fa9e4066Sahrens 	 * any reads that were queued up before the missed update are still
235fa9e4066Sahrens 	 * valid, so we can satisfy them from this line before we evict it.
236fa9e4066Sahrens 	 */
2370f7643c7SGeorge Wilson 	zio_link_t *zl = NULL;
2380f7643c7SGeorge Wilson 	while ((pio = zio_walk_parents(fio, &zl)) != NULL)
239a3f829aeSBill Moore 		vdev_cache_hit(vc, ve, pio);
241a3f829aeSBill Moore 	if (fio->io_error || ve->ve_missed_update)
242fa9e4066Sahrens 		vdev_cache_evict(vc, ve);
244fa9e4066Sahrens 	mutex_exit(&vc->vc_lock);
245fa9e4066Sahrens }
247fa9e4066Sahrens /*
24843466aaeSMax Grossman  * Read data from the cache.  Returns B_TRUE cache hit, B_FALSE on miss.
249fa9e4066Sahrens  */
25043466aaeSMax Grossman boolean_t
vdev_cache_read(zio_t * zio)251fa9e4066Sahrens vdev_cache_read(zio_t *zio)
252fa9e4066Sahrens {
253fa9e4066Sahrens 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
254fa9e4066Sahrens 	vdev_cache_entry_t *ve, ve_search;
255614409b5Sahrens 	uint64_t cache_offset = P2ALIGN(zio->io_offset, VCBS);
256614409b5Sahrens 	uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
257fa9e4066Sahrens 	zio_t *fio;
259770499e1SDan Kimmel 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
261fa9e4066Sahrens 	if (zio->io_flags & ZIO_FLAG_DONT_CACHE)
26243466aaeSMax Grossman 		return (B_FALSE);
264614409b5Sahrens 	if (zio->io_size > zfs_vdev_cache_max)
26543466aaeSMax Grossman 		return (B_FALSE);
267fa9e4066Sahrens 	/*
268fa9e4066Sahrens 	 * If the I/O straddles two or more cache blocks, don't cache it.
269fa9e4066Sahrens 	 */
27088b7b0f2SMatthew Ahrens 	if (P2BOUNDARY(zio->io_offset, zio->io_size, VCBS))
27143466aaeSMax Grossman 		return (B_FALSE);
273770499e1SDan Kimmel 	ASSERT3U(cache_phase + zio->io_size, <=, VCBS);
275fa9e4066Sahrens 	mutex_enter(&vc->vc_lock);
277fa9e4066Sahrens 	ve_search.ve_offset = cache_offset;
278fa9e4066Sahrens 	ve = avl_find(&vc->vc_offset_tree, &ve_search, NULL);
280fa9e4066Sahrens 	if (ve != NULL) {
281fa9e4066Sahrens 		if (ve->ve_missed_update) {
282fa9e4066Sahrens 			mutex_exit(&vc->vc_lock);
28343466aaeSMax Grossman 			return (B_FALSE);
284fa9e4066Sahrens 		}
286fa9e4066Sahrens 		if ((fio = ve->ve_fill_io) != NULL) {
287fa9e4066Sahrens 			zio_vdev_io_bypass(zio);
288a3f829aeSBill Moore 			zio_add_child(zio, fio);
289fa9e4066Sahrens 			mutex_exit(&vc->vc_lock);
29087db74c1Sek 			VDCSTAT_BUMP(vdc_stat_delegations);
29143466aaeSMax Grossman 			return (B_TRUE);
292fa9e4066Sahrens 		}
294fa9e4066Sahrens 		vdev_cache_hit(vc, ve, zio);
295fa9e4066Sahrens 		zio_vdev_io_bypass(zio);
297fa9e4066Sahrens 		mutex_exit(&vc->vc_lock);
29887db74c1Sek 		VDCSTAT_BUMP(vdc_stat_hits);
29943466aaeSMax Grossman 		return (B_TRUE);
300fa9e4066Sahrens 	}
302fa9e4066Sahrens 	ve = vdev_cache_allocate(zio);
304fa9e4066Sahrens 	if (ve == NULL) {
305fa9e4066Sahrens 		mutex_exit(&vc->vc_lock);
30643466aaeSMax Grossman 		return (B_FALSE);
307fa9e4066Sahrens 	}
309e14bb325SJeff Bonwick 	fio = zio_vdev_delegated_io(zio->io_vd, cache_offset,
310770499e1SDan Kimmel 	    ve->ve_abd, VCBS, ZIO_TYPE_READ, ZIO_PRIORITY_NOW,
311e14bb325SJeff Bonwick 	    ZIO_FLAG_DONT_CACHE, vdev_cache_fill, ve);
313fa9e4066Sahrens 	ve->ve_fill_io = fio;
314fa9e4066Sahrens 	zio_vdev_io_bypass(zio);
315a3f829aeSBill Moore 	zio_add_child(zio, fio);
317fa9e4066Sahrens 	mutex_exit(&vc->vc_lock);
318fa9e4066Sahrens 	zio_nowait(fio);
31987db74c1Sek 	VDCSTAT_BUMP(vdc_stat_misses);
32143466aaeSMax Grossman 	return (B_TRUE);
322fa9e4066Sahrens }
324fa9e4066Sahrens /*
325fa9e4066Sahrens  * Update cache contents upon write completion.
326fa9e4066Sahrens  */
327fa9e4066Sahrens void
vdev_cache_write(zio_t * zio)328fa9e4066Sahrens vdev_cache_write(zio_t *zio)
329fa9e4066Sahrens {
330fa9e4066Sahrens 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
331fa9e4066Sahrens 	vdev_cache_entry_t *ve, ve_search;
332fa9e4066Sahrens 	uint64_t io_start = zio->io_offset;
333fa9e4066Sahrens 	uint64_t io_end = io_start + zio->io_size;
334614409b5Sahrens 	uint64_t min_offset = P2ALIGN(io_start, VCBS);
335614409b5Sahrens 	uint64_t max_offset = P2ROUNDUP(io_end, VCBS);
336fa9e4066Sahrens 	avl_index_t where;
338770499e1SDan Kimmel 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
340fa9e4066Sahrens 	mutex_enter(&vc->vc_lock);
342fa9e4066Sahrens 	ve_search.ve_offset = min_offset;
343fa9e4066Sahrens 	ve = avl_find(&vc->vc_offset_tree, &ve_search, &where);
345fa9e4066Sahrens 	if (ve == NULL)
346fa9e4066Sahrens 		ve = avl_nearest(&vc->vc_offset_tree, where, AVL_AFTER);
348fa9e4066Sahrens 	while (ve != NULL && ve->ve_offset < max_offset) {
349fa9e4066Sahrens 		uint64_t start = MAX(ve->ve_offset, io_start);
350614409b5Sahrens 		uint64_t end = MIN(ve->ve_offset + VCBS, io_end);
352fa9e4066Sahrens 		if (ve->ve_fill_io != NULL) {
353fa9e4066Sahrens 			ve->ve_missed_update = 1;
354fa9e4066Sahrens 		} else {
355770499e1SDan Kimmel 			abd_copy_off(ve->ve_abd, zio->io_abd,
356770499e1SDan Kimmel 			    start - ve->ve_offset, start - io_start,
357770499e1SDan Kimmel 			    end - start);
358fa9e4066Sahrens 		}
359fa9e4066Sahrens 		ve = AVL_NEXT(&vc->vc_offset_tree, ve);
360fa9e4066Sahrens 	}
361fa9e4066Sahrens 	mutex_exit(&vc->vc_lock);
362fa9e4066Sahrens }
3643d7072f8Seschrock void
vdev_cache_purge(vdev_t * vd)3653d7072f8Seschrock vdev_cache_purge(vdev_t *vd)
3663d7072f8Seschrock {
3673d7072f8Seschrock 	vdev_cache_t *vc = &vd->vdev_cache;
3683d7072f8Seschrock 	vdev_cache_entry_t *ve;
3703d7072f8Seschrock 	mutex_enter(&vc->vc_lock);
3713d7072f8Seschrock 	while ((ve = avl_first(&vc->vc_offset_tree)) != NULL)
3723d7072f8Seschrock 		vdev_cache_evict(vc, ve);
3733d7072f8Seschrock 	mutex_exit(&vc->vc_lock);
3743d7072f8Seschrock }
376fa9e4066Sahrens void
vdev_cache_init(vdev_t * vd)377fa9e4066Sahrens vdev_cache_init(vdev_t *vd)
378fa9e4066Sahrens {
379fa9e4066Sahrens 	vdev_cache_t *vc = &vd->vdev_cache;
381fa9e4066Sahrens 	mutex_init(&vc->vc_lock, NULL, MUTEX_DEFAULT, NULL);
383fa9e4066Sahrens 	avl_create(&vc->vc_offset_tree, vdev_cache_offset_compare,
384fa9e4066Sahrens 	    sizeof (vdev_cache_entry_t),
385fa9e4066Sahrens 	    offsetof(struct vdev_cache_entry, ve_offset_node));
387fa9e4066Sahrens 	avl_create(&vc->vc_lastused_tree, vdev_cache_lastused_compare,
388fa9e4066Sahrens 	    sizeof (vdev_cache_entry_t),
389fa9e4066Sahrens 	    offsetof(struct vdev_cache_entry, ve_lastused_node));
390fa9e4066Sahrens }
392fa9e4066Sahrens void
vdev_cache_fini(vdev_t * vd)393fa9e4066Sahrens vdev_cache_fini(vdev_t *vd)
394fa9e4066Sahrens {
395fa9e4066Sahrens 	vdev_cache_t *vc = &vd->vdev_cache;
3973d7072f8Seschrock 	vdev_cache_purge(vd);
399fa9e4066Sahrens 	avl_destroy(&vc->vc_offset_tree);
400fa9e4066Sahrens 	avl_destroy(&vc->vc_lastused_tree);
402fa9e4066Sahrens 	mutex_destroy(&vc->vc_lock);
403fa9e4066Sahrens }
40587db74c1Sek void
vdev_cache_stat_init(void)40687db74c1Sek vdev_cache_stat_init(void)
40787db74c1Sek {
40887db74c1Sek 	vdc_ksp = kstat_create("zfs", 0, "vdev_cache_stats", "misc",
40987db74c1Sek 	    KSTAT_TYPE_NAMED, sizeof (vdc_stats) / sizeof (kstat_named_t),
41087db74c1Sek 	    KSTAT_FLAG_VIRTUAL);
41187db74c1Sek 	if (vdc_ksp != NULL) {
41287db74c1Sek 		vdc_ksp->ks_data = &vdc_stats;
41387db74c1Sek 		kstat_install(vdc_ksp);
41487db74c1Sek 	}
41587db74c1Sek }
41787db74c1Sek void
vdev_cache_stat_fini(void)41887db74c1Sek vdev_cache_stat_fini(void)
41987db74c1Sek {
42087db74c1Sek 	if (vdc_ksp != NULL) {
42187db74c1Sek 		kstat_delete(vdc_ksp);
42287db74c1Sek 		vdc_ksp = NULL;
42387db74c1Sek 	}
42487db74c1Sek }