Skip to content
Permalink
Browse files
btrfs: defrag: use control structure in btrfs_defrag_file()
Use defrag control structure and replace function arguments. 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.

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

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
adam900710 authored and kdave committed Feb 15, 2022
1 parent a65b76f commit e6c69fcbee7ef1d7bde4ff78eb1377dbe09d71cf
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 110 deletions.
@@ -3340,8 +3340,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,
@@ -277,8 +277,7 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
{
struct btrfs_root *inode_root;
struct inode *inode;
struct btrfs_ioctl_defrag_range_args range;
int num_defrag;
struct btrfs_defrag_ctrl ctrl = { 0 };
int ret;

/* get the inode */
@@ -297,21 +296,23 @@ 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);
memset(&range, 0, sizeof(range));
range.len = (u64)-1;
range.start = defrag->last_offset;
ctrl.len = (u64)-1;
ctrl.start = defrag->last_offset;
ctrl.newer_than = defrag->transid;
ctrl.max_sectors_to_defrag = BTRFS_DEFRAG_BATCH;

sb_start_write(fs_info->sb);
num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
BTRFS_DEFRAG_BATCH);
ret = btrfs_defrag_file(inode, NULL, &ctrl);
sb_end_write(fs_info->sb);
if (ret < 0)
goto out;
/*
* if we filled the whole defrag batch, there
* must be more work to do. Queue this defrag
* again
*/
if (num_defrag == BTRFS_DEFRAG_BATCH) {
defrag->last_offset = range.start;
if (ctrl.sectors_defragged == BTRFS_DEFRAG_BATCH) {
defrag->last_offset = ctrl.last_scanned;
btrfs_requeue_inode_defrag(BTRFS_I(inode), defrag);
} else if (defrag->last_offset && !defrag->cycled) {
/*
@@ -325,7 +326,7 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
} else {
kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
}

out:
iput(inode);
return 0;
cleanup:

0 comments on commit e6c69fc

Please sign in to comment.