Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1587672ybv; Thu, 20 Feb 2020 23:49:08 -0800 (PST) X-Google-Smtp-Source: APXvYqwElYMQtln3LM3v75eL10EoZoJNPVwSwbEWLd23GCm+CC3S1+QhD5ywYSJij8NF2POAW2bY X-Received: by 2002:a54:4895:: with SMTP id r21mr939504oic.107.1582271348328; Thu, 20 Feb 2020 23:49:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582271348; cv=none; d=google.com; s=arc-20160816; b=ynO64TZ6ERGq+Gn4dYlh1J+B+d5dqKHTNbSF1A6PzWR+i3fO+9N+90+m+rDJ/SkBYp 8wzh6GWUc8+fF9DPg3yWtVJ+o5uE+nI2b/hmHjWwINjLRfbo5acyt+DTfsA+s7WW+ZIc 2EsJMz1n8B2XGwXQeSfwZ+CKqjbf50Ig4cjeN5Mha/8Yiwj5F+8kbfl3BMWL9eqVZ9br Tyr7pGjeNfQUDL1c3a94EFzgDZ0XdRmYkB4EI29caQF5tO7xRn4aMpWFcYeDVN5Wj5Tq Bre/fjm+UDHE0b5DtVWUz+/qgYZAIJdo7CQPkvidHzz5l8KUs9jE4Z6XSFCWJkAxULpw WHUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sHP6R8Jp+1pp3QvXck7eeA9eQj5gRmk3qTAtynKNi5k=; b=P6OJ6PANaM1fHXliyOHJdYLx5No0N5iQo6aCc6Vi+4IflvSHXze6pSBOpKvSvIU7Ym t6UYCKuz74tN+gR+GawDHHXW/66SeGLgEHH1LJFlrgajZYuKZCApSipzHR66WDR89O7f OJT3bXLk31EgARc+e8COKMPw9A1KYv+GwA8fRMgXp0m7cn5mvMb23Cks63uDcorV9Y+8 Jb4j62l89i9S88FzKWQi9dDF7nSlUREldI8xky05aejR+/I8o9KLIyb8vcVL+HYTtASO 9+tPhnceMz3T1PrWLPu/KKKnPrf7EnrlxGFlXPNrJLd4DKZLvSjzTXgVDbv6sUJvjVUq RLmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IKQAOnw9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e15si1081415otq.237.2020.02.20.23.48.56; Thu, 20 Feb 2020 23:49:08 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IKQAOnw9; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728939AbgBUHrf (ORCPT + 99 others); Fri, 21 Feb 2020 02:47:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:43422 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728032AbgBUHrd (ORCPT ); Fri, 21 Feb 2020 02:47:33 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id F22B124653; Fri, 21 Feb 2020 07:47:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582271252; bh=9gbTX4h6BPWBNpPKfY9Ft7bGOh6sTb6+5ICetoDwNw8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IKQAOnw908JDVznsjHW1hV8ZGavsGHon/tgmMbRyysiTk7y8m6DpLZWOgZ9hz3DRz 4yY+B8iJC8BsPYZc/n0DYc2dPR4EFWsq2WzSw214nfKfaGt3opsyHkL4cZqpL8LS0g qNoxlKGZyMLGY3v0M09ZX1BHGXlOaEJV26MRzV60= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chris Mason , Josef Bacik , David Sterba , Sasha Levin Subject: [PATCH 5.5 096/399] Btrfs: keep pages dirty when using btrfs_writepage_fixup_worker Date: Fri, 21 Feb 2020 08:37:01 +0100 Message-Id: <20200221072411.721545853@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200221072402.315346745@linuxfoundation.org> References: <20200221072402.315346745@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Chris Mason [ Upstream commit 25f3c5021985e885292980d04a1423fd83c967bb ] For COW, btrfs expects pages dirty pages to have been through a few setup steps. This includes reserving space for the new block allocations and marking the range in the state tree for delayed allocation. A few places outside btrfs will dirty pages directly, especially when unmapping mmap'd pages. In order for these to properly go through COW, we run them through a fixup worker to wait for stable pages, and do the delalloc prep. 87826df0ec36 added a window where the dirty pages were cleaned, but pending more action from the fixup worker. We clear_page_dirty_for_io() before we call into writepage, so the page is no longer dirty. The commit changed it so now we leave the page clean between unlocking it here and the fixup worker starting at some point in the future. During this window, page migration can jump in and relocate the page. Once our fixup work actually starts, it finds page->mapping is NULL and we end up freeing the page without ever writing it. This leads to crc errors and other exciting problems, since it screws up the whole statemachine for waiting for ordered extents. The fix here is to keep the page dirty while we're waiting for the fixup worker to get to work. This is accomplished by returning -EAGAIN from btrfs_writepage_cow_fixup if we queued the page up for fixup, which will cause the writepage function to redirty the page. Because we now expect the page to be dirty once it gets to the fixup worker we must adjust the error cases to call clear_page_dirty_for_io() on the page. That is the bulk of the patch, but it is not the fix, the fix is the -EAGAIN from btrfs_writepage_cow_fixup. We cannot separate these two changes out because the error conditions change with the new expectations. Signed-off-by: Chris Mason Signed-off-by: Josef Bacik Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/inode.c | 61 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index c70baafb2a392..27f2c554cac32 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -2204,17 +2204,27 @@ static void btrfs_writepage_fixup_worker(struct btrfs_work *work) struct inode *inode; u64 page_start; u64 page_end; - int ret; + int ret = 0; fixup = container_of(work, struct btrfs_writepage_fixup, work); page = fixup->page; again: lock_page(page); - if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { - ClearPageChecked(page); + + /* + * Before we queued this fixup, we took a reference on the page. + * page->mapping may go NULL, but it shouldn't be moved to a different + * address space. + */ + if (!page->mapping || !PageDirty(page) || !PageChecked(page)) goto out_page; - } + /* + * We keep the PageChecked() bit set until we're done with the + * btrfs_start_ordered_extent() dance that we do below. That drops and + * retakes the page lock, so we don't want new fixup workers queued for + * this page during the churn. + */ inode = page->mapping->host; page_start = page_offset(page); page_end = page_offset(page) + PAGE_SIZE - 1; @@ -2239,24 +2249,22 @@ again: ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start, PAGE_SIZE); - if (ret) { - mapping_set_error(page->mapping, ret); - end_extent_writepage(page, ret, page_start, page_end); - ClearPageChecked(page); + if (ret) goto out; - } ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0, &cached_state); - if (ret) { - mapping_set_error(page->mapping, ret); - end_extent_writepage(page, ret, page_start, page_end); - ClearPageChecked(page); + if (ret) goto out_reserved; - } - ClearPageChecked(page); - set_page_dirty(page); + /* + * Everything went as planned, we're now the owner of a dirty page with + * delayed allocation bits set and space reserved for our COW + * destination. + * + * The page was dirty when we started, nothing should have cleaned it. + */ + BUG_ON(!PageDirty(page)); out_reserved: btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE); if (ret) @@ -2266,6 +2274,17 @@ out: unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end, &cached_state); out_page: + if (ret) { + /* + * We hit ENOSPC or other errors. Update the mapping and page + * to reflect the errors and clean the page. + */ + mapping_set_error(page->mapping, ret); + end_extent_writepage(page, ret, page_start, page_end); + clear_page_dirty_for_io(page); + SetPageError(page); + } + ClearPageChecked(page); unlock_page(page); put_page(page); kfree(fixup); @@ -2293,6 +2312,13 @@ int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) if (TestClearPagePrivate2(page)) return 0; + /* + * PageChecked is set below when we create a fixup worker for this page, + * don't try to create another one if we're already PageChecked() + * + * The extent_io writepage code will redirty the page if we send back + * EAGAIN. + */ if (PageChecked(page)) return -EAGAIN; @@ -2305,7 +2331,8 @@ int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end) btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL); fixup->page = page; btrfs_queue_work(fs_info->fixup_workers, &fixup->work); - return -EBUSY; + + return -EAGAIN; } static int insert_reserved_file_extent(struct btrfs_trans_handle *trans, -- 2.20.1