]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
fs/ntfs3: improve readahead for bitmap initialization and large directory scans
authorKonstantin Komarov <almaz.alexandrovich@paragon-software.com>
Mon, 8 Dec 2025 20:27:20 +0000 (23:27 +0300)
committerKonstantin Komarov <almaz.alexandrovich@paragon-software.com>
Fri, 19 Dec 2025 18:04:01 +0000 (19:04 +0100)
Previously sequential reads operations relied solely on single-page reads,
causing the block layer to perform many synchronous I/O requests,
especially for large volumes or large directories. This patch introduces
explicit readahead via page_cache_sync_readahead() and file_ra_state to
reduce I/O latency and improve sequential throughput.

Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
fs/ntfs3/bitmap.c
fs/ntfs3/dir.c
fs/ntfs3/fslog.c
fs/ntfs3/fsntfs.c
fs/ntfs3/index.c
fs/ntfs3/ntfs_fs.h

index 65d05e6a05665088ce39d0a8782078410fb9d8f5..db7d0ecfb46961b38e3e5571e5514bc0a29578c4 100644 (file)
@@ -508,6 +508,8 @@ static int wnd_rescan(struct wnd_bitmap *wnd)
        size_t wpos, wbit, iw, vbo;
        struct buffer_head *bh = NULL;
        CLST lcn, clen;
+       struct file_ra_state *ra;
+       struct address_space *mapping = sb->s_bdev->bd_mapping;
 
        wnd->uptodated = 0;
        wnd->extent_max = 0;
@@ -516,6 +518,13 @@ static int wnd_rescan(struct wnd_bitmap *wnd)
 
        vbo = 0;
 
+       /* Allocate in memory instead of stack. Not critical if failed. */
+       ra = kzalloc(sizeof(*ra), GFP_NOFS);
+       if (ra) {
+               file_ra_state_init(ra, mapping);
+               ra->ra_pages = (wnd->nbits / 8 + PAGE_SIZE - 1) >> PAGE_SHIFT;
+       }
+
        for (iw = 0; iw < wnd->nwnd; iw++) {
                if (iw + 1 == wnd->nwnd)
                        wbits = wnd->bits_last;
@@ -552,6 +561,13 @@ static int wnd_rescan(struct wnd_bitmap *wnd)
                        len = ((u64)clen << cluster_bits) - off;
                }
 
+               if (ra) {
+                       pgoff_t idx = lbo >> PAGE_SHIFT;
+                       if (!ra_has_index(ra, idx))
+                               page_cache_sync_readahead(mapping, ra, NULL,
+                                                         idx, 1);
+               }
+
                bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
                if (!bh) {
                        err = -EIO;
@@ -638,6 +654,7 @@ next_wnd:
        }
 
 out:
+       kfree(ra);
        return err;
 }
 
index b98e95d6b4d993db114283a0f38cf10b1a7520a9..1dbb661ffe0f6acdf41f072d9dce2ab9931895f2 100644 (file)
@@ -487,8 +487,8 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx)
                        goto out;
                }
 
-               err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits,
-                               &node);
+               err = indx_read_ra(&ni->dir, ni, bit << ni->dir.idx2vbn_bits,
+                                  &node, &file->f_ra);
                if (err)
                        goto out;
 
index 38934e6978ececd6630017ef01d9ef14911032f1..ee24ef0dd7256f934652450fc476d4c349f6fd69 100644 (file)
@@ -1074,6 +1074,8 @@ struct ntfs_log {
        u32 client_undo_commit;
 
        struct restart_info rst_info, rst_info2;
+
+       struct file_ra_state read_ahead;
 };
 
 static inline u32 lsn_to_vbo(struct ntfs_log *log, const u64 lsn)
@@ -1164,8 +1166,8 @@ static int read_log_page(struct ntfs_log *log, u32 vbo,
 
        page_buf = page_off ? log->one_page_buf : *buffer;
 
-       err = ntfs_read_run_nb(ni->mi.sbi, &ni->file.run, page_vbo, page_buf,
-                              log->page_size, NULL);
+       err = ntfs_read_run_nb_ra(ni->mi.sbi, &ni->file.run, page_vbo, page_buf,
+                                 log->page_size, NULL, &log->read_ahead);
        if (err)
                goto out;
 
index a3cb39ff470f0e3740958be3d001849c8c9fab82..ff0b2595f32a21c91a2b7bfc2e8853b3695e5d4e 100644 (file)
@@ -1164,11 +1164,13 @@ struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi,
        return ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
 }
 
-int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run,
-                    u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb)
+int ntfs_read_run_nb_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run,
+                       u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb,
+                       struct file_ra_state *ra)
 {
        int err;
        struct super_block *sb = sbi->sb;
+       struct address_space *mapping = sb->s_bdev->bd_mapping;
        u32 blocksize = sb->s_blocksize;
        u8 cluster_bits = sbi->cluster_bits;
        u32 off = vbo & sbi->cluster_mask;
@@ -1208,10 +1210,22 @@ int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run,
                nb->bytes = bytes;
        }
 
+       if (ra && !ra->ra_pages)
+               file_ra_state_init(ra, mapping);
+
        for (;;) {
                u32 len32 = len >= bytes ? bytes : len;
                sector_t block = lbo >> sb->s_blocksize_bits;
 
+               if (ra) {
+                       pgoff_t index = lbo >> PAGE_SHIFT;
+                       if (!ra_has_index(ra, index)) {
+                               page_cache_sync_readahead(mapping, ra, NULL,
+                                                         index, 1);
+                               ra->prev_pos = (loff_t)index << PAGE_SHIFT;
+                       }
+               }
+
                do {
                        u32 op = blocksize - off;
 
@@ -1282,11 +1296,11 @@ out:
  *
  * Return: < 0 if error, 0 if ok, -E_NTFS_FIXUP if need to update fixups.
  */
-int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
-                struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
-                struct ntfs_buffers *nb)
+int ntfs_read_bh_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run,
+                   u64 vbo, struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
+                   struct ntfs_buffers *nb, struct file_ra_state *ra)
 {
-       int err = ntfs_read_run_nb(sbi, run, vbo, rhdr, bytes, nb);
+       int err = ntfs_read_run_nb_ra(sbi, run, vbo, rhdr, bytes, nb, ra);
 
        if (err)
                return err;
@@ -1347,8 +1361,7 @@ int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
                                wait_on_buffer(bh);
 
                                lock_buffer(bh);
-                               if (!buffer_uptodate(bh))
-                               {
+                               if (!buffer_uptodate(bh)) {
                                        memset(bh->b_data, 0, blocksize);
                                        set_buffer_uptodate(bh);
                                }
index 7c7bae84ec9ac058572d1e6986101931311b5653..f0cfa000ffbb066dbe3fa8378761eca2950d1429 100644 (file)
@@ -1026,17 +1026,18 @@ static int indx_write(struct ntfs_index *indx, struct ntfs_inode *ni,
 }
 
 /*
- * indx_read
+ * indx_read_ra
  *
  * If ntfs_readdir calls this function
  * inode is shared locked and no ni_lock.
  * Use rw_semaphore for read/write access to alloc_run.
  */
-int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
-             struct indx_node **node)
+int indx_read_ra(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
+                struct indx_node **node, struct file_ra_state *ra)
 {
        int err;
        struct INDEX_BUFFER *ib;
+       struct ntfs_sb_info *sbi = ni->mi.sbi;
        struct runs_tree *run = &indx->alloc_run;
        struct rw_semaphore *lock = &indx->run_lock;
        u64 vbo = (u64)vbn << indx->vbn2vbo_bits;
@@ -1062,7 +1063,7 @@ int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
        }
 
        down_read(lock);
-       err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
+       err = ntfs_read_bh_ra(sbi, run, vbo, &ib->rhdr, bytes, &in->nb, ra);
        up_read(lock);
        if (!err)
                goto ok;
@@ -1082,7 +1083,7 @@ int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn,
                goto out;
 
        down_read(lock);
-       err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb);
+       err = ntfs_read_bh_ra(sbi, run, vbo, &ib->rhdr, bytes, &in->nb, ra);
        up_read(lock);
        if (err == -E_NTFS_FIXUP)
                goto ok;
@@ -1098,7 +1099,7 @@ ok:
        }
 
        if (err == -E_NTFS_FIXUP) {
-               ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &in->nb, 0);
+               ntfs_write_bh(sbi, &ib->rhdr, &in->nb, 0);
                err = 0;
        }
 
index 7b619bb151ce9a625cb889dd828bd7921e90f598..18b14f7db4adbe415b8608e5f9f24dc1d438471b 100644 (file)
@@ -625,11 +625,27 @@ int ntfs_sb_write_run(struct ntfs_sb_info *sbi, const struct runs_tree *run,
                      u64 vbo, const void *buf, size_t bytes, int sync);
 struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi,
                                   const struct runs_tree *run, u64 vbo);
-int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run,
-                    u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb);
-int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
-                struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
-                struct ntfs_buffers *nb);
+int ntfs_read_run_nb_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run,
+                       u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb,
+                       struct file_ra_state *ra);
+static inline int ntfs_read_run_nb(struct ntfs_sb_info *sbi,
+                                  const struct runs_tree *run, u64 vbo,
+                                  void *buf, u32 bytes,
+                                  struct ntfs_buffers *nb)
+{
+       return ntfs_read_run_nb_ra(sbi, run, vbo, buf, bytes, nb, NULL);
+}
+int ntfs_read_bh_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run,
+                   u64 vbo, struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
+                   struct ntfs_buffers *nb, struct file_ra_state *ra);
+static inline int ntfs_read_bh(struct ntfs_sb_info *sbi,
+                              const struct runs_tree *run, u64 vbo,
+                              struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
+                              struct ntfs_buffers *nb)
+{
+       return ntfs_read_bh_ra(sbi, run, vbo, rhdr, bytes, nb, NULL);
+}
+
 int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
                u32 bytes, struct ntfs_buffers *nb);
 int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr,
@@ -695,8 +711,13 @@ int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
              const struct ATTRIB *attr, enum index_mutex_classed type);
 struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
                                 struct ATTRIB **attr, struct mft_inode **mi);
-int indx_read(struct ntfs_index *idx, struct ntfs_inode *ni, CLST vbn,
-             struct indx_node **node);
+int indx_read_ra(struct ntfs_index *idx, struct ntfs_inode *ni, CLST vbn,
+                struct indx_node **node, struct file_ra_state *ra);
+static inline int indx_read(struct ntfs_index *idx, struct ntfs_inode *ni,
+                           CLST vbn, struct indx_node **node)
+{
+       return indx_read_ra(idx, ni, vbn, node, NULL);
+}
 int indx_find(struct ntfs_index *indx, struct ntfs_inode *dir,
              const struct INDEX_ROOT *root, const void *Key, size_t KeyLen,
              const void *param, int *diff, struct NTFS_DE **entry,