source: trunk/server/source3/smbd/notify_internal.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

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