]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/lock.c
tdb2: remove all the dead code
[ccan] / ccan / tdb2 / lock.c
index 5d13a23aaec89f17e731ffc71415fb43ceb7bafb..fe6072439f1c00c20ddf8005698c7608f7de2390 100644 (file)
@@ -190,7 +190,6 @@ static int tdb_brunlock(struct tdb_context *tdb,
        return ret;
 }
 
-#if 0
 /*
   upgrade a read lock to a write lock. This needs to be handled in a
   special way as some OSes (such as solaris) have too conservative
@@ -217,8 +216,7 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb)
        while (count--) {
                struct timeval tv;
                if (tdb_brlock(tdb, F_WRLCK,
-                              TDB_HASH_LOCK_START
-                              + (1ULL << tdb->header.v.hash_bits), 0,
+                              TDB_HASH_LOCK_START, 0,
                               TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
                        tdb->allrecord_lock.ltype = F_WRLCK;
                        tdb->allrecord_lock.off = 0;
@@ -236,7 +234,6 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb)
                 "tdb_allrecord_upgrade failed\n");
        return -1;
 }
-#endif
 
 static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
                                           tdb_off_t offset)
@@ -251,6 +248,27 @@ static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
        return NULL;
 }
 
+int tdb_lock_and_recover(struct tdb_context *tdb)
+{
+       int ret;
+
+       if (tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK,
+                              false) == -1) {
+               return -1;
+       }
+
+       if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
+               tdb_allrecord_unlock(tdb, F_WRLCK);
+               return -1;
+       }
+       ret = tdb_transaction_recover(tdb);
+
+       tdb_unlock_open(tdb);
+       tdb_allrecord_unlock(tdb, F_WRLCK);
+
+       return ret;
+}
+
 /* lock an offset in the database. */
 static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
                         enum tdb_lock_flags flags)
@@ -270,10 +288,14 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
 
        new_lck = find_nestlock(tdb, offset);
        if (new_lck) {
-               /*
-                * Just increment the in-memory struct, posix locks
-                * don't stack.
-                */
+               if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
+                       tdb->ecode = TDB_ERR_LOCK;
+                       tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
+                                "tdb_nest_lock: offset %llu has read lock\n",
+                                (long long)offset);
+                       return -1;
+               }
+               /* Just increment the struct, posix locks don't stack. */
                new_lck->count++;
                return 0;
        }
@@ -306,6 +328,21 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
                return -1;
        }
 
+       /* First time we grab a lock, perhaps someone died in commit? */
+       if (!(flags & TDB_LOCK_NOCHECK)
+           && tdb->num_lockrecs == 0
+           && unlikely(tdb_needs_recovery(tdb))) {
+               tdb_brunlock(tdb, ltype, offset, 1);
+
+               if (tdb_lock_and_recover(tdb) == -1) {
+                       return -1;
+               }
+
+               if (tdb_brlock(tdb, ltype, offset, 1, flags)) {
+                       return -1;
+               }
+       }
+
        tdb->lockrecs[tdb->num_lockrecs].off = offset;
        tdb->lockrecs[tdb->num_lockrecs].count = 1;
        tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
@@ -314,40 +351,6 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
        return 0;
 }
 
-static int tdb_lock_and_recover(struct tdb_context *tdb)
-{
-#if 0 /* FIXME */
-
-       int ret;
-
-       /* We need to match locking order in transaction commit. */
-       if (tdb_brlock(tdb, F_WRLCK, FREELIST_TOP, 0, TDB_LOCK_WAIT)) {
-               return -1;
-       }
-
-       if (tdb_brlock(tdb, F_WRLCK, OPEN_LOCK, 1, TDB_LOCK_WAIT)) {
-               tdb_brunlock(tdb, F_WRLCK, FREELIST_TOP, 0);
-               return -1;
-       }
-
-       ret = tdb_transaction_recover(tdb);
-
-       tdb_brunlock(tdb, F_WRLCK, OPEN_LOCK, 1);
-       tdb_brunlock(tdb, F_WRLCK, FREELIST_TOP, 0);
-
-       return ret;
-#else
-       abort();
-       return -1;
-#endif
-}
-
-static bool tdb_needs_recovery(struct tdb_context *tdb)
-{
-       /* FIXME */
-       return false;
-}
-
 static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
 {
        int ret = -1;
@@ -386,14 +389,12 @@ static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
        return ret;
 }
 
