linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.cz
Subject: [PATCH 08/16] btrfs-progs: check: Move count_csum_range function to check/common.c
Date: Fri, 19 Jan 2018 13:37:23 +0800	[thread overview]
Message-ID: <20180119053731.10795-9-wqu@suse.com> (raw)
In-Reply-To: <20180119053731.10795-1-wqu@suse.com>

Despite of moving it to check/common.c, also:

1) Add extra comment of the function
2) Change @root paramter to @fs_info
   Since @root is never used, csum_root is picked from fs_info anyway.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 Makefile       |   2 +-
 check/common.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 check/common.h |   3 ++
 check/main.c   |  77 ++-----------------------------------------
 4 files changed, 108 insertions(+), 75 deletions(-)
 create mode 100644 check/common.c

diff --git a/Makefile b/Makefile
index c4e2dc5b68a9..a00a982a18df 100644
--- a/Makefile
+++ b/Makefile
@@ -113,7 +113,7 @@ cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
 	       cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
 	       cmds-property.o cmds-fi-usage.o cmds-inspect-dump-tree.o \
 	       cmds-inspect-dump-super.o cmds-inspect-tree-stats.o cmds-fi-du.o \
-	       mkfs/common.o
+	       mkfs/common.o check/common.o
 libbtrfs_objects = send-stream.o send-utils.o kernel-lib/rbtree.o btrfs-list.o \
 		   kernel-lib/crc32c.o messages.o \
 		   uuid-tree.o utils-lib.o rbtree-utils.o
diff --git a/check/common.c b/check/common.c
new file mode 100644
index 000000000000..ed4f2a40bac2
--- /dev/null
+++ b/check/common.c
@@ -0,0 +1,101 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include "ctree.h"
+#include "internal.h"
+#include "check/common.h"
+
+/*
+ * Search in csum tree to find how many bytes of range [@start, @start + @len)
+ * has the corresponding csum item.
+ *
+ * @start:	range start
+ * @len:	range length
+ * @found:	return value of found csum bytes
+ *		unit is BYTE.
+ */
+int count_csum_range(struct btrfs_fs_info *fs_info, u64 start,
+		     u64 len, u64 *found)
+{
+	struct btrfs_key key;
+	struct btrfs_path path;
+	struct extent_buffer *leaf;
+	int ret;
+	size_t size;
+	*found = 0;
+	u64 csum_end;
+	u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
+
+	btrfs_init_path(&path);
+
+	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
+	key.offset = start;
+	key.type = BTRFS_EXTENT_CSUM_KEY;
+
+	ret = btrfs_search_slot(NULL, fs_info->csum_root,
+				&key, &path, 0, 0);
+	if (ret < 0)
+		goto out;
+	if (ret > 0 && path.slots[0] > 0) {
+		leaf = path.nodes[0];
+		btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
+		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
+		    key.type == BTRFS_EXTENT_CSUM_KEY)
+			path.slots[0]--;
+	}
+
+	while (len > 0) {
+		leaf = path.nodes[0];
+		if (path.slots[0] >= btrfs_header_nritems(leaf)) {
+			ret = btrfs_next_leaf(fs_info->csum_root, &path);
+			if (ret > 0)
+				break;
+			else if (ret < 0)
+				goto out;
+			leaf = path.nodes[0];
+		}
+
+		btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
+		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
+		    key.type != BTRFS_EXTENT_CSUM_KEY)
+			break;
+
+		btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
+		if (key.offset >= start + len)
+			break;
+
+		if (key.offset > start)
+			start = key.offset;
+
+		size = btrfs_item_size_nr(leaf, path.slots[0]);
+		csum_end = key.offset + (size / csum_size) *
+			   fs_info->sectorsize;
+		if (csum_end > start) {
+			size = min(csum_end - start, len);
+			len -= size;
+			start += size;
+			*found += size;
+		}
+
+		path.slots[0]++;
+	}
+out:
+	btrfs_release_path(&path);
+	if (ret < 0)
+		return ret;
+	return 0;
+}
+
diff --git a/check/common.h b/check/common.h
index 77a0ab54166f..cd64798f4804 100644
--- a/check/common.h
+++ b/check/common.h
@@ -80,4 +80,7 @@ static inline int fs_root_objectid(u64 objectid)
 	return is_fstree(objectid);
 }
 
+int count_csum_range(struct btrfs_fs_info *fs_info, u64 start,
+		     u64 len, u64 *found);
+
 #endif
diff --git a/check/main.c b/check/main.c
index 9ecbac8f19c3..b891f4815d30 100644
--- a/check/main.c
+++ b/check/main.c
@@ -1423,78 +1423,6 @@ static int process_inode_extref(struct extent_buffer *eb,
 
 }
 
