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