- /* r->prev->next = r->next */
- if (tdb_write_off(tdb, off, r->next)) {
- return -1;
- }
-
- if (r->next != 0) {
- off = r->next + offsetof(struct tdb_free_record, prev);
- /* r->next->prev = r->prev */
-
-#ifdef DEBUG
- if (tdb_read_off(tdb, off) != r_off) {
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
- "remove_from_list: %llu bad list %llu\n",
- (long long)r_off, (long long)b_off);
- return -1;
+/* Remove from free bucket. */
+static enum TDB_ERROR remove_from_list(struct tdb_context *tdb,
+ tdb_off_t b_off, tdb_off_t r_off,
+ const struct tdb_free_record *r)
+{
+ tdb_off_t off, prev_next, head;
+ enum TDB_ERROR ecode;
+
+ /* Is this only element in list? Zero out bucket, and we're done. */
+ if (frec_prev(r) == r_off)
+ return tdb_write_off(tdb, b_off, 0);
+
+ /* off = &r->prev->next */
+ off = frec_prev(r) + offsetof(struct tdb_free_record, next);
+
+ /* Get prev->next */
+ prev_next = tdb_read_off(tdb, off);
+ if (TDB_OFF_IS_ERR(prev_next))
+ return prev_next;
+
+ /* If prev->next == 0, we were head: update bucket to point to next. */
+ if (prev_next == 0) {
+#ifdef CCAN_TDB2_DEBUG
+ if (tdb_read_off(tdb, b_off) != r_off) {
+ return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+ "remove_from_list:"
+ " %llu head %llu on list %llu",
+ (long long)r_off,
+ (long long)tdb_read_off(tdb, b_off),
+ (long long)b_off);