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 14/22] btrfs-progs: extent-tree: Enhance btrfs_record_file_extent
Date: Fri, 29 Jan 2016 13:03:24 +0800	[thread overview]
Message-ID: <1454043812-7893-15-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1454043812-7893-1-git-send-email-quwenruo@cn.fujitsu.com>

Btrfs_record_file_extent() has some small problems like:
1) Can't handle overlap extent
2) May create extent larger than BTRFS_MAX_EXTENT_SIZE

So enhance it using previous added facilites.
This is used for later btrfs-convert, as for new convert, we create save
image first, then copy inode.
Which will also cause extent overlap.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.com>
---
 ctree.h       |   1 +
 extent-tree.c | 165 ++++++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 115 insertions(+), 51 deletions(-)

diff --git a/ctree.h b/ctree.h
index 59ea01c..1443746 100644
--- a/ctree.h
+++ b/ctree.h
@@ -577,6 +577,7 @@ struct btrfs_extent_item_v0 {
 
 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r) >> 4) - \
 					sizeof(struct btrfs_item))
+#define BTRFS_MAX_EXTENT_SIZE		(128 * 1024 * 1024)
 
 #define BTRFS_EXTENT_FLAG_DATA		(1ULL << 0)
 #define BTRFS_EXTENT_FLAG_TREE_BLOCK	(1ULL << 1)
diff --git a/extent-tree.c b/extent-tree.c
index 29469ff..93b1945 100644
--- a/extent-tree.c
+++ b/extent-tree.c
@@ -3971,16 +3971,11 @@ next:
 	return 0;
 }
 
