From patchwork Thu Oct 6 09:13:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 9364327 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 804C96077E for ; Thu, 6 Oct 2016 09:13:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7133728E7C for ; Thu, 6 Oct 2016 09:13:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 65BE828E7E; Thu, 6 Oct 2016 09:13:59 +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 49A7228E7D for ; Thu, 6 Oct 2016 09:13:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966858AbcJFJNt (ORCPT ); Thu, 6 Oct 2016 05:13:49 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:56888 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S966728AbcJFJNq (ORCPT ); Thu, 6 Oct 2016 05:13:46 -0400 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="882186" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 06 Oct 2016 17:13:40 +0800 Received: from adam-work.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id 16DFA40027FF; Thu, 6 Oct 2016 17:13:39 +0800 (CST) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz Subject: [PATCH] btrfs-progs: Fix stack overflow for checking qgroup on tree reloc tree Date: Thu, 6 Oct 2016 17:13:32 +0800 Message-Id: <20161006091332.12836-1-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.10.0 MIME-Version: 1.0 X-yoursite-MailScanner-ID: 16DFA40027FF.ADE65 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@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 For tree reloc tree whose level is >= 2, the root node's parent will point to itself. In this case it will make btrfsck overflow its stack and cause segfault. While for tree reloc tree, it doesn't affect qgroup and kernel can handle it well. So add tree reloc tree check for qgroup-verify.c and fix the bug. Test case will follow soon after I make a minimal image for it. Current xz ziped image is still over 10M for a 512M fs. Signed-off-by: Qu Wenruo --- qgroup-verify.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/qgroup-verify.c b/qgroup-verify.c index df0e547..9a56f59 100644 --- a/qgroup-verify.c +++ b/qgroup-verify.c @@ -369,6 +369,11 @@ static int find_parent_roots(struct ulist *roots, u64 parent) if (ret < 0) goto out; } + } else if (ref->parent == ref->bytenr) { + /* + * Special loop case for tree reloc tree + */ + ref->root = BTRFS_TREE_RELOC_OBJECTID; } else { ret = find_parent_roots(roots, ref->parent); if (ret < 0) @@ -578,6 +583,8 @@ static u64 resolve_one_root(u64 bytenr) if (ref->root) return ref->root; + if (ref->parent == bytenr) + return BTRFS_TREE_RELOC_OBJECTID; return resolve_one_root(ref->parent); } @@ -748,6 +755,9 @@ static int add_refs_for_implied(struct btrfs_fs_info *info, u64 bytenr, struct btrfs_root *root; struct btrfs_key key; + /* Tree reloc tree doesn't contribute qgroup, skip it */ + if (root_id == BTRFS_TREE_RELOC_OBJECTID) + return 0; key.objectid = root_id; key.type = BTRFS_ROOT_ITEM_KEY; key.offset = (u64)-1;