Skip to contentSkip to conversation

Btrfs: defrag: the remaining unmerged part

PATCH New Qu Wenruo 1 participant 8 messages Feb 24, 2022

Subsystems (2)
lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover Btrfs: defrag: the remaining unmerged part

David has already rebased all bug fixes into his fix/autodefrag-io branch.
(Although the last patch still lacks one line to make it work).

This patchset is the remaining related cleanups.

If the 2nd patch doesn't apply, it means the patch "trfs:
reduce extent threshold for autodefrag" is not correct, and doesn't apply extent_thresh properly.

The first patch removes one unused parameter.
The second patch makes __btrfs_run_defrag_inode() to meet our current standard.

Patch 3~6 re-introduce the btrfs_defrag_ctrl to cleanup the argument lists, and make btrfs_defrag_file() reports accurate sectors_defragged.

The last patch introduces the needed trace events.
With the trace events, it's much easier to verify the defrag behavior, and provide a user-friendly way to debug defrag bugs.

Originally I used those trace events to verfy the reduced extent_thresh for autodefrag, but I found out a better way to verify the behavior without using trace events.

So I'll send a new test case to check the changed autodefrag behavior.

Qu Wenruo (7):
btrfs: remove unused parameter for btrfs_add_inode_defrag()
btrfs: refactor __btrfs_run_defrag_inode()
btrfs: uapi: introduce BTRFS_DEFRAG_RANGE_MASK for later sanity check
btrfs: defrag: introduce btrfs_defrag_ctrl structure for later usage
btrfs: defrag: use btrfs_defrag_ctrl to replace
btrfs_ioctl_defrag_range_args for btrfs_defrag_file()
btrfs: defrag: make btrfs_defrag_file() to report accurate number of
defragged sectors
btrfs: add trace events for defrag

 fs/btrfs/ctree.h             |  25 +++-
 fs/btrfs/file.c              |  92 +++++++--------
 fs/btrfs/inode.c             |   2 +-
 fs/btrfs/ioctl.c             | 213 ++++++++++++++++++-----------------
 include/trace/events/btrfs.h | 127 +++++++++++++++++++++
 include/uapi/linux/btrfs.h   |   6 +-
 6 files changed, 310 insertions(+), 155 deletions(-)
Signature · 1 line
2.35.1
author
#2 lore raw

PATCH 2/7 btrfs: refactor __btrfs_run_defrag_inode()

This refactor involves:

- Use while () loop to replace open-coded loop

- Add extra comments on btrfs_get_fs_root()/btrfs_iget() calls

- Add extra comment on the meaning of range.start

- Move @root, @inode and @range into the loop

- Initial @range to 0 at its declaration
So we don't need to reset it to zero.

There is one behavior change:

- Now we check @cur against @isize from last iteration. And the initial dummy @isize is set to (u64)-1 so we can always initial the loop.

This slightly enlarge the race window where isize can be changed.

---
 fs/btrfs/file.c | 85 ++++++++++++++++++++++++++-----------------------
 1 file changed, 45 insertions(+), 40 deletions(-)
fs/btrfs/file.c +45 -40
index 7d096c3f9127..5b11a8c72114 100644
@@ -247,57 +247,62 @@ void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info)
 static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
 				    struct inode_defrag *defrag)
 {
-	struct btrfs_root *inode_root;
-	struct inode *inode;
-	struct btrfs_ioctl_defrag_range_args range;
 	int ret = 0;
+	/* Initially we don't have the inode, use -1 as dummy isize */
+	u64 isize = (u64)-1;
 	u64 cur = 0;
 
-again:
-	if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state))
-		goto cleanup;
-	if (!__need_auto_defrag(fs_info))
-		goto cleanup;
+	while (cur < isize) {
+		struct btrfs_ioctl_defrag_range_args range = { 0 };
+		struct btrfs_root *inode_root;
+		struct inode *inode;
 
-	/* get the inode */
-	inode_root = btrfs_get_fs_root(fs_info, defrag->root, true);
-	if (IS_ERR(inode_root)) {
-		ret = PTR_ERR(inode_root);
-		goto cleanup;
-	}
+		if (test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state))
+			break;
+		if (!__need_auto_defrag(fs_info))
+			break;
 
-	inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
-	btrfs_put_root(inode_root);
-	if (IS_ERR(inode)) {
-		ret = PTR_ERR(inode);
-		goto cleanup;
-	}
+		/* Check if the root is still there and grab it */
+		inode_root = btrfs_get_fs_root(fs_info, defrag->root, true);
+		if (IS_ERR(inode_root)) {
+			ret = PTR_ERR(inode_root);
+			break;
+		}
 
