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"
29 /****************************************************************************
30 Get the number of current exclusive oplocks.
31 ****************************************************************************/
33 int32 get_number_of_exclusive_open_oplocks(void)
35 return exclusive_oplocks_open;
39 * helper function used by the kernel oplock backends to post the break message
41 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
43 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
45 /* Put the kernel break info into the message. */
46 push_file_id_24((char *)msg, &fsp->file_id);
47 SIVAL(msg,24,fsp->fh->gen_id);
49 /* Don't need to be root here as we're only ever
50 sending to ourselves. */
52 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
54 msg, MSG_SMB_KERNEL_BREAK_SIZE);
57 /****************************************************************************
58 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
59 disabled (just sets flags) and no byte-range locks in the file. Returns True
61 ****************************************************************************/
63 bool set_file_oplock(files_struct *fsp, int oplock_type)
65 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
67 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
68 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
69 "don't support them\n"));
74 if ((fsp->oplock_type != NO_OPLOCK) &&
75 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
77 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
81 fsp->oplock_type = oplock_type;
82 fsp->sent_oplock_break = NO_BREAK_SENT;
83 if (oplock_type == LEVEL_II_OPLOCK) {
84 level_II_oplocks_open++;
85 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
86 exclusive_oplocks_open++;
89 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
90 "tv_sec = %x, tv_usec = %x\n",
91 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
92 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
93 (int)fsp->open_time.tv_usec ));
98 /****************************************************************************
99 Attempt to release an oplock on a file. Decrements oplock count.
100 ****************************************************************************/
102 void release_file_oplock(files_struct *fsp)
104 if ((fsp->oplock_type != NO_OPLOCK) &&
105 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
107 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
110 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
111 level_II_oplocks_open--;
112 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
113 exclusive_oplocks_open--;
116 SMB_ASSERT(exclusive_oplocks_open>=0);
117 SMB_ASSERT(level_II_oplocks_open>=0);
119 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
120 /* This doesn't matter for close. */
121 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
123 fsp->oplock_type = NO_OPLOCK;
125 fsp->sent_oplock_break = NO_BREAK_SENT;
127 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
128 delete_write_cache(fsp);
130 TALLOC_FREE(fsp->oplock_timeout);
133 /****************************************************************************
134 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
135 ****************************************************************************/
137 static void downgrade_file_oplock(files_struct *fsp)
139 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
140 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
145 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
147 fsp->oplock_type = LEVEL_II_OPLOCK;
148 exclusive_oplocks_open--;
149 level_II_oplocks_open++;
150 fsp->sent_oplock_break = NO_BREAK_SENT;
153 /****************************************************************************
154 Remove a file oplock. Copes with level II and exclusive.
155 Locks then unlocks the share mode lock. Client can decide to go directly
156 to none even if a "break-to-level II" was sent.
157 ****************************************************************************/
159 bool remove_oplock(files_struct *fsp)
162 struct share_mode_lock *lck;
164 /* Remove the oplock flag from the sharemode. */
165 lck = get_share_mode_lock(talloc_tos(), fsp->file_id);
167 DEBUG(0,("remove_oplock: failed to lock share entry for "
168 "file %s\n", fsp_str_dbg(fsp)));
171 ret = remove_share_oplock(lck, fsp);
173 DEBUG(0,("remove_oplock: failed to remove share oplock for "
174 "file %s fnum %d, %s\n",
175 fsp_str_dbg(fsp), fsp->fnum,
176 file_id_string_tos(&fsp->file_id)));
178 release_file_oplock(fsp);
184 * Deal with a reply when a break-to-level II was sent.
186 bool downgrade_oplock(files_struct *fsp)
189 struct share_mode_lock *lck;
191 lck = get_share_mode_lock(talloc_tos(), fsp->file_id);
193 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
194 "file %s\n", fsp_str_dbg(fsp)));
197 ret = downgrade_share_oplock(lck, fsp);
199 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
200 "for file %s fnum %d, file_id %s\n",
201 fsp_str_dbg(fsp), fsp->fnum,
202 file_id_string_tos(&fsp->file_id)));
205 downgrade_file_oplock(fsp);
211 * Some kernel oplock implementations handle the notification themselves.
213 bool should_notify_deferred_opens()
216 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
219 /****************************************************************************
220 Set up an oplock break message.
221 ****************************************************************************/
223 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
224 files_struct *fsp, int cmd)
226 char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
228 if (result == NULL) {
229 DEBUG(0, ("talloc failed\n"));
233 memset(result,'\0',smb_size);
234 srv_set_message(result,8,0,true);
235 SCVAL(result,smb_com,SMBlockingX);
236 SSVAL(result,smb_tid,fsp->conn->cnum);
237 SSVAL(result,smb_pid,0xFFFF);
238 SSVAL(result,smb_uid,0);
239 SSVAL(result,smb_mid,0xFFFF);
240 SCVAL(result,smb_vwv0,0xFF);
241 SSVAL(result,smb_vwv2,fsp->fnum);
242 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
243 SCVAL(result,smb_vwv3+1,cmd);
247 /****************************************************************************
248 Function to do the waiting before sending a local break.
249 ****************************************************************************/
251 static void wait_before_sending_break(void)
253 long wait_time = (long)lp_oplock_break_wait_time();
256 smb_msleep(wait_time);
260 /****************************************************************************
261 Ensure that we have a valid oplock.
262 ****************************************************************************/
264 static files_struct *initial_break_processing(
265 struct smbd_server_connection *sconn, struct file_id id,
266 unsigned long file_id)
268 files_struct *fsp = NULL;
270 if( DEBUGLVL( 3 ) ) {
271 dbgtext( "initial_break_processing: called for %s/%u\n",
272 file_id_string_tos(&id), (int)file_id);
273 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
274 exclusive_oplocks_open, level_II_oplocks_open );
278 * We need to search the file open table for the
279 * entry containing this dev and inode, and ensure
280 * we have an oplock on it.
283 fsp = file_find_dif(sconn, id, file_id);
286 /* The file could have been closed in the meantime - return success. */
287 if( DEBUGLVL( 3 ) ) {
288 dbgtext( "initial_break_processing: cannot find open file with " );
289 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
290 dbgtext( "allowing break to succeed.\n" );
295 /* Ensure we have an oplock on the file */
298 * There is a potential race condition in that an oplock could
299 * have been broken due to another udp request, and yet there are
300 * still oplock break messages being sent in the udp message
301 * queue for this file. So return true if we don't have an oplock,
302 * as we may have just freed it.
305 if(fsp->oplock_type == NO_OPLOCK) {
306 if( DEBUGLVL( 3 ) ) {
307 dbgtext( "initial_break_processing: file %s ",
309 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
310 file_id_string_tos(&id), fsp->fh->gen_id );
311 dbgtext( "Allowing break to succeed regardless.\n" );
319 static void oplock_timeout_handler(struct event_context *ctx,
320 struct timed_event *te,
324 files_struct *fsp = (files_struct *)private_data;
326 /* Remove the timed event handler. */
327 TALLOC_FREE(fsp->oplock_timeout);
328 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
331 reply_to_oplock_break_requests(fsp);
334 /*******************************************************************
335 Add a timeout handler waiting for the client reply.
336 *******************************************************************/
338 static void add_oplock_timeout_handler(files_struct *fsp)
341 * If kernel oplocks already notifies smbds when an oplock break times
345 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
349 if (fsp->oplock_timeout != NULL) {
350 DEBUG(0, ("Logic problem -- have an oplock event hanging "
354 fsp->oplock_timeout =
355 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
356 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
357 oplock_timeout_handler, fsp);
359 if (fsp->oplock_timeout == NULL) {
360 DEBUG(0, ("Could not add oplock timeout handler\n"));
364 static void send_break_message_smb1(files_struct *fsp, int level)
366 char *break_msg = new_break_message_smb1(talloc_tos(),
369 if (break_msg == NULL) {
370 exit_server("Could not talloc break_msg\n");
374 if (!srv_send_smb(fsp->conn->sconn,
376 IS_CONN_ENCRYPTED(fsp->conn),
378 exit_server_cleanly("send_break_message_smb1: "
379 "srv_send_smb failed.");
382 TALLOC_FREE(break_msg);
385 void break_level2_to_none_async(files_struct *fsp)
387 struct smbd_server_connection *sconn = fsp->conn->sconn;
389 if (fsp->oplock_type == NO_OPLOCK) {
390 /* We already got a "break to none" message and we've handled
391 * it. just ignore. */
392 DEBUG(3, ("process_oplock_async_level2_break_message: already "
393 "broken to none, ignoring.\n"));
397 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
398 /* Don't tell the client, just downgrade. */
399 DEBUG(3, ("process_oplock_async_level2_break_message: "
400 "downgrading fake level 2 oplock.\n"));
405 /* Ensure we're really at level2 state. */
406 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
408 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
409 "to none message for fid %d, file %s\n", fsp->fnum,
412 /* Now send a break to none message to our client. */
413 if (sconn->using_smb2) {
414 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
416 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
419 /* Async level2 request, don't send a reply, just remove the oplock. */
423 /*******************************************************************
424 This handles the case of a write triggering a break to none
425 message on a level2 oplock.
426 When we get this message we may be in any of three states :
427 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
428 the client for LEVEL2.
429 *******************************************************************/
431 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
434 struct server_id src,
437 struct share_mode_entry msg;
439 struct smbd_server_connection *sconn =
440 talloc_get_type_abort(private_data,
441 struct smbd_server_connection);
443 if (data->data == NULL) {
444 DEBUG(0, ("Got NULL buffer\n"));
448 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
449 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
453 /* De-linearize incoming message. */
454 message_to_share_mode_entry(&msg, (char *)data->data);
456 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
457 "%s/%llu\n", server_id_str(talloc_tos(), &src),
458 file_id_string_tos(&msg.id),
459 (unsigned long long)msg.share_file_id));
461 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
464 /* We hit a race here. Break messages are sent, and before we
465 * get to process this message, we have closed the file.
466 * No need to reply as this is an async message. */
467 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
471 break_level2_to_none_async(fsp);
474 /*******************************************************************
475 This handles the generic oplock break message from another smbd.
476 *******************************************************************/
478 static void process_oplock_break_message(struct messaging_context *msg_ctx,
481 struct server_id src,
484 struct share_mode_entry msg;
486 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 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
547 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
548 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
549 lp_level2_oplocks(SNUM(fsp->conn))) {
550 break_to_level2 = True;
553 /* Need to wait before sending a break
554 message if we sent ourselves this message. */
555 if (procid_is_me(&src)) {
556 wait_before_sending_break();
559 if (sconn->using_smb2) {
560 send_break_message_smb2(fsp, break_to_level2 ?
561 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
563 send_break_message_smb1(fsp, break_to_level2 ?
564 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
567 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
570 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
571 &fsp->pending_break_messages,
572 &fsp->num_pending_break_messages);
574 add_oplock_timeout_handler(fsp);
577 /*******************************************************************
578 This handles the kernel oplock break message.
579 *******************************************************************/
581 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
584 struct server_id src,
588 unsigned long file_id;
590 struct smbd_server_connection *sconn =
591 talloc_get_type_abort(private_data,
592 struct smbd_server_connection);
594 if (data->data == NULL) {
595 DEBUG(0, ("Got NULL buffer\n"));
599 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
600 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
604 /* Pull the data from the message. */
605 pull_file_id_24((char *)data->data, &id);
606 file_id = (unsigned long)IVAL(data->data, 24);
608 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
609 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
610 (unsigned int)file_id));
612 fsp = initial_break_processing(sconn, id, file_id);
615 DEBUG(3, ("Got a kernel oplock break message for a file "
616 "I don't know about\n"));
620 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
621 /* This is ok, kernel oplocks come in completely async */
622 DEBUG(3, ("Got a kernel oplock request while waiting for a "
627 if (sconn->using_smb2) {
628 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
630 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
633 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
635 add_oplock_timeout_handler(fsp);
638 void reply_to_oplock_break_requests(files_struct *fsp)
643 * If kernel oplocks already notifies smbds when oplocks are
644 * broken/removed, just return.
647 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
651 for (i=0; i<fsp->num_pending_break_messages; i++) {
652 struct share_mode_entry *e = &fsp->pending_break_messages[i];
653 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
655 share_mode_entry_to_message(msg, e);
657 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
658 MSG_SMB_BREAK_RESPONSE,
660 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
663 SAFE_FREE(fsp->pending_break_messages);
664 fsp->num_pending_break_messages = 0;
665 if (fsp->oplock_timeout != NULL) {
666 /* Remove the timed event handler. */
667 TALLOC_FREE(fsp->oplock_timeout);
668 fsp->oplock_timeout = NULL;
673 static void process_oplock_break_response(struct messaging_context *msg_ctx,
676 struct server_id src,
679 struct share_mode_entry msg;
680 struct smbd_server_connection *sconn =
681 talloc_get_type_abort(private_data,
682 struct smbd_server_connection);
684 if (data->data == NULL) {
685 DEBUG(0, ("Got NULL buffer\n"));
689 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
690 DEBUG(0, ("Got invalid msg len %u\n",
691 (unsigned int)data->length));
695 /* De-linearize incoming message. */
696 message_to_share_mode_entry(&msg, (char *)data->data);
698 DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
699 server_id_str(talloc_tos(), &src),
700 file_id_string_tos(&msg.id),
701 (unsigned long long)msg.share_file_id,
702 (unsigned long long)msg.op_mid));
704 schedule_deferred_open_message_smb(sconn, msg.op_mid);
707 static void process_open_retry_message(struct messaging_context *msg_ctx,
710 struct server_id src,
713 struct share_mode_entry msg;
714 struct smbd_server_connection *sconn =
715 talloc_get_type_abort(private_data,
716 struct smbd_server_connection);
718 if (data->data == NULL) {
719 DEBUG(0, ("Got NULL buffer\n"));
723 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
724 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
728 /* De-linearize incoming message. */
729 message_to_share_mode_entry(&msg, (char *)data->data);
731 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
732 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
733 (unsigned long long)msg.op_mid));
735 schedule_deferred_open_message_smb(sconn, msg.op_mid);
738 /****************************************************************************
739 This function is called on any file modification or lock request. If a file
740 is level 2 oplocked then it must tell all other level 2 holders to break to
742 ****************************************************************************/
744 static void contend_level2_oplocks_begin_default(files_struct *fsp,
745 enum level2_contention_type type)
748 struct share_mode_lock *lck;
751 * If this file is level II oplocked then we need
752 * to grab the shared memory lock and inform all
753 * other files with a level II lock that they need
754 * to flush their read caches. We keep the lock over
755 * the shared memory area whilst doing this.
758 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
761 lck = get_share_mode_lock(talloc_tos(), fsp->file_id);
763 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
764 "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
768 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
769 lck->num_share_modes ));
771 for(i = 0; i < lck->num_share_modes; i++) {
772 struct share_mode_entry *share_entry = &lck->share_modes[i];
773 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
775 if (!is_valid_share_mode_entry(share_entry)) {
780 * As there could have been multiple writes waiting at the
781 * lock_share_entry gate we may not be the first to
782 * enter. Hence the state of the op_types in the share mode
783 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
784 * oplock. It will do no harm to re-send break messages to
785 * those smbd's that are still waiting their turn to remove
786 * their LEVEL_II state, and also no harm to ignore existing
787 * NO_OPLOCK states. JRA.
790 DEBUG(10,("release_level_2_oplocks_on_change: "
791 "share_entry[%i]->op_type == %d\n",
792 i, share_entry->op_type ));
794 if (share_entry->op_type == NO_OPLOCK) {
799 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
800 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
801 "share mode entry %d is an exlusive "
807 share_mode_entry_to_message(msg, share_entry);
810 * Deal with a race condition when breaking level2
811 * oplocks. Don't send all the messages and release
812 * the lock, this allows someone else to come in and
813 * get a level2 lock before any of the messages are
814 * processed, and thus miss getting a break message.
815 * Ensure at least one entry (the one we're breaking)
816 * is processed immediately under the lock and becomes
817 * set as NO_OPLOCK to stop any waiter getting a level2.
821 if (procid_is_me(&share_entry->pid)) {
822 struct files_struct *cur_fsp =
823 initial_break_processing(fsp->conn->sconn,
825 share_entry->share_file_id);
826 wait_before_sending_break();
827 if (cur_fsp != NULL) {
828 break_level2_to_none_async(cur_fsp);
830 DEBUG(3, ("release_level_2_oplocks_on_change: "
831 "Did not find fsp, ignoring\n"));
834 messaging_send_buf(fsp->conn->sconn->msg_ctx,
836 MSG_SMB_ASYNC_LEVEL2_BREAK,
838 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
842 /* We let the message receivers handle removing the oplock state
843 in the share mode lock db. */
848 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
849 enum level2_contention_type type)
851 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
852 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
856 contend_level2_oplocks_begin_default(fsp, type);
859 void smbd_contend_level2_oplocks_end(files_struct *fsp,
860 enum level2_contention_type type)
862 /* Only kernel oplocks implement this so far */
863 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
864 koplocks->ops->contend_level2_oplocks_end(fsp, type);
868 /****************************************************************************
869 Linearize a share mode entry struct to an internal oplock break message.
870 ****************************************************************************/
872 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
874 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
875 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
876 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
877 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
878 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
879 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
880 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
881 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
882 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
883 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
884 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
885 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
886 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
887 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
890 /****************************************************************************
891 De-linearize an internal oplock break message to a share mode entry struct.
892 ****************************************************************************/
894 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
896 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
897 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
898 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
899 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
900 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
901 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
902 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
903 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
904 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
905 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
906 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
907 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
908 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
909 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
912 /****************************************************************************
913 Setup oplocks for this process.
914 ****************************************************************************/
916 bool init_oplocks(struct smbd_server_connection *sconn)
918 DEBUG(3,("init_oplocks: initializing messages.\n"));
920 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
921 process_oplock_break_message);
922 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
923 process_oplock_async_level2_break_message);
924 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_RESPONSE,
925 process_oplock_break_response);
926 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
927 process_kernel_oplock_break);
928 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_OPEN_RETRY,
929 process_open_retry_message);
931 if (lp_kernel_oplocks()) {
932 #if HAVE_KERNEL_OPLOCKS_IRIX
933 koplocks = irix_init_kernel_oplocks(sconn);
934 #elif HAVE_KERNEL_OPLOCKS_LINUX
935 koplocks = linux_init_kernel_oplocks(sconn);
937 #error Isilon, please check if the NULL context is okay here. Thanks!
938 koplocks = onefs_init_kernel_oplocks(sconn);