From patchwork Fri Aug 23 16:12:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 13775477 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65152C5321D for ; Fri, 23 Aug 2024 16:12:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6FC06B02DC; Fri, 23 Aug 2024 12:12:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B1DF1800A4; Fri, 23 Aug 2024 12:12:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9993A6B02DE; Fri, 23 Aug 2024 12:12:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7333E6B02DC for ; Fri, 23 Aug 2024 12:12:39 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 65B98AA1D8 for ; Fri, 23 Aug 2024 16:12:36 +0000 (UTC) X-FDA: 82484003112.20.711DF0B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf16.hostedemail.com (Postfix) with ESMTP id 9DBC318001E for ; Fri, 23 Aug 2024 16:12:34 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E4YH4ZAk; spf=pass (imf16.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724429537; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BgDxF6Y2BCwscRMX/1nHWh5c3+O9vNtYrvIJazlGbCw=; b=0qtaeXjyRPTi98IjioNzOY3CBa2Yl80FIv75rDdnjQxqboJpybK5oHQwdXTxPkBZ/hZQvG pl/pHH68ZfCTTHOgvF2LPY4jbPTkz1U3AeMMEWkjemNt18FTTNd2aINMgOzKLwl/uSH6/G 3WhuV54InFNbFWphqAz3y7cmcxWNCvg= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E4YH4ZAk; spf=pass (imf16.hostedemail.com: domain of dhowells@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724429537; a=rsa-sha256; cv=none; b=ql1v/mp4QPZCpwEzxlzOlDpS9bcsdhvJ6lSR8Mw8eiEp15BEvU9Hn15OZI3JW9npcyZ0GY cPHPuCBaEHIBErYhS2XnYMRGCHw2gQ5G88XXQyZhl/LHWBsjdEyY/9bQE9o//YrGTMYodh 9BcQ1kh4PB8S6eafj8LpT26hcR4lW7I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724429554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BgDxF6Y2BCwscRMX/1nHWh5c3+O9vNtYrvIJazlGbCw=; b=E4YH4ZAkzouIzyTrNuZp6xiEqf+Q3S1CSyLdJUkv6vT7Pi2B6fdWNsbOgKRkd1C9rKjA/a bv2l6Fvc0pSXb1o4SauI8a5e8Bolo62Xrqr/e7uE7fLPRbdepWjjyMTvyG7EGAurLpSlIt Tfayw2bltJvxYtiF50zZ3oX8QgsPyi8= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-77-w4BHTpKQMVyxJPieBikXbA-1; Fri, 23 Aug 2024 12:12:27 -0400 X-MC-Unique: w4BHTpKQMVyxJPieBikXbA-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1319F1955D45; Fri, 23 Aug 2024 16:12:24 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.30]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9FC54300019C; Fri, 23 Aug 2024 16:12:17 +0000 (UTC) From: David Howells To: Christian Brauner Cc: David Howells , Pankaj Raghav , Jeff Layton , Matthew Wilcox , netfs@lists.linux.dev, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs@lists.linux.dev, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Steve French , Paulo Alcantara , Shyam Prasad N Subject: [PATCH 1/5] cifs: Fix FALLOC_FL_PUNCH_HOLE support Date: Fri, 23 Aug 2024 17:12:02 +0100 Message-ID: <20240823161209.434705-2-dhowells@redhat.com> In-Reply-To: <20240823161209.434705-1-dhowells@redhat.com> References: <20240823161209.434705-1-dhowells@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Rspam-User: X-Stat-Signature: 6ijm4knm5j3fkeshgt9as533kgpx3k5s X-Rspamd-Queue-Id: 9DBC318001E X-Rspamd-Server: rspam11 X-HE-Tag: 1724429554-326740 X-HE-Meta: U2FsdGVkX1+gs89jPXO6Aamp6JTpGjmsundoCNXmyTgGSas1Qkc7uSYuXw4M3mIV3Tu1njgkTEQBPmra9Wc0Sg2lpPAlcwwT96Q9OQzq1TezUzK/eUtSQWQS0VnYBvcxy0ym9uauynlUN1aEwJUYCczxnHBYuegz5QRPvMX+SDRj3Hez2IXEOfpDm/SQgKD5yHOswyEucyyfmMsBFxVLswwjgaLRwP+0ygyWjFYCzWjS4Uzd/JPEIihvFeiHpSriHNU/Yd+0mrVliQSevBTq/oTRPSgDDX/AbaVliB2gLpKjPTTiZYp6mVx/LXfBvUEALrmEpdbq00H52yfA0Y0xJKKLcKucJEzqUpA2jKuaS24JevAfaApMHeT9oTctvNn9FSiTgDS/xdSrFNxBpkvSdeCJeM3c6tNrXQQWBPJ+zxGH5+CczoeVYlqb3oU1ShhpvR3Cp0K5/2BwDyKYXiEWENZeB/U/rNdG4pJ7+AoiRx1hxfT0HiqVNGh9dtwnREbuZ1hB12uq6u8Wl54Q+rF+MiEEg/2kyJikzkIiy9j+A81Kw1UiOshwCzUi0IXeUe/8vGbozVGKpf2h6MBfLqPOI/2vBGK1kNXYSXJeAaSkaw4JITmNOHKAhpBFiCSg8T8NQVvQMCh6v0WtopRTDl+rQqK+oaad8sf951IvC1BEsVbrAc8xBrHftqx8J1f22wB4c3QawJvym4RwKFxBwV9MFMTuoA8d6tXilOmWTt/LpCDQgG9a8JG3I4ZODGPdo5kxloIJvK4q7UkBDIbyj8Zr6fSlC+wokIzNCd+ixIKyERyyUpej0Bwhh+JfWEtj9lJ9Xluv/MHVSWUsxa0C7xO+iTQVR82MifyUBYyM+Z0CIb1vYonjfXlJh8z3/2eSHE6FpsiSXhj13TdqpQmkxIQ9BtCWkeljJCNzCl783rsUEfqvIW/3xXp1xXBHKBFS8Yu0iuQNlqsMbV1+oFsgEW+ 6ExbPP7N AQEka/kvwAXyYttHgJjQIlhg69rBiJLtMYdLpL4N5uShlVcI61Oh0Br69SQBJoYFVB4kGrB+jmAkO3ueeNSrZcorjl5IdFILaOLZWCHH/3RupO+z+5Kz0Db18VjwgGxo7rMlXbioN4makC0qBsNQfwBVNvgntqnI6B7o9riYvfqph6VedK/UKjgQC+hIDHQR4rK/DA6fpaWNNkepgAz+AbkXxZDLN5XfxZwYk2GfseAHc4N803aiJbSVX/89+tb3zfZO11gKNmUhRr9YApbosrB+s+yUnFyoX0qsnVKRDUtYi25k9f3Xa1NwTvOeAWK+CUwdkRMCpEhrhyYq24RPGtq90A4aMvTmCyMd0c+aRBBS6MxU+rf2x8OKMnzyt25dHN7cKtZo6DoaMAcEs5SjXpLSvkCY8PdTo81GGeEgDyas23DMO0ytEtdddBsQ3J9Mxn3QPvyzBAIKcNR/RDwmD7JAD46XijYM7fZ5Jb3SaXezt4Fr/bEVwuTlcrNw6ajzTDaKBm6UVOMKdHg6cYNqbW0dbxx7YTADY+yIo6xYotVSONTs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The cifs filesystem doesn't quite emulate FALLOC_FL_PUNCH_HOLE correctly (note that due to lack of protocol support, it can't actually implement it directly). Whilst it will (partially) invalidate dirty folios in the pagecache, it doesn't write them back first, and so the EOF marker on the server may be lower than inode->i_size. This presents a problem, however, as if the punched hole invalidates the tail of the locally cached dirty data, writeback won't know it needs to move the EOF over to account for the hole punch (which isn't supposed to move the EOF). We could just write zeroes over the punched out region of the pagecache and write that back - but this is supposed to be a deallocatory operation. Fix this by manually moving the EOF over on the server after the operation if the hole punched would corrupt it. Note that the FSCTL_SET_ZERO_DATA RPC and the setting of the EOF should probably be compounded to stop a third party interfering (or, at least, massively reduce the chance). This was reproducible occasionally by using fsx with the following script: truncate 0x0 0x375e2 0x0 punch_hole 0x2f6d3 0x6ab5 0x375e2 truncate 0x0 0x3a71f 0x375e2 mapread 0xee05 0xcf12 0x3a71f write 0x2078e 0x5604 0x3a71f write 0x3ebdf 0x1421 0x3a71f * punch_hole 0x379d0 0x8630 0x40000 * mapread 0x2aaa2 0x85b 0x40000 fallocate 0x1b401 0x9ada 0x40000 read 0x15f2 0x7d32 0x40000 read 0x32f37 0x7a3b 0x40000 * The second "write" should extend the EOF to 0x40000, and the "punch_hole" should operate inside of that - but that depends on whether the VM gets in and writes back the data first. If it doesn't, the file ends up 0x3a71f in size, not 0x40000. Fixes: 31742c5a3317 ("enable fallocate punch hole ("fallocate -p") for SMB3") Signed-off-by: David Howells cc: Steve French cc: Paulo Alcantara cc: Shyam Prasad N cc: Jeff Layton cc: linux-cifs@vger.kernel.org cc: netfs@lists.linux.dev --- fs/smb/client/smb2ops.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c index 322cabc69c6f..763a17e62750 100644 --- a/fs/smb/client/smb2ops.c +++ b/fs/smb/client/smb2ops.c @@ -3305,6 +3305,7 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon, struct inode *inode = file_inode(file); struct cifsFileInfo *cfile = file->private_data; struct file_zero_data_information fsctl_buf; + unsigned long long end = offset + len, i_size, remote_i_size; long rc; unsigned int xid; __u8 set_sparse = 1; @@ -3336,6 +3337,29 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon, (char *)&fsctl_buf, sizeof(struct file_zero_data_information), CIFSMaxBufSize, NULL, NULL); + + if (rc) + goto unlock; + + /* If there's dirty data in the buffer that would extend the EOF if it + * were written, then we need to move the EOF marker over to the lower + * of the high end of the hole and the proposed EOF. The problem is + * that we locally hole-punch the tail of the dirty data, the proposed + * EOF update will end up in the wrong place. + */ + i_size = i_size_read(inode); + remote_i_size = netfs_inode(inode)->remote_i_size; + if (end > remote_i_size && i_size > remote_i_size) { + unsigned long long extend_to = umin(end, i_size); + rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid, + cfile->fid.volatile_fid, cfile->pid, extend_to); + if (rc >= 0) { + netfs_inode(inode)->remote_i_size = extend_to; + trace_netfs_set_size(inode, netfs_size_trace_punch_hole); + } + } + +unlock: filemap_invalidate_unlock(inode->i_mapping); out: inode_unlock(inode);