Skip to contentSkip to conversation

cifs: do not attempt unlink-and-retry-rename for SMB2+ mounts

PATCH In review Ronnie Sahlberg 3 participants 4 messages Aug 5, 2019 – Aug 7, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cifs: do not attempt unlink-and-retry-rename for SMB2+ mounts

Normally in smb you can not rename ontop of a destination that is held open except in SMB1 where this is allowed IFF the delete-on-close flag is also set. This special case is not supported in SMB2 so should not attempt the unlink-and-try-again since the rename will still fail but we now also delete the destination file.

---
 fs/cifs/inode.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/cifs/inode.c +10 -0
index 56ca4b8ccaba..fdea45267a39 100644
@@ -1777,6 +1777,7 @@ cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
 	FILE_UNIX_BASIC_INFO *info_buf_target;
 	unsigned int xid;
 	int rc, tmprc;
+	struct TCP_Server_Info *server;
 
 	if (flags & ~RENAME_NOREPLACE)
 		return -EINVAL;
@@ -1786,6 +1787,7 @@ cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
 	if (IS_ERR(tlink))
 		return PTR_ERR(tlink);
 	tcon = tlink_tcon(tlink);
+	server = tcon->ses->server;
 
 	xid = get_xid();
 
@@ -1809,6 +1811,14 @@ cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
 			    to_name);
 
 	/*
+	 * Do not attempt unlink-then-try-rename-again for SMB2+.
+	 * Renaming ontop of an existing open file IF the delete-on-close
+	 * flag is set is only supported for SMB1.
+	 */
+	if (rc == -EACCES && server->vals->protocol_id != 0)
+		goto cifs_rename_exit;
+
+	/*
 	 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
 	 */
 	if (flags & RENAME_NOREPLACE)
Signature · 1 line
2.13.6
#1 lore raw
Quoted 10 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Ronnie Sahlberg
Sent: Monday, August 5, 2019 6:47 PM
To: linux-cifs <[email protected]>
Cc: Steve French <[email protected]>; Ronnie Sahlberg <[email protected]>
Subject: [PATCH] cifs: do not attempt unlink-and-retry-rename for SMB2+ mounts

Quoted 2 lines

Normally in smb you can not rename ontop of a destination that is held open except in SMB1 where this is allowed IFF the delete-on-close flag is also set.

The FILE_DELETE_ON_CLOSE flag doesn't really have any significance in the protocol, it is passed to the underlying filesystem except in one special case where the server validates that the DELETE or GENERIC is granted on the share. Did you find some reference in the documents to the contrary?

In other words, I think the fix may be ok, but the server->vals->protocol_id != 0 conditional may need more thought.

Tom.

Quoted 49 lines

This special case is not supported in SMB2 so should not attempt the unlink- and-try-again
since the rename will still fail but we now also delete the destination file.

---
 fs/cifs/inode.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/cifs/inode.c +1 -0
