public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH RFC 08/10] btrfs-progs: mkfs/sprout: introduce a helper to relocate system chunks
Date: Wed, 20 Apr 2022 08:19:57 +0800	[thread overview]
Message-ID: <840cf2c6421abd49b2fee4bbacfe3478baf159d9.1650413308.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1650413308.git.wqu@suse.com>

In kernel, for seed sprout we always relocate all system chunks.

The reason is a little complex, at mount time, especially for
sys_chunk_array processing, we don't have any idea which device is seed
device.

And all we can access is just all deviecs with the same fsid, not even
knowing if that fsid has any seed device.

Thus kernel choose to relocate all system chunks, and remove the empty
seed system chunks to allow a proper mount.

Here we do the same thing, but since in progs we don't have chunk
relocation ability, here we just CoW every leaf, then all nodes will
also be CoWed, thus the whole chunk tree will be relocated.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 mkfs/sprout.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/mkfs/sprout.c b/mkfs/sprout.c
index 66119bbe975f..38d80d789084 100644
--- a/mkfs/sprout.c
+++ b/mkfs/sprout.c
@@ -187,3 +187,57 @@ error:
 	btrfs_abort_transaction(trans, ret);
 	return ret;
 }
+
+/*
+ * Relocate all chunk tree blocks by CoWing every leaf.
+ *
+ * Kernel and btrfs-progs requires sys_chunk_array to only contain devices from
+ * current fsid.
+ *
+ * As btrfs_stripe only contains devid and dev uuid, no fsid to determine if
+ * the current device is a seed.
+ *
+ * And at sys_chunk_array read time, btrfs doesn't have seed devices setup,
+ * thus if we have a chunk with seed device in it, kernel and progs will
+ * treat it as missing directly.
+ *
+ * So we need this function to relocate all system chunks from seed device,
+ * so later we can cleanup those system chunks.
+ */
+static int sprout_relocate_chunk_tree(struct btrfs_trans_handle *trans)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_root *chunk_root = fs_info->chunk_root;
+	struct btrfs_key key = { 0 };
+	struct btrfs_path path;
+	int ret;
+
+	while (true) {
+		btrfs_init_path(&path);
+		ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
+		if (ret < 0)
+			break;
+		/*
+		 * This is for the first search, we should be at the first item
+		 * of chunk tree. That's expected.
+		 */
+		if (ret > 0) {
+			ASSERT(key.offset == 0 && key.type == 0 &&
+			       key.objectid == 0);
+			ret = 0;
+		}
+
+		ret = btrfs_next_leaf(chunk_root, &path);
+		if (ret < 0)
+			break;
+		if (ret > 0) {
+			ret = 0;
+			break;
+		}
+		/* Save the key for next search */
+		btrfs_item_key_to_cpu(path.nodes[0], &key, 0);
+		btrfs_release_path(&path);
+	}
+	btrfs_release_path(&path);
+	return ret;
+}
-- 
2.35.1


  parent reply	other threads:[~2022-04-20  0:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-20  0:19 [PATCH RFC 00/10] btrfs-progs: mkfs: add --seed option to sprout a seed device at mkfs time Qu Wenruo
2022-04-20  0:19 ` [PATCH RFC 01/10] btrfs-progs: refactor find_free_dev_extent_start() for later expansion Qu Wenruo
2022-04-20  0:19 ` [PATCH RFC 02/10] btrfs-progs: delay chunk and device extent items insertion Qu Wenruo
2022-04-20  0:19 ` [PATCH RFC 03/10] btrfs-progs: mkfs: introduce helper to set seed flag Qu Wenruo
2022-04-20  0:19 ` [PATCH RFC 04/10] btrfs-progs: mkfs: avoid error out if some trees exist Qu Wenruo
2022-04-20  0:19 ` [PATCH RFC 05/10] btrfs-progs: extract btrfs_fs_devices structure allocation into a helper Qu Wenruo
2022-04-20  0:19 ` [PATCH RFC 06/10] btrfs-progs: mkfs/sprout: add a helper to update generation for seed device Qu Wenruo
2022-04-20  0:19 ` [PATCH RFC 07/10] btrfs-progs: mkfs/sprout: introduce helper to force allocating a chunk Qu Wenruo
2022-04-20  0:19 ` Qu Wenruo [this message]
2022-04-20  0:19 ` [PATCH RFC 09/10] btrfs-progs: mkfs/sprout: introduce a helper to remove empty system chunks from seed device Qu Wenruo
2022-04-20  0:19 ` [PATCH RFC 10/10] btrfs-progs: mkfs: add support for seed sprout Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=840cf2c6421abd49b2fee4bbacfe3478baf159d9.1650413308.git.wqu@suse.com \
    --to=wqu@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox