summaryrefslogtreecommitdiff
path: root/source3/smbd/notify_internal.c
blob: a42404db3e6b4acbe99bca99ac6e61f90d6ed5a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
/* 
   Unix SMB/CIFS implementation.

   Copyright (C) Andrew Tridgell 2006

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
  this is the change notify database. It implements mechanisms for
  storing current change notify waiters in a tdb, and checking if a
  given event matches any of the stored notify waiiters.
*/

#include "includes.h"
#include "librpc/gen_ndr/ndr_notify.h"

struct notify_context {
	struct db_context *db_recursive;
	struct db_context *db_onelevel;
	struct server_id server;
	struct messaging_context *messaging_ctx;
	struct notify_list *list;
	struct notify_array *array;
	int seqnum;
	struct sys_notify_context *sys_notify_ctx;
	TDB_DATA key;
};


struct notify_list {
	struct notify_list *next, *prev;
	void *private_data;
	void (*callback)(void *, const struct notify_event *);
	void *sys_notify_handle;
	int depth;
};

#define NOTIFY_KEY "notify array"

#define NOTIFY_ENABLE		"notify:enable"
#define NOTIFY_ENABLE_DEFAULT	True

static NTSTATUS notify_remove_all(struct notify_context *notify,
				  const struct server_id *server);
static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
			   uint32_t msg_type, struct server_id server_id, DATA_BLOB *data);

/*
  destroy the notify context
*/
static int notify_destructor(struct notify_context *notify)
{
	messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);

	if (notify->list != NULL) {
		notify_remove_all(notify, &notify->server);
	}

	return 0;
}

/*
  Open up the notify.tdb database. You should close it down using
  talloc_free(). We need the messaging_ctx to allow for notifications
  via internal messages
*/
struct notify_context *notify_init(TALLOC_CTX *mem_ctx, struct server_id server, 
				   struct messaging_context *messaging_ctx,
				   struct event_context *ev,
				   connection_struct *conn)
{
	struct notify_context *notify;

	if (!lp_change_notify(conn->params)) {
		return NULL;
	}

	notify = talloc(mem_ctx, struct notify_context);
	if (notify == NULL) {
		return NULL;
	}

	notify->db_recursive = db_open(notify, lock_path("notify.tdb"),
				       0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST,
				       O_RDWR|O_CREAT, 0644);
	if (notify->db_recursive == NULL) {
		talloc_free(notify);
		return NULL;
	}

	notify->db_onelevel = db_open(notify, lock_path("notify_onelevel.tdb"),
				      0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST,
				      O_RDWR|O_CREAT, 0644);
	if (notify->db_onelevel == NULL) {
		talloc_free(notify);
		return NULL;
	}

	notify->server = server;
	notify->messaging_ctx = messaging_ctx;
	notify->list = NULL;
	notify->array = NULL;
	notify->seqnum = notify->db_recursive->get_seqnum(
		notify->db_recursive);
	notify->key = string_term_tdb_data(NOTIFY_KEY);

	talloc_set_destructor(notify, notify_destructor);

	/* register with the messaging subsystem for the notify
	   message type */
	messaging_register(notify->messaging_ctx, notify, 
			   MSG_PVFS_NOTIFY, notify_handler);

	notify->sys_notify_ctx = sys_notify_context_create(conn, notify, ev);

	return notify;
}

/*
  lock and fetch the record
*/
static NTSTATUS notify_fetch_locked(struct notify_context *notify, struct db_record **rec)
{
	*rec = notify->db_recursive->fetch_locked(notify->db_recursive,
						  notify, notify->key);
	if (*rec == NULL) {
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}
	return NT_STATUS_OK;
}

/*
  load the notify array
*/
static NTSTATUS notify_load(struct notify_context *notify, struct db_record *rec)
{
	TDB_DATA dbuf;
	DATA_BLOB blob;
	NTSTATUS status;
	int seqnum;

	seqnum = notify->db_recursive->get_seqnum(notify->db_recursive);

	if (seqnum == notify->seqnum && notify->array != NULL) {
		return NT_STATUS_OK;
	}

	notify->seqnum = seqnum;

	talloc_free(notify->array);
	notify->array = TALLOC_ZERO_P(notify, struct notify_array);
	NT_STATUS_HAVE_NO_MEMORY(notify->array);

	if (!rec) {
		if (notify->db_recursive->fetch(notify->db_recursive, notify,
						notify->key, &dbuf) != 0) {
			return NT_STATUS_INTERNAL_DB_CORRUPTION;
		}
	} else {
		dbuf = rec->value;
	}

	blob.data = (uint8 *)dbuf.dptr;
	blob.length = dbuf.dsize;

	status = NT_STATUS_OK;
	if (blob.length > 0) {
		enum ndr_err_code ndr_err;
		ndr_err = ndr_pull_struct_blob(&blob, notify->array, NULL, notify->array,
					       (ndr_pull_flags_fn_t)ndr_pull_notify_array);
		if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
			status = ndr_map_error2ntstatus(ndr_err);
		}
	}

	if (DEBUGLEVEL >= 10) {
		DEBUG(10, ("notify_load:\n"));
		NDR_PRINT_DEBUG(notify_array, notify->array);
	}

	if (!rec) {
		talloc_free(dbuf.dptr);
	}

	return status;
}

