]> git.hungrycats.org Git - linux/commitdiff
btrfs: prefer aligned dev_extent holes when allocating chunks misc-next/topics/dev-extent-defrag
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 14 Feb 2024 05:10:23 +0000 (00:10 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Fri, 6 Nov 2026 07:29:42 +0000 (02:29 -0500)
With the previous patch, the data, metadata and system chunk sizes can
all be set to 1 GiB, so that every dev_extent is 1 GiB long.  The holes
between them are only 1 GiB long as well if the dev_extents also start
on a common grid.  Every device reserves its first 1 MiB, so that grid
is N GiB + 1 MiB.  A dev_extent placed off the grid splits the holes on
either side into pieces shorter than 1 GiB, and the fragmentation
described in the previous patch can start from there.

find_free_dev_extent() takes the first hole that is large enough,
wherever it starts.  Make it prefer a hole on the grid: walk the holes in
device order as before, and take the first one that is large enough and
starts at N GiB + 1 MiB.  If there is none, take the first hole that is
large enough, which is what the function returned before.  If no hole is
large enough, the largest hole is still returned, with -ENOSPC.  The
hole after the last dev_extent is checked the same way.

Unless a hole on the grid is found, the search now visits every
dev_extent of the device instead of stopping at the first hole that is
large enough.

On a device where every dev_extent is 1 GiB long and on the grid, every
hole starts on the grid too, and the result is the same as before.  On a
device that has off-grid dev_extents, new chunks fill the aligned holes
first, so balancing the off-grid ones moves them onto the grid.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/volumes.c

index 3d4ef6725de0e7e4e3894eae51d74d73ab3f82bf..8ac535d43871316f1edde8d96ae332dea974b3d5 100644 (file)
@@ -1907,21 +1907,25 @@ static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
                        dev_extent_hole_check(device, &search_start, &hole_size,
                                              num_bytes);
 
-                       if (hole_size > max_hole_size) {
+                       /*
+                        * Once we find a hole of sufficient size we
+                        * ignore any larger holes that may appear at
+                        * higher device offsets.  If we later find a
+                        * hole that is sufficiently large, but aligned,
+                        * we'll use that instead.
+                        */
+                       if (hole_size > max_hole_size && max_hole_size < num_bytes) {
                                max_hole_start = search_start;
                                max_hole_size = hole_size;
                        }
 
                        /*
-                        * If this free space is greater than which we need,
-                        * it must be the max free space that we have found
-                        * until now, so max_hole_start must point to the start
-                        * of this free space and the length of this free space
-                        * is stored in max_hole_size. Thus, we return
-                        * max_hole_start and max_hole_size and go back to the
-                        * caller.
+                        * If we find a hole that is aligned and as large
+                        * as we need, then exit early with that hole.
                         */
-                       if (hole_size >= num_bytes) {
+                       if (hole_size >= num_bytes && (search_start & (SZ_1G - 1)) == SZ_1M) {
+                               max_hole_start = search_start;
+                               max_hole_size = hole_size;
                                ret = 0;
                                goto out;
                        }
@@ -1946,9 +1950,24 @@ next:
                hole_size = search_end - search_start;
                dev_extent_hole_check(device, &search_start, &hole_size, num_bytes);
 
-               if (hole_size > max_hole_size) {
+               /*
+                * If this is a larger hole it might not be aligned.
+                * Keep the hole we found above if it was large enough.
+                */
+               if (hole_size > max_hole_size && max_hole_size < num_bytes) {
+                       max_hole_start = search_start;
+                       max_hole_size = hole_size;
+               }
+
+               /*
+                * The last hole may be aligned and longer than any other,
+                * so do the aligned check and exit early again.
+                */
+               if (hole_size >= num_bytes && (search_start & (SZ_1G - 1)) == SZ_1M) {
                        max_hole_start = search_start;
                        max_hole_size = hole_size;
+                       ret = 0;
+                       goto out;
                }
        }