All of lore.kernel.org
 help / color / mirror / Atom feed
From: Su Yue <suy.fnst@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: <quwenruo@cn.fujitsu.com>
Subject: [PATCH 07/20] btrfs-progs: cmds-check.c: introduce print_inode_ref
Date: Wed, 1 Mar 2017 11:13:50 +0800	[thread overview]
Message-ID: <20170301031403.23902-8-suy.fnst@cn.fujitsu.com> (raw)
In-Reply-To: <20170301031403.23902-1-suy.fnst@cn.fujitsu.com>

Introduce 'print_inode_ref' to print error msg while checking inode ref.

Add args 'name_ret' and 'namelen_ret' to 'check_inode_ref' because
they are essential while doing nlinks repair.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 cmds-check.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 74 insertions(+), 19 deletions(-)

diff --git a/cmds-check.c b/cmds-check.c
index c45dfae4..24a39e54 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -4334,34 +4334,76 @@ out:
 }
 
 /*
+ * Print inode ref error message
+ */
+static void print_inode_ref_err(struct btrfs_root *root, struct btrfs_key *key,
+				u64 index, const char *namebuf, int name_len,
+				u8 filetype, int err)
+{
+	if (!err)
+		return;
+
+	/*root dir error */
+	if (key->objectid == BTRFS_FIRST_FREE_OBJECTID) {
+		error("root %llu root dir shouldn't have INODE REF[%llu %llu] name %s",
+		      root->objectid, key->objectid, key->offset, namebuf);
+		return;
+	}
+
+	/* normal error */
+	if (err & (DIR_ITEM_MISMATCH | DIR_ITEM_MISSING))
+		error("root %llu DIR ITEM[%llu %llu] %s name %s filetype %u",
+		       root->objectid, key->offset,
+			   btrfs_name_hash(namebuf, name_len),
+		       err & DIR_ITEM_MISMATCH ? "mismath" : "missing",
+		       namebuf, filetype);
+	if (err & (DIR_INDEX_MISMATCH | DIR_INDEX_MISSING))
+		error("root %llu DIR INDEX[%llu %llu] %s name %s filetype %u",
+		       root->objectid, key->offset,
+		       index,
+		       err & DIR_ITEM_MISMATCH ? "mismath" : "missing",
+		       namebuf, filetype);
+}
+
+/*
  * Traverse the given INODE_REF and call find_dir_item() to find related
- * DIR_ITEM/DIR_INDEX.
+ * DIR_ITEM/DIR_INDEX.If repair is enable, research @ref_key and
+ * @path may change.
  *
  * @root:	the root of the fs/file tree
  * @ref_key:	the key of the INODE_REF
+ * @path        the path provides node and slot
  * @refs:	the count of INODE_REF
  * @mode:	the st_mode of INODE_ITEM
+ * @name_ret:   returns with the first ref's name
+ * @name_len_ret:    len of the name_ret
  *
+ * Return <0 on error.
  * Return 0 if no error occurred.
  */
 static int check_inode_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
