s3/packaging: Fix typo in comment.
[Samba.git] / source3 / smbd / oplock.c
blob22870283fa5996d2e519096ca8ba319d7ea7f144
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_24((char *)msg, &fsp->file_id);
44 SIVAL(msg,24,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 (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
126 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
127 return;
130 if (koplocks) {
131 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
133 fsp->oplock_type = LEVEL_II_OPLOCK;
134 exclusive_oplocks_open--;
135 level_II_oplocks_open++;
136 fsp->sent_oplock_break = NO_BREAK_SENT;
139 /****************************************************************************
140 Remove a file oplock. Copes with level II and exclusive.
141 Locks then unlocks the share mode lock. Client can decide to go directly
142 to none even if a "break-to-level II" was sent.
143 ****************************************************************************/
145 bool remove_oplock(files_struct *fsp)
147 bool ret;
148 struct share_mode_lock *lck;
150 /* Remove the oplock flag from the sharemode. */
151 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
152 NULL);
153 if (lck == NULL) {
154 DEBUG(0,("remove_oplock: failed to lock share entry for "
155 "file %s\n", fsp->fsp_name ));
156 return False;
158 ret = remove_share_oplock(lck, fsp);
159 if (!ret) {
160 DEBUG(0,("remove_oplock: failed to remove share oplock for "
161 "file %s fnum %d, %s\n",
162 fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
164 release_file_oplock(fsp);
165 TALLOC_FREE(lck);
166 return ret;
170 * Deal with a reply when a break-to-level II was sent.
172 bool downgrade_oplock(files_struct *fsp)
174 bool ret;
175 struct share_mode_lock *lck;
177 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
178 NULL);
179 if (lck == NULL) {
180 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
181 "file %s\n", fsp->fsp_name ));
182 return False;
184 ret = downgrade_share_oplock(lck, fsp);
185 if (!ret) {
186 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
187 "for file %s fnum %d, file_id %s\n",
188 fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
191 downgrade_file_oplock(fsp);
192 TALLOC_FREE(lck);
193 return ret;
197 * Some kernel oplock implementations handle the notification themselves.
199 bool should_notify_deferred_opens()
201 return !(koplocks &&
202 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
205 /****************************************************************************
206 Set up an oplock break message.
207 ****************************************************************************/
209 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
210 files_struct *fsp, uint8 cmd)
212 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
214 if (result == NULL) {
215 DEBUG(0, ("talloc failed\n"));
216 return NULL;
219 memset(result,'\0',smb_size);
220 srv_set_message(result,8,0,true);
221 SCVAL(result,smb_com,SMBlockingX);
222 SSVAL(result,smb_tid,fsp->conn->cnum);
223 SSVAL(result,smb_pid,0xFFFF);
224 SSVAL(result,smb_uid,0);
225 SSVAL(result,smb_mid,0xFFFF);
226 SCVAL(result,smb_vwv0,0xFF);
227 SSVAL(result,smb_vwv2,fsp->fnum);
228 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
229 SCVAL(result,smb_vwv3+1,cmd);
230 return result;
233 /****************************************************************************
234 Function to do the waiting before sending a local break.
235 ****************************************************************************/
237 static void wait_before_sending_break(void)
239 long wait_time = (long)lp_oplock_break_wait_time();
241 if (wait_time) {
242 smb_msleep(wait_time);
246 /****************************************************************************
247 Ensure that we have a valid oplock.
248 ****************************************************************************/
250 static files_struct *initial_break_processing(struct file_id id, unsigned long file_id)
252 files_struct *fsp = NULL;
254 if( DEBUGLVL( 3 ) ) {
255 dbgtext( "initial_break_processing: called for %s/%u\n",
256 file_id_string_tos(&id), (int)file_id);
257 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
258 exclusive_oplocks_open, level_II_oplocks_open );
262 * We need to search the file open table for the
263 * entry containing this dev and inode, and ensure
264 * we have an oplock on it.
267 fsp = file_find_dif(id, file_id);
269 if(fsp == NULL) {
270 /* The file could have been closed in the meantime - return success. */
271 if( DEBUGLVL( 3 ) ) {
272 dbgtext( "initial_break_processing: cannot find open file with " );
273 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
274 dbgtext( "allowing break to succeed.\n" );
276 return NULL;
279 /* Ensure we have an oplock on the file */
282 * There is a potential race condition in that an oplock could
283 * have been broken due to another udp request, and yet there are
284 * still oplock break messages being sent in the udp message
285 * queue for this file. So return true if we don't have an oplock,
286 * as we may have just freed it.
289 if(fsp->oplock_type == NO_OPLOCK) {
290 if( DEBUGLVL( 3 ) ) {
291 dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
292 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
293 file_id_string_tos(&id), fsp->fh->gen_id );
294 dbgtext( "Allowing break to succeed regardless.\n" );
296 return NULL;
299 return fsp;
302 static void oplock_timeout_handler(struct event_context *ctx,
303 struct timed_event *te,
304 struct timeval now,
305 void *private_data)
307 files_struct *fsp = (files_struct *)private_data;
309 /* Remove the timed event handler. */
310 TALLOC_FREE(fsp->oplock_timeout);
311 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
312 global_client_failed_oplock_break = True;
313 remove_oplock(fsp);
314 reply_to_oplock_break_requests(fsp);
317 /*******************************************************************
318 Add a timeout handler waiting for the client reply.
319 *******************************************************************/
321 static void add_oplock_timeout_handler(files_struct *fsp)
324 * If kernel oplocks already notifies smbds when an oplock break times
325 * out, just return.
327 if (koplocks &&
328 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
329 return;
332 if (fsp->oplock_timeout != NULL) {
333 DEBUG(0, ("Logic problem -- have an oplock event hanging "
334 "around\n"));
337 fsp->oplock_timeout =
338 event_add_timed(smbd_event_context(), NULL,
339 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
340 oplock_timeout_handler, fsp);
342 if (fsp->oplock_timeout == NULL) {
343 DEBUG(0, ("Could not add oplock timeout handler\n"));
347 /*******************************************************************
348 This handles the case of a write triggering a break to none
349 message on a level2 oplock.
350 When we get this message we may be in any of three states :
351 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
352 the client for LEVEL2.
353 *******************************************************************/
355 void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
356 void *private_data,
357 uint32_t msg_type,
358 struct server_id src,
359 DATA_BLOB *data)
361 struct share_mode_entry msg;
362 files_struct *fsp;
363 char *break_msg;
364 bool sign_state;
366 if (data->data == NULL) {
367 DEBUG(0, ("Got NULL buffer\n"));
368 return;
371 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
372 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
373 return;
376 /* De-linearize incoming message. */
377 message_to_share_mode_entry(&msg, (char *)data->data);
379 DEBUG(10, ("Got oplock async level 2 break message from pid %d: %s/%lu\n",
380 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
382 fsp = initial_break_processing(msg.id, msg.share_file_id);
384 if (fsp == NULL) {
385 /* We hit a race here. Break messages are sent, and before we
386 * get to process this message, we have closed the file.
387 * No need to reply as this is an async message. */
388 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
389 return;
392 if (fsp->oplock_type == NO_OPLOCK) {
393 /* We already got a "break to none" message and we've handled it.
394 * just ignore. */
395 DEBUG(3, ("process_oplock_async_level2_break_message: already 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: 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 to "
410 "none message for fid %d, file %s\n",
411 fsp->fnum,
412 fsp->fsp_name));
414 /* Now send a break to none message to our client. */
416 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
417 if (break_msg == NULL) {
418 exit_server("Could not talloc break_msg\n");
421 /* Need to wait before sending a break message if we sent ourselves this message. */
422 if (procid_to_pid(&src) == sys_getpid()) {
423 wait_before_sending_break();
426 /* Save the server smb signing state. */
427 sign_state = srv_oplock_set_signing(False);
429 show_msg(break_msg);
430 if (!srv_send_smb(smbd_server_fd(),
431 break_msg,
432 IS_CONN_ENCRYPTED(fsp->conn),
433 NULL)) {
434 exit_server_cleanly("oplock_break: srv_send_smb failed.");
437 /* Restore the sign state to what it was. */
438 srv_oplock_set_signing(sign_state);
440 TALLOC_FREE(break_msg);
442 /* Async level2 request, don't send a reply, just remove the oplock. */
443 remove_oplock(fsp);
446 /*******************************************************************
447 This handles the generic oplock break message from another smbd.
448 *******************************************************************/
450 static void process_oplock_break_message(struct messaging_context *msg_ctx,
451 void *private_data,
452 uint32_t msg_type,
453 struct server_id src,
454 DATA_BLOB *data)
456 struct share_mode_entry msg;
457 files_struct *fsp;
458 char *break_msg;
459 bool break_to_level2 = False;
460 bool sign_state;
462 if (data->data == NULL) {
463 DEBUG(0, ("Got NULL buffer\n"));
464 return;
467 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
468 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
469 return;
472 /* De-linearize incoming message. */
473 message_to_share_mode_entry(&msg, (char *)data->data);
475 DEBUG(10, ("Got oplock break message from pid %d: %s/%lu\n",
476 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
478 fsp = initial_break_processing(msg.id, msg.share_file_id);
480 if (fsp == NULL) {
481 /* a We hit race here. Break messages are sent, and before we
482 * get to process this message, we have closed the file. Reply
483 * with 'ok, oplock broken' */
484 DEBUG(3, ("Did not find fsp\n"));
486 /* We just send the same message back. */
487 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
488 (uint8 *)data->data,
489 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
490 return;
493 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
494 /* Remember we have to inform the requesting PID when the
495 * client replies */
496 msg.pid = src;
497 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
498 &fsp->pending_break_messages,
499 &fsp->num_pending_break_messages);
500 return;
503 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
504 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
505 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
506 file_id_string_tos(&fsp->file_id),
507 fsp->fsp_name));
508 /* We just send the same message back. */
509 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
510 (uint8 *)data->data,
511 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
512 return;
515 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
516 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
517 !(koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
518 lp_level2_oplocks(SNUM(fsp->conn))) {
519 break_to_level2 = True;
522 break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
523 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
524 if (break_msg == NULL) {
525 exit_server("Could not talloc break_msg\n");
528 /* Need to wait before sending a break message if we sent ourselves this message. */
529 if (procid_to_pid(&src) == sys_getpid()) {
530 wait_before_sending_break();
533 /* Save the server smb signing state. */
534 sign_state = srv_oplock_set_signing(False);
536 show_msg(break_msg);
537 if (!srv_send_smb(smbd_server_fd(),
538 break_msg,
539 IS_CONN_ENCRYPTED(fsp->conn),
540 NULL)) {
541 exit_server_cleanly("oplock_break: srv_send_smb failed.");
544 /* Restore the sign state to what it was. */
545 srv_oplock_set_signing(sign_state);
547 TALLOC_FREE(break_msg);
549 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
551 msg.pid = src;
552 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
553 &fsp->pending_break_messages,
554 &fsp->num_pending_break_messages);
556 add_oplock_timeout_handler(fsp);
559 /*******************************************************************
560 This handles the kernel oplock break message.
561 *******************************************************************/
563 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
564 void *private_data,
565 uint32_t msg_type,
566 struct server_id src,
567 DATA_BLOB *data)
569 struct file_id id;
570 unsigned long file_id;
571 files_struct *fsp;
572 char *break_msg;
573 bool sign_state;
575 if (data->data == NULL) {
576 DEBUG(0, ("Got NULL buffer\n"));
577 return;
580 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
581 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
582 return;
585 /* Pull the data from the message. */
586 pull_file_id_24((char *)data->data, &id);
587 file_id = (unsigned long)IVAL(data->data, 24);
589 DEBUG(10, ("Got kernel oplock break message from pid %d: %s/%u\n",
590 (int)procid_to_pid(&src), file_id_string_tos(&id),
591 (unsigned int)file_id));
593 fsp = initial_break_processing(id, file_id);
595 if (fsp == NULL) {
596 DEBUG(3, ("Got a kernel oplock break message for a file "
597 "I don't know about\n"));
598 return;
601 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
602 /* This is ok, kernel oplocks come in completely async */
603 DEBUG(3, ("Got a kernel oplock request while waiting for a "
604 "break reply\n"));
605 return;
608 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
609 if (break_msg == NULL) {
610 exit_server("Could not talloc break_msg\n");
613 /* Save the server smb signing state. */
614 sign_state = srv_oplock_set_signing(False);
616 show_msg(break_msg);
617 if (!srv_send_smb(smbd_server_fd(),
618 break_msg,
619 IS_CONN_ENCRYPTED(fsp->conn),
620 NULL)) {
621 exit_server_cleanly("oplock_break: srv_send_smb failed.");
624 /* Restore the sign state to what it was. */
625 srv_oplock_set_signing(sign_state);
627 TALLOC_FREE(break_msg);
629 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
631 add_oplock_timeout_handler(fsp);
634 void reply_to_oplock_break_requests(files_struct *fsp)
636 int i;
639 * If kernel oplocks already notifies smbds when oplocks are
640 * broken/removed, just return.
642 if (koplocks &&
643 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
644 return;
647 for (i=0; i<fsp->num_pending_break_messages; i++) {
648 struct share_mode_entry *e = &fsp->pending_break_messages[i];
649 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
651 share_mode_entry_to_message(msg, e);
653 messaging_send_buf(smbd_messaging_context(), e->pid,
654 MSG_SMB_BREAK_RESPONSE,
655 (uint8 *)msg,
656 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
659 SAFE_FREE(fsp->pending_break_messages);
660 fsp->num_pending_break_messages = 0;
661 if (fsp->oplock_timeout != NULL) {
662 /* Remove the timed event handler. */
663 TALLOC_FREE(fsp->oplock_timeout);
664 fsp->oplock_timeout = NULL;
666 return;
669 static void process_oplock_break_response(struct messaging_context *msg_ctx,
670 void *private_data,
671 uint32_t msg_type,
672 struct server_id src,
673 DATA_BLOB *data)
675 struct share_mode_entry msg;
677 if (data->data == NULL) {
678 DEBUG(0, ("Got NULL buffer\n"));
679 return;
682 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
683 DEBUG(0, ("Got invalid msg len %u\n",
684 (unsigned int)data->length));
685 return;
688 /* De-linearize incoming message. */
689 message_to_share_mode_entry(&msg, (char *)data->data);
691 DEBUG(10, ("Got oplock break response from pid %d: %s/%lu mid %u\n",
692 (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id,
693 (unsigned int)msg.op_mid));
695 /* Here's the hack from open.c, store the mid in the 'port' field */
696 schedule_deferred_open_smb_message(msg.op_mid);
699 static void process_open_retry_message(struct messaging_context *msg_ctx,
700 void *private_data,
701 uint32_t msg_type,
702 struct server_id src,
703 DATA_BLOB *data)
705 struct share_mode_entry msg;
707 if (data->data == NULL) {
708 DEBUG(0, ("Got NULL buffer\n"));
709 return;
712 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
713 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
714 return;
717 /* De-linearize incoming message. */
718 message_to_share_mode_entry(&msg, (char *)data->data);
720 DEBUG(10, ("Got open retry msg from pid %d: %s mid %u\n",
721 (int)procid_to_pid(&src), file_id_string_tos(&msg.id),
722 (unsigned int)msg.op_mid));
724 schedule_deferred_open_smb_message(msg.op_mid);
727 /****************************************************************************
728 This function is called on any file modification or lock request. If a file
729 is level 2 oplocked then it must tell all other level 2 holders to break to
730 none.
731 ****************************************************************************/
733 static void contend_level2_oplocks_begin_default(files_struct *fsp,
734 enum level2_contention_type type)
736 int i;
737 struct share_mode_lock *lck;
740 * If this file is level II oplocked then we need
741 * to grab the shared memory lock and inform all
742 * other files with a level II lock that they need
743 * to flush their read caches. We keep the lock over
744 * the shared memory area whilst doing this.
747 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
748 return;
750 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
751 NULL);
752 if (lck == NULL) {
753 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
754 "share mode entry for file %s.\n", fsp->fsp_name ));
755 return;
758 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
759 lck->num_share_modes ));
761 for(i = 0; i < lck->num_share_modes; i++) {
762 struct share_mode_entry *share_entry = &lck->share_modes[i];
763 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
765 if (!is_valid_share_mode_entry(share_entry)) {
766 continue;
770 * As there could have been multiple writes waiting at the
771 * lock_share_entry gate we may not be the first to
772 * enter. Hence the state of the op_types in the share mode
773 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
774 * oplock. It will do no harm to re-send break messages to
775 * those smbd's that are still waiting their turn to remove
776 * their LEVEL_II state, and also no harm to ignore existing
777 * NO_OPLOCK states. JRA.
780 DEBUG(10,("release_level_2_oplocks_on_change: "
781 "share_entry[%i]->op_type == %d\n",
782 i, share_entry->op_type ));
784 if (share_entry->op_type == NO_OPLOCK) {
785 continue;
788 /* Paranoia .... */
789 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
790 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
791 "share mode entry %d is an exlusive "
792 "oplock !\n", i ));
793 TALLOC_FREE(lck);
794 abort();
797 share_mode_entry_to_message(msg, share_entry);
800 * Deal with a race condition when breaking level2
801 * oplocks. Don't send all the messages and release
802 * the lock, this allows someone else to come in and
803 * get a level2 lock before any of the messages are
804 * processed, and thus miss getting a break message.
805 * Ensure at least one entry (the one we're breaking)
806 * is processed immediately under the lock and becomes
807 * set as NO_OPLOCK to stop any waiter getting a level2.
808 * Bugid #5980.
811 if (procid_is_me(&share_entry->pid)) {
812 DATA_BLOB blob = data_blob_const(msg,
813 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
814 process_oplock_async_level2_break_message(smbd_messaging_context(),
815 NULL,
816 MSG_SMB_ASYNC_LEVEL2_BREAK,
817 share_entry->pid,
818 &blob);
819 } else {
820 messaging_send_buf(smbd_messaging_context(),
821 share_entry->pid,
822 MSG_SMB_ASYNC_LEVEL2_BREAK,
823 (uint8 *)msg,
824 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
828 /* We let the message receivers handle removing the oplock state
829 in the share mode lock db. */
831 TALLOC_FREE(lck);
834 void contend_level2_oplocks_begin(files_struct *fsp,
835 enum level2_contention_type type)
837 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
838 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
839 return;
842 contend_level2_oplocks_begin_default(fsp, type);
845 void contend_level2_oplocks_end(files_struct *fsp,
846 enum level2_contention_type type)
848 /* Only kernel oplocks implement this so far */
849 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
850 koplocks->ops->contend_level2_oplocks_end(fsp, type);
854 /****************************************************************************
855 Linearize a share mode entry struct to an internal oplock break message.
856 ****************************************************************************/
858 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
860 SIVAL(msg,0,(uint32)e->pid.pid);
861 SSVAL(msg,4,e->op_mid);
862 SSVAL(msg,6,e->op_type);
863 SIVAL(msg,8,e->access_mask);
864 SIVAL(msg,12,e->share_access);
865 SIVAL(msg,16,e->private_options);
866 SIVAL(msg,20,(uint32)e->time.tv_sec);
867 SIVAL(msg,24,(uint32)e->time.tv_usec);
868 push_file_id_24(msg+28, &e->id);
869 SIVAL(msg,52,e->share_file_id);
870 SIVAL(msg,56,e->uid);
871 SSVAL(msg,60,e->flags);
872 #ifdef CLUSTER_SUPPORT
873 SIVAL(msg,62,e->pid.vnn);
874 #endif
877 /****************************************************************************
878 De-linearize an internal oplock break message to a share mode entry struct.
879 ****************************************************************************/
881 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
883 e->pid.pid = (pid_t)IVAL(msg,0);
884 e->op_mid = SVAL(msg,4);
885 e->op_type = SVAL(msg,6);
886 e->access_mask = IVAL(msg,8);
887 e->share_access = IVAL(msg,12);
888 e->private_options = IVAL(msg,16);
889 e->time.tv_sec = (time_t)IVAL(msg,20);
890 e->time.tv_usec = (int)IVAL(msg,24);
891 pull_file_id_24(msg+28, &e->id);
892 e->share_file_id = (unsigned long)IVAL(msg,52);
893 e->uid = (uint32)IVAL(msg,56);
894 e->flags = (uint16)SVAL(msg,60);
895 #ifdef CLUSTER_SUPPORT
896 e->pid.vnn = IVAL(msg,62);
897 #endif
900 /****************************************************************************
901 Setup oplocks for this process.
902 ****************************************************************************/
904 bool init_oplocks(struct messaging_context *msg_ctx)
906 DEBUG(3,("init_oplocks: initializing messages.\n"));
908 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
909 process_oplock_break_message);
910 messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
911 process_oplock_async_level2_break_message);
912 messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
913 process_oplock_break_response);
914 messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
915 process_kernel_oplock_break);
916 messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
917 process_open_retry_message);
919 if (lp_kernel_oplocks()) {
920 #if HAVE_KERNEL_OPLOCKS_IRIX
921 koplocks = irix_init_kernel_oplocks(talloc_autofree_context());
922 #elif HAVE_KERNEL_OPLOCKS_LINUX
923 koplocks = linux_init_kernel_oplocks(talloc_autofree_context());
924 #elif HAVE_ONEFS
925 koplocks = onefs_init_kernel_oplocks(talloc_autofree_context());
926 #endif
929 return True;