r21077: A step to minimize the diff later: This pulls in unmodified files from Samba4,
authorVolker Lendecke <vlendec@samba.org>
Wed, 31 Jan 2007 12:24:59 +0000 (12:24 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:17:33 +0000 (12:17 -0500)
not compiled yet. This way the modifications become visible later.

ntvfs/common/notify.c -> smbd/notify_internal.c
ntvfs/sysdep/inotify.c -> smbd/notify_inotify.c

Naturally I had to disable notify again :-)

Volker

source/Makefile.in
source/smbd/notify.c
source/smbd/notify_inotify.c
source/smbd/notify_internal.c [new file with mode: 0644]

index 4b590119e99e9daaa8bff10fe8f8b8d5fbd9bc78..c21cea0aecdf07f9126bd9ad8d6e3e65cf12f893 100644 (file)
@@ -409,7 +409,7 @@ PROFILES_OBJ = utils/profiles.o \
 OPLOCK_OBJ = smbd/oplock.o smbd/oplock_irix.o smbd/oplock_linux.o
 
 NOTIFY_OBJ = smbd/notify.o smbd/notify_hash.o smbd/notify_kernel.o \
-       smbd/notify_fam.o smbd/notify_inotify.o
+       smbd/notify_fam.o
 
 VFS_DEFAULT_OBJ = modules/vfs_default.o
 VFS_AUDIT_OBJ = modules/vfs_audit.o
index d920633b0aa66f1f7af125501f328b2e718aa421..4c82b9fca82dde4576ef1e617bbd21de28cfa133 100644 (file)
@@ -529,11 +529,6 @@ BOOL init_change_notify(void)
 {
        cnotify = NULL;
 
-#if HAVE_INOTIFY
-       if ((cnotify == NULL) && lp_kernel_change_notify()) {
-               cnotify = inotify_notify_init(smbd_event_context());
-       }
-#endif
 #if HAVE_KERNEL_CHANGE_NOTIFY
        if (cnotify == NULL && lp_kernel_change_notify())
                cnotify = kernel_notify_init(smbd_event_context());
index 64ea62ff74189b38ff1b1ae9d93523f36a0f89e0..8bb0096dccd98397dc150b8f01cff65379360147 100644 (file)
@@ -1,27 +1,33 @@
+/* 
+   Unix SMB/CIFS implementation.
+
+   Copyright (C) Andrew Tridgell 2006
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
 /*
- * inotify change notify support
- *
- * Copyright (c) Andrew Tridgell 2006
- * Copyright (c) Volker Lendecke 2007
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
- */
+  notify implementation using inotify
+*/
 
 #include "includes.h"
-
-#ifdef HAVE_INOTIFY
+#include "system/filesys.h"
+#include "ntvfs/sysdep/sys_notify.h"
+#include "lib/events/events.h"
+#include "lib/util/dlinklist.h"
+#include "libcli/raw/smb.h"
 
 #include <linux/inotify.h>
 #include <asm/unistd.h>
@@ -46,218 +52,367 @@ static int inotify_rm_watch(int fd, int wd)
 }
 #endif
 
-struct inotify_ctx {
-       struct inotify_ctx *prev, *next;
-       int wd;
-       files_struct *fsp;
-};
 
-static struct inotify_ctx *inotify_list;
+/* older glibc headers don't have these defines either */
+#ifndef IN_ONLYDIR
+#define IN_ONLYDIR 0x01000000
+#endif
+#ifndef IN_MASK_ADD
+#define IN_MASK_ADD 0x20000000
+#endif
 
-static int inotify_watch_fd;
+struct inotify_private {
+       struct sys_notify_context *ctx;
+       int fd;
+       struct watch_context *watches;
+};
 
