vfs_fruit: use check on global_fruit_config.nego_aapl for macOS specific behaviour
[Samba.git] / source3 / smbd / oplock.c
blob34bebc61f7a758e077e7ed0bdec39a26fff03f43
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 "../librpc/gen_ndr/open_files.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) {
63 if (use_kernel &&
64 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
65 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
66 "don't support them\n"));
67 return NT_STATUS_NOT_SUPPORTED;
71 if ((fsp->oplock_type != NO_OPLOCK) &&
72 use_kernel &&
73 !koplocks->ops->set_oplock(koplocks, fsp, fsp->oplock_type))
75 return map_nt_error_from_unix(errno);
78 fsp->sent_oplock_break = NO_BREAK_SENT;
79 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
80 sconn->oplocks.level_II_open++;
81 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
82 sconn->oplocks.exclusive_open++;
85 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
86 "tv_sec = %x, tv_usec = %x\n",
87 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
88 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
89 (int)fsp->open_time.tv_usec ));
91 return NT_STATUS_OK;
94 /****************************************************************************
95 Attempt to release an oplock on a file. Decrements oplock count.
96 ****************************************************************************/
98 static void release_file_oplock(files_struct *fsp)
100 struct smbd_server_connection *sconn = fsp->conn->sconn;
101 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
102 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
103 (koplocks != NULL);
105 if ((fsp->oplock_type != NO_OPLOCK) &&
106 use_kernel) {
107 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
110 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
111 sconn->oplocks.level_II_open--;
112 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
113 sconn->oplocks.exclusive_open--;
116 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
117 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
119 fsp->oplock_type = NO_OPLOCK;
120 fsp->sent_oplock_break = NO_BREAK_SENT;
122 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
123 delete_write_cache(fsp);
125 TALLOC_FREE(fsp->oplock_timeout);
128 /****************************************************************************
129 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
130 ****************************************************************************/
132 static void downgrade_file_oplock(files_struct *fsp)
134 struct smbd_server_connection *sconn = fsp->conn->sconn;
135 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
136 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
137 (koplocks != NULL);
139 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
140 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
141 return;
144 if (use_kernel) {
145 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
147 fsp->oplock_type = LEVEL_II_OPLOCK;
148 sconn->oplocks.exclusive_open--;
149 sconn->oplocks.level_II_open++;
150 fsp->sent_oplock_break = NO_BREAK_SENT;
152 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
153 delete_write_cache(fsp);
155 TALLOC_FREE(fsp->oplock_timeout);
158 uint32_t get_lease_type(const struct share_mode_data *d,
159 const struct share_mode_entry *e)
161 if (e->op_type == LEASE_OPLOCK) {
162 return d->leases[e->lease_idx].current_state;
164 return map_oplock_to_lease_type(e->op_type);
167 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
169 struct share_mode_data *d = lck->data;
170 struct byte_range_lock *br_lck;
171 uint32_t num_read_oplocks = 0;
172 uint32_t i;
174 if (fsp_lease_type_is_exclusive(fsp)) {
175 const struct share_mode_entry *e = NULL;
176 uint32_t e_lease_type = 0;
179 * If we're fully exclusive, we don't need a brlock entry
181 remove_stale_share_mode_entries(d);
183 e = find_share_mode_entry(lck, fsp);
184 if (e != NULL) {
185 e_lease_type = get_lease_type(d, e);
188 if (!lease_type_is_exclusive(e_lease_type)) {
189 char *timestr = NULL;
191 timestr = timeval_string(talloc_tos(),
192 &fsp->open_time,
193 true);
195 NDR_PRINT_DEBUG(share_mode_data, d);
196 DBG_ERR("file [%s] file_id [%s] gen_id [%lu] "
197 "open_time[%s] lease_type [0x%x] "
198 "oplock_type [0x%x]\n",
199 fsp_str_dbg(fsp),
200 file_id_string_tos(&fsp->file_id),
201 fsp->fh->gen_id, timestr,
202 e_lease_type, fsp->oplock_type);
204 smb_panic("Found non-exclusive lease");
207 return true;
210 for (i=0; i<d->num_share_modes; i++) {
211 struct share_mode_entry *e = &d->share_modes[i];
212 uint32_t e_lease_type = get_lease_type(d, e);
214 if (e_lease_type & SMB2_LEASE_READ) {
215 num_read_oplocks += 1;
219 br_lck = brl_get_locks_readonly(fsp);
220 if (br_lck == NULL) {
221 return false;
223 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
224 return true;
227 br_lck = brl_get_locks(talloc_tos(), fsp);
228 if (br_lck == NULL) {
229 return false;
231 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
232 TALLOC_FREE(br_lck);
233 return true;
236 /****************************************************************************
237 Remove a file oplock with lock already held. Copes with level II and exclusive.
238 ****************************************************************************/
240 bool remove_oplock_under_lock(files_struct *fsp, struct share_mode_lock *lck)
242 bool ret;
244 ret = remove_share_oplock(lck, fsp);
245 if (!ret) {
246 DBG_ERR("failed to remove share oplock for "
247 "file %s, %s, %s\n",
248 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
249 file_id_string_tos(&fsp->file_id));
251 release_file_oplock(fsp);
253 ret = update_num_read_oplocks(fsp, lck);
254 if (!ret) {
255 DBG_ERR("update_num_read_oplocks failed for "
256 "file %s, %s, %s\n",
257 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
258 file_id_string_tos(&fsp->file_id));
261 return ret;
264 /****************************************************************************
265 Remove a file oplock. Copes with level II and exclusive.
266 Locks then unlocks the share mode lock. Client can decide to go directly
267 to none even if a "break-to-level II" was sent.
268 ****************************************************************************/
270 bool remove_oplock(files_struct *fsp)
272 bool ret;
273 struct share_mode_lock *lck;
275 DBG_DEBUG("remove_oplock called for %s\n", fsp_str_dbg(fsp));
277 /* Remove the oplock flag from the sharemode. */
278 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
279 if (lck == NULL) {
280 DBG_ERR("failed to lock share entry for "
281 "file %s\n", fsp_str_dbg(fsp));
282 return false;
285 ret = remove_oplock_under_lock(fsp, lck);
287 TALLOC_FREE(lck);
288 return ret;
292 * Deal with a reply when a break-to-level II was sent.
294 bool downgrade_oplock(files_struct *fsp)
296 bool ret;
297 struct share_mode_lock *lck;
299 DEBUG(10, ("downgrade_oplock called for %s\n",
300 fsp_str_dbg(fsp)));
302 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
303 if (lck == NULL) {
304 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
305 "file %s\n", fsp_str_dbg(fsp)));
306 return False;
308 ret = downgrade_share_oplock(lck, fsp);
309 if (!ret) {
310 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
311 "for file %s, %s, file_id %s\n",
312 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
313 file_id_string_tos(&fsp->file_id)));
315 downgrade_file_oplock(fsp);
317 ret = update_num_read_oplocks(fsp, lck);
318 if (!ret) {
319 DEBUG(0, ("%s: update_num_read_oplocks failed for "
320 "file %s, %s, %s\n",
321 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
322 file_id_string_tos(&fsp->file_id)));
325 TALLOC_FREE(lck);
326 return ret;
329 static void lease_timeout_handler(struct tevent_context *ctx,
330 struct tevent_timer *te,
331 struct timeval now,
332 void *private_data)
334 struct fsp_lease *lease =
335 talloc_get_type_abort(private_data,
336 struct fsp_lease);
337 struct files_struct *fsp;
338 struct share_mode_lock *lck;
339 uint16_t old_epoch = lease->lease.lease_epoch;
342 * This function runs without any specific impersonation
343 * and must not call any SMB_VFS operations!
346 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
347 &lease->lease.lease_key);
348 if (fsp == NULL) {
349 /* race? */
350 TALLOC_FREE(lease->timeout);
351 return;
354 lck = get_existing_share_mode_lock(
355 talloc_tos(), fsp->file_id);
356 if (lck == NULL) {
357 /* race? */
358 TALLOC_FREE(lease->timeout);
359 return;
362 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
364 if (lease->lease.lease_epoch != old_epoch) {
366 * If the epoch changed we need to wait for
367 * the next timeout to happen.
369 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
370 fsp_str_dbg(fsp)));
371 TALLOC_FREE(lck);
372 return;
375 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
377 * If the epoch changed we need to wait for
378 * the next timeout to happen.
380 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
381 fsp_str_dbg(fsp)));
382 TALLOC_FREE(lck);
383 return;
386 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
387 fsp_str_dbg(fsp)));
388 (void)downgrade_lease(lease->sconn->client->connections,
390 &fsp->file_id,
391 &lease->lease.lease_key,
392 SMB2_LEASE_NONE);
394 TALLOC_FREE(lck);
397 bool fsp_lease_update(struct share_mode_lock *lck,
398 const struct GUID *client_guid,
399 struct fsp_lease *lease)
401 struct share_mode_data *d = lck->data;
402 int idx;
403 struct share_mode_lease *l = NULL;
405 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
406 if (idx != -1) {
407 l = &d->leases[idx];
410 if (l == NULL) {
411 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
412 TALLOC_FREE(lease->timeout);
413 lease->lease.lease_state = SMB2_LEASE_NONE;
414 lease->lease.lease_epoch += 1;
415 lease->lease.lease_flags = 0;
416 return false;
419 DEBUG(10,("%s: refresh lease state\n", __func__));
421 /* Ensure we're in sync with current lease state. */
422 if (lease->lease.lease_epoch != l->epoch) {
423 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
424 TALLOC_FREE(lease->timeout);
426 lease->lease.lease_epoch = l->epoch;
427 lease->lease.lease_state = l->current_state;
429 if (l->breaking) {
430 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
432 if (lease->timeout == NULL) {
433 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
435 DEBUG(10,("%s: setup timeout handler\n", __func__));
438 * lease_timeout_handler() only accesses locking.tdb
439 * so we don't use any impersonation and use
440 * the raw tevent context.
442 lease->timeout = tevent_add_timer(lease->sconn->raw_ev_ctx,
443 lease, t,
444 lease_timeout_handler,
445 lease);
446 if (lease->timeout == NULL) {
447 DEBUG(0, ("%s: Could not add lease timeout handler\n",
448 __func__));
451 } else {
452 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
453 TALLOC_FREE(lease->timeout);
456 return true;
459 struct downgrade_lease_additional_state {
460 struct tevent_immediate *im;
461 struct smbXsrv_connection *xconn;
462 uint32_t break_flags;
463 struct smb2_lease_key lease_key;
464 uint32_t break_from;
465 uint32_t break_to;
466 uint16_t new_epoch;
469 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
470 struct tevent_immediate *im,
471 void *private_data)
473 struct downgrade_lease_additional_state *state =
474 talloc_get_type_abort(private_data,
475 struct downgrade_lease_additional_state);
476 struct smbXsrv_connection *xconn = state->xconn;
477 NTSTATUS status;
479 status = smbd_smb2_send_lease_break(xconn,
480 state->new_epoch,
481 state->break_flags,
482 &state->lease_key,
483 state->break_from,
484 state->break_to);
485 TALLOC_FREE(state);
486 if (!NT_STATUS_IS_OK(status)) {
487 smbd_server_connection_terminate(xconn,
488 nt_errstr(status));
489 return;
493 struct downgrade_lease_fsps_state {
494 struct file_id id;
495 struct share_mode_lock *lck;
496 const struct smb2_lease_key *key;
499 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
500 void *private_data)
502 struct downgrade_lease_fsps_state *state =
503 (struct downgrade_lease_fsps_state *)private_data;
505 if (fsp->oplock_type != LEASE_OPLOCK) {
506 return NULL;
508 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
509 return NULL;
511 if (!file_id_equal(&fsp->file_id, &state->id)) {
512 return NULL;
515 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
517 return NULL;
520 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
521 uint32_t num_file_ids,
522 const struct file_id *ids,
523 const struct smb2_lease_key *key,
524 uint32_t lease_state)
526 struct smbd_server_connection *sconn = xconn->client->sconn;
527 struct share_mode_lock *lck;
528 struct share_mode_lease *l = NULL;
529 const struct file_id id = ids[0];
530 uint32_t i;
531 NTSTATUS status;
533 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
534 file_id_string_tos(&id), (unsigned)lease_state));
536 lck = get_existing_share_mode_lock(talloc_tos(), id);
537 if (lck == NULL) {
538 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
540 status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
542 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
543 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
545 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
546 struct downgrade_lease_additional_state *state;
548 state = talloc_zero(xconn,
549 struct downgrade_lease_additional_state);
550 if (state == NULL) {
551 TALLOC_FREE(lck);
552 return NT_STATUS_NO_MEMORY;
555 state->im = tevent_create_immediate(state);
556 if (state->im == NULL) {
557 TALLOC_FREE(state);
558 TALLOC_FREE(lck);
559 return NT_STATUS_NO_MEMORY;
562 state->xconn = xconn;
563 if (l->current_state & (~SMB2_LEASE_READ)) {
564 state->break_flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
566 state->lease_key = l->lease_key;
567 state->break_from = l->current_state;
568 state->break_to = l->breaking_to_requested;
569 if (l->lease_version > 1) {
570 state->new_epoch = l->epoch;
573 if (state->break_flags == 0) {
575 * This is an async break without
576 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
578 * we need to store NONE state in the
579 * database.
581 l->current_state = 0;
582 l->breaking_to_requested = 0;
583 l->breaking_to_required = 0;
584 l->breaking = false;
586 lck->data->modified = true;
589 tevent_schedule_immediate(state->im,
590 xconn->client->raw_ev_ctx,
591 downgrade_lease_additional_trigger,
592 state);
596 struct downgrade_lease_fsps_state state = {
597 .id = id, .lck = lck, .key = key,
600 files_forall(sconn, downgrade_lease_fsps, &state);
603 TALLOC_FREE(lck);
604 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
605 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
608 * Dynamic share case. Ensure other opens are copies.
609 * This will only be breaking to NONE.
612 for (i = 1; i < num_file_ids; i++) {
613 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
614 if (lck == NULL) {
615 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
619 struct downgrade_lease_fsps_state state = {
620 .id = ids[i], .lck = lck, .key = key,
623 files_forall(sconn, downgrade_lease_fsps, &state);
626 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
627 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
629 TALLOC_FREE(lck);
632 return status;
635 /****************************************************************************
636 Set up an oplock break message.
637 ****************************************************************************/
639 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
641 static void new_break_message_smb1(files_struct *fsp, int cmd,
642 char result[SMB1_BREAK_MESSAGE_LENGTH])
644 memset(result,'\0',smb_size);
645 srv_set_message(result,8,0,true);
646 SCVAL(result,smb_com,SMBlockingX);
647 SSVAL(result,smb_tid,fsp->conn->cnum);
648 SSVAL(result,smb_pid,0xFFFF);
649 SSVAL(result,smb_uid,0);
650 SSVAL(result,smb_mid,0xFFFF);
651 SCVAL(result,smb_vwv0,0xFF);
652 SSVAL(result,smb_vwv2,fsp->fnum);
653 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
654 SCVAL(result,smb_vwv3+1,cmd);
657 /****************************************************************************
658 Function to do the waiting before sending a local break.
659 ****************************************************************************/
661 static void wait_before_sending_break(void)
663 long wait_time = (long)lp_oplock_break_wait_time();
665 if (wait_time) {
666 smb_msleep(wait_time);
670 /****************************************************************************
671 Ensure that we have a valid oplock.
672 ****************************************************************************/
674 static files_struct *initial_break_processing(
675 struct smbd_server_connection *sconn, struct file_id id,
676 unsigned long file_id)
678 files_struct *fsp = NULL;
680 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
681 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
682 file_id_string_tos(&id), (int)file_id,
683 sconn->oplocks.exclusive_open,
684 sconn->oplocks.level_II_open));
687 * We need to search the file open table for the
688 * entry containing this dev and inode, and ensure
689 * we have an oplock on it.
692 fsp = file_find_dif(sconn, id, file_id);
694 if(fsp == NULL) {
695 /* The file could have been closed in the meantime - return success. */
696 DEBUG(3, ("initial_break_processing: cannot find open file "
697 "with file_id %s gen_id = %lu, allowing break to "
698 "succeed.\n", file_id_string_tos(&id), file_id));
699 return NULL;
702 /* Ensure we have an oplock on the file */
705 * There is a potential race condition in that an oplock could
706 * have been broken due to another udp request, and yet there are
707 * still oplock break messages being sent in the udp message
708 * queue for this file. So return true if we don't have an oplock,
709 * as we may have just freed it.
712 if(fsp->oplock_type == NO_OPLOCK) {
713 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
714 "gen_id = %lu) has no oplock. Allowing break to "
715 "succeed regardless.\n", fsp_str_dbg(fsp),
716 file_id_string_tos(&id), fsp->fh->gen_id));
717 return NULL;
720 return fsp;
723 static void oplock_timeout_handler(struct tevent_context *ctx,
724 struct tevent_timer *te,
725 struct timeval now,
726 void *private_data)
728 files_struct *fsp = (files_struct *)private_data;
731 * Note this function doesn't run under any specific impersonation and
732 * is not expected to call any SMB_VFS operation!
735 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
737 /* Remove the timed event handler. */
738 TALLOC_FREE(fsp->oplock_timeout);
739 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
740 fsp_str_dbg(fsp)));
741 remove_oplock(fsp);
744 /*******************************************************************
745 Add a timeout handler waiting for the client reply.
746 *******************************************************************/
748 static void add_oplock_timeout_handler(files_struct *fsp)
750 struct smbd_server_connection *sconn = fsp->conn->sconn;
751 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
752 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
753 (koplocks != NULL);
756 * If kernel oplocks already notifies smbds when an oplock break times
757 * out, just return.
759 if (use_kernel &&
760 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
761 return;
764 if (fsp->oplock_timeout != NULL) {
765 DEBUG(0, ("Logic problem -- have an oplock event hanging "
766 "around\n"));
770 * For now we keep the logic and use the
771 * raw event context. We're called from
772 * the messaging system from a raw event context.
773 * Also oplock_timeout_handler doesn't invoke
774 * SMB_VFS calls.
776 fsp->oplock_timeout =
777 tevent_add_timer(fsp->conn->sconn->raw_ev_ctx, fsp,
778 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
779 oplock_timeout_handler, fsp);
781 if (fsp->oplock_timeout == NULL) {
782 DEBUG(0, ("Could not add oplock timeout handler\n"));
786 static void send_break_message_smb1(files_struct *fsp, int level)
788 struct smbXsrv_connection *xconn = NULL;
789 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
792 * For SMB1 we only have one connection
794 xconn = fsp->conn->sconn->client->connections;
796 new_break_message_smb1(fsp, level, break_msg);
798 show_msg(break_msg);
799 if (!srv_send_smb(xconn,
800 break_msg, false, 0,
801 IS_CONN_ENCRYPTED(fsp->conn),
802 NULL)) {
803 exit_server_cleanly("send_break_message_smb1: "
804 "srv_send_smb failed.");
808 /*******************************************************************
809 This handles the generic oplock break message from another smbd.
810 *******************************************************************/
812 static void process_oplock_break_message(struct messaging_context *msg_ctx,
813 void *private_data,
814 uint32_t msg_type,
815 struct server_id src,
816 DATA_BLOB *data)
818 struct file_id id;
819 struct share_mode_entry msg;
820 files_struct *fsp;
821 bool use_kernel;
822 struct smbd_server_connection *sconn =
823 talloc_get_type_abort(private_data,
824 struct smbd_server_connection);
825 struct server_id self = messaging_server_id(sconn->msg_ctx);
826 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
827 uint16_t break_from;
828 uint16_t break_to;
829 bool break_needed = true;
830 struct server_id_buf tmp;
832 if (data->data == NULL) {
833 DEBUG(0, ("Got NULL buffer\n"));
834 return;
837 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
838 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
839 return;
842 /* De-linearize incoming message. */
843 message_to_share_mode_entry(&id, &msg, (char *)data->data);
844 break_to = msg.op_type;
846 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
847 (unsigned)break_to, server_id_str_buf(src, &tmp),
848 file_id_string_tos(&id),
849 (unsigned long long)msg.share_file_id));
851 fsp = initial_break_processing(sconn, id, msg.share_file_id);
853 if (fsp == NULL) {
854 /* We hit a race here. Break messages are sent, and before we
855 * get to process this message, we have closed the file. */
856 DEBUG(3, ("Did not find fsp\n"));
857 return;
860 break_from = fsp_lease_type(fsp);
862 if (fsp->oplock_type != LEASE_OPLOCK) {
863 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
865 * Nothing to do anymore
867 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
868 fsp->sent_oplock_break));
869 return;
873 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
874 DEBUG(10, ("client_caps without level2 oplocks\n"));
875 break_to &= ~SMB2_LEASE_READ;
878 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
879 (koplocks != NULL);
880 if (use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
881 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
882 break_to &= ~SMB2_LEASE_READ;
885 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
886 DEBUG(10, ("no level2 oplocks by config\n"));
887 break_to &= ~SMB2_LEASE_READ;
890 if (fsp->oplock_type == LEASE_OPLOCK) {
891 struct share_mode_lock *lck;
892 int idx;
894 lck = get_existing_share_mode_lock(
895 talloc_tos(), fsp->file_id);
896 if (lck == NULL) {
898 * We hit a race here. Break messages are sent, and
899 * before we get to process this message, we have closed
900 * the file.
902 DEBUG(3, ("Did not find share_mode\n"));
903 return;
906 idx = find_share_mode_lease(
907 lck->data,
908 fsp_client_guid(fsp),
909 &fsp->lease->lease.lease_key);
910 if (idx != -1) {
911 struct share_mode_lease *l;
912 l = &lck->data->leases[idx];
914 break_from = l->current_state;
915 break_to &= l->current_state;
917 if (l->breaking) {
918 break_to &= l->breaking_to_required;
919 if (l->breaking_to_required != break_to) {
921 * Note we don't increment the epoch
922 * here, which might be a bug in
923 * Windows too...
925 l->breaking_to_required = break_to;
926 lck->data->modified = true;
928 break_needed = false;
929 } else if (l->current_state == break_to) {
930 break_needed = false;
931 } else if (l->current_state == SMB2_LEASE_READ) {
932 l->current_state = SMB2_LEASE_NONE;
933 /* Need to increment the epoch */
934 l->epoch += 1;
935 lck->data->modified = true;
936 } else {
937 l->breaking = true;
938 l->breaking_to_required = break_to;
939 l->breaking_to_requested = break_to;
940 /* Need to increment the epoch */
941 l->epoch += 1;
942 lck->data->modified = true;
945 /* Ensure we're in sync with current lease state. */
946 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
949 TALLOC_FREE(lck);
952 if (!break_needed) {
953 DEBUG(10,("%s: skip break\n", __func__));
954 return;
957 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
958 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
959 file_id_string_tos(&fsp->file_id),
960 fsp_str_dbg(fsp)));
961 return;
964 DEBUG(10, ("break_from=%u, break_to=%u\n",
965 (unsigned)break_from, (unsigned)break_to));
967 if ((break_from == break_to) && !break_needed) {
968 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
969 (unsigned)break_to,
970 file_id_string_tos(&fsp->file_id),
971 fsp_str_dbg(fsp)));
972 return;
975 /* Need to wait before sending a break
976 message if we sent ourselves this message. */
977 if (serverid_equal(&self, &src)) {
978 wait_before_sending_break();
981 if (sconn->using_smb2) {
982 send_break_message_smb2(fsp, break_from, break_to);
983 } else {
984 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
985 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
988 if ((break_from == SMB2_LEASE_READ) &&
989 (break_to == SMB2_LEASE_NONE)) {
991 * This is an async break without a reply and thus no timeout
993 * leases are handled above.
995 if (fsp->oplock_type != LEASE_OPLOCK) {
996 remove_oplock(fsp);
998 return;
1000 if (fsp->oplock_type == LEASE_OPLOCK) {
1001 return;
1004 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
1005 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
1007 add_oplock_timeout_handler(fsp);
1010 /*******************************************************************
1011 This handles the kernel oplock break message.
1012 *******************************************************************/
1014 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
1015 void *private_data,
1016 uint32_t msg_type,
1017 struct server_id src,
1018 DATA_BLOB *data)
1020 struct file_id id;
1021 unsigned long file_id;
1022 files_struct *fsp;
1023 struct smbd_server_connection *sconn =
1024 talloc_get_type_abort(private_data,
1025 struct smbd_server_connection);
1026 struct server_id_buf tmp;
1028 if (data->data == NULL) {
1029 DEBUG(0, ("Got NULL buffer\n"));
1030 return;
1033 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
1034 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
1035 return;
1038 /* Pull the data from the message. */
1039 pull_file_id_24((char *)data->data, &id);
1040 file_id = (unsigned long)IVAL(data->data, 24);
1042 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
1043 server_id_str_buf(src, &tmp), file_id_string_tos(&id),
1044 (unsigned int)file_id));
1046 fsp = initial_break_processing(sconn, id, file_id);
1048 if (fsp == NULL) {
1049 DEBUG(3, ("Got a kernel oplock break message for a file "
1050 "I don't know about\n"));
1051 return;
1054 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1055 /* This is ok, kernel oplocks come in completely async */
1056 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1057 "break reply\n"));
1058 return;
1061 if (sconn->using_smb2) {
1062 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1063 } else {
1064 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1067 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1069 add_oplock_timeout_handler(fsp);
1072 struct break_to_none_state {
1073 struct smbd_server_connection *sconn;
1074 struct file_id id;
1075 struct smb2_lease_key lease_key;
1076 struct GUID client_guid;
1078 static void do_break_to_none(struct tevent_context *ctx,
1079 struct tevent_immediate *im,
1080 void *private_data);
1082 /****************************************************************************
1083 This function is called on any file modification or lock request. If a file
1084 is level 2 oplocked then it must tell all other level 2 holders to break to
1085 none.
1086 ****************************************************************************/
1088 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1089 enum level2_contention_type type)
1091 struct smbd_server_connection *sconn = fsp->conn->sconn;
1092 struct tevent_immediate *im;
1093 struct break_to_none_state *state;
1094 struct byte_range_lock *brl;
1095 uint32_t num_read_oplocks = 0;
1098 * If this file is level II oplocked then we need
1099 * to grab the shared memory lock and inform all
1100 * other files with a level II lock that they need
1101 * to flush their read caches. We keep the lock over
1102 * the shared memory area whilst doing this.
1105 if (fsp_lease_type_is_exclusive(fsp)) {
1107 * There can't be any level2 oplocks, we're alone.
1109 return;
1112 brl = brl_get_locks_readonly(fsp);
1113 if (brl != NULL) {
1114 num_read_oplocks = brl_num_read_oplocks(brl);
1117 DEBUG(10, ("num_read_oplocks = %"PRIu32"\n", num_read_oplocks));
1119 if (num_read_oplocks == 0) {
1120 DEBUG(10, ("No read oplocks around\n"));
1121 return;
1125 * When we get here we might have a brlock entry locked. Also
1126 * locking the share mode entry would violate the locking
1127 * order. Breaking level2 oplocks to none is asynchronous
1128 * anyway, so we postpone this into an immediate event.
1131 state = talloc_zero(sconn, struct break_to_none_state);
1132 if (state == NULL) {
1133 DEBUG(1, ("talloc failed\n"));
1134 return;
1136 state->sconn = sconn;
1137 state->id = fsp->file_id;
1139 if (fsp->oplock_type == LEASE_OPLOCK) {
1140 state->client_guid = *fsp_client_guid(fsp);
1141 state->lease_key = fsp->lease->lease.lease_key;
1142 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1143 state->lease_key.data[0],
1144 state->lease_key.data[1]));
1147 im = tevent_create_immediate(state);
1148 if (im == NULL) {
1149 DEBUG(1, ("tevent_create_immediate failed\n"));
1150 TALLOC_FREE(state);
1151 return;
1155 * do_break_to_none() only operates on the
1156 * locking.tdb and send network packets to
1157 * the client. That doesn't require any
1158 * impersonation, so we just use the
1159 * raw tevent context here.
1161 tevent_schedule_immediate(im, sconn->raw_ev_ctx, do_break_to_none, state);
1164 static void send_break_to_none(struct messaging_context *msg_ctx,
1165 const struct file_id *id,
1166 const struct share_mode_entry *e)
1168 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1170 share_mode_entry_to_message(msg, id, e);
1171 /* Overload entry->op_type */
1172 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1174 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1175 (uint8_t *)msg, sizeof(msg));
1178 static void do_break_to_none(struct tevent_context *ctx,
1179 struct tevent_immediate *im,
1180 void *private_data)
1182 struct break_to_none_state *state = talloc_get_type_abort(
1183 private_data, struct break_to_none_state);
1184 uint32_t i;
1185 struct share_mode_lock *lck;
1186 struct share_mode_data *d;
1189 * Note this function doesn't run under any specific impersonation and
1190 * is not expected to call any SMB_VFS operation!
1193 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1194 if (lck == NULL) {
1195 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1196 __func__, file_id_string_tos(&state->id)));
1197 goto done;
1199 d = lck->data;
1202 * Walk leases and oplocks separately: We have to send one break per
1203 * lease. If we have multiple share_mode_entry having a common lease,
1204 * we would break the lease twice if we don't walk the leases list
1205 * separately.
1208 for (i=0; i<d->num_leases; i++) {
1209 struct share_mode_lease *l = &d->leases[i];
1210 struct share_mode_entry *e = NULL;
1211 uint32_t j;
1213 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1214 continue;
1216 if (smb2_lease_equal(&state->client_guid,
1217 &state->lease_key,
1218 &l->client_guid,
1219 &l->lease_key)) {
1220 DEBUG(10, ("Don't break our own lease\n"));
1221 continue;
1224 for (j=0; j<d->num_share_modes; j++) {
1225 e = &d->share_modes[j];
1227 if (!is_valid_share_mode_entry(e)) {
1228 continue;
1230 if (e->lease_idx == i) {
1231 break;
1234 if (j == d->num_share_modes) {
1235 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1236 i));
1237 continue;
1240 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1241 "%"PRIu32"\n", i, j));
1243 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1246 for(i = 0; i < d->num_share_modes; i++) {
1247 struct share_mode_entry *e = &d->share_modes[i];
1249 if (!is_valid_share_mode_entry(e)) {
1250 continue;
1252 if (e->op_type == LEASE_OPLOCK) {
1254 * Took care of those in the loop above
1256 continue;
1260 * As there could have been multiple writes waiting at the
1261 * lock_share_entry gate we may not be the first to
1262 * enter. Hence the state of the op_types in the share mode
1263 * entries may be partly NO_OPLOCK and partly LEVEL_II
1264 * oplock. It will do no harm to re-send break messages to
1265 * those smbd's that are still waiting their turn to remove
1266 * their LEVEL_II state, and also no harm to ignore existing
1267 * NO_OPLOCK states. JRA.
1270 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1271 i, e->op_type ));
1273 if (e->op_type == NO_OPLOCK) {
1274 continue;
1277 /* Paranoia .... */
1278 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1279 DEBUG(0,("%s: PANIC. "
1280 "share mode entry %d is an exclusive "
1281 "oplock !\n", __func__, i ));
1282 TALLOC_FREE(lck);
1283 abort();
1286 send_break_to_none(state->sconn->msg_ctx, &state->id, e);
1289 /* We let the message receivers handle removing the oplock state
1290 in the share mode lock db. */
1292 TALLOC_FREE(lck);
1293 done:
1294 TALLOC_FREE(state);
1295 return;
1298 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1299 enum level2_contention_type type)
1301 struct smbd_server_connection *sconn = fsp->conn->sconn;
1302 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1303 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
1304 (koplocks != NULL);
1306 if (use_kernel && koplocks->ops->contend_level2_oplocks_begin) {
1307 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
1308 return;
1311 contend_level2_oplocks_begin_default(fsp, type);
1314 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1315 enum level2_contention_type type)
1317 struct smbd_server_connection *sconn = fsp->conn->sconn;
1318 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1319 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) &&
1320 (koplocks != NULL);
1322 /* Only kernel oplocks implement this so far */
1323 if (use_kernel && koplocks->ops->contend_level2_oplocks_end) {
1324 koplocks->ops->contend_level2_oplocks_end(fsp, type);
1328 /****************************************************************************
1329 Linearize a share mode entry struct to an internal oplock break message.
1330 ****************************************************************************/
1332 void share_mode_entry_to_message(char *msg, const struct file_id *id,
1333 const struct share_mode_entry *e)
1335 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32_t)e->pid.pid);
1336 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1337 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1338 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1339 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1340 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1341 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1342 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1344 * "id" used to be part of share_mode_entry, thus the strange
1345 * place to put this. Feel free to move somewhere else :-)
1347 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1348 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1349 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1350 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1351 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1352 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1355 /****************************************************************************
1356 De-linearize an internal oplock break message to a share mode entry struct.
1357 ****************************************************************************/
1359 void message_to_share_mode_entry(struct file_id *id,
1360 struct share_mode_entry *e,
1361 const char *msg)
1363 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1364 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1365 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1366 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1367 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1368 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1369 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1370 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1372 * "id" used to be part of share_mode_entry, thus the strange
1373 * place to put this. Feel free to move somewhere else :-)
1375 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, id);
1376 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1377 e->uid = (uint32_t)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1378 e->flags = (uint16_t)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1379 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1380 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1383 /****************************************************************************
1384 Setup oplocks for this process.
1385 ****************************************************************************/
1387 bool init_oplocks(struct smbd_server_connection *sconn)
1389 DEBUG(3,("init_oplocks: initializing messages.\n"));
1391 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1392 process_oplock_break_message);
1393 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1394 process_kernel_oplock_break);
1395 return true;
1398 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1400 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1402 /* only initialize once */
1403 if (koplocks == NULL) {
1404 #if HAVE_KERNEL_OPLOCKS_IRIX
1405 koplocks = irix_init_kernel_oplocks(sconn);
1406 #elif HAVE_KERNEL_OPLOCKS_LINUX
1407 koplocks = linux_init_kernel_oplocks(sconn);
1408 #endif
1409 sconn->oplocks.kernel_ops = koplocks;