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 11/20] btrfs-progs: inode.c: alter btrfs_add_link
Date: Wed, 1 Mar 2017 11:13:54 +0800	[thread overview]
Message-ID: <20170301031403.23902-12-suy.fnst@cn.fujitsu.com> (raw)
In-Reply-To: <20170301031403.23902-1-suy.fnst@cn.fujitsu.com>

Add an arg 'ignore_existed' to btrfs_add_link.
If ignore_existed=1, continue to add while relative dir index/item
or inode ref is already existed.

This patch is for further repair.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 cmds-check.c   |  6 +++---
 convert/main.c |  2 +-
 ctree.h        |  2 +-
 inode.c        | 46 ++++++++++++++++++++++++++--------------------
 4 files changed, 31 insertions(+), 25 deletions(-)

diff --git a/cmds-check.c b/cmds-check.c
index 685f4f5d..bda0849b 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -2855,7 +2855,7 @@ static int reset_nlink(struct btrfs_trans_handle *trans,
 	list_for_each_entry(backref, &rec->backrefs, list) {
 		ret = btrfs_add_link(trans, root, rec->ino, backref->dir,
 				     backref->name, backref->namelen,
-				     backref->filetype, &backref->index, 1);
+				     backref->filetype, &backref->index, 1, 0);
 		if (ret < 0)
 			goto out;
 	}
@@ -2947,7 +2947,7 @@ static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
 			goto out;
 		}
 		ret = btrfs_add_link(trans, root, rec->ino, lost_found_ino,
-				     namebuf, namelen, type, NULL, 1);
+				     namebuf, namelen, type, NULL, 1, 0);
 		/*
 		 * Add ".INO" suffix several times to handle case where
 		 * "FILENAME.INO" is already taken by another file.
@@ -2966,7 +2966,7 @@ static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
 			namelen += count_digits(rec->ino) + 1;
 			ret = btrfs_add_link(trans, root, rec->ino,
 					     lost_found_ino, namebuf,
-					     namelen, type, NULL, 1);
+					     namelen, type, NULL, 1, 0);
 		}
 		if (ret < 0) {
 			fprintf(stderr,
diff --git a/convert/main.c b/convert/main.c
index 8d9f29fa..7607bec1 100644
--- a/convert/main.c
+++ b/convert/main.c
@@ -998,7 +998,7 @@ static int create_image(struct btrfs_root *root,
 	if (ret < 0)
 		goto out;
 	ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name,
-			     strlen(name), BTRFS_FT_REG_FILE, NULL, 1);
+			     strlen(name), BTRFS_FT_REG_FILE, NULL, 1, 0);
 	if (ret < 0)
 		goto out;
 
diff --git a/ctree.h b/ctree.h
index 0c34ae20..a28e36de 100644
--- a/ctree.h
+++ b/ctree.h
@@ -2779,7 +2779,7 @@ int btrfs_change_inode_flags(struct btrfs_trans_handle *trans,
 			     struct btrfs_root *root, u64 ino, u64 flags);
 int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		   u64 ino, u64 parent_ino, char *name, int namelen,
-		   u8 type, u64 *index, int add_backref);
+		   u8 type, u64 *index, int add_backref, int ignore_existed);
 int btrfs_unlink(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		 u64 ino, u64 parent_ino, u64 index, const char *name,
 		 int namelen, int add_orphan);
diff --git a/inode.c b/inode.c
index 991b8ddb..62e8abec 100644
--- a/inode.c
+++ b/inode.c
@@ -161,7 +161,7 @@ out:
  */
 int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		   u64 ino, u64 parent_ino, char *name, int namelen,
-		   u8 type, u64 *index, int add_backref)
+		   u8 type, u64 *index, int add_backref, int ignore_existed)
 {
 	struct btrfs_path *path;
 	struct btrfs_key key;
@@ -184,33 +184,38 @@ int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	}
 
 	ret = check_dir_conflict(root, name, namelen, parent_ino, ret_index);
-	if (ret < 0)
+	if (ret < 0 && (!ignore_existed || ret != -EEXIST))
 		goto out;
 
 	/* Add inode ref */
 	if (add_backref) {
 		ret = btrfs_insert_inode_ref(trans, root, name, namelen,
 					     ino, parent_ino, ret_index);
-		if (ret < 0)
+		if (ret < 0 && (!ignore_existed || ret != -EEXIST))
 			goto out;
 
-		/* Update nlinks for the inode */
-		key.objectid = ino;
-		key.type = BTRFS_INODE_ITEM_KEY;
-		key.offset = 0;
-		ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
-		if (ret) {
-			if (ret > 0)
-				ret = -ENOENT;
-			goto out;
+		/* do not update nlinks if existed */
+		if (!ret) {
+			/* Update nlinks for the inode */
+			key.objectid = ino;
+			key.type = BTRFS_INODE_ITEM_KEY;
+			key.offset = 0;
+			ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
+			if (ret) {
+				if (ret > 0)
+					ret = -ENOENT;
+				goto out;
+			}
+			inode_item = btrfs_item_ptr(path->nodes[0],
+						    path->slots[0],
+						    struct btrfs_inode_item);
+			nlink = btrfs_inode_nlink(path->nodes[0], inode_item);
+			nlink++;
+			btrfs_set_inode_nlink(path->nodes[0], inode_item,
+					      nlink);
+			btrfs_mark_buffer_dirty(path->nodes[0]);
+			btrfs_release_path(path);
 		}
-		inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
-				    struct btrfs_inode_item);
-		nlink = btrfs_inode_nlink(path->nodes[0], inode_item);
-		nlink++;
-		btrfs_set_inode_nlink(path->nodes[0], inode_item, nlink);
-		btrfs_mark_buffer_dirty(path->nodes[0]);
-		btrfs_release_path(path);
 	}
 
 	/* Add dir_item and dir_index */
@@ -219,6 +224,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	key.offset = 0;
 	ret = btrfs_insert_dir_item(trans, root, name, namelen, parent_ino,
 				    &key, type, ret_index);
+
 	if (ret < 0)
 		goto out;
 
@@ -561,7 +567,7 @@ int btrfs_mkdir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	if (ret)
 		goto out;
 	ret = btrfs_add_link(trans, root, ret_ino, parent_ino, name, namelen,
-			     BTRFS_FT_DIR, NULL, 1);
+			     BTRFS_FT_DIR, NULL, 1, 0);
 	if (ret)
 		goto out;
 out:
-- 
2.11.1




  parent reply	other threads:[~2017-03-01  4:29 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 ` [PATCH 07/20] btrfs-progs: cmds-check.c: introduce print_inode_ref Su Yue
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 ` Su Yue [this message]
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-12-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.