From patchwork Mon Sep 21 12:59:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhaolei X-Patchwork-Id: 7229821 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id ED9E8BEEC1 for ; Mon, 21 Sep 2015 13:00:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E61EF206C0 for ; Mon, 21 Sep 2015 13:00:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 17060206BC for ; Mon, 21 Sep 2015 13:00:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756669AbbIUNAx (ORCPT ); Mon, 21 Sep 2015 09:00:53 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:17678 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752966AbbIUNAw (ORCPT ); Mon, 21 Sep 2015 09:00:52 -0400 X-IronPort-AV: E=Sophos;i="5.15,520,1432569600"; d="scan'208";a="100938399" Received: from bogon (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 21 Sep 2015 21:03:36 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t8LD0WkH002837 for ; Mon, 21 Sep 2015 21:00:32 +0800 Received: from localhost.localdomain (10.167.226.114) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server id 14.3.181.6; Mon, 21 Sep 2015 21:00:49 +0800 From: Zhao Lei To: CC: Zhao Lei Subject: [PATCH] btrfs: Fix no space bug caused by removing bg Date: Mon, 21 Sep 2015 20:59:13 +0800 Message-ID: <15fc8f8d002e4ffcdb46e769736f240ae7ace20b.1442839332.git.zhaolei@cn.fujitsu.com> X-Mailer: git-send-email 1.8.5.1 MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP btrfs in v4.3-rc1 failed many xfstests items with '-o nospace_cache' mount option. Failed cases are: btrfs/008,016,019,020,026,027,028,029,031,041,046,048,050,051,053,054, 077,083,084,087,092,094 generic/004,010,014,023,024,074,075,080,086,087,089,091,092,100,112,123, 124,125,126,127,131,133,192,193,198,207,208,209,213,214,215,228,239,240, 246,247,248,255,263,285,306,313,316,323 We can reproduce this bug with following simple command: TEST_DEV=/dev/vdh TEST_DIR=/mnt/tmp umount "$TEST_DEV" >/dev/null mkfs.btrfs -f "$TEST_DEV" mount "$TEST_DEV" "$TEST_DIR" umount "$TEST_DEV" mount "$TEST_DEV" "$TEST_DIR" cp /bin/bash $TEST_DIR Result is: (omit previous commands) # cp /bin/bash $TEST_DIR cp: writing `/mnt/tmp/bash': No space left on device By bisect, we can see it is triggered by patch titled: commit e44163e17796 ("btrfs: explictly delete unused block groups in close_ctree and ro-remount") But the wrong code is not in above patch, btrfs delete all chunks if no data in filesystem, and above patch just make it obviously. Detail reason: 1: mkfs a blank filesystem, or delete everything in filesystem 2: umount fs (current code will delete all data chunks) 3: mount fs Because no any data chunks, data's space_cache have no chance to init, it means: space_info->total_bytes == 0, and space_info->full == 1. 4: do some write Current code will ignore chunk allocate because space_info->full, and return -ENOSPC. Fix: Don't auto-delete last blockgroup for a raid type. If we delete all blockgroup for a raidtype, it not only cause above bug, but also may change filesystem to all-single in some case. Test: Test by above script, and confirmed the logic by debug output. Signed-off-by: Zhao Lei --- fs/btrfs/extent-tree.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index 5411f0a..35cf7eb 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -10012,7 +10012,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) bg_list); space_info = block_group->space_info; list_del_init(&block_group->bg_list); - if (ret || btrfs_mixed_space_info(space_info)) { + if (ret || btrfs_mixed_space_info(space_info) || + block_group->list.next == block_group->list.prev) { btrfs_put_block_group(block_group); continue; }