Skip to contentSkip to conversation

Error condition failure fixes

PATCH In review Josef Bacik 2 participants 6 messages Feb 13, 2020 – Feb 18, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

v2 cover Error condition failure fixes

v1->v2:
- Fixed the ins.objectid thing Nikolay pointed out, and reworked that patch a bit to do the right thing in case of a add_extent_mapping() further down in that function as well.

------------------- Original email ----------------------------- I've been running my bpf error injection stress script and been fixing what has fallen out. I don't think I've fixed everything yet, but to reduce the noise from Dave's testing here's the current set of fixes I have. These are based on misc-next from late last week, but should apply cleanly to the recent set. These aren't super important, but will cut down on the noise from things like generic/019 and generic/475. Thanks,

Josef

#1 lore raw

PATCH v2 1/4 btrfs: set fs_root = NULL on error

While running my error injection script I hit a panic when we tried to clean up the fs_root when free'ing the fs_root. This is because fs_info->fs_root == PTR_ERR(-EIO), which isn't great. Fix this by setting fs_info->fs_root = NULL; if we fail to read the root.

---
 fs/btrfs/disk-io.c | 1 +
 1 file changed, 1 insertion(+)
fs/btrfs/disk-io.c +1 -0
index eb441fa3711b..5b6140482cef 100644
@@ -3260,6 +3260,7 @@ int __cold open_ctree(struct super_block *sb,
 	if (IS_ERR(fs_info->fs_root)) {
 		err = PTR_ERR(fs_info->fs_root);
 		btrfs_warn(fs_info, "failed to read fs tree: %d", err);
+		fs_info->fs_root = NULL;
 		goto fail_qgroup;
 	}
 
Signature · 1 line
2.24.1
#2 lore raw

PATCH v2 2/4 btrfs: do not check delayed items are empty for single trans cleanup

In reply to Josef Bacik

btrfs_assert_delayed_root_empty() will check if the delayed root is completely empty, but this is a fs wide check. On cleanup we may have allowed other transactions to begin, for whatever reason, and thus the delayed root is not empty. So remove this check from cleanup_one_transation(). This however can stay in btrfs_cleanup_transaction(), because it checks only after all of the transactions have been properly cleaned up, and thus is valid.

---
 fs/btrfs/disk-io.c | 1 -
 1 file changed, 1 deletion(-)
fs/btrfs/disk-io.c +0 -1
index 5b6140482cef..601ed3335cf6 100644
@@ -4543,7 +4543,6 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
 	wake_up(&fs_info->transaction_wait);
 
 	btrfs_destroy_delayed_inodes(fs_info);
-	btrfs_assert_delayed_root_empty(fs_info);
 
 	btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
 				     EXTENT_DIRTY);
Signature · 1 line
2.24.1
#3 lore raw

PATCH v2 3/4 btrfs: handle logged extent failure properly

In reply to Josef Bacik

If we're allocating a logged extent we attempt to insert an extent record for the file extent directly. We increase space_info->bytes_reserved, because the extent entry addition will call btrfs_update_block_group(), which will convert the ->bytes_reserved to ->bytes_used. However if we fail at any point while inserting the extent entry we will bail and leave space on ->bytes_reserved, which will trigger a WARN_ON() on umount. Fix this by pinning the space if we fail to insert, which is what happens in every other failure case that involves adding the extent entry.

---
 fs/btrfs/extent-tree.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/extent-tree.c +2 -0
index c43acb329fa6..2b4c3ca5e651 100644
@@ -4430,6 +4430,8 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
 
 	ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
 					 offset, ins, 1);
+	if (ret)
+		btrfs_pin_extent(fs_info, ins->objectid, ins->offset, 1);
 	btrfs_put_block_group(block_group);
 	return ret;
 }
Signature · 1 line
2.24.1
#4 lore raw

PATCH v2 4/4 btrfs: fix bytes_may_use underflow in prealloc error condtition

In reply to Josef Bacik

I hit the following warning while running my error injection stress testing

