ctdb-daemon: Fix typo in debug message
[Samba.git] / source3 / smbd / notify_internal.c
blob845a7cc8f7f69c835887272fb9571a6302589dda
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2006
5 Copyright (C) Volker Lendecke 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 this is the change notify database. It implements mechanisms for
23 storing current change notify waiters in a tdb, and checking if a
24 given event matches any of the stored notify waiters.
27 #include "includes.h"
28 #include "system/filesys.h"
29 #include "librpc/gen_ndr/ndr_notify.h"
30 #include "dbwrap/dbwrap.h"
31 #include "dbwrap/dbwrap_open.h"
32 #include "dbwrap/dbwrap_tdb.h"
33 #include "smbd/smbd.h"
34 #include "messages.h"
35 #include "lib/tdb_wrap/tdb_wrap.h"
36 #include "util_tdb.h"
37 #include "lib/param/param.h"
38 #include "lib/dbwrap/dbwrap_cache.h"
39 #include "ctdb_srvids.h"
40 #include "ctdbd_conn.h"
41 #include "ctdb_conn.h"
42 #include "lib/util/tevent_unix.h"
44 struct notify_list {
45 struct notify_list *next, *prev;
46 const char *path;
47 void (*callback)(void *, struct timespec, const struct notify_event *);
48 void *private_data;
51 struct notify_context {
52 struct messaging_context *msg;
53 struct notify_list *list;
56 * The notify database is split up into two databases: One
57 * relatively static index db and the real notify db with the
58 * volatile entries.
62 * "db_notify" is indexed by pathname. Per record it stores an
63 * array of notify_db_entry structs. These represent the
64 * notify records as requested by the smb client. This
65 * database is always held locally, it is never clustered.
67 struct db_context *db_notify;
70 * "db_index" is indexed by pathname. The records are an array
71 * of VNNs which have any interest in notifies for this path
72 * name.
74 * In the non-clustered case this database is cached in RAM by
75 * means of db_cache_open, which maintains a cache per
76 * process. Cache consistency is maintained by the tdb
77 * sequence number.
79 * In the clustered case right now we can not use the tdb
80 * sequence number, but by means of read only records we
81 * should be able to avoid a lot of full migrations.
83 * In both cases, it is important to keep the update
84 * operations to db_index to a minimum. This is achieved by
85 * delayed deletion. When a db_notify is initially created,
86 * the db_index record is also created. When more notifies are
87 * added for a path, then only the db_notify record needs to be
88 * modified, the db_index record is not touched. When the last
89 * entry from the db_notify record is deleted, the db_index
90 * record is not immediately deleted. Instead, the db_notify
91 * record is replaced with a current timestamp. A regular
92 * cleanup process will delete all db_index records that are
93 * older than a minute.
95 struct db_context *db_index;
98 static void notify_trigger_local(struct notify_context *notify,
99 uint32_t action, uint32_t filter,
100 const char *path, size_t path_len,
101 bool recursive);
102 static NTSTATUS notify_send(struct notify_context *notify,
103 struct server_id *pid,
104 const char *path, uint32_t action,
105 void *private_data);
106 static NTSTATUS notify_add_entry(struct db_record *rec,
107 const struct notify_db_entry *e,
108 bool *p_add_idx);
109 static NTSTATUS notify_add_idx(struct db_record *rec, uint32_t vnn);
111 static NTSTATUS notify_del_entry(struct db_record *rec,
112 const struct server_id *pid,
113 void *private_data);
114 static NTSTATUS notify_del_idx(struct db_record *rec, uint32_t vnn);
116 static int notify_context_destructor(struct notify_context *notify);
118 static void notify_handler(struct messaging_context *msg_ctx,
119 void *private_data, uint32_t msg_type,
120 struct server_id server_id, DATA_BLOB *data);
122 struct notify_context *notify_init(TALLOC_CTX *mem_ctx,
123 struct messaging_context *msg,
124 struct tevent_context *ev)
126 struct loadparm_context *lp_ctx;
127 struct notify_context *notify;
128 char *db_path;
130 notify = talloc(mem_ctx, struct notify_context);
131 if (notify == NULL) {
132 goto fail;
134 notify->msg = msg;
135 notify->list = NULL;
137 lp_ctx = loadparm_init_s3(notify, loadparm_s3_helpers());
139 db_path = lock_path("notify.tdb");
140 if (db_path == NULL) {
141 goto fail;
144 notify->db_notify = db_open_tdb(
145 notify, lp_ctx, db_path,
146 0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
147 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
148 talloc_unlink(notify, lp_ctx);
149 TALLOC_FREE(db_path);
150 if (notify->db_notify == NULL) {
151 goto fail;
154 db_path = lock_path("notify_index.tdb");
155 if (db_path == NULL) {
156 goto fail;
159 notify->db_index = db_open(
160 notify, db_path,
161 0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
162 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_3, DBWRAP_FLAG_NONE);
163 TALLOC_FREE(db_path);
164 if (notify->db_index == NULL) {
165 goto fail;
167 if (!lp_clustering()) {
168 notify->db_index = db_open_cache(notify, notify->db_index);
169 if (notify->db_index == NULL) {
170 goto fail;
174 if (notify->msg != NULL) {
175 NTSTATUS status;
177 status = messaging_register(notify->msg, notify,
178 MSG_PVFS_NOTIFY, notify_handler);
179 if (!NT_STATUS_IS_OK(status)) {
180 DEBUG(1, ("messaging_register returned %s\n",
181 nt_errstr(status)));
182 goto fail;
186 talloc_set_destructor(notify, notify_context_destructor);
188 return notify;
189 fail:
190 TALLOC_FREE(notify);
191 return NULL;
194 static int notify_context_destructor(struct notify_context *notify)
196 DEBUG(10, ("notify_context_destructor called\n"));
198 if (notify->msg != NULL) {
199 messaging_deregister(notify->msg, MSG_PVFS_NOTIFY, notify);
202 while (notify->list != NULL) {
203 DEBUG(10, ("Removing private_data=%p\n",
204 notify->list->private_data));
205 notify_remove(notify, notify->list->private_data);
207 return 0;
210 NTSTATUS notify_add(struct notify_context *notify,
211 const char *path, uint32_t filter, uint32_t subdir_filter,
212 void (*callback)(void *, struct timespec,
213 const struct notify_event *),
214 void *private_data)
216 struct notify_db_entry e;
217 struct notify_list *listel;
218 struct db_record *notify_rec, *idx_rec;
219 bool add_idx;
220 NTSTATUS status;
221 TDB_DATA key, notify_copy;
223 if (notify == NULL) {
224 return NT_STATUS_NOT_IMPLEMENTED;
227 DEBUG(10, ("notify_add: path=[%s], private_data=%p\n", path,
228 private_data));
230 listel = talloc(notify, struct notify_list);
231 if (listel == NULL) {
232 return NT_STATUS_NO_MEMORY;
234 listel->callback = callback;
235 listel->private_data = private_data;
236 listel->path = talloc_strdup(listel, path);
237 if (listel->path == NULL) {
238 TALLOC_FREE(listel);
239 return NT_STATUS_NO_MEMORY;
241 DLIST_ADD(notify->list, listel);
243 ZERO_STRUCT(e);
244 e.filter = filter;
245 e.subdir_filter = subdir_filter;
246 e.server = messaging_server_id(notify->msg);
247 e.private_data = private_data;
249 key = string_tdb_data(path);
251 notify_rec = dbwrap_fetch_locked(notify->db_notify,
252 talloc_tos(), key);
253 if (notify_rec == NULL) {
254 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
255 goto fail;
259 * Make a copy of the notify_rec for easy restore in case
260 * updating the index_rec fails;
262 notify_copy = dbwrap_record_get_value(notify_rec);
263 if (notify_copy.dsize != 0) {
264 notify_copy.dptr = (uint8_t *)talloc_memdup(
265 notify_rec, notify_copy.dptr,
266 notify_copy.dsize);
267 if (notify_copy.dptr == NULL) {
268 TALLOC_FREE(notify_rec);
269 status = NT_STATUS_NO_MEMORY;
270 goto fail;
274 if (DEBUGLEVEL >= 10) {
275 NDR_PRINT_DEBUG(notify_db_entry, &e);
278 status = notify_add_entry(notify_rec, &e, &add_idx);
279 if (!NT_STATUS_IS_OK(status)) {
280 goto fail;
282 if (!add_idx) {
284 * Someone else has added the idx entry already
286 TALLOC_FREE(notify_rec);
287 return NT_STATUS_OK;
290 idx_rec = dbwrap_fetch_locked(notify->db_index,
291 talloc_tos(), key);
292 if (idx_rec == NULL) {
293 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
294 goto restore_notify;
296 status = notify_add_idx(idx_rec, get_my_vnn());
297 if (!NT_STATUS_IS_OK(status)) {
298 goto restore_notify;
301 TALLOC_FREE(idx_rec);
302 TALLOC_FREE(notify_rec);
303 return NT_STATUS_OK;
305 restore_notify:
306 if (notify_copy.dsize != 0) {
307 dbwrap_record_store(notify_rec, notify_copy, 0);
308 } else {
309 dbwrap_record_delete(notify_rec);
311 TALLOC_FREE(notify_rec);
312 fail:
313 DLIST_REMOVE(notify->list, listel);
314 TALLOC_FREE(listel);
315 return status;
318 static NTSTATUS notify_add_entry(struct db_record *rec,
319 const struct notify_db_entry *e,
320 bool *p_add_idx)
322 TDB_DATA value = dbwrap_record_get_value(rec);
323 struct notify_db_entry *entries;
324 size_t num_entries;
325 bool add_idx = true;
326 NTSTATUS status;
328 if (value.dsize == sizeof(time_t)) {
329 DEBUG(10, ("Re-using deleted entry\n"));
330 value.dsize = 0;
331 add_idx = false;
334 if ((value.dsize % sizeof(struct notify_db_entry)) != 0) {
335 DEBUG(1, ("Invalid value.dsize = %u\n",
336 (unsigned)value.dsize));
337 return NT_STATUS_INTERNAL_DB_CORRUPTION;
339 num_entries = value.dsize / sizeof(struct notify_db_entry);
341 if (num_entries != 0) {
342 add_idx = false;
345 entries = talloc_array(rec, struct notify_db_entry, num_entries + 1);
346 if (entries == NULL) {
347 return NT_STATUS_NO_MEMORY;
349 memcpy(entries, value.dptr, value.dsize);
351 entries[num_entries] = *e;
352 value = make_tdb_data((uint8_t *)entries, talloc_get_size(entries));
353 status = dbwrap_record_store(rec, value, 0);
354 TALLOC_FREE(entries);
355 if (!NT_STATUS_IS_OK(status)) {
356 return status;
358 *p_add_idx = add_idx;
359 return NT_STATUS_OK;
362 static NTSTATUS notify_add_idx(struct db_record *rec, uint32_t vnn)
364 TDB_DATA value = dbwrap_record_get_value(rec);
365 uint32_t *vnns;
366 size_t i, num_vnns;
367 NTSTATUS status;
369 if ((value.dsize % sizeof(uint32_t)) != 0) {
370 DEBUG(1, ("Invalid value.dsize = %u\n",
371 (unsigned)value.dsize));
372 return NT_STATUS_INTERNAL_DB_CORRUPTION;
374 num_vnns = value.dsize / sizeof(uint32_t);
375 vnns = (uint32_t *)value.dptr;
377 for (i=0; i<num_vnns; i++) {
378 if (vnns[i] == vnn) {
379 return NT_STATUS_OK;
381 if (vnns[i] > vnn) {
382 break;
386 value.dptr = (uint8_t *)talloc_realloc(
387 rec, value.dptr, uint32_t, num_vnns + 1);
388 if (value.dptr == NULL) {
389 return NT_STATUS_NO_MEMORY;
391 value.dsize = talloc_get_size(value.dptr);
393 vnns = (uint32_t *)value.dptr;
395 memmove(&vnns[i+1], &vnns[i], sizeof(uint32_t) * (num_vnns - i));
396 vnns[i] = vnn;
398 status = dbwrap_record_store(rec, value, 0);
399 if (!NT_STATUS_IS_OK(status)) {
400 return status;
402 return NT_STATUS_OK;
405 NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
407 struct server_id pid;
408 struct notify_list *listel;
409 struct db_record *notify_rec;
410 NTSTATUS status;
412 if ((notify == NULL) || (notify->msg == NULL)) {
413 return NT_STATUS_NOT_IMPLEMENTED;
416 DEBUG(10, ("notify_remove: private_data=%p\n", private_data));
418 pid = messaging_server_id(notify->msg);
420 for (listel=notify->list;listel;listel=listel->next) {
421 if (listel->private_data == private_data) {
422 DLIST_REMOVE(notify->list, listel);
423 break;
426 if (listel == NULL) {
427 DEBUG(10, ("%p not found\n", private_data));
428 return NT_STATUS_NOT_FOUND;
430 notify_rec = dbwrap_fetch_locked(notify->db_notify, talloc_tos(),
431 string_tdb_data(listel->path));
432 TALLOC_FREE(listel);
433 if (notify_rec == NULL) {
434 return NT_STATUS_INTERNAL_DB_CORRUPTION;
436 status = notify_del_entry(notify_rec, &pid, private_data);
437 DEBUG(10, ("del_entry returned %s\n", nt_errstr(status)));
438 TALLOC_FREE(notify_rec);
439 return status;
442 static NTSTATUS notify_del_entry(struct db_record *rec,
443 const struct server_id *pid,
444 void *private_data)
446 TDB_DATA value = dbwrap_record_get_value(rec);
447 struct server_id_buf tmp;
448 struct notify_db_entry *entries;
449 size_t i, num_entries;
450 time_t now;
452 DEBUG(10, ("del_entry called for %s %p\n",
453 server_id_str_buf(*pid, &tmp), private_data));
455 if ((value.dsize % sizeof(struct notify_db_entry)) != 0) {
456 DEBUG(1, ("Invalid value.dsize = %u\n",
457 (unsigned)value.dsize));
458 return NT_STATUS_INTERNAL_DB_CORRUPTION;
460 num_entries = value.dsize / sizeof(struct notify_db_entry);
461 entries = (struct notify_db_entry *)value.dptr;
463 for (i=0; i<num_entries; i++) {
464 struct notify_db_entry *e = &entries[i];
466 if (DEBUGLEVEL >= 10) {
467 NDR_PRINT_DEBUG(notify_db_entry, e);
470 if (e->private_data != private_data) {
471 continue;
473 if (serverid_equal(&e->server, pid)) {
474 break;
477 if (i == num_entries) {
478 return NT_STATUS_NOT_FOUND;
480 entries[i] = entries[num_entries-1];
481 value.dsize -= sizeof(struct notify_db_entry);
483 if (value.dsize == 0) {
484 now = time(NULL);
485 value.dptr = (uint8_t *)&now;
486 value.dsize = sizeof(now);
488 return dbwrap_record_store(rec, value, 0);
491 struct notify_trigger_index_state {
492 TALLOC_CTX *mem_ctx;
493 uint32_t *vnns;
494 uint32_t my_vnn;
495 bool found_my_vnn;
498 static void notify_trigger_index_parser(TDB_DATA key, TDB_DATA data,
499 void *private_data)
501 struct notify_trigger_index_state *state =
502 (struct notify_trigger_index_state *)private_data;
503 uint32_t *new_vnns;
504 size_t i, num_vnns, num_new_vnns, num_remote_vnns;
506 if ((data.dsize % sizeof(uint32_t)) != 0) {
507 DEBUG(1, ("Invalid record size in notify index db: %u\n",
508 (unsigned)data.dsize));
509 return;
511 new_vnns = (uint32_t *)data.dptr;
512 num_new_vnns = data.dsize / sizeof(uint32_t);
513 num_remote_vnns = num_new_vnns;
515 for (i=0; i<num_new_vnns; i++) {
516 if (new_vnns[i] == state->my_vnn) {
517 state->found_my_vnn = true;
518 num_remote_vnns -= 1;
521 if (num_remote_vnns == 0) {
522 return;
525 num_vnns = talloc_array_length(state->vnns);
526 state->vnns = talloc_realloc(state->mem_ctx, state->vnns, uint32_t,
527 num_vnns + num_remote_vnns);
528 if (state->vnns == NULL) {
529 DEBUG(1, ("talloc_realloc failed\n"));
530 return;
533 for (i=0; i<num_new_vnns; i++) {
534 if (new_vnns[i] != state->my_vnn) {
535 state->vnns[num_vnns] = new_vnns[i];
536 num_vnns += 1;
541 static int vnn_cmp(const void *p1, const void *p2)
543 const uint32_t *vnn1 = (const uint32_t *)p1;
544 const uint32_t *vnn2 = (const uint32_t *)p2;
546 if (*vnn1 < *vnn2) {
547 return -1;
549 if (*vnn1 == *vnn2) {
550 return 0;
552 return 1;
555 static bool notify_push_remote_blob(TALLOC_CTX *mem_ctx, uint32_t action,
556 uint32_t filter, const char *path,
557 uint8_t **pblob, size_t *pblob_len)
559 struct notify_remote_event ev;
560 DATA_BLOB data;
561 enum ndr_err_code ndr_err;
563 ev.action = action;
564 ev.filter = filter;
565 ev.path = path;
567 if (DEBUGLEVEL >= 10) {
568 NDR_PRINT_DEBUG(notify_remote_event, &ev);
571 ndr_err = ndr_push_struct_blob(
572 &data, mem_ctx, &ev,
573 (ndr_push_flags_fn_t)ndr_push_notify_remote_event);
574 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
575 return false;
577 *pblob = data.data;
578 *pblob_len = data.length;
579 return true;
582 static bool notify_pull_remote_blob(TALLOC_CTX *mem_ctx,
583 const uint8_t *blob, size_t blob_len,
584 uint32_t *paction, uint32_t *pfilter,
585 char **path)
587 struct notify_remote_event *ev;
588 enum ndr_err_code ndr_err;
589 DATA_BLOB data;
590 char *p;
592 data.data = discard_const_p(uint8_t, blob);
593 data.length = blob_len;
595 ev = talloc(mem_ctx, struct notify_remote_event);
596 if (ev == NULL) {
597 return false;
600 ndr_err = ndr_pull_struct_blob(
601 &data, ev, ev,
602 (ndr_pull_flags_fn_t)ndr_pull_notify_remote_event);
603 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
604 TALLOC_FREE(ev);
605 return false;
607 if (DEBUGLEVEL >= 10) {
608 NDR_PRINT_DEBUG(notify_remote_event, ev);
610 *paction = ev->action;
611 *pfilter = ev->filter;
612 p = discard_const_p(char, ev->path);
613 *path = talloc_move(mem_ctx, &p);
615 TALLOC_FREE(ev);
616 return true;
619 void notify_trigger(struct notify_context *notify,
620 uint32_t action, uint32_t filter,
621 const char *dir, const char *name)
623 struct ctdbd_connection *ctdbd_conn;
624 struct notify_trigger_index_state idx_state;
625 const char *p, *next_p;
626 size_t i, num_vnns;
627 uint32_t last_vnn;
628 uint8_t *remote_blob = NULL;
629 size_t remote_blob_len = 0;
630 char *path, *to_free;
631 char tmpbuf[PATH_MAX];
632 ssize_t len;
634 DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
635 "dir=%s, name=%s\n", (unsigned)action, (unsigned)filter,
636 dir, name));
638 /* see if change notify is enabled at all */
639 if (notify == NULL) {
640 return;
643 if (dir[0] != '/') {
645 * The rest of this routine assumes an absolute path.
647 return;
650 len = full_path_tos(dir, name, tmpbuf, sizeof(tmpbuf),
651 &path, &to_free);
652 if (len == -1) {
653 DEBUG(1, ("full_path_tos failed\n"));
654 return;
657 idx_state.mem_ctx = talloc_tos();
658 idx_state.vnns = NULL;
659 idx_state.found_my_vnn = false;
660 idx_state.my_vnn = get_my_vnn();
662 for (p = strchr(path+1, '/'); p != NULL; p = next_p) {
663 ptrdiff_t path_len = p - path;
664 bool recursive;
666 next_p = strchr(p+1, '/');
667 recursive = (next_p != NULL);
669 dbwrap_parse_record(
670 notify->db_index,
671 make_tdb_data(discard_const_p(uint8_t, path), path_len),
672 notify_trigger_index_parser, &idx_state);
674 if (idx_state.found_my_vnn) {
675 notify_trigger_local(notify, action, filter,
676 path, path_len, recursive);
677 idx_state.found_my_vnn = false;
681 if (idx_state.vnns == NULL) {
682 goto done;
685 ctdbd_conn = messaging_ctdbd_connection();
686 if (ctdbd_conn == NULL) {
687 goto done;
690 num_vnns = talloc_array_length(idx_state.vnns);
691 qsort(idx_state.vnns, num_vnns, sizeof(uint32_t), vnn_cmp);
693 last_vnn = 0xffffffff;
695 if (!notify_push_remote_blob(talloc_tos(), action, filter, path,
696 &remote_blob, &remote_blob_len)) {
697 DEBUG(1, ("notify_push_remote_blob failed\n"));
698 goto done;
701 for (i=0; i<num_vnns; i++) {
702 uint32_t vnn = idx_state.vnns[i];
703 NTSTATUS status;
705 if (vnn == last_vnn) {
706 continue;
709 status = ctdbd_messaging_send_blob(
710 ctdbd_conn, vnn, CTDB_SRVID_SAMBA_NOTIFY_PROXY,
711 remote_blob, remote_blob_len);
712 if (!NT_STATUS_IS_OK(status)) {
713 DEBUG(10, ("ctdbd_messaging_send_blob to vnn %d "
714 "returned %s, ignoring\n", (int)vnn,
715 nt_errstr(status)));
718 last_vnn = vnn;
721 done:
722 TALLOC_FREE(remote_blob);
723 TALLOC_FREE(idx_state.vnns);
724 TALLOC_FREE(to_free);
727 static void notify_trigger_local(struct notify_context *notify,
728 uint32_t action, uint32_t filter,
729 const char *path, size_t path_len,
730 bool recursive)
732 TDB_DATA data;
733 struct notify_db_entry *entries;
734 size_t i, num_entries;
735 NTSTATUS status;
737 DEBUG(10, ("notify_trigger_local called for %*s, path_len=%d, "
738 "filter=%d\n", (int)path_len, path, (int)path_len,
739 (int)filter));
741 status = dbwrap_fetch(
742 notify->db_notify, talloc_tos(),
743 make_tdb_data(discard_const_p(uint8_t, path), path_len), &data);
744 if (!NT_STATUS_IS_OK(status)) {
745 DEBUG(10, ("dbwrap_fetch returned %s\n",
746 nt_errstr(status)));
747 return;
749 if (data.dsize == sizeof(time_t)) {
750 DEBUG(10, ("Got deleted record\n"));
751 goto done;
753 if ((data.dsize % sizeof(struct notify_db_entry)) != 0) {
754 DEBUG(1, ("Invalid data.dsize = %u\n",
755 (unsigned)data.dsize));
756 goto done;
759 entries = (struct notify_db_entry *)data.dptr;
760 num_entries = data.dsize / sizeof(struct notify_db_entry);
762 DEBUG(10, ("recursive = %s pathlen=%d (%c)\n",
763 recursive ? "true" : "false", (int)path_len,
764 path[path_len]));
766 for (i=0; i<num_entries; i++) {
767 struct notify_db_entry *e = &entries[i];
768 uint32_t e_filter;
770 if (DEBUGLEVEL >= 10) {
771 NDR_PRINT_DEBUG(notify_db_entry, e);
774 e_filter = recursive ? e->subdir_filter : e->filter;
776 if ((filter & e_filter) == 0) {
777 continue;
780 if (!procid_is_local(&e->server)) {
781 struct server_id_buf tmp;
782 DEBUG(1, ("internal error: Non-local pid %s in "
783 "notify.tdb\n",
784 server_id_str_buf(e->server, &tmp)));
785 continue;
788 status = notify_send(notify, &e->server, path + path_len + 1,
789 action, e->private_data);
790 if (!NT_STATUS_IS_OK(status)) {
791 DEBUG(10, ("notify_send returned %s\n",
792 nt_errstr(status)));
796 done:
797 TALLOC_FREE(data.dptr);
800 struct notify_msg {
801 struct timespec when;
802 void *private_data;
803 uint32_t action;
804 char path[1];
807 static NTSTATUS notify_send(struct notify_context *notify,
808 struct server_id *pid,
809 const char *path, uint32_t action,
810 void *private_data)
812 struct notify_msg m = {};
813 struct iovec iov[2];
815 m.when = timespec_current();
816 m.private_data = private_data;
817 m.action = action;
819 iov[0].iov_base = &m;
820 iov[0].iov_len = offsetof(struct notify_msg, path);
821 iov[1].iov_base = discard_const_p(char, path);
822 iov[1].iov_len = strlen(path)+1;
824 return messaging_send_iov(notify->msg, *pid, MSG_PVFS_NOTIFY,
825 iov, ARRAY_SIZE(iov), NULL, 0);
828 static void notify_handler(struct messaging_context *msg_ctx,
829 void *private_data, uint32_t msg_type,
830 struct server_id server_id, DATA_BLOB *data)
832 struct notify_context *notify = talloc_get_type_abort(
833 private_data, struct notify_context);
834 struct notify_msg *m;
835 struct notify_event e;
836 struct notify_list *listel;
838 if (data->length == 0) {
839 DEBUG(1, ("%s: Got 0-sized MSG_PVFS_NOTIFY msg\n", __func__));
840 return;
842 if (data->data[data->length-1] != 0) {
843 DEBUG(1, ("%s: MSG_PVFS_NOTIFY path not 0-terminated\n",
844 __func__));
845 return;
848 m = (struct notify_msg *)data->data;
850 e = (struct notify_event) {
851 .action = m->action,
852 .path = m->path,
853 .private_data = m->private_data,
854 .dir = discard_const_p(char, "")
857 for (listel=notify->list;listel;listel=listel->next) {
858 if (listel->private_data == m->private_data) {
859 listel->callback(listel->private_data, m->when, &e);
860 break;
865 struct notify_walk_idx_state {
866 void (*fn)(const char *path,
867 uint32_t *vnns, size_t num_vnns,
868 void *private_data);
869 void *private_data;
872 static int notify_walk_idx_fn(struct db_record *rec, void *private_data)
874 struct notify_walk_idx_state *state =
875 (struct notify_walk_idx_state *)private_data;
876 TDB_DATA key, value;
877 char *path;
879 key = dbwrap_record_get_key(rec);
880 value = dbwrap_record_get_value(rec);
882 if ((value.dsize % sizeof(uint32_t)) != 0) {
883 DEBUG(1, ("invalid value size in notify index db: %u\n",
884 (unsigned)(value.dsize)));
885 return 0;
888 path = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
889 if (path == NULL) {
890 DEBUG(1, ("talloc_strndup failed\n"));
891 return 0;
893 state->fn(path, (uint32_t *)value.dptr, value.dsize/sizeof(uint32_t),
894 state->private_data);
895 TALLOC_FREE(path);
896 return 0;
899 void notify_walk_idx(struct notify_context *notify,
900 void (*fn)(const char *path,
901 uint32_t *vnns, size_t num_vnns,
902 void *private_data),
903 void *private_data)
905 struct notify_walk_idx_state state;
906 state.fn = fn;
907 state.private_data = private_data;
908 dbwrap_traverse_read(notify->db_index, notify_walk_idx_fn, &state,
909 NULL);
912 struct notify_walk_state {
913 void (*fn)(const char *path,
914 struct notify_db_entry *entries, size_t num_entries,
915 time_t deleted_time, void *private_data);
916 void *private_data;
919 static int notify_walk_fn(struct db_record *rec, void *private_data)
921 struct notify_walk_state *state =
922 (struct notify_walk_state *)private_data;
923 TDB_DATA key, value;
924 struct notify_db_entry *entries;
925 size_t num_entries;
926 time_t deleted_time;
927 char *path;
929 key = dbwrap_record_get_key(rec);
930 value = dbwrap_record_get_value(rec);
932 if (value.dsize == sizeof(deleted_time)) {
933 memcpy(&deleted_time, value.dptr, sizeof(deleted_time));
934 entries = NULL;
935 num_entries = 0;
936 } else {
937 if ((value.dsize % sizeof(struct notify_db_entry)) != 0) {
938 DEBUG(1, ("invalid value size in notify db: %u\n",
939 (unsigned)(value.dsize)));
940 return 0;
942 entries = (struct notify_db_entry *)value.dptr;
943 num_entries = value.dsize / sizeof(struct notify_db_entry);
944 deleted_time = 0;
947 path = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
948 if (path == NULL) {
949 DEBUG(1, ("talloc_strndup failed\n"));
950 return 0;
952 state->fn(path, entries, num_entries, deleted_time,
953 state->private_data);
954 TALLOC_FREE(path);
955 return 0;
958 void notify_walk(struct notify_context *notify,
959 void (*fn)(const char *path,
960 struct notify_db_entry *entries,
961 size_t num_entries,
962 time_t deleted_time, void *private_data),
963 void *private_data)
965 struct notify_walk_state state;
966 state.fn = fn;
967 state.private_data = private_data;
968 dbwrap_traverse_read(notify->db_notify, notify_walk_fn, &state,
969 NULL);
972 struct notify_cleanup_state {
973 TALLOC_CTX *mem_ctx;
974 time_t delete_before;
975 ssize_t array_size;
976 uint32_t num_paths;
977 char **paths;
980 static void notify_cleanup_collect(
981 const char *path, struct notify_db_entry *entries, size_t num_entries,
982 time_t deleted_time, void *private_data)
984 struct notify_cleanup_state *state =
985 (struct notify_cleanup_state *)private_data;
986 char *p;
988 if (num_entries != 0) {
989 return;
991 if (deleted_time >= state->delete_before) {
992 return;
995 p = talloc_strdup(state->mem_ctx, path);
996 if (p == NULL) {
997 DEBUG(1, ("talloc_strdup failed\n"));
998 return;
1000 add_to_large_array(state->mem_ctx, sizeof(p), (void *)&p,
1001 &state->paths, &state->num_paths,
1002 &state->array_size);
1003 if (state->array_size == -1) {
1004 TALLOC_FREE(p);
1008 static bool notify_cleanup_path(struct notify_context *notify,
1009 const char *path, time_t delete_before);
1011 void notify_cleanup(struct notify_context *notify)
1013 struct notify_cleanup_state state;
1014 uint32_t failure_pool;
1016 ZERO_STRUCT(state);
1017 state.mem_ctx = talloc_stackframe();
1019 state.delete_before = time(NULL)
1020 - lp_parm_int(-1, "smbd", "notify cleanup interval", 60);
1022 notify_walk(notify, notify_cleanup_collect, &state);
1024 failure_pool = state.num_paths;
1026 while (state.num_paths != 0) {
1027 size_t idx;
1030 * This loop is designed to be as kind as possible to
1031 * ctdb. ctdb does not like it if many smbds hammer on a
1032 * single record. If on many nodes the cleanup process starts
1033 * running, it can happen that all of them need to clean up
1034 * records in the same order. This would generate a ctdb
1035 * migrate storm on these records. Randomizing the load across
1036 * multiple records reduces the load on the individual record.
1039 generate_random_buffer((uint8_t *)&idx, sizeof(idx));
1040 idx = idx % state.num_paths;
1042 if (!notify_cleanup_path(notify, state.paths[idx],
1043 state.delete_before)) {
1045 * notify_cleanup_path failed, the most likely reason
1046 * is that dbwrap_try_fetch_locked failed due to
1047 * contention. We allow one failed attempt per deleted
1048 * path on average before we give up.
1050 failure_pool -= 1;
1051 if (failure_pool == 0) {
1053 * Too many failures. We will come back here,
1054 * maybe next time there is less contention.
1056 break;
1060 TALLOC_FREE(state.paths[idx]);
1061 state.paths[idx] = state.paths[state.num_paths-1];
1062 state.num_paths -= 1;
1064 TALLOC_FREE(state.mem_ctx);
1067 static bool notify_cleanup_path(struct notify_context *notify,
1068 const char *path, time_t delete_before)
1070 struct db_record *notify_rec = NULL;
1071 struct db_record *idx_rec = NULL;
1072 TDB_DATA key = string_tdb_data(path);
1073 TDB_DATA value;
1074 time_t deleted;
1075 NTSTATUS status;
1077 notify_rec = dbwrap_fetch_locked(notify->db_notify, talloc_tos(), key);
1078 if (notify_rec == NULL) {
1079 DEBUG(10, ("Could not fetch notify_rec\n"));
1080 return false;
1082 value = dbwrap_record_get_value(notify_rec);
1084 if (value.dsize != sizeof(deleted)) {
1085 DEBUG(10, ("record %s has been re-used\n", path));
1086 goto done;
1088 memcpy(&deleted, value.dptr, sizeof(deleted));
1090 if (deleted >= delete_before) {
1091 DEBUG(10, ("record %s too young\n", path));
1092 goto done;
1096 * Be kind to ctdb and only try one dmaster migration at most.
1098 idx_rec = dbwrap_try_fetch_locked(notify->db_index, talloc_tos(), key);
1099 if (idx_rec == NULL) {
1100 DEBUG(10, ("Could not fetch idx_rec\n"));
1101 goto done;
1104 status = dbwrap_record_delete(notify_rec);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 DEBUG(10, ("Could not delete notify_rec: %s\n",
1107 nt_errstr(status)));
1110 status = notify_del_idx(idx_rec, get_my_vnn());
1111 if (!NT_STATUS_IS_OK(status)) {
1112 DEBUG(10, ("Could not delete idx_rec: %s\n",
1113 nt_errstr(status)));
1116 done:
1117 TALLOC_FREE(idx_rec);
1118 TALLOC_FREE(notify_rec);
1119 return true;
1122 static NTSTATUS notify_del_idx(struct db_record *rec, uint32_t vnn)
1124 TDB_DATA value = dbwrap_record_get_value(rec);
1125 uint32_t *vnns;
1126 size_t i, num_vnns;
1128 if ((value.dsize % sizeof(uint32_t)) != 0) {
1129 DEBUG(1, ("Invalid value.dsize = %u\n",
1130 (unsigned)value.dsize));
1131 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1133 num_vnns = value.dsize / sizeof(uint32_t);
1134 vnns = (uint32_t *)value.dptr;
1136 for (i=0; i<num_vnns; i++) {
1137 if (vnns[i] == vnn) {
1138 break;
1142 if (i == num_vnns) {
1144 * Not found. Should not happen, but okay...
1146 return NT_STATUS_OK;
1149 memmove(&vnns[i], &vnns[i+1], sizeof(uint32_t) * (num_vnns - i - 1));
1150 value.dsize -= sizeof(uint32_t);
1152 if (value.dsize == 0) {
1153 return dbwrap_record_delete(rec);
1155 return dbwrap_record_store(rec, value, 0);
1158 struct notify_cluster_proxy_state {
1159 struct tevent_context *ev;
1160 struct notify_context *notify;
1161 struct ctdb_msg_channel *chan;
1164 static void notify_cluster_proxy_got_chan(struct tevent_req *subreq);
1165 static void notify_cluster_proxy_got_msg(struct tevent_req *subreq);
1166 static void notify_cluster_proxy_trigger(struct notify_context *notify,
1167 uint32_t action, uint32_t filter,
1168 char *path);
1170 struct tevent_req *notify_cluster_proxy_send(
1171 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1172 struct notify_context *notify)
1174 struct tevent_req *req, *subreq;
1175 struct notify_cluster_proxy_state *state;
1177 req = tevent_req_create(mem_ctx, &state,
1178 struct notify_cluster_proxy_state);
1179 if (req == NULL) {
1180 return NULL;
1182 state->ev = ev;
1183 state->notify = notify;
1185 subreq = ctdb_msg_channel_init_send(
1186 state, state->ev, lp_ctdbd_socket(),
1187 CTDB_SRVID_SAMBA_NOTIFY_PROXY);
1188 if (tevent_req_nomem(subreq, req)) {
1189 return tevent_req_post(req, ev);
1191 tevent_req_set_callback(subreq, notify_cluster_proxy_got_chan, req);
1192 return req;
1195 static void notify_cluster_proxy_got_chan(struct tevent_req *subreq)
1197 struct tevent_req *req = tevent_req_callback_data(
1198 subreq, struct tevent_req);
1199 struct notify_cluster_proxy_state *state = tevent_req_data(
1200 req, struct notify_cluster_proxy_state);
1201 int ret;
1203 ret = ctdb_msg_channel_init_recv(subreq, state, &state->chan);
1204 TALLOC_FREE(subreq);
1205 if (ret != 0) {
1206 tevent_req_error(req, ret);
1207 return;
1209 subreq = ctdb_msg_read_send(state, state->ev, state->chan);
1210 if (tevent_req_nomem(subreq, req)) {
1211 return;
1213 tevent_req_set_callback(subreq, notify_cluster_proxy_got_msg, req);
1216 static void notify_cluster_proxy_got_msg(struct tevent_req *subreq)
1218 struct tevent_req *req = tevent_req_callback_data(
1219 subreq, struct tevent_req);
1220 struct notify_cluster_proxy_state *state = tevent_req_data(
1221 req, struct notify_cluster_proxy_state);
1222 uint8_t *msg;
1223 size_t msg_len;
1224 uint32_t action, filter;
1225 char *path;
1226 int ret;
1227 bool res;
1229 ret = ctdb_msg_read_recv(subreq, talloc_tos(), &msg, &msg_len);
1230 TALLOC_FREE(subreq);
1231 if (ret != 0) {
1232 tevent_req_error(req, ret);
1233 return;
1236 res = notify_pull_remote_blob(talloc_tos(), msg, msg_len,
1237 &action, &filter, &path);
1238 TALLOC_FREE(msg);
1239 if (!res) {
1240 tevent_req_error(req, EIO);
1241 return;
1243 notify_cluster_proxy_trigger(state->notify, action, filter, path);
1244 TALLOC_FREE(path);
1246 subreq = ctdb_msg_read_send(state, state->ev, state->chan);
1247 if (tevent_req_nomem(subreq, req)) {
1248 return;
1250 tevent_req_set_callback(subreq, notify_cluster_proxy_got_msg, req);
1253 static void notify_cluster_proxy_trigger(struct notify_context *notify,
1254 uint32_t action, uint32_t filter,
1255 char *path)
1257 const char *p, *next_p;
1259 for (p = path; p != NULL; p = next_p) {
1260 ptrdiff_t path_len = p - path;
1261 bool recursive;
1263 next_p = strchr(p+1, '/');
1264 recursive = (next_p != NULL);
1266 notify_trigger_local(notify, action, filter,
1267 path, path_len, recursive);
1271 int notify_cluster_proxy_recv(struct tevent_req *req)
1273 return tevent_req_simple_recv_unix(req);