]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb/tdb.c
tdb: use TDB_RECOVERY_INVALID_MAGIC rather than 0
[ccan] / ccan / tdb / tdb.c
index 3e663345e60c5f9ebc14e1d41813f39a7d3bc4bd..cb0b88519d961ea292bcfd7d2b8db2772f50ad37 100644 (file)
@@ -59,13 +59,14 @@ static void tdb_increment_seqnum(struct tdb_context *tdb)
                return;
        }
 
-       if (tdb_brlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, F_SETLKW, 1, 1) != 0) {
+       if (tdb_brlock(tdb, F_WRLCK, TDB_SEQNUM_OFS, 1,
+                      TDB_LOCK_WAIT|TDB_LOCK_PROBE) != 0) {
                return;
        }
 
        tdb_increment_seqnum_nonblock(tdb);
 
-       tdb_brlock(tdb, TDB_SEQNUM_OFS, F_UNLCK, F_SETLKW, 1, 1);
+       tdb_brunlock(tdb, F_WRLCK, TDB_SEQNUM_OFS, 1);
 }
 
 static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
@@ -76,7 +77,7 @@ static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
 /* Returns 0 on fail.  On success, return offset of record, and fills
    in rec */
 static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
-                       struct list_struct *r)
+                       struct tdb_record *r)
 {
        tdb_off_t rec_ptr;
        
@@ -98,17 +99,19 @@ static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
                }
                /* detect tight infinite loop */
                if (rec_ptr == r->next) {
+                       tdb->ecode = TDB_ERR_CORRUPT;
                        TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n"));
-                       return TDB_ERRCODE(TDB_ERR_CORRUPT, 0);
+                       return 0;
                }
                rec_ptr = r->next;
        }
-       return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
+       tdb->ecode = TDB_ERR_NOEXIST;
+       return 0;
 }
 
 /* As tdb_find, but if you succeed, keep the lock */
 tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
-                          struct list_struct *rec)
+                          struct tdb_record *rec)
 {
        uint32_t rec_ptr;
 
@@ -119,6 +122,7 @@ tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t has
        return rec_ptr;
 }
 
