Skip to content

Commit bfe023d

Browse files
fdmananakdave
authored andcommitted
btrfs: simplify error handling logic for btrfs_link()
Instead of incrementing the inode's link count and refcount early before adding the link, updating the inode and deleting orphan item, do it after all those steps succeeded right before calling d_instantiate(). This makes the error handling logic simpler by avoiding the need for the 'drop_inode' variable to signal if we need to undo the link count increment and the inode refcount increase under the 'fail' label. This also reduces the level of indentation by one, making the code easier to read. Reviewed-by: Johannes Thumshirn <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 8b0af35 commit bfe023d

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

fs/btrfs/inode.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6799,7 +6799,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
67996799
struct fscrypt_name fname;
68006800
u64 index;
68016801
int ret;
6802-
int drop_inode = 0;
68036802

68046803
/* do not allow sys_link's with other subvols of the same device */
68056804
if (btrfs_root_id(root) != btrfs_root_id(BTRFS_I(inode)->root))
@@ -6831,50 +6830,44 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
68316830

68326831
/* There are several dir indexes for this inode, clear the cache. */
68336832
BTRFS_I(inode)->dir_index = 0ULL;
6834-
inc_nlink(inode);
68356833
inode_inc_iversion(inode);
68366834
inode_set_ctime_current(inode);
6837-
ihold(inode);
68386835
set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
68396836

68406837
ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
68416838
&fname.disk_name, 1, index);
6839+
if (ret)
6840+
goto fail;
68426841

6842+
/* Link added now we update the inode item with the new link count. */
6843+
inc_nlink(inode);
6844+
ret = btrfs_update_inode(trans, BTRFS_I(inode));
68436845
if (ret) {
6844-
drop_inode = 1;
6845-
} else {
6846-
struct dentry *parent = dentry->d_parent;
6846+
btrfs_abort_transaction(trans, ret);
6847+
goto fail;
6848+
}
68476849

6848-
ret = btrfs_update_inode(trans, BTRFS_I(inode));
6850+
if (inode->i_nlink == 1) {
6851+
/*
6852+
* If the new hard link count is 1, it's a file created with the
6853+
* open(2) O_TMPFILE flag.
6854+
*/
6855+
ret = btrfs_orphan_del(trans, BTRFS_I(inode));
68496856
if (ret) {
68506857
btrfs_abort_transaction(trans, ret);
6851-
drop_inode = 1;
68526858
goto fail;
68536859
}
6854-
if (inode->i_nlink == 1) {
6855-
/*
6856-
* If new hard link count is 1, it's a file created
6857-
* with open(2) O_TMPFILE flag.
6858-
*/
6859-
ret = btrfs_orphan_del(trans, BTRFS_I(inode));
6860-
if (ret) {
6861-
btrfs_abort_transaction(trans, ret);
6862-
drop_inode = 1;
6863-
goto fail;
6864-
}
6865-
}
6866-
d_instantiate(dentry, inode);
6867-
btrfs_log_new_name(trans, old_dentry, NULL, 0, parent);
68686860
}
68696861

6862+
/* Grab reference for the new dentry passed to d_instantiate(). */
6863+
ihold(inode);
6864+
d_instantiate(dentry, inode);
6865+
btrfs_log_new_name(trans, old_dentry, NULL, 0, dentry->d_parent);
6866+
68706867
fail:
68716868
fscrypt_free_filename(&fname);
68726869
if (trans)
68736870
btrfs_end_transaction(trans);
6874-
if (drop_inode) {
6875-
inode_dec_link_count(inode);
6876-
iput(inode);
6877-
}
68786871
btrfs_btree_balance_dirty(fs_info);
68796872
return ret;
68806873
}

0 commit comments

Comments
 (0)