2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
19 #include <netinet/in.h>
28 #include <sys/types.h>
31 #include <afs/bubasics.h>
32 #include "budb_errs.h"
34 #include "error_macros.h"
37 int sizeFunctions[HT_MAX_FUNCTION + 1];
38 int nHTBuckets = NhtBucketS; /* testing: we need small HT blocks */
40 /* ht_TableSize - return the size of table necessary to represent a hashtable
41 * of given length in memory. It basically rounds the length up by the number
42 * of buckets per block. */
51 n = (length + nHTBuckets - 1) / nHTBuckets;
52 return n * sizeof(struct memoryHTBlock *);
55 /* ht_ResetT - resets the in-memory representation of a hashtable block array.
56 * It also resets the global variable nHTBuckets. */
59 ht_ResetT(blocksP, sizeP, length)
60 struct memoryHTBlock ***blocksP;
64 struct memoryHTBlock **b = *blocksP;
69 nHTBuckets = ntohl(db.h.nHTBuckets);
71 n = *sizeP / sizeof(b[0]);
72 newsize = ht_TableSize(length);
73 if (*sizeP != newsize) {
74 /* free all blocks in the old array */
75 for (i = 0; i < n; i++)
82 /* invalidate the blocks of the array */
83 for (i = 0; i < n; i++)
91 * reinitialize a memory hash table.
92 * Calls ht_ResetT to invalidate the two block arrays.
97 struct memoryHashTable *mht;
101 if (!(mht && (ht = mht->ht)))
102 db_panic("some ht called with bad mht");
103 mht->threadOffset = ntohl(ht->threadOffset);
104 mht->length = ntohl(ht->length);
105 mht->oldLength = ntohl(ht->oldLength);
106 mht->progress = ntohl(ht->progress);
107 ht_ResetT(&mht->blocks, &mht->size, mht->length);
108 ht_ResetT(&mht->oldBlocks, &mht->oldSize, mht->oldLength);
111 /* InitDBhash - When server starts, do hash table initialization.
112 test - initialization parameters: bit 4 is small ht. */
117 sizeFunctions[0] = 0;
119 sizeFunctions[HT_dumpIden_FUNCTION] = sizeof(struct dump);
120 sizeFunctions[HT_dumpName_FUNCTION] = sizeof(struct dump);
121 sizeFunctions[HT_volName_FUNCTION] = sizeof(struct volInfo);
122 sizeFunctions[HT_tapeName_FUNCTION] = sizeof(struct tape);
124 db.volName.ht = &db.h.volName;
125 db.tapeName.ht = &db.h.tapeName;
126 db.dumpName.ht = &db.h.dumpName;
127 db.dumpIden.ht = &db.h.dumpIden;
131 /* ht_DBInit - When rebuilding database, this sets up the hash tables. */
136 db.h.nHTBuckets = htonl(nHTBuckets);
139 struct volInfo *s = 0;
140 db.h.volName.threadOffset =
141 htonl((char *)&s->nameHashChain - (char *)s);
142 db.h.volName.functionType = htonl(HT_volName_FUNCTION);
146 db.h.tapeName.threadOffset =
147 htonl((char *)&s->nameHashChain - (char *)s);
148 db.h.tapeName.functionType = htonl(HT_tapeName_FUNCTION);
152 db.h.dumpName.threadOffset =
153 htonl((char *)&s->nameHashChain - (char *)s);
154 db.h.dumpName.functionType = htonl(HT_dumpName_FUNCTION);
156 db.h.dumpIden.threadOffset =
157 htonl((char *)&s->idHashChain - (char *)s);
158 db.h.dumpIden.functionType = htonl(HT_dumpIden_FUNCTION);
160 ht_Reset(&db.volName);
161 ht_Reset(&db.tapeName);
162 ht_Reset(&db.dumpName);
163 ht_Reset(&db.dumpIden);
167 ht_AllocTable(ut, mht)
168 struct ubik_trans *ut;
169 struct memoryHashTable *mht;
171 struct hashTable *ht;
174 int nb, mnb; /* number of blocks for hashTable */
176 struct memoryHTBlock **b;
178 if (!(mht && (ht = mht->ht)))
179 db_panic("some ht called with bad mht");
180 if (ht->length || mht->blocks)
181 db_panic("previous table still allocated");
183 len = ntohl(ht->entries) * 2; /* allow room to grow */
184 nb = (len + nHTBuckets - 1) / nHTBuckets;
185 mnb = ht_minHBlocks(mht);
187 nb = mnb; /* use minimum */
188 len = nb * nHTBuckets; /* new hash table length */
190 mht->size = nb * sizeof(struct memoryHTBlock *);
191 b = mht->blocks = (struct memoryHTBlock **)malloc(mht->size);
192 memset(b, 0, mht->size);
194 for (i = 0; i < nb; i++) {
195 b[i] = (struct memoryHTBlock *)malloc(sizeof(struct memoryHTBlock));
196 code = AllocBlock(ut, (struct block *)&b[i]->b, &b[i]->a);
201 b[i]->b.h.type = hashTable_BLOCK;
203 /* thread the blocks */
205 b[i - 1]->b.h.next = htonl(b[i]->a);
207 for (i = 0; i < nb; i++) {
209 dbwrite(ut, b[i]->a, (char *)&b[i]->b,
210 sizeof(struct htBlock) + (nHTBuckets -
211 NhtBucketS) * sizeof(dbadr));
215 if (code = set_word_addr(ut, 0, &db.h, &ht->table, htonl(b[0]->a)))
218 if (code = set_word_addr(ut, 0, &db.h, &ht->length, htonl(len)))
225 ht_FreeTable(ut, mht)
226 struct ubik_trans *ut;
227 struct memoryHashTable *mht;
229 struct hashTable *ht;
231 struct blockHeader bh;
234 if (!(mht && (ht = mht->ht)))
235 db_panic("some ht called with bad mht");
236 if (ht->oldLength == 0)
237 db_panic("no table to free");
239 ht_ResetT(&mht->oldBlocks, &mht->oldSize, 0);
241 for (a = ntohl(ht->oldTable); a; a = na) {
242 if (dbread(ut, a, (char *)&bh, sizeof(bh))) {
243 Log("ht_FreeTable: dbread failed\n");
247 if (code = FreeBlock(ut, &bh, a))
250 if (set_word_addr(ut, 0, &db.h, &ht->oldTable, 0)
251 || set_word_addr(ut, 0, &db.h, &ht->oldLength, 0)
252 || set_word_addr(ut, 0, &db.h, &ht->progress, 0))
254 mht->oldLength = mht->progress = 0;
259 ht_GetTableBlock(ut, mht, hash, old, blockP, boP)
260 struct ubik_trans *ut;
261 struct memoryHashTable *mht;
264 struct memoryHTBlock **blockP;
267 struct hashTable *ht;
268 struct memoryHTBlock **b;
270 struct memoryHTBlock ***blocksP;
278 || ((ht = mht->ht) == 0)
280 db_panic("some ht called with bad mht");
286 if ((length = mht->oldLength) == 0)
287 return 0; /* no entries */
289 if (hi < mht->progress)
290 return 0; /* no such entry */
291 blocksP = &mht->oldBlocks;
292 sizeP = &mht->oldSize;
294 if ((length = mht->length) == 0)
295 return 0; /* no entries */
297 blocksP = &mht->blocks;
301 bi = hi / nHTBuckets; /* block index */
302 *boP = hi - bi * nHTBuckets; /* block offset ptr */
305 *sizeP = ht_TableSize(length);
306 *blocksP = (struct memoryHTBlock **)malloc(*sizeP);
307 memset(*blocksP, 0, *sizeP);
309 n = *sizeP / sizeof(struct memoryHTBlock *);
311 db_panic("table size inconsistent");
314 /* find an allocated block or the beginning of the block array */
315 for (i = bi; (i > 0) && (b[i] == 0); i--);
319 if (i == 0) { /* the first block is found from the hashTable */
320 ta = ntohl(old ? ht->oldTable : ht->table);
322 db_panic("non-zero length, but no table");
324 /* else ta is set from last time around loop */
326 (struct memoryHTBlock *)malloc(sizeof(struct memoryHTBlock));
332 if (dbread(ut, b[i]->a, (char *)&b[i]->b, sizeof(struct htBlock)))
339 /* printf("ht_GetTableBlock: hash %d block %d offset %d\n",
340 * hash, *blockP, *boP); */
344 ta = ntohl(b[i++]->b.h.next); /* get next ptr from current block */
349 * Decide when to push the current hash table to the old hash table.
350 * The entries in the old hash table are VALID, and are slowly hashed
351 * into the current table.
355 ht_MaybeAdjust(ut, mht)
356 struct ubik_trans *ut;
357 struct memoryHashTable *mht;
359 struct hashTable *ht = mht->ht;
360 int numberEntries = ntohl(ht->entries);
362 /* old hash table must be empty */
363 if (mht->oldLength != 0)
367 * It costs a lot to grow and shrink the hash table. Therefore, we will not
368 * shrink the hash table (only grow it). If the table is more than 2 entries per
369 * chain (average) we need to grow: push the entries to the old hash table.
372 * || ((mht->length > nHTBuckets) && (numberEntries*8 < mht->length))
375 /* Only grow a hash table if the number of entries is twice the
376 * number of hash length and is less than 20,450 (20 hash blocks). This
377 * means that the volname hash table will not grow (its initial
378 * hashtable size contains 30,600 buckets). Earlier revisions of
379 * the buserver have the initial size at 510 and 5,100 buckets -
380 * in which case we do want to grow it). We don't grow anything larger
381 * than 20,450 entries because it's expensive to re-hash everything.
383 if ((numberEntries > mht->length * 2) && (numberEntries < 20450)) { /* push current hash table to old hash table */
384 ht->oldLength = ht->length;
385 ht->oldTable = ht->table;
390 (ut, ((char *)ht - (char *)&db.h), (char *)ht, sizeof(*ht)))
394 LogDebug(2, "ht_MaybeAdjust: push ht to old\n");
400 ht_LookupBucket(ut, mht, hash, old)
401 struct ubik_trans *ut;
402 struct memoryHashTable *mht;
406 struct memoryHTBlock *block;
410 if ((old ? mht->oldLength : mht->length) == 0)
412 code = ht_GetTableBlock(ut, mht, hash, old, &block, &bo);
413 if (code || (block == 0))
415 return ntohl(block->b.bucket[bo]);
418 /* This function is not too bad, for small hash tables, but suffers, I think,
419 * from insufficient mixing of the hash information. */
422 Old2StringHashFunction(str)
425 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
427 hash = (hash << 1) + (hash >> 31) + *str++;
431 /* This was actually a coding error, and produces dreadful results. The
432 * problem is that the hash needs to be mixed up not the incoming character. */
435 Old3StringHashFunction(str)
438 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
440 hash += (*str++) * 0x072a51a4;
444 /* This function is pretty good. Its main problem is that the low two bits of
445 * the hash multiplier are zero which tends to shift information too far left.
446 * It behaves especially badly for hash tables whose size is a power of two. */
449 Old4StringHashFunction(str)
452 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
454 hash = (*str++) + hash * 0x072a51a4;
458 /* While this is good for a hash table with 500 buckets it is nearly as bad as
459 * #3 with a hash table as big as 8200. */
462 Old5StringHashFunction(str)
465 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
471 /* This was an attempt to produce a hash function with the smallest and
472 * simplest mixing multiplier. This is only a little worse than the real one,
473 * and the difference seems to be smaller with larger hash tables. It behaves
474 * better than the random hash function. */
477 Old6StringHashFunction(str)
480 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
482 hash = hash * 0x81 + (*str++);
486 /* This actually seems to be little better then the real one. Having the same
487 * number of bits but only 5 bits apart seems to produce worse results but
488 * having the bits spanning the same range farther apart also doesn't do as
489 * well. All these differences are fairly small, however. */
492 Old7StringHashFunction(str)
495 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
497 hash = hash * 0x42108421 + (*str++);
501 /* This function tries to provide some non-linearity by providing some feedback
502 * from higher-order bits in the word. It also uses shifts instead of
503 * multiplies, which may be faster on some architectures. */
506 Old8StringHashFunction(str)
509 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
512 hash + (hash << 7) + (hash << 14) + (hash << 21) + (hash << 28) +
513 (hash >> 17) + *str++;
517 /* This is the result of the above search for good hash functions. It seems
518 * that the choice of multipliers is somewhat arbitrary but has several
519 * constraints. It shouldn't have too many or too few one bits and should be
520 * odd. It behaves beeter than the random hash function. */
523 StringHashFunction(str)
526 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
527 /* The multiplicative constant should be odd and have a goodly number of
530 hash = (*str++) + hash * 0x10204081;
545 /* The minimum hash table blocks to allocate. Each block contains 510
546 * buckets. They hash table grows when the number of entries reaches
547 * twice the number of buckets.
551 struct memoryHashTable *mht;
555 switch (ntohl(mht->ht->functionType)) {
556 case HT_dumpIden_FUNCTION:
557 case HT_dumpName_FUNCTION: /* hash table able to handle (befor it grows) ... */
558 retval = 2; /* 1,020 dump entries */
561 case HT_tapeName_FUNCTION:
562 retval = 4; /* 2,040 tape entries */
565 case HT_volName_FUNCTION:
566 retval = 60; /* 61,200 volInfo entries (with different names) */
570 db_panic("Illegal hash function type");
577 struct memoryHashTable *mht;
578 char *e; /* entry's address (in b) */
580 int type = ntohl(mht->ht->functionType);
584 case HT_dumpIden_FUNCTION:
585 retval = IdHashFunction(ntohl(((struct dump *)e)->id));
586 LogDebug(5, "HashEntry: dumpid returns %d\n", retval);
589 case HT_dumpName_FUNCTION:
590 retval = StringHashFunction(((struct dump *)e)->dumpName);
591 LogDebug(5, "HashEntry: dumpname returns %d\n", retval);
594 case HT_tapeName_FUNCTION:
595 retval = StringHashFunction(((struct tape *)e)->name);
596 LogDebug(5, "HashEntry: tapename returns %d\n", retval);
599 case HT_volName_FUNCTION:
600 retval = StringHashFunction(((struct volInfo *)e)->name);
601 LogDebug(5, "HashEntry: volname returns %d\n", retval);
605 db_panic("illegal hash function");
613 * returns a ptr to the memory hash table for the specified hash
617 struct memoryHashTable *
618 ht_GetType(type, e_sizeP)
622 struct memoryHashTable *mht;
624 if ((type <= 0) || (type > HT_MAX_FUNCTION))
628 *e_sizeP = sizeFunctions[type];
630 case HT_dumpIden_FUNCTION:
634 case HT_dumpName_FUNCTION:
638 case HT_tapeName_FUNCTION:
642 case HT_volName_FUNCTION:
649 if (ntohl(mht->ht->functionType) != type)
650 db_panic("ht types don't match");
655 ht_KeyMatch(type, key, e)
661 case HT_dumpIden_FUNCTION:
662 return *(dumpId *) key == ntohl(((struct dump *)e)->id);
663 case HT_dumpName_FUNCTION:
664 return strcmp(key, ((struct dump *)e)->dumpName) == 0;
665 case HT_tapeName_FUNCTION:
666 return strcmp(key, ((struct tape *)e)->name) == 0;
667 case HT_volName_FUNCTION:
668 return strcmp(key, ((struct volInfo *)e)->name) == 0;
671 db_panic("illegal hash function");
679 * ut - ubik transaction
680 * mht - memory hash table ptr
681 * key - hash and lookup key
683 * eaP - dbaddr of entry found or zero if failed
684 * e - contents of located entry
688 ht_LookupEntry(ut, mht, key, eaP, e)
689 struct ubik_trans *ut;
690 struct memoryHashTable *mht;
691 char *key; /* pointer to lookup key to match */
692 dbadr *eaP; /* db addr of entry found or zero */
693 char *e; /* contents of located entry */
695 struct hashTable *ht;
702 if (!key || !eaP || !e)
703 db_panic("null ptrs passed to LookupEntry");
704 if (!(mht && (ht = mht->ht)))
705 db_panic("some ht called with bad mht");
707 *eaP = 0; /* initialize not-found indicator */
709 type = ntohl(ht->functionType);
710 e_size = sizeFunctions[type];
711 if (type == HT_dumpIden_FUNCTION)
712 hash = IdHashFunction(*(dumpId *) key);
714 hash = StringHashFunction(key);
716 for (old = 0;; old++) {
717 a = ht_LookupBucket(ut, mht, hash, old);
719 if (dbread(ut, a, e, e_size))
721 if (ht_KeyMatch(type, key, e)) {
725 a = ntohl(*(dbadr *) (e + mht->threadOffset));
734 * opQuota - max # of items to move
736 * opQuota - adjusted to reflect # of moves
740 ht_HashInList(ut, mht, opQuota, block, blockOffset)
741 struct ubik_trans *ut;
742 struct memoryHashTable *mht;
744 struct memoryHTBlock *block;
747 struct hashTable *ht = mht->ht;
751 char e[sizeof(struct block)]; /* unnecessarily conservative */
752 int e_size = sizeFunctions[ntohl(ht->functionType)];
754 if (mht->length == 0) {
755 if (code = ht_AllocTable(ut, mht)) {
756 Log("ht_HashInList: ht_AllocTable failed\n");
761 listA = ntohl(block->b.bucket[blockOffset]);
764 Log("ht_HashInList: expecting non-zero bucket\n");
768 for (ea = listA; ea; ea = next_ea) { /*f */
770 LogDebug(3, "ht_HashInList: move entry at %d, type %d\n", ea,
771 ntohl(mht->ht->functionType));
773 if (dbread(ut, ea, e, e_size))
776 /* LogNetDump((struct dump *) e); */
778 /* get the address of the next item on the list */
779 next_ea = ntohl(*(dbadr *) (e + mht->threadOffset));
781 /* write the link into the bucket */
783 set_word_addr(ut, block->a, &block->b,
784 &block->b.bucket[blockOffset], htonl(next_ea));
786 Log("ht_HashInList: bucket update failed\n");
791 struct memoryHTBlock *block;
795 /* get the hash value */
796 hash = ht_HashEntry(mht, e) % mht->length;
797 LogDebug(4, "ht_HashInList: moved to %d\n", hash);
799 /* get the new hash table block */
800 code = ht_GetTableBlock(ut, mht, hash, 0 /*old */ , &block, &bo);
802 Log("ht_HashInList: ht_GetTableBlock failed\n");
806 Log("ht_HashInList: ht_GetTableBlock returned 0\n");
807 return BUDB_INTERNALERROR;
810 /* Chain entry at front of bucket;
811 * first threadOffset of entry = bucket
812 * then bucket = addr of entry
815 (ut, ea, e, mht->threadOffset, block->b.bucket[bo])
816 || set_word_addr(ut, block->a, &block->b,
817 &block->b.bucket[bo], htonl(ea)))
821 if (--(*opQuota) == 0)
829 * The hash table is needs to be re-sized. Move entries from the old
834 ht_MoveEntries(ut, mht)
835 struct ubik_trans *ut;
836 struct memoryHashTable *mht;
838 struct memoryHTBlock *block;
844 if (mht->oldLength == 0)
847 LogDebug(3, "ht_MoveEntries:\n");
848 /* we assume here that the hash function will map numbers smaller than the
849 * size of the hash table straight through to hash table indexes.
851 hash = mht->progress;
853 /* get hash table block ? */
854 code = ht_GetTableBlock(ut, mht, hash, 1 /*old */ , &block, &bo);
859 return BUDB_INTERNALERROR;
861 count = 10; /* max. # entries to move */
864 if (block->b.bucket[bo]) {
865 code = ht_HashInList(ut, mht, &count, block, bo);
867 Log("ht_MoveEntries: ht_HashInList failed\n");
872 if (block->b.bucket[bo] == 0) {
873 /* this bucket is now empty */
877 /* don't exceed the quota of items to be moved */
881 } while (++bo < nHTBuckets);
883 if (mht->progress >= mht->oldLength)
884 return (ht_FreeTable(ut, mht));
886 if (set_word_addr(ut, 0, &db.h, &mht->ht->progress, htonl(mht->progress))) {
887 Log("ht_MoveEntries: progress set failed\n");
896 ht_MoveEntries(ut, mht)
897 struct ubik_trans *ut;
898 struct memoryHashTable *mht;
902 struct memoryHTBlock *block;
905 if (mht->oldLength == 0)
908 LogDebug(3, "ht_MoveEntries:\n");
909 /* we assume here that the hash function will map numbers smaller than the
910 * size of the hash table straight through to hash table indexes.
912 hash = mht->progress;
914 /* get hash table block ? */
915 code = ht_GetTableBlock(ut, mht, hash, 1 /*old */ , &block, &bo);
920 return BUDB_INTERNALERROR;
924 if (block->b.bucket[bo]) {
925 code = ht_HashInList(ut, mht, ntohl(block->b.bucket[bo]));
927 Log("ht_MoveEntries: ht_HashInList failed\n");
931 set_word_addr(ut, block->a, &block->b, &block->b.bucket[bo],
934 Log("ht_MoveEntries: clear old entry failed\n");
939 } while (++bo < nHTBuckets);
941 if (mht->progress >= mht->oldLength)
942 return (ht_FreeTable(ut, mht));
944 if (set_word_addr(ut, 0, &db.h, &mht->ht->progress, htonl(mht->progress))) {
945 Log("ht_MoveEntries: progress set failed\n");
953 ht_HashIn(ut, mht, ea, e)
954 struct ubik_trans *ut;
955 struct memoryHashTable *mht;
956 dbadr ea; /* block db address */
957 char *e; /* entry's address (in b) */
959 struct hashTable *ht;
961 struct memoryHTBlock *block;
965 if (!(mht && (ht = mht->ht)))
966 db_panic("some ht called with bad mht");
968 if (code = ht_MaybeAdjust(ut, mht))
970 if (mht->length == 0)
971 if (code = ht_AllocTable(ut, mht))
974 hash = ht_HashEntry(mht, e);
975 code = ht_GetTableBlock(ut, mht, hash, 0 /*old */ , &block, &bo);
979 return BUDB_INTERNALERROR;
981 code = set_word_offset(ut, ea, e, mht->threadOffset, block->b.bucket[bo]);
984 LogDebug(5, "Hashin: set %d to %d\n", mht->threadOffset,
985 block->b.bucket[bo]);
988 set_word_addr(ut, block->a, &block->b, &block->b.bucket[bo],
992 LogDebug(5, "Hashin: set %d to %d\n", &block->b.bucket[bo], htonl(ea));
995 set_word_addr(ut, 0, &db.h, &ht->entries,
996 htonl(ntohl(ht->entries) + 1));
1000 return ht_MoveEntries(ut, mht);
1003 /* RemoveFromList - generic procedure to delete an entry from a list given its
1004 * head and thread offset. Only a single long is modified by this routine.
1005 * The head pointer is modified, in place, using set_word_addr if the entry is
1006 * at the head of the list, otherwise only the thread of the previous entry is
1007 * modified. The entry pointer is only used to calculate the thread offset,
1008 * but is not otherwise used. */
1011 RemoveFromList(ut, ea, e, head, ta, t, thread)
1012 struct ubik_trans *ut;
1013 dbadr ea; /* db addr of head structure */
1014 char *e; /* head structure */
1015 dbadr *head; /* address of head pointer */
1016 dbadr ta; /* db addr of strucure to be removed */
1017 char *t; /* structure being removed */
1018 dbadr *thread; /* pointer to thread pointer */
1021 int threadOffset = ((char *)thread - t);
1022 dbadr next_a; /* db addr of next element in list */
1023 dbadr loop_a; /* db addr of current list element */
1026 return -1; /* empty list: not found */
1027 next_a = ntohl(*head); /* start at head of list */
1028 if (next_a == ta) { /* remove from head of list */
1029 code = set_word_addr(ut, ea, e, head, *thread);
1035 dbread(ut, loop_a + threadOffset, (char *)&next_a, sizeof(dbadr));
1039 return -1; /* end of list: not found */
1040 } while (ta != (next_a = ntohl(next_a)));
1041 code = dbwrite(ut, loop_a + threadOffset, (char *)thread, sizeof(dbadr));
1046 ht_HashOutT(ut, mht, hash, ea, e, old)
1047 struct ubik_trans *ut;
1048 struct memoryHashTable *mht;
1054 struct memoryHTBlock *block;
1058 if ((old ? mht->oldLength : mht->length) == 0)
1060 code = ht_GetTableBlock(ut, mht, hash, old, &block, &bo);
1063 if ((block == 0) || (block->b.bucket[bo] == 0))
1067 RemoveFromList(ut, block->a, (char *)&block->b, &block->b.bucket[bo],
1068 ea, e, (dbadr *) (e + mht->threadOffset));
1073 unthread_ea = *(afs_int32 *) ((char *)e + mht->threadOffset);
1074 if (block->b.bucket[bo] == net_ea) {
1076 (ut, block->a, &block->b, &block->b.bucket[bo], unthread_ea))
1080 loop_a = ntohl(block->b.bucket[bo]);
1083 (ut, loop_a + mht->threadOffset, (char *)&next_loop_a,
1086 if (next_loop_a == 0)
1087 return -1; /* not found */
1088 if (net_ea == next_loop_a) {
1090 (ut, loop_a + mht->threadOffset, (char *)&unthread_ea,
1095 loop_a = ntohl(next_loop_a);
1100 (ut, 0, &db.h, &mht->ht->entries, htonl(ntohl(mht->ht->entries) - 1)))
1106 ht_HashOut(ut, mht, ea, e)
1107 struct ubik_trans *ut;
1108 struct memoryHashTable *mht;
1116 db_panic("some ht called with bad mht");
1117 hash = ht_HashEntry(mht, e);
1118 if (mht->oldLength) {
1119 code = ht_HashOutT(ut, mht, hash, ea, e, 1 /*old */ );
1122 else if (code != -1)
1125 if (mht->length == 0) /* not found */
1126 ERROR(BUDB_INTERNALERROR);
1127 code = ht_HashOutT(ut, mht, hash, ea, e, 0 /*old */ );
1133 code = ht_MoveEntries(ut, mht);
1136 code = ht_MaybeAdjust(ut, mht);
1144 /* generic hash table traversal routines */
1148 scanHashTableBlock(ut, mhtPtr, htBlockPtr, old, length, index, selectFn,
1149 operationFn, rockPtr)
1150 struct ubik_trans *ut;
1151 struct memoryHashTable *mhtPtr;
1152 struct htBlock *htBlockPtr;
1154 afs_int32 length; /* size of whole hash table */
1155 int index; /* base index of this block */
1157 int (*operationFn) ();
1160 int type; /* hash table type */
1161 int entrySize; /* hashed entry size */
1163 afs_uint32 *mapEntryPtr = 0; /* for status checks */
1165 char entry[sizeof(struct block)];
1166 dbadr entryAddr, nextEntryAddr;
1171 type = ntohl(mhtPtr->ht->functionType);
1172 entrySize = sizeFunctions[type];
1174 /* step through this hash table block, being careful to stop
1175 * before the end of the overall hash table
1178 for (i = 0; (i < nHTBuckets) && (index < length); i++, index++) { /*f */
1180 nextEntryAddr = ntohl(htBlockPtr->bucket[i]);
1182 /* if this is the old hash table, all entries below the progress mark
1183 * should have been moved to the new hash table
1185 if (old && (index < mhtPtr->progress) && nextEntryAddr)
1186 return BUDB_INTERNALERROR;
1188 /* now walk down the chain of each bucket */
1189 while (nextEntryAddr) { /*w */
1191 entryAddr = nextEntryAddr;
1192 if (dbread(ut, entryAddr, &entry[0], entrySize))
1193 return (BUDB_INTERNALERROR);
1195 if ((*selectFn) (entryAddr, &entry[0], rockPtr)) {
1196 (*operationFn) (entryAddr, &entry[0], rockPtr);
1200 ntohl(*((dbadr *) (entry + mhtPtr->threadOffset)));
1209 scanHashTable(ut, mhtPtr, selectFn, operationFn, rockPtr)
1210 struct ubik_trans *ut;
1211 struct memoryHashTable *mhtPtr;
1213 int (*operationFn) ();
1216 struct htBlock hashTableBlock;
1217 dbadr tableAddr; /* disk addr of hash block */
1218 int tableLength; /* # entries */
1219 int blockLength; /* # blocks */
1225 extern int nHTBuckets; /* # buckets in a hash table */
1227 for (old = 0; old <= 1; old++) { /*fo */
1229 /* check the old hash table */
1230 tableLength = mhtPtr->oldLength;
1231 if (tableLength == 0)
1232 continue; /* nothing to do */
1234 tableAddr = ntohl(mhtPtr->ht->oldTable);
1236 /* check current hash table */
1237 tableLength = mhtPtr->length;
1238 if (tableLength == 0)
1239 continue; /* nothing to do */
1241 tableAddr = ntohl(mhtPtr->ht->table);
1244 blockLength = (tableLength - 1) / nHTBuckets;
1247 /* follow the hash chain */
1248 for (i = 0; i <= blockLength; i++) { /*fi */
1249 /* chain too short */
1251 ERROR(BUDB_DATABASEINCONSISTENT);
1254 dbread(ut, tableAddr, &hashTableBlock,
1255 sizeof(hashTableBlock));
1260 scanHashTableBlock(ut, mhtPtr, &hashTableBlock, old,
1261 tableLength, hashIndex, selectFn,
1262 operationFn, rockPtr);
1266 hashIndex += nHTBuckets;
1267 tableAddr = ntohl(hashTableBlock.h.next);