Revert "smbd: explain that/why we use the raw tevent_context for do_break_to_none()"
[garming/samba-autobuild/.git] / source3 / smbd / oplock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    oplock processing
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 1998 - 2001
6    Copyright (C) Volker Lendecke 2005
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 "lib/util/server_id.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "messages.h"
28 #include "../librpc/gen_ndr/ndr_open_files.h"
29
30 /*
31  * helper function used by the kernel oplock backends to post the break message
32  */
33 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
34 {
35         uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
36
37         /* Put the kernel break info into the message. */
38         push_file_id_24((char *)msg, &fsp->file_id);
39         SIVAL(msg,24,fsp->fh->gen_id);
40
41         /* Don't need to be root here as we're only ever
42            sending to ourselves. */
43
44         messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
45                            MSG_SMB_KERNEL_BREAK,
46                            msg, MSG_SMB_KERNEL_BREAK_SIZE);
47 }
48
49 /****************************************************************************
50  Attempt to set an oplock on a file. Succeeds if kernel oplocks are
51  disabled (just sets flags).
52 ****************************************************************************/
53
54 NTSTATUS set_file_oplock(files_struct *fsp)
55 {
56         struct smbd_server_connection *sconn = fsp->conn->sconn;
57         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
58         bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
59                         (koplocks != NULL);
60
61         if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
62                 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
63                            "don't support them\n"));
64                 return NT_STATUS_NOT_SUPPORTED;
65         }
66
67         if ((fsp->oplock_type != NO_OPLOCK) &&
68             use_kernel &&
69             !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
70         {
71                 return map_nt_error_from_unix(errno);
72         }
73
74         fsp->sent_oplock_break = NO_BREAK_SENT;
75         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
76                 sconn->oplocks.level_II_open++;
77         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
78                 sconn->oplocks.exclusive_open++;
79         }
80
81         DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
82                     "tv_sec = %x, tv_usec = %x\n",
83                  fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
84                  fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
85                  (int)fsp->open_time.tv_usec ));
86
87         return NT_STATUS_OK;
88 }
89
90 static void release_fsp_kernel_oplock(files_struct *fsp)
91 {
92         struct smbd_server_connection *sconn = fsp->conn->sconn;
93         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
94         bool use_kernel;
95
96         if (koplocks == NULL) {
97                 return;
98         }
99         use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
100         if (!use_kernel) {
101                 return;
102         }
103         if (fsp->oplock_type == NO_OPLOCK) {
104                 return;
105         }
106         if (fsp->oplock_type == LEASE_OPLOCK) {
107                 /*
108                  * For leases we don't touch kernel oplocks at all
109                  */
110                 return;
111         }
112
113         koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
114 }
115
116 /****************************************************************************
117  Attempt to release an oplock on a file. Decrements oplock count.
118 ****************************************************************************/
119
120 static void release_file_oplock(files_struct *fsp)
121 {
122         struct smbd_server_connection *sconn = fsp->conn->sconn;
123
124         release_fsp_kernel_oplock(fsp);
125
126         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
127                 sconn->oplocks.level_II_open--;
128         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
129                 sconn->oplocks.exclusive_open--;
130         }
131
132         SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
133         SMB_ASSERT(sconn->oplocks.level_II_open>=0);
134
135         fsp->oplock_type = NO_OPLOCK;
136         fsp->sent_oplock_break = NO_BREAK_SENT;
137
138         flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
139         delete_write_cache(fsp);
140
141         TALLOC_FREE(fsp->oplock_timeout);
142 }
143
144 /****************************************************************************
145  Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
146 ****************************************************************************/
147
148 static void downgrade_file_oplock(files_struct *fsp)
149 {
150         struct smbd_server_connection *sconn = fsp->conn->sconn;
151         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
152         bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
153                         (koplocks != NULL);
154
155         if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
156                 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
157                 return;
158         }
159
160         if (use_kernel) {
161                 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
162         }
163         fsp->oplock_type = LEVEL_II_OPLOCK;
164         sconn->oplocks.exclusive_open--;
165         sconn->oplocks.level_II_open++;
166         fsp->sent_oplock_break = NO_BREAK_SENT;
167
168         flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
169         delete_write_cache(fsp);
170
171         TALLOC_FREE(fsp->oplock_timeout);
172 }
173
174 uint32_t get_lease_type(const struct share_mode_data *d,
175                         const struct share_mode_entry *e)
176 {
177         if (e->op_type == LEASE_OPLOCK) {
178                 return d->leases[e->lease_idx].current_state;
179         }
180         return map_oplock_to_lease_type(e->op_type);
181 }
182
183 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
184 {
185         struct share_mode_data *d = lck->data;
186         struct byte_range_lock *br_lck;
187         uint32_t num_read_oplocks = 0;
188         uint32_t i;
189
190         if (fsp_lease_type_is_exclusive(fsp)) {
191                 const struct share_mode_entry *e = NULL;
192                 uint32_t e_lease_type = 0;
193
194                 /*
195                  * If we're fully exclusive, we don't need a brlock entry
196                  */
197                 remove_stale_share_mode_entries(d);
198
199                 e = find_share_mode_entry(lck, fsp);
200                 if (e != NULL) {
201                         e_lease_type = get_lease_type(d, e);
202                 }
203
204                 if (!lease_type_is_exclusive(e_lease_type)) {
205                         char *timestr = NULL;
206
207                         timestr = timeval_string(talloc_tos(),
208                                                  &fsp->open_time,
209                                                  true);
210
211                         NDR_PRINT_DEBUG(share_mode_data, d);
212                         DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
213                                 "open_time[%s] lease_type [0x%x] "
214                                 "oplock_type [0x%x]\n",
215                                 fsp_str_dbg(fsp),
216                                 file_id_string_tos(&fsp->file_id),
217                                 fsp->fh->gen_id, timestr,
218                                 e_lease_type, fsp->oplock_type);
219
220                         smb_panic("Found non-exclusive lease");
221                 }
222
223                 return true;
224         }
225
226         for (i=0; i<d->num_share_modes; i++) {
227                 struct share_mode_entry *e = &d->share_modes[i];
228                 uint32_t e_lease_type = get_lease_type(d, e);
229
230                 if (e_lease_type & SMB2_LEASE_READ) {
231                         num_read_oplocks += 1;
232                 }
233         }
234
235         br_lck = brl_get_locks_readonly(fsp);
236         if (br_lck == NULL) {
237                 return false;
238         }
239         if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
240                 return true;
241         }
242
243         br_lck = brl_get_locks(talloc_tos(), fsp);
244         if (br_lck == NULL) {
245                 return false;
246         }
247         brl_set_num_read_oplocks(br_lck, num_read_oplocks);
248         TALLOC_FREE(br_lck);
249         return true;
250 }
251
252 /****************************************************************************
253  Remove a file oplock with lock already held. Copes with level II and exclusive.
254 ****************************************************************************/
255
256 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
257 {
258         bool ret;
259
260         ret = remove_share_oplock(lck, fsp);
261         if (!ret) {
262                 DBG_ERR("failed to remove share oplock for "
263                         "file %s, %s, %s\n",
264                         fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
265                         file_id_string_tos(&fsp->file_id));
266         }
267         release_file_oplock(fsp);
268
269         ret = update_num_read_oplocks(fsp, lck);
270         if (!ret) {
271                 DBG_ERR("update_num_read_oplocks failed for "
272                         "file %s, %s, %s\n",
273                         fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
274                         file_id_string_tos(&fsp->file_id));
275         }
276
277         return ret;
278 }
279
280 /****************************************************************************
281  Remove a file oplock. Copes with level II and exclusive.
282  Locks then unlocks the share mode lock. Client can decide to go directly
283  to none even if a "break-to-level II" was sent.
284 ****************************************************************************/
285
286 bool remove_oplock(files_struct *fsp)
287 {
288         bool ret;
289         struct share_mode_lock *lck;
290
291         DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
292
293         /* Remove the oplock flag from the sharemode. */
294         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
295         if (lck == NULL) {
296                 DBG_ERR("failed to lock share entry for "
297                          "file %s\n", fsp_str_dbg(fsp));
298                 return false;
299         }
300
301         ret = remove_oplock_under_lock(fsp, lck);
302
303         TALLOC_FREE(lck);
304         return ret;
305 }
306
307 /*
308  * Deal with a reply when a break-to-level II was sent.
309  */
310 bool downgrade_oplock(files_struct *fsp)
311 {
312         bool ret;
313         struct share_mode_lock *lck;
314
315         DEBUG(10, ("downgrade_oplock called for %s\n",
316                    fsp_str_dbg(fsp)));
317
318         lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
319         if (lck == NULL) {
320                 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
321                          "file %s\n", fsp_str_dbg(fsp)));
322                 return False;
323         }
324         ret = downgrade_share_oplock(lck, fsp);
325         if (!ret) {
326                 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
327                          "for file %s, %s, file_id %s\n",
328                          fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
329                          file_id_string_tos(&fsp->file_id)));
330         }
331         downgrade_file_oplock(fsp);
332
333         ret = update_num_read_oplocks(fsp, lck);
334         if (!ret) {
335                 DEBUG(0, ("%s: update_num_read_oplocks failed for "
336                          "file %s, %s, %s\n",
337                           __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
338                          file_id_string_tos(&fsp->file_id)));
339         }
340
341         TALLOC_FREE(lck);
342         return ret;
343 }
344
345 static void lease_timeout_handler(struct tevent_context *ctx,
346                                   struct tevent_timer *te,
347                                   struct timeval now,
348                                   void *private_data)
349 {
350         struct fsp_lease *lease =
351                 talloc_get_type_abort(private_data,
352                 struct fsp_lease);
353         struct files_struct *fsp;
354         struct share_mode_lock *lck;
355         uint16_t old_epoch = lease->lease.lease_epoch;
356
357         /*
358          * This function runs without any specific impersonation
359          * and must not call any SMB_VFS operations!
360          */
361
362         fsp = file_find_one_fsp_from_lease_key(lease->sconn,
363                                                &lease->lease.lease_key);
364         if (fsp == NULL) {
365                 /* race? */
366                 TALLOC_FREE(lease->timeout);
367                 return;
368         }
369
370         /*
371          * Paranoia check: There can only be one fsp_lease per lease
372          * key
373          */
374         SMB_ASSERT(fsp->lease == lease);
375
376         lck = get_existing_share_mode_lock(
377                         talloc_tos(), fsp->file_id);
378         if (lck == NULL) {
379                 /* race? */
380                 TALLOC_FREE(lease->timeout);
381                 return;
382         }
383
384         fsp_lease_update(lck, fsp_client_guid(fsp), lease);
385
386         if (lease->lease.lease_epoch != old_epoch) {
387                 /*
388                  * If the epoch changed we need to wait for
389                  * the next timeout to happen.
390                  */
391                 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
392                            fsp_str_dbg(fsp)));
393                 TALLOC_FREE(lck);
394                 return;
395         }
396
397         if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
398                 /*
399                  * If the epoch changed we need to wait for
400                  * the next timeout to happen.
401                  */
402                 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
403                            fsp_str_dbg(fsp)));
404                 TALLOC_FREE(lck);
405                 return;
406         }
407
408         DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
409                   fsp_str_dbg(fsp)));
410         (void)downgrade_lease(lease->sconn->client->connections,
411                         1,
412                         &fsp->file_id,
413                         &lease->lease.lease_key,
414                         SMB2_LEASE_NONE);
415
416         TALLOC_FREE(lck);
417 }
418
419 bool fsp_lease_update(struct share_mode_lock *lck,
420                       const struct GUID *client_guid,
421                       struct fsp_lease *lease)
422 {
423         struct share_mode_data *d = lck->data;
424         int idx;
425         struct share_mode_lease *l = NULL;
426
427         idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
428         if (idx == -1) {
429                 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
430                 TALLOC_FREE(lease->timeout);
431                 lease->lease.lease_state = SMB2_LEASE_NONE;
432                 lease->lease.lease_epoch += 1;
433                 lease->lease.lease_flags = 0;
434                 return false;
435         }
436
437         l = &d->leases[idx];
438
439         DEBUG(10,("%s: refresh lease state\n", __func__));
440
441         /* Ensure we're in sync with current lease state. */
442         if (lease->lease.lease_epoch != l->epoch) {
443                 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
444                 TALLOC_FREE(lease->timeout);
445         }
446         lease->lease.lease_epoch = l->epoch;
447         lease->lease.lease_state = l->current_state;
448
449         if (l->breaking) {
450                 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
451
452                 if (lease->timeout == NULL) {
453                         struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
454
455                         DEBUG(10,("%s: setup timeout handler\n", __func__));
456
457                         /*
458                          * lease_timeout_handler() only accesses locking.tdb
459                          * so we don't use any impersonation and use
460                          * the raw tevent context.
461                          */
462                         lease->timeout = tevent_add_timer(lease->sconn->raw_ev_ctx,
463                                                           lease, t,
464                                                           lease_timeout_handler,
465                                                           lease);
466                         if (lease->timeout == NULL) {
467                                 DEBUG(0, ("%s: Could not add lease timeout handler\n",
468                                           __func__));
469                         }
470                 }
471         } else {
472                 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
473                 TALLOC_FREE(lease->timeout);
474         }
475
476         return true;
477 }
478
479 struct downgrade_lease_additional_state {
480         struct tevent_immediate *im;
481         struct smbXsrv_connection *xconn;
482         uint32_t break_flags;
483         struct smb2_lease_key lease_key;
484         uint32_t break_from;
485         uint32_t break_to;
486         uint16_t new_epoch;
487 };
488
489 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
490                                                struct tevent_immediate *im,
491                                                void *private_data)
492 {
493         struct downgrade_lease_additional_state *state =
494                 talloc_get_type_abort(private_data,
495                 struct downgrade_lease_additional_state);
496         struct smbXsrv_connection *xconn = state->xconn;
497         NTSTATUS status;
498
499         status = smbd_smb2_send_lease_break(xconn,
500                                             state->new_epoch,
501                                             state->break_flags,
502                                             &state->lease_key,
503                                             state->break_from,
504                                             state->break_to);
505         TALLOC_FREE(state);
506         if (!NT_STATUS_IS_OK(status)) {
507                 smbd_server_connection_terminate(xconn,
508                                                  nt_errstr(status));
509                 return;
510         }
511 }
512
513 struct downgrade_lease_fsps_state {
514         struct file_id id;
515         struct share_mode_lock *lck;
516         const struct smb2_lease_key *key;
517 };
518
519 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
520                                                  void *private_data)
521 {
522         struct downgrade_lease_fsps_state *state =
523                 (struct downgrade_lease_fsps_state *)private_data;
524
525         if (fsp->oplock_type != LEASE_OPLOCK) {
526                 return NULL;
527         }
528         if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
529                 return NULL;
530         }
531         if (!file_id_equal(&fsp->file_id, &state->id)) {
532                 return NULL;
533         }
534
535         fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
536
537         return NULL;
538 }
539
540 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
541                          uint32_t num_file_ids,
542                          const struct file_id *ids,
543                          const struct smb2_lease_key *key,
544                          uint32_t lease_state)
545 {
546         struct smbd_server_connection *sconn = xconn->client->sconn;
547         struct share_mode_lock *lck;
548         struct share_mode_data *d = NULL;
549         struct share_mode_lease *l = NULL;
550         const struct file_id id = ids[0];
551         int idx;
552         uint32_t i;
553         NTSTATUS status;
554
555         DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
556                    file_id_string_tos(&id), (unsigned)lease_state));
557
558         lck = get_existing_share_mode_lock(talloc_tos(), id);
559         if (lck == NULL) {
560                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
561         }
562         d = lck->data;
563
564         idx = find_share_mode_lease(
565                 d, &sconn->client->connections->smb2.client.guid, key);
566         if (idx == -1) {
567                 DEBUG(10, ("lease not found\n"));
568                 return NT_STATUS_INVALID_PARAMETER;
569         }
570         l = &d->leases[idx];
571
572         if (!l->breaking) {
573                 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
574                             "but we're not in breaking state\n",
575                             l->current_state, lease_state);
576                 TALLOC_FREE(lck);
577                 return NT_STATUS_UNSUCCESSFUL;
578         }
579
580         /*
581          * Can't upgrade anything: l->breaking_to_requested (and l->current_state)
582          * must be a strict bitwise superset of new_lease_state
583          */
584         if ((lease_state & l->breaking_to_requested) != lease_state) {
585                 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
586                             "- expected %"PRIu32"\n",
587                             l->current_state, lease_state,
588                             l->breaking_to_requested);
589                 TALLOC_FREE(lck);
590                 return NT_STATUS_REQUEST_NOT_ACCEPTED;
591         }
592
593         if (l->current_state != lease_state) {
594                 l->current_state = lease_state;
595                 d->modified = true;
596         }
597
598         status = NT_STATUS_OK;
599
600         d->modified = true;
601
602         if ((lease_state & ~l->breaking_to_required) != 0) {
603                 struct downgrade_lease_additional_state *state;
604
605                 DBG_INFO("lease state %"PRIu32" not fully broken from "
606                          "%"PRIu32" to %"PRIu32"\n",
607                          lease_state,
608                          l->current_state,
609                          l->breaking_to_required);
610
611                 l->breaking_to_requested = l->breaking_to_required;
612
613                 if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
614                         /*
615                          * Here we break in steps, as windows does
616                          * see the breaking3 and v2_breaking3 tests.
617                          */
618                         l->breaking_to_requested |= SMB2_LEASE_READ;
619                 }
620
621                 state = talloc_zero(xconn,
622                                     struct downgrade_lease_additional_state);
623                 if (state == NULL) {
624                         TALLOC_FREE(lck);
625                         return NT_STATUS_NO_MEMORY;
626                 }
627
628                 state->im = tevent_create_immediate(state);
629                 if (state->im == NULL) {
630                         TALLOC_FREE(state);
631                         TALLOC_FREE(lck);
632                         return NT_STATUS_NO_MEMORY;
633                 }
634
635                 state->xconn = xconn;
636                 state->lease_key = l->lease_key;
637                 state->break_from = l->current_state;
638                 state->break_to = l->breaking_to_requested;
639                 if (l->lease_version > 1) {
640                         state->new_epoch = l->epoch;
641                 }
642
643                 if (l->current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
644                         state->break_flags =
645                                 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
646                 } else {
647                         /*
648                          * This is an async break without
649                          * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
650                          *
651                          * we need to store NONE state in the
652                          * database.
653                          */
654                         l->current_state = 0;
655                         l->breaking_to_requested = 0;
656                         l->breaking_to_required = 0;
657                         l->breaking = false;
658
659                         lck->data->modified = true;
660                 }
661
662                 tevent_schedule_immediate(state->im,
663                                           xconn->client->raw_ev_ctx,
664                                           downgrade_lease_additional_trigger,
665                                           state);
666
667                 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
668         } else {
669                 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
670                           "expected %"PRIu32"\n",
671                           l->current_state,
672                           lease_state,
673                           l->breaking_to_requested);
674
675                 l->breaking_to_requested = 0;
676                 l->breaking_to_required = 0;
677                 l->breaking = false;
678
679                 d->modified = true;
680         }
681
682         DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
683                    file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
684
685         {
686                 struct downgrade_lease_fsps_state state = {
687                         .id = id, .lck = lck, .key = key,
688                 };
689
690                 files_forall(sconn, downgrade_lease_fsps, &state);
691         }
692
693         TALLOC_FREE(lck);
694         DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
695                    file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
696
697         /*
698          * Dynamic share case. Ensure other opens are copies.
699          * This will only be breaking to NONE.
700          */
701
702         for (i = 1; i < num_file_ids; i++) {
703                 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
704                 if (lck == NULL) {
705                         return NT_STATUS_OBJECT_NAME_NOT_FOUND;
706                 }
707
708                 {
709                         struct downgrade_lease_fsps_state state = {
710                                 .id = ids[i], .lck = lck, .key = key,
711                         };
712
713                         files_forall(sconn, downgrade_lease_fsps, &state);
714                 }
715
716                 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
717                         file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
718
719                 TALLOC_FREE(lck);
720         }
721
722         return status;
723 }
724
725 /****************************************************************************
726  Set up an oplock break message.
727 ****************************************************************************/
728
729 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
730
731 static void new_break_message_smb1(files_struct *fsp, int cmd,
732                                    char result[SMB1_BREAK_MESSAGE_LENGTH])
733 {
734         memset(result,'\0',smb_size);
735         srv_set_message(result,8,0,true);
736         SCVAL(result,smb_com,SMBlockingX);
737         SSVAL(result,smb_tid,fsp->conn->cnum);
738         SSVAL(result,smb_pid,0xFFFF);
739         SSVAL(result,smb_uid,0);
740         SSVAL(result,smb_mid,0xFFFF);
741         SCVAL(result,smb_vwv0,0xFF);
742         SSVAL(result,smb_vwv2,fsp->fnum);
743         SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
744         SCVAL(result,smb_vwv3+1,cmd);
745 }
746
747 /****************************************************************************
748  Function to do the waiting before sending a local break.
749 ****************************************************************************/
750
751 static void wait_before_sending_break(void)
752 {
753         long wait_time = (long)lp_oplock_break_wait_time();
754
755         if (wait_time) {
756                 smb_msleep(wait_time);
757         }
758 }
759
760 /****************************************************************************
761  Ensure that we have a valid oplock.
762 ****************************************************************************/
763
764 static files_struct *initial_break_processing(
765         struct smbd_server_connection *sconn, struct file_id id,
766         unsigned long file_id)
767 {
768         files_struct *fsp = NULL;
769
770         DEBUG(3, ("initial_break_processing: called for %s/%u\n"
771                   "Current oplocks_open (exclusive = %d, levelII = %d)\n",
772                   file_id_string_tos(&id), (int)file_id,
773                   sconn->oplocks.exclusive_open,
774                   sconn->oplocks.level_II_open));
775
776         /*
777          * We need to search the file open table for the
778          * entry containing this dev and inode, and ensure
779          * we have an oplock on it.
780          */
781
782         fsp = file_find_dif(sconn, id, file_id);
783
784         if(fsp == NULL) {
785                 /* The file could have been closed in the meantime - return success. */
786                 DEBUG(3, ("initial_break_processing: cannot find open file "
787                           "with file_id %s gen_id = %lu, allowing break to "
788                           "succeed.\n", file_id_string_tos(&id), file_id));
789                 return NULL;
790         }
791
792         /* Ensure we have an oplock on the file */
793
794         /*
795          * There is a potential race condition in that an oplock could
796          * have been broken due to another udp request, and yet there are
797          * still oplock break messages being sent in the udp message
798          * queue for this file. So return true if we don't have an oplock,
799          * as we may have just freed it.
800          */
801
802         if(fsp->oplock_type == NO_OPLOCK) {
803                 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
804                           "gen_id = %lu) has no oplock. Allowing break to "
805                           "succeed regardless.\n", fsp_str_dbg(fsp),
806                           file_id_string_tos(&id), fsp->fh->gen_id));
807                 return NULL;
808         }
809
810         return fsp;
811 }
812
813 static void oplock_timeout_handler(struct tevent_context *ctx,
814                                    struct tevent_timer *te,
815                                    struct timeval now,
816                                    void *private_data)
817 {
818         files_struct *fsp = (files_struct *)private_data;
819
820         /*
821          * Note this function doesn't run under any specific impersonation and
822          * is not expected to call any SMB_VFS operation!
823          */
824
825         SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
826
827         /* Remove the timed event handler. */
828         TALLOC_FREE(fsp->oplock_timeout);
829         DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
830                   fsp_str_dbg(fsp)));
831         remove_oplock(fsp);
832 }
833
834 /*******************************************************************
835  Add a timeout handler waiting for the client reply.
836 *******************************************************************/
837
838 static void add_oplock_timeout_handler(files_struct *fsp)
839 {
840         if (fsp->oplock_timeout != NULL) {
841                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
842                           "around\n"));
843         }
844
845         /*
846          * For now we keep the logic and use the
847          * raw event context. We're called from
848          * the messaging system from a raw event context.
849          * Also oplock_timeout_handler doesn't invoke
850          * SMB_VFS calls.
851          */
852         fsp->oplock_timeout =
853                 tevent_add_timer(fsp->conn->sconn->raw_ev_ctx, fsp,
854                                  timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
855                                  oplock_timeout_handler, fsp);
856
857         if (fsp->oplock_timeout == NULL) {
858                 DEBUG(0, ("Could not add oplock timeout handler\n"));
859         }
860 }
861
862 static void send_break_message_smb1(files_struct *fsp, int level)
863 {
864         struct smbXsrv_connection *xconn = NULL;
865         char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
866
867         /*
868          * For SMB1 we only have one connection
869          */
870         xconn = fsp->conn->sconn->client->connections;
871
872         new_break_message_smb1(fsp, level, break_msg);
873
874         show_msg(break_msg);
875         if (!srv_send_smb(xconn,
876                         break_msg, false, 0,
877                         IS_CONN_ENCRYPTED(fsp->conn),
878                         NULL)) {
879                 exit_server_cleanly("send_break_message_smb1: "
880                         "srv_send_smb failed.");
881         }
882 }
883
884 /*******************************************************************
885  This handles the generic oplock break message from another smbd.
886 *******************************************************************/
887
888 static void process_oplock_break_message(struct messaging_context *msg_ctx,
889                                          void *private_data,
890                                          uint32_t msg_type,
891                                          struct server_id src,
892                                          DATA_BLOB *data)
893 {
894         struct file_id id;
895         struct share_mode_entry msg;
896         files_struct *fsp;
897         bool use_kernel;
898         struct smbd_server_connection *sconn =
899                 talloc_get_type_abort(private_data,
900                 struct smbd_server_connection);
901         struct server_id self = messaging_server_id(sconn->msg_ctx);
902         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
903         uint16_t break_from;
904         uint16_t break_to;
905         bool break_needed = true;
906         struct server_id_buf tmp;
907
908         if (data->data == NULL) {
909                 DEBUG(0, ("Got NULL buffer\n"));
910                 return;
911         }
912
913         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
914                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
915                 return;
916         }
917
918         /* De-linearize incoming message. */
919         message_to_share_mode_entry(&id, &msg, (char *)data->data);
920         break_to = msg.op_type;
921
922         DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
923                    (unsigned)break_to, server_id_str_buf(src, &tmp),
924                    file_id_string_tos(&id),
925                    (unsigned long long)msg.share_file_id));
926
927         fsp = initial_break_processing(sconn, id, msg.share_file_id);
928
929         if (fsp == NULL) {
930                 /* We hit a race here. Break messages are sent, and before we
931                  * get to process this message, we have closed the file. */
932                 DEBUG(3, ("Did not find fsp\n"));
933                 return;
934         }
935
936         break_from = fsp_lease_type(fsp);
937
938         if (fsp->oplock_type != LEASE_OPLOCK) {
939                 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
940                         /*
941                          * Nothing to do anymore
942                          */
943                         DEBUG(10, ("fsp->sent_oplock_break = %d\n",
944                                    fsp->sent_oplock_break));
945                         return;
946                 }
947         }
948
949         if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
950                 DEBUG(10, ("client_caps without level2 oplocks\n"));
951                 break_to &= ~SMB2_LEASE_READ;
952         }
953
954         use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
955                         (koplocks != NULL);
956         if (use_kernel) {
957                 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
958                 break_to &= ~SMB2_LEASE_READ;
959         }
960
961         if (!lp_level2_oplocks(SNUM(fsp->conn))) {
962                 DEBUG(10, ("no level2 oplocks by config\n"));
963                 break_to &= ~SMB2_LEASE_READ;
964         }
965
966         if (fsp->oplock_type == LEASE_OPLOCK) {
967                 struct share_mode_lock *lck;
968                 int idx;
969
970                 lck = get_existing_share_mode_lock(
971                         talloc_tos(), fsp->file_id);
972                 if (lck == NULL) {
973                         /*
974                          * We hit a race here. Break messages are sent, and
975                          * before we get to process this message, we have closed
976                          * the file.
977                          */
978                         DEBUG(3, ("Did not find share_mode\n"));
979                         return;
980                 }
981
982                 idx = find_share_mode_lease(
983                         lck->data,
984                         fsp_client_guid(fsp),
985                         &fsp->lease->lease.lease_key);
986                 if (idx != -1) {
987                         struct share_mode_lease *l;
988                         l = &lck->data->leases[idx];
989
990                         break_from = l->current_state;
991                         break_to &= l->current_state;
992
993                         if (l->breaking) {
994                                 break_to &= l->breaking_to_required;
995                                 if (l->breaking_to_required != break_to) {
996                                         /*
997                                          * Note we don't increment the epoch
998                                          * here, which might be a bug in
999                                          * Windows too...
1000                                          */
1001                                         l->breaking_to_required = break_to;
1002                                         lck->data->modified = true;
1003                                 }
1004                                 break_needed = false;
1005                         } else if (l->current_state == break_to) {
1006                                 break_needed = false;
1007                         } else if (l->current_state == SMB2_LEASE_READ) {
1008                                 l->current_state = SMB2_LEASE_NONE;
1009                                 /* Need to increment the epoch */
1010                                 l->epoch += 1;
1011                                 lck->data->modified = true;
1012                         } else {
1013                                 l->breaking = true;
1014                                 l->breaking_to_required = break_to;
1015                                 l->breaking_to_requested = break_to;
1016                                 /* Need to increment the epoch */
1017                                 l->epoch += 1;
1018                                 lck->data->modified = true;
1019                         }
1020
1021                         /* Ensure we're in sync with current lease state. */
1022                         fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
1023                 }
1024
1025                 TALLOC_FREE(lck);
1026         }
1027
1028         if (!break_needed) {
1029                 DEBUG(10,("%s: skip break\n", __func__));
1030                 return;
1031         }
1032
1033         if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1034                 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1035                           file_id_string_tos(&fsp->file_id),
1036                           fsp_str_dbg(fsp)));
1037                 return;
1038         }
1039
1040         DEBUG(10, ("break_from=%u, break_to=%u\n",
1041                    (unsigned)break_from, (unsigned)break_to));
1042
1043         if ((break_from == break_to) && !break_needed) {
1044                 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1045                           (unsigned)break_to,
1046                           file_id_string_tos(&fsp->file_id),
1047                           fsp_str_dbg(fsp)));
1048                 return;
1049         }
1050
1051         /* Need to wait before sending a break
1052            message if we sent ourselves this message. */
1053         if (serverid_equal(&self, &src)) {
1054                 wait_before_sending_break();
1055         }
1056
1057         if (sconn->using_smb2) {
1058                 send_break_message_smb2(fsp, break_from, break_to);
1059         } else {
1060                 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1061                                         OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1062         }
1063
1064         if ((break_from == SMB2_LEASE_READ) &&
1065             (break_to == SMB2_LEASE_NONE)) {
1066                 /*
1067                  * This is an async break without a reply and thus no timeout
1068                  *
1069                  * leases are handled above.
1070                  */
1071                 if (fsp->oplock_type != LEASE_OPLOCK) {
1072                         remove_oplock(fsp);
1073                 }
1074                 return;
1075         }
1076         if (fsp->oplock_type == LEASE_OPLOCK) {
1077                 return;
1078         }
1079
1080         fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1081                 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1082
1083         add_oplock_timeout_handler(fsp);
1084 }
1085
1086 /*******************************************************************
1087  This handles the kernel oplock break message.
1088 *******************************************************************/
1089
1090 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1091                                         void *private_data,
1092                                         uint32_t msg_type,
1093                                         struct server_id src,
1094                                         DATA_BLOB *data)
1095 {
1096         struct file_id id;
1097         unsigned long file_id;
1098         files_struct *fsp;
1099         struct smbd_server_connection *sconn =
1100                 talloc_get_type_abort(private_data,
1101                 struct smbd_server_connection);
1102         struct server_id_buf tmp;
1103
1104         if (data->data == NULL) {
1105                 DEBUG(0, ("Got NULL buffer\n"));
1106                 return;
1107         }
1108
1109         if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1110                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1111                 return;
1112         }
1113
1114         /* Pull the data from the message. */
1115         pull_file_id_24((char *)data->data, &id);
1116         file_id = (unsigned long)IVAL(data->data, 24);
1117
1118         DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1119                    server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1120                    (unsigned int)file_id));
1121
1122         fsp = initial_break_processing(sconn, id, file_id);
1123
1124         if (fsp == NULL) {
1125                 DEBUG(3, ("Got a kernel oplock break message for a file "
1126                           "I don't know about\n"));
1127                 return;
1128         }
1129
1130         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1131                 /* This is ok, kernel oplocks come in completely async */
1132                 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1133                           "break reply\n"));
1134                 return;
1135         }
1136
1137         if (sconn->using_smb2) {
1138                 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1139         } else {
1140                 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1141         }
1142
1143         fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1144
1145         add_oplock_timeout_handler(fsp);
1146 }
1147
1148 static bool file_has_read_oplocks(struct files_struct *fsp)
1149 {
1150         struct byte_range_lock *brl;
1151         uint32_t num_read_oplocks = 0;
1152
1153         brl = brl_get_locks_readonly(fsp);
1154         if (brl == NULL) {
1155                 return false;
1156         }
1157
1158         num_read_oplocks = brl_num_read_oplocks(brl);
1159
1160         DBG_DEBUG("num_read_oplocks = %"PRIu32"\n", num_read_oplocks);
1161
1162         return (num_read_oplocks != 0);
1163 }
1164
1165 struct break_to_none_state {
1166         struct smbd_server_connection *sconn;
1167         struct file_id id;
1168         struct smb2_lease_key lease_key;
1169         struct GUID client_guid;
1170 };
1171 static void do_break_to_none(struct tevent_context *ctx,
1172                              struct tevent_immediate *im,
1173                              void *private_data);
1174
1175 /****************************************************************************
1176  This function is called on any file modification or lock request. If a file
1177  is level 2 oplocked then it must tell all other level 2 holders to break to
1178  none.
1179 ****************************************************************************/
1180
1181 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1182                                               enum level2_contention_type type)
1183 {
1184         struct smbd_server_connection *sconn = fsp->conn->sconn;
1185         struct tevent_immediate *im;
1186         struct break_to_none_state *state;
1187         bool has_read_oplocks;
1188
1189         /*
1190          * If this file is level II oplocked then we need
1191          * to grab the shared memory lock and inform all
1192          * other files with a level II lock that they need
1193          * to flush their read caches. We keep the lock over
1194          * the shared memory area whilst doing this.
1195          */
1196
1197         if (fsp_lease_type_is_exclusive(fsp)) {
1198                 /*
1199                  * There can't be any level2 oplocks, we're alone.
1200                  */
1201                 return;
1202         }
1203
1204         has_read_oplocks = file_has_read_oplocks(fsp);
1205         if (!has_read_oplocks) {
1206                 DEBUG(10, ("No read oplocks around\n"));
1207                 return;
1208         }
1209
1210         /*
1211          * When we get here we might have a brlock entry locked. Also
1212          * locking the share mode entry would violate the locking
1213          * order. Breaking level2 oplocks to none is asynchronous
1214          * anyway, so we postpone this into an immediate event.
1215          */
1216
1217         state = talloc_zero(sconn, struct break_to_none_state);
1218         if (state == NULL) {
1219                 DEBUG(1, ("talloc failed\n"));
1220                 return;
1221         }
1222         state->sconn = sconn;
1223         state->id = fsp->file_id;
1224
1225         if (fsp->oplock_type == LEASE_OPLOCK) {
1226                 state->client_guid = *fsp_client_guid(fsp);
1227                 state->lease_key = fsp->lease->lease.lease_key;
1228                 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1229                            state->lease_key.data[0],
1230                            state->lease_key.data[1]));
1231         }
1232
1233         im = tevent_create_immediate(state);
1234         if (im == NULL) {
1235                 DEBUG(1, ("tevent_create_immediate failed\n"));
1236                 TALLOC_FREE(state);
1237                 return;
1238         }
1239         tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1240 }
1241
1242 static void send_break_to_none(struct messaging_context *msg_ctx,
1243                                const struct file_id *id,
1244                                const struct share_mode_entry *e)
1245 {
1246         char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1247
1248         share_mode_entry_to_message(msg, id, e);
1249         /* Overload entry->op_type */
1250         SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1251
1252         messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1253                            (uint8_t *)msg, sizeof(msg));
1254 }
1255
1256 static void do_break_to_none(struct tevent_context *ctx,
1257                              struct tevent_immediate *im,
1258                              void *private_data)
1259 {
1260         struct break_to_none_state *state = talloc_get_type_abort(
1261                 private_data, struct break_to_none_state);
1262         uint32_t i;
1263         struct share_mode_lock *lck;
1264         struct share_mode_data *d;
1265
1266         lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1267         if (lck == NULL) {
1268                 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1269                           __func__, file_id_string_tos(&state->id)));
1270                 goto done;
1271         }
1272         d = lck->data;
1273
1274         /*
1275          * Walk leases and oplocks separately: We have to send one break per
1276          * lease. If we have multiple share_mode_entry having a common lease,
1277          * we would break the lease twice if we don't walk the leases list
1278          * separately.
1279          */
1280
1281         for (i=0; i<d->num_leases; i++) {
1282                 struct share_mode_lease *l = &d->leases[i];
1283                 struct share_mode_entry *e = NULL;
1284                 uint32_t j;
1285
1286                 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1287                         continue;
1288                 }
1289                 if (smb2_lease_equal(&state->client_guid,
1290                                      &state->lease_key,
1291                                      &l->client_guid,
1292                                      &l->lease_key)) {
1293                         DEBUG(10, ("Don't break our own lease\n"));
1294                         continue;
1295                 }
1296
1297                 for (j=0; j<d->num_share_modes; j++) {
1298                         e = &d->share_modes[j];
1299
1300                         if (!is_valid_share_mode_entry(e)) {
1301                                 continue;
1302                         }
1303                         if (e->lease_idx == i) {
1304                                 break;
1305                         }
1306                 }
1307                 if (j == d->num_share_modes) {
1308                         DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1309                                   i));
1310                         continue;
1311                 }
1312
1313                 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1314                            "%"PRIu32"\n", i, j));
1315
1316                 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1317         }
1318
1319         for(i = 0; i < d->num_share_modes; i++) {
1320                 struct share_mode_entry *e = &d->share_modes[i];
1321
1322                 if (!is_valid_share_mode_entry(e)) {
1323                         continue;
1324                 }
1325                 if (e->op_type == LEASE_OPLOCK) {
1326                         /*
1327                          * Took care of those in the loop above
1328                          */
1329                         continue;
1330                 }
1331
1332                 /*
1333                  * As there could have been multiple writes waiting at the
1334                  * lock_share_entry gate we may not be the first to
1335                  * enter. Hence the state of the op_types in the share mode
1336                  * entries may be partly NO_OPLOCK and partly LEVEL_II
1337                  * oplock. It will do no harm to re-send break messages to
1338                  * those smbd's that are still waiting their turn to remove
1339                  * their LEVEL_II state, and also no harm to ignore existing
1340                  * NO_OPLOCK states. JRA.
1341                  */
1342
1343                 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1344                            i, e->op_type ));
1345
1346                 if (e->op_type == NO_OPLOCK) {
1347                         continue;
1348                 }
1349
1350                 /* Paranoia .... */
1351                 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1352                         DEBUG(0,("%s: PANIC. "
1353                                  "share mode entry %d is an exclusive "
1354                                  "oplock !\n", __func__, i ));
1355                         TALLOC_FREE(lck);
1356                         abort();
1357                 }
1358
1359                 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1360         }
1361
1362         /* We let the message receivers handle removing the oplock state
1363            in the share mode lock db. */
1364
1365         TALLOC_FREE(lck);
1366 done:
1367         TALLOC_FREE(state);
1368         return;
1369 }
1370
1371 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1372                                   enum level2_contention_type type)
1373 {
1374         contend_level2_oplocks_begin_default(fsp, type);
1375 }
1376
1377 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1378                                 enum level2_contention_type type)
1379 {
1380         return;
1381 }
1382
1383 /****************************************************************************
1384  Linearize a share mode entry struct to an internal oplock break message.
1385 ****************************************************************************/
1386
1387 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1388                                  const struct share_mode_entry *e)
1389 {
1390         SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1391         SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1392         SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1393         SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1394         SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1395         SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1396         SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1397         SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1398         /*
1399          * "id" used to be part of share_mode_entry, thus the strange
1400          * place to put this. Feel free to move somewhere else :-)
1401          */
1402         push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1403         SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1404         SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1405         SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1406         SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1407         SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1408 }
1409
1410 /****************************************************************************
1411  De-linearize an internal oplock break message to a share mode entry struct.
1412 ****************************************************************************/
1413
1414 void message_to_share_mode_entry(struct file_id *id,
1415                                  struct share_mode_entry *e,
1416                                  const char *msg)
1417 {
1418         e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1419         e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1420         e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1421         e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1422         e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1423         e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1424         e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1425         e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1426         /*
1427          * "id" used to be part of share_mode_entry, thus the strange
1428          * place to put this. Feel free to move somewhere else :-)
1429          */
1430         pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1431         e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1432         e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1433         e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1434         e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1435         e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1436 }
1437
1438 /****************************************************************************
1439  Setup oplocks for this process.
1440 ****************************************************************************/
1441
1442 bool init_oplocks(struct smbd_server_connection *sconn)
1443 {
1444         DEBUG(3,("init_oplocks: initializing messages.\n"));
1445
1446         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1447                            process_oplock_break_message);
1448         messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1449                            process_kernel_oplock_break);
1450         return true;
1451 }
1452
1453 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1454 {
1455         struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1456
1457         /* only initialize once */
1458         if (koplocks == NULL) {
1459 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1460                 koplocks = linux_init_kernel_oplocks(sconn);
1461 #endif
1462                 sconn->oplocks.kernel_ops = koplocks;
1463         }
1464 }