Skip to contentSkip to conversation

xfs: introduce xfs_validate_stripe_geometry()

PATCH Superseded Gao Xiang 2 participants 5 messages Oct 13, 2020 XFS filesystem

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH v2 1/1 xfs: introduce xfs_validate_stripe_geometry()

Introduce a common helper to consolidate stripe validation process. Also make kernel code xfs_validate_sb_common() use it first.

---
v1: https://lore.kernel.org/r/[email protected]

changes since v1:
- rename the helper to xfs_validate_stripe_geometry() (Brian);
- drop a new added trailing newline in xfs_sb.c (Brian);
- add a "bool silent" argument to avoid too many error messages (Brian).

 fs/xfs/libxfs/xfs_sb.c | 70 +++++++++++++++++++++++++++++++++++-------
 fs/xfs/libxfs/xfs_sb.h |  3 ++
 2 files changed, 62 insertions(+), 11 deletions(-)
fs/xfs/libxfs/xfs_sb.c +59 -11
index 5aeafa59ed27..9178715ded45 100644
@@ -360,21 +360,18 @@ xfs_validate_sb_common(
 		}
 	}
 
-	if (sbp->sb_unit) {
-		if (!xfs_sb_version_hasdalign(sbp) ||
-		    sbp->sb_unit > sbp->sb_width ||
-		    (sbp->sb_width % sbp->sb_unit) != 0) {
-			xfs_notice(mp, "SB stripe unit sanity check failed");
-			return -EFSCORRUPTED;
-		}
-	} else if (xfs_sb_version_hasdalign(sbp)) {
+	/*
+	 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
+	 * would imply the image is corrupted.
+	 */
+	if (!sbp->sb_unit ^ !xfs_sb_version_hasdalign(sbp)) {
 		xfs_notice(mp, "SB stripe alignment sanity check failed");
 		return -EFSCORRUPTED;
-	} else if (sbp->sb_width) {
-		xfs_notice(mp, "SB stripe width sanity check failed");
-		return -EFSCORRUPTED;
 	}
 
+	if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
+			XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
+		return -EFSCORRUPTED;
 
 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
 	    sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
@@ -1233,3 +1230,54 @@ xfs_sb_get_secondary(
 	*bpp = bp;
 	return 0;
 }
+
+/*
+ * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
+ * so users won't be confused by values in error messages.
+ */
+bool
+xfs_validate_stripe_geometry(
+	struct xfs_mount	*mp,
+	__s64			sunit,
+	__s64			swidth,
+	int			sectorsize,
+	bool			silent)
+{
+	if (sectorsize && sunit % sectorsize) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe unit (%lld) must be a multiple of the sector size (%d)",
+				   sunit, sectorsize);
+		return false;
+	}
+
+	if (sunit && !swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"invalid stripe unit (%lld) and stripe width of 0", sunit);
+		return false;
+	}
+
+	if (!sunit && swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"invalid stripe width (%lld) and stripe unit of 0", swidth);
+		return false;
+	}
+
+	if (sunit > swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
+		return false;
+	}
+
+	if (sunit && (swidth % sunit)) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe width (%lld) must be a multiple of the stripe unit (%lld)",
+				   swidth, sunit);
+		return false;
+	}
+	return true;
+}
fs/xfs/libxfs/xfs_sb.h +3 -0
index 92465a9a5162..f79f9dc632b6 100644
@@ -42,4 +42,7 @@ extern int	xfs_sb_get_secondary(struct xfs_mount *mp,
 				struct xfs_trans *tp, xfs_agnumber_t agno,
 				struct xfs_buf **bpp);
 
+extern bool	xfs_validate_stripe_geometry(struct xfs_mount *mp,
+		__s64 sunit, __s64 swidth, int sectorsize, bool silent);
+
 #endif	/* __XFS_SB_H__ */
Signature · 1 line
2.18.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 27 lines · Gao Xiang

Introduce a common helper to consolidate stripe validation process. Also make kernel code xfs_validate_sb_common() use it first.

---
v1: https://lore.kernel.org/r/[email protected]

changes since v1:
- rename the helper to xfs_validate_stripe_geometry() (Brian);
- drop a new added trailing newline in xfs_sb.c (Brian);
- add a "bool silent" argument to avoid too many error messages (Brian).

 fs/xfs/libxfs/xfs_sb.c | 70 +++++++++++++++++++++++++++++++++++-------
 fs/xfs/libxfs/xfs_sb.h |  3 ++
 2 files changed, 62 insertions(+), 11 deletions(-)
