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)
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: