From mboxrd@z Thu Jan 1 00:00:00 1970 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 8934FC4167B for ; Mon, 11 Dec 2023 18:52:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21C266B01A3; Mon, 11 Dec 2023 13:52:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1A67F6B01A4; Mon, 11 Dec 2023 13:52:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 046686B01A5; Mon, 11 Dec 2023 13:52:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E2F5D6B01A3 for ; Mon, 11 Dec 2023 13:52:46 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B49E31607A3 for ; Mon, 11 Dec 2023 18:52:46 +0000 (UTC) X-FDA: 81555433932.20.2A57260 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf20.hostedemail.com (Postfix) with ESMTP id 46E731C0012 for ; Mon, 11 Dec 2023 18:52:43 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=1NXVFx9X; dmarc=pass (policy=none) header.from=linuxfoundation.org; spf=pass (imf20.hostedemail.com: domain of gregkh@linuxfoundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702320763; 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=VcingBAz6KJVJk5ANBgvmOq6h+LmVN0TkzDdthvVst4=; b=o+ohi56LRqM7rwM4xQnAhvLXUiyHdan5LjBXfzjt89Nle8R+jMIA2PywzcL7EPR+IfS+qH n+p3zZUmeWyrHf61OSt3ED0bSGQ601XW6a2XRSJrTZuLm0BZVzkBASbuMlGt7+A9XBhNNB FMjm88xWNYFXH18lXlNLrzg2sybCqSM= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=1NXVFx9X; dmarc=pass (policy=none) header.from=linuxfoundation.org; spf=pass (imf20.hostedemail.com: domain of gregkh@linuxfoundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702320763; a=rsa-sha256; cv=none; b=rJIEIo10z0xC89PDChTXcyJQwfNzZmDqBepMROGg26ak2d6CHer0h2hhlpgi2gGWMRB+JM BpQheZAPvX338iHBXnaKs5B0ZmywdZn1dS4UZo3Jm8NJf6aCUAuh1KYXHhlpPZA+pESri8 /auNUnhLsJ1WccE/rPZdgciO+qX7pa0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 39457B80F79; Mon, 11 Dec 2023 18:52:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F65AC433C8; Mon, 11 Dec 2023 18:52:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1702320760; bh=Dae98sA+NJSuWPKFzhhxEfwIoYWG7wmJKBnEJjrqGSA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1NXVFx9XTpDe4Vql2zDN5JrNY3qBJBrjKnrbwYKleWeG3NhjLI/JZt/67QiydUOMT j+XJt+Hin0AHsipSIl9oW5It4QOKdIpXAVOfDXTgQcqbfQ+0Ysc6Ete2fOgRxXQCW5 LYy8d76E+QdK4x9zWWNK4zHi4YaXepxmmbQa9qD8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, David Howells , Paulo Alcantara , Shyam Prasad N , Rohith Surabattula , Matthew Wilcox , Jeff Layton , linux-cifs@vger.kernel.org, linux-mm@kvack.org, Steve French Subject: [PATCH 6.1 181/194] cifs: Fix flushing, invalidation and file size with copy_file_range() Date: Mon, 11 Dec 2023 19:22:51 +0100 Message-ID: <20231211182044.740126090@linuxfoundation.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231211182036.606660304@linuxfoundation.org> References: <20231211182036.606660304@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 46E731C0012 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: k4wxzu4h16hx3x4zte4otqrfanuyibny X-HE-Tag: 1702320763-128860 X-HE-Meta: U2FsdGVkX1/l6An+gJ9NccD+RO6JPHCFFcpRtiZiBBbTpV1cPFb2lCYfiqVD93zUmvZxy3H0oQv2EjTs8/0KJAc6Bu4FF+ZWyrpB/lDkyp2rwno3ztgOHnvn8R3V8ICPDsWd8Q2bDBKPGCU0+NuR5z0lNEDYQVyx83qNcZST6VJ5RgxgieqdUOCowYSVpl4skOADfMEBM7dJbSRiRcHjpV2JlycNyIXfhyr2LnFuqRNjolHFulVfOEpjgNWctyanxjpN/ovSaqjKunuhinJ2LK+hjgUqO6vY52ef4q4YDliUZVCqe4pKNBwQfHQGeJoYzy0k+hlZNE4q2j38V3AnRPC5W6GhWUVITaxNNm1OlfSqEUL7Ht5c8zpHli/L9l2DKF4rUIC+M4/MwmGjM9WJE1HXcwGajBnCC5pH91ZqyRN3iEVjNsLZwL9IFyOAlkj71T99LJaR2b4sqVZgO1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5tdzzMaQYl7Xpu2YXpr++JmdxOtHq3wkn/8EDwPRGbtPgT8eccefXcJg3LTL8JvnOdfINYh5T9kPX/0AA1HMUjz3/mmQo2ziSzPU06KVOjdKmuaXxQo2vCRXT5Zz024c2qpxbG1+Y0pBhtTRvqhWT4IYG8 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: 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Howells commit 7b2404a886f8b91250c31855d287e632123e1746 upstream. Fix a number of issues in the cifs filesystem implementation of the copy_file_range() syscall in cifs_file_copychunk_range(). Firstly, the invalidation of the destination range is handled incorrectly: We shouldn't just invalidate the whole file as dirty data in the file may get lost and we can't just call truncate_inode_pages_range() to invalidate the destination range as that will erase parts of a partial folio at each end whilst invalidating and discarding all the folios in the middle. We need to force all the folios covering the range to be reloaded, but we mustn't lose dirty data in them that's not in the destination range. Further, we shouldn't simply round out the range to PAGE_SIZE at each end as cifs should move to support multipage folios. Secondly, there's an issue whereby a write may have extended the file locally, but not have been written back yet. This can leaves the local idea of the EOF at a later point than the server's EOF. If a copy request is issued, this will fail on the server with STATUS_INVALID_VIEW_SIZE (which gets translated to -EIO locally) if the copy source extends past the server's EOF. Fix this by: (0) Flush the source region (already done). The flush does nothing and the EOF isn't moved if the source region has no dirty data. (1) Move the EOF to the end of the source region if it isn't already at least at this point. If we can't do this, for instance if the server doesn't support it, just flush the entire source file. (2) Find the folio (if present) at each end of the range, flushing it and increasing the region-to-be-invalidated to cover those in their entirety. (3) Fully discard all the folios covering the range as we want them to be reloaded. (4) Then perform the copy. Thirdly, set i_size after doing the copychunk_range operation as this value may be used by various things internally. stat() hides the issue because setting ->time to 0 causes cifs_getatr() to revalidate the attributes. These were causing the generic/075 xfstest to fail. Fixes: 620d8745b35d ("Introduce cifs_copy_file_range()") Cc: stable@vger.kernel.org Signed-off-by: David Howells cc: Paulo Alcantara cc: Shyam Prasad N cc: Rohith Surabattula cc: Matthew Wilcox cc: Jeff Layton cc: linux-cifs@vger.kernel.org cc: linux-mm@kvack.org Signed-off-by: David Howells Signed-off-by: Steve French Signed-off-by: Greg Kroah-Hartman --- fs/smb/client/cifsfs.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 3 deletions(-) --- a/fs/smb/client/cifsfs.c +++ b/fs/smb/client/cifsfs.c @@ -1191,6 +1191,72 @@ const struct inode_operations cifs_symli .listxattr = cifs_listxattr, }; +/* + * Advance the EOF marker to after the source range. + */ +static int cifs_precopy_set_eof(struct inode *src_inode, struct cifsInodeInfo *src_cifsi, + struct cifs_tcon *src_tcon, + unsigned int xid, loff_t src_end) +{ + struct cifsFileInfo *writeable_srcfile; + int rc = -EINVAL; + + writeable_srcfile = find_writable_file(src_cifsi, FIND_WR_FSUID_ONLY); + if (writeable_srcfile) { + if (src_tcon->ses->server->ops->set_file_size) + rc = src_tcon->ses->server->ops->set_file_size( + xid, src_tcon, writeable_srcfile, + src_inode->i_size, true /* no need to set sparse */); + else + rc = -ENOSYS; + cifsFileInfo_put(writeable_srcfile); + cifs_dbg(FYI, "SetFSize for copychunk rc = %d\n", rc); + } + + if (rc < 0) + goto set_failed; + + netfs_resize_file(&src_cifsi->netfs, src_end); + fscache_resize_cookie(cifs_inode_cookie(src_inode), src_end); + return 0; + +set_failed: + return filemap_write_and_wait(src_inode->i_mapping); +} + +/* + * Flush out either the folio that overlaps the beginning of a range in which + * pos resides or the folio that overlaps the end of a range unless that folio + * is entirely within the range we're going to invalidate. We extend the flush + * bounds to encompass the folio. + */ +static int cifs_flush_folio(struct inode *inode, loff_t pos, loff_t *_fstart, loff_t *_fend, + bool first) +{ + struct folio *folio; + unsigned long long fpos, fend; + pgoff_t index = pos / PAGE_SIZE; + size_t size; + int rc = 0; + + folio = filemap_get_folio(inode->i_mapping, index); + if (IS_ERR(folio)) + return 0; + + size = folio_size(folio); + fpos = folio_pos(folio); + fend = fpos + size - 1; + *_fstart = min_t(unsigned long long, *_fstart, fpos); + *_fend = max_t(unsigned long long, *_fend, fend); + if ((first && pos == fpos) || (!first && pos == fend)) + goto out; + + rc = filemap_write_and_wait_range(inode->i_mapping, fpos, fend); +out: + folio_put(folio); + return rc; +} + static loff_t cifs_remap_file_range(struct file *src_file, loff_t off, struct file *dst_file, loff_t destoff, loff_t len, unsigned int remap_flags) @@ -1260,10 +1326,12 @@ ssize_t cifs_file_copychunk_range(unsign { struct inode *src_inode = file_inode(src_file); struct inode *target_inode = file_inode(dst_file); + struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode); struct cifsFileInfo *smb_file_src; struct cifsFileInfo *smb_file_target; struct cifs_tcon *src_tcon; struct cifs_tcon *target_tcon; + unsigned long long destend, fstart, fend; ssize_t rc; cifs_dbg(FYI, "copychunk range\n"); @@ -1303,13 +1371,41 @@ ssize_t cifs_file_copychunk_range(unsign if (rc) goto unlock; - /* should we flush first and last page first */ - truncate_inode_pages(&target_inode->i_data, 0); + /* The server-side copy will fail if the source crosses the EOF marker. + * Advance the EOF marker after the flush above to the end of the range + * if it's short of that. + */ + if (src_cifsi->server_eof < off + len) { + rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len); + if (rc < 0) + goto unlock; + } + + destend = destoff + len - 1; + + /* Flush the folios at either end of the destination range to prevent + * accidental loss of dirty data outside of the range. + */ + fstart = destoff; + fend = destend; + + rc = cifs_flush_folio(target_inode, destoff, &fstart, &fend, true); + if (rc) + goto unlock; + rc = cifs_flush_folio(target_inode, destend, &fstart, &fend, false); + if (rc) + goto unlock; + + /* Discard all the folios that overlap the destination region. */ + truncate_inode_pages_range(&target_inode->i_data, fstart, fend); rc = file_modified(dst_file); - if (!rc) + if (!rc) { rc = target_tcon->ses->server->ops->copychunk_range(xid, smb_file_src, smb_file_target, off, len, destoff); + if (rc > 0 && destoff + rc > i_size_read(target_inode)) + truncate_setsize(target_inode, destoff + rc); + } file_accessed(src_file);