2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 this is the change notify database. It implements mechanisms for
22 storing current change notify waiters in a tdb, and checking if a
23 given event matches any of the stored notify waiiters.
27 #include "system/filesys.h"
28 #include "lib/tdb/include/tdb.h"
29 #include "lib/util/util_tdb.h"
30 #include "messaging/messaging.h"
32 #include "lib/messaging/irpc.h"
33 #include "librpc/gen_ndr/ndr_notify.h"
34 #include "lib/util/dlinklist.h"
35 #include "ntvfs/common/ntvfs_common.h"
36 #include "ntvfs/sysdep/sys_notify.h"
37 #include "cluster/cluster.h"
38 #include "param/param.h"
40 struct notify_context {
42 struct server_id server;
43 struct messaging_context *messaging_ctx;
44 struct notify_list *list;
45 struct notify_array *array;
47 struct sys_notify_context *sys_notify_ctx;
52 struct notify_list *next, *prev;
54 void (*callback)(void *, const struct notify_event *);
55 void *sys_notify_handle;
59 #define NOTIFY_KEY "notify array"
61 #define NOTIFY_ENABLE "notify:enable"
62 #define NOTIFY_ENABLE_DEFAULT true
64 static NTSTATUS notify_remove_all(struct notify_context *notify);
65 static void notify_handler(struct messaging_context *msg_ctx, void *private_data,
66 uint32_t msg_type, struct server_id server_id, DATA_BLOB *data);
69 destroy the notify context
71 static int notify_destructor(struct notify_context *notify)
73 messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
74 notify_remove_all(notify);
79 Open up the notify.tdb database. You should close it down using
80 talloc_free(). We need the messaging_ctx to allow for notifications
83 struct notify_context *notify_init(TALLOC_CTX *mem_ctx, struct server_id server,
84 struct messaging_context *messaging_ctx,
85 struct event_context *ev,
86 struct share_config *scfg)
88 struct notify_context *notify;
90 if (share_bool_option(scfg, NOTIFY_ENABLE, NOTIFY_ENABLE_DEFAULT) != true) {
94 notify = talloc(mem_ctx, struct notify_context);
99 notify->w = cluster_tdb_tmp_open(notify, global_loadparm, "notify.tdb", TDB_SEQNUM);
100 if (notify->w == NULL) {
105 notify->server = server;
106 notify->messaging_ctx = messaging_ctx;
108 notify->array = NULL;
109 notify->seqnum = tdb_get_seqnum(notify->w->tdb);
111 talloc_set_destructor(notify, notify_destructor);
113 /* register with the messaging subsystem for the notify
115 messaging_register(notify->messaging_ctx, notify,
116 MSG_PVFS_NOTIFY, notify_handler);
118 notify->sys_notify_ctx = sys_notify_context_create(scfg, notify, ev);
127 static NTSTATUS notify_lock(struct notify_context *notify)
129 if (tdb_lock_bystring(notify->w->tdb, NOTIFY_KEY) != 0) {
130 return NT_STATUS_INTERNAL_DB_CORRUPTION;
138 static void notify_unlock(struct notify_context *notify)
140 tdb_unlock_bystring(notify->w->tdb, NOTIFY_KEY);
144 load the notify array
146 static NTSTATUS notify_load(struct notify_context *notify)
150 enum ndr_err_code ndr_err;
153 seqnum = tdb_get_seqnum(notify->w->tdb);
155 if (seqnum == notify->seqnum && notify->array != NULL) {
159 notify->seqnum = seqnum;
161 talloc_free(notify->array);
162 notify->array = talloc_zero(notify, struct notify_array);
163 NT_STATUS_HAVE_NO_MEMORY(notify->array);
165 dbuf = tdb_fetch_bystring(notify->w->tdb, NOTIFY_KEY);
166 if (dbuf.dptr == NULL) {
170 blob.data = dbuf.dptr;
171 blob.length = dbuf.dsize;
173 ndr_err = ndr_pull_struct_blob(&blob, notify->array, notify->array,
174 (ndr_pull_flags_fn_t)ndr_pull_notify_array);
176 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
177 return ndr_map_error2ntstatus(ndr_err);
184 compare notify entries for sorting
186 static int notify_compare(const void *p1, const void *p2)
188 const struct notify_entry *e1 = p1, *e2 = p2;
189 return strcmp(e1->path, e2->path);
193 save the notify array
195 static NTSTATUS notify_save(struct notify_context *notify)
199 enum ndr_err_code ndr_err;
203 /* if possible, remove some depth arrays */
204 while (notify->array->num_depths > 0 &&
205 notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
206 notify->array->num_depths--;
209 /* we might just be able to delete the record */
210 if (notify->array->num_depths == 0) {
211 ret = tdb_delete_bystring(notify->w->tdb, NOTIFY_KEY);
213 return NT_STATUS_INTERNAL_DB_CORRUPTION;
218 tmp_ctx = talloc_new(notify);
219 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
221 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, notify->array,
222 (ndr_push_flags_fn_t)ndr_push_notify_array);
223 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
224 talloc_free(tmp_ctx);
225 return ndr_map_error2ntstatus(ndr_err);
228 dbuf.dptr = blob.data;
229 dbuf.dsize = blob.length;
231 ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
232 talloc_free(tmp_ctx);
234 return NT_STATUS_INTERNAL_DB_CORRUPTION;
242 handle incoming notify messages
244 static void notify_handler(struct messaging_context *msg_ctx, void *private_data,
245 uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
247 struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
248 enum ndr_err_code ndr_err;
249 struct notify_event ev;
250 TALLOC_CTX *tmp_ctx = talloc_new(notify);
251 struct notify_list *listel;
253 if (tmp_ctx == NULL) {
257 ndr_err = ndr_pull_struct_blob(data, tmp_ctx, &ev,
258 (ndr_pull_flags_fn_t)ndr_pull_notify_event);
259 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
260 talloc_free(tmp_ctx);
264 for (listel=notify->list;listel;listel=listel->next) {
265 if (listel->private_data == ev.private_data) {
266 listel->callback(listel->private_data, &ev);
271 talloc_free(tmp_ctx);
275 callback from sys_notify telling us about changes from the OS
277 static void sys_notify_callback(struct sys_notify_context *ctx,
278 void *ptr, struct notify_event *ev)
280 struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
281 ev->private_data = listel;
282 listel->callback(listel->private_data, ev);
286 add an entry to the notify array
288 static NTSTATUS notify_add_array(struct notify_context *notify, struct notify_entry *e,
289 void *private_data, int depth)
292 struct notify_depth *d;
293 struct notify_entry *ee;
295 /* possibly expand the depths array */
296 if (depth >= notify->array->num_depths) {
297 d = talloc_realloc(notify->array, notify->array->depth,
298 struct notify_depth, depth+1);
299 NT_STATUS_HAVE_NO_MEMORY(d);
300 for (i=notify->array->num_depths;i<=depth;i++) {
303 notify->array->depth = d;
304 notify->array->num_depths = depth+1;
306 d = ¬ify->array->depth[depth];
308 /* expand the entries array */
309 ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
311 NT_STATUS_HAVE_NO_MEMORY(ee);
314 d->entries[d->num_entries] = *e;
315 d->entries[d->num_entries].private_data = private_data;
316 d->entries[d->num_entries].server = notify->server;
317 d->entries[d->num_entries].path_len = strlen(e->path);
320 d->max_mask |= e->filter;
321 d->max_mask_subdir |= e->subdir_filter;
323 if (d->num_entries > 1) {
324 qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
327 /* recalculate the maximum masks */
329 d->max_mask_subdir = 0;
331 for (i=0;i<d->num_entries;i++) {
332 d->max_mask |= d->entries[i].filter;
333 d->max_mask_subdir |= d->entries[i].subdir_filter;
336 return notify_save(notify);
340 add a notify watch. This is called when a notify is first setup on a open
343 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
344 void (*callback)(void *, const struct notify_event *),
347 struct notify_entry e = *e0;
349 char *tmp_path = NULL;
350 struct notify_list *listel;
354 /* see if change notify is enabled at all */
355 if (notify == NULL) {
356 return NT_STATUS_NOT_IMPLEMENTED;
359 status = notify_lock(notify);
360 NT_STATUS_NOT_OK_RETURN(status);
362 status = notify_load(notify);
363 if (!NT_STATUS_IS_OK(status)) {
367 /* cope with /. on the end of the path */
368 len = strlen(e.path);
369 if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
370 tmp_path = talloc_strndup(notify, e.path, len-2);
371 if (tmp_path == NULL) {
372 status = NT_STATUS_NO_MEMORY;
378 depth = count_chars(e.path, '/');
380 listel = talloc_zero(notify, struct notify_list);
381 if (listel == NULL) {
382 status = NT_STATUS_NO_MEMORY;
386 listel->private_data = private_data;
387 listel->callback = callback;
388 listel->depth = depth;
389 DLIST_ADD(notify->list, listel);
391 /* ignore failures from sys_notify */
392 if (notify->sys_notify_ctx != NULL) {
394 this call will modify e.filter and e.subdir_filter
395 to remove bits handled by the backend
397 status = sys_notify_watch(notify->sys_notify_ctx, &e,
398 sys_notify_callback, listel,
399 &listel->sys_notify_handle);
400 if (NT_STATUS_IS_OK(status)) {
401 talloc_steal(listel, listel->sys_notify_handle);
405 /* if the system notify handler couldn't handle some of the
406 filter bits, or couldn't handle a request for recursion
407 then we need to install it in the array used for the
408 intra-samba notify handling */
409 if (e.filter != 0 || e.subdir_filter != 0) {
410 status = notify_add_array(notify, &e, private_data, depth);
414 notify_unlock(notify);
415 talloc_free(tmp_path);
421 remove a notify watch. Called when the directory handle is closed
423 NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
426 struct notify_list *listel;
428 struct notify_depth *d;
430 /* see if change notify is enabled at all */
431 if (notify == NULL) {
432 return NT_STATUS_NOT_IMPLEMENTED;
435 for (listel=notify->list;listel;listel=listel->next) {
436 if (listel->private_data == private_data) {
437 DLIST_REMOVE(notify->list, listel);
441 if (listel == NULL) {
442 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
445 depth = listel->depth;
449 status = notify_lock(notify);
450 NT_STATUS_NOT_OK_RETURN(status);
452 status = notify_load(notify);
453 if (!NT_STATUS_IS_OK(status)) {
454 notify_unlock(notify);
458 if (depth >= notify->array->num_depths) {
459 notify_unlock(notify);
460 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
463 /* we only have to search at the depth of this element */
464 d = ¬ify->array->depth[depth];
466 for (i=0;i<d->num_entries;i++) {
467 if (private_data == d->entries[i].private_data &&
468 cluster_id_equal(¬ify->server, &d->entries[i].server)) {
472 if (i == d->num_entries) {
473 notify_unlock(notify);
474 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
477 if (i < d->num_entries-1) {
478 memmove(&d->entries[i], &d->entries[i+1],
479 sizeof(d->entries[i])*(d->num_entries-(i+1)));
483 status = notify_save(notify);
485 notify_unlock(notify);
491 remove all notify watches for this messaging server
493 static NTSTATUS notify_remove_all(struct notify_context *notify)
496 int i, depth, del_count=0;
498 if (notify->list == NULL) {
502 status = notify_lock(notify);
503 NT_STATUS_NOT_OK_RETURN(status);
505 status = notify_load(notify);
506 if (!NT_STATUS_IS_OK(status)) {
507 notify_unlock(notify);
511 /* we have to search for all entries across all depths, looking for matches
513 for (depth=0;depth<notify->array->num_depths;depth++) {
514 struct notify_depth *d = ¬ify->array->depth[depth];
515 for (i=0;i<d->num_entries;i++) {
516 if (cluster_id_equal(¬ify->server, &d->entries[i].server)) {
517 if (i < d->num_entries-1) {
518 memmove(&d->entries[i], &d->entries[i+1],
519 sizeof(d->entries[i])*(d->num_entries-(i+1)));
529 status = notify_save(notify);
532 notify_unlock(notify);
539 send a notify message to another messaging server
541 static void notify_send(struct notify_context *notify, struct notify_entry *e,
542 const char *path, uint32_t action)
544 struct notify_event ev;
547 enum ndr_err_code ndr_err;
552 ev.private_data = e->private_data;
554 tmp_ctx = talloc_new(notify);
556 ndr_err = ndr_push_struct_blob(&data, tmp_ctx, &ev,
557 (ndr_push_flags_fn_t)ndr_push_notify_event);
558 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
559 talloc_free(tmp_ctx);
563 status = messaging_send(notify->messaging_ctx, e->server,
564 MSG_PVFS_NOTIFY, &data);
565 talloc_free(tmp_ctx);
570 trigger a notify message for anyone waiting on a matching event
572 This function is called a lot, and needs to be very fast. The unusual data structure
573 and traversal is designed to be fast in the average case, even for large numbers of
576 void notify_trigger(struct notify_context *notify,
577 uint32_t action, uint32_t filter, const char *path)
581 const char *p, *next_p;
583 /* see if change notify is enabled at all */
584 if (notify == NULL) {
588 status = notify_load(notify);
589 if (!NT_STATUS_IS_OK(status)) {
593 /* loop along the given path, working with each directory depth separately */
595 p && depth < notify->array->num_depths;
597 int p_len = p - path;
599 struct notify_depth *d = ¬ify->array->depth[depth];
600 next_p = strchr(p+1, '/');
602 /* see if there are any entries at this depth */
603 if (d->num_entries == 0) continue;
605 /* try to skip based on the maximum mask. If next_p is
606 NULL then we know it will be a 'this directory'
607 match, otherwise it must be a subdir match */
608 if (next_p != NULL) {
609 if (0 == (filter & d->max_mask_subdir)) {
613 if (0 == (filter & d->max_mask)) {
618 /* we know there is an entry here worth looking
619 for. Use a bisection search to find the first entry
620 with a matching path */
622 max_i = d->num_entries-1;
624 while (min_i < max_i) {
625 struct notify_entry *e;
629 cmp = strncmp(path, e->path, p_len);
631 if (p_len == e->path_len) {
636 } else if (cmp < 0) {
643 if (min_i != max_i) {
648 /* we now know that the entries start at min_i */
649 for (i=min_i;i<d->num_entries;i++) {
650 struct notify_entry *e = &d->entries[i];
651 if (p_len != e->path_len ||
652 strncmp(path, e->path, p_len) != 0) break;
653 if (next_p != NULL) {
654 if (0 == (filter & e->subdir_filter)) {
658 if (0 == (filter & e->filter)) {
662 notify_send(notify, e, path + e->path_len + 1, action);