]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/vdev_cache.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / vdev_cache.c
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 #include <sys/zfs_context.h>
29 #include <sys/spa.h>
30 #include <sys/vdev_impl.h>
31 #include <sys/zio.h>
32
33 /*
34  * Virtual device read-ahead caching.
35  *
36  * This file implements a simple LRU read-ahead cache.  When the DMU reads
37  * a given block, it will often want other, nearby blocks soon thereafter.
38  * We take advantage of this by reading a larger disk region and caching
39  * the result.  In the best case, this can turn 256 back-to-back 512-byte
40  * reads into a single 128k read followed by 255 cache hits; this reduces
41  * latency dramatically.  In the worst case, it can turn an isolated 512-byte
42  * read into a 128k read, which doesn't affect latency all that much but is
43  * terribly wasteful of bandwidth.  A more intelligent version of the cache
44  * could keep track of access patterns and not do read-ahead unless it sees
45  * at least two temporally close I/Os to the same region.  It could also
46  * take advantage of semantic information about the I/O.  And it could use
47  * something faster than an AVL tree; that was chosen solely for convenience.
48  *
49  * There are five cache operations: allocate, fill, read, write, evict.
50  *
51  * (1) Allocate.  This reserves a cache entry for the specified region.
52  *     We separate the allocate and fill operations so that multiple threads
53  *     don't generate I/O for the same cache miss.
54  *
55  * (2) Fill.  When the I/O for a cache miss completes, the fill routine
56  *     places the data in the previously allocated cache entry.
57  *
58  * (3) Read.  Read data from the cache.
59  *
60  * (4) Write.  Update cache contents after write completion.
61  *
62  * (5) Evict.  When allocating a new entry, we evict the oldest (LRU) entry
63  *     if the total cache size exceeds zfs_vdev_cache_size.
64  */
65
66 /*
67  * These tunables are for performance analysis.
68  */
69 /*
70  * All i/os smaller than zfs_vdev_cache_max will be turned into
71  * 1<<zfs_vdev_cache_bshift byte reads by the vdev_cache (aka software
72  * track buffer.  At most zfs_vdev_cache_size bytes will be kept in each
73  * vdev's vdev_cache.
74  */
75 int zfs_vdev_cache_max = 1<<14;
76 int zfs_vdev_cache_size = 10ULL << 20;
77 int zfs_vdev_cache_bshift = 16;
78
79 SYSCTL_DECL(_vfs_zfs_vdev);
80 SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, cache, CTLFLAG_RW, 0, "ZFS VDEV Cache");
81 TUNABLE_INT("vfs.zfs.vdev.cache.max", &zfs_vdev_cache_max);
82 SYSCTL_INT(_vfs_zfs_vdev_cache, OID_AUTO, max, CTLFLAG_RDTUN,
83     &zfs_vdev_cache_max, 0, "Maximum I/O request size that increase read size");
84 TUNABLE_INT("vfs.zfs.vdev.cache.size", &zfs_vdev_cache_size);
85 SYSCTL_INT(_vfs_zfs_vdev_cache, OID_AUTO, size, CTLFLAG_RDTUN,
86     &zfs_vdev_cache_size, 0, "Size of VDEV cache");
87
88 #define VCBS (1 << zfs_vdev_cache_bshift)
89
90 static int
91 vdev_cache_offset_compare(const void *a1, const void *a2)
92 {
93         const vdev_cache_entry_t *ve1 = a1;
94         const vdev_cache_entry_t *ve2 = a2;
95
96         if (ve1->ve_offset < ve2->ve_offset)
97                 return (-1);
98         if (ve1->ve_offset > ve2->ve_offset)
99                 return (1);
100         return (0);
101 }
102
103 static int
104 vdev_cache_lastused_compare(const void *a1, const void *a2)
105 {
106         const vdev_cache_entry_t *ve1 = a1;
107         const vdev_cache_entry_t *ve2 = a2;
108
109         if (ve1->ve_lastused < ve2->ve_lastused)
110                 return (-1);
111         if (ve1->ve_lastused > ve2->ve_lastused)
112                 return (1);
113
114         /*
115          * Among equally old entries, sort by offset to ensure uniqueness.
116          */
117         return (vdev_cache_offset_compare(a1, a2));
118 }
119
120 /*
121  * Evict the specified entry from the cache.
122  */
123 static void
124 vdev_cache_evict(vdev_cache_t *vc, vdev_cache_entry_t *ve)
125 {
126         ASSERT(MUTEX_HELD(&vc->vc_lock));
127         ASSERT(ve->ve_fill_io == NULL);
128         ASSERT(ve->ve_data != NULL);
129
130         dprintf("evicting %p, off %llx, LRU %llu, age %lu, hits %u, stale %u\n",
131             vc, ve->ve_offset, ve->ve_lastused, LBOLT - ve->ve_lastused,
132             ve->ve_hits, ve->ve_missed_update);
133
134         avl_remove(&vc->vc_lastused_tree, ve);
135         avl_remove(&vc->vc_offset_tree, ve);
136         zio_buf_free(ve->ve_data, VCBS);
137         kmem_free(ve, sizeof (vdev_cache_entry_t));
138 }
139
140 /*
141  * Allocate an entry in the cache.  At the point we don't have the data,
142  * we're just creating a placeholder so that multiple threads don't all
143  * go off and read the same blocks.
144  */
145 static vdev_cache_entry_t *
146 vdev_cache_allocate(zio_t *zio)
147 {
148         vdev_cache_t *vc = &zio->io_vd->vdev_cache;
149         uint64_t offset = P2ALIGN(zio->io_offset, VCBS);
150         vdev_cache_entry_t *ve;
151
152         ASSERT(MUTEX_HELD(&vc->vc_lock));
153
154         if (zfs_vdev_cache_size == 0)
155                 return (NULL);
156
157         /*
158          * If adding a new entry would exceed the cache size,
159          * evict the oldest entry (LRU).
160          */
161         if ((avl_numnodes(&vc->vc_lastused_tree) << zfs_vdev_cache_bshift) >
162             zfs_vdev_cache_size) {
163                 ve = avl_first(&vc->vc_lastused_tree);
164                 if (ve->ve_fill_io != NULL) {
165                         dprintf("can't evict in %p, still filling\n", vc);
166                         return (NULL);
167                 }
168                 ASSERT(ve->ve_hits != 0);
169                 vdev_cache_evict(vc, ve);
170         }
171
172         ve = kmem_zalloc(sizeof (vdev_cache_entry_t), KM_SLEEP);
173         ve->ve_offset = offset;
174         ve->ve_lastused = LBOLT;
175         ve->ve_data = zio_buf_alloc(VCBS);
176
177         avl_add(&vc->vc_offset_tree, ve);
178         avl_add(&vc->vc_lastused_tree, ve);
179
180         return (ve);
181 }
182
183 static void
184 vdev_cache_hit(vdev_cache_t *vc, vdev_cache_entry_t *ve, zio_t *zio)
185 {
186         uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
187
188         ASSERT(MUTEX_HELD(&vc->vc_lock));
189         ASSERT(ve->ve_fill_io == NULL);
190
191         if (ve->ve_lastused != LBOLT) {
192                 avl_remove(&vc->vc_lastused_tree, ve);
193                 ve->ve_lastused = LBOLT;
194                 avl_add(&vc->vc_lastused_tree, ve);
195         }
196
197         ve->ve_hits++;
198         bcopy(ve->ve_data + cache_phase, zio->io_data, zio->io_size);
199 }
200
201 /*
202  * Fill a previously allocated cache entry with data.
203  */
204 static void
205 vdev_cache_fill(zio_t *zio)
206 {
207         vdev_t *vd = zio->io_vd;
208         vdev_cache_t *vc = &vd->vdev_cache;
209         vdev_cache_entry_t *ve = zio->io_private;
210         zio_t *dio;
211
212         ASSERT(zio->io_size == VCBS);
213
214         /*
215          * Add data to the cache.
216          */
217         mutex_enter(&vc->vc_lock);
218
219         ASSERT(ve->ve_fill_io == zio);
220         ASSERT(ve->ve_offset == zio->io_offset);
221         ASSERT(ve->ve_data == zio->io_data);
222
223         ve->ve_fill_io = NULL;
224
225         /*
226          * Even if this cache line was invalidated by a missed write update,
227          * any reads that were queued up before the missed update are still
228          * valid, so we can satisfy them from this line before we evict it.
229          */
230         for (dio = zio->io_delegate_list; dio; dio = dio->io_delegate_next)
231                 vdev_cache_hit(vc, ve, dio);
232
233         if (zio->io_error || ve->ve_missed_update)
234                 vdev_cache_evict(vc, ve);
235
236         mutex_exit(&vc->vc_lock);
237
238         while ((dio = zio->io_delegate_list) != NULL) {
239                 zio->io_delegate_list = dio->io_delegate_next;
240                 dio->io_delegate_next = NULL;
241                 dio->io_error = zio->io_error;
242                 zio_next_stage(dio);
243         }
244 }
245
246 /*
247  * Read data from the cache.  Returns 0 on cache hit, errno on a miss.
248  */
249 int
250 vdev_cache_read(zio_t *zio)
251 {
252         vdev_cache_t *vc = &zio->io_vd->vdev_cache;
253         vdev_cache_entry_t *ve, ve_search;
254         uint64_t cache_offset = P2ALIGN(zio->io_offset, VCBS);
255         uint64_t cache_phase = P2PHASE(zio->io_offset, VCBS);
256         zio_t *fio;
257
258         ASSERT(zio->io_type == ZIO_TYPE_READ);
259
260         if (zio->io_flags & ZIO_FLAG_DONT_CACHE)
261                 return (EINVAL);
262
263         if (zio->io_size > zfs_vdev_cache_max)
264                 return (EOVERFLOW);
265
266         /*
267          * If the I/O straddles two or more cache blocks, don't cache it.
268          */
269         if (P2CROSS(zio->io_offset, zio->io_offset + zio->io_size - 1, VCBS))
270                 return (EXDEV);
271
272         ASSERT(cache_phase + zio->io_size <= VCBS);
273
274         mutex_enter(&vc->vc_lock);
275
276         ve_search.ve_offset = cache_offset;
277         ve = avl_find(&vc->vc_offset_tree, &ve_search, NULL);
278
279         if (ve != NULL) {
280                 if (ve->ve_missed_update) {
281                         mutex_exit(&vc->vc_lock);
282                         return (ESTALE);
283                 }
284
285                 if ((fio = ve->ve_fill_io) != NULL) {
286                         zio->io_delegate_next = fio->io_delegate_list;
287                         fio->io_delegate_list = zio;
288                         zio_vdev_io_bypass(zio);
289                         mutex_exit(&vc->vc_lock);
290                         return (0);
291                 }
292
293                 vdev_cache_hit(vc, ve, zio);
294                 zio_vdev_io_bypass(zio);
295
296                 mutex_exit(&vc->vc_lock);
297                 zio_next_stage(zio);
298                 return (0);
299         }
300
301         ve = vdev_cache_allocate(zio);
302
303         if (ve == NULL) {
304                 mutex_exit(&vc->vc_lock);
305                 return (ENOMEM);
306         }
307
308         fio = zio_vdev_child_io(zio, NULL, zio->io_vd, cache_offset,
309             ve->ve_data, VCBS, ZIO_TYPE_READ, ZIO_PRIORITY_CACHE_FILL,
310             ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_PROPAGATE |
311             ZIO_FLAG_DONT_RETRY | ZIO_FLAG_NOBOOKMARK,
312             vdev_cache_fill, ve);
313
314         ve->ve_fill_io = fio;
315         fio->io_delegate_list = zio;
316         zio_vdev_io_bypass(zio);
317
318         mutex_exit(&vc->vc_lock);
319         zio_nowait(fio);
320
321         return (0);
322 }
323
324 /*
325  * Update cache contents upon write completion.
326  */
327 void
328 vdev_cache_write(zio_t *zio)
329 {
330         vdev_cache_t *vc = &zio->io_vd->vdev_cache;
331         vdev_cache_entry_t *ve, ve_search;
332         uint64_t io_start = zio->io_offset;
333         uint64_t io_end = io_start + zio->io_size;
334         uint64_t min_offset = P2ALIGN(io_start, VCBS);
335         uint64_t max_offset = P2ROUNDUP(io_end, VCBS);
336         avl_index_t where;
337
338         ASSERT(zio->io_type == ZIO_TYPE_WRITE);
339
340         mutex_enter(&vc->vc_lock);
341
342         ve_search.ve_offset = min_offset;
343         ve = avl_find(&vc->vc_offset_tree, &ve_search, &where);
344
345         if (ve == NULL)
346                 ve = avl_nearest(&vc->vc_offset_tree, where, AVL_AFTER);
347
348         while (ve != NULL && ve->ve_offset < max_offset) {
349                 uint64_t start = MAX(ve->ve_offset, io_start);
350                 uint64_t end = MIN(ve->ve_offset + VCBS, io_end);
351
352                 if (ve->ve_fill_io != NULL) {
353                         ve->ve_missed_update = 1;
354                 } else {
355                         bcopy((char *)zio->io_data + start - io_start,
356                             ve->ve_data + start - ve->ve_offset, end - start);
357                 }
358                 ve = AVL_NEXT(&vc->vc_offset_tree, ve);
359         }
360         mutex_exit(&vc->vc_lock);
361 }
362
363 void
364 vdev_cache_init(vdev_t *vd)
365 {
366         vdev_cache_t *vc = &vd->vdev_cache;
367
368         mutex_init(&vc->vc_lock, NULL, MUTEX_DEFAULT, NULL);
369
370         avl_create(&vc->vc_offset_tree, vdev_cache_offset_compare,
371             sizeof (vdev_cache_entry_t),
372             offsetof(struct vdev_cache_entry, ve_offset_node));
373
374         avl_create(&vc->vc_lastused_tree, vdev_cache_lastused_compare,
375             sizeof (vdev_cache_entry_t),
376             offsetof(struct vdev_cache_entry, ve_lastused_node));
377 }
378
379 void
380 vdev_cache_fini(vdev_t *vd)
381 {
382         vdev_cache_t *vc = &vd->vdev_cache;
383         vdev_cache_entry_t *ve;
384
385         mutex_enter(&vc->vc_lock);
386         while ((ve = avl_first(&vc->vc_offset_tree)) != NULL)
387                 vdev_cache_evict(vc, ve);
388         mutex_exit(&vc->vc_lock);
389
390         avl_destroy(&vc->vc_offset_tree);
391         avl_destroy(&vc->vc_lastused_tree);
392
393         mutex_destroy(&vc->vc_lock);
394 }