Skip to content

Commit ac1af25

Browse files
fdmananakdave
authored andcommitted
btrfs: fix race between logging inode and checking if it was logged before
There's a race between checking if an inode was logged before and logging an inode that can cause us to mark an inode as not logged just after it was logged by a concurrent task: 1) We have inode X which was not logged before neither in the current transaction not in past transaction since the inode was loaded into memory, so it's ->logged_trans value is 0; 2) We are at transaction N; 3) Task A calls inode_logged() against inode X, sees that ->logged_trans is 0 and there is a log tree and so it proceeds to search in the log tree for an inode item for inode X. It doesn't see any, but before it sets ->logged_trans to N - 1... 3) Task B calls btrfs_log_inode() against inode X, logs the inode and sets ->logged_trans to N; 4) Task A now sets ->logged_trans to N - 1; 5) At this point anyone calling inode_logged() gets 0 (inode not logged) since ->logged_trans is greater than 0 and less than N, but our inode was really logged. As a consequence operations like rename, unlink and link that happen afterwards in the current transaction end up not updating the log when they should. Fix this by ensuring inode_logged() only updates ->logged_trans in case the inode item is not found in the log tree if after tacking the inode's lock (spinlock struct btrfs_inode::lock) the ->logged_trans value is still zero, since the inode lock is what protects setting ->logged_trans at btrfs_log_inode(). Fixes: 0f8ce49 ("btrfs: avoid inode logging during rename and link when possible") Reviewed-by: Boris Burkov <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 25e9b7c commit ac1af25

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

fs/btrfs/tree-log.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3489,6 +3489,31 @@ int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
34893489
return 0;
34903490
}
34913491

3492+
static bool mark_inode_as_not_logged(const struct btrfs_trans_handle *trans,
3493+
struct btrfs_inode *inode)
3494+
{
3495+
bool ret = false;
3496+
3497+
/*
3498+
* Do this only if ->logged_trans is still 0 to prevent races with
3499+
* concurrent logging as we may see the inode not logged when
3500+
* inode_logged() is called but it gets logged after inode_logged() did
3501+
* not find it in the log tree and we end up setting ->logged_trans to a
3502+
* value less than trans->transid after the concurrent logging task has
3503+
* set it to trans->transid. As a consequence, subsequent rename, unlink
3504+
* and link operations may end up not logging new names and removing old
3505+
* names from the log.
3506+
*/
3507+
spin_lock(&inode->lock);
3508+
if (inode->logged_trans == 0)
3509+
inode->logged_trans = trans->transid - 1;
3510+
else if (inode->logged_trans == trans->transid)
3511+
ret = true;
3512+
spin_unlock(&inode->lock);
3513+
3514+
return ret;
3515+
}
3516+
34923517
/*
34933518
* Check if an inode was logged in the current transaction. This correctly deals
34943519
* with the case where the inode was logged but has a logged_trans of 0, which
@@ -3523,10 +3548,8 @@ static int inode_logged(const struct btrfs_trans_handle *trans,
35233548
* transaction's ID, to avoid the search below in a future call in case
35243549
* a log tree gets created after this.
35253550
*/
3526-
if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state)) {
3527-
inode->logged_trans = trans->transid - 1;
3528-
return 0;
3529-
}
3551+
if (!test_bit(BTRFS_ROOT_HAS_LOG_TREE, &inode->root->state))
3552+
return mark_inode_as_not_logged(trans, inode);
35303553

35313554
/*
35323555
* We have a log tree and the inode's logged_trans is 0. We can't tell
@@ -3580,16 +3603,17 @@ static int inode_logged(const struct btrfs_trans_handle *trans,
35803603
* Set logged_trans to a value greater than 0 and less then the
35813604
* current transaction to avoid doing the search in future calls.
35823605
*/
3583-
inode->logged_trans = trans->transid - 1;
3584-
return 0;
3606+
return mark_inode_as_not_logged(trans, inode);
35853607
}
35863608

35873609
/*
35883610
* The inode was previously logged and then evicted, set logged_trans to
35893611
* the current transacion's ID, to avoid future tree searches as long as
35903612
* the inode is not evicted again.
35913613
*/
3614+
spin_lock(&inode->lock);
35923615
inode->logged_trans = trans->transid;
3616+
spin_unlock(&inode->lock);
35933617

35943618
/*
35953619
* If it's a directory, then we must set last_dir_index_offset to the

0 commit comments

Comments
 (0)