]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dmu_zfetch.c
MFV ntp 4.2.8p2 (r281348)
[FreeBSD/FreeBSD.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 /*
27  * Copyright (c) 2013 by Delphix. All rights reserved.
28  */
29
30 #include <sys/zfs_context.h>
31 #include <sys/dnode.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_zfetch.h>
34 #include <sys/dmu.h>
35 #include <sys/dbuf.h>
36 #include <sys/kstat.h>
37
38 /*
39  * I'm against tune-ables, but these should probably exist as tweakable globals
40  * until we can get this working the way we want it to.
41  */
42
43 int zfs_prefetch_disable = 0;
44
45 /* max # of streams per zfetch */
46 uint32_t        zfetch_max_streams = 8;
47 /* min time before stream reclaim */
48 uint32_t        zfetch_min_sec_reap = 2;
49 /* max number of blocks to fetch at a time */
50 uint32_t        zfetch_block_cap = 256;
51 /* number of bytes in a array_read at which we stop prefetching (1Mb) */
52 uint64_t        zfetch_array_rd_sz = 1024 * 1024;
53
54 SYSCTL_DECL(_vfs_zfs);
55 SYSCTL_INT(_vfs_zfs, OID_AUTO, prefetch_disable, CTLFLAG_RW,
56     &zfs_prefetch_disable, 0, "Disable prefetch");
57 SYSCTL_NODE(_vfs_zfs, OID_AUTO, zfetch, CTLFLAG_RW, 0, "ZFS ZFETCH");
58 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, max_streams, CTLFLAG_RWTUN,
59     &zfetch_max_streams, 0, "Max # of streams per zfetch");
60 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, min_sec_reap, CTLFLAG_RWTUN,
61     &zfetch_min_sec_reap, 0, "Min time before stream reclaim");
62 SYSCTL_UINT(_vfs_zfs_zfetch, OID_AUTO, block_cap, CTLFLAG_RWTUN,
63     &zfetch_block_cap, 0, "Max number of blocks to fetch at a time");
64 SYSCTL_UQUAD(_vfs_zfs_zfetch, OID_AUTO, array_rd_sz, CTLFLAG_RWTUN,
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 boolean_t        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 boolean_t        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  * Returns whether co-linear streams were found.
126  */
127 static boolean_t
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 = ddi_get_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, ZIO_PRIORITY_ASYNC_READ);
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 boolean_t
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
409                         if (zh->zst_offset != zs->zst_offset + zs->zst_len) {
410                                 mutex_exit(&zs->zst_lock);
411                                 goto top;
412                         }
413                         zs->zst_len += zh->zst_len;
414                         diff = zs->zst_len - zfetch_block_cap;
415                         if (diff > 0) {
416                                 zs->zst_offset += diff;
417                                 zs->zst_len = zs->zst_len > diff ?
418                                     zs->zst_len - diff : 0;
419                         }
420                         zs->zst_direction = ZFETCH_FORWARD;
421
422                         break;
423
424                 /*
425                  * Same as above, but reading backwards through the file.
426                  */
427                 } else if (zh->zst_offset == zs->zst_offset - zh->zst_len) {
428                         /* backwards sequential access */
429
430                         reset = !prefetched && zs->zst_len > 1;
431
432                         if (mutex_tryenter(&zs->zst_lock) == 0) {
433                                 rc = 1;
434                                 goto out;
435                         }
436
437                         if (zh->zst_offset != zs->zst_offset - zh->zst_len) {
438                                 mutex_exit(&zs->zst_lock);
439                                 goto top;
440                         }
441
442                         zs->zst_offset = zs->zst_offset > zh->zst_len ?
443                             zs->zst_offset - zh->zst_len : 0;
444                         zs->zst_ph_offset = zs->zst_ph_offset > zh->zst_len ?
445                             zs->zst_ph_offset - zh->zst_len : 0;
446                         zs->zst_len += zh->zst_len;
447
448                         diff = zs->zst_len - zfetch_block_cap;
449                         if (diff > 0) {
450                                 zs->zst_ph_offset = zs->zst_ph_offset > diff ?
451                                     zs->zst_ph_offset - diff : 0;
452                                 zs->zst_len = zs->zst_len > diff ?
453                                     zs->zst_len - diff : zs->zst_len;
454                         }
455                         zs->zst_direction = ZFETCH_BACKWARD;
456
457                         break;
458
459                 } else if ((zh->zst_offset - zs->zst_offset - zs->zst_stride <
460                     zs->zst_len) && (zs->zst_len != zs->zst_stride)) {
461                         /* strided forward access */
462
463                         if (mutex_tryenter(&zs->zst_lock) == 0) {
464                                 rc = 1;
465                                 goto out;
466                         }
467
468                         if ((zh->zst_offset - zs->zst_offset - zs->zst_stride >=
469                             zs->zst_len) || (zs->zst_len == zs->zst_stride)) {
470                                 mutex_exit(&zs->zst_lock);
471                                 goto top;
472                         }
473
474                         zs->zst_offset += zs->zst_stride;
475                         zs->zst_direction = ZFETCH_FORWARD;
476
477                         break;
478
479                 } else if ((zh->zst_offset - zs->zst_offset + zs->zst_stride <
480                     zs->zst_len) && (zs->zst_len != zs->zst_stride)) {
481                         /* strided reverse access */
482
483                         if (mutex_tryenter(&zs->zst_lock) == 0) {
484                                 rc = 1;
485                                 goto out;
486                         }
487
488                         if ((zh->zst_offset - zs->zst_offset + zs->zst_stride >=
489                             zs->zst_len) || (zs->zst_len == zs->zst_stride)) {
490                                 mutex_exit(&zs->zst_lock);
491                                 goto top;
492                         }
493
494                         zs->zst_offset = zs->zst_offset > zs->zst_stride ?
495                             zs->zst_offset - zs->zst_stride : 0;
496                         zs->zst_ph_offset = (zs->zst_ph_offset >
497                             (2 * zs->zst_stride)) ?
498                             (zs->zst_ph_offset - (2 * zs->zst_stride)) : 0;
499                         zs->zst_direction = ZFETCH_BACKWARD;
500
501                         break;
502                 }
503         }
504
505         if (zs) {
506                 if (reset) {
507                         zstream_t *remove = zs;
508
509                         ZFETCHSTAT_BUMP(zfetchstat_stream_resets);
510                         rc = 0;
511                         mutex_exit(&zs->zst_lock);
512                         rw_exit(&zf->zf_rwlock);
513                         rw_enter(&zf->zf_rwlock, RW_WRITER);
514                         /*
515                          * Relocate the stream, in case someone removes
516                          * it while we were acquiring the WRITER lock.
517                          */
518                         for (zs = list_head(&zf->zf_stream); zs;
519                             zs = list_next(&zf->zf_stream, zs)) {
520                                 if (zs == remove) {
521                                         dmu_zfetch_stream_remove(zf, zs);
522                                         mutex_destroy(&zs->zst_lock);
523                                         kmem_free(zs, sizeof (zstream_t));
524                                         break;
525                                 }
526                         }
527                 } else {
528                         ZFETCHSTAT_BUMP(zfetchstat_stream_noresets);
529                         rc = 1;
530                         dmu_zfetch_dofetch(zf, zs);
531                         mutex_exit(&zs->zst_lock);
532                 }
533         }
534 out:
535         rw_exit(&zf->zf_rwlock);
536         return (rc);
537 }
538
539 /*
540  * Clean-up state associated with a zfetch structure.  This frees allocated
541  * structure members, empties the zf_stream tree, and generally makes things
542  * nice.  This doesn't free the zfetch_t itself, that's left to the caller.
543  */
544 void
545 dmu_zfetch_rele(zfetch_t *zf)
546 {
547         zstream_t       *zs;
548         zstream_t       *zs_next;
549
550         ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock));
551
552         for (zs = list_head(&zf->zf_stream); zs; zs = zs_next) {
553                 zs_next = list_next(&zf->zf_stream, zs);
554
555                 list_remove(&zf->zf_stream, zs);
556                 mutex_destroy(&zs->zst_lock);
557                 kmem_free(zs, sizeof (zstream_t));
558         }
559         list_destroy(&zf->zf_stream);
560         rw_destroy(&zf->zf_rwlock);
561
562         zf->zf_dnode = NULL;
563 }
564
565 /*
566  * Given a zfetch and zstream structure, insert the zstream structure into the
567  * AVL tree contained within the zfetch structure.  Peform the appropriate
568  * book-keeping.  It is possible that another thread has inserted a stream which
569  * matches one that we are about to insert, so we must be sure to check for this
570  * case.  If one is found, return failure, and let the caller cleanup the
571  * duplicates.
572  */
573 static int
574 dmu_zfetch_stream_insert(zfetch_t *zf, zstream_t *zs)
575 {
576         zstream_t       *zs_walk;
577         zstream_t       *zs_next;
578
579         ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
580
581         for (zs_walk = list_head(&zf->zf_stream); zs_walk; zs_walk = zs_next) {
582                 zs_next = list_next(&zf->zf_stream, zs_walk);
583
584                 if (dmu_zfetch_streams_equal(zs_walk, zs)) {
585                         return (0);
586                 }
587         }
588
589         list_insert_head(&zf->zf_stream, zs);
590         zf->zf_stream_cnt++;
591         return (1);
592 }
593
594
595 /*
596  * Walk the list of zstreams in the given zfetch, find an old one (by time), and
597  * reclaim it for use by the caller.
598  */
599 static zstream_t *
600 dmu_zfetch_stream_reclaim(zfetch_t *zf)
601 {
602         zstream_t       *zs;
603         clock_t         ticks;
604
605         ticks = zfetch_min_sec_reap * hz;
606         if (! rw_tryenter(&zf->zf_rwlock, RW_WRITER))
607                 return (0);
608
609         for (zs = list_head(&zf->zf_stream); zs;
610             zs = list_next(&zf->zf_stream, zs)) {
611
612                 if (ddi_get_lbolt() - zs->zst_last > ticks)
613                         break;
614         }
615
616         if (zs) {
617                 dmu_zfetch_stream_remove(zf, zs);
618                 mutex_destroy(&zs->zst_lock);
619                 bzero(zs, sizeof (zstream_t));
620         } else {
621                 zf->zf_alloc_fail++;
622         }
623         rw_exit(&zf->zf_rwlock);
624
625         return (zs);
626 }
627
628 /*
629  * Given a zfetch and zstream structure, remove the zstream structure from its
630  * container in the zfetch structure.  Perform the appropriate book-keeping.
631  */
632 static void
633 dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
634 {
635         ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
636
637         list_remove(&zf->zf_stream, zs);
638         zf->zf_stream_cnt--;
639 }
640
641 static int
642 dmu_zfetch_streams_equal(zstream_t *zs1, zstream_t *zs2)
643 {
644         if (zs1->zst_offset != zs2->zst_offset)
645                 return (0);
646
647         if (zs1->zst_len != zs2->zst_len)
648                 return (0);
649
650         if (zs1->zst_stride != zs2->zst_stride)
651                 return (0);
652
653         if (zs1->zst_ph_offset != zs2->zst_ph_offset)
654                 return (0);
655
656         if (zs1->zst_cap != zs2->zst_cap)
657                 return (0);
658
659         if (zs1->zst_direction != zs2->zst_direction)
660                 return (0);
661
662         return (1);
663 }
664
665 /*
666  * This is the prefetch entry point.  It calls all of the other dmu_zfetch
667  * routines to create, delete, find, or operate upon prefetch streams.
668  */
669 void
670 dmu_zfetch(zfetch_t *zf, uint64_t offset, uint64_t size, int prefetched)
671 {
672         zstream_t       zst;
673         zstream_t       *newstream;
674         boolean_t       fetched;
675         int             inserted;
676         unsigned int    blkshft;
677         uint64_t        blksz;
678
679         if (zfs_prefetch_disable)
680                 return;
681
682         /* files that aren't ln2 blocksz are only one block -- nothing to do */
683         if (!zf->zf_dnode->dn_datablkshift)
684                 return;
685
686         /* convert offset and size, into blockid and nblocks */
687         blkshft = zf->zf_dnode->dn_datablkshift;
688         blksz = (1 << blkshft);
689
690         bzero(&zst, sizeof (zstream_t));
691         zst.zst_offset = offset >> blkshft;
692         zst.zst_len = (P2ROUNDUP(offset + size, blksz) -
693             P2ALIGN(offset, blksz)) >> blkshft;
694
695         fetched = dmu_zfetch_find(zf, &zst, prefetched);
696         if (fetched) {
697                 ZFETCHSTAT_BUMP(zfetchstat_hits);
698         } else {
699                 ZFETCHSTAT_BUMP(zfetchstat_misses);
700                 fetched = dmu_zfetch_colinear(zf, &zst);
701                 if (fetched) {
702                         ZFETCHSTAT_BUMP(zfetchstat_colinear_hits);
703                 } else {
704                         ZFETCHSTAT_BUMP(zfetchstat_colinear_misses);
705                 }
706         }
707
708         if (!fetched) {
709                 newstream = dmu_zfetch_stream_reclaim(zf);
710
711                 /*
712                  * we still couldn't find a stream, drop the lock, and allocate
713                  * one if possible.  Otherwise, give up and go home.
714                  */
715                 if (newstream) {
716                         ZFETCHSTAT_BUMP(zfetchstat_reclaim_successes);
717                 } else {
718                         uint64_t        maxblocks;
719                         uint32_t        max_streams;
720                         uint32_t        cur_streams;
721
722                         ZFETCHSTAT_BUMP(zfetchstat_reclaim_failures);
723                         cur_streams = zf->zf_stream_cnt;
724                         maxblocks = zf->zf_dnode->dn_maxblkid;
725
726                         max_streams = MIN(zfetch_max_streams,
727                             (maxblocks / zfetch_block_cap));
728                         if (max_streams == 0) {
729                                 max_streams++;
730                         }
731
732                         if (cur_streams >= max_streams) {
733                                 return;
734                         }
735                         newstream = kmem_zalloc(sizeof (zstream_t), KM_SLEEP);
736                 }
737
738                 newstream->zst_offset = zst.zst_offset;
739                 newstream->zst_len = zst.zst_len;
740                 newstream->zst_stride = zst.zst_len;
741                 newstream->zst_ph_offset = zst.zst_len + zst.zst_offset;
742                 newstream->zst_cap = zst.zst_len;
743                 newstream->zst_direction = ZFETCH_FORWARD;
744                 newstream->zst_last = ddi_get_lbolt();
745
746                 mutex_init(&newstream->zst_lock, NULL, MUTEX_DEFAULT, NULL);
747
748                 rw_enter(&zf->zf_rwlock, RW_WRITER);
749                 inserted = dmu_zfetch_stream_insert(zf, newstream);
750                 rw_exit(&zf->zf_rwlock);
751
752                 if (!inserted) {
753                         mutex_destroy(&newstream->zst_lock);
754                         kmem_free(newstream, sizeof (zstream_t));
755                 }
756         }
757 }