X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb%2Ftransaction.c;h=e34dee05c6bd80d148e6965ba1908e9265d7b01a;hb=695e2ae319052ceb410ef00d0181aa607f1ceb6d;hp=fc0ca1d9a15e50d69a1aa29e69d92195781dc7a4;hpb=f0d790f1304cf66b679e0c7ee92bc52933b562b8;p=ccan diff --git a/ccan/tdb/transaction.c b/ccan/tdb/transaction.c index fc0ca1d9..e34dee05 100644 --- a/ccan/tdb/transaction.c +++ b/ccan/tdb/transaction.c @@ -408,33 +408,12 @@ static int transaction_expand_file(struct tdb_context *tdb, tdb_off_t size, return 0; } -/* - brlock during a transaction - ignore them -*/ -static int transaction_brlock(struct tdb_context *tdb, - int rw_type, tdb_off_t offset, size_t len, - enum tdb_lock_flags flags) -{ - /* FIXME: We actually grab the open lock during a transaction. */ - if (offset == OPEN_LOCK) - return tdb_brlock(tdb, rw_type, offset, len, flags); - return 0; -} - -static int transaction_brunlock(struct tdb_context *tdb, - int rw_type, tdb_off_t offset, size_t len) -{ - return 0; -} - static const struct tdb_methods transaction_methods = { transaction_read, transaction_write, transaction_next_hash_chain, transaction_oob, transaction_expand_file, - transaction_brlock, - transaction_brunlock }; /* @@ -674,7 +653,7 @@ static int tdb_recovery_allocate(struct tdb_context *tdb, { struct tdb_record rec; const struct tdb_methods *methods = tdb->transaction->io_methods; - tdb_off_t recovery_head; + tdb_off_t recovery_head, new_end; if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) { TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery head\n")); @@ -683,14 +662,21 @@ static int tdb_recovery_allocate(struct tdb_context *tdb, rec.rec_len = 0; - if (recovery_head != 0 && - methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) { - TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery record\n")); - return -1; + if (recovery_head != 0) { + if (methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) { + TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery record\n")); + return -1; + } + /* ignore invalid recovery regions: can happen in crash */ + if (rec.magic != TDB_RECOVERY_MAGIC && + rec.magic != TDB_RECOVERY_INVALID_MAGIC) { + recovery_head = 0; + } } *recovery_size = tdb_recovery_size(tdb); + /* Existing recovery area? */ if (recovery_head != 0 && *recovery_size <= rec.rec_len) { /* it fits in the existing area */ *recovery_max_size = rec.rec_len; @@ -698,29 +684,45 @@ static int tdb_recovery_allocate(struct tdb_context *tdb, return 0; } - /* we need to free up the old recovery area, then allocate a - new one at the end of the file. Note that we cannot use - tdb_allocate() to allocate the new one as that might return - us an area that is being currently used (as of the start of - the transaction) */ - if (recovery_head != 0) { - if (tdb_free(tdb, recovery_head, &rec) == -1) { - TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to free previous recovery area\n")); - return -1; + /* If recovery area in middle of file, we need a new one. */ + if (recovery_head == 0 + || recovery_head + sizeof(rec) + rec.rec_len != tdb->map_size) { + /* we need to free up the old recovery area, then allocate a + new one at the end of the file. Note that we cannot use + tdb_allocate() to allocate the new one as that might return + us an area that is being currently used (as of the start of + the transaction) */ + if (recovery_head) { + if (tdb_free(tdb, recovery_head, &rec) == -1) { + TDB_LOG((tdb, TDB_DEBUG_FATAL, + "tdb_recovery_allocate: failed to" + " free previous recovery area\n")); + return -1; + } + + /* the tdb_free() call might have increased + * the recovery size */ + *recovery_size = tdb_recovery_size(tdb); } + + /* New head will be at end of file. */ + recovery_head = tdb->map_size; } - /* the tdb_free() call might have increased the recovery size */ - *recovery_size = tdb_recovery_size(tdb); + /* Now we know where it will be. */ + *recovery_offset = recovery_head; + + /* Expand by more than we need, so we don't do it often. */ + *recovery_max_size = tdb_expand_adjust(tdb->map_size, + *recovery_size, + tdb->page_size) + - sizeof(rec); - /* round up to a multiple of page size */ - *recovery_max_size = TDB_ALIGN(sizeof(rec) + *recovery_size, tdb->page_size) - sizeof(rec); - *recovery_offset = tdb->map_size; - recovery_head = *recovery_offset; + new_end = recovery_head + sizeof(rec) + *recovery_max_size; if (methods->tdb_expand_file(tdb, tdb->transaction->old_map_size, - (tdb->map_size - tdb->transaction->old_map_size) + - sizeof(rec) + *recovery_max_size) == -1) { + new_end - tdb->transaction->old_map_size) + == -1) { TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to create recovery area\n")); return -1; } @@ -1197,16 +1199,6 @@ int tdb_transaction_recover(struct tdb_context *tdb) return -1; } - /* reduce the file size to the old size */ - tdb_munmap(tdb); - if (ftruncate(tdb->fd, recovery_eof) != 0) { - TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to reduce to recovery size\n")); - tdb->ecode = TDB_ERR_IO; - return -1; - } - tdb->map_size = recovery_eof; - tdb_mmap(tdb); - if (transaction_sync(tdb, 0, recovery_eof) == -1) { TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to sync2 recovery\n")); tdb->ecode = TDB_ERR_IO; @@ -1219,3 +1211,28 @@ int tdb_transaction_recover(struct tdb_context *tdb) /* all done */ return 0; } + +/* Any I/O failures we say "needs recovery". */ +bool tdb_needs_recovery(struct tdb_context *tdb) +{ + tdb_off_t recovery_head; + struct tdb_record rec; + + /* find the recovery area */ + if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) { + return true; + } + + if (recovery_head == 0) { + /* we have never allocated a recovery record */ + return false; + } + + /* read the recovery record */ + if (tdb->methods->tdb_read(tdb, recovery_head, &rec, + sizeof(rec), DOCONV()) == -1) { + return true; + } + + return (rec.magic == TDB_RECOVERY_MAGIC); +}