ntdb: simply disallow NULL names.
[Samba/gbeck.git] / lib / ntdb / open.c
blob01a0928074ba61ebdb64dc38e6509c1725f47ae6
1 /*
2 Trivial Database 2: opening and closing TDBs
3 Copyright (C) Rusty Russell 2010
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 #include "private.h"
19 #include <ccan/build_assert/build_assert.h>
20 #include <assert.h>
22 /* all tdbs, to detect double-opens (fcntl file don't nest!) */
23 static struct ntdb_context *tdbs = NULL;
25 static struct ntdb_file *find_file(dev_t device, ino_t ino)
27 struct ntdb_context *i;
29 for (i = tdbs; i; i = i->next) {
30 if (i->file->device == device && i->file->inode == ino) {
31 i->file->refcnt++;
32 return i->file;
35 return NULL;
38 static bool read_all(int fd, void *buf, size_t len)
40 while (len) {
41 ssize_t ret;
42 ret = read(fd, buf, len);
43 if (ret < 0)
44 return false;
45 if (ret == 0) {
46 /* ETOOSHORT? */
47 errno = EWOULDBLOCK;
48 return false;
50 buf = (char *)buf + ret;
51 len -= ret;
53 return true;
56 static uint64_t random_number(struct ntdb_context *ntdb)
58 int fd;
59 uint64_t ret = 0;
60 struct timeval now;
62 fd = open("/dev/urandom", O_RDONLY);
63 if (fd >= 0) {
64 if (read_all(fd, &ret, sizeof(ret))) {
65 close(fd);
66 return ret;
68 close(fd);
70 /* FIXME: Untested! Based on Wikipedia protocol description! */
71 fd = open("/dev/egd-pool", O_RDWR);
72 if (fd >= 0) {
73 /* Command is 1, next byte is size we want to read. */
74 char cmd[2] = { 1, sizeof(uint64_t) };
75 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
76 char reply[1 + sizeof(uint64_t)];
77 int r = read(fd, reply, sizeof(reply));
78 if (r > 1) {
79 /* Copy at least some bytes. */
80 memcpy(&ret, reply+1, r - 1);
81 if (reply[0] == sizeof(uint64_t)
82 && r == sizeof(reply)) {
83 close(fd);
84 return ret;
88 close(fd);
91 /* Fallback: pid and time. */
92 gettimeofday(&now, NULL);
93 ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
94 ntdb_logerr(ntdb, NTDB_SUCCESS, NTDB_LOG_WARNING,
95 "ntdb_open: random from getpid and time");
96 return ret;
99 static void ntdb_context_init(struct ntdb_context *ntdb)
101 /* Initialize the NTDB fields here */
102 ntdb_io_init(ntdb);
103 ntdb->direct_access = 0;
104 ntdb->transaction = NULL;
105 ntdb->access = NULL;
108 struct new_database {
109 struct ntdb_header hdr;
110 struct ntdb_freetable ftable;
111 struct ntdb_free_record remainder;
114 /* initialise a new database */
115 static enum NTDB_ERROR ntdb_new_database(struct ntdb_context *ntdb,
116 struct ntdb_attribute_seed *seed,
117 struct ntdb_header *hdr)
119 /* We make it up in memory, then write it out if not internal */
120 struct new_database *newdb;
121 unsigned int magic_len;
122 ssize_t rlen;
123 size_t dbsize, remaindersize;
124 enum NTDB_ERROR ecode;
126 /* Always make db a multiple of NTDB_PGSIZE */
127 dbsize = (sizeof(*newdb) + NTDB_PGSIZE-1) & ~(NTDB_PGSIZE-1);
128 remaindersize = dbsize - sizeof(*newdb);
129 newdb = malloc(dbsize);
130 if (!newdb) {
131 return ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
132 "ntdb_new_database: failed to allocate");
135 /* Fill in the header */
136 newdb->hdr.version = NTDB_VERSION;
137 if (seed)
138 newdb->hdr.hash_seed = seed->seed;
139 else
140 newdb->hdr.hash_seed = random_number(ntdb);
141 newdb->hdr.hash_test = NTDB_HASH_MAGIC;
142 newdb->hdr.hash_test = ntdb->hash_fn(&newdb->hdr.hash_test,
143 sizeof(newdb->hdr.hash_test),
144 newdb->hdr.hash_seed,
145 ntdb->hash_data);
146 newdb->hdr.recovery = 0;
147 newdb->hdr.features_used = newdb->hdr.features_offered = NTDB_FEATURE_MASK;
148 newdb->hdr.seqnum = 0;
149 newdb->hdr.capabilities = 0;
150 memset(newdb->hdr.reserved, 0, sizeof(newdb->hdr.reserved));
151 /* Initial hashes are empty. */
152 memset(newdb->hdr.hashtable, 0, sizeof(newdb->hdr.hashtable));
154 /* Free is empty. */
155 newdb->hdr.free_table = offsetof(struct new_database, ftable);
156 memset(&newdb->ftable, 0, sizeof(newdb->ftable));
157 ecode = set_header(NULL, &newdb->ftable.hdr, NTDB_FTABLE_MAGIC, 0,
158 sizeof(newdb->ftable) - sizeof(newdb->ftable.hdr),
159 sizeof(newdb->ftable) - sizeof(newdb->ftable.hdr),
161 if (ecode != NTDB_SUCCESS) {
162 goto out;
165 /* Rest of database is a free record, containing junk. */
166 newdb->remainder.ftable_and_len
167 = (remaindersize + sizeof(newdb->remainder)
168 - sizeof(struct ntdb_used_record));
169 newdb->remainder.next = 0;
170 newdb->remainder.magic_and_prev
171 = (NTDB_FREE_MAGIC << (64-NTDB_OFF_UPPER_STEAL))
172 | offsetof(struct new_database, remainder);
173 memset(&newdb->remainder + 1, 0x43, remaindersize);
175 /* Put in our single free entry. */
176 newdb->ftable.buckets[size_to_bucket(remaindersize)] =
177 offsetof(struct new_database, remainder);
179 /* Magic food */
180 memset(newdb->hdr.magic_food, 0, sizeof(newdb->hdr.magic_food));
181 strcpy(newdb->hdr.magic_food, NTDB_MAGIC_FOOD);
183 /* This creates an endian-converted database, as if read from disk */
184 magic_len = sizeof(newdb->hdr.magic_food);
185 ntdb_convert(ntdb,
186 (char *)&newdb->hdr + magic_len,
187 sizeof(*newdb) - magic_len);
189 *hdr = newdb->hdr;
191 if (ntdb->flags & NTDB_INTERNAL) {
192 ntdb->file->map_size = dbsize;
193 ntdb->file->map_ptr = newdb;
194 return NTDB_SUCCESS;
196 if (lseek(ntdb->file->fd, 0, SEEK_SET) == -1) {
197 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
198 "ntdb_new_database:"
199 " failed to seek: %s", strerror(errno));
200 goto out;
203 if (ftruncate(ntdb->file->fd, 0) == -1) {
204 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
205 "ntdb_new_database:"
206 " failed to truncate: %s", strerror(errno));
207 goto out;
210 rlen = write(ntdb->file->fd, newdb, dbsize);
211 if (rlen != dbsize) {
212 if (rlen >= 0)
213 errno = ENOSPC;
214 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
215 "ntdb_new_database: %zi writing header: %s",
216 rlen, strerror(errno));
217 goto out;
220 out:
221 free(newdb);
222 return ecode;
225 static enum NTDB_ERROR ntdb_new_file(struct ntdb_context *ntdb)
227 ntdb->file = malloc(sizeof(*ntdb->file));
228 if (!ntdb->file)
229 return ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
230 "ntdb_open: cannot alloc ntdb_file structure");
231 ntdb->file->num_lockrecs = 0;
232 ntdb->file->lockrecs = NULL;
233 ntdb->file->allrecord_lock.count = 0;
234 ntdb->file->refcnt = 1;
235 ntdb->file->map_ptr = NULL;
236 return NTDB_SUCCESS;
239 _PUBLIC_ enum NTDB_ERROR ntdb_set_attribute(struct ntdb_context *ntdb,
240 const union ntdb_attribute *attr)
242 switch (attr->base.attr) {
243 case NTDB_ATTRIBUTE_LOG:
244 ntdb->log_fn = attr->log.fn;
245 ntdb->log_data = attr->log.data;
246 break;
247 case NTDB_ATTRIBUTE_HASH:
248 case NTDB_ATTRIBUTE_SEED:
249 case NTDB_ATTRIBUTE_OPENHOOK:
250 return ntdb_logerr(ntdb, NTDB_ERR_EINVAL,
251 NTDB_LOG_USE_ERROR,
252 "ntdb_set_attribute:"
253 " cannot set %s after opening",
254 attr->base.attr == NTDB_ATTRIBUTE_HASH
255 ? "NTDB_ATTRIBUTE_HASH"
256 : attr->base.attr == NTDB_ATTRIBUTE_SEED
257 ? "NTDB_ATTRIBUTE_SEED"
258 : "NTDB_ATTRIBUTE_OPENHOOK");
259 case NTDB_ATTRIBUTE_STATS:
260 return ntdb_logerr(ntdb, NTDB_ERR_EINVAL,
261 NTDB_LOG_USE_ERROR,
262 "ntdb_set_attribute:"
263 " cannot set NTDB_ATTRIBUTE_STATS");
264 case NTDB_ATTRIBUTE_FLOCK:
265 ntdb->lock_fn = attr->flock.lock;
266 ntdb->unlock_fn = attr->flock.unlock;
267 ntdb->lock_data = attr->flock.data;
268 break;
269 default:
270 return ntdb_logerr(ntdb, NTDB_ERR_EINVAL,
271 NTDB_LOG_USE_ERROR,
272 "ntdb_set_attribute:"
273 " unknown attribute type %u",
274 attr->base.attr);
276 return NTDB_SUCCESS;
279 _PUBLIC_ enum NTDB_ERROR ntdb_get_attribute(struct ntdb_context *ntdb,
280 union ntdb_attribute *attr)
282 switch (attr->base.attr) {
283 case NTDB_ATTRIBUTE_LOG:
284 if (!ntdb->log_fn)
285 return NTDB_ERR_NOEXIST;
286 attr->log.fn = ntdb->log_fn;
287 attr->log.data = ntdb->log_data;
288 break;
289 case NTDB_ATTRIBUTE_HASH:
290 attr->hash.fn = ntdb->hash_fn;
291 attr->hash.data = ntdb->hash_data;
292 break;
293 case NTDB_ATTRIBUTE_SEED:
294 attr->seed.seed = ntdb->hash_seed;
295 break;
296 case NTDB_ATTRIBUTE_OPENHOOK:
297 if (!ntdb->openhook)
298 return NTDB_ERR_NOEXIST;
299 attr->openhook.fn = ntdb->openhook;
300 attr->openhook.data = ntdb->openhook_data;
301 break;
302 case NTDB_ATTRIBUTE_STATS: {
303 size_t size = attr->stats.size;
304 if (size > ntdb->stats.size)
305 size = ntdb->stats.size;
306 memcpy(&attr->stats, &ntdb->stats, size);
307 break;
309 case NTDB_ATTRIBUTE_FLOCK:
310 attr->flock.lock = ntdb->lock_fn;
311 attr->flock.unlock = ntdb->unlock_fn;
312 attr->flock.data = ntdb->lock_data;
313 break;
314 default:
315 return ntdb_logerr(ntdb, NTDB_ERR_EINVAL,
316 NTDB_LOG_USE_ERROR,
317 "ntdb_get_attribute:"
318 " unknown attribute type %u",
319 attr->base.attr);
321 attr->base.next = NULL;
322 return NTDB_SUCCESS;
325 _PUBLIC_ void ntdb_unset_attribute(struct ntdb_context *ntdb,
326 enum ntdb_attribute_type type)
328 switch (type) {
329 case NTDB_ATTRIBUTE_LOG:
330 ntdb->log_fn = NULL;
331 break;
332 case NTDB_ATTRIBUTE_OPENHOOK:
333 ntdb->openhook = NULL;
334 break;
335 case NTDB_ATTRIBUTE_HASH:
336 case NTDB_ATTRIBUTE_SEED:
337 ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
338 "ntdb_unset_attribute: cannot unset %s after opening",
339 type == NTDB_ATTRIBUTE_HASH
340 ? "NTDB_ATTRIBUTE_HASH"
341 : "NTDB_ATTRIBUTE_SEED");
342 break;
343 case NTDB_ATTRIBUTE_STATS:
344 ntdb_logerr(ntdb, NTDB_ERR_EINVAL,
345 NTDB_LOG_USE_ERROR,
346 "ntdb_unset_attribute:"
347 "cannot unset NTDB_ATTRIBUTE_STATS");
348 break;
349 case NTDB_ATTRIBUTE_FLOCK:
350 ntdb->lock_fn = ntdb_fcntl_lock;
351 ntdb->unlock_fn = ntdb_fcntl_unlock;
352 break;
353 default:
354 ntdb_logerr(ntdb, NTDB_ERR_EINVAL,
355 NTDB_LOG_USE_ERROR,
356 "ntdb_unset_attribute: unknown attribute type %u",
357 type);
361 /* The top three bits of the capability tell us whether it matters. */
362 enum NTDB_ERROR unknown_capability(struct ntdb_context *ntdb, const char *caller,
363 ntdb_off_t type)
365 if (type & NTDB_CAP_NOOPEN) {
366 return ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
367 "%s: file has unknown capability %llu",
368 caller, type & NTDB_CAP_NOOPEN);
371 if ((type & NTDB_CAP_NOWRITE) && !(ntdb->flags & NTDB_RDONLY)) {
372 return ntdb_logerr(ntdb, NTDB_ERR_RDONLY, NTDB_LOG_ERROR,
373 "%s: file has unknown capability %llu"
374 " (cannot write to it)",
375 caller, type & NTDB_CAP_NOOPEN);
378 if (type & NTDB_CAP_NOCHECK) {
379 ntdb->flags |= NTDB_CANT_CHECK;
381 return NTDB_SUCCESS;
384 static enum NTDB_ERROR capabilities_ok(struct ntdb_context *ntdb,
385 ntdb_off_t capabilities)
387 ntdb_off_t off, next;
388 enum NTDB_ERROR ecode = NTDB_SUCCESS;
389 const struct ntdb_capability *cap;
391 /* Check capability list. */
392 for (off = capabilities; off && ecode == NTDB_SUCCESS; off = next) {
393 cap = ntdb_access_read(ntdb, off, sizeof(*cap), true);
394 if (NTDB_PTR_IS_ERR(cap)) {
395 return NTDB_PTR_ERR(cap);
398 switch (cap->type & NTDB_CAP_TYPE_MASK) {
399 /* We don't understand any capabilities (yet). */
400 default:
401 ecode = unknown_capability(ntdb, "ntdb_open", cap->type);
403 next = cap->next;
404 ntdb_access_release(ntdb, cap);
406 return ecode;
409 _PUBLIC_ struct ntdb_context *ntdb_open(const char *name, int ntdb_flags,
410 int open_flags, mode_t mode,
411 union ntdb_attribute *attr)
413 struct ntdb_context *ntdb;
414 struct stat st;
415 int saved_errno = 0;
416 uint64_t hash_test;
417 unsigned v;
418 ssize_t rlen;
419 struct ntdb_header hdr;
420 struct ntdb_attribute_seed *seed = NULL;
421 ntdb_bool_err berr;
422 enum NTDB_ERROR ecode;
423 int openlock;
425 ntdb = malloc(sizeof(*ntdb) + strlen(name) + 1);
426 if (!ntdb) {
427 /* Can't log this */
428 errno = ENOMEM;
429 return NULL;
431 /* Set name immediately for logging functions. */
432 ntdb->name = strcpy((char *)(ntdb + 1), name);
433 ntdb->flags = ntdb_flags;
434 ntdb->log_fn = NULL;
435 ntdb->open_flags = open_flags;
436 ntdb->file = NULL;
437 ntdb->openhook = NULL;
438 ntdb->lock_fn = ntdb_fcntl_lock;
439 ntdb->unlock_fn = ntdb_fcntl_unlock;
440 ntdb->hash_fn = ntdb_jenkins_hash;
441 memset(&ntdb->stats, 0, sizeof(ntdb->stats));
442 ntdb->stats.base.attr = NTDB_ATTRIBUTE_STATS;
443 ntdb->stats.size = sizeof(ntdb->stats);
445 while (attr) {
446 switch (attr->base.attr) {
447 case NTDB_ATTRIBUTE_HASH:
448 ntdb->hash_fn = attr->hash.fn;
449 ntdb->hash_data = attr->hash.data;
450 break;
451 case NTDB_ATTRIBUTE_SEED:
452 seed = &attr->seed;
453 break;
454 case NTDB_ATTRIBUTE_OPENHOOK:
455 ntdb->openhook = attr->openhook.fn;
456 ntdb->openhook_data = attr->openhook.data;
457 break;
458 default:
459 /* These are set as normal. */
460 ecode = ntdb_set_attribute(ntdb, attr);
461 if (ecode != NTDB_SUCCESS)
462 goto fail;
464 attr = attr->base.next;
467 if (ntdb_flags & ~(NTDB_INTERNAL | NTDB_NOLOCK | NTDB_NOMMAP | NTDB_CONVERT
468 | NTDB_NOSYNC | NTDB_SEQNUM | NTDB_ALLOW_NESTING
469 | NTDB_RDONLY)) {
470 ecode = ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
471 "ntdb_open: unknown flags %u", ntdb_flags);
472 goto fail;
475 if (seed) {
476 if (!(ntdb_flags & NTDB_INTERNAL) && !(open_flags & O_CREAT)) {
477 ecode = ntdb_logerr(ntdb, NTDB_ERR_EINVAL,
478 NTDB_LOG_USE_ERROR,
479 "ntdb_open:"
480 " cannot set NTDB_ATTRIBUTE_SEED"
481 " without O_CREAT.");
482 goto fail;
486 if ((open_flags & O_ACCMODE) == O_WRONLY) {
487 ecode = ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
488 "ntdb_open: can't open ntdb %s write-only",
489 name);
490 goto fail;
493 if ((open_flags & O_ACCMODE) == O_RDONLY) {
494 openlock = F_RDLCK;
495 ntdb->flags |= NTDB_RDONLY;
496 } else {
497 if (ntdb_flags & NTDB_RDONLY) {
498 ecode = ntdb_logerr(ntdb, NTDB_ERR_EINVAL,
499 NTDB_LOG_USE_ERROR,
500 "ntdb_open: can't use NTDB_RDONLY"
501 " without O_RDONLY");
502 goto fail;
504 openlock = F_WRLCK;
507 /* internal databases don't need any of the rest. */
508 if (ntdb->flags & NTDB_INTERNAL) {
509 ntdb->flags |= (NTDB_NOLOCK | NTDB_NOMMAP);
510 ecode = ntdb_new_file(ntdb);
511 if (ecode != NTDB_SUCCESS) {
512 goto fail;
514 ntdb->file->fd = -1;
515 ecode = ntdb_new_database(ntdb, seed, &hdr);
516 if (ecode == NTDB_SUCCESS) {
517 ntdb_convert(ntdb, &hdr.hash_seed,
518 sizeof(hdr.hash_seed));
519 ntdb->hash_seed = hdr.hash_seed;
520 ntdb_context_init(ntdb);
521 ntdb_ftable_init(ntdb);
523 if (ecode != NTDB_SUCCESS) {
524 goto fail;
526 return ntdb;
529 if (stat(name, &st) != -1)
530 ntdb->file = find_file(st.st_dev, st.st_ino);
532 if (!ntdb->file) {
533 ecode = ntdb_new_file(ntdb);
534 if (ecode != NTDB_SUCCESS) {
535 goto fail;
538 /* Set this now, as ntdb_nest_lock examines it. */
539 ntdb->file->map_size = 0;
541 if ((ntdb->file->fd = open(name, open_flags, mode)) == -1) {
542 enum ntdb_log_level lvl;
543 /* errno set by open(2) */
544 saved_errno = errno;
546 /* Probing for files like this is a common pattern. */
547 if (!(open_flags & O_CREAT) && errno == ENOENT) {
548 lvl = NTDB_LOG_WARNING;
549 } else {
550 lvl = NTDB_LOG_ERROR;
552 ntdb_logerr(ntdb, NTDB_ERR_IO, lvl,
553 "ntdb_open: could not open file %s: %s",
554 name, strerror(errno));
556 goto fail_errno;
559 /* ensure there is only one process initialising at once:
560 * do it immediately to reduce the create/openlock race. */
561 ecode = ntdb_lock_open(ntdb, openlock,
562 NTDB_LOCK_WAIT|NTDB_LOCK_NOCHECK);
563 if (ecode != NTDB_SUCCESS) {
564 saved_errno = errno;
565 goto fail_errno;
568 /* on exec, don't inherit the fd */
569 v = fcntl(ntdb->file->fd, F_GETFD, 0);
570 fcntl(ntdb->file->fd, F_SETFD, v | FD_CLOEXEC);
572 if (fstat(ntdb->file->fd, &st) == -1) {
573 saved_errno = errno;
574 ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
575 "ntdb_open: could not stat open %s: %s",
576 name, strerror(errno));
577 goto fail_errno;
580 ntdb->file->device = st.st_dev;
581 ntdb->file->inode = st.st_ino;
582 } else {
583 /* ensure there is only one process initialising at once */
584 ecode = ntdb_lock_open(ntdb, openlock,
585 NTDB_LOCK_WAIT|NTDB_LOCK_NOCHECK);
586 if (ecode != NTDB_SUCCESS) {
587 saved_errno = errno;
588 goto fail_errno;
592 /* call their open hook if they gave us one. */
593 if (ntdb->openhook) {
594 ecode = ntdb->openhook(ntdb->file->fd, ntdb->openhook_data);
595 if (ecode != NTDB_SUCCESS) {
596 ntdb_logerr(ntdb, ecode, NTDB_LOG_ERROR,
597 "ntdb_open: open hook failed");
598 goto fail;
600 open_flags |= O_CREAT;
603 /* If they used O_TRUNC, read will return 0. */
604 rlen = pread(ntdb->file->fd, &hdr, sizeof(hdr), 0);
605 if (rlen == 0 && (open_flags & O_CREAT)) {
606 ecode = ntdb_new_database(ntdb, seed, &hdr);
607 if (ecode != NTDB_SUCCESS) {
608 goto fail;
610 } else if (rlen < 0) {
611 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
612 "ntdb_open: error %s reading %s",
613 strerror(errno), name);
614 goto fail;
615 } else if (rlen < sizeof(hdr)
616 || strcmp(hdr.magic_food, NTDB_MAGIC_FOOD) != 0) {
617 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
618 "ntdb_open: %s is not a ntdb file", name);
619 goto fail;
622 if (hdr.version != NTDB_VERSION) {
623 if (hdr.version == bswap_64(NTDB_VERSION))
624 ntdb->flags |= NTDB_CONVERT;
625 else {
626 /* wrong version */
627 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
628 "ntdb_open:"
629 " %s is unknown version 0x%llx",
630 name, (long long)hdr.version);
631 goto fail;
633 } else if (ntdb->flags & NTDB_CONVERT) {
634 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
635 "ntdb_open:"
636 " %s does not need NTDB_CONVERT",
637 name);
638 goto fail;
641 ntdb_context_init(ntdb);
643 ntdb_convert(ntdb, &hdr, sizeof(hdr));
644 ntdb->hash_seed = hdr.hash_seed;
645 hash_test = NTDB_HASH_MAGIC;
646 hash_test = ntdb_hash(ntdb, &hash_test, sizeof(hash_test));
647 if (hdr.hash_test != hash_test) {
648 /* wrong hash variant */
649 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
650 "ntdb_open:"
651 " %s uses a different hash function",
652 name);
653 goto fail;
656 ecode = capabilities_ok(ntdb, hdr.capabilities);
657 if (ecode != NTDB_SUCCESS) {
658 goto fail;
661 /* Clear any features we don't understand. */
662 if ((open_flags & O_ACCMODE) != O_RDONLY) {
663 hdr.features_used &= NTDB_FEATURE_MASK;
664 ecode = ntdb_write_convert(ntdb, offsetof(struct ntdb_header,
665 features_used),
666 &hdr.features_used,
667 sizeof(hdr.features_used));
668 if (ecode != NTDB_SUCCESS)
669 goto fail;
672 ntdb_unlock_open(ntdb, openlock);
674 /* This makes sure we have current map_size and mmap. */
675 ecode = ntdb->io->oob(ntdb, ntdb->file->map_size, 1, true);
676 if (unlikely(ecode != NTDB_SUCCESS))
677 goto fail;
679 if (ntdb->file->map_size % NTDB_PGSIZE != 0) {
680 ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
681 "ntdb_open:"
682 " %s size %llu isn't a multiple of %u",
683 name, (long long)ntdb->file->map_size,
684 NTDB_PGSIZE);
685 goto fail;
688 /* Now it's fully formed, recover if necessary. */
689 berr = ntdb_needs_recovery(ntdb);
690 if (unlikely(berr != false)) {
691 if (berr < 0) {
692 ecode = NTDB_OFF_TO_ERR(berr);
693 goto fail;
695 ecode = ntdb_lock_and_recover(ntdb);
696 if (ecode != NTDB_SUCCESS) {
697 goto fail;
701 ecode = ntdb_ftable_init(ntdb);
702 if (ecode != NTDB_SUCCESS) {
703 goto fail;
706 ntdb->next = tdbs;
707 tdbs = ntdb;
708 return ntdb;
710 fail:
711 /* Map ecode to some logical errno. */
712 switch (NTDB_ERR_TO_OFF(ecode)) {
713 case NTDB_ERR_TO_OFF(NTDB_ERR_CORRUPT):
714 case NTDB_ERR_TO_OFF(NTDB_ERR_IO):
715 saved_errno = EIO;
716 break;
717 case NTDB_ERR_TO_OFF(NTDB_ERR_LOCK):
718 saved_errno = EWOULDBLOCK;
719 break;
720 case NTDB_ERR_TO_OFF(NTDB_ERR_OOM):
721 saved_errno = ENOMEM;
722 break;
723 case NTDB_ERR_TO_OFF(NTDB_ERR_EINVAL):
724 saved_errno = EINVAL;
725 break;
726 default:
727 saved_errno = EINVAL;
728 break;
731 fail_errno:
732 #ifdef NTDB_TRACE
733 close(ntdb->tracefd);
734 #endif
735 if (ntdb->file) {
736 ntdb_lock_cleanup(ntdb);
737 if (--ntdb->file->refcnt == 0) {
738 assert(ntdb->file->num_lockrecs == 0);
739 if (ntdb->file->map_ptr) {
740 if (ntdb->flags & NTDB_INTERNAL) {
741 free(ntdb->file->map_ptr);
742 } else
743 ntdb_munmap(ntdb->file);
745 if (ntdb->file->fd != -1 && close(ntdb->file->fd) != 0)
746 ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
747 "ntdb_open: failed to close ntdb fd"
748 " on error: %s", strerror(errno));
749 free(ntdb->file->lockrecs);
750 free(ntdb->file);
754 free(ntdb);
755 errno = saved_errno;
756 return NULL;
759 _PUBLIC_ int ntdb_close(struct ntdb_context *ntdb)
761 int ret = 0;
762 struct ntdb_context **i;
764 ntdb_trace(ntdb, "ntdb_close");
766 if (ntdb->transaction) {
767 ntdb_transaction_cancel(ntdb);
770 if (ntdb->file->map_ptr) {
771 if (ntdb->flags & NTDB_INTERNAL)
772 free(ntdb->file->map_ptr);
773 else
774 ntdb_munmap(ntdb->file);
776 if (ntdb->file) {
777 ntdb_lock_cleanup(ntdb);
778 if (--ntdb->file->refcnt == 0) {
779 ret = close(ntdb->file->fd);
780 free(ntdb->file->lockrecs);
781 free(ntdb->file);
785 /* Remove from tdbs list */
786 for (i = &tdbs; *i; i = &(*i)->next) {
787 if (*i == ntdb) {
788 *i = ntdb->next;
789 break;
793 #ifdef NTDB_TRACE
794 close(ntdb->tracefd);
795 #endif
796 free(ntdb);
798 return ret;
801 _PUBLIC_ void ntdb_foreach_(int (*fn)(struct ntdb_context *, void *), void *p)
803 struct ntdb_context *i;
805 for (i = tdbs; i; i = i->next) {
806 if (fn(i, p) != 0)
807 break;