s3:smbd/oplock: pass smbd_server_connection to linux_init_kernel_oplocks()
[Samba/vl.git] / source3 / smbd / oplock.c
blobfd02fbf60ae9c358706886732110c156abf1251a
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"
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),
53 MSG_SMB_KERNEL_BREAK,
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
60 if oplock set.
61 ****************************************************************************/
63 bool set_file_oplock(files_struct *fsp, int oplock_type)
65 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
66 if (koplocks &&
67 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
68 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
69 "don't support them\n"));
70 return false;
74 if ((fsp->oplock_type != NO_OPLOCK) &&
75 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
76 koplocks &&
77 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
78 return False;
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 ));
95 return True;
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) &&
106 koplocks) {
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;
122 } else {
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"));
141 return;
144 if (koplocks) {
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)
161 bool ret;
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, NULL, NULL,
166 NULL);
167 if (lck == NULL) {
168 DEBUG(0,("remove_oplock: failed to lock share entry for "
169 "file %s\n", fsp_str_dbg(fsp)));
170 return False;
172 ret = remove_share_oplock(lck, fsp);
173 if (!ret) {
174 DEBUG(0,("remove_oplock: failed to remove share oplock for "
175 "file %s fnum %d, %s\n",
176 fsp_str_dbg(fsp), fsp->fnum,
177 file_id_string_tos(&fsp->file_id)));
179 release_file_oplock(fsp);
180 TALLOC_FREE(lck);
181 return ret;
185 * Deal with a reply when a break-to-level II was sent.
187 bool downgrade_oplock(files_struct *fsp)
189 bool ret;
190 struct share_mode_lock *lck;
192 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
193 NULL);
194 if (lck == NULL) {
195 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
196 "file %s\n", fsp_str_dbg(fsp)));
197 return False;
199 ret = downgrade_share_oplock(lck, fsp);
200 if (!ret) {
201 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
202 "for file %s fnum %d, file_id %s\n",
203 fsp_str_dbg(fsp), fsp->fnum,
204 file_id_string_tos(&fsp->file_id)));
207 downgrade_file_oplock(fsp);
208 TALLOC_FREE(lck);
209 return ret;
213 * Some kernel oplock implementations handle the notification themselves.
215 bool should_notify_deferred_opens()
217 return !(koplocks &&
218 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
221 /****************************************************************************
222 Set up an oplock break message.
223 ****************************************************************************/
225 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
226 files_struct *fsp, int cmd)
228 char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
230 if (result == NULL) {
231 DEBUG(0, ("talloc failed\n"));
232 return NULL;
235 memset(result,'\0',smb_size);
236 srv_set_message(result,8,0,true);
237 SCVAL(result,smb_com,SMBlockingX);
238 SSVAL(result,smb_tid,fsp->conn->cnum);
239 SSVAL(result,smb_pid,0xFFFF);
240 SSVAL(result,smb_uid,0);
241 SSVAL(result,smb_mid,0xFFFF);
242 SCVAL(result,smb_vwv0,0xFF);
243 SSVAL(result,smb_vwv2,fsp->fnum);
244 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
245 SCVAL(result,smb_vwv3+1,cmd);
246 return result;
249 /****************************************************************************
250 Function to do the waiting before sending a local break.
251 ****************************************************************************/
253 static void wait_before_sending_break(void)
255 long wait_time = (long)lp_oplock_break_wait_time();
257 if (wait_time) {
258 smb_msleep(wait_time);
262 /****************************************************************************
263 Ensure that we have a valid oplock.
264 ****************************************************************************/
266 static files_struct *initial_break_processing(
267 struct smbd_server_connection *sconn, struct file_id id,
268 unsigned long file_id)
270 files_struct *fsp = NULL;
272 if( DEBUGLVL( 3 ) ) {
273 dbgtext( "initial_break_processing: called for %s/%u\n",
274 file_id_string_tos(&id), (int)file_id);
275 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
276 exclusive_oplocks_open, level_II_oplocks_open );
280 * We need to search the file open table for the
281 * entry containing this dev and inode, and ensure
282 * we have an oplock on it.
285 fsp = file_find_dif(sconn, id, file_id);
287 if(fsp == NULL) {
288 /* The file could have been closed in the meantime - return success. */
289 if( DEBUGLVL( 3 ) ) {
290 dbgtext( "initial_break_processing: cannot find open file with " );
291 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
292 dbgtext( "allowing break to succeed.\n" );
294 return NULL;
297 /* Ensure we have an oplock on the file */
300 * There is a potential race condition in that an oplock could
301 * have been broken due to another udp request, and yet there are
302 * still oplock break messages being sent in the udp message
303 * queue for this file. So return true if we don't have an oplock,
304 * as we may have just freed it.
307 if(fsp->oplock_type == NO_OPLOCK) {
308 if( DEBUGLVL( 3 ) ) {
309 dbgtext( "initial_break_processing: file %s ",
310 fsp_str_dbg(fsp));
311 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
312 file_id_string_tos(&id), fsp->fh->gen_id );
313 dbgtext( "Allowing break to succeed regardless.\n" );
315 return NULL;
318 return fsp;
321 static void oplock_timeout_handler(struct event_context *ctx,
322 struct timed_event *te,
323 struct timeval now,
324 void *private_data)
326 files_struct *fsp = (files_struct *)private_data;
328 /* Remove the timed event handler. */
329 TALLOC_FREE(fsp->oplock_timeout);
330 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
331 fsp_str_dbg(fsp)));
332 remove_oplock(fsp);
333 reply_to_oplock_break_requests(fsp);
336 /*******************************************************************
337 Add a timeout handler waiting for the client reply.
338 *******************************************************************/
340 static void add_oplock_timeout_handler(files_struct *fsp)
343 * If kernel oplocks already notifies smbds when an oplock break times
344 * out, just return.
346 if (koplocks &&
347 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
348 return;
351 if (fsp->oplock_timeout != NULL) {
352 DEBUG(0, ("Logic problem -- have an oplock event hanging "
353 "around\n"));
356 fsp->oplock_timeout =
357 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
358 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
359 oplock_timeout_handler, fsp);
361 if (fsp->oplock_timeout == NULL) {
362 DEBUG(0, ("Could not add oplock timeout handler\n"));
366 static void send_break_message_smb1(files_struct *fsp, int level)
368 char *break_msg = new_break_message_smb1(talloc_tos(),
369 fsp,
370 level);
371 if (break_msg == NULL) {
372 exit_server("Could not talloc break_msg\n");
375 show_msg(break_msg);
376 if (!srv_send_smb(fsp->conn->sconn,
377 break_msg, false, 0,
378 IS_CONN_ENCRYPTED(fsp->conn),
379 NULL)) {
380 exit_server_cleanly("send_break_message_smb1: "
381 "srv_send_smb failed.");
384 TALLOC_FREE(break_msg);
387 void break_level2_to_none_async(files_struct *fsp)
389 struct smbd_server_connection *sconn = fsp->conn->sconn;
391 if (fsp->oplock_type == NO_OPLOCK) {
392 /* We already got a "break to none" message and we've handled
393 * it. just ignore. */
394 DEBUG(3, ("process_oplock_async_level2_break_message: already "
395 "broken to none, ignoring.\n"));
396 return;
399 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
400 /* Don't tell the client, just downgrade. */
401 DEBUG(3, ("process_oplock_async_level2_break_message: "
402 "downgrading fake level 2 oplock.\n"));
403 remove_oplock(fsp);
404 return;
407 /* Ensure we're really at level2 state. */
408 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
410 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
411 "to none message for fid %d, file %s\n", fsp->fnum,
412 fsp_str_dbg(fsp)));
414 /* Now send a break to none message to our client. */
415 if (sconn->using_smb2) {
416 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
417 } else {
418 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
421 /* Async level2 request, don't send a reply, just remove the oplock. */
422 remove_oplock(fsp);
425 /*******************************************************************
426 This handles the case of a write triggering a break to none
427 message on a level2 oplock.
428 When we get this message we may be in any of three states :
429 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
430 the client for LEVEL2.
431 *******************************************************************/
433 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
434 void *private_data,
435 uint32_t msg_type,
436 struct server_id src,
437 DATA_BLOB *data)
439 struct share_mode_entry msg;
440 files_struct *fsp;
441 struct smbd_server_connection *sconn =
442 talloc_get_type(private_data,
443 struct smbd_server_connection);
445 if (sconn == NULL) {
446 return;
449 if (data->data == NULL) {
450 DEBUG(0, ("Got NULL buffer\n"));
451 return;
454 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
455 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
456 return;
459 /* De-linearize incoming message. */
460 message_to_share_mode_entry(&msg, (char *)data->data);
462 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
463 "%s/%llu\n", server_id_str(talloc_tos(), &src),
464 file_id_string_tos(&msg.id),
465 (unsigned long long)msg.share_file_id));
467 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
469 if (fsp == NULL) {
470 /* We hit a race here. Break messages are sent, and before we
471 * get to process this message, we have closed the file.
472 * No need to reply as this is an async message. */
473 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
474 return;
477 break_level2_to_none_async(fsp);
480 /*******************************************************************
481 This handles the generic oplock break message from another smbd.
482 *******************************************************************/
484 static void process_oplock_break_message(struct messaging_context *msg_ctx,
485 void *private_data,
486 uint32_t msg_type,
487 struct server_id src,
488 DATA_BLOB *data)
490 struct share_mode_entry msg;
491 files_struct *fsp;
492 bool break_to_level2 = False;
493 struct smbd_server_connection *sconn =
494 talloc_get_type(private_data,
495 struct smbd_server_connection);
497 if (sconn == NULL) {
498 return;
501 if (data->data == NULL) {
502 DEBUG(0, ("Got NULL buffer\n"));
503 return;
506 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
507 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
508 return;
511 /* De-linearize incoming message. */
512 message_to_share_mode_entry(&msg, (char *)data->data);
514 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
515 server_id_str(talloc_tos(), &src),
516 file_id_string_tos(&msg.id),
517 (unsigned long long)msg.share_file_id));
519 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
521 if (fsp == NULL) {
522 /* We hit a race here. Break messages are sent, and before we
523 * get to process this message, we have closed the file. Reply
524 * with 'ok, oplock broken' */
525 DEBUG(3, ("Did not find fsp\n"));
527 /* We just send the same message back. */
528 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
529 (uint8 *)data->data,
530 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
531 return;
534 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
535 /* Remember we have to inform the requesting PID when the
536 * client replies */
537 msg.pid = src;
538 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
539 &fsp->pending_break_messages,
540 &fsp->num_pending_break_messages);
541 return;
544 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
545 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
546 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
547 file_id_string_tos(&fsp->file_id),
548 fsp_str_dbg(fsp)));
549 /* We just send the same message back. */
550 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
551 (uint8 *)data->data,
552 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
553 return;
556 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
557 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
558 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
559 lp_level2_oplocks(SNUM(fsp->conn))) {
560 break_to_level2 = True;
563 /* Need to wait before sending a break
564 message if we sent ourselves this message. */
565 if (procid_is_me(&src)) {
566 wait_before_sending_break();
569 if (sconn->using_smb2) {
570 send_break_message_smb2(fsp, break_to_level2 ?
571 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
572 } else {
573 send_break_message_smb1(fsp, break_to_level2 ?
574 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
577 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
579 msg.pid = src;
580 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
581 &fsp->pending_break_messages,
582 &fsp->num_pending_break_messages);
584 add_oplock_timeout_handler(fsp);
587 /*******************************************************************
588 This handles the kernel oplock break message.
589 *******************************************************************/
591 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
592 void *private_data,
593 uint32_t msg_type,
594 struct server_id src,
595 DATA_BLOB *data)
597 struct file_id id;
598 unsigned long file_id;
599 files_struct *fsp;
600 struct smbd_server_connection *sconn =
601 talloc_get_type(private_data,
602 struct smbd_server_connection);
604 if (sconn == NULL) {
605 return;
608 if (data->data == NULL) {
609 DEBUG(0, ("Got NULL buffer\n"));
610 return;
613 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
614 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
615 return;
618 /* Pull the data from the message. */
619 pull_file_id_24((char *)data->data, &id);
620 file_id = (unsigned long)IVAL(data->data, 24);
622 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
623 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
624 (unsigned int)file_id));
626 fsp = initial_break_processing(sconn, id, file_id);
628 if (fsp == NULL) {
629 DEBUG(3, ("Got a kernel oplock break message for a file "
630 "I don't know about\n"));
631 return;
634 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
635 /* This is ok, kernel oplocks come in completely async */
636 DEBUG(3, ("Got a kernel oplock request while waiting for a "
637 "break reply\n"));
638 return;
641 if (sconn->using_smb2) {
642 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
643 } else {
644 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
647 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
649 add_oplock_timeout_handler(fsp);
652 void reply_to_oplock_break_requests(files_struct *fsp)
654 int i;
657 * If kernel oplocks already notifies smbds when oplocks are
658 * broken/removed, just return.
660 if (koplocks &&
661 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
662 return;
665 for (i=0; i<fsp->num_pending_break_messages; i++) {
666 struct share_mode_entry *e = &fsp->pending_break_messages[i];
667 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
669 share_mode_entry_to_message(msg, e);
671 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
672 MSG_SMB_BREAK_RESPONSE,
673 (uint8 *)msg,
674 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
677 SAFE_FREE(fsp->pending_break_messages);
678 fsp->num_pending_break_messages = 0;
679 if (fsp->oplock_timeout != NULL) {
680 /* Remove the timed event handler. */
681 TALLOC_FREE(fsp->oplock_timeout);
682 fsp->oplock_timeout = NULL;
684 return;
687 static void process_oplock_break_response(struct messaging_context *msg_ctx,
688 void *private_data,
689 uint32_t msg_type,
690 struct server_id src,
691 DATA_BLOB *data)
693 struct share_mode_entry msg;
694 struct smbd_server_connection *sconn =
695 talloc_get_type(private_data,
696 struct smbd_server_connection);
698 if (sconn == NULL) {
699 return;
702 if (data->data == NULL) {
703 DEBUG(0, ("Got NULL buffer\n"));
704 return;
707 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
708 DEBUG(0, ("Got invalid msg len %u\n",
709 (unsigned int)data->length));
710 return;
713 /* De-linearize incoming message. */
714 message_to_share_mode_entry(&msg, (char *)data->data);
716 DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
717 server_id_str(talloc_tos(), &src),
718 file_id_string_tos(&msg.id),
719 (unsigned long long)msg.share_file_id,
720 (unsigned long long)msg.op_mid));
722 schedule_deferred_open_message_smb(sconn, msg.op_mid);
725 static void process_open_retry_message(struct messaging_context *msg_ctx,
726 void *private_data,
727 uint32_t msg_type,
728 struct server_id src,
729 DATA_BLOB *data)
731 struct share_mode_entry msg;
732 struct smbd_server_connection *sconn =
733 talloc_get_type(private_data,
734 struct smbd_server_connection);
736 if (sconn == NULL) {
737 return;
740 if (data->data == NULL) {
741 DEBUG(0, ("Got NULL buffer\n"));
742 return;
745 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
746 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
747 return;
750 /* De-linearize incoming message. */
751 message_to_share_mode_entry(&msg, (char *)data->data);
753 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
754 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
755 (unsigned long long)msg.op_mid));
757 schedule_deferred_open_message_smb(sconn, msg.op_mid);
760 /****************************************************************************
761 This function is called on any file modification or lock request. If a file
762 is level 2 oplocked then it must tell all other level 2 holders to break to
763 none.
764 ****************************************************************************/
766 static void contend_level2_oplocks_begin_default(files_struct *fsp,
767 enum level2_contention_type type)
769 int i;
770 struct share_mode_lock *lck;
773 * If this file is level II oplocked then we need
774 * to grab the shared memory lock and inform all
775 * other files with a level II lock that they need
776 * to flush their read caches. We keep the lock over
777 * the shared memory area whilst doing this.
780 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
781 return;
783 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
784 NULL);
785 if (lck == NULL) {
786 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
787 "share mode entry for file %s.\n", fsp_str_dbg(fsp)));
788 return;
791 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
792 lck->num_share_modes ));
794 for(i = 0; i < lck->num_share_modes; i++) {
795 struct share_mode_entry *share_entry = &lck->share_modes[i];
796 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
798 if (!is_valid_share_mode_entry(share_entry)) {
799 continue;
803 * As there could have been multiple writes waiting at the
804 * lock_share_entry gate we may not be the first to
805 * enter. Hence the state of the op_types in the share mode
806 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
807 * oplock. It will do no harm to re-send break messages to
808 * those smbd's that are still waiting their turn to remove
809 * their LEVEL_II state, and also no harm to ignore existing
810 * NO_OPLOCK states. JRA.
813 DEBUG(10,("release_level_2_oplocks_on_change: "
814 "share_entry[%i]->op_type == %d\n",
815 i, share_entry->op_type ));
817 if (share_entry->op_type == NO_OPLOCK) {
818 continue;
821 /* Paranoia .... */
822 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
823 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
824 "share mode entry %d is an exlusive "
825 "oplock !\n", i ));
826 TALLOC_FREE(lck);
827 abort();
830 share_mode_entry_to_message(msg, share_entry);
833 * Deal with a race condition when breaking level2
834 * oplocks. Don't send all the messages and release
835 * the lock, this allows someone else to come in and
836 * get a level2 lock before any of the messages are
837 * processed, and thus miss getting a break message.
838 * Ensure at least one entry (the one we're breaking)
839 * is processed immediately under the lock and becomes
840 * set as NO_OPLOCK to stop any waiter getting a level2.
841 * Bugid #5980.
844 if (procid_is_me(&share_entry->pid)) {
845 struct files_struct *cur_fsp =
846 initial_break_processing(fsp->conn->sconn,
847 share_entry->id,
848 share_entry->share_file_id);
849 wait_before_sending_break();
850 if (cur_fsp != NULL) {
851 break_level2_to_none_async(cur_fsp);
852 } else {
853 DEBUG(3, ("release_level_2_oplocks_on_change: "
854 "Did not find fsp, ignoring\n"));
856 } else {
857 messaging_send_buf(fsp->conn->sconn->msg_ctx,
858 share_entry->pid,
859 MSG_SMB_ASYNC_LEVEL2_BREAK,
860 (uint8 *)msg,
861 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
865 /* We let the message receivers handle removing the oplock state
866 in the share mode lock db. */
868 TALLOC_FREE(lck);
871 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
872 enum level2_contention_type type)
874 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
875 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
876 return;
879 contend_level2_oplocks_begin_default(fsp, type);
882 void smbd_contend_level2_oplocks_end(files_struct *fsp,
883 enum level2_contention_type type)
885 /* Only kernel oplocks implement this so far */
886 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
887 koplocks->ops->contend_level2_oplocks_end(fsp, type);
891 /****************************************************************************
892 Linearize a share mode entry struct to an internal oplock break message.
893 ****************************************************************************/
895 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
897 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
898 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
899 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
900 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
901 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
902 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
903 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
904 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
905 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
906 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
907 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
908 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
909 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
910 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
913 /****************************************************************************
914 De-linearize an internal oplock break message to a share mode entry struct.
915 ****************************************************************************/
917 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
919 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
920 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
921 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
922 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
923 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
924 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
925 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
926 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
927 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
928 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
929 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
930 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
931 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
932 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
935 /****************************************************************************
936 Setup oplocks for this process.
937 ****************************************************************************/
939 bool init_oplocks(struct smbd_server_connection *sconn)
941 DEBUG(3,("init_oplocks: initializing messages.\n"));
943 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
944 process_oplock_break_message);
945 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
946 process_oplock_async_level2_break_message);
947 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_RESPONSE,
948 process_oplock_break_response);
949 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
950 process_kernel_oplock_break);
951 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_OPEN_RETRY,
952 process_open_retry_message);
954 if (lp_kernel_oplocks()) {
955 #if HAVE_KERNEL_OPLOCKS_IRIX
956 koplocks = irix_init_kernel_oplocks(sconn);
957 #elif HAVE_KERNEL_OPLOCKS_LINUX
958 koplocks = linux_init_kernel_oplocks(sconn);
959 #elif HAVE_ONEFS
960 #error Isilon, please check if the NULL context is okay here. Thanks!
961 koplocks = onefs_init_kernel_oplocks(NULL);
962 #endif
965 return True;