Skip to contentSkip to conversation

Remap tree

RFC PATCH In review Mark Harmstone 6 participants 21 messages May 15, 2025 – May 23, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover Remap tree

Hi all,

This is an RFC for an experimental remap-tree incompat feature, which reworks how we perform relocations. Our present system involves following backrefs to rewrite addresses in the metadata trees, which can be slow and has knock-on effects such as breaking nocow files. Instead, this adds a remap tree to act as another layer of indirection: if a block group has the REMAPPED bit set, all I/O to addresses nominally within it get translated by a lookup to the remap tree.

See https://github.com/btrfs/btrfs-todo/issues/54 for Josef's original design, which I've elaborated on, and more on the rationale. To test this you will also need a patched btrfs-progs, for mkfs.btrfs -O remap-tree:
https://github.com/maharmstone/btrfs-progs/tree/remap-tree. See also
https://github.com/maharmstone/btrfs/blob/remap-tree/btrfs-dump.pl for a dumper script that's remap-aware.

The BTRFS_FEATURE_INCOMPAT_REMAP_TREE flag implies the following changes:

* There's a new remap tree, which is created empty

* The data reloc tree isn't created, as it's no longer needed (data relocation is no longer done via snapshots)

* There's a new metadata chunk type, REMAP, which consists solely of the remap tree. This is to avoid bootstrapping issues: REMAP chunks, like SYSTEM, are still relocated the old way (i.e. by COWing every leaf). We can't put the remap tree in SYSTEM: because the chunk items are put in the superblock, it would limit our available space

* The top of the remap tree is recorded in the superblock

* Two new fields are added to struct btrfs_block_group_item: le64 remap_bytes and le32 identity_remap_count. remap_bytes records how much data in a block group has been relocated from another block group. identity_remap_count records how many identity remaps exist for this block group (see later)

* If a block group has the REMAPPED flag set and identity_remaps == 0, its chunk will have 0 stripes and 0 dev extents: everything nominally within it its actually located elsewhere

* All data and metadata addresses are now translated addresses, for which we need to consult the remap tree to find the underlying addresses before doing I/O, if they live in a remapped BG. The exception is the free-space tree, which is entirely underlying addresses (the free-space cache v1 isn't supported here).

REMAP TREE
----------

The remap tree consists of three types of items: identity remaps, remaps, and remap backrefs. Each represents a non-overlapping range. Remaps are orthogonal to extents: an extent might be split into several ranges in the remap tree, or multiple consecutive extents might be remapped together.

Identity remaps represent a range for which we don't need to do a translation, i.e. the REMAPPED flag has been set for the BG but we haven't yet done a relocation.

Remaps have a u64 payload which gives the underlying address to use for the start of this range, i.e. the non-REMAPPED block group we should use for I/O.

Remap backrefs are a reverse index for the remaps, with their objectid being the underlying address and their u64 payload being the translated address. We need backrefs because when relocation a block group that contains existing remaps, these need to be moved first (we don't want to have to consult an ever-increasing chain of BGs).

RELOCATION
----------

For SYSTEM and REMAP block groups, relocation is as it is at the moment (mark the BG readonly, COW every leaf).

For DATA and METADATA block groups, we do the following:

* If remap_bytes > 0, search the remap tree for the remap backrefs that are physically located in this block group. Move the data on disk, munge the remap and free-space trees to reflect this, and reduce remap_bytes. Loop until remap_bytes is 0.

* Search the free-space tree for holes, convert these into identity remaps within the remap tree, set the identity_remap_count within in the block group item, and set the REMAPPED flag on the block group item and the chunk. The REMAPPED flag will prevent new allocations from being made from this block group.

* Walk through the remap tree looking for the identity remaps that we have created. For each one, try to reserve the same amount of space in another block group. Read the data into memory, and write an exact copy into the new location. Increase remap_bytes in the destination block group, and reduce identity_remap_count in the source block group if we can move the whole thing. Add the space back to the free-space tree in the source BG, and remove the space in the free-space tree for the destination BG.

* When identity_remaps_count == 0, the block group has been fully remapped, and now exists solely for the purposes of remapping. Set num_stripes to 0 in the chunk item, remove its stripes, and remove the entries in the dev extent tree.

* If a block group has the REMAPPED flag set, identity_remaps_count == 0, and remap_bytes == 0, it is now empty. The block group item, chunk item, and entries in the free-space tree can be removed.

KNOWN ISSUES
------------

* Still a few problems with some fstests: btrfs/156, btrfs/170, btrfs/177, btrfs/226, btrfs/250, btrfs/252.

* There's a race when it comes to nodatacow writes. I think we ought to be calling btrfs_inc_nocow_writers(), but that COWs rather than blocking.

* It can be reluctant to drop entries for empty remapped block groups. This doesn't waste substantial amounts of space on disk as there's no dev extents, but it is polluting the block group and chunk trees. Similarly we ought to be removing any enties in the free-space tree for fully remapped block groups: no new allocations can be done within them, and they no longer correspond to a location on disk.

* At the moment we're allocating 1GB for the REMAP chunks, which is probably overkilled. One 16KB leaf of the remap tree can cover ~250GB in the best case and ~1MB in the worst case. Possibly 32MB is the sweet spot, as for SYSTEM. Allocating more REMAP chunks isn't a problem, as unlike SYSTEM they don't go in the superblock.

* There's a spurious lockdep warning when doing remapped metadata reads, as we're locking an extent buffer within the remap tree while already holding another extent buffer lock. We either need to disable the warning for this, or change the code to use search_commit_root. We can't add another level to the lockdep hierarchy as we're already maxed out at 8.

* I'm think the lookup code in btrfs_translate_remap() probably needs to be refined. Possibly we can do without the call to btrfs_prev_leaf().

* At present we scan the free-space holes and create the identity remaps all in the same transaction. For the worst-case scenario of a 1GB block group with every other sector allocated, relocation takes ~30 seconds on my system, on an NVMe drive with no other load. Josef has suggested that we split this into multiple transactions, setting something like BTRFS_BLOCK_GROUP_ADDING_IDENTITY_REMAPS at the start, and discarding any progress on mount if we find a BG with this flag set, which makes sense to me. The code is currently gated behind CONFIG_BTRFS_EXPERIMENTAL. My preference would be that, like with the RAID stripe tree, we accept that there may be minor on-disk format changes until it can be moved out of experimental - i.e. we treat this as a (near-)future improvement rather than a blocker.

* Josef has also suggested that we don't log metadata items for the remap tree in the extent tree, in anticipation of later omitting all COW-only metadata items from the extent tree. My view is that treating the remap tree as a special case would be more trouble than it's worth, both here and in progs, and omitting all COW-only metadata items should be relegated to a later incompat change that depends on this one.

* There's still a lot of userspace work to be done: making sure that all space reporting etc. tools are okay, adding a comprehensive series of tests to btrfs check, allowing toggling this incompat flag on and off in btrfstune.

Thanks

Mark

Mark Harmstone (10):
btrfs: add definitions and constants for remap-tree
btrfs: add REMAP chunk type
btrfs: allow remapped chunks to have zero stripes
btrfs: add extended version of struct block_group_item
btrfs: allow mounting filesystems with remap-tree incompat flag
btrfs: redirect I/O for remapped block groups
btrfs: handle deletions from remapped block group
btrfs: handle setting up relocation of block group with remap-tree
btrfs: move existing remaps before relocating block group
btrfs: replace identity maps with actual remaps when doing relocations

 fs/btrfs/Kconfig                |    2 +
 fs/btrfs/accessors.h            |   29 +
 fs/btrfs/block-group.c          |  181 ++-
 fs/btrfs/block-group.h          |   15 +-
 fs/btrfs/block-rsv.c            |    8 +
 fs/btrfs/block-rsv.h            |    1 +
 fs/btrfs/ctree.c                |   11 +-
 fs/btrfs/ctree.h                |    3 +
 fs/btrfs/disk-io.c              |   88 +-
 fs/btrfs/extent-tree.c          |   38 +-
 fs/btrfs/free-space-tree.c      |    4 +-
 fs/btrfs/free-space-tree.h      |    5 +-
 fs/btrfs/fs.h                   |    7 +-
 fs/btrfs/relocation.c           | 2065 ++++++++++++++++++++++++++++++-
 fs/btrfs/relocation.h           |    8 +-
 fs/btrfs/space-info.c           |   20 +-
 fs/btrfs/sysfs.c                |    4 +
 fs/btrfs/transaction.c          |    7 +
 fs/btrfs/tree-checker.c         |   37 +-
 fs/btrfs/volumes.c              |  106 +-
 fs/btrfs/volumes.h              |   17 +-
 include/uapi/linux/btrfs.h      |    1 +
 include/uapi/linux/btrfs_tree.h |   29 +-
 23 files changed, 2509 insertions(+), 177 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

RFC PATCH 1/10 btrfs: add definitions and constants for remap-tree

Add an incompat flag for the new remap-tree feature, and the constants and definitions needed to support it.

---
 fs/btrfs/accessors.h            |  3 +++
 fs/btrfs/sysfs.c                |  2 ++
 fs/btrfs/tree-checker.c         |  6 ++++--
 fs/btrfs/volumes.c              |  1 +
 include/uapi/linux/btrfs.h      |  1 +
 include/uapi/linux/btrfs_tree.h | 12 ++++++++++++
 6 files changed, 23 insertions(+), 2 deletions(-)
fs/btrfs/accessors.h +3 -0
index 15ea6348800b..5f5eda8d6f9e 100644
@@ -1046,6 +1046,9 @@ BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_encryption,
 BTRFS_SETGET_STACK_FUNCS(stack_verity_descriptor_size,
 			 struct btrfs_verity_descriptor_item, size, 64);
 
+BTRFS_SETGET_FUNCS(remap_address, struct btrfs_remap, address, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_remap_address, struct btrfs_remap, address, 64);
+
 /* Cast into the data area of the leaf. */
 #define btrfs_item_ptr(leaf, slot, type)				\
 	((type *)(btrfs_item_nr_offset(leaf, 0) + btrfs_item_offset(leaf, slot)))
fs/btrfs/sysfs.c +2 -0
index 5d93d9dd2c12..3165194f62ab 100644
@@ -292,6 +292,7 @@ BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
 BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
 BTRFS_FEAT_ATTR_INCOMPAT(simple_quota, SIMPLE_QUOTA);
+BTRFS_FEAT_ATTR_INCOMPAT(remap_tree, REMAP_TREE);
 #ifdef CONFIG_BLK_DEV_ZONED
 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
 #endif
@@ -326,6 +327,7 @@ static struct attribute *btrfs_supported_feature_attrs[] = {
 	BTRFS_FEAT_ATTR_PTR(raid1c34),
 	BTRFS_FEAT_ATTR_PTR(block_group_tree),
 	BTRFS_FEAT_ATTR_PTR(simple_quota),
+	BTRFS_FEAT_ATTR_PTR(remap_tree),
 #ifdef CONFIG_BLK_DEV_ZONED
 	BTRFS_FEAT_ATTR_PTR(zoned),
 #endif
fs/btrfs/tree-checker.c +4 -2
index 8f4703b488b7..a83fb828723a 100644
@@ -913,11 +913,13 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
 		return -EUCLEAN;
 	}
 	if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
-			      BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
+			      BTRFS_BLOCK_GROUP_PROFILE_MASK |
+			      BTRFS_BLOCK_GROUP_REMAPPED))) {
 		chunk_err(fs_info, leaf, chunk, logical,
 			  "unrecognized chunk type: 0x%llx",
 			  ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
-			    BTRFS_BLOCK_GROUP_PROFILE_MASK) & type);
+			    BTRFS_BLOCK_GROUP_PROFILE_MASK |
+			    BTRFS_BLOCK_GROUP_REMAPPED) & type);
 		return -EUCLEAN;
 	}
 
fs/btrfs/volumes.c +1 -0
index 89835071cfea..e041964d03c8 100644
@@ -234,6 +234,7 @@ void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
 	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
 	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
 	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
+	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_REMAPPED, "remapped");
 
 	DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
 	for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
include/uapi/linux/btrfs.h +1 -0
index dd02160015b2..d857cdc7694a 100644
@@ -336,6 +336,7 @@ struct btrfs_ioctl_fs_info_args {
 #define BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2	(1ULL << 13)
 #define BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE	(1ULL << 14)
 #define BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA	(1ULL << 16)
+#define BTRFS_FEATURE_INCOMPAT_REMAP_TREE	(1ULL << 17)
 
 struct btrfs_ioctl_feature_flags {
 	__u64 compat_flags;
include/uapi/linux/btrfs_tree.h +12 -0
index fc29d273845d..4439d77a7252 100644
@@ -76,6 +76,9 @@
 /* Tracks RAID stripes in block groups. */
 #define BTRFS_RAID_STRIPE_TREE_OBJECTID 12ULL
 
+/* Holds details of remapped addresses after relocation. */
+#define BTRFS_REMAP_TREE_OBJECTID 13ULL
+
 /* device stats in the device tree */
 #define BTRFS_DEV_STATS_OBJECTID 0ULL
 
@@ -282,6 +285,10 @@
 
 #define BTRFS_RAID_STRIPE_KEY	230
 
+#define BTRFS_IDENTITY_REMAP_KEY 	234
+#define BTRFS_REMAP_KEY		 	235
+#define BTRFS_REMAP_BACKREF_KEY	 	236
+
 /*
  * Records the overall state of the qgroups.
  * There's only one instance of this key present,
@@ -1161,6 +1168,7 @@ struct btrfs_dev_replace_item {
 #define BTRFS_BLOCK_GROUP_RAID6         (1ULL << 8)
 #define BTRFS_BLOCK_GROUP_RAID1C3       (1ULL << 9)
 #define BTRFS_BLOCK_GROUP_RAID1C4       (1ULL << 10)
+#define BTRFS_BLOCK_GROUP_REMAPPED      (1ULL << 11)
 #define BTRFS_BLOCK_GROUP_RESERVED	(BTRFS_AVAIL_ALLOC_BIT_SINGLE | \
 					 BTRFS_SPACE_INFO_GLOBAL_RSV)
 
@@ -1323,4 +1331,8 @@ struct btrfs_verity_descriptor_item {
 	__u8 encryption;
 } __attribute__ ((__packed__));
 
+struct btrfs_remap {
+	__le64 address;
+} __attribute__ ((__packed__));
+
 #endif /* _BTRFS_CTREE_H_ */
Signature · 1 line
2.49.0
#2 lore raw

Re: [RFC PATCH 01/10] btrfs: add definitions and constants for remap-tree

include/uapi/linux/btrfs_tree.h line 286

Quoted 3 lines · Mark Harmstone
include/uapi/linux/btrfs_tree.h +0 -0
@@ -282,6 +285,10 @@
 
  #define BTRFS_RAID_STRIPE_KEY	230

Just a small heads up, I'd need 231 for BTRFS_RAID_STRIPE_PARITY_KEY and maybe a 232 as well, so there's still space just to let you know.

Quoted 4 lines
include/uapi/linux/btrfs_tree.h +3 -0
+#define BTRFS_IDENTITY_REMAP_KEY 	234
+#define BTRFS_REMAP_KEY		 	235
+#define BTRFS_REMAP_BACKREF_KEY	 	236
#3 lore raw

Re: [RFC PATCH 01/10] btrfs: add definitions and constants for remap-tree

Quoted 10 lines · Johannes Thumshirn
Quoted 1 line

include/uapi/linux/btrfs_tree.h line 286

Quoted 3 lines · Mark Harmstone
include/uapi/linux/btrfs_tree.h +0 -0
@@ -282,6 +285,10 @@
 
   #define BTRFS_RAID_STRIPE_KEY	230

Just a small heads up, I'd need 231 for BTRFS_RAID_STRIPE_PARITY_KEY and maybe a 232 as well, so there's still space just to let you know.

Quoted 1 line
Quoted 4 lines

include/uapi/linux/btrfs_tree.h line 290

Quoted 3 lines
include/uapi/linux/btrfs_tree.h +3 -0
+#define BTRFS_IDENTITY_REMAP_KEY 	234
+#define BTRFS_REMAP_KEY		 	235
+#define BTRFS_REMAP_BACKREF_KEY	 	236

No worries, thanks Johannes

#4 lore raw

RFC PATCH 2/10 btrfs: add REMAP chunk type

In reply to Mark Harmstone

Add a new REMAP chunk type, which is a metadata chunk that holds the remap tree.

This is needed for bootstrapping purposes: the remap tree can't itself be remapped, and must be relocated the existing way, by COWing every leaf. The remap tree can't go in the SYSTEM chunk as space there is limited, because a copy of the chunk item gets placed in the superblock.

The changes in fs/btrfs/volumes.h are because we're adding a new block group type bit after the profile bits, and so can no longer rely on the const_ilog2 trick.

---
 fs/btrfs/block-rsv.c            |  8 ++++++++
 fs/btrfs/block-rsv.h            |  1 +
 fs/btrfs/disk-io.c              |  1 +
 fs/btrfs/fs.h                   |  2 ++
 fs/btrfs/space-info.c           | 11 +++++++++++
 fs/btrfs/sysfs.c                |  2 ++
 fs/btrfs/tree-checker.c         |  5 +++--
 fs/btrfs/volumes.c              |  7 +++++++
 fs/btrfs/volumes.h              | 11 +++++++++--
 include/uapi/linux/btrfs_tree.h |  4 +++-
 10 files changed, 47 insertions(+), 5 deletions(-)
fs/btrfs/block-rsv.c +8 -0
index 5ad6de738aee..2678cd3bed29 100644
@@ -421,6 +421,9 @@ void btrfs_init_root_block_rsv(struct btrfs_root *root)
 	case BTRFS_TREE_LOG_OBJECTID:
 		root->block_rsv = &fs_info->treelog_rsv;
 		break;
+	case BTRFS_REMAP_TREE_OBJECTID:
+		root->block_rsv = &fs_info->remap_block_rsv;
+		break;
 	default:
 		root->block_rsv = NULL;
 		break;
@@ -434,6 +437,9 @@ void btrfs_init_global_block_rsv(struct btrfs_fs_info *fs_info)
 	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
 	fs_info->chunk_block_rsv.space_info = space_info;
 
+	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_REMAP);
+	fs_info->remap_block_rsv.space_info = space_info;
+
 	space_info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
 	fs_info->global_block_rsv.space_info = space_info;
 	fs_info->trans_block_rsv.space_info = space_info;
@@ -460,6 +466,8 @@ void btrfs_release_global_block_rsv(struct btrfs_fs_info *fs_info)
 	WARN_ON(fs_info->trans_block_rsv.reserved > 0);
 	WARN_ON(fs_info->chunk_block_rsv.size > 0);
 	WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
+	WARN_ON(fs_info->remap_block_rsv.size > 0);
+	WARN_ON(fs_info->remap_block_rsv.reserved > 0);
 	WARN_ON(fs_info->delayed_block_rsv.size > 0);
 	WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
 	WARN_ON(fs_info->delayed_refs_rsv.reserved > 0);
fs/btrfs/block-rsv.h +1 -0
index 79ae9d05cd91..8359fb96bc3c 100644
@@ -22,6 +22,7 @@ enum btrfs_rsv_type {
 	BTRFS_BLOCK_RSV_DELALLOC,
 	BTRFS_BLOCK_RSV_TRANS,
 	BTRFS_BLOCK_RSV_CHUNK,
+	BTRFS_BLOCK_RSV_REMAP,
 	BTRFS_BLOCK_RSV_DELOPS,
 	BTRFS_BLOCK_RSV_DELREFS,
 	BTRFS_BLOCK_RSV_TREELOG,
fs/btrfs/disk-io.c +1 -0
index 1beb9458f622..95058c9aa31b 100644
@@ -2831,6 +2831,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 			     BTRFS_BLOCK_RSV_GLOBAL);
 	btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
 	btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
+	btrfs_init_block_rsv(&fs_info->remap_block_rsv, BTRFS_BLOCK_RSV_REMAP);
 	btrfs_init_block_rsv(&fs_info->treelog_rsv, BTRFS_BLOCK_RSV_TREELOG);
 	btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
 	btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
fs/btrfs/fs.h +2 -0
index 4394de12a767..2dfdbfda5901 100644
@@ -468,6 +468,8 @@ struct btrfs_fs_info {
 	struct btrfs_block_rsv trans_block_rsv;
 	/* Block reservation for chunk tree */
 	struct btrfs_block_rsv chunk_block_rsv;
+	/* Block reservation for remap tree */
+	struct btrfs_block_rsv remap_block_rsv;
 	/* Block reservation for delayed operations */
 	struct btrfs_block_rsv delayed_block_rsv;
 	/* Block reservation for delayed refs */
fs/btrfs/space-info.c +11 -0
index d9087aa81b21..3f927a514643 100644
@@ -343,6 +343,8 @@ int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
 	if (mixed) {
 		flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
 		ret = create_space_info(fs_info, flags);
+		if (ret)
+			goto out;
 	} else {
 		flags = BTRFS_BLOCK_GROUP_METADATA;
 		ret = create_space_info(fs_info, flags);
@@ -351,7 +353,15 @@ int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
 
 		flags = BTRFS_BLOCK_GROUP_DATA;
 		ret = create_space_info(fs_info, flags);
+		if (ret)
+			goto out;
+	}
+
+	if (features & BTRFS_FEATURE_INCOMPAT_REMAP_TREE) {
+		flags = BTRFS_BLOCK_GROUP_REMAP;
+		ret = create_space_info(fs_info, flags);
 	}
+
 out:
 	return ret;
 }
@@ -590,6 +600,7 @@ static void dump_global_block_rsv(struct btrfs_fs_info *fs_info)
 	DUMP_BLOCK_RSV(fs_info, global_block_rsv);
 	DUMP_BLOCK_RSV(fs_info, trans_block_rsv);
 	DUMP_BLOCK_RSV(fs_info, chunk_block_rsv);
+	DUMP_BLOCK_RSV(fs_info, remap_block_rsv);
 	DUMP_BLOCK_RSV(fs_info, delayed_block_rsv);
 	DUMP_BLOCK_RSV(fs_info, delayed_refs_rsv);
 }
