smbd: There's only one notify_callback
[kai/samba-autobuild/.git] / source3 / smbd / notify.c
index 4842d6f2fcb33b4a1ba34e404fa6a6666cb87c76..f493f11aa69a558389689b2fe3fc271b297adf32 100644 (file)
 #include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "../librpc/gen_ndr/ndr_notify.h"
+#include "librpc/gen_ndr/ndr_file_id.h"
+
+struct notify_change_event {
+       struct timespec when;
+       uint32_t action;
+       const char *name;
+};
 
 struct notify_change_buf {
        /*
@@ -35,7 +42,7 @@ struct notify_change_buf {
         * asked we just return NT_STATUS_OK without specific changes.
         */
        int num_changes;
-       struct notify_change *changes;
+       struct notify_change_event *changes;
 
        /*
         * If no changes are around requests are queued here. Using a linked
@@ -48,8 +55,8 @@ struct notify_change_request {
        struct notify_change_request *prev, *next;
        struct files_struct *fsp;       /* backpointer for cancel by mid */
        struct smb_request *req;
-       uint32 filter;
-       uint32 max_param;
+       uint32_t filter;
+       uint32_t max_param;
        void (*reply_fn)(struct smb_request *req,
                         NTSTATUS error_code,
                         uint8_t *buf, size_t len);
@@ -57,7 +64,8 @@ struct notify_change_request {
        void *backend_data;
 };
 
-static void notify_fsp(files_struct *fsp, uint32 action, const char *name);
+static void notify_fsp(files_struct *fsp, struct timespec when,
+                      uint32_t action, const char *name);
 
 bool change_notify_fsp_has_changes(struct files_struct *fsp)
 {
@@ -87,8 +95,8 @@ struct notify_mid_map {
        uint64_t mid;
 };
 
-static bool notify_change_record_identical(struct notify_change *c1,
-                                       struct notify_change *c2)
+static bool notify_change_record_identical(struct notify_change_event *c1,
+                                          struct notify_change_event *c2)
 {
        /* Note this is deliberately case sensitive. */
        if (c1->action == c2->action &&
@@ -98,9 +106,17 @@ static bool notify_change_record_identical(struct notify_change *c1,
        return False;
 }
 
+static int compare_notify_change_events(const void *p1, const void *p2)
+{
+       const struct notify_change_event *e1 = p1;
+       const struct notify_change_event *e2 = p2;
+
+       return timespec_compare(&e1->when, &e2->when);
+}
+
 static bool notify_marshall_changes(int num_changes,
-                               uint32 max_offset,
-                               struct notify_change *changes,
+                               uint32_t max_offset,
+                               struct notify_change_event *changes,
                                DATA_BLOB *final_blob)
 {
        int i;
@@ -109,11 +125,20 @@ static bool notify_marshall_changes(int num_changes,
                return false;
        }
 
+       /*
+        * Sort the notifies by timestamp when the event happened to avoid
+        * coalescing and thus dropping events.
+        */
+
+       qsort(changes, num_changes,
+             sizeof(*changes), compare_notify_change_events);
+
        for (i=0; i<num_changes; i++) {
                enum ndr_err_code ndr_err;
-               struct notify_change *c;
+               struct notify_change_event *c;
                struct FILE_NOTIFY_INFORMATION m;
                DATA_BLOB blob;
+               uint16_t pad = 0;
 
                /* Coalesce any identical records. */
                while (i+1 < num_changes &&
@@ -127,12 +152,23 @@ static bool notify_marshall_changes(int num_changes,
                m.FileName1 = c->name;
                m.FileNameLength = strlen_m(c->name)*2;
                m.Action = c->action;
-               m.NextEntryOffset = (i == num_changes-1) ? 0 : ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
+
+               m._pad = data_blob_null;
 
                /*
                 * Offset to next entry, only if there is one
                 */
 
+               if (i == (num_changes-1)) {
+                       m.NextEntryOffset = 0;
+               } else {
+                       if ((m.FileNameLength % 4) == 2) {
+                               m._pad = data_blob_const(&pad, 2);
+                       }
+                       m.NextEntryOffset =
+                               ndr_size_FILE_NOTIFY_INFORMATION(&m, 0);
+               }
+
                ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &m,
                        (ndr_push_flags_fn_t)ndr_push_FILE_NOTIFY_INFORMATION);
                if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -204,27 +240,19 @@ void change_notify_reply(struct smb_request *req,
        notify_buf->num_changes = 0;
 }
 
-static void notify_callback(void *private_data, const struct notify_event *e)
+void notify_callback(void *private_data, struct timespec when,
+                    const struct notify_event *e)
 {
        files_struct *fsp = (files_struct *)private_data;
        DEBUG(10, ("notify_callback called for %s\n", fsp_str_dbg(fsp)));
-       notify_fsp(fsp, e->action, e->path);
+       notify_fsp(fsp, when, e->action, e->path);
 }
 
-static void sys_notify_callback(struct sys_notify_context *ctx,
-                               void *private_data,
-                               struct notify_event *e)
-{
-       files_struct *fsp = (files_struct *)private_data;
-       DEBUG(10, ("sys_notify_callback called for %s\n", fsp_str_dbg(fsp)));
-       notify_fsp(fsp, e->action, e->path);
-}
-
-NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
+NTSTATUS change_notify_create(struct files_struct *fsp, uint32_t filter,
                              bool recursive)
 {
-       char *fullpath;
-       size_t len;
+       size_t len = fsp_fullbasepath(fsp, NULL, 0);
+       char fullpath[len+1];
        uint32_t subdir_filter;
        NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
 
@@ -239,51 +267,28 @@ NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
                return NT_STATUS_NO_MEMORY;
        }
 
-       /* Do notify operations on the base_name. */
-       fullpath = talloc_asprintf(
-               talloc_tos(), "%s/%s", fsp->conn->connectpath,
-               fsp->fsp_name->base_name);
-       if (fullpath == NULL) {
-               DEBUG(0, ("talloc_asprintf failed\n"));
-               TALLOC_FREE(fsp->notify);
-               return NT_STATUS_NO_MEMORY;
-       }
+       fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
 
        /*
         * Avoid /. at the end of the path name. notify can't deal with it.
         */
-       len = strlen(fullpath);
        if (len > 1 && fullpath[len-1] == '.' && fullpath[len-2] == '/') {
                fullpath[len-2] = '\0';
        }
 
        subdir_filter = recursive ? filter : 0;
 
-       if (fsp->conn->sconn->sys_notify_ctx != NULL) {
-               void *sys_notify_handle = NULL;
-
-               status = SMB_VFS_NOTIFY_WATCH(
-                       fsp->conn, fsp->conn->sconn->sys_notify_ctx,
-                       fullpath, &filter, &subdir_filter,
-                       sys_notify_callback, fsp, &sys_notify_handle);
-
-               if (NT_STATUS_IS_OK(status)) {
-                       talloc_steal(fsp->notify, sys_notify_handle);
-               }
-       }
-
        if ((filter != 0) || (subdir_filter != 0)) {
                status = notify_add(fsp->conn->sconn->notify_ctx,
-                                   fullpath, filter, subdir_filter,
-                                   notify_callback, fsp);
+                                   fullpath, filter, subdir_filter, fsp);
        }
-       TALLOC_FREE(fullpath);
+
        return status;
 }
 
 NTSTATUS change_notify_add_request(struct smb_request *req,
-                               uint32 max_param,
-                               uint32 filter, bool recursive,
+                               uint32_t max_param,
+                               uint32_t filter, bool recursive,
                                struct files_struct *fsp,
                                void (*reply_fn)(struct smb_request *req,
                                        NTSTATUS error_code,
@@ -312,8 +317,7 @@ NTSTATUS change_notify_add_request(struct smb_request *req,
        request->reply_fn = reply_fn;
        request->backend_data = NULL;
 
-       DLIST_ADD_END(fsp->notify->requests, request,
-                     struct notify_change_request *);
+       DLIST_ADD_END(fsp->notify->requests, request);
 
        map->mid = request->req->mid;
        DLIST_ADD(sconn->smb1.notify_mid_maps, map);
@@ -350,6 +354,31 @@ static void change_notify_remove_request(struct smbd_server_connection *sconn,
        TALLOC_FREE(req);
 }
 
+static void smbd_notify_cancel_by_map(struct notify_mid_map *map)
+{
+       struct smb_request *smbreq = map->req->req;
+       struct smbd_server_connection *sconn = smbreq->sconn;
+       struct smbd_smb2_request *smb2req = smbreq->smb2req;
+       NTSTATUS notify_status = NT_STATUS_CANCELLED;
+
+       if (smb2req != NULL) {
+               if (smb2req->session == NULL) {
+                       notify_status = STATUS_NOTIFY_CLEANUP;
+               } else if (!NT_STATUS_IS_OK(smb2req->session->status)) {
+                       notify_status = STATUS_NOTIFY_CLEANUP;
+               }
+               if (smb2req->tcon == NULL) {
+                       notify_status = STATUS_NOTIFY_CLEANUP;
+               } else if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
+                       notify_status = STATUS_NOTIFY_CLEANUP;
+               }
+       }
+
+       change_notify_reply(smbreq, notify_status,
+                           0, NULL, map->req->reply_fn);
+       change_notify_remove_request(sconn, map->req);
+}
+
 /****************************************************************************
  Delete entries by mid from the change notify pending queue. Always send reply.
 *****************************************************************************/
@@ -369,9 +398,7 @@ void remove_pending_change_notify_requests_by_mid(
                return;
        }
 
-       change_notify_reply(map->req->req,
-                           NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
-       change_notify_remove_request(sconn, map->req);
+       smbd_notify_cancel_by_map(map);
 }
 
 void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
@@ -389,9 +416,49 @@ void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
                return;
        }
 
-       change_notify_reply(map->req->req,
-                           NT_STATUS_CANCELLED, 0, NULL, map->req->reply_fn);
-       change_notify_remove_request(sconn, map->req);
+       smbd_notify_cancel_by_map(map);
+}
+
+static struct files_struct *smbd_notify_cancel_deleted_fn(
+       struct files_struct *fsp, void *private_data)
+{
+       struct file_id *fid = talloc_get_type_abort(
+               private_data, struct file_id);
+
+       if (file_id_equal(&fsp->file_id, fid)) {
+               remove_pending_change_notify_requests_by_fid(
+                       fsp, NT_STATUS_DELETE_PENDING);
+       }
+       return NULL;
+}
+
+void smbd_notify_cancel_deleted(struct messaging_context *msg,
+                               void *private_data, uint32_t msg_type,
+                               struct server_id server_id, DATA_BLOB *data)
+{
+       struct smbd_server_connection *sconn = talloc_get_type_abort(
+               private_data, struct smbd_server_connection);
+       struct file_id *fid;
+       enum ndr_err_code ndr_err;
+
+       fid = talloc(talloc_tos(), struct file_id);
+       if (fid == NULL) {
+               DEBUG(1, ("talloc failed\n"));
+               return;
+       }
+
+       ndr_err = ndr_pull_struct_blob_all(
+               data, fid, fid, (ndr_pull_flags_fn_t)ndr_pull_file_id);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               DEBUG(10, ("%s: ndr_pull_file_id failed: %s\n", __func__,
+                          ndr_errstr(ndr_err)));
+               goto done;
+       }
+
+       files_forall(sconn, smbd_notify_cancel_deleted_fn, fid);
+
+done:
+       TALLOC_FREE(fid);
 }
 
 /****************************************************************************
@@ -414,28 +481,22 @@ void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
        }
 }
 
-void notify_fname(connection_struct *conn, uint32 action, uint32 filter,
+void notify_fname(connection_struct *conn, uint32_t action, uint32_t filter,
                  const char *path)
 {
        struct notify_context *notify_ctx = conn->sconn->notify_ctx;
-       char *fullpath;
 
        if (path[0] == '.' && path[1] == '/') {
                path += 2;
        }
-       fullpath = talloc_asprintf(talloc_tos(), "%s/%s", conn->connectpath,
-                                  path);
-       if (fullpath == NULL) {
-               DEBUG(0, ("asprintf failed\n"));
-               return;
-       }
-       notify_trigger(notify_ctx, action, filter, fullpath);
-       TALLOC_FREE(fullpath);
+
+       notify_trigger(notify_ctx, action, filter, conn->connectpath, path);
 }
 
-static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
+static void notify_fsp(files_struct *fsp, struct timespec when,
+                      uint32_t action, const char *name)
 {
-       struct notify_change *change, *changes;
+       struct notify_change_event *change, *changes;
        char *tmp;
 
        if (fsp->notify == NULL) {
@@ -481,7 +542,8 @@ static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
 
        if (!(changes = talloc_realloc(
                      fsp->notify, fsp->notify->changes,
-                     struct notify_change, fsp->notify->num_changes+1))) {
+                     struct notify_change_event,
+                     fsp->notify->num_changes+1))) {
                DEBUG(0, ("talloc_realloc failed\n"));
                return;
        }
@@ -498,6 +560,7 @@ static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
        string_replace(tmp, '/', '\\');
        change->name = tmp;     
 
+       change->when = when;
        change->action = action;
        fsp->notify->num_changes += 1;
 
@@ -531,7 +594,7 @@ static void notify_fsp(files_struct *fsp, uint32 action, const char *name)
        change_notify_remove_request(fsp->conn->sconn, fsp->notify->requests);
 }
 
-char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32 filter)
+char *notify_filter_string(TALLOC_CTX *mem_ctx, uint32_t filter)
 {
        char *result = NULL;