From patchwork Wed Dec 20 04:57:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Su Yue X-Patchwork-Id: 10124987 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 8835A60390 for ; Wed, 20 Dec 2017 04:54:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 80B1D28E2D for ; Wed, 20 Dec 2017 04:54:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 759BF291FC; Wed, 20 Dec 2017 04:54:05 +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 1991728E2D for ; Wed, 20 Dec 2017 04:54:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754525AbdLTEyB (ORCPT ); Tue, 19 Dec 2017 23:54:01 -0500 Received: from mail.cn.fujitsu.com ([183.91.158.132]:24084 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754454AbdLTExs (ORCPT ); Tue, 19 Dec 2017 23:53:48 -0500 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="31879399" Received: from localhost (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 20 Dec 2017 12:53:42 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 9C8B148AE9E9; Wed, 20 Dec 2017 12:53:41 +0800 (CST) Received: from archlinux.g08.fujitsu.local (10.167.226.31) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.361.1; Wed, 20 Dec 2017 12:53:40 +0800 From: Su Yue To: CC: , Subject: [PATCH v2 07/17] btrfs-progs: lowmem check: introduce try_avoid_extents_overwrite() Date: Wed, 20 Dec 2017 12:57:21 +0800 Message-ID: <20171220045731.19343-8-suy.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20171220045731.19343-1-suy.fnst@cn.fujitsu.com> References: <20171220045731.19343-1-suy.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.31] X-yoursite-MailScanner-ID: 9C8B148AE9E9.AC099 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 Define a global enum extents_operation to record extents are pinned, excluded or new chunk is allocated for extents. Although global variable is not so graceful, it simplifies codes much. New function try_avoid_extents_overwrite() will try to mark block groups full and allocate a new chunk. If it failed because of no space or wrong used bytes(fsck-tests/004), then try to exclude metadata blocks. Signed-off-by: Su Yue --- cmds-check.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/cmds-check.c b/cmds-check.c index 311c8a9f45e8..9042bab93785 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -84,6 +84,15 @@ enum btrfs_check_mode { static enum btrfs_check_mode check_mode = CHECK_MODE_DEFAULT; +enum lowmem_extents_operation { + EXTENTS_PIN, + EXTENTS_EXCLUDE, + EXTENTS_MARK_BG_FULL, + EXTENTS_NONE +}; + +static enum lowmem_extents_operation extents_operation = EXTENTS_NONE; + struct extent_backref { struct rb_node node; unsigned int is_data:1; @@ -13517,6 +13526,98 @@ out: return err; } +static void cleanup_excluded_extents(struct btrfs_fs_info *fs_info); +static int end_avoid_extents_overwrite(struct btrfs_fs_info *fs_info) +{ + int ret; + + switch (extents_operation) { + case EXTENTS_PIN: + ret = btrfs_finish_extent_commit(NULL, fs_info->extent_root, + &fs_info->pinned_extents); + break; + case EXTENTS_EXCLUDE: + cleanup_excluded_extents(fs_info); + ret = 0; + break; + case EXTENTS_MARK_BG_FULL: + ret = modify_block_groups_cache(fs_info, + BTRFS_BLOCK_GROUP_METADATA, 0); + break; + case EXTENTS_NONE: + ret = 0; + break; + default: + ret = -EINVAL; + } + + if (!ret) + extents_operation = EXTENTS_NONE; + return ret; +} + +static int pin_metadata_blocks(struct btrfs_fs_info *fs_info); +static int exclude_metadata_blocks(struct btrfs_fs_info *fs_info); +/* + * NOTE: Do not call this function during transaction. + */ +static int avoid_extents_overwrite(struct btrfs_fs_info *fs_info, + enum lowmem_extents_operation op) +{ + int ret; + + if (op == extents_operation) + return 0; + + switch (op) { + case EXTENTS_PIN: + ret = pin_metadata_blocks(fs_info); + break; + case EXTENTS_EXCLUDE: + ret = exclude_metadata_blocks(fs_info); + break; + case EXTENTS_MARK_BG_FULL: + ret = force_cow_in_new_chunk(fs_info); + break; + case EXTENTS_NONE: + ret = 0; + break; + default: + return -EINVAL; + } + + /* extents_operation should be assigned anyway for latter clean up. */ + extents_operation = op; + + if (ret) + end_avoid_extents_overwrite(fs_info); + return ret; +} + +static int try_avoid_extents_overwrite(struct btrfs_fs_info *fs_info) +{ + int ret; + int mixed = btrfs_fs_incompat(fs_info, MIXED_GROUPS); + + if (extents_operation != EXTENTS_NONE) + return 0; + ret = avoid_extents_overwrite(fs_info, EXTENTS_MARK_BG_FULL); + + /* + * If there is no space left to allocate, try to exclude all metadata + * blocks. Mix filesystem is unsupported. + */ + if (ret && ret == -ENOSPC && !mixed) { + printf( + "Try to exclude all metadata blcoks and extents, it may be slow\n"); + ret = avoid_extents_overwrite(fs_info, EXTENTS_EXCLUDE); + } + + if (ret) + error("failed to avoid extents overwrite %s", strerror(-ret)); + return ret; +} + /* * Low memory usage version check_chunks_and_extents. */