smbd: Remove struct blocking_lock_record
[Samba.git] / source3 / smbd / oplock.c
blobaad85fbb2d18d626a931cab551ecb6e41d896c52
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 "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"
32 * helper function used by the kernel oplock backends to post the break message
34 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
36 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
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);
42 /* Don't need to be root here as we're only ever
43 sending to ourselves. */
45 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
46 MSG_SMB_KERNEL_BREAK,
47 msg, MSG_SMB_KERNEL_BREAK_SIZE);
50 /****************************************************************************
51 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
52 disabled (just sets flags).
53 ****************************************************************************/
55 NTSTATUS set_file_oplock(files_struct *fsp)
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);
62 if (fsp->oplock_type == LEVEL_II_OPLOCK && use_kernel) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
64 "don't support them\n"));
65 return NT_STATUS_NOT_SUPPORTED;
68 if ((fsp->oplock_type != NO_OPLOCK) &&
69 use_kernel &&
70 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
72 return map_nt_error_from_unix(errno);
75 fsp->sent_oplock_break = NO_BREAK_SENT;
76 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
77 sconn->oplocks.level_II_open++;
78 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
79 sconn->oplocks.exclusive_open++;
82 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
83 "tv_sec = %x, tv_usec = %x\n",
84 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
85 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
86 (int)fsp->open_time.tv_usec ));
88 return NT_STATUS_OK;
91 static void release_fsp_kernel_oplock(files_struct *fsp)
93 struct smbd_server_connection *sconn = fsp->conn->sconn;
94 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
95 bool use_kernel;
97 if (koplocks == NULL) {
98 return;
100 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn));
101 if (!use_kernel) {
102 return;
104 if (fsp->oplock_type == NO_OPLOCK) {
105 return;
107 if (fsp->oplock_type == LEASE_OPLOCK) {
109 * For leases we don't touch kernel oplocks at all
111 return;
114 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
117 /****************************************************************************
118 Attempt to release an oplock on a file. Decrements oplock count.
119 ****************************************************************************/
121 static void release_file_oplock(files_struct *fsp)
123 struct smbd_server_connection *sconn = fsp->conn->sconn;
125 release_fsp_kernel_oplock(fsp);
127 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
128 sconn->oplocks.level_II_open--;
129 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
130 sconn->oplocks.exclusive_open--;
133 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
134 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
136 fsp->oplock_type = NO_OPLOCK;
137 fsp->sent_oplock_break = NO_BREAK_SENT;
139 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
140 delete_write_cache(fsp);
142 TALLOC_FREE(fsp->oplock_timeout);
145 /****************************************************************************
146 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
147 ****************************************************************************/
149 static void downgrade_file_oplock(files_struct *fsp)
151 struct smbd_server_connection *sconn = fsp->conn->sconn;
152 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
153 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
154 (koplocks != NULL);
156 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
157 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
158 return;
161 if (use_kernel) {
162 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
164 fsp->oplock_type = LEVEL_II_OPLOCK;
165 sconn->oplocks.exclusive_open--;
166 sconn->oplocks.level_II_open++;
167 fsp->sent_oplock_break = NO_BREAK_SENT;
169 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
170 delete_write_cache(fsp);
172 TALLOC_FREE(fsp->oplock_timeout);
175 uint32_t get_lease_type(const struct share_mode_data *d,
176 const struct share_mode_entry *e)
178 if (e->op_type == LEASE_OPLOCK) {
179 NTSTATUS status;
180 uint32_t current_state;
182 status = leases_db_get(
183 &e->client_guid,
184 &e->lease_key,
185 &d->id,
186 &current_state,
187 NULL, /* breaking */
188 NULL, /* breaking_to_requested */
189 NULL, /* breaking_to_required */
190 NULL, /* lease_version */
191 NULL); /* epoch */
192 SMB_ASSERT(NT_STATUS_IS_OK(status));
193 return current_state;
195 return map_oplock_to_lease_type(e->op_type);
198 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
200 struct share_mode_data *d = lck->data;
201 struct byte_range_lock *br_lck;
202 uint32_t num_read_oplocks = 0;
203 uint32_t i;
205 if (fsp_lease_type_is_exclusive(fsp)) {
206 const struct share_mode_entry *e = NULL;
207 uint32_t e_lease_type = 0;
210 * If we're fully exclusive, we don't need a brlock entry
212 remove_stale_share_mode_entries(d);
214 e = find_share_mode_entry(lck, fsp);
215 if (e != NULL) {
216 e_lease_type = get_lease_type(d, e);
219 if (!lease_type_is_exclusive(e_lease_type)) {
220 char *timestr = NULL;
222 timestr = timeval_string(talloc_tos(),
223 &fsp->open_time,
224 true);
226 NDR_PRINT_DEBUG(share_mode_data, d);
227 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
228 "open_time[%s] lease_type [0x%x] "
229 "oplock_type [0x%x]\n",
230 fsp_str_dbg(fsp),
231 file_id_string_tos(&fsp->file_id),
232 fsp->fh->gen_id, timestr,
233 e_lease_type, fsp->oplock_type);
235 smb_panic("Found non-exclusive lease");
238 return true;
241 for (i=0; i<d->num_share_modes; i++) {
242 struct share_mode_entry *e = &d->share_modes[i];
243 uint32_t e_lease_type = get_lease_type(d, e);
245 if (e_lease_type & SMB2_LEASE_READ) {
246 num_read_oplocks += 1;
250 br_lck = brl_get_locks_readonly(fsp);
251 if (br_lck == NULL) {
252 return false;
254 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
255 return true;
258 br_lck = brl_get_locks(talloc_tos(), fsp);
259 if (br_lck == NULL) {
260 return false;
262 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
263 TALLOC_FREE(br_lck);
264 return true;
267 /****************************************************************************
268 Remove a file oplock with lock already held. Copes with level II and exclusive.
269 ****************************************************************************/
271 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
273 bool ret;
275 ret = remove_share_oplock(lck, fsp);
276 if (!ret) {
277 DBG_ERR("failed to remove share oplock for "
278 "file %s, %s, %s\n",
279 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
280 file_id_string_tos(&fsp->file_id));
282 release_file_oplock(fsp);
284 ret = update_num_read_oplocks(fsp, lck);
285 if (!ret) {
286 DBG_ERR("update_num_read_oplocks failed for "
287 "file %s, %s, %s\n",
288 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
289 file_id_string_tos(&fsp->file_id));
292 return ret;
295 /****************************************************************************
296 Remove a file oplock. Copes with level II and exclusive.
297 Locks then unlocks the share mode lock. Client can decide to go directly
298 to none even if a "break-to-level II" was sent.
299 ****************************************************************************/
301 bool remove_oplock(files_struct *fsp)
303 bool ret;
304 struct share_mode_lock *lck;
306 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
308 /* Remove the oplock flag from the sharemode. */
309 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
310 if (lck == NULL) {
311 DBG_ERR("failed to lock share entry for "
312 "file %s\n", fsp_str_dbg(fsp));
313 return false;
316 ret = remove_oplock_under_lock(fsp, lck);
318 TALLOC_FREE(lck);
319 return ret;
323 * Deal with a reply when a break-to-level II was sent.
325 bool downgrade_oplock(files_struct *fsp)
327 bool ret;
328 struct share_mode_lock *lck;
330 DEBUG(10, ("downgrade_oplock called for %s\n",
331 fsp_str_dbg(fsp)));
333 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
334 if (lck == NULL) {
335 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
336 "file %s\n", fsp_str_dbg(fsp)));
337 return False;
339 ret = downgrade_share_oplock(lck, fsp);
340 if (!ret) {
341 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
342 "for file %s, %s, file_id %s\n",
343 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
344 file_id_string_tos(&fsp->file_id)));
346 downgrade_file_oplock(fsp);
348 ret = update_num_read_oplocks(fsp, lck);
349 if (!ret) {
350 DEBUG(0, ("%s: update_num_read_oplocks failed for "
351 "file %s, %s, %s\n",
352 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
353 file_id_string_tos(&fsp->file_id)));
356 TALLOC_FREE(lck);
357 return ret;
360 static void lease_timeout_handler(struct tevent_context *ctx,
361 struct tevent_timer *te,
362 struct timeval now,
363 void *private_data)
365 struct fsp_lease *lease =
366 talloc_get_type_abort(private_data,
367 struct fsp_lease);
368 struct files_struct *fsp;
369 struct share_mode_lock *lck;
370 uint16_t old_epoch = lease->lease.lease_epoch;
372 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
373 &lease->lease.lease_key);
374 if (fsp == NULL) {
375 /* race? */
376 TALLOC_FREE(lease->timeout);
377 return;
381 * Paranoia check: There can only be one fsp_lease per lease
382 * key
384 SMB_ASSERT(fsp->lease == lease);
386 lck = get_existing_share_mode_lock(
387 talloc_tos(), fsp->file_id);
388 if (lck == NULL) {
389 /* race? */
390 TALLOC_FREE(lease->timeout);
391 return;
394 fsp_lease_update(fsp);
396 if (lease->lease.lease_epoch != old_epoch) {
398 * If the epoch changed we need to wait for
399 * the next timeout to happen.
401 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
402 fsp_str_dbg(fsp)));
403 TALLOC_FREE(lck);
404 return;
407 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
409 * If the epoch changed we need to wait for
410 * the next timeout to happen.
412 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
413 fsp_str_dbg(fsp)));
414 TALLOC_FREE(lck);
415 return;
418 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
419 fsp_str_dbg(fsp)));
420 (void)downgrade_lease(lease->sconn->client->connections,
422 &fsp->file_id,
423 &lease->lease.lease_key,
424 SMB2_LEASE_NONE);
426 TALLOC_FREE(lck);
429 bool fsp_lease_update(struct files_struct *fsp)
431 const struct GUID *client_guid = fsp_client_guid(fsp);
432 struct fsp_lease *lease = fsp->lease;
433 uint32_t current_state;
434 bool breaking;
435 uint16_t lease_version, epoch;
436 NTSTATUS status;
438 status = leases_db_get(client_guid,
439 &fsp->lease->lease.lease_key,
440 &fsp->file_id,
441 &current_state,
442 &breaking,
443 NULL, /* breaking_to_requested */
444 NULL, /* breaking_to_required */
445 &lease_version,
446 &epoch);
447 if (!NT_STATUS_IS_OK(status)) {
448 DBG_WARNING("Could not find lease entry: %s\n",
449 nt_errstr(status));
450 TALLOC_FREE(lease->timeout);
451 lease->lease.lease_state = SMB2_LEASE_NONE;
452 lease->lease.lease_epoch += 1;
453 lease->lease.lease_flags = 0;
454 return false;
457 DEBUG(10,("%s: refresh lease state\n", __func__));
459 /* Ensure we're in sync with current lease state. */
460 if (lease->lease.lease_epoch != epoch) {
461 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
462 TALLOC_FREE(lease->timeout);
464 lease->lease.lease_epoch = epoch;
465 lease->lease.lease_state = current_state;
467 if (breaking) {
468 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
470 if (lease->timeout == NULL) {
471 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
473 DEBUG(10,("%s: setup timeout handler\n", __func__));
475 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
476 lease, t,
477 lease_timeout_handler,
478 lease);
479 if (lease->timeout == NULL) {
480 DEBUG(0, ("%s: Could not add lease timeout handler\n",
481 __func__));
484 } else {
485 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
486 TALLOC_FREE(lease->timeout);
489 return true;
492 struct downgrade_lease_additional_state {
493 struct tevent_immediate *im;
494 struct smbXsrv_connection *xconn;
495 uint32_t break_flags;
496 struct smb2_lease_key lease_key;
497 uint32_t break_from;
498 uint32_t break_to;
499 uint16_t new_epoch;
502 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
503 struct tevent_immediate *im,
504 void *private_data)
506 struct downgrade_lease_additional_state *state =
507 talloc_get_type_abort(private_data,
508 struct downgrade_lease_additional_state);
509 struct smbXsrv_connection *xconn = state->xconn;
510 NTSTATUS status;
512 status = smbd_smb2_send_lease_break(xconn,
513 state->new_epoch,
514 state->break_flags,
515 &state->lease_key,
516 state->break_from,
517 state->break_to);
518 TALLOC_FREE(state);
519 if (!NT_STATUS_IS_OK(status)) {
520 smbd_server_connection_terminate(xconn,
521 nt_errstr(status));
522 return;
526 struct fsps_lease_update_state {
527 const struct file_id *id;
528 const struct smb2_lease_key *key;
531 static struct files_struct *fsps_lease_update_fn(
532 struct files_struct *fsp, void *private_data)
534 struct fsps_lease_update_state *state =
535 (struct fsps_lease_update_state *)private_data;
537 if (fsp->oplock_type != LEASE_OPLOCK) {
538 return NULL;
540 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
541 return NULL;
543 if (!file_id_equal(&fsp->file_id, state->id)) {
544 return NULL;
547 fsp_lease_update(fsp);
549 return NULL;
552 static void fsps_lease_update(struct smbd_server_connection *sconn,
553 const struct file_id *id,
554 const struct smb2_lease_key *key)
556 struct fsps_lease_update_state state = { .id = id, .key = key };
557 files_forall(sconn, fsps_lease_update_fn, &state);
560 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
561 uint32_t num_file_ids,
562 const struct file_id *ids,
563 const struct smb2_lease_key *key,
564 uint32_t lease_state)
566 struct smbd_server_connection *sconn = xconn->client->sconn;
567 const struct GUID *client_guid = NULL;
568 struct share_mode_lock *lck;
569 const struct file_id id = ids[0];
570 uint32_t current_state, breaking_to_requested, breaking_to_required;
571 bool breaking;
572 uint16_t lease_version, epoch;
573 NTSTATUS status;
574 uint32_t i;
576 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
577 file_id_string_tos(&id), (unsigned)lease_state));
579 lck = get_existing_share_mode_lock(talloc_tos(), id);
580 if (lck == NULL) {
581 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
584 client_guid = &sconn->client->connections->smb2.client.guid;
586 status = leases_db_get(client_guid,
587 key,
588 &id,
589 &current_state,
590 &breaking,
591 &breaking_to_requested,
592 &breaking_to_required,
593 &lease_version,
594 &epoch);
595 if (!NT_STATUS_IS_OK(status)) {
596 DBG_WARNING("leases_db_get returned %s\n",
597 nt_errstr(status));
598 TALLOC_FREE(lck);
599 return status;
602 if (!breaking) {
603 DBG_WARNING("Attempt to break from %"PRIu32" to %"PRIu32" - "
604 "but we're not in breaking state\n",
605 current_state, lease_state);
606 TALLOC_FREE(lck);
607 return NT_STATUS_UNSUCCESSFUL;
611 * Can't upgrade anything: breaking_to_requested (and current_state)
612 * must be a strict bitwise superset of new_lease_state
614 if ((lease_state & breaking_to_requested) != lease_state) {
615 DBG_WARNING("Attempt to upgrade from %"PRIu32" to %"PRIu32" "
616 "- expected %"PRIu32"\n",
617 current_state, lease_state,
618 breaking_to_requested);
619 TALLOC_FREE(lck);
620 return NT_STATUS_REQUEST_NOT_ACCEPTED;
623 if (current_state != lease_state) {
624 current_state = lease_state;
627 status = NT_STATUS_OK;
629 if ((lease_state & ~breaking_to_required) != 0) {
630 struct downgrade_lease_additional_state *state;
632 DBG_INFO("lease state %"PRIu32" not fully broken from "
633 "%"PRIu32" to %"PRIu32"\n",
634 lease_state,
635 current_state,
636 breaking_to_required);
638 breaking_to_requested = breaking_to_required;
640 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
642 * Here we break in steps, as windows does
643 * see the breaking3 and v2_breaking3 tests.
645 breaking_to_requested |= SMB2_LEASE_READ;
648 state = talloc_zero(xconn,
649 struct downgrade_lease_additional_state);
650 if (state == NULL) {
651 TALLOC_FREE(lck);
652 return NT_STATUS_NO_MEMORY;
655 state->im = tevent_create_immediate(state);
656 if (state->im == NULL) {
657 TALLOC_FREE(state);
658 TALLOC_FREE(lck);
659 return NT_STATUS_NO_MEMORY;
662 state->xconn = xconn;
663 state->lease_key = *key;
664 state->break_from = current_state;
665 state->break_to = breaking_to_requested;
666 if (lease_version > 1) {
667 state->new_epoch = epoch;
670 if (current_state & (SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE)) {
671 state->break_flags =
672 SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
673 } else {
675 * This is an async break without
676 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
678 * we need to store NONE state in the
679 * database.
681 current_state = 0;
682 breaking_to_requested = 0;
683 breaking_to_required = 0;
684 breaking = false;
687 NTSTATUS set_status;
689 set_status = leases_db_set(
690 &sconn->client->connections->
691 smb2.client.guid,
692 key,
693 current_state,
694 breaking,
695 breaking_to_requested,
696 breaking_to_required,
697 lease_version,
698 epoch);
700 if (!NT_STATUS_IS_OK(set_status)) {
701 DBG_DEBUG("leases_db_set failed: %s\n",
702 nt_errstr(set_status));
703 return set_status;
708 tevent_schedule_immediate(state->im,
709 xconn->client->raw_ev_ctx,
710 downgrade_lease_additional_trigger,
711 state);
713 status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
714 } else {
715 DBG_DEBUG("breaking from %"PRIu32" to %"PRIu32" - "
716 "expected %"PRIu32"\n",
717 current_state,
718 lease_state,
719 breaking_to_requested);
721 breaking_to_requested = 0;
722 breaking_to_required = 0;
723 breaking = false;
727 NTSTATUS set_status;
729 set_status = leases_db_set(
730 client_guid,
731 key,
732 current_state,
733 breaking,
734 breaking_to_requested,
735 breaking_to_required,
736 lease_version,
737 epoch);
739 if (!NT_STATUS_IS_OK(set_status)) {
740 DBG_DEBUG("leases_db_set failed: %s\n",
741 nt_errstr(set_status));
742 TALLOC_FREE(lck);
743 return set_status;
747 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
748 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
751 * No, we did not modify the share mode array. We did modify
752 * the leases_db. But without this we don't notify a lease
753 * break waiter via dbwrap_watch_record. We need to make
754 * leases_db watched too.
756 lck->data->modified = true;
758 fsps_lease_update(sconn, &id, key);
760 TALLOC_FREE(lck);
761 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
762 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
765 * Dynamic share case. Ensure other opens are copies.
766 * This will only be breaking to NONE.
769 for (i = 1; i < num_file_ids; i++) {
770 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
771 if (lck == NULL) {
772 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
775 fsps_lease_update(sconn, &ids[i], key);
777 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
778 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
780 TALLOC_FREE(lck);
783 return status;
786 /****************************************************************************
787 Set up an oplock break message.
788 ****************************************************************************/
790 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
792 static void new_break_message_smb1(files_struct *fsp, int cmd,
793 char result[SMB1_BREAK_MESSAGE_LENGTH])
795 memset(result,'\0',smb_size);
796 srv_set_message(result,8,0,true);
797 SCVAL(result,smb_com,SMBlockingX);
798 SSVAL(result,smb_tid,fsp->conn->cnum);
799 SSVAL(result,smb_pid,0xFFFF);
800 SSVAL(result,smb_uid,0);
801 SSVAL(result,smb_mid,0xFFFF);
802 SCVAL(result,smb_vwv0,0xFF);
803 SSVAL(result,smb_vwv2,fsp->fnum);
804 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
805 SCVAL(result,smb_vwv3+1,cmd);
808 /****************************************************************************
809 Function to do the waiting before sending a local break.
810 ****************************************************************************/
812 static void wait_before_sending_break(void)
814 long wait_time = (long)lp_oplock_break_wait_time();
816 if (wait_time) {
817 smb_msleep(wait_time);
821 /****************************************************************************
822 Ensure that we have a valid oplock.
823 ****************************************************************************/
825 static files_struct *initial_break_processing(
826 struct smbd_server_connection *sconn, struct file_id id,
827 unsigned long file_id)
829 files_struct *fsp = NULL;
831 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
832 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
833 file_id_string_tos(&id), (int)file_id,
834 sconn->oplocks.exclusive_open,
835 sconn->oplocks.level_II_open));
838 * We need to search the file open table for the
839 * entry containing this dev and inode, and ensure
840 * we have an oplock on it.
843 fsp = file_find_dif(sconn, id, file_id);
845 if(fsp == NULL) {
846 /* The file could have been closed in the meantime - return success. */
847 DEBUG(3, ("initial_break_processing: cannot find open file "
848 "with file_id %s gen_id = %lu, allowing break to "
849 "succeed.\n", file_id_string_tos(&id), file_id));
850 return NULL;
853 /* Ensure we have an oplock on the file */
856 * There is a potential race condition in that an oplock could
857 * have been broken due to another udp request, and yet there are
858 * still oplock break messages being sent in the udp message
859 * queue for this file. So return true if we don't have an oplock,
860 * as we may have just freed it.
863 if(fsp->oplock_type == NO_OPLOCK) {
864 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
865 "gen_id = %lu) has no oplock. Allowing break to "
866 "succeed regardless.\n", fsp_str_dbg(fsp),
867 file_id_string_tos(&id), fsp->fh->gen_id));
868 return NULL;
871 return fsp;
874 static void oplock_timeout_handler(struct tevent_context *ctx,
875 struct tevent_timer *te,
876 struct timeval now,
877 void *private_data)
879 files_struct *fsp = (files_struct *)private_data;
881 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
883 /* Remove the timed event handler. */
884 TALLOC_FREE(fsp->oplock_timeout);
885 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
886 fsp_str_dbg(fsp)));
887 remove_oplock(fsp);
890 /*******************************************************************
891 Add a timeout handler waiting for the client reply.
892 *******************************************************************/
894 static void add_oplock_timeout_handler(files_struct *fsp)
896 if (fsp->oplock_timeout != NULL) {
897 DEBUG(0, ("Logic problem -- have an oplock event hanging "
898 "around\n"));
901 fsp->oplock_timeout =
902 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
903 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
904 oplock_timeout_handler, fsp);
906 if (fsp->oplock_timeout == NULL) {
907 DEBUG(0, ("Could not add oplock timeout handler\n"));
911 static void send_break_message_smb1(files_struct *fsp, int level)
913 struct smbXsrv_connection *xconn = NULL;
914 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
917 * For SMB1 we only have one connection
919 xconn = fsp->conn->sconn->client->connections;
921 new_break_message_smb1(fsp, level, break_msg);
923 show_msg(break_msg);
924 if (!srv_send_smb(xconn,
925 break_msg, false, 0,
926 IS_CONN_ENCRYPTED(fsp->conn),
927 NULL)) {
928 exit_server_cleanly("send_break_message_smb1: "
929 "srv_send_smb failed.");
933 /*******************************************************************
934 This handles the generic oplock break message from another smbd.
935 *******************************************************************/
937 static void process_oplock_break_message(struct messaging_context *msg_ctx,
938 void *private_data,
939 uint32_t msg_type,
940 struct server_id src,
941 DATA_BLOB *data)
943 struct oplock_break_message *msg = NULL;
944 enum ndr_err_code ndr_err;
945 files_struct *fsp;
946 bool use_kernel;
947 struct smbd_server_connection *sconn =
948 talloc_get_type_abort(private_data,
949 struct smbd_server_connection);
950 struct server_id self = messaging_server_id(sconn->msg_ctx);
951 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
952 uint16_t break_from;
953 uint16_t break_to;
954 bool break_needed = true;
956 msg = talloc(talloc_tos(), struct oplock_break_message);
957 if (msg == NULL) {
958 DBG_WARNING("talloc failed\n");
959 return;
962 ndr_err = ndr_pull_struct_blob_all(
963 data,
964 msg,
965 msg,
966 (ndr_pull_flags_fn_t)ndr_pull_oplock_break_message);
967 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
968 DBG_DEBUG("ndr_pull_oplock_break_message failed: %s\n",
969 ndr_errstr(ndr_err));
970 TALLOC_FREE(msg);
971 return;
973 if (DEBUGLEVEL >= 10) {
974 struct server_id_buf buf;
975 DBG_DEBUG("Got break message from %s\n",
976 server_id_str_buf(src, &buf));
977 NDR_PRINT_DEBUG(oplock_break_message, msg);
980 break_to = msg->break_to;
981 fsp = initial_break_processing(sconn, msg->id, msg->share_file_id);
983 TALLOC_FREE(msg);
985 if (fsp == NULL) {
986 /* We hit a race here. Break messages are sent, and before we
987 * get to process this message, we have closed the file. */
988 DEBUG(3, ("Did not find fsp\n"));
989 return;
992 break_from = fsp_lease_type(fsp);
994 if (fsp->oplock_type != LEASE_OPLOCK) {
995 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
997 * Nothing to do anymore
999 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
1000 fsp->sent_oplock_break));
1001 return;
1005 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
1006 DEBUG(10, ("client_caps without level2 oplocks\n"));
1007 break_to &= ~SMB2_LEASE_READ;
1010 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
1011 (koplocks != NULL);
1012 if (use_kernel) {
1013 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
1014 break_to &= ~SMB2_LEASE_READ;
1017 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
1018 DEBUG(10, ("no level2 oplocks by config\n"));
1019 break_to &= ~SMB2_LEASE_READ;
1022 if (fsp->oplock_type == LEASE_OPLOCK) {
1023 const struct GUID *client_guid = fsp_client_guid(fsp);
1024 struct share_mode_lock *lck;
1025 uint32_t current_state;
1026 uint32_t breaking_to_requested, breaking_to_required;
1027 bool breaking;
1028 uint16_t lease_version, epoch;
1029 NTSTATUS status;
1031 lck = get_existing_share_mode_lock(
1032 talloc_tos(), fsp->file_id);
1033 if (lck == NULL) {
1035 * We hit a race here. Break messages are sent, and
1036 * before we get to process this message, we have closed
1037 * the file.
1039 DEBUG(3, ("Did not find share_mode\n"));
1040 return;
1043 status = leases_db_get(client_guid,
1044 &fsp->lease->lease.lease_key,
1045 &fsp->file_id,
1046 &current_state,
1047 &breaking,
1048 &breaking_to_requested,
1049 &breaking_to_required,
1050 &lease_version,
1051 &epoch);
1052 if (!NT_STATUS_IS_OK(status)) {
1053 DBG_WARNING("leases_db_get returned %s\n",
1054 nt_errstr(status));
1055 TALLOC_FREE(lck);
1056 return;
1059 break_from = current_state;
1060 break_to &= current_state;
1062 if (breaking) {
1063 break_to &= breaking_to_required;
1064 if (breaking_to_required != break_to) {
1066 * Note we don't increment the epoch
1067 * here, which might be a bug in
1068 * Windows too...
1070 breaking_to_required = break_to;
1072 break_needed = false;
1073 } else if (current_state == break_to) {
1074 break_needed = false;
1075 } else if (current_state == SMB2_LEASE_READ) {
1076 current_state = SMB2_LEASE_NONE;
1077 /* Need to increment the epoch */
1078 epoch += 1;
1079 } else {
1080 breaking = true;
1081 breaking_to_required = break_to;
1082 breaking_to_requested = break_to;
1083 /* Need to increment the epoch */
1084 epoch += 1;
1088 NTSTATUS set_status;
1090 set_status = leases_db_set(
1091 client_guid,
1092 &fsp->lease->lease.lease_key,
1093 current_state,
1094 breaking,
1095 breaking_to_requested,
1096 breaking_to_required,
1097 lease_version,
1098 epoch);
1100 if (!NT_STATUS_IS_OK(set_status)) {
1101 DBG_DEBUG("leases_db_set failed: %s\n",
1102 nt_errstr(set_status));
1103 return;
1107 /* Ensure we're in sync with current lease state. */
1108 fsp_lease_update(fsp);
1110 TALLOC_FREE(lck);
1113 if (!break_needed) {
1114 DEBUG(10,("%s: skip break\n", __func__));
1115 return;
1118 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
1119 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
1120 file_id_string_tos(&fsp->file_id),
1121 fsp_str_dbg(fsp)));
1122 return;
1125 DEBUG(10, ("break_from=%u, break_to=%u\n",
1126 (unsigned)break_from, (unsigned)break_to));
1128 if ((break_from == break_to) && !break_needed) {
1129 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
1130 (unsigned)break_to,
1131 file_id_string_tos(&fsp->file_id),
1132 fsp_str_dbg(fsp)));
1133 return;
1136 /* Need to wait before sending a break
1137 message if we sent ourselves this message. */
1138 if (serverid_equal(&self, &src)) {
1139 wait_before_sending_break();
1142 if (sconn->using_smb2) {
1143 send_break_message_smb2(fsp, break_from, break_to);
1144 } else {
1145 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
1146 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
1149 if ((break_from == SMB2_LEASE_READ) &&
1150 (break_to == SMB2_LEASE_NONE)) {
1152 * This is an async break without a reply and thus no timeout
1154 * leases are handled above.
1156 if (fsp->oplock_type != LEASE_OPLOCK) {
1157 remove_oplock(fsp);
1159 return;
1161 if (fsp->oplock_type == LEASE_OPLOCK) {
1162 return;
1165 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1166 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1168 add_oplock_timeout_handler(fsp);
1171 /*******************************************************************
1172 This handles the kernel oplock break message.
1173 *******************************************************************/
1175 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1176 void *private_data,
1177 uint32_t msg_type,
1178 struct server_id src,
1179 DATA_BLOB *data)
1181 struct file_id id;
1182 unsigned long file_id;
1183 files_struct *fsp;
1184 struct smbd_server_connection *sconn =
1185 talloc_get_type_abort(private_data,
1186 struct smbd_server_connection);
1187 struct server_id_buf tmp;
1189 if (data->data == NULL) {
1190 DEBUG(0, ("Got NULL buffer\n"));
1191 return;
1194 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1195 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1196 return;
1199 /* Pull the data from the message. */
1200 pull_file_id_24((char *)data->data, &id);
1201 file_id = (unsigned long)IVAL(data->data, 24);
1203 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1204 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1205 (unsigned int)file_id));
1207 fsp = initial_break_processing(sconn, id, file_id);
1209 if (fsp == NULL) {
1210 DEBUG(3, ("Got a kernel oplock break message for a file "
1211 "I don't know about\n"));
1212 return;
1215 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1216 /* This is ok, kernel oplocks come in completely async */
1217 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1218 "break reply\n"));
1219 return;
1222 if (sconn->using_smb2) {
1223 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1224 } else {
1225 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1228 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1230 add_oplock_timeout_handler(fsp);
1233 static bool file_has_read_oplocks(struct files_struct *fsp)
1235 struct byte_range_lock *brl;
1236 uint32_t num_read_oplocks = 0;
1238 brl = brl_get_locks_readonly(fsp);
1239 if (brl == NULL) {
1240 return false;
1243 num_read_oplocks = brl_num_read_oplocks(brl);
1245 DBG_DEBUG("num_read_oplocks = %"PRIu32"\n", num_read_oplocks);
1247 return (num_read_oplocks != 0);
1250 struct break_to_none_state {
1251 struct smbd_server_connection *sconn;
1252 struct file_id id;
1253 struct smb2_lease_key lease_key;
1254 struct GUID client_guid;
1256 static void do_break_to_none(struct tevent_context *ctx,
1257 struct tevent_immediate *im,
1258 void *private_data);
1260 /****************************************************************************
1261 This function is called on any file modification or lock request. If a file
1262 is level 2 oplocked then it must tell all other level 2 holders to break to
1263 none.
1264 ****************************************************************************/
1266 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1267 enum level2_contention_type type)
1269 struct smbd_server_connection *sconn = fsp->conn->sconn;
1270 struct tevent_immediate *im;
1271 struct break_to_none_state *state;
1272 bool has_read_oplocks;
1275 * If this file is level II oplocked then we need
1276 * to grab the shared memory lock and inform all
1277 * other files with a level II lock that they need
1278 * to flush their read caches. We keep the lock over
1279 * the shared memory area whilst doing this.
1282 if (fsp_lease_type_is_exclusive(fsp)) {
1284 * There can't be any level2 oplocks, we're alone.
1286 return;
1289 has_read_oplocks = file_has_read_oplocks(fsp);
1290 if (!has_read_oplocks) {
1291 DEBUG(10, ("No read oplocks around\n"));
1292 return;
1296 * When we get here we might have a brlock entry locked. Also
1297 * locking the share mode entry would violate the locking
1298 * order. Breaking level2 oplocks to none is asynchronous
1299 * anyway, so we postpone this into an immediate event.
1302 state = talloc(sconn, struct break_to_none_state);
1303 if (state == NULL) {
1304 DEBUG(1, ("talloc failed\n"));
1305 return;
1307 *state = (struct break_to_none_state) {
1308 .sconn = sconn, .id = fsp->file_id,
1311 if (fsp->oplock_type == LEASE_OPLOCK) {
1312 state->client_guid = *fsp_client_guid(fsp);
1313 state->lease_key = fsp->lease->lease.lease_key;
1314 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1315 state->lease_key.data[0],
1316 state->lease_key.data[1]));
1319 im = tevent_create_immediate(state);
1320 if (im == NULL) {
1321 DEBUG(1, ("tevent_create_immediate failed\n"));
1322 TALLOC_FREE(state);
1323 return;
1325 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1328 static void send_break_to_none(struct messaging_context *msg_ctx,
1329 const struct file_id *id,
1330 const struct share_mode_entry *e)
1332 NTSTATUS status;
1333 status = send_break_message(msg_ctx, id, e, OPLOCK_NONE);
1334 if (!NT_STATUS_IS_OK(status)) {
1335 DBG_DEBUG("send_break_message failed: %s\n",
1336 nt_errstr(status));
1340 static bool do_break_lease_to_none(struct share_mode_lock *lck,
1341 struct share_mode_entry *e,
1342 void *private_data)
1344 struct break_to_none_state *state = talloc_get_type_abort(
1345 private_data, struct break_to_none_state);
1346 uint32_t current_state = 0;
1347 bool our_own;
1348 NTSTATUS status;
1350 DBG_DEBUG("lease_key=%"PRIu64"/%"PRIu64"\n",
1351 e->lease_key.data[0],
1352 e->lease_key.data[1]);
1354 status = leases_db_get(&e->client_guid,
1355 &e->lease_key,
1356 &state->id,
1357 &current_state,
1358 NULL, /* breaking */
1359 NULL, /* breaking_to_requested */
1360 NULL, /* breaking_to_required */
1361 NULL, /* lease_version */
1362 NULL); /* epoch */
1363 if (!NT_STATUS_IS_OK(status)) {
1364 DBG_WARNING("leases_db_get failed: %s\n",
1365 nt_errstr(status));
1366 return false;
1369 if ((current_state & SMB2_LEASE_READ) == 0) {
1370 return false;
1373 our_own = smb2_lease_equal(&state->client_guid,
1374 &state->lease_key,
1375 &e->client_guid,
1376 &e->lease_key);
1377 if (our_own) {
1378 DEBUG(10, ("Don't break our own lease\n"));
1379 return false;
1382 DBG_DEBUG("Breaking %"PRIu64"/%"PRIu64" to none\n",
1383 e->lease_key.data[0],
1384 e->lease_key.data[1]);
1386 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1388 return false;
1391 static void do_break_to_none(struct tevent_context *ctx,
1392 struct tevent_immediate *im,
1393 void *private_data)
1395 struct break_to_none_state *state = talloc_get_type_abort(
1396 private_data, struct break_to_none_state);
1397 uint32_t i;
1398 struct share_mode_lock *lck;
1399 struct share_mode_data *d;
1400 bool ok;
1402 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1403 if (lck == NULL) {
1404 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1405 __func__, file_id_string_tos(&state->id)));
1406 goto done;
1408 d = lck->data;
1411 * Walk leases and oplocks separately: We have to send one break per
1412 * lease. If we have multiple share_mode_entry having a common lease,
1413 * we would break the lease twice if we don't walk the leases list
1414 * separately.
1417 ok = share_mode_forall_leases(lck, do_break_lease_to_none, state);
1418 if (!ok) {
1419 DBG_WARNING("share_mode_forall_leases failed\n");
1422 for(i = 0; i < d->num_share_modes; i++) {
1423 struct share_mode_entry *e = &d->share_modes[i];
1425 if (!is_valid_share_mode_entry(e)) {
1426 continue;
1428 if (e->op_type == LEASE_OPLOCK) {
1430 * Took care of those in the loop above
1432 continue;
1436 * As there could have been multiple writes waiting at the
1437 * lock_share_entry gate we may not be the first to
1438 * enter. Hence the state of the op_types in the share mode
1439 * entries may be partly NO_OPLOCK and partly LEVEL_II
1440 * oplock. It will do no harm to re-send break messages to
1441 * those smbd's that are still waiting their turn to remove
1442 * their LEVEL_II state, and also no harm to ignore existing
1443 * NO_OPLOCK states. JRA.
1446 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1447 i, e->op_type ));
1449 if (e->op_type == NO_OPLOCK) {
1450 continue;
1453 /* Paranoia .... */
1454 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1455 DEBUG(0,("%s: PANIC. "
1456 "share mode entry %d is an exclusive "
1457 "oplock !\n", __func__, i ));
1458 TALLOC_FREE(lck);
1459 abort();
1462 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1465 /* We let the message receivers handle removing the oplock state
1466 in the share mode lock db. */
1468 TALLOC_FREE(lck);
1469 done:
1470 TALLOC_FREE(state);
1471 return;
1474 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1475 enum level2_contention_type type)
1477 contend_level2_oplocks_begin_default(fsp, type);
1480 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1481 enum level2_contention_type type)
1483 return;
1486 /****************************************************************************
1487 Linearize a share mode entry struct to an internal oplock break message.
1488 ****************************************************************************/
1490 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1491 const struct share_mode_entry *e)
1493 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1494 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1495 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1496 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1497 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1498 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1499 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1500 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1502 * "id" used to be part of share_mode_entry, thus the strange
1503 * place to put this. Feel free to move somewhere else :-)
1505 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1506 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1507 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1508 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1509 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1510 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1513 /****************************************************************************
1514 De-linearize an internal oplock break message to a share mode entry struct.
1515 ****************************************************************************/
1517 void message_to_share_mode_entry(struct file_id *id,
1518 struct share_mode_entry *e,
1519 const char *msg)
1521 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1522 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1523 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1524 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1525 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1526 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1527 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1528 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1530 * "id" used to be part of share_mode_entry, thus the strange
1531 * place to put this. Feel free to move somewhere else :-)
1533 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1534 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1535 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1536 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1537 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1538 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1541 /****************************************************************************
1542 Setup oplocks for this process.
1543 ****************************************************************************/
1545 bool init_oplocks(struct smbd_server_connection *sconn)
1547 DEBUG(3,("init_oplocks: initializing messages.\n"));
1549 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1550 process_oplock_break_message);
1551 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1552 process_kernel_oplock_break);
1553 return true;
1556 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1558 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1560 /* only initialize once */
1561 if (koplocks == NULL) {
1562 #ifdef HAVE_KERNEL_OPLOCKS_LINUX
1563 koplocks = linux_init_kernel_oplocks(sconn);
1564 #endif
1565 sconn->oplocks.kernel_ops = koplocks;