Skip to contentSkip to conversation

cifs: prevent updating file size from server if we have a read/write lease

PATCH In review Bharath SM 3 participants 7 messages Feb 26, 2024 – Mar 11, 2024

Subsystems (2)
lore
Share & tools
lore

Bharath SM

author
#0 lore raw

PATCH 1/1 cifs: prevent updating file size from server if we have a read/write lease

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from server when a read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/client/file.c +2 -1
index f2db4a1f81ad..e742d0d0e579 100644
@@ -2952,7 +2952,8 @@ bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
 	if (!cifsInode)
 		return true;
 
-	if (is_inode_writable(cifsInode)) {
+	if (is_inode_writable(cifsInode) ||
+			((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0)) {
 		/* This inode is open for write at least once */
 		struct cifs_sb_info *cifs_sb;
 
Signature · 1 line
2.34.1
#1 lore raw

My only worry is that perhaps we should make it more narrow (ie only when called from readdir ie cifs_prime_dcache() rather than also never updating it on query_info calls)

Quoted 39 lines · Bharath SM

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from server when a read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/client/file.c +2 -2
index f2db4a1f81ad..e742d0d0e579 100644
@@ -2952,7 +2952,8 @@ bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
        if (!cifsInode)
                return true;
 
-       if (is_inode_writable(cifsInode)) {
+       if (is_inode_writable(cifsInode) ||
+                       ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0)) {
                /* This inode is open for write at least once */
                struct cifs_sb_info *cifs_sb;
 
--
Quoted 2 lines

2.34.1

--
Thanks,

Steve

author
#2 lore raw

Attached updated patch to have this fix only for calls from readdir i.e cifs_prime_dcache.

Quoted 52 lines · Steve French

My only worry is that perhaps we should make it more narrow (ie only when called from readdir ie cifs_prime_dcache() rather than also never updating it on query_info calls)

Quoted 39 lines · Bharath SM

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from server when a read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/client/file.c +2 -2
index f2db4a1f81ad..e742d0d0e579 100644
@@ -2952,7 +2952,8 @@ bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
        if (!cifsInode)
                return true;
 
-       if (is_inode_writable(cifsInode)) {
+       if (is_inode_writable(cifsInode) ||
+                       ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0)) {
                /* This inode is open for write at least once */
                struct cifs_sb_info *cifs_sb;
 
--
Quoted 2 lines

2.34.1

--
Thanks,

Quoted 1 line

Steve

From 1a23885cbeb49659f0cbcbfa2fa64174ff79f18b Mon Sep 17 00:00:00 2001
From: Bharath SM <[email protected]>
Date: Sun, 25 Feb 2024 21:48:07 +0530
Subject: [PATCH] cifs: prevent updating file size from server if we have a
read/write lease

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from readdir when we have read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/cifsproto.h |  6 ++++--
 fs/smb/client/file.c      |  8 +++++---
 fs/smb/client/inode.c     | 13 +++++++------
 fs/smb/client/readdir.c   |  2 +-
 4 files changed, 17 insertions(+), 12 deletions(-)
fs/smb/client/cifsproto.h +4 -2
index 1468df1ff47d..9602cf6c72a4 100644
@@ -144,7 +144,8 @@ extern int cifs_reconnect(struct TCP_Server_Info *server,
 extern int checkSMB(char *buf, unsigned int len, struct TCP_Server_Info *srvr);
 extern bool is_valid_oplock_break(char *, struct TCP_Server_Info *);
 extern bool backup_cred(struct cifs_sb_info *);
-extern bool is_size_safe_to_change(struct cifsInodeInfo *, __u64 eof);
+extern bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 eof,
+				   bool from_readdir);
 void cifs_write_subrequest_terminated(struct cifs_io_subrequest *wdata, ssize_t result,
 				      bool was_async);
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, int);
@@ -201,7 +202,8 @@ extern void cifs_unix_basic_to_fattr(struct cifs_fattr *fattr,
 				     struct cifs_sb_info *cifs_sb);
 extern void cifs_dir_info_to_fattr(struct cifs_fattr *, FILE_DIRECTORY_INFO *,
 					struct cifs_sb_info *);
-extern int cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr);
+extern int cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
+			       bool from_readdir);
 extern struct inode *cifs_iget(struct super_block *sb,
 			       struct cifs_fattr *fattr);
 
fs/smb/client/file.c +5 -3
index f2db4a1f81ad..bc320c567d61 100644
@@ -562,7 +562,7 @@ int cifs_posix_open(const char *full_path, struct inode **pinode,
 		}
 	} else {
 		cifs_revalidate_mapping(*pinode);
-		rc = cifs_fattr_to_inode(*pinode, &fattr);
+		rc = cifs_fattr_to_inode(*pinode, &fattr, false);
 	}
 
 posix_open_ret:
@@ -2947,12 +2947,14 @@ static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
    refreshing the inode only on increases in the file size
    but this is tricky to do without racing with writebehind
    page caching in the current Linux kernel design */
-bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
+bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
+			    bool from_readdir)
 {
 	if (!cifsInode)
 		return true;
 
-	if (is_inode_writable(cifsInode)) {
+	if (is_inode_writable(cifsInode) ||
+		((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) {
 		/* This inode is open for write at least once */
 		struct cifs_sb_info *cifs_sb;
 
fs/smb/client/inode.c +7 -6
index 7acaf75feabc..d1dea5333981 100644
@@ -163,7 +163,8 @@ cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 
 /* populate an inode with info from a cifs_fattr struct */
 int
-cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
+cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
+		    bool from_readdir)
 {
 	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
@@ -215,7 +216,7 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 	 * Can't safely change the file size here if the client is writing to
 	 * it due to potential races.
 	 */
-	if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
+	if (is_size_safe_to_change(cifs_i, fattr->cf_eof, from_readdir)) {
 		i_size_write(inode, fattr->cf_eof);
 
 		/*
@@ -386,7 +387,7 @@ static int update_inode_info(struct super_block *sb,
 		CIFS_I(*inode)->time = 0; /* force reval */
 		return -ESTALE;
 	}
-	return cifs_fattr_to_inode(*inode, fattr);
+	return cifs_fattr_to_inode(*inode, fattr, false);
 }
 
 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
@@ -421,7 +422,7 @@ cifs_get_file_info_unix(struct file *filp)
 	} else
 		goto cifs_gfiunix_out;
 
-	rc = cifs_fattr_to_inode(inode, &fattr);
+	rc = cifs_fattr_to_inode(inode, &fattr, false);
 
 cifs_gfiunix_out:
 	free_xid(xid);
@@ -873,7 +874,7 @@ cifs_get_file_info(struct file *filp)
 	fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
 	fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
 	/* if filetype is different, return error */
-	rc = cifs_fattr_to_inode(inode, &fattr);
+	rc = cifs_fattr_to_inode(inode, &fattr, false);
 cgfi_exit:
 	cifs_free_open_info(&data);
 	free_xid(xid);
@@ -1430,7 +1431,7 @@ cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
 		}
 
 		/* can't fail - see cifs_find_inode() */
-		cifs_fattr_to_inode(inode, fattr);
+		cifs_fattr_to_inode(inode, fattr, false);
 		if (sb->s_flags & SB_NOATIME)
 			inode->i_flags |= S_NOATIME | S_NOCMTIME;
 		if (inode->i_state & I_NEW) {
fs/smb/client/readdir.c +1 -1
index a8aee9148e89..ebe1cb30e18e 100644
@@ -134,7 +134,7 @@ cifs_prime_dcache(struct dentry *parent, struct qstr *name,
 						rc = -ESTALE;
 					}
 				}
-				if (!rc && !cifs_fattr_to_inode(inode, fattr)) {
+				if (!rc && !cifs_fattr_to_inode(inode, fattr, true)) {
 					dput(dentry);
 					return;
 				}
Signature · 1 line
2.34.1
author
#3 lore raw

minor update to resolve conflicts.
And Cc: [email protected]

Quoted 58 lines · Bharath SM

Attached updated patch to have this fix only for calls from readdir i.e cifs_prime_dcache.

Quoted 53 lines · Steve French

My only worry is that perhaps we should make it more narrow (ie only when called from readdir ie cifs_prime_dcache() rather than also never updating it on query_info calls)

Quoted 39 lines · Bharath SM

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from server when a read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/client/file.c +2 -2
index f2db4a1f81ad..e742d0d0e579 100644
@@ -2952,7 +2952,8 @@ bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
        if (!cifsInode)
                return true;
 
-       if (is_inode_writable(cifsInode)) {
+       if (is_inode_writable(cifsInode) ||
+                       ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0)) {
                /* This inode is open for write at least once */
                struct cifs_sb_info *cifs_sb;
 
--
Quoted 2 lines

2.34.1

--
Thanks,

Steve

author
#4 lore raw

Attached updated patch.

Quoted 63 lines · Bharath SM

minor update to resolve conflicts.
And Cc: [email protected]

Quoted 58 lines · Bharath SM

Attached updated patch to have this fix only for calls from readdir i.e cifs_prime_dcache.

Quoted 53 lines · Steve French

My only worry is that perhaps we should make it more narrow (ie only when called from readdir ie cifs_prime_dcache() rather than also never updating it on query_info calls)

Quoted 39 lines · Bharath SM

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from server when a read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/client/file.c +2 -2
index f2db4a1f81ad..e742d0d0e579 100644
@@ -2952,7 +2952,8 @@ bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
        if (!cifsInode)
                return true;
 
-       if (is_inode_writable(cifsInode)) {
+       if (is_inode_writable(cifsInode) ||
+                       ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0)) {
                /* This inode is open for write at least once */
                struct cifs_sb_info *cifs_sb;
 
--
Quoted 2 lines

2.34.1

--
Thanks,

Steve

From 5474dc566eb0a90f1eeaff801efe6dc5716b0417 Mon Sep 17 00:00:00 2001
From: Bharath SM <[email protected]>
Date: Thu, 29 Feb 2024 23:09:52 +0530
Subject: [PATCH] cifs: prevent updating file size from server if we have a
read/write lease

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from readdir when we have read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/cifsproto.h |  6 ++++--
 fs/smb/client/file.c      |  8 +++++---
 fs/smb/client/inode.c     | 13 +++++++------
 fs/smb/client/readdir.c   |  2 +-
 4 files changed, 17 insertions(+), 12 deletions(-)
fs/smb/client/cifsproto.h +4 -2
index a841bf4967fa..58cfbd450a55 100644
@@ -144,7 +144,8 @@ extern int cifs_reconnect(struct TCP_Server_Info *server,
 extern int checkSMB(char *buf, unsigned int len, struct TCP_Server_Info *srvr);
 extern bool is_valid_oplock_break(char *, struct TCP_Server_Info *);
 extern bool backup_cred(struct cifs_sb_info *);
-extern bool is_size_safe_to_change(struct cifsInodeInfo *, __u64 eof);
+extern bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 eof,
+				   bool from_readdir);
 extern void cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
 			    unsigned int bytes_written);
 extern struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *, int);
@@ -201,7 +202,8 @@ extern void cifs_unix_basic_to_fattr(struct cifs_fattr *fattr,
 				     struct cifs_sb_info *cifs_sb);
 extern void cifs_dir_info_to_fattr(struct cifs_fattr *, FILE_DIRECTORY_INFO *,
 					struct cifs_sb_info *);
-extern int cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr);
+extern int cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
+			       bool from_readdir);
 extern struct inode *cifs_iget(struct super_block *sb,
 			       struct cifs_fattr *fattr);
 
fs/smb/client/file.c +5 -3
index f391c9b803d8..9cff5f7dc062 100644
@@ -329,7 +329,7 @@ int cifs_posix_open(const char *full_path, struct inode **pinode,
 		}
 	} else {
 		cifs_revalidate_mapping(*pinode);
-		rc = cifs_fattr_to_inode(*pinode, &fattr);
+		rc = cifs_fattr_to_inode(*pinode, &fattr, false);
 	}
 
 posix_open_ret:
