Skip to contentSkip to conversation

Btrfs: fix fsync data loss after a ranged fsync

PATCH Superseded Filipe Manana 1 participant 5 messages Aug 30, 2014 – Sep 8, 2014 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: fix fsync data loss after a ranged fsync

While we're doing a full fsync (when the inode has the flag BTRFS_INODE_NEEDS_FULL_SYNC set) that is ranged too (covers only a portion of the file), we might have ordered operations that are started while we're logging the inode and that fall outside the fsync range.

This means we can get extent maps outside our range added to the inode's extent map tree's modified list for which the corresponding ordered operation wasn't captured by our call to btrfs_get_logged_extents() - the fill delalloc callbacks, inode.c:cow_file_range() and inode.c:submit_compressed_extents() add an extent map to the modified list before creating the respective ordered operation - and they do this without holding the inode's mutex nor the inode's log mutex.

Therefore when a full ranged fsync finishes don't remove every extent map from the modified list of extent maps - as for some of them, that fall outside our fsync range, we might have not waited for their respective ordered operation to finish (meaning the corresponding file extent item wasn't inserted into the fs/subvol tree yet), and we must let the next fsync (very likely a fast one that checks only the modified list) see this extent map and log a matching file extent item to the log btree and wait for its ordered operation to finish (if it's still ongoing).

---
 fs/btrfs/file.c     |  2 +-
 fs/btrfs/tree-log.c | 50 ++++++++++++++++++++++++++++++++++++++++----------
 fs/btrfs/tree-log.h |  2 ++
 3 files changed, 43 insertions(+), 11 deletions(-)
fs/btrfs/file.c +1 -1
index 66c4076..e5534c1 100644
@@ -1979,7 +1979,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 
 	btrfs_init_log_ctx(&ctx);
 
-	ret = btrfs_log_dentry_safe(trans, root, dentry, &ctx);
+	ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
 	if (ret < 0) {
 		/* Fallthrough and commit/free transaction. */
 		ret = 1;
fs/btrfs/tree-log.c +40 -10

104 lines not on this page · raw message

fs/btrfs/tree-log.h +2 -0
index 7f5b41b..e2e798a 100644
@@ -59,6 +59,8 @@ int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
 int btrfs_recover_log_trees(struct btrfs_root *tree_root);
 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
 			  struct btrfs_root *root, struct dentry *dentry,
+			  const loff_t start,
+			  const loff_t end,
 			  struct btrfs_log_ctx *ctx);
 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
 				 struct btrfs_root *root,
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/1 Btrfs: fix fsync data loss after a ranged fsync

While we're doing a full fsync (when the inode has the flag BTRFS_INODE_NEEDS_FULL_SYNC set) that is ranged too (covers only a portion of the file), we might have ordered operations that are started before or while we're logging the inode and that fall outside the fsync range.

Therefore when a full ranged fsync finishes don't remove every extent map from the list of modified extent maps - as for some of them, that fall outside our fsync range, their respective ordered operation hasn't finished yet, meaning the corresponding file extent item wasn't inserted into the fs/subvol tree yet and therefore we didn't log it, and we must let the next fast fsync (one that checks only the modified list) see this extent map and log a matching file extent item to the log btree and wait for its ordered operation to finish (if it's still ongoing).

---

V2: No code change, only updated the changelog and the comment, to make
them more clear and accurate.

 fs/btrfs/file.c     |  2 +-
 fs/btrfs/tree-log.c | 54 +++++++++++++++++++++++++++++++++++++++++++----------
 fs/btrfs/tree-log.h |  2 ++
 3 files changed, 47 insertions(+), 11 deletions(-)
fs/btrfs/file.c +1 -1
index 66c4076..e5534c1 100644
@@ -1979,7 +1979,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 
 	btrfs_init_log_ctx(&ctx);
 
-	ret = btrfs_log_dentry_safe(trans, root, dentry, &ctx);
+	ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
 	if (ret < 0) {
 		/* Fallthrough and commit/free transaction. */
 		ret = 1;
fs/btrfs/tree-log.c +44 -10

108 lines not on this page · raw message

fs/btrfs/tree-log.h +2 -0
index 7f5b41b..e2e798a 100644
@@ -59,6 +59,8 @@ int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
 int btrfs_recover_log_trees(struct btrfs_root *tree_root);
 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
 			  struct btrfs_root *root, struct dentry *dentry,
+			  const loff_t start,
+			  const loff_t end,
 			  struct btrfs_log_ctx *ctx);
 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
 				 struct btrfs_root *root,
Signature · 1 line
1.9.1
#2 lore raw

PATCH v3 1/1 Btrfs: fix fsync data loss after a ranged fsync

In reply to Filipe Manana

While we're doing a full fsync (when the inode has the flag BTRFS_INODE_NEEDS_FULL_SYNC set) that is ranged too (covers only a portion of the file), we might have ordered operations that are started before or while we're logging the inode and that fall outside the fsync range.

Therefore when a full ranged fsync finishes don't remove every extent map from the list of modified extent maps - as for some of them, that fall outside our fsync range, their respective ordered operation hasn't finished yet, meaning the corresponding file extent item wasn't inserted into the fs/subvol tree yet and therefore we didn't log it, and we must let the next fast fsync (one that checks only the modified list) see this extent map and log a matching file extent item to the log btree and wait for its ordered operation to finish (if it's still ongoing).

A test case for xfstests follows.

---

V2: No code change, only updated the changelog and the comment, to make
them more clear and accurate.

V3: Added missing condition to exclude extent map from the modified list
and ensure btrfs_log_inode() is called for the next fsync if the modified list didn't get empty after logging the inode. This time this follows with a test case for xfstests that is better then my previous local test and benefits everyone.

 fs/btrfs/file.c     |  2 +-
 fs/btrfs/tree-log.c | 78 ++++++++++++++++++++++++++++++++++++++++++-----------
 fs/btrfs/tree-log.h |  2 ++
 3 files changed, 66 insertions(+), 16 deletions(-)
fs/btrfs/file.c +1 -1
index 66c4076..e5534c1 100644
@@ -1979,7 +1979,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 
 	btrfs_init_log_ctx(&ctx);
 
-	ret = btrfs_log_dentry_safe(trans, root, dentry, &ctx);
+	ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
 	if (ret < 0) {
 		/* Fallthrough and commit/free transaction. */
 		ret = 1;
fs/btrfs/tree-log.c +63 -15

146 lines not on this page · raw message

fs/btrfs/tree-log.h +2 -0
index 7f5b41b..e2e798a 100644
@@ -59,6 +59,8 @@ int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
 int btrfs_recover_log_trees(struct btrfs_root *tree_root);
 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
 			  struct btrfs_root *root, struct dentry *dentry,
+			  const loff_t start,
+			  const loff_t end,
 			  struct btrfs_log_ctx *ctx);
 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
 				 struct btrfs_root *root,
Signature · 1 line
1.9.1
#3 lore raw

PATCH v4 1/1 Btrfs: fix fsync data loss after a ranged fsync

In reply to Filipe Manana

While we're doing a full fsync (when the inode has the flag BTRFS_INODE_NEEDS_FULL_SYNC set) that is ranged too (covers only a portion of the file), we might have ordered operations that are started before or while we're logging the inode and that fall outside the fsync range.

Therefore when a full ranged fsync finishes don't remove every extent map from the list of modified extent maps - as for some of them, that fall outside our fsync range, their respective ordered operation hasn't finished yet, meaning the corresponding file extent item wasn't inserted into the fs/subvol tree yet and therefore we didn't log it, and we must let the next fast fsync (one that checks only the modified list) see this extent map and log a matching file extent item to the log btree and wait for its ordered operation to finish (if it's still ongoing).

A test case for xfstests follows.

---

V2: No code change, only updated the changelog and the comment, to make
them more clear and accurate.

V3: Added missing condition to exclude extent map from the modified list
and ensure btrfs_log_inode() is called for the next fsync if the modified list didn't get empty after logging the inode. This time this follows with a test case for xfstests that is better then my previous local test and benefits everyone.

V4: Simplifed em exclusion logic.

 fs/btrfs/file.c     |  2 +-
 fs/btrfs/tree-log.c | 77 ++++++++++++++++++++++++++++++++++++++++++-----------
 fs/btrfs/tree-log.h |  2 ++
 3 files changed, 64 insertions(+), 17 deletions(-)
fs/btrfs/file.c +1 -1
index 66c4076..e5534c1 100644
@@ -1979,7 +1979,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 
 	btrfs_init_log_ctx(&ctx);
 
-	ret = btrfs_log_dentry_safe(trans, root, dentry, &ctx);
+	ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
 	if (ret < 0) {
 		/* Fallthrough and commit/free transaction. */
 		ret = 1;
fs/btrfs/tree-log.c +61 -16

144 lines not on this page · raw message

fs/btrfs/tree-log.h +2 -0
index 7f5b41b..e2e798a 100644
@@ -59,6 +59,8 @@ int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
 int btrfs_recover_log_trees(struct btrfs_root *tree_root);
 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
 			  struct btrfs_root *root, struct dentry *dentry,
+			  const loff_t start,
+			  const loff_t end,
 			  struct btrfs_log_ctx *ctx);
 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
 				 struct btrfs_root *root,
Signature · 1 line
1.9.1
#4 lore raw

PATCH v5 1/1 Btrfs: fix fsync data loss after a ranged fsync

In reply to Filipe Manana

While we're doing a full fsync (when the inode has the flag BTRFS_INODE_NEEDS_FULL_SYNC set) that is ranged too (covers only a portion of the file), we might have ordered operations that are started before or while we're logging the inode and that fall outside the fsync range.

Therefore when a full ranged fsync finishes don't remove every extent map from the list of modified extent maps - as for some of them, that fall outside our fsync range, their respective ordered operation hasn't finished yet, meaning the corresponding file extent item wasn't inserted into the fs/subvol tree yet and therefore we didn't log it, and we must let the next fast fsync (one that checks only the modified list) see this extent map and log a matching file extent item to the log btree and wait for its ordered operation to finish (if it's still ongoing).

A test case for xfstests follows.

---

V2: No code change, only updated the changelog and the comment, to make
them more clear and accurate.

V3: Added missing condition to exclude extent map from the modified list
and ensure btrfs_log_inode() is called for the next fsync if the modified list didn't get empty after logging the inode. This time this follows with a test case for xfstests that is better then my previous local test and benefits everyone.

V4: Simplifed em exclusion logic.

V5: Removed the hack that doesn't set the inode's logged_trans and
last_log_commit if the list of modified extent maps isn't empty. This prevented an unlink in the same transaction from removing the dentry from the log tree (if an fsync against the parent dir was made before).

 fs/btrfs/btrfs_inode.h | 13 ++++++++++--
 fs/btrfs/file.c        |  2 +-
 fs/btrfs/tree-log.c    | 55 ++++++++++++++++++++++++++++++++++++++++----------
 fs/btrfs/tree-log.h    |  2 ++
 4 files changed, 58 insertions(+), 14 deletions(-)
fs/btrfs/btrfs_inode.h +11 -2
index 74ff403..3511031 100644
@@ -246,8 +246,17 @@ static inline int btrfs_inode_in_log(struct inode *inode, u64 generation)
 	    BTRFS_I(inode)->last_sub_trans <=
 	    BTRFS_I(inode)->last_log_commit &&
 	    BTRFS_I(inode)->last_sub_trans <=
-	    BTRFS_I(inode)->root->last_log_commit)
-		return 1;
+	    BTRFS_I(inode)->root->last_log_commit) {
+		/*
+		 * After a ranged fsync we might have left some extent maps
+		 * (that fall outside the fsync's range). So return false
+		 * here if the list isn't empty, to make sure btrfs_log_inode()
+		 * will be called and process those extent maps.
+		 */
+		smp_mb();
+		if (list_empty(&BTRFS_I(inode)->extent_tree.modified_extents))
+			return 1;
+	}
 	return 0;
 }
 
fs/btrfs/file.c +1 -1
index 66c4076..e5534c1 100644
@@ -1979,7 +1979,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 
 	btrfs_init_log_ctx(&ctx);
 
-	ret = btrfs_log_dentry_safe(trans, root, dentry, &ctx);
+	ret = btrfs_log_dentry_safe(trans, root, dentry, start, end, &ctx);
 	if (ret < 0) {
 		/* Fallthrough and commit/free transaction. */
 		ret = 1;
fs/btrfs/tree-log.c +44 -11

108 lines not on this page · raw message

fs/btrfs/tree-log.h +2 -0
index 7f5b41b..e2e798a 100644
@@ -59,6 +59,8 @@ int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
 int btrfs_recover_log_trees(struct btrfs_root *tree_root);
 int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
 			  struct btrfs_root *root, struct dentry *dentry,
+			  const loff_t start,
+			  const loff_t end,
 			  struct btrfs_log_ctx *ctx);
 int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
 				 struct btrfs_root *root,
Signature · 1 line
1.9.1