From patchwork Wed Mar 1 03:14:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Su Yue X-Patchwork-Id: 9597365 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 1D0BE604AA for ; Wed, 1 Mar 2017 03:18:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 162DA28428 for ; Wed, 1 Mar 2017 03:18:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0B36228492; Wed, 1 Mar 2017 03:18:28 +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 05AAF284DE for ; Wed, 1 Mar 2017 03:18:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751770AbdCADSM (ORCPT ); Tue, 28 Feb 2017 22:18:12 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:57673 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751663AbdCADSI (ORCPT ); Tue, 28 Feb 2017 22:18:08 -0500 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="16081022" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 01 Mar 2017 11:13:24 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 1171848A2947 for ; Wed, 1 Mar 2017 11:13:21 +0800 (CST) Received: from localhost.localdomain (10.167.226.129) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Wed, 1 Mar 2017 11:13:20 +0800 From: Su Yue To: CC: Subject: [PATCH 19/20] btrfs-progs: cmds-check.c: add punch_extent_hole Date: Wed, 1 Mar 2017 11:14:02 +0800 Message-ID: <20170301031403.23902-20-suy.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170301031403.23902-1-suy.fnst@cn.fujitsu.com> References: <20170301031403.23902-1-suy.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.129] X-yoursite-MailScanner-ID: 1171848A2947.A5647 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 Introduce 'punch_extent_hole' to punch holes while repair file extent. Signed-off-by: Su Yue --- cmds-check.c | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/cmds-check.c b/cmds-check.c index ae80d5f0..d228fc62 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -4483,6 +4483,7 @@ next: memcpy(name_ret, namebuf, len); *namelen_ret = len; } + /* Check root dir ref */ if (ref_key->objectid == BTRFS_FIRST_FREE_OBJECTID) { if (index != 0 || len != strlen("..") || @@ -5105,6 +5106,33 @@ next: } /* + * Wrapper function of btrfs_punch_hole. + * + * Returns 0 means success. + */ +static int punch_extent_hole(struct btrfs_root *root, u64 ino, u64 start, + u64 len) +{ + struct btrfs_trans_handle *trans; + int ret = 0; + + trans = btrfs_start_transaction(root, 1); + if (IS_ERR(trans)) + return PTR_ERR(trans); + + ret = btrfs_punch_hole(trans, root, ino, start, len); + if (ret) { + error("Failed to add hole [%llu, %llu] in inode [%llu]", + start, len, ino); + } else { + btrfs_commit_transaction(trans, root); + printf("Added hole [%llu, %llu] in inode [%llu]\n", start, len, + ino); + } + return ret; +} + +/* * Check file extent datasum/hole, update the size of the file extents, * check and update the last offset of the file extent. * @@ -5222,9 +5250,14 @@ static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey, error("root %llu EXTENT_DATA[%llu %llu] shouldn't be hole", root->objectid, fkey->objectid, fkey->offset); } else if (!no_holes && *end != fkey->offset) { - err |= FILE_EXTENT_ERROR; - error("root %llu EXTENT_DATA[%llu %llu] interrupt", - root->objectid, fkey->objectid, fkey->offset); + if (repair) + ret = punch_extent_hole(root, fkey->objectid, + *end, fkey->offset - *end); + if (!!repair || ret) { + err |= FILE_EXTENT_ERROR; + error("root %llu EXTENT_DATA[%llu %llu] interrupt", + root->objectid, fkey->objectid, fkey->offset); + } } *end += extent_num_bytes; @@ -5724,9 +5757,15 @@ out: } if (!nbytes && !no_holes && extent_end < isize) { - err |= NBYTES_ERROR; - error("root %llu INODE[%llu] size (%llu) should have a file extent hole", - root->objectid, inode_id, isize); + if (repair) + ret = punch_extent_hole(root, inode_id, + extent_end, + isize-extent_end); + if (!repair || ret) { + err |= NBYTES_ERROR; + error("root %llu INODE[%llu] size (%llu) should have a file extent hole", + root->objectid, inode_id, isize); + } } if (nbytes != extent_size) {