-#if 0
 /*
   get the transaction lock
  */
-int tdb_transaction_lock(struct tdb_context *tdb, int ltype,
-                        enum tdb_lock_flags lockflags)
+int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
 {
-       return tdb_nest_lock(tdb, TRANSACTION_LOCK, ltype, lockflags);
+       return tdb_nest_lock(tdb, TDB_TRANSACTION_LOCK, ltype, TDB_LOCK_WAIT);
 }
 
 /*
@@ -401,9 +402,8 @@ int tdb_transaction_lock(struct tdb_context *tdb, int ltype,
  */
 int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
 {
-       return tdb_nest_unlock(tdb, TRANSACTION_LOCK, ltype, false);
+       return tdb_nest_unlock(tdb, TDB_TRANSACTION_LOCK, ltype);
 }
-#endif
 
 /* We only need to lock individual bytes, but Linux merges consecutive locks
  * so we lock in contiguous ranges. */
@@ -454,7 +454,8 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
                return -1;
        }
 
-       if (tdb->allrecord_lock.count && tdb->allrecord_lock.ltype == ltype) {
+       if (tdb->allrecord_lock.count
+           && (ltype == F_RDLCK || tdb->allrecord_lock.ltype == F_WRLCK)) {
                tdb->allrecord_lock.count++;
                return 0;
        }
@@ -469,7 +470,7 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
                return -1;
        }
 
-       if (tdb_has_locks(tdb)) {
+       if (tdb_has_hash_locks(tdb)) {
                /* can't combine global and chain locks */
                tdb->ecode = TDB_ERR_LOCK;
                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
@@ -517,7 +518,7 @@ again:
        tdb->allrecord_lock.off = upgradable;
 
        /* Now check for needing recovery. */
-       if (unlikely(tdb_needs_recovery(tdb))) {
+       if (!(flags & TDB_LOCK_NOCHECK) && unlikely(tdb_needs_recovery(tdb))) {
                tdb_allrecord_unlock(tdb, ltype);
                if (tdb_lock_and_recover(tdb) == -1) {
                        return -1;
@@ -528,9 +529,9 @@ again:
        return 0;
 }
 
-int tdb_lock_open(struct tdb_context *tdb)
+int tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags)
 {
-       return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, TDB_LOCK_WAIT);
+       return tdb_nest_lock(tdb, TDB_OPEN_LOCK, F_WRLCK, flags);
 }
 
 void tdb_unlock_open(struct tdb_context *tdb)
@@ -538,9 +539,16 @@ void tdb_unlock_open(struct tdb_context *tdb)
        tdb_nest_unlock(tdb, TDB_OPEN_LOCK, F_WRLCK);
 }
 
+bool tdb_has_open_lock(struct tdb_context *tdb)
+{
+       return find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
+}
+
 int tdb_lock_expand(struct tdb_context *tdb, int ltype)
 {
-       return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype, TDB_LOCK_WAIT);
+       /* Lock doesn't protect data, so don't check (we recurse if we do!) */
+       return tdb_nest_lock(tdb, TDB_EXPANSION_LOCK, ltype,
+                            TDB_LOCK_WAIT | TDB_LOCK_NOCHECK);
 }
 
 void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
@@ -593,56 +601,18 @@ bool tdb_has_expansion_lock(struct tdb_context *tdb)
        return find_nestlock(tdb, TDB_EXPANSION_LOCK) != NULL;
 }
 
-bool tdb_has_locks(struct tdb_context *tdb)
-{
-       return tdb->allrecord_lock.count || tdb->num_lockrecs;
-}
-
-#if 0
-/* lock entire database with write lock */
-int tdb_lockall(struct tdb_context *tdb)
-{
-       tdb_trace(tdb, "tdb_lockall");
-       return tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
-}
-
-/* lock entire database with write lock - nonblocking varient */
-int tdb_lockall_nonblock(struct tdb_context *tdb)
-{
-       int ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_NOWAIT, false);
-       tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
-       return ret;
-}
-
-/* unlock entire database with write lock */
-int tdb_unlockall(struct tdb_context *tdb)
-{
-       tdb_trace(tdb, "tdb_unlockall");
-       return tdb_allrecord_unlock(tdb, F_WRLCK);
-}
-
-/* lock entire database with read lock */
-int tdb_lockall_read(struct tdb_context *tdb)
+bool tdb_has_hash_locks(struct tdb_context *tdb)
 {
-       tdb_trace(tdb, "tdb_lockall_read");
-       return tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
-}
-
-/* lock entire database with read lock - nonblock varient */
-int tdb_lockall_read_nonblock(struct tdb_context *tdb)
-{
-       int ret = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_NOWAIT, false);
-       tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
-       return ret;
-}
+       unsigned int i;
 
