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 #ifdef CLUSTER_SUPPORT
33 * It is not possible to include ctdb.h and tdb_compat.h (included via
34 * some other include above) without warnings. This fixes those
42 #ifdef typesafe_cb_preargs
43 #undef typesafe_cb_preargs
46 #ifdef typesafe_cb_postargs
47 #undef typesafe_cb_postargs
51 #include "ctdb_private.h"
52 #include "ctdbd_conn.h"
53 #include "dbwrap/dbwrap.h"
54 #include "dbwrap/dbwrap_private.h"
55 #include "dbwrap/dbwrap_ctdb.h"
59 struct db_ctdb_transaction_handle
{
60 struct db_ctdb_ctx
*ctx
;
62 * we store the writes done under a transaction:
64 struct ctdb_marshall_buffer
*m_write
;
71 struct db_context
*db
;
72 struct tdb_wrap
*wtdb
;
74 struct db_ctdb_transaction_handle
*transaction
;
75 struct g_lock_ctx
*lock_ctx
;
77 /* thresholds for warning messages */
78 int warn_unlock_msecs
;
79 int warn_migrate_msecs
;
80 int warn_migrate_attempts
;
81 int warn_locktime_msecs
;
85 struct db_ctdb_ctx
*ctdb_ctx
;
86 struct ctdb_ltdb_header header
;
87 struct timeval lock_time
;
90 static NTSTATUS
tdb_error_to_ntstatus(struct tdb_context
*tdb
)
92 enum TDB_ERROR tret
= tdb_error(tdb
);
94 return map_nt_error_from_tdb(tret
);
97 struct db_ctdb_ltdb_parse_state
{
98 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
99 TDB_DATA data
, void *private_data
);
103 static int db_ctdb_ltdb_parser(TDB_DATA key
, TDB_DATA data
,
106 struct db_ctdb_ltdb_parse_state
*state
=
107 (struct db_ctdb_ltdb_parse_state
*)private_data
;
109 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
112 if (data
.dsize
== sizeof(struct ctdb_ltdb_header
)) {
114 * Making this a separate case that needs fixing
115 * separately. This is an empty record. ctdbd does not
116 * distinguish between empty and deleted records. Samba right
117 * now can live without empty records, so lets treat zero-size
118 * (i.e. deleted) records as non-existing.
123 key
, (struct ctdb_ltdb_header
*)data
.dptr
,
124 make_tdb_data(data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
125 data
.dsize
- sizeof(struct ctdb_ltdb_header
)),
126 state
->private_data
);
130 static NTSTATUS
db_ctdb_ltdb_parse(
131 struct db_ctdb_ctx
*db
, TDB_DATA key
,
132 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
133 TDB_DATA data
, void *private_data
),
136 struct db_ctdb_ltdb_parse_state state
;
139 state
.parser
= parser
;
140 state
.private_data
= private_data
;
142 ret
= tdb_parse_record(db
->wtdb
->tdb
, key
, db_ctdb_ltdb_parser
,
145 return NT_STATUS_NOT_FOUND
;
151 * Store a record together with the ctdb record header
152 * in the local copy of the database.
154 static NTSTATUS
db_ctdb_ltdb_store(struct db_ctdb_ctx
*db
,
156 struct ctdb_ltdb_header
*header
,
162 rec
.dsize
= data
.dsize
+ sizeof(struct ctdb_ltdb_header
);
163 rec
.dptr
= (uint8_t *)talloc_size(talloc_tos(), rec
.dsize
);
165 if (rec
.dptr
== NULL
) {
166 return NT_STATUS_NO_MEMORY
;
169 memcpy(rec
.dptr
, header
, sizeof(struct ctdb_ltdb_header
));
170 memcpy(sizeof(struct ctdb_ltdb_header
) + (uint8_t *)rec
.dptr
, data
.dptr
, data
.dsize
);
172 ret
= tdb_store(db
->wtdb
->tdb
, key
, rec
, TDB_REPLACE
);
174 talloc_free(rec
.dptr
);
176 return (ret
== 0) ? NT_STATUS_OK
177 : tdb_error_to_ntstatus(db
->wtdb
->tdb
);
182 form a ctdb_rec_data record from a key/data pair
184 static struct ctdb_rec_data
*db_ctdb_marshall_record(TALLOC_CTX
*mem_ctx
, uint32_t reqid
,
186 struct ctdb_ltdb_header
*header
,
190 struct ctdb_rec_data
*d
;
192 length
= offsetof(struct ctdb_rec_data
, data
) + key
.dsize
+
193 data
.dsize
+ sizeof(*header
);
194 d
= (struct ctdb_rec_data
*)talloc_size(mem_ctx
, length
);
200 d
->keylen
= key
.dsize
;
201 memcpy(&d
->data
[0], key
.dptr
, key
.dsize
);
203 d
->datalen
= data
.dsize
+ sizeof(*header
);
204 memcpy(&d
->data
[key
.dsize
], header
, sizeof(*header
));
205 memcpy(&d
->data
[key
.dsize
+sizeof(*header
)], data
.dptr
, data
.dsize
);
210 /* helper function for marshalling multiple records */
211 static struct ctdb_marshall_buffer
*db_ctdb_marshall_add(TALLOC_CTX
*mem_ctx
,
212 struct ctdb_marshall_buffer
*m
,
216 struct ctdb_ltdb_header
*header
,
219 struct ctdb_rec_data
*r
;
220 size_t m_size
, r_size
;
221 struct ctdb_marshall_buffer
*m2
= NULL
;
223 r
= db_ctdb_marshall_record(talloc_tos(), reqid
, key
, header
, data
);
230 m
= (struct ctdb_marshall_buffer
*)talloc_zero_size(
231 mem_ctx
, offsetof(struct ctdb_marshall_buffer
, data
));
238 m_size
= talloc_get_size(m
);
239 r_size
= talloc_get_size(r
);
241 m2
= (struct ctdb_marshall_buffer
*)talloc_realloc_size(
242 mem_ctx
, m
, m_size
+ r_size
);
248 memcpy(m_size
+ (uint8_t *)m2
, r
, r_size
);
257 /* we've finished marshalling, return a data blob with the marshalled records */
258 static TDB_DATA
db_ctdb_marshall_finish(struct ctdb_marshall_buffer
*m
)
261 data
.dptr
= (uint8_t *)m
;
262 data
.dsize
= talloc_get_size(m
);
267 loop over a marshalling buffer
269 - pass r==NULL to start
270 - loop the number of times indicated by m->count
272 static struct ctdb_rec_data
*db_ctdb_marshall_loop_next_key(
273 struct ctdb_marshall_buffer
*m
, struct ctdb_rec_data
*r
, TDB_DATA
*key
)
276 r
= (struct ctdb_rec_data
*)&m
->data
[0];
278 r
= (struct ctdb_rec_data
*)(r
->length
+ (uint8_t *)r
);
281 key
->dptr
= &r
->data
[0];
282 key
->dsize
= r
->keylen
;
286 static bool db_ctdb_marshall_buf_parse(
287 struct ctdb_rec_data
*r
, uint32_t *reqid
,
288 struct ctdb_ltdb_header
**header
, TDB_DATA
*data
)
290 if (r
->datalen
< sizeof(struct ctdb_ltdb_header
)) {
296 data
->dptr
= &r
->data
[r
->keylen
] + sizeof(struct ctdb_ltdb_header
);
297 data
->dsize
= r
->datalen
- sizeof(struct ctdb_ltdb_header
);
299 *header
= (struct ctdb_ltdb_header
*)&r
->data
[r
->keylen
];
305 * CTDB transaction destructor
307 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle
*h
)
311 status
= g_lock_unlock(h
->ctx
->lock_ctx
, h
->lock_name
);
312 if (!NT_STATUS_IS_OK(status
)) {
313 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h
->lock_name
,
321 * CTDB dbwrap API: transaction_start function
322 * starts a transaction on a persistent database
324 static int db_ctdb_transaction_start(struct db_context
*db
)
326 struct db_ctdb_transaction_handle
*h
;
328 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
331 if (!db
->persistent
) {
332 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
337 if (ctx
->transaction
) {
338 ctx
->transaction
->nesting
++;
339 DEBUG(5, (__location__
" transaction start on db 0x%08x: nesting %d -> %d\n",
340 ctx
->db_id
, ctx
->transaction
->nesting
- 1, ctx
->transaction
->nesting
));
344 h
= talloc_zero(db
, struct db_ctdb_transaction_handle
);
346 DEBUG(0,(__location__
" oom for transaction handle\n"));
352 h
->lock_name
= talloc_asprintf(h
, "transaction_db_0x%08x",
353 (unsigned int)ctx
->db_id
);
354 if (h
->lock_name
== NULL
) {
355 DEBUG(0, ("talloc_asprintf failed\n"));
361 * Wait a day, i.e. forever...
363 status
= g_lock_lock(ctx
->lock_ctx
, h
->lock_name
, G_LOCK_WRITE
,
364 timeval_set(86400, 0));
365 if (!NT_STATUS_IS_OK(status
)) {
366 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status
)));
371 talloc_set_destructor(h
, db_ctdb_transaction_destructor
);
373 ctx
->transaction
= h
;
375 DEBUG(5,(__location__
" transaction started on db 0x%08x\n", ctx
->db_id
));
380 static bool parse_newest_in_marshall_buffer(
381 struct ctdb_marshall_buffer
*buf
, TDB_DATA key
,
382 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
383 TDB_DATA data
, void *private_data
),
386 struct ctdb_rec_data
*rec
= NULL
;
387 struct ctdb_ltdb_header
*h
= NULL
;
396 * Walk the list of records written during this
397 * transaction. If we want to read one we have already
398 * written, return the last written sample. Thus we do not do
399 * a "break;" for the first hit, this record might have been
403 for (i
=0; i
<buf
->count
; i
++) {
407 rec
= db_ctdb_marshall_loop_next_key(buf
, rec
, &tkey
);
412 if (!tdb_data_equal(key
, tkey
)) {
416 if (!db_ctdb_marshall_buf_parse(rec
, &reqid
, &h
, &data
)) {
425 parser(key
, h
, data
, private_data
);
430 struct pull_newest_from_marshall_buffer_state
{
431 struct ctdb_ltdb_header
*pheader
;
436 static void pull_newest_from_marshall_buffer_parser(
437 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
438 TDB_DATA data
, void *private_data
)
440 struct pull_newest_from_marshall_buffer_state
*state
=
441 (struct pull_newest_from_marshall_buffer_state
*)private_data
;
443 if (state
->pheader
!= NULL
) {
444 memcpy(state
->pheader
, header
, sizeof(*state
->pheader
));
446 if (state
->pdata
!= NULL
) {
447 state
->pdata
->dsize
= data
.dsize
;
448 state
->pdata
->dptr
= (uint8_t *)talloc_memdup(
449 state
->mem_ctx
, data
.dptr
, data
.dsize
);
453 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer
*buf
,
455 struct ctdb_ltdb_header
*pheader
,
459 struct pull_newest_from_marshall_buffer_state state
;
461 state
.pheader
= pheader
;
462 state
.mem_ctx
= mem_ctx
;
465 if (!parse_newest_in_marshall_buffer(
466 buf
, key
, pull_newest_from_marshall_buffer_parser
,
470 if ((pdata
!= NULL
) && (pdata
->dsize
!= 0) && (pdata
->dptr
== NULL
)) {
477 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
);
478 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
);
480 static struct db_record
*db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx
*ctx
,
484 struct db_record
*result
;
487 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
488 DEBUG(0, ("talloc failed\n"));
492 result
->db
= ctx
->db
;
493 result
->private_data
= ctx
->transaction
;
495 result
->key
.dsize
= key
.dsize
;
496 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
498 if (result
->key
.dptr
== NULL
) {
499 DEBUG(0, ("talloc failed\n"));
504 result
->store
= db_ctdb_store_transaction
;
505 result
->delete_rec
= db_ctdb_delete_transaction
;
507 if (pull_newest_from_marshall_buffer(ctx
->transaction
->m_write
, key
,
508 NULL
, result
, &result
->value
)) {
512 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
513 if (ctdb_data
.dptr
== NULL
) {
514 /* create the record */
515 result
->value
= tdb_null
;
519 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
520 result
->value
.dptr
= NULL
;
522 if ((result
->value
.dsize
!= 0)
523 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
524 result
, ctdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
525 result
->value
.dsize
))) {
526 DEBUG(0, ("talloc failed\n"));
530 SAFE_FREE(ctdb_data
.dptr
);
535 static int db_ctdb_record_destructor(struct db_record
**recp
)
537 struct db_record
*rec
= talloc_get_type_abort(*recp
, struct db_record
);
538 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
539 rec
->private_data
, struct db_ctdb_transaction_handle
);
540 int ret
= h
->ctx
->db
->transaction_commit(h
->ctx
->db
);
542 DEBUG(0,(__location__
" transaction_commit failed\n"));
548 auto-create a transaction for persistent databases
550 static struct db_record
*db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx
*ctx
,
555 struct db_record
*rec
, **recp
;
557 res
= db_ctdb_transaction_start(ctx
->db
);
562 rec
= db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
564 ctx
->db
->transaction_cancel(ctx
->db
);
568 /* destroy this transaction when we release the lock */
569 recp
= talloc(rec
, struct db_record
*);
571 ctx
->db
->transaction_cancel(ctx
->db
);
576 talloc_set_destructor(recp
, db_ctdb_record_destructor
);
582 stores a record inside a transaction
584 static NTSTATUS
db_ctdb_transaction_store(struct db_ctdb_transaction_handle
*h
,
585 TDB_DATA key
, TDB_DATA data
)
587 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
589 struct ctdb_ltdb_header header
;
593 /* we need the header so we can update the RSN */
595 if (!pull_newest_from_marshall_buffer(h
->m_write
, key
, &header
,
598 rec
= tdb_fetch_compat(h
->ctx
->wtdb
->tdb
, key
);
600 if (rec
.dptr
!= NULL
) {
601 memcpy(&header
, rec
.dptr
,
602 sizeof(struct ctdb_ltdb_header
));
603 rec
.dsize
-= sizeof(struct ctdb_ltdb_header
);
606 * a special case, we are writing the same
607 * data that is there now
609 if (data
.dsize
== rec
.dsize
&&
611 rec
.dptr
+ sizeof(struct ctdb_ltdb_header
),
614 talloc_free(tmp_ctx
);
621 header
.dmaster
= get_my_vnn();
624 h
->m_write
= db_ctdb_marshall_add(h
, h
->m_write
, h
->ctx
->db_id
, 0, key
, &header
, data
);
625 if (h
->m_write
== NULL
) {
626 DEBUG(0,(__location__
" Failed to add to marshalling record\n"));
627 talloc_free(tmp_ctx
);
628 return NT_STATUS_NO_MEMORY
;
631 talloc_free(tmp_ctx
);
637 a record store inside a transaction
639 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
)
641 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
642 rec
->private_data
, struct db_ctdb_transaction_handle
);
645 status
= db_ctdb_transaction_store(h
, rec
->key
, data
);
650 a record delete inside a transaction
652 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
)
654 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
655 rec
->private_data
, struct db_ctdb_transaction_handle
);
658 status
= db_ctdb_transaction_store(h
, rec
->key
, tdb_null
);
662 static void db_ctdb_fetch_db_seqnum_parser(
663 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
664 TDB_DATA data
, void *private_data
)
666 uint64_t *seqnum
= (uint64_t *)private_data
;
668 if (data
.dsize
!= sizeof(uint64_t)) {
672 memcpy(seqnum
, data
.dptr
, sizeof(*seqnum
));
676 * Fetch the db sequence number of a persistent db directly from the db.
678 static NTSTATUS
db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx
*db
,
684 if (seqnum
== NULL
) {
685 return NT_STATUS_INVALID_PARAMETER
;
688 key
= string_term_tdb_data(CTDB_DB_SEQNUM_KEY
);
690 status
= db_ctdb_ltdb_parse(
691 db
, key
, db_ctdb_fetch_db_seqnum_parser
, seqnum
);
693 if (NT_STATUS_IS_OK(status
)) {
696 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
704 * Store the database sequence number inside a transaction.
706 static NTSTATUS
db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle
*h
,
710 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
714 key
= string_term_tdb_data(keyname
);
716 data
.dptr
= (uint8_t *)&seqnum
;
717 data
.dsize
= sizeof(uint64_t);
719 status
= db_ctdb_transaction_store(h
, key
, data
);
727 static int db_ctdb_transaction_commit(struct db_context
*db
)
729 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
733 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
734 uint64_t old_seqnum
, new_seqnum
;
738 DEBUG(0,(__location__
" transaction commit with no open transaction on db 0x%08x\n", ctx
->db_id
));
742 if (h
->nested_cancel
) {
743 db
->transaction_cancel(db
);
744 DEBUG(5,(__location__
" Failed transaction commit after nested cancel\n"));
748 if (h
->nesting
!= 0) {
750 DEBUG(5, (__location__
" transaction commit on db 0x%08x: nesting %d -> %d\n",
751 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
755 if (h
->m_write
== NULL
) {
757 * No changes were made, so don't change the seqnum,
758 * don't push to other node, just exit with success.
764 DEBUG(5,(__location__
" transaction commit on db 0x%08x\n", ctx
->db_id
));
767 * As the last db action before committing, bump the database sequence
768 * number. Note that this undoes all changes to the seqnum records
769 * performed under the transaction. This record is not meant to be
770 * modified by user interaction. It is for internal use only...
772 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &old_seqnum
);
773 if (!NT_STATUS_IS_OK(rets
)) {
774 DEBUG(1, (__location__
" failed to fetch the db sequence number "
775 "in transaction commit on db 0x%08x\n", ctx
->db_id
));
780 new_seqnum
= old_seqnum
+ 1;
782 rets
= db_ctdb_store_db_seqnum(h
, new_seqnum
);
783 if (!NT_STATUS_IS_OK(rets
)) {
784 DEBUG(1, (__location__
"failed to store the db sequence number "
785 " in transaction commit on db 0x%08x\n", ctx
->db_id
));
791 /* tell ctdbd to commit to the other nodes */
792 rets
= ctdbd_control_local(messaging_ctdbd_connection(),
793 CTDB_CONTROL_TRANS3_COMMIT
,
795 db_ctdb_marshall_finish(h
->m_write
),
796 NULL
, NULL
, &status
);
797 if (!NT_STATUS_IS_OK(rets
) || status
!= 0) {
799 * The TRANS3_COMMIT control should only possibly fail when a
800 * recovery has been running concurrently. In any case, the db
801 * will be the same on all nodes, either the new copy or the
802 * old copy. This can be detected by comparing the old and new
803 * local sequence numbers.
805 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &new_seqnum
);
806 if (!NT_STATUS_IS_OK(rets
)) {
807 DEBUG(1, (__location__
" failed to refetch db sequence "
808 "number after failed TRANS3_COMMIT\n"));
813 if (new_seqnum
== old_seqnum
) {
814 /* Recovery prevented all our changes: retry. */
817 if (new_seqnum
!= (old_seqnum
+ 1)) {
818 DEBUG(0, (__location__
" ERROR: new_seqnum[%lu] != "
819 "old_seqnum[%lu] + (0 or 1) after failed "
820 "TRANS3_COMMIT - this should not happen!\n",
821 (unsigned long)new_seqnum
,
822 (unsigned long)old_seqnum
));
827 * Recovery propagated our changes to all nodes, completing
828 * our commit for us - succeed.
835 h
->ctx
->transaction
= NULL
;
844 static int db_ctdb_transaction_cancel(struct db_context
*db
)
846 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
848 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
851 DEBUG(0,(__location__
" transaction cancel with no open transaction on db 0x%08x\n", ctx
->db_id
));
855 if (h
->nesting
!= 0) {
857 h
->nested_cancel
= true;
858 DEBUG(5, (__location__
" transaction cancel on db 0x%08x: nesting %d -> %d\n",
859 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
863 DEBUG(5,(__location__
" Cancel transaction on db 0x%08x\n", ctx
->db_id
));
865 ctx
->transaction
= NULL
;
871 static NTSTATUS
db_ctdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
)
873 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
874 rec
->private_data
, struct db_ctdb_rec
);
876 return db_ctdb_ltdb_store(crec
->ctdb_ctx
, rec
->key
, &(crec
->header
), data
);
881 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
882 static NTSTATUS
db_ctdb_send_schedule_for_deletion(struct db_record
*rec
)
885 struct ctdb_control_schedule_for_deletion
*dd
;
888 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
889 rec
->private_data
, struct db_ctdb_rec
);
891 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + rec
->key
.dsize
;
892 indata
.dptr
= talloc_zero_array(crec
, uint8_t, indata
.dsize
);
893 if (indata
.dptr
== NULL
) {
894 DEBUG(0, (__location__
" talloc failed!\n"));
895 return NT_STATUS_NO_MEMORY
;
898 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
899 dd
->db_id
= crec
->ctdb_ctx
->db_id
;
900 dd
->hdr
= crec
->header
;
901 dd
->keylen
= rec
->key
.dsize
;
902 memcpy(dd
->key
, rec
->key
.dptr
, rec
->key
.dsize
);
904 status
= ctdbd_control_local(messaging_ctdbd_connection(),
905 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
906 crec
->ctdb_ctx
->db_id
,
907 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
912 talloc_free(indata
.dptr
);
914 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
915 DEBUG(1, (__location__
" Error sending local control "
916 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
917 nt_errstr(status
), cstatus
));
918 if (NT_STATUS_IS_OK(status
)) {
919 status
= NT_STATUS_UNSUCCESSFUL
;
927 static NTSTATUS
db_ctdb_delete(struct db_record
*rec
)
932 * We have to store the header with empty data. TODO: Fix the
936 status
= db_ctdb_store(rec
, tdb_null
, 0);
937 if (!NT_STATUS_IS_OK(status
)) {
941 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
942 status
= db_ctdb_send_schedule_for_deletion(rec
);
948 static int db_ctdb_record_destr(struct db_record
* data
)
950 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
951 data
->private_data
, struct db_ctdb_rec
);
954 struct timeval before
;
957 DEBUG(10, (DEBUGLEVEL
> 10
958 ? "Unlocking db %u key %s\n"
959 : "Unlocking db %u key %.20s\n",
960 (int)crec
->ctdb_ctx
->db_id
,
961 hex_encode_talloc(data
, (unsigned char *)data
->key
.dptr
,
964 before
= timeval_current();
966 ret
= tdb_chainunlock(crec
->ctdb_ctx
->wtdb
->tdb
, data
->key
);
968 timediff
= timeval_elapsed(&before
);
969 timediff
*= 1000; /* get us milliseconds */
971 if (timediff
> crec
->ctdb_ctx
->warn_unlock_msecs
) {
973 key
= hex_encode_talloc(talloc_tos(),
974 (unsigned char *)data
->key
.dptr
,
976 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
977 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
), key
,
983 DEBUG(0, ("tdb_chainunlock failed\n"));
987 threshold
= crec
->ctdb_ctx
->warn_locktime_msecs
;
988 if (threshold
!= 0) {
989 timediff
= timeval_elapsed(&crec
->lock_time
) * 1000;
990 if (timediff
> threshold
) {
993 key
= hex_encode_talloc(data
,
994 (unsigned char *)data
->key
.dptr
,
996 DEBUG(0, ("Held tdb lock on db %s, key %s "
998 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
),
1007 * Check whether we have a valid local copy of the given record,
1008 * either for reading or for writing.
1010 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header
*hdr
,
1013 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1014 if (hdr
->dmaster
!= get_my_vnn()) {
1015 /* If we're not dmaster, it must be r/o copy. */
1016 return read_only
&& (hdr
->flags
& CTDB_REC_RO_HAVE_READONLY
);
1020 * If we want write access, no one may have r/o copies.
1022 return read_only
|| !(hdr
->flags
& CTDB_REC_RO_HAVE_DELEGATIONS
);
1024 return (hdr
->dmaster
== get_my_vnn());
1028 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data
, bool read_only
)
1030 if (ctdb_data
.dptr
== NULL
) {
1034 if (ctdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1038 return db_ctdb_can_use_local_hdr(
1039 (struct ctdb_ltdb_header
*)ctdb_data
.dptr
, read_only
);
1042 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
1043 TALLOC_CTX
*mem_ctx
,
1047 struct db_record
*result
;
1048 struct db_ctdb_rec
*crec
;
1051 int migrate_attempts
;
1052 struct timeval migrate_start
;
1053 struct timeval chainlock_start
;
1054 struct timeval ctdb_start_time
;
1055 double chainlock_time
= 0;
1056 double ctdb_time
= 0;
1060 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
1061 DEBUG(0, ("talloc failed\n"));
1065 if (!(crec
= talloc_zero(result
, struct db_ctdb_rec
))) {
1066 DEBUG(0, ("talloc failed\n"));
1067 TALLOC_FREE(result
);
1071 result
->db
= ctx
->db
;
1072 result
->private_data
= (void *)crec
;
1073 crec
->ctdb_ctx
= ctx
;
1075 result
->key
.dsize
= key
.dsize
;
1076 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
1078 if (result
->key
.dptr
== NULL
) {
1079 DEBUG(0, ("talloc failed\n"));
1080 TALLOC_FREE(result
);
1084 migrate_attempts
= 0;
1085 GetTimeOfDay(&migrate_start
);
1088 * Do a blocking lock on the record
1092 if (DEBUGLEVEL
>= 10) {
1093 char *keystr
= hex_encode_talloc(result
, key
.dptr
, key
.dsize
);
1094 DEBUG(10, (DEBUGLEVEL
> 10
1095 ? "Locking db %u key %s\n"
1096 : "Locking db %u key %.20s\n",
1097 (int)crec
->ctdb_ctx
->db_id
, keystr
));
1098 TALLOC_FREE(keystr
);
1101 GetTimeOfDay(&chainlock_start
);
1103 ? tdb_chainlock_nonblock(ctx
->wtdb
->tdb
, key
)
1104 : tdb_chainlock(ctx
->wtdb
->tdb
, key
);
1105 chainlock_time
+= timeval_elapsed(&chainlock_start
);
1108 DEBUG(3, ("tdb_chainlock failed\n"));
1109 TALLOC_FREE(result
);
1113 result
->store
= db_ctdb_store
;
1114 result
->delete_rec
= db_ctdb_delete
;
1115 talloc_set_destructor(result
, db_ctdb_record_destr
);
1117 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
1120 * See if we have a valid record and we are the dmaster. If so, we can
1121 * take the shortcut and just return it.
1124 if (!db_ctdb_can_use_local_copy(ctdb_data
, false)) {
1125 SAFE_FREE(ctdb_data
.dptr
);
1126 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
1127 talloc_set_destructor(result
, NULL
);
1129 if (tryonly
&& (migrate_attempts
!= 0)) {
1130 DEBUG(5, ("record migrated away again\n"));
1131 TALLOC_FREE(result
);
1135 migrate_attempts
+= 1;
1137 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1138 ctdb_data
.dptr
, ctdb_data
.dptr
?
1139 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
: -1,
1142 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->flags
: 0));
1144 GetTimeOfDay(&ctdb_start_time
);
1145 status
= ctdbd_migrate(messaging_ctdbd_connection(), ctx
->db_id
,
1147 ctdb_time
+= timeval_elapsed(&ctdb_start_time
);
1149 if (!NT_STATUS_IS_OK(status
)) {
1150 DEBUG(5, ("ctdb_migrate failed: %s\n",
1151 nt_errstr(status
)));
1152 TALLOC_FREE(result
);
1155 /* now its migrated, try again */
1161 duration
= timeval_elapsed(&migrate_start
);
1164 * Convert the duration to milliseconds to avoid a
1165 * floating-point division of
1166 * lp_parm_int("migrate_duration") by 1000.
1168 duration_msecs
= duration
* 1000;
1171 if ((migrate_attempts
> ctx
->warn_migrate_attempts
) ||
1172 (duration_msecs
> ctx
->warn_migrate_msecs
)) {
1175 if (tdb_get_flags(ctx
->wtdb
->tdb
) & TDB_INCOMPATIBLE_HASH
) {
1176 chain
= tdb_jenkins_hash(&key
) %
1177 tdb_hash_size(ctx
->wtdb
->tdb
);
1180 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1181 "needed %d attempts, %d milliseconds, "
1182 "chainlock: %d ms, CTDB %d ms\n",
1183 tdb_name(ctx
->wtdb
->tdb
),
1184 hex_encode_talloc(talloc_tos(),
1185 (unsigned char *)key
.dptr
,
1188 migrate_attempts
, duration_msecs
,
1189 (int) chainlock_time
* 1000,
1190 (int) ctdb_time
* 1000));
1193 GetTimeOfDay(&crec
->lock_time
);
1195 memcpy(&crec
->header
, ctdb_data
.dptr
, sizeof(crec
->header
));
1197 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(crec
->header
);
1198 result
->value
.dptr
= NULL
;
1200 if ((result
->value
.dsize
!= 0)
1201 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
1202 result
, ctdb_data
.dptr
+ sizeof(crec
->header
),
1203 result
->value
.dsize
))) {
1204 DEBUG(0, ("talloc failed\n"));
1205 TALLOC_FREE(result
);
1208 SAFE_FREE(ctdb_data
.dptr
);
1213 static struct db_record
*db_ctdb_fetch_locked(struct db_context
*db
,
1214 TALLOC_CTX
*mem_ctx
,
1217 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1218 struct db_ctdb_ctx
);
1220 if (ctx
->transaction
!= NULL
) {
1221 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1224 if (db
->persistent
) {
1225 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1228 return fetch_locked_internal(ctx
, mem_ctx
, key
, false);
1231 static struct db_record
*db_ctdb_try_fetch_locked(struct db_context
*db
,
1232 TALLOC_CTX
*mem_ctx
,
1235 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1236 struct db_ctdb_ctx
);
1238 if (ctx
->transaction
!= NULL
) {
1239 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1242 if (db
->persistent
) {
1243 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1246 return fetch_locked_internal(ctx
, mem_ctx
, key
, true);
1249 struct db_ctdb_parse_record_state
{
1250 void (*parser
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1252 bool ask_for_readonly_copy
;
1256 static void db_ctdb_parse_record_parser(
1257 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1258 TDB_DATA data
, void *private_data
)
1260 struct db_ctdb_parse_record_state
*state
=
1261 (struct db_ctdb_parse_record_state
*)private_data
;
1262 state
->parser(key
, data
, state
->private_data
);
1265 static void db_ctdb_parse_record_parser_nonpersistent(
1266 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1267 TDB_DATA data
, void *private_data
)
1269 struct db_ctdb_parse_record_state
*state
=
1270 (struct db_ctdb_parse_record_state
*)private_data
;
1272 if (db_ctdb_can_use_local_hdr(header
, true)) {
1273 state
->parser(key
, data
, state
->private_data
);
1277 * We found something in the db, so it seems that this record,
1278 * while not usable locally right now, is popular. Ask for a
1281 state
->ask_for_readonly_copy
= true;
1285 static NTSTATUS
db_ctdb_parse_record(struct db_context
*db
, TDB_DATA key
,
1286 void (*parser
)(TDB_DATA key
,
1288 void *private_data
),
1291 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1292 db
->private_data
, struct db_ctdb_ctx
);
1293 struct db_ctdb_parse_record_state state
;
1296 state
.parser
= parser
;
1297 state
.private_data
= private_data
;
1299 if (ctx
->transaction
!= NULL
) {
1300 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
1304 * Transactions only happen for persistent db's.
1307 found
= parse_newest_in_marshall_buffer(
1308 h
->m_write
, key
, db_ctdb_parse_record_parser
, &state
);
1311 return NT_STATUS_OK
;
1315 if (db
->persistent
) {
1317 * Persistent db, but not found in the transaction buffer
1319 return db_ctdb_ltdb_parse(
1320 ctx
, key
, db_ctdb_parse_record_parser
, &state
);
1324 state
.ask_for_readonly_copy
= false;
1326 status
= db_ctdb_ltdb_parse(
1327 ctx
, key
, db_ctdb_parse_record_parser_nonpersistent
, &state
);
1328 if (NT_STATUS_IS_OK(status
) && state
.done
) {
1329 return NT_STATUS_OK
;
1332 return ctdbd_parse(messaging_ctdbd_connection(), ctx
->db_id
, key
,
1333 state
.ask_for_readonly_copy
, parser
, private_data
);
1336 struct traverse_state
{
1337 struct db_context
*db
;
1338 int (*fn
)(struct db_record
*rec
, void *private_data
);
1343 static void traverse_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1345 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1346 struct db_record
*rec
;
1347 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1348 /* we have to give them a locked record to prevent races */
1349 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, key
);
1350 if (rec
&& rec
->value
.dsize
> 0) {
1351 state
->fn(rec
, state
->private_data
);
1353 talloc_free(tmp_ctx
);
1356 static int traverse_persistent_callback(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1359 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1360 struct db_record
*rec
;
1361 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1365 * Skip the __db_sequence_number__ key:
1366 * This is used for persistent transactions internally.
1368 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1369 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1374 /* we have to give them a locked record to prevent races */
1375 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, kbuf
);
1376 if (rec
&& rec
->value
.dsize
> 0) {
1377 ret
= state
->fn(rec
, state
->private_data
);
1381 talloc_free(tmp_ctx
);
1385 /* wrapper to use traverse_persistent_callback with dbwrap */
1386 static int traverse_persistent_callback_dbwrap(struct db_record
*rec
, void* data
)
1388 return traverse_persistent_callback(NULL
, rec
->key
, rec
->value
, data
);
1392 static int db_ctdb_traverse(struct db_context
*db
,
1393 int (*fn
)(struct db_record
*rec
,
1394 void *private_data
),
1398 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1399 struct db_ctdb_ctx
);
1400 struct traverse_state state
;
1404 state
.private_data
= private_data
;
1407 if (db
->persistent
) {
1408 struct tdb_context
*ltdb
= ctx
->wtdb
->tdb
;
1411 /* for persistent databases we don't need to do a ctdb traverse,
1412 we can do a faster local traverse */
1413 ret
= tdb_traverse(ltdb
, traverse_persistent_callback
, &state
);
1417 if (ctx
->transaction
&& ctx
->transaction
->m_write
) {
1419 * we now have to handle keys not yet
1420 * present at transaction start
1422 struct db_context
*newkeys
= db_open_rbt(talloc_tos());
1423 struct ctdb_marshall_buffer
*mbuf
= ctx
->transaction
->m_write
;
1424 struct ctdb_rec_data
*rec
=NULL
;
1428 if (newkeys
== NULL
) {
1432 for (i
=0; i
<mbuf
->count
; i
++) {
1434 rec
= db_ctdb_marshall_loop_next_key(
1436 SMB_ASSERT(rec
!= NULL
);
1438 if (!tdb_exists(ltdb
, key
)) {
1439 dbwrap_store(newkeys
, key
, tdb_null
, 0);
1442 status
= dbwrap_traverse(newkeys
,
1443 traverse_persistent_callback_dbwrap
,
1446 talloc_free(newkeys
);
1447 if (!NT_STATUS_IS_OK(status
)) {
1455 status
= ctdbd_traverse(ctx
->db_id
, traverse_callback
, &state
);
1456 if (!NT_STATUS_IS_OK(status
)) {
1462 static NTSTATUS
db_ctdb_store_deny(struct db_record
*rec
, TDB_DATA data
, int flag
)
1464 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1467 static NTSTATUS
db_ctdb_delete_deny(struct db_record
*rec
)
1469 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1472 static void traverse_read_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1474 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1475 struct db_record rec
;
1481 rec
.store
= db_ctdb_store_deny
;
1482 rec
.delete_rec
= db_ctdb_delete_deny
;
1483 rec
.private_data
= NULL
;
1484 state
->fn(&rec
, state
->private_data
);
1488 static int traverse_persistent_callback_read(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1491 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1492 struct db_record rec
;
1495 * Skip the __db_sequence_number__ key:
1496 * This is used for persistent transactions internally.
1498 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1499 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1508 rec
.store
= db_ctdb_store_deny
;
1509 rec
.delete_rec
= db_ctdb_delete_deny
;
1510 rec
.private_data
= NULL
;
1512 if (rec
.value
.dsize
<= sizeof(struct ctdb_ltdb_header
)) {
1513 /* a deleted record */
1516 rec
.value
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1517 rec
.value
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1520 return state
->fn(&rec
, state
->private_data
);
1523 static int db_ctdb_traverse_read(struct db_context
*db
,
1524 int (*fn
)(struct db_record
*rec
,
1525 void *private_data
),
1529 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1530 struct db_ctdb_ctx
);
1531 struct traverse_state state
;
1535 state
.private_data
= private_data
;
1538 if (db
->persistent
) {
1539 /* for persistent databases we don't need to do a ctdb traverse,
1540 we can do a faster local traverse */
1541 return tdb_traverse_read(ctx
->wtdb
->tdb
, traverse_persistent_callback_read
, &state
);
1544 status
= ctdbd_traverse(ctx
->db_id
, traverse_read_callback
, &state
);
1545 if (!NT_STATUS_IS_OK(status
)) {
1551 static int db_ctdb_get_seqnum(struct db_context
*db
)
1553 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1554 struct db_ctdb_ctx
);
1555 return tdb_get_seqnum(ctx
->wtdb
->tdb
);
1558 static void db_ctdb_id(struct db_context
*db
, const uint8_t **id
,
1561 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1562 db
->private_data
, struct db_ctdb_ctx
);
1564 *id
= (uint8_t *)&ctx
->db_id
;
1565 *idlen
= sizeof(ctx
->db_id
);
1568 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1570 int hash_size
, int tdb_flags
,
1571 int open_flags
, mode_t mode
,
1572 enum dbwrap_lock_order lock_order
,
1573 uint64_t dbwrap_flags
)
1575 struct db_context
*result
;
1576 struct db_ctdb_ctx
*db_ctdb
;
1578 struct ctdbd_connection
*conn
;
1579 struct loadparm_context
*lp_ctx
;
1580 struct ctdb_db_priority prio
;
1584 if (!lp_clustering()) {
1585 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1589 if (!(result
= talloc_zero(mem_ctx
, struct db_context
))) {
1590 DEBUG(0, ("talloc failed\n"));
1591 TALLOC_FREE(result
);
1595 if (!(db_ctdb
= talloc(result
, struct db_ctdb_ctx
))) {
1596 DEBUG(0, ("talloc failed\n"));
1597 TALLOC_FREE(result
);
1601 result
->name
= talloc_strdup(result
, name
);
1602 if (result
->name
== NULL
) {
1603 DEBUG(0, ("talloc failed\n"));
1604 TALLOC_FREE(result
);
1608 db_ctdb
->transaction
= NULL
;
1609 db_ctdb
->db
= result
;
1611 conn
= messaging_ctdbd_connection();
1613 DEBUG(1, ("Could not connect to ctdb\n"));
1614 TALLOC_FREE(result
);
1618 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn
, name
, &db_ctdb
->db_id
, tdb_flags
))) {
1619 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name
));
1620 TALLOC_FREE(result
);
1624 db_path
= ctdbd_dbpath(conn
, db_ctdb
, db_ctdb
->db_id
);
1626 result
->persistent
= ((tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0);
1627 result
->lock_order
= lock_order
;
1629 /* only pass through specific flags */
1630 tdb_flags
&= TDB_SEQNUM
;
1632 /* honor permissions if user has specified O_CREAT */
1633 if (open_flags
& O_CREAT
) {
1634 chmod(db_path
, mode
);
1637 prio
.db_id
= db_ctdb
->db_id
;
1638 prio
.priority
= lock_order
;
1640 status
= ctdbd_control_local(
1641 conn
, CTDB_CONTROL_SET_DB_PRIORITY
, 0, 0,
1642 make_tdb_data((uint8_t *)&prio
, sizeof(prio
)),
1643 NULL
, NULL
, &cstatus
);
1645 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1646 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1647 nt_errstr(status
), cstatus
));
1648 TALLOC_FREE(result
);
1652 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1653 if (!result
->persistent
&&
1654 (dbwrap_flags
& DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS
))
1658 indata
= make_tdb_data((uint8_t *)&db_ctdb
->db_id
,
1659 sizeof(db_ctdb
->db_id
));
1661 status
= ctdbd_control_local(
1662 conn
, CTDB_CONTROL_SET_DB_READONLY
, 0, 0, indata
,
1663 NULL
, NULL
, &cstatus
);
1664 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1665 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1666 "%s, %d\n", nt_errstr(status
), cstatus
));
1667 TALLOC_FREE(result
);
1673 lp_ctx
= loadparm_init_s3(db_path
, loadparm_s3_helpers());
1675 db_ctdb
->wtdb
= tdb_wrap_open(db_ctdb
, db_path
, hash_size
, tdb_flags
,
1677 talloc_unlink(db_path
, lp_ctx
);
1678 if (db_ctdb
->wtdb
== NULL
) {
1679 DEBUG(0, ("Could not open tdb %s: %s\n", db_path
, strerror(errno
)));
1680 TALLOC_FREE(result
);
1683 talloc_free(db_path
);
1685 if (result
->persistent
) {
1686 db_ctdb
->lock_ctx
= g_lock_ctx_init(db_ctdb
,
1687 ctdb_conn_msg_ctx(conn
));
1688 if (db_ctdb
->lock_ctx
== NULL
) {
1689 DEBUG(0, ("g_lock_ctx_init failed\n"));
1690 TALLOC_FREE(result
);
1695 db_ctdb
->warn_unlock_msecs
= lp_parm_int(-1, "ctdb",
1696 "unlock_warn_threshold", 5);
1697 db_ctdb
->warn_migrate_attempts
= lp_parm_int(-1, "ctdb",
1698 "migrate_attempts", 10);
1699 db_ctdb
->warn_migrate_msecs
= lp_parm_int(-1, "ctdb",
1700 "migrate_duration", 5000);
1701 db_ctdb
->warn_locktime_msecs
= lp_ctdb_locktime_warn_threshold();
1703 result
->private_data
= (void *)db_ctdb
;
1704 result
->fetch_locked
= db_ctdb_fetch_locked
;
1705 result
->try_fetch_locked
= db_ctdb_try_fetch_locked
;
1706 result
->parse_record
= db_ctdb_parse_record
;
1707 result
->traverse
= db_ctdb_traverse
;
1708 result
->traverse_read
= db_ctdb_traverse_read
;
1709 result
->get_seqnum
= db_ctdb_get_seqnum
;
1710 result
->transaction_start
= db_ctdb_transaction_start
;
1711 result
->transaction_commit
= db_ctdb_transaction_commit
;
1712 result
->transaction_cancel
= db_ctdb_transaction_cancel
;
1713 result
->id
= db_ctdb_id
;
1714 result
->stored_callback
= NULL
;
1716 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1717 name
, db_ctdb
->db_id
));
1722 #else /* CLUSTER_SUPPORT */
1724 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1726 int hash_size
, int tdb_flags
,
1727 int open_flags
, mode_t mode
,
1728 enum dbwrap_lock_order lock_order
,
1729 uint64_t dbwrap_flags
)
1731 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));