Skip to contentSkip to conversation

btrfs: don't ignore ENOMEM in btrfs_add_chunk_map()

PATCH In review Boris Burkov 2 participants 3 messages Sep 24, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: don't ignore ENOMEM in btrfs_add_chunk_map()

btrfs_set_extent_bit() can return ENOMEM, which chunk_map_device_(set|clear)_bits() was ignoring, so btrfs_add_chunk_map() was silently failing in those cases. This led to double allocating dev extents, ultimately resulting in an EEXIST in add_dev_extent_item(): (ignore the btrfs_force_chunk_alloc_store thing, we are using a DEBUG feature that isn't widely used here)

  ------------[ cut here ]------------
  BTRFS: Transaction aborted (error -17)
  WARNING: CPU: 5 PID: 3586339 at fs/btrfs/block-group.c:2764 btrfs_create_pending_block_groups+0x5fc/0x620
  RIP: 0010:btrfs_create_pending_block_groups+0x5fc/0x620
  Code: 9c fd ff ff 48 c7 c7 e8 69 64 82 44 89 ee e8 4b 87 e6 ff 0f 0b e9 99 fe ff ff 48 c7 c7 e8 69 64 82 48 8b 34 24 e8 34 87 e6 ff <0f> 0b eb a0 48 c7 c7 e8 69 64 82 44 89 e6 e8 21 87 e6 ff 0f 0b e9
  RSP: 0018:ffffc90060693d38 EFLAGS: 00010286
  RAX: 0000000000000026 RBX: ffff8882be92fed8 RCX: 0000000000000027
  RDX: ffff88bf42f6e040 RSI: ffff88bf42f60c48 RDI: ffff88bf42f60c48
  RBP: ffff888282585d18 R08: 0000000000000000 R09: 0000000000000000
  R10: 4000000000000000 R11: 000000000000691d R12: ffff88c0471a3300
  R13: 0000001dc6500000 R14: ffff8882829d9000 R15: ffff8882be92ff40
  FS:  00007f7ccbc2c740(0000) GS:ffff88bf42f40000(0000) knlGS:0000000000000000
  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  CR2: 0000000004e421c8 CR3: 0000000cd6384001 CR4: 0000000000770ef0
  PKRU: 55555554
  Call Trace:
   <TASK>
   ? __warn+0xa0/0x130
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   ? report_bug+0xf2/0x150
   ? handle_bug+0x3d/0x70
   ? exc_invalid_op+0x16/0x40
   ? asm_exc_invalid_op+0x16/0x20
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   __btrfs_end_transaction.llvm.3999538623537568469+0x3d/0x1c0
   btrfs_force_chunk_alloc_store+0xaf/0x100
   ? sysfs_kf_read+0x90/0x90
   kernfs_fop_write_iter.llvm.10031329899921036925+0xd0/0x180
   __x64_sys_write+0x279/0x5a0
   do_syscall_64+0x63/0x130
   ? exc_page_fault+0x63/0x130
   entry_SYSCALL_64_after_hwframe+0x4b/0x53
  RIP: 0033:0x7f7ccb4ff28f
  Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 a9 89 f8 ff 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 31 44 89 c7 48 89 44 24 08 e8 fc 89 f8 ff 48
  RSP: 002b:00007ffda7138f20 EFLAGS: 00000293 ORIG_RAX: 0000000000000001
  RAX: ffffffffffffffda RBX: 0000000009c13140 RCX: 00007f7ccb4ff28f
  RDX: 0000000000000001 RSI: 0000000004e401b0 RDI: 00000000000000c8
  RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000000
  R10: 0000000000008000 R11: 0000000000000293 R12: 0000000000000000
  R13: 0000000000408820 R14: 0000000000407f00 R15: 00007ffda7138fc0
   </TASK>
  ---[ end trace 0000000000000000 ]---
  BTRFS: error (device nvme0n1p2 state A) in btrfs_create_pending_block_groups:2764: errno=-17 Object already exists
  BTRFS warning (device nvme0n1p2 state EA): Skipping commit of aborted transaction.
  BTRFS: error (device nvme0n1p2 state EA) in cleanup_transaction:2018: errno=-17 Object already exists

This was pretty confusing to debug and I think it would be helpful to get the proper ENOMEM error sent up to the appropriate transaction when it happens.

Note:
Most callsites of btrfs_set_extent_bit() are not checked, however, which does give me pause. Either we have a lot more inaccuracies like this out there, or I have misanalyzed this scenario. I was able to reproduce the above calltrace by injecting enomem errors in to the set_extent_bits path here, for what it's worth.

---
 fs/btrfs/volumes.c | 47 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 12 deletions(-)
fs/btrfs/volumes.c +35 -12
index 2bec544d8ba3..eda5b6b907d9 100644
@@ -5434,28 +5434,42 @@ static int decide_stripe_size(struct btrfs_fs_devices *fs_devices,
 	}
 }
 
