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