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>
22 #include <sys/types.h>
25 #include <afs/bubasics.h>
26 #include "budb_errs.h"
28 #include "error_macros.h"
31 int sizeFunctions[HT_MAX_FUNCTION + 1];
32 int nHTBuckets = NhtBucketS; /* testing: we need small HT blocks */
34 /* ht_TableSize - return the size of table necessary to represent a hashtable
35 * of given length in memory. It basically rounds the length up by the number
36 * of buckets per block. */
45 n = (length + nHTBuckets - 1) / nHTBuckets;
46 return n * sizeof(struct memoryHTBlock *);
49 /* ht_ResetT - resets the in-memory representation of a hashtable block array.
50 * It also resets the global variable nHTBuckets. */
53 ht_ResetT(blocksP, sizeP, length)
54 struct memoryHTBlock ***blocksP;
58 struct memoryHTBlock **b = *blocksP;
63 nHTBuckets = ntohl(db.h.nHTBuckets);
65 n = *sizeP / sizeof(b[0]);
66 newsize = ht_TableSize(length);
67 if (*sizeP != newsize) {
68 /* free all blocks in the old array */
69 for (i = 0; i < n; i++)
76 /* invalidate the blocks of the array */
77 for (i = 0; i < n; i++)
85 * reinitialize a memory hash table.
86 * Calls ht_ResetT to invalidate the two block arrays.
91 struct memoryHashTable *mht;
95 if (!(mht && (ht = mht->ht)))
96 db_panic("some ht called with bad mht");
97 mht->threadOffset = ntohl(ht->threadOffset);
98 mht->length = ntohl(ht->length);
99 mht->oldLength = ntohl(ht->oldLength);
100 mht->progress = ntohl(ht->progress);
101 ht_ResetT(&mht->blocks, &mht->size, mht->length);
102 ht_ResetT(&mht->oldBlocks, &mht->oldSize, mht->oldLength);
105 /* InitDBhash - When server starts, do hash table initialization.
106 test - initialization parameters: bit 4 is small ht. */
111 sizeFunctions[0] = 0;
113 sizeFunctions[HT_dumpIden_FUNCTION] = sizeof(struct dump);
114 sizeFunctions[HT_dumpName_FUNCTION] = sizeof(struct dump);
115 sizeFunctions[HT_volName_FUNCTION] = sizeof(struct volInfo);
116 sizeFunctions[HT_tapeName_FUNCTION] = sizeof(struct tape);
118 db.volName.ht = &db.h.volName;
119 db.tapeName.ht = &db.h.tapeName;
120 db.dumpName.ht = &db.h.dumpName;
121 db.dumpIden.ht = &db.h.dumpIden;
125 /* ht_DBInit - When rebuilding database, this sets up the hash tables. */
130 db.h.nHTBuckets = htonl(nHTBuckets);
133 struct volInfo *s = 0;
134 db.h.volName.threadOffset =
135 htonl((char *)&s->nameHashChain - (char *)s);
136 db.h.volName.functionType = htonl(HT_volName_FUNCTION);
140 db.h.tapeName.threadOffset =
141 htonl((char *)&s->nameHashChain - (char *)s);
142 db.h.tapeName.functionType = htonl(HT_tapeName_FUNCTION);
146 db.h.dumpName.threadOffset =
147 htonl((char *)&s->nameHashChain - (char *)s);
148 db.h.dumpName.functionType = htonl(HT_dumpName_FUNCTION);
150 db.h.dumpIden.threadOffset =
151 htonl((char *)&s->idHashChain - (char *)s);
152 db.h.dumpIden.functionType = htonl(HT_dumpIden_FUNCTION);
154 ht_Reset(&db.volName);
155 ht_Reset(&db.tapeName);
156 ht_Reset(&db.dumpName);
157 ht_Reset(&db.dumpIden);
161 ht_AllocTable(ut, mht)
162 struct ubik_trans *ut;
163 struct memoryHashTable *mht;
165 struct hashTable *ht;
168 int nb, mnb; /* number of blocks for hashTable */
170 struct memoryHTBlock **b;
172 if (!(mht && (ht = mht->ht)))
173 db_panic("some ht called with bad mht");
174 if (ht->length || mht->blocks)
175 db_panic("previous table still allocated");
177 len = ntohl(ht->entries) * 2; /* allow room to grow */
178 nb = (len + nHTBuckets - 1) / nHTBuckets;
179 mnb = ht_minHBlocks(mht);
181 nb = mnb; /* use minimum */
182 len = nb * nHTBuckets; /* new hash table length */
184 mht->size = nb * sizeof(struct memoryHTBlock *);
185 b = mht->blocks = (struct memoryHTBlock **)malloc(mht->size);
186 memset(b, 0, mht->size);
188 for (i = 0; i < nb; i++) {
189 b[i] = (struct memoryHTBlock *)malloc(sizeof(struct memoryHTBlock));
190 code = AllocBlock(ut, (struct block *)&b[i]->b, &b[i]->a);
195 b[i]->b.h.type = hashTable_BLOCK;
197 /* thread the blocks */
199 b[i - 1]->b.h.next = htonl(b[i]->a);
201 for (i = 0; i < nb; i++) {
203 dbwrite(ut, b[i]->a, (char *)&b[i]->b,
204 sizeof(struct htBlock) + (nHTBuckets -
205 NhtBucketS) * sizeof(dbadr));
209 if (code = set_word_addr(ut, 0, &db.h, &ht->table, htonl(b[0]->a)))
212 if (code = set_word_addr(ut, 0, &db.h, &ht->length, htonl(len)))
219 ht_FreeTable(ut, mht)
220 struct ubik_trans *ut;
221 struct memoryHashTable *mht;
223 struct hashTable *ht;
225 struct blockHeader bh;
228 if (!(mht && (ht = mht->ht)))
229 db_panic("some ht called with bad mht");
230 if (ht->oldLength == 0)
231 db_panic("no table to free");
233 ht_ResetT(&mht->oldBlocks, &mht->oldSize, 0);
235 for (a = ntohl(ht->oldTable); a; a = na) {
236 if (dbread(ut, a, (char *)&bh, sizeof(bh))) {
237 Log("ht_FreeTable: dbread failed\n");
241 if (code = FreeBlock(ut, &bh, a))
244 if (set_word_addr(ut, 0, &db.h, &ht->oldTable, 0)
245 || set_word_addr(ut, 0, &db.h, &ht->oldLength, 0)
246 || set_word_addr(ut, 0, &db.h, &ht->progress, 0))
248 mht->oldLength = mht->progress = 0;
253 ht_GetTableBlock(ut, mht, hash, old, blockP, boP)
254 struct ubik_trans *ut;
255 struct memoryHashTable *mht;
258 struct memoryHTBlock **blockP;
261 struct hashTable *ht;
262 struct memoryHTBlock **b;
264 struct memoryHTBlock ***blocksP;
272 || ((ht = mht->ht) == 0)
274 db_panic("some ht called with bad mht");
280 if ((length = mht->oldLength) == 0)
281 return 0; /* no entries */
283 if (hi < mht->progress)
284 return 0; /* no such entry */
285 blocksP = &mht->oldBlocks;
286 sizeP = &mht->oldSize;
288 if ((length = mht->length) == 0)
289 return 0; /* no entries */
291 blocksP = &mht->blocks;
295 bi = hi / nHTBuckets; /* block index */
296 *boP = hi - bi * nHTBuckets; /* block offset ptr */
299 *sizeP = ht_TableSize(length);
300 *blocksP = (struct memoryHTBlock **)malloc(*sizeP);
301 memset(*blocksP, 0, *sizeP);
303 n = *sizeP / sizeof(struct memoryHTBlock *);
305 db_panic("table size inconsistent");
308 /* find an allocated block or the beginning of the block array */
309 for (i = bi; (i > 0) && (b[i] == 0); i--);
313 if (i == 0) { /* the first block is found from the hashTable */
314 ta = ntohl(old ? ht->oldTable : ht->table);
316 db_panic("non-zero length, but no table");
318 /* else ta is set from last time around loop */
320 (struct memoryHTBlock *)malloc(sizeof(struct memoryHTBlock));
326 if (dbread(ut, b[i]->a, (char *)&b[i]->b, sizeof(struct htBlock)))
333 /* printf("ht_GetTableBlock: hash %d block %d offset %d\n",
334 * hash, *blockP, *boP); */
338 ta = ntohl(b[i++]->b.h.next); /* get next ptr from current block */
343 * Decide when to push the current hash table to the old hash table.
344 * The entries in the old hash table are VALID, and are slowly hashed
345 * into the current table.
349 ht_MaybeAdjust(ut, mht)
350 struct ubik_trans *ut;
351 struct memoryHashTable *mht;
353 struct hashTable *ht = mht->ht;
354 int numberEntries = ntohl(ht->entries);
356 /* old hash table must be empty */
357 if (mht->oldLength != 0)
361 * It costs a lot to grow and shrink the hash table. Therefore, we will not
362 * shrink the hash table (only grow it). If the table is more than 2 entries per
363 * chain (average) we need to grow: push the entries to the old hash table.
366 * || ((mht->length > nHTBuckets) && (numberEntries*8 < mht->length))
369 /* Only grow a hash table if the number of entries is twice the
370 * number of hash length and is less than 20,450 (20 hash blocks). This
371 * means that the volname hash table will not grow (its initial
372 * hashtable size contains 30,600 buckets). Earlier revisions of
373 * the buserver have the initial size at 510 and 5,100 buckets -
374 * in which case we do want to grow it). We don't grow anything larger
375 * than 20,450 entries because it's expensive to re-hash everything.
377 if ((numberEntries > mht->length * 2) && (numberEntries < 20450)) { /* push current hash table to old hash table */
378 ht->oldLength = ht->length;
379 ht->oldTable = ht->table;
384 (ut, ((char *)ht - (char *)&db.h), (char *)ht, sizeof(*ht)))
388 LogDebug(2, "ht_MaybeAdjust: push ht to old\n");
394 ht_LookupBucket(ut, mht, hash, old)
395 struct ubik_trans *ut;
396 struct memoryHashTable *mht;
400 struct memoryHTBlock *block;
404 if ((old ? mht->oldLength : mht->length) == 0)
406 code = ht_GetTableBlock(ut, mht, hash, old, &block, &bo);
407 if (code || (block == 0))
409 return ntohl(block->b.bucket[bo]);
412 /* This function is not too bad, for small hash tables, but suffers, I think,
413 * from insufficient mixing of the hash information. */
416 Old2StringHashFunction(str)
419 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
421 hash = (hash << 1) + (hash >> 31) + *str++;
425 /* This was actually a coding error, and produces dreadful results. The
426 * problem is that the hash needs to be mixed up not the incoming character. */
429 Old3StringHashFunction(str)
432 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
434 hash += (*str++) * 0x072a51a4;
438 /* This function is pretty good. Its main problem is that the low two bits of
439 * the hash multiplier are zero which tends to shift information too far left.
440 * It behaves especially badly for hash tables whose size is a power of two. */
443 Old4StringHashFunction(str)
446 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
448 hash = (*str++) + hash * 0x072a51a4;
452 /* While this is good for a hash table with 500 buckets it is nearly as bad as
453 * #3 with a hash table as big as 8200. */
456 Old5StringHashFunction(str)
459 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
465 /* This was an attempt to produce a hash function with the smallest and
466 * simplest mixing multiplier. This is only a little worse than the real one,
467 * and the difference seems to be smaller with larger hash tables. It behaves
468 * better than the random hash function. */
471 Old6StringHashFunction(str)
474 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
476 hash = hash * 0x81 + (*str++);
480 /* This actually seems to be little better then the real one. Having the same
481 * number of bits but only 5 bits apart seems to produce worse results but
482 * having the bits spanning the same range farther apart also doesn't do as
483 * well. All these differences are fairly small, however. */
486 Old7StringHashFunction(str)
489 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
491 hash = hash * 0x42108421 + (*str++);
495 /* This function tries to provide some non-linearity by providing some feedback
496 * from higher-order bits in the word. It also uses shifts instead of
497 * multiplies, which may be faster on some architectures. */
500 Old8StringHashFunction(str)
503 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
506 hash + (hash << 7) + (hash << 14) + (hash << 21) + (hash << 28) +
507 (hash >> 17) + *str++;
511 /* This is the result of the above search for good hash functions. It seems
512 * that the choice of multipliers is somewhat arbitrary but has several
513 * constraints. It shouldn't have too many or too few one bits and should be
514 * odd. It behaves beeter than the random hash function. */
517 StringHashFunction(str)
520 afs_uint32 hash = 1000003; /* big prime to make "" hash nicely */
521 /* The multiplicative constant should be odd and have a goodly number of
524 hash = (*str++) + hash * 0x10204081;
539 /* The minimum hash table blocks to allocate. Each block contains 510
540 * buckets. They hash table grows when the number of entries reaches
541 * twice the number of buckets.
545 struct memoryHashTable *mht;
549 switch (ntohl(mht->ht->functionType)) {
550 case HT_dumpIden_FUNCTION:
551 case HT_dumpName_FUNCTION: /* hash table able to handle (befor it grows) ... */
552 retval = 2; /* 1,020 dump entries */
555 case HT_tapeName_FUNCTION:
556 retval = 4; /* 2,040 tape entries */
559 case HT_volName_FUNCTION:
560 retval = 60; /* 61,200 volInfo entries (with different names) */
564 db_panic("Illegal hash function type");
571 struct memoryHashTable *mht;
572 char *e; /* entry's address (in b) */
574 int type = ntohl(mht->ht->functionType);
578 case HT_dumpIden_FUNCTION:
579 retval = IdHashFunction(ntohl(((struct dump *)e)->id));
580 LogDebug(5, "HashEntry: dumpid returns %d\n", retval);
583 case HT_dumpName_FUNCTION:
584 retval = StringHashFunction(((struct dump *)e)->dumpName);
585 LogDebug(5, "HashEntry: dumpname returns %d\n", retval);
588 case HT_tapeName_FUNCTION:
589 retval = StringHashFunction(((struct tape *)e)->name);
590 LogDebug(5, "HashEntry: tapename returns %d\n", retval);
593 case HT_volName_FUNCTION:
594 retval = StringHashFunction(((struct volInfo *)e)->name);
595 LogDebug(5, "HashEntry: volname returns %d\n", retval);
599 db_panic("illegal hash function");
607 * returns a ptr to the memory hash table for the specified hash
611 struct memoryHashTable *
612 ht_GetType(type, e_sizeP)
616 struct memoryHashTable *mht;
618 if ((type <= 0) || (type > HT_MAX_FUNCTION))
622 *e_sizeP = sizeFunctions[type];
624 case HT_dumpIden_FUNCTION:
628 case HT_dumpName_FUNCTION:
632 case HT_tapeName_FUNCTION:
636 case HT_volName_FUNCTION:
643 if (ntohl(mht->ht->functionType) != type)
644 db_panic("ht types don't match");
649 ht_KeyMatch(type, key, e)
655 case HT_dumpIden_FUNCTION:
656 return *(dumpId *) key == ntohl(((struct dump *)e)->id);
657 case HT_dumpName_FUNCTION:
658 return strcmp(key, ((struct dump *)e)->dumpName) == 0;
659 case HT_tapeName_FUNCTION:
660 return strcmp(key, ((struct tape *)e)->name) == 0;
661 case HT_volName_FUNCTION:
662 return strcmp(key, ((struct volInfo *)e)->name) == 0;
665 db_panic("illegal hash function");
673 * ut - ubik transaction
674 * mht - memory hash table ptr
675 * key - hash and lookup key
677 * eaP - dbaddr of entry found or zero if failed
678 * e - contents of located entry
682 ht_LookupEntry(ut, mht, key, eaP, e)
683 struct ubik_trans *ut;
684 struct memoryHashTable *mht;
685 char *key; /* pointer to lookup key to match */
686 dbadr *eaP; /* db addr of entry found or zero */
687 char *e; /* contents of located entry */
689 struct hashTable *ht;
696 if (!key || !eaP || !e)
697 db_panic("null ptrs passed to LookupEntry");
698 if (!(mht && (ht = mht->ht)))
699 db_panic("some ht called with bad mht");
701 *eaP = 0; /* initialize not-found indicator */
703 type = ntohl(ht->functionType);
704 e_size = sizeFunctions[type];
705 if (type == HT_dumpIden_FUNCTION)
706 hash = IdHashFunction(*(dumpId *) key);
708 hash = StringHashFunction(key);
710 for (old = 0;; old++) {
711 a = ht_LookupBucket(ut, mht, hash, old);
713 if (dbread(ut, a, e, e_size))
715 if (ht_KeyMatch(type, key, e)) {
719 a = ntohl(*(dbadr *) (e + mht->threadOffset));
728 * opQuota - max # of items to move
730 * opQuota - adjusted to reflect # of moves
734 ht_HashInList(ut, mht, opQuota, block, blockOffset)
735 struct ubik_trans *ut;
736 struct memoryHashTable *mht;
738 struct memoryHTBlock *block;
741 struct hashTable *ht = mht->ht;
745 char e[sizeof(struct block)]; /* unnecessarily conservative */
746 int e_size = sizeFunctions[ntohl(ht->functionType)];
748 if (mht->length == 0) {
749 if (code = ht_AllocTable(ut, mht)) {
750 Log("ht_HashInList: ht_AllocTable failed\n");
755 listA = ntohl(block->b.bucket[blockOffset]);
758 Log("ht_HashInList: expecting non-zero bucket\n");
762 for (ea = listA; ea; ea = next_ea) { /*f */
764 LogDebug(3, "ht_HashInList: move entry at %d, type %d\n", ea,
765 ntohl(mht->ht->functionType));
767 if (dbread(ut, ea, e, e_size))
770 /* LogNetDump((struct dump *) e); */
772 /* get the address of the next item on the list */
773 next_ea = ntohl(*(dbadr *) (e + mht->threadOffset));
775 /* write the link into the bucket */
777 set_word_addr(ut, block->a, &block->b,
778 &block->b.bucket[blockOffset], htonl(next_ea));
780 Log("ht_HashInList: bucket update failed\n");
785 struct memoryHTBlock *block;
789 /* get the hash value */
790 hash = ht_HashEntry(mht, e) % mht->length;
791 LogDebug(4, "ht_HashInList: moved to %d\n", hash);
793 /* get the new hash table block */
794 code = ht_GetTableBlock(ut, mht, hash, 0 /*old */ , &block, &bo);
796 Log("ht_HashInList: ht_GetTableBlock failed\n");
800 Log("ht_HashInList: ht_GetTableBlock returned 0\n");
801 return BUDB_INTERNALERROR;
804 /* Chain entry at front of bucket;
805 * first threadOffset of entry = bucket
806 * then bucket = addr of entry
809 (ut, ea, e, mht->threadOffset, block->b.bucket[bo])
810 || set_word_addr(ut, block->a, &block->b,
811 &block->b.bucket[bo], htonl(ea)))
815 if (--(*opQuota) == 0)
823 * The hash table is needs to be re-sized. Move entries from the old
828 ht_MoveEntries(ut, mht)
829 struct ubik_trans *ut;
830 struct memoryHashTable *mht;
832 struct memoryHTBlock *block;
838 if (mht->oldLength == 0)
841 LogDebug(3, "ht_MoveEntries:\n");
842 /* we assume here that the hash function will map numbers smaller than the
843 * size of the hash table straight through to hash table indexes.
845 hash = mht->progress;
847 /* get hash table block ? */
848 code = ht_GetTableBlock(ut, mht, hash, 1 /*old */ , &block, &bo);
853 return BUDB_INTERNALERROR;
855 count = 10; /* max. # entries to move */
858 if (block->b.bucket[bo]) {
859 code = ht_HashInList(ut, mht, &count, block, bo);
861 Log("ht_MoveEntries: ht_HashInList failed\n");
866 if (block->b.bucket[bo] == 0) {
867 /* this bucket is now empty */
871 /* don't exceed the quota of items to be moved */
875 } while (++bo < nHTBuckets);
877 if (mht->progress >= mht->oldLength)
878 return (ht_FreeTable(ut, mht));
880 if (set_word_addr(ut, 0, &db.h, &mht->ht->progress, htonl(mht->progress))) {
881 Log("ht_MoveEntries: progress set failed\n");
890 ht_MoveEntries(ut, mht)
891 struct ubik_trans *ut;
892 struct memoryHashTable *mht;
896 struct memoryHTBlock *block;
899 if (mht->oldLength == 0)
902 LogDebug(3, "ht_MoveEntries:\n");
903 /* we assume here that the hash function will map numbers smaller than the
904 * size of the hash table straight through to hash table indexes.
906 hash = mht->progress;
908 /* get hash table block ? */
909 code = ht_GetTableBlock(ut, mht, hash, 1 /*old */ , &block, &bo);
914 return BUDB_INTERNALERROR;
918 if (block->b.bucket[bo]) {
919 code = ht_HashInList(ut, mht, ntohl(block->b.bucket[bo]));
921 Log("ht_MoveEntries: ht_HashInList failed\n");
925 set_word_addr(ut, block->a, &block->b, &block->b.bucket[bo],
928 Log("ht_MoveEntries: clear old entry failed\n");
933 } while (++bo < nHTBuckets);
935 if (mht->progress >= mht->oldLength)
936 return (ht_FreeTable(ut, mht));
938 if (set_word_addr(ut, 0, &db.h, &mht->ht->progress, htonl(mht->progress))) {
939 Log("ht_MoveEntries: progress set failed\n");
947 ht_HashIn(ut, mht, ea, e)
948 struct ubik_trans *ut;
949 struct memoryHashTable *mht;
950 dbadr ea; /* block db address */
951 char *e; /* entry's address (in b) */
953 struct hashTable *ht;
955 struct memoryHTBlock *block;
959 if (!(mht && (ht = mht->ht)))
960 db_panic("some ht called with bad mht");
962 if (code = ht_MaybeAdjust(ut, mht))
964 if (mht->length == 0)
965 if (code = ht_AllocTable(ut, mht))
968 hash = ht_HashEntry(mht, e);
969 code = ht_GetTableBlock(ut, mht, hash, 0 /*old */ , &block, &bo);
973 return BUDB_INTERNALERROR;
975 code = set_word_offset(ut, ea, e, mht->threadOffset, block->b.bucket[bo]);
978 LogDebug(5, "Hashin: set %d to %d\n", mht->threadOffset,
979 block->b.bucket[bo]);
982 set_word_addr(ut, block->a, &block->b, &block->b.bucket[bo],
986 LogDebug(5, "Hashin: set %d to %d\n", &block->b.bucket[bo], htonl(ea));
989 set_word_addr(ut, 0, &db.h, &ht->entries,
990 htonl(ntohl(ht->entries) + 1));
994 return ht_MoveEntries(ut, mht);
997 /* RemoveFromList - generic procedure to delete an entry from a list given its
998 * head and thread offset. Only a single long is modified by this routine.
999 * The head pointer is modified, in place, using set_word_addr if the entry is
1000 * at the head of the list, otherwise only the thread of the previous entry is
1001 * modified. The entry pointer is only used to calculate the thread offset,
1002 * but is not otherwise used. */
1005 RemoveFromList(ut, ea, e, head, ta, t, thread)
1006 struct ubik_trans *ut;
1007 dbadr ea; /* db addr of head structure */
1008 char *e; /* head structure */
1009 dbadr *head; /* address of head pointer */
1010 dbadr ta; /* db addr of strucure to be removed */
1011 char *t; /* structure being removed */
1012 dbadr *thread; /* pointer to thread pointer */
1015 int threadOffset = ((char *)thread - t);
1016 dbadr next_a; /* db addr of next element in list */
1017 dbadr loop_a; /* db addr of current list element */
1020 return -1; /* empty list: not found */
1021 next_a = ntohl(*head); /* start at head of list */
1022 if (next_a == ta) { /* remove from head of list */
1023 code = set_word_addr(ut, ea, e, head, *thread);
1029 dbread(ut, loop_a + threadOffset, (char *)&next_a, sizeof(dbadr));
1033 return -1; /* end of list: not found */
1034 } while (ta != (next_a = ntohl(next_a)));
1035 code = dbwrite(ut, loop_a + threadOffset, (char *)thread, sizeof(dbadr));
1040 ht_HashOutT(ut, mht, hash, ea, e, old)
1041 struct ubik_trans *ut;
1042 struct memoryHashTable *mht;
1048 struct memoryHTBlock *block;
1052 if ((old ? mht->oldLength : mht->length) == 0)
1054 code = ht_GetTableBlock(ut, mht, hash, old, &block, &bo);
1057 if ((block == 0) || (block->b.bucket[bo] == 0))
1061 RemoveFromList(ut, block->a, (char *)&block->b, &block->b.bucket[bo],
1062 ea, e, (dbadr *) (e + mht->threadOffset));
1067 unthread_ea = *(afs_int32 *) ((char *)e + mht->threadOffset);
1068 if (block->b.bucket[bo] == net_ea) {
1070 (ut, block->a, &block->b, &block->b.bucket[bo], unthread_ea))
1074 loop_a = ntohl(block->b.bucket[bo]);
1077 (ut, loop_a + mht->threadOffset, (char *)&next_loop_a,
1080 if (next_loop_a == 0)
1081 return -1; /* not found */
1082 if (net_ea == next_loop_a) {
1084 (ut, loop_a + mht->threadOffset, (char *)&unthread_ea,
1089 loop_a = ntohl(next_loop_a);
1094 (ut, 0, &db.h, &mht->ht->entries, htonl(ntohl(mht->ht->entries) - 1)))
1100 ht_HashOut(ut, mht, ea, e)
1101 struct ubik_trans *ut;
1102 struct memoryHashTable *mht;
1110 db_panic("some ht called with bad mht");
1111 hash = ht_HashEntry(mht, e);
1112 if (mht->oldLength) {
1113 code = ht_HashOutT(ut, mht, hash, ea, e, 1 /*old */ );
1116 else if (code != -1)
1119 if (mht->length == 0) /* not found */
1120 ERROR(BUDB_INTERNALERROR);
1121 code = ht_HashOutT(ut, mht, hash, ea, e, 0 /*old */ );
1127 code = ht_MoveEntries(ut, mht);
1130 code = ht_MaybeAdjust(ut, mht);
1138 /* generic hash table traversal routines */
1142 scanHashTableBlock(ut, mhtPtr, htBlockPtr, old, length, index, selectFn,
1143 operationFn, rockPtr)
1144 struct ubik_trans *ut;
1145 struct memoryHashTable *mhtPtr;
1146 struct htBlock *htBlockPtr;
1148 afs_int32 length; /* size of whole hash table */
1149 int index; /* base index of this block */
1151 int (*operationFn) ();
1154 int type; /* hash table type */
1155 int entrySize; /* hashed entry size */
1157 afs_uint32 *mapEntryPtr = 0; /* for status checks */
1159 char entry[sizeof(struct block)];
1160 dbadr entryAddr, nextEntryAddr;
1165 type = ntohl(mhtPtr->ht->functionType);
1166 entrySize = sizeFunctions[type];
1168 /* step through this hash table block, being careful to stop
1169 * before the end of the overall hash table
1172 for (i = 0; (i < nHTBuckets) && (index < length); i++, index++) { /*f */
1174 nextEntryAddr = ntohl(htBlockPtr->bucket[i]);
1176 /* if this is the old hash table, all entries below the progress mark
1177 * should have been moved to the new hash table
1179 if (old && (index < mhtPtr->progress) && nextEntryAddr)
1180 return BUDB_INTERNALERROR;
1182 /* now walk down the chain of each bucket */
1183 while (nextEntryAddr) { /*w */
1185 entryAddr = nextEntryAddr;
1186 if (dbread(ut, entryAddr, &entry[0], entrySize))
1187 return (BUDB_INTERNALERROR);
1189 if ((*selectFn) (entryAddr, &entry[0], rockPtr)) {
1190 (*operationFn) (entryAddr, &entry[0], rockPtr);
1194 ntohl(*((dbadr *) (entry + mhtPtr->threadOffset)));
1203 scanHashTable(ut, mhtPtr, selectFn, operationFn, rockPtr)
1204 struct ubik_trans *ut;
1205 struct memoryHashTable *mhtPtr;
1207 int (*operationFn) ();
1210 struct htBlock hashTableBlock;
1211 dbadr tableAddr; /* disk addr of hash block */
1212 int tableLength; /* # entries */
1213 int blockLength; /* # blocks */
1219 extern int nHTBuckets; /* # buckets in a hash table */
1221 for (old = 0; old <= 1; old++) { /*fo */
1223 /* check the old hash table */
1224 tableLength = mhtPtr->oldLength;
1225 if (tableLength == 0)
1226 continue; /* nothing to do */
1228 tableAddr = ntohl(mhtPtr->ht->oldTable);
1230 /* check current hash table */
1231 tableLength = mhtPtr->length;
1232 if (tableLength == 0)
1233 continue; /* nothing to do */
1235 tableAddr = ntohl(mhtPtr->ht->table);
1238 blockLength = (tableLength - 1) / nHTBuckets;
1241 /* follow the hash chain */
1242 for (i = 0; i <= blockLength; i++) { /*fi */
1243 /* chain too short */
1245 ERROR(BUDB_DATABASEINCONSISTENT);
1248 dbread(ut, tableAddr, &hashTableBlock,
1249 sizeof(hashTableBlock));
1254 scanHashTableBlock(ut, mhtPtr, &hashTableBlock, old,
1255 tableLength, hashIndex, selectFn,
1256 operationFn, rockPtr);
1260 hashIndex += nHTBuckets;
1261 tableAddr = ntohl(hashTableBlock.h.next);