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
)) {
114 key
, (struct ctdb_ltdb_header
*)data
.dptr
,
115 make_tdb_data(data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
116 data
.dsize
- sizeof(struct ctdb_ltdb_header
)),
117 state
->private_data
);
121 static NTSTATUS
db_ctdb_ltdb_parse(
122 struct db_ctdb_ctx
*db
, TDB_DATA key
,
123 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
124 TDB_DATA data
, void *private_data
),
127 struct db_ctdb_ltdb_parse_state state
;
130 state
.parser
= parser
;
131 state
.private_data
= private_data
;
133 ret
= tdb_parse_record(db
->wtdb
->tdb
, key
, db_ctdb_ltdb_parser
,
136 return NT_STATUS_NOT_FOUND
;
142 * Store a record together with the ctdb record header
143 * in the local copy of the database.
145 static NTSTATUS
db_ctdb_ltdb_store(struct db_ctdb_ctx
*db
,
147 struct ctdb_ltdb_header
*header
,
153 rec
.dsize
= data
.dsize
+ sizeof(struct ctdb_ltdb_header
);
154 rec
.dptr
= (uint8_t *)talloc_size(talloc_tos(), rec
.dsize
);
156 if (rec
.dptr
== NULL
) {
157 return NT_STATUS_NO_MEMORY
;
160 memcpy(rec
.dptr
, header
, sizeof(struct ctdb_ltdb_header
));
161 memcpy(sizeof(struct ctdb_ltdb_header
) + (uint8_t *)rec
.dptr
, data
.dptr
, data
.dsize
);
163 ret
= tdb_store(db
->wtdb
->tdb
, key
, rec
, TDB_REPLACE
);
165 talloc_free(rec
.dptr
);
167 return (ret
== 0) ? NT_STATUS_OK
168 : tdb_error_to_ntstatus(db
->wtdb
->tdb
);
173 form a ctdb_rec_data record from a key/data pair
175 static struct ctdb_rec_data
*db_ctdb_marshall_record(TALLOC_CTX
*mem_ctx
, uint32_t reqid
,
177 struct ctdb_ltdb_header
*header
,
181 struct ctdb_rec_data
*d
;
183 length
= offsetof(struct ctdb_rec_data
, data
) + key
.dsize
+
184 data
.dsize
+ sizeof(*header
);
185 d
= (struct ctdb_rec_data
*)talloc_size(mem_ctx
, length
);
191 d
->keylen
= key
.dsize
;
192 memcpy(&d
->data
[0], key
.dptr
, key
.dsize
);
194 d
->datalen
= data
.dsize
+ sizeof(*header
);
195 memcpy(&d
->data
[key
.dsize
], header
, sizeof(*header
));
196 memcpy(&d
->data
[key
.dsize
+sizeof(*header
)], data
.dptr
, data
.dsize
);
201 /* helper function for marshalling multiple records */
202 static struct ctdb_marshall_buffer
*db_ctdb_marshall_add(TALLOC_CTX
*mem_ctx
,
203 struct ctdb_marshall_buffer
*m
,
207 struct ctdb_ltdb_header
*header
,
210 struct ctdb_rec_data
*r
;
211 size_t m_size
, r_size
;
212 struct ctdb_marshall_buffer
*m2
= NULL
;
214 r
= db_ctdb_marshall_record(talloc_tos(), reqid
, key
, header
, data
);
221 m
= (struct ctdb_marshall_buffer
*)talloc_zero_size(
222 mem_ctx
, offsetof(struct ctdb_marshall_buffer
, data
));
229 m_size
= talloc_get_size(m
);
230 r_size
= talloc_get_size(r
);
232 m2
= (struct ctdb_marshall_buffer
*)talloc_realloc_size(
233 mem_ctx
, m
, m_size
+ r_size
);
239 memcpy(m_size
+ (uint8_t *)m2
, r
, r_size
);
248 /* we've finished marshalling, return a data blob with the marshalled records */
249 static TDB_DATA
db_ctdb_marshall_finish(struct ctdb_marshall_buffer
*m
)
252 data
.dptr
= (uint8_t *)m
;
253 data
.dsize
= talloc_get_size(m
);
258 loop over a marshalling buffer
260 - pass r==NULL to start
261 - loop the number of times indicated by m->count
263 static struct ctdb_rec_data
*db_ctdb_marshall_loop_next_key(
264 struct ctdb_marshall_buffer
*m
, struct ctdb_rec_data
*r
, TDB_DATA
*key
)
267 r
= (struct ctdb_rec_data
*)&m
->data
[0];
269 r
= (struct ctdb_rec_data
*)(r
->length
+ (uint8_t *)r
);
272 key
->dptr
= &r
->data
[0];
273 key
->dsize
= r
->keylen
;
277 static bool db_ctdb_marshall_buf_parse(
278 struct ctdb_rec_data
*r
, uint32_t *reqid
,
279 struct ctdb_ltdb_header
**header
, TDB_DATA
*data
)
281 if (r
->datalen
< sizeof(struct ctdb_ltdb_header
)) {
287 data
->dptr
= &r
->data
[r
->keylen
] + sizeof(struct ctdb_ltdb_header
);
288 data
->dsize
= r
->datalen
- sizeof(struct ctdb_ltdb_header
);
290 *header
= (struct ctdb_ltdb_header
*)&r
->data
[r
->keylen
];
296 * CTDB transaction destructor
298 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle
*h
)
302 status
= g_lock_unlock(h
->ctx
->lock_ctx
, h
->lock_name
);
303 if (!NT_STATUS_IS_OK(status
)) {
304 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h
->lock_name
,
312 * CTDB dbwrap API: transaction_start function
313 * starts a transaction on a persistent database
315 static int db_ctdb_transaction_start(struct db_context
*db
)
317 struct db_ctdb_transaction_handle
*h
;
319 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
322 if (!db
->persistent
) {
323 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
328 if (ctx
->transaction
) {
329 ctx
->transaction
->nesting
++;
330 DEBUG(5, (__location__
" transaction start on db 0x%08x: nesting %d -> %d\n",
331 ctx
->db_id
, ctx
->transaction
->nesting
- 1, ctx
->transaction
->nesting
));
335 h
= talloc_zero(db
, struct db_ctdb_transaction_handle
);
337 DEBUG(0,(__location__
" oom for transaction handle\n"));
343 h
->lock_name
= talloc_asprintf(h
, "transaction_db_0x%08x",
344 (unsigned int)ctx
->db_id
);
345 if (h
->lock_name
== NULL
) {
346 DEBUG(0, ("talloc_asprintf failed\n"));
352 * Wait a day, i.e. forever...
354 status
= g_lock_lock(ctx
->lock_ctx
, h
->lock_name
, G_LOCK_WRITE
,
355 timeval_set(86400, 0));
356 if (!NT_STATUS_IS_OK(status
)) {
357 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status
)));
362 talloc_set_destructor(h
, db_ctdb_transaction_destructor
);
364 ctx
->transaction
= h
;
366 DEBUG(5,(__location__
" transaction started on db 0x%08x\n", ctx
->db_id
));
371 static bool parse_newest_in_marshall_buffer(
372 struct ctdb_marshall_buffer
*buf
, TDB_DATA key
,
373 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
374 TDB_DATA data
, void *private_data
),
377 struct ctdb_rec_data
*rec
= NULL
;
378 struct ctdb_ltdb_header
*h
= NULL
;
387 * Walk the list of records written during this
388 * transaction. If we want to read one we have already
389 * written, return the last written sample. Thus we do not do
390 * a "break;" for the first hit, this record might have been
394 for (i
=0; i
<buf
->count
; i
++) {
398 rec
= db_ctdb_marshall_loop_next_key(buf
, rec
, &tkey
);
403 if (!tdb_data_equal(key
, tkey
)) {
407 if (!db_ctdb_marshall_buf_parse(rec
, &reqid
, &h
, &data
)) {
416 parser(key
, h
, data
, private_data
);
421 struct pull_newest_from_marshall_buffer_state
{
422 struct ctdb_ltdb_header
*pheader
;
427 static void pull_newest_from_marshall_buffer_parser(
428 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
429 TDB_DATA data
, void *private_data
)
431 struct pull_newest_from_marshall_buffer_state
*state
=
432 (struct pull_newest_from_marshall_buffer_state
*)private_data
;
434 if (state
->pheader
!= NULL
) {
435 memcpy(state
->pheader
, header
, sizeof(*state
->pheader
));
437 if (state
->pdata
!= NULL
) {
438 state
->pdata
->dsize
= data
.dsize
;
439 state
->pdata
->dptr
= (uint8_t *)talloc_memdup(
440 state
->mem_ctx
, data
.dptr
, data
.dsize
);
444 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer
*buf
,
446 struct ctdb_ltdb_header
*pheader
,
450 struct pull_newest_from_marshall_buffer_state state
;
452 state
.pheader
= pheader
;
453 state
.mem_ctx
= mem_ctx
;
456 if (!parse_newest_in_marshall_buffer(
457 buf
, key
, pull_newest_from_marshall_buffer_parser
,
461 if ((pdata
!= NULL
) && (pdata
->dsize
!= 0) && (pdata
->dptr
== NULL
)) {
468 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
);
469 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
);
471 static struct db_record
*db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx
*ctx
,
475 struct db_record
*result
;
478 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
479 DEBUG(0, ("talloc failed\n"));
483 result
->db
= ctx
->db
;
484 result
->private_data
= ctx
->transaction
;
486 result
->key
.dsize
= key
.dsize
;
487 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
489 if (result
->key
.dptr
== NULL
) {
490 DEBUG(0, ("talloc failed\n"));
495 result
->store
= db_ctdb_store_transaction
;
496 result
->delete_rec
= db_ctdb_delete_transaction
;
498 if (pull_newest_from_marshall_buffer(ctx
->transaction
->m_write
, key
,
499 NULL
, result
, &result
->value
)) {
503 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
504 if (ctdb_data
.dptr
== NULL
) {
505 /* create the record */
506 result
->value
= tdb_null
;
510 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
511 result
->value
.dptr
= NULL
;
513 if ((result
->value
.dsize
!= 0)
514 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
515 result
, ctdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
516 result
->value
.dsize
))) {
517 DEBUG(0, ("talloc failed\n"));
521 SAFE_FREE(ctdb_data
.dptr
);
526 static int db_ctdb_record_destructor(struct db_record
**recp
)
528 struct db_record
*rec
= talloc_get_type_abort(*recp
, struct db_record
);
529 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
530 rec
->private_data
, struct db_ctdb_transaction_handle
);
531 int ret
= h
->ctx
->db
->transaction_commit(h
->ctx
->db
);
533 DEBUG(0,(__location__
" transaction_commit failed\n"));
539 auto-create a transaction for persistent databases
541 static struct db_record
*db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx
*ctx
,
546 struct db_record
*rec
, **recp
;
548 res
= db_ctdb_transaction_start(ctx
->db
);
553 rec
= db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
555 ctx
->db
->transaction_cancel(ctx
->db
);
559 /* destroy this transaction when we release the lock */
560 recp
= talloc(rec
, struct db_record
*);
562 ctx
->db
->transaction_cancel(ctx
->db
);
567 talloc_set_destructor(recp
, db_ctdb_record_destructor
);
573 stores a record inside a transaction
575 static NTSTATUS
db_ctdb_transaction_store(struct db_ctdb_transaction_handle
*h
,
576 TDB_DATA key
, TDB_DATA data
)
578 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
580 struct ctdb_ltdb_header header
;
584 /* we need the header so we can update the RSN */
586 if (!pull_newest_from_marshall_buffer(h
->m_write
, key
, &header
,
589 rec
= tdb_fetch_compat(h
->ctx
->wtdb
->tdb
, key
);
591 if (rec
.dptr
!= NULL
) {
592 memcpy(&header
, rec
.dptr
,
593 sizeof(struct ctdb_ltdb_header
));
594 rec
.dsize
-= sizeof(struct ctdb_ltdb_header
);
597 * a special case, we are writing the same
598 * data that is there now
600 if (data
.dsize
== rec
.dsize
&&
602 rec
.dptr
+ sizeof(struct ctdb_ltdb_header
),
605 talloc_free(tmp_ctx
);
612 header
.dmaster
= get_my_vnn();
615 h
->m_write
= db_ctdb_marshall_add(h
, h
->m_write
, h
->ctx
->db_id
, 0, key
, &header
, data
);
616 if (h
->m_write
== NULL
) {
617 DEBUG(0,(__location__
" Failed to add to marshalling record\n"));
618 talloc_free(tmp_ctx
);
619 return NT_STATUS_NO_MEMORY
;
622 talloc_free(tmp_ctx
);
628 a record store inside a transaction
630 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
)
632 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
633 rec
->private_data
, struct db_ctdb_transaction_handle
);
636 status
= db_ctdb_transaction_store(h
, rec
->key
, data
);
641 a record delete inside a transaction
643 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
)
645 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
646 rec
->private_data
, struct db_ctdb_transaction_handle
);
649 status
= db_ctdb_transaction_store(h
, rec
->key
, tdb_null
);
653 static void db_ctdb_fetch_db_seqnum_parser(
654 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
655 TDB_DATA data
, void *private_data
)
657 uint64_t *seqnum
= (uint64_t *)private_data
;
659 if (data
.dsize
!= sizeof(uint64_t)) {
663 memcpy(seqnum
, data
.dptr
, sizeof(*seqnum
));
667 * Fetch the db sequence number of a persistent db directly from the db.
669 static NTSTATUS
db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx
*db
,
675 if (seqnum
== NULL
) {
676 return NT_STATUS_INVALID_PARAMETER
;
679 key
= string_term_tdb_data(CTDB_DB_SEQNUM_KEY
);
681 status
= db_ctdb_ltdb_parse(
682 db
, key
, db_ctdb_fetch_db_seqnum_parser
, seqnum
);
684 if (NT_STATUS_IS_OK(status
)) {
687 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
695 * Store the database sequence number inside a transaction.
697 static NTSTATUS
db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle
*h
,
701 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
705 key
= string_term_tdb_data(keyname
);
707 data
.dptr
= (uint8_t *)&seqnum
;
708 data
.dsize
= sizeof(uint64_t);
710 status
= db_ctdb_transaction_store(h
, key
, data
);
718 static int db_ctdb_transaction_commit(struct db_context
*db
)
720 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
724 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
725 uint64_t old_seqnum
, new_seqnum
;
729 DEBUG(0,(__location__
" transaction commit with no open transaction on db 0x%08x\n", ctx
->db_id
));
733 if (h
->nested_cancel
) {
734 db
->transaction_cancel(db
);
735 DEBUG(5,(__location__
" Failed transaction commit after nested cancel\n"));
739 if (h
->nesting
!= 0) {
741 DEBUG(5, (__location__
" transaction commit on db 0x%08x: nesting %d -> %d\n",
742 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
746 if (h
->m_write
== NULL
) {
748 * No changes were made, so don't change the seqnum,
749 * don't push to other node, just exit with success.
755 DEBUG(5,(__location__
" transaction commit on db 0x%08x\n", ctx
->db_id
));
758 * As the last db action before committing, bump the database sequence
759 * number. Note that this undoes all changes to the seqnum records
760 * performed under the transaction. This record is not meant to be
761 * modified by user interaction. It is for internal use only...
763 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &old_seqnum
);
764 if (!NT_STATUS_IS_OK(rets
)) {
765 DEBUG(1, (__location__
" failed to fetch the db sequence number "
766 "in transaction commit on db 0x%08x\n", ctx
->db_id
));
771 new_seqnum
= old_seqnum
+ 1;
773 rets
= db_ctdb_store_db_seqnum(h
, new_seqnum
);
774 if (!NT_STATUS_IS_OK(rets
)) {
775 DEBUG(1, (__location__
"failed to store the db sequence number "
776 " in transaction commit on db 0x%08x\n", ctx
->db_id
));
782 /* tell ctdbd to commit to the other nodes */
783 rets
= ctdbd_control_local(messaging_ctdbd_connection(),
784 CTDB_CONTROL_TRANS3_COMMIT
,
786 db_ctdb_marshall_finish(h
->m_write
),
787 NULL
, NULL
, &status
);
788 if (!NT_STATUS_IS_OK(rets
) || status
!= 0) {
790 * The TRANS3_COMMIT control should only possibly fail when a
791 * recovery has been running concurrently. In any case, the db
792 * will be the same on all nodes, either the new copy or the
793 * old copy. This can be detected by comparing the old and new
794 * local sequence numbers.
796 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &new_seqnum
);
797 if (!NT_STATUS_IS_OK(rets
)) {
798 DEBUG(1, (__location__
" failed to refetch db sequence "
799 "number after failed TRANS3_COMMIT\n"));
804 if (new_seqnum
== old_seqnum
) {
805 /* Recovery prevented all our changes: retry. */
808 if (new_seqnum
!= (old_seqnum
+ 1)) {
809 DEBUG(0, (__location__
" ERROR: new_seqnum[%lu] != "
810 "old_seqnum[%lu] + (0 or 1) after failed "
811 "TRANS3_COMMIT - this should not happen!\n",
812 (unsigned long)new_seqnum
,
813 (unsigned long)old_seqnum
));
818 * Recovery propagated our changes to all nodes, completing
819 * our commit for us - succeed.
826 h
->ctx
->transaction
= NULL
;
835 static int db_ctdb_transaction_cancel(struct db_context
*db
)
837 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
839 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
842 DEBUG(0,(__location__
" transaction cancel with no open transaction on db 0x%08x\n", ctx
->db_id
));
846 if (h
->nesting
!= 0) {
848 h
->nested_cancel
= true;
849 DEBUG(5, (__location__
" transaction cancel on db 0x%08x: nesting %d -> %d\n",
850 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
854 DEBUG(5,(__location__
" Cancel transaction on db 0x%08x\n", ctx
->db_id
));
856 ctx
->transaction
= NULL
;
862 static NTSTATUS
db_ctdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
)
864 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
865 rec
->private_data
, struct db_ctdb_rec
);
867 return db_ctdb_ltdb_store(crec
->ctdb_ctx
, rec
->key
, &(crec
->header
), data
);
872 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
873 static NTSTATUS
db_ctdb_send_schedule_for_deletion(struct db_record
*rec
)
876 struct ctdb_control_schedule_for_deletion
*dd
;
879 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
880 rec
->private_data
, struct db_ctdb_rec
);
882 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + rec
->key
.dsize
;
883 indata
.dptr
= talloc_zero_array(crec
, uint8_t, indata
.dsize
);
884 if (indata
.dptr
== NULL
) {
885 DEBUG(0, (__location__
" talloc failed!\n"));
886 return NT_STATUS_NO_MEMORY
;
889 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
890 dd
->db_id
= crec
->ctdb_ctx
->db_id
;
891 dd
->hdr
= crec
->header
;
892 dd
->keylen
= rec
->key
.dsize
;
893 memcpy(dd
->key
, rec
->key
.dptr
, rec
->key
.dsize
);
895 status
= ctdbd_control_local(messaging_ctdbd_connection(),
896 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
897 crec
->ctdb_ctx
->db_id
,
898 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
903 talloc_free(indata
.dptr
);
905 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
906 DEBUG(1, (__location__
" Error sending local control "
907 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
908 nt_errstr(status
), cstatus
));
909 if (NT_STATUS_IS_OK(status
)) {
910 status
= NT_STATUS_UNSUCCESSFUL
;
918 static NTSTATUS
db_ctdb_delete(struct db_record
*rec
)
923 * We have to store the header with empty data. TODO: Fix the
927 status
= db_ctdb_store(rec
, tdb_null
, 0);
928 if (!NT_STATUS_IS_OK(status
)) {
932 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
933 status
= db_ctdb_send_schedule_for_deletion(rec
);
939 static int db_ctdb_record_destr(struct db_record
* data
)
941 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
942 data
->private_data
, struct db_ctdb_rec
);
945 struct timeval before
;
948 DEBUG(10, (DEBUGLEVEL
> 10
949 ? "Unlocking db %u key %s\n"
950 : "Unlocking db %u key %.20s\n",
951 (int)crec
->ctdb_ctx
->db_id
,
952 hex_encode_talloc(data
, (unsigned char *)data
->key
.dptr
,
955 before
= timeval_current();
957 ret
= tdb_chainunlock(crec
->ctdb_ctx
->wtdb
->tdb
, data
->key
);
959 timediff
= timeval_elapsed(&before
);
960 timediff
*= 1000; /* get us milliseconds */
962 if (timediff
> crec
->ctdb_ctx
->warn_unlock_msecs
) {
964 key
= hex_encode_talloc(talloc_tos(),
965 (unsigned char *)data
->key
.dptr
,
967 DEBUG(0, ("tdb_chainunlock on db %s, key %s took %f milliseconds\n",
968 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
), key
,
974 DEBUG(0, ("tdb_chainunlock failed\n"));
978 threshold
= crec
->ctdb_ctx
->warn_locktime_msecs
;
979 if (threshold
!= 0) {
980 timediff
= timeval_elapsed(&crec
->lock_time
) * 1000;
981 if (timediff
> threshold
) {
984 key
= hex_encode_talloc(data
,
985 (unsigned char *)data
->key
.dptr
,
987 DEBUG(0, ("Held tdb lock on db %s, key %s "
989 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
),
998 * Check whether we have a valid local copy of the given record,
999 * either for reading or for writing.
1001 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header
*hdr
,
1004 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1005 if (hdr
->dmaster
!= get_my_vnn()) {
1006 /* If we're not dmaster, it must be r/o copy. */
1007 return read_only
&& (hdr
->flags
& CTDB_REC_RO_HAVE_READONLY
);
1011 * If we want write access, no one may have r/o copies.
1013 return read_only
|| !(hdr
->flags
& CTDB_REC_RO_HAVE_DELEGATIONS
);
1015 return (hdr
->dmaster
== get_my_vnn());
1019 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data
, bool read_only
)
1021 if (ctdb_data
.dptr
== NULL
) {
1025 if (ctdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
1029 return db_ctdb_can_use_local_hdr(
1030 (struct ctdb_ltdb_header
*)ctdb_data
.dptr
, read_only
);
1033 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
1034 TALLOC_CTX
*mem_ctx
,
1038 struct db_record
*result
;
1039 struct db_ctdb_rec
*crec
;
1042 int migrate_attempts
;
1043 struct timeval migrate_start
;
1044 struct timeval chainlock_start
;
1045 struct timeval ctdb_start_time
;
1046 double chainlock_time
= 0;
1047 double ctdb_time
= 0;
1051 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
1052 DEBUG(0, ("talloc failed\n"));
1056 if (!(crec
= talloc_zero(result
, struct db_ctdb_rec
))) {
1057 DEBUG(0, ("talloc failed\n"));
1058 TALLOC_FREE(result
);
1062 result
->db
= ctx
->db
;
1063 result
->private_data
= (void *)crec
;
1064 crec
->ctdb_ctx
= ctx
;
1066 result
->key
.dsize
= key
.dsize
;
1067 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
1069 if (result
->key
.dptr
== NULL
) {
1070 DEBUG(0, ("talloc failed\n"));
1071 TALLOC_FREE(result
);
1075 migrate_attempts
= 0;
1076 GetTimeOfDay(&migrate_start
);
1079 * Do a blocking lock on the record
1083 if (DEBUGLEVEL
>= 10) {
1084 char *keystr
= hex_encode_talloc(result
, key
.dptr
, key
.dsize
);
1085 DEBUG(10, (DEBUGLEVEL
> 10
1086 ? "Locking db %u key %s\n"
1087 : "Locking db %u key %.20s\n",
1088 (int)crec
->ctdb_ctx
->db_id
, keystr
));
1089 TALLOC_FREE(keystr
);
1092 GetTimeOfDay(&chainlock_start
);
1094 ? tdb_chainlock_nonblock(ctx
->wtdb
->tdb
, key
)
1095 : tdb_chainlock(ctx
->wtdb
->tdb
, key
);
1096 chainlock_time
+= timeval_elapsed(&chainlock_start
);
1099 DEBUG(3, ("tdb_chainlock failed\n"));
1100 TALLOC_FREE(result
);
1104 result
->store
= db_ctdb_store
;
1105 result
->delete_rec
= db_ctdb_delete
;
1106 talloc_set_destructor(result
, db_ctdb_record_destr
);
1108 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
1111 * See if we have a valid record and we are the dmaster. If so, we can
1112 * take the shortcut and just return it.
1115 if (!db_ctdb_can_use_local_copy(ctdb_data
, false)) {
1116 SAFE_FREE(ctdb_data
.dptr
);
1117 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
1118 talloc_set_destructor(result
, NULL
);
1120 if (tryonly
&& (migrate_attempts
!= 0)) {
1121 DEBUG(5, ("record migrated away again\n"));
1122 TALLOC_FREE(result
);
1126 migrate_attempts
+= 1;
1128 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1129 ctdb_data
.dptr
, ctdb_data
.dptr
?
1130 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
: -1,
1133 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->flags
: 0));
1135 GetTimeOfDay(&ctdb_start_time
);
1136 status
= ctdbd_migrate(messaging_ctdbd_connection(), ctx
->db_id
,
1138 ctdb_time
+= timeval_elapsed(&ctdb_start_time
);
1140 if (!NT_STATUS_IS_OK(status
)) {
1141 DEBUG(5, ("ctdb_migrate failed: %s\n",
1142 nt_errstr(status
)));
1143 TALLOC_FREE(result
);
1146 /* now its migrated, try again */
1152 duration
= timeval_elapsed(&migrate_start
);
1155 * Convert the duration to milliseconds to avoid a
1156 * floating-point division of
1157 * lp_parm_int("migrate_duration") by 1000.
1159 duration_msecs
= duration
* 1000;
1162 if ((migrate_attempts
> ctx
->warn_migrate_attempts
) ||
1163 (duration_msecs
> ctx
->warn_migrate_msecs
)) {
1166 if (tdb_get_flags(ctx
->wtdb
->tdb
) & TDB_INCOMPATIBLE_HASH
) {
1167 chain
= tdb_jenkins_hash(&key
) %
1168 tdb_hash_size(ctx
->wtdb
->tdb
);
1171 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s, chain %d "
1172 "needed %d attempts, %d milliseconds, "
1173 "chainlock: %d ms, CTDB %d ms\n",
1174 tdb_name(ctx
->wtdb
->tdb
),
1175 hex_encode_talloc(talloc_tos(),
1176 (unsigned char *)key
.dptr
,
1179 migrate_attempts
, duration_msecs
,
1180 (int) chainlock_time
* 1000,
1181 (int) ctdb_time
* 1000));
1184 GetTimeOfDay(&crec
->lock_time
);
1186 memcpy(&crec
->header
, ctdb_data
.dptr
, sizeof(crec
->header
));
1188 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(crec
->header
);
1189 result
->value
.dptr
= NULL
;
1191 if ((result
->value
.dsize
!= 0)
1192 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
1193 result
, ctdb_data
.dptr
+ sizeof(crec
->header
),
1194 result
->value
.dsize
))) {
1195 DEBUG(0, ("talloc failed\n"));
1196 TALLOC_FREE(result
);
1199 SAFE_FREE(ctdb_data
.dptr
);
1204 static struct db_record
*db_ctdb_fetch_locked(struct db_context
*db
,
1205 TALLOC_CTX
*mem_ctx
,
1208 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1209 struct db_ctdb_ctx
);
1211 if (ctx
->transaction
!= NULL
) {
1212 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1215 if (db
->persistent
) {
1216 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1219 return fetch_locked_internal(ctx
, mem_ctx
, key
, false);
1222 static struct db_record
*db_ctdb_try_fetch_locked(struct db_context
*db
,
1223 TALLOC_CTX
*mem_ctx
,
1226 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1227 struct db_ctdb_ctx
);
1229 if (ctx
->transaction
!= NULL
) {
1230 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1233 if (db
->persistent
) {
1234 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1237 return fetch_locked_internal(ctx
, mem_ctx
, key
, true);
1240 struct db_ctdb_parse_record_state
{
1241 void (*parser
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1243 bool ask_for_readonly_copy
;
1247 static void db_ctdb_parse_record_parser(
1248 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1249 TDB_DATA data
, void *private_data
)
1251 struct db_ctdb_parse_record_state
*state
=
1252 (struct db_ctdb_parse_record_state
*)private_data
;
1253 state
->parser(key
, data
, state
->private_data
);
1256 static void db_ctdb_parse_record_parser_nonpersistent(
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
;
1263 if (db_ctdb_can_use_local_hdr(header
, true)) {
1264 state
->parser(key
, data
, state
->private_data
);
1268 * We found something in the db, so it seems that this record,
1269 * while not usable locally right now, is popular. Ask for a
1272 state
->ask_for_readonly_copy
= true;
1276 static NTSTATUS
db_ctdb_parse_record(struct db_context
*db
, TDB_DATA key
,
1277 void (*parser
)(TDB_DATA key
,
1279 void *private_data
),
1282 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1283 db
->private_data
, struct db_ctdb_ctx
);
1284 struct db_ctdb_parse_record_state state
;
1287 state
.parser
= parser
;
1288 state
.private_data
= private_data
;
1290 if (ctx
->transaction
!= NULL
) {
1291 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
1295 * Transactions only happen for persistent db's.
1298 found
= parse_newest_in_marshall_buffer(
1299 h
->m_write
, key
, db_ctdb_parse_record_parser
, &state
);
1302 return NT_STATUS_OK
;
1306 if (db
->persistent
) {
1308 * Persistent db, but not found in the transaction buffer
1310 return db_ctdb_ltdb_parse(
1311 ctx
, key
, db_ctdb_parse_record_parser
, &state
);
1315 state
.ask_for_readonly_copy
= false;
1317 status
= db_ctdb_ltdb_parse(
1318 ctx
, key
, db_ctdb_parse_record_parser_nonpersistent
, &state
);
1319 if (NT_STATUS_IS_OK(status
) && state
.done
) {
1320 return NT_STATUS_OK
;
1323 return ctdbd_parse(messaging_ctdbd_connection(), ctx
->db_id
, key
,
1324 state
.ask_for_readonly_copy
, parser
, private_data
);
1327 struct traverse_state
{
1328 struct db_context
*db
;
1329 int (*fn
)(struct db_record
*rec
, void *private_data
);
1334 static void traverse_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1336 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1337 struct db_record
*rec
;
1338 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1339 /* we have to give them a locked record to prevent races */
1340 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, key
);
1341 if (rec
&& rec
->value
.dsize
> 0) {
1342 state
->fn(rec
, state
->private_data
);
1344 talloc_free(tmp_ctx
);
1347 static int traverse_persistent_callback(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1350 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1351 struct db_record
*rec
;
1352 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1356 * Skip the __db_sequence_number__ key:
1357 * This is used for persistent transactions internally.
1359 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1360 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1365 /* we have to give them a locked record to prevent races */
1366 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, kbuf
);
1367 if (rec
&& rec
->value
.dsize
> 0) {
1368 ret
= state
->fn(rec
, state
->private_data
);
1372 talloc_free(tmp_ctx
);
1376 /* wrapper to use traverse_persistent_callback with dbwrap */
1377 static int traverse_persistent_callback_dbwrap(struct db_record
*rec
, void* data
)
1379 return traverse_persistent_callback(NULL
, rec
->key
, rec
->value
, data
);
1383 static int db_ctdb_traverse(struct db_context
*db
,
1384 int (*fn
)(struct db_record
*rec
,
1385 void *private_data
),
1389 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1390 struct db_ctdb_ctx
);
1391 struct traverse_state state
;
1395 state
.private_data
= private_data
;
1398 if (db
->persistent
) {
1399 struct tdb_context
*ltdb
= ctx
->wtdb
->tdb
;
1402 /* for persistent databases we don't need to do a ctdb traverse,
1403 we can do a faster local traverse */
1404 ret
= tdb_traverse(ltdb
, traverse_persistent_callback
, &state
);
1408 if (ctx
->transaction
&& ctx
->transaction
->m_write
) {
1410 * we now have to handle keys not yet
1411 * present at transaction start
1413 struct db_context
*newkeys
= db_open_rbt(talloc_tos());
1414 struct ctdb_marshall_buffer
*mbuf
= ctx
->transaction
->m_write
;
1415 struct ctdb_rec_data
*rec
=NULL
;
1419 if (newkeys
== NULL
) {
1423 for (i
=0; i
<mbuf
->count
; i
++) {
1425 rec
= db_ctdb_marshall_loop_next_key(
1427 SMB_ASSERT(rec
!= NULL
);
1429 if (!tdb_exists(ltdb
, key
)) {
1430 dbwrap_store(newkeys
, key
, tdb_null
, 0);
1433 status
= dbwrap_traverse(newkeys
,
1434 traverse_persistent_callback_dbwrap
,
1437 talloc_free(newkeys
);
1438 if (!NT_STATUS_IS_OK(status
)) {
1446 status
= ctdbd_traverse(ctx
->db_id
, traverse_callback
, &state
);
1447 if (!NT_STATUS_IS_OK(status
)) {
1453 static NTSTATUS
db_ctdb_store_deny(struct db_record
*rec
, TDB_DATA data
, int flag
)
1455 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1458 static NTSTATUS
db_ctdb_delete_deny(struct db_record
*rec
)
1460 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1463 static void traverse_read_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1465 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1466 struct db_record rec
;
1472 rec
.store
= db_ctdb_store_deny
;
1473 rec
.delete_rec
= db_ctdb_delete_deny
;
1474 rec
.private_data
= NULL
;
1475 state
->fn(&rec
, state
->private_data
);
1479 static int traverse_persistent_callback_read(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1482 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1483 struct db_record rec
;
1486 * Skip the __db_sequence_number__ key:
1487 * This is used for persistent transactions internally.
1489 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1490 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1499 rec
.store
= db_ctdb_store_deny
;
1500 rec
.delete_rec
= db_ctdb_delete_deny
;
1501 rec
.private_data
= NULL
;
1503 if (rec
.value
.dsize
<= sizeof(struct ctdb_ltdb_header
)) {
1504 /* a deleted record */
1507 rec
.value
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1508 rec
.value
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1511 return state
->fn(&rec
, state
->private_data
);
1514 static int db_ctdb_traverse_read(struct db_context
*db
,
1515 int (*fn
)(struct db_record
*rec
,
1516 void *private_data
),
1520 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1521 struct db_ctdb_ctx
);
1522 struct traverse_state state
;
1526 state
.private_data
= private_data
;
1529 if (db
->persistent
) {
1530 /* for persistent databases we don't need to do a ctdb traverse,
1531 we can do a faster local traverse */
1532 return tdb_traverse_read(ctx
->wtdb
->tdb
, traverse_persistent_callback_read
, &state
);
1535 status
= ctdbd_traverse(ctx
->db_id
, traverse_read_callback
, &state
);
1536 if (!NT_STATUS_IS_OK(status
)) {
1542 static int db_ctdb_get_seqnum(struct db_context
*db
)
1544 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1545 struct db_ctdb_ctx
);
1546 return tdb_get_seqnum(ctx
->wtdb
->tdb
);
1549 static void db_ctdb_id(struct db_context
*db
, const uint8_t **id
,
1552 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1553 db
->private_data
, struct db_ctdb_ctx
);
1555 *id
= (uint8_t *)&ctx
->db_id
;
1556 *idlen
= sizeof(ctx
->db_id
);
1559 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1561 int hash_size
, int tdb_flags
,
1562 int open_flags
, mode_t mode
,
1563 enum dbwrap_lock_order lock_order
,
1564 uint64_t dbwrap_flags
)
1566 struct db_context
*result
;
1567 struct db_ctdb_ctx
*db_ctdb
;
1569 struct ctdbd_connection
*conn
;
1570 struct loadparm_context
*lp_ctx
;
1571 struct ctdb_db_priority prio
;
1575 if (!lp_clustering()) {
1576 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1580 if (!(result
= talloc_zero(mem_ctx
, struct db_context
))) {
1581 DEBUG(0, ("talloc failed\n"));
1582 TALLOC_FREE(result
);
1586 if (!(db_ctdb
= talloc(result
, struct db_ctdb_ctx
))) {
1587 DEBUG(0, ("talloc failed\n"));
1588 TALLOC_FREE(result
);
1592 result
->name
= talloc_strdup(result
, name
);
1593 if (result
->name
== NULL
) {
1594 DEBUG(0, ("talloc failed\n"));
1595 TALLOC_FREE(result
);
1599 db_ctdb
->transaction
= NULL
;
1600 db_ctdb
->db
= result
;
1602 conn
= messaging_ctdbd_connection();
1604 DEBUG(1, ("Could not connect to ctdb\n"));
1605 TALLOC_FREE(result
);
1609 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn
, name
, &db_ctdb
->db_id
, tdb_flags
))) {
1610 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name
));
1611 TALLOC_FREE(result
);
1615 db_path
= ctdbd_dbpath(conn
, db_ctdb
, db_ctdb
->db_id
);
1617 result
->persistent
= ((tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0);
1618 result
->lock_order
= lock_order
;
1620 /* only pass through specific flags */
1621 tdb_flags
&= TDB_SEQNUM
;
1623 /* honor permissions if user has specified O_CREAT */
1624 if (open_flags
& O_CREAT
) {
1625 chmod(db_path
, mode
);
1628 prio
.db_id
= db_ctdb
->db_id
;
1629 prio
.priority
= lock_order
;
1631 status
= ctdbd_control_local(
1632 conn
, CTDB_CONTROL_SET_DB_PRIORITY
, 0, 0,
1633 make_tdb_data((uint8_t *)&prio
, sizeof(prio
)),
1634 NULL
, NULL
, &cstatus
);
1636 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1637 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1638 nt_errstr(status
), cstatus
));
1639 TALLOC_FREE(result
);
1643 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1644 if (!result
->persistent
&&
1645 (dbwrap_flags
& DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS
))
1649 indata
= make_tdb_data((uint8_t *)&db_ctdb
->db_id
,
1650 sizeof(db_ctdb
->db_id
));
1652 status
= ctdbd_control_local(
1653 conn
, CTDB_CONTROL_SET_DB_READONLY
, 0, 0, indata
,
1654 NULL
, NULL
, &cstatus
);
1655 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1656 DEBUG(1, ("CTDB_CONTROL_SET_DB_READONLY failed: "
1657 "%s, %d\n", nt_errstr(status
), cstatus
));
1658 TALLOC_FREE(result
);
1664 lp_ctx
= loadparm_init_s3(db_path
, loadparm_s3_helpers());
1666 db_ctdb
->wtdb
= tdb_wrap_open(db_ctdb
, db_path
, hash_size
, tdb_flags
,
1668 talloc_unlink(db_path
, lp_ctx
);
1669 if (db_ctdb
->wtdb
== NULL
) {
1670 DEBUG(0, ("Could not open tdb %s: %s\n", db_path
, strerror(errno
)));
1671 TALLOC_FREE(result
);
1674 talloc_free(db_path
);
1676 if (result
->persistent
) {
1677 db_ctdb
->lock_ctx
= g_lock_ctx_init(db_ctdb
,
1678 ctdb_conn_msg_ctx(conn
));
1679 if (db_ctdb
->lock_ctx
== NULL
) {
1680 DEBUG(0, ("g_lock_ctx_init failed\n"));
1681 TALLOC_FREE(result
);
1686 db_ctdb
->warn_unlock_msecs
= lp_parm_int(-1, "ctdb",
1687 "unlock_warn_threshold", 5);
1688 db_ctdb
->warn_migrate_attempts
= lp_parm_int(-1, "ctdb",
1689 "migrate_attempts", 10);
1690 db_ctdb
->warn_migrate_msecs
= lp_parm_int(-1, "ctdb",
1691 "migrate_duration", 5000);
1692 db_ctdb
->warn_locktime_msecs
= lp_ctdb_locktime_warn_threshold();
1694 result
->private_data
= (void *)db_ctdb
;
1695 result
->fetch_locked
= db_ctdb_fetch_locked
;
1696 result
->try_fetch_locked
= db_ctdb_try_fetch_locked
;
1697 result
->parse_record
= db_ctdb_parse_record
;
1698 result
->traverse
= db_ctdb_traverse
;
1699 result
->traverse_read
= db_ctdb_traverse_read
;
1700 result
->get_seqnum
= db_ctdb_get_seqnum
;
1701 result
->transaction_start
= db_ctdb_transaction_start
;
1702 result
->transaction_commit
= db_ctdb_transaction_commit
;
1703 result
->transaction_cancel
= db_ctdb_transaction_cancel
;
1704 result
->id
= db_ctdb_id
;
1705 result
->stored_callback
= NULL
;
1707 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1708 name
, db_ctdb
->db_id
));