Skip to content

Commit 18b02e4

Browse files
dhowellsgregkh
authored andcommitted
cifs: Fix flushing, invalidation and file size with copy_file_range()
commit 7b2404a 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: 620d874 ("Introduce cifs_copy_file_range()") Cc: stable@vger.kernel.org Signed-off-by: David Howells <dhowells@redhat.com> cc: Paulo Alcantara <pc@manguebit.com> cc: Shyam Prasad N <nspmangalore@gmail.com> cc: Rohith Surabattula <rohiths.msft@gmail.com> cc: Matthew Wilcox <willy@infradead.org> cc: Jeff Layton <jlayton@kernel.org> cc: linux-cifs@vger.kernel.org cc: linux-mm@kvack.org Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Steve French <stfrench@microsoft.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 69540c1 commit 18b02e4

1 file changed

Lines changed: 99 additions & 3 deletions

File tree

fs/smb/client/cifsfs.c

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,72 @@ const struct inode_operations cifs_symlink_inode_ops = {
11911191
.listxattr = cifs_listxattr,
11921192
};
11931193

1194+
/*
1195+
* Advance the EOF marker to after the source range.
1196+
*/
1197+
static int cifs_precopy_set_eof(struct inode *src_inode, struct cifsInodeInfo *src_cifsi,
1198+
struct cifs_tcon *src_tcon,
1199+
unsigned int xid, loff_t src_end)
1200+
{
1201+
struct cifsFileInfo *writeable_srcfile;
1202+
int rc = -EINVAL;
1203+
1204+
writeable_srcfile = find_writable_file(src_cifsi, FIND_WR_FSUID_ONLY);
1205+
if (writeable_srcfile) {
1206+
if (src_tcon->ses->server->ops->set_file_size)
1207+
rc = src_tcon->ses->server->ops->set_file_size(
1208+
xid, src_tcon, writeable_srcfile,
1209+
src_inode->i_size, true /* no need to set sparse */);
1210+
else
1211+
rc = -ENOSYS;
1212+
cifsFileInfo_put(writeable_srcfile);
1213+
cifs_dbg(FYI, "SetFSize for copychunk rc = %d\n", rc);
1214+
}
1215+
1216+
if (rc < 0)
1217+
goto set_failed;
1218+
1219+
netfs_resize_file(&src_cifsi->netfs, src_end);
1220+
fscache_resize_cookie(cifs_inode_cookie(src_inode), src_end);
1221+
return 0;
1222+
1223+
set_failed:
1224+
return filemap_write_and_wait(src_inode->i_mapping);
1225+
}
1226+
1227+
/*
1228+
* Flush out either the folio that overlaps the beginning of a range in which
1229+
* pos resides or the folio that overlaps the end of a range unless that folio
1230+
* is entirely within the range we're going to invalidate. We extend the flush
1231+
* bounds to encompass the folio.
1232+
*/
1233+
static int cifs_flush_folio(struct inode *inode, loff_t pos, loff_t *_fstart, loff_t *_fend,
1234+
bool first)
1235+
{
1236+
struct folio *folio;
1237+
unsigned long long fpos, fend;
1238+
pgoff_t index = pos / PAGE_SIZE;
1239+
size_t size;
1240+
int rc = 0;
1241+
1242+
folio = filemap_get_folio(inode->i_mapping, index);
1243+
if (IS_ERR(folio))
1244+
return 0;
1245+
1246+
size = folio_size(folio);
1247+
fpos = folio_pos(folio);
1248+
fend = fpos + size - 1;
1249+
*_fstart = min_t(unsigned long long, *_fstart, fpos);
1250+
*_fend = max_t(unsigned long long, *_fend, fend);
1251+
if ((first && pos == fpos) || (!first && pos == fend))
1252+
goto out;
1253+
1254+
rc = filemap_write_and_wait_range(inode->i_mapping, fpos, fend);
1255+
out:
1256+
folio_put(folio);
1257+
return rc;
1258+
}
1259+
11941260
static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
11951261
struct file *dst_file, loff_t destoff, loff_t len,
11961262
unsigned int remap_flags)
@@ -1260,10 +1326,12 @@ ssize_t cifs_file_copychunk_range(unsigned int xid,
12601326
{
12611327
struct inode *src_inode = file_inode(src_file);
12621328
struct inode *target_inode = file_inode(dst_file);
1329+
struct cifsInodeInfo *src_cifsi = CIFS_I(src_inode);
12631330
struct cifsFileInfo *smb_file_src;
12641331
struct cifsFileInfo *smb_file_target;
12651332
struct cifs_tcon *src_tcon;
12661333
struct cifs_tcon *target_tcon;
1334+
unsigned long long destend, fstart, fend;
12671335
ssize_t rc;
12681336

12691337
cifs_dbg(FYI, "copychunk range\n");
@@ -1303,13 +1371,41 @@ ssize_t cifs_file_copychunk_range(unsigned int xid,
13031371
if (rc)
13041372
goto unlock;
13051373

1306-
/* should we flush first and last page first */
1307-
truncate_inode_pages(&target_inode->i_data, 0);
1374+
/* The server-side copy will fail if the source crosses the EOF marker.
1375+
* Advance the EOF marker after the flush above to the end of the range
1376+
* if it's short of that.
1377+
*/
1378+
if (src_cifsi->server_eof < off + len) {
1379+
rc = cifs_precopy_set_eof(src_inode, src_cifsi, src_tcon, xid, off + len);
1380+
if (rc < 0)
1381+
goto unlock;
1382+
}
1383+
1384+
destend = destoff + len - 1;
1385+
1386+
/* Flush the folios at either end of the destination range to prevent
1387+
* accidental loss of dirty data outside of the range.
1388+
*/
1389+
fstart = destoff;
1390+
fend = destend;
1391+
1392+
rc = cifs_flush_folio(target_inode, destoff, &fstart, &fend, true);
1393+
if (rc)
1394+
goto unlock;
1395+
rc = cifs_flush_folio(target_inode, destend, &fstart, &fend, false);
1396+
if (rc)
1397+
goto unlock;
1398+
1399+
/* Discard all the folios that overlap the destination region. */
1400+
truncate_inode_pages_range(&target_inode->i_data, fstart, fend);
13081401

13091402
rc = file_modified(dst_file);
1310-
if (!rc)
1403+
if (!rc) {
13111404
rc = target_tcon->ses->server->ops->copychunk_range(xid,
13121405
smb_file_src, smb_file_target, off, len, destoff);
1406+
if (rc > 0 && destoff + rc > i_size_read(target_inode))
1407+
truncate_setsize(target_inode, destoff + rc);
1408+
}
13131409

13141410
file_accessed(src_file);
13151411

0 commit comments

Comments
 (0)