Skip to contentSkip to conversation

btrfs: keep a logged extent allocated when its ordered extent fails

PATCH In review Leo Martins 2 participants 2 messages Nov 10 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: keep a logged extent allocated when its ordered extent fails

A fast fsync doesn't wait for its ordered extents to complete. Once their IO is done it logs the new extents, with the checksums from the ordered extents, and syncs the log. Until an ordered extent completes and inserts its file extent item, the log is the only thing referencing its extent.

If the transaction aborts before that, the ordered extent fails to join the transaction and its error path returns the extent to the free space cache, even though the log still points at it. Writeback keeps going after the abort, so a page dirtied earlier can get allocated that range and overwrite it. When the log is replayed on the next mount, the file points at the overwritten blocks with the old checksums, and every read fails with a csum error while btrfs check reports no problems.

fsync completion of X writeback

write X, wait for IO
log X, sync the log
transaction aborts,
X returned to the
free space cache
allocate X, overwrite X
mount: log replay points the file at X, with X's logged checksums

Fix this by not freeing the extent of a failed ordered extent that a fast fsync logged (BTRFS_ORDERED_LOGGED). Release its reservation but keep the range out of the free space cache until the next mount, when log replay takes a real reference. clean_log_buffer() already does the same for log tree blocks when it has no transaction.

The transaction that logged the extent also must not commit, since the commit would drop the log, the only record of the fsynced data. It can't have committed yet because it waits for this ordered extent. Every failure after joining the transaction already aborts it, and a failed join means the transaction already aborted or the filesystem already has an error, unless allocating the handle failed. In that case there's no handle to abort with, so set the error with btrfs_handle_fs_error(). Once the filesystem has an error, the commit fails to write its tree blocks and never writes a superblock.

A completion can also fail while the fsync is logging: the log already points at the extent, but the completion checks BTRFS_ORDERED_LOGGED before the fsync sets it and frees the extent. To close that window, set BTRFS_ORDERED_LOGGED and check BTRFS_ORDERED_IOERR under the ordered tree lock. The error path sets IOERR before checking LOGGED, so either it sees LOGGED and keeps the extent, or the fsync sees IOERR and fails logging, which forces a full commit instead of a log sync.

This depends on "btrfs: wait for ordered extent IO in fast fsync", which stops a fast fsync from logging ordered extents whose write failed. So only failures after the IO is done, from an abort or a failed join, reach the new path, and a plain write error doesn't force the filesystem read-only.

Without this patch, delaying and then failing reads on the device while fsync-heavy writers and writers that never sync are running left persistent csum errors after log replay in 5 of 24 runs where the transaction aborted.

Fixes 487781796d30 ("btrfs: make fast fsyncs wait only for writeback")

---
This applies on for-next with Josef's "btrfs: data integrity fixes" v2 [1] and depends on "btrfs: wait for ordered extent IO in fast fsync" from it. That patch keeps a fast fsync from logging an ordered extent whose write failed. Without it, a plain write error with no abort could reach the new path with BTRFS_ORDERED_LOGGED set, and this patch would force the filesystem read-only over it. With it, only an abort (already read-only) or a failed handle allocation gets there, so a backport needs both.

[1] https://lore.kernel.org/linux-btrfs/20261103-b4-btrfs-data-integrity-v2-0-577dc2690061@toxicpanda.com/

 fs/btrfs/inode.c    | 57 +++++++++++++++++++++++++++++++++++++--------
 fs/btrfs/tree-log.c | 21 ++++++++++-------
 2 files changed, 60 insertions(+), 18 deletions(-)
fs/btrfs/inode.c +47 -10
index 19b1eba2dd84..c8af19a05405 100644
@@ -3170,6 +3170,30 @@ static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
 					   update_inode_bytes, oe->qgroup_rsv);
 }
 
