From patchwork Fri Jan 26 08:35:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Su Yue X-Patchwork-Id: 10185291 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0A3AA60383 for ; Fri, 26 Jan 2018 08:31:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECB31290EB for ; Fri, 26 Jan 2018 08:31:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E150B290F6; Fri, 26 Jan 2018 08:31:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7127B290EB for ; Fri, 26 Jan 2018 08:31:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752470AbeAZIbF (ORCPT ); Fri, 26 Jan 2018 03:31:05 -0500 Received: from mail.cn.fujitsu.com ([183.91.158.132]:34399 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752377AbeAZIbB (ORCPT ); Fri, 26 Jan 2018 03:31:01 -0500 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="36018074" Received: from bogon (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 26 Jan 2018 16:30:53 +0800 Received: from G08CNEXCHPEKD03.g08.fujitsu.local (unknown [10.167.33.85]) by cn.fujitsu.com (Postfix) with ESMTP id BE61949F19B9 for ; Fri, 26 Jan 2018 16:30:52 +0800 (CST) Received: from archlinux.g08.fujitsu.local (10.167.226.31) by G08CNEXCHPEKD03.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.361.1; Fri, 26 Jan 2018 16:30:52 +0800 From: Su Yue To: CC: Subject: [PATCH 14/15] btrfs-progs: check: handle mismatched filetype in repair_inode_backref Date: Fri, 26 Jan 2018 16:35:18 +0800 Message-ID: <20180126083519.28373-15-suy.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180126083519.28373-1-suy.fnst@cn.fujitsu.com> References: <20180126083519.28373-1-suy.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.31] X-yoursite-MailScanner-ID: BE61949F19B9.AC8CF X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: suy.fnst@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Original can't handle mismatched well. Since it only records inode mode and one filetype of dir_item or dir_index. If backref has mismatched filetype, the one filetype is untrusted. The only trusted thing is inode mode. So, if we found mismatched filetype in a backref, just delete dir_index and dir_item. Then next round of repair will add new dir_index and dir_item whose filetype bases on the inode mode. Transform delete_dir_index() to __delete_dir_item() for code reuse. Signed-off-by: Su Yue --- cmds-check.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/cmds-check.c b/cmds-check.c index 6091c7ef3442..156b4063bf40 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -3059,27 +3059,41 @@ static int add_missing_dir_index(struct btrfs_root *root, return 0; } -static int delete_dir_index(struct btrfs_root *root, - struct inode_backref *backref) +static int __delete_dir_item(struct btrfs_root *root, + struct inode_backref *backref, bool is_index) { struct btrfs_trans_handle *trans; struct btrfs_dir_item *di; struct btrfs_path path; int ret = 0; + u64 offset; + u8 type; + + if (is_index) { + type = BTRFS_DIR_INDEX_KEY; + offset = backref->index; + } else { + type = BTRFS_DIR_ITEM_KEY; + offset = btrfs_name_hash(backref->name, backref->namelen); + } trans = btrfs_start_transaction(root, 1); if (IS_ERR(trans)) return PTR_ERR(trans); - fprintf(stderr, "Deleting bad dir index [%llu,%u,%llu] root %llu\n", - (unsigned long long)backref->dir, - BTRFS_DIR_INDEX_KEY, (unsigned long long)backref->index, - (unsigned long long)root->objectid); + fprintf(stderr, "Deleting bad dir %s [%llu,%u,%llu] root %llu\n", + is_index ? "index" : "item", (unsigned long long)backref->dir, + type, offset, (unsigned long long)root->objectid); btrfs_init_path(&path); - di = btrfs_lookup_dir_index(trans, root, &path, backref->dir, - backref->name, backref->namelen, - backref->index, -1); + if (is_index) + di = btrfs_lookup_dir_index(trans, root, &path, backref->dir, + backref->name, backref->namelen, + backref->index, -1); + else + di = btrfs_lookup_dir_item(trans, root, &path, backref->dir, + backref->name, backref->namelen, + -1); if (IS_ERR(di)) { ret = PTR_ERR(di); btrfs_release_path(&path); @@ -3099,6 +3113,18 @@ static int delete_dir_index(struct btrfs_root *root, return ret; } +static int delete_dir_index(struct btrfs_root *root, + struct inode_backref *backref) +{ + return __delete_dir_item(root, backref, true); +} + +static int delete_dir_item(struct btrfs_root *root, + struct inode_backref *backref) +{ + return __delete_dir_item(root, backref, false); +} + static int __create_inode_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 ino, u64 size, u64 nbytes, u64 nlink, u32 mode) @@ -3228,6 +3254,22 @@ static int repair_inode_backrefs(struct btrfs_root *root, continue; } + if (delete && + (backref->errors & REF_ERR_FILETYPE_UNMATCH)) { + if (backref->found_dir_index) + ret = delete_dir_index(root, backref); + if (ret) + break; + if (backref->found_dir_item) + ret = delete_dir_item(root, backref); + if (ret) + break; + backref->found_dir_index = 0; + backref->found_dir_item = 0; + repaired++; + continue; + } + if (!delete && !backref->found_dir_index && backref->found_dir_item && backref->found_inode_ref) { ret = add_missing_dir_index(root, inode_cache, rec, @@ -3285,6 +3327,12 @@ static int repair_inode_backrefs(struct btrfs_root *root, btrfs_commit_transaction(trans, root); backref->found_dir_index = 1; backref->found_dir_item = 1; + /* + * We can't judge backre->filetype is right or not. + * Just rely on the inode mode. + */ + if (rec->found_inode_item) + backref->errors &= ~REF_ERR_FILETYPE_UNMATCH; repaired++; }