Path: blob/main/sys/contrib/openzfs/module/zfs/dmu_zfetch.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2009 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526/*27* Copyright (c) 2013, 2017 by Delphix. All rights reserved.28*/2930#include <sys/zfs_context.h>31#include <sys/arc_impl.h>32#include <sys/dnode.h>33#include <sys/dmu_objset.h>34#include <sys/dmu_zfetch.h>35#include <sys/dmu.h>36#include <sys/dbuf.h>37#include <sys/kstat.h>38#include <sys/wmsum.h>3940/*41* This tunable disables predictive prefetch. Note that it leaves "prescient"42* prefetch (e.g. prefetch for zfs send) intact. Unlike predictive prefetch,43* prescient prefetch never issues i/os that end up not being needed,44* so it can't hurt performance.45*/4647static int zfs_prefetch_disable = B_FALSE;4849/* max # of streams per zfetch */50static unsigned int zfetch_max_streams = 8;51/* min time before stream reclaim */52static unsigned int zfetch_min_sec_reap = 1;53/* max time before stream delete */54static unsigned int zfetch_max_sec_reap = 2;55#ifdef _ILP3256/* min bytes to prefetch per stream (default 2MB) */57static unsigned int zfetch_min_distance = 2 * 1024 * 1024;58/* max bytes to prefetch per stream (default 8MB) */59unsigned int zfetch_max_distance = 8 * 1024 * 1024;60#else61/* min bytes to prefetch per stream (default 4MB) */62static unsigned int zfetch_min_distance = 4 * 1024 * 1024;63/* max bytes to prefetch per stream (default 64MB) */64unsigned int zfetch_max_distance = 64 * 1024 * 1024;65#endif66/* max bytes to prefetch indirects for per stream (default 128MB) */67unsigned int zfetch_max_idistance = 128 * 1024 * 1024;68/* max request reorder distance within a stream (default 16MB) */69unsigned int zfetch_max_reorder = 16 * 1024 * 1024;70/* Max log2 fraction of holes in a stream */71unsigned int zfetch_hole_shift = 2;7273typedef struct zfetch_stats {74kstat_named_t zfetchstat_hits;75kstat_named_t zfetchstat_future;76kstat_named_t zfetchstat_stride;77kstat_named_t zfetchstat_past;78kstat_named_t zfetchstat_misses;79kstat_named_t zfetchstat_max_streams;80kstat_named_t zfetchstat_io_issued;81kstat_named_t zfetchstat_io_active;82} zfetch_stats_t;8384static zfetch_stats_t zfetch_stats = {85{ "hits", KSTAT_DATA_UINT64 },86{ "future", KSTAT_DATA_UINT64 },87{ "stride", KSTAT_DATA_UINT64 },88{ "past", KSTAT_DATA_UINT64 },89{ "misses", KSTAT_DATA_UINT64 },90{ "max_streams", KSTAT_DATA_UINT64 },91{ "io_issued", KSTAT_DATA_UINT64 },92{ "io_active", KSTAT_DATA_UINT64 },93};9495struct {96wmsum_t zfetchstat_hits;97wmsum_t zfetchstat_future;98wmsum_t zfetchstat_stride;99wmsum_t zfetchstat_past;100wmsum_t zfetchstat_misses;101wmsum_t zfetchstat_max_streams;102wmsum_t zfetchstat_io_issued;103aggsum_t zfetchstat_io_active;104} zfetch_sums;105106#define ZFETCHSTAT_BUMP(stat) \107wmsum_add(&zfetch_sums.stat, 1)108#define ZFETCHSTAT_ADD(stat, val) \109wmsum_add(&zfetch_sums.stat, val)110111112static kstat_t *zfetch_ksp;113114static int115zfetch_kstats_update(kstat_t *ksp, int rw)116{117zfetch_stats_t *zs = ksp->ks_data;118119if (rw == KSTAT_WRITE)120return (EACCES);121zs->zfetchstat_hits.value.ui64 =122wmsum_value(&zfetch_sums.zfetchstat_hits);123zs->zfetchstat_future.value.ui64 =124wmsum_value(&zfetch_sums.zfetchstat_future);125zs->zfetchstat_stride.value.ui64 =126wmsum_value(&zfetch_sums.zfetchstat_stride);127zs->zfetchstat_past.value.ui64 =128wmsum_value(&zfetch_sums.zfetchstat_past);129zs->zfetchstat_misses.value.ui64 =130wmsum_value(&zfetch_sums.zfetchstat_misses);131zs->zfetchstat_max_streams.value.ui64 =132wmsum_value(&zfetch_sums.zfetchstat_max_streams);133zs->zfetchstat_io_issued.value.ui64 =134wmsum_value(&zfetch_sums.zfetchstat_io_issued);135zs->zfetchstat_io_active.value.ui64 =136aggsum_value(&zfetch_sums.zfetchstat_io_active);137return (0);138}139140void141zfetch_init(void)142{143wmsum_init(&zfetch_sums.zfetchstat_hits, 0);144wmsum_init(&zfetch_sums.zfetchstat_future, 0);145wmsum_init(&zfetch_sums.zfetchstat_stride, 0);146wmsum_init(&zfetch_sums.zfetchstat_past, 0);147wmsum_init(&zfetch_sums.zfetchstat_misses, 0);148wmsum_init(&zfetch_sums.zfetchstat_max_streams, 0);149wmsum_init(&zfetch_sums.zfetchstat_io_issued, 0);150aggsum_init(&zfetch_sums.zfetchstat_io_active, 0);151152zfetch_ksp = kstat_create("zfs", 0, "zfetchstats", "misc",153KSTAT_TYPE_NAMED, sizeof (zfetch_stats) / sizeof (kstat_named_t),154KSTAT_FLAG_VIRTUAL);155156if (zfetch_ksp != NULL) {157zfetch_ksp->ks_data = &zfetch_stats;158zfetch_ksp->ks_update = zfetch_kstats_update;159kstat_install(zfetch_ksp);160}161}162163void164zfetch_fini(void)165{166if (zfetch_ksp != NULL) {167kstat_delete(zfetch_ksp);168zfetch_ksp = NULL;169}170171wmsum_fini(&zfetch_sums.zfetchstat_hits);172wmsum_fini(&zfetch_sums.zfetchstat_future);173wmsum_fini(&zfetch_sums.zfetchstat_stride);174wmsum_fini(&zfetch_sums.zfetchstat_past);175wmsum_fini(&zfetch_sums.zfetchstat_misses);176wmsum_fini(&zfetch_sums.zfetchstat_max_streams);177wmsum_fini(&zfetch_sums.zfetchstat_io_issued);178ASSERT0(aggsum_value(&zfetch_sums.zfetchstat_io_active));179aggsum_fini(&zfetch_sums.zfetchstat_io_active);180}181182/*183* This takes a pointer to a zfetch structure and a dnode. It performs the184* necessary setup for the zfetch structure, grokking data from the185* associated dnode.186*/187void188dmu_zfetch_init(zfetch_t *zf, dnode_t *dno)189{190if (zf == NULL)191return;192zf->zf_dnode = dno;193zf->zf_numstreams = 0;194195list_create(&zf->zf_stream, sizeof (zstream_t),196offsetof(zstream_t, zs_node));197198mutex_init(&zf->zf_lock, NULL, MUTEX_DEFAULT, NULL);199}200201static void202dmu_zfetch_stream_fini(zstream_t *zs)203{204ASSERT(!list_link_active(&zs->zs_node));205zfs_refcount_destroy(&zs->zs_callers);206zfs_refcount_destroy(&zs->zs_refs);207kmem_free(zs, sizeof (*zs));208}209210static void211dmu_zfetch_stream_remove(zfetch_t *zf, zstream_t *zs)212{213ASSERT(MUTEX_HELD(&zf->zf_lock));214list_remove(&zf->zf_stream, zs);215zf->zf_numstreams--;216membar_producer();217if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0)218dmu_zfetch_stream_fini(zs);219}220221/*222* Clean-up state associated with a zfetch structure (e.g. destroy the223* streams). This doesn't free the zfetch_t itself, that's left to the caller.224*/225void226dmu_zfetch_fini(zfetch_t *zf)227{228zstream_t *zs;229230mutex_enter(&zf->zf_lock);231while ((zs = list_head(&zf->zf_stream)) != NULL)232dmu_zfetch_stream_remove(zf, zs);233mutex_exit(&zf->zf_lock);234list_destroy(&zf->zf_stream);235mutex_destroy(&zf->zf_lock);236237zf->zf_dnode = NULL;238}239240/*241* If there aren't too many active streams already, create one more.242* In process delete/reuse all streams without hits for zfetch_max_sec_reap.243* If needed, reuse oldest stream without hits for zfetch_min_sec_reap or ever.244* The "blkid" argument is the next block that we expect this stream to access.245*/246static void247dmu_zfetch_stream_create(zfetch_t *zf, uint64_t blkid)248{249zstream_t *zs, *zs_next, *zs_old = NULL;250uint_t now = gethrestime_sec(), t;251252ASSERT(MUTEX_HELD(&zf->zf_lock));253254/*255* Delete too old streams, reusing the first found one.256*/257t = now - zfetch_max_sec_reap;258for (zs = list_head(&zf->zf_stream); zs != NULL; zs = zs_next) {259zs_next = list_next(&zf->zf_stream, zs);260/*261* Skip if still active. 1 -- zf_stream reference.262*/263if ((int)(zs->zs_atime - t) >= 0)264continue;265if (zfs_refcount_count(&zs->zs_refs) != 1)266continue;267if (zs_old)268dmu_zfetch_stream_remove(zf, zs);269else270zs_old = zs;271}272if (zs_old) {273zs = zs_old;274list_remove(&zf->zf_stream, zs);275goto reuse;276}277278/*279* The maximum number of streams is normally zfetch_max_streams,280* but for small files we lower it such that it's at least possible281* for all the streams to be non-overlapping.282*/283uint32_t max_streams = MAX(1, MIN(zfetch_max_streams,284(zf->zf_dnode->dn_maxblkid << zf->zf_dnode->dn_datablkshift) /285zfetch_max_distance));286if (zf->zf_numstreams >= max_streams) {287t = now - zfetch_min_sec_reap;288for (zs = list_head(&zf->zf_stream); zs != NULL;289zs = list_next(&zf->zf_stream, zs)) {290if ((int)(zs->zs_atime - t) >= 0)291continue;292if (zfs_refcount_count(&zs->zs_refs) != 1)293continue;294if (zs_old == NULL ||295(int)(zs_old->zs_atime - zs->zs_atime) >= 0)296zs_old = zs;297}298if (zs_old) {299zs = zs_old;300list_remove(&zf->zf_stream, zs);301goto reuse;302}303ZFETCHSTAT_BUMP(zfetchstat_max_streams);304return;305}306307zs = kmem_zalloc(sizeof (*zs), KM_SLEEP);308zfs_refcount_create(&zs->zs_callers);309zfs_refcount_create(&zs->zs_refs);310/* One reference for zf_stream. */311zfs_refcount_add(&zs->zs_refs, NULL);312zf->zf_numstreams++;313314reuse:315list_insert_head(&zf->zf_stream, zs);316zs->zs_blkid = blkid;317/* Allow immediate stream reuse until first hit. */318zs->zs_atime = now - zfetch_min_sec_reap;319memset(zs->zs_ranges, 0, sizeof (zs->zs_ranges));320zs->zs_pf_dist = 0;321zs->zs_ipf_dist = 0;322zs->zs_pf_start = blkid;323zs->zs_pf_end = blkid;324zs->zs_ipf_start = blkid;325zs->zs_ipf_end = blkid;326zs->zs_missed = B_FALSE;327zs->zs_more = B_FALSE;328}329330static void331dmu_zfetch_done(void *arg, uint64_t level, uint64_t blkid, boolean_t io_issued)332{333zstream_t *zs = arg;334335if (io_issued && level == 0 && blkid < zs->zs_blkid)336zs->zs_more = B_TRUE;337if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0)338dmu_zfetch_stream_fini(zs);339aggsum_add(&zfetch_sums.zfetchstat_io_active, -1);340}341342/*343* Process stream hit access for nblks blocks starting at zs_blkid. Return344* number of blocks to proceed for after aggregation with future ranges.345*/346static uint64_t347dmu_zfetch_hit(zstream_t *zs, uint64_t nblks)348{349uint_t i, j;350351/* Optimize sequential accesses (no future ranges). */352if (zs->zs_ranges[0].start == 0)353goto done;354355/* Look for intersections with further ranges. */356for (i = 0; i < ZFETCH_RANGES; i++) {357zsrange_t *r = &zs->zs_ranges[i];358if (r->start == 0 || r->start > nblks)359break;360if (r->end >= nblks) {361nblks = r->end;362i++;363break;364}365}366367/* Delete all found intersecting ranges, updates remaining. */368for (j = 0; i < ZFETCH_RANGES; i++, j++) {369if (zs->zs_ranges[i].start == 0)370break;371ASSERT3U(zs->zs_ranges[i].start, >, nblks);372ASSERT3U(zs->zs_ranges[i].end, >, nblks);373zs->zs_ranges[j].start = zs->zs_ranges[i].start - nblks;374zs->zs_ranges[j].end = zs->zs_ranges[i].end - nblks;375}376if (j < ZFETCH_RANGES) {377zs->zs_ranges[j].start = 0;378zs->zs_ranges[j].end = 0;379}380381done:382zs->zs_blkid += nblks;383return (nblks);384}385386/*387* Process future stream access for nblks blocks starting at blkid. Return388* number of blocks to proceed for if future ranges reach fill threshold.389*/390static uint64_t391dmu_zfetch_future(zstream_t *zs, uint64_t blkid, uint64_t nblks)392{393ASSERT3U(blkid, >, zs->zs_blkid);394blkid -= zs->zs_blkid;395ASSERT3U(blkid + nblks, <=, UINT16_MAX);396397/* Search for first and last intersection or insert point. */398uint_t f = ZFETCH_RANGES, l = 0, i;399for (i = 0; i < ZFETCH_RANGES; i++) {400zsrange_t *r = &zs->zs_ranges[i];401if (r->start == 0 || r->start > blkid + nblks)402break;403if (r->end < blkid)404continue;405if (f > i)406f = i;407if (l < i)408l = i;409}410if (f <= l) {411/* Got some intersecting range, expand it if needed. */412if (zs->zs_ranges[f].start > blkid)413zs->zs_ranges[f].start = blkid;414zs->zs_ranges[f].end = MAX(zs->zs_ranges[l].end, blkid + nblks);415if (f < l) {416/* Got more than one intersection, remove others. */417for (f++, l++; l < ZFETCH_RANGES; f++, l++) {418zs->zs_ranges[f].start = zs->zs_ranges[l].start;419zs->zs_ranges[f].end = zs->zs_ranges[l].end;420}421zs->zs_ranges[f].start = 0;422zs->zs_ranges[f].end = 0;423}424} else if (i < ZFETCH_RANGES) {425/* Got no intersecting ranges, insert new one. */426for (l = ZFETCH_RANGES - 1; l > i; l--) {427zs->zs_ranges[l].start = zs->zs_ranges[l - 1].start;428zs->zs_ranges[l].end = zs->zs_ranges[l - 1].end;429}430zs->zs_ranges[i].start = blkid;431zs->zs_ranges[i].end = blkid + nblks;432} else {433/* No space left to insert. Drop the range. */434return (0);435}436437/* Check if with the new access addition we reached fill threshold. */438if (zfetch_hole_shift >= 16)439return (0);440uint_t hole = 0;441for (i = f = l = 0; i < ZFETCH_RANGES; i++) {442zsrange_t *r = &zs->zs_ranges[i];443if (r->start == 0)444break;445hole += r->start - f;446f = r->end;447if (hole <= r->end >> zfetch_hole_shift)448l = r->end;449}450if (l > 0)451return (dmu_zfetch_hit(zs, l));452453return (0);454}455456/*457* This is the predictive prefetch entry point. dmu_zfetch_prepare()458* associates dnode access specified with blkid and nblks arguments with459* prefetch stream, predicts further accesses based on that stats and returns460* the stream pointer on success. That pointer must later be passed to461* dmu_zfetch_run() to initiate the speculative prefetch for the stream and462* release it. dmu_zfetch() is a wrapper for simple cases when window between463* prediction and prefetch initiation is not needed.464* fetch_data argument specifies whether actual data blocks should be fetched:465* FALSE -- prefetch only indirect blocks for predicted data blocks;466* TRUE -- prefetch predicted data blocks plus following indirect blocks.467*/468zstream_t *469dmu_zfetch_prepare(zfetch_t *zf, uint64_t blkid, uint64_t nblks,470boolean_t fetch_data, boolean_t have_lock)471{472zstream_t *zs;473spa_t *spa = zf->zf_dnode->dn_objset->os_spa;474zfs_prefetch_type_t os_prefetch = zf->zf_dnode->dn_objset->os_prefetch;475int64_t ipf_start, ipf_end;476477if (zfs_prefetch_disable || os_prefetch == ZFS_PREFETCH_NONE)478return (NULL);479480if (os_prefetch == ZFS_PREFETCH_METADATA)481fetch_data = B_FALSE;482483/*484* If we haven't yet loaded the indirect vdevs' mappings, we485* can only read from blocks that we carefully ensure are on486* concrete vdevs (or previously-loaded indirect vdevs). So we487* can't allow the predictive prefetcher to attempt reads of other488* blocks (e.g. of the MOS's dnode object).489*/490if (!spa_indirect_vdevs_loaded(spa))491return (NULL);492493/*494* As a fast path for small (single-block) files, ignore access495* to the first block.496*/497if (!have_lock && blkid == 0)498return (NULL);499500if (!have_lock)501rw_enter(&zf->zf_dnode->dn_struct_rwlock, RW_READER);502503/*504* A fast path for small files for which no prefetch will505* happen.506*/507uint64_t maxblkid = zf->zf_dnode->dn_maxblkid;508if (maxblkid < 2) {509if (!have_lock)510rw_exit(&zf->zf_dnode->dn_struct_rwlock);511return (NULL);512}513mutex_enter(&zf->zf_lock);514515/*516* Find perfect prefetch stream. Depending on whether the accesses517* are block-aligned, first block of the new access may either follow518* the last block of the previous access, or be equal to it.519*/520unsigned int dbs = zf->zf_dnode->dn_datablkshift;521uint64_t end_blkid = blkid + nblks;522for (zs = list_head(&zf->zf_stream); zs != NULL;523zs = list_next(&zf->zf_stream, zs)) {524if (blkid == zs->zs_blkid) {525goto hit;526} else if (blkid + 1 == zs->zs_blkid) {527blkid++;528nblks--;529goto hit;530}531}532533/*534* Find close enough prefetch stream. Access crossing stream position535* is a hit in its new part. Access ahead of stream position considered536* a hit for metadata prefetch, since we do not care about fill percent,537* or stored for future otherwise. Access behind stream position is538* silently ignored, since we already skipped it reaching fill percent.539*/540uint_t max_reorder = MIN((zfetch_max_reorder >> dbs) + 1, UINT16_MAX);541uint_t t = gethrestime_sec() - zfetch_max_sec_reap;542for (zs = list_head(&zf->zf_stream); zs != NULL;543zs = list_next(&zf->zf_stream, zs)) {544if (blkid > zs->zs_blkid) {545if (end_blkid <= zs->zs_blkid + max_reorder) {546if (!fetch_data) {547nblks = dmu_zfetch_hit(zs,548end_blkid - zs->zs_blkid);549ZFETCHSTAT_BUMP(zfetchstat_stride);550goto future;551}552nblks = dmu_zfetch_future(zs, blkid, nblks);553if (nblks > 0)554ZFETCHSTAT_BUMP(zfetchstat_stride);555else556ZFETCHSTAT_BUMP(zfetchstat_future);557goto future;558}559} else if (end_blkid >= zs->zs_blkid) {560nblks -= zs->zs_blkid - blkid;561blkid += zs->zs_blkid - blkid;562goto hit;563} else if (end_blkid + max_reorder > zs->zs_blkid &&564(int)(zs->zs_atime - t) >= 0) {565ZFETCHSTAT_BUMP(zfetchstat_past);566zs->zs_atime = gethrestime_sec();567goto out;568}569}570571/*572* This access is not part of any existing stream. Create a new573* stream for it unless we are at the end of file.574*/575ASSERT0P(zs);576if (end_blkid < maxblkid)577dmu_zfetch_stream_create(zf, end_blkid);578mutex_exit(&zf->zf_lock);579ZFETCHSTAT_BUMP(zfetchstat_misses);580ipf_start = 0;581goto prescient;582583hit:584nblks = dmu_zfetch_hit(zs, nblks);585ZFETCHSTAT_BUMP(zfetchstat_hits);586587future:588zs->zs_atime = gethrestime_sec();589590/* Exit if we already prefetched for this position before. */591if (nblks == 0)592goto out;593594/* If the file is ending, remove the stream. */595end_blkid = zs->zs_blkid;596if (end_blkid >= maxblkid) {597dmu_zfetch_stream_remove(zf, zs);598out:599mutex_exit(&zf->zf_lock);600if (!have_lock)601rw_exit(&zf->zf_dnode->dn_struct_rwlock);602return (NULL);603}604605/*606* This access was to a block that we issued a prefetch for on607* behalf of this stream. Calculate further prefetch distances.608*609* Start prefetch from the demand access size (nblks). Double the610* distance every access up to zfetch_min_distance. After that only611* if needed increase the distance by 1/8 up to zfetch_max_distance.612*613* Don't double the distance beyond single block if we have more614* than ~6% of ARC held by active prefetches. It should help with615* getting out of RAM on some badly mispredicted read patterns.616*/617unsigned int nbytes = nblks << dbs;618unsigned int pf_nblks;619if (fetch_data) {620if (unlikely(zs->zs_pf_dist < nbytes))621zs->zs_pf_dist = nbytes;622else if (zs->zs_pf_dist < zfetch_min_distance &&623(zs->zs_pf_dist < (1 << dbs) ||624aggsum_compare(&zfetch_sums.zfetchstat_io_active,625arc_c_max >> (4 + dbs)) < 0))626zs->zs_pf_dist *= 2;627else if (zs->zs_more)628zs->zs_pf_dist += zs->zs_pf_dist / 8;629zs->zs_more = B_FALSE;630if (zs->zs_pf_dist > zfetch_max_distance)631zs->zs_pf_dist = zfetch_max_distance;632pf_nblks = zs->zs_pf_dist >> dbs;633} else {634pf_nblks = 0;635}636if (zs->zs_pf_start < end_blkid)637zs->zs_pf_start = end_blkid;638if (zs->zs_pf_end < end_blkid + pf_nblks)639zs->zs_pf_end = end_blkid + pf_nblks;640641/*642* Do the same for indirects, starting where we will stop reading643* data blocks (and the indirects that point to them).644*/645if (unlikely(zs->zs_ipf_dist < nbytes))646zs->zs_ipf_dist = nbytes;647else648zs->zs_ipf_dist *= 2;649if (zs->zs_ipf_dist > zfetch_max_idistance)650zs->zs_ipf_dist = zfetch_max_idistance;651pf_nblks = zs->zs_ipf_dist >> dbs;652if (zs->zs_ipf_start < zs->zs_pf_end)653zs->zs_ipf_start = zs->zs_pf_end;654ipf_start = zs->zs_ipf_end;655if (zs->zs_ipf_end < zs->zs_pf_end + pf_nblks)656zs->zs_ipf_end = zs->zs_pf_end + pf_nblks;657658zfs_refcount_add(&zs->zs_refs, NULL);659/* Count concurrent callers. */660zfs_refcount_add(&zs->zs_callers, NULL);661mutex_exit(&zf->zf_lock);662663prescient:664/*665* Prefetch the following indirect blocks for this access to reduce666* dbuf_hold() sync read delays in dmu_buf_hold_array_by_dnode().667* This covers the gap during the first couple accesses when we can668* not predict the future yet, but know what is needed right now.669* This should be very rare for reads/writes to need more than one670* indirect, but more useful for cloning due to much bigger accesses.671*/672ipf_start = MAX(ipf_start, blkid + 1);673int epbs = zf->zf_dnode->dn_indblkshift - SPA_BLKPTRSHIFT;674ipf_start = P2ROUNDUP(ipf_start, 1 << epbs) >> epbs;675ipf_end = P2ROUNDUP(end_blkid, 1 << epbs) >> epbs;676677int issued = 0;678for (int64_t iblk = ipf_start; iblk < ipf_end; iblk++) {679issued += dbuf_prefetch(zf->zf_dnode, 1, iblk,680ZIO_PRIORITY_SYNC_READ, ARC_FLAG_PRESCIENT_PREFETCH);681}682683if (!have_lock)684rw_exit(&zf->zf_dnode->dn_struct_rwlock);685if (issued)686ZFETCHSTAT_ADD(zfetchstat_io_issued, issued);687return (zs);688}689690void691dmu_zfetch_run(zfetch_t *zf, zstream_t *zs, boolean_t missed,692boolean_t have_lock, boolean_t uncached)693{694int64_t pf_start, pf_end, ipf_start, ipf_end;695int epbs, issued;696697if (missed)698zs->zs_missed = missed;699700/*701* Postpone the prefetch if there are more concurrent callers.702* It happens when multiple requests are waiting for the same703* indirect block. The last one will run the prefetch for all.704*/705if (zfs_refcount_remove(&zs->zs_callers, NULL) != 0) {706/* Drop reference taken in dmu_zfetch_prepare(). */707if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0)708dmu_zfetch_stream_fini(zs);709return;710}711712mutex_enter(&zf->zf_lock);713if (zs->zs_missed) {714pf_start = zs->zs_pf_start;715pf_end = zs->zs_pf_start = zs->zs_pf_end;716} else {717pf_start = pf_end = 0;718}719ipf_start = zs->zs_ipf_start;720ipf_end = zs->zs_ipf_start = zs->zs_ipf_end;721mutex_exit(&zf->zf_lock);722ASSERT3S(pf_start, <=, pf_end);723ASSERT3S(ipf_start, <=, ipf_end);724725epbs = zf->zf_dnode->dn_indblkshift - SPA_BLKPTRSHIFT;726ipf_start = P2ROUNDUP(ipf_start, 1 << epbs) >> epbs;727ipf_end = P2ROUNDUP(ipf_end, 1 << epbs) >> epbs;728ASSERT3S(ipf_start, <=, ipf_end);729issued = pf_end - pf_start + ipf_end - ipf_start;730if (issued > 1) {731/* More references on top of taken in dmu_zfetch_prepare(). */732zfs_refcount_add_few(&zs->zs_refs, issued - 1, NULL);733} else if (issued == 0) {734/* Some other thread has done our work, so drop the ref. */735if (zfs_refcount_remove(&zs->zs_refs, NULL) == 0)736dmu_zfetch_stream_fini(zs);737return;738}739aggsum_add(&zfetch_sums.zfetchstat_io_active, issued);740741if (!have_lock)742rw_enter(&zf->zf_dnode->dn_struct_rwlock, RW_READER);743744issued = 0;745for (int64_t blk = pf_start; blk < pf_end; blk++) {746issued += dbuf_prefetch_impl(zf->zf_dnode, 0, blk,747ZIO_PRIORITY_ASYNC_READ, uncached ?748ARC_FLAG_UNCACHED : 0, dmu_zfetch_done, zs);749}750for (int64_t iblk = ipf_start; iblk < ipf_end; iblk++) {751issued += dbuf_prefetch_impl(zf->zf_dnode, 1, iblk,752ZIO_PRIORITY_ASYNC_READ, 0, dmu_zfetch_done, zs);753}754755if (!have_lock)756rw_exit(&zf->zf_dnode->dn_struct_rwlock);757758if (issued)759ZFETCHSTAT_ADD(zfetchstat_io_issued, issued);760}761762void763dmu_zfetch(zfetch_t *zf, uint64_t blkid, uint64_t nblks, boolean_t fetch_data,764boolean_t missed, boolean_t have_lock, boolean_t uncached)765{766zstream_t *zs;767768zs = dmu_zfetch_prepare(zf, blkid, nblks, fetch_data, have_lock);769if (zs)770dmu_zfetch_run(zf, zs, missed, have_lock, uncached);771}772773ZFS_MODULE_PARAM(zfs_prefetch, zfs_prefetch_, disable, INT, ZMOD_RW,774"Disable all ZFS prefetching");775776ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_streams, UINT, ZMOD_RW,777"Max number of streams per zfetch");778779ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, min_sec_reap, UINT, ZMOD_RW,780"Min time before stream reclaim");781782ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_sec_reap, UINT, ZMOD_RW,783"Max time before stream delete");784785ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, min_distance, UINT, ZMOD_RW,786"Min bytes to prefetch per stream");787788ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_distance, UINT, ZMOD_RW,789"Max bytes to prefetch per stream");790791ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_idistance, UINT, ZMOD_RW,792"Max bytes to prefetch indirects for per stream");793794ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, max_reorder, UINT, ZMOD_RW,795"Max request reorder distance within a stream");796797ZFS_MODULE_PARAM(zfs_prefetch, zfetch_, hole_shift, UINT, ZMOD_RW,798"Max log2 fraction of holes in a stream");799800801