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