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
;
130 notify
= talloc(mem_ctx
, struct notify_context
);
131 if (notify
== NULL
) {
137 lp_ctx
= loadparm_init_s3(notify
, loadparm_s3_helpers());
139 db_path
= lock_path("notify.tdb");
140 if (db_path
== NULL
) {
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
) {
154 db_path
= lock_path("notify_index.tdb");
155 if (db_path
== NULL
) {
159 notify
->db_index
= db_open(
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
) {
167 if (!lp_clustering()) {
168 notify
->db_index
= db_open_cache(notify
, notify
->db_index
);
169 if (notify
->db_index
== NULL
) {
174 if (notify
->msg
!= NULL
) {
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",
186 talloc_set_destructor(notify
, notify_context_destructor
);
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
);
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
*),
216 struct notify_db_entry e
;
217 struct notify_list
*listel
;
218 struct db_record
*notify_rec
, *idx_rec
;
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
,
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
) {
239 return NT_STATUS_NO_MEMORY
;
241 DLIST_ADD(notify
->list
, listel
);
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
,
253 if (notify_rec
== NULL
) {
254 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
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
,
267 if (notify_copy
.dptr
== NULL
) {
268 TALLOC_FREE(notify_rec
);
269 status
= NT_STATUS_NO_MEMORY
;
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
)) {
284 * Someone else has added the idx entry already
286 TALLOC_FREE(notify_rec
);
290 idx_rec
= dbwrap_fetch_locked(notify
->db_index
,
292 if (idx_rec
== NULL
) {
293 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
296 status
= notify_add_idx(idx_rec
, get_my_vnn());
297 if (!NT_STATUS_IS_OK(status
)) {
301 TALLOC_FREE(idx_rec
);
302 TALLOC_FREE(notify_rec
);
306 if (notify_copy
.dsize
!= 0) {
307 dbwrap_record_store(notify_rec
, notify_copy
, 0);
309 dbwrap_record_delete(notify_rec
);
311 TALLOC_FREE(notify_rec
);
313 DLIST_REMOVE(notify
->list
, listel
);
318 static NTSTATUS
notify_add_entry(struct db_record
*rec
,
319 const struct notify_db_entry
*e
,
322 TDB_DATA value
= dbwrap_record_get_value(rec
);
323 struct notify_db_entry
*entries
;
328 if (value
.dsize
== sizeof(time_t)) {
329 DEBUG(10, ("Re-using deleted entry\n"));
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) {
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
)) {
358 *p_add_idx
= add_idx
;
362 static NTSTATUS
notify_add_idx(struct db_record
*rec
, uint32_t vnn
)
364 TDB_DATA value
= dbwrap_record_get_value(rec
);
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
) {
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
));
398 status
= dbwrap_record_store(rec
, value
, 0);
399 if (!NT_STATUS_IS_OK(status
)) {
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
;
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
);
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
));
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
);
442 static NTSTATUS
notify_del_entry(struct db_record
*rec
,
443 const struct server_id
*pid
,
446 TDB_DATA value
= dbwrap_record_get_value(rec
);
447 struct notify_db_entry
*entries
;
448 size_t i
, num_entries
;
451 DEBUG(10, ("del_entry called for %s %p\n", procid_str_static(pid
),
454 if ((value
.dsize
% sizeof(struct notify_db_entry
)) != 0) {
455 DEBUG(1, ("Invalid value.dsize = %u\n",
456 (unsigned)value
.dsize
));
457 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
459 num_entries
= value
.dsize
/ sizeof(struct notify_db_entry
);
460 entries
= (struct notify_db_entry
*)value
.dptr
;
462 for (i
=0; i
<num_entries
; i
++) {
463 struct notify_db_entry
*e
= &entries
[i
];
465 if (DEBUGLEVEL
>= 10) {
466 NDR_PRINT_DEBUG(notify_db_entry
, e
);
469 if (e
->private_data
!= private_data
) {
472 if (serverid_equal(&e
->server
, pid
)) {
476 if (i
== num_entries
) {
477 return NT_STATUS_NOT_FOUND
;
479 entries
[i
] = entries
[num_entries
-1];
480 value
.dsize
-= sizeof(struct notify_db_entry
);
482 if (value
.dsize
== 0) {
484 value
.dptr
= (uint8_t *)&now
;
485 value
.dsize
= sizeof(now
);
487 return dbwrap_record_store(rec
, value
, 0);
490 struct notify_trigger_index_state
{
497 static void notify_trigger_index_parser(TDB_DATA key
, TDB_DATA data
,
500 struct notify_trigger_index_state
*state
=
501 (struct notify_trigger_index_state
*)private_data
;
503 size_t i
, num_vnns
, num_new_vnns
, num_remote_vnns
;
505 if ((data
.dsize
% sizeof(uint32_t)) != 0) {
506 DEBUG(1, ("Invalid record size in notify index db: %u\n",
507 (unsigned)data
.dsize
));
510 new_vnns
= (uint32_t *)data
.dptr
;
511 num_new_vnns
= data
.dsize
/ sizeof(uint32_t);
512 num_remote_vnns
= num_new_vnns
;
514 for (i
=0; i
<num_new_vnns
; i
++) {
515 if (new_vnns
[i
] == state
->my_vnn
) {
516 state
->found_my_vnn
= true;
517 num_remote_vnns
-= 1;
520 if (num_remote_vnns
== 0) {
524 num_vnns
= talloc_array_length(state
->vnns
);
525 state
->vnns
= talloc_realloc(state
->mem_ctx
, state
->vnns
, uint32_t,
526 num_vnns
+ num_remote_vnns
);
527 if (state
->vnns
== NULL
) {
528 DEBUG(1, ("talloc_realloc failed\n"));
532 for (i
=0; i
<num_new_vnns
; i
++) {
533 if (new_vnns
[i
] != state
->my_vnn
) {
534 state
->vnns
[num_vnns
] = new_vnns
[i
];
540 static int vnn_cmp(const void *p1
, const void *p2
)
542 const uint32_t *vnn1
= (const uint32_t *)p1
;
543 const uint32_t *vnn2
= (const uint32_t *)p2
;
548 if (*vnn1
== *vnn2
) {
554 static bool notify_push_remote_blob(TALLOC_CTX
*mem_ctx
, uint32_t action
,
555 uint32_t filter
, const char *path
,
556 uint8_t **pblob
, size_t *pblob_len
)
558 struct notify_remote_event ev
;
560 enum ndr_err_code ndr_err
;
566 if (DEBUGLEVEL
>= 10) {
567 NDR_PRINT_DEBUG(notify_remote_event
, &ev
);
570 ndr_err
= ndr_push_struct_blob(
572 (ndr_push_flags_fn_t
)ndr_push_notify_remote_event
);
573 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
577 *pblob_len
= data
.length
;
581 static bool notify_pull_remote_blob(TALLOC_CTX
*mem_ctx
,
582 const uint8_t *blob
, size_t blob_len
,
583 uint32_t *paction
, uint32_t *pfilter
,
586 struct notify_remote_event
*ev
;
587 enum ndr_err_code ndr_err
;
591 data
.data
= discard_const_p(uint8_t, blob
);
592 data
.length
= blob_len
;
594 ev
= talloc(mem_ctx
, struct notify_remote_event
);
599 ndr_err
= ndr_pull_struct_blob(
601 (ndr_pull_flags_fn_t
)ndr_pull_notify_remote_event
);
602 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
606 if (DEBUGLEVEL
>= 10) {
607 NDR_PRINT_DEBUG(notify_remote_event
, ev
);
609 *paction
= ev
->action
;
610 *pfilter
= ev
->filter
;
611 p
= discard_const_p(char, ev
->path
);
612 *path
= talloc_move(mem_ctx
, &p
);
618 void notify_trigger(struct notify_context
*notify
,
619 uint32_t action
, uint32_t filter
,
620 const char *dir
, const char *name
)
622 struct ctdbd_connection
*ctdbd_conn
;
623 struct notify_trigger_index_state idx_state
;
624 const char *p
, *next_p
;
627 uint8_t *remote_blob
= NULL
;
628 size_t remote_blob_len
= 0;
629 char *path
, *to_free
;
630 char tmpbuf
[PATH_MAX
];
633 DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
634 "dir=%s, name=%s\n", (unsigned)action
, (unsigned)filter
,
637 /* see if change notify is enabled at all */
638 if (notify
== NULL
) {
644 * The rest of this routine assumes an absolute path.
649 len
= full_path_tos(dir
, name
, tmpbuf
, sizeof(tmpbuf
),
652 DEBUG(1, ("full_path_tos failed\n"));
656 idx_state
.mem_ctx
= talloc_tos();
657 idx_state
.vnns
= NULL
;
658 idx_state
.found_my_vnn
= false;
659 idx_state
.my_vnn
= get_my_vnn();
661 for (p
= strchr(path
+1, '/'); p
!= NULL
; p
= next_p
) {
662 ptrdiff_t path_len
= p
- path
;
665 next_p
= strchr(p
+1, '/');
666 recursive
= (next_p
!= NULL
);
670 make_tdb_data(discard_const_p(uint8_t, path
), path_len
),
671 notify_trigger_index_parser
, &idx_state
);
673 if (idx_state
.found_my_vnn
) {
674 notify_trigger_local(notify
, action
, filter
,
675 path
, path_len
, recursive
);
676 idx_state
.found_my_vnn
= false;
680 if (idx_state
.vnns
== NULL
) {
684 ctdbd_conn
= messaging_ctdbd_connection();
685 if (ctdbd_conn
== NULL
) {
689 num_vnns
= talloc_array_length(idx_state
.vnns
);
690 qsort(idx_state
.vnns
, num_vnns
, sizeof(uint32_t), vnn_cmp
);
692 last_vnn
= 0xffffffff;
694 if (!notify_push_remote_blob(talloc_tos(), action
, filter
, path
,
695 &remote_blob
, &remote_blob_len
)) {
696 DEBUG(1, ("notify_push_remote_blob failed\n"));
700 for (i
=0; i
<num_vnns
; i
++) {
701 uint32_t vnn
= idx_state
.vnns
[i
];
704 if (vnn
== last_vnn
) {
708 status
= ctdbd_messaging_send_blob(
709 ctdbd_conn
, vnn
, CTDB_SRVID_SAMBA_NOTIFY_PROXY
,
710 remote_blob
, remote_blob_len
);
711 if (!NT_STATUS_IS_OK(status
)) {
712 DEBUG(10, ("ctdbd_messaging_send_blob to vnn %d "
713 "returned %s, ignoring\n", (int)vnn
,
721 TALLOC_FREE(remote_blob
);
722 TALLOC_FREE(idx_state
.vnns
);
723 TALLOC_FREE(to_free
);
726 static void notify_trigger_local(struct notify_context
*notify
,
727 uint32_t action
, uint32_t filter
,
728 const char *path
, size_t path_len
,
732 struct notify_db_entry
*entries
;
733 size_t i
, num_entries
;
736 DEBUG(10, ("notify_trigger_local called for %*s, path_len=%d, "
737 "filter=%d\n", (int)path_len
, path
, (int)path_len
,
740 status
= dbwrap_fetch(
741 notify
->db_notify
, talloc_tos(),
742 make_tdb_data(discard_const_p(uint8_t, path
), path_len
), &data
);
743 if (!NT_STATUS_IS_OK(status
)) {
744 DEBUG(10, ("dbwrap_fetch returned %s\n",
748 if (data
.dsize
== sizeof(time_t)) {
749 DEBUG(10, ("Got deleted record\n"));
752 if ((data
.dsize
% sizeof(struct notify_db_entry
)) != 0) {
753 DEBUG(1, ("Invalid data.dsize = %u\n",
754 (unsigned)data
.dsize
));
758 entries
= (struct notify_db_entry
*)data
.dptr
;
759 num_entries
= data
.dsize
/ sizeof(struct notify_db_entry
);
761 DEBUG(10, ("recursive = %s pathlen=%d (%c)\n",
762 recursive
? "true" : "false", (int)path_len
,
765 for (i
=0; i
<num_entries
; i
++) {
766 struct notify_db_entry
*e
= &entries
[i
];
769 if (DEBUGLEVEL
>= 10) {
770 NDR_PRINT_DEBUG(notify_db_entry
, e
);
773 e_filter
= recursive
? e
->subdir_filter
: e
->filter
;
775 if ((filter
& e_filter
) == 0) {
779 if (!procid_is_local(&e
->server
)) {
780 DEBUG(1, ("internal error: Non-local pid %s in "
782 procid_str_static(&e
->server
)));
786 status
= notify_send(notify
, &e
->server
, path
+ path_len
+ 1,
787 action
, e
->private_data
);
788 if (!NT_STATUS_IS_OK(status
)) {
789 DEBUG(10, ("notify_send returned %s\n",
795 TALLOC_FREE(data
.dptr
);
799 struct timespec when
;
805 static NTSTATUS
notify_send(struct notify_context
*notify
,
806 struct server_id
*pid
,
807 const char *path
, uint32_t action
,
810 struct notify_msg m
= {};
813 m
.when
= timespec_current();
814 m
.private_data
= private_data
;
817 iov
[0].iov_base
= &m
;
818 iov
[0].iov_len
= offsetof(struct notify_msg
, path
);
819 iov
[1].iov_base
= discard_const_p(char, path
);
820 iov
[1].iov_len
= strlen(path
)+1;
822 return messaging_send_iov(notify
->msg
, *pid
, MSG_PVFS_NOTIFY
,
823 iov
, ARRAY_SIZE(iov
), NULL
, 0);
826 static void notify_handler(struct messaging_context
*msg_ctx
,
827 void *private_data
, uint32_t msg_type
,
828 struct server_id server_id
, DATA_BLOB
*data
)
830 struct notify_context
*notify
= talloc_get_type_abort(
831 private_data
, struct notify_context
);
832 struct notify_msg
*m
;
833 struct notify_event e
;
834 struct notify_list
*listel
;
836 if (data
->length
== 0) {
837 DEBUG(1, ("%s: Got 0-sized MSG_PVFS_NOTIFY msg\n", __func__
));
840 if (data
->data
[data
->length
-1] != 0) {
841 DEBUG(1, ("%s: MSG_PVFS_NOTIFY path not 0-terminated\n",
846 m
= (struct notify_msg
*)data
->data
;
848 e
= (struct notify_event
) {
851 .private_data
= m
->private_data
,
852 .dir
= discard_const_p(char, "")
855 for (listel
=notify
->list
;listel
;listel
=listel
->next
) {
856 if (listel
->private_data
== m
->private_data
) {
857 listel
->callback(listel
->private_data
, m
->when
, &e
);
863 struct notify_walk_idx_state
{
864 void (*fn
)(const char *path
,
865 uint32_t *vnns
, size_t num_vnns
,
870 static int notify_walk_idx_fn(struct db_record
*rec
, void *private_data
)
872 struct notify_walk_idx_state
*state
=
873 (struct notify_walk_idx_state
*)private_data
;
877 key
= dbwrap_record_get_key(rec
);
878 value
= dbwrap_record_get_value(rec
);
880 if ((value
.dsize
% sizeof(uint32_t)) != 0) {
881 DEBUG(1, ("invalid value size in notify index db: %u\n",
882 (unsigned)(value
.dsize
)));
886 path
= talloc_strndup(talloc_tos(), (char *)key
.dptr
, key
.dsize
);
888 DEBUG(1, ("talloc_strndup failed\n"));
891 state
->fn(path
, (uint32_t *)value
.dptr
, value
.dsize
/sizeof(uint32_t),
892 state
->private_data
);
897 void notify_walk_idx(struct notify_context
*notify
,
898 void (*fn
)(const char *path
,
899 uint32_t *vnns
, size_t num_vnns
,
903 struct notify_walk_idx_state state
;
905 state
.private_data
= private_data
;
906 dbwrap_traverse_read(notify
->db_index
, notify_walk_idx_fn
, &state
,
910 struct notify_walk_state
{
911 void (*fn
)(const char *path
,
912 struct notify_db_entry
*entries
, size_t num_entries
,
913 time_t deleted_time
, void *private_data
);
917 static int notify_walk_fn(struct db_record
*rec
, void *private_data
)
919 struct notify_walk_state
*state
=
920 (struct notify_walk_state
*)private_data
;
922 struct notify_db_entry
*entries
;
927 key
= dbwrap_record_get_key(rec
);
928 value
= dbwrap_record_get_value(rec
);
930 if (value
.dsize
== sizeof(deleted_time
)) {
931 memcpy(&deleted_time
, value
.dptr
, sizeof(deleted_time
));
935 if ((value
.dsize
% sizeof(struct notify_db_entry
)) != 0) {
936 DEBUG(1, ("invalid value size in notify db: %u\n",
937 (unsigned)(value
.dsize
)));
940 entries
= (struct notify_db_entry
*)value
.dptr
;
941 num_entries
= value
.dsize
/ sizeof(struct notify_db_entry
);
945 path
= talloc_strndup(talloc_tos(), (char *)key
.dptr
, key
.dsize
);
947 DEBUG(1, ("talloc_strndup failed\n"));
950 state
->fn(path
, entries
, num_entries
, deleted_time
,
951 state
->private_data
);
956 void notify_walk(struct notify_context
*notify
,
957 void (*fn
)(const char *path
,
958 struct notify_db_entry
*entries
,
960 time_t deleted_time
, void *private_data
),
963 struct notify_walk_state state
;
965 state
.private_data
= private_data
;
966 dbwrap_traverse_read(notify
->db_notify
, notify_walk_fn
, &state
,
970 struct notify_cleanup_state
{
972 time_t delete_before
;
978 static void notify_cleanup_collect(
979 const char *path
, struct notify_db_entry
*entries
, size_t num_entries
,
980 time_t deleted_time
, void *private_data
)
982 struct notify_cleanup_state
*state
=
983 (struct notify_cleanup_state
*)private_data
;
986 if (num_entries
!= 0) {
989 if (deleted_time
>= state
->delete_before
) {
993 p
= talloc_strdup(state
->mem_ctx
, path
);
995 DEBUG(1, ("talloc_strdup failed\n"));
998 add_to_large_array(state
->mem_ctx
, sizeof(p
), (void *)&p
,
999 &state
->paths
, &state
->num_paths
,
1000 &state
->array_size
);
1001 if (state
->array_size
== -1) {
1006 static bool notify_cleanup_path(struct notify_context
*notify
,
1007 const char *path
, time_t delete_before
);
1009 void notify_cleanup(struct notify_context
*notify
)
1011 struct notify_cleanup_state state
;
1012 uint32_t failure_pool
;
1015 state
.mem_ctx
= talloc_stackframe();
1017 state
.delete_before
= time(NULL
)
1018 - lp_parm_int(-1, "smbd", "notify cleanup interval", 60);
1020 notify_walk(notify
, notify_cleanup_collect
, &state
);
1022 failure_pool
= state
.num_paths
;
1024 while (state
.num_paths
!= 0) {
1028 * This loop is designed to be as kind as possible to
1029 * ctdb. ctdb does not like it if many smbds hammer on a
1030 * single record. If on many nodes the cleanup process starts
1031 * running, it can happen that all of them need to clean up
1032 * records in the same order. This would generate a ctdb
1033 * migrate storm on these records. Randomizing the load across
1034 * multiple records reduces the load on the individual record.
1037 generate_random_buffer((uint8_t *)&idx
, sizeof(idx
));
1038 idx
= idx
% state
.num_paths
;
1040 if (!notify_cleanup_path(notify
, state
.paths
[idx
],
1041 state
.delete_before
)) {
1043 * notify_cleanup_path failed, the most likely reason
1044 * is that dbwrap_try_fetch_locked failed due to
1045 * contention. We allow one failed attempt per deleted
1046 * path on average before we give up.
1049 if (failure_pool
== 0) {
1051 * Too many failures. We will come back here,
1052 * maybe next time there is less contention.
1058 TALLOC_FREE(state
.paths
[idx
]);
1059 state
.paths
[idx
] = state
.paths
[state
.num_paths
-1];
1060 state
.num_paths
-= 1;
1062 TALLOC_FREE(state
.mem_ctx
);
1065 static bool notify_cleanup_path(struct notify_context
*notify
,
1066 const char *path
, time_t delete_before
)
1068 struct db_record
*notify_rec
= NULL
;
1069 struct db_record
*idx_rec
= NULL
;
1070 TDB_DATA key
= string_tdb_data(path
);
1075 notify_rec
= dbwrap_fetch_locked(notify
->db_notify
, talloc_tos(), key
);
1076 if (notify_rec
== NULL
) {
1077 DEBUG(10, ("Could not fetch notify_rec\n"));
1080 value
= dbwrap_record_get_value(notify_rec
);
1082 if (value
.dsize
!= sizeof(deleted
)) {
1083 DEBUG(10, ("record %s has been re-used\n", path
));
1086 memcpy(&deleted
, value
.dptr
, sizeof(deleted
));
1088 if (deleted
>= delete_before
) {
1089 DEBUG(10, ("record %s too young\n", path
));
1094 * Be kind to ctdb and only try one dmaster migration at most.
1096 idx_rec
= dbwrap_try_fetch_locked(notify
->db_index
, talloc_tos(), key
);
1097 if (idx_rec
== NULL
) {
1098 DEBUG(10, ("Could not fetch idx_rec\n"));
1102 status
= dbwrap_record_delete(notify_rec
);
1103 if (!NT_STATUS_IS_OK(status
)) {
1104 DEBUG(10, ("Could not delete notify_rec: %s\n",
1105 nt_errstr(status
)));
1108 status
= notify_del_idx(idx_rec
, get_my_vnn());
1109 if (!NT_STATUS_IS_OK(status
)) {
1110 DEBUG(10, ("Could not delete idx_rec: %s\n",
1111 nt_errstr(status
)));
1115 TALLOC_FREE(idx_rec
);
1116 TALLOC_FREE(notify_rec
);
1120 static NTSTATUS
notify_del_idx(struct db_record
*rec
, uint32_t vnn
)
1122 TDB_DATA value
= dbwrap_record_get_value(rec
);
1126 if ((value
.dsize
% sizeof(uint32_t)) != 0) {
1127 DEBUG(1, ("Invalid value.dsize = %u\n",
1128 (unsigned)value
.dsize
));
1129 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
1131 num_vnns
= value
.dsize
/ sizeof(uint32_t);
1132 vnns
= (uint32_t *)value
.dptr
;
1134 for (i
=0; i
<num_vnns
; i
++) {
1135 if (vnns
[i
] == vnn
) {
1140 if (i
== num_vnns
) {
1142 * Not found. Should not happen, but okay...
1144 return NT_STATUS_OK
;
1147 memmove(&vnns
[i
], &vnns
[i
+1], sizeof(uint32_t) * (num_vnns
- i
- 1));
1148 value
.dsize
-= sizeof(uint32_t);
1150 if (value
.dsize
== 0) {
1151 return dbwrap_record_delete(rec
);
1153 return dbwrap_record_store(rec
, value
, 0);
1156 struct notify_cluster_proxy_state
{
1157 struct tevent_context
*ev
;
1158 struct notify_context
*notify
;
1159 struct ctdb_msg_channel
*chan
;
1162 static void notify_cluster_proxy_got_chan(struct tevent_req
*subreq
);
1163 static void notify_cluster_proxy_got_msg(struct tevent_req
*subreq
);
1164 static void notify_cluster_proxy_trigger(struct notify_context
*notify
,
1165 uint32_t action
, uint32_t filter
,
1168 struct tevent_req
*notify_cluster_proxy_send(
1169 TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
1170 struct notify_context
*notify
)
1172 struct tevent_req
*req
, *subreq
;
1173 struct notify_cluster_proxy_state
*state
;
1175 req
= tevent_req_create(mem_ctx
, &state
,
1176 struct notify_cluster_proxy_state
);
1181 state
->notify
= notify
;
1183 subreq
= ctdb_msg_channel_init_send(
1184 state
, state
->ev
, lp_ctdbd_socket(),
1185 CTDB_SRVID_SAMBA_NOTIFY_PROXY
);
1186 if (tevent_req_nomem(subreq
, req
)) {
1187 return tevent_req_post(req
, ev
);
1189 tevent_req_set_callback(subreq
, notify_cluster_proxy_got_chan
, req
);
1193 static void notify_cluster_proxy_got_chan(struct tevent_req
*subreq
)
1195 struct tevent_req
*req
= tevent_req_callback_data(
1196 subreq
, struct tevent_req
);
1197 struct notify_cluster_proxy_state
*state
= tevent_req_data(
1198 req
, struct notify_cluster_proxy_state
);
1201 ret
= ctdb_msg_channel_init_recv(subreq
, state
, &state
->chan
);
1202 TALLOC_FREE(subreq
);
1204 tevent_req_error(req
, ret
);
1207 subreq
= ctdb_msg_read_send(state
, state
->ev
, state
->chan
);
1208 if (tevent_req_nomem(subreq
, req
)) {
1211 tevent_req_set_callback(subreq
, notify_cluster_proxy_got_msg
, req
);
1214 static void notify_cluster_proxy_got_msg(struct tevent_req
*subreq
)
1216 struct tevent_req
*req
= tevent_req_callback_data(
1217 subreq
, struct tevent_req
);
1218 struct notify_cluster_proxy_state
*state
= tevent_req_data(
1219 req
, struct notify_cluster_proxy_state
);
1222 uint32_t action
, filter
;
1227 ret
= ctdb_msg_read_recv(subreq
, talloc_tos(), &msg
, &msg_len
);
1228 TALLOC_FREE(subreq
);
1230 tevent_req_error(req
, ret
);
1234 res
= notify_pull_remote_blob(talloc_tos(), msg
, msg_len
,
1235 &action
, &filter
, &path
);
1238 tevent_req_error(req
, EIO
);
1241 notify_cluster_proxy_trigger(state
->notify
, action
, filter
, path
);
1244 subreq
= ctdb_msg_read_send(state
, state
->ev
, state
->chan
);
1245 if (tevent_req_nomem(subreq
, req
)) {
1248 tevent_req_set_callback(subreq
, notify_cluster_proxy_got_msg
, req
);
1251 static void notify_cluster_proxy_trigger(struct notify_context
*notify
,
1252 uint32_t action
, uint32_t filter
,
1255 const char *p
, *next_p
;
1257 for (p
= path
; p
!= NULL
; p
= next_p
) {
1258 ptrdiff_t path_len
= p
- path
;
1261 next_p
= strchr(p
+1, '/');
1262 recursive
= (next_p
!= NULL
);
1264 notify_trigger_local(notify
, action
, filter
,
1265 path
, path_len
, recursive
);
1269 int notify_cluster_proxy_recv(struct tevent_req
*req
)
1271 return tevent_req_simple_recv_unix(req
);