Skip to content

Commit 9a8fca6

Browse files
fdmananakdave
authored andcommitted
Btrfs: fix xattr loss after power failure
If a file has xattrs, we fsync it, to ensure we clear the flags BTRFS_INODE_NEEDS_FULL_SYNC and BTRFS_INODE_COPY_EVERYTHING from its inode, the current transaction commits and then we fsync it (without either of those bits being set in its inode), we end up not logging all its xattrs. This results in deleting all xattrs when replying the log after a power failure. Trivial reproducer $ mkfs.btrfs -f /dev/sdb $ mount /dev/sdb /mnt $ touch /mnt/foobar $ setfattr -n user.xa -v qwerty /mnt/foobar $ xfs_io -c "fsync" /mnt/foobar $ sync $ xfs_io -c "pwrite -S 0xab 0 64K" /mnt/foobar $ xfs_io -c "fsync" /mnt/foobar <power failure> $ mount /dev/sdb /mnt $ getfattr --absolute-names --dump /mnt/foobar <empty output> $ So fix this by making sure all xattrs are logged if we log a file's inode item and neither the flags BTRFS_INODE_NEEDS_FULL_SYNC nor BTRFS_INODE_COPY_EVERYTHING were set in the inode. Fixes: 36283bf ("Btrfs: fix fsync xattr loss in the fast fsync path") Cc: <[email protected]> # 4.2+ Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 6f2f0b3 commit 9a8fca6

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

fs/btrfs/tree-log.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4827,6 +4827,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
48274827
struct extent_map_tree *em_tree = &inode->extent_tree;
48284828
u64 logged_isize = 0;
48294829
bool need_log_inode_item = true;
4830+
bool xattrs_logged = false;
48304831

48314832
path = btrfs_alloc_path();
48324833
if (!path)
@@ -5128,6 +5129,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
51285129
err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
51295130
if (err)
51305131
goto out_unlock;
5132+
xattrs_logged = true;
51315133
if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
51325134
btrfs_release_path(path);
51335135
btrfs_release_path(dst_path);
@@ -5140,6 +5142,11 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
51405142
btrfs_release_path(dst_path);
51415143
if (need_log_inode_item) {
51425144
err = log_inode_item(trans, log, dst_path, inode);
5145+
if (!err && !xattrs_logged) {
5146+
err = btrfs_log_all_xattrs(trans, root, inode, path,
5147+
dst_path);
5148+
btrfs_release_path(path);
5149+
}
51435150
if (err)
51445151
goto out_unlock;
51455152
}

0 commit comments

Comments
 (0)