]> git.ozlabs.org Git - ccan/commitdiff
tdb2: move file operations into separate structure
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 17 Mar 2011 11:42:21 +0000 (22:12 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 17 Mar 2011 11:42:21 +0000 (22:12 +1030)
This moves the fd and locking information into a new 'struct tdb_file',
opening the way for it to be shared by multiple tdb_open calls on the
same file.

14 files changed:
ccan/tdb2/free.c
ccan/tdb2/io.c
ccan/tdb2/lock.c
ccan/tdb2/open.c
ccan/tdb2/private.h
ccan/tdb2/test/failtest_helper.h
ccan/tdb2/test/run-03-coalesce.c
ccan/tdb2/test/run-04-basichash.c
ccan/tdb2/test/run-13-delete.c
ccan/tdb2/test/run-15-append.c
ccan/tdb2/test/run-55-transaction.c
ccan/tdb2/test/run-57-die-during-transaction.c
ccan/tdb2/test/run-remap-in-read_traverse.c
ccan/tdb2/transaction.c

index e853d97eedce65dc8ae4b52313ac5a12ca2ba80d..9ea8efd1134542b1dc0a92a1eb2d0d43364210bc 100644 (file)
@@ -661,7 +661,7 @@ static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
 
        /* Need to hold a hash lock to expand DB: transactions rely on it. */
        if (!(tdb->flags & TDB_NOLOCK)
-           && !tdb->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
+           && !tdb->file->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
                                  "tdb_expand: must hold lock during expand");
        }
index 11914b3c76b639f63e89442758e259b21aeeb5fe..979ecef03865870b1a1d6bebc9b4daf717e23aa5 100644 (file)
@@ -49,7 +49,7 @@ void tdb_mmap(struct tdb_context *tdb)
                return;
 
        tdb->map_ptr = mmap(NULL, tdb->map_size, tdb->mmap_flags,
-                           MAP_SHARED, tdb->fd, 0);
+                           MAP_SHARED, tdb->file->fd, 0);
 
        /*
         * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
@@ -96,7 +96,7 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len,
                return ecode;
        }
 
-       if (fstat(tdb->fd, &st) != 0) {
+       if (fstat(tdb->file->fd, &st) != 0) {
                tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                           "Failed to fstat file: %s", strerror(errno));
                tdb_unlock_expand(tdb, F_RDLCK);
@@ -245,7 +245,7 @@ static enum TDB_ERROR tdb_write(struct tdb_context *tdb, tdb_off_t off,
                memcpy(off + (char *)tdb->map_ptr, buf, len);
        } else {
                ssize_t ret;
-               ret = pwrite(tdb->fd, buf, len, off);
+               ret = pwrite(tdb->file->fd, buf, len, off);
                if (ret != len) {
                        /* This shouldn't happen: we avoid sparse files. */
                        if (ret >= 0)
