From patchwork Thu Sep 28 03:36:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9975251 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 7C91B603F2 for ; Thu, 28 Sep 2017 03:36:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6C8222933B for ; Thu, 28 Sep 2017 03:36:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6024329413; Thu, 28 Sep 2017 03:36:37 +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.4 required=2.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_HI,RCVD_IN_SORBS_SPAM 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 D9E1A2933B for ; Thu, 28 Sep 2017 03:36:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752648AbdI1Dgf (ORCPT ); Wed, 27 Sep 2017 23:36:35 -0400 Received: from mout.gmx.net ([212.227.17.21]:64930 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752560AbdI1DgZ (ORCPT ); Wed, 27 Sep 2017 23:36:25 -0400 Received: from localhost.localdomain ([45.32.39.184]) by mail.gmx.com (mrgmx102 [212.227.17.174]) with ESMTPSA (Nemesis) id 0M82zV-1dBNVH2X9l-00vcYC; Thu, 28 Sep 2017 05:36:23 +0200 From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz Subject: [PATCH 5/5] btrfs: tree-checker: Enhance output for check_extent_data_item Date: Thu, 28 Sep 2017 12:36:10 +0900 Message-Id: <20170928033610.16882-6-quwenruo.btrfs@gmx.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170928033610.16882-1-quwenruo.btrfs@gmx.com> References: <20170928033610.16882-1-quwenruo.btrfs@gmx.com> X-Provags-ID: V03:K0:ablAcOOVYjkMtAotDA8TKlEa0YlML96QYBJlX5hBqOEVXU884PJ 3lzNisf/SoEr/BpQkCaMXFTSNyxNq+BvjA8D6vIm70X9Se6jRHwysxJdjfx8NFZBDj5O5u6 vxebQnZFnVeTOSK+Fpn8cuaKoUgPrtPiJZgLpbSbaaFtFRQ0F4NJfxokxCggDo3vuAMF5xF hMYb22wZ4jePrKczVVRgw== X-UI-Out-Filterresults: notjunk:1; V01:K0:WmTIcZSwFKg=:uK9fDHMOKgp7qze1T/bMZv bnYWp4nsOhyFIW9+ZDvdUImtvaw60pDji/SfLp3zy2qFIKHDIl8qNjGU486aXy6jQKGyL/O+H FP7JaIrbODZ60NQURXMavcrh5rtkgw+jhG/0GhKuGJzYWNHjDMhg4TNrl7Z/J06MbNtqW77+8 zyU8tqUIzh27Ui7spwcfD2EaZNnEjEfvMCG7qTBF9QnPo7GiuSFM29E2xqYHPfvPNLb/bkCff awLIuJjBT3lMV50SXJn+wrPlifuhNJuJP8h46twkQ5n5SvL26sxywbRLGgCDYwVvE8x7TfB0X KaCN5RG5cOYvenVJGEyL/LhNQ32yXxI+Ilm/K7pb2fRbPC/C/04NXzXSVygjwFUEfV35+7D47 qQlxvsfEDljApP0MqnmYMk0xZCLrmIXiO0is8SuVMQf5ZLw1G4rbido9RZex0pk8cYDDuXX/N zpdDWO1Ag9OJOE7RwDnjS0K6BlWw3G3zkWrFwuFtznWbqUiedXAbQAmEUMlU2ROBWDkQJs+lv ysxb1RFsS/EjOAwdSJxXEAp509HOjNI45maL34U9Yl3oopSaz1TSDtdh2+AketszelJWjIcto /b5bf1Cwy4eIU1SGpK/teVKZ1ZzTLtlPoD/biuk3kcBxPAB6g27ybweNHLxZXTJQza9T3BrGF IW6thNdOSEN13XSDgzGVxXTSgoSfB7N+PERLG2WNPIQCqvfnlzDfUYLeVB+U6rguf20tikj8m LSMXU2Zd8rxQsXrG9fER6OyfwL7pbbPqWsmEqD/cPe2cqjn9DBHG81aBsWLJNs9quUndIMD/H uHMcjKAj5s+6oMUqCrWpQgSsSQsbA== 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 Output the invalid member name and its bad value, along with its expected value range or alignment. Signed-off-by: Qu Wenruo --- fs/btrfs/tree-checker.c | 92 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 22 deletions(-) diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c index 52e9ab8c2a79..1324fcae93c0 100644 --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c @@ -63,6 +63,47 @@ static void generic_err(const struct btrfs_root *root, va_end(args); } +/* + * Customized reporter for extent data item, since its key objectid and + * offset has its own meaning. + */ +__printf(4, 5) +static void file_extent_err(const struct btrfs_root *root, + const struct extent_buffer *eb, + int slot, const char *fmt, ...) +{ + struct btrfs_key key; + struct va_format vaf; + va_list args; + + btrfs_item_key_to_cpu(eb, &key, slot); + va_start(args, fmt); + + vaf.fmt = fmt; + vaf.va = &args; + + btrfs_crit(root->fs_info, + "corrupt %s root=%llu tree_block=%llu slot=%d ino=%llu file_offset=%llu: %pV", + btrfs_header_level(eb) == 0 ? "leaf" : "node", + root->objectid, btrfs_header_bytenr(eb), slot, + key.objectid, key.offset, &vaf); + va_end(args); +} + +/* + * Return 0 if the btrfs_file_extent_##name is aligned to @align + * Else return 1 + */ +#define CHECK_FI_ALIGN(root, leaf, slot, fi, name, align) \ +({ \ + if (!IS_ALIGNED(btrfs_file_extent_##name(leaf, fi), align)) \ + file_extent_err(root, leaf, slot, \ + "invalid file extent %s, have %llu, should be aligned to %u",\ + #name, btrfs_file_extent_##name(leaf, fi), \ + align); \ + (!IS_ALIGNED(btrfs_file_extent_##name(leaf, fi), align)); \ +}) + static int check_extent_data_item(struct btrfs_root *root, struct extent_buffer *leaf, struct btrfs_key *key, int slot) @@ -72,15 +113,19 @@ static int check_extent_data_item(struct btrfs_root *root, u32 item_size = btrfs_item_size_nr(leaf, slot); if (!IS_ALIGNED(key->offset, sectorsize)) { - CORRUPT("unaligned key offset for file extent", - leaf, root, slot); + file_extent_err(root, leaf, slot, + "unaligned key offset, have %llu should be aligned to %u", + key->offset, sectorsize); return -EUCLEAN; } fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) { - CORRUPT("invalid file extent type", leaf, root, slot); + file_extent_err(root, leaf, slot, + "invalid file extent type, have %u expect range [0, %u]", + btrfs_file_extent_type(leaf, fi), + BTRFS_FILE_EXTENT_TYPES); return -EUCLEAN; } @@ -89,18 +134,24 @@ static int check_extent_data_item(struct btrfs_root *root, * and must be caught in open_ctree(). */ if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { - CORRUPT("invalid file extent compression", leaf, root, slot); + file_extent_err(root, leaf, slot, + "invalid file extent compression, have %u expect range [0, %u]", + btrfs_file_extent_compression(leaf, fi), + BTRFS_COMPRESS_TYPES); return -EUCLEAN; } if (btrfs_file_extent_encryption(leaf, fi)) { - CORRUPT("invalid file extent encryption", leaf, root, slot); + file_extent_err(root, leaf, slot, + "invalid file extent encryption, have %u expect 0", + btrfs_file_extent_encryption(leaf, fi)); return -EUCLEAN; } if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { /* Inline extent must have 0 as key offset */ if (key->offset) { - CORRUPT("inline extent has non-zero key offset", - leaf, root, slot); + file_extent_err(root, leaf, slot, + "invalid offset for inline extent, have %llu expect 0", + key->offset); return -EUCLEAN; } @@ -112,8 +163,10 @@ static int check_extent_data_item(struct btrfs_root *root, /* Uncompressed inline extent size must match item size */ if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + btrfs_file_extent_ram_bytes(leaf, fi)) { - CORRUPT("plaintext inline extent has invalid size", - leaf, root, slot); + file_extent_err(root, leaf, slot, + "invalid uncompressed inline extent size, have %u expect %llu", + item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + + btrfs_file_extent_ram_bytes(leaf, fi)); return -EUCLEAN; } return 0; @@ -121,22 +174,17 @@ static int check_extent_data_item(struct btrfs_root *root, /* Regular or preallocated extent has fixed item size */ if (item_size != sizeof(*fi)) { - CORRUPT( - "regluar or preallocated extent data item size is invalid", - leaf, root, slot); + file_extent_err(root, leaf, slot, + "invalid extent data item size for reg/prealloc, have %u expect %lu", + item_size, sizeof(*fi)); return -EUCLEAN; } - if (!IS_ALIGNED(btrfs_file_extent_ram_bytes(leaf, fi), sectorsize) || - !IS_ALIGNED(btrfs_file_extent_disk_bytenr(leaf, fi), sectorsize) || - !IS_ALIGNED(btrfs_file_extent_disk_num_bytes(leaf, fi), sectorsize) || - !IS_ALIGNED(btrfs_file_extent_offset(leaf, fi), sectorsize) || - !IS_ALIGNED(btrfs_file_extent_num_bytes(leaf, fi), sectorsize)) { - CORRUPT( - "regular or preallocated extent data item has unaligned value", - leaf, root, slot); + if (CHECK_FI_ALIGN(root, leaf, slot, fi, ram_bytes, sectorsize) || + CHECK_FI_ALIGN(root, leaf, slot, fi, disk_bytenr, sectorsize) || + CHECK_FI_ALIGN(root, leaf, slot, fi, disk_num_bytes, sectorsize) || + CHECK_FI_ALIGN(root, leaf, slot, fi, offset, sectorsize) || + CHECK_FI_ALIGN(root, leaf, slot, fi, num_bytes, sectorsize)) return -EUCLEAN; - } - return 0; }