@@ -4738,12 +4738,14 @@ static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
    refreshing the inode only on increases in the file size
    but this is tricky to do without racing with writebehind
    page caching in the current Linux kernel design */
-bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
+bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
+			    bool from_readdir)
 {
 	if (!cifsInode)
 		return true;
 
-	if (is_inode_writable(cifsInode)) {
+	if (is_inode_writable(cifsInode) ||
+		((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) {
 		/* This inode is open for write at least once */
 		struct cifs_sb_info *cifs_sb;
 
fs/smb/client/inode.c +7 -6
index d02f8ba29cb5..7f28edf4b20f 100644
@@ -147,7 +147,8 @@ cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 
 /* populate an inode with info from a cifs_fattr struct */
 int
-cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
+cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
+		    bool from_readdir)
 {
 	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
@@ -199,7 +200,7 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 	 * Can't safely change the file size here if the client is writing to
 	 * it due to potential races.
 	 */
-	if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
+	if (is_size_safe_to_change(cifs_i, fattr->cf_eof, from_readdir)) {
 		i_size_write(inode, fattr->cf_eof);
 
 		/*
@@ -368,7 +369,7 @@ static int update_inode_info(struct super_block *sb,
 		CIFS_I(*inode)->time = 0; /* force reval */
 		return -ESTALE;
 	}
-	return cifs_fattr_to_inode(*inode, fattr);
+	return cifs_fattr_to_inode(*inode, fattr, false);
 }
 
 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
@@ -403,7 +404,7 @@ cifs_get_file_info_unix(struct file *filp)
 	} else
 		goto cifs_gfiunix_out;
 
-	rc = cifs_fattr_to_inode(inode, &fattr);
+	rc = cifs_fattr_to_inode(inode, &fattr, false);
 
 cifs_gfiunix_out:
 	free_xid(xid);
@@ -934,7 +935,7 @@ cifs_get_file_info(struct file *filp)
 	fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
 	fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
 	/* if filetype is different, return error */
-	rc = cifs_fattr_to_inode(inode, &fattr);
+	rc = cifs_fattr_to_inode(inode, &fattr, false);
 cgfi_exit:
 	cifs_free_open_info(&data);
 	free_xid(xid);
@@ -1491,7 +1492,7 @@ cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
 		}
 
 		/* can't fail - see cifs_find_inode() */
-		cifs_fattr_to_inode(inode, fattr);
+		cifs_fattr_to_inode(inode, fattr, false);
 		if (sb->s_flags & SB_NOATIME)
 			inode->i_flags |= S_NOATIME | S_NOCMTIME;
 		if (inode->i_state & I_NEW) {
fs/smb/client/readdir.c +1 -1
index b520eea7bfce..132ae7d884a9 100644
@@ -148,7 +148,7 @@ cifs_prime_dcache(struct dentry *parent, struct qstr *name,
 						rc = -ESTALE;
 					}
 				}
-				if (!rc && !cifs_fattr_to_inode(inode, fattr)) {
+				if (!rc && !cifs_fattr_to_inode(inode, fattr, true)) {
 					dput(dentry);
 					return;
 				}
Signature · 1 line
2.34.1
Reviewed-by
#5 lore raw
Quoted 67 lines · Bharath SM

Attached updated patch.

Quoted 63 lines · Bharath SM

minor update to resolve conflicts.
And Cc: [email protected]

Quoted 58 lines · Bharath SM

Attached updated patch to have this fix only for calls from readdir i.e cifs_prime_dcache.

Quoted 53 lines · Steve French

My only worry is that perhaps we should make it more narrow (ie only when called from readdir ie cifs_prime_dcache() rather than also never updating it on query_info calls)

Quoted 39 lines · Bharath SM

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from server when a read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/client/file.c +2 -2
index f2db4a1f81ad..e742d0d0e579 100644
@@ -2952,7 +2952,8 @@ bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
        if (!cifsInode)
                return true;
 
-       if (is_inode_writable(cifsInode)) {
+       if (is_inode_writable(cifsInode) ||
+                       ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0)) {
                /* This inode is open for write at least once */
                struct cifs_sb_info *cifs_sb;
 
--
Quoted 2 lines

2.34.1

--
Thanks,

Steve

Changes look mostly good.

Quoted 7 lines
Quoted 7 lines

return true;

- if (is_inode_writable(cifsInode)) {
+ if (is_inode_writable(cifsInode) ||
+ ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) { /* This inode is open for write at least once */
struct cifs_sb_info *cifs_sb;

Why not use CIFS_CACHE_READ(cifsInode) || CIFS_CACHE_WRITE(cifsInode) instead of just checking the flag?
That will cover other cache modes where attrs cannot change outside the client.

Once this change is done, you can add my RB.

Signature · 2 lines
Regards,
Shyam
#6 lore raw
Quoted 80 lines · Shyam Prasad N
Quoted 67 lines · Bharath SM

Attached updated patch.

Quoted 63 lines · Bharath SM

minor update to resolve conflicts.
And Cc: [email protected]

Quoted 58 lines · Bharath SM

Attached updated patch to have this fix only for calls from readdir i.e cifs_prime_dcache.

Quoted 53 lines · Steve French

My only worry is that perhaps we should make it more narrow (ie only when called from readdir ie cifs_prime_dcache() rather than also never updating it on query_info calls)

Quoted 39 lines · Bharath SM

In cases of large directories, the readdir operation may span multiple round trips to retrieve contents. This introduces a potential race condition in case of concurrent write and readdir operations. If the readdir operation initiates before a write has been processed by the server, it may update the file size attribute to an older value. Address this issue by avoiding file size updates from server when a read/write lease.

Scenario:
1) process1: open dir xyz
2) process1: readdir instance 1 on xyz
3) process2: create file.txt for write
4) process2: write x bytes to file.txt
5) process2: close file.txt
6) process2: open file.txt for read
7) process1: readdir 2 - overwrites file.txt inode size to 0
8) process2: read contents of file.txt - bug, short read with 0 bytes

