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