fs/btrfs/sysfs.c +2 -0
index 3165194f62ab..b8c2d9a5ebeb 100644
@@ -1962,6 +1962,8 @@ static const char *alloc_name(struct btrfs_space_info *space_info)
 	case BTRFS_BLOCK_GROUP_SYSTEM:
 		ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_PRIMARY);
 		return "system";
+	case BTRFS_BLOCK_GROUP_REMAP:
+		return "remap";
 	default:
 		WARN_ON(1);
 		return "invalid-combination";
fs/btrfs/tree-checker.c +3 -2
index a83fb828723a..0505f8d76581 100644
@@ -751,13 +751,14 @@ static int check_block_group_item(struct extent_buffer *leaf,
 	if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
 		     type != BTRFS_BLOCK_GROUP_METADATA &&
 		     type != BTRFS_BLOCK_GROUP_SYSTEM &&
+		     type != BTRFS_BLOCK_GROUP_REMAP &&
 		     type != (BTRFS_BLOCK_GROUP_METADATA |
 			      BTRFS_BLOCK_GROUP_DATA))) {
 		block_group_err(leaf, slot,
-"invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
+"invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx, 0x%llx or 0x%llx",
 			type, hweight64(type),
 			BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
-			BTRFS_BLOCK_GROUP_SYSTEM,
+			BTRFS_BLOCK_GROUP_SYSTEM, BTRFS_BLOCK_GROUP_REMAP,
 			BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
 		return -EUCLEAN;
 	}
fs/btrfs/volumes.c +7 -0
index e041964d03c8..0698613276d9 100644
@@ -234,6 +234,7 @@ void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
 	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
 	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
 	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
+	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_REMAP, "remap");
 	DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_REMAPPED, "remapped");
 
 	DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
@@ -3974,6 +3975,12 @@ static bool should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk
 	struct btrfs_balance_args *bargs = NULL;
 	u64 chunk_type = btrfs_chunk_type(leaf, chunk);
 
+	/* treat REMAP chunks as METADATA */
+	if (chunk_type & BTRFS_BLOCK_GROUP_REMAP) {
+		chunk_type &= ~BTRFS_BLOCK_GROUP_REMAP;
+		chunk_type |= BTRFS_BLOCK_GROUP_METADATA;
+	}
+
 	/* type filter */
 	if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
 	      (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
fs/btrfs/volumes.h +9 -2
index 137cc232f58e..670d7bf18c40 100644
@@ -59,8 +59,6 @@ static_assert(const_ilog2(BTRFS_STRIPE_LEN) == BTRFS_STRIPE_LEN_SHIFT);
  */
 static_assert(const_ffs(BTRFS_BLOCK_GROUP_RAID0) <
 	      const_ffs(BTRFS_BLOCK_GROUP_PROFILE_MASK & ~BTRFS_BLOCK_GROUP_RAID0));
-static_assert(const_ilog2(BTRFS_BLOCK_GROUP_RAID0) >
-	      ilog2(BTRFS_BLOCK_GROUP_TYPE_MASK));
 
 /* ilog2() can handle both constants and variables */
 #define BTRFS_BG_FLAG_TO_INDEX(profile)					\
@@ -82,6 +80,15 @@ enum btrfs_raid_types {
 	BTRFS_NR_RAID_TYPES
 };
 
+static_assert(BTRFS_RAID_RAID0 == 1);
+static_assert(BTRFS_RAID_RAID1 == 2);
+static_assert(BTRFS_RAID_DUP == 3);
+static_assert(BTRFS_RAID_RAID10 == 4);
+static_assert(BTRFS_RAID_RAID5 == 5);
+static_assert(BTRFS_RAID_RAID6 == 6);
+static_assert(BTRFS_RAID_RAID1C3 == 7);
+static_assert(BTRFS_RAID_RAID1C4 == 8);
+
 /*
  * Use sequence counter to get consistent device stat data on
  * 32-bit processors.
include/uapi/linux/btrfs_tree.h +3 -1
index 4439d77a7252..9a36f0206d90 100644
@@ -1169,12 +1169,14 @@ struct btrfs_dev_replace_item {
 #define BTRFS_BLOCK_GROUP_RAID1C3       (1ULL << 9)
 #define BTRFS_BLOCK_GROUP_RAID1C4       (1ULL << 10)
 #define BTRFS_BLOCK_GROUP_REMAPPED      (1ULL << 11)
+#define BTRFS_BLOCK_GROUP_REMAP         (1ULL << 12)
 #define BTRFS_BLOCK_GROUP_RESERVED	(BTRFS_AVAIL_ALLOC_BIT_SINGLE | \
 					 BTRFS_SPACE_INFO_GLOBAL_RSV)
 
 #define BTRFS_BLOCK_GROUP_TYPE_MASK	(BTRFS_BLOCK_GROUP_DATA |    \
 					 BTRFS_BLOCK_GROUP_SYSTEM |  \
-					 BTRFS_BLOCK_GROUP_METADATA)
+					 BTRFS_BLOCK_GROUP_METADATA | \
+					 BTRFS_BLOCK_GROUP_REMAP)
 
 #define BTRFS_BLOCK_GROUP_PROFILE_MASK	(BTRFS_BLOCK_GROUP_RAID0 |   \
 					 BTRFS_BLOCK_GROUP_RAID1 |   \
Signature · 1 line
2.49.0
#5 lore raw

RFC PATCH 3/10 btrfs: allow remapped chunks to have zero stripes

In reply to Mark Harmstone

When a chunk has been fully remapped, we are going to set its num_stripes to 0, as it will no longer represent a physical location on disk.

Change tree-checker to allow for this, and fix a couple of divide-by-zeroes seen elsewhere.

---
 fs/btrfs/tree-checker.c | 16 +++++++++-------
 fs/btrfs/volumes.c      |  8 +++++++-
 2 files changed, 16 insertions(+), 8 deletions(-)
fs/btrfs/tree-checker.c +9 -7
index 0505f8d76581..fd83df06e3fb 100644
@@ -829,7 +829,7 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
 	u64 type;
 	u64 features;
 	u32 chunk_sector_size;
-	bool mixed = false;
+	bool mixed = false, remapped;
 	int raid_index;
 	int nparity;
 	int ncopies;
@@ -853,12 +853,14 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
 	ncopies = btrfs_raid_array[raid_index].ncopies;
 	nparity = btrfs_raid_array[raid_index].nparity;
 
-	if (unlikely(!num_stripes)) {
+	remapped = type & BTRFS_BLOCK_GROUP_REMAPPED;
+
+	if (unlikely(!remapped && !num_stripes)) {
 		chunk_err(fs_info, leaf, chunk, logical,
 			  "invalid chunk num_stripes, have %u", num_stripes);
 		return -EUCLEAN;
 	}
-	if (unlikely(num_stripes < ncopies)) {
+	if (unlikely(!remapped && num_stripes < ncopies)) {
 		chunk_err(fs_info, leaf, chunk, logical,
 			  "invalid chunk num_stripes < ncopies, have %u < %d",
 			  num_stripes, ncopies);
@@ -960,7 +962,7 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
 		}
 	}
 
-	if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
+	if (unlikely(!remapped && ((type & BTRFS_BLOCK_GROUP_RAID10 &&
 		      sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
 		     (type & BTRFS_BLOCK_GROUP_RAID1 &&
 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
@@ -975,7 +977,7 @@ int btrfs_check_chunk_valid(const struct btrfs_fs_info *fs_info,
 		     (type & BTRFS_BLOCK_GROUP_DUP &&
 		      num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
 		     ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
-		      num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
+		      num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes)))) {
 		chunk_err(fs_info, leaf, chunk, logical,
 			"invalid num_stripes:sub_stripes %u:%u for profile %llu",
 			num_stripes, sub_stripes,
@@ -999,11 +1001,11 @@ static int check_leaf_chunk_item(struct extent_buffer *leaf,
 	struct btrfs_fs_info *fs_info = leaf->fs_info;
 	int num_stripes;
 
-	if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
+	if (unlikely(btrfs_item_size(leaf, slot) < offsetof(struct btrfs_chunk, stripe))) {
 		chunk_err(fs_info, leaf, chunk, key->offset,
 			"invalid chunk item size: have %u expect [%zu, %u)",
 			btrfs_item_size(leaf, slot),
-			sizeof(struct btrfs_chunk),
+			offsetof(struct btrfs_chunk, stripe),
 			BTRFS_LEAF_DATA_SIZE(fs_info));
 		return -EUCLEAN;
 	}
fs/btrfs/volumes.c +7 -1
index 0698613276d9..77194bb46b40 100644
@@ -6133,6 +6133,12 @@ struct btrfs_discard_stripe *btrfs_map_discard(struct btrfs_fs_info *fs_info,
 		goto out_free_map;
 	}
 
+	/* avoid divide by zero on fully-remapped chunks */
+	if (map->num_stripes == 0) {
+		ret = -EOPNOTSUPP;
+		goto out_free_map;
+	}
+
 	offset = logical - map->start;
 	length = min_t(u64, map->start + map->chunk_len - logical, length);
 	*length_ret = length;
@@ -6953,7 +6959,7 @@ u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map)
 {
 	const int data_stripes = calc_data_stripes(map->type, map->num_stripes);
 
-	return div_u64(map->chunk_len, data_stripes);
+	return data_stripes ? div_u64(map->chunk_len, data_stripes) : 0;
 }
 
 #if BITS_PER_LONG == 32
Signature · 1 line
2.49.0
#6 lore raw

RFC PATCH 4/10 btrfs: add extended version of struct block_group_item

In reply to Mark Harmstone

Add a struct btrfs_block_group_item_v2, which is used in the block group tree if the remap-tree incompat flag is set.

This adds two new fields to the block group item: remap_bytes and identity_remap_count.

remap_bytes records the amount of data that's physically within this block group, but nominally in another, remapped block group. This is necessary because this data will need to be moved first if this block group is itself relocated. If remap_bytes > 0, this is an indicator to the relocation thread that it will need to search the remap-tree for backrefs. A block group must also have remap_bytes == 0 before it can be dropped.

identity_remap_count records how many identity remap items are located in the remap tree for this block group. When relocation is begun for this block group, this is set to the number of holes in the free-space tree for this range. As identity remaps are converted into actual remaps by the relocation process, this number is decreased. Once it reaches 0, either because of relocation or because extents have been deleted, the block group has been fully remapped and its chunk's device extents are removed.

---
 fs/btrfs/accessors.h            |  20 +++++++
 fs/btrfs/block-group.c          | 101 ++++++++++++++++++++++++--------
 fs/btrfs/block-group.h          |  14 ++++-
 fs/btrfs/tree-checker.c         |  10 +++-
 include/uapi/linux/btrfs_tree.h |   8 +++
 5 files changed, 126 insertions(+), 27 deletions(-)
fs/btrfs/accessors.h +20 -0
index 5f5eda8d6f9e..6e6dd664217b 100644
@@ -264,6 +264,26 @@ BTRFS_SETGET_FUNCS(block_group_flags, struct btrfs_block_group_item, flags, 64);
 BTRFS_SETGET_STACK_FUNCS(stack_block_group_flags,
 			struct btrfs_block_group_item, flags, 64);
 
+/* struct btrfs_block_group_item_v2 */
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_used, struct btrfs_block_group_item_v2,
+			 used, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_used, struct btrfs_block_group_item_v2, used, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_chunk_objectid,
+			 struct btrfs_block_group_item_v2, chunk_objectid, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_chunk_objectid,
+		   struct btrfs_block_group_item_v2, chunk_objectid, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_flags,
+			 struct btrfs_block_group_item_v2, flags, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_flags, struct btrfs_block_group_item_v2, flags, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_remap_bytes,
+			 struct btrfs_block_group_item_v2, remap_bytes, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_remap_bytes, struct btrfs_block_group_item_v2,
+		   remap_bytes, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_identity_remap_count,
+			 struct btrfs_block_group_item_v2, identity_remap_count, 32);
+BTRFS_SETGET_FUNCS(block_group_v2_identity_remap_count, struct btrfs_block_group_item_v2,
+		   identity_remap_count, 32);
+
 /* struct btrfs_free_space_info */
 BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
 		   extent_count, 32);
fs/btrfs/block-group.c +77 -24

191 lines not on this page · raw message

fs/btrfs/block-group.h +13 -1
index 9de356bcb411..c484118b8b8d 100644
@@ -127,6 +127,8 @@ struct btrfs_block_group {
 	u64 flags;
 	u64 cache_generation;
 	u64 global_root_id;
+	u64 remap_bytes;
+	u32 identity_remap_count;
 
 	/*
 	 * The last committed used bytes of this block group, if the above @used
@@ -134,6 +136,15 @@ struct btrfs_block_group {
 	 * group item of this block group.
 	 */
 	u64 commit_used;
+	/*
+	 * The last committed remap_bytes value of this block group.
+	 */
+	u64 commit_remap_bytes;
+	/*
+	 * The last commited identity_remap_count value of this block group.
+	 */
+	u32 commit_identity_remap_count;
+
 	/*
 	 * If the free space extent count exceeds this number, convert the block
 	 * group to bitmaps.
@@ -275,7 +286,8 @@ static inline bool btrfs_is_block_group_used(const struct btrfs_block_group *bg)
 {
 	lockdep_assert_held(&bg->lock);
 
-	return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0);
+	return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0 ||
+		bg->remap_bytes > 0);
 }
 
 static inline bool btrfs_is_block_group_data_only(const struct btrfs_block_group *block_group)
fs/btrfs/tree-checker.c +8 -2
index fd83df06e3fb..25311576fab6 100644
@@ -687,6 +687,7 @@ static int check_block_group_item(struct extent_buffer *leaf,
 	u64 chunk_objectid;
 	u64 flags;
 	u64 type;
+	size_t exp_size;
 
 	/*
 	 * Here we don't really care about alignment since extent allocator can
@@ -698,10 +699,15 @@ static int check_block_group_item(struct extent_buffer *leaf,
 		return -EUCLEAN;
 	}
 
-	if (unlikely(item_size != sizeof(bgi))) {
+	if (btrfs_fs_incompat(fs_info, REMAP_TREE))
+		exp_size = sizeof(struct btrfs_block_group_item_v2);
+	else
+		exp_size = sizeof(struct btrfs_block_group_item);
+
+	if (unlikely(item_size != exp_size)) {
 		block_group_err(leaf, slot,
 			"invalid item size, have %u expect %zu",
-				item_size, sizeof(bgi));
+				item_size, exp_size);
 		return -EUCLEAN;
 	}
 
include/uapi/linux/btrfs_tree.h +8 -0
index 9a36f0206d90..500e3a7df90b 100644
@@ -1229,6 +1229,14 @@ struct btrfs_block_group_item {
 	__le64 flags;
 } __attribute__ ((__packed__));
 
+struct btrfs_block_group_item_v2 {
+	__le64 used;
+	__le64 chunk_objectid;
+	__le64 flags;
+	__le64 remap_bytes;
+	__le32 identity_remap_count;
+} __attribute__ ((__packed__));
+
 struct btrfs_free_space_info {
 	__le32 extent_count;
 	__le32 flags;
Signature · 1 line
2.49.0
#7 lore raw

Re: [RFC PATCH 04/10] btrfs: add extended version of struct block_group_item

Quoted 14 lines · Mark Harmstone

Add a struct btrfs_block_group_item_v2, which is used in the block group tree if the remap-tree incompat flag is set.

This adds two new fields to the block group item: remap_bytes and identity_remap_count.

remap_bytes records the amount of data that's physically within this block group, but nominally in another, remapped block group. This is necessary because this data will need to be moved first if this block group is itself relocated. If remap_bytes > 0, this is an indicator to the relocation thread that it will need to search the remap-tree for backrefs. A block group must also have remap_bytes == 0 before it can be dropped.

Quoted 8 lines

identity_remap_count records how many identity remap items are located in the remap tree for this block group. When relocation is begun for this block group, this is set to the number of holes in the free-space tree for this range. As identity remaps are converted into actual remaps by the relocation process, this number is decreased. Once it reaches 0, either because of relocation or because extents have been deleted, the block group has been fully remapped and its chunk's device extents are removed.

Can we add those two items into a new item other than a completely new v2 block group item?

I mean for regular block groups they do not need those members, and all block groups starts from regular ones at mkfs time.

We can add a regular block group flag to indicate if the bg has the extra members.

Thanks,
Qu

Quoted 335 lines
---
  fs/btrfs/accessors.h            |  20 +++++++
  fs/btrfs/block-group.c          | 101 ++++++++++++++++++++++++--------
  fs/btrfs/block-group.h          |  14 ++++-
  fs/btrfs/tree-checker.c         |  10 +++-
  include/uapi/linux/btrfs_tree.h |   8 +++
  5 files changed, 126 insertions(+), 27 deletions(-)
fs/btrfs/accessors.h +20 -0
index 5f5eda8d6f9e..6e6dd664217b 100644
@@ -264,6 +264,26 @@ BTRFS_SETGET_FUNCS(block_group_flags, struct btrfs_block_group_item, flags, 64);
  BTRFS_SETGET_STACK_FUNCS(stack_block_group_flags,
  			struct btrfs_block_group_item, flags, 64);
 
+/* struct btrfs_block_group_item_v2 */
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_used, struct btrfs_block_group_item_v2,
+			 used, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_used, struct btrfs_block_group_item_v2, used, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_chunk_objectid,
+			 struct btrfs_block_group_item_v2, chunk_objectid, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_chunk_objectid,
+		   struct btrfs_block_group_item_v2, chunk_objectid, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_flags,
+			 struct btrfs_block_group_item_v2, flags, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_flags, struct btrfs_block_group_item_v2, flags, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_remap_bytes,
+			 struct btrfs_block_group_item_v2, remap_bytes, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_remap_bytes, struct btrfs_block_group_item_v2,
+		   remap_bytes, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_identity_remap_count,
+			 struct btrfs_block_group_item_v2, identity_remap_count, 32);
+BTRFS_SETGET_FUNCS(block_group_v2_identity_remap_count, struct btrfs_block_group_item_v2,
+		   identity_remap_count, 32);
+
  /* struct btrfs_free_space_info */
  BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
  		   extent_count, 32);