---
 fs/smb/client/file.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/smb/client/file.c +2 -2
index f2db4a1f81ad..e742d0d0e579 100644
@@ -2952,7 +2952,8 @@ bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
        if (!cifsInode)
                return true;
 
-       if (is_inode_writable(cifsInode)) {
+       if (is_inode_writable(cifsInode) ||
+                       ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0)) {
                /* This inode is open for write at least once */
                struct cifs_sb_info *cifs_sb;
 
--
Quoted 2 lines

2.34.1

--
Thanks,

Steve

Changes look mostly good.

Quoted 7 lines
Quoted 7 lines

return true;

- if (is_inode_writable(cifsInode)) {
+ if (is_inode_writable(cifsInode) ||
+ ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) { /* This inode is open for write at least once */
struct cifs_sb_info *cifs_sb;

Quoted 3 lines

Why not use CIFS_CACHE_READ(cifsInode) || CIFS_CACHE_WRITE(cifsInode) instead of just checking the flag?
That will cover other cache modes where attrs cannot change outside the client.

I think Bharath's version is safer since if you did CIFS_CACHE_READ check you would be adding a case where you mounted read-only (and in that case it looks confusing to allow write size to be updated
locally). It may also be a little risky to allow this when no you
were not able to get a lease from the server and are mounting in "singleclient" mode.

Signature · 3 lines
Thanks,

Steve