@@ -274,7 +274,7 @@ static enum TDB_ERROR tdb_read(struct tdb_context *tdb, tdb_off_t off,
        if (tdb->map_ptr) {
                memcpy(buf, off + (char *)tdb->map_ptr, len);
        } else {
-               ssize_t r = pread(tdb->fd, buf, len, off);
+               ssize_t r = pread(tdb->file->fd, buf, len, off);
                if (r != len) {
                        return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                          "tdb_read failed with %zi at %zu "
@@ -374,7 +374,7 @@ static enum TDB_ERROR fill(struct tdb_context *tdb,
 {
        while (len) {
                size_t n = len > size ? size : len;
-               ssize_t ret = pwrite(tdb->fd, buf, n, off);
+               ssize_t ret = pwrite(tdb->file->fd, buf, n, off);
                if (ret != n) {
                        if (ret >= 0)
                                errno = ENOSPC;
@@ -418,7 +418,7 @@ static enum TDB_ERROR tdb_expand_file(struct tdb_context *tdb,
                tdb_munmap(tdb);
 
                /* If this fails, we try to fill anyway. */
-               if (ftruncate(tdb->fd, tdb->map_size + addition))
+               if (ftruncate(tdb->file->fd, tdb->map_size + addition))
                        ;
 
                /* now fill the file with something. This ensures that the
index b1799a7e1b9aa0ca9ec5652f573ba74ac7019761..98570d7627e7a38272c4f72ebcf60edece5dadfd 100644 (file)
@@ -42,10 +42,10 @@ static int fcntl_lock(struct tdb_context *tdb,
 
        add_stat(tdb, lock_lowlevel, 1);
        if (waitflag)
-               return fcntl(tdb->fd, F_SETLKW, &fl);
+               return fcntl(tdb->file->fd, F_SETLKW, &fl);
        else {
                add_stat(tdb, lock_nonblock, 1);
-               return fcntl(tdb->fd, F_SETLK, &fl);
+               return fcntl(tdb->file->fd, F_SETLK, &fl);
        }
 }
 
@@ -116,7 +116,7 @@ static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
        fl.l_len = len;
        fl.l_pid = 0;
 
-       return fcntl(tdb->fd, F_SETLKW, &fl);
+       return fcntl(tdb->file->fd, F_SETLKW, &fl);
 }
 
 /* a byte range locking function - return 0 on success
@@ -161,7 +161,7 @@ static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb,
                                   "tdb_brlock failed (fd=%d) at"
                                   " offset %zu rw_type=%d flags=%d len=%zu:"
                                   " %s",
-                                  tdb->fd, (size_t)offset, rw_type,
+                                  tdb->file->fd, (size_t)offset, rw_type,
                                   flags, (size_t)len, strerror(errno));
                }
                return TDB_ERR_LOCK;
@@ -186,7 +186,7 @@ static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
                                  "tdb_brunlock failed (fd=%d) at offset %zu"
                                  " rw_type=%d len=%zu",
-                                 tdb->fd, (size_t)offset, rw_type,
+                                 tdb->file->fd, (size_t)offset, rw_type,
                                  (size_t)len);
        }
        return TDB_SUCCESS;
@@ -202,14 +202,14 @@ enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb)
 {
        int count = 1000;
 
-       if (tdb->allrecord_lock.count != 1) {
+       if (tdb->file->allrecord_lock.count != 1) {
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
                                  "tdb_allrecord_upgrade failed:"
                                  " count %u too high",
-                                 tdb->allrecord_lock.count);
+                                 tdb->file->allrecord_lock.count);
        }
 
-       if (tdb->allrecord_lock.off != 1) {
+       if (tdb->file->allrecord_lock.off != 1) {
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
                                  "tdb_allrecord_upgrade failed:"
                                  " already upgraded?");
@@ -220,8 +220,8 @@ enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb)
                if (tdb_brlock(tdb, F_WRLCK,
                               TDB_HASH_LOCK_START, 0,
                               TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) {
-                       tdb->allrecord_lock.ltype = F_WRLCK;
-                       tdb->allrecord_lock.off = 0;
+                       tdb->file->allrecord_lock.ltype = F_WRLCK;
+                       tdb->file->allrecord_lock.off = 0;
                        return TDB_SUCCESS;
                }
                if (errno != EDEADLK) {
@@ -241,9 +241,9 @@ static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
 {
        unsigned int i;
 
-       for (i=0; i<tdb->num_lockrecs; i++) {
-               if (tdb->lockrecs[i].off == offset) {
-                       return &tdb->lockrecs[i];
+       for (i=0; i<tdb->file->num_lockrecs; i++) {
+               if (tdb->file->lockrecs[i].off == offset) {
+                       return &tdb->file->lockrecs[i];
                }
        }
        return NULL;
@@ -303,7 +303,7 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
                return TDB_SUCCESS;
        }
 
-       if (tdb->num_lockrecs
+       if (tdb->file->num_lockrecs
            && offset >= TDB_HASH_LOCK_START
            && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
@@ -311,15 +311,15 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
        }
 
        new_lck = (struct tdb_lock_type *)realloc(
-               tdb->lockrecs,
-               sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
+               tdb->file->lockrecs,
+               sizeof(*tdb->file->lockrecs) * (tdb->file->num_lockrecs+1));
        if (new_lck == NULL) {
                return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
                                  "tdb_nest_lock:"
                                  " unable to allocate %zu lock struct",
-                                 tdb->num_lockrecs + 1);
+                                 tdb->file->num_lockrecs + 1);
        }
-       tdb->lockrecs = new_lck;
+       tdb->file->lockrecs = new_lck;
 
        /* Since fcntl locks don't nest, we do a lock for the first one,
           and simply bump the count for future ones */
@@ -330,7 +330,7 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
 
        /* First time we grab a lock, perhaps someone died in commit? */
        if (!(flags & TDB_LOCK_NOCHECK)
-           && tdb->num_lockrecs == 0) {
+           && tdb->file->num_lockrecs == 0) {
                tdb_bool_err berr = tdb_needs_recovery(tdb);
                if (berr != false) {
                        tdb_brunlock(tdb, ltype, offset, 1);
@@ -348,10 +348,10 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
                }
        }
 
-       tdb->lockrecs[tdb->num_lockrecs].off = offset;
-       tdb->lockrecs[tdb->num_lockrecs].count = 1;
-       tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
-       tdb->num_lockrecs++;
+       tdb->file->lockrecs[tdb->file->num_lockrecs].off = offset;
+       tdb->file->lockrecs[tdb->file->num_lockrecs].count = 1;
+       tdb->file->lockrecs[tdb->file->num_lockrecs].ltype = ltype;
+       tdb->file->num_lockrecs++;
 
        return TDB_SUCCESS;
 }
@@ -389,7 +389,7 @@ static enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
         * Shrink the array by overwriting the element just unlocked with the
         * last array element.
         */
-       *lck = tdb->lockrecs[--tdb->num_lockrecs];
+       *lck = tdb->file->lockrecs[--tdb->file->num_lockrecs];
 
        return ecode;
 }
@@ -452,17 +452,18 @@ enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
        enum TDB_ERROR ecode;
        tdb_bool_err berr;
 
-       if (tdb->allrecord_lock.count
-           && (ltype == F_RDLCK || tdb->allrecord_lock.ltype == F_WRLCK)) {
-               tdb->allrecord_lock.count++;
+       if (tdb->file->allrecord_lock.count
+           && (ltype == F_RDLCK
+               || tdb->file->allrecord_lock.ltype == F_WRLCK)) {
+               tdb->file->allrecord_lock.count++;
                return TDB_SUCCESS;
        }
 
-       if (tdb->allrecord_lock.count) {
+       if (tdb->file->allrecord_lock.count) {
                /* a global lock of a different type exists */
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
                                  "tdb_allrecord_lock: already have %s lock",
-                                 tdb->allrecord_lock.ltype == F_RDLCK
+                                 tdb->file->allrecord_lock.ltype == F_RDLCK
                                  ? "read" : "write");
        }
 
@@ -507,11 +508,11 @@ again:
                return ecode;
        }
 
-       tdb->allrecord_lock.count = 1;
+       tdb->file->allrecord_lock.count = 1;
        /* If it's upgradable, it's actually exclusive so we can treat
         * it as a write lock. */
-       tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
-       tdb->allrecord_lock.off = upgradable;
+       tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
+       tdb->file->allrecord_lock.off = upgradable;
 
        /* Now check for needing recovery. */
        if (flags & TDB_LOCK_NOCHECK)
@@ -543,7 +544,8 @@ void tdb_unlock_open(struct tdb_context *tdb)
 
 bool tdb_has_open_lock(struct tdb_context *tdb)
 {
-       return find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
+       return !(tdb->flags & TDB_NOLOCK)
+               && find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
 }
 
 enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype)
@@ -561,15 +563,15 @@ void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
 /* unlock entire db */
 void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
 {
-       if (tdb->allrecord_lock.count == 0) {
+       if (tdb->file->allrecord_lock.count == 0) {
                tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
                           "tdb_allrecord_unlock: not locked!");
                return;
        }
 
        /* Upgradable locks are marked as write locks. */
-       if (tdb->allrecord_lock.ltype != ltype
-           && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
+       if (tdb->file->allrecord_lock.ltype != ltype
+           && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
                tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
                           "tdb_allrecord_unlock: have %s lock",
                           tdb->allrecord_lock.ltype == F_RDLCK
@@ -577,13 +579,13 @@ void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
                return;
        }
 
-       if (tdb->allrecord_lock.count > 1) {
-               tdb->allrecord_lock.count--;
+       if (tdb->file->allrecord_lock.count > 1) {
+               tdb->file->allrecord_lock.count--;
                return;
        }
 
-       tdb->allrecord_lock.count = 0;
-       tdb->allrecord_lock.ltype = 0;
+       tdb->file->allrecord_lock.count = 0;
+       tdb->file->allrecord_lock.ltype = 0;
 
        tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
 }
@@ -597,10 +599,10 @@ bool tdb_has_hash_locks(struct tdb_context *tdb)
 {
        unsigned int i;
 
-       for (i=0; i<tdb->num_lockrecs; i++) {
-               if (tdb->lockrecs[i].off >= TDB_HASH_LOCK_START
-                   && tdb->lockrecs[i].off < (TDB_HASH_LOCK_START
-                                              + TDB_HASH_LOCK_RANGE))
+       for (i=0; i<tdb->file->num_lockrecs; i++) {
+               if (tdb->file->lockrecs[i].off >= TDB_HASH_LOCK_START
+                   && tdb->file->lockrecs[i].off < (TDB_HASH_LOCK_START
+                                                    + TDB_HASH_LOCK_RANGE))
                        return true;
        }
        return false;
@@ -610,8 +612,11 @@ static bool tdb_has_free_lock(struct tdb_context *tdb)
 {
        unsigned int i;
 
-       for (i=0; i<tdb->num_lockrecs; i++) {
-               if (tdb->lockrecs[i].off
+       if (tdb->flags & TDB_NOLOCK)
+               return false;
+
+       for (i=0; i<tdb->file->num_lockrecs; i++) {
+               if (tdb->file->lockrecs[i].off
                    > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
                        return true;
        }
@@ -628,16 +633,16 @@ enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
                + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
 
        /* a allrecord lock allows us to avoid per chain locks */
-       if (tdb->allrecord_lock.count &&
-           (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
+       if (tdb->file->allrecord_lock.count &&
+           (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
                return TDB_SUCCESS;
        }
 
-       if (tdb->allrecord_lock.count) {
+       if (tdb->file->allrecord_lock.count) {
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
                                  "tdb_lock_hashes:"
                                  " already have %s allrecordlock",
-                                 tdb->allrecord_lock.ltype == F_RDLCK
+                                 tdb->file->allrecord_lock.ltype == F_RDLCK
                                  ? "read" : "write");
        }
 
@@ -662,9 +667,12 @@ enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
        unsigned lock = TDB_HASH_LOCK_START
                + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
 
+       if (tdb->flags & TDB_NOLOCK)
+               return 0;
+
        /* a allrecord lock allows us to avoid per chain locks */
-       if (tdb->allrecord_lock.count) {
-               if (tdb->allrecord_lock.ltype == F_RDLCK
+       if (tdb->file->allrecord_lock.count) {
+               if (tdb->file->allrecord_lock.ltype == F_RDLCK
                    && ltype == F_WRLCK) {
                        return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
                                          "tdb_unlock_hashes RO allrecord!");
@@ -691,9 +699,12 @@ enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
 {
        assert(b_off >= sizeof(struct tdb_header));
 
+       if (tdb->flags & TDB_NOLOCK)
+               return 0;
+
        /* a allrecord lock allows us to avoid per chain locks */
-       if (tdb->allrecord_lock.count) {
-               if (tdb->allrecord_lock.ltype == F_WRLCK)
+       if (tdb->file->allrecord_lock.count) {
+               if (tdb->file->allrecord_lock.ltype == F_WRLCK)
                        return 0;
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
                                  "tdb_lock_free_bucket with"
@@ -713,15 +724,8 @@ enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
 
 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
 {
-       if (tdb->allrecord_lock.count)
+       if (tdb->file->allrecord_lock.count)
                return;
 
        tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
 }
-
-void tdb_lock_init(struct tdb_context *tdb)
-{
-       tdb->num_lockrecs = 0;
-       tdb->lockrecs = NULL;
-       tdb->allrecord_lock.count = 0;
-}
index 21881c350e084dcc5751c395a773ec974662244b..9ad9e3ba9f878f8dd876623a29cf3424135b3eb6 100644 (file)
@@ -1,19 +1,18 @@
 #include "private.h"
 
-/* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
-static struct tdb_context *tdbs = NULL;
+/* all lock info, to detect double-opens (fcntl file don't nest!) */
+static struct tdb_file *files = NULL;
 
-static bool tdb_already_open(dev_t device, ino_t ino)
+static struct tdb_file *find_file(dev_t device, ino_t ino)
 {
-       struct tdb_context *i;
+       struct tdb_file *i;
 
-       for (i = tdbs; i; i = i->next) {
+       for (i = files; i; i = i->next) {
                if (i->device == device && i->inode == ino) {
-                       return true;
+                       break;
                }
        }
-
-       return false;
+       return i;
 }
 
 static bool read_all(int fd, void *buf, size_t len)
@@ -143,19 +142,19 @@ static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
                memcpy(tdb->map_ptr, &newdb, tdb->map_size);
                return TDB_SUCCESS;
        }
-       if (lseek(tdb->fd, 0, SEEK_SET) == -1) {
+       if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) {
                return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                  "tdb_new_database:"
                                  " failed to seek: %s", strerror(errno));
        }
 
-       if (ftruncate(tdb->fd, 0) == -1) {
+       if (ftruncate(tdb->file->fd, 0) == -1) {
                return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                  "tdb_new_database:"
                                  " failed to truncate: %s", strerror(errno));
        }
 
-       rlen = write(tdb->fd, &newdb, sizeof(newdb));
+       rlen = write(tdb->file->fd, &newdb, sizeof(newdb));
        if (rlen != sizeof(newdb)) {
                if (rlen >= 0)
                        errno = ENOSPC;
@@ -190,16 +189,15 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        tdb->name = NULL;
        tdb->map_ptr = NULL;
        tdb->direct_access = 0;
-       tdb->fd = -1;
        tdb->map_size = sizeof(struct tdb_header);
        tdb->flags = tdb_flags;
        tdb->logfn = NULL;
        tdb->transaction = NULL;
        tdb->stats = NULL;
        tdb->access = NULL;
+       tdb->file = NULL;
        tdb_hash_init(tdb);
        tdb_io_init(tdb);
-       tdb_lock_init(tdb);
 
        while (attr) {
                switch (attr->base.attr) {
@@ -266,18 +264,56 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                return tdb;
        }
 
-       if ((tdb->fd = open(name, open_flags, mode)) == -1) {
-               /* errno set by open(2) */
-               saved_errno = errno;
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
+       if (stat(name, &st) != -1)
+               tdb->file = find_file(st.st_dev, st.st_ino);
+
+       if (!tdb->file) {
+               int fd;
+
+               if ((fd = open(name, open_flags, mode)) == -1) {
+                       /* errno set by open(2) */
+                       saved_errno = errno;
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                   "tdb_open: could not open file %s: %s",
                                   name, strerror(errno));
-               goto fail;
-       }
+                       goto fail;
+               }
+
+               /* on exec, don't inherit the fd */
+               v = fcntl(fd, F_GETFD, 0);
+               fcntl(fd, F_SETFD, v | FD_CLOEXEC);
+
+               if (fstat(fd, &st) == -1) {
+                       saved_errno = errno;
+                       tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
+                                  "tdb_open: could not stat open %s: %s",
+                                  name, strerror(errno));
+                       goto fail;
+               }
+
+               tdb->file = malloc(sizeof(*tdb->file));
+               if (!tdb->file) {
+                       saved_errno = ENOMEM;
+                       tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
+                                  "tdb_open: could alloc file");
+                       goto fail;
+               }
 
-       /* on exec, don't inherit the fd */
-       v = fcntl(tdb->fd, F_GETFD, 0);
-        fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
+               tdb->file->next = files;
+               tdb->file->num_lockrecs = 0;
+               tdb->file->lockrecs = NULL;
+               tdb->file->allrecord_lock.count = 0;
+               tdb->file->fd = fd;
+               tdb->file->device = st.st_dev;
+               tdb->file->inode = st.st_ino;
+       } else {
+               /* FIXME */
+               ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
+                                  "tdb_open: %s (%d,%d) is already open in"
+                                  " this process",
+                                  name, (int)st.st_dev, (int)st.st_ino);
+               goto fail;
+       }
 
        /* ensure there is only one process initialising at once */
        ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
@@ -286,7 +322,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
        }
 
        /* If they used O_TRUNC, read will return 0. */
-       rlen = read(tdb->fd, &hdr, sizeof(hdr));
+       rlen = read(tdb->file->fd, &hdr, sizeof(hdr));
        if (rlen == 0 && (open_flags & O_CREAT)) {
                ecode = tdb_new_database(tdb, seed, &hdr);
                if (ecode != TDB_SUCCESS) {
@@ -330,24 +366,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                goto fail;
        }
 
-       if (fstat(tdb->fd, &st) == -1) {
-               saved_errno = errno;
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                  "tdb_open: could not stat open %s: %s",
-                                  name, strerror(errno));
-               goto fail;
-       }
-
-       /* Is it already in the open list?  If so, fail. */
-       if (tdb_already_open(st.st_dev, st.st_ino)) {
-               /* FIXME */
-               ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
-                                  "tdb_open: %s (%d,%d) is already open"
-                                  " in this process",
-                                  name, (int)st.st_dev, (int)st.st_ino);
-               goto fail;
-       }
-
        tdb->name = strdup(name);
        if (!tdb->name) {
                ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
@@ -365,8 +383,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                        goto fail;
        }
 
-       tdb->device = st.st_dev;
-       tdb->inode = st.st_ino;
        tdb_unlock_open(tdb);
 
        /* This make sure we have current map_size and mmap. */
@@ -390,8 +406,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                goto fail;
        }
 
-       tdb->next = tdbs;
-       tdbs = tdb;
+       /* Add to linked list. */
+       files = tdb->file;
        return tdb;
 
  fail:
@@ -426,13 +442,16 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
                } else
                        tdb_munmap(tdb);
        }
-       free(tdb->lockrecs);
        free((char *)tdb->name);
-       if (tdb->fd != -1)
-               if (close(tdb->fd) != 0)
+       if (tdb->file) {
+               if (close(tdb->file->fd) != 0)
                        tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
-                                  "tdb_open: failed to close tdb->fd"
+                                  "tdb_open: failed to close tdb fd"
                                   " on error: %s", strerror(errno));
+               free(tdb->file->lockrecs);
+               free(tdb->file);
+       }
+
        free(tdb);
        errno = saved_errno;
        return NULL;
@@ -440,7 +459,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
 
 int tdb_close(struct tdb_context *tdb)
 {
-       struct tdb_context **i;
        int ret = 0;
 
        tdb_trace(tdb, "tdb_close");
@@ -456,18 +474,19 @@ int tdb_close(struct tdb_context *tdb)
                        tdb_munmap(tdb);
        }
        free((char *)tdb->name);
-       if (tdb->fd != -1) {
-               ret = close(tdb->fd);
-               tdb->fd = -1;
-       }
-       free(tdb->lockrecs);
-
-       /* Remove from contexts list */
-       for (i = &tdbs; *i; i = &(*i)->next) {
-               if (*i == tdb) {
-                       *i = tdb->next;
-                       break;
+       if (tdb->file) {
+               struct tdb_file **i;
+               ret = close(tdb->file->fd);
+
+               /* Remove from files list */
+               for (i = &files; *i; i = &(*i)->next) {
+                       if (*i == tdb->file) {
+                               *i = tdb->file->next;
+                               break;
+                       }
                }
+               free(tdb->file->lockrecs);
+               free(tdb->file);
        }
 
 #ifdef TDB_TRACE
index 8e7df501b716a139c77e8de2ff673fe1afa55266..9ae5bed2e6bc6cb8c397d32d1f7f6f6ef8574930 100644 (file)
@@ -315,6 +315,23 @@ struct tdb_access_hdr {
        bool convert;
 };
 
+struct tdb_file {
+       /* Single list of all TDBs, to detect multiple opens. */
+       struct tdb_file *next;
+
+       /* The file descriptor. */
+       int fd;
+
+       /* Lock information */
+       struct tdb_lock_type allrecord_lock;
+       size_t num_lockrecs;
+       struct tdb_lock_type *lockrecs;
+
+       /* Identity of this file. */
+       dev_t device;
+       ino_t inode;
+};
+
 struct tdb_context {
        /* Filename of the database. */
        const char *name;
@@ -325,9 +342,6 @@ struct tdb_context {
        /* Are we accessing directly? (debugging check). */
        int direct_access;
 
-        /* Open file descriptor (undefined for TDB_INTERNAL). */
-       int fd;
-
        /* How much space has been mapped (<= current file size) */
        tdb_len_t map_size;
 
@@ -365,20 +379,13 @@ struct tdb_context {
        /* IO methods: changes for transactions. */
        const struct tdb_methods *methods;
 
-       /* Lock information */
-       struct tdb_lock_type allrecord_lock;
-       size_t num_lockrecs;
-       struct tdb_lock_type *lockrecs;
-
        struct tdb_attribute_stats *stats;
 
        /* Direct access information */
        struct tdb_access_hdr *access;
 
-       /* Single list of all TDBs, to avoid multiple opens. */
-       struct tdb_context *next;
-       dev_t device;
-       ino_t inode;
+       /* The actual file information */
+       struct tdb_file *file;
 };
 
 struct tdb_methods {
@@ -521,8 +528,6 @@ void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val);
        } while (0)
 
 /* lock.c: */
-void tdb_lock_init(struct tdb_context *tdb);
-
 /* Lock/unlock a range of hashes. */
 enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
                               tdb_off_t hash_lock, tdb_len_t hash_range,
index 56cb267aec1caa25efb4883d8cbbdf57ccc12c45..64629a697cb525ef697827d3427cca3027fc60c9 100644 (file)
@@ -4,7 +4,7 @@
 #include <stdbool.h>
 
 /* FIXME: Check these! */
-#define INITIAL_TDB_MALLOC     "open.c", 184, FAILTEST_MALLOC
+#define INITIAL_TDB_MALLOC     "open.c", 183, FAILTEST_MALLOC
 #define URANDOM_OPEN           "open.c", 43, FAILTEST_OPEN
 #define URANDOM_READ           "open.c", 23, FAILTEST_READ
 
index d40096515a1ba5c245c2af1f452fb926b89cb471..c3583d850c369b74af84817d84f623729a851386 100644 (file)
@@ -94,7 +94,7 @@ int main(int argc, char *argv[])
        /* Lock and coalesce. */
        ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
        ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
-       ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+       ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
        ok1(free_record_length(tdb, layout->elem[1].base.off)
            == 1024 + sizeof(struct tdb_used_record) + 2048);
        ok1(tdb_check(tdb, NULL, NULL) == 0);
@@ -117,7 +117,7 @@ int main(int argc, char *argv[])
        /* Lock and coalesce. */
        ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
        ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
-       ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+       ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
        ok1(free_record_length(tdb, layout->elem[1].base.off)
            == 1024 + sizeof(struct tdb_used_record) + 512);
        ok1(tdb_check(tdb, NULL, NULL) == 0);
@@ -141,7 +141,8 @@ int main(int argc, char *argv[])
        /* Lock and coalesce. */
        ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
        ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
-       ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+       ok1(tdb->file->allrecord_lock.count == 0
+           && tdb->file->num_lockrecs == 0);
        ok1(free_record_length(tdb, layout->elem[1].base.off)
            == 1024 + sizeof(struct tdb_used_record) + 512
            + sizeof(struct tdb_used_record) + 256);
index 8a7566372589fdaece7686045211f5dba3aac2e8..b3a5d1c5bd0b8d2b1d1ffe6d03cee05e429ec110 100644 (file)
@@ -60,9 +60,9 @@ int main(int argc, char *argv[])
                ok1(h.hlock_start == 0);
                ok1(h.hlock_range == 
                    1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
-               ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+               ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
                ok1((tdb->flags & TDB_NOLOCK)
-                   || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+                   || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
                /* FIXME: Check lock length */
 
                /* Allocate a new record. */
@@ -101,9 +101,9 @@ int main(int argc, char *argv[])
                ok1(h.hlock_start == 0);
                ok1(h.hlock_range == 
                    1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
-               ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+               ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
                ok1((tdb->flags & TDB_NOLOCK)
-                   || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+                   || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
                /* FIXME: Check lock length */
 
                ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
@@ -127,9 +127,9 @@ int main(int argc, char *argv[])
                ok1(h.hlock_start == 0);
                ok1(h.hlock_range == 
                    1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
-               ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+               ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
                ok1((tdb->flags & TDB_NOLOCK)
-                   || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+                   || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
                /* FIXME: Check lock length */
 
                /* Make it expand 0'th bucket. */
@@ -165,9 +165,9 @@ int main(int argc, char *argv[])
                ok1(h.hlock_start == 0);
                ok1(h.hlock_range == 
                    1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
-               ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+               ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
                ok1((tdb->flags & TDB_NOLOCK)
-                   || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+                   || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
                /* FIXME: Check lock length */
 
                /* Simple delete should work. */
@@ -196,9 +196,9 @@ int main(int argc, char *argv[])
                ok1(h.hlock_start == 0);
                ok1(h.hlock_range == 
                    1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
-               ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+               ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
                ok1((tdb->flags & TDB_NOLOCK)
-                   || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+                   || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
                /* FIXME: Check lock length */
 
                ok1(expand_group(tdb, &h) == 0);
index 8322ff50c863702d1f2de552a9a6affdc5f9bb07..3eaeee7a29b1ff8e3f707da522d4c6ed7ca29319 100644 (file)
@@ -180,7 +180,8 @@ int main(int argc, char *argv[])
                /* Check mixed bitpattern. */
                test_val(tdb, 0x123456789ABCDEF0ULL);
 
-               ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+               ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+                                  && tdb->file->num_lockrecs == 0));
                tdb_close(tdb);
 
                /* Deleting these entries in the db gave problems. */
index e1b85b4880fc0bf45427e0368eeab18c52540e94..e11289e99b8493723f0c4a641b34bd15a2fdd829 100644 (file)
@@ -69,7 +69,8 @@ int main(int argc, char *argv[])
                                moves++;
                        oldoff = newoff;
                }
-               ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+               ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+                                  && tdb->file->num_lockrecs == 0));
                /* We should increase by 50% each time... */
                ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
                tdb_close(tdb);
@@ -100,7 +101,8 @@ int main(int argc, char *argv[])
                                moves++;
                        oldoff = newoff;
                }
-               ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+               ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+                                  && tdb->file->num_lockrecs == 0));
                /* We should increase by 50% each time... */
                ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
                tdb_close(tdb);
@@ -122,7 +124,8 @@ int main(int argc, char *argv[])
                ok1(data.dsize == MAX_SIZE);
                ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
                free(data.dptr);
-               ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+               ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+                                  && tdb->file->num_lockrecs == 0));
                tdb_close(tdb);
        }
 
index d1224a7fdad6b61567d15edf60055773d9ac9c62..d7fd951e57046aa5a17fdbc466bebe70c49d0865 100644 (file)
@@ -43,7 +43,8 @@ int main(int argc, char *argv[])
 
                /* Cancelling a transaction means no store */
                tdb_transaction_cancel(tdb);
-               ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+               ok1(tdb->file->allrecord_lock.count == 0
+                   && tdb->file->num_lockrecs == 0);
                ok1(tdb_check(tdb, NULL, NULL) == 0);
                ok1(tdb_fetch(tdb, key, &data) == TDB_ERR_NOEXIST);
 
@@ -57,7 +58,8 @@ int main(int argc, char *argv[])
                ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
                free(data.dptr);
                ok1(tdb_transaction_commit(tdb) == 0);
-               ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+               ok1(tdb->file->allrecord_lock.count == 0
+                   && tdb->file->num_lockrecs == 0);
                ok1(tdb_check(tdb, NULL, NULL) == 0);
                ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
                ok1(data.dsize == 1000);
index 59b4d62c1c002403a3086a6c959a486fd4ea37a8..54dcdca66b5e771899acb1ae33a6f403e543afe8 100644 (file)
@@ -153,7 +153,7 @@ reset:
 
        if (setjmp(jmpbuf) != 0) {
                /* We're partway through.  Simulate our death. */
-               close(tdb->fd);
+               close(tdb->file->fd);
                forget_locking();
                in_transaction = false;
 
index 6285bf47e1b804e9428bdee971e6cec17f6c1dc2..e89d7df7b21f8df47b800cecc4d243c4f7342d83 100644 (file)
@@ -51,7 +51,7 @@ int main(int argc, char *argv[])
                       O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
 
        ok1(external_agent_operation(agent, OPEN, filename) == SUCCESS);
-       i = add_records_to_grow(agent, tdb->fd, tdb->map_size);
+       i = add_records_to_grow(agent, tdb->file->fd, tdb->map_size);
 
        /* Do a traverse. */
        ok1(tdb_traverse(tdb, NULL, NULL) == i);
index 2cab4655661768a80e44593211f152dd3c73175c..3c17bc7dd7cf808c4687880ceaea2feadeedda7f 100644 (file)
@@ -430,7 +430,7 @@ static enum TDB_ERROR transaction_sync(struct tdb_context *tdb,
                return TDB_SUCCESS;
        }
 
-       if (fsync(tdb->fd) != 0) {
+       if (fsync(tdb->file->fd) != 0) {
                return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                  "tdb_transaction: fsync failed: %s",
                                  strerror(errno));
@@ -495,8 +495,8 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
                }
        }
 
-       if (tdb->allrecord_lock.count)
-               tdb_allrecord_unlock(tdb, tdb->allrecord_lock.ltype);
+       if (tdb->file->allrecord_lock.count)
+               tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);
 
        /* restore the normal io methods */
        tdb->methods = tdb->transaction->io_methods;