Skip to contentSkip to conversation

gfs2: Do not call iomap_zero_range beyond eof

RFC In review Andreas Gruenbacher 4 participants 8 messages May 8, 2025

Subsystems (2)
lore
Share & tools
lore

Andreas Gruenbacher

author maintainer
#0 lore raw

RFC 1/1 gfs2: Do not call iomap_zero_range beyond eof

Since commit eb65540aa9fc ("iomap: warn on zero range of a post-eof folio"), iomap_zero_range() warns when asked to zero a folio beyond eof. The warning triggers on the following code path:

gfs2_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)

    __gfs2_punch_hole()
      gfs2_block_zero_range()
        iomap_zero_range()

So far, gfs2 is just zeroing out partial pages at the beginning and end of the range, whether beyond eof or not. The data beyond eof is already expected to be all zeroes, though. Truncate the range passed to iomap_zero_range().

As an alternative approach, we could also implicitly truncate the range inside iomap_zero_range() instead of issuing a warning. Any thoughts?

Thanks,
Andreas

--

fs/gfs2/bmap.c +4 -0
index b81984def58e..d9a4309cd414 100644
@@ -1301,6 +1301,10 @@ static int gfs2_block_zero_range(struct inode *inode, loff_t from,
 				 unsigned int length)
 {
 	BUG_ON(current->journal_info);
+	if (from > inode->i_size)
+		return 0;
+	if (from + length > inode->i_size)
+		length = inode->i_size - from;
 	return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops,
 			NULL);
 }
#1 lore raw
Quoted 2 lines · Andreas Gruenbacher

As an alternative approach, we could also implicitly truncate the range inside iomap_zero_range() instead of issuing a warning. Any thoughts?

I'd rather not magically change the range.

fs/gfs2/bmap.c static int gfs2_block_zero_range(struct inode *inode, loff_t from, · line 1307

Quoted 3 lines
fs/gfs2/bmap.c +2 -0
 	BUG_ON(current->journal_info);
+	if (from + length > inode->i_size)
+		length = inode->i_size - from;
	length = min(length, inode->i_size - from);
#2 lore raw
In reply to Andreas Gruenbacher
Quoted 9 lines · Andreas Gruenbacher

Since commit eb65540aa9fc ("iomap: warn on zero range of a post-eof folio"), iomap_zero_range() warns when asked to zero a folio beyond eof. The warning triggers on the following code path:

gfs2_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)

    __gfs2_punch_hole()
      gfs2_block_zero_range()
        iomap_zero_range()
Quoted 8 lines

So far, gfs2 is just zeroing out partial pages at the beginning and end of the range, whether beyond eof or not. The data beyond eof is already expected to be all zeroes, though. Truncate the range passed to iomap_zero_range().

As an alternative approach, we could also implicitly truncate the range inside iomap_zero_range() instead of issuing a warning. Any thoughts?

Thanks Andreas. The more I think about this the more it seems like lifting this logic into iomap is a reasonable compromise between just dropping the warning and forcing individual filesystems to work around it. The original intent of the warning was to have something to catch subtle bad behavior since zero range did update i_size for so long.

OTOH I think it's reasonable to argue that we shouldn't need to warn in situations where we could just enforce correct behavior. Also, I believe we introduced something similar to avoid post-eof weirdness wrt unshare range [1], so precedent exists.

I'm interested if others have opinions on the iomap side.. (though as I write this it looks like hch sits on the side of not tweaking iomap).

Brian

[1] a311a08a4237 ("iomap: constrain the file range passed to iomap_file_unshare")

Quoted 23 lines

Thanks,
Andreas

--

fs/gfs2/bmap.c +4 -0
index b81984def58e..d9a4309cd414 100644
@@ -1301,6 +1301,10 @@ static int gfs2_block_zero_range(struct inode *inode, loff_t from,
 				 unsigned int length)
 {
 	BUG_ON(current->journal_info);
+	if (from > inode->i_size)
+		return 0;
+	if (from + length > inode->i_size)
+		length = inode->i_size - from;
 	return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops,
 			NULL);
 }
#3 lore raw
Quoted 4 lines · Brian Foster
Quoted 3 lines · Andreas Gruenbacher

Since commit eb65540aa9fc ("iomap: warn on zero range of a post-eof folio"), iomap_zero_range() warns when asked to zero a folio beyond eof. The warning triggers on the following code path:

Which warning? This one?

	/* warn about zeroing folios beyond eof that won't write back */
	WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);

