Skip to contentSkip to conversation

xfs: overflow in xfs_iomap_eof_align_last_fsb

PATCH Superseded Peter Watkins 3 participants 5 messages Nov 24, 2014 – Dec 2, 2014 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: overflow in xfs_iomap_eof_align_last_fsb

Someone else may have run into this already, if not please take a look.

Peter

If extsize is set and new_last_fsb is larger than 32 bits, the roundup to extsize will overflow the align variable. Instead, combine alignments by rounding extsize hint up to stripe size.

---
 fs/xfs/xfs_iomap.c |   19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
fs/xfs/xfs_iomap.c +10 -9
index afcf3c9..0c4abfe 100644
@@ -52,7 +52,7 @@ xfs_iomap_eof_align_last_fsb(
 	xfs_extlen_t	extsize,
 	xfs_fileoff_t	*last_fsb)
 {
-	xfs_fileoff_t	new_last_fsb = 0;
+	xfs_fileoff_t	new_last_fsb;
 	xfs_extlen_t	align = 0;
 	int		eof, error;
 
@@ -70,23 +70,24 @@ xfs_iomap_eof_align_last_fsb(
 		else if (mp->m_dalign)
 			align = mp->m_dalign;
 
-		if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
-			new_last_fsb = roundup_64(*last_fsb, align);
+		if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
+			align = 0;
 	}
 
 	/*
-	 * Always round up the allocation request to an extent boundary
-	 * (when file on a real-time subvolume or has di_extsize hint).
+	 * Round up the allocation request to an extent boundary. If
+	 * already aligned to a stripe, round extsize up to a stripe
+	 * boundary.
 	 */
 	if (extsize) {
-		if (new_last_fsb)
-			align = roundup_64(new_last_fsb, extsize);
+		if (align)
+			align = roundup_64(extsize, align);
 		else
 			align = extsize;
-		new_last_fsb = roundup_64(*last_fsb, align);
 	}
 
-	if (new_last_fsb) {
+	if (align) {
+		new_last_fsb = roundup_64(*last_fsb, align);
 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
 		if (error)
 			return error;
Signature · 6 lines
1.7.9.5

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 5 lines · Peter Watkins

Someone else may have run into this already, if not please take a look.

Peter

Quoted 7 lines

If extsize is set and new_last_fsb is larger than 32 bits, the roundup to extsize will overflow the align variable. Instead, combine alignments by rounding extsize hint up to stripe size.

---

So we basically assign a file offset value to an extent length variable. Nice spot...

Quoted 28 lines
 fs/xfs/xfs_iomap.c |   19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
fs/xfs/xfs_iomap.c +3 -4
index afcf3c9..0c4abfe 100644
@@ -52,7 +52,7 @@ xfs_iomap_eof_align_last_fsb(
 	xfs_extlen_t	extsize,
 	xfs_fileoff_t	*last_fsb)
 {
-	xfs_fileoff_t	new_last_fsb = 0;
+	xfs_fileoff_t	new_last_fsb;
 	xfs_extlen_t	align = 0;
 	int		eof, error;
 
@@ -70,23 +70,24 @@ xfs_iomap_eof_align_last_fsb(
 		else if (mp->m_dalign)
 			align = mp->m_dalign;
 
-		if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
-			new_last_fsb = roundup_64(*last_fsb, align);
+		if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
+			align = 0;
 	}
 
 	/*
-	 * Always round up the allocation request to an extent boundary

fs/xfs/xfs_iomap.c xfs_iomap_eof_align_last_fsb( · line 84

Quoted 10 lines
fs/xfs/xfs_iomap.c +5 -3
-	 * (when file on a real-time subvolume or has di_extsize hint).
+	 * Round up the allocation request to an extent boundary. If
+	 * already aligned to a stripe, round extsize up to a stripe
+	 * boundary.
 	 */
 	if (extsize) {
-		if (new_last_fsb)
-			align = roundup_64(new_last_fsb, extsize);
+		if (align)
+			align = roundup_64(extsize, align);

I think the previous behavior would swap this around and round up the alignment to the extsize. Because we aligned from the actual fsb, we'd effectively use the stripe alignment as a min. alloc and always align to the hint. Here we simply round up extsize to the stripe alignment. IOW, the alignment was previously always to the hint, even if smaller than the stripe alignment, and now we align to the larger of the hint or stripe.

That said, it's kind of a weird scenario and it's not clear to me whether it's important or even intended. If not, the patch seems Ok to me...

Brian

Quoted 18 lines
fs/xfs/xfs_iomap.c +2 -2
 		else
 			align = extsize;
-		new_last_fsb = roundup_64(*last_fsb, align);
 	}
 
-	if (new_last_fsb) {
+	if (align) {
+		new_last_fsb = roundup_64(*last_fsb, align);
 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
 		if (error)
 			return error;
Signature · 6 lines
1.7.9.5

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw
In reply to Peter Watkins
Quoted 8 lines · Peter Watkins

Someone else may have run into this already, if not please take a look.

Peter

If extsize is set and new_last_fsb is larger than 32 bits, the roundup to extsize will overflow the align variable. Instead, combine alignments by rounding extsize hint up to stripe size.

Change of allocator behaviour that will cause significant problems for applications that depend on deterministic behaviour from the extent size hint.

i.e. The extsize hint is supposed to override all other alignments that are made - extent size hints are effectively a "unit of allocation". Hence we round up to stripe unit/width, then apply the extent size hint alignment so that extent sizes are always aligned to the extent size hint....

....

fs/xfs/xfs_iomap.c xfs_iomap_eof_align_last_fsb( · line 55

Quoted 6 lines
fs/xfs/xfs_iomap.c +1 -1
@@ -52,7 +52,7 @@ xfs_iomap_eof_align_last_fsb(
 	xfs_extlen_t	extsize,
 	xfs_fileoff_t	*last_fsb)
 {
-	xfs_fileoff_t	new_last_fsb = 0;
+	xfs_fileoff_t	new_last_fsb;

That can be declared in the context that uses it now.

Quoted 10 lines
fs/xfs/xfs_iomap.c +1 -2
 	xfs_extlen_t	align = 0;
 	int		eof, error;
 
@@ -70,23 +70,24 @@ xfs_iomap_eof_align_last_fsb(
 		else if (mp->m_dalign)
 			align = mp->m_dalign;
 
-		if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
-			new_last_fsb = roundup_64(*last_fsb, align);
+		if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))

fs/xfs/xfs_iomap.c xfs_iomap_eof_align_last_fsb( · line 81

Quoted 10 lines
fs/xfs/xfs_iomap.c +4 -2
+			align = 0;
 	}
 
 	/*
-	 * Always round up the allocation request to an extent boundary
-	 * (when file on a real-time subvolume or has di_extsize hint).
+	 * Round up the allocation request to an extent boundary. If
+	 * already aligned to a stripe, round extsize up to a stripe
+	 * boundary.
 	 */

Drop the comment change...

fs/xfs/xfs_iomap.c xfs_iomap_eof_align_last_fsb( · line 84

Quoted 5 lines
fs/xfs/xfs_iomap.c +2 -2
 	if (extsize) {
-		if (new_last_fsb)
-			align = roundup_64(new_last_fsb, extsize);
+		if (align)
+			align = roundup_64(extsize, align);
			align = roundup(align, extsize);

fs/xfs/xfs_iomap.c xfs_iomap_eof_align_last_fsb( · line 90

Quoted 8 lines
fs/xfs/xfs_iomap.c +2 -2
 		else
 			align = extsize;
-		new_last_fsb = roundup_64(*last_fsb, align);
 	}
 
-	if (new_last_fsb) {
+	if (align) {
+		new_last_fsb = roundup_64(*last_fsb, align);
		xfs_fileoff_t	new_last_fsb = roundup_64(*last_fsb, align);

fs/xfs/xfs_iomap.c xfs_iomap_eof_align_last_fsb( · line 93

Quoted 3 lines
fs/xfs/xfs_iomap.c +0 -0
 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
 		if (error)
 			return error;

Cheers,

Dave.

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#3 lore raw

PATCH v2 1/1 xfs: overflow in xfs_iomap_eof_align_last_fsb

In reply to Peter Watkins

If extsize is set and new_last_fsb is larger than 32 bits, the roundup to extsize will overflow the align variable. Instead, combine alignments by rounding stripe size up to extsize.

---
 fs/xfs/xfs_iomap.c |   13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
fs/xfs/xfs_iomap.c +6 -7
index afcf3c9..3fad071 100644
@@ -52,7 +52,6 @@ xfs_iomap_eof_align_last_fsb(
 	xfs_extlen_t	extsize,
 	xfs_fileoff_t	*last_fsb)
 {
-	xfs_fileoff_t	new_last_fsb = 0;
 	xfs_extlen_t	align = 0;
 	int		eof, error;
 
@@ -70,8 +69,8 @@ xfs_iomap_eof_align_last_fsb(
 		else if (mp->m_dalign)
 			align = mp->m_dalign;
 
-		if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
-			new_last_fsb = roundup_64(*last_fsb, align);
+		if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
+			align = 0;
 	}
 
 	/*
@@ -79,14 +78,14 @@ xfs_iomap_eof_align_last_fsb(
 	 * (when file on a real-time subvolume or has di_extsize hint).
 	 */
 	if (extsize) {
-		if (new_last_fsb)
-			align = roundup_64(new_last_fsb, extsize);
+		if (align)
+			align = roundup_64(align, extsize);
 		else
 			align = extsize;
-		new_last_fsb = roundup_64(*last_fsb, align);
 	}
 
-	if (new_last_fsb) {
+	if (align) {
+		xfs_fileoff_t	new_last_fsb = roundup_64(*last_fsb, align);
 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
 		if (error)
 			return error;
Signature · 6 lines
1.7.9.5

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
Reviewed-by on v2 1/1
#4 lore raw
Quoted 7 lines · Peter Watkins

If extsize is set and new_last_fsb is larger than 32 bits, the roundup to extsize will overflow the align variable. Instead, combine alignments by rounding stripe size up to extsize.

---

Looks fine to me:

Quoted 52 lines
 fs/xfs/xfs_iomap.c |   13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
fs/xfs/xfs_iomap.c +6 -7
index afcf3c9..3fad071 100644
@@ -52,7 +52,6 @@ xfs_iomap_eof_align_last_fsb(
 	xfs_extlen_t	extsize,
 	xfs_fileoff_t	*last_fsb)
 {
-	xfs_fileoff_t	new_last_fsb = 0;
 	xfs_extlen_t	align = 0;
 	int		eof, error;
 
@@ -70,8 +69,8 @@ xfs_iomap_eof_align_last_fsb(
 		else if (mp->m_dalign)
 			align = mp->m_dalign;
 
-		if (align && XFS_ISIZE(ip) >= XFS_FSB_TO_B(mp, align))
-			new_last_fsb = roundup_64(*last_fsb, align);
+		if (align && XFS_ISIZE(ip) < XFS_FSB_TO_B(mp, align))
+			align = 0;
 	}
 
 	/*
@@ -79,14 +78,14 @@ xfs_iomap_eof_align_last_fsb(
 	 * (when file on a real-time subvolume or has di_extsize hint).
 	 */
 	if (extsize) {
-		if (new_last_fsb)
-			align = roundup_64(new_last_fsb, extsize);
+		if (align)
+			align = roundup_64(align, extsize);
 		else
 			align = extsize;
-		new_last_fsb = roundup_64(*last_fsb, align);
 	}
 
-	if (new_last_fsb) {
+	if (align) {
+		xfs_fileoff_t	new_last_fsb = roundup_64(*last_fsb, align);
 		error = xfs_bmap_eof(ip, new_last_fsb, XFS_DATA_FORK, &eof);
 		if (error)
 			return error;
Signature · 6 lines
1.7.9.5

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs