X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Flock.c;h=76b8bc3157977462fbebc3983caad5dbb366ed5c;hp=4033d0afe813e59bf4dee4b8bcc9e5d34757c298;hb=c2d60644d2c00fb32258b380bb25ce0a26b87026;hpb=dc9da1e34fe6a9d113fd57e116ebbc6d5bd54819 diff --git a/ccan/tdb2/lock.c b/ccan/tdb2/lock.c index 4033d0af..76b8bc31 100644 --- a/ccan/tdb2/lock.c +++ b/ccan/tdb2/lock.c @@ -37,7 +37,7 @@ static enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call) call); } -/* If we fork, we no longer really own locks: preserves errno */ +/* If we fork, we no longer really own locks. */ static bool check_lock_pid(struct tdb_context *tdb, const char *call, bool log) { @@ -99,16 +99,21 @@ int tdb_fcntl_unlock(int fd, int rw, off_t off, off_t len, void *unused) static int lock(struct tdb_context *tdb, int rw, off_t off, off_t len, bool waitflag) { + int ret; if (tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0) { tdb->file->locker = getpid(); } tdb->stats.lock_lowlevel++; - if (!waitflag) + ret = tdb->lock_fn(tdb->file->fd, rw, off, len, waitflag, + tdb->lock_data); + if (!waitflag) { tdb->stats.lock_nonblock++; - return tdb->lock_fn(tdb->file->fd, rw, off, len, waitflag, - tdb->lock_data); + if (ret != 0) + tdb->stats.lock_nonblock_fail++; + } + return ret; } static int unlock(struct tdb_context *tdb, int rw, off_t off, off_t len) @@ -224,16 +229,14 @@ static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb, static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb, int rw_type, tdb_off_t offset, size_t len) { - int ret; - if (tdb->flags & TDB_NOLOCK) { return TDB_SUCCESS; } - ret = unlock(tdb, rw_type, offset, len); + if (!check_lock_pid(tdb, "tdb_brunlock", true)) + return TDB_ERR_LOCK; - /* If we fail, *then* we verify that we owned the lock. If not, ok. */ - if (ret == -1 && check_lock_pid(tdb, "tdb_brunlock", false)) { + if (unlock(tdb, rw_type, offset, len) == -1) { return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, "tdb_brunlock failed (fd=%d) at offset %zu" " rw_type=%d len=%zu: %s", @@ -325,13 +328,13 @@ enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb) return ecode; } - ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK); + ecode = tdb_lock_open(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK); if (ecode != TDB_SUCCESS) { tdb_allrecord_unlock(tdb, F_WRLCK); return ecode; } ecode = tdb_transaction_recover(tdb); - tdb_unlock_open(tdb); + tdb_unlock_open(tdb, F_WRLCK); tdb_allrecord_unlock(tdb, F_WRLCK); return ecode; @@ -612,14 +615,15 @@ again: goto again; } -enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags) +enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb, + int ltype, enum tdb_lock_flags flags) { - return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, flags); + return tdb_nest_lock(tdb, TDB_OPEN_LOCK, ltype, flags); } -void tdb_unlock_open(struct tdb_context *tdb) +void tdb_unlock_open(struct tdb_context *tdb, int ltype) { - tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK); + tdb_nest_unlock(tdb, TDB_OPEN_LOCK, ltype); } bool tdb_has_open_lock(struct tdb_context *tdb) @@ -851,6 +855,10 @@ void tdb_lock_cleanup(struct tdb_context *tdb) { unsigned int i; + /* We don't want to warn: they're allowed to close tdb after fork. */ + if (!check_lock_pid(tdb, "tdb_close", false)) + return; + while (tdb->file->allrecord_lock.count && tdb->file->allrecord_lock.owner == tdb) { tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);