If so, then why are there folios that start entirely beyond EOF?

Quoted 21 lines
Quoted 6 lines

gfs2_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)

    __gfs2_punch_hole()
      gfs2_block_zero_range()
        iomap_zero_range()
Quoted 8 lines

So far, gfs2 is just zeroing out partial pages at the beginning and end of the range, whether beyond eof or not. The data beyond eof is already expected to be all zeroes, though. Truncate the range passed to iomap_zero_range().

As an alternative approach, we could also implicitly truncate the range inside iomap_zero_range() instead of issuing a warning. Any thoughts?

Thanks Andreas. The more I think about this the more it seems like lifting this logic into iomap is a reasonable compromise between just dropping the warning and forcing individual filesystems to work around it. The original intent of the warning was to have something to catch subtle bad behavior since zero range did update i_size for so long.

Quoted 7 lines

OTOH I think it's reasonable to argue that we shouldn't need to warn in situations where we could just enforce correct behavior. Also, I believe we introduced something similar to avoid post-eof weirdness wrt unshare range [1], so precedent exists.

I'm interested if others have opinions on the iomap side.. (though as I write this it looks like hch sits on the side of not tweaking iomap).

IIRC XFS calls iomap_zero_range during file extending operations to zero the tail of a folio that spans EOF, so you'd have to allow for that too.

--D

Quoted 29 lines

Brian

[1] a311a08a4237 ("iomap: constrain the file range passed to iomap_file_unshare")

Quoted 23 lines

Thanks,
Andreas

--

fs/gfs2/bmap.c +4 -0
index b81984def58e..d9a4309cd414 100644
@@ -1301,6 +1301,10 @@ static int gfs2_block_zero_range(struct inode *inode, loff_t from,
 				 unsigned int length)
 {
 	BUG_ON(current->journal_info);
+	if (from > inode->i_size)
+		return 0;
+	if (from + length > inode->i_size)
+		length = inode->i_size - from;
 	return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops,
 			NULL);
 }
#4 lore raw
Quoted 6 lines · Darrick J. Wong
Quoted 4 lines · Brian Foster
Quoted 3 lines · Andreas Gruenbacher

Since commit eb65540aa9fc ("iomap: warn on zero range of a post-eof folio"), iomap_zero_range() warns when asked to zero a folio beyond eof. The warning triggers on the following code path:

Quoted 7 lines

Which warning? This one?

	/* warn about zeroing folios beyond eof that won't write back */
	WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);

If so, then why are there folios that start entirely beyond EOF?

Yeah.. this gfs2 instance is simply a case of their punch hole mechanism does unconditional partial folio zeroing via iomap zero range, so if a punch hole occurs on some unaligned range of post-eof blocks, it will basically create and perform zeroing of post-eof folios. IIUC the caveat here is that these blocks are all zeroed on alloc (unwritten extents are apparently not a thing in gfs2), so the punch time zeroing and warning are spurious. Andreas can correct me if I have any of that wrong.

Quoted 26 lines
Quoted 26 lines
Quoted 6 lines

gfs2_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)

    __gfs2_punch_hole()
      gfs2_block_zero_range()
        iomap_zero_range()
Quoted 8 lines

So far, gfs2 is just zeroing out partial pages at the beginning and end of the range, whether beyond eof or not. The data beyond eof is already expected to be all zeroes, though. Truncate the range passed to iomap_zero_range().

As an alternative approach, we could also implicitly truncate the range inside iomap_zero_range() instead of issuing a warning. Any thoughts?

Thanks Andreas. The more I think about this the more it seems like lifting this logic into iomap is a reasonable compromise between just dropping the warning and forcing individual filesystems to work around it. The original intent of the warning was to have something to catch subtle bad behavior since zero range did update i_size for so long.

OTOH I think it's reasonable to argue that we shouldn't need to warn in situations where we could just enforce correct behavior. Also, I believe we introduced something similar to avoid post-eof weirdness wrt unshare range [1], so precedent exists.

Quoted 6 lines
Quoted 2 lines

I'm interested if others have opinions on the iomap side.. (though as I write this it looks like hch sits on the side of not tweaking iomap).

IIRC XFS calls iomap_zero_range during file extending operations to zero the tail of a folio that spans EOF, so you'd have to allow for that too.

Yeah, good point. Perhaps we'd want to bail on a folio that starts beyond EOF with this approach, similar to the warning logic.

Brian

Quoted 32 lines

