]> git.hungrycats.org Git - linux/commitdiff
Btrfs: fix defrag to merge tail file extent
authorLiu Bo <bo.li.liu@oracle.com>
Fri, 7 Aug 2015 08:48:41 +0000 (16:48 +0800)
committerZygo Blaxell <zblaxell@thirteen.furryterror.org>
Fri, 12 Feb 2016 03:15:42 +0000 (22:15 -0500)
The file layout is

[extent 1]...[extent n][4k extent][HOLE][extent x]

extent 1~n and 4k extent can be merged during defrag, and the whole
defrag bytes is larger than our defrag thresh(256k), 4k extent as a
tail is left unmerged since we check if its next extent can be merged
(the next one is a hole, so the check will fail), the layout thus can
be

[new extent][4k extent][HOLE][extent x]
 (1~n)

To fix it, beside looking at the next one, this also looks at the
previous one by checking @defrag_end, which is set to 0 when we
decide to stop merging contiguous extents, otherwise, we can merge
the previous one with our extent.

Also, this makes btrfs behave consistent with how xfs and ext4 do.

Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
Signed-off-by: Chris Mason <clm@fb.com>
(cherry picked from commit 4a3560c4f3f0f92d3b673944753e3e947e030bc4)

fs/btrfs/ioctl.c

index 3cfe326d6bba9be8801b8dc10524f2eb3f3fb50c..0ac1dd09fc98a9d2e79eb21ca86c7d563aa7e6ce 100644 (file)
@@ -1040,6 +1040,7 @@ static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
        struct extent_map *em;
        int ret = 1;
        bool next_mergeable = true;
+       bool prev_mergeable = true;
 
        /*
         * make sure that once we start defragging an extent, we keep on
@@ -1060,13 +1061,16 @@ static int should_defrag_range(struct inode *inode, u64 start, u32 thresh,
                goto out;
        }
 
+       if (!*defrag_end)
+               prev_mergeable = false;
+
        next_mergeable = defrag_check_next_extent(inode, em);
        /*
         * we hit a real extent, if it is big or the next extent is not a
         * real extent, don't bother defragging it
         */
        if (!compress && (*last_len == 0 || *last_len >= thresh) &&
-           (em->len >= thresh || !next_mergeable))
+           (em->len >= thresh || (!next_mergeable && !prev_mergeable)))
                ret = 0;
 out:
        /*