linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Liu Bo <bo.li.liu@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH] Btrfs-progs: elaborate error handling of mkfs
Date: Sat,  8 Jun 2013 17:45:14 +0800	[thread overview]
Message-ID: <1370684714-30373-1-git-send-email-bo.li.liu@oracle.com> (raw)

$./mkfs.btrfs -f /dev/sdd -b 2M
[...]
mkfs.btrfs: volumes.c:845: btrfs_alloc_chunk: Assertion `!(ret)' failed.
Aborted (core dumped).

We should return error to userspace instead of the above.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
---
 mkfs.c    |   23 +++++++++++++++--------
 volumes.c |   16 +++++++++++-----
 2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/mkfs.c b/mkfs.c
index 7ff60e5..e3789cc 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -74,31 +74,36 @@ static int make_root_dir(struct btrfs_root *root, int mixed)
 				     BTRFS_BLOCK_GROUP_SYSTEM,
 				     BTRFS_FIRST_CHUNK_TREE_OBJECTID,
 				     0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
-	BUG_ON(ret);
+	if (ret)
+		goto err;
 
 	if (mixed) {
 		ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
 					&chunk_start, &chunk_size,
 					BTRFS_BLOCK_GROUP_METADATA |
 					BTRFS_BLOCK_GROUP_DATA);
-		BUG_ON(ret);
+		if (ret)
+			goto err;
 		ret = btrfs_make_block_group(trans, root, 0,
 					     BTRFS_BLOCK_GROUP_METADATA |
 					     BTRFS_BLOCK_GROUP_DATA,
 					     BTRFS_FIRST_CHUNK_TREE_OBJECTID,
 					     chunk_start, chunk_size);
-		BUG_ON(ret);
+		if (ret)
+			goto err;
 		printf("Created a data/metadata chunk of size %llu\n", chunk_size);
 	} else {
 		ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
 					&chunk_start, &chunk_size,
 					BTRFS_BLOCK_GROUP_METADATA);
-		BUG_ON(ret);
+		if (ret)
+			goto err;
 		ret = btrfs_make_block_group(trans, root, 0,
 					     BTRFS_BLOCK_GROUP_METADATA,
 					     BTRFS_FIRST_CHUNK_TREE_OBJECTID,
 					     chunk_start, chunk_size);
-		BUG_ON(ret);
+		if (ret)
+			goto err;
 	}
 
 	root->fs_info->system_allocs = 0;
@@ -110,12 +115,14 @@ static int make_root_dir(struct btrfs_root *root, int mixed)
 		ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
 					&chunk_start, &chunk_size,
 					BTRFS_BLOCK_GROUP_DATA);
-		BUG_ON(ret);
+		if (ret)
+			goto err;
 		ret = btrfs_make_block_group(trans, root, 0,
 					     BTRFS_BLOCK_GROUP_DATA,
 					     BTRFS_FIRST_CHUNK_TREE_OBJECTID,
 					     chunk_start, chunk_size);
-		BUG_ON(ret);
+		if (ret)
+			goto err;
 	}
 
 	ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
@@ -1460,7 +1467,7 @@ int main(int ac, char **av)
 
 	ret = make_root_dir(root, mixed);
 	if (ret) {
-		fprintf(stderr, "failed to setup the root directory\n");
+		fprintf(stderr, "failed to setup the root directory %d\n", ret);
 		exit(1);
 	}
 
diff --git a/volumes.c b/volumes.c
index d6f81f8..8285240 100644
--- a/volumes.c
+++ b/volumes.c
@@ -842,11 +842,13 @@ again:
 			     info->chunk_root->root_key.objectid,
 			     BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
 			     calc_size, &dev_offset);
-		BUG_ON(ret);
+		if (ret)
+			goto out;
 
 		device->bytes_used += calc_size;
 		ret = btrfs_update_device(trans, device);
-		BUG_ON(ret);
+		if (ret)
+			goto out;
 
 		map->stripes[index].dev = device;
 		map->stripes[index].physical = dev_offset;
@@ -878,7 +880,6 @@ again:
 
 	ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
 				btrfs_chunk_item_size(num_stripes));
-	BUG_ON(ret);
 	*start = key.offset;;
 
 	map->ce.start = key.offset;
@@ -887,14 +888,19 @@ again:
 	ret = insert_existing_cache_extent(
 			   &extent_root->fs_info->mapping_tree.cache_tree,
 			   &map->ce);
-	BUG_ON(ret);
+	if (ret)
+		goto out;
 
 	if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
 		ret = btrfs_add_system_chunk(trans, chunk_root, &key,
 				    chunk, btrfs_chunk_item_size(num_stripes));
-		BUG_ON(ret);
+		if (ret)
+			goto out;
 	}
 
+out:
+	if (ret)
+		kfree(map);
 	kfree(chunk);
 	return ret;
 }
-- 
1.7.7


                 reply	other threads:[~2013-06-08  9:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1370684714-30373-1-git-send-email-bo.li.liu@oracle.com \
    --to=bo.li.liu@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).