-static void chunk_map_device_set_bits(struct btrfs_chunk_map *map, unsigned int bits)
+static int chunk_map_device_set_bits(struct btrfs_chunk_map *map, unsigned int bits)
 {
+	int ret;
+
 	for (int i = 0; i < map->num_stripes; i++) {
 		struct btrfs_io_stripe *stripe = &map->stripes[i];
 		struct btrfs_device *device = stripe->dev;
 
-		btrfs_set_extent_bit(&device->alloc_state, stripe->physical,
-				     stripe->physical + map->stripe_size - 1,
-				     bits | EXTENT_NOWAIT, NULL);
+		ret = btrfs_set_extent_bit(
+			&device->alloc_state, stripe->physical,
+			stripe->physical + map->stripe_size - 1,
+			bits | EXTENT_NOWAIT, NULL);
+		if (ret)
+			return ret;
 	}
+	ret = 0;
+	return ret;
 }
 
-static void chunk_map_device_clear_bits(struct btrfs_chunk_map *map, unsigned int bits)
+static int chunk_map_device_clear_bits(struct btrfs_chunk_map *map, unsigned int bits)
 {
+	int ret;
+
 	for (int i = 0; i < map->num_stripes; i++) {
 		struct btrfs_io_stripe *stripe = &map->stripes[i];
 		struct btrfs_device *device = stripe->dev;
 
-		btrfs_clear_extent_bit(&device->alloc_state, stripe->physical,
-				       stripe->physical + map->stripe_size - 1,
-				       bits | EXTENT_NOWAIT, NULL);
+		ret = btrfs_clear_extent_bit(
+			&device->alloc_state, stripe->physical,
+			stripe->physical + map->stripe_size - 1,
+			bits | EXTENT_NOWAIT, NULL);
+		if (ret)
+			return ret;
 	}
+	ret = 0;
+	return ret;
 }
 
 void btrfs_remove_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map)
@@ -5488,6 +5502,7 @@ static int btrfs_chunk_map_cmp(const struct rb_node *new,
 EXPORT_FOR_TESTS
 int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map)
 {
+	int ret;
 	struct rb_node *exist;
 
 	write_lock(&fs_info->mapping_tree_lock);
@@ -5498,11 +5513,19 @@ int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *m
 		write_unlock(&fs_info->mapping_tree_lock);
 		return -EEXIST;
 	}
-	chunk_map_device_set_bits(map, CHUNK_ALLOCATED);
-	chunk_map_device_clear_bits(map, CHUNK_TRIMMED);
-	write_unlock(&fs_info->mapping_tree_lock);
 
-	return 0;
+	ret = chunk_map_device_set_bits(map, CHUNK_ALLOCATED);
+	if (ret)
+		goto out;
+	ret = chunk_map_device_clear_bits(map, CHUNK_TRIMMED);
+
+out:
+	if (ret) {
+		rb_erase_cached(&map->rb_node, &fs_info->mapping_tree);
+		RB_CLEAR_NODE(&map->rb_node);
+	}
+	write_unlock(&fs_info->mapping_tree_lock);
+	return ret;
 }
 
 EXPORT_FOR_TESTS
Signature · 1 line
2.50.1
#1 lore raw
Quoted 59 lines · Boris Burkov

