Skip to content

Commit 5655b8b

Browse files
smfrenchgregkh
authored andcommitted
smb3: do not error on fsync when readonly
[ Upstream commit 71e6864 ] Linux allows doing a flush/fsync on a file open for read-only, but the protocol does not allow that. If the file passed in on the flush is read-only try to find a writeable handle for the same inode, if that is not possible skip sending the fsync call to the server to avoid breaking the apps. Reported-by: Julian Sikorski <belegdol@gmail.com> Tested-by: Julian Sikorski <belegdol@gmail.com> Suggested-by: Jeremy Allison <jra@samba.org> Reviewed-by: Paulo Alcantara (SUSE) <pc@cjr.nz> Signed-off-by: Steve French <stfrench@microsoft.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c380062 commit 5655b8b

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

‎fs/cifs/file.c‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,12 +2618,23 @@ int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
26182618
tcon = tlink_tcon(smbfile->tlink);
26192619
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
26202620
server = tcon->ses->server;
2621-
if (server->ops->flush)
2622-
rc = server->ops->flush(xid, tcon, &smbfile->fid);
2623-
else
2621+
if (server->ops->flush == NULL) {
26242622
rc = -ENOSYS;
2623+
goto strict_fsync_exit;
2624+
}
2625+
2626+
if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
2627+
smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
2628+
if (smbfile) {
2629+
rc = server->ops->flush(xid, tcon, &smbfile->fid);
2630+
cifsFileInfo_put(smbfile);
2631+
} else
2632+
cifs_dbg(FYI, "ignore fsync for file not open for write\n");
2633+
} else
2634+
rc = server->ops->flush(xid, tcon, &smbfile->fid);
26252635
}
26262636

2637+
strict_fsync_exit:
26272638
free_xid(xid);
26282639
return rc;
26292640
}
@@ -2635,6 +2646,7 @@ int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
26352646
struct cifs_tcon *tcon;
26362647
struct TCP_Server_Info *server;
26372648
struct cifsFileInfo *smbfile = file->private_data;
2649+
struct inode *inode = file_inode(file);
26382650
struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
26392651

26402652
rc = file_write_and_wait_range(file, start, end);
@@ -2651,12 +2663,23 @@ int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
26512663
tcon = tlink_tcon(smbfile->tlink);
26522664
if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
26532665
server = tcon->ses->server;
2654-
if (server->ops->flush)
2655-
rc = server->ops->flush(xid, tcon, &smbfile->fid);
2656-
else
2666+
if (server->ops->flush == NULL) {
26572667
rc = -ENOSYS;
2668+
goto fsync_exit;
2669+
}
2670+
2671+
if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
2672+
smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
2673+
if (smbfile) {
2674+
rc = server->ops->flush(xid, tcon, &smbfile->fid);
2675+
cifsFileInfo_put(smbfile);
2676+
} else
2677+
cifs_dbg(FYI, "ignore fsync for file not open for write\n");
2678+
} else
2679+
rc = server->ops->flush(xid, tcon, &smbfile->fid);
26582680
}
26592681

2682+
fsync_exit:
26602683
free_xid(xid);
26612684
return rc;
26622685
}

0 commit comments

Comments
 (0)