2 Unix SMB/CIFS implementation.
3 Database interface wrapper around ctdbd
4 Copyright (C) Volker Lendecke 2007-2009
5 Copyright (C) Michael Adam 2009
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 #include "system/filesys.h"
23 #include "lib/tdb_wrap/tdb_wrap.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_ctdb.h"
27 #include "dbwrap/dbwrap_rbt.h"
28 #include "lib/param/param.h"
30 #include "ctdb/include/ctdb_protocol.h"
31 #include "ctdbd_conn.h"
32 #include "dbwrap/dbwrap.h"
33 #include "dbwrap/dbwrap_private.h"
34 #include "dbwrap/dbwrap_ctdb.h"
37 #include "messages_ctdb.h"
38 #include "lib/cluster_support.h"
39 #include "lib/util/tevent_ntstatus.h"
41 struct db_ctdb_transaction_handle
{
42 struct db_ctdb_ctx
*ctx
;
44 * we store the writes done under a transaction:
46 struct ctdb_marshall_buffer
*m_write
;
53 struct db_context
*db
;
54 struct tdb_wrap
*wtdb
;
56 struct db_ctdb_transaction_handle
*transaction
;
57 struct g_lock_ctx
*lock_ctx
;
59 /* thresholds for warning messages */
60 int warn_unlock_msecs
;
61 int warn_migrate_msecs
;
62 int warn_migrate_attempts
;
63 int warn_locktime_msecs
;
67 struct db_ctdb_ctx
*ctdb_ctx
;
68 struct ctdb_ltdb_header header
;
69 struct timeval lock_time
;
72 struct ctdb_async_ctx
{
74 struct ctdbd_connection
*async_conn
;
77 static struct ctdb_async_ctx ctdb_async_ctx
;
79 static int ctdb_async_ctx_init_internal(TALLOC_CTX
*mem_ctx
,
80 struct tevent_context
*ev
,
86 TALLOC_FREE(ctdb_async_ctx
.async_conn
);
87 ctdb_async_ctx
.initialized
= false;
90 if (ctdb_async_ctx
.initialized
) {
95 ret
= ctdbd_init_connection(mem_ctx
,
98 &ctdb_async_ctx
.async_conn
);
101 if (ctdb_async_ctx
.async_conn
== NULL
) {
102 DBG_ERR("ctdbd_init_connection failed\n");
106 ret
= ctdbd_setup_fde(ctdb_async_ctx
.async_conn
, ev
);
108 DBG_ERR("ctdbd_setup_fde failed\n");
109 TALLOC_FREE(ctdb_async_ctx
.async_conn
);
113 ctdb_async_ctx
.initialized
= true;
117 static int ctdb_async_ctx_init(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
)
119 return ctdb_async_ctx_init_internal(mem_ctx
, ev
, false);
122 int ctdb_async_ctx_reinit(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
)
124 return ctdb_async_ctx_init_internal(mem_ctx
, ev
, true);
127 static NTSTATUS
tdb_error_to_ntstatus(struct tdb_context
*tdb
)
129 enum TDB_ERROR tret
= tdb_error(tdb
);
131 return map_nt_error_from_tdb(tret
);
134 struct db_ctdb_ltdb_parse_state
{
135 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
136 TDB_DATA data
, void *private_data
);
140 static int db_ctdb_ltdb_parser(TDB_DATA key
, TDB_DATA data
,
143 struct db_ctdb_ltdb_parse_state
*state
=
144 (struct db_ctdb_ltdb_parse_state
*)private_data
;
146 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
151 key
, (struct ctdb_ltdb_header
*)data
.dptr
,
152 make_tdb_data(data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
153 data
.dsize
- sizeof(struct ctdb_ltdb_header
)),
154 state
->private_data
);
158 static NTSTATUS
db_ctdb_ltdb_parse(
159 struct db_ctdb_ctx
*db
, TDB_DATA key
,
160 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
161 TDB_DATA data
, void *private_data
),
164 struct db_ctdb_ltdb_parse_state state
;
167 state
.parser
= parser
;
168 state
.private_data
= private_data
;
170 ret
= tdb_parse_record(db
->wtdb
->tdb
, key
, db_ctdb_ltdb_parser
,
173 return NT_STATUS_NOT_FOUND
;
179 * Store a record together with the ctdb record header
180 * in the local copy of the database.
182 static NTSTATUS
db_ctdb_ltdb_store(struct db_ctdb_ctx
*db
,
184 struct ctdb_ltdb_header
*header
,
185 const TDB_DATA
*dbufs
, int num_dbufs
)
187 TDB_DATA recs
[num_dbufs
+1];
190 recs
[0] = (TDB_DATA
) { .dptr
= (uint8_t *)header
,
191 .dsize
= sizeof(struct ctdb_ltdb_header
) };
192 memcpy(&recs
[1], dbufs
, sizeof(TDB_DATA
) * num_dbufs
);
194 ret
= tdb_storev(db
->wtdb
->tdb
, key
, recs
, num_dbufs
+ 1, TDB_REPLACE
);
196 return (ret
== 0) ? NT_STATUS_OK
197 : tdb_error_to_ntstatus(db
->wtdb
->tdb
);
202 form a ctdb_rec_data record from a key/data pair
204 static struct ctdb_rec_data_old
*db_ctdb_marshall_record(TALLOC_CTX
*mem_ctx
, uint32_t reqid
,
206 struct ctdb_ltdb_header
*header
,
210 struct ctdb_rec_data_old
*d
;
212 length
= offsetof(struct ctdb_rec_data_old
, data
) + key
.dsize
+
213 data
.dsize
+ sizeof(*header
);
214 d
= (struct ctdb_rec_data_old
*)talloc_size(mem_ctx
, length
);
220 d
->keylen
= key
.dsize
;
221 memcpy(&d
->data
[0], key
.dptr
, key
.dsize
);
223 d
->datalen
= data
.dsize
+ sizeof(*header
);
224 memcpy(&d
->data
[key
.dsize
], header
, sizeof(*header
));
225 memcpy(&d
->data
[key
.dsize
+sizeof(*header
)], data
.dptr
, data
.dsize
);
230 /* helper function for marshalling multiple records */
231 static struct ctdb_marshall_buffer
*db_ctdb_marshall_add(TALLOC_CTX
*mem_ctx
,
232 struct ctdb_marshall_buffer
*m
,
236 struct ctdb_ltdb_header
*header
,
239 struct ctdb_rec_data_old
*r
;
240 size_t m_size
, r_size
;
241 struct ctdb_marshall_buffer
*m2
= NULL
;
243 r
= db_ctdb_marshall_record(talloc_tos(), reqid
, key
, header
, data
);
250 m
= (struct ctdb_marshall_buffer
*)talloc_zero_size(
251 mem_ctx
, offsetof(struct ctdb_marshall_buffer
, data
));
258 m_size
= talloc_get_size(m
);
259 r_size
= talloc_get_size(r
);
261 m2
= (struct ctdb_marshall_buffer
*)talloc_realloc_size(
262 mem_ctx
, m
, m_size
+ r_size
);
268 memcpy(m_size
+ (uint8_t *)m2
, r
, r_size
);
277 /* we've finished marshalling, return a data blob with the marshalled records */
278 static TDB_DATA
db_ctdb_marshall_finish(struct ctdb_marshall_buffer
*m
)
281 data
.dptr
= (uint8_t *)m
;
282 data
.dsize
= talloc_get_size(m
);
287 loop over a marshalling buffer
289 - pass r==NULL to start
290 - loop the number of times indicated by m->count
292 static struct ctdb_rec_data_old
*db_ctdb_marshall_loop_next_key(
293 struct ctdb_marshall_buffer
*m
, struct ctdb_rec_data_old
*r
, TDB_DATA
*key
)
296 r
= (struct ctdb_rec_data_old
*)&m
->data
[0];
298 r
= (struct ctdb_rec_data_old
*)(r
->length
+ (uint8_t *)r
);
301 key
->dptr
= &r
->data
[0];
302 key
->dsize
= r
->keylen
;
306 static bool db_ctdb_marshall_buf_parse(
307 struct ctdb_rec_data_old
*r
, uint32_t *reqid
,
308 struct ctdb_ltdb_header
**header
, TDB_DATA
*data
)
310 if (r
->datalen
< sizeof(struct ctdb_ltdb_header
)) {
316 data
->dptr
= &r
->data
[r
->keylen
] + sizeof(struct ctdb_ltdb_header
);
317 data
->dsize
= r
->datalen
- sizeof(struct ctdb_ltdb_header
);
319 *header
= (struct ctdb_ltdb_header
*)&r
->data
[r
->keylen
];
325 * CTDB transaction destructor
327 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle
*h
)
331 status
= g_lock_unlock(h
->ctx
->lock_ctx
, h
->lock_name
);
332 if (!NT_STATUS_IS_OK(status
)) {
333 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h
->lock_name
,
341 * CTDB dbwrap API: transaction_start function
342 * starts a transaction on a persistent database
344 static int db_ctdb_transaction_start(struct db_context
*db
)
346 struct db_ctdb_transaction_handle
*h
;
348 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
351 if (!db
->persistent
) {
352 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
357 if (ctx
->transaction
) {
358 ctx
->transaction
->nesting
++;
359 DEBUG(5, (__location__
" transaction start on db 0x%08x: nesting %d -> %d\n",
360 ctx
->db_id
, ctx
->transaction
->nesting
- 1, ctx
->transaction
->nesting
));
364 h
= talloc_zero(db
, struct db_ctdb_transaction_handle
);
366 DEBUG(0,(__location__
" oom for transaction handle\n"));
372 h
->lock_name
= talloc_asprintf(h
, "transaction_db_0x%08x",
373 (unsigned int)ctx
->db_id
);
374 if (h
->lock_name
== NULL
) {
375 DEBUG(0, ("talloc_asprintf failed\n"));
381 * Wait a day, i.e. forever...
383 status
= g_lock_lock(ctx
->lock_ctx
, h
->lock_name
, G_LOCK_WRITE
,
384 timeval_set(86400, 0));
385 if (!NT_STATUS_IS_OK(status
)) {
386 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status
)));
391 talloc_set_destructor(h
, db_ctdb_transaction_destructor
);
393 ctx
->transaction
= h
;
395 DEBUG(5,(__location__
" transaction started on db 0x%08x\n", ctx
->db_id
));
400 static bool parse_newest_in_marshall_buffer(
401 struct ctdb_marshall_buffer
*buf
, TDB_DATA key
,
402 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
403 TDB_DATA data
, void *private_data
),
406 struct ctdb_rec_data_old
*rec
= NULL
;
407 struct ctdb_ltdb_header
*h
= NULL
;
416 * Walk the list of records written during this
417 * transaction. If we want to read one we have already
418 * written, return the last written sample. Thus we do not do
419 * a "break;" for the first hit, this record might have been
423 for (i
=0; i
<buf
->count
; i
++) {
427 rec
= db_ctdb_marshall_loop_next_key(buf
, rec
, &tkey
);
432 if (!tdb_data_equal(key
, tkey
)) {
436 if (!db_ctdb_marshall_buf_parse(rec
, &reqid
, &h
, &data
)) {
445 parser(key
, h
, data
, private_data
);
450 struct pull_newest_from_marshall_buffer_state
{
451 struct ctdb_ltdb_header
*pheader
;
456 static void pull_newest_from_marshall_buffer_parser(
457 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
458 TDB_DATA data
, void *private_data
)
460 struct pull_newest_from_marshall_buffer_state
*state
=
461 (struct pull_newest_from_marshall_buffer_state
*)private_data
;
463 if (state
->pheader
!= NULL
) {
464 memcpy(state
->pheader
, header
, sizeof(*state
->pheader
));
466 if (state
->pdata
!= NULL
) {
467 state
->pdata
->dsize
= data
.dsize
;
468 state
->pdata
->dptr
= (uint8_t *)talloc_memdup(
469 state
->mem_ctx
, data
.dptr
, data
.dsize
);
473 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer
*buf
,
475 struct ctdb_ltdb_header
*pheader
,
479 struct pull_newest_from_marshall_buffer_state state
;
481 state
.pheader
= pheader
;
482 state
.mem_ctx
= mem_ctx
;
485 if (!parse_newest_in_marshall_buffer(
486 buf
, key
, pull_newest_from_marshall_buffer_parser
,
490 if ((pdata
!= NULL
) && (pdata
->dsize
!= 0) && (pdata
->dptr
== NULL
)) {
497 static NTSTATUS
db_ctdb_storev_transaction(struct db_record
*rec
,
498 const TDB_DATA
*dbufs
, int num_dbufs
,
500 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
);
502 static struct db_record
*db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx
*ctx
,
506 struct db_record
*result
;
509 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
510 DEBUG(0, ("talloc failed\n"));
514 result
->db
= ctx
->db
;
515 result
->private_data
= ctx
->transaction
;
517 result
->key
.dsize
= key
.dsize
;
518 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
520 if (result
->key
.dptr
== NULL
) {
521 DEBUG(0, ("talloc failed\n"));
526 result
->storev
= db_ctdb_storev_transaction
;
527 result
->delete_rec
= db_ctdb_delete_transaction
;
529 if (pull_newest_from_marshall_buffer(ctx
->transaction
->m_write
, key
,
530 NULL
, result
, &result
->value
)) {
534 ctdb_data
= tdb_fetch(ctx
->wtdb
->tdb
, key
);
535 if (ctdb_data
.dptr
== NULL
) {
536 /* create the record */
537 result
->value
= tdb_null
;
541 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
542 result
->value
.dptr
= NULL
;
544 if ((result
->value
.dsize
!= 0)
545 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
546 result
, ctdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
547 result
->value
.dsize
))) {
548 DEBUG(0, ("talloc failed\n"));
552 SAFE_FREE(ctdb_data
.dptr
);
557 static int db_ctdb_record_destructor(struct db_record
**recp
)
559 struct db_record
*rec
= talloc_get_type_abort(*recp
, struct db_record
);
560 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
561 rec
->private_data
, struct db_ctdb_transaction_handle
);
562 int ret
= h
->ctx
->db
->transaction_commit(h
->ctx
->db
);
564 DEBUG(0,(__location__
" transaction_commit failed\n"));
570 auto-create a transaction for persistent databases
572 static struct db_record
*db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx
*ctx
,
577 struct db_record
*rec
, **recp
;
579 res
= db_ctdb_transaction_start(ctx
->db
);
584 rec
= db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
586 ctx
->db
->transaction_cancel(ctx
->db
);
590 /* destroy this transaction when we release the lock */
591 recp
= talloc(rec
, struct db_record
*);
593 ctx
->db
->transaction_cancel(ctx
->db
);
598 talloc_set_destructor(recp
, db_ctdb_record_destructor
);
604 stores a record inside a transaction
606 static NTSTATUS
db_ctdb_transaction_store(struct db_ctdb_transaction_handle
*h
,
607 TDB_DATA key
, TDB_DATA data
)
609 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
611 struct ctdb_ltdb_header header
;
615 /* we need the header so we can update the RSN */
617 if (!pull_newest_from_marshall_buffer(h
->m_write
, key
, &header
,
620 rec
= tdb_fetch(h
->ctx
->wtdb
->tdb
, key
);
622 if (rec
.dptr
!= NULL
) {
623 memcpy(&header
, rec
.dptr
,
624 sizeof(struct ctdb_ltdb_header
));
625 rec
.dsize
-= sizeof(struct ctdb_ltdb_header
);
628 * a special case, we are writing the same
629 * data that is there now
631 if (data
.dsize
== rec
.dsize
&&
633 rec
.dptr
+ sizeof(struct ctdb_ltdb_header
),
636 talloc_free(tmp_ctx
);
643 header
.dmaster
= get_my_vnn();
646 h
->m_write
= db_ctdb_marshall_add(h
, h
->m_write
, h
->ctx
->db_id
, 0, key
, &header
, data
);
647 if (h
->m_write
== NULL
) {
648 DEBUG(0,(__location__
" Failed to add to marshalling record\n"));
649 talloc_free(tmp_ctx
);
650 return NT_STATUS_NO_MEMORY
;
653 talloc_free(tmp_ctx
);
659 a record store inside a transaction
661 static NTSTATUS
db_ctdb_storev_transaction(
662 struct db_record
*rec
, const TDB_DATA
*dbufs
, int num_dbufs
, int flag
)
664 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
665 rec
->private_data
, struct db_ctdb_transaction_handle
);
669 data
= dbwrap_merge_dbufs(rec
, dbufs
, num_dbufs
);
670 if (data
.dptr
== NULL
) {
671 return NT_STATUS_NO_MEMORY
;
674 status
= db_ctdb_transaction_store(h
, rec
->key
, data
);
676 TALLOC_FREE(data
.dptr
);
682 a record delete inside a transaction
684 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
)
686 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
687 rec
->private_data
, struct db_ctdb_transaction_handle
);
690 status
= db_ctdb_transaction_store(h
, rec
->key
, tdb_null
);
694 static void db_ctdb_fetch_db_seqnum_parser(
695 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
696 TDB_DATA data
, void *private_data
)
698 uint64_t *seqnum
= (uint64_t *)private_data
;
700 if (data
.dsize
!= sizeof(uint64_t)) {
704 memcpy(seqnum
, data
.dptr
, sizeof(*seqnum
));
708 * Fetch the db sequence number of a persistent db directly from the db.
710 static NTSTATUS
db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx
*db
,
716 if (seqnum
== NULL
) {
717 return NT_STATUS_INVALID_PARAMETER
;
720 key
= string_term_tdb_data(CTDB_DB_SEQNUM_KEY
);
722 status
= db_ctdb_ltdb_parse(
723 db
, key
, db_ctdb_fetch_db_seqnum_parser
, seqnum
);
725 if (NT_STATUS_IS_OK(status
)) {
728 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
736 * Store the database sequence number inside a transaction.
738 static NTSTATUS
db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle
*h
,
742 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
746 key
= string_term_tdb_data(keyname
);
748 data
.dptr
= (uint8_t *)&seqnum
;
749 data
.dsize
= sizeof(uint64_t);
751 status
= db_ctdb_transaction_store(h
, key
, data
);
759 static int db_ctdb_transaction_commit(struct db_context
*db
)
761 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
765 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
766 uint64_t old_seqnum
, new_seqnum
;
770 DEBUG(0,(__location__
" transaction commit with no open transaction on db 0x%08x\n", ctx
->db_id
));
774 if (h
->nested_cancel
) {
775 db
->transaction_cancel(db
);
776 DEBUG(5,(__location__
" Failed transaction commit after nested cancel\n"));
780 if (h
->nesting
!= 0) {
782 DEBUG(5, (__location__
" transaction commit on db 0x%08x: nesting %d -> %d\n",
783 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
787 if (h
->m_write
== NULL
) {
789 * No changes were made, so don't change the seqnum,
790 * don't push to other node, just exit with success.
796 DEBUG(5,(__location__
" transaction commit on db 0x%08x\n", ctx
->db_id
));
799 * As the last db action before committing, bump the database sequence
800 * number. Note that this undoes all changes to the seqnum records
801 * performed under the transaction. This record is not meant to be
802 * modified by user interaction. It is for internal use only...
804 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &old_seqnum
);
805 if (!NT_STATUS_IS_OK(rets
)) {
806 DEBUG(1, (__location__
" failed to fetch the db sequence number "
807 "in transaction commit on db 0x%08x\n", ctx
->db_id
));
812 new_seqnum
= old_seqnum
+ 1;
814 rets
= db_ctdb_store_db_seqnum(h
, new_seqnum
);
815 if (!NT_STATUS_IS_OK(rets
)) {
816 DEBUG(1, (__location__
"failed to store the db sequence number "
817 " in transaction commit on db 0x%08x\n", ctx
->db_id
));
823 /* tell ctdbd to commit to the other nodes */
824 ret
= ctdbd_control_local(messaging_ctdb_connection(),
825 CTDB_CONTROL_TRANS3_COMMIT
,
827 db_ctdb_marshall_finish(h
->m_write
),
828 NULL
, NULL
, &status
);
829 if ((ret
!= 0) || status
!= 0) {
831 * The TRANS3_COMMIT control should only possibly fail when a
832 * recovery has been running concurrently. In any case, the db
833 * will be the same on all nodes, either the new copy or the
834 * old copy. This can be detected by comparing the old and new
835 * local sequence numbers.
837 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &new_seqnum
);
838 if (!NT_STATUS_IS_OK(rets
)) {
839 DEBUG(1, (__location__
" failed to refetch db sequence "
840 "number after failed TRANS3_COMMIT\n"));
845 if (new_seqnum
== old_seqnum
) {
846 /* Recovery prevented all our changes: retry. */
849 if (new_seqnum
!= (old_seqnum
+ 1)) {
850 DEBUG(0, (__location__
" ERROR: new_seqnum[%lu] != "
851 "old_seqnum[%lu] + (0 or 1) after failed "
852 "TRANS3_COMMIT - this should not happen!\n",
853 (unsigned long)new_seqnum
,
854 (unsigned long)old_seqnum
));
859 * Recovery propagated our changes to all nodes, completing
860 * our commit for us - succeed.
867 h
->ctx
->transaction
= NULL
;
876 static int db_ctdb_transaction_cancel(struct db_context
*db
)
878 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
880 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
883 DEBUG(0,(__location__
" transaction cancel with no open transaction on db 0x%08x\n", ctx
->db_id
));
887 if (h
->nesting
!= 0) {
889 h
->nested_cancel
= true;
890 DEBUG(5, (__location__
" transaction cancel on db 0x%08x: nesting %d -> %d\n",
891 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
895 DEBUG(5,(__location__
" Cancel transaction on db 0x%08x\n", ctx
->db_id
));
897 ctx
->transaction
= NULL
;
903 static NTSTATUS
db_ctdb_storev(struct db_record
*rec
,
904 const TDB_DATA
*dbufs
, int num_dbufs
, int flag
)
906 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
907 rec
->private_data
, struct db_ctdb_rec
);
910 status
= db_ctdb_ltdb_store(crec
->ctdb_ctx
, rec
->key
, &(crec
->header
),
917 static NTSTATUS
db_ctdb_send_schedule_for_deletion(struct db_record
*rec
)
919 NTSTATUS status
= NT_STATUS_OK
;
921 struct ctdb_control_schedule_for_deletion
*dd
;
924 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
925 rec
->private_data
, struct db_ctdb_rec
);
926 struct db_ctdb_ctx
*ctx
= crec
->ctdb_ctx
;
928 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + rec
->key
.dsize
;
929 indata
.dptr
= talloc_zero_array(crec
, uint8_t, indata
.dsize
);
930 if (indata
.dptr
== NULL
) {
931 DEBUG(0, (__location__
" talloc failed!\n"));
932 return NT_STATUS_NO_MEMORY
;
935 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
936 dd
->db_id
= ctx
->db_id
;
937 dd
->hdr
= crec
->header
;
938 dd
->keylen
= rec
->key
.dsize
;
939 memcpy(dd
->key
, rec
->key
.dptr
, rec
->key
.dsize
);
941 ret
= ctdbd_control_local(messaging_ctdb_connection(),
942 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
943 crec
->ctdb_ctx
->db_id
,
944 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
949 talloc_free(indata
.dptr
);
951 if ((ret
!= 0) || cstatus
!= 0) {
952 DEBUG(1, (__location__
" Error sending local control "
953 "SCHEDULE_FOR_DELETION: %s, cstatus = %"PRIi32
"\n",
954 strerror(ret
), cstatus
));
956 status
= map_nt_error_from_unix(ret
);
958 status
= NT_STATUS_UNSUCCESSFUL
;
965 static NTSTATUS
db_ctdb_delete(struct db_record
*rec
)
970 * We have to store the header with empty data. TODO: Fix the
974 status
= db_ctdb_storev(rec
, &tdb_null
, 1, 0);
975 if (!NT_STATUS_IS_OK(status
)) {
979 status
= db_ctdb_send_schedule_for_deletion(rec
);
983 static int db_ctdb_record_destr(struct db_record
* data
)
985 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
986 data
->private_data
, struct db_ctdb_rec
);
989 struct timeval before
;
992 DEBUG(10, (DEBUGLEVEL
> 10
993 ? "Unlocking db %u key %s\n"
994 : "Unlocking db %u key %.20s\n",
995 (int)crec
->ctdb_ctx
->db_id
,
996 hex_encode_talloc(data
, (unsigned char *)data
->key
.dptr
,
999 before
= timeval_current();
1001 ret
= tdb_chainunlock(crec
->ctdb_ctx
->wtdb
->tdb
, data
->key
);
1003 timediff
= timeval_elapsed(&before
);
1004 timediff
*= 1000; /* get us milliseconds */
1006 if (timediff
> crec
->ctdb_ctx
->warn_unlock_msecs
) {
1008 key
= hex_encode_talloc(talloc_tos(),
1009 (unsigned char *)data
->key
.dptr
,
1011 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
1012 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
), key
,
1018 DEBUG(0, ("tdb_chainunlock failed\n"));
1022 threshold
= crec
->ctdb_ctx
->warn_locktime_msecs
;
1023 if (threshold
!= 0) {
1024 timediff
= timeval_elapsed(&crec
->lock_time
) * 1000;
1025 if (timediff
> threshold
) {
1028 key
= hex_encode_talloc(data
,
1029 (unsigned char *)data
->key
.dptr
,
1031 DEBUG(0, ("Held tdb lock on db %s, key %s "
1032 "%f milliseconds\n",
1033 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
),
1042 * Check whether we have a valid local copy of the given record,
1043 * either for reading or for writing.
1045 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header
*hdr
,
1046 uint32_t my_vnn
, bool read_only
)
1048 if (hdr
->dmaster
!= my_vnn
) {
1049 /* If we're not dmaster, it must be r/o copy. */
1050 return read_only
&& (hdr
->flags
& CTDB_REC_RO_HAVE_READONLY
);
1054 * If we want write access, no one may have r/o copies.
1056 return read_only
|| !(hdr
->flags
& CTDB_REC_RO_HAVE_DELEGATIONS
);
1059 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data
, uint32_t my_vnn
,
1062 if (ctdb_data
.dptr
== NULL
) {
1066 if (ctdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1070 return db_ctdb_can_use_local_hdr(
1071 (struct ctdb_ltdb_header
*)ctdb_data
.dptr
, my_vnn
, read_only
);
1074 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
1075 TALLOC_CTX
*mem_ctx
,
1079 struct db_record
*result
;
1080 struct db_ctdb_rec
*crec
;
1082 int migrate_attempts
;
1083 struct timeval migrate_start
;
1084 struct timeval chainlock_start
;
1085 struct timeval ctdb_start_time
;
1086 double chainlock_time
= 0;
1087 double ctdb_time
= 0;
1092 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
1093 DEBUG(0, ("talloc failed\n"));
1097 if (!(crec
= talloc_zero(result
, struct db_ctdb_rec
))) {
1098 DEBUG(0, ("talloc failed\n"));
1099 TALLOC_FREE(result
);
1103 result
->db
= ctx
->db
;
1104 result
->private_data
= (void *)crec
;
1105 crec
->ctdb_ctx
= ctx
;
1107 result
->key
.dsize
= key
.dsize
;
1108 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
1110 if (result
->key
.dptr
== NULL
) {
1111 DEBUG(0, ("talloc failed\n"));
1112 TALLOC_FREE(result
);
1116 migrate_attempts
= 0;
1117 GetTimeOfDay(&migrate_start
);
1120 * Do a blocking lock on the record
1124 if (DEBUGLEVEL
>= 10) {
1125 char *keystr
= hex_encode_talloc(result
, key
.dptr
, key
.dsize
);
1126 DEBUG(10, (DEBUGLEVEL
> 10
1127 ? "Locking db %u key %s\n"
1128 : "Locking db %u key %.20s\n",
1129 (int)crec
->ctdb_ctx
->db_id
, keystr
));
1130 TALLOC_FREE(keystr
);
1133 GetTimeOfDay(&chainlock_start
);
1135 ? tdb_chainlock_nonblock(ctx
->wtdb
->tdb
, key
)
1136 : tdb_chainlock(ctx
->wtdb
->tdb
, key
);
1137 chainlock_time
+= timeval_elapsed(&chainlock_start
);
1140 DEBUG(3, ("tdb_chainlock failed\n"));
1141 TALLOC_FREE(result
);
1145 result
->storev
= db_ctdb_storev
;
1146 result
->delete_rec
= db_ctdb_delete
;
1147 talloc_set_destructor(result
, db_ctdb_record_destr
);
1149 ctdb_data
= tdb_fetch(ctx
->wtdb
->tdb
, key
);
1152 * See if we have a valid record and we are the dmaster. If so, we can
1153 * take the shortcut and just return it.
1156 if (!db_ctdb_can_use_local_copy(ctdb_data
, get_my_vnn(), false)) {
1157 SAFE_FREE(ctdb_data
.dptr
);
1158 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
1159 talloc_set_destructor(result
, NULL
);
1161 if (tryonly
&& (migrate_attempts
!= 0)) {
1162 DEBUG(5, ("record migrated away again\n"));
1163 TALLOC_FREE(result
);
1167 migrate_attempts
+= 1;
1169 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %"PRIu32
" "
1170 "(%"PRIu32
") %"PRIu32
"\n",
1171 ctdb_data
.dptr
, ctdb_data
.dptr
?
1172 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
:
1176 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->flags
: 0));
1178 GetTimeOfDay(&ctdb_start_time
);
1179 ret
= ctdbd_migrate(messaging_ctdb_connection(), ctx
->db_id
,
1181 ctdb_time
+= timeval_elapsed(&ctdb_start_time
);
1184 DEBUG(5, ("ctdbd_migrate failed: %s\n",
1186 TALLOC_FREE(result
);
1189 /* now its migrated, try again */
1195 duration
= timeval_elapsed(&migrate_start
);
1198 * Convert the duration to milliseconds to avoid a
1199 * floating-point division of
1200 * lp_parm_int("migrate_duration") by 1000.
1202 duration_msecs
= duration
* 1000;
1205 if ((migrate_attempts
> ctx
->warn_migrate_attempts
) ||
1206 (duration_msecs
> ctx
->warn_migrate_msecs
)) {
1209 if (tdb_get_flags(ctx
->wtdb
->tdb
) & TDB_INCOMPATIBLE_HASH
) {
1210 chain
= tdb_jenkins_hash(&key
) %
1211 tdb_hash_size(ctx
->wtdb
->tdb
);
1214 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1215 "needed %d attempts, %d milliseconds, "
1216 "chainlock: %f ms, CTDB %f ms\n",
1217 tdb_name(ctx
->wtdb
->tdb
),
1218 hex_encode_talloc(talloc_tos(),
1219 (unsigned char *)key
.dptr
,
1222 migrate_attempts
, duration_msecs
,
1223 chainlock_time
* 1000.0,
1224 ctdb_time
* 1000.0));
1227 GetTimeOfDay(&crec
->lock_time
);
1229 memcpy(&crec
->header
, ctdb_data
.dptr
, sizeof(crec
->header
));
1231 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(crec
->header
);
1232 result
->value
.dptr
= NULL
;
1234 if ((result
->value
.dsize
!= 0)
1235 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
1236 result
, ctdb_data
.dptr
+ sizeof(crec
->header
),
1237 result
->value
.dsize
))) {
1238 DEBUG(0, ("talloc failed\n"));
1239 TALLOC_FREE(result
);
1242 SAFE_FREE(ctdb_data
.dptr
);
1247 static struct db_record
*db_ctdb_fetch_locked(struct db_context
*db
,
1248 TALLOC_CTX
*mem_ctx
,
1251 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1252 struct db_ctdb_ctx
);
1254 if (ctx
->transaction
!= NULL
) {
1255 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1258 if (db
->persistent
) {
1259 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1262 return fetch_locked_internal(ctx
, mem_ctx
, key
, false);
1265 static struct db_record
*db_ctdb_try_fetch_locked(struct db_context
*db
,
1266 TALLOC_CTX
*mem_ctx
,
1269 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1270 struct db_ctdb_ctx
);
1272 if (ctx
->transaction
!= NULL
) {
1273 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1276 if (db
->persistent
) {
1277 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1280 return fetch_locked_internal(ctx
, mem_ctx
, key
, true);
1283 struct db_ctdb_parse_record_state
{
1284 void (*parser
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1287 bool ask_for_readonly_copy
;
1292 static void db_ctdb_parse_record_parser(
1293 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1294 TDB_DATA data
, void *private_data
)
1296 struct db_ctdb_parse_record_state
*state
=
1297 (struct db_ctdb_parse_record_state
*)private_data
;
1298 state
->parser(key
, data
, state
->private_data
);
1301 static void db_ctdb_parse_record_parser_nonpersistent(
1302 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1303 TDB_DATA data
, void *private_data
)
1305 struct db_ctdb_parse_record_state
*state
=
1306 (struct db_ctdb_parse_record_state
*)private_data
;
1308 if (db_ctdb_can_use_local_hdr(header
, state
->my_vnn
, true)) {
1310 * A record consisting only of the ctdb header can be
1311 * a validly created empty record or a tombstone
1312 * record of a deleted record (not vacuumed yet). Mark
1315 state
->empty_record
= (data
.dsize
== 0);
1316 if (!state
->empty_record
) {
1317 state
->parser(key
, data
, state
->private_data
);
1322 * We found something in the db, so it seems that this record,
1323 * while not usable locally right now, is popular. Ask for a
1326 state
->ask_for_readonly_copy
= true;
1330 static NTSTATUS
db_ctdb_try_parse_local_record(struct db_ctdb_ctx
*ctx
,
1332 struct db_ctdb_parse_record_state
*state
)
1336 if (ctx
->transaction
!= NULL
) {
1337 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
1341 * Transactions only happen for persistent db's.
1344 found
= parse_newest_in_marshall_buffer(
1345 h
->m_write
, key
, db_ctdb_parse_record_parser
, state
);
1348 return NT_STATUS_OK
;
1352 if (ctx
->db
->persistent
) {
1354 * Persistent db, but not found in the transaction buffer
1356 return db_ctdb_ltdb_parse(
1357 ctx
, key
, db_ctdb_parse_record_parser
, state
);
1360 state
->done
= false;
1361 state
->ask_for_readonly_copy
= false;
1363 status
= db_ctdb_ltdb_parse(
1364 ctx
, key
, db_ctdb_parse_record_parser_nonpersistent
, state
);
1365 if (NT_STATUS_IS_OK(status
) && state
->done
) {
1366 if (state
->empty_record
) {
1368 * We know authoritatively, that this is an empty
1369 * record. Since ctdb does not distinguish between empty
1370 * and deleted records, this can be a record stored as
1371 * empty or a not-yet-vacuumed tombstone record of a
1372 * deleted record. Now Samba right now can live without
1373 * empty records, so we can safely report this record
1376 * See bugs 10008 and 12005.
1378 return NT_STATUS_NOT_FOUND
;
1380 return NT_STATUS_OK
;
1383 return NT_STATUS_MORE_PROCESSING_REQUIRED
;
1386 static NTSTATUS
db_ctdb_parse_record(struct db_context
*db
, TDB_DATA key
,
1387 void (*parser
)(TDB_DATA key
,
1389 void *private_data
),
1392 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1393 db
->private_data
, struct db_ctdb_ctx
);
1394 struct db_ctdb_parse_record_state state
;
1398 state
.parser
= parser
;
1399 state
.private_data
= private_data
;
1400 state
.my_vnn
= get_my_vnn();
1401 state
.empty_record
= false;
1403 status
= db_ctdb_try_parse_local_record(ctx
, key
, &state
);
1404 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1408 ret
= ctdbd_parse(messaging_ctdb_connection(), ctx
->db_id
, key
,
1409 state
.ask_for_readonly_copy
, parser
, private_data
);
1411 if (ret
== ENOENT
) {
1414 * NT_STATUS_OBJECT_NAME_NOT_FOUND. Our upper
1415 * layers expect NT_STATUS_NOT_FOUND for "no
1416 * record around". We need to convert dbwrap
1417 * to 0/errno away from NTSTATUS ... :-)
1419 return NT_STATUS_NOT_FOUND
;
1421 return map_nt_error_from_unix(ret
);
1423 return NT_STATUS_OK
;
1426 static void db_ctdb_parse_record_done(struct tevent_req
*subreq
);
1428 static struct tevent_req
*db_ctdb_parse_record_send(
1429 TALLOC_CTX
*mem_ctx
,
1430 struct tevent_context
*ev
,
1431 struct db_context
*db
,
1433 void (*parser
)(TDB_DATA key
,
1435 void *private_data
),
1437 enum dbwrap_req_state
*req_state
)
1439 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1440 db
->private_data
, struct db_ctdb_ctx
);
1441 struct tevent_req
*req
= NULL
;
1442 struct tevent_req
*subreq
= NULL
;
1443 struct db_ctdb_parse_record_state
*state
= NULL
;
1446 req
= tevent_req_create(mem_ctx
, &state
,
1447 struct db_ctdb_parse_record_state
);
1449 *req_state
= DBWRAP_REQ_ERROR
;
1454 *state
= (struct db_ctdb_parse_record_state
) {
1456 .private_data
= private_data
,
1457 .my_vnn
= get_my_vnn(),
1458 .empty_record
= false,
1461 status
= db_ctdb_try_parse_local_record(ctx
, key
, state
);
1462 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
1463 if (tevent_req_nterror(req
, status
)) {
1464 *req_state
= DBWRAP_REQ_ERROR
;
1465 return tevent_req_post(req
, ev
);
1467 *req_state
= DBWRAP_REQ_DONE
;
1468 tevent_req_done(req
);
1469 return tevent_req_post(req
, ev
);
1472 subreq
= ctdbd_parse_send(state
,
1474 ctdb_async_ctx
.async_conn
,
1477 state
->ask_for_readonly_copy
,
1481 if (tevent_req_nomem(subreq
, req
)) {
1482 *req_state
= DBWRAP_REQ_ERROR
;
1483 return tevent_req_post(req
, ev
);
1485 tevent_req_set_callback(subreq
, db_ctdb_parse_record_done
, req
);
1490 static void db_ctdb_parse_record_done(struct tevent_req
*subreq
)
1492 struct tevent_req
*req
= tevent_req_callback_data(
1493 subreq
, struct tevent_req
);
1496 ret
= ctdbd_parse_recv(subreq
);
1497 TALLOC_FREE(subreq
);
1499 if (ret
== ENOENT
) {
1501 * This maps to NT_STATUS_OBJECT_NAME_NOT_FOUND. Our
1502 * upper layers expect NT_STATUS_NOT_FOUND for "no
1503 * record around". We need to convert dbwrap to 0/errno
1504 * away from NTSTATUS ... :-)
1506 tevent_req_nterror(req
, NT_STATUS_NOT_FOUND
);
1509 tevent_req_nterror(req
, map_nt_error_from_unix(ret
));
1513 tevent_req_done(req
);
1517 static NTSTATUS
db_ctdb_parse_record_recv(struct tevent_req
*req
)
1519 return tevent_req_simple_recv_ntstatus(req
);
1522 struct traverse_state
{
1523 struct db_context
*db
;
1524 int (*fn
)(struct db_record
*rec
, void *private_data
);
1529 static void traverse_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1531 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1532 struct db_record
*rec
;
1533 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1534 /* we have to give them a locked record to prevent races */
1535 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, key
);
1536 if (rec
&& rec
->value
.dsize
> 0) {
1537 state
->fn(rec
, state
->private_data
);
1539 talloc_free(tmp_ctx
);
1542 static int traverse_persistent_callback(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1545 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1546 struct db_record
*rec
;
1547 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1551 * Skip the __db_sequence_number__ key:
1552 * This is used for persistent transactions internally.
1554 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1555 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1560 /* we have to give them a locked record to prevent races */
1561 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, kbuf
);
1562 if (rec
&& rec
->value
.dsize
> 0) {
1563 ret
= state
->fn(rec
, state
->private_data
);
1567 talloc_free(tmp_ctx
);
1571 /* wrapper to use traverse_persistent_callback with dbwrap */
1572 static int traverse_persistent_callback_dbwrap(struct db_record
*rec
, void* data
)
1574 return traverse_persistent_callback(NULL
, rec
->key
, rec
->value
, data
);
1577 static int db_ctdbd_traverse(uint32_t db_id
,
1578 void (*fn
)(TDB_DATA key
, TDB_DATA data
,
1579 void *private_data
),
1582 struct ctdbd_connection
*conn
;
1586 ret
= ctdbd_init_connection(talloc_tos(), lp_ctdbd_socket(),
1587 lp_ctdb_timeout(), &conn
);
1590 DBG_WARNING("ctdbd_init_connection failed: %s\n",
1595 ret
= ctdbd_traverse(conn
, db_id
, fn
, private_data
);
1599 DBG_WARNING("ctdbd_traverse failed: %s\n",
1608 static int db_ctdb_traverse(struct db_context
*db
,
1609 int (*fn
)(struct db_record
*rec
,
1610 void *private_data
),
1614 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1615 struct db_ctdb_ctx
);
1616 struct traverse_state state
;
1620 state
.private_data
= private_data
;
1623 if (db
->persistent
) {
1624 struct tdb_context
*ltdb
= ctx
->wtdb
->tdb
;
1626 /* for persistent databases we don't need to do a ctdb traverse,
1627 we can do a faster local traverse */
1628 ret
= tdb_traverse(ltdb
, traverse_persistent_callback
, &state
);
1632 if (ctx
->transaction
&& ctx
->transaction
->m_write
) {
1634 * we now have to handle keys not yet
1635 * present at transaction start
1637 struct db_context
*newkeys
= db_open_rbt(talloc_tos());
1638 struct ctdb_marshall_buffer
*mbuf
= ctx
->transaction
->m_write
;
1639 struct ctdb_rec_data_old
*rec
=NULL
;
1644 if (newkeys
== NULL
) {
1648 for (i
=0; i
<mbuf
->count
; i
++) {
1650 rec
= db_ctdb_marshall_loop_next_key(
1652 SMB_ASSERT(rec
!= NULL
);
1654 if (!tdb_exists(ltdb
, key
)) {
1655 dbwrap_store(newkeys
, key
, tdb_null
, 0);
1658 status
= dbwrap_traverse(newkeys
,
1659 traverse_persistent_callback_dbwrap
,
1662 talloc_free(newkeys
);
1663 if (!NT_STATUS_IS_OK(status
)) {
1671 ret
= db_ctdbd_traverse(ctx
->db_id
, traverse_callback
, &state
);
1678 static NTSTATUS
db_ctdb_storev_deny(struct db_record
*rec
,
1679 const TDB_DATA
*dbufs
, int num_dbufs
, int flag
)
1681 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1684 static NTSTATUS
db_ctdb_delete_deny(struct db_record
*rec
)
1686 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1689 static void traverse_read_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1691 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1692 struct db_record rec
;
1698 rec
.storev
= db_ctdb_storev_deny
;
1699 rec
.delete_rec
= db_ctdb_delete_deny
;
1700 rec
.private_data
= NULL
;
1701 state
->fn(&rec
, state
->private_data
);
1705 static int traverse_persistent_callback_read(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1708 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1709 struct db_record rec
;
1712 * Skip the __db_sequence_number__ key:
1713 * This is used for persistent transactions internally.
1715 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1716 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1725 rec
.storev
= db_ctdb_storev_deny
;
1726 rec
.delete_rec
= db_ctdb_delete_deny
;
1727 rec
.private_data
= NULL
;
1729 if (rec
.value
.dsize
<= sizeof(struct ctdb_ltdb_header
)) {
1730 /* a deleted record */
1733 rec
.value
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1734 rec
.value
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1737 return state
->fn(&rec
, state
->private_data
);
1740 static int db_ctdb_traverse_read(struct db_context
*db
,
1741 int (*fn
)(struct db_record
*rec
,
1742 void *private_data
),
1746 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1747 struct db_ctdb_ctx
);
1748 struct traverse_state state
;
1752 state
.private_data
= private_data
;
1755 if (db
->persistent
) {
1756 /* for persistent databases we don't need to do a ctdb traverse,
1757 we can do a faster local traverse */
1758 return tdb_traverse_read(ctx
->wtdb
->tdb
, traverse_persistent_callback_read
, &state
);
1761 ret
= db_ctdbd_traverse(ctx
->db_id
, traverse_read_callback
, &state
);
1768 static int db_ctdb_get_seqnum(struct db_context
*db
)
1770 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1771 struct db_ctdb_ctx
);
1772 return tdb_get_seqnum(ctx
->wtdb
->tdb
);
1775 static size_t db_ctdb_id(struct db_context
*db
, uint8_t *id
, size_t idlen
)
1777 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1778 db
->private_data
, struct db_ctdb_ctx
);
1780 if (idlen
>= sizeof(ctx
->db_id
)) {
1781 memcpy(id
, &ctx
->db_id
, sizeof(ctx
->db_id
));
1784 return sizeof(ctx
->db_id
);
1787 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1788 struct messaging_context
*msg_ctx
,
1790 int hash_size
, int tdb_flags
,
1791 int open_flags
, mode_t mode
,
1792 enum dbwrap_lock_order lock_order
,
1793 uint64_t dbwrap_flags
)
1795 struct db_context
*result
;
1796 struct db_ctdb_ctx
*db_ctdb
;
1798 struct loadparm_context
*lp_ctx
;
1800 bool persistent
= (tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0;
1804 if (!lp_clustering()) {
1805 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1809 if (!(result
= talloc_zero(mem_ctx
, struct db_context
))) {
1810 DEBUG(0, ("talloc failed\n"));
1811 TALLOC_FREE(result
);
1815 if (!(db_ctdb
= talloc(result
, struct db_ctdb_ctx
))) {
1816 DEBUG(0, ("talloc failed\n"));
1817 TALLOC_FREE(result
);
1821 result
->name
= talloc_strdup(result
, name
);
1822 if (result
->name
== NULL
) {
1823 DEBUG(0, ("talloc failed\n"));
1824 TALLOC_FREE(result
);
1828 db_ctdb
->transaction
= NULL
;
1829 db_ctdb
->db
= result
;
1831 ret
= ctdbd_db_attach(messaging_ctdb_connection(), name
,
1832 &db_ctdb
->db_id
, persistent
);
1834 DEBUG(0, ("ctdbd_db_attach failed for %s: %s\n", name
,
1836 TALLOC_FREE(result
);
1840 if (tdb_flags
& TDB_SEQNUM
) {
1841 data
.dptr
= (uint8_t *)&db_ctdb
->db_id
;
1842 data
.dsize
= sizeof(db_ctdb
->db_id
);
1844 ret
= ctdbd_control_local(messaging_ctdb_connection(),
1845 CTDB_CONTROL_ENABLE_SEQNUM
,
1847 NULL
, NULL
, &cstatus
);
1848 if ((ret
!= 0) || cstatus
!= 0) {
1849 DBG_ERR("ctdb_control for enable seqnum "
1850 "failed: %s\n", strerror(ret
));
1851 TALLOC_FREE(result
);
1856 db_path
= ctdbd_dbpath(messaging_ctdb_connection(), db_ctdb
,
1859 result
->persistent
= persistent
;
1860 result
->lock_order
= lock_order
;
1862 data
.dptr
= (uint8_t *)&db_ctdb
->db_id
;
1863 data
.dsize
= sizeof(db_ctdb
->db_id
);
1865 ret
= ctdbd_control_local(messaging_ctdb_connection(),
1866 CTDB_CONTROL_DB_OPEN_FLAGS
,
1867 0, 0, data
, NULL
, &data
, &cstatus
);
1869 DBG_ERR(" ctdb control for db_open_flags "
1870 "failed: %s\n", strerror(ret
));
1871 TALLOC_FREE(result
);
1875 if (cstatus
!= 0 || data
.dsize
!= sizeof(int)) {
1876 DBG_ERR("ctdb_control for db_open_flags failed\n");
1877 TALLOC_FREE(result
);
1881 tdb_flags
= *(int *)data
.dptr
;
1883 if (!result
->persistent
) {
1884 ret
= ctdb_async_ctx_init(NULL
, messaging_tevent_context(msg_ctx
));
1886 DBG_ERR("ctdb_async_ctx_init failed: %s\n", strerror(ret
));
1887 TALLOC_FREE(result
);
1892 if (!result
->persistent
&&
1893 (dbwrap_flags
& DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS
))
1897 indata
= make_tdb_data((uint8_t *)&db_ctdb
->db_id
,
1898 sizeof(db_ctdb
->db_id
));
1900 ret
= ctdbd_control_local(
1901 messaging_ctdb_connection(),
1902 CTDB_CONTROL_SET_DB_READONLY
, 0, 0,
1903 indata
, NULL
, NULL
, &cstatus
);
1904 if ((ret
!= 0) || (cstatus
!= 0)) {
1905 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1906 "%s, %"PRIi32
"\n", strerror(ret
), cstatus
));
1907 TALLOC_FREE(result
);
1912 lp_ctx
= loadparm_init_s3(db_path
, loadparm_s3_helpers());
1914 if (hash_size
== 0) {
1915 hash_size
= lpcfg_tdb_hash_size(lp_ctx
, db_path
);
1918 db_ctdb
->wtdb
= tdb_wrap_open(db_ctdb
, db_path
, hash_size
,
1919 lpcfg_tdb_flags(lp_ctx
, tdb_flags
),
1921 talloc_unlink(db_path
, lp_ctx
);
1922 if (db_ctdb
->wtdb
== NULL
) {
1923 DEBUG(0, ("Could not open tdb %s: %s\n", db_path
, strerror(errno
)));
1924 TALLOC_FREE(result
);
1927 talloc_free(db_path
);
1929 /* honor permissions if user has specified O_CREAT */
1930 if (open_flags
& O_CREAT
) {
1932 fd
= tdb_fd(db_ctdb
->wtdb
->tdb
);
1933 ret
= fchmod(fd
, mode
);
1935 DBG_WARNING("fchmod failed: %s\n",
1937 TALLOC_FREE(result
);
1942 if (result
->persistent
) {
1943 db_ctdb
->lock_ctx
= g_lock_ctx_init(db_ctdb
, msg_ctx
);
1944 if (db_ctdb
->lock_ctx
== NULL
) {
1945 DEBUG(0, ("g_lock_ctx_init failed\n"));
1946 TALLOC_FREE(result
);
1951 db_ctdb
->warn_unlock_msecs
= lp_parm_int(-1, "ctdb",
1952 "unlock_warn_threshold", 5);
1953 db_ctdb
->warn_migrate_attempts
= lp_parm_int(-1, "ctdb",
1954 "migrate_attempts", 10);
1955 db_ctdb
->warn_migrate_msecs
= lp_parm_int(-1, "ctdb",
1956 "migrate_duration", 5000);
1957 db_ctdb
->warn_locktime_msecs
= lp_ctdb_locktime_warn_threshold();
1959 result
->private_data
= (void *)db_ctdb
;
1960 result
->fetch_locked
= db_ctdb_fetch_locked
;
1961 result
->try_fetch_locked
= db_ctdb_try_fetch_locked
;
1962 result
->parse_record
= db_ctdb_parse_record
;
1963 result
->parse_record_send
= db_ctdb_parse_record_send
;
1964 result
->parse_record_recv
= db_ctdb_parse_record_recv
;
1965 result
->traverse
= db_ctdb_traverse
;
1966 result
->traverse_read
= db_ctdb_traverse_read
;
1967 result
->get_seqnum
= db_ctdb_get_seqnum
;
1968 result
->transaction_start
= db_ctdb_transaction_start
;
1969 result
->transaction_commit
= db_ctdb_transaction_commit
;
1970 result
->transaction_cancel
= db_ctdb_transaction_cancel
;
1971 result
->id
= db_ctdb_id
;
1973 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1974 name
, db_ctdb
->db_id
));