Fix "--remove faulty" and similar commands.

A recent change to improve error messages for subdev management broken
all use cases were device names like %d:%d were used.
Re-arrange the code again so we use dev_open first - which understands
those names - and then only try 'stat' if that failed.
The important thing is to base the 'Cannot find' message on the result
of 'stat', not on the result of 'open'.

Signed-off-by: NeilBrown <neilb@suse.de>
This commit is contained in:
NeilBrown 2012-11-28 10:12:09 +11:00
parent 66eb2c93a6
commit 5fe7f5f7c8
1 changed files with 23 additions and 24 deletions

View File

@ -1205,38 +1205,37 @@ int Manage_subdevs(char *devname, int fd,
} }
} }
} else { } else {
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 (dv->disposition == 'M')
/* non-fatal. Also improbable */
continue;
pr_err("%s is not a block device.\n",
dv->devname);
goto abort;
}
tfd = dev_open(dv->devname, O_RDONLY); tfd = dev_open(dv->devname, O_RDONLY);
if (tfd < 0 && dv->disposition == 'r') if (tfd >= 0)
/* Be happy, the stat worked, that is fstat(tfd, &stb);
* enough for --remove
*/
;
else { else {
if (tfd < 0 || fstat(tfd, &stb) != 0) { int open_err = errno;
if (tfd >= 0) if (stat(dv->devname, &stb) != 0) {
close(tfd); pr_err("Cannot find %s: %s\n",
dv->devname, strerror(errno));
goto abort;
}
if ((stb.st_mode & S_IFMT) != S_IFBLK) {
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')
/* Be happy, the stat worked, that is
* enough for --remove
*/
;
else {
if (dv->disposition == 'M') if (dv->disposition == 'M')
/* non-fatal */ /* non-fatal */
continue; continue;
pr_err("Cannot open %s: %s\n", pr_err("Cannot open %s: %s\n",
dv->devname, strerror(errno)); dv->devname, strerror(open_err));
goto abort; goto abort;
} }
close(tfd);
tfd = -1;
} }
} }
switch(dv->disposition){ switch(dv->disposition){