--- /dev/null
+From 66d45ca1350a3bb8d5f4db8879ccad3ed492337a Mon Sep 17 00:00:00 2001
+From: Ronnie Sahlberg <lsahlber@redhat.com>
+Date: Fri, 17 Feb 2023 13:35:00 +1000
+Subject: cifs: Check the lease context if we actually got a lease
+
+From: Ronnie Sahlberg <lsahlber@redhat.com>
+
+commit 66d45ca1350a3bb8d5f4db8879ccad3ed492337a upstream.
+
+Some servers may return that we got a lease in rsp->OplockLevel
+but then in the lease context contradict this and say we got no lease
+at all. Thus we need to check the context if we have a lease.
+Additionally, If we do not get a lease we need to make sure we close
+the handle before we return an error to the caller.
+
+Signed-off-by: Ronnie Sahlberg <lsahlber@redhat.com>
+Cc: stable@vger.kernel.org
+Reviewed-by: Bharath SM <bharathsm@microsoft.com>
+Reviewed-by: Paulo Alcantara (SUSE) <pc@manguebit.com>
+Signed-off-by: Steve French <stfrench@microsoft.com>
+Signed-off-by: Meetakshi Setiya <msetiya@microsoft.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ fs/cifs/smb2ops.c | 24 ++++++++++++++++--------
+ 1 file changed, 16 insertions(+), 8 deletions(-)
+
+--- a/fs/cifs/smb2ops.c
++++ b/fs/cifs/smb2ops.c
+@@ -886,8 +886,6 @@ int open_cached_dir(unsigned int xid, st
+ goto oshr_exit;
+ }
+
+- atomic_inc(&tcon->num_remote_opens);
+-
+ o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
+ oparms.fid->persistent_fid = o_rsp->PersistentFileId;
+ oparms.fid->volatile_fid = o_rsp->VolatileFileId;
+@@ -897,8 +895,6 @@ int open_cached_dir(unsigned int xid, st
+
+ tcon->crfid.tcon = tcon;
+ tcon->crfid.is_valid = true;
+- tcon->crfid.dentry = dentry;
+- dget(dentry);
+ kref_init(&tcon->crfid.refcount);
+
+ /* BB TBD check to see if oplock level check can be removed below */
+@@ -907,14 +903,16 @@ int open_cached_dir(unsigned int xid, st
+ * See commit 2f94a3125b87. Increment the refcount when we
+ * get a lease for root, release it if lease break occurs
+ */
+- kref_get(&tcon->crfid.refcount);
+- tcon->crfid.has_lease = true;
+ rc = smb2_parse_contexts(server, rsp_iov,
+ &oparms.fid->epoch,
+ oparms.fid->lease_key, &oplock,
+ NULL, NULL);
+ if (rc)
+ goto oshr_exit;
++
++ if (!(oplock & SMB2_LEASE_READ_CACHING_HE))
++ goto oshr_exit;
++
+ } else
+ goto oshr_exit;
+
+@@ -928,7 +926,10 @@ int open_cached_dir(unsigned int xid, st
+ (char *)&tcon->crfid.file_all_info))
+ tcon->crfid.file_all_info_is_valid = true;
+ tcon->crfid.time = jiffies;
+-
++ tcon->crfid.dentry = dentry;
++ dget(dentry);
++ kref_get(&tcon->crfid.refcount);
++ tcon->crfid.has_lease = true;
+
+ oshr_exit:
+ mutex_unlock(&tcon->crfid.fid_mutex);
+@@ -937,8 +938,15 @@ oshr_free:
+ SMB2_query_info_free(&rqst[1]);
+ free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
+ free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
+- if (rc == 0)
++ if (rc) {
++ if (tcon->crfid.is_valid)
++ SMB2_close(0, tcon, oparms.fid->persistent_fid,
++ oparms.fid->volatile_fid);
++ }
++ if (rc == 0) {
+ *cfid = &tcon->crfid;
++ atomic_inc(&tcon->num_remote_opens);
++ }
+ return rc;
+ }
+