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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
27  */
28 
29 #include <sys/zfs_context.h>
30 #include <sys/spa.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/zio.h>
33 #include <sys/kstat.h>
34 #include <sys/abd.h>
35 
36 /*
37  * Virtual device read-ahead caching.
38  *
39  * This file implements a simple LRU read-ahead cache.  When the DMU reads
40  * a given block, it will often want other, nearby blocks soon thereafter.
41  * We take advantage of this by reading a larger disk region and caching
42  * the result.  In the best case, this can turn 128 back-to-back 512-byte
43  * reads into a single 64k read followed by 127 cache hits; this reduces
44  * latency dramatically.  In the worst case, it can turn an isolated 512-byte
45  * read into a 64k read, which doesn't affect latency all that much but is
46  * terribly wasteful of bandwidth.  A more intelligent version of the cache
47  * could keep track of access patterns and not do read-ahead unless it sees
48  * at least two temporally close I/Os to the same region.  Currently, only
49  * metadata I/O is inflated.  A futher enhancement could take advantage of
50  * more semantic information about the I/O.  And it could use something
51  * faster than an AVL tree; that was chosen solely for convenience.
52  *
53  * There are five cache operations: allocate, fill, read, write, evict.
54  *
55  * (1) Allocate.  This reserves a cache entry for the specified region.
56  *     We separate the allocate and fill operations so that multiple threads
57  *     don't generate I/O for the same cache miss.
58  *
59  * (2) Fill.  When the I/O for a cache miss completes, the fill routine
60  *     places the data in the previously allocated cache entry.
61  *
62  * (3) Read.  Read data from the cache.
63  *
64  * (4) Write.  Update cache contents after write completion.
65  *
66  * (5) Evict.  When allocating a new entry, we evict the oldest (LRU) entry
67  *     if the total cache size exceeds zfs_vdev_cache_size.
68  */
69 
70 /*
71  * These tunables are for performance analysis.
72  */
73 /*
74  * All i/os smaller than zfs_vdev_cache_max will be turned into
75  * 1<<zfs_vdev_cache_bshift byte reads by the vdev_cache (aka software
76  * track buffer).  At most zfs_vdev_cache_size bytes will be kept in each
77  * vdev's vdev_cache.
78  *
79  * TODO: Note that with the current ZFS code, it turns out that the
80  * vdev cache is not helpful, and in some cases actually harmful.  It
81  * is better if we disable this.  Once some time has passed, we should
82  * actually remove this to simplify the code.  For now we just disable
83  * it by setting the zfs_vdev_cache_size to zero.  Note that Solaris 11
84  * has made these same changes.
85  */
86 int zfs_vdev_cache_max = 1<<14;			/* 16KB */
87 int zfs_vdev_cache_size = 0;
88 int zfs_vdev_cache_bshift = 16;
89 
90 #define	VCBS (1 << zfs_vdev_cache_bshift)	/* 64KB */
91 
92 SYSCTL_DECL(_vfs_zfs_vdev);
93 SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, cache, CTLFLAG_RW, 0, "ZFS VDEV Cache");
94 SYSCTL_INT(_vfs_zfs_vdev_cache, OID_AUTO, max, CTLFLAG_RDTUN,
95     &zfs_vdev_cache_max, 0, "Maximum I/O request size that increase read size");
96 SYSCTL_INT(_vfs_zfs_vdev_cache, OID_AUTO, size, CTLFLAG_RDTUN,
97     &zfs_vdev_cache_size, 0, "Size of VDEV cache");
98 SYSCTL_INT(_vfs_zfs_vdev_cache, OID_AUTO, bshift, CTLFLAG_RDTUN,
99     &zfs_vdev_cache_bshift, 0, "Turn too small requests into 1 << this value");
100 
101 kstat_t	*vdc_ksp = NULL;
102 
103 typedef struct vdc_stats {
104 	kstat_named_t vdc_stat_delegations;
105 	kstat_named_t vdc_stat_hits;
106 	kstat_named_t vdc_stat_misses;
107 } vdc_stats_t;
108 
109 static vdc_stats_t vdc_stats = {
110 	{ "delegations",	KSTAT_DATA_UINT64 },
111 	{ "hits",		KSTAT_DATA_UINT64 },
112 	{ "misses",		KSTAT_DATA_UINT64 }
113 };
114 
115 #define	VDCSTAT_BUMP(stat)	atomic_inc_64(&vdc_stats.stat.value.ui64);
116 
117 static inline int
vdev_cache_offset_compare(const void * a1,const void * a2)118 vdev_cache_offset_compare(const void *a1, const void *a2)
119 {
120 	const vdev_cache_entry_t *ve1 = (const vdev_cache_entry_t *)a1;
121 	const vdev_cache_entry_t *ve2 = (const vdev_cache_entry_t *)a2;
122 
123 	return (AVL_CMP(ve1->ve_offset, ve2->ve_offset));
124 }
125 
126 static int
vdev_cache_lastused_compare(const void * a1,const void * a2)127 vdev_cache_lastused_compare(const void *a1, const void *a2)
128 {
129 	const vdev_cache_entry_t *ve1 = (const vdev_cache_entry_t *)a1;
130 	const vdev_cache_entry_t *ve2 = (const vdev_cache_entry_t *)a2;
131 
132 	int cmp = AVL_CMP(ve1->ve_lastused, ve2->ve_lastused);
133 	if (likely(cmp))
134 		return (cmp);
135 
136 	/*
137 	 * Among equally old entries, sort by offset to ensure uniqueness.
138 	 */
139 	return (vdev_cache_offset_compare(a1, a2));
140 }
141 
142 /*
143  * Evict the specified entry from the cache.
144  */
145 static void
vdev_cache_evict(vdev_cache_t * vc,vdev_cache_entry_t * ve)146 vdev_cache_evict(vdev_cache_t *vc, vdev_cache_entry_t *ve)
147 {
148 	ASSERT(MUTEX_HELD(&vc->vc_lock));
149 	ASSERT3P(ve->ve_fill_io, ==, NULL);
150 	ASSERT3P(ve->ve_abd, !=, NULL);
151 
152 	avl_remove(&vc->vc_lastused_tree, ve);
153 	avl_remove(&vc->vc_offset_tree, ve);
154 	abd_free(ve->ve_abd);
155 	kmem_free(ve, sizeof (vdev_cache_entry_t));
156 }
157 
158 /*
159  * Allocate an entry in the cache.  At the point we don't have the data,
160  * we're just creating a placeholder so that multiple threads don't all
161  * go off and read the same blocks.
162  */
163 static vdev_cache_entry_t *
vdev_cache_allocate(zio_t * zio)164 vdev_cache_allocate(zio_t *zio)
165 {
166 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
167 	uint64_t offset = P2ALIGN(zio->io_offset, VCBS);
168 	vdev_cache_entry_t *ve;
169 
170 	ASSERT(MUTEX_HELD(&vc->vc_lock));
171 
172 	if (zfs_vdev_cache_size == 0)
173 		return (NULL);
174 
175 	/*
176 	 * If adding a new entry would exceed the cache size,
177 	 * evict the oldest entry (LRU).
178 	 */
179 	if ((avl_numnodes(&vc->vc_lastused_tree) << zfs_vdev_cache_bshift) >
180 	    zfs_vdev_cache_size) {
181 		ve = avl_first(&vc->vc_lastused_tree);
182 		if (ve->ve_fill_io != NULL)
183 			return (NULL);
184 		ASSERT3U(ve->ve_hits, !=, 0);
185 		vdev_cache_evict(vc, ve);
186 	}
187 
188 	ve = kmem_zalloc(sizeof (vdev_cache_entry_t), KM_SLEEP);
189 	ve->ve_offset = offset;
190 	ve->ve_lastused = ddi_get_lbolt();
191 	ve->ve_abd = abd_alloc_for_io(VCBS, B_TRUE);
192 
193 	avl_add(&vc->vc_offset_tree, ve);
194 	avl_add(&vc->vc_lastused_tree, ve);
195 
196 	return (ve);
197 }
198 
199 static void
vdev_cache_hit(vdev_cache_t * vc,vdev_cache_entry_t * ve,zio_t * zio)200 vdev_cache_hit(vdev_cache_t *vc, vdev_cache_entry_t *ve, zio_t *zio)
201 {
202 	uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
203 
204 	ASSERT(MUTEX_HELD(&vc->vc_lock));
205 	ASSERT3P(ve->ve_fill_io, ==, NULL);
206 
207 	if (ve->ve_lastused != ddi_get_lbolt()) {
208 		avl_remove(&vc->vc_lastused_tree, ve);
209 		ve->ve_lastused = ddi_get_lbolt();
210 		avl_add(&vc->vc_lastused_tree, ve);
211 	}
212 
213 	ve->ve_hits++;
214 	abd_copy_off(zio->io_abd, ve->ve_abd, 0, cache_phase, zio->io_size);
215 }
216 
217 /*
218  * Fill a previously allocated cache entry with data.
219  */
220 static void
vdev_cache_fill(zio_t * fio)221 vdev_cache_fill(zio_t *fio)
222 {
223 	vdev_t *vd = fio->io_vd;
224 	vdev_cache_t *vc = &vd->vdev_cache;
225 	vdev_cache_entry_t *ve = fio->io_private;
226 	zio_t *pio;
227 
228 	ASSERT3U(fio->io_size, ==, VCBS);
229 
230 	/*
231 	 * Add data to the cache.
232 	 */
233 	mutex_enter(&vc->vc_lock);
234 
235 	ASSERT3P(ve->ve_fill_io, ==, fio);
236 	ASSERT3U(ve->ve_offset, ==, fio->io_offset);
237 	ASSERT3P(ve->ve_abd, ==, fio->io_abd);
238 
239 	ve->ve_fill_io = NULL;
240 
241 	/*
242 	 * Even if this cache line was invalidated by a missed write update,
243 	 * any reads that were queued up before the missed update are still
244 	 * valid, so we can satisfy them from this line before we evict it.
245 	 */
246 	zio_link_t *zl = NULL;
247 	while ((pio = zio_walk_parents(fio, &zl)) != NULL)
248 		vdev_cache_hit(vc, ve, pio);
249 
250 	if (fio->io_error || ve->ve_missed_update)
251 		vdev_cache_evict(vc, ve);
252 
253 	mutex_exit(&vc->vc_lock);
254 }
255 
256 /*
257  * Read data from the cache.  Returns B_TRUE cache hit, B_FALSE on miss.
258  */
259 boolean_t
vdev_cache_read(zio_t * zio)260 vdev_cache_read(zio_t *zio)
261 {
262 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
263 	vdev_cache_entry_t *ve, ve_search;
264 	uint64_t cache_offset = P2ALIGN(zio->io_offset, VCBS);
265 	uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
266 	zio_t *fio;
267 
268 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
269 
270 	if (zio->io_flags & ZIO_FLAG_DONT_CACHE)
271 		return (B_FALSE);
272 
273 	if (zio->io_size > zfs_vdev_cache_max)
274 		return (B_FALSE);
275 
276 	/*
277 	 * If the I/O straddles two or more cache blocks, don't cache it.
278 	 */
279 	if (P2BOUNDARY(zio->io_offset, zio->io_size, VCBS))
280 		return (B_FALSE);
281 
282 	ASSERT3U(cache_phase + zio->io_size, <=, VCBS);
283 
284 	mutex_enter(&vc->vc_lock);
285 
286 	ve_search.ve_offset = cache_offset;
287 	ve = avl_find(&vc->vc_offset_tree, &ve_search, NULL);
288 
289 	if (ve != NULL) {
290 		if (ve->ve_missed_update) {
291 			mutex_exit(&vc->vc_lock);
292 			return (B_FALSE);
293 		}
294 
295 		if ((fio = ve->ve_fill_io) != NULL) {
296 			zio_vdev_io_bypass(zio);
297 			zio_add_child(zio, fio);
298 			mutex_exit(&vc->vc_lock);
299 			VDCSTAT_BUMP(vdc_stat_delegations);
300 			return (B_TRUE);
301 		}
302 
303 		vdev_cache_hit(vc, ve, zio);
304 		zio_vdev_io_bypass(zio);
305 
306 		mutex_exit(&vc->vc_lock);
307 		VDCSTAT_BUMP(vdc_stat_hits);
308 		return (B_TRUE);
309 	}
310 
311 	ve = vdev_cache_allocate(zio);
312 
313 	if (ve == NULL) {
314 		mutex_exit(&vc->vc_lock);
315 		return (B_FALSE);
316 	}
317 
318 	fio = zio_vdev_delegated_io(zio->io_vd, cache_offset,
319 	    ve->ve_abd, VCBS, ZIO_TYPE_READ, ZIO_PRIORITY_NOW,
320 	    ZIO_FLAG_DONT_CACHE, vdev_cache_fill, ve);
321 
322 	ve->ve_fill_io = fio;
323 	zio_vdev_io_bypass(zio);
324 	zio_add_child(zio, fio);
325 
326 	mutex_exit(&vc->vc_lock);
327 	zio_nowait(fio);
328 	VDCSTAT_BUMP(vdc_stat_misses);
329 
330 	return (B_TRUE);
331 }
332 
333 /*
334  * Update cache contents upon write completion.
335  */
336 void
vdev_cache_write(zio_t * zio)337 vdev_cache_write(zio_t *zio)
338 {
339 	vdev_cache_t *vc = &zio->io_vd->vdev_cache;
340 	vdev_cache_entry_t *ve, ve_search;
341 	uint64_t io_start = zio->io_offset;
342 	uint64_t io_end = io_start + zio->io_size;
343 	uint64_t min_offset = P2ALIGN(io_start, VCBS);
344 	uint64_t max_offset = P2ROUNDUP(io_end, VCBS);
345 	avl_index_t where;
346 
347 	ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
348 
349 	mutex_enter(&vc->vc_lock);
350 
351 	ve_search.ve_offset = min_offset;
352 	ve = avl_find(&vc->vc_offset_tree, &ve_search, &where);
353 
354 	if (ve == NULL)
355 		ve = avl_nearest(&vc->vc_offset_tree, where, AVL_AFTER);
356 
357 	while (ve != NULL && ve->ve_offset < max_offset) {
358 		uint64_t start = MAX(ve->ve_offset, io_start);
359 		uint64_t end = MIN(ve->ve_offset + VCBS, io_end);
360 
361 		if (ve->ve_fill_io != NULL) {
362 			ve->ve_missed_update = 1;
363 		} else {
364 			abd_copy_off(ve->ve_abd, zio->io_abd,
365 			    start - ve->ve_offset, start - io_start,
366 			    end - start);
367 		}
368 		ve = AVL_NEXT(&vc->vc_offset_tree, ve);
369 	}
370 	mutex_exit(&vc->vc_lock);
371 }
372 
373 void
vdev_cache_purge(vdev_t * vd)374 vdev_cache_purge(vdev_t *vd)
375 {
376 	vdev_cache_t *vc = &vd->vdev_cache;
377 	vdev_cache_entry_t *ve;
378 
379 	mutex_enter(&vc->vc_lock);
380 	while ((ve = avl_first(&vc->vc_offset_tree)) != NULL)
381 		vdev_cache_evict(vc, ve);
382 	mutex_exit(&vc->vc_lock);
383 }
384 
385 void
vdev_cache_init(vdev_t * vd)386 vdev_cache_init(vdev_t *vd)
387 {
388 	vdev_cache_t *vc = &vd->vdev_cache;
389 
390 	mutex_init(&vc->vc_lock, NULL, MUTEX_DEFAULT, NULL);
391 
392 	avl_create(&vc->vc_offset_tree, vdev_cache_offset_compare,
393 	    sizeof (vdev_cache_entry_t),
394 	    offsetof(struct vdev_cache_entry, ve_offset_node));
395 
396 	avl_create(&vc->vc_lastused_tree, vdev_cache_lastused_compare,
397 	    sizeof (vdev_cache_entry_t),
398 	    offsetof(struct vdev_cache_entry, ve_lastused_node));
399 }
400 
401 void
vdev_cache_fini(vdev_t * vd)402 vdev_cache_fini(vdev_t *vd)
403 {
404 	vdev_cache_t *vc = &vd->vdev_cache;
405 
406 	vdev_cache_purge(vd);
407 
408 	avl_destroy(&vc->vc_offset_tree);
409 	avl_destroy(&vc->vc_lastused_tree);
410 
411 	mutex_destroy(&vc->vc_lock);
412 }
413 
414 void
vdev_cache_stat_init(void)415 vdev_cache_stat_init(void)
416 {
417 	vdc_ksp = kstat_create("zfs", 0, "vdev_cache_stats", "misc",
418 	    KSTAT_TYPE_NAMED, sizeof (vdc_stats) / sizeof (kstat_named_t),
419 	    KSTAT_FLAG_VIRTUAL);
420 	if (vdc_ksp != NULL) {
421 		vdc_ksp->ks_data = &vdc_stats;
422 		kstat_install(vdc_ksp);
423 	}
424 }
425 
426 void
vdev_cache_stat_fini(void)427 vdev_cache_stat_fini(void)
428 {
429 	if (vdc_ksp != NULL) {
430 		kstat_delete(vdc_ksp);
431 		vdc_ksp = NULL;
432 	}
433 }
434