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