+/*
+ * The log may already reference this extent, so keep it out of the free space
+ * cache until log replay at the next mount.
+ *
+ * The transaction that logged it must not commit either, since that would drop
+ * the log. Every failure after joining it aborts it, and a failed join means it
+ * already aborted or the filesystem already has an error, unless allocating
+ * the handle failed. In that case set the error here, so the commit fails to
+ * write its tree blocks and never writes a superblock.
+ */
+static void keep_logged_extent(struct btrfs_ordered_extent *oe, int error)
+{
+	struct btrfs_fs_info *fs_info = oe->inode->root->fs_info;
+	struct btrfs_block_group *bg;
+
+	if (!BTRFS_FS_ERROR(fs_info))
+		btrfs_handle_fs_error(fs_info, error, NULL);
+
+	bg = btrfs_lookup_block_group(fs_info, oe->disk_bytenr);
+	ASSERT(bg);
+	btrfs_free_reserved_bytes(bg, oe->disk_num_bytes, true);
+	btrfs_put_block_group(bg);
+}
+
 /*
  * As ordered data IO finishes, this gets called so we can finish
  * an ordered extent if the range of bytes in the file it covers are
@@ -3373,18 +3397,31 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
 		    clear_reserved_extent &&
 		    !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
 		    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
+			const u64 disk_bytenr = ordered_extent->disk_bytenr;
+			const u64 disk_num_bytes = ordered_extent->disk_num_bytes;
+			bool logged;
+
 			/*
-			 * Discard the range before returning it back to the
-			 * free space pool
+			 * A fast fsync may have logged this extent. Pairs with
+			 * the end of btrfs_log_changed_extents().
 			 */
-			if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
-				btrfs_discard_extent(fs_info,
-						ordered_extent->disk_bytenr,
-						ordered_extent->disk_num_bytes,
-						NULL, true);
-			btrfs_free_reserved_extent(fs_info,
-					ordered_extent->disk_bytenr,
-					ordered_extent->disk_num_bytes, true);
+			spin_lock(&inode->ordered_tree_lock);
+			logged = test_bit(BTRFS_ORDERED_LOGGED, &ordered_extent->flags);
+			spin_unlock(&inode->ordered_tree_lock);
+
+			if (ret && logged) {
+				keep_logged_extent(ordered_extent, ret);
+			} else {
+				/*
+				 * Discard the range before returning it back to
+				 * the free space pool
+				 */
+				if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
+					btrfs_discard_extent(fs_info, disk_bytenr,
+							     disk_num_bytes, NULL, true);
+				btrfs_free_reserved_extent(fs_info, disk_bytenr,
+							   disk_num_bytes, true);
+			}
 			/*
 			 * Actually free the qgroup rsv which was released when
 			 * the ordered extent was created.
fs/btrfs/tree-log.c +13 -8
index e15b26f9b920..bd04dceca700 100644
@@ -5455,23 +5455,28 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
 	 * before it commits and wipes out the log trees, otherwise we would
 	 * lose data if an ordered extents completes after the transaction
 	 * commits and a power failure happens after the transaction commit.
+	 *
+	 * A failed ordered extent keeps its extent allocated only if it sees
+	 * BTRFS_ORDERED_LOGGED. It sets BTRFS_ORDERED_IOERR before checking, so
+	 * if we see IOERR here it may have already freed the extent, and this
+	 * log must not be synced.
 	 */
 	list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
 		list_del_init(&ordered->log_list);
-		set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
 
+		spin_lock(&inode->ordered_tree_lock);
+		set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
+		if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
+			ret = -EIO;
 		if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
-			spin_lock(&inode->ordered_tree_lock);
-			if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
-				set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
-				atomic_inc(&trans->transaction->pending_ordered);
-			}
-			spin_unlock(&inode->ordered_tree_lock);
+			set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
+			atomic_inc(&trans->transaction->pending_ordered);
 		}
+		spin_unlock(&inode->ordered_tree_lock);
 		btrfs_put_ordered_extent(ordered);
 	}
 
-	return 0;
+	return ret;
 }
 
 static int get_inode_size_to_log(struct btrfs_trans_handle *trans,

base-commit: 5e79ca7342fb6e8c7ead78abc422d24396cff1cf
prerequisite-patch-id: 4a028e5abff0520f479d35aa5cb8a1bc5091d1ac

Signature · 1 line
2.53.0-Meta
#1 lore raw
Quoted 134 lines · Leo Martins

A fast fsync doesn't wait for its ordered extents to complete. Once their IO is done it logs the new extents, with the checksums from the ordered extents, and syncs the log. Until an ordered extent completes and inserts its file extent item, the log is the only thing referencing its extent.

If the transaction aborts before that, the ordered extent fails to join the transaction and its error path returns the extent to the free space cache, even though the log still points at it. Writeback keeps going after the abort, so a page dirtied earlier can get allocated that range and overwrite it. When the log is replayed on the next mount, the file points at the overwritten blocks with the old checksums, and every read fails with a csum error while btrfs check reports no problems.

fsync completion of X writeback

write X, wait for IO
log X, sync the log
transaction aborts,
X returned to the
free space cache
allocate X, overwrite X
mount: log replay points the file at X, with X's logged checksums

Fix this by not freeing the extent of a failed ordered extent that a fast fsync logged (BTRFS_ORDERED_LOGGED). Release its reservation but keep the range out of the free space cache until the next mount, when log replay takes a real reference. clean_log_buffer() already does the same for log tree blocks when it has no transaction.

The transaction that logged the extent also must not commit, since the commit would drop the log, the only record of the fsynced data. It can't have committed yet because it waits for this ordered extent. Every failure after joining the transaction already aborts it, and a failed join means the transaction already aborted or the filesystem already has an error, unless allocating the handle failed. In that case there's no handle to abort with, so set the error with btrfs_handle_fs_error(). Once the filesystem has an error, the commit fails to write its tree blocks and never writes a superblock.

A completion can also fail while the fsync is logging: the log already points at the extent, but the completion checks BTRFS_ORDERED_LOGGED before the fsync sets it and frees the extent. To close that window, set BTRFS_ORDERED_LOGGED and check BTRFS_ORDERED_IOERR under the ordered tree lock. The error path sets IOERR before checking LOGGED, so either it sees LOGGED and keeps the extent, or the fsync sees IOERR and fails logging, which forces a full commit instead of a log sync.

This depends on "btrfs: wait for ordered extent IO in fast fsync", which stops a fast fsync from logging ordered extents whose write failed. So only failures after the IO is done, from an abort or a failed join, reach the new path, and a plain write error doesn't force the filesystem read-only.

Without this patch, delaying and then failing reads on the device while fsync-heavy writers and writers that never sync are running left persistent csum errors after log replay in 5 of 24 runs where the transaction aborted.

Fixes 487781796d30 ("btrfs: make fast fsyncs wait only for writeback")

---
This applies on for-next with Josef's "btrfs: data integrity fixes" v2 [1] and depends on "btrfs: wait for ordered extent IO in fast fsync" from it. That patch keeps a fast fsync from logging an ordered extent whose write failed. Without it, a plain write error with no abort could reach the new path with BTRFS_ORDERED_LOGGED set, and this patch would force the filesystem read-only over it. With it, only an abort (already read-only) or a failed handle allocation gets there, so a backport needs both.

[1] https://lore.kernel.org/linux-btrfs/20261103-b4-btrfs-data-integrity-v2-0-577dc2690061@toxicpanda.com/

 fs/btrfs/inode.c    | 57 +++++++++++++++++++++++++++++++++++++--------
 fs/btrfs/tree-log.c | 21 ++++++++++-------
 2 files changed, 60 insertions(+), 18 deletions(-)
fs/btrfs/inode.c +30 -10
index 19b1eba2dd84..c8af19a05405 100644
@@ -3170,6 +3170,30 @@ static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
                                           update_inode_bytes, oe->qgroup_rsv);
 }
 
+/*
+ * The log may already reference this extent, so keep it out of the free space
+ * cache until log replay at the next mount.
+ *
+ * The transaction that logged it must not commit either, since that would drop
+ * the log. Every failure after joining it aborts it, and a failed join means it
+ * already aborted or the filesystem already has an error, unless allocating
+ * the handle failed. In that case set the error here, so the commit fails to
+ * write its tree blocks and never writes a superblock.
+ */
+static void keep_logged_extent(struct btrfs_ordered_extent *oe, int error)
+{
+       struct btrfs_fs_info *fs_info = oe->inode->root->fs_info;
+       struct btrfs_block_group *bg;
+
+       if (!BTRFS_FS_ERROR(fs_info))
+               btrfs_handle_fs_error(fs_info, error, NULL);
+
+       bg = btrfs_lookup_block_group(fs_info, oe->disk_bytenr);
+       ASSERT(bg);
+       btrfs_free_reserved_bytes(bg, oe->disk_num_bytes, true);
+       btrfs_put_block_group(bg);
+}
+
 /*
  * As ordered data IO finishes, this gets called so we can finish
  * an ordered extent if the range of bytes in the file it covers are
@@ -3373,18 +3397,31 @@ int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent)
                    clear_reserved_extent &&
                    !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
                    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
+                       const u64 disk_bytenr = ordered_extent->disk_bytenr;
+                       const u64 disk_num_bytes = ordered_extent->disk_num_bytes;
+                       bool logged;
+
                        /*
-                        * Discard the range before returning it back to the
-                        * free space pool
+                        * A fast fsync may have logged this extent. Pairs with
+                        * the end of btrfs_log_changed_extents().
                         */
