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
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.
43 #define TDB_MAGIC_FOOD "TDB file\n"
44 #define TDB_VERSION (0x26011967 + 6)
45 #define TDB_MAGIC (0x26011999U)
46 #define TDB_FREE_MAGIC (~TDB_MAGIC)
47 #define TDB_DEAD_MAGIC (0xFEE1DEAD)
48 #define TDB_ALIGNMENT 4
49 #define MIN_REC_SIZE (2*sizeof(struct list_struct) + TDB_ALIGNMENT)
50 #define DEFAULT_HASH_SIZE 131
51 #define TDB_PAGE_SIZE 0x2000
52 #define FREELIST_TOP (sizeof(struct tdb_header))
53 #define TDB_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
54 #define TDB_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
55 #define TDB_DEAD(r) ((r)->magic == TDB_DEAD_MAGIC)
56 #define TDB_BAD_MAGIC(r) ((r)->magic != TDB_MAGIC && !TDB_DEAD(r))
57 #define TDB_HASH_TOP(hash) (FREELIST_TOP + (BUCKET(hash)+1)*sizeof(tdb_off))
59 /* NB assumes there is a local variable called "tdb" that is the
60 * current context, also takes doubly-parenthesized print-style
62 #define TDB_LOG(x) (tdb->log_fn?((tdb->log_fn x),0) : 0)
73 #define MAP_FAILED ((void *)-1)
76 /* free memory if the pointer is valid and zero the pointer */
78 #define SAFE_FREE(x) do { if ((x) != NULL) {free((x)); (x)=NULL;} } while(0)
81 #define BUCKET(hash) ((hash) % tdb->header.hash_size)
84 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
85 static TDB_CONTEXT *tdbs = NULL;
87 static void tdb_munmap(TDB_CONTEXT *tdb)
89 if (tdb->flags & TDB_INTERNAL)
94 munmap(tdb->map_ptr, tdb->map_size);
99 static void tdb_mmap(TDB_CONTEXT *tdb)
101 if (tdb->flags & TDB_INTERNAL)
105 if (!(tdb->flags & TDB_NOMMAP)) {
106 tdb->map_ptr = mmap(NULL, tdb->map_size,
107 PROT_READ|(tdb->read_only? 0:PROT_WRITE),
108 MAP_SHARED|MAP_FILE, tdb->fd, 0);
111 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
114 if (tdb->map_ptr == MAP_FAILED) {
116 TDB_LOG((tdb, 2, "tdb_mmap failed for size %d (%s)\n",
117 tdb->map_size, strerror(errno)));
127 /* Endian conversion: we only ever deal with 4 byte quantities */
128 static void *convert(void *buf, u32 size)
131 for (i = 0; i < size / 4; i++)
132 p[i] = TDB_BYTEREV(p[i]);
135 #define DOCONV() (tdb->flags & TDB_CONVERT)
136 #define CONVERT(x) (DOCONV() ? convert(&x, sizeof(x)) : &x)
138 /* the body of the database is made of one list_struct for the free space
139 plus a separate data list for each hash value */
141 tdb_off next; /* offset of the next record in the list */
142 tdb_len rec_len; /* total byte length of record */
143 tdb_len key_len; /* byte length of key */
144 tdb_len data_len; /* byte length of data */
145 u32 full_hash; /* the full 32 bit hash of the key */
146 u32 magic; /* try to catch errors */
147 /* the following union is implied:
149 char record[rec_len];
154 u32 totalsize; (tailer)
159 /* a byte range locking function - return 0 on success
160 this functions locks/unlocks 1 byte at the specified offset.
162 On error, errno is also set so that errors are passed back properly
163 through tdb_open(). */
164 static int tdb_brlock(TDB_CONTEXT *tdb, tdb_off offset,
165 int rw_type, int lck_type, int probe)
169 if (tdb->flags & TDB_NOLOCK)
171 if (tdb->read_only) {
177 fl.l_whence = SEEK_SET;
182 if (fcntl(tdb->fd,lck_type,&fl) == -1) {
184 TDB_LOG((tdb, 5,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d\n",
185 tdb->fd, offset, rw_type, lck_type));
187 /* errno set by fcntl */
188 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
193 /* lock a list in the database. list -1 is the alloc list */
194 static int tdb_lock(TDB_CONTEXT *tdb, int list, int ltype)
196 if (list < -1 || list >= (int)tdb->header.hash_size) {
197 TDB_LOG((tdb, 0,"tdb_lock: invalid list %d for ltype=%d\n",
201 if (tdb->flags & TDB_NOLOCK)
204 /* Since fcntl locks don't nest, we do a lock for the first one,
205 and simply bump the count for future ones */
206 if (tdb->locked[list+1].count == 0) {
207 if (!tdb->read_only && tdb->header.rwlocks) {
208 if (tdb_spinlock(tdb, list, ltype)) {
209 TDB_LOG((tdb, 0, "tdb_lock spinlock failed on list ltype=%d\n",
213 } else if (tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW, 0)) {
214 TDB_LOG((tdb, 0,"tdb_lock failed on list %d ltype=%d (%s)\n",
215 list, ltype, strerror(errno)));
218 tdb->locked[list+1].ltype = ltype;
220 tdb->locked[list+1].count++;
224 /* unlock the database: returns void because it's too late for errors. */
225 static void tdb_unlock(TDB_CONTEXT *tdb, int list, int ltype)
227 if (tdb->flags & TDB_NOLOCK)
231 if (list < -1 || list >= (int)tdb->header.hash_size)
233 if (tdb->locked[list+1].count==0)
236 if (tdb->locked[list+1].count == 1) {
237 /* Down to last nested lock: unlock underneath */
238 if (!tdb->read_only && tdb->header.rwlocks)
239 tdb_spinunlock(tdb, list, ltype);
241 tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK, F_SETLKW, 0);
243 tdb->locked[list+1].count--;
246 /* This is based on the hash agorithm from gdbm */
247 static u32 tdb_hash(TDB_DATA *key)
249 u32 value; /* Used to compute the hash value. */
250 u32 i; /* Used to cycle through random values. */
252 /* Set the initial value from the key size. */
253 for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
254 value = (value + (key->dptr[i] << (i*5 % 24)));
256 return (1103515243 * value + 12345);
259 /* check for an out of bounds access - if it is out of bounds then
260 see if the database has been expanded by someone else and expand
262 note that "len" is the minimum length needed for the db
264 static int tdb_oob(TDB_CONTEXT *tdb, tdb_off len, int probe)
267 if (len <= tdb->map_size)
269 if (tdb->flags & TDB_INTERNAL) {
271 TDB_LOG((tdb, 0,"tdb_oob len %d beyond internal malloc size %d\n",
272 (int)len, (int)tdb->map_size));
274 return TDB_ERRCODE(TDB_ERR_IO, -1);
277 if (fstat(tdb->fd, &st) == -1)
278 return TDB_ERRCODE(TDB_ERR_IO, -1);
280 if (st.st_size < (size_t)len) {
282 TDB_LOG((tdb, 0,"tdb_oob len %d beyond eof at %d\n",
283 (int)len, (int)st.st_size));
285 return TDB_ERRCODE(TDB_ERR_IO, -1);
288 /* Unmap, update size, remap */
290 tdb->map_size = st.st_size;
295 /* write a lump of data at a specified offset */
296 static int tdb_write(TDB_CONTEXT *tdb, tdb_off off, void *buf, tdb_len len)
298 if (tdb_oob(tdb, off + len, 0) != 0)
302 memcpy(off + (char *)tdb->map_ptr, buf, len);
304 else if (pwrite(tdb->fd, buf, len, off) != (ssize_t)len) {
306 else if (lseek(tdb->fd, off, SEEK_SET) != off
307 || write(tdb->fd, buf, len) != (ssize_t)len) {
309 TDB_LOG((tdb, 0,"tdb_write failed at %d len=%d (%s)\n",
310 off, len, strerror(errno)));
311 return TDB_ERRCODE(TDB_ERR_IO, -1);
316 /* read a lump of data at a specified offset, maybe convert */
317 static int tdb_read(TDB_CONTEXT *tdb,tdb_off off,void *buf,tdb_len len,int cv)
319 if (tdb_oob(tdb, off + len, 0) != 0)
323 memcpy(buf, off + (char *)tdb->map_ptr, len);
325 else if (pread(tdb->fd, buf, len, off) != (ssize_t)len) {
327 else if (lseek(tdb->fd, off, SEEK_SET) != off
328 || read(tdb->fd, buf, len) != (ssize_t)len) {
330 TDB_LOG((tdb, 0,"tdb_read failed at %d len=%d (%s)\n",
331 off, len, strerror(errno)));
332 return TDB_ERRCODE(TDB_ERR_IO, -1);
339 /* read a lump of data, allocating the space for it */
340 static char *tdb_alloc_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_len len)
344 if (!(buf = malloc(len))) {
345 TDB_LOG((tdb, 0,"tdb_alloc_read malloc failed len=%d (%s)\n",
346 len, strerror(errno)));
347 return TDB_ERRCODE(TDB_ERR_OOM, buf);
349 if (tdb_read(tdb, offset, buf, len, 0) == -1) {
356 /* read/write a tdb_off */
357 static int ofs_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d)
359 return tdb_read(tdb, offset, (char*)d, sizeof(*d), DOCONV());
361 static int ofs_write(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d)
364 return tdb_write(tdb, offset, CONVERT(off), sizeof(*d));
367 /* read/write a record */
368 static int rec_read(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
370 if (tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
372 if (TDB_BAD_MAGIC(rec)) {
373 TDB_LOG((tdb, 0,"rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
374 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
376 return tdb_oob(tdb, rec->next+sizeof(*rec), 0);
378 static int rec_write(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
380 struct list_struct r = *rec;
381 return tdb_write(tdb, offset, CONVERT(r), sizeof(r));
384 /* read a freelist record and check for simple errors */
385 static int rec_free_read(TDB_CONTEXT *tdb, tdb_off off, struct list_struct *rec)
387 if (tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
389 if (rec->magic != TDB_FREE_MAGIC) {
390 TDB_LOG((tdb, 0,"rec_free_read bad magic 0x%x at offset=%d\n",
392 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
394 if (tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
399 /* update a record tailer (must hold allocation lock) */
400 static int update_tailer(TDB_CONTEXT *tdb, tdb_off offset,
401 const struct list_struct *rec)
405 /* Offset of tailer from record header */
406 totalsize = sizeof(*rec) + rec->rec_len;
407 return ofs_write(tdb, offset + totalsize - sizeof(tdb_off),
411 static tdb_off tdb_dump_record(TDB_CONTEXT *tdb, tdb_off offset)
413 struct list_struct rec;
414 tdb_off tailer_ofs, tailer;
416 if (tdb_read(tdb, offset, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
417 printf("ERROR: failed to read record at %u\n", offset);
421 printf(" rec: offset=%u next=%d rec_len=%d key_len=%d data_len=%d full_hash=0x%x magic=0x%x\n",
422 offset, rec.next, rec.rec_len, rec.key_len, rec.data_len, rec.full_hash, rec.magic);
424 tailer_ofs = offset + sizeof(rec) + rec.rec_len - sizeof(tdb_off);
425 if (ofs_read(tdb, tailer_ofs, &tailer) == -1) {
426 printf("ERROR: failed to read tailer at %u\n", tailer_ofs);
430 if (tailer != rec.rec_len + sizeof(rec)) {
431 printf("ERROR: tailer does not match record! tailer=%u totalsize=%u\n",
432 (unsigned)tailer, (unsigned)(rec.rec_len + sizeof(rec)));
437 static void tdb_dump_chain(TDB_CONTEXT *tdb, int i)
439 tdb_off rec_ptr, top;
441 top = TDB_HASH_TOP(i);
443 tdb_lock(tdb, i, F_WRLCK);
445 if (ofs_read(tdb, top, &rec_ptr) == -1) {
446 tdb_unlock(tdb, i, F_WRLCK);
451 printf("hash=%d\n", i);
454 rec_ptr = tdb_dump_record(tdb, rec_ptr);
456 tdb_unlock(tdb, i, F_WRLCK);
459 void tdb_dump_all(TDB_CONTEXT *tdb)
462 for (i=0;i<tdb->header.hash_size;i++) {
463 tdb_dump_chain(tdb, i);
465 printf("freelist:\n");
466 tdb_dump_chain(tdb, -1);
469 void tdb_printfreelist(TDB_CONTEXT *tdb)
472 tdb_off offset, rec_ptr;
473 struct list_struct rec;
475 tdb_lock(tdb, -1, F_WRLCK);
477 offset = FREELIST_TOP;
479 /* read in the freelist top */
480 if (ofs_read(tdb, offset, &rec_ptr) == -1) {
484 printf("freelist top=[0x%08x]\n", rec_ptr );
486 if (tdb_read(tdb, rec_ptr, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
490 if (rec.magic != TDB_FREE_MAGIC) {
491 printf("bad magic 0x%08x in free list\n", rec.magic);
495 printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%d)]\n", rec.next, rec.rec_len, rec.rec_len );
496 total_free += rec.rec_len;
498 /* move to the next record */
501 printf("total rec_len = [0x%08x (%d)]\n", (int)total_free,
504 tdb_unlock(tdb, -1, F_WRLCK);
507 /* Remove an element from the freelist. Must have alloc lock. */
508 static int remove_from_freelist(TDB_CONTEXT *tdb, tdb_off off, tdb_off next)
512 /* read in the freelist top */
513 last_ptr = FREELIST_TOP;
514 while (ofs_read(tdb, last_ptr, &i) != -1 && i != 0) {
516 /* We've found it! */
517 return ofs_write(tdb, last_ptr, &next);
519 /* Follow chain (next offset is at start of record) */
522 TDB_LOG((tdb, 0,"remove_from_freelist: not on list at off=%d\n", off));
523 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
526 /* Add an element into the freelist. Merge adjacent records if
528 static int tdb_free(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
532 /* Allocation and tailer lock */
533 if (tdb_lock(tdb, -1, F_WRLCK) != 0)
536 /* set an initial tailer, so if we fail we don't leave a bogus record */
537 update_tailer(tdb, offset, rec);
539 /* Look right first (I'm an Australian, dammit) */
540 right = offset + sizeof(*rec) + rec->rec_len;
541 if (right + sizeof(*rec) <= tdb->map_size) {
542 struct list_struct r;
544 if (tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
545 TDB_LOG((tdb, 0, "tdb_free: right read failed at %u\n", right));
549 /* If it's free, expand to include it. */
550 if (r.magic == TDB_FREE_MAGIC) {
551 if (remove_from_freelist(tdb, right, r.next) == -1) {
552 TDB_LOG((tdb, 0, "tdb_free: right free failed at %u\n", right));
555 rec->rec_len += sizeof(r) + r.rec_len;
561 left = offset - sizeof(tdb_off);
562 if (left > TDB_HASH_TOP(tdb->header.hash_size-1)) {
563 struct list_struct l;
566 /* Read in tailer and jump back to header */
567 if (ofs_read(tdb, left, &leftsize) == -1) {
568 TDB_LOG((tdb, 0, "tdb_free: left offset read failed at %u\n", left));
571 left = offset - leftsize;
573 /* Now read in record */
574 if (tdb_read(tdb, left, &l, sizeof(l), DOCONV()) == -1) {
575 TDB_LOG((tdb, 0, "tdb_free: left read failed at %u (%u)\n", left, leftsize));
579 /* If it's free, expand to include it. */
580 if (l.magic == TDB_FREE_MAGIC) {
581 if (remove_from_freelist(tdb, left, l.next) == -1) {
582 TDB_LOG((tdb, 0, "tdb_free: left free failed at %u\n", left));
586 rec->rec_len += leftsize;
592 if (update_tailer(tdb, offset, rec) == -1) {
593 TDB_LOG((tdb, 0, "tdb_free: update_tailer failed at %u\n", offset));
597 /* Now, prepend to free list */
598 rec->magic = TDB_FREE_MAGIC;
600 if (ofs_read(tdb, FREELIST_TOP, &rec->next) == -1 ||
601 rec_write(tdb, offset, rec) == -1 ||
602 ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
603 TDB_LOG((tdb, 0, "tdb_free record write failed at offset=%d\n", offset));
607 /* And we're done. */
608 tdb_unlock(tdb, -1, F_WRLCK);
612 tdb_unlock(tdb, -1, F_WRLCK);
617 /* expand a file. we prefer to use ftruncate, as that is what posix
618 says to use for mmap expansion */
619 static int expand_file(TDB_CONTEXT *tdb, tdb_off size, tdb_off addition)
622 #if HAVE_FTRUNCATE_EXTEND
623 if (ftruncate(tdb->fd, size+addition) != 0) {
624 TDB_LOG((tdb, 0, "expand_file ftruncate to %d failed (%s)\n",
625 size+addition, strerror(errno)));
632 if (pwrite(tdb->fd, &b, 1, (size+addition) - 1) != 1) {
634 if (lseek(tdb->fd, (size+addition) - 1, SEEK_SET) != (size+addition) - 1 ||
635 write(tdb->fd, &b, 1) != 1) {
637 TDB_LOG((tdb, 0, "expand_file to %d failed (%s)\n",
638 size+addition, strerror(errno)));
643 /* now fill the file with something. This ensures that the file isn't sparse, which would be
644 very bad if we ran out of disk. This must be done with write, not via mmap */
645 memset(buf, 0x42, sizeof(buf));
647 int n = addition>sizeof(buf)?sizeof(buf):addition;
649 int ret = pwrite(tdb->fd, buf, n, size);
652 if (lseek(tdb->fd, size, SEEK_SET) != size)
654 ret = write(tdb->fd, buf, n);
657 TDB_LOG((tdb, 0, "expand_file write of %d failed (%s)\n",
658 n, strerror(errno)));
668 /* expand the database at least size bytes by expanding the underlying
669 file and doing the mmap again if necessary */
670 static int tdb_expand(TDB_CONTEXT *tdb, tdb_off size)
672 struct list_struct rec;
675 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
676 TDB_LOG((tdb, 0, "lock failed in tdb_expand\n"));
680 /* must know about any previous expansions by another process */
681 tdb_oob(tdb, tdb->map_size + 1, 1);
683 /* always make room for at least 10 more records, and round
684 the database up to a multiple of TDB_PAGE_SIZE */
685 size = TDB_ALIGN(tdb->map_size + size*10, TDB_PAGE_SIZE) - tdb->map_size;
687 if (!(tdb->flags & TDB_INTERNAL))
691 * We must ensure the file is unmapped before doing this
692 * to ensure consistency with systems like OpenBSD where
693 * writes and mmaps are not consistent.
696 /* expand the file itself */
697 if (!(tdb->flags & TDB_INTERNAL)) {
698 if (expand_file(tdb, tdb->map_size, size) != 0)
702 tdb->map_size += size;
704 if (tdb->flags & TDB_INTERNAL)
705 tdb->map_ptr = realloc(tdb->map_ptr, tdb->map_size);
708 * We must ensure the file is remapped before adding the space
709 * to ensure consistency with systems like OpenBSD where
710 * writes and mmaps are not consistent.
713 /* We're ok if the mmap fails as we'll fallback to read/write */
717 /* form a new freelist record */
718 memset(&rec,'\0',sizeof(rec));
719 rec.rec_len = size - sizeof(rec);
721 /* link it into the free list */
722 offset = tdb->map_size - size;
723 if (tdb_free(tdb, offset, &rec) == -1)
726 tdb_unlock(tdb, -1, F_WRLCK);
729 tdb_unlock(tdb, -1, F_WRLCK);
733 /* allocate some space from the free list. The offset returned points
734 to a unconnected list_struct within the database with room for at
735 least length bytes of total data
737 0 is returned if the space could not be allocated
739 static tdb_off tdb_allocate(TDB_CONTEXT *tdb, tdb_len length,
740 struct list_struct *rec)
742 tdb_off rec_ptr, last_ptr, newrec_ptr;
743 struct list_struct newrec;
745 if (tdb_lock(tdb, -1, F_WRLCK) == -1)
748 /* Extra bytes required for tailer */
749 length += sizeof(tdb_off);
752 last_ptr = FREELIST_TOP;
754 /* read in the freelist top */
755 if (ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1)
758 /* keep looking until we find a freelist record big enough */
760 if (rec_free_read(tdb, rec_ptr, rec) == -1)
763 if (rec->rec_len >= length) {
764 /* found it - now possibly split it up */
765 if (rec->rec_len > length + MIN_REC_SIZE) {
766 /* Length of left piece */
767 length = TDB_ALIGN(length, TDB_ALIGNMENT);
769 /* Right piece to go on free list */
770 newrec.rec_len = rec->rec_len
771 - (sizeof(*rec) + length);
772 newrec_ptr = rec_ptr + sizeof(*rec) + length;
774 /* And left record is shortened */
775 rec->rec_len = length;
779 /* Remove allocated record from the free list */
780 if (ofs_write(tdb, last_ptr, &rec->next) == -1)
783 /* Update header: do this before we drop alloc
784 lock, otherwise tdb_free() might try to
785 merge with us, thinking we're free.
786 (Thanks Jeremy Allison). */
787 rec->magic = TDB_MAGIC;
788 if (rec_write(tdb, rec_ptr, rec) == -1)
791 /* Did we create new block? */
793 /* Update allocated record tailer (we
795 if (update_tailer(tdb, rec_ptr, rec) == -1)
798 /* Free new record */
799 if (tdb_free(tdb, newrec_ptr, &newrec) == -1)
803 /* all done - return the new record offset */
804 tdb_unlock(tdb, -1, F_WRLCK);
807 /* move to the next record */
811 /* we didn't find enough space. See if we can expand the
812 database and if we can then try again */
813 if (tdb_expand(tdb, length + sizeof(*rec)) == 0)
816 tdb_unlock(tdb, -1, F_WRLCK);
820 /* initialise a new database with a specified hash size */
821 static int tdb_new_database(TDB_CONTEXT *tdb, int hash_size)
823 struct tdb_header *newdb;
826 /* We make it up in memory, then write it out if not internal */
827 size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off);
828 if (!(newdb = calloc(size, 1)))
829 return TDB_ERRCODE(TDB_ERR_OOM, -1);
831 /* Fill in the header */
832 newdb->version = TDB_VERSION;
833 newdb->hash_size = hash_size;
835 newdb->rwlocks = size;
837 if (tdb->flags & TDB_INTERNAL) {
838 tdb->map_size = size;
839 tdb->map_ptr = (char *)newdb;
840 memcpy(&tdb->header, newdb, sizeof(tdb->header));
841 /* Convert the `ondisk' version if asked. */
845 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
848 if (ftruncate(tdb->fd, 0) == -1)
851 /* This creates an endian-converted header, as if read from disk */
853 memcpy(&tdb->header, newdb, sizeof(tdb->header));
854 /* Don't endian-convert the magic food! */
855 memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
856 if (write(tdb->fd, newdb, size) != size)
859 ret = tdb_create_rwlocks(tdb->fd, hash_size);
866 /* Returns 0 on fail. On success, return offset of record, and fills
868 static tdb_off tdb_find(TDB_CONTEXT *tdb, TDB_DATA key, u32 hash,
869 struct list_struct *r)
873 /* read in the hash top */
874 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
877 /* keep looking until we find the right record */
879 if (rec_read(tdb, rec_ptr, r) == -1)
882 if (!TDB_DEAD(r) && hash==r->full_hash && key.dsize==r->key_len) {
884 /* a very likely hit - read the key */
885 k = tdb_alloc_read(tdb, rec_ptr + sizeof(*r),
890 if (memcmp(key.dptr, k, key.dsize) == 0) {
898 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
901 /* If they do lockkeys, check that this hash is one they locked */
902 static int tdb_keylocked(TDB_CONTEXT *tdb, u32 hash)
905 if (!tdb->lockedkeys)
907 for (i = 0; i < tdb->lockedkeys[0]; i++)
908 if (tdb->lockedkeys[i+1] == hash)
910 return TDB_ERRCODE(TDB_ERR_NOLOCK, 0);
913 /* As tdb_find, but if you succeed, keep the lock */
914 static tdb_off tdb_find_lock(TDB_CONTEXT *tdb, TDB_DATA key, int locktype,
915 struct list_struct *rec)
919 hash = tdb_hash(&key);
920 if (!tdb_keylocked(tdb, hash))
922 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
924 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
925 tdb_unlock(tdb, BUCKET(hash), locktype);
929 enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb)
934 static struct tdb_errname {
935 enum TDB_ERROR ecode; const char *estring;
936 } emap[] = { {TDB_SUCCESS, "Success"},
937 {TDB_ERR_CORRUPT, "Corrupt database"},
938 {TDB_ERR_IO, "IO Error"},
939 {TDB_ERR_LOCK, "Locking error"},
940 {TDB_ERR_OOM, "Out of memory"},
941 {TDB_ERR_EXISTS, "Record exists"},
942 {TDB_ERR_NOLOCK, "Lock exists on other keys"},
943 {TDB_ERR_NOEXIST, "Record does not exist"} };
945 /* Error string for the last tdb error */
946 const char *tdb_errorstr(TDB_CONTEXT *tdb)
949 for (i = 0; i < sizeof(emap) / sizeof(struct tdb_errname); i++)
950 if (tdb->ecode == emap[i].ecode)
951 return emap[i].estring;
952 return "Invalid error code";
955 /* update an entry in place - this only works if the new data size
956 is <= the old data size and the key exists.
959 static int tdb_update(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf)
961 struct list_struct rec;
966 if (!(rec_ptr = tdb_find_lock(tdb, key, F_WRLCK, &rec)))
969 /* must be long enough key, data and tailer */
970 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off)) {
971 tdb->ecode = TDB_SUCCESS; /* Not really an error */
975 if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
976 dbuf.dptr, dbuf.dsize) == -1)
979 if (dbuf.dsize != rec.data_len) {
981 rec.data_len = dbuf.dsize;
982 ret = rec_write(tdb, rec_ptr, &rec);
986 tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK);
990 /* find an entry in the database given a key */
991 TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key)
994 struct list_struct rec;
997 /* find which hash bucket it is in */
998 if (!(rec_ptr = tdb_find_lock(tdb,key,F_RDLCK,&rec)))
1001 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
1003 ret.dsize = rec.data_len;
1004 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
1008 /* check if an entry in the database exists
1010 note that 1 is returned if the key is found and 0 is returned if not found
1011 this doesn't match the conventions in the rest of this module, but is
1012 compatible with gdbm
1014 int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key)
1016 struct list_struct rec;
1018 if (tdb_find_lock(tdb, key, F_RDLCK, &rec) == 0)
1020 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
1024 /* record lock stops delete underneath */
1025 static int lock_record(TDB_CONTEXT *tdb, tdb_off off)
1027 return off ? tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0) : 0;
1030 Write locks override our own fcntl readlocks, so check it here.
1031 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
1032 an error to fail to get the lock here.
1035 static int write_lock_record(TDB_CONTEXT *tdb, tdb_off off)
1037 struct tdb_traverse_lock *i;
1038 for (i = &tdb->travlocks; i; i = i->next)
1041 return tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1);
1045 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
1046 an error to fail to get the lock here.
1049 static int write_unlock_record(TDB_CONTEXT *tdb, tdb_off off)
1051 return tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0);
1053 /* fcntl locks don't stack: avoid unlocking someone else's */
1054 static int unlock_record(TDB_CONTEXT *tdb, tdb_off off)
1056 struct tdb_traverse_lock *i;
1061 for (i = &tdb->travlocks; i; i = i->next)
1064 return (count == 1 ? tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0) : 0);
1067 /* actually delete an entry in the database given the offset */
1068 static int do_delete(TDB_CONTEXT *tdb, tdb_off rec_ptr, struct list_struct*rec)
1070 tdb_off last_ptr, i;
1071 struct list_struct lastrec;
1073 if (tdb->read_only) return -1;
1075 if (write_lock_record(tdb, rec_ptr) == -1) {
1076 /* Someone traversing here: mark it as dead */
1077 rec->magic = TDB_DEAD_MAGIC;
1078 return rec_write(tdb, rec_ptr, rec);
1080 write_unlock_record(tdb, rec_ptr);
1082 /* find previous record in hash chain */
1083 if (ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
1085 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
1086 if (rec_read(tdb, i, &lastrec) == -1)
1089 /* unlink it: next ptr is at start of record. */
1091 last_ptr = TDB_HASH_TOP(rec->full_hash);
1092 if (ofs_write(tdb, last_ptr, &rec->next) == -1)
1095 /* recover the space */
1096 if (tdb_free(tdb, rec_ptr, rec) == -1)
1101 /* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
1102 static int tdb_next_lock(TDB_CONTEXT *tdb, struct tdb_traverse_lock *tlock,
1103 struct list_struct *rec)
1105 int want_next = (tlock->off != 0);
1107 /* No traversal allows if you've called tdb_lockkeys() */
1108 if (tdb->lockedkeys)
1109 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1111 /* Lock each chain from the start one. */
1112 for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
1113 if (tdb_lock(tdb, tlock->hash, F_WRLCK) == -1)
1116 /* No previous record? Start at top of chain. */
1118 if (ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
1122 /* Otherwise unlock the previous record. */
1123 unlock_record(tdb, tlock->off);
1127 /* We have offset of old record: grab next */
1128 if (rec_read(tdb, tlock->off, rec) == -1)
1130 tlock->off = rec->next;
1133 /* Iterate through chain */
1134 while( tlock->off) {
1136 if (rec_read(tdb, tlock->off, rec) == -1)
1138 if (!TDB_DEAD(rec)) {
1139 /* Woohoo: we found one! */
1140 lock_record(tdb, tlock->off);
1143 /* Try to clean dead ones from old traverses */
1144 current = tlock->off;
1145 tlock->off = rec->next;
1146 do_delete(tdb, current, rec);
1148 tdb_unlock(tdb, tlock->hash, F_WRLCK);
1151 /* We finished iteration without finding anything */
1152 return TDB_ERRCODE(TDB_SUCCESS, 0);
1156 tdb_unlock(tdb, tlock->hash, F_WRLCK);
1160 /* traverse the entire database - calling fn(tdb, key, data) on each element.
1161 return -1 on error or the record count traversed
1162 if fn is NULL then it is not called
1163 a non-zero return value from fn() indicates that the traversal should stop
1165 int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *state)
1168 struct list_struct rec;
1169 struct tdb_traverse_lock tl = { NULL, 0, 0 };
1172 /* This was in the initializaton, above, but the IRIX compiler
1173 * did not like it. crh
1175 tl.next = tdb->travlocks.next;
1177 /* fcntl locks don't stack: beware traverse inside traverse */
1178 tdb->travlocks.next = &tl;
1180 /* tdb_next_lock places locks on the record returned, and its chain */
1181 while ((ret = tdb_next_lock(tdb, &tl, &rec)) > 0) {
1183 /* now read the full record */
1184 key.dptr = tdb_alloc_read(tdb, tl.off + sizeof(rec),
1185 rec.key_len + rec.data_len);
1187 tdb_unlock(tdb, tl.hash, F_WRLCK);
1188 unlock_record(tdb, tl.off);
1189 tdb->travlocks.next = tl.next;
1192 key.dsize = rec.key_len;
1193 dbuf.dptr = key.dptr + rec.key_len;
1194 dbuf.dsize = rec.data_len;
1196 /* Drop chain lock, call out */
1197 tdb_unlock(tdb, tl.hash, F_WRLCK);
1198 if (fn && fn(tdb, key, dbuf, state)) {
1199 /* They want us to terminate traversal */
1200 unlock_record(tdb, tl.off);
1201 tdb->travlocks.next = tl.next;
1202 SAFE_FREE(key.dptr);
1205 SAFE_FREE(key.dptr);
1207 tdb->travlocks.next = tl.next;
1214 /* find the first entry in the database and return its key */
1215 TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb)
1218 struct list_struct rec;
1220 /* release any old lock */
1221 unlock_record(tdb, tdb->travlocks.off);
1222 tdb->travlocks.off = tdb->travlocks.hash = 0;
1224 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0)
1226 /* now read the key */
1227 key.dsize = rec.key_len;
1228 key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
1229 tdb_unlock(tdb, BUCKET(tdb->travlocks.hash), F_WRLCK);
1233 /* find the next entry in the database, returning its key */
1234 TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA oldkey)
1237 TDB_DATA key = tdb_null;
1238 struct list_struct rec;
1241 /* Is locked key the old key? If so, traverse will be reliable. */
1242 if (tdb->travlocks.off) {
1243 if (tdb_lock(tdb,tdb->travlocks.hash,F_WRLCK))
1245 if (rec_read(tdb, tdb->travlocks.off, &rec) == -1
1246 || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
1248 || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
1249 /* No, it wasn't: unlock it and start from scratch */
1250 unlock_record(tdb, tdb->travlocks.off);
1251 tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK);
1252 tdb->travlocks.off = 0;
1258 if (!tdb->travlocks.off) {
1259 /* No previous element: do normal find, and lock record */
1260 tdb->travlocks.off = tdb_find_lock(tdb, oldkey, F_WRLCK, &rec);
1261 if (!tdb->travlocks.off)
1263 tdb->travlocks.hash = BUCKET(rec.full_hash);
1264 lock_record(tdb, tdb->travlocks.off);
1266 oldhash = tdb->travlocks.hash;
1268 /* Grab next record: locks chain and returned record,
1269 unlocks old record */
1270 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
1271 key.dsize = rec.key_len;
1272 key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
1274 /* Unlock the chain of this new record */
1275 tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK);
1277 /* Unlock the chain of old record */
1278 tdb_unlock(tdb, BUCKET(oldhash), F_WRLCK);
1282 /* delete an entry in the database given a key */
1283 int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key)
1286 struct list_struct rec;
1289 if (!(rec_ptr = tdb_find_lock(tdb, key, F_WRLCK, &rec)))
1291 ret = do_delete(tdb, rec_ptr, &rec);
1292 tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK);
1296 /* store an element in the database, replacing any existing element
1299 return 0 on success, -1 on failure
1301 int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
1303 struct list_struct rec;
1309 /* find which hash bucket it is in */
1310 hash = tdb_hash(&key);
1311 if (!tdb_keylocked(tdb, hash))
1313 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
1316 /* check for it existing, on insert. */
1317 if (flag == TDB_INSERT) {
1318 if (tdb_exists(tdb, key)) {
1319 tdb->ecode = TDB_ERR_EXISTS;
1323 /* first try in-place update, on modify or replace. */
1324 if (tdb_update(tdb, key, dbuf) == 0)
1326 if (flag == TDB_MODIFY && tdb->ecode == TDB_ERR_NOEXIST)
1329 /* reset the error code potentially set by the tdb_update() */
1330 tdb->ecode = TDB_SUCCESS;
1332 /* delete any existing record - if it doesn't exist we don't
1333 care. Doing this first reduces fragmentation, and avoids
1334 coalescing with `allocated' block before it's updated. */
1335 if (flag != TDB_INSERT)
1336 tdb_delete(tdb, key);
1338 /* Copy key+value *before* allocating free space in case malloc
1339 fails and we are left with a dead spot in the tdb. */
1341 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
1342 tdb->ecode = TDB_ERR_OOM;
1346 memcpy(p, key.dptr, key.dsize);
1347 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
1349 /* now we're into insert / modify / replace of a record which
1350 * we know could not be optimised by an in-place store (for
1351 * various reasons). */
1352 if (!(rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec)))
1355 /* Read hash top into next ptr */
1356 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
1359 rec.key_len = key.dsize;
1360 rec.data_len = dbuf.dsize;
1361 rec.full_hash = hash;
1362 rec.magic = TDB_MAGIC;
1364 /* write out and point the top of the hash chain at it */
1365 if (rec_write(tdb, rec_ptr, &rec) == -1
1366 || tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
1367 || ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
1369 /* Need to tdb_unallocate() here */
1374 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
1378 static int tdb_already_open(dev_t device,
1383 for (i = tdbs; i; i = i->next) {
1384 if (i->device == device && i->inode == ino) {
1392 /* open the database, creating it if necessary
1394 The open_flags and mode are passed straight to the open call on the
1395 database file. A flags value of O_WRONLY is invalid. The hash size
1396 is advisory, use zero for a default value.
1398 Return is NULL on error, in which case errno is also set. Don't
1399 try to call tdb_error or tdb_errname, just do strerror(errno).
1401 @param name may be NULL for internal databases. */
1402 TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
1403 int open_flags, mode_t mode)
1405 return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL);
1409 TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
1410 int open_flags, mode_t mode,
1411 tdb_log_func log_fn)
1415 int rev = 0, locked;
1417 if (!(tdb = calloc(1, sizeof *tdb))) {
1418 /* Can't log this */
1424 tdb->map_ptr = NULL;
1425 tdb->lockedkeys = NULL;
1426 tdb->flags = tdb_flags;
1427 tdb->open_flags = open_flags;
1428 tdb->log_fn = log_fn;
1430 if ((open_flags & O_ACCMODE) == O_WRONLY) {
1431 TDB_LOG((tdb, 0, "tdb_open_ex: can't open tdb %s write-only\n",
1438 hash_size = DEFAULT_HASH_SIZE;
1439 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1441 /* read only databases don't do locking or clear if first */
1442 tdb->flags |= TDB_NOLOCK;
1443 tdb->flags &= ~TDB_CLEAR_IF_FIRST;
1446 /* internal databases don't mmap or lock, and start off cleared */
1447 if (tdb->flags & TDB_INTERNAL) {
1448 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
1449 tdb->flags &= ~TDB_CLEAR_IF_FIRST;
1450 tdb_new_database(tdb, hash_size);
1454 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
1455 TDB_LOG((tdb, 5, "tdb_open_ex: could not open file %s: %s\n",
1456 name, strerror(errno)));
1457 goto fail; /* errno set by open(2) */
1460 /* ensure there is only one process initialising at once */
1461 if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0) == -1) {
1462 TDB_LOG((tdb, 0, "tdb_open_ex: failed to get global lock on %s: %s\n",
1463 name, strerror(errno)));
1464 goto fail; /* errno set by tdb_brlock */
1467 /* we need to zero database if we are the only one with it open */
1468 if ((locked = (tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0) == 0))
1469 && (tdb_flags & TDB_CLEAR_IF_FIRST)) {
1470 open_flags |= O_CREAT;
1471 if (ftruncate(tdb->fd, 0) == -1) {
1472 TDB_LOG((tdb, 0, "tdb_open_ex: "
1473 "failed to truncate %s: %s\n",
1474 name, strerror(errno)));
1475 goto fail; /* errno set by ftruncate */
1479 if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
1480 || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
1481 || (tdb->header.version != TDB_VERSION
1482 && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
1483 /* its not a valid database - possibly initialise it */
1484 if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
1485 errno = EIO; /* ie bad format or something */
1488 rev = (tdb->flags & TDB_CONVERT);
1491 tdb->flags &= ~TDB_CONVERT;
1493 tdb->flags |= TDB_CONVERT;
1494 convert(&tdb->header, sizeof(tdb->header));
1496 if (fstat(tdb->fd, &st) == -1)
1499 /* Is it already in the open list? If so, fail. */
1500 if (tdb_already_open(st.st_dev, st.st_ino)) {
1501 TDB_LOG((tdb, 2, "tdb_open_ex: "
1502 "%s (%d,%d) is already open in this process\n",
1503 name, st.st_dev, st.st_ino));
1508 if (!(tdb->name = (char *)strdup(name))) {
1513 tdb->map_size = st.st_size;
1514 tdb->device = st.st_dev;
1515 tdb->inode = st.st_ino;
1516 tdb->locked = calloc(tdb->header.hash_size+1, sizeof(tdb->locked[0]));
1518 TDB_LOG((tdb, 2, "tdb_open_ex: "
1519 "failed to allocate lock structure for %s\n",
1526 if (!tdb->read_only)
1527 tdb_clear_spinlocks(tdb);
1528 if (tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0) == -1) {
1529 TDB_LOG((tdb, 0, "tdb_open_ex: "
1530 "failed to take ACTIVE_LOCK on %s: %s\n",
1531 name, strerror(errno)));
1535 /* leave this lock in place to indicate it's in use */
1536 if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1)
1540 /* Internal (memory-only) databases skip all the code above to
1541 * do with disk files, and resume here by releasing their
1542 * global lock and hooking into the active list. */
1543 if (tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0) == -1)
1550 { int save_errno = errno;
1556 if (tdb->flags & TDB_INTERNAL)
1557 SAFE_FREE(tdb->map_ptr);
1561 SAFE_FREE(tdb->name);
1564 SAFE_FREE(tdb->locked);
1571 /* close a database */
1572 int tdb_close(TDB_CONTEXT *tdb)
1578 if (tdb->flags & TDB_INTERNAL)
1579 SAFE_FREE(tdb->map_ptr);
1583 SAFE_FREE(tdb->name);
1585 ret = close(tdb->fd);
1586 SAFE_FREE(tdb->locked);
1587 SAFE_FREE(tdb->lockedkeys);
1589 /* Remove from contexts list */
1590 for (i = &tdbs; *i; i = &(*i)->next) {
1597 memset(tdb, 0, sizeof(*tdb));
1603 /* lock/unlock entire database */
1604 int tdb_lockall(TDB_CONTEXT *tdb)
1608 /* There are no locks on read-only dbs */
1610 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
1611 if (tdb->lockedkeys)
1612 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1613 for (i = 0; i < tdb->header.hash_size; i++)
1614 if (tdb_lock(tdb, i, F_WRLCK))
1617 /* If error, release locks we have... */
1618 if (i < tdb->header.hash_size) {
1621 for ( j = 0; j < i; j++)
1622 tdb_unlock(tdb, j, F_WRLCK);
1623 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1628 void tdb_unlockall(TDB_CONTEXT *tdb)
1631 for (i=0; i < tdb->header.hash_size; i++)
1632 tdb_unlock(tdb, i, F_WRLCK);
1635 int tdb_lockkeys(TDB_CONTEXT *tdb, u32 number, TDB_DATA keys[])
1639 /* Can't lock more keys if already locked */
1640 if (tdb->lockedkeys)
1641 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1642 if (!(tdb->lockedkeys = malloc(sizeof(u32) * (number+1))))
1643 return TDB_ERRCODE(TDB_ERR_OOM, -1);
1644 /* First number in array is # keys */
1645 tdb->lockedkeys[0] = number;
1647 /* Insertion sort by bucket */
1648 for (i = 0; i < number; i++) {
1649 hash = tdb_hash(&keys[i]);
1650 for (j = 0; j < i && BUCKET(tdb->lockedkeys[j+1]) < BUCKET(hash); j++);
1651 memmove(&tdb->lockedkeys[j+2], &tdb->lockedkeys[j+1], sizeof(u32) * (i-j));
1652 tdb->lockedkeys[j+1] = hash;
1654 /* Finally, lock in order */
1655 for (i = 0; i < number; i++)
1656 if (tdb_lock(tdb, i, F_WRLCK))
1659 /* If error, release locks we have... */
1661 for ( j = 0; j < i; j++)
1662 tdb_unlock(tdb, j, F_WRLCK);
1663 SAFE_FREE(tdb->lockedkeys);
1664 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1669 /* Unlock the keys previously locked by tdb_lockkeys() */
1670 void tdb_unlockkeys(TDB_CONTEXT *tdb)
1673 for (i = 0; i < tdb->lockedkeys[0]; i++)
1674 tdb_unlock(tdb, tdb->lockedkeys[i+1], F_WRLCK);
1675 SAFE_FREE(tdb->lockedkeys);
1678 /* lock/unlock one hash chain. This is meant to be used to reduce
1679 contention - it cannot guarantee how many records will be locked */
1680 int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key)
1682 return tdb_lock(tdb, BUCKET(tdb_hash(&key)), F_WRLCK);
1684 void tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key)
1686 tdb_unlock(tdb, BUCKET(tdb_hash(&key)), F_WRLCK);
1690 /* register a loging function */
1691 void tdb_logging_function(TDB_CONTEXT *tdb, void (*fn)(TDB_CONTEXT *, int , const char *, ...))
1697 /* reopen a tdb - this is used after a fork to ensure that we have an independent
1698 seek pointer from our parent and to re-establish locks */
1699 int tdb_reopen(TDB_CONTEXT *tdb)
1705 tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
1706 if (tdb->fd == -1) {
1707 TDB_LOG((tdb, 0, "tdb_reopen: open failed (%s)\n", strerror(errno)));
1710 fstat(tdb->fd, &st);
1711 if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
1712 TDB_LOG((tdb, 0, "tdb_reopen: file dev/inode has changed!\n"));
1716 if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1) {
1717 TDB_LOG((tdb, 0, "tdb_reopen: failed to obtain active lock\n"));
1728 /* reopen all tdb's */
1729 int tdb_reopen_all(void)
1733 for (tdb=tdbs; tdb; tdb = tdb->next) {
1734 if (tdb_reopen(tdb) != 0) return -1;