smbd: Let fsp_lease_type() look at leases.tdb
authorVolker Lendecke <vl@samba.org>
Thu, 15 Aug 2019 10:10:52 +0000 (12:10 +0200)
committerJeremy Allison <jra@samba.org>
Tue, 17 Sep 2019 22:49:36 +0000 (22:49 +0000)
The same lease can be used via different TCP connections (yes, we have
tests for this!). At the end of downgrade_lease() we update all fsp's
with fsps_lease_update() that link to the lease that just was
changed. However, this is only in the local process, this is not
cross-smbd. So other smbds using the same lease can use stale
information and for example get the mandatory locking wrong.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/include/vfs.h
source3/locking/leases_util.c
source3/locking/proto.h

index fef68396221a265a680aa8b4f8c2cdadb48f330f..3c80a5fdd331a1dfe35258609f23f63ccff42c42 100644 (file)
@@ -354,6 +354,14 @@ typedef struct files_struct {
        bool write_time_forced;
 
        int oplock_type;
+
+       /*
+        * Cache of our lease_type, stored as "current_state" in
+        * leases.tdb
+        */
+       int leases_db_seqnum;
+       uint32_t lease_type;
+
        struct fsp_lease *lease;
        int sent_oplock_break;
        struct tevent_timer *oplock_timeout;
index da094069941dbc3b2c8c2229f432186a8c887f57..d307f420c7c0ca2b44f68f07e7cd342f62d16321 100644 (file)
@@ -24,6 +24,7 @@
 #include "../librpc/gen_ndr/open_files.h"
 #include "locking/proto.h"
 #include "smbd/globals.h"
+#include "locking/leases_db.h"
 
 uint32_t map_oplock_to_lease_type(uint16_t op_type)
 {
@@ -47,12 +48,27 @@ uint32_t map_oplock_to_lease_type(uint16_t op_type)
        return ret;
 }
 
-uint32_t fsp_lease_type(const struct files_struct *fsp)
+uint32_t fsp_lease_type(struct files_struct *fsp)
 {
-       if (fsp->oplock_type == LEASE_OPLOCK) {
-               return fsp->lease->lease.lease_state;
+       NTSTATUS status;
+
+       if (fsp->oplock_type != LEASE_OPLOCK) {
+               uint32_t type = map_oplock_to_lease_type(fsp->oplock_type);
+               return type;
+       }
+
+       status = leases_db_get_current_state(
+               fsp_client_guid(fsp),
+               &fsp->lease->lease.lease_key,
+               &fsp->leases_db_seqnum,
+               &fsp->lease_type);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_DEBUG("leases_db_get_current_state failed: %s\n",
+                         nt_errstr(status));
+               fsp->lease_type = 0; /* no lease */
        }
-       return map_oplock_to_lease_type(fsp->oplock_type);
+
+       return fsp->lease_type;
 }
 
 static uint32_t lease_type_is_exclusive(uint32_t lease_type)
@@ -65,7 +81,7 @@ static uint32_t lease_type_is_exclusive(uint32_t lease_type)
        return false;
 }
 
-bool fsp_lease_type_is_exclusive(const struct files_struct *fsp)
+bool fsp_lease_type_is_exclusive(struct files_struct *fsp)
 {
        uint32_t lease_type = fsp_lease_type(fsp);
 
index 9094cc02651c5faa536f370491676f3440af98eb..7d2d28f4172ec41bdbed2a96cccf3c6ee8733bd3 100644 (file)
@@ -263,8 +263,8 @@ bool release_posix_lock_posix_flavour(files_struct *fsp,
 
 /* The following definitions come from locking/leases_util.c */
 uint32_t map_oplock_to_lease_type(uint16_t op_type);
-uint32_t fsp_lease_type(const struct files_struct *fsp);
-bool fsp_lease_type_is_exclusive(const struct files_struct *fsp);
+uint32_t fsp_lease_type(struct files_struct *fsp);
+bool fsp_lease_type_is_exclusive(struct files_struct *fsp);
 const struct GUID *fsp_client_guid(const files_struct *fsp);
 
 #endif /* _LOCKING_PROTO_H_ */