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