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