s3:dbwrap_ctdb: send the SCHEDULE_FOR_DELETION control with the NOREPLY flag.
[obnox/samba-ctdb.git] / source3 / lib / dbwrap_ctdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Database interface wrapper around ctdbd
4    Copyright (C) Volker Lendecke 2007-2009
5    Copyright (C) Michael Adam 2009
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #ifdef CLUSTER_SUPPORT
23 #include "ctdb.h"
24 #include "ctdb_private.h"
25 #include "ctdbd_conn.h"
26 #include "g_lock.h"
27
28 struct db_ctdb_transaction_handle {
29         struct db_ctdb_ctx *ctx;
30         /*
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)
34          */
35         struct ctdb_marshall_buffer *m_all;
36         struct ctdb_marshall_buffer *m_write;
37         uint32_t nesting;
38         bool nested_cancel;
39         char *lock_name;
40 };
41
42 struct db_ctdb_ctx {
43         struct db_context *db;
44         struct tdb_wrap *wtdb;
45         uint32 db_id;
46         struct db_ctdb_transaction_handle *transaction;
47         struct g_lock_ctx *lock_ctx;
48 };
49
50 struct db_ctdb_rec {
51         struct db_ctdb_ctx *ctdb_ctx;
52         struct ctdb_ltdb_header header;
53         struct timeval lock_time;
54 };
55
56 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
57                                                TALLOC_CTX *mem_ctx,
58                                                TDB_DATA key,
59                                                bool persistent);
60
61 static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
62 {
63         NTSTATUS status;
64         enum TDB_ERROR tret = tdb_error(tdb);
65
66         switch (tret) {
67         case TDB_ERR_EXISTS:
68                 status = NT_STATUS_OBJECT_NAME_COLLISION;
69                 break;
70         case TDB_ERR_NOEXIST:
71                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
72                 break;
73         default:
74                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
75                 break;
76         }
77
78         return status;
79 }
80
81
82 /**
83  * fetch a record from the tdb, separating out the header
84  * information and returning the body of the record.
85  */
86 static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
87                                    TDB_DATA key,
88                                    struct ctdb_ltdb_header *header,
89                                    TALLOC_CTX *mem_ctx,
90                                    TDB_DATA *data)
91 {
92         TDB_DATA rec;
93         NTSTATUS status;
94
95         rec = tdb_fetch(db->wtdb->tdb, key);
96         if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
97                 status = NT_STATUS_NOT_FOUND;
98                 if (data) {
99                         ZERO_STRUCTP(data);
100                 }
101                 if (header) {
102                         header->dmaster = (uint32_t)-1;
103                         header->rsn = 0;
104                 }
105                 goto done;
106         }
107
108         if (header) {
109                 *header = *(struct ctdb_ltdb_header *)rec.dptr;
110         }
111
112         if (data) {
113                 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
114                 if (data->dsize == 0) {
115                         data->dptr = NULL;
116                 } else {
117                         data->dptr = (unsigned char *)talloc_memdup(mem_ctx,
118                                         rec.dptr
119                                          + sizeof(struct ctdb_ltdb_header),
120                                         data->dsize);
121                         if (data->dptr == NULL) {
122                                 status = NT_STATUS_NO_MEMORY;
123                                 goto done;
124                         }
125                 }
126         }
127
128         status = NT_STATUS_OK;
129
130 done:
131         SAFE_FREE(rec.dptr);
132         return status;
133 }
134
135 /*
136  * Store a record together with the ctdb record header
137  * in the local copy of the database.
138  */
139 static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
140                                    TDB_DATA key,
141                                    struct ctdb_ltdb_header *header,
142                                    TDB_DATA data)
143 {
144         TALLOC_CTX *tmp_ctx = talloc_stackframe();
145         TDB_DATA rec;
146         int ret;
147
148         rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
149         rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
150
151         if (rec.dptr == NULL) {
152                 talloc_free(tmp_ctx);
153                 return NT_STATUS_NO_MEMORY;
154         }
155
156         memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
157         memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
158
159         ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
160
161         talloc_free(tmp_ctx);
162
163         return (ret == 0) ? NT_STATUS_OK
164                           : tdb_error_to_ntstatus(db->wtdb->tdb);
165
166 }
167
168 /*
169   form a ctdb_rec_data record from a key/data pair
170
171   note that header may be NULL. If not NULL then it is included in the data portion
172   of the record
173  */
174 static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,       
175                                                   TDB_DATA key, 
176                                                   struct ctdb_ltdb_header *header,
177                                                   TDB_DATA data)
178 {
179         size_t length;
180         struct ctdb_rec_data *d;
181
182         length = offsetof(struct ctdb_rec_data, data) + key.dsize + 
183                 data.dsize + (header?sizeof(*header):0);
184         d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
185         if (d == NULL) {
186                 return NULL;
187         }
188         d->length = length;
189         d->reqid = reqid;
190         d->keylen = key.dsize;
191         memcpy(&d->data[0], key.dptr, key.dsize);
192         if (header) {
193                 d->datalen = data.dsize + sizeof(*header);
194                 memcpy(&d->data[key.dsize], header, sizeof(*header));
195                 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
196         } else {
197                 d->datalen = data.dsize;
198                 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
199         }
200         return d;
201 }
202
203
204 /* helper function for marshalling multiple records */
205 static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx, 
206                                                struct ctdb_marshall_buffer *m,
207                                                uint64_t db_id,
208                                                uint32_t reqid,
209                                                TDB_DATA key,
210                                                struct ctdb_ltdb_header *header,
211                                                TDB_DATA data)
212 {
213         struct ctdb_rec_data *r;
214         size_t m_size, r_size;
215         struct ctdb_marshall_buffer *m2 = NULL;
216
217         r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
218         if (r == NULL) {
219                 talloc_free(m);
220                 return NULL;
221         }
222
223         if (m == NULL) {
224                 m = (struct ctdb_marshall_buffer *)talloc_zero_size(
225                         mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
226                 if (m == NULL) {
227                         goto done;
228                 }
229                 m->db_id = db_id;
230         }
231
232         m_size = talloc_get_size(m);
233         r_size = talloc_get_size(r);
234
235         m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
236                 mem_ctx, m,  m_size + r_size);
237         if (m2 == NULL) {
238                 talloc_free(m);
239                 goto done;
240         }
241
242         memcpy(m_size + (uint8_t *)m2, r, r_size);
243
244         m2->count++;
245
246 done:
247         talloc_free(r);
248         return m2;
249 }
250
251 /* we've finished marshalling, return a data blob with the marshalled records */
252 static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
253 {
254         TDB_DATA data;
255         data.dptr = (uint8_t *)m;
256         data.dsize = talloc_get_size(m);
257         return data;
258 }
259
260 /* 
261    loop over a marshalling buffer 
262
263      - pass r==NULL to start
264      - loop the number of times indicated by m->count
265 */
266 static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
267                                                      uint32_t *reqid,
268                                                      struct ctdb_ltdb_header *header,
269                                                      TDB_DATA *key, TDB_DATA *data)
270 {
271         if (r == NULL) {
272                 r = (struct ctdb_rec_data *)&m->data[0];
273         } else {
274                 r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
275         }
276
277         if (reqid != NULL) {
278                 *reqid = r->reqid;
279         }
280
281         if (key != NULL) {
282                 key->dptr   = &r->data[0];
283                 key->dsize  = r->keylen;
284         }
285         if (data != NULL) {
286                 data->dptr  = &r->data[r->keylen];
287                 data->dsize = r->datalen;
288                 if (header != NULL) {
289                         data->dptr += sizeof(*header);
290                         data->dsize -= sizeof(*header);
291                 }
292         }
293
294         if (header != NULL) {
295                 if (r->datalen < sizeof(*header)) {
296                         return NULL;
297                 }
298                 *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
299         }
300
301         return r;
302 }
303
304 /**
305  * CTDB transaction destructor
306  */
307 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
308 {
309         NTSTATUS status;
310
311         status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
312         if (!NT_STATUS_IS_OK(status)) {
313                 DEBUG(0, ("g_lock_unlock failed: %s\n", nt_errstr(status)));
314                 return -1;
315         }
316         return 0;
317 }
318
319 /**
320  * CTDB dbwrap API: transaction_start function
321  * starts a transaction on a persistent database
322  */
323 static int db_ctdb_transaction_start(struct db_context *db)
324 {
325         struct db_ctdb_transaction_handle *h;
326         NTSTATUS status;
327         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
328                                                         struct db_ctdb_ctx);
329
330         if (!db->persistent) {
331                 DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n", 
332                          ctx->db_id));
333                 return -1;
334         }
335
336         if (ctx->transaction) {
337                 ctx->transaction->nesting++;
338                 return 0;
339         }
340
341         h = talloc_zero(db, struct db_ctdb_transaction_handle);
342         if (h == NULL) {
343                 DEBUG(0,(__location__ " oom for transaction handle\n"));                
344                 return -1;
345         }
346
347         h->ctx = ctx;
348
349         h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
350                                        (unsigned int)ctx->db_id);
351         if (h->lock_name == NULL) {
352                 DEBUG(0, ("talloc_asprintf failed\n"));
353                 TALLOC_FREE(h);
354                 return -1;
355         }
356
357         /*
358          * Wait a day, i.e. forever...
359          */
360         status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
361                              timeval_set(86400, 0));
362         if (!NT_STATUS_IS_OK(status)) {
363                 DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
364                 TALLOC_FREE(h);
365                 return -1;
366         }
367
368         talloc_set_destructor(h, db_ctdb_transaction_destructor);
369
370         ctx->transaction = h;
371
372         DEBUG(5,(__location__ " Started transaction on db 0x%08x\n", ctx->db_id));
373
374         return 0;
375 }
376
377 static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
378                                              TDB_DATA key,
379                                              struct ctdb_ltdb_header *pheader,
380                                              TALLOC_CTX *mem_ctx,
381                                              TDB_DATA *pdata)
382 {
383         struct ctdb_rec_data *rec = NULL;
384         struct ctdb_ltdb_header h;
385         bool found = false;
386         TDB_DATA data;
387         int i;
388
389         if (buf == NULL) {
390                 return false;
391         }
392
393         ZERO_STRUCT(h);
394         ZERO_STRUCT(data);
395
396         /*
397          * Walk the list of records written during this
398          * transaction. If we want to read one we have already
399          * written, return the last written sample. Thus we do not do
400          * a "break;" for the first hit, this record might have been
401          * overwritten later.
402          */
403
404         for (i=0; i<buf->count; i++) {
405                 TDB_DATA tkey, tdata;
406                 uint32_t reqid;
407                 struct ctdb_ltdb_header hdr;
408
409                 ZERO_STRUCT(hdr);
410
411                 rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &hdr, &tkey,
412                                                  &tdata);
413                 if (rec == NULL) {
414                         return false;
415                 }
416
417                 if (tdb_data_equal(key, tkey)) {
418                         found = true;
419                         data = tdata;
420                         h = hdr;
421                 }
422         }
423
424         if (!found) {
425                 return false;
426         }
427
428         if (pdata != NULL) {
429                 data.dptr = (uint8_t *)talloc_memdup(mem_ctx, data.dptr,
430                                                      data.dsize);
431                 if ((data.dsize != 0) && (data.dptr == NULL)) {
432                         return false;
433                 }
434                 *pdata = data;
435         }
436
437         if (pheader != NULL) {
438                 *pheader = h;
439         }
440
441         return true;
442 }
443
444 /*
445   fetch a record inside a transaction
446  */
447 static int db_ctdb_transaction_fetch(struct db_ctdb_ctx *db, 
448                                      TALLOC_CTX *mem_ctx, 
449                                      TDB_DATA key, TDB_DATA *data)
450 {
451         struct db_ctdb_transaction_handle *h = db->transaction;
452         NTSTATUS status;
453         bool found;
454
455         found = pull_newest_from_marshall_buffer(h->m_write, key, NULL,
456                                                  mem_ctx, data);
457         if (found) {
458                 return 0;
459         }
460
461         status = db_ctdb_ltdb_fetch(h->ctx, key, NULL, mem_ctx, data);
462
463         if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
464                 *data = tdb_null;
465         } else if (!NT_STATUS_IS_OK(status)) {
466                 return -1;
467         }
468
469         h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 1, key,
470                                         NULL, *data);
471         if (h->m_all == NULL) {
472                 DEBUG(0,(__location__ " Failed to add to marshalling "
473                          "record\n"));
474                 data->dsize = 0;
475                 talloc_free(data->dptr);
476                 return -1;
477         }
478
479         return 0;
480 }
481
482
483 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
484 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
485
486 static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
487                                                           TALLOC_CTX *mem_ctx,
488                                                           TDB_DATA key)
489 {
490         struct db_record *result;
491         TDB_DATA ctdb_data;
492
493         if (!(result = talloc(mem_ctx, struct db_record))) {
494                 DEBUG(0, ("talloc failed\n"));
495                 return NULL;
496         }
497
498         result->private_data = ctx->transaction;
499
500         result->key.dsize = key.dsize;
501         result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
502         if (result->key.dptr == NULL) {
503                 DEBUG(0, ("talloc failed\n"));
504                 TALLOC_FREE(result);
505                 return NULL;
506         }
507
508         result->store = db_ctdb_store_transaction;
509         result->delete_rec = db_ctdb_delete_transaction;
510
511         if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
512                                              NULL, result, &result->value)) {
513                 return result;
514         }
515
516         ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
517         if (ctdb_data.dptr == NULL) {
518                 /* create the record */
519                 result->value = tdb_null;
520                 return result;
521         }
522
523         result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
524         result->value.dptr = NULL;
525
526         if ((result->value.dsize != 0)
527             && !(result->value.dptr = (uint8 *)talloc_memdup(
528                          result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
529                          result->value.dsize))) {
530                 DEBUG(0, ("talloc failed\n"));
531                 TALLOC_FREE(result);
532         }
533
534         SAFE_FREE(ctdb_data.dptr);
535
536         return result;
537 }
538
539 static int db_ctdb_record_destructor(struct db_record **recp)
540 {
541         struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
542         struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
543                 rec->private_data, struct db_ctdb_transaction_handle);
544         int ret = h->ctx->db->transaction_commit(h->ctx->db);
545         if (ret != 0) {
546                 DEBUG(0,(__location__ " transaction_commit failed\n"));
547         }
548         return 0;
549 }
550
551 /*
552   auto-create a transaction for persistent databases
553  */
554 static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
555                                                          TALLOC_CTX *mem_ctx,
556                                                          TDB_DATA key)
557 {
558         int res;
559         struct db_record *rec, **recp;
560
561         res = db_ctdb_transaction_start(ctx->db);
562         if (res == -1) {
563                 return NULL;
564         }
565
566         rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
567         if (rec == NULL) {
568                 ctx->db->transaction_cancel(ctx->db);           
569                 return NULL;
570         }
571
572         /* destroy this transaction when we release the lock */
573         recp = talloc(rec, struct db_record *);
574         if (recp == NULL) {
575                 ctx->db->transaction_cancel(ctx->db);
576                 talloc_free(rec);
577                 return NULL;
578         }
579         *recp = rec;
580         talloc_set_destructor(recp, db_ctdb_record_destructor);
581         return rec;
582 }
583
584
585 /*
586   stores a record inside a transaction
587  */
588 static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
589                                           TDB_DATA key, TDB_DATA data)
590 {
591         TALLOC_CTX *tmp_ctx = talloc_new(h);
592         TDB_DATA rec;
593         struct ctdb_ltdb_header header;
594
595         ZERO_STRUCT(header);
596
597         /* we need the header so we can update the RSN */
598
599         if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
600                                               NULL, NULL)) {
601
602                 rec = tdb_fetch(h->ctx->wtdb->tdb, key);
603
604                 if (rec.dptr != NULL) {
605                         memcpy(&header, rec.dptr,
606                                sizeof(struct ctdb_ltdb_header));
607                         rec.dsize -= sizeof(struct ctdb_ltdb_header);
608
609                         /*
610                          * a special case, we are writing the same
611                          * data that is there now
612                          */
613                         if (data.dsize == rec.dsize &&
614                             memcmp(data.dptr,
615                                    rec.dptr + sizeof(struct ctdb_ltdb_header),
616                                    data.dsize) == 0) {
617                                 SAFE_FREE(rec.dptr);
618                                 talloc_free(tmp_ctx);
619                                 return NT_STATUS_OK;
620                         }
621                 }
622                 SAFE_FREE(rec.dptr);
623         }
624
625         header.dmaster = get_my_vnn();
626         header.rsn++;
627
628         h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 0, key,
629                                         NULL, data);
630         if (h->m_all == NULL) {
631                 DEBUG(0,(__location__ " Failed to add to marshalling "
632                          "record\n"));
633                 talloc_free(tmp_ctx);
634                 return NT_STATUS_NO_MEMORY;
635         }
636
637         h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
638         if (h->m_write == NULL) {
639                 DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
640                 talloc_free(tmp_ctx);
641                 return NT_STATUS_NO_MEMORY;
642         }
643
644         talloc_free(tmp_ctx);
645         return NT_STATUS_OK;
646 }
647
648
649 /* 
650    a record store inside a transaction
651  */
652 static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
653 {
654         struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
655                 rec->private_data, struct db_ctdb_transaction_handle);
656         NTSTATUS status;
657
658         status = db_ctdb_transaction_store(h, rec->key, data);
659         return status;
660 }
661
662 /* 
663    a record delete inside a transaction
664  */
665 static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
666 {
667         struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
668                 rec->private_data, struct db_ctdb_transaction_handle);
669         NTSTATUS status;
670
671         status =  db_ctdb_transaction_store(h, rec->key, tdb_null);
672         return status;
673 }
674
675 /**
676  * Fetch the db sequence number of a persistent db directly from the db.
677  */
678 static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
679                                                 uint64_t *seqnum)
680 {
681         NTSTATUS status;
682         const char *keyname = CTDB_DB_SEQNUM_KEY;
683         TDB_DATA key;
684         TDB_DATA data;
685         struct ctdb_ltdb_header header;
686         TALLOC_CTX *mem_ctx = talloc_stackframe();
687
688         if (seqnum == NULL) {
689                 return NT_STATUS_INVALID_PARAMETER;
690         }
691
692         key = string_term_tdb_data(keyname);
693
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))
697         {
698                 goto done;
699         }
700
701         status = NT_STATUS_OK;
702
703         if (data.dsize != sizeof(uint64_t)) {
704                 *seqnum = 0;
705                 goto done;
706         }
707
708         *seqnum = *(uint64_t *)data.dptr;
709
710 done:
711         TALLOC_FREE(mem_ctx);
712         return status;
713 }
714
715 /**
716  * Store the database sequence number inside a transaction.
717  */
718 static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
719                                         uint64_t seqnum)
720 {
721         NTSTATUS status;
722         const char *keyname = CTDB_DB_SEQNUM_KEY;
723         TDB_DATA key;
724         TDB_DATA data;
725
726         key = string_term_tdb_data(keyname);
727
728         data.dptr = (uint8_t *)&seqnum;
729         data.dsize = sizeof(uint64_t);
730
731         status = db_ctdb_transaction_store(h, key, data);
732
733         return status;
734 }
735
736 /*
737   commit a transaction
738  */
739 static int db_ctdb_transaction_commit(struct db_context *db)
740 {
741         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
742                                                         struct db_ctdb_ctx);
743         NTSTATUS rets;
744         int status;
745         struct db_ctdb_transaction_handle *h = ctx->transaction;
746         uint64_t old_seqnum, new_seqnum;
747         int ret;
748
749         if (h == NULL) {
750                 DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
751                 return -1;
752         }
753
754         if (h->nested_cancel) {
755                 db->transaction_cancel(db);
756                 DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
757                 return -1;
758         }
759
760         if (h->nesting != 0) {
761                 h->nesting--;
762                 return 0;
763         }
764
765         if (h->m_write == NULL) {
766                 /*
767                  * No changes were made, so don't change the seqnum,
768                  * don't push to other node, just exit with success.
769                  */
770                 ret = 0;
771                 goto done;
772         }
773
774         DEBUG(5,(__location__ " Commit transaction on db 0x%08x\n", ctx->db_id));
775
776         /*
777          * As the last db action before committing, bump the database sequence
778          * number. Note that this undoes all changes to the seqnum records
779          * performed under the transaction. This record is not meant to be
780          * modified by user interaction. It is for internal use only...
781          */
782         rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
783         if (!NT_STATUS_IS_OK(rets)) {
784                 DEBUG(1, (__location__ " failed to fetch the db sequence number "
785                           "in transaction commit on db 0x%08x\n", ctx->db_id));
786                 ret = -1;
787                 goto done;
788         }
789
790         new_seqnum = old_seqnum + 1;
791
792         rets = db_ctdb_store_db_seqnum(h, new_seqnum);
793         if (!NT_STATUS_IS_OK(rets)) {
794                 DEBUG(1, (__location__ "failed to store the db sequence number "
795                           " in transaction commit on db 0x%08x\n", ctx->db_id));
796                 ret = -1;
797                 goto done;
798         }
799
800 again:
801         /* tell ctdbd to commit to the other nodes */
802         rets = ctdbd_control_local(messaging_ctdbd_connection(),
803                                    CTDB_CONTROL_TRANS3_COMMIT,
804                                    h->ctx->db_id, 0,
805                                    db_ctdb_marshall_finish(h->m_write),
806                                    NULL, NULL, &status);
807         if (!NT_STATUS_IS_OK(rets) || status != 0) {
808                 /*
809                  * The TRANS3_COMMIT control should only possibly fail when a
810                  * recovery has been running concurrently. In any case, the db
811                  * will be the same on all nodes, either the new copy or the
812                  * old copy.  This can be detected by comparing the old and new
813                  * local sequence numbers.
814                  */
815                 rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
816                 if (!NT_STATUS_IS_OK(rets)) {
817                         DEBUG(1, (__location__ " failed to refetch db sequence "
818                                   "number after failed TRANS3_COMMIT\n"));
819                         ret = -1;
820                         goto done;
821                 }
822
823                 if (new_seqnum == old_seqnum) {
824                         /* Recovery prevented all our changes: retry. */
825                         goto again;
826                 } else if (new_seqnum != (old_seqnum + 1)) {
827                         DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
828                                   "old_seqnum[%lu] + (0 or 1) after failed "
829                                   "TRANS3_COMMIT - this should not happen!\n",
830                                   (unsigned long)new_seqnum,
831                                   (unsigned long)old_seqnum));
832                         ret = -1;
833                         goto done;
834                 }
835                 /*
836                  * Recovery propagated our changes to all nodes, completing
837                  * our commit for us - succeed.
838                  */
839         }
840
841         ret = 0;
842
843 done:
844         h->ctx->transaction = NULL;
845         talloc_free(h);
846         return ret;
847 }
848
849
850 /*
851   cancel a transaction
852  */
853 static int db_ctdb_transaction_cancel(struct db_context *db)
854 {
855         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
856                                                         struct db_ctdb_ctx);
857         struct db_ctdb_transaction_handle *h = ctx->transaction;
858
859         if (h == NULL) {
860                 DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
861                 return -1;
862         }
863
864         if (h->nesting != 0) {
865                 h->nesting--;
866                 h->nested_cancel = true;
867                 return 0;
868         }
869
870         DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
871
872         ctx->transaction = NULL;
873         talloc_free(h);
874         return 0;
875 }
876
877
878 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
879 {
880         struct db_ctdb_rec *crec = talloc_get_type_abort(
881                 rec->private_data, struct db_ctdb_rec);
882
883         return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
884 }
885
886
887
888 static NTSTATUS db_ctdb_delete(struct db_record *rec)
889 {
890         TDB_DATA data;
891         NTSTATUS status;
892         struct ctdb_control_schedule_for_deletion *dd;
893         TDB_DATA indata;
894         int cstatus;
895         struct db_ctdb_rec *crec = talloc_get_type_abort(
896                 rec->private_data, struct db_ctdb_rec);
897
898         /*
899          * We have to store the header with empty data. TODO: Fix the
900          * tdb-level cleanup
901          */
902
903         ZERO_STRUCT(data);
904
905         status = db_ctdb_store(rec, data, 0);
906         if (!NT_STATUS_IS_OK(status)) {
907                 return status;
908         }
909
910         indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
911         indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
912         if (indata.dptr == NULL) {
913                 printf("out of memory\n");
914                 exit(1);
915         }
916         dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
917         dd->db_id = crec->ctdb_ctx->db_id;
918         dd->hdr = crec->header;
919         dd->keylen = rec->key.dsize;
920         memcpy(dd->key, rec->key.dptr, rec->key.dsize);
921
922         status = ctdbd_control_local(messaging_ctdbd_connection(),
923                                      CTDB_CONTROL_SCHEDULE_FOR_DELETION,
924                                      crec->ctdb_ctx->db_id,
925                                      CTDB_CTRL_FLAG_NOREPLY, /* flags */
926                                      indata,
927                                      NULL, /* outdata */
928                                      NULL, /* errmsg */
929                                      &cstatus);
930         talloc_free(indata.dptr);
931
932         if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
933                 DEBUG(1, (__location__ " Error sending local control "
934                           "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
935                           nt_errstr(status), cstatus));
936                 if (NT_STATUS_IS_OK(status)) {
937                         status = NT_STATUS_UNSUCCESSFUL;
938                 }
939         }
940
941         return status;
942 }
943
944 static int db_ctdb_record_destr(struct db_record* data)
945 {
946         struct db_ctdb_rec *crec = talloc_get_type_abort(
947                 data->private_data, struct db_ctdb_rec);
948         int threshold;
949
950         DEBUG(10, (DEBUGLEVEL > 10
951                    ? "Unlocking db %u key %s\n"
952                    : "Unlocking db %u key %.20s\n",
953                    (int)crec->ctdb_ctx->db_id,
954                    hex_encode_talloc(data, (unsigned char *)data->key.dptr,
955                               data->key.dsize)));
956
957         if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
958                 DEBUG(0, ("tdb_chainunlock failed\n"));
959                 return -1;
960         }
961
962         threshold = lp_ctdb_locktime_warn_threshold();
963         if (threshold != 0) {
964                 double timediff = timeval_elapsed(&crec->lock_time);
965                 if ((timediff * 1000) > threshold) {
966                         DEBUG(0, ("Held tdb lock %f seconds\n", timediff));
967                 }
968         }
969
970         return 0;
971 }
972
973 static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
974                                                TALLOC_CTX *mem_ctx,
975                                                TDB_DATA key,
976                                                bool persistent)
977 {
978         struct db_record *result;
979         struct db_ctdb_rec *crec;
980         NTSTATUS status;
981         TDB_DATA ctdb_data;
982         int migrate_attempts = 0;
983
984         if (!(result = talloc(mem_ctx, struct db_record))) {
985                 DEBUG(0, ("talloc failed\n"));
986                 return NULL;
987         }
988
989         if (!(crec = TALLOC_ZERO_P(result, struct db_ctdb_rec))) {
990                 DEBUG(0, ("talloc failed\n"));
991                 TALLOC_FREE(result);
992                 return NULL;
993         }
994
995         result->private_data = (void *)crec;
996         crec->ctdb_ctx = ctx;
997
998         result->key.dsize = key.dsize;
999         result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
1000         if (result->key.dptr == NULL) {
1001                 DEBUG(0, ("talloc failed\n"));
1002                 TALLOC_FREE(result);
1003                 return NULL;
1004         }
1005
1006         /*
1007          * Do a blocking lock on the record
1008          */
1009 again:
1010
1011         if (DEBUGLEVEL >= 10) {
1012                 char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
1013                 DEBUG(10, (DEBUGLEVEL > 10
1014                            ? "Locking db %u key %s\n"
1015                            : "Locking db %u key %.20s\n",
1016                            (int)crec->ctdb_ctx->db_id, keystr));
1017                 TALLOC_FREE(keystr);
1018         }
1019
1020         if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
1021                 DEBUG(3, ("tdb_chainlock failed\n"));
1022                 TALLOC_FREE(result);
1023                 return NULL;
1024         }
1025
1026         result->store = db_ctdb_store;
1027         result->delete_rec = db_ctdb_delete;
1028         talloc_set_destructor(result, db_ctdb_record_destr);
1029
1030         ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
1031
1032         /*
1033          * See if we have a valid record and we are the dmaster. If so, we can
1034          * take the shortcut and just return it.
1035          */
1036
1037         if ((ctdb_data.dptr == NULL) ||
1038             (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
1039             ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
1040 #if 0
1041             || (random() % 2 != 0)
1042 #endif
1043 ) {
1044                 SAFE_FREE(ctdb_data.dptr);
1045                 tdb_chainunlock(ctx->wtdb->tdb, key);
1046                 talloc_set_destructor(result, NULL);
1047
1048                 migrate_attempts += 1;
1049
1050                 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
1051                            ctdb_data.dptr, ctdb_data.dptr ?
1052                            ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
1053                            get_my_vnn()));
1054
1055                 status = ctdbd_migrate(messaging_ctdbd_connection(),ctx->db_id, key);
1056                 if (!NT_STATUS_IS_OK(status)) {
1057                         DEBUG(5, ("ctdb_migrate failed: %s\n",
1058                                   nt_errstr(status)));
1059                         TALLOC_FREE(result);
1060                         return NULL;
1061                 }
1062                 /* now its migrated, try again */
1063                 goto again;
1064         }
1065
1066         if (migrate_attempts > 10) {
1067                 DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
1068                           migrate_attempts));
1069         }
1070
1071         GetTimeOfDay(&crec->lock_time);
1072
1073         memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
1074
1075         result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
1076         result->value.dptr = NULL;
1077
1078         if ((result->value.dsize != 0)
1079             && !(result->value.dptr = (uint8 *)talloc_memdup(
1080                          result, ctdb_data.dptr + sizeof(crec->header),
1081                          result->value.dsize))) {
1082                 DEBUG(0, ("talloc failed\n"));
1083                 TALLOC_FREE(result);
1084         }
1085
1086         SAFE_FREE(ctdb_data.dptr);
1087
1088         return result;
1089 }
1090
1091 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
1092                                               TALLOC_CTX *mem_ctx,
1093                                               TDB_DATA key)
1094 {
1095         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1096                                                         struct db_ctdb_ctx);
1097
1098         if (ctx->transaction != NULL) {
1099                 return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
1100         }
1101
1102         if (db->persistent) {
1103                 return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
1104         }
1105
1106         return fetch_locked_internal(ctx, mem_ctx, key, db->persistent);
1107 }
1108
1109 /*
1110   fetch (unlocked, no migration) operation on ctdb
1111  */
1112 static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
1113                          TDB_DATA key, TDB_DATA *data)
1114 {
1115         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1116                                                         struct db_ctdb_ctx);
1117         NTSTATUS status;
1118         TDB_DATA ctdb_data;
1119
1120         if (ctx->transaction) {
1121                 return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
1122         }
1123
1124         /* try a direct fetch */
1125         ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
1126
1127         /*
1128          * See if we have a valid record and we are the dmaster. If so, we can
1129          * take the shortcut and just return it.
1130          * we bypass the dmaster check for persistent databases
1131          */
1132         if ((ctdb_data.dptr != NULL) &&
1133             (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
1134             (db->persistent ||
1135              ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn())) {
1136                 /* we are the dmaster - avoid the ctdb protocol op */
1137
1138                 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
1139                 if (data->dsize == 0) {
1140                         SAFE_FREE(ctdb_data.dptr);
1141                         data->dptr = NULL;
1142                         return 0;
1143                 }
1144
1145                 data->dptr = (uint8 *)talloc_memdup(
1146                         mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
1147                         data->dsize);
1148
1149                 SAFE_FREE(ctdb_data.dptr);
1150
1151                 if (data->dptr == NULL) {
1152                         return -1;
1153                 }
1154                 return 0;
1155         }
1156
1157         SAFE_FREE(ctdb_data.dptr);
1158
1159         /* we weren't able to get it locally - ask ctdb to fetch it for us */
1160         status = ctdbd_fetch(messaging_ctdbd_connection(),ctx->db_id, key, mem_ctx, data);
1161         if (!NT_STATUS_IS_OK(status)) {
1162                 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
1163                 return -1;
1164         }
1165
1166         return 0;
1167 }
1168
1169 struct traverse_state {
1170         struct db_context *db;
1171         int (*fn)(struct db_record *rec, void *private_data);
1172         void *private_data;
1173 };
1174
1175 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1176 {
1177         struct traverse_state *state = (struct traverse_state *)private_data;
1178         struct db_record *rec;
1179         TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1180         /* we have to give them a locked record to prevent races */
1181         rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
1182         if (rec && rec->value.dsize > 0) {
1183                 state->fn(rec, state->private_data);
1184         }
1185         talloc_free(tmp_ctx);
1186 }
1187
1188 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1189                                         void *private_data)
1190 {
1191         struct traverse_state *state = (struct traverse_state *)private_data;
1192         struct db_record *rec;
1193         TALLOC_CTX *tmp_ctx = talloc_new(state->db);
1194         int ret = 0;
1195         /* we have to give them a locked record to prevent races */
1196         rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
1197         if (rec && rec->value.dsize > 0) {
1198                 ret = state->fn(rec, state->private_data);
1199         }
1200         talloc_free(tmp_ctx);
1201         return ret;
1202 }
1203
1204 static int db_ctdb_traverse(struct db_context *db,
1205                             int (*fn)(struct db_record *rec,
1206                                       void *private_data),
1207                             void *private_data)
1208 {
1209         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1210                                                         struct db_ctdb_ctx);
1211         struct traverse_state state;
1212
1213         state.db = db;
1214         state.fn = fn;
1215         state.private_data = private_data;
1216
1217         if (db->persistent) {
1218                 /* for persistent databases we don't need to do a ctdb traverse,
1219                    we can do a faster local traverse */
1220                 return tdb_traverse(ctx->wtdb->tdb, traverse_persistent_callback, &state);
1221         }
1222
1223
1224         ctdbd_traverse(ctx->db_id, traverse_callback, &state);
1225         return 0;
1226 }
1227
1228 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
1229 {
1230         return NT_STATUS_MEDIA_WRITE_PROTECTED;
1231 }
1232
1233 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
1234 {
1235         return NT_STATUS_MEDIA_WRITE_PROTECTED;
1236 }
1237
1238 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
1239 {
1240         struct traverse_state *state = (struct traverse_state *)private_data;
1241         struct db_record rec;
1242         rec.key = key;
1243         rec.value = data;
1244         rec.store = db_ctdb_store_deny;
1245         rec.delete_rec = db_ctdb_delete_deny;
1246         rec.private_data = state->db;
1247         state->fn(&rec, state->private_data);
1248 }
1249
1250 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
1251                                         void *private_data)
1252 {
1253         struct traverse_state *state = (struct traverse_state *)private_data;
1254         struct db_record rec;
1255         rec.key = kbuf;
1256         rec.value = dbuf;
1257         rec.store = db_ctdb_store_deny;
1258         rec.delete_rec = db_ctdb_delete_deny;
1259         rec.private_data = state->db;
1260
1261         if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
1262                 /* a deleted record */
1263                 return 0;
1264         }
1265         rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
1266         rec.value.dptr += sizeof(struct ctdb_ltdb_header);
1267
1268         return state->fn(&rec, state->private_data);
1269 }
1270
1271 static int db_ctdb_traverse_read(struct db_context *db,
1272                                  int (*fn)(struct db_record *rec,
1273                                            void *private_data),
1274                                  void *private_data)
1275 {
1276         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1277                                                         struct db_ctdb_ctx);
1278         struct traverse_state state;
1279
1280         state.db = db;
1281         state.fn = fn;
1282         state.private_data = private_data;
1283
1284         if (db->persistent) {
1285                 /* for persistent databases we don't need to do a ctdb traverse,
1286                    we can do a faster local traverse */
1287                 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
1288         }
1289
1290         ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
1291         return 0;
1292 }
1293
1294 static int db_ctdb_get_seqnum(struct db_context *db)
1295 {
1296         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1297                                                         struct db_ctdb_ctx);
1298         return tdb_get_seqnum(ctx->wtdb->tdb);
1299 }
1300
1301 static int db_ctdb_get_flags(struct db_context *db)
1302 {
1303         struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
1304                                                         struct db_ctdb_ctx);
1305         return tdb_get_flags(ctx->wtdb->tdb);
1306 }
1307
1308 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
1309                                 const char *name,
1310                                 int hash_size, int tdb_flags,
1311                                 int open_flags, mode_t mode)
1312 {
1313         struct db_context *result;
1314         struct db_ctdb_ctx *db_ctdb;
1315         char *db_path;
1316         struct ctdbd_connection *conn;
1317
1318         if (!lp_clustering()) {
1319                 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
1320                 return NULL;
1321         }
1322
1323         if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
1324                 DEBUG(0, ("talloc failed\n"));
1325                 TALLOC_FREE(result);
1326                 return NULL;
1327         }
1328
1329         if (!(db_ctdb = TALLOC_P(result, struct db_ctdb_ctx))) {
1330                 DEBUG(0, ("talloc failed\n"));
1331                 TALLOC_FREE(result);
1332                 return NULL;
1333         }
1334
1335         db_ctdb->transaction = NULL;
1336         db_ctdb->db = result;
1337
1338         conn = messaging_ctdbd_connection();
1339         if (conn == NULL) {
1340                 DEBUG(1, ("Could not connect to ctdb\n"));
1341                 TALLOC_FREE(result);
1342                 return NULL;
1343         }
1344
1345         if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
1346                 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
1347                 TALLOC_FREE(result);
1348                 return NULL;
1349         }
1350
1351         db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
1352
1353         result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
1354
1355         /* only pass through specific flags */
1356         tdb_flags &= TDB_SEQNUM;
1357
1358         /* honor permissions if user has specified O_CREAT */
1359         if (open_flags & O_CREAT) {
1360                 chmod(db_path, mode);
1361         }
1362
1363         db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0);
1364         if (db_ctdb->wtdb == NULL) {
1365                 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
1366                 TALLOC_FREE(result);
1367                 return NULL;
1368         }
1369         talloc_free(db_path);
1370
1371         if (result->persistent) {
1372                 db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
1373                                                     ctdb_conn_msg_ctx(conn));
1374                 if (db_ctdb->lock_ctx == NULL) {
1375                         DEBUG(0, ("g_lock_ctx_init failed\n"));
1376                         TALLOC_FREE(result);
1377                         return NULL;
1378                 }
1379         }
1380
1381         result->private_data = (void *)db_ctdb;
1382         result->fetch_locked = db_ctdb_fetch_locked;
1383         result->fetch = db_ctdb_fetch;
1384         result->traverse = db_ctdb_traverse;
1385         result->traverse_read = db_ctdb_traverse_read;
1386         result->get_seqnum = db_ctdb_get_seqnum;
1387         result->get_flags = db_ctdb_get_flags;
1388         result->transaction_start = db_ctdb_transaction_start;
1389         result->transaction_commit = db_ctdb_transaction_commit;
1390         result->transaction_cancel = db_ctdb_transaction_cancel;
1391
1392         DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
1393                  name, db_ctdb->db_id));
1394
1395         return result;
1396 }
1397 #endif