2022-08-30 13:41:33

by Zeng Heng

[permalink] [raw]
Subject: [PATCH -next 0/2] Simplify if-else condition for clean code

Simplify if-else condition for clean code,
which makes them easier to understand.

Zeng Heng (2):
xfs: simplify if-else condition in xfs_validate_new_dalign
xfs: simplify if-else condition in xfs_reflink_trim_around_shared

fs/xfs/xfs_mount.c | 38 ++++++++++++++++++++------------------
fs/xfs/xfs_reflink.c | 22 ++++++++++++----------
2 files changed, 32 insertions(+), 28 deletions(-)

--
2.25.1


2022-08-30 13:42:18

by Zeng Heng

[permalink] [raw]
Subject: [PATCH -next 1/2] xfs: simplify if-else condition in xfs_validate_new_dalign

"else" is not generally useful after a return,
so remove them which makes if condition a bit
more clear.

There is no logical changes.

Signed-off-by: Zeng Heng <[email protected]>
---
fs/xfs/xfs_mount.c | 38 ++++++++++++++++++++------------------
1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c
index f10c88cee116..e8bb3c2e847e 100644
--- a/fs/xfs/xfs_mount.c
+++ b/fs/xfs/xfs_mount.c
@@ -300,26 +300,28 @@ xfs_validate_new_dalign(
"alignment check failed: sunit/swidth vs. blocksize(%d)",
mp->m_sb.sb_blocksize);
return -EINVAL;
- } else {
- /*
- * Convert the stripe unit and width to FSBs.
- */
- mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
- if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) {
- xfs_warn(mp,
- "alignment check failed: sunit/swidth vs. agsize(%d)",
- mp->m_sb.sb_agblocks);
- return -EINVAL;
- } else if (mp->m_dalign) {
- mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
- } else {
- xfs_warn(mp,
- "alignment check failed: sunit(%d) less than bsize(%d)",
- mp->m_dalign, mp->m_sb.sb_blocksize);
- return -EINVAL;
- }
}

