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