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