+static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
 
 /* update an entry in place - this only works if the new data size
    is <= the old data size and the key exists.
@@ -126,13 +130,32 @@ tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t has
 */
 static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, TDB_DATA dbuf)
 {
-       struct list_struct rec;
+       struct tdb_record rec;
        tdb_off_t rec_ptr;
 
        /* find entry */
        if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
                return -1;
 
+       /* it could be an exact duplicate of what is there - this is
+        * surprisingly common (eg. with a ldb re-index). */
+       if (rec.key_len == key.dsize && 
+           rec.data_len == dbuf.dsize &&
+           rec.full_hash == hash) {
+               TDB_DATA data = _tdb_fetch(tdb, key);
+               if (data.dsize == dbuf.dsize &&
+                   memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
+                       if (data.dptr) {
+                               free(data.dptr);
+                       }
+                       return 0;
+               }
+               if (data.dptr) {
+                       free(data.dptr);
+               }
+       }
+        
+
        /* must be long enough key, data and tailer */
        if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
                tdb->ecode = TDB_SUCCESS; /* Not really an error */
@@ -158,10 +181,10 @@ static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
  * then the TDB_DATA will have zero length but
  * a non-zero pointer
  */
-static TDB_DATA do_tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
+static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
 {
        tdb_off_t rec_ptr;
-       struct list_struct rec;
+       struct tdb_record rec;
        TDB_DATA ret;
        uint32_t hash;
 
@@ -179,7 +202,7 @@ static TDB_DATA do_tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
 
 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
 {
-       TDB_DATA ret = do_tdb_fetch(tdb, key);
+       TDB_DATA ret = _tdb_fetch(tdb, key);
 
        tdb_trace_1rec_retrec(tdb, "tdb_fetch", key, ret);
        return ret;
@@ -207,7 +230,7 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
                     void *private_data)
 {
        tdb_off_t rec_ptr;
-       struct list_struct rec;
+       struct tdb_record rec;
        int ret;
        uint32_t hash;
 
@@ -217,7 +240,8 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
        if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
                tdb_trace_1rec_ret(tdb, "tdb_parse_record", key,
                                   -TDB_ERR_NOEXIST);
-               return TDB_ERRCODE(TDB_ERR_NOEXIST, 0);
+               tdb->ecode = TDB_ERR_NOEXIST;
+               return 0;
        }
        tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
 
@@ -237,7 +261,7 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
 */
 static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
 {
-       struct list_struct rec;
+       struct tdb_record rec;
        
        if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
                return 0;
@@ -256,10 +280,10 @@ int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
 }
 
 /* actually delete an entry in the database given the offset */
-int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec)
+int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct tdb_record *rec)
 {
        tdb_off_t last_ptr, i;
-       struct list_struct lastrec;
+       struct tdb_record lastrec;
 
        if (tdb->read_only || tdb->traverse_read) return -1;
 
@@ -295,7 +319,7 @@ static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
 {
        int res = 0;
        tdb_off_t rec_ptr;
-       struct list_struct rec;
+       struct tdb_record rec;
        
        /* read in the hash top */
        if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
@@ -319,7 +343,7 @@ static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
 static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
 {
        int res = -1;
-       struct list_struct rec;
+       struct tdb_record rec;
        tdb_off_t rec_ptr;
 
        if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
@@ -355,7 +379,7 @@ static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
 static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
 {
        tdb_off_t rec_ptr;
-       struct list_struct rec;
+       struct tdb_record rec;
        int ret;
 
        if (tdb->max_dead_records != 0) {
@@ -418,7 +442,7 @@ int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
  * See if we have a dead record around with enough space
  */
 static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
-                              struct list_struct *r, tdb_len_t length)
+                              struct tdb_record *r, tdb_len_t length)
 {
        tdb_off_t rec_ptr;
        
@@ -443,10 +467,10 @@ static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
        return 0;
 }
 
-static int _tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
-                     int flag, uint32_t hash)
+static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
+                     TDB_DATA dbuf, int flag, uint32_t hash)
 {
-       struct list_struct rec;
+       struct tdb_record rec;
        tdb_off_t rec_ptr;
        char *p = NULL;
        int ret = -1;
@@ -610,7 +634,7 @@ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
        if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
                return -1;
 
-       dbuf = do_tdb_fetch(tdb, key);
+       dbuf = _tdb_fetch(tdb, key);
 
        if (dbuf.dptr == NULL) {
                dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
@@ -711,11 +735,41 @@ int tdb_get_flags(struct tdb_context *tdb)
 
 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
 {
+       if ((flags & TDB_ALLOW_NESTING) &&
+           (flags & TDB_DISALLOW_NESTING)) {
+               tdb->ecode = TDB_ERR_NESTING;
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_add_flags: "
+                       "allow_nesting and disallow_nesting are not allowed together!"));
+               return;
+       }
+
+       if (flags & TDB_ALLOW_NESTING) {
+               tdb->flags &= ~TDB_DISALLOW_NESTING;
+       }
+       if (flags & TDB_DISALLOW_NESTING) {
+               tdb->flags &= ~TDB_ALLOW_NESTING;
+       }
+
        tdb->flags |= flags;
 }
 
 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
 {
+       if ((flags & TDB_ALLOW_NESTING) &&
+           (flags & TDB_DISALLOW_NESTING)) {
+               tdb->ecode = TDB_ERR_NESTING;
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_remove_flags: "
+                       "allow_nesting and disallow_nesting are not allowed together!"));
+               return;
+       }
+
+       if (flags & TDB_ALLOW_NESTING) {
+               tdb->flags |= TDB_DISALLOW_NESTING;
+       }
+       if (flags & TDB_DISALLOW_NESTING) {
+               tdb->flags |= TDB_ALLOW_NESTING;
+       }
+
        tdb->flags &= ~flags;
 }
 
@@ -735,7 +789,7 @@ void tdb_enable_seqnum(struct tdb_context *tdb)
  */
 static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
 {
-       struct list_struct rec;
+       struct tdb_record rec;
        if (length <= sizeof(rec)) {
                /* the region is not worth adding */
                return 0;
@@ -784,7 +838,7 @@ int tdb_wipe_all(struct tdb_context *tdb)
        }
 
        if (recovery_head != 0) {
-               struct list_struct rec;
+               struct tdb_record rec;
                if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
                        TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
                        return -1;