fs/btrfs/block-group.c +77 -24
index 5b0cb04b2b93..6a2aa792ccb2 100644
@@ -2351,7 +2351,7 @@ static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
  }
 
  static int read_one_block_group(struct btrfs_fs_info *info,
-				struct btrfs_block_group_item *bgi,
+				struct btrfs_block_group_item_v2 *bgi,
  				const struct btrfs_key *key,
  				int need_clear)
  {
@@ -2366,11 +2366,16 @@ static int read_one_block_group(struct btrfs_fs_info *info,
  		return -ENOMEM;
 
  	cache->length = key->offset;
-	cache->used = btrfs_stack_block_group_used(bgi);
+	cache->used = btrfs_stack_block_group_v2_used(bgi);
  	cache->commit_used = cache->used;
-	cache->flags = btrfs_stack_block_group_flags(bgi);
-	cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi);
+	cache->flags = btrfs_stack_block_group_v2_flags(bgi);
+	cache->global_root_id = btrfs_stack_block_group_v2_chunk_objectid(bgi);
  	cache->space_info = btrfs_find_space_info(info, cache->flags);
+	cache->remap_bytes = btrfs_stack_block_group_v2_remap_bytes(bgi);
+	cache->commit_remap_bytes = cache->remap_bytes;
+	cache->identity_remap_count =
+		btrfs_stack_block_group_v2_identity_remap_count(bgi);
+	cache->commit_identity_remap_count = cache->identity_remap_count;
 
  	set_free_space_tree_thresholds(cache);
 
@@ -2435,7 +2440,7 @@ static int read_one_block_group(struct btrfs_fs_info *info,
  	} else if (cache->length == cache->used) {
  		cache->cached = BTRFS_CACHE_FINISHED;
  		btrfs_free_excluded_extents(cache);
-	} else if (cache->used == 0) {
+	} else if (cache->used == 0 && cache->remap_bytes == 0) {
  		cache->cached = BTRFS_CACHE_FINISHED;
  		ret = btrfs_add_new_free_space(cache, cache->start,
  					       cache->start + cache->length, NULL);
@@ -2455,7 +2460,8 @@ static int read_one_block_group(struct btrfs_fs_info *info,
 
  	set_avail_alloc_bits(info, cache->flags);
  	if (btrfs_chunk_writeable(info, cache->start)) {
-		if (cache->used == 0) {
+		if (cache->used == 0 && cache->identity_remap_count == 0 &&
+		    cache->remap_bytes == 0) {
  			ASSERT(list_empty(&cache->bg_list));
  			if (btrfs_test_opt(info, DISCARD_ASYNC))
  				btrfs_discard_queue_work(&info->discard_ctl, cache);
@@ -2559,9 +2565,10 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
  		need_clear = 1;
 
  	while (1) {
-		struct btrfs_block_group_item bgi;
+		struct btrfs_block_group_item_v2 bgi;
  		struct extent_buffer *leaf;
  		int slot;
+		size_t size;
 
  		ret = find_first_block_group(info, path, &key);
  		if (ret > 0)
@@ -2572,8 +2579,16 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
  		leaf = path->nodes[0];
  		slot = path->slots[0];
 
+		if (btrfs_fs_incompat(info, REMAP_TREE)) {
+			size = sizeof(struct btrfs_block_group_item_v2);
+		} else {
+			size = sizeof(struct btrfs_block_group_item);
+			btrfs_set_stack_block_group_v2_remap_bytes(&bgi, 0);
+			btrfs_set_stack_block_group_v2_identity_remap_count(&bgi, 0);
+		}
+
  		read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
-				   sizeof(bgi));
+				   size);
 
  		btrfs_item_key_to_cpu(leaf, &key, slot);
  		btrfs_release_path(path);
@@ -2643,25 +2658,38 @@ static int insert_block_group_item(struct btrfs_trans_handle *trans,
  				   struct btrfs_block_group *block_group)
  {
  	struct btrfs_fs_info *fs_info = trans->fs_info;
-	struct btrfs_block_group_item bgi;
+	struct btrfs_block_group_item_v2 bgi;
  	struct btrfs_root *root = btrfs_block_group_root(fs_info);
  	struct btrfs_key key;
  	u64 old_commit_used;
+	size_t size;
  	int ret;
 
  	spin_lock(&block_group->lock);
-	btrfs_set_stack_block_group_used(&bgi, block_group->used);
-	btrfs_set_stack_block_group_chunk_objectid(&bgi,
-						   block_group->global_root_id);
-	btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
+	btrfs_set_stack_block_group_v2_used(&bgi, block_group->used);
+	btrfs_set_stack_block_group_v2_chunk_objectid(&bgi,
+						      block_group->global_root_id);
+	btrfs_set_stack_block_group_v2_flags(&bgi, block_group->flags);
+	btrfs_set_stack_block_group_v2_remap_bytes(&bgi,
+						   block_group->remap_bytes);
+	btrfs_set_stack_block_group_v2_identity_remap_count(&bgi,
+					block_group->identity_remap_count);
  	old_commit_used = block_group->commit_used;
  	block_group->commit_used = block_group->used;
+	block_group->commit_remap_bytes = block_group->remap_bytes;
+	block_group->commit_identity_remap_count =
+		block_group->identity_remap_count;
  	key.objectid = block_group->start;
  	key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  	key.offset = block_group->length;
  	spin_unlock(&block_group->lock);
 
-	ret = btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
+	if (btrfs_fs_incompat(fs_info, REMAP_TREE))
+		size = sizeof(struct btrfs_block_group_item_v2);
+	else
+		size = sizeof(struct btrfs_block_group_item);
+
+	ret = btrfs_insert_item(trans, root, &key, &bgi, size);
  	if (ret < 0) {
  		spin_lock(&block_group->lock);
  		block_group->commit_used = old_commit_used;
@@ -3116,10 +3144,12 @@ static int update_block_group_item(struct btrfs_trans_handle *trans,
  	struct btrfs_root *root = btrfs_block_group_root(fs_info);
  	unsigned long bi;
  	struct extent_buffer *leaf;
-	struct btrfs_block_group_item bgi;
+	struct btrfs_block_group_item_v2 bgi;
  	struct btrfs_key key;
-	u64 old_commit_used;
-	u64 used;
+	u64 old_commit_used, old_commit_remap_bytes;
+	u32 old_commit_identity_remap_count;
+	u64 used, remap_bytes;
+	u32 identity_remap_count;
 
  	/*
  	 * Block group items update can be triggered out of commit transaction
@@ -3129,13 +3159,21 @@ static int update_block_group_item(struct btrfs_trans_handle *trans,
  	 */
  	spin_lock(&cache->lock);
  	old_commit_used = cache->commit_used;
+	old_commit_remap_bytes = cache->commit_remap_bytes;
+	old_commit_identity_remap_count = cache->commit_identity_remap_count;
  	used = cache->used;
-	/* No change in used bytes, can safely skip it. */
-	if (cache->commit_used == used) {
+	remap_bytes = cache->remap_bytes;
+	identity_remap_count = cache->identity_remap_count;
+	/* No change in values, can safely skip it. */
+	if (cache->commit_used == used &&
+	    cache->commit_remap_bytes == remap_bytes &&
+	    cache->commit_identity_remap_count == identity_remap_count) {
  		spin_unlock(&cache->lock);
  		return 0;
  	}
  	cache->commit_used = used;
+	cache->commit_remap_bytes = remap_bytes;
+	cache->commit_identity_remap_count = identity_remap_count;
  	spin_unlock(&cache->lock);
 
  	key.objectid = cache->start;
@@ -3151,11 +3189,23 @@ static int update_block_group_item(struct btrfs_trans_handle *trans,
 
  	leaf = path->nodes[0];
  	bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
-	btrfs_set_stack_block_group_used(&bgi, used);
-	btrfs_set_stack_block_group_chunk_objectid(&bgi,
-						   cache->global_root_id);
-	btrfs_set_stack_block_group_flags(&bgi, cache->flags);
-	write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
+	btrfs_set_stack_block_group_v2_used(&bgi, used);
+	btrfs_set_stack_block_group_v2_chunk_objectid(&bgi,
+						      cache->global_root_id);
+	btrfs_set_stack_block_group_v2_flags(&bgi, cache->flags);
+
+	if (btrfs_fs_incompat(fs_info, REMAP_TREE)) {
+		btrfs_set_stack_block_group_v2_remap_bytes(&bgi,
+							   cache->remap_bytes);
+		btrfs_set_stack_block_group_v2_identity_remap_count(&bgi,
+						cache->identity_remap_count);
+		write_extent_buffer(leaf, &bgi, bi,
+				    sizeof(struct btrfs_block_group_item_v2));
+	} else {
+		write_extent_buffer(leaf, &bgi, bi,
+				    sizeof(struct btrfs_block_group_item));
+	}
+
  fail:
  	btrfs_release_path(path);
  	/*
@@ -3170,6 +3220,9 @@ static int update_block_group_item(struct btrfs_trans_handle *trans,
  	if (ret < 0 && ret != -ENOENT) {
  		spin_lock(&cache->lock);
  		cache->commit_used = old_commit_used;
+		cache->commit_remap_bytes = old_commit_remap_bytes;
+		cache->commit_identity_remap_count =
+			old_commit_identity_remap_count;
  		spin_unlock(&cache->lock);
  	}
  	return ret;
fs/btrfs/block-group.h +13 -1
index 9de356bcb411..c484118b8b8d 100644
@@ -127,6 +127,8 @@ struct btrfs_block_group {
  	u64 flags;
  	u64 cache_generation;
  	u64 global_root_id;
+	u64 remap_bytes;
+	u32 identity_remap_count;
 
  	/*
  	 * The last committed used bytes of this block group, if the above @used
@@ -134,6 +136,15 @@ struct btrfs_block_group {
  	 * group item of this block group.
  	 */
  	u64 commit_used;
+	/*
+	 * The last committed remap_bytes value of this block group.
+	 */
+	u64 commit_remap_bytes;
+	/*
+	 * The last commited identity_remap_count value of this block group.
+	 */
+	u32 commit_identity_remap_count;
+
  	/*
  	 * If the free space extent count exceeds this number, convert the block
  	 * group to bitmaps.
@@ -275,7 +286,8 @@ static inline bool btrfs_is_block_group_used(const struct btrfs_block_group *bg)
  {
  	lockdep_assert_held(&bg->lock);
 
-	return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0);
+	return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0 ||
+		bg->remap_bytes > 0);
  }
 
  static inline bool btrfs_is_block_group_data_only(const struct btrfs_block_group *block_group)
fs/btrfs/tree-checker.c +8 -2
index fd83df06e3fb..25311576fab6 100644
@@ -687,6 +687,7 @@ static int check_block_group_item(struct extent_buffer *leaf,
  	u64 chunk_objectid;
  	u64 flags;
  	u64 type;
+	size_t exp_size;
 
  	/*
  	 * Here we don't really care about alignment since extent allocator can
@@ -698,10 +699,15 @@ static int check_block_group_item(struct extent_buffer *leaf,
  		return -EUCLEAN;
  	}
 
-	if (unlikely(item_size != sizeof(bgi))) {
+	if (btrfs_fs_incompat(fs_info, REMAP_TREE))
+		exp_size = sizeof(struct btrfs_block_group_item_v2);
+	else
+		exp_size = sizeof(struct btrfs_block_group_item);
+
+	if (unlikely(item_size != exp_size)) {
  		block_group_err(leaf, slot,
  			"invalid item size, have %u expect %zu",
-				item_size, sizeof(bgi));
+				item_size, exp_size);
  		return -EUCLEAN;
  	}
 
include/uapi/linux/btrfs_tree.h +8 -0
index 9a36f0206d90..500e3a7df90b 100644
@@ -1229,6 +1229,14 @@ struct btrfs_block_group_item {
  	__le64 flags;
  } __attribute__ ((__packed__));
 
+struct btrfs_block_group_item_v2 {
+	__le64 used;
+	__le64 chunk_objectid;
+	__le64 flags;
+	__le64 remap_bytes;
+	__le32 identity_remap_count;
+} __attribute__ ((__packed__));
+
  struct btrfs_free_space_info {
  	__le32 extent_count;
  	__le32 flags;
#8 lore raw

Re: [RFC PATCH 04/10] btrfs: add extended version of struct block_group_item

Quoted 30 lines · Qu Wenruo
Quoted 1 line
Quoted 14 lines · Mark Harmstone

Add a struct btrfs_block_group_item_v2, which is used in the block group tree if the remap-tree incompat flag is set.

This adds two new fields to the block group item: remap_bytes and identity_remap_count.

remap_bytes records the amount of data that's physically within this block group, but nominally in another, remapped block group. This is necessary because this data will need to be moved first if this block group is itself relocated. If remap_bytes > 0, this is an indicator to the relocation thread that it will need to search the remap-tree for backrefs. A block group must also have remap_bytes == 0 before it can be dropped.

Quoted 8 lines

identity_remap_count records how many identity remap items are located in the remap tree for this block group. When relocation is begun for this block group, this is set to the number of holes in the free-space tree for this range. As identity remaps are converted into actual remaps by the relocation process, this number is decreased. Once it reaches 0, either because of relocation or because extents have been deleted, the block group has been fully remapped and its chunk's device extents are removed.

Can we add those two items into a new item other than a completely new v2 block group item?

Quoted 8 lines

I mean for regular block groups they do not need those members, and all block groups starts from regular ones at mkfs time.

We can add a regular block group flag to indicate if the bg has the extra members.

Thanks,
Qu

I did consider that, but the downside of doing that is that it makes the timing of updating the block group tree less predictable. It would imply that when relocating a block group we have to lock the BGT up to the root, as we'd be changing keys and the length of items rather than doing everything in place.
This didn't seem worth it to save a few bytes, particularly as it's anticipated that in practice most block groups will have the REMAPPED flag set.

Mark

Quoted 347 lines
Quoted 346 lines

---
  fs/btrfs/accessors.h            |  20 +++++++ fs/btrfs/block-group.c          | 101 ++++++++++++++++++++++++-------- fs/btrfs/block-group.h          |  14 ++++- fs/btrfs/tree-checker.c         |  10 +++- include/uapi/linux/btrfs_tree.h |   8 +++

  5 files changed, 126 insertions(+), 27 deletions(-)
fs/btrfs/accessors.h +14 -0
index 5f5eda8d6f9e..6e6dd664217b 100644
@@ -264,6 +264,26 @@ BTRFS_SETGET_FUNCS(block_group_flags, struct 
 btrfs_block_group_item, flags, 64);
   BTRFS_SETGET_STACK_FUNCS(stack_block_group_flags,
               struct btrfs_block_group_item, flags, 64);
+/* struct btrfs_block_group_item_v2 */
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_used, struct 
 btrfs_block_group_item_v2,
+             used, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_used, struct 
 btrfs_block_group_item_v2, used, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_chunk_objectid,
+             struct btrfs_block_group_item_v2, chunk_objectid, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_chunk_objectid,
+           struct btrfs_block_group_item_v2, chunk_objectid, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_flags,
+             struct btrfs_block_group_item_v2, flags, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_flags, struct 
 btrfs_block_group_item_v2, flags, 64);
+BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_remap_bytes,
+             struct btrfs_block_group_item_v2, remap_bytes, 64);
+BTRFS_SETGET_FUNCS(block_group_v2_remap_bytes, struct 

btrfs_block_group_item_v2,
+           remap_bytes, 64); +BTRFS_SETGET_STACK_FUNCS(stack_block_group_v2_identity_remap_count, +             struct btrfs_block_group_item_v2, identity_remap_count, 32); +BTRFS_SETGET_FUNCS(block_group_v2_identity_remap_count, struct btrfs_block_group_item_v2,
+           identity_remap_count, 32);
+
  /* struct btrfs_free_space_info */ BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
             extent_count, 32);

fs/btrfs/block-group.c +10 -5
index 5b0cb04b2b93..6a2aa792ccb2 100644
@@ -2351,7 +2351,7 @@ static int 
 check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
   }
   static int read_one_block_group(struct btrfs_fs_info *info,
-                struct btrfs_block_group_item *bgi,
+                struct btrfs_block_group_item_v2 *bgi,
                   const struct btrfs_key *key,
                   int need_clear)
   {
@@ -2366,11 +2366,16 @@ static int read_one_block_group(struct 
 btrfs_fs_info *info,
           return -ENOMEM;
       cache->length = key->offset;
-    cache->used = btrfs_stack_block_group_used(bgi);
+    cache->used = btrfs_stack_block_group_v2_used(bgi);
       cache->commit_used = cache->used;
-    cache->flags = btrfs_stack_block_group_flags(bgi);
-    cache->global_root_id = btrfs_stack_block_group_chunk_objectid(bgi);
+    cache->flags = btrfs_stack_block_group_v2_flags(bgi);
+    cache->global_root_id = 
 btrfs_stack_block_group_v2_chunk_objectid(bgi);
       cache->space_info = btrfs_find_space_info(info, cache->flags);
+    cache->remap_bytes = btrfs_stack_block_group_v2_remap_bytes(bgi);
+    cache->commit_remap_bytes = cache->remap_bytes;
+    cache->identity_remap_count =
+        btrfs_stack_block_group_v2_identity_remap_count(bgi);
+    cache->commit_identity_remap_count = cache->identity_remap_count;
       set_free_space_tree_thresholds(cache);
@@ -2435,7 +2440,7 @@ static int read_one_block_group(struct 
 btrfs_fs_info *info,
       } else if (cache->length == cache->used) {
           cache->cached = BTRFS_CACHE_FINISHED;
           btrfs_free_excluded_extents(cache);
-    } else if (cache->used == 0) {
+    } else if (cache->used == 0 && cache->remap_bytes == 0) {
           cache->cached = BTRFS_CACHE_FINISHED;
           ret = btrfs_add_new_free_space(cache, cache->start,

                             cache->start + cache->length, NULL);

fs/btrfs/block-group.c +13 -3
@@ -2455,7 +2460,8 @@ static int read_one_block_group(struct 
 btrfs_fs_info *info,
       set_avail_alloc_bits(info, cache->flags);
       if (btrfs_chunk_writeable(info, cache->start)) {
-        if (cache->used == 0) {
+        if (cache->used == 0 && cache->identity_remap_count == 0 &&
+            cache->remap_bytes == 0) {
               ASSERT(list_empty(&cache->bg_list));
               if (btrfs_test_opt(info, DISCARD_ASYNC))
                   btrfs_discard_queue_work(&info->discard_ctl, cache);
@@ -2559,9 +2565,10 @@ int btrfs_read_block_groups(struct 
 btrfs_fs_info *info)
           need_clear = 1;
       while (1) {
-        struct btrfs_block_group_item bgi;
+        struct btrfs_block_group_item_v2 bgi;
           struct extent_buffer *leaf;
           int slot;
+        size_t size;
           ret = find_first_block_group(info, path, &key);
           if (ret > 0)
@@ -2572,8 +2579,16 @@ int btrfs_read_block_groups(struct 
 btrfs_fs_info *info)
           leaf = path->nodes[0];
           slot = path->slots[0];
+        if (btrfs_fs_incompat(info, REMAP_TREE)) {
+            size = sizeof(struct btrfs_block_group_item_v2);
+        } else {
+            size = sizeof(struct btrfs_block_group_item);
+            btrfs_set_stack_block_group_v2_remap_bytes(&bgi, 0);
+            btrfs_set_stack_block_group_v2_identity_remap_count(&bgi, 
 0);
+        }
+
           read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, 
 slot),
-                   sizeof(bgi));
+                   size);
           btrfs_item_key_to_cpu(leaf, &key, slot);

          btrfs_release_path(path);

fs/btrfs/block-group.c +24 -9
@@ -2643,25 +2658,38 @@ static int insert_block_group_item(struct 
 btrfs_trans_handle *trans,
                      struct btrfs_block_group *block_group)
   {
       struct btrfs_fs_info *fs_info = trans->fs_info;
-    struct btrfs_block_group_item bgi;
+    struct btrfs_block_group_item_v2 bgi;
       struct btrfs_root *root = btrfs_block_group_root(fs_info);
       struct btrfs_key key;
       u64 old_commit_used;
+    size_t size;
       int ret;
       spin_lock(&block_group->lock);
-    btrfs_set_stack_block_group_used(&bgi, block_group->used);
-    btrfs_set_stack_block_group_chunk_objectid(&bgi,
-                           block_group->global_root_id);
-    btrfs_set_stack_block_group_flags(&bgi, block_group->flags);
+    btrfs_set_stack_block_group_v2_used(&bgi, block_group->used);
+    btrfs_set_stack_block_group_v2_chunk_objectid(&bgi,
+                              block_group->global_root_id);
+    btrfs_set_stack_block_group_v2_flags(&bgi, block_group->flags);
+    btrfs_set_stack_block_group_v2_remap_bytes(&bgi,
+                           block_group->remap_bytes);
+    btrfs_set_stack_block_group_v2_identity_remap_count(&bgi,
+                    block_group->identity_remap_count);
       old_commit_used = block_group->commit_used;
       block_group->commit_used = block_group->used;
+    block_group->commit_remap_bytes = block_group->remap_bytes;
+    block_group->commit_identity_remap_count =
+        block_group->identity_remap_count;
       key.objectid = block_group->start;
       key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
       key.offset = block_group->length;
       spin_unlock(&block_group->lock);
-    ret = btrfs_insert_item(trans, root, &key, &bgi, sizeof(bgi));
+    if (btrfs_fs_incompat(fs_info, REMAP_TREE))
+        size = sizeof(struct btrfs_block_group_item_v2);
+    else
+        size = sizeof(struct btrfs_block_group_item);
+
+    ret = btrfs_insert_item(trans, root, &key, &bgi, size);
       if (ret < 0) {
           spin_lock(&block_group->lock);
           block_group->commit_used = old_commit_used;
@@ -3116,10 +3144,12 @@ static int update_block_group_item(struct 
 btrfs_trans_handle *trans,
       struct btrfs_root *root = btrfs_block_group_root(fs_info);
       unsigned long bi;
       struct extent_buffer *leaf;
-    struct btrfs_block_group_item bgi;
+    struct btrfs_block_group_item_v2 bgi;
       struct btrfs_key key;
-    u64 old_commit_used;
-    u64 used;
+    u64 old_commit_used, old_commit_remap_bytes;
+    u32 old_commit_identity_remap_count;
+    u64 used, remap_bytes;
+    u32 identity_remap_count;
       /*
        * Block group items update can be triggered out of commit 

transaction

fs/btrfs/block-group.c +10 -2
@@ -3129,13 +3159,21 @@ static int update_block_group_item(struct 
 btrfs_trans_handle *trans,
        */
       spin_lock(&cache->lock);
       old_commit_used = cache->commit_used;
+    old_commit_remap_bytes = cache->commit_remap_bytes;
+    old_commit_identity_remap_count = cache- 
 >commit_identity_remap_count;
       used = cache->used;
-    /* No change in used bytes, can safely skip it. */
-    if (cache->commit_used == used) {
+    remap_bytes = cache->remap_bytes;
+    identity_remap_count = cache->identity_remap_count;
+    /* No change in values, can safely skip it. */
+    if (cache->commit_used == used &&
+        cache->commit_remap_bytes == remap_bytes &&
+        cache->commit_identity_remap_count == identity_remap_count) {
           spin_unlock(&cache->lock);
           return 0;
       }
       cache->commit_used = used;
+    cache->commit_remap_bytes = remap_bytes;
+    cache->commit_identity_remap_count = identity_remap_count;
       spin_unlock(&cache->lock);

      key.objectid = cache->start;

fs/btrfs/block-group.c +17 -5
@@ -3151,11 +3189,23 @@ static int update_block_group_item(struct 
 btrfs_trans_handle *trans,
       leaf = path->nodes[0];
       bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
-    btrfs_set_stack_block_group_used(&bgi, used);
-    btrfs_set_stack_block_group_chunk_objectid(&bgi,
-                           cache->global_root_id);
-    btrfs_set_stack_block_group_flags(&bgi, cache->flags);
-    write_extent_buffer(leaf, &bgi, bi, sizeof(bgi));
+    btrfs_set_stack_block_group_v2_used(&bgi, used);
+    btrfs_set_stack_block_group_v2_chunk_objectid(&bgi,
+                              cache->global_root_id);
+    btrfs_set_stack_block_group_v2_flags(&bgi, cache->flags);
+
+    if (btrfs_fs_incompat(fs_info, REMAP_TREE)) {
+        btrfs_set_stack_block_group_v2_remap_bytes(&bgi,
+                               cache->remap_bytes);
+        btrfs_set_stack_block_group_v2_identity_remap_count(&bgi,
+                        cache->identity_remap_count);
+        write_extent_buffer(leaf, &bgi, bi,
+                    sizeof(struct btrfs_block_group_item_v2));
+    } else {
+        write_extent_buffer(leaf, &bgi, bi,
+                    sizeof(struct btrfs_block_group_item));
+    }
+

  fail:
      btrfs_release_path(path);
      /*

fs/btrfs/block-group.c +3 -0
@@ -3170,6 +3220,9 @@ static int update_block_group_item(struct 
 btrfs_trans_handle *trans,
       if (ret < 0 && ret != -ENOENT) {
           spin_lock(&cache->lock);
           cache->commit_used = old_commit_used;
+        cache->commit_remap_bytes = old_commit_remap_bytes;
+        cache->commit_identity_remap_count =
+            old_commit_identity_remap_count;
           spin_unlock(&cache->lock);
       }

      return ret;

fs/btrfs/block-group.h +2 -0
index 9de356bcb411..c484118b8b8d 100644
@@ -127,6 +127,8 @@ struct btrfs_block_group {
       u64 flags;
       u64 cache_generation;
       u64 global_root_id;
+    u64 remap_bytes;
+    u32 identity_remap_count;
       /*
        * The last committed used bytes of this block group, if the 

above @used

fs/btrfs/block-group.h +9 -0
@@ -134,6 +136,15 @@ struct btrfs_block_group {
        * group item of this block group.
        */
       u64 commit_used;
+    /*
+     * The last committed remap_bytes value of this block group.
+     */
+    u64 commit_remap_bytes;
+    /*
+     * The last commited identity_remap_count value of this block group.
+     */
+    u32 commit_identity_remap_count;
+
       /*
        * If the free space extent count exceeds this number, convert 

the block
       * group to bitmaps.

fs/btrfs/block-group.h +2 -1
@@ -275,7 +286,8 @@ static inline bool btrfs_is_block_group_used(const 
 struct btrfs_block_group *bg)
   {
       lockdep_assert_held(&bg->lock);
-    return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0);
+    return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0 ||
+        bg->remap_bytes > 0);
   }

  static inline bool btrfs_is_block_group_data_only(const struct btrfs_block_group *block_group)

fs/btrfs/tree-checker.c +1 -0
index fd83df06e3fb..25311576fab6 100644
@@ -687,6 +687,7 @@ static int check_block_group_item(struct 
 extent_buffer *leaf,
       u64 chunk_objectid;
       u64 flags;
       u64 type;
+    size_t exp_size;
       /*
        * Here we don't really care about alignment since extent 

allocator can

fs/btrfs/tree-checker.c +7 -2
@@ -698,10 +699,15 @@ static int check_block_group_item(struct 
 extent_buffer *leaf,
           return -EUCLEAN;
       }
-    if (unlikely(item_size != sizeof(bgi))) {
+    if (btrfs_fs_incompat(fs_info, REMAP_TREE))
+        exp_size = sizeof(struct btrfs_block_group_item_v2);
+    else
+        exp_size = sizeof(struct btrfs_block_group_item);
+
+    if (unlikely(item_size != exp_size)) {
           block_group_err(leaf, slot,
               "invalid item size, have %u expect %zu",
-                item_size, sizeof(bgi));
+                item_size, exp_size);
           return -EUCLEAN;
       }
include/uapi/linux/ renamed from include/uapi/linux/btrfs_tree.h +0 -0

btrfs_tree.h
index 9a36f0206d90..500e3a7df90b 100644

include/uapi/linux/btrfs_tree.h +8 -0
@@ -1229,6 +1229,14 @@ struct btrfs_block_group_item {
       __le64 flags;
   } __attribute__ ((__packed__));
+struct btrfs_block_group_item_v2 {
+    __le64 used;
+    __le64 chunk_objectid;
+    __le64 flags;
+    __le64 remap_bytes;
+    __le32 identity_remap_count;
+} __attribute__ ((__packed__));
+
   struct btrfs_free_space_info {
       __le32 extent_count;
       __le32 flags;
#9 lore raw

RFC PATCH 5/10 btrfs: allow mounting filesystems with remap-tree incompat flag

In reply to Mark Harmstone

If we encounter a filesystem with the remap-tree incompat flag set, valdiate its compatibility with the other flags, and load the remap tree using the values that have been added to the superblock.

The remap-tree feature depends on the free space tere, but no-holes and block-group-tree have been made dependencies to reduce the testing matrix. Similarly I'm not aware of any reason why mixed-bg and zoned would be incompatible with remap-tree, but this is blocked for the time being until it can be fully tested.

---
 fs/btrfs/Kconfig                |  2 ++
 fs/btrfs/accessors.h            |  6 ++++
 fs/btrfs/disk-io.c              | 60 +++++++++++++++++++++++++++++++++
 fs/btrfs/extent-tree.c          |  2 ++
 fs/btrfs/fs.h                   |  4 ++-
 fs/btrfs/transaction.c          |  7 ++++
 include/uapi/linux/btrfs_tree.h |  5 ++-
 7 files changed, 84 insertions(+), 2 deletions(-)
fs/btrfs/Kconfig +2 -0
index c352f3ae0385..f41446102b14 100644
@@ -114,6 +114,8 @@ config BTRFS_EXPERIMENTAL
 
 	  - extent tree v2 - complex rework of extent tracking
 
+	  - remap-tree - logical address remapping tree
+
 	  If unsure, say N.
 
 config BTRFS_FS_REF_VERIFY
fs/btrfs/accessors.h +6 -0
index 6e6dd664217b..1bb6c0439ba7 100644
@@ -919,6 +919,12 @@ BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
 			 uuid_tree_generation, 64);
 BTRFS_SETGET_STACK_FUNCS(super_nr_global_roots, struct btrfs_super_block,
 			 nr_global_roots, 64);
+BTRFS_SETGET_STACK_FUNCS(super_remap_root, struct btrfs_super_block,
+			 remap_root, 64);
+BTRFS_SETGET_STACK_FUNCS(super_remap_root_generation, struct btrfs_super_block,
+			 remap_root_generation, 64);
+BTRFS_SETGET_STACK_FUNCS(super_remap_root_level, struct btrfs_super_block,
+			 remap_root_level, 8);
 
 /* struct btrfs_file_extent_item */
 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item,
fs/btrfs/disk-io.c +60 -0

108 lines not on this page · raw message

fs/btrfs/extent-tree.c +2 -0
index cb6128778a83..065bd48fcebe 100644
@@ -2562,6 +2562,8 @@ static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
 		flags = BTRFS_BLOCK_GROUP_DATA;
 	else if (root == fs_info->chunk_root)
 		flags = BTRFS_BLOCK_GROUP_SYSTEM;
+	else if (root == fs_info->remap_root)
+		flags = BTRFS_BLOCK_GROUP_REMAP;
 	else
 		flags = BTRFS_BLOCK_GROUP_METADATA;
 
fs/btrfs/fs.h +3 -1
index 2dfdbfda5901..5072a2031631 100644
@@ -286,7 +286,8 @@ enum {
 #define BTRFS_FEATURE_INCOMPAT_SUPP		\
 	(BTRFS_FEATURE_INCOMPAT_SUPP_STABLE |	\
 	 BTRFS_FEATURE_INCOMPAT_RAID_STRIPE_TREE | \
-	 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2)
+	 BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2 | \
+	 BTRFS_FEATURE_INCOMPAT_REMAP_TREE)
 
 #else
 
@@ -434,6 +435,7 @@ struct btrfs_fs_info {
 	struct btrfs_root *data_reloc_root;
 	struct btrfs_root *block_group_root;
 	struct btrfs_root *stripe_root;
+	struct btrfs_root *remap_root;
 
 	/* The log root tree is a directory of all the other log roots */
 	struct btrfs_root *log_root_tree;
fs/btrfs/transaction.c +7 -0
index b96195d6480f..68525e833753 100644
@@ -1951,6 +1951,13 @@ static void update_super_roots(struct btrfs_fs_info *fs_info)
 		super->cache_generation = 0;
 	if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
 		super->uuid_tree_generation = root_item->generation;
+
+	if (btrfs_fs_incompat(fs_info, REMAP_TREE)) {
+		root_item = &fs_info->remap_root->root_item;
+		super->remap_root = root_item->bytenr;
+		super->remap_root_generation = root_item->generation;
+		super->remap_root_level = root_item->level;
+	}
 }
 
 int btrfs_transaction_blocked(struct btrfs_fs_info *info)
include/uapi/linux/btrfs_tree.h +4 -1
index 500e3a7df90b..89bcb80081a6 100644
@@ -721,9 +721,12 @@ struct btrfs_super_block {
 	__u8 metadata_uuid[BTRFS_FSID_SIZE];
 
 	__u64 nr_global_roots;
+	__le64 remap_root;
+	__le64 remap_root_generation;
+	__u8 remap_root_level;
 
 	/* Future expansion */
-	__le64 reserved[27];
+	__u8 reserved[199];
 	__u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
 	struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
 
Signature · 1 line
2.49.0
#10 lore raw

RFC PATCH 6/10 btrfs: redirect I/O for remapped block groups

In reply to Mark Harmstone

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.

---
 fs/btrfs/ctree.c      | 11 ++++---
 fs/btrfs/ctree.h      |  3 ++
 fs/btrfs/relocation.c | 75 +++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/relocation.h |  2 ++
 fs/btrfs/volumes.c    | 19 +++++++++++
 5 files changed, 105 insertions(+), 5 deletions(-)
fs/btrfs/ctree.c +6 -5
index a2e7979372cc..7808f7bc2303 100644
@@ -2331,7 +2331,8 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
  * This may release the path, and so you may lose any locks held at the
  * time you call it.
  */
-static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
+int btrfs_prev_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
+		    struct btrfs_path *path, int ins_len, int cow)
 {
 	struct btrfs_key key;
 	struct btrfs_key orig_key;
@@ -2355,7 +2356,7 @@ static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
 	}
 
 	btrfs_release_path(path);
-	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
 	if (ret <= 0)
 		return ret;
 
@@ -2454,7 +2455,7 @@ int btrfs_search_slot_for_read(struct btrfs_root *root,
 		}
 	} else {
 		if (p->slots[0] == 0) {
-			ret = btrfs_prev_leaf(root, p);
+			ret = btrfs_prev_leaf(NULL, root, p, 0, 0);
 			if (ret < 0)
 				return ret;
 			if (!ret) {
@@ -5003,7 +5004,7 @@ int btrfs_previous_item(struct btrfs_root *root,
 
 	while (1) {
 		if (path->slots[0] == 0) {
-			ret = btrfs_prev_leaf(root, path);
+			ret = btrfs_prev_leaf(NULL, root, path, 0, 0);
 			if (ret != 0)
 				return ret;
 		} else {
@@ -5044,7 +5045,7 @@ int btrfs_previous_extent_item(struct btrfs_root *root,
 
 	while (1) {
 		if (path->slots[0] == 0) {
-			ret = btrfs_prev_leaf(root, path);
+			ret = btrfs_prev_leaf(NULL, root, path, 0, 0);
 			if (ret != 0)
 				return ret;
 		} else {
fs/btrfs/ctree.h +3 -0
index 075a06db43a1..90a0d38a31c9 100644
@@ -721,6 +721,9 @@ static inline int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *pa
 	return btrfs_next_old_leaf(root, path, 0);
 }
 
+int btrfs_prev_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
+		    struct btrfs_path *path, int ins_len, int cow);
+
 static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
 {
 	return btrfs_next_old_item(root, p, 0);
fs/btrfs/relocation.c +75 -0

81 lines not on this page · raw message

fs/btrfs/relocation.h +2 -0
index 788c86d8633a..f07dbd9a89c6 100644
@@ -30,5 +30,7 @@ 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
fs/btrfs/volumes.c +19 -0
index 77194bb46b40..4777926213c0 100644
@@ -6620,6 +6620,25 @@ 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;
Signature · 1 line
2.49.0
#11 lore raw

Re: [RFC PATCH 06/10] btrfs: redirect I/O for remapped block groups

Quoted 2 lines · Mark Harmstone

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.

I'm wondering if we can do it a little simpler:

- Delete the chunk item for a fully relocated/remapped chunk So that future read/write into that logical range will not find a chunk.

- If chunk map lookup failed, search remap tree instead

By this we do not need the REMAPPED flag at all.

Thanks,
Qu

Quoted 211 lines

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.

---
  fs/btrfs/ctree.c      | 11 ++++---
  fs/btrfs/ctree.h      |  3 ++
  fs/btrfs/relocation.c | 75 +++++++++++++++++++++++++++++++++++++++++++
  fs/btrfs/relocation.h |  2 ++
  fs/btrfs/volumes.c    | 19 +++++++++++
  5 files changed, 105 insertions(+), 5 deletions(-)
fs/btrfs/ctree.c +6 -5
index a2e7979372cc..7808f7bc2303 100644
@@ -2331,7 +2331,8 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
   * This may release the path, and so you may lose any locks held at the
   * time you call it.
   */
-static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
+int btrfs_prev_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
+		    struct btrfs_path *path, int ins_len, int cow)
  {
  	struct btrfs_key key;
  	struct btrfs_key orig_key;
@@ -2355,7 +2356,7 @@ static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  	}
 
  	btrfs_release_path(path);
-	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
  	if (ret <= 0)
  		return ret;
 
@@ -2454,7 +2455,7 @@ int btrfs_search_slot_for_read(struct btrfs_root *root,
  		}
  	} else {
  		if (p->slots[0] == 0) {
-			ret = btrfs_prev_leaf(root, p);
+			ret = btrfs_prev_leaf(NULL, root, p, 0, 0);
  			if (ret < 0)
  				return ret;
  			if (!ret) {
@@ -5003,7 +5004,7 @@ int btrfs_previous_item(struct btrfs_root *root,
 
  	while (1) {
  		if (path->slots[0] == 0) {
-			ret = btrfs_prev_leaf(root, path);
+			ret = btrfs_prev_leaf(NULL, root, path, 0, 0);
  			if (ret != 0)
  				return ret;
  		} else {
@@ -5044,7 +5045,7 @@ int btrfs_previous_extent_item(struct btrfs_root *root,
 
  	while (1) {
  		if (path->slots[0] == 0) {
-			ret = btrfs_prev_leaf(root, path);
+			ret = btrfs_prev_leaf(NULL, root, path, 0, 0);
  			if (ret != 0)
  				return ret;
  		} else {
fs/btrfs/ctree.h +3 -0
index 075a06db43a1..90a0d38a31c9 100644
@@ -721,6 +721,9 @@ static inline int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *pa
  	return btrfs_next_old_leaf(root, path, 0);
  }
 
+int btrfs_prev_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
+		    struct btrfs_path *path, int ins_len, int cow);
+
  static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
  {
  	return btrfs_next_old_item(root, p, 0);
fs/btrfs/relocation.c +75 -0
index 02086191630d..e5571c897906 100644
@@ -3897,6 +3897,81 @@ 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 *remap;
+	BTRFS_PATH_AUTO_FREE(path);
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	key.objectid = *logical;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+
+	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] >= btrfs_header_nritems(leaf)) {
+		ret = btrfs_next_leaf(fs_info->remap_root, path);
+		if (ret < 0)
+			return ret;
+
+		leaf = path->nodes[0];
+	}
+
+	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+	if (found_key.objectid > *logical) {
+		if (path->slots[0] == 0) {
+			ret = btrfs_prev_leaf(NULL, fs_info->remap_root, path,
+					      0, 0);
+			if (ret) {
+				if (ret == 1)
+					ret = -ENOENT;
+				return ret;
+			}
+
+			leaf = path->nodes[0];
+		} else {
+			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);
+
+	*logical = *logical - found_key.objectid + btrfs_remap_address(leaf, remap);
+
+	return 0;
+}
+
  /*
   * function to relocate all extents in a block group.
   */
fs/btrfs/relocation.h +2 -0
index 788c86d8633a..f07dbd9a89c6 100644
@@ -30,5 +30,7 @@ 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
fs/btrfs/volumes.c +19 -0
index 77194bb46b40..4777926213c0 100644
@@ -6620,6 +6620,25 @@ 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;
#12 lore raw

Re: [RFC PATCH 06/10] btrfs: redirect I/O for remapped block groups

Quoted 12 lines · Qu Wenruo
Quoted 1 line
Quoted 2 lines · Mark Harmstone

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.

I'm wondering if we can do it a little simpler:

- Delete the chunk item for a fully relocated/remapped chunk
  So that future read/write into that logical range will not find a chunk.

Quoted 6 lines

- If chunk map lookup failed, search remap tree instead

By this we do not need the REMAPPED flag at all.

Thanks,
Qu

You would still need the REMAPPED flag, as that's also set on partially-remapped block groups.

The life cycle is:
* Normal block group
* Block group with REMAPPED flag set and identity remaps covering its data. The REMAPPED flag is an instruction to search the remap tree for this BG, and also means that no new allocations can be made from it
* Block group with a mixture of identity remaps and actual remaps
* Fully-remapped block group, with no chunk stripes and no identity remaps left

My concern with making fully-remapped block groups implicit is that it makes it harder to diagnose corruption. If we see an address that's outside of a block group but has no remap entry, is it a bit-flip error or a bug in the remap tree code?

Mark

Quoted 219 lines
Quoted 218 lines

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.

---
  fs/btrfs/ctree.c      | 11 ++++---
  fs/btrfs/ctree.h      |  3 ++
  fs/btrfs/relocation.c | 75 +++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/relocation.h |  2 ++
  fs/btrfs/volumes.c    | 19 +++++++++++

  5 files changed, 105 insertions(+), 5 deletions(-)
fs/btrfs/ctree.c +2 -1
index a2e7979372cc..7808f7bc2303 100644
@@ -2331,7 +2331,8 @@ int btrfs_search_old_slot(struct btrfs_root 
 *root, const struct btrfs_key *key,
    * This may release the path, and so you may lose any locks held at the
    * time you call it.
    */
-static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path 
 *path)
+int btrfs_prev_leaf(struct btrfs_trans_handle *trans, struct 
 btrfs_root *root,
+            struct btrfs_path *path, int ins_len, int cow)

  {
      struct btrfs_key key;
      struct btrfs_key orig_key;

fs/btrfs/ctree.c +2 -2
@@ -2355,7 +2356,7 @@ static int btrfs_prev_leaf(struct btrfs_root 
 *root, struct btrfs_path *path)
       }
       btrfs_release_path(path);
-    ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+    ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
       if (ret <= 0)
           return ret;
@@ -2454,7 +2455,7 @@ int btrfs_search_slot_for_read(struct btrfs_root 
 *root,
           }
       } else {
           if (p->slots[0] == 0) {
-            ret = btrfs_prev_leaf(root, p);
+            ret = btrfs_prev_leaf(NULL, root, p, 0, 0);
               if (ret < 0)
                   return ret;

              if (!ret) {

fs/btrfs/ctree.c +2 -2
@@ -5003,7 +5004,7 @@ int btrfs_previous_item(struct btrfs_root *root,
       while (1) {
           if (path->slots[0] == 0) {
-            ret = btrfs_prev_leaf(root, path);
+            ret = btrfs_prev_leaf(NULL, root, path, 0, 0);
               if (ret != 0)
                   return ret;
           } else {
@@ -5044,7 +5045,7 @@ int btrfs_previous_extent_item(struct btrfs_root 
 *root,
       while (1) {
           if (path->slots[0] == 0) {
-            ret = btrfs_prev_leaf(root, path);
+            ret = btrfs_prev_leaf(NULL, root, path, 0, 0);
               if (ret != 0)
                   return ret;
           } else {
fs/btrfs/ctree.h +0 -0
index 075a06db43a1..90a0d38a31c9 100644
@@ -721,6 +721,9 @@ static inline int btrfs_next_leaf(struct 

btrfs_root *root, struct btrfs_path *pa
      return btrfs_next_old_leaf(root, path, 0);
  }
+int btrfs_prev_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
+            struct btrfs_path *path, int ins_len, int cow); +
  static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
  {
      return btrfs_next_old_item(root, p, 0);

fs/btrfs/relocation.c +75 -0
index 02086191630d..e5571c897906 100644
@@ -3897,6 +3897,81 @@ 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 *remap;
+    BTRFS_PATH_AUTO_FREE(path);
+
+    path = btrfs_alloc_path();
+    if (!path)
+        return -ENOMEM;
+
+    key.objectid = *logical;
+    key.type = BTRFS_IDENTITY_REMAP_KEY;
+    key.offset = 0;
+
+    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] >= btrfs_header_nritems(leaf)) {
+        ret = btrfs_next_leaf(fs_info->remap_root, path);
+        if (ret < 0)
+            return ret;
+
+        leaf = path->nodes[0];
+    }
+
+    btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+    if (found_key.objectid > *logical) {
+        if (path->slots[0] == 0) {
+            ret = btrfs_prev_leaf(NULL, fs_info->remap_root, path,
+                          0, 0);
+            if (ret) {
+                if (ret == 1)
+                    ret = -ENOENT;
+                return ret;
+            }
+
+            leaf = path->nodes[0];
+        } else {
+            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);
+
+    *logical = *logical - found_key.objectid + 
 btrfs_remap_address(leaf, remap);
+
+    return 0;
+}
+
   /*
    * function to relocate all extents in a block group.

   */

fs/btrfs/relocation.h +2 -0
index 788c86d8633a..f07dbd9a89c6 100644
@@ -30,5 +30,7 @@ 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

fs/btrfs/volumes.c +19 -0
index 77194bb46b40..4777926213c0 100644
@@ -6620,6 +6620,25 @@ 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;
#13 lore raw

RFC PATCH 7/10 btrfs: handle deletions from remapped block group

In reply to Mark Harmstone

Handle the case where we free an extent from a block group that has the REMAPPED flag set. Because the remap tree is orthogonal to the extent tree, for data this may be within any number of identity remaps or actual remaps. If we're freeing a metadata node, this will be wholly inside one or the other.

btrfs_remove_extent_from_remap_tree() searches the remap tree for the remaps that cover the range in question, then calls remove_range_from_remap_tree() for each one, to punch a hole in the remap and adjust the free-space tree.

For an identity remap, remove_range_from_remap_tree() will adjust the block group's identity_remap_count if this changes. If it reaches zero we call last_identity_remap_gone(), which removes the chunk's stripes and device extents - it is now fully remapped.

The changes which involve the block group's ro flag are because the REMAPPED flag itself prevents a block group from having any new allocations within it, and so we don't need to account for this separately.

---
 fs/btrfs/block-group.c |  80 ++++---
 fs/btrfs/block-group.h |   1 +
 fs/btrfs/disk-io.c     |   1 +
 fs/btrfs/extent-tree.c |  30 ++-
 fs/btrfs/fs.h          |   1 +
 fs/btrfs/relocation.c  | 512 +++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/relocation.h  |   3 +
 fs/btrfs/volumes.c     |  56 +++--
 fs/btrfs/volumes.h     |   6 +
 9 files changed, 635 insertions(+), 55 deletions(-)
fs/btrfs/block-group.c +51 -29
index 6a2aa792ccb2..ce5ad7bf8025 100644
@@ -1048,6 +1048,32 @@ static int remove_block_group_item(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+void btrfs_remove_bg_from_sinfo(struct btrfs_block_group *block_group)
+{
+	int factor = btrfs_bg_type_to_factor(block_group->flags);
+
+	spin_lock(&block_group->space_info->lock);
+
+	if (btrfs_test_opt(block_group->fs_info, ENOSPC_DEBUG)) {
+		WARN_ON(block_group->space_info->total_bytes
+			< block_group->length);
+		WARN_ON(block_group->space_info->bytes_readonly
+			< block_group->length - block_group->zone_unusable);
+		WARN_ON(block_group->space_info->bytes_zone_unusable
+			< block_group->zone_unusable);
+		WARN_ON(block_group->space_info->disk_total
+			< block_group->length * factor);
+	}
+	block_group->space_info->total_bytes -= block_group->length;
+	block_group->space_info->bytes_readonly -=
+		(block_group->length - block_group->zone_unusable);
+	btrfs_space_info_update_bytes_zone_unusable(block_group->space_info,
+						    -block_group->zone_unusable);
+	block_group->space_info->disk_total -= block_group->length * factor;
+
+	spin_unlock(&block_group->space_info->lock);
+}
+
 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 			     struct btrfs_chunk_map *map)
 {
@@ -1059,7 +1085,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 	struct kobject *kobj = NULL;
 	int ret;
 	int index;
-	int factor;
 	struct btrfs_caching_control *caching_ctl = NULL;
 	bool remove_map;
 	bool remove_rsv = false;
@@ -1068,7 +1093,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 	if (!block_group)
 		return -ENOENT;
 
-	BUG_ON(!block_group->ro);
+	BUG_ON(!block_group->ro && !(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED));
 
 	trace_btrfs_remove_block_group(block_group);
 	/*
@@ -1080,7 +1105,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 				  block_group->length);
 
 	index = btrfs_bg_flags_to_raid_index(block_group->flags);
-	factor = btrfs_bg_type_to_factor(block_group->flags);
 
 	/* make sure this block group isn't part of an allocation cluster */
 	cluster = &fs_info->data_alloc_cluster;
@@ -1204,26 +1228,11 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 
 	spin_lock(&block_group->space_info->lock);
 	list_del_init(&block_group->ro_list);
-
-	if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
-		WARN_ON(block_group->space_info->total_bytes
-			< block_group->length);
-		WARN_ON(block_group->space_info->bytes_readonly
-			< block_group->length - block_group->zone_unusable);
-		WARN_ON(block_group->space_info->bytes_zone_unusable
-			< block_group->zone_unusable);
-		WARN_ON(block_group->space_info->disk_total
-			< block_group->length * factor);
-	}
-	block_group->space_info->total_bytes -= block_group->length;
-	block_group->space_info->bytes_readonly -=
-		(block_group->length - block_group->zone_unusable);
-	btrfs_space_info_update_bytes_zone_unusable(block_group->space_info,
-						    -block_group->zone_unusable);
-	block_group->space_info->disk_total -= block_group->length * factor;
-
 	spin_unlock(&block_group->space_info->lock);
 
+	if (!(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED))
+		btrfs_remove_bg_from_sinfo(block_group);
+
 	/*
 	 * Remove the free space for the block group from the free space tree
 	 * and the block group's item from the extent tree before marking the
@@ -1508,6 +1517,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 	while (!list_empty(&fs_info->unused_bgs)) {
 		u64 used;
 		int trimming;
+		bool made_ro = false;
 
 		block_group = list_first_entry(&fs_info->unused_bgs,
 					       struct btrfs_block_group,
@@ -1544,7 +1554,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 
 		spin_lock(&space_info->lock);
 		spin_lock(&block_group->lock);
-		if (btrfs_is_block_group_used(block_group) || block_group->ro ||
+		if (btrfs_is_block_group_used(block_group) ||
+		    (block_group->ro && !(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)) ||
 		    list_is_singular(&block_group->list)) {
 			/*
 			 * We want to bail if we made new allocations or have
@@ -1587,7 +1598,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 		 */
 		used = btrfs_space_info_used(space_info, true);
 		if (space_info->total_bytes - block_group->length < used &&
-		    block_group->zone_unusable < block_group->length) {
+		    block_group->zone_unusable < block_group->length &&
+		    !(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)) {
 			/*
 			 * Add a reference for the list, compensate for the ref
 			 * drop under the "next" label for the
@@ -1605,8 +1617,14 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 		spin_unlock(&block_group->lock);
 		spin_unlock(&space_info->lock);
 
-		/* We don't want to force the issue, only flip if it's ok. */
-		ret = inc_block_group_ro(block_group, 0);
+		if (!(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED)) {
+			/* We don't want to force the issue, only flip if it's ok. */
+			ret = inc_block_group_ro(block_group, 0);
+			made_ro = true;
+		} else {
+			ret = 0;
+		}
+
 		up_write(&space_info->groups_sem);
 		if (ret < 0) {
 			ret = 0;
@@ -1615,7 +1633,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 
 		ret = btrfs_zone_finish(block_group);
 		if (ret < 0) {
-			btrfs_dec_block_group_ro(block_group);
+			if (made_ro)
+				btrfs_dec_block_group_ro(block_group);
 			if (ret == -EAGAIN)
 				ret = 0;
 			goto next;
@@ -1628,7 +1647,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 		trans = btrfs_start_trans_remove_block_group(fs_info,
 						     block_group->start);
 		if (IS_ERR(trans)) {
-			btrfs_dec_block_group_ro(block_group);
+			if (made_ro)
+				btrfs_dec_block_group_ro(block_group);
 			ret = PTR_ERR(trans);
 			goto next;
 		}
@@ -1638,7 +1658,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 		 * just delete them, we don't care about them anymore.
 		 */
 		if (!clean_pinned_extents(trans, block_group)) {
-			btrfs_dec_block_group_ro(block_group);
+			if (made_ro)
+				btrfs_dec_block_group_ro(block_group);
 			goto end_trans;
 		}
 
@@ -1652,7 +1673,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 		spin_lock(&fs_info->discard_ctl.lock);
 		if (!list_empty(&block_group->discard_list)) {
 			spin_unlock(&fs_info->discard_ctl.lock);
-			btrfs_dec_block_group_ro(block_group);
+			if (made_ro)
+				btrfs_dec_block_group_ro(block_group);
 			btrfs_discard_queue_work(&fs_info->discard_ctl,
 						 block_group);
 			goto end_trans;
fs/btrfs/block-group.h +1 -0
index c484118b8b8d..767898929960 100644
@@ -329,6 +329,7 @@ int btrfs_add_new_free_space(struct btrfs_block_group *block_group,
 struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
 				struct btrfs_fs_info *fs_info,
 				const u64 chunk_offset);
+void btrfs_remove_bg_from_sinfo(struct btrfs_block_group *block_group);
 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 			     struct btrfs_chunk_map *map);
 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
fs/btrfs/disk-io.c +1 -0
index 68fc6fea221d..55be43bc50d5 100644
@@ -2923,6 +2923,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 	mutex_init(&fs_info->chunk_mutex);
 	mutex_init(&fs_info->transaction_kthread_mutex);
 	mutex_init(&fs_info->cleaner_mutex);
+	mutex_init(&fs_info->remap_mutex);
 	mutex_init(&fs_info->ro_block_group_mutex);
 	init_rwsem(&fs_info->commit_root_sem);
 	init_rwsem(&fs_info->cleanup_work_sem);
fs/btrfs/extent-tree.c +26 -4
index 065bd48fcebe..857a06553b19 100644
@@ -40,6 +40,7 @@
 #include "orphan.h"
 #include "tree-checker.h"
 #include "raid-stripe-tree.h"
+#include "relocation.h"
 
 #undef SCRAMBLE_DELAYED_REFS
 
@@ -2975,6 +2976,8 @@ static int do_free_extent_accounting(struct btrfs_trans_handle *trans,
 				     u64 bytenr, struct btrfs_squota_delta *delta)
 {
 	int ret;
+	struct btrfs_block_group *bg;
+	bool bg_is_remapped = false;
 	u64 num_bytes = delta->num_bytes;
 
 	if (delta->is_data) {
@@ -3000,10 +3003,22 @@ static int do_free_extent_accounting(struct btrfs_trans_handle *trans,
 		return ret;
 	}
 
-	ret = add_to_free_space_tree(trans, bytenr, num_bytes);
-	if (ret) {
-		btrfs_abort_transaction(trans, ret);
-		return ret;
+	if (btrfs_fs_incompat(trans->fs_info, REMAP_TREE)) {
+		bg = btrfs_lookup_block_group(trans->fs_info, bytenr);
+		bg_is_remapped = bg->flags & BTRFS_BLOCK_GROUP_REMAPPED;
+		btrfs_put_block_group(bg);
+	}
+
+	/*
+	 * If remapped, FST has already been taken care of in
+	 * remove_range_from_remap_tree().
+	 */
+	if (!bg_is_remapped) {
+		ret = add_to_free_space_tree(trans, bytenr, num_bytes);
+		if (ret) {
+			btrfs_abort_transaction(trans, ret);
+			return ret;
+		}
 	}
 
 	ret = btrfs_update_block_group(trans, bytenr, num_bytes, false);
@@ -3369,6 +3384,13 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
 		}
 		btrfs_release_path(path);
 
+		ret = btrfs_remove_extent_from_remap_tree(trans, path, bytenr,
+							  num_bytes);
+		if (ret) {
+			btrfs_abort_transaction(trans, ret);
+			goto out;
+		}
+
 		ret = do_free_extent_accounting(trans, bytenr, &delta);
 	}
 	btrfs_release_path(path);
fs/btrfs/fs.h +1 -0
index 5072a2031631..4577768e55c2 100644
@@ -543,6 +543,7 @@ struct btrfs_fs_info {
 	struct mutex transaction_kthread_mutex;
 	struct mutex cleaner_mutex;
 	struct mutex chunk_mutex;
+	struct mutex remap_mutex;
 
 	/*
 	 * This is taken to make sure we don't set block groups ro after the
fs/btrfs/relocation.c +512 -0

527 lines not on this page · raw message

fs/btrfs/relocation.h +3 -0
index f07dbd9a89c6..7f8e27f638bc 100644
@@ -32,5 +32,8 @@ 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);
+int btrfs_remove_extent_from_remap_tree(struct btrfs_trans_handle *trans,
+					struct btrfs_path *path,
+					u64 bytenr, u64 num_bytes);
 
 #endif
fs/btrfs/volumes.c +34 -22

108 lines not on this page · raw message

fs/btrfs/volumes.h +6 -0
index 670d7bf18c40..5bab153926e0 100644
@@ -779,6 +779,8 @@ u64 btrfs_calc_stripe_length(const struct btrfs_chunk_map *map);
 int btrfs_nr_parity_stripes(u64 type);
 int btrfs_chunk_alloc_add_chunk_item(struct btrfs_trans_handle *trans,
 				     struct btrfs_block_group *bg);
+int btrfs_remove_dev_extents(struct btrfs_trans_handle *trans,
+			     struct btrfs_chunk_map *map);
 int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset);
 
 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
@@ -876,6 +878,10 @@ bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
 
 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
 const u8 *btrfs_sb_fsid_ptr(const struct btrfs_super_block *sb);
+int btrfs_update_device(struct btrfs_trans_handle *trans,
+			struct btrfs_device *device);
+void btrfs_chunk_map_device_clear_bits(struct btrfs_chunk_map *map,
+				       unsigned int bits);
 
 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
 struct btrfs_io_context *alloc_btrfs_io_context(struct btrfs_fs_info *fs_info,
Signature · 1 line
2.49.0
#14 lore raw

RFC PATCH 8/10 btrfs: handle setting up relocation of block group with remap-tree

In reply to Mark Harmstone

Handle the preliminary work for relocating a block group in a filesystem with the remap-tree flag set.

If the block group is SYSTEM or REMAP btrfs_relocate_block_group() proceeds as it does already, as bootstrapping issues mean that these block groups have to be processed the existing way.

Otherwise we walk the free-space tree for the block group in question, recording any holes. These get converted into identity remaps and placed in the remap tree, and the block group's REMAPPED flag is set. From now on no new allocations are possible within this block group, and any I/O to it will be funnelled through btrfs_translate_remap(). We store the number of identity remaps in identity_remap_count, so that we know when we've removed the last one and the block group is fully remapped.

The change in btrfs_read_roots() is because data relocations no longer rely on the data reloc tree as a hidden subvolume in which to do snapshots.

---
 fs/btrfs/disk-io.c         |  30 +--
 fs/btrfs/free-space-tree.c |   4 +-
 fs/btrfs/free-space-tree.h |   5 +-
 fs/btrfs/relocation.c      | 436 ++++++++++++++++++++++++++++++++++++-
 fs/btrfs/relocation.h      |   3 +-
 fs/btrfs/space-info.c      |   9 +-
 fs/btrfs/volumes.c         |  15 +-
 7 files changed, 467 insertions(+), 35 deletions(-)
fs/btrfs/disk-io.c +15 -15
index 55be43bc50d5..73e451c32bf1 100644
@@ -2271,22 +2271,22 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
 		root->root_key.objectid = BTRFS_REMAP_TREE_OBJECTID;
 		root->root_key.type = BTRFS_ROOT_ITEM_KEY;
 		root->root_key.offset = 0;
-	}
-
-	/*
-	 * This tree can share blocks with some other fs tree during relocation
-	 * and we need a proper setup by btrfs_get_fs_root
-	 */
-	root = btrfs_get_fs_root(tree_root->fs_info,
-				 BTRFS_DATA_RELOC_TREE_OBJECTID, true);
-	if (IS_ERR(root)) {
-		if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
-			ret = PTR_ERR(root);
-			goto out;
-		}
 	} else {
-		set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
-		fs_info->data_reloc_root = root;
+		/*
+		 * This tree can share blocks with some other fs tree during
+		 * relocation and we need a proper setup by btrfs_get_fs_root
+		 */
+		root = btrfs_get_fs_root(tree_root->fs_info,
+					 BTRFS_DATA_RELOC_TREE_OBJECTID, true);
+		if (IS_ERR(root)) {
+			if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
+				ret = PTR_ERR(root);
+				goto out;
+			}
+		} else {
+			set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
+			fs_info->data_reloc_root = root;
+		}
 	}
 
 	location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
fs/btrfs/free-space-tree.c +1 -3
index 0c573d46639a..85e8f3137973 100644
@@ -21,8 +21,7 @@ static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
 					struct btrfs_block_group *block_group,
 					struct btrfs_path *path);
 
-static struct btrfs_root *btrfs_free_space_root(
-				struct btrfs_block_group *block_group)
+struct btrfs_root *btrfs_free_space_root(struct btrfs_block_group *block_group)
 {
 	struct btrfs_key key = {
 		.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
@@ -96,7 +95,6 @@ static int add_new_free_space_info(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
-EXPORT_FOR_TESTS
 struct btrfs_free_space_info *search_free_space_info(
 		struct btrfs_trans_handle *trans,
 		struct btrfs_block_group *block_group,
fs/btrfs/free-space-tree.h +3 -2
index e6c6d6f4f221..1b804544730a 100644
@@ -35,12 +35,13 @@ int add_to_free_space_tree(struct btrfs_trans_handle *trans,
 			   u64 start, u64 size);
 int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
 				u64 start, u64 size);
-
-#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
 struct btrfs_free_space_info *
 search_free_space_info(struct btrfs_trans_handle *trans,
 		       struct btrfs_block_group *block_group,
 		       struct btrfs_path *path, int cow);
+struct btrfs_root *btrfs_free_space_root(struct btrfs_block_group *block_group);
+
+#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
 int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
 			     struct btrfs_block_group *block_group,
 			     struct btrfs_path *path, u64 start, u64 size);
fs/btrfs/relocation.c +425 -11

504 lines not on this page · raw message

fs/btrfs/relocation.h +2 -1
index 7f8e27f638bc..bfef88b47b0e 100644
@@ -12,7 +12,8 @@ struct btrfs_trans_handle;
 struct btrfs_ordered_extent;
 struct btrfs_pending_snapshot;
 
-int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start);
+int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
+			       bool *using_remap_tree);
 int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, struct btrfs_root *root);
 int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
 			    struct btrfs_root *root);
fs/btrfs/space-info.c +7 -2
index 3f927a514643..e6f6463c8a6d 100644
@@ -375,8 +375,13 @@ void btrfs_add_bg_to_space_info(struct btrfs_fs_info *info,
 	factor = btrfs_bg_type_to_factor(block_group->flags);
 
 	spin_lock(&space_info->lock);
-	space_info->total_bytes += block_group->length;
-	space_info->disk_total += block_group->length * factor;
+
+	if (!(block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED) ||
+	    block_group->identity_remap_count != 0) {
+		space_info->total_bytes += block_group->length;
+		space_info->disk_total += block_group->length * factor;
+	}
+
 	space_info->bytes_used += block_group->used;
 	space_info->disk_used += block_group->used * factor;
 	space_info->bytes_readonly += block_group->bytes_super;
fs/btrfs/volumes.c +14 -1
index ea32ee9a63fd..e59aa0b5c4f3 100644
@@ -3419,6 +3419,7 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
 	struct btrfs_block_group *block_group;
 	u64 length;
 	int ret;
+	bool using_remap_tree;
 
 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
 		btrfs_err(fs_info,
@@ -3442,7 +3443,8 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
 
 	/* step one, relocate all the extents inside this chunk */
 	btrfs_scrub_pause(fs_info);
-	ret = btrfs_relocate_block_group(fs_info, chunk_offset);
+	ret = btrfs_relocate_block_group(fs_info, chunk_offset,
+					 &using_remap_tree);
 	btrfs_scrub_continue(fs_info);
 	if (ret) {
 		/*
@@ -3461,6 +3463,9 @@ int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
 	length = block_group->length;
 	btrfs_put_block_group(block_group);
 
+	if (using_remap_tree)
+		return 0;
+
 	/*
 	 * On a zoned file system, discard the whole block group, this will
 	 * trigger a REQ_OP_ZONE_RESET operation on the device zone. If
@@ -4162,6 +4167,14 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
 		chunk_type = btrfs_chunk_type(leaf, chunk);
 
+		/* Check if chunk has already been fully relocated. */
+		if (chunk_type & BTRFS_BLOCK_GROUP_REMAPPED &&
+		    btrfs_chunk_num_stripes(leaf, chunk) == 0) {
+			btrfs_release_path(path);
+			mutex_unlock(&fs_info->reclaim_bgs_lock);
+			goto loop;
+		}
+
 		if (!counting) {
 			spin_lock(&fs_info->balance_lock);
 			bctl->stat.considered++;
Signature · 1 line
2.49.0
#15 lore raw

RFC PATCH 9/10 btrfs: move existing remaps before relocating block group

In reply to Mark Harmstone

If when relocating a block group we find that remap_bytes > 0 in its block group item, that means that it has been the destination block group for another that has been remapped.

We need to seach the remap tree for any remap backrefs within this range, and move the data to a third block group. This is because otherwise btrfs_translate_remap() could end up following an unbounded chain of remaps, which would only get worse over time.

We only relocate one block group at a time, so remap_bytes will only ever go down while we are doing this. Once we're finished we set the REMAPPED flag on the block group, which will permanently prevent any other data from being moved to within it.

---
 fs/btrfs/extent-tree.c |   6 +-
 fs/btrfs/relocation.c  | 444 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 448 insertions(+), 2 deletions(-)
fs/btrfs/extent-tree.c +4 -2
index 857a06553b19..223904c2a8d8 100644
@@ -4472,7 +4472,8 @@ static noinline int find_free_extent(struct btrfs_root *root,
 		    block_group->cached != BTRFS_CACHE_NO) {
 			down_read(&space_info->groups_sem);
 			if (list_empty(&block_group->list) ||
-			    block_group->ro) {
+			    block_group->ro ||
+			    block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED) {
 				/*
 				 * someone is removing this block group,
 				 * we can't jump into the have_block_group
@@ -4506,7 +4507,8 @@ static noinline int find_free_extent(struct btrfs_root *root,
 
 		ffe_ctl->hinted = false;
 		/* If the block group is read-only, we can skip it entirely. */
-		if (unlikely(block_group->ro)) {
+		if (unlikely(block_group->ro) ||
+		    block_group->flags & BTRFS_BLOCK_GROUP_REMAPPED) {
 			if (ffe_ctl->for_treelog)
 				btrfs_clear_treelog_bg(block_group);
 			if (ffe_ctl->for_data_reloc)
fs/btrfs/relocation.c +444 -0

456 lines not on this page · raw message

Signature · 1 line
2.49.0
#16 lore raw

Re: [RFC PATCH 09/10] btrfs: move existing remaps before relocating block group

kernel test robot noticed the following build errors:

Hi Mark,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on kdave/for-next]
[also build test ERROR on next-20250515]
[cannot apply to linus/master v6.15-rc6]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Mark-Harmstone/btrfs-add-definitions-and-constants-for-remap-tree/20250516-003914
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next
patch link:    https://lore.kernel.org/r/20250515163641.3449017-10-maharmstone%40fb.com
patch subject: [RFC PATCH 09/10] btrfs: move existing remaps before relocating block group
config: i386-buildonly-randconfig-002-20250516 (https://download.01.org/0day-ci/archive/20250516/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250516/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "__umoddi3" [fs/btrfs/btrfs.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#17 lore raw

Re: [RFC PATCH 09/10] btrfs: move existing remaps before relocating block group

Kernel bot is saying here that I'm relying on 64-bit modulo even on 32-bit processors, which is easy enough to sort for the actual patch series.

Quoted 30 lines · kernel test robot
Quoted 1 line

Hi Mark,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on kdave/for-next]
[also build test ERROR on next-20250515]
[cannot apply to linus/master v6.15-rc6]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in

https://urldefense.com/v3/__https://git-scm.com/docs/git-format-patch*_base_tree_information__;Iw!!Bt8RZUm9aw!62MBgtgUBKJiRzdqPxiQTuw-_8GZHkeBKL3gkYvxPzAhENOx0BjCPNqmDBzj50VZzLPMMjlG$ ]
url:    https://github.com/intel-lab-lkp/linux/commits/Mark-Harmstone/btrfs-add-definitions-and-constants-for-remap-tree/20250516-003914
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git  for-next
patch link:    https://lore.kernel.org/r/20250515163641.3449017-10-maharmstone%40fb.com
patch subject: [RFC PATCH 09/10] btrfs: move existing remaps before relocating block group
config: i386-buildonly-randconfig-002-20250516 (https://urldefense.com/v3/__https://download.01.org/0day-ci/archive/20250516/[email protected]/config__;!!Bt8RZUm9aw!62MBgtgUBKJiRzdqPxiQTuw-_8GZHkeBKL3gkYvxPzAhENOx0BjCPNqmDBzj50VZzMhIuygf$ )
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://urldefense.com/v3/__https://download.01.org/0day-ci/archive/20250516/[email protected]/reproduce__;!!Bt8RZUm9aw!62MBgtgUBKJiRzdqPxiQTuw-_8GZHkeBKL3gkYvxPzAhENOx0BjCPNqmDBzj50VZzNUkCx9l$ )

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

Quoted 1 line
Quoted 1 line

ERROR: modpost: "__umoddi3" [fs/btrfs/btrfs.ko] undefined!

#18 lore raw

RFC PATCH 10/10 btrfs: replace identity maps with actual remaps when doing relocations

In reply to Mark Harmstone

Add a function do_remap_tree_reloc(), which does the actual work of doing a relocation using the remap tree.

In a loop we call do_remap_tree_reloc_trans(), which searches for the first identity remap for the block group. We call btrfs_reserve_extent() to find space elsewhere for it, and read the data into memory and write it to the new location. We then carve out the identity remap and replace it with an actual remap, which points to the new location in which to look.

Once the last identity remap has been removed we call last_identity_remap_gone(), which, as with deletions, removes the chunk's stripes and device extents.

---
 fs/btrfs/relocation.c | 522 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 522 insertions(+)
fs/btrfs/relocation.c +522 -0
index 7da95b82c798..bcf04d4c5af1 100644
@@ -4660,6 +4660,60 @@ static int mark_bg_remapped(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+static int find_next_identity_remap(struct btrfs_trans_handle *trans,
+				    struct btrfs_path *path, u64 bg_end,
+				    u64 last_start, u64 *start,
+				    u64 *length)
+{
+	int ret;
+	struct btrfs_key key, found_key;
+	struct btrfs_root *remap_root = trans->fs_info->remap_root;
+	struct extent_buffer *leaf;
+
+	key.objectid = last_start;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+
+	ret = btrfs_search_slot(trans, remap_root, &key, path, 0, 0);
+	if (ret < 0)
+		goto out;
+
+	leaf = path->nodes[0];
+	while (true) {
+		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+		if (found_key.objectid >= bg_end) {
+			ret = -ENOENT;
+			goto out;
+		}
+
+		if (found_key.type == BTRFS_IDENTITY_REMAP_KEY) {
+			*start = found_key.objectid;
+			*length = found_key.offset;
+			ret = 0;
+			goto out;
+		}
+
+		path->slots[0]++;
+		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
+			ret = btrfs_next_leaf(remap_root, path);
+
+			if (ret != 0) {
+				if (ret == 1)
+					ret = -ENOENT;
+				goto out;
+			}
+
+			leaf = path->nodes[0];
+		}
+	}
+
+out:
+	btrfs_release_path(path);
+
+	return ret;
+}
+
 static int remove_chunk_stripes(struct btrfs_trans_handle *trans,
 				struct btrfs_chunk_map *chunk,
 				struct btrfs_path *path)
@@ -4779,6 +4833,288 @@ static int adjust_identity_remap_count(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+static int merge_remap_entries(struct btrfs_trans_handle *trans,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *src_bg, u64 old_addr,
+			       u64 new_addr, u64 length)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_remap *remap_ptr;
+	struct extent_buffer *leaf;
+	struct btrfs_key key, new_key;
+	u64 last_addr, old_length;
+	int ret;
+
+	leaf = path->nodes[0];
+	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+
+	remap_ptr = btrfs_item_ptr(leaf, path->slots[0],
+				   struct btrfs_remap);
+
+	last_addr = btrfs_remap_address(leaf, remap_ptr);
+	old_length = key.offset;
+
+	if (last_addr + old_length != new_addr)
+		return 0;
+
+	/* Merge entries. */
+
+	new_key.objectid = key.objectid;
+	new_key.type = BTRFS_REMAP_KEY;
+	new_key.offset = old_length + length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	/* Merge backref too. */
+
+	key.objectid = new_addr - old_length;
+	key.type = BTRFS_REMAP_BACKREF_KEY;
+	key.offset = old_length;
+
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0) {
+		return ret;
+	} else if (ret == 1) {
+		btrfs_release_path(path);
+		return -ENOENT;
+	}
+
+	new_key.objectid = new_addr - old_length;
+	new_key.type = BTRFS_REMAP_BACKREF_KEY;
+	new_key.offset = old_length + length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	/* Fix the following identity map. */
+
+	key.objectid = old_addr;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	if (key.objectid != old_addr || key.type != BTRFS_IDENTITY_REMAP_KEY)
+		return -ENOENT;
+
+	if (key.offset == length) {
+		ret = btrfs_del_item(trans, fs_info->remap_root, path);
+		if (ret)
+			return ret;
+
+		btrfs_release_path(path);
+
+		ret = adjust_identity_remap_count(trans, path, src_bg, -1);
+		if (ret)
+			return ret;
+
+		return 1;
+	}
+
+	new_key.objectid = old_addr + length;
+	new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+	new_key.offset = key.offset - length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	return 1;
+}
+
+static int add_new_remap_entry(struct btrfs_trans_handle *trans,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *src_bg, u64 old_addr,
+			       u64 new_addr, u64 length)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_key key, new_key;
+	struct btrfs_remap remap;
+	int ret;
+	int identity_count_delta = 0;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	/* Shorten or delete identity mapping entry. */
+
+	if (key.objectid == old_addr) {
+		ret = btrfs_del_item(trans, fs_info->remap_root, path);
+		if (ret)
+			return ret;
+
+		identity_count_delta--;
+	} else {
+		new_key.objectid = key.objectid;
+		new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+		new_key.offset = old_addr - key.objectid;
+
+		btrfs_set_item_key_safe(trans, path, &new_key);
+	}
+
+	btrfs_release_path(path);
+
+	/* Create new remap entry. */
+
+	new_key.objectid = old_addr;
+	new_key.type = BTRFS_REMAP_KEY;
+	new_key.offset = length;
+
+	ret = btrfs_insert_empty_item(trans, fs_info->remap_root,
+		path, &new_key, sizeof(struct btrfs_remap));
+	if (ret)
+		return ret;
+
+	btrfs_set_stack_remap_address(&remap, new_addr);
+
+	write_extent_buffer(path->nodes[0], &remap,
+		btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
+		sizeof(struct btrfs_remap));
+
+	btrfs_release_path(path);
+
+	/* Add entry for remainder of identity mapping, if necessary. */
+
+	if (key.objectid + key.offset != old_addr + length) {
+		new_key.objectid = old_addr + length;
+		new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+		new_key.offset = key.objectid + key.offset - old_addr - length;
+
+		ret = btrfs_insert_empty_item(trans, fs_info->remap_root,
+					      path, &new_key, 0);
+		if (ret)
+			return ret;
+
+		btrfs_release_path(path);
+
+		identity_count_delta++;
+	}
+
+	/* Add backref. */
+
+	new_key.objectid = new_addr;
+	new_key.type = BTRFS_REMAP_BACKREF_KEY;
+	new_key.offset = length;
+
+	ret = btrfs_insert_empty_item(trans, fs_info->remap_root, path,
+				      &new_key, sizeof(struct btrfs_remap));
+	if (ret)
+		return ret;
+
+	btrfs_set_stack_remap_address(&remap, old_addr);
+
+	write_extent_buffer(path->nodes[0], &remap,
+		btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
+		sizeof(struct btrfs_remap));
+
+	btrfs_release_path(path);
+
+	if (identity_count_delta == 0)
+		return 0;
+
+	ret = adjust_identity_remap_count(trans, path, src_bg,
+					  identity_count_delta);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int add_remap_entry(struct btrfs_trans_handle *trans,
+			   struct btrfs_path *path,
+			   struct btrfs_block_group *src_bg, u64 old_addr,
+			   u64 new_addr, u64 length)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_key key;
+	struct extent_buffer *leaf;
+	int ret;
+
+	key.objectid = old_addr;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0)
+		goto end;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	if (key.objectid >= old_addr) {
+		if (path->slots[0] == 0) {
+			ret = btrfs_prev_leaf(trans, fs_info->remap_root, path,
+					      0, 1);
+			if (ret < 0)
+				goto end;
+		} else {
+			path->slots[0]--;
+		}
+	}
+
+	while (true) {
+		leaf = path->nodes[0];
+		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
+			ret = btrfs_next_leaf(fs_info->remap_root, path);
+			if (ret < 0)
+				goto end;
+			else if (ret == 1)
+				break;
+			leaf = path->nodes[0];
+		}
+
+		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+		if (key.objectid >= old_addr + length) {
+			ret = -ENOENT;
+			goto end;
+		}
+
+		if (key.type != BTRFS_REMAP_KEY &&
+		    key.type != BTRFS_IDENTITY_REMAP_KEY) {
+			path->slots[0]++;
+			continue;
+		}
+
+		if (key.type == BTRFS_REMAP_KEY &&
+		    key.objectid + key.offset == old_addr) {
+			ret = merge_remap_entries(trans, path, src_bg, old_addr,
+						  new_addr, length);
+			if (ret < 0) {
+				goto end;
+			} else if (ret == 0) {
+				path->slots[0]++;
+				continue;
+			}
+			break;
+		}
+
+		if (key.objectid <= old_addr &&
+		    key.type == BTRFS_IDENTITY_REMAP_KEY &&
+		    key.objectid + key.offset > old_addr) {
+			ret = add_new_remap_entry(trans, path, src_bg,
+						  old_addr, new_addr, length);
+			if (ret)
+				goto end;
+			break;
+		}
+
+		path->slots[0]++;
+	}
+
+	ret = 0;
+
+end:
+	btrfs_release_path(path);
+
+	return ret;
+}
+
 static int mark_chunk_remapped(struct btrfs_trans_handle *trans,
 			       struct btrfs_path *path, uint64_t start)
 {
@@ -4828,6 +5164,188 @@ static int mark_chunk_remapped(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+static int do_remap_tree_reloc_trans(struct btrfs_fs_info *fs_info,
+				     struct btrfs_block_group *src_bg,
+				     struct btrfs_path *path, u64 *last_start)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_root *extent_root;
+	struct btrfs_key ins;
+	struct btrfs_block_group *dest_bg = NULL;
+	struct btrfs_chunk_map *chunk;
+	u64 start, remap_length, length, new_addr, min_size;
+	int ret;
+	bool no_more = false;
+	bool is_data = src_bg->flags & BTRFS_BLOCK_GROUP_DATA;
+	bool made_reservation = false, bg_needs_free_space;
+	struct btrfs_space_info *sinfo = src_bg->space_info;
+
+	extent_root = btrfs_extent_root(fs_info, src_bg->start);
+
+	trans = btrfs_start_transaction(extent_root, 0);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+
+	mutex_lock(&fs_info->remap_mutex);
+
+	ret = find_next_identity_remap(trans, path, src_bg->start + src_bg->length,
+				       *last_start, &start, &remap_length);
+	if (ret == -ENOENT) {
+		no_more = true;
+		goto next;
+	} else if (ret) {
+		mutex_unlock(&fs_info->remap_mutex);
+		btrfs_end_transaction(trans);
+		return ret;
+	}
+
+	/* Try to reserve enough space for block. */
+
+	spin_lock(&sinfo->lock);
+	btrfs_space_info_update_bytes_may_use(sinfo, remap_length);
+	spin_unlock(&sinfo->lock);
+
+	if (is_data)
+		min_size = fs_info->sectorsize;
+	else
+		min_size = fs_info->nodesize;
+
+	ret = btrfs_reserve_extent(fs_info->fs_root, remap_length,
+				   remap_length, min_size,
+				   0, 0, &ins, is_data, false);
+	if (ret) {
+		spin_lock(&sinfo->lock);
+		btrfs_space_info_update_bytes_may_use(sinfo, -remap_length);
+		spin_unlock(&sinfo->lock);
+
+		mutex_unlock(&fs_info->remap_mutex);
+		btrfs_end_transaction(trans);
+		return ret;
+	}
+
+	made_reservation = true;
+
+	new_addr = ins.objectid;
+	length = ins.offset;
+
+	if (!is_data && length % fs_info->nodesize) {
+		u64 new_length = length - (length % fs_info->nodesize);
+
+		btrfs_free_reserved_extent(fs_info, new_addr + new_length,
+					   length - new_length, 0);
+
+		length = new_length;
+	}
+
+	ret = add_to_free_space_tree(trans, start, length);
+	if (ret)
+		goto fail;
+
+	dest_bg = btrfs_lookup_block_group(fs_info, new_addr);
+
+	mutex_lock(&dest_bg->free_space_lock);
+	bg_needs_free_space = test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE,
+				       &dest_bg->runtime_flags);
+	mutex_unlock(&dest_bg->free_space_lock);
+
+	if (bg_needs_free_space) {
+		ret = add_block_group_free_space(trans, dest_bg);
+		if (ret)
+			goto fail;
+	}
+
+	ret = remove_from_free_space_tree(trans, new_addr, length);
+	if (ret)
+		goto fail;
+
+	ret = do_copy(fs_info, start, new_addr, length);
+	if (ret)
+		goto fail;
+
+	ret = add_remap_entry(trans, path, src_bg, start, new_addr, length);
+	if (ret)
+		goto fail;
+
+	adjust_block_group_remap_bytes(trans, dest_bg, length);
+	btrfs_free_reserved_bytes(dest_bg, length, 0);
+
+	spin_lock(&sinfo->lock);
+	sinfo->bytes_readonly += length;
+	spin_unlock(&sinfo->lock);
+
+next:
+	if (dest_bg)
+		btrfs_put_block_group(dest_bg);
+
+	if (made_reservation)
+		btrfs_dec_block_group_reservations(fs_info, new_addr);
+
+	if (src_bg->used == 0 && src_bg->remap_bytes == 0) {
+		chunk = btrfs_find_chunk_map(fs_info, src_bg->start, 1);
+		if (!chunk) {
+			mutex_unlock(&fs_info->remap_mutex);
+			btrfs_end_transaction(trans);
+			return -ENOENT;
+		}
+
+		ret = last_identity_remap_gone(trans, chunk, src_bg, path);
+		if (ret) {
+			btrfs_free_chunk_map(chunk);
+			mutex_unlock(&fs_info->remap_mutex);
+			btrfs_end_transaction(trans);
+			return ret;
+		}
+
+		btrfs_free_chunk_map(chunk);
+	}
+
+	mutex_unlock(&fs_info->remap_mutex);
+
+	ret = btrfs_end_transaction(trans);
+	if (ret)
+		return ret;
+
+	if (no_more)
+		return 1;
+
+	*last_start = start;
+
+	return 0;
+
+fail:
+	if (dest_bg)
+		btrfs_put_block_group(dest_bg);
+
+	btrfs_free_reserved_extent(fs_info, new_addr, length, 0);
+
+	mutex_unlock(&fs_info->remap_mutex);
+	btrfs_end_transaction(trans);
+
+	return ret;
+}
+
+static int do_remap_tree_reloc(struct btrfs_fs_info *fs_info,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *bg)
+{
+	u64 last_start;
+	int ret;
+
+	last_start = bg->start;
+
+	while (true) {
+		ret = do_remap_tree_reloc_trans(fs_info, bg, path,
+						&last_start);
+		if (ret) {
+			if (ret == 1)
+				ret = 0;
+			break;
+		}
+	}
+
+	return ret;
+}
+
 int btrfs_translate_remap(struct btrfs_fs_info *fs_info, u64 *logical,
 			  u64 *length)
 {
@@ -5073,6 +5591,10 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
 		}
 
 		err = start_block_group_remapping(fs_info, path, bg);
+		if (err)
+			goto out;
+
+		err = do_remap_tree_reloc(fs_info, path, rc->block_group);
 
 		goto out;
 	}
Signature · 1 line
2.49.0
#19 lore raw

Re: [RFC PATCH 10/10] btrfs: replace identity maps with actual remaps when doing relocations

Quoted 10 lines · Mark Harmstone

Add a function do_remap_tree_reloc(), which does the actual work of doing a relocation using the remap tree.

In a loop we call do_remap_tree_reloc_trans(), which searches for the first identity remap for the block group. We call btrfs_reserve_extent() to find space elsewhere for it, and read the data into memory and write it to the new location. We then carve out the identity remap and replace it with an actual remap, which points to the new location in which to look.

Quoted 3 lines

Once the last identity remap has been removed we call last_identity_remap_gone(), which, as with deletions, removes the chunk's stripes and device extents.

I think this is a good candidate for unit testing. Just hammer a bunch of cases adding/removing/merging remaps.

Quoted 11 lines
---
 fs/btrfs/relocation.c | 522 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 522 insertions(+)
fs/btrfs/relocation.c +0 -0
index 7da95b82c798..bcf04d4c5af1 100644

fs/btrfs/relocation.c static int mark_bg_remapped(struct btrfs_trans_handle *trans, · line 4662

Quoted 3 lines
fs/btrfs/relocation.c +0 -0
@@ -4660,6 +4660,60 @@ static int mark_bg_remapped(struct btrfs_trans_handle *trans,
 	return ret;
 }
 

Thinking out loud: I wonder if you do end up re-modeling the transactions s.t. we do one transaction per loop or something, then maybe you can use btrfs_for_each_slot.

Quoted 258 lines
fs/btrfs/relocation.c +251 -0
+static int find_next_identity_remap(struct btrfs_trans_handle *trans,
+				    struct btrfs_path *path, u64 bg_end,
+				    u64 last_start, u64 *start,
+				    u64 *length)
+{
+	int ret;
+	struct btrfs_key key, found_key;
+	struct btrfs_root *remap_root = trans->fs_info->remap_root;
+	struct extent_buffer *leaf;
+
+	key.objectid = last_start;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+
+	ret = btrfs_search_slot(trans, remap_root, &key, path, 0, 0);
+	if (ret < 0)
+		goto out;
+
+	leaf = path->nodes[0];
+	while (true) {
+		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+		if (found_key.objectid >= bg_end) {
+			ret = -ENOENT;
+			goto out;
+		}
+
+		if (found_key.type == BTRFS_IDENTITY_REMAP_KEY) {
+			*start = found_key.objectid;
+			*length = found_key.offset;
+			ret = 0;
+			goto out;
+		}
+
+		path->slots[0]++;
+		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
+			ret = btrfs_next_leaf(remap_root, path);
+
+			if (ret != 0) {
+				if (ret == 1)
+					ret = -ENOENT;
+				goto out;
+			}
+
+			leaf = path->nodes[0];
+		}
+	}
+
+out:
+	btrfs_release_path(path);
+
+	return ret;
+}
+
 static int remove_chunk_stripes(struct btrfs_trans_handle *trans,
 				struct btrfs_chunk_map *chunk,
 				struct btrfs_path *path)
@@ -4779,6 +4833,288 @@ static int adjust_identity_remap_count(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+static int merge_remap_entries(struct btrfs_trans_handle *trans,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *src_bg, u64 old_addr,
+			       u64 new_addr, u64 length)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_remap *remap_ptr;
+	struct extent_buffer *leaf;
+	struct btrfs_key key, new_key;
+	u64 last_addr, old_length;
+	int ret;
+
+	leaf = path->nodes[0];
+	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+
+	remap_ptr = btrfs_item_ptr(leaf, path->slots[0],
+				   struct btrfs_remap);
+
+	last_addr = btrfs_remap_address(leaf, remap_ptr);
+	old_length = key.offset;
+
+	if (last_addr + old_length != new_addr)
+		return 0;
+
+	/* Merge entries. */
+
+	new_key.objectid = key.objectid;
+	new_key.type = BTRFS_REMAP_KEY;
+	new_key.offset = old_length + length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	/* Merge backref too. */
+
+	key.objectid = new_addr - old_length;
+	key.type = BTRFS_REMAP_BACKREF_KEY;
+	key.offset = old_length;
+
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0) {
+		return ret;
+	} else if (ret == 1) {
+		btrfs_release_path(path);
+		return -ENOENT;
+	}
+
+	new_key.objectid = new_addr - old_length;
+	new_key.type = BTRFS_REMAP_BACKREF_KEY;
+	new_key.offset = old_length + length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	/* Fix the following identity map. */
+
+	key.objectid = old_addr;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	if (key.objectid != old_addr || key.type != BTRFS_IDENTITY_REMAP_KEY)
+		return -ENOENT;
+
+	if (key.offset == length) {
+		ret = btrfs_del_item(trans, fs_info->remap_root, path);
+		if (ret)
+			return ret;
+
+		btrfs_release_path(path);
+
+		ret = adjust_identity_remap_count(trans, path, src_bg, -1);
+		if (ret)
+			return ret;
+
+		return 1;
+	}
+
+	new_key.objectid = old_addr + length;
+	new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+	new_key.offset = key.offset - length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	return 1;
+}
+
+static int add_new_remap_entry(struct btrfs_trans_handle *trans,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *src_bg, u64 old_addr,
+			       u64 new_addr, u64 length)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_key key, new_key;
+	struct btrfs_remap remap;
+	int ret;
+	int identity_count_delta = 0;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	/* Shorten or delete identity mapping entry. */
+
+	if (key.objectid == old_addr) {
+		ret = btrfs_del_item(trans, fs_info->remap_root, path);
+		if (ret)
+			return ret;
+
+		identity_count_delta--;
+	} else {
+		new_key.objectid = key.objectid;
+		new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+		new_key.offset = old_addr - key.objectid;
+
+		btrfs_set_item_key_safe(trans, path, &new_key);
+	}
+
+	btrfs_release_path(path);
+
+	/* Create new remap entry. */
+
+	new_key.objectid = old_addr;
+	new_key.type = BTRFS_REMAP_KEY;
+	new_key.offset = length;
+
+	ret = btrfs_insert_empty_item(trans, fs_info->remap_root,
+		path, &new_key, sizeof(struct btrfs_remap));
+	if (ret)
+		return ret;
+
+	btrfs_set_stack_remap_address(&remap, new_addr);
+
+	write_extent_buffer(path->nodes[0], &remap,
+		btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
+		sizeof(struct btrfs_remap));
+
+	btrfs_release_path(path);
+
+	/* Add entry for remainder of identity mapping, if necessary. */
+
+	if (key.objectid + key.offset != old_addr + length) {
+		new_key.objectid = old_addr + length;
+		new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+		new_key.offset = key.objectid + key.offset - old_addr - length;
+
+		ret = btrfs_insert_empty_item(trans, fs_info->remap_root,
+					      path, &new_key, 0);
+		if (ret)
+			return ret;
+
+		btrfs_release_path(path);
+
+		identity_count_delta++;
+	}
+
+	/* Add backref. */
+
+	new_key.objectid = new_addr;
+	new_key.type = BTRFS_REMAP_BACKREF_KEY;
+	new_key.offset = length;
+
+	ret = btrfs_insert_empty_item(trans, fs_info->remap_root, path,
+				      &new_key, sizeof(struct btrfs_remap));
+	if (ret)
+		return ret;
+
+	btrfs_set_stack_remap_address(&remap, old_addr);
+
+	write_extent_buffer(path->nodes[0], &remap,
+		btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
+		sizeof(struct btrfs_remap));
+
+	btrfs_release_path(path);
+
+	if (identity_count_delta == 0)
+		return 0;
+
+	ret = adjust_identity_remap_count(trans, path, src_bg,
+					  identity_count_delta);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int add_remap_entry(struct btrfs_trans_handle *trans,
+			   struct btrfs_path *path,
+			   struct btrfs_block_group *src_bg, u64 old_addr,
+			   u64 new_addr, u64 length)

fs/btrfs/relocation.c static int adjust_identity_remap_count(struct btrfs_trans_handle *trans, · line 5042

Quoted 10 lines
fs/btrfs/relocation.c +10 -0
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_key key;
+	struct extent_buffer *leaf;
+	int ret;
+
+	key.objectid = old_addr;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+

Can this lookup code be shared at all with the remapping logic in the previous patch? It seems fundamentally both are finding a remap entry for a given logical address. Or is it impossible since this one needs cow?

Maybe some kind of prev_item helper that's either remap tree specific or for this use case of going back exactly one item instead of obeying a min_objectid?

Quoted 111 lines
fs/btrfs/relocation.c +104 -0
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0)
+		goto end;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	if (key.objectid >= old_addr) {
+		if (path->slots[0] == 0) {
+			ret = btrfs_prev_leaf(trans, fs_info->remap_root, path,
+					      0, 1);
+			if (ret < 0)
+				goto end;
+		} else {
+			path->slots[0]--;
+		}
+	}
+
+	while (true) {
+		leaf = path->nodes[0];
+		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
+			ret = btrfs_next_leaf(fs_info->remap_root, path);
+			if (ret < 0)
+				goto end;
+			else if (ret == 1)
+				break;
+			leaf = path->nodes[0];
+		}
+
+		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+		if (key.objectid >= old_addr + length) {
+			ret = -ENOENT;
+			goto end;
+		}
+
+		if (key.type != BTRFS_REMAP_KEY &&
+		    key.type != BTRFS_IDENTITY_REMAP_KEY) {
+			path->slots[0]++;
+			continue;
+		}
+
+		if (key.type == BTRFS_REMAP_KEY &&
+		    key.objectid + key.offset == old_addr) {
+			ret = merge_remap_entries(trans, path, src_bg, old_addr,
+						  new_addr, length);
+			if (ret < 0) {
+				goto end;
+			} else if (ret == 0) {
+				path->slots[0]++;
+				continue;
+			}
+			break;
+		}
+
+		if (key.objectid <= old_addr &&
+		    key.type == BTRFS_IDENTITY_REMAP_KEY &&
+		    key.objectid + key.offset > old_addr) {
+			ret = add_new_remap_entry(trans, path, src_bg,
+						  old_addr, new_addr, length);
+			if (ret)
+				goto end;
+			break;
+		}
+
+		path->slots[0]++;
+	}
+
+	ret = 0;
+
+end:
+	btrfs_release_path(path);
+
+	return ret;
+}
+
 static int mark_chunk_remapped(struct btrfs_trans_handle *trans,
 			       struct btrfs_path *path, uint64_t start)
 {
@@ -4828,6 +5164,188 @@ static int mark_chunk_remapped(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
+static int do_remap_tree_reloc_trans(struct btrfs_fs_info *fs_info,
+				     struct btrfs_block_group *src_bg,
+				     struct btrfs_path *path, u64 *last_start)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_root *extent_root;
+	struct btrfs_key ins;
+	struct btrfs_block_group *dest_bg = NULL;
+	struct btrfs_chunk_map *chunk;
+	u64 start, remap_length, length, new_addr, min_size;
+	int ret;
+	bool no_more = false;
+	bool is_data = src_bg->flags & BTRFS_BLOCK_GROUP_DATA;
+	bool made_reservation = false, bg_needs_free_space;
+	struct btrfs_space_info *sinfo = src_bg->space_info;
+
+	extent_root = btrfs_extent_root(fs_info, src_bg->start);
+
+	trans = btrfs_start_transaction(extent_root, 0);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+
+	mutex_lock(&fs_info->remap_mutex);
+
+	ret = find_next_identity_remap(trans, path, src_bg->start + src_bg->length,
+				       *last_start, &start, &remap_length);
+	if (ret == -ENOENT) {
+		no_more = true;
+		goto next;

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5205

Quoted 10 lines
fs/btrfs/relocation.c +10 -0
+	} else if (ret) {
+		mutex_unlock(&fs_info->remap_mutex);
+		btrfs_end_transaction(trans);
+		return ret;
+	}
+
+	/* Try to reserve enough space for block. */
+
+	spin_lock(&sinfo->lock);
+	btrfs_space_info_update_bytes_may_use(sinfo, remap_length);

Why isn't this partly leaked if btrfs_reserve_extent returns a smaller extent than remap_length?

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5215

Quoted 10 lines
fs/btrfs/relocation.c +10 -0
+	spin_unlock(&sinfo->lock);
+
+	if (is_data)
+		min_size = fs_info->sectorsize;
+	else
+		min_size = fs_info->nodesize;
+
+	ret = btrfs_reserve_extent(fs_info->fs_root, remap_length,
+				   remap_length, min_size,
+				   0, 0, &ins, is_data, false);

^ i.e., this will reduce bytes_may_use by the amount it actually reserved, and I don't see anywhere where we make up the difference. Then it looks like we will remap the extent we can, find the next free range and come back to this function and that remaining range to bytes_may_use a second time.

Quoted 7 lines
fs/btrfs/relocation.c +7 -0
+	if (ret) {
+		spin_lock(&sinfo->lock);
+		btrfs_space_info_update_bytes_may_use(sinfo, -remap_length);
+		spin_unlock(&sinfo->lock);
+
+		mutex_unlock(&fs_info->remap_mutex);
+		btrfs_end_transaction(trans);

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5232

Quoted 10 lines
fs/btrfs/relocation.c +10 -0
+		return ret;
+	}
+
+	made_reservation = true;
+
+	new_addr = ins.objectid;
+	length = ins.offset;
+
+	if (!is_data && length % fs_info->nodesize) {
+		u64 new_length = length - (length % fs_info->nodesize);

Why not use the IS_ALIGNED / ALIGN_DOWN macros? Nodesize is a power of two, so I think it should be quicker. Probably doesn't matter, but it does seem to be the predominant pattern in the code base. Also avoids ever worrying about dividing by zero.

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5240

Quoted 8 lines
fs/btrfs/relocation.c +8 -0
+
+		btrfs_free_reserved_extent(fs_info, new_addr + new_length,
+					   length - new_length, 0);
+
+		length = new_length;
+	}
+
+	ret = add_to_free_space_tree(trans, start, length);

Can you explain this? Intuitively, to me, the old remapped address is not a logical range we can allocate from, so it should not be in the free space tree. Is this a hack to get the bytes back into the accounting and allocations are blocked by the remapped block group being remapped / read-only?

Quoted 9 lines
fs/btrfs/relocation.c +9 -0
+	if (ret)
+		goto fail;
+
+	dest_bg = btrfs_lookup_block_group(fs_info, new_addr);
+
+	mutex_lock(&dest_bg->free_space_lock);
+	bg_needs_free_space = test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE,
+				       &dest_bg->runtime_flags);
+	mutex_unlock(&dest_bg->free_space_lock);

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5259

Quoted 10 lines
fs/btrfs/relocation.c +10 -0
+
+	if (bg_needs_free_space) {
+		ret = add_block_group_free_space(trans, dest_bg);
+		if (ret)
+			goto fail;
+	}
+
+	ret = remove_from_free_space_tree(trans, new_addr, length);
+	if (ret)
+		goto fail;

I think you have also discussed this recently with Josef, but it seems a little sketchy. I suppose it depends if the remap tree ends up getting delayed refs and going in the extent tree? I think this is currently only called from alloc_reserved_extent.

Quoted 106 lines
fs/btrfs/relocation.c +93 -0
+
+	ret = do_copy(fs_info, start, new_addr, length);
+	if (ret)
+		goto fail;
+
+	ret = add_remap_entry(trans, path, src_bg, start, new_addr, length);
+	if (ret)
+		goto fail;
+
+	adjust_block_group_remap_bytes(trans, dest_bg, length);
+	btrfs_free_reserved_bytes(dest_bg, length, 0);
+
+	spin_lock(&sinfo->lock);
+	sinfo->bytes_readonly += length;
+	spin_unlock(&sinfo->lock);
+
+next:
+	if (dest_bg)
+		btrfs_put_block_group(dest_bg);
+
+	if (made_reservation)
+		btrfs_dec_block_group_reservations(fs_info, new_addr);
+
+	if (src_bg->used == 0 && src_bg->remap_bytes == 0) {
+		chunk = btrfs_find_chunk_map(fs_info, src_bg->start, 1);
+		if (!chunk) {
+			mutex_unlock(&fs_info->remap_mutex);
+			btrfs_end_transaction(trans);
+			return -ENOENT;
+		}
+
+		ret = last_identity_remap_gone(trans, chunk, src_bg, path);
+		if (ret) {
+			btrfs_free_chunk_map(chunk);
+			mutex_unlock(&fs_info->remap_mutex);
+			btrfs_end_transaction(trans);
+			return ret;
+		}
+
+		btrfs_free_chunk_map(chunk);
+	}
+
+	mutex_unlock(&fs_info->remap_mutex);
+
+	ret = btrfs_end_transaction(trans);
+	if (ret)
+		return ret;
+
+	if (no_more)
+		return 1;
+
+	*last_start = start;
+
+	return 0;
+
+fail:
+	if (dest_bg)
+		btrfs_put_block_group(dest_bg);
+
+	btrfs_free_reserved_extent(fs_info, new_addr, length, 0);
+
+	mutex_unlock(&fs_info->remap_mutex);
+	btrfs_end_transaction(trans);
+
+	return ret;
+}
+
+static int do_remap_tree_reloc(struct btrfs_fs_info *fs_info,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *bg)
+{
+	u64 last_start;
+	int ret;
+
+	last_start = bg->start;
+
+	while (true) {
+		ret = do_remap_tree_reloc_trans(fs_info, bg, path,
+						&last_start);
+		if (ret) {
+			if (ret == 1)
+				ret = 0;
+			break;
+		}
+	}
+
+	return ret;
+}
+
 int btrfs_translate_remap(struct btrfs_fs_info *fs_info, u64 *logical,
 			  u64 *length)
 {
@@ -5073,6 +5591,10 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
 		}
 
 		err = start_block_group_remapping(fs_info, path, bg);
+		if (err)
+			goto out;
+
+		err = do_remap_tree_reloc(fs_info, path, rc->block_group);
 
 		goto out;
 	}
Signature · 1 line
2.49.0
#20 lore raw

Re: [RFC PATCH 10/10] btrfs: replace identity maps with actual remaps when doing relocations

Thanks Boris.

Quoted 12 lines · Boris Burkov
Quoted 1 line
Quoted 10 lines · Mark Harmstone

Add a function do_remap_tree_reloc(), which does the actual work of doing a relocation using the remap tree.

In a loop we call do_remap_tree_reloc_trans(), which searches for the first identity remap for the block group. We call btrfs_reserve_extent() to find space elsewhere for it, and read the data into memory and write it to the new location. We then carve out the identity remap and replace it with an actual remap, which points to the new location in which to look.

Quoted 6 lines
Quoted 3 lines

Once the last identity remap has been removed we call last_identity_remap_gone(), which, as with deletions, removes the chunk's stripes and device extents.

I think this is a good candidate for unit testing. Just hammer a bunch of cases adding/removing/merging remaps.

Yes, makes sense

Quoted 6 lines
Quoted 6 lines
---
  fs/btrfs/relocation.c | 522 ++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 522 insertions(+)
Quoted 12 lines

fs/btrfs/relocation.c static int mark_bg_remapped(struct btrfs_trans_handle *trans, · line 4662

Quoted 8 lines
fs/btrfs/relocation.c +0 -0
index 7da95b82c798..bcf04d4c5af1 100644
@@ -4660,6 +4660,60 @@ static int mark_bg_remapped(struct btrfs_trans_handle *trans,
  	return ret;
  }
 

Thinking out loud: I wonder if you do end up re-modeling the transactions s.t. we do one transaction per loop or something, then maybe you can use btrfs_for_each_slot.

That is what we're doing in do_remap_tree_reloc(). I think the downside of btrfs_for_each_slot is that we're not necessarily removing the whole identity remap every time, as btrfs_reserve_extent() might give us less than we asked for.

Quoted 273 lines
Quoted 258 lines
fs/btrfs/relocation.c +251 -0
+static int find_next_identity_remap(struct btrfs_trans_handle *trans,
+				    struct btrfs_path *path, u64 bg_end,
+				    u64 last_start, u64 *start,
+				    u64 *length)
+{
+	int ret;
+	struct btrfs_key key, found_key;
+	struct btrfs_root *remap_root = trans->fs_info->remap_root;
+	struct extent_buffer *leaf;
+
+	key.objectid = last_start;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+
+	ret = btrfs_search_slot(trans, remap_root, &key, path, 0, 0);
+	if (ret < 0)
+		goto out;
+
+	leaf = path->nodes[0];
+	while (true) {
+		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+		if (found_key.objectid >= bg_end) {
+			ret = -ENOENT;
+			goto out;
+		}
+
+		if (found_key.type == BTRFS_IDENTITY_REMAP_KEY) {
+			*start = found_key.objectid;
+			*length = found_key.offset;
+			ret = 0;
+			goto out;
+		}
+
+		path->slots[0]++;
+		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
+			ret = btrfs_next_leaf(remap_root, path);
+
+			if (ret != 0) {
+				if (ret == 1)
+					ret = -ENOENT;
+				goto out;
+			}
+
+			leaf = path->nodes[0];
+		}
+	}
+
+out:
+	btrfs_release_path(path);
+
+	return ret;
+}
+
  static int remove_chunk_stripes(struct btrfs_trans_handle *trans,
  				struct btrfs_chunk_map *chunk,
  				struct btrfs_path *path)
@@ -4779,6 +4833,288 @@ static int adjust_identity_remap_count(struct btrfs_trans_handle *trans,
  	return ret;
  }
 
+static int merge_remap_entries(struct btrfs_trans_handle *trans,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *src_bg, u64 old_addr,
+			       u64 new_addr, u64 length)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_remap *remap_ptr;
+	struct extent_buffer *leaf;
+	struct btrfs_key key, new_key;
+	u64 last_addr, old_length;
+	int ret;
+
+	leaf = path->nodes[0];
+	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
+
+	remap_ptr = btrfs_item_ptr(leaf, path->slots[0],
+				   struct btrfs_remap);
+
+	last_addr = btrfs_remap_address(leaf, remap_ptr);
+	old_length = key.offset;
+
+	if (last_addr + old_length != new_addr)
+		return 0;
+
+	/* Merge entries. */
+
+	new_key.objectid = key.objectid;
+	new_key.type = BTRFS_REMAP_KEY;
+	new_key.offset = old_length + length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	/* Merge backref too. */
+
+	key.objectid = new_addr - old_length;
+	key.type = BTRFS_REMAP_BACKREF_KEY;
+	key.offset = old_length;
+
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0) {
+		return ret;
+	} else if (ret == 1) {
+		btrfs_release_path(path);
+		return -ENOENT;
+	}
+
+	new_key.objectid = new_addr - old_length;
+	new_key.type = BTRFS_REMAP_BACKREF_KEY;
+	new_key.offset = old_length + length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	/* Fix the following identity map. */
+
+	key.objectid = old_addr;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0)
+		return ret;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	if (key.objectid != old_addr || key.type != BTRFS_IDENTITY_REMAP_KEY)
+		return -ENOENT;
+
+	if (key.offset == length) {
+		ret = btrfs_del_item(trans, fs_info->remap_root, path);
+		if (ret)
+			return ret;
+
+		btrfs_release_path(path);
+
+		ret = adjust_identity_remap_count(trans, path, src_bg, -1);
+		if (ret)
+			return ret;
+
+		return 1;
+	}
+
+	new_key.objectid = old_addr + length;
+	new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+	new_key.offset = key.offset - length;
+
+	btrfs_set_item_key_safe(trans, path, &new_key);
+
+	btrfs_release_path(path);
+
+	return 1;
+}
+
+static int add_new_remap_entry(struct btrfs_trans_handle *trans,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *src_bg, u64 old_addr,
+			       u64 new_addr, u64 length)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_key key, new_key;
+	struct btrfs_remap remap;
+	int ret;
+	int identity_count_delta = 0;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	/* Shorten or delete identity mapping entry. */
+
+	if (key.objectid == old_addr) {
+		ret = btrfs_del_item(trans, fs_info->remap_root, path);
+		if (ret)
+			return ret;
+
+		identity_count_delta--;
+	} else {
+		new_key.objectid = key.objectid;
+		new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+		new_key.offset = old_addr - key.objectid;
+
+		btrfs_set_item_key_safe(trans, path, &new_key);
+	}
+
+	btrfs_release_path(path);
+
+	/* Create new remap entry. */
+
+	new_key.objectid = old_addr;
+	new_key.type = BTRFS_REMAP_KEY;
+	new_key.offset = length;
+
+	ret = btrfs_insert_empty_item(trans, fs_info->remap_root,
+		path, &new_key, sizeof(struct btrfs_remap));
+	if (ret)
+		return ret;
+
+	btrfs_set_stack_remap_address(&remap, new_addr);
+
+	write_extent_buffer(path->nodes[0], &remap,
+		btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
+		sizeof(struct btrfs_remap));
+
+	btrfs_release_path(path);
+
+	/* Add entry for remainder of identity mapping, if necessary. */
+
+	if (key.objectid + key.offset != old_addr + length) {
+		new_key.objectid = old_addr + length;
+		new_key.type = BTRFS_IDENTITY_REMAP_KEY;
+		new_key.offset = key.objectid + key.offset - old_addr - length;
+
+		ret = btrfs_insert_empty_item(trans, fs_info->remap_root,
+					      path, &new_key, 0);
+		if (ret)
+			return ret;
+
+		btrfs_release_path(path);
+
+		identity_count_delta++;
+	}
+
+	/* Add backref. */
+
+	new_key.objectid = new_addr;
+	new_key.type = BTRFS_REMAP_BACKREF_KEY;
+	new_key.offset = length;
+
+	ret = btrfs_insert_empty_item(trans, fs_info->remap_root, path,
+				      &new_key, sizeof(struct btrfs_remap));
+	if (ret)
+		return ret;
+
+	btrfs_set_stack_remap_address(&remap, old_addr);
+
+	write_extent_buffer(path->nodes[0], &remap,
+		btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
+		sizeof(struct btrfs_remap));
+
+	btrfs_release_path(path);
+
+	if (identity_count_delta == 0)
+		return 0;
+
+	ret = adjust_identity_remap_count(trans, path, src_bg,
+					  identity_count_delta);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int add_remap_entry(struct btrfs_trans_handle *trans,
+			   struct btrfs_path *path,
+			   struct btrfs_block_group *src_bg, u64 old_addr,
+			   u64 new_addr, u64 length)

fs/btrfs/relocation.c static int adjust_identity_remap_count(struct btrfs_trans_handle *trans, · line 5042

Quoted 10 lines
fs/btrfs/relocation.c +10 -0
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_key key;
+	struct extent_buffer *leaf;
+	int ret;
+
+	key.objectid = old_addr;
+	key.type = BTRFS_IDENTITY_REMAP_KEY;
+	key.offset = 0;
+

Can this lookup code be shared at all with the remapping logic in the previous patch? It seems fundamentally both are finding a remap entry for a given logical address. Or is it impossible since this one needs cow?

Quoted 3 lines

Maybe some kind of prev_item helper that's either remap tree specific or for this use case of going back exactly one item instead of obeying a min_objectid?

Thanks - yes, there's some overlap between add_remap_entry() and move_existing_remap(), it'd make sense to merge them as much as possible.

Quoted 126 lines
Quoted 112 lines
fs/btrfs/relocation.c +104 -0
+	ret = btrfs_search_slot(trans, fs_info->remap_root, &key, path, 0, 1);
+	if (ret < 0)
+		goto end;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+	if (key.objectid >= old_addr) {
+		if (path->slots[0] == 0) {
+			ret = btrfs_prev_leaf(trans, fs_info->remap_root, path,
+					      0, 1);
+			if (ret < 0)
+				goto end;
+		} else {
+			path->slots[0]--;
+		}
+	}
+
+	while (true) {
+		leaf = path->nodes[0];
+		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
+			ret = btrfs_next_leaf(fs_info->remap_root, path);
+			if (ret < 0)
+				goto end;
+			else if (ret == 1)
+				break;
+			leaf = path->nodes[0];
+		}
+
+		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+		if (key.objectid >= old_addr + length) {
+			ret = -ENOENT;
+			goto end;
+		}
+
+		if (key.type != BTRFS_REMAP_KEY &&
+		    key.type != BTRFS_IDENTITY_REMAP_KEY) {
+			path->slots[0]++;
+			continue;
+		}
+
+		if (key.type == BTRFS_REMAP_KEY &&
+		    key.objectid + key.offset == old_addr) {
+			ret = merge_remap_entries(trans, path, src_bg, old_addr,
+						  new_addr, length);
+			if (ret < 0) {
+				goto end;
+			} else if (ret == 0) {
+				path->slots[0]++;
+				continue;
+			}
+			break;
+		}
+
+		if (key.objectid <= old_addr &&
+		    key.type == BTRFS_IDENTITY_REMAP_KEY &&
+		    key.objectid + key.offset > old_addr) {
+			ret = add_new_remap_entry(trans, path, src_bg,
+						  old_addr, new_addr, length);
+			if (ret)
+				goto end;
+			break;
+		}
+
+		path->slots[0]++;
+	}
+
+	ret = 0;
+
+end:
+	btrfs_release_path(path);
+
+	return ret;
+}
+
  static int mark_chunk_remapped(struct btrfs_trans_handle *trans,
  			       struct btrfs_path *path, uint64_t start)
  {
@@ -4828,6 +5164,188 @@ static int mark_chunk_remapped(struct btrfs_trans_handle *trans,
  	return ret;
  }
 
+static int do_remap_tree_reloc_trans(struct btrfs_fs_info *fs_info,
+				     struct btrfs_block_group *src_bg,
+				     struct btrfs_path *path, u64 *last_start)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_root *extent_root;
+	struct btrfs_key ins;
+	struct btrfs_block_group *dest_bg = NULL;
+	struct btrfs_chunk_map *chunk;
+	u64 start, remap_length, length, new_addr, min_size;
+	int ret;
+	bool no_more = false;
+	bool is_data = src_bg->flags & BTRFS_BLOCK_GROUP_DATA;
+	bool made_reservation = false, bg_needs_free_space;
+	struct btrfs_space_info *sinfo = src_bg->space_info;
+
+	extent_root = btrfs_extent_root(fs_info, src_bg->start);
+
+	trans = btrfs_start_transaction(extent_root, 0);
+	if (IS_ERR(trans))
+		return PTR_ERR(trans);
+
+	mutex_lock(&fs_info->remap_mutex);
+
+	ret = find_next_identity_remap(trans, path, src_bg->start + src_bg->length,
+				       *last_start, &start, &remap_length);
+	if (ret == -ENOENT) {
+		no_more = true;
+		goto next;

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5205

Quoted 10 lines
fs/btrfs/relocation.c +10 -0
+	} else if (ret) {
+		mutex_unlock(&fs_info->remap_mutex);
+		btrfs_end_transaction(trans);
+		return ret;
+	}
+
+	/* Try to reserve enough space for block. */
+
+	spin_lock(&sinfo->lock);
+	btrfs_space_info_update_bytes_may_use(sinfo, remap_length);

Why isn't this partly leaked if btrfs_reserve_extent returns a smaller extent than remap_length?

Quoted 16 lines

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5215

Quoted 10 lines
fs/btrfs/relocation.c +10 -0
+	spin_unlock(&sinfo->lock);
+
+	if (is_data)
+		min_size = fs_info->sectorsize;
+	else
+		min_size = fs_info->nodesize;
+
+	ret = btrfs_reserve_extent(fs_info->fs_root, remap_length,
+				   remap_length, min_size,
+				   0, 0, &ins, is_data, false);

^ i.e., this will reduce bytes_may_use by the amount it actually reserved, and I don't see anywhere where we make up the difference. Then it looks like we will remap the extent we can, find the next free range and come back to this function and that remaining range to bytes_may_use a second time.

As I said to you off-list, this tripped me up! btrfs_reserve_extent() reverses the whole bytes_may_use value, even if it hands out less than that.

Quoted 15 lines
Quoted 15 lines
fs/btrfs/relocation.c +14 -0
+	if (ret) {
+		spin_lock(&sinfo->lock);
+		btrfs_space_info_update_bytes_may_use(sinfo, -remap_length);
+		spin_unlock(&sinfo->lock);
+
+		mutex_unlock(&fs_info->remap_mutex);
+		btrfs_end_transaction(trans);
+		return ret;
+	}
+
+	made_reservation = true;
+
+	new_addr = ins.objectid;
+	length = ins.offset;
Quoted 8 lines

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5232

Quoted 3 lines
fs/btrfs/relocation.c +3 -0
+
+	if (!is_data && length % fs_info->nodesize) {
+		u64 new_length = length - (length % fs_info->nodesize);

Why not use the IS_ALIGNED / ALIGN_DOWN macros? Nodesize is a power of two, so I think it should be quicker. Probably doesn't matter, but it does seem to be the predominant pattern in the code base. Also avoids ever worrying about dividing by zero.

Makes sense, thank you. I changed this to use & after kernelbot complained that I broke compilation on 32-bit CPUs, which is presumably what the macro is doing.

Quoted 15 lines

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5240

Quoted 9 lines
fs/btrfs/relocation.c +8 -0
+
+		btrfs_free_reserved_extent(fs_info, new_addr + new_length,
+					   length - new_length, 0);
+
+		length = new_length;
+	}
+
+	ret = add_to_free_space_tree(trans, start, length);

Can you explain this? Intuitively, to me, the old remapped address is not a logical range we can allocate from, so it should not be in the free space tree. Is this a hack to get the bytes back into the accounting and allocations are blocked by the remapped block group being remapped / read-only?

Yes, good point - we should be clearing out the FST once a block is marked REMAPPED. I think I'll have to fix the discard code, IIRC it works by walking the FST. Plus btrfs-check as well of course

Quoted 17 lines
Quoted 17 lines
fs/btrfs/relocation.c +16 -0
+	if (ret)
+		goto fail;
+
+	dest_bg = btrfs_lookup_block_group(fs_info, new_addr);
+
+	mutex_lock(&dest_bg->free_space_lock);
+	bg_needs_free_space = test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE,
+				       &dest_bg->runtime_flags);
+	mutex_unlock(&dest_bg->free_space_lock);
+
+	if (bg_needs_free_space) {
+		ret = add_block_group_free_space(trans, dest_bg);
+		if (ret)
+			goto fail;
+	}
+
Quoted 8 lines

fs/btrfs/relocation.c static int mark_chunk_remapped(struct btrfs_trans_handle *trans, · line 5259

Quoted 3 lines
fs/btrfs/relocation.c +3 -0
+	ret = remove_from_free_space_tree(trans, new_addr, length);
+	if (ret)
+		goto fail;

I think you have also discussed this recently with Josef, but it seems a little sketchy. I suppose it depends if the remap tree ends up getting delayed refs and going in the extent tree? I think this is currently only called from alloc_reserved_extent.

Do you mean the bit about BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE? We delay populating the in-memory representation of the FST for a BG until we need it, so we have to force it here. Presumably if we accept that remapped BGs don't have anything in the FST we can remove this code.

Quoted 106 lines
Quoted 106 lines
fs/btrfs/relocation.c +93 -0
+
+	ret = do_copy(fs_info, start, new_addr, length);
+	if (ret)
+		goto fail;
+
+	ret = add_remap_entry(trans, path, src_bg, start, new_addr, length);
+	if (ret)
+		goto fail;
+
+	adjust_block_group_remap_bytes(trans, dest_bg, length);
+	btrfs_free_reserved_bytes(dest_bg, length, 0);
+
+	spin_lock(&sinfo->lock);
+	sinfo->bytes_readonly += length;
+	spin_unlock(&sinfo->lock);
+
+next:
+	if (dest_bg)
+		btrfs_put_block_group(dest_bg);
+
+	if (made_reservation)
+		btrfs_dec_block_group_reservations(fs_info, new_addr);
+
+	if (src_bg->used == 0 && src_bg->remap_bytes == 0) {
+		chunk = btrfs_find_chunk_map(fs_info, src_bg->start, 1);
+		if (!chunk) {
+			mutex_unlock(&fs_info->remap_mutex);
+			btrfs_end_transaction(trans);
+			return -ENOENT;
+		}
+
+		ret = last_identity_remap_gone(trans, chunk, src_bg, path);
+		if (ret) {
+			btrfs_free_chunk_map(chunk);
+			mutex_unlock(&fs_info->remap_mutex);
+			btrfs_end_transaction(trans);
+			return ret;
+		}
+
+		btrfs_free_chunk_map(chunk);
+	}
+
+	mutex_unlock(&fs_info->remap_mutex);
+
+	ret = btrfs_end_transaction(trans);
+	if (ret)
+		return ret;
+
+	if (no_more)
+		return 1;
+
+	*last_start = start;
+
+	return 0;
+
+fail:
+	if (dest_bg)
+		btrfs_put_block_group(dest_bg);
+
+	btrfs_free_reserved_extent(fs_info, new_addr, length, 0);
+
+	mutex_unlock(&fs_info->remap_mutex);
+	btrfs_end_transaction(trans);
+
+	return ret;
+}
+
+static int do_remap_tree_reloc(struct btrfs_fs_info *fs_info,
+			       struct btrfs_path *path,
+			       struct btrfs_block_group *bg)
+{
+	u64 last_start;
+	int ret;
+
+	last_start = bg->start;
+
+	while (true) {
+		ret = do_remap_tree_reloc_trans(fs_info, bg, path,
+						&last_start);
+		if (ret) {
+			if (ret == 1)
+				ret = 0;
+			break;
+		}
+	}
+
+	return ret;
+}
+
  int btrfs_translate_remap(struct btrfs_fs_info *fs_info, u64 *logical,
  			  u64 *length)
  {
@@ -5073,6 +5591,10 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
  		}
 
  		err = start_block_group_remapping(fs_info, path, bg);
+		if (err)
+			goto out;
+
+		err = do_remap_tree_reloc(fs_info, path, rc->block_group);
 
  		goto out;
  	}
Signature · 1 line
2.49.0