]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dmu_zfetch.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dmu_zfetch.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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #include <sys/zfs_context.h>
27 #include <sys/dnode.h>
28 #include <sys/dmu_objset.h>
29 #include <sys/dmu_zfetch.h>
30 #include <sys/dmu.h>
31 #include <sys/dbuf.h>
32 #include <sys/kstat.h>
33
34 /*
35  * I'm against tune-ables, but these should probably exist as tweakable globals
36  * until we can get this working the way we want it to.
37  */
38
39 int zfs_prefetch_disable = 0;
40
41 /* max # of streams per zfetch */
42 uint32_t        zfetch_max_streams = 8;
43 /* min time before stream reclaim */
44 uint32_t        zfetch_min_sec_reap = 2;
45 /* max number of blocks to fetch at a time */
46 uint32_t        zfetch_block_cap = 256;
47 /* number of bytes in a array_read at which we stop prefetching (1Mb) */
48 uint64_t        zfetch_array_rd_sz = 1024 * 1024;
49
50 SYSCTL_DECL(_vfs_zfs);
51 SYSCTL_INT(_vfs_zfs, OID_AUTO, prefetch_disable, CTLFLAG_RW,
52     &zfs_prefetch_disable, 0, "Disable prefetch");
53 SYSCTL_NODE(_vfs_zfs, OID_AUTO, zfetch, CTLFLAG_RW, 0, "ZFS ZFETCH");
54 TUNABLE_INT("vfs.zfs.zfetch.max_streams", &zfetch_max_streams);
55 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, max_streams, CTLFLAG_RW,
56     &zfetch_max_streams, 0, "Max # of streams per zfetch");
57 TUNABLE_INT("vfs.zfs.zfetch.min_sec_reap", &zfetch_min_sec_reap);
58 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, min_sec_reap, CTLFLAG_RDTUN,
59     &zfetch_min_sec_reap, 0, "Min time before stream reclaim");
60 TUNABLE_INT("vfs.zfs.zfetch.block_cap", &zfetch_block_cap);
61 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, block_cap, CTLFLAG_RDTUN,
62     &zfetch_block_cap, 0, "Max number of blocks to fetch at a time");
63 TUNABLE_QUAD("vfs.zfs.zfetch.array_rd_sz", &zfetch_array_rd_sz);
64 SYSCTL_QUAD(_vfs_zfs_zfetch, OID_AUTO, array_rd_sz, CTLFLAG_RDTUN,
65     &zfetch_array_rd_sz, 0,
66     "Number of bytes in a array_read at which we stop prefetching");
67
68 /* forward decls for static routines */
69 static int              dmu_zfetch_colinear(zfetch_t *, zstream_t *);
70 static void             dmu_zfetch_dofetch(zfetch_t *, zstream_t *);
71 static uint64_t         dmu_zfetch_fetch(dnode_t *, uint64_t, uint64_t);
72 static uint64_t         dmu_zfetch_fetchsz(dnode_t *, uint64_t, uint64_t);
73 static int              dmu_zfetch_find(zfetch_t *, zstream_t *, int);
74 static int              dmu_zfetch_stream_insert(zfetch_t *, zstream_t *);
75 static zstream_t        *dmu_zfetch_stream_reclaim(zfetch_t *);
76 static void             dmu_zfetch_stream_remove(zfetch_t *, zstream_t *);
77 static int              dmu_zfetch_streams_equal(zstream_t *, zstream_t *);
78
79 typedef struct zfetch_stats {
80         kstat_named_t zfetchstat_hits;
81         kstat_named_t zfetchstat_misses;
82         kstat_named_t zfetchstat_colinear_hits;
83         kstat_named_t zfetchstat_colinear_misses;
84         kstat_named_t zfetchstat_stride_hits;
85         kstat_named_t zfetchstat_stride_misses;
86         kstat_named_t zfetchstat_reclaim_successes;
87         kstat_named_t zfetchstat_reclaim_failures;
88         kstat_named_t zfetchstat_stream_resets;
89         kstat_named_t zfetchstat_stream_noresets;
90         kstat_named_t zfetchstat_bogus_streams;
91 } zfetch_stats_t;
92
93 static zfetch_stats_t zfetch_stats = {
94         { "hits",                       KSTAT_DATA_UINT64 },
95         { "misses",                     KSTAT_DATA_UINT64 },
96         { "colinear_hits",              KSTAT_DATA_UINT64 },
97         { "colinear_misses",            KSTAT_DATA_UINT64 },
98         { "stride_hits",                KSTAT_DATA_UINT64 },
99         { "stride_misses",              KSTAT_DATA_UINT64 },
100         { "reclaim_successes",          KSTAT_DATA_UINT64 },
101         { "reclaim_failures",           KSTAT_DATA_UINT64 },
102         { "streams_resets",             KSTAT_DATA_UINT64 },
103         { "streams_noresets",           KSTAT_DATA_UINT64 },
104         { "bogus_streams",              KSTAT_DATA_UINT64 },
105 };
106
107 #define ZFETCHSTAT_INCR(stat, val) \
108         atomic_add_64(&zfetch_stats.stat.value.ui64, (val));
109
110 #define ZFETCHSTAT_BUMP(stat)           ZFETCHSTAT_INCR(stat, 1);
111
112 kstat_t         *zfetch_ksp;
113
114 /*
115  * Given a zfetch structure and a zstream structure, determine whether the
116  * blocks to be read are part of a co-linear pair of existing prefetch
117  * streams.  If a set is found, coalesce the streams, removing one, and
118  * configure the prefetch so it looks for a strided access pattern.
119  *
120  * In other words: if we find two sequential access streams that are
121  * the same length and distance N appart, and this read is N from the
122  * last stream, then we are probably in a strided access pattern.  So
123  * combine the two sequential streams into a single strided stream.
124  *
125  * If no co-linear streams are found, return NULL.
126  */
127 static int
128 dmu_zfetch_colinear(zfetch_t *zf, zstream_t *zh)
129 {
130         zstream_t       *z_walk;
131         zstream_t       *z_comp;
132
133         if (! rw_tryenter(&zf->zf_rwlock, RW_WRITER))
134                 return (0);
135
136         if (zh == NULL) {
137                 rw_exit(&zf->zf_rwlock);
138                 return (0);
139         }
140
141         for (z_walk = list_head(&zf->zf_stream); z_walk;
142             z_walk = list_next(&zf->zf_stream, z_walk)) {
143                 for (z_comp = list_next(&zf->zf_stream, z_walk); z_comp;
144                     z_comp = list_next(&zf->zf_stream, z_comp)) {
145                         int64_t         diff;
146
147                         if (z_walk->zst_len != z_walk->zst_stride ||
148                             z_comp->zst_len != z_comp->zst_stride) {
149                                 continue;
150                         }
151
152                         diff = z_comp->zst_offset - z_walk->zst_offset;
153                         if (z_comp->zst_offset + diff == zh->zst_offset) {
154                                 z_walk->zst_offset = zh->zst_offset;
155                                 z_walk->zst_direction = diff < 0 ? -1 : 1;
156                                 z_walk->zst_stride =
157                                     diff * z_walk->zst_direction;
158                                 z_walk->zst_ph_offset =
159                                     zh->zst_offset + z_walk->zst_stride;
160                                 dmu_zfetch_stream_remove(zf, z_comp);
161                                 mutex_destroy(&z_comp->zst_lock);
162                                 kmem_free(z_comp, sizeof (zstream_t));
163
164                                 dmu_zfetch_dofetch(zf, z_walk);
165
166                                 rw_exit(&zf->zf_rwlock);
167                                 return (1);
168                         }
169
170                         diff = z_walk->zst_offset - z_comp->zst_offset;
171                         if (z_walk->zst_offset + diff == zh->zst_offset) {
172                                 z_walk->zst_offset = zh->zst_offset;
173                                 z_walk->zst_direction = diff < 0 ? -1 : 1;
174                                 z_walk->zst_stride =
175                                     diff * z_walk->zst_direction;
176                                 z_walk->zst_ph_offset =
177                                     zh->zst_offset + z_walk->zst_stride;
178                                 dmu_zfetch_stream_remove(zf, z_comp);
179                                 mutex_destroy(&z_comp->zst_lock);
180                                 kmem_free(z_comp, sizeof (zstream_t));
181
182                                 dmu_zfetch_dofetch(zf, z_walk);
183
184                                 rw_exit(&zf->zf_rwlock);
185                                 return (1);
186                         }
187                 }
188         }
189
190         rw_exit(&zf->zf_rwlock);
191         return (0);
192 }
193
194 /*
195  * Given a zstream_t, determine the bounds of the prefetch.  Then call the
196  * routine that actually prefetches the individual blocks.
197  */
198 static void
199 dmu_zfetch_dofetch(zfetch_t *zf, zstream_t *zs)
200 {
201         uint64_t        prefetch_tail;
202         uint64_t        prefetch_limit;
203         uint64_t        prefetch_ofst;
204         uint64_t        prefetch_len;
205         uint64_t        blocks_fetched;
206
207         zs->zst_stride = MAX((int64_t)zs->zst_stride, zs->zst_len);
208         zs->zst_cap = MIN(zfetch_block_cap, 2 * zs->zst_cap);
209
210         prefetch_tail = MAX((int64_t)zs->zst_ph_offset,
211             (int64_t)(zs->zst_offset + zs->zst_stride));
212         /*
213          * XXX: use a faster division method?
214          */
215         prefetch_limit = zs->zst_offset + zs->zst_len +
216             (zs->zst_cap * zs->zst_stride) / zs->zst_len;
217
218         while (prefetch_tail < prefetch_limit) {
219                 prefetch_ofst = zs->zst_offset + zs->zst_direction *
220                     (prefetch_tail - zs->zst_offset);
221
222                 prefetch_len = zs->zst_len;
223
224                 /*
225                  * Don't prefetch beyond the end of the file, if working
226                  * backwards.
227                  */
228                 if ((zs->zst_direction == ZFETCH_BACKWARD) &&
229                     (prefetch_ofst > prefetch_tail)) {
230                         prefetch_len += prefetch_ofst;
231                         prefetch_ofst = 0;
232                 }
233
234                 /* don't prefetch more than we're supposed to */
235                 if (prefetch_len > zs->zst_len)
236                         break;
237
238                 blocks_fetched = dmu_zfetch_fetch(zf->zf_dnode,
239                     prefetch_ofst, zs->zst_len);
240
241                 prefetch_tail += zs->zst_stride;
242                 /* stop if we've run out of stuff to prefetch */
243                 if (blocks_fetched < zs->zst_len)
244                         break;
245         }
246         zs->zst_ph_offset = prefetch_tail;
247         zs->zst_last = LBOLT;
248 }
249
250 void
251 zfetch_init(void)
252 {
253
254         zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",
255             KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),
256             KSTAT_FLAG_VIRTUAL);
257
258         if (zfetch_ksp != NULL) {
259                 zfetch_ksp->ks_data = &zfetch_stats;
260                 kstat_install(zfetch_ksp);
261         }
262 }
263
264 void
265 zfetch_fini(void)
266 {
267         if (zfetch_ksp != NULL) {
268                 kstat_delete(zfetch_ksp);
269                 zfetch_ksp = NULL;
270         }
271 }
272
273 /*
274  * This takes a pointer to a zfetch structure and a dnode.  It performs the
275  * necessary setup for the zfetch structure, grokking data from the
276  * associated dnode.
277  */
278 void
279 dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
280 {
281         if (zf == NULL) {
282                 return;
283         }
284
285         zf->zf_dnode = dno;
286         zf->zf_stream_cnt = 0;
287         zf->zf_alloc_fail = 0;
288
289         list_create(&zf->zf_stream, sizeof (zstream_t),
290             offsetof(zstream_t, zst_node));
291
292         rw_init(&zf->zf_rwlock, NULL, RW_DEFAULT, NULL);
293 }
294
295 /*
296  * This function computes the actual size, in blocks, that can be prefetched,
297  * and fetches it.
298  */
299 static uint64_t
300 dmu_zfetch_fetch(dnode_t *dn, uint64_t blkid, uint64_t nblks)
301 {
302         uint64_t        fetchsz;
303         uint64_t        i;
304
305         fetchsz = dmu_zfetch_fetchsz(dn, blkid, nblks);
306
307         for (i = 0; i < fetchsz; i++) {
308                 dbuf_prefetch(dn, blkid + i);
309         }
310
311         return (fetchsz);
312 }
313
314 /*
315  * this function returns the number of blocks that would be prefetched, based
316  * upon the supplied dnode, blockid, and nblks.  This is used so that we can
317  * update streams in place, and then prefetch with their old value after the
318  * fact.  This way, we can delay the prefetch, but subsequent accesses to the
319  * stream won't result in the same data being prefetched multiple times.
320  */
321 static uint64_t
322 dmu_zfetch_fetchsz(dnode_t *dn, uint64_t blkid, uint64_t nblks)
323 {
324         uint64_t        fetchsz;
325
326         if (blkid > dn->dn_maxblkid) {
327                 return (0);
328         }
329
330         /* compute fetch size */
331         if (blkid + nblks + 1 > dn->dn_maxblkid) {
332                 fetchsz = (dn->dn_maxblkid - blkid) + 1;
333                 ASSERT(blkid + fetchsz - 1 <= dn->dn_maxblkid);
334         } else {
335                 fetchsz = nblks;
336         }
337
338
339         return (fetchsz);
340 }
341
342 /*
343  * given a zfetch and a zstream structure, see if there is an associated zstream
344  * for this block read.  If so, it starts a prefetch for the stream it
345  * located and returns true, otherwise it returns false
346  */
347 static int
348 dmu_zfetch_find(zfetch_t *zf, zstream_t *zh, int prefetched)
349 {
350         zstream_t       *zs;
351         int64_t         diff;
352         int             reset = !prefetched;
353         int             rc = 0;
354
355         if (zh == NULL)
356                 return (0);
357
358         /*
359          * XXX: This locking strategy is a bit coarse; however, it's impact has
360          * yet to be tested.  If this turns out to be an issue, it can be
361          * modified in a number of different ways.
362          */
363
364         rw_enter(&zf->zf_rwlock, RW_READER);
365 top:
366
367         for (zs = list_head(&zf->zf_stream); zs;
368             zs = list_next(&zf->zf_stream, zs)) {
369
370                 /*
371                  * XXX - should this be an assert?
372                  */
373                 if (zs->zst_len == 0) {
374                         /* bogus stream */
375                         ZFETCHSTAT_BUMP(zfetchstat_bogus_streams);
376                         continue;
377                 }
378
379                 /*
380                  * We hit this case when we are in a strided prefetch stream:
381                  * we will read "len" blocks before "striding".
382                  */
383                 if (zh->zst_offset >= zs->zst_offset &&
384                     zh->zst_offset < zs->zst_offset + zs->zst_len) {
385                         if (prefetched) {
386                                 /* already fetched */
387                                 ZFETCHSTAT_BUMP(zfetchstat_stride_hits);
388                                 rc = 1;
389                                 goto out;
390                         } else {
391                                 ZFETCHSTAT_BUMP(zfetchstat_stride_misses);
392                         }
393                 }
394
395                 /*
396                  * This is the forward sequential read case: we increment
397                  * len by one each time we hit here, so we will enter this
398                  * case on every read.
399                  */
400                 if (zh->zst_offset == zs->zst_offset + zs->zst_len) {
401
402                         reset = !prefetched && zs->zst_len > 1;
403
404                         if (mutex_tryenter(&zs->zst_lock) == 0) {
405                                 rc = 1;
406                                 goto out;
407                         }
408                         if (zh->zst_offset != zs->zst_offset + zs->zst_len) {
409                                 mutex_exit(&zs->zst_lock);
410                                 goto top;
411                         }
412                         zs->zst_len += zh->zst_len;
413                         diff = zs->zst_len - zfetch_block_cap;
414                         if (diff > 0) {
415                                 zs->zst_offset += diff;
416                                 zs->zst_len = zs->zst_len > diff ?
417                                     zs->zst_len - diff : 0;
418                         }
419                         zs->zst_direction = ZFETCH_FORWARD;
420
421                         break;
422
423                 /*
424                  * Same as above, but reading backwards through the file.
425                  */
426                 } else if (zh->zst_offset == zs->zst_offset - zh->zst_len) {
427                         /* backwards sequential access */
428
429                         reset = !prefetched && zs->zst_len > 1;
430
431                         if (mutex_tryenter(&zs->zst_lock) == 0) {
432                                 rc = 1;
433                                 goto out;
434                         }
435                         if (zh->zst_offset != zs->zst_offset - zh->zst_len) {
436                                 mutex_exit(&zs->zst_lock);
437                                 goto top;
438                         }
439
440                         zs->zst_offset = zs->zst_offset > zh->zst_len ?
441                             zs->zst_offset - zh->zst_len : 0;
442                         zs->zst_ph_offset = zs->zst_ph_offset > zh->zst_len ?
443                             zs->zst_ph_offset - zh->zst_len : 0;
444                         zs->zst_len += zh->zst_len;
445
446                         diff = zs->zst_len - zfetch_block_cap;
447                         if (diff > 0) {
448                                 zs->zst_ph_offset = zs->zst_ph_offset > diff ?
449                                     zs->zst_ph_offset - diff : 0;
450                                 zs->zst_len = zs->zst_len > diff ?
451                                     zs->zst_len - diff : zs->zst_len;
452                         }
453                         zs->zst_direction = ZFETCH_BACKWARD;
454
455                         break;
456
457                 } else if ((zh->zst_offset - zs->zst_offset - zs->zst_stride <
458                     zs->zst_len) && (zs->zst_len != zs->zst_stride)) {
459                         /* strided forward access */
460
461                         if (mutex_tryenter(&zs->zst_lock) == 0) {
462                                 rc = 1;
463                                 goto out;
464                         }
465                         if ((zh->zst_offset - zs->zst_offset - zs->zst_stride >=
466                             zs->zst_len) || (zs->zst_len == zs->zst_stride)) {
467                                 mutex_exit(&zs->zst_lock);
468                                 goto top;
469                         }
470
471                         zs->zst_offset += zs->zst_stride;
472                         zs->zst_direction = ZFETCH_FORWARD;
473
474                         break;
475
476                 } else if ((zh->zst_offset - zs->zst_offset + zs->zst_stride <
477                     zs->zst_len) && (zs->zst_len != zs->zst_stride)) {
478                         /* strided reverse access */
479
480                         if (mutex_tryenter(&zs->zst_lock) == 0) {
481                                 rc = 1;
482                                 goto out;
483                         }
484                         if ((zh->zst_offset - zs->zst_offset + zs->zst_stride >=
485                             zs->zst_len) || (zs->zst_len == zs->zst_stride)) {
486                                 mutex_exit(&zs->zst_lock);
487                                 goto top;
488                         }
489
490                         zs->zst_offset = zs->zst_offset > zs->zst_stride ?
491                             zs->zst_offset - zs->zst_stride : 0;
492                         zs->zst_ph_offset = (zs->zst_ph_offset >
493                             (2 * zs->zst_stride)) ?
494                             (zs->zst_ph_offset - (2 * zs->zst_stride)) : 0;
495                         zs->zst_direction = ZFETCH_BACKWARD;
496
497                         break;
498                 }
499         }
500
501         if (zs) {
502                 if (reset) {
503                         zstream_t *remove = zs;
504
505                         ZFETCHSTAT_BUMP(zfetchstat_stream_resets);
506                         rc = 0;
507                         mutex_exit(&zs->zst_lock);
508                         rw_exit(&zf->zf_rwlock);
509                         rw_enter(&zf->zf_rwlock, RW_WRITER);
510                         /*
511                          * Relocate the stream, in case someone removes
512                          * it while we were acquiring the WRITER lock.
513                          */
514                         for (zs = list_head(&zf->zf_stream); zs;
515                             zs = list_next(&zf->zf_stream, zs)) {
516                                 if (zs == remove) {
517                                         dmu_zfetch_stream_remove(zf, zs);
518                                         mutex_destroy(&zs->zst_lock);
519                                         kmem_free(zs, sizeof (zstream_t));
520                                         break;
521                                 }
522                         }
523                 } else {
524                         ZFETCHSTAT_BUMP(zfetchstat_stream_noresets);
525                         rc = 1;
526                         dmu_zfetch_dofetch(zf, zs);
527                         mutex_exit(&zs->zst_lock);
528                 }
529         }
530 out:
531         rw_exit(&zf->zf_rwlock);
532         return (rc);
533 }
534
535 /*
536  * Clean-up state associated with a zfetch structure.  This frees allocated
537  * structure members, empties the zf_stream tree, and generally makes things
538  * nice.  This doesn't free the zfetch_t itself, that's left to the caller.
539  */
540 void
541 dmu_zfetch_rele(zfetch_t *zf)
542 {
543         zstream_t       *zs;
544         zstream_t       *zs_next;
545
546         ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock));
547
548         for (zs = list_head(&zf->zf_stream); zs; zs = zs_next) {
549                 zs_next = list_next(&zf->zf_stream, zs);
550
551                 list_remove(&zf->zf_stream, zs);
552                 mutex_destroy(&zs->zst_lock);
553                 kmem_free(zs, sizeof (zstream_t));
554         }
555         list_destroy(&zf->zf_stream);
556         rw_destroy(&zf->zf_rwlock);
557
558         zf->zf_dnode = NULL;
559 }
560
561 /*
562  * Given a zfetch and zstream structure, insert the zstream structure into the
563  * AVL tree contained within the zfetch structure.  Peform the appropriate
564  * book-keeping.  It is possible that another thread has inserted a stream which
565  * matches one that we are about to insert, so we must be sure to check for this
566  * case.  If one is found, return failure, and let the caller cleanup the
567  * duplicates.
568  */
569 static int
570 dmu_zfetch_stream_insert(zfetch_t *zf, zstream_t *zs)
571 {
572         zstream_t       *zs_walk;
573         zstream_t       *zs_next;
574
575         ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
576
577         for (zs_walk = list_head(&zf->zf_stream); zs_walk; zs_walk = zs_next) {
578                 zs_next = list_next(&zf->zf_stream, zs_walk);
579
580                 if (dmu_zfetch_streams_equal(zs_walk, zs)) {
581                         return (0);
582                 }
583         }
584
585         list_insert_head(&zf->zf_stream, zs);
586         zf->zf_stream_cnt++;
587         return (1);
588 }
589
590
591 /*
592  * Walk the list of zstreams in the given zfetch, find an old one (by time), and
593  * reclaim it for use by the caller.
594  */
595 static zstream_t *
596 dmu_zfetch_stream_reclaim(zfetch_t *zf)
597 {
598         zstream_t       *zs;
599
600         if (! rw_tryenter(&zf->zf_rwlock, RW_WRITER))
601                 return (0);
602
603         for (zs = list_head(&zf->zf_stream); zs;
604             zs = list_next(&zf->zf_stream, zs)) {
605
606                 if (((LBOLT - zs->zst_last) / hz) > zfetch_min_sec_reap)
607                         break;
608         }
609
610         if (zs) {
611                 dmu_zfetch_stream_remove(zf, zs);
612                 mutex_destroy(&zs->zst_lock);
613                 bzero(zs, sizeof (zstream_t));
614         } else {
615                 zf->zf_alloc_fail++;
616         }
617         rw_exit(&zf->zf_rwlock);
618
619         return (zs);
620 }
621
622 /*
623  * Given a zfetch and zstream structure, remove the zstream structure from its
624  * container in the zfetch structure.  Perform the appropriate book-keeping.
625  */
626 static void
627 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
628 {
629         ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
630
631         list_remove(&zf->zf_stream, zs);
632         zf->zf_stream_cnt--;
633 }
634
635 static int
636 dmu_zfetch_streams_equal(zstream_t *zs1, zstream_t *zs2)
637 {
638         if (zs1->zst_offset != zs2->zst_offset)
639                 return (0);
640
641         if (zs1->zst_len != zs2->zst_len)
642                 return (0);
643
644         if (zs1->zst_stride != zs2->zst_stride)
645                 return (0);
646
647         if (zs1->zst_ph_offset != zs2->zst_ph_offset)
648                 return (0);
649
650         if (zs1->zst_cap != zs2->zst_cap)
651                 return (0);
652
653         if (zs1->zst_direction != zs2->zst_direction)
654                 return (0);
655
656         return (1);
657 }
658
659 /*
660  * This is the prefetch entry point.  It calls all of the other dmu_zfetch
661  * routines to create, delete, find, or operate upon prefetch streams.
662  */
663 void
664 dmu_zfetch(zfetch_t *zf, uint64_t offset, uint64_t size, int prefetched)
665 {
666         zstream_t       zst;
667         zstream_t       *newstream;
668         int             fetched;
669         int             inserted;
670         unsigned int    blkshft;
671         uint64_t        blksz;
672
673         if (zfs_prefetch_disable)
674                 return;
675
676         /* files that aren't ln2 blocksz are only one block -- nothing to do */
677         if (!zf->zf_dnode->dn_datablkshift)
678                 return;
679
680         /* convert offset and size, into blockid and nblocks */
681         blkshft = zf->zf_dnode->dn_datablkshift;
682         blksz = (1 << blkshft);
683
684         bzero(&zst, sizeof (zstream_t));
685         zst.zst_offset = offset >> blkshft;
686         zst.zst_len = (P2ROUNDUP(offset + size, blksz) -
687             P2ALIGN(offset, blksz)) >> blkshft;
688
689         fetched = dmu_zfetch_find(zf, &zst, prefetched);
690         if (fetched) {
691                 ZFETCHSTAT_BUMP(zfetchstat_hits);
692         } else {
693                 ZFETCHSTAT_BUMP(zfetchstat_misses);
694                 if (fetched = dmu_zfetch_colinear(zf, &zst)) {
695                         ZFETCHSTAT_BUMP(zfetchstat_colinear_hits);
696                 } else {
697                         ZFETCHSTAT_BUMP(zfetchstat_colinear_misses);
698                 }
699         }
700
701         if (!fetched) {
702                 newstream = dmu_zfetch_stream_reclaim(zf);
703
704                 /*
705                  * we still couldn't find a stream, drop the lock, and allocate
706                  * one if possible.  Otherwise, give up and go home.
707                  */
708                 if (newstream) {
709                         ZFETCHSTAT_BUMP(zfetchstat_reclaim_successes);
710                 } else {
711                         uint64_t        maxblocks;
712                         uint32_t        max_streams;
713                         uint32_t        cur_streams;
714
715                         ZFETCHSTAT_BUMP(zfetchstat_reclaim_failures);
716                         cur_streams = zf->zf_stream_cnt;
717                         maxblocks = zf->zf_dnode->dn_maxblkid;
718
719                         max_streams = MIN(zfetch_max_streams,
720                             (maxblocks / zfetch_block_cap));
721                         if (max_streams == 0) {
722                                 max_streams++;
723                         }
724
725                         if (cur_streams >= max_streams) {
726                                 return;
727                         }
728                         newstream = kmem_zalloc(sizeof (zstream_t), KM_SLEEP);
729                 }
730
731                 newstream->zst_offset = zst.zst_offset;
732                 newstream->zst_len = zst.zst_len;
733                 newstream->zst_stride = zst.zst_len;
734                 newstream->zst_ph_offset = zst.zst_len + zst.zst_offset;
735                 newstream->zst_cap = zst.zst_len;
736                 newstream->zst_direction = ZFETCH_FORWARD;
737                 newstream->zst_last = LBOLT;
738
739                 mutex_init(&newstream->zst_lock, NULL, MUTEX_DEFAULT, NULL);
740
741                 rw_enter(&zf->zf_rwlock, RW_WRITER);
742                 inserted = dmu_zfetch_stream_insert(zf, newstream);
743                 rw_exit(&zf->zf_rwlock);
744
745                 if (!inserted) {
746                         mutex_destroy(&newstream->zst_lock);
747                         kmem_free(newstream, sizeof (zstream_t));
748                 }
749         }
750 }