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).
51 ****************************************************************************/
53 NTSTATUS set_file_oplock(files_struct *fsp, int oplock_type)
55 struct smbd_server_connection *sconn = fsp->conn->sconn;
56 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
57 bool use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
59 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
61 !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
62 DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
63 "don't support them\n"));
64 return NT_STATUS_NOT_SUPPORTED;
68 if ((fsp->oplock_type != NO_OPLOCK) &&
70 !koplocks->ops->set_oplock(koplocks, fsp, oplock_type))
72 return map_nt_error_from_unix(errno);
75 fsp->oplock_type = oplock_type;
76 fsp->sent_oplock_break = NO_BREAK_SENT;
77 if (oplock_type == LEVEL_II_OPLOCK) {
78 sconn->oplocks.level_II_open++;
79 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
80 sconn->oplocks.exclusive_open++;
83 DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
84 "tv_sec = %x, tv_usec = %x\n",
85 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
86 fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
87 (int)fsp->open_time.tv_usec ));
92 /****************************************************************************
93 Attempt to release an oplock on a file. Decrements oplock count.
94 ****************************************************************************/
96 void release_file_oplock(files_struct *fsp)
98 struct smbd_server_connection *sconn = fsp->conn->sconn;
99 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
101 if ((fsp->oplock_type != NO_OPLOCK) &&
103 koplocks->ops->release_oplock(koplocks, fsp, NO_OPLOCK);
106 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
107 sconn->oplocks.level_II_open--;
108 } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
109 sconn->oplocks.exclusive_open--;
112 SMB_ASSERT(sconn->oplocks.exclusive_open>=0);
113 SMB_ASSERT(sconn->oplocks.level_II_open>=0);
115 fsp->oplock_type = NO_OPLOCK;
116 fsp->sent_oplock_break = NO_BREAK_SENT;
118 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
119 delete_write_cache(fsp);
121 TALLOC_FREE(fsp->oplock_timeout);
124 /****************************************************************************
125 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
126 ****************************************************************************/
128 static void downgrade_file_oplock(files_struct *fsp)
130 struct smbd_server_connection *sconn = fsp->conn->sconn;
131 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
133 if (!EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
134 DEBUG(0, ("trying to downgrade an already-downgraded oplock!\n"));
139 koplocks->ops->release_oplock(koplocks, fsp, LEVEL_II_OPLOCK);
141 fsp->oplock_type = LEVEL_II_OPLOCK;
142 sconn->oplocks.exclusive_open--;
143 sconn->oplocks.level_II_open++;
144 fsp->sent_oplock_break = NO_BREAK_SENT;
146 TALLOC_FREE(fsp->oplock_timeout);
149 /****************************************************************************
150 Remove a file oplock. Copes with level II and exclusive.
151 Locks then unlocks the share mode lock. Client can decide to go directly
152 to none even if a "break-to-level II" was sent.
153 ****************************************************************************/
155 bool remove_oplock(files_struct *fsp)
158 struct share_mode_lock *lck;
160 DEBUG(10, ("remove_oplock called for %s\n",
163 /* Remove the oplock flag from the sharemode. */
164 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
166 DEBUG(0,("remove_oplock: failed to lock share entry for "
167 "file %s\n", fsp_str_dbg(fsp)));
171 if (fsp->oplock_type == LEVEL_II_OPLOCK) {
174 * If we're the only LEVEL_II holder, we have to remove the
175 * have_read_oplocks from the brlock entry
178 struct share_mode_data *data = lck->data;
179 uint32_t i, num_level2;
182 for (i=0; i<data->num_share_modes; i++) {
183 if (data->share_modes[i].op_type == LEVEL_II_OPLOCK) {
186 if (num_level2 > 1) {
188 * No need to count them all...
194 if (num_level2 == 1) {
196 * That's only us. We are dropping that level2 oplock,
197 * so remove the brlock flag.
199 struct byte_range_lock *brl;
201 brl = brl_get_locks(talloc_tos(), fsp);
203 brl_set_have_read_oplocks(brl, false);
209 ret = remove_share_oplock(lck, fsp);
211 DEBUG(0,("remove_oplock: failed to remove share oplock for "
213 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
214 file_id_string_tos(&fsp->file_id)));
216 release_file_oplock(fsp);
222 * Deal with a reply when a break-to-level II was sent.
224 bool downgrade_oplock(files_struct *fsp)
227 struct share_mode_lock *lck;
228 struct byte_range_lock *brl;
230 DEBUG(10, ("downgrade_oplock called for %s\n",
233 lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
235 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
236 "file %s\n", fsp_str_dbg(fsp)));
239 ret = downgrade_share_oplock(lck, fsp);
241 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
242 "for file %s, %s, file_id %s\n",
243 fsp_str_dbg(fsp), fsp_fnum_dbg(fsp),
244 file_id_string_tos(&fsp->file_id)));
247 downgrade_file_oplock(fsp);
249 brl = brl_get_locks(talloc_tos(), fsp);
251 brl_set_have_read_oplocks(brl, true);
259 /****************************************************************************
260 Set up an oplock break message.
261 ****************************************************************************/
263 #define SMB1_BREAK_MESSAGE_LENGTH (smb_size + 8*2)
265 static void new_break_message_smb1(files_struct *fsp, int cmd,
266 char result[SMB1_BREAK_MESSAGE_LENGTH])
268 memset(result,'\0',smb_size);
269 srv_set_message(result,8,0,true);
270 SCVAL(result,smb_com,SMBlockingX);
271 SSVAL(result,smb_tid,fsp->conn->cnum);
272 SSVAL(result,smb_pid,0xFFFF);
273 SSVAL(result,smb_uid,0);
274 SSVAL(result,smb_mid,0xFFFF);
275 SCVAL(result,smb_vwv0,0xFF);
276 SSVAL(result,smb_vwv2,fsp->fnum);
277 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
278 SCVAL(result,smb_vwv3+1,cmd);
281 /****************************************************************************
282 Function to do the waiting before sending a local break.
283 ****************************************************************************/
285 static void wait_before_sending_break(void)
287 long wait_time = (long)lp_oplock_break_wait_time();
290 smb_msleep(wait_time);
294 /****************************************************************************
295 Ensure that we have a valid oplock.
296 ****************************************************************************/
298 static files_struct *initial_break_processing(
299 struct smbd_server_connection *sconn, struct file_id id,
300 unsigned long file_id)
302 files_struct *fsp = NULL;
304 DEBUG(3, ("initial_break_processing: called for %s/%u\n"
305 "Current oplocks_open (exclusive = %d, levelII = %d)\n",
306 file_id_string_tos(&id), (int)file_id,
307 sconn->oplocks.exclusive_open,
308 sconn->oplocks.level_II_open));
311 * We need to search the file open table for the
312 * entry containing this dev and inode, and ensure
313 * we have an oplock on it.
316 fsp = file_find_dif(sconn, id, file_id);
319 /* The file could have been closed in the meantime - return success. */
320 DEBUG(3, ("initial_break_processing: cannot find open file "
321 "with file_id %s gen_id = %lu, allowing break to "
322 "succeed.\n", file_id_string_tos(&id), file_id));
326 /* Ensure we have an oplock on the file */
329 * There is a potential race condition in that an oplock could
330 * have been broken due to another udp request, and yet there are
331 * still oplock break messages being sent in the udp message
332 * queue for this file. So return true if we don't have an oplock,
333 * as we may have just freed it.
336 if(fsp->oplock_type == NO_OPLOCK) {
337 DEBUG(3, ("initial_break_processing: file %s (file_id = %s "
338 "gen_id = %lu) has no oplock. Allowing break to "
339 "succeed regardless.\n", fsp_str_dbg(fsp),
340 file_id_string_tos(&id), fsp->fh->gen_id));
347 static void oplock_timeout_handler(struct tevent_context *ctx,
348 struct tevent_timer *te,
352 files_struct *fsp = (files_struct *)private_data;
354 SMB_ASSERT(fsp->sent_oplock_break != NO_BREAK_SENT);
356 /* Remove the timed event handler. */
357 TALLOC_FREE(fsp->oplock_timeout);
358 DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n",
363 /*******************************************************************
364 Add a timeout handler waiting for the client reply.
365 *******************************************************************/
367 static void add_oplock_timeout_handler(files_struct *fsp)
369 struct smbd_server_connection *sconn = fsp->conn->sconn;
370 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
373 * If kernel oplocks already notifies smbds when an oplock break times
377 (koplocks->flags & KOPLOCKS_TIMEOUT_NOTIFICATION)) {
381 if (fsp->oplock_timeout != NULL) {
382 DEBUG(0, ("Logic problem -- have an oplock event hanging "
386 fsp->oplock_timeout =
387 tevent_add_timer(fsp->conn->sconn->ev_ctx, fsp,
388 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
389 oplock_timeout_handler, fsp);
391 if (fsp->oplock_timeout == NULL) {
392 DEBUG(0, ("Could not add oplock timeout handler\n"));
396 static void send_break_message_smb1(files_struct *fsp, int level)
398 char break_msg[SMB1_BREAK_MESSAGE_LENGTH];
400 new_break_message_smb1(fsp, level, break_msg);
403 if (!srv_send_smb(fsp->conn->sconn,
405 IS_CONN_ENCRYPTED(fsp->conn),
407 exit_server_cleanly("send_break_message_smb1: "
408 "srv_send_smb failed.");
412 static void break_level2_to_none_async(files_struct *fsp)
414 struct smbd_server_connection *sconn = fsp->conn->sconn;
416 if (fsp->oplock_type == NO_OPLOCK) {
417 /* We already got a "break to none" message and we've handled
418 * it. just ignore. */
419 DEBUG(3, ("process_oplock_async_level2_break_message: already "
420 "broken to none, ignoring.\n"));
424 /* Ensure we're really at level2 state. */
425 SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
427 DEBUG(10,("process_oplock_async_level2_break_message: sending break "
428 "to none message for %s, file %s\n", fsp_fnum_dbg(fsp),
431 /* Now send a break to none message to our client. */
432 if (sconn->using_smb2) {
433 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
435 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
438 /* Async level2 request, don't send a reply, just remove the oplock. */
442 /*******************************************************************
443 This handles the case of a write triggering a break to none
444 message on a level2 oplock.
445 When we get this message we may be in any of three states :
446 NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
447 the client for LEVEL2.
448 *******************************************************************/
450 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
453 struct server_id src,
456 struct share_mode_entry msg;
458 struct smbd_server_connection *sconn =
459 talloc_get_type_abort(private_data,
460 struct smbd_server_connection);
462 if (data->data == NULL) {
463 DEBUG(0, ("Got NULL buffer\n"));
467 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
468 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
472 /* De-linearize incoming message. */
473 message_to_share_mode_entry(&msg, (char *)data->data);
475 DEBUG(10, ("Got oplock async level 2 break message from pid %s: "
476 "%s/%llu\n", server_id_str(talloc_tos(), &src),
477 file_id_string_tos(&msg.id),
478 (unsigned long long)msg.share_file_id));
480 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
483 /* We hit a race here. Break messages are sent, and before we
484 * get to process this message, we have closed the file.
485 * No need to reply as this is an async message. */
486 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
490 break_level2_to_none_async(fsp);
493 /*******************************************************************
494 This handles the generic oplock break message from another smbd.
495 *******************************************************************/
497 static void process_oplock_break_message(struct messaging_context *msg_ctx,
500 struct server_id src,
503 struct share_mode_entry msg;
505 bool break_to_level2 = False;
507 struct smbd_server_connection *sconn =
508 talloc_get_type_abort(private_data,
509 struct smbd_server_connection);
510 struct server_id self = messaging_server_id(sconn->msg_ctx);
511 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
513 if (data->data == NULL) {
514 DEBUG(0, ("Got NULL buffer\n"));
518 if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
519 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
523 /* De-linearize incoming message. */
524 message_to_share_mode_entry(&msg, (char *)data->data);
526 DEBUG(10, ("Got oplock break message from pid %s: %s/%llu\n",
527 server_id_str(talloc_tos(), &src),
528 file_id_string_tos(&msg.id),
529 (unsigned long long)msg.share_file_id));
531 fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
534 /* We hit a race here. Break messages are sent, and before we
535 * get to process this message, we have closed the file. */
536 DEBUG(3, ("Did not find fsp\n"));
540 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
542 * Nothing to do anymore
547 if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
548 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
549 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
550 file_id_string_tos(&fsp->file_id),
555 use_kernel = lp_kernel_oplocks(SNUM(fsp->conn)) && koplocks;
557 if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
558 !(use_kernel && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) &&
559 lp_level2_oplocks(SNUM(fsp->conn))) {
560 break_to_level2 = True;
563 /* Need to wait before sending a break
564 message if we sent ourselves this message. */
565 if (serverid_equal(&self, &src)) {
566 wait_before_sending_break();
569 if (sconn->using_smb2) {
570 send_break_message_smb2(fsp, break_to_level2 ?
571 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
573 send_break_message_smb1(fsp, break_to_level2 ?
574 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
577 fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
579 add_oplock_timeout_handler(fsp);
582 /*******************************************************************
583 This handles the kernel oplock break message.
584 *******************************************************************/
586 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
589 struct server_id src,
593 unsigned long file_id;
595 struct smbd_server_connection *sconn =
596 talloc_get_type_abort(private_data,
597 struct smbd_server_connection);
599 if (data->data == NULL) {
600 DEBUG(0, ("Got NULL buffer\n"));
604 if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
605 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
609 /* Pull the data from the message. */
610 pull_file_id_24((char *)data->data, &id);
611 file_id = (unsigned long)IVAL(data->data, 24);
613 DEBUG(10, ("Got kernel oplock break message from pid %s: %s/%u\n",
614 server_id_str(talloc_tos(), &src), file_id_string_tos(&id),
615 (unsigned int)file_id));
617 fsp = initial_break_processing(sconn, id, file_id);
620 DEBUG(3, ("Got a kernel oplock break message for a file "
621 "I don't know about\n"));
625 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
626 /* This is ok, kernel oplocks come in completely async */
627 DEBUG(3, ("Got a kernel oplock request while waiting for a "
632 if (sconn->using_smb2) {
633 send_break_message_smb2(fsp, OPLOCKLEVEL_NONE);
635 send_break_message_smb1(fsp, OPLOCKLEVEL_NONE);
638 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
640 add_oplock_timeout_handler(fsp);
643 struct break_to_none_state {
644 struct smbd_server_connection *sconn;
647 static void do_break_to_none(struct tevent_context *ctx,
648 struct tevent_immediate *im,
651 /****************************************************************************
652 This function is called on any file modification or lock request. If a file
653 is level 2 oplocked then it must tell all other level 2 holders to break to
655 ****************************************************************************/
657 static void contend_level2_oplocks_begin_default(files_struct *fsp,
658 enum level2_contention_type type)
660 struct smbd_server_connection *sconn = fsp->conn->sconn;
661 struct tevent_immediate *im;
662 struct break_to_none_state *state;
663 struct byte_range_lock *brl;
666 * If this file is level II oplocked then we need
667 * to grab the shared memory lock and inform all
668 * other files with a level II lock that they need
669 * to flush their read caches. We keep the lock over
670 * the shared memory area whilst doing this.
673 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
675 * There can't be any level2 oplocks, we're alone.
680 brl = brl_get_locks_readonly(fsp);
681 if ((brl != NULL) && !brl_have_read_oplocks(brl)) {
682 DEBUG(10, ("No read oplocks around\n"));
687 * When we get here we might have a brlock entry locked. Also
688 * locking the share mode entry would violate the locking
689 * order. Breaking level2 oplocks to none is asynchronous
690 * anyway, so we postpone this into an immediate event.
693 state = talloc(sconn, struct break_to_none_state);
695 DEBUG(1, ("talloc failed\n"));
698 state->sconn = sconn;
699 state->id = fsp->file_id;
701 im = tevent_create_immediate(state);
703 DEBUG(1, ("tevent_create_immediate failed\n"));
707 tevent_schedule_immediate(im, sconn->ev_ctx, do_break_to_none, state);
710 static void do_break_to_none(struct tevent_context *ctx,
711 struct tevent_immediate *im,
714 struct break_to_none_state *state = talloc_get_type_abort(
715 private_data, struct break_to_none_state);
716 struct server_id self = messaging_server_id(state->sconn->msg_ctx);
718 struct share_mode_lock *lck;
720 lck = get_existing_share_mode_lock(talloc_tos(), state->id);
722 DEBUG(1, ("release_level_2_oplocks_on_change: failed to lock "
723 "share mode entry for file %s.\n",
724 file_id_string_tos(&state->id)));
728 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
729 lck->data->num_share_modes ));
731 for(i = 0; i < lck->data->num_share_modes; i++) {
732 struct share_mode_entry *share_entry = &lck->data->share_modes[i];
733 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
735 if (!is_valid_share_mode_entry(share_entry)) {
740 * As there could have been multiple writes waiting at the
741 * lock_share_entry gate we may not be the first to
742 * enter. Hence the state of the op_types in the share mode
743 * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
744 * oplock. It will do no harm to re-send break messages to
745 * those smbd's that are still waiting their turn to remove
746 * their LEVEL_II state, and also no harm to ignore existing
747 * NO_OPLOCK states. JRA.
750 DEBUG(10,("release_level_2_oplocks_on_change: "
751 "share_entry[%i]->op_type == %d\n",
752 i, share_entry->op_type ));
754 if (share_entry->op_type == NO_OPLOCK) {
759 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
760 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
761 "share mode entry %d is an exlusive "
767 share_mode_entry_to_message(msg, share_entry);
770 * Deal with a race condition when breaking level2
771 * oplocks. Don't send all the messages and release
772 * the lock, this allows someone else to come in and
773 * get a level2 lock before any of the messages are
774 * processed, and thus miss getting a break message.
775 * Ensure at least one entry (the one we're breaking)
776 * is processed immediately under the lock and becomes
777 * set as NO_OPLOCK to stop any waiter getting a level2.
781 if (serverid_equal(&self, &share_entry->pid)) {
782 struct files_struct *cur_fsp =
783 initial_break_processing(state->sconn,
785 share_entry->share_file_id);
786 if (cur_fsp != NULL) {
787 wait_before_sending_break();
788 break_level2_to_none_async(cur_fsp);
790 DEBUG(3, ("release_level_2_oplocks_on_change: "
791 "Did not find fsp, ignoring\n"));
794 messaging_send_buf(state->sconn->msg_ctx,
796 MSG_SMB_ASYNC_LEVEL2_BREAK,
797 (uint8 *)msg, sizeof(msg));
801 /* We let the message receivers handle removing the oplock state
802 in the share mode lock db. */
810 void smbd_contend_level2_oplocks_begin(files_struct *fsp,
811 enum level2_contention_type type)
813 struct smbd_server_connection *sconn = fsp->conn->sconn;
814 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
816 if (koplocks && koplocks->ops->contend_level2_oplocks_begin) {
817 koplocks->ops->contend_level2_oplocks_begin(fsp, type);
821 contend_level2_oplocks_begin_default(fsp, type);
824 void smbd_contend_level2_oplocks_end(files_struct *fsp,
825 enum level2_contention_type type)
827 struct smbd_server_connection *sconn = fsp->conn->sconn;
828 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
830 /* Only kernel oplocks implement this so far */
831 if (koplocks && koplocks->ops->contend_level2_oplocks_end) {
832 koplocks->ops->contend_level2_oplocks_end(fsp, type);
836 /****************************************************************************
837 Linearize a share mode entry struct to an internal oplock break message.
838 ****************************************************************************/
840 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
842 SIVAL(msg,OP_BREAK_MSG_PID_OFFSET,(uint32)e->pid.pid);
843 SBVAL(msg,OP_BREAK_MSG_MID_OFFSET,e->op_mid);
844 SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET,e->op_type);
845 SIVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET,e->access_mask);
846 SIVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET,e->share_access);
847 SIVAL(msg,OP_BREAK_MSG_PRIV_OFFSET,e->private_options);
848 SIVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET,(uint32_t)e->time.tv_sec);
849 SIVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET,(uint32_t)e->time.tv_usec);
850 push_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
851 SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
852 SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
853 SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
854 SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
855 SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
858 /****************************************************************************
859 De-linearize an internal oplock break message to a share mode entry struct.
860 ****************************************************************************/
862 void message_to_share_mode_entry(struct share_mode_entry *e, const char *msg)
864 e->pid.pid = (pid_t)IVAL(msg,OP_BREAK_MSG_PID_OFFSET);
865 e->op_mid = BVAL(msg,OP_BREAK_MSG_MID_OFFSET);
866 e->op_type = SVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET);
867 e->access_mask = IVAL(msg,OP_BREAK_MSG_ACCESS_MASK_OFFSET);
868 e->share_access = IVAL(msg,OP_BREAK_MSG_SHARE_ACCESS_OFFSET);
869 e->private_options = IVAL(msg,OP_BREAK_MSG_PRIV_OFFSET);
870 e->time.tv_sec = (time_t)IVAL(msg,OP_BREAK_MSG_TIME_SEC_OFFSET);
871 e->time.tv_usec = (int)IVAL(msg,OP_BREAK_MSG_TIME_USEC_OFFSET);
872 pull_file_id_24(msg+OP_BREAK_MSG_DEV_OFFSET, &e->id);
873 e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
874 e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
875 e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
876 e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
877 e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
880 /****************************************************************************
881 Setup oplocks for this process.
882 ****************************************************************************/
884 bool init_oplocks(struct smbd_server_connection *sconn)
886 DEBUG(3,("init_oplocks: initializing messages.\n"));
888 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_BREAK_REQUEST,
889 process_oplock_break_message);
890 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_ASYNC_LEVEL2_BREAK,
891 process_oplock_async_level2_break_message);
892 messaging_register(sconn->msg_ctx, sconn, MSG_SMB_KERNEL_BREAK,
893 process_kernel_oplock_break);
897 void init_kernel_oplocks(struct smbd_server_connection *sconn)
899 struct kernel_oplocks *koplocks = sconn->oplocks.kernel_ops;
901 /* only initialize once */
902 if (koplocks == NULL) {
903 #if HAVE_KERNEL_OPLOCKS_IRIX
904 koplocks = irix_init_kernel_oplocks(sconn);
905 #elif HAVE_KERNEL_OPLOCKS_LINUX
906 koplocks = linux_init_kernel_oplocks(sconn);
908 sconn->oplocks.kernel_ops = koplocks;