btrfs: pass NOWAIT for set/clear extent bits as another bit
authorDavid Sterba <dsterba@suse.com>
Wed, 24 May 2023 23:04:37 +0000 (01:04 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 19 Jun 2023 11:59:30 +0000 (13:59 +0200)
The only flags we now pass to set_extent_bit/__clear_extent_bit are
GFP_NOFS and GFP_NOWAIT (a few functions handling mappings). This
requires an extra parameter to be passed everywhere but is almost always
the same.

Encode the GFP_NOWAIT as an artificial extent bit and extract the
real bits and gfp mask in the lowest level helpers. Now the passed
gfp mask is not actually used and can be removed.

Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/extent-io-tree.c
fs/btrfs/extent-io-tree.h
fs/btrfs/extent_map.c
fs/btrfs/zoned.c

index 29a225836e286e10768dbeaf46305d5d2b88dbee..83e40c02f62e56777ceba3932e7b263a9985b39b 100644 (file)
@@ -532,6 +532,16 @@ static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
        return next;
 }
 
+/*
+ * Detect if extent bits request NOWAIT semantics and set the gfp mask accordingly,
+ * unset the EXTENT_NOWAIT bit.
+ */
+static void set_gfp_mask_from_bits(u32 *bits, gfp_t *mask)
+{
+       *mask = (*bits & EXTENT_NOWAIT ? GFP_NOWAIT : GFP_NOFS);
+       *bits &= EXTENT_NOWAIT - 1;
+}
+
 /*
  * Clear some bits on a range in the tree.  This may require splitting or
  * inserting elements in the tree, so the gfp mask is used to indicate which
@@ -557,6 +567,7 @@ int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
        int wake;
        int delete = (bits & EXTENT_CLEAR_ALL_BITS);
 
+       set_gfp_mask_from_bits(&bits, &mask);
        btrfs_debug_check_extent_io_range(tree, start, end);
        trace_btrfs_clear_extent_bit(tree, start, end - start + 1, bits);
 
@@ -979,6 +990,7 @@ static int __set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
        u64 last_end;
        u32 exclusive_bits = (bits & EXTENT_LOCKED);
 
+       set_gfp_mask_from_bits(&bits, &mask);
        btrfs_debug_check_extent_io_range(tree, start, end);
        trace_btrfs_set_extent_bit(tree, start, end - start + 1, bits);
 
index 5a53a4558366901a3b2c9659a651d698bd71113a..d7f5afeb5ce7a5c87b226c195b05d424cb0ea37e 100644 (file)
@@ -43,6 +43,15 @@ enum {
         * want the extent states to go away.
         */
        ENUM_BIT(EXTENT_CLEAR_ALL_BITS),
+
+       /*
+        * This must be last.
+        *
+        * Bit not representing a state but a request for NOWAIT semantics,
+        * e.g. when allocating memory, and must be masked out from the other
+        * bits.
+        */
+       ENUM_BIT(EXTENT_NOWAIT)
 };
 
 #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
index 918ce12ea412b3ea0a7794febf4bfb971b900729..4c8c87524d62af5ea99c49a2fe650fbd6dc2f566 100644 (file)
@@ -365,8 +365,8 @@ static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
                struct btrfs_device *device = stripe->dev;
 
                set_extent_bit(&device->alloc_state, stripe->physical,
-                              stripe->physical + stripe_size - 1, bits, NULL,
-                              GFP_NOWAIT);
+                              stripe->physical + stripe_size - 1,
+                              bits | EXTENT_NOWAIT, NULL, GFP_NOWAIT);
        }
 }
 
@@ -381,7 +381,8 @@ static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
                struct btrfs_device *device = stripe->dev;
 
                __clear_extent_bit(&device->alloc_state, stripe->physical,
-                                  stripe->physical + stripe_size - 1, bits,
+                                  stripe->physical + stripe_size - 1,
+                                  bits | EXTENT_NOWAIT,
                                   NULL, GFP_NOWAIT, NULL);
        }
 }
index b82a350c4c594806d4b598e4fae04a3f014b7bc1..fb90e2b20614c7b9a63a2ccce8df30ae5a433501 100644 (file)
@@ -1612,7 +1612,7 @@ void btrfs_redirty_list_add(struct btrfs_transaction *trans,
        set_bit(EXTENT_BUFFER_NO_CHECK, &eb->bflags);
        set_extent_buffer_dirty(eb);
        set_extent_bit(&trans->dirty_pages, eb->start, eb->start + eb->len - 1,
-                       EXTENT_DIRTY, NULL, GFP_NOWAIT);
+                       EXTENT_DIRTY | EXTENT_NOWAIT, NULL, GFP_NOWAIT);
 }
 
 bool btrfs_use_zone_append(struct btrfs_bio *bbio)