Skip to content

Commit 7bc1fea

Browse files
Gao Xiangdjwong
authored andcommitted
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. Signed-off-by: Gao Xiang <hsiangkao@redhat.com> Reviewed-by: Brian Foster <bfoster@redhat.com> Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
1 parent 237d788 commit 7bc1fea

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

fs/xfs/libxfs/xfs_sb.c

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,21 +360,18 @@ xfs_validate_sb_common(
360360
}
361361
}
362362

363-
if (sbp->sb_unit) {
364-
if (!xfs_sb_version_hasdalign(sbp) ||
365-
sbp->sb_unit > sbp->sb_width ||
366-
(sbp->sb_width % sbp->sb_unit) != 0) {
367-
xfs_notice(mp, "SB stripe unit sanity check failed");
368-
return -EFSCORRUPTED;
369-
}
370-
} else if (xfs_sb_version_hasdalign(sbp)) {
363+
/*
364+
* Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
365+
* would imply the image is corrupted.
366+
*/
367+
if (!!sbp->sb_unit ^ xfs_sb_version_hasdalign(sbp)) {
371368
xfs_notice(mp, "SB stripe alignment sanity check failed");
372369
return -EFSCORRUPTED;
373-
} else if (sbp->sb_width) {
374-
xfs_notice(mp, "SB stripe width sanity check failed");
375-
return -EFSCORRUPTED;
376370
}
377371

372+
if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
373+
XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
374+
return -EFSCORRUPTED;
378375

379376
if (xfs_sb_version_hascrc(&mp->m_sb) &&
380377
sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
@@ -1206,3 +1203,61 @@ xfs_sb_get_secondary(
12061203
*bpp = bp;
12071204
return 0;
12081205
}
1206+
1207+
/*
1208+
* sunit, swidth, sectorsize(optional with 0) should be all in bytes,
1209+
* so users won't be confused by values in error messages.
1210+
*/
1211+
bool
1212+
xfs_validate_stripe_geometry(
1213+
struct xfs_mount *mp,
1214+
__s64 sunit,
1215+
__s64 swidth,
1216+
int sectorsize,
1217+
bool silent)
1218+
{
1219+
if (swidth > INT_MAX) {
1220+
if (!silent)
1221+
xfs_notice(mp,
1222+
"stripe width (%lld) is too large", swidth);
1223+
return false;
1224+
}
1225+
1226+
if (sunit > swidth) {
1227+
if (!silent)
1228+
xfs_notice(mp,
1229+
"stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
1230+
return false;
1231+
}
1232+
1233+
if (sectorsize && (int)sunit % sectorsize) {
1234+
if (!silent)
1235+
xfs_notice(mp,
1236+
"stripe unit (%lld) must be a multiple of the sector size (%d)",
1237+
sunit, sectorsize);
1238+
return false;
1239+
}
1240+
1241+
if (sunit && !swidth) {
1242+
if (!silent)
1243+
xfs_notice(mp,
1244+
"invalid stripe unit (%lld) and stripe width of 0", sunit);
1245+
return false;
1246+
}
1247+
1248+
if (!sunit && swidth) {
1249+
if (!silent)
1250+
xfs_notice(mp,
1251+
"invalid stripe width (%lld) and stripe unit of 0", swidth);
1252+
return false;
1253+
}
1254+
1255+
if (sunit && (int)swidth % (int)sunit) {
1256+
if (!silent)
1257+
xfs_notice(mp,
1258+
"stripe width (%lld) must be a multiple of the stripe unit (%lld)",
1259+
swidth, sunit);
1260+
return false;
1261+
}
1262+
return true;
1263+
}

fs/xfs/libxfs/xfs_sb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ extern int xfs_sb_get_secondary(struct xfs_mount *mp,
4242
struct xfs_trans *tp, xfs_agnumber_t agno,
4343
struct xfs_buf **bpp);
4444

45+
extern bool xfs_validate_stripe_geometry(struct xfs_mount *mp,
46+
__s64 sunit, __s64 swidth, int sectorsize, bool silent);
47+
4548
#endif /* __XFS_SB_H__ */

0 commit comments

Comments
 (0)