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
;
79 struct db_ctdb_ctx
*ctdb_ctx
;
80 struct ctdb_ltdb_header header
;
81 struct timeval lock_time
;
84 static NTSTATUS
tdb_error_to_ntstatus(struct tdb_context
*tdb
)
86 enum TDB_ERROR tret
= tdb_error(tdb
);
88 return map_nt_error_from_tdb(tret
);
91 struct db_ctdb_ltdb_parse_state
{
92 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
93 TDB_DATA data
, void *private_data
);
97 static int db_ctdb_ltdb_parser(TDB_DATA key
, TDB_DATA data
,
100 struct db_ctdb_ltdb_parse_state
*state
=
101 (struct db_ctdb_ltdb_parse_state
*)private_data
;
103 if (data
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
107 key
, (struct ctdb_ltdb_header
*)data
.dptr
,
108 make_tdb_data(data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
109 data
.dsize
- sizeof(struct ctdb_ltdb_header
)),
110 state
->private_data
);
114 static NTSTATUS
db_ctdb_ltdb_parse(
115 struct db_ctdb_ctx
*db
, TDB_DATA key
,
116 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
117 TDB_DATA data
, void *private_data
),
120 struct db_ctdb_ltdb_parse_state state
;
123 state
.parser
= parser
;
124 state
.private_data
= private_data
;
126 ret
= tdb_parse_record(db
->wtdb
->tdb
, key
, db_ctdb_ltdb_parser
,
129 return NT_STATUS_NOT_FOUND
;
135 * Store a record together with the ctdb record header
136 * in the local copy of the database.
138 static NTSTATUS
db_ctdb_ltdb_store(struct db_ctdb_ctx
*db
,
140 struct ctdb_ltdb_header
*header
,
146 rec
.dsize
= data
.dsize
+ sizeof(struct ctdb_ltdb_header
);
147 rec
.dptr
= (uint8_t *)talloc_size(talloc_tos(), rec
.dsize
);
149 if (rec
.dptr
== NULL
) {
150 return NT_STATUS_NO_MEMORY
;
153 memcpy(rec
.dptr
, header
, sizeof(struct ctdb_ltdb_header
));
154 memcpy(sizeof(struct ctdb_ltdb_header
) + (uint8_t *)rec
.dptr
, data
.dptr
, data
.dsize
);
156 ret
= tdb_store(db
->wtdb
->tdb
, key
, rec
, TDB_REPLACE
);
158 talloc_free(rec
.dptr
);
160 return (ret
== 0) ? NT_STATUS_OK
161 : tdb_error_to_ntstatus(db
->wtdb
->tdb
);
166 form a ctdb_rec_data record from a key/data pair
168 static struct ctdb_rec_data
*db_ctdb_marshall_record(TALLOC_CTX
*mem_ctx
, uint32_t reqid
,
170 struct ctdb_ltdb_header
*header
,
174 struct ctdb_rec_data
*d
;
176 length
= offsetof(struct ctdb_rec_data
, data
) + key
.dsize
+
177 data
.dsize
+ sizeof(*header
);
178 d
= (struct ctdb_rec_data
*)talloc_size(mem_ctx
, length
);
184 d
->keylen
= key
.dsize
;
185 memcpy(&d
->data
[0], key
.dptr
, key
.dsize
);
187 d
->datalen
= data
.dsize
+ sizeof(*header
);
188 memcpy(&d
->data
[key
.dsize
], header
, sizeof(*header
));
189 memcpy(&d
->data
[key
.dsize
+sizeof(*header
)], data
.dptr
, data
.dsize
);
194 /* helper function for marshalling multiple records */
195 static struct ctdb_marshall_buffer
*db_ctdb_marshall_add(TALLOC_CTX
*mem_ctx
,
196 struct ctdb_marshall_buffer
*m
,
200 struct ctdb_ltdb_header
*header
,
203 struct ctdb_rec_data
*r
;
204 size_t m_size
, r_size
;
205 struct ctdb_marshall_buffer
*m2
= NULL
;
207 r
= db_ctdb_marshall_record(talloc_tos(), reqid
, key
, header
, data
);
214 m
= (struct ctdb_marshall_buffer
*)talloc_zero_size(
215 mem_ctx
, offsetof(struct ctdb_marshall_buffer
, data
));
222 m_size
= talloc_get_size(m
);
223 r_size
= talloc_get_size(r
);
225 m2
= (struct ctdb_marshall_buffer
*)talloc_realloc_size(
226 mem_ctx
, m
, m_size
+ r_size
);
232 memcpy(m_size
+ (uint8_t *)m2
, r
, r_size
);
241 /* we've finished marshalling, return a data blob with the marshalled records */
242 static TDB_DATA
db_ctdb_marshall_finish(struct ctdb_marshall_buffer
*m
)
245 data
.dptr
= (uint8_t *)m
;
246 data
.dsize
= talloc_get_size(m
);
251 loop over a marshalling buffer
253 - pass r==NULL to start
254 - loop the number of times indicated by m->count
256 static struct ctdb_rec_data
*db_ctdb_marshall_loop_next_key(
257 struct ctdb_marshall_buffer
*m
, struct ctdb_rec_data
*r
, TDB_DATA
*key
)
260 r
= (struct ctdb_rec_data
*)&m
->data
[0];
262 r
= (struct ctdb_rec_data
*)(r
->length
+ (uint8_t *)r
);
265 key
->dptr
= &r
->data
[0];
266 key
->dsize
= r
->keylen
;
270 static bool db_ctdb_marshall_buf_parse(
271 struct ctdb_rec_data
*r
, uint32_t *reqid
,
272 struct ctdb_ltdb_header
**header
, TDB_DATA
*data
)
274 if (r
->datalen
< sizeof(struct ctdb_ltdb_header
)) {
280 data
->dptr
= &r
->data
[r
->keylen
] + sizeof(struct ctdb_ltdb_header
);
281 data
->dsize
= r
->datalen
- sizeof(struct ctdb_ltdb_header
);
283 *header
= (struct ctdb_ltdb_header
*)&r
->data
[r
->keylen
];
289 * CTDB transaction destructor
291 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle
*h
)
295 status
= g_lock_unlock(h
->ctx
->lock_ctx
, h
->lock_name
);
296 if (!NT_STATUS_IS_OK(status
)) {
297 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h
->lock_name
,
305 * CTDB dbwrap API: transaction_start function
306 * starts a transaction on a persistent database
308 static int db_ctdb_transaction_start(struct db_context
*db
)
310 struct db_ctdb_transaction_handle
*h
;
312 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
315 if (!db
->persistent
) {
316 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
321 if (ctx
->transaction
) {
322 ctx
->transaction
->nesting
++;
323 DEBUG(5, (__location__
" transaction start on db 0x%08x: nesting %d -> %d\n",
324 ctx
->db_id
, ctx
->transaction
->nesting
- 1, ctx
->transaction
->nesting
));
328 h
= talloc_zero(db
, struct db_ctdb_transaction_handle
);
330 DEBUG(0,(__location__
" oom for transaction handle\n"));
336 h
->lock_name
= talloc_asprintf(h
, "transaction_db_0x%08x",
337 (unsigned int)ctx
->db_id
);
338 if (h
->lock_name
== NULL
) {
339 DEBUG(0, ("talloc_asprintf failed\n"));
345 * Wait a day, i.e. forever...
347 status
= g_lock_lock(ctx
->lock_ctx
, h
->lock_name
, G_LOCK_WRITE
,
348 timeval_set(86400, 0));
349 if (!NT_STATUS_IS_OK(status
)) {
350 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status
)));
355 talloc_set_destructor(h
, db_ctdb_transaction_destructor
);
357 ctx
->transaction
= h
;
359 DEBUG(5,(__location__
" transaction started on db 0x%08x\n", ctx
->db_id
));
364 static bool parse_newest_in_marshall_buffer(
365 struct ctdb_marshall_buffer
*buf
, TDB_DATA key
,
366 void (*parser
)(TDB_DATA key
, struct ctdb_ltdb_header
*header
,
367 TDB_DATA data
, void *private_data
),
370 struct ctdb_rec_data
*rec
= NULL
;
371 struct ctdb_ltdb_header
*h
= NULL
;
380 * Walk the list of records written during this
381 * transaction. If we want to read one we have already
382 * written, return the last written sample. Thus we do not do
383 * a "break;" for the first hit, this record might have been
387 for (i
=0; i
<buf
->count
; i
++) {
391 rec
= db_ctdb_marshall_loop_next_key(buf
, rec
, &tkey
);
396 if (!tdb_data_equal(key
, tkey
)) {
400 if (!db_ctdb_marshall_buf_parse(rec
, &reqid
, &h
, &data
)) {
409 parser(key
, h
, data
, private_data
);
414 struct pull_newest_from_marshall_buffer_state
{
415 struct ctdb_ltdb_header
*pheader
;
420 static void pull_newest_from_marshall_buffer_parser(
421 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
422 TDB_DATA data
, void *private_data
)
424 struct pull_newest_from_marshall_buffer_state
*state
=
425 (struct pull_newest_from_marshall_buffer_state
*)private_data
;
427 if (state
->pheader
!= NULL
) {
428 memcpy(state
->pheader
, header
, sizeof(*state
->pheader
));
430 if (state
->pdata
!= NULL
) {
431 state
->pdata
->dsize
= data
.dsize
;
432 state
->pdata
->dptr
= (uint8_t *)talloc_memdup(
433 state
->mem_ctx
, data
.dptr
, data
.dsize
);
437 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer
*buf
,
439 struct ctdb_ltdb_header
*pheader
,
443 struct pull_newest_from_marshall_buffer_state state
;
445 state
.pheader
= pheader
;
446 state
.mem_ctx
= mem_ctx
;
449 if (!parse_newest_in_marshall_buffer(
450 buf
, key
, pull_newest_from_marshall_buffer_parser
,
454 if ((pdata
!= NULL
) && (pdata
->dsize
!= 0) && (pdata
->dptr
== NULL
)) {
461 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
);
462 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
);
464 static struct db_record
*db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx
*ctx
,
468 struct db_record
*result
;
471 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
472 DEBUG(0, ("talloc failed\n"));
476 result
->db
= ctx
->db
;
477 result
->private_data
= ctx
->transaction
;
479 result
->key
.dsize
= key
.dsize
;
480 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
482 if (result
->key
.dptr
== NULL
) {
483 DEBUG(0, ("talloc failed\n"));
488 result
->store
= db_ctdb_store_transaction
;
489 result
->delete_rec
= db_ctdb_delete_transaction
;
491 if (pull_newest_from_marshall_buffer(ctx
->transaction
->m_write
, key
,
492 NULL
, result
, &result
->value
)) {
496 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
497 if (ctdb_data
.dptr
== NULL
) {
498 /* create the record */
499 result
->value
= tdb_null
;
503 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
504 result
->value
.dptr
= NULL
;
506 if ((result
->value
.dsize
!= 0)
507 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
508 result
, ctdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
509 result
->value
.dsize
))) {
510 DEBUG(0, ("talloc failed\n"));
514 SAFE_FREE(ctdb_data
.dptr
);
519 static int db_ctdb_record_destructor(struct db_record
**recp
)
521 struct db_record
*rec
= talloc_get_type_abort(*recp
, struct db_record
);
522 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
523 rec
->private_data
, struct db_ctdb_transaction_handle
);
524 int ret
= h
->ctx
->db
->transaction_commit(h
->ctx
->db
);
526 DEBUG(0,(__location__
" transaction_commit failed\n"));
532 auto-create a transaction for persistent databases
534 static struct db_record
*db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx
*ctx
,
539 struct db_record
*rec
, **recp
;
541 res
= db_ctdb_transaction_start(ctx
->db
);
546 rec
= db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
548 ctx
->db
->transaction_cancel(ctx
->db
);
552 /* destroy this transaction when we release the lock */
553 recp
= talloc(rec
, struct db_record
*);
555 ctx
->db
->transaction_cancel(ctx
->db
);
560 talloc_set_destructor(recp
, db_ctdb_record_destructor
);
566 stores a record inside a transaction
568 static NTSTATUS
db_ctdb_transaction_store(struct db_ctdb_transaction_handle
*h
,
569 TDB_DATA key
, TDB_DATA data
)
571 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
573 struct ctdb_ltdb_header header
;
577 /* we need the header so we can update the RSN */
579 if (!pull_newest_from_marshall_buffer(h
->m_write
, key
, &header
,
582 rec
= tdb_fetch_compat(h
->ctx
->wtdb
->tdb
, key
);
584 if (rec
.dptr
!= NULL
) {
585 memcpy(&header
, rec
.dptr
,
586 sizeof(struct ctdb_ltdb_header
));
587 rec
.dsize
-= sizeof(struct ctdb_ltdb_header
);
590 * a special case, we are writing the same
591 * data that is there now
593 if (data
.dsize
== rec
.dsize
&&
595 rec
.dptr
+ sizeof(struct ctdb_ltdb_header
),
598 talloc_free(tmp_ctx
);
605 header
.dmaster
= get_my_vnn();
608 h
->m_write
= db_ctdb_marshall_add(h
, h
->m_write
, h
->ctx
->db_id
, 0, key
, &header
, data
);
609 if (h
->m_write
== NULL
) {
610 DEBUG(0,(__location__
" Failed to add to marshalling record\n"));
611 talloc_free(tmp_ctx
);
612 return NT_STATUS_NO_MEMORY
;
615 talloc_free(tmp_ctx
);
621 a record store inside a transaction
623 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
)
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
, data
);
634 a record delete inside a transaction
636 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
)
638 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
639 rec
->private_data
, struct db_ctdb_transaction_handle
);
642 status
= db_ctdb_transaction_store(h
, rec
->key
, tdb_null
);
646 static void db_ctdb_fetch_db_seqnum_parser(
647 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
648 TDB_DATA data
, void *private_data
)
650 uint64_t *seqnum
= (uint64_t *)private_data
;
652 if (data
.dsize
!= sizeof(uint64_t)) {
656 memcpy(seqnum
, data
.dptr
, sizeof(*seqnum
));
660 * Fetch the db sequence number of a persistent db directly from the db.
662 static NTSTATUS
db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx
*db
,
668 if (seqnum
== NULL
) {
669 return NT_STATUS_INVALID_PARAMETER
;
672 key
= string_term_tdb_data(CTDB_DB_SEQNUM_KEY
);
674 status
= db_ctdb_ltdb_parse(
675 db
, key
, db_ctdb_fetch_db_seqnum_parser
, seqnum
);
677 if (NT_STATUS_IS_OK(status
)) {
680 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
688 * Store the database sequence number inside a transaction.
690 static NTSTATUS
db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle
*h
,
694 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
698 key
= string_term_tdb_data(keyname
);
700 data
.dptr
= (uint8_t *)&seqnum
;
701 data
.dsize
= sizeof(uint64_t);
703 status
= db_ctdb_transaction_store(h
, key
, data
);
711 static int db_ctdb_transaction_commit(struct db_context
*db
)
713 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
717 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
718 uint64_t old_seqnum
, new_seqnum
;
722 DEBUG(0,(__location__
" transaction commit with no open transaction on db 0x%08x\n", ctx
->db_id
));
726 if (h
->nested_cancel
) {
727 db
->transaction_cancel(db
);
728 DEBUG(5,(__location__
" Failed transaction commit after nested cancel\n"));
732 if (h
->nesting
!= 0) {
734 DEBUG(5, (__location__
" transaction commit on db 0x%08x: nesting %d -> %d\n",
735 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
739 if (h
->m_write
== NULL
) {
741 * No changes were made, so don't change the seqnum,
742 * don't push to other node, just exit with success.
748 DEBUG(5,(__location__
" transaction commit on db 0x%08x\n", ctx
->db_id
));
751 * As the last db action before committing, bump the database sequence
752 * number. Note that this undoes all changes to the seqnum records
753 * performed under the transaction. This record is not meant to be
754 * modified by user interaction. It is for internal use only...
756 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &old_seqnum
);
757 if (!NT_STATUS_IS_OK(rets
)) {
758 DEBUG(1, (__location__
" failed to fetch the db sequence number "
759 "in transaction commit on db 0x%08x\n", ctx
->db_id
));
764 new_seqnum
= old_seqnum
+ 1;
766 rets
= db_ctdb_store_db_seqnum(h
, new_seqnum
);
767 if (!NT_STATUS_IS_OK(rets
)) {
768 DEBUG(1, (__location__
"failed to store the db sequence number "
769 " in transaction commit on db 0x%08x\n", ctx
->db_id
));
775 /* tell ctdbd to commit to the other nodes */
776 rets
= ctdbd_control_local(messaging_ctdbd_connection(),
777 CTDB_CONTROL_TRANS3_COMMIT
,
779 db_ctdb_marshall_finish(h
->m_write
),
780 NULL
, NULL
, &status
);
781 if (!NT_STATUS_IS_OK(rets
) || status
!= 0) {
783 * The TRANS3_COMMIT control should only possibly fail when a
784 * recovery has been running concurrently. In any case, the db
785 * will be the same on all nodes, either the new copy or the
786 * old copy. This can be detected by comparing the old and new
787 * local sequence numbers.
789 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &new_seqnum
);
790 if (!NT_STATUS_IS_OK(rets
)) {
791 DEBUG(1, (__location__
" failed to refetch db sequence "
792 "number after failed TRANS3_COMMIT\n"));
797 if (new_seqnum
== old_seqnum
) {
798 /* Recovery prevented all our changes: retry. */
801 if (new_seqnum
!= (old_seqnum
+ 1)) {
802 DEBUG(0, (__location__
" ERROR: new_seqnum[%lu] != "
803 "old_seqnum[%lu] + (0 or 1) after failed "
804 "TRANS3_COMMIT - this should not happen!\n",
805 (unsigned long)new_seqnum
,
806 (unsigned long)old_seqnum
));
811 * Recovery propagated our changes to all nodes, completing
812 * our commit for us - succeed.
819 h
->ctx
->transaction
= NULL
;
828 static int db_ctdb_transaction_cancel(struct db_context
*db
)
830 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
832 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
835 DEBUG(0,(__location__
" transaction cancel with no open transaction on db 0x%08x\n", ctx
->db_id
));
839 if (h
->nesting
!= 0) {
841 h
->nested_cancel
= true;
842 DEBUG(5, (__location__
" transaction cancel on db 0x%08x: nesting %d -> %d\n",
843 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
847 DEBUG(5,(__location__
" Cancel transaction on db 0x%08x\n", ctx
->db_id
));
849 ctx
->transaction
= NULL
;
855 static NTSTATUS
db_ctdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
)
857 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
858 rec
->private_data
, struct db_ctdb_rec
);
860 return db_ctdb_ltdb_store(crec
->ctdb_ctx
, rec
->key
, &(crec
->header
), data
);
865 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
866 static NTSTATUS
db_ctdb_send_schedule_for_deletion(struct db_record
*rec
)
869 struct ctdb_control_schedule_for_deletion
*dd
;
872 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
873 rec
->private_data
, struct db_ctdb_rec
);
875 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + rec
->key
.dsize
;
876 indata
.dptr
= talloc_zero_array(crec
, uint8_t, indata
.dsize
);
877 if (indata
.dptr
== NULL
) {
878 DEBUG(0, (__location__
" talloc failed!\n"));
879 return NT_STATUS_NO_MEMORY
;
882 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
883 dd
->db_id
= crec
->ctdb_ctx
->db_id
;
884 dd
->hdr
= crec
->header
;
885 dd
->keylen
= rec
->key
.dsize
;
886 memcpy(dd
->key
, rec
->key
.dptr
, rec
->key
.dsize
);
888 status
= ctdbd_control_local(messaging_ctdbd_connection(),
889 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
890 crec
->ctdb_ctx
->db_id
,
891 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
896 talloc_free(indata
.dptr
);
898 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
899 DEBUG(1, (__location__
" Error sending local control "
900 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
901 nt_errstr(status
), cstatus
));
902 if (NT_STATUS_IS_OK(status
)) {
903 status
= NT_STATUS_UNSUCCESSFUL
;
911 static NTSTATUS
db_ctdb_delete(struct db_record
*rec
)
916 * We have to store the header with empty data. TODO: Fix the
920 status
= db_ctdb_store(rec
, tdb_null
, 0);
921 if (!NT_STATUS_IS_OK(status
)) {
925 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
926 status
= db_ctdb_send_schedule_for_deletion(rec
);
932 static int db_ctdb_record_destr(struct db_record
* data
)
934 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
935 data
->private_data
, struct db_ctdb_rec
);
938 DEBUG(10, (DEBUGLEVEL
> 10
939 ? "Unlocking db %u key %s\n"
940 : "Unlocking db %u key %.20s\n",
941 (int)crec
->ctdb_ctx
->db_id
,
942 hex_encode_talloc(data
, (unsigned char *)data
->key
.dptr
,
945 tdb_chainunlock(crec
->ctdb_ctx
->wtdb
->tdb
, data
->key
);
947 threshold
= lp_ctdb_locktime_warn_threshold();
948 if (threshold
!= 0) {
949 double timediff
= timeval_elapsed(&crec
->lock_time
);
950 if ((timediff
* 1000) > threshold
) {
953 key
= hex_encode_talloc(data
,
954 (unsigned char *)data
->key
.dptr
,
956 DEBUG(0, ("Held tdb lock on db %s, key %s %f seconds\n",
957 tdb_name(crec
->ctdb_ctx
->wtdb
->tdb
), key
,
966 * Check whether we have a valid local copy of the given record,
967 * either for reading or for writing.
969 static bool db_ctdb_can_use_local_hdr(const struct ctdb_ltdb_header
*hdr
,
972 #ifdef HAVE_CTDB_WANT_READONLY_DECL
973 if (hdr
->dmaster
!= get_my_vnn()) {
974 /* If we're not dmaster, it must be r/o copy. */
975 return read_only
&& (hdr
->flags
& CTDB_REC_RO_HAVE_READONLY
);
979 * If we want write access, no one may have r/o copies.
981 return read_only
|| !(hdr
->flags
& CTDB_REC_RO_HAVE_DELEGATIONS
);
983 return (hdr
->dmaster
== get_my_vnn());
987 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data
, bool read_only
)
989 if (ctdb_data
.dptr
== NULL
)
992 if (ctdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
))
995 return db_ctdb_can_use_local_hdr(
996 (struct ctdb_ltdb_header
*)ctdb_data
.dptr
, read_only
);
999 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
1000 TALLOC_CTX
*mem_ctx
,
1004 struct db_record
*result
;
1005 struct db_ctdb_rec
*crec
;
1008 int migrate_attempts
= 0;
1011 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
1012 DEBUG(0, ("talloc failed\n"));
1016 if (!(crec
= talloc_zero(result
, struct db_ctdb_rec
))) {
1017 DEBUG(0, ("talloc failed\n"));
1018 TALLOC_FREE(result
);
1022 result
->db
= ctx
->db
;
1023 result
->private_data
= (void *)crec
;
1024 crec
->ctdb_ctx
= ctx
;
1026 result
->key
.dsize
= key
.dsize
;
1027 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
1029 if (result
->key
.dptr
== NULL
) {
1030 DEBUG(0, ("talloc failed\n"));
1031 TALLOC_FREE(result
);
1036 * Do a blocking lock on the record
1040 if (DEBUGLEVEL
>= 10) {
1041 char *keystr
= hex_encode_talloc(result
, key
.dptr
, key
.dsize
);
1042 DEBUG(10, (DEBUGLEVEL
> 10
1043 ? "Locking db %u key %s\n"
1044 : "Locking db %u key %.20s\n",
1045 (int)crec
->ctdb_ctx
->db_id
, keystr
));
1046 TALLOC_FREE(keystr
);
1050 ? tdb_chainlock_nonblock(ctx
->wtdb
->tdb
, key
)
1051 : tdb_chainlock(ctx
->wtdb
->tdb
, key
);
1053 DEBUG(3, ("tdb_chainlock failed\n"));
1054 TALLOC_FREE(result
);
1058 result
->store
= db_ctdb_store
;
1059 result
->delete_rec
= db_ctdb_delete
;
1060 talloc_set_destructor(result
, db_ctdb_record_destr
);
1062 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
1065 * See if we have a valid record and we are the dmaster. If so, we can
1066 * take the shortcut and just return it.
1069 if (!db_ctdb_can_use_local_copy(ctdb_data
, false)) {
1070 SAFE_FREE(ctdb_data
.dptr
);
1071 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
1072 talloc_set_destructor(result
, NULL
);
1074 if (tryonly
&& (migrate_attempts
!= 0)) {
1075 DEBUG(5, ("record migrated away again\n"));
1076 TALLOC_FREE(result
);
1080 migrate_attempts
+= 1;
1082 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1083 ctdb_data
.dptr
, ctdb_data
.dptr
?
1084 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
: -1,
1087 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->flags
: 0));
1089 status
= ctdbd_migrate(messaging_ctdbd_connection(), ctx
->db_id
,
1091 if (!NT_STATUS_IS_OK(status
)) {
1092 DEBUG(5, ("ctdb_migrate failed: %s\n",
1093 nt_errstr(status
)));
1094 TALLOC_FREE(result
);
1097 /* now its migrated, try again */
1101 if (migrate_attempts
> 10) {
1102 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s needed %d "
1103 "attempts\n", tdb_name(ctx
->wtdb
->tdb
),
1104 hex_encode_talloc(talloc_tos(),
1105 (unsigned char *)key
.dptr
,
1110 GetTimeOfDay(&crec
->lock_time
);
1112 memcpy(&crec
->header
, ctdb_data
.dptr
, sizeof(crec
->header
));
1114 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(crec
->header
);
1115 result
->value
.dptr
= NULL
;
1117 if ((result
->value
.dsize
!= 0)
1118 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
1119 result
, ctdb_data
.dptr
+ sizeof(crec
->header
),
1120 result
->value
.dsize
))) {
1121 DEBUG(0, ("talloc failed\n"));
1122 TALLOC_FREE(result
);
1125 SAFE_FREE(ctdb_data
.dptr
);
1130 static struct db_record
*db_ctdb_fetch_locked(struct db_context
*db
,
1131 TALLOC_CTX
*mem_ctx
,
1134 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1135 struct db_ctdb_ctx
);
1137 if (ctx
->transaction
!= NULL
) {
1138 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1141 if (db
->persistent
) {
1142 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1145 return fetch_locked_internal(ctx
, mem_ctx
, key
, false);
1148 static struct db_record
*db_ctdb_try_fetch_locked(struct db_context
*db
,
1149 TALLOC_CTX
*mem_ctx
,
1152 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1153 struct db_ctdb_ctx
);
1155 if (ctx
->transaction
!= NULL
) {
1156 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1159 if (db
->persistent
) {
1160 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1163 return fetch_locked_internal(ctx
, mem_ctx
, key
, true);
1166 struct db_ctdb_parse_record_state
{
1167 void (*parser
)(TDB_DATA key
, TDB_DATA data
, void *private_data
);
1169 bool ask_for_readonly_copy
;
1173 static void db_ctdb_parse_record_parser(
1174 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1175 TDB_DATA data
, void *private_data
)
1177 struct db_ctdb_parse_record_state
*state
=
1178 (struct db_ctdb_parse_record_state
*)private_data
;
1179 state
->parser(key
, data
, state
->private_data
);
1182 static void db_ctdb_parse_record_parser_nonpersistent(
1183 TDB_DATA key
, struct ctdb_ltdb_header
*header
,
1184 TDB_DATA data
, void *private_data
)
1186 struct db_ctdb_parse_record_state
*state
=
1187 (struct db_ctdb_parse_record_state
*)private_data
;
1189 if (db_ctdb_can_use_local_hdr(header
, true)) {
1190 state
->parser(key
, data
, state
->private_data
);
1194 * We found something in the db, so it seems that this record,
1195 * while not usable locally right now, is popular. Ask for a
1198 state
->ask_for_readonly_copy
= true;
1202 static NTSTATUS
db_ctdb_parse_record(struct db_context
*db
, TDB_DATA key
,
1203 void (*parser
)(TDB_DATA key
,
1205 void *private_data
),
1208 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1209 db
->private_data
, struct db_ctdb_ctx
);
1210 struct db_ctdb_parse_record_state state
;
1213 state
.parser
= parser
;
1214 state
.private_data
= private_data
;
1216 if (ctx
->transaction
!= NULL
) {
1217 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
1221 * Transactions only happen for persistent db's.
1224 found
= parse_newest_in_marshall_buffer(
1225 h
->m_write
, key
, db_ctdb_parse_record_parser
, &state
);
1228 return NT_STATUS_OK
;
1232 if (db
->persistent
) {
1234 * Persistent db, but not found in the transaction buffer
1236 return db_ctdb_ltdb_parse(
1237 ctx
, key
, db_ctdb_parse_record_parser
, &state
);
1241 state
.ask_for_readonly_copy
= false;
1243 status
= db_ctdb_ltdb_parse(
1244 ctx
, key
, db_ctdb_parse_record_parser_nonpersistent
, &state
);
1245 if (NT_STATUS_IS_OK(status
) && state
.done
) {
1246 return NT_STATUS_OK
;
1249 return ctdbd_parse(messaging_ctdbd_connection(), ctx
->db_id
, key
,
1250 state
.ask_for_readonly_copy
, parser
, private_data
);
1253 struct traverse_state
{
1254 struct db_context
*db
;
1255 int (*fn
)(struct db_record
*rec
, void *private_data
);
1260 static void traverse_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1262 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1263 struct db_record
*rec
;
1264 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1265 /* we have to give them a locked record to prevent races */
1266 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, key
);
1267 if (rec
&& rec
->value
.dsize
> 0) {
1268 state
->fn(rec
, state
->private_data
);
1270 talloc_free(tmp_ctx
);
1273 static int traverse_persistent_callback(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1276 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1277 struct db_record
*rec
;
1278 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1282 * Skip the __db_sequence_number__ key:
1283 * This is used for persistent transactions internally.
1285 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1286 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1291 /* we have to give them a locked record to prevent races */
1292 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, kbuf
);
1293 if (rec
&& rec
->value
.dsize
> 0) {
1294 ret
= state
->fn(rec
, state
->private_data
);
1298 talloc_free(tmp_ctx
);
1302 /* wrapper to use traverse_persistent_callback with dbwrap */
1303 static int traverse_persistent_callback_dbwrap(struct db_record
*rec
, void* data
)
1305 return traverse_persistent_callback(NULL
, rec
->key
, rec
->value
, data
);
1309 static int db_ctdb_traverse(struct db_context
*db
,
1310 int (*fn
)(struct db_record
*rec
,
1311 void *private_data
),
1315 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1316 struct db_ctdb_ctx
);
1317 struct traverse_state state
;
1321 state
.private_data
= private_data
;
1324 if (db
->persistent
) {
1325 struct tdb_context
*ltdb
= ctx
->wtdb
->tdb
;
1328 /* for persistent databases we don't need to do a ctdb traverse,
1329 we can do a faster local traverse */
1330 ret
= tdb_traverse(ltdb
, traverse_persistent_callback
, &state
);
1334 if (ctx
->transaction
&& ctx
->transaction
->m_write
) {
1336 * we now have to handle keys not yet
1337 * present at transaction start
1339 struct db_context
*newkeys
= db_open_rbt(talloc_tos());
1340 struct ctdb_marshall_buffer
*mbuf
= ctx
->transaction
->m_write
;
1341 struct ctdb_rec_data
*rec
=NULL
;
1345 if (newkeys
== NULL
) {
1349 for (i
=0; i
<mbuf
->count
; i
++) {
1351 rec
= db_ctdb_marshall_loop_next_key(
1353 SMB_ASSERT(rec
!= NULL
);
1355 if (!tdb_exists(ltdb
, key
)) {
1356 dbwrap_store(newkeys
, key
, tdb_null
, 0);
1359 status
= dbwrap_traverse(newkeys
,
1360 traverse_persistent_callback_dbwrap
,
1363 talloc_free(newkeys
);
1364 if (!NT_STATUS_IS_OK(status
)) {
1372 status
= ctdbd_traverse(ctx
->db_id
, traverse_callback
, &state
);
1373 if (!NT_STATUS_IS_OK(status
)) {
1379 static NTSTATUS
db_ctdb_store_deny(struct db_record
*rec
, TDB_DATA data
, int flag
)
1381 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1384 static NTSTATUS
db_ctdb_delete_deny(struct db_record
*rec
)
1386 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1389 static void traverse_read_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1391 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1392 struct db_record rec
;
1398 rec
.store
= db_ctdb_store_deny
;
1399 rec
.delete_rec
= db_ctdb_delete_deny
;
1400 rec
.private_data
= NULL
;
1401 state
->fn(&rec
, state
->private_data
);
1405 static int traverse_persistent_callback_read(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1408 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1409 struct db_record rec
;
1412 * Skip the __db_sequence_number__ key:
1413 * This is used for persistent transactions internally.
1415 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1416 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1425 rec
.store
= db_ctdb_store_deny
;
1426 rec
.delete_rec
= db_ctdb_delete_deny
;
1427 rec
.private_data
= NULL
;
1429 if (rec
.value
.dsize
<= sizeof(struct ctdb_ltdb_header
)) {
1430 /* a deleted record */
1433 rec
.value
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1434 rec
.value
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1437 return state
->fn(&rec
, state
->private_data
);
1440 static int db_ctdb_traverse_read(struct db_context
*db
,
1441 int (*fn
)(struct db_record
*rec
,
1442 void *private_data
),
1446 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1447 struct db_ctdb_ctx
);
1448 struct traverse_state state
;
1452 state
.private_data
= private_data
;
1455 if (db
->persistent
) {
1456 /* for persistent databases we don't need to do a ctdb traverse,
1457 we can do a faster local traverse */
1458 return tdb_traverse_read(ctx
->wtdb
->tdb
, traverse_persistent_callback_read
, &state
);
1461 status
= ctdbd_traverse(ctx
->db_id
, traverse_read_callback
, &state
);
1462 if (!NT_STATUS_IS_OK(status
)) {
1468 static int db_ctdb_get_seqnum(struct db_context
*db
)
1470 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1471 struct db_ctdb_ctx
);
1472 return tdb_get_seqnum(ctx
->wtdb
->tdb
);
1475 static void db_ctdb_id(struct db_context
*db
, const uint8_t **id
,
1478 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1479 db
->private_data
, struct db_ctdb_ctx
);
1481 *id
= (uint8_t *)&ctx
->db_id
;
1482 *idlen
= sizeof(ctx
->db_id
);
1485 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1487 int hash_size
, int tdb_flags
,
1488 int open_flags
, mode_t mode
,
1489 enum dbwrap_lock_order lock_order
)
1491 struct db_context
*result
;
1492 struct db_ctdb_ctx
*db_ctdb
;
1494 struct ctdbd_connection
*conn
;
1495 struct loadparm_context
*lp_ctx
;
1496 struct ctdb_db_priority prio
;
1500 if (!lp_clustering()) {
1501 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1505 if (!(result
= talloc_zero(mem_ctx
, struct db_context
))) {
1506 DEBUG(0, ("talloc failed\n"));
1507 TALLOC_FREE(result
);
1511 if (!(db_ctdb
= talloc(result
, struct db_ctdb_ctx
))) {
1512 DEBUG(0, ("talloc failed\n"));
1513 TALLOC_FREE(result
);
1517 result
->name
= talloc_strdup(result
, name
);
1518 if (result
->name
== NULL
) {
1519 DEBUG(0, ("talloc failed\n"));
1520 TALLOC_FREE(result
);
1524 db_ctdb
->transaction
= NULL
;
1525 db_ctdb
->db
= result
;
1527 conn
= messaging_ctdbd_connection();
1529 DEBUG(1, ("Could not connect to ctdb\n"));
1530 TALLOC_FREE(result
);
1534 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn
, name
, &db_ctdb
->db_id
, tdb_flags
))) {
1535 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name
));
1536 TALLOC_FREE(result
);
1540 db_path
= ctdbd_dbpath(conn
, db_ctdb
, db_ctdb
->db_id
);
1542 result
->persistent
= ((tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0);
1543 result
->lock_order
= lock_order
;
1545 /* only pass through specific flags */
1546 tdb_flags
&= TDB_SEQNUM
;
1548 /* honor permissions if user has specified O_CREAT */
1549 if (open_flags
& O_CREAT
) {
1550 chmod(db_path
, mode
);
1553 prio
.db_id
= db_ctdb
->db_id
;
1554 prio
.priority
= lock_order
;
1556 status
= ctdbd_control_local(
1557 conn
, CTDB_CONTROL_SET_DB_PRIORITY
, 0, 0,
1558 make_tdb_data((uint8_t *)&prio
, sizeof(prio
)),
1559 NULL
, NULL
, &cstatus
);
1561 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1562 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1563 nt_errstr(status
), cstatus
));
1564 TALLOC_FREE(result
);
1568 lp_ctx
= loadparm_init_s3(db_path
, loadparm_s3_helpers());
1570 db_ctdb
->wtdb
= tdb_wrap_open(db_ctdb
, db_path
, hash_size
, tdb_flags
,
1572 talloc_unlink(db_path
, lp_ctx
);
1573 if (db_ctdb
->wtdb
== NULL
) {
1574 DEBUG(0, ("Could not open tdb %s: %s\n", db_path
, strerror(errno
)));
1575 TALLOC_FREE(result
);
1578 talloc_free(db_path
);
1580 if (result
->persistent
) {
1581 db_ctdb
->lock_ctx
= g_lock_ctx_init(db_ctdb
,
1582 ctdb_conn_msg_ctx(conn
));
1583 if (db_ctdb
->lock_ctx
== NULL
) {
1584 DEBUG(0, ("g_lock_ctx_init failed\n"));
1585 TALLOC_FREE(result
);
1590 result
->private_data
= (void *)db_ctdb
;
1591 result
->fetch_locked
= db_ctdb_fetch_locked
;
1592 result
->try_fetch_locked
= db_ctdb_try_fetch_locked
;
1593 result
->parse_record
= db_ctdb_parse_record
;
1594 result
->traverse
= db_ctdb_traverse
;
1595 result
->traverse_read
= db_ctdb_traverse_read
;
1596 result
->get_seqnum
= db_ctdb_get_seqnum
;
1597 result
->transaction_start
= db_ctdb_transaction_start
;
1598 result
->transaction_commit
= db_ctdb_transaction_commit
;
1599 result
->transaction_cancel
= db_ctdb_transaction_cancel
;
1600 result
->id
= db_ctdb_id
;
1601 result
->stored_callback
= NULL
;
1603 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1604 name
, db_ctdb
->db_id
));
1609 #else /* CLUSTER_SUPPORT */
1611 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1613 int hash_size
, int tdb_flags
,
1614 int open_flags
, mode_t mode
,
1615 enum dbwrap_lock_order lock_order
)
1617 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));