Skip to contentSkip to conversation

xfs-4.18: various fixes

PATCH In review Darrick J. Wong 3 participants 31 messages Jun 21, 2018 – Jun 23, 2018 XFS filesystem

lore
Share & tools
lore
#0 lore raw

cover xfs-4.18: various fixes

Hi all,

Here's a series of patches to fix some problems I noticed in 4.18.

The first patch fixes a problem where we WARN_ON if someone tries to create an empty transaction while the fs is frozen. Since these transactions can't modify anything, it seems unnecessary to warn about them.

The second patch fixes per-ag rmapbt reservation accounting problems. The on-disk fdblocks counter is not decremented for rmapbt blocks, so the incore reservation should always decrement the incore fdblocks by the entire reservation regardless of rmapbt use. This fixes the problem where b_avail as reported by statfs changes across an unmount/mount cycle if the rmapbt had grown prior to the unmount, even though the number of blocks in use has not changed at all.

The third patch fixes the userspace-configurable default reserved blocks calculation when there are fewer fdblocks than what we set aside to avoid deadlocks.

The fourth patch fixes a problem Zorro Lang found where on a 512b block filesystem we can cause an extent at s_maxbytes to be right shifted to the point that it block offset exceeds the field size of 54 bytes. generic/485 is the regression test.

The fifth patch fixes a hard to trigger TOCTOU bug in generic/166 where we take ILOCK_SHARED in preparation for a directio write but by the time we get the lock a second thread has set the reflink flag on the file and shared the extent we're trying to write but we proceed into the COW allocation paths without the required ILOCK_EXCL.

The sixth patch fixes an uninitialized field in the rtbitmap fsmap code.

The seventh pach fixes an off-by-one error in the rtbitmap range query function (which fortunately only affects the rtbitmap fsmap results).

The eighth patch fixes a bug reported by fsx in generic/127 wherein if we write a file to a non-page-aligned length, mmap read the EOF page, truncate the file to a smaller non-page-aligned length, run zero_range out to EOF, and then mmap read the EOF page a second time, we briefly see non-zero bytes after EOF.

Comments and questions are, as always, welcome.

--D

#1 lore raw

PATCH 1/8 xfs: allow empty transactions while frozen

From: Darrick J. Wong <[email protected]>

In commit e89c041338ed6ef ("xfs: implement the GETFSMAP ioctl") we created the ability to obtain empty transactions. These transactions have no log or block reservations and therefore can't modify anything. Since they're also NO_WRITECOUNT they can run while the fs is frozen, so we don't need to WARN_ON about that usage.

---
 fs/xfs/xfs_trans.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/xfs/xfs_trans.c +6 -1
