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