/*
  compare notify entries for sorting
*/
static int notify_compare(const void *p1, const void *p2)
{
	const struct notify_entry *e1 = (const struct notify_entry *)p1;
	const struct notify_entry *e2 = (const struct notify_entry *)p2;
	return strcmp(e1->path, e2->path);
}

/*
  save the notify array
*/
static NTSTATUS notify_save(struct notify_context *notify, struct db_record *rec)
{
	TDB_DATA dbuf;
	DATA_BLOB blob;
	NTSTATUS status;
	enum ndr_err_code ndr_err;
	TALLOC_CTX *tmp_ctx;

	/* if possible, remove some depth arrays */
	while (notify->array->num_depths > 0 &&
	       notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
		notify->array->num_depths--;
	}

	/* we might just be able to delete the record */
	if (notify->array->num_depths == 0) {
		return rec->delete_rec(rec);
	}

	tmp_ctx = talloc_new(notify);
	NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);

	ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, NULL, notify->array,
				      (ndr_push_flags_fn_t)ndr_push_notify_array);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		talloc_free(tmp_ctx);
		return ndr_map_error2ntstatus(ndr_err);
	}

	if (DEBUGLEVEL >= 10) {
		DEBUG(10, ("notify_save:\n"));
		NDR_PRINT_DEBUG(notify_array, notify->array);
	}

	dbuf.dptr = blob.data;
	dbuf.dsize = blob.length;

	status = rec->store(rec, dbuf, TDB_REPLACE);
	talloc_free(tmp_ctx);

	return status;
}


/*
  handle incoming notify messages
*/
static void notify_handler(struct messaging_context *msg_ctx, void *private_data, 
			   uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
{
	struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
	enum ndr_err_code ndr_err;
	struct notify_event ev;
	TALLOC_CTX *tmp_ctx = talloc_new(notify);
	struct notify_list *listel;

	if (tmp_ctx == NULL) {
		return;
	}

	ndr_err = ndr_pull_struct_blob(data, tmp_ctx, NULL, &ev,
				       (ndr_pull_flags_fn_t)ndr_pull_notify_event);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		talloc_free(tmp_ctx);
		return;
	}

	for (listel=notify->list;listel;listel=listel->next) {
		if (listel->private_data == ev.private_data) {
			listel->callback(listel->private_data, &ev);
			break;
		}
	}

	talloc_free(tmp_ctx);	
}

/*
  callback from sys_notify telling us about changes from the OS
*/
static void sys_notify_callback(struct sys_notify_context *ctx, 
				void *ptr, struct notify_event *ev)
{
	struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
	ev->private_data = listel;
	DEBUG(10, ("sys_notify_callback called with action=%d, for %s\n",
		   ev->action, ev->path));
	listel->callback(listel->private_data, ev);
}

/*
  add an entry to the notify array
*/
static NTSTATUS notify_add_array(struct notify_context *notify, struct db_record *rec,
				 struct notify_entry *e,
				 void *private_data, int depth)
{
	int i;
	struct notify_depth *d;
	struct notify_entry *ee;

	/* possibly expand the depths array */
	if (depth >= notify->array->num_depths) {
		d = talloc_realloc(notify->array, notify->array->depth, 
				   struct notify_depth, depth+1);
		NT_STATUS_HAVE_NO_MEMORY(d);
		for (i=notify->array->num_depths;i<=depth;i++) {
			ZERO_STRUCT(d[i]);
		}
		notify->array->depth = d;
		notify->array->num_depths = depth+1;
	}
	d = &notify->array->depth[depth];

	/* expand the entries array */
	ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
			    d->num_entries+1);
	NT_STATUS_HAVE_NO_MEMORY(ee);
	d->entries = ee;

	d->entries[d->num_entries] = *e;
	d->entries[d->num_entries].private_data = private_data;
	d->entries[d->num_entries].server = notify->server;
	d->entries[d->num_entries].path_len = strlen(e->path);
	d->num_entries++;

	d->max_mask |= e->filter;
	d->max_mask_subdir |= e->subdir_filter;

	if (d->num_entries > 1) {
		qsort(d->entries, d->num_entries, sizeof(d->entries[0]), notify_compare);
	}

	/* recalculate the maximum masks */
	d->max_mask = 0;
	d->max_mask_subdir = 0;

	for (i=0;i<d->num_entries;i++) {
		d->max_mask |= d->entries[i].filter;
		d->max_mask_subdir |= d->entries[i].subdir_filter;
	}

	return notify_save(notify, rec);
}

