public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Dave Chinner <david@fromorbit.com>
To: Stephen Zhang <starzhangzsd@gmail.com>
Cc: djwong@kernel.org, dchinner@redhat.com, chandan.babu@oracle.com,
	zhangshida@kylinos.cn, linux-xfs@vger.kernel.org
Subject: Re: [PATCH] xfs: fix up the comment in xfs_dir2_isleaf
Date: Mon, 12 Sep 2022 15:00:48 +1000	[thread overview]
Message-ID: <20220912050048.GC3600936@dread.disaster.area> (raw)
In-Reply-To: <CANubcdUrZQTQnokcb8FUm31sgUToriaS1uNVXNYvNyeZ+ZUHkA@mail.gmail.com>

On Mon, Sep 12, 2022 at 11:14:51AM +0800, Stephen Zhang wrote:
> Dave Chinner <david@fromorbit.com> 于2022年9月12日周一 06:20写道:
> >
> > The "*vp" parameter should be a "bool *isleaf", in which case the
> > return value is obvious and the comment can be removed. Then the
> > logic in the function can be cleaned up to be obvious instead of
> > relying on easy to mistake conditional logic in assignemnts...
> 
> Thanks for the suggestion.In order to make sure we are at the same page,
> so this change will be shown like:

That's not what I was thinking. Cleanup involves converting
everything over to standard formatting and conventions. It also
means rethinking the logic to make the code more correct, easier to
read and understand, and so involves more than just changing the
name of a variable.

> ====
>  xfs_dir2_isblock(
>         struct xfs_da_args      *args,
> -       int                     *vp)    /* out: 1 is block, 0 is not block */
> +       bool                    *isblock)
>  {
>         xfs_fileoff_t           last;   /* last file offset */
>         int                     rval;
> 
>         if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
>                 return rval;

We don't put assingments in if statements anymore, so this needs to
be rewritten in the form:

	error = foo();
	if (error) {
		....
		return error;
	}

> -       rval = XFS_FSB_TO_B(args->dp->i_mount, last) == args->geo->blksize;
> +       *isblock = XFS_FSB_TO_B(args->dp->i_mount, last) == args->geo->blksize;

Similarly, we don't elide if() statements in this way anymore,
because it's easy to mistake this code as a multiple assignment
rather than a combination of assignment and logic. if() is much
clearer.

>         if (XFS_IS_CORRUPT(args->dp->i_mount,
> -                          rval != 0 &&
> +                          *isblock &&
>                            args->dp->i_disk_size != args->geo->blksize))
>                 return -EFSCORRUPTED;

And this only ever evaluates as true if *isblock is true, so why
run this logic check when *isblock is false?

IOWs, we can rearrange the logic so that it's made up of simple,
individual single comparisons that are obviously self documenting:

int
xfs_dir2_isblock(
	struct xfs_da_args	*args,
	bool			*isblock)
{
	struct xfs_mount	*mp = args->dp->i_mount;
	xfs_fileoff_t		eof;
	int			error;

	error = xfs_bmap_last_offset(args->dp, &eof, XFS_DATA_FORK);
	if (error)
		return error;

	*isblock = false;
	if (XFS_FSB_TO_B(mp, eof) != args->geo->blksize)
		return 0;

	*isblock = true;
	if (XFS_IS_CORRUPT(mp, args->dp->i_disk_size != args->geo->blksize))
		return -EFSCORRUPTED;
	return 0;
}


-- 
Dave Chinner
david@fromorbit.com

      reply	other threads:[~2022-09-12  5:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-11  3:31 [PATCH] xfs: fix up the comment in xfs_dir2_isleaf Stephen Zhang
2022-09-11 22:20 ` Dave Chinner
2022-09-12  3:14   ` Stephen Zhang
2022-09-12  5:00     ` Dave Chinner [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220912050048.GC3600936@dread.disaster.area \
    --to=david@fromorbit.com \
    --cc=chandan.babu@oracle.com \
    --cc=dchinner@redhat.com \
    --cc=djwong@kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=starzhangzsd@gmail.com \
    --cc=zhangshida@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox