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.
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"
35 #include "lib/tdb_wrap/tdb_wrap.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"
45 struct notify_list *next, *prev;
47 void (*callback)(void *, struct timespec, const struct notify_event *);
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
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
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
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,
102 static NTSTATUS notify_send(struct notify_context *notify,
103 struct server_id *pid,
104 const char *path, uint32_t action,
106 static NTSTATUS notify_add_entry(struct db_record *rec,
107 const struct notify_db_entry *e,
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,
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;
129 notify = talloc(mem_ctx, struct notify_context);
130 if (notify == NULL) {
136 lp_ctx = loadparm_init_s3(notify, loadparm_s3_helpers());
137 notify->db_notify = db_open_tdb(
138 notify, lp_ctx, lock_path("notify.tdb"),
139 0, TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
140 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
141 talloc_unlink(notify, lp_ctx);
142 if (notify->db_notify == NULL) {
145 notify->db_index = db_open(
146 notify, lock_path("notify_index.tdb"),
147 0, TDB_SEQNUM|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
148 O_RDWR|O_CREAT, 0644, DBWRAP_LOCK_ORDER_3, DBWRAP_FLAG_NONE);
149 if (notify->db_index == NULL) {
152 if (!lp_clustering()) {
153 notify->db_index = db_open_cache(notify, notify->db_index);
154 if (notify->db_index == NULL) {
159 if (notify->msg != NULL) {
162 status = messaging_register(notify->msg, notify,
163 MSG_PVFS_NOTIFY, notify_handler);
164 if (!NT_STATUS_IS_OK(status)) {
165 DEBUG(1, ("messaging_register returned %s\n",
171 talloc_set_destructor(notify, notify_context_destructor);
179 static int notify_context_destructor(struct notify_context *notify)
181 DEBUG(10, ("notify_context_destructor called\n"));
183 if (notify->msg != NULL) {
184 messaging_deregister(notify->msg, MSG_PVFS_NOTIFY, notify);
187 while (notify->list != NULL) {
188 DEBUG(10, ("Removing private_data=%p\n",
189 notify->list->private_data));
190 notify_remove(notify, notify->list->private_data);
195 NTSTATUS notify_add(struct notify_context *notify,
196 const char *path, uint32_t filter, uint32_t subdir_filter,
197 void (*callback)(void *, struct timespec,
198 const struct notify_event *),
201 struct notify_db_entry e;
202 struct notify_list *listel;
203 struct db_record *notify_rec, *idx_rec;
206 TDB_DATA key, notify_copy;
208 if (notify == NULL) {
209 return NT_STATUS_NOT_IMPLEMENTED;
212 DEBUG(10, ("notify_add: path=[%s], private_data=%p\n", path,
215 listel = talloc(notify, struct notify_list);
216 if (listel == NULL) {
217 return NT_STATUS_NO_MEMORY;
219 listel->callback = callback;
220 listel->private_data = private_data;
221 listel->path = talloc_strdup(listel, path);
222 if (listel->path == NULL) {
224 return NT_STATUS_NO_MEMORY;
226 DLIST_ADD(notify->list, listel);
230 e.subdir_filter = subdir_filter;
231 e.server = messaging_server_id(notify->msg);
232 e.private_data = private_data;
234 key = string_tdb_data(path);
236 notify_rec = dbwrap_fetch_locked(notify->db_notify,
238 if (notify_rec == NULL) {
239 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
244 * Make a copy of the notify_rec for easy restore in case
245 * updating the index_rec fails;
247 notify_copy = dbwrap_record_get_value(notify_rec);
248 if (notify_copy.dsize != 0) {
249 notify_copy.dptr = (uint8_t *)talloc_memdup(
250 notify_rec, notify_copy.dptr,
252 if (notify_copy.dptr == NULL) {
253 TALLOC_FREE(notify_rec);
254 status = NT_STATUS_NO_MEMORY;
259 if (DEBUGLEVEL >= 10) {
260 NDR_PRINT_DEBUG(notify_db_entry, &e);
263 status = notify_add_entry(notify_rec, &e, &add_idx);
264 if (!NT_STATUS_IS_OK(status)) {
269 * Someone else has added the idx entry already
271 TALLOC_FREE(notify_rec);
275 idx_rec = dbwrap_fetch_locked(notify->db_index,
277 if (idx_rec == NULL) {
278 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
281 status = notify_add_idx(idx_rec, get_my_vnn());
282 if (!NT_STATUS_IS_OK(status)) {
286 TALLOC_FREE(idx_rec);
287 TALLOC_FREE(notify_rec);
291 if (notify_copy.dsize != 0) {
292 dbwrap_record_store(notify_rec, notify_copy, 0);
294 dbwrap_record_delete(notify_rec);
296 TALLOC_FREE(notify_rec);
298 DLIST_REMOVE(notify->list, listel);
303 static NTSTATUS notify_add_entry(struct db_record *rec,
304 const struct notify_db_entry *e,
307 TDB_DATA value = dbwrap_record_get_value(rec);
308 struct notify_db_entry *entries;
313 if (value.dsize == sizeof(time_t)) {
314 DEBUG(10, ("Re-using deleted entry\n"));
319 if ((value.dsize % sizeof(struct notify_db_entry)) != 0) {
320 DEBUG(1, ("Invalid value.dsize = %u\n",
321 (unsigned)value.dsize));
322 return NT_STATUS_INTERNAL_DB_CORRUPTION;
324 num_entries = value.dsize / sizeof(struct notify_db_entry);
326 if (num_entries != 0) {
330 entries = talloc_array(rec, struct notify_db_entry, num_entries + 1);
331 if (entries == NULL) {
332 return NT_STATUS_NO_MEMORY;
334 memcpy(entries, value.dptr, value.dsize);
336 entries[num_entries] = *e;
337 value = make_tdb_data((uint8_t *)entries, talloc_get_size(entries));
338 status = dbwrap_record_store(rec, value, 0);
339 TALLOC_FREE(entries);
340 if (!NT_STATUS_IS_OK(status)) {
343 *p_add_idx = add_idx;
347 static NTSTATUS notify_add_idx(struct db_record *rec, uint32_t vnn)
349 TDB_DATA value = dbwrap_record_get_value(rec);
354 if ((value.dsize % sizeof(uint32_t)) != 0) {
355 DEBUG(1, ("Invalid value.dsize = %u\n",
356 (unsigned)value.dsize));
357 return NT_STATUS_INTERNAL_DB_CORRUPTION;
359 num_vnns = value.dsize / sizeof(uint32_t);
360 vnns = (uint32_t *)value.dptr;
362 for (i=0; i<num_vnns; i++) {
363 if (vnns[i] == vnn) {
371 value.dptr = (uint8_t *)talloc_realloc(
372 rec, value.dptr, uint32_t, num_vnns + 1);
373 if (value.dptr == NULL) {
374 return NT_STATUS_NO_MEMORY;
376 value.dsize = talloc_get_size(value.dptr);
378 vnns = (uint32_t *)value.dptr;
380 memmove(&vnns[i+1], &vnns[i], sizeof(uint32_t) * (num_vnns - i));
383 status = dbwrap_record_store(rec, value, 0);
384 if (!NT_STATUS_IS_OK(status)) {
390 NTSTATUS notify_remove(struct notify_context *notify, void *private_data)
392 struct server_id pid;
393 struct notify_list *listel;
394 struct db_record *notify_rec;
397 if ((notify == NULL) || (notify->msg == NULL)) {
398 return NT_STATUS_NOT_IMPLEMENTED;
401 DEBUG(10, ("notify_remove: private_data=%p\n", private_data));
403 pid = messaging_server_id(notify->msg);
405 for (listel=notify->list;listel;listel=listel->next) {
406 if (listel->private_data == private_data) {
407 DLIST_REMOVE(notify->list, listel);
411 if (listel == NULL) {
412 DEBUG(10, ("%p not found\n", private_data));
413 return NT_STATUS_NOT_FOUND;
415 notify_rec = dbwrap_fetch_locked(notify->db_notify, talloc_tos(),
416 string_tdb_data(listel->path));
418 if (notify_rec == NULL) {
419 return NT_STATUS_INTERNAL_DB_CORRUPTION;
421 status = notify_del_entry(notify_rec, &pid, private_data);
422 DEBUG(10, ("del_entry returned %s\n", nt_errstr(status)));
423 TALLOC_FREE(notify_rec);
427 static NTSTATUS notify_del_entry(struct db_record *rec,
428 const struct server_id *pid,
431 TDB_DATA value = dbwrap_record_get_value(rec);
432 struct notify_db_entry *entries;
433 size_t i, num_entries;
436 DEBUG(10, ("del_entry called for %s %p\n", procid_str_static(pid),
439 if ((value.dsize % sizeof(struct notify_db_entry)) != 0) {
440 DEBUG(1, ("Invalid value.dsize = %u\n",
441 (unsigned)value.dsize));
442 return NT_STATUS_INTERNAL_DB_CORRUPTION;
444 num_entries = value.dsize / sizeof(struct notify_db_entry);
445 entries = (struct notify_db_entry *)value.dptr;
447 for (i=0; i<num_entries; i++) {
448 struct notify_db_entry *e = &entries[i];
450 if (DEBUGLEVEL >= 10) {
451 NDR_PRINT_DEBUG(notify_db_entry, e);
454 if (e->private_data != private_data) {
457 if (serverid_equal(&e->server, pid)) {
461 if (i == num_entries) {
462 return NT_STATUS_NOT_FOUND;
464 entries[i] = entries[num_entries-1];
465 value.dsize -= sizeof(struct notify_db_entry);
467 if (value.dsize == 0) {
469 value.dptr = (uint8_t *)&now;
470 value.dsize = sizeof(now);
472 return dbwrap_record_store(rec, value, 0);
475 struct notify_trigger_index_state {
482 static void notify_trigger_index_parser(TDB_DATA key, TDB_DATA data,
485 struct notify_trigger_index_state *state =
486 (struct notify_trigger_index_state *)private_data;
488 size_t i, num_vnns, num_new_vnns, num_remote_vnns;
490 if ((data.dsize % sizeof(uint32_t)) != 0) {
491 DEBUG(1, ("Invalid record size in notify index db: %u\n",
492 (unsigned)data.dsize));
495 new_vnns = (uint32_t *)data.dptr;
496 num_new_vnns = data.dsize / sizeof(uint32_t);
497 num_remote_vnns = num_new_vnns;
499 for (i=0; i<num_new_vnns; i++) {
500 if (new_vnns[i] == state->my_vnn) {
501 state->found_my_vnn = true;
502 num_remote_vnns -= 1;
505 if (num_remote_vnns == 0) {
509 num_vnns = talloc_array_length(state->vnns);
510 state->vnns = talloc_realloc(state->mem_ctx, state->vnns, uint32_t,
511 num_vnns + num_remote_vnns);
512 if (state->vnns == NULL) {
513 DEBUG(1, ("talloc_realloc failed\n"));
517 for (i=0; i<num_new_vnns; i++) {
518 if (new_vnns[i] != state->my_vnn) {
519 state->vnns[num_vnns] = new_vnns[i];
525 static int vnn_cmp(const void *p1, const void *p2)
527 const uint32_t *vnn1 = (const uint32_t *)p1;
528 const uint32_t *vnn2 = (const uint32_t *)p2;
533 if (*vnn1 == *vnn2) {
539 static bool notify_push_remote_blob(TALLOC_CTX *mem_ctx, uint32_t action,
540 uint32_t filter, const char *path,
541 uint8_t **pblob, size_t *pblob_len)
543 struct notify_remote_event ev;
545 enum ndr_err_code ndr_err;
551 if (DEBUGLEVEL >= 10) {
552 NDR_PRINT_DEBUG(notify_remote_event, &ev);
555 ndr_err = ndr_push_struct_blob(
557 (ndr_push_flags_fn_t)ndr_push_notify_remote_event);
558 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
562 *pblob_len = data.length;
566 static bool notify_pull_remote_blob(TALLOC_CTX *mem_ctx,
567 const uint8_t *blob, size_t blob_len,
568 uint32_t *paction, uint32_t *pfilter,
571 struct notify_remote_event *ev;
572 enum ndr_err_code ndr_err;
576 data.data = discard_const_p(uint8_t, blob);
577 data.length = blob_len;
579 ev = talloc(mem_ctx, struct notify_remote_event);
584 ndr_err = ndr_pull_struct_blob(
586 (ndr_pull_flags_fn_t)ndr_pull_notify_remote_event);
587 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
591 if (DEBUGLEVEL >= 10) {
592 NDR_PRINT_DEBUG(notify_remote_event, ev);
594 *paction = ev->action;
595 *pfilter = ev->filter;
596 p = discard_const_p(char, ev->path);
597 *path = talloc_move(mem_ctx, &p);
603 void notify_trigger(struct notify_context *notify,
604 uint32_t action, uint32_t filter, const char *path)
606 struct ctdbd_connection *ctdbd_conn;
607 struct notify_trigger_index_state idx_state;
608 const char *p, *next_p;
611 uint8_t *remote_blob = NULL;
612 size_t remote_blob_len = 0;
614 DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
615 "path=%s\n", (unsigned)action, (unsigned)filter, path));
617 /* see if change notify is enabled at all */
618 if (notify == NULL) {
622 if (path[0] != '/') {
624 * The rest of this routine assumes an absolute path.
629 idx_state.mem_ctx = talloc_tos();
630 idx_state.vnns = NULL;
631 idx_state.found_my_vnn = false;
632 idx_state.my_vnn = get_my_vnn();
634 for (p = strchr(path+1, '/'); p != NULL; p = next_p) {
635 ptrdiff_t path_len = p - path;
638 next_p = strchr(p+1, '/');
639 recursive = (next_p != NULL);
643 make_tdb_data(discard_const_p(uint8_t, path), path_len),
644 notify_trigger_index_parser, &idx_state);
646 if (idx_state.found_my_vnn) {
647 notify_trigger_local(notify, action, filter,
648 path, path_len, recursive);
649 idx_state.found_my_vnn = false;
653 if (idx_state.vnns == NULL) {
657 ctdbd_conn = messaging_ctdbd_connection();
658 if (ctdbd_conn == NULL) {
662 num_vnns = talloc_array_length(idx_state.vnns);
663 qsort(idx_state.vnns, num_vnns, sizeof(uint32_t), vnn_cmp);
665 last_vnn = 0xffffffff;
667 if (!notify_push_remote_blob(talloc_tos(), action, filter, path,
668 &remote_blob, &remote_blob_len)) {
669 DEBUG(1, ("notify_push_remote_blob failed\n"));
673 for (i=0; i<num_vnns; i++) {
674 uint32_t vnn = idx_state.vnns[i];
677 if (vnn == last_vnn) {
681 status = ctdbd_messaging_send_blob(
682 ctdbd_conn, vnn, CTDB_SRVID_SAMBA_NOTIFY_PROXY,
683 remote_blob, remote_blob_len);
684 if (!NT_STATUS_IS_OK(status)) {
685 DEBUG(10, ("ctdbd_messaging_send_blob to vnn %d "
686 "returned %s, ignoring\n", (int)vnn,
694 TALLOC_FREE(remote_blob);
695 TALLOC_FREE(idx_state.vnns);
698 static void notify_trigger_local(struct notify_context *notify,
699 uint32_t action, uint32_t filter,
700 const char *path, size_t path_len,
704 struct notify_db_entry *entries;
705 size_t i, num_entries;
708 DEBUG(10, ("notify_trigger_local called for %*s, path_len=%d, "
709 "filter=%d\n", (int)path_len, path, (int)path_len,
712 status = dbwrap_fetch(
713 notify->db_notify, talloc_tos(),
714 make_tdb_data(discard_const_p(uint8_t, path), path_len), &data);
715 if (!NT_STATUS_IS_OK(status)) {
716 DEBUG(10, ("dbwrap_fetch returned %s\n",
720 if (data.dsize == sizeof(time_t)) {
721 DEBUG(10, ("Got deleted record\n"));
724 if ((data.dsize % sizeof(struct notify_db_entry)) != 0) {
725 DEBUG(1, ("Invalid data.dsize = %u\n",
726 (unsigned)data.dsize));
730 entries = (struct notify_db_entry *)data.dptr;
731 num_entries = data.dsize / sizeof(struct notify_db_entry);
733 DEBUG(10, ("recursive = %s pathlen=%d (%c)\n",
734 recursive ? "true" : "false", (int)path_len,
737 for (i=0; i<num_entries; i++) {
738 struct notify_db_entry *e = &entries[i];
741 if (DEBUGLEVEL >= 10) {
742 NDR_PRINT_DEBUG(notify_db_entry, e);
745 e_filter = recursive ? e->subdir_filter : e->filter;
747 if ((filter & e_filter) == 0) {
751 if (!procid_is_local(&e->server)) {
752 DEBUG(1, ("internal error: Non-local pid %s in "
754 procid_str_static(&e->server)));
758 status = notify_send(notify, &e->server, path + path_len + 1,
759 action, e->private_data);
760 if (!NT_STATUS_IS_OK(status)) {
761 DEBUG(10, ("notify_send returned %s\n",
767 TALLOC_FREE(data.dptr);
770 static NTSTATUS notify_send(struct notify_context *notify,
771 struct server_id *pid,
772 const char *path, uint32_t action,
775 struct notify_event ev;
778 enum ndr_err_code ndr_err;
782 ev.private_data = private_data;
784 ndr_err = ndr_push_struct_blob(
785 &data, talloc_tos(), &ev,
786 (ndr_push_flags_fn_t)ndr_push_notify_event);
787 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
788 return ndr_map_error2ntstatus(ndr_err);
790 status = messaging_send(notify->msg, *pid, MSG_PVFS_NOTIFY,
792 TALLOC_FREE(data.data);
796 static void notify_handler(struct messaging_context *msg_ctx,
797 void *private_data, uint32_t msg_type,
798 struct server_id server_id, DATA_BLOB *data)
800 struct notify_context *notify = talloc_get_type_abort(
801 private_data, struct notify_context);
802 enum ndr_err_code ndr_err;
803 struct notify_event *n;
804 struct notify_list *listel;
806 n = talloc(talloc_tos(), struct notify_event);
808 DEBUG(1, ("talloc failed\n"));
812 ndr_err = ndr_pull_struct_blob(
813 data, n, n, (ndr_pull_flags_fn_t)ndr_pull_notify_event);
814 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
818 if (DEBUGLEVEL >= 10) {
819 NDR_PRINT_DEBUG(notify_event, n);
822 for (listel=notify->list;listel;listel=listel->next) {
823 if (listel->private_data == n->private_data) {
824 listel->callback(listel->private_data,
825 timespec_current(), n);
832 struct notify_walk_idx_state {
833 void (*fn)(const char *path,
834 uint32_t *vnns, size_t num_vnns,
839 static int notify_walk_idx_fn(struct db_record *rec, void *private_data)
841 struct notify_walk_idx_state *state =
842 (struct notify_walk_idx_state *)private_data;
846 key = dbwrap_record_get_key(rec);
847 value = dbwrap_record_get_value(rec);
849 if ((value.dsize % sizeof(uint32_t)) != 0) {
850 DEBUG(1, ("invalid value size in notify index db: %u\n",
851 (unsigned)(value.dsize)));
855 path = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
857 DEBUG(1, ("talloc_strndup failed\n"));
860 state->fn(path, (uint32_t *)value.dptr, value.dsize/sizeof(uint32_t),
861 state->private_data);
866 void notify_walk_idx(struct notify_context *notify,
867 void (*fn)(const char *path,
868 uint32_t *vnns, size_t num_vnns,
872 struct notify_walk_idx_state state;
874 state.private_data = private_data;
875 dbwrap_traverse_read(notify->db_index, notify_walk_idx_fn, &state,
879 struct notify_walk_state {
880 void (*fn)(const char *path,
881 struct notify_db_entry *entries, size_t num_entries,
882 time_t deleted_time, void *private_data);
886 static int notify_walk_fn(struct db_record *rec, void *private_data)
888 struct notify_walk_state *state =
889 (struct notify_walk_state *)private_data;
891 struct notify_db_entry *entries;
896 key = dbwrap_record_get_key(rec);
897 value = dbwrap_record_get_value(rec);
899 if (value.dsize == sizeof(deleted_time)) {
900 memcpy(&deleted_time, value.dptr, sizeof(deleted_time));
904 if ((value.dsize % sizeof(struct notify_db_entry)) != 0) {
905 DEBUG(1, ("invalid value size in notify db: %u\n",
906 (unsigned)(value.dsize)));
909 entries = (struct notify_db_entry *)value.dptr;
910 num_entries = value.dsize / sizeof(struct notify_db_entry);
914 path = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
916 DEBUG(1, ("talloc_strndup failed\n"));
919 state->fn(path, entries, num_entries, deleted_time,
920 state->private_data);
925 void notify_walk(struct notify_context *notify,
926 void (*fn)(const char *path,
927 struct notify_db_entry *entries,
929 time_t deleted_time, void *private_data),
932 struct notify_walk_state state;
934 state.private_data = private_data;
935 dbwrap_traverse_read(notify->db_notify, notify_walk_fn, &state,
939 struct notify_cleanup_state {
941 time_t delete_before;
947 static void notify_cleanup_collect(
948 const char *path, struct notify_db_entry *entries, size_t num_entries,
949 time_t deleted_time, void *private_data)
951 struct notify_cleanup_state *state =
952 (struct notify_cleanup_state *)private_data;
955 if (num_entries != 0) {
958 if (deleted_time >= state->delete_before) {
962 p = talloc_strdup(state->mem_ctx, path);
964 DEBUG(1, ("talloc_strdup failed\n"));
967 add_to_large_array(state->mem_ctx, sizeof(p), (void *)&p,
968 &state->paths, &state->num_paths,
970 if (state->array_size == -1) {
975 static bool notify_cleanup_path(struct notify_context *notify,
976 const char *path, time_t delete_before);
978 void notify_cleanup(struct notify_context *notify)
980 struct notify_cleanup_state state;
981 uint32_t failure_pool;
984 state.mem_ctx = talloc_stackframe();
986 state.delete_before = time(NULL)
987 - lp_parm_int(-1, "smbd", "notify cleanup interval", 60);
989 notify_walk(notify, notify_cleanup_collect, &state);
991 failure_pool = state.num_paths;
993 while (state.num_paths != 0) {
997 * This loop is designed to be as kind as possible to
998 * ctdb. ctdb does not like it if many smbds hammer on a
999 * single record. If on many nodes the cleanup process starts
1000 * running, it can happen that all of them need to clean up
1001 * records in the same order. This would generate a ctdb
1002 * migrate storm on these records. Randomizing the load across
1003 * multiple records reduces the load on the individual record.
1006 generate_random_buffer((uint8_t *)&idx, sizeof(idx));
1007 idx = idx % state.num_paths;
1009 if (!notify_cleanup_path(notify, state.paths[idx],
1010 state.delete_before)) {
1012 * notify_cleanup_path failed, the most likely reason
1013 * is that dbwrap_try_fetch_locked failed due to
1014 * contention. We allow one failed attempt per deleted
1015 * path on average before we give up.
1018 if (failure_pool == 0) {
1020 * Too many failures. We will come back here,
1021 * maybe next time there is less contention.
1027 TALLOC_FREE(state.paths[idx]);
1028 state.paths[idx] = state.paths[state.num_paths-1];
1029 state.num_paths -= 1;
1031 TALLOC_FREE(state.mem_ctx);
1034 static bool notify_cleanup_path(struct notify_context *notify,
1035 const char *path, time_t delete_before)
1037 struct db_record *notify_rec = NULL;
1038 struct db_record *idx_rec = NULL;
1039 TDB_DATA key = string_tdb_data(path);
1044 notify_rec = dbwrap_fetch_locked(notify->db_notify, talloc_tos(), key);
1045 if (notify_rec == NULL) {
1046 DEBUG(10, ("Could not fetch notify_rec\n"));
1049 value = dbwrap_record_get_value(notify_rec);
1051 if (value.dsize != sizeof(deleted)) {
1052 DEBUG(10, ("record %s has been re-used\n", path));
1055 memcpy(&deleted, value.dptr, sizeof(deleted));
1057 if (deleted >= delete_before) {
1058 DEBUG(10, ("record %s too young\n", path));
1063 * Be kind to ctdb and only try one dmaster migration at most.
1065 idx_rec = dbwrap_try_fetch_locked(notify->db_index, talloc_tos(), key);
1066 if (idx_rec == NULL) {
1067 DEBUG(10, ("Could not fetch idx_rec\n"));
1071 status = dbwrap_record_delete(notify_rec);
1072 if (!NT_STATUS_IS_OK(status)) {
1073 DEBUG(10, ("Could not delete notify_rec: %s\n",
1074 nt_errstr(status)));
1077 status = notify_del_idx(idx_rec, get_my_vnn());
1078 if (!NT_STATUS_IS_OK(status)) {
1079 DEBUG(10, ("Could not delete idx_rec: %s\n",
1080 nt_errstr(status)));
1084 TALLOC_FREE(idx_rec);
1085 TALLOC_FREE(notify_rec);
1089 static NTSTATUS notify_del_idx(struct db_record *rec, uint32_t vnn)
1091 TDB_DATA value = dbwrap_record_get_value(rec);
1095 if ((value.dsize % sizeof(uint32_t)) != 0) {
1096 DEBUG(1, ("Invalid value.dsize = %u\n",
1097 (unsigned)value.dsize));
1098 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1100 num_vnns = value.dsize / sizeof(uint32_t);
1101 vnns = (uint32_t *)value.dptr;
1103 for (i=0; i<num_vnns; i++) {
1104 if (vnns[i] == vnn) {
1109 if (i == num_vnns) {
1111 * Not found. Should not happen, but okay...
1113 return NT_STATUS_OK;
1116 memmove(&vnns[i], &vnns[i+1], sizeof(uint32_t) * (num_vnns - i - 1));
1117 value.dsize -= sizeof(uint32_t);
1119 if (value.dsize == 0) {
1120 return dbwrap_record_delete(rec);
1122 return dbwrap_record_store(rec, value, 0);
1125 struct notify_cluster_proxy_state {
1126 struct tevent_context *ev;
1127 struct notify_context *notify;
1128 struct ctdb_msg_channel *chan;
1131 static void notify_cluster_proxy_got_chan(struct tevent_req *subreq);
1132 static void notify_cluster_proxy_got_msg(struct tevent_req *subreq);
1133 static void notify_cluster_proxy_trigger(struct notify_context *notify,
1134 uint32_t action, uint32_t filter,
1137 struct tevent_req *notify_cluster_proxy_send(
1138 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1139 struct notify_context *notify)
1141 struct tevent_req *req, *subreq;
1142 struct notify_cluster_proxy_state *state;
1144 req = tevent_req_create(mem_ctx, &state,
1145 struct notify_cluster_proxy_state);
1150 state->notify = notify;
1152 subreq = ctdb_msg_channel_init_send(
1153 state, state->ev, lp_ctdbd_socket(),
1154 CTDB_SRVID_SAMBA_NOTIFY_PROXY);
1155 if (tevent_req_nomem(subreq, req)) {
1156 return tevent_req_post(req, ev);
1158 tevent_req_set_callback(subreq, notify_cluster_proxy_got_chan, req);
1162 static void notify_cluster_proxy_got_chan(struct tevent_req *subreq)
1164 struct tevent_req *req = tevent_req_callback_data(
1165 subreq, struct tevent_req);
1166 struct notify_cluster_proxy_state *state = tevent_req_data(
1167 req, struct notify_cluster_proxy_state);
1170 ret = ctdb_msg_channel_init_recv(subreq, state, &state->chan);
1171 TALLOC_FREE(subreq);
1173 tevent_req_error(req, ret);
1176 subreq = ctdb_msg_read_send(state, state->ev, state->chan);
1177 if (tevent_req_nomem(subreq, req)) {
1180 tevent_req_set_callback(subreq, notify_cluster_proxy_got_msg, req);
1183 static void notify_cluster_proxy_got_msg(struct tevent_req *subreq)
1185 struct tevent_req *req = tevent_req_callback_data(
1186 subreq, struct tevent_req);
1187 struct notify_cluster_proxy_state *state = tevent_req_data(
1188 req, struct notify_cluster_proxy_state);
1191 uint32_t action, filter;
1196 ret = ctdb_msg_read_recv(subreq, talloc_tos(), &msg, &msg_len);
1197 TALLOC_FREE(subreq);
1199 tevent_req_error(req, ret);
1203 res = notify_pull_remote_blob(talloc_tos(), msg, msg_len,
1204 &action, &filter, &path);
1207 tevent_req_error(req, EIO);
1210 notify_cluster_proxy_trigger(state->notify, action, filter, path);
1213 subreq = ctdb_msg_read_send(state, state->ev, state->chan);
1214 if (tevent_req_nomem(subreq, req)) {
1217 tevent_req_set_callback(subreq, notify_cluster_proxy_got_msg, req);
1220 static void notify_cluster_proxy_trigger(struct notify_context *notify,
1221 uint32_t action, uint32_t filter,
1224 const char *p, *next_p;
1226 for (p = path; p != NULL; p = next_p) {
1227 ptrdiff_t path_len = p - path;
1230 next_p = strchr(p+1, '/');
1231 recursive = (next_p != NULL);
1233 notify_trigger_local(notify, action, filter,
1234 path, path_len, recursive);
1238 int notify_cluster_proxy_recv(struct tevent_req *req)
1242 if (tevent_req_is_unix_error(req, &err)) {