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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* Current number of oplocks we have outstanding. */
26 static int32 exclusive_oplocks_open = 0;
27 static int32 level_II_oplocks_open = 0;
28 BOOL global_client_failed_oplock_break = False;
30 extern struct timeval smb_last_time;
31 extern uint32 global_client_caps;
32 extern int smb_read_error;
34 static struct kernel_oplocks *koplocks;
36 /****************************************************************************
37 Get the number of current exclusive oplocks.
38 ****************************************************************************/
40 int32 get_number_of_exclusive_open_oplocks(void)
42 return exclusive_oplocks_open;
45 /****************************************************************************
46 Return True if an oplock message is pending.
47 ****************************************************************************/
49 BOOL oplock_message_waiting(fd_set *fds)
51 if (koplocks && koplocks->msg_waiting(fds))
57 /****************************************************************************
58 Read an oplock break message from either the oplock UDP fd or the
59 kernel (if kernel oplocks are supported).
61 If timeout is zero then *fds contains the file descriptors that
62 are ready to be read and acted upon. If timeout is non-zero then
63 *fds contains the file descriptors to be selected on for read.
64 The timeout is in milliseconds
66 ****************************************************************************/
68 void process_kernel_oplocks(void)
76 * We need to check for kernel oplocks before going into the select
77 * here, as the EINTR generated by the linux kernel oplock may have
78 * already been eaten. JRA.
85 while (koplocks->msg_waiting(&fds)) {
87 struct kernel_oplock_message msg;
89 fsp = koplocks->receive_message(&fds);
92 DEBUG(3, ("Kernel oplock message announced, but none "
98 msg.inode = fsp->inode;
99 msg.file_id = fsp->file_id;
100 message_send_pid(pid_to_procid(sys_getpid()),
101 MSG_SMB_KERNEL_BREAK,
102 &msg, sizeof(msg), True);
106 /****************************************************************************
107 Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
108 disabled (just sets flags). Returns True if oplock set.
109 ****************************************************************************/
111 BOOL set_file_oplock(files_struct *fsp, int oplock_type)
113 if (koplocks && !koplocks->set_oplock(fsp, oplock_type))
116 fsp->oplock_type = oplock_type;
117 fsp->sent_oplock_break = NO_BREAK_SENT;
118 if (oplock_type == LEVEL_II_OPLOCK)
119 level_II_oplocks_open++;
121 exclusive_oplocks_open++;
123 DEBUG(5,("set_file_oplock: granted oplock on file %s, dev = %x, inode = %.0f, file_id = %lu, \
124 tv_sec = %x, tv_usec = %x\n",
125 fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id,
126 (int)fsp->open_time.tv_sec, (int)fsp->open_time.tv_usec ));
131 /****************************************************************************
132 Attempt to release an oplock on a file. Decrements oplock count.
133 ****************************************************************************/
135 void release_file_oplock(files_struct *fsp)
137 if ((fsp->oplock_type != NO_OPLOCK) &&
138 (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
140 koplocks->release_oplock(fsp);
143 if (fsp->oplock_type == LEVEL_II_OPLOCK)
144 level_II_oplocks_open--;
145 else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
146 exclusive_oplocks_open--;
148 SMB_ASSERT(exclusive_oplocks_open>=0);
149 SMB_ASSERT(level_II_oplocks_open>=0);
151 fsp->oplock_type = NO_OPLOCK;
152 fsp->sent_oplock_break = NO_BREAK_SENT;
154 flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
157 /****************************************************************************
158 Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
159 ****************************************************************************/
161 static void downgrade_file_oplock(files_struct *fsp)
164 koplocks->release_oplock(fsp);
165 fsp->oplock_type = LEVEL_II_OPLOCK;
166 exclusive_oplocks_open--;
167 level_II_oplocks_open++;
168 fsp->sent_oplock_break = NO_BREAK_SENT;
171 /****************************************************************************
172 Remove a file oplock. Copes with level II and exclusive.
173 Locks then unlocks the share mode lock. Client can decide to go directly
174 to none even if a "break-to-level II" was sent.
175 ****************************************************************************/
177 BOOL remove_oplock(files_struct *fsp)
179 SMB_DEV_T dev = fsp->dev;
180 SMB_INO_T inode = fsp->inode;
182 struct share_mode_lock *lck;
184 /* Remove the oplock flag from the sharemode. */
185 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL);
187 DEBUG(0,("remove_oplock: failed to lock share entry for "
188 "file %s\n", fsp->fsp_name ));
191 ret = remove_share_oplock(lck, fsp);
193 DEBUG(0,("remove_oplock: failed to remove share oplock for "
194 "file %s fnum %d, dev = %x, inode = %.0f\n",
195 fsp->fsp_name, fsp->fnum, (unsigned int)dev,
198 release_file_oplock(fsp);
204 * Deal with a reply when a break-to-level II was sent.
206 BOOL downgrade_oplock(files_struct *fsp)
208 SMB_DEV_T dev = fsp->dev;
209 SMB_INO_T inode = fsp->inode;
211 struct share_mode_lock *lck;
213 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL);
215 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
216 "file %s\n", fsp->fsp_name ));
219 ret = downgrade_share_oplock(lck, fsp);
221 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
222 "for file %s fnum %d, dev = %x, inode = %.0f\n",
223 fsp->fsp_name, fsp->fnum, (unsigned int)dev,
227 downgrade_file_oplock(fsp);
232 /****************************************************************************
233 Setup the listening set of file descriptors for an oplock break
234 message either from the UDP socket or from the kernel. Returns the maximum
236 ****************************************************************************/
238 int setup_oplock_select_set( fd_set *fds)
242 if (koplocks && koplocks->notification_fd != -1) {
243 FD_SET(koplocks->notification_fd, fds);
244 maxfd = MAX(maxfd, koplocks->notification_fd);
250 /****************************************************************************
251 Set up an oplock break message.
252 ****************************************************************************/
254 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
255 files_struct *fsp, uint8_t cmd)
257 char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
259 if (result == NULL) {
260 DEBUG(0, ("talloc failed\n"));
264 memset(result,'\0',smb_size);
265 set_message(result,8,0,True);
266 SCVAL(result,smb_com,SMBlockingX);
267 SSVAL(result,smb_tid,fsp->conn->cnum);
268 SSVAL(result,smb_pid,0xFFFF);
269 SSVAL(result,smb_uid,0);
270 SSVAL(result,smb_mid,0xFFFF);
271 SCVAL(result,smb_vwv0,0xFF);
272 SSVAL(result,smb_vwv2,fsp->fnum);
273 SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
274 SCVAL(result,smb_vwv3+1,cmd);
278 /****************************************************************************
279 Function to do the waiting before sending a local break.
280 ****************************************************************************/
282 static void wait_before_sending_break(void)
284 struct timeval cur_tv;
285 long wait_left = (long)lp_oplock_break_wait_time();
290 GetTimeOfDay(&cur_tv);
292 wait_left -= ((cur_tv.tv_sec - smb_last_time.tv_sec)*1000) +
293 ((cur_tv.tv_usec - smb_last_time.tv_usec)/1000);
296 wait_left = MIN(wait_left, 1000);
297 sys_usleep(wait_left * 1000);
301 /****************************************************************************
302 Ensure that we have a valid oplock.
303 ****************************************************************************/
305 static files_struct *initial_break_processing(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
307 files_struct *fsp = NULL;
309 if( DEBUGLVL( 3 ) ) {
310 dbgtext( "initial_break_processing: called for dev = %x, inode = %.0f file_id = %lu\n",
311 (unsigned int)dev, (double)inode, file_id);
312 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
313 exclusive_oplocks_open, level_II_oplocks_open );
317 * We need to search the file open table for the
318 * entry containing this dev and inode, and ensure
319 * we have an oplock on it.
322 fsp = file_find_dif(dev, inode, file_id);
325 /* The file could have been closed in the meantime - return success. */
326 if( DEBUGLVL( 3 ) ) {
327 dbgtext( "initial_break_processing: cannot find open file with " );
328 dbgtext( "dev = %x, inode = %.0f file_id = %lu", (unsigned int)dev,
329 (double)inode, file_id);
330 dbgtext( "allowing break to succeed.\n" );
335 /* Ensure we have an oplock on the file */
338 * There is a potential race condition in that an oplock could
339 * have been broken due to another udp request, and yet there are
340 * still oplock break messages being sent in the udp message
341 * queue for this file. So return true if we don't have an oplock,
342 * as we may have just freed it.
345 if(fsp->oplock_type == NO_OPLOCK) {
346 if( DEBUGLVL( 3 ) ) {
347 dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
348 dbgtext( "(dev = %x, inode = %.0f, file_id = %lu) has no oplock.\n",
349 (unsigned int)dev, (double)inode, fsp->file_id );
350 dbgtext( "Allowing break to succeed regardless.\n" );
358 static void oplock_timeout_handler(struct timed_event *te,
359 const struct timeval *now,
362 files_struct *fsp = private_data;
364 DEBUG(0, ("Oplock break failed -- replying anyway\n"));
365 global_client_failed_oplock_break = True;
367 reply_to_oplock_break_requests(fsp);
370 static void process_oplock_break_message(int msg_type, struct process_id src,
371 void *buf, size_t len)
373 struct share_mode_entry *msg = buf;
376 BOOL break_to_level2 = False;
380 DEBUG(0, ("Got NULL buffer\n"));
384 if (len != sizeof(*msg)) {
385 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
389 DEBUG(10, ("Got oplock break message from pid %d: %d/%d/%d\n",
390 (int)procid_to_pid(&src), (int)msg->dev, (int)msg->inode,
391 (int)msg->share_file_id));
393 fsp = initial_break_processing(msg->dev, msg->inode,
397 /* We hit race here. Break messages are sent, and before we
398 * get to process this message, we have closed the file. Reply
399 * with 'ok, oplock broken' */
400 DEBUG(3, ("Did not find fsp\n"));
401 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
402 msg, sizeof(*msg), True);
406 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
407 /* Remember we have to inform the requesting PID when the
410 ADD_TO_ARRAY(NULL, struct share_mode_entry, *msg,
411 &fsp->pending_break_messages,
412 &fsp->num_pending_break_messages);
416 if (EXCLUSIVE_OPLOCK_TYPE(msg->op_type) &&
417 !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
418 DEBUG(3, ("Already downgraded oplock on %.0f/%.0f: %s\n",
419 (double)fsp->dev, (double)fsp->inode,
421 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
422 msg, sizeof(*msg), True);
426 if ((msg_type == MSG_SMB_BREAK_REQUEST) &&
427 (global_client_caps & CAP_LEVEL_II_OPLOCKS) &&
428 !koplocks && /* NOTE: we force levelII off for kernel oplocks -
429 * this will change when it is supported */
430 lp_level2_oplocks(SNUM(fsp->conn))) {
431 break_to_level2 = True;
434 break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
435 OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
436 if (break_msg == NULL) {
437 exit_server("Could not talloc break_msg\n");
440 /* Need to wait before sending a break message to a file of our own */
441 if (procid_to_pid(&src) == sys_getpid()) {
442 wait_before_sending_break();
445 /* Save the server smb signing state. */
446 sign_state = srv_oplock_set_signing(False);
449 if (!send_smb(smbd_server_fd(), break_msg)) {
450 exit_server("oplock_break: send_smb failed.");
453 /* Restore the sign state to what it was. */
454 srv_oplock_set_signing(sign_state);
456 talloc_free(break_msg);
458 if (msg_type == MSG_SMB_BREAK_REQUEST) {
459 fsp->sent_oplock_break = break_to_level2 ?
460 LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
462 /* Async level2 request, don't send a reply */
463 fsp->sent_oplock_break = ASYNC_LEVEL_II_BREAK_SENT;
466 ADD_TO_ARRAY(NULL, struct share_mode_entry, *msg,
467 &fsp->pending_break_messages,
468 &fsp->num_pending_break_messages);
470 if (fsp->oplock_timeout != NULL) {
471 DEBUG(0, ("Logic problem -- have an oplock event hanging "
475 fsp->oplock_timeout =
476 add_timed_event(NULL,
477 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
478 "oplock_timeout_handler",
479 oplock_timeout_handler, fsp);
481 if (fsp->oplock_timeout == NULL) {
482 DEBUG(0, ("Could not add oplock timeout handler\n"));
486 static void process_kernel_oplock_break(int msg_type, struct process_id src,
487 void *buf, size_t len)
489 struct kernel_oplock_message *msg = buf;
495 DEBUG(0, ("Got NULL buffer\n"));
499 if (len != sizeof(*msg)) {
500 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
504 DEBUG(10, ("Got kernel oplock break message from pid %d: %d/%d/%d\n",
505 (int)procid_to_pid(&src), (int)msg->dev, (int)msg->inode,
508 fsp = initial_break_processing(msg->dev, msg->inode, msg->file_id);
511 DEBUG(3, ("Got a kernel oplock break message for a file "
512 "I don't know about\n"));
516 if (fsp->sent_oplock_break != NO_BREAK_SENT) {
517 /* This is ok, kernel oplocks come in completely async */
518 DEBUG(3, ("Got a kernel oplock request while waiting for a "
523 break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
524 if (break_msg == NULL) {
525 exit_server("Could not talloc break_msg\n");
528 /* Save the server smb signing state. */
529 sign_state = srv_oplock_set_signing(False);
532 if (!send_smb(smbd_server_fd(), break_msg)) {
533 exit_server("oplock_break: send_smb failed.");
536 /* Restore the sign state to what it was. */
537 srv_oplock_set_signing(sign_state);
539 talloc_free(break_msg);
541 fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
544 void reply_to_oplock_break_requests(files_struct *fsp)
548 for (i=0; i<fsp->num_pending_break_messages; i++) {
549 struct share_mode_entry *msg = &fsp->pending_break_messages[i];
550 message_send_pid(msg->pid, MSG_SMB_BREAK_RESPONSE,
551 msg, sizeof(*msg), True);
554 SAFE_FREE(fsp->pending_break_messages);
555 fsp->num_pending_break_messages = 0;
556 if (fsp->oplock_timeout != NULL) {
557 talloc_free(fsp->oplock_timeout);
558 fsp->oplock_timeout = NULL;
563 static void process_oplock_break_response(int msg_type, struct process_id src,
564 void *buf, size_t len)
566 struct share_mode_entry *msg = buf;
569 DEBUG(0, ("Got NULL buffer\n"));
573 if (len != sizeof(*msg)) {
574 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
578 DEBUG(10, ("Got oplock break response from pid %d: %d/%d/%d mid %d\n",
579 (int)procid_to_pid(&src), (int)msg->dev, (int)msg->inode,
580 (int)msg->share_file_id, (int)msg->op_mid));
582 /* Here's the hack from open.c, store the mid in the 'port' field */
583 schedule_deferred_open_smb_message(msg->op_mid);
586 static void process_open_retry_message(int msg_type, struct process_id src,
587 void *buf, size_t len)
589 struct share_mode_entry *msg = buf;
592 DEBUG(0, ("Got NULL buffer\n"));
596 if (len != sizeof(*msg)) {
597 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
601 DEBUG(10, ("Got open retry msg from pid %d: %d/%d mid %d\n",
602 (int)procid_to_pid(&src), (int)msg->dev, (int)msg->inode,
605 schedule_deferred_open_smb_message(msg->op_mid);
608 /****************************************************************************
609 This function is called on any file modification or lock request. If a file
610 is level 2 oplocked then it must tell all other level 2 holders to break to
612 ****************************************************************************/
614 void release_level_2_oplocks_on_change(files_struct *fsp)
617 struct share_mode_lock *lck;
620 * If this file is level II oplocked then we need
621 * to grab the shared memory lock and inform all
622 * other files with a level II lock that they need
623 * to flush their read caches. We keep the lock over
624 * the shared memory area whilst doing this.
627 if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
630 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL);
632 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
633 "share mode entry for file %s.\n", fsp->fsp_name ));
636 DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n",
637 lck->num_share_modes ));
639 if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
640 /* See if someone else has already downgraded us, then we
641 don't have to do anything */
642 for (i=0; i<lck->num_share_modes; i++) {
643 struct share_mode_entry *e = &lck->share_modes[i];
644 if ((e->op_type == NO_OPLOCK) &&
645 (e->share_file_id == fsp->file_id) &&
646 (e->dev == fsp->dev) &&
647 (e->inode == fsp->inode) &&
648 (procid_is_me(&e->pid))) {
650 fsp->oplock_type = NO_OPLOCK;
657 for(i = 0; i < lck->num_share_modes; i++) {
658 struct share_mode_entry *share_entry = &lck->share_modes[i];
661 * As there could have been multiple writes waiting at the
662 * lock_share_entry gate we may not be the first to
663 * enter. Hence the state of the op_types in the share mode
664 * entries may be partly NO_OPLOCK and partly LEVEL_II
665 * oplock. It will do no harm to re-send break messages to
666 * those smbd's that are still waiting their turn to remove
667 * their LEVEL_II state, and also no harm to ignore existing
668 * NO_OPLOCK states. JRA.
671 DEBUG(10,("release_level_2_oplocks_on_change: "
672 "share_entry[%i]->op_type == %d\n",
673 i, share_entry->op_type ));
675 if ((share_entry->op_type == NO_OPLOCK) ||
676 (share_entry->op_type == FAKE_LEVEL_II_OPLOCK)) {
681 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
682 DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
683 "share mode entry %d is an exlusive "
689 message_send_pid(share_entry->pid, MSG_SMB_ASYNC_LEVEL2_BREAK,
690 share_entry, sizeof(*share_entry), True);
693 remove_all_share_oplocks(lck, fsp);
697 /****************************************************************************
698 Setup oplocks for this process.
699 ****************************************************************************/
701 BOOL init_oplocks(void)
703 DEBUG(3,("open_oplock_ipc: opening loopback UDP socket.\n"));
705 message_register(MSG_SMB_BREAK_REQUEST,
706 process_oplock_break_message);
707 message_register(MSG_SMB_ASYNC_LEVEL2_BREAK,
708 process_oplock_break_message);
709 message_register(MSG_SMB_BREAK_RESPONSE,
710 process_oplock_break_response);
711 message_register(MSG_SMB_KERNEL_BREAK,
712 process_kernel_oplock_break);
713 message_register(MSG_SMB_OPEN_RETRY,
714 process_open_retry_message);
716 if (lp_kernel_oplocks()) {
717 #if HAVE_KERNEL_OPLOCKS_IRIX
718 koplocks = irix_init_kernel_oplocks();
719 #elif HAVE_KERNEL_OPLOCKS_LINUX
720 koplocks = linux_init_kernel_oplocks();