2 Unix SMB/CIFS implementation.
3 Samba database functions
4 Copyright (C) Andrew Tridgell 1999-2000
5 Copyright (C) Luke Kenneth Casson Leighton 2000
6 Copyright (C) Paul `Rusty' Russell 2000
7 Copyright (C) Jeremy Allison 2000-2003
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* NOTE: If you use tdbs under valgrind, and in particular if you run
26 * tdbtorture, you may get spurious "uninitialized value" warnings. I
27 * think this is because valgrind doesn't understand that the mmap'd
28 * area may be written to by other processes. Memory can, from the
29 * point of view of the grinded process, spontaneously become
32 * I can think of a few solutions. [mbp 20030311]
34 * 1 - Write suppressions for Valgrind so that it doesn't complain
35 * about this. Probably the most reasonable but people need to
36 * remember to use them.
38 * 2 - Use IO not mmap when running under valgrind. Not so nice.
40 * 3 - Use the special valgrind macros to mark memory as valid at the
41 * right time. Probably too hard -- the process just doesn't know.
55 #define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
68 #include "../udev_lib.h"
73 #define TDB_MAGIC_FOOD "TDB file\n"
74 #define TDB_VERSION (0x26011967 + 6)
75 #define TDB_MAGIC (0x26011999U)
76 #define TDB_FREE_MAGIC (~TDB_MAGIC)
77 #define TDB_DEAD_MAGIC (0xFEE1DEAD)
78 #define TDB_ALIGNMENT 4
79 #define MIN_REC_SIZE (2*sizeof(struct list_struct) + TDB_ALIGNMENT)
80 #define DEFAULT_HASH_SIZE 131
81 #define TDB_PAGE_SIZE 0x2000
82 #define FREELIST_TOP (sizeof(struct tdb_header))
83 #define TDB_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
84 #define TDB_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
85 #define TDB_DEAD(r) ((r)->magic == TDB_DEAD_MAGIC)
86 #define TDB_BAD_MAGIC(r) ((r)->magic != TDB_MAGIC && !TDB_DEAD(r))
87 #define TDB_HASH_TOP(hash) (FREELIST_TOP + (BUCKET(hash)+1)*sizeof(tdb_off))
89 /* NB assumes there is a local variable called "tdb" that is the
90 * current context, also takes doubly-parenthesized print-style
92 #define TDB_LOG(x) (tdb->log_fn?((tdb->log_fn x),0) : 0)
103 #define MAP_FAILED ((void *)-1)
106 /* free memory if the pointer is valid and zero the pointer */
108 #define SAFE_FREE(x) do { if ((x) != NULL) {free((x)); (x)=NULL;} } while(0)
111 #define BUCKET(hash) ((hash) % tdb->header.hash_size)
114 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
115 static TDB_CONTEXT *tdbs = NULL;
117 static int tdb_munmap(TDB_CONTEXT *tdb)
119 if (tdb->flags & TDB_INTERNAL)
124 int ret = munmap(tdb->map_ptr, tdb->map_size);
133 static void tdb_mmap(TDB_CONTEXT *tdb)
135 if (tdb->flags & TDB_INTERNAL)
139 if (!(tdb->flags & TDB_NOMMAP)) {
140 tdb->map_ptr = mmap(NULL, tdb->map_size,
141 PROT_READ|(tdb->read_only? 0:PROT_WRITE),
142 MAP_SHARED|MAP_FILE, tdb->fd, 0);
145 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
148 if (tdb->map_ptr == MAP_FAILED) {
150 TDB_LOG((tdb, 2, "tdb_mmap failed for size %d (%s)\n",
151 tdb->map_size, strerror(errno)));
161 /* Endian conversion: we only ever deal with 4 byte quantities */
162 static void *convert(void *buf, u32 size)
165 for (i = 0; i < size / 4; i++)
166 p[i] = TDB_BYTEREV(p[i]);
169 #define DOCONV() (tdb->flags & TDB_CONVERT)
170 #define CONVERT(x) (DOCONV() ? convert(&x, sizeof(x)) : &x)
172 /* the body of the database is made of one list_struct for the free space
173 plus a separate data list for each hash value */
175 tdb_off next; /* offset of the next record in the list */
176 tdb_len rec_len; /* total byte length of record */
177 tdb_len key_len; /* byte length of key */
178 tdb_len data_len; /* byte length of data */
179 u32 full_hash; /* the full 32 bit hash of the key */
180 u32 magic; /* try to catch errors */
181 /* the following union is implied:
183 char record[rec_len];
188 u32 totalsize; (tailer)
193 /***************************************************************
194 Allow a caller to set a "alarm" flag that tdb can check to abort
195 a blocking lock on SIGALRM.
196 ***************************************************************/
198 static sig_atomic_t *palarm_fired;
200 void tdb_set_lock_alarm(sig_atomic_t *palarm)
202 palarm_fired = palarm;
205 /* a byte range locking function - return 0 on success
206 this functions locks/unlocks 1 byte at the specified offset.
208 On error, errno is also set so that errors are passed back properly
209 through tdb_open(). */
210 static int tdb_brlock(TDB_CONTEXT *tdb, tdb_off offset,
211 int rw_type, int lck_type, int probe)
216 if (tdb->flags & TDB_NOLOCK)
218 if ((rw_type == F_WRLCK) && (tdb->read_only)) {
224 fl.l_whence = SEEK_SET;
230 ret = fcntl(tdb->fd,lck_type,&fl);
231 if (ret == -1 && errno == EINTR && palarm_fired && *palarm_fired)
233 } while (ret == -1 && errno == EINTR);
236 if (!probe && lck_type != F_SETLK) {
237 /* Ensure error code is set for log fun to examine. */
238 if (errno == EINTR && palarm_fired && *palarm_fired)
239 tdb->ecode = TDB_ERR_LOCK_TIMEOUT;
241 tdb->ecode = TDB_ERR_LOCK;
242 TDB_LOG((tdb, 5,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d\n",
243 tdb->fd, offset, rw_type, lck_type));
245 /* Was it an alarm timeout ? */
246 if (errno == EINTR && palarm_fired && *palarm_fired)
247 return TDB_ERRCODE(TDB_ERR_LOCK_TIMEOUT, -1);
248 /* Otherwise - generic lock error. */
249 /* errno set by fcntl */
250 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
255 /* lock a list in the database. list -1 is the alloc list */
256 static int tdb_lock(TDB_CONTEXT *tdb, int list, int ltype)
258 if (list < -1 || list >= (int)tdb->header.hash_size) {
259 TDB_LOG((tdb, 0,"tdb_lock: invalid list %d for ltype=%d\n",
263 if (tdb->flags & TDB_NOLOCK)
266 /* Since fcntl locks don't nest, we do a lock for the first one,
267 and simply bump the count for future ones */
268 if (tdb->locked[list+1].count == 0) {
269 if (!tdb->read_only && tdb->header.rwlocks) {
270 if (tdb_spinlock(tdb, list, ltype)) {
271 TDB_LOG((tdb, 0, "tdb_lock spinlock failed on list ltype=%d\n",
275 } else if (tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW, 0)) {
276 TDB_LOG((tdb, 0,"tdb_lock failed on list %d ltype=%d (%s)\n",
277 list, ltype, strerror(errno)));
280 tdb->locked[list+1].ltype = ltype;
282 tdb->locked[list+1].count++;
286 /* unlock the database: returns void because it's too late for errors. */
287 /* changed to return int it may be interesting to know there
288 has been an error --simo */
289 static int tdb_unlock(TDB_CONTEXT *tdb, int list, int ltype)
293 if (tdb->flags & TDB_NOLOCK)
297 if (list < -1 || list >= (int)tdb->header.hash_size) {
298 TDB_LOG((tdb, 0, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
302 if (tdb->locked[list+1].count==0) {
303 TDB_LOG((tdb, 0, "tdb_unlock: count is 0\n"));
307 if (tdb->locked[list+1].count == 1) {
308 /* Down to last nested lock: unlock underneath */
309 if (!tdb->read_only && tdb->header.rwlocks) {
310 ret = tdb_spinunlock(tdb, list, ltype);
312 ret = tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK, F_SETLKW, 0);
317 tdb->locked[list+1].count--;
320 TDB_LOG((tdb, 0,"tdb_unlock: An error occurred unlocking!\n"));
324 /* This is based on the hash algorithm from gdbm */
325 static u32 tdb_hash(TDB_DATA *key)
327 u32 value; /* Used to compute the hash value. */
328 u32 i; /* Used to cycle through random values. */
330 /* Set the initial value from the key size. */
331 for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
332 value = (value + (key->dptr[i] << (i*5 % 24)));
334 return (1103515243 * value + 12345);
337 /* check for an out of bounds access - if it is out of bounds then
338 see if the database has been expanded by someone else and expand
340 note that "len" is the minimum length needed for the db
342 static int tdb_oob(TDB_CONTEXT *tdb, tdb_off len, int probe)
345 if (len <= tdb->map_size)
347 if (tdb->flags & TDB_INTERNAL) {
349 /* Ensure ecode is set for log fn. */
350 tdb->ecode = TDB_ERR_IO;
351 TDB_LOG((tdb, 0,"tdb_oob len %d beyond internal malloc size %d\n",
352 (int)len, (int)tdb->map_size));
354 return TDB_ERRCODE(TDB_ERR_IO, -1);
357 if (fstat(tdb->fd, &st) == -1)
358 return TDB_ERRCODE(TDB_ERR_IO, -1);
360 if (st.st_size < (size_t)len) {
362 /* Ensure ecode is set for log fn. */
363 tdb->ecode = TDB_ERR_IO;
364 TDB_LOG((tdb, 0,"tdb_oob len %d beyond eof at %d\n",
365 (int)len, (int)st.st_size));
367 return TDB_ERRCODE(TDB_ERR_IO, -1);
370 /* Unmap, update size, remap */
371 if (tdb_munmap(tdb) == -1)
372 return TDB_ERRCODE(TDB_ERR_IO, -1);
373 tdb->map_size = st.st_size;
378 /* write a lump of data at a specified offset */
379 static int tdb_write(TDB_CONTEXT *tdb, tdb_off off, void *buf, tdb_len len)
381 if (tdb_oob(tdb, off + len, 0) != 0)
385 memcpy(off + (char *)tdb->map_ptr, buf, len);
387 else if (pwrite(tdb->fd, buf, len, off) != (ssize_t)len) {
389 else if (lseek(tdb->fd, off, SEEK_SET) != off
390 || write(tdb->fd, buf, len) != (ssize_t)len) {
392 /* Ensure ecode is set for log fn. */
393 tdb->ecode = TDB_ERR_IO;
394 TDB_LOG((tdb, 0,"tdb_write failed at %d len=%d (%s)\n",
395 off, len, strerror(errno)));
396 return TDB_ERRCODE(TDB_ERR_IO, -1);
401 /* read a lump of data at a specified offset, maybe convert */
402 static int tdb_read(TDB_CONTEXT *tdb,tdb_off off,void *buf,tdb_len len,int cv)
404 if (tdb_oob(tdb, off + len, 0) != 0)
408 memcpy(buf, off + (char *)tdb->map_ptr, len);
410 else if (pread(tdb->fd, buf, len, off) != (ssize_t)len) {
412 else if (lseek(tdb->fd, off, SEEK_SET) != off
413 || read(tdb->fd, buf, len) != (ssize_t)len) {
415 /* Ensure ecode is set for log fn. */
416 tdb->ecode = TDB_ERR_IO;
417 TDB_LOG((tdb, 0,"tdb_read failed at %d len=%d (%s)\n",
418 off, len, strerror(errno)));
419 return TDB_ERRCODE(TDB_ERR_IO, -1);
426 /* read a lump of data, allocating the space for it */
427 static char *tdb_alloc_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_len len)
431 if (!(buf = malloc(len))) {
432 /* Ensure ecode is set for log fn. */
433 tdb->ecode = TDB_ERR_OOM;
434 TDB_LOG((tdb, 0,"tdb_alloc_read malloc failed len=%d (%s)\n",
435 len, strerror(errno)));
436 return TDB_ERRCODE(TDB_ERR_OOM, buf);
438 if (tdb_read(tdb, offset, buf, len, 0) == -1) {
445 /* read/write a tdb_off */
446 static int ofs_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d)
448 return tdb_read(tdb, offset, (char*)d, sizeof(*d), DOCONV());
450 static int ofs_write(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d)
453 return tdb_write(tdb, offset, CONVERT(off), sizeof(*d));
456 /* read/write a record */
457 static int rec_read(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
459 if (tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
461 if (TDB_BAD_MAGIC(rec)) {
462 /* Ensure ecode is set for log fn. */
463 tdb->ecode = TDB_ERR_CORRUPT;
464 TDB_LOG((tdb, 0,"rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
465 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
467 return tdb_oob(tdb, rec->next+sizeof(*rec), 0);
469 static int rec_write(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
471 struct list_struct r = *rec;
472 return tdb_write(tdb, offset, CONVERT(r), sizeof(r));
475 /* read a freelist record and check for simple errors */
476 static int rec_free_read(TDB_CONTEXT *tdb, tdb_off off, struct list_struct *rec)
478 if (tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
481 if (rec->magic == TDB_MAGIC) {
482 /* this happens when a app is showdown while deleting a record - we should
483 not completely fail when this happens */
484 TDB_LOG((tdb, 0,"rec_free_read non-free magic at offset=%d - fixing\n",
486 rec->magic = TDB_FREE_MAGIC;
487 if (tdb_write(tdb, off, rec, sizeof(*rec)) == -1)
491 if (rec->magic != TDB_FREE_MAGIC) {
492 /* Ensure ecode is set for log fn. */
493 tdb->ecode = TDB_ERR_CORRUPT;
494 TDB_LOG((tdb, 0,"rec_free_read bad magic 0x%x at offset=%d\n",
496 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
498 if (tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
503 /* update a record tailer (must hold allocation lock) */
504 static int update_tailer(TDB_CONTEXT *tdb, tdb_off offset,
505 const struct list_struct *rec)
509 /* Offset of tailer from record header */
510 totalsize = sizeof(*rec) + rec->rec_len;
511 return ofs_write(tdb, offset + totalsize - sizeof(tdb_off),
515 static tdb_off tdb_dump_record(TDB_CONTEXT *tdb, tdb_off offset)
517 struct list_struct rec;
518 tdb_off tailer_ofs, tailer;
520 if (tdb_read(tdb, offset, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
521 printf("ERROR: failed to read record at %u\n", offset);
525 printf(" rec: offset=%u next=%d rec_len=%d key_len=%d data_len=%d full_hash=0x%x magic=0x%x\n",
526 offset, rec.next, rec.rec_len, rec.key_len, rec.data_len, rec.full_hash, rec.magic);
528 tailer_ofs = offset + sizeof(rec) + rec.rec_len - sizeof(tdb_off);
529 if (ofs_read(tdb, tailer_ofs, &tailer) == -1) {
530 printf("ERROR: failed to read tailer at %u\n", tailer_ofs);
534 if (tailer != rec.rec_len + sizeof(rec)) {
535 printf("ERROR: tailer does not match record! tailer=%u totalsize=%u\n",
536 (unsigned)tailer, (unsigned)(rec.rec_len + sizeof(rec)));
541 static int tdb_dump_chain(TDB_CONTEXT *tdb, int i)
543 tdb_off rec_ptr, top;
545 top = TDB_HASH_TOP(i);
547 if (tdb_lock(tdb, i, F_WRLCK) != 0)
550 if (ofs_read(tdb, top, &rec_ptr) == -1)
551 return tdb_unlock(tdb, i, F_WRLCK);
554 printf("hash=%d\n", i);
557 rec_ptr = tdb_dump_record(tdb, rec_ptr);
560 return tdb_unlock(tdb, i, F_WRLCK);
563 void tdb_dump_all(TDB_CONTEXT *tdb)
566 for (i=0;i<tdb->header.hash_size;i++) {
567 tdb_dump_chain(tdb, i);
569 printf("freelist:\n");
570 tdb_dump_chain(tdb, -1);
573 int tdb_printfreelist(TDB_CONTEXT *tdb)
577 tdb_off offset, rec_ptr;
578 struct list_struct rec;
580 if ((ret = tdb_lock(tdb, -1, F_WRLCK)) != 0)
583 offset = FREELIST_TOP;
585 /* read in the freelist top */
586 if (ofs_read(tdb, offset, &rec_ptr) == -1) {
587 tdb_unlock(tdb, -1, F_WRLCK);
591 printf("freelist top=[0x%08x]\n", rec_ptr );
593 if (tdb_read(tdb, rec_ptr, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
594 tdb_unlock(tdb, -1, F_WRLCK);
598 if (rec.magic != TDB_FREE_MAGIC) {
599 printf("bad magic 0x%08x in free list\n", rec.magic);
600 tdb_unlock(tdb, -1, F_WRLCK);
604 printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%d)]\n", rec.next, rec.rec_len, rec.rec_len );
605 total_free += rec.rec_len;
607 /* move to the next record */
610 printf("total rec_len = [0x%08x (%d)]\n", (int)total_free,
613 return tdb_unlock(tdb, -1, F_WRLCK);
616 /* Remove an element from the freelist. Must have alloc lock. */
617 static int remove_from_freelist(TDB_CONTEXT *tdb, tdb_off off, tdb_off next)
621 /* read in the freelist top */
622 last_ptr = FREELIST_TOP;
623 while (ofs_read(tdb, last_ptr, &i) != -1 && i != 0) {
625 /* We've found it! */
626 return ofs_write(tdb, last_ptr, &next);
628 /* Follow chain (next offset is at start of record) */
631 TDB_LOG((tdb, 0,"remove_from_freelist: not on list at off=%d\n", off));
632 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
635 /* Add an element into the freelist. Merge adjacent records if
637 static int tdb_free(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
641 /* Allocation and tailer lock */
642 if (tdb_lock(tdb, -1, F_WRLCK) != 0)
645 /* set an initial tailer, so if we fail we don't leave a bogus record */
646 if (update_tailer(tdb, offset, rec) != 0) {
647 TDB_LOG((tdb, 0, "tdb_free: upfate_tailer failed!\n"));
651 /* Look right first (I'm an Australian, dammit) */
652 right = offset + sizeof(*rec) + rec->rec_len;
653 if (right + sizeof(*rec) <= tdb->map_size) {
654 struct list_struct r;
656 if (tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
657 TDB_LOG((tdb, 0, "tdb_free: right read failed at %u\n", right));
661 /* If it's free, expand to include it. */
662 if (r.magic == TDB_FREE_MAGIC) {
663 if (remove_from_freelist(tdb, right, r.next) == -1) {
664 TDB_LOG((tdb, 0, "tdb_free: right free failed at %u\n", right));
667 rec->rec_len += sizeof(r) + r.rec_len;
673 left = offset - sizeof(tdb_off);
674 if (left > TDB_HASH_TOP(tdb->header.hash_size-1)) {
675 struct list_struct l;
678 /* Read in tailer and jump back to header */
679 if (ofs_read(tdb, left, &leftsize) == -1) {
680 TDB_LOG((tdb, 0, "tdb_free: left offset read failed at %u\n", left));
683 left = offset - leftsize;
685 /* Now read in record */
686 if (tdb_read(tdb, left, &l, sizeof(l), DOCONV()) == -1) {
687 TDB_LOG((tdb, 0, "tdb_free: left read failed at %u (%u)\n", left, leftsize));
691 /* If it's free, expand to include it. */
692 if (l.magic == TDB_FREE_MAGIC) {
693 if (remove_from_freelist(tdb, left, l.next) == -1) {
694 TDB_LOG((tdb, 0, "tdb_free: left free failed at %u\n", left));
698 rec->rec_len += leftsize;
704 if (update_tailer(tdb, offset, rec) == -1) {
705 TDB_LOG((tdb, 0, "tdb_free: update_tailer failed at %u\n", offset));
709 /* Now, prepend to free list */
710 rec->magic = TDB_FREE_MAGIC;
712 if (ofs_read(tdb, FREELIST_TOP, &rec->next) == -1 ||
713 rec_write(tdb, offset, rec) == -1 ||
714 ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
715 TDB_LOG((tdb, 0, "tdb_free record write failed at offset=%d\n", offset));
719 /* And we're done. */
720 tdb_unlock(tdb, -1, F_WRLCK);
724 tdb_unlock(tdb, -1, F_WRLCK);
729 /* expand a file. we prefer to use ftruncate, as that is what posix
730 says to use for mmap expansion */
731 static int expand_file(TDB_CONTEXT *tdb, tdb_off size, tdb_off addition)
734 #if HAVE_FTRUNCATE_EXTEND
735 if (ftruncate(tdb->fd, size+addition) != 0) {
736 TDB_LOG((tdb, 0, "expand_file ftruncate to %d failed (%s)\n",
737 size+addition, strerror(errno)));
744 if (pwrite(tdb->fd, &b, 1, (size+addition) - 1) != 1) {
746 if (lseek(tdb->fd, (size+addition) - 1, SEEK_SET) != (size+addition) - 1 ||
747 write(tdb->fd, &b, 1) != 1) {
749 TDB_LOG((tdb, 0, "expand_file to %d failed (%s)\n",
750 size+addition, strerror(errno)));
755 /* now fill the file with something. This ensures that the file isn't sparse, which would be
756 very bad if we ran out of disk. This must be done with write, not via mmap */
757 memset(buf, 0x42, sizeof(buf));
759 int n = addition>sizeof(buf)?sizeof(buf):addition;
761 int ret = pwrite(tdb->fd, buf, n, size);
764 if (lseek(tdb->fd, size, SEEK_SET) != size)
766 ret = write(tdb->fd, buf, n);
769 TDB_LOG((tdb, 0, "expand_file write of %d failed (%s)\n",
770 n, strerror(errno)));
780 /* expand the database at least size bytes by expanding the underlying
781 file and doing the mmap again if necessary */
782 static int tdb_expand(TDB_CONTEXT *tdb, tdb_off size)
784 struct list_struct rec;
787 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
788 TDB_LOG((tdb, 0, "lock failed in tdb_expand\n"));
792 /* must know about any previous expansions by another process */
793 tdb_oob(tdb, tdb->map_size + 1, 1);
795 /* always make room for at least 10 more records, and round
796 the database up to a multiple of TDB_PAGE_SIZE */
797 size = TDB_ALIGN(tdb->map_size + size*10, TDB_PAGE_SIZE) - tdb->map_size;
799 if (!(tdb->flags & TDB_INTERNAL))
803 * We must ensure the file is unmapped before doing this
804 * to ensure consistency with systems like OpenBSD where
805 * writes and mmaps are not consistent.
808 /* expand the file itself */
809 if (!(tdb->flags & TDB_INTERNAL)) {
810 if (expand_file(tdb, tdb->map_size, size) != 0)
814 tdb->map_size += size;
816 if (tdb->flags & TDB_INTERNAL)
817 tdb->map_ptr = realloc(tdb->map_ptr, tdb->map_size);
820 * We must ensure the file is remapped before adding the space
821 * to ensure consistency with systems like OpenBSD where
822 * writes and mmaps are not consistent.
825 /* We're ok if the mmap fails as we'll fallback to read/write */
829 /* form a new freelist record */
830 memset(&rec,'\0',sizeof(rec));
831 rec.rec_len = size - sizeof(rec);
833 /* link it into the free list */
834 offset = tdb->map_size - size;
835 if (tdb_free(tdb, offset, &rec) == -1)
838 tdb_unlock(tdb, -1, F_WRLCK);
841 tdb_unlock(tdb, -1, F_WRLCK);
845 /* allocate some space from the free list. The offset returned points
846 to a unconnected list_struct within the database with room for at
847 least length bytes of total data
849 0 is returned if the space could not be allocated
851 static tdb_off tdb_allocate(TDB_CONTEXT *tdb, tdb_len length,
852 struct list_struct *rec)
854 tdb_off rec_ptr, last_ptr, newrec_ptr;
855 struct list_struct newrec;
857 if (tdb_lock(tdb, -1, F_WRLCK) == -1)
860 /* Extra bytes required for tailer */
861 length += sizeof(tdb_off);
864 last_ptr = FREELIST_TOP;
866 /* read in the freelist top */
867 if (ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1)
870 /* keep looking until we find a freelist record big enough */
872 if (rec_free_read(tdb, rec_ptr, rec) == -1)
875 if (rec->rec_len >= length) {
876 /* found it - now possibly split it up */
877 if (rec->rec_len > length + MIN_REC_SIZE) {
878 /* Length of left piece */
879 length = TDB_ALIGN(length, TDB_ALIGNMENT);
881 /* Right piece to go on free list */
882 newrec.rec_len = rec->rec_len
883 - (sizeof(*rec) + length);
884 newrec_ptr = rec_ptr + sizeof(*rec) + length;
886 /* And left record is shortened */
887 rec->rec_len = length;
891 /* Remove allocated record from the free list */
892 if (ofs_write(tdb, last_ptr, &rec->next) == -1)
895 /* Update header: do this before we drop alloc
896 lock, otherwise tdb_free() might try to
897 merge with us, thinking we're free.
898 (Thanks Jeremy Allison). */
899 rec->magic = TDB_MAGIC;
900 if (rec_write(tdb, rec_ptr, rec) == -1)
903 /* Did we create new block? */
905 /* Update allocated record tailer (we
907 if (update_tailer(tdb, rec_ptr, rec) == -1)
910 /* Free new record */
911 if (tdb_free(tdb, newrec_ptr, &newrec) == -1)
915 /* all done - return the new record offset */
916 tdb_unlock(tdb, -1, F_WRLCK);
919 /* move to the next record */
923 /* we didn't find enough space. See if we can expand the
924 database and if we can then try again */
925 if (tdb_expand(tdb, length + sizeof(*rec)) == 0)
928 tdb_unlock(tdb, -1, F_WRLCK);
932 /* initialise a new database with a specified hash size */
933 static int tdb_new_database(TDB_CONTEXT *tdb, int hash_size)
935 struct tdb_header *newdb;
938 /* We make it up in memory, then write it out if not internal */
939 size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off);
940 if (!(newdb = calloc(size, 1)))
941 return TDB_ERRCODE(TDB_ERR_OOM, -1);
943 /* Fill in the header */
944 newdb->version = TDB_VERSION;
945 newdb->hash_size = hash_size;
947 newdb->rwlocks = size;
949 if (tdb->flags & TDB_INTERNAL) {
950 tdb->map_size = size;
951 tdb->map_ptr = (char *)newdb;
952 memcpy(&tdb->header, newdb, sizeof(tdb->header));
953 /* Convert the `ondisk' version if asked. */
957 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
960 if (ftruncate(tdb->fd, 0) == -1)
963 /* This creates an endian-converted header, as if read from disk */
965 memcpy(&tdb->header, newdb, sizeof(tdb->header));
966 /* Don't endian-convert the magic food! */
967 memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
968 if (write(tdb->fd, newdb, size) != size)
971 ret = tdb_create_rwlocks(tdb->fd, hash_size);
978 /* Returns 0 on fail. On success, return offset of record, and fills
980 static tdb_off tdb_find(TDB_CONTEXT *tdb, TDB_DATA key, u32 hash,
981 struct list_struct *r)
985 /* read in the hash top */
986 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
989 /* keep looking until we find the right record */
991 if (rec_read(tdb, rec_ptr, r) == -1)
994 if (!TDB_DEAD(r) && hash==r->full_hash && key.dsize==r->key_len) {
996 /* a very likely hit - read the key */
997 k = tdb_alloc_read(tdb, rec_ptr + sizeof(*r),
1002 if (memcmp(key.dptr, k, key.dsize) == 0) {
1010 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
1013 /* If they do lockkeys, check that this hash is one they locked */
1014 static int tdb_keylocked(TDB_CONTEXT *tdb, u32 hash)
1017 if (!tdb->lockedkeys)
1019 for (i = 0; i < tdb->lockedkeys[0]; i++)
1020 if (tdb->lockedkeys[i+1] == hash)
1022 return TDB_ERRCODE(TDB_ERR_NOLOCK, 0);
1025 /* As tdb_find, but if you succeed, keep the lock */
1026 static tdb_off tdb_find_lock(TDB_CONTEXT *tdb, TDB_DATA key, int locktype,
1027 struct list_struct *rec)
1031 hash = tdb_hash(&key);
1032 if (!tdb_keylocked(tdb, hash))
1034 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
1036 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
1037 tdb_unlock(tdb, BUCKET(hash), locktype);
1041 enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb)
1046 static struct tdb_errname {
1047 enum TDB_ERROR ecode; const char *estring;
1048 } emap[] = { {TDB_SUCCESS, "Success"},
1049 {TDB_ERR_CORRUPT, "Corrupt database"},
1050 {TDB_ERR_IO, "IO Error"},
1051 {TDB_ERR_LOCK, "Locking error"},
1052 {TDB_ERR_OOM, "Out of memory"},
1053 {TDB_ERR_EXISTS, "Record exists"},
1054 {TDB_ERR_NOLOCK, "Lock exists on other keys"},
1055 {TDB_ERR_NOEXIST, "Record does not exist"} };
1057 /* Error string for the last tdb error */
1058 const char *tdb_errorstr(TDB_CONTEXT *tdb)
1061 for (i = 0; i < sizeof(emap) / sizeof(struct tdb_errname); i++)
1062 if (tdb->ecode == emap[i].ecode)
1063 return emap[i].estring;
1064 return "Invalid error code";
1067 /* update an entry in place - this only works if the new data size
1068 is <= the old data size and the key exists.
1069 on failure return -1.
1072 static int tdb_update(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf)
1074 struct list_struct rec;
1078 if (!(rec_ptr = tdb_find(tdb, key, tdb_hash(&key), &rec)))
1081 /* must be long enough key, data and tailer */
1082 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off)) {
1083 tdb->ecode = TDB_SUCCESS; /* Not really an error */
1087 if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
1088 dbuf.dptr, dbuf.dsize) == -1)
1091 if (dbuf.dsize != rec.data_len) {
1093 rec.data_len = dbuf.dsize;
1094 return rec_write(tdb, rec_ptr, &rec);
1100 /* find an entry in the database given a key */
1101 /* If an entry doesn't exist tdb_err will be set to
1102 * TDB_ERR_NOEXIST. If a key has no data attached
1103 * tdb_err will not be set. Both will return a
1104 * zero pptr and zero dsize.
1107 TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key)
1110 struct list_struct rec;
1113 /* find which hash bucket it is in */
1114 if (!(rec_ptr = tdb_find_lock(tdb,key,F_RDLCK,&rec)))
1118 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
1122 ret.dsize = rec.data_len;
1123 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
1127 /* check if an entry in the database exists
1129 note that 1 is returned if the key is found and 0 is returned if not found
1130 this doesn't match the conventions in the rest of this module, but is
1131 compatible with gdbm
1133 int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key)
1135 struct list_struct rec;
1137 if (tdb_find_lock(tdb, key, F_RDLCK, &rec) == 0)
1139 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
1143 /* record lock stops delete underneath */
1144 static int lock_record(TDB_CONTEXT *tdb, tdb_off off)
1146 return off ? tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0) : 0;
1149 Write locks override our own fcntl readlocks, so check it here.
1150 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
1151 an error to fail to get the lock here.
1154 static int write_lock_record(TDB_CONTEXT *tdb, tdb_off off)
1156 struct tdb_traverse_lock *i;
1157 for (i = &tdb->travlocks; i; i = i->next)
1160 return tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1);
1164 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
1165 an error to fail to get the lock here.
1168 static int write_unlock_record(TDB_CONTEXT *tdb, tdb_off off)
1170 return tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0);
1172 /* fcntl locks don't stack: avoid unlocking someone else's */
1173 static int unlock_record(TDB_CONTEXT *tdb, tdb_off off)
1175 struct tdb_traverse_lock *i;
1180 for (i = &tdb->travlocks; i; i = i->next)
1183 return (count == 1 ? tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0) : 0);
1186 /* actually delete an entry in the database given the offset */
1187 static int do_delete(TDB_CONTEXT *tdb, tdb_off rec_ptr, struct list_struct*rec)
1189 tdb_off last_ptr, i;
1190 struct list_struct lastrec;
1192 if (tdb->read_only) return -1;
1194 if (write_lock_record(tdb, rec_ptr) == -1) {
1195 /* Someone traversing here: mark it as dead */
1196 rec->magic = TDB_DEAD_MAGIC;
1197 return rec_write(tdb, rec_ptr, rec);
1199 if (write_unlock_record(tdb, rec_ptr) != 0)
1202 /* find previous record in hash chain */
1203 if (ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
1205 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
1206 if (rec_read(tdb, i, &lastrec) == -1)
1209 /* unlink it: next ptr is at start of record. */
1211 last_ptr = TDB_HASH_TOP(rec->full_hash);
1212 if (ofs_write(tdb, last_ptr, &rec->next) == -1)
1215 /* recover the space */
1216 if (tdb_free(tdb, rec_ptr, rec) == -1)
1221 /* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
1222 static int tdb_next_lock(TDB_CONTEXT *tdb, struct tdb_traverse_lock *tlock,
1223 struct list_struct *rec)
1225 int want_next = (tlock->off != 0);
1227 /* No traversal allows if you've called tdb_lockkeys() */
1228 if (tdb->lockedkeys)
1229 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1231 /* Lock each chain from the start one. */
1232 for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
1233 if (tdb_lock(tdb, tlock->hash, F_WRLCK) == -1)
1236 /* No previous record? Start at top of chain. */
1238 if (ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
1242 /* Otherwise unlock the previous record. */
1243 if (unlock_record(tdb, tlock->off) != 0)
1248 /* We have offset of old record: grab next */
1249 if (rec_read(tdb, tlock->off, rec) == -1)
1251 tlock->off = rec->next;
1254 /* Iterate through chain */
1255 while( tlock->off) {
1257 if (rec_read(tdb, tlock->off, rec) == -1)
1259 if (!TDB_DEAD(rec)) {
1260 /* Woohoo: we found one! */
1261 if (lock_record(tdb, tlock->off) != 0)
1265 /* Try to clean dead ones from old traverses */
1266 current = tlock->off;
1267 tlock->off = rec->next;
1268 if (!tdb->read_only &&
1269 do_delete(tdb, current, rec) != 0)
1272 tdb_unlock(tdb, tlock->hash, F_WRLCK);
1275 /* We finished iteration without finding anything */
1276 return TDB_ERRCODE(TDB_SUCCESS, 0);
1280 if (tdb_unlock(tdb, tlock->hash, F_WRLCK) != 0)
1281 TDB_LOG((tdb, 0, "tdb_next_lock: On error unlock failed!\n"));
1285 /* traverse the entire database - calling fn(tdb, key, data) on each element.
1286 return -1 on error or the record count traversed
1287 if fn is NULL then it is not called
1288 a non-zero return value from fn() indicates that the traversal should stop
1290 int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *state)
1293 struct list_struct rec;
1294 struct tdb_traverse_lock tl = { NULL, 0, 0 };
1297 /* This was in the initializaton, above, but the IRIX compiler
1298 * did not like it. crh
1300 tl.next = tdb->travlocks.next;
1302 /* fcntl locks don't stack: beware traverse inside traverse */
1303 tdb->travlocks.next = &tl;
1305 /* tdb_next_lock places locks on the record returned, and its chain */
1306 while ((ret = tdb_next_lock(tdb, &tl, &rec)) > 0) {
1308 /* now read the full record */
1309 key.dptr = tdb_alloc_read(tdb, tl.off + sizeof(rec),
1310 rec.key_len + rec.data_len);
1313 if (tdb_unlock(tdb, tl.hash, F_WRLCK) != 0)
1315 if (unlock_record(tdb, tl.off) != 0)
1316 TDB_LOG((tdb, 0, "tdb_traverse: key.dptr == NULL and unlock_record failed!\n"));
1319 key.dsize = rec.key_len;
1320 dbuf.dptr = key.dptr + rec.key_len;
1321 dbuf.dsize = rec.data_len;
1323 /* Drop chain lock, call out */
1324 if (tdb_unlock(tdb, tl.hash, F_WRLCK) != 0) {
1328 if (fn && fn(tdb, key, dbuf, state)) {
1329 /* They want us to terminate traversal */
1331 if (unlock_record(tdb, tl.off) != 0) {
1332 TDB_LOG((tdb, 0, "tdb_traverse: unlock_record failed!\n"));;
1335 tdb->travlocks.next = tl.next;
1336 SAFE_FREE(key.dptr);
1339 SAFE_FREE(key.dptr);
1342 tdb->travlocks.next = tl.next;
1349 /* find the first entry in the database and return its key */
1350 TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb)
1353 struct list_struct rec;
1355 /* release any old lock */
1356 if (unlock_record(tdb, tdb->travlocks.off) != 0)
1358 tdb->travlocks.off = tdb->travlocks.hash = 0;
1360 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0)
1362 /* now read the key */
1363 key.dsize = rec.key_len;
1364 key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
1365 if (tdb_unlock(tdb, BUCKET(tdb->travlocks.hash), F_WRLCK) != 0)
1366 TDB_LOG((tdb, 0, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
1370 /* find the next entry in the database, returning its key */
1371 TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA oldkey)
1374 TDB_DATA key = tdb_null;
1375 struct list_struct rec;
1378 /* Is locked key the old key? If so, traverse will be reliable. */
1379 if (tdb->travlocks.off) {
1380 if (tdb_lock(tdb,tdb->travlocks.hash,F_WRLCK))
1382 if (rec_read(tdb, tdb->travlocks.off, &rec) == -1
1383 || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
1385 || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
1386 /* No, it wasn't: unlock it and start from scratch */
1387 if (unlock_record(tdb, tdb->travlocks.off) != 0)
1389 if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) != 0)
1391 tdb->travlocks.off = 0;
1397 if (!tdb->travlocks.off) {
1398 /* No previous element: do normal find, and lock record */
1399 tdb->travlocks.off = tdb_find_lock(tdb, oldkey, F_WRLCK, &rec);
1400 if (!tdb->travlocks.off)
1402 tdb->travlocks.hash = BUCKET(rec.full_hash);
1403 if (lock_record(tdb, tdb->travlocks.off) != 0) {
1404 TDB_LOG((tdb, 0, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
1408 oldhash = tdb->travlocks.hash;
1410 /* Grab next record: locks chain and returned record,
1411 unlocks old record */
1412 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
1413 key.dsize = rec.key_len;
1414 key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
1416 /* Unlock the chain of this new record */
1417 if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) != 0)
1418 TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
1420 /* Unlock the chain of old record */
1421 if (tdb_unlock(tdb, BUCKET(oldhash), F_WRLCK) != 0)
1422 TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
1426 /* delete an entry in the database given a key */
1427 int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key)
1430 struct list_struct rec;
1433 if (!(rec_ptr = tdb_find_lock(tdb, key, F_WRLCK, &rec)))
1435 ret = do_delete(tdb, rec_ptr, &rec);
1436 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
1437 TDB_LOG((tdb, 0, "tdb_delete: WARNING tdb_unlock failed!\n"));
1441 /* store an element in the database, replacing any existing element
1444 return 0 on success, -1 on failure
1446 int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
1448 struct list_struct rec;
1454 /* find which hash bucket it is in */
1455 hash = tdb_hash(&key);
1456 if (!tdb_keylocked(tdb, hash))
1458 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
1461 /* check for it existing, on insert. */
1462 if (flag == TDB_INSERT) {
1463 if (tdb_exists(tdb, key)) {
1464 tdb->ecode = TDB_ERR_EXISTS;
1468 /* first try in-place update, on modify or replace. */
1469 if (tdb_update(tdb, key, dbuf) == 0)
1471 if (flag == TDB_MODIFY && tdb->ecode == TDB_ERR_NOEXIST)
1474 /* reset the error code potentially set by the tdb_update() */
1475 tdb->ecode = TDB_SUCCESS;
1477 /* delete any existing record - if it doesn't exist we don't
1478 care. Doing this first reduces fragmentation, and avoids
1479 coalescing with `allocated' block before it's updated. */
1480 if (flag != TDB_INSERT)
1481 tdb_delete(tdb, key);
1483 /* Copy key+value *before* allocating free space in case malloc
1484 fails and we are left with a dead spot in the tdb. */
1486 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
1487 tdb->ecode = TDB_ERR_OOM;
1491 memcpy(p, key.dptr, key.dsize);
1493 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
1495 /* now we're into insert / modify / replace of a record which
1496 * we know could not be optimised by an in-place store (for
1497 * various reasons). */
1498 if (!(rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec)))
1501 /* Read hash top into next ptr */
1502 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
1505 rec.key_len = key.dsize;
1506 rec.data_len = dbuf.dsize;
1507 rec.full_hash = hash;
1508 rec.magic = TDB_MAGIC;
1510 /* write out and point the top of the hash chain at it */
1511 if (rec_write(tdb, rec_ptr, &rec) == -1
1512 || tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
1513 || ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
1514 /* Need to tdb_unallocate() here */
1519 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
1526 /* Attempt to append data to an entry in place - this only works if the new data size
1527 is <= the old data size and the key exists.
1528 on failure return -1. Record must be locked before calling.
1530 static int tdb_append_inplace(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf)
1532 struct list_struct rec;
1536 if (!(rec_ptr = tdb_find(tdb, key, tdb_hash(&key), &rec)))
1539 /* Append of 0 is always ok. */
1540 if (new_dbuf.dsize == 0)
1543 /* must be long enough for key, old data + new data and tailer */
1544 if (rec.rec_len < key.dsize + rec.data_len + new_dbuf.dsize + sizeof(tdb_off)) {
1546 tdb->ecode = TDB_SUCCESS; /* Not really an error */
1550 if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len + rec.data_len,
1551 new_dbuf.dptr, new_dbuf.dsize) == -1)
1555 rec.data_len += new_dbuf.dsize;
1556 return rec_write(tdb, rec_ptr, &rec);
1559 /* Append to an entry. Create if not exist. */
1561 int tdb_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf)
1563 struct list_struct rec;
1568 size_t new_data_size = 0;
1570 /* find which hash bucket it is in */
1571 hash = tdb_hash(&key);
1572 if (!tdb_keylocked(tdb, hash))
1574 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
1577 /* first try in-place. */
1578 if (tdb_append_inplace(tdb, key, new_dbuf) == 0)
1581 /* reset the error code potentially set by the tdb_append_inplace() */
1582 tdb->ecode = TDB_SUCCESS;
1585 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
1586 if (tdb->ecode != TDB_ERR_NOEXIST)
1589 /* Not found - create. */
1591 ret = tdb_store(tdb, key, new_dbuf, TDB_INSERT);
1595 new_data_size = rec.data_len + new_dbuf.dsize;
1597 /* Copy key+old_value+value *before* allocating free space in case malloc
1598 fails and we are left with a dead spot in the tdb. */
1600 if (!(p = (char *)malloc(key.dsize + new_data_size))) {
1601 tdb->ecode = TDB_ERR_OOM;
1605 /* Copy the key in place. */
1606 memcpy(p, key.dptr, key.dsize);
1608 /* Now read the old data into place. */
1610 tdb_read(tdb, rec_ptr + sizeof(rec) + rec.key_len, p + key.dsize, rec.data_len, 0) == -1)
1613 /* Finally append the new data. */
1615 memcpy(p+key.dsize+rec.data_len, new_dbuf.dptr, new_dbuf.dsize);
1617 /* delete any existing record - if it doesn't exist we don't
1618 care. Doing this first reduces fragmentation, and avoids
1619 coalescing with `allocated' block before it's updated. */
1621 tdb_delete(tdb, key);
1623 if (!(rec_ptr = tdb_allocate(tdb, key.dsize + new_data_size, &rec)))
1626 /* Read hash top into next ptr */
1627 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
1630 rec.key_len = key.dsize;
1631 rec.data_len = new_data_size;
1632 rec.full_hash = hash;
1633 rec.magic = TDB_MAGIC;
1635 /* write out and point the top of the hash chain at it */
1636 if (rec_write(tdb, rec_ptr, &rec) == -1
1637 || tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+new_data_size)==-1
1638 || ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
1639 /* Need to tdb_unallocate() here */
1645 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
1653 static int tdb_already_open(dev_t device,
1658 for (i = tdbs; i; i = i->next) {
1659 if (i->device == device && i->inode == ino) {
1667 /* open the database, creating it if necessary
1669 The open_flags and mode are passed straight to the open call on the
1670 database file. A flags value of O_WRONLY is invalid. The hash size
1671 is advisory, use zero for a default value.
1673 Return is NULL on error, in which case errno is also set. Don't
1674 try to call tdb_error or tdb_errname, just do strerror(errno).
1676 @param name may be NULL for internal databases. */
1677 TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
1678 int open_flags, mode_t mode)
1680 return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL);
1684 TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
1685 int open_flags, mode_t mode,
1686 tdb_log_func log_fn)
1690 int rev = 0, locked;
1694 if (!(tdb = calloc(1, sizeof *tdb))) {
1695 /* Can't log this */
1701 tdb->map_ptr = NULL;
1702 tdb->lockedkeys = NULL;
1703 tdb->flags = tdb_flags;
1704 tdb->open_flags = open_flags;
1705 tdb->log_fn = log_fn;
1707 if ((open_flags & O_ACCMODE) == O_WRONLY) {
1708 TDB_LOG((tdb, 0, "tdb_open_ex: can't open tdb %s write-only\n",
1715 hash_size = DEFAULT_HASH_SIZE;
1716 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1718 /* read only databases don't do locking or clear if first */
1719 tdb->flags |= TDB_NOLOCK;
1720 tdb->flags &= ~TDB_CLEAR_IF_FIRST;
1723 /* internal databases don't mmap or lock, and start off cleared */
1724 if (tdb->flags & TDB_INTERNAL) {
1725 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
1726 tdb->flags &= ~TDB_CLEAR_IF_FIRST;
1727 if (tdb_new_database(tdb, hash_size) != 0) {
1728 TDB_LOG((tdb, 0, "tdb_open_ex: tdb_new_database failed!"));
1734 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
1735 TDB_LOG((tdb, 5, "tdb_open_ex: could not open file %s: %s\n",
1736 name, strerror(errno)));
1737 goto fail; /* errno set by open(2) */
1741 Close file when execing another process.
1742 Prevents SELinux access errors.
1744 set_cloexec_flag(tdb->fd, 1);
1746 /* ensure there is only one process initialising at once */
1747 if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0) == -1) {
1748 TDB_LOG((tdb, 0, "tdb_open_ex: failed to get global lock on %s: %s\n",
1749 name, strerror(errno)));
1750 goto fail; /* errno set by tdb_brlock */
1753 /* we need to zero database if we are the only one with it open */
1754 if ((locked = (tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0) == 0))
1755 && (tdb_flags & TDB_CLEAR_IF_FIRST)) {
1756 open_flags |= O_CREAT;
1757 if (ftruncate(tdb->fd, 0) == -1) {
1758 TDB_LOG((tdb, 0, "tdb_open_ex: "
1759 "failed to truncate %s: %s\n",
1760 name, strerror(errno)));
1761 goto fail; /* errno set by ftruncate */
1765 if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
1766 || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
1767 || (tdb->header.version != TDB_VERSION
1768 && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
1769 /* its not a valid database - possibly initialise it */
1770 if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
1771 errno = EIO; /* ie bad format or something */
1774 rev = (tdb->flags & TDB_CONVERT);
1776 vp = (unsigned char *)&tdb->header.version;
1777 vertest = (((u32)vp[0]) << 24) | (((u32)vp[1]) << 16) |
1778 (((u32)vp[2]) << 8) | (u32)vp[3];
1779 tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
1781 tdb->flags &= ~TDB_CONVERT;
1783 tdb->flags |= TDB_CONVERT;
1784 convert(&tdb->header, sizeof(tdb->header));
1786 if (fstat(tdb->fd, &st) == -1)
1789 /* Is it already in the open list? If so, fail. */
1790 if (tdb_already_open(st.st_dev, st.st_ino)) {
1791 TDB_LOG((tdb, 2, "tdb_open_ex: "
1792 "%s (%d,%d) is already open in this process\n",
1793 name, st.st_dev, st.st_ino));
1798 if (!(tdb->name = (char *)strdup(name))) {
1803 tdb->map_size = st.st_size;
1804 tdb->device = st.st_dev;
1805 tdb->inode = st.st_ino;
1806 tdb->locked = calloc(tdb->header.hash_size+1, sizeof(tdb->locked[0]));
1808 TDB_LOG((tdb, 2, "tdb_open_ex: "
1809 "failed to allocate lock structure for %s\n",
1816 if (!tdb->read_only)
1817 if (tdb_clear_spinlocks(tdb) != 0) {
1818 TDB_LOG((tdb, 0, "tdb_open_ex: "
1819 "failed to clear spinlock\n"));
1822 if (tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0) == -1) {
1823 TDB_LOG((tdb, 0, "tdb_open_ex: "
1824 "failed to take ACTIVE_LOCK on %s: %s\n",
1825 name, strerror(errno)));
1829 /* leave this lock in place to indicate it's in use */
1830 if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1)
1834 /* Internal (memory-only) databases skip all the code above to
1835 * do with disk files, and resume here by releasing their
1836 * global lock and hooking into the active list. */
1837 if (tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0) == -1)
1844 { int save_errno = errno;
1850 if (tdb->flags & TDB_INTERNAL)
1851 SAFE_FREE(tdb->map_ptr);
1855 SAFE_FREE(tdb->name);
1857 if (close(tdb->fd) != 0)
1858 TDB_LOG((tdb, 5, "tdb_open_ex: failed to close tdb->fd on error!\n"));
1859 SAFE_FREE(tdb->locked);
1869 * @returns -1 for error; 0 for success.
1871 int tdb_close(TDB_CONTEXT *tdb)
1877 if (tdb->flags & TDB_INTERNAL)
1878 SAFE_FREE(tdb->map_ptr);
1882 SAFE_FREE(tdb->name);
1884 ret = close(tdb->fd);
1885 SAFE_FREE(tdb->locked);
1886 SAFE_FREE(tdb->lockedkeys);
1888 /* Remove from contexts list */
1889 for (i = &tdbs; *i; i = &(*i)->next) {
1896 memset(tdb, 0, sizeof(*tdb));
1902 /* lock/unlock entire database */
1903 int tdb_lockall(TDB_CONTEXT *tdb)
1907 /* There are no locks on read-only dbs */
1909 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
1910 if (tdb->lockedkeys)
1911 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1912 for (i = 0; i < tdb->header.hash_size; i++)
1913 if (tdb_lock(tdb, i, F_WRLCK))
1916 /* If error, release locks we have... */
1917 if (i < tdb->header.hash_size) {
1920 for ( j = 0; j < i; j++)
1921 tdb_unlock(tdb, j, F_WRLCK);
1922 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1927 void tdb_unlockall(TDB_CONTEXT *tdb)
1930 for (i=0; i < tdb->header.hash_size; i++)
1931 tdb_unlock(tdb, i, F_WRLCK);
1934 int tdb_lockkeys(TDB_CONTEXT *tdb, u32 number, TDB_DATA keys[])
1938 /* Can't lock more keys if already locked */
1939 if (tdb->lockedkeys)
1940 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1941 if (!(tdb->lockedkeys = malloc(sizeof(u32) * (number+1))))
1942 return TDB_ERRCODE(TDB_ERR_OOM, -1);
1943 /* First number in array is # keys */
1944 tdb->lockedkeys[0] = number;
1946 /* Insertion sort by bucket */
1947 for (i = 0; i < number; i++) {
1948 hash = tdb_hash(&keys[i]);
1949 for (j = 0; j < i && BUCKET(tdb->lockedkeys[j+1]) < BUCKET(hash); j++);
1950 memmove(&tdb->lockedkeys[j+2], &tdb->lockedkeys[j+1], sizeof(u32) * (i-j));
1951 tdb->lockedkeys[j+1] = hash;
1953 /* Finally, lock in order */
1954 for (i = 0; i < number; i++)
1955 if (tdb_lock(tdb, i, F_WRLCK))
1958 /* If error, release locks we have... */
1960 for ( j = 0; j < i; j++)
1961 tdb_unlock(tdb, j, F_WRLCK);
1962 SAFE_FREE(tdb->lockedkeys);
1963 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1968 /* Unlock the keys previously locked by tdb_lockkeys() */
1969 void tdb_unlockkeys(TDB_CONTEXT *tdb)
1972 if (!tdb->lockedkeys)
1974 for (i = 0; i < tdb->lockedkeys[0]; i++)
1975 tdb_unlock(tdb, tdb->lockedkeys[i+1], F_WRLCK);
1976 SAFE_FREE(tdb->lockedkeys);
1979 /* lock/unlock one hash chain. This is meant to be used to reduce
1980 contention - it cannot guarantee how many records will be locked */
1981 int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key)
1983 return tdb_lock(tdb, BUCKET(tdb_hash(&key)), F_WRLCK);
1986 int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key)
1988 return tdb_unlock(tdb, BUCKET(tdb_hash(&key)), F_WRLCK);
1991 int tdb_chainlock_read(TDB_CONTEXT *tdb, TDB_DATA key)
1993 return tdb_lock(tdb, BUCKET(tdb_hash(&key)), F_RDLCK);
1996 int tdb_chainunlock_read(TDB_CONTEXT *tdb, TDB_DATA key)
1998 return tdb_unlock(tdb, BUCKET(tdb_hash(&key)), F_RDLCK);
2002 /* register a loging function */
2003 void tdb_logging_function(TDB_CONTEXT *tdb, void (*fn)(TDB_CONTEXT *, int , const char *, ...))
2009 /* reopen a tdb - this is used after a fork to ensure that we have an independent
2010 seek pointer from our parent and to re-establish locks */
2011 int tdb_reopen(TDB_CONTEXT *tdb)
2015 if (tdb_munmap(tdb) != 0) {
2016 TDB_LOG((tdb, 0, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
2019 if (close(tdb->fd) != 0)
2020 TDB_LOG((tdb, 0, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
2021 tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
2022 if (tdb->fd == -1) {
2023 TDB_LOG((tdb, 0, "tdb_reopen: open failed (%s)\n", strerror(errno)));
2026 if (fstat(tdb->fd, &st) != 0) {
2027 TDB_LOG((tdb, 0, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
2030 if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
2031 TDB_LOG((tdb, 0, "tdb_reopen: file dev/inode has changed!\n"));
2035 if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1) {
2036 TDB_LOG((tdb, 0, "tdb_reopen: failed to obtain active lock\n"));
2047 /* reopen all tdb's */
2048 int tdb_reopen_all(void)
2052 for (tdb=tdbs; tdb; tdb = tdb->next) {
2053 if (tdb_reopen(tdb) != 0) return -1;