/*
  Add a non-recursive watch
*/

static void notify_add_onelevel(struct notify_context *notify,
				struct notify_entry *e, void *private_data)
{
	struct notify_entry_array *array;
	struct db_record *rec;
	DATA_BLOB blob;
	TDB_DATA dbuf;
	enum ndr_err_code ndr_err;
	NTSTATUS status;

	array = talloc_zero(talloc_tos(), struct notify_entry_array);
	if (array == NULL) {
		return;
	}

	rec = notify->db_onelevel->fetch_locked(
		notify->db_onelevel, talloc_tos(),
		make_tdb_data((uint8_t *)&e->dir_id, sizeof(e->dir_id)));
	if (rec == NULL) {
		DEBUG(10, ("notify_add_onelevel: fetch_locked for %s failed"
			   "\n", file_id_string_tos(&e->dir_id)));
		TALLOC_FREE(array);
		return;
	}

	blob.data = (uint8_t *)rec->value.dptr;
	blob.length = rec->value.dsize;

	if (blob.length > 0) {
		ndr_err = ndr_pull_struct_blob(
			&blob, array, NULL, array,
			(ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
		if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
			DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
				   ndr_errstr(ndr_err)));
			TALLOC_FREE(array);
			return;
		}
		if (DEBUGLEVEL >= 10) {
			DEBUG(10, ("notify_add_onelevel:\n"));
			NDR_PRINT_DEBUG(notify_entry_array, array);
		}
	}

	array->entries = talloc_realloc(array, array->entries,
					struct notify_entry,
					array->num_entries+1);
	if (array->entries == NULL) {
		TALLOC_FREE(array);
		return;
	}
	array->entries[array->num_entries] = *e;
	array->entries[array->num_entries].private_data = private_data;
	array->entries[array->num_entries].server = notify->server;
	array->num_entries += 1;

	ndr_err = ndr_push_struct_blob(
		&blob, rec, NULL, array,
		(ndr_push_flags_fn_t)ndr_push_notify_entry_array);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		DEBUG(10, ("ndr_push_notify_entry_array failed: %s\n",
			   ndr_errstr(ndr_err)));
		TALLOC_FREE(array);
		return;
	}

	if (DEBUGLEVEL >= 10) {
		DEBUG(10, ("notify_add_onelevel:\n"));
		NDR_PRINT_DEBUG(notify_entry_array, array);
	}

	dbuf.dptr = blob.data;
	dbuf.dsize = blob.length;

	status = rec->store(rec, dbuf, TDB_REPLACE);
	TALLOC_FREE(array);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(10, ("notify_add_onelevel: store failed: %s\n",
			   nt_errstr(status)));
		return;
	}
	e->filter = 0;
	return;
}


