X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Fio.c;h=f326d98ba6d03e4c49e37409ae61280ae8a894bb;hp=999922b12e517c63be16ce1b20d7d22d077f9acc;hb=2a585ebca2a23c536520d854749fc6a813e9b12a;hpb=6dbbfabca414018b4c5acb3e6e597d84e2b19caf diff --git a/ccan/tdb2/io.c b/ccan/tdb2/io.c index 999922b1..f326d98b 100644 --- a/ccan/tdb2/io.c +++ b/ccan/tdb2/io.c @@ -1,4 +1,4 @@ - /* + /* Unix SMB/CIFS implementation. trivial database library @@ -56,7 +56,7 @@ void tdb_mmap(struct tdb_context *tdb) */ if (tdb->map_ptr == MAP_FAILED) { tdb->map_ptr = NULL; - tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_WARNING, + tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING, "tdb_mmap failed for size %lld (%s)", (long long)tdb->map_size, strerror(errno)); } @@ -64,12 +64,13 @@ 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 + if necessary note that "len" is the minimum length needed for the db */ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) { struct stat st; + enum TDB_ERROR ecode; /* We can't hold pointers during this: we could unmap! */ assert(!tdb->direct_access @@ -80,7 +81,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) return 0; if (tdb->flags & TDB_INTERNAL) { if (!probe) { - tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, "tdb_oob len %lld beyond internal" " malloc size %lld", (long long)len, @@ -89,11 +90,14 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) return -1; } - if (tdb_lock_expand(tdb, F_RDLCK) != 0) + ecode = tdb_lock_expand(tdb, F_RDLCK); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; return -1; + } if (fstat(tdb->fd, &st) != 0) { - tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, "Failed to fstat file: %s", strerror(errno)); tdb_unlock_expand(tdb, F_RDLCK); return -1; @@ -103,7 +107,7 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) if (st.st_size < (size_t)len) { if (!probe) { - tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, "tdb_oob len %zu beyond eof at %zu", (size_t)len, st.st_size); } @@ -182,7 +186,7 @@ int zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len) } while (len) { unsigned todo = len < sizeof(buf) ? len : sizeof(buf); - if (tdb->methods->write(tdb, off, buf, todo) == -1) + if (tdb->methods->twrite(tdb, off, buf, todo) == -1) return -1; len -= todo; off += todo; @@ -206,31 +210,12 @@ tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off) return ret; } -/* Even on files, we can get partial writes due to signals. */ -bool tdb_pwrite_all(int fd, const void *buf, size_t len, tdb_off_t off) -{ - while (len) { - ssize_t ret; - ret = pwrite(fd, buf, len, off); - if (ret < 0) - return false; - if (ret == 0) { - errno = ENOSPC; - return false; - } - buf = (char *)buf + ret; - off += ret; - len -= ret; - } - return true; -} - /* write a lump of data at a specified offset */ -static int tdb_write(struct tdb_context *tdb, tdb_off_t off, +static int tdb_write(struct tdb_context *tdb, tdb_off_t off, const void *buf, tdb_len_t len) { if (tdb->read_only) { - tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, + tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR, "Write to read-only database"); return -1; } @@ -246,10 +231,17 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off, if (tdb->map_ptr) { memcpy(off + (char *)tdb->map_ptr, buf, len); } else { - if (!tdb_pwrite_all(tdb->fd, buf, len, off)) { - tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, - "tdb_write failed at %zu len=%zu (%s)", - (size_t)off, (size_t)len, strerror(errno)); + ssize_t ret; + ret = pwrite(tdb->fd, buf, len, off); + if (ret < len) { + /* This shouldn't happen: we avoid sparse files. */ + if (ret >= 0) + errno = ENOSPC; + + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, + "tdb_write: %zi at %zu len=%zu (%s)", + ret, (size_t)off, (size_t)len, + strerror(errno)); return -1; } } @@ -269,7 +261,7 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf, } else { ssize_t r = pread(tdb->fd, buf, len, off); if (r != len) { - tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, "tdb_read failed with %zi at %zu " "len=%zu (%s) map_size=%zu", r, (size_t)off, (size_t)len, @@ -288,17 +280,17 @@ int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off, if (unlikely((tdb->flags & TDB_CONVERT))) { void *conv = malloc(len); if (!conv) { - tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL, + tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, "tdb_write: no memory converting" " %zu bytes", len); return -1; } memcpy(conv, rec, len); - ret = tdb->methods->write(tdb, off, - tdb_convert(tdb, conv, len), len); + ret = tdb->methods->twrite(tdb, off, + tdb_convert(tdb, conv, len), len); free(conv); } else - ret = tdb->methods->write(tdb, off, rec, len); + ret = tdb->methods->twrite(tdb, off, rec, len); return ret; } @@ -306,7 +298,7 @@ int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off, int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off, void *rec, size_t len) { - int ret = tdb->methods->read(tdb, off, rec, len); + int ret = tdb->methods->tread(tdb, off, rec, len); tdb_convert(tdb, rec, len); return ret; } @@ -314,7 +306,7 @@ int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off, int tdb_write_off(struct tdb_context *tdb, tdb_off_t off, tdb_off_t val) { if (tdb->read_only) { - tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, + tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR, "Write to read-only database"); return -1; } @@ -338,11 +330,11 @@ static void *_tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, /* some systems don't like zero length malloc */ buf = malloc(prefix + len ? prefix + len : 1); if (!buf) { - tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR, + tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_USE_ERROR, "tdb_alloc_read malloc failed len=%zu", (size_t)(prefix + len)); - } else if (unlikely(tdb->methods->read(tdb, offset, buf+prefix, - len) == -1)) { + } else if (unlikely(tdb->methods->tread(tdb, offset, buf+prefix, len) + == -1)) { free(buf); buf = NULL; } @@ -361,10 +353,15 @@ static int fill(struct tdb_context *tdb, { while (len) { size_t n = len > size ? size : len; - - if (!tdb_pwrite_all(tdb->fd, buf, n, off)) { - tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL, - "fill write failed: giving up!"); + ssize_t ret = pwrite(tdb->fd, buf, n, off); + if (ret < n) { + if (ret >= 0) + errno = ENOSPC; + + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, + "fill failed: %zi at %zu len=%zu (%s)", + ret, (size_t)off, (size_t)len, + strerror(errno)); return -1; } len -= n; @@ -380,7 +377,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition) char buf[8192]; if (tdb->read_only) { - tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, + tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR, "Expand on read-only database"); return -1; } @@ -388,7 +385,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition) if (tdb->flags & TDB_INTERNAL) { char *new = realloc(tdb->map_ptr, tdb->map_size + addition); if (!new) { - tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL, + tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, "No memory to expand database"); return -1; } @@ -407,7 +404,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition) file isn't sparse, which would be very bad if we ran out of disk. This must be done with write, not via mmap */ memset(buf, 0x43, sizeof(buf)); - if (0 || fill(tdb, buf, sizeof(buf), tdb->map_size, addition) == -1) + if (fill(tdb, buf, sizeof(buf), tdb->map_size, addition) == -1) return -1; tdb->map_size += addition; tdb_mmap(tdb); @@ -418,7 +415,7 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition) const void *tdb_access_read(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len, bool convert) { - const void *ret = NULL; + const void *ret = NULL; if (likely(!(tdb->flags & TDB_CONVERT))) ret = tdb->methods->direct(tdb, off, len, false); @@ -445,7 +442,7 @@ void *tdb_access_write(struct tdb_context *tdb, void *ret = NULL; if (tdb->read_only) { - tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING, + tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR, "Write to read-only database"); return NULL; }