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 server_id_buf tmp
;
448 struct notify_db_entry
*entries
;
449 size_t i
, num_entries
;
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
) {
473 if (serverid_equal(&e
->server
, pid
)) {
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) {
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
{
498 static void notify_trigger_index_parser(TDB_DATA key
, TDB_DATA data
,
501 struct notify_trigger_index_state
*state
=
502 (struct notify_trigger_index_state
*)private_data
;
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
));
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) {
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"));
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
];
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
;
549 if (*vnn1
== *vnn2
) {
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
;
561 enum ndr_err_code ndr_err
;
567 if (DEBUGLEVEL
>= 10) {
568 NDR_PRINT_DEBUG(notify_remote_event
, &ev
);
571 ndr_err
= ndr_push_struct_blob(
573 (ndr_push_flags_fn_t
)ndr_push_notify_remote_event
);
574 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
578 *pblob_len
= data
.length
;
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
,
587 struct notify_remote_event
*ev
;
588 enum ndr_err_code ndr_err
;
592 data
.data
= discard_const_p(uint8_t, blob
);
593 data
.length
= blob_len
;
595 ev
= talloc(mem_ctx
, struct notify_remote_event
);
600 ndr_err
= ndr_pull_struct_blob(
602 (ndr_pull_flags_fn_t
)ndr_pull_notify_remote_event
);
603 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
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
);
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
;
628 uint8_t *remote_blob
= NULL
;
629 size_t remote_blob_len
= 0;
630 char *path
, *to_free
;
631 char tmpbuf
[PATH_MAX
];
634 DEBUG(10, ("notify_trigger called action=0x%x, filter=0x%x, "
635 "dir=%s, name=%s\n", (unsigned)action
, (unsigned)filter
,
638 /* see if change notify is enabled at all */
639 if (notify
== NULL
) {
645 * The rest of this routine assumes an absolute path.
650 len
= full_path_tos(dir
, name
, tmpbuf
, sizeof(tmpbuf
),
653 DEBUG(1, ("full_path_tos failed\n"));
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
;
666 next_p
= strchr(p
+1, '/');
667 recursive
= (next_p
!= NULL
);
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
) {
685 ctdbd_conn
= messaging_ctdbd_connection();
686 if (ctdbd_conn
== NULL
) {
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"));
701 for (i
=0; i
<num_vnns
; i
++) {
702 uint32_t vnn
= idx_state
.vnns
[i
];
705 if (vnn
== last_vnn
) {
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
,
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
,
733 struct notify_db_entry
*entries
;
734 size_t i
, num_entries
;
737 DEBUG(10, ("notify_trigger_local called for %*s, path_len=%d, "
738 "filter=%d\n", (int)path_len
, path
, (int)path_len
,
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",
749 if (data
.dsize
== sizeof(time_t)) {
750 DEBUG(10, ("Got deleted record\n"));
753 if ((data
.dsize
% sizeof(struct notify_db_entry
)) != 0) {
754 DEBUG(1, ("Invalid data.dsize = %u\n",
755 (unsigned)data
.dsize
));
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
,
766 for (i
=0; i
<num_entries
; i
++) {
767 struct notify_db_entry
*e
= &entries
[i
];
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) {
780 if (!procid_is_local(&e
->server
)) {
781 struct server_id_buf tmp
;
782 DEBUG(1, ("internal error: Non-local pid %s in "
784 server_id_str_buf(e
->server
, &tmp
)));
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",
797 TALLOC_FREE(data
.dptr
);
801 struct timespec when
;
807 static NTSTATUS
notify_send(struct notify_context
*notify
,
808 struct server_id
*pid
,
809 const char *path
, uint32_t action
,
812 struct notify_msg m
= {};
815 m
.when
= timespec_current();
816 m
.private_data
= private_data
;
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__
));
842 if (data
->data
[data
->length
-1] != 0) {
843 DEBUG(1, ("%s: MSG_PVFS_NOTIFY path not 0-terminated\n",
848 m
= (struct notify_msg
*)data
->data
;
850 e
= (struct notify_event
) {
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
);
865 struct notify_walk_idx_state
{
866 void (*fn
)(const char *path
,
867 uint32_t *vnns
, size_t num_vnns
,
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
;
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
)));
888 path
= talloc_strndup(talloc_tos(), (char *)key
.dptr
, key
.dsize
);
890 DEBUG(1, ("talloc_strndup failed\n"));
893 state
->fn(path
, (uint32_t *)value
.dptr
, value
.dsize
/sizeof(uint32_t),
894 state
->private_data
);
899 void notify_walk_idx(struct notify_context
*notify
,
900 void (*fn
)(const char *path
,
901 uint32_t *vnns
, size_t num_vnns
,
905 struct notify_walk_idx_state state
;
907 state
.private_data
= private_data
;
908 dbwrap_traverse_read(notify
->db_index
, notify_walk_idx_fn
, &state
,
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
);
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
;
924 struct notify_db_entry
*entries
;
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
));
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
)));
942 entries
= (struct notify_db_entry
*)value
.dptr
;
943 num_entries
= value
.dsize
/ sizeof(struct notify_db_entry
);
947 path
= talloc_strndup(talloc_tos(), (char *)key
.dptr
, key
.dsize
);
949 DEBUG(1, ("talloc_strndup failed\n"));
952 state
->fn(path
, entries
, num_entries
, deleted_time
,
953 state
->private_data
);
958 void notify_walk(struct notify_context
*notify
,
959 void (*fn
)(const char *path
,
960 struct notify_db_entry
*entries
,
962 time_t deleted_time
, void *private_data
),
965 struct notify_walk_state state
;
967 state
.private_data
= private_data
;
968 dbwrap_traverse_read(notify
->db_notify
, notify_walk_fn
, &state
,
972 struct notify_cleanup_state
{
974 time_t delete_before
;
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
;
988 if (num_entries
!= 0) {
991 if (deleted_time
>= state
->delete_before
) {
995 p
= talloc_strdup(state
->mem_ctx
, path
);
997 DEBUG(1, ("talloc_strdup failed\n"));
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) {
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
;
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) {
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.
1051 if (failure_pool
== 0) {
1053 * Too many failures. We will come back here,
1054 * maybe next time there is less contention.
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
);
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"));
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
));
1088 memcpy(&deleted
, value
.dptr
, sizeof(deleted
));
1090 if (deleted
>= delete_before
) {
1091 DEBUG(10, ("record %s too young\n", path
));
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"));
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
)));
1117 TALLOC_FREE(idx_rec
);
1118 TALLOC_FREE(notify_rec
);
1122 static NTSTATUS
notify_del_idx(struct db_record
*rec
, uint32_t vnn
)
1124 TDB_DATA value
= dbwrap_record_get_value(rec
);
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
) {
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
,
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
);
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
);
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
);
1203 ret
= ctdb_msg_channel_init_recv(subreq
, state
, &state
->chan
);
1204 TALLOC_FREE(subreq
);
1206 tevent_req_error(req
, ret
);
1209 subreq
= ctdb_msg_read_send(state
, state
->ev
, state
->chan
);
1210 if (tevent_req_nomem(subreq
, req
)) {
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
);
1224 uint32_t action
, filter
;
1229 ret
= ctdb_msg_read_recv(subreq
, talloc_tos(), &msg
, &msg_len
);
1230 TALLOC_FREE(subreq
);
1232 tevent_req_error(req
, ret
);
1236 res
= notify_pull_remote_blob(talloc_tos(), msg
, msg_len
,
1237 &action
, &filter
, &path
);
1240 tevent_req_error(req
, EIO
);
1243 notify_cluster_proxy_trigger(state
->notify
, action
, filter
, path
);
1246 subreq
= ctdb_msg_read_send(state
, state
->ev
, state
->chan
);
1247 if (tevent_req_nomem(subreq
, req
)) {
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
,
1257 const char *p
, *next_p
;
1259 for (p
= path
; p
!= NULL
; p
= next_p
) {
1260 ptrdiff_t path_len
= p
- path
;
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
);