From: jes.sorensen@gmail.com
To: Zhilong Liu <zlliu@suse.com>
Cc: linux-raid@vger.kernel.org
Subject: Re: [PATCH 1/4] mdadm/util:integrate stat operations into one utility
Date: Wed, 05 Apr 2017 11:42:35 -0400 [thread overview]
Message-ID: <wrfj8tneeu8k.fsf@gmail.com> (raw)
In-Reply-To: <20170401125145.14440-2-zlliu@suse.com> (Zhilong Liu's message of "Sat, 1 Apr 2017 20:51:42 +0800")
Zhilong Liu <zlliu@suse.com> writes:
> mdadm/util: there are dupilicate codes about stat checking the
> block device, move the operations into one utility function and
> make it concise.
>
> Signed-off-by: Zhilong Liu <zlliu@suse.com>
> ---
> Assemble.c | 5 ++---
> Build.c | 21 +++------------------
> Incremental.c | 12 +-----------
> Manage.c | 10 ++--------
> mdadm.h | 1 +
> super-intel.c | 4 +---
> util.c | 15 +++++++++++++++
> 7 files changed, 25 insertions(+), 43 deletions(-)
>
> diff --git a/Assemble.c b/Assemble.c
> index 672cd12..c6571e6 100644
> --- a/Assemble.c
> +++ b/Assemble.c
> @@ -522,9 +522,8 @@ static int select_devices(struct mddev_dev *devlist,
> struct stat stb;
> if (tmpdev->used != 3)
> continue;
> - if (stat(tmpdev->devname, &stb)< 0) {
> - pr_err("fstat failed for %s: %s\n",
> - tmpdev->devname, strerror(errno));
> + if (check_blkdev_via_stat(tmpdev->devname) &&
> + stat(tmpdev->devname, &stb)) {
> tmpdev->used = 2;
> } else {
> struct dev_policy *pol = devid_policy(stb.st_rdev);
Sorry, but this change makes no sense. First calling a function using
stat() to check, and then call stat() one more time to get the stat data
anyway.
In fact, looking through this patch, you are doing it all over the
place.
This makes things worse, not better!
> diff --git a/Build.c b/Build.c
> index 691dd6f..b500ad7 100644
> --- a/Build.c
> +++ b/Build.c
> @@ -67,16 +67,8 @@ int Build(char *mddev, struct mddev_dev *devlist,
> missing_disks++;
> continue;
> }
> - if (stat(dv->devname, &stb)) {
> - pr_err("Cannot find %s: %s\n",
> - dv->devname, strerror(errno));
> - return 1;
> - }
> - if ((stb.st_mode & S_IFMT) != S_IFBLK) {
> - pr_err("%s is not a block device.\n",
> - dv->devname);
> + if (check_blkdev_via_stat(dv->devname))
> return 1;
> - }
> }
>
> if (s->raiddisks != subdevs) {
> @@ -171,16 +163,9 @@ int Build(char *mddev, struct mddev_dev *devlist,
> int fd;
> if (strcmp("missing", dv->devname) == 0)
> continue;
> - if (stat(dv->devname, &stb)) {
> - pr_err("Weird: %s has disappeared.\n",
> - dv->devname);
> + if (check_blkdev_via_stat(dv->devname))
> goto abort;
> - }
> - if ((stb.st_mode & S_IFMT)!= S_IFBLK) {
> - pr_err("Weird: %s is no longer a block device.\n",
> - dv->devname);
> - goto abort;
> - }
> + stat(dv->devname, &stb);
> fd = open(dv->devname, O_RDONLY|O_EXCL);
> if (fd < 0) {
> pr_err("Cannot open %s: %s\n",
> diff --git a/Incremental.c b/Incremental.c
> index 28f1f77..78adf18 100644
> --- a/Incremental.c
> +++ b/Incremental.c
> @@ -108,18 +108,8 @@ int Incremental(struct mddev_dev *devlist, struct context *c,
>
> struct createinfo *ci = conf_get_create_info();
>
> - if (stat(devname, &stb) < 0) {
> - if (c->verbose >= 0)
> - pr_err("stat failed for %s: %s.\n",
> - devname, strerror(errno));
> + if (check_blkdev_via_stat(devname) && stat(devname, &stb))
> return rv;
> - }
> - if ((stb.st_mode & S_IFMT) != S_IFBLK) {
> - if (c->verbose >= 0)
> - pr_err("%s is not a block device.\n",
> - devname);
> - return rv;
> - }
> dfd = dev_open(devname, O_RDONLY);
> if (dfd < 0) {
> if (c->verbose >= 0)
> diff --git a/Manage.c b/Manage.c
> index 618c98b..6aec65d 100644
> --- a/Manage.c
> +++ b/Manage.c
> @@ -1544,17 +1544,11 @@ int Manage_subdevs(char *devname, int fd,
> close(tfd);
> } else {
> int open_err = errno;
> - if (stat(dv->devname, &stb) != 0) {
> - pr_err("Cannot find %s: %s\n",
> - dv->devname, strerror(errno));
> - goto abort;
> - }
> - if ((stb.st_mode & S_IFMT) != S_IFBLK) {
> + if (check_blkdev_via_stat(dv->devname) &&
> + stat(dv->devname, &stb)) {
> if (dv->disposition == 'M')
> /* non-fatal. Also improbable */
> continue;
> - pr_err("%s is not a block device.\n",
> - dv->devname);
> goto abort;
> }
> if (dv->disposition == 'r')
> diff --git a/mdadm.h b/mdadm.h
> index 612bd86..0aea822 100644
> --- a/mdadm.h
> +++ b/mdadm.h
> @@ -1422,6 +1422,7 @@ extern int check_raid(int fd, char *name);
> extern int check_partitions(int fd, char *dname,
> unsigned long long freesize,
> unsigned long long size);
> +extern int check_blkdev_via_stat(char *dev);
>
> extern int get_mdp_major(void);
> extern int get_maj_min(char *dev, int *major, int *minor);
> diff --git a/super-intel.c b/super-intel.c
> index 84dfe2b..924ad8a 100644
> --- a/super-intel.c
> +++ b/super-intel.c
> @@ -6953,9 +6953,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
> }
>
> /* This device must be a member of the set */
> - if (stat(dev, &stb) < 0)
> - return 0;
> - if ((S_IFMT & stb.st_mode) != S_IFBLK)
> + if (check_blkdev_via_stat(dev) && stat(dev, &stb))
> return 0;
> for (dl = super->disks ; dl ; dl = dl->next) {
> if (dl->major == (int)major(stb.st_rdev) &&
> diff --git a/util.c b/util.c
> index 9fc7ba0..03d4256 100644
> --- a/util.c
> +++ b/util.c
> @@ -774,6 +774,21 @@ int ask(char *mesg)
> }
> #endif /* MDASSEMBLE */
>
> +int check_blkdev_via_stat(char *dev)
> +{
> + struct stat stb;
> +
> + if (stat(dev, &stb) != 0) {
> + pr_err("stat failed for %s: %s\n", dev, strerror(errno));
> + return -1;
> + }
> + if ((S_IFMT & stb.st_mode) != S_IFBLK) {
> + pr_err("%s is not a block device.\n", dev);
> + return -1;
> + }
> + return 0;
> +}
> +
For a function like this, lets name it better md_is_blkdev()
Jes
next prev parent reply other threads:[~2017-04-05 15:42 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-01 12:51 [PATCH 0/4] integrated stat and fstat into utility functions Zhilong Liu
2017-04-01 12:51 ` [PATCH 1/4] mdadm/util:integrate stat operations into one utility Zhilong Liu
2017-04-05 15:42 ` jes.sorensen [this message]
2017-04-14 10:14 ` Liu Zhilong
2017-04-14 15:20 ` Jes Sorensen
2017-04-17 7:08 ` Liu Zhilong
2017-04-17 7:18 ` Zhilong Liu
2017-04-20 16:42 ` Jes Sorensen
2017-04-01 12:51 ` [PATCH 2/4] mdadm/util:integrate fstat operations into one utility function Zhilong Liu
2017-04-05 15:43 ` jes.sorensen
2017-04-01 12:51 ` [PATCH 3/4] mdadm/Create:declaring an existing struct within same function Zhilong Liu
2017-04-05 15:47 ` jes.sorensen
2017-04-01 12:51 ` [PATCH 4/4] mdadm/Monitor:check the block device when use waitclean parameter Zhilong Liu
2017-04-14 12:31 ` [PATCH 0/4] integrated stat and fstat into utility functions Paul Menzel
2017-04-17 2:23 ` Zhilong Liu
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=wrfj8tneeu8k.fsf@gmail.com \
--to=jes.sorensen@gmail.com \
--cc=linux-raid@vger.kernel.org \
--cc=zlliu@suse.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.