Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3091494ybt; Mon, 29 Jun 2020 15:08:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx3m0W3VVBDTwoSjGCNSYDsIurJH/RIa/Hly+GROLoZ5Sq1dihWWGs2UbXs5S73qWSx09oP X-Received: by 2002:aa7:c887:: with SMTP id p7mr5299693eds.240.1593468485457; Mon, 29 Jun 2020 15:08:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593468485; cv=none; d=google.com; s=arc-20160816; b=gfrjREBGziVYSPy7IktVywhKxkJXtCko7DlFFMoCWf87A0gyy0rpVAW3vPrbH9xk2W h0dvOZwd7mBR9OqtnnYrmylmBh4DJQgoUTrQ+QuN3tMQdRQ5D+guKtjG88/3rw/p895y wVB5Qy8frzf5CoB01hk7bJm1q2Kx3n8KvUpqSj1xPYRlR17O7N1WSXhvTDv7LlWmsAMo +z9rvGmWtoWcT/QDZEnxev6AW8YqZAQhw+EcMmZ/Awj/ujH1ZIHFypatmnCvSsxoZsv4 kgRo/jAkOaDHgL0eQWEaryAnUzBFZ6ubils3lPsP/8Rojg7vdAcIPLIrvq+aqwerxNB5 dq5Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7uBTg43303Fwv84cRF+nQ3O6kLhCGoQB/hzpEtxoK1s=; b=L60Tcj8D8X91U6KDS57XI//+RDBpHPKFsHSvp1HcItsxQ0Q4lBKigIfxTvPIuARwiP LLWel690QAlH8Y6n8D/z+vFwiY76YSBVA77unbGfsCswxhGY91K3XbSVMjr68HZk1awd 5TXuio38wRf5osBQj64LW04rmKjhtTuU9outEXdgqr9YDnBcykhr03oDLTZ0k4JRAZNQ r3pb7K6DMs/7rygBfMLPxYBDfTozUddY9P9C8lwtVxlIQn+NY3WcPp2pQJmcOSRSIACk jxv+XjU+f9hs9KJO/iGZtsJt5ScvJl+mM/j6X27FHI3zPf7FePmnYbfb/4qMUEWXiEg3 ewyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iHzGEEX5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c17si494585edr.237.2020.06.29.15.07.42; Mon, 29 Jun 2020 15:08:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iHzGEEX5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391792AbgF2WEb (ORCPT + 99 others); Mon, 29 Jun 2020 18:04:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:56906 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726180AbgF2SfW (ORCPT ); Mon, 29 Jun 2020 14:35:22 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1BA6A247B3; Mon, 29 Jun 2020 15:21:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1593444115; bh=L/UZd5a6IcXtfLJej821jWf2eMKINcIs7qnt+UN8m3A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iHzGEEX5U7eFucDSiqKTbxkbLUmuq/X6OFZjQRTpONApPbe2hk6igrdKDEiQenmHz wCd5V7ePvN/ZGcaZ2aguVhFvjoljYmLP9OTYtKtGGKIjOfkJpW/aVFEPMc7cjSAOsy v+D1DGsH5OX0xejXLkZYo7rM2y9RscuOZvJnLs54= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Filipe Manana , David Sterba , Greg Kroah-Hartman Subject: [PATCH 5.7 227/265] btrfs: fix RWF_NOWAIT write not failling when we need to cow Date: Mon, 29 Jun 2020 11:17:40 -0400 Message-Id: <20200629151818.2493727-228-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200629151818.2493727-1-sashal@kernel.org> References: <20200629151818.2493727-1-sashal@kernel.org> MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.7.7-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.7.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.7.7-rc1 X-KernelTest-Deadline: 2020-07-01T15:14+00:00 X-stable: review X-Patchwork-Hint: Ignore 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 260a63395f90f67d6ab89e4266af9e3dc34a77e9 upstream. If we attempt to do a RWF_NOWAIT write against a file range for which we can only do NOCOW for a part of it, due to the existence of holes or shared extents for example, we proceed with the write as if it were possible to NOCOW the whole range. Example: $ mkfs.btrfs -f /dev/sdb $ mount /dev/sdb /mnt $ touch /mnt/sdj/bar $ chattr +C /mnt/sdj/bar $ xfs_io -d -c "pwrite -S 0xab -b 256K 0 256K" /mnt/bar wrote 262144/262144 bytes at offset 0 256 KiB, 1 ops; 0.0003 sec (694.444 MiB/sec and 2777.7778 ops/sec) $ xfs_io -c "fpunch 64K 64K" /mnt/bar $ sync $ xfs_io -d -c "pwrite -N -V 1 -b 128K -S 0xfe 0 128K" /mnt/bar wrote 131072/131072 bytes at offset 0 128 KiB, 1 ops; 0.0007 sec (160.051 MiB/sec and 1280.4097 ops/sec) This last write should fail with -EAGAIN since the file range from 64K to 128K is a hole. On xfs it fails, as expected, but on ext4 it currently succeeds because apparently it is expensive to check if there are extents allocated for the whole range, but I'll check with the ext4 people. Fix the issue by checking if check_can_nocow() returns a number of NOCOW'able bytes smaller then the requested number of bytes, and if it does return -EAGAIN. Fixes: edf064e7c6fec3 ("btrfs: nowait aio support") CC: stable@vger.kernel.org # 4.14+ Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/file.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c index 484803f8b2290..52d565ff66e2d 100644 --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1912,18 +1912,29 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb, pos = iocb->ki_pos; count = iov_iter_count(from); if (iocb->ki_flags & IOCB_NOWAIT) { + size_t nocow_bytes = count; + /* * We will allocate space in case nodatacow is not set, * so bail */ if (!(BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) || - check_can_nocow(BTRFS_I(inode), pos, &count) <= 0) { + check_can_nocow(BTRFS_I(inode), pos, &nocow_bytes) <= 0) { inode_unlock(inode); return -EAGAIN; } /* check_can_nocow() locks the snapshot lock on success */ btrfs_drew_write_unlock(&root->snapshot_lock); + /* + * There are holes in the range or parts of the range that must + * be COWed (shared extents, RO block groups, etc), so just bail + * out. + */ + if (nocow_bytes < count) { + inode_unlock(inode); + return -EAGAIN; + } } current->backing_dev_info = inode_to_bdi(inode); -- 2.25.1