smbd: Simplify new_break_message_smb1
[Samba.git] / source3 / smbd / oplock.c
blob21792bdee40675a3c8f407568069f0f8a1b96b0f
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"
30 * helper function used by the kernel oplock backends to post the break message
32 void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
34 uint8_t msg[MSG_SMB_KERNEL_BREAK_SIZE];
36 /* Put the kernel break info into the message. */
37 push_file_id_24((char *)msg, &fsp->file_id);
38 SIVAL(msg,24,fsp->fh->gen_id);
40 /* Don't need to be root here as we're only ever
41 sending to ourselves. */
43 messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
44 MSG_SMB_KERNEL_BREAK,
45 msg, MSG_SMB_KERNEL_BREAK_SIZE);
48 /****************************************************************************
49 Attempt to set an oplock on a file. Succeeds if kernel oplocks are
50 disabled (just sets flags) and no byte-range locks in the file. Returns True
51 if oplock set.
52 ****************************************************************************/
54 NTSTATUS set_file_oplock(files_struct *fsp, int oplock_type)
56 struct smbd_server_connection *sconn = fsp->conn->sconn;
57 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
58 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
60 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
61 if (use_kernel &&
62 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
63 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
64 "don't support them\n"));
65 return NT_STATUS_NOT_SUPPORTED;
69 if ((fsp->oplock_type != NO_OPLOCK) &&
70 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
71 use_kernel &&
72 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type))
74 return map_nt_error_from_unix(errno);
77 fsp->oplock_type = oplock_type;
78 fsp->sent_oplock_break = NO_BREAK_SENT;
79 if (oplock_type == LEVEL_II_OPLOCK) {
80 sconn->oplocks.level_II_open++;
81 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
82 sconn->oplocks.exclusive_open++;
85 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
86 "tv_sec = %x, tv_usec = %x\n",
87 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
88 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
89 (int)fsp->open_time.tv_usec ));
91 return NT_STATUS_OK;
94 /****************************************************************************
95 Attempt to release an oplock on a file. Decrements oplock count.
96 ****************************************************************************/
98 void release_file_oplock(files_struct *fsp)
100 struct smbd_server_connection *sconn = fsp->conn->sconn;
101 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
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 sconn->oplocks.level_II_open--;
111 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
112 sconn->oplocks.exclusive_open--;
115 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
116 SMB_ASSERT(sconn->oplocks.level_II_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 struct smbd_server_connection *sconn = fsp->conn->sconn;
139 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
141 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
142 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
143 return;
146 if (koplocks) {
147 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
149 fsp->oplock_type = LEVEL_II_OPLOCK;
150 sconn->oplocks.exclusive_open--;
151 sconn->oplocks.level_II_open++;
152 fsp->sent_oplock_break = NO_BREAK_SENT;
155 /****************************************************************************
156 Remove a file oplock. Copes with level II and exclusive.
157 Locks then unlocks the share mode lock. Client can decide to go directly
158 to none even if a "break-to-level II" was sent.
159 ****************************************************************************/
161 bool remove_oplock(files_struct *fsp)
163 bool ret;
164 struct share_mode_lock *lck;
166 /* Remove the oplock flag from the sharemode. */
167 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
168 if (lck == NULL) {
169 DEBUG(0,("remove_oplock: failed to lock share entry for "
170 "file %s\n", fsp_str_dbg(fsp)));
171 return False;
173 ret = remove_share_oplock(lck, fsp);
174 if (!ret) {
175 DEBUG(0,("remove_oplock: failed to remove share oplock for "
176 "file %s, %s, %s\n",
177 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
178 file_id_string_tos(&fsp->file_id)));
180 release_file_oplock(fsp);
181 TALLOC_FREE(lck);
182 return ret;
186 * Deal with a reply when a break-to-level II was sent.
188 bool downgrade_oplock(files_struct *fsp)
190 bool ret;
191 struct share_mode_lock *lck;
193 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
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, %s, file_id %s\n",
203 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
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(struct smbd_server_connection *sconn)
217 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
218 return !(koplocks &&
219 (koplocks->flags & KOPLOCKS_DEFERRED_OPEN_NOTIFICATION));
222 /****************************************************************************
223 Set up an oplock break message.
224 ****************************************************************************/
226 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
228 static void new_break_message_smb1(files_struct *fsp, int cmd,
229 char result[SMB1_BREAK_MESSAGE_LENGTH])
231 memset(result,'\0',smb_size);
232 srv_set_message(result,8,0,true);
233 SCVAL(result,smb_com,SMBlockingX);
234 SSVAL(result,smb_tid,fsp->conn->cnum);
235 SSVAL(result,smb_pid,0xFFFF);
236 SSVAL(result,smb_uid,0);
237 SSVAL(result,smb_mid,0xFFFF);
238 SCVAL(result,smb_vwv0,0xFF);
239 SSVAL(result,smb_vwv2,fsp->fnum);
240 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
241 SCVAL(result,smb_vwv3+1,cmd);
244 /****************************************************************************
245 Function to do the waiting before sending a local break.
246 ****************************************************************************/
248 static void wait_before_sending_break(void)
250 long wait_time = (long)lp_oplock_break_wait_time();
252 if (wait_time) {
253 smb_msleep(wait_time);
257 /****************************************************************************
258 Ensure that we have a valid oplock.
259 ****************************************************************************/
261 static files_struct *initial_break_processing(
262 struct smbd_server_connection *sconn, struct file_id id,
263 unsigned long file_id)
265 files_struct *fsp = NULL;
267 if( DEBUGLVL( 3 ) ) {
268 dbgtext( "initial_break_processing: called for %s/%u\n",
269 file_id_string_tos(&id), (int)file_id);
270 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
271 sconn->oplocks.exclusive_open,
272 sconn->oplocks.level_II_open);
276 * We need to search the file open table for the
277 * entry containing this dev and inode, and ensure
278 * we have an oplock on it.
281 fsp = file_find_dif(sconn, id, file_id);
283 if(fsp == NULL) {
284 /* The file could have been closed in the meantime - return success. */
285 if( DEBUGLVL( 3 ) ) {
286 dbgtext( "initial_break_processing: cannot find open file with " );
287 dbgtext( "file_id %s gen_id = %lu, ", file_id_string_tos(&id), file_id);
288 dbgtext( "allowing break to succeed.\n" );
290 return NULL;
293 /* Ensure we have an oplock on the file */
296 * There is a potential race condition in that an oplock could
297 * have been broken due to another udp request, and yet there are
298 * still oplock break messages being sent in the udp message
299 * queue for this file. So return true if we don't have an oplock,
300 * as we may have just freed it.
303 if(fsp->oplock_type == NO_OPLOCK) {
304 if( DEBUGLVL( 3 ) ) {
305 dbgtext( "initial_break_processing: file %s ",
306 fsp_str_dbg(fsp));
307 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
308 file_id_string_tos(&id), fsp->fh->gen_id );
309 dbgtext( "Allowing break to succeed regardless.\n" );
311 return NULL;
314 return fsp;
317 static void oplock_timeout_handler(struct tevent_context *ctx,
318 struct tevent_timer *te,
319 struct timeval now,
320 void *private_data)
322 files_struct *fsp = (files_struct *)private_data;
324 /* Remove the timed event handler. */
325 TALLOC_FREE(fsp->oplock_timeout);
326 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
327 fsp_str_dbg(fsp)));
328 remove_oplock(fsp);
331 /*******************************************************************
332 Add a timeout handler waiting for the client reply.
333 *******************************************************************/
335 static void add_oplock_timeout_handler(files_struct *fsp)
337 struct smbd_server_connection *sconn = fsp->conn->sconn;
338 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
341 * If kernel oplocks already notifies smbds when an oplock break times
342 * out, just return.
344 if (koplocks &&
345 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
346 return;
349 if (fsp->oplock_timeout != NULL) {
350 DEBUG(0, ("Logic problem -- have an oplock event hanging "
351 "around\n"));
354 fsp->oplock_timeout =
355 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
356 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
357 oplock_timeout_handler, fsp);
359 if (fsp->oplock_timeout == NULL) {
360 DEBUG(0, ("Could not add oplock timeout handler\n"));
364 static void send_break_message_smb1(files_struct *fsp, int level)
366 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
368 new_break_message_smb1(fsp, level, break_msg);
370 show_msg(break_msg);
371 if (!srv_send_smb(fsp->conn->sconn,
372 break_msg, false, 0,
373 IS_CONN_ENCRYPTED(fsp->conn),
374 NULL)) {
375 exit_server_cleanly("send_break_message_smb1: "
376 "srv_send_smb failed.");
380 static void break_level2_to_none_async(files_struct *fsp)
382 struct smbd_server_connection *sconn = fsp->conn->sconn;
384 if (fsp->oplock_type == NO_OPLOCK) {
385 /* We already got a "break to none" message and we've handled
386 * it. just ignore. */
387 DEBUG(3, ("process_oplock_async_level2_break_message: already "
388 "broken to none, ignoring.\n"));
389 return;
392 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
393 /* Don't tell the client, just downgrade. */
394 DEBUG(3, ("process_oplock_async_level2_break_message: "
395 "downgrading fake level 2 oplock.\n"));
396 remove_oplock(fsp);
397 return;
400 /* Ensure we're really at level2 state. */
401 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
403 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
404 "to none message for %s, file %s\n", fsp_fnum_dbg(fsp),
405 fsp_str_dbg(fsp)));
407 /* Now send a break to none message to our client. */
408 if (sconn->using_smb2) {
409 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
410 } else {
411 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
414 /* Async level2 request, don't send a reply, just remove the oplock. */
415 remove_oplock(fsp);
418 /*******************************************************************
419 This handles the case of a write triggering a break to none
420 message on a level2 oplock.
421 When we get this message we may be in any of three states :
422 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
423 the client for LEVEL2.
424 *******************************************************************/
426 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
427 void *private_data,
428 uint32_t msg_type,
429 struct server_id src,
430 DATA_BLOB *data)
432 struct share_mode_entry msg;
433 files_struct *fsp;
434 struct smbd_server_connection *sconn =
435 talloc_get_type_abort(private_data,
436 struct smbd_server_connection);
438 if (data->data == NULL) {
439 DEBUG(0, ("Got NULL buffer\n"));
440 return;
443 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
444 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
445 return;
448 /* De-linearize incoming message. */
449 message_to_share_mode_entry(&msg, (char *)data->data);
451 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
452 "%s/%llu\n", server_id_str(talloc_tos(), &src),
453 file_id_string_tos(&msg.id),
454 (unsigned long long)msg.share_file_id));
456 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
458 if (fsp == NULL) {
459 /* We hit a race here. Break messages are sent, and before we
460 * get to process this message, we have closed the file.
461 * No need to reply as this is an async message. */
462 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
463 return;
466 break_level2_to_none_async(fsp);
469 /*******************************************************************
470 This handles the generic oplock break message from another smbd.
471 *******************************************************************/
473 static void process_oplock_break_message(struct messaging_context *msg_ctx,
474 void *private_data,
475 uint32_t msg_type,
476 struct server_id src,
477 DATA_BLOB *data)
479 struct share_mode_entry msg;
480 files_struct *fsp;
481 bool break_to_level2 = False;
482 bool use_kernel;
483 struct smbd_server_connection *sconn =
484 talloc_get_type_abort(private_data,
485 struct smbd_server_connection);
486 struct server_id self = messaging_server_id(sconn->msg_ctx);
487 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
489 if (data->data == NULL) {
490 DEBUG(0, ("Got NULL buffer\n"));
491 return;
494 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
495 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
496 return;
499 /* De-linearize incoming message. */
500 message_to_share_mode_entry(&msg, (char *)data->data);
502 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
503 server_id_str(talloc_tos(), &src),
504 file_id_string_tos(&msg.id),
505 (unsigned long long)msg.share_file_id));
507 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
509 if (fsp == NULL) {
510 /* We hit a race here. Break messages are sent, and before we
511 * get to process this message, we have closed the file. */
512 DEBUG(3, ("Did not find fsp\n"));
513 return;
516 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
518 * Nothing to do anymore
520 return;
523 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
524 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
525 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
526 file_id_string_tos(&fsp->file_id),
527 fsp_str_dbg(fsp)));
528 return;
531 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
533 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
534 !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
535 !(use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
536 lp_level2_oplocks(SNUM(fsp->conn))) {
537 break_to_level2 = True;
540 /* Need to wait before sending a break
541 message if we sent ourselves this message. */
542 if (serverid_equal(&self, &src)) {
543 wait_before_sending_break();
546 if (sconn->using_smb2) {
547 send_break_message_smb2(fsp, break_to_level2 ?
548 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
549 } else {
550 send_break_message_smb1(fsp, break_to_level2 ?
551 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
554 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
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 struct smbd_server_connection *sconn =
573 talloc_get_type_abort(private_data,
574 struct smbd_server_connection);
576 if (data->data == NULL) {
577 DEBUG(0, ("Got NULL buffer\n"));
578 return;
581 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
582 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
583 return;
586 /* Pull the data from the message. */
587 pull_file_id_24((char *)data->data, &id);
588 file_id = (unsigned long)IVAL(data->data, 24);
590 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
591 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
592 (unsigned int)file_id));
594 fsp = initial_break_processing(sconn, id, file_id);
596 if (fsp == NULL) {
597 DEBUG(3, ("Got a kernel oplock break message for a file "
598 "I don't know about\n"));
599 return;
602 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
603 /* This is ok, kernel oplocks come in completely async */
604 DEBUG(3, ("Got a kernel oplock request while waiting for a "
605 "break reply\n"));
606 return;
609 if (sconn->using_smb2) {
610 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
611 } else {
612 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
615 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
617 add_oplock_timeout_handler(fsp);
620 struct break_to_none_state {
621 struct smbd_server_connection *sconn;
622 struct file_id id;
624 static void do_break_to_none(struct tevent_context *ctx,
625 struct tevent_immediate *im,
626 void *private_data);
628 /****************************************************************************
629 This function is called on any file modification or lock request. If a file
630 is level 2 oplocked then it must tell all other level 2 holders to break to
631 none.
632 ****************************************************************************/
634 static void contend_level2_oplocks_begin_default(files_struct *fsp,
635 enum level2_contention_type type)
637 struct smbd_server_connection *sconn = fsp->conn->sconn;
638 struct tevent_immediate *im;
639 struct break_to_none_state *state;
642 * If this file is level II oplocked then we need
643 * to grab the shared memory lock and inform all
644 * other files with a level II lock that they need
645 * to flush their read caches. We keep the lock over
646 * the shared memory area whilst doing this.
649 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
650 return;
653 * When we get here we might have a brlock entry locked. Also
654 * locking the share mode entry would violate the locking
655 * order. Breaking level2 oplocks to none is asynchronous
656 * anyway, so we postpone this into an immediate timed event.
659 state = talloc(sconn, struct break_to_none_state);
660 if (state == NULL) {
661 DEBUG(1, ("talloc failed\n"));
662 return;
664 state->sconn = sconn;
665 state->id = fsp->file_id;
667 im = tevent_create_immediate(state);
668 if (im == NULL) {
669 DEBUG(1, ("tevent_create_immediate failed\n"));
670 TALLOC_FREE(state);
671 return;
673 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
676 static void do_break_to_none(struct tevent_context *ctx,
677 struct tevent_immediate *im,
678 void *private_data)
680 struct break_to_none_state *state = talloc_get_type_abort(
681 private_data, struct break_to_none_state);
682 struct server_id self = messaging_server_id(state->sconn->msg_ctx);
683 int i;
684 struct share_mode_lock *lck;
686 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
687 if (lck == NULL) {
688 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
689 "share mode entry for file %s.\n",
690 file_id_string_tos(&state->id)));
691 goto done;
694 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
695 lck->data->num_share_modes ));
697 for(i = 0; i < lck->data->num_share_modes; i++) {
698 struct share_mode_entry *share_entry = &lck->data->share_modes[i];
699 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
701 if (!is_valid_share_mode_entry(share_entry)) {
702 continue;
706 * As there could have been multiple writes waiting at the
707 * lock_share_entry gate we may not be the first to
708 * enter. Hence the state of the op_types in the share mode
709 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
710 * oplock. It will do no harm to re-send break messages to
711 * those smbd's that are still waiting their turn to remove
712 * their LEVEL_II state, and also no harm to ignore existing
713 * NO_OPLOCK states. JRA.
716 DEBUG(10,("release_level_2_oplocks_on_change: "
717 "share_entry[%i]->op_type == %d\n",
718 i, share_entry->op_type ));
720 if (share_entry->op_type == NO_OPLOCK) {
721 continue;
724 /* Paranoia .... */
725 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
726 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
727 "share mode entry %d is an exlusive "
728 "oplock !\n", i ));
729 TALLOC_FREE(lck);
730 abort();
733 share_mode_entry_to_message(msg, share_entry);
736 * Deal with a race condition when breaking level2
737 * oplocks. Don't send all the messages and release
738 * the lock, this allows someone else to come in and
739 * get a level2 lock before any of the messages are
740 * processed, and thus miss getting a break message.
741 * Ensure at least one entry (the one we're breaking)
742 * is processed immediately under the lock and becomes
743 * set as NO_OPLOCK to stop any waiter getting a level2.
744 * Bugid #5980.
747 if (serverid_equal(&self, &share_entry->pid)) {
748 struct files_struct *cur_fsp =
749 initial_break_processing(state->sconn,
750 share_entry->id,
751 share_entry->share_file_id);
752 if (cur_fsp != NULL) {
753 wait_before_sending_break();
754 break_level2_to_none_async(cur_fsp);
755 } else {
756 DEBUG(3, ("release_level_2_oplocks_on_change: "
757 "Did not find fsp, ignoring\n"));
759 } else {
760 messaging_send_buf(state->sconn->msg_ctx,
761 share_entry->pid,
762 MSG_SMB_ASYNC_LEVEL2_BREAK,
763 (uint8 *)msg,
764 MSG_SMB_SHARE_MODE_ENTRY_SIZE);
768 /* We let the message receivers handle removing the oplock state
769 in the share mode lock db. */
771 TALLOC_FREE(lck);
772 done:
773 TALLOC_FREE(state);
774 return;
777 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
778 enum level2_contention_type type)
780 struct smbd_server_connection *sconn = fsp->conn->sconn;
781 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
783 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
784 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
785 return;
788 contend_level2_oplocks_begin_default(fsp, type);
791 void smbd_contend_level2_oplocks_end(files_struct *fsp,
792 enum level2_contention_type type)
794 struct smbd_server_connection *sconn = fsp->conn->sconn;
795 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
797 /* Only kernel oplocks implement this so far */
798 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
799 koplocks->ops->contend_level2_oplocks_end(fsp, type);
803 /****************************************************************************
804 Linearize a share mode entry struct to an internal oplock break message.
805 ****************************************************************************/
807 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
809 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
810 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
811 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
812 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
813 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
814 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
815 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
816 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
817 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
818 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
819 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
820 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
821 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
822 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
825 /****************************************************************************
826 De-linearize an internal oplock break message to a share mode entry struct.
827 ****************************************************************************/
829 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
831 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
832 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
833 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
834 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
835 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
836 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
837 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
838 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
839 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
840 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
841 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
842 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
843 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
844 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
847 /****************************************************************************
848 Setup oplocks for this process.
849 ****************************************************************************/
851 bool init_oplocks(struct smbd_server_connection *sconn)
853 DEBUG(3,("init_oplocks: initializing messages.\n"));
855 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
856 process_oplock_break_message);
857 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
858 process_oplock_async_level2_break_message);
859 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
860 process_kernel_oplock_break);
861 return true;
864 void init_kernel_oplocks(struct smbd_server_connection *sconn)
866 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
868 /* only initialize once */
869 if (koplocks == NULL) {
870 #if HAVE_KERNEL_OPLOCKS_IRIX
871 koplocks = irix_init_kernel_oplocks(sconn);
872 #elif HAVE_KERNEL_OPLOCKS_LINUX
873 koplocks = linux_init_kernel_oplocks(sconn);
874 #endif
875 sconn->oplocks.kernel_ops = koplocks;