X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Fio.c;h=4166cd4c67caf0475684fc03a0e9de86e714500e;hb=77658070a3e4f712b94d659b2e399031ce3394c8;hp=4ed37ca2cffbeaac1c6398ac5dbf20926e6d0d83;hpb=45e9956d665dc8819eb183ae239581410dcecdb3;p=ccan diff --git a/ccan/tdb2/io.c b/ccan/tdb2/io.c index 4ed37ca2..4166cd4c 100644 --- a/ccan/tdb2/io.c +++ b/ccan/tdb2/io.c @@ -48,8 +48,13 @@ void tdb_mmap(struct tdb_context *tdb) if (tdb->flags & TDB_NOMMAP) return; - tdb->file->map_ptr = mmap(NULL, tdb->file->map_size, tdb->mmap_flags, - MAP_SHARED, tdb->file->fd, 0); + /* size_t can be smaller than off_t. */ + if ((size_t)tdb->file->map_size == tdb->file->map_size) { + tdb->file->map_ptr = mmap(NULL, tdb->file->map_size, + tdb->mmap_flags, + MAP_SHARED, tdb->file->fd, 0); + } else + tdb->file->map_ptr = MAP_FAILED; /* * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!! @@ -65,7 +70,9 @@ void tdb_mmap(struct tdb_context *tdb) /* check for an out of bounds access - if it is out of bounds then see if the database has been expanded by someone else and expand if necessary - note that "len" is the minimum length needed for the db + note that "len" is the minimum length needed for the db. + + If probe is true, len being too large isn't a failure. */ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) @@ -79,15 +86,16 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len, || tdb_has_expansion_lock(tdb)); if (len <= tdb->file->map_size) - return 0; + return TDB_SUCCESS; if (tdb->flags & TDB_INTERNAL) { - if (!probe) { - tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, - "tdb_oob len %lld beyond internal" - " malloc size %lld", - (long long)len, - (long long)tdb->file->map_size); - } + if (probe) + return TDB_SUCCESS; + + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, + "tdb_oob len %lld beyond internal" + " malloc size %lld", + (long long)len, + (long long)tdb->file->map_size); return TDB_ERR_IO; } @@ -106,11 +114,12 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len, tdb_unlock_expand(tdb, F_RDLCK); if (st.st_size < (size_t)len) { - if (!probe) { - tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, - "tdb_oob len %zu beyond eof at %zu", - (size_t)len, st.st_size); - } + if (probe) + return TDB_SUCCESS; + + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, + "tdb_oob len %zu beyond eof at %zu", + (size_t)len, st.st_size); return TDB_ERR_IO; } @@ -125,6 +134,7 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len, /* Endian conversion: we only ever deal with 8 byte quantities */ void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size) { + assert(size % 8 == 0); if (unlikely((tdb->flags & TDB_CONVERT)) && buf) { uint64_t i, *p = (uint64_t *)buf; for (i = 0; i < size / 8; i++) @@ -236,7 +246,7 @@ static enum TDB_ERROR tdb_write(struct tdb_context *tdb, tdb_off_t off, "Write to read-only database"); } - ecode = tdb->methods->oob(tdb, off + len, 0); + ecode = tdb->methods->oob(tdb, off + len, false); if (ecode != TDB_SUCCESS) { return ecode; } @@ -266,7 +276,7 @@ static enum TDB_ERROR tdb_read(struct tdb_context *tdb, tdb_off_t off, { enum TDB_ERROR ecode; - ecode = tdb->methods->oob(tdb, off + len, 0); + ecode = tdb->methods->oob(tdb, off + len, false); if (ecode != TDB_SUCCESS) { return ecode; } @@ -342,7 +352,7 @@ enum TDB_ERROR tdb_write_off(struct tdb_context *tdb, static void *_tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len, unsigned int prefix) { - void *buf; + unsigned char *buf; enum TDB_ERROR ecode; /* some systems don't like zero length malloc */ @@ -557,7 +567,7 @@ static void *tdb_direct(struct tdb_context *tdb, tdb_off_t off, size_t len, if (unlikely(!tdb->file->map_ptr)) return NULL; - ecode = tdb_oob(tdb, off + len, true); + ecode = tdb_oob(tdb, off + len, false); if (unlikely(ecode != TDB_SUCCESS)) return TDB_ERR_PTR(ecode); return (char *)tdb->file->map_ptr + off; @@ -592,12 +602,6 @@ void tdb_inc_seqnum(struct tdb_context *tdb) } } -void add_stat_(struct tdb_context *tdb, uint64_t *s, size_t val) -{ - if ((uintptr_t)s < (uintptr_t)tdb->stats + tdb->stats->size) - *s += val; -} - static const struct tdb_methods io_methods = { tdb_read, tdb_write,