dsdb-tests: Add new test samba4.user_account_control.python
[Samba.git] / source3 / smbd / oplock.c
blob8f318f58485f3371adea5a759d60b76b2819daf7
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 "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "messages.h"
27 #include "../librpc/gen_ndr/open_files.h"
30 * helper function used by the kernel oplock backends to post the break message
32 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
34 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
36 /* Put the kernel break info into the message. */
37 push_file_id_24((char *)msg, &fsp->file_id);
38 SIVAL(msg,24,fsp->fh->gen_id);
40 /* Don't need to be root here as we're only ever
41 sending to ourselves. */
43 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
44 MSG_SMB_KERNEL_BREAK,
45 msg, MSG_SMB_KERNEL_BREAK_SIZE);
48 /****************************************************************************
49 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
50 disabled (just sets flags).
51 ****************************************************************************/
53 NTSTATUS set_file_oplock(files_struct *fsp)
55 struct smbd_server_connection *sconn = fsp->conn->sconn;
56 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
57 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
59 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
60 if (use_kernel &&
61 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
62 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
63 "don't support them\n"));
64 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 /****************************************************************************
92 Attempt to release an oplock on a file. Decrements oplock count.
93 ****************************************************************************/
95 static void release_file_oplock(files_struct *fsp)
97 struct smbd_server_connection *sconn = fsp->conn->sconn;
98 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
100 if ((fsp->oplock_type != NO_OPLOCK) &&
101 koplocks) {
102 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
105 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
106 sconn->oplocks.level_II_open--;
107 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
108 sconn->oplocks.exclusive_open--;
111 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
112 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
114 fsp->oplock_type = NO_OPLOCK;
115 fsp->sent_oplock_break = NO_BREAK_SENT;
117 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
118 delete_write_cache(fsp);
120 TALLOC_FREE(fsp->oplock_timeout);
123 /****************************************************************************
124 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
125 ****************************************************************************/
127 static void downgrade_file_oplock(files_struct *fsp)
129 struct smbd_server_connection *sconn = fsp->conn->sconn;
130 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
132 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
133 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
134 return;
137 if (koplocks) {
138 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
140 fsp->oplock_type = LEVEL_II_OPLOCK;
141 sconn->oplocks.exclusive_open--;
142 sconn->oplocks.level_II_open++;
143 fsp->sent_oplock_break = NO_BREAK_SENT;
145 flush_write_cache(fsp, SAMBA_OPLOCK_RELEASE_FLUSH);
146 delete_write_cache(fsp);
148 TALLOC_FREE(fsp->oplock_timeout);
151 uint32_t map_oplock_to_lease_type(uint16_t op_type)
153 uint32_t ret;
155 switch(op_type) {
156 case BATCH_OPLOCK:
157 case BATCH_OPLOCK|EXCLUSIVE_OPLOCK:
158 ret = SMB2_LEASE_READ|SMB2_LEASE_WRITE|SMB2_LEASE_HANDLE;
159 break;
160 case EXCLUSIVE_OPLOCK:
161 ret = SMB2_LEASE_READ|SMB2_LEASE_WRITE;
162 break;
163 case LEVEL_II_OPLOCK:
164 ret = SMB2_LEASE_READ;
165 break;
166 default:
167 ret = SMB2_LEASE_NONE;
168 break;
170 return ret;
173 uint32_t get_lease_type(struct share_mode_data *d, struct share_mode_entry *e)
175 if (e->op_type == LEASE_OPLOCK) {
176 return d->leases[e->lease_idx].current_state;
178 return map_oplock_to_lease_type(e->op_type);
181 bool update_num_read_oplocks(files_struct *fsp, struct share_mode_lock *lck)
183 struct share_mode_data *d = lck->data;
184 struct byte_range_lock *br_lck;
185 uint32_t num_read_oplocks = 0;
186 uint32_t i;
188 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
190 * If we're the only one, we don't need a brlock entry
192 SMB_ASSERT(d->num_share_modes == 1);
193 SMB_ASSERT(EXCLUSIVE_OPLOCK_TYPE(d->share_modes[0].op_type));
194 return true;
197 for (i=0; i<d->num_share_modes; i++) {
198 struct share_mode_entry *e = &d->share_modes[i];
199 uint32_t e_lease_type = get_lease_type(d, e);
201 if (e_lease_type & SMB2_LEASE_READ) {
202 num_read_oplocks += 1;
206 br_lck = brl_get_locks_readonly(fsp);
207 if (br_lck == NULL) {
208 return false;
210 if (brl_num_read_oplocks(br_lck) == num_read_oplocks) {
211 return true;
214 br_lck = brl_get_locks(talloc_tos(), fsp);
215 if (br_lck == NULL) {
216 return false;
218 brl_set_num_read_oplocks(br_lck, num_read_oplocks);
219 TALLOC_FREE(br_lck);
220 return true;
223 /****************************************************************************
224 Remove a file oplock. Copes with level II and exclusive.
225 Locks then unlocks the share mode lock. Client can decide to go directly
226 to none even if a "break-to-level II" was sent.
227 ****************************************************************************/
229 bool remove_oplock(files_struct *fsp)
231 bool ret;
232 struct share_mode_lock *lck;
234 DEBUG(10, ("remove_oplock called for %s\n",
235 fsp_str_dbg(fsp)));
237 /* Remove the oplock flag from the sharemode. */
238 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
239 if (lck == NULL) {
240 DEBUG(0,("remove_oplock: failed to lock share entry for "
241 "file %s\n", fsp_str_dbg(fsp)));
242 return False;
245 ret = remove_share_oplock(lck, fsp);
246 if (!ret) {
247 DEBUG(0,("remove_oplock: failed to remove share oplock for "
248 "file %s, %s, %s\n",
249 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
250 file_id_string_tos(&fsp->file_id)));
252 release_file_oplock(fsp);
254 ret = update_num_read_oplocks(fsp, lck);
255 if (!ret) {
256 DEBUG(0, ("%s: update_num_read_oplocks failed for "
257 "file %s, %s, %s\n",
258 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
259 file_id_string_tos(&fsp->file_id)));
262 TALLOC_FREE(lck);
263 return ret;
267 * Deal with a reply when a break-to-level II was sent.
269 bool downgrade_oplock(files_struct *fsp)
271 bool ret;
272 struct share_mode_lock *lck;
274 DEBUG(10, ("downgrade_oplock called for %s\n",
275 fsp_str_dbg(fsp)));
277 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
278 if (lck == NULL) {
279 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
280 "file %s\n", fsp_str_dbg(fsp)));
281 return False;
283 ret = downgrade_share_oplock(lck, fsp);
284 if (!ret) {
285 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
286 "for file %s, %s, file_id %s\n",
287 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
288 file_id_string_tos(&fsp->file_id)));
290 downgrade_file_oplock(fsp);
292 ret = update_num_read_oplocks(fsp, lck);
293 if (!ret) {
294 DEBUG(0, ("%s: update_num_read_oplocks failed for "
295 "file %s, %s, %s\n",
296 __func__, fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
297 file_id_string_tos(&fsp->file_id)));
300 TALLOC_FREE(lck);
301 return ret;
304 static void lease_timeout_handler(struct tevent_context *ctx,
305 struct tevent_timer *te,
306 struct timeval now,
307 void *private_data)
309 struct fsp_lease *lease =
310 talloc_get_type_abort(private_data,
311 struct fsp_lease);
312 struct files_struct *fsp;
313 struct share_mode_lock *lck;
314 uint16_t old_epoch = lease->lease.lease_epoch;
316 fsp = file_find_one_fsp_from_lease_key(lease->sconn,
317 &lease->lease.lease_key);
318 if (fsp == NULL) {
319 /* race? */
320 TALLOC_FREE(lease->timeout);
321 return;
324 lck = get_existing_share_mode_lock(
325 talloc_tos(), fsp->file_id);
326 if (lck == NULL) {
327 /* race? */
328 TALLOC_FREE(lease->timeout);
329 return;
332 fsp_lease_update(lck, fsp_client_guid(fsp), lease);
334 if (lease->lease.lease_epoch != old_epoch) {
336 * If the epoch changed we need to wait for
337 * the next timeout to happen.
339 DEBUG(10, ("lease break timeout race (epoch) for file %s - ignoring\n",
340 fsp_str_dbg(fsp)));
341 TALLOC_FREE(lck);
342 return;
345 if (!(lease->lease.lease_flags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)) {
347 * If the epoch changed we need to wait for
348 * the next timeout to happen.
350 DEBUG(10, ("lease break timeout race (flags) for file %s - ignoring\n",
351 fsp_str_dbg(fsp)));
352 TALLOC_FREE(lck);
353 return;
356 DEBUG(1, ("lease break timed out for file %s -- replying anyway\n",
357 fsp_str_dbg(fsp)));
358 (void)downgrade_lease(lease->sconn->client->connections,
360 &fsp->file_id,
361 &lease->lease.lease_key,
362 SMB2_LEASE_NONE);
364 TALLOC_FREE(lck);
367 bool fsp_lease_update(struct share_mode_lock *lck,
368 const struct GUID *client_guid,
369 struct fsp_lease *lease)
371 struct share_mode_data *d = lck->data;
372 int idx;
373 struct share_mode_lease *l = NULL;
375 idx = find_share_mode_lease(d, client_guid, &lease->lease.lease_key);
376 if (idx != -1) {
377 l = &d->leases[idx];
380 if (l == NULL) {
381 DEBUG(1, ("%s: Could not find lease entry\n", __func__));
382 TALLOC_FREE(lease->timeout);
383 lease->lease.lease_state = SMB2_LEASE_NONE;
384 lease->lease.lease_epoch += 1;
385 lease->lease.lease_flags = 0;
386 return false;
389 DEBUG(10,("%s: refresh lease state\n", __func__));
391 /* Ensure we're in sync with current lease state. */
392 if (lease->lease.lease_epoch != l->epoch) {
393 DEBUG(10,("%s: cancel outdated timeout\n", __func__));
394 TALLOC_FREE(lease->timeout);
396 lease->lease.lease_epoch = l->epoch;
397 lease->lease.lease_state = l->current_state;
399 if (l->breaking) {
400 lease->lease.lease_flags |= SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
402 if (lease->timeout == NULL) {
403 struct timeval t = timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0);
405 DEBUG(10,("%s: setup timeout handler\n", __func__));
407 lease->timeout = tevent_add_timer(lease->sconn->ev_ctx,
408 lease, t,
409 lease_timeout_handler,
410 lease);
411 if (lease->timeout == NULL) {
412 DEBUG(0, ("%s: Could not add lease timeout handler\n",
413 __func__));
416 } else {
417 lease->lease.lease_flags &= ~SMB2_LEASE_FLAG_BREAK_IN_PROGRESS;
418 TALLOC_FREE(lease->timeout);
421 return true;
424 struct downgrade_lease_additional_state {
425 struct tevent_immediate *im;
426 struct smbXsrv_connection *xconn;
427 uint32_t break_flags;
428 struct smb2_lease_key lease_key;
429 uint32_t break_from;
430 uint32_t break_to;
431 uint16_t new_epoch;
434 static void downgrade_lease_additional_trigger(struct tevent_context *ev,
435 struct tevent_immediate *im,
436 void *private_data)
438 struct downgrade_lease_additional_state *state =
439 talloc_get_type_abort(private_data,
440 struct downgrade_lease_additional_state);
441 struct smbXsrv_connection *xconn = state->xconn;
442 NTSTATUS status;
444 status = smbd_smb2_send_lease_break(xconn,
445 state->new_epoch,
446 state->break_flags,
447 &state->lease_key,
448 state->break_from,
449 state->break_to);
450 TALLOC_FREE(state);
451 if (!NT_STATUS_IS_OK(status)) {
452 smbd_server_connection_terminate(xconn,
453 nt_errstr(status));
454 return;
458 struct downgrade_lease_fsps_state {
459 struct file_id id;
460 struct share_mode_lock *lck;
461 const struct smb2_lease_key *key;
464 static struct files_struct *downgrade_lease_fsps(struct files_struct *fsp,
465 void *private_data)
467 struct downgrade_lease_fsps_state *state =
468 (struct downgrade_lease_fsps_state *)private_data;
470 if (fsp->oplock_type != LEASE_OPLOCK) {
471 return NULL;
473 if (!smb2_lease_key_equal(&fsp->lease->lease.lease_key, state->key)) {
474 return NULL;
476 if (!file_id_equal(&fsp->file_id, &state->id)) {
477 return NULL;
480 fsp_lease_update(state->lck, fsp_client_guid(fsp), fsp->lease);
482 return NULL;
485 NTSTATUS downgrade_lease(struct smbXsrv_connection *xconn,
486 uint32_t num_file_ids,
487 const struct file_id *ids,
488 const struct smb2_lease_key *key,
489 uint32_t lease_state)
491 struct smbd_server_connection *sconn = xconn->client->sconn;
492 struct share_mode_lock *lck;
493 struct share_mode_lease *l = NULL;
494 const struct file_id id = ids[0];
495 uint32_t i;
496 NTSTATUS status;
498 DEBUG(10, ("%s: Downgrading %s to %x\n", __func__,
499 file_id_string_tos(&id), (unsigned)lease_state));
501 lck = get_existing_share_mode_lock(talloc_tos(), id);
502 if (lck == NULL) {
503 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
505 status = downgrade_share_lease(sconn, lck, key, lease_state, &l);
507 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
508 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
510 if (NT_STATUS_EQUAL(status, NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)) {
511 struct downgrade_lease_additional_state *state;
513 state = talloc_zero(xconn,
514 struct downgrade_lease_additional_state);
515 if (state == NULL) {
516 TALLOC_FREE(lck);
517 return NT_STATUS_NO_MEMORY;
520 state->im = tevent_create_immediate(state);
521 if (state->im == NULL) {
522 TALLOC_FREE(state);
523 TALLOC_FREE(lck);
524 return NT_STATUS_NO_MEMORY;
527 state->xconn = xconn;
528 if (l->current_state & (~SMB2_LEASE_READ)) {
529 state->break_flags = SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED;
531 state->lease_key = l->lease_key;
532 state->break_from = l->current_state;
533 state->break_to = l->breaking_to_requested;
534 if (l->lease_version > 1) {
535 state->new_epoch = l->epoch;
538 if (state->break_flags == 0) {
540 * This is an async break without
541 * SMB2_NOTIFY_BREAK_LEASE_FLAG_ACK_REQUIRED
543 * we need to store NONE state in the
544 * database.
546 l->current_state = 0;
547 l->breaking_to_requested = 0;
548 l->breaking_to_required = 0;
549 l->breaking = false;
551 lck->data->modified = true;
554 tevent_schedule_immediate(state->im, xconn->ev_ctx,
555 downgrade_lease_additional_trigger,
556 state);
560 struct downgrade_lease_fsps_state state = {
561 .id = id, .lck = lck, .key = key,
564 files_forall(sconn, downgrade_lease_fsps, &state);
567 TALLOC_FREE(lck);
568 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
569 file_id_string_tos(&id), (unsigned)lease_state, nt_errstr(status)));
572 * Dynamic share case. Ensure other opens are copies.
573 * This will only be breaking to NONE.
576 for (i = 1; i < num_file_ids; i++) {
577 lck = get_existing_share_mode_lock(talloc_tos(), ids[i]);
578 if (lck == NULL) {
579 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
583 struct downgrade_lease_fsps_state state = {
584 .id = ids[i], .lck = lck, .key = key,
587 files_forall(sconn, downgrade_lease_fsps, &state);
590 DEBUG(10, ("%s: Downgrading %s to %x => %s\n", __func__,
591 file_id_string_tos(&ids[i]), (unsigned)lease_state, nt_errstr(status)));
593 TALLOC_FREE(lck);
596 return status;
599 /****************************************************************************
600 Set up an oplock break message.
601 ****************************************************************************/
603 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
605 static void new_break_message_smb1(files_struct *fsp, int cmd,
606 char result[SMB1_BREAK_MESSAGE_LENGTH])
608 memset(result,'\0',smb_size);
609 srv_set_message(result,8,0,true);
610 SCVAL(result,smb_com,SMBlockingX);
611 SSVAL(result,smb_tid,fsp->conn->cnum);
612 SSVAL(result,smb_pid,0xFFFF);
613 SSVAL(result,smb_uid,0);
614 SSVAL(result,smb_mid,0xFFFF);
615 SCVAL(result,smb_vwv0,0xFF);
616 SSVAL(result,smb_vwv2,fsp->fnum);
617 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
618 SCVAL(result,smb_vwv3+1,cmd);
621 /****************************************************************************
622 Function to do the waiting before sending a local break.
623 ****************************************************************************/
625 static void wait_before_sending_break(void)
627 long wait_time = (long)lp_oplock_break_wait_time();
629 if (wait_time) {
630 smb_msleep(wait_time);
634 /****************************************************************************
635 Ensure that we have a valid oplock.
636 ****************************************************************************/
638 static files_struct *initial_break_processing(
639 struct smbd_server_connection *sconn, struct file_id id,
640 unsigned long file_id)
642 files_struct *fsp = NULL;
644 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
645 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
646 file_id_string_tos(&id), (int)file_id,
647 sconn->oplocks.exclusive_open,
648 sconn->oplocks.level_II_open));
651 * We need to search the file open table for the
652 * entry containing this dev and inode, and ensure
653 * we have an oplock on it.
656 fsp = file_find_dif(sconn, id, file_id);
658 if(fsp == NULL) {
659 /* The file could have been closed in the meantime - return success. */
660 DEBUG(3, ("initial_break_processing: cannot find open file "
661 "with file_id %s gen_id = %lu, allowing break to "
662 "succeed.\n", file_id_string_tos(&id), file_id));
663 return NULL;
666 /* Ensure we have an oplock on the file */
669 * There is a potential race condition in that an oplock could
670 * have been broken due to another udp request, and yet there are
671 * still oplock break messages being sent in the udp message
672 * queue for this file. So return true if we don't have an oplock,
673 * as we may have just freed it.
676 if(fsp->oplock_type == NO_OPLOCK) {
677 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
678 "gen_id = %lu) has no oplock. Allowing break to "
679 "succeed regardless.\n", fsp_str_dbg(fsp),
680 file_id_string_tos(&id), fsp->fh->gen_id));
681 return NULL;
684 return fsp;
687 static void oplock_timeout_handler(struct tevent_context *ctx,
688 struct tevent_timer *te,
689 struct timeval now,
690 void *private_data)
692 files_struct *fsp = (files_struct *)private_data;
694 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
696 /* Remove the timed event handler. */
697 TALLOC_FREE(fsp->oplock_timeout);
698 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
699 fsp_str_dbg(fsp)));
700 remove_oplock(fsp);
703 /*******************************************************************
704 Add a timeout handler waiting for the client reply.
705 *******************************************************************/
707 static void add_oplock_timeout_handler(files_struct *fsp)
709 struct smbd_server_connection *sconn = fsp->conn->sconn;
710 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
713 * If kernel oplocks already notifies smbds when an oplock break times
714 * out, just return.
716 if (koplocks &&
717 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
718 return;
721 if (fsp->oplock_timeout != NULL) {
722 DEBUG(0, ("Logic problem -- have an oplock event hanging "
723 "around\n"));
726 fsp->oplock_timeout =
727 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
728 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
729 oplock_timeout_handler, fsp);
731 if (fsp->oplock_timeout == NULL) {
732 DEBUG(0, ("Could not add oplock timeout handler\n"));
736 static void send_break_message_smb1(files_struct *fsp, int level)
738 struct smbXsrv_connection *xconn = NULL;
739 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
742 * For SMB1 we only have one connection
744 xconn = fsp->conn->sconn->client->connections;
746 new_break_message_smb1(fsp, level, break_msg);
748 show_msg(break_msg);
749 if (!srv_send_smb(xconn,
750 break_msg, false, 0,
751 IS_CONN_ENCRYPTED(fsp->conn),
752 NULL)) {
753 exit_server_cleanly("send_break_message_smb1: "
754 "srv_send_smb failed.");
758 /*******************************************************************
759 This handles the generic oplock break message from another smbd.
760 *******************************************************************/
762 static void process_oplock_break_message(struct messaging_context *msg_ctx,
763 void *private_data,
764 uint32_t msg_type,
765 struct server_id src,
766 DATA_BLOB *data)
768 struct share_mode_entry msg;
769 files_struct *fsp;
770 bool use_kernel;
771 struct smbd_server_connection *sconn =
772 talloc_get_type_abort(private_data,
773 struct smbd_server_connection);
774 struct server_id self = messaging_server_id(sconn->msg_ctx);
775 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
776 uint16_t break_from;
777 uint16_t break_to;
778 bool break_needed = true;
780 if (data->data == NULL) {
781 DEBUG(0, ("Got NULL buffer\n"));
782 return;
785 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
786 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
787 return;
790 /* De-linearize incoming message. */
791 message_to_share_mode_entry(&msg, (char *)data->data);
792 break_to = msg.op_type;
794 DEBUG(10, ("Got oplock break to %u message from pid %s: %s/%llu\n",
795 (unsigned)break_to, server_id_str(talloc_tos(), &src),
796 file_id_string_tos(&msg.id),
797 (unsigned long long)msg.share_file_id));
799 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
801 if (fsp == NULL) {
802 /* We hit a race here. Break messages are sent, and before we
803 * get to process this message, we have closed the file. */
804 DEBUG(3, ("Did not find fsp\n"));
805 return;
808 break_from = fsp_lease_type(fsp);
810 if (fsp->oplock_type != LEASE_OPLOCK) {
811 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
813 * Nothing to do anymore
815 DEBUG(10, ("fsp->sent_oplock_break = %d\n",
816 fsp->sent_oplock_break));
817 return;
821 if (!(global_client_caps & CAP_LEVEL_II_OPLOCKS)) {
822 DEBUG(10, ("client_caps without level2 oplocks\n"));
823 break_to &= ~SMB2_LEASE_READ;
826 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
827 if (use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
828 DEBUG(10, ("Kernel oplocks don't allow level2\n"));
829 break_to &= ~SMB2_LEASE_READ;
832 if (!lp_level2_oplocks(SNUM(fsp->conn))) {
833 DEBUG(10, ("no level2 oplocks by config\n"));
834 break_to &= ~SMB2_LEASE_READ;
837 if (fsp->oplock_type == LEASE_OPLOCK) {
838 struct share_mode_lock *lck;
839 int idx;
841 lck = get_existing_share_mode_lock(
842 talloc_tos(), fsp->file_id);
843 if (lck == NULL) {
845 * We hit a race here. Break messages are sent, and
846 * before we get to process this message, we have closed
847 * the file.
849 DEBUG(3, ("Did not find share_mode\n"));
850 return;
853 idx = find_share_mode_lease(
854 lck->data,
855 fsp_client_guid(fsp),
856 &fsp->lease->lease.lease_key);
857 if (idx != -1) {
858 struct share_mode_lease *l;
859 l = &lck->data->leases[idx];
861 break_from = l->current_state;
862 break_to &= l->current_state;
864 if (l->breaking) {
865 break_to &= l->breaking_to_required;
866 if (l->breaking_to_required != break_to) {
868 * Note we don't increment the epoch
869 * here, which might be a bug in
870 * Windows too...
872 l->breaking_to_required = break_to;
873 lck->data->modified = true;
875 break_needed = false;
876 } else if (l->current_state == break_to) {
877 break_needed = false;
878 } else if (l->current_state == SMB2_LEASE_READ) {
879 l->current_state = SMB2_LEASE_NONE;
880 /* Need to increment the epoch */
881 l->epoch += 1;
882 lck->data->modified = true;
883 } else {
884 l->breaking = true;
885 l->breaking_to_required = break_to;
886 l->breaking_to_requested = break_to;
887 /* Need to increment the epoch */
888 l->epoch += 1;
889 lck->data->modified = true;
892 /* Ensure we're in sync with current lease state. */
893 fsp_lease_update(lck, fsp_client_guid(fsp), fsp->lease);
896 TALLOC_FREE(lck);
899 if (!break_needed) {
900 DEBUG(10,("%s: skip break\n", __func__));
901 return;
904 if ((break_from == SMB2_LEASE_NONE) && !break_needed) {
905 DEBUG(3, ("Already downgraded oplock to none on %s: %s\n",
906 file_id_string_tos(&fsp->file_id),
907 fsp_str_dbg(fsp)));
908 return;
911 DEBUG(10, ("break_from=%u, break_to=%u\n",
912 (unsigned)break_from, (unsigned)break_to));
914 if ((break_from == break_to) && !break_needed) {
915 DEBUG(3, ("Already downgraded oplock to %u on %s: %s\n",
916 (unsigned)break_to,
917 file_id_string_tos(&fsp->file_id),
918 fsp_str_dbg(fsp)));
919 return;
922 /* Need to wait before sending a break
923 message if we sent ourselves this message. */
924 if (serverid_equal(&self, &src)) {
925 wait_before_sending_break();
928 if (sconn->using_smb2) {
929 send_break_message_smb2(fsp, break_from, break_to);
930 } else {
931 send_break_message_smb1(fsp, (break_to & SMB2_LEASE_READ) ?
932 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
935 if ((break_from == SMB2_LEASE_READ) &&
936 (break_to == SMB2_LEASE_NONE)) {
938 * This is an async break without a reply and thus no timeout
940 * leases are handled above.
942 if (fsp->oplock_type != LEASE_OPLOCK) {
943 remove_oplock(fsp);
945 return;
947 if (fsp->oplock_type == LEASE_OPLOCK) {
948 return;
951 fsp->sent_oplock_break = (break_to & SMB2_LEASE_READ) ?
952 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
954 add_oplock_timeout_handler(fsp);
957 /*******************************************************************
958 This handles the kernel oplock break message.
959 *******************************************************************/
961 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
962 void *private_data,
963 uint32_t msg_type,
964 struct server_id src,
965 DATA_BLOB *data)
967 struct file_id id;
968 unsigned long file_id;
969 files_struct *fsp;
970 struct smbd_server_connection *sconn =
971 talloc_get_type_abort(private_data,
972 struct smbd_server_connection);
974 if (data->data == NULL) {
975 DEBUG(0, ("Got NULL buffer\n"));
976 return;
979 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
980 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
981 return;
984 /* Pull the data from the message. */
985 pull_file_id_24((char *)data->data, &id);
986 file_id = (unsigned long)IVAL(data->data, 24);
988 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
989 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
990 (unsigned int)file_id));
992 fsp = initial_break_processing(sconn, id, file_id);
994 if (fsp == NULL) {
995 DEBUG(3, ("Got a kernel oplock break message for a file "
996 "I don't know about\n"));
997 return;
1000 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
1001 /* This is ok, kernel oplocks come in completely async */
1002 DEBUG(3, ("Got a kernel oplock request while waiting for a "
1003 "break reply\n"));
1004 return;
1007 if (sconn->using_smb2) {
1008 send_break_message_smb2(fsp, 0, OPLOCKLEVEL_NONE);
1009 } else {
1010 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
1013 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
1015 add_oplock_timeout_handler(fsp);
1018 struct break_to_none_state {
1019 struct smbd_server_connection *sconn;
1020 struct file_id id;
1021 struct smb2_lease_key lease_key;
1022 struct GUID client_guid;
1024 static void do_break_to_none(struct tevent_context *ctx,
1025 struct tevent_immediate *im,
1026 void *private_data);
1028 /****************************************************************************
1029 This function is called on any file modification or lock request. If a file
1030 is level 2 oplocked then it must tell all other level 2 holders to break to
1031 none.
1032 ****************************************************************************/
1034 static void contend_level2_oplocks_begin_default(files_struct *fsp,
1035 enum level2_contention_type type)
1037 struct smbd_server_connection *sconn = fsp->conn->sconn;
1038 struct tevent_immediate *im;
1039 struct break_to_none_state *state;
1040 struct byte_range_lock *brl;
1041 uint32_t num_read_oplocks = 0;
1044 * If this file is level II oplocked then we need
1045 * to grab the shared memory lock and inform all
1046 * other files with a level II lock that they need
1047 * to flush their read caches. We keep the lock over
1048 * the shared memory area whilst doing this.
1051 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1053 * There can't be any level2 oplocks, we're alone.
1055 return;
1058 brl = brl_get_locks_readonly(fsp);
1059 if (brl != NULL) {
1060 num_read_oplocks = brl_num_read_oplocks(brl);
1063 DEBUG(10, ("num_read_oplocks = %"PRIu32"\n", num_read_oplocks));
1065 if (num_read_oplocks == 0) {
1066 DEBUG(10, ("No read oplocks around\n"));
1067 return;
1071 * When we get here we might have a brlock entry locked. Also
1072 * locking the share mode entry would violate the locking
1073 * order. Breaking level2 oplocks to none is asynchronous
1074 * anyway, so we postpone this into an immediate event.
1077 state = talloc_zero(sconn, struct break_to_none_state);
1078 if (state == NULL) {
1079 DEBUG(1, ("talloc failed\n"));
1080 return;
1082 state->sconn = sconn;
1083 state->id = fsp->file_id;
1085 if (fsp->oplock_type == LEASE_OPLOCK) {
1086 state->client_guid = *fsp_client_guid(fsp);
1087 state->lease_key = fsp->lease->lease.lease_key;
1088 DEBUG(10, ("Breaking through lease key %"PRIu64"/%"PRIu64"\n",
1089 state->lease_key.data[0],
1090 state->lease_key.data[1]));
1093 im = tevent_create_immediate(state);
1094 if (im == NULL) {
1095 DEBUG(1, ("tevent_create_immediate failed\n"));
1096 TALLOC_FREE(state);
1097 return;
1099 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
1102 static void send_break_to_none(struct messaging_context *msg_ctx,
1103 const struct share_mode_entry *e)
1105 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
1107 share_mode_entry_to_message(msg, e);
1108 /* Overload entry->op_type */
1109 SSVAL(msg, OP_BREAK_MSG_OP_TYPE_OFFSET, NO_OPLOCK);
1111 messaging_send_buf(msg_ctx, e->pid, MSG_SMB_BREAK_REQUEST,
1112 (uint8 *)msg, sizeof(msg));
1115 static void do_break_to_none(struct tevent_context *ctx,
1116 struct tevent_immediate *im,
1117 void *private_data)
1119 struct break_to_none_state *state = talloc_get_type_abort(
1120 private_data, struct break_to_none_state);
1121 uint32_t i;
1122 struct share_mode_lock *lck;
1123 struct share_mode_data *d;
1125 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
1126 if (lck == NULL) {
1127 DEBUG(1, ("%s: failed to lock share mode entry for file %s.\n",
1128 __func__, file_id_string_tos(&state->id)));
1129 goto done;
1131 d = lck->data;
1134 * Walk leases and oplocks separately: We have to send one break per
1135 * lease. If we have multiple share_mode_entry having a common lease,
1136 * we would break the lease twice if we don't walk the leases list
1137 * separately.
1140 for (i=0; i<d->num_leases; i++) {
1141 struct share_mode_lease *l = &d->leases[i];
1142 struct share_mode_entry *e;
1143 uint32_t j;
1145 if ((l->current_state & SMB2_LEASE_READ) == 0) {
1146 continue;
1148 if (smb2_lease_equal(&state->client_guid,
1149 &state->lease_key,
1150 &l->client_guid,
1151 &l->lease_key)) {
1152 DEBUG(10, ("Don't break our own lease\n"));
1153 continue;
1156 for (j=0; j<d->num_share_modes; j++) {
1157 e = &d->share_modes[j];
1159 if (!is_valid_share_mode_entry(e)) {
1160 continue;
1162 if (e->lease_idx == i) {
1163 break;
1166 if (j == d->num_share_modes) {
1167 DEBUG(0, ("leases[%"PRIu32"] has no share mode\n",
1168 i));
1169 continue;
1172 DEBUG(10, ("Breaking lease# %"PRIu32" with share_entry# "
1173 "%"PRIu32"\n", i, j));
1175 send_break_to_none(state->sconn->msg_ctx, e);
1178 for(i = 0; i < d->num_share_modes; i++) {
1179 struct share_mode_entry *e = &d->share_modes[i];
1181 if (!is_valid_share_mode_entry(e)) {
1182 continue;
1184 if (e->op_type == LEASE_OPLOCK) {
1186 * Took care of those in the loop above
1188 continue;
1192 * As there could have been multiple writes waiting at the
1193 * lock_share_entry gate we may not be the first to
1194 * enter. Hence the state of the op_types in the share mode
1195 * entries may be partly NO_OPLOCK and partly LEVEL_II
1196 * oplock. It will do no harm to re-send break messages to
1197 * those smbd's that are still waiting their turn to remove
1198 * their LEVEL_II state, and also no harm to ignore existing
1199 * NO_OPLOCK states. JRA.
1202 DEBUG(10, ("%s: share_entry[%i]->op_type == %d\n", __func__,
1203 i, e->op_type ));
1205 if (e->op_type == NO_OPLOCK) {
1206 continue;
1209 /* Paranoia .... */
1210 if (EXCLUSIVE_OPLOCK_TYPE(e->op_type)) {
1211 DEBUG(0,("%s: PANIC. "
1212 "share mode entry %d is an exlusive "
1213 "oplock !\n", __func__, i ));
1214 TALLOC_FREE(lck);
1215 abort();
1218 send_break_to_none(state->sconn->msg_ctx, e);
1221 /* We let the message receivers handle removing the oplock state
1222 in the share mode lock db. */
1224 TALLOC_FREE(lck);
1225 done:
1226 TALLOC_FREE(state);
1227 return;
1230 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
1231 enum level2_contention_type type)
1233 struct smbd_server_connection *sconn = fsp->conn->sconn;
1234 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1236 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
1237 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
1238 return;
1241 contend_level2_oplocks_begin_default(fsp, type);
1244 void smbd_contend_level2_oplocks_end(files_struct *fsp,
1245 enum level2_contention_type type)
1247 struct smbd_server_connection *sconn = fsp->conn->sconn;
1248 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1250 /* Only kernel oplocks implement this so far */
1251 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
1252 koplocks->ops->contend_level2_oplocks_end(fsp, type);
1256 /****************************************************************************
1257 Linearize a share mode entry struct to an internal oplock break message.
1258 ****************************************************************************/
1260 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
1262 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
1263 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
1264 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
1265 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
1266 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
1267 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
1268 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
1269 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
1270 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
1271 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
1272 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
1273 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
1274 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
1275 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
1278 /****************************************************************************
1279 De-linearize an internal oplock break message to a share mode entry struct.
1280 ****************************************************************************/
1282 void message_to_share_mode_entry(struct share_mode_entry *e, const char *msg)
1284 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
1285 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
1286 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
1287 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
1288 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
1289 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
1290 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
1291 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
1292 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
1293 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
1294 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
1295 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
1296 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
1297 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
1300 /****************************************************************************
1301 Setup oplocks for this process.
1302 ****************************************************************************/
1304 bool init_oplocks(struct smbd_server_connection *sconn)
1306 DEBUG(3,("init_oplocks: initializing messages.\n"));
1308 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
1309 process_oplock_break_message);
1310 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
1311 process_kernel_oplock_break);
1312 return true;
1315 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1317 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1319 /* only initialize once */
1320 if (koplocks == NULL) {
1321 #if HAVE_KERNEL_OPLOCKS_IRIX
1322 koplocks = irix_init_kernel_oplocks(sconn);
1323 #elif HAVE_KERNEL_OPLOCKS_LINUX
1324 koplocks = linux_init_kernel_oplocks(sconn);
1325 #endif
1326 sconn->oplocks.kernel_ops = koplocks;