Skip to content

Commit 5bb0087

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 e87e953 commit 5bb0087

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
@@ -6805,7 +6805,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
68056805
struct fscrypt_name fname;
68066806
u64 index;
68076807
int ret;
6808-
int drop_inode = 0;
68096808

68106809
/* do not allow sys_link's with other subvols of the same device */
68116810
if (btrfs_root_id(root) != btrfs_root_id(BTRFS_I(inode)->root))
@@ -6837,50 +6836,44 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
68376836

68386837
/* There are several dir indexes for this inode, clear the cache. */
68396838
BTRFS_I(inode)->dir_index = 0ULL;
6840-
inc_nlink(inode);
68416839
inode_inc_iversion(inode);
68426840
inode_set_ctime_current(inode);
6843-
ihold(inode);
68446841
set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
68456842

68466843
ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
68476844
&fname.disk_name, 1, index);
6845+
if (ret)
6846+
goto fail;
68486847

6848+
/* Link added now we update the inode item with the new link count. */
6849+
inc_nlink(inode);
6850+
ret = btrfs_update_inode(trans, BTRFS_I(inode));
68496851
if (ret) {
6850-
drop_inode = 1;
6851-
} else {
6852-
struct dentry *parent = dentry->d_parent;
6852+
btrfs_abort_transaction(trans, ret);
6853+
goto fail;
6854+
}
68536855

6854-
ret = btrfs_update_inode(trans, BTRFS_I(inode));
6856+
if (inode->i_nlink == 1) {
6857+
/*
6858+
* If the new hard link count is 1, it's a file created with the
6859+
* open(2) O_TMPFILE flag.
6860+
*/
6861+
ret = btrfs_orphan_del(trans, BTRFS_I(inode));
68556862
if (ret) {
68566863
btrfs_abort_transaction(trans, ret);
6857-
drop_inode = 1;
68586864
goto fail;
68596865
}
6860-
if (inode->i_nlink == 1) {
6861-
/*
6862-
* If new hard link count is 1, it's a file created
6863-
* with open(2) O_TMPFILE flag.
6864-
*/
6865-
ret = btrfs_orphan_del(trans, BTRFS_I(inode));
6866-
if (ret) {
6867-
btrfs_abort_transaction(trans, ret);
6868-
drop_inode = 1;
6869-
goto fail;
6870-
}
6871-
}
6872-
d_instantiate(dentry, inode);
6873-
btrfs_log_new_name(trans, old_dentry, NULL, 0, parent);
68746866
}
68756867

6868+
/* Grab reference for the new dentry passed to d_instantiate(). */
6869+
ihold(inode);
6870+
d_instantiate(dentry, inode);
6871+
btrfs_log_new_name(trans, old_dentry, NULL, 0, dentry->d_parent);
6872+
68766873
fail:
68776874
fscrypt_free_filename(&fname);
68786875
if (trans)
68796876
btrfs_end_transaction(trans);
6880-
if (drop_inode) {
6881-
inode_dec_link_count(inode);
6882-
iput(inode);
6883-
}
68846877
btrfs_btree_balance_dirty(fs_info);
68856878
return ret;
68866879
}

0 commit comments

Comments
 (0)