Re: [PATCH 11/12] iomap: add read_folio_range() handler for buffered writes

Brian Foster
2025-06-27T19:15:07Z

On Fri, Jun 27, 2025 at 09:02:44AM +0200, Christoph Hellwig wrote:
> From: Joanne Koong <joannelkoong@gmail.com>
>
> Add a read_folio_range() handler for buffered writes that filesystems
> may pass in if they wish to provide a custom handler for synchronously
> reading in the contents of a folio.
>
> Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
> [hch: renamed to read_folio_range, pass less arguments]
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
> .../filesystems/iomap/operations.rst | 6 +++++
> fs/iomap/buffered-io.c | 25 +++++++++++--------
> include/linux/iomap.h | 10 ++++++++
> 3 files changed, 31 insertions(+), 10 deletions(-)
>
> diff --git a/Documentation/filesystems/iomap/operations.rst b/Documentation/filesystems/iomap/operations.rst
> index 167d3ca7819c..04432f40e7a2 100644
> --- a/Documentation/filesystems/iomap/operations.rst
> +++ b/Documentation/filesystems/iomap/operations.rst
> @@ -68,6 +68,8 @@ The following address space operations can be wrapped easily:
> void (*put_folio)(struct inode *inode, loff_t pos, unsigned copied,
> struct folio *folio);
> bool (*iomap_valid)(struct inode *inode, const struct iomap *iomap);
> + int (*read_folio_range)(const struct iomap_iter *iter,
> + struct folio *folio, loff_t pos, size_t len);

Whitespace ^

> };
>
> iomap calls these functions:
...
> diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
> index a77686977a2e..1a9ade77aeeb 100644
> --- a/fs/iomap/buffered-io.c
> +++ b/fs/iomap/buffered-io.c
> @@ -667,22 +667,23 @@ iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
> pos + len - 1);
> }
>
> -static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
> - size_t poff, size_t plen, const struct iomap *iomap)
> +static int iomap_read_folio_range(const struct iomap_iter *iter,
> + struct folio *folio, loff_t pos, size_t len)
> {
> + const struct iomap *srcmap = iomap_iter_srcmap(iter);
> struct bio_vec bvec;
> struct bio bio;
>
> - bio_init(&bio, iomap->bdev, &bvec, 1, REQ_OP_READ);
> - bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
> - bio_add_folio_nofail(&bio, folio, plen, poff);
> + bio_init(&bio, srcmap->bdev, &bvec, 1, REQ_OP_READ);
> + bio.bi_iter.bi_sector = iomap_sector(srcmap, pos);
> + bio_add_folio_nofail(&bio, folio, len, offset_in_folio(folio, pos));
> return submit_bio_wait(&bio);
> }

Hmm, so this kind of makes my brain hurt... pos here is now the old
block_start and len is the old plen. We used to pass poff to the
add_folio_nofail() call, and now that is dropped and instead we use
offset_in_folio(..., pos). The old poff is an output of the previous
iomap_adjust_read_range() call, which is initially set to
offset_in_folio(folio, *pos), of which *pos is block_start and is bumped
in that function in the same places that poff is. Therefore old poff and
new offset_in_folio(folio, pos) are logically equivalent. Am I following
that correctly?

Brian

>
> -static int __iomap_write_begin(const struct iomap_iter *iter, size_t len,
> +static int __iomap_write_begin(const struct iomap_iter *iter,
> + const struct iomap_write_ops *write_ops, size_t len,
> struct folio *folio)
> {
> - const struct iomap *srcmap = iomap_iter_srcmap(iter);
> struct iomap_folio_state *ifs;
> loff_t pos = iter->pos;
> loff_t block_size = i_blocksize(iter->inode);
> @@ -731,8 +732,12 @@ static int __iomap_write_begin(const struct iomap_iter *iter, size_t len,
> if (iter->flags & IOMAP_NOWAIT)
> return -EAGAIN;
>
> - status = iomap_read_folio_sync(block_start, folio,
> - poff, plen, srcmap);
> + if (write_ops && write_ops->read_folio_range)
> + status = write_ops->read_folio_range(iter,
> + folio, block_start, plen);
> + else
> + status = iomap_read_folio_range(iter,
> + folio, block_start, plen);
> if (status)
> return status;
> }
> @@ -848,7 +853,7 @@ static int iomap_write_begin(struct iomap_iter *iter,
> else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
> status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
> else
> - status = __iomap_write_begin(iter, len, folio);
> + status = __iomap_write_begin(iter, write_ops, len, folio);
>
> if (unlikely(status))
> goto out_unlock;
> diff --git a/include/linux/iomap.h b/include/linux/iomap.h
> index 482787013ff7..b3588dd43105 100644
> --- a/include/linux/iomap.h
> +++ b/include/linux/iomap.h
> @@ -166,6 +166,16 @@ struct iomap_write_ops {
> * locked by the iomap code.
> */
> bool (*iomap_valid)(struct inode *inode, const struct iomap *iomap);
> +
> + /*
> + * Optional if the filesystem wishes to provide a custom handler for
> + * reading in the contents of a folio, otherwise iomap will default to
> + * submitting a bio read request.
> + *
> + * The read must be done synchronously.
> + */
> + int (*read_folio_range)(const struct iomap_iter *iter,
> + struct folio *folio, loff_t pos, size_t len);
> };
>
> /*
> --
> 2.47.2
>
>