-	if (cur >= i_size_read(inode)) {
-		iput(inode);
-		goto cleanup;
-	}
+		/* Check if the inode is still there and grab it */
+		inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
+		btrfs_put_root(inode_root);
+		if (IS_ERR(inode)) {
+			ret = PTR_ERR(inode);
+			break;
+		}
 
-	/* do a chunk of defrag */
-	clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
-	memset(&range, 0, sizeof(range));
-	range.len = (u64)-1;
-	range.start = cur;
-	range.extent_thresh = defrag->extent_thresh;
+		isize = i_size_read(inode);
 
-	sb_start_write(fs_info->sb);
-	ret = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
-				       BTRFS_DEFRAG_BATCH);
-	sb_end_write(fs_info->sb);
-	iput(inode);
+		/* do a chunk of defrag */
+		clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
+		range.len = (u64)-1;
+		range.start = cur;
+		range.extent_thresh = defrag->extent_thresh;
 
-	if (ret < 0)
-		goto cleanup;
+		sb_start_write(fs_info->sb);
+		ret = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
+				       BTRFS_DEFRAG_BATCH);
+		sb_end_write(fs_info->sb);
+		iput(inode);
 
-	cur = max(cur + fs_info->sectorsize, range.start);
-	goto again;
+		if (ret < 0)
+			break;
 
-cleanup:
+		/*
+		 * Range.start is the last scanned bytenr.
+		 *
+		 * And just in case the last scanned bytenr doesn't get
+		 * increased at all, at least start from the next sector
+		 * of current bytenr.
+		 */
+		cur = max(cur + fs_info->sectorsize, range.start);
+	}
 	kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
 	return ret;
 }
Signature · 1 line
2.35.1
author
#3 lore raw

PATCH 3/7 btrfs: uapi: introduce BTRFS_DEFRAG_RANGE_MASK for later sanity check

In reply to Qu Wenruo

And since we're here, replace the hardcoded bit flags (1, 2) with (1UL << 0) and (1UL << 1), respectively.

---
 include/uapi/linux/btrfs.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/uapi/linux/btrfs.h +4 -2