/*
  add a notify watch. This is called when a notify is first setup on a open
  directory handle.
*/
NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
		    void (*callback)(void *, const struct notify_event *), 
		    void *private_data)
{
	struct notify_entry e = *e0;
	NTSTATUS status;
	char *tmp_path = NULL;
	struct notify_list *listel;
	size_t len;
	int depth;
	struct db_record *rec;

	/* see if change notify is enabled at all */
	if (notify == NULL) {
		return NT_STATUS_NOT_IMPLEMENTED;
	}

	status = notify_fetch_locked(notify, &rec);
	NT_STATUS_NOT_OK_RETURN(status);

	status = notify_load(notify, rec);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(rec);
		return status;
	}

	/* cope with /. on the end of the path */
	len = strlen(e.path);
	if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
		tmp_path = talloc_strndup(notify, e.path, len-2);
		if (tmp_path == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto done;
		}
		e.path = tmp_path;
	}

	depth = count_chars(e.path, '/');

	listel = TALLOC_ZERO_P(notify, struct notify_list);
	if (listel == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto done;
	}

	listel->private_data = private_data;
	listel->callback = callback;
	listel->depth = depth;
	DLIST_ADD(notify->list, listel);

	/* ignore failures from sys_notify */
	if (notify->sys_notify_ctx != NULL) {
		/*
		  this call will modify e.filter and e.subdir_filter
		  to remove bits handled by the backend
		*/
		status = sys_notify_watch(notify->sys_notify_ctx, &e,
					  sys_notify_callback, listel, 
					  &listel->sys_notify_handle);
		if (NT_STATUS_IS_OK(status)) {
			talloc_steal(listel, listel->sys_notify_handle);
		}
	}

	if (e.filter != 0) {
		notify_add_onelevel(notify, &e, private_data);
		status = NT_STATUS_OK;
	}

	/* if the system notify handler couldn't handle some of the
	   filter bits, or couldn't handle a request for recursion
	   then we need to install it in the array used for the
	   intra-samba notify handling */
	if (e.filter != 0 || e.subdir_filter != 0) {
		status = notify_add_array(notify, rec, &e, private_data, depth);
	}

done:
	talloc_free(rec);
	talloc_free(tmp_path);

	return status;
}

NTSTATUS notify_remove_onelevel(struct notify_context *notify,
				const struct file_id *fid,
				void *private_data)
{
	struct notify_entry_array *array;
	struct db_record *rec;
	DATA_BLOB blob;
	TDB_DATA dbuf;
	enum ndr_err_code ndr_err;
	NTSTATUS status;
	int i;

	array = talloc_zero(talloc_tos(), struct notify_entry_array);
	if (array == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	rec = notify->db_onelevel->fetch_locked(
		notify->db_onelevel, talloc_tos(),
		make_tdb_data((uint8_t *)fid, sizeof(*fid)));
	if (rec == NULL) {
		DEBUG(10, ("notify_remove_onelevel: fetch_locked for %s failed"
			   "\n", file_id_string_tos(fid)));
		TALLOC_FREE(array);
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	blob.data = (uint8_t *)rec->value.dptr;
	blob.length = rec->value.dsize;

	if (blob.length > 0) {
		ndr_err = ndr_pull_struct_blob(
			&blob, array, NULL, array,
			(ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
		if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
			DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
				   ndr_errstr(ndr_err)));
			TALLOC_FREE(array);
			return ndr_map_error2ntstatus(ndr_err);
		}
		if (DEBUGLEVEL >= 10) {
			DEBUG(10, ("notify_remove_onelevel:\n"));
			NDR_PRINT_DEBUG(notify_entry_array, array);
		}
	}

	for (i=0; i<array->num_entries; i++) {
		if ((private_data == array->entries[i].private_data) &&
		    cluster_id_equal(&notify->server,
				     &array->entries[i].server)) {
			break;
		}
	}

	if (i == array->num_entries) {
		TALLOC_FREE(array);
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	array->entries[i] = array->entries[array->num_entries-1];
	array->num_entries -= 1;

	if (array->num_entries == 0) {
		rec->delete_rec(rec);
		TALLOC_FREE(array);
		return NT_STATUS_OK;
	}

	ndr_err = ndr_push_struct_blob(
		&blob, rec, NULL, array,
		(ndr_push_flags_fn_t)ndr_push_notify_entry_array);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		DEBUG(10, ("ndr_push_notify_entry_array failed: %s\n",
			   ndr_errstr(ndr_err)));
		TALLOC_FREE(array);
		return ndr_map_error2ntstatus(ndr_err);
	}

	if (DEBUGLEVEL >= 10) {
		DEBUG(10, ("notify_add_onelevel:\n"));
		NDR_PRINT_DEBUG(notify_entry_array, array);
	}

	dbuf.dptr = blob.data;
	dbuf.dsize = blob.length;

	status = rec->store(rec, dbuf, TDB_REPLACE);
	TALLOC_FREE(array);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(10, ("notify_add_onelevel: store failed: %s\n",
			   nt_errstr(status)));
		return status;
	}
	return NT_STATUS_OK;
}

