+/* Returns -1 on error, 0 on OK" */
+static int replace_data(struct tdb_context *tdb,
+ uint64_t h, struct tdb_data key, struct tdb_data dbuf,
+ tdb_off_t bucket,
+ tdb_off_t old_off, tdb_len_t old_room,
+ unsigned old_zone,
+ bool growing)
+{
+ tdb_off_t new_off;
+
+ /* Allocate a new record. */
+ new_off = alloc(tdb, key.dsize, dbuf.dsize, h, growing);
+ if (unlikely(new_off == TDB_OFF_ERR))
+ return -1;
+
+ /* We didn't like the existing one: remove it. */
+ if (old_off)
+ add_free_record(tdb, old_zone, old_off,
+ sizeof(struct tdb_used_record)
+ + key.dsize + old_room);
+
+ /* FIXME: Encode extra hash bits! */
+ if (tdb_write_off(tdb, hash_off(tdb, bucket), new_off) == -1)
+ return -1;
+
+ new_off += sizeof(struct tdb_used_record);
+ if (tdb->methods->write(tdb, new_off, key.dptr, key.dsize) == -1)
+ return -1;
+
+ new_off += key.dsize;
+ if (tdb->methods->write(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
+ return -1;
+
+ /* FIXME: tdb_increment_seqnum(tdb); */
+ return 0;
+}
+
+int tdb_store(struct tdb_context *tdb,
+ struct tdb_data key, struct tdb_data dbuf, int flag)
+{
+ tdb_off_t off, bucket, start, num;
+ tdb_len_t old_room = 0;
+ struct tdb_used_record rec;
+ uint64_t h;
+ int ret;
+
+ h = tdb_hash(tdb, key.dptr, key.dsize);
+ off = find_and_lock(tdb, key, h, F_WRLCK, &start, &num, &bucket, &rec);
+ if (unlikely(off == TDB_OFF_ERR))
+ return -1;