btrfs_set_extent_bit() can return ENOMEM, which chunk_map_device_(set|clear)_bits() was ignoring, so btrfs_add_chunk_map() was silently failing in those cases. This led to double allocating dev extents, ultimately resulting in an EEXIST in add_dev_extent_item(): (ignore the btrfs_force_chunk_alloc_store thing, we are using a DEBUG feature that isn't widely used here)

  ------------[ cut here ]------------
  BTRFS: Transaction aborted (error -17)
  WARNING: CPU: 5 PID: 3586339 at fs/btrfs/block-group.c:2764 btrfs_create_pending_block_groups+0x5fc/0x620
  RIP: 0010:btrfs_create_pending_block_groups+0x5fc/0x620
  Code: 9c fd ff ff 48 c7 c7 e8 69 64 82 44 89 ee e8 4b 87 e6 ff 0f 0b e9 99 fe ff ff 48 c7 c7 e8 69 64 82 48 8b 34 24 e8 34 87 e6 ff <0f> 0b eb a0 48 c7 c7 e8 69 64 82 44 89 e6 e8 21 87 e6 ff 0f 0b e9
  RSP: 0018:ffffc90060693d38 EFLAGS: 00010286
  RAX: 0000000000000026 RBX: ffff8882be92fed8 RCX: 0000000000000027
  RDX: ffff88bf42f6e040 RSI: ffff88bf42f60c48 RDI: ffff88bf42f60c48
  RBP: ffff888282585d18 R08: 0000000000000000 R09: 0000000000000000
  R10: 4000000000000000 R11: 000000000000691d R12: ffff88c0471a3300
  R13: 0000001dc6500000 R14: ffff8882829d9000 R15: ffff8882be92ff40
  FS:  00007f7ccbc2c740(0000) GS:ffff88bf42f40000(0000) knlGS:0000000000000000
  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  CR2: 0000000004e421c8 CR3: 0000000cd6384001 CR4: 0000000000770ef0
  PKRU: 55555554
  Call Trace:
   <TASK>
   ? __warn+0xa0/0x130
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   ? report_bug+0xf2/0x150
   ? handle_bug+0x3d/0x70
   ? exc_invalid_op+0x16/0x40
   ? asm_exc_invalid_op+0x16/0x20
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   __btrfs_end_transaction.llvm.3999538623537568469+0x3d/0x1c0
   btrfs_force_chunk_alloc_store+0xaf/0x100
   ? sysfs_kf_read+0x90/0x90
   kernfs_fop_write_iter.llvm.10031329899921036925+0xd0/0x180
   __x64_sys_write+0x279/0x5a0
   do_syscall_64+0x63/0x130
   ? exc_page_fault+0x63/0x130
   entry_SYSCALL_64_after_hwframe+0x4b/0x53
  RIP: 0033:0x7f7ccb4ff28f
  Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 a9 89 f8 ff 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 31 44 89 c7 48 89 44 24 08 e8 fc 89 f8 ff 48
  RSP: 002b:00007ffda7138f20 EFLAGS: 00000293 ORIG_RAX: 0000000000000001
  RAX: ffffffffffffffda RBX: 0000000009c13140 RCX: 00007f7ccb4ff28f
  RDX: 0000000000000001 RSI: 0000000004e401b0 RDI: 00000000000000c8
  RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000000
  R10: 0000000000008000 R11: 0000000000000293 R12: 0000000000000000
  R13: 0000000000408820 R14: 0000000000407f00 R15: 00007ffda7138fc0
   </TASK>
  ---[ end trace 0000000000000000 ]---
  BTRFS: error (device nvme0n1p2 state A) in btrfs_create_pending_block_groups:2764: errno=-17 Object already exists
  BTRFS warning (device nvme0n1p2 state EA): Skipping commit of aborted transaction.
  BTRFS: error (device nvme0n1p2 state EA) in cleanup_transaction:2018: errno=-17 Object already exists

This was pretty confusing to debug and I think it would be helpful to get the proper ENOMEM error sent up to the appropriate transaction when it happens.

Quoted 6 lines

Note:
Most callsites of btrfs_set_extent_bit() are not checked, however, which does give me pause. Either we have a lot more inaccuracies like this out there, or I have misanalyzed this scenario. I was able to reproduce the above calltrace by injecting enomem errors in to the set_extent_bits path here, for what it's worth.

Most callers are not checking -ENOMEM (or other errors) because we shouldn't have -ENOMEM with any of the functions that end up at set_extent_bit().
There, under the 'again' label, before taking the io tree's lock we do:

prealloc = alloc_extent_state(mask);

If that returns NULL, we proceed, take the lock and navigate the rb tree. If it turns out we need an extent state record (and we don't always need one), we call alloc_extent_state_atomic().
If that fails (returns NULL) we do a goto 'search_again', which unlocks the tree and does a goto to the 'again' label at the very top, where we once again call alloc_extent_state() and repeat all those steps again.

So if we can't allocate memory, we keep doing that loop over and over until we can allocate or don't need to allocate (due to concurrent changes in the tree) - we never return -ENOMEM in set_extent_bit().

The same logic applies to btrfs_clear_extent_bit_changeset(), the core function to clear bits from a range - we don't ever return -ENOMEM and keep looping until we can allocate.

The only io tree function we return -ENOMEM is for the convert operation (btrfs_convert_extent_bit()), where the single caller can deal with -ENOMEM (I remember doing that logic many years ago).

So I don't know what that error injection you mention is doing or that btrfs_force_chunk_alloc_store thing you mention with some custom debugging feature, but I don't see how we can return -ENOMEM. Care to elaborate in detail?

Thanks.

Quoted 97 lines
---
 fs/btrfs/volumes.c | 47 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 12 deletions(-)
fs/btrfs/volumes.c +35 -13
index 2bec544d8ba3..eda5b6b907d9 100644
@@ -5434,28 +5434,42 @@ static int decide_stripe_size(struct btrfs_fs_devices *fs_devices,
        }
 }
 
-static void chunk_map_device_set_bits(struct btrfs_chunk_map *map, unsigned int bits)
+static int chunk_map_device_set_bits(struct btrfs_chunk_map *map, unsigned int bits)
 {
+       int ret;
+
        for (int i = 0; i < map->num_stripes; i++) {
                struct btrfs_io_stripe *stripe = &map->stripes[i];
                struct btrfs_device *device = stripe->dev;
 
-               btrfs_set_extent_bit(&device->alloc_state, stripe->physical,
-                                    stripe->physical + map->stripe_size - 1,
-                                    bits | EXTENT_NOWAIT, NULL);
+               ret = btrfs_set_extent_bit(
+                       &device->alloc_state, stripe->physical,
+                       stripe->physical + map->stripe_size - 1,
+                       bits | EXTENT_NOWAIT, NULL);
+               if (ret)
+                       return ret;
        }
+       ret = 0;
+       return ret;
 }
 
-static void chunk_map_device_clear_bits(struct btrfs_chunk_map *map, unsigned int bits)
+static int chunk_map_device_clear_bits(struct btrfs_chunk_map *map, unsigned int bits)
 {
+       int ret;
+
        for (int i = 0; i < map->num_stripes; i++) {
                struct btrfs_io_stripe *stripe = &map->stripes[i];
                struct btrfs_device *device = stripe->dev;
 
-               btrfs_clear_extent_bit(&device->alloc_state, stripe->physical,
-                                      stripe->physical + map->stripe_size - 1,
-                                      bits | EXTENT_NOWAIT, NULL);
+               ret = btrfs_clear_extent_bit(
+                       &device->alloc_state, stripe->physical,
+                       stripe->physical + map->stripe_size - 1,
+                       bits | EXTENT_NOWAIT, NULL);
+               if (ret)
+                       return ret;
        }
+       ret = 0;
+       return ret;
 }
 
 void btrfs_remove_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map)
