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