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