Skip to content

Commit 7b2404a

Browse files
dhowellssmfrench
authored andcommitted
cifs: Fix flushing, invalidation and file size with copy_file_range()
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: [email protected] Signed-off-by: David Howells <[email protected]> cc: Paulo Alcantara <[email protected]> cc: Shyam Prasad N <[email protected]> cc: Rohith Surabattula <[email protected]> cc: Matthew Wilcox <[email protected]> cc: Jeff Layton <[email protected]> cc: [email protected] cc: [email protected] Signed-off-by: David Howells <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 33cc938 commit 7b2404a

File tree

1 file changed

+99
-3
lines changed

1 file changed

+99
-3
lines changed

fs/smb/client/cifsfs.c

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,72 @@ const struct inode_operations cifs_symlink_inode_ops = {
11961196
.listxattr = cifs_listxattr,
11971197
};
11981198

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

12721340
cifs_dbg(FYI, "copychunk range\n");
@@ -1306,13 +1374,41 @@ ssize_t cifs_file_copychunk_range(unsigned int xid,
13061374
if (rc)
13071375
goto unlock;
13081376

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

13121405
rc = file_modified(dst_file);
1313-
if (!rc)
1406+
if (!rc) {
13141407
rc = target_tcon->ses->server->ops->copychunk_range(xid,
13151408
smb_file_src, smb_file_target, off, len, destoff);
1409+
if (rc > 0 && destoff + rc > i_size_read(target_inode))
1410+
truncate_setsize(target_inode, destoff + rc);
1411+
}
13161412

13171413
file_accessed(src_file);
13181414

0 commit comments

Comments
 (0)