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 2 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, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "tdb_private.h"
33 static ssize_t pread(int fd, void *buf, size_t count, off_t offset)
35 if (lseek(fd, offset, SEEK_SET) != offset) {
39 return read(fd, buf, count);
44 static ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
46 if (lseek(fd, offset, SEEK_SET) != offset) {
50 return write(fd, buf, count);
54 /* check for an out of bounds access - if it is out of bounds then
55 see if the database has been expanded by someone else and expand
57 note that "len" is the minimum length needed for the db
59 int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
62 if (len <= tdb->map_size)
64 if (tdb->flags & TDB_INTERNAL) {
66 /* Ensure ecode is set for log fn. */
67 tdb->ecode = TDB_ERR_IO;
68 TDB_LOG((tdb, 0,"tdb_oob len %d beyond internal malloc size %d\n",
69 (int)len, (int)tdb->map_size));
71 return TDB_ERRCODE(TDB_ERR_IO, -1);
74 if (fstat(tdb->fd, &st) == -1) {
75 return TDB_ERRCODE(TDB_ERR_IO, -1);
78 if (st.st_size < (size_t)len) {
80 /* Ensure ecode is set for log fn. */
81 tdb->ecode = TDB_ERR_IO;
82 TDB_LOG((tdb, 0,"tdb_oob len %d beyond eof at %d\n",
83 (int)len, (int)st.st_size));
85 return TDB_ERRCODE(TDB_ERR_IO, -1);
88 /* Unmap, update size, remap */
89 if (tdb_munmap(tdb) == -1)
90 return TDB_ERRCODE(TDB_ERR_IO, -1);
91 tdb->map_size = st.st_size;
96 /* write a lump of data at a specified offset */
97 int tdb_write(struct tdb_context *tdb, tdb_off_t off, void *buf, tdb_len_t len)
99 if (tdb_oob(tdb, off + len, 0) != 0)
103 memcpy(off + (char *)tdb->map_ptr, buf, len);
104 } else if (pwrite(tdb->fd, buf, len, off) != (ssize_t)len) {
105 /* Ensure ecode is set for log fn. */
106 tdb->ecode = TDB_ERR_IO;
107 TDB_LOG((tdb, 0,"tdb_write failed at %d len=%d (%s)\n",
108 off, len, strerror(errno)));
109 return TDB_ERRCODE(TDB_ERR_IO, -1);
114 /* Endian conversion: we only ever deal with 4 byte quantities */
115 void *tdb_convert(void *buf, u32 size)
118 for (i = 0; i < size / 4; i++)
119 p[i] = TDB_BYTEREV(p[i]);
124 /* read a lump of data at a specified offset, maybe convert */
125 int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf, tdb_len_t len, int cv)
127 if (tdb_oob(tdb, off + len, 0) != 0) {
132 memcpy(buf, off + (char *)tdb->map_ptr, len);
134 ssize_t ret = pread(tdb->fd, buf, len, off);
135 if (ret != (ssize_t)len) {
136 /* Ensure ecode is set for log fn. */
137 tdb->ecode = TDB_ERR_IO;
138 TDB_LOG((tdb, 0,"tdb_read failed at %d len=%d ret=%d (%s) map_size=%d\n",
139 off, len, ret, strerror(errno), tdb->map_size));
140 return TDB_ERRCODE(TDB_ERR_IO, -1);
144 tdb_convert(buf, len);
151 do an unlocked scan of the hash table heads to find the next non-zero head. The value
152 will then be confirmed with the lock held
154 void tdb_next_hash_chain(struct tdb_context *tdb, u32 *chain)
158 for (;h < tdb->header.hash_size;h++) {
159 if (0 != *(u32 *)(TDB_HASH_TOP(h) + (unsigned char *)tdb->map_ptr)) {
165 for (;h < tdb->header.hash_size;h++) {
166 if (tdb_ofs_read(tdb, TDB_HASH_TOP(h), &off) != 0 || off != 0) {
175 int tdb_munmap(struct tdb_context *tdb)
177 if (tdb->flags & TDB_INTERNAL)
182 int ret = munmap(tdb->map_ptr, tdb->map_size);
191 void tdb_mmap(struct tdb_context *tdb)
193 if (tdb->flags & TDB_INTERNAL)
197 if (!(tdb->flags & TDB_NOMMAP)) {
198 tdb->map_ptr = mmap(NULL, tdb->map_size,
199 PROT_READ|(tdb->read_only? 0:PROT_WRITE),
200 MAP_SHARED|MAP_FILE, tdb->fd, 0);
203 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
206 if (tdb->map_ptr == MAP_FAILED) {
208 TDB_LOG((tdb, 2, "tdb_mmap failed for size %d (%s)\n",
209 tdb->map_size, strerror(errno)));
219 /* expand a file. we prefer to use ftruncate, as that is what posix
220 says to use for mmap expansion */
221 static int tdb_expand_file(struct tdb_context *tdb, tdb_off_t size, tdb_off_t addition)
224 if (ftruncate(tdb->fd, size+addition) == -1) {
226 if (pwrite(tdb->fd, &b, 1, (size+addition) - 1) != 1) {
227 TDB_LOG((tdb, 0, "expand_file to %d failed (%s)\n",
228 size+addition, strerror(errno)));
233 /* now fill the file with something. This ensures that the file isn't sparse, which would be
234 very bad if we ran out of disk. This must be done with write, not via mmap */
235 memset(buf, 0x42, sizeof(buf));
237 int n = addition>sizeof(buf)?sizeof(buf):addition;
238 int ret = pwrite(tdb->fd, buf, n, size);
240 TDB_LOG((tdb, 0, "expand_file write of %d failed (%s)\n",
241 n, strerror(errno)));
251 /* expand the database at least size bytes by expanding the underlying
252 file and doing the mmap again if necessary */
253 int tdb_expand(struct tdb_context *tdb, tdb_off_t size)
255 struct list_struct rec;
258 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
259 TDB_LOG((tdb, 0, "lock failed in tdb_expand\n"));
263 /* must know about any previous expansions by another process */
264 tdb_oob(tdb, tdb->map_size + 1, 1);
266 /* always make room for at least 10 more records, and round
267 the database up to a multiple of TDB_PAGE_SIZE */
268 size = TDB_ALIGN(tdb->map_size + size*10, TDB_PAGE_SIZE) - tdb->map_size;
270 if (!(tdb->flags & TDB_INTERNAL))
274 * We must ensure the file is unmapped before doing this
275 * to ensure consistency with systems like OpenBSD where
276 * writes and mmaps are not consistent.
279 /* expand the file itself */
280 if (!(tdb->flags & TDB_INTERNAL)) {
281 if (tdb_expand_file(tdb, tdb->map_size, size) != 0)
285 tdb->map_size += size;
287 if (tdb->flags & TDB_INTERNAL) {
288 char *new_map_ptr = realloc(tdb->map_ptr, tdb->map_size);
290 tdb->map_size -= size;
293 tdb->map_ptr = new_map_ptr;
296 * We must ensure the file is remapped before adding the space
297 * to ensure consistency with systems like OpenBSD where
298 * writes and mmaps are not consistent.
301 /* We're ok if the mmap fails as we'll fallback to read/write */
305 /* form a new freelist record */
306 memset(&rec,'\0',sizeof(rec));
307 rec.rec_len = size - sizeof(rec);
309 /* link it into the free list */
310 offset = tdb->map_size - size;
311 if (tdb_free(tdb, offset, &rec) == -1)
314 tdb_unlock(tdb, -1, F_WRLCK);
317 tdb_unlock(tdb, -1, F_WRLCK);
321 /* read/write a tdb_off_t */
322 int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d)
324 return tdb_read(tdb, offset, (char*)d, sizeof(*d), DOCONV());
327 int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d)
330 return tdb_write(tdb, offset, CONVERT(off), sizeof(*d));
334 /* read a lump of data, allocating the space for it */
335 unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len)
339 if (!(buf = malloc(len))) {
340 /* Ensure ecode is set for log fn. */
341 tdb->ecode = TDB_ERR_OOM;
342 TDB_LOG((tdb, 0,"tdb_alloc_read malloc failed len=%d (%s)\n",
343 len, strerror(errno)));
344 return TDB_ERRCODE(TDB_ERR_OOM, buf);
346 if (tdb_read(tdb, offset, buf, len, 0) == -1) {
353 /* read/write a record */
354 int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
356 if (tdb_read(tdb, offset, rec, sizeof(*rec),DOCONV()) == -1)
358 if (TDB_BAD_MAGIC(rec)) {
359 /* Ensure ecode is set for log fn. */
360 tdb->ecode = TDB_ERR_CORRUPT;
361 TDB_LOG((tdb, 0,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
362 return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
364 return tdb_oob(tdb, rec->next+sizeof(*rec), 0);
367 int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
369 struct list_struct r = *rec;
370 return tdb_write(tdb, offset, CONVERT(r), sizeof(r));