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"
39 struct notify_context {
41 struct server_id server;
42 struct messaging_context *messaging_ctx;
43 struct notify_list *list;
44 struct notify_array *array;
46 struct sys_notify_context *sys_notify_ctx;
51 struct notify_list *next, *prev;
53 void (*callback)(void *, const struct notify_event *);
54 void *sys_notify_handle;
58 #define NOTIFY_KEY "notify array"
60 #define NOTIFY_ENABLE "notify:enable"
61 #define NOTIFY_ENABLE_DEFAULT true
63 static NTSTATUS notify_remove_all(struct notify_context *notify);
64 static void notify_handler(struct messaging_context *msg_ctx, void *private_data,
65 uint32_t msg_type, struct server_id server_id, DATA_BLOB *data);
68 destroy the notify context
70 static int notify_destructor(struct notify_context *notify)
72 messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
73 notify_remove_all(notify);
78 Open up the notify.tdb database. You should close it down using
79 talloc_free(). We need the messaging_ctx to allow for notifications
82 struct notify_context *notify_init(TALLOC_CTX *mem_ctx, struct server_id server,
83 struct messaging_context *messaging_ctx,
84 struct event_context *ev,
85 struct share_config *scfg)
87 struct notify_context *notify;
89 if (share_bool_option(scfg, NOTIFY_ENABLE, NOTIFY_ENABLE_DEFAULT) != true) {
93 notify = talloc(mem_ctx, struct notify_context);
98 notify->w = cluster_tdb_tmp_open(notify, "notify.tdb", TDB_SEQNUM);
99 if (notify->w == NULL) {
104 notify->server = server;
105 notify->messaging_ctx = messaging_ctx;
107 notify->array = NULL;
108 notify->seqnum = tdb_get_seqnum(notify->w->tdb);
110 talloc_set_destructor(notify, notify_destructor);
112 /* register with the messaging subsystem for the notify
114 messaging_register(notify->messaging_ctx, notify,
115 MSG_PVFS_NOTIFY, notify_handler);
117 notify->sys_notify_ctx = sys_notify_context_create(scfg, notify, ev);
126 static NTSTATUS notify_lock(struct notify_context *notify)
128 if (tdb_lock_bystring(notify->w->tdb, NOTIFY_KEY) != 0) {
129 return NT_STATUS_INTERNAL_DB_CORRUPTION;
137 static void notify_unlock(struct notify_context *notify)
139 tdb_unlock_bystring(notify->w->tdb, NOTIFY_KEY);
143 load the notify array
145 static NTSTATUS notify_load(struct notify_context *notify)
149 enum ndr_err_code ndr_err;
152 seqnum = tdb_get_seqnum(notify->w->tdb);
154 if (seqnum == notify->seqnum && notify->array != NULL) {
158 notify->seqnum = seqnum;
160 talloc_free(notify->array);
161 notify->array = talloc_zero(notify, struct notify_array);
162 NT_STATUS_HAVE_NO_MEMORY(notify->array);
164 dbuf = tdb_fetch_bystring(notify->w->tdb, NOTIFY_KEY);
165 if (dbuf.dptr == NULL) {
169 blob.data = dbuf.dptr;
170 blob.length = dbuf.dsize;
172 ndr_err = ndr_pull_struct_blob(&blob, notify->array, notify->array,
173 (ndr_pull_flags_fn_t)ndr_pull_notify_array);
175 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
176 return ndr_map_error2ntstatus(ndr_err);
183 compare notify entries for sorting
185 static int notify_compare(const void *p1, const void *p2)
187 const struct notify_entry *e1 = p1, *e2 = p2;
188 return strcmp(e1->path, e2->path);
192 save the notify array
194 static NTSTATUS notify_save(struct notify_context *notify)
198 enum ndr_err_code ndr_err;
202 /* if possible, remove some depth arrays */
203 while (notify->array->num_depths > 0 &&
204 notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
205 notify->array->num_depths--;
208 /* we might just be able to delete the record */
209 if (notify->array->num_depths == 0) {
210 ret = tdb_delete_bystring(notify->w->tdb, NOTIFY_KEY);
212 return NT_STATUS_INTERNAL_DB_CORRUPTION;
217 tmp_ctx = talloc_new(notify);
218 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
220 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, notify->array,
221 (ndr_push_flags_fn_t)ndr_push_notify_array);
222 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
223 talloc_free(tmp_ctx);
224 return ndr_map_error2ntstatus(ndr_err);
227 dbuf.dptr = blob.data;
228 dbuf.dsize = blob.length;
230 ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
231 talloc_free(tmp_ctx);
233 return NT_STATUS_INTERNAL_DB_CORRUPTION;
241 handle incoming notify messages
243 static void notify_handler(struct messaging_context *msg_ctx, void *private_data,
244 uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
246 struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
247 enum ndr_err_code ndr_err;
248 struct notify_event ev;
249 TALLOC_CTX *tmp_ctx = talloc_new(notify);
250 struct notify_list *listel;
252 if (tmp_ctx == NULL) {
256 ndr_err = ndr_pull_struct_blob(data, tmp_ctx, &ev,
257 (ndr_pull_flags_fn_t)ndr_pull_notify_event);
258 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
259 talloc_free(tmp_ctx);
263 for (listel=notify->list;listel;listel=listel->next) {
264 if (listel->private_data == ev.private_data) {
265 listel->callback(listel->private_data, &ev);
270 talloc_free(tmp_ctx);
274 callback from sys_notify telling us about changes from the OS
276 static void sys_notify_callback(struct sys_notify_context *ctx,
277 void *ptr, struct notify_event *ev)
279 struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
280 ev->private_data = listel;
281 listel->callback(listel->private_data, ev);
285 add an entry to the notify array
287 static NTSTATUS notify_add_array(struct notify_context *notify, struct notify_entry *e,
288 void *private_data, int depth)
291 struct notify_depth *d;
292 struct notify_entry *ee;
294 /* possibly expand the depths array */
295 if (depth >= notify->array->num_depths) {
296 d = talloc_realloc(notify->array, notify->array->depth,
297 struct notify_depth, depth+1);
298 NT_STATUS_HAVE_NO_MEMORY(d);
299 for (i=notify->array->num_depths;i<=depth;i++) {
302 notify->array->depth = d;
303 notify->array->num_depths = depth+1;
305 d = ¬ify->array->depth[depth];
307 /* expand the entries array */
308 ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
310 NT_STATUS_HAVE_NO_MEMORY(ee);
313 d->entries[d->num_entries] = *e;
314 d->entries[d->num_entries].private_data = private_data;
315 d->entries[d->num_entries].server = notify->server;
316 d->entries[d->num_entries].path_len = strlen(e->path);
319 d->max_mask |= e->filter;
320 d->max_mask_subdir |= e->subdir_filter;
322 if (d->num_entries > 1) {
323 qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
326 /* recalculate the maximum masks */
328 d->max_mask_subdir = 0;
330 for (i=0;i<d->num_entries;i++) {
331 d->max_mask |= d->entries[i].filter;
332 d->max_mask_subdir |= d->entries[i].subdir_filter;
335 return notify_save(notify);
339 add a notify watch. This is called when a notify is first setup on a open
342 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
343 void (*callback)(void *, const struct notify_event *),
346 struct notify_entry e = *e0;
348 char *tmp_path = NULL;
349 struct notify_list *listel;
353 /* see if change notify is enabled at all */
354 if (notify == NULL) {
355 return NT_STATUS_NOT_IMPLEMENTED;
358 status = notify_lock(notify);
359 NT_STATUS_NOT_OK_RETURN(status);
361 status = notify_load(notify);
362 if (!NT_STATUS_IS_OK(status)) {
366 /* cope with /. on the end of the path */
367 len = strlen(e.path);
368 if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
369 tmp_path = talloc_strndup(notify, e.path, len-2);
370 if (tmp_path == NULL) {
371 status = NT_STATUS_NO_MEMORY;
377 depth = count_chars(e.path, '/');
379 listel = talloc_zero(notify, struct notify_list);
380 if (listel == NULL) {
381 status = NT_STATUS_NO_MEMORY;
385 listel->private_data = private_data;
386 listel->callback = callback;
387 listel->depth = depth;
388 DLIST_ADD(notify->list, listel);
390 /* ignore failures from sys_notify */
391 if (notify->sys_notify_ctx != NULL) {
393 this call will modify e.filter and e.subdir_filter
394 to remove bits handled by the backend
396 status = sys_notify_watch(notify->sys_notify_ctx, &e,
397 sys_notify_callback, listel,
398 &listel->sys_notify_handle);
399 if (NT_STATUS_IS_OK(status)) {
400 talloc_steal(listel, listel->sys_notify_handle);
404 /* if the system notify handler couldn't handle some of the
405 filter bits, or couldn't handle a request for recursion
406 then we need to install it in the array used for the
407 intra-samba notify handling */
408 if (e.filter != 0 || e.subdir_filter != 0) {
409 status = notify_add_array(notify, &e, private_data, depth);
413 notify_unlock(notify);
414 talloc_free(tmp_path);
420 remove a notify watch. Called when the directory handle is closed
422 NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
425 struct notify_list *listel;
427 struct notify_depth *d;
429 /* see if change notify is enabled at all */
430 if (notify == NULL) {
431 return NT_STATUS_NOT_IMPLEMENTED;
434 for (listel=notify->list;listel;listel=listel->next) {
435 if (listel->private_data == private_data) {
436 DLIST_REMOVE(notify->list, listel);
440 if (listel == NULL) {
441 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
444 depth = listel->depth;
448 status = notify_lock(notify);
449 NT_STATUS_NOT_OK_RETURN(status);
451 status = notify_load(notify);
452 if (!NT_STATUS_IS_OK(status)) {
453 notify_unlock(notify);
457 if (depth >= notify->array->num_depths) {
458 notify_unlock(notify);
459 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
462 /* we only have to search at the depth of this element */
463 d = ¬ify->array->depth[depth];
465 for (i=0;i<d->num_entries;i++) {
466 if (private_data == d->entries[i].private_data &&
467 cluster_id_equal(¬ify->server, &d->entries[i].server)) {
471 if (i == d->num_entries) {
472 notify_unlock(notify);
473 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
476 if (i < d->num_entries-1) {
477 memmove(&d->entries[i], &d->entries[i+1],
478 sizeof(d->entries[i])*(d->num_entries-(i+1)));
482 status = notify_save(notify);
484 notify_unlock(notify);
490 remove all notify watches for this messaging server
492 static NTSTATUS notify_remove_all(struct notify_context *notify)
495 int i, depth, del_count=0;
497 if (notify->list == NULL) {
501 status = notify_lock(notify);
502 NT_STATUS_NOT_OK_RETURN(status);
504 status = notify_load(notify);
505 if (!NT_STATUS_IS_OK(status)) {
506 notify_unlock(notify);
510 /* we have to search for all entries across all depths, looking for matches
512 for (depth=0;depth<notify->array->num_depths;depth++) {
513 struct notify_depth *d = ¬ify->array->depth[depth];
514 for (i=0;i<d->num_entries;i++) {
515 if (cluster_id_equal(¬ify->server, &d->entries[i].server)) {
516 if (i < d->num_entries-1) {
517 memmove(&d->entries[i], &d->entries[i+1],
518 sizeof(d->entries[i])*(d->num_entries-(i+1)));
528 status = notify_save(notify);
531 notify_unlock(notify);
538 send a notify message to another messaging server
540 static void notify_send(struct notify_context *notify, struct notify_entry *e,
541 const char *path, uint32_t action)
543 struct notify_event ev;
546 enum ndr_err_code ndr_err;
551 ev.private_data = e->private_data;
553 tmp_ctx = talloc_new(notify);
555 ndr_err = ndr_push_struct_blob(&data, tmp_ctx, &ev,
556 (ndr_push_flags_fn_t)ndr_push_notify_event);
557 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
558 talloc_free(tmp_ctx);
562 status = messaging_send(notify->messaging_ctx, e->server,
563 MSG_PVFS_NOTIFY, &data);
564 talloc_free(tmp_ctx);
569 trigger a notify message for anyone waiting on a matching event
571 This function is called a lot, and needs to be very fast. The unusual data structure
572 and traversal is designed to be fast in the average case, even for large numbers of
575 void notify_trigger(struct notify_context *notify,
576 uint32_t action, uint32_t filter, const char *path)
580 const char *p, *next_p;
582 /* see if change notify is enabled at all */
583 if (notify == NULL) {
587 status = notify_load(notify);
588 if (!NT_STATUS_IS_OK(status)) {
592 /* loop along the given path, working with each directory depth separately */
594 p && depth < notify->array->num_depths;
596 int p_len = p - path;
598 struct notify_depth *d = ¬ify->array->depth[depth];
599 next_p = strchr(p+1, '/');
601 /* see if there are any entries at this depth */
602 if (d->num_entries == 0) continue;
604 /* try to skip based on the maximum mask. If next_p is
605 NULL then we know it will be a 'this directory'
606 match, otherwise it must be a subdir match */
607 if (next_p != NULL) {
608 if (0 == (filter & d->max_mask_subdir)) {
612 if (0 == (filter & d->max_mask)) {
617 /* we know there is an entry here worth looking
618 for. Use a bisection search to find the first entry
619 with a matching path */
621 max_i = d->num_entries-1;
623 while (min_i < max_i) {
624 struct notify_entry *e;
628 cmp = strncmp(path, e->path, p_len);
630 if (p_len == e->path_len) {
635 } else if (cmp < 0) {
642 if (min_i != max_i) {
647 /* we now know that the entries start at min_i */
648 for (i=min_i;i<d->num_entries;i++) {
649 struct notify_entry *e = &d->entries[i];
650 if (p_len != e->path_len ||
651 strncmp(path, e->path, p_len) != 0) break;
652 if (next_p != NULL) {
653 if (0 == (filter & e->subdir_filter)) {
657 if (0 == (filter & e->filter)) {
661 notify_send(notify, e, path + e->path_len + 1, action);