Improve the VFS Makefile so that it is easier for use out of tree but still works...
[Samba/gebeck_regimport.git] / lib / tdb / common / io.c
blob3131f4fa0af6fb1e331a918526f2d8986cd3980f
1 /*
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
12 ** under the LGPL
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
33 if necessary
35 static int tdb_oob(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len,
36 int probe)
38 struct stat st;
39 if (len + off < len) {
40 if (!probe) {
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",
44 (int)off, (int)len));
46 return -1;
49 if (off + len <= tdb->map_size)
50 return 0;
51 if (tdb->flags & TDB_INTERNAL) {
52 if (!probe) {
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));
58 return -1;
61 if (fstat(tdb->fd, &st) == -1) {
62 tdb->ecode = TDB_ERR_IO;
63 return -1;
66 if (st.st_size < (size_t)off + len) {
67 if (!probe) {
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));
73 return -1;
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));
82 return -1;
85 /* Unmap, update size, remap */
86 if (tdb_munmap(tdb) == -1) {
87 tdb->ecode = TDB_ERR_IO;
88 return -1;
90 tdb->map_size = st.st_size;
91 return tdb_mmap(tdb);
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)
98 if (len == 0) {
99 return 0;
102 if (tdb->read_only || tdb->traverse_read) {
103 tdb->ecode = TDB_ERR_RDONLY;
104 return -1;
107 if (tdb->methods->tdb_oob(tdb, off, len, 0) != 0)
108 return -1;
110 if (tdb->map_ptr) {
111 memcpy(off + (char *)tdb->map_ptr, buf, len);
112 } else {
113 #ifdef HAVE_INCOHERENT_MMAP
114 tdb->ecode = TDB_ERR_IO;
115 return -1;
116 #else
117 ssize_t written = pwrite(tdb->fd, buf, len, off);
118 if ((written != (ssize_t)len) && (written != -1)) {
119 /* try once more */
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,
125 len-written,
126 off+written);
128 if (written == -1) {
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)));
133 return -1;
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",
138 len, off));
139 return -1;
141 #endif
143 return 0;
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]);
152 return buf;
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) {
161 return -1;
164 if (tdb->map_ptr) {
165 memcpy(buf, off + (char *)tdb->map_ptr, len);
166 } else {
167 #ifdef HAVE_INCOHERENT_MMAP
168 tdb->ecode = TDB_ERR_IO;
169 return -1;
170 #else
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));
179 return -1;
181 #endif
183 if (cv) {
184 tdb_convert(buf, len);
186 return 0;
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)
197 uint32_t h = *chain;
198 if (tdb->map_ptr) {
199 for (;h < tdb->header.hash_size;h++) {
200 if (0 != *(uint32_t *)(TDB_HASH_TOP(h) + (unsigned char *)tdb->map_ptr)) {
201 break;
204 } else {
205 uint32_t off=0;
206 for (;h < tdb->header.hash_size;h++) {
207 if (tdb_ofs_read(tdb, TDB_HASH_TOP(h), &off) != 0 || off != 0) {
208 break;
212 (*chain) = h;
216 int tdb_munmap(struct tdb_context *tdb)
218 if (tdb->flags & TDB_INTERNAL)
219 return 0;
221 #ifdef HAVE_MMAP
222 if (tdb->map_ptr) {
223 int ret;
225 ret = munmap(tdb->map_ptr, tdb->map_size);
226 if (ret != 0)
227 return ret;
229 #endif
230 tdb->map_ptr = NULL;
231 return 0;
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
238 return true;
239 #else
240 return !(tdb->flags & TDB_NOMMAP);
241 #endif
244 int tdb_mmap(struct tdb_context *tdb)
246 if (tdb->flags & TDB_INTERNAL)
247 return 0;
249 #ifdef HAVE_MMAP
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) {
260 tdb->map_ptr = NULL;
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;
265 return -1;
266 #endif
268 } else {
269 tdb->map_ptr = NULL;
271 #else
272 tdb->map_ptr = NULL;
273 #endif
274 return 0;
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)
281 char buf[8192];
283 if (tdb->read_only || tdb->traverse_read) {
284 tdb->ecode = TDB_ERR_RDONLY;
285 return -1;
288 if (ftruncate(tdb->fd, size+addition) == -1) {
289 char b = 0;
290 ssize_t written = pwrite(tdb->fd, &b, 1, (size+addition) - 1);
291 if (written == 0) {
292 /* try once more, potentially revealing errno */
293 written = pwrite(tdb->fd, &b, 1, (size+addition) - 1);
295 if (written == 0) {
296 /* again - give up, guessing errno */
297 errno = ENOSPC;
299 if (written != 1) {
300 TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n",
301 size+addition, strerror(errno)));
302 return -1;
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));
310 while (addition) {
311 size_t n = addition>sizeof(buf)?sizeof(buf):addition;
312 ssize_t written = pwrite(tdb->fd, buf, n, size);
313 if (written == 0) {
314 /* prevent infinite loops: try _once_ more */
315 written = pwrite(tdb->fd, buf, n, size);
317 if (written == 0) {
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"));
321 errno = ENOSPC;
322 return -1;
323 } else if (written == -1) {
324 TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file write of "
325 "%d bytes failed (%s)\n", (int)n,
326 strerror(errno)));
327 return -1;
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,
331 (int)n));
333 addition -= written;
334 size += written;
336 return 0;
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;
349 } else {
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;
358 } else {
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;
372 tdb_off_t offset;
374 if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
375 TDB_LOG((tdb, TDB_DEBUG_ERROR, "lock failed in tdb_expand\n"));
376 return -1;
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)
387 goto fail;
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);
398 if (!new_map_ptr) {
399 goto fail;
401 tdb->map_ptr = new_map_ptr;
402 tdb->map_size += size;
403 } else {
404 /* Explicitly remap: if we're in a transaction, this won't
405 * happen automatically! */
406 tdb_munmap(tdb);
407 tdb->map_size += size;
408 if (tdb_mmap(tdb) != 0) {
409 goto fail;
413 /* link it into the free list */
414 if (tdb_free(tdb, offset, &rec) == -1)
415 goto fail;
417 tdb_unlock(tdb, -1, F_WRLCK);
418 return 0;
419 fail:
420 tdb_unlock(tdb, -1, F_WRLCK);
421 return -1;
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)
432 tdb_off_t off = *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)
440 unsigned char *buf;
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)));
449 return NULL;
451 if (tdb->methods->tdb_read(tdb, offset, buf, len, 0) == -1) {
452 SAFE_FREE(buf);
453 return NULL;
455 return buf;
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,
463 void *private_data),
464 void *private_data)
466 TDB_DATA data;
467 int result;
469 data.dsize = len;
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) {
477 return -1;
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))) {
484 return -1;
487 result = parser(key, data, private_data);
488 free(data.dptr);
489 return result;
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)
496 return -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));
501 return -1;
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 = {
513 tdb_read,
514 tdb_write,
515 tdb_next_hash_chain,
516 tdb_oob,
517 tdb_expand_file,
521 initialise the default methods table
523 void tdb_io_init(struct tdb_context *tdb)
525 tdb->methods = &io_methods;