--D

Quoted 29 lines

Brian

[1] a311a08a4237 ("iomap: constrain the file range passed to iomap_file_unshare")

Quoted 23 lines

Thanks,
Andreas

--

fs/gfs2/bmap.c +4 -0
index b81984def58e..d9a4309cd414 100644
@@ -1301,6 +1301,10 @@ static int gfs2_block_zero_range(struct inode *inode, loff_t from,
 				 unsigned int length)
 {
 	BUG_ON(current->journal_info);
+	if (from > inode->i_size)
+		return 0;
+	if (from + length > inode->i_size)
+		length = inode->i_size - from;
 	return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops,
 			NULL);
 }
#5 lore raw
Quoted 15 lines · Brian Foster
Quoted 6 lines · Darrick J. Wong
Quoted 4 lines · Brian Foster
Quoted 3 lines · Andreas Gruenbacher

Since commit eb65540aa9fc ("iomap: warn on zero range of a post-eof folio"), iomap_zero_range() warns when asked to zero a folio beyond eof. The warning triggers on the following code path:

Quoted 7 lines

Which warning? This one?

	/* warn about zeroing folios beyond eof that won't write back */
	WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);

If so, then why are there folios that start entirely beyond EOF?

Quoted 7 lines

Yeah.. this gfs2 instance is simply a case of their punch hole mechanism does unconditional partial folio zeroing via iomap zero range, so if a punch hole occurs on some unaligned range of post-eof blocks, it will basically create and perform zeroing of post-eof folios. IIUC the caveat here is that these blocks are all zeroed on alloc (unwritten extents are apparently not a thing in gfs2), so the punch time zeroing and warning are spurious. Andreas can correct me if I have any of that wrong.

Oh, right, because iomap_zero_iter calls iomap_write_begin, which allocates a new folio completely beyond EOF, and then we see that new folio and WARN about it before scribbling on the folio and dirtying it. Correct?

If so then yeah, it doesn't seem useful to do that... unless the file size immediately gets extended such that at least one byte of the dirty folio is within EOF. Even then, that seems like a stretch...

Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 6 lines

gfs2_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)

    __gfs2_punch_hole()
      gfs2_block_zero_range()
        iomap_zero_range()
Quoted 8 lines

So far, gfs2 is just zeroing out partial pages at the beginning and end of the range, whether beyond eof or not. The data beyond eof is already expected to be all zeroes, though. Truncate the range passed to iomap_zero_range().

As an alternative approach, we could also implicitly truncate the range inside iomap_zero_range() instead of issuing a warning. Any thoughts?

Thanks Andreas. The more I think about this the more it seems like lifting this logic into iomap is a reasonable compromise between just dropping the warning and forcing individual filesystems to work around it. The original intent of the warning was to have something to catch subtle bad behavior since zero range did update i_size for so long.

OTOH I think it's reasonable to argue that we shouldn't need to warn in situations where we could just enforce correct behavior. Also, I believe we introduced something similar to avoid post-eof weirdness wrt unshare range [1], so precedent exists.

I'm interested if others have opinions on the iomap side.. (though as I write this it looks like hch sits on the side of not tweaking iomap).

Quoted 6 lines
Quoted 3 lines

IIRC XFS calls iomap_zero_range during file extending operations to zero the tail of a folio that spans EOF, so you'd have to allow for that too.

Yeah, good point. Perhaps we'd want to bail on a folio that starts beyond EOF with this approach, similar to the warning logic.

...because I don't see much use in zeroing and dirtying a folio that starts well beyond EOF since iomap_writepage_handle_eof will ignore it and there are several gigantic comments in buffered-io.c about clamping to EOF.

<shrug> But maybe I'm missing a usecase?

--D

Quoted 36 lines

Brian

Quoted 32 lines

--D

Quoted 29 lines

Brian

[1] a311a08a4237 ("iomap: constrain the file range passed to iomap_file_unshare")

Quoted 23 lines

Thanks,
Andreas

--

fs/gfs2/bmap.c +4 -0
index b81984def58e..d9a4309cd414 100644
@@ -1301,6 +1301,10 @@ static int gfs2_block_zero_range(struct inode *inode, loff_t from,
 				 unsigned int length)
 {
 	BUG_ON(current->journal_info);
+	if (from > inode->i_size)
+		return 0;
+	if (from + length > inode->i_size)
+		length = inode->i_size - from;
 	return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops,
 			NULL);
 }
#6 lore raw
Quoted 24 lines · Darrick J. Wong
Quoted 15 lines · Brian Foster
Quoted 6 lines · Darrick J. Wong
Quoted 4 lines · Brian Foster
Quoted 3 lines · Andreas Gruenbacher

Since commit eb65540aa9fc ("iomap: warn on zero range of a post-eof folio"), iomap_zero_range() warns when asked to zero a folio beyond eof. The warning triggers on the following code path:

Quoted 7 lines

Which warning? This one?

	/* warn about zeroing folios beyond eof that won't write back */
	WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);

If so, then why are there folios that start entirely beyond EOF?

Quoted 7 lines

Yeah.. this gfs2 instance is simply a case of their punch hole mechanism does unconditional partial folio zeroing via iomap zero range, so if a punch hole occurs on some unaligned range of post-eof blocks, it will basically create and perform zeroing of post-eof folios. IIUC the caveat here is that these blocks are all zeroed on alloc (unwritten extents are apparently not a thing in gfs2), so the punch time zeroing and warning are spurious. Andreas can correct me if I have any of that wrong.

Quoted 9 lines

Oh, right, because iomap_zero_iter calls iomap_write_begin, which allocates a new folio completely beyond EOF, and then we see that new folio and WARN about it before scribbling on the folio and dirtying it. Correct?

If so then yeah, it doesn't seem useful to do that... unless the file size immediately gets extended such that at least one byte of the dirty folio is within EOF. Even then, that seems like a stretch...

Yep, agreed. An i_size update after a zero op would come after locks are dropped and whatnot as well, so seems racy and wrong.

Quoted 36 lines
Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 6 lines

gfs2_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)

    __gfs2_punch_hole()
      gfs2_block_zero_range()
        iomap_zero_range()
Quoted 8 lines

So far, gfs2 is just zeroing out partial pages at the beginning and end of the range, whether beyond eof or not. The data beyond eof is already expected to be all zeroes, though. Truncate the range passed to iomap_zero_range().

As an alternative approach, we could also implicitly truncate the range inside iomap_zero_range() instead of issuing a warning. Any thoughts?

Thanks Andreas. The more I think about this the more it seems like lifting this logic into iomap is a reasonable compromise between just dropping the warning and forcing individual filesystems to work around it. The original intent of the warning was to have something to catch subtle bad behavior since zero range did update i_size for so long.

OTOH I think it's reasonable to argue that we shouldn't need to warn in situations where we could just enforce correct behavior. Also, I believe we introduced something similar to avoid post-eof weirdness wrt unshare range [1], so precedent exists.

I'm interested if others have opinions on the iomap side.. (though as I write this it looks like hch sits on the side of not tweaking iomap).

Quoted 6 lines
Quoted 3 lines

IIRC XFS calls iomap_zero_range during file extending operations to zero the tail of a folio that spans EOF, so you'd have to allow for that too.

Yeah, good point. Perhaps we'd want to bail on a folio that starts beyond EOF with this approach, similar to the warning logic.

Quoted 7 lines

...because I don't see much use in zeroing and dirtying a folio that starts well beyond EOF since iomap_writepage_handle_eof will ignore it and there are several gigantic comments in buffered-io.c about clamping to EOF.

<shrug> But maybe I'm missing a usecase?

Not that I'm aware of.

Brian

Quoted 39 lines

--D

Quoted 36 lines

Brian

Quoted 32 lines

--D

Quoted 29 lines

Brian

[1] a311a08a4237 ("iomap: constrain the file range passed to iomap_file_unshare")

Quoted 23 lines

Thanks,
Andreas

--

fs/gfs2/bmap.c +4 -0
index b81984def58e..d9a4309cd414 100644
@@ -1301,6 +1301,10 @@ static int gfs2_block_zero_range(struct inode *inode, loff_t from,
 				 unsigned int length)
 {
 	BUG_ON(current->journal_info);
+	if (from > inode->i_size)
+		return 0;
+	if (from + length > inode->i_size)
+		length = inode->i_size - from;
 	return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops,
 			NULL);
 }
author maintainer
#7 lore raw
In reply to Darrick J. Wong
Quoted 16 lines · Darrick J. Wong
Quoted 15 lines · Brian Foster
Quoted 13 lines · Darrick J. Wong
Quoted 4 lines · Brian Foster
Quoted 3 lines · Andreas Gruenbacher

