s3:locking: add share_mode_entry_prepare_{lock,unlock}() infrastructure
[bbaumbach/samba-autobuild/.git] / source3 / locking / share_mode_lock.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14  */
15
16 #ifndef __LOCKING_SHARE_MODE_LOCK_H__
17 #define __LOCKING_SHARE_MODE_LOCK_H__
18
19 #include "replace.h"
20 #include <tevent.h>
21 #include "librpc/gen_ndr/file_id.h"
22 #include "lib/util/time.h"
23
24 struct share_mode_data;
25 struct share_mode_lock;
26 struct share_mode_entry;
27 struct smb_filename;
28 struct files_struct;
29 struct smb2_lease_key;
30
31 bool locking_init(void);
32 bool locking_init_readonly(void);
33 bool locking_end(void);
34
35 struct file_id share_mode_lock_file_id(const struct share_mode_lock *lck);
36
37 struct share_mode_lock *get_share_mode_lock(
38         TALLOC_CTX *mem_ctx,
39         struct file_id id,
40         const char *servicepath,
41         const struct smb_filename *smb_fname,
42         const struct timespec *old_write_time);
43 struct share_mode_lock *get_existing_share_mode_lock(TALLOC_CTX *mem_ctx,
44                                                      struct file_id id);
45
46 bool del_share_mode(struct share_mode_lock *lck,
47                     struct files_struct *fsp);
48 bool downgrade_share_oplock(struct share_mode_lock *lck,
49                             struct files_struct *fsp);
50 bool remove_share_oplock(struct share_mode_lock *lck,
51                          struct files_struct *fsp);
52 bool file_has_read_lease(struct files_struct *fsp);
53
54 bool set_share_mode(
55         struct share_mode_lock *lck,
56         struct files_struct *fsp,
57         uid_t uid,
58         uint64_t mid,
59         uint16_t op_type,
60         const struct smb2_lease_key *lease_key,
61         uint32_t share_access,
62         uint32_t access_mask);
63 bool reset_share_mode_entry(
64         struct share_mode_lock *lck,
65         struct server_id old_pid,
66         uint64_t old_share_file_id,
67         struct server_id new_pid,
68         uint64_t new_mid,
69         uint64_t new_share_file_id);
70
71 bool mark_share_mode_disconnected(
72         struct share_mode_lock *lck, struct files_struct *fsp);
73
74 struct share_mode_lock *fetch_share_mode_unlocked(
75         TALLOC_CTX *mem_ctx,
76         struct file_id id);
77
78 struct tevent_req *fetch_share_mode_send(
79         TALLOC_CTX *mem_ctx,
80         struct tevent_context *ev,
81         struct file_id id,
82         bool *queued);
83 NTSTATUS fetch_share_mode_recv(
84         struct tevent_req *req,
85         TALLOC_CTX *mem_ctx,
86         struct share_mode_lock **_lck);
87
88 int share_entry_forall(
89         int (*fn)(struct file_id fid,
90                   const struct share_mode_data *data,
91                   const struct share_mode_entry *entry,
92                   void *private_data),
93         void *private_data);
94
95 NTSTATUS share_mode_count_entries(struct file_id fid, size_t *num_share_modes);
96 int share_mode_forall(
97         int (*fn)(struct file_id fid,
98                   const struct share_mode_data *data,
99                   void *private_data),
100         void *private_data);
101 bool share_mode_forall_entries(
102         struct share_mode_lock *lck,
103         bool (*fn)(struct share_mode_entry *e,
104                    bool *modified,
105                    void *private_data),
106         void *private_data);
107
108 NTTIME share_mode_changed_write_time(struct share_mode_lock *lck);
109 void share_mode_set_changed_write_time(struct share_mode_lock *lck, struct timespec write_time);
110 void share_mode_set_old_write_time(struct share_mode_lock *lck, struct timespec write_time);
111 const char *share_mode_servicepath(struct share_mode_lock *lck);
112 char *share_mode_filename(TALLOC_CTX *mem_ctx, struct share_mode_lock *lck);
113 char *share_mode_data_dump(
114         TALLOC_CTX *mem_ctx, struct share_mode_lock *lck);
115
116 void share_mode_flags_get(
117         struct share_mode_lock *lck,
118         uint32_t *access_mask,
119         uint32_t *share_mode,
120         uint32_t *lease_type);
121 void share_mode_flags_set(
122         struct share_mode_lock *lck,
123         uint32_t access_mask,
124         uint32_t share_mode,
125         uint32_t lease_type,
126         bool *modified);
127
128 struct tevent_req *share_mode_watch_send(
129         TALLOC_CTX *mem_ctx,
130         struct tevent_context *ev,
131         struct share_mode_lock *lck,
132         struct server_id blocker);
133 NTSTATUS share_mode_watch_recv(
134         struct tevent_req *req, bool *blockerdead, struct server_id *blocker);
135 NTSTATUS share_mode_wakeup_waiters(struct file_id id);
136
137 typedef void (*share_mode_do_locked_vfs_fn_t)(
138                 struct share_mode_lock *lck,
139                 void *private_data);
140 NTSTATUS _share_mode_do_locked_vfs_denied(
141         struct file_id id,
142         share_mode_do_locked_vfs_fn_t fn,
143         void *private_data,
144         const char *location);
145 #define share_mode_do_locked_vfs_denied(__id, __fn, __private_data) \
146         _share_mode_do_locked_vfs_denied(__id, __fn, __private_data, __location__)
147 NTSTATUS _share_mode_do_locked_vfs_allowed(
148         struct file_id id,
149         share_mode_do_locked_vfs_fn_t fn,
150         void *private_data,
151         const char *location);
152 #define share_mode_do_locked_vfs_allowed(__id, __fn, __private_data) \
153         _share_mode_do_locked_vfs_allowed(__id, __fn, __private_data, __location__)
154
155 struct share_mode_entry_prepare_state {
156         struct file_id __fid;
157         struct share_mode_lock *__lck_ptr;
158         union {
159 #define __SHARE_MODE_LOCK_SPACE 32
160                 uint8_t __u8_space[__SHARE_MODE_LOCK_SPACE];
161 #ifdef SHARE_MODE_ENTRY_PREPARE_STATE_LCK_SPACE
162                 struct share_mode_lock __lck_space;
163 #endif
164         };
165 };
166
167 typedef void (*share_mode_entry_prepare_lock_fn_t)(
168                 struct share_mode_lock *lck,
169                 bool *keep_locked,
170                 void *private_data);
171 NTSTATUS _share_mode_entry_prepare_lock(
172         struct share_mode_entry_prepare_state *prepare_state,
173         struct file_id id,
174         const char *servicepath,
175         const struct smb_filename *smb_fname,
176         const struct timespec *old_write_time,
177         share_mode_entry_prepare_lock_fn_t fn,
178         void *private_data,
179         const char *location);
180 #define share_mode_entry_prepare_lock_add(__prepare_state, __id, \
181                 __servicepath, __smb_fname, __old_write_time, \
182                 __fn, __private_data) \
183         _share_mode_entry_prepare_lock(__prepare_state, __id, \
184                 __servicepath, __smb_fname, __old_write_time, \
185                 __fn, __private_data, __location__);
186 #define share_mode_entry_prepare_lock_del(__prepare_state, __id, \
187                 __fn, __private_data) \
188         _share_mode_entry_prepare_lock(__prepare_state, __id, \
189                 NULL, NULL, NULL, \
190                 __fn, __private_data, __location__);
191
192 typedef void (*share_mode_entry_prepare_unlock_fn_t)(
193                 struct share_mode_lock *lck,
194                 void *private_data);
195 NTSTATUS _share_mode_entry_prepare_unlock(
196         struct share_mode_entry_prepare_state *prepare_state,
197         share_mode_entry_prepare_unlock_fn_t fn,
198         void *private_data,
199         const char *location);
200 #define share_mode_entry_prepare_unlock(__prepare_state, \
201                 __fn, __private_data) \
202         _share_mode_entry_prepare_unlock(__prepare_state, \
203                 __fn, __private_data, __location__);
204
205 #endif