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
);
93 * fetch a record from the tdb, separating out the header
94 * information and returning the body of the record.
96 static NTSTATUS
db_ctdb_ltdb_fetch(struct db_ctdb_ctx
*db
,
98 struct ctdb_ltdb_header
*header
,
105 rec
= tdb_fetch_compat(db
->wtdb
->tdb
, key
);
106 if (rec
.dsize
< sizeof(struct ctdb_ltdb_header
)) {
107 status
= NT_STATUS_NOT_FOUND
;
112 header
->dmaster
= (uint32_t)-1;
119 *header
= *(struct ctdb_ltdb_header
*)rec
.dptr
;
123 data
->dsize
= rec
.dsize
- sizeof(struct ctdb_ltdb_header
);
124 if (data
->dsize
== 0) {
127 data
->dptr
= (unsigned char *)talloc_memdup(mem_ctx
,
129 + sizeof(struct ctdb_ltdb_header
),
131 if (data
->dptr
== NULL
) {
132 status
= NT_STATUS_NO_MEMORY
;
138 status
= NT_STATUS_OK
;
146 * Store a record together with the ctdb record header
147 * in the local copy of the database.
149 static NTSTATUS
db_ctdb_ltdb_store(struct db_ctdb_ctx
*db
,
151 struct ctdb_ltdb_header
*header
,
154 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
158 rec
.dsize
= data
.dsize
+ sizeof(struct ctdb_ltdb_header
);
159 rec
.dptr
= (uint8_t *)talloc_size(tmp_ctx
, rec
.dsize
);
161 if (rec
.dptr
== NULL
) {
162 talloc_free(tmp_ctx
);
163 return NT_STATUS_NO_MEMORY
;
166 memcpy(rec
.dptr
, header
, sizeof(struct ctdb_ltdb_header
));
167 memcpy(sizeof(struct ctdb_ltdb_header
) + (uint8_t *)rec
.dptr
, data
.dptr
, data
.dsize
);
169 ret
= tdb_store(db
->wtdb
->tdb
, key
, rec
, TDB_REPLACE
);
171 talloc_free(tmp_ctx
);
173 return (ret
== 0) ? NT_STATUS_OK
174 : tdb_error_to_ntstatus(db
->wtdb
->tdb
);
179 form a ctdb_rec_data record from a key/data pair
181 note that header may be NULL. If not NULL then it is included in the data portion
184 static struct ctdb_rec_data
*db_ctdb_marshall_record(TALLOC_CTX
*mem_ctx
, uint32_t reqid
,
186 struct ctdb_ltdb_header
*header
,
190 struct ctdb_rec_data
*d
;
192 length
= offsetof(struct ctdb_rec_data
, data
) + key
.dsize
+
193 data
.dsize
+ (header
?sizeof(*header
):0);
194 d
= (struct ctdb_rec_data
*)talloc_size(mem_ctx
, length
);
200 d
->keylen
= key
.dsize
;
201 memcpy(&d
->data
[0], key
.dptr
, key
.dsize
);
203 d
->datalen
= data
.dsize
+ sizeof(*header
);
204 memcpy(&d
->data
[key
.dsize
], header
, sizeof(*header
));
205 memcpy(&d
->data
[key
.dsize
+sizeof(*header
)], data
.dptr
, data
.dsize
);
207 d
->datalen
= data
.dsize
;
208 memcpy(&d
->data
[key
.dsize
], data
.dptr
, data
.dsize
);
214 /* helper function for marshalling multiple records */
215 static struct ctdb_marshall_buffer
*db_ctdb_marshall_add(TALLOC_CTX
*mem_ctx
,
216 struct ctdb_marshall_buffer
*m
,
220 struct ctdb_ltdb_header
*header
,
223 struct ctdb_rec_data
*r
;
224 size_t m_size
, r_size
;
225 struct ctdb_marshall_buffer
*m2
= NULL
;
227 r
= db_ctdb_marshall_record(talloc_tos(), reqid
, key
, header
, data
);
234 m
= (struct ctdb_marshall_buffer
*)talloc_zero_size(
235 mem_ctx
, offsetof(struct ctdb_marshall_buffer
, data
));
242 m_size
= talloc_get_size(m
);
243 r_size
= talloc_get_size(r
);
245 m2
= (struct ctdb_marshall_buffer
*)talloc_realloc_size(
246 mem_ctx
, m
, m_size
+ r_size
);
252 memcpy(m_size
+ (uint8_t *)m2
, r
, r_size
);
261 /* we've finished marshalling, return a data blob with the marshalled records */
262 static TDB_DATA
db_ctdb_marshall_finish(struct ctdb_marshall_buffer
*m
)
265 data
.dptr
= (uint8_t *)m
;
266 data
.dsize
= talloc_get_size(m
);
271 loop over a marshalling buffer
273 - pass r==NULL to start
274 - loop the number of times indicated by m->count
276 static struct ctdb_rec_data
*db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer
*m
, struct ctdb_rec_data
*r
,
278 struct ctdb_ltdb_header
*header
,
279 TDB_DATA
*key
, TDB_DATA
*data
)
282 r
= (struct ctdb_rec_data
*)&m
->data
[0];
284 r
= (struct ctdb_rec_data
*)(r
->length
+ (uint8_t *)r
);
292 key
->dptr
= &r
->data
[0];
293 key
->dsize
= r
->keylen
;
296 data
->dptr
= &r
->data
[r
->keylen
];
297 data
->dsize
= r
->datalen
;
298 if (header
!= NULL
) {
299 data
->dptr
+= sizeof(*header
);
300 data
->dsize
-= sizeof(*header
);
304 if (header
!= NULL
) {
305 if (r
->datalen
< sizeof(*header
)) {
308 *header
= *(struct ctdb_ltdb_header
*)&r
->data
[r
->keylen
];
315 * CTDB transaction destructor
317 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle
*h
)
321 status
= g_lock_unlock(h
->ctx
->lock_ctx
, h
->lock_name
);
322 if (!NT_STATUS_IS_OK(status
)) {
323 DEBUG(0, ("g_lock_unlock failed for %s: %s\n", h
->lock_name
,
331 * CTDB dbwrap API: transaction_start function
332 * starts a transaction on a persistent database
334 static int db_ctdb_transaction_start(struct db_context
*db
)
336 struct db_ctdb_transaction_handle
*h
;
338 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
341 if (!db
->persistent
) {
342 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
347 if (ctx
->transaction
) {
348 ctx
->transaction
->nesting
++;
349 DEBUG(5, (__location__
" transaction start on db 0x%08x: nesting %d -> %d\n",
350 ctx
->db_id
, ctx
->transaction
->nesting
- 1, ctx
->transaction
->nesting
));
354 h
= talloc_zero(db
, struct db_ctdb_transaction_handle
);
356 DEBUG(0,(__location__
" oom for transaction handle\n"));
362 h
->lock_name
= talloc_asprintf(h
, "transaction_db_0x%08x",
363 (unsigned int)ctx
->db_id
);
364 if (h
->lock_name
== NULL
) {
365 DEBUG(0, ("talloc_asprintf failed\n"));
371 * Wait a day, i.e. forever...
373 status
= g_lock_lock(ctx
->lock_ctx
, h
->lock_name
, G_LOCK_WRITE
,
374 timeval_set(86400, 0));
375 if (!NT_STATUS_IS_OK(status
)) {
376 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status
)));
381 talloc_set_destructor(h
, db_ctdb_transaction_destructor
);
383 ctx
->transaction
= h
;
385 DEBUG(5,(__location__
" transaction started on db 0x%08x\n", ctx
->db_id
));
390 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer
*buf
,
392 struct ctdb_ltdb_header
*pheader
,
396 struct ctdb_rec_data
*rec
= NULL
;
397 struct ctdb_ltdb_header h
;
410 * Walk the list of records written during this
411 * transaction. If we want to read one we have already
412 * written, return the last written sample. Thus we do not do
413 * a "break;" for the first hit, this record might have been
417 for (i
=0; i
<buf
->count
; i
++) {
418 TDB_DATA tkey
, tdata
;
420 struct ctdb_ltdb_header hdr
;
424 rec
= db_ctdb_marshall_loop_next(buf
, rec
, &reqid
, &hdr
, &tkey
,
430 if (tdb_data_equal(key
, tkey
)) {
442 data
.dptr
= (uint8_t *)talloc_memdup(mem_ctx
, data
.dptr
,
444 if ((data
.dsize
!= 0) && (data
.dptr
== NULL
)) {
450 if (pheader
!= NULL
) {
458 fetch a record inside a transaction
460 static NTSTATUS
db_ctdb_transaction_fetch(struct db_ctdb_ctx
*db
,
462 TDB_DATA key
, TDB_DATA
*data
)
464 struct db_ctdb_transaction_handle
*h
= db
->transaction
;
468 found
= pull_newest_from_marshall_buffer(h
->m_write
, key
, NULL
,
474 status
= db_ctdb_ltdb_fetch(h
->ctx
, key
, NULL
, mem_ctx
, data
);
476 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
484 * Fetch a record from a persistent database
485 * without record locking and without an active transaction.
487 * This just fetches from the local database copy.
488 * Since the databases are kept in syc cluster-wide,
489 * there is no point in doing a ctdb call to fetch the
490 * record from the lmaster. It does even harm since migration
491 * of records bump their RSN and hence render the persistent
492 * database inconsistent.
494 static NTSTATUS
db_ctdb_fetch_persistent(struct db_ctdb_ctx
*db
,
496 TDB_DATA key
, TDB_DATA
*data
)
500 status
= db_ctdb_ltdb_fetch(db
, key
, NULL
, mem_ctx
, data
);
502 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
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_t *)talloc_memdup(result
, key
.dptr
,
529 if (result
->key
.dptr
== NULL
) {
530 DEBUG(0, ("talloc failed\n"));
535 result
->store
= db_ctdb_store_transaction
;
536 result
->delete_rec
= db_ctdb_delete_transaction
;
538 if (pull_newest_from_marshall_buffer(ctx
->transaction
->m_write
, key
,
539 NULL
, result
, &result
->value
)) {
543 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
544 if (ctdb_data
.dptr
== NULL
) {
545 /* create the record */
546 result
->value
= tdb_null
;
550 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
551 result
->value
.dptr
= NULL
;
553 if ((result
->value
.dsize
!= 0)
554 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
555 result
, ctdb_data
.dptr
+ sizeof(struct ctdb_ltdb_header
),
556 result
->value
.dsize
))) {
557 DEBUG(0, ("talloc failed\n"));
561 SAFE_FREE(ctdb_data
.dptr
);
566 static int db_ctdb_record_destructor(struct db_record
**recp
)
568 struct db_record
*rec
= talloc_get_type_abort(*recp
, struct db_record
);
569 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
570 rec
->private_data
, struct db_ctdb_transaction_handle
);
571 int ret
= h
->ctx
->db
->transaction_commit(h
->ctx
->db
);
573 DEBUG(0,(__location__
" transaction_commit failed\n"));
579 auto-create a transaction for persistent databases
581 static struct db_record
*db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx
*ctx
,
586 struct db_record
*rec
, **recp
;
588 res
= db_ctdb_transaction_start(ctx
->db
);
593 rec
= db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
595 ctx
->db
->transaction_cancel(ctx
->db
);
599 /* destroy this transaction when we release the lock */
600 recp
= talloc(rec
, struct db_record
*);
602 ctx
->db
->transaction_cancel(ctx
->db
);
607 talloc_set_destructor(recp
, db_ctdb_record_destructor
);
613 stores a record inside a transaction
615 static NTSTATUS
db_ctdb_transaction_store(struct db_ctdb_transaction_handle
*h
,
616 TDB_DATA key
, TDB_DATA data
)
618 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
620 struct ctdb_ltdb_header header
;
624 /* we need the header so we can update the RSN */
626 if (!pull_newest_from_marshall_buffer(h
->m_write
, key
, &header
,
629 rec
= tdb_fetch_compat(h
->ctx
->wtdb
->tdb
, key
);
631 if (rec
.dptr
!= NULL
) {
632 memcpy(&header
, rec
.dptr
,
633 sizeof(struct ctdb_ltdb_header
));
634 rec
.dsize
-= sizeof(struct ctdb_ltdb_header
);
637 * a special case, we are writing the same
638 * data that is there now
640 if (data
.dsize
== rec
.dsize
&&
642 rec
.dptr
+ sizeof(struct ctdb_ltdb_header
),
645 talloc_free(tmp_ctx
);
652 header
.dmaster
= get_my_vnn();
655 h
->m_write
= db_ctdb_marshall_add(h
, h
->m_write
, h
->ctx
->db_id
, 0, key
, &header
, data
);
656 if (h
->m_write
== NULL
) {
657 DEBUG(0,(__location__
" Failed to add to marshalling record\n"));
658 talloc_free(tmp_ctx
);
659 return NT_STATUS_NO_MEMORY
;
662 talloc_free(tmp_ctx
);
668 a record store inside a transaction
670 static NTSTATUS
db_ctdb_store_transaction(struct db_record
*rec
, TDB_DATA data
, int flag
)
672 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
673 rec
->private_data
, struct db_ctdb_transaction_handle
);
676 status
= db_ctdb_transaction_store(h
, rec
->key
, data
);
681 a record delete inside a transaction
683 static NTSTATUS
db_ctdb_delete_transaction(struct db_record
*rec
)
685 struct db_ctdb_transaction_handle
*h
= talloc_get_type_abort(
686 rec
->private_data
, struct db_ctdb_transaction_handle
);
689 status
= db_ctdb_transaction_store(h
, rec
->key
, tdb_null
);
694 * Fetch the db sequence number of a persistent db directly from the db.
696 static NTSTATUS
db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx
*db
,
700 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
703 struct ctdb_ltdb_header header
;
704 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
706 if (seqnum
== NULL
) {
707 return NT_STATUS_INVALID_PARAMETER
;
710 key
= string_term_tdb_data(keyname
);
712 status
= db_ctdb_ltdb_fetch(db
, key
, &header
, mem_ctx
, &data
);
713 if (!NT_STATUS_IS_OK(status
) &&
714 !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
))
719 status
= NT_STATUS_OK
;
721 if (data
.dsize
!= sizeof(uint64_t)) {
726 *seqnum
= *(uint64_t *)data
.dptr
;
729 TALLOC_FREE(mem_ctx
);
734 * Store the database sequence number inside a transaction.
736 static NTSTATUS
db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle
*h
,
740 const char *keyname
= CTDB_DB_SEQNUM_KEY
;
744 key
= string_term_tdb_data(keyname
);
746 data
.dptr
= (uint8_t *)&seqnum
;
747 data
.dsize
= sizeof(uint64_t);
749 status
= db_ctdb_transaction_store(h
, key
, data
);
757 static int db_ctdb_transaction_commit(struct db_context
*db
)
759 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
763 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
764 uint64_t old_seqnum
, new_seqnum
;
768 DEBUG(0,(__location__
" transaction commit with no open transaction on db 0x%08x\n", ctx
->db_id
));
772 if (h
->nested_cancel
) {
773 db
->transaction_cancel(db
);
774 DEBUG(5,(__location__
" Failed transaction commit after nested cancel\n"));
778 if (h
->nesting
!= 0) {
780 DEBUG(5, (__location__
" transaction commit on db 0x%08x: nesting %d -> %d\n",
781 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
785 if (h
->m_write
== NULL
) {
787 * No changes were made, so don't change the seqnum,
788 * don't push to other node, just exit with success.
794 DEBUG(5,(__location__
" transaction commit on db 0x%08x\n", ctx
->db_id
));
797 * As the last db action before committing, bump the database sequence
798 * number. Note that this undoes all changes to the seqnum records
799 * performed under the transaction. This record is not meant to be
800 * modified by user interaction. It is for internal use only...
802 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &old_seqnum
);
803 if (!NT_STATUS_IS_OK(rets
)) {
804 DEBUG(1, (__location__
" failed to fetch the db sequence number "
805 "in transaction commit on db 0x%08x\n", ctx
->db_id
));
810 new_seqnum
= old_seqnum
+ 1;
812 rets
= db_ctdb_store_db_seqnum(h
, new_seqnum
);
813 if (!NT_STATUS_IS_OK(rets
)) {
814 DEBUG(1, (__location__
"failed to store the db sequence number "
815 " in transaction commit on db 0x%08x\n", ctx
->db_id
));
821 /* tell ctdbd to commit to the other nodes */
822 rets
= ctdbd_control_local(messaging_ctdbd_connection(),
823 CTDB_CONTROL_TRANS3_COMMIT
,
825 db_ctdb_marshall_finish(h
->m_write
),
826 NULL
, NULL
, &status
);
827 if (!NT_STATUS_IS_OK(rets
) || status
!= 0) {
829 * The TRANS3_COMMIT control should only possibly fail when a
830 * recovery has been running concurrently. In any case, the db
831 * will be the same on all nodes, either the new copy or the
832 * old copy. This can be detected by comparing the old and new
833 * local sequence numbers.
835 rets
= db_ctdb_fetch_db_seqnum_from_db(ctx
, &new_seqnum
);
836 if (!NT_STATUS_IS_OK(rets
)) {
837 DEBUG(1, (__location__
" failed to refetch db sequence "
838 "number after failed TRANS3_COMMIT\n"));
843 if (new_seqnum
== old_seqnum
) {
844 /* Recovery prevented all our changes: retry. */
846 } else if (new_seqnum
!= (old_seqnum
+ 1)) {
847 DEBUG(0, (__location__
" ERROR: new_seqnum[%lu] != "
848 "old_seqnum[%lu] + (0 or 1) after failed "
849 "TRANS3_COMMIT - this should not happen!\n",
850 (unsigned long)new_seqnum
,
851 (unsigned long)old_seqnum
));
856 * Recovery propagated our changes to all nodes, completing
857 * our commit for us - succeed.
864 h
->ctx
->transaction
= NULL
;
873 static int db_ctdb_transaction_cancel(struct db_context
*db
)
875 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
877 struct db_ctdb_transaction_handle
*h
= ctx
->transaction
;
880 DEBUG(0,(__location__
" transaction cancel with no open transaction on db 0x%08x\n", ctx
->db_id
));
884 if (h
->nesting
!= 0) {
886 h
->nested_cancel
= true;
887 DEBUG(5, (__location__
" transaction cancel on db 0x%08x: nesting %d -> %d\n",
888 ctx
->db_id
, ctx
->transaction
->nesting
+ 1, ctx
->transaction
->nesting
));
892 DEBUG(5,(__location__
" Cancel transaction on db 0x%08x\n", ctx
->db_id
));
894 ctx
->transaction
= NULL
;
900 static NTSTATUS
db_ctdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
)
902 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
903 rec
->private_data
, struct db_ctdb_rec
);
905 return db_ctdb_ltdb_store(crec
->ctdb_ctx
, rec
->key
, &(crec
->header
), data
);
910 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
911 static NTSTATUS
db_ctdb_send_schedule_for_deletion(struct db_record
*rec
)
914 struct ctdb_control_schedule_for_deletion
*dd
;
917 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
918 rec
->private_data
, struct db_ctdb_rec
);
920 indata
.dsize
= offsetof(struct ctdb_control_schedule_for_deletion
, key
) + rec
->key
.dsize
;
921 indata
.dptr
= talloc_zero_array(crec
, uint8_t, indata
.dsize
);
922 if (indata
.dptr
== NULL
) {
923 DEBUG(0, (__location__
" talloc failed!\n"));
924 return NT_STATUS_NO_MEMORY
;
927 dd
= (struct ctdb_control_schedule_for_deletion
*)(void *)indata
.dptr
;
928 dd
->db_id
= crec
->ctdb_ctx
->db_id
;
929 dd
->hdr
= crec
->header
;
930 dd
->keylen
= rec
->key
.dsize
;
931 memcpy(dd
->key
, rec
->key
.dptr
, rec
->key
.dsize
);
933 status
= ctdbd_control_local(messaging_ctdbd_connection(),
934 CTDB_CONTROL_SCHEDULE_FOR_DELETION
,
935 crec
->ctdb_ctx
->db_id
,
936 CTDB_CTRL_FLAG_NOREPLY
, /* flags */
941 talloc_free(indata
.dptr
);
943 if (!NT_STATUS_IS_OK(status
) || cstatus
!= 0) {
944 DEBUG(1, (__location__
" Error sending local control "
945 "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
946 nt_errstr(status
), cstatus
));
947 if (NT_STATUS_IS_OK(status
)) {
948 status
= NT_STATUS_UNSUCCESSFUL
;
956 static NTSTATUS
db_ctdb_delete(struct db_record
*rec
)
962 * We have to store the header with empty data. TODO: Fix the
968 status
= db_ctdb_store(rec
, data
, 0);
969 if (!NT_STATUS_IS_OK(status
)) {
973 #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
974 status
= db_ctdb_send_schedule_for_deletion(rec
);
980 static int db_ctdb_record_destr(struct db_record
* data
)
982 struct db_ctdb_rec
*crec
= talloc_get_type_abort(
983 data
->private_data
, struct db_ctdb_rec
);
986 DEBUG(10, (DEBUGLEVEL
> 10
987 ? "Unlocking db %u key %s\n"
988 : "Unlocking db %u key %.20s\n",
989 (int)crec
->ctdb_ctx
->db_id
,
990 hex_encode_talloc(data
, (unsigned char *)data
->key
.dptr
,
993 tdb_chainunlock(crec
->ctdb_ctx
->wtdb
->tdb
, data
->key
);
995 threshold
= lp_ctdb_locktime_warn_threshold();
996 if (threshold
!= 0) {
997 double timediff
= timeval_elapsed(&crec
->lock_time
);
998 if ((timediff
* 1000) > threshold
) {
999 DEBUG(0, ("Held tdb lock %f seconds\n", timediff
));
1006 /* Do I own this record? */
1007 static bool db_ctdb_can_use_local_copy(TDB_DATA ctdb_data
, bool read_only
)
1009 struct ctdb_ltdb_header
*hdr
;
1011 if (ctdb_data
.dptr
== NULL
)
1014 if (ctdb_data
.dsize
< sizeof(struct ctdb_ltdb_header
))
1017 hdr
= (struct ctdb_ltdb_header
*)ctdb_data
.dptr
;
1019 #ifdef HAVE_CTDB_WANT_READONLY_DECL
1020 if (hdr
->dmaster
!= get_my_vnn()) {
1021 /* If we're not dmaster, it must be r/o copy. */
1022 return read_only
&& (hdr
->flags
& CTDB_REC_RO_HAVE_READONLY
);
1025 /* If we want write access, noone can have r/o copies. */
1026 return read_only
|| !(hdr
->flags
& CTDB_REC_RO_HAVE_DELEGATIONS
);
1028 return (hdr
->dmaster
== get_my_vnn());
1032 static struct db_record
*fetch_locked_internal(struct db_ctdb_ctx
*ctx
,
1033 TALLOC_CTX
*mem_ctx
,
1037 struct db_record
*result
;
1038 struct db_ctdb_rec
*crec
;
1041 int migrate_attempts
= 0;
1044 if (!(result
= talloc(mem_ctx
, struct db_record
))) {
1045 DEBUG(0, ("talloc failed\n"));
1049 if (!(crec
= talloc_zero(result
, struct db_ctdb_rec
))) {
1050 DEBUG(0, ("talloc failed\n"));
1051 TALLOC_FREE(result
);
1055 result
->private_data
= (void *)crec
;
1056 crec
->ctdb_ctx
= ctx
;
1058 result
->key
.dsize
= key
.dsize
;
1059 result
->key
.dptr
= (uint8_t *)talloc_memdup(result
, key
.dptr
,
1061 if (result
->key
.dptr
== NULL
) {
1062 DEBUG(0, ("talloc failed\n"));
1063 TALLOC_FREE(result
);
1068 * Do a blocking lock on the record
1072 if (DEBUGLEVEL
>= 10) {
1073 char *keystr
= hex_encode_talloc(result
, key
.dptr
, key
.dsize
);
1074 DEBUG(10, (DEBUGLEVEL
> 10
1075 ? "Locking db %u key %s\n"
1076 : "Locking db %u key %.20s\n",
1077 (int)crec
->ctdb_ctx
->db_id
, keystr
));
1078 TALLOC_FREE(keystr
);
1082 ? tdb_chainlock_nonblock(ctx
->wtdb
->tdb
, key
)
1083 : tdb_chainlock(ctx
->wtdb
->tdb
, key
);
1085 DEBUG(3, ("tdb_chainlock failed\n"));
1086 TALLOC_FREE(result
);
1090 result
->store
= db_ctdb_store
;
1091 result
->delete_rec
= db_ctdb_delete
;
1092 talloc_set_destructor(result
, db_ctdb_record_destr
);
1094 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
1097 * See if we have a valid record and we are the dmaster. If so, we can
1098 * take the shortcut and just return it.
1101 if (!db_ctdb_can_use_local_copy(ctdb_data
, false)) {
1102 SAFE_FREE(ctdb_data
.dptr
);
1103 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
1104 talloc_set_destructor(result
, NULL
);
1106 if (tryonly
&& (migrate_attempts
!= 0)) {
1107 DEBUG(5, ("record migrated away again\n"));
1108 TALLOC_FREE(result
);
1112 migrate_attempts
+= 1;
1114 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u) %u\n",
1115 ctdb_data
.dptr
, ctdb_data
.dptr
?
1116 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->dmaster
: -1,
1119 ((struct ctdb_ltdb_header
*)ctdb_data
.dptr
)->flags
: 0));
1121 status
= ctdbd_migrate(messaging_ctdbd_connection(), ctx
->db_id
,
1123 if (!NT_STATUS_IS_OK(status
)) {
1124 DEBUG(5, ("ctdb_migrate failed: %s\n",
1125 nt_errstr(status
)));
1126 TALLOC_FREE(result
);
1129 /* now its migrated, try again */
1133 if (migrate_attempts
> 10) {
1134 DEBUG(0, ("db_ctdb_fetch_locked for %s key %s needed %d "
1135 "attempts\n", tdb_name(ctx
->wtdb
->tdb
),
1136 hex_encode_talloc(talloc_tos(),
1137 (unsigned char *)key
.dptr
,
1142 GetTimeOfDay(&crec
->lock_time
);
1144 memcpy(&crec
->header
, ctdb_data
.dptr
, sizeof(crec
->header
));
1146 result
->value
.dsize
= ctdb_data
.dsize
- sizeof(crec
->header
);
1147 result
->value
.dptr
= NULL
;
1149 if ((result
->value
.dsize
!= 0)
1150 && !(result
->value
.dptr
= (uint8_t *)talloc_memdup(
1151 result
, ctdb_data
.dptr
+ sizeof(crec
->header
),
1152 result
->value
.dsize
))) {
1153 DEBUG(0, ("talloc failed\n"));
1154 TALLOC_FREE(result
);
1157 SAFE_FREE(ctdb_data
.dptr
);
1162 static struct db_record
*db_ctdb_fetch_locked(struct db_context
*db
,
1163 TALLOC_CTX
*mem_ctx
,
1166 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1167 struct db_ctdb_ctx
);
1169 if (ctx
->transaction
!= NULL
) {
1170 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1173 if (db
->persistent
) {
1174 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1177 return fetch_locked_internal(ctx
, mem_ctx
, key
, false);
1180 static struct db_record
*db_ctdb_try_fetch_locked(struct db_context
*db
,
1181 TALLOC_CTX
*mem_ctx
,
1184 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1185 struct db_ctdb_ctx
);
1187 if (ctx
->transaction
!= NULL
) {
1188 return db_ctdb_fetch_locked_transaction(ctx
, mem_ctx
, key
);
1191 if (db
->persistent
) {
1192 return db_ctdb_fetch_locked_persistent(ctx
, mem_ctx
, key
);
1195 return fetch_locked_internal(ctx
, mem_ctx
, key
, true);
1199 fetch (unlocked, no migration) operation on ctdb
1201 static NTSTATUS
db_ctdb_fetch(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
1202 TDB_DATA key
, TDB_DATA
*data
)
1204 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1205 struct db_ctdb_ctx
);
1209 if (ctx
->transaction
) {
1210 return db_ctdb_transaction_fetch(ctx
, mem_ctx
, key
, data
);
1213 if (db
->persistent
) {
1214 return db_ctdb_fetch_persistent(ctx
, mem_ctx
, key
, data
);
1217 /* try a direct fetch */
1218 ctdb_data
= tdb_fetch_compat(ctx
->wtdb
->tdb
, key
);
1221 * See if we have a valid record and we are the dmaster. If so, we can
1222 * take the shortcut and just return it.
1223 * we bypass the dmaster check for persistent databases
1225 if (db_ctdb_can_use_local_copy(ctdb_data
, true)) {
1226 /* we are the dmaster - avoid the ctdb protocol op */
1228 data
->dsize
= ctdb_data
.dsize
- sizeof(struct ctdb_ltdb_header
);
1230 data
->dptr
= (uint8_t *)talloc_memdup(
1231 mem_ctx
, ctdb_data
.dptr
+sizeof(struct ctdb_ltdb_header
),
1234 SAFE_FREE(ctdb_data
.dptr
);
1236 if (data
->dptr
== NULL
) {
1237 return NT_STATUS_NO_MEMORY
;
1239 return NT_STATUS_OK
;
1242 SAFE_FREE(ctdb_data
.dptr
);
1245 * We weren't able to get it locally - ask ctdb to fetch it for us.
1246 * If we already had *something*, it's probably worth making a local
1249 status
= ctdbd_fetch(messaging_ctdbd_connection(), ctx
->db_id
, key
,
1251 ctdb_data
.dsize
>= sizeof(struct ctdb_ltdb_header
));
1252 if (!NT_STATUS_IS_OK(status
)) {
1253 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status
)));
1259 static NTSTATUS
db_ctdb_parse_record(struct db_context
*db
, TDB_DATA key
,
1260 void (*parser
)(TDB_DATA key
,
1262 void *private_data
),
1268 status
= db_ctdb_fetch(db
, talloc_tos(), key
, &data
);
1269 if (!NT_STATUS_IS_OK(status
)) {
1272 parser(key
, data
, private_data
);
1273 TALLOC_FREE(data
.dptr
);
1274 return NT_STATUS_OK
;
1277 struct traverse_state
{
1278 struct db_context
*db
;
1279 int (*fn
)(struct db_record
*rec
, void *private_data
);
1284 static void traverse_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1286 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1287 struct db_record
*rec
;
1288 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1289 /* we have to give them a locked record to prevent races */
1290 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, key
);
1291 if (rec
&& rec
->value
.dsize
> 0) {
1292 state
->fn(rec
, state
->private_data
);
1294 talloc_free(tmp_ctx
);
1297 static int traverse_persistent_callback(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1300 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1301 struct db_record
*rec
;
1302 TALLOC_CTX
*tmp_ctx
= talloc_new(state
->db
);
1306 * Skip the __db_sequence_number__ key:
1307 * This is used for persistent transactions internally.
1309 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1310 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1315 /* we have to give them a locked record to prevent races */
1316 rec
= db_ctdb_fetch_locked(state
->db
, tmp_ctx
, kbuf
);
1317 if (rec
&& rec
->value
.dsize
> 0) {
1318 ret
= state
->fn(rec
, state
->private_data
);
1322 talloc_free(tmp_ctx
);
1326 /* wrapper to use traverse_persistent_callback with dbwrap */
1327 static int traverse_persistent_callback_dbwrap(struct db_record
*rec
, void* data
)
1329 return traverse_persistent_callback(NULL
, rec
->key
, rec
->value
, data
);
1333 static int db_ctdb_traverse(struct db_context
*db
,
1334 int (*fn
)(struct db_record
*rec
,
1335 void *private_data
),
1339 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1340 struct db_ctdb_ctx
);
1341 struct traverse_state state
;
1345 state
.private_data
= private_data
;
1348 if (db
->persistent
) {
1349 struct tdb_context
*ltdb
= ctx
->wtdb
->tdb
;
1352 /* for persistent databases we don't need to do a ctdb traverse,
1353 we can do a faster local traverse */
1354 ret
= tdb_traverse(ltdb
, traverse_persistent_callback
, &state
);
1358 if (ctx
->transaction
&& ctx
->transaction
->m_write
) {
1360 * we now have to handle keys not yet
1361 * present at transaction start
1363 struct db_context
*newkeys
= db_open_rbt(talloc_tos());
1364 struct ctdb_marshall_buffer
*mbuf
= ctx
->transaction
->m_write
;
1365 struct ctdb_rec_data
*rec
=NULL
;
1369 if (newkeys
== NULL
) {
1373 for (i
=0; i
<mbuf
->count
; i
++) {
1375 rec
=db_ctdb_marshall_loop_next(mbuf
, rec
,
1378 SMB_ASSERT(rec
!= NULL
);
1380 if (!tdb_exists(ltdb
, key
)) {
1381 dbwrap_store(newkeys
, key
, tdb_null
, 0);
1384 status
= dbwrap_traverse(newkeys
,
1385 traverse_persistent_callback_dbwrap
,
1388 talloc_free(newkeys
);
1389 if (!NT_STATUS_IS_OK(status
)) {
1397 status
= ctdbd_traverse(ctx
->db_id
, traverse_callback
, &state
);
1398 if (!NT_STATUS_IS_OK(status
)) {
1404 static NTSTATUS
db_ctdb_store_deny(struct db_record
*rec
, TDB_DATA data
, int flag
)
1406 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1409 static NTSTATUS
db_ctdb_delete_deny(struct db_record
*rec
)
1411 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1414 static void traverse_read_callback(TDB_DATA key
, TDB_DATA data
, void *private_data
)
1416 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1417 struct db_record rec
;
1420 rec
.store
= db_ctdb_store_deny
;
1421 rec
.delete_rec
= db_ctdb_delete_deny
;
1422 rec
.private_data
= state
->db
;
1423 state
->fn(&rec
, state
->private_data
);
1427 static int traverse_persistent_callback_read(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
1430 struct traverse_state
*state
= (struct traverse_state
*)private_data
;
1431 struct db_record rec
;
1434 * Skip the __db_sequence_number__ key:
1435 * This is used for persistent transactions internally.
1437 if (kbuf
.dsize
== strlen(CTDB_DB_SEQNUM_KEY
) + 1 &&
1438 strcmp((const char*)kbuf
.dptr
, CTDB_DB_SEQNUM_KEY
) == 0)
1445 rec
.store
= db_ctdb_store_deny
;
1446 rec
.delete_rec
= db_ctdb_delete_deny
;
1447 rec
.private_data
= state
->db
;
1449 if (rec
.value
.dsize
<= sizeof(struct ctdb_ltdb_header
)) {
1450 /* a deleted record */
1453 rec
.value
.dsize
-= sizeof(struct ctdb_ltdb_header
);
1454 rec
.value
.dptr
+= sizeof(struct ctdb_ltdb_header
);
1457 return state
->fn(&rec
, state
->private_data
);
1460 static int db_ctdb_traverse_read(struct db_context
*db
,
1461 int (*fn
)(struct db_record
*rec
,
1462 void *private_data
),
1466 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1467 struct db_ctdb_ctx
);
1468 struct traverse_state state
;
1472 state
.private_data
= private_data
;
1475 if (db
->persistent
) {
1476 /* for persistent databases we don't need to do a ctdb traverse,
1477 we can do a faster local traverse */
1478 return tdb_traverse_read(ctx
->wtdb
->tdb
, traverse_persistent_callback_read
, &state
);
1481 status
= ctdbd_traverse(ctx
->db_id
, traverse_read_callback
, &state
);
1482 if (!NT_STATUS_IS_OK(status
)) {
1488 static int db_ctdb_get_seqnum(struct db_context
*db
)
1490 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
1491 struct db_ctdb_ctx
);
1492 return tdb_get_seqnum(ctx
->wtdb
->tdb
);
1495 static void db_ctdb_id(struct db_context
*db
, const uint8_t **id
,
1498 struct db_ctdb_ctx
*ctx
= talloc_get_type_abort(
1499 db
->private_data
, struct db_ctdb_ctx
);
1501 *id
= (uint8_t *)&ctx
->db_id
;
1502 *idlen
= sizeof(ctx
->db_id
);
1505 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1507 int hash_size
, int tdb_flags
,
1508 int open_flags
, mode_t mode
,
1509 enum dbwrap_lock_order lock_order
)
1511 struct db_context
*result
;
1512 struct db_ctdb_ctx
*db_ctdb
;
1514 struct ctdbd_connection
*conn
;
1515 struct loadparm_context
*lp_ctx
;
1516 struct ctdb_db_priority prio
;
1520 if (!lp_clustering()) {
1521 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1525 if (!(result
= talloc_zero(mem_ctx
, struct db_context
))) {
1526 DEBUG(0, ("talloc failed\n"));
1527 TALLOC_FREE(result
);
1531 if (!(db_ctdb
= talloc(result
, struct db_ctdb_ctx
))) {
1532 DEBUG(0, ("talloc failed\n"));
1533 TALLOC_FREE(result
);
1537 db_ctdb
->transaction
= NULL
;
1538 db_ctdb
->db
= result
;
1540 conn
= messaging_ctdbd_connection();
1542 DEBUG(1, ("Could not connect to ctdb\n"));
1543 TALLOC_FREE(result
);
1547 if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn
, name
, &db_ctdb
->db_id
, tdb_flags
))) {
1548 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name
));
1549 TALLOC_FREE(result
);
1553 db_path
= ctdbd_dbpath(conn
, db_ctdb
, db_ctdb
->db_id
);
1555 result
->persistent
= ((tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0);
1556 result
->lock_order
= lock_order
;
1558 /* only pass through specific flags */
1559 tdb_flags
&= TDB_SEQNUM
;
1561 /* honor permissions if user has specified O_CREAT */
1562 if (open_flags
& O_CREAT
) {
1563 chmod(db_path
, mode
);
1566 prio
.db_id
= db_ctdb
->db_id
;
1567 prio
.priority
= lock_order
;
1569 status
= ctdbd_control_local(
1570 conn
, CTDB_CONTROL_SET_DB_PRIORITY
, 0, 0,
1571 make_tdb_data((uint8_t *)&prio
, sizeof(prio
)),
1572 NULL
, NULL
, &cstatus
);
1574 if (!NT_STATUS_IS_OK(status
) || (cstatus
!= 0)) {
1575 DEBUG(1, ("CTDB_CONTROL_SET_DB_PRIORITY failed: %s, %d\n",
1576 nt_errstr(status
), cstatus
));
1577 TALLOC_FREE(result
);
1581 lp_ctx
= loadparm_init_s3(db_path
, loadparm_s3_helpers());
1583 db_ctdb
->wtdb
= tdb_wrap_open(db_ctdb
, db_path
, hash_size
, tdb_flags
,
1585 talloc_unlink(db_path
, lp_ctx
);
1586 if (db_ctdb
->wtdb
== NULL
) {
1587 DEBUG(0, ("Could not open tdb %s: %s\n", db_path
, strerror(errno
)));
1588 TALLOC_FREE(result
);
1591 talloc_free(db_path
);
1593 if (result
->persistent
) {
1594 db_ctdb
->lock_ctx
= g_lock_ctx_init(db_ctdb
,
1595 ctdb_conn_msg_ctx(conn
));
1596 if (db_ctdb
->lock_ctx
== NULL
) {
1597 DEBUG(0, ("g_lock_ctx_init failed\n"));
1598 TALLOC_FREE(result
);
1603 result
->private_data
= (void *)db_ctdb
;
1604 result
->fetch_locked
= db_ctdb_fetch_locked
;
1605 result
->try_fetch_locked
= db_ctdb_try_fetch_locked
;
1606 result
->parse_record
= db_ctdb_parse_record
;
1607 result
->traverse
= db_ctdb_traverse
;
1608 result
->traverse_read
= db_ctdb_traverse_read
;
1609 result
->get_seqnum
= db_ctdb_get_seqnum
;
1610 result
->transaction_start
= db_ctdb_transaction_start
;
1611 result
->transaction_commit
= db_ctdb_transaction_commit
;
1612 result
->transaction_cancel
= db_ctdb_transaction_cancel
;
1613 result
->id
= db_ctdb_id
;
1614 result
->stored_callback
= NULL
;
1616 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1617 name
, db_ctdb
->db_id
));
1622 #else /* CLUSTER_SUPPORT */
1624 struct db_context
*db_open_ctdb(TALLOC_CTX
*mem_ctx
,
1626 int hash_size
, int tdb_flags
,
1627 int open_flags
, mode_t mode
,
1628 enum dbwrap_lock_order lock_order
)
1630 DEBUG(3, ("db_open_ctdb: no cluster support!\n"));