xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_zfetch.c (revision 8be869c0c2fb6677f65f293a53e36156ea86c9a7)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
513506d1eSmaybee  * Common Development and Distribution License (the "License").
613506d1eSmaybee  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
227cbf8b43SRich Morris  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
2669962b56SMatthew Ahrens /*
279704bf7fSPaul Dagnelie  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
2869962b56SMatthew Ahrens  */
2969962b56SMatthew Ahrens 
30fa9e4066Sahrens #include <sys/zfs_context.h>
31fa9e4066Sahrens #include <sys/dnode.h>
32fa9e4066Sahrens #include <sys/dmu_objset.h>
33fa9e4066Sahrens #include <sys/dmu_zfetch.h>
34fa9e4066Sahrens #include <sys/dmu.h>
35fa9e4066Sahrens #include <sys/dbuf.h>
367cbf8b43SRich Morris #include <sys/kstat.h>
37fa9e4066Sahrens 
38fa9e4066Sahrens /*
39cf6106c8SMatthew Ahrens  * This tunable disables predictive prefetch.  Note that it leaves "prescient"
40cf6106c8SMatthew Ahrens  * prefetch (e.g. prefetch for zfs send) intact.  Unlike predictive prefetch,
41cf6106c8SMatthew Ahrens  * prescient prefetch never issues i/os that end up not being needed,
42cf6106c8SMatthew Ahrens  * so it can't hurt performance.
43fa9e4066Sahrens  */
44cf6106c8SMatthew Ahrens boolean_t zfs_prefetch_disable = B_FALSE;
45a2eea2e1Sahrens 
46fa9e4066Sahrens /* max # of streams per zfetch */
47fa9e4066Sahrens uint32_t	zfetch_max_streams = 8;
48fa9e4066Sahrens /* min time before stream reclaim */
49fa9e4066Sahrens uint32_t	zfetch_min_sec_reap = 2;
50cf6106c8SMatthew Ahrens /* max bytes to prefetch per stream (default 8MB) */
51cf6106c8SMatthew Ahrens uint32_t	zfetch_max_distance = 8 * 1024 * 1024;
52cb92f413SAlexander Motin /* max bytes to prefetch indirects for per stream (default 64MB) */
53cb92f413SAlexander Motin uint32_t	zfetch_max_idistance = 64 * 1024 * 1024;
5463280274SGeorge Wilson /* max number of bytes in an array_read in which we allow prefetching (1MB) */
55fa9e4066Sahrens uint64_t	zfetch_array_rd_sz = 1024 * 1024;
56fa9e4066Sahrens 
577cbf8b43SRich Morris typedef struct zfetch_stats {
587cbf8b43SRich Morris 	kstat_named_t zfetchstat_hits;
597cbf8b43SRich Morris 	kstat_named_t zfetchstat_misses;
60cf6106c8SMatthew Ahrens 	kstat_named_t zfetchstat_max_streams;
619e3493cbSJason King 	kstat_named_t zfetchstat_max_completion_us;
629e3493cbSJason King 	kstat_named_t zfetchstat_last_completion_us;
639e3493cbSJason King 	kstat_named_t zfetchstat_io_issued;
647cbf8b43SRich Morris } zfetch_stats_t;
657cbf8b43SRich Morris 
667cbf8b43SRich Morris static zfetch_stats_t zfetch_stats = {
677cbf8b43SRich Morris 	{ "hits",			KSTAT_DATA_UINT64 },
687cbf8b43SRich Morris 	{ "misses",			KSTAT_DATA_UINT64 },
69cf6106c8SMatthew Ahrens 	{ "max_streams",		KSTAT_DATA_UINT64 },
709e3493cbSJason King 	{ "max_completion_us",		KSTAT_DATA_UINT64 },
719e3493cbSJason King 	{ "last_completion_us",		KSTAT_DATA_UINT64 },
729e3493cbSJason King 	{ "io_issued",		KSTAT_DATA_UINT64 },
737cbf8b43SRich Morris };
747cbf8b43SRich Morris 
75cf6106c8SMatthew Ahrens #define	ZFETCHSTAT_BUMP(stat) \
769e3493cbSJason King 	atomic_inc_64(&zfetch_stats.stat.value.ui64)
779e3493cbSJason King #define	ZFETCHSTAT_ADD(stat, val)				\
789e3493cbSJason King 	atomic_add_64(&zfetch_stats.stat.value.ui64, val)
799e3493cbSJason King #define	ZFETCHSTAT_SET(stat, val)				\
809e3493cbSJason King 	zfetch_stats.stat.value.ui64 = val
819e3493cbSJason King #define	ZFETCHSTAT_GET(stat)					\
829e3493cbSJason King 	zfetch_stats.stat.value.ui64
839e3493cbSJason King 
847cbf8b43SRich Morris 
857cbf8b43SRich Morris kstat_t		*zfetch_ksp;
867cbf8b43SRich Morris 
877cbf8b43SRich Morris void
zfetch_init(void)887cbf8b43SRich Morris zfetch_init(void)
897cbf8b43SRich Morris {
907cbf8b43SRich Morris 	zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",
917cbf8b43SRich Morris 	    KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),
927cbf8b43SRich Morris 	    KSTAT_FLAG_VIRTUAL);
937cbf8b43SRich Morris 
947cbf8b43SRich Morris 	if (zfetch_ksp != NULL) {
957cbf8b43SRich Morris 		zfetch_ksp->ks_data = &zfetch_stats;
967cbf8b43SRich Morris 		kstat_install(zfetch_ksp);
977cbf8b43SRich Morris 	}
987cbf8b43SRich Morris }
997cbf8b43SRich Morris 
1007cbf8b43SRich Morris void
zfetch_fini(void)1017cbf8b43SRich Morris zfetch_fini(void)
1027cbf8b43SRich Morris {
1037cbf8b43SRich Morris 	if (zfetch_ksp != NULL) {
1047cbf8b43SRich Morris 		kstat_delete(zfetch_ksp);
1057cbf8b43SRich Morris 		zfetch_ksp = NULL;
1067cbf8b43SRich Morris 	}
1077cbf8b43SRich Morris }
1087cbf8b43SRich Morris 
109fa9e4066Sahrens /*
110fa9e4066Sahrens  * This takes a pointer to a zfetch structure and a dnode.  It performs the
111fa9e4066Sahrens  * necessary setup for the zfetch structure, grokking data from the
112fa9e4066Sahrens  * associated dnode.
113fa9e4066Sahrens  */
114fa9e4066Sahrens void
dmu_zfetch_init(zfetch_t * zf,dnode_t * dno)115fa9e4066Sahrens dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)
116fa9e4066Sahrens {
117cf6106c8SMatthew Ahrens 	if (zf == NULL)
118fa9e4066Sahrens 		return;
119fa9e4066Sahrens 	zf->zf_dnode = dno;
1209e3493cbSJason King 	zf->zf_numstreams = 0;
121fa9e4066Sahrens 
122fa9e4066Sahrens 	list_create(&zf->zf_stream, sizeof (zstream_t),
123cf6106c8SMatthew Ahrens 	    offsetof(zstream_t, zs_node));
124fa9e4066Sahrens 
125fa9e4066Sahrens 	rw_init(&zf->zf_rwlock, NULL, RW_DEFAULT, NULL);
126fa9e4066Sahrens }
127fa9e4066Sahrens 
128cf6106c8SMatthew Ahrens static void
dmu_zfetch_stream_fini(zstream_t * zs)1299e3493cbSJason King dmu_zfetch_stream_fini(zstream_t *zs)
1309e3493cbSJason King {
1319e3493cbSJason King 	mutex_destroy(&zs->zs_lock);
132*8be869c0SAndy Fiddaman 	zfs_refcount_destroy(&zs->zs_blocks);
1339e3493cbSJason King 	kmem_free(zs, sizeof (*zs));
1349e3493cbSJason King }
1359e3493cbSJason King 
1369e3493cbSJason King static void
dmu_zfetch_stream_remove(zfetch_t * zf,zstream_t * zs)137cf6106c8SMatthew Ahrens dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)
138fa9e4066Sahrens {
139cf6106c8SMatthew Ahrens 	ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
140cf6106c8SMatthew Ahrens 	list_remove(&zf->zf_stream, zs);
1419e3493cbSJason King 	dmu_zfetch_stream_fini(zs);
1429e3493cbSJason King 	zf->zf_numstreams--;
1439e3493cbSJason King }
1449e3493cbSJason King 
1459e3493cbSJason King static void
dmu_zfetch_stream_orphan(zfetch_t * zf,zstream_t * zs)1469e3493cbSJason King dmu_zfetch_stream_orphan(zfetch_t *zf, zstream_t *zs)
1479e3493cbSJason King {
1489e3493cbSJason King 	ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
1499e3493cbSJason King 	list_remove(&zf->zf_stream, zs);
1509e3493cbSJason King 	zs->zs_fetch = NULL;
1519e3493cbSJason King 	zf->zf_numstreams--;
152fa9e4066Sahrens }
153fa9e4066Sahrens 
154fa9e4066Sahrens /*
155cf6106c8SMatthew Ahrens  * Clean-up state associated with a zfetch structure (e.g. destroy the
156cf6106c8SMatthew Ahrens  * streams).  This doesn't free the zfetch_t itself, that's left to the caller.
157fa9e4066Sahrens  */
158fa9e4066Sahrens void
dmu_zfetch_fini(zfetch_t * zf)159cf6106c8SMatthew Ahrens dmu_zfetch_fini(zfetch_t *zf)
160fa9e4066Sahrens {
161fa9e4066Sahrens 	zstream_t *zs;
162fa9e4066Sahrens 
163fa9e4066Sahrens 	ASSERT(!RW_LOCK_HELD(&zf->zf_rwlock));
164fa9e4066Sahrens 
165cf6106c8SMatthew Ahrens 	rw_enter(&zf->zf_rwlock, RW_WRITER);
1662966a4e8SMatt Macy 	while ((zs = list_head(&zf->zf_stream)) != NULL) {
1672966a4e8SMatt Macy 		if (zfs_refcount_count(&zs->zs_blocks) != 0)
1689e3493cbSJason King 			dmu_zfetch_stream_orphan(zf, zs);
1692966a4e8SMatt Macy 		else
1702966a4e8SMatt Macy 			dmu_zfetch_stream_remove(zf, zs);
1712966a4e8SMatt Macy 	}
172cf6106c8SMatthew Ahrens 	rw_exit(&zf->zf_rwlock);
173fa9e4066Sahrens 	list_destroy(&zf->zf_stream);
174fa9e4066Sahrens 	rw_destroy(&zf->zf_rwlock);
175fa9e4066Sahrens 
176fa9e4066Sahrens 	zf->zf_dnode = NULL;
177fa9e4066Sahrens }
178fa9e4066Sahrens 
179fa9e4066Sahrens /*
180cf6106c8SMatthew Ahrens  * If there aren't too many streams already, create a new stream.
181cf6106c8SMatthew Ahrens  * The "blkid" argument is the next block that we expect this stream to access.
182cf6106c8SMatthew Ahrens  * While we're here, clean up old streams (which haven't been
183cf6106c8SMatthew Ahrens  * accessed for at least zfetch_min_sec_reap seconds).
184fa9e4066Sahrens  */
185fa9e4066Sahrens static void
dmu_zfetch_stream_create(zfetch_t * zf,uint64_t blkid)186cf6106c8SMatthew Ahrens dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid)
187fa9e4066Sahrens {
188cf6106c8SMatthew Ahrens 	zstream_t *zs_next;
1899e3493cbSJason King 	hrtime_t now = gethrtime();
190cf6106c8SMatthew Ahrens 
191fa9e4066Sahrens 	ASSERT(RW_WRITE_HELD(&zf->zf_rwlock));
192fa9e4066Sahrens 
193cf6106c8SMatthew Ahrens 	/*
194cf6106c8SMatthew Ahrens 	 * Clean up old streams.
195cf6106c8SMatthew Ahrens 	 */
196cf6106c8SMatthew Ahrens 	for (zstream_t *zs = list_head(&zf->zf_stream);
197cf6106c8SMatthew Ahrens 	    zs != NULL; zs = zs_next) {
198cf6106c8SMatthew Ahrens 		zs_next = list_next(&zf->zf_stream, zs);
1999e3493cbSJason King 		/*
2009e3493cbSJason King 		 * Skip gethrtime() call if there are still references
2019e3493cbSJason King 		 */
2029e3493cbSJason King 		if (zfs_refcount_count(&zs->zs_blocks) != 0)
2039e3493cbSJason King 			continue;
2049e3493cbSJason King 		if (((now - zs->zs_atime) / NANOSEC) >
205cf6106c8SMatthew Ahrens 		    zfetch_min_sec_reap)
206cf6106c8SMatthew Ahrens 			dmu_zfetch_stream_remove(zf, zs);
207fa9e4066Sahrens 	}
208fa9e4066Sahrens 
209cf6106c8SMatthew Ahrens 	/*
210cf6106c8SMatthew Ahrens 	 * The maximum number of streams is normally zfetch_max_streams,
211cf6106c8SMatthew Ahrens 	 * but for small files we lower it such that it's at least possible
212cf6106c8SMatthew Ahrens 	 * for all the streams to be non-overlapping.
213cf6106c8SMatthew Ahrens 	 *
214cf6106c8SMatthew Ahrens 	 * If we are already at the maximum number of streams for this file,
215cf6106c8SMatthew Ahrens 	 * even after removing old streams, then don't create this stream.
216cf6106c8SMatthew Ahrens 	 */
217cf6106c8SMatthew Ahrens 	uint32_t max_streams = MAX(1, MIN(zfetch_max_streams,
218cf6106c8SMatthew Ahrens 	    zf->zf_dnode->dn_maxblkid * zf->zf_dnode->dn_datablksz /
219cf6106c8SMatthew Ahrens 	    zfetch_max_distance));
2209e3493cbSJason King 	if (zf->zf_numstreams >= max_streams) {
221cf6106c8SMatthew Ahrens 		ZFETCHSTAT_BUMP(zfetchstat_max_streams);
222cf6106c8SMatthew Ahrens 		return;
223cf6106c8SMatthew Ahrens 	}
224fa9e4066Sahrens 
225cf6106c8SMatthew Ahrens 	zstream_t *zs = kmem_zalloc(sizeof (*zs), KM_SLEEP);
226cf6106c8SMatthew Ahrens 	zs->zs_blkid = blkid;
227cf6106c8SMatthew Ahrens 	zs->zs_pf_blkid = blkid;
228cb92f413SAlexander Motin 	zs->zs_ipf_blkid = blkid;
2299e3493cbSJason King 	zs->zs_atime = now;
2309e3493cbSJason King 	zs->zs_fetch = zf;
2319e3493cbSJason King 	zfs_refcount_create(&zs->zs_blocks);
232cf6106c8SMatthew Ahrens 	mutex_init(&zs->zs_lock, NULL, MUTEX_DEFAULT, NULL);
2339e3493cbSJason King 	zf->zf_numstreams++;
234cf6106c8SMatthew Ahrens 	list_insert_head(&zf->zf_stream, zs);
235fa9e4066Sahrens }
236fa9e4066Sahrens 
2379e3493cbSJason King static void
dmu_zfetch_stream_done(void * arg,boolean_t io_issued)2389e3493cbSJason King dmu_zfetch_stream_done(void *arg, boolean_t io_issued)
2399e3493cbSJason King {
2409e3493cbSJason King 	zstream_t *zs = arg;
2419e3493cbSJason King 
2429e3493cbSJason King 	if (zs->zs_start_time && io_issued) {
2439e3493cbSJason King 		hrtime_t now = gethrtime();
2449e3493cbSJason King 		hrtime_t delta = NSEC2USEC(now - zs->zs_start_time);
2459e3493cbSJason King 
2469e3493cbSJason King 		zs->zs_start_time = 0;
2479e3493cbSJason King 		ZFETCHSTAT_SET(zfetchstat_last_completion_us, delta);
2489e3493cbSJason King 		if (delta > ZFETCHSTAT_GET(zfetchstat_max_completion_us))
2499e3493cbSJason King 			ZFETCHSTAT_SET(zfetchstat_max_completion_us, delta);
2509e3493cbSJason King 	}
2519e3493cbSJason King 
2529e3493cbSJason King 	if (zfs_refcount_remove(&zs->zs_blocks, NULL) != 0)
2539e3493cbSJason King 		return;
2549e3493cbSJason King 
2559e3493cbSJason King 	/*
2569e3493cbSJason King 	 * The parent fetch structure has gone away
2579e3493cbSJason King 	 */
2589e3493cbSJason King 	if (zs->zs_fetch == NULL)
2599e3493cbSJason King 		dmu_zfetch_stream_fini(zs);
2609e3493cbSJason King }
2619e3493cbSJason King 
262fa9e4066Sahrens /*
263cb92f413SAlexander Motin  * This is the predictive prefetch entry point.  It associates dnode access
264cb92f413SAlexander Motin  * specified with blkid and nblks arguments with prefetch stream, predicts
265cb92f413SAlexander Motin  * further accesses based on that stats and initiates speculative prefetch.
266cb92f413SAlexander Motin  * fetch_data argument specifies whether actual data blocks should be fetched:
267cb92f413SAlexander Motin  *   FALSE -- prefetch only indirect blocks for predicted data blocks;
268cb92f413SAlexander Motin  *   TRUE -- prefetch predicted data blocks plus following indirect blocks.
269fa9e4066Sahrens  */
270fa9e4066Sahrens void
dmu_zfetch(zfetch_t * zf,uint64_t blkid,uint64_t nblks,boolean_t fetch_data,boolean_t have_lock)2719704bf7fSPaul Dagnelie dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks, boolean_t fetch_data,
2729704bf7fSPaul Dagnelie     boolean_t have_lock)
273fa9e4066Sahrens {
274cf6106c8SMatthew Ahrens 	zstream_t *zs;
275cb92f413SAlexander Motin 	int64_t pf_start, ipf_start, ipf_istart, ipf_iend;
276cb92f413SAlexander Motin 	int64_t pf_ahead_blks, max_blks;
2779e3493cbSJason King 	int epbs, max_dist_blks, pf_nblks, ipf_nblks, issued;
278cb92f413SAlexander Motin 	uint64_t end_of_access_blkid = blkid + nblks;
2795cabbc6bSPrashanth Sreenivasa 	spa_t *spa = zf->zf_dnode->dn_objset->os_spa;
280fa9e4066Sahrens 
281a2eea2e1Sahrens 	if (zfs_prefetch_disable)
282fa9e4066Sahrens 		return;
283a2eea2e1Sahrens 
284cf6106c8SMatthew Ahrens 	/*
2855cabbc6bSPrashanth Sreenivasa 	 * If we haven't yet loaded the indirect vdevs' mappings, we
2865cabbc6bSPrashanth Sreenivasa 	 * can only read from blocks that we carefully ensure are on
2875cabbc6bSPrashanth Sreenivasa 	 * concrete vdevs (or previously-loaded indirect vdevs).  So we
2885cabbc6bSPrashanth Sreenivasa 	 * can't allow the predictive prefetcher to attempt reads of other
2895cabbc6bSPrashanth Sreenivasa 	 * blocks (e.g. of the MOS's dnode obejct).
2905cabbc6bSPrashanth Sreenivasa 	 */
2915cabbc6bSPrashanth Sreenivasa 	if (!spa_indirect_vdevs_loaded(spa))
2925cabbc6bSPrashanth Sreenivasa 		return;
2935cabbc6bSPrashanth Sreenivasa 
2945cabbc6bSPrashanth Sreenivasa 	/*
295cf6106c8SMatthew Ahrens 	 * As a fast path for small (single-block) files, ignore access
296cf6106c8SMatthew Ahrens 	 * to the first block.
297cf6106c8SMatthew Ahrens 	 */
2989e3493cbSJason King 	if (!have_lock && blkid == 0)
299a2eea2e1Sahrens 		return;
300fa9e4066Sahrens 
3019704bf7fSPaul Dagnelie 	if (!have_lock)
3029704bf7fSPaul Dagnelie 		rw_enter(&zf->zf_dnode->dn_struct_rwlock, RW_READER);
3039704bf7fSPaul Dagnelie 
3049e3493cbSJason King 
3059e3493cbSJason King 	/*
3069e3493cbSJason King 	 * A fast path for small files for which no prefetch will
3079e3493cbSJason King 	 * happen.
3089e3493cbSJason King 	 */
3099e3493cbSJason King 	if (zf->zf_dnode->dn_maxblkid < 2) {
3109e3493cbSJason King 		if (!have_lock)
3119e3493cbSJason King 			rw_exit(&zf->zf_dnode->dn_struct_rwlock);
3129e3493cbSJason King 		return;
3139e3493cbSJason King 	}
314cf6106c8SMatthew Ahrens 	rw_enter(&zf->zf_rwlock, RW_READER);
315fa9e4066Sahrens 
3165cb8d943SAlexander Motin 	/*
3175cb8d943SAlexander Motin 	 * Find matching prefetch stream.  Depending on whether the accesses
3185cb8d943SAlexander Motin 	 * are block-aligned, first block of the new access may either follow
3195cb8d943SAlexander Motin 	 * the last block of the previous access, or be equal to it.
3205cb8d943SAlexander Motin 	 */
321cf6106c8SMatthew Ahrens 	for (zs = list_head(&zf->zf_stream); zs != NULL;
322cf6106c8SMatthew Ahrens 	    zs = list_next(&zf->zf_stream, zs)) {
3235cb8d943SAlexander Motin 		if (blkid == zs->zs_blkid || blkid + 1 == zs->zs_blkid) {
324cf6106c8SMatthew Ahrens 			mutex_enter(&zs->zs_lock);
325cf6106c8SMatthew Ahrens 			/*
326cf6106c8SMatthew Ahrens 			 * zs_blkid could have changed before we
327cf6106c8SMatthew Ahrens 			 * acquired zs_lock; re-check them here.
328cf6106c8SMatthew Ahrens 			 */
3295cb8d943SAlexander Motin 			if (blkid == zs->zs_blkid) {
3305cb8d943SAlexander Motin 				break;
3315cb8d943SAlexander Motin 			} else if (blkid + 1 == zs->zs_blkid) {
3325cb8d943SAlexander Motin 				blkid++;
3335cb8d943SAlexander Motin 				nblks--;
3345cb8d943SAlexander Motin 				if (nblks == 0) {
3355cb8d943SAlexander Motin 					/* Already prefetched this before. */
336cf6106c8SMatthew Ahrens 					mutex_exit(&zs->zs_lock);
3375cb8d943SAlexander Motin 					rw_exit(&zf->zf_rwlock);
3389704bf7fSPaul Dagnelie 					if (!have_lock) {
3399704bf7fSPaul Dagnelie 						rw_exit(&zf->zf_dnode->
3409704bf7fSPaul Dagnelie 						    dn_struct_rwlock);
3419704bf7fSPaul Dagnelie 					}
3425cb8d943SAlexander Motin 					return;
343cf6106c8SMatthew Ahrens 				}
344cf6106c8SMatthew Ahrens 				break;
345cf6106c8SMatthew Ahrens 			}
3465cb8d943SAlexander Motin 			mutex_exit(&zs->zs_lock);
3475cb8d943SAlexander Motin 		}
348cf6106c8SMatthew Ahrens 	}
349fa9e4066Sahrens 
350cf6106c8SMatthew Ahrens 	if (zs == NULL) {
351cf6106c8SMatthew Ahrens 		/*
352cf6106c8SMatthew Ahrens 		 * This access is not part of any existing stream.  Create
353cf6106c8SMatthew Ahrens 		 * a new stream for it.
354cf6106c8SMatthew Ahrens 		 */
3557cbf8b43SRich Morris 		ZFETCHSTAT_BUMP(zfetchstat_misses);
356cf6106c8SMatthew Ahrens 		if (rw_tryupgrade(&zf->zf_rwlock))
357cb92f413SAlexander Motin 			dmu_zfetch_stream_create(zf, end_of_access_blkid);
358cf6106c8SMatthew Ahrens 		rw_exit(&zf->zf_rwlock);
3599704bf7fSPaul Dagnelie 		if (!have_lock)
3609704bf7fSPaul Dagnelie 			rw_exit(&zf->zf_dnode->dn_struct_rwlock);
361cf6106c8SMatthew Ahrens 		return;
3627cbf8b43SRich Morris 	}
363fa9e4066Sahrens 
364fa9e4066Sahrens 	/*
365cf6106c8SMatthew Ahrens 	 * This access was to a block that we issued a prefetch for on
366cf6106c8SMatthew Ahrens 	 * behalf of this stream. Issue further prefetches for this stream.
367cf6106c8SMatthew Ahrens 	 *
368cf6106c8SMatthew Ahrens 	 * Normally, we start prefetching where we stopped
369cf6106c8SMatthew Ahrens 	 * prefetching last (zs_pf_blkid).  But when we get our first
370cf6106c8SMatthew Ahrens 	 * hit on this stream, zs_pf_blkid == zs_blkid, we don't
371cb92f413SAlexander Motin 	 * want to prefetch the block we just accessed.  In this case,
372cf6106c8SMatthew Ahrens 	 * start just after the block we just accessed.
373fa9e4066Sahrens 	 */
374cb92f413SAlexander Motin 	pf_start = MAX(zs->zs_pf_blkid, end_of_access_blkid);
375fa9e4066Sahrens 
376cf6106c8SMatthew Ahrens 	/*
377cf6106c8SMatthew Ahrens 	 * Double our amount of prefetched data, but don't let the
378cf6106c8SMatthew Ahrens 	 * prefetch get further ahead than zfetch_max_distance.
379cf6106c8SMatthew Ahrens 	 */
380cb92f413SAlexander Motin 	if (fetch_data) {
381cb92f413SAlexander Motin 		max_dist_blks =
382cb92f413SAlexander Motin 		    zfetch_max_distance >> zf->zf_dnode->dn_datablkshift;
383cb92f413SAlexander Motin 		/*
384cb92f413SAlexander Motin 		 * Previously, we were (zs_pf_blkid - blkid) ahead.  We
385cb92f413SAlexander Motin 		 * want to now be double that, so read that amount again,
386cb92f413SAlexander Motin 		 * plus the amount we are catching up by (i.e. the amount
387cb92f413SAlexander Motin 		 * read just now).
388cb92f413SAlexander Motin 		 */
389cb92f413SAlexander Motin 		pf_ahead_blks = zs->zs_pf_blkid - blkid + nblks;
390cb92f413SAlexander Motin 		max_blks = max_dist_blks - (pf_start - end_of_access_blkid);
391cb92f413SAlexander Motin 		pf_nblks = MIN(pf_ahead_blks, max_blks);
392cb92f413SAlexander Motin 	} else {
393cb92f413SAlexander Motin 		pf_nblks = 0;
394cb92f413SAlexander Motin 	}
395fa9e4066Sahrens 
396cf6106c8SMatthew Ahrens 	zs->zs_pf_blkid = pf_start + pf_nblks;
397fa9e4066Sahrens 
398cf6106c8SMatthew Ahrens 	/*
399cb92f413SAlexander Motin 	 * Do the same for indirects, starting from where we stopped last,
400cb92f413SAlexander Motin 	 * or where we will stop reading data blocks (and the indirects
401cb92f413SAlexander Motin 	 * that point to them).
402cf6106c8SMatthew Ahrens 	 */
403cb92f413SAlexander Motin 	ipf_start = MAX(zs->zs_ipf_blkid, zs->zs_pf_blkid);
404cb92f413SAlexander Motin 	max_dist_blks = zfetch_max_idistance >> zf->zf_dnode->dn_datablkshift;
405cb92f413SAlexander Motin 	/*
406cb92f413SAlexander Motin 	 * We want to double our distance ahead of the data prefetch
407cb92f413SAlexander Motin 	 * (or reader, if we are not prefetching data).  Previously, we
408cb92f413SAlexander Motin 	 * were (zs_ipf_blkid - blkid) ahead.  To double that, we read
409cb92f413SAlexander Motin 	 * that amount again, plus the amount we are catching up by
410cb92f413SAlexander Motin 	 * (i.e. the amount read now + the amount of data prefetched now).
411cb92f413SAlexander Motin 	 */
412cb92f413SAlexander Motin 	pf_ahead_blks = zs->zs_ipf_blkid - blkid + nblks + pf_nblks;
413cb92f413SAlexander Motin 	max_blks = max_dist_blks - (ipf_start - end_of_access_blkid);
414cb92f413SAlexander Motin 	ipf_nblks = MIN(pf_ahead_blks, max_blks);
415cb92f413SAlexander Motin 	zs->zs_ipf_blkid = ipf_start + ipf_nblks;
416cb92f413SAlexander Motin 
417cb92f413SAlexander Motin 	epbs = zf->zf_dnode->dn_indblkshift - SPA_BLKPTRSHIFT;
418cb92f413SAlexander Motin 	ipf_istart = P2ROUNDUP(ipf_start, 1 << epbs) >> epbs;
419cb92f413SAlexander Motin 	ipf_iend = P2ROUNDUP(zs->zs_ipf_blkid, 1 << epbs) >> epbs;
420cb92f413SAlexander Motin 
421cb92f413SAlexander Motin 	zs->zs_atime = gethrtime();
4229e3493cbSJason King 	/* no prior reads in progress */
4239e3493cbSJason King 	if (zfs_refcount_count(&zs->zs_blocks) == 0)
4249e3493cbSJason King 		zs->zs_start_time = zs->zs_atime;
425cb92f413SAlexander Motin 	zs->zs_blkid = end_of_access_blkid;
426ecd18decSAlexander Motin 	zfs_refcount_add_few(&zs->zs_blocks, pf_nblks + ipf_iend - ipf_istart,
4279e3493cbSJason King 	    NULL);
428cf6106c8SMatthew Ahrens 	mutex_exit(&zs->zs_lock);
429fa9e4066Sahrens 	rw_exit(&zf->zf_rwlock);
4309e3493cbSJason King 	issued = 0;
431cb92f413SAlexander Motin 
432cb92f413SAlexander Motin 	/*
433cb92f413SAlexander Motin 	 * dbuf_prefetch() is asynchronous (even when it needs to read
434cb92f413SAlexander Motin 	 * indirect blocks), but we still prefer to drop our locks before
435cb92f413SAlexander Motin 	 * calling it to reduce the time we hold them.
436cb92f413SAlexander Motin 	 */
437cb92f413SAlexander Motin 
438cf6106c8SMatthew Ahrens 	for (int i = 0; i < pf_nblks; i++) {
4399e3493cbSJason King 		issued += dbuf_prefetch_impl(zf->zf_dnode, 0, pf_start + i,
4409e3493cbSJason King 		    ZIO_PRIORITY_ASYNC_READ, ARC_FLAG_PREDICTIVE_PREFETCH,
4419e3493cbSJason King 		    dmu_zfetch_stream_done, zs);
442fa9e4066Sahrens 	}
443cb92f413SAlexander Motin 	for (int64_t iblk = ipf_istart; iblk < ipf_iend; iblk++) {
4449e3493cbSJason King 		issued += dbuf_prefetch_impl(zf->zf_dnode, 1, iblk,
4459e3493cbSJason King 		    ZIO_PRIORITY_ASYNC_READ, ARC_FLAG_PREDICTIVE_PREFETCH,
4469e3493cbSJason King 		    dmu_zfetch_stream_done, zs);
447cb92f413SAlexander Motin 	}
4489704bf7fSPaul Dagnelie 	if (!have_lock)
4499704bf7fSPaul Dagnelie 		rw_exit(&zf->zf_dnode->dn_struct_rwlock);
450cf6106c8SMatthew Ahrens 	ZFETCHSTAT_BUMP(zfetchstat_hits);
4519e3493cbSJason King 
4529e3493cbSJason King 	if (issued)
4539e3493cbSJason King 		ZFETCHSTAT_ADD(zfetchstat_io_issued, issued);
454fa9e4066Sahrens }
455