From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v4 3/3] btrfs-progs: handle error in the btrfs_prepare_device
Date: Wed, 18 Dec 2013 12:07:55 +0800 [thread overview]
Message-ID: <1387339675-29543-1-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1387279007-10388-3-git-send-email-anand.jain@oracle.com>
this patch will handle the strerror reporting of the error instead of
printing errno, and also replaced the BUG_ON with the error handling
Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
v4: replaced ? statement with proper if statement
v3: fix per Stefan review, update error message
v2: commit update
cmds-device.c | 7 +++----
cmds-replace.c | 9 ++++-----
mkfs.c | 9 ++++++++-
utils.c | 31 ++++++++++++++++++++-----------
4 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/cmds-device.c b/cmds-device.c
index bc4a8dc..ada0bcd 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -111,13 +111,11 @@ static int cmd_add_dev(int argc, char **argv)
res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
0, &mixed, discard);
+ close(devfd);
if (res) {
- fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]);
- close(devfd);
ret++;
- continue;
+ goto error_out;
}
- close(devfd);
strncpy_null(ioctl_args.name, argv[i]);
res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
@@ -130,6 +128,7 @@ static int cmd_add_dev(int argc, char **argv)
}
+error_out:
close_file_or_dir(fdmnt, dirstream);
return !!ret;
}
diff --git a/cmds-replace.c b/cmds-replace.c
index d9b0940..c683d6c 100644
--- a/cmds-replace.c
+++ b/cmds-replace.c
@@ -276,12 +276,11 @@ static int cmd_start_replace(int argc, char **argv)
}
strncpy((char *)start_args.start.tgtdev_name, dstdev,
BTRFS_DEVICE_PATH_NAME_MAX);
- if (btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0,
- &mixed, 0)) {
- fprintf(stderr, "Error: Failed to prepare device '%s'\n",
- dstdev);
+ ret = btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0,
+ &mixed, 0);
+ if (ret)
goto leave_with_error;
- }
+
close(fddstdev);
fddstdev = -1;
diff --git a/mkfs.c b/mkfs.c
index 33369f9..18df087 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -1446,6 +1446,10 @@ int main(int ac, char **av)
first_file = file;
ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
block_count, &mixed, discard);
+ if (ret) {
+ close(fd);
+ exit(1);
+ }
if (block_count && block_count > dev_block_count) {
fprintf(stderr, "%s is smaller than requested size\n", file);
exit(1);
@@ -1553,8 +1557,11 @@ int main(int ac, char **av)
}
ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
block_count, &mixed, discard);
+ if (ret) {
+ close(fd);
+ exit(1);
+ }
mixed = old_mixed;
- BUG_ON(ret);
ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
sectorsize, sectorsize, sectorsize);
diff --git a/utils.c b/utils.c
index f499023..f37083a 100644
--- a/utils.c
+++ b/utils.c
@@ -581,13 +581,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
ret = fstat(fd, &st);
if (ret < 0) {
fprintf(stderr, "unable to stat %s\n", file);
- exit(1);
+ return 1;
}
block_count = btrfs_device_size(fd, &st);
if (block_count == 0) {
fprintf(stderr, "unable to find %s size\n", file);
- exit(1);
+ return 1;
}
if (max_block_count)
block_count = min(block_count, max_block_count);
@@ -612,26 +612,35 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
}
ret = zero_dev_start(fd);
- if (ret) {
- fprintf(stderr, "failed to zero device start %d\n", ret);
- exit(1);
- }
+ if (ret)
+ goto zero_dev_error;
for (i = 0 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
bytenr = btrfs_sb_offset(i);
if (bytenr >= block_count)
break;
- zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
+ ret = zero_blocks(fd, bytenr, BTRFS_SUPER_INFO_SIZE);
+ if (ret)
+ goto zero_dev_error;
}
if (zero_end) {
ret = zero_dev_end(fd, block_count);
- if (ret) {
- fprintf(stderr, "failed to zero device end %d\n", ret);
- exit(1);
- }
+ if (ret)
+ goto zero_dev_error;
}
*block_count_ret = block_count;
+
+zero_dev_error:
+ if (ret < 0) {
+ fprintf(stderr, "ERROR: failed to zero device '%s' - %s\n",
+ file, strerror(-ret));
+ return 1;
+ } else if (ret > 0) {
+ fprintf(stderr, "ERROR: failed to zero device '%s' - %d\n",
+ file, ret);
+ return 1;
+ }
return 0;
}
--
1.7.1
prev parent reply other threads:[~2013-12-18 3:59 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-17 11:16 [PATCH v2 1/3] btrfs-progs: don't replicate the stripe_len defines Anand Jain
2013-12-17 11:16 ` [PATCH v3 2/3] btrfs-progs: use stripe_len define here Anand Jain
2013-12-17 11:16 ` [PATCH v3 3/3] btrfs-progs: handle error in the btrfs_prepare_device Anand Jain
2013-12-18 4:07 ` Anand Jain [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=1387339675-29543-1-git-send-email-anand.jain@oracle.com \
--to=anand.jain@oracle.com \
--cc=linux-btrfs@vger.kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).