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