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"
31 /* check for an out of bounds access - if it is out of bounds then
32 see if the database has been expanded by someone else and expand
35 static int tdb_oob(struct tdb_context
*tdb
, tdb_off_t off
, tdb_len_t len
,
39 if (len
+ off
< len
) {
41 /* Ensure ecode is set for log fn. */
42 tdb
->ecode
= TDB_ERR_IO
;
43 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_oob off %d len %d wrap\n",
49 if (off
+ len
<= tdb
->map_size
)
51 if (tdb
->flags
& TDB_INTERNAL
) {
53 /* Ensure ecode is set for log fn. */
54 tdb
->ecode
= TDB_ERR_IO
;
55 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_oob len %u beyond internal malloc size %u\n",
56 (int)(off
+ len
), (int)tdb
->map_size
));
61 if (fstat(tdb
->fd
, &st
) == -1) {
62 tdb
->ecode
= TDB_ERR_IO
;
66 if (st
.st_size
< (size_t)off
+ len
) {
68 /* Ensure ecode is set for log fn. */
69 tdb
->ecode
= TDB_ERR_IO
;
70 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_oob len %u beyond eof at %u\n",
71 (int)(off
+ len
), (int)st
.st_size
));
76 /* Beware >4G files! */
77 if ((tdb_off_t
)st
.st_size
!= st
.st_size
) {
78 /* Ensure ecode is set for log fn. */
79 tdb
->ecode
= TDB_ERR_IO
;
80 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_oob len %llu too large!\n",
81 (long long)st
.st_size
));
85 /* Unmap, update size, remap */
86 if (tdb_munmap(tdb
) == -1) {
87 tdb
->ecode
= TDB_ERR_IO
;
90 tdb
->map_size
= st
.st_size
;
94 /* write a lump of data at a specified offset */
95 static int tdb_write(struct tdb_context
*tdb
, tdb_off_t off
,
96 const void *buf
, tdb_len_t len
)
102 if (tdb
->read_only
|| tdb
->traverse_read
) {
103 tdb
->ecode
= TDB_ERR_RDONLY
;
107 if (tdb
->methods
->tdb_oob(tdb
, off
, len
, 0) != 0)
111 memcpy(off
+ (char *)tdb
->map_ptr
, buf
, len
);
113 #ifdef HAVE_INCOHERENT_MMAP
114 tdb
->ecode
= TDB_ERR_IO
;
117 ssize_t written
= pwrite(tdb
->fd
, buf
, len
, off
);
118 if ((written
!= (ssize_t
)len
) && (written
!= -1)) {
120 tdb
->ecode
= TDB_ERR_IO
;
121 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_write: wrote only "
122 "%d of %d bytes at %d, trying once more\n",
123 (int)written
, len
, off
));
124 written
= pwrite(tdb
->fd
, (const char *)buf
+written
,
129 /* Ensure ecode is set for log fn. */
130 tdb
->ecode
= TDB_ERR_IO
;
131 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_write failed at %d "
132 "len=%d (%s)\n", off
, len
, strerror(errno
)));
134 } else if (written
!= (ssize_t
)len
) {
135 tdb
->ecode
= TDB_ERR_IO
;
136 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_write: failed to "
137 "write %d bytes at %d in two attempts\n",
146 /* Endian conversion: we only ever deal with 4 byte quantities */
147 void *tdb_convert(void *buf
, uint32_t size
)
149 uint32_t i
, *p
= (uint32_t *)buf
;
150 for (i
= 0; i
< size
/ 4; i
++)
151 p
[i
] = TDB_BYTEREV(p
[i
]);
156 /* read a lump of data at a specified offset, maybe convert */
157 static int tdb_read(struct tdb_context
*tdb
, tdb_off_t off
, void *buf
,
158 tdb_len_t len
, int cv
)
160 if (tdb
->methods
->tdb_oob(tdb
, off
, len
, 0) != 0) {
165 memcpy(buf
, off
+ (char *)tdb
->map_ptr
, len
);
167 #ifdef HAVE_INCOHERENT_MMAP
168 tdb
->ecode
= TDB_ERR_IO
;
171 ssize_t ret
= pread(tdb
->fd
, buf
, len
, off
);
172 if (ret
!= (ssize_t
)len
) {
173 /* Ensure ecode is set for log fn. */
174 tdb
->ecode
= TDB_ERR_IO
;
175 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_read failed at %d "
176 "len=%d ret=%d (%s) map_size=%d\n",
177 (int)off
, (int)len
, (int)ret
, strerror(errno
),
178 (int)tdb
->map_size
));
184 tdb_convert(buf
, len
);
192 do an unlocked scan of the hash table heads to find the next non-zero head. The value
193 will then be confirmed with the lock held
195 static void tdb_next_hash_chain(struct tdb_context
*tdb
, uint32_t *chain
)
199 for (;h
< tdb
->header
.hash_size
;h
++) {
200 if (0 != *(uint32_t *)(TDB_HASH_TOP(h
) + (unsigned char *)tdb
->map_ptr
)) {
206 for (;h
< tdb
->header
.hash_size
;h
++) {
207 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(h
), &off
) != 0 || off
!= 0) {
216 int tdb_munmap(struct tdb_context
*tdb
)
218 if (tdb
->flags
& TDB_INTERNAL
)
225 ret
= munmap(tdb
->map_ptr
, tdb
->map_size
);
234 /* If mmap isn't coherent, *everyone* must always mmap. */
235 static bool should_mmap(const struct tdb_context
*tdb
)
237 #ifdef HAVE_INCOHERENT_MMAP
240 return !(tdb
->flags
& TDB_NOMMAP
);
244 int tdb_mmap(struct tdb_context
*tdb
)
246 if (tdb
->flags
& TDB_INTERNAL
)
250 if (should_mmap(tdb
)) {
251 tdb
->map_ptr
= mmap(NULL
, tdb
->map_size
,
252 PROT_READ
|(tdb
->read_only
? 0:PROT_WRITE
),
253 MAP_SHARED
|MAP_FILE
, tdb
->fd
, 0);
256 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
259 if (tdb
->map_ptr
== MAP_FAILED
) {
261 TDB_LOG((tdb
, TDB_DEBUG_WARNING
, "tdb_mmap failed for size %d (%s)\n",
262 tdb
->map_size
, strerror(errno
)));
263 #ifdef HAVE_INCOHERENT_MMAP
264 tdb
->ecode
= TDB_ERR_IO
;
277 /* expand a file. we prefer to use ftruncate, as that is what posix
278 says to use for mmap expansion */
279 static int tdb_expand_file(struct tdb_context
*tdb
, tdb_off_t size
, tdb_off_t addition
)
283 if (tdb
->read_only
|| tdb
->traverse_read
) {
284 tdb
->ecode
= TDB_ERR_RDONLY
;
288 if (ftruncate(tdb
->fd
, size
+addition
) == -1) {
290 ssize_t written
= pwrite(tdb
->fd
, &b
, 1, (size
+addition
) - 1);
292 /* try once more, potentially revealing errno */
293 written
= pwrite(tdb
->fd
, &b
, 1, (size
+addition
) - 1);
296 /* again - give up, guessing errno */
300 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "expand_file to %d failed (%s)\n",
301 size
+addition
, strerror(errno
)));
306 /* now fill the file with something. This ensures that the
307 file isn't sparse, which would be very bad if we ran out of
308 disk. This must be done with write, not via mmap */
309 memset(buf
, TDB_PAD_BYTE
, sizeof(buf
));
311 size_t n
= addition
>sizeof(buf
)?sizeof(buf
):addition
;
312 ssize_t written
= pwrite(tdb
->fd
, buf
, n
, size
);
314 /* prevent infinite loops: try _once_ more */
315 written
= pwrite(tdb
->fd
, buf
, n
, size
);
318 /* give up, trying to provide a useful errno */
319 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "expand_file write "
320 "returned 0 twice: giving up!\n"));
323 } else if (written
== -1) {
324 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "expand_file write of "
325 "%d bytes failed (%s)\n", (int)n
,
328 } else if (written
!= n
) {
329 TDB_LOG((tdb
, TDB_DEBUG_WARNING
, "expand_file: wrote "
330 "only %d of %d bytes - retrying\n", (int)written
,
340 /* You need 'size', this tells you how much you should expand by. */
341 tdb_off_t
tdb_expand_adjust(tdb_off_t map_size
, tdb_off_t size
, int page_size
)
343 tdb_off_t new_size
, top_size
;
345 /* limit size in order to avoid using up huge amounts of memory for
346 * in memory tdbs if an oddball huge record creeps in */
347 if (size
> 100 * 1024) {
348 top_size
= map_size
+ size
* 2;
350 top_size
= map_size
+ size
* 100;
353 /* always make room for at least top_size more records, and at
354 least 25% more space. if the DB is smaller than 100MiB,
355 otherwise grow it by 10% only. */
356 if (map_size
> 100 * 1024 * 1024) {
357 new_size
= map_size
* 1.10;
359 new_size
= map_size
* 1.25;
362 /* Round the database up to a multiple of the page size */
363 new_size
= MAX(top_size
, new_size
);
364 return TDB_ALIGN(new_size
, page_size
) - map_size
;
367 /* expand the database at least size bytes by expanding the underlying
368 file and doing the mmap again if necessary */
369 int tdb_expand(struct tdb_context
*tdb
, tdb_off_t size
)
371 struct tdb_record rec
;
374 if (tdb_lock(tdb
, -1, F_WRLCK
) == -1) {
375 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "lock failed in tdb_expand\n"));
379 /* must know about any previous expansions by another process */
380 tdb
->methods
->tdb_oob(tdb
, tdb
->map_size
, 1, 1);
382 size
= tdb_expand_adjust(tdb
->map_size
, size
, tdb
->page_size
);
384 /* expand the file itself */
385 if (!(tdb
->flags
& TDB_INTERNAL
)) {
386 if (tdb
->methods
->tdb_expand_file(tdb
, tdb
->map_size
, size
) != 0)
390 /* form a new freelist record */
391 offset
= tdb
->map_size
;
392 memset(&rec
,'\0',sizeof(rec
));
393 rec
.rec_len
= size
- sizeof(rec
);
395 if (tdb
->flags
& TDB_INTERNAL
) {
396 char *new_map_ptr
= (char *)realloc(tdb
->map_ptr
,
397 tdb
->map_size
+ size
);
401 tdb
->map_ptr
= new_map_ptr
;
402 tdb
->map_size
+= size
;
404 /* Explicitly remap: if we're in a transaction, this won't
405 * happen automatically! */
407 tdb
->map_size
+= size
;
408 if (tdb_mmap(tdb
) != 0) {
413 /* link it into the free list */
414 if (tdb_free(tdb
, offset
, &rec
) == -1)
417 tdb_unlock(tdb
, -1, F_WRLCK
);
420 tdb_unlock(tdb
, -1, F_WRLCK
);
424 /* read/write a tdb_off_t */
425 int tdb_ofs_read(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_off_t
*d
)
427 return tdb
->methods
->tdb_read(tdb
, offset
, (char*)d
, sizeof(*d
), DOCONV());
430 int tdb_ofs_write(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_off_t
*d
)
433 return tdb
->methods
->tdb_write(tdb
, offset
, CONVERT(off
), sizeof(*d
));
437 /* read a lump of data, allocating the space for it */
438 unsigned char *tdb_alloc_read(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_len_t len
)
442 /* some systems don't like zero length malloc */
444 if (!(buf
= (unsigned char *)malloc(len
? len
: 1))) {
445 /* Ensure ecode is set for log fn. */
446 tdb
->ecode
= TDB_ERR_OOM
;
447 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,"tdb_alloc_read malloc failed len=%d (%s)\n",
448 len
, strerror(errno
)));
451 if (tdb
->methods
->tdb_read(tdb
, offset
, buf
, len
, 0) == -1) {
458 /* Give a piece of tdb data to a parser */
460 int tdb_parse_data(struct tdb_context
*tdb
, TDB_DATA key
,
461 tdb_off_t offset
, tdb_len_t len
,
462 int (*parser
)(TDB_DATA key
, TDB_DATA data
,
471 if ((tdb
->transaction
== NULL
) && (tdb
->map_ptr
!= NULL
)) {
473 * Optimize by avoiding the malloc/memcpy/free, point the
474 * parser directly at the mmap area.
476 if (tdb
->methods
->tdb_oob(tdb
, offset
, len
, 0) != 0) {
479 data
.dptr
= offset
+ (unsigned char *)tdb
->map_ptr
;
480 return parser(key
, data
, private_data
);
483 if (!(data
.dptr
= tdb_alloc_read(tdb
, offset
, len
))) {
487 result
= parser(key
, data
, private_data
);
492 /* read/write a record */
493 int tdb_rec_read(struct tdb_context
*tdb
, tdb_off_t offset
, struct tdb_record
*rec
)
495 if (tdb
->methods
->tdb_read(tdb
, offset
, rec
, sizeof(*rec
),DOCONV()) == -1)
497 if (TDB_BAD_MAGIC(rec
)) {
498 /* Ensure ecode is set for log fn. */
499 tdb
->ecode
= TDB_ERR_CORRUPT
;
500 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec
->magic
, offset
));
503 return tdb
->methods
->tdb_oob(tdb
, rec
->next
, sizeof(*rec
), 0);
506 int tdb_rec_write(struct tdb_context
*tdb
, tdb_off_t offset
, struct tdb_record
*rec
)
508 struct tdb_record r
= *rec
;
509 return tdb
->methods
->tdb_write(tdb
, offset
, CONVERT(r
), sizeof(r
));
512 static const struct tdb_methods io_methods
= {
521 initialise the default methods table
523 void tdb_io_init(struct tdb_context
*tdb
)
525 tdb
->methods
= &io_methods
;