From af816ca930037063768c9eed8991d80223ededfd Mon Sep 17 00:00:00 2001 From: Qu Wenruo Date: Thu, 25 Oct 2018 15:26:09 +0800 Subject: [PATCH] btrfs-progs: check: orig: Add ability to repair dir item with invalid hash The repair function is reusing delete_corrupted_dir_item(). Since the error can happen for root dir inode, also call try_repair_inode() on root dir inode. This is especially important for old filesystems, since later kernel introduces stricter tree-checker, which could detect such hash mismatch and refuse to read the corrupted leaf. With this repair ability, user could repair with btrfs check --repair. Link: https://bugzilla.opensuse.org/show_bug.cgi?id=1111991 Signed-off-by: Qu Wenruo Signed-off-by: David Sterba --- check/main.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/check/main.c b/check/main.c index 7634ec28..eb6a4a50 100644 --- a/check/main.c +++ b/check/main.c @@ -2658,6 +2658,41 @@ out: return ret; } +static int repair_mismatch_dir_hash(struct btrfs_trans_handle *trans, + struct btrfs_root *root, + struct inode_record *rec) +{ + struct mismatch_dir_hash_record *hash; + int ret; + + printf( + "Deleting bad dir items with invalid hash for root %llu ino %llu\n", + root->root_key.objectid, rec->ino); + while (!list_empty(&rec->mismatch_dir_hash)) { + char *namebuf; + + hash = list_entry(rec->mismatch_dir_hash.next, + struct mismatch_dir_hash_record, list); + namebuf = (char *)(hash + 1); + + ret = delete_corrupted_dir_item(trans, root, &hash->key, + namebuf, hash->namelen); + if (ret < 0) + break; + + /* Also reduce dir isize */ + rec->found_size -= hash->namelen; + list_del(&hash->list); + free(hash); + } + if (!ret) { + rec->errors &= ~I_ERR_MISMATCH_DIR_HASH; + /* We rely on later dir isize repair to reset dir isize */ + rec->errors |= I_ERR_DIR_ISIZE_WRONG; + } + return ret; +} + static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec) { struct btrfs_trans_handle *trans; @@ -2671,7 +2706,8 @@ static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec) I_ERR_FILE_EXTENT_ORPHAN | I_ERR_FILE_EXTENT_DISCOUNT | I_ERR_FILE_NBYTES_WRONG | - I_ERR_INLINE_RAM_BYTES_WRONG))) + I_ERR_INLINE_RAM_BYTES_WRONG | + I_ERR_MISMATCH_DIR_HASH))) return rec->errors; /* @@ -2686,6 +2722,8 @@ static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec) return PTR_ERR(trans); btrfs_init_path(&path); + if (!ret && rec->errors & I_ERR_MISMATCH_DIR_HASH) + ret = repair_mismatch_dir_hash(trans, root, rec); if (rec->errors & I_ERR_NO_INODE_ITEM) ret = repair_inode_no_item(trans, root, &path, rec); if (!ret && rec->errors & I_ERR_FILE_EXTENT_ORPHAN) @@ -2780,6 +2818,11 @@ static int check_inode_recs(struct btrfs_root *root, rec = get_inode_rec(inode_cache, root_dirid, 0); BUG_ON(IS_ERR(rec)); if (rec) { + if (repair) { + ret = try_repair_inode(root, rec); + if (ret < 0) + error++; + } ret = check_root_dir(rec); if (ret) { fprintf(stderr, "root %llu root dir %llu error\n",