Commit 3d38f77c authored by Dave Chinner's avatar Dave Chinner Committed by Long Li
Browse files

xfs: fix off-by-one-block in xfs_discard_folio()

mainline inclusion
from mainline-v6.3-rc1
commit 8ac5b996
category: bugfix
bugzilla: 189079, https://gitee.com/openeuler/kernel/issues/I76JSK
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8ac5b996bf5199f15b7687ceae989f8b2a410dda

--------------------------------

The recent writeback corruption fixes changed the code in
xfs_discard_folio() to calculate a byte range to for punching
delalloc extents. A mistake was made in using round_up(pos) for the
end offset, because when pos points at the first byte of a block, it
does not get rounded up to point to the end byte of the block. hence
the punch range is short, and this leads to unexpected behaviour in
certain cases in xfs_bmap_punch_delalloc_range.

e.g. pos = 0 means we call xfs_bmap_punch_delalloc_range(0,0), so
there is no previous extent and it rounds up the punch to the end of
the delalloc extent it found at offset 0, not the end of the range
given to xfs_bmap_punch_delalloc_range().

Fix this by handling the zero block offset case correctly.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=217030
Link: https://lore.kernel.org/linux-xfs/Y+vOfaxIWX1c%2Fyy9@bfoster/


Fixes: 7348b322 ("xfs: xfs_bmap_punch_delalloc_range() should take a byte range")
Reported-by: default avatarPengfei Xu <pengfei.xu@intel.com>
Found-by: default avatarBrian Foster <bfoster@redhat.com>
Signed-off-by: default avatarDave Chinner <dchinner@redhat.com>
Reviewed-by: default avatarDarrick J. Wong <djwong@kernel.org>
Signed-off-by: default avatarDarrick J. Wong <djwong@kernel.org>

Conflicts:
	fs/xfs/xfs_aops.c

Signed-off-by: default avatarLong Li <leo.lilong@huawei.com>
parent f76b635e
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -500,15 +500,17 @@ xfs_prepare_ioend(
}

/*
 * If the page has delalloc blocks on it, we need to punch them out before we
 * invalidate the page.  If we don't, we leave a stale delalloc mapping on the
 * inode that can trip up a later direct I/O read operation on the same region.
 * If the folio has delalloc blocks on it, the caller is asking us to punch them
 * out. If we don't, we can leave a stale delalloc mapping covered by a clean
 * page that needs to be dirtied again before the delalloc mapping can be
 * converted. This stale delalloc mapping can trip up a later direct I/O read
 * operation on the same region.
 *
 * We prevent this by truncating away the delalloc regions on the page.  Because
 * they are delalloc, we can do this without needing a transaction. Indeed - if
 * we get ENOSPC errors, we have to be able to do this truncation without a
 * transaction as there is no space left for block reservation (typically why we
 * see a ENOSPC in writeback).
 * transaction as there is no space left for block reservation (typically why
 * we see a ENOSPC in writeback).
 */
static void
xfs_discard_page(
@@ -526,8 +528,13 @@ xfs_discard_page(
		"page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
			page, ip->i_ino, fileoff);

	/*
	 * The end of the punch range is always the offset of the the first
	 * byte of the next page. Hence the end offset is only dependent on the
	 * page itself and not the start offset that is passed in.
	 */
	error = xfs_bmap_punch_delalloc_range(ip, fileoff,
			round_up(fileoff, thp_size(page)));
			page_offset(page) + thp_size(page));
	if (error && !xfs_is_shutdown(mp))
		xfs_alert(mp, "page discard unable to remove delalloc mapping.");
}