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