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"
31 #include "ctdb_private.h"
32 #include "ctdbd_conn.h"
33 #include "dbwrap/dbwrap.h"
34 #include "dbwrap/dbwrap_private.h"
35 #include "dbwrap/dbwrap_ctdb.h"
39 struct db_ctdb_transaction_handle
{
40 struct db_ctdb_ctx
*ctx
;
42 * we store the writes done under a transaction:
44 struct ctdb_marshall_buffer
*m_write
;
51 struct db_context
*db
;
52 struct tdb_wrap
*wtdb
;
54 struct db_ctdb_transaction_handle
*transaction
;
55 struct g_lock_ctx
*lock_ctx
;
57 /* thresholds for warning messages */
58 int warn_unlock_msecs
;
59 int warn_migrate_msecs
;
60 int warn_migrate_attempts
;
61 int warn_locktime_msecs
;
65 struct db_ctdb_ctx
*ctdb_ctx
;
66 struct ctdb_ltdb_header header
;
67 struct timeval lock_time
;
70 static NTSTATUS
tdb_error_to_ntstatus(struct tdb_context
*tdb
)
72 enum TDB_ERROR tret
= tdb_error(tdb
);
74 return map_nt_error_from_tdb(tret
);
77 struct db_ctdb_ltdb_parse_state
{
78 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
79 TDB_DATA data
, void *private_data
);
83 static int db_ctdb_ltdb_parser(TDB_DATA key
, TDB_DATA data
,
86 struct db_ctdb_ltdb_parse_state
*state
=
87 (struct db_ctdb_ltdb_parse_state
*)private_data
;
89 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
94 key
, (struct ctdb_ltdb_header
*)data
.dptr
,
95 make_tdb_data(data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
96 data
.dsize
- sizeof(struct ctdb_ltdb_header
)),
101 static NTSTATUS
db_ctdb_ltdb_parse(
102 struct db_ctdb_ctx
*db
, TDB_DATA key
,
103 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
104 TDB_DATA data
, void *private_data
),
107 struct db_ctdb_ltdb_parse_state state
;
110 state
.parser
= parser
;
111 state
.private_data
= private_data
;
113 ret
= tdb_parse_record(db
->wtdb
->tdb
, key
, db_ctdb_ltdb_parser
,
116 return NT_STATUS_NOT_FOUND
;
122 * Store a record together with the ctdb record header
123 * in the local copy of the database.
125 static NTSTATUS
db_ctdb_ltdb_store(struct db_ctdb_ctx
*db
,
127 struct ctdb_ltdb_header
*header
,
133 rec
.dsize
= data
.dsize
+ sizeof(struct ctdb_ltdb_header
);
134 rec
.dptr
= (uint8_t *)talloc_size(talloc_tos(), rec
.dsize
);
136 if (rec
.dptr
== NULL
) {
137 return NT_STATUS_NO_MEMORY
;
140 memcpy(rec
.dptr
, header
, sizeof(struct ctdb_ltdb_header
));
141 memcpy(sizeof(struct ctdb_ltdb_header
) + (uint8_t *)rec
.dptr
, data
.dptr
, data
.dsize
);
143 ret
= tdb_store(db
->wtdb
->tdb
, key
, rec
, TDB_REPLACE
);
145 talloc_free(rec
.dptr
);
147 return (ret
== 0) ? NT_STATUS_OK
148 : tdb_error_to_ntstatus(db
->wtdb
->tdb
);
153 form a ctdb_rec_data record from a key/data pair
155 static struct ctdb_rec_data
*db_ctdb_marshall_record(TALLOC_CTX
*mem_ctx
, uint32_t reqid
,
157 struct ctdb_ltdb_header
*header
,
161 struct ctdb_rec_data
*d
;
163 length
= offsetof(struct ctdb_rec_data
, data
) + key
.dsize
+
164 data
.dsize
+ sizeof(*header
);
165 d
= (struct ctdb_rec_data
*)talloc_size(mem_ctx
, length
);
171 d
->keylen
= key
.dsize
;
172 memcpy(&d
->data
[0], key
.dptr
, key
.dsize
);
174 d
->datalen
= data
.dsize
+ sizeof(*header
);
175 memcpy(&d
->data
[key
.dsize
], header
, sizeof(*header
));
176 memcpy(&d
->data
[key
.dsize
+sizeof(*header
)], data
.dptr
, data
.dsize
);
181 /* helper function for marshalling multiple records */
182 static struct ctdb_marshall_buffer
*db_ctdb_marshall_add(TALLOC_CTX
*mem_ctx
,
183 struct ctdb_marshall_buffer
*m
,
187 struct ctdb_ltdb_header
*header
,
190 struct ctdb_rec_data
*r
;
191 size_t m_size
, r_size
;
192 struct ctdb_marshall_buffer
*m2
= NULL
;
194 r
= db_ctdb_marshall_record(talloc_tos(), reqid
, key
, header
, data
);
201 m
= (struct ctdb_marshall_buffer
*)talloc_zero_size(
202 mem_ctx
, offsetof(struct ctdb_marshall_buffer
, data
));
209 m_size
= talloc_get_size(m
);
210 r_size
= talloc_get_size(r
);
212 m2
= (struct ctdb_marshall_buffer
*)talloc_realloc_size(
213 mem_ctx
, m
, m_size
+ r_size
);
219 memcpy(m_size
+ (uint8_t *)m2
, r
, r_size
);
228 /* we've finished marshalling, return a data blob with the marshalled records */
229 static TDB_DATA
db_ctdb_marshall_finish(struct ctdb_marshall_buffer
*m
)
232 data
.dptr
= (uint8_t *)m
;
233 data
.dsize
= talloc_get_size(m
);
238 loop over a marshalling buffer
240 - pass r==NULL to start
241 - loop the number of times indicated by m->count
243 static struct ctdb_rec_data
*db_ctdb_marshall_loop_next_key(
244 struct ctdb_marshall_buffer
*m
, struct ctdb_rec_data
*r
, TDB_DATA
*key
)
247 r
= (struct ctdb_rec_data
*)&m
->data
[0];
249 r
= (struct ctdb_rec_data
*)(r
->length
+ (uint8_t *)r
);
252 key
->dptr
= &r
->data
[0];
253 key
->dsize
= r
->keylen
;
257 static bool db_ctdb_marshall_buf_parse(
258 struct ctdb_rec_data
*r
, uint32_t *reqid
,
259 struct ctdb_ltdb_header
**header
, TDB_DATA
*data
)
261 if (r
->datalen
< sizeof(struct ctdb_ltdb_header
)) {
267 data
->dptr
= &r
->data
[r
->keylen
] + sizeof(struct ctdb_ltdb_header
);
268 data
->dsize
= r
->datalen
- sizeof(struct ctdb_ltdb_header
);
270 *header
= (struct ctdb_ltdb_header
*)&r
->data
[r
->keylen
];
276 * CTDB transaction destructor
278 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle
*h
)
282 status
= g_lock_unlock(h
->ctx
->lock_ctx
, h
->lock_name
);
283 if (!NT_STATUS_IS_OK(status
)) {
284 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h
->lock_name
,
292 * CTDB dbwrap API: transaction_start function
293 * starts a transaction on a persistent database
295 static int db_ctdb_transaction_start(struct db_context
*db
)
297 struct db_ctdb_transaction_handle
*h
;
299 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
302 if (!db
->persistent
) {
303 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
308 if (ctx
->transaction
) {
309 ctx
->transaction
->nesting
++;
310 DEBUG(5, (__location__
" transaction start on db 0x%08x: nesting %d -> %d\n",
311 ctx
->db_id
, ctx
->transaction
->nesting
- 1, ctx
->transaction
->nesting
));
315 h
= talloc_zero(db
, struct db_ctdb_transaction_handle
);
317 DEBUG(0,(__location__
" oom for transaction handle\n"));
323 h
->lock_name
= talloc_asprintf(h
, "transaction_db_0x%08x",
324 (unsigned int)ctx
->db_id
);
325 if (h
->lock_name
== NULL
) {
326 DEBUG(0, ("talloc_asprintf failed\n"));
332 * Wait a day, i.e. forever...
334 status
= g_lock_lock(ctx
->lock_ctx
, h
->lock_name
, G_LOCK_WRITE
,
335 timeval_set(86400, 0));
336 if (!NT_STATUS_IS_OK(status
)) {
337 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status
)));
342 talloc_set_destructor(h
, db_ctdb_transaction_destructor
);
344 ctx
->transaction
= h
;
346 DEBUG(5,(__location__
" transaction started on db 0x%08x\n", ctx
->db_id
));
351 static bool parse_newest_in_marshall_buffer(
352 struct ctdb_marshall_buffer
*buf
, TDB_DATA key
,
353 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
354 TDB_DATA data
, void *private_data
),
357 struct ctdb_rec_data
*rec
= NULL
;
358 struct ctdb_ltdb_header
*h
= NULL
;
367 * Walk the list of records written during this
368 * transaction. If we want to read one we have already
369 * written, return the last written sample. Thus we do not do
370 * a "break;" for the first hit, this record might have been
374 for (i
=0; i
<buf
->count
; i
++) {
378 rec
= db_ctdb_marshall_loop_next_key(buf
, rec
, &tkey
);
383 if (!tdb_data_equal(key
, tkey
)) {
387 if (!db_ctdb_marshall_buf_parse(rec
, &reqid
, &h
, &data
)) {
396 parser(key
, h
, data
, private_data
);
401 struct pull_newest_from_marshall_buffer_state
{
402 struct ctdb_ltdb_header
*pheader
;
407 static void pull_newest_from_marshall_buffer_parser(
408 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
409 TDB_DATA data
, void *private_data
)
411 struct pull_newest_from_marshall_buffer_state
*state
=
412 (struct pull_newest_from_marshall_buffer_state
*)private_data
;
414 if (state
->pheader
!= NULL
) {
415 memcpy(state
->pheader
, header
, sizeof(*state
->pheader
));
417 if (state
->pdata
!= NULL
) {
418 state
->pdata
->dsize
= data
.dsize
;
419 state
->pdata
->dptr
= (uint8_t *)talloc_memdup(
420 state
->mem_ctx
, data
.dptr
, data
.dsize
);
424 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer
*buf
,
426 struct ctdb_ltdb_header
*pheader
,
430 struct pull_newest_from_marshall_buffer_state state
;
432 state
.pheader
= pheader
;
433 state
.mem_ctx
= mem_ctx
;
436 if (!parse_newest_in_marshall_buffer(
437 buf
, key
, pull_newest_from_marshall_buffer_parser
,
441 if ((pdata
!= NULL
) && (pdata
->dsize
!= 0) && (pdata
->dptr
== NULL
)) {
448 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
);
449 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
);
451 static struct db_record
*db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx
*ctx
,
455 struct db_record
*result
;
458 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
459 DEBUG(0, ("talloc failed\n"));
463 result
->db
= ctx
->db
;
464 result
->private_data
= ctx
->transaction
;
466 result
->key
.dsize
= key
.dsize
;
467 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
469 if (result
->key
.dptr
== NULL
) {
470 DEBUG(0, ("talloc failed\n"));
475 result
->store
= db_ctdb_store_transaction
;
476 result
->delete_rec
= db_ctdb_delete_transaction
;
478 if (pull_newest_from_marshall_buffer(ctx
->transaction
->m_write
, key
,
479 NULL
, result
, &result
->value
)) {
483 ctdb_data
= tdb_fetch(ctx
->wtdb
->tdb
, key
);
484 if (ctdb_data
.dptr
== NULL
) {
485 /* create the record */
486 result
->value
= tdb_null
;
490 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
491 result
->value
.dptr
= NULL
;
493 if ((result
->value
.dsize
!= 0)
494 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
495 result
, ctdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
496 result
->value
.dsize
))) {
497 DEBUG(0, ("talloc failed\n"));
501 SAFE_FREE(ctdb_data
.dptr
);
506 static int db_ctdb_record_destructor(struct db_record
**recp
)
508 struct db_record
*rec
= talloc_get_type_abort(*recp
, struct db_record
);
509 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
510 rec
->private_data
, struct db_ctdb_transaction_handle
);
511 int ret
= h
->ctx
->db
->transaction_commit(h
->ctx
->db
);
513 DEBUG(0,(__location__
" transaction_commit failed\n"));
519 auto-create a transaction for persistent databases
521 static struct db_record
*db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx
*ctx
,
526 struct db_record
*rec
, **recp
;
528 res
= db_ctdb_transaction_start(ctx
->db
);
533 rec
= db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
535 ctx
->db
->transaction_cancel(ctx
->db
);
539 /* destroy this transaction when we release the lock */
540 recp
= talloc(rec
, struct db_record
*);
542 ctx
->db
->transaction_cancel(ctx
->db
);
547 talloc_set_destructor(recp
, db_ctdb_record_destructor
);
553 stores a record inside a transaction
555 static NTSTATUS
db_ctdb_transaction_store(struct db_ctdb_transaction_handle
*h
,
556 TDB_DATA key
, TDB_DATA data
)
558 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
560 struct ctdb_ltdb_header header
;
564 /* we need the header so we can update the RSN */
566 if (!pull_newest_from_marshall_buffer(h
->m_write
, key
, &header
,
569 rec
= tdb_fetch(h
->ctx
->wtdb
->tdb
, key
);
571 if (rec
.dptr
!= NULL
) {
572 memcpy(&header
, rec
.dptr
,
573 sizeof(struct ctdb_ltdb_header
));
574 rec
.dsize
-= sizeof(struct ctdb_ltdb_header
);
577 * a special case, we are writing the same
578 * data that is there now
580 if (data
.dsize
== rec
.dsize
&&
582 rec
.dptr
+ sizeof(struct ctdb_ltdb_header
),
585 talloc_free(tmp_ctx
);
592 header
.dmaster
= get_my_vnn();
595 h
->m_write
= db_ctdb_marshall_add(h
, h
->m_write
, h
->ctx
->db_id
, 0, key
, &header
, data
);
596 if (h
->m_write
== NULL
) {
597 DEBUG(0,(__location__
" Failed to add to marshalling record\n"));
598 talloc_free(tmp_ctx
);
599 return NT_STATUS_NO_MEMORY
;
602 talloc_free(tmp_ctx
);
608 a record store inside a transaction
610 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
)
612 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
613 rec
->private_data
, struct db_ctdb_transaction_handle
);
616 status
= db_ctdb_transaction_store(h
, rec
->key
, data
);
621 a record delete inside a transaction
623 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
)
625 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
626 rec
->private_data
, struct db_ctdb_transaction_handle
);
629 status
= db_ctdb_transaction_store(h
, rec
->key
, tdb_null
);
633 static void db_ctdb_fetch_db_seqnum_parser(
634 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
635 TDB_DATA data
, void *private_data
)
637 uint64_t *seqnum
= (uint64_t *)private_data
;
639 if (data
.dsize
!= sizeof(uint64_t)) {
643 memcpy(seqnum
, data
.dptr
, sizeof(*seqnum
));
647 * Fetch the db sequence number of a persistent db directly from the db.
649 static NTSTATUS
db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx
*db
,
655 if (seqnum
== NULL
) {
656 return NT_STATUS_INVALID_PARAMETER
;
659 key
= string_term_tdb_data(CTDB_DB_SEQNUM_KEY
);
661 status
= db_ctdb_ltdb_parse(
662 db
, key
, db_ctdb_fetch_db_seqnum_parser
, seqnum
);
664 if (NT_STATUS_IS_OK(status
)) {
667 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
675 * Store the database sequence number inside a transaction.
677 static NTSTATUS
db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle
*h
,
681 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
685 key
= string_term_tdb_data(keyname
);
687 data
.dptr
= (uint8_t *)&seqnum
;
688 data
.dsize
= sizeof(uint64_t);
690 status
= db_ctdb_transaction_store(h
, key
, data
);
698 static int db_ctdb_transaction_commit(struct db_context
*db
)
700 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
704 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
705 uint64_t old_seqnum
, new_seqnum
;
709 DEBUG(0,(__location__
" transaction commit with no open transaction on db 0x%08x\n", ctx
->db_id
));
713 if (h
->nested_cancel
) {
714 db
->transaction_cancel(db
);
715 DEBUG(5,(__location__
" Failed transaction commit after nested cancel\n"));
719 if (h
->nesting
!= 0) {
721 DEBUG(5, (__location__
" transaction commit on db 0x%08x: nesting %d -> %d\n",
722 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
726 if (h
->m_write
== NULL
) {
728 * No changes were made, so don't change the seqnum,
729 * don't push to other node, just exit with success.
735 DEBUG(5,(__location__
" transaction commit on db 0x%08x\n", ctx
->db_id
));
738 * As the last db action before committing, bump the database sequence
739 * number. Note that this undoes all changes to the seqnum records
740 * performed under the transaction. This record is not meant to be
741 * modified by user interaction. It is for internal use only...
743 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &old_seqnum
);
744 if (!NT_STATUS_IS_OK(rets
)) {
745 DEBUG(1, (__location__
" failed to fetch the db sequence number "
746 "in transaction commit on db 0x%08x\n", ctx
->db_id
));
751 new_seqnum
= old_seqnum
+ 1;
753 rets
= db_ctdb_store_db_seqnum(h
, new_seqnum
);
754 if (!NT_STATUS_IS_OK(rets
)) {
755 DEBUG(1, (__location__
"failed to store the db sequence number "
756 " in transaction commit on db 0x%08x\n", ctx
->db_id
));
762 /* tell ctdbd to commit to the other nodes */
763 rets
= ctdbd_control_local(messaging_ctdbd_connection(),
764 CTDB_CONTROL_TRANS3_COMMIT
,
766 db_ctdb_marshall_finish(h
->m_write
),
767 NULL
, NULL
, &status
);
768 if (!NT_STATUS_IS_OK(rets
) || status
!= 0) {
770 * The TRANS3_COMMIT control should only possibly fail when a
771 * recovery has been running concurrently. In any case, the db
772 * will be the same on all nodes, either the new copy or the
773 * old copy. This can be detected by comparing the old and new
774 * local sequence numbers.
776 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &new_seqnum
);
777 if (!NT_STATUS_IS_OK(rets
)) {
778 DEBUG(1, (__location__
" failed to refetch db sequence "
779 "number after failed TRANS3_COMMIT\n"));
784 if (new_seqnum
== old_seqnum
) {
785 /* Recovery prevented all our changes: retry. */
788 if (new_seqnum
!= (old_seqnum
+ 1)) {
789 DEBUG(0, (__location__
" ERROR: new_seqnum[%lu] != "
790 "old_seqnum[%lu] + (0 or 1) after failed "
791 "TRANS3_COMMIT - this should not happen!\n",
792 (unsigned long)new_seqnum
,
793 (unsigned long)old_seqnum
));
798 * Recovery propagated our changes to all nodes, completing
799 * our commit for us - succeed.
806 h
->ctx
->transaction
= NULL
;
815 static int db_ctdb_transaction_cancel(struct db_context
*db
)
817 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
819 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
822 DEBUG(0,(__location__
" transaction cancel with no open transaction on db 0x%08x\n", ctx
->db_id
));
826 if (h
->nesting
!= 0) {
828 h
->nested_cancel
= true;
829 DEBUG(5, (__location__
" transaction cancel on db 0x%08x: nesting %d -> %d\n",
830 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
834 DEBUG(5,(__location__
" Cancel transaction on db 0x%08x\n", ctx
->db_id
));
836 ctx
->transaction
= NULL
;
842 static NTSTATUS
db_ctdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
)
844 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
845 rec
->private_data
, struct db_ctdb_rec
);
847 return db_ctdb_ltdb_store(crec
->ctdb_ctx
, rec
->key
, &(crec
->header
), data
);
852 static NTSTATUS
db_ctdb_send_schedule_for_deletion(struct db_record
*rec
)
855 struct ctdb_control_schedule_for_deletion
*dd
;
858 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
859 rec
->private_data
, struct db_ctdb_rec
);
861 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + rec
->key
.dsize
;
862 indata
.dptr
= talloc_zero_array(crec
, uint8_t, indata
.dsize
);
863 if (indata
.dptr
== NULL
) {
864 DEBUG(0, (__location__
" talloc failed!\n"));
865 return NT_STATUS_NO_MEMORY
;
868 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
869 dd
->db_id
= crec
->ctdb_ctx
->db_id
;
870 dd
->hdr
= crec
->header
;
871 dd
->keylen
= rec
->key
.dsize
;
872 memcpy(dd
->key
, rec
->key
.dptr
, rec
->key
.dsize
);
874 status
= ctdbd_control_local(messaging_ctdbd_connection(),
875 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
876 crec
->ctdb_ctx
->db_id
,
877 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
882 talloc_free(indata
.dptr
);
884 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
885 DEBUG(1, (__location__
" Error sending local control "
886 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
887 nt_errstr(status
), cstatus
));
888 if (NT_STATUS_IS_OK(status
)) {
889 status
= NT_STATUS_UNSUCCESSFUL
;
896 static NTSTATUS
db_ctdb_delete(struct db_record
*rec
)
901 * We have to store the header with empty data. TODO: Fix the
905 status
= db_ctdb_store(rec
, tdb_null
, 0);
906 if (!NT_STATUS_IS_OK(status
)) {
910 status
= db_ctdb_send_schedule_for_deletion(rec
);
914 static int db_ctdb_record_destr(struct db_record
* data
)
916 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
917 data
->private_data
, struct db_ctdb_rec
);
920 struct timeval before
;
923 DEBUG(10, (DEBUGLEVEL
> 10
924 ? "Unlocking db %u key %s\n"
925 : "Unlocking db %u key %.20s\n",
926 (int)crec
->ctdb_ctx
->db_id
,
927 hex_encode_talloc(data
, (unsigned char *)data
->key
.dptr
,
930 before
= timeval_current();
932 ret
= tdb_chainunlock(crec
->ctdb_ctx
->wtdb
->tdb
, data
->key
);
934 timediff
= timeval_elapsed(&before
);
935 timediff
*= 1000; /* get us milliseconds */
937 if (timediff
> crec
->ctdb_ctx
->warn_unlock_msecs
) {
939 key
= hex_encode_talloc(talloc_tos(),
940 (unsigned char *)data
->key
.dptr
,
942 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
943 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
), key
,
949 DEBUG(0, ("tdb_chainunlock failed\n"));
953 threshold
= crec
->ctdb_ctx
->warn_locktime_msecs
;
954 if (threshold
!= 0) {
955 timediff
= timeval_elapsed(&crec
->lock_time
) * 1000;
956 if (timediff
> threshold
) {
959 key
= hex_encode_talloc(data
,
960 (unsigned char *)data
->key
.dptr
,
962 DEBUG(0, ("Held tdb lock on db %s, key %s "
964 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
),
973 * Check whether we have a valid local copy of the given record,
974 * either for reading or for writing.
976 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header
*hdr
,
979 if (hdr
->dmaster
!= get_my_vnn()) {
980 /* If we're not dmaster, it must be r/o copy. */
981 return read_only
&& (hdr
->flags
& CTDB_REC_RO_HAVE_READONLY
);
985 * If we want write access, no one may have r/o copies.
987 return read_only
|| !(hdr
->flags
& CTDB_REC_RO_HAVE_DELEGATIONS
);
990 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data
, bool read_only
)
992 if (ctdb_data
.dptr
== NULL
) {
996 if (ctdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1000 return db_ctdb_can_use_local_hdr(
1001 (struct ctdb_ltdb_header
*)ctdb_data
.dptr
, read_only
);
1004 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
1005 TALLOC_CTX
*mem_ctx
,
1009 struct db_record
*result
;
1010 struct db_ctdb_rec
*crec
;
1013 int migrate_attempts
;
1014 struct timeval migrate_start
;
1015 struct timeval chainlock_start
;
1016 struct timeval ctdb_start_time
;
1017 double chainlock_time
= 0;
1018 double ctdb_time
= 0;
1022 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
1023 DEBUG(0, ("talloc failed\n"));
1027 if (!(crec
= talloc_zero(result
, struct db_ctdb_rec
))) {
1028 DEBUG(0, ("talloc failed\n"));
1029 TALLOC_FREE(result
);
1033 result
->db
= ctx
->db
;
1034 result
->private_data
= (void *)crec
;
1035 crec
->ctdb_ctx
= ctx
;
1037 result
->key
.dsize
= key
.dsize
;
1038 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
1040 if (result
->key
.dptr
== NULL
) {
1041 DEBUG(0, ("talloc failed\n"));
1042 TALLOC_FREE(result
);
1046 migrate_attempts
= 0;
1047 GetTimeOfDay(&migrate_start
);
1050 * Do a blocking lock on the record
1054 if (DEBUGLEVEL
>= 10) {
1055 char *keystr
= hex_encode_talloc(result
, key
.dptr
, key
.dsize
);
1056 DEBUG(10, (DEBUGLEVEL
> 10
1057 ? "Locking db %u key %s\n"
1058 : "Locking db %u key %.20s\n",
1059 (int)crec
->ctdb_ctx
->db_id
, keystr
));
1060 TALLOC_FREE(keystr
);
1063 GetTimeOfDay(&chainlock_start
);
1065 ? tdb_chainlock_nonblock(ctx
->wtdb
->tdb
, key
)
1066 : tdb_chainlock(ctx
->wtdb
->tdb
, key
);
1067 chainlock_time
+= timeval_elapsed(&chainlock_start
);
1070 DEBUG(3, ("tdb_chainlock failed\n"));
1071 TALLOC_FREE(result
);
1075 result
->store
= db_ctdb_store
;
1076 result
->delete_rec
= db_ctdb_delete
;
1077 talloc_set_destructor(result
, db_ctdb_record_destr
);
1079 ctdb_data
= tdb_fetch(ctx
->wtdb
->tdb
, key
);
1082 * See if we have a valid record and we are the dmaster. If so, we can
1083 * take the shortcut and just return it.
1086 if (!db_ctdb_can_use_local_copy(ctdb_data
, false)) {
1087 SAFE_FREE(ctdb_data
.dptr
);
1088 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
1089 talloc_set_destructor(result
, NULL
);
1091 if (tryonly
&& (migrate_attempts
!= 0)) {
1092 DEBUG(5, ("record migrated away again\n"));
1093 TALLOC_FREE(result
);
1097 migrate_attempts
+= 1;
1099 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1100 ctdb_data
.dptr
, ctdb_data
.dptr
?
1101 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
: -1,
1104 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->flags
: 0));
1106 GetTimeOfDay(&ctdb_start_time
);
1107 status
= ctdbd_migrate(messaging_ctdbd_connection(), ctx
->db_id
,
1109 ctdb_time
+= timeval_elapsed(&ctdb_start_time
);
1111 if (!NT_STATUS_IS_OK(status
)) {
1112 DEBUG(5, ("ctdb_migrate failed: %s\n",
1113 nt_errstr(status
)));
1114 TALLOC_FREE(result
);
1117 /* now its migrated, try again */
1123 duration
= timeval_elapsed(&migrate_start
);
1126 * Convert the duration to milliseconds to avoid a
1127 * floating-point division of
1128 * lp_parm_int("migrate_duration") by 1000.
1130 duration_msecs
= duration
* 1000;
1133 if ((migrate_attempts
> ctx
->warn_migrate_attempts
) ||
1134 (duration_msecs
> ctx
->warn_migrate_msecs
)) {
1137 if (tdb_get_flags(ctx
->wtdb
->tdb
) & TDB_INCOMPATIBLE_HASH
) {
1138 chain
= tdb_jenkins_hash(&key
) %
1139 tdb_hash_size(ctx
->wtdb
->tdb
);
1142 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1143 "needed %d attempts, %d milliseconds, "
1144 "chainlock: %f ms, CTDB %f ms\n",
1145 tdb_name(ctx
->wtdb
->tdb
),
1146 hex_encode_talloc(talloc_tos(),
1147 (unsigned char *)key
.dptr
,
1150 migrate_attempts
, duration_msecs
,
1151 chainlock_time
* 1000.0,
1152 ctdb_time
* 1000.0));
1155 GetTimeOfDay(&crec
->lock_time
);
1157 memcpy(&crec
->header
, ctdb_data
.dptr
, sizeof(crec
->header
));
1159 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(crec
->header
);
1160 result
->value
.dptr
= NULL
;
1162 if ((result
->value
.dsize
!= 0)
1163 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
1164 result
, ctdb_data
.dptr
+ sizeof(crec
->header
),
1165 result
->value
.dsize
))) {
1166 DEBUG(0, ("talloc failed\n"));
1167 TALLOC_FREE(result
);
1170 SAFE_FREE(ctdb_data
.dptr
);
1175 static struct db_record
*db_ctdb_fetch_locked(struct db_context
*db
,
1176 TALLOC_CTX
*mem_ctx
,
1179 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1180 struct db_ctdb_ctx
);
1182 if (ctx
->transaction
!= NULL
) {
1183 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1186 if (db
->persistent
) {
1187 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1190 return fetch_locked_internal(ctx
, mem_ctx
, key
, false);
1193 static struct db_record
*db_ctdb_try_fetch_locked(struct db_context
*db
,
1194 TALLOC_CTX
*mem_ctx
,
1197 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1198 struct db_ctdb_ctx
);
1200 if (ctx
->transaction
!= NULL
) {
1201 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1204 if (db
->persistent
) {
1205 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1208 return fetch_locked_internal(ctx
, mem_ctx
, key
, true);
1211 struct db_ctdb_parse_record_state
{
1212 void (*parser
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1214 bool ask_for_readonly_copy
;
1218 static void db_ctdb_parse_record_parser(
1219 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1220 TDB_DATA data
, void *private_data
)
1222 struct db_ctdb_parse_record_state
*state
=
1223 (struct db_ctdb_parse_record_state
*)private_data
;
1224 state
->parser(key
, data
, state
->private_data
);
1227 static void db_ctdb_parse_record_parser_nonpersistent(
1228 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1229 TDB_DATA data
, void *private_data
)
1231 struct db_ctdb_parse_record_state
*state
=
1232 (struct db_ctdb_parse_record_state
*)private_data
;
1234 if (db_ctdb_can_use_local_hdr(header
, true)) {
1235 state
->parser(key
, data
, state
->private_data
);
1239 * We found something in the db, so it seems that this record,
1240 * while not usable locally right now, is popular. Ask for a
1243 state
->ask_for_readonly_copy
= true;
1247 static NTSTATUS
db_ctdb_parse_record(struct db_context
*db
, TDB_DATA key
,
1248 void (*parser
)(TDB_DATA key
,
1250 void *private_data
),
1253 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1254 db
->private_data
, struct db_ctdb_ctx
);
1255 struct db_ctdb_parse_record_state state
;
1258 state
.parser
= parser
;
1259 state
.private_data
= private_data
;
1261 if (ctx
->transaction
!= NULL
) {
1262 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
1266 * Transactions only happen for persistent db's.
1269 found
= parse_newest_in_marshall_buffer(
1270 h
->m_write
, key
, db_ctdb_parse_record_parser
, &state
);
1273 return NT_STATUS_OK
;
1277 if (db
->persistent
) {
1279 * Persistent db, but not found in the transaction buffer
1281 return db_ctdb_ltdb_parse(
1282 ctx
, key
, db_ctdb_parse_record_parser
, &state
);
1286 state
.ask_for_readonly_copy
= false;
1288 status
= db_ctdb_ltdb_parse(
1289 ctx
, key
, db_ctdb_parse_record_parser_nonpersistent
, &state
);
1290 if (NT_STATUS_IS_OK(status
) && state
.done
) {
1291 return NT_STATUS_OK
;
1294 return ctdbd_parse(messaging_ctdbd_connection(), ctx
->db_id
, key
,
1295 state
.ask_for_readonly_copy
, parser
, private_data
);
1298 struct traverse_state
{
1299 struct db_context
*db
;
1300 int (*fn
)(struct db_record
*rec
, void *private_data
);
1305 static void traverse_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1307 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1308 struct db_record
*rec
;
1309 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1310 /* we have to give them a locked record to prevent races */
1311 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, key
);
1312 if (rec
&& rec
->value
.dsize
> 0) {
1313 state
->fn(rec
, state
->private_data
);
1315 talloc_free(tmp_ctx
);
1318 static int traverse_persistent_callback(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1321 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1322 struct db_record
*rec
;
1323 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1327 * Skip the __db_sequence_number__ key:
1328 * This is used for persistent transactions internally.
1330 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1331 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1336 /* we have to give them a locked record to prevent races */
1337 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, kbuf
);
1338 if (rec
&& rec
->value
.dsize
> 0) {
1339 ret
= state
->fn(rec
, state
->private_data
);
1343 talloc_free(tmp_ctx
);
1347 /* wrapper to use traverse_persistent_callback with dbwrap */
1348 static int traverse_persistent_callback_dbwrap(struct db_record
*rec
, void* data
)
1350 return traverse_persistent_callback(NULL
, rec
->key
, rec
->value
, data
);
1354 static int db_ctdb_traverse(struct db_context
*db
,
1355 int (*fn
)(struct db_record
*rec
,
1356 void *private_data
),
1360 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1361 struct db_ctdb_ctx
);
1362 struct traverse_state state
;
1366 state
.private_data
= private_data
;
1369 if (db
->persistent
) {
1370 struct tdb_context
*ltdb
= ctx
->wtdb
->tdb
;
1373 /* for persistent databases we don't need to do a ctdb traverse,
1374 we can do a faster local traverse */
1375 ret
= tdb_traverse(ltdb
, traverse_persistent_callback
, &state
);
1379 if (ctx
->transaction
&& ctx
->transaction
->m_write
) {
1381 * we now have to handle keys not yet
1382 * present at transaction start
1384 struct db_context
*newkeys
= db_open_rbt(talloc_tos());
1385 struct ctdb_marshall_buffer
*mbuf
= ctx
->transaction
->m_write
;
1386 struct ctdb_rec_data
*rec
=NULL
;
1390 if (newkeys
== NULL
) {
1394 for (i
=0; i
<mbuf
->count
; i
++) {
1396 rec
= db_ctdb_marshall_loop_next_key(
1398 SMB_ASSERT(rec
!= NULL
);
1400 if (!tdb_exists(ltdb
, key
)) {
1401 dbwrap_store(newkeys
, key
, tdb_null
, 0);
1404 status
= dbwrap_traverse(newkeys
,
1405 traverse_persistent_callback_dbwrap
,
1408 talloc_free(newkeys
);
1409 if (!NT_STATUS_IS_OK(status
)) {
1417 status
= ctdbd_traverse(ctx
->db_id
, traverse_callback
, &state
);
1418 if (!NT_STATUS_IS_OK(status
)) {
1424 static NTSTATUS
db_ctdb_store_deny(struct db_record
*rec
, TDB_DATA data
, int flag
)
1426 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1429 static NTSTATUS
db_ctdb_delete_deny(struct db_record
*rec
)
1431 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1434 static void traverse_read_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1436 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1437 struct db_record rec
;
1443 rec
.store
= db_ctdb_store_deny
;
1444 rec
.delete_rec
= db_ctdb_delete_deny
;
1445 rec
.private_data
= NULL
;
1446 state
->fn(&rec
, state
->private_data
);
1450 static int traverse_persistent_callback_read(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1453 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1454 struct db_record rec
;
1457 * Skip the __db_sequence_number__ key:
1458 * This is used for persistent transactions internally.
1460 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1461 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1470 rec
.store
= db_ctdb_store_deny
;
1471 rec
.delete_rec
= db_ctdb_delete_deny
;
1472 rec
.private_data
= NULL
;
1474 if (rec
.value
.dsize
<= sizeof(struct ctdb_ltdb_header
)) {
1475 /* a deleted record */
1478 rec
.value
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1479 rec
.value
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1482 return state
->fn(&rec
, state
->private_data
);
1485 static int db_ctdb_traverse_read(struct db_context
*db
,
1486 int (*fn
)(struct db_record
*rec
,
1487 void *private_data
),
1491 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1492 struct db_ctdb_ctx
);
1493 struct traverse_state state
;
1497 state
.private_data
= private_data
;
1500 if (db
->persistent
) {
1501 /* for persistent databases we don't need to do a ctdb traverse,
1502 we can do a faster local traverse */
1503 return tdb_traverse_read(ctx
->wtdb
->tdb
, traverse_persistent_callback_read
, &state
);
1506 status
= ctdbd_traverse(ctx
->db_id
, traverse_read_callback
, &state
);
1507 if (!NT_STATUS_IS_OK(status
)) {
1513 static int db_ctdb_get_seqnum(struct db_context
*db
)
1515 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1516 struct db_ctdb_ctx
);
1517 return tdb_get_seqnum(ctx
->wtdb
->tdb
);
1520 static size_t db_ctdb_id(struct db_context
*db
, uint8_t *id
, size_t idlen
)
1522 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1523 db
->private_data
, struct db_ctdb_ctx
);
1525 if (idlen
>= sizeof(ctx
->db_id
)) {
1526 memcpy(id
, &ctx
->db_id
, sizeof(ctx
->db_id
));
1529 return sizeof(ctx
->db_id
);
1532 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1534 int hash_size
, int tdb_flags
,
1535 int open_flags
, mode_t mode
,
1536 enum dbwrap_lock_order lock_order
,
1537 uint64_t dbwrap_flags
)
1539 struct db_context
*result
;
1540 struct db_ctdb_ctx
*db_ctdb
;
1542 struct ctdbd_connection
*conn
;
1543 struct loadparm_context
*lp_ctx
;
1544 struct ctdb_db_priority prio
;
1548 if (!lp_clustering()) {
1549 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1553 if (!(result
= talloc_zero(mem_ctx
, struct db_context
))) {
1554 DEBUG(0, ("talloc failed\n"));
1555 TALLOC_FREE(result
);
1559 if (!(db_ctdb
= talloc(result
, struct db_ctdb_ctx
))) {
1560 DEBUG(0, ("talloc failed\n"));
1561 TALLOC_FREE(result
);
1565 result
->name
= talloc_strdup(result
, name
);
1566 if (result
->name
== NULL
) {
1567 DEBUG(0, ("talloc failed\n"));
1568 TALLOC_FREE(result
);
1572 db_ctdb
->transaction
= NULL
;
1573 db_ctdb
->db
= result
;
1575 conn
= messaging_ctdbd_connection();
1577 DEBUG(1, ("Could not connect to ctdb\n"));
1578 TALLOC_FREE(result
);
1582 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn
, name
, &db_ctdb
->db_id
, tdb_flags
))) {
1583 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name
));
1584 TALLOC_FREE(result
);
1588 db_path
= ctdbd_dbpath(conn
, db_ctdb
, db_ctdb
->db_id
);
1590 result
->persistent
= ((tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0);
1591 result
->lock_order
= lock_order
;
1593 /* only pass through specific flags */
1594 tdb_flags
&= TDB_SEQNUM
|TDB_VOLATILE
|
1595 TDB_MUTEX_LOCKING
|TDB_CLEAR_IF_FIRST
;
1597 /* honor permissions if user has specified O_CREAT */
1598 if (open_flags
& O_CREAT
) {
1599 chmod(db_path
, mode
);
1602 prio
.db_id
= db_ctdb
->db_id
;
1603 prio
.priority
= lock_order
;
1605 status
= ctdbd_control_local(
1606 conn
, CTDB_CONTROL_SET_DB_PRIORITY
, 0, 0,
1607 make_tdb_data((uint8_t *)&prio
, sizeof(prio
)),
1608 NULL
, NULL
, &cstatus
);
1610 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1611 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1612 nt_errstr(status
), cstatus
));
1613 TALLOC_FREE(result
);
1617 if (!result
->persistent
&&
1618 (dbwrap_flags
& DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS
))
1622 indata
= make_tdb_data((uint8_t *)&db_ctdb
->db_id
,
1623 sizeof(db_ctdb
->db_id
));
1625 status
= ctdbd_control_local(
1626 conn
, CTDB_CONTROL_SET_DB_READONLY
, 0, 0, indata
,
1627 NULL
, NULL
, &cstatus
);
1628 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1629 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1630 "%s, %d\n", nt_errstr(status
), cstatus
));
1631 TALLOC_FREE(result
);
1636 lp_ctx
= loadparm_init_s3(db_path
, loadparm_s3_helpers());
1638 if (hash_size
== 0) {
1639 hash_size
= lpcfg_tdb_hash_size(lp_ctx
, db_path
);
1642 db_ctdb
->wtdb
= tdb_wrap_open(db_ctdb
, db_path
, hash_size
,
1643 lpcfg_tdb_flags(lp_ctx
, tdb_flags
),
1645 talloc_unlink(db_path
, lp_ctx
);
1646 if (db_ctdb
->wtdb
== NULL
) {
1647 DEBUG(0, ("Could not open tdb %s: %s\n", db_path
, strerror(errno
)));
1648 TALLOC_FREE(result
);
1651 talloc_free(db_path
);
1653 if (result
->persistent
) {
1654 db_ctdb
->lock_ctx
= g_lock_ctx_init(db_ctdb
,
1655 ctdb_conn_msg_ctx(conn
));
1656 if (db_ctdb
->lock_ctx
== NULL
) {
1657 DEBUG(0, ("g_lock_ctx_init failed\n"));
1658 TALLOC_FREE(result
);
1663 db_ctdb
->warn_unlock_msecs
= lp_parm_int(-1, "ctdb",
1664 "unlock_warn_threshold", 5);
1665 db_ctdb
->warn_migrate_attempts
= lp_parm_int(-1, "ctdb",
1666 "migrate_attempts", 10);
1667 db_ctdb
->warn_migrate_msecs
= lp_parm_int(-1, "ctdb",
1668 "migrate_duration", 5000);
1669 db_ctdb
->warn_locktime_msecs
= lp_ctdb_locktime_warn_threshold();
1671 result
->private_data
= (void *)db_ctdb
;
1672 result
->fetch_locked
= db_ctdb_fetch_locked
;
1673 result
->try_fetch_locked
= db_ctdb_try_fetch_locked
;
1674 result
->parse_record
= db_ctdb_parse_record
;
1675 result
->traverse
= db_ctdb_traverse
;
1676 result
->traverse_read
= db_ctdb_traverse_read
;
1677 result
->get_seqnum
= db_ctdb_get_seqnum
;
1678 result
->transaction_start
= db_ctdb_transaction_start
;
1679 result
->transaction_commit
= db_ctdb_transaction_commit
;
1680 result
->transaction_cancel
= db_ctdb_transaction_cancel
;
1681 result
->id
= db_ctdb_id
;
1682 result
->stored_callback
= NULL
;
1684 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1685 name
, db_ctdb
->db_id
));