source: trunk/server/source4/ntvfs/common/notify.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: 16.6 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 <tdb.h>
29#include "../lib/util/util_tdb.h"
30#include "messaging/messaging.h"
31#include "lib/util/tdb_wrap.h"
32#include "lib/messaging/irpc.h"
33#include "librpc/gen_ndr/ndr_s4_notify.h"
34#include "../lib/util/dlinklist.h"
35#include "ntvfs/common/ntvfs_common.h"
36#include "ntvfs/sysdep/sys_notify.h"
37#include "cluster/cluster.h"
38#include "param/param.h"
39#include "lib/util/tsort.h"
40
41struct notify_context {
42 struct tdb_wrap *w;
43 struct server_id server;
44 struct messaging_context *messaging_ctx;
45 struct notify_list *list;
46 struct notify_array *array;
47 int seqnum;
48 struct sys_notify_context *sys_notify_ctx;
49};
50
51
52struct notify_list {
53 struct notify_list *next, *prev;
54 void *private_data;
55 void (*callback)(void *, const struct notify_event *);
56 void *sys_notify_handle;
57 int depth;
58};
59
60#define NOTIFY_KEY "notify array"
61
62#define NOTIFY_ENABLE "notify:enable"
63#define NOTIFY_ENABLE_DEFAULT true
64
65static NTSTATUS notify_remove_all(struct notify_context *notify);
66static void notify_handler(struct messaging_context *msg_ctx, void *private_data,
67 uint32_t msg_type, struct server_id server_id, DATA_BLOB *data);
68
69/*
70 destroy the notify context
71*/
72static int notify_destructor(struct notify_context *notify)
73{
74 messaging_deregister(notify->messaging_ctx, MSG_PVFS_NOTIFY, notify);
75 notify_remove_all(notify);
76 return 0;
77}
78
79/*
80 Open up the notify.tdb database. You should close it down using
81 talloc_free(). We need the messaging_ctx to allow for notifications
82 via internal messages
83*/
84struct notify_context *notify_init(TALLOC_CTX *mem_ctx, struct server_id server,
85 struct messaging_context *messaging_ctx,
86 struct loadparm_context *lp_ctx,
87 struct tevent_context *ev,
88 struct share_config *scfg)
89{
90 struct notify_context *notify;
91
92 if (share_bool_option(scfg, NOTIFY_ENABLE, NOTIFY_ENABLE_DEFAULT) != true) {
93 return NULL;
94 }
95
96 if (ev == NULL) {
97 return NULL;
98 }
99
100 notify = talloc(mem_ctx, struct notify_context);
101 if (notify == NULL) {
102 return NULL;
103 }
104
105 notify->w = cluster_tdb_tmp_open(notify, lp_ctx, "notify.tdb", TDB_SEQNUM);
106 if (notify->w == 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 = tdb_get_seqnum(notify->w->tdb);
116
117 talloc_set_destructor(notify, notify_destructor);
118
119 /* register with the messaging subsystem for the notify
120 message type */
121 messaging_register(notify->messaging_ctx, notify,
122 MSG_PVFS_NOTIFY, notify_handler);
123
124 notify->sys_notify_ctx = sys_notify_context_create(scfg, notify, ev);
125
126 return notify;
127}
128
129
130/*
131 lock the notify db
132*/
133static NTSTATUS notify_lock(struct notify_context *notify)
134{
135 if (tdb_lock_bystring(notify->w->tdb, NOTIFY_KEY) != 0) {
136 return NT_STATUS_INTERNAL_DB_CORRUPTION;
137 }
138 return NT_STATUS_OK;
139}
140
141/*
142 unlock the notify db
143*/
144static void notify_unlock(struct notify_context *notify)
145{
146 tdb_unlock_bystring(notify->w->tdb, NOTIFY_KEY);
147}
148
149/*
150 load the notify array
151*/
152static NTSTATUS notify_load(struct notify_context *notify)
153{
154 TDB_DATA dbuf;
155 DATA_BLOB blob;
156 enum ndr_err_code ndr_err;
157 int seqnum;
158
159 seqnum = tdb_get_seqnum(notify->w->tdb);
160
161 if (seqnum == notify->seqnum && notify->array != NULL) {
162 return NT_STATUS_OK;
163 }
164
165 notify->seqnum = seqnum;
166
167 talloc_free(notify->array);
168 notify->array = talloc_zero(notify, struct notify_array);
169 NT_STATUS_HAVE_NO_MEMORY(notify->array);
170
171 dbuf = tdb_fetch_bystring(notify->w->tdb, NOTIFY_KEY);
172 if (dbuf.dptr == NULL) {
173 return NT_STATUS_OK;
174 }
175
176 blob.data = dbuf.dptr;
177 blob.length = dbuf.dsize;
178
179 ndr_err = ndr_pull_struct_blob(&blob, notify->array, notify->array,
180 (ndr_pull_flags_fn_t)ndr_pull_notify_array);
181 free(dbuf.dptr);
182 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
183 return ndr_map_error2ntstatus(ndr_err);
184 }
185
186 return NT_STATUS_OK;
187}
188
189/*
190 compare notify entries for sorting
191*/
192static int notify_compare(const void *p1, const void *p2)
193{
194 const struct notify_entry *e1 = p1, *e2 = p2;
195 return strcmp(e1->path, e2->path);
196}
197
198/*
199 save the notify array
200*/
201static NTSTATUS notify_save(struct notify_context *notify)
202{
203 TDB_DATA dbuf;
204 DATA_BLOB blob;
205 enum ndr_err_code ndr_err;
206 int ret;
207 TALLOC_CTX *tmp_ctx;
208
209 /* if possible, remove some depth arrays */
210 while (notify->array->num_depths > 0 &&
211 notify->array->depth[notify->array->num_depths-1].num_entries == 0) {
212 notify->array->num_depths--;
213 }
214
215 /* we might just be able to delete the record */
216 if (notify->array->num_depths == 0) {
217 ret = tdb_delete_bystring(notify->w->tdb, NOTIFY_KEY);
218 if (ret != 0) {
219 return NT_STATUS_INTERNAL_DB_CORRUPTION;
220 }
221 return NT_STATUS_OK;
222 }
223
224 tmp_ctx = talloc_new(notify);
225 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
226
227 ndr_err = ndr_push_struct_blob(&blob, tmp_ctx, notify->array,
228 (ndr_push_flags_fn_t)ndr_push_notify_array);
229 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
230 talloc_free(tmp_ctx);
231 return ndr_map_error2ntstatus(ndr_err);
232 }
233
234 dbuf.dptr = blob.data;
235 dbuf.dsize = blob.length;
236
237 ret = tdb_store_bystring(notify->w->tdb, NOTIFY_KEY, dbuf, TDB_REPLACE);
238 talloc_free(tmp_ctx);
239 if (ret != 0) {
240 return NT_STATUS_INTERNAL_DB_CORRUPTION;
241 }
242
243 return NT_STATUS_OK;
244}
245
246
247/*
248 handle incoming notify messages
249*/
250static void notify_handler(struct messaging_context *msg_ctx, void *private_data,
251 uint32_t msg_type, struct server_id server_id, DATA_BLOB *data)
252{
253 struct notify_context *notify = talloc_get_type(private_data, struct notify_context);
254 enum ndr_err_code ndr_err;
255 struct notify_event ev;
256 TALLOC_CTX *tmp_ctx = talloc_new(notify);
257 struct notify_list *listel;
258
259 if (tmp_ctx == NULL) {
260 return;
261 }
262
263 ndr_err = ndr_pull_struct_blob(data, tmp_ctx, &ev,
264 (ndr_pull_flags_fn_t)ndr_pull_notify_event);
265 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
266 talloc_free(tmp_ctx);
267 return;
268 }
269
270 for (listel=notify->list;listel;listel=listel->next) {
271 if (listel->private_data == ev.private_data) {
272 listel->callback(listel->private_data, &ev);
273 break;
274 }
275 }
276
277 talloc_free(tmp_ctx);
278}
279
280/*
281 callback from sys_notify telling us about changes from the OS
282*/
283static void sys_notify_callback(struct sys_notify_context *ctx,
284 void *ptr, struct notify_event *ev)
285{
286 struct notify_list *listel = talloc_get_type(ptr, struct notify_list);
287 ev->private_data = listel;
288 listel->callback(listel->private_data, ev);
289}
290
291/*
292 add an entry to the notify array
293*/
294static NTSTATUS notify_add_array(struct notify_context *notify, struct notify_entry *e,
295 void *private_data, int depth)
296{
297 int i;
298 struct notify_depth *d;
299 struct notify_entry *ee;
300
301 /* possibly expand the depths array */
302 if (depth >= notify->array->num_depths) {
303 d = talloc_realloc(notify->array, notify->array->depth,
304 struct notify_depth, depth+1);
305 NT_STATUS_HAVE_NO_MEMORY(d);
306 for (i=notify->array->num_depths;i<=depth;i++) {
307 ZERO_STRUCT(d[i]);
308 }
309 notify->array->depth = d;
310 notify->array->num_depths = depth+1;
311 }
312 d = &notify->array->depth[depth];
313
314 /* expand the entries array */
315 ee = talloc_realloc(notify->array->depth, d->entries, struct notify_entry,
316 d->num_entries+1);
317 NT_STATUS_HAVE_NO_MEMORY(ee);
318 d->entries = ee;
319
320 d->entries[d->num_entries] = *e;
321 d->entries[d->num_entries].private_data = private_data;
322 d->entries[d->num_entries].server = notify->server;
323 d->entries[d->num_entries].path_len = strlen(e->path);
324 d->num_entries++;
325
326 d->max_mask |= e->filter;
327 d->max_mask_subdir |= e->subdir_filter;
328
329 TYPESAFE_QSORT(d->entries, d->num_entries, notify_compare);
330
331 /* recalculate the maximum masks */
332 d->max_mask = 0;
333 d->max_mask_subdir = 0;
334
335 for (i=0;i<d->num_entries;i++) {
336 d->max_mask |= d->entries[i].filter;
337 d->max_mask_subdir |= d->entries[i].subdir_filter;
338 }
339
340 return notify_save(notify);
341}
342
343/*
344 add a notify watch. This is called when a notify is first setup on a open
345 directory handle.
346*/
347NTSTATUS notify_add(struct notify_context *notify, struct notify_entry *e0,
348 void (*callback)(void *, const struct notify_event *),
349 void *private_data)
350{
351 struct notify_entry e = *e0;
352 NTSTATUS status;
353 char *tmp_path = NULL;
354 struct notify_list *listel;
355 size_t len;
356 int depth;
357
358 /* see if change notify is enabled at all */
359 if (notify == NULL) {
360 return NT_STATUS_NOT_IMPLEMENTED;
361 }
362
363 status = notify_lock(notify);
364 NT_STATUS_NOT_OK_RETURN(status);
365
366 status = notify_load(notify);
367 if (!NT_STATUS_IS_OK(status)) {
368 goto done;
369 }
370
371 /* cope with /. on the end of the path */
372 len = strlen(e.path);
373 if (len > 1 && e.path[len-1] == '.' && e.path[len-2] == '/') {
374 tmp_path = talloc_strndup(notify, e.path, len-2);
375 if (tmp_path == NULL) {
376 status = NT_STATUS_NO_MEMORY;
377 goto done;
378 }
379 e.path = tmp_path;
380 }
381
382 depth = count_chars(e.path, '/');
383
384 listel = talloc_zero(notify, struct notify_list);
385 if (listel == NULL) {
386 status = NT_STATUS_NO_MEMORY;
387 goto done;
388 }
389
390 listel->private_data = private_data;
391 listel->callback = callback;
392 listel->depth = depth;
393 DLIST_ADD(notify->list, listel);
394
395 /* ignore failures from sys_notify */
396 if (notify->sys_notify_ctx != NULL) {
397 /*
398 this call will modify e.filter and e.subdir_filter
399 to remove bits handled by the backend
400 */
401 status = sys_notify_watch(notify->sys_notify_ctx, &e,
402 sys_notify_callback, listel,
403 &listel->sys_notify_handle);
404 if (NT_STATUS_IS_OK(status)) {
405 talloc_steal(listel, listel->sys_notify_handle);
406 }
407 }
408
409 /* if the system notify handler couldn't handle some of the
410 filter bits, or couldn't handle a request for recursion
411 then we need to install it in the array used for the
412 intra-samba notify handling */
413 if (e.filter != 0 || e.subdir_filter != 0) {
414 status = notify_add_array(notify, &e, private_data, depth);
415 }
416
417done:
418 notify_unlock(notify);
419 talloc_free(tmp_path);
420
421 return status;
422}
423
424/*
425 remove a notify watch. Called when the directory handle is closed
426*/
427NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
428{
429 NTSTATUS status;
430 struct notify_list *listel;
431 int i, depth;
432 struct notify_depth *d;
433
434 /* see if change notify is enabled at all */
435 if (notify == NULL) {
436 return NT_STATUS_NOT_IMPLEMENTED;
437 }
438
439 for (listel=notify->list;listel;listel=listel->next) {
440 if (listel->private_data == private_data) {
441 DLIST_REMOVE(notify->list, listel);
442 break;
443 }
444 }
445 if (listel == NULL) {
446 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
447 }
448
449 depth = listel->depth;
450
451 talloc_free(listel);
452
453 status = notify_lock(notify);
454 NT_STATUS_NOT_OK_RETURN(status);
455
456 status = notify_load(notify);
457 if (!NT_STATUS_IS_OK(status)) {
458 notify_unlock(notify);
459 return status;
460 }
461
462 if (depth >= notify->array->num_depths) {
463 notify_unlock(notify);
464 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
465 }
466
467 /* we only have to search at the depth of this element */
468 d = &notify->array->depth[depth];
469
470 for (i=0;i<d->num_entries;i++) {
471 if (private_data == d->entries[i].private_data &&
472 cluster_id_equal(&notify->server, &d->entries[i].server)) {
473 break;
474 }
475 }
476 if (i == d->num_entries) {
477 notify_unlock(notify);
478 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
479 }
480
481 if (i < d->num_entries-1) {
482 memmove(&d->entries[i], &d->entries[i+1],
483 sizeof(d->entries[i])*(d->num_entries-(i+1)));
484 }
485 d->num_entries--;
486
487 status = notify_save(notify);
488
489 notify_unlock(notify);
490
491 return status;
492}
493
494/*
495 remove all notify watches for this messaging server
496*/
497static NTSTATUS notify_remove_all(struct notify_context *notify)
498{
499 NTSTATUS status;
500 int i, depth, del_count=0;
501
502 if (notify->list == NULL) {
503 return NT_STATUS_OK;
504 }
505
506 status = notify_lock(notify);
507 NT_STATUS_NOT_OK_RETURN(status);
508
509 status = notify_load(notify);
510 if (!NT_STATUS_IS_OK(status)) {
511 notify_unlock(notify);
512 return status;
513 }
514
515 /* we have to search for all entries across all depths, looking for matches
516 for our server id */
517 for (depth=0;depth<notify->array->num_depths;depth++) {
518 struct notify_depth *d = &notify->array->depth[depth];
519 for (i=0;i<d->num_entries;i++) {
520 if (cluster_id_equal(&notify->server, &d->entries[i].server)) {
521 if (i < d->num_entries-1) {
522 memmove(&d->entries[i], &d->entries[i+1],
523 sizeof(d->entries[i])*(d->num_entries-(i+1)));
524 }
525 i--;
526 d->num_entries--;
527 del_count++;
528 }
529 }
530 }
531
532 if (del_count > 0) {
533 status = notify_save(notify);
534 }
535
536 notify_unlock(notify);
537
538 return status;
539}
540
541
542/*
543 send a notify message to another messaging server
544*/
545static void notify_send(struct notify_context *notify, struct notify_entry *e,
546 const char *path, uint32_t action)
547{
548 struct notify_event ev;
549 DATA_BLOB data;
550 NTSTATUS status;
551 enum ndr_err_code ndr_err;
552 TALLOC_CTX *tmp_ctx;
553
554 ev.action = action;
555 ev.path = path;
556 ev.private_data = e->private_data;
557
558 tmp_ctx = talloc_new(notify);
559
560 ndr_err = ndr_push_struct_blob(&data, tmp_ctx, &ev, (ndr_push_flags_fn_t)ndr_push_notify_event);
561 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
562 talloc_free(tmp_ctx);
563 return;
564 }
565
566 status = messaging_send(notify->messaging_ctx, e->server,
567 MSG_PVFS_NOTIFY, &data);
568 talloc_free(tmp_ctx);
569}
570
571
572/*
573 trigger a notify message for anyone waiting on a matching event
574
575 This function is called a lot, and needs to be very fast. The unusual data structure
576 and traversal is designed to be fast in the average case, even for large numbers of
577 notifies
578*/
579void notify_trigger(struct notify_context *notify,
580 uint32_t action, uint32_t filter, const char *path)
581{
582 NTSTATUS status;
583 int depth;
584 const char *p, *next_p;
585
586 /* see if change notify is enabled at all */
587 if (notify == NULL) {
588 return;
589 }
590
591 status = notify_load(notify);
592 if (!NT_STATUS_IS_OK(status)) {
593 return;
594 }
595
596 /* loop along the given path, working with each directory depth separately */
597 for (depth=0,p=path;
598 p && depth < notify->array->num_depths;
599 p=next_p,depth++) {
600 int p_len = p - path;
601 int min_i, max_i, i;
602 struct notify_depth *d = &notify->array->depth[depth];
603 next_p = strchr(p+1, '/');
604
605 /* see if there are any entries at this depth */
606 if (d->num_entries == 0) continue;
607
608 /* try to skip based on the maximum mask. If next_p is
609 NULL then we know it will be a 'this directory'
610 match, otherwise it must be a subdir match */
611 if (next_p != NULL) {
612 if (0 == (filter & d->max_mask_subdir)) {
613 continue;
614 }
615 } else {
616 if (0 == (filter & d->max_mask)) {
617 continue;
618 }
619 }
620
621 /* we know there is an entry here worth looking
622 for. Use a bisection search to find the first entry
623 with a matching path */
624 min_i = 0;
625 max_i = d->num_entries-1;
626
627 while (min_i < max_i) {
628 struct notify_entry *e;
629 int cmp;
630 i = (min_i+max_i)/2;
631 e = &d->entries[i];
632 cmp = strncmp(path, e->path, p_len);
633 if (cmp == 0) {
634 if (p_len == e->path_len) {
635 max_i = i;
636 } else {
637 max_i = i-1;
638 }
639 } else if (cmp < 0) {
640 max_i = i-1;
641 } else {
642 min_i = i+1;
643 }
644 }
645
646 if (min_i != max_i) {
647 /* none match */
648 continue;
649 }
650
651 /* we now know that the entries start at min_i */
652 for (i=min_i;i<d->num_entries;i++) {
653 struct notify_entry *e = &d->entries[i];
654 if (p_len != e->path_len ||
655 strncmp(path, e->path, p_len) != 0) break;
656 if (next_p != NULL) {
657 if (0 == (filter & e->subdir_filter)) {
658 continue;
659 }
660 } else {
661 if (0 == (filter & e->filter)) {
662 continue;
663 }
664 }
665 notify_send(notify, e, path + e->path_len + 1, action);
666 }
667 }
668}
Note: See TracBrowser for help on using the repository browser.