All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.cz, David Sterba <dsterba@suse.com>
Subject: [PATCH v3 17/22] btrfs-progs: convert: Enhance record_file_blocks to handle reserved ranges
Date: Fri, 29 Jan 2016 13:03:27 +0800	[thread overview]
Message-ID: <1454043812-7893-18-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1454043812-7893-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>
Signed-off-by: David Sterba <dsterba@suse.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.7.0




  parent reply	other threads:[~2016-01-29  5:17 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-29  5:03 [PATCH v3 00/22] Btrfs-convert rework to support separate chunk type Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 01/22] btrfs-progs: convert: Introduce functions to read used space Qu Wenruo
2016-04-04 13:35   ` David Sterba
2016-04-05  1:35     ` Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 02/22] btrfs-progs: convert: Introduce new function to remove reserved ranges Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 03/22] btrfs-progs: convert: Introduce function to calculate the available space Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 04/22] btrfs-progs: utils: Introduce new function for convert Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 05/22] btrfs-progs: Introduce function to setup temporary superblock Qu Wenruo
2016-05-28  3:04   ` Liu Bo
2016-05-29 10:52     ` Qu Wenruo
2016-06-02 16:41       ` David Sterba
2016-01-29  5:03 ` [PATCH v3 06/22] btrfs-progs: Introduce function to setup temporary tree root Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 07/22] btrfs-progs: Introduce function to setup temporary chunk root Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 08/22] btrfs-progs: Introduce function to initialize device tree Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 09/22] btrfs-progs: Introduce function to initialize fs tree Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 10/22] btrfs-progs: Introduce function to initialize csum tree Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 11/22] btrfs-progs: Introduce function to setup temporary extent tree Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 12/22] btrfs-progs: Introduce function to create convert data chunks Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 13/22] btrfs-progs: extent-tree: Introduce function to find the first overlap extent Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 14/22] btrfs-progs: extent-tree: Enhance btrfs_record_file_extent Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 15/22] btrfs-progs: convert: Introduce new function to create converted image Qu Wenruo
2016-05-28  3:14   ` Liu Bo
2016-01-29  5:03 ` [PATCH v3 16/22] btrfs-progs: convert: Introduce function to migrate reserved ranges Qu Wenruo
2016-05-28  3:16   ` Liu Bo
2016-05-29 11:07     ` Qu Wenruo
2016-01-29  5:03 ` Qu Wenruo [this message]
2016-01-29  5:03 ` [PATCH v3 18/22] btrfs-progs: convert: Introduce init_btrfs_v2 function Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 19/22] btrfs-progs: Introduce do_convert_v2 function Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 20/22] btrfs-progs: Convert: Add support for rollback new convert behavior Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 21/22] btrfs-progs: convert: Strictly avoid meta or system chunk allocation Qu Wenruo
2016-05-28  3:30   ` Liu Bo
2016-05-29 11:05     ` Qu Wenruo
2016-01-29  5:03 ` [PATCH v3 22/22] btrfs-progs: Cleanup old btrfs-convert Qu Wenruo
2016-02-11 17:37 ` [PATCH v3 00/22] Btrfs-convert rework to support separate chunk type David Sterba

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=1454043812-7893-18-git-send-email-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=dsterba@suse.com \
    --cc=dsterba@suse.cz \
    --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.