From patchwork Fri Apr 29 07:19:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 8977861 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 644F89F372 for ; Fri, 29 Apr 2016 07:20:20 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6A7A12024C for ; Fri, 29 Apr 2016 07:20:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7A76620220 for ; Fri, 29 Apr 2016 07:20:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752701AbcD2HUM (ORCPT ); Fri, 29 Apr 2016 03:20:12 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:19142 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1752604AbcD2HUK (ORCPT ); Fri, 29 Apr 2016 03:20:10 -0400 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="462501" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 29 Apr 2016 15:19:19 +0800 Received: from adam-work.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id 8915E4043CFA; Fri, 29 Apr 2016 15:19:16 +0800 (CST) From: Qu Wenruo To: jbacik@fb.com, linux-btrfs@vger.kernel.org Cc: Lu Fengqi Subject: [PATCH RFC v2 15/16] btrfs-progs: fsck: Introduce traversal function for fsck Date: Fri, 29 Apr 2016 15:19:09 +0800 Message-Id: <1461914350-19875-16-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.8.0 In-Reply-To: <1461914350-19875-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1461914350-19875-1-git-send-email-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-yoursite-MailScanner-ID: 8915E4043CFA.AE5BB X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Lu Fengqi Introduce a new function traversal_tree_block() to do pre-order traversal, to co-operate with new fs/subvolume tree skip function. Signed-off-by: Lu Fengqi Signed-off-by: Qu Wenruo --- cmds-check.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/cmds-check.c b/cmds-check.c index 0d651e6..637a4c1 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -9713,6 +9713,83 @@ need_check: return 1; } +/* + * Traversal function for tree block. + * We will do + * 1) Skip shared fs/subvolume tree blocks + * 2) Update related bytes accounting + * 3) Pre-order traversal + */ +static int traversal_tree_block(struct btrfs_root *root, + struct extent_buffer *node) +{ + struct extent_buffer *eb; + int level; + u64 nr; + int i; + int err = 0; + int ret; + + /* + * Skip shared fs/subvolume tree block, in that case they will + * be checked by referencer with lowest rootid + */ + if (is_fstree(root->objectid) && !should_check(root, node)) + return 0; + + /* Update bytes accounting */ + total_btree_bytes += node->len; + if (fs_root_objectid(btrfs_header_owner(node))) + total_fs_tree_bytes += node->len; + if (btrfs_header_owner(node) == BTRFS_EXTENT_TREE_OBJECTID) + total_extent_tree_bytes += node->len; + if (!found_old_backref && + btrfs_header_owner(node) == BTRFS_TREE_RELOC_OBJECTID && + btrfs_header_backref_rev(node) == BTRFS_MIXED_BACKREF_REV && + !btrfs_header_flag(node, BTRFS_HEADER_FLAG_RELOC)) + found_old_backref = 1; + + /* pre-order tranversal, check itself first */ + level = btrfs_header_level(node); + ret = check_tree_block_ref(root, node, btrfs_header_bytenr(node), + btrfs_header_level(node), + btrfs_header_owner(node)); + err |= ret; + if (err) + error("check %s failed root %llu bytenr %llu level %d, force continue check", + level ? "node":"leaf", root->objectid, + btrfs_header_bytenr(node), btrfs_header_level(node)); + + if (!level) { + btree_space_waste += btrfs_leaf_free_space(root, node); + ret = check_leaf_items(root, node); + err |= ret; + return err; + } + + nr = btrfs_header_nritems(node); + btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) - nr) * + sizeof(struct btrfs_key_ptr); + + /* Then check all its children */ + for (i = 0; i < nr; i++) { + u64 blocknr = btrfs_node_blockptr(node, i); + + /* + * As a btrfs tree has most 8 levels(0~7), so it's quite + * safe to call the function itself. + */ + eb = read_tree_block(root, blocknr, root->nodesize, 0); + if (extent_buffer_uptodate(eb)) { + ret = traversal_tree_block(root, eb); + err |= ret; + } + free_extent_buffer(eb); + } + + return err; +} + static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, int overwrite) {