linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Filipe David Borba Manana <fdmanana@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Filipe David Borba Manana <fdmanana@gmail.com>
Subject: [PATCH 4/5] Btrfs-progs: return error on write failure in make_btrfs()
Date: Thu,  4 Jul 2013 10:48:39 +0100	[thread overview]
Message-ID: <1372931320-8340-2-git-send-email-fdmanana@gmail.com> (raw)
In-Reply-To: <1372931320-8340-1-git-send-email-fdmanana@gmail.com>

Instead of aborting with a BUG_ON() statement, return a
negated errno code. Also updated mkfs and convert tools
to print a nicer error message when make_btrfs() returns
an error.

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
---
 btrfs-convert.c |    3 ++-
 mkfs.c          |    2 +-
 utils.c         |   36 ++++++++++++++++++++++++++++--------
 3 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/btrfs-convert.c b/btrfs-convert.c
index 399856f..0e66eb9 100644
--- a/btrfs-convert.c
+++ b/btrfs-convert.c
@@ -2323,7 +2323,8 @@ int do_convert(const char *devname, int datacsum, int packing, int noxattr)
 			 blocks, total_bytes, blocksize, blocksize,
 			 blocksize, blocksize);
 	if (ret) {
-		fprintf(stderr, "unable to create initial ctree\n");
+		fprintf(stderr, "unable to create initial ctree: %s\n",
+			strerror(-ret));
 		goto fail;
 	}
 	/* create a system chunk that maps the whole device */
diff --git a/mkfs.c b/mkfs.c
index 7ff60e5..348bcaa 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -1446,7 +1446,7 @@ int main(int ac, char **av)
 			 nodesize, leafsize,
 			 sectorsize, stripesize);
 	if (ret) {
-		fprintf(stderr, "error during mkfs %d\n", ret);
+		fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
 		exit(1);
 	}
 
diff --git a/utils.c b/utils.c
index 702a0b1..f43816b 100644
--- a/utils.c
+++ b/utils.c
@@ -215,7 +215,10 @@ int make_btrfs(int fd, const char *device, const char *label,
 
 	csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
 	ret = pwrite(fd, buf->data, leafsize, blocks[1]);
-	BUG_ON(ret != leafsize);
+	if (ret < 0)
+		return -errno;
+	else if (ret != leafsize)
+		return -EIO;
 
 	/* create the items for the extent tree */
 	memset(buf->data+sizeof(struct btrfs_header), 0,
@@ -262,7 +265,10 @@ int make_btrfs(int fd, const char *device, const char *label,
 	btrfs_set_header_nritems(buf, nritems);
 	csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
 	ret = pwrite(fd, buf->data, leafsize, blocks[2]);
-	BUG_ON(ret != leafsize);
+	if (ret < 0)
+		return -errno;
+	else if (ret != leafsize)
+		return -EIO;
 
 	/* create the chunk tree */
 	memset(buf->data+sizeof(struct btrfs_header), 0,
@@ -346,7 +352,10 @@ int make_btrfs(int fd, const char *device, const char *label,
 	btrfs_set_header_nritems(buf, nritems);
 	csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
 	ret = pwrite(fd, buf->data, leafsize, blocks[3]);
-	BUG_ON(ret != leafsize);
+	if (ret < 0)
+		return -errno;
+	else if (ret != leafsize)
+		return -EIO;
 
 	/* create the device tree */
 	memset(buf->data+sizeof(struct btrfs_header), 0,
@@ -382,7 +391,10 @@ int make_btrfs(int fd, const char *device, const char *label,
 	btrfs_set_header_nritems(buf, nritems);
 	csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
 	ret = pwrite(fd, buf->data, leafsize, blocks[4]);
-	BUG_ON(ret != leafsize);
+	if (ret < 0)
+		return -errno;
+	else if (ret != leafsize)
+		return -EIO;
 
 	/* create the FS root */
 	memset(buf->data+sizeof(struct btrfs_header), 0,
@@ -392,7 +404,10 @@ int make_btrfs(int fd, const char *device, const char *label,
 	btrfs_set_header_nritems(buf, 0);
 	csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
 	ret = pwrite(fd, buf->data, leafsize, blocks[5]);
-	BUG_ON(ret != leafsize);
+	if (ret < 0)
+		return -errno;
+	else if (ret != leafsize)
+		return -EIO;
 
 	/* finally create the csum root */
 	memset(buf->data+sizeof(struct btrfs_header), 0,
@@ -402,7 +417,10 @@ int make_btrfs(int fd, const char *device, const char *label,
 	btrfs_set_header_nritems(buf, 0);
 	csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
 	ret = pwrite(fd, buf->data, leafsize, blocks[6]);
-	BUG_ON(ret != leafsize);
+	if (ret < 0)
+		return -errno;
+	else if (ret != leafsize)
+		return -EIO;
 
 	/* and write out the super block */
 	BUG_ON(sizeof(super) > sectorsize);
@@ -411,8 +429,10 @@ int make_btrfs(int fd, const char *device, const char *label,
 	buf->len = sectorsize;
 	csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
 	ret = pwrite(fd, buf->data, sectorsize, blocks[0]);
-	BUG_ON(ret != sectorsize);
-
+	if (ret < 0)
+		return -errno;
+	else if (ret != leafsize)
+		return -EIO;
 
 	free(buf);
 	return 0;
-- 
1.7.9.5


  reply	other threads:[~2013-07-04  9:49 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-30 11:51 [PATCH 0/3] Small fixes for btrfs-progs Filipe David Borba Manana
2013-06-30 11:51 ` [PATCH 1/3] Btrfs-progs: add missing write check for mkfs Filipe David Borba Manana
2013-07-03 17:09   ` David Sterba
2013-07-03 17:25     ` Filipe David Manana
2013-07-03 21:12       ` David Sterba
2013-06-30 11:51 ` [PATCH 2/3] Btrfs-progs: add kstrdup() return value check Filipe David Borba Manana
2013-06-30 11:51 ` [PATCH 3/3] Btrfs-progs: remove unused code Filipe David Borba Manana
2013-07-01  8:21   ` Anand Jain
2013-07-03 17:30 ` [PATCH v2 1/3] Btrfs-progs: add missing write check for mkfs Filipe David Borba Manana
2013-07-03 17:32 ` [PATCH v2 3/3] Btrfs-progs: remove unused code Filipe David Borba Manana
2013-07-04  9:48 ` [PATCH v2 0/5] Small fixes for btrfs-progs Filipe David Borba Manana
2013-07-04  9:48   ` Filipe David Borba Manana [this message]
2013-08-08 10:52     ` [PATCH 4/5] Btrfs-progs: return error on write failure in make_btrfs() Stefan Behrens
2013-08-08 10:56       ` Filipe David Manana
2013-07-04  9:48   ` [PATCH 5/5] Btrfs-progs: don't ignore errors in btrfs_add_block_group() Filipe David Borba Manana
2013-07-08 13:21 ` [PATCH v2 5/5] Btrfs-progs: don't ignore errors in extent-tree.c Filipe David Borba Manana

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=1372931320-8340-2-git-send-email-fdmanana@gmail.com \
    --to=fdmanana@gmail.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).