2 Unix SMB/CIFS implementation.
4 trivial database library
6 Copyright (C) Andrew Tridgell 1999-2005
7 Copyright (C) Paul `Rusty' Russell 2000
8 Copyright (C) Jeremy Allison 2000-2003
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 #include "tdb_private.h"
32 * We prepend the mutex area, so fixup offsets. See mutex.c for details.
33 * tdb->hdr_ofs is 0 or header.mutex_size.
35 * Note: that we only have the 4GB limit of tdb_off_t for
36 * tdb->map_size. The file size on disk can be 4GB + tdb->hdr_ofs!
39 static bool tdb_adjust_offset(struct tdb_context *tdb, off_t *off)
41 off_t tmp = tdb->hdr_ofs + *off;
43 if ((tmp < tdb->hdr_ofs) || (tmp < *off)) {
52 static ssize_t tdb_pwrite(struct tdb_context *tdb, const void *buf,
53 size_t count, off_t offset)
57 if (!tdb_adjust_offset(tdb, &offset)) {
62 ret = pwrite(tdb->fd, buf, count, offset);
63 } while ((ret == -1) && (errno == EINTR));
68 static ssize_t tdb_pread(struct tdb_context *tdb, void *buf,
69 size_t count, off_t offset)
73 if (!tdb_adjust_offset(tdb, &offset)) {
78 ret = pread(tdb->fd, buf, count, offset);
79 } while ((ret == -1) && (errno == EINTR));
84 static int tdb_ftruncate(struct tdb_context *tdb, off_t length)
88 if (!tdb_adjust_offset(tdb, &length)) {
93 ret = ftruncate(tdb->fd, length);
94 } while ((ret == -1) && (errno == EINTR));
99 #ifdef HAVE_POSIX_FALLOCATE
100 static int tdb_posix_fallocate(struct tdb_context *tdb, off_t offset,
105 if (!tdb_adjust_offset(tdb, &offset)) {
110 ret = posix_fallocate(tdb->fd, offset, len);
111 } while ((ret == -1) && (errno == EINTR));
117 static int tdb_fstat(struct tdb_context *tdb, struct stat *buf)
121 ret = fstat(tdb->fd, buf);
126 if (buf->st_size < tdb->hdr_ofs) {
130 buf->st_size -= tdb->hdr_ofs;
135 /* check for an out of bounds access - if it is out of bounds then
136 see if the database has been expanded by someone else and expand
139 static int tdb_notrans_oob(
140 struct tdb_context *tdb, tdb_off_t off, tdb_len_t len, int probe)
143 if (len + off < len) {
145 /* Ensure ecode is set for log fn. */
146 tdb->ecode = TDB_ERR_IO;
147 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob off %u len %u wrap\n",
154 * This duplicates functionality from tdb_oob(). Don't remove:
155 * we still have direct callers of tdb->methods->tdb_oob()
156 * inside transaction.c.
158 if (off + len <= tdb->map_size)
160 if (tdb->flags & TDB_INTERNAL) {
162 /* Ensure ecode is set for log fn. */
163 tdb->ecode = TDB_ERR_IO;
164 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %u beyond internal malloc size %u\n",
165 (int)(off + len), (int)tdb->map_size));
170 if (tdb_fstat(tdb, &st) == -1) {
171 tdb->ecode = TDB_ERR_IO;
175 /* Beware >4G files! */
176 if ((tdb_off_t)st.st_size != st.st_size) {
177 /* Ensure ecode is set for log fn. */
178 tdb->ecode = TDB_ERR_IO;
179 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_oob len %llu too large!\n",
180 (long long)st.st_size));
184 /* Unmap, update size, remap. We do this unconditionally, to handle
185 * the unusual case where the db is truncated.
187 * This can happen to a child using tdb_reopen_all(true) on a
188 * TDB_CLEAR_IF_FIRST tdb whose parent crashes: the next
189 * opener will truncate the database. */
190 if (tdb_munmap(tdb) == -1) {
191 tdb->ecode = TDB_ERR_IO;
194 tdb->map_size = st.st_size;
195 if (tdb_mmap(tdb) != 0) {
199 if (st.st_size < (size_t)off + len) {
201 /* Ensure ecode is set for log fn. */
202 tdb->ecode = TDB_ERR_IO;
203 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %u beyond eof at %u\n",
204 (int)(off + len), (int)st.st_size));
211 /* write a lump of data at a specified offset */
212 static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
213 const void *buf, tdb_len_t len)
219 if (tdb->read_only || tdb->traverse_read) {
220 tdb->ecode = TDB_ERR_RDONLY;
224 if (tdb_oob(tdb, off, len, 0) != 0)
228 memcpy(off + (char *)tdb->map_ptr, buf, len);
230 #ifdef HAVE_INCOHERENT_MMAP
231 tdb->ecode = TDB_ERR_IO;
236 written = tdb_pwrite(tdb, buf, len, off);
238 if ((written != (ssize_t)len) && (written != -1)) {
240 tdb->ecode = TDB_ERR_IO;
241 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: wrote only "
242 "%zi of %u bytes at %u, trying once more\n",
244 written = tdb_pwrite(tdb, (const char *)buf+written,
245 len-written, off+written);
248 /* Ensure ecode is set for log fn. */
249 tdb->ecode = TDB_ERR_IO;
250 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_write failed at %u "
251 "len=%u (%s)\n", off, len, strerror(errno)));
253 } else if (written != (ssize_t)len) {
254 tdb->ecode = TDB_ERR_IO;
255 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: failed to "
256 "write %u bytes at %u in two attempts\n",
265 /* Endian conversion: we only ever deal with 4 byte quantities */
266 void *tdb_convert(void *buf, uint32_t size)
268 uint32_t i, *p = (uint32_t *)buf;
269 for (i = 0; i < size / 4; i++)
270 p[i] = TDB_BYTEREV(p[i]);
275 /* read a lump of data at a specified offset, maybe convert */
276 static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
277 tdb_len_t len, int cv)
279 if (tdb_oob(tdb, off, len, 0) != 0) {
284 memcpy(buf, off + (char *)tdb->map_ptr, len);
286 #ifdef HAVE_INCOHERENT_MMAP
287 tdb->ecode = TDB_ERR_IO;
292 ret = tdb_pread(tdb, buf, len, off);
293 if (ret != (ssize_t)len) {
294 /* Ensure ecode is set for log fn. */
295 tdb->ecode = TDB_ERR_IO;
296 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_read failed at %u "
297 "len=%u ret=%zi (%s) map_size=%u\n",
298 off, len, ret, strerror(errno),
305 tdb_convert(buf, len);
313 do an unlocked scan of the hash table heads to find the next non-zero head. The value
314 will then be confirmed with the lock held
316 static void tdb_next_hash_chain(struct tdb_context *tdb, uint32_t *chain)
320 for (;h < tdb->hash_size;h++) {
321 if (0 != *(uint32_t *)(TDB_HASH_TOP(h) + (unsigned char *)tdb->map_ptr)) {
327 for (;h < tdb->hash_size;h++) {
328 if (tdb_ofs_read(tdb, TDB_HASH_TOP(h), &off) != 0 || off != 0) {
337 int tdb_munmap(struct tdb_context *tdb)
339 if (tdb->flags & TDB_INTERNAL)
346 ret = munmap(tdb->map_ptr, tdb->map_size);
355 /* If mmap isn't coherent, *everyone* must always mmap. */
356 static bool should_mmap(const struct tdb_context *tdb)
358 #ifdef HAVE_INCOHERENT_MMAP
361 return !(tdb->flags & TDB_NOMMAP);
365 int tdb_mmap(struct tdb_context *tdb)
367 if (tdb->flags & TDB_INTERNAL)
371 if (should_mmap(tdb)) {
372 tdb->map_ptr = mmap(NULL, tdb->map_size,
373 PROT_READ|(tdb->read_only? 0:PROT_WRITE),
374 MAP_SHARED|MAP_FILE, tdb->fd,
378 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
381 if (tdb->map_ptr == MAP_FAILED) {
383 TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_mmap failed for size %u (%s)\n",
384 tdb->map_size, strerror(errno)));
385 #ifdef HAVE_INCOHERENT_MMAP
386 tdb->ecode = TDB_ERR_IO;
399 /* expand a file. we prefer to use ftruncate, as that is what posix
400 says to use for mmap expansion */
401 static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t addition)
407 if (tdb->read_only || tdb->traverse_read) {
408 tdb->ecode = TDB_ERR_RDONLY;
412 if (!tdb_add_off_t(size, addition, &new_size)) {
413 tdb->ecode = TDB_ERR_OOM;
414 TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write "
415 "overflow detected current size[%u] addition[%u]!\n",
416 (unsigned)size, (unsigned)addition));
421 #ifdef HAVE_POSIX_FALLOCATE
422 ret = tdb_posix_fallocate(tdb, size, addition);
428 * The Linux glibc (at least as of 2.24) fallback if
429 * the file system does not support fallocate does not
430 * reset the file size back to where it was. Also, to
431 * me it is unclear from the posix spec of
432 * posix_fallocate whether this is allowed or
433 * not. Better be safe than sorry and "goto fail" but
434 * "return -1" here, leaving the EOF pointer too
441 * Retry the "old" way. Possibly unnecessary, but looking at
442 * our configure script there seem to be weird failure modes
443 * for posix_fallocate. See commit 3264a98ff16de, which
445 * https://sourceware.org/bugzilla/show_bug.cgi?id=1083.
449 ret = tdb_ftruncate(tdb, new_size);
452 ssize_t written = tdb_pwrite(tdb, &b, 1, new_size - 1);
454 /* try once more, potentially revealing errno */
455 written = tdb_pwrite(tdb, &b, 1, new_size - 1);
458 /* again - give up, guessing errno */
462 tdb->ecode = TDB_ERR_OOM;
463 TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %u failed (%s)\n",
464 (unsigned)new_size, strerror(errno)));
469 /* now fill the file with something. This ensures that the
470 file isn't sparse, which would be very bad if we ran out of
471 disk. This must be done with write, not via mmap */
472 memset(buf, TDB_PAD_BYTE, sizeof(buf));
474 size_t n = addition>sizeof(buf)?sizeof(buf):addition;
475 ssize_t written = tdb_pwrite(tdb, buf, n, size);
477 /* prevent infinite loops: try _once_ more */
478 written = tdb_pwrite(tdb, buf, n, size);
481 /* give up, trying to provide a useful errno */
482 tdb->ecode = TDB_ERR_OOM;
483 TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write "
484 "returned 0 twice: giving up!\n"));
489 tdb->ecode = TDB_ERR_OOM;
490 TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write of "
491 "%u bytes failed (%s)\n", (int)n,
496 TDB_LOG((tdb, TDB_DEBUG_WARNING, "expand_file: wrote "
497 "only %zu of %zi bytes - retrying\n", written,
510 * We're holding the freelist lock or are inside a
511 * transaction. Cutting the file is safe, the space we
512 * tried to allocate can't have been used anywhere in
516 ret = tdb_ftruncate(tdb, size);
518 TDB_LOG((tdb, TDB_DEBUG_WARNING, "expand_file: "
519 "retruncate to %ju failed\n",
529 /* You need 'size', this tells you how much you should expand by. */
530 tdb_off_t tdb_expand_adjust(tdb_off_t map_size, tdb_off_t size, int page_size)
532 tdb_off_t new_size, top_size, increment;
533 tdb_off_t max_size = UINT32_MAX - map_size;
535 if (size > max_size) {
537 * We can't round up anymore, just give back
538 * what we're asked for.
540 * The caller has to take care of the ENOSPC handling.
545 /* limit size in order to avoid using up huge amounts of memory for
546 * in memory tdbs if an oddball huge record creeps in */
547 if (size > 100 * 1024) {
548 increment = size * 2;
550 increment = size * 100;
552 if (increment < size) {
556 if (!tdb_add_off_t(map_size, increment, &top_size)) {
560 /* always make room for at least top_size more records, and at
561 least 25% more space. if the DB is smaller than 100MiB,
562 otherwise grow it by 10% only. */
563 if (map_size > 100 * 1024 * 1024) {
564 new_size = map_size * 1.10;
566 new_size = map_size * 1.25;
568 if (new_size < map_size) {
572 /* Round the database up to a multiple of the page size */
573 new_size = MAX(top_size, new_size);
575 if (new_size + page_size < new_size) {
576 /* There's a "+" in TDB_ALIGN that might overflow... */
580 return TDB_ALIGN(new_size, page_size) - map_size;
584 * Somewhere in between we went over 4GB. Make one big jump to
585 * exactly 4GB database size.
590 /* expand the database at least size bytes by expanding the underlying
591 file and doing the mmap again if necessary */
592 int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
594 struct tdb_record rec;
598 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
599 TDB_LOG((tdb, TDB_DEBUG_ERROR, "lock failed in tdb_expand\n"));
603 /* must know about any previous expansions by another process */
604 tdb_oob(tdb, tdb->map_size, 1, 1);
607 * Note: that we don't care about tdb->hdr_ofs != 0 here
609 * The 4GB limitation is just related to tdb->map_size
610 * and the offset calculation in the records.
612 * The file on disk can be up to 4GB + tdb->hdr_ofs
614 size = tdb_expand_adjust(tdb->map_size, size, tdb->page_size);
616 if (!tdb_add_off_t(tdb->map_size, size, &new_size)) {
617 tdb->ecode = TDB_ERR_OOM;
618 TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_expand "
619 "overflow detected current map_size[%u] size[%u]!\n",
620 (unsigned)tdb->map_size, (unsigned)size));
624 /* form a new freelist record */
625 offset = tdb->map_size;
626 memset(&rec,'\0',sizeof(rec));
627 rec.rec_len = size - sizeof(rec);
629 if (tdb->flags & TDB_INTERNAL) {
632 new_map_ptr = (char *)realloc(tdb->map_ptr, new_size);
634 tdb->ecode = TDB_ERR_OOM;
637 tdb->map_ptr = new_map_ptr;
638 tdb->map_size = new_size;
643 * expand the file itself
645 ret = tdb->methods->tdb_expand_file(tdb, tdb->map_size, size);
650 /* Explicitly remap: if we're in a transaction, this won't
651 * happen automatically! */
653 tdb->map_size = new_size;
654 if (tdb_mmap(tdb) != 0) {
659 /* link it into the free list */
660 if (tdb_free(tdb, offset, &rec) == -1)
663 tdb_unlock(tdb, -1, F_WRLCK);
666 tdb_unlock(tdb, -1, F_WRLCK);
670 int _tdb_oob(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len, int probe)
672 int ret = tdb->methods->tdb_oob(tdb, off, len, probe);
676 /* read/write a tdb_off_t */
677 int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d)
679 return tdb->methods->tdb_read(tdb, offset, (char*)d, sizeof(*d), DOCONV());
682 int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d)
685 return tdb->methods->tdb_write(tdb, offset, CONVERT(off), sizeof(*d));
689 /* read a lump of data, allocating the space for it */
690 unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len)
694 /* some systems don't like zero length malloc */
696 if (!(buf = (unsigned char *)malloc(len ? len : 1))) {
697 /* Ensure ecode is set for log fn. */
698 tdb->ecode = TDB_ERR_OOM;
699 TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_alloc_read malloc failed len=%u (%s)\n",
700 len, strerror(errno)));
703 if (tdb->methods->tdb_read(tdb, offset, buf, len, 0) == -1) {
710 /* Give a piece of tdb data to a parser */
712 int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
713 tdb_off_t offset, tdb_len_t len,
714 int (*parser)(TDB_DATA key, TDB_DATA data,
723 if ((tdb->transaction == NULL) && (tdb->map_ptr != NULL)) {
725 * Optimize by avoiding the malloc/memcpy/free, point the
726 * parser directly at the mmap area.
728 if (tdb_oob(tdb, offset, len, 0) != 0) {
731 data.dptr = offset + (unsigned char *)tdb->map_ptr;
732 return parser(key, data, private_data);
735 if (!(data.dptr = tdb_alloc_read(tdb, offset, len))) {
739 result = parser(key, data, private_data);
744 /* read/write a record */
745 int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
748 tdb_len_t overall_len;
750 if (tdb->methods->tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
752 if (TDB_BAD_MAGIC(rec)) {
753 /* Ensure ecode is set for log fn. */
754 tdb->ecode = TDB_ERR_CORRUPT;
755 TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_rec_read bad magic 0x%x at offset=%u\n", rec->magic, offset));
759 overall_len = rec->key_len + rec->data_len;
760 if (overall_len < rec->data_len) {
765 if (overall_len > rec->rec_len) {
770 ret = tdb_oob(tdb, offset, rec->key_len, 1);
774 ret = tdb_oob(tdb, offset, rec->data_len, 1);
778 ret = tdb_oob(tdb, offset, rec->rec_len, 1);
783 return tdb_oob(tdb, rec->next, sizeof(*rec), 0);
786 int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *rec)
788 struct tdb_record r = *rec;
789 return tdb->methods->tdb_write(tdb, offset, CONVERT(r), sizeof(r));
792 static const struct tdb_methods io_methods = {
801 initialise the default methods table
803 void tdb_io_init(struct tdb_context *tdb)
805 tdb->methods = &io_methods;