-			   struct extent_buffer *node, int slot, u64 *refs,
-			   int mode)
+			   struct btrfs_path *path, char *name_ret,
+			   u32 *namelen_ret, u64 *refs, int mode)
 {
 	struct btrfs_key key;
 	struct btrfs_key location;
 	struct btrfs_inode_ref *ref;
+	struct extent_buffer *node;
 	char namebuf[BTRFS_NAME_LEN] = {0};
+	int name_len;
 	u32 total;
 	u32 cur = 0;
-	u32 len;
-	u32 name_len;
+	long len;
 	u64 index;
-	int ret, err = 0;
+	int err = 0;
+	int tmp_err;
+	int slot;
 
 	location.objectid = ref_key->objectid;
 	location.type = BTRFS_INODE_ITEM_KEY;
 	location.offset = 0;
+	node = path->nodes[0];
+	slot = path->slots[0];
 
 	ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
 	total = btrfs_item_size_nr(node, slot);
@@ -4370,6 +4412,7 @@ next:
 	/* Update inode ref count */
 	(*refs)++;
 
+	tmp_err = 0;
 	index = btrfs_inode_ref_index(node, ref);
 	name_len = btrfs_inode_ref_name_len(node, ref);
 	if (name_len <= BTRFS_NAME_LEN) {
@@ -4382,30 +4425,40 @@ next:
 
 	read_extent_buffer(node, namebuf, (unsigned long)(ref + 1), len);
 
-	/* Check root dir ref name */
-	if (index == 0 && strncmp(namebuf, "..", name_len)) {
-		error("root %llu INODE_REF[%llu %llu] ROOT_DIR name shouldn't be %s",
-		      root->objectid, ref_key->objectid, ref_key->offset,
-		      namebuf);
-		err |= ROOT_DIR_ERROR;
+	/* copy the firt name found to name_ret */
+	if (*refs == 1 && name_ret) {
+		memcpy(name_ret, namebuf, len);
+		*namelen_ret = len;
+	}
+	/* Check root dir ref */
+	if (ref_key->objectid == BTRFS_FIRST_FREE_OBJECTID) {
+		if (index != 0 || len != strlen("..") ||
+		    strncmp("..", namebuf, len) ||
+		    ref_key->offset != BTRFS_FIRST_FREE_OBJECTID) {
+			/* set fake err bit so repair will delete the ref */
+			err |= DIR_INDEX_MISSING;
+			err |= DIR_ITEM_MISSING;
+		}
+		goto end;
 	}
 
 	/* Find related DIR_INDEX */
 	key.objectid = ref_key->offset;
 	key.type = BTRFS_DIR_INDEX_KEY;
 	key.offset = index;
-	ret = find_dir_item(root, &key, &location, namebuf, len,
+	tmp_err |= find_dir_item(root, &key, &location, namebuf, len,
 			    imode_to_type(mode));
-	err |= ret;
 
 	/* Find related dir_item */
 	key.objectid = ref_key->offset;
 	key.type = BTRFS_DIR_ITEM_KEY;
 	key.offset = btrfs_name_hash(namebuf, len);
-	ret = find_dir_item(root, &key, &location, namebuf, len,
+	tmp_err |= find_dir_item(root, &key, &location, namebuf, len,
 			    imode_to_type(mode));
-	err |= ret;
-
+end:
+	print_inode_ref_err(root, ref_key, index, namebuf, name_len,
+			    imode_to_type(mode), tmp_err);
+	err |= tmp_err;
 	len = sizeof(*ref) + name_len;
 	ref = (struct btrfs_inode_ref *)((char *)ref + len);
 	cur += len;
@@ -5093,6 +5146,8 @@ static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
 	int slot;
 	int ret;
 	int err = 0;
+	char namebuf[BTRFS_NAME_LEN] = {0};
+	u32 name_len = 0;
 
 	node = path->nodes[0];
 	slot = path->slots[0];
@@ -5133,8 +5188,8 @@ static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
 
 		switch (key.type) {
 		case BTRFS_INODE_REF_KEY:
-			ret = check_inode_ref(root, &key, node, slot, &refs,
-					      mode);
+			ret = check_inode_ref(root, &key, path, namebuf,
+					      &name_len, &refs, mode);
 			err |= ret;
 			break;
 		case BTRFS_INODE_EXTREF_KEY:
-- 
2.11.1




  parent reply	other threads:[~2017-03-01  3:25 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-01  3:13 [PATCH 00/20] Enable lowmem repair for fs/subvolume tree Su Yue
2017-03-01  3:13 ` [PATCH 01/20] btrfs-progs: cmds-check.c: supports inode nbytes fix in lowmem Su Yue
2017-03-01  3:13 ` [PATCH 02/20] btrfs-progs: cmds-check.c: supports dir isize " Su Yue
2017-03-01  3:13 ` [PATCH 03/20] btrfs-progs: cmds-check.c: inode orphan item repair Su Yue
2017-03-01  3:13 ` [PATCH 04/20] btrfs-progs: cmds-check.c: change find_inode_ref's arg Su Yue
2017-03-01  3:13 ` [PATCH 05/20] btrfs-progs: cmds-check.c: modify check_fs_first_inode Su Yue
2017-03-01  3:13 ` [PATCH 06/20] btrfs-progs: cmds-check.c: change find_dir_index/item Su Yue
2017-03-01  3:13 ` Su Yue [this message]
2017-03-01  3:13 ` [PATCH 08/20] btrfs-progs: cmds-check.c: print_dir_item_err Su Yue
2017-03-01  3:13 ` [PATCH 09/20] btrfs-progs: cmds-check.c: introduce count_dir_isize Su Yue
2017-03-01  3:13 ` [PATCH 10/20] btrfs-progs: dir-item.c: modify btrfs_insert_dir_item Su Yue
2017-03-01  3:13 ` [PATCH 11/20] btrfs-progs: inode.c: alter btrfs_add_link Su Yue
2017-03-01  3:13 ` [PATCH 12/20] btrfs-progs: cmds-check.c: introduce __create_inode_item Su Yue
2017-03-01  3:13 ` [PATCH 13/20] btrfs-progs: cmds-check.c: repair_inode_item_missing Su Yue
2017-03-01  3:13 ` [PATCH 14/20] btrfs-progs: cmds-check.c: repair_fs_first_inode Su Yue
2017-03-01  3:13 ` [PATCH 15/20] btrfs-progs: cmds-check.c: introduce repair_ternary_lowmem Su Yue
2017-03-01  3:13 ` [PATCH 16/20] btrfs-progs: cmds-check.c: Introduce repair_dir_item Su Yue
2017-03-01  3:14 ` [PATCH 17/20] btrfs-progs: cmds-check.c: repair inode ref Su Yue
2017-03-01  3:14 ` [PATCH 18/20] btrfs-progs: cmds-check.c: repair nlinks lowmem Su Yue
2017-03-01  3:14 ` [PATCH 19/20] btrfs-progs: cmds-check.c: add punch_extent_hole Su Yue
2017-03-01  3:14 ` [PATCH 20/20] btrfs-progs: fsck-check: Allow fsck check test to repair in lowmem mode for certain test cases Su Yue
2017-03-30 16:44 ` [PATCH 00/20] Enable lowmem repair for fs/subvolume tree David Sterba
2017-03-31  1:49   ` Su Yue

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=20170301031403.23902-8-suy.fnst@cn.fujitsu.com \
    --to=suy.fnst@cn.fujitsu.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=quwenruo@cn.fujitsu.com \
    /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.