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 #ifdef CLUSTER_SUPPORT
24 #include "ctdb_private.h"
25 #include "ctdbd_conn.h"
28 struct db_ctdb_transaction_handle
{
29 struct db_ctdb_ctx
*ctx
;
31 * we store the reads and writes done under a transaction:
32 * - one list stores both reads and writes (m_all),
33 * - the other just writes (m_write)
35 struct ctdb_marshall_buffer
*m_all
;
36 struct ctdb_marshall_buffer
*m_write
;
43 struct db_context
*db
;
44 struct tdb_wrap
*wtdb
;
46 struct db_ctdb_transaction_handle
*transaction
;
47 struct g_lock_ctx
*lock_ctx
;
51 struct db_ctdb_ctx
*ctdb_ctx
;
52 struct ctdb_ltdb_header header
;
55 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
60 static NTSTATUS
tdb_error_to_ntstatus(struct tdb_context
*tdb
)
63 enum TDB_ERROR tret
= tdb_error(tdb
);
67 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
70 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
73 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
82 * fetch a record from the tdb, separating out the header
83 * information and returning the body of the record.
85 static NTSTATUS
db_ctdb_ltdb_fetch(struct db_ctdb_ctx
*db
,
87 struct ctdb_ltdb_header
*header
,
94 rec
= tdb_fetch(db
->wtdb
->tdb
, key
);
95 if (rec
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
96 status
= NT_STATUS_NOT_FOUND
;
101 header
->dmaster
= (uint32_t)-1;
108 *header
= *(struct ctdb_ltdb_header
*)rec
.dptr
;
112 data
->dsize
= rec
.dsize
- sizeof(struct ctdb_ltdb_header
);
113 if (data
->dsize
== 0) {
116 data
->dptr
= (unsigned char *)talloc_memdup(mem_ctx
,
118 + sizeof(struct ctdb_ltdb_header
),
120 if (data
->dptr
== NULL
) {
121 status
= NT_STATUS_NO_MEMORY
;
127 status
= NT_STATUS_OK
;
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
,
143 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
147 rec
.dsize
= data
.dsize
+ sizeof(struct ctdb_ltdb_header
);
148 rec
.dptr
= (uint8_t *)talloc_size(tmp_ctx
, rec
.dsize
);
150 if (rec
.dptr
== NULL
) {
151 talloc_free(tmp_ctx
);
152 return NT_STATUS_NO_MEMORY
;
155 memcpy(rec
.dptr
, header
, sizeof(struct ctdb_ltdb_header
));
156 memcpy(sizeof(struct ctdb_ltdb_header
) + (uint8_t *)rec
.dptr
, data
.dptr
, data
.dsize
);
158 ret
= tdb_store(db
->wtdb
->tdb
, key
, rec
, TDB_REPLACE
);
160 talloc_free(tmp_ctx
);
162 return (ret
== 0) ? NT_STATUS_OK
163 : tdb_error_to_ntstatus(db
->wtdb
->tdb
);
168 form a ctdb_rec_data record from a key/data pair
170 note that header may be NULL. If not NULL then it is included in the data portion
173 static struct ctdb_rec_data
*db_ctdb_marshall_record(TALLOC_CTX
*mem_ctx
, uint32_t reqid
,
175 struct ctdb_ltdb_header
*header
,
179 struct ctdb_rec_data
*d
;
181 length
= offsetof(struct ctdb_rec_data
, data
) + key
.dsize
+
182 data
.dsize
+ (header
?sizeof(*header
):0);
183 d
= (struct ctdb_rec_data
*)talloc_size(mem_ctx
, length
);
189 d
->keylen
= key
.dsize
;
190 memcpy(&d
->data
[0], key
.dptr
, key
.dsize
);
192 d
->datalen
= data
.dsize
+ sizeof(*header
);
193 memcpy(&d
->data
[key
.dsize
], header
, sizeof(*header
));
194 memcpy(&d
->data
[key
.dsize
+sizeof(*header
)], data
.dptr
, data
.dsize
);
196 d
->datalen
= data
.dsize
;
197 memcpy(&d
->data
[key
.dsize
], data
.dptr
, data
.dsize
);
203 /* helper function for marshalling multiple records */
204 static struct ctdb_marshall_buffer
*db_ctdb_marshall_add(TALLOC_CTX
*mem_ctx
,
205 struct ctdb_marshall_buffer
*m
,
209 struct ctdb_ltdb_header
*header
,
212 struct ctdb_rec_data
*r
;
213 size_t m_size
, r_size
;
214 struct ctdb_marshall_buffer
*m2
= NULL
;
216 r
= db_ctdb_marshall_record(talloc_tos(), reqid
, key
, header
, data
);
223 m
= (struct ctdb_marshall_buffer
*)talloc_zero_size(
224 mem_ctx
, offsetof(struct ctdb_marshall_buffer
, data
));
231 m_size
= talloc_get_size(m
);
232 r_size
= talloc_get_size(r
);
234 m2
= (struct ctdb_marshall_buffer
*)talloc_realloc_size(
235 mem_ctx
, m
, m_size
+ r_size
);
241 memcpy(m_size
+ (uint8_t *)m2
, r
, r_size
);
250 /* we've finished marshalling, return a data blob with the marshalled records */
251 static TDB_DATA
db_ctdb_marshall_finish(struct ctdb_marshall_buffer
*m
)
254 data
.dptr
= (uint8_t *)m
;
255 data
.dsize
= talloc_get_size(m
);
260 loop over a marshalling buffer
262 - pass r==NULL to start
263 - loop the number of times indicated by m->count
265 static struct ctdb_rec_data
*db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer
*m
, struct ctdb_rec_data
*r
,
267 struct ctdb_ltdb_header
*header
,
268 TDB_DATA
*key
, TDB_DATA
*data
)
271 r
= (struct ctdb_rec_data
*)&m
->data
[0];
273 r
= (struct ctdb_rec_data
*)(r
->length
+ (uint8_t *)r
);
281 key
->dptr
= &r
->data
[0];
282 key
->dsize
= r
->keylen
;
285 data
->dptr
= &r
->data
[r
->keylen
];
286 data
->dsize
= r
->datalen
;
287 if (header
!= NULL
) {
288 data
->dptr
+= sizeof(*header
);
289 data
->dsize
-= sizeof(*header
);
293 if (header
!= NULL
) {
294 if (r
->datalen
< sizeof(*header
)) {
297 *header
= *(struct ctdb_ltdb_header
*)&r
->data
[r
->keylen
];
304 * CTDB transaction destructor
306 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle
*h
)
310 status
= g_lock_unlock(h
->ctx
->lock_ctx
, h
->lock_name
);
311 if (!NT_STATUS_IS_OK(status
)) {
312 DEBUG(0, ("g_lock_unlock failed: %s\n", nt_errstr(status
)));
319 * CTDB dbwrap API: transaction_start function
320 * starts a transaction on a persistent database
322 static int db_ctdb_transaction_start(struct db_context
*db
)
324 struct db_ctdb_transaction_handle
*h
;
326 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
329 if (!db
->persistent
) {
330 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
335 if (ctx
->transaction
) {
336 ctx
->transaction
->nesting
++;
340 h
= talloc_zero(db
, struct db_ctdb_transaction_handle
);
342 DEBUG(0,(__location__
" oom for transaction handle\n"));
348 h
->lock_name
= talloc_asprintf(h
, "transaction_db_0x%08x",
349 (unsigned int)ctx
->db_id
);
350 if (h
->lock_name
== NULL
) {
351 DEBUG(0, ("talloc_asprintf failed\n"));
357 * Wait a day, i.e. forever...
359 status
= g_lock_lock(ctx
->lock_ctx
, h
->lock_name
, G_LOCK_WRITE
,
360 timeval_set(86400, 0));
361 if (!NT_STATUS_IS_OK(status
)) {
362 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status
)));
367 talloc_set_destructor(h
, db_ctdb_transaction_destructor
);
369 ctx
->transaction
= h
;
371 DEBUG(5,(__location__
" Started transaction on db 0x%08x\n", ctx
->db_id
));
376 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer
*buf
,
378 struct ctdb_ltdb_header
*pheader
,
382 struct ctdb_rec_data
*rec
= NULL
;
383 struct ctdb_ltdb_header h
;
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
++) {
404 TDB_DATA tkey
, tdata
;
406 struct ctdb_ltdb_header hdr
;
410 rec
= db_ctdb_marshall_loop_next(buf
, rec
, &reqid
, &hdr
, &tkey
,
416 if (tdb_data_equal(key
, tkey
)) {
428 data
.dptr
= (uint8_t *)talloc_memdup(mem_ctx
, data
.dptr
,
430 if ((data
.dsize
!= 0) && (data
.dptr
== NULL
)) {
436 if (pheader
!= NULL
) {
444 fetch a record inside a transaction
446 static int db_ctdb_transaction_fetch(struct db_ctdb_ctx
*db
,
448 TDB_DATA key
, TDB_DATA
*data
)
450 struct db_ctdb_transaction_handle
*h
= db
->transaction
;
454 found
= pull_newest_from_marshall_buffer(h
->m_write
, key
, NULL
,
460 status
= db_ctdb_ltdb_fetch(h
->ctx
, key
, NULL
, mem_ctx
, data
);
462 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
464 } else if (!NT_STATUS_IS_OK(status
)) {
468 h
->m_all
= db_ctdb_marshall_add(h
, h
->m_all
, h
->ctx
->db_id
, 1, key
,
470 if (h
->m_all
== NULL
) {
471 DEBUG(0,(__location__
" Failed to add to marshalling "
474 talloc_free(data
->dptr
);
482 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
);
483 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
);
485 static struct db_record
*db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx
*ctx
,
489 struct db_record
*result
;
492 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
493 DEBUG(0, ("talloc failed\n"));
497 result
->private_data
= ctx
->transaction
;
499 result
->key
.dsize
= key
.dsize
;
500 result
->key
.dptr
= (uint8
*)talloc_memdup(result
, key
.dptr
, key
.dsize
);
501 if (result
->key
.dptr
== NULL
) {
502 DEBUG(0, ("talloc failed\n"));
507 result
->store
= db_ctdb_store_transaction
;
508 result
->delete_rec
= db_ctdb_delete_transaction
;
510 if (pull_newest_from_marshall_buffer(ctx
->transaction
->m_write
, key
,
511 NULL
, result
, &result
->value
)) {
515 ctdb_data
= tdb_fetch(ctx
->wtdb
->tdb
, key
);
516 if (ctdb_data
.dptr
== NULL
) {
517 /* create the record */
518 result
->value
= tdb_null
;
522 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
523 result
->value
.dptr
= NULL
;
525 if ((result
->value
.dsize
!= 0)
526 && !(result
->value
.dptr
= (uint8
*)talloc_memdup(
527 result
, ctdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
528 result
->value
.dsize
))) {
529 DEBUG(0, ("talloc failed\n"));
533 SAFE_FREE(ctdb_data
.dptr
);
538 static int db_ctdb_record_destructor(struct db_record
**recp
)
540 struct db_record
*rec
= talloc_get_type_abort(*recp
, struct db_record
);
541 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
542 rec
->private_data
, struct db_ctdb_transaction_handle
);
543 int ret
= h
->ctx
->db
->transaction_commit(h
->ctx
->db
);
545 DEBUG(0,(__location__
" transaction_commit failed\n"));
551 auto-create a transaction for persistent databases
553 static struct db_record
*db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx
*ctx
,
558 struct db_record
*rec
, **recp
;
560 res
= db_ctdb_transaction_start(ctx
->db
);
565 rec
= db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
567 ctx
->db
->transaction_cancel(ctx
->db
);
571 /* destroy this transaction when we release the lock */
572 recp
= talloc(rec
, struct db_record
*);
574 ctx
->db
->transaction_cancel(ctx
->db
);
579 talloc_set_destructor(recp
, db_ctdb_record_destructor
);
585 stores a record inside a transaction
587 static NTSTATUS
db_ctdb_transaction_store(struct db_ctdb_transaction_handle
*h
,
588 TDB_DATA key
, TDB_DATA data
)
590 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
592 struct ctdb_ltdb_header header
;
596 /* we need the header so we can update the RSN */
598 if (!pull_newest_from_marshall_buffer(h
->m_write
, key
, &header
,
601 rec
= tdb_fetch(h
->ctx
->wtdb
->tdb
, key
);
603 if (rec
.dptr
!= NULL
) {
604 memcpy(&header
, rec
.dptr
,
605 sizeof(struct ctdb_ltdb_header
));
606 rec
.dsize
-= sizeof(struct ctdb_ltdb_header
);
609 * a special case, we are writing the same
610 * data that is there now
612 if (data
.dsize
== rec
.dsize
&&
614 rec
.dptr
+ sizeof(struct ctdb_ltdb_header
),
617 talloc_free(tmp_ctx
);
624 header
.dmaster
= get_my_vnn();
627 h
->m_all
= db_ctdb_marshall_add(h
, h
->m_all
, h
->ctx
->db_id
, 0, key
,
629 if (h
->m_all
== NULL
) {
630 DEBUG(0,(__location__
" Failed to add to marshalling "
632 talloc_free(tmp_ctx
);
633 return NT_STATUS_NO_MEMORY
;
636 h
->m_write
= db_ctdb_marshall_add(h
, h
->m_write
, h
->ctx
->db_id
, 0, key
, &header
, data
);
637 if (h
->m_write
== NULL
) {
638 DEBUG(0,(__location__
" Failed to add to marshalling record\n"));
639 talloc_free(tmp_ctx
);
640 return NT_STATUS_NO_MEMORY
;
643 talloc_free(tmp_ctx
);
649 a record store inside a transaction
651 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
)
653 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
654 rec
->private_data
, struct db_ctdb_transaction_handle
);
657 status
= db_ctdb_transaction_store(h
, rec
->key
, data
);
662 a record delete inside a transaction
664 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
)
666 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
667 rec
->private_data
, struct db_ctdb_transaction_handle
);
670 status
= db_ctdb_transaction_store(h
, rec
->key
, tdb_null
);
675 * Fetch the db sequence number of a persistent db directly from the db.
677 static NTSTATUS
db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx
*db
,
681 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
684 struct ctdb_ltdb_header header
;
685 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
687 if (seqnum
== NULL
) {
688 return NT_STATUS_INVALID_PARAMETER
;
691 key
.dptr
= (uint8_t *)discard_const(keyname
);
692 key
.dsize
= strlen(keyname
) + 1;
694 status
= db_ctdb_ltdb_fetch(db
, key
, &header
, mem_ctx
, &data
);
695 if (!NT_STATUS_IS_OK(status
) &&
696 !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
))
701 status
= NT_STATUS_OK
;
703 if (data
.dsize
!= sizeof(uint64_t)) {
708 *seqnum
= *(uint64_t *)data
.dptr
;
711 TALLOC_FREE(mem_ctx
);
716 * Store the database sequence number inside a transaction.
718 static NTSTATUS
db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle
*h
,
722 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
726 key
.dptr
= (uint8_t *)discard_const(keyname
);
727 key
.dsize
= strlen(keyname
);
729 data
.dptr
= (uint8_t *)&seqnum
;
730 data
.dsize
= sizeof(uint64_t);
732 status
= db_ctdb_transaction_store(h
, key
, data
);
740 static int db_ctdb_transaction_commit(struct db_context
*db
)
742 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
746 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
747 uint64_t old_seqnum
, new_seqnum
;
751 DEBUG(0,(__location__
" transaction commit with no open transaction on db 0x%08x\n", ctx
->db_id
));
755 if (h
->nested_cancel
) {
756 db
->transaction_cancel(db
);
757 DEBUG(5,(__location__
" Failed transaction commit after nested cancel\n"));
761 if (h
->nesting
!= 0) {
766 DEBUG(5,(__location__
" Commit transaction on db 0x%08x\n", ctx
->db_id
));
769 * As the last db action before committing, bump the database sequence
770 * number. Note that this undoes all changes to the seqnum records
771 * performed under the transaction. This record is not meant to be
772 * modified by user interaction. It is for internal use only...
774 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &old_seqnum
);
775 if (!NT_STATUS_IS_OK(rets
)) {
776 DEBUG(1, (__location__
" failed to fetch the db sequence number "
777 "in transaction commit on db 0x%08x\n", ctx
->db_id
));
782 new_seqnum
= old_seqnum
+ 1;
784 rets
= db_ctdb_store_db_seqnum(h
, new_seqnum
);
785 if (!NT_STATUS_IS_OK(rets
)) {
786 DEBUG(1, (__location__
"failed to store the db sequence number "
787 " in transaction commit on db 0x%08x\n", ctx
->db_id
));
793 if (h
->m_write
== NULL
) {
794 /* no changes were made, potentially after a retry */
798 /* tell ctdbd to commit to the other nodes */
799 rets
= ctdbd_control_local(messaging_ctdbd_connection(),
800 CTDB_CONTROL_TRANS3_COMMIT
,
802 db_ctdb_marshall_finish(h
->m_write
),
803 NULL
, NULL
, &status
);
804 if (!NT_STATUS_IS_OK(rets
) || status
!= 0) {
806 * The TRANS3_COMMIT control should only possibly fail when a
807 * recovery has been running concurrently. In any case, the db
808 * will be the same on all nodes, either the new copy or the
809 * old copy. This can be detected by comparing the old and new
810 * local sequence numbers.
812 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &new_seqnum
);
813 if (!NT_STATUS_IS_OK(rets
)) {
814 DEBUG(1, (__location__
" failed to refetch db sequence "
815 "number after failed TRANS3_COMMIT\n"));
820 if (new_seqnum
== old_seqnum
) {
821 /* Recovery prevented all our changes: retry. */
823 } else if (new_seqnum
!= (old_seqnum
+ 1)) {
824 DEBUG(0, (__location__
" ERROR: new_seqnum[%lu] != "
825 "old_seqnum[%lu] + (0 or 1) after failed "
826 "TRANS3_COMMIT - this should not happen!\n",
827 (unsigned long)new_seqnum
,
828 (unsigned long)old_seqnum
));
833 * Recovery propagated our changes to all nodes, completing
834 * our commit for us - succeed.
841 h
->ctx
->transaction
= NULL
;
850 static int db_ctdb_transaction_cancel(struct db_context
*db
)
852 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
854 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
857 DEBUG(0,(__location__
" transaction cancel with no open transaction on db 0x%08x\n", ctx
->db_id
));
861 if (h
->nesting
!= 0) {
863 h
->nested_cancel
= true;
867 DEBUG(5,(__location__
" Cancel transaction on db 0x%08x\n", ctx
->db_id
));
869 ctx
->transaction
= NULL
;
875 static NTSTATUS
db_ctdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
)
877 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
878 rec
->private_data
, struct db_ctdb_rec
);
880 return db_ctdb_ltdb_store(crec
->ctdb_ctx
, rec
->key
, &(crec
->header
), data
);
885 static NTSTATUS
db_ctdb_delete(struct db_record
*rec
)
890 * We have to store the header with empty data. TODO: Fix the
896 return db_ctdb_store(rec
, data
, 0);
900 static int db_ctdb_record_destr(struct db_record
* data
)
902 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
903 data
->private_data
, struct db_ctdb_rec
);
905 DEBUG(10, (DEBUGLEVEL
> 10
906 ? "Unlocking db %u key %s\n"
907 : "Unlocking db %u key %.20s\n",
908 (int)crec
->ctdb_ctx
->db_id
,
909 hex_encode_talloc(data
, (unsigned char *)data
->key
.dptr
,
912 if (tdb_chainunlock(crec
->ctdb_ctx
->wtdb
->tdb
, data
->key
) != 0) {
913 DEBUG(0, ("tdb_chainunlock failed\n"));
920 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
925 struct db_record
*result
;
926 struct db_ctdb_rec
*crec
;
929 int migrate_attempts
= 0;
931 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
932 DEBUG(0, ("talloc failed\n"));
936 if (!(crec
= TALLOC_ZERO_P(result
, struct db_ctdb_rec
))) {
937 DEBUG(0, ("talloc failed\n"));
942 result
->private_data
= (void *)crec
;
943 crec
->ctdb_ctx
= ctx
;
945 result
->key
.dsize
= key
.dsize
;
946 result
->key
.dptr
= (uint8
*)talloc_memdup(result
, key
.dptr
, key
.dsize
);
947 if (result
->key
.dptr
== NULL
) {
948 DEBUG(0, ("talloc failed\n"));
954 * Do a blocking lock on the record
958 if (DEBUGLEVEL
>= 10) {
959 char *keystr
= hex_encode_talloc(result
, key
.dptr
, key
.dsize
);
960 DEBUG(10, (DEBUGLEVEL
> 10
961 ? "Locking db %u key %s\n"
962 : "Locking db %u key %.20s\n",
963 (int)crec
->ctdb_ctx
->db_id
, keystr
));
967 if (tdb_chainlock(ctx
->wtdb
->tdb
, key
) != 0) {
968 DEBUG(3, ("tdb_chainlock failed\n"));
973 result
->store
= db_ctdb_store
;
974 result
->delete_rec
= db_ctdb_delete
;
975 talloc_set_destructor(result
, db_ctdb_record_destr
);
977 ctdb_data
= tdb_fetch(ctx
->wtdb
->tdb
, key
);
980 * See if we have a valid record and we are the dmaster. If so, we can
981 * take the shortcut and just return it.
984 if ((ctdb_data
.dptr
== NULL
) ||
985 (ctdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
)) ||
986 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
!= get_my_vnn()
988 || (random() % 2 != 0)
991 SAFE_FREE(ctdb_data
.dptr
);
992 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
993 talloc_set_destructor(result
, NULL
);
995 migrate_attempts
+= 1;
997 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
998 ctdb_data
.dptr
, ctdb_data
.dptr
?
999 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
: -1,
1002 status
= ctdbd_migrate(messaging_ctdbd_connection(),ctx
->db_id
, key
);
1003 if (!NT_STATUS_IS_OK(status
)) {
1004 DEBUG(5, ("ctdb_migrate failed: %s\n",
1005 nt_errstr(status
)));
1006 TALLOC_FREE(result
);
1009 /* now its migrated, try again */
1013 if (migrate_attempts
> 10) {
1014 DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
1018 memcpy(&crec
->header
, ctdb_data
.dptr
, sizeof(crec
->header
));
1020 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(crec
->header
);
1021 result
->value
.dptr
= NULL
;
1023 if ((result
->value
.dsize
!= 0)
1024 && !(result
->value
.dptr
= (uint8
*)talloc_memdup(
1025 result
, ctdb_data
.dptr
+ sizeof(crec
->header
),
1026 result
->value
.dsize
))) {
1027 DEBUG(0, ("talloc failed\n"));
1028 TALLOC_FREE(result
);
1031 SAFE_FREE(ctdb_data
.dptr
);
1036 static struct db_record
*db_ctdb_fetch_locked(struct db_context
*db
,
1037 TALLOC_CTX
*mem_ctx
,
1040 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1041 struct db_ctdb_ctx
);
1043 if (ctx
->transaction
!= NULL
) {
1044 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1047 if (db
->persistent
) {
1048 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1051 return fetch_locked_internal(ctx
, mem_ctx
, key
, db
->persistent
);
1055 fetch (unlocked, no migration) operation on ctdb
1057 static int db_ctdb_fetch(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
1058 TDB_DATA key
, TDB_DATA
*data
)
1060 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1061 struct db_ctdb_ctx
);
1065 if (ctx
->transaction
) {
1066 return db_ctdb_transaction_fetch(ctx
, mem_ctx
, key
, data
);
1069 /* try a direct fetch */
1070 ctdb_data
= tdb_fetch(ctx
->wtdb
->tdb
, key
);
1073 * See if we have a valid record and we are the dmaster. If so, we can
1074 * take the shortcut and just return it.
1075 * we bypass the dmaster check for persistent databases
1077 if ((ctdb_data
.dptr
!= NULL
) &&
1078 (ctdb_data
.dsize
>= sizeof(struct ctdb_ltdb_header
)) &&
1080 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
== get_my_vnn())) {
1081 /* we are the dmaster - avoid the ctdb protocol op */
1083 data
->dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
1084 if (data
->dsize
== 0) {
1085 SAFE_FREE(ctdb_data
.dptr
);
1090 data
->dptr
= (uint8
*)talloc_memdup(
1091 mem_ctx
, ctdb_data
.dptr
+sizeof(struct ctdb_ltdb_header
),
1094 SAFE_FREE(ctdb_data
.dptr
);
1096 if (data
->dptr
== NULL
) {
1102 SAFE_FREE(ctdb_data
.dptr
);
1104 /* we weren't able to get it locally - ask ctdb to fetch it for us */
1105 status
= ctdbd_fetch(messaging_ctdbd_connection(),ctx
->db_id
, key
, mem_ctx
, data
);
1106 if (!NT_STATUS_IS_OK(status
)) {
1107 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status
)));
1114 struct traverse_state
{
1115 struct db_context
*db
;
1116 int (*fn
)(struct db_record
*rec
, void *private_data
);
1120 static void traverse_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1122 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1123 struct db_record
*rec
;
1124 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1125 /* we have to give them a locked record to prevent races */
1126 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, key
);
1127 if (rec
&& rec
->value
.dsize
> 0) {
1128 state
->fn(rec
, state
->private_data
);
1130 talloc_free(tmp_ctx
);
1133 static int traverse_persistent_callback(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1136 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1137 struct db_record
*rec
;
1138 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1140 /* we have to give them a locked record to prevent races */
1141 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, kbuf
);
1142 if (rec
&& rec
->value
.dsize
> 0) {
1143 ret
= state
->fn(rec
, state
->private_data
);
1145 talloc_free(tmp_ctx
);
1149 static int db_ctdb_traverse(struct db_context
*db
,
1150 int (*fn
)(struct db_record
*rec
,
1151 void *private_data
),
1154 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1155 struct db_ctdb_ctx
);
1156 struct traverse_state state
;
1160 state
.private_data
= private_data
;
1162 if (db
->persistent
) {
1163 /* for persistent databases we don't need to do a ctdb traverse,
1164 we can do a faster local traverse */
1165 return tdb_traverse(ctx
->wtdb
->tdb
, traverse_persistent_callback
, &state
);
1169 ctdbd_traverse(ctx
->db_id
, traverse_callback
, &state
);
1173 static NTSTATUS
db_ctdb_store_deny(struct db_record
*rec
, TDB_DATA data
, int flag
)
1175 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1178 static NTSTATUS
db_ctdb_delete_deny(struct db_record
*rec
)
1180 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1183 static void traverse_read_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1185 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1186 struct db_record rec
;
1189 rec
.store
= db_ctdb_store_deny
;
1190 rec
.delete_rec
= db_ctdb_delete_deny
;
1191 rec
.private_data
= state
->db
;
1192 state
->fn(&rec
, state
->private_data
);
1195 static int traverse_persistent_callback_read(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1198 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1199 struct db_record rec
;
1202 rec
.store
= db_ctdb_store_deny
;
1203 rec
.delete_rec
= db_ctdb_delete_deny
;
1204 rec
.private_data
= state
->db
;
1206 if (rec
.value
.dsize
<= sizeof(struct ctdb_ltdb_header
)) {
1207 /* a deleted record */
1210 rec
.value
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1211 rec
.value
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1213 return state
->fn(&rec
, state
->private_data
);
1216 static int db_ctdb_traverse_read(struct db_context
*db
,
1217 int (*fn
)(struct db_record
*rec
,
1218 void *private_data
),
1221 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1222 struct db_ctdb_ctx
);
1223 struct traverse_state state
;
1227 state
.private_data
= private_data
;
1229 if (db
->persistent
) {
1230 /* for persistent databases we don't need to do a ctdb traverse,
1231 we can do a faster local traverse */
1232 return tdb_traverse_read(ctx
->wtdb
->tdb
, traverse_persistent_callback_read
, &state
);
1235 ctdbd_traverse(ctx
->db_id
, traverse_read_callback
, &state
);
1239 static int db_ctdb_get_seqnum(struct db_context
*db
)
1241 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1242 struct db_ctdb_ctx
);
1243 return tdb_get_seqnum(ctx
->wtdb
->tdb
);
1246 static int db_ctdb_get_flags(struct db_context
*db
)
1248 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1249 struct db_ctdb_ctx
);
1250 return tdb_get_flags(ctx
->wtdb
->tdb
);
1253 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1255 int hash_size
, int tdb_flags
,
1256 int open_flags
, mode_t mode
)
1258 struct db_context
*result
;
1259 struct db_ctdb_ctx
*db_ctdb
;
1261 struct ctdbd_connection
*conn
;
1263 if (!lp_clustering()) {
1264 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1268 if (!(result
= TALLOC_ZERO_P(mem_ctx
, struct db_context
))) {
1269 DEBUG(0, ("talloc failed\n"));
1270 TALLOC_FREE(result
);
1274 if (!(db_ctdb
= TALLOC_P(result
, struct db_ctdb_ctx
))) {
1275 DEBUG(0, ("talloc failed\n"));
1276 TALLOC_FREE(result
);
1280 db_ctdb
->transaction
= NULL
;
1281 db_ctdb
->db
= result
;
1283 conn
= messaging_ctdbd_connection();
1285 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn
, name
, &db_ctdb
->db_id
, tdb_flags
))) {
1286 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name
));
1287 TALLOC_FREE(result
);
1291 db_path
= ctdbd_dbpath(conn
, db_ctdb
, db_ctdb
->db_id
);
1293 result
->persistent
= ((tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0);
1295 /* only pass through specific flags */
1296 tdb_flags
&= TDB_SEQNUM
;
1298 /* honor permissions if user has specified O_CREAT */
1299 if (open_flags
& O_CREAT
) {
1300 chmod(db_path
, mode
);
1303 db_ctdb
->wtdb
= tdb_wrap_open(db_ctdb
, db_path
, hash_size
, tdb_flags
, O_RDWR
, 0);
1304 if (db_ctdb
->wtdb
== NULL
) {
1305 DEBUG(0, ("Could not open tdb %s: %s\n", db_path
, strerror(errno
)));
1306 TALLOC_FREE(result
);
1309 talloc_free(db_path
);
1311 if (result
->persistent
) {
1312 db_ctdb
->lock_ctx
= g_lock_ctx_init(db_ctdb
,
1313 ctdb_conn_msg_ctx(conn
));
1314 if (db_ctdb
->lock_ctx
== NULL
) {
1315 DEBUG(0, ("g_lock_ctx_init failed\n"));
1316 TALLOC_FREE(result
);
1321 result
->private_data
= (void *)db_ctdb
;
1322 result
->fetch_locked
= db_ctdb_fetch_locked
;
1323 result
->fetch
= db_ctdb_fetch
;
1324 result
->traverse
= db_ctdb_traverse
;
1325 result
->traverse_read
= db_ctdb_traverse_read
;
1326 result
->get_seqnum
= db_ctdb_get_seqnum
;
1327 result
->get_flags
= db_ctdb_get_flags
;
1328 result
->transaction_start
= db_ctdb_transaction_start
;
1329 result
->transaction_commit
= db_ctdb_transaction_commit
;
1330 result
->transaction_cancel
= db_ctdb_transaction_cancel
;
1332 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1333 name
, db_ctdb
->db_id
));