@@ -5488,6 +5502,7 @@ static int btrfs_chunk_map_cmp(const struct rb_node *new,
 EXPORT_FOR_TESTS
 int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map)
 {
+       int ret;
        struct rb_node *exist;
 
        write_lock(&fs_info->mapping_tree_lock);
@@ -5498,11 +5513,19 @@ int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *m
                write_unlock(&fs_info->mapping_tree_lock);
                return -EEXIST;
        }
-       chunk_map_device_set_bits(map, CHUNK_ALLOCATED);
-       chunk_map_device_clear_bits(map, CHUNK_TRIMMED);
-       write_unlock(&fs_info->mapping_tree_lock);
 
-       return 0;
+       ret = chunk_map_device_set_bits(map, CHUNK_ALLOCATED);
+       if (ret)
+               goto out;
+       ret = chunk_map_device_clear_bits(map, CHUNK_TRIMMED);
+
+out:
+       if (ret) {
+               rb_erase_cached(&map->rb_node, &fs_info->mapping_tree);
+               RB_CLEAR_NODE(&map->rb_node);
+       }
+       write_unlock(&fs_info->mapping_tree_lock);
+       return ret;
 }
 
 EXPORT_FOR_TESTS
--

2.50.1

#2 lore raw
Quoted 94 lines · Filipe Manana
Quoted 59 lines · Boris Burkov

