s3 oplocks: Differentiate between releasing an oplock vs. downgrading to Level 2...
[Samba.git] / source3 / smbd / oplock.c
blob0945ac66776a295218bb3ad7096665040c01381b
1 /*
2 Unix SMB/CIFS implementation.
3 oplock processing
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998 - 2001
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #define DBGC_CLASS DBGC_LOCKING
23 #include "includes.h"
24 #include "smbd/globals.h"
26 /****************************************************************************
27 Get the number of current exclusive oplocks.
28 ****************************************************************************/
30 int32 get_number_of_exclusive_open_oplocks(void)
32 return exclusive_oplocks_open;
36 * helper function used by the kernel oplock backends to post the break message
38 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
40 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
42 /* Put the kernel break info into the message. */
43 push_file_id_16((char *)msg, &fsp->file_id);
44 SIVAL(msg,16,fsp->fh->gen_id);
46 /* Don't need to be root here as we're only ever
47 sending to ourselves. */
49 messaging_send_buf(msg_ctx, procid_self(),
50 MSG_SMB_KERNEL_BREAK,
51 msg, MSG_SMB_KERNEL_BREAK_SIZE);
54 /****************************************************************************
55 Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
56 disabled (just sets flags). Returns True if oplock set.
57 ****************************************************************************/
59 bool set_file_oplock(files_struct *fsp, int oplock_type)
61 if ((fsp->oplock_type != NO_OPLOCK) &&
62 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
63 koplocks &&
64 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
65 return False;
68 fsp->oplock_type = oplock_type;
69 fsp->sent_oplock_break = NO_BREAK_SENT;
70 if (oplock_type == LEVEL_II_OPLOCK) {
71 level_II_oplocks_open++;
72 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
73 exclusive_oplocks_open++;
76 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
77 "tv_sec = %x, tv_usec = %x\n",
78 fsp->fsp_name, file_id_string_tos(&fsp->file_id),
79 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
80 (int)fsp->open_time.tv_usec ));
82 return True;
85 /****************************************************************************
86 Attempt to release an oplock on a file. Decrements oplock count.
87 ****************************************************************************/
89 void release_file_oplock(files_struct *fsp)
91 if ((fsp->oplock_type != NO_OPLOCK) &&
92 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
93 koplocks) {
94 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
97 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
98 level_II_oplocks_open--;
99 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
100 exclusive_oplocks_open--;
103 SMB_ASSERT(exclusive_oplocks_open>=0);
104 SMB_ASSERT(level_II_oplocks_open>=0);
106 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
107 /* This doesn't matter for close. */
108 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
109 } else {
110 fsp->oplock_type = NO_OPLOCK;
112 fsp->sent_oplock_break = NO_BREAK_SENT;
114 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
116 TALLOC_FREE(fsp->oplock_timeout);
119 /****************************************************************************
120 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
121 ****************************************************************************/
123 static void downgrade_file_oplock(files_struct *fsp)
125 if (koplocks) {
126 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
128 fsp->oplock_type = LEVEL_II_OPLOCK;
129 exclusive_oplocks_open--;
130 level_II_oplocks_open++;
131 fsp->sent_oplock_break = NO_BREAK_SENT;
134 /****************************************************************************
135 Remove a file oplock. Copes with level II and exclusive.
136 Locks then unlocks the share mode lock. Client can decide to go directly
137 to none even if a "break-to-level II" was sent.
138 ****************************************************************************/
140 bool remove_oplock(files_struct *fsp)
142 bool ret;
143 struct share_mode_lock *lck;
145 /* Remove the oplock flag from the sharemode. */
146 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
147 NULL);
148 if (lck == NULL) {
149 DEBUG(0,("remove_oplock: failed to lock share entry for "
150 "file %s\n", fsp->fsp_name ));
151 return False;
153 ret = remove_share_oplock(lck, fsp);
154 if (!ret) {
155 DEBUG(0,("remove_oplock: failed to remove share oplock for "
156 "file %s fnum %d, %s\n",
157 fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
159 release_file_oplock(fsp);
160 TALLOC_FREE(lck);
161 return ret;
165 * Deal with a reply when a break-to-level II was sent.
167 bool downgrade_oplock(files_struct *fsp)
169 bool ret;
170 struct share_mode_lock *lck;
172 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
173 NULL);
174 if (lck == NULL) {
175 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
176 "file %s\n", fsp->fsp_name ));
177 return False;
179 ret = downgrade_share_oplock(lck, fsp);
180 if (!ret) {
181 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
182 "for file %s fnum %d, file_id %s\n",
183 fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
186 downgrade_file_oplock(fsp);
187 TALLOC_FREE(lck);
188 return ret;
191 /****************************************************************************
192 Set up an oplock break message.
193 ****************************************************************************/
195 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
196 files_struct *fsp, uint8 cmd)
198 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
200 if (result == NULL) {
201 DEBUG(0, ("talloc failed\n"));
202 return NULL;
205 memset(result,'\0',smb_size);
206 srv_set_message(result,8,0,true);
207 SCVAL(result,smb_com,SMBlockingX);
208 SSVAL(result,smb_tid,fsp->conn->cnum);
209 SSVAL(result,smb_pid,0xFFFF);
210 SSVAL(result,smb_uid,0);
211 SSVAL(result,smb_mid,0xFFFF);
212 SCVAL(result,smb_vwv0,0xFF);
213 SSVAL(result,smb_vwv2,fsp->fnum);
214 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
215 SCVAL(result,smb_vwv3+1,cmd);
216 return result;
219 /****************************************************************************
220 Function to do the waiting before sending a local break.
221 ****************************************************************************/
223 static void wait_before_sending_break(void)
225 long wait_time = (long)lp_oplock_break_wait_time();
227 if (wait_time) {
228 smb_msleep(wait_time);
232 /****************************************************************************
233 Ensure that we have a valid oplock.
234 ****************************************************************************/
236 static files_struct *initial_break_processing(struct file_id id, unsigned long file_id)
238 files_struct *fsp = NULL;
240 if( DEBUGLVL( 3 ) ) {
241 dbgtext( "initial_break_processing: called for %s/%u\n",
242 file_id_string_tos(&id), (int)file_id);
243 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
244 exclusive_oplocks_open, level_II_oplocks_open );
248 * We need to search the file open table for the
249 * entry containing this dev and inode, and ensure
250 * we have an oplock on it.
253 fsp = file_find_dif(id, file_id);
255 if(fsp == NULL) {
256 /* The file could have been closed in the meantime - return success. */
257 if( DEBUGLVL( 3 ) ) {
258 dbgtext( "initial_break_processing: cannot find open file with " );
259 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
260 dbgtext( "allowing break to succeed.\n" );
262 return NULL;
265 /* Ensure we have an oplock on the file */
268 * There is a potential race condition in that an oplock could
269 * have been broken due to another udp request, and yet there are
270 * still oplock break messages being sent in the udp message
271 * queue for this file. So return true if we don't have an oplock,
272 * as we may have just freed it.
275 if(fsp->oplock_type == NO_OPLOCK) {
276 if( DEBUGLVL( 3 ) ) {
277 dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
278 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
279 file_id_string_tos(&id), fsp->fh->gen_id );
280 dbgtext( "Allowing break to succeed regardless.\n" );
282 return NULL;
285 return fsp;
288 static void oplock_timeout_handler(struct event_context *ctx,
289 struct timed_event *te,
290 struct timeval now,
291 void *private_data)
293 files_struct *fsp = (files_struct *)private_data;
295 /* Remove the timed event handler. */
296 TALLOC_FREE(fsp->oplock_timeout);
297 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
298 global_client_failed_oplock_break = True;
299 remove_oplock(fsp);
300 reply_to_oplock_break_requests(fsp);
303 /*******************************************************************
304 Add a timeout handler waiting for the client reply.
305 *******************************************************************/
307 static void add_oplock_timeout_handler(files_struct *fsp)
309 if (fsp->oplock_timeout != NULL) {
310 DEBUG(0, ("Logic problem -- have an oplock event hanging "
311 "around\n"));
314 fsp->oplock_timeout =
315 event_add_timed(smbd_event_context(), NULL,
316 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
317 oplock_timeout_handler, fsp);
319 if (fsp->oplock_timeout == NULL) {
320 DEBUG(0, ("Could not add oplock timeout handler\n"));
324 /*******************************************************************
325 This handles the case of a write triggering a break to none
326 message on a level2 oplock.
327 When we get this message we may be in any of three states :
328 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
329 the client for LEVEL2.
330 *******************************************************************/
332 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
333 void *private_data,
334 uint32_t msg_type,
335 struct server_id src,
336 DATA_BLOB *data)
338 struct share_mode_entry msg;
339 files_struct *fsp;
340 char *break_msg;
341 bool sign_state;
343 if (data->data == NULL) {
344 DEBUG(0, ("Got NULL buffer\n"));
345 return;
348 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
349 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
350 return;
353 /* De-linearize incoming message. */
354 message_to_share_mode_entry(&msg, (char *)data->data);
356 DEBUG(10, ("Got oplock async level 2 break message from pid %d: %s/%lu\n",
357 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
359 fsp = initial_break_processing(msg.id, msg.share_file_id);
361 if (fsp == NULL) {
362 /* We hit a race here. Break messages are sent, and before we
363 * get to process this message, we have closed the file.
364 * No need to reply as this is an async message. */
365 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
366 return;
369 if (fsp->oplock_type == NO_OPLOCK) {
370 /* We already got a "break to none" message and we've handled it.
371 * just ignore. */
372 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
373 return;
376 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
377 /* Don't tell the client, just downgrade. */
378 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
379 remove_oplock(fsp);
380 return;
383 /* Ensure we're really at level2 state. */
384 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
386 DEBUG(10,("process_oplock_async_level2_break_message: sending break to "
387 "none message for fid %d, file %s\n",
388 fsp->fnum,
389 fsp->fsp_name));
391 /* Now send a break to none message to our client. */
393 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
394 if (break_msg == NULL) {
395 exit_server("Could not talloc break_msg\n");
398 /* Need to wait before sending a break message if we sent ourselves this message. */
399 if (procid_to_pid(&src) == sys_getpid()) {
400 wait_before_sending_break();
403 /* Save the server smb signing state. */
404 sign_state = srv_oplock_set_signing(False);
406 show_msg(break_msg);
407 if (!srv_send_smb(smbd_server_fd(),
408 break_msg,
409 IS_CONN_ENCRYPTED(fsp->conn),
410 NULL)) {
411 exit_server_cleanly("oplock_break: srv_send_smb failed.");
414 /* Restore the sign state to what it was. */
415 srv_oplock_set_signing(sign_state);
417 TALLOC_FREE(break_msg);
419 /* Async level2 request, don't send a reply, just remove the oplock. */
420 remove_oplock(fsp);
423 /*******************************************************************
424 This handles the generic oplock break message from another smbd.
425 *******************************************************************/
427 static void process_oplock_break_message(struct messaging_context *msg_ctx,
428 void *private_data,
429 uint32_t msg_type,
430 struct server_id src,
431 DATA_BLOB *data)
433 struct share_mode_entry msg;
434 files_struct *fsp;
435 char *break_msg;
436 bool break_to_level2 = False;
437 bool sign_state;
439 if (data->data == NULL) {
440 DEBUG(0, ("Got NULL buffer\n"));
441 return;
444 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
445 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
446 return;
449 /* De-linearize incoming message. */
450 message_to_share_mode_entry(&msg, (char *)data->data);
452 DEBUG(10, ("Got oplock break message from pid %d: %s/%lu\n",
453 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
455 fsp = initial_break_processing(msg.id, msg.share_file_id);
457 if (fsp == NULL) {
458 /* a We hit race here. Break messages are sent, and before we
459 * get to process this message, we have closed the file. Reply
460 * with 'ok, oplock broken' */
461 DEBUG(3, ("Did not find fsp\n"));
463 /* We just send the same message back. */
464 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
465 (uint8 *)data->data,
466 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
467 return;
470 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
471 /* Remember we have to inform the requesting PID when the
472 * client replies */
473 msg.pid = src;
474 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
475 &fsp->pending_break_messages,
476 &fsp->num_pending_break_messages);
477 return;
480 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
481 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
482 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
483 file_id_string_tos(&fsp->file_id),
484 fsp->fsp_name));
485 /* We just send the same message back. */
486 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
487 (uint8 *)data->data,
488 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
489 return;
492 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
493 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
494 !koplocks && /* NOTE: we force levelII off for kernel oplocks -
495 * this will change when it is supported */
496 lp_level2_oplocks(SNUM(fsp->conn))) {
497 break_to_level2 = True;
500 break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
501 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
502 if (break_msg == NULL) {
503 exit_server("Could not talloc break_msg\n");
506 /* Need to wait before sending a break message if we sent ourselves this message. */
507 if (procid_to_pid(&src) == sys_getpid()) {
508 wait_before_sending_break();
511 /* Save the server smb signing state. */
512 sign_state = srv_oplock_set_signing(False);
514 show_msg(break_msg);
515 if (!srv_send_smb(smbd_server_fd(),
516 break_msg,
517 IS_CONN_ENCRYPTED(fsp->conn),
518 NULL)) {
519 exit_server_cleanly("oplock_break: srv_send_smb failed.");
522 /* Restore the sign state to what it was. */
523 srv_oplock_set_signing(sign_state);
525 TALLOC_FREE(break_msg);
527 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
529 msg.pid = src;
530 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
531 &fsp->pending_break_messages,
532 &fsp->num_pending_break_messages);
534 add_oplock_timeout_handler(fsp);
537 /*******************************************************************
538 This handles the kernel oplock break message.
539 *******************************************************************/
541 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
542 void *private_data,
543 uint32_t msg_type,
544 struct server_id src,
545 DATA_BLOB *data)
547 struct file_id id;
548 unsigned long file_id;
549 files_struct *fsp;
550 char *break_msg;
551 bool sign_state;
553 if (data->data == NULL) {
554 DEBUG(0, ("Got NULL buffer\n"));
555 return;
558 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
559 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
560 return;
563 /* Pull the data from the message. */
564 pull_file_id_16((char *)data->data, &id);
565 file_id = (unsigned long)IVAL(data->data, 16);
567 DEBUG(10, ("Got kernel oplock break message from pid %d: %s/%u\n",
568 (int)procid_to_pid(&src), file_id_string_tos(&id),
569 (unsigned int)file_id));
571 fsp = initial_break_processing(id, file_id);
573 if (fsp == NULL) {
574 DEBUG(3, ("Got a kernel oplock break message for a file "
575 "I don't know about\n"));
576 return;
579 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
580 /* This is ok, kernel oplocks come in completely async */
581 DEBUG(3, ("Got a kernel oplock request while waiting for a "
582 "break reply\n"));
583 return;
586 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
587 if (break_msg == NULL) {
588 exit_server("Could not talloc break_msg\n");
591 /* Save the server smb signing state. */
592 sign_state = srv_oplock_set_signing(False);
594 show_msg(break_msg);
595 if (!srv_send_smb(smbd_server_fd(),
596 break_msg,
597 IS_CONN_ENCRYPTED(fsp->conn),
598 NULL)) {
599 exit_server_cleanly("oplock_break: srv_send_smb failed.");
602 /* Restore the sign state to what it was. */
603 srv_oplock_set_signing(sign_state);
605 TALLOC_FREE(break_msg);
607 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
609 add_oplock_timeout_handler(fsp);
612 void reply_to_oplock_break_requests(files_struct *fsp)
614 int i;
616 for (i=0; i<fsp->num_pending_break_messages; i++) {
617 struct share_mode_entry *e = &fsp->pending_break_messages[i];
618 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
620 share_mode_entry_to_message(msg, e);
622 messaging_send_buf(smbd_messaging_context(), e->pid,
623 MSG_SMB_BREAK_RESPONSE,
624 (uint8 *)msg,
625 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
628 SAFE_FREE(fsp->pending_break_messages);
629 fsp->num_pending_break_messages = 0;
630 if (fsp->oplock_timeout != NULL) {
631 /* Remove the timed event handler. */
632 TALLOC_FREE(fsp->oplock_timeout);
633 fsp->oplock_timeout = NULL;
635 return;
638 static void process_oplock_break_response(struct messaging_context *msg_ctx,
639 void *private_data,
640 uint32_t msg_type,
641 struct server_id src,
642 DATA_BLOB *data)
644 struct share_mode_entry msg;
646 if (data->data == NULL) {
647 DEBUG(0, ("Got NULL buffer\n"));
648 return;
651 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
652 DEBUG(0, ("Got invalid msg len %u\n",
653 (unsigned int)data->length));
654 return;
657 /* De-linearize incoming message. */
658 message_to_share_mode_entry(&msg, (char *)data->data);
660 DEBUG(10, ("Got oplock break response from pid %d: %s/%lu mid %u\n",
661 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id,
662 (unsigned int)msg.op_mid));
664 /* Here's the hack from open.c, store the mid in the 'port' field */
665 schedule_deferred_open_smb_message(msg.op_mid);
668 static void process_open_retry_message(struct messaging_context *msg_ctx,
669 void *private_data,
670 uint32_t msg_type,
671 struct server_id src,
672 DATA_BLOB *data)
674 struct share_mode_entry msg;
676 if (data->data == NULL) {
677 DEBUG(0, ("Got NULL buffer\n"));
678 return;
681 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
682 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
683 return;
686 /* De-linearize incoming message. */
687 message_to_share_mode_entry(&msg, (char *)data->data);
689 DEBUG(10, ("Got open retry msg from pid %d: %s mid %u\n",
690 (int)procid_to_pid(&src), file_id_string_tos(&msg.id),
691 (unsigned int)msg.op_mid));
693 schedule_deferred_open_smb_message(msg.op_mid);
696 /****************************************************************************
697 This function is called on any file modification or lock request. If a file
698 is level 2 oplocked then it must tell all other level 2 holders to break to
699 none.
700 ****************************************************************************/
702 void release_level_2_oplocks_on_change(files_struct *fsp)
704 int i;
705 struct share_mode_lock *lck;
708 * If this file is level II oplocked then we need
709 * to grab the shared memory lock and inform all
710 * other files with a level II lock that they need
711 * to flush their read caches. We keep the lock over
712 * the shared memory area whilst doing this.
715 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
716 return;
718 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
719 NULL);
720 if (lck == NULL) {
721 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
722 "share mode entry for file %s.\n", fsp->fsp_name ));
723 return;
726 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
727 lck->num_share_modes ));
729 for(i = 0; i < lck->num_share_modes; i++) {
730 struct share_mode_entry *share_entry = &lck->share_modes[i];
731 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
733 if (!is_valid_share_mode_entry(share_entry)) {
734 continue;
738 * As there could have been multiple writes waiting at the
739 * lock_share_entry gate we may not be the first to
740 * enter. Hence the state of the op_types in the share mode
741 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
742 * oplock. It will do no harm to re-send break messages to
743 * those smbd's that are still waiting their turn to remove
744 * their LEVEL_II state, and also no harm to ignore existing
745 * NO_OPLOCK states. JRA.
748 DEBUG(10,("release_level_2_oplocks_on_change: "
749 "share_entry[%i]->op_type == %d\n",
750 i, share_entry->op_type ));
752 if (share_entry->op_type == NO_OPLOCK) {
753 continue;
756 /* Paranoia .... */
757 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
758 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
759 "share mode entry %d is an exlusive "
760 "oplock !\n", i ));
761 TALLOC_FREE(lck);
762 abort();
765 share_mode_entry_to_message(msg, share_entry);
768 * Deal with a race condition when breaking level2
769 * oplocks. Don't send all the messages and release
770 * the lock, this allows someone else to come in and
771 * get a level2 lock before any of the messages are
772 * processed, and thus miss getting a break message.
773 * Ensure at least one entry (the one we're breaking)
774 * is processed immediately under the lock and becomes
775 * set as NO_OPLOCK to stop any waiter getting a level2.
776 * Bugid #5980.
779 if (procid_is_me(&share_entry->pid)) {
780 DATA_BLOB blob = data_blob_const(msg,
781 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
782 process_oplock_async_level2_break_message(smbd_messaging_context(),
783 NULL,
784 MSG_SMB_ASYNC_LEVEL2_BREAK,
785 share_entry->pid,
786 &blob);
787 } else {
788 messaging_send_buf(smbd_messaging_context(),
789 share_entry->pid,
790 MSG_SMB_ASYNC_LEVEL2_BREAK,
791 (uint8 *)msg,
792 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
796 /* We let the message receivers handle removing the oplock state
797 in the share mode lock db. */
799 TALLOC_FREE(lck);
802 /****************************************************************************
803 Linearize a share mode entry struct to an internal oplock break message.
804 ****************************************************************************/
806 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
808 SIVAL(msg,0,(uint32)e->pid.pid);
809 SSVAL(msg,4,e->op_mid);
810 SSVAL(msg,6,e->op_type);
811 SIVAL(msg,8,e->access_mask);
812 SIVAL(msg,12,e->share_access);
813 SIVAL(msg,16,e->private_options);
814 SIVAL(msg,20,(uint32)e->time.tv_sec);
815 SIVAL(msg,24,(uint32)e->time.tv_usec);
816 push_file_id_16(msg+28, &e->id);
817 SIVAL(msg,44,e->share_file_id);
818 SIVAL(msg,48,e->uid);
819 SSVAL(msg,52,e->flags);
820 #ifdef CLUSTER_SUPPORT
821 SIVAL(msg,54,e->pid.vnn);
822 #endif
825 /****************************************************************************
826 De-linearize an internal oplock break message to a share mode entry struct.
827 ****************************************************************************/
829 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
831 e->pid.pid = (pid_t)IVAL(msg,0);
832 e->op_mid = SVAL(msg,4);
833 e->op_type = SVAL(msg,6);
834 e->access_mask = IVAL(msg,8);
835 e->share_access = IVAL(msg,12);
836 e->private_options = IVAL(msg,16);
837 e->time.tv_sec = (time_t)IVAL(msg,20);
838 e->time.tv_usec = (int)IVAL(msg,24);
839 pull_file_id_16(msg+28, &e->id);
840 e->share_file_id = (unsigned long)IVAL(msg,44);
841 e->uid = (uint32)IVAL(msg,48);
842 e->flags = (uint16)SVAL(msg,52);
843 #ifdef CLUSTER_SUPPORT
844 e->pid.vnn = IVAL(msg,54);
845 #endif
848 /****************************************************************************
849 Setup oplocks for this process.
850 ****************************************************************************/
852 bool init_oplocks(struct messaging_context *msg_ctx)
854 DEBUG(3,("init_oplocks: initializing messages.\n"));
856 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
857 process_oplock_break_message);
858 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
859 process_oplock_async_level2_break_message);
860 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
861 process_oplock_break_response);
862 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
863 process_kernel_oplock_break);
864 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
865 process_open_retry_message);
867 if (lp_kernel_oplocks()) {
868 #if HAVE_KERNEL_OPLOCKS_IRIX
869 koplocks = irix_init_kernel_oplocks(talloc_autofree_context());
870 #elif HAVE_KERNEL_OPLOCKS_LINUX
871 koplocks = linux_init_kernel_oplocks(talloc_autofree_context());
872 #endif
875 return True;