newdb.hdr.recovery = 0;
newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK;
newdb.hdr.seqnum = 0;
+ newdb.hdr.capabilities = 0;
memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
/* Initial hashes are empty. */
memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
|| hdr->version == TDB1_BYTEREV(TDB1_VERSION);
}
+/* The top three bits of the capability tell us whether it matters. */
+enum TDB_ERROR unknown_capability(struct tdb_context *tdb, const char *caller,
+ tdb_off_t type)
+{
+ if (type & TDB_CAP_NOOPEN) {
+ return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
+ "%s: file has unknown capability %llu",
+ caller, type & TDB_CAP_NOOPEN);
+ }
+
+ if ((type & TDB_CAP_NOWRITE) && !(tdb->flags & TDB_RDONLY)) {
+ return tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_ERROR,
+ "%s: file has unknown capability %llu"
+ " (cannot write to it)",
+ caller, type & TDB_CAP_NOOPEN);
+ }
+
+ if (type & TDB_CAP_NOCHECK) {
+ tdb->flags |= TDB_CANT_CHECK;
+ }
+ return TDB_SUCCESS;
+}
+
+static enum TDB_ERROR capabilities_ok(struct tdb_context *tdb,
+ tdb_off_t capabilities)
+{
+ tdb_off_t off, next;
+ enum TDB_ERROR ecode = TDB_SUCCESS;
+ const struct tdb_capability *cap;
+
+ /* Check capability list. */
+ for (off = capabilities; off && ecode == TDB_SUCCESS; off = next) {
+ cap = tdb_access_read(tdb, off, sizeof(*cap), true);
+ if (TDB_PTR_IS_ERR(cap)) {
+ return TDB_PTR_ERR(cap);
+ }
+
+ switch (cap->type & TDB_CAP_TYPE_MASK) {
+ /* We don't understand any capabilities (yet). */
+ default:
+ ecode = unknown_capability(tdb, "tdb_open", cap->type);
+ }
+ next = cap->next;
+ tdb_access_release(tdb, cap);
+ }
+ return ecode;
+}
+
struct tdb_context *tdb_open(const char *name, int tdb_flags,
int open_flags, mode_t mode,
union tdb_attribute *attr)
struct tdb_header hdr;
struct tdb_attribute_seed *seed = NULL;
struct tdb_attribute_tdb1_hashsize *hsize_attr = NULL;
+ struct tdb_attribute_tdb1_max_dead *maxsize_attr = NULL;
tdb_bool_err berr;
enum TDB_ERROR ecode;
int openlock;
case TDB_ATTRIBUTE_TDB1_HASHSIZE:
hsize_attr = &attr->tdb1_hashsize;
break;
+ case TDB_ATTRIBUTE_TDB1_MAX_DEAD:
+ maxsize_attr = &attr->tdb1_max_dead;
+ break;
default:
/* These are set as normal. */
ecode = tdb_set_attribute(tdb, attr);
}
tdb->file->fd = -1;
if (tdb->flags & TDB_VERSION1)
- ecode = tdb1_new_database(tdb, hsize_attr);
+ ecode = tdb1_new_database(tdb, hsize_attr, maxsize_attr);
else {
ecode = tdb_new_database(tdb, seed, &hdr);
if (ecode == TDB_SUCCESS) {
rlen = pread(tdb->file->fd, &hdr, sizeof(hdr), 0);
if (rlen == 0 && (open_flags & O_CREAT)) {
if (tdb->flags & TDB_VERSION1) {
- ecode = tdb1_new_database(tdb, hsize_attr);
+ ecode = tdb1_new_database(tdb, hsize_attr, maxsize_attr);
if (ecode != TDB_SUCCESS)
goto fail;
goto finished;
} else if (rlen < sizeof(hdr)
|| strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
if (is_tdb1(&tdb->tdb1.header, &hdr, rlen)) {
- ecode = tdb1_open(tdb);
+ ecode = tdb1_open(tdb, maxsize_attr);
if (!ecode)
goto finished;
goto fail;
tdb->flags |= TDB_CONVERT;
else {
if (is_tdb1(&tdb->tdb1.header, &hdr, rlen)) {
- ecode = tdb1_open(tdb);
+ ecode = tdb1_open(tdb, maxsize_attr);
if (!ecode)
goto finished;
goto fail;
goto fail;
}
+ /* This is a version2 tdb. */
if (tdb->flags & TDB_VERSION1) {
- ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb_open:"
- " %s does not need TDB_VERSION1",
- name);
- goto fail;
+ tdb->flags &= ~TDB_VERSION1;
}
tdb2_context_init(tdb);
goto fail;
}
+ ecode = capabilities_ok(tdb, hdr.capabilities);
+ if (ecode != TDB_SUCCESS) {
+ goto fail;
+ }
+
/* Clear any features we don't understand. */
if ((open_flags & O_ACCMODE) != O_RDONLY) {
hdr.features_used &= TDB_FEATURE_MASK;
berr = tdb_needs_recovery(tdb);
if (unlikely(berr != false)) {
if (berr < 0) {
- ecode = berr;
+ ecode = TDB_OFF_TO_ERR(berr);
goto fail;
}
ecode = tdb_lock_and_recover(tdb);
fail:
/* Map ecode to some logical errno. */
- switch (ecode) {
- case TDB_ERR_CORRUPT:
- case TDB_ERR_IO:
+ switch (TDB_ERR_TO_OFF(ecode)) {
+ case TDB_ERR_TO_OFF(TDB_ERR_CORRUPT):
+ case TDB_ERR_TO_OFF(TDB_ERR_IO):
saved_errno = EIO;
break;
- case TDB_ERR_LOCK:
+ case TDB_ERR_TO_OFF(TDB_ERR_LOCK):
saved_errno = EWOULDBLOCK;
break;
- case TDB_ERR_OOM:
+ case TDB_ERR_TO_OFF(TDB_ERR_OOM):
saved_errno = ENOMEM;
break;
- case TDB_ERR_EINVAL:
+ case TDB_ERR_TO_OFF(TDB_ERR_EINVAL):
saved_errno = EINVAL;
break;
default: