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 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
59 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
61 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
62 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
63 "don't support them\n"));
68 if ((fsp->oplock_type != NO_OPLOCK) &&
69 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
71 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
75 fsp->oplock_type = oplock_type;
76 fsp->sent_oplock_break = NO_BREAK_SENT;
77 if (oplock_type == LEVEL_II_OPLOCK) {
78 sconn->oplocks.level_II_open++;
79 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
80 sconn->oplocks.exclusive_open++;
83 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
84 "tv_sec = %x, tv_usec = %x\n",
85 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
86 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
87 (int)fsp->open_time.tv_usec ));
92 /****************************************************************************
93 Attempt to release an oplock on a file. Decrements oplock count.
94 ****************************************************************************/
96 void release_file_oplock(files_struct *fsp)
98 struct smbd_server_connection *sconn = fsp->conn->sconn;
100 if ((fsp->oplock_type != NO_OPLOCK) &&
101 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
103 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
106 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
107 sconn->oplocks.level_II_open--;
108 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
109 sconn->oplocks.exclusive_open--;
112 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
113 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
115 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
116 /* This doesn't matter for close. */
117 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
119 fsp->oplock_type = NO_OPLOCK;
121 fsp->sent_oplock_break = NO_BREAK_SENT;
123 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
124 delete_write_cache(fsp);
126 TALLOC_FREE(fsp->oplock_timeout);
129 /****************************************************************************
130 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
131 ****************************************************************************/
133 static void downgrade_file_oplock(files_struct *fsp)
135 struct smbd_server_connection *sconn = fsp->conn->sconn;
137 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
138 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
143 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
145 fsp->oplock_type = LEVEL_II_OPLOCK;
146 sconn->oplocks.exclusive_open--;
147 sconn->oplocks.level_II_open++;
148 fsp->sent_oplock_break = NO_BREAK_SENT;
151 /****************************************************************************
152 Remove a file oplock. Copes with level II and exclusive.
153 Locks then unlocks the share mode lock. Client can decide to go directly
154 to none even if a "break-to-level II" was sent.
155 ****************************************************************************/
157 bool remove_oplock(files_struct *fsp)
160 struct share_mode_lock *lck;
162 /* Remove the oplock flag from the sharemode. */
163 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
165 DEBUG(0,("remove_oplock: failed to lock share entry for "
166 "file %s\n", fsp_str_dbg(fsp)));
169 ret = remove_share_oplock(lck, fsp);
171 DEBUG(0,("remove_oplock: failed to remove share oplock for "
172 "file %s fnum %d, %s\n",
173 fsp_str_dbg(fsp), fsp->fnum,
174 file_id_string_tos(&fsp->file_id)));
176 release_file_oplock(fsp);
182 * Deal with a reply when a break-to-level II was sent.
184 bool downgrade_oplock(files_struct *fsp)
187 struct share_mode_lock *lck;
189 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
191 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
192 "file %s\n", fsp_str_dbg(fsp)));
195 ret = downgrade_share_oplock(lck, fsp);
197 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
198 "for file %s fnum %d, file_id %s\n",
199 fsp_str_dbg(fsp), fsp->fnum,
200 file_id_string_tos(&fsp->file_id)));
203 downgrade_file_oplock(fsp);
209 * Some kernel oplock implementations handle the notification themselves.
211 bool should_notify_deferred_opens()
214 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
217 /****************************************************************************
218 Set up an oplock break message.
219 ****************************************************************************/
221 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
222 files_struct *fsp, int cmd)
224 char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
226 if (result == NULL) {
227 DEBUG(0, ("talloc failed\n"));
231 memset(result,'\0',smb_size);
232 srv_set_message(result,8,0,true);
233 SCVAL(result,smb_com,SMBlockingX);
234 SSVAL(result,smb_tid,fsp->conn->cnum);
235 SSVAL(result,smb_pid,0xFFFF);
236 SSVAL(result,smb_uid,0);
237 SSVAL(result,smb_mid,0xFFFF);
238 SCVAL(result,smb_vwv0,0xFF);
239 SSVAL(result,smb_vwv2,fsp->fnum);
240 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
241 SCVAL(result,smb_vwv3+1,cmd);
245 /****************************************************************************
246 Function to do the waiting before sending a local break.
247 ****************************************************************************/
249 static void wait_before_sending_break(void)
251 long wait_time = (long)lp_oplock_break_wait_time();
254 smb_msleep(wait_time);
258 /****************************************************************************
259 Ensure that we have a valid oplock.
260 ****************************************************************************/
262 static files_struct *initial_break_processing(
263 struct smbd_server_connection *sconn, struct file_id id,
264 unsigned long file_id)
266 files_struct *fsp = NULL;
268 if( DEBUGLVL( 3 ) ) {
269 dbgtext( "initial_break_processing: called for %s/%u\n",
270 file_id_string_tos(&id), (int)file_id);
271 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
272 sconn->oplocks.exclusive_open,
273 sconn->oplocks.level_II_open);
277 * We need to search the file open table for the
278 * entry containing this dev and inode, and ensure
279 * we have an oplock on it.
282 fsp = file_find_dif(sconn, id, file_id);
285 /* The file could have been closed in the meantime - return success. */
286 if( DEBUGLVL( 3 ) ) {
287 dbgtext( "initial_break_processing: cannot find open file with " );
288 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
289 dbgtext( "allowing break to succeed.\n" );
294 /* Ensure we have an oplock on the file */
297 * There is a potential race condition in that an oplock could
298 * have been broken due to another udp request, and yet there are
299 * still oplock break messages being sent in the udp message
300 * queue for this file. So return true if we don't have an oplock,
301 * as we may have just freed it.
304 if(fsp->oplock_type == NO_OPLOCK) {
305 if( DEBUGLVL( 3 ) ) {
306 dbgtext( "initial_break_processing: file %s ",
308 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
309 file_id_string_tos(&id), fsp->fh->gen_id );
310 dbgtext( "Allowing break to succeed regardless.\n" );
318 static void oplock_timeout_handler(struct event_context *ctx,
319 struct timed_event *te,
323 files_struct *fsp = (files_struct *)private_data;
325 /* Remove the timed event handler. */
326 TALLOC_FREE(fsp->oplock_timeout);
327 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
330 reply_to_oplock_break_requests(fsp);
333 /*******************************************************************
334 Add a timeout handler waiting for the client reply.
335 *******************************************************************/
337 static void add_oplock_timeout_handler(files_struct *fsp)
340 * If kernel oplocks already notifies smbds when an oplock break times
344 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
348 if (fsp->oplock_timeout != NULL) {
349 DEBUG(0, ("Logic problem -- have an oplock event hanging "
353 fsp->oplock_timeout =
354 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
355 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
356 oplock_timeout_handler, fsp);
358 if (fsp->oplock_timeout == NULL) {
359 DEBUG(0, ("Could not add oplock timeout handler\n"));
363 static void send_break_message_smb1(files_struct *fsp, int level)
365 char *break_msg = new_break_message_smb1(talloc_tos(),
368 if (break_msg == NULL) {
369 exit_server("Could not talloc break_msg\n");
373 if (!srv_send_smb(fsp->conn->sconn,
375 IS_CONN_ENCRYPTED(fsp->conn),
377 exit_server_cleanly("send_break_message_smb1: "
378 "srv_send_smb failed.");
381 TALLOC_FREE(break_msg);
384 void break_level2_to_none_async(files_struct *fsp)
386 struct smbd_server_connection *sconn = fsp->conn->sconn;
388 if (fsp->oplock_type == NO_OPLOCK) {
389 /* We already got a "break to none" message and we've handled
390 * it. just ignore. */
391 DEBUG(3, ("process_oplock_async_level2_break_message: already "
392 "broken to none, ignoring.\n"));
396 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
397 /* Don't tell the client, just downgrade. */
398 DEBUG(3, ("process_oplock_async_level2_break_message: "
399 "downgrading fake level 2 oplock.\n"));
404 /* Ensure we're really at level2 state. */
405 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
407 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
408 "to none message for fid %d, file %s\n", fsp->fnum,
411 /* Now send a break to none message to our client. */
412 if (sconn->using_smb2) {
413 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
415 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
418 /* Async level2 request, don't send a reply, just remove the oplock. */
422 /*******************************************************************
423 This handles the case of a write triggering a break to none
424 message on a level2 oplock.
425 When we get this message we may be in any of three states :
426 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
427 the client for LEVEL2.
428 *******************************************************************/
430 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
433 struct server_id src,
436 struct share_mode_entry msg;
438 struct smbd_server_connection *sconn =
439 talloc_get_type_abort(private_data,
440 struct smbd_server_connection);
442 if (data->data == NULL) {
443 DEBUG(0, ("Got NULL buffer\n"));
447 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
448 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
452 /* De-linearize incoming message. */
453 message_to_share_mode_entry(&msg, (char *)data->data);
455 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
456 "%s/%llu\n", server_id_str(talloc_tos(), &src),
457 file_id_string_tos(&msg.id),
458 (unsigned long long)msg.share_file_id));
460 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
463 /* We hit a race here. Break messages are sent, and before we
464 * get to process this message, we have closed the file.
465 * No need to reply as this is an async message. */
466 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
470 break_level2_to_none_async(fsp);
473 /*******************************************************************
474 This handles the generic oplock break message from another smbd.
475 *******************************************************************/
477 static void process_oplock_break_message(struct messaging_context *msg_ctx,
480 struct server_id src,
483 struct share_mode_entry msg;
485 bool break_to_level2 = False;
487 struct smbd_server_connection *sconn =
488 talloc_get_type_abort(private_data,
489 struct smbd_server_connection);
491 if (data->data == NULL) {
492 DEBUG(0, ("Got NULL buffer\n"));
496 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
497 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
501 /* De-linearize incoming message. */
502 message_to_share_mode_entry(&msg, (char *)data->data);
504 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
505 server_id_str(talloc_tos(), &src),
506 file_id_string_tos(&msg.id),
507 (unsigned long long)msg.share_file_id));
509 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
512 /* We hit a race here. Break messages are sent, and before we
513 * get to process this message, we have closed the file. Reply
514 * with 'ok, oplock broken' */
515 DEBUG(3, ("Did not find fsp\n"));
517 /* We just send the same message back. */
518 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
520 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
524 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
525 /* Remember we have to inform the requesting PID when the
528 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
529 &fsp->pending_break_messages,
530 &fsp->num_pending_break_messages);
534 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
535 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
536 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
537 file_id_string_tos(&fsp->file_id),
539 /* We just send the same message back. */
540 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
542 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
546 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
548 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
549 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
550 !(use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
551 lp_level2_oplocks(SNUM(fsp->conn))) {
552 break_to_level2 = True;
555 /* Need to wait before sending a break
556 message if we sent ourselves this message. */
557 if (procid_is_me(&src)) {
558 wait_before_sending_break();
561 if (sconn->using_smb2) {
562 send_break_message_smb2(fsp, break_to_level2 ?
563 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
565 send_break_message_smb1(fsp, break_to_level2 ?
566 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
569 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
572 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
573 &fsp->pending_break_messages,
574 &fsp->num_pending_break_messages);
576 add_oplock_timeout_handler(fsp);
579 /*******************************************************************
580 This handles the kernel oplock break message.
581 *******************************************************************/
583 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
586 struct server_id src,
590 unsigned long file_id;
592 struct smbd_server_connection *sconn =
593 talloc_get_type_abort(private_data,
594 struct smbd_server_connection);
596 if (data->data == NULL) {
597 DEBUG(0, ("Got NULL buffer\n"));
601 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
602 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
606 /* Pull the data from the message. */
607 pull_file_id_24((char *)data->data, &id);
608 file_id = (unsigned long)IVAL(data->data, 24);
610 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
611 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
612 (unsigned int)file_id));
614 fsp = initial_break_processing(sconn, id, file_id);
617 DEBUG(3, ("Got a kernel oplock break message for a file "
618 "I don't know about\n"));
622 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
623 /* This is ok, kernel oplocks come in completely async */
624 DEBUG(3, ("Got a kernel oplock request while waiting for a "
629 if (sconn->using_smb2) {
630 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
632 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
635 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
637 add_oplock_timeout_handler(fsp);
640 void reply_to_oplock_break_requests(files_struct *fsp)
645 * If kernel oplocks already notifies smbds when oplocks are
646 * broken/removed, just return.
649 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
653 for (i=0; i<fsp->num_pending_break_messages; i++) {
654 struct share_mode_entry *e = &fsp->pending_break_messages[i];
655 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
657 share_mode_entry_to_message(msg, e);
659 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
660 MSG_SMB_BREAK_RESPONSE,
662 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
665 SAFE_FREE(fsp->pending_break_messages);
666 fsp->num_pending_break_messages = 0;
667 TALLOC_FREE(fsp->oplock_timeout);
671 static void process_oplock_break_response(struct messaging_context *msg_ctx,
674 struct server_id src,
677 struct share_mode_entry msg;
678 struct smbd_server_connection *sconn =
679 talloc_get_type_abort(private_data,
680 struct smbd_server_connection);
682 if (data->data == NULL) {
683 DEBUG(0, ("Got NULL buffer\n"));
687 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
688 DEBUG(0, ("Got invalid msg len %u\n",
689 (unsigned int)data->length));
693 /* De-linearize incoming message. */
694 message_to_share_mode_entry(&msg, (char *)data->data);
696 DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
697 server_id_str(talloc_tos(), &src),
698 file_id_string_tos(&msg.id),
699 (unsigned long long)msg.share_file_id,
700 (unsigned long long)msg.op_mid));
702 schedule_deferred_open_message_smb(sconn, msg.op_mid);
705 static void process_open_retry_message(struct messaging_context *msg_ctx,
708 struct server_id src,
711 struct share_mode_entry msg;
712 struct smbd_server_connection *sconn =
713 talloc_get_type_abort(private_data,
714 struct smbd_server_connection);
716 if (data->data == NULL) {
717 DEBUG(0, ("Got NULL buffer\n"));
721 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
722 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
726 /* De-linearize incoming message. */
727 message_to_share_mode_entry(&msg, (char *)data->data);
729 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
730 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
731 (unsigned long long)msg.op_mid));
733 schedule_deferred_open_message_smb(sconn, msg.op_mid);
736 struct break_to_none_state {
737 struct smbd_server_connection *sconn;
740 static void do_break_to_none(struct tevent_req *req);
742 /****************************************************************************
743 This function is called on any file modification or lock request. If a file
744 is level 2 oplocked then it must tell all other level 2 holders to break to
746 ****************************************************************************/
748 static void contend_level2_oplocks_begin_default(files_struct *fsp,
749 enum level2_contention_type type)
751 struct smbd_server_connection *sconn = fsp->conn->sconn;
752 struct tevent_req *req;
753 struct break_to_none_state *state;
756 * If this file is level II oplocked then we need
757 * to grab the shared memory lock and inform all
758 * other files with a level II lock that they need
759 * to flush their read caches. We keep the lock over
760 * the shared memory area whilst doing this.
763 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
767 * When we get here we might have a brlock entry locked. Also
768 * locking the share mode entry would violate the locking
769 * order. Breaking level2 oplocks to none is asynchronous
770 * anyway, so we postpone this into an immediate timed event.
773 state = talloc(sconn, struct break_to_none_state);
775 DEBUG(1, ("talloc failed\n"));
778 state->sconn = sconn;
779 state->id = fsp->file_id;
781 req = tevent_wakeup_send(state, sconn->ev_ctx, timeval_set(0, 0));
783 DEBUG(1, ("tevent_wakeup_send failed\n"));
787 tevent_req_set_callback(req, do_break_to_none, state);
791 static void do_break_to_none(struct tevent_req *req)
793 struct break_to_none_state *state = tevent_req_callback_data(
794 req, struct break_to_none_state);
797 struct share_mode_lock *lck;
799 ret = tevent_wakeup_recv(req);
802 DEBUG(1, ("tevent_wakeup_recv failed\n"));
805 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
807 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
808 "share mode entry for file %s.\n",
809 file_id_string_tos(&state->id)));
813 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
814 lck->data->num_share_modes ));
816 for(i = 0; i < lck->data->num_share_modes; i++) {
817 struct share_mode_entry *share_entry = &lck->data->share_modes[i];
818 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
820 if (!is_valid_share_mode_entry(share_entry)) {
825 * As there could have been multiple writes waiting at the
826 * lock_share_entry gate we may not be the first to
827 * enter. Hence the state of the op_types in the share mode
828 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
829 * oplock. It will do no harm to re-send break messages to
830 * those smbd's that are still waiting their turn to remove
831 * their LEVEL_II state, and also no harm to ignore existing
832 * NO_OPLOCK states. JRA.
835 DEBUG(10,("release_level_2_oplocks_on_change: "
836 "share_entry[%i]->op_type == %d\n",
837 i, share_entry->op_type ));
839 if (share_entry->op_type == NO_OPLOCK) {
844 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
845 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
846 "share mode entry %d is an exlusive "
852 share_mode_entry_to_message(msg, share_entry);
855 * Deal with a race condition when breaking level2
856 * oplocks. Don't send all the messages and release
857 * the lock, this allows someone else to come in and
858 * get a level2 lock before any of the messages are
859 * processed, and thus miss getting a break message.
860 * Ensure at least one entry (the one we're breaking)
861 * is processed immediately under the lock and becomes
862 * set as NO_OPLOCK to stop any waiter getting a level2.
866 if (procid_is_me(&share_entry->pid)) {
867 struct files_struct *cur_fsp =
868 initial_break_processing(state->sconn,
870 share_entry->share_file_id);
871 wait_before_sending_break();
872 if (cur_fsp != NULL) {
873 break_level2_to_none_async(cur_fsp);
875 DEBUG(3, ("release_level_2_oplocks_on_change: "
876 "Did not find fsp, ignoring\n"));
879 messaging_send_buf(state->sconn->msg_ctx,
881 MSG_SMB_ASYNC_LEVEL2_BREAK,
883 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
887 /* We let the message receivers handle removing the oplock state
888 in the share mode lock db. */
896 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
897 enum level2_contention_type type)
899 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
900 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
904 contend_level2_oplocks_begin_default(fsp, type);
907 void smbd_contend_level2_oplocks_end(files_struct *fsp,
908 enum level2_contention_type type)
910 /* Only kernel oplocks implement this so far */
911 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
912 koplocks->ops->contend_level2_oplocks_end(fsp, type);
916 /****************************************************************************
917 Linearize a share mode entry struct to an internal oplock break message.
918 ****************************************************************************/
920 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
922 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
923 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
924 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
925 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
926 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
927 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
928 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
929 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
930 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
931 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
932 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
933 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
934 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
935 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
938 /****************************************************************************
939 De-linearize an internal oplock break message to a share mode entry struct.
940 ****************************************************************************/
942 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
944 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
945 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
946 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
947 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
948 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
949 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
950 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
951 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
952 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
953 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
954 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
955 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
956 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
957 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
960 /****************************************************************************
961 Setup oplocks for this process.
962 ****************************************************************************/
964 bool init_oplocks(struct smbd_server_connection *sconn)
966 DEBUG(3,("init_oplocks: initializing messages.\n"));
968 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
969 process_oplock_break_message);
970 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
971 process_oplock_async_level2_break_message);
972 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_RESPONSE,
973 process_oplock_break_response);
974 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
975 process_kernel_oplock_break);
976 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_OPEN_RETRY,
977 process_open_retry_message);
982 void init_kernel_oplocks(struct smbd_server_connection *sconn)
984 /* only initialize once */
985 if (koplocks == NULL) {
986 #if HAVE_KERNEL_OPLOCKS_IRIX
987 koplocks = irix_init_kernel_oplocks(sconn);
988 #elif HAVE_KERNEL_OPLOCKS_LINUX
989 koplocks = linux_init_kernel_oplocks(sconn);