index e040af120b69..524f543c5b82 100644
@@ -258,7 +258,12 @@ xfs_trans_alloc(
 	if (!(flags & XFS_TRANS_NO_WRITECOUNT))
 		sb_start_intwrite(mp->m_super);
 
-	WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
+	/*
+	 * Zero-reservation ("empty") transactions can't modify anything, so
+	 * they're allowed to run while we're frozen.
+	 */
+	WARN_ON(resp->tr_logres > 0 &&
+		mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
 	atomic_inc(&mp->m_active_trans);
 
 	tp = kmem_zone_zalloc(xfs_trans_zone,
Reviewed-by on v1 1/8
#2 lore raw

Re: [PATCH 1/8] xfs: allow empty transactions while frozen

Looks fine

Quoted 38 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In commit e89c041338ed6ef ("xfs: implement the GETFSMAP ioctl") we created the ability to obtain empty transactions. These transactions have no log or block reservations and therefore can't modify anything. Since they're also NO_WRITECOUNT they can run while the fs is frozen, so we don't need to WARN_ON about that usage.

---
  fs/xfs/xfs_trans.c |    7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
fs/xfs/xfs_trans.c +6 -2
index e040af120b69..524f543c5b82 100644
@@ -258,7 +258,12 @@ xfs_trans_alloc(
  	if (!(flags & XFS_TRANS_NO_WRITECOUNT))
  		sb_start_intwrite(mp->m_super);
 
-	WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
+	/*
+	 * Zero-reservation ("empty") transactions can't modify anything, so
+	 * they're allowed to run while we're frozen.
+	 */
+	WARN_ON(resp->tr_logres > 0 &&
+		mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE);
  	atomic_inc(&mp->m_active_trans);
 
  	tp = kmem_zone_zalloc(xfs_trans_zone,
 
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at https://urldefense.proofpoint.com/v2/url?u=http-3A__vger.kernel.org_majordomo-2Dinfo.html&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=LHZQ8fHvy6wDKXGTWcm97burZH5sQKHRDMaY1UthQxc&m=jfRT1oYtPVNVBt-lMSR8ZrPgEPjfv7foQ0meuw0oGxM&s=GsacuLUdUkUoZWMAAmlRTyyR7FByN9OA43H6dYDIK6Q&e=

Reviewed-by on v1 1/8
#3 lore raw

Re: [PATCH 1/8] xfs: allow empty transactions while frozen

In reply to Darrick J. Wong
Quoted 4 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In commit e89c041338ed6ef ("xfs: implement the GETFSMAP ioctl") we created the ability to obtain empty transactions.

That was sneaked in nicely, normally it should be a separate commit..

Quoted 4 lines

These transactions
have no log or block reservations and therefore can't modify anything. Since they're also NO_WRITECOUNT they can run while the fs is frozen, so we don't need to WARN_ON about that usage.

This looks correct, but it really makes me hate these empty transactions. We're going to find more and more issues like this and will end up with a huge bag of special cases.

Reluctantly:

#4 lore raw

PATCH 2/8 xfs: fix fdblocks accounting w/ RMAPBT per-AG reservation

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In __xfs_ag_resv_init we incorrectly calculate the amount by which to decrease fdblocks when reserving blocks for the rmapbt. Because rmapbt allocations do not decrease fdblocks, we must decrease fdblocks by the entire size of the requested reservation in order to achieve our goal of always having enough free blocks to satisfy an rmapbt expansion.

This is in contrast to the refcountbt/finobt, which /do/ subtract from fdblocks whenever they allocate a block. For this allocation type we preserve the existing behavior where we decrease fdblocks only by the requested reservation minus the size of the existing tree.

This fixes the problem where the available block counts reported by statfs change across a remount if there had been an rmapbt size change since mount time.

---
 fs/xfs/libxfs/xfs_ag_resv.c |   31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
fs/xfs/libxfs/xfs_ag_resv.c +27 -4
index 84db76e0e3e3..fecd187fcf2c 100644
@@ -157,6 +157,7 @@ __xfs_ag_resv_free(
 	error = xfs_mod_fdblocks(pag->pag_mount, oldresv, true);
 	resv->ar_reserved = 0;
 	resv->ar_asked = 0;
+	resv->ar_orig_reserved = 0;
 
 	if (error)
 		trace_xfs_ag_resv_free_error(pag->pag_mount, pag->pag_agno,
@@ -189,13 +190,34 @@ __xfs_ag_resv_init(
 	struct xfs_mount		*mp = pag->pag_mount;
 	struct xfs_ag_resv		*resv;
 	int				error;
-	xfs_extlen_t			reserved;
+	xfs_extlen_t			hidden_space;
 
 	if (used > ask)
 		ask = used;
-	reserved = ask - used;
 
-	error = xfs_mod_fdblocks(mp, -(int64_t)reserved, true);
+	switch (type) {
+	case XFS_AG_RESV_RMAPBT:
+		/*
+		 * Space taken by the rmapbt is not subtracted from fdblocks
+		 * because the rmapbt lives in the free space.  Here we must
+		 * subtract the entire reservation from fdblocks so that we
+		 * always have blocks available for rmapbt expansion.
+		 */
+		hidden_space = ask;
+		break;
+	case XFS_AG_RESV_METADATA:
+		/*
+		 * Space taken by all other metadata btrees are accounted
+		 * on-disk as used space.  We therefore only hide the space
+		 * that is reserved but not used by the trees.
+		 */
+		hidden_space = ask - used;
+		break;
+	default:
+		ASSERT(0);
+		return -EINVAL;
+	}
+	error = xfs_mod_fdblocks(mp, -(int64_t)hidden_space, true);
 	if (error) {
 		trace_xfs_ag_resv_init_error(pag->pag_mount, pag->pag_agno,
 				error, _RET_IP_);
@@ -216,7 +238,8 @@ __xfs_ag_resv_init(
 
 	resv = xfs_perag_resv(pag, type);
 	resv->ar_asked = ask;
-	resv->ar_reserved = resv->ar_orig_reserved = reserved;
+	resv->ar_orig_reserved = hidden_space;
+	resv->ar_reserved = ask - used;
 
 	trace_xfs_ag_resv_init(pag, type, ask);
 	return 0;
Reviewed-by on v1 2/8
#5 lore raw

Re: [PATCH 2/8] xfs: fix fdblocks accounting w/ RMAPBT per-AG reservation

Alrighty, thx for the comments

Quoted 89 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In __xfs_ag_resv_init we incorrectly calculate the amount by which to decrease fdblocks when reserving blocks for the rmapbt. Because rmapbt allocations do not decrease fdblocks, we must decrease fdblocks by the entire size of the requested reservation in order to achieve our goal of always having enough free blocks to satisfy an rmapbt expansion.

This is in contrast to the refcountbt/finobt, which /do/ subtract from fdblocks whenever they allocate a block. For this allocation type we preserve the existing behavior where we decrease fdblocks only by the requested reservation minus the size of the existing tree.

This fixes the problem where the available block counts reported by statfs change across a remount if there had been an rmapbt size change since mount time.

---
  fs/xfs/libxfs/xfs_ag_resv.c |   31 +++++++++++++++++++++++++++----
  1 file changed, 27 insertions(+), 4 deletions(-)
fs/xfs/libxfs/xfs_ag_resv.c +27 -5
index 84db76e0e3e3..fecd187fcf2c 100644
@@ -157,6 +157,7 @@ __xfs_ag_resv_free(
  	error = xfs_mod_fdblocks(pag->pag_mount, oldresv, true);
  	resv->ar_reserved = 0;
  	resv->ar_asked = 0;
+	resv->ar_orig_reserved = 0;
 
  	if (error)
  		trace_xfs_ag_resv_free_error(pag->pag_mount, pag->pag_agno,
@@ -189,13 +190,34 @@ __xfs_ag_resv_init(
  	struct xfs_mount		*mp = pag->pag_mount;
  	struct xfs_ag_resv		*resv;
  	int				error;
-	xfs_extlen_t			reserved;
+	xfs_extlen_t			hidden_space;
 
  	if (used > ask)
  		ask = used;
-	reserved = ask - used;
 
-	error = xfs_mod_fdblocks(mp, -(int64_t)reserved, true);
+	switch (type) {
+	case XFS_AG_RESV_RMAPBT:
+		/*
+		 * Space taken by the rmapbt is not subtracted from fdblocks
+		 * because the rmapbt lives in the free space.  Here we must
+		 * subtract the entire reservation from fdblocks so that we
+		 * always have blocks available for rmapbt expansion.
+		 */
+		hidden_space = ask;
+		break;
+	case XFS_AG_RESV_METADATA:
+		/*
+		 * Space taken by all other metadata btrees are accounted
+		 * on-disk as used space.  We therefore only hide the space
+		 * that is reserved but not used by the trees.
+		 */
+		hidden_space = ask - used;
+		break;
+	default:
+		ASSERT(0);
+		return -EINVAL;
+	}
+	error = xfs_mod_fdblocks(mp, -(int64_t)hidden_space, true);
  	if (error) {
  		trace_xfs_ag_resv_init_error(pag->pag_mount, pag->pag_agno,
  				error, _RET_IP_);
@@ -216,7 +238,8 @@ __xfs_ag_resv_init(
 
  	resv = xfs_perag_resv(pag, type);
  	resv->ar_asked = ask;
-	resv->ar_reserved = resv->ar_orig_reserved = reserved;
+	resv->ar_orig_reserved = hidden_space;
+	resv->ar_reserved = ask - used;
 
  	trace_xfs_ag_resv_init(pag, type, ask);
  	return 0;
 
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at https://urldefense.proofpoint.com/v2/url?u=http-3A__vger.kernel.org_majordomo-2Dinfo.html&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=LHZQ8fHvy6wDKXGTWcm97burZH5sQKHRDMaY1UthQxc&m=57YhFRxyRqjdPfAPfs4wnqA9n455Z75z7qYPx5N5g5A&s=xlMBoLgUrvEbqMKbDc72qnV-gJsBNzbR1VFqvVF9XKg&e=

#6 lore raw

PATCH 3/8 xfs: don't trip over negative free space in xfs_reserve_blocks

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If we somehow end up with a filesystem that has fewer free blocks than the blocks set aside to avoid ENOSPC deadlocks, it's possible that the free space calculation in xfs_reserve_blocks will spit out a negative number (because percpu_counter_sum returns s64). We fail to notice this negative number and set fdblks_delta to it. Now we increment fdblocks(!) and the unsigned type of m_resblks means that we end up setting a ridiculously huge m_resblks reservation.

Avoid this comedy of errors by detecting the negative free space and returning -ENOSPC.

---
 fs/xfs/xfs_fsops.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/xfs_fsops.c +1 -1
index a7afcad6b711..3f2bd6032cf8 100644
@@ -387,7 +387,7 @@ xfs_reserve_blocks(
 	do {
 		free = percpu_counter_sum(&mp->m_fdblocks) -
 						mp->m_alloc_set_aside;
-		if (!free)
+		if (free <= 0)
 			break;
 
 		delta = request - mp->m_resblks;
Reviewed-by on v1 3/8
#7 lore raw

Re: [PATCH 3/8] xfs: don't trip over negative free space in xfs_reserve_blocks

Quoted 10 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If we somehow end up with a filesystem that has fewer free blocks than the blocks set aside to avoid ENOSPC deadlocks, it's possible that the free space calculation in xfs_reserve_blocks will spit out a negative number (because percpu_counter_sum returns s64). We fail to notice this negative number and set fdblks_delta to it. Now we increment fdblocks(!) and the unsigned type of m_resblks means that we end up setting a ridiculously huge m_resblks reservation.

Quoted 4 lines

Avoid this comedy of errors by detecting the negative free space and returning -ENOSPC.

Looks good,

Reviewed-by on v1 3/8
#8 lore raw

Re: [PATCH 3/8] xfs: don't trip over negative free space in xfs_reserve_blocks

In reply to Darrick J. Wong

Looks ok, thx!

Quoted 38 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If we somehow end up with a filesystem that has fewer free blocks than the blocks set aside to avoid ENOSPC deadlocks, it's possible that the free space calculation in xfs_reserve_blocks will spit out a negative number (because percpu_counter_sum returns s64). We fail to notice this negative number and set fdblks_delta to it. Now we increment fdblocks(!) and the unsigned type of m_resblks means that we end up setting a ridiculously huge m_resblks reservation.

Avoid this comedy of errors by detecting the negative free space and returning -ENOSPC.

---
  fs/xfs/xfs_fsops.c |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/xfs_fsops.c +1 -2
index a7afcad6b711..3f2bd6032cf8 100644
@@ -387,7 +387,7 @@ xfs_reserve_blocks(
  	do {
  		free = percpu_counter_sum(&mp->m_fdblocks) -
  						mp->m_alloc_set_aside;
-		if (!free)
+		if (free <= 0)
  			break;
 
  		delta = request - mp->m_resblks;
 
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at https://urldefense.proofpoint.com/v2/url?u=http-3A__vger.kernel.org_majordomo-2Dinfo.html&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=LHZQ8fHvy6wDKXGTWcm97burZH5sQKHRDMaY1UthQxc&m=N6x3Cwn-CGLBE6LOOAbcetDshZ_SB_Mnv0Jn2FdPq38&s=CizYCC09hHj0NL-62QgWA-Tla03lpYgbx5ytFsHSFx0&e=

#9 lore raw

PATCH 4/8 xfs: don't allow insert-range to shift extents past the maximum offset

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Zorro Lang reports that generic/485 blows an assert on a filesystem with 512 byte blocks. The test tries to fallocate a post-eof extent at the maximum file size and calls insert range to shift the extents right by two blocks. On a 512b block filesystem this causes startoff to overflow the 54-bit startoff field, leading to the assert.

Therefore, always check the rightmost extent to see if it would overflow prior to invoking the insert range machinery.

Reported-by [email protected]

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=200137

---
 fs/xfs/libxfs/xfs_bmap.c |   40 ++++++++++++++++++++++++++++++++++++++++
 fs/xfs/libxfs/xfs_bmap.h |    2 ++
 fs/xfs/xfs_bmap_util.c   |    4 ++++
 3 files changed, 46 insertions(+)
fs/xfs/libxfs/xfs_bmap.c +40 -0
index 01628f0c9a0c..b7f094e19bab 100644
@@ -5780,6 +5780,46 @@ xfs_bmap_collapse_extents(
 	return error;
 }
 
+#define BMBT_STARTOFF_MASK	((1ULL << BMBT_STARTOFF_BITLEN) - 1)
+/* Make sure we won't be right-shifting an extent past the maximum bound. */
+int
+xfs_bmap_can_insert_extents(
+	struct xfs_inode	*ip,
+	xfs_fileoff_t		off,
+	xfs_fileoff_t		shift)
+{
+	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
+	struct xfs_bmbt_irec	got;
+	struct xfs_iext_cursor	icur;
+	xfs_fileoff_t		new_off;
+	int			error = 0;
+
+	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
+		return -EIO;
+
+	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
+	xfs_ilock(ip, XFS_ILOCK_EXCL);
+
+	if (!(ifp->if_flags & XFS_IFEXTENTS)) {
+		error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
+		if (error)
+			goto out;
+	}
+
+	xfs_iext_last(ifp, &icur);
+	if (!xfs_iext_get_extent(ifp, &icur, &got) || off > got.br_startoff)
+		goto out;
+
+	/* Make sure we can't overflow the 54-bit offset field. */
+	new_off = (got.br_startoff + shift) & BMBT_STARTOFF_MASK;
+	if (new_off < got.br_startoff)
+		error = -EINVAL;
+
+out:
+	xfs_iunlock(ip, XFS_ILOCK_EXCL);
+	return error;
+}
+
 int
 xfs_bmap_insert_extents(
 	struct xfs_trans	*tp,
fs/xfs/libxfs/xfs_bmap.h +2 -0
index 99dddbd0fcc6..9b49ddf99c41 100644
@@ -227,6 +227,8 @@ int	xfs_bmap_collapse_extents(struct xfs_trans *tp, struct xfs_inode *ip,
 		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
 		bool *done, xfs_fsblock_t *firstblock,
 		struct xfs_defer_ops *dfops);
+int	xfs_bmap_can_insert_extents(struct xfs_inode *ip, xfs_fileoff_t off,
+		xfs_fileoff_t shift);
 int	xfs_bmap_insert_extents(struct xfs_trans *tp, struct xfs_inode *ip,
 		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
 		bool *done, xfs_fileoff_t stop_fsb, xfs_fsblock_t *firstblock,
fs/xfs/xfs_bmap_util.c +4 -0
index c35009a86699..abc37b0899c0 100644
@@ -1404,6 +1404,10 @@ xfs_insert_file_space(
 
 	trace_xfs_insert_file_space(ip);
 
+	error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
+	if (error)
+		return error;
+
 	error = xfs_prepare_shift(ip, offset);
 	if (error)
 		return error;
Reviewed-by on v1 4/8
#10 lore raw

Re: [PATCH 4/8] xfs: don't allow insert-range to shift extents past the maximum offset

Ok, you can add my review

Quoted 106 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Zorro Lang reports that generic/485 blows an assert on a filesystem with 512 byte blocks. The test tries to fallocate a post-eof extent at the maximum file size and calls insert range to shift the extents right by two blocks. On a 512b block filesystem this causes startoff to overflow the 54-bit startoff field, leading to the assert.

Therefore, always check the rightmost extent to see if it would overflow prior to invoking the insert range machinery.

Reported-by [email protected]

Bugzilla: https://urldefense.proofpoint.com/v2/url?u=https-3A__bugzilla.kernel.org_show-5Fbug.cgi-3Fid-3D200137&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=LHZQ8fHvy6wDKXGTWcm97burZH5sQKHRDMaY1UthQxc&m=YCaDC-F1dLxMWur8rwN6LkUZEkB0mCLT1kaX18afe3Y&s=21OAUtb7E_eZcNMI0iepr7J9fg90RHPo0hUJBgJxDrI&e=

---
  fs/xfs/libxfs/xfs_bmap.c |   40 ++++++++++++++++++++++++++++++++++++++++
  fs/xfs/libxfs/xfs_bmap.h |    2 ++
  fs/xfs/xfs_bmap_util.c   |    4 ++++
  3 files changed, 46 insertions(+)
fs/xfs/libxfs/xfs_bmap.c +40 -0
index 01628f0c9a0c..b7f094e19bab 100644
@@ -5780,6 +5780,46 @@ xfs_bmap_collapse_extents(
  	return error;
  }
 
+#define BMBT_STARTOFF_MASK	((1ULL << BMBT_STARTOFF_BITLEN) - 1)
+/* Make sure we won't be right-shifting an extent past the maximum bound. */
+int
+xfs_bmap_can_insert_extents(
+	struct xfs_inode	*ip,
+	xfs_fileoff_t		off,
+	xfs_fileoff_t		shift)
+{
+	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
+	struct xfs_bmbt_irec	got;
+	struct xfs_iext_cursor	icur;
+	xfs_fileoff_t		new_off;
+	int			error = 0;
+
+	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
+		return -EIO;
+
+	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
+	xfs_ilock(ip, XFS_ILOCK_EXCL);
+
+	if (!(ifp->if_flags & XFS_IFEXTENTS)) {
+		error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
+		if (error)
+			goto out;
+	}
+
+	xfs_iext_last(ifp, &icur);
+	if (!xfs_iext_get_extent(ifp, &icur, &got) || off > got.br_startoff)
+		goto out;
+
+	/* Make sure we can't overflow the 54-bit offset field. */
+	new_off = (got.br_startoff + shift) & BMBT_STARTOFF_MASK;
+	if (new_off < got.br_startoff)
+		error = -EINVAL;
+
+out:
+	xfs_iunlock(ip, XFS_ILOCK_EXCL);
+	return error;
+}
+
  int
  xfs_bmap_insert_extents(
  	struct xfs_trans	*tp,
fs/xfs/libxfs/xfs_bmap.h +2 -0
index 99dddbd0fcc6..9b49ddf99c41 100644
@@ -227,6 +227,8 @@ int	xfs_bmap_collapse_extents(struct xfs_trans *tp, struct xfs_inode *ip,
  		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
  		bool *done, xfs_fsblock_t *firstblock,
  		struct xfs_defer_ops *dfops);
+int	xfs_bmap_can_insert_extents(struct xfs_inode *ip, xfs_fileoff_t off,
+		xfs_fileoff_t shift);
  int	xfs_bmap_insert_extents(struct xfs_trans *tp, struct xfs_inode *ip,
  		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
  		bool *done, xfs_fileoff_t stop_fsb, xfs_fsblock_t *firstblock,
fs/xfs/xfs_bmap_util.c +4 -1
index c35009a86699..abc37b0899c0 100644
@@ -1404,6 +1404,10 @@ xfs_insert_file_space(
 
  	trace_xfs_insert_file_space(ip);
 
+	error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
+	if (error)
+		return error;
+
  	error = xfs_prepare_shift(ip, offset);
  	if (error)
  		return error;
 
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at https://urldefense.proofpoint.com/v2/url?u=http-3A__vger.kernel.org_majordomo-2Dinfo.html&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=LHZQ8fHvy6wDKXGTWcm97burZH5sQKHRDMaY1UthQxc&m=YCaDC-F1dLxMWur8rwN6LkUZEkB0mCLT1kaX18afe3Y&s=RhleurH5wYku58VywlbdBFmjaXSEGIfX7SCThJq1clE&e=

#11 lore raw

Re: [PATCH 4/8] xfs: don't allow insert-range to shift extents past the maximum offset

In reply to Darrick J. Wong
Quoted 1 line

+#define BMBT_STARTOFF_MASK ((1ULL << BMBT_STARTOFF_BITLEN) - 1)

Please move this next to BMBT_STARTOFF_BITLEN.

For brownie points also use this new defined instead of XFS_IEXT_STARTOFF_MASK in the extent tree code.

Quoted 18 lines
fs/xfs/libxfs/xfs_bmap.c +18 -0
+/* Make sure we won't be right-shifting an extent past the maximum bound. */
+int
+xfs_bmap_can_insert_extents(
+	struct xfs_inode	*ip,
+	xfs_fileoff_t		off,
+	xfs_fileoff_t		shift)
+{
+	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
+	struct xfs_bmbt_irec	got;
+	struct xfs_iext_cursor	icur;
+	xfs_fileoff_t		new_off;
+	int			error = 0;
+
+	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
+		return -EIO;
+
+	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
+	xfs_ilock(ip, XFS_ILOCK_EXCL);

fs/xfs/libxfs/xfs_bmap.c xfs_bmap_collapse_extents( · line 5811

Quoted 10 lines
fs/xfs/libxfs/xfs_bmap.c +10 -0
+
+	if (!(ifp->if_flags & XFS_IFEXTENTS)) {
+		error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
+		if (error)
+			goto out;
+	}
+
+	xfs_iext_last(ifp, &icur);
+	if (!xfs_iext_get_extent(ifp, &icur, &got) || off > got.br_startoff)
+		goto out;

This largely duplicates xfs_bmap_last_extent. The function body could probably be written as:

{
	struct xfs_bmbt_irec	got;
	int			error = 0;

	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));

	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
		return -EIO;

	xfs_ilock(ip, XFS_ILOCK_EXCL);
	error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got,
			&is_empty)
	if (!error && !is_empty && got.br_startoff >= off &&
	    got.br_startoff + shift) & BMBT_STARTOFF_MASK < got.br_startoff)
		error = -EINVAL;
	xfs_iunlock(ip, XFS_ILOCK_EXCL);

	return error;
#12 lore raw

Re: [PATCH 4/8] xfs: don't allow insert-range to shift extents past the maximum offset

Quoted 6 lines · Christoph Hellwig
Quoted 1 line

+#define BMBT_STARTOFF_MASK ((1ULL << BMBT_STARTOFF_BITLEN) - 1)

Please move this next to BMBT_STARTOFF_BITLEN.

For brownie points also use this new defined instead of XFS_IEXT_STARTOFF_MASK in the extent tree code.

Separate cleanup patch, but ok.

Quoted 43 lines
Quoted 19 lines
fs/xfs/libxfs/xfs_bmap.c +18 -0
+/* Make sure we won't be right-shifting an extent past the maximum bound. */
+int
+xfs_bmap_can_insert_extents(
+	struct xfs_inode	*ip,
+	xfs_fileoff_t		off,
+	xfs_fileoff_t		shift)
+{
+	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
+	struct xfs_bmbt_irec	got;
+	struct xfs_iext_cursor	icur;
+	xfs_fileoff_t		new_off;
+	int			error = 0;
+
+	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
+		return -EIO;
+
+	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
+	xfs_ilock(ip, XFS_ILOCK_EXCL);

fs/xfs/libxfs/xfs_bmap.c xfs_bmap_collapse_extents( · line 5811

Quoted 10 lines
fs/xfs/libxfs/xfs_bmap.c +10 -0
+
+	if (!(ifp->if_flags & XFS_IFEXTENTS)) {
+		error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
+		if (error)
+			goto out;
+	}
+
+	xfs_iext_last(ifp, &icur);
+	if (!xfs_iext_get_extent(ifp, &icur, &got) || off > got.br_startoff)
+		goto out;

This largely duplicates xfs_bmap_last_extent. The function body could probably be written as:

{
	struct xfs_bmbt_irec	got;
	int			error = 0;

	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));

	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
		return -EIO;

	xfs_ilock(ip, XFS_ILOCK_EXCL);
Quoted 8 lines
	error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got,
			&is_empty)
	if (!error && !is_empty && got.br_startoff >= off &&
	    got.br_startoff + shift) & BMBT_STARTOFF_MASK < got.br_startoff)
		error = -EINVAL;
	xfs_iunlock(ip, XFS_ILOCK_EXCL);

	return error;

I'll give this a test run over night.

--D

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#13 lore raw

PATCH v2 4/8 xfs: don't allow insert-range to shift extents past the maximum offset

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Zorro Lang reports that generic/485 blows an assert on a filesystem with 512 byte blocks. The test tries to fallocate a post-eof extent at the maximum file size and calls insert range to shift the extents right by two blocks. On a 512b block filesystem this causes startoff to overflow the 54-bit startoff field, leading to the assert.

Therefore, always check the rightmost extent to see if it would overflow prior to invoking the insert range machinery.

Reported-by [email protected]

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=200137

---
v2: use xfs_bmap_last_extent, move macro defs to headers

---
 fs/xfs/libxfs/xfs_bmap.c   |   26 ++++++++++++++++++++++++++
 fs/xfs/libxfs/xfs_bmap.h   |    2 ++
 fs/xfs/libxfs/xfs_format.h |    2 ++
 fs/xfs/xfs_bmap_util.c     |    4 ++++
 4 files changed, 34 insertions(+)
fs/xfs/libxfs/xfs_bmap.c +26 -0
index 01628f0c9a0c..7205268b30bc 100644
@@ -5780,6 +5780,32 @@ xfs_bmap_collapse_extents(
 	return error;
 }
 
+/* Make sure we won't be right-shifting an extent past the maximum bound. */
+int
+xfs_bmap_can_insert_extents(
+	struct xfs_inode	*ip,
+	xfs_fileoff_t		off,
+	xfs_fileoff_t		shift)
+{
+	struct xfs_bmbt_irec	got;
+	int			is_empty;
+	int			error = 0;
+
+	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
+
+	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
+		return -EIO;
+
+	xfs_ilock(ip, XFS_ILOCK_EXCL);
+	error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
+	if (!error && !is_empty && got.br_startoff >= off &&
+	    ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
+		error = -EINVAL;
+	xfs_iunlock(ip, XFS_ILOCK_EXCL);
+
+	return error;
+}
+
 int
 xfs_bmap_insert_extents(
 	struct xfs_trans	*tp,
fs/xfs/libxfs/xfs_bmap.h +2 -0
index 99dddbd0fcc6..9b49ddf99c41 100644
@@ -227,6 +227,8 @@ int	xfs_bmap_collapse_extents(struct xfs_trans *tp, struct xfs_inode *ip,
 		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
 		bool *done, xfs_fsblock_t *firstblock,
 		struct xfs_defer_ops *dfops);
+int	xfs_bmap_can_insert_extents(struct xfs_inode *ip, xfs_fileoff_t off,
+		xfs_fileoff_t shift);
 int	xfs_bmap_insert_extents(struct xfs_trans *tp, struct xfs_inode *ip,
 		xfs_fileoff_t *next_fsb, xfs_fileoff_t offset_shift_fsb,
 		bool *done, xfs_fileoff_t stop_fsb, xfs_fsblock_t *firstblock,
fs/xfs/libxfs/xfs_format.h +2 -0
index 7b4a43deb83e..059bc44c27e8 100644
@@ -1529,6 +1529,8 @@ typedef struct xfs_bmdr_block {
 #define BMBT_STARTBLOCK_BITLEN	52
 #define BMBT_BLOCKCOUNT_BITLEN	21
 
+#define BMBT_STARTOFF_MASK	((1ULL << BMBT_STARTOFF_BITLEN) - 1)
+
 typedef struct xfs_bmbt_rec {
 	__be64			l0, l1;
 } xfs_bmbt_rec_t;
fs/xfs/xfs_bmap_util.c +4 -0
index c195b9d857af..bb417156e3bf 100644
@@ -1383,6 +1383,10 @@ xfs_insert_file_space(
 
 	trace_xfs_insert_file_space(ip);
 
+	error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
+	if (error)
+		return error;
+
 	error = xfs_prepare_shift(ip, offset);
 	if (error)
 		return error;
Reviewed-by on v2 4/8
#14 lore raw

Re: [PATCH v2 4/8] xfs: don't allow insert-range to shift extents past the maximum offset

Quoted 8 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Zorro Lang reports that generic/485 blows an assert on a filesystem with 512 byte blocks. The test tries to fallocate a post-eof extent at the maximum file size and calls insert range to shift the extents right by two blocks. On a 512b block filesystem this causes startoff to overflow the 54-bit startoff field, leading to the assert.

Quoted 7 lines

Therefore, always check the rightmost extent to see if it would overflow prior to invoking the insert range machinery.

Reported-by [email protected]

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=200137

Looks good,

#15 lore raw

PATCH 5/8 xfs: recheck reflink state after grabbing ILOCK_SHARED for a write

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

The reflink iflag could have changed since the earlier unlocked check, so if we got ILOCK_SHARED for a write and but we're now a reflink inode we have to switch to ILOCK_EXCL and relock.

This helps us avoid blowing lock assertions in things like generic/166:

XFS: Assertion failed: xfs_isilocked(ip, XFS_ILOCK_EXCL), file: fs/xfs/xfs_reflink.c, line: 383

WARNING: CPU: 1 PID: 24707 at fs/xfs/xfs_message.c:104 assfail+0x25/0x30 [xfs]
Modules linked in: deadline_iosched dm_snapshot dm_bufio ext4 mbcache jbd2 dm_flakey xfs libcrc32c dax_pmem device_dax nd_pmem sch_fq_codel af_packet [last unloaded: scsi_debug]
CPU: 1 PID: 24707 Comm: xfs_io Not tainted 4.18.0-rc1-djw #1
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.10.2-1ubuntu1 04/01/2014
RIP: 0010:assfail+0x25/0x30 [xfs]
Code: ff 0f 0b c3 90 66 66 66 66 90 48 89 f1 41 89 d0 48 c7 c6 e8 ef 1b a0 48 89 fa 31 ff e8 54 f9 ff ff 80 3d fd ba 0f 00 00 75 03 <0f> 0b c3 0f 0b 66 0f 1f 44 00 00 66 66 66 66 90 48 63 f6 49 89 f9
RSP: 0018:ffffc90006423ad8 EFLAGS: 00010246
RAX: 0000000000000000 RBX: ffff880030b65e80 RCX: 0000000000000000
RDX: 00000000ffffffc0 RSI: 000000000000000a RDI: ffffffffa01b0447
RBP: ffffc90006423c10 R08: 0000000000000000 R09: 0000000000000000
R10: ffff88003d43fc30 R11: f000000000000000 R12: ffff880077cda000
R13: 0000000000000000 R14: ffffc90006423c30 R15: ffffc90006423bf9
FS:  00007feba8986800(0000) GS:ffff88003ec00000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 000000000138ab58 CR3: 000000003d40a000 CR4: 00000000000006a0
Call Trace:
 xfs_reflink_allocate_cow+0x24c/0x3d0 [xfs]
 xfs_file_iomap_begin+0x6d2/0xeb0 [xfs]
 ? iomap_to_fiemap+0x80/0x80
 iomap_apply+0x5e/0x130
 iomap_dio_rw+0x2e0/0x400
 ? iomap_to_fiemap+0x80/0x80
 ? xfs_file_dio_aio_write+0x133/0x4a0 [xfs]
 xfs_file_dio_aio_write+0x133/0x4a0 [xfs]
 xfs_file_write_iter+0x7b/0xb0 [xfs]
 __vfs_write+0x16f/0x1f0
 vfs_write+0xc8/0x1c0
 ksys_pwrite64+0x74/0x90
 do_syscall_64+0x56/0x180
 entry_SYSCALL_64_after_hwframe+0x49/0xbe
---
 fs/xfs/xfs_iomap.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/xfs/xfs_iomap.c +14 -1
index 49f5492eed3b..55876dd02f0c 100644
@@ -963,12 +963,13 @@ xfs_ilock_for_iomap(
 	unsigned		*lockmode)
 {
 	unsigned		mode = XFS_ILOCK_SHARED;
+	bool			is_write = flags & (IOMAP_WRITE | IOMAP_ZERO);
 
 	/*
 	 * COW writes may allocate delalloc space or convert unwritten COW
 	 * extents, so we need to make sure to take the lock exclusively here.
 	 */
-	if (xfs_is_reflink_inode(ip) && (flags & (IOMAP_WRITE | IOMAP_ZERO))) {
+	if (xfs_is_reflink_inode(ip) && is_write) {
 		/*
 		 * FIXME: It could still overwrite on unshared extents and not
 		 * need allocation.
@@ -989,6 +990,7 @@ xfs_ilock_for_iomap(
 		mode = XFS_ILOCK_EXCL;
 	}
 
+relock:
 	if (flags & IOMAP_NOWAIT) {
 		if (!xfs_ilock_nowait(ip, mode))
 			return -EAGAIN;
@@ -996,6 +998,17 @@ xfs_ilock_for_iomap(
 		xfs_ilock(ip, mode);
 	}
 
+	/*
+	 * The reflink iflag could have changed since the earlier unlocked
+	 * check, so if we got ILOCK_SHARED for a write and but we're now a
+	 * reflink inode we have to switch to ILOCK_EXCL and relock.
+	 */
+	if (mode == XFS_ILOCK_SHARED && is_write && xfs_is_reflink_inode(ip)) {
+		xfs_iunlock(ip, mode);
+		mode = XFS_ILOCK_EXCL;
+		goto relock;
+	}
+
 	*lockmode = mode;
 	return 0;
 }
#17 lore raw

PATCH 6/8 xfs: fix uninitialized field in rtbitmap fsmap backend

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Initialize the extent count field of the high key so that when we use the high key to synthesize an 'unknown owner' record (i.e. used space record) at the end of the queried range we have a field with which to compute rm_blockcount. This is not strictly necessary because the synthesizer never uses the rm_blockcount field, but we can shut up the static code analysis anyway.

Coverity-id: 1437358

---
 fs/xfs/xfs_fsmap.c |    1 +
 1 file changed, 1 insertion(+)
fs/xfs/xfs_fsmap.c +1 -0
index c34fa9c342f2..2be795b01ce0 100644
@@ -531,6 +531,7 @@ xfs_getfsmap_rtdev_rtbitmap_query(
 
 	/* Report any gaps at the end of the rtbitmap */
 	info->last = true;
+	ahigh.ar_extcount = 0;
 	error = xfs_getfsmap_rtdev_rtbitmap_helper(tp, &ahigh, info);
 	if (error)
 		goto err;
Reviewed-by on v1 6/8
#18 lore raw

Re: [PATCH 6/8] xfs: fix uninitialized field in rtbitmap fsmap backend

Looks ok:

Quoted 34 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Initialize the extent count field of the high key so that when we use the high key to synthesize an 'unknown owner' record (i.e. used space record) at the end of the queried range we have a field with which to compute rm_blockcount. This is not strictly necessary because the synthesizer never uses the rm_blockcount field, but we can shut up the static code analysis anyway.

Coverity-id: 1437358

---
  fs/xfs/xfs_fsmap.c |    1 +
  1 file changed, 1 insertion(+)
fs/xfs/xfs_fsmap.c +1 -1
index c34fa9c342f2..2be795b01ce0 100644
@@ -531,6 +531,7 @@ xfs_getfsmap_rtdev_rtbitmap_query(
 
  	/* Report any gaps at the end of the rtbitmap */
  	info->last = true;
+	ahigh.ar_extcount = 0;
  	error = xfs_getfsmap_rtdev_rtbitmap_helper(tp, &ahigh, info);
  	if (error)
  		goto err;
 
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at https://urldefense.proofpoint.com/v2/url?u=http-3A__vger.kernel.org_majordomo-2Dinfo.html&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=LHZQ8fHvy6wDKXGTWcm97burZH5sQKHRDMaY1UthQxc&m=kG0q-nvJIi8c2zctskm0vTRxZIBqEK8mud41-TvKgAI&s=c2QBy7jBkpo_q56thW4Lrp4PTZl40Ulep8xufwE5NVc&e=

#19 lore raw

Re: [PATCH 6/8] xfs: fix uninitialized field in rtbitmap fsmap backend

In reply to Darrick J. Wong
Quoted 11 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Initialize the extent count field of the high key so that when we use the high key to synthesize an 'unknown owner' record (i.e. used space record) at the end of the queried range we have a field with which to compute rm_blockcount. This is not strictly necessary because the synthesizer never uses the rm_blockcount field, but we can shut up the static code analysis anyway.

Coverity-id: 1437358

I'd rather ensure the structs are entirely zeroed to start with, e.g.:

fs/xfs/xfs_fsmap.c +2 -2
index 0299febece9c..21bbdee451a6 100644
@@ -527,8 +527,8 @@ xfs_getfsmap_rtdev_rtbitmap_query(
 	struct xfs_trans		*tp,
 	struct xfs_getfsmap_info	*info)
 {
-	struct xfs_rtalloc_rec		alow;
-	struct xfs_rtalloc_rec		ahigh;
+	struct xfs_rtalloc_rec		alow = { 0, };
+	struct xfs_rtalloc_rec		ahigh = { 0, };
 	int				error;
 
 	xfs_ilock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
#20 lore raw

Re: [PATCH 6/8] xfs: fix uninitialized field in rtbitmap fsmap backend

Quoted 20 lines · Christoph Hellwig
Quoted 11 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Initialize the extent count field of the high key so that when we use the high key to synthesize an 'unknown owner' record (i.e. used space record) at the end of the queried range we have a field with which to compute rm_blockcount. This is not strictly necessary because the synthesizer never uses the rm_blockcount field, but we can shut up the static code analysis anyway.

Coverity-id: 1437358

I'd rather ensure the structs are entirely zeroed to start with, e.g.:

fs/xfs/xfs_fsmap.c +0 -0
index 0299febece9c..21bbdee451a6 100644

fs/xfs/xfs_fsmap.c xfs_getfsmap_rtdev_rtbitmap_query( · line 531

Quoted 7 lines
fs/xfs/xfs_fsmap.c +2 -2
@@ -527,8 +527,8 @@ xfs_getfsmap_rtdev_rtbitmap_query(
 	struct xfs_trans		*tp,
 	struct xfs_getfsmap_info	*info)
 {
-	struct xfs_rtalloc_rec		alow;
-	struct xfs_rtalloc_rec		ahigh;
+	struct xfs_rtalloc_rec		alow = { 0, };
+	struct xfs_rtalloc_rec		ahigh = { 0, };

Looks reasonable, I'll give it a spin overnight.

--D

Quoted 7 lines
fs/xfs/xfs_fsmap.c +0 -1
 	int				error;
 
 	xfs_ilock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#21 lore raw

PATCH v2 6/8 xfs: fix uninitialized field in rtbitmap fsmap backend

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Initialize the extent count field of the high key so that when we use the high key to synthesize an 'unknown owner' record (i.e. used space record) at the end of the queried range we have a field with which to compute rm_blockcount. This is not strictly necessary because the synthesizer never uses the rm_blockcount field, but we can shut up the static code analysis anyway.

Coverity-id: 1437358

---
v2: init both structs to zero

---
 fs/xfs/xfs_fsmap.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/xfs/xfs_fsmap.c +2 -2
index c34fa9c342f2..c7157bc48bd1 100644
@@ -513,8 +513,8 @@ xfs_getfsmap_rtdev_rtbitmap_query(
 	struct xfs_trans		*tp,
 	struct xfs_getfsmap_info	*info)
 {
-	struct xfs_rtalloc_rec		alow;
-	struct xfs_rtalloc_rec		ahigh;
+	struct xfs_rtalloc_rec		alow = { 0 };
+	struct xfs_rtalloc_rec		ahigh = { 0 };
 	int				error;
 
 	xfs_ilock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
Reviewed-by on v2 6/8
#22 lore raw

Re: [PATCH v2 6/8] xfs: fix uninitialized field in rtbitmap fsmap backend

Quoted 9 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Initialize the extent count field of the high key so that when we use the high key to synthesize an 'unknown owner' record (i.e. used space record) at the end of the queried range we have a field with which to compute rm_blockcount. This is not strictly necessary because the synthesizer never uses the rm_blockcount field, but we can shut up the static code analysis anyway.

Quoted 3 lines

Coverity-id: 1437358

Looks good,

#23 lore raw

PATCH 7/8 xfs: fix off-by-one error in xfs_rtalloc_query_range

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In commit 8ad560d2565e6 ("xfs: strengthen rtalloc query range checks") we strengthened the input parameter checks in the rtbitmap range query function, but introduced an off-by-one error in the process. The call to xfs_rtfind_forw deals with the high key being rextents, but we clamp the high key to rextents - 1. This causes the returned results to stop one block short of the end of the rtdev, which is incorrect.

---
 fs/xfs/libxfs/xfs_rtbitmap.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_rtbitmap.c +2 -2
index 65fc4ed2e9a1..b228c821bae6 100644
@@ -1029,8 +1029,8 @@ xfs_rtalloc_query_range(
 	if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
 	    low_rec->ar_startext == high_rec->ar_startext)
 		return 0;
-	if (high_rec->ar_startext >= mp->m_sb.sb_rextents)
-		high_rec->ar_startext = mp->m_sb.sb_rextents - 1;
+	if (high_rec->ar_startext > mp->m_sb.sb_rextents)
+		high_rec->ar_startext = mp->m_sb.sb_rextents;
 
 	/* Iterate the bitmap, looking for discrepancies. */
 	rtstart = low_rec->ar_startext;
Reviewed-by on v1 7/8
#25 lore raw

Re: [PATCH 7/8] xfs: fix off-by-one error in xfs_rtalloc_query_range

In reply to Darrick J. Wong

Looks good:

Quoted 36 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

In commit 8ad560d2565e6 ("xfs: strengthen rtalloc query range checks") we strengthened the input parameter checks in the rtbitmap range query function, but introduced an off-by-one error in the process. The call to xfs_rtfind_forw deals with the high key being rextents, but we clamp the high key to rextents - 1. This causes the returned results to stop one block short of the end of the rtdev, which is incorrect.

---
  fs/xfs/libxfs/xfs_rtbitmap.c |    4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/xfs/libxfs/xfs_rtbitmap.c +2 -3
index 65fc4ed2e9a1..b228c821bae6 100644
@@ -1029,8 +1029,8 @@ xfs_rtalloc_query_range(
  	if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
  	    low_rec->ar_startext == high_rec->ar_startext)
  		return 0;
-	if (high_rec->ar_startext >= mp->m_sb.sb_rextents)
-		high_rec->ar_startext = mp->m_sb.sb_rextents - 1;
+	if (high_rec->ar_startext > mp->m_sb.sb_rextents)
+		high_rec->ar_startext = mp->m_sb.sb_rextents;
 
  	/* Iterate the bitmap, looking for discrepancies. */
  	rtstart = low_rec->ar_startext;
 
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at https://urldefense.proofpoint.com/v2/url?u=http-3A__vger.kernel.org_majordomo-2Dinfo.html&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=LHZQ8fHvy6wDKXGTWcm97burZH5sQKHRDMaY1UthQxc&m=zsOQBNyhInhyExIQUb5j_oNmILj9vGQF3A7q32gZLuQ&s=JMHXMs2LK6ulhFTcRhasFM3G1BC2jgwHvrD9CU3INrk&e=

#26 lore raw

PATCH 8/8 xfs: ensure post-EOF zeroing happens after zeroing part of a file

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If a user asks us to zero_range part of a file, the end of the range is EOF, and not aligned to a page boundary, invoke writeback of the EOF page to ensure that the post-EOF part of the page is zeroed. This ensures that we don't expose stale memory contents via mmap, if in a clumsy manner.

Found by running generic/127 when it runs zero_range and mapread at EOF one after the other.

---
 fs/xfs/xfs_bmap_util.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/xfs/xfs_bmap_util.c +14 -1
index abc37b0899c0..c94d376e4152 100644
@@ -1208,7 +1208,20 @@ xfs_free_file_space(
 		return 0;
 	if (offset + len > XFS_ISIZE(ip))
 		len = XFS_ISIZE(ip) - offset;
-	return iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
+	error = iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
+	if (error)
+		return error;
+
+	/*
+	 * If we zeroed right up to EOF and EOF straddles a page boundary we
+	 * must make sure that the post-EOF area is also zeroed because the
+	 * page could be mmap'd and iomap_zero_range doesn't do that for us.
+	 * Writeback of the eof page will do this, albeit clumsily.
+	 */
+	if (offset + len < XFS_ISIZE(ip) || ((offset + len) & PAGE_MASK) == 0)
+		return 0;
+	return filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
+			(offset + len) & ~PAGE_MASK, LLONG_MAX);
 }
 
 /*
Reviewed-by on v1 8/8
#27 lore raw

Re: [PATCH 8/8] xfs: ensure post-EOF zeroing happens after zeroing part of a file

Quoted 30 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If a user asks us to zero_range part of a file, the end of the range is EOF, and not aligned to a page boundary, invoke writeback of the EOF page to ensure that the post-EOF part of the page is zeroed. This ensures that we don't expose stale memory contents via mmap, if in a clumsy manner.

Found by running generic/127 when it runs zero_range and mapread at EOF one after the other.

---
 fs/xfs/xfs_bmap_util.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/xfs/xfs_bmap_util.c +4 -1
index abc37b0899c0..c94d376e4152 100644
@@ -1208,7 +1208,20 @@ xfs_free_file_space(
 		return 0;
 	if (offset + len > XFS_ISIZE(ip))
 		len = XFS_ISIZE(ip) - offset;
-	return iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
+	error = iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
+	if (error)
+		return error;
+

fs/xfs/xfs_bmap_util.c xfs_free_file_space( · line 1224

Quoted 10 lines
fs/xfs/xfs_bmap_util.c +10 -0
+	/*
+	 * If we zeroed right up to EOF and EOF straddles a page boundary we
+	 * must make sure that the post-EOF area is also zeroed because the
+	 * page could be mmap'd and iomap_zero_range doesn't do that for us.
+	 * Writeback of the eof page will do this, albeit clumsily.
+	 */
+	if (offset + len < XFS_ISIZE(ip) || ((offset + len) & PAGE_MASK) == 0)
+		return 0;
+	return filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
+			(offset + len) & ~PAGE_MASK, LLONG_MAX);

Total nitpick, but between the kernel logic flow and comment I'd invert the check:

	if (offset + len >= XFS_ISIZE(ip) && (offset + len) & PAGE_MASK) {
		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
			(offset + len) & ~PAGE_MASK, LLONG_MAX);
	}

	return error;

Otherwise looks good:

#28 lore raw

Re: [PATCH 8/8] xfs: ensure post-EOF zeroing happens after zeroing part of a file

Quoted 52 lines · Christoph Hellwig
Quoted 30 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If a user asks us to zero_range part of a file, the end of the range is EOF, and not aligned to a page boundary, invoke writeback of the EOF page to ensure that the post-EOF part of the page is zeroed. This ensures that we don't expose stale memory contents via mmap, if in a clumsy manner.

Found by running generic/127 when it runs zero_range and mapread at EOF one after the other.

---
 fs/xfs/xfs_bmap_util.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
fs/xfs/xfs_bmap_util.c +4 -1
index abc37b0899c0..c94d376e4152 100644
@@ -1208,7 +1208,20 @@ xfs_free_file_space(
 		return 0;
 	if (offset + len > XFS_ISIZE(ip))
 		len = XFS_ISIZE(ip) - offset;
-	return iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
+	error = iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
+	if (error)
+		return error;
+

fs/xfs/xfs_bmap_util.c xfs_free_file_space( · line 1224

Quoted 10 lines
fs/xfs/xfs_bmap_util.c +10 -0
+	/*
+	 * If we zeroed right up to EOF and EOF straddles a page boundary we
+	 * must make sure that the post-EOF area is also zeroed because the
+	 * page could be mmap'd and iomap_zero_range doesn't do that for us.
+	 * Writeback of the eof page will do this, albeit clumsily.
+	 */
+	if (offset + len < XFS_ISIZE(ip) || ((offset + len) & PAGE_MASK) == 0)
+		return 0;
+	return filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
+			(offset + len) & ~PAGE_MASK, LLONG_MAX);

Total nitpick, but between the kernel logic flow and comment I'd invert the check:

	if (offset + len >= XFS_ISIZE(ip) && (offset + len) & PAGE_MASK) {
		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
			(offset + len) & ~PAGE_MASK, LLONG_MAX);
	}

	return error;
Quoted 3 lines

Otherwise looks good:

Ok, will update & test overnight.

--D

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Reviewed-by on v1 8/8
#29 lore raw

Re: [PATCH 8/8] xfs: ensure post-EOF zeroing happens after zeroing part of a file

In reply to Darrick J. Wong

Looks ok to me. Thx!

Quoted 49 lines · Darrick J. Wong

From: Darrick J. Wong <[email protected]>

If a user asks us to zero_range part of a file, the end of the range is EOF, and not aligned to a page boundary, invoke writeback of the EOF page to ensure that the post-EOF part of the page is zeroed. This ensures that we don't expose stale memory contents via mmap, if in a clumsy manner.

Found by running generic/127 when it runs zero_range and mapread at EOF one after the other.

---
  fs/xfs/xfs_bmap_util.c |   15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)
fs/xfs/xfs_bmap_util.c +14 -2
index abc37b0899c0..c94d376e4152 100644
@@ -1208,7 +1208,20 @@ xfs_free_file_space(
  		return 0;
  	if (offset + len > XFS_ISIZE(ip))
  		len = XFS_ISIZE(ip) - offset;
-	return iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
+	error = iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
+	if (error)
+		return error;
+
+	/*
+	 * If we zeroed right up to EOF and EOF straddles a page boundary we
+	 * must make sure that the post-EOF area is also zeroed because the
+	 * page could be mmap'd and iomap_zero_range doesn't do that for us.
+	 * Writeback of the eof page will do this, albeit clumsily.
+	 */
+	if (offset + len < XFS_ISIZE(ip) || ((offset + len) & PAGE_MASK) == 0)
+		return 0;
+	return filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
+			(offset + len) & ~PAGE_MASK, LLONG_MAX);
  }
 
  /*
 
--

To unsubscribe from this list: send the line "unsubscribe linux-xfs" in
the body of a message to [email protected]
More majordomo info at https://urldefense.proofpoint.com/v2/url?u=http-3A__vger.kernel.org_majordomo-2Dinfo.html&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=LHZQ8fHvy6wDKXGTWcm97burZH5sQKHRDMaY1UthQxc&m=wue-_aW-3CpAS-pXEQXCk8gKGGwIqQ1nh1DhJ-3JtBU&s=q874s9Hzp4N6x29WZ6DqXxY7xh1oCZSwFHNb6ZqnTs4&e=

#30 lore raw

[PATCH 9/8] xfs: check leaf attribute block freemap in verifier

In reply to Darrick J. Wong

From: Darrick J. Wong <[email protected]>

Check the leaf attribute freemap when we we're verifying the block. Found via generic/070.

---
 fs/xfs/libxfs/xfs_attr_leaf.c |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
fs/xfs/libxfs/xfs_attr_leaf.c +18 -0
index 76e90046731c..b3c19339e1b5 100644
@@ -244,6 +244,8 @@ xfs_attr3_leaf_verify(
 	struct xfs_attr_leafblock	*leaf = bp->b_addr;
 	struct xfs_perag		*pag = bp->b_pag;
 	struct xfs_attr_leaf_entry	*entries;
+	uint16_t			end;
+	int				i;
 
 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
 
@@ -289,6 +291,22 @@ xfs_attr3_leaf_verify(
 	/* XXX: need to range check rest of attr header values */
 	/* XXX: hash order check? */
 
+	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
+		if (ichdr.freemap[i].base > mp->m_attr_geo->blksize)
+			return __this_address;
+		if (ichdr.freemap[i].base & 0x3)
+			return __this_address;
+		if (ichdr.freemap[i].size > mp->m_attr_geo->blksize)
+			return __this_address;
+		if (ichdr.freemap[i].size & 0x3)
+			return __this_address;
+		end = ichdr.freemap[i].base + ichdr.freemap[i].size;
+		if (end < ichdr.freemap[i].base)
+			return __this_address;
+		if (end > mp->m_attr_geo->blksize)
+			return __this_address;
+	}
+
 	return NULL;
 }