/*
  remove a notify watch. Called when the directory handle is closed
*/
NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
{
	NTSTATUS status;
	struct notify_list *listel;
	int i, depth;
	struct notify_depth *d;
	struct db_record *rec;

	/* see if change notify is enabled at all */
	if (notify == NULL) {
		return NT_STATUS_NOT_IMPLEMENTED;
	}

	for (listel=notify->list;listel;listel=listel->next) {
		if (listel->private_data == private_data) {
			DLIST_REMOVE(notify->list, listel);
			break;
		}
	}
	if (listel == NULL) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	depth = listel->depth;

	talloc_free(listel);

	status = notify_fetch_locked(notify, &rec);
	NT_STATUS_NOT_OK_RETURN(status);

	status = notify_load(notify, rec);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(rec);
		return status;
	}

	if (depth >= notify->array->num_depths) {
		talloc_free(rec);
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	/* we only have to search at the depth of this element */
	d = &notify->array->depth[depth];

	for (i=0;i<d->num_entries;i++) {
		if (private_data == d->entries[i].private_data &&
		    cluster_id_equal(&notify->server, &d->entries[i].server)) {
			break;
		}
	}
	if (i == d->num_entries) {
		talloc_free(rec);
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	if (i < d->num_entries-1) {
		memmove(&d->entries[i], &d->entries[i+1], 
			sizeof(d->entries[i])*(d->num_entries-(i+1)));
	}
	d->num_entries--;

	status = notify_save(notify, rec);

	talloc_free(rec);

	return status;
}

/*
  remove all notify watches for a messaging server
*/
static NTSTATUS notify_remove_all(struct notify_context *notify,
				  const struct server_id *server)
{
	NTSTATUS status;
	int i, depth, del_count=0;
	struct db_record *rec;

	status = notify_fetch_locked(notify, &rec);
	NT_STATUS_NOT_OK_RETURN(status);

	status = notify_load(notify, rec);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(rec);
		return status;
	}

	/* we have to search for all entries across all depths, looking for matches
	   for the server id */
	for (depth=0;depth<notify->array->num_depths;depth++) {
		struct notify_depth *d = &notify->array->depth[depth];
		for (i=0;i<d->num_entries;i++) {
			if (cluster_id_equal(server, &d->entries[i].server)) {
				if (i < d->num_entries-1) {
					memmove(&d->entries[i], &d->entries[i+1], 
						sizeof(d->entries[i])*(d->num_entries-(i+1)));
				}
				i--;
				d->num_entries--;
				del_count++;
			}
		}
	}

	if (del_count > 0) {
		status = notify_save(notify, rec);
	}

	talloc_free(rec);

	return status;
}


/*
  send a notify message to another messaging server
*/
static NTSTATUS notify_send(struct notify_context *notify, struct notify_entry *e,
			    const char *path, uint32_t action)
{
	struct notify_event ev;
	DATA_BLOB data;
	NTSTATUS status;
	enum ndr_err_code ndr_err;
	TALLOC_CTX *tmp_ctx;

	ev.action = action;
	ev.path = path;
	ev.private_data = e->private_data;

	tmp_ctx = talloc_new(notify);

	ndr_err = ndr_push_struct_blob(&data, tmp_ctx, NULL, &ev,
				       (ndr_push_flags_fn_t)ndr_push_notify_event);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		talloc_free(tmp_ctx);
		return ndr_map_error2ntstatus(ndr_err);
	}

	status = messaging_send(notify->messaging_ctx, e->server, 
				MSG_PVFS_NOTIFY, &data);
	talloc_free(tmp_ctx);
	return status;
}

