root/source3/smbd/oplock.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_number_of_exclusive_open_oplocks
  2. break_kernel_oplock
  3. set_file_oplock
  4. release_file_oplock
  5. downgrade_file_oplock
  6. remove_oplock
  7. downgrade_oplock
  8. should_notify_deferred_opens
  9. new_break_smb_message
  10. wait_before_sending_break
  11. initial_break_processing
  12. oplock_timeout_handler
  13. add_oplock_timeout_handler
  14. break_level2_to_none_async
  15. process_oplock_async_level2_break_message
  16. process_oplock_break_message
  17. process_kernel_oplock_break
  18. reply_to_oplock_break_requests
  19. process_oplock_break_response
  20. process_open_retry_message
  21. contend_level2_oplocks_begin_default
  22. contend_level2_oplocks_begin
  23. contend_level2_oplocks_end
  24. share_mode_entry_to_message
  25. message_to_share_mode_entry
  26. init_oplocks

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

/* [<][>][^][v][top][bottom][index][help] */