]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/io.c
tdb2: remove tailer
[ccan] / ccan / tdb2 / io.c
index 9f0582e0df338998302422aeba94396ea72b9a15..676f4942bf604c879b767e8a296eca0f319d2dae 100644 (file)
@@ -48,8 +48,7 @@ void tdb_mmap(struct tdb_context *tdb)
        if (tdb->flags & TDB_NOMMAP)
                return;
 
-       tdb->map_ptr = mmap(NULL, tdb->map_size, 
-                           PROT_READ|(tdb->read_only? 0:PROT_WRITE), 
+       tdb->map_ptr = mmap(NULL, tdb->map_size, tdb->mmap_flags,
                            MAP_SHARED, tdb->fd, 0);
 
        /*
@@ -74,7 +73,9 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
        int ret;
 
        /* We can't hold pointers during this: we could unmap! */
-       assert(!tdb->direct_access || tdb_has_expansion_lock(tdb));
+       assert(!tdb->direct_access
+              || (tdb->flags & TDB_NOLOCK)
+              || tdb_has_expansion_lock(tdb));
 
        if (len <= tdb->map_size)
                return 0;
@@ -122,27 +123,13 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
        return 0;
 }
 
-static void *tdb_direct(struct tdb_context *tdb, tdb_off_t off, size_t len)
-{
-       if (unlikely(!tdb->map_ptr))
-               return NULL;
-
-       /* FIXME: We can do a subset of this! */
-       if (tdb->transaction)
-               return NULL;
-
-       if (unlikely(tdb_oob(tdb, off + len, true) == -1))
-               return NULL;
-       return (char *)tdb->map_ptr + off;
-}
-
 /* Either make a copy into pad and return that, or return ptr into mmap. */
 /* Note: pad has to be a real object, so we can't get here if len
  * overflows size_t */
 void *tdb_get(struct tdb_context *tdb, tdb_off_t off, void *pad, size_t len)
 {
        if (likely(!(tdb->flags & TDB_CONVERT))) {
-               void *ret = tdb_direct(tdb, off, len);
+               void *ret = tdb->methods->direct(tdb, off, len);
                if (ret)
                        return ret;
        }
@@ -204,7 +191,7 @@ uint64_t tdb_find_zero_off(struct tdb_context *tdb, tdb_off_t off,
 int zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len)
 {
        char buf[8192] = { 0 };
-       void *p = tdb_direct(tdb, off, len);
+       void *p = tdb->methods->direct(tdb, off, len);
        if (p) {
                memset(p, 0, len);
                return 0;
@@ -310,7 +297,8 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
                        tdb->ecode = TDB_ERR_IO;
                        tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
                                 "tdb_write failed at %llu len=%llu (%s)\n",
-                                off, len, strerror(errno));
+                                (long long)off, (long long)len,
+                                strerror(errno));
                        return -1;
                }
        }
@@ -476,7 +464,7 @@ const void *tdb_access_read(struct tdb_context *tdb,
        const void *ret = NULL; 
 
        if (likely(!(tdb->flags & TDB_CONVERT)))
-               ret = tdb_direct(tdb, off, len);
+               ret = tdb->methods->direct(tdb, off, len);
 
        if (!ret) {
                struct tdb_access_hdr *hdr;
@@ -498,7 +486,7 @@ void *tdb_access_write(struct tdb_context *tdb,
        void *ret = NULL;
 
        if (likely(!(tdb->flags & TDB_CONVERT)))
-               ret = tdb_direct(tdb, off, len);
+               ret = tdb->methods->direct(tdb, off, len);
 
        if (!ret) {
                struct tdb_access_hdr *hdr;
@@ -656,11 +644,22 @@ int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct tdb_record *
 }
 #endif
 
+static void *tdb_direct(struct tdb_context *tdb, tdb_off_t off, size_t len)
+{
+       if (unlikely(!tdb->map_ptr))
+               return NULL;
+
+       if (unlikely(tdb_oob(tdb, off + len, true) == -1))
+               return NULL;
+       return (char *)tdb->map_ptr + off;
+}
+
 static const struct tdb_methods io_methods = {
        tdb_read,
        tdb_write,
        tdb_oob,
        tdb_expand_file,
+       tdb_direct,
 };
 
 /*