smbd: Cancel pending notifies if the directory goes away
authorVolker Lendecke <vl@samba.org>
Tue, 21 Apr 2015 08:16:16 +0000 (10:16 +0200)
committerJeremy Allison <jra@samba.org>
Wed, 22 Apr 2015 21:00:20 +0000 (23:00 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
librpc/idl/messaging.idl
source3/locking/locking.c
source3/smbd/notify.c
source3/smbd/proto.h
source3/smbd/service.c

index 04dfa1eff8f0d2309af4ecef17d3d96bd13da423..2b902ec0a1dcd9ade21a1b8ca91866d026c127ba 100644 (file)
@@ -96,6 +96,9 @@ interface messaging
                MSG_SMB_TELL_NUM_CHILDREN       = 0x0317,
                MSG_SMB_NUM_CHILDREN            = 0x0318,
 
+               /* Cancel a notify, directory got deleted */
+               MSG_SMB_NOTIFY_CANCEL_DELETED   = 0x0319,
+
                /* winbind messages */
                MSG_WINBIND_FINISHED            = 0x0401,
                MSG_WINBIND_FORGET_STATE        = 0x0402,
index bcc9bfeff85e7e21f3a99bdeef98631c21f176d0..ce595e12d0d2f1d8bee02b3ee610102c06cdd173 100644 (file)
@@ -46,6 +46,7 @@
 #include "messages.h"
 #include "util_tdb.h"
 #include "../librpc/gen_ndr/ndr_open_files.h"
+#include "librpc/gen_ndr/ndr_file_id.h"
 #include "locking/leases_db.h"
 
 #undef DBGC_CLASS
@@ -1118,9 +1119,12 @@ void set_delete_on_close_lck(files_struct *fsp,
                        const struct security_token *nt_tok,
                        const struct security_unix_token *tok)
 {
+       struct messaging_context *msg_ctx = fsp->conn->sconn->msg_ctx;
        struct share_mode_data *d = lck->data;
-       int i;
+       uint32_t i;
        bool ret;
+       DATA_BLOB fid_blob = {};
+       enum ndr_err_code ndr_err;
 
        SMB_ASSERT(nt_tok != NULL);
        SMB_ASSERT(tok != NULL);
@@ -1144,6 +1148,31 @@ void set_delete_on_close_lck(files_struct *fsp,
 
        ret = add_delete_on_close_token(lck->data, fsp->name_hash, nt_tok, tok);
        SMB_ASSERT(ret);
+
+       ndr_err = ndr_push_struct_blob(&fid_blob, talloc_tos(), &fsp->file_id,
+                                      (ndr_push_flags_fn_t)ndr_push_file_id);
+       if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
+               DEBUG(10, ("ndr_push_file_id failed: %s\n",
+                          ndr_errstr(ndr_err)));
+       }
+
+       for (i=0; i<d->num_share_modes; i++) {
+               struct share_mode_entry *e = &d->share_modes[i];
+               NTSTATUS status;
+
+               status = messaging_send(
+                       msg_ctx, e->pid, MSG_SMB_NOTIFY_CANCEL_DELETED,
+                       &fid_blob);
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       struct server_id_buf tmp;
+                       DEBUG(10, ("%s: messaging_send to %s returned %s\n",
+                                  __func__, server_id_str_buf(e->pid, &tmp),
+                                  nt_errstr(status)));
+               }
+       }
+
+       TALLOC_FREE(fid_blob.data);
 }
 
 bool set_delete_on_close(files_struct *fsp, bool delete_on_close,
index 5ac8c0c791da7f5333fc3b4b17485241ecd5778c..8cb44df3c1d4d236948d5a747efc2bab8f4a8cd7 100644 (file)
@@ -23,6 +23,7 @@
 #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;
@@ -418,6 +419,48 @@ void smbd_notify_cancel_by_smbreq(const struct smb_request *smbreq)
        change_notify_remove_request(sconn, map->req);
 }
 
+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);
+}
+
 /****************************************************************************
  Delete entries by fnum from the change notify pending queue.
 *****************************************************************************/
index f01bbbdfedc9c1c4208f16b76e57603dc20c92cd..a5144d5e0781c753eafea23681fb314a4e83fd9f 100644 (file)
@@ -517,6 +517,9 @@ NTSTATUS change_notify_add_request(struct smb_request *req,
                                void (*reply_fn)(struct smb_request *req,
                                        NTSTATUS error_code,
                                        uint8_t *buf, size_t len));
+void smbd_notify_cancel_deleted(struct messaging_context *msg,
+                               void *private_data, uint32_t msg_type,
+                               struct server_id server_id, DATA_BLOB *data);
 void remove_pending_change_notify_requests_by_mid(
        struct smbd_server_connection *sconn, uint64_t mid);
 void remove_pending_change_notify_requests_by_fid(files_struct *fsp,
index ada2d07aa7c48bf6f7164a5ddde1f7fed6e578c7..d11987e63eef4cc77a3edb5d1570c2cd48e6dd5d 100644 (file)
@@ -682,6 +682,10 @@ static NTSTATUS make_connection_snum(struct smbXsrv_connection *xconn,
                if (sconn->notify_ctx == NULL) {
                        sconn->notify_ctx = notify_init(
                                sconn, sconn->msg_ctx, sconn->ev_ctx);
+                       status = messaging_register(
+                               sconn->msg_ctx, sconn,
+                               MSG_SMB_NOTIFY_CANCEL_DELETED,
+                               smbd_notify_cancel_deleted);
                }
                if (sconn->sys_notify_ctx == NULL) {
                        sconn->sys_notify_ctx = sys_notify_context_create(