Skip to content

Commit 008873e

Browse files
Li Zefankdave
authored andcommitted
Btrfs: honor extent thresh during defragmentation
We won't defrag an extent, if it's bigger than the threshold we specified and there's no small extent before it, but actually the code doesn't work this way. There are three bugs: - When should_defrag_range() decides we should keep on defragmenting an extent, last_len is not incremented. (old bug) - The length that passes to should_defrag_range() is not the length we're going to defrag. (new bug) - We always defrag 256K bytes data, and a big extent can be part of this range. (new bug) For a file with 4 extents: | 4K | 4K | 256K | 256K | The result of defrag with (the default) 256K extent thresh should be: | 264K | 256K | but with those bugs, we'll get: | 520K | Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
1 parent 83c8c9b commit 008873e

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

‎fs/btrfs/ioctl.c‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ static int should_defrag_range(struct inode *inode, u64 start, u64 len,
765765
int ret = 1;
766766

767767
/*
768-
* make sure that once we start defragging and extent, we keep on
768+
* make sure that once we start defragging an extent, we keep on
769769
* defragging it
770770
*/
771771
if (start < *defrag_end)
@@ -810,7 +810,6 @@ static int should_defrag_range(struct inode *inode, u64 start, u64 len,
810810
* extent will force at least part of that big extent to be defragged.
811811
*/
812812
if (ret) {
813-
*last_len += len;
814813
*defrag_end = extent_map_end(em);
815814
} else {
816815
*last_len = 0;
@@ -984,13 +983,14 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
984983
u64 skip = 0;
985984
u64 defrag_end = 0;
986985
u64 newer_off = range->start;
987-
int newer_left = 0;
988986
unsigned long i;
987+
unsigned long ra_index = 0;
989988
int ret;
990989
int defrag_count = 0;
991990
int compress_type = BTRFS_COMPRESS_ZLIB;
992991
int extent_thresh = range->extent_thresh;
993-
int newer_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
992+
int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
993+
int cluster = max_cluster;
994994
u64 new_align = ~((u64)128 * 1024 - 1);
995995
struct page **pages = NULL;
996996

@@ -1020,7 +1020,7 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
10201020
ra = &file->f_ra;
10211021
}
10221022

1023-
pages = kmalloc(sizeof(struct page *) * newer_cluster,
1023+
pages = kmalloc(sizeof(struct page *) * max_cluster,
10241024
GFP_NOFS);
10251025
if (!pages) {
10261026
ret = -ENOMEM;
@@ -1045,7 +1045,6 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
10451045
* the extents in the file evenly spaced
10461046
*/
10471047
i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1048-
newer_left = newer_cluster;
10491048
} else
10501049
goto out_ra;
10511050
} else {
@@ -1077,12 +1076,26 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
10771076
i = max(i + 1, next);
10781077
continue;
10791078
}
1079+
1080+
if (!newer_than) {
1081+
cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
1082+
PAGE_CACHE_SHIFT) - i;
1083+
cluster = min(cluster, max_cluster);
1084+
} else {
1085+
cluster = max_cluster;
1086+
}
1087+
10801088
if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
10811089
BTRFS_I(inode)->force_compress = compress_type;
10821090

1083-
btrfs_force_ra(inode->i_mapping, ra, file, i, newer_cluster);
1091+
if (i + cluster > ra_index) {
1092+
ra_index = max(i, ra_index);
1093+
btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
1094+
cluster);
1095+
ra_index += max_cluster;
1096+
}
10841097

1085-
ret = cluster_pages_for_defrag(inode, pages, i, newer_cluster);
1098+
ret = cluster_pages_for_defrag(inode, pages, i, cluster);
10861099
if (ret < 0)
10871100
goto out_ra;
10881101

@@ -1102,15 +1115,17 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
11021115
if (!ret) {
11031116
range->start = newer_off;
11041117
i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
1105-
newer_left = newer_cluster;
11061118
} else {
11071119
break;
11081120
}
11091121
} else {
1110-
if (ret > 0)
1122+
if (ret > 0) {
11111123
i += ret;
1112-
else
1124+
last_len += ret << PAGE_CACHE_SHIFT;
1125+
} else {
11131126
i++;
1127+
last_len = 0;
1128+
}
11141129
}
11151130
}
11161131

0 commit comments

Comments
 (0)