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