All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH v2 20/25] btrfs-progs: convert: Enhance record_file_blocks to handle reserved ranges
Date: Tue,  1 Dec 2015 15:11:40 +0800	[thread overview]
Message-ID: <1448953905-28673-21-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1448953905-28673-1-git-send-email-quwenruo@cn.fujitsu.com>

Enhance record_file_blocks() to handle reserved ranges.

Old file system can use the space in btrfs reserved ranges.
So we could not use the bytenr of old filesystem directly.

Thanks to previous patches, we have a full fs image in convert_root, and
it has already relocated the blocks in reserved ranges.
So here we just search the convert_root to get correct disk_bytenr and
use it.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 btrfs-convert.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 94 insertions(+), 9 deletions(-)

diff --git a/btrfs-convert.c b/btrfs-convert.c
index f6126db..8956a86 100644
--- a/btrfs-convert.c
+++ b/btrfs-convert.c
@@ -629,7 +629,9 @@ static int csum_disk_extent(struct btrfs_trans_handle *trans,
 struct blk_iterate_data {
 	struct btrfs_trans_handle *trans;
 	struct btrfs_root *root;
+	struct btrfs_root *convert_root;
 	struct btrfs_inode_item *inode;
+	u64 convert_ino;
 	u64 objectid;
 	u64 first_block;
 	u64 disk_block;
@@ -645,6 +647,8 @@ static void init_blk_iterate_data(struct blk_iterate_data *data,
 				  struct btrfs_inode_item *inode,
 				  u64 objectid, int checksum)
 {
+	struct btrfs_key key;
+
 	data->trans		= trans;
 	data->root		= root;
 	data->inode		= inode;
@@ -655,25 +659,106 @@ static void init_blk_iterate_data(struct blk_iterate_data *data,
 	data->boundary		= (u64)-1;
 	data->checksum		= checksum;
 	data->errcode		= 0;
+
+	key.objectid = CONV_IMAGE_SUBVOL_OBJECTID;
+	key.type = BTRFS_ROOT_ITEM_KEY;
+	key.offset = (u64)-1;
+	data->convert_root = btrfs_read_fs_root(root->fs_info, &key);
+	/* Impossible as we just opened it before */
+	BUG_ON(!data->convert_root || IS_ERR(data->convert_root));
+	data->convert_ino = BTRFS_FIRST_FREE_OBJECTID + 1;
 }
 
+/*
+ * Record a file extent in original file system into btrfs one.
+ * The special point is, old disk_block can point to a reserved range.
+ * So here, we don't use disk_block directly but search convert_root
+ * to get the real disk_bytenr.
+ */
 static int record_file_blocks(struct blk_iterate_data *data,
 			      u64 file_block, u64 disk_block, u64 num_blocks)
 {
-	int ret;
+	int ret = 0;
 	struct btrfs_root *root = data->root;
+	struct btrfs_root *convert_root = data->convert_root;
+	struct btrfs_path *path;
 	u64 file_pos = file_block * root->sectorsize;
-	u64 disk_bytenr = disk_block * root->sectorsize;
+	u64 old_disk_bytenr = disk_block * root->sectorsize;
 	u64 num_bytes = num_blocks * root->sectorsize;
-	ret = btrfs_record_file_extent(data->trans, data->root,
-				       data->objectid, data->inode, file_pos,
-				       disk_bytenr, num_bytes);
-
-	if (ret || !data->checksum || disk_bytenr == 0)
-		return ret;
+	u64 cur_off = old_disk_bytenr;
 
-	return csum_disk_extent(data->trans, data->root, disk_bytenr,
+	/* Hole, pass it to record_file_extent directly */
+	if (old_disk_bytenr == 0)
+		return btrfs_record_file_extent(data->trans, root,
+				data->objectid, data->inode, file_pos, 0,
 				num_bytes);
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	/*
+	 * Search real disk bytenr from convert root
+	 */
+	while (cur_off < old_disk_bytenr + num_bytes) {
+		struct btrfs_key key;
+		struct btrfs_file_extent_item *fi;
+		struct extent_buffer *node;
+		int slot;
+		u64 extent_disk_bytenr;
+		u64 extent_num_bytes;
+		u64 real_disk_bytenr;
+		u64 cur_len;
+
+		key.objectid = data->convert_ino;
+		key.type = BTRFS_EXTENT_DATA_KEY;
+		key.offset = cur_off;
+
+		ret = btrfs_search_slot(NULL, convert_root, &key, path, 0, 0);
+		if (ret < 0)
+			break;
+		if (ret > 0) {
+			ret = btrfs_previous_item(convert_root, path,
+						  data->convert_ino,
+						  BTRFS_EXTENT_DATA_KEY);
+			if (ret < 0)
+				break;
+			if (ret > 0) {
+				ret = -ENOENT;
+				break;
+			}
+		}
+		node = path->nodes[0];
+		slot = path->slots[0];
+		btrfs_item_key_to_cpu(node, &key, slot);
+		BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY ||
+		       key.objectid != data->convert_ino ||
+		       key.offset > cur_off);
+		fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
+		extent_disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
+		extent_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
+		BUG_ON(cur_off - key.offset >= extent_num_bytes);
+		btrfs_release_path(path);
+
+		real_disk_bytenr = cur_off - key.offset + extent_disk_bytenr;
+		cur_len = min(key.offset + extent_num_bytes,
+			      old_disk_bytenr + num_bytes) - cur_off;
+		ret = btrfs_record_file_extent(data->trans, data->root,
+					data->objectid, data->inode, file_pos,
+					real_disk_bytenr, cur_len);
+		if (ret < 0)
+			break;
+		cur_off += cur_len;
+		file_pos += cur_len;
+
+		/*
+		 * No need to care about csum
+		 * As every byte of old fs image is calculated for csum, no
+		 * need to waste cpu cycle now.
+		 */
+	}
+	btrfs_free_path(path);
+	return ret;
 }
 
 static int block_iterate_proc(u64 disk_block, u64 file_block,
-- 
2.6.2




  parent reply	other threads:[~2015-12-01  7:14 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-01  7:11 [PATCH v2 00/25] Btrfs-convert rework to support separate chunk type Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 01/25] btrfs-progs: extent-cache: Add comments for search/lookup functions Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 02/25] btrfs-progs: extent-tree: Add add_merge_cache_extent function Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 03/25] btrfs-progs: Introduce new members for btrfs_convert_context Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 04/25] btrfs-progs: convert: Introduce functions to read used space Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 05/25] btrfs-progs: convert: Introduce new function to remove reserved ranges Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 06/25] btrfs-progs: convert: Introduce function to calculate the available space Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 07/25] btrfs-progs: utils: Introduce new function for convert Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 08/25] btrfs-progs: Introduce function to setup temporary superblock Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 09/25] btrfs-progs: Introduce function to setup temporary tree root Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 10/25] btrfs-progs: Introduce function to setup temporary chunk root Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 11/25] btrfs-progs: Introduce function to initialize device tree Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 12/25] btrfs-progs: Introduce function to initialize fs tree Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 13/25] btrfs-progs: Introduce function to initialize csum tree Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 14/25] btrfs-progs: Introduce function to setup temporary extent tree Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 15/25] btrfs-progs: Introduce function to create convert data chunks Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 16/25] btrfs-progs: extent-tree: Introduce function to find the first overlap extent Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 17/25] btrfs-progs: extent-tree: Enhance btrfs_record_file_extent Qu Wenruo
2016-01-12 10:17   ` David Sterba
2016-01-13  0:33     ` Qu Wenruo
2016-01-13  8:55       ` David Sterba
2015-12-01  7:11 ` [PATCH v2 18/25] btrfs-progs: convert: Introduce new function to create converted image Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 19/25] btrfs-progs: convert: Introduce function to migrate reserved ranges Qu Wenruo
2015-12-01  7:11 ` Qu Wenruo [this message]
2015-12-01  7:11 ` [PATCH v2 21/25] btrfs-progs: convert: Introduce init_btrfs_v2 function Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 22/25] btrfs-progs: Introduce do_convert_v2 function Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 23/25] btrfs-progs: Convert: Add support for rollback new convert behavior Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 24/25] btrfs-progs: convert: Strictly avoid meta or system chunk allocation Qu Wenruo
2015-12-01  7:11 ` [PATCH v2 25/25] btrfs-progs: Cleanup old btrfs-convert Qu Wenruo
2015-12-07 15:20 ` [PATCH v2 00/25] Btrfs-convert rework to support separate chunk type David Sterba
2015-12-08  1:50   ` 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=1448953905-28673-21-git-send-email-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.