s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / smbd / oplock.c
blob284b0321aa8102d07a55de831e1530339d7db15d
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/smbd.h"
25 #include "smbd/globals.h"
26 #include "messages.h"
28 /****************************************************************************
29 Get the number of current exclusive oplocks.
30 ****************************************************************************/
32 int32 get_number_of_exclusive_open_oplocks(void)
34 return exclusive_oplocks_open;
38 * helper function used by the kernel oplock backends to post the break message
40 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
42 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
44 /* Put the kernel break info into the message. */
45 push_file_id_24((char *)msg, &fsp->file_id);
46 SIVAL(msg,24,fsp->fh->gen_id);
48 /* Don't need to be root here as we're only ever
49 sending to ourselves. */
51 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
52 MSG_SMB_KERNEL_BREAK,
53 msg, MSG_SMB_KERNEL_BREAK_SIZE);
56 /****************************************************************************
57 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
58 disabled (just sets flags) and no byte-range locks in the file. Returns True
59 if oplock set.
60 ****************************************************************************/
62 bool set_file_oplock(files_struct *fsp, int oplock_type)
64 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
65 if (koplocks &&
66 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
67 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
68 "don't support them\n"));
69 return false;
73 if ((fsp->oplock_type != NO_OPLOCK) &&
74 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
75 koplocks &&
76 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
77 return False;
80 fsp->oplock_type = oplock_type;
81 fsp->sent_oplock_break = NO_BREAK_SENT;
82 if (oplock_type == LEVEL_II_OPLOCK) {
83 level_II_oplocks_open++;
84 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
85 exclusive_oplocks_open++;
88 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
89 "tv_sec = %x, tv_usec = %x\n",
90 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
91 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
92 (int)fsp->open_time.tv_usec ));
94 return True;
97 /****************************************************************************
98 Attempt to release an oplock on a file. Decrements oplock count.
99 ****************************************************************************/
101 void release_file_oplock(files_struct *fsp)
103 if ((fsp->oplock_type != NO_OPLOCK) &&
104 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
105 koplocks) {
106 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
109 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
110 level_II_oplocks_open--;
111 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
112 exclusive_oplocks_open--;
115 SMB_ASSERT(exclusive_oplocks_open>=0);
116 SMB_ASSERT(level_II_oplocks_open>=0);
118 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
119 /* This doesn't matter for close. */
120 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
121 } else {
122 fsp->oplock_type = NO_OPLOCK;
124 fsp->sent_oplock_break = NO_BREAK_SENT;
126 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
127 delete_write_cache(fsp);
129 TALLOC_FREE(fsp->oplock_timeout);
132 /****************************************************************************
133 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
134 ****************************************************************************/
136 static void downgrade_file_oplock(files_struct *fsp)
138 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
139 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
140 return;
143 if (koplocks) {
144 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
146 fsp->oplock_type = LEVEL_II_OPLOCK;
147 exclusive_oplocks_open--;
148 level_II_oplocks_open++;
149 fsp->sent_oplock_break = NO_BREAK_SENT;
152 /****************************************************************************
153 Remove a file oplock. Copes with level II and exclusive.
154 Locks then unlocks the share mode lock. Client can decide to go directly
155 to none even if a "break-to-level II" was sent.
156 ****************************************************************************/
158 bool remove_oplock(files_struct *fsp)
160 bool ret;
161 struct share_mode_lock *lck;
163 /* Remove the oplock flag from the sharemode. */
164 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
165 NULL);
166 if (lck == NULL) {
167 DEBUG(0,("remove_oplock: failed to lock share entry for "
168 "file %s\n", fsp_str_dbg(fsp)));
169 return False;
171 ret = remove_share_oplock(lck, fsp);
172 if (!ret) {
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);
179 TALLOC_FREE(lck);
180 return ret;
184 * Deal with a reply when a break-to-level II was sent.
186 bool downgrade_oplock(files_struct *fsp)
188 bool ret;
189 struct share_mode_lock *lck;
191 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
192 NULL);
193 if (lck == NULL) {
194 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
195 "file %s\n", fsp_str_dbg(fsp)));
196 return False;
198 ret = downgrade_share_oplock(lck, fsp);
199 if (!ret) {
200 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
201 "for file %s fnum %d, file_id %s\n",
202 fsp_str_dbg(fsp), fsp->fnum,
203 file_id_string_tos(&fsp->file_id)));
206 downgrade_file_oplock(fsp);
207 TALLOC_FREE(lck);
208 return ret;
212 * Some kernel oplock implementations handle the notification themselves.
214 bool should_notify_deferred_opens()
216 return !(koplocks &&
217 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
220 /****************************************************************************
221 Set up an oplock break message.
222 ****************************************************************************/
224 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
225 files_struct *fsp, int cmd)
227 char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
229 if (result == NULL) {
230 DEBUG(0, ("talloc failed\n"));
231 return NULL;
234 memset(result,'\0',smb_size);
235 srv_set_message(result,8,0,true);
236 SCVAL(result,smb_com,SMBlockingX);
237 SSVAL(result,smb_tid,fsp->conn->cnum);
238 SSVAL(result,smb_pid,0xFFFF);
239 SSVAL(result,smb_uid,0);
240 SSVAL(result,smb_mid,0xFFFF);
241 SCVAL(result,smb_vwv0,0xFF);
242 SSVAL(result,smb_vwv2,fsp->fnum);
243 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
244 SCVAL(result,smb_vwv3+1,cmd);
245 return result;
248 /****************************************************************************
249 Function to do the waiting before sending a local break.
250 ****************************************************************************/
252 static void wait_before_sending_break(void)
254 long wait_time = (long)lp_oplock_break_wait_time();
256 if (wait_time) {
257 smb_msleep(wait_time);
261 /****************************************************************************
262 Ensure that we have a valid oplock.
263 ****************************************************************************/
265 static files_struct *initial_break_processing(
266 struct smbd_server_connection *sconn, struct file_id id,
267 unsigned long file_id)
269 files_struct *fsp = NULL;
271 if( DEBUGLVL( 3 ) ) {
272 dbgtext( "initial_break_processing: called for %s/%u\n",
273 file_id_string_tos(&id), (int)file_id);
274 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
275 exclusive_oplocks_open, level_II_oplocks_open );
279 * We need to search the file open table for the
280 * entry containing this dev and inode, and ensure
281 * we have an oplock on it.
284 fsp = file_find_dif(sconn, id, file_id);
286 if(fsp == NULL) {
287 /* The file could have been closed in the meantime - return success. */
288 if( DEBUGLVL( 3 ) ) {
289 dbgtext( "initial_break_processing: cannot find open file with " );
290 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
291 dbgtext( "allowing break to succeed.\n" );
293 return NULL;
296 /* Ensure we have an oplock on the file */
299 * There is a potential race condition in that an oplock could
300 * have been broken due to another udp request, and yet there are
301 * still oplock break messages being sent in the udp message
302 * queue for this file. So return true if we don't have an oplock,
303 * as we may have just freed it.
306 if(fsp->oplock_type == NO_OPLOCK) {
307 if( DEBUGLVL( 3 ) ) {
308 dbgtext( "initial_break_processing: file %s ",
309 fsp_str_dbg(fsp));
310 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
311 file_id_string_tos(&id), fsp->fh->gen_id );
312 dbgtext( "Allowing break to succeed regardless.\n" );
314 return NULL;
317 return fsp;
320 static void oplock_timeout_handler(struct event_context *ctx,
321 struct timed_event *te,
322 struct timeval now,
323 void *private_data)
325 files_struct *fsp = (files_struct *)private_data;
327 /* Remove the timed event handler. */
328 TALLOC_FREE(fsp->oplock_timeout);
329 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
330 fsp_str_dbg(fsp)));
331 remove_oplock(fsp);
332 reply_to_oplock_break_requests(fsp);
335 /*******************************************************************
336 Add a timeout handler waiting for the client reply.
337 *******************************************************************/
339 static void add_oplock_timeout_handler(files_struct *fsp)
342 * If kernel oplocks already notifies smbds when an oplock break times
343 * out, just return.
345 if (koplocks &&
346 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
347 return;
350 if (fsp->oplock_timeout != NULL) {
351 DEBUG(0, ("Logic problem -- have an oplock event hanging "
352 "around\n"));
355 fsp->oplock_timeout =
356 event_add_timed(server_event_context(), fsp,
357 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
358 oplock_timeout_handler, fsp);
360 if (fsp->oplock_timeout == NULL) {
361 DEBUG(0, ("Could not add oplock timeout handler\n"));
365 static void send_break_message_smb1(files_struct *fsp, int level)
367 char *break_msg = new_break_message_smb1(talloc_tos(),
368 fsp,
369 level);
370 if (break_msg == NULL) {
371 exit_server("Could not talloc break_msg\n");
374 show_msg(break_msg);
375 if (!srv_send_smb(fsp->conn->sconn,
376 break_msg, false, 0,
377 IS_CONN_ENCRYPTED(fsp->conn),
378 NULL)) {
379 exit_server_cleanly("send_break_message_smb1: "
380 "srv_send_smb failed.");
383 TALLOC_FREE(break_msg);
386 void break_level2_to_none_async(files_struct *fsp)
388 struct smbd_server_connection *sconn = fsp->conn->sconn;
390 if (fsp->oplock_type == NO_OPLOCK) {
391 /* We already got a "break to none" message and we've handled
392 * it. just ignore. */
393 DEBUG(3, ("process_oplock_async_level2_break_message: already "
394 "broken to none, ignoring.\n"));
395 return;
398 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
399 /* Don't tell the client, just downgrade. */
400 DEBUG(3, ("process_oplock_async_level2_break_message: "
401 "downgrading fake level 2 oplock.\n"));
402 remove_oplock(fsp);
403 return;
406 /* Ensure we're really at level2 state. */
407 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
409 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
410 "to none message for fid %d, file %s\n", fsp->fnum,
411 fsp_str_dbg(fsp)));
413 /* Now send a break to none message to our client. */
414 if (sconn->using_smb2) {
415 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
416 } else {
417 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
420 /* Async level2 request, don't send a reply, just remove the oplock. */
421 remove_oplock(fsp);
424 /*******************************************************************
425 This handles the case of a write triggering a break to none
426 message on a level2 oplock.
427 When we get this message we may be in any of three states :
428 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
429 the client for LEVEL2.
430 *******************************************************************/
432 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
433 void *private_data,
434 uint32_t msg_type,
435 struct server_id src,
436 DATA_BLOB *data)
438 struct smbd_server_connection *sconn;
439 struct share_mode_entry msg;
440 files_struct *fsp;
442 if (data->data == NULL) {
443 DEBUG(0, ("Got NULL buffer\n"));
444 return;
447 sconn = msg_ctx_to_sconn(msg_ctx);
448 if (sconn == NULL) {
449 DEBUG(1, ("could not find sconn\n"));
450 return;
453 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
454 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
455 return;
458 /* De-linearize incoming message. */
459 message_to_share_mode_entry(&msg, (char *)data->data);
461 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
462 "%s/%lu\n", server_id_str(talloc_tos(), &src),
463 file_id_string_tos(&msg.id), msg.share_file_id));
465 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
467 if (fsp == NULL) {
468 /* We hit a race here. Break messages are sent, and before we
469 * get to process this message, we have closed the file.
470 * No need to reply as this is an async message. */
471 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
472 return;
475 break_level2_to_none_async(fsp);
478 /*******************************************************************
479 This handles the generic oplock break message from another smbd.
480 *******************************************************************/
482 static void process_oplock_break_message(struct messaging_context *msg_ctx,
483 void *private_data,
484 uint32_t msg_type,
485 struct server_id src,
486 DATA_BLOB *data)
488 struct smbd_server_connection *sconn;
489 struct share_mode_entry msg;
490 files_struct *fsp;
491 bool break_to_level2 = False;
493 if (data->data == NULL) {
494 DEBUG(0, ("Got NULL buffer\n"));
495 return;
498 sconn = msg_ctx_to_sconn(msg_ctx);
499 if (sconn == NULL) {
500 DEBUG(1, ("could not find sconn\n"));
501 return;
504 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
505 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
506 return;
509 /* De-linearize incoming message. */
510 message_to_share_mode_entry(&msg, (char *)data->data);
512 DEBUG(10, ("Got oplock break message from pid %s: %s/%lu\n",
513 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
514 msg.share_file_id));
516 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
518 if (fsp == NULL) {
519 /* We hit a race here. Break messages are sent, and before we
520 * get to process this message, we have closed the file. Reply
521 * with 'ok, oplock broken' */
522 DEBUG(3, ("Did not find fsp\n"));
524 /* We just send the same message back. */
525 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
526 (uint8 *)data->data,
527 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
528 return;
531 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
532 /* Remember we have to inform the requesting PID when the
533 * client replies */
534 msg.pid = src;
535 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
536 &fsp->pending_break_messages,
537 &fsp->num_pending_break_messages);
538 return;
541 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
542 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
543 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
544 file_id_string_tos(&fsp->file_id),
545 fsp_str_dbg(fsp)));
546 /* We just send the same message back. */
547 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
548 (uint8 *)data->data,
549 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
550 return;
553 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
554 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
555 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
556 lp_level2_oplocks(SNUM(fsp->conn))) {
557 break_to_level2 = True;
560 /* Need to wait before sending a break
561 message if we sent ourselves this message. */
562 if (procid_is_me(&src)) {
563 wait_before_sending_break();
566 if (sconn->using_smb2) {
567 send_break_message_smb2(fsp, break_to_level2 ?
568 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
569 } else {
570 send_break_message_smb1(fsp, break_to_level2 ?
571 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
574 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
576 msg.pid = src;
577 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
578 &fsp->pending_break_messages,
579 &fsp->num_pending_break_messages);
581 add_oplock_timeout_handler(fsp);
584 /*******************************************************************
585 This handles the kernel oplock break message.
586 *******************************************************************/
588 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
589 void *private_data,
590 uint32_t msg_type,
591 struct server_id src,
592 DATA_BLOB *data)
594 struct smbd_server_connection *sconn;
595 struct file_id id;
596 unsigned long file_id;
597 files_struct *fsp;
599 if (data->data == NULL) {
600 DEBUG(0, ("Got NULL buffer\n"));
601 return;
604 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
605 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
606 return;
609 sconn = msg_ctx_to_sconn(msg_ctx);
610 if (sconn == NULL) {
611 DEBUG(1, ("could not find sconn\n"));
612 return;
615 /* Pull the data from the message. */
616 pull_file_id_24((char *)data->data, &id);
617 file_id = (unsigned long)IVAL(data->data, 24);
619 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
620 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
621 (unsigned int)file_id));
623 fsp = initial_break_processing(sconn, id, file_id);
625 if (fsp == NULL) {
626 DEBUG(3, ("Got a kernel oplock break message for a file "
627 "I don't know about\n"));
628 return;
631 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
632 /* This is ok, kernel oplocks come in completely async */
633 DEBUG(3, ("Got a kernel oplock request while waiting for a "
634 "break reply\n"));
635 return;
638 if (sconn->using_smb2) {
639 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
640 } else {
641 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
644 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
646 add_oplock_timeout_handler(fsp);
649 void reply_to_oplock_break_requests(files_struct *fsp)
651 int i;
654 * If kernel oplocks already notifies smbds when oplocks are
655 * broken/removed, just return.
657 if (koplocks &&
658 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
659 return;
662 for (i=0; i<fsp->num_pending_break_messages; i++) {
663 struct share_mode_entry *e = &fsp->pending_break_messages[i];
664 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
666 share_mode_entry_to_message(msg, e);
668 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
669 MSG_SMB_BREAK_RESPONSE,
670 (uint8 *)msg,
671 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
674 SAFE_FREE(fsp->pending_break_messages);
675 fsp->num_pending_break_messages = 0;
676 if (fsp->oplock_timeout != NULL) {
677 /* Remove the timed event handler. */
678 TALLOC_FREE(fsp->oplock_timeout);
679 fsp->oplock_timeout = NULL;
681 return;
684 static void process_oplock_break_response(struct messaging_context *msg_ctx,
685 void *private_data,
686 uint32_t msg_type,
687 struct server_id src,
688 DATA_BLOB *data)
690 struct share_mode_entry msg;
692 if (data->data == NULL) {
693 DEBUG(0, ("Got NULL buffer\n"));
694 return;
697 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
698 DEBUG(0, ("Got invalid msg len %u\n",
699 (unsigned int)data->length));
700 return;
703 /* De-linearize incoming message. */
704 message_to_share_mode_entry(&msg, (char *)data->data);
706 DEBUG(10, ("Got oplock break response from pid %s: %s/%lu mid %llu\n",
707 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
708 msg.share_file_id, (unsigned long long)msg.op_mid));
710 schedule_deferred_open_message_smb(msg.op_mid);
713 static void process_open_retry_message(struct messaging_context *msg_ctx,
714 void *private_data,
715 uint32_t msg_type,
716 struct server_id src,
717 DATA_BLOB *data)
719 struct share_mode_entry msg;
721 if (data->data == NULL) {
722 DEBUG(0, ("Got NULL buffer\n"));
723 return;
726 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
727 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
728 return;
731 /* De-linearize incoming message. */
732 message_to_share_mode_entry(&msg, (char *)data->data);
734 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
735 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
736 (unsigned long long)msg.op_mid));
738 schedule_deferred_open_message_smb(msg.op_mid);
741 /****************************************************************************
742 This function is called on any file modification or lock request. If a file
743 is level 2 oplocked then it must tell all other level 2 holders to break to
744 none.
745 ****************************************************************************/
747 static void contend_level2_oplocks_begin_default(files_struct *fsp,
748 enum level2_contention_type type)
750 int i;
751 struct share_mode_lock *lck;
754 * If this file is level II oplocked then we need
755 * to grab the shared memory lock and inform all
756 * other files with a level II lock that they need
757 * to flush their read caches. We keep the lock over
758 * the shared memory area whilst doing this.
761 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
762 return;
764 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
765 NULL);
766 if (lck == NULL) {
767 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
768 "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
769 return;
772 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
773 lck->num_share_modes ));
775 for(i = 0; i < lck->num_share_modes; i++) {
776 struct share_mode_entry *share_entry = &lck->share_modes[i];
777 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
779 if (!is_valid_share_mode_entry(share_entry)) {
780 continue;
784 * As there could have been multiple writes waiting at the
785 * lock_share_entry gate we may not be the first to
786 * enter. Hence the state of the op_types in the share mode
787 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
788 * oplock. It will do no harm to re-send break messages to
789 * those smbd's that are still waiting their turn to remove
790 * their LEVEL_II state, and also no harm to ignore existing
791 * NO_OPLOCK states. JRA.
794 DEBUG(10,("release_level_2_oplocks_on_change: "
795 "share_entry[%i]->op_type == %d\n",
796 i, share_entry->op_type ));
798 if (share_entry->op_type == NO_OPLOCK) {
799 continue;
802 /* Paranoia .... */
803 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
804 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
805 "share mode entry %d is an exlusive "
806 "oplock !\n", i ));
807 TALLOC_FREE(lck);
808 abort();
811 share_mode_entry_to_message(msg, share_entry);
814 * Deal with a race condition when breaking level2
815 * oplocks. Don't send all the messages and release
816 * the lock, this allows someone else to come in and
817 * get a level2 lock before any of the messages are
818 * processed, and thus miss getting a break message.
819 * Ensure at least one entry (the one we're breaking)
820 * is processed immediately under the lock and becomes
821 * set as NO_OPLOCK to stop any waiter getting a level2.
822 * Bugid #5980.
825 if (procid_is_me(&share_entry->pid)) {
826 struct files_struct *cur_fsp =
827 initial_break_processing(fsp->conn->sconn,
828 share_entry->id,
829 share_entry->share_file_id);
830 wait_before_sending_break();
831 if (cur_fsp != NULL) {
832 break_level2_to_none_async(cur_fsp);
833 } else {
834 DEBUG(3, ("release_level_2_oplocks_on_change: "
835 "Did not find fsp, ignoring\n"));
837 } else {
838 messaging_send_buf(fsp->conn->sconn->msg_ctx,
839 share_entry->pid,
840 MSG_SMB_ASYNC_LEVEL2_BREAK,
841 (uint8 *)msg,
842 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
846 /* We let the message receivers handle removing the oplock state
847 in the share mode lock db. */
849 TALLOC_FREE(lck);
852 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
853 enum level2_contention_type type)
855 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
856 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
857 return;
860 contend_level2_oplocks_begin_default(fsp, type);
863 void smbd_contend_level2_oplocks_end(files_struct *fsp,
864 enum level2_contention_type type)
866 /* Only kernel oplocks implement this so far */
867 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
868 koplocks->ops->contend_level2_oplocks_end(fsp, type);
872 /****************************************************************************
873 Linearize a share mode entry struct to an internal oplock break message.
874 ****************************************************************************/
876 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
878 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
879 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
880 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
881 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
882 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
883 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
884 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
885 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
886 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
887 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
888 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
889 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
890 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
891 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
894 /****************************************************************************
895 De-linearize an internal oplock break message to a share mode entry struct.
896 ****************************************************************************/
898 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
900 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
901 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
902 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
903 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
904 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
905 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
906 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
907 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
908 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
909 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
910 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
911 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
912 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
913 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
916 /****************************************************************************
917 Setup oplocks for this process.
918 ****************************************************************************/
920 bool init_oplocks(struct messaging_context *msg_ctx)
922 DEBUG(3,("init_oplocks: initializing messages.\n"));
924 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
925 process_oplock_break_message);
926 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
927 process_oplock_async_level2_break_message);
928 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
929 process_oplock_break_response);
930 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
931 process_kernel_oplock_break);
932 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
933 process_open_retry_message);
935 if (lp_kernel_oplocks()) {
936 #if HAVE_KERNEL_OPLOCKS_IRIX
937 koplocks = irix_init_kernel_oplocks(NULL);
938 #elif HAVE_KERNEL_OPLOCKS_LINUX
939 koplocks = linux_init_kernel_oplocks(NULL);
940 #elif HAVE_ONEFS
941 #error Isilon, please check if the NULL context is okay here. Thanks!
942 koplocks = onefs_init_kernel_oplocks(NULL);
943 #endif
946 return True;