fs/xfs/libxfs/xfs_sb.c +0 -3
index 5aeafa59ed27..9178715ded45 100644
@@ -360,21 +360,18 @@ xfs_validate_sb_common(
 		}
 	}
 
-	if (sbp->sb_unit) {
-		if (!xfs_sb_version_hasdalign(sbp) ||
-		    sbp->sb_unit > sbp->sb_width ||

fs/xfs/libxfs/xfs_sb.c xfs_validate_sb_common( · line 367

Quoted 10 lines
fs/xfs/libxfs/xfs_sb.c +5 -5
-		    (sbp->sb_width % sbp->sb_unit) != 0) {
-			xfs_notice(mp, "SB stripe unit sanity check failed");
-			return -EFSCORRUPTED;
-		}
-	} else if (xfs_sb_version_hasdalign(sbp)) {
+	/*
+	 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
+	 * would imply the image is corrupted.
+	 */
+	if (!sbp->sb_unit ^ !xfs_sb_version_hasdalign(sbp)) {

This can be simplified to drop the negations (!), right?

Quoted 50 lines
fs/xfs/libxfs/xfs_sb.c +36 -3
 		xfs_notice(mp, "SB stripe alignment sanity check failed");
 		return -EFSCORRUPTED;
-	} else if (sbp->sb_width) {
-		xfs_notice(mp, "SB stripe width sanity check failed");
-		return -EFSCORRUPTED;
 	}
 
+	if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
+			XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
+		return -EFSCORRUPTED;
 
 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
 	    sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
@@ -1233,3 +1230,54 @@ xfs_sb_get_secondary(
 	*bpp = bp;
 	return 0;
 }
+
+/*
+ * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
+ * so users won't be confused by values in error messages.
+ */
+bool
+xfs_validate_stripe_geometry(
+	struct xfs_mount	*mp,
+	__s64			sunit,
+	__s64			swidth,
+	int			sectorsize,
+	bool			silent)
+{
+	if (sectorsize && sunit % sectorsize) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe unit (%lld) must be a multiple of the sector size (%d)",
+				   sunit, sectorsize);
+		return false;
+	}
+
+	if (sunit && !swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"invalid stripe unit (%lld) and stripe width of 0", sunit);
+		return false;
+	}
+
+	if (!sunit && swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"invalid stripe width (%lld) and stripe unit of 0", swidth);
+		return false;

fs/xfs/libxfs/xfs_sb.c xfs_sb_get_secondary( · line 1275

Quoted 10 lines
fs/xfs/libxfs/xfs_sb.c +10 -0
+	}
+
+	if (sunit > swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
+		return false;
+	}
+
+	if (sunit && (swidth % sunit)) {

It might be good to use (or not) params consistently. I.e., the sectorsize check earlier in the function has similar logic structure but drops the params.

Those nits aside:

Quoted 23 lines
fs/xfs/libxfs/xfs_sb.c +8 -0
+		if (!silent)
+			xfs_notice(mp,
+"stripe width (%lld) must be a multiple of the stripe unit (%lld)",
+				   swidth, sunit);
+		return false;
+	}
+	return true;
+}
fs/xfs/libxfs/xfs_sb.h +3 -0
index 92465a9a5162..f79f9dc632b6 100644
@@ -42,4 +42,7 @@ extern int	xfs_sb_get_secondary(struct xfs_mount *mp,
 				struct xfs_trans *tp, xfs_agnumber_t agno,
 				struct xfs_buf **bpp);
 
+extern bool	xfs_validate_stripe_geometry(struct xfs_mount *mp,
+		__s64 sunit, __s64 swidth, int sectorsize, bool silent);
+
 #endif	/* __XFS_SB_H__ */
Signature · 1 line
2.18.1
author
#2 lore raw

Hi Brian,

Quoted 25 lines · Brian Foster
Quoted 24 lines · Gao Xiang

Introduce a common helper to consolidate stripe validation process. Also make kernel code xfs_validate_sb_common() use it first.

---
v1: https://lore.kernel.org/r/[email protected]

changes since v1:
- rename the helper to xfs_validate_stripe_geometry() (Brian);
- drop a new added trailing newline in xfs_sb.c (Brian);
- add a "bool silent" argument to avoid too many error messages (Brian).

 fs/xfs/libxfs/xfs_sb.c | 70 +++++++++++++++++++++++++++++++++++-------
 fs/xfs/libxfs/xfs_sb.h |  3 ++
 2 files changed, 62 insertions(+), 11 deletions(-)
