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