Btrfs-progs: return error on write failure in make_btrfs()

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>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
This commit is contained in:
Filipe David Borba Manana 2013-07-04 10:48:39 +01:00 committed by David Sterba
parent 80bab0f18c
commit 2e9adfb909
3 changed files with 31 additions and 10 deletions

View File

@ -2323,7 +2323,8 @@ int do_convert(const char *devname, int datacsum, int packing, int noxattr)
blocks, total_bytes, blocksize, blocksize, blocks, total_bytes, blocksize, blocksize,
blocksize, blocksize); blocksize, blocksize);
if (ret) { if (ret) {
fprintf(stderr, "unable to create initial ctree\n"); fprintf(stderr, "unable to create initial ctree: %s\n",
strerror(-ret));
goto fail; goto fail;
} }
/* create a system chunk that maps the whole device */ /* create a system chunk that maps the whole device */

2
mkfs.c
View File

@ -1534,7 +1534,7 @@ int main(int ac, char **av)
nodesize, leafsize, nodesize, leafsize,
sectorsize, stripesize); sectorsize, stripesize);
if (ret) { if (ret) {
fprintf(stderr, "error during mkfs %d\n", ret); fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
exit(1); exit(1);
} }

36
utils.c
View File

@ -215,7 +215,10 @@ int make_btrfs(int fd, const char *device, const char *label,
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
ret = pwrite(fd, buf->data, leafsize, blocks[1]); 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 */ /* create the items for the extent tree */
memset(buf->data+sizeof(struct btrfs_header), 0, 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); btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
ret = pwrite(fd, buf->data, leafsize, blocks[2]); 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 */ /* create the chunk tree */
memset(buf->data+sizeof(struct btrfs_header), 0, 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); btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
ret = pwrite(fd, buf->data, leafsize, blocks[3]); 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 */ /* create the device tree */
memset(buf->data+sizeof(struct btrfs_header), 0, 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); btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
ret = pwrite(fd, buf->data, leafsize, blocks[4]); 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 */ /* create the FS root */
memset(buf->data+sizeof(struct btrfs_header), 0, 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); btrfs_set_header_nritems(buf, 0);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
ret = pwrite(fd, buf->data, leafsize, blocks[5]); 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 */ /* finally create the csum root */
memset(buf->data+sizeof(struct btrfs_header), 0, 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); btrfs_set_header_nritems(buf, 0);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
ret = pwrite(fd, buf->data, leafsize, blocks[6]); 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 */ /* and write out the super block */
BUG_ON(sizeof(super) > sectorsize); BUG_ON(sizeof(super) > sectorsize);
@ -411,8 +429,10 @@ int make_btrfs(int fd, const char *device, const char *label,
buf->len = sectorsize; buf->len = sectorsize;
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0); csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
ret = pwrite(fd, buf->data, sectorsize, blocks[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); free(buf);
return 0; return 0;