smbd: Move fsp_client_guid() to locking/
[bbaumbach/samba-autobuild/.git] / source3 / locking / leases_util.c
1 /*
2    Unix SMB/CIFS implementation.
3    Lease utility functions
4
5    Copyright (C) Jeremy Allison 2017.
6    Copyright (C) Stefan (metze) Metzmacher 2017.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #define DBGC_CLASS DBGC_LOCKING
23 #include "includes.h"
24 #include "../librpc/gen_ndr/open_files.h"
25 #include "locking/proto.h"
26 #include "smbd/globals.h"
27
28 uint32_t map_oplock_to_lease_type(uint16_t op_type)
29 {
30         uint32_t ret;
31
32         switch(op_type) {
33         case BATCH_OPLOCK:
34         case BATCH_OPLOCK|EXCLUSIVE_OPLOCK:
35                 ret = SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE;
36                 break;
37         case EXCLUSIVE_OPLOCK:
38                 ret = SMB2_LEASE_READ|SMB2_LEASE_WRITE;
39                 break;
40         case LEVEL_II_OPLOCK:
41                 ret = SMB2_LEASE_READ;
42                 break;
43         default:
44                 ret = SMB2_LEASE_NONE;
45                 break;
46         }
47         return ret;
48 }
49
50 uint32_t fsp_lease_type(const struct files_struct *fsp)
51 {
52         if (fsp->oplock_type == LEASE_OPLOCK) {
53                 return fsp->lease->lease.lease_state;
54         }
55         return map_oplock_to_lease_type(fsp->oplock_type);
56 }
57
58 uint32_t lease_type_is_exclusive(uint32_t lease_type)
59 {
60         if ((lease_type & (SMB2_LEASE_READ | SMB2_LEASE_WRITE)) ==
61             (SMB2_LEASE_READ | SMB2_LEASE_WRITE)) {
62                 return true;
63         }
64
65         return false;
66 }
67
68 bool fsp_lease_type_is_exclusive(const struct files_struct *fsp)
69 {
70         uint32_t lease_type = fsp_lease_type(fsp);
71
72         return lease_type_is_exclusive(lease_type);
73 }
74
75 const struct GUID *fsp_client_guid(const files_struct *fsp)
76 {
77         return &fsp->conn->sconn->client->connections->smb2.client.guid;
78 }