From patchwork Fri May 26 02:02:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Su Yue X-Patchwork-Id: 9749607 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 BAE4C60249 for ; Fri, 26 May 2017 02:00:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ADD84283BB for ; Fri, 26 May 2017 02:00:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A2BCE283BD; Fri, 26 May 2017 02:00:33 +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 30B8728381 for ; Fri, 26 May 2017 02:00:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S939570AbdEZCAb (ORCPT ); Thu, 25 May 2017 22:00:31 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:3453 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1763601AbdEZCA3 (ORCPT ); Thu, 25 May 2017 22:00:29 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="19323638" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 26 May 2017 10:00:26 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id B807F47C6D39 for ; Fri, 26 May 2017 10:00:25 +0800 (CST) Received: from localhost.localdomain (10.167.226.129) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Fri, 26 May 2017 10:00:25 +0800 From: Su Yue To: Subject: [PATCH v2 1/5] btrfs-progs: check: inode nbytes fix in lowmem Date: Fri, 26 May 2017 10:02:14 +0800 Message-ID: <20170526020218.24502-2-suy.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170526020218.24502-1-suy.fnst@cn.fujitsu.com> References: <20170526020218.24502-1-suy.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.129] X-yoursite-MailScanner-ID: B807F47C6D39.AC6EE 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 After checking one inode item, we should get the actual nbytes of the inode item. Introduce function 'repair_inode_nbytes_lowmem' to set nbytes in struct btrfs_inode_item to the actual nbytes. After call of the function, the wrong nbytes should have been corrected. Signed-off-by: Su Yue --- cmds-check.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/cmds-check.c b/cmds-check.c index ad7c81b2..2797ab9e 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -1922,6 +1922,9 @@ static int process_one_leaf_v2(struct btrfs_root *root, struct btrfs_path *path, again: err |= check_inode_item(root, path, ext_ref); + /* modified cur since check_inode_item may change path */ + cur = path->nodes[0]; + if (err & LAST_ITEM) goto out; @@ -2271,6 +2274,7 @@ static int walk_down_tree_v2(struct btrfs_root *root, struct btrfs_path *path, } ret = process_one_leaf_v2(root, path, nrefs, level, ext_ref); + cur = path->nodes[*level]; break; } else { ret = btrfs_check_node(root, NULL, cur); @@ -4854,10 +4858,69 @@ static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey, } /* + * Set inode item nbytes to @nbytes + * + * Returns <0 means on error + * Returns 0 means successful repair + */ +static int repair_inode_nbytes_lowmem(struct btrfs_root *root, + struct btrfs_path *path, + u64 ino, u64 nbytes) +{ + struct btrfs_trans_handle *trans; + struct btrfs_inode_item *ii; + struct btrfs_key key; + struct btrfs_key research_key; + int ret; + int ret2; + + key.objectid = ino; + key.type = BTRFS_INODE_ITEM_KEY; + key.offset = 0; + btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]); + btrfs_release_path(path); + + trans = btrfs_start_transaction(root, 1); + if (IS_ERR(trans)) { + ret = PTR_ERR(trans); + goto out; + } + + ret = btrfs_search_slot(trans, root, &key, path, 0, 1); + if (ret < 0) + goto out; + if (ret > 0) { + ret = -ENOENT; + goto out; + } + + ii = btrfs_item_ptr(path->nodes[0], path->slots[0], + struct btrfs_inode_item); + btrfs_set_inode_nbytes(path->nodes[0], ii, nbytes); + btrfs_mark_buffer_dirty(path->nodes[0]); + + printf("reset nbytes for inode %llu root %llu\n", ino, + root->root_key.objectid); + + btrfs_commit_transaction(trans, root); +out: + if (ret < 0) + error("failed to reset nbytes for inode %llu root %llu due to %s", + ino, root->root_key.objectid, strerror(-ret)); + + /* research path */ + btrfs_release_path(path); + ret2 = btrfs_search_slot(NULL, root, &research_key, path, 0, 0); + return ret2 < 0 ? ret2 : ret; +} + +/* * Check INODE_ITEM and related ITEMs (the same inode number) * 1. check link count * 2. check inode ref/extref * 3. check dir item/index + * Be Careful, if repair is enable, @path may be changed. + * Remember to reassign any context about @path in repair mode. * * @ext_ref: the EXTENDED_IREF feature * @@ -5007,9 +5070,16 @@ out: } if (nbytes != extent_size) { - err |= NBYTES_ERROR; - error("root %llu INODE[%llu] nbytes(%llu) not equal to extent_size(%llu)", - root->objectid, inode_id, nbytes, extent_size); + if (repair) { + ret = repair_inode_nbytes_lowmem(root, path, + inode_id, extent_size); + } + if (!repair || ret) { + err |= NBYTES_ERROR; + error("root %llu INODE[%llu] nbytes(%llu) not equal to extent_size(%llu)", + root->objectid, inode_id, nbytes, + extent_size); + } } }