index d956b2993970..3d4bfdacf935 100644
@@ -576,8 +576,10 @@ struct btrfs_ioctl_clone_range_args {
  * Used by:
  * struct btrfs_ioctl_defrag_range_args.flags
  */
-#define BTRFS_DEFRAG_RANGE_COMPRESS 1
-#define BTRFS_DEFRAG_RANGE_START_IO 2
+#define BTRFS_DEFRAG_RANGE_COMPRESS	(1UL << 0)
+#define BTRFS_DEFRAG_RANGE_START_IO	(1UL << 1)
+#define BTRFS_DEFRAG_RANGE_FLAGS_MASK	(BTRFS_DEFRAG_RANGE_COMPRESS |\
+					 BTRFS_DEFRAG_RANGE_START_IO)
 struct btrfs_ioctl_defrag_range_args {
 	/* start of the defrag operation */
 	__u64 start;
Signature · 1 line
2.35.1
author
#4 lore raw

PATCH 4/7 btrfs: defrag: introduce btrfs_defrag_ctrl structure for later usage

In reply to Qu Wenruo

Currently btrfs_defrag_file() accepts not only btrfs_ioctl_defrag_range_args but also other parameters like @newer_than and @max_sectors_to_defrag for extra policies.

Those extra values are hidden from defrag ioctl and even caused bugs in the past due to different behaviors based on those extra values.

Here we introduce a new structure, btrfs_defrag_ctrl, to include:

- all members in btrfs_ioctl_defrag_range_args

- @max_sectors_to_defrag and @newer_than

- Extra values which callers of btrfs_defrag_file() may care Like @sectors_defragged and @last_scanned.

With the new structure, also introduce a new helper, btrfs_defrag_ioctl_args_to_ctrl() to:

- Do extra sanity check on @compress and @flags

- Do range alignment when possible

- Set default values.

---
 fs/btrfs/ctree.h | 19 +++++++++++++++++++
 fs/btrfs/ioctl.c | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
fs/btrfs/ctree.h +19 -0
index c72790c3bd41..97a9f17fc7f1 100644
@@ -3325,6 +3325,25 @@ int btrfs_fileattr_set(struct user_namespace *mnt_userns,
 int btrfs_ioctl_get_supported_features(void __user *arg);
 void btrfs_sync_inode_flags_to_i_flags(struct inode *inode);
 int __pure btrfs_is_empty_uuid(u8 *uuid);
+
+struct btrfs_defrag_ctrl {
+	/* Input, read-only fields */
+	u64	start;
+	u64	len;
+	u32	extent_thresh;
+	u64	newer_than;
+	u64	max_sectors_to_defrag;
+	u8	compress;
+	u8	flags;
+
+	/* Output fields */
+	u64	sectors_defragged;
+	u64	last_scanned;	/* Exclusive bytenr */
+};
+int btrfs_defrag_ioctl_args_to_ctrl(struct btrfs_fs_info *fs_info,
+				    struct btrfs_ioctl_defrag_range_args *args,
+				    struct btrfs_defrag_ctrl *ctrl,
+				    u64 max_sectors_to_defrag, u64 newer_than);
 int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
 		      struct btrfs_ioctl_defrag_range_args *range,
 		      u64 newer_than, unsigned long max_to_defrag);
fs/btrfs/ioctl.c +41 -0

47 lines not on this page · raw message

Signature · 1 line
2.35.1
author
#5 lore raw

PATCH 5/7 btrfs: defrag: use btrfs_defrag_ctrl to replace btrfs_ioctl_defrag_range_args for btrfs_defrag_file()

In reply to Qu Wenruo

This brings the following benefits:

- No more strange range->start update to indicate last scanned bytenr We have btrfs_defrag_ctrl::last_scanned (exclusive) for it directly.

- No more return value to indicate defragged sectors Now btrfs_defrag_file() will just return 0 if no error happened. And btrfs_defrag_ctrl::sectors_defragged will show that value.

- Less parameters to carry around
Now most defrag_* functions only need to fetch its policy parameters from btrfs_defrag_ctrl directly.

---
 fs/btrfs/ctree.h |   3 +-
 fs/btrfs/file.c  |  17 +++--
 fs/btrfs/ioctl.c | 162 +++++++++++++++++++----------------------------
 3 files changed, 75 insertions(+), 107 deletions(-)
fs/btrfs/ctree.h +1 -2
index 97a9f17fc7f1..a8a3de10cead 100644
@@ -3345,8 +3345,7 @@ int btrfs_defrag_ioctl_args_to_ctrl(struct btrfs_fs_info *fs_info,
 				    struct btrfs_defrag_ctrl *ctrl,
 				    u64 max_sectors_to_defrag, u64 newer_than);
 int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
-		      struct btrfs_ioctl_defrag_range_args *range,
-		      u64 newer_than, unsigned long max_to_defrag);
+		      struct btrfs_defrag_ctrl *ctrl);
 void btrfs_get_block_group_info(struct list_head *groups_list,
 				struct btrfs_ioctl_space_info *space);
 void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
fs/btrfs/file.c +8 -9
index 5b11a8c72114..d50d6b042e67 100644
@@ -253,7 +253,7 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
 	u64 cur = 0;
 
 	while (cur < isize) {
-		struct btrfs_ioctl_defrag_range_args range = { 0 };
+		struct btrfs_defrag_ctrl ctrl = {0};
 		struct btrfs_root *inode_root;
 		struct inode *inode;
 
@@ -281,13 +281,14 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
 
 		/* do a chunk of defrag */
 		clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
-		range.len = (u64)-1;
-		range.start = cur;
-		range.extent_thresh = defrag->extent_thresh;
+		ctrl.len = (u64)-1;
+		ctrl.start = cur;
+		ctrl.newer_than = defrag->transid;
+		ctrl.extent_thresh = defrag->extent_thresh;
+		ctrl.max_sectors_to_defrag = BTRFS_DEFRAG_BATCH;
 
 		sb_start_write(fs_info->sb);
-		ret = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
-				       BTRFS_DEFRAG_BATCH);
+		ret = btrfs_defrag_file(inode, NULL, &ctrl);
 		sb_end_write(fs_info->sb);
 		iput(inode);
 
@@ -295,13 +296,11 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
 			break;
 
 		/*
-		 * Range.start is the last scanned bytenr.
-		 *
 		 * And just in case the last scanned bytenr doesn't get
 		 * increased at all, at least start from the next sector
 		 * of current bytenr.
 		 */
-		cur = max(cur + fs_info->sectorsize, range.start);
+		cur = max(cur + fs_info->sectorsize, ctrl.last_scanned);
 	}
 	kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
 	return ret;