index 56ca4b8ccaba..fdea45267a39 100644
@@ -1777,6 +1777,7 @@ cifs_rename2(struct inode *source_dir, struct dentry
 *source_dentry,
 	FILE_UNIX_BASIC_INFO *info_buf_target;
 	unsigned int xid;
 	int rc, tmprc;
+	struct TCP_Server_Info *server;
 
 	if (flags & ~RENAME_NOREPLACE)
fs/cifs/inode.c +9 -0
 		return -EINVAL;
@@ -1786,6 +1787,7 @@ cifs_rename2(struct inode *source_dir, struct dentry
 *source_dentry,
 	if (IS_ERR(tlink))
 		return PTR_ERR(tlink);
 	tcon = tlink_tcon(tlink);
+	server = tcon->ses->server;
 
 	xid = get_xid();
@@ -1809,6 +1811,14 @@ cifs_rename2(struct inode *source_dir, struct
 dentry *source_dentry,
 			    to_name);
 
 	/*
+	 * Do not attempt unlink-then-try-rename-again for SMB2+.
+	 * Renaming ontop of an existing open file IF the delete-on-close
+	 * flag is set is only supported for SMB1.
+	 */
+	if (rc == -EACCES && server->vals->protocol_id != 0)
+		goto cifs_rename_exit;
+
+	/*
 	 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
 	 */

if (flags & RENAME_NOREPLACE)
--
2.13.6

#2 lore raw
Quoted 11 lines · Tom Talpey
Quoted 11 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Ronnie Sahlberg
Sent: Monday, August 5, 2019 6:47 PM
To: linux-cifs <[email protected]>
Cc: Steve French <[email protected]>; Ronnie Sahlberg <[email protected]>
Subject: [PATCH] cifs: do not attempt unlink-and-retry-rename for SMB2+ mounts

Quoted 8 lines
Quoted 2 lines

Normally in smb you can not rename ontop of a destination that is held open except in SMB1 where this is allowed IFF the delete-on-close flag is also set.

The FILE_DELETE_ON_CLOSE flag doesn't really have any significance in the protocol, it is passed to the underlying filesystem except in one special case where the server validates that the DELETE or GENERIC is granted on the share. Did you find some reference in the documents to the contrary?

No but emperical testing, and xfstests, show it. It does not just affect rename() but the same thing happens with "create and replace an existing file".

There is definitely a conditional somewhere in the SMB server that affects the behaviour or the delete-on-close flag.

This particular scenario is for the behaviour when we try to rename onto an existing file that have open handles.
The process used in cifs.ko is :

1, Try to rename the file. This fails with ACCESS_DENIED if there are open handles
if this fails, then fallback to
2, set delete-on-close
3, try the rename again

In SMB1, this works. In step 3 the file is renamed EVEN if there are still open handles to it.
In SMB2+ this fails. If there are open handles then this fails with DELETE_PENDING and the rename will not happen.
(then sometime later when the other process closes its handle then the file is deleted)

This still happens in windows 2016. Delete-on-close and other similar unlink() related operations
behave differently between SMB1 and SMB2.

Quoted 103 lines

In other words, I think the fix may be ok, but the server->vals->protocol_id != 0 conditional may need more thought.

Tom.

Quoted 98 lines

This special case is not supported in SMB2 so should not attempt the unlink- and-try-again
since the rename will still fail but we now also delete the destination file.

---
 fs/cifs/inode.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/cifs/inode.c +1 -0
index 56ca4b8ccaba..fdea45267a39 100644
@@ -1777,6 +1777,7 @@ cifs_rename2(struct inode *source_dir, struct dentry
 *source_dentry,
      FILE_UNIX_BASIC_INFO *info_buf_target;
      unsigned int xid;
      int rc, tmprc;
+     struct TCP_Server_Info *server;
 
      if (flags & ~RENAME_NOREPLACE)
              return -EINVAL;
fs/cifs/inode.c +9 -0
@@ -1786,6 +1787,7 @@ cifs_rename2(struct inode *source_dir, struct dentry
 *source_dentry,
      if (IS_ERR(tlink))
              return PTR_ERR(tlink);
      tcon = tlink_tcon(tlink);
+     server = tcon->ses->server;
 
      xid = get_xid();
@@ -1809,6 +1811,14 @@ cifs_rename2(struct inode *source_dir, struct
 dentry *source_dentry,
                          to_name);
 
      /*
+      * Do not attempt unlink-then-try-rename-again for SMB2+.
+      * Renaming ontop of an existing open file IF the delete-on-close
+      * flag is set is only supported for SMB1.
+      */
+     if (rc == -EACCES && server->vals->protocol_id != 0)
+             goto cifs_rename_exit;
+
+     /*
       * No-replace is the natural behavior for CIFS, so skip unlink hacks.
       */

if (flags & RENAME_NOREPLACE)
--
2.13.6
This special case is not supported in SMB2 so should not attempt the unlink- and-try-again
since the rename will still fail but we now also delete the destination file.

---
 fs/cifs/inode.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/cifs/inode.c +1 -0
index 56ca4b8ccaba..fdea45267a39 100644
@@ -1777,6 +1777,7 @@ cifs_rename2(struct inode *source_dir, struct dentry
 *source_dentry,
      FILE_UNIX_BASIC_INFO *info_buf_target;
      unsigned int xid;
      int rc, tmprc;
+     struct TCP_Server_Info *server;
 
      if (flags & ~RENAME_NOREPLACE)
              return -EINVAL;
fs/cifs/inode.c +9 -0
@@ -1786,6 +1787,7 @@ cifs_rename2(struct inode *source_dir, struct dentry
 *source_dentry,
      if (IS_ERR(tlink))
              return PTR_ERR(tlink);
      tcon = tlink_tcon(tlink);
+     server = tcon->ses->server;
 
      xid = get_xid();
@@ -1809,6 +1811,14 @@ cifs_rename2(struct inode *source_dir, struct
 dentry *source_dentry,
                          to_name);
 
      /*
+      * Do not attempt unlink-then-try-rename-again for SMB2+.
+      * Renaming ontop of an existing open file IF the delete-on-close
+      * flag is set is only supported for SMB1.
+      */
+     if (rc == -EACCES && server->vals->protocol_id != 0)
+             goto cifs_rename_exit;
+
+     /*
       * No-replace is the natural behavior for CIFS, so skip unlink hacks.
       */

if (flags & RENAME_NOREPLACE)
--
2.13.6

#3 lore raw
Quoted 57 lines

-----Original Message-----
From: ronnie sahlberg <[email protected]>
Sent: Tuesday, August 6, 2019 6:00 PM
To: Tom Talpey <[email protected]>
Cc: Ronnie Sahlberg <[email protected]>; linux-cifs <linux- [email protected]>; Steve French <[email protected]>
Subject: Re: [PATCH] cifs: do not attempt unlink-and-retry-rename for SMB2+ mounts

Quoted 3 lines · Tom Talpey
Quoted 3 lines

-----Original Message-----
From: [email protected] <[email protected]>

On

Quoted 9 lines
Quoted 9 lines

Behalf Of Ronnie Sahlberg
Sent: Monday, August 5, 2019 6:47 PM
To: linux-cifs <[email protected]>
Cc: Steve French <[email protected]>; Ronnie Sahlberg <[email protected]>
Subject: [PATCH] cifs: do not attempt unlink-and-retry-rename for SMB2+ mounts

Normally in smb you can not rename ontop of a destination that is held

open

Quoted 7 lines
Quoted 1 line

except in SMB1 where this is allowed IFF the delete-on-close flag is also set.

The FILE_DELETE_ON_CLOSE flag doesn't really have any significance in the protocol, it is passed to the underlying filesystem except in one special case where the server validates that the DELETE or GENERIC is granted on the share. Did you find some reference in the documents to the contrary?

No but emperical testing, and xfstests, show it. It does not just affect rename() but the same thing happens with "create and replace an existing file".

There is definitely a conditional somewhere in the SMB server that affects the behaviour or the delete-on-close flag.

This particular scenario is for the behaviour when we try to rename onto an existing file that have open handles.
The process used in cifs.ko is :

1, Try to rename the file. This fails with ACCESS_DENIED if there are open handles
if this fails, then fallback to
2, set delete-on-close
3, try the rename again

In SMB1, this works. In step 3 the file is renamed EVEN if there are still open handles to it.
In SMB2+ this fails. If there are open handles then this fails with DELETE_PENDING and the rename will not happen.
(then sometime later when the other process closes its handle then the file is deleted)

Quoted 3 lines

This still happens in windows 2016. Delete-on-close and other similar unlink() related operations
behave differently between SMB1 and SMB2.

Huh. I'm going to look into this on the Windows side. I wonder if something in the Linux client is falling back to the ancient SMB_COM_RENAME or SMB_COM_DELETE ops, which of course are path-based and exhibit different behaviors. The mystery is that FILE_DELETE_ON_CLOSE is not relevant to those.

Anyway, thanks for the report. You wouldn't have a network trace of these differences, would you?

Tom.

Quoted 108 lines
Quoted 1 line

In other words, I think the fix may be ok, but the server->vals->protocol_id !=

0

Quoted 13 lines

conditional may need more thought.

Tom.

Quoted 9 lines

This special case is not supported in SMB2 so should not attempt the unlink- and-try-again
since the rename will still fail but we now also delete the destination file.

---
 fs/cifs/inode.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
Quoted 5 lines

fs/cifs/inode.c cifs_rename2(struct inode *source_dir, struct

Quoted 5 lines
fs/cifs/inode.c +0 -0
index 56ca4b8ccaba..fdea45267a39 100644
@@ -1777,6 +1777,7 @@ cifs_rename2(struct inode *source_dir, struct

dentry

Quoted 9 lines

fs/cifs/inode.c cifs_rename2(struct inode *source_dir, struct

Quoted 9 lines
fs/cifs/inode.c +1 -0
 *source_dentry,
      FILE_UNIX_BASIC_INFO *info_buf_target;
      unsigned int xid;
      int rc, tmprc;
+     struct TCP_Server_Info *server;
 
      if (flags & ~RENAME_NOREPLACE)
              return -EINVAL;
fs/cifs/inode.c +0 -0
@@ -1786,6 +1787,7 @@ cifs_rename2(struct inode *source_dir, struct

dentry

Quoted 35 lines
Quoted 35 lines
fs/cifs/inode.c +9 -0
 *source_dentry,
      if (IS_ERR(tlink))
              return PTR_ERR(tlink);
      tcon = tlink_tcon(tlink);
+     server = tcon->ses->server;
 
      xid = get_xid();
@@ -1809,6 +1811,14 @@ cifs_rename2(struct inode *source_dir, struct
 dentry *source_dentry,
                          to_name);
 
      /*
+      * Do not attempt unlink-then-try-rename-again for SMB2+.
+      * Renaming ontop of an existing open file IF the delete-on-close
+      * flag is set is only supported for SMB1.
+      */
+     if (rc == -EACCES && server->vals->protocol_id != 0)
+             goto cifs_rename_exit;
+
+     /*
       * No-replace is the natural behavior for CIFS, so skip unlink hacks.
       */

if (flags & RENAME_NOREPLACE)
--
2.13.6
This special case is not supported in SMB2 so should not attempt the unlink- and-try-again
since the rename will still fail but we now also delete the destination file.

---
 fs/cifs/inode.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
Quoted 5 lines

fs/cifs/inode.c cifs_rename2(struct inode *source_dir, struct

Quoted 5 lines
fs/cifs/inode.c +0 -0
index 56ca4b8ccaba..fdea45267a39 100644
@@ -1777,6 +1777,7 @@ cifs_rename2(struct inode *source_dir, struct

dentry

Quoted 9 lines

fs/cifs/inode.c cifs_rename2(struct inode *source_dir, struct

Quoted 9 lines
fs/cifs/inode.c +1 -0
 *source_dentry,
      FILE_UNIX_BASIC_INFO *info_buf_target;
      unsigned int xid;
      int rc, tmprc;
+     struct TCP_Server_Info *server;
 
      if (flags & ~RENAME_NOREPLACE)
              return -EINVAL;
fs/cifs/inode.c +0 -0
@@ -1786,6 +1787,7 @@ cifs_rename2(struct inode *source_dir, struct

dentry

Quoted 26 lines
Quoted 26 lines
fs/cifs/inode.c +9 -0
 *source_dentry,
      if (IS_ERR(tlink))
              return PTR_ERR(tlink);
      tcon = tlink_tcon(tlink);
+     server = tcon->ses->server;
 
      xid = get_xid();
@@ -1809,6 +1811,14 @@ cifs_rename2(struct inode *source_dir, struct
 dentry *source_dentry,
                          to_name);
 
      /*
+      * Do not attempt unlink-then-try-rename-again for SMB2+.
+      * Renaming ontop of an existing open file IF the delete-on-close
+      * flag is set is only supported for SMB1.
+      */
+     if (rc == -EACCES && server->vals->protocol_id != 0)
+             goto cifs_rename_exit;
+
+     /*
       * No-replace is the natural behavior for CIFS, so skip unlink hacks.
       */

if (flags & RENAME_NOREPLACE)
--
2.13.6