From patchwork Thu Feb 13 15:47:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11380599 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0277113A4 for ; Thu, 13 Feb 2020 15:47:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D2B7020675 for ; Thu, 13 Feb 2020 15:47:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="owBk+r/w" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729342AbgBMPro (ORCPT ); Thu, 13 Feb 2020 10:47:44 -0500 Received: from mail-qk1-f196.google.com ([209.85.222.196]:39245 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729392AbgBMPri (ORCPT ); Thu, 13 Feb 2020 10:47:38 -0500 Received: by mail-qk1-f196.google.com with SMTP id w15so6089163qkf.6 for ; Thu, 13 Feb 2020 07:47:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vC1p4faqe5ftKr0MQUOFDv6QU+rKjzLAO2BlnoeynwU=; b=owBk+r/wsS1yoP4TUG/BbFjBmbA29zXFfW4yL8XBeI0Ef9CTUoEjIrKU0SDy78lebz FNErSdJsDX3Aglhw9amPKbB/GghluDTQMmCUbrDC+UH81WBTMyFGIlHB74iYizAjO+eZ nIhfvAaLv3Eg7/kuvdFDn0q6BMlT5T6p8BbAaEpS3Hfb96WHa184inKp449TodSIyTPC F96n6pzmofBPsi81Da3Pjsk/ge5P08v5/Z2VMnOsYWVyN8XwdH29q+VSS8l8kQRyhNX4 ciNLunzVI7rM5tgiNa+WhnFBRrRNtnbFg4rKZavdhmIFucQ9UXWzwyyRVksihUPgpGJA mnCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vC1p4faqe5ftKr0MQUOFDv6QU+rKjzLAO2BlnoeynwU=; b=CgUg1TH2diM6Lcq+G8XF86Hp9YI+k+5C1MjtW7EN9KKNfxBhTIqGtgeHu2soK50neA +iW7jyfpauvsPFLa/1TVr06pqzQGuR2sLE/jRYSfoF35avGVHAlgth0ofr5vGdKDj8i3 61rR7/CKK7/RNr9lZfZgRoXV5t4Oy/N24O7Zj5sagP2jvf4+wk0wg4nk++As03J4BiVQ 2VSQRwT7KpwvvPZ5gs7LgsrlTASaIypjZmvunyd4bcprXZwvbuhaZ7MBfm/ijXzdjxJC FsYhdiZKQqgM7bheMDIrvKcph9fcPBQCrZvKMSjD0uAkLNYYRmgn7NXetopaXgq/vcLE M4Fw== X-Gm-Message-State: APjAAAU8H7vTO9AWf+uN3j5Paztx7vHxcIkbB68T6ZTa6uLyNDHazgYC +vdTGOdGEyEELrAv0hxO4SyMEQJNzD0= X-Google-Smtp-Source: APXvYqwimrHga8MOEWpdk9JwMQ7eeR0+g/TqGaitcLKUMqu8DvYizhhPQY54JxZhTHjVgRC/iZ/9Lw== X-Received: by 2002:a37:9407:: with SMTP id w7mr10872300qkd.55.1581608857139; Thu, 13 Feb 2020 07:47:37 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id x197sm1536700qkb.28.2020.02.13.07.47.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Feb 2020 07:47:35 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Cc: Nikolay Borisov , Johannes Thumshirn , Qu Wenruo Subject: [PATCH 1/4] btrfs: set fs_root = NULL on error Date: Thu, 13 Feb 2020 10:47:28 -0500 Message-Id: <20200213154731.90994-2-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200213154731.90994-1-josef@toxicpanda.com> References: <20200213154731.90994-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org While running my error injection script I hit a panic when we tried to clean up the fs_root when free'ing the fs_root. This is because fs_info->fs_root == PTR_ERR(-EIO), which isn't great. Fix this by setting fs_info->fs_root = NULL; if we fail to read the root. Reviewed-by: Nikolay Borisov Reviewed-by: Johannes Thumshirn Reviewed-by: Qu Wenruo Signed-off-by: Josef Bacik --- fs/btrfs/disk-io.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index eb441fa3711b..5b6140482cef 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -3260,6 +3260,7 @@ int __cold open_ctree(struct super_block *sb, if (IS_ERR(fs_info->fs_root)) { err = PTR_ERR(fs_info->fs_root); btrfs_warn(fs_info, "failed to read fs tree: %d", err); + fs_info->fs_root = NULL; goto fail_qgroup; } From patchwork Thu Feb 13 15:47:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11380597 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C539613A4 for ; Thu, 13 Feb 2020 15:47:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A598920675 for ; Thu, 13 Feb 2020 15:47:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="jUsYbuZT" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728583AbgBMPrm (ORCPT ); Thu, 13 Feb 2020 10:47:42 -0500 Received: from mail-qv1-f68.google.com ([209.85.219.68]:37437 "EHLO mail-qv1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387954AbgBMPrk (ORCPT ); Thu, 13 Feb 2020 10:47:40 -0500 Received: by mail-qv1-f68.google.com with SMTP id m5so2814938qvv.4 for ; Thu, 13 Feb 2020 07:47:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EJuMZhJ5ecpuzOM5SSHGH0NirU904GzOSGEgGaVuNEY=; b=jUsYbuZTYCEMy/FUA5A9PTnuo2E4TYgWzW7YckGoya/9ZX8/0+UPzOgXe0TaiUM47W VAxBn1LNP0iF4DvlAEVOjAjOijtDem2NAxGnAsOgisFHLJebtHhJjMO5rvhDnOKsH1AN cIJn5wLv8QBc+34o7CGuNSft6Lx5x+P8bNrZkCkmkzFgbwTISELPjloO0VDgFz1Mb0hI jd5TrNNehSVkhsl8fmqgOPtfW3w+boiv+uRhYjbA/nomaCsQ9hTGrJ6S6BtIqrwSKHzr 6vtUs5SIWTVnRO5slADEgizHsRHieRvROT/zNiVxCajjCWWlZQy12KDyMzZMY0qH/lCx W/bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EJuMZhJ5ecpuzOM5SSHGH0NirU904GzOSGEgGaVuNEY=; b=ke1pVJVIigcDN19D6rMz16FrrD0OqbOJMYAnJ1Mdk0Hd2g30GoYwNWSVTunvAVQAHw hIOJCmCCBCvXbsckKXjIiEColMRIA/qFsRtkAlFWosNrz2fZBRbtWabTDQIle0rgDDmm 21XMZ3CBQ8tGTAwddFR3zZ2aiNobBNdulTuL5QV3TDkhXYn528UD7d40kjIUqTnU1A5G sTF+sPTaHecMB1f1eVSF2QB5EgkDGObLrNr3SZUlcyIrcLRFstaLFHyOREhcpr2UT69s 2MTvuygPlzimsQ6e234yGnXby7+OX3tgJDAJXaMw357a9D99eAz5YnMYQSoU/eHrFZgL tYJw== X-Gm-Message-State: APjAAAXK+1M+fIi0uYkmBqPV6ECcnFSyovM0Pi8B22mpC9CTFV2T5YeI hqgDCus6HPLUZBOd8m1oDX0Fz+9MElA= X-Google-Smtp-Source: APXvYqy641CW1DMGwaG/Tpt/2xNZ090Vin7HLAX+eJcJbyI/TpMv9JVUxe42z32U1fOyzXA7fJN5XQ== X-Received: by 2002:ad4:4c42:: with SMTP id cs2mr12118586qvb.198.1581608859348; Thu, 13 Feb 2020 07:47:39 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id z5sm1773211qta.7.2020.02.13.07.47.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Feb 2020 07:47:38 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Cc: Johannes Thumshirn , Nikolay Borisov , Qu Wenruo Subject: [PATCH 2/4] btrfs: do not check delayed items are empty for single trans cleanup Date: Thu, 13 Feb 2020 10:47:29 -0500 Message-Id: <20200213154731.90994-3-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200213154731.90994-1-josef@toxicpanda.com> References: <20200213154731.90994-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org btrfs_assert_delayed_root_empty() will check if the delayed root is completely empty, but this is a fs wide check. On cleanup we may have allowed other transactions to begin, for whatever reason, and thus the delayed root is not empty. So remove this check from cleanup_one_transation(). This however can stay in btrfs_cleanup_transaction(), because it checks only after all of the transactions have been properly cleaned up, and thus is valid. eviewed-by: Johannes Thumshirn Reviewed-by: Nikolay Borisov Reviewed-by: Qu Wenruo Signed-off-by: Josef Bacik --- fs/btrfs/disk-io.c | 1 - 1 file changed, 1 deletion(-) diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c index 5b6140482cef..601ed3335cf6 100644 --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -4543,7 +4543,6 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, wake_up(&fs_info->transaction_wait); btrfs_destroy_delayed_inodes(fs_info); - btrfs_assert_delayed_root_empty(fs_info); btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages, EXTENT_DIRTY); From patchwork Thu Feb 13 15:47:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11380601 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 18AF6921 for ; Thu, 13 Feb 2020 15:47:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EBDB720661 for ; Thu, 13 Feb 2020 15:47:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="PurAWdjA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729400AbgBMPrp (ORCPT ); Thu, 13 Feb 2020 10:47:45 -0500 Received: from mail-qv1-f67.google.com ([209.85.219.67]:38480 "EHLO mail-qv1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729024AbgBMPro (ORCPT ); Thu, 13 Feb 2020 10:47:44 -0500 Received: by mail-qv1-f67.google.com with SMTP id g6so2819818qvy.5 for ; Thu, 13 Feb 2020 07:47:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zaenaX+MsEq5QFPOTT1r8BMG2JGhZph2jZ8eABR49Lw=; b=PurAWdjAhLNhp5ZZ1i0j0jlftx5W4o/NKzhod/ol2flWEhdHZSMqs7LQ4EDWGE3rTV WurHdR4IVvK43lqDsheKyzTkLEjd5Rm1sXvnmfENAGhIk8a7Sy+e4i765PKA1yXftCys YAk8ejYRMBkubKH3M8e/EmA2AK3KybGKiGj6NioQ6CoQsypVUW/RdjhysA+19z2ncdIj klEtSmHL+i0vj9zDW/SmlnOW6ITssuYz2xwyqc4Zd/K4wsPH8VvuAaAUDPErDCt1sGv4 boLk/zBqxBCTSBv1JeF0YO10u5u2twkc8hJmzia75sKVD+T2WA3Grauzr+1HfoQ81hs7 F7Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zaenaX+MsEq5QFPOTT1r8BMG2JGhZph2jZ8eABR49Lw=; b=LD/0vA5iHd3ll/0H0J7Q8sEFrUEOaqz6LfHwPqjRLil1B20htbTSEltG+L54vbSoxw A8XpxhfsKXgH0Hi/2uraGy6qvZFKgiuZ7nJYr6RQdpZ4lDqpqRZXDZxRkFaJ1SbL/mEu 1KtrYSPyYr1ZMZO94x/ftoM8FACNNNzL5NptIMUKUe34VBmMMoT2IYzRWOyVp1ptdnI9 +/JZVvXMdTuUuBccO6q6Gn+ouJ5LUNHgEclgsgFvRWjhFudoNC1DBlLobetaVh22+mFX s3IE4LvH75s/3+EhAWM+H8gcFE+z33dCQ9dhhTfO5EEA0vSwfeF6F0q5tNDo4BjSFZ4L wJZQ== X-Gm-Message-State: APjAAAXw+OrsQohErJ4udLbpr6YOpP+Af3CjohffXejSFTKi1HFlnDXv MUzyD8Yz/Iq1MGWnWYk7czJZG7RXPys= X-Google-Smtp-Source: APXvYqzHMtcvEQTdpfoI52DldnGy1ozBVp89EUXulKJRresLrm5H09YfwMQAn8Cv9wDSgVgZ+Z3PvA== X-Received: by 2002:a0c:ead1:: with SMTP id y17mr11595967qvp.210.1581608861953; Thu, 13 Feb 2020 07:47:41 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id q6sm1468296qkm.46.2020.02.13.07.47.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Feb 2020 07:47:40 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Cc: Johannes Thumshirn , Nikolay Borisov , Qu Wenruo Subject: [PATCH 3/4] btrfs: handle logged extent failure properly Date: Thu, 13 Feb 2020 10:47:30 -0500 Message-Id: <20200213154731.90994-4-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200213154731.90994-1-josef@toxicpanda.com> References: <20200213154731.90994-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If we're allocating a logged extent we attempt to insert an extent record for the file extent directly. We increase space_info->bytes_reserved, because the extent entry addition will call btrfs_update_block_group(), which will convert the ->bytes_reserved to ->bytes_used. However if we fail at any point while inserting the extent entry we will bail and leave space on ->bytes_reserved, which will trigger a WARN_ON() on umount. Fix this by pinning the space if we fail to insert, which is what happens in every other failure case that involves adding the extent entry. Reviewed-by: Johannes Thumshirn Reviewed-by: Nikolay Borisov Reviewed-by: Qu Wenruo Signed-off-by: Josef Bacik --- fs/btrfs/extent-tree.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index c43acb329fa6..2b4c3ca5e651 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -4430,6 +4430,8 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner, offset, ins, 1); + if (ret) + btrfs_pin_extent(fs_info, ins->objectid, ins->offset, 1); btrfs_put_block_group(block_group); return ret; } From patchwork Thu Feb 13 15:47:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 11380603 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7887513A4 for ; Thu, 13 Feb 2020 15:47:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 51EC220661 for ; Thu, 13 Feb 2020 15:47:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20150623.gappssmtp.com header.i=@toxicpanda-com.20150623.gappssmtp.com header.b="hyC6Y00O" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729402AbgBMPrq (ORCPT ); Thu, 13 Feb 2020 10:47:46 -0500 Received: from mail-qk1-f195.google.com ([209.85.222.195]:34870 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729345AbgBMPrp (ORCPT ); Thu, 13 Feb 2020 10:47:45 -0500 Received: by mail-qk1-f195.google.com with SMTP id v2so6118636qkj.2 for ; Thu, 13 Feb 2020 07:47:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Mqg/GgvdfGEGTnTTwE9OKg0ZA4Kue1VMfko7sV3Fl5k=; b=hyC6Y00O57g1wZ0lXSQ1EuTH1d1DmKHktjwrIlcnaph/MteoX3z2tkApKBQql+TyK+ z/xGTsWPKOUQZqNXpy0ouW0G25QE7iIguqV+NRe/E7PtSaE7pf8WMW4MzOxLCuXur7sz cPIYEn+Rss1YkwwH3ScLO8MDQrWep9lhY6VtjDyHUaXx8IZzCbCb63EIHEjkxy4ol1aB ZjEouNm52qJW+ksW3uxavCh5SLeLp8LDmdmt/e/vGS8yMyEr62sOcJ7IODQ0LDbgqSzc VcNerHep+gLs2t9MBqtar2jYKt5R5odtZ+S70IlScff1EZZh1ZdNWVIogzzKUDVXt6Xp fkmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Mqg/GgvdfGEGTnTTwE9OKg0ZA4Kue1VMfko7sV3Fl5k=; b=RiOLA4Or9l7pBwo1ZLjyqj2MxLZD77Fbnoj7Jy2GlgYcuFfGtpozPPFdBsQL0HxK3t uNdMTE7kWZECYFIx+GWpBsxjRLcXk2n0GLXu716U7etqIiBEQvaAqIP6ZH0UMnWcEuDv KUdOUNGFypFY58W4tFeNzZwhDtagCIiPW14SnN1/Vt8wCbz5KvRrPfGp4IyUmIE4HLy+ H3VzDUWrJ9bnM2VrL9ATxG30GBgvjXYzDt+Xjd+UGh2rO75yut2te/GcsynkKvfPLXi8 Uf/be3M8y+G3UuMOsjx/m8P8UFTQEgj9clutqHFAD44t/HRLzw0qmS3+g4BI7Do+TJiL wyUA== X-Gm-Message-State: APjAAAW9fP8M6HUgv9ZBwW3UpFvL+0Vv1Nn1J9ambIhC5+Y7qKbaYWZm 57qT9MePh4htJobfGTqREZzYxxFT1to= X-Google-Smtp-Source: APXvYqyG2csrBOZDdqQ6I+sL+1LmAqRXdUB937xruq5KzbqDuk/TcTXhZucEX9IsGfXwji256kR36w== X-Received: by 2002:a05:620a:13a9:: with SMTP id m9mr16623092qki.359.1581608863674; Thu, 13 Feb 2020 07:47:43 -0800 (PST) Received: from localhost ([107.15.81.208]) by smtp.gmail.com with ESMTPSA id q6sm1468363qkm.46.2020.02.13.07.47.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Feb 2020 07:47:42 -0800 (PST) From: Josef Bacik To: linux-btrfs@vger.kernel.org, kernel-team@fb.com Cc: Qu Wenruo Subject: [PATCH 4/4] btrfs: fix bytes_may_use underflow in prealloc error condtition Date: Thu, 13 Feb 2020 10:47:31 -0500 Message-Id: <20200213154731.90994-5-josef@toxicpanda.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200213154731.90994-1-josef@toxicpanda.com> References: <20200213154731.90994-1-josef@toxicpanda.com> MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org I hit the following warning while running my error injection stress testing ------------[ cut here ]------------ WARNING: CPU: 3 PID: 1453 at fs/btrfs/space-info.h:108 btrfs_free_reserved_data_space_noquota+0xfd/0x160 [btrfs] RIP: 0010:btrfs_free_reserved_data_space_noquota+0xfd/0x160 [btrfs] Call Trace: btrfs_free_reserved_data_space+0x4f/0x70 [btrfs] __btrfs_prealloc_file_range+0x378/0x470 [btrfs] elfcorehdr_read+0x40/0x40 ? elfcorehdr_read+0x40/0x40 ? btrfs_commit_transaction+0xca/0xa50 [btrfs] ? dput+0xb4/0x2a0 ? btrfs_log_dentry_safe+0x55/0x70 [btrfs] ? btrfs_sync_file+0x30e/0x420 [btrfs] ? do_fsync+0x38/0x70 ? __x64_sys_fdatasync+0x13/0x20 ? do_syscall_64+0x5b/0x1b0 ? entry_SYSCALL_64_after_hwframe+0x44/0xa9 ---[ end trace 70ccb5d0fe51151c ]--- This happens if we fail to insert our reserved file extent. At this point we've already converted our reservation from ->bytes_may_use to ->bytes_reserved. However once we break we will attempt to free everything from [cur_offset, end] from ->bytes_may_use, but our extent reservation will overlap part of this. Fix this problem by adding ins.offset (our extent allocation size) to cur_offset so we remove the actual remaining part from ->bytes_may_use. I validated this fix using my inject-error.py script python inject-error.py -o should_fail_bio -t cache_save_setup -t \ __btrfs_prealloc_file_range \ -t insert_reserved_file_extent.constprop.0 \ -r "-5" ./run-fsstress.sh where run-fsstress.sh simply mounts and runs fsstress on a disk. Reviewed-by: Qu Wenruo Signed-off-by: Josef Bacik --- fs/btrfs/inode.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 84e649724549..ffc6fcfe805c 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -9876,6 +9876,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode, struct btrfs_root *root = BTRFS_I(inode)->root; struct btrfs_key ins; u64 cur_offset = start; + u64 clear_offset = start; u64 i_size; u64 cur_bytes; u64 last_alloc = (u64)-1; @@ -9910,6 +9911,15 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode, btrfs_end_transaction(trans); break; } + + /* + * We've reserved this space, and thus converted it from + * ->bytes_may_use to ->bytes_reserved. Any error that happens + * from here on out we will only need to clear our reservation + * for the remaining unreserved area, so advance our + * clear_offset by our extent size. + */ + clear_offset += ins.offset; btrfs_dec_block_group_reservations(fs_info, ins.objectid); last_alloc = ins.offset; @@ -9989,9 +9999,9 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode, if (own_trans) btrfs_end_transaction(trans); } - if (cur_offset < end) - btrfs_free_reserved_data_space(inode, NULL, cur_offset, - end - cur_offset + 1); + if (clear_offset < end) + btrfs_free_reserved_data_space(inode, NULL, clear_offset, + end - clear_offset + 1); return ret; }