btrfs: redirect I/O for remapped block groups
authorMark Harmstone <mark@harmstone.com>
Wed, 7 Jan 2026 14:09:09 +0000 (14:09 +0000)
committerDavid Sterba <dsterba@suse.com>
Tue, 3 Feb 2026 06:54:35 +0000 (07:54 +0100)
Change btrfs_map_block() so that if the block group has the REMAPPED
flag set, we call btrfs_translate_remap() to obtain a new address.

btrfs_translate_remap() searches the remap tree for a range
corresponding to the logical address passed to btrfs_map_block(). If it
is within an identity remap, this part of the block group hasn't yet
been relocated, and so we use the existing address.

If it is within an actual remap, we subtract the start of the remap
range and add the address of its destination, contained in the item's
payload.

Reviewed-by: Boris Burkov <boris@bur.io>
Signed-off-by: Mark Harmstone <mark@harmstone.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/relocation.c
fs/btrfs/relocation.h
fs/btrfs/volumes.c

index 310b7d8..6de5083 100644 (file)
@@ -3859,6 +3859,56 @@ static const char *stage_to_string(enum reloc_stage stage)
        return "unknown";
 }
 
+int btrfs_translate_remap(struct btrfs_fs_info *fs_info, u64 *logical, u64 *length)
+{
+       int ret;
+       struct btrfs_key key, found_key;
+       struct extent_buffer *leaf;
+       struct btrfs_remap_item *remap;
+       BTRFS_PATH_AUTO_FREE(path);
+
+       path = btrfs_alloc_path();
+       if (!path)
+               return -ENOMEM;
+
+       key.objectid = *logical;
+       key.type = (u8)-1;
+       key.offset = (u64)-1;
+
+       ret = btrfs_search_slot(NULL, fs_info->remap_root, &key, path, 0, 0);
+       if (ret < 0)
+               return ret;
+
+       leaf = path->nodes[0];
+       if (path->slots[0] == 0)
+               return -ENOENT;
+
+       path->slots[0]--;
+
+       btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+       if (found_key.type != BTRFS_REMAP_KEY &&
+           found_key.type != BTRFS_IDENTITY_REMAP_KEY) {
+               return -ENOENT;
+       }
+
+       if (found_key.objectid > *logical ||
+           found_key.objectid + found_key.offset <= *logical) {
+               return -ENOENT;
+       }
+
+       if (*logical + *length > found_key.objectid + found_key.offset)
+               *length = found_key.objectid + found_key.offset - *logical;
+
+       if (found_key.type == BTRFS_IDENTITY_REMAP_KEY)
+               return 0;
+
+       remap = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_remap_item);
+       *logical += btrfs_remap_address(leaf, remap) - found_key.objectid;
+
+       return 0;
+}
+
 /*
  * function to relocate all extents in a block group.
  */
index 5c36b3f..c0ee260 100644 (file)
@@ -31,5 +31,6 @@ int btrfs_should_cancel_balance(const struct btrfs_fs_info *fs_info);
 struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr);
 bool btrfs_should_ignore_reloc_root(const struct btrfs_root *root);
 u64 btrfs_get_reloc_bg_bytenr(const struct btrfs_fs_info *fs_info);
+int btrfs_translate_remap(struct btrfs_fs_info *fs_info, u64 *logical, u64 *length);
 
 #endif
index 6280a1a..2a4bda4 100644 (file)
@@ -6586,6 +6586,24 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
        if (IS_ERR(map))
                return PTR_ERR(map);
 
+       if (map->type & BTRFS_BLOCK_GROUP_REMAPPED) {
+               u64 new_logical = logical;
+
+               ret = btrfs_translate_remap(fs_info, &new_logical, length);
+               if (ret)
+                       return ret;
+
+               if (new_logical != logical) {
+                       btrfs_free_chunk_map(map);
+
+                       map = btrfs_get_chunk_map(fs_info, new_logical, *length);
+                       if (IS_ERR(map))
+                               return PTR_ERR(map);
+
+                       logical = new_logical;
+               }
+       }
+
        num_copies = btrfs_chunk_map_num_copies(map);
        if (io_geom.mirror_num > num_copies)
                return -EINVAL;