Since commit eb65540aa9fc ("iomap: warn on zero range of a post-eof folio"), iomap_zero_range() warns when asked to zero a folio beyond eof. The warning triggers on the following code path:

Which warning? This one?

    /* warn about zeroing folios beyond eof that won't write back */
    WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);

If so, then why are there folios that start entirely beyond EOF?

Quoted 7 lines
Quoted 7 lines

Yeah.. this gfs2 instance is simply a case of their punch hole mechanism does unconditional partial folio zeroing via iomap zero range, so if a punch hole occurs on some unaligned range of post-eof blocks, it will basically create and perform zeroing of post-eof folios. IIUC the caveat here is that these blocks are all zeroed on alloc (unwritten extents are apparently not a thing in gfs2), so the punch time zeroing and warning are spurious. Andreas can correct me if I have any of that wrong.

gfs2 uses ext2-style indirect blocks. It doesn't have extents, delayed allocation, or any kind of unwritten data tracking.

Quoted 4 lines

Oh, right, because iomap_zero_iter calls iomap_write_begin, which allocates a new folio completely beyond EOF, and then we see that new folio and WARN about it before scribbling on the folio and dirtying it. Correct?

Yes, or at least that's also my understanding.

Quoted 3 lines

If so then yeah, it doesn't seem useful to do that... unless the file size immediately gets extended such that at least one byte of the dirty folio is within EOF. Even then, that seems like a stretch...

i_size isn't going to change in a punch hole operation:

int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)

{
        [...]
        switch (mode & FALLOC_FL_MODE_MASK) {
        [...]
        case FALLOC_FL_PUNCH_HOLE:
                if (!(mode & FALLOC_FL_KEEP_SIZE))
                        return -EOPNOTSUPP;
Quoted 35 lines
Quoted 28 lines
Quoted 28 lines
Quoted 27 lines
Quoted 5 lines

gfs2_fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)

    __gfs2_punch_hole()
      gfs2_block_zero_range()
        iomap_zero_range()
Quoted 8 lines

So far, gfs2 is just zeroing out partial pages at the beginning and end of the range, whether beyond eof or not. The data beyond eof is already expected to be all zeroes, though. Truncate the range passed to iomap_zero_range().

As an alternative approach, we could also implicitly truncate the range inside iomap_zero_range() instead of issuing a warning. Any thoughts?

Thanks Andreas. The more I think about this the more it seems like lifting this logic into iomap is a reasonable compromise between just dropping the warning and forcing individual filesystems to work around it. The original intent of the warning was to have something to catch subtle bad behavior since zero range did update i_size for so long.

OTOH I think it's reasonable to argue that we shouldn't need to warn in situations where we could just enforce correct behavior. Also, I believe we introduced something similar to avoid post-eof weirdness wrt unshare range [1], so precedent exists.

I'm interested if others have opinions on the iomap side.. (though as I write this it looks like hch sits on the side of not tweaking iomap).

Quoted 6 lines
Quoted 3 lines

IIRC XFS calls iomap_zero_range during file extending operations to zero the tail of a folio that spans EOF, so you'd have to allow for that too.

Yeah, good point. Perhaps we'd want to bail on a folio that starts beyond EOF with this approach, similar to the warning logic.

Quoted 6 lines

...because I don't see much use in zeroing and dirtying a folio that starts well beyond EOF since iomap_writepage_handle_eof will ignore it and there are several gigantic comments in buffered-io.c about clamping to EOF.

<shrug> But maybe I'm missing a usecase?

Unrelated to iomap_zero_range(), but gfs2 has this fairly scary special case in which it does insist on writing folios beyond eof. See gfs2_write_jdata_folio(); this was introduced and is described in commit fd4c5748b8d3 ("gfs2: writeout truncated pages").

Quoted 39 lines

--D

Quoted 36 lines

Brian

Quoted 32 lines

--D

Quoted 29 lines

Brian

[1] a311a08a4237 ("iomap: constrain the file range passed to iomap_file_unshare")

Quoted 23 lines

Thanks,
Andreas

--

fs/gfs2/bmap.c +4 -0
index b81984def58e..d9a4309cd414 100644
@@ -1301,6 +1301,10 @@ static int gfs2_block_zero_range(struct inode *inode, loff_t from,
                                 unsigned int length)
 {
        BUG_ON(current->journal_info);
+       if (from > inode->i_size)
+               return 0;
+       if (from + length > inode->i_size)
+               length = inode->i_size - from;
        return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops,
                        NULL);
 }

Thanks,
Andreas