Skip to content

Commit dba9f99

Browse files
pcacjrsmfrench
authored andcommitted
smb: client: fix race with fallocate(2) and AIO+DIO
AIO+DIO may extend the file size, hence we need to make sure ->i_size is stable across the entire fallocate(2) operation, otherwise it would become a truncate and then inode size reduced back down when it finishes. Fix this by calling netfs_wait_for_outstanding_io() right after acquiring ->i_rwsem exclusively in cifs_fallocate() and then guarantee a stable ->i_size across fallocate(2). Also call netfs_wait_for_outstanding_io() after truncating pagecache to avoid any potential races with writeback. Signed-off-by: Paulo Alcantara (Red Hat) <[email protected]> Reviewed-by: David Howells <[email protected]> Fixes: 210627b ("smb: client: fix missing timestamp updates with O_TRUNC") Cc: Frank Sorenson <[email protected]> Cc: [email protected] Signed-off-by: Steve French <[email protected]>
1 parent b95cd1b commit dba9f99

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

fs/smb/client/cifsfs.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,27 @@ static long cifs_fallocate(struct file *file, int mode, loff_t off, loff_t len)
392392
struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
393393
struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
394394
struct TCP_Server_Info *server = tcon->ses->server;
395+
struct inode *inode = file_inode(file);
396+
int rc;
397+
398+
if (!server->ops->fallocate)
399+
return -EOPNOTSUPP;
400+
401+
rc = inode_lock_killable(inode);
402+
if (rc)
403+
return rc;
404+
405+
netfs_wait_for_outstanding_io(inode);
395406

396-
if (server->ops->fallocate)
397-
return server->ops->fallocate(file, tcon, mode, off, len);
407+
rc = file_modified(file);
408+
if (rc)
409+
goto out_unlock;
410+
411+
rc = server->ops->fallocate(file, tcon, mode, off, len);
398412

399-
return -EOPNOTSUPP;
413+
out_unlock:
414+
inode_unlock(inode);
415+
return rc;
400416
}
401417

402418
static int cifs_permission(struct mnt_idmap *idmap,

fs/smb/client/inode.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,6 +3012,7 @@ void cifs_setsize(struct inode *inode, loff_t offset)
30123012
spin_unlock(&inode->i_lock);
30133013
inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
30143014
truncate_pagecache(inode, offset);
3015+
netfs_wait_for_outstanding_io(inode);
30153016
}
30163017

30173018
int cifs_file_set_size(const unsigned int xid, struct dentry *dentry,

fs/smb/client/smb2ops.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,7 +3367,6 @@ static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
33673367
trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
33683368
ses->Suid, offset, len);
33693369

3370-
inode_lock(inode);
33713370
filemap_invalidate_lock(inode->i_mapping);
33723371

33733372
i_size = i_size_read(inode);
@@ -3385,6 +3384,7 @@ static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
33853384
* first, otherwise the data may be inconsistent with the server.
33863385
*/
33873386
truncate_pagecache_range(inode, offset, offset + len - 1);
3387+
netfs_wait_for_outstanding_io(inode);
33883388

33893389
/* if file not oplocked can't be sure whether asking to extend size */
33903390
rc = -EOPNOTSUPP;
@@ -3413,7 +3413,6 @@ static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
34133413

34143414
zero_range_exit:
34153415
filemap_invalidate_unlock(inode->i_mapping);
3416-
inode_unlock(inode);
34173416
free_xid(xid);
34183417
if (rc)
34193418
trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
@@ -3437,7 +3436,6 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
34373436

34383437
xid = get_xid();
34393438

3440-
inode_lock(inode);
34413439
/* Need to make file sparse, if not already, before freeing range. */
34423440
/* Consider adding equivalent for compressed since it could also work */
34433441
if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
@@ -3451,6 +3449,7 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
34513449
* caches first, otherwise the data may be inconsistent with the server.
34523450
*/
34533451
truncate_pagecache_range(inode, offset, offset + len - 1);
3452+
netfs_wait_for_outstanding_io(inode);
34543453

34553454
cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
34563455

@@ -3485,7 +3484,6 @@ static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
34853484
unlock:
34863485
filemap_invalidate_unlock(inode->i_mapping);
34873486
out:
3488-
inode_unlock(inode);
34893487
free_xid(xid);
34903488
return rc;
34913489
}
@@ -3749,8 +3747,6 @@ static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
37493747

37503748
xid = get_xid();
37513749

3752-
inode_lock(inode);
3753-
37543750
old_eof = i_size_read(inode);
37553751
if ((off >= old_eof) ||
37563752
off + len >= old_eof) {
@@ -3765,6 +3761,7 @@ static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
37653761

37663762
truncate_pagecache_range(inode, off, old_eof);
37673763
ictx->zero_point = old_eof;
3764+
netfs_wait_for_outstanding_io(inode);
37683765

37693766
rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
37703767
old_eof - off - len, off);
@@ -3785,8 +3782,7 @@ static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
37853782
fscache_resize_cookie(cifs_inode_cookie(inode), new_eof);
37863783
out_2:
37873784
filemap_invalidate_unlock(inode->i_mapping);
3788-
out:
3789-
inode_unlock(inode);
3785+
out:
37903786
free_xid(xid);
37913787
return rc;
37923788
}
@@ -3803,8 +3799,6 @@ static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
38033799

38043800
xid = get_xid();
38053801

3806-
inode_lock(inode);
3807-
38083802
old_eof = i_size_read(inode);
38093803
if (off >= old_eof) {
38103804
rc = -EINVAL;
@@ -3819,6 +3813,7 @@ static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
38193813
if (rc < 0)
38203814
goto out_2;
38213815
truncate_pagecache_range(inode, off, old_eof);
3816+
netfs_wait_for_outstanding_io(inode);
38223817

38233818
rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
38243819
cfile->fid.volatile_fid, cfile->pid, new_eof);
@@ -3841,8 +3836,7 @@ static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
38413836
rc = 0;
38423837
out_2:
38433838
filemap_invalidate_unlock(inode->i_mapping);
3844-
out:
3845-
inode_unlock(inode);
3839+
out:
38463840
free_xid(xid);
38473841
return rc;
38483842
}

0 commit comments

Comments
 (0)