btrfs-progs: add crude error handling when transaction start fails

Currently transaction bugs out insided btrfs_start_transaction in case
of error, we want to lift the error handling to the callers. This patch
adds the BUG_ON anywhere it's been missing so far. This is not the best
way of course. Transforming BUG_ON to a proper error handling highly
depends on the caller and should be dealt with case by case.

Signed-off-by: David Sterba <dsterba@suse.com>
master
David Sterba 2017-08-28 16:48:16 +02:00
parent 4ef5a112c9
commit 448999d84d
14 changed files with 48 additions and 22 deletions

View File

@ -1226,13 +1226,16 @@ int main(int argc, char **argv)
if (logical == (u64)-1)
print_usage(1);
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
ret = corrupt_extent(trans, root, logical);
btrfs_commit_transaction(trans, root);
goto out_close;
}
if (extent_tree) {
struct btrfs_trans_handle *trans;
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root,
root->fs_info->extent_root->node);
btrfs_commit_transaction(trans, root);
@ -1258,6 +1261,7 @@ int main(int argc, char **argv)
goto out_close;
}
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
ret = corrupt_item_nocow(trans, root->fs_info->chunk_root,
path, del);
if (ret < 0)
@ -1267,7 +1271,9 @@ int main(int argc, char **argv)
}
if (chunk_tree) {
struct btrfs_trans_handle *trans;
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
ret = corrupt_chunk_tree(trans, root->fs_info->chunk_root);
if (ret < 0)
fprintf(stderr, "Failed to corrupt chunk tree\n");
@ -1281,6 +1287,7 @@ int main(int argc, char **argv)
print_usage(1);
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
if (file_extent == (u64)-1) {
printf("corrupting inode\n");
ret = corrupt_inode(trans, root, inode, field);

View File

@ -68,6 +68,7 @@ int main(int argc, char **argv)
(unsigned long long)btrfs_super_log_root(sb),
(unsigned)btrfs_super_log_root_level(sb));
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
btrfs_set_super_log_root(root->fs_info->super_copy, 0);
btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
btrfs_commit_transaction(trans, root);

View File

@ -66,6 +66,7 @@ static int update_seeding_flag(struct btrfs_root *root, int set_flag)
}
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
btrfs_set_super_flags(disk_super, super_flags);
ret = btrfs_commit_transaction(trans, root);
@ -83,6 +84,7 @@ static int set_super_incompat_flags(struct btrfs_root *root, u64 flags)
super_flags = btrfs_super_incompat_flags(disk_super);
super_flags |= flags;
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
btrfs_set_super_incompat_flags(disk_super, super_flags);
ret = btrfs_commit_transaction(trans, root);

View File

@ -2389,6 +2389,7 @@ int btrfs_recover_chunk_tree(char *path, int verbose, int yes)
}
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
ret = remove_chunk_extent_item(trans, &rc, root);
BUG_ON(ret);

View File

@ -191,6 +191,7 @@ static int cmd_rescue_zero_log(int argc, char **argv)
(unsigned long long)btrfs_super_log_root(sb),
(unsigned)btrfs_super_log_root_level(sb));
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
btrfs_set_super_log_root(sb, 0);
btrfs_set_super_log_root_level(sb, 0);
btrfs_commit_transaction(trans, root);

View File

