source: vendor/current/source4/ntvfs/common/notify.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

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