bd5c268173dcda9b11e1889957b64274c03773d9
[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_FMT(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         struct tdb_freelist flist;
84 };
85
86 /* initialise a new database */
87 static int tdb_new_database(struct tdb_context *tdb,
88                             struct tdb_attribute_seed *seed,
89                             struct tdb_header *hdr)
90 {
91         /* We make it up in memory, then write it out if not internal */
92         struct new_database newdb;
93         unsigned int magic_len;
94
95         /* Fill in the header */
96         newdb.hdr.version = TDB_VERSION;
97         if (seed)
98                 newdb.hdr.hash_seed = seed->seed;
99         else
100                 newdb.hdr.hash_seed = random_number(tdb);
101         newdb.hdr.hash_test = TDB_HASH_MAGIC;
102         newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
103                                          sizeof(newdb.hdr.hash_test),
104                                          newdb.hdr.hash_seed,
105                                          tdb->hash_priv);
106         newdb.hdr.recovery = 0;
107         memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
108         /* Initial hashes are empty. */
109         memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
110
111         /* Free is empty. */
112         newdb.hdr.free_list = offsetof(struct new_database, flist);
113         memset(&newdb.flist, 0, sizeof(newdb.flist));
114         set_used_header(NULL, &newdb.flist.hdr, 0,
115                         sizeof(newdb.flist) - sizeof(newdb.flist.hdr),
116                         sizeof(newdb.flist) - sizeof(newdb.flist.hdr), 1);
117
118         /* Magic food */
119         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
120         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
121
122         /* This creates an endian-converted database, as if read from disk */
123         magic_len = sizeof(newdb.hdr.magic_food);
124         tdb_convert(tdb,
125                     (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
126
127         *hdr = newdb.hdr;
128
129         if (tdb->flags & TDB_INTERNAL) {
130                 tdb->map_size = sizeof(newdb);
131                 tdb->map_ptr = malloc(tdb->map_size);
132                 if (!tdb->map_ptr) {
133                         tdb->ecode = TDB_ERR_OOM;
134                         return -1;
135                 }
136                 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
137                 return 0;
138         }
139         if (lseek(tdb->fd, 0, SEEK_SET) == -1)
140                 return -1;
141
142         if (ftruncate(tdb->fd, 0) == -1)
143                 return -1;
144
145         if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
146                 tdb->ecode = TDB_ERR_IO;
147                 return -1;
148         }
149         return 0;
150 }
151
152 struct tdb_context *tdb_open(const char *name, int tdb_flags,
153                              int open_flags, mode_t mode,
154                              union tdb_attribute *attr)
155 {
156         struct tdb_context *tdb;
157         struct stat st;
158         int save_errno;
159         uint64_t hash_test;
160         unsigned v;
161         struct tdb_header hdr;
162         struct tdb_attribute_seed *seed = NULL;
163
164         tdb = malloc(sizeof(*tdb));
165         if (!tdb) {
166                 /* Can't log this */
167                 errno = ENOMEM;
168                 goto fail;
169         }
170         tdb->name = NULL;
171         tdb->map_ptr = NULL;
172         tdb->direct_access = 0;
173         tdb->fd = -1;
174         tdb->map_size = sizeof(struct tdb_header);
175         tdb->ecode = TDB_SUCCESS;
176         tdb->flags = tdb_flags;
177         tdb->log = null_log_fn;
178         tdb->log_priv = NULL;
179         tdb->transaction = NULL;
180         tdb->stats = NULL;
181         tdb_hash_init(tdb);
182         tdb_io_init(tdb);
183         tdb_lock_init(tdb);
184
185         while (attr) {
186                 switch (attr->base.attr) {
187                 case TDB_ATTRIBUTE_LOG:
188                         tdb->log = attr->log.log_fn;
189                         tdb->log_priv = attr->log.log_private;
190                         break;
191                 case TDB_ATTRIBUTE_HASH:
192                         tdb->khash = attr->hash.hash_fn;
193                         tdb->hash_priv = attr->hash.hash_private;
194                         break;
195                 case TDB_ATTRIBUTE_SEED:
196                         seed = &attr->seed;
197                         break;
198                 case TDB_ATTRIBUTE_STATS:
199                         tdb->stats = &attr->stats;
200                         /* They have stats we don't know about?  Tell them. */
201                         if (tdb->stats->size > sizeof(attr->stats))
202                                 tdb->stats->size = sizeof(attr->stats);
203                         break;
204                 default:
205                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
206                                  "tdb_open: unknown attribute type %u\n",
207                                  attr->base.attr);
208                         errno = EINVAL;
209                         goto fail;
210                 }
211                 attr = attr->base.next;
212         }
213
214         if ((open_flags & O_ACCMODE) == O_WRONLY) {
215                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
216                          "tdb_open: can't open tdb %s write-only\n", name);
217                 errno = EINVAL;
218                 goto fail;
219         }
220
221         if ((open_flags & O_ACCMODE) == O_RDONLY) {
222                 tdb->read_only = true;
223                 /* read only databases don't do locking */
224                 tdb->flags |= TDB_NOLOCK;
225                 tdb->mmap_flags = PROT_READ;
226         } else {
227                 tdb->read_only = false;
228                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
229         }
230
231         /* internal databases don't need any of the rest. */
232         if (tdb->flags & TDB_INTERNAL) {
233                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
234                 if (tdb_new_database(tdb, seed, &hdr) != 0) {
235                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
236                                  "tdb_open: tdb_new_database failed!");
237                         goto fail;
238                 }
239                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
240                 tdb->hash_seed = hdr.hash_seed;
241                 tdb_flist_init(tdb);
242                 return tdb;
243         }
244
245         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
246                 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
247                          "tdb_open: could not open file %s: %s\n",
248                          name, strerror(errno));
249                 goto fail;      /* errno set by open(2) */
250         }
251
252         /* on exec, don't inherit the fd */
253         v = fcntl(tdb->fd, F_GETFD, 0);
254         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
255
256         /* ensure there is only one process initialising at once */
257         if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
258                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
259                          "tdb_open: failed to get open lock on %s: %s\n",
260                          name, strerror(errno));
261                 goto fail;      /* errno set by tdb_brlock */
262         }
263
264         if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
265             || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
266                 if (!(open_flags & O_CREAT)
267                     || tdb_new_database(tdb, seed, &hdr) == -1) {
268                         if (errno == 0) {
269                                 errno = EIO; /* ie bad format or something */
270                         }
271                         goto fail;
272                 }
273         } else if (hdr.version != TDB_VERSION) {
274                 if (hdr.version == bswap_64(TDB_VERSION))
275                         tdb->flags |= TDB_CONVERT;
276                 else {
277                         /* wrong version */
278                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
279                                  "tdb_open: %s is unknown version 0x%llx\n",
280                                  name, (long long)hdr.version);
281                         errno = EIO;
282                         goto fail;
283                 }
284         }
285
286         tdb_convert(tdb, &hdr, sizeof(hdr));
287         tdb->hash_seed = hdr.hash_seed;
288         hash_test = TDB_HASH_MAGIC;
289         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
290         if (hdr.hash_test != hash_test) {
291                 /* wrong hash variant */
292                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
293                          "tdb_open: %s uses a different hash function\n",
294                          name);
295                 errno = EIO;
296                 goto fail;
297         }
298
299         if (fstat(tdb->fd, &st) == -1)
300                 goto fail;
301
302         /* Is it already in the open list?  If so, fail. */
303         if (tdb_already_open(st.st_dev, st.st_ino)) {
304                 /* FIXME */
305                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
306                          "tdb_open: %s (%d,%d) is already open in this process\n",
307                          name, (int)st.st_dev, (int)st.st_ino);
308                 errno = EBUSY;
309                 goto fail;
310         }
311
312         tdb->name = strdup(name);
313         if (!tdb->name) {
314                 errno = ENOMEM;
315                 goto fail;
316         }
317
318         tdb->device = st.st_dev;
319         tdb->inode = st.st_ino;
320         tdb_unlock_open(tdb);
321
322         /* This make sure we have current map_size and mmap. */
323         tdb->methods->oob(tdb, tdb->map_size + 1, true);
324
325         /* Now it's fully formed, recover if necessary. */
326         if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
327                 errno = EIO;
328                 goto fail;
329         }
330
331         if (tdb_flist_init(tdb) == -1)
332                 goto fail;
333
334         tdb->next = tdbs;
335         tdbs = tdb;
336         return tdb;
337
338  fail:
339         save_errno = errno;
340
341         if (!tdb)
342                 return NULL;
343
344 #ifdef TDB_TRACE
345         close(tdb->tracefd);
346 #endif
347         if (tdb->map_ptr) {
348                 if (tdb->flags & TDB_INTERNAL) {
349                         free(tdb->map_ptr);
350                 } else
351                         tdb_munmap(tdb);
352         }
353         free((char *)tdb->name);
354         if (tdb->fd != -1)
355                 if (close(tdb->fd) != 0)
356                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
357                                  "tdb_open: failed to close tdb->fd"
358                                  " on error!\n");
359         free(tdb);
360         errno = save_errno;
361         return NULL;
362 }
363
364 /* FIXME: modify, don't rewrite! */
365 static int update_rec_hdr(struct tdb_context *tdb,
366                           tdb_off_t off,
367                           tdb_len_t keylen,
368                           tdb_len_t datalen,
369                           struct tdb_used_record *rec,
370                           uint64_t h)
371 {
372         uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
373
374         if (set_used_header(tdb, rec, keylen, datalen, keylen + dataroom, h))
375                 return -1;
376
377         return tdb_write_convert(tdb, off, rec, sizeof(*rec));
378 }
379
380 /* Returns -1 on error, 0 on OK */
381 static int replace_data(struct tdb_context *tdb,
382                         struct hash_info *h,
383                         struct tdb_data key, struct tdb_data dbuf,
384                         tdb_off_t old_off, tdb_len_t old_room,
385                         bool growing)
386 {
387         tdb_off_t new_off;
388
389         /* Allocate a new record. */
390         new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, growing);
391         if (unlikely(new_off == TDB_OFF_ERR))
392                 return -1;
393
394         /* We didn't like the existing one: remove it. */
395         if (old_off) {
396                 add_stat(tdb, frees, 1);
397                 add_free_record(tdb, old_off,
398                                 sizeof(struct tdb_used_record)
399                                 + key.dsize + old_room);
400                 if (replace_in_hash(tdb, h, new_off) == -1)
401                         return -1;
402         } else {
403                 if (add_to_hash(tdb, h, new_off) == -1)
404                         return -1;
405         }
406
407         new_off += sizeof(struct tdb_used_record);
408         if (tdb->methods->write(tdb, new_off, key.dptr, key.dsize) == -1)
409                 return -1;
410
411         new_off += key.dsize;
412         if (tdb->methods->write(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
413                 return -1;
414
415         /* FIXME: tdb_increment_seqnum(tdb); */
416         return 0;
417 }
418
419 int tdb_store(struct tdb_context *tdb,
420               struct tdb_data key, struct tdb_data dbuf, int flag)
421 {
422         struct hash_info h;
423         tdb_off_t off;
424         tdb_len_t old_room = 0;
425         struct tdb_used_record rec;
426         int ret;
427
428         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
429         if (unlikely(off == TDB_OFF_ERR))
430                 return -1;
431
432         /* Now we have lock on this hash bucket. */
433         if (flag == TDB_INSERT) {
434                 if (off) {
435                         tdb->ecode = TDB_ERR_EXISTS;
436                         goto fail;
437                 }
438         } else {
439                 if (off) {
440                         old_room = rec_data_length(&rec)
441                                 + rec_extra_padding(&rec);
442                         if (old_room >= dbuf.dsize) {
443                                 /* Can modify in-place.  Easy! */
444                                 if (update_rec_hdr(tdb, off,
445                                                    key.dsize, dbuf.dsize,
446                                                    &rec, h.h))
447                                         goto fail;
448                                 if (tdb->methods->write(tdb, off + sizeof(rec)
449                                                         + key.dsize,
450                                                         dbuf.dptr, dbuf.dsize))
451                                         goto fail;
452                                 tdb_unlock_hashes(tdb, h.hlock_start,
453                                                   h.hlock_range, F_WRLCK);
454                                 return 0;
455                         }
456                         /* FIXME: See if right record is free? */
457                 } else {
458                         if (flag == TDB_MODIFY) {
459                                 /* if the record doesn't exist and we
460                                    are in TDB_MODIFY mode then we should fail
461                                    the store */
462                                 tdb->ecode = TDB_ERR_NOEXIST;
463                                 goto fail;
464                         }
465                 }
466         }
467
468         /* If we didn't use the old record, this implies we're growing. */
469         ret = replace_data(tdb, &h, key, dbuf, off, old_room, off != 0);
470         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
471         return ret;
472
473 fail:
474         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
475         return -1;
476 }
477
478 int tdb_append(struct tdb_context *tdb,
479                struct tdb_data key, struct tdb_data dbuf)
480 {
481         struct hash_info h;
482         tdb_off_t off;
483         struct tdb_used_record rec;
484         tdb_len_t old_room = 0, old_dlen;
485         unsigned char *newdata;
486         struct tdb_data new_dbuf;
487         int ret;
488
489         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
490         if (unlikely(off == TDB_OFF_ERR))
491                 return -1;
492
493         if (off) {
494                 old_dlen = rec_data_length(&rec);
495                 old_room = old_dlen + rec_extra_padding(&rec);
496
497                 /* Fast path: can append in place. */
498                 if (rec_extra_padding(&rec) >= dbuf.dsize) {
499                         if (update_rec_hdr(tdb, off, key.dsize,
500                                            old_dlen + dbuf.dsize, &rec, h.h))
501                                 goto fail;
502
503                         off += sizeof(rec) + key.dsize + old_dlen;
504                         if (tdb->methods->write(tdb, off, dbuf.dptr,
505                                                 dbuf.dsize) == -1)
506                                 goto fail;
507
508                         /* FIXME: tdb_increment_seqnum(tdb); */
509                         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
510                                           F_WRLCK);
511                         return 0;
512                 }
513                 /* FIXME: Check right record free? */
514
515                 /* Slow path. */
516                 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
517                 if (!newdata) {
518                         tdb->ecode = TDB_ERR_OOM;
519                         tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
520                                  "tdb_append: cannot allocate %llu bytes!\n",
521                                  (long long)key.dsize + old_dlen + dbuf.dsize);
522                         goto fail;
523                 }
524                 if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
525                                        newdata, old_dlen) != 0) {
526                         free(newdata);
527                         goto fail;
528                 }
529                 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
530                 new_dbuf.dptr = newdata;
531                 new_dbuf.dsize = old_dlen + dbuf.dsize;
532         } else {
533                 newdata = NULL;
534                 new_dbuf = dbuf;
535         }
536
537         /* If they're using tdb_append(), it implies they're growing record. */
538         ret = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
539         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
540         free(newdata);
541
542         return ret;
543
544 fail:
545         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
546         return -1;
547 }
548
549 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
550 {
551         tdb_off_t off;
552         struct tdb_used_record rec;
553         struct hash_info h;
554         struct tdb_data ret;
555
556         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
557         if (unlikely(off == TDB_OFF_ERR))
558                 return tdb_null;
559
560         if (!off) {
561                 tdb->ecode = TDB_ERR_NOEXIST;
562                 ret = tdb_null;
563         } else {
564                 ret.dsize = rec_data_length(&rec);
565                 ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
566                                           ret.dsize);
567         }
568
569         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
570         return ret;
571 }
572
573 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
574 {
575         tdb_off_t off;
576         struct tdb_used_record rec;
577         struct hash_info h;
578
579         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
580         if (unlikely(off == TDB_OFF_ERR))
581                 return -1;
582
583         if (!off) {
584                 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
585                 tdb->ecode = TDB_ERR_NOEXIST;
586                 return -1;
587         }
588
589         if (delete_from_hash(tdb, &h) == -1)
590                 goto unlock_err;
591
592         /* Free the deleted entry. */
593         add_stat(tdb, frees, 1);
594         if (add_free_record(tdb, off,
595                             sizeof(struct tdb_used_record)
596                             + rec_key_length(&rec)
597                             + rec_data_length(&rec)
598                             + rec_extra_padding(&rec)) != 0)
599                 goto unlock_err;
600
601         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
602         return 0;
603
604 unlock_err:
605         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
606         return -1;
607 }
608
609 int tdb_close(struct tdb_context *tdb)
610 {
611         struct tdb_context **i;
612         int ret = 0;
613
614         tdb_trace(tdb, "tdb_close");
615
616         if (tdb->transaction) {
617                 tdb_transaction_cancel(tdb);
618         }
619
620         if (tdb->map_ptr) {
621                 if (tdb->flags & TDB_INTERNAL)
622                         free(tdb->map_ptr);
623                 else
624                         tdb_munmap(tdb);
625         }
626         free((char *)tdb->name);
627         if (tdb->fd != -1) {
628                 ret = close(tdb->fd);
629                 tdb->fd = -1;
630         }
631         free(tdb->lockrecs);
632
633         /* Remove from contexts list */
634         for (i = &tdbs; *i; i = &(*i)->next) {
635                 if (*i == tdb) {
636                         *i = tdb->next;
637                         break;
638                 }
639         }
640
641 #ifdef TDB_TRACE
642         close(tdb->tracefd);
643 #endif
644         free(tdb);
645
646         return ret;
647 }
648
649 enum TDB_ERROR tdb_error(struct tdb_context *tdb)
650 {
651         return tdb->ecode;
652 }
653
654 const char *tdb_errorstr(struct tdb_context *tdb)
655 {
656         /* Gcc warns if you miss a case in the switch, so use that. */
657         switch (tdb->ecode) {
658         case TDB_SUCCESS: return "Success";
659         case TDB_ERR_CORRUPT: return "Corrupt database";
660         case TDB_ERR_IO: return "IO Error";
661         case TDB_ERR_LOCK: return "Locking error";
662         case TDB_ERR_OOM: return "Out of memory";
663         case TDB_ERR_EXISTS: return "Record exists";
664         case TDB_ERR_NESTING: return "Transaction already started";
665         case TDB_ERR_EINVAL: return "Invalid parameter";
666         case TDB_ERR_NOEXIST: return "Record does not exist";
667         case TDB_ERR_RDONLY: return "write not permitted";
668         }
669         return "Invalid error code";
670 }