Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp4361214pxu; Mon, 12 Oct 2020 17:34:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgXKHLSUm0ZgknX1tmoZVbalsH0v+93vcJyYAP+gwERm3FZ85ycE/q6vPhBjGfOrEoAG1S X-Received: by 2002:a05:6402:2076:: with SMTP id bd22mr17586114edb.197.1602549272790; Mon, 12 Oct 2020 17:34:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602549272; cv=none; d=google.com; s=arc-20160816; b=IZgKvI2AbbF7pFGIU5PLdk3YwkfdsGSuua30CC6ZZZVznb44asorUoJ4qYu7YRfEQx L/AXyGhLT3QoQ6yGAYvef8pa41lPor5lUNfLTYvefztKYclfPYyqtJvcwftj1dOILTr4 /uoJaYQGO1Pw7pj/oYpORUCDnoCMXY4jYQSgygpxNuUDL4+NMD/85XJY8jcIY+EGsQW5 KfdBmL5gqnjcn9+yDUcxJho/N750/S1Mb3JprrrPvfNHitXesaBllOowpSq31ToNZaui jx9Kl94yHP3j/ULFFKHGA9HCJ5F1t8ftPcXymvgD+0PkA/stJ9RzleX0jiVhRwgVWrV8 JkGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Q8zI9S8l9truBI3KnIZsTUk/JVq6qFP4cHlY7EOPJ+Q=; b=R8MgPJqQRIIIE8pKxfFFY3DsS6vX7Mj79BoBwIDK2XcJd3nuwVe1YZlWctPr7KLe+J X8Z6PzQzNLEd8Xte+FAQZ6qDudPMdmG+aTRn1w3UP+gD81axSks/0uMZYGSvrOG3K0B+ LZml6FCxYUi6MylwnYI+bTQ/hg8C16aY7lHQy5ACPyPPgwzrm8mlgkBdOgsRz/Tw6OvB CY52ky9MYhfBsNTIH/QUSAGV4vaBeHau2VgRXbTqxTtY6RseyYAp96F/9hu8sh2HH1pv 6QhkZ/JycPdPDYbs5VwiWAANw9i7nckUbfWmBRDH2QJB3OJYrMdMTjls2roEQf+ePbI0 KRBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=lmhEZV+C; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a18si13789245edn.400.2020.10.12.17.34.10; Mon, 12 Oct 2020 17:34:32 -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=lmhEZV+C; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730412AbgJLN5w (ORCPT + 99 others); Mon, 12 Oct 2020 09:57:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:46186 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730960AbgJLNlh (ORCPT ); Mon, 12 Oct 2020 09:41:37 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 2516B206D9; Mon, 12 Oct 2020 13:41:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1602510077; bh=pOCpEOGMex8qls1Uwxs4Jm9Q+5I0f51vsBX7PKvY1us=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lmhEZV+C1/7sXkOSwgZFfMoHydXp4Pgwf4ZctTEYPzB5HJBF7hVn325NzUgh/FAyV z6QY4PAiosOZ3qksUKtlumZkUVszSi/e5EfW9NRkREl0W1oXAjZF8qFuWEMBfM/oYH twTTRdhcHO0RQg1R3Ya6jwLx1AyJqEUV+5s9aRu8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , David Sterba , Anand Jain Subject: [PATCH 5.4 30/85] btrfs: fix RWF_NOWAIT write not failling when we need to cow Date: Mon, 12 Oct 2020 15:26:53 +0200 Message-Id: <20201012132634.303967835@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201012132632.846779148@linuxfoundation.org> References: <20201012132632.846779148@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: Anand Jain Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/file.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) --- a/fs/btrfs/file.c +++ b/fs/btrfs/file.c @@ -1919,13 +1919,27 @@ static ssize_t btrfs_file_write_iter(str 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_end_write_no_snapshotting(root); + /* + * 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; }