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