fs/btrfs/ioctl.c +66 -96

337 lines not on this page · raw message

Signature · 1 line
2.35.1
author
#7 lore raw

PATCH 7/7 btrfs: add trace events for defrag

In reply to Qu Wenruo

This patch will introduce the following trace events:

- trace_defrag_add_target()
- trace_defrag_one_locked_range()
- trace_defrag_file_start()
- trace_defrag_file_end()

Under most cases, all of them are needed to debug policy related defrag bugs.

The example output would look like this: (with TASK, CPU, TIMESTAMP and UUID skipped)

defrag_file_start: <UUID>: root=5 ino=257 start=0 len=131072 extent_thresh=262144 newer_than=7 flags=0x0 compress=0 max_sectors_to_defrag=1024
defrag_add_target: <UUID>: root=5 ino=257 target_start=0 target_len=4096 found em=0 len=4096 generation=7
defrag_add_target: <UUID>: root=5 ino=257 target_start=4096 target_len=4096 found em=4096 len=4096 generation=7
...
defrag_add_target: <UUID>: root=5 ino=257 target_start=57344 target_len=4096 found em=57344 len=4096 generation=7
defrag_add_target: <UUID>: root=5 ino=257 target_start=61440 target_len=4096 found em=61440 len=4096 generation=7
defrag_add_target: <UUID>: root=5 ino=257 target_start=0 target_len=4096 found em=0 len=4096 generation=7
defrag_add_target: <UUID>: root=5 ino=257 target_start=4096 target_len=4096 found em=4096 len=4096 generation=7
...
defrag_add_target: <UUID>: root=5 ino=257 target_start=57344 target_len=4096 found em=57344 len=4096 generation=7
defrag_add_target: <UUID>: root=5 ino=257 target_start=61440 target_len=4096 found em=61440 len=4096 generation=7
defrag_one_locked_range: <UUID>: root=5 ino=257 start=0 len=65536
defrag_file_end: <UUID>: root=5 ino=257 sectors_defragged=16 last_scanned=131072 ret=0

Although the defrag_add_target() part is lengthy, it shows some details of the extent map we get.
With the extra info from defrag_file_start(), we can check if the target em is correct for our defrag policy.

---
 fs/btrfs/ioctl.c             |   4 ++
 include/trace/events/btrfs.h | 127 +++++++++++++++++++++++++++++++++++
 2 files changed, 131 insertions(+)
