r20972: "private" -> "private_data"
[metze/samba/wb-ndr.git] / source / ntvfs / common / notify.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2006
5    
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 2 of the License, or
9    (at your option) any later version.
10    
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.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22   this is the change notify database. It implements mechanisms for
23   storing current change notify waiters in a tdb, and checking if a
24   given event matches any of the stored notify waiiters.
25 */
26
27 #include "includes.h"
28 #include "system/filesys.h"
29 #include "lib/tdb/include/tdb.h"
30 #include "lib/util/util_tdb.h"
31 #include "messaging/messaging.h"
32 #include "db_wrap.h"
33 #include "lib/messaging/irpc.h"
34 #include "librpc/gen_ndr/ndr_notify.h"
35 #include "lib/util/dlinklist.h"
36 #include "ntvfs/common/ntvfs_common.h"
37 #include "ntvfs/sysdep/sys_notify.h"
38 #include "cluster/cluster.h"
39
40 struct notify_context {
41         struct tdb_wrap *w;
42         struct server_id server;
43         struct messaging_context *messaging_ctx;
44         struct notify_list *list;
45         struct notify_array *array;
46         int seqnum;
47         struct sys_notify_context *sys_notify_ctx;
48 };
49
50
51 struct notify_list {
52         struct notify_list *next, *prev;
53         void *private_data;
54         void (*callback)(void *, const struct notify_event *);
55         void *sys_notify_handle;
56         int depth;
57 };
58
59 #define NOTIFY_KEY "notify array"
60
61 #define NOTIFY_ENABLE           "notify:enable"
62 #define NOTIFY_ENABLE_DEFAULT   True
63
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);
67
68 /*
69   destroy the notify context
70 */
71 static int notify_destructor(struct notify_context *notify)
72 {
73         messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
74         notify_remove_all(notify);
75         return 0;
76 }
77
78 /*
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
81   via internal messages
82 */
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)
87 {
88         struct notify_context *notify;
89
90         if (share_bool_option(scfg, NOTIFY_ENABLE, NOTIFY_ENABLE_DEFAULT) != True) {
91                 return NULL;
92         }
93
94         notify = talloc(mem_ctx, struct notify_context);
95         if (notify == NULL) {
96                 return NULL;
97         }
98
99         notify->w = cluster_tdb_tmp_open(notify, "notify.tdb", TDB_SEQNUM);
100         if (notify->w == NULL) {
101                 talloc_free(notify);
102                 return NULL;
103         }
104
105         notify->server = server;
106         notify->messaging_ctx = messaging_ctx;
107         notify->list = NULL;
108         notify->array = NULL;
109         notify->seqnum = tdb_get_seqnum(notify->w->tdb);
110
111         talloc_set_destructor(notify, notify_destructor);
112
113         /* register with the messaging subsystem for the notify
114            message type */
115         messaging_register(notify->messaging_ctx, notify, 
116                            MSG_PVFS_NOTIFY, notify_handler);
117
118         notify->sys_notify_ctx = sys_notify_context_create(scfg, notify, ev);
119
120         return notify;
121 }
122
123
124 /*
125   lock the notify db
126 */
127 static NTSTATUS notify_lock(struct notify_context *notify)
128 {
129         if (tdb_lock_bystring(notify->w->tdb, NOTIFY_KEY) != 0) {
130                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
131         }
132         return NT_STATUS_OK;
133 }
134
135 /*
136   unlock the notify db
137 */
138 static void notify_unlock(struct notify_context *notify)
139 {
140         tdb_unlock_bystring(notify->w->tdb, NOTIFY_KEY);
141 }
142
143 /*
144   load the notify array
145 */
146 static NTSTATUS notify_load(struct notify_context *notify)
147 {
148         TDB_DATA dbuf;
149         DATA_BLOB blob;
150         NTSTATUS status;
151         int seqnum;
152
153         seqnum = tdb_get_seqnum(notify->w->tdb);
154
155         if (seqnum == notify->seqnum && notify->array != NULL) {
156                 return NT_STATUS_OK;
157         }
158
159         notify->seqnum = seqnum;
160
161         talloc_free(notify->array);
162         notify->array = talloc_zero(notify, struct notify_array);
163         NT_STATUS_HAVE_NO_MEMORY(notify->array);
164
165         dbuf = tdb_fetch_bystring(notify->w->tdb, NOTIFY_KEY);
166         if (dbuf.dptr == NULL) {
167                 return NT_STATUS_OK;
168         }
169
170         blob.data = dbuf.dptr;
171         blob.length = dbuf.dsize;
172
173         status = ndr_pull_struct_blob(&blob, notify->array, notify->array, 
174                                       (ndr_pull_flags_fn_t)ndr_pull_notify_array);
175         free(dbuf.dptr);
176
177         return status;
178 }
179
180 /*
181   compare notify entries for sorting
182 */
183 static int notify_compare(const void *p1, const void *p2)
184 {
185         const struct notify_entry *e1 = p1, *e2 = p2;
186         return strcmp(e1->path, e2->path);
187 }
188
189 /*
190   save the notify array
191 */
192 static NTSTATUS notify_save(struct notify_context *notify)
193 {
194         TDB_DATA dbuf;
195         DATA_BLOB blob;
196         NTSTATUS status;
197         int ret;
198         TALLOC_CTX *tmp_ctx;
199
200         /* if possible, remove some depth arrays */
201         while (notify->array->num_depths > 0 &&
202                notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
203                 notify->array->num_depths--;
204         }
205
206         /* we might just be able to delete the record */
207         if (notify->array->num_depths == 0) {
208                 ret = tdb_delete_bystring(notify->w->tdb, NOTIFY_KEY);
209                 if (ret != 0) {
210                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
211                 }
212                 return NT_STATUS_OK;
213         }
214
215         tmp_ctx = talloc_new(notify);
216
217         status = ndr_push_struct_blob(&blob, tmp_ctx, notify->array, 
218                                       (ndr_push_flags_fn_t)ndr_push_notify_array);
219         if (!NT_STATUS_IS_OK(status)) {
220                 talloc_free(tmp_ctx);
221                 return status;
222         }
223
224         dbuf.dptr = blob.data;
225         dbuf.dsize = blob.length;
226                 
227         ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
228         talloc_free(tmp_ctx);
229         if (ret != 0) {
230                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
231         }
232
233         return NT_STATUS_OK;
234 }
235
236
237 /*
238   handle incoming notify messages
239 */
240 static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
241                            uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
242 {
243         struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
244         NTSTATUS status;
245         struct notify_event ev;
246         TALLOC_CTX *tmp_ctx = talloc_new(notify);
247         struct notify_list *listel;
248
249         status = ndr_pull_struct_blob(data, tmp_ctx, &ev, 
250                                       (ndr_pull_flags_fn_t)ndr_pull_notify_event);
251         if (!NT_STATUS_IS_OK(status)) {
252                 talloc_free(tmp_ctx);
253                 return;
254         }
255
256         for (listel=notify->list;listel;listel=listel->next) {
257                 if (listel->private_data == ev.private_data) {
258                         listel->callback(listel->private_data, &ev);
259                         break;
260                 }
261         }
262
263         talloc_free(tmp_ctx);   
264 }
265
266 /*
267   callback from sys_notify telling us about changes from the OS
268 */
269 static void sys_notify_callback(struct sys_notify_context *ctx, 
270                                 void *ptr, struct notify_event *ev)
271 {
272         struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
273         ev->private_data = listel;
274         listel->callback(listel->private_data, ev);
275 }
276
277 /*
278   add an entry to the notify array
279 */
280 static NTSTATUS notify_add_array(struct notify_context *notify, struct notify_entry *e,
281                                  void *private_data, int depth)
282 {
283         int i;
284         struct notify_depth *d;
285         struct notify_entry *ee;
286
287         /* possibly expand the depths array */
288         if (depth >= notify->array->num_depths) {
289                 d = talloc_realloc(notify->array, notify->array->depth, 
290                                    struct notify_depth, depth+1);
291                 NT_STATUS_HAVE_NO_MEMORY(d);
292                 for (i=notify->array->num_depths;i<=depth;i++) {
293                         ZERO_STRUCT(d[i]);
294                 }
295                 notify->array->depth = d;
296                 notify->array->num_depths = depth+1;
297         }
298         d = &notify->array->depth[depth];
299
300         /* expand the entries array */
301         ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
302                             d->num_entries+1);
303         NT_STATUS_HAVE_NO_MEMORY(ee);
304         d->entries = ee;
305
306         d->entries[d->num_entries] = *e;
307         d->entries[d->num_entries].private_data = private_data;
308         d->entries[d->num_entries].server = notify->server;
309         d->entries[d->num_entries].path_len = strlen(e->path);
310         d->num_entries++;
311
312         d->max_mask |= e->filter;
313         d->max_mask_subdir |= e->subdir_filter;
314
315         if (d->num_entries > 1) {
316                 qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
317         }
318
319         /* recalculate the maximum masks */
320         d->max_mask = 0;
321         d->max_mask_subdir = 0;
322
323         for (i=0;i<d->num_entries;i++) {
324                 d->max_mask |= d->entries[i].filter;
325                 d->max_mask_subdir |= d->entries[i].subdir_filter;
326         }
327
328         return notify_save(notify);
329 }
330
331 /*
332   add a notify watch. This is called when a notify is first setup on a open
333   directory handle.
334 */
335 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
336                     void (*callback)(void *, const struct notify_event *), 
337                     void *private_data)
338 {
339         struct notify_entry e = *e0;
340         NTSTATUS status;
341         char *tmp_path = NULL;
342         struct notify_list *listel;
343         size_t len;
344         int depth;
345
346         /* see if change notify is enabled at all */
347         if (notify == NULL) {
348                 return NT_STATUS_NOT_IMPLEMENTED;
349         }
350
351         status = notify_lock(notify);
352         NT_STATUS_NOT_OK_RETURN(status);
353
354         status = notify_load(notify);
355         if (!NT_STATUS_IS_OK(status)) {
356                 goto done;
357         }
358
359         /* cope with /. on the end of the path */
360         len = strlen(e.path);
361         if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
362                 tmp_path = talloc_strndup(notify, e.path, len-2);
363                 if (tmp_path == NULL) {
364                         status = NT_STATUS_NO_MEMORY;
365                         goto done;
366                 }
367                 e.path = tmp_path;
368         }
369
370         depth = count_chars(e.path, '/');
371
372         listel = talloc_zero(notify, struct notify_list);
373         if (listel == NULL) {
374                 status = NT_STATUS_NO_MEMORY;
375                 goto done;
376         }
377
378         listel->private_data = private_data;
379         listel->callback = callback;
380         listel->depth = depth;
381         DLIST_ADD(notify->list, listel);
382
383         /* ignore failures from sys_notify */
384         if (notify->sys_notify_ctx != NULL) {
385                 /*
386                   this call will modify e.filter and e.subdir_filter
387                   to remove bits handled by the backend
388                 */
389                 status = sys_notify_watch(notify->sys_notify_ctx, &e,
390                                           sys_notify_callback, listel, 
391                                           &listel->sys_notify_handle);
392                 if (NT_STATUS_IS_OK(status)) {
393                         talloc_steal(listel, listel->sys_notify_handle);
394                 }
395         }
396
397         /* if the system notify handler couldn't handle some of the
398            filter bits, or couldn't handle a request for recursion
399            then we need to install it in the array used for the
400            intra-samba notify handling */
401         if (e.filter != 0 || e.subdir_filter != 0) {
402                 status = notify_add_array(notify, &e, private_data, depth);
403         }
404
405 done:
406         notify_unlock(notify);
407         talloc_free(tmp_path);
408
409         return status;
410 }
411
412 /*
413   remove a notify watch. Called when the directory handle is closed
414 */
415 NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
416 {
417         NTSTATUS status;
418         struct notify_list *listel;
419         int i, depth;
420         struct notify_depth *d;
421
422         /* see if change notify is enabled at all */
423         if (notify == NULL) {
424                 return NT_STATUS_NOT_IMPLEMENTED;
425         }
426
427         for (listel=notify->list;listel;listel=listel->next) {
428                 if (listel->private_data == private_data) {
429                         DLIST_REMOVE(notify->list, listel);
430                         break;
431                 }
432         }
433         if (listel == NULL) {
434                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
435         }
436
437         depth = listel->depth;
438
439         talloc_free(listel);
440
441         status = notify_lock(notify);
442         NT_STATUS_NOT_OK_RETURN(status);
443
444         status = notify_load(notify);
445         if (!NT_STATUS_IS_OK(status)) {
446                 notify_unlock(notify);
447                 return status;
448         }
449
450         if (depth >= notify->array->num_depths) {
451                 notify_unlock(notify);
452                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
453         }
454
455         /* we only have to search at the depth of this element */
456         d = &notify->array->depth[depth];
457
458         for (i=0;i<d->num_entries;i++) {
459                 if (private_data == d->entries[i].private_data &&
460                     cluster_id_equal(&notify->server, &d->entries[i].server)) {
461                         break;
462                 }
463         }
464         if (i == d->num_entries) {
465                 notify_unlock(notify);
466                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
467         }
468
469         if (i < d->num_entries-1) {
470                 memmove(&d->entries[i], &d->entries[i+1], 
471                         sizeof(d->entries[i])*(d->num_entries-(i+1)));
472         }
473         d->num_entries--;
474
475         status = notify_save(notify);
476
477         notify_unlock(notify);
478
479         return status;
480 }
481
482 /*
483   remove all notify watches for this messaging server
484 */
485 static NTSTATUS notify_remove_all(struct notify_context *notify)
486 {
487         NTSTATUS status;
488         int i, depth, del_count=0;
489
490         if (notify->list == NULL) {
491                 return NT_STATUS_OK;
492         }
493
494         status = notify_lock(notify);
495         NT_STATUS_NOT_OK_RETURN(status);
496
497         status = notify_load(notify);
498         if (!NT_STATUS_IS_OK(status)) {
499                 notify_unlock(notify);
500                 return status;
501         }
502
503         /* we have to search for all entries across all depths, looking for matches
504            for our server id */
505         for (depth=0;depth<notify->array->num_depths;depth++) {
506                 struct notify_depth *d = &notify->array->depth[depth];
507                 for (i=0;i<d->num_entries;i++) {
508                         if (cluster_id_equal(&notify->server, &d->entries[i].server)) {
509                                 if (i < d->num_entries-1) {
510                                         memmove(&d->entries[i], &d->entries[i+1], 
511                                                 sizeof(d->entries[i])*(d->num_entries-(i+1)));
512                                 }
513                                 i--;
514                                 d->num_entries--;
515                                 del_count++;
516                         }
517                 }
518         }
519
520         if (del_count > 0) {
521                 status = notify_save(notify);
522         }
523
524         notify_unlock(notify);
525
526         return status;
527 }
528
529
530 /*
531   send a notify message to another messaging server
532 */
533 static void notify_send(struct notify_context *notify, struct notify_entry *e,
534                         const char *path, uint32_t action)
535 {
536         struct notify_event ev;
537         DATA_BLOB data;
538         NTSTATUS status;
539         TALLOC_CTX *tmp_ctx;
540
541         ev.action = action;
542         ev.path = path;
543         ev.private_data = e->private_data;
544
545         tmp_ctx = talloc_new(notify);
546
547         status = ndr_push_struct_blob(&data, tmp_ctx, &ev, 
548                                       (ndr_push_flags_fn_t)ndr_push_notify_event);
549         if (!NT_STATUS_IS_OK(status)) {
550                 talloc_free(tmp_ctx);
551                 return;
552         }
553
554         status = messaging_send(notify->messaging_ctx, e->server, 
555                                 MSG_PVFS_NOTIFY, &data);
556         talloc_free(tmp_ctx);
557 }
558
559
560 /*
561   trigger a notify message for anyone waiting on a matching event
562
563   This function is called a lot, and needs to be very fast. The unusual data structure
564   and traversal is designed to be fast in the average case, even for large numbers of
565   notifies
566 */
567 void notify_trigger(struct notify_context *notify,
568                     uint32_t action, uint32_t filter, const char *path)
569 {
570         NTSTATUS status;
571         int depth;
572         const char *p, *next_p;
573
574         /* see if change notify is enabled at all */
575         if (notify == NULL) {
576                 return;
577         }
578
579         status = notify_load(notify);
580         if (!NT_STATUS_IS_OK(status)) {
581                 return;
582         }
583
584         /* loop along the given path, working with each directory depth separately */
585         for (depth=0,p=path;
586              p && depth < notify->array->num_depths;
587              p=next_p,depth++) {
588                 int p_len = p - path;
589                 int min_i, max_i, i;
590                 struct notify_depth *d = &notify->array->depth[depth];
591                 next_p = strchr(p+1, '/');
592
593                 /* see if there are any entries at this depth */
594                 if (d->num_entries == 0) continue;
595                 
596                 /* try to skip based on the maximum mask. If next_p is
597                  NULL then we know it will be a 'this directory'
598                  match, otherwise it must be a subdir match */
599                 if (next_p != NULL) {
600                         if (0 == (filter & d->max_mask_subdir)) {
601                                 continue;
602                         }
603                 } else {
604                         if (0 == (filter & d->max_mask)) {
605                                 continue;
606                         }
607                 }
608
609                 /* we know there is an entry here worth looking
610                  for. Use a bisection search to find the first entry
611                  with a matching path */
612                 min_i = 0;
613                 max_i = d->num_entries-1;
614
615                 while (min_i < max_i) {
616                         struct notify_entry *e;
617                         int cmp;
618                         i = (min_i+max_i)/2;
619                         e = &d->entries[i];
620                         cmp = strncmp(path, e->path, p_len);
621                         if (cmp == 0) {
622                                 if (p_len == e->path_len) {
623                                         max_i = i;
624                                 } else {
625                                         max_i = i-1;
626                                 }
627                         } else if (cmp < 0) {
628                                 max_i = i-1;
629                         } else {
630                                 min_i = i+1;
631                         }
632                 }
633
634                 if (min_i != max_i) {
635                         /* none match */
636                         continue;
637                 }
638
639                 /* we now know that the entries start at min_i */
640                 for (i=min_i;i<d->num_entries;i++) {
641                         struct notify_entry *e = &d->entries[i];
642                         if (p_len != e->path_len ||
643                             strncmp(path, e->path, p_len) != 0) break;
644                         if (next_p != NULL) {
645                                 if (0 == (filter & e->subdir_filter)) {
646                                         continue;
647                                 }
648                         } else {
649                                 if (0 == (filter & e->filter)) {
650                                         continue;
651                                 }
652                         }
653                         notify_send(notify, e, path + e->path_len + 1, action);
654                 }
655         }
656 }