-/*
- * Record a file extent. Do all the required works, such as inserting
- * file extent item, inserting extent item and backref item into extent
- * tree and updating block accounting.
- */
-int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
-			      struct btrfs_root *root, u64 objectid,
-			      struct btrfs_inode_item *inode,
-			      u64 file_pos, u64 disk_bytenr,
-			      u64 num_bytes)
+static int __btrfs_record_file_extent(struct btrfs_trans_handle *trans,
+				      struct btrfs_root *root, u64 objectid,
+				      struct btrfs_inode_item *inode,
+				      u64 file_pos, u64 disk_bytenr,
+				      u64 *ret_num_bytes)
 {
 	int ret;
 	struct btrfs_fs_info *info = root->fs_info;
@@ -3988,10 +3983,19 @@ int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
 	struct extent_buffer *leaf;
 	struct btrfs_file_extent_item *fi;
 	struct btrfs_key ins_key;
-	struct btrfs_path path;
+	struct btrfs_path *path;
 	struct btrfs_extent_item *ei;
 	u64 nbytes;
+	u64 extent_num_bytes;
+	u64 extent_bytenr;
+	u64 extent_offset;
+	u64 num_bytes = *ret_num_bytes;
 
+	num_bytes = min_t(u64, num_bytes, BTRFS_MAX_EXTENT_SIZE);
+	/*
+	 * All supported file system should not use its 0 extent.
+	 * As it's for hole
+	 */
 	if (disk_bytenr == 0) {
 		ret = btrfs_insert_file_extent(trans, root, objectid,
 						file_pos, disk_bytenr,
@@ -3999,25 +4003,80 @@ int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
 		return ret;
 	}
 
-	btrfs_init_path(&path);
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	/* First to check extent overlap */
+	ret = btrfs_search_overlap_extent(extent_root, path, disk_bytenr,
+					  num_bytes);
+	if (ret < 0)
+		goto fail;
+	if (ret > 0) {
+		/* Found overlap */
+		u64 cur_start;
+		u64 cur_len;
+
+		__get_extent_size(extent_root, path, &cur_start, &cur_len);
+		/*
+		 * For convert case, this extent should be a subset of
+		 * existing one.
+		 */
+		BUG_ON(disk_bytenr < cur_start);
 
+		extent_bytenr = cur_start;
+		extent_num_bytes = cur_len;
+		extent_offset = disk_bytenr - extent_bytenr;
+	} else {
+		/* No overlap, create new extent */
+		btrfs_release_path(path);
+		ins_key.objectid = disk_bytenr;
+		ins_key.offset = num_bytes;
+		ins_key.type = BTRFS_EXTENT_ITEM_KEY;
+
+		ret = btrfs_insert_empty_item(trans, extent_root, path,
+					      &ins_key, sizeof(*ei));
+		if (ret == 0) {
+			leaf = path->nodes[0];
+			ei = btrfs_item_ptr(leaf, path->slots[0],
+					    struct btrfs_extent_item);
+
+			btrfs_set_extent_refs(leaf, ei, 0);
+			btrfs_set_extent_generation(leaf, ei, 0);
+			btrfs_set_extent_flags(leaf, ei,
+					       BTRFS_EXTENT_FLAG_DATA);
+			btrfs_mark_buffer_dirty(leaf);
+
+			ret = btrfs_update_block_group(trans, root, disk_bytenr,
+						       num_bytes, 1, 0);
+			if (ret)
+				goto fail;
+		} else if (ret != -EEXIST) {
+			goto fail;
+		}
+		btrfs_extent_post_op(trans, extent_root);
+		extent_bytenr = disk_bytenr;
+		extent_num_bytes = num_bytes;
+		extent_offset = 0;
+	}
+	btrfs_release_path(path);
 	ins_key.objectid = objectid;
 	ins_key.offset = file_pos;
 	btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
-	ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
+	ret = btrfs_insert_empty_item(trans, root, path, &ins_key,
 				      sizeof(*fi));
 	if (ret)
 		goto fail;
-	leaf = path.nodes[0];
-	fi = btrfs_item_ptr(leaf, path.slots[0],
+	leaf = path->nodes[0];
+	fi = btrfs_item_ptr(leaf, path->slots[0],
 			    struct btrfs_file_extent_item);
 	btrfs_set_file_extent_generation(leaf, fi, trans->transid);
 	btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
-	btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
-	btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
-	btrfs_set_file_extent_offset(leaf, fi, 0);
+	btrfs_set_file_extent_disk_bytenr(leaf, fi, extent_bytenr);
+	btrfs_set_file_extent_disk_num_bytes(leaf, fi, extent_num_bytes);
+	btrfs_set_file_extent_offset(leaf, fi, extent_offset);
 	btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
-	btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
+	btrfs_set_file_extent_ram_bytes(leaf, fi, extent_num_bytes);
 	btrfs_set_file_extent_compression(leaf, fi, 0);
 	btrfs_set_file_extent_encryption(leaf, fi, 0);
 	btrfs_set_file_extent_other_encoding(leaf, fi, 0);
@@ -4025,43 +4084,47 @@ int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
 
 	nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
 	btrfs_set_stack_inode_nbytes(inode, nbytes);
+	btrfs_release_path(path);
 
-	btrfs_release_path(&path);
-
-	ins_key.objectid = disk_bytenr;
-	ins_key.offset = num_bytes;
-	ins_key.type = BTRFS_EXTENT_ITEM_KEY;
-
-	ret = btrfs_insert_empty_item(trans, extent_root, &path,
-				      &ins_key, sizeof(*ei));
-	if (ret == 0) {
-		leaf = path.nodes[0];
-		ei = btrfs_item_ptr(leaf, path.slots[0],
-				    struct btrfs_extent_item);
-
-		btrfs_set_extent_refs(leaf, ei, 0);
-		btrfs_set_extent_generation(leaf, ei, 0);
-		btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
-
-		btrfs_mark_buffer_dirty(leaf);
-
-		ret = btrfs_update_block_group(trans, root, disk_bytenr,
-					       num_bytes, 1, 0);
-		if (ret)
-			goto fail;
-	} else if (ret != -EEXIST) {
-		goto fail;
-	}
-	btrfs_extent_post_op(trans, extent_root);
-
-	ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
-				   root->root_key.objectid,
-				   objectid, file_pos);
+	ret = btrfs_inc_extent_ref(trans, root, extent_bytenr, extent_num_bytes,
+				   0, root->root_key.objectid, objectid,
+				   file_pos - extent_offset);
 	if (ret)
 		goto fail;
 	ret = 0;
+	*ret_num_bytes = min(extent_num_bytes - extent_offset, num_bytes);
 fail:
-	btrfs_release_path(&path);
+	btrfs_free_path(path);
+	return ret;
+}
+
+/*
+ * Record a file extent. Do all the required works, such as inserting
+ * file extent item, inserting extent item and backref item into extent
+ * tree and updating block accounting.
+ */
+int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
+			      struct btrfs_root *root, u64 objectid,
+			      struct btrfs_inode_item *inode,
+			      u64 file_pos, u64 disk_bytenr,
+			      u64 num_bytes)
+{
+	u64 cur_disk_bytenr = disk_bytenr;
+	u64 cur_file_pos = file_pos;
+	u64 cur_num_bytes = num_bytes;
+	int ret = 0;
+
+	while (num_bytes > 0) {
+		ret = __btrfs_record_file_extent(trans, root, objectid,
+						 inode, cur_file_pos,
+						 cur_disk_bytenr,
+						 &cur_num_bytes);
+		if (ret < 0)
+			break;
+		cur_disk_bytenr += cur_num_bytes;
+		cur_file_pos += cur_num_bytes;
+		num_bytes -= cur_num_bytes;
+	}
 	return ret;
 }
 
-- 
2.7.0




  parent reply	other threads:[~2016-01-29  5:16 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 ` Qu Wenruo [this message]
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 ` [PATCH v3 17/22] btrfs-progs: convert: Enhance record_file_blocks to handle " Qu Wenruo
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-15-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.