-                       if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
-                               btrfs_discard_extent(fs_info,
-                                               ordered_extent->disk_bytenr,
-                                               ordered_extent->disk_num_bytes,
-                                               NULL, true);
-                       btrfs_free_reserved_extent(fs_info,
-                                       ordered_extent->disk_bytenr,
-                                       ordered_extent->disk_num_bytes, true);

fs/btrfs/inode.c int btrfs_finish_one_ordered(struct btrfs_ordered_extent *ordered_extent) · line 3417

Quoted 10 lines
fs/btrfs/inode.c +10 -0
+                       spin_lock(&inode->ordered_tree_lock);
+                       logged = test_bit(BTRFS_ORDERED_LOGGED, &ordered_extent->flags);
+                       spin_unlock(&inode->ordered_tree_lock);
+
+                       if (ret && logged) {
+                               keep_logged_extent(ordered_extent, ret);
+                       } else {
+                               /*
+                                * Discard the range before returning it back to
+                                * the free space pool

Since we are here, we can fix the original comment and add punctuation.

Looks good in general, but there are some valid points from Sashiko regarding writes into prealloc extents:

https://sashiko.dev/#/patchset/54da67463c57f41302f8456a8a7a1190dcb65ae2.1794266796.git.loemra.dev%40gmail.com

Thanks.

Quoted 58 lines
fs/btrfs/inode.c +7 -0
+                                */
+                               if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
+                                       btrfs_discard_extent(fs_info, disk_bytenr,
+                                                            disk_num_bytes, NULL, true);
+                               btrfs_free_reserved_extent(fs_info, disk_bytenr,
+                                                          disk_num_bytes, true);
+                       }
                        /*
                         * Actually free the qgroup rsv which was released when
                         * the ordered extent was created.
fs/btrfs/tree-log.c +13 -8
index e15b26f9b920..bd04dceca700 100644
@@ -5455,23 +5455,28 @@ static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
         * before it commits and wipes out the log trees, otherwise we would
         * lose data if an ordered extents completes after the transaction
         * commits and a power failure happens after the transaction commit.
+        *
+        * A failed ordered extent keeps its extent allocated only if it sees
+        * BTRFS_ORDERED_LOGGED. It sets BTRFS_ORDERED_IOERR before checking, so
+        * if we see IOERR here it may have already freed the extent, and this
+        * log must not be synced.
         */
        list_for_each_entry_safe(ordered, tmp, &ctx->ordered_extents, log_list) {
                list_del_init(&ordered->log_list);
-               set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
 
+               spin_lock(&inode->ordered_tree_lock);
+               set_bit(BTRFS_ORDERED_LOGGED, &ordered->flags);
+               if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags))
+                       ret = -EIO;
                if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
-                       spin_lock(&inode->ordered_tree_lock);
-                       if (!test_bit(BTRFS_ORDERED_COMPLETE, &ordered->flags)) {
-                               set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
-                               atomic_inc(&trans->transaction->pending_ordered);
-                       }
-                       spin_unlock(&inode->ordered_tree_lock);
+                       set_bit(BTRFS_ORDERED_PENDING, &ordered->flags);
+                       atomic_inc(&trans->transaction->pending_ordered);
                }
+               spin_unlock(&inode->ordered_tree_lock);
                btrfs_put_ordered_extent(ordered);
        }
 
-       return 0;
+       return ret;
 }
 
 static int get_inode_size_to_log(struct btrfs_trans_handle *trans,

base-commit: 5e79ca7342fb6e8c7ead78abc422d24396cff1cf
prerequisite-patch-id: 4a028e5abff0520f479d35aa5cb8a1bc5091d1ac --
2.53.0-Meta