Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp7810ybm; Mon, 20 May 2019 10:54:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqyU4J6EmgVodam/U3GbxCqmJs7G3R3lbB+fUMdyyqCXyRizgODzYW2bec7VuIgUUOEmpK1A X-Received: by 2002:a17:902:7d8a:: with SMTP id a10mr56932567plm.63.1558374888793; Mon, 20 May 2019 10:54:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558374888; cv=none; d=google.com; s=arc-20160816; b=BQJ6Ed+Ut1GICXetrMTkcvMNay/59OXsd1ADSxP1EHmEtkUxArByqDMHGUUFInGVQC Mu2fhNaTfLm6v77LLeNtaiYO7csMnIbGNPQ813eiaflM6IZx1TUZoiw4dE8r5NYrpH/E r0nSJsby2geHNKmI/AqwEF4dzhhXF/ZVymyzunsajoH0UahZ5DD8heslnYMx6ph2uzU3 Lz3EY/d9RxamTpcleKI3HOZwRgxSxwS1PNU4TB4RJMmJFE7HKLGhC5i31gu1093S+xjv PQsrVDaSlGovTLKw7uQv0cBRfehL7zPsCjNeKwvaQG/ynomB4pBm0VoJgdBpjvcdsCjA O7Zw== 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=bjDJxyAQyXcD2gMwFJsWZXLGvZ0LM7M5cl9wV3V/PL4=; b=Kh/oGEo2RQ/yPlHjsr4qPwDIz9YvPHXukCv8fU9ypsZZQq1BXtUK9AdvwGcx8dLVVH kcvDvylh30jRe9pds/jeWIzwXVTJEKqS6dk7X97jSli/G1kLnKYL+oCRrG+pKHVRfvOQ TyJn4ZYP507Pgx2AMX33g44La/2PvNfQHjYMANqnnq9pe3XIvSpJjt66mBbkF0jPoa6/ WMOJyAxYyqenf1jDEjHCzEuZvFXbHgZjeykJq0CSnN6i1fRO/Q7x72JkO6s1D+H8URjl Lckflc2MzQ6AxWRd1IC5YDjp57zM5lt54RS3vrHBwpyhc5cTIH0T0vzZwdRtKOyLpaiA 2ycQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2C7X05gD; 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 y135si11889431pfc.114.2019.05.20.10.54.33; Mon, 20 May 2019 10:54:48 -0700 (PDT) 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=2C7X05gD; 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 S2391256AbfETMiY (ORCPT + 99 others); Mon, 20 May 2019 08:38:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:52960 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390550AbfETMfF (ORCPT ); Mon, 20 May 2019 08:35:05 -0400 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 0CCB8204FD; Mon, 20 May 2019 12:35:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558355704; bh=55R11hn68ok3pmwLcWUc8/u/Khor9f6UQacwivXnHnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2C7X05gDZvkTN4s4xpzAFDw6gBShTesYypTzLZhTsn6ijS6BEjp55Rlv7Y7JR+5eq Dfto8AV8WlT886BIhF6ej+cubRX4umWdktvrYgFrMnWaFxE+VciYBRPqY1pg5NB7Az tD25nS9rP6yb+0Y2fgwgKDwu8wjj4t3QMG7c4L9s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , David Sterba Subject: [PATCH 5.1 095/128] Btrfs: send, flush dellaloc in order to avoid data loss Date: Mon, 20 May 2019 14:14:42 +0200 Message-Id: <20190520115255.704058582@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190520115249.449077487@linuxfoundation.org> References: <20190520115249.449077487@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: Filipe Manana commit 9f89d5de8631c7930898a601b6612e271aa2261c upstream. When we set a subvolume to read-only mode we do not flush dellaloc for any of its inodes (except if the filesystem is mounted with -o flushoncommit), since it does not affect correctness for any subsequent operations - except for a future send operation. The send operation will not be able to see the delalloc data since the respective file extent items, inode item updates, backreferences, etc, have not hit yet the subvolume and extent trees. Effectively this means data loss, since the send stream will not contain any data from existing delalloc. Another problem from this is that if the writeback starts and finishes while the send operation is in progress, we have the subvolume tree being being modified concurrently which can result in send failing unexpectedly with EIO or hitting runtime errors, assertion failures or hitting BUG_ONs, etc. Simple reproducer: $ mkfs.btrfs -f /dev/sdb $ mount /dev/sdb /mnt $ btrfs subvolume create /mnt/sv $ xfs_io -f -c "pwrite -S 0xea 0 108K" /mnt/sv/foo $ btrfs property set /mnt/sv ro true $ btrfs send -f /tmp/send.stream /mnt/sv $ od -t x1 -A d /mnt/sv/foo 0000000 ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea * 0110592 $ umount /mnt $ mkfs.btrfs -f /dev/sdc $ mount /dev/sdc /mnt $ btrfs receive -f /tmp/send.stream /mnt $ echo $? 0 $ od -t x1 -A d /mnt/sv/foo 0000000 # ---> empty file Since this a problem that affects send only, fix it in send by flushing dellaloc for all the roots used by the send operation before send starts to process the commit roots. This is a problem that affects send since it was introduced (commit 31db9f7c23fbf7 ("Btrfs: introduce BTRFS_IOC_SEND for btrfs send/receive")) but backporting it to older kernels has some dependencies: - For kernels between 3.19 and 4.20, it depends on commit 3cd24c698004d2 ("btrfs: use tagged writepage to mitigate livelock of snapshot") because the function btrfs_start_delalloc_snapshot() does not exist before that commit. So one has to either pick that commit or replace the calls to btrfs_start_delalloc_snapshot() in this patch with calls to btrfs_start_delalloc_inodes(). - For kernels older than 3.19 it also requires commit e5fa8f865b3324 ("Btrfs: ensure send always works on roots without orphans") because it depends on the function ensure_commit_roots_uptodate() which that commits introduced. - No dependencies for 5.0+ kernels. A test case for fstests follows soon. CC: stable@vger.kernel.org # 3.19+ Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/send.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) --- a/fs/btrfs/send.c +++ b/fs/btrfs/send.c @@ -6579,6 +6579,38 @@ commit_trans: return btrfs_commit_transaction(trans); } +/* + * Make sure any existing dellaloc is flushed for any root used by a send + * operation so that we do not miss any data and we do not race with writeback + * finishing and changing a tree while send is using the tree. This could + * happen if a subvolume is in RW mode, has delalloc, is turned to RO mode and + * a send operation then uses the subvolume. + * After flushing delalloc ensure_commit_roots_uptodate() must be called. + */ +static int flush_delalloc_roots(struct send_ctx *sctx) +{ + struct btrfs_root *root = sctx->parent_root; + int ret; + int i; + + if (root) { + ret = btrfs_start_delalloc_snapshot(root); + if (ret) + return ret; + btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX); + } + + for (i = 0; i < sctx->clone_roots_cnt; i++) { + root = sctx->clone_roots[i].root; + ret = btrfs_start_delalloc_snapshot(root); + if (ret) + return ret; + btrfs_wait_ordered_extents(root, U64_MAX, 0, U64_MAX); + } + + return 0; +} + static void btrfs_root_dec_send_in_progress(struct btrfs_root* root) { spin_lock(&root->root_item_lock); @@ -6803,6 +6835,10 @@ long btrfs_ioctl_send(struct file *mnt_f NULL); sort_clone_roots = 1; + ret = flush_delalloc_roots(sctx); + if (ret) + goto out; + ret = ensure_commit_roots_uptodate(sctx); if (ret) goto out;