aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
authorFilipe Manana <fdmanana@suse.com>2015-02-13 11:56:14 -0500
committerChris Mason <clm@fb.com>2015-02-14 11:22:49 -0500
commita742994aa2e271eb8cd8e043d276515ec858ed73 (patch)
tree7c475a13b9407306cb3754cf150beabf2433dbd4 /fs/btrfs
parent1a4bcf470c886b955adf36486f4c86f2441d85cb (diff)
Btrfs: don't remove extents and xattrs when logging new names
If we are recording in the tree log that an inode has new names (new hard links were added), we would drop items, belonging to the inode, that we shouldn't: 1) When the flag BTRFS_INODE_COPY_EVERYTHING is set in the inode's runtime flags, we ended up dropping all the extent and xattr items that were previously logged. This was done only in memory, since logging a new name doesn't imply syncing the log; 2) When the flag BTRFS_INODE_COPY_EVERYTHING is set in the inode's runtime flags, we ended up dropping all the xattr items that were previously logged. Like the case before, this was done only in memory because logging a new name doesn't imply syncing the log. This led to some surprises in scenarios such as the following: 1) write some extents to an inode; 2) fsync the inode; 3) truncate the inode or delete/modify some of its xattrs 4) add a new hard link for that inode 5) fsync some other file, to force the log tree to be durably persisted 6) power failure happens The next time the fs is mounted, the fsync log replay code is executed, and the resulting file doesn't have the content it had when the last fsync against it was performed, instead if has a content matching what it had when the last transaction commit happened. So change the behaviour such that when a new name is logged, only the inode item and reference items are processed. This is easy to reproduce with the test I just made for xfstests, whose main body is: _scratch_mkfs >> $seqres.full 2>&1 _init_flakey _mount_flakey # Create our test file with some data. $XFS_IO_PROG -f -c "pwrite -S 0xaa -b 8K 0 8K" \ $SCRATCH_MNT/foo | _filter_xfs_io # Make sure the file is durably persisted. sync # Append some data to our file, to increase its size. $XFS_IO_PROG -f -c "pwrite -S 0xcc -b 4K 8K 4K" \ $SCRATCH_MNT/foo | _filter_xfs_io # Fsync the file, so from this point on if a crash/power failure happens, our # new data is guaranteed to be there next time the fs is mounted. $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo # Now shrink our file to 5000 bytes. $XFS_IO_PROG -c "truncate 5000" $SCRATCH_MNT/foo # Now do an expanding truncate to a size larger than what we had when we last # fsync'ed our file. This is just to verify that after power failure and # replaying the fsync log, our file matches what it was when we last fsync'ed # it - 12Kb size, first 8Kb of data had a value of 0xaa and the last 4Kb of # data had a value of 0xcc. $XFS_IO_PROG -c "truncate 32K" $SCRATCH_MNT/foo # Add one hard link to our file. This made btrfs drop all of our file's # metadata from the fsync log, including the metadata relative to the # extent we just wrote and fsync'ed. This change was made only to the fsync # log in memory, so adding the hard link alone doesn't change the persisted # fsync log. This happened because the previous truncates set the runtime # flag BTRFS_INODE_NEEDS_FULL_SYNC in the btrfs inode structure. ln $SCRATCH_MNT/foo $SCRATCH_MNT/foo_link # Now make sure the in memory fsync log is durably persisted. # Creating and fsync'ing another file will do it. # After this our persisted fsync log will no longer have metadata for our file # foo that points to the extent we wrote and fsync'ed before. touch $SCRATCH_MNT/bar $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar # As expected, before the crash/power failure, we should be able to see a file # with a size of 32Kb, with its first 5000 bytes having the value 0xaa and all # the remaining bytes with value 0x00. echo "File content before:" od -t x1 $SCRATCH_MNT/foo # Simulate a crash/power loss. _load_flakey_table $FLAKEY_DROP_WRITES _unmount_flakey _load_flakey_table $FLAKEY_ALLOW_WRITES _mount_flakey # After mounting the fs again, the fsync log was replayed. # The expected result is to see a file with a size of 12Kb, with its first 8Kb # of data having the value 0xaa and its last 4Kb of data having a value of 0xcc. # The btrfs bug used to leave the file as it used te be as of the last # transaction commit - that is, with a size of 8Kb with all bytes having a # value of 0xaa. echo "File content after:" od -t x1 $SCRATCH_MNT/foo The test case for xfstests follows soon. Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Chris Mason <clm@fb.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/tree-log.c39
1 files changed, 27 insertions, 12 deletions
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 5f649bb32bec..f96996a1b70c 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -4069,8 +4069,10 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4069 if (S_ISDIR(inode->i_mode)) { 4069 if (S_ISDIR(inode->i_mode)) {
4070 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY; 4070 int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
4071 4071
4072 if (inode_only == LOG_INODE_EXISTS) 4072 if (inode_only == LOG_INODE_EXISTS) {
4073 max_key_type = BTRFS_XATTR_ITEM_KEY; 4073 max_key_type = BTRFS_INODE_EXTREF_KEY;
4074 max_key.type = max_key_type;
4075 }
4074 ret = drop_objectid_items(trans, log, path, ino, max_key_type); 4076 ret = drop_objectid_items(trans, log, path, ino, max_key_type);
4075 } else { 4077 } else {
4076 if (inode_only == LOG_INODE_EXISTS) { 4078 if (inode_only == LOG_INODE_EXISTS) {
@@ -4092,18 +4094,31 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4092 if (err) 4094 if (err)
4093 goto out_unlock; 4095 goto out_unlock;
4094 } 4096 }
4095 if (test_and_clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 4097 if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4096 &BTRFS_I(inode)->runtime_flags)) { 4098 &BTRFS_I(inode)->runtime_flags)) {
4097 clear_bit(BTRFS_INODE_COPY_EVERYTHING, 4099 if (inode_only == LOG_INODE_EXISTS) {
4098 &BTRFS_I(inode)->runtime_flags); 4100 max_key.type = BTRFS_INODE_EXTREF_KEY;
4099 ret = btrfs_truncate_inode_items(trans, log, 4101 ret = drop_objectid_items(trans, log, path, ino,
4100 inode, 0, 0); 4102 max_key.type);
4101 } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING, 4103 } else {
4102 &BTRFS_I(inode)->runtime_flags) || 4104 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4105 &BTRFS_I(inode)->runtime_flags);
4106 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4107 &BTRFS_I(inode)->runtime_flags);
4108 ret = btrfs_truncate_inode_items(trans, log,
4109 inode, 0, 0);
4110 }
4111 } else if (test_bit(BTRFS_INODE_COPY_EVERYTHING,
4112 &BTRFS_I(inode)->runtime_flags) ||
4103 inode_only == LOG_INODE_EXISTS) { 4113 inode_only == LOG_INODE_EXISTS) {
4104 if (inode_only == LOG_INODE_ALL) 4114 if (inode_only == LOG_INODE_ALL) {
4115 clear_bit(BTRFS_INODE_COPY_EVERYTHING,
4116 &BTRFS_I(inode)->runtime_flags);
4105 fast_search = true; 4117 fast_search = true;
4106 max_key.type = BTRFS_XATTR_ITEM_KEY; 4118 max_key.type = BTRFS_XATTR_ITEM_KEY;
4119 } else {
4120 max_key.type = BTRFS_INODE_EXTREF_KEY;
4121 }
4107 ret = drop_objectid_items(trans, log, path, ino, 4122 ret = drop_objectid_items(trans, log, path, ino,
4108 max_key.type); 4123 max_key.type);
4109 } else { 4124 } else {