2 Unix SMB/CIFS implementation.
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
24 #include "smbd/smbd.h"
25 #include "smbd/globals.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),
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) and no byte-range locks in the file. Returns True
52 ****************************************************************************/
54 bool set_file_oplock(files_struct *fsp, int oplock_type)
56 struct smbd_server_connection *sconn = fsp->conn->sconn;
57 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
58 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
60 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
62 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
64 "don't support them\n"));
69 if ((fsp->oplock_type != NO_OPLOCK) &&
70 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
72 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
76 fsp->oplock_type = oplock_type;
77 fsp->sent_oplock_break = NO_BREAK_SENT;
78 if (oplock_type == LEVEL_II_OPLOCK) {
79 sconn->oplocks.level_II_open++;
80 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
81 sconn->oplocks.exclusive_open++;
84 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
85 "tv_sec = %x, tv_usec = %x\n",
86 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
87 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
88 (int)fsp->open_time.tv_usec ));
93 /****************************************************************************
94 Attempt to release an oplock on a file. Decrements oplock count.
95 ****************************************************************************/
97 void release_file_oplock(files_struct *fsp)
99 struct smbd_server_connection *sconn = fsp->conn->sconn;
100 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
102 if ((fsp->oplock_type != NO_OPLOCK) &&
103 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
105 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
108 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
109 sconn->oplocks.level_II_open--;
110 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
111 sconn->oplocks.exclusive_open--;
114 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
115 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
117 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
118 /* This doesn't matter for close. */
119 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
121 fsp->oplock_type = NO_OPLOCK;
123 fsp->sent_oplock_break = NO_BREAK_SENT;
125 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
126 delete_write_cache(fsp);
128 TALLOC_FREE(fsp->oplock_timeout);
131 /****************************************************************************
132 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
133 ****************************************************************************/
135 static void downgrade_file_oplock(files_struct *fsp)
137 struct smbd_server_connection *sconn = fsp->conn->sconn;
138 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
140 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
141 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
146 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
148 fsp->oplock_type = LEVEL_II_OPLOCK;
149 sconn->oplocks.exclusive_open--;
150 sconn->oplocks.level_II_open++;
151 fsp->sent_oplock_break = NO_BREAK_SENT;
154 /****************************************************************************
155 Remove a file oplock. Copes with level II and exclusive.
156 Locks then unlocks the share mode lock. Client can decide to go directly
157 to none even if a "break-to-level II" was sent.
158 ****************************************************************************/
160 bool remove_oplock(files_struct *fsp)
163 struct share_mode_lock *lck;
165 /* Remove the oplock flag from the sharemode. */
166 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
168 DEBUG(0,("remove_oplock: failed to lock share entry for "
169 "file %s\n", fsp_str_dbg(fsp)));
172 ret = remove_share_oplock(lck, fsp);
174 DEBUG(0,("remove_oplock: failed to remove share oplock for "
175 "file %s fnum %d, %s\n",
176 fsp_str_dbg(fsp), fsp->fnum,
177 file_id_string_tos(&fsp->file_id)));
179 release_file_oplock(fsp);
185 * Deal with a reply when a break-to-level II was sent.
187 bool downgrade_oplock(files_struct *fsp)
190 struct share_mode_lock *lck;
192 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
194 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
195 "file %s\n", fsp_str_dbg(fsp)));
198 ret = downgrade_share_oplock(lck, fsp);
200 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
201 "for file %s fnum %d, file_id %s\n",
202 fsp_str_dbg(fsp), fsp->fnum,
203 file_id_string_tos(&fsp->file_id)));
206 downgrade_file_oplock(fsp);
212 * Some kernel oplock implementations handle the notification themselves.
214 bool should_notify_deferred_opens(struct smbd_server_connection *sconn)
216 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
218 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
221 /****************************************************************************
222 Set up an oplock break message.
223 ****************************************************************************/
225 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
226 files_struct *fsp, int cmd)
228 char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
230 if (result == NULL) {
231 DEBUG(0, ("talloc failed\n"));
235 memset(result,'\0',smb_size);
236 srv_set_message(result,8,0,true);
237 SCVAL(result,smb_com,SMBlockingX);
238 SSVAL(result,smb_tid,fsp->conn->cnum);
239 SSVAL(result,smb_pid,0xFFFF);
240 SSVAL(result,smb_uid,0);
241 SSVAL(result,smb_mid,0xFFFF);
242 SCVAL(result,smb_vwv0,0xFF);
243 SSVAL(result,smb_vwv2,fsp->fnum);
244 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
245 SCVAL(result,smb_vwv3+1,cmd);
249 /****************************************************************************
250 Function to do the waiting before sending a local break.
251 ****************************************************************************/
253 static void wait_before_sending_break(void)
255 long wait_time = (long)lp_oplock_break_wait_time();
258 smb_msleep(wait_time);
262 /****************************************************************************
263 Ensure that we have a valid oplock.
264 ****************************************************************************/
266 static files_struct *initial_break_processing(
267 struct smbd_server_connection *sconn, struct file_id id,
268 unsigned long file_id)
270 files_struct *fsp = NULL;
272 if( DEBUGLVL( 3 ) ) {
273 dbgtext( "initial_break_processing: called for %s/%u\n",
274 file_id_string_tos(&id), (int)file_id);
275 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
276 sconn->oplocks.exclusive_open,
277 sconn->oplocks.level_II_open);
281 * We need to search the file open table for the
282 * entry containing this dev and inode, and ensure
283 * we have an oplock on it.
286 fsp = file_find_dif(sconn, id, file_id);
289 /* The file could have been closed in the meantime - return success. */
290 if( DEBUGLVL( 3 ) ) {
291 dbgtext( "initial_break_processing: cannot find open file with " );
292 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
293 dbgtext( "allowing break to succeed.\n" );
298 /* Ensure we have an oplock on the file */
301 * There is a potential race condition in that an oplock could
302 * have been broken due to another udp request, and yet there are
303 * still oplock break messages being sent in the udp message
304 * queue for this file. So return true if we don't have an oplock,
305 * as we may have just freed it.
308 if(fsp->oplock_type == NO_OPLOCK) {
309 if( DEBUGLVL( 3 ) ) {
310 dbgtext( "initial_break_processing: file %s ",
312 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
313 file_id_string_tos(&id), fsp->fh->gen_id );
314 dbgtext( "Allowing break to succeed regardless.\n" );
322 static void oplock_timeout_handler(struct event_context *ctx,
323 struct timed_event *te,
327 files_struct *fsp = (files_struct *)private_data;
329 /* Remove the timed event handler. */
330 TALLOC_FREE(fsp->oplock_timeout);
331 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
334 reply_to_oplock_break_requests(fsp);
337 /*******************************************************************
338 Add a timeout handler waiting for the client reply.
339 *******************************************************************/
341 static void add_oplock_timeout_handler(files_struct *fsp)
343 struct smbd_server_connection *sconn = fsp->conn->sconn;
344 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
347 * If kernel oplocks already notifies smbds when an oplock break times
351 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
355 if (fsp->oplock_timeout != NULL) {
356 DEBUG(0, ("Logic problem -- have an oplock event hanging "
360 fsp->oplock_timeout =
361 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
362 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
363 oplock_timeout_handler, fsp);
365 if (fsp->oplock_timeout == NULL) {
366 DEBUG(0, ("Could not add oplock timeout handler\n"));
370 static void send_break_message_smb1(files_struct *fsp, int level)
372 char *break_msg = new_break_message_smb1(talloc_tos(),
375 if (break_msg == NULL) {
376 exit_server("Could not talloc break_msg\n");
380 if (!srv_send_smb(fsp->conn->sconn,
382 IS_CONN_ENCRYPTED(fsp->conn),
384 exit_server_cleanly("send_break_message_smb1: "
385 "srv_send_smb failed.");
388 TALLOC_FREE(break_msg);
391 void break_level2_to_none_async(files_struct *fsp)
393 struct smbd_server_connection *sconn = fsp->conn->sconn;
395 if (fsp->oplock_type == NO_OPLOCK) {
396 /* We already got a "break to none" message and we've handled
397 * it. just ignore. */
398 DEBUG(3, ("process_oplock_async_level2_break_message: already "
399 "broken to none, ignoring.\n"));
403 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
404 /* Don't tell the client, just downgrade. */
405 DEBUG(3, ("process_oplock_async_level2_break_message: "
406 "downgrading fake level 2 oplock.\n"));
411 /* Ensure we're really at level2 state. */
412 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
414 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
415 "to none message for fid %d, file %s\n", fsp->fnum,
418 /* Now send a break to none message to our client. */
419 if (sconn->using_smb2) {
420 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
422 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
425 /* Async level2 request, don't send a reply, just remove the oplock. */
429 /*******************************************************************
430 This handles the case of a write triggering a break to none
431 message on a level2 oplock.
432 When we get this message we may be in any of three states :
433 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
434 the client for LEVEL2.
435 *******************************************************************/
437 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
440 struct server_id src,
443 struct share_mode_entry msg;
445 struct smbd_server_connection *sconn =
446 talloc_get_type_abort(private_data,
447 struct smbd_server_connection);
449 if (data->data == NULL) {
450 DEBUG(0, ("Got NULL buffer\n"));
454 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
455 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
459 /* De-linearize incoming message. */
460 message_to_share_mode_entry(&msg, (char *)data->data);
462 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
463 "%s/%llu\n", server_id_str(talloc_tos(), &src),
464 file_id_string_tos(&msg.id),
465 (unsigned long long)msg.share_file_id));
467 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
470 /* We hit a race here. Break messages are sent, and before we
471 * get to process this message, we have closed the file.
472 * No need to reply as this is an async message. */
473 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
477 break_level2_to_none_async(fsp);
480 /*******************************************************************
481 This handles the generic oplock break message from another smbd.
482 *******************************************************************/
484 static void process_oplock_break_message(struct messaging_context *msg_ctx,
487 struct server_id src,
490 struct share_mode_entry msg;
492 bool break_to_level2 = False;
494 struct smbd_server_connection *sconn =
495 talloc_get_type_abort(private_data,
496 struct smbd_server_connection);
497 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
499 if (data->data == NULL) {
500 DEBUG(0, ("Got NULL buffer\n"));
504 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
505 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
509 /* De-linearize incoming message. */
510 message_to_share_mode_entry(&msg, (char *)data->data);
512 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
513 server_id_str(talloc_tos(), &src),
514 file_id_string_tos(&msg.id),
515 (unsigned long long)msg.share_file_id));
517 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
520 /* We hit a race here. Break messages are sent, and before we
521 * get to process this message, we have closed the file. Reply
522 * with 'ok, oplock broken' */
523 DEBUG(3, ("Did not find fsp\n"));
525 /* We just send the same message back. */
526 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
528 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
532 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
533 /* Remember we have to inform the requesting PID when the
536 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
537 &fsp->pending_break_messages,
538 &fsp->num_pending_break_messages);
542 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
543 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
544 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
545 file_id_string_tos(&fsp->file_id),
547 /* We just send the same message back. */
548 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
550 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
554 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
556 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
557 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
558 !(use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
559 lp_level2_oplocks(SNUM(fsp->conn))) {
560 break_to_level2 = True;
563 /* Need to wait before sending a break
564 message if we sent ourselves this message. */
565 if (procid_is_me(&src)) {
566 wait_before_sending_break();
569 if (sconn->using_smb2) {
570 send_break_message_smb2(fsp, break_to_level2 ?
571 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
573 send_break_message_smb1(fsp, break_to_level2 ?
574 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
577 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
580 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
581 &fsp->pending_break_messages,
582 &fsp->num_pending_break_messages);
584 add_oplock_timeout_handler(fsp);
587 /*******************************************************************
588 This handles the kernel oplock break message.
589 *******************************************************************/
591 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
594 struct server_id src,
598 unsigned long file_id;
600 struct smbd_server_connection *sconn =
601 talloc_get_type_abort(private_data,
602 struct smbd_server_connection);
604 if (data->data == NULL) {
605 DEBUG(0, ("Got NULL buffer\n"));
609 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
610 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
614 /* Pull the data from the message. */
615 pull_file_id_24((char *)data->data, &id);
616 file_id = (unsigned long)IVAL(data->data, 24);
618 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
619 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
620 (unsigned int)file_id));
622 fsp = initial_break_processing(sconn, id, file_id);
625 DEBUG(3, ("Got a kernel oplock break message for a file "
626 "I don't know about\n"));
630 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
631 /* This is ok, kernel oplocks come in completely async */
632 DEBUG(3, ("Got a kernel oplock request while waiting for a "
637 if (sconn->using_smb2) {
638 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
640 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
643 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
645 add_oplock_timeout_handler(fsp);
648 void reply_to_oplock_break_requests(files_struct *fsp)
650 struct smbd_server_connection *sconn = fsp->conn->sconn;
651 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
655 * If kernel oplocks already notifies smbds when oplocks are
656 * broken/removed, just return.
659 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
663 for (i=0; i<fsp->num_pending_break_messages; i++) {
664 struct share_mode_entry *e = &fsp->pending_break_messages[i];
665 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
667 share_mode_entry_to_message(msg, e);
669 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
670 MSG_SMB_BREAK_RESPONSE,
672 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
675 SAFE_FREE(fsp->pending_break_messages);
676 fsp->num_pending_break_messages = 0;
677 TALLOC_FREE(fsp->oplock_timeout);
681 static void process_oplock_break_response(struct messaging_context *msg_ctx,
684 struct server_id src,
687 struct share_mode_entry msg;
688 struct smbd_server_connection *sconn =
689 talloc_get_type_abort(private_data,
690 struct smbd_server_connection);
692 if (data->data == NULL) {
693 DEBUG(0, ("Got NULL buffer\n"));
697 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
698 DEBUG(0, ("Got invalid msg len %u\n",
699 (unsigned int)data->length));
703 /* De-linearize incoming message. */
704 message_to_share_mode_entry(&msg, (char *)data->data);
706 DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
707 server_id_str(talloc_tos(), &src),
708 file_id_string_tos(&msg.id),
709 (unsigned long long)msg.share_file_id,
710 (unsigned long long)msg.op_mid));
712 schedule_deferred_open_message_smb(sconn, msg.op_mid);
715 static void process_open_retry_message(struct messaging_context *msg_ctx,
718 struct server_id src,
721 struct share_mode_entry msg;
722 struct smbd_server_connection *sconn =
723 talloc_get_type_abort(private_data,
724 struct smbd_server_connection);
726 if (data->data == NULL) {
727 DEBUG(0, ("Got NULL buffer\n"));
731 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
732 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
736 /* De-linearize incoming message. */
737 message_to_share_mode_entry(&msg, (char *)data->data);
739 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
740 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
741 (unsigned long long)msg.op_mid));
743 schedule_deferred_open_message_smb(sconn, msg.op_mid);
746 struct break_to_none_state {
747 struct smbd_server_connection *sconn;
750 static void do_break_to_none(struct tevent_req *req);
752 /****************************************************************************
753 This function is called on any file modification or lock request. If a file
754 is level 2 oplocked then it must tell all other level 2 holders to break to
756 ****************************************************************************/
758 static void contend_level2_oplocks_begin_default(files_struct *fsp,
759 enum level2_contention_type type)
761 struct smbd_server_connection *sconn = fsp->conn->sconn;
762 struct tevent_req *req;
763 struct break_to_none_state *state;
766 * If this file is level II oplocked then we need
767 * to grab the shared memory lock and inform all
768 * other files with a level II lock that they need
769 * to flush their read caches. We keep the lock over
770 * the shared memory area whilst doing this.
773 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
777 * When we get here we might have a brlock entry locked. Also
778 * locking the share mode entry would violate the locking
779 * order. Breaking level2 oplocks to none is asynchronous
780 * anyway, so we postpone this into an immediate timed event.
783 state = talloc(sconn, struct break_to_none_state);
785 DEBUG(1, ("talloc failed\n"));
788 state->sconn = sconn;
789 state->id = fsp->file_id;
791 req = tevent_wakeup_send(state, sconn->ev_ctx, timeval_set(0, 0));
793 DEBUG(1, ("tevent_wakeup_send failed\n"));
797 tevent_req_set_callback(req, do_break_to_none, state);
801 static void do_break_to_none(struct tevent_req *req)
803 struct break_to_none_state *state = tevent_req_callback_data(
804 req, struct break_to_none_state);
807 struct share_mode_lock *lck;
809 ret = tevent_wakeup_recv(req);
812 DEBUG(1, ("tevent_wakeup_recv failed\n"));
815 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
817 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
818 "share mode entry for file %s.\n",
819 file_id_string_tos(&state->id)));
823 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
824 lck->data->num_share_modes ));
826 for(i = 0; i < lck->data->num_share_modes; i++) {
827 struct share_mode_entry *share_entry = &lck->data->share_modes[i];
828 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
830 if (!is_valid_share_mode_entry(share_entry)) {
835 * As there could have been multiple writes waiting at the
836 * lock_share_entry gate we may not be the first to
837 * enter. Hence the state of the op_types in the share mode
838 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
839 * oplock. It will do no harm to re-send break messages to
840 * those smbd's that are still waiting their turn to remove
841 * their LEVEL_II state, and also no harm to ignore existing
842 * NO_OPLOCK states. JRA.
845 DEBUG(10,("release_level_2_oplocks_on_change: "
846 "share_entry[%i]->op_type == %d\n",
847 i, share_entry->op_type ));
849 if (share_entry->op_type == NO_OPLOCK) {
854 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
855 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
856 "share mode entry %d is an exlusive "
862 share_mode_entry_to_message(msg, share_entry);
865 * Deal with a race condition when breaking level2
866 * oplocks. Don't send all the messages and release
867 * the lock, this allows someone else to come in and
868 * get a level2 lock before any of the messages are
869 * processed, and thus miss getting a break message.
870 * Ensure at least one entry (the one we're breaking)
871 * is processed immediately under the lock and becomes
872 * set as NO_OPLOCK to stop any waiter getting a level2.
876 if (procid_is_me(&share_entry->pid)) {
877 struct files_struct *cur_fsp =
878 initial_break_processing(state->sconn,
880 share_entry->share_file_id);
881 wait_before_sending_break();
882 if (cur_fsp != NULL) {
883 break_level2_to_none_async(cur_fsp);
885 DEBUG(3, ("release_level_2_oplocks_on_change: "
886 "Did not find fsp, ignoring\n"));
889 messaging_send_buf(state->sconn->msg_ctx,
891 MSG_SMB_ASYNC_LEVEL2_BREAK,
893 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
897 /* We let the message receivers handle removing the oplock state
898 in the share mode lock db. */
906 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
907 enum level2_contention_type type)
909 struct smbd_server_connection *sconn = fsp->conn->sconn;
910 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
912 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
913 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
917 contend_level2_oplocks_begin_default(fsp, type);
920 void smbd_contend_level2_oplocks_end(files_struct *fsp,
921 enum level2_contention_type type)
923 struct smbd_server_connection *sconn = fsp->conn->sconn;
924 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
926 /* Only kernel oplocks implement this so far */
927 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
928 koplocks->ops->contend_level2_oplocks_end(fsp, type);
932 /****************************************************************************
933 Linearize a share mode entry struct to an internal oplock break message.
934 ****************************************************************************/
936 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
938 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
939 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
940 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
941 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
942 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
943 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
944 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
945 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
946 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
947 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
948 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
949 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
950 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
951 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
954 /****************************************************************************
955 De-linearize an internal oplock break message to a share mode entry struct.
956 ****************************************************************************/
958 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
960 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
961 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
962 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
963 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
964 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
965 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
966 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
967 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
968 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
969 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
970 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
971 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
972 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
973 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
976 /****************************************************************************
977 Setup oplocks for this process.
978 ****************************************************************************/
980 bool init_oplocks(struct smbd_server_connection *sconn)
982 DEBUG(3,("init_oplocks: initializing messages.\n"));
984 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
985 process_oplock_break_message);
986 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
987 process_oplock_async_level2_break_message);
988 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_RESPONSE,
989 process_oplock_break_response);
990 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
991 process_kernel_oplock_break);
992 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_OPEN_RETRY,
993 process_open_retry_message);
998 void init_kernel_oplocks(struct smbd_server_connection *sconn)
1000 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
1002 /* only initialize once */
1003 if (koplocks == NULL) {
1004 #if HAVE_KERNEL_OPLOCKS_IRIX
1005 koplocks = irix_init_kernel_oplocks(sconn);
1006 #elif HAVE_KERNEL_OPLOCKS_LINUX
1007 koplocks = linux_init_kernel_oplocks(sconn);
1009 sconn->oplocks.kernel_ops = koplocks;