]> git.hungrycats.org Git - linux/commitdiff
Btrfs: fix prealloc under heavy fragmentation conditions
authorJosef Bacik <jbacik@fb.com>
Wed, 23 Sep 2015 21:11:16 +0000 (17:11 -0400)
committerZygo Blaxell <zblaxell@serenity.furryterror.org>
Wed, 28 Oct 2015 01:11:43 +0000 (21:11 -0400)
If we are heavily fragmented we will continually try to prealloc the largest
extent size we can every time we call btrfs_reserve_extent.  This can be very
expensive when we are heavily fragmented, burning lots of CPU cycles and loops
through the allocator.  So instead notice when we get a smaller chunk from the
allocator than what we specified and use this as the new maximum size we try to
allocate.  Thanks,

Signed-off-by: Josef Bacik <jbacik@fb.com>
(cherry picked from commit b060d847f40c270a4f7a2da8b7ef9a04c697e30d)
(cherry picked from commit a9f8eb6d063e1647a7ece72790cf8f642e1ff643)

fs/btrfs/inode.c

index 195b9bb70e8a387630431c8d17469ad9927b2719..5414b13e57376c20e5193b3843fd9cb01d610ad9 100644 (file)
@@ -9655,6 +9655,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
        u64 cur_offset = start;
        u64 i_size;
        u64 cur_bytes;
+       u64 last_alloc = (u64)-1;
        int ret = 0;
        bool own_trans = true;
 
@@ -9671,6 +9672,13 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
 
                cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
                cur_bytes = max(cur_bytes, min_size);
+               /*
+                * If we are severely fragmented we could end up with really
+                * small allocations, so if the allocator is returning small
+                * chunks lets make its job easier by only searching for those
+                * sized chunks.
+                */
+               cur_bytes = min(cur_bytes, last_alloc);
                ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
                                           *alloc_hint, &ins, 1, 0);
                if (ret) {
@@ -9679,6 +9687,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
                        break;
                }
 
+               last_alloc = ins.offset;
                ret = insert_reserved_file_extent(trans, inode,
                                                  cur_offset, ins.objectid,
                                                  ins.offset, ins.offset,