@ -751,8 +751,8 @@ static int create_image(struct btrfs_root *root,
flags |= BTRFS_INODE_NODATASUM;
trans = btrfs_start_transaction(root, 1);
if (!trans)
return -ENOMEM;
if (IS_ERR(trans))
return PTR_ERR(trans);
cache_tree_init(&used_tmp);
btrfs_init_path(&path);
@ -881,7 +881,7 @@ static struct btrfs_root* link_subvol(struct btrfs_root *root,
btrfs_release_path(&path);
trans = btrfs_start_transaction(root, 1);
if (!trans) {
if (IS_ERR(trans)) {
error("unable to start transaction");
goto fail;
}
@ -1077,9 +1077,9 @@ static int init_btrfs(struct btrfs_mkfs_config *cfg, struct btrfs_root *root,
fs_info->avoid_sys_chunk_alloc = 1;
fs_info->avoid_meta_chunk_alloc = 1;
trans = btrfs_start_transaction(root, 1);
if (!trans) {
if (IS_ERR(trans)) {
error("unable to start transaction");
ret = -EINVAL;
ret = PTR_ERR(trans);
goto err;
}
ret = btrfs_fix_block_accounting(trans, root);

View File

@ -803,8 +803,8 @@ static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
struct btrfs_trans_handle *trans;
trans = btrfs_start_transaction(root, 1);
if (!trans)
return -ENOMEM;
if (IS_ERR(trans))
return PTR_ERR(trans);
err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
if (err) {
fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
@ -830,7 +830,7 @@ static int ext2_copy_inodes(struct btrfs_convert_context *cctx,
ret = btrfs_commit_transaction(trans, root);
BUG_ON(ret);
trans = btrfs_start_transaction(root, 1);
BUG_ON(!trans);
BUG_ON(IS_ERR(trans));
}
}
if (err) {

View File

@ -500,8 +500,8 @@ static int reiserfs_copy_dirent(reiserfs_filsys_t fs,
return ret;
}
trans = btrfs_start_transaction(root, 1);
if (!trans)
return -ENOMEM;
if (IS_ERR(trans))
return PTR_ERR(trans);
ret = convert_insert_dirent(trans, root, name, len, dir_objectid,
objectid, type, dirent_data->index++,
@ -613,8 +613,8 @@ static int reiserfs_copy_meta(reiserfs_filsys_t fs, struct btrfs_root *root,
switch (mode & S_IFMT) {
case S_IFREG:
trans = btrfs_start_transaction(root, 1);
if (!trans) {
ret = -ENOMEM;
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto fail;
}
ret = reiserfs_record_file_extents(fs, trans, root, objectid,
@ -629,8 +629,8 @@ static int reiserfs_copy_meta(reiserfs_filsys_t fs, struct btrfs_root *root,
if (ret)
goto fail;
trans = btrfs_start_transaction(root, 1);
if (!trans) {
ret = -ENOMEM;
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto fail;
}
@ -639,8 +639,8 @@ static int reiserfs_copy_meta(reiserfs_filsys_t fs, struct btrfs_root *root,
break;
case S_IFLNK:
trans = btrfs_start_transaction(root, 1);
if (!trans) {
ret = -ENOMEM;
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto fail;
}
ret = reiserfs_copy_symlink(trans, root, objectid,
@ -650,8 +650,8 @@ static int reiserfs_copy_meta(reiserfs_filsys_t fs, struct btrfs_root *root,
break;
default:
trans = btrfs_start_transaction(root, 1);
if (!trans) {
ret = -ENOMEM;
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto fail;
}
}
@ -872,8 +872,8 @@ static int reiserfs_copy_xattr_dir(reiserfs_filsys_t fs,
xa_data->target_oid += OID_OFFSET;
xa_data->trans = btrfs_start_transaction(xa_data->root, 1);
if (!xa_data->trans)
return -ENOMEM;
if (IS_ERR(xa_data->trans))
return PTR_ERR(xa_data->trans);
ret = reiserfs_iterate_dir(fs, &dir_key,
reiserfs_copy_one_xattr, xa_data);

View File

@ -443,6 +443,7 @@ int main(int ac, char **av)
}
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
dir_oid = btrfs_super_root_dir(&super);

View File

@ -1753,8 +1753,10 @@ int close_ctree_fs_info(struct btrfs_fs_info *fs_info)
fs_info->generation) {
BUG_ON(!root);
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
btrfs_commit_transaction(trans, root);
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
ret = commit_tree_roots(trans, fs_info);
BUG_ON(ret);
ret = __commit_transaction(trans, root);

View File

@ -75,6 +75,7 @@ static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
int ret;
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
bytes_used = btrfs_super_bytes_used(fs_info->super_copy);
root->fs_info->system_allocs = 1;
@ -1047,6 +1048,7 @@ static int make_image(const char *source_dir, struct btrfs_root *root)
INIT_LIST_HEAD(&dir_head.list);
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
ret = traverse_directory(trans, root, source_dir, &dir_head);
if (ret) {
error("unable to traverse directory %s: %d", source_dir, ret);
@ -1325,6 +1327,7 @@ static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
btrfs_init_path(&path);
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
key.objectid = 0;
key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
@ -1758,7 +1761,7 @@ int main(int argc, char **argv)
}
trans = btrfs_start_transaction(root, 1);
if (!trans) {
if (IS_ERR(trans)) {
error("failed to start transaction");
goto error;
}
@ -1782,7 +1785,7 @@ int main(int argc, char **argv)
}
trans = btrfs_start_transaction(root, 1);
if (!trans) {
if (IS_ERR(trans)) {
error("failed to start transaction");
goto error;
}
@ -1860,6 +1863,7 @@ raid_groups:
if (source_dir_set) {
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
ret = create_chunks(trans, root,
num_of_meta_chunks, size_of_data,
&allocation);

View File

@ -56,6 +56,7 @@ int main(int ac, char **av) {
exit(1);
}
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
srand(55);
ins.type = BTRFS_STRING_ITEM_KEY;
for (i = 0; i < run_size; i++) {
@ -72,6 +73,7 @@ int main(int ac, char **av) {
if (i == run_size - 5) {
btrfs_commit_transaction(trans, root);
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
}
}
btrfs_commit_transaction(trans, root);
@ -114,6 +116,7 @@ int main(int ac, char **av) {
i = 0;
srand(55);
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
for (i = 0 ; i < run_size/4; i++) {
num = next_key(i, max_key);
ins.objectid = num;
@ -138,6 +141,7 @@ int main(int ac, char **av) {
exit(1);
}
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
srand(128);
for (i = 0; i < run_size; i++) {
num = next_key(i, max_key);
@ -175,6 +179,7 @@ int main(int ac, char **av) {
}
printf("starting big long delete run\n");
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
while(root->node && btrfs_header_nritems(root->node) > 0) {
struct extent_buffer *leaf;
int slot;

View File

@ -380,6 +380,7 @@ int main(int ac, char **av)
}
printf("initial fill\n");
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
ret = fill_tree(trans, root, &radix, init_fill_count);
printf("starting run\n");
if (ret) {

View File

@ -1229,6 +1229,7 @@ static int set_label_unmounted(const char *dev, const char *label)
return -1;
trans = btrfs_start_transaction(root, 1);
BUG_ON(IS_ERR(trans));
__strncpy_null(root->fs_info->super_copy->label, label, BTRFS_LABEL_SIZE - 1);
btrfs_commit_transaction(trans, root);