opendb: pass down a pointer to the fd in odb_open_file()
[jelmer/samba4-debian.git] / source / cluster / ctdb / opendb_ctdb.c
index 86dc1f50f11c6fd18d8aef6fd247c00a931adafd..402f3a2a1afe8ed1883f01d4fbf4bd22b7c7cc50 100644 (file)
@@ -261,6 +261,7 @@ static NTSTATUS odb_push_record(struct odb_lock *lck, struct opendb_file *file)
        return NT_STATUS_OK;
 }
 
+#if 0
 /*
   send an oplock break to a client
 */
@@ -271,6 +272,7 @@ static NTSTATUS odb_oplock_break_send(struct odb_context *odb, struct opendb_ent
        return messaging_send_ptr(odb->ntvfs_ctx->msg_ctx, e->server, 
                                  MSG_NTVFS_OPLOCK_BREAK, e->file_handle);
 }
+#endif
 
 /*
   register an open file in the open files database. This implements the share_access
@@ -279,103 +281,18 @@ static NTSTATUS odb_oplock_break_send(struct odb_context *odb, struct opendb_ent
   Note that the path is only used by the delete on close logic, not
   for comparing with other filenames
 */
-static NTSTATUS odb_ctdb_open_file(struct odb_lock *lck, void *file_handle,
-                                 uint32_t stream_id, uint32_t share_access, 
-                                 uint32_t access_mask, bool delete_on_close,
-                                 const char *path, 
-                                 uint32_t oplock_level, uint32_t *oplock_granted)
-{
-       struct odb_context *odb = lck->odb;
-       struct opendb_entry e;
-       int i;
-       struct opendb_file file;
-       NTSTATUS status;
-
-       if (odb->oplocks == false) {
-               oplock_level = OPLOCK_NONE;
-       }
-
-       status = odb_pull_record(lck, &file);
-       if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
-               /* initialise a blank structure */
-               ZERO_STRUCT(file);
-               file.path = path;
-       } else {
-               NT_STATUS_NOT_OK_RETURN(status);
-       }
-
-       /* see if it conflicts */
-       e.server          = odb->ntvfs_ctx->server_id;
-       e.file_handle     = file_handle;
-       e.stream_id       = stream_id;
-       e.share_access    = share_access;
-       e.access_mask     = access_mask;
-       e.delete_on_close = delete_on_close;
-       e.oplock_level    = OPLOCK_NONE;
-               
-       /* see if anyone has an oplock, which we need to break */
-       for (i=0;i<file.num_entries;i++) {
-               if (file.entries[i].oplock_level == OPLOCK_BATCH) {
-                       /* a batch oplock caches close calls, which
-                          means the client application might have
-                          already closed the file. We have to allow
-                          this close to propogate by sending a oplock
-                          break request and suspending this call
-                          until the break is acknowledged or the file
-                          is closed */
-                       odb_oplock_break_send(odb, &file.entries[i]);
-                       return NT_STATUS_OPLOCK_NOT_GRANTED;
-               }
-       }
-
-       if (file.delete_on_close || 
-           (file.num_entries != 0 && delete_on_close)) {
-               /* while delete on close is set, no new opens are allowed */
-               return NT_STATUS_DELETE_PENDING;
-       }
-
-       /* check for sharing violations */
-       for (i=0;i<file.num_entries;i++) {
-               status = share_conflict(&file.entries[i], &e);
-               NT_STATUS_NOT_OK_RETURN(status);
-       }
-
-       /* we now know the open could succeed, but we need to check
-          for any exclusive oplocks. We can't grant a second open
-          till these are broken. Note that we check for batch oplocks
-          before checking for sharing violations, and check for
-          exclusive oplocks afterwards. */
-       for (i=0;i<file.num_entries;i++) {
-               if (file.entries[i].oplock_level == OPLOCK_EXCLUSIVE) {
-                       odb_oplock_break_send(odb, &file.entries[i]);
-                       return NT_STATUS_OPLOCK_NOT_GRANTED;
-               }
-       }
+static NTSTATUS odb_ctdb_open_file(struct odb_lock *lck,
+                                  void *file_handle, const char *path,
+                                  int *fd, bool allow_level_II_oplock,
+                                  uint32_t oplock_level, uint32_t *oplock_granted)
 
+{
        /*
-         possibly grant an exclusive or batch oplock if this is the only client
-         with the file open. We don't yet grant levelII oplocks.
-       */
-       if (oplock_granted != NULL) {
-               if ((oplock_level == OPLOCK_BATCH ||
-                    oplock_level == OPLOCK_EXCLUSIVE) &&
-                   file.num_entries == 0) {
-                       (*oplock_granted) = oplock_level;
-               } else {
-                       (*oplock_granted) = OPLOCK_NONE;
-               }
-               e.oplock_level = (*oplock_granted);
-       }
-
-       /* it doesn't conflict, so add it to the end */
-       file.entries = talloc_realloc(lck, file.entries, struct opendb_entry, 
-                                     file.num_entries+1);
-       NT_STATUS_HAVE_NO_MEMORY(file.entries);
-
-       file.entries[file.num_entries] = e;
-       file.num_entries++;
-
-       return odb_push_record(lck, &file);
+        * as this file will went away and isn't used yet,
+        * copy the implementation from the tdb backend
+        * --metze
+        */
+       return NT_STATUS_FOOBAR;
 }
 
 
@@ -407,10 +324,12 @@ static NTSTATUS odb_ctdb_open_file_pending(struct odb_lock *lck, void *private)
 /*
   remove a opendb entry
 */
-static NTSTATUS odb_ctdb_close_file(struct odb_lock *lck, void *file_handle)
+static NTSTATUS odb_ctdb_close_file(struct odb_lock *lck, void *file_handle,
+                                   const char **_delete_path)
 {
        struct odb_context *odb = lck->odb;
        struct opendb_file file;
+       const char *delete_path = NULL;
        int i;
        NTSTATUS status;
 
@@ -446,6 +365,15 @@ static NTSTATUS odb_ctdb_close_file(struct odb_lock *lck, void *file_handle)
        file.num_pending = 0;
 
        file.num_entries--;
+
+       if (file.num_entries == 0 && file.delete_on_close) {
+               delete_path = talloc_strdup(lck, file.path);
+               NT_STATUS_HAVE_NO_MEMORY(delete_path);
+       }
+
+       if (_delete_path) {
+               *_delete_path = delete_path;
+       }
        
        return odb_push_record(lck, &file);
 }
@@ -464,6 +392,16 @@ static NTSTATUS odb_ctdb_update_oplock(struct odb_lock *lck, void *file_handle,
        return NT_STATUS_FOOBAR;
 }
 
+static NTSTATUS odb_ctdb_break_oplocks(struct odb_lock *lck)
+{
+       /*
+        * as this file will went away and isn't used yet,
+        * copy the implementation from the tdb backend
+        * --metze
+        */
+       return NT_STATUS_FOOBAR;
+}
+
 /*
   remove a pending opendb entry
 */
@@ -519,6 +457,25 @@ static NTSTATUS odb_ctdb_rename(struct odb_lock *lck, const char *path)
        return odb_push_record(lck, &file);
 }
 
+/*
+  get the path of an open file
+*/
+static NTSTATUS odb_ctdb_get_path(struct odb_lock *lck, const char **path)
+{
+       struct opendb_file file;
+       NTSTATUS status;
+
+       *path = NULL;
+
+       status = odb_pull_record(lck, &file);
+       /* we don't ignore NT_STATUS_OBJECT_NAME_NOT_FOUND here */
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       *path = file.path;
+
+       return NT_STATUS_OK;
+}
+
 /*
   update delete on close flag on an open file
 */
@@ -540,20 +497,20 @@ static NTSTATUS odb_ctdb_set_delete_on_close(struct odb_lock *lck, bool del_on_c
   people still have the file open
 */
 static NTSTATUS odb_ctdb_get_delete_on_close(struct odb_context *odb, 
-                                           DATA_BLOB *key, bool *del_on_close, 
-                                           int *open_count, char **path)
+                                           DATA_BLOB *key, bool *del_on_close)
 {
        NTSTATUS status;
        struct opendb_file file;
        struct odb_lock *lck;
 
+       (*del_on_close) = false;
+
        lck = odb_lock(odb, odb, key);
        NT_STATUS_HAVE_NO_MEMORY(lck);
 
        status = odb_pull_record(lck, &file);
        if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND, status)) {
                talloc_free(lck);
-               (*del_on_close) = false;
                return NT_STATUS_OK;
        }
        if (!NT_STATUS_IS_OK(status)) {
@@ -562,16 +519,6 @@ static NTSTATUS odb_ctdb_get_delete_on_close(struct odb_context *odb,
        }
 
        (*del_on_close) = file.delete_on_close;
-       if (open_count != NULL) {
-               (*open_count) = file.num_entries;
-       }
-       if (path != NULL) {
-               *path = talloc_strdup(odb, file.path);
-               NT_STATUS_HAVE_NO_MEMORY(*path);
-               if (file.num_entries == 1 && file.entries[0].delete_on_close) {
-                       (*del_on_close) = true;
-               }
-       }
 
        talloc_free(lck);
 
@@ -584,8 +531,9 @@ static NTSTATUS odb_ctdb_get_delete_on_close(struct odb_context *odb,
   create_options and access_mask
 */
 static NTSTATUS odb_ctdb_can_open(struct odb_lock *lck,
-                                uint32_t share_access, uint32_t create_options, 
-                                uint32_t access_mask)
+                                 uint32_t stream_id, uint32_t share_access,
+                                 uint32_t access_mask, bool delete_on_close,
+                                 uint32_t open_disposition, bool break_to_none)
 {
        struct odb_context *odb = lck->odb;
        NTSTATUS status;
@@ -599,7 +547,7 @@ static NTSTATUS odb_ctdb_can_open(struct odb_lock *lck,
        }
        NT_STATUS_NOT_OK_RETURN(status);
 
-       if ((create_options & NTCREATEX_OPTIONS_DELETE_ON_CLOSE) && 
+       if (delete_on_close &&
            file.num_entries != 0) {
                return NT_STATUS_SHARING_VIOLATION;
        }
@@ -639,10 +587,12 @@ static const struct opendb_ops opendb_ctdb_ops = {
        .odb_close_file          = odb_ctdb_close_file,
        .odb_remove_pending      = odb_ctdb_remove_pending,
        .odb_rename              = odb_ctdb_rename,
+       .odb_get_path            = odb_ctdb_get_path,
        .odb_set_delete_on_close = odb_ctdb_set_delete_on_close,
        .odb_get_delete_on_close = odb_ctdb_get_delete_on_close,
        .odb_can_open            = odb_ctdb_can_open,
-       .odb_update_oplock       = odb_ctdb_update_oplock
+       .odb_update_oplock       = odb_ctdb_update_oplock,
+       .odb_break_oplocks       = odb_ctdb_break_oplocks
 };