btrfs_set_extent_bit() can return ENOMEM, which chunk_map_device_(set|clear)_bits() was ignoring, so btrfs_add_chunk_map() was silently failing in those cases. This led to double allocating dev extents, ultimately resulting in an EEXIST in add_dev_extent_item(): (ignore the btrfs_force_chunk_alloc_store thing, we are using a DEBUG feature that isn't widely used here)

  ------------[ cut here ]------------
  BTRFS: Transaction aborted (error -17)
  WARNING: CPU: 5 PID: 3586339 at fs/btrfs/block-group.c:2764 btrfs_create_pending_block_groups+0x5fc/0x620
  RIP: 0010:btrfs_create_pending_block_groups+0x5fc/0x620
  Code: 9c fd ff ff 48 c7 c7 e8 69 64 82 44 89 ee e8 4b 87 e6 ff 0f 0b e9 99 fe ff ff 48 c7 c7 e8 69 64 82 48 8b 34 24 e8 34 87 e6 ff <0f> 0b eb a0 48 c7 c7 e8 69 64 82 44 89 e6 e8 21 87 e6 ff 0f 0b e9
  RSP: 0018:ffffc90060693d38 EFLAGS: 00010286
  RAX: 0000000000000026 RBX: ffff8882be92fed8 RCX: 0000000000000027
  RDX: ffff88bf42f6e040 RSI: ffff88bf42f60c48 RDI: ffff88bf42f60c48
  RBP: ffff888282585d18 R08: 0000000000000000 R09: 0000000000000000
  R10: 4000000000000000 R11: 000000000000691d R12: ffff88c0471a3300
  R13: 0000001dc6500000 R14: ffff8882829d9000 R15: ffff8882be92ff40
  FS:  00007f7ccbc2c740(0000) GS:ffff88bf42f40000(0000) knlGS:0000000000000000
  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
  CR2: 0000000004e421c8 CR3: 0000000cd6384001 CR4: 0000000000770ef0
  PKRU: 55555554
  Call Trace:
   <TASK>
   ? __warn+0xa0/0x130
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   ? report_bug+0xf2/0x150
   ? handle_bug+0x3d/0x70
   ? exc_invalid_op+0x16/0x40
   ? asm_exc_invalid_op+0x16/0x20
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   ? btrfs_create_pending_block_groups+0x5fc/0x620
   __btrfs_end_transaction.llvm.3999538623537568469+0x3d/0x1c0
   btrfs_force_chunk_alloc_store+0xaf/0x100
   ? sysfs_kf_read+0x90/0x90
   kernfs_fop_write_iter.llvm.10031329899921036925+0xd0/0x180
   __x64_sys_write+0x279/0x5a0
   do_syscall_64+0x63/0x130
   ? exc_page_fault+0x63/0x130
   entry_SYSCALL_64_after_hwframe+0x4b/0x53
  RIP: 0033:0x7f7ccb4ff28f
  Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 a9 89 f8 ff 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 31 44 89 c7 48 89 44 24 08 e8 fc 89 f8 ff 48
  RSP: 002b:00007ffda7138f20 EFLAGS: 00000293 ORIG_RAX: 0000000000000001
  RAX: ffffffffffffffda RBX: 0000000009c13140 RCX: 00007f7ccb4ff28f
  RDX: 0000000000000001 RSI: 0000000004e401b0 RDI: 00000000000000c8
  RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000000
  R10: 0000000000008000 R11: 0000000000000293 R12: 0000000000000000
  R13: 0000000000408820 R14: 0000000000407f00 R15: 00007ffda7138fc0
   </TASK>
  ---[ end trace 0000000000000000 ]---
  BTRFS: error (device nvme0n1p2 state A) in btrfs_create_pending_block_groups:2764: errno=-17 Object already exists
  BTRFS warning (device nvme0n1p2 state EA): Skipping commit of aborted transaction.
  BTRFS: error (device nvme0n1p2 state EA) in cleanup_transaction:2018: errno=-17 Object already exists

This was pretty confusing to debug and I think it would be helpful to get the proper ENOMEM error sent up to the appropriate transaction when it happens.

Quoted 6 lines

Note:
Most callsites of btrfs_set_extent_bit() are not checked, however, which does give me pause. Either we have a lot more inaccuracies like this out there, or I have misanalyzed this scenario. I was able to reproduce the above calltrace by injecting enomem errors in to the set_extent_bits path here, for what it's worth.

Most callers are not checking -ENOMEM (or other errors) because we shouldn't have -ENOMEM with any of the functions that end up at set_extent_bit().
There, under the 'again' label, before taking the io tree's lock we do:

prealloc = alloc_extent_state(mask);

If that returns NULL, we proceed, take the lock and navigate the rb tree. If it turns out we need an extent state record (and we don't always need one), we call alloc_extent_state_atomic().
If that fails (returns NULL) we do a goto 'search_again', which unlocks the tree and does a goto to the 'again' label at the very top, where we once again call alloc_extent_state() and repeat all those steps again.

So if we can't allocate memory, we keep doing that loop over and over until we can allocate or don't need to allocate (due to concurrent changes in the tree) - we never return -ENOMEM in set_extent_bit().

The same logic applies to btrfs_clear_extent_bit_changeset(), the core function to clear bits from a range - we don't ever return -ENOMEM and keep looping until we can allocate.

The only io tree function we return -ENOMEM is for the convert operation (btrfs_convert_extent_bit()), where the single caller can deal with -ENOMEM (I remember doing that logic many years ago).

Quoted 9 lines

So I don't know what that error injection you mention is doing or that btrfs_force_chunk_alloc_store thing you mention with some custom debugging feature, but I don't see how we can return -ENOMEM. Care to elaborate in detail?

Thanks.

Thanks for the reply and explanation.

I did indeed miss the looping in set_extent_bit() and somehow got it confused with convert_extent_bit(), because I do remember reading the first_iteration check. So first things first, I agree with your analysis on the return value.

As for the error injection and debug feature, here is the full story of what I was up to:

For a few years now, in prod, we have been using the sysfs force_chunk_alloc file added here
https://lore.kernel.org/linux-btrfs/[email protected]/
to force metadata allocations on healthy filesystems to create a buffer to protect from ENOSPC. This predates our adoption of auto relocation and then dynamic relocation, so it is possible we don't need it anymore. But we are writing to it for metadata block groups when they have <5G allocated. (And we carry an out of tree patch to include that sysfs file without CONFIG_BTRFS_DEBUG)

Now, what I have observed on prod hosts is the stack trace that I included in the commit message, so I was investigating that. I looked pretty closely at one such host in drgn and observed that we had a pattern in the dev extents that looked like:

Logical L: Single data chunk at physical P
Logical L+1: Dup metadata chunk at physical P-1, dup mapped to P

And writing the DEV_EXTENT item for the second stripe was causing the EEXIST.

During that investigation, due to mis-remembering ENOMEMs from extent state bits in my quotas work (that code also uses changesets...) and mixing up convert_extent_bit() and set_extent_bit(), I hypothesized that ignoring an ENOMEM might cause that error. So I made a workload that hit the force chunk alloc file, added manual random ENOMEM injection into chunk_map_device_set_bits() (which I now agree is pointless and can't happen), and did in fact reproduce the same pattern of error. From there I jumped to the conclusion that this is the problem and sent this patch (and tested my new code with the stupid non-real error injection). It didn't help that the host I was investigating did have a good amount of OOMs in the dmesg to further lead me astray :)

So at this point, I need to go back to the drawing board on what is causing the overlapping chunk allocations, it does not appear to be OOM.

On the bright side, this is great news, because I was NOT looking forward to trying to fix all the callsites, especially if unwinding the successfully set bits before an error was needed...

Thanks,
Boris

Quoted 97 lines
Quoted 97 lines
---
 fs/btrfs/volumes.c | 47 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 12 deletions(-)
fs/btrfs/volumes.c +35 -13
index 2bec544d8ba3..eda5b6b907d9 100644
@@ -5434,28 +5434,42 @@ static int decide_stripe_size(struct btrfs_fs_devices *fs_devices,
        }
 }
 
-static void chunk_map_device_set_bits(struct btrfs_chunk_map *map, unsigned int bits)
+static int chunk_map_device_set_bits(struct btrfs_chunk_map *map, unsigned int bits)
 {
+       int ret;
+
        for (int i = 0; i < map->num_stripes; i++) {
                struct btrfs_io_stripe *stripe = &map->stripes[i];
                struct btrfs_device *device = stripe->dev;
 
-               btrfs_set_extent_bit(&device->alloc_state, stripe->physical,
-                                    stripe->physical + map->stripe_size - 1,
-                                    bits | EXTENT_NOWAIT, NULL);
+               ret = btrfs_set_extent_bit(
+                       &device->alloc_state, stripe->physical,
+                       stripe->physical + map->stripe_size - 1,
+                       bits | EXTENT_NOWAIT, NULL);
+               if (ret)
+                       return ret;
        }
+       ret = 0;
+       return ret;
 }
 
-static void chunk_map_device_clear_bits(struct btrfs_chunk_map *map, unsigned int bits)
+static int chunk_map_device_clear_bits(struct btrfs_chunk_map *map, unsigned int bits)
 {
+       int ret;
+
        for (int i = 0; i < map->num_stripes; i++) {
                struct btrfs_io_stripe *stripe = &map->stripes[i];
                struct btrfs_device *device = stripe->dev;
 
-               btrfs_clear_extent_bit(&device->alloc_state, stripe->physical,
-                                      stripe->physical + map->stripe_size - 1,
-                                      bits | EXTENT_NOWAIT, NULL);
+               ret = btrfs_clear_extent_bit(
+                       &device->alloc_state, stripe->physical,
+                       stripe->physical + map->stripe_size - 1,
+                       bits | EXTENT_NOWAIT, NULL);
+               if (ret)
+                       return ret;
        }
+       ret = 0;
+       return ret;
 }
 
 void btrfs_remove_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map)
