#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 {
+ /*
+ * Filters for reinitializing after notifyd has been restarted
+ */
+ uint32_t filter;
+ uint32_t subdir_filter;
+
/*
* If no requests are pending, changes are queued here. Simple array,
* we only append.
*/
+ uint32_t max_buffer_size;
+
/*
* num_changes == -1 means that we have got a catch-all change, when
* 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
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);
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)
{
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 &&
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;
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 &&
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)) {
return;
}
- if (max_param == 0 || notify_buf == NULL) {
+ if (notify_buf == NULL) {
reply_fn(req, NT_STATUS_OK, NULL, 0);
return;
}
+ max_param = MIN(max_param, notify_buf->max_buffer_size);
+
if (!notify_marshall_changes(notify_buf->num_changes, max_param,
notify_buf->changes, &blob)) {
/*
notify_buf->num_changes = 0;
}
-static void notify_callback(void *private_data, const struct notify_event *e)
+struct notify_fsp_state {
+ struct files_struct *notified_fsp;
+ struct timespec when;
+ const struct notify_event *e;
+};
+
+static struct files_struct *notify_fsp_cb(struct files_struct *fsp,
+ void *private_data)
{
- 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);
+ struct notify_fsp_state *state = private_data;
+
+ if (fsp == state->notified_fsp) {
+ DBG_DEBUG("notify_callback called for %s\n", fsp_str_dbg(fsp));
+ notify_fsp(fsp, state->when, state->e->action, state->e->path);
+ return fsp;
+ }
+
+ return NULL;
}
-static void sys_notify_callback(struct sys_notify_context *ctx,
- void *private_data,
- struct notify_event *e)
+void notify_callback(struct smbd_server_connection *sconn,
+ void *private_data, struct timespec when,
+ const 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);
+ struct notify_fsp_state state = {
+ .notified_fsp = private_data, .when = when, .e = e
+ };
+ files_forall(sconn, notify_fsp_cb, &state);
}
-NTSTATUS change_notify_create(struct files_struct *fsp, uint32 filter,
+NTSTATUS change_notify_create(struct files_struct *fsp,
+ uint32_t max_buffer_size,
+ uint32_t filter,
bool recursive)
{
- char *fullpath;
- size_t len;
- uint32_t subdir_filter;
+ size_t len = fsp_fullbasepath(fsp, NULL, 0);
+ char fullpath[len+1];
NTSTATUS status = NT_STATUS_NOT_IMPLEMENTED;
if (fsp->notify != NULL) {
DEBUG(0, ("talloc failed\n"));
return NT_STATUS_NO_MEMORY;
}
+ fsp->notify->filter = filter;
+ fsp->notify->subdir_filter = recursive ? filter : 0;
+ fsp->notify->max_buffer_size = max_buffer_size;
- /* 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)) {
+ if ((fsp->notify->filter != 0) ||
+ (fsp->notify->subdir_filter != 0)) {
status = notify_add(fsp->conn->sconn->notify_ctx,
- fullpath, filter, subdir_filter,
- notify_callback, fsp);
+ fullpath, fsp->notify->filter,
+ fsp->notify->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,
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);
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) {
+ NTSTATUS sstatus;
+
+ if (smb2req->session == NULL) {
+ sstatus = NT_STATUS_USER_SESSION_DELETED;
+ } else {
+ sstatus = smb2req->session->status;
+ }
+
+ if (NT_STATUS_EQUAL(sstatus, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
+ sstatus = NT_STATUS_OK;
+ }
+
+ if (!NT_STATUS_IS_OK(sstatus)) {
+ notify_status = STATUS_NOTIFY_CLEANUP;
+ } else 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.
*****************************************************************************/
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)
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);
+}
+
+static struct files_struct *smbd_notifyd_reregister(struct files_struct *fsp,
+ void *private_data)
+{
+ DBG_DEBUG("reregister %s\n", fsp->fsp_name->base_name);
+
+ if ((fsp->conn->sconn->notify_ctx != NULL) &&
+ (fsp->notify != NULL) &&
+ ((fsp->notify->filter != 0) ||
+ (fsp->notify->subdir_filter != 0))) {
+ size_t len = fsp_fullbasepath(fsp, NULL, 0);
+ char fullpath[len+1];
+
+ NTSTATUS status;
+
+ fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
+ if (len > 1 && fullpath[len-1] == '.' &&
+ fullpath[len-2] == '/') {
+ fullpath[len-2] = '\0';
+ }
+
+ status = notify_add(fsp->conn->sconn->notify_ctx,
+ fullpath, fsp->notify->filter,
+ fsp->notify->subdir_filter, fsp);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_DEBUG("notify_add failed: %s\n",
+ nt_errstr(status));
+ }
+ }
+ return NULL;
+}
+
+void smbd_notifyd_restarted(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);
+
+ TALLOC_FREE(sconn->notify_ctx);
+
+ sconn->notify_ctx = notify_init(sconn, sconn->msg_ctx,
+ sconn, notify_callback);
+ if (sconn->notify_ctx == NULL) {
+ DBG_DEBUG("notify_init failed\n");
+ return;
+ }
+
+ files_forall(sconn, smbd_notifyd_reregister, sconn->notify_ctx);
}
/****************************************************************************
}
}
-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, *to_free;
- char tmpbuf[PATH_MAX];
- ssize_t len;
if (path[0] == '.' && path[1] == '/') {
path += 2;
}
- len = full_path_tos(conn->connectpath, path, tmpbuf, sizeof(tmpbuf),
- &fullpath, &to_free);
- if (len == -1) {
- DEBUG(0, ("full_path_tos failed\n"));
- return;
- }
- notify_trigger(notify_ctx, action, filter, fullpath);
- TALLOC_FREE(to_free);
+
+ 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) {
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;
}
string_replace(tmp, '/', '\\');
change->name = tmp;
+ change->when = when;
change->action = action;
fsp->notify->num_changes += 1;
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;