-/* unlock entire database with read lock */
-int tdb_unlockall_read(struct tdb_context *tdb)
-{
-       tdb_trace(tdb, "tdb_unlockall_read");
-       return tdb_allrecord_unlock(tdb, F_RDLCK);
+       for (i=0; i<tdb->num_lockrecs; i++) {
+               if (tdb->lockrecs[i].off >= TDB_HASH_LOCK_START
+                   && tdb->lockrecs[i].off < (TDB_HASH_LOCK_START
+                                              + TDB_HASH_LOCK_RANGE))
+                       return true;
+       }
+       return false;
 }
-#endif
 
 static bool tdb_has_free_lock(struct tdb_context *tdb)
 {
@@ -765,117 +735,6 @@ void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
        tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
 }
 
-#if 0
-/* lock/unlock one hash chain, non-blocking. This is meant to be used
-   to reduce contention - it cannot guarantee how many records will be
-   locked */
-int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
-{
-       return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_NOWAIT,
-                        "tdb_chainlock_nonblock");
-}
-
-int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
-{
-       return chainlock(tdb, &key, F_RDLCK, TDB_LOCK_WAIT,
-                        "tdb_chainlock_read");
-}
-
-int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
-{
-       uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
-       tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
-       return tdb_unlock_list(tdb, h & ((1ULL << tdb->header.v.hash_bits)-1),
-                              F_RDLCK);
-}
-
-/* record lock stops delete underneath */
-int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
-{
-       if (tdb->allrecord_lock.count) {
-               return 0;
-       }
-       return off ? tdb_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
-}
-
-/*
-  Write locks override our own fcntl readlocks, so check it here.
-  Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
-  an error to fail to get the lock here.
-*/
-int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
-{
-       struct tdb_traverse_lock *i;
-       for (i = &tdb->travlocks; i; i = i->next)
-               if (i->off == off)
-                       return -1;
-       if (tdb->allrecord_lock.count) {
-               if (tdb->allrecord_lock.ltype == F_WRLCK) {
-                       return 0;
-               }
-               return -1;
-       }
-       return tdb_brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
-}
-
-int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
-{
-       if (tdb->allrecord_lock.count) {
-               return 0;
-       }
-       return tdb_brunlock(tdb, F_WRLCK, off, 1);
-}
-
-/* fcntl locks don't stack: avoid unlocking someone else's */
-int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
-{
-       struct tdb_traverse_lock *i;
-       uint32_t count = 0;
-
-       if (tdb->allrecord_lock.count) {
-               return 0;
-       }
-
-       if (off == 0)
-               return 0;
-       for (i = &tdb->travlocks; i; i = i->next)
-               if (i->off == off)
-                       count++;
-       return (count == 1 ? tdb_brunlock(tdb, F_RDLCK, off, 1) : 0);
-}
-
-/* The transaction code uses this to remove all locks. */
-void tdb_release_transaction_locks(struct tdb_context *tdb)
-{
-       unsigned int i;
-
-       if (tdb->allrecord_lock.count != 0) {
-               tdb_off_t hash_size, free_size;
-
-               hash_size = (1ULL << tdb->header.v.hash_bits)
-                       * sizeof(tdb_off_t);
-               free_size = tdb->header.v.free_zones 
-                       * (tdb->header.v.free_buckets + 1) * sizeof(tdb_off_t);
-
-               tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
-                            tdb->header.v.hash_off, hash_size);
-               tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
-                            tdb->header.v.free_off, free_size);
-               tdb->allrecord_lock.count = 0;
-               tdb->allrecord_lock.ltype = 0;
-       }
-
-       for (i = 0; i<tdb->num_lockrecs; i++) {
-               struct tdb_lock_type *lck = &tdb->lockrecs[i];
-
-               tdb_brunlock(tdb, lck->ltype, lck->off, 1);
-       }
-       tdb->num_lockrecs = 0;
-       SAFE_FREE(tdb->lockrecs);
-       tdb->header_uptodate = false;
-}
-#endif
-
 void tdb_lock_init(struct tdb_context *tdb)
 {
        tdb->num_lockrecs = 0;