Skip to content

Commit c9bc068

Browse files
chunjiezjfvogel
authored andcommitted
smb3 client: fix open hardlink on deferred close file error
commit 262b73ef442e68e53220b9d6fc5a0d08b557fa42 upstream. The following Python script results in unexpected behaviour when run on a CIFS filesystem against a Windows Server: # Create file fd = os.open('test', os.O_WRONLY|os.O_CREAT) os.write(fd, b'foo') os.close(fd) # Open and close the file to leave a pending deferred close fd = os.open('test', os.O_RDONLY|os.O_DIRECT) os.close(fd) # Try to open the file via a hard link os.link('test', 'new') newfd = os.open('new', os.O_RDONLY|os.O_DIRECT) The final open returns EINVAL due to the server returning STATUS_INVALID_PARAMETER. The root cause of this is that the client caches lease keys per inode, but the spec requires them to be related to the filename which causes problems when hard links are involved: From MS-SMB2 section 3.3.5.9.11: "The server MUST attempt to locate a Lease by performing a lookup in the LeaseTable.LeaseList using the LeaseKey in the SMB2_CREATE_REQUEST_LEASE_V2 as the lookup key. If a lease is found, Lease.FileDeleteOnClose is FALSE, and Lease.Filename does not match the file name for the incoming request, the request MUST be failed with STATUS_INVALID_PARAMETER" On client side, we first check the context of file open, if it hits above conditions, we first close all opening files which are belong to the same inode, then we do open the hard link file. Cc: [email protected] Signed-off-by: Chunjie Zhu <[email protected]> Signed-off-by: Steve French <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 9042efa7f129b0a4cc8db7fb5f593f36c18a99f7) Signed-off-by: Jack Vogel <[email protected]>
1 parent ca2af38 commit c9bc068

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

fs/smb/client/cifsproto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ extern int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
160160
extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, bool);
161161
extern int cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
162162
struct cifsFileInfo **ret_file);
163+
extern int cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode,
164+
struct file *file);
163165
extern unsigned int smbCalcSize(void *buf);
164166
extern int decode_negTokenInit(unsigned char *security_blob, int length,
165167
struct TCP_Server_Info *server);

fs/smb/client/file.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,11 @@ int cifs_open(struct inode *inode, struct file *file)
10021002
} else {
10031003
_cifsFileInfo_put(cfile, true, false);
10041004
}
1005+
} else {
1006+
/* hard link on the defeered close file */
1007+
rc = cifs_get_hardlink_path(tcon, inode, file);
1008+
if (rc)
1009+
cifs_close_deferred_file(CIFS_I(inode));
10051010
}
10061011

10071012
if (server->oplocks)
@@ -2066,6 +2071,29 @@ cifs_move_llist(struct list_head *source, struct list_head *dest)
20662071
list_move(li, dest);
20672072
}
20682073

2074+
int
2075+
cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode,
2076+
struct file *file)
2077+
{
2078+
struct cifsFileInfo *open_file = NULL;
2079+
struct cifsInodeInfo *cinode = CIFS_I(inode);
2080+
int rc = 0;
2081+
2082+
spin_lock(&tcon->open_file_lock);
2083+
spin_lock(&cinode->open_file_lock);
2084+
2085+
list_for_each_entry(open_file, &cinode->openFileList, flist) {
2086+
if (file->f_flags == open_file->f_flags) {
2087+
rc = -EINVAL;
2088+
break;
2089+
}
2090+
}
2091+
2092+
spin_unlock(&cinode->open_file_lock);
2093+
spin_unlock(&tcon->open_file_lock);
2094+
return rc;
2095+
}
2096+
20692097
void
20702098
cifs_free_llist(struct list_head *llist)
20712099
{

0 commit comments

Comments
 (0)