s3: use TDB_INCOMPATIBLE_HASH (the jenkins hash) on all TDB_CLEAR_IF_FIRST tdb's.
[obnox/samba-ctdb.git] / source3 / smbd / notify_internal.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 "librpc/gen_ndr/ndr_notify.h"
28
29 struct notify_context {
30         struct db_context *db_recursive;
31         struct db_context *db_onelevel;
32         struct server_id server;
33         struct messaging_context *messaging_ctx;
34         struct notify_list *list;
35         struct notify_array *array;
36         int seqnum;
37         struct sys_notify_context *sys_notify_ctx;
38         TDB_DATA key;
39 };
40
41
42 struct notify_list {
43         struct notify_list *next, *prev;
44         void *private_data;
45         void (*callback)(void *, const struct notify_event *);
46         void *sys_notify_handle;
47         int depth;
48 };
49
50 #define NOTIFY_KEY "notify array"
51
52 #define NOTIFY_ENABLE           "notify:enable"
53 #define NOTIFY_ENABLE_DEFAULT   True
54
55 static NTSTATUS notify_remove_all(struct notify_context *notify,
56                                   const struct server_id *server);
57 static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
58                            uint32_t msg_type, struct server_id server_id, DATA_BLOB *data);
59
60 /*
61   destroy the notify context
62 */
63 static int notify_destructor(struct notify_context *notify)
64 {
65         messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
66
67         if (notify->list != NULL) {
68                 notify_remove_all(notify, &notify->server);
69         }
70
71         return 0;
72 }
73
74 /*
75   Open up the notify.tdb database. You should close it down using
76   talloc_free(). We need the messaging_ctx to allow for notifications
77   via internal messages
78 */
79 struct notify_context *notify_init(TALLOC_CTX *mem_ctx, struct server_id server, 
80                                    struct messaging_context *messaging_ctx,
81                                    struct event_context *ev,
82                                    connection_struct *conn)
83 {
84         struct notify_context *notify;
85
86         if (!lp_change_notify(conn->params)) {
87                 return NULL;
88         }
89
90         notify = talloc(mem_ctx, struct notify_context);
91         if (notify == NULL) {
92                 return NULL;
93         }
94
95         notify->db_recursive = db_open(notify, lock_path("notify.tdb"),
96                                        0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
97                                        O_RDWR|O_CREAT, 0644);
98         if (notify->db_recursive == NULL) {
99                 talloc_free(notify);
100                 return NULL;
101         }
102
103         notify->db_onelevel = db_open(notify, lock_path("notify_onelevel.tdb"),
104                                       0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
105                                       O_RDWR|O_CREAT, 0644);
106         if (notify->db_onelevel == NULL) {
107                 talloc_free(notify);
108                 return NULL;
109         }
110
111         notify->server = server;
112         notify->messaging_ctx = messaging_ctx;
113         notify->list = NULL;
114         notify->array = NULL;
115         notify->seqnum = notify->db_recursive->get_seqnum(
116                 notify->db_recursive);
117         notify->key = string_term_tdb_data(NOTIFY_KEY);
118
119         talloc_set_destructor(notify, notify_destructor);
120
121         /* register with the messaging subsystem for the notify
122            message type */
123         messaging_register(notify->messaging_ctx, notify, 
124                            MSG_PVFS_NOTIFY, notify_handler);
125
126         notify->sys_notify_ctx = sys_notify_context_create(conn, notify, ev);
127
128         return notify;
129 }
130
131 bool notify_internal_parent_init(void)
132 {
133         struct tdb_wrap *db1, *db2;
134
135         if (lp_clustering()) {
136                 return true;
137         }
138
139         /*
140          * Open the tdbs in the parent process (smbd) so that our
141          * CLEAR_IF_FIRST optimization in tdb_reopen_all can properly
142          * work.
143          */
144
145         db1 = tdb_wrap_open(talloc_autofree_context(), lock_path("notify.tdb"),
146                             0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
147                            O_RDWR|O_CREAT, 0644);
148         if (db1 == NULL) {
149                 DEBUG(1, ("could not open notify.tdb: %s\n", strerror(errno)));
150                 return false;
151         }
152         db2 = tdb_wrap_open(talloc_autofree_context(),
153                             lock_path("notify_onelevel.tdb"),
154                             0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDWR|O_CREAT, 0644);
155         if (db2 == NULL) {
156                 DEBUG(1, ("could not open notify_onelevel.tdb: %s\n",
157                           strerror(errno)));
158                 TALLOC_FREE(db1);
159                 return false;
160         }
161         return true;
162 }
163
164 /*
165   lock and fetch the record
166 */
167 static NTSTATUS notify_fetch_locked(struct notify_context *notify, struct db_record **rec)
168 {
169         *rec = notify->db_recursive->fetch_locked(notify->db_recursive,
170                                                   notify, notify->key);
171         if (*rec == NULL) {
172                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
173         }
174         return NT_STATUS_OK;
175 }
176
177 /*
178   load the notify array
179 */
180 static NTSTATUS notify_load(struct notify_context *notify, struct db_record *rec)
181 {
182         TDB_DATA dbuf;
183         DATA_BLOB blob;
184         NTSTATUS status;
185         int seqnum;
186
187         seqnum = notify->db_recursive->get_seqnum(notify->db_recursive);
188
189         if (seqnum == notify->seqnum && notify->array != NULL) {
190                 return NT_STATUS_OK;
191         }
192
193         notify->seqnum = seqnum;
194
195         talloc_free(notify->array);
196         notify->array = TALLOC_ZERO_P(notify, struct notify_array);
197         NT_STATUS_HAVE_NO_MEMORY(notify->array);
198
199         if (!rec) {
200                 if (notify->db_recursive->fetch(notify->db_recursive, notify,
201                                                 notify->key, &dbuf) != 0) {
202                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
203                 }
204         } else {
205                 dbuf = rec->value;
206         }
207
208         blob.data = (uint8 *)dbuf.dptr;
209         blob.length = dbuf.dsize;
210
211         status = NT_STATUS_OK;
212         if (blob.length > 0) {
213                 enum ndr_err_code ndr_err;
214                 ndr_err = ndr_pull_struct_blob(&blob, notify->array, NULL, notify->array,
215                                                (ndr_pull_flags_fn_t)ndr_pull_notify_array);
216                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
217                         /* 1. log that we got a corrupt notify_array
218                          * 2. clear the variable the garbage was stored into to not trip
219                          *  over it next time this method is entered with the same seqnum
220                          * 3. delete it from the database */
221                         DEBUG(2, ("notify_array is corrupt, discarding it\n"));
222
223                         ZERO_STRUCTP(notify->array);
224                         if (rec != NULL) {
225                                 rec->delete_rec(rec);
226                         }
227
228                 } else {
229                         if (DEBUGLEVEL >= 10) {
230                                 DEBUG(10, ("notify_load:\n"));
231                                 NDR_PRINT_DEBUG(notify_array, notify->array);
232                         }
233                 }
234         }
235
236
237         if (!rec) {
238                 talloc_free(dbuf.dptr);
239         }
240
241         return status;
242 }
243
244 /*
245   compare notify entries for sorting
246 */
247 static int notify_compare(const void *p1, const void *p2)
248 {
249         const struct notify_entry *e1 = (const struct notify_entry *)p1;
250         const struct notify_entry *e2 = (const struct notify_entry *)p2;
251         return strcmp(e1->path, e2->path);
252 }
253
254 /*
255   save the notify array
256 */
257 static NTSTATUS notify_save(struct notify_context *notify, struct db_record *rec)
258 {
259         TDB_DATA dbuf;
260         DATA_BLOB blob;
261         NTSTATUS status;
262         enum ndr_err_code ndr_err;
263         TALLOC_CTX *tmp_ctx;
264
265         /* if possible, remove some depth arrays */
266         while (notify->array->num_depths > 0 &&
267                notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
268                 notify->array->num_depths--;
269         }
270
271         /* we might just be able to delete the record */
272         if (notify->array->num_depths == 0) {
273                 return rec->delete_rec(rec);
274         }
275
276         tmp_ctx = talloc_new(notify);
277         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
278
279         ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, NULL, notify->array,
280                                       (ndr_push_flags_fn_t)ndr_push_notify_array);
281         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
282                 talloc_free(tmp_ctx);
283                 return ndr_map_error2ntstatus(ndr_err);
284         }
285
286         if (DEBUGLEVEL >= 10) {
287                 DEBUG(10, ("notify_save:\n"));
288                 NDR_PRINT_DEBUG(notify_array, notify->array);
289         }
290
291         dbuf.dptr = blob.data;
292         dbuf.dsize = blob.length;
293
294         status = rec->store(rec, dbuf, TDB_REPLACE);
295         talloc_free(tmp_ctx);
296
297         return status;
298 }
299
300
301 /*
302   handle incoming notify messages
303 */
304 static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
305                            uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
306 {
307         struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
308         enum ndr_err_code ndr_err;
309         struct notify_event ev;
310         TALLOC_CTX *tmp_ctx = talloc_new(notify);
311         struct notify_list *listel;
312
313         if (tmp_ctx == NULL) {
314                 return;
315         }
316
317         ndr_err = ndr_pull_struct_blob(data, tmp_ctx, NULL, &ev,
318                                        (ndr_pull_flags_fn_t)ndr_pull_notify_event);
319         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
320                 talloc_free(tmp_ctx);
321                 return;
322         }
323
324         for (listel=notify->list;listel;listel=listel->next) {
325                 if (listel->private_data == ev.private_data) {
326                         listel->callback(listel->private_data, &ev);
327                         break;
328                 }
329         }
330
331         talloc_free(tmp_ctx);   
332 }
333
334 /*
335   callback from sys_notify telling us about changes from the OS
336 */
337 static void sys_notify_callback(struct sys_notify_context *ctx, 
338                                 void *ptr, struct notify_event *ev)
339 {
340         struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
341         ev->private_data = listel;
342         DEBUG(10, ("sys_notify_callback called with action=%d, for %s\n",
343                    ev->action, ev->path));
344         listel->callback(listel->private_data, ev);
345 }
346
347 /*
348   add an entry to the notify array
349 */
350 static NTSTATUS notify_add_array(struct notify_context *notify, struct db_record *rec,
351                                  struct notify_entry *e,
352                                  void *private_data, int depth)
353 {
354         int i;
355         struct notify_depth *d;
356         struct notify_entry *ee;
357
358         /* possibly expand the depths array */
359         if (depth >= notify->array->num_depths) {
360                 d = talloc_realloc(notify->array, notify->array->depth, 
361                                    struct notify_depth, depth+1);
362                 NT_STATUS_HAVE_NO_MEMORY(d);
363                 for (i=notify->array->num_depths;i<=depth;i++) {
364                         ZERO_STRUCT(d[i]);
365                 }
366                 notify->array->depth = d;
367                 notify->array->num_depths = depth+1;
368         }
369         d = &notify->array->depth[depth];
370
371         /* expand the entries array */
372         ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
373                             d->num_entries+1);
374         NT_STATUS_HAVE_NO_MEMORY(ee);
375         d->entries = ee;
376
377         d->entries[d->num_entries] = *e;
378         d->entries[d->num_entries].private_data = private_data;
379         d->entries[d->num_entries].server = notify->server;
380         d->entries[d->num_entries].path_len = strlen(e->path);
381         d->num_entries++;
382
383         d->max_mask |= e->filter;
384         d->max_mask_subdir |= e->subdir_filter;
385
386         if (d->num_entries > 1) {
387                 qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
388         }
389
390         /* recalculate the maximum masks */
391         d->max_mask = 0;
392         d->max_mask_subdir = 0;
393
394         for (i=0;i<d->num_entries;i++) {
395                 d->max_mask |= d->entries[i].filter;
396                 d->max_mask_subdir |= d->entries[i].subdir_filter;
397         }
398
399         return notify_save(notify, rec);
400 }
401
402 /*
403   Add a non-recursive watch
404 */
405
406 static void notify_add_onelevel(struct notify_context *notify,
407                                 struct notify_entry *e, void *private_data)
408 {
409         struct notify_entry_array *array;
410         struct db_record *rec;
411         DATA_BLOB blob;
412         TDB_DATA dbuf;
413         enum ndr_err_code ndr_err;
414         NTSTATUS status;
415
416         array = talloc_zero(talloc_tos(), struct notify_entry_array);
417         if (array == NULL) {
418                 return;
419         }
420
421         rec = notify->db_onelevel->fetch_locked(
422                 notify->db_onelevel, talloc_tos(),
423                 make_tdb_data((uint8_t *)&e->dir_id, sizeof(e->dir_id)));
424         if (rec == NULL) {
425                 DEBUG(10, ("notify_add_onelevel: fetch_locked for %s failed"
426                            "\n", file_id_string_tos(&e->dir_id)));
427                 TALLOC_FREE(array);
428                 return;
429         }
430
431         blob.data = (uint8_t *)rec->value.dptr;
432         blob.length = rec->value.dsize;
433
434         if (blob.length > 0) {
435                 ndr_err = ndr_pull_struct_blob(
436                         &blob, array, NULL, array,
437                         (ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
438                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
439                         DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
440                                    ndr_errstr(ndr_err)));
441                         TALLOC_FREE(array);
442                         return;
443                 }
444                 if (DEBUGLEVEL >= 10) {
445                         DEBUG(10, ("notify_add_onelevel:\n"));
446                         NDR_PRINT_DEBUG(notify_entry_array, array);
447                 }
448         }
449
450         array->entries = talloc_realloc(array, array->entries,
451                                         struct notify_entry,
452                                         array->num_entries+1);
453         if (array->entries == NULL) {
454                 TALLOC_FREE(array);
455                 return;
456         }
457         array->entries[array->num_entries] = *e;
458         array->entries[array->num_entries].private_data = private_data;
459         array->entries[array->num_entries].server = notify->server;
460         array->num_entries += 1;
461
462         ndr_err = ndr_push_struct_blob(
463                 &blob, rec, NULL, array,
464                 (ndr_push_flags_fn_t)ndr_push_notify_entry_array);
465         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
466                 DEBUG(10, ("ndr_push_notify_entry_array failed: %s\n",
467                            ndr_errstr(ndr_err)));
468                 TALLOC_FREE(array);
469                 return;
470         }
471
472         if (DEBUGLEVEL >= 10) {
473                 DEBUG(10, ("notify_add_onelevel:\n"));
474                 NDR_PRINT_DEBUG(notify_entry_array, array);
475         }
476
477         dbuf.dptr = blob.data;
478         dbuf.dsize = blob.length;
479
480         status = rec->store(rec, dbuf, TDB_REPLACE);
481         TALLOC_FREE(array);
482         if (!NT_STATUS_IS_OK(status)) {
483                 DEBUG(10, ("notify_add_onelevel: store failed: %s\n",
484                            nt_errstr(status)));
485                 return;
486         }
487         e->filter = 0;
488         return;
489 }
490
491
492 /*
493   add a notify watch. This is called when a notify is first setup on a open
494   directory handle.
495 */
496 NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
497                     void (*callback)(void *, const struct notify_event *), 
498                     void *private_data)
499 {
500         struct notify_entry e = *e0;
501         NTSTATUS status;
502         char *tmp_path = NULL;
503         struct notify_list *listel;
504         size_t len;
505         int depth;
506         struct db_record *rec;
507
508         /* see if change notify is enabled at all */
509         if (notify == NULL) {
510                 return NT_STATUS_NOT_IMPLEMENTED;
511         }
512
513         status = notify_fetch_locked(notify, &rec);
514         NT_STATUS_NOT_OK_RETURN(status);
515
516         status = notify_load(notify, rec);
517         if (!NT_STATUS_IS_OK(status)) {
518                 talloc_free(rec);
519                 return status;
520         }
521
522         /* cope with /. on the end of the path */
523         len = strlen(e.path);
524         if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
525                 tmp_path = talloc_strndup(notify, e.path, len-2);
526                 if (tmp_path == NULL) {
527                         status = NT_STATUS_NO_MEMORY;
528                         goto done;
529                 }
530                 e.path = tmp_path;
531         }
532
533         depth = count_chars(e.path, '/');
534
535         listel = TALLOC_ZERO_P(notify, struct notify_list);
536         if (listel == NULL) {
537                 status = NT_STATUS_NO_MEMORY;
538                 goto done;
539         }
540
541         listel->private_data = private_data;
542         listel->callback = callback;
543         listel->depth = depth;
544         DLIST_ADD(notify->list, listel);
545
546         /* ignore failures from sys_notify */
547         if (notify->sys_notify_ctx != NULL) {
548                 /*
549                   this call will modify e.filter and e.subdir_filter
550                   to remove bits handled by the backend
551                 */
552                 status = sys_notify_watch(notify->sys_notify_ctx, &e,
553                                           sys_notify_callback, listel, 
554                                           &listel->sys_notify_handle);
555                 if (NT_STATUS_IS_OK(status)) {
556                         talloc_steal(listel, listel->sys_notify_handle);
557                 }
558         }
559
560         if (e.filter != 0) {
561                 notify_add_onelevel(notify, &e, private_data);
562                 status = NT_STATUS_OK;
563         }
564
565         /* if the system notify handler couldn't handle some of the
566            filter bits, or couldn't handle a request for recursion
567            then we need to install it in the array used for the
568            intra-samba notify handling */
569         if (e.filter != 0 || e.subdir_filter != 0) {
570                 status = notify_add_array(notify, rec, &e, private_data, depth);
571         }
572
573 done:
574         talloc_free(rec);
575         talloc_free(tmp_path);
576
577         return status;
578 }
579
580 NTSTATUS notify_remove_onelevel(struct notify_context *notify,
581                                 const struct file_id *fid,
582                                 void *private_data)
583 {
584         struct notify_entry_array *array;
585         struct db_record *rec;
586         DATA_BLOB blob;
587         TDB_DATA dbuf;
588         enum ndr_err_code ndr_err;
589         NTSTATUS status;
590         int i;
591
592         if (notify == NULL) {
593                 return NT_STATUS_NOT_IMPLEMENTED;
594         }
595
596         array = talloc_zero(talloc_tos(), struct notify_entry_array);
597         if (array == NULL) {
598                 return NT_STATUS_NO_MEMORY;
599         }
600
601         rec = notify->db_onelevel->fetch_locked(
602                 notify->db_onelevel, array,
603                 make_tdb_data((uint8_t *)fid, sizeof(*fid)));
604         if (rec == NULL) {
605                 DEBUG(10, ("notify_remove_onelevel: fetch_locked for %s failed"
606                            "\n", file_id_string_tos(fid)));
607                 TALLOC_FREE(array);
608                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
609         }
610
611         blob.data = (uint8_t *)rec->value.dptr;
612         blob.length = rec->value.dsize;
613
614         if (blob.length > 0) {
615                 ndr_err = ndr_pull_struct_blob(
616                         &blob, array, NULL, array,
617                         (ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
618                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
619                         DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
620                                    ndr_errstr(ndr_err)));
621                         TALLOC_FREE(array);
622                         return ndr_map_error2ntstatus(ndr_err);
623                 }
624                 if (DEBUGLEVEL >= 10) {
625                         DEBUG(10, ("notify_remove_onelevel:\n"));
626                         NDR_PRINT_DEBUG(notify_entry_array, array);
627                 }
628         }
629
630         for (i=0; i<array->num_entries; i++) {
631                 if ((private_data == array->entries[i].private_data) &&
632                     cluster_id_equal(&notify->server,
633                                      &array->entries[i].server)) {
634                         break;
635                 }
636         }
637
638         if (i == array->num_entries) {
639                 TALLOC_FREE(array);
640                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
641         }
642
643         array->entries[i] = array->entries[array->num_entries-1];
644         array->num_entries -= 1;
645
646         if (array->num_entries == 0) {
647                 rec->delete_rec(rec);
648                 TALLOC_FREE(array);
649                 return NT_STATUS_OK;
650         }
651
652         ndr_err = ndr_push_struct_blob(
653                 &blob, rec, NULL, array,
654                 (ndr_push_flags_fn_t)ndr_push_notify_entry_array);
655         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
656                 DEBUG(10, ("ndr_push_notify_entry_array failed: %s\n",
657                            ndr_errstr(ndr_err)));
658                 TALLOC_FREE(array);
659                 return ndr_map_error2ntstatus(ndr_err);
660         }
661
662         if (DEBUGLEVEL >= 10) {
663                 DEBUG(10, ("notify_add_onelevel:\n"));
664                 NDR_PRINT_DEBUG(notify_entry_array, array);
665         }
666
667         dbuf.dptr = blob.data;
668         dbuf.dsize = blob.length;
669
670         status = rec->store(rec, dbuf, TDB_REPLACE);
671         TALLOC_FREE(array);
672         if (!NT_STATUS_IS_OK(status)) {
673                 DEBUG(10, ("notify_add_onelevel: store failed: %s\n",
674                            nt_errstr(status)));
675                 return status;
676         }
677         return NT_STATUS_OK;
678 }
679
680 /*
681   remove a notify watch. Called when the directory handle is closed
682 */
683 NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
684 {
685         NTSTATUS status;
686         struct notify_list *listel;
687         int i, depth;
688         struct notify_depth *d;
689         struct db_record *rec;
690
691         /* see if change notify is enabled at all */
692         if (notify == NULL) {
693                 return NT_STATUS_NOT_IMPLEMENTED;
694         }
695
696         for (listel=notify->list;listel;listel=listel->next) {
697                 if (listel->private_data == private_data) {
698                         DLIST_REMOVE(notify->list, listel);
699                         break;
700                 }
701         }
702         if (listel == NULL) {
703                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
704         }
705
706         depth = listel->depth;
707
708         talloc_free(listel);
709
710         status = notify_fetch_locked(notify, &rec);
711         NT_STATUS_NOT_OK_RETURN(status);
712
713         status = notify_load(notify, rec);
714         if (!NT_STATUS_IS_OK(status)) {
715                 talloc_free(rec);
716                 return status;
717         }
718
719         if (depth >= notify->array->num_depths) {
720                 talloc_free(rec);
721                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
722         }
723
724         /* we only have to search at the depth of this element */
725         d = &notify->array->depth[depth];
726
727         for (i=0;i<d->num_entries;i++) {
728                 if (private_data == d->entries[i].private_data &&
729                     cluster_id_equal(&notify->server, &d->entries[i].server)) {
730                         break;
731                 }
732         }
733         if (i == d->num_entries) {
734                 talloc_free(rec);
735                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
736         }
737
738         if (i < d->num_entries-1) {
739                 memmove(&d->entries[i], &d->entries[i+1], 
740                         sizeof(d->entries[i])*(d->num_entries-(i+1)));
741         }
742         d->num_entries--;
743
744         status = notify_save(notify, rec);
745
746         talloc_free(rec);
747
748         return status;
749 }
750
751 /*
752   remove all notify watches for a messaging server
753 */
754 static NTSTATUS notify_remove_all(struct notify_context *notify,
755                                   const struct server_id *server)
756 {
757         NTSTATUS status;
758         int i, depth, del_count=0;
759         struct db_record *rec;
760
761         status = notify_fetch_locked(notify, &rec);
762         NT_STATUS_NOT_OK_RETURN(status);
763
764         status = notify_load(notify, rec);
765         if (!NT_STATUS_IS_OK(status)) {
766                 talloc_free(rec);
767                 return status;
768         }
769
770         /* we have to search for all entries across all depths, looking for matches
771            for the server id */
772         for (depth=0;depth<notify->array->num_depths;depth++) {
773                 struct notify_depth *d = &notify->array->depth[depth];
774                 for (i=0;i<d->num_entries;i++) {
775                         if (cluster_id_equal(server, &d->entries[i].server)) {
776                                 if (i < d->num_entries-1) {
777                                         memmove(&d->entries[i], &d->entries[i+1], 
778                                                 sizeof(d->entries[i])*(d->num_entries-(i+1)));
779                                 }
780                                 i--;
781                                 d->num_entries--;
782                                 del_count++;
783                         }
784                 }
785         }
786
787         if (del_count > 0) {
788                 status = notify_save(notify, rec);
789         }
790
791         talloc_free(rec);
792
793         return status;
794 }
795
796
797 /*
798   send a notify message to another messaging server
799 */
800 static NTSTATUS notify_send(struct notify_context *notify, struct notify_entry *e,
801                             const char *path, uint32_t action)
802 {
803         struct notify_event ev;
804         DATA_BLOB data;
805         NTSTATUS status;
806         enum ndr_err_code ndr_err;
807         TALLOC_CTX *tmp_ctx;
808
809         ev.action = action;
810         ev.path = path;
811         ev.private_data = e->private_data;
812
813         tmp_ctx = talloc_new(notify);
814
815         ndr_err = ndr_push_struct_blob(&data, tmp_ctx, NULL, &ev,
816                                        (ndr_push_flags_fn_t)ndr_push_notify_event);
817         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
818                 talloc_free(tmp_ctx);
819                 return ndr_map_error2ntstatus(ndr_err);
820         }
821
822         status = messaging_send(notify->messaging_ctx, e->server, 
823                                 MSG_PVFS_NOTIFY, &data);
824         talloc_free(tmp_ctx);
825         return status;
826 }
827
828 void notify_onelevel(struct notify_context *notify, uint32_t action,
829                      uint32_t filter, struct file_id fid, const char *name)
830 {
831         struct notify_entry_array *array;
832         TDB_DATA dbuf;
833         DATA_BLOB blob;
834         bool have_dead_entries = false;
835         int i;
836
837         if (notify == NULL) {
838                 return;
839         }
840
841         array = talloc_zero(talloc_tos(), struct notify_entry_array);
842         if (array == NULL) {
843                 return;
844         }
845
846         if (notify->db_onelevel->fetch(
847                     notify->db_onelevel, array,
848                     make_tdb_data((uint8_t *)&fid, sizeof(fid)),
849                     &dbuf) == -1) {
850                 TALLOC_FREE(array);
851                 return;
852         }
853
854         blob.data = (uint8 *)dbuf.dptr;
855         blob.length = dbuf.dsize;
856
857         if (blob.length > 0) {
858                 enum ndr_err_code ndr_err;
859                 ndr_err = ndr_pull_struct_blob(
860                         &blob, array, NULL, array,
861                         (ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
862                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
863                         DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
864                                    ndr_errstr(ndr_err)));
865                         TALLOC_FREE(array);
866                         return;
867                 }
868                 if (DEBUGLEVEL >= 10) {
869                         DEBUG(10, ("notify_onelevel:\n"));
870                         NDR_PRINT_DEBUG(notify_entry_array, array);
871                 }
872         }
873
874         for (i=0; i<array->num_entries; i++) {
875                 struct notify_entry *e = &array->entries[i];
876
877                 if ((e->filter & filter) != 0) {
878                         NTSTATUS status;
879
880                         status = notify_send(notify, e, name, action);
881                         if (NT_STATUS_EQUAL(
882                                     status, NT_STATUS_INVALID_HANDLE)) {
883                                 /*
884                                  * Mark the entry as dead. All entries have a
885                                  * path set. The marker used here is setting
886                                  * that to NULL.
887                                  */
888                                 e->path = NULL;
889                                 have_dead_entries = true;
890                         }
891                 }
892         }
893
894         if (!have_dead_entries) {
895                 TALLOC_FREE(array);
896                 return;
897         }
898
899         for (i=0; i<array->num_entries; i++) {
900                 struct notify_entry *e = &array->entries[i];
901                 if (e->path != NULL) {
902                         continue;
903                 }
904                 DEBUG(10, ("Deleting notify entries for process %s because "
905                            "it's gone\n", procid_str_static(&e->server)));
906                 /*
907                  * Potential TODO: This might need optimizing,
908                  * notify_remove_onelevel() does a fetch_locked() operation at
909                  * every call. But this would only matter if a process with
910                  * MANY notifies has died without shutting down properly.
911                  */
912                 notify_remove_onelevel(notify, &e->dir_id, e->private_data);
913         }
914
915         TALLOC_FREE(array);
916         return;
917 }
918
919 /*
920   trigger a notify message for anyone waiting on a matching event
921
922   This function is called a lot, and needs to be very fast. The unusual data structure
923   and traversal is designed to be fast in the average case, even for large numbers of
924   notifies
925 */
926 void notify_trigger(struct notify_context *notify,
927                     uint32_t action, uint32_t filter, const char *path)
928 {
929         NTSTATUS status;
930         int depth;
931         const char *p, *next_p;
932
933         DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
934                    "path=%s\n", (unsigned)action, (unsigned)filter, path));
935
936         /* see if change notify is enabled at all */
937         if (notify == NULL) {
938                 return;
939         }
940
941  again:
942         status = notify_load(notify, NULL);
943         if (!NT_STATUS_IS_OK(status)) {
944                 return;
945         }
946
947         /* loop along the given path, working with each directory depth separately */
948         for (depth=0,p=path;
949              p && depth < notify->array->num_depths;
950              p=next_p,depth++) {
951                 int p_len = p - path;
952                 int min_i, max_i, i;
953                 struct notify_depth *d = &notify->array->depth[depth];
954                 next_p = strchr(p+1, '/');
955
956                 /* see if there are any entries at this depth */
957                 if (d->num_entries == 0) continue;
958                 
959                 /* try to skip based on the maximum mask. If next_p is
960                  NULL then we know it will be a 'this directory'
961                  match, otherwise it must be a subdir match */
962                 if (next_p != NULL) {
963                         if (0 == (filter & d->max_mask_subdir)) {
964                                 continue;
965                         }
966                 } else {
967                         if (0 == (filter & d->max_mask)) {
968                                 continue;
969                         }
970                 }
971
972                 /* we know there is an entry here worth looking
973                  for. Use a bisection search to find the first entry
974                  with a matching path */
975                 min_i = 0;
976                 max_i = d->num_entries-1;
977
978                 while (min_i < max_i) {
979                         struct notify_entry *e;
980                         int cmp;
981                         i = (min_i+max_i)/2;
982                         e = &d->entries[i];
983                         cmp = strncmp(path, e->path, p_len);
984                         if (cmp == 0) {
985                                 if (p_len == e->path_len) {
986                                         max_i = i;
987                                 } else {
988                                         max_i = i-1;
989                                 }
990                         } else if (cmp < 0) {
991                                 max_i = i-1;
992                         } else {
993                                 min_i = i+1;
994                         }
995                 }
996
997                 if (min_i != max_i) {
998                         /* none match */
999                         continue;
1000                 }
1001
1002                 /* we now know that the entries start at min_i */
1003                 for (i=min_i;i<d->num_entries;i++) {
1004                         struct notify_entry *e = &d->entries[i];
1005                         if (p_len != e->path_len ||
1006                             strncmp(path, e->path, p_len) != 0) break;
1007                         if (next_p != NULL) {
1008                                 if (0 == (filter & e->subdir_filter)) {
1009                                         continue;
1010                                 }
1011                         } else {
1012                                 if (0 == (filter & e->filter)) {
1013                                         continue;
1014                                 }
1015                         }
1016                         status = notify_send(notify, e, path + e->path_len + 1,
1017                                              action);
1018
1019                         if (NT_STATUS_EQUAL(
1020                                     status, NT_STATUS_INVALID_HANDLE)) {
1021                                 struct server_id server = e->server;
1022
1023                                 DEBUG(10, ("Deleting notify entries for "
1024                                            "process %s because it's gone\n",
1025                                            procid_str_static(&e->server)));
1026                                 notify_remove_all(notify, &server);
1027                                 goto again;
1028                         }
1029                 }
1030         }
1031 }