1a229dc2b94c1538e5093a8df55d74b8dd8d74f8
[ccan] / ccan / tdb2 / tdb.c
1 #include "private.h"
2 #include <ccan/tdb2/tdb2.h>
3 #include <ccan/build_assert/build_assert.h>
4 #include <ccan/likely/likely.h>
5 #include <assert.h>
6
7 /* The null return. */
8 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
9
10 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
11 static struct tdb_context *tdbs = NULL;
12
13 PRINTF_ATTRIBUTE(4, 5) static void
14 null_log_fn(struct tdb_context *tdb,
15             enum tdb_debug_level level, void *priv,
16             const char *fmt, ...)
17 {
18 }
19
20 static bool tdb_already_open(dev_t device, ino_t ino)
21 {
22         struct tdb_context *i;
23         
24         for (i = tdbs; i; i = i->next) {
25                 if (i->device == device && i->inode == ino) {
26                         return true;
27                 }
28         }
29
30         return false;
31 }
32
33 static uint64_t random_number(struct tdb_context *tdb)
34 {
35         int fd;
36         uint64_t ret = 0;
37         struct timeval now;
38
39         fd = open("/dev/urandom", O_RDONLY);
40         if (fd >= 0) {
41                 if (tdb_read_all(fd, &ret, sizeof(ret))) {
42                         tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
43                                  "tdb_open: random from /dev/urandom\n");
44                         close(fd);
45                         return ret;
46                 }
47                 close(fd);
48         }
49         /* FIXME: Untested!  Based on Wikipedia protocol description! */
50         fd = open("/dev/egd-pool", O_RDWR);
51         if (fd >= 0) {
52                 /* Command is 1, next byte is size we want to read. */
53                 char cmd[2] = { 1, sizeof(uint64_t) };
54                 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
55                         char reply[1 + sizeof(uint64_t)];
56                         int r = read(fd, reply, sizeof(reply));
57                         if (r > 1) {
58                                 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
59                                          "tdb_open: %u random bytes from"
60                                          " /dev/egd-pool\n", r-1);
61                                 /* Copy at least some bytes. */
62                                 memcpy(&ret, reply+1, r - 1);
63                                 if (reply[0] == sizeof(uint64_t)
64                                     && r == sizeof(reply)) {
65                                         close(fd);
66                                         return ret;
67                                 }
68                         }
69                 }
70                 close(fd);
71         }
72
73         /* Fallback: pid and time. */
74         gettimeofday(&now, NULL);
75         ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
76         tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
77                  "tdb_open: random from getpid and time\n");
78         return ret;
79 }
80
81 struct new_database {
82         struct tdb_header hdr;
83         /* Initial free zone. */
84         struct free_zone_header zhdr;
85         tdb_off_t free[BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS) + 1];
86         struct tdb_free_record frec;
87         /* Rest up to 1 << INITIAL_ZONE_BITS is empty. */
88         char space[(1 << INITIAL_ZONE_BITS)
89                    - sizeof(struct free_zone_header)
90                    - sizeof(tdb_off_t) * (BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS)+1)
91                    - sizeof(struct tdb_free_record)];
92         uint8_t tailer;
93         /* Don't count final padding! */
94 };
95
96 /* initialise a new database */
97 static int tdb_new_database(struct tdb_context *tdb, struct tdb_header *hdr)
98 {
99         /* We make it up in memory, then write it out if not internal */
100         struct new_database newdb;
101         unsigned int bucket, magic_len, dbsize;
102
103         /* Don't want any extra padding! */
104         dbsize = offsetof(struct new_database, tailer) + sizeof(newdb.tailer);
105
106         /* Fill in the header */
107         newdb.hdr.version = TDB_VERSION;
108         newdb.hdr.hash_seed = random_number(tdb);
109         newdb.hdr.hash_test = TDB_HASH_MAGIC;
110         newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
111                                          sizeof(newdb.hdr.hash_test),
112                                          newdb.hdr.hash_seed,
113                                          tdb->hash_priv);
114         memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
115         /* Initial hashes are empty. */
116         memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
117
118         /* Free is mostly empty... */
119         newdb.zhdr.zone_bits = INITIAL_ZONE_BITS;
120         memset(newdb.free, 0, sizeof(newdb.free));
121
122         /* Create the single free entry. */
123         newdb.frec.magic_and_meta = TDB_FREE_MAGIC | INITIAL_ZONE_BITS;
124         newdb.frec.data_len = (sizeof(newdb.frec)
125                                  - sizeof(struct tdb_used_record)
126                                  + sizeof(newdb.space));
127
128         /* Add it to the correct bucket. */
129         bucket = size_to_bucket(INITIAL_ZONE_BITS, newdb.frec.data_len);
130         newdb.free[bucket] = offsetof(struct new_database, frec);
131         newdb.frec.next = newdb.frec.prev = 0;
132
133         /* Clear free space to keep valgrind happy, and avoid leaking stack. */
134         memset(newdb.space, 0, sizeof(newdb.space));
135
136         /* Tailer contains maximum number of free_zone bits. */
137         newdb.tailer = INITIAL_ZONE_BITS;
138
139         /* Magic food */
140         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
141         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
142
143         /* This creates an endian-converted database, as if read from disk */
144         magic_len = sizeof(newdb.hdr.magic_food);
145         tdb_convert(tdb,
146                     (char *)&newdb.hdr + magic_len,
147                     offsetof(struct new_database, space) - magic_len);
148
149         *hdr = newdb.hdr;
150
151         if (tdb->flags & TDB_INTERNAL) {
152                 tdb->map_size = dbsize;
153                 tdb->map_ptr = malloc(tdb->map_size);
154                 if (!tdb->map_ptr) {
155                         tdb->ecode = TDB_ERR_OOM;
156                         return -1;
157                 }
158                 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
159                 return 0;
160         }
161         if (lseek(tdb->fd, 0, SEEK_SET) == -1)
162                 return -1;
163
164         if (ftruncate(tdb->fd, 0) == -1)
165                 return -1;
166
167         if (!tdb_pwrite_all(tdb->fd, &newdb, dbsize, 0)) {
168                 tdb->ecode = TDB_ERR_IO;
169                 return -1;
170         }
171         return 0;
172 }
173
174 struct tdb_context *tdb_open(const char *name, int tdb_flags,
175                              int open_flags, mode_t mode,
176                              union tdb_attribute *attr)
177 {
178         struct tdb_context *tdb;
179         struct stat st;
180         int save_errno;
181         uint64_t hash_test;
182         unsigned v;
183         struct tdb_header hdr;
184
185         tdb = malloc(sizeof(*tdb));
186         if (!tdb) {
187                 /* Can't log this */
188                 errno = ENOMEM;
189                 goto fail;
190         }
191         tdb->name = NULL;
192         tdb->map_ptr = NULL;
193         tdb->direct_access = 0;
194         tdb->fd = -1;
195         tdb->map_size = sizeof(struct tdb_header);
196         tdb->ecode = TDB_SUCCESS;
197         tdb->flags = tdb_flags;
198         tdb->log = null_log_fn;
199         tdb->log_priv = NULL;
200         tdb->transaction = NULL;
201         tdb_hash_init(tdb);
202         /* last_zone will be set below. */
203         tdb_io_init(tdb);
204         tdb_lock_init(tdb);
205
206         while (attr) {
207                 switch (attr->base.attr) {
208                 case TDB_ATTRIBUTE_LOG:
209                         tdb->log = attr->log.log_fn;
210                         tdb->log_priv = attr->log.log_private;
211                         break;
212                 case TDB_ATTRIBUTE_HASH:
213                         tdb->khash = attr->hash.hash_fn;
214                         tdb->hash_priv = attr->hash.hash_private;
215                         break;
216                 default:
217                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
218                                  "tdb_open: unknown attribute type %u\n",
219                                  attr->base.attr);
220                         errno = EINVAL;
221                         goto fail;
222                 }
223                 attr = attr->base.next;
224         }
225
226         if ((open_flags & O_ACCMODE) == O_WRONLY) {
227                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
228                          "tdb_open: can't open tdb %s write-only\n", name);
229                 errno = EINVAL;
230                 goto fail;
231         }
232
233         if ((open_flags & O_ACCMODE) == O_RDONLY) {
234                 tdb->read_only = true;
235                 /* read only databases don't do locking */
236                 tdb->flags |= TDB_NOLOCK;
237         } else
238                 tdb->read_only = false;
239
240         /* internal databases don't need any of the rest. */
241         if (tdb->flags & TDB_INTERNAL) {
242                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
243                 if (tdb_new_database(tdb, &hdr) != 0) {
244                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
245                                  "tdb_open: tdb_new_database failed!");
246                         goto fail;
247                 }
248                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
249                 tdb->hash_seed = hdr.hash_seed;
250                 tdb_zone_init(tdb);
251                 return tdb;
252         }
253
254         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
255                 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
256                          "tdb_open: could not open file %s: %s\n",
257                          name, strerror(errno));
258                 goto fail;      /* errno set by open(2) */
259         }
260
261         /* on exec, don't inherit the fd */
262         v = fcntl(tdb->fd, F_GETFD, 0);
263         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
264
265         /* ensure there is only one process initialising at once */
266         if (tdb_lock_open(tdb) == -1) {
267                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
268                          "tdb_open: failed to get open lock on %s: %s\n",
269                          name, strerror(errno));
270                 goto fail;      /* errno set by tdb_brlock */
271         }
272
273         if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
274             || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
275                 if (!(open_flags & O_CREAT) || tdb_new_database(tdb, &hdr) == -1) {
276                         if (errno == 0) {
277                                 errno = EIO; /* ie bad format or something */
278                         }
279                         goto fail;
280                 }
281         } else if (hdr.version != TDB_VERSION) {
282                 if (hdr.version == bswap_64(TDB_VERSION))
283                         tdb->flags |= TDB_CONVERT;
284                 else {
285                         /* wrong version */
286                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
287                                  "tdb_open: %s is unknown version 0x%llx\n",
288                                  name, (long long)hdr.version);
289                         errno = EIO;
290                         goto fail;
291                 }
292         }
293
294         tdb_convert(tdb, &hdr, sizeof(hdr));
295         tdb->hash_seed = hdr.hash_seed;
296         hash_test = TDB_HASH_MAGIC;
297         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
298         if (hdr.hash_test != hash_test) {
299                 /* wrong hash variant */
300                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
301                          "tdb_open: %s uses a different hash function\n",
302                          name);
303                 errno = EIO;
304                 goto fail;
305         }
306
307         if (fstat(tdb->fd, &st) == -1)
308                 goto fail;
309
310         /* Is it already in the open list?  If so, fail. */
311         if (tdb_already_open(st.st_dev, st.st_ino)) {
312                 /* FIXME */
313                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
314                          "tdb_open: %s (%d,%d) is already open in this process\n",
315                          name, (int)st.st_dev, (int)st.st_ino);
316                 errno = EBUSY;
317                 goto fail;
318         }
319
320         tdb->name = strdup(name);
321         if (!tdb->name) {
322                 errno = ENOMEM;
323                 goto fail;
324         }
325
326         tdb->device = st.st_dev;
327         tdb->inode = st.st_ino;
328         tdb_unlock_open(tdb);
329
330         /* This make sure we have current map_size and mmap. */
331         tdb->methods->oob(tdb, tdb->map_size + 1, true);
332
333         /* Now we can pick a random free zone to start from. */
334         if (tdb_zone_init(tdb) == -1)
335                 goto fail;
336
337         tdb->next = tdbs;
338         tdbs = tdb;
339         return tdb;
340
341  fail:
342         save_errno = errno;
343
344         if (!tdb)
345                 return NULL;
346
347 #ifdef TDB_TRACE
348         close(tdb->tracefd);
349 #endif
350         if (tdb->map_ptr) {
351                 if (tdb->flags & TDB_INTERNAL) {
352                         free(tdb->map_ptr);
353                 } else
354                         tdb_munmap(tdb);
355         }
356         free((char *)tdb->name);
357         if (tdb->fd != -1)
358                 if (close(tdb->fd) != 0)
359                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
360                                  "tdb_open: failed to close tdb->fd"
361                                  " on error!\n");
362         free(tdb);
363         errno = save_errno;
364         return NULL;
365 }
366
367 /* FIXME: modify, don't rewrite! */
368 static int update_rec_hdr(struct tdb_context *tdb,
369                           tdb_off_t off,
370                           tdb_len_t keylen,
371                           tdb_len_t datalen,
372                           struct tdb_used_record *rec,
373                           uint64_t h)
374 {
375         uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
376
377         if (set_header(tdb, rec, keylen, datalen, keylen + dataroom, h,
378                        rec_zone_bits(rec)))
379                 return -1;
380
381         return tdb_write_convert(tdb, off, rec, sizeof(*rec));
382 }
383
384 /* Returns -1 on error, 0 on OK */
385 static int replace_data(struct tdb_context *tdb,
386                         struct hash_info *h,
387                         struct tdb_data key, struct tdb_data dbuf,
388                         tdb_off_t old_off, tdb_len_t old_room,
389                         unsigned old_zone,
390                         bool growing)
391 {
392         tdb_off_t new_off;
393
394         /* Allocate a new record. */
395         new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, growing);
396         if (unlikely(new_off == TDB_OFF_ERR))
397                 return -1;
398
399         /* We didn't like the existing one: remove it. */
400         if (old_off) {
401                 add_free_record(tdb, old_zone, old_off,
402                                 sizeof(struct tdb_used_record)
403                                 + key.dsize + old_room);
404                 if (replace_in_hash(tdb, h, new_off) == -1)
405                         return -1;
406         } else {
407                 if (add_to_hash(tdb, h, new_off) == -1)
408                         return -1;
409         }
410
411         new_off += sizeof(struct tdb_used_record);
412         if (tdb->methods->write(tdb, new_off, key.dptr, key.dsize) == -1)
413                 return -1;
414
415         new_off += key.dsize;
416         if (tdb->methods->write(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
417                 return -1;
418
419         /* FIXME: tdb_increment_seqnum(tdb); */
420         return 0;
421 }
422
423 int tdb_store(struct tdb_context *tdb,
424               struct tdb_data key, struct tdb_data dbuf, int flag)
425 {
426         struct hash_info h;
427         tdb_off_t off;
428         tdb_len_t old_room = 0;
429         struct tdb_used_record rec;
430         int ret;
431
432         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec);
433         if (unlikely(off == TDB_OFF_ERR))
434                 return -1;
435
436         /* Now we have lock on this hash bucket. */
437         if (flag == TDB_INSERT) {
438                 if (off) {
439                         tdb->ecode = TDB_ERR_EXISTS;
440                         goto fail;
441                 }
442         } else {
443                 if (off) {
444                         old_room = rec_data_length(&rec)
445                                 + rec_extra_padding(&rec);
446                         if (old_room >= dbuf.dsize) {
447                                 /* Can modify in-place.  Easy! */
448                                 if (update_rec_hdr(tdb, off,
449                                                    key.dsize, dbuf.dsize,
450                                                    &rec, h.h))
451                                         goto fail;
452                                 if (tdb->methods->write(tdb, off + sizeof(rec)
453                                                         + key.dsize,
454                                                         dbuf.dptr, dbuf.dsize))
455                                         goto fail;
456                                 tdb_unlock_hashes(tdb, h.hlock_start,
457                                                   h.hlock_range, F_WRLCK);
458                                 return 0;
459                         }
460                         /* FIXME: See if right record is free? */
461                 } else {
462                         if (flag == TDB_MODIFY) {
463                                 /* if the record doesn't exist and we
464                                    are in TDB_MODIFY mode then we should fail
465                                    the store */
466                                 tdb->ecode = TDB_ERR_NOEXIST;
467                                 goto fail;
468                         }
469                 }
470         }
471
472         /* If we didn't use the old record, this implies we're growing. */
473         ret = replace_data(tdb, &h, key, dbuf, off, old_room,
474                            rec_zone_bits(&rec), off != 0);
475         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
476         return ret;
477
478 fail:
479         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
480         return -1;
481 }
482
483 int tdb_append(struct tdb_context *tdb,
484                struct tdb_data key, struct tdb_data dbuf)
485 {
486         struct hash_info h;
487         tdb_off_t off;
488         struct tdb_used_record rec;
489         tdb_len_t old_room = 0, old_dlen;
490         unsigned char *newdata;
491         struct tdb_data new_dbuf;
492         int ret;
493
494         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec);
495         if (unlikely(off == TDB_OFF_ERR))
496                 return -1;
497
498         if (off) {
499                 old_dlen = rec_data_length(&rec);
500                 old_room = old_dlen + rec_extra_padding(&rec);
501
502                 /* Fast path: can append in place. */
503                 if (rec_extra_padding(&rec) >= dbuf.dsize) {
504                         if (update_rec_hdr(tdb, off, key.dsize,
505                                            old_dlen + dbuf.dsize, &rec, h.h))
506                                 goto fail;
507
508                         off += sizeof(rec) + key.dsize + old_dlen;
509                         if (tdb->methods->write(tdb, off, dbuf.dptr,
510                                                 dbuf.dsize) == -1)
511                                 goto fail;
512
513                         /* FIXME: tdb_increment_seqnum(tdb); */
514                         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
515                                           F_WRLCK);
516                         return 0;
517                 }
518                 /* FIXME: Check right record free? */
519
520                 /* Slow path. */
521                 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
522                 if (!newdata) {
523                         tdb->ecode = TDB_ERR_OOM;
524                         tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
525                                  "tdb_append: cannot allocate %llu bytes!\n",
526                                  (long long)key.dsize + old_dlen + dbuf.dsize);
527                         goto fail;
528                 }
529                 if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
530                                        newdata, old_dlen) != 0) {
531                         free(newdata);
532                         goto fail;
533                 }
534                 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
535                 new_dbuf.dptr = newdata;
536                 new_dbuf.dsize = old_dlen + dbuf.dsize;
537         } else {
538                 newdata = NULL;
539                 new_dbuf = dbuf;
540         }
541
542         /* If they're using tdb_append(), it implies they're growing record. */
543         ret = replace_data(tdb, &h, key, new_dbuf, off,
544                            old_room, rec_zone_bits(&rec), true);
545         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
546         free(newdata);
547
548         return ret;
549
550 fail:
551         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
552         return -1;
553 }
554
555 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
556 {
557         tdb_off_t off;
558         struct tdb_used_record rec;
559         struct hash_info h;
560         struct tdb_data ret;
561
562         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec);
563         if (unlikely(off == TDB_OFF_ERR))
564                 return tdb_null;
565
566         if (!off) {
567                 tdb->ecode = TDB_ERR_NOEXIST;
568                 ret = tdb_null;
569         } else {
570                 ret.dsize = rec_data_length(&rec);
571                 ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
572                                           ret.dsize);
573         }
574
575         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
576         return ret;
577 }
578
579 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
580 {
581         tdb_off_t off;
582         struct tdb_used_record rec;
583         struct hash_info h;
584
585         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec);
586         if (unlikely(off == TDB_OFF_ERR))
587                 return -1;
588
589         if (!off) {
590                 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
591                 tdb->ecode = TDB_ERR_NOEXIST;
592                 return -1;
593         }
594
595         if (delete_from_hash(tdb, &h) == -1)
596                 goto unlock_err;
597
598         /* Free the deleted entry. */
599         if (add_free_record(tdb, rec_zone_bits(&rec), off,
600                             sizeof(struct tdb_used_record)
601                             + rec_key_length(&rec)
602                             + rec_data_length(&rec)
603                             + rec_extra_padding(&rec)) != 0)
604                 goto unlock_err;
605
606         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
607         return 0;
608
609 unlock_err:
610         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
611         return -1;
612 }
613
614 int tdb_close(struct tdb_context *tdb)
615 {
616         struct tdb_context **i;
617         int ret = 0;
618
619         /* FIXME:
620         if (tdb->transaction) {
621                 tdb_transaction_cancel(tdb);
622         }
623         */
624         tdb_trace(tdb, "tdb_close");
625
626         if (tdb->map_ptr) {
627                 if (tdb->flags & TDB_INTERNAL)
628                         free(tdb->map_ptr);
629                 else
630                         tdb_munmap(tdb);
631         }
632         free((char *)tdb->name);
633         if (tdb->fd != -1) {
634                 ret = close(tdb->fd);
635                 tdb->fd = -1;
636         }
637         free(tdb->lockrecs);
638
639         /* Remove from contexts list */
640         for (i = &tdbs; *i; i = &(*i)->next) {
641                 if (*i == tdb) {
642                         *i = tdb->next;
643                         break;
644                 }
645         }
646
647 #ifdef TDB_TRACE
648         close(tdb->tracefd);
649 #endif
650         free(tdb);
651
652         return ret;
653 }
654
655 enum TDB_ERROR tdb_error(struct tdb_context *tdb)
656 {
657         return tdb->ecode;
658 }