r12713: Remove use of uint8_t -> uint8.
[samba.git] / 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         return False;
55 }
56
57 /****************************************************************************
58  Read an oplock break message from either the oplock UDP fd or the
59  kernel (if kernel oplocks are supported).
60
61  If timeout is zero then *fds contains the file descriptors that
62  are ready to be read and acted upon. If timeout is non-zero then
63  *fds contains the file descriptors to be selected on for read.
64  The timeout is in milliseconds
65
66 ****************************************************************************/
67
68 void process_kernel_oplocks(void)
69 {
70         fd_set fds;
71
72         FD_ZERO(&fds);
73         smb_read_error = 0;
74
75         /*
76          * We need to check for kernel oplocks before going into the select
77          * here, as the EINTR generated by the linux kernel oplock may have
78          * already been eaten. JRA.
79          */
80
81         if (!koplocks) {
82                 return;
83         }
84
85         while (koplocks->msg_waiting(&fds)) { 
86                 files_struct *fsp;
87                 struct kernel_oplock_message msg;
88
89                 fsp = koplocks->receive_message(&fds);
90
91                 if (fsp == NULL) {
92                         DEBUG(3, ("Kernel oplock message announced, but none "
93                                   "received\n"));
94                         return;
95                 }
96
97                 msg.dev = fsp->dev;
98                 msg.inode = fsp->inode;
99                 msg.file_id = fsp->file_id;
100                 message_send_pid(pid_to_procid(sys_getpid()),
101                                  MSG_SMB_KERNEL_BREAK,
102                                  &msg, sizeof(msg), True);
103         }
104 }
105
106 /****************************************************************************
107  Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
108  disabled (just sets flags). Returns True if oplock set.
109 ****************************************************************************/
110
111 BOOL set_file_oplock(files_struct *fsp, int oplock_type)
112 {
113         if (koplocks && !koplocks->set_oplock(fsp, oplock_type))
114                 return False;
115
116         fsp->oplock_type = oplock_type;
117         fsp->sent_oplock_break = NO_BREAK_SENT;
118         if (oplock_type == LEVEL_II_OPLOCK)
119                 level_II_oplocks_open++;
120         else
121                 exclusive_oplocks_open++;
122
123         DEBUG(5,("set_file_oplock: granted oplock on file %s, dev = %x, inode = %.0f, file_id = %lu, \
124 tv_sec = %x, tv_usec = %x\n",
125                  fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode, fsp->file_id,
126                  (int)fsp->open_time.tv_sec, (int)fsp->open_time.tv_usec ));
127
128         return True;
129 }
130
131 /****************************************************************************
132  Attempt to release an oplock on a file. Decrements oplock count.
133 ****************************************************************************/
134
135 void release_file_oplock(files_struct *fsp)
136 {
137         if ((fsp->oplock_type != NO_OPLOCK) &&
138             (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
139             koplocks) {
140                 koplocks->release_oplock(fsp);
141         }
142
143         if (fsp->oplock_type == LEVEL_II_OPLOCK)
144                 level_II_oplocks_open--;
145         else if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
146                 exclusive_oplocks_open--;
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         fsp->oplock_type = LEVEL_II_OPLOCK;
166         exclusive_oplocks_open--;
167         level_II_oplocks_open++;
168         fsp->sent_oplock_break = NO_BREAK_SENT;
169 }
170
171 /****************************************************************************
172  Remove a file oplock. Copes with level II and exclusive.
173  Locks then unlocks the share mode lock. Client can decide to go directly
174  to none even if a "break-to-level II" was sent.
175 ****************************************************************************/
176
177 BOOL remove_oplock(files_struct *fsp)
178 {
179         SMB_DEV_T dev = fsp->dev;
180         SMB_INO_T inode = fsp->inode;
181         BOOL ret;
182         struct share_mode_lock *lck;
183
184         /* Remove the oplock flag from the sharemode. */
185         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
186         if (lck == NULL) {
187                 DEBUG(0,("remove_oplock: failed to lock share entry for "
188                          "file %s\n", fsp->fsp_name ));
189                 return False;
190         }
191         ret = remove_share_oplock(lck, fsp);
192         if (!ret) {
193                 DEBUG(0,("remove_oplock: failed to remove share oplock for "
194                          "file %s fnum %d, dev = %x, inode = %.0f\n",
195                          fsp->fsp_name, fsp->fnum, (unsigned int)dev,
196                          (double)inode));
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         SMB_DEV_T dev = fsp->dev;
209         SMB_INO_T inode = fsp->inode;
210         BOOL ret;
211         struct share_mode_lock *lck;
212
213         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
214         if (lck == NULL) {
215                 DEBUG(0,("downgrade_oplock: failed to lock share entry for "
216                          "file %s\n", fsp->fsp_name ));
217                 return False;
218         }
219         ret = downgrade_share_oplock(lck, fsp);
220         if (!ret) {
221                 DEBUG(0,("downgrade_oplock: failed to downgrade share oplock "
222                          "for file %s fnum %d, dev = %x, inode = %.0f\n",
223                          fsp->fsp_name, fsp->fnum, (unsigned int)dev,
224                          (double)inode));
225         }
226                 
227         downgrade_file_oplock(fsp);
228         talloc_free(lck);
229         return ret;
230 }
231
232 /****************************************************************************
233  Setup the listening set of file descriptors for an oplock break
234  message either from the UDP socket or from the kernel. Returns the maximum
235  fd used.
236 ****************************************************************************/
237
238 int setup_oplock_select_set( fd_set *fds)
239 {
240         int maxfd = 0;
241
242         if (koplocks && koplocks->notification_fd != -1) {
243                 FD_SET(koplocks->notification_fd, fds);
244                 maxfd = MAX(maxfd, koplocks->notification_fd);
245         }
246
247         return maxfd;
248 }
249
250 /****************************************************************************
251  Set up an oplock break message.
252 ****************************************************************************/
253
254 static char *new_break_smb_message(TALLOC_CTX *mem_ctx,
255                                    files_struct *fsp, uint8 cmd)
256 {
257         char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
258
259         if (result == NULL) {
260                 DEBUG(0, ("talloc failed\n"));
261                 return NULL;
262         }
263
264         memset(result,'\0',smb_size);
265         set_message(result,8,0,True);
266         SCVAL(result,smb_com,SMBlockingX);
267         SSVAL(result,smb_tid,fsp->conn->cnum);
268         SSVAL(result,smb_pid,0xFFFF);
269         SSVAL(result,smb_uid,0);
270         SSVAL(result,smb_mid,0xFFFF);
271         SCVAL(result,smb_vwv0,0xFF);
272         SSVAL(result,smb_vwv2,fsp->fnum);
273         SCVAL(result,smb_vwv3,LOCKING_ANDX_OPLOCK_RELEASE);
274         SCVAL(result,smb_vwv3+1,cmd);
275         return result;
276 }
277
278 /****************************************************************************
279  Function to do the waiting before sending a local break.
280 ****************************************************************************/
281
282 static void wait_before_sending_break(void)
283 {
284         struct timeval cur_tv;
285         long wait_left = (long)lp_oplock_break_wait_time();
286
287         if (wait_left == 0)
288                 return;
289
290         GetTimeOfDay(&cur_tv);
291
292         wait_left -= ((cur_tv.tv_sec - smb_last_time.tv_sec)*1000) +
293                 ((cur_tv.tv_usec - smb_last_time.tv_usec)/1000);
294
295         if(wait_left > 0) {
296                 wait_left = MIN(wait_left, 1000);
297                 sys_usleep(wait_left * 1000);
298         }
299 }
300
301 /****************************************************************************
302  Ensure that we have a valid oplock.
303 ****************************************************************************/
304
305 static files_struct *initial_break_processing(SMB_DEV_T dev, SMB_INO_T inode, unsigned long file_id)
306 {
307         files_struct *fsp = NULL;
308
309         if( DEBUGLVL( 3 ) ) {
310                 dbgtext( "initial_break_processing: called for dev = %x, inode = %.0f file_id = %lu\n",
311                         (unsigned int)dev, (double)inode, file_id);
312                 dbgtext( "Current oplocks_open (exclusive = %d, levelII = %d)\n",
313                         exclusive_oplocks_open, level_II_oplocks_open );
314         }
315
316         /*
317          * We need to search the file open table for the
318          * entry containing this dev and inode, and ensure
319          * we have an oplock on it.
320          */
321
322         fsp = file_find_dif(dev, inode, file_id);
323
324         if(fsp == NULL) {
325                 /* The file could have been closed in the meantime - return success. */
326                 if( DEBUGLVL( 3 ) ) {
327                         dbgtext( "initial_break_processing: cannot find open file with " );
328                         dbgtext( "dev = %x, inode = %.0f file_id = %lu", (unsigned int)dev,
329                                 (double)inode, file_id);
330                         dbgtext( "allowing break to succeed.\n" );
331                 }
332                 return NULL;
333         }
334
335         /* Ensure we have an oplock on the file */
336
337         /*
338          * There is a potential race condition in that an oplock could
339          * have been broken due to another udp request, and yet there are
340          * still oplock break messages being sent in the udp message
341          * queue for this file. So return true if we don't have an oplock,
342          * as we may have just freed it.
343          */
344
345         if(fsp->oplock_type == NO_OPLOCK) {
346                 if( DEBUGLVL( 3 ) ) {
347                         dbgtext( "initial_break_processing: file %s ", fsp->fsp_name );
348                         dbgtext( "(dev = %x, inode = %.0f, file_id = %lu) has no oplock.\n",
349                                 (unsigned int)dev, (double)inode, fsp->file_id );
350                         dbgtext( "Allowing break to succeed regardless.\n" );
351                 }
352                 return NULL;
353         }
354
355         return fsp;
356 }
357
358 static void oplock_timeout_handler(struct timed_event *te,
359                                    const struct timeval *now,
360                                    void *private_data)
361 {
362         files_struct *fsp = private_data;
363
364         DEBUG(0, ("Oplock break failed -- replying anyway\n"));
365         global_client_failed_oplock_break = True;
366         remove_oplock(fsp);
367         reply_to_oplock_break_requests(fsp);
368 }
369
370 static void process_oplock_break_message(int msg_type, struct process_id src,
371                                          void *buf, size_t len)
372 {
373         struct share_mode_entry *msg = buf;
374         files_struct *fsp;
375         char *break_msg;
376         BOOL break_to_level2 = False;
377         BOOL sign_state;
378
379         if (buf == NULL) {
380                 DEBUG(0, ("Got NULL buffer\n"));
381                 return;
382         }
383
384         if (len != sizeof(*msg)) {
385                 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
386                 return;
387         }
388
389         DEBUG(10, ("Got oplock break message from pid %d: %d/%d/%d\n",
390                    (int)procid_to_pid(&src), (int)msg->dev, (int)msg->inode,
391                    (int)msg->share_file_id));
392
393         fsp = initial_break_processing(msg->dev, msg->inode,
394                                        msg->share_file_id);
395
396         if (fsp == NULL) {
397                 /* We hit race here. Break messages are sent, and before we
398                  * get to process this message, we have closed the file. Reply
399                  * with 'ok, oplock broken' */
400                 DEBUG(3, ("Did not find fsp\n"));
401                 become_root();
402                 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
403                                  msg, sizeof(*msg), True);
404                 unbecome_root();
405                 return;
406         }
407
408         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
409                 /* Remember we have to inform the requesting PID when the
410                  * client replies */
411                 msg->pid = src;
412                 ADD_TO_ARRAY(NULL, struct share_mode_entry, *msg,
413                              &fsp->pending_break_messages,
414                              &fsp->num_pending_break_messages);
415                 return;
416         }
417
418         if (EXCLUSIVE_OPLOCK_TYPE(msg->op_type) &&
419             !EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
420                 DEBUG(3, ("Already downgraded oplock on %.0f/%.0f: %s\n",
421                           (double)fsp->dev, (double)fsp->inode,
422                           fsp->fsp_name));
423                 become_root();
424                 message_send_pid(src, MSG_SMB_BREAK_RESPONSE,
425                                  msg, sizeof(*msg), True);
426                 unbecome_root();
427                 return;
428         }
429
430         if ((msg_type == MSG_SMB_BREAK_REQUEST) &&
431             (global_client_caps & CAP_LEVEL_II_OPLOCKS) && 
432             !koplocks && /* NOTE: we force levelII off for kernel oplocks -
433                           * this will change when it is supported */
434             lp_level2_oplocks(SNUM(fsp->conn))) {
435                 break_to_level2 = True;
436         }
437
438         break_msg = new_break_smb_message(NULL, fsp, break_to_level2 ?
439                                           OPLOCKLEVEL_II : OPLOCKLEVEL_NONE);
440         if (break_msg == NULL) {
441                 exit_server("Could not talloc break_msg\n");
442         }
443
444         /* Need to wait before sending a break message to a file of our own */
445         if (procid_to_pid(&src) == sys_getpid()) {
446                 wait_before_sending_break();
447         }
448
449         /* Save the server smb signing state. */
450         sign_state = srv_oplock_set_signing(False);
451
452         show_msg(break_msg);
453         if (!send_smb(smbd_server_fd(), break_msg)) {
454                 exit_server("oplock_break: send_smb failed.");
455         }
456
457         /* Restore the sign state to what it was. */
458         srv_oplock_set_signing(sign_state);
459
460         talloc_free(break_msg);
461
462         if (msg_type == MSG_SMB_BREAK_REQUEST) {
463                 fsp->sent_oplock_break = break_to_level2 ?
464                         LEVEL_II_BREAK_SENT:BREAK_TO_NONE_SENT;
465         } else {
466                 /* Async level2 request, don't send a reply */
467                 fsp->sent_oplock_break = ASYNC_LEVEL_II_BREAK_SENT;
468         }
469         msg->pid = src;
470         ADD_TO_ARRAY(NULL, struct share_mode_entry, *msg,
471                      &fsp->pending_break_messages,
472                      &fsp->num_pending_break_messages);
473
474         if (fsp->oplock_timeout != NULL) {
475                 DEBUG(0, ("Logic problem -- have an oplock event hanging "
476                           "around\n"));
477         }
478
479         fsp->oplock_timeout =
480                 add_timed_event(NULL,
481                                 timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
482                                 "oplock_timeout_handler",
483                                 oplock_timeout_handler, fsp);
484
485         if (fsp->oplock_timeout == NULL) {
486                 DEBUG(0, ("Could not add oplock timeout handler\n"));
487         }
488 }
489
490 static void process_kernel_oplock_break(int msg_type, struct process_id src,
491                                         void *buf, size_t len)
492 {
493         struct kernel_oplock_message *msg = buf;
494         files_struct *fsp;
495         char *break_msg;
496         BOOL sign_state;
497
498         if (buf == NULL) {
499                 DEBUG(0, ("Got NULL buffer\n"));
500                 return;
501         }
502
503         if (len != sizeof(*msg)) {
504                 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
505                 return;
506         }
507
508         DEBUG(10, ("Got kernel oplock break message from pid %d: %d/%d/%d\n",
509                    (int)procid_to_pid(&src), (int)msg->dev, (int)msg->inode,
510                    (int)msg->file_id));
511
512         fsp = initial_break_processing(msg->dev, msg->inode, msg->file_id);
513
514         if (fsp == NULL) {
515                 DEBUG(3, ("Got a kernel oplock break message for a file "
516                           "I don't know about\n"));
517                 return;
518         }
519
520         if (fsp->sent_oplock_break != NO_BREAK_SENT) {
521                 /* This is ok, kernel oplocks come in completely async */
522                 DEBUG(3, ("Got a kernel oplock request while waiting for a "
523                           "break reply\n"));
524                 return;
525         }
526
527         break_msg = new_break_smb_message(NULL, fsp, OPLOCKLEVEL_NONE);
528         if (break_msg == NULL) {
529                 exit_server("Could not talloc break_msg\n");
530         }
531
532         /* Save the server smb signing state. */
533         sign_state = srv_oplock_set_signing(False);
534
535         show_msg(break_msg);
536         if (!send_smb(smbd_server_fd(), break_msg)) {
537                 exit_server("oplock_break: send_smb failed.");
538         }
539
540         /* Restore the sign state to what it was. */
541         srv_oplock_set_signing(sign_state);
542
543         talloc_free(break_msg);
544
545         fsp->sent_oplock_break = BREAK_TO_NONE_SENT;
546 }
547
548 void reply_to_oplock_break_requests(files_struct *fsp)
549 {
550         int i;
551
552         become_root();
553         for (i=0; i<fsp->num_pending_break_messages; i++) {
554                 struct share_mode_entry *msg = &fsp->pending_break_messages[i];
555                 message_send_pid(msg->pid, MSG_SMB_BREAK_RESPONSE,
556                                  msg, sizeof(*msg), True);
557         }
558         unbecome_root();
559
560         SAFE_FREE(fsp->pending_break_messages);
561         fsp->num_pending_break_messages = 0;
562         if (fsp->oplock_timeout != NULL) {
563                 talloc_free(fsp->oplock_timeout);
564                 fsp->oplock_timeout = NULL;
565         }
566         return;
567 }
568
569 static void process_oplock_break_response(int msg_type, struct process_id src,
570                                           void *buf, size_t len)
571 {
572         struct share_mode_entry *msg = buf;
573
574         if (buf == NULL) {
575                 DEBUG(0, ("Got NULL buffer\n"));
576                 return;
577         }
578
579         if (len != sizeof(*msg)) {
580                 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
581                 return;
582         }
583
584         DEBUG(10, ("Got oplock break response from pid %d: %d/%d/%d mid %d\n",
585                    (int)procid_to_pid(&src), (int)msg->dev, (int)msg->inode,
586                    (int)msg->share_file_id, (int)msg->op_mid));
587
588         /* Here's the hack from open.c, store the mid in the 'port' field */
589         schedule_deferred_open_smb_message(msg->op_mid);
590 }
591
592 static void process_open_retry_message(int msg_type, struct process_id src,
593                                        void *buf, size_t len)
594 {
595         struct share_mode_entry *msg = buf;
596         
597         if (buf == NULL) {
598                 DEBUG(0, ("Got NULL buffer\n"));
599                 return;
600         }
601
602         if (len != sizeof(*msg)) {
603                 DEBUG(0, ("Got invalid msg len %d\n", (int)len));
604                 return;
605         }
606
607         DEBUG(10, ("Got open retry msg from pid %d: %d/%d mid %d\n",
608                    (int)procid_to_pid(&src), (int)msg->dev, (int)msg->inode,
609                    (int)msg->op_mid));
610
611         schedule_deferred_open_smb_message(msg->op_mid);
612 }
613
614 /****************************************************************************
615  This function is called on any file modification or lock request. If a file
616  is level 2 oplocked then it must tell all other level 2 holders to break to
617  none.
618 ****************************************************************************/
619
620 void release_level_2_oplocks_on_change(files_struct *fsp)
621 {
622         int i;
623         struct share_mode_lock *lck;
624
625         /*
626          * If this file is level II oplocked then we need
627          * to grab the shared memory lock and inform all
628          * other files with a level II lock that they need
629          * to flush their read caches. We keep the lock over
630          * the shared memory area whilst doing this.
631          */
632
633         if (!LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
634                 return;
635
636         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
637         if (lck == NULL) {
638                 DEBUG(0,("release_level_2_oplocks_on_change: failed to lock "
639                          "share mode entry for file %s.\n", fsp->fsp_name ));
640         }
641
642         DEBUG(10,("release_level_2_oplocks_on_change: num_share_modes = %d\n", 
643                   lck->num_share_modes ));
644
645         if (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) {
646                 /* See if someone else has already downgraded us, then we
647                    don't have to do anything */
648                 for (i=0; i<lck->num_share_modes; i++) {
649                         struct share_mode_entry *e = &lck->share_modes[i];
650                         if ((e->op_type == NO_OPLOCK) &&
651                             (e->share_file_id == fsp->file_id) &&
652                             (e->dev == fsp->dev) &&
653                             (e->inode == fsp->inode) &&
654                             (procid_is_me(&e->pid))) {
655                                 /* We're done */
656                                 fsp->oplock_type = NO_OPLOCK;
657                                 talloc_free(lck);
658                                 return;
659                         }
660                 }
661         }
662
663         for(i = 0; i < lck->num_share_modes; i++) {
664                 struct share_mode_entry *share_entry = &lck->share_modes[i];
665
666                 /*
667                  * As there could have been multiple writes waiting at the
668                  * lock_share_entry gate we may not be the first to
669                  * enter. Hence the state of the op_types in the share mode
670                  * entries may be partly NO_OPLOCK and partly LEVEL_II
671                  * oplock. It will do no harm to re-send break messages to
672                  * those smbd's that are still waiting their turn to remove
673                  * their LEVEL_II state, and also no harm to ignore existing
674                  * NO_OPLOCK states. JRA.
675                  */
676
677                 DEBUG(10,("release_level_2_oplocks_on_change: "
678                           "share_entry[%i]->op_type == %d\n",
679                           i, share_entry->op_type ));
680
681                 if ((share_entry->op_type == NO_OPLOCK) ||
682                     (share_entry->op_type == FAKE_LEVEL_II_OPLOCK)) {
683                         continue;
684                 }
685
686                 /* Paranoia .... */
687                 if (EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) {
688                         DEBUG(0,("release_level_2_oplocks_on_change: PANIC. "
689                                  "share mode entry %d is an exlusive "
690                                  "oplock !\n", i ));
691                         talloc_free(lck);
692                         abort();
693                 }
694
695                 become_root();
696                 message_send_pid(share_entry->pid, MSG_SMB_ASYNC_LEVEL2_BREAK,
697                                  share_entry, sizeof(*share_entry), True);
698                 unbecome_root();
699         }
700
701         remove_all_share_oplocks(lck, fsp);
702         talloc_free(lck);
703 }
704
705 /****************************************************************************
706  Setup oplocks for this process.
707 ****************************************************************************/
708
709 BOOL init_oplocks(void)
710 {
711         DEBUG(3,("open_oplock_ipc: opening loopback UDP socket.\n"));
712
713         message_register(MSG_SMB_BREAK_REQUEST,
714                          process_oplock_break_message);
715         message_register(MSG_SMB_ASYNC_LEVEL2_BREAK,
716                          process_oplock_break_message);
717         message_register(MSG_SMB_BREAK_RESPONSE,
718                          process_oplock_break_response);
719         message_register(MSG_SMB_KERNEL_BREAK,
720                          process_kernel_oplock_break);
721         message_register(MSG_SMB_OPEN_RETRY,
722                          process_open_retry_message);
723
724         if (lp_kernel_oplocks()) {
725 #if HAVE_KERNEL_OPLOCKS_IRIX
726                 koplocks = irix_init_kernel_oplocks();
727 #elif HAVE_KERNEL_OPLOCKS_LINUX
728                 koplocks = linux_init_kernel_oplocks();
729 #endif
730         }
731
732         return True;
733 }