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/util/tdb_wrap.h"
25 #ifdef CLUSTER_SUPPORT
27 #include "ctdb_private.h"
28 #include "ctdbd_conn.h"
32 struct db_ctdb_transaction_handle
{
33 struct db_ctdb_ctx
*ctx
;
35 * we store the reads and writes done under a transaction:
36 * - one list stores both reads and writes (m_all),
37 * - the other just writes (m_write)
39 struct ctdb_marshall_buffer
*m_all
;
40 struct ctdb_marshall_buffer
*m_write
;
47 struct db_context
*db
;
48 struct tdb_wrap
*wtdb
;
50 struct db_ctdb_transaction_handle
*transaction
;
51 struct g_lock_ctx
*lock_ctx
;
55 struct db_ctdb_ctx
*ctdb_ctx
;
56 struct ctdb_ltdb_header header
;
57 struct timeval lock_time
;
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_compat(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 * Fetch a record from a persistent database
483 * without record locking and without an active transaction.
485 * This just fetches from the local database copy.
486 * Since the databases are kept in syc cluster-wide,
487 * there is no point in doing a ctdb call to fetch the
488 * record from the lmaster. It does even harm since migration
489 * of records bump their RSN and hence render the persistent
490 * database inconsistent.
492 static int db_ctdb_fetch_persistent(struct db_ctdb_ctx
*db
,
494 TDB_DATA key
, TDB_DATA
*data
)
498 status
= db_ctdb_ltdb_fetch(db
, key
, NULL
, mem_ctx
, data
);
500 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
502 } else if (!NT_STATUS_IS_OK(status
)) {
509 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
);
510 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
);
512 static struct db_record
*db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx
*ctx
,
516 struct db_record
*result
;
519 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
520 DEBUG(0, ("talloc failed\n"));
524 result
->private_data
= ctx
->transaction
;
526 result
->key
.dsize
= key
.dsize
;
527 result
->key
.dptr
= (uint8
*)talloc_memdup(result
, key
.dptr
, key
.dsize
);
528 if (result
->key
.dptr
== NULL
) {
529 DEBUG(0, ("talloc failed\n"));
534 result
->store
= db_ctdb_store_transaction
;
535 result
->delete_rec
= db_ctdb_delete_transaction
;
537 if (pull_newest_from_marshall_buffer(ctx
->transaction
->m_write
, key
,
538 NULL
, result
, &result
->value
)) {
542 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
543 if (ctdb_data
.dptr
== NULL
) {
544 /* create the record */
545 result
->value
= tdb_null
;
549 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
550 result
->value
.dptr
= NULL
;
552 if ((result
->value
.dsize
!= 0)
553 && !(result
->value
.dptr
= (uint8
*)talloc_memdup(
554 result
, ctdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
555 result
->value
.dsize
))) {
556 DEBUG(0, ("talloc failed\n"));
560 SAFE_FREE(ctdb_data
.dptr
);
565 static int db_ctdb_record_destructor(struct db_record
**recp
)
567 struct db_record
*rec
= talloc_get_type_abort(*recp
, struct db_record
);
568 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
569 rec
->private_data
, struct db_ctdb_transaction_handle
);
570 int ret
= h
->ctx
->db
->transaction_commit(h
->ctx
->db
);
572 DEBUG(0,(__location__
" transaction_commit failed\n"));
578 auto-create a transaction for persistent databases
580 static struct db_record
*db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx
*ctx
,
585 struct db_record
*rec
, **recp
;
587 res
= db_ctdb_transaction_start(ctx
->db
);
592 rec
= db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
594 ctx
->db
->transaction_cancel(ctx
->db
);
598 /* destroy this transaction when we release the lock */
599 recp
= talloc(rec
, struct db_record
*);
601 ctx
->db
->transaction_cancel(ctx
->db
);
606 talloc_set_destructor(recp
, db_ctdb_record_destructor
);
612 stores a record inside a transaction
614 static NTSTATUS
db_ctdb_transaction_store(struct db_ctdb_transaction_handle
*h
,
615 TDB_DATA key
, TDB_DATA data
)
617 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
619 struct ctdb_ltdb_header header
;
623 /* we need the header so we can update the RSN */
625 if (!pull_newest_from_marshall_buffer(h
->m_write
, key
, &header
,
628 rec
= tdb_fetch_compat(h
->ctx
->wtdb
->tdb
, key
);
630 if (rec
.dptr
!= NULL
) {
631 memcpy(&header
, rec
.dptr
,
632 sizeof(struct ctdb_ltdb_header
));
633 rec
.dsize
-= sizeof(struct ctdb_ltdb_header
);
636 * a special case, we are writing the same
637 * data that is there now
639 if (data
.dsize
== rec
.dsize
&&
641 rec
.dptr
+ sizeof(struct ctdb_ltdb_header
),
644 talloc_free(tmp_ctx
);
651 header
.dmaster
= get_my_vnn();
654 h
->m_all
= db_ctdb_marshall_add(h
, h
->m_all
, h
->ctx
->db_id
, 0, key
,
656 if (h
->m_all
== NULL
) {
657 DEBUG(0,(__location__
" Failed to add to marshalling "
659 talloc_free(tmp_ctx
);
660 return NT_STATUS_NO_MEMORY
;
663 h
->m_write
= db_ctdb_marshall_add(h
, h
->m_write
, h
->ctx
->db_id
, 0, key
, &header
, data
);
664 if (h
->m_write
== NULL
) {
665 DEBUG(0,(__location__
" Failed to add to marshalling record\n"));
666 talloc_free(tmp_ctx
);
667 return NT_STATUS_NO_MEMORY
;
670 talloc_free(tmp_ctx
);
676 a record store inside a transaction
678 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
)
680 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
681 rec
->private_data
, struct db_ctdb_transaction_handle
);
684 status
= db_ctdb_transaction_store(h
, rec
->key
, data
);
689 a record delete inside a transaction
691 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
)
693 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
694 rec
->private_data
, struct db_ctdb_transaction_handle
);
697 status
= db_ctdb_transaction_store(h
, rec
->key
, tdb_null
);
702 * Fetch the db sequence number of a persistent db directly from the db.
704 static NTSTATUS
db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx
*db
,
708 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
711 struct ctdb_ltdb_header header
;
712 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
714 if (seqnum
== NULL
) {
715 return NT_STATUS_INVALID_PARAMETER
;
718 key
= string_term_tdb_data(keyname
);
720 status
= db_ctdb_ltdb_fetch(db
, key
, &header
, mem_ctx
, &data
);
721 if (!NT_STATUS_IS_OK(status
) &&
722 !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
))
727 status
= NT_STATUS_OK
;
729 if (data
.dsize
!= sizeof(uint64_t)) {
734 *seqnum
= *(uint64_t *)data
.dptr
;
737 TALLOC_FREE(mem_ctx
);
742 * Store the database sequence number inside a transaction.
744 static NTSTATUS
db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle
*h
,
748 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
752 key
= string_term_tdb_data(keyname
);
754 data
.dptr
= (uint8_t *)&seqnum
;
755 data
.dsize
= sizeof(uint64_t);
757 status
= db_ctdb_transaction_store(h
, key
, data
);
765 static int db_ctdb_transaction_commit(struct db_context
*db
)
767 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
771 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
772 uint64_t old_seqnum
, new_seqnum
;
776 DEBUG(0,(__location__
" transaction commit with no open transaction on db 0x%08x\n", ctx
->db_id
));
780 if (h
->nested_cancel
) {
781 db
->transaction_cancel(db
);
782 DEBUG(5,(__location__
" Failed transaction commit after nested cancel\n"));
786 if (h
->nesting
!= 0) {
791 if (h
->m_write
== NULL
) {
793 * No changes were made, so don't change the seqnum,
794 * don't push to other node, just exit with success.
800 DEBUG(5,(__location__
" Commit transaction on db 0x%08x\n", ctx
->db_id
));
803 * As the last db action before committing, bump the database sequence
804 * number. Note that this undoes all changes to the seqnum records
805 * performed under the transaction. This record is not meant to be
806 * modified by user interaction. It is for internal use only...
808 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &old_seqnum
);
809 if (!NT_STATUS_IS_OK(rets
)) {
810 DEBUG(1, (__location__
" failed to fetch the db sequence number "
811 "in transaction commit on db 0x%08x\n", ctx
->db_id
));
816 new_seqnum
= old_seqnum
+ 1;
818 rets
= db_ctdb_store_db_seqnum(h
, new_seqnum
);
819 if (!NT_STATUS_IS_OK(rets
)) {
820 DEBUG(1, (__location__
"failed to store the db sequence number "
821 " in transaction commit on db 0x%08x\n", ctx
->db_id
));
827 /* tell ctdbd to commit to the other nodes */
828 rets
= ctdbd_control_local(messaging_ctdbd_connection(),
829 CTDB_CONTROL_TRANS3_COMMIT
,
831 db_ctdb_marshall_finish(h
->m_write
),
832 NULL
, NULL
, &status
);
833 if (!NT_STATUS_IS_OK(rets
) || status
!= 0) {
835 * The TRANS3_COMMIT control should only possibly fail when a
836 * recovery has been running concurrently. In any case, the db
837 * will be the same on all nodes, either the new copy or the
838 * old copy. This can be detected by comparing the old and new
839 * local sequence numbers.
841 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &new_seqnum
);
842 if (!NT_STATUS_IS_OK(rets
)) {
843 DEBUG(1, (__location__
" failed to refetch db sequence "
844 "number after failed TRANS3_COMMIT\n"));
849 if (new_seqnum
== old_seqnum
) {
850 /* Recovery prevented all our changes: retry. */
852 } else if (new_seqnum
!= (old_seqnum
+ 1)) {
853 DEBUG(0, (__location__
" ERROR: new_seqnum[%lu] != "
854 "old_seqnum[%lu] + (0 or 1) after failed "
855 "TRANS3_COMMIT - this should not happen!\n",
856 (unsigned long)new_seqnum
,
857 (unsigned long)old_seqnum
));
862 * Recovery propagated our changes to all nodes, completing
863 * our commit for us - succeed.
870 h
->ctx
->transaction
= NULL
;
879 static int db_ctdb_transaction_cancel(struct db_context
*db
)
881 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
883 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
886 DEBUG(0,(__location__
" transaction cancel with no open transaction on db 0x%08x\n", ctx
->db_id
));
890 if (h
->nesting
!= 0) {
892 h
->nested_cancel
= true;
896 DEBUG(5,(__location__
" Cancel transaction on db 0x%08x\n", ctx
->db_id
));
898 ctx
->transaction
= NULL
;
904 static NTSTATUS
db_ctdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
)
906 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
907 rec
->private_data
, struct db_ctdb_rec
);
909 return db_ctdb_ltdb_store(crec
->ctdb_ctx
, rec
->key
, &(crec
->header
), data
);
914 #ifdef CTDB_CONTROL_SCHEDULE_FOR_DELETION
915 static NTSTATUS
db_ctdb_send_schedule_for_deletion(struct db_record
*rec
)
918 struct ctdb_control_schedule_for_deletion
*dd
;
921 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
922 rec
->private_data
, struct db_ctdb_rec
);
924 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + rec
->key
.dsize
;
925 indata
.dptr
= talloc_zero_array(crec
, uint8_t, indata
.dsize
);
926 if (indata
.dptr
== NULL
) {
927 DEBUG(0, (__location__
" talloc failed!\n"));
928 return NT_STATUS_NO_MEMORY
;
931 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
932 dd
->db_id
= crec
->ctdb_ctx
->db_id
;
933 dd
->hdr
= crec
->header
;
934 dd
->keylen
= rec
->key
.dsize
;
935 memcpy(dd
->key
, rec
->key
.dptr
, rec
->key
.dsize
);
937 status
= ctdbd_control_local(messaging_ctdbd_connection(),
938 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
939 crec
->ctdb_ctx
->db_id
,
940 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
945 talloc_free(indata
.dptr
);
947 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
948 DEBUG(1, (__location__
" Error sending local control "
949 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
950 nt_errstr(status
), cstatus
));
951 if (NT_STATUS_IS_OK(status
)) {
952 status
= NT_STATUS_UNSUCCESSFUL
;
960 static NTSTATUS
db_ctdb_delete(struct db_record
*rec
)
966 * We have to store the header with empty data. TODO: Fix the
972 status
= db_ctdb_store(rec
, data
, 0);
973 if (!NT_STATUS_IS_OK(status
)) {
977 #ifdef CTDB_CONTROL_SCHEDULE_FOR_DELETION
978 status
= db_ctdb_send_schedule_for_deletion(rec
);
984 static int db_ctdb_record_destr(struct db_record
* data
)
986 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
987 data
->private_data
, struct db_ctdb_rec
);
990 DEBUG(10, (DEBUGLEVEL
> 10
991 ? "Unlocking db %u key %s\n"
992 : "Unlocking db %u key %.20s\n",
993 (int)crec
->ctdb_ctx
->db_id
,
994 hex_encode_talloc(data
, (unsigned char *)data
->key
.dptr
,
997 tdb_chainunlock(crec
->ctdb_ctx
->wtdb
->tdb
, data
->key
);
999 threshold
= lp_ctdb_locktime_warn_threshold();
1000 if (threshold
!= 0) {
1001 double timediff
= timeval_elapsed(&crec
->lock_time
);
1002 if ((timediff
* 1000) > threshold
) {
1003 DEBUG(0, ("Held tdb lock %f seconds\n", timediff
));
1010 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
1011 TALLOC_CTX
*mem_ctx
,
1014 struct db_record
*result
;
1015 struct db_ctdb_rec
*crec
;
1018 int migrate_attempts
= 0;
1020 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
1021 DEBUG(0, ("talloc failed\n"));
1025 if (!(crec
= talloc_zero(result
, struct db_ctdb_rec
))) {
1026 DEBUG(0, ("talloc failed\n"));
1027 TALLOC_FREE(result
);
1031 result
->private_data
= (void *)crec
;
1032 crec
->ctdb_ctx
= ctx
;
1034 result
->key
.dsize
= key
.dsize
;
1035 result
->key
.dptr
= (uint8
*)talloc_memdup(result
, key
.dptr
, key
.dsize
);
1036 if (result
->key
.dptr
== NULL
) {
1037 DEBUG(0, ("talloc failed\n"));
1038 TALLOC_FREE(result
);
1043 * Do a blocking lock on the record
1047 if (DEBUGLEVEL
>= 10) {
1048 char *keystr
= hex_encode_talloc(result
, key
.dptr
, key
.dsize
);
1049 DEBUG(10, (DEBUGLEVEL
> 10
1050 ? "Locking db %u key %s\n"
1051 : "Locking db %u key %.20s\n",
1052 (int)crec
->ctdb_ctx
->db_id
, keystr
));
1053 TALLOC_FREE(keystr
);
1056 if (tdb_chainlock(ctx
->wtdb
->tdb
, key
) != 0) {
1057 DEBUG(3, ("tdb_chainlock failed\n"));
1058 TALLOC_FREE(result
);
1062 result
->store
= db_ctdb_store
;
1063 result
->delete_rec
= db_ctdb_delete
;
1064 talloc_set_destructor(result
, db_ctdb_record_destr
);
1066 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
1069 * See if we have a valid record and we are the dmaster. If so, we can
1070 * take the shortcut and just return it.
1073 if ((ctdb_data
.dptr
== NULL
) ||
1074 (ctdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
)) ||
1075 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
!= get_my_vnn()
1077 || (random() % 2 != 0)
1080 SAFE_FREE(ctdb_data
.dptr
);
1081 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
1082 talloc_set_destructor(result
, NULL
);
1084 migrate_attempts
+= 1;
1086 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
1087 ctdb_data
.dptr
, ctdb_data
.dptr
?
1088 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
: -1,
1091 status
= ctdbd_migrate(messaging_ctdbd_connection(), ctx
->db_id
,
1093 if (!NT_STATUS_IS_OK(status
)) {
1094 DEBUG(5, ("ctdb_migrate failed: %s\n",
1095 nt_errstr(status
)));
1096 TALLOC_FREE(result
);
1099 /* now its migrated, try again */
1103 if (migrate_attempts
> 10) {
1104 DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
1108 GetTimeOfDay(&crec
->lock_time
);
1110 memcpy(&crec
->header
, ctdb_data
.dptr
, sizeof(crec
->header
));
1112 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(crec
->header
);
1113 result
->value
.dptr
= NULL
;
1115 if ((result
->value
.dsize
!= 0)
1116 && !(result
->value
.dptr
= (uint8
*)talloc_memdup(
1117 result
, ctdb_data
.dptr
+ sizeof(crec
->header
),
1118 result
->value
.dsize
))) {
1119 DEBUG(0, ("talloc failed\n"));
1120 TALLOC_FREE(result
);
1123 SAFE_FREE(ctdb_data
.dptr
);
1128 static struct db_record
*db_ctdb_fetch_locked(struct db_context
*db
,
1129 TALLOC_CTX
*mem_ctx
,
1132 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1133 struct db_ctdb_ctx
);
1135 if (ctx
->transaction
!= NULL
) {
1136 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1139 if (db
->persistent
) {
1140 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1143 return fetch_locked_internal(ctx
, mem_ctx
, key
);
1147 fetch (unlocked, no migration) operation on ctdb
1149 static int db_ctdb_fetch(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
1150 TDB_DATA key
, TDB_DATA
*data
)
1152 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1153 struct db_ctdb_ctx
);
1157 if (ctx
->transaction
) {
1158 return db_ctdb_transaction_fetch(ctx
, mem_ctx
, key
, data
);
1161 if (db
->persistent
) {
1162 return db_ctdb_fetch_persistent(ctx
, mem_ctx
, key
, data
);
1165 /* try a direct fetch */
1166 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
1169 * See if we have a valid record and we are the dmaster. If so, we can
1170 * take the shortcut and just return it.
1171 * we bypass the dmaster check for persistent databases
1173 if ((ctdb_data
.dptr
!= NULL
) &&
1174 (ctdb_data
.dsize
>= sizeof(struct ctdb_ltdb_header
)) &&
1175 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
== get_my_vnn())
1177 /* we are the dmaster - avoid the ctdb protocol op */
1179 data
->dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
1180 if (data
->dsize
== 0) {
1181 SAFE_FREE(ctdb_data
.dptr
);
1186 data
->dptr
= (uint8
*)talloc_memdup(
1187 mem_ctx
, ctdb_data
.dptr
+sizeof(struct ctdb_ltdb_header
),
1190 SAFE_FREE(ctdb_data
.dptr
);
1192 if (data
->dptr
== NULL
) {
1198 SAFE_FREE(ctdb_data
.dptr
);
1200 /* we weren't able to get it locally - ask ctdb to fetch it for us */
1201 status
= ctdbd_fetch(messaging_ctdbd_connection(), ctx
->db_id
, key
,
1203 if (!NT_STATUS_IS_OK(status
)) {
1204 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status
)));
1211 struct traverse_state
{
1212 struct db_context
*db
;
1213 int (*fn
)(struct db_record
*rec
, void *private_data
);
1217 static void traverse_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1219 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1220 struct db_record
*rec
;
1221 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1222 /* we have to give them a locked record to prevent races */
1223 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, key
);
1224 if (rec
&& rec
->value
.dsize
> 0) {
1225 state
->fn(rec
, state
->private_data
);
1227 talloc_free(tmp_ctx
);
1230 static int traverse_persistent_callback(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1233 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1234 struct db_record
*rec
;
1235 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1237 /* we have to give them a locked record to prevent races */
1238 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, kbuf
);
1239 if (rec
&& rec
->value
.dsize
> 0) {
1240 ret
= state
->fn(rec
, state
->private_data
);
1242 talloc_free(tmp_ctx
);
1246 static int db_ctdb_traverse(struct db_context
*db
,
1247 int (*fn
)(struct db_record
*rec
,
1248 void *private_data
),
1251 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1252 struct db_ctdb_ctx
);
1253 struct traverse_state state
;
1257 state
.private_data
= private_data
;
1259 if (db
->persistent
) {
1260 /* for persistent databases we don't need to do a ctdb traverse,
1261 we can do a faster local traverse */
1262 return tdb_traverse(ctx
->wtdb
->tdb
, traverse_persistent_callback
, &state
);
1266 ctdbd_traverse(ctx
->db_id
, traverse_callback
, &state
);
1270 static NTSTATUS
db_ctdb_store_deny(struct db_record
*rec
, TDB_DATA data
, int flag
)
1272 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1275 static NTSTATUS
db_ctdb_delete_deny(struct db_record
*rec
)
1277 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1280 static void traverse_read_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1282 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1283 struct db_record rec
;
1286 rec
.store
= db_ctdb_store_deny
;
1287 rec
.delete_rec
= db_ctdb_delete_deny
;
1288 rec
.private_data
= state
->db
;
1289 state
->fn(&rec
, state
->private_data
);
1292 static int traverse_persistent_callback_read(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1295 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1296 struct db_record rec
;
1299 rec
.store
= db_ctdb_store_deny
;
1300 rec
.delete_rec
= db_ctdb_delete_deny
;
1301 rec
.private_data
= state
->db
;
1303 if (rec
.value
.dsize
<= sizeof(struct ctdb_ltdb_header
)) {
1304 /* a deleted record */
1307 rec
.value
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1308 rec
.value
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1310 return state
->fn(&rec
, state
->private_data
);
1313 static int db_ctdb_traverse_read(struct db_context
*db
,
1314 int (*fn
)(struct db_record
*rec
,
1315 void *private_data
),
1318 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1319 struct db_ctdb_ctx
);
1320 struct traverse_state state
;
1324 state
.private_data
= private_data
;
1326 if (db
->persistent
) {
1327 /* for persistent databases we don't need to do a ctdb traverse,
1328 we can do a faster local traverse */
1329 return tdb_traverse_read(ctx
->wtdb
->tdb
, traverse_persistent_callback_read
, &state
);
1332 ctdbd_traverse(ctx
->db_id
, traverse_read_callback
, &state
);
1336 static int db_ctdb_get_seqnum(struct db_context
*db
)
1338 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1339 struct db_ctdb_ctx
);
1340 return tdb_get_seqnum(ctx
->wtdb
->tdb
);
1343 static int db_ctdb_get_flags(struct db_context
*db
)
1345 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1346 struct db_ctdb_ctx
);
1347 return tdb_get_flags(ctx
->wtdb
->tdb
);
1350 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1352 int hash_size
, int tdb_flags
,
1353 int open_flags
, mode_t mode
)
1355 struct db_context
*result
;
1356 struct db_ctdb_ctx
*db_ctdb
;
1358 struct ctdbd_connection
*conn
;
1360 if (!lp_clustering()) {
1361 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1365 if (!(result
= talloc_zero(mem_ctx
, struct db_context
))) {
1366 DEBUG(0, ("talloc failed\n"));
1367 TALLOC_FREE(result
);
1371 if (!(db_ctdb
= talloc(result
, struct db_ctdb_ctx
))) {
1372 DEBUG(0, ("talloc failed\n"));
1373 TALLOC_FREE(result
);
1377 db_ctdb
->transaction
= NULL
;
1378 db_ctdb
->db
= result
;
1380 conn
= messaging_ctdbd_connection();
1382 DEBUG(1, ("Could not connect to ctdb\n"));
1383 TALLOC_FREE(result
);
1387 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn
, name
, &db_ctdb
->db_id
, tdb_flags
))) {
1388 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name
));
1389 TALLOC_FREE(result
);
1393 db_path
= ctdbd_dbpath(conn
, db_ctdb
, db_ctdb
->db_id
);
1395 result
->persistent
= ((tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0);
1397 /* only pass through specific flags */
1398 tdb_flags
&= TDB_SEQNUM
;
1400 /* honor permissions if user has specified O_CREAT */
1401 if (open_flags
& O_CREAT
) {
1402 chmod(db_path
, mode
);
1405 db_ctdb
->wtdb
= tdb_wrap_open(db_ctdb
, db_path
, hash_size
, tdb_flags
, O_RDWR
, 0);
1406 if (db_ctdb
->wtdb
== NULL
) {
1407 DEBUG(0, ("Could not open tdb %s: %s\n", db_path
, strerror(errno
)));
1408 TALLOC_FREE(result
);
1411 talloc_free(db_path
);
1413 if (result
->persistent
) {
1414 db_ctdb
->lock_ctx
= g_lock_ctx_init(db_ctdb
,
1415 ctdb_conn_msg_ctx(conn
));
1416 if (db_ctdb
->lock_ctx
== NULL
) {
1417 DEBUG(0, ("g_lock_ctx_init failed\n"));
1418 TALLOC_FREE(result
);
1423 result
->private_data
= (void *)db_ctdb
;
1424 result
->fetch_locked
= db_ctdb_fetch_locked
;
1425 result
->fetch
= db_ctdb_fetch
;
1426 result
->traverse
= db_ctdb_traverse
;
1427 result
->traverse_read
= db_ctdb_traverse_read
;
1428 result
->get_seqnum
= db_ctdb_get_seqnum
;
1429 result
->get_flags
= db_ctdb_get_flags
;
1430 result
->transaction_start
= db_ctdb_transaction_start
;
1431 result
->transaction_commit
= db_ctdb_transaction_commit
;
1432 result
->transaction_cancel
= db_ctdb_transaction_cancel
;
1434 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1435 name
, db_ctdb
->db_id
));