Merge branch 'master' of ssh://git.samba.org/data/git/samba
[kai/samba.git] / source3 / smbd / oplock.c
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
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #define DBGC_CLASS DBGC_LOCKING
23 #include "includes.h"
24 #include "smbd/globals.h"
25
26 /****************************************************************************
27  Get the number of current exclusive oplocks.
28 ****************************************************************************/
29
30 int32 get_number_of_exclusive_open_oplocks(void)
31 {
32   return exclusive_oplocks_open;
33 }
34
35 /****************************************************************************
36  Return True if an oplock message is pending.
37 ****************************************************************************/
38
39 bool oplock_message_waiting(void)
40 {
41         if (koplocks && koplocks->ops->msg_waiting(koplocks)) {
42                 return True;
43         }
44
45         return False;
46 }
47
48 /****************************************************************************
49  Find out if there are any kernel oplock messages waiting and process them
50  if so. pfds is the fd_set from the main select loop (which contains any
51  kernel oplock fd if that's what the system uses (IRIX). If may be NULL if
52  we're calling this in a shutting down state.
53 ****************************************************************************/
54
55 void process_kernel_oplocks(struct messaging_context *msg_ctx)
56 {
57         /*
58          * We need to check for kernel oplocks before going into the select
59          * here, as the EINTR generated by the linux kernel oplock may have
60          * already been eaten. JRA.
61          */
62
63         if (!koplocks) {
64                 return;
65         }
66
67         while (koplocks->ops->msg_waiting(koplocks)) {
68                 files_struct *fsp;
69                 char msg[MSG_SMB_KERNEL_BREAK_SIZE];
70
71                 fsp = koplocks->ops->receive_message(koplocks);
72
73                 if (fsp == NULL) {
74                         DEBUG(3, ("Kernel oplock message announced, but none "
75                                   "received\n"));
76                         return;
77                 }
78
79                 /* Put the kernel break info into the message. */
80                 push_file_id_16(msg, &fsp->file_id);
81                 SIVAL(msg,16,fsp->fh->gen_id);
82
83                 /* Don't need to be root here as we're only ever
84                    sending to ourselves. */
85
86                 messaging_send_buf(msg_ctx, procid_self(),
87                                    MSG_SMB_KERNEL_BREAK,
88                                    (uint8 *)&msg, MSG_SMB_KERNEL_BREAK_SIZE);
89         }
90 }
91
92 /****************************************************************************
93  Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
94  disabled (just sets flags). Returns True if oplock set.
95 ****************************************************************************/
96
97 bool set_file_oplock(files_struct *fsp, int oplock_type)
98 {
99         if ((fsp->oplock_type != NO_OPLOCK) &&
100             (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
101             koplocks &&
102             !koplocks->ops->set_oplock(koplocks, fsp, oplock_type)) {
103                 return False;
104         }
105
106         fsp->oplock_type = oplock_type;
107         fsp->sent_oplock_break = NO_BREAK_SENT;
108         if (oplock_type == LEVEL_II_OPLOCK) {
109                 level_II_oplocks_open++;
110         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
111                 exclusive_oplocks_open++;
112         }
113
114         DEBUG(5,("set_file_oplock: granted oplock on file %s, %s/%lu, "
115                     "tv_sec = %x, tv_usec = %x\n",
116                  fsp->fsp_name, file_id_string_tos(&fsp->file_id),
117                  fsp->fh->gen_id, (int)fsp->open_time.tv_sec,
118                  (int)fsp->open_time.tv_usec ));
119
120         return True;
121 }
122
123 /****************************************************************************
124  Attempt to release an oplock on a file. Decrements oplock count.
125 ****************************************************************************/
126
127 void release_file_oplock(files_struct *fsp)
128 {
129         if ((fsp->oplock_type != NO_OPLOCK) &&
130             (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
131             koplocks) {
132                 koplocks->ops->release_oplock(koplocks, fsp);
133         }
134
135         if (fsp->oplock_type == LEVEL_II_OPLOCK) {
136                 level_II_oplocks_open--;
137         } else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
138                 exclusive_oplocks_open--;
139         }
140
141         SMB_ASSERT(exclusive_oplocks_open>=0);
142         SMB_ASSERT(level_II_oplocks_open>=0);
143
144         if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
145                 /* This doesn't matter for close. */
146                 fsp->oplock_type = FAKE_LEVEL_II_OPLOCK;
147         } else {
148                 fsp->oplock_type = NO_OPLOCK;
149         }
150         fsp->sent_oplock_break = NO_BREAK_SENT;
151
152         flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
153
154         TALLOC_FREE(fsp->oplock_timeout);
155 }
156
157 /****************************************************************************
158  Attempt to downgrade an oplock on a file. Doesn't decrement oplock count.
159 ****************************************************************************/
160
161 static void downgrade_file_oplock(files_struct *fsp)
162 {
163         if (koplocks) {
164                 koplocks->ops->release_oplock(koplocks, fsp);
165         }
166         fsp->oplock_type = LEVEL_II_OPLOCK;
167         exclusive_oplocks_open--;
168         level_II_oplocks_open++;
169         fsp->sent_oplock_break = NO_BREAK_SENT;
170 }
171
172 /****************************************************************************
173  Remove a file oplock. Copes with level II and exclusive.
174  Locks then unlocks the share mode lock. Client can decide to go directly
175  to none even if a "break-to-level II" was sent.
176 ****************************************************************************/
177
178 bool remove_oplock(files_struct *fsp)
179 {
180         bool ret;
181         struct share_mode_lock *lck;
182
183         /* Remove the oplock flag from the sharemode. */
184         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
185                                   NULL);
186         if (lck == NULL) {
187                 DEBUG(0,("remove_oplock: failed to lock share entry for "
188                          "file %s\n", fsp->fsp_name ));
189                 return False;
190         }
191         ret = remove_share_oplock(lck, fsp);
192         if (!ret) {
193                 DEBUG(0,("remove_oplock: failed to remove share oplock for "
194                          "file %s fnum %d, %s\n",
195                          fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
196         }
197         release_file_oplock(fsp);
198         TALLOC_FREE(lck);
199         return ret;
200 }
201
202 /*
203  * Deal with a reply when a break-to-level II was sent.
204  */
205 bool downgrade_oplock(files_struct *fsp)
206 {
207         bool ret;
208         struct share_mode_lock *lck;
209
210         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
211                                   NULL);
212         if (lck == NULL) {
213                 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
214                          "file %s\n", fsp->fsp_name ));
215                 return False;
216         }
217         ret = downgrade_share_oplock(lck, fsp);
218         if (!ret) {
219                 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
220                          "for file %s fnum %d, file_id %s\n",
221                          fsp->fsp_name, fsp->fnum, file_id_string_tos(&fsp->file_id)));
222         }
223
224         downgrade_file_oplock(fsp);
225         TALLOC_FREE(lck);
226         return ret;
227 }
228
229 /****************************************************************************
230  Set up an oplock break message.
231 ****************************************************************************/
232
233 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
234                                    files_struct *fsp, uint8 cmd)
235 {
236         char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
237
238         if (result == NULL) {
239                 DEBUG(0, ("talloc failed\n"));
240                 return NULL;
241         }
242
243         memset(result,'\0',smb_size);
244         srv_set_message(result,8,0,true);
245         SCVAL(result,smb_com,SMBlockingX);
246         SSVAL(result,smb_tid,fsp->conn->cnum);
247         SSVAL(result,smb_pid,0xFFFF);
248         SSVAL(result,smb_uid,0);
249         SSVAL(result,smb_mid,0xFFFF);
250         SCVAL(result,smb_vwv0,0xFF);
251         SSVAL(result,smb_vwv2,fsp->fnum);
252         SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
253         SCVAL(result,smb_vwv3+1,cmd);
254         return result;
255 }
256
257 /****************************************************************************
258  Function to do the waiting before sending a local break.
259 ****************************************************************************/
260
261 static void wait_before_sending_break(void)
262 {
263         long wait_time = (long)lp_oplock_break_wait_time();
264
265         if (wait_time) {
266                 smb_msleep(wait_time);
267         }
268 }
269
270 /****************************************************************************
271  Ensure that we have a valid oplock.
272 ****************************************************************************/
273
274 static files_struct *initial_break_processing(struct file_id id, unsigned long file_id)
275 {
276         files_struct *fsp = NULL;
277
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                         exclusive_oplocks_open, level_II_oplocks_open );
283         }
284
285         /*
286          * We need to search the file open table for the
287          * entry containing this dev and inode, and ensure
288          * we have an oplock on it.
289          */
290
291         fsp = file_find_dif(id, file_id);
292
293         if(fsp == NULL) {
294                 /* The file could have been closed in the meantime - return success. */
295                 if( DEBUGLVL( 3 ) ) {
296                         dbgtext( "initial_break_processing: cannot find open file with " );
297                         dbgtext( "file_id %s gen_id = %lu", file_id_string_tos(&id), file_id);
298                         dbgtext( "allowing break to succeed.\n" );
299                 }
300                 return NULL;
301         }
302
303         /* Ensure we have an oplock on the file */
304
305         /*
306          * There is a potential race condition in that an oplock could
307          * have been broken due to another udp request, and yet there are
308          * still oplock break messages being sent in the udp message
309          * queue for this file. So return true if we don't have an oplock,
310          * as we may have just freed it.
311          */
312
313         if(fsp->oplock_type == NO_OPLOCK) {
314                 if( DEBUGLVL( 3 ) ) {
315                         dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
316                         dbgtext( "(file_id = %s gen_id = %lu) has no oplock.\n",
317                                  file_id_string_tos(&id), fsp->fh->gen_id );
318                         dbgtext( "Allowing break to succeed regardless.\n" );
319                 }
320                 return NULL;
321         }
322
323         return fsp;
324 }
325
326 static void oplock_timeout_handler(struct event_context *ctx,
327                                    struct timed_event *te,
328                                    struct timeval now,
329                                    void *private_data)
330 {
331         files_struct *fsp = (files_struct *)private_data;
332
333         /* Remove the timed event handler. */
334         TALLOC_FREE(fsp->oplock_timeout);
335         DEBUG(0, ("Oplock break failed for file %s -- replying anyway\n", fsp->fsp_name));
336         global_client_failed_oplock_break = True;
337         remove_oplock(fsp);
338         reply_to_oplock_break_requests(fsp);
339 }
340
341 /*******************************************************************
342  Add a timeout handler waiting for the client reply.
343 *******************************************************************/
344
345 static void add_oplock_timeout_handler(files_struct *fsp)
346 {
347         if (fsp->oplock_timeout != NULL) {
348                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
349                           "around\n"));
350         }
351
352         fsp->oplock_timeout =
353                 event_add_timed(smbd_event_context(), NULL,
354                                 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
355                                 oplock_timeout_handler, fsp);
356
357         if (fsp->oplock_timeout == NULL) {
358                 DEBUG(0, ("Could not add oplock timeout handler\n"));
359         }
360 }
361
362 /*******************************************************************
363  This handles the case of a write triggering a break to none
364  message on a level2 oplock.
365  When we get this message we may be in any of three states :
366  NO_OPLOCK, LEVEL_II, FAKE_LEVEL2. We only send a message to
367  the client for LEVEL2.
368 *******************************************************************/
369
370 static void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx,
371                                                       void *private_data,
372                                                       uint32_t msg_type,
373                                                       struct server_id src,
374                                                       DATA_BLOB *data)
375 {
376         struct share_mode_entry msg;
377         files_struct *fsp;
378         char *break_msg;
379         bool sign_state;
380
381         if (data->data == NULL) {
382                 DEBUG(0, ("Got NULL buffer\n"));
383                 return;
384         }
385
386         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
387                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
388                 return;
389         }
390
391         /* De-linearize incoming message. */
392         message_to_share_mode_entry(&msg, (char *)data->data);
393
394         DEBUG(10, ("Got oplock async level 2 break message from pid %d: %s/%lu\n",
395                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
396
397         fsp = initial_break_processing(msg.id, msg.share_file_id);
398
399         if (fsp == NULL) {
400                 /* We hit a race here. Break messages are sent, and before we
401                  * get to process this message, we have closed the file. 
402                  * No need to reply as this is an async message. */
403                 DEBUG(3, ("process_oplock_async_level2_break_message: Did not find fsp, ignoring\n"));
404                 return;
405         }
406
407         if (fsp->oplock_type == NO_OPLOCK) {
408                 /* We already got a "break to none" message and we've handled it.
409                  * just ignore. */
410                 DEBUG(3, ("process_oplock_async_level2_break_message: already broken to none, ignoring.\n"));
411                 return;
412         }
413
414         if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
415                 /* Don't tell the client, just downgrade. */
416                 DEBUG(3, ("process_oplock_async_level2_break_message: downgrading fake level 2 oplock.\n"));
417                 remove_oplock(fsp);
418                 return;
419         }
420
421         /* Ensure we're really at level2 state. */
422         SMB_ASSERT(fsp->oplock_type == LEVEL_II_OPLOCK);
423
424         DEBUG(10,("process_oplock_async_level2_break_message: sending break to "
425                 "none message for fid %d, file %s\n",
426                 fsp->fnum,
427                 fsp->fsp_name));
428
429         /* Now send a break to none message to our client. */
430
431         break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
432         if (break_msg == NULL) {
433                 exit_server("Could not talloc break_msg\n");
434         }
435
436         /* Need to wait before sending a break message if we sent ourselves this message. */
437         if (procid_to_pid(&src) == sys_getpid()) {
438                 wait_before_sending_break();
439         }
440
441         /* Save the server smb signing state. */
442         sign_state = srv_oplock_set_signing(False);
443
444         show_msg(break_msg);
445         if (!srv_send_smb(smbd_server_fd(),
446                         break_msg,
447                         IS_CONN_ENCRYPTED(fsp->conn))) {
448                 exit_server_cleanly("oplock_break: srv_send_smb failed.");
449         }
450
451         /* Restore the sign state to what it was. */
452         srv_oplock_set_signing(sign_state);
453
454         TALLOC_FREE(break_msg);
455
456         /* Async level2 request, don't send a reply, just remove the oplock. */
457         remove_oplock(fsp);
458 }
459
460 /*******************************************************************
461  This handles the generic oplock break message from another smbd.
462 *******************************************************************/
463
464 static void process_oplock_break_message(struct messaging_context *msg_ctx,
465                                          void *private_data,
466                                          uint32_t msg_type,
467                                          struct server_id src,
468                                          DATA_BLOB *data)
469 {
470         struct share_mode_entry msg;
471         files_struct *fsp;
472         char *break_msg;
473         bool break_to_level2 = False;
474         bool sign_state;
475
476         if (data->data == NULL) {
477                 DEBUG(0, ("Got NULL buffer\n"));
478                 return;
479         }
480
481         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
482                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
483                 return;
484         }
485
486         /* De-linearize incoming message. */
487         message_to_share_mode_entry(&msg, (char *)data->data);
488
489         DEBUG(10, ("Got oplock break message from pid %d: %s/%lu\n",
490                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id));
491
492         fsp = initial_break_processing(msg.id, msg.share_file_id);
493
494         if (fsp == NULL) {
495                 /* a We hit race here. Break messages are sent, and before we
496                  * get to process this message, we have closed the file. Reply
497                  * with 'ok, oplock broken' */
498                 DEBUG(3, ("Did not find fsp\n"));
499
500                 /* We just send the same message back. */
501                 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
502                                    (uint8 *)data->data,
503                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
504                 return;
505         }
506
507         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
508                 /* Remember we have to inform the requesting PID when the
509                  * client replies */
510                 msg.pid = src;
511                 ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
512                              &fsp->pending_break_messages,
513                              &fsp->num_pending_break_messages);
514                 return;
515         }
516
517         if (EXCLUSIVE_OPLOCK_TYPE(msg.op_type) &&
518             !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
519                 DEBUG(3, ("Already downgraded oplock on %s: %s\n",
520                           file_id_string_tos(&fsp->file_id),
521                           fsp->fsp_name));
522                 /* We just send the same message back. */
523                 messaging_send_buf(msg_ctx, src, MSG_SMB_BREAK_RESPONSE,
524                                    (uint8 *)data->data,
525                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
526                 return;
527         }
528
529         if ((global_client_caps & CAP_LEVEL_II_OPLOCKS) && 
530             !(msg.op_type & FORCE_OPLOCK_BREAK_TO_NONE) &&
531             !koplocks && /* NOTE: we force levelII off for kernel oplocks -
532                           * this will change when it is supported */
533             lp_level2_oplocks(SNUM(fsp->conn))) {
534                 break_to_level2 = True;
535         }
536
537         break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
538                                           OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
539         if (break_msg == NULL) {
540                 exit_server("Could not talloc break_msg\n");
541         }
542
543         /* Need to wait before sending a break message if we sent ourselves this message. */
544         if (procid_to_pid(&src) == sys_getpid()) {
545                 wait_before_sending_break();
546         }
547
548         /* Save the server smb signing state. */
549         sign_state = srv_oplock_set_signing(False);
550
551         show_msg(break_msg);
552         if (!srv_send_smb(smbd_server_fd(),
553                         break_msg,
554                         IS_CONN_ENCRYPTED(fsp->conn))) {
555                 exit_server_cleanly("oplock_break: srv_send_smb failed.");
556         }
557
558         /* Restore the sign state to what it was. */
559         srv_oplock_set_signing(sign_state);
560
561         TALLOC_FREE(break_msg);
562
563         fsp->sent_oplock_break = break_to_level2 ? LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
564
565         msg.pid = src;
566         ADD_TO_ARRAY(NULL, struct share_mode_entry, msg,
567                      &fsp->pending_break_messages,
568                      &fsp->num_pending_break_messages);
569
570         add_oplock_timeout_handler(fsp);
571 }
572
573 /*******************************************************************
574  This handles the kernel oplock break message.
575 *******************************************************************/
576
577 static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
578                                         void *private_data,
579                                         uint32_t msg_type,
580                                         struct server_id src,
581                                         DATA_BLOB *data)
582 {
583         struct file_id id;
584         unsigned long file_id;
585         files_struct *fsp;
586         char *break_msg;
587         bool sign_state;
588
589         if (data->data == NULL) {
590                 DEBUG(0, ("Got NULL buffer\n"));
591                 return;
592         }
593
594         if (data->length != MSG_SMB_KERNEL_BREAK_SIZE) {
595                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
596                 return;
597         }
598
599         /* Pull the data from the message. */
600         pull_file_id_16((char *)data->data, &id);
601         file_id = (unsigned long)IVAL(data->data, 16);
602
603         DEBUG(10, ("Got kernel oplock break message from pid %d: %s/%u\n",
604                    (int)procid_to_pid(&src), file_id_string_tos(&id),
605                    (unsigned int)file_id));
606
607         fsp = initial_break_processing(id, file_id);
608
609         if (fsp == NULL) {
610                 DEBUG(3, ("Got a kernel oplock break message for a file "
611                           "I don't know about\n"));
612                 return;
613         }
614
615         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
616                 /* This is ok, kernel oplocks come in completely async */
617                 DEBUG(3, ("Got a kernel oplock request while waiting for a "
618                           "break reply\n"));
619                 return;
620         }
621
622         break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
623         if (break_msg == NULL) {
624                 exit_server("Could not talloc break_msg\n");
625         }
626
627         /* Save the server smb signing state. */
628         sign_state = srv_oplock_set_signing(False);
629
630         show_msg(break_msg);
631         if (!srv_send_smb(smbd_server_fd(),
632                         break_msg,
633                         IS_CONN_ENCRYPTED(fsp->conn))) {
634                 exit_server_cleanly("oplock_break: srv_send_smb failed.");
635         }
636
637         /* Restore the sign state to what it was. */
638         srv_oplock_set_signing(sign_state);
639
640         TALLOC_FREE(break_msg);
641
642         fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
643
644         add_oplock_timeout_handler(fsp);
645 }
646
647 void reply_to_oplock_break_requests(files_struct *fsp)
648 {
649         int i;
650
651         for (i=0; i<fsp->num_pending_break_messages; i++) {
652                 struct share_mode_entry *e = &fsp->pending_break_messages[i];
653                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
654
655                 share_mode_entry_to_message(msg, e);
656
657                 messaging_send_buf(smbd_messaging_context(), e->pid,
658                                    MSG_SMB_BREAK_RESPONSE,
659                                    (uint8 *)msg,
660                                    MSG_SMB_SHARE_MODE_ENTRY_SIZE);
661         }
662
663         SAFE_FREE(fsp->pending_break_messages);
664         fsp->num_pending_break_messages = 0;
665         if (fsp->oplock_timeout != NULL) {
666                 /* Remove the timed event handler. */
667                 TALLOC_FREE(fsp->oplock_timeout);
668                 fsp->oplock_timeout = NULL;
669         }
670         return;
671 }
672
673 static void process_oplock_break_response(struct messaging_context *msg_ctx,
674                                           void *private_data,
675                                           uint32_t msg_type,
676                                           struct server_id src,
677                                           DATA_BLOB *data)
678 {
679         struct share_mode_entry msg;
680
681         if (data->data == NULL) {
682                 DEBUG(0, ("Got NULL buffer\n"));
683                 return;
684         }
685
686         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
687                 DEBUG(0, ("Got invalid msg len %u\n",
688                           (unsigned int)data->length));
689                 return;
690         }
691
692         /* De-linearize incoming message. */
693         message_to_share_mode_entry(&msg, (char *)data->data);
694
695         DEBUG(10, ("Got oplock break response from pid %d: %s/%lu mid %u\n",
696                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id), msg.share_file_id,
697                    (unsigned int)msg.op_mid));
698
699         /* Here's the hack from open.c, store the mid in the 'port' field */
700         schedule_deferred_open_smb_message(msg.op_mid);
701 }
702
703 static void process_open_retry_message(struct messaging_context *msg_ctx,
704                                        void *private_data,
705                                        uint32_t msg_type,
706                                        struct server_id src,
707                                        DATA_BLOB *data)
708 {
709         struct share_mode_entry msg;
710         
711         if (data->data == NULL) {
712                 DEBUG(0, ("Got NULL buffer\n"));
713                 return;
714         }
715
716         if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
717                 DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
718                 return;
719         }
720
721         /* De-linearize incoming message. */
722         message_to_share_mode_entry(&msg, (char *)data->data);
723
724         DEBUG(10, ("Got open retry msg from pid %d: %s mid %u\n",
725                    (int)procid_to_pid(&src), file_id_string_tos(&msg.id),
726                    (unsigned int)msg.op_mid));
727
728         schedule_deferred_open_smb_message(msg.op_mid);
729 }
730
731 /****************************************************************************
732  This function is called on any file modification or lock request. If a file
733  is level 2 oplocked then it must tell all other level 2 holders to break to
734  none.
735 ****************************************************************************/
736
737 void release_level_2_oplocks_on_change(files_struct *fsp)
738 {
739         int i;
740         struct share_mode_lock *lck;
741
742         /*
743          * If this file is level II oplocked then we need
744          * to grab the shared memory lock and inform all
745          * other files with a level II lock that they need
746          * to flush their read caches. We keep the lock over
747          * the shared memory area whilst doing this.
748          */
749
750         if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
751                 return;
752
753         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
754                                   NULL);
755         if (lck == NULL) {
756                 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
757                          "share mode entry for file %s.\n", fsp->fsp_name ));
758                 return;
759         }
760
761         DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n", 
762                   lck->num_share_modes ));
763
764         for(i = 0; i < lck->num_share_modes; i++) {
765                 struct share_mode_entry *share_entry = &lck->share_modes[i];
766                 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
767
768                 if (!is_valid_share_mode_entry(share_entry)) {
769                         continue;
770                 }
771
772                 /*
773                  * As there could have been multiple writes waiting at the
774                  * lock_share_entry gate we may not be the first to
775                  * enter. Hence the state of the op_types in the share mode
776                  * entries may be partly NO_OPLOCK and partly LEVEL_II or FAKE_LEVEL_II
777                  * oplock. It will do no harm to re-send break messages to
778                  * those smbd's that are still waiting their turn to remove
779                  * their LEVEL_II state, and also no harm to ignore existing
780                  * NO_OPLOCK states. JRA.
781                  */
782
783                 DEBUG(10,("release_level_2_oplocks_on_change: "
784                           "share_entry[%i]->op_type == %d\n",
785                           i, share_entry->op_type ));
786
787                 if (share_entry->op_type == NO_OPLOCK) {
788                         continue;
789                 }
790
791                 /* Paranoia .... */
792                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
793                         DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
794                                  "share mode entry %d is an exlusive "
795                                  "oplock !\n", i ));
796                         TALLOC_FREE(lck);
797                         abort();
798                 }
799
800                 share_mode_entry_to_message(msg, share_entry);
801
802                 /*
803                  * Deal with a race condition when breaking level2
804                  * oplocks. Don't send all the messages and release
805                  * the lock, this allows someone else to come in and
806                  * get a level2 lock before any of the messages are
807                  * processed, and thus miss getting a break message.
808                  * Ensure at least one entry (the one we're breaking)
809                  * is processed immediately under the lock and becomes
810                  * set as NO_OPLOCK to stop any waiter getting a level2.
811                  * Bugid #5980.
812                  */
813
814                 if (procid_is_me(&share_entry->pid)) {
815                         DATA_BLOB blob = data_blob_const(msg,
816                                         MSG_SMB_SHARE_MODE_ENTRY_SIZE);
817                         process_oplock_async_level2_break_message(smbd_messaging_context(),
818                                                 NULL,
819                                                 MSG_SMB_ASYNC_LEVEL2_BREAK,
820                                                 share_entry->pid,
821                                                 &blob);
822                 } else {
823                         messaging_send_buf(smbd_messaging_context(),
824                                         share_entry->pid,
825                                         MSG_SMB_ASYNC_LEVEL2_BREAK,
826                                         (uint8 *)msg,
827                                         MSG_SMB_SHARE_MODE_ENTRY_SIZE);
828                 }
829         }
830
831         /* We let the message receivers handle removing the oplock state
832            in the share mode lock db. */
833
834         TALLOC_FREE(lck);
835 }
836
837 /****************************************************************************
838  Linearize a share mode entry struct to an internal oplock break message.
839 ****************************************************************************/
840
841 void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
842 {
843         SIVAL(msg,0,(uint32)e->pid.pid);
844         SSVAL(msg,4,e->op_mid);
845         SSVAL(msg,6,e->op_type);
846         SIVAL(msg,8,e->access_mask);
847         SIVAL(msg,12,e->share_access);
848         SIVAL(msg,16,e->private_options);
849         SIVAL(msg,20,(uint32)e->time.tv_sec);
850         SIVAL(msg,24,(uint32)e->time.tv_usec);
851         push_file_id_16(msg+28, &e->id);
852         SIVAL(msg,44,e->share_file_id);
853         SIVAL(msg,48,e->uid);
854         SSVAL(msg,52,e->flags);
855 #ifdef CLUSTER_SUPPORT
856         SIVAL(msg,54,e->pid.vnn);
857 #endif
858 }
859
860 /****************************************************************************
861  De-linearize an internal oplock break message to a share mode entry struct.
862 ****************************************************************************/
863
864 void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
865 {
866         e->pid.pid = (pid_t)IVAL(msg,0);
867         e->op_mid = SVAL(msg,4);
868         e->op_type = SVAL(msg,6);
869         e->access_mask = IVAL(msg,8);
870         e->share_access = IVAL(msg,12);
871         e->private_options = IVAL(msg,16);
872         e->time.tv_sec = (time_t)IVAL(msg,20);
873         e->time.tv_usec = (int)IVAL(msg,24);
874         pull_file_id_16(msg+28, &e->id);
875         e->share_file_id = (unsigned long)IVAL(msg,44);
876         e->uid = (uint32)IVAL(msg,48);
877         e->flags = (uint16)SVAL(msg,52);
878 #ifdef CLUSTER_SUPPORT
879         e->pid.vnn = IVAL(msg,54);
880 #endif
881 }
882
883 /****************************************************************************
884  Setup oplocks for this process.
885 ****************************************************************************/
886
887 bool init_oplocks(struct messaging_context *msg_ctx)
888 {
889         DEBUG(3,("init_oplocks: initializing messages.\n"));
890
891         messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_REQUEST,
892                            process_oplock_break_message);
893         messaging_register(msg_ctx, NULL, MSG_SMB_ASYNC_LEVEL2_BREAK,
894                            process_oplock_async_level2_break_message);
895         messaging_register(msg_ctx, NULL, MSG_SMB_BREAK_RESPONSE,
896                            process_oplock_break_response);
897         messaging_register(msg_ctx, NULL, MSG_SMB_KERNEL_BREAK,
898                            process_kernel_oplock_break);
899         messaging_register(msg_ctx, NULL, MSG_SMB_OPEN_RETRY,
900                            process_open_retry_message);
901
902         if (lp_kernel_oplocks()) {
903 #if HAVE_KERNEL_OPLOCKS_IRIX
904                 koplocks = irix_init_kernel_oplocks(talloc_autofree_context());
905 #elif HAVE_KERNEL_OPLOCKS_LINUX
906                 koplocks = linux_init_kernel_oplocks(talloc_autofree_context());
907 #endif
908         }
909
910         return True;
911 }