open hardlink on deferred close file return EINVAL
Subsystems (2)
PATCH 1/1 open hardlink on deferred close file return EINVAL
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"
The client side can return EINVAL directly without invoking server operations. This reduces client server network communication overhead.
--- fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/file.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
fs/smb/client/cifsproto.h +2 -0
@@ -157,6 +157,8 @@ extern int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,extern struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *,bool );extern int cifs_get_readable_path (struct cifs_tcon *tcon,const char *name,struct cifsFileInfo **ret_file); +extern int cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file);extern unsigned int smbCalcSize (void *buf);extern int decode_negTokenInit (unsigned char *security_blob,int length,struct TCP_Server_Info *server);
fs/smb/client/file.c +29 -0
@@ -751,6 +751,12 @@ int cifs_open(struct inode *inode, struct file *file) }else {_cifsFileInfo_put (cfile, true, false); } + }else { +/* hard link on the defeered close file */ + rc =cifs_get_hardlink_path (tcon, inode, file); +if (rc) { + goto out; + } }if (server->oplocks)
@@ -2413,6 +2419,29 @@ cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,return -ENOENT ; } +int +cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file) +{ +struct cifsFileInfo *open_file =NULL ; +struct cifsInodeInfo *cinode =CIFS_I (inode); +int rc =0 ; + +spin_lock (&tcon->open_file_lock); +spin_lock (&cinode->open_file_lock); + +list_for_each_entry (open_file, &cinode->openFileList, flist) { +if (file->f_flags == open_file->f_flags) { + rc = -EINVAL ; +break ; + } + } + +spin_unlock (&cinode->open_file_lock); +spin_unlock (&tcon->open_file_lock); +return rc; +} +void cifs_writedata_release (struct kref *refcount) {
Signature · 1 line
2.34.1
I tried out the patch and it does fix getting STATUS_INVALID_PARAMETER error from the server, but doesn't fix the issue of getting EINVAL
Traceback (most recent call last):
File "/root/hl-test.py", line 15, in <module>
newfd = os.open('new', os.O_RDONLY|os.O_DIRECT)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^OSError: [Errno 22] Invalid argument: 'new'
It is fixed by running with leases disable (via mount parm), but wouldn't it be better to fix the error so apps don't break. Ideas?
Quoted 104 lines · Chunjie Zhu
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_V2as 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 withSTATUS_INVALID_PARAMETER"The client side can return EINVAL directly without invoking server operations. This reduces client server network communication overhead.
Signed-off-by Chunjie Zhu <[email protected]>--- fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/file.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)fs/smb/client/cifsproto.h +2
-0@@ -157,6 +157,8 @@ extern int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,extern struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *,bool );extern int cifs_get_readable_path (struct cifs_tcon *tcon,const char *name,struct cifsFileInfo **ret_file); +extern int cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file);extern unsigned int smbCalcSize (void *buf);extern int decode_negTokenInit (unsigned char *security_blob,int length,struct TCP_Server_Info *server);fs/smb/client/file.c +29
-1@@ -751,6 +751,12 @@ int cifs_open(struct inode *inode, struct file *file) }else {_cifsFileInfo_put (cfile, true, false); } + }else { +/* hard link on the defeered close file */ + rc =cifs_get_hardlink_path (tcon, inode, file); +if (rc) { + goto out; + } }if (server->oplocks)@@ -2413,6 +2419,29 @@ cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,return -ENOENT ; } +int +cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file) +{ +struct cifsFileInfo *open_file =NULL ; +struct cifsInodeInfo *cinode =CIFS_I (inode); +int rc =0 ; + +spin_lock (&tcon->open_file_lock); +spin_lock (&cinode->open_file_lock); + +list_for_each_entry (open_file, &cinode->openFileList, flist) { +if (file->f_flags == open_file->f_flags) { + rc = -EINVAL ; +break ; + } + } + +spin_unlock (&cinode->open_file_lock); +spin_unlock (&tcon->open_file_lock); +return rc; +} +void cifs_writedata_release (struct kref *refcount) { --2.34.1
Signature · 3 lines
Thanks, Steve
If we run 2 applications on a CIFS client machine, one application opens file A, the other application opens file B which is hard link of file A, this issue would happen, as well.
The purpose of this patch is to reduce the CIFS protocol network communication as we can decide how to responsd to application at client side.
Quoted 3 lines
It is fixed by running with leases disable (via mount parm), but wouldn't it be better to fix the error so apps don't break. Ideas?
Ideas,
Extend SMB SMB_COM_OPEN_ANDX and SMB_COM_NT_CREATE_ANDX messages, add file alias into open or create request messages, an alias means a hard link of the original file.
Quoted 119 lines
fs/smb/client/cifsproto.h extern int cifs_get_writable_path(struct cifs_tcon *t=
Quoted 45 lines · On Mon, Mar 17, 2025 at 3:41=E2=80=AFAM Chunjie Zhu <[email protected]>=…
The following Python script results in unexpected behaviour when run on a CIFS filesystem against a Windows Server:
# Create file fd =3D 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 =3D 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 =3D 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_V2as 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 withSTATUS_INVALID_PARAMETER"The client side can return EINVAL directly without invoking server operations. This reduces client server network communication overhead.
Signed-off-by Chunjie Zhu <[email protected]>--- fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/file.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)fs/smb/client/cifsproto.h +0
-0@@ -157,6 +157,8 @@ extern int cifs_get_writable_path(struct cifs_tcon *t=con, const char *name,
fs/smb/client/cifsproto.h extern int cifs_get_writable_path(struct cifs_tcon *t= · line 157
Quoted 1 line
fs/smb/client/cifsproto.h +0
-0extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, b=ool);
fs/smb/client/cifsproto.h extern int cifs_get_writable_path(struct cifs_tcon *t= · line 158
Quoted 1 line
fs/smb/client/cifsproto.h +0
-0extern int cifs_get_readable_path(struct cifs_tcon *tcon, const char *na=me,
fs/smb/client/cifsproto.h extern int cifs_get_writable_path(struct cifs_tcon *t= · line 160
Quoted 2 lines
fs/smb/client/cifsproto.h +1
-0struct cifsFileInfo **ret_file); +extern int cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *=inode,
fs/smb/client/file.c int cifs_open(struct inode *inode, struct file *file=
Quoted 9 lines
fs/smb/client/cifsproto.h +1
-0+ struct file *file);extern unsigned int smbCalcSize (void *buf);extern int decode_negTokenInit (unsigned char *security_blob,int length,struct TCP_Server_Info *server);fs/smb/client/file.c +0
-0@@ -751,6 +751,12 @@ int cifs_open(struct inode *inode, struct file *file=)
fs/smb/client/file.c cifs_get_readable_path(struct cifs_tcon *tcon, con=
Quoted 13 lines
fs/smb/client/file.c +6
-0}else {_cifsFileInfo_put (cfile, true, false); } + }else { +/* hard link on the defeered close file */ + rc =3D cifs_get_hardlink_path(tcon, inode, file); +if (rc) { + goto out; + } }if (server->oplocks)@@ -2413,6 +2419,29 @@ cifs_get_readable_path(struct cifs_tcon *tcon, con=st char *name,
Quoted 30 lines
fs/smb/client/file.c +23
-1return -ENOENT ; } +int +cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file) +{ + struct cifsFileInfo *open_file =3D NULL; + struct cifsInodeInfo *cinode =3D CIFS_I(inode); + int rc =3D 0; + +spin_lock (&tcon->open_file_lock); +spin_lock (&cinode->open_file_lock); + +list_for_each_entry (open_file, &cinode->openFileList, flist) { + if (file->f_flags =3D=3D open_file->f_flags) { + rc =3D -EINVAL ; +break ; + } + } + +spin_unlock (&cinode->open_file_lock); +spin_unlock (&tcon->open_file_lock); +return rc; +} +void cifs_writedata_release (struct kref *refcount) { --Quoted 3 lines
2.34.1
--=20
Thanks,Steve
Wouldn't a simpler fix be to either not include the lease key when opening the hardlink of the file which already has a lease on the other filename? or alternatively don't request a lease at all on the second open? or if we get the error invalid parameter then check if the problem was the other open of the hardlink and either immediately close it and reopen (if deferred close) or retry the open without requesting leases?
Failing intentionally seems wrong
Quoted 137 lines · Chunjie Zhu
If we run 2 applications on a CIFS client machine, one application opens file A, the other application opens file B which is hard link of file A, this issue would happen, as well.
The purpose of this patch is to reduce the CIFS protocol network communication as we can decide how to responsd to application at client side.
Quoted 3 lines
It is fixed by running with leases disable (via mount parm), but wouldn't it be better to fix the error so apps don't break. Ideas?
Ideas,
Extend SMB
SMB_COM_OPEN_ANDXandSMB_COM_NT_CREATE_ANDXmessages, add file alias into open or create request messages, an alias means a hard link of the original file.Quoted 119 lines
fs/smb/client/cifsproto.h extern int cifs_get_writable_path(struct cifs_tcon *t=
Quoted 45 lines · On Mon, Mar 17, 2025 at 3:41=E2=80=AFAM Chunjie Zhu <[email protected]>=…
The following Python script results in unexpected behaviour when run on a CIFS filesystem against a Windows Server:
# Create file fd =3D 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 =3D 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 =3D 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_V2as 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 withSTATUS_INVALID_PARAMETER"The client side can return EINVAL directly without invoking server operations. This reduces client server network communication overhead.
Signed-off-by Chunjie Zhu <[email protected]>--- fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/file.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)fs/smb/client/cifsproto.h +0
-0@@ -157,6 +157,8 @@ extern int cifs_get_writable_path(struct cifs_tcon *t=con, const char *name,
fs/smb/client/cifsproto.h extern int cifs_get_writable_path(struct cifs_tcon *t= · line 157
Quoted 1 line
fs/smb/client/cifsproto.h +0
-0extern struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *, b=ool);
fs/smb/client/cifsproto.h extern int cifs_get_writable_path(struct cifs_tcon *t= · line 158
Quoted 1 line
fs/smb/client/cifsproto.h +0
-0extern int cifs_get_readable_path(struct cifs_tcon *tcon, const char *na=me,
fs/smb/client/cifsproto.h extern int cifs_get_writable_path(struct cifs_tcon *t= · line 160
Quoted 2 lines
fs/smb/client/cifsproto.h +1
-0struct cifsFileInfo **ret_file); +extern int cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *=inode,
fs/smb/client/file.c int cifs_open(struct inode *inode, struct file *file=
Quoted 9 lines
fs/smb/client/cifsproto.h +1
-0+ struct file *file);extern unsigned int smbCalcSize (void *buf);extern int decode_negTokenInit (unsigned char *security_blob,int length,struct TCP_Server_Info *server);fs/smb/client/file.c +0
-0@@ -751,6 +751,12 @@ int cifs_open(struct inode *inode, struct file *file=)
fs/smb/client/file.c cifs_get_readable_path(struct cifs_tcon *tcon, con=
Quoted 13 lines
fs/smb/client/file.c +6
-0}else {_cifsFileInfo_put (cfile, true, false); } + }else { +/* hard link on the defeered close file */ + rc =3D cifs_get_hardlink_path(tcon, inode, file); +if (rc) { + goto out; + } }if (server->oplocks)@@ -2413,6 +2419,29 @@ cifs_get_readable_path(struct cifs_tcon *tcon, con=st char *name,
Quoted 30 lines
fs/smb/client/file.c +23
-1return -ENOENT ; } +int +cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file) +{ + struct cifsFileInfo *open_file =3D NULL; + struct cifsInodeInfo *cinode =3D CIFS_I(inode); + int rc =3D 0; + +spin_lock (&tcon->open_file_lock); +spin_lock (&cinode->open_file_lock); + +list_for_each_entry (open_file, &cinode->openFileList, flist) { + if (file->f_flags =3D=3D open_file->f_flags) { + rc =3D -EINVAL ; +break ; + } + } + +spin_unlock (&cinode->open_file_lock); +spin_unlock (&tcon->open_file_lock); +return rc; +} +void cifs_writedata_release (struct kref *refcount) { --Quoted 3 lines
2.34.1
--=20
Thanks,Steve
Signature · 3 lines
Thanks, Steve
PATCH v2 1/1 fix open hardlink on deferred close file error
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.
---
v2: if error, first close inode opening files and then open hard link
fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/file.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
fs/smb/client/cifsproto.h +2 -0
@@ -157,6 +157,8 @@ extern int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,extern struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *,bool );extern int cifs_get_readable_path (struct cifs_tcon *tcon,const char *name,struct cifsFileInfo **ret_file); +extern int cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file);extern unsigned int smbCalcSize (void *buf);extern int decode_negTokenInit (unsigned char *security_blob,int length,struct TCP_Server_Info *server);
fs/smb/client/file.c +29 -0
@@ -751,6 +751,12 @@ int cifs_open(struct inode *inode, struct file *file) }else {_cifsFileInfo_put (cfile, true, false); } + }else { +/* hard link on the defeered close file */ + rc =cifs_get_hardlink_path (tcon, inode, file); +if (rc) { +cifs_close_deferred_file (CIFS_I (inode)); + } }if (server->oplocks)
@@ -2413,6 +2419,29 @@ cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,return -ENOENT ; } +int +cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file) +{ +struct cifsFileInfo *open_file =NULL ; +struct cifsInodeInfo *cinode =CIFS_I (inode); +int rc =0 ; + +spin_lock (&tcon->open_file_lock); +spin_lock (&cinode->open_file_lock); + +list_for_each_entry (open_file, &cinode->openFileList, flist) { +if (file->f_flags == open_file->f_flags) { + rc = -EINVAL ; +break ; + } + } + +spin_unlock (&cinode->open_file_lock); +spin_unlock (&tcon->open_file_lock); +return rc; +} +void cifs_writedata_release (struct kref *refcount) {
Signature · 1 line
2.34.1
Re: [PATCH v2] fix open hardlink on deferred close file error
Have rebased the patch so it would merge onto current for-next (6.15-rc1+), and cleanup a checkpatch warning and added Cc:stable
Have tentatively merged the updated patch (see attached) into cifs-2.6.git Although I tested that it does fix the problem described in the commit description, additional review/testing would be welcome.
Quoted 107 lines · Chunjie Zhu
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_V2as 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 withSTATUS_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.
Signed-off-by Chunjie Zhu <[email protected]>---
v2: if error, first close inode opening files and then open hard link
fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/file.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)fs/smb/client/cifsproto.h +2
-0@@ -157,6 +157,8 @@ extern int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,extern struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *,bool );extern int cifs_get_readable_path (struct cifs_tcon *tcon,const char *name,struct cifsFileInfo **ret_file); +extern int cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file);extern unsigned int smbCalcSize (void *buf);extern int decode_negTokenInit (unsigned char *security_blob,int length,struct TCP_Server_Info *server);fs/smb/client/file.c +29
-1@@ -751,6 +751,12 @@ int cifs_open(struct inode *inode, struct file *file) }else {_cifsFileInfo_put (cfile, true, false); } + }else { +/* hard link on the defeered close file */ + rc =cifs_get_hardlink_path (tcon, inode, file); +if (rc) { +cifs_close_deferred_file (CIFS_I (inode)); + } }if (server->oplocks)@@ -2413,6 +2419,29 @@ cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,return -ENOENT ; } +int +cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file) +{ +struct cifsFileInfo *open_file =NULL ; +struct cifsInodeInfo *cinode =CIFS_I (inode); +int rc =0 ; + +spin_lock (&tcon->open_file_lock); +spin_lock (&cinode->open_file_lock); + +list_for_each_entry (open_file, &cinode->openFileList, flist) { +if (file->f_flags == open_file->f_flags) { + rc = -EINVAL ; +break ; + } + } + +spin_unlock (&cinode->open_file_lock); +spin_unlock (&tcon->open_file_lock); +return rc; +} +void cifs_writedata_release (struct kref *refcount) { --2.34.1
Signature · 3 lines
Thanks, Steve
From b5e909ed835c96b36d536b66e8767f20dfb13559 Mon Sep 17 00:00:00 2001
From: Chunjie Zhu <[email protected]>
Date: Sat, 12 Apr 2025 21:15:55 -0500
Subject: [PATCH 2/2] smb3 client: fix open hardlink on deferred close file
error
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.
---
v2: if error, first close inode opening files and then open hard link
--- fs/smb/client/cifsproto.h | 2 ++ fs/smb/client/file.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+)
fs/smb/client/cifsproto.h +2 -0
@@ -163,6 +163,8 @@ extern int cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,extern struct cifsFileInfo *find_readable_file (struct cifsInodeInfo *,bool );extern int cifs_get_readable_path (struct cifs_tcon *tcon,const char *name,struct cifsFileInfo **ret_file); +extern int cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file);extern unsigned int smbCalcSize (void *buf);extern int decode_negTokenInit (unsigned char *security_blob,int length,struct TCP_Server_Info *server);
fs/smb/client/file.c +28 -0
@@ -1007,6 +1007,11 @@ int cifs_open(struct inode *inode, struct file *file) }else {_cifsFileInfo_put (cfile, true, false); } + }else { +/* hard link on the defeered close file */ + rc =cifs_get_hardlink_path (tcon, inode, file); +if (rc) +cifs_close_deferred_file (CIFS_I (inode)); }if (server->oplocks)
@@ -2071,6 +2076,29 @@ cifs_move_llist(struct list_head *source, struct list_head *dest)list_move (li, dest); } +int +cifs_get_hardlink_path (struct cifs_tcon *tcon,struct inode *inode, +struct file *file) +{ +struct cifsFileInfo *open_file =NULL ; +struct cifsInodeInfo *cinode =CIFS_I (inode); +int rc =0 ; + +spin_lock (&tcon->open_file_lock); +spin_lock (&cinode->open_file_lock); + +list_for_each_entry (open_file, &cinode->openFileList, flist) { +if (file->f_flags == open_file->f_flags) { + rc = -EINVAL ; +break ; + } + } + +spin_unlock (&cinode->open_file_lock); +spin_unlock (&tcon->open_file_lock); +return rc; +} +void cifs_free_llist (struct list_head *llist) {
Signature · 1 line
2.43.0