-static int count_csum_range(struct btrfs_root *root, u64 start,
-			    u64 len, u64 *found)
-{
-	struct btrfs_key key;
-	struct btrfs_path path;
-	struct extent_buffer *leaf;
-	int ret;
-	size_t size;
-	*found = 0;
-	u64 csum_end;
-	u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
-
-	btrfs_init_path(&path);
-
-	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
-	key.offset = start;
-	key.type = BTRFS_EXTENT_CSUM_KEY;
-
-	ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
-				&key, &path, 0, 0);
-	if (ret < 0)
-		goto out;
-	if (ret > 0 && path.slots[0] > 0) {
-		leaf = path.nodes[0];
-		btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
-		if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
-		    key.type == BTRFS_EXTENT_CSUM_KEY)
-			path.slots[0]--;
-	}
-
-	while (len > 0) {
-		leaf = path.nodes[0];
-		if (path.slots[0] >= btrfs_header_nritems(leaf)) {
-			ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
-			if (ret > 0)
-				break;
-			else if (ret < 0)
-				goto out;
-			leaf = path.nodes[0];
-		}
-
-		btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
-		if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
-		    key.type != BTRFS_EXTENT_CSUM_KEY)
-			break;
-
-		btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
-		if (key.offset >= start + len)
-			break;
-
-		if (key.offset > start)
-			start = key.offset;
-
-		size = btrfs_item_size_nr(leaf, path.slots[0]);
-		csum_end = key.offset + (size / csum_size) *
-			   root->fs_info->sectorsize;
-		if (csum_end > start) {
-			size = min(csum_end - start, len);
-			len -= size;
-			start += size;
-			*found += size;
-		}
-
-		path.slots[0]++;
-	}
-out:
-	btrfs_release_path(&path);
-	if (ret < 0)
-		return ret;
-	return 0;
-}
-
 static int process_file_extent(struct btrfs_root *root,
 				struct extent_buffer *eb,
 				int slot, struct btrfs_key *key,
@@ -1574,7 +1502,8 @@ static int process_file_extent(struct btrfs_root *root,
 		else
 			disk_bytenr += extent_offset;
 
-		ret = count_csum_range(root, disk_bytenr, num_bytes, &found);
+		ret = count_csum_range(root->fs_info, disk_bytenr, num_bytes,
+				       &found);
 		if (ret < 0)
 			return ret;
 		if (extent_type == BTRFS_FILE_EXTENT_REG) {
@@ -5508,7 +5437,7 @@ static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey,
 		search_start = disk_bytenr;
 		search_len = disk_num_bytes;
 	}
-	ret = count_csum_range(root, search_start, search_len, &csum_found);
+	ret = count_csum_range(root->fs_info, search_start, search_len, &csum_found);
 	if (csum_found > 0 && nodatasum) {
 		err |= ODD_CSUM_ITEM;
 		error("root %llu EXTENT_DATA[%llu %llu] nodatasum shouldn't have datasum",
-- 
2.15.1


  parent reply	other threads:[~2018-01-19  5:38 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-19  5:37 [PATCH 00/16] btrfs-progs: Split lowmem mode check to its own Qu Wenruo
2018-01-19  5:37 ` [PATCH 01/16] btrfs-progs: Moves cmds-check.c to check/main.c Qu Wenruo
2018-01-19  5:37 ` [PATCH 02/16] btrfs-progs: check: Move original mode definitions to check/original.h Qu Wenruo
2018-01-19  5:37 ` [PATCH 03/16] btrfs-progs: check: Move definitions of lowmem mode to check/lowmem.h Qu Wenruo
2018-01-19  5:37 ` [PATCH 04/16] btrfs-progs: check: Move node_ptr structure to check/common.h Qu Wenruo
2018-01-19  5:52   ` Su Yue
2018-01-19  5:53     ` Qu Wenruo
2018-01-19  5:37 ` [PATCH 05/16] btrfs-progs: check: Export check global variables " Qu Wenruo
2018-01-19  6:55   ` Su Yue
2018-01-19  7:26     ` Qu Wenruo
2018-01-19  5:37 ` [PATCH 06/16] btrfs-progs: check: Move imode_to_type function " Qu Wenruo
2018-01-19  5:37 ` [PATCH 07/16] btrfs-progs: check: Move fs_root_objectid " Qu Wenruo
2018-01-19  5:37 ` Qu Wenruo [this message]
2018-01-19  6:46   ` [PATCH 08/16] btrfs-progs: check: Move count_csum_range function to check/common.c Su Yue
2018-01-19  5:37 ` [PATCH 09/16] btrfs-progs: check: Move __create_inode_item " Qu Wenruo
2018-01-19  5:37 ` [PATCH 10/16] btrfs-progs: check: Move link_inode_to_lostfound function to common.c Qu Wenruo
2018-01-19  5:37 ` [PATCH 11/16] btrfs-progs: check: Move check_dev_size_alignment to check/common.c Qu Wenruo
2018-01-19  5:37 ` [PATCH 12/16] btrfs-progs: check: move reada_walk_down " Qu Wenruo
2018-01-19  5:37 ` [PATCH 13/16] btrfs-progs: check: Move check_child_node " Qu Wenruo
2018-01-19  5:37 ` [PATCH 14/16] btrfs-progs: check: Move reset_cached_block_groups " Qu Wenruo
2018-01-19  5:37 ` [PATCH 16/16] btrfs-progs: check/lowmem: Cleanup unnecessary _v2 suffix Qu Wenruo
2018-01-19  7:50 ` [PATCH 00/16] btrfs-progs: Split lowmem mode check to its own Su Yue
2018-01-31 18:26 ` David Sterba
2018-02-01  5:00   ` 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=20180119053731.10795-9-wqu@suse.com \
    --to=wqu@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).