s3-talloc Change TALLOC_ARRAY() to talloc_array()
[samba.git] / source3 / smbd / oplock.c
index 88fce91954d5df181d3fb98a727c486b2d33b82f..8482a00f641a0fe30cddf280e89f06952ebec373 100644 (file)
@@ -21,8 +21,9 @@
 
 #define DBGC_CLASS DBGC_LOCKING
 #include "includes.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
-#include "librpc/gen_ndr/messaging.h"
+#include "messages.h"
 
 /****************************************************************************
  Get the number of current exclusive oplocks.
@@ -47,24 +48,28 @@ void break_kernel_oplock(struct messaging_context *msg_ctx, files_struct *fsp)
        /* Don't need to be root here as we're only ever
           sending to ourselves. */
 
-       messaging_send_buf(msg_ctx, procid_self(),
+       messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
                           MSG_SMB_KERNEL_BREAK,
                           msg, MSG_SMB_KERNEL_BREAK_SIZE);
 }
 
 /****************************************************************************
- Attempt to set an oplock on a file. Always succeeds if kernel oplocks are
- disabled (just sets flags). Returns True if oplock set.
+ Attempt to set an oplock on a file. Succeeds if kernel oplocks are
+ disabled (just sets flags) and no byte-range locks in the file. Returns True
+ if oplock set.
 ****************************************************************************/
 
 bool set_file_oplock(files_struct *fsp, int oplock_type)
 {
-       if ((fsp->oplock_type == LEVEL_II_OPLOCK)
-           && koplocks && !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
-               DEBUG(10, ("Refusing level2 oplock, kernel oplocks don't "
-                          "support them\n"));
-               return false;
+       if (fsp->oplock_type == LEVEL_II_OPLOCK) {
+               if (koplocks &&
+                   !(koplocks->flags & KOPLOCKS_LEVEL2_SUPPORTED)) {
+                       DEBUG(10, ("Refusing level2 oplock, kernel oplocks "
+                                  "don't support them\n"));
+                       return false;
+               }
        }
+
        if ((fsp->oplock_type != NO_OPLOCK) &&
            (fsp->oplock_type != FAKE_LEVEL_II_OPLOCK) &&
            koplocks &&
@@ -119,6 +124,7 @@ void release_file_oplock(files_struct *fsp)
        fsp->sent_oplock_break = NO_BREAK_SENT;
 
        flush_write_cache(fsp, OPLOCK_RELEASE_FLUSH);
+       delete_write_cache(fsp);
 
        TALLOC_FREE(fsp->oplock_timeout);
 }
@@ -218,7 +224,7 @@ bool should_notify_deferred_opens()
 static char *new_break_message_smb1(TALLOC_CTX *mem_ctx,
                                   files_struct *fsp, int cmd)
 {
-       char *result = TALLOC_ARRAY(mem_ctx, char, smb_size + 8*2 + 0);
+       char *result = talloc_array(mem_ctx, char, smb_size + 8*2 + 0);
 
        if (result == NULL) {
                DEBUG(0, ("talloc failed\n"));
@@ -256,7 +262,9 @@ static void wait_before_sending_break(void)
  Ensure that we have a valid oplock.
 ****************************************************************************/
 
-static files_struct *initial_break_processing(struct file_id id, unsigned long file_id)
+static files_struct *initial_break_processing(
+       struct smbd_server_connection *sconn, struct file_id id,
+       unsigned long file_id)
 {
        files_struct *fsp = NULL;
 
@@ -273,7 +281,7 @@ static files_struct *initial_break_processing(struct file_id id, unsigned long f
         * we have an oplock on it.
         */
 
-       fsp = file_find_dif(id, file_id);
+       fsp = file_find_dif(sconn, id, file_id);
 
        if(fsp == NULL) {
                /* The file could have been closed in the meantime - return success. */
@@ -345,7 +353,7 @@ static void add_oplock_timeout_handler(files_struct *fsp)
        }
 
        fsp->oplock_timeout =
-               event_add_timed(smbd_event_context(), fsp,
+               event_add_timed(server_event_context(), fsp,
                                timeval_current_ofs(OPLOCK_BREAK_TIMEOUT, 0),
                                oplock_timeout_handler, fsp);
 
@@ -364,7 +372,7 @@ static void send_break_message_smb1(files_struct *fsp, int level)
        }
 
        show_msg(break_msg);
-       if (!srv_send_smb(smbd_server_fd(),
+       if (!srv_send_smb(fsp->conn->sconn,
                        break_msg, false, 0,
                        IS_CONN_ENCRYPTED(fsp->conn),
                        NULL)) {
@@ -377,7 +385,7 @@ static void send_break_message_smb1(files_struct *fsp, int level)
 
 void break_level2_to_none_async(files_struct *fsp)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = fsp->conn->sconn;
 
        if (fsp->oplock_type == NO_OPLOCK) {
                /* We already got a "break to none" message and we've handled
@@ -427,6 +435,7 @@ void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx
                                                      struct server_id src,
                                                      DATA_BLOB *data)
 {
+       struct smbd_server_connection *sconn;
        struct share_mode_entry msg;
        files_struct *fsp;
 
@@ -435,6 +444,12 @@ void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx
                return;
        }
 
+       sconn = msg_ctx_to_sconn(msg_ctx);
+       if (sconn == NULL) {
+               DEBUG(1, ("could not find sconn\n"));
+               return;
+       }
+
        if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
                DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
                return;
@@ -447,7 +462,7 @@ void process_oplock_async_level2_break_message(struct messaging_context *msg_ctx
                   "%s/%lu\n", procid_str(talloc_tos(), &src),
                   file_id_string_tos(&msg.id), msg.share_file_id));
 
-       fsp = initial_break_processing(msg.id, msg.share_file_id);
+       fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
 
        if (fsp == NULL) {
                /* We hit a race here. Break messages are sent, and before we
@@ -470,7 +485,7 @@ static void process_oplock_break_message(struct messaging_context *msg_ctx,
                                         struct server_id src,
                                         DATA_BLOB *data)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn;
        struct share_mode_entry msg;
        files_struct *fsp;
        bool break_to_level2 = False;
@@ -480,6 +495,12 @@ static void process_oplock_break_message(struct messaging_context *msg_ctx,
                return;
        }
 
+       sconn = msg_ctx_to_sconn(msg_ctx);
+       if (sconn == NULL) {
+               DEBUG(1, ("could not find sconn\n"));
+               return;
+       }
+
        if (data->length != MSG_SMB_SHARE_MODE_ENTRY_SIZE) {
                DEBUG(0, ("Got invalid msg len %d\n", (int)data->length));
                return;
@@ -492,7 +513,7 @@ static void process_oplock_break_message(struct messaging_context *msg_ctx,
                   procid_str(talloc_tos(), &src), file_id_string_tos(&msg.id),
                   msg.share_file_id));
 
-       fsp = initial_break_processing(msg.id, msg.share_file_id);
+       fsp = initial_break_processing(sconn, msg.id, msg.share_file_id);
 
        if (fsp == NULL) {
                /* We hit a race here. Break messages are sent, and before we
@@ -570,7 +591,7 @@ static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
                                        struct server_id src,
                                        DATA_BLOB *data)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn;
        struct file_id id;
        unsigned long file_id;
        files_struct *fsp;
@@ -585,6 +606,12 @@ static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
                return;
        }
 
+       sconn = msg_ctx_to_sconn(msg_ctx);
+       if (sconn == NULL) {
+               DEBUG(1, ("could not find sconn\n"));
+               return;
+       }
+
        /* Pull the data from the message. */
        pull_file_id_24((char *)data->data, &id);
        file_id = (unsigned long)IVAL(data->data, 24);
@@ -593,7 +620,7 @@ static void process_kernel_oplock_break(struct messaging_context *msg_ctx,
                   procid_str(talloc_tos(), &src), file_id_string_tos(&id),
                   (unsigned int)file_id));
 
-       fsp = initial_break_processing(id, file_id);
+       fsp = initial_break_processing(sconn, id, file_id);
 
        if (fsp == NULL) {
                DEBUG(3, ("Got a kernel oplock break message for a file "
@@ -638,7 +665,7 @@ void reply_to_oplock_break_requests(files_struct *fsp)
 
                share_mode_entry_to_message(msg, e);
 
-               messaging_send_buf(smbd_messaging_context(), e->pid,
+               messaging_send_buf(fsp->conn->sconn->msg_ctx, e->pid,
                                   MSG_SMB_BREAK_RESPONSE,
                                   (uint8 *)msg,
                                   MSG_SMB_SHARE_MODE_ENTRY_SIZE);
@@ -796,10 +823,19 @@ static void contend_level2_oplocks_begin_default(files_struct *fsp,
                 */
 
                if (procid_is_me(&share_entry->pid)) {
+                       struct files_struct *cur_fsp =
+                               initial_break_processing(fsp->conn->sconn,
+                                       share_entry->id,
+                                       share_entry->share_file_id);
                        wait_before_sending_break();
-                       break_level2_to_none_async(fsp);
+                       if (cur_fsp != NULL) {
+                               break_level2_to_none_async(cur_fsp);
+                       } else {
+                               DEBUG(3, ("release_level_2_oplocks_on_change: "
+                               "Did not find fsp, ignoring\n"));
+                       }
                } else {
-                       messaging_send_buf(smbd_messaging_context(),
+                       messaging_send_buf(fsp->conn->sconn->msg_ctx,
                                        share_entry->pid,
                                        MSG_SMB_ASYNC_LEVEL2_BREAK,
                                        (uint8 *)msg,
@@ -851,9 +887,8 @@ void share_mode_entry_to_message(char *msg, const struct share_mode_entry *e)
        SIVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET,e->share_file_id);
        SIVAL(msg,OP_BREAK_MSG_UID_OFFSET,e->uid);
        SSVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET,e->flags);
-#ifdef CLUSTER_SUPPORT
+       SIVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET,e->name_hash);
        SIVAL(msg,OP_BREAK_MSG_VNN_OFFSET,e->pid.vnn);
-#endif
 }
 
 /****************************************************************************
@@ -874,9 +909,8 @@ void message_to_share_mode_entry(struct share_mode_entry *e, char *msg)
        e->share_file_id = (unsigned long)IVAL(msg,OP_BREAK_MSG_FILE_ID_OFFSET);
        e->uid = (uint32)IVAL(msg,OP_BREAK_MSG_UID_OFFSET);
        e->flags = (uint16)SVAL(msg,OP_BREAK_MSG_FLAGS_OFFSET);
-#ifdef CLUSTER_SUPPORT
+       e->name_hash = IVAL(msg,OP_BREAK_MSG_NAME_HASH_OFFSET);
        e->pid.vnn = IVAL(msg,OP_BREAK_MSG_VNN_OFFSET);
-#endif
 }
 
 /****************************************************************************
@@ -900,11 +934,12 @@ bool init_oplocks(struct messaging_context *msg_ctx)
 
        if (lp_kernel_oplocks()) {
 #if HAVE_KERNEL_OPLOCKS_IRIX
-               koplocks = irix_init_kernel_oplocks(talloc_autofree_context());
+               koplocks = irix_init_kernel_oplocks(NULL);
 #elif HAVE_KERNEL_OPLOCKS_LINUX
-               koplocks = linux_init_kernel_oplocks(talloc_autofree_context());
+               koplocks = linux_init_kernel_oplocks(NULL);
 #elif HAVE_ONEFS
-               koplocks = onefs_init_kernel_oplocks(talloc_autofree_context());
+#error Isilon, please check if the NULL context is okay here. Thanks!
+               koplocks = onefs_init_kernel_oplocks(NULL);
 #endif
        }