smbd: Simplify sending oplock_break_message
[Samba.git] / source3 / smbd / smb2_oplock.c
blobeec805f1b4658e4d6ac297650f4aeaf9bd94467c
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
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.
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.
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/>.
22 #define DBGC_CLASS DBGC_LOCKING
23 #include "includes.h"
24 #include "lib/util/server_id.h"
25 #include "locking/share_mode_lock.h"
26 #include "smbd/smbd.h"
27 #include "smbd/globals.h"
28 #include "messages.h"
29 #include "locking/leases_db.h"
30 #include "../librpc/gen_ndr/ndr_open_files.h"
33 * helper function used by the kernel oplock backends to post the break message
35 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
37 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
39 /* Put the kernel break info into the message. */
40 push_file_id_24((char *)msg, &fsp->file_id);
41 SIVAL(msg, 24, fh_get_gen_id(fsp->fh));
43 /* Don't need to be root here as we're only ever
44 sending to ourselves. */
46 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
47 MSG_SMB_KERNEL_BREAK,
48 msg, MSG_SMB_KERNEL_BREAK_SIZE);
51 /****************************************************************************
52 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
53 disabled (just sets flags).
54 ****************************************************************************/
56 NTSTATUS set_file_oplock(files_struct *fsp)
58 struct smbd_server_connection *sconn = fsp->conn->sconn;
59 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
60 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
61 (koplocks != NULL);
62 struct file_id_buf buf;
64 smb_vfs_assert_allowed();
66 if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
67 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
68 "don't support them\n"));
69 return NT_STATUS_NOT_SUPPORTED;
72 if ((fsp->oplock_type != NO_OPLOCK) &&
73 use_kernel &&
74 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
76 return map_nt_error_from_unix(errno);
79 fsp->sent_oplock_break = NO_BREAK_SENT;
80 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
81 sconn->oplocks.level_II_open++;
82 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
83 sconn->oplocks.exclusive_open++;
86 DBG_INFO("granted oplock on file %s, %s/%"PRIu64", "
87 "tv_sec = %x, tv_usec = %x\n",
88 fsp_str_dbg(fsp),
89 file_id_str_buf(fsp->file_id, &buf),
90 fh_get_gen_id(fsp->fh),
91 (int)fsp->open_time.tv_sec,
92 (int)fsp->open_time.tv_usec);
94 return NT_STATUS_OK;
97 static void release_fsp_kernel_oplock(files_struct *fsp)
99 struct smbd_server_connection *sconn = fsp->conn->sconn;
100 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
101 bool use_kernel;
103 smb_vfs_assert_allowed();
105 if (koplocks == NULL) {
106 return;
108 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
109 if (!use_kernel) {
110 return;
112 if (fsp->oplock_type == NO_OPLOCK) {
113 return;
115 if (fsp->oplock_type == LEASE_OPLOCK) {
117 * For leases we don't touch kernel oplocks at all
119 return;
122 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
125 /****************************************************************************
126 Attempt to release an oplock on a file. Decrements oplock count.
127 ****************************************************************************/
129 void release_file_oplock(files_struct *fsp)
131 struct smbd_server_connection *sconn = fsp->conn->sconn;
133 release_fsp_kernel_oplock(fsp);
135 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
136 sconn->oplocks.level_II_open--;
137 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
138 sconn->oplocks.exclusive_open--;
141 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
142 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
144 fsp->oplock_type = NO_OPLOCK;
145 fsp->sent_oplock_break = NO_BREAK_SENT;
147 TALLOC_FREE(fsp->oplock_timeout);
150 /****************************************************************************
151 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
152 ****************************************************************************/
154 static void downgrade_file_oplock(files_struct *fsp)
156 struct smbd_server_connection *sconn = fsp->conn->sconn;
157 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
158 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
159 (koplocks != NULL);
161 smb_vfs_assert_allowed();
163 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
164 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
165 return;
168 if (use_kernel) {
169 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
171 fsp->oplock_type = LEVEL_II_OPLOCK;
172 sconn->oplocks.exclusive_open--;
173 sconn->oplocks.level_II_open++;
174 fsp->sent_oplock_break = NO_BREAK_SENT;
176 TALLOC_FREE(fsp->oplock_timeout);
179 uint32_t get_lease_type(struct share_mode_entry *e, struct file_id id)
181 struct GUID_txt_buf guid_strbuf;
182 struct file_id_buf file_id_strbuf;
183 NTSTATUS status;
184 uint32_t current_state;
186 if (e->op_type != LEASE_OPLOCK) {
187 return map_oplock_to_lease_type(e->op_type);
190 status = leases_db_get(&e->client_guid,
191 &e->lease_key,
192 &id,
193 &current_state,
194 NULL, /* breaking */
195 NULL, /* breaking_to_requested */
196 NULL, /* breaking_to_required */
197 NULL, /* lease_version */
198 NULL); /* epoch */
199 if (NT_STATUS_IS_OK(status)) {
200 return current_state;
203 if (share_entry_stale_pid(e)) {
204 return 0;
206 DBG_ERR("leases_db_get for client_guid [%s] "
207 "lease_key [%"PRIu64"/%"PRIu64"] "
208 "file_id [%s] failed: %s\n",
209 GUID_buf_string(&e->client_guid, &guid_strbuf),
210 e->lease_key.data[0],
211 e->lease_key.data[1],
212 file_id_str_buf(id, &file_id_strbuf),
213 nt_errstr(status));
214 smb_panic("leases_db_get() failed");
217 /****************************************************************************
218 Remove a file oplock. Copes with level II and exclusive.
219 Locks then unlocks the share mode lock. Client can decide to go directly
220 to none even if a "break-to-level II" was sent.
221 ****************************************************************************/
223 bool remove_oplock(files_struct *fsp)
225 bool ret;
226 struct share_mode_lock *lck;
228 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
230 /* Remove the oplock flag from the sharemode. */
231 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
232 if (lck == NULL) {
233 DBG_ERR("failed to lock share entry for "
234 "file %s\n", fsp_str_dbg(fsp));
235 return false;
238 ret = remove_share_oplock(lck, fsp);
239 if (!ret) {
240 struct file_id_buf buf;
242 DBG_ERR("failed to remove share oplock for "
243 "file %s, %s, %s\n",
244 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
245 file_id_str_buf(fsp->file_id, &buf));
247 release_file_oplock(fsp);
249 TALLOC_FREE(lck);
250 return ret;
254 * Deal with a reply when a break-to-level II was sent.
256 bool downgrade_oplock(files_struct *fsp)
258 bool ret;
259 struct share_mode_lock *lck;
261 DEBUG(10, ("downgrade_oplock called for %s\n",
262 fsp_str_dbg(fsp)));
264 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
265 if (lck == NULL) {
266 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
267 "file %s\n", fsp_str_dbg(fsp)));
268 return False;
270 ret = downgrade_share_oplock(lck, fsp);
271 if (!ret) {
272 struct file_id_buf idbuf;
273 DBG_ERR("failed to downgrade share oplock "
274 "for file %s, %s, file_id %s\n",
275 fsp_str_dbg(fsp),
276 fsp_fnum_dbg(fsp),
277 file_id_str_buf(fsp->file_id, &idbuf));
279 downgrade_file_oplock(fsp);
281 TALLOC_FREE(lck);
282 return ret;
285 static void lease_timeout_handler(struct tevent_context *ctx,
286 struct tevent_timer *te,
287 struct timeval now,
288 void *private_data)
290 struct fsp_lease *lease =
291 talloc_get_type_abort(private_data,
292 struct fsp_lease);
293 struct files_struct *fsp;
294 struct share_mode_lock *lck;
295 uint16_t old_epoch = lease->lease.lease_epoch;
297 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
298 &lease->lease.lease_key);
299 if (fsp == NULL) {
300 /* race? */
301 TALLOC_FREE(lease->timeout);
302 return;
306 * Paranoia check: There can only be one fsp_lease per lease
307 * key
309 SMB_ASSERT(fsp->lease == lease);
311 lck = get_existing_share_mode_lock(
312 talloc_tos(), fsp->file_id);
313 if (lck == NULL) {
314 /* race? */
315 TALLOC_FREE(lease->timeout);
316 return;
319 fsp_lease_update(fsp);
321 if (lease->lease.lease_epoch != old_epoch) {
323 * If the epoch changed we need to wait for
324 * the next timeout to happen.
326 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
327 fsp_str_dbg(fsp)));
328 TALLOC_FREE(lck);
329 return;
332 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
334 * If the epoch changed we need to wait for
335 * the next timeout to happen.
337 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
338 fsp_str_dbg(fsp)));
339 TALLOC_FREE(lck);
340 return;
343 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
344 fsp_str_dbg(fsp)));
345 (void)downgrade_lease(lease->sconn->client,
347 &fsp->file_id,
348 &lease->lease.lease_key,
349 SMB2_LEASE_NONE);
351 TALLOC_FREE(lck);
354 bool fsp_lease_update(struct files_struct *fsp)
356 const struct GUID *client_guid = fsp_client_guid(fsp);
357 struct fsp_lease *lease = fsp->lease;
358 uint32_t current_state;
359 bool breaking;
360 uint16_t lease_version, epoch;
361 NTSTATUS status;
363 status = leases_db_get(client_guid,
364 &lease->lease.lease_key,
365 &fsp->file_id,
366 &current_state,
367 &breaking,
368 NULL, /* breaking_to_requested */
369 NULL, /* breaking_to_required */
370 &lease_version,
371 &epoch);
372 if (!NT_STATUS_IS_OK(status)) {
373 DBG_WARNING("Could not find lease entry: %s\n",
374 nt_errstr(status));
375 TALLOC_FREE(lease->timeout);
376 lease->lease.lease_state = SMB2_LEASE_NONE;
377 lease->lease.lease_epoch += 1;
378 lease->lease.lease_flags = 0;
379 return false;
382 DEBUG(10,("%s: refresh lease state\n", __func__));
384 /* Ensure we're in sync with current lease state. */
385 if (lease->lease.lease_epoch != epoch) {
386 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
387 TALLOC_FREE(lease->timeout);
389 lease->lease.lease_epoch = epoch;
390 lease->lease.lease_state = current_state;
392 if (breaking) {
393 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
395 if (lease->timeout == NULL) {
396 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
398 DEBUG(10,("%s: setup timeout handler\n", __func__));
400 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
401 lease, t,
402 lease_timeout_handler,
403 lease);
404 if (lease->timeout == NULL) {
405 DEBUG(0, ("%s: Could not add lease timeout handler\n",
406 __func__));
409 } else {
410 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
411 TALLOC_FREE(lease->timeout);
414 return true;
417 struct downgrade_lease_additional_state {
418 struct tevent_immediate *im;
419 struct smbXsrv_client *client;
420 uint32_t break_flags;
421 struct smb2_lease_key lease_key;
422 uint32_t break_from;
423 uint32_t break_to;
424 uint16_t new_epoch;
427 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
428 struct tevent_immediate *im,
429 void *private_data)
431 struct downgrade_lease_additional_state *state =
432 talloc_get_type_abort(private_data,
433 struct downgrade_lease_additional_state);
434 NTSTATUS status;
436 status = smbd_smb2_send_lease_break(state->client,
437 state->new_epoch,
438 state->break_flags,
439 &state->lease_key,
440 state->break_from,
441 state->break_to);
442 if (!NT_STATUS_IS_OK(status)) {
443 smbd_server_disconnect_client(state->client,
444 nt_errstr(status));
446 TALLOC_FREE(state);
449 struct fsps_lease_update_state {
450 const struct file_id *id;
451 const struct smb2_lease_key *key;
454 static struct files_struct *fsps_lease_update_fn(
455 struct files_struct *fsp, void *private_data)
457 struct fsps_lease_update_state *state =
458 (struct fsps_lease_update_state *)private_data;
460 if (fsp->oplock_type != LEASE_OPLOCK) {
461 return NULL;
463 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
464 return NULL;
466 if (!file_id_equal(&fsp->file_id, state->id)) {
467 return NULL;
470 fsp_lease_update(fsp);
472 return NULL;
475 static void fsps_lease_update(struct smbd_server_connection *sconn,
476 const struct file_id *id,
477 const struct smb2_lease_key *key)
479 struct fsps_lease_update_state state = { .id = id, .key = key };
480 files_forall(sconn, fsps_lease_update_fn, &state);
483 NTSTATUS downgrade_lease(struct smbXsrv_client *client,
484 uint32_t num_file_ids,
485 const struct file_id *ids,
486 const struct smb2_lease_key *key,
487 uint32_t lease_state)
489 struct smbd_server_connection *sconn = client->sconn;
490 const struct GUID *client_guid = NULL;
491 struct share_mode_lock *lck;
492 const struct file_id id = ids[0];
493 uint32_t current_state, breaking_to_requested, breaking_to_required;
494 bool breaking;
495 uint16_t lease_version, epoch;
496 NTSTATUS status;
497 uint32_t i;
498 struct file_id_buf idbuf;
500 DBG_DEBUG("Downgrading %s to %"PRIu32"\n",
501 file_id_str_buf(id, &idbuf),
502 lease_state);
504 lck = get_existing_share_mode_lock(talloc_tos(), id);
505 if (lck == NULL) {
506 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
509 client_guid = &sconn->client->global->client_guid;
511 status = leases_db_get(client_guid,
512 key,
513 &id,
514 &current_state,
515 &breaking,
516 &breaking_to_requested,
517 &breaking_to_required,
518 &lease_version,
519 &epoch);
520 if (!NT_STATUS_IS_OK(status)) {
521 DBG_WARNING("leases_db_get returned %s\n",
522 nt_errstr(status));
523 TALLOC_FREE(lck);
524 return status;
527 if (!breaking) {
528 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
529 "but we're not in breaking state\n",
530 current_state, lease_state);
531 TALLOC_FREE(lck);
532 return NT_STATUS_UNSUCCESSFUL;
536 * Can't upgrade anything: breaking_to_requested (and current_state)
537 * must be a strict bitwise superset of new_lease_state
539 if ((lease_state & breaking_to_requested) != lease_state) {
540 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
541 "- expected %"PRIu32"\n",
542 current_state, lease_state,
543 breaking_to_requested);
544 TALLOC_FREE(lck);
545 return NT_STATUS_REQUEST_NOT_ACCEPTED;
548 if (current_state != lease_state) {
549 current_state = lease_state;
552 status = NT_STATUS_OK;
554 if ((lease_state & ~breaking_to_required) != 0) {
555 struct downgrade_lease_additional_state *state;
557 DBG_INFO("lease state %"PRIu32" not fully broken from "
558 "%"PRIu32" to %"PRIu32"\n",
559 lease_state,
560 current_state,
561 breaking_to_required);
563 breaking_to_requested = breaking_to_required;
565 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
567 * Here we break in steps, as windows does
568 * see the breaking3 and v2_breaking3 tests.
570 breaking_to_requested |= SMB2_LEASE_READ;
573 state = talloc_zero(client,
574 struct downgrade_lease_additional_state);
575 if (state == NULL) {
576 TALLOC_FREE(lck);
577 return NT_STATUS_NO_MEMORY;
580 state->im = tevent_create_immediate(state);
581 if (state->im == NULL) {
582 TALLOC_FREE(state);
583 TALLOC_FREE(lck);
584 return NT_STATUS_NO_MEMORY;
587 state->client = client;
588 state->lease_key = *key;
589 state->break_from = current_state;
590 state->break_to = breaking_to_requested;
591 if (lease_version > 1) {
592 state->new_epoch = epoch;
595 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
596 state->break_flags =
597 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
598 } else {
600 * This is an async break without
601 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
603 * we need to store NONE state in the
604 * database.
606 current_state = 0;
607 breaking_to_requested = 0;
608 breaking_to_required = 0;
609 breaking = false;
612 NTSTATUS set_status;
614 set_status = leases_db_set(
615 &sconn->client->global->client_guid,
616 key,
617 current_state,
618 breaking,
619 breaking_to_requested,
620 breaking_to_required,
621 lease_version,
622 epoch);
624 if (!NT_STATUS_IS_OK(set_status)) {
625 DBG_DEBUG("leases_db_set failed: %s\n",
626 nt_errstr(set_status));
627 return set_status;
632 tevent_schedule_immediate(state->im,
633 client->raw_ev_ctx,
634 downgrade_lease_additional_trigger,
635 state);
637 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
638 } else {
639 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
640 "expected %"PRIu32"\n",
641 current_state,
642 lease_state,
643 breaking_to_requested);
645 breaking_to_requested = 0;
646 breaking_to_required = 0;
647 breaking = false;
651 NTSTATUS set_status;
653 set_status = leases_db_set(
654 client_guid,
655 key,
656 current_state,
657 breaking,
658 breaking_to_requested,
659 breaking_to_required,
660 lease_version,
661 epoch);
663 if (!NT_STATUS_IS_OK(set_status)) {
664 DBG_DEBUG("leases_db_set failed: %s\n",
665 nt_errstr(set_status));
666 TALLOC_FREE(lck);
667 return set_status;
671 DBG_DEBUG("Downgrading %s to %"PRIu32" => %s\n",
672 file_id_str_buf(id, &idbuf),
673 lease_state,
674 nt_errstr(status));
676 share_mode_wakeup_waiters(id);
678 fsps_lease_update(sconn, &id, key);
680 TALLOC_FREE(lck);
682 DBG_DEBUG("Downgrading %s to %"PRIu32" => %s\n",
683 file_id_str_buf(id, &idbuf),
684 lease_state,
685 nt_errstr(status));
688 * Dynamic share case. Ensure other opens are copies.
689 * This will only be breaking to NONE.
692 for (i = 1; i < num_file_ids; i++) {
693 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
694 if (lck == NULL) {
695 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
698 fsps_lease_update(sconn, &ids[i], key);
700 DBG_DEBUG("Downgrading %s to %"PRIu32" => %s\n",
701 file_id_str_buf(ids[i], &idbuf),
702 lease_state,
703 nt_errstr(status));
705 TALLOC_FREE(lck);
708 return status;
711 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
713 /****************************************************************************
714 Function to do the waiting before sending a local break.
715 ****************************************************************************/
717 static void wait_before_sending_break(void)
719 long wait_time = (long)lp_oplock_break_wait_time();
721 if (wait_time) {
722 smb_msleep(wait_time);
726 /****************************************************************************
727 Ensure that we have a valid oplock.
728 ****************************************************************************/
730 static files_struct *initial_break_processing(
731 struct smbd_server_connection *sconn, struct file_id id,
732 unsigned long file_id)
734 files_struct *fsp = NULL;
735 struct file_id_buf idbuf;
737 DBG_NOTICE("called for %s/%u\n"
738 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
739 file_id_str_buf(id, &idbuf),
740 (int)file_id,
741 sconn->oplocks.exclusive_open,
742 sconn->oplocks.level_II_open);
745 * We need to search the file open table for the
746 * entry containing this dev and inode, and ensure
747 * we have an oplock on it.
750 fsp = file_find_dif(sconn, id, file_id);
752 if(fsp == NULL) {
753 /* The file could have been closed in the meantime - return success. */
754 DBG_NOTICE("cannot find open file "
755 "with file_id %s gen_id = %lu, allowing break to "
756 "succeed.\n",
757 file_id_str_buf(id, &idbuf),
758 file_id);
759 return NULL;
762 /* Ensure we have an oplock on the file */
765 * There is a potential race condition in that an oplock could
766 * have been broken due to another udp request, and yet there are
767 * still oplock break messages being sent in the udp message
768 * queue for this file. So return true if we don't have an oplock,
769 * as we may have just freed it.
772 if(fsp->oplock_type == NO_OPLOCK) {
773 DBG_NOTICE("file %s (file_id = %s gen_id = %"PRIu64") "
774 "has no oplock. "
775 "Allowing break to succeed regardless.\n",
776 fsp_str_dbg(fsp),
777 file_id_str_buf(id, &idbuf),
778 fh_get_gen_id(fsp->fh));
779 return NULL;
782 return fsp;
785 static void oplock_timeout_handler(struct tevent_context *ctx,
786 struct tevent_timer *te,
787 struct timeval now,
788 void *private_data)
790 files_struct *fsp = (files_struct *)private_data;
792 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
794 /* Remove the timed event handler. */
795 TALLOC_FREE(fsp->oplock_timeout);
796 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
797 fsp_str_dbg(fsp)));
798 remove_oplock(fsp);
801 /*******************************************************************
802 Add a timeout handler waiting for the client reply.
803 *******************************************************************/
805 static void add_oplock_timeout_handler(files_struct *fsp)
807 if (fsp->oplock_timeout != NULL) {
808 DEBUG(0, ("Logic problem -- have an oplock event hanging "
809 "around\n"));
812 fsp->oplock_timeout =
813 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
814 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
815 oplock_timeout_handler, fsp);
817 if (fsp->oplock_timeout == NULL) {
818 DEBUG(0, ("Could not add oplock timeout handler\n"));
822 /*******************************************************************
823 This handles the generic oplock break message from another smbd.
824 *******************************************************************/
826 static void process_oplock_break_message(struct messaging_context *msg_ctx,
827 void *private_data,
828 uint32_t msg_type,
829 struct server_id src,
830 DATA_BLOB *data)
832 struct oplock_break_message msg;
833 enum ndr_err_code ndr_err;
834 files_struct *fsp;
835 bool use_kernel;
836 struct smbd_server_connection *sconn =
837 talloc_get_type_abort(private_data,
838 struct smbd_server_connection);
839 struct server_id self = messaging_server_id(sconn->msg_ctx);
840 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
841 uint16_t break_from;
842 uint16_t break_to;
843 bool break_needed = true;
845 smb_vfs_assert_allowed();
847 ndr_err = ndr_pull_struct_blob_all_noalloc(
848 data, &msg, (ndr_pull_flags_fn_t)ndr_pull_oplock_break_message);
849 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
850 DBG_DEBUG("ndr_pull_oplock_break_message failed: %s\n",
851 ndr_errstr(ndr_err));
852 return;
854 if (DEBUGLEVEL >= 10) {
855 struct server_id_buf buf;
856 DBG_DEBUG("Got break message from %s\n",
857 server_id_str_buf(src, &buf));
858 NDR_PRINT_DEBUG(oplock_break_message, &msg);
861 break_to = msg.break_to;
862 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
864 if (fsp == NULL) {
865 /* We hit a race here. Break messages are sent, and before we
866 * get to process this message, we have closed the file. */
867 DEBUG(3, ("Did not find fsp\n"));
868 return;
871 break_from = fsp_lease_type(fsp);
873 if (fsp->oplock_type != LEASE_OPLOCK) {
874 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
876 * Nothing to do anymore
878 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
879 fsp->sent_oplock_break));
880 return;
884 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
885 DEBUG(10, ("client_caps without level2 oplocks\n"));
886 break_to &= ~SMB2_LEASE_READ;
889 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
890 (koplocks != NULL);
891 if (use_kernel) {
892 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
893 break_to &= ~SMB2_LEASE_READ;
896 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
897 DEBUG(10, ("no level2 oplocks by config\n"));
898 break_to &= ~SMB2_LEASE_READ;
901 if (fsp->oplock_type == LEASE_OPLOCK) {
902 const struct GUID *client_guid = fsp_client_guid(fsp);
903 struct share_mode_lock *lck;
904 uint32_t current_state;
905 uint32_t breaking_to_requested, breaking_to_required;
906 bool breaking;
907 uint16_t lease_version, epoch;
908 NTSTATUS status;
910 lck = get_existing_share_mode_lock(
911 talloc_tos(), fsp->file_id);
912 if (lck == NULL) {
914 * We hit a race here. Break messages are sent, and
915 * before we get to process this message, we have closed
916 * the file.
918 DEBUG(3, ("Did not find share_mode\n"));
919 return;
922 status = leases_db_get(client_guid,
923 &fsp->lease->lease.lease_key,
924 &fsp->file_id,
925 &current_state,
926 &breaking,
927 &breaking_to_requested,
928 &breaking_to_required,
929 &lease_version,
930 &epoch);
931 if (!NT_STATUS_IS_OK(status)) {
932 DBG_WARNING("leases_db_get returned %s\n",
933 nt_errstr(status));
934 TALLOC_FREE(lck);
935 return;
938 break_from = current_state;
939 break_to &= current_state;
941 if (breaking) {
942 break_to &= breaking_to_required;
943 if (breaking_to_required != break_to) {
945 * Note we don't increment the epoch
946 * here, which might be a bug in
947 * Windows too...
949 breaking_to_required = break_to;
951 break_needed = false;
952 } else if (current_state == break_to) {
953 break_needed = false;
954 } else if (current_state == SMB2_LEASE_READ) {
955 current_state = SMB2_LEASE_NONE;
956 /* Need to increment the epoch */
957 epoch += 1;
958 } else {
959 breaking = true;
960 breaking_to_required = break_to;
961 breaking_to_requested = break_to;
962 /* Need to increment the epoch */
963 epoch += 1;
967 NTSTATUS set_status;
969 set_status = leases_db_set(
970 client_guid,
971 &fsp->lease->lease.lease_key,
972 current_state,
973 breaking,
974 breaking_to_requested,
975 breaking_to_required,
976 lease_version,
977 epoch);
979 if (!NT_STATUS_IS_OK(set_status)) {
980 DBG_DEBUG("leases_db_set failed: %s\n",
981 nt_errstr(set_status));
982 return;
986 /* Ensure we're in sync with current lease state. */
987 fsp_lease_update(fsp);
989 TALLOC_FREE(lck);
992 if (!break_needed) {
993 DEBUG(10,("%s: skip break\n", __func__));
994 return;
997 if (break_from == SMB2_LEASE_NONE) {
998 struct file_id_buf idbuf;
999 DBG_NOTICE("Already downgraded oplock to none on %s: %s\n",
1000 file_id_str_buf(fsp->file_id, &idbuf),
1001 fsp_str_dbg(fsp));
1002 return;
1005 DEBUG(10, ("break_from=%u, break_to=%u\n",
1006 (unsigned)break_from, (unsigned)break_to));
1008 if (break_from == break_to) {
1009 struct file_id_buf idbuf;
1010 DBG_NOTICE("Already downgraded oplock to %u on %s: %s\n",
1011 (unsigned)break_to,
1012 file_id_str_buf(fsp->file_id, &idbuf),
1013 fsp_str_dbg(fsp));
1014 return;
1017 /* Need to wait before sending a break
1018 message if we sent ourselves this message. */
1019 if (server_id_equal(&self, &src)) {
1020 wait_before_sending_break();
1023 #if defined(WITH_SMB1SERVER)
1024 if (conn_using_smb2(sconn)) {
1025 #endif
1026 send_break_message_smb2(fsp, break_from, break_to);
1027 #if defined(WITH_SMB1SERVER)
1028 } else {
1029 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1030 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1032 #endif
1034 if ((break_from == SMB2_LEASE_READ) &&
1035 (break_to == SMB2_LEASE_NONE)) {
1037 * This is an async break without a reply and thus no timeout
1039 * leases are handled above.
1041 if (fsp->oplock_type != LEASE_OPLOCK) {
1042 remove_oplock(fsp);
1044 return;
1046 if (fsp->oplock_type == LEASE_OPLOCK) {
1047 return;
1050 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1051 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1053 add_oplock_timeout_handler(fsp);
1056 /*******************************************************************
1057 This handles the kernel oplock break message.
1058 *******************************************************************/
1060 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1061 void *private_data,
1062 uint32_t msg_type,
1063 struct server_id src,
1064 DATA_BLOB *data)
1066 struct file_id id;
1067 struct file_id_buf idbuf;
1068 unsigned long file_id;
1069 files_struct *fsp;
1070 struct smbd_server_connection *sconn =
1071 talloc_get_type_abort(private_data,
1072 struct smbd_server_connection);
1073 struct server_id_buf tmp;
1075 if (data->data == NULL) {
1076 DEBUG(0, ("Got NULL buffer\n"));
1077 return;
1080 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1081 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1082 return;
1085 /* Pull the data from the message. */
1086 pull_file_id_24((char *)data->data, &id);
1087 file_id = (unsigned long)IVAL(data->data, 24);
1089 DBG_DEBUG("Got kernel oplock break message from pid %s: %s/%u\n",
1090 server_id_str_buf(src, &tmp),
1091 file_id_str_buf(id, &idbuf),
1092 (unsigned int)file_id);
1094 fsp = initial_break_processing(sconn, id, file_id);
1096 if (fsp == NULL) {
1097 DEBUG(3, ("Got a kernel oplock break message for a file "
1098 "I don't know about\n"));
1099 return;
1102 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1103 /* This is ok, kernel oplocks come in completely async */
1104 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1105 "break reply\n"));
1106 return;
1109 #if defined(WITH_SMB1SERVER)
1110 if (conn_using_smb2(sconn)) {
1111 #endif
1112 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1113 #if defined(WITH_SMB1SERVER)
1114 } else {
1115 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1117 #endif
1119 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1121 add_oplock_timeout_handler(fsp);
1124 static void send_break_to_none(struct messaging_context *msg_ctx,
1125 const struct file_id *id,
1126 const struct share_mode_entry *e)
1128 NTSTATUS status;
1129 status = send_break_message(msg_ctx, id, e, OPLOCK_NONE);
1130 if (!NT_STATUS_IS_OK(status)) {
1131 DBG_DEBUG("send_break_message failed: %s\n",
1132 nt_errstr(status));
1135 struct break_to_none_state {
1136 struct smbd_server_connection *sconn;
1137 struct file_id id;
1138 struct smb2_lease_key lease_key;
1139 struct GUID client_guid;
1140 size_t num_read_leases;
1141 uint32_t total_lease_types;
1144 static bool do_break_lease_to_none(struct share_mode_entry *e,
1145 void *private_data)
1147 struct break_to_none_state *state = private_data;
1148 uint32_t current_state = 0;
1149 bool our_own;
1150 NTSTATUS status;
1152 DBG_DEBUG("lease_key=%"PRIu64"/%"PRIu64"\n",
1153 e->lease_key.data[0],
1154 e->lease_key.data[1]);
1156 status = leases_db_get(&e->client_guid,
1157 &e->lease_key,
1158 &state->id,
1159 &current_state,
1160 NULL, /* breaking */
1161 NULL, /* breaking_to_requested */
1162 NULL, /* breaking_to_required */
1163 NULL, /* lease_version */
1164 NULL); /* epoch */
1165 if (!NT_STATUS_IS_OK(status)) {
1166 DBG_WARNING("leases_db_get failed: %s\n",
1167 nt_errstr(status));
1168 return false;
1171 state->total_lease_types |= current_state;
1173 if ((current_state & SMB2_LEASE_READ) == 0) {
1174 return false;
1177 state->num_read_leases += 1;
1179 our_own = smb2_lease_equal(&state->client_guid,
1180 &state->lease_key,
1181 &e->client_guid,
1182 &e->lease_key);
1183 if (our_own) {
1184 DEBUG(10, ("Don't break our own lease\n"));
1185 return false;
1188 DBG_DEBUG("Breaking %"PRIu64"/%"PRIu64" to none\n",
1189 e->lease_key.data[0],
1190 e->lease_key.data[1]);
1192 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1194 return false;
1197 static bool do_break_oplock_to_none(struct share_mode_entry *e,
1198 bool *modified,
1199 void *private_data)
1201 struct break_to_none_state *state = private_data;
1203 if (e->op_type == LEASE_OPLOCK) {
1205 * Already being taken care of
1207 return false;
1211 * As there could have been multiple writes waiting at the
1212 * lock_share_entry gate we may not be the first to
1213 * enter. Hence the state of the op_types in the share mode
1214 * entries may be partly NO_OPLOCK and partly LEVEL_II
1215 * oplock. It will do no harm to re-send break messages to
1216 * those smbd's that are still waiting their turn to remove
1217 * their LEVEL_II state, and also no harm to ignore existing
1218 * NO_OPLOCK states. JRA.
1221 DBG_DEBUG("e->op_type == %d\n", e->op_type);
1223 state->total_lease_types |= map_oplock_to_lease_type(e->op_type);
1225 if (e->op_type == NO_OPLOCK) {
1226 return false;
1229 state->num_read_leases += 1;
1231 /* Paranoia .... */
1232 SMB_ASSERT(!EXCLUSIVE_OPLOCK_TYPE(e->op_type));
1234 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1236 return false;
1239 /****************************************************************************
1240 This function is called on any file modification or lock request. If a file
1241 is level 2 oplocked then it must tell all other level 2 holders to break to
1242 none.
1243 ****************************************************************************/
1245 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1246 enum level2_contention_type type)
1248 struct break_to_none_state state = {
1249 .sconn = fsp->conn->sconn, .id = fsp->file_id,
1251 struct share_mode_lock *lck = NULL;
1252 uint32_t fsp_lease = fsp_lease_type(fsp);
1253 bool ok, has_read_lease;
1256 * If this file is level II oplocked then we need
1257 * to grab the shared memory lock and inform all
1258 * other files with a level II lock that they need
1259 * to flush their read caches. We keep the lock over
1260 * the shared memory area whilst doing this.
1263 if (fsp_lease & SMB2_LEASE_WRITE) {
1265 * There can't be any level2 oplocks, we're alone.
1267 return;
1270 has_read_lease = file_has_read_lease(fsp);
1271 if (!has_read_lease) {
1272 DEBUG(10, ("No read oplocks around\n"));
1273 return;
1276 if (fsp->oplock_type == LEASE_OPLOCK) {
1277 state.client_guid = *fsp_client_guid(fsp);
1278 state.lease_key = fsp->lease->lease.lease_key;
1279 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1280 state.lease_key.data[0],
1281 state.lease_key.data[1]));
1284 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
1285 if (lck == NULL) {
1286 struct file_id_buf idbuf;
1287 DBG_WARNING("failed to lock share mode entry for file %s.\n",
1288 file_id_str_buf(state.id, &idbuf));
1289 return;
1293 * Walk leases and oplocks separately: We have to send one break per
1294 * lease. If we have multiple share_mode_entry having a common lease,
1295 * we would break the lease twice if we don't walk the leases list
1296 * separately.
1299 ok = share_mode_forall_leases(lck, do_break_lease_to_none, &state);
1300 if (!ok) {
1301 DBG_WARNING("share_mode_forall_leases failed\n");
1304 ok = share_mode_forall_entries(lck, do_break_oplock_to_none, &state);
1305 if (!ok) {
1306 DBG_WARNING("share_mode_forall_entries failed\n");
1311 * Lazy update here. It might be that all leases
1312 * have gone in the meantime.
1314 uint32_t acc, sh, ls;
1315 share_mode_flags_get(lck, &acc, &sh, &ls);
1316 ls = state.total_lease_types;
1317 share_mode_flags_set(lck, acc, sh, ls, NULL);
1320 TALLOC_FREE(lck);
1323 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1324 enum level2_contention_type type)
1326 contend_level2_oplocks_begin_default(fsp, type);
1329 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1330 enum level2_contention_type type)
1332 return;
1335 /****************************************************************************
1336 Linearize a share mode entry struct to an internal oplock break message.
1337 ****************************************************************************/
1339 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1340 const struct share_mode_entry *e)
1342 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1343 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1344 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1345 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1346 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1347 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1348 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1349 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1351 * "id" used to be part of share_mode_entry, thus the strange
1352 * place to put this. Feel free to move somewhere else :-)
1354 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1355 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1356 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1357 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1358 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1359 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1362 /****************************************************************************
1363 De-linearize an internal oplock break message to a share mode entry struct.
1364 ****************************************************************************/
1366 void message_to_share_mode_entry(struct file_id *id,
1367 struct share_mode_entry *e,
1368 const char *msg)
1370 e->pid = (struct server_id){
1371 .pid = (pid_t)IVAL(msg, OP_BREAK_MSG_PID_OFFSET),
1372 .vnn = IVAL(msg, OP_BREAK_MSG_VNN_OFFSET),
1374 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1375 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1376 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1377 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1378 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1379 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1380 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1382 * "id" used to be part of share_mode_entry, thus the strange
1383 * place to put this. Feel free to move somewhere else :-)
1385 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1386 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1387 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1388 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1389 e->name_hash = IVAL(msg, OP_BREAK_MSG_NAME_HASH_OFFSET);
1392 /****************************************************************************
1393 Setup oplocks for this process.
1394 ****************************************************************************/
1396 bool init_oplocks(struct smbd_server_connection *sconn)
1398 DEBUG(3,("init_oplocks: initializing messages.\n"));
1400 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1401 process_oplock_break_message);
1402 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1403 process_kernel_oplock_break);
1404 return true;
1407 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1409 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1411 /* only initialize once */
1412 if (koplocks == NULL) {
1413 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1414 koplocks = linux_init_kernel_oplocks(sconn);
1415 #endif
1416 sconn->oplocks.kernel_ops = koplocks;