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