X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Flock.c;h=957adf83358057d458d09320d1ce546feb62bffe;hp=49313a5419cb7b83b7f672aaff32f5942ddf079b;hb=b87e14495d5b07e1b247218a72329f10ecb3da7f;hpb=f6900d2358ca0ec2f2dc776b9e42e4fca7bffbdf diff --git a/ccan/tdb2/lock.c b/ccan/tdb2/lock.c index 49313a54..957adf83 100644 --- a/ccan/tdb2/lock.c +++ b/ccan/tdb2/lock.c @@ -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) @@ -189,7 +194,7 @@ static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb, return TDB_SUCCESS; } - if (rw_type == F_WRLCK && tdb->read_only) { + if (rw_type == F_WRLCK && (tdb->flags & TDB_RDONLY)) { return tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_LOG_USE_ERROR, "Write lock attempted on read-only database"); } @@ -323,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; @@ -503,8 +508,9 @@ static enum TDB_ERROR tdb_lock_gradual(struct tdb_context *tdb, } /* First we try non-blocking. */ - if (tdb_brlock(tdb, ltype, off, len, nb_flags) == TDB_SUCCESS) { - return TDB_SUCCESS; + ecode = tdb_brlock(tdb, ltype, off, len, nb_flags); + if (ecode != TDB_ERR_LOCK) { + return ecode; } /* Try locking first half, then second. */ @@ -610,14 +616,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)