fs/xfs/libxfs/xfs_sb.c +0 -0
index 5aeafa59ed27..9178715ded45 100644
@@ -360,21 +360,18 @@ xfs_validate_sb_common(
 		}
 	}
 
Quoted 15 lines

fs/xfs/libxfs/xfs_sb.c xfs_validate_sb_common( · line 367

Quoted 13 lines
fs/xfs/libxfs/xfs_sb.c +5 -8
-	if (sbp->sb_unit) {
-		if (!xfs_sb_version_hasdalign(sbp) ||
-		    sbp->sb_unit > sbp->sb_width ||
-		    (sbp->sb_width % sbp->sb_unit) != 0) {
-			xfs_notice(mp, "SB stripe unit sanity check failed");
-			return -EFSCORRUPTED;
-		}
-	} else if (xfs_sb_version_hasdalign(sbp)) {
+	/*
+	 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
+	 * would imply the image is corrupted.
+	 */
+	if (!sbp->sb_unit ^ !xfs_sb_version_hasdalign(sbp)) {

This can be simplified to drop the negations (!), right?

Thanks for the suggestion.

yet nope, honestly I don't think so, the reason is that sbp->sb_unit is an integer here rather than a boolean, so negations cannot be simplified and I think it's simpliest now... (some boolean algebra...)

Quoted 2 lines
Quoted 2 lines
fs/xfs/libxfs/xfs_sb.c +0 -0
 		xfs_notice(mp, "SB stripe alignment sanity check failed");

...

Quoted 26 lines
Quoted 26 lines
fs/xfs/libxfs/xfs_sb.c +26 -0
+	if (sectorsize && sunit % sectorsize) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe unit (%lld) must be a multiple of the sector size (%d)",
+				   sunit, sectorsize);
+		return false;
+	}
+
+	if (sunit && !swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"invalid stripe unit (%lld) and stripe width of 0", sunit);
+		return false;
+	}
+
+	if (!sunit && swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"invalid stripe width (%lld) and stripe unit of 0", swidth);
+		return false;
+	}
+
+	if (sunit > swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
Quoted 8 lines

fs/xfs/libxfs/xfs_sb.c xfs_sb_get_secondary( · line 1275

Quoted 4 lines
fs/xfs/libxfs/xfs_sb.c +4 -0
+		return false;
+	}
+
+	if (sunit && (swidth % sunit)) {

It might be good to use (or not) params consistently. I.e., the sectorsize check earlier in the function has similar logic structure but drops the params.

Yeah, that is due to the line was copied from somewhere else... so... Anyway, I can resend a quick fix for this if needed. Wait a sec for some potential feedback...

Thanks,
Gao Xiang

Quoted 5 lines

Those nits aside:

#3 lore raw
Quoted 44 lines · Gao Xiang

Hi Brian,

Quoted 25 lines · Brian Foster
Quoted 24 lines · Gao Xiang

Introduce a common helper to consolidate stripe validation process. Also make kernel code xfs_validate_sb_common() use it first.

---
v1: https://lore.kernel.org/r/[email protected]

changes since v1:
- rename the helper to xfs_validate_stripe_geometry() (Brian);
- drop a new added trailing newline in xfs_sb.c (Brian);
- add a "bool silent" argument to avoid too many error messages (Brian).

 fs/xfs/libxfs/xfs_sb.c | 70 +++++++++++++++++++++++++++++++++++-------
 fs/xfs/libxfs/xfs_sb.h |  3 ++
 2 files changed, 62 insertions(+), 11 deletions(-)
fs/xfs/libxfs/xfs_sb.c +0 -0
index 5aeafa59ed27..9178715ded45 100644
@@ -360,21 +360,18 @@ xfs_validate_sb_common(
 		}
 	}
 
Quoted 15 lines

fs/xfs/libxfs/xfs_sb.c xfs_validate_sb_common( · line 367

Quoted 13 lines
fs/xfs/libxfs/xfs_sb.c +5 -8
-	if (sbp->sb_unit) {
-		if (!xfs_sb_version_hasdalign(sbp) ||
-		    sbp->sb_unit > sbp->sb_width ||
-		    (sbp->sb_width % sbp->sb_unit) != 0) {
-			xfs_notice(mp, "SB stripe unit sanity check failed");
-			return -EFSCORRUPTED;
-		}
-	} else if (xfs_sb_version_hasdalign(sbp)) {
+	/*
+	 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
+	 * would imply the image is corrupted.
+	 */
+	if (!sbp->sb_unit ^ !xfs_sb_version_hasdalign(sbp)) {

This can be simplified to drop the negations (!), right?

Quoted 6 lines

Thanks for the suggestion.

yet nope, honestly I don't think so, the reason is that sbp->sb_unit is an integer here rather than a boolean, so negations cannot be simplified and I think it's simpliest now... (some boolean algebra...)

Oh, right. So you'd actually need something like (!!sunit ^ hasdalign()) to avoid the bit operation.

Brian

Quoted 53 lines
Quoted 2 lines
Quoted 2 lines
fs/xfs/libxfs/xfs_sb.c +0 -0
 		xfs_notice(mp, "SB stripe alignment sanity check failed");

...

Quoted 26 lines
Quoted 26 lines
fs/xfs/libxfs/xfs_sb.c +26 -0
+	if (sectorsize && sunit % sectorsize) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe unit (%lld) must be a multiple of the sector size (%d)",
+				   sunit, sectorsize);
+		return false;
+	}
+
+	if (sunit && !swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"invalid stripe unit (%lld) and stripe width of 0", sunit);
+		return false;
+	}
+
+	if (!sunit && swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"invalid stripe width (%lld) and stripe unit of 0", swidth);
+		return false;
+	}
+
+	if (sunit > swidth) {
+		if (!silent)
+			xfs_notice(mp,
+"stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
Quoted 8 lines

fs/xfs/libxfs/xfs_sb.c xfs_sb_get_secondary( · line 1275

Quoted 4 lines
fs/xfs/libxfs/xfs_sb.c +4 -0
+		return false;
+	}
+
+	if (sunit && (swidth % sunit)) {

It might be good to use (or not) params consistently. I.e., the sectorsize check earlier in the function has similar logic structure but drops the params.

Yeah, that is due to the line was copied from somewhere else... so... Anyway, I can resend a quick fix for this if needed. Wait a sec for some potential feedback...

Thanks,
Gao Xiang

Quoted 5 lines

Those nits aside:

author
#4 lore raw
Quoted 47 lines · Brian Foster
Quoted 46 lines · Gao Xiang

Hi Brian,

Quoted 25 lines · Brian Foster
Quoted 24 lines · Gao Xiang

Introduce a common helper to consolidate stripe validation process. Also make kernel code xfs_validate_sb_common() use it first.

---
v1: https://lore.kernel.org/r/[email protected]

changes since v1:
- rename the helper to xfs_validate_stripe_geometry() (Brian);
- drop a new added trailing newline in xfs_sb.c (Brian);
- add a "bool silent" argument to avoid too many error messages (Brian).

 fs/xfs/libxfs/xfs_sb.c | 70 +++++++++++++++++++++++++++++++++++-------
 fs/xfs/libxfs/xfs_sb.h |  3 ++
 2 files changed, 62 insertions(+), 11 deletions(-)
fs/xfs/libxfs/xfs_sb.c +0 -0
index 5aeafa59ed27..9178715ded45 100644
@@ -360,21 +360,18 @@ xfs_validate_sb_common(
 		}
 	}
 
Quoted 15 lines

fs/xfs/libxfs/xfs_sb.c xfs_validate_sb_common( · line 367

Quoted 13 lines
fs/xfs/libxfs/xfs_sb.c +5 -8
-	if (sbp->sb_unit) {
-		if (!xfs_sb_version_hasdalign(sbp) ||
-		    sbp->sb_unit > sbp->sb_width ||
-		    (sbp->sb_width % sbp->sb_unit) != 0) {
-			xfs_notice(mp, "SB stripe unit sanity check failed");
-			return -EFSCORRUPTED;
-		}
-	} else if (xfs_sb_version_hasdalign(sbp)) {
+	/*
+	 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
+	 * would imply the image is corrupted.
+	 */
+	if (!sbp->sb_unit ^ !xfs_sb_version_hasdalign(sbp)) {

This can be simplified to drop the negations (!), right?

Thanks for the suggestion.

Quoted 7 lines
Quoted 4 lines

yet nope, honestly I don't think so, the reason is that sbp->sb_unit is an integer here rather than a boolean, so negations cannot be simplified and I think it's simpliest now... (some boolean algebra...)

Oh, right. So you'd actually need something like (!!sunit ^ hasdalign()) to avoid the bit operation.

Agree, that expression looks better <nod>
I will switch to it then. Thanks!

Thanks,
Gao Xiang

Quoted 2 lines

Brian