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 int tdb_munmap(TDB_CONTEXT *tdb)
89 if (tdb->flags & TDB_INTERNAL)
94 int ret = munmap(tdb->map_ptr, tdb->map_size);
103 static void tdb_mmap(TDB_CONTEXT *tdb)
105 if (tdb->flags & TDB_INTERNAL)
109 if (!(tdb->flags & TDB_NOMMAP)) {
110 tdb->map_ptr = mmap(NULL, tdb->map_size,
111 PROT_READ|(tdb->read_only? 0:PROT_WRITE),
112 MAP_SHARED|MAP_FILE, tdb->fd, 0);
115 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
118 if (tdb->map_ptr == MAP_FAILED) {
120 TDB_LOG((tdb, 2, "tdb_mmap failed for size %d (%s)\n",
121 tdb->map_size, strerror(errno)));
131 /* Endian conversion: we only ever deal with 4 byte quantities */
132 static void *convert(void *buf, u32 size)
135 for (i = 0; i < size / 4; i++)
136 p[i] = TDB_BYTEREV(p[i]);
139 #define DOCONV() (tdb->flags & TDB_CONVERT)
140 #define CONVERT(x) (DOCONV() ? convert(&x, sizeof(x)) : &x)
142 /* the body of the database is made of one list_struct for the free space
143 plus a separate data list for each hash value */
145 tdb_off next; /* offset of the next record in the list */
146 tdb_len rec_len; /* total byte length of record */
147 tdb_len key_len; /* byte length of key */
148 tdb_len data_len; /* byte length of data */
149 u32 full_hash; /* the full 32 bit hash of the key */
150 u32 magic; /* try to catch errors */
151 /* the following union is implied:
153 char record[rec_len];
158 u32 totalsize; (tailer)
163 /* a byte range locking function - return 0 on success
164 this functions locks/unlocks 1 byte at the specified offset.
166 On error, errno is also set so that errors are passed back properly
167 through tdb_open(). */
168 static int tdb_brlock(TDB_CONTEXT *tdb, tdb_off offset,
169 int rw_type, int lck_type, int probe)
174 if (tdb->flags & TDB_NOLOCK)
176 if (tdb->read_only) {
182 fl.l_whence = SEEK_SET;
188 ret = fcntl(tdb->fd,lck_type,&fl);
189 } while (ret == -1 && errno == EINTR);
192 if (!probe && lck_type != F_SETLK) {
193 TDB_LOG((tdb, 5,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d\n",
194 tdb->fd, offset, rw_type, lck_type));
196 /* errno set by fcntl */
197 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
202 /* lock a list in the database. list -1 is the alloc list */
203 static int tdb_lock(TDB_CONTEXT *tdb, int list, int ltype)
205 if (list < -1 || list >= (int)tdb->header.hash_size) {
206 TDB_LOG((tdb, 0,"tdb_lock: invalid list %d for ltype=%d\n",
210 if (tdb->flags & TDB_NOLOCK)
213 /* Since fcntl locks don't nest, we do a lock for the first one,
214 and simply bump the count for future ones */
215 if (tdb->locked[list+1].count == 0) {
216 if (!tdb->read_only && tdb->header.rwlocks) {
217 if (tdb_spinlock(tdb, list, ltype)) {
218 TDB_LOG((tdb, 0, "tdb_lock spinlock failed on list ltype=%d\n",
222 } else if (tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW, 0)) {
223 TDB_LOG((tdb, 0,"tdb_lock failed on list %d ltype=%d (%s)\n",
224 list, ltype, strerror(errno)));
227 tdb->locked[list+1].ltype = ltype;
229 tdb->locked[list+1].count++;
233 /* unlock the database: returns void because it's too late for errors. */
234 /* changed to return int it may be interesting to know there
235 has been an error --simo */
236 static int tdb_unlock(TDB_CONTEXT *tdb, int list, int ltype)
240 if (tdb->flags & TDB_NOLOCK)
244 if (list < -1 || list >= (int)tdb->header.hash_size) {
245 TDB_LOG((tdb, 0, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
249 if (tdb->locked[list+1].count==0) {
250 TDB_LOG((tdb, 0, "tdb_unlock: count is 0\n"));
254 if (tdb->locked[list+1].count == 1) {
255 /* Down to last nested lock: unlock underneath */
256 if (!tdb->read_only && tdb->header.rwlocks) {
257 ret = tdb_spinunlock(tdb, list, ltype);
259 ret = tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK, F_SETLKW, 0);
264 tdb->locked[list+1].count--;
267 TDB_LOG((tdb, 0,"tdb_unlock: An error occurred unlocking!\n"));
271 /* This is based on the hash algorithm from gdbm */
272 static u32 tdb_hash(TDB_DATA *key)
274 u32 value; /* Used to compute the hash value. */
275 u32 i; /* Used to cycle through random values. */
277 /* Set the initial value from the key size. */
278 for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
279 value = (value + (key->dptr[i] << (i*5 % 24)));
281 return (1103515243 * value + 12345);
284 /* check for an out of bounds access - if it is out of bounds then
285 see if the database has been expanded by someone else and expand
287 note that "len" is the minimum length needed for the db
289 static int tdb_oob(TDB_CONTEXT *tdb, tdb_off len, int probe)
292 if (len <= tdb->map_size)
294 if (tdb->flags & TDB_INTERNAL) {
296 TDB_LOG((tdb, 0,"tdb_oob len %d beyond internal malloc size %d\n",
297 (int)len, (int)tdb->map_size));
299 return TDB_ERRCODE(TDB_ERR_IO, -1);
302 if (fstat(tdb->fd, &st) == -1)
303 return TDB_ERRCODE(TDB_ERR_IO, -1);
305 if (st.st_size < (size_t)len) {
307 TDB_LOG((tdb, 0,"tdb_oob len %d beyond eof at %d\n",
308 (int)len, (int)st.st_size));
310 return TDB_ERRCODE(TDB_ERR_IO, -1);
313 /* Unmap, update size, remap */
314 if (tdb_munmap(tdb) == -1)
315 return TDB_ERRCODE(TDB_ERR_IO, -1);
316 tdb->map_size = st.st_size;
321 /* write a lump of data at a specified offset */
322 static int tdb_write(TDB_CONTEXT *tdb, tdb_off off, void *buf, tdb_len len)
324 if (tdb_oob(tdb, off + len, 0) != 0)
328 memcpy(off + (char *)tdb->map_ptr, buf, len);
330 else if (pwrite(tdb->fd, buf, len, off) != (ssize_t)len) {
332 else if (lseek(tdb->fd, off, SEEK_SET) != off
333 || write(tdb->fd, buf, len) != (ssize_t)len) {
335 TDB_LOG((tdb, 0,"tdb_write failed at %d len=%d (%s)\n",
336 off, len, strerror(errno)));
337 return TDB_ERRCODE(TDB_ERR_IO, -1);
342 /* read a lump of data at a specified offset, maybe convert */
343 static int tdb_read(TDB_CONTEXT *tdb,tdb_off off,void *buf,tdb_len len,int cv)
345 if (tdb_oob(tdb, off + len, 0) != 0)
349 memcpy(buf, off + (char *)tdb->map_ptr, len);
351 else if (pread(tdb->fd, buf, len, off) != (ssize_t)len) {
353 else if (lseek(tdb->fd, off, SEEK_SET) != off
354 || read(tdb->fd, buf, len) != (ssize_t)len) {
356 TDB_LOG((tdb, 0,"tdb_read failed at %d len=%d (%s)\n",
357 off, len, strerror(errno)));
358 return TDB_ERRCODE(TDB_ERR_IO, -1);
365 /* read a lump of data, allocating the space for it */
366 static char *tdb_alloc_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_len len)
370 if (!(buf = malloc(len))) {
371 TDB_LOG((tdb, 0,"tdb_alloc_read malloc failed len=%d (%s)\n",
372 len, strerror(errno)));
373 return TDB_ERRCODE(TDB_ERR_OOM, buf);
375 if (tdb_read(tdb, offset, buf, len, 0) == -1) {
382 /* read/write a tdb_off */
383 static int ofs_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d)
385 return tdb_read(tdb, offset, (char*)d, sizeof(*d), DOCONV());
387 static int ofs_write(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d)
390 return tdb_write(tdb, offset, CONVERT(off), sizeof(*d));
393 /* read/write a record */
394 static int rec_read(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
396 if (tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
398 if (TDB_BAD_MAGIC(rec)) {
399 TDB_LOG((tdb, 0,"rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
400 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
402 return tdb_oob(tdb, rec->next+sizeof(*rec), 0);
404 static int rec_write(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
406 struct list_struct r = *rec;
407 return tdb_write(tdb, offset, CONVERT(r), sizeof(r));
410 /* read a freelist record and check for simple errors */
411 static int rec_free_read(TDB_CONTEXT *tdb, tdb_off off, struct list_struct *rec)
413 if (tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
416 if (rec->magic == TDB_MAGIC) {
417 /* this happens when a app is showdown while deleting a record - we should
418 not completely fail when this happens */
419 TDB_LOG((tdb, 0,"rec_free_read non-free magic at offset=%d - fixing\n",
421 rec->magic = TDB_FREE_MAGIC;
422 if (tdb_write(tdb, off, rec, sizeof(*rec)) == -1)
426 if (rec->magic != TDB_FREE_MAGIC) {
427 TDB_LOG((tdb, 0,"rec_free_read bad magic 0x%x at offset=%d\n",
429 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
431 if (tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
436 /* update a record tailer (must hold allocation lock) */
437 static int update_tailer(TDB_CONTEXT *tdb, tdb_off offset,
438 const struct list_struct *rec)
442 /* Offset of tailer from record header */
443 totalsize = sizeof(*rec) + rec->rec_len;
444 return ofs_write(tdb, offset + totalsize - sizeof(tdb_off),
448 static tdb_off tdb_dump_record(TDB_CONTEXT *tdb, tdb_off offset)
450 struct list_struct rec;
451 tdb_off tailer_ofs, tailer;
453 if (tdb_read(tdb, offset, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
454 printf("ERROR: failed to read record at %u\n", offset);
458 printf(" rec: offset=%u next=%d rec_len=%d key_len=%d data_len=%d full_hash=0x%x magic=0x%x\n",
459 offset, rec.next, rec.rec_len, rec.key_len, rec.data_len, rec.full_hash, rec.magic);
461 tailer_ofs = offset + sizeof(rec) + rec.rec_len - sizeof(tdb_off);
462 if (ofs_read(tdb, tailer_ofs, &tailer) == -1) {
463 printf("ERROR: failed to read tailer at %u\n", tailer_ofs);
467 if (tailer != rec.rec_len + sizeof(rec)) {
468 printf("ERROR: tailer does not match record! tailer=%u totalsize=%u\n",
469 (unsigned)tailer, (unsigned)(rec.rec_len + sizeof(rec)));
474 static int tdb_dump_chain(TDB_CONTEXT *tdb, int i)
476 tdb_off rec_ptr, top;
478 top = TDB_HASH_TOP(i);
480 if (tdb_lock(tdb, i, F_WRLCK) != 0)
483 if (ofs_read(tdb, top, &rec_ptr) == -1)
484 return tdb_unlock(tdb, i, F_WRLCK);
487 printf("hash=%d\n", i);
490 rec_ptr = tdb_dump_record(tdb, rec_ptr);
493 return tdb_unlock(tdb, i, F_WRLCK);
496 void tdb_dump_all(TDB_CONTEXT *tdb)
499 for (i=0;i<tdb->header.hash_size;i++) {
500 tdb_dump_chain(tdb, i);
502 printf("freelist:\n");
503 tdb_dump_chain(tdb, -1);
506 int tdb_printfreelist(TDB_CONTEXT *tdb)
510 tdb_off offset, rec_ptr;
511 struct list_struct rec;
513 if ((ret = tdb_lock(tdb, -1, F_WRLCK)) != 0)
516 offset = FREELIST_TOP;
518 /* read in the freelist top */
519 if (ofs_read(tdb, offset, &rec_ptr) == -1) {
523 printf("freelist top=[0x%08x]\n", rec_ptr );
525 if (tdb_read(tdb, rec_ptr, (char *)&rec, sizeof(rec), DOCONV()) == -1) {
529 if (rec.magic != TDB_FREE_MAGIC) {
530 printf("bad magic 0x%08x in free list\n", rec.magic);
534 printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%d)]\n", rec.next, rec.rec_len, rec.rec_len );
535 total_free += rec.rec_len;
537 /* move to the next record */
540 printf("total rec_len = [0x%08x (%d)]\n", (int)total_free,
543 return tdb_unlock(tdb, -1, F_WRLCK);
546 /* Remove an element from the freelist. Must have alloc lock. */
547 static int remove_from_freelist(TDB_CONTEXT *tdb, tdb_off off, tdb_off next)
551 /* read in the freelist top */
552 last_ptr = FREELIST_TOP;
553 while (ofs_read(tdb, last_ptr, &i) != -1 && i != 0) {
555 /* We've found it! */
556 return ofs_write(tdb, last_ptr, &next);
558 /* Follow chain (next offset is at start of record) */
561 TDB_LOG((tdb, 0,"remove_from_freelist: not on list at off=%d\n", off));
562 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
565 /* Add an element into the freelist. Merge adjacent records if
567 static int tdb_free(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec)
571 /* Allocation and tailer lock */
572 if (tdb_lock(tdb, -1, F_WRLCK) != 0)
575 /* set an initial tailer, so if we fail we don't leave a bogus record */
576 if (update_tailer(tdb, offset, rec) != 0) {
577 TDB_LOG((tdb, 0, "tdb_free: upfate_tailer failed!\n"));
581 /* Look right first (I'm an Australian, dammit) */
582 right = offset + sizeof(*rec) + rec->rec_len;
583 if (right + sizeof(*rec) <= tdb->map_size) {
584 struct list_struct r;
586 if (tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
587 TDB_LOG((tdb, 0, "tdb_free: right read failed at %u\n", right));
591 /* If it's free, expand to include it. */
592 if (r.magic == TDB_FREE_MAGIC) {
593 if (remove_from_freelist(tdb, right, r.next) == -1) {
594 TDB_LOG((tdb, 0, "tdb_free: right free failed at %u\n", right));
597 rec->rec_len += sizeof(r) + r.rec_len;
603 left = offset - sizeof(tdb_off);
604 if (left > TDB_HASH_TOP(tdb->header.hash_size-1)) {
605 struct list_struct l;
608 /* Read in tailer and jump back to header */
609 if (ofs_read(tdb, left, &leftsize) == -1) {
610 TDB_LOG((tdb, 0, "tdb_free: left offset read failed at %u\n", left));
613 left = offset - leftsize;
615 /* Now read in record */
616 if (tdb_read(tdb, left, &l, sizeof(l), DOCONV()) == -1) {
617 TDB_LOG((tdb, 0, "tdb_free: left read failed at %u (%u)\n", left, leftsize));
621 /* If it's free, expand to include it. */
622 if (l.magic == TDB_FREE_MAGIC) {
623 if (remove_from_freelist(tdb, left, l.next) == -1) {
624 TDB_LOG((tdb, 0, "tdb_free: left free failed at %u\n", left));
628 rec->rec_len += leftsize;
634 if (update_tailer(tdb, offset, rec) == -1) {
635 TDB_LOG((tdb, 0, "tdb_free: update_tailer failed at %u\n", offset));
639 /* Now, prepend to free list */
640 rec->magic = TDB_FREE_MAGIC;
642 if (ofs_read(tdb, FREELIST_TOP, &rec->next) == -1 ||
643 rec_write(tdb, offset, rec) == -1 ||
644 ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
645 TDB_LOG((tdb, 0, "tdb_free record write failed at offset=%d\n", offset));
649 /* And we're done. */
650 tdb_unlock(tdb, -1, F_WRLCK);
654 tdb_unlock(tdb, -1, F_WRLCK);
659 /* expand a file. we prefer to use ftruncate, as that is what posix
660 says to use for mmap expansion */
661 static int expand_file(TDB_CONTEXT *tdb, tdb_off size, tdb_off addition)
664 #if HAVE_FTRUNCATE_EXTEND
665 if (ftruncate(tdb->fd, size+addition) != 0) {
666 TDB_LOG((tdb, 0, "expand_file ftruncate to %d failed (%s)\n",
667 size+addition, strerror(errno)));
674 if (pwrite(tdb->fd, &b, 1, (size+addition) - 1) != 1) {
676 if (lseek(tdb->fd, (size+addition) - 1, SEEK_SET) != (size+addition) - 1 ||
677 write(tdb->fd, &b, 1) != 1) {
679 TDB_LOG((tdb, 0, "expand_file to %d failed (%s)\n",
680 size+addition, strerror(errno)));
685 /* now fill the file with something. This ensures that the file isn't sparse, which would be
686 very bad if we ran out of disk. This must be done with write, not via mmap */
687 memset(buf, 0x42, sizeof(buf));
689 int n = addition>sizeof(buf)?sizeof(buf):addition;
691 int ret = pwrite(tdb->fd, buf, n, size);
694 if (lseek(tdb->fd, size, SEEK_SET) != size)
696 ret = write(tdb->fd, buf, n);
699 TDB_LOG((tdb, 0, "expand_file write of %d failed (%s)\n",
700 n, strerror(errno)));
710 /* expand the database at least size bytes by expanding the underlying
711 file and doing the mmap again if necessary */
712 static int tdb_expand(TDB_CONTEXT *tdb, tdb_off size)
714 struct list_struct rec;
717 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
718 TDB_LOG((tdb, 0, "lock failed in tdb_expand\n"));
722 /* must know about any previous expansions by another process */
723 tdb_oob(tdb, tdb->map_size + 1, 1);
725 /* always make room for at least 10 more records, and round
726 the database up to a multiple of TDB_PAGE_SIZE */
727 size = TDB_ALIGN(tdb->map_size + size*10, TDB_PAGE_SIZE) - tdb->map_size;
729 if (!(tdb->flags & TDB_INTERNAL))
733 * We must ensure the file is unmapped before doing this
734 * to ensure consistency with systems like OpenBSD where
735 * writes and mmaps are not consistent.
738 /* expand the file itself */
739 if (!(tdb->flags & TDB_INTERNAL)) {
740 if (expand_file(tdb, tdb->map_size, size) != 0)
744 tdb->map_size += size;
746 if (tdb->flags & TDB_INTERNAL)
747 tdb->map_ptr = realloc(tdb->map_ptr, tdb->map_size);
750 * We must ensure the file is remapped before adding the space
751 * to ensure consistency with systems like OpenBSD where
752 * writes and mmaps are not consistent.
755 /* We're ok if the mmap fails as we'll fallback to read/write */
759 /* form a new freelist record */
760 memset(&rec,'\0',sizeof(rec));
761 rec.rec_len = size - sizeof(rec);
763 /* link it into the free list */
764 offset = tdb->map_size - size;
765 if (tdb_free(tdb, offset, &rec) == -1)
768 tdb_unlock(tdb, -1, F_WRLCK);
771 tdb_unlock(tdb, -1, F_WRLCK);
775 /* allocate some space from the free list. The offset returned points
776 to a unconnected list_struct within the database with room for at
777 least length bytes of total data
779 0 is returned if the space could not be allocated
781 static tdb_off tdb_allocate(TDB_CONTEXT *tdb, tdb_len length,
782 struct list_struct *rec)
784 tdb_off rec_ptr, last_ptr, newrec_ptr;
785 struct list_struct newrec;
787 if (tdb_lock(tdb, -1, F_WRLCK) == -1)
790 /* Extra bytes required for tailer */
791 length += sizeof(tdb_off);
794 last_ptr = FREELIST_TOP;
796 /* read in the freelist top */
797 if (ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1)
800 /* keep looking until we find a freelist record big enough */
802 if (rec_free_read(tdb, rec_ptr, rec) == -1)
805 if (rec->rec_len >= length) {
806 /* found it - now possibly split it up */
807 if (rec->rec_len > length + MIN_REC_SIZE) {
808 /* Length of left piece */
809 length = TDB_ALIGN(length, TDB_ALIGNMENT);
811 /* Right piece to go on free list */
812 newrec.rec_len = rec->rec_len
813 - (sizeof(*rec) + length);
814 newrec_ptr = rec_ptr + sizeof(*rec) + length;
816 /* And left record is shortened */
817 rec->rec_len = length;
821 /* Remove allocated record from the free list */
822 if (ofs_write(tdb, last_ptr, &rec->next) == -1)
825 /* Update header: do this before we drop alloc
826 lock, otherwise tdb_free() might try to
827 merge with us, thinking we're free.
828 (Thanks Jeremy Allison). */
829 rec->magic = TDB_MAGIC;
830 if (rec_write(tdb, rec_ptr, rec) == -1)
833 /* Did we create new block? */
835 /* Update allocated record tailer (we
837 if (update_tailer(tdb, rec_ptr, rec) == -1)
840 /* Free new record */
841 if (tdb_free(tdb, newrec_ptr, &newrec) == -1)
845 /* all done - return the new record offset */
846 tdb_unlock(tdb, -1, F_WRLCK);
849 /* move to the next record */
853 /* we didn't find enough space. See if we can expand the
854 database and if we can then try again */
855 if (tdb_expand(tdb, length + sizeof(*rec)) == 0)
858 tdb_unlock(tdb, -1, F_WRLCK);
862 /* initialise a new database with a specified hash size */
863 static int tdb_new_database(TDB_CONTEXT *tdb, int hash_size)
865 struct tdb_header *newdb;
868 /* We make it up in memory, then write it out if not internal */
869 size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off);
870 if (!(newdb = calloc(size, 1)))
871 return TDB_ERRCODE(TDB_ERR_OOM, -1);
873 /* Fill in the header */
874 newdb->version = TDB_VERSION;
875 newdb->hash_size = hash_size;
877 newdb->rwlocks = size;
879 if (tdb->flags & TDB_INTERNAL) {
880 tdb->map_size = size;
881 tdb->map_ptr = (char *)newdb;
882 memcpy(&tdb->header, newdb, sizeof(tdb->header));
883 /* Convert the `ondisk' version if asked. */
887 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
890 if (ftruncate(tdb->fd, 0) == -1)
893 /* This creates an endian-converted header, as if read from disk */
895 memcpy(&tdb->header, newdb, sizeof(tdb->header));
896 /* Don't endian-convert the magic food! */
897 memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
898 if (write(tdb->fd, newdb, size) != size)
901 ret = tdb_create_rwlocks(tdb->fd, hash_size);
908 /* Returns 0 on fail. On success, return offset of record, and fills
910 static tdb_off tdb_find(TDB_CONTEXT *tdb, TDB_DATA key, u32 hash,
911 struct list_struct *r)
915 /* read in the hash top */
916 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
919 /* keep looking until we find the right record */
921 if (rec_read(tdb, rec_ptr, r) == -1)
924 if (!TDB_DEAD(r) && hash==r->full_hash && key.dsize==r->key_len) {
926 /* a very likely hit - read the key */
927 k = tdb_alloc_read(tdb, rec_ptr + sizeof(*r),
932 if (memcmp(key.dptr, k, key.dsize) == 0) {
940 return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
943 /* If they do lockkeys, check that this hash is one they locked */
944 static int tdb_keylocked(TDB_CONTEXT *tdb, u32 hash)
947 if (!tdb->lockedkeys)
949 for (i = 0; i < tdb->lockedkeys[0]; i++)
950 if (tdb->lockedkeys[i+1] == hash)
952 return TDB_ERRCODE(TDB_ERR_NOLOCK, 0);
955 /* As tdb_find, but if you succeed, keep the lock */
956 static tdb_off tdb_find_lock(TDB_CONTEXT *tdb, TDB_DATA key, int locktype,
957 struct list_struct *rec)
961 hash = tdb_hash(&key);
962 if (!tdb_keylocked(tdb, hash))
964 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
966 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
967 tdb_unlock(tdb, BUCKET(hash), locktype);
971 enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb)
976 static struct tdb_errname {
977 enum TDB_ERROR ecode; const char *estring;
978 } emap[] = { {TDB_SUCCESS, "Success"},
979 {TDB_ERR_CORRUPT, "Corrupt database"},
980 {TDB_ERR_IO, "IO Error"},
981 {TDB_ERR_LOCK, "Locking error"},
982 {TDB_ERR_OOM, "Out of memory"},
983 {TDB_ERR_EXISTS, "Record exists"},
984 {TDB_ERR_NOLOCK, "Lock exists on other keys"},
985 {TDB_ERR_NOEXIST, "Record does not exist"} };
987 /* Error string for the last tdb error */
988 const char *tdb_errorstr(TDB_CONTEXT *tdb)
991 for (i = 0; i < sizeof(emap) / sizeof(struct tdb_errname); i++)
992 if (tdb->ecode == emap[i].ecode)
993 return emap[i].estring;
994 return "Invalid error code";
997 /* update an entry in place - this only works if the new data size
998 is <= the old data size and the key exists.
1001 static int tdb_update(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf)
1003 struct list_struct rec;
1008 if (!(rec_ptr = tdb_find_lock(tdb, key, F_WRLCK, &rec)))
1011 /* must be long enough key, data and tailer */
1012 if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off)) {
1013 tdb->ecode = TDB_SUCCESS; /* Not really an error */
1017 if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
1018 dbuf.dptr, dbuf.dsize) == -1)
1021 if (dbuf.dsize != rec.data_len) {
1023 rec.data_len = dbuf.dsize;
1024 ret = rec_write(tdb, rec_ptr, &rec);
1028 tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK);
1032 /* find an entry in the database given a key */
1033 TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key)
1036 struct list_struct rec;
1039 /* find which hash bucket it is in */
1040 if (!(rec_ptr = tdb_find_lock(tdb,key,F_RDLCK,&rec)))
1043 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
1045 ret.dsize = rec.data_len;
1046 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
1050 /* check if an entry in the database exists
1052 note that 1 is returned if the key is found and 0 is returned if not found
1053 this doesn't match the conventions in the rest of this module, but is
1054 compatible with gdbm
1056 int tdb_exists(TDB_CONTEXT *tdb, TDB_DATA key)
1058 struct list_struct rec;
1060 if (tdb_find_lock(tdb, key, F_RDLCK, &rec) == 0)
1062 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
1066 /* record lock stops delete underneath */
1067 static int lock_record(TDB_CONTEXT *tdb, tdb_off off)
1069 return off ? tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0) : 0;
1072 Write locks override our own fcntl readlocks, so check it here.
1073 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
1074 an error to fail to get the lock here.
1077 static int write_lock_record(TDB_CONTEXT *tdb, tdb_off off)
1079 struct tdb_traverse_lock *i;
1080 for (i = &tdb->travlocks; i; i = i->next)
1083 return tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1);
1087 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
1088 an error to fail to get the lock here.
1091 static int write_unlock_record(TDB_CONTEXT *tdb, tdb_off off)
1093 return tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0);
1095 /* fcntl locks don't stack: avoid unlocking someone else's */
1096 static int unlock_record(TDB_CONTEXT *tdb, tdb_off off)
1098 struct tdb_traverse_lock *i;
1103 for (i = &tdb->travlocks; i; i = i->next)
1106 return (count == 1 ? tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0) : 0);
1109 /* actually delete an entry in the database given the offset */
1110 static int do_delete(TDB_CONTEXT *tdb, tdb_off rec_ptr, struct list_struct*rec)
1112 tdb_off last_ptr, i;
1113 struct list_struct lastrec;
1115 if (tdb->read_only) return -1;
1117 if (write_lock_record(tdb, rec_ptr) == -1) {
1118 /* Someone traversing here: mark it as dead */
1119 rec->magic = TDB_DEAD_MAGIC;
1120 return rec_write(tdb, rec_ptr, rec);
1122 if (write_unlock_record(tdb, rec_ptr) != 0)
1125 /* find previous record in hash chain */
1126 if (ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
1128 for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
1129 if (rec_read(tdb, i, &lastrec) == -1)
1132 /* unlink it: next ptr is at start of record. */
1134 last_ptr = TDB_HASH_TOP(rec->full_hash);
1135 if (ofs_write(tdb, last_ptr, &rec->next) == -1)
1138 /* recover the space */
1139 if (tdb_free(tdb, rec_ptr, rec) == -1)
1144 /* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
1145 static int tdb_next_lock(TDB_CONTEXT *tdb, struct tdb_traverse_lock *tlock,
1146 struct list_struct *rec)
1148 int want_next = (tlock->off != 0);
1150 /* No traversal allows if you've called tdb_lockkeys() */
1151 if (tdb->lockedkeys)
1152 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1154 /* Lock each chain from the start one. */
1155 for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
1156 if (tdb_lock(tdb, tlock->hash, F_WRLCK) == -1)
1159 /* No previous record? Start at top of chain. */
1161 if (ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
1165 /* Otherwise unlock the previous record. */
1166 if (unlock_record(tdb, tlock->off) != 0)
1171 /* We have offset of old record: grab next */
1172 if (rec_read(tdb, tlock->off, rec) == -1)
1174 tlock->off = rec->next;
1177 /* Iterate through chain */
1178 while( tlock->off) {
1180 if (rec_read(tdb, tlock->off, rec) == -1)
1182 if (!TDB_DEAD(rec)) {
1183 /* Woohoo: we found one! */
1184 if (lock_record(tdb, tlock->off) != 0)
1188 /* Try to clean dead ones from old traverses */
1189 current = tlock->off;
1190 tlock->off = rec->next;
1191 if (do_delete(tdb, current, rec) != 0)
1194 tdb_unlock(tdb, tlock->hash, F_WRLCK);
1197 /* We finished iteration without finding anything */
1198 return TDB_ERRCODE(TDB_SUCCESS, 0);
1202 if (tdb_unlock(tdb, tlock->hash, F_WRLCK) != 0)
1203 TDB_LOG((tdb, 0, "tdb_next_lock: On error unlock failed!\n"));
1207 /* traverse the entire database - calling fn(tdb, key, data) on each element.
1208 return -1 on error or the record count traversed
1209 if fn is NULL then it is not called
1210 a non-zero return value from fn() indicates that the traversal should stop
1212 int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *state)
1215 struct list_struct rec;
1216 struct tdb_traverse_lock tl = { NULL, 0, 0 };
1219 /* This was in the initializaton, above, but the IRIX compiler
1220 * did not like it. crh
1222 tl.next = tdb->travlocks.next;
1224 /* fcntl locks don't stack: beware traverse inside traverse */
1225 tdb->travlocks.next = &tl;
1227 /* tdb_next_lock places locks on the record returned, and its chain */
1228 while ((ret = tdb_next_lock(tdb, &tl, &rec)) > 0) {
1230 /* now read the full record */
1231 key.dptr = tdb_alloc_read(tdb, tl.off + sizeof(rec),
1232 rec.key_len + rec.data_len);
1235 if (tdb_unlock(tdb, tl.hash, F_WRLCK) != 0)
1237 if (unlock_record(tdb, tl.off) != 0)
1238 TDB_LOG((tdb, 0, "tdb_traverse: key.dptr == NULL and unlock_record failed!\n"));
1241 key.dsize = rec.key_len;
1242 dbuf.dptr = key.dptr + rec.key_len;
1243 dbuf.dsize = rec.data_len;
1245 /* Drop chain lock, call out */
1246 if (tdb_unlock(tdb, tl.hash, F_WRLCK) != 0) {
1250 if (fn && fn(tdb, key, dbuf, state)) {
1251 /* They want us to terminate traversal */
1253 if (unlock_record(tdb, tl.off) != 0) {
1254 TDB_LOG((tdb, 0, "tdb_traverse: unlock_record failed!\n"));;
1257 tdb->travlocks.next = tl.next;
1258 SAFE_FREE(key.dptr);
1261 SAFE_FREE(key.dptr);
1264 tdb->travlocks.next = tl.next;
1271 /* find the first entry in the database and return its key */
1272 TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb)
1275 struct list_struct rec;
1277 /* release any old lock */
1278 if (unlock_record(tdb, tdb->travlocks.off) != 0)
1280 tdb->travlocks.off = tdb->travlocks.hash = 0;
1282 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0)
1284 /* now read the key */
1285 key.dsize = rec.key_len;
1286 key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
1287 if (tdb_unlock(tdb, BUCKET(tdb->travlocks.hash), F_WRLCK) != 0)
1288 TDB_LOG((tdb, 0, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
1292 /* find the next entry in the database, returning its key */
1293 TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA oldkey)
1296 TDB_DATA key = tdb_null;
1297 struct list_struct rec;
1300 /* Is locked key the old key? If so, traverse will be reliable. */
1301 if (tdb->travlocks.off) {
1302 if (tdb_lock(tdb,tdb->travlocks.hash,F_WRLCK))
1304 if (rec_read(tdb, tdb->travlocks.off, &rec) == -1
1305 || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
1307 || memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
1308 /* No, it wasn't: unlock it and start from scratch */
1309 if (unlock_record(tdb, tdb->travlocks.off) != 0)
1311 if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) != 0)
1313 tdb->travlocks.off = 0;
1319 if (!tdb->travlocks.off) {
1320 /* No previous element: do normal find, and lock record */
1321 tdb->travlocks.off = tdb_find_lock(tdb, oldkey, F_WRLCK, &rec);
1322 if (!tdb->travlocks.off)
1324 tdb->travlocks.hash = BUCKET(rec.full_hash);
1325 if (lock_record(tdb, tdb->travlocks.off) != 0) {
1326 TDB_LOG((tdb, 0, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
1330 oldhash = tdb->travlocks.hash;
1332 /* Grab next record: locks chain and returned record,
1333 unlocks old record */
1334 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
1335 key.dsize = rec.key_len;
1336 key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
1338 /* Unlock the chain of this new record */
1339 if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) != 0)
1340 TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
1342 /* Unlock the chain of old record */
1343 if (tdb_unlock(tdb, BUCKET(oldhash), F_WRLCK) != 0)
1344 TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
1348 /* delete an entry in the database given a key */
1349 int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key)
1352 struct list_struct rec;
1355 if (!(rec_ptr = tdb_find_lock(tdb, key, F_WRLCK, &rec)))
1357 ret = do_delete(tdb, rec_ptr, &rec);
1358 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
1359 TDB_LOG((tdb, 0, "tdb_delete: WARNING tdb_unlock failed!\n"));
1363 /* store an element in the database, replacing any existing element
1366 return 0 on success, -1 on failure
1368 int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
1370 struct list_struct rec;
1376 /* find which hash bucket it is in */
1377 hash = tdb_hash(&key);
1378 if (!tdb_keylocked(tdb, hash))
1380 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
1383 /* check for it existing, on insert. */
1384 if (flag == TDB_INSERT) {
1385 if (tdb_exists(tdb, key)) {
1386 tdb->ecode = TDB_ERR_EXISTS;
1390 /* first try in-place update, on modify or replace. */
1391 if (tdb_update(tdb, key, dbuf) == 0)
1393 if (flag == TDB_MODIFY && tdb->ecode == TDB_ERR_NOEXIST)
1396 /* reset the error code potentially set by the tdb_update() */
1397 tdb->ecode = TDB_SUCCESS;
1399 /* delete any existing record - if it doesn't exist we don't
1400 care. Doing this first reduces fragmentation, and avoids
1401 coalescing with `allocated' block before it's updated. */
1402 if (flag != TDB_INSERT)
1403 tdb_delete(tdb, key);
1405 /* Copy key+value *before* allocating free space in case malloc
1406 fails and we are left with a dead spot in the tdb. */
1408 if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
1409 tdb->ecode = TDB_ERR_OOM;
1413 memcpy(p, key.dptr, key.dsize);
1414 memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
1416 /* now we're into insert / modify / replace of a record which
1417 * we know could not be optimised by an in-place store (for
1418 * various reasons). */
1419 if (!(rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec)))
1422 /* Read hash top into next ptr */
1423 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
1426 rec.key_len = key.dsize;
1427 rec.data_len = dbuf.dsize;
1428 rec.full_hash = hash;
1429 rec.magic = TDB_MAGIC;
1431 /* write out and point the top of the hash chain at it */
1432 if (rec_write(tdb, rec_ptr, &rec) == -1
1433 || tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
1434 || ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
1435 /* Need to tdb_unallocate() here */
1440 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
1447 static int tdb_already_open(dev_t device,
1452 for (i = tdbs; i; i = i->next) {
1453 if (i->device == device && i->inode == ino) {
1461 /* open the database, creating it if necessary
1463 The open_flags and mode are passed straight to the open call on the
1464 database file. A flags value of O_WRONLY is invalid. The hash size
1465 is advisory, use zero for a default value.
1467 Return is NULL on error, in which case errno is also set. Don't
1468 try to call tdb_error or tdb_errname, just do strerror(errno).
1470 @param name may be NULL for internal databases. */
1471 TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags,
1472 int open_flags, mode_t mode)
1474 return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL);
1478 TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
1479 int open_flags, mode_t mode,
1480 tdb_log_func log_fn)
1484 int rev = 0, locked;
1488 if (!(tdb = calloc(1, sizeof *tdb))) {
1489 /* Can't log this */
1495 tdb->map_ptr = NULL;
1496 tdb->lockedkeys = NULL;
1497 tdb->flags = tdb_flags;
1498 tdb->open_flags = open_flags;
1499 tdb->log_fn = log_fn;
1501 if ((open_flags & O_ACCMODE) == O_WRONLY) {
1502 TDB_LOG((tdb, 0, "tdb_open_ex: can't open tdb %s write-only\n",
1509 hash_size = DEFAULT_HASH_SIZE;
1510 if ((open_flags & O_ACCMODE) == O_RDONLY) {
1512 /* read only databases don't do locking or clear if first */
1513 tdb->flags |= TDB_NOLOCK;
1514 tdb->flags &= ~TDB_CLEAR_IF_FIRST;
1517 /* internal databases don't mmap or lock, and start off cleared */
1518 if (tdb->flags & TDB_INTERNAL) {
1519 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
1520 tdb->flags &= ~TDB_CLEAR_IF_FIRST;
1521 if (tdb_new_database(tdb, hash_size) != 0) {
1522 TDB_LOG((tdb, 0, "tdb_open_ex: tdb_new_database failed!"));
1528 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
1529 TDB_LOG((tdb, 5, "tdb_open_ex: could not open file %s: %s\n",
1530 name, strerror(errno)));
1531 goto fail; /* errno set by open(2) */
1534 /* ensure there is only one process initialising at once */
1535 if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0) == -1) {
1536 TDB_LOG((tdb, 0, "tdb_open_ex: failed to get global lock on %s: %s\n",
1537 name, strerror(errno)));
1538 goto fail; /* errno set by tdb_brlock */
1541 /* we need to zero database if we are the only one with it open */
1542 if ((locked = (tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0) == 0))
1543 && (tdb_flags & TDB_CLEAR_IF_FIRST)) {
1544 open_flags |= O_CREAT;
1545 if (ftruncate(tdb->fd, 0) == -1) {
1546 TDB_LOG((tdb, 0, "tdb_open_ex: "
1547 "failed to truncate %s: %s\n",
1548 name, strerror(errno)));
1549 goto fail; /* errno set by ftruncate */
1553 if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
1554 || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
1555 || (tdb->header.version != TDB_VERSION
1556 && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
1557 /* its not a valid database - possibly initialise it */
1558 if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
1559 errno = EIO; /* ie bad format or something */
1562 rev = (tdb->flags & TDB_CONVERT);
1564 vp = (unsigned char *)&tdb->header.version;
1565 vertest = (((u32)vp[0]) << 24) | (((u32)vp[1]) << 16) |
1566 (((u32)vp[2]) << 8) | (u32)vp[3];
1567 tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
1569 tdb->flags &= ~TDB_CONVERT;
1571 tdb->flags |= TDB_CONVERT;
1572 convert(&tdb->header, sizeof(tdb->header));
1574 if (fstat(tdb->fd, &st) == -1)
1577 /* Is it already in the open list? If so, fail. */
1578 if (tdb_already_open(st.st_dev, st.st_ino)) {
1579 TDB_LOG((tdb, 2, "tdb_open_ex: "
1580 "%s (%d,%d) is already open in this process\n",
1581 name, st.st_dev, st.st_ino));
1586 if (!(tdb->name = (char *)strdup(name))) {
1591 tdb->map_size = st.st_size;
1592 tdb->device = st.st_dev;
1593 tdb->inode = st.st_ino;
1594 tdb->locked = calloc(tdb->header.hash_size+1, sizeof(tdb->locked[0]));
1596 TDB_LOG((tdb, 2, "tdb_open_ex: "
1597 "failed to allocate lock structure for %s\n",
1604 if (!tdb->read_only)
1605 if (tdb_clear_spinlocks(tdb) != 0) {
1606 TDB_LOG((tdb, 0, "tdb_open_ex: "
1607 "failed to clear spinlock\n"));
1610 if (tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0) == -1) {
1611 TDB_LOG((tdb, 0, "tdb_open_ex: "
1612 "failed to take ACTIVE_LOCK on %s: %s\n",
1613 name, strerror(errno)));
1617 /* leave this lock in place to indicate it's in use */
1618 if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1)
1622 /* Internal (memory-only) databases skip all the code above to
1623 * do with disk files, and resume here by releasing their
1624 * global lock and hooking into the active list. */
1625 if (tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0) == -1)
1632 { int save_errno = errno;
1638 if (tdb->flags & TDB_INTERNAL)
1639 SAFE_FREE(tdb->map_ptr);
1643 SAFE_FREE(tdb->name);
1645 if (close(tdb->fd) != 0)
1646 TDB_LOG((tdb, 5, "tdb_open_ex: failed to close tdb->fd on error!\n"));
1647 SAFE_FREE(tdb->locked);
1654 /* close a database */
1655 int tdb_close(TDB_CONTEXT *tdb)
1661 if (tdb->flags & TDB_INTERNAL)
1662 SAFE_FREE(tdb->map_ptr);
1666 SAFE_FREE(tdb->name);
1668 ret = close(tdb->fd);
1669 SAFE_FREE(tdb->locked);
1670 SAFE_FREE(tdb->lockedkeys);
1672 /* Remove from contexts list */
1673 for (i = &tdbs; *i; i = &(*i)->next) {
1680 memset(tdb, 0, sizeof(*tdb));
1686 /* lock/unlock entire database */
1687 int tdb_lockall(TDB_CONTEXT *tdb)
1691 /* There are no locks on read-only dbs */
1693 return TDB_ERRCODE(TDB_ERR_LOCK, -1);
1694 if (tdb->lockedkeys)
1695 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1696 for (i = 0; i < tdb->header.hash_size; i++)
1697 if (tdb_lock(tdb, i, F_WRLCK))
1700 /* If error, release locks we have... */
1701 if (i < tdb->header.hash_size) {
1704 for ( j = 0; j < i; j++)
1705 tdb_unlock(tdb, j, F_WRLCK);
1706 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1711 void tdb_unlockall(TDB_CONTEXT *tdb)
1714 for (i=0; i < tdb->header.hash_size; i++)
1715 tdb_unlock(tdb, i, F_WRLCK);
1718 int tdb_lockkeys(TDB_CONTEXT *tdb, u32 number, TDB_DATA keys[])
1722 /* Can't lock more keys if already locked */
1723 if (tdb->lockedkeys)
1724 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1725 if (!(tdb->lockedkeys = malloc(sizeof(u32) * (number+1))))
1726 return TDB_ERRCODE(TDB_ERR_OOM, -1);
1727 /* First number in array is # keys */
1728 tdb->lockedkeys[0] = number;
1730 /* Insertion sort by bucket */
1731 for (i = 0; i < number; i++) {
1732 hash = tdb_hash(&keys[i]);
1733 for (j = 0; j < i && BUCKET(tdb->lockedkeys[j+1]) < BUCKET(hash); j++);
1734 memmove(&tdb->lockedkeys[j+2], &tdb->lockedkeys[j+1], sizeof(u32) * (i-j));
1735 tdb->lockedkeys[j+1] = hash;
1737 /* Finally, lock in order */
1738 for (i = 0; i < number; i++)
1739 if (tdb_lock(tdb, i, F_WRLCK))
1742 /* If error, release locks we have... */
1744 for ( j = 0; j < i; j++)
1745 tdb_unlock(tdb, j, F_WRLCK);
1746 SAFE_FREE(tdb->lockedkeys);
1747 return TDB_ERRCODE(TDB_ERR_NOLOCK, -1);
1752 /* Unlock the keys previously locked by tdb_lockkeys() */
1753 void tdb_unlockkeys(TDB_CONTEXT *tdb)
1756 for (i = 0; i < tdb->lockedkeys[0]; i++)
1757 tdb_unlock(tdb, tdb->lockedkeys[i+1], F_WRLCK);
1758 SAFE_FREE(tdb->lockedkeys);
1761 /* lock/unlock one hash chain. This is meant to be used to reduce
1762 contention - it cannot guarantee how many records will be locked */
1763 int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key)
1765 return tdb_lock(tdb, BUCKET(tdb_hash(&key)), F_WRLCK);
1768 int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key)
1770 return tdb_unlock(tdb, BUCKET(tdb_hash(&key)), F_WRLCK);
1774 /* register a loging function */
1775 void tdb_logging_function(TDB_CONTEXT *tdb, void (*fn)(TDB_CONTEXT *, int , const char *, ...))
1781 /* reopen a tdb - this is used after a fork to ensure that we have an independent
1782 seek pointer from our parent and to re-establish locks */
1783 int tdb_reopen(TDB_CONTEXT *tdb)
1787 if (tdb_munmap(tdb) != 0) {
1788 TDB_LOG((tdb, 0, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
1791 if (close(tdb->fd) != 0)
1792 TDB_LOG((tdb, 0, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
1793 tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
1794 if (tdb->fd == -1) {
1795 TDB_LOG((tdb, 0, "tdb_reopen: open failed (%s)\n", strerror(errno)));
1798 if (fstat(tdb->fd, &st) != 0) {
1799 TDB_LOG((tdb, 0, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
1802 if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
1803 TDB_LOG((tdb, 0, "tdb_reopen: file dev/inode has changed!\n"));
1807 if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1) {
1808 TDB_LOG((tdb, 0, "tdb_reopen: failed to obtain active lock\n"));
1819 /* reopen all tdb's */
1820 int tdb_reopen_all(void)
1824 for (tdb=tdbs; tdb; tdb = tdb->next) {
1825 if (tdb_reopen(tdb) != 0) return -1;