void notify_onelevel(struct notify_context *notify, uint32_t action,
		     uint32_t filter, struct file_id fid, const char *name)
{
	struct notify_entry_array *array;
	TDB_DATA dbuf;
	DATA_BLOB blob;
	bool have_dead_entries = false;
	int i;

	array = talloc_zero(talloc_tos(), struct notify_entry_array);
	if (array == NULL) {
		return;
	}

	if (notify->db_onelevel->fetch(
		    notify->db_onelevel, array,
		    make_tdb_data((uint8_t *)&fid, sizeof(fid)),
		    &dbuf) == -1) {
		TALLOC_FREE(array);
		return;
	}

	blob.data = (uint8 *)dbuf.dptr;
	blob.length = dbuf.dsize;

	if (blob.length > 0) {
		enum ndr_err_code ndr_err;
		ndr_err = ndr_pull_struct_blob(
			&blob, array, NULL, array,
			(ndr_pull_flags_fn_t)ndr_pull_notify_entry_array);
		if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
			DEBUG(10, ("ndr_pull_notify_entry_array failed: %s\n",
				   ndr_errstr(ndr_err)));
			TALLOC_FREE(array);
			return;
		}
		if (DEBUGLEVEL >= 10) {
			DEBUG(10, ("notify_onelevel:\n"));
			NDR_PRINT_DEBUG(notify_entry_array, array);
		}
	}

	for (i=0; i<array->num_entries; i++) {
		struct notify_entry *e = &array->entries[i];

		if ((e->filter & filter) != 0) {
			NTSTATUS status;

			status = notify_send(notify, e, name, action);
			if (NT_STATUS_EQUAL(
				    status, NT_STATUS_INVALID_HANDLE)) {
				/*
				 * Mark the entry as dead. All entries have a
				 * path set. The marker used here is setting
				 * that to NULL.
				 */
				e->path = NULL;
				have_dead_entries = true;
			}
		}
	}

	if (!have_dead_entries) {
		TALLOC_FREE(array);
		return;
	}

	for (i=0; i<array->num_entries; i++) {
		struct notify_entry *e = &array->entries[i];
		if (e->path != NULL) {
			continue;
		}
		DEBUG(10, ("Deleting notify entries for process %s because "
			   "it's gone\n", procid_str_static(&e->server)));
		/*
		 * Potential TODO: This might need optimizing,
		 * notify_remove_onelevel() does a fetch_locked() operation at
		 * every call. But this would only matter if a process with
		 * MANY notifies has died without shutting down properly.
		 */
		notify_remove_onelevel(notify, &e->dir_id, e->private_data);
	}

	TALLOC_FREE(array);
	return;
}

/*
  trigger a notify message for anyone waiting on a matching event

  This function is called a lot, and needs to be very fast. The unusual data structure
  and traversal is designed to be fast in the average case, even for large numbers of
  notifies
*/
void notify_trigger(struct notify_context *notify,
		    uint32_t action, uint32_t filter, const char *path)
{
	NTSTATUS status;
	int depth;
	const char *p, *next_p;

	DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
		   "path=%s\n", (unsigned)action, (unsigned)filter, path));

	/* see if change notify is enabled at all */
	if (notify == NULL) {
		return;
	}

 again:
	status = notify_load(notify, NULL);
	if (!NT_STATUS_IS_OK(status)) {
		return;
	}

	/* loop along the given path, working with each directory depth separately */
	for (depth=0,p=path;
	     p && depth < notify->array->num_depths;
	     p=next_p,depth++) {
		int p_len = p - path;
		int min_i, max_i, i;
		struct notify_depth *d = &notify->array->depth[depth];
		next_p = strchr(p+1, '/');

		/* see if there are any entries at this depth */
		if (d->num_entries == 0) continue;

		/* try to skip based on the maximum mask. If next_p is
		 NULL then we know it will be a 'this directory'
		 match, otherwise it must be a subdir match */
		if (next_p != NULL) {
			if (0 == (filter & d->max_mask_subdir)) {
				continue;
			}
		} else {
			if (0 == (filter & d->max_mask)) {
				continue;
			}
		}

		/* we know there is an entry here worth looking
		 for. Use a bisection search to find the first entry
		 with a matching path */
		min_i = 0;
		max_i = d->num_entries-1;

		while (min_i < max_i) {
			struct notify_entry *e;
			int cmp;
			i = (min_i+max_i)/2;
			e = &d->entries[i];
			cmp = strncmp(path, e->path, p_len);
			if (cmp == 0) {
				if (p_len == e->path_len) {
					max_i = i;
				} else {
					max_i = i-1;
				}
			} else if (cmp < 0) {
				max_i = i-1;
			} else {
				min_i = i+1;
			}
		}

		if (min_i != max_i) {
			/* none match */
			continue;
		}

		/* we now know that the entries start at min_i */
		for (i=min_i;i<d->num_entries;i++) {
			struct notify_entry *e = &d->entries[i];
			if (p_len != e->path_len ||
			    strncmp(path, e->path, p_len) != 0) break;
			if (next_p != NULL) {
				if (0 == (filter & e->subdir_filter)) {
					continue;
				}
			} else {
				if (0 == (filter & e->filter)) {
					continue;
				}
			}
			status = notify_send(notify, e,	path + e->path_len + 1,
					     action);

			if (NT_STATUS_EQUAL(
				    status, NT_STATUS_INVALID_HANDLE)) {
				struct server_id server = e->server;

				DEBUG(10, ("Deleting notify entries for "
					   "process %s because it's gone\n",
					   procid_str_static(&e->server)));
				notify_remove_all(notify, &server);
				goto again;
			}
		}
	}
}