fs/btrfs/ioctl.c +4 -0
index 1f0bf6181013..0d79dc28a9d4 100644
@@ -1486,6 +1486,7 @@ static int defrag_collect_targets(struct btrfs_inode *inode,
 add:
 		last_is_target = true;
 		range_len = min(extent_map_end(em), start + len) - cur;
+		trace_defrag_add_target(inode, em, cur, range_len);
 		/*
 		 * This one is a good target, check if it can be merged into
 		 * last range of the target list.
@@ -1581,6 +1582,7 @@ static int defrag_one_locked_target(struct btrfs_inode *inode,
 	ret = btrfs_delalloc_reserve_space(inode, &data_reserved, start, len);
 	if (ret < 0)
 		return ret;
+	trace_defrag_one_locked_range(inode, start, (u32)len);
 	clear_extent_bit(&inode->io_tree, start, start + len - 1,
 			 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
 			 EXTENT_DEFRAG, 0, 0, cached_state);
@@ -1818,6 +1820,7 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
 	cur = round_down(ctrl->start, fs_info->sectorsize);
 	ctrl->last_scanned = cur;
 	last_byte = round_up(last_byte, fs_info->sectorsize) - 1;
+	trace_defrag_file_start(BTRFS_I(inode), ctrl, cur, last_byte + 1 - cur);
 
 	/*
 	 * If we were not given a ra, allocate a readahead context. As
@@ -1905,6 +1908,7 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
 		BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE;
 		btrfs_inode_unlock(inode, 0);
 	}
+	trace_defrag_file_end(BTRFS_I(inode), ctrl, ret);
 	return ret;
 }
 
include/trace/events/btrfs.h +127 -0

139 lines not on this page · raw message

Signature · 1 line
2.35.1
author
#6 lore raw

PATCH 6/7 btrfs: defrag: make btrfs_defrag_file() to report accurate number of defragged sectors

In reply to Qu Wenruo

Previously rework btrfs_defrag_file() can only report the number of sectors from the first run of defrag_collect_targets().

This number is not accurate as if holes are punched after the first defrag_collect_targets() call, we will not choose to defrag the holes.

Originally this is to avoid passing @sectors_defragged to every involved functions.

But now since we have btrfs_defrag_ctrl, there is no need to do such inaccurate accounting, just update btrfs_defrag_ctrl::sectors_defragged after a successful defrag_one_locked_target() call.

---
 fs/btrfs/ioctl.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
fs/btrfs/ioctl.c +2 -8
index 72d157b89a17..1f0bf6181013 100644
@@ -1652,6 +1652,8 @@ static int defrag_one_range(struct btrfs_inode *inode,
 					       &cached_state);
 		if (ret < 0)
 			break;
+		ctrl->sectors_defragged += entry->len >>
+					   inode->root->fs_info->sectorsize_bits;
 	}
 
 	list_for_each_entry_safe(entry, tmp, &target_list, list) {
@@ -1716,17 +1718,9 @@ static int defrag_one_cluster(struct btrfs_inode *inode,
 				ra, NULL, entry->start >> PAGE_SHIFT,
 				((entry->start + range_len - 1) >> PAGE_SHIFT) -
 				(entry->start >> PAGE_SHIFT) + 1);
-		/*
-		 * Here we may not defrag any range if holes are punched before
-		 * we locked the pages.
-		 * But that's fine, it only affects the @sectors_defragged
-		 * accounting.
-		 */
 		ret = defrag_one_range(inode, ctrl, entry->start, range_len);
 		if (ret < 0)
 			break;
-		ctrl->sectors_defragged += range_len >>
-					   inode->root->fs_info->sectorsize_bits;
 	}
 out:
 	list_for_each_entry_safe(entry, tmp, &target_list, list) {
Signature · 1 line
2.35.1
author
#1 lore raw

PATCH 1/7 btrfs: remove unused parameter for btrfs_add_inode_defrag()

In reply to Qu Wenruo

Since commit 543eabd5e192 ("Btrfs: don't auto defrag a file when doing directIO"), there is no more caller passing a transaction handler to btrfs_add_inode_defrag().

So the @trans parameter of btrfs_add_inode_defrag() is unnecessary and we can just remove it.

---
 fs/btrfs/ctree.h | 3 +--
 fs/btrfs/file.c  | 8 ++------
 fs/btrfs/inode.c | 2 +-
 3 files changed, 4 insertions(+), 9 deletions(-)
fs/btrfs/ctree.h +1 -2
index 5e02bf13fd1b..c72790c3bd41 100644
@@ -3345,8 +3345,7 @@ void btrfs_exclop_balance(struct btrfs_fs_info *fs_info,
 /* file.c */
 int __init btrfs_auto_defrag_init(void);
 void __cold btrfs_auto_defrag_exit(void);
-int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
-			   struct btrfs_inode *inode, u32 extent_thresh);
+int btrfs_add_inode_defrag(struct btrfs_inode *inode, u32 extent_thresh);
 int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);
 void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);
 int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
fs/btrfs/file.c +2 -6
index 9f455c96c974..7d096c3f9127 100644
@@ -136,8 +136,7 @@ static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info)
  * insert a defrag record for this inode if auto defrag is
  * enabled
  */
-int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
-			   struct btrfs_inode *inode, u32 extent_thresh)
+int btrfs_add_inode_defrag(struct btrfs_inode *inode, u32 extent_thresh)
 {
 	struct btrfs_root *root = inode->root;
 	struct btrfs_fs_info *fs_info = root->fs_info;
@@ -151,10 +150,7 @@ int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
 	if (test_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags))
 		return 0;
 
-	if (trans)
-		transid = trans->transid;
-	else
-		transid = inode->root->last_trans;
+	transid = inode->root->last_trans;
 
 	defrag = kmem_cache_zalloc(btrfs_inode_defrag_cachep, GFP_NOFS);
 	if (!defrag)
fs/btrfs/inode.c +1 -1
index b2b26d786fde..24125d5860be 100644
@@ -560,7 +560,7 @@ static inline void inode_should_defrag(struct btrfs_inode *inode,
 	/* If this is a small write inside eof, kick off a defrag */
 	if (num_bytes < small_write &&
 	    (start > 0 || end + 1 < inode->disk_i_size))
-		btrfs_add_inode_defrag(NULL, inode, small_write);
+		btrfs_add_inode_defrag(inode, small_write);
 }
 
 /*
Signature · 1 line
2.35.1