Skip to content

Commit

Permalink
Btrfs: honor extent thresh during defragmentation
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Li Zefan authored and David Sterba committed Oct 20, 2011
1 parent 83c8c9b commit 008873e
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions fs/btrfs/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ static int should_defrag_range(struct inode *inode, u64 start, u64 len,
int ret = 1;

/*
* make sure that once we start defragging and extent, we keep on
* make sure that once we start defragging an extent, we keep on
* defragging it
*/
if (start < *defrag_end)
Expand Down Expand Up @@ -810,7 +810,6 @@ static int should_defrag_range(struct inode *inode, u64 start, u64 len,
* extent will force at least part of that big extent to be defragged.
*/
if (ret) {
*last_len += len;
*defrag_end = extent_map_end(em);
} else {
*last_len = 0;
Expand Down Expand Up @@ -984,13 +983,14 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
u64 skip = 0;
u64 defrag_end = 0;
u64 newer_off = range->start;
int newer_left = 0;
unsigned long i;
unsigned long ra_index = 0;
int ret;
int defrag_count = 0;
int compress_type = BTRFS_COMPRESS_ZLIB;
int extent_thresh = range->extent_thresh;
int newer_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT;
int cluster = max_cluster;
u64 new_align = ~((u64)128 * 1024 - 1);
struct page **pages = NULL;

Expand Down Expand Up @@ -1020,7 +1020,7 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
ra = &file->f_ra;
}

pages = kmalloc(sizeof(struct page *) * newer_cluster,
pages = kmalloc(sizeof(struct page *) * max_cluster,
GFP_NOFS);
if (!pages) {
ret = -ENOMEM;
Expand All @@ -1045,7 +1045,6 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
* the extents in the file evenly spaced
*/
i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
newer_left = newer_cluster;
} else
goto out_ra;
} else {
Expand Down Expand Up @@ -1077,12 +1076,26 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
i = max(i + 1, next);
continue;
}

if (!newer_than) {
cluster = (PAGE_CACHE_ALIGN(defrag_end) >>
PAGE_CACHE_SHIFT) - i;
cluster = min(cluster, max_cluster);
} else {
cluster = max_cluster;
}

if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
BTRFS_I(inode)->force_compress = compress_type;

btrfs_force_ra(inode->i_mapping, ra, file, i, newer_cluster);
if (i + cluster > ra_index) {
ra_index = max(i, ra_index);
btrfs_force_ra(inode->i_mapping, ra, file, ra_index,
cluster);
ra_index += max_cluster;
}

ret = cluster_pages_for_defrag(inode, pages, i, newer_cluster);
ret = cluster_pages_for_defrag(inode, pages, i, cluster);
if (ret < 0)
goto out_ra;

Expand All @@ -1102,15 +1115,17 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
if (!ret) {
range->start = newer_off;
i = (newer_off & new_align) >> PAGE_CACHE_SHIFT;
newer_left = newer_cluster;
} else {
break;
}
} else {
if (ret > 0)
if (ret > 0) {
i += ret;
else
last_len += ret << PAGE_CACHE_SHIFT;
} else {
i++;
last_len = 0;
}
}
}

Expand Down

0 comments on commit 008873e

Please sign in to comment.