------------[ cut here ]------------
WARNING: CPU: 3 PID: 1453 at fs/btrfs/space-info.h:108 btrfs_free_reserved_data_space_noquota+0xfd/0x160 [btrfs]
RIP: 0010:btrfs_free_reserved_data_space_noquota+0xfd/0x160 [btrfs]
Call Trace:
btrfs_free_reserved_data_space+0x4f/0x70 [btrfs]
__btrfs_prealloc_file_range+0x378/0x470 [btrfs]
elfcorehdr_read+0x40/0x40
? elfcorehdr_read+0x40/0x40
? btrfs_commit_transaction+0xca/0xa50 [btrfs]
? dput+0xb4/0x2a0
? btrfs_log_dentry_safe+0x55/0x70 [btrfs]
? btrfs_sync_file+0x30e/0x420 [btrfs]
? do_fsync+0x38/0x70
? __x64_sys_fdatasync+0x13/0x20
? do_syscall_64+0x5b/0x1b0
? entry_SYSCALL_64_after_hwframe+0x44/0xa9
---[ end trace 70ccb5d0fe51151c ]---

This happens if we fail to insert our reserved file extent. At this point we've already converted our reservation from ->bytes_may_use to ->bytes_reserved. However once we break we will attempt to free everything from [cur_offset, end] from ->bytes_may_use, but our extent reservation will overlap part of this.

Fix this problem by adding ins.offset (our extent allocation size) to cur_offset so we remove the actual remaining part from ->bytes_may_use.

I validated this fix using my inject-error.py script

python inject-error.py -o should_fail_bio -t cache_save_setup -t \
	__btrfs_prealloc_file_range \
	-t insert_reserved_file_extent.constprop.0 \
	-r "-5" ./run-fsstress.sh

where run-fsstress.sh simply mounts and runs fsstress on a disk.

---
 fs/btrfs/inode.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
fs/btrfs/inode.c +13 -3
index 84e649724549..ffc6fcfe805c 100644
@@ -9876,6 +9876,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
 	struct btrfs_root *root = BTRFS_I(inode)->root;
 	struct btrfs_key ins;
 	u64 cur_offset = start;
+	u64 clear_offset = start;
 	u64 i_size;
 	u64 cur_bytes;
 	u64 last_alloc = (u64)-1;
@@ -9910,6 +9911,15 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
 				btrfs_end_transaction(trans);
 			break;
 		}
+
+		/*
+		 * We've reserved this space, and thus converted it from
+		 * ->bytes_may_use to ->bytes_reserved.  Any error that happens
+		 * from here on out we will only need to clear our reservation
+		 * for the remaining unreserved area, so advance our
+		 * clear_offset by our extent size.
+		 */
+		clear_offset += ins.offset;
 		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
 
 		last_alloc = ins.offset;
@@ -9989,9 +9999,9 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
 		if (own_trans)
 			btrfs_end_transaction(trans);
 	}
-	if (cur_offset < end)
-		btrfs_free_reserved_data_space(inode, NULL, cur_offset,
-			end - cur_offset + 1);
+	if (clear_offset < end)
+		btrfs_free_reserved_data_space(inode, NULL, clear_offset,
+			end - clear_offset + 1);
 	return ret;
 }
 
Signature · 1 line
2.24.1
#5 lore raw
In reply to Josef Bacik
Quoted 5 lines · Josef Bacik

v1->v2:
- Fixed the ins.objectid thing Nikolay pointed out, and reworked that patch a bit to do the right thing in case of a add_extent_mapping() further down in that function as well.

Quoted 7 lines

------------------- Original email ----------------------------- I've been running my bpf error injection stress script and been fixing what has fallen out. I don't think I've fixed everything yet, but to reduce the noise from Dave's testing here's the current set of fixes I have. These are based on misc-next from late last week, but should apply cleanly to the recent set. These aren't super important, but will cut down on the noise from things like generic/019 and generic/475. Thanks,

The VM with 1G of memory does not complain anymore and completes the whole fstests. Patchset moved from topic branch to misc-next and despite the fixes are not considered serious I'll forward them to current rc. Thanks.