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