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