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