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