@@ -5488,6 +5502,7 @@ static int btrfs_chunk_map_cmp(const struct rb_node *new,
 EXPORT_FOR_TESTS
 int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *map)
 {
+       int ret;
        struct rb_node *exist;
 
        write_lock(&fs_info->mapping_tree_lock);
@@ -5498,11 +5513,19 @@ int btrfs_add_chunk_map(struct btrfs_fs_info *fs_info, struct btrfs_chunk_map *m
                write_unlock(&fs_info->mapping_tree_lock);
                return -EEXIST;
        }
-       chunk_map_device_set_bits(map, CHUNK_ALLOCATED);
-       chunk_map_device_clear_bits(map, CHUNK_TRIMMED);
-       write_unlock(&fs_info->mapping_tree_lock);
 
-       return 0;
+       ret = chunk_map_device_set_bits(map, CHUNK_ALLOCATED);
+       if (ret)
+               goto out;
+       ret = chunk_map_device_clear_bits(map, CHUNK_TRIMMED);
+
+out:
+       if (ret) {
+               rb_erase_cached(&map->rb_node, &fs_info->mapping_tree);
+               RB_CLEAR_NODE(&map->rb_node);
+       }
+       write_unlock(&fs_info->mapping_tree_lock);
+       return ret;
 }
 
 EXPORT_FOR_TESTS
--

2.50.1