smb: client: retry a hard link open without a lease
Subsystems (3)
Paulo Alcantara
maintainerPATCH v2 1/2 smb: client: retry a hard link open without a lease
Opening a hard link of a file that currently holds a lease fails with EINVAL over SMB3. The client caches lease keys per inode, but the server ties a lease to the file name it was granted under, so reusing the inode's lease key to open a different name of the same inode is rejected with STATUS_INVALID_PARAMETER (MS-SMB2 3.3.5.9.11).
Resend the open without a lease when it is rejected that way, as smb2_rename_path() already does. Fold it into the same retry loop as the FILE_READ_ATTRIBUTES fallback so either can follow the other in whichever order the server rejects them. Reacting to the server response covers every case - including a name kept open by an active handle - without racing a handle opened concurrently.
--- fs/smb/client/smb2file.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-)
fs/smb/client/smb2file.c +16 -7
@@ -201,18 +201,27 @@ int smb2_open_file(const unsigned int xid, struct cifs_open_parms *oparms, } smb2_oplock =SMB2_OPLOCK_LEVEL_BATCH ; - rc =SMB2_open (xid, oparms, smb2_path, &smb2_oplock, data,NULL , &err_iov, - &err_buftype); -if (rc == -EACCES && retry_without_read_attributes && - !oparms->pending) { +for (;;) { + rc =SMB2_open (xid, oparms, smb2_path, &smb2_oplock, data,NULL , + &err_iov, &err_buftype); +if (oparms->pending) +break ; +if (rc == -EACCES && retry_without_read_attributes) { + retry_without_read_attributes = false; + oparms->desired_access &= ~FILE_READ_ATTRIBUTES ; + }else if (rc == -EINVAL && + smb2_oplock !=SMB2_OPLOCK_LEVEL_NONE ) { +cifs_dbg (FYI ,"%s: invalid lease key, resending request without lease\n" , + __func__); + smb2_oplock =SMB2_OPLOCK_LEVEL_NONE ; + }else { +break ; + }free_rsp_buf (err_buftype, err_iov.iov_base);memset (&err_iov,0 ,sizeof (err_iov)); err_buftype =CIFS_NO_BUFFER ; - oparms->desired_access &= ~FILE_READ_ATTRIBUTES ; oparms->reconnect = reconnect; oparms->replay = false; - rc =SMB2_open (xid, oparms, smb2_path, &smb2_oplock, data,NULL , &err_iov, - &err_buftype); }if (rc && data) {struct smb2_hdr *hdr = err_iov.iov_base;
Signature · 1 line
2.56.0
Paulo Alcantara
maintainerPATCH v2 2/2 smb: client: flush deferred handles of other names before a hard link open
cifs_open() flushes an inode's deferred closes before opening a different name (hard link) of it, so the open can take a fresh lease rather than be reissued without one (see "smb: client: retry a hard link open without a lease"). cifs_get_hardlink_path() only triggered that flush when a cached handle had the same open flags as the new open, so a differently accessed name - written then read, say - skipped it and opened without a lease.
Match on the name instead: flush when a cached handle for another name of the inode is open, so those opens keep lease caching too.
--- fs/smb/client/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/client/file.c +1 -1
@@ -2737,7 +2737,7 @@ cifs_get_hardlink_path(struct cifs_tcon *tcon, struct inode *inode,spin_lock (&cinode->open_file_lock);list_for_each_entry (open_file, &cinode->openFileList, flist) { -if (file->f_flags == open_file->f_flags) { +if (open_file->dentry !=file_dentry (file)) { rc = -EINVAL ;break ; }
Signature · 1 line
2.56.0
Paulo Alcantara
maintainerRe: [PATCH v2 2/2] smb: client: flush deferred handles of other names before a hard link open
Quoted 10 lines · Paulo Alcantara
cifs_open()flushes an inode's deferred closes before opening a different name (hard link) of it, so the open can take a fresh lease rather than be reissued without one (see "smb: client: retry a hard link open without a lease").cifs_get_hardlink_path()only triggered that flush when a cached handle had the same open flags as the new open, so a differently accessed name - written then read, say - skipped it and opened without a lease.Match on the name instead: flush when a cached handle for another name of the inode is open, so those opens keep lease caching too.
...
Sashiko found other potential issues with patch 1/2, which would require a lot of rework to get it right. Therefore I've applied this one as it already fixes the bug reported by Giuseppe Piscitelli.
Quoted 14 lines · Paulo Alcantara
Opening a hard link of a file that currently holds a lease fails with EINVAL over SMB3. The client caches lease keys per inode, but the server ties a lease to the file name it was granted under, so reusing the inode's lease key to open a different name of the same inode is rejected with
STATUS_INVALID_PARAMETER(MS-SMB2 3.3.5.9.11).Resend the open without a lease when it is rejected that way, as
smb2_rename_path()already does. Fold it into the same retry loop as theFILE_READ_ATTRIBUTESfallback so either can follow the other in whichever order the server rejects them. Reacting to the server response covers every case - including a name kept open by an active handle - without racing a handle opened concurrently.
Quoted 5 lines
Reported-by Giuseppe Piscitelli <[email protected]>Closes https://lore.kernel.org/r/CACmoRBbDZR3eyEM65j73sWMS7A=T89z1QAvCP=+u0WGzZhwW6g@mail.gmail.com open hereFixes 262b73ef442e ("smb3 client: fix open hardlink on deferred close file error")Signed-off-by Paulo Alcantara <[email protected]>
Looks good to me:
Thanks!