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