From 3efc459166ad8d104912ec06c4ec9e0db6b7ef79 Mon Sep 17 00:00:00 2001 From: Su Yue Date: Mon, 28 Aug 2017 15:08:09 +0800 Subject: [PATCH] btrfs-progs: check: adjustments for further repair For code reuse, btrfs_insert_dir_item() now calls inserts_with_overflow() even if the dir_item existed. Add a parameter @ignore_existed to btrfs_add_link(). If @ignore_existed is not zero, btrfs_add_link() continues to do link. Signed-off-by: Su Yue Signed-off-by: David Sterba --- cmds-check.c | 6 +++--- convert/main.c | 2 +- ctree.h | 2 +- dir-item.c | 12 +++++++++++- inode.c | 44 ++++++++++++++++++++++++-------------------- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/cmds-check.c b/cmds-check.c index e1fe9a49..4f3958b9 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -2961,7 +2961,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; } @@ -3053,7 +3053,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. @@ -3072,7 +3072,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 882daf7c..67616c4b 100644 --- a/convert/main.c +++ b/convert/main.c @@ -768,7 +768,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 22806599..b92df1c1 100644 --- a/ctree.h +++ b/ctree.h @@ -2738,7 +2738,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/dir-item.c b/dir-item.c index e34f6935..462546c0 100644 --- a/dir-item.c +++ b/dir-item.c @@ -135,7 +135,14 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root name, name_len); if (IS_ERR(dir_item)) { ret = PTR_ERR(dir_item); - goto out; + + /* Continue to insert item if existed */ + if (ret == -EEXIST) { + ret = 0; + goto insert; + } else { + goto out; + } } leaf = path->nodes[0]; @@ -149,6 +156,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root write_extent_buffer(leaf, name, name_ptr, name_len); btrfs_mark_buffer_dirty(leaf); +insert: /* FIXME, use some real flag for selecting the extra index */ if (root == root->fs_info->tree_root) { ret = 0; @@ -162,6 +170,8 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root name, name_len); if (IS_ERR(dir_item)) { ret2 = PTR_ERR(dir_item); + if (ret2 == -EEXIST) + ret = 0; goto out; } leaf = path->nodes[0]; diff --git a/inode.c b/inode.c index ea812ce8..2398bca4 100644 --- a/inode.c +++ b/inode.c @@ -162,7 +162,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; @@ -185,33 +185,37 @@ 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 */ @@ -562,7 +566,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: