2 Unix SMB/CIFS implementation.
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
24 #include "smbd/smbd.h"
25 #include "smbd/globals.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
),
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
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
) {
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
) &&
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
));
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
) &&
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
;
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"));
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
;
154 flush_write_cache(fsp
, OPLOCK_RELEASE_FLUSH
);
155 delete_write_cache(fsp
);
157 TALLOC_FREE(fsp
->oplock_timeout
);
160 /****************************************************************************
161 Remove a file oplock. Copes with level II and exclusive.
162 Locks then unlocks the share mode lock. Client can decide to go directly
163 to none even if a "break-to-level II" was sent.
164 ****************************************************************************/
166 bool remove_oplock(files_struct
*fsp
)
169 struct share_mode_lock
*lck
;
171 /* Remove the oplock flag from the sharemode. */
172 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
174 DEBUG(0,("remove_oplock: failed to lock share entry for "
175 "file %s\n", fsp_str_dbg(fsp
)));
178 ret
= remove_share_oplock(lck
, fsp
);
180 DEBUG(0,("remove_oplock: failed to remove share oplock for "
182 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
183 file_id_string_tos(&fsp
->file_id
)));
185 release_file_oplock(fsp
);
191 * Deal with a reply when a break-to-level II was sent.
193 bool downgrade_oplock(files_struct
*fsp
)
196 struct share_mode_lock
*lck
;
198 lck
= get_existing_share_mode_lock(talloc_tos(), fsp
->file_id
);
200 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
201 "file %s\n", fsp_str_dbg(fsp
)));
204 ret
= downgrade_share_oplock(lck
, fsp
);
206 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
207 "for file %s, %s, file_id %s\n",
208 fsp_str_dbg(fsp
), fsp_fnum_dbg(fsp
),
209 file_id_string_tos(&fsp
->file_id
)));
212 downgrade_file_oplock(fsp
);
218 * Some kernel oplock implementations handle the notification themselves.
220 bool should_notify_deferred_opens(struct smbd_server_connection
*sconn
)
222 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
224 (koplocks
->flags
& KOPLOCKS_DEFERRED_OPEN_NOTIFICATION
));
227 /****************************************************************************
228 Set up an oplock break message.
229 ****************************************************************************/
231 static char *new_break_message_smb1(TALLOC_CTX
*mem_ctx
,
232 files_struct
*fsp
, int cmd
)
234 char *result
= talloc_array(mem_ctx
, char, smb_size
+ 8*2 + 0);
236 if (result
== NULL
) {
237 DEBUG(0, ("talloc failed\n"));
241 memset(result
,'\0',smb_size
);
242 srv_set_message(result
,8,0,true);
243 SCVAL(result
,smb_com
,SMBlockingX
);
244 SSVAL(result
,smb_tid
,fsp
->conn
->cnum
);
245 SSVAL(result
,smb_pid
,0xFFFF);
246 SSVAL(result
,smb_uid
,0);
247 SSVAL(result
,smb_mid
,0xFFFF);
248 SCVAL(result
,smb_vwv0
,0xFF);
249 SSVAL(result
,smb_vwv2
,fsp
->fnum
);
250 SCVAL(result
,smb_vwv3
,LOCKING_ANDX_OPLOCK_RELEASE
);
251 SCVAL(result
,smb_vwv3
+1,cmd
);
255 /****************************************************************************
256 Function to do the waiting before sending a local break.
257 ****************************************************************************/
259 static void wait_before_sending_break(void)
261 long wait_time
= (long)lp_oplock_break_wait_time();
264 smb_msleep(wait_time
);
268 /****************************************************************************
269 Ensure that we have a valid oplock.
270 ****************************************************************************/
272 static files_struct
*initial_break_processing(
273 struct smbd_server_connection
*sconn
, struct file_id id
,
274 unsigned long file_id
)
276 files_struct
*fsp
= NULL
;
278 if( DEBUGLVL( 3 ) ) {
279 dbgtext( "initial_break_processing: called for %s/%u\n",
280 file_id_string_tos(&id
), (int)file_id
);
281 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
282 sconn
->oplocks
.exclusive_open
,
283 sconn
->oplocks
.level_II_open
);
287 * We need to search the file open table for the
288 * entry containing this dev and inode, and ensure
289 * we have an oplock on it.
292 fsp
= file_find_dif(sconn
, id
, file_id
);
295 /* The file could have been closed in the meantime - return success. */
296 if( DEBUGLVL( 3 ) ) {
297 dbgtext( "initial_break_processing: cannot find open file with " );
298 dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id
), file_id
);
299 dbgtext( "allowing break to succeed.\n" );
304 /* Ensure we have an oplock on the file */
307 * There is a potential race condition in that an oplock could
308 * have been broken due to another udp request, and yet there are
309 * still oplock break messages being sent in the udp message
310 * queue for this file. So return true if we don't have an oplock,
311 * as we may have just freed it.
314 if(fsp
->oplock_type
== NO_OPLOCK
) {
315 if( DEBUGLVL( 3 ) ) {
316 dbgtext( "initial_break_processing: file %s ",
318 dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
319 file_id_string_tos(&id
), fsp
->fh
->gen_id
);
320 dbgtext( "Allowing break to succeed regardless.\n" );
328 static void oplock_timeout_handler(struct event_context
*ctx
,
329 struct timed_event
*te
,
333 files_struct
*fsp
= (files_struct
*)private_data
;
335 /* Remove the timed event handler. */
336 TALLOC_FREE(fsp
->oplock_timeout
);
337 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
340 reply_to_oplock_break_requests(fsp
);
343 /*******************************************************************
344 Add a timeout handler waiting for the client reply.
345 *******************************************************************/
347 static void add_oplock_timeout_handler(files_struct
*fsp
)
349 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
350 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
353 * If kernel oplocks already notifies smbds when an oplock break times
357 (koplocks
->flags
& KOPLOCKS_TIMEOUT_NOTIFICATION
)) {
361 if (fsp
->oplock_timeout
!= NULL
) {
362 DEBUG(0, ("Logic problem -- have an oplock event hanging "
366 fsp
->oplock_timeout
=
367 tevent_add_timer(fsp
->conn
->sconn
->ev_ctx
, fsp
,
368 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT
, 0),
369 oplock_timeout_handler
, fsp
);
371 if (fsp
->oplock_timeout
== NULL
) {
372 DEBUG(0, ("Could not add oplock timeout handler\n"));
376 static void send_break_message_smb1(files_struct
*fsp
, int level
)
378 char *break_msg
= new_break_message_smb1(talloc_tos(),
381 if (break_msg
== NULL
) {
382 exit_server("Could not talloc break_msg\n");
386 if (!srv_send_smb(fsp
->conn
->sconn
,
388 IS_CONN_ENCRYPTED(fsp
->conn
),
390 exit_server_cleanly("send_break_message_smb1: "
391 "srv_send_smb failed.");
394 TALLOC_FREE(break_msg
);
397 void break_level2_to_none_async(files_struct
*fsp
)
399 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
401 if (fsp
->oplock_type
== NO_OPLOCK
) {
402 /* We already got a "break to none" message and we've handled
403 * it. just ignore. */
404 DEBUG(3, ("process_oplock_async_level2_break_message: already "
405 "broken to none, ignoring.\n"));
409 if (fsp
->oplock_type
== FAKE_LEVEL_II_OPLOCK
) {
410 /* Don't tell the client, just downgrade. */
411 DEBUG(3, ("process_oplock_async_level2_break_message: "
412 "downgrading fake level 2 oplock.\n"));
417 /* Ensure we're really at level2 state. */
418 SMB_ASSERT(fsp
->oplock_type
== LEVEL_II_OPLOCK
);
420 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
421 "to none message for %s, file %s\n", fsp_fnum_dbg(fsp
),
424 /* Now send a break to none message to our client. */
425 if (sconn
->using_smb2
) {
426 send_break_message_smb2(fsp
, OPLOCKLEVEL_NONE
);
428 send_break_message_smb1(fsp
, OPLOCKLEVEL_NONE
);
431 /* Async level2 request, don't send a reply, just remove the oplock. */
435 /*******************************************************************
436 This handles the case of a write triggering a break to none
437 message on a level2 oplock.
438 When we get this message we may be in any of three states :
439 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
440 the client for LEVEL2.
441 *******************************************************************/
443 static void process_oplock_async_level2_break_message(struct messaging_context
*msg_ctx
,
446 struct server_id src
,
449 struct share_mode_entry msg
;
451 struct smbd_server_connection
*sconn
=
452 talloc_get_type_abort(private_data
,
453 struct smbd_server_connection
);
455 if (data
->data
== NULL
) {
456 DEBUG(0, ("Got NULL buffer\n"));
460 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
461 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
465 /* De-linearize incoming message. */
466 message_to_share_mode_entry(&msg
, (char *)data
->data
);
468 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
469 "%s/%llu\n", server_id_str(talloc_tos(), &src
),
470 file_id_string_tos(&msg
.id
),
471 (unsigned long long)msg
.share_file_id
));
473 fsp
= initial_break_processing(sconn
, msg
.id
, msg
.share_file_id
);
476 /* We hit a race here. Break messages are sent, and before we
477 * get to process this message, we have closed the file.
478 * No need to reply as this is an async message. */
479 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
483 break_level2_to_none_async(fsp
);
486 /*******************************************************************
487 This handles the generic oplock break message from another smbd.
488 *******************************************************************/
490 static void process_oplock_break_message(struct messaging_context
*msg_ctx
,
493 struct server_id src
,
496 struct share_mode_entry msg
;
498 bool break_to_level2
= False
;
500 struct smbd_server_connection
*sconn
=
501 talloc_get_type_abort(private_data
,
502 struct smbd_server_connection
);
503 struct server_id self
= messaging_server_id(sconn
->msg_ctx
);
504 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
506 if (data
->data
== NULL
) {
507 DEBUG(0, ("Got NULL buffer\n"));
511 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
512 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
516 /* De-linearize incoming message. */
517 message_to_share_mode_entry(&msg
, (char *)data
->data
);
519 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
520 server_id_str(talloc_tos(), &src
),
521 file_id_string_tos(&msg
.id
),
522 (unsigned long long)msg
.share_file_id
));
524 fsp
= initial_break_processing(sconn
, msg
.id
, msg
.share_file_id
);
527 /* We hit a race here. Break messages are sent, and before we
528 * get to process this message, we have closed the file. Reply
529 * with 'ok, oplock broken' */
530 DEBUG(3, ("Did not find fsp\n"));
532 /* We just send the same message back. */
533 messaging_send_buf(msg_ctx
, src
, MSG_SMB_BREAK_RESPONSE
,
535 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
539 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
540 /* Remember we have to inform the requesting PID when the
543 ADD_TO_ARRAY(NULL
, struct share_mode_entry
, msg
,
544 &fsp
->pending_break_messages
,
545 &fsp
->num_pending_break_messages
);
549 if (EXCLUSIVE_OPLOCK_TYPE(msg
.op_type
) &&
550 !EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
551 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
552 file_id_string_tos(&fsp
->file_id
),
554 /* We just send the same message back. */
555 messaging_send_buf(msg_ctx
, src
, MSG_SMB_BREAK_RESPONSE
,
557 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
561 use_kernel
= lp_kernel_oplocks(SNUM(fsp
->conn
)) && koplocks
;
563 if ((global_client_caps
& CAP_LEVEL_II_OPLOCKS
) &&
564 !(msg
.op_type
& FORCE_OPLOCK_BREAK_TO_NONE
) &&
565 !(use_kernel
&& !(koplocks
->flags
& KOPLOCKS_LEVEL2_SUPPORTED
)) &&
566 lp_level2_oplocks(SNUM(fsp
->conn
))) {
567 break_to_level2
= True
;
570 /* Need to wait before sending a break
571 message if we sent ourselves this message. */
572 if (serverid_equal(&self
, &src
)) {
573 wait_before_sending_break();
576 if (sconn
->using_smb2
) {
577 send_break_message_smb2(fsp
, break_to_level2
?
578 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
580 send_break_message_smb1(fsp
, break_to_level2
?
581 OPLOCKLEVEL_II
: OPLOCKLEVEL_NONE
);
584 fsp
->sent_oplock_break
= break_to_level2
? LEVEL_II_BREAK_SENT
:BREAK_TO_NONE_SENT
;
587 ADD_TO_ARRAY(NULL
, struct share_mode_entry
, msg
,
588 &fsp
->pending_break_messages
,
589 &fsp
->num_pending_break_messages
);
591 add_oplock_timeout_handler(fsp
);
594 /*******************************************************************
595 This handles the kernel oplock break message.
596 *******************************************************************/
598 static void process_kernel_oplock_break(struct messaging_context
*msg_ctx
,
601 struct server_id src
,
605 unsigned long file_id
;
607 struct smbd_server_connection
*sconn
=
608 talloc_get_type_abort(private_data
,
609 struct smbd_server_connection
);
611 if (data
->data
== NULL
) {
612 DEBUG(0, ("Got NULL buffer\n"));
616 if (data
->length
!= MSG_SMB_KERNEL_BREAK_SIZE
) {
617 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
621 /* Pull the data from the message. */
622 pull_file_id_24((char *)data
->data
, &id
);
623 file_id
= (unsigned long)IVAL(data
->data
, 24);
625 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
626 server_id_str(talloc_tos(), &src
), file_id_string_tos(&id
),
627 (unsigned int)file_id
));
629 fsp
= initial_break_processing(sconn
, id
, file_id
);
632 DEBUG(3, ("Got a kernel oplock break message for a file "
633 "I don't know about\n"));
637 if (fsp
->sent_oplock_break
!= NO_BREAK_SENT
) {
638 /* This is ok, kernel oplocks come in completely async */
639 DEBUG(3, ("Got a kernel oplock request while waiting for a "
644 if (sconn
->using_smb2
) {
645 send_break_message_smb2(fsp
, OPLOCKLEVEL_NONE
);
647 send_break_message_smb1(fsp
, OPLOCKLEVEL_NONE
);
650 fsp
->sent_oplock_break
= BREAK_TO_NONE_SENT
;
652 add_oplock_timeout_handler(fsp
);
655 void reply_to_oplock_break_requests(files_struct
*fsp
)
657 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
658 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
662 * If kernel oplocks already notifies smbds when oplocks are
663 * broken/removed, just return.
666 (koplocks
->flags
& KOPLOCKS_OPLOCK_BROKEN_NOTIFICATION
)) {
670 for (i
=0; i
<fsp
->num_pending_break_messages
; i
++) {
671 struct share_mode_entry
*e
= &fsp
->pending_break_messages
[i
];
672 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
674 share_mode_entry_to_message(msg
, e
);
676 messaging_send_buf(fsp
->conn
->sconn
->msg_ctx
, e
->pid
,
677 MSG_SMB_BREAK_RESPONSE
,
679 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
682 SAFE_FREE(fsp
->pending_break_messages
);
683 fsp
->num_pending_break_messages
= 0;
684 TALLOC_FREE(fsp
->oplock_timeout
);
688 static void process_oplock_break_response(struct messaging_context
*msg_ctx
,
691 struct server_id src
,
694 struct share_mode_entry msg
;
695 struct smbd_server_connection
*sconn
=
696 talloc_get_type_abort(private_data
,
697 struct smbd_server_connection
);
699 if (data
->data
== NULL
) {
700 DEBUG(0, ("Got NULL buffer\n"));
704 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
705 DEBUG(0, ("Got invalid msg len %u\n",
706 (unsigned int)data
->length
));
710 /* De-linearize incoming message. */
711 message_to_share_mode_entry(&msg
, (char *)data
->data
);
713 DEBUG(10, ("Got oplock break response from pid %s: %s/%llu mid %llu\n",
714 server_id_str(talloc_tos(), &src
),
715 file_id_string_tos(&msg
.id
),
716 (unsigned long long)msg
.share_file_id
,
717 (unsigned long long)msg
.op_mid
));
719 schedule_deferred_open_message_smb(sconn
, msg
.op_mid
);
722 static void process_open_retry_message(struct messaging_context
*msg_ctx
,
725 struct server_id src
,
728 struct share_mode_entry msg
;
729 struct smbd_server_connection
*sconn
=
730 talloc_get_type_abort(private_data
,
731 struct smbd_server_connection
);
733 if (data
->data
== NULL
) {
734 DEBUG(0, ("Got NULL buffer\n"));
738 if (data
->length
!= MSG_SMB_SHARE_MODE_ENTRY_SIZE
) {
739 DEBUG(0, ("Got invalid msg len %d\n", (int)data
->length
));
743 /* De-linearize incoming message. */
744 message_to_share_mode_entry(&msg
, (char *)data
->data
);
746 DEBUG(10, ("Got open retry msg from pid %s: %s mid %llu\n",
747 server_id_str(talloc_tos(), &src
), file_id_string_tos(&msg
.id
),
748 (unsigned long long)msg
.op_mid
));
750 schedule_deferred_open_message_smb(sconn
, msg
.op_mid
);
753 struct break_to_none_state
{
754 struct smbd_server_connection
*sconn
;
757 static void do_break_to_none(struct tevent_req
*req
);
759 /****************************************************************************
760 This function is called on any file modification or lock request. If a file
761 is level 2 oplocked then it must tell all other level 2 holders to break to
763 ****************************************************************************/
765 static void contend_level2_oplocks_begin_default(files_struct
*fsp
,
766 enum level2_contention_type type
)
768 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
769 struct tevent_req
*req
;
770 struct break_to_none_state
*state
;
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
))
784 * When we get here we might have a brlock entry locked. Also
785 * locking the share mode entry would violate the locking
786 * order. Breaking level2 oplocks to none is asynchronous
787 * anyway, so we postpone this into an immediate timed event.
790 state
= talloc(sconn
, struct break_to_none_state
);
792 DEBUG(1, ("talloc failed\n"));
795 state
->sconn
= sconn
;
796 state
->id
= fsp
->file_id
;
798 req
= tevent_wakeup_send(state
, sconn
->ev_ctx
, timeval_set(0, 0));
800 DEBUG(1, ("tevent_wakeup_send failed\n"));
804 tevent_req_set_callback(req
, do_break_to_none
, state
);
808 static void do_break_to_none(struct tevent_req
*req
)
810 struct break_to_none_state
*state
= tevent_req_callback_data(
811 req
, struct break_to_none_state
);
812 struct server_id self
= messaging_server_id(state
->sconn
->msg_ctx
);
815 struct share_mode_lock
*lck
;
817 ret
= tevent_wakeup_recv(req
);
820 DEBUG(1, ("tevent_wakeup_recv failed\n"));
823 lck
= get_existing_share_mode_lock(talloc_tos(), state
->id
);
825 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
826 "share mode entry for file %s.\n",
827 file_id_string_tos(&state
->id
)));
831 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
832 lck
->data
->num_share_modes
));
834 for(i
= 0; i
< lck
->data
->num_share_modes
; i
++) {
835 struct share_mode_entry
*share_entry
= &lck
->data
->share_modes
[i
];
836 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
838 if (!is_valid_share_mode_entry(share_entry
)) {
843 * As there could have been multiple writes waiting at the
844 * lock_share_entry gate we may not be the first to
845 * enter. Hence the state of the op_types in the share mode
846 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
847 * oplock. It will do no harm to re-send break messages to
848 * those smbd's that are still waiting their turn to remove
849 * their LEVEL_II state, and also no harm to ignore existing
850 * NO_OPLOCK states. JRA.
853 DEBUG(10,("release_level_2_oplocks_on_change: "
854 "share_entry[%i]->op_type == %d\n",
855 i
, share_entry
->op_type
));
857 if (share_entry
->op_type
== NO_OPLOCK
) {
862 if (EXCLUSIVE_OPLOCK_TYPE(share_entry
->op_type
)) {
863 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
864 "share mode entry %d is an exlusive "
870 share_mode_entry_to_message(msg
, share_entry
);
873 * Deal with a race condition when breaking level2
874 * oplocks. Don't send all the messages and release
875 * the lock, this allows someone else to come in and
876 * get a level2 lock before any of the messages are
877 * processed, and thus miss getting a break message.
878 * Ensure at least one entry (the one we're breaking)
879 * is processed immediately under the lock and becomes
880 * set as NO_OPLOCK to stop any waiter getting a level2.
884 if (serverid_equal(&self
, &share_entry
->pid
)) {
885 struct files_struct
*cur_fsp
=
886 initial_break_processing(state
->sconn
,
888 share_entry
->share_file_id
);
889 wait_before_sending_break();
890 if (cur_fsp
!= NULL
) {
891 break_level2_to_none_async(cur_fsp
);
893 DEBUG(3, ("release_level_2_oplocks_on_change: "
894 "Did not find fsp, ignoring\n"));
897 messaging_send_buf(state
->sconn
->msg_ctx
,
899 MSG_SMB_ASYNC_LEVEL2_BREAK
,
901 MSG_SMB_SHARE_MODE_ENTRY_SIZE
);
905 /* We let the message receivers handle removing the oplock state
906 in the share mode lock db. */
914 void smbd_contend_level2_oplocks_begin(files_struct
*fsp
,
915 enum level2_contention_type type
)
917 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
918 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
920 if (koplocks
&& koplocks
->ops
->contend_level2_oplocks_begin
) {
921 koplocks
->ops
->contend_level2_oplocks_begin(fsp
, type
);
925 contend_level2_oplocks_begin_default(fsp
, type
);
928 void smbd_contend_level2_oplocks_end(files_struct
*fsp
,
929 enum level2_contention_type type
)
931 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
932 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
934 /* Only kernel oplocks implement this so far */
935 if (koplocks
&& koplocks
->ops
->contend_level2_oplocks_end
) {
936 koplocks
->ops
->contend_level2_oplocks_end(fsp
, type
);
940 /****************************************************************************
941 Linearize a share mode entry struct to an internal oplock break message.
942 ****************************************************************************/
944 void share_mode_entry_to_message(char *msg
, const struct share_mode_entry
*e
)
946 SIVAL(msg
,OP_BREAK_MSG_PID_OFFSET
,(uint32
)e
->pid
.pid
);
947 SBVAL(msg
,OP_BREAK_MSG_MID_OFFSET
,e
->op_mid
);
948 SSVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
,e
->op_type
);
949 SIVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
,e
->access_mask
);
950 SIVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
,e
->share_access
);
951 SIVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
,e
->private_options
);
952 SIVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
,(uint32_t)e
->time
.tv_sec
);
953 SIVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
,(uint32_t)e
->time
.tv_usec
);
954 push_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
955 SIVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
,e
->share_file_id
);
956 SIVAL(msg
,OP_BREAK_MSG_UID_OFFSET
,e
->uid
);
957 SSVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
,e
->flags
);
958 SIVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
,e
->name_hash
);
959 SIVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
,e
->pid
.vnn
);
962 /****************************************************************************
963 De-linearize an internal oplock break message to a share mode entry struct.
964 ****************************************************************************/
966 void message_to_share_mode_entry(struct share_mode_entry
*e
, char *msg
)
968 e
->pid
.pid
= (pid_t
)IVAL(msg
,OP_BREAK_MSG_PID_OFFSET
);
969 e
->op_mid
= BVAL(msg
,OP_BREAK_MSG_MID_OFFSET
);
970 e
->op_type
= SVAL(msg
,OP_BREAK_MSG_OP_TYPE_OFFSET
);
971 e
->access_mask
= IVAL(msg
,OP_BREAK_MSG_ACCESS_MASK_OFFSET
);
972 e
->share_access
= IVAL(msg
,OP_BREAK_MSG_SHARE_ACCESS_OFFSET
);
973 e
->private_options
= IVAL(msg
,OP_BREAK_MSG_PRIV_OFFSET
);
974 e
->time
.tv_sec
= (time_t)IVAL(msg
,OP_BREAK_MSG_TIME_SEC_OFFSET
);
975 e
->time
.tv_usec
= (int)IVAL(msg
,OP_BREAK_MSG_TIME_USEC_OFFSET
);
976 pull_file_id_24(msg
+OP_BREAK_MSG_DEV_OFFSET
, &e
->id
);
977 e
->share_file_id
= (unsigned long)IVAL(msg
,OP_BREAK_MSG_FILE_ID_OFFSET
);
978 e
->uid
= (uint32
)IVAL(msg
,OP_BREAK_MSG_UID_OFFSET
);
979 e
->flags
= (uint16
)SVAL(msg
,OP_BREAK_MSG_FLAGS_OFFSET
);
980 e
->name_hash
= IVAL(msg
,OP_BREAK_MSG_NAME_HASH_OFFSET
);
981 e
->pid
.vnn
= IVAL(msg
,OP_BREAK_MSG_VNN_OFFSET
);
984 /****************************************************************************
985 Setup oplocks for this process.
986 ****************************************************************************/
988 bool init_oplocks(struct smbd_server_connection
*sconn
)
990 DEBUG(3,("init_oplocks: initializing messages.\n"));
992 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_BREAK_REQUEST
,
993 process_oplock_break_message
);
994 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_ASYNC_LEVEL2_BREAK
,
995 process_oplock_async_level2_break_message
);
996 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_BREAK_RESPONSE
,
997 process_oplock_break_response
);
998 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_KERNEL_BREAK
,
999 process_kernel_oplock_break
);
1000 messaging_register(sconn
->msg_ctx
, sconn
, MSG_SMB_OPEN_RETRY
,
1001 process_open_retry_message
);
1006 void init_kernel_oplocks(struct smbd_server_connection
*sconn
)
1008 struct kernel_oplocks
*koplocks
= sconn
->oplocks
.kernel_ops
;
1010 /* only initialize once */
1011 if (koplocks
== NULL
) {
1012 #if HAVE_KERNEL_OPLOCKS_IRIX
1013 koplocks
= irix_init_kernel_oplocks(sconn
);
1014 #elif HAVE_KERNEL_OPLOCKS_LINUX
1015 koplocks
= linux_init_kernel_oplocks(sconn
);
1017 sconn
->oplocks
.kernel_ops
= koplocks
;