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)
66 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
68 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
70 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
71 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
72 "don't support them\n"));
77 if ((fsp->oplock_type != NO_OPLOCK) &&
78 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
80 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
84 fsp->oplock_type = oplock_type;
85 fsp->sent_oplock_break = NO_BREAK_SENT;
86 if (oplock_type == LEVEL_II_OPLOCK) {
87 level_II_oplocks_open++;
88 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
89 exclusive_oplocks_open++;
92 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
93 "tv_sec = %x, tv_usec = %x\n",
94 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
95 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
96 (int)fsp->open_time.tv_usec ));
101 /****************************************************************************
102 Attempt to release an oplock on a file. Decrements oplock count.
103 ****************************************************************************/
105 void release_file_oplock(files_struct *fsp)
107 if ((fsp->oplock_type != NO_OPLOCK) &&
108 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
110 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
113 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
114 level_II_oplocks_open--;
115 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
116 exclusive_oplocks_open--;
119 SMB_ASSERT(exclusive_oplocks_open>=0);
120 SMB_ASSERT(level_II_oplocks_open>=0);
122 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
123 /* This doesn't matter for close. */
124 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
126 fsp->oplock_type = NO_OPLOCK;
128 fsp->sent_oplock_break = NO_BREAK_SENT;
130 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
131 delete_write_cache(fsp);
133 TALLOC_FREE(fsp->oplock_timeout);
136 /****************************************************************************
137 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
138 ****************************************************************************/
140 static void downgrade_file_oplock(files_struct *fsp)
142 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
143 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
148 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
150 fsp->oplock_type = LEVEL_II_OPLOCK;
151 exclusive_oplocks_open--;
152 level_II_oplocks_open++;
153 fsp->sent_oplock_break = NO_BREAK_SENT;
156 /****************************************************************************
157 Remove a file oplock. Copes with level II and exclusive.
158 Locks then unlocks the share mode lock. Client can decide to go directly
159 to none even if a "break-to-level II" was sent.
160 ****************************************************************************/
162 bool remove_oplock(files_struct *fsp)
165 struct share_mode_lock *lck;
167 /* Remove the oplock flag from the sharemode. */
168 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
170 DEBUG(0,("remove_oplock: failed to lock share entry for "
171 "file %s\n", fsp_str_dbg(fsp)));
174 ret = remove_share_oplock(lck, fsp);
176 DEBUG(0,("remove_oplock: failed to remove share oplock for "
177 "file %s fnum %d, %s\n",
178 fsp_str_dbg(fsp), fsp->fnum,
179 file_id_string_tos(&fsp->file_id)));
181 release_file_oplock(fsp);
187 * Deal with a reply when a break-to-level II was sent.
189 bool downgrade_oplock(files_struct *fsp)
192 struct share_mode_lock *lck;
194 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
196 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
197 "file %s\n", fsp_str_dbg(fsp)));
200 ret = downgrade_share_oplock(lck, fsp);
202 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
203 "for file %s fnum %d, file_id %s\n",
204 fsp_str_dbg(fsp), fsp->fnum,
205 file_id_string_tos(&fsp->file_id)));
208 downgrade_file_oplock(fsp);
214 * Some kernel oplock implementations handle the notification themselves.
216 bool should_notify_deferred_opens()
219 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
222 /****************************************************************************
223 Set up an oplock break message.
224 ****************************************************************************/
226 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
227 files_struct *fsp, int cmd)
229 char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
231 if (result == NULL) {
232 DEBUG(0, ("talloc failed\n"));
236 memset(result,'\0',smb_size);
237 srv_set_message(result,8,0,true);
238 SCVAL(result,smb_com,SMBlockingX);
239 SSVAL(result,smb_tid,fsp->conn->cnum);
240 SSVAL(result,smb_pid,0xFFFF);
241 SSVAL(result,smb_uid,0);
242 SSVAL(result,smb_mid,0xFFFF);
243 SCVAL(result,smb_vwv0,0xFF);
244 SSVAL(result,smb_vwv2,fsp->fnum);
245 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
246 SCVAL(result,smb_vwv3+1,cmd);
250 /****************************************************************************
251 Function to do the waiting before sending a local break.
252 ****************************************************************************/
254 static void wait_before_sending_break(void)
256 long wait_time = (long)lp_oplock_break_wait_time();
259 smb_msleep(wait_time);
263 /****************************************************************************
264 Ensure that we have a valid oplock.
265 ****************************************************************************/
267 static files_struct *initial_break_processing(
268 struct smbd_server_connection *sconn, struct file_id id,
269 unsigned long file_id)
271 files_struct *fsp = NULL;
273 if( DEBUGLVL( 3 ) ) {
274 dbgtext( "initial_break_processing: called for %s/%u\n",
275 file_id_string_tos(&id), (int)file_id);
276 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
277 exclusive_oplocks_open, level_II_oplocks_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)
344 * If kernel oplocks already notifies smbds when an oplock break times
348 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
352 if (fsp->oplock_timeout != NULL) {
353 DEBUG(0, ("Logic problem -- have an oplock event hanging "
357 fsp->oplock_timeout =
358 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
359 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
360 oplock_timeout_handler, fsp);
362 if (fsp->oplock_timeout == NULL) {
363 DEBUG(0, ("Could not add oplock timeout handler\n"));
367 static void send_break_message_smb1(files_struct *fsp, int level)
369 char *break_msg = new_break_message_smb1(talloc_tos(),
372 if (break_msg == NULL) {
373 exit_server("Could not talloc break_msg\n");
377 if (!srv_send_smb(fsp->conn->sconn,
379 IS_CONN_ENCRYPTED(fsp->conn),
381 exit_server_cleanly("send_break_message_smb1: "
382 "srv_send_smb failed.");
385 TALLOC_FREE(break_msg);
388 void break_level2_to_none_async(files_struct *fsp)
390 struct smbd_server_connection *sconn = fsp->conn->sconn;
392 if (fsp->oplock_type == NO_OPLOCK) {
393 /* We already got a "break to none" message and we've handled
394 * it. just ignore. */
395 DEBUG(3, ("process_oplock_async_level2_break_message: already "
396 "broken to none, ignoring.\n"));
400 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
401 /* Don't tell the client, just downgrade. */
402 DEBUG(3, ("process_oplock_async_level2_break_message: "
403 "downgrading fake level 2 oplock.\n"));
408 /* Ensure we're really at level2 state. */
409 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
411 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
412 "to none message for fid %d, file %s\n", fsp->fnum,
415 /* Now send a break to none message to our client. */
416 if (sconn->using_smb2) {
417 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
419 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
422 /* Async level2 request, don't send a reply, just remove the oplock. */
426 /*******************************************************************
427 This handles the case of a write triggering a break to none
428 message on a level2 oplock.
429 When we get this message we may be in any of three states :
430 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
431 the client for LEVEL2.
432 *******************************************************************/
434 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
437 struct server_id src,
440 struct share_mode_entry msg;
442 struct smbd_server_connection *sconn =
443 talloc_get_type_abort(private_data,
444 struct smbd_server_connection);
446 if (data->data == NULL) {
447 DEBUG(0, ("Got NULL buffer\n"));
451 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
452 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
456 /* De-linearize incoming message. */
457 message_to_share_mode_entry(&msg, (char *)data->data);
459 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
460 "%s/%llu\n", server_id_str(talloc_tos(), &src),
461 file_id_string_tos(&msg.id),
462 (unsigned long long)msg.share_file_id));
464 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
467 /* We hit a race here. Break messages are sent, and before we
468 * get to process this message, we have closed the file.
469 * No need to reply as this is an async message. */
470 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
474 break_level2_to_none_async(fsp);
477 /*******************************************************************
478 This handles the generic oplock break message from another smbd.
479 *******************************************************************/
481 static void process_oplock_break_message(struct messaging_context *msg_ctx,
484 struct server_id src,
487 struct share_mode_entry msg;
489 bool break_to_level2 = False;
491 struct smbd_server_connection *sconn =
492 talloc_get_type_abort(private_data,
493 struct smbd_server_connection);
495 if (data->data == NULL) {
496 DEBUG(0, ("Got NULL buffer\n"));
500 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
501 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
505 /* De-linearize incoming message. */
506 message_to_share_mode_entry(&msg, (char *)data->data);
508 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
509 server_id_str(talloc_tos(), &src),
510 file_id_string_tos(&msg.id),
511 (unsigned long long)msg.share_file_id));
513 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
516 /* We hit a race here. Break messages are sent, and before we
517 * get to process this message, we have closed the file. Reply
518 * with 'ok, oplock broken' */
519 DEBUG(3, ("Did not find fsp\n"));
521 /* We just send the same message back. */
522 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
524 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
528 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
529 /* Remember we have to inform the requesting PID when the
532 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
533 &fsp->pending_break_messages,
534 &fsp->num_pending_break_messages);
538 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
539 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
540 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
541 file_id_string_tos(&fsp->file_id),
543 /* We just send the same message back. */
544 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
546 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
550 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
552 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
553 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
554 !(use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
555 lp_level2_oplocks(SNUM(fsp->conn))) {
556 break_to_level2 = True;
559 /* Need to wait before sending a break
560 message if we sent ourselves this message. */
561 if (procid_is_me(&src)) {
562 wait_before_sending_break();
565 if (sconn->using_smb2) {
566 send_break_message_smb2(fsp, break_to_level2 ?
567 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
569 send_break_message_smb1(fsp, break_to_level2 ?
570 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
573 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
576 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
577 &fsp->pending_break_messages,
578 &fsp->num_pending_break_messages);
580 add_oplock_timeout_handler(fsp);
583 /*******************************************************************
584 This handles the kernel oplock break message.
585 *******************************************************************/
587 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
590 struct server_id src,
594 unsigned long file_id;
596 struct smbd_server_connection *sconn =
597 talloc_get_type_abort(private_data,
598 struct smbd_server_connection);
600 if (data->data == NULL) {
601 DEBUG(0, ("Got NULL buffer\n"));
605 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
606 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
610 /* Pull the data from the message. */
611 pull_file_id_24((char *)data->data, &id);
612 file_id = (unsigned long)IVAL(data->data, 24);
614 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
615 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
616 (unsigned int)file_id));
618 fsp = initial_break_processing(sconn, id, file_id);
621 DEBUG(3, ("Got a kernel oplock break message for a file "
622 "I don't know about\n"));
626 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
627 /* This is ok, kernel oplocks come in completely async */
628 DEBUG(3, ("Got a kernel oplock request while waiting for a "
633 if (sconn->using_smb2) {
634 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
636 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
639 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
641 add_oplock_timeout_handler(fsp);
644 void reply_to_oplock_break_requests(files_struct *fsp)
649 * If kernel oplocks already notifies smbds when oplocks are
650 * broken/removed, just return.
653 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
657 for (i=0; i<fsp->num_pending_break_messages; i++) {
658 struct share_mode_entry *e = &fsp->pending_break_messages[i];
659 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
661 share_mode_entry_to_message(msg, e);
663 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
664 MSG_SMB_BREAK_RESPONSE,
666 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
669 SAFE_FREE(fsp->pending_break_messages);
670 fsp->num_pending_break_messages = 0;
671 if (fsp->oplock_timeout != NULL) {
672 /* Remove the timed event handler. */
673 TALLOC_FREE(fsp->oplock_timeout);
674 fsp->oplock_timeout = NULL;
679 static void process_oplock_break_response(struct messaging_context *msg_ctx,
682 struct server_id src,
685 struct share_mode_entry msg;
686 struct smbd_server_connection *sconn =
687 talloc_get_type_abort(private_data,
688 struct smbd_server_connection);
690 if (data->data == NULL) {
691 DEBUG(0, ("Got NULL buffer\n"));
695 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
696 DEBUG(0, ("Got invalid msg len %u\n",
697 (unsigned int)data->length));
701 /* De-linearize incoming message. */
702 message_to_share_mode_entry(&msg, (char *)data->data);
704 DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
705 server_id_str(talloc_tos(), &src),
706 file_id_string_tos(&msg.id),
707 (unsigned long long)msg.share_file_id,
708 (unsigned long long)msg.op_mid));
710 schedule_deferred_open_message_smb(sconn, msg.op_mid);
713 static void process_open_retry_message(struct messaging_context *msg_ctx,
716 struct server_id src,
719 struct share_mode_entry msg;
720 struct smbd_server_connection *sconn =
721 talloc_get_type_abort(private_data,
722 struct smbd_server_connection);
724 if (data->data == NULL) {
725 DEBUG(0, ("Got NULL buffer\n"));
729 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
730 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
734 /* De-linearize incoming message. */
735 message_to_share_mode_entry(&msg, (char *)data->data);
737 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
738 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
739 (unsigned long long)msg.op_mid));
741 schedule_deferred_open_message_smb(sconn, msg.op_mid);
744 struct break_to_none_state {
745 struct smbd_server_connection *sconn;
748 static void do_break_to_none(struct tevent_req *req);
750 /****************************************************************************
751 This function is called on any file modification or lock request. If a file
752 is level 2 oplocked then it must tell all other level 2 holders to break to
754 ****************************************************************************/
756 static void contend_level2_oplocks_begin_default(files_struct *fsp,
757 enum level2_contention_type type)
759 struct smbd_server_connection *sconn = fsp->conn->sconn;
760 struct tevent_req *req;
761 struct break_to_none_state *state;
764 * If this file is level II oplocked then we need
765 * to grab the shared memory lock and inform all
766 * other files with a level II lock that they need
767 * to flush their read caches. We keep the lock over
768 * the shared memory area whilst doing this.
771 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
775 * When we get here we might have a brlock entry locked. Also
776 * locking the share mode entry would violate the locking
777 * order. Breaking level2 oplocks to none is asynchronous
778 * anyway, so we postpone this into an immediate timed event.
781 state = talloc(sconn, struct break_to_none_state);
783 DEBUG(1, ("talloc failed\n"));
786 state->sconn = sconn;
787 state->id = fsp->file_id;
789 req = tevent_wakeup_send(state, sconn->ev_ctx, timeval_set(0, 0));
791 DEBUG(1, ("tevent_wakeup_send failed\n"));
795 tevent_req_set_callback(req, do_break_to_none, state);
799 static void do_break_to_none(struct tevent_req *req)
801 struct break_to_none_state *state = tevent_req_callback_data(
802 req, struct break_to_none_state);
805 struct share_mode_lock *lck;
807 ret = tevent_wakeup_recv(req);
810 DEBUG(1, ("tevent_wakeup_recv failed\n"));
813 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
815 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
816 "share mode entry for file %s.\n",
817 file_id_string_tos(&state->id)));
821 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
822 lck->data->num_share_modes ));
824 for(i = 0; i < lck->data->num_share_modes; i++) {
825 struct share_mode_entry *share_entry = &lck->data->share_modes[i];
826 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
828 if (!is_valid_share_mode_entry(share_entry)) {
833 * As there could have been multiple writes waiting at the
834 * lock_share_entry gate we may not be the first to
835 * enter. Hence the state of the op_types in the share mode
836 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
837 * oplock. It will do no harm to re-send break messages to
838 * those smbd's that are still waiting their turn to remove
839 * their LEVEL_II state, and also no harm to ignore existing
840 * NO_OPLOCK states. JRA.
843 DEBUG(10,("release_level_2_oplocks_on_change: "
844 "share_entry[%i]->op_type == %d\n",
845 i, share_entry->op_type ));
847 if (share_entry->op_type == NO_OPLOCK) {
852 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
853 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
854 "share mode entry %d is an exlusive "
860 share_mode_entry_to_message(msg, share_entry);
863 * Deal with a race condition when breaking level2
864 * oplocks. Don't send all the messages and release
865 * the lock, this allows someone else to come in and
866 * get a level2 lock before any of the messages are
867 * processed, and thus miss getting a break message.
868 * Ensure at least one entry (the one we're breaking)
869 * is processed immediately under the lock and becomes
870 * set as NO_OPLOCK to stop any waiter getting a level2.
874 if (procid_is_me(&share_entry->pid)) {
875 struct files_struct *cur_fsp =
876 initial_break_processing(state->sconn,
878 share_entry->share_file_id);
879 wait_before_sending_break();
880 if (cur_fsp != NULL) {
881 break_level2_to_none_async(cur_fsp);
883 DEBUG(3, ("release_level_2_oplocks_on_change: "
884 "Did not find fsp, ignoring\n"));
887 messaging_send_buf(state->sconn->msg_ctx,
889 MSG_SMB_ASYNC_LEVEL2_BREAK,
891 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
895 /* We let the message receivers handle removing the oplock state
896 in the share mode lock db. */
904 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
905 enum level2_contention_type type)
907 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
908 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
912 contend_level2_oplocks_begin_default(fsp, type);
915 void smbd_contend_level2_oplocks_end(files_struct *fsp,
916 enum level2_contention_type type)
918 /* Only kernel oplocks implement this so far */
919 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
920 koplocks->ops->contend_level2_oplocks_end(fsp, type);
924 /****************************************************************************
925 Linearize a share mode entry struct to an internal oplock break message.
926 ****************************************************************************/
928 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
930 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
931 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
932 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
933 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
934 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
935 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
936 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
937 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
938 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
939 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
940 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
941 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
942 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
943 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
946 /****************************************************************************
947 De-linearize an internal oplock break message to a share mode entry struct.
948 ****************************************************************************/
950 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
952 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
953 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
954 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
955 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
956 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
957 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
958 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
959 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
960 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
961 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
962 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
963 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
964 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
965 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
968 /****************************************************************************
969 Setup oplocks for this process.
970 ****************************************************************************/
972 bool init_oplocks(struct smbd_server_connection *sconn)
974 DEBUG(3,("init_oplocks: initializing messages.\n"));
976 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
977 process_oplock_break_message);
978 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
979 process_oplock_async_level2_break_message);
980 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_RESPONSE,
981 process_oplock_break_response);
982 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
983 process_kernel_oplock_break);
984 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_OPEN_RETRY,
985 process_open_retry_message);
990 void init_kernel_oplocks(struct smbd_server_connection *sconn)
992 /* only initialize once */
993 if (koplocks == NULL) {
994 #if HAVE_KERNEL_OPLOCKS_IRIX
995 koplocks = irix_init_kernel_oplocks(sconn);
996 #elif HAVE_KERNEL_OPLOCKS_LINUX
997 koplocks = linux_init_kernel_oplocks(sconn);
999 #error Isilon, please check if the NULL context is okay here. Thanks!
1000 koplocks = onefs_init_kernel_oplocks(sconn);