Lines Matching refs:tdb
77 #define TDB_LOG(x) tdb->log_fn x
105 #define BUCKET(hash) ((hash) % tdb->header.hash_size)
111 static int tdb_munmap(TDB_CONTEXT *tdb) in tdb_munmap() argument
113 if (tdb->flags & TDB_INTERNAL) in tdb_munmap()
117 if (tdb->map_ptr) { in tdb_munmap()
118 int ret = munmap(tdb->map_ptr, tdb->map_size); in tdb_munmap()
123 tdb->map_ptr = NULL; in tdb_munmap()
127 static void tdb_mmap(TDB_CONTEXT *tdb) in tdb_mmap() argument
129 if (tdb->flags & TDB_INTERNAL) in tdb_mmap()
133 if (!(tdb->flags & TDB_NOMMAP)) { in tdb_mmap()
134 tdb->map_ptr = mmap(NULL, tdb->map_size, in tdb_mmap()
135 PROT_READ|(tdb->read_only? 0:PROT_WRITE), in tdb_mmap()
136 MAP_SHARED|MAP_FILE, tdb->fd, 0); in tdb_mmap()
142 if (tdb->map_ptr == MAP_FAILED) { in tdb_mmap()
143 tdb->map_ptr = NULL; in tdb_mmap()
144 TDB_LOG((tdb, 2, "tdb_mmap failed for size %d (%s)\n", in tdb_mmap()
145 tdb->map_size, strerror(errno))); in tdb_mmap()
148 tdb->map_ptr = NULL; in tdb_mmap()
151 tdb->map_ptr = NULL; in tdb_mmap()
163 #define DOCONV() (tdb->flags & TDB_CONVERT)
192 static int tdb_brlock(TDB_CONTEXT *tdb, tdb_off offset, in tdb_brlock() argument
198 if (tdb->flags & TDB_NOLOCK) in tdb_brlock()
200 if ((rw_type == F_WRLCK) && (tdb->read_only)) { in tdb_brlock()
212 ret = fcntl(tdb->fd,lck_type,&fl); in tdb_brlock()
218 tdb->ecode = TDB_ERR_LOCK; in tdb_brlock()
219 TDB_LOG((tdb, 5,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d\n", in tdb_brlock()
220 tdb->fd, offset, rw_type, lck_type)); in tdb_brlock()
226 TDB_LOG((tdb, 5, "tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d: %s\n", in tdb_brlock()
227 tdb->fd, offset, rw_type, lck_type, in tdb_brlock()
236 static int tdb_lock(TDB_CONTEXT *tdb, int list, int ltype) in tdb_lock() argument
238 if (list < -1 || list >= (int)tdb->header.hash_size) { in tdb_lock()
239 TDB_LOG((tdb, 0,"tdb_lock: invalid list %d for ltype=%d\n", in tdb_lock()
243 if (tdb->flags & TDB_NOLOCK) in tdb_lock()
248 if (tdb->locked[list+1].count == 0) { in tdb_lock()
249 if (tdb_brlock(tdb,FREELIST_TOP+4*list,ltype,F_SETLKW, 0)) { in tdb_lock()
250 TDB_LOG((tdb, 0,"tdb_lock failed on list %d ltype=%d (%s)\n", in tdb_lock()
254 tdb->locked[list+1].ltype = ltype; in tdb_lock()
256 tdb->locked[list+1].count++; in tdb_lock()
263 static int tdb_unlock(TDB_CONTEXT *tdb, int list, in tdb_unlock() argument
268 if (tdb->flags & TDB_NOLOCK) in tdb_unlock()
272 if (list < -1 || list >= (int)tdb->header.hash_size) { in tdb_unlock()
273 TDB_LOG((tdb, 0, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size)); in tdb_unlock()
277 if (tdb->locked[list+1].count==0) { in tdb_unlock()
278 TDB_LOG((tdb, 0, "tdb_unlock: count is 0\n")); in tdb_unlock()
282 if (tdb->locked[list+1].count == 1) { in tdb_unlock()
284 ret = tdb_brlock(tdb, FREELIST_TOP+4*list, F_UNLCK, F_SETLKW, 0); in tdb_unlock()
288 tdb->locked[list+1].count--; in tdb_unlock()
291 TDB_LOG((tdb, 0,"tdb_unlock: An error occurred unlocking!\n")); in tdb_unlock()
313 static int tdb_oob(TDB_CONTEXT *tdb, tdb_off len, int probe) in tdb_oob() argument
316 if (len <= tdb->map_size) in tdb_oob()
318 if (tdb->flags & TDB_INTERNAL) { in tdb_oob()
321 tdb->ecode = TDB_ERR_IO; in tdb_oob()
322 TDB_LOG((tdb, 0,"tdb_oob len %d beyond internal malloc size %d\n", in tdb_oob()
323 (int)len, (int)tdb->map_size)); in tdb_oob()
328 if (fstat(tdb->fd, &st) == -1) in tdb_oob()
334 tdb->ecode = TDB_ERR_IO; in tdb_oob()
335 TDB_LOG((tdb, 0,"tdb_oob len %d beyond eof at %d\n", in tdb_oob()
342 if (tdb_munmap(tdb) == -1) in tdb_oob()
344 tdb->map_size = st.st_size; in tdb_oob()
345 tdb_mmap(tdb); in tdb_oob()
350 static int tdb_write(TDB_CONTEXT *tdb, tdb_off off, void *buf, tdb_len len) argument
352 if (tdb_oob(tdb, off + len, 0) != 0)
355 if (tdb->map_ptr)
356 memcpy(off + (char *)tdb->map_ptr, buf, len);
358 else if (pwrite(tdb->fd, buf, len, off) != (ssize_t)len) {
360 else if (lseek(tdb->fd, off, SEEK_SET) != (off_t)off
361 || write(tdb->fd, buf, len) != (off_t)len) {
364 tdb->ecode = TDB_ERR_IO;
365 TDB_LOG((tdb, 0,"tdb_write failed at %d len=%d (%s)\n",
373 static int tdb_read(TDB_CONTEXT *tdb,tdb_off off,void *buf,tdb_len len,int cv) argument
375 if (tdb_oob(tdb, off + len, 0) != 0)
378 if (tdb->map_ptr)
379 memcpy(buf, off + (char *)tdb->map_ptr, len);
381 else if (pread(tdb->fd, buf, len, off) != (off_t)len) {
383 else if (lseek(tdb->fd, off, SEEK_SET) != (off_t)off
384 || read(tdb->fd, buf, len) != (off_t)len) {
387 tdb->ecode = TDB_ERR_IO;
388 TDB_LOG((tdb, 0,"tdb_read failed at %d len=%d (%s)\n",
398 static int tdb_key_eq(TDB_CONTEXT *tdb, tdb_off off, TDB_DATA key) argument
403 if (tdb_oob(tdb, off + key.dsize, 0) != 0)
406 if (tdb->map_ptr)
407 return !memcmp(off + (char*)tdb->map_ptr, key.dptr, key.dsize);
413 if (tdb_read(tdb, off, buf, len, 0) != 0)
425 static char *tdb_alloc_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_len len) argument
429 if (!(buf = talloc_size(tdb, len))) {
431 tdb->ecode = TDB_ERR_OOM;
432 TDB_LOG((tdb, 0,"tdb_alloc_read malloc failed len=%d (%s)\n",
436 if (tdb_read(tdb, offset, buf, len, 0) == -1) {
444 static int ofs_read(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d) argument
446 return tdb_read(tdb, offset, (char*)d, sizeof(*d), DOCONV());
448 static int ofs_write(TDB_CONTEXT *tdb, tdb_off offset, tdb_off *d) argument
451 return tdb_write(tdb, offset, CONVERT(off), sizeof(*d));
455 static int rec_read(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec) argument
457 if (tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
461 tdb->ecode = TDB_ERR_CORRUPT;
462 TDB_LOG((tdb, 0,"rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
465 return tdb_oob(tdb, rec->next+sizeof(*rec), 0);
467 static int rec_write(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec) argument
470 return tdb_write(tdb, offset, CONVERT(r), sizeof(r));
474 static int rec_free_read(TDB_CONTEXT *tdb, tdb_off off, struct list_struct *rec) argument
476 if (tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
482 TDB_LOG((tdb, 0,"rec_free_read non-free magic 0x%x at offset=%d - fixing\n",
485 if (tdb_write(tdb, off, rec, sizeof(*rec)) == -1)
491 tdb->ecode = TDB_ERR_CORRUPT;
492 TDB_LOG((tdb, 0,"rec_free_read bad magic 0x%x at offset=%d\n",
496 if (tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
502 static int update_tailer(TDB_CONTEXT *tdb, tdb_off offset, argument
509 return ofs_write(tdb, offset + totalsize - sizeof(tdb_off),
514 static int remove_from_freelist(TDB_CONTEXT *tdb, tdb_off off, tdb_off next) argument
520 while (ofs_read(tdb, last_ptr, &i) != -1 && i != 0) {
523 return ofs_write(tdb, last_ptr, &next);
528 TDB_LOG((tdb, 0,"remove_from_freelist: not on list at off=%d\n", off));
534 static int tdb_free(TDB_CONTEXT *tdb, tdb_off offset, struct list_struct *rec) argument
539 if (tdb_lock(tdb, -1, F_WRLCK) != 0)
543 if (update_tailer(tdb, offset, rec) != 0) {
544 TDB_LOG((tdb, 0, "tdb_free: upfate_tailer failed!\n"));
550 if (right + sizeof(*rec) <= tdb->map_size) {
553 if (tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
554 TDB_LOG((tdb, 0, "tdb_free: right read failed at %u\n", right));
560 if (remove_from_freelist(tdb, right, r.next) == -1) {
561 TDB_LOG((tdb, 0, "tdb_free: right free failed at %u\n", right));
571 if (left > TDB_DATA_START(tdb->header.hash_size)) {
576 if (ofs_read(tdb, left, &leftsize) == -1) {
577 TDB_LOG((tdb, 0, "tdb_free: left offset read failed at %u\n", left));
583 if (tdb_read(tdb, left, &l, sizeof(l), DOCONV()) == -1) {
584 TDB_LOG((tdb, 0, "tdb_free: left read failed at %u (%u)\n", left, leftsize));
590 if (remove_from_freelist(tdb, left, l.next) == -1) {
591 TDB_LOG((tdb, 0, "tdb_free: left free failed at %u\n", left));
601 if (update_tailer(tdb, offset, rec) == -1) {
602 TDB_LOG((tdb, 0, "tdb_free: update_tailer failed at %u\n", offset));
609 if (ofs_read(tdb, FREELIST_TOP, &rec->next) == -1 ||
610 rec_write(tdb, offset, rec) == -1 ||
611 ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
612 TDB_LOG((tdb, 0, "tdb_free record write failed at offset=%d\n", offset));
617 tdb_unlock(tdb, -1, F_WRLCK);
621 tdb_unlock(tdb, -1, F_WRLCK);
628 static int expand_file(TDB_CONTEXT *tdb, tdb_off size, tdb_off addition) argument
632 if (ftruncate(tdb->fd, size+addition) != 0) {
633 TDB_LOG((tdb, 0, "expand_file ftruncate to %d failed (%s)\n",
641 if (pwrite(tdb->fd, &b, 1, (size+addition) - 1) != 1) {
643 if (lseek(tdb->fd, (size+addition) - 1, SEEK_SET) != (off_t)(size+addition) - 1 ||
644 write(tdb->fd, &b, 1) != 1) {
646 TDB_LOG((tdb, 0, "expand_file to %d failed (%s)\n",
658 int ret = pwrite(tdb->fd, buf, n, size);
661 if (lseek(tdb->fd, size, SEEK_SET) != (off_t)size)
663 ret = write(tdb->fd, buf, n);
666 TDB_LOG((tdb, 0, "expand_file write of %d failed (%s)\n",
679 static int tdb_expand(TDB_CONTEXT *tdb, tdb_off size) argument
684 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
685 TDB_LOG((tdb, 0, "lock failed in tdb_expand\n"));
690 tdb_oob(tdb, tdb->map_size + 1, 1);
694 size = TDB_ALIGN(tdb->map_size + size*10, TDB_PAGE_SIZE) - tdb->map_size;
696 if (!(tdb->flags & TDB_INTERNAL))
697 tdb_munmap(tdb);
706 if (!(tdb->flags & TDB_INTERNAL)) {
707 if (expand_file(tdb, tdb->map_size, size) != 0)
711 tdb->map_size += size;
713 if (tdb->flags & TDB_INTERNAL) {
714 char *new_map_ptr = talloc_realloc_size(tdb, tdb->map_ptr,
715 tdb->map_size);
717 tdb->map_size -= size;
720 tdb->map_ptr = new_map_ptr;
729 tdb_mmap(tdb);
737 offset = tdb->map_size - size;
738 if (tdb_free(tdb, offset, &rec) == -1)
741 tdb_unlock(tdb, -1, F_WRLCK);
744 tdb_unlock(tdb, -1, F_WRLCK);
753 static tdb_off tdb_allocate_ofs(TDB_CONTEXT *tdb, tdb_len length, tdb_off rec_ptr, argument
777 if (ofs_write(tdb, last_ptr, &rec->next) == -1) {
786 if (rec_write(tdb, rec_ptr, rec) == -1) {
794 if (update_tailer(tdb, rec_ptr, rec) == -1) {
799 if (tdb_free(tdb, newrec_ptr, &newrec) == -1) {
814 static tdb_off tdb_allocate(TDB_CONTEXT *tdb, tdb_len length, argument
823 if (tdb_lock(tdb, -1, F_WRLCK) == -1)
833 if (ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1)
844 if (rec_free_read(tdb, rec_ptr, rec) == -1) {
867 if (rec_free_read(tdb, bestfit.rec_ptr, rec) == -1) {
871 newrec_ptr = tdb_allocate_ofs(tdb, length, bestfit.rec_ptr, rec, bestfit.last_ptr);
872 tdb_unlock(tdb, -1, F_WRLCK);
878 if (tdb_expand(tdb, length + sizeof(*rec)) == 0)
881 tdb_unlock(tdb, -1, F_WRLCK);
886 static int tdb_new_database(TDB_CONTEXT *tdb, int hash_size) argument
893 if (!(newdb = talloc_zero_size(tdb, size)))
899 if (tdb->flags & TDB_INTERNAL) {
900 tdb->map_size = size;
901 tdb->map_ptr = (char *)newdb;
902 memcpy(&tdb->header, newdb, sizeof(tdb->header));
907 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
910 if (ftruncate(tdb->fd, 0) == -1)
915 memcpy(&tdb->header, newdb, sizeof(tdb->header));
918 if (write(tdb->fd, newdb, size) != size)
930 static tdb_off tdb_find(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash, argument
936 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
941 if (rec_read(tdb, rec_ptr, r) == -1)
946 int cmp = tdb_key_eq(tdb, rec_ptr + sizeof(*r), key);
958 static tdb_off tdb_find_lock_hash(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash, int locktype, argument
963 if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
965 if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
966 tdb_unlock(tdb, BUCKET(hash), locktype);
970 enum TDB_ERROR tdb_error(TDB_CONTEXT *tdb) argument
972 return tdb->ecode;
987 const char *tdb_errorstr(TDB_CONTEXT *tdb) argument
991 if (tdb->ecode == emap[i].ecode)
1001 static int tdb_update_hash(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash, TDB_DATA dbuf) argument
1007 if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
1012 tdb->ecode = TDB_SUCCESS; /* Not really an error */
1016 if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
1023 return rec_write(tdb, rec_ptr, &rec);
1036 TDB_DATA tdb_fetch(TDB_CONTEXT *tdb, TDB_DATA key) argument
1044 hash = tdb->hash_fn(&key);
1045 if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
1048 ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
1051 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
1061 static int tdb_exists_hash(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash) argument
1065 if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
1067 tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
1072 static int lock_record(TDB_CONTEXT *tdb, tdb_off off) argument
1074 return off ? tdb_brlock(tdb, off, F_RDLCK, F_SETLKW, 0) : 0;
1082 static int write_lock_record(TDB_CONTEXT *tdb, tdb_off off) argument
1085 for (i = &tdb->travlocks; i; i = i->next)
1088 return tdb_brlock(tdb, off, F_WRLCK, F_SETLK, 1);
1096 static int write_unlock_record(TDB_CONTEXT *tdb, tdb_off off) argument
1098 return tdb_brlock(tdb, off, F_UNLCK, F_SETLK, 0);
1101 static int unlock_record(TDB_CONTEXT *tdb, tdb_off off) argument
1108 for (i = &tdb->travlocks; i; i = i->next)
1111 return (count == 1 ? tdb_brlock(tdb, off, F_UNLCK, F_SETLKW, 0) : 0);
1115 static int do_delete(TDB_CONTEXT *tdb, tdb_off rec_ptr, struct list_struct*rec) argument
1120 if (tdb->read_only) return -1;
1122 if (write_lock_record(tdb, rec_ptr) == -1) {
1125 return rec_write(tdb, rec_ptr, rec);
1127 if (write_unlock_record(tdb, rec_ptr) != 0)
1131 if (ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
1134 if (rec_read(tdb, i, &lastrec) == -1)
1140 if (ofs_write(tdb, last_ptr, &rec->next) == -1)
1144 if (tdb_free(tdb, rec_ptr, rec) == -1)
1150 static int tdb_next_lock(TDB_CONTEXT *tdb, struct tdb_traverse_lock *tlock, argument
1156 for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
1188 if (tdb->map_ptr) {
1189 for (;tlock->hash < tdb->header.hash_size;tlock->hash++) {
1190 if (0 != *(uint32_t *)(TDB_HASH_TOP(tlock->hash) + (unsigned char *)tdb->map_ptr)) {
1194 if (tlock->hash == tdb->header.hash_size) {
1198 if (ofs_read(tdb, TDB_HASH_TOP(tlock->hash), &off) == 0 &&
1205 if (tdb_lock(tdb, tlock->hash, F_WRLCK) == -1)
1210 if (ofs_read(tdb, TDB_HASH_TOP(tlock->hash),
1215 if (unlock_record(tdb, tlock->off) != 0)
1221 if (rec_read(tdb, tlock->off, rec) == -1)
1229 if (rec_read(tdb, tlock->off, rec) == -1)
1234 TDB_LOG((tdb, 0, "tdb_next_lock: loop detected.\n"));
1240 if (lock_record(tdb, tlock->off) != 0)
1248 if (!tdb->read_only &&
1249 do_delete(tdb, current, rec) != 0)
1252 tdb_unlock(tdb, tlock->hash, F_WRLCK);
1260 if (tdb_unlock(tdb, tlock->hash, F_WRLCK) != 0)
1261 TDB_LOG((tdb, 0, "tdb_next_lock: On error unlock failed!\n"));
1270 int tdb_traverse(TDB_CONTEXT *tdb, tdb_traverse_func fn, void *private) argument
1280 tl.next = tdb->travlocks.next;
1283 tdb->travlocks.next = &tl;
1286 while ((ret = tdb_next_lock(tdb, &tl, &rec)) > 0) {
1289 key.dptr = tdb_alloc_read(tdb, tl.off + sizeof(rec),
1293 if (tdb_unlock(tdb, tl.hash, F_WRLCK) != 0)
1295 if (unlock_record(tdb, tl.off) != 0)
1296 TDB_LOG((tdb, 0, "tdb_traverse: key.dptr == NULL and unlock_record failed!\n"));
1304 if (tdb_unlock(tdb, tl.hash, F_WRLCK) != 0) {
1308 if (fn && fn(tdb, key, dbuf, private)) {
1311 if (unlock_record(tdb, tl.off) != 0) {
1312 TDB_LOG((tdb, 0, "tdb_traverse: unlock_record failed!\n"));
1315 tdb->travlocks.next = tl.next;
1322 tdb->travlocks.next = tl.next;
1330 TDB_DATA tdb_firstkey(TDB_CONTEXT *tdb) argument
1336 if (unlock_record(tdb, tdb->travlocks.off) != 0)
1338 tdb->travlocks.off = tdb->travlocks.hash = 0;
1340 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) <= 0)
1344 key.dptr =tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
1345 if (tdb_unlock(tdb, BUCKET(tdb->travlocks.hash), F_WRLCK) != 0)
1346 TDB_LOG((tdb, 0, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
1351 TDB_DATA tdb_nextkey(TDB_CONTEXT *tdb, TDB_DATA oldkey) argument
1359 if (tdb->travlocks.off) {
1360 if (tdb_lock(tdb,tdb->travlocks.hash,F_WRLCK))
1362 if (rec_read(tdb, tdb->travlocks.off, &rec) == -1
1363 || !(k = tdb_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
1367 if (unlock_record(tdb, tdb->travlocks.off) != 0)
1369 if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) != 0)
1371 tdb->travlocks.off = 0;
1377 if (!tdb->travlocks.off) {
1379 tdb->travlocks.off = tdb_find_lock_hash(tdb, oldkey, tdb->hash_fn(&oldkey), F_WRLCK, &rec);
1380 if (!tdb->travlocks.off)
1382 tdb->travlocks.hash = BUCKET(rec.full_hash);
1383 if (lock_record(tdb, tdb->travlocks.off) != 0) {
1384 TDB_LOG((tdb, 0, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno)));
1388 oldhash = tdb->travlocks.hash;
1392 if (tdb_next_lock(tdb, &tdb->travlocks, &rec) > 0) {
1394 key.dptr = tdb_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
1397 if (tdb_unlock(tdb, tdb->travlocks.hash, F_WRLCK) != 0)
1398 TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
1401 if (tdb_unlock(tdb, BUCKET(oldhash), F_WRLCK) != 0)
1402 TDB_LOG((tdb, 0, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
1407 static int tdb_delete_hash(TDB_CONTEXT *tdb, TDB_DATA key, uint32_t hash) argument
1413 if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK, &rec)))
1415 ret = do_delete(tdb, rec_ptr, &rec);
1416 if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
1417 TDB_LOG((tdb, 0, "tdb_delete: WARNING tdb_unlock failed!\n"));
1421 int tdb_delete(TDB_CONTEXT *tdb, TDB_DATA key) argument
1423 uint32_t hash = tdb->hash_fn(&key);
1424 return tdb_delete_hash(tdb, key, hash);
1432 int tdb_store(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) argument
1441 hash = tdb->hash_fn(&key);
1442 if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
1447 if (tdb_exists_hash(tdb, key, hash)) {
1448 tdb->ecode = TDB_ERR_EXISTS;
1453 if (tdb_update_hash(tdb, key, hash, dbuf) == 0)
1455 if (tdb->ecode == TDB_ERR_NOEXIST &&
1463 tdb->ecode = TDB_SUCCESS;
1469 tdb_delete_hash(tdb, key, hash);
1474 if (!(p = (char *)talloc_size(tdb, key.dsize + dbuf.dsize))) {
1475 tdb->ecode = TDB_ERR_OOM;
1484 if (!(rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec)))
1488 if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
1497 if (rec_write(tdb, rec_ptr, &rec) == -1
1498 || tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
1499 || ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
1505 tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
1527 static void null_log_fn(TDB_CONTEXT *tdb __attribute__((unused)), argument
1539 TDB_CONTEXT *tdb; local
1545 if (!(tdb = talloc_zero(name, TDB_CONTEXT))) {
1550 tdb->fd = -1;
1551 tdb->name = NULL;
1552 tdb->map_ptr = NULL;
1553 tdb->flags = tdb_flags;
1554 tdb->open_flags = open_flags;
1555 tdb->log_fn = log_fn?log_fn:null_log_fn;
1556 tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash;
1559 TDB_LOG((tdb, 0, "tdb_open_ex: can't open tdb %s write-only\n",
1568 tdb->read_only = 1;
1570 tdb->flags |= TDB_NOLOCK;
1571 tdb->flags &= ~TDB_CLEAR_IF_FIRST;
1575 if (tdb->flags & TDB_INTERNAL) {
1576 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
1577 tdb->flags &= ~TDB_CLEAR_IF_FIRST;
1578 if (tdb_new_database(tdb, hash_size) != 0) {
1579 TDB_LOG((tdb, 0, "tdb_open_ex: tdb_new_database failed!"));
1585 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
1586 TDB_LOG((tdb, 5, "tdb_open_ex: could not open file %s: %s\n",
1592 if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0) == -1) {
1593 TDB_LOG((tdb, 0, "tdb_open_ex: failed to get global lock on %s: %s\n",
1600 (locked = (tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0) == 0))) {
1602 if (ftruncate(tdb->fd, 0) == -1) {
1603 TDB_LOG((tdb, 0, "tdb_open_ex: "
1610 if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
1611 || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
1612 || (tdb->header.version != TDB_VERSION
1613 && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
1615 if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
1619 rev = (tdb->flags & TDB_CONVERT);
1621 vp = (uint8_t *)&tdb->header.version;
1624 tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
1626 tdb->flags &= ~TDB_CONVERT;
1628 tdb->flags |= TDB_CONVERT;
1629 convert(&tdb->header, sizeof(tdb->header));
1631 if (fstat(tdb->fd, &st) == -1)
1636 TDB_LOG((tdb, 2, "tdb_open_ex: "
1643 if (!(tdb->name = (char *)talloc_strdup(tdb, name))) {
1648 tdb->map_size = st.st_size;
1649 tdb->device = st.st_dev;
1650 tdb->inode = st.st_ino;
1651 tdb->locked = talloc_zero_array(tdb, struct tdb_lock_type,
1652 tdb->header.hash_size+1);
1653 if (!tdb->locked) {
1654 TDB_LOG((tdb, 2, "tdb_open_ex: "
1660 tdb_mmap(tdb);
1662 if (tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0) == -1) {
1663 TDB_LOG((tdb, 0, "tdb_open_ex: "
1677 if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1)
1686 if (tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0) == -1)
1688 tdb->next = tdbs;
1689 tdbs = tdb;
1690 return tdb;
1695 if (!tdb)
1698 if (tdb->map_ptr) {
1699 if (tdb->flags & TDB_INTERNAL)
1700 SAFE_FREE(tdb->map_ptr);
1702 tdb_munmap(tdb);
1704 SAFE_FREE(tdb->name);
1705 if (tdb->fd != -1)
1706 if (close(tdb->fd) != 0)
1707 TDB_LOG((tdb, 5, "tdb_open_ex: failed to close tdb->fd on error!\n"));
1708 SAFE_FREE(tdb->locked);
1709 SAFE_FREE(tdb);
1720 int tdb_close(TDB_CONTEXT *tdb) argument
1725 if (tdb->map_ptr) {
1726 if (tdb->flags & TDB_INTERNAL)
1727 SAFE_FREE(tdb->map_ptr);
1729 tdb_munmap(tdb);
1731 SAFE_FREE(tdb->name);
1732 if (tdb->fd != -1)
1733 ret = close(tdb->fd);
1734 SAFE_FREE(tdb->locked);
1738 if (*i == tdb) {
1739 *i = tdb->next;
1744 memset(tdb, 0, sizeof(*tdb));
1745 SAFE_FREE(tdb);