[PATCH 1/2] iomap: add iomap_writepages_unbound() to write beyond EOF

Andrey Albershteyn
2024-12-29T13:37:06Z

From: Andrey Albershteyn <aalbersh@redhat.com>

Add iomap_writepages_unbound() without limit in form of EOF. XFS
will use this to writeback extended attributes (fs-verity Merkle
tree) in range far beyond EOF.

Signed-off-by: Andrey Albershteyn <aalbersh@redhat.com>
---
fs/iomap/buffered-io.c | 55 ++++++++++++++++++++++++++++++++----------
include/linux/iomap.h | 4 +++
2 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 955f19e27e47..61ec924c5b80 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -979,13 +979,13 @@ static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
* unlock and release the folio.
*/
old_size = iter->inode->i_size;
- if (pos + written > old_size) {
+ if (!(iter->flags & IOMAP_NOSIZE) && (pos + written > old_size)) {
i_size_write(iter->inode, pos + written);
iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
}
__iomap_put_folio(iter, pos, written, folio);

- if (old_size < pos)
+ if (!(iter->flags & IOMAP_NOSIZE) && (old_size < pos))
pagecache_isize_extended(iter->inode, old_size, pos);

cond_resched();
@@ -1918,18 +1918,10 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,
int error = 0;
u32 rlen;

- WARN_ON_ONCE(!folio_test_locked(folio));
- WARN_ON_ONCE(folio_test_dirty(folio));
- WARN_ON_ONCE(folio_test_writeback(folio));
-
- trace_iomap_writepage(inode, pos, folio_size(folio));
-
- if (!iomap_writepage_handle_eof(folio, inode, &end_pos)) {
- folio_unlock(folio);
- return 0;
- }
WARN_ON_ONCE(end_pos <= pos);

+ trace_iomap_writepage(inode, pos, folio_size(folio));
+
if (i_blocks_per_folio(inode, folio) > 1) {
if (!ifs) {
ifs = ifs_alloc(inode, folio, 0);
@@ -1992,6 +1984,23 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,
return error;
}

+/* Map pages bound by EOF */
+static int iomap_writepage_map_eof(struct iomap_writepage_ctx *wpc,
+ struct writeback_control *wbc, struct folio *folio)
+{
+ int error;
+ struct inode *inode = folio->mapping->host;
+ u64 end_pos = folio_pos(folio) + folio_size(folio);
+
+ if (!iomap_writepage_handle_eof(folio, inode, &end_pos)) {
+ folio_unlock(folio);
+ return 0;
+ }
+
+ error = iomap_writepage_map(wpc, wbc, folio);
+ return error;
+}
+
int
iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
struct iomap_writepage_ctx *wpc,
@@ -2008,12 +2017,32 @@ iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
PF_MEMALLOC))
return -EIO;

+ wpc->ops = ops;
+ while ((folio = writeback_iter(mapping, wbc, folio, &error))) {
+ WARN_ON_ONCE(!folio_test_locked(folio));
+ WARN_ON_ONCE(folio_test_dirty(folio));
+ WARN_ON_ONCE(folio_test_writeback(folio));
+
+ error = iomap_writepage_map_eof(wpc, wbc, folio);
+ }
+ return iomap_submit_ioend(wpc, error);
+}
+EXPORT_SYMBOL_GPL(iomap_writepages);
+
+int
+iomap_writepages_unbound(struct address_space *mapping, struct writeback_control *wbc,
+ struct iomap_writepage_ctx *wpc,
+ const struct iomap_writeback_ops *ops)
+{
+ struct folio *folio = NULL;
+ int error;
+
wpc->ops = ops;
while ((folio = writeback_iter(mapping, wbc, folio, &error)))
error = iomap_writepage_map(wpc, wbc, folio);
return iomap_submit_ioend(wpc, error);
}
-EXPORT_SYMBOL_GPL(iomap_writepages);
+EXPORT_SYMBOL_GPL(iomap_writepages_unbound);

static int __init iomap_buffered_init(void)
{
diff --git a/include/linux/iomap.h b/include/linux/iomap.h
index 5675af6b740c..3bfd3035ac28 100644
--- a/include/linux/iomap.h
+++ b/include/linux/iomap.h
@@ -181,6 +181,7 @@ struct iomap_folio_ops {
#define IOMAP_DAX (1 << 8) /* DAX mapping */
#else
#define IOMAP_DAX 0
+#define IOMAP_NOSIZE (1 << 9) /* Don't update in-memory inode size*/
#endif /* CONFIG_FS_DAX */
#define IOMAP_ATOMIC (1 << 9)

@@ -390,6 +391,9 @@ void iomap_sort_ioends(struct list_head *ioend_list);
int iomap_writepages(struct address_space *mapping,
struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
const struct iomap_writeback_ops *ops);
+int iomap_writepages_unbound(struct address_space *mapping,
+ struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
+ const struct iomap_writeback_ops *ops);

/*
* Flags for direct I/O ->end_io:
--
2.47.0