+ /*
+ * Convert the stripe unit and width to FSBs.
+ */
+ mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
+ if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) {
+ xfs_warn(mp,
+ "alignment check failed: sunit/swidth vs. agsize(%d)",
+ mp->m_sb.sb_agblocks);
+ return -EINVAL;
+ }
+
+ if (!mp->m_dalign) {
+ xfs_warn(mp,
+ "alignment check failed: sunit(%d) less than bsize(%d)",
+ mp->m_dalign, mp->m_sb.sb_blocksize);
+ return -EINVAL;
+ }
+
+ mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
+
if (!xfs_has_dalign(mp)) {
xfs_warn(mp,
"cannot change alignment: superblock does not support data alignment");
--
2.25.1

2022-08-30 14:15:55

by Zeng Heng

[permalink] [raw]
Subject: [PATCH -next 2/2] xfs: simplify if-else condition in xfs_reflink_trim_around_shared

"else" is not generally useful after a return,
so remove it for clean code.

There is no logical changes.

Signed-off-by: Zeng Heng <[email protected]>
---
fs/xfs/xfs_reflink.c | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/fs/xfs/xfs_reflink.c b/fs/xfs/xfs_reflink.c
index 251f20ddd368..93bdd25680bc 100644
--- a/fs/xfs/xfs_reflink.c
+++ b/fs/xfs/xfs_reflink.c
@@ -200,7 +200,9 @@ xfs_reflink_trim_around_shared(
if (fbno == NULLAGBLOCK) {
/* No shared blocks at all. */
return 0;
- } else if (fbno == agbno) {
+ }
+
+ if (fbno == agbno) {
/*
* The start of this extent is shared. Truncate the
* mapping at the end of the shared region so that a
@@ -210,16 +212,16 @@ xfs_reflink_trim_around_shared(
irec->br_blockcount = flen;
*shared = true;
return 0;
- } else {
- /*
- * There's a shared extent midway through this extent.
- * Truncate the mapping at the start of the shared
- * extent so that a subsequent iteration starts at the
- * start of the shared region.
- */
- irec->br_blockcount = fbno - agbno;
- return 0;
}
+
+ /*
+ * There's a shared extent midway through this extent.
+ * Truncate the mapping at the start of the shared
+ * extent so that a subsequent iteration starts at the
+ * start of the shared region.
+ */
+ irec->br_blockcount = fbno - agbno;
+ return 0;
}

int
--
2.25.1

2022-08-30 15:46:14

by Darrick J. Wong

[permalink] [raw]
Subject: Re: [PATCH -next 2/2] xfs: simplify if-else condition in xfs_reflink_trim_around_shared

On Tue, Aug 30, 2022 at 09:39:39PM +0800, Zeng Heng wrote:
> "else" is not generally useful after a return,
> so remove it for clean code.
>
> There is no logical changes.
>
> Signed-off-by: Zeng Heng <[email protected]>

Looks correct to me,
Reviewed-by: Darrick J. Wong <[email protected]>

--D

> ---
> fs/xfs/xfs_reflink.c | 22 ++++++++++++----------
> 1 file changed, 12 insertions(+), 10 deletions(-)
>
> diff --git a/fs/xfs/xfs_reflink.c b/fs/xfs/xfs_reflink.c
> index 251f20ddd368..93bdd25680bc 100644
> --- a/fs/xfs/xfs_reflink.c
> +++ b/fs/xfs/xfs_reflink.c
> @@ -200,7 +200,9 @@ xfs_reflink_trim_around_shared(
> if (fbno == NULLAGBLOCK) {
> /* No shared blocks at all. */
> return 0;
> - } else if (fbno == agbno) {
> + }
> +
> + if (fbno == agbno) {
> /*
> * The start of this extent is shared. Truncate the
> * mapping at the end of the shared region so that a
> @@ -210,16 +212,16 @@ xfs_reflink_trim_around_shared(
> irec->br_blockcount = flen;
> *shared = true;
> return 0;
> - } else {
> - /*
> - * There's a shared extent midway through this extent.
> - * Truncate the mapping at the start of the shared
> - * extent so that a subsequent iteration starts at the
> - * start of the shared region.
> - */
> - irec->br_blockcount = fbno - agbno;
> - return 0;
> }
> +
> + /*
> + * There's a shared extent midway through this extent.
> + * Truncate the mapping at the start of the shared
> + * extent so that a subsequent iteration starts at the
> + * start of the shared region.
> + */
> + irec->br_blockcount = fbno - agbno;
> + return 0;
> }
>
> int
> --
> 2.25.1
>

2022-08-30 16:19:15

by Darrick J. Wong

[permalink] [raw]
Subject: Re: [PATCH -next 1/2] xfs: simplify if-else condition in xfs_validate_new_dalign

On Tue, Aug 30, 2022 at 09:39:38PM +0800, Zeng Heng wrote:
> "else" is not generally useful after a return,
> so remove them which makes if condition a bit
> more clear.
>
> There is no logical changes.
>
> Signed-off-by: Zeng Heng <[email protected]>

Yep.
Reviewed-by: Darrick J. Wong <[email protected]>

--D

> ---
> fs/xfs/xfs_mount.c | 38 ++++++++++++++++++++------------------
> 1 file changed, 20 insertions(+), 18 deletions(-)
>
> diff --git a/fs/xfs/xfs_mount.c b/fs/xfs/xfs_mount.c
> index f10c88cee116..e8bb3c2e847e 100644
> --- a/fs/xfs/xfs_mount.c
> +++ b/fs/xfs/xfs_mount.c
> @@ -300,26 +300,28 @@ xfs_validate_new_dalign(
> "alignment check failed: sunit/swidth vs. blocksize(%d)",
> mp->m_sb.sb_blocksize);
> return -EINVAL;
> - } else {
> - /*
> - * Convert the stripe unit and width to FSBs.
> - */
> - mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
> - if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) {
> - xfs_warn(mp,
> - "alignment check failed: sunit/swidth vs. agsize(%d)",
> - mp->m_sb.sb_agblocks);
> - return -EINVAL;
> - } else if (mp->m_dalign) {
> - mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);
> - } else {
> - xfs_warn(mp,
> - "alignment check failed: sunit(%d) less than bsize(%d)",
> - mp->m_dalign, mp->m_sb.sb_blocksize);
> - return -EINVAL;
> - }
> }
>
> + /*
> + * Convert the stripe unit and width to FSBs.
> + */
> + mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign);
> + if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) {
> + xfs_warn(mp,
> + "alignment check failed: sunit/swidth vs. agsize(%d)",
> + mp->m_sb.sb_agblocks);
> + return -EINVAL;
> + }
> +
> + if (!mp->m_dalign) {
> + xfs_warn(mp,
> + "alignment check failed: sunit(%d) less than bsize(%d)",
> + mp->m_dalign, mp->m_sb.sb_blocksize);
> + return -EINVAL;
> + }
> +
> + mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth);

I think this reorganization of the if test logic is correct, but I
really wish this unit abuse ("m_swidth is a BB for short periods of time
and fsblock everywhere else") would get fixed to simplify analysis and
prevent us from stumbling over things like that some time later.

So for this change,
Reviewed-by: Darrick J. Wong <[email protected]>

But I would be very happy to see some tripping hazard removal. ;)

--D

> +
> if (!xfs_has_dalign(mp)) {
> xfs_warn(mp,
> "cannot change alignment: superblock does not support data alignment");
> --
> 2.25.1
>