* [PATCH 0/3] Simplify error handling in 3 functions
@ 2020-11-24 15:49 Nikolay Borisov
2020-11-24 15:49 ` [PATCH 1/3] btrfs: Remove err variable from btrfs_delete_subvolume Nikolay Borisov
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Nikolay Borisov @ 2020-11-24 15:49 UTC (permalink / raw)
To: linux-btrfs; +Cc: josef, Nikolay Borisov
Here are 3 very similar patches which switch some functions from using 2
variables to implement their error handling to using simply 1 as the standard in
the codebase dictates.
Nikolay Borisov (3):
btrfs: Remove err variable from btrfs_delete_subvolume
btrfs: Eliminate err variable from merge_reloc_root
btrfs: Remove err variable from do_relocation
fs/btrfs/inode.c | 21 ++++++----------
fs/btrfs/relocation.c | 57 +++++++++++++++----------------------------
2 files changed, 26 insertions(+), 52 deletions(-)
--
2.25.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/3] btrfs: Remove err variable from btrfs_delete_subvolume
2020-11-24 15:49 [PATCH 0/3] Simplify error handling in 3 functions Nikolay Borisov
@ 2020-11-24 15:49 ` Nikolay Borisov
2020-11-24 15:49 ` [PATCH 2/3] btrfs: Eliminate err variable from merge_reloc_root Nikolay Borisov
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Nikolay Borisov @ 2020-11-24 15:49 UTC (permalink / raw)
To: linux-btrfs; +Cc: josef, Nikolay Borisov
Use only a single 'ret' to control whether we should abort the
transaction or not. That's fine, because if we abort a transaction then
btrfs_end_transaction will return the same value as passed to
btrfs_abort_transaction. No semantic changes.
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
fs/btrfs/inode.c | 21 +++++++--------------
1 file changed, 7 insertions(+), 14 deletions(-)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index ed120c09acf6..875ba51839cc 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -4083,7 +4083,6 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
struct btrfs_block_rsv block_rsv;
u64 root_flags;
int ret;
- int err;
/*
* Don't allow to delete a subvolume with send in progress. This is
@@ -4105,8 +4104,8 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
down_write(&fs_info->subvol_sem);
- err = may_destroy_subvol(dest);
- if (err)
+ ret = may_destroy_subvol(dest);
+ if (ret)
goto out_up_write;
btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
@@ -4115,13 +4114,13 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
* two for dir entries,
* two for root ref/backref.
*/
- err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
- if (err)
+ ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
+ if (ret)
goto out_up_write;
trans = btrfs_start_transaction(root, 0);
if (IS_ERR(trans)) {
- err = PTR_ERR(trans);
+ ret = PTR_ERR(trans);
goto out_release;
}
trans->block_rsv = &block_rsv;
@@ -4131,7 +4130,6 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
ret = btrfs_unlink_subvol(trans, dir, dentry);
if (ret) {
- err = ret;
btrfs_abort_transaction(trans, ret);
goto out_end_trans;
}
@@ -4149,7 +4147,6 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
dest->root_key.objectid);
if (ret) {
btrfs_abort_transaction(trans, ret);
- err = ret;
goto out_end_trans;
}
}
@@ -4159,7 +4156,6 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
dest->root_key.objectid);
if (ret && ret != -ENOENT) {
btrfs_abort_transaction(trans, ret);
- err = ret;
goto out_end_trans;
}
if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
@@ -4169,7 +4165,6 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
dest->root_key.objectid);
if (ret && ret != -ENOENT) {
btrfs_abort_transaction(trans, ret);
- err = ret;
goto out_end_trans;
}
}
@@ -4180,14 +4175,12 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
trans->block_rsv = NULL;
trans->bytes_reserved = 0;
ret = btrfs_end_transaction(trans);
- if (ret && !err)
- err = ret;
inode->i_flags |= S_DEAD;
out_release:
btrfs_subvolume_release_metadata(root, &block_rsv);
out_up_write:
up_write(&fs_info->subvol_sem);
- if (err) {
+ if (ret) {
spin_lock(&dest->root_item_lock);
root_flags = btrfs_root_flags(&dest->root_item);
btrfs_set_root_flags(&dest->root_item,
@@ -4205,7 +4198,7 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
}
}
- return err;
+ return ret;
}
static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/3] btrfs: Eliminate err variable from merge_reloc_root
2020-11-24 15:49 [PATCH 0/3] Simplify error handling in 3 functions Nikolay Borisov
2020-11-24 15:49 ` [PATCH 1/3] btrfs: Remove err variable from btrfs_delete_subvolume Nikolay Borisov
@ 2020-11-24 15:49 ` Nikolay Borisov
2020-11-24 15:49 ` [PATCH 3/3] btrfs: Remove err variable from do_relocation Nikolay Borisov
2020-11-24 16:12 ` [PATCH 0/3] Simplify error handling in 3 functions David Sterba
3 siblings, 0 replies; 5+ messages in thread
From: Nikolay Borisov @ 2020-11-24 15:49 UTC (permalink / raw)
To: linux-btrfs; +Cc: josef, Nikolay Borisov
In most cases when an error is returned from a function 'ret' is simply
assigned to 'err'. There is only 1 case where walk_up_reloc_tree can
return a positive value - in this case the code breaks from the loop and
ret is going to get its return value from btrfs_cow_block - either 0 or
negative. This retains the old logic of how 'err' used to be set at
this call site.
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
fs/btrfs/relocation.c | 24 +++++++-----------------
1 file changed, 7 insertions(+), 17 deletions(-)
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index c5774a8e6ff7..8fc75db901c8 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -1630,8 +1630,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
int level;
int max_level;
int replaced = 0;
- int ret;
- int err = 0;
+ int ret = 0;
u32 min_reserved;
path = btrfs_alloc_path();
@@ -1682,13 +1681,11 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
while (1) {
ret = btrfs_block_rsv_refill(root, rc->block_rsv, min_reserved,
BTRFS_RESERVE_FLUSH_LIMIT);
- if (ret) {
- err = ret;
+ if (ret)
goto out;
- }
trans = btrfs_start_transaction(root, 0);
if (IS_ERR(trans)) {
- err = PTR_ERR(trans);
+ ret = PTR_ERR(trans);
trans = NULL;
goto out;
}
@@ -1710,10 +1707,8 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
max_level = level;
ret = walk_down_reloc_tree(reloc_root, path, &level);
- if (ret < 0) {
- err = ret;
+ if (ret < 0)
goto out;
- }
if (ret > 0)
break;
@@ -1724,11 +1719,8 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
ret = replace_path(trans, rc, root, reloc_root, path,
&next_key, level, max_level);
}
- if (ret < 0) {
- err = ret;
+ if (ret < 0)
goto out;
- }
-
if (ret > 0) {
level = ret;
btrfs_node_key_to_cpu(path->nodes[level], &key,
@@ -1767,12 +1759,10 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
BTRFS_NESTING_COW);
btrfs_tree_unlock(leaf);
free_extent_buffer(leaf);
- if (ret < 0)
- err = ret;
out:
btrfs_free_path(path);
- if (err == 0)
+ if (ret == 0)
insert_dirty_subvol(trans, rc, root);
if (trans)
@@ -1783,7 +1773,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
if (replaced && rc->stage == UPDATE_DATA_PTRS)
invalidate_extent_cache(root, &key, &next_key);
- return err;
+ return ret;
}
static noinline_for_stack
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/3] btrfs: Remove err variable from do_relocation
2020-11-24 15:49 [PATCH 0/3] Simplify error handling in 3 functions Nikolay Borisov
2020-11-24 15:49 ` [PATCH 1/3] btrfs: Remove err variable from btrfs_delete_subvolume Nikolay Borisov
2020-11-24 15:49 ` [PATCH 2/3] btrfs: Eliminate err variable from merge_reloc_root Nikolay Borisov
@ 2020-11-24 15:49 ` Nikolay Borisov
2020-11-24 16:12 ` [PATCH 0/3] Simplify error handling in 3 functions David Sterba
3 siblings, 0 replies; 5+ messages in thread
From: Nikolay Borisov @ 2020-11-24 15:49 UTC (permalink / raw)
To: linux-btrfs; +Cc: josef, Nikolay Borisov
It simply gets assigned to 'ret' in case of errors. The flow of the
while loop is not changed by this commit since the few call sites
that 'goto next' will simply break from the loop.
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
fs/btrfs/relocation.c | 33 ++++++++++++---------------------
1 file changed, 12 insertions(+), 21 deletions(-)
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 8fc75db901c8..6e7e5fe2e277 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -2181,8 +2181,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
u32 blocksize;
u64 bytenr;
int slot;
- int ret;
- int err = 0;
+ int ret = 0;
BUG_ON(lowest && node->eb);
@@ -2200,10 +2199,8 @@ static int do_relocation(struct btrfs_trans_handle *trans,
if (upper->eb && !upper->locked) {
if (!lowest) {
ret = btrfs_bin_search(upper->eb, key, &slot);
- if (ret < 0) {
- err = ret;
+ if (ret < 0)
goto next;
- }
BUG_ON(ret);
bytenr = btrfs_node_blockptr(upper->eb, slot);
if (node->eb->start == bytenr)
@@ -2215,10 +2212,8 @@ static int do_relocation(struct btrfs_trans_handle *trans,
if (!upper->eb) {
ret = btrfs_search_slot(trans, root, key, path, 0, 1);
if (ret) {
- if (ret < 0)
- err = ret;
- else
- err = -ENOENT;
+ if (ret > 0)
+ ret = -ENOENT;
btrfs_release_path(path);
break;
@@ -2238,10 +2233,8 @@ static int do_relocation(struct btrfs_trans_handle *trans,
btrfs_release_path(path);
} else {
ret = btrfs_bin_search(upper->eb, key, &slot);
- if (ret < 0) {
- err = ret;
+ if (ret < 0)
goto next;
- }
BUG_ON(ret);
}
@@ -2252,7 +2245,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
"lowest leaf/node mismatch: bytenr %llu node->bytenr %llu slot %d upper %llu",
bytenr, node->bytenr, slot,
upper->eb->start);
- err = -EIO;
+ ret = -EIO;
goto next;
}
} else {
@@ -2263,7 +2256,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
blocksize = root->fs_info->nodesize;
eb = btrfs_read_node_slot(upper->eb, slot);
if (IS_ERR(eb)) {
- err = PTR_ERR(eb);
+ ret = PTR_ERR(eb);
goto next;
}
btrfs_tree_lock(eb);
@@ -2273,10 +2266,8 @@ static int do_relocation(struct btrfs_trans_handle *trans,
slot, &eb, BTRFS_NESTING_COW);
btrfs_tree_unlock(eb);
free_extent_buffer(eb);
- if (ret < 0) {
- err = ret;
+ if (ret < 0)
goto next;
- }
BUG_ON(node->eb != eb);
} else {
btrfs_set_node_blockptr(upper->eb, slot,
@@ -2302,19 +2293,19 @@ static int do_relocation(struct btrfs_trans_handle *trans,
btrfs_backref_drop_node_buffer(upper);
else
btrfs_backref_unlock_node_buffer(upper);
- if (err)
+ if (ret)
break;
}
- if (!err && node->pending) {
+ if (!ret && node->pending) {
btrfs_backref_drop_node_buffer(node);
list_move_tail(&node->list, &rc->backref_cache.changed);
node->pending = 0;
}
path->lowest_level = 0;
- BUG_ON(err == -ENOSPC);
- return err;
+ BUG_ON(ret == -ENOSPC);
+ return ret;
}
static int link_to_upper(struct btrfs_trans_handle *trans,
--
2.25.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 0/3] Simplify error handling in 3 functions
2020-11-24 15:49 [PATCH 0/3] Simplify error handling in 3 functions Nikolay Borisov
` (2 preceding siblings ...)
2020-11-24 15:49 ` [PATCH 3/3] btrfs: Remove err variable from do_relocation Nikolay Borisov
@ 2020-11-24 16:12 ` David Sterba
3 siblings, 0 replies; 5+ messages in thread
From: David Sterba @ 2020-11-24 16:12 UTC (permalink / raw)
To: Nikolay Borisov; +Cc: linux-btrfs, josef
On Tue, Nov 24, 2020 at 05:49:29PM +0200, Nikolay Borisov wrote:
> Here are 3 very similar patches which switch some functions from using 2
> variables to implement their error handling to using simply 1 as the standard in
> the codebase dictates.
Nice, added to misc-next, thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-11-24 16:14 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-24 15:49 [PATCH 0/3] Simplify error handling in 3 functions Nikolay Borisov
2020-11-24 15:49 ` [PATCH 1/3] btrfs: Remove err variable from btrfs_delete_subvolume Nikolay Borisov
2020-11-24 15:49 ` [PATCH 2/3] btrfs: Eliminate err variable from merge_reloc_root Nikolay Borisov
2020-11-24 15:49 ` [PATCH 3/3] btrfs: Remove err variable from do_relocation Nikolay Borisov
2020-11-24 16:12 ` [PATCH 0/3] Simplify error handling in 3 functions David Sterba
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox