s3: Remove code TALLOC_FREE already does
[Samba/gebeck_regimport.git] / source3 / smbd / oplock.c
blob173c53e2ec0e0657bb28aed36a90f740520db0f9
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)
66 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
68 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
69 if (use_kernel &&
70 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
71 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
72 "don't support them\n"));
73 return false;
77 if ((fsp->oplock_type != NO_OPLOCK) &&
78 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
79 use_kernel &&
80 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
81 return False;
84 fsp->oplock_type = oplock_type;
85 fsp->sent_oplock_break = NO_BREAK_SENT;
86 if (oplock_type == LEVEL_II_OPLOCK) {
87 level_II_oplocks_open++;
88 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
89 exclusive_oplocks_open++;
92 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
93 "tv_sec = %x, tv_usec = %x\n",
94 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
95 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
96 (int)fsp->open_time.tv_usec ));
98 return True;
101 /****************************************************************************
102 Attempt to release an oplock on a file. Decrements oplock count.
103 ****************************************************************************/
105 void release_file_oplock(files_struct *fsp)
107 if ((fsp->oplock_type != NO_OPLOCK) &&
108 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
109 koplocks) {
110 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
113 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
114 level_II_oplocks_open--;
115 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
116 exclusive_oplocks_open--;
119 SMB_ASSERT(exclusive_oplocks_open>=0);
120 SMB_ASSERT(level_II_oplocks_open>=0);
122 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
123 /* This doesn't matter for close. */
124 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
125 } else {
126 fsp->oplock_type = NO_OPLOCK;
128 fsp->sent_oplock_break = NO_BREAK_SENT;
130 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
131 delete_write_cache(fsp);
133 TALLOC_FREE(fsp->oplock_timeout);
136 /****************************************************************************
137 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
138 ****************************************************************************/
140 static void downgrade_file_oplock(files_struct *fsp)
142 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
143 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
144 return;
147 if (koplocks) {
148 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
150 fsp->oplock_type = LEVEL_II_OPLOCK;
151 exclusive_oplocks_open--;
152 level_II_oplocks_open++;
153 fsp->sent_oplock_break = NO_BREAK_SENT;
156 /****************************************************************************
157 Remove a file oplock. Copes with level II and exclusive.
158 Locks then unlocks the share mode lock. Client can decide to go directly
159 to none even if a "break-to-level II" was sent.
160 ****************************************************************************/
162 bool remove_oplock(files_struct *fsp)
164 bool ret;
165 struct share_mode_lock *lck;
167 /* Remove the oplock flag from the sharemode. */
168 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
169 if (lck == NULL) {
170 DEBUG(0,("remove_oplock: failed to lock share entry for "
171 "file %s\n", fsp_str_dbg(fsp)));
172 return False;
174 ret = remove_share_oplock(lck, fsp);
175 if (!ret) {
176 DEBUG(0,("remove_oplock: failed to remove share oplock for "
177 "file %s fnum %d, %s\n",
178 fsp_str_dbg(fsp), fsp->fnum,
179 file_id_string_tos(&fsp->file_id)));
181 release_file_oplock(fsp);
182 TALLOC_FREE(lck);
183 return ret;
187 * Deal with a reply when a break-to-level II was sent.
189 bool downgrade_oplock(files_struct *fsp)
191 bool ret;
192 struct share_mode_lock *lck;
194 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
195 if (lck == NULL) {
196 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
197 "file %s\n", fsp_str_dbg(fsp)));
198 return False;
200 ret = downgrade_share_oplock(lck, fsp);
201 if (!ret) {
202 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
203 "for file %s fnum %d, file_id %s\n",
204 fsp_str_dbg(fsp), fsp->fnum,
205 file_id_string_tos(&fsp->file_id)));
208 downgrade_file_oplock(fsp);
209 TALLOC_FREE(lck);
210 return ret;
214 * Some kernel oplock implementations handle the notification themselves.
216 bool should_notify_deferred_opens()
218 return !(koplocks &&
219 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
222 /****************************************************************************
223 Set up an oplock break message.
224 ****************************************************************************/
226 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
227 files_struct *fsp, int cmd)
229 char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
231 if (result == NULL) {
232 DEBUG(0, ("talloc failed\n"));
233 return NULL;
236 memset(result,'\0',smb_size);
237 srv_set_message(result,8,0,true);
238 SCVAL(result,smb_com,SMBlockingX);
239 SSVAL(result,smb_tid,fsp->conn->cnum);
240 SSVAL(result,smb_pid,0xFFFF);
241 SSVAL(result,smb_uid,0);
242 SSVAL(result,smb_mid,0xFFFF);
243 SCVAL(result,smb_vwv0,0xFF);
244 SSVAL(result,smb_vwv2,fsp->fnum);
245 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
246 SCVAL(result,smb_vwv3+1,cmd);
247 return result;
250 /****************************************************************************
251 Function to do the waiting before sending a local break.
252 ****************************************************************************/
254 static void wait_before_sending_break(void)
256 long wait_time = (long)lp_oplock_break_wait_time();
258 if (wait_time) {
259 smb_msleep(wait_time);
263 /****************************************************************************
264 Ensure that we have a valid oplock.
265 ****************************************************************************/
267 static files_struct *initial_break_processing(
268 struct smbd_server_connection *sconn, struct file_id id,
269 unsigned long file_id)
271 files_struct *fsp = NULL;
273 if( DEBUGLVL( 3 ) ) {
274 dbgtext( "initial_break_processing: called for %s/%u\n",
275 file_id_string_tos(&id), (int)file_id);
276 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
277 exclusive_oplocks_open, level_II_oplocks_open );
281 * We need to search the file open table for the
282 * entry containing this dev and inode, and ensure
283 * we have an oplock on it.
286 fsp = file_find_dif(sconn, id, file_id);
288 if(fsp == NULL) {
289 /* The file could have been closed in the meantime - return success. */
290 if( DEBUGLVL( 3 ) ) {
291 dbgtext( "initial_break_processing: cannot find open file with " );
292 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
293 dbgtext( "allowing break to succeed.\n" );
295 return NULL;
298 /* Ensure we have an oplock on the file */
301 * There is a potential race condition in that an oplock could
302 * have been broken due to another udp request, and yet there are
303 * still oplock break messages being sent in the udp message
304 * queue for this file. So return true if we don't have an oplock,
305 * as we may have just freed it.
308 if(fsp->oplock_type == NO_OPLOCK) {
309 if( DEBUGLVL( 3 ) ) {
310 dbgtext( "initial_break_processing: file %s ",
311 fsp_str_dbg(fsp));
312 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
313 file_id_string_tos(&id), fsp->fh->gen_id );
314 dbgtext( "Allowing break to succeed regardless.\n" );
316 return NULL;
319 return fsp;
322 static void oplock_timeout_handler(struct event_context *ctx,
323 struct timed_event *te,
324 struct timeval now,
325 void *private_data)
327 files_struct *fsp = (files_struct *)private_data;
329 /* Remove the timed event handler. */
330 TALLOC_FREE(fsp->oplock_timeout);
331 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
332 fsp_str_dbg(fsp)));
333 remove_oplock(fsp);
334 reply_to_oplock_break_requests(fsp);
337 /*******************************************************************
338 Add a timeout handler waiting for the client reply.
339 *******************************************************************/
341 static void add_oplock_timeout_handler(files_struct *fsp)
344 * If kernel oplocks already notifies smbds when an oplock break times
345 * out, just return.
347 if (koplocks &&
348 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
349 return;
352 if (fsp->oplock_timeout != NULL) {
353 DEBUG(0, ("Logic problem -- have an oplock event hanging "
354 "around\n"));
357 fsp->oplock_timeout =
358 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
359 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
360 oplock_timeout_handler, fsp);
362 if (fsp->oplock_timeout == NULL) {
363 DEBUG(0, ("Could not add oplock timeout handler\n"));
367 static void send_break_message_smb1(files_struct *fsp, int level)
369 char *break_msg = new_break_message_smb1(talloc_tos(),
370 fsp,
371 level);
372 if (break_msg == NULL) {
373 exit_server("Could not talloc break_msg\n");
376 show_msg(break_msg);
377 if (!srv_send_smb(fsp->conn->sconn,
378 break_msg, false, 0,
379 IS_CONN_ENCRYPTED(fsp->conn),
380 NULL)) {
381 exit_server_cleanly("send_break_message_smb1: "
382 "srv_send_smb failed.");
385 TALLOC_FREE(break_msg);
388 void break_level2_to_none_async(files_struct *fsp)
390 struct smbd_server_connection *sconn = fsp->conn->sconn;
392 if (fsp->oplock_type == NO_OPLOCK) {
393 /* We already got a "break to none" message and we've handled
394 * it. just ignore. */
395 DEBUG(3, ("process_oplock_async_level2_break_message: already "
396 "broken to none, ignoring.\n"));
397 return;
400 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
401 /* Don't tell the client, just downgrade. */
402 DEBUG(3, ("process_oplock_async_level2_break_message: "
403 "downgrading fake level 2 oplock.\n"));
404 remove_oplock(fsp);
405 return;
408 /* Ensure we're really at level2 state. */
409 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
411 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
412 "to none message for fid %d, file %s\n", fsp->fnum,
413 fsp_str_dbg(fsp)));
415 /* Now send a break to none message to our client. */
416 if (sconn->using_smb2) {
417 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
418 } else {
419 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
422 /* Async level2 request, don't send a reply, just remove the oplock. */
423 remove_oplock(fsp);
426 /*******************************************************************
427 This handles the case of a write triggering a break to none
428 message on a level2 oplock.
429 When we get this message we may be in any of three states :
430 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
431 the client for LEVEL2.
432 *******************************************************************/
434 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
435 void *private_data,
436 uint32_t msg_type,
437 struct server_id src,
438 DATA_BLOB *data)
440 struct share_mode_entry msg;
441 files_struct *fsp;
442 struct smbd_server_connection *sconn =
443 talloc_get_type_abort(private_data,
444 struct smbd_server_connection);
446 if (data->data == NULL) {
447 DEBUG(0, ("Got NULL buffer\n"));
448 return;
451 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
452 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
453 return;
456 /* De-linearize incoming message. */
457 message_to_share_mode_entry(&msg, (char *)data->data);
459 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
460 "%s/%llu\n", server_id_str(talloc_tos(), &src),
461 file_id_string_tos(&msg.id),
462 (unsigned long long)msg.share_file_id));
464 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
466 if (fsp == NULL) {
467 /* We hit a race here. Break messages are sent, and before we
468 * get to process this message, we have closed the file.
469 * No need to reply as this is an async message. */
470 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
471 return;
474 break_level2_to_none_async(fsp);
477 /*******************************************************************
478 This handles the generic oplock break message from another smbd.
479 *******************************************************************/
481 static void process_oplock_break_message(struct messaging_context *msg_ctx,
482 void *private_data,
483 uint32_t msg_type,
484 struct server_id src,
485 DATA_BLOB *data)
487 struct share_mode_entry msg;
488 files_struct *fsp;
489 bool break_to_level2 = False;
490 bool use_kernel;
491 struct smbd_server_connection *sconn =
492 talloc_get_type_abort(private_data,
493 struct smbd_server_connection);
495 if (data->data == NULL) {
496 DEBUG(0, ("Got NULL buffer\n"));
497 return;
500 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
501 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
502 return;
505 /* De-linearize incoming message. */
506 message_to_share_mode_entry(&msg, (char *)data->data);
508 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
509 server_id_str(talloc_tos(), &src),
510 file_id_string_tos(&msg.id),
511 (unsigned long long)msg.share_file_id));
513 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
515 if (fsp == NULL) {
516 /* We hit a race here. Break messages are sent, and before we
517 * get to process this message, we have closed the file. Reply
518 * with 'ok, oplock broken' */
519 DEBUG(3, ("Did not find fsp\n"));
521 /* We just send the same message back. */
522 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
523 (uint8 *)data->data,
524 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
525 return;
528 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
529 /* Remember we have to inform the requesting PID when the
530 * client replies */
531 msg.pid = src;
532 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
533 &fsp->pending_break_messages,
534 &fsp->num_pending_break_messages);
535 return;
538 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
539 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
540 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
541 file_id_string_tos(&fsp->file_id),
542 fsp_str_dbg(fsp)));
543 /* We just send the same message back. */
544 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
545 (uint8 *)data->data,
546 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
547 return;
550 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
552 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
553 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
554 !(use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
555 lp_level2_oplocks(SNUM(fsp->conn))) {
556 break_to_level2 = True;
559 /* Need to wait before sending a break
560 message if we sent ourselves this message. */
561 if (procid_is_me(&src)) {
562 wait_before_sending_break();
565 if (sconn->using_smb2) {
566 send_break_message_smb2(fsp, break_to_level2 ?
567 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
568 } else {
569 send_break_message_smb1(fsp, break_to_level2 ?
570 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
573 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
575 msg.pid = src;
576 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
577 &fsp->pending_break_messages,
578 &fsp->num_pending_break_messages);
580 add_oplock_timeout_handler(fsp);
583 /*******************************************************************
584 This handles the kernel oplock break message.
585 *******************************************************************/
587 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
588 void *private_data,
589 uint32_t msg_type,
590 struct server_id src,
591 DATA_BLOB *data)
593 struct file_id id;
594 unsigned long file_id;
595 files_struct *fsp;
596 struct smbd_server_connection *sconn =
597 talloc_get_type_abort(private_data,
598 struct smbd_server_connection);
600 if (data->data == NULL) {
601 DEBUG(0, ("Got NULL buffer\n"));
602 return;
605 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
606 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
607 return;
610 /* Pull the data from the message. */
611 pull_file_id_24((char *)data->data, &id);
612 file_id = (unsigned long)IVAL(data->data, 24);
614 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
615 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
616 (unsigned int)file_id));
618 fsp = initial_break_processing(sconn, id, file_id);
620 if (fsp == NULL) {
621 DEBUG(3, ("Got a kernel oplock break message for a file "
622 "I don't know about\n"));
623 return;
626 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
627 /* This is ok, kernel oplocks come in completely async */
628 DEBUG(3, ("Got a kernel oplock request while waiting for a "
629 "break reply\n"));
630 return;
633 if (sconn->using_smb2) {
634 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
635 } else {
636 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
639 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
641 add_oplock_timeout_handler(fsp);
644 void reply_to_oplock_break_requests(files_struct *fsp)
646 int i;
649 * If kernel oplocks already notifies smbds when oplocks are
650 * broken/removed, just return.
652 if (koplocks &&
653 (koplocks->flags & KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION)) {
654 return;
657 for (i=0; i<fsp->num_pending_break_messages; i++) {
658 struct share_mode_entry *e = &fsp->pending_break_messages[i];
659 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
661 share_mode_entry_to_message(msg, e);
663 messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
664 MSG_SMB_BREAK_RESPONSE,
665 (uint8 *)msg,
666 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
669 SAFE_FREE(fsp->pending_break_messages);
670 fsp->num_pending_break_messages = 0;
671 TALLOC_FREE(fsp->oplock_timeout);
672 return;
675 static void process_oplock_break_response(struct messaging_context *msg_ctx,
676 void *private_data,
677 uint32_t msg_type,
678 struct server_id src,
679 DATA_BLOB *data)
681 struct share_mode_entry msg;
682 struct smbd_server_connection *sconn =
683 talloc_get_type_abort(private_data,
684 struct smbd_server_connection);
686 if (data->data == NULL) {
687 DEBUG(0, ("Got NULL buffer\n"));
688 return;
691 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
692 DEBUG(0, ("Got invalid msg len %u\n",
693 (unsigned int)data->length));
694 return;
697 /* De-linearize incoming message. */
698 message_to_share_mode_entry(&msg, (char *)data->data);
700 DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
701 server_id_str(talloc_tos(), &src),
702 file_id_string_tos(&msg.id),
703 (unsigned long long)msg.share_file_id,
704 (unsigned long long)msg.op_mid));
706 schedule_deferred_open_message_smb(sconn, msg.op_mid);
709 static void process_open_retry_message(struct messaging_context *msg_ctx,
710 void *private_data,
711 uint32_t msg_type,
712 struct server_id src,
713 DATA_BLOB *data)
715 struct share_mode_entry msg;
716 struct smbd_server_connection *sconn =
717 talloc_get_type_abort(private_data,
718 struct smbd_server_connection);
720 if (data->data == NULL) {
721 DEBUG(0, ("Got NULL buffer\n"));
722 return;
725 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
726 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
727 return;
730 /* De-linearize incoming message. */
731 message_to_share_mode_entry(&msg, (char *)data->data);
733 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
734 server_id_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
735 (unsigned long long)msg.op_mid));
737 schedule_deferred_open_message_smb(sconn, msg.op_mid);
740 struct break_to_none_state {
741 struct smbd_server_connection *sconn;
742 struct file_id id;
744 static void do_break_to_none(struct tevent_req *req);
746 /****************************************************************************
747 This function is called on any file modification or lock request. If a file
748 is level 2 oplocked then it must tell all other level 2 holders to break to
749 none.
750 ****************************************************************************/
752 static void contend_level2_oplocks_begin_default(files_struct *fsp,
753 enum level2_contention_type type)
755 struct smbd_server_connection *sconn = fsp->conn->sconn;
756 struct tevent_req *req;
757 struct break_to_none_state *state;
760 * If this file is level II oplocked then we need
761 * to grab the shared memory lock and inform all
762 * other files with a level II lock that they need
763 * to flush their read caches. We keep the lock over
764 * the shared memory area whilst doing this.
767 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
768 return;
771 * When we get here we might have a brlock entry locked. Also
772 * locking the share mode entry would violate the locking
773 * order. Breaking level2 oplocks to none is asynchronous
774 * anyway, so we postpone this into an immediate timed event.
777 state = talloc(sconn, struct break_to_none_state);
778 if (state == NULL) {
779 DEBUG(1, ("talloc failed\n"));
780 return;
782 state->sconn = sconn;
783 state->id = fsp->file_id;
785 req = tevent_wakeup_send(state, sconn->ev_ctx, timeval_set(0, 0));
786 if (req == NULL) {
787 DEBUG(1, ("tevent_wakeup_send failed\n"));
788 TALLOC_FREE(state);
789 return;
791 tevent_req_set_callback(req, do_break_to_none, state);
792 return;
795 static void do_break_to_none(struct tevent_req *req)
797 struct break_to_none_state *state = tevent_req_callback_data(
798 req, struct break_to_none_state);
799 bool ret;
800 int i;
801 struct share_mode_lock *lck;
803 ret = tevent_wakeup_recv(req);
804 TALLOC_FREE(req);
805 if (!ret) {
806 DEBUG(1, ("tevent_wakeup_recv failed\n"));
807 goto done;
809 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
810 if (lck == NULL) {
811 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
812 "share mode entry for file %s.\n",
813 file_id_string_tos(&state->id)));
814 goto done;
817 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
818 lck->data->num_share_modes ));
820 for(i = 0; i < lck->data->num_share_modes; i++) {
821 struct share_mode_entry *share_entry = &lck->data->share_modes[i];
822 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
824 if (!is_valid_share_mode_entry(share_entry)) {
825 continue;
829 * As there could have been multiple writes waiting at the
830 * lock_share_entry gate we may not be the first to
831 * enter. Hence the state of the op_types in the share mode
832 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
833 * oplock. It will do no harm to re-send break messages to
834 * those smbd's that are still waiting their turn to remove
835 * their LEVEL_II state, and also no harm to ignore existing
836 * NO_OPLOCK states. JRA.
839 DEBUG(10,("release_level_2_oplocks_on_change: "
840 "share_entry[%i]->op_type == %d\n",
841 i, share_entry->op_type ));
843 if (share_entry->op_type == NO_OPLOCK) {
844 continue;
847 /* Paranoia .... */
848 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
849 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
850 "share mode entry %d is an exlusive "
851 "oplock !\n", i ));
852 TALLOC_FREE(lck);
853 abort();
856 share_mode_entry_to_message(msg, share_entry);
859 * Deal with a race condition when breaking level2
860 * oplocks. Don't send all the messages and release
861 * the lock, this allows someone else to come in and
862 * get a level2 lock before any of the messages are
863 * processed, and thus miss getting a break message.
864 * Ensure at least one entry (the one we're breaking)
865 * is processed immediately under the lock and becomes
866 * set as NO_OPLOCK to stop any waiter getting a level2.
867 * Bugid #5980.
870 if (procid_is_me(&share_entry->pid)) {
871 struct files_struct *cur_fsp =
872 initial_break_processing(state->sconn,
873 share_entry->id,
874 share_entry->share_file_id);
875 wait_before_sending_break();
876 if (cur_fsp != NULL) {
877 break_level2_to_none_async(cur_fsp);
878 } else {
879 DEBUG(3, ("release_level_2_oplocks_on_change: "
880 "Did not find fsp, ignoring\n"));
882 } else {
883 messaging_send_buf(state->sconn->msg_ctx,
884 share_entry->pid,
885 MSG_SMB_ASYNC_LEVEL2_BREAK,
886 (uint8 *)msg,
887 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
891 /* We let the message receivers handle removing the oplock state
892 in the share mode lock db. */
894 TALLOC_FREE(lck);
895 done:
896 TALLOC_FREE(state);
897 return;
900 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
901 enum level2_contention_type type)
903 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
904 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
905 return;
908 contend_level2_oplocks_begin_default(fsp, type);
911 void smbd_contend_level2_oplocks_end(files_struct *fsp,
912 enum level2_contention_type type)
914 /* Only kernel oplocks implement this so far */
915 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
916 koplocks->ops->contend_level2_oplocks_end(fsp, type);
920 /****************************************************************************
921 Linearize a share mode entry struct to an internal oplock break message.
922 ****************************************************************************/
924 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
926 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
927 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
928 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
929 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
930 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
931 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
932 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
933 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
934 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
935 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
936 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
937 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
938 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
939 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
942 /****************************************************************************
943 De-linearize an internal oplock break message to a share mode entry struct.
944 ****************************************************************************/
946 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
948 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
949 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
950 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
951 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
952 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
953 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
954 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
955 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
956 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
957 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
958 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
959 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
960 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
961 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
964 /****************************************************************************
965 Setup oplocks for this process.
966 ****************************************************************************/
968 bool init_oplocks(struct smbd_server_connection *sconn)
970 DEBUG(3,("init_oplocks: initializing messages.\n"));
972 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
973 process_oplock_break_message);
974 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
975 process_oplock_async_level2_break_message);
976 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_RESPONSE,
977 process_oplock_break_response);
978 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
979 process_kernel_oplock_break);
980 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_OPEN_RETRY,
981 process_open_retry_message);
983 return true;
986 void init_kernel_oplocks(struct smbd_server_connection *sconn)
988 /* only initialize once */
989 if (koplocks == NULL) {
990 #if HAVE_KERNEL_OPLOCKS_IRIX
991 koplocks = irix_init_kernel_oplocks(sconn);
992 #elif HAVE_KERNEL_OPLOCKS_LINUX
993 koplocks = linux_init_kernel_oplocks(sconn);
994 #elif HAVE_ONEFS
995 #error Isilon, please check if the NULL context is okay here. Thanks!
996 koplocks = onefs_init_kernel_oplocks(sconn);
997 #endif