r25920: ndr: change NTSTAUS into enum ndr_err_code (samba4 callers)
[kai/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         enum ndr_err_code ndr_err;
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         ndr_err = ndr_pull_struct_blob(&blob, notify->array, notify->array,
173                                        (ndr_pull_flags_fn_t)ndr_pull_notify_array);
174         free(dbuf.dptr);
175         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
176                 return ndr_map_error2ntstatus(ndr_err);
177         }
178
179         return NT_STATUS_OK;
180 }
181
182 /*
183   compare notify entries for sorting
184 */
185 static int notify_compare(const void *p1, const void *p2)
186 {
187         const struct notify_entry *e1 = p1, *e2 = p2;
188         return strcmp(e1->path, e2->path);
189 }
190
191 /*
192   save the notify array
193 */
194 static NTSTATUS notify_save(struct notify_context *notify)
195 {
196         TDB_DATA dbuf;
197         DATA_BLOB blob;
198         enum ndr_err_code ndr_err;
199         int ret;
200         TALLOC_CTX *tmp_ctx;
201
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--;
206         }
207
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);
211                 if (ret != 0) {
212                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
213                 }
214                 return NT_STATUS_OK;
215         }
216
217         tmp_ctx = talloc_new(notify);
218         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
219
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);
225         }
226
227         dbuf.dptr = blob.data;
228         dbuf.dsize = blob.length;
229                 
230         ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
231         talloc_free(tmp_ctx);
232         if (ret != 0) {
233                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
234         }
235
236         return NT_STATUS_OK;
237 }
238
239
240 /*
241   handle incoming notify messages
242 */
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)
245 {
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;
251
252         if (tmp_ctx == NULL) {
253                 return;
254         }
255
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);
260                 return;
261         }
262
263         for (listel=notify->list;listel;listel=listel->next) {
264                 if (listel->private_data == ev.private_data) {
265                         listel->callback(listel->private_data, &ev);
266                         break;
267                 }
268         }
269
270         talloc_free(tmp_ctx);   
271 }
272
273 /*
274   callback from sys_notify telling us about changes from the OS
275 */
276 static void sys_notify_callback(struct sys_notify_context *ctx, 
277                                 void *ptr, struct notify_event *ev)
278 {
279         struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
280         ev->private_data = listel;
281         listel->callback(listel->private_data, ev);
282 }
283
284 /*
285   add an entry to the notify array
286 */
287 static NTSTATUS notify_add_array(struct notify_context *notify, struct notify_entry *e,
288                                  void *private_data, int depth)
289 {
290         int i;
291         struct notify_depth *d;
292         struct notify_entry *ee;
293
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++) {
300                         ZERO_STRUCT(d[i]);
301                 }
302                 notify->array->depth = d;
303                 notify->array->num_depths = depth+1;
304         }
305         d = &notify->array->depth[depth];
306
307         /* expand the entries array */
308         ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
309                             d->num_entries+1);
310         NT_STATUS_HAVE_NO_MEMORY(ee);
311         d->entries = ee;
312
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);
317         d->num_entries++;
318
319         d->max_mask |= e->filter;
320         d->max_mask_subdir |= e->subdir_filter;
321
322         if (d->num_entries > 1) {
323                 qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
324         }
325
326         /* recalculate the maximum masks */
327         d->max_mask = 0;
328         d->max_mask_subdir = 0;
329
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;
333         }
334
335         return notify_save(notify);
336 }
337
338 /*
339   add a notify watch. This is called when a notify is first setup on a open
340   directory handle.
341 */
342 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
343                     void (*callback)(void *, const struct notify_event *), 
344                     void *private_data)
345 {
346         struct notify_entry e = *e0;
347         NTSTATUS status;
348         char *tmp_path = NULL;
349         struct notify_list *listel;
350         size_t len;
351         int depth;
352
353         /* see if change notify is enabled at all */
354         if (notify == NULL) {
355                 return NT_STATUS_NOT_IMPLEMENTED;
356         }
357
358         status = notify_lock(notify);
359         NT_STATUS_NOT_OK_RETURN(status);
360
361         status = notify_load(notify);
362         if (!NT_STATUS_IS_OK(status)) {
363                 goto done;
364         }
365
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;
372                         goto done;
373                 }
374                 e.path = tmp_path;
375         }
376
377         depth = count_chars(e.path, '/');
378
379         listel = talloc_zero(notify, struct notify_list);
380         if (listel == NULL) {
381                 status = NT_STATUS_NO_MEMORY;
382                 goto done;
383         }
384
385         listel->private_data = private_data;
386         listel->callback = callback;
387         listel->depth = depth;
388         DLIST_ADD(notify->list, listel);
389
390         /* ignore failures from sys_notify */
391         if (notify->sys_notify_ctx != NULL) {
392                 /*
393                   this call will modify e.filter and e.subdir_filter
394                   to remove bits handled by the backend
395                 */
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);
401                 }
402         }
403
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);
410         }
411
412 done:
413         notify_unlock(notify);
414         talloc_free(tmp_path);
415
416         return status;
417 }
418
419 /*
420   remove a notify watch. Called when the directory handle is closed
421 */
422 NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
423 {
424         NTSTATUS status;
425         struct notify_list *listel;
426         int i, depth;
427         struct notify_depth *d;
428
429         /* see if change notify is enabled at all */
430         if (notify == NULL) {
431                 return NT_STATUS_NOT_IMPLEMENTED;
432         }
433
434         for (listel=notify->list;listel;listel=listel->next) {
435                 if (listel->private_data == private_data) {
436                         DLIST_REMOVE(notify->list, listel);
437                         break;
438                 }
439         }
440         if (listel == NULL) {
441                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
442         }
443
444         depth = listel->depth;
445
446         talloc_free(listel);
447
448         status = notify_lock(notify);
449         NT_STATUS_NOT_OK_RETURN(status);
450
451         status = notify_load(notify);
452         if (!NT_STATUS_IS_OK(status)) {
453                 notify_unlock(notify);
454                 return status;
455         }
456
457         if (depth >= notify->array->num_depths) {
458                 notify_unlock(notify);
459                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
460         }
461
462         /* we only have to search at the depth of this element */
463         d = &notify->array->depth[depth];
464
465         for (i=0;i<d->num_entries;i++) {
466                 if (private_data == d->entries[i].private_data &&
467                     cluster_id_equal(&notify->server, &d->entries[i].server)) {
468                         break;
469                 }
470         }
471         if (i == d->num_entries) {
472                 notify_unlock(notify);
473                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
474         }
475
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)));
479         }
480         d->num_entries--;
481
482         status = notify_save(notify);
483
484         notify_unlock(notify);
485
486         return status;
487 }
488
489 /*
490   remove all notify watches for this messaging server
491 */
492 static NTSTATUS notify_remove_all(struct notify_context *notify)
493 {
494         NTSTATUS status;
495         int i, depth, del_count=0;
496
497         if (notify->list == NULL) {
498                 return NT_STATUS_OK;
499         }
500
501         status = notify_lock(notify);
502         NT_STATUS_NOT_OK_RETURN(status);
503
504         status = notify_load(notify);
505         if (!NT_STATUS_IS_OK(status)) {
506                 notify_unlock(notify);
507                 return status;
508         }
509
510         /* we have to search for all entries across all depths, looking for matches
511            for our server id */
512         for (depth=0;depth<notify->array->num_depths;depth++) {
513                 struct notify_depth *d = &notify->array->depth[depth];
514                 for (i=0;i<d->num_entries;i++) {
515                         if (cluster_id_equal(&notify->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)));
519                                 }
520                                 i--;
521                                 d->num_entries--;
522                                 del_count++;
523                         }
524                 }
525         }
526
527         if (del_count > 0) {
528                 status = notify_save(notify);
529         }
530
531         notify_unlock(notify);
532
533         return status;
534 }
535
536
537 /*
538   send a notify message to another messaging server
539 */
540 static void notify_send(struct notify_context *notify, struct notify_entry *e,
541                         const char *path, uint32_t action)
542 {
543         struct notify_event ev;
544         DATA_BLOB data;
545         NTSTATUS status;
546         enum ndr_err_code ndr_err;
547         TALLOC_CTX *tmp_ctx;
548
549         ev.action = action;
550         ev.path = path;
551         ev.private_data = e->private_data;
552
553         tmp_ctx = talloc_new(notify);
554
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);
559                 return;
560         }
561
562         status = messaging_send(notify->messaging_ctx, e->server, 
563                                 MSG_PVFS_NOTIFY, &data);
564         talloc_free(tmp_ctx);
565 }
566
567
568 /*
569   trigger a notify message for anyone waiting on a matching event
570
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
573   notifies
574 */
575 void notify_trigger(struct notify_context *notify,
576                     uint32_t action, uint32_t filter, const char *path)
577 {
578         NTSTATUS status;
579         int depth;
580         const char *p, *next_p;
581
582         /* see if change notify is enabled at all */
583         if (notify == NULL) {
584                 return;
585         }
586
587         status = notify_load(notify);
588         if (!NT_STATUS_IS_OK(status)) {
589                 return;
590         }
591
592         /* loop along the given path, working with each directory depth separately */
593         for (depth=0,p=path;
594              p && depth < notify->array->num_depths;
595              p=next_p,depth++) {
596                 int p_len = p - path;
597                 int min_i, max_i, i;
598                 struct notify_depth *d = &notify->array->depth[depth];
599                 next_p = strchr(p+1, '/');
600
601                 /* see if there are any entries at this depth */
602                 if (d->num_entries == 0) continue;
603                 
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)) {
609                                 continue;
610                         }
611                 } else {
612                         if (0 == (filter & d->max_mask)) {
613                                 continue;
614                         }
615                 }
616
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 */
620                 min_i = 0;
621                 max_i = d->num_entries-1;
622
623                 while (min_i < max_i) {
624                         struct notify_entry *e;
625                         int cmp;
626                         i = (min_i+max_i)/2;
627                         e = &d->entries[i];
628                         cmp = strncmp(path, e->path, p_len);
629                         if (cmp == 0) {
630                                 if (p_len == e->path_len) {
631                                         max_i = i;
632                                 } else {
633                                         max_i = i-1;
634                                 }
635                         } else if (cmp < 0) {
636                                 max_i = i-1;
637                         } else {
638                                 min_i = i+1;
639                         }
640                 }
641
642                 if (min_i != max_i) {
643                         /* none match */
644                         continue;
645                 }
646
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)) {
654                                         continue;
655                                 }
656                         } else {
657                                 if (0 == (filter & e->filter)) {
658                                         continue;
659                                 }
660                         }
661                         notify_send(notify, e, path + e->path_len + 1, action);
662                 }
663         }
664 }