X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Ffree.c;h=897d3cbe4627eaf69c9bf985a53608741b045fe5;hb=323a9473c0aef80b8e18ef0c53529c8dfc29ec45;hp=bface0a319fb94504059160ed9247db493e16e9f;hpb=850c5cfed46cd4f38df79783791969c7b30ad9da;p=ccan diff --git a/ccan/tdb2/free.c b/ccan/tdb2/free.c index bface0a3..897d3cbe 100644 --- a/ccan/tdb2/free.c +++ b/ccan/tdb2/free.c @@ -69,8 +69,10 @@ int tdb_ftable_init(struct tdb_context *tdb) tdb->ftable = 0; while (off) { - if (off == TDB_OFF_ERR) + if (TDB_OFF_IS_ERR(off)) { + tdb->ecode = off; return -1; + } rnd = random(); if (rnd >= max) { @@ -92,7 +94,7 @@ tdb_off_t bucket_off(tdb_off_t ftable_off, unsigned bucket) + bucket * sizeof(tdb_off_t); } -/* Returns free_buckets + 1, or list number to search. */ +/* Returns free_buckets + 1, or list number to search, or -ve error. */ static tdb_off_t find_free_head(struct tdb_context *tdb, tdb_off_t ftable_off, tdb_off_t bucket) @@ -108,6 +110,7 @@ static int remove_from_list(struct tdb_context *tdb, const struct tdb_free_record *r) { tdb_off_t off; + enum TDB_ERROR ecode; /* Front of list? */ if (frec_prev(r) == 0) { @@ -126,7 +129,9 @@ static int remove_from_list(struct tdb_context *tdb, #endif /* r->prev->next = r->next */ - if (tdb_write_off(tdb, off, r->next)) { + ecode = tdb_write_off(tdb, off, r->next); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; return -1; } @@ -143,7 +148,9 @@ static int remove_from_list(struct tdb_context *tdb, } #endif - if (tdb_write_off(tdb, off, r->magic_and_prev)) { + ecode = tdb_write_off(tdb, off, r->magic_and_prev); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; return -1; } } @@ -157,6 +164,7 @@ static int enqueue_in_free(struct tdb_context *tdb, tdb_len_t len) { struct tdb_free_record new; + enum TDB_ERROR ecode; uint64_t magic = (TDB_FREE_MAGIC << (64 - TDB_OFF_UPPER_STEAL)); /* We only need to set ftable_and_len; rest is set in enqueue_in_free */ @@ -167,8 +175,10 @@ static int enqueue_in_free(struct tdb_context *tdb, /* new->next = head. */ new.next = tdb_read_off(tdb, b_off); - if (new.next == TDB_OFF_ERR) + if (TDB_OFF_IS_ERR(new.next)) { + tdb->ecode = new.next; return -1; + } if (new.next) { #ifdef CCAN_TDB2_DEBUG @@ -184,17 +194,28 @@ static int enqueue_in_free(struct tdb_context *tdb, } #endif /* next->prev = new. */ - if (tdb_write_off(tdb, new.next - + offsetof(struct tdb_free_record, - magic_and_prev), - off | magic) != 0) + ecode = tdb_write_off(tdb, new.next + + offsetof(struct tdb_free_record, + magic_and_prev), + off | magic); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; return -1; + } } /* head = new */ - if (tdb_write_off(tdb, b_off, off) != 0) + ecode = tdb_write_off(tdb, b_off, off); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; return -1; + } - return tdb_write_convert(tdb, off, &new, sizeof(new)); + ecode = tdb_write_convert(tdb, off, &new, sizeof(new)); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; + return -1; + } + return 0; } /* List need not be locked. */ @@ -258,8 +279,13 @@ static tdb_off_t ftable_offset(struct tdb_context *tdb, unsigned int ftable) return tdb->ftable_off; off = first_ftable(tdb); - for (i = 0; i < ftable; i++) + for (i = 0; i < ftable; i++) { + if (TDB_OFF_IS_ERR(off)) { + tdb->ecode = off; + break; + } off = next_ftable(tdb, off); + } return off; } @@ -269,6 +295,7 @@ static int coalesce(struct tdb_context *tdb, { tdb_off_t end; struct tdb_free_record rec; + enum TDB_ERROR ecode; add_stat(tdb, alloc_coalesce_tried, 1); end = off + sizeof(struct tdb_used_record) + data_len; @@ -279,8 +306,10 @@ static int coalesce(struct tdb_context *tdb, unsigned ftable, bucket; r = tdb_access_read(tdb, end, sizeof(*r), true); - if (!r) + if (TDB_PTR_IS_ERR(r)) { + tdb->ecode = TDB_PTR_ERR(r); goto err; + } if (frec_magic(r) != TDB_FREE_MAGIC || frec_ftable(r) == TDB_FTABLE_NONE) { @@ -301,7 +330,9 @@ static int coalesce(struct tdb_context *tdb, } /* Now we have lock, re-check. */ - if (tdb_read_convert(tdb, end, &rec, sizeof(rec))) { + ecode = tdb_read_convert(tdb, end, &rec, sizeof(rec)); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; tdb_unlock_free_bucket(tdb, nb_off); goto err; } @@ -334,8 +365,11 @@ static int coalesce(struct tdb_context *tdb, return 0; /* OK, expand initial record */ - if (tdb_read_convert(tdb, off, &rec, sizeof(rec))) + ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec)); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; goto err; + } if (frec_len(&rec) != data_len) { tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, @@ -351,10 +385,13 @@ static int coalesce(struct tdb_context *tdb, * doesn't get coalesced by someone else! */ rec.ftable_and_len = (TDB_FTABLE_NONE << (64 - TDB_OFF_UPPER_STEAL)) | (end - off - sizeof(struct tdb_used_record)); - if (tdb_write_off(tdb, off + offsetof(struct tdb_free_record, - ftable_and_len), - rec.ftable_and_len) != 0) + ecode = tdb_write_off(tdb, off + offsetof(struct tdb_free_record, + ftable_and_len), + rec.ftable_and_len); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; goto err; + } add_stat(tdb, alloc_coalesce_succeeded, 1); tdb_unlock_free_bucket(tdb, b_off); @@ -408,8 +445,10 @@ again: /* Walk the list to see if any are large enough, getting less fussy * as we go. */ off = tdb_read_off(tdb, b_off); - if (unlikely(off == TDB_OFF_ERR)) + if (TDB_OFF_IS_ERR(off)) { + tdb->ecode = off; goto unlock_err; + } while (off) { const struct tdb_free_record *r; @@ -417,8 +456,10 @@ again: tdb_off_t next; r = tdb_access_read(tdb, off, sizeof(*r), true); - if (!r) + if (TDB_PTR_IS_ERR(r)) { + tdb->ecode = TDB_PTR_ERR(r); goto unlock_err; + } if (frec_magic(r) != TDB_FREE_MAGIC) { tdb_access_release(tdb, r); @@ -475,8 +516,11 @@ again: frec_len(&best) - leftover, hashlow) != 0) goto unlock_err; - if (tdb_write_convert(tdb, best_off, &rec, sizeof(rec)) != 0) + ecode = tdb_write_convert(tdb, best_off, &rec, sizeof(rec)); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; goto unlock_err; + } /* Bucket of leftover will be <= current bucket, so nested * locking is allowed. */ @@ -507,7 +551,7 @@ static tdb_off_t get_free(struct tdb_context *tdb, unsigned magic, unsigned hashlow) { tdb_off_t off, ftable_off; - unsigned start_b, b, ftable; + tdb_off_t start_b, b, ftable; bool wrapped = false; /* If they are growing, add 50% to get to higher bucket. */ @@ -543,13 +587,26 @@ static tdb_off_t get_free(struct tdb_context *tdb, /* Didn't work. Try next bucket. */ } + if (TDB_OFF_IS_ERR(b)) { + tdb->ecode = b; + return 0; + } + /* Hmm, try next table. */ ftable_off = next_ftable(tdb, ftable_off); + if (TDB_OFF_IS_ERR(ftable_off)) { + tdb->ecode = ftable_off; + return 0; + } ftable++; if (ftable_off == 0) { wrapped = true; ftable_off = first_ftable(tdb); + if (TDB_OFF_IS_ERR(ftable_off)) { + tdb->ecode = ftable_off; + return 0; + } ftable = 0; } } @@ -625,7 +682,9 @@ static int tdb_expand(struct tdb_context *tdb, tdb_len_t size) return 0; } - if (tdb->methods->expand_file(tdb, wanted) == -1) { + ecode = tdb->methods->expand_file(tdb, wanted); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; tdb_unlock_expand(tdb, F_WRLCK); return -1; }