-/*
-  map from a change notify mask to a inotify mask. Remove any bits
-  which we can handle
-*/
-static const struct {
-       uint32_t notify_mask;
-       uint32_t inotify_mask;
-} inotify_mapping[] = {
-       {FILE_NOTIFY_CHANGE_FILE_NAME,
-        IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
-       {FILE_NOTIFY_CHANGE_DIR_NAME,
-        IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
-       {FILE_NOTIFY_CHANGE_ATTRIBUTES,
-        IN_ATTRIB|IN_MOVED_TO|IN_MOVED_FROM|IN_MODIFY},
-       {FILE_NOTIFY_CHANGE_LAST_WRITE,  IN_ATTRIB},
-       {FILE_NOTIFY_CHANGE_LAST_ACCESS, IN_ATTRIB},
-       {FILE_NOTIFY_CHANGE_EA,          IN_ATTRIB},
-       {FILE_NOTIFY_CHANGE_SECURITY,    IN_ATTRIB}
+struct watch_context {
+       struct watch_context *next, *prev;
+       struct inotify_private *in;
+       int wd;
+       sys_notify_callback_t callback;
+       void *private_data;
+       uint32_t mask; /* the inotify mask */
+       uint32_t filter; /* the windows completion filter */
+       const char *path;
 };
 
-static uint32_t inotify_map(uint32 *filter)
-{
-       int i;
-       uint32_t out=0;
-       for (i=0;i<ARRAY_SIZE(inotify_mapping);i++) {
-               if (inotify_mapping[i].notify_mask & *filter) {
-                       out |= inotify_mapping[i].inotify_mask;
-                       *filter &= ~inotify_mapping[i].notify_mask;
-               }
-       }
-       return out;
-}
 
-static int inotify_ctx_destructor(struct inotify_ctx *ctx)
+/*
+  destroy the inotify private context
+*/
+static int inotify_destructor(struct inotify_private *in)
 {
-       if (inotify_rm_watch(inotify_watch_fd, ctx->wd) == -1) {
-               DEBUG(0, ("inotify_rm_watch failed: %s\n", strerror(errno)));
-       }
-       DLIST_REMOVE(inotify_list, ctx);
+       close(in->fd);
        return 0;
 }
 
-static void *inotify_add(TALLOC_CTX *mem_ctx, 
-                        struct event_context *event_ctx,
-                        files_struct *fsp, uint32 *pfilter)
-{
-       struct inotify_ctx *ctx;
-       uint32_t inotify_mask;
-       pstring fullpath;
-       uint32 filter;
-       
-       filter = *pfilter;
-       if ((filter & (FILE_NOTIFY_CHANGE_FILE
-                      |FILE_NOTIFY_CHANGE_DIR_NAME)) == 0) {
-               /*
-                * This first implementation only looks at create/delete
-                */
-               return NULL;
-       }
 
-       inotify_mask = inotify_map(&filter);
-
-       if (inotify_mask == 0) {
-               DEBUG(10, ("inotify_mask == 0, nothing to do\n"));
-               return NULL;
+/*
+  see if a particular event from inotify really does match a requested
+  notify event in SMB
+*/
+static BOOL filter_match(struct watch_context *w, struct inotify_event *e)
+{
+       if ((e->mask & w->mask) == 0) {
+               /* this happens because inotify_add_watch() coalesces watches on the same
+                  path, oring their masks together */
+               return False;
        }
 
-       pstrcpy(fullpath, fsp->fsp_name);
-       if (!canonicalize_path(fsp->conn, fullpath)) {
-               DEBUG(0, ("failed to canonicalize path '%s'\n", fullpath));
-               return NULL;
+       /* SMB separates the filters for files and directories */
+       if (e->mask & IN_ISDIR) {
+               if ((w->filter & FILE_NOTIFY_CHANGE_DIR_NAME) == 0) {
+                       return False;
+               }
+       } else {
+               if ((e->mask & IN_ATTRIB) &&
+                   (w->filter & (FILE_NOTIFY_CHANGE_ATTRIBUTES|
+                                 FILE_NOTIFY_CHANGE_LAST_WRITE|
+                                 FILE_NOTIFY_CHANGE_LAST_ACCESS|
+                                 FILE_NOTIFY_CHANGE_EA|
+                                 FILE_NOTIFY_CHANGE_SECURITY))) {
+                       return True;
+               }
+               if ((e->mask & IN_MODIFY) && 
+                   (w->filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)) {
+                       return True;
+               }
+               if ((w->filter & FILE_NOTIFY_CHANGE_FILE_NAME) == 0) {
+                       return False;
+               }
        }
 
-       if (*fullpath != '/') {
-               DEBUG(0, ("canonicalized path '%s' into `%s`\n", fsp->fsp_name,
-                         fullpath));
-               DEBUGADD(0, ("but expected an absolute path\n"));
-               return NULL;
-       }
+       return True;
+}
        
-       if (!(ctx = TALLOC_P(mem_ctx, struct inotify_ctx))) {
-               DEBUG(0, ("talloc failed\n"));
-               return NULL;
-       }
-
-       ctx->fsp = fsp;
-       ctx->wd = inotify_add_watch(inotify_watch_fd, fullpath, inotify_mask);
 
-       if (ctx->wd == -1) {
-               DEBUG(5, ("inotify_add_watch failed: %s\n", strerror(errno)));
-               TALLOC_FREE(ctx);
-               return NULL;
-       }
-
-       DLIST_ADD(inotify_list, ctx);
-       talloc_set_destructor(ctx, inotify_ctx_destructor);
 
-       *pfilter = filter;
-       return ctx;
-}
-
-static void inotify_dispatch(struct inotify_event *e)
+/*
+  dispatch one inotify event
+  
+  the cookies are used to correctly handle renames
+*/
+static void inotify_dispatch(struct inotify_private *in, 
+                            struct inotify_event *e, 
+                            uint32_t prev_cookie,
+                            struct inotify_event *e2)
 {
-       struct inotify_ctx *ctx;
+       struct watch_context *w, *next;
+       struct notify_event ne;
+
+       /* ignore extraneous events, such as unmount and IN_IGNORED events */
+       if ((e->mask & (IN_ATTRIB|IN_MODIFY|IN_CREATE|IN_DELETE|
+                       IN_MOVED_FROM|IN_MOVED_TO)) == 0) {
+               return;
+       }
 
-       for (ctx = inotify_list; ctx; ctx = ctx->next) {
-               if (ctx->wd == e->wd) {
-                       break;
+       /* map the inotify mask to a action. This gets complicated for
+          renames */
+       if (e->mask & IN_CREATE) {
+               ne.action = NOTIFY_ACTION_ADDED;
+       } else if (e->mask & IN_DELETE) {
+               ne.action = NOTIFY_ACTION_REMOVED;
+       } else if (e->mask & IN_MOVED_FROM) {
+               if (e2 != NULL && e2->cookie == e->cookie) {
+                       ne.action = NOTIFY_ACTION_OLD_NAME;
+               } else {
+                       ne.action = NOTIFY_ACTION_REMOVED;
+               }
+       } else if (e->mask & IN_MOVED_TO) {
+               if (e->cookie == prev_cookie) {
+                       ne.action = NOTIFY_ACTION_NEW_NAME;
+               } else {
+                       ne.action = NOTIFY_ACTION_ADDED;
                }
+       } else {
+               ne.action = NOTIFY_ACTION_MODIFIED;
        }
+       ne.path = e->name;
 
-       if (ctx == NULL) {
-               /* not found */
-               return;
+       /* find any watches that have this watch descriptor */
+       for (w=in->watches;w;w=next) {
+               next = w->next;
+               if (w->wd == e->wd && filter_match(w, e)) {
+                       w->callback(in->ctx, w->private_data, &ne);
+               }
        }
 
-       if (e->mask & IN_CREATE) {
-               notify_fsp(ctx->fsp, NOTIFY_ACTION_ADDED, e->name);
+       /* SMB expects a file rename to generate three events, two for
+          the rename and the other for a modify of the
+          destination. Strange! */
+       if (ne.action != NOTIFY_ACTION_NEW_NAME ||
+           (e->mask & IN_ISDIR) != 0) {
+               return;
        }
 
-       if (e->mask & IN_DELETE) {
-               notify_fsp(ctx->fsp, NOTIFY_ACTION_REMOVED, e->name);
+       ne.action = NOTIFY_ACTION_MODIFIED;
+       e->mask = IN_ATTRIB;
+       
+       for (w=in->watches;w;w=next) {
+               next = w->next;
+               if (w->wd == e->wd && filter_match(w, e) &&
+                   !(w->filter & FILE_NOTIFY_CHANGE_CREATION)) {
+                       w->callback(in->ctx, w->private_data, &ne);
+               }
        }
 }
 
-static void inotify_callback(struct event_context *event_ctx,
-                            struct fd_event *event,
-                            uint16 flags,
-                            void *private_data)
+/*
+  called when the kernel has some events for us
+*/
+static void inotify_handler(struct event_context *ev, struct fd_event *fde,
+                           uint16_t flags, void *private_data)
 {
-       char *buf, *p;
-       int bufsize;
+       struct inotify_private *in = talloc_get_type(private_data,
+                                                    struct inotify_private);
+       int bufsize = 0;
+       struct inotify_event *e0, *e;
+       uint32_t prev_cookie=0;
 
        /*
          we must use FIONREAD as we cannot predict the length of the
          filenames, and thus can't know how much to allocate
          otherwise
        */
-       if ((ioctl(inotify_watch_fd, FIONREAD, &bufsize) != 0)
-           || (bufsize == 0)) {
+       if (ioctl(in->fd, FIONREAD, &bufsize) != 0 || 
+           bufsize == 0) {
                DEBUG(0,("No data on inotify fd?!\n"));
                return;
        }
 
-       if (!(buf = SMB_MALLOC_ARRAY(char, bufsize))) {
-               DEBUG(0, ("malloc failed\n"));
-               return;
-       }
+       e0 = e = talloc_size(in, bufsize);
+       if (e == NULL) return;
 
-       if (read(inotify_watch_fd, buf, bufsize) != bufsize) {
+       if (read(in->fd, e0, bufsize) != bufsize) {
                DEBUG(0,("Failed to read all inotify data\n"));
-               SAFE_FREE(buf);
+               talloc_free(e0);
                return;
        }
 
-       p = buf;
+       /* we can get more than one event in the buffer */
+       while (bufsize >= sizeof(*e)) {
+               struct inotify_event *e2 = NULL;
+               bufsize -= e->len + sizeof(*e);
+               if (bufsize >= sizeof(*e)) {
+                       e2 = (struct inotify_event *)(e->len + sizeof(*e) + (char *)e);
+               }
+               inotify_dispatch(in, e, prev_cookie, e2);
+               prev_cookie = e->cookie;
+               e = e2;
+       }
 
-       while (bufsize > sizeof(struct inotify_event)) {
-               struct inotify_event *iev = (struct inotify_event *)p;
-               size_t len = sizeof(struct inotify_event) + iev->len;
+       talloc_free(e0);
+}
 
-               if ((len > bufsize)
-                   || ((iev->len != 0) && (iev->name[iev->len-1] != '\0'))) {
-                       smb_panic("invalid inotify reply\n");
-               }
+/*
+  setup the inotify handle - called the first time a watch is added on
+  this context
+*/
+static NTSTATUS inotify_setup(struct sys_notify_context *ctx)
+{
+       struct inotify_private *in;
 
-               inotify_dispatch(iev);
+       if (!lp_parm_bool(-1, "notify", "inotify", True)) {
+               return NT_STATUS_INVALID_SYSTEM_SERVICE;
+       }
 
-               p += len;
-               bufsize -= len;
+       in = talloc(ctx, struct inotify_private);
+       NT_STATUS_HAVE_NO_MEMORY(in);
+       in->fd = inotify_init();
+       if (in->fd == -1) {
+               DEBUG(0,("Failed to init inotify - %s\n", strerror(errno)));
+               talloc_free(in);
+               return map_nt_error_from_unix(errno);
        }
+       in->ctx = ctx;
+       in->watches = NULL;
+
+       ctx->private_data = in;
+       talloc_set_destructor(in, inotify_destructor);
 
-       SAFE_FREE(buf);
-       return;
+       /* add a event waiting for the inotify fd to be readable */
+       event_add_fd(ctx->ev, in, in->fd, EVENT_FD_READ, inotify_handler, in);
+       
+       return NT_STATUS_OK;
 }
 
-static struct cnotify_fns inotify_fns =
-{
-    inotify_add,
+
+/*
+  map from a change notify mask to a inotify mask. Remove any bits
+  which we can handle
+*/
+static const struct {
+       uint32_t notify_mask;
+       uint32_t inotify_mask;
+} inotify_mapping[] = {
+       {FILE_NOTIFY_CHANGE_FILE_NAME,   IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
+       {FILE_NOTIFY_CHANGE_DIR_NAME,    IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO},
+       {FILE_NOTIFY_CHANGE_ATTRIBUTES,  IN_ATTRIB|IN_MOVED_TO|IN_MOVED_FROM|IN_MODIFY},
+       {FILE_NOTIFY_CHANGE_LAST_WRITE,  IN_ATTRIB},
+       {FILE_NOTIFY_CHANGE_LAST_ACCESS, IN_ATTRIB},
+       {FILE_NOTIFY_CHANGE_EA,          IN_ATTRIB},
+       {FILE_NOTIFY_CHANGE_SECURITY,    IN_ATTRIB}
 };
 
-struct cnotify_fns *inotify_notify_init(struct event_context *event_ctx)
+static uint32_t inotify_map(struct notify_entry *e)
+{
+       int i;
+       uint32_t out=0;
+       for (i=0;i<ARRAY_SIZE(inotify_mapping);i++) {
+               if (inotify_mapping[i].notify_mask & e->filter) {
+                       out |= inotify_mapping[i].inotify_mask;
+                       e->filter &= ~inotify_mapping[i].notify_mask;
+               }
+       }
+       return out;
+}
+
+/*
+  destroy a watch
+*/
+static int watch_destructor(struct watch_context *w)
 {
-       inotify_watch_fd = inotify_init();
+       struct inotify_private *in = w->in;
+       int wd = w->wd;
+       DLIST_REMOVE(w->in->watches, w);
+
+       /* only rm the watch if its the last one with this wd */
+       for (w=in->watches;w;w=w->next) {
+               if (w->wd == wd) break;
+       }
+       if (w == NULL) {
+               inotify_rm_watch(in->fd, wd);
+       }
+       return 0;
+}
+
+
+/*
+  add a watch. The watch is removed when the caller calls
+  talloc_free() on *handle
+*/
+static NTSTATUS inotify_watch(struct sys_notify_context *ctx,
+                             struct notify_entry *e,
+                             sys_notify_callback_t callback,
+                             void *private_data, 
+                             void *handle_p)
+{
+       struct inotify_private *in;
+       int wd;
+       uint32_t mask;
+       struct watch_context *w;
+       uint32_t filter = e->filter;
+       void **handle = (void **)handle_p;
+
+       /* maybe setup the inotify fd */
+       if (ctx->private_data == NULL) {
+               NTSTATUS status;
+               status = inotify_setup(ctx);
+               NT_STATUS_NOT_OK_RETURN(status);
+       }
 
-       DEBUG(10, ("inotify_notify_init called\n"));
+       in = talloc_get_type(ctx->private_data, struct inotify_private);
 
-       if (inotify_watch_fd == -1) {
-               DEBUG(0, ("inotify_init failed: %s\n", strerror(errno)));
-               return NULL;
+       mask = inotify_map(e);
+       if (mask == 0) {
+               /* this filter can't be handled by inotify */
+               return NT_STATUS_INVALID_PARAMETER;
        }
 
-       if (event_add_fd(event_ctx, NULL, inotify_watch_fd,
-                        EVENT_FD_READ, inotify_callback,
-                        NULL) == NULL) {
-               DEBUG(0, ("event_add_fd failed\n"));
-               close(inotify_watch_fd);
-               inotify_watch_fd = -1;
-               return NULL;
+       /* using IN_MASK_ADD allows us to cope with inotify() returning the same
+          watch descriptor for muliple watches on the same path */
+       mask |= (IN_MASK_ADD | IN_ONLYDIR);
+
+       /* get a new watch descriptor for this path */
+       wd = inotify_add_watch(in->fd, e->path, mask);
+       if (wd == -1) {
+               e->filter = filter;
+               return map_nt_error_from_unix(errno);
        }
 
-       return &inotify_fns;
+       w = talloc(in, struct watch_context);
+       if (w == NULL) {
+               inotify_rm_watch(in->fd, wd);
+               e->filter = filter;
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       w->in = in;
+       w->wd = wd;
+       w->callback = callback;
+       w->private_data = private_data;
+       w->mask = mask;
+       w->filter = filter;
+       w->path = talloc_strdup(w, e->path);
+       if (w->path == NULL) {
+               inotify_rm_watch(in->fd, wd);
+               e->filter = filter;
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       (*handle) = w;
+
+       DLIST_ADD(in->watches, w);
+
+       /* the caller frees the handle to stop watching */
+       talloc_set_destructor(w, watch_destructor);
+       
+       return NT_STATUS_OK;
 }
 
-#endif
+
+static struct sys_notify_backend inotify = {
+       .name = "inotify",
+       .notify_watch = inotify_watch
+};
+
+/*
+  initialialise the inotify module
+ */
+NTSTATUS sys_notify_inotify_init(void)
+{
+       /* register ourselves as a system inotify module */
+       return sys_notify_register(&inotify);
+}
diff --git a/source/smbd/notify_internal.c b/source/smbd/notify_internal.c
new file mode 100644 (file)
index 0000000..91fa8a1
--- /dev/null
@@ -0,0 +1,661 @@
+/* 
+   Unix SMB/CIFS implementation.
+
+   Copyright (C) Andrew Tridgell 2006
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+/*
+  this is the change notify database. It implements mechanisms for
+  storing current change notify waiters in a tdb, and checking if a
+  given event matches any of the stored notify waiiters.
+*/
+
+#include "includes.h"
+#include "system/filesys.h"
+#include "lib/tdb/include/tdb.h"
+#include "lib/util/util_tdb.h"
+#include "messaging/messaging.h"
+#include "db_wrap.h"
+#include "lib/messaging/irpc.h"
+#include "librpc/gen_ndr/ndr_notify.h"
+#include "lib/util/dlinklist.h"
+#include "ntvfs/common/ntvfs_common.h"
+#include "ntvfs/sysdep/sys_notify.h"
+#include "cluster/cluster.h"
+
+struct notify_context {
+       struct tdb_wrap *w;
+       struct server_id server;
+       struct messaging_context *messaging_ctx;
+       struct notify_list *list;
+       struct notify_array *array;
+       int seqnum;
+       struct sys_notify_context *sys_notify_ctx;
+};
+
+
+struct notify_list {
+       struct notify_list *next, *prev;
+       void *private_data;
+       void (*callback)(void *, const struct notify_event *);
+       void *sys_notify_handle;
+       int depth;
+};
+
+#define NOTIFY_KEY "notify array"
+
+#define NOTIFY_ENABLE          "notify:enable"
+#define NOTIFY_ENABLE_DEFAULT  True
+
+static NTSTATUS notify_remove_all(struct notify_context *notify);
+static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
+                          uint32_t msg_type, struct server_id server_id, DATA_BLOB *data);
+
+/*
+  destroy the notify context
+*/
+static int notify_destructor(struct notify_context *notify)
+{
+       messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
+       notify_remove_all(notify);
+       return 0;
+}
+
+/*
+  Open up the notify.tdb database. You should close it down using
+  talloc_free(). We need the messaging_ctx to allow for notifications
+  via internal messages
+*/
+struct notify_context *notify_init(TALLOC_CTX *mem_ctx, struct server_id server, 
+                                  struct messaging_context *messaging_ctx,
+                                  struct event_context *ev,
+                                  struct share_config *scfg)
+{
+       struct notify_context *notify;
+
+       if (share_bool_option(scfg, NOTIFY_ENABLE, NOTIFY_ENABLE_DEFAULT) != True) {
+               return NULL;
+       }
+
+       notify = talloc(mem_ctx, struct notify_context);
+       if (notify == NULL) {
+               return NULL;
+       }
+
+       notify->w = cluster_tdb_tmp_open(notify, "notify.tdb", TDB_SEQNUM);
+       if (notify->w == NULL) {
+               talloc_free(notify);
+               return NULL;
+       }
+
+       notify->server = server;
+       notify->messaging_ctx = messaging_ctx;
+       notify->list = NULL;
+       notify->array = NULL;
+       notify->seqnum = tdb_get_seqnum(notify->w->tdb);
+
+       talloc_set_destructor(notify, notify_destructor);
+
+       /* register with the messaging subsystem for the notify
+          message type */
+       messaging_register(notify->messaging_ctx, notify, 
+                          MSG_PVFS_NOTIFY, notify_handler);
+
+       notify->sys_notify_ctx = sys_notify_context_create(scfg, notify, ev);
+
+       return notify;
+}
+
+
+/*
+  lock the notify db
+*/
+static NTSTATUS notify_lock(struct notify_context *notify)
+{
+       if (tdb_lock_bystring(notify->w->tdb, NOTIFY_KEY) != 0) {
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+       return NT_STATUS_OK;
+}
+
+/*
+  unlock the notify db
+*/
+static void notify_unlock(struct notify_context *notify)
+{
+       tdb_unlock_bystring(notify->w->tdb, NOTIFY_KEY);
+}
+
+/*
+  load the notify array
+*/
+static NTSTATUS notify_load(struct notify_context *notify)
+{
+       TDB_DATA dbuf;
+       DATA_BLOB blob;
+       NTSTATUS status;
+       int seqnum;
+
+       seqnum = tdb_get_seqnum(notify->w->tdb);
+
+       if (seqnum == notify->seqnum && notify->array != NULL) {
+               return NT_STATUS_OK;
+       }
+
+       notify->seqnum = seqnum;
+
+       talloc_free(notify->array);
+       notify->array = talloc_zero(notify, struct notify_array);
+       NT_STATUS_HAVE_NO_MEMORY(notify->array);
+
+       dbuf = tdb_fetch_bystring(notify->w->tdb, NOTIFY_KEY);
+       if (dbuf.dptr == NULL) {
+               return NT_STATUS_OK;
+       }
+
+       blob.data = dbuf.dptr;
+       blob.length = dbuf.dsize;
+
+       status = ndr_pull_struct_blob(&blob, notify->array, notify->array, 
+                                     (ndr_pull_flags_fn_t)ndr_pull_notify_array);
+       free(dbuf.dptr);
+
+       return status;
+}
+
+/*
+  compare notify entries for sorting
+*/
+static int notify_compare(const void *p1, const void *p2)
+{
+       const struct notify_entry *e1 = p1, *e2 = p2;
+       return strcmp(e1->path, e2->path);
+}
+
+/*
+  save the notify array
+*/
+static NTSTATUS notify_save(struct notify_context *notify)
+{
+       TDB_DATA dbuf;
+       DATA_BLOB blob;
+       NTSTATUS status;
+       int ret;
+       TALLOC_CTX *tmp_ctx;
+
+       /* if possible, remove some depth arrays */
+       while (notify->array->num_depths > 0 &&
+              notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
+               notify->array->num_depths--;
+       }
+
+       /* we might just be able to delete the record */
+       if (notify->array->num_depths == 0) {
+               ret = tdb_delete_bystring(notify->w->tdb, NOTIFY_KEY);
+               if (ret != 0) {
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+               return NT_STATUS_OK;
+       }
+
+       tmp_ctx = talloc_new(notify);
+       NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
+
+       status = ndr_push_struct_blob(&blob, tmp_ctx, notify->array, 
+                                     (ndr_push_flags_fn_t)ndr_push_notify_array);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(tmp_ctx);
+               return status;
+       }
+
+       dbuf.dptr = blob.data;
+       dbuf.dsize = blob.length;
+               
+       ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
+       talloc_free(tmp_ctx);
+       if (ret != 0) {
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       return NT_STATUS_OK;
+}
+
+
+/*
+  handle incoming notify messages
+*/
+static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
+                          uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
+{
+       struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
+       NTSTATUS status;
+       struct notify_event ev;
+       TALLOC_CTX *tmp_ctx = talloc_new(notify);
+       struct notify_list *listel;
+
+       if (tmp_ctx == NULL) {
+               return;
+       }
+
+       status = ndr_pull_struct_blob(data, tmp_ctx, &ev, 
+                                     (ndr_pull_flags_fn_t)ndr_pull_notify_event);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(tmp_ctx);
+               return;
+       }
+
+       for (listel=notify->list;listel;listel=listel->next) {
+               if (listel->private_data == ev.private_data) {
+                       listel->callback(listel->private_data, &ev);
+                       break;
+               }
+       }
+
+       talloc_free(tmp_ctx);   
+}
+
+/*
+  callback from sys_notify telling us about changes from the OS
+*/
+static void sys_notify_callback(struct sys_notify_context *ctx, 
+                               void *ptr, struct notify_event *ev)
+{
+       struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
+       ev->private_data = listel;
+       listel->callback(listel->private_data, ev);
+}
+
+/*
+  add an entry to the notify array
+*/
+static NTSTATUS notify_add_array(struct notify_context *notify, struct notify_entry *e,
+                                void *private_data, int depth)
+{
+       int i;
+       struct notify_depth *d;
+       struct notify_entry *ee;
+
+       /* possibly expand the depths array */
+       if (depth >= notify->array->num_depths) {
+               d = talloc_realloc(notify->array, notify->array->depth, 
+                                  struct notify_depth, depth+1);
+               NT_STATUS_HAVE_NO_MEMORY(d);
+               for (i=notify->array->num_depths;i<=depth;i++) {
+                       ZERO_STRUCT(d[i]);
+               }
+               notify->array->depth = d;
+               notify->array->num_depths = depth+1;
+       }
+       d = &notify->array->depth[depth];
+
+       /* expand the entries array */
+       ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
+                           d->num_entries+1);
+       NT_STATUS_HAVE_NO_MEMORY(ee);
+       d->entries = ee;
+
+       d->entries[d->num_entries] = *e;
+       d->entries[d->num_entries].private_data = private_data;
+       d->entries[d->num_entries].server = notify->server;
+       d->entries[d->num_entries].path_len = strlen(e->path);
+       d->num_entries++;
+
+       d->max_mask |= e->filter;
+       d->max_mask_subdir |= e->subdir_filter;
+
+       if (d->num_entries > 1) {
+               qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
+       }
+
+       /* recalculate the maximum masks */
+       d->max_mask = 0;
+       d->max_mask_subdir = 0;
+
+       for (i=0;i<d->num_entries;i++) {
+               d->max_mask |= d->entries[i].filter;
+               d->max_mask_subdir |= d->entries[i].subdir_filter;
+       }
+
+       return notify_save(notify);
+}
+
+/*
+  add a notify watch. This is called when a notify is first setup on a open
+  directory handle.
+*/
+NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
+                   void (*callback)(void *, const struct notify_event *), 
+                   void *private_data)
+{
+       struct notify_entry e = *e0;
+       NTSTATUS status;
+       char *tmp_path = NULL;
+       struct notify_list *listel;
+       size_t len;
+       int depth;
+
+       /* see if change notify is enabled at all */
+       if (notify == NULL) {
+               return NT_STATUS_NOT_IMPLEMENTED;
+       }
+
+       status = notify_lock(notify);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       status = notify_load(notify);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto done;
+       }
+
+       /* cope with /. on the end of the path */
+       len = strlen(e.path);
+       if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
+               tmp_path = talloc_strndup(notify, e.path, len-2);
+               if (tmp_path == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto done;
+               }
+               e.path = tmp_path;
+       }
+
+       depth = count_chars(e.path, '/');
+
+       listel = talloc_zero(notify, struct notify_list);
+       if (listel == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto done;
+       }
+
+       listel->private_data = private_data;
+       listel->callback = callback;
+       listel->depth = depth;
+       DLIST_ADD(notify->list, listel);
+
+       /* ignore failures from sys_notify */
+       if (notify->sys_notify_ctx != NULL) {
+               /*
+                 this call will modify e.filter and e.subdir_filter
+                 to remove bits handled by the backend
+               */
+               status = sys_notify_watch(notify->sys_notify_ctx, &e,
+                                         sys_notify_callback, listel, 
+                                         &listel->sys_notify_handle);
+               if (NT_STATUS_IS_OK(status)) {
+                       talloc_steal(listel, listel->sys_notify_handle);
+               }
+       }
+
+       /* if the system notify handler couldn't handle some of the
+          filter bits, or couldn't handle a request for recursion
+          then we need to install it in the array used for the
+          intra-samba notify handling */
+       if (e.filter != 0 || e.subdir_filter != 0) {
+               status = notify_add_array(notify, &e, private_data, depth);
+       }
+
+done:
+       notify_unlock(notify);
+       talloc_free(tmp_path);
+
+       return status;
+}
+
+/*
+  remove a notify watch. Called when the directory handle is closed
+*/
+NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
+{
+       NTSTATUS status;
+       struct notify_list *listel;
+       int i, depth;
+       struct notify_depth *d;
+
+       /* see if change notify is enabled at all */
+       if (notify == NULL) {
+               return NT_STATUS_NOT_IMPLEMENTED;
+       }
+
+       for (listel=notify->list;listel;listel=listel->next) {
+               if (listel->private_data == private_data) {
+                       DLIST_REMOVE(notify->list, listel);
+                       break;
+               }
+       }
+       if (listel == NULL) {
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       depth = listel->depth;
+
+       talloc_free(listel);
+
+       status = notify_lock(notify);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       status = notify_load(notify);
+       if (!NT_STATUS_IS_OK(status)) {
+               notify_unlock(notify);
+               return status;
+       }
+
+       if (depth >= notify->array->num_depths) {
+               notify_unlock(notify);
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       /* we only have to search at the depth of this element */
+       d = &notify->array->depth[depth];
+
+       for (i=0;i<d->num_entries;i++) {
+               if (private_data == d->entries[i].private_data &&
+                   cluster_id_equal(&notify->server, &d->entries[i].server)) {
+                       break;
+               }
+       }
+       if (i == d->num_entries) {
+               notify_unlock(notify);
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       if (i < d->num_entries-1) {
+               memmove(&d->entries[i], &d->entries[i+1], 
+                       sizeof(d->entries[i])*(d->num_entries-(i+1)));
+       }
+       d->num_entries--;
+
+       status = notify_save(notify);
+
+       notify_unlock(notify);
+
+       return status;
+}
+
+/*
+  remove all notify watches for this messaging server
+*/
+static NTSTATUS notify_remove_all(struct notify_context *notify)
+{
+       NTSTATUS status;
+       int i, depth, del_count=0;
+
+       if (notify->list == NULL) {
+               return NT_STATUS_OK;
+       }
+
+       status = notify_lock(notify);
+       NT_STATUS_NOT_OK_RETURN(status);
+
+       status = notify_load(notify);
+       if (!NT_STATUS_IS_OK(status)) {
+               notify_unlock(notify);
+               return status;
+       }
+
+       /* we have to search for all entries across all depths, looking for matches
+          for our server id */
+       for (depth=0;depth<notify->array->num_depths;depth++) {
+               struct notify_depth *d = &notify->array->depth[depth];
+               for (i=0;i<d->num_entries;i++) {
+                       if (cluster_id_equal(&notify->server, &d->entries[i].server)) {
+                               if (i < d->num_entries-1) {
+                                       memmove(&d->entries[i], &d->entries[i+1], 
+                                               sizeof(d->entries[i])*(d->num_entries-(i+1)));
+                               }
+                               i--;
+                               d->num_entries--;
+                               del_count++;
+                       }
+               }
+       }
+
+       if (del_count > 0) {
+               status = notify_save(notify);
+       }
+
+       notify_unlock(notify);
+
+       return status;
+}
+
+
+/*
+  send a notify message to another messaging server
+*/
+static void notify_send(struct notify_context *notify, struct notify_entry *e,
+                       const char *path, uint32_t action)
+{
+       struct notify_event ev;
+       DATA_BLOB data;
+       NTSTATUS status;
+       TALLOC_CTX *tmp_ctx;
+
+       ev.action = action;
+       ev.path = path;
+       ev.private_data = e->private_data;
+
+       tmp_ctx = talloc_new(notify);
+
+       status = ndr_push_struct_blob(&data, tmp_ctx, &ev, 
+                                     (ndr_push_flags_fn_t)ndr_push_notify_event);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(tmp_ctx);
+               return;
+       }
+
+       status = messaging_send(notify->messaging_ctx, e->server, 
+                               MSG_PVFS_NOTIFY, &data);
+       talloc_free(tmp_ctx);
+}
+
+
+/*
+  trigger a notify message for anyone waiting on a matching event
+
+  This function is called a lot, and needs to be very fast. The unusual data structure
+  and traversal is designed to be fast in the average case, even for large numbers of
+  notifies
+*/
+void notify_trigger(struct notify_context *notify,
+                   uint32_t action, uint32_t filter, const char *path)
+{
+       NTSTATUS status;
+       int depth;
+       const char *p, *next_p;
+
+       /* see if change notify is enabled at all */
+       if (notify == NULL) {
+               return;
+       }
+
+       status = notify_load(notify);
+       if (!NT_STATUS_IS_OK(status)) {
+               return;
+       }
+
+       /* loop along the given path, working with each directory depth separately */
+       for (depth=0,p=path;
+            p && depth < notify->array->num_depths;
+            p=next_p,depth++) {
+               int p_len = p - path;
+               int min_i, max_i, i;
+               struct notify_depth *d = &notify->array->depth[depth];
+               next_p = strchr(p+1, '/');
+
+               /* see if there are any entries at this depth */
+               if (d->num_entries == 0) continue;
+               
+               /* try to skip based on the maximum mask. If next_p is
+                NULL then we know it will be a 'this directory'
+                match, otherwise it must be a subdir match */
+               if (next_p != NULL) {
+                       if (0 == (filter & d->max_mask_subdir)) {
+                               continue;
+                       }
+               } else {
+                       if (0 == (filter & d->max_mask)) {
+                               continue;
+                       }
+               }
+
+               /* we know there is an entry here worth looking
+                for. Use a bisection search to find the first entry
+                with a matching path */
+               min_i = 0;
+               max_i = d->num_entries-1;
+
+               while (min_i < max_i) {
+                       struct notify_entry *e;
+                       int cmp;
+                       i = (min_i+max_i)/2;
+                       e = &d->entries[i];
+                       cmp = strncmp(path, e->path, p_len);
+                       if (cmp == 0) {
+                               if (p_len == e->path_len) {
+                                       max_i = i;
+                               } else {
+                                       max_i = i-1;
+                               }
+                       } else if (cmp < 0) {
+                               max_i = i-1;
+                       } else {
+                               min_i = i+1;
+                       }
+               }
+
+               if (min_i != max_i) {
+                       /* none match */
+                       continue;
+               }
+
+               /* we now know that the entries start at min_i */
+               for (i=min_i;i<d->num_entries;i++) {
+                       struct notify_entry *e = &d->entries[i];
+                       if (p_len != e->path_len ||
+                           strncmp(path, e->path, p_len) != 0) break;
+                       if (next_p != NULL) {
+                               if (0 == (filter & e->subdir_filter)) {
+                                       continue;
+                               }
+                       } else {
+                               if (0 == (filter & e->filter)) {
+                                       continue;
+                               }
+                       }
+                       notify_send(notify, e, path + e->path_len + 1, action);
+               }
+       }
+}