hash.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. Trivial Database 2: hash handling
  3. Copyright (C) Rusty Russell 2010
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "private.h"
  16. #include <assert.h>
  17. #include <ccan/hash/hash.h>
  18. static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
  19. void *arg)
  20. {
  21. uint64_t ret;
  22. /* hash64_stable assumes lower bits are more important; they are a
  23. * slightly better hash. We use the upper bits first, so swap them. */
  24. ret = hash64_stable((const unsigned char *)key, length, seed);
  25. return (ret >> 32) | (ret << 32);
  26. }
  27. void tdb_hash_init(struct tdb_context *tdb)
  28. {
  29. tdb->khash = jenkins_hash;
  30. tdb->hash_priv = NULL;
  31. }
  32. uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len)
  33. {
  34. return tdb->khash(ptr, len, tdb->hash_seed, tdb->hash_priv);
  35. }
  36. uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off)
  37. {
  38. const struct tdb_used_record *r;
  39. const void *key;
  40. uint64_t klen, hash;
  41. r = tdb_access_read(tdb, off, sizeof(*r), true);
  42. if (!r)
  43. /* FIXME */
  44. return 0;
  45. klen = rec_key_length(r);
  46. tdb_access_release(tdb, r);
  47. key = tdb_access_read(tdb, off + sizeof(*r), klen, false);
  48. if (!key)
  49. return 0;
  50. hash = tdb_hash(tdb, key, klen);
  51. tdb_access_release(tdb, key);
  52. return hash;
  53. }
  54. /* Get bits from a value. */
  55. static uint32_t bits(uint64_t val, unsigned start, unsigned num)
  56. {
  57. assert(num <= 32);
  58. return (val >> start) & ((1U << num) - 1);
  59. }
  60. /* We take bits from the top: that way we can lock whole sections of the hash
  61. * by using lock ranges. */
  62. static uint32_t use_bits(struct hash_info *h, unsigned num)
  63. {
  64. h->hash_used += num;
  65. return bits(h->h, 64 - h->hash_used, num);
  66. }
  67. static bool key_matches(struct tdb_context *tdb,
  68. const struct tdb_used_record *rec,
  69. tdb_off_t off,
  70. const struct tdb_data *key)
  71. {
  72. bool ret = false;
  73. const char *rkey;
  74. if (rec_key_length(rec) != key->dsize) {
  75. add_stat(tdb, compare_wrong_keylen, 1);
  76. return ret;
  77. }
  78. rkey = tdb_access_read(tdb, off + sizeof(*rec), key->dsize, false);
  79. if (!rkey)
  80. return ret;
  81. if (memcmp(rkey, key->dptr, key->dsize) == 0)
  82. ret = true;
  83. else
  84. add_stat(tdb, compare_wrong_keycmp, 1);
  85. tdb_access_release(tdb, rkey);
  86. return ret;
  87. }
  88. /* Does entry match? */
  89. static bool match(struct tdb_context *tdb,
  90. struct hash_info *h,
  91. const struct tdb_data *key,
  92. tdb_off_t val,
  93. struct tdb_used_record *rec)
  94. {
  95. tdb_off_t off;
  96. add_stat(tdb, compares, 1);
  97. /* Desired bucket must match. */
  98. if (h->home_bucket != (val & TDB_OFF_HASH_GROUP_MASK)) {
  99. add_stat(tdb, compare_wrong_bucket, 1);
  100. return false;
  101. }
  102. /* Top bits of offset == next bits of hash. */
  103. if (bits(val, TDB_OFF_HASH_EXTRA_BIT, TDB_OFF_UPPER_STEAL_EXTRA)
  104. != bits(h->h, 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
  105. TDB_OFF_UPPER_STEAL_EXTRA)) {
  106. add_stat(tdb, compare_wrong_offsetbits, 1);
  107. return false;
  108. }
  109. off = val & TDB_OFF_MASK;
  110. if (tdb_read_convert(tdb, off, rec, sizeof(*rec)) == -1)
  111. return false;
  112. if ((h->h & ((1 << 11)-1)) != rec_hash(rec)) {
  113. add_stat(tdb, compare_wrong_rechash, 1);
  114. return false;
  115. }
  116. return key_matches(tdb, rec, off, key);
  117. }
  118. static tdb_off_t hbucket_off(tdb_off_t group_start, unsigned bucket)
  119. {
  120. return group_start
  121. + (bucket % (1 << TDB_HASH_GROUP_BITS)) * sizeof(tdb_off_t);
  122. }
  123. bool is_subhash(tdb_off_t val)
  124. {
  125. return (val >> TDB_OFF_UPPER_STEAL_SUBHASH_BIT) & 1;
  126. }
  127. /* FIXME: Guess the depth, don't over-lock! */
  128. static tdb_off_t hlock_range(tdb_off_t group, tdb_off_t *size)
  129. {
  130. *size = 1ULL << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
  131. return group << (64 - (TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS));
  132. }
  133. static tdb_off_t COLD find_in_chain(struct tdb_context *tdb,
  134. struct tdb_data key,
  135. tdb_off_t chain,
  136. struct hash_info *h,
  137. struct tdb_used_record *rec,
  138. struct traverse_info *tinfo)
  139. {
  140. tdb_off_t off, next;
  141. /* In case nothing is free, we set these to zero. */
  142. h->home_bucket = h->found_bucket = 0;
  143. for (off = chain; off; off = next) {
  144. unsigned int i;
  145. h->group_start = off;
  146. if (tdb_read_convert(tdb, off, h->group, sizeof(h->group)))
  147. return TDB_OFF_ERR;
  148. for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
  149. tdb_off_t recoff;
  150. if (!h->group[i]) {
  151. /* Remember this empty bucket. */
  152. h->home_bucket = h->found_bucket = i;
  153. continue;
  154. }
  155. /* We can insert extra bits via add_to_hash
  156. * empty bucket logic. */
  157. recoff = h->group[i] & TDB_OFF_MASK;
  158. if (tdb_read_convert(tdb, recoff, rec, sizeof(*rec)))
  159. return TDB_OFF_ERR;
  160. if (key_matches(tdb, rec, recoff, &key)) {
  161. h->home_bucket = h->found_bucket = i;
  162. if (tinfo) {
  163. tinfo->levels[tinfo->num_levels]
  164. .hashtable = off;
  165. tinfo->levels[tinfo->num_levels]
  166. .total_buckets
  167. = 1 << TDB_HASH_GROUP_BITS;
  168. tinfo->levels[tinfo->num_levels].entry
  169. = i;
  170. tinfo->num_levels++;
  171. }
  172. return recoff;
  173. }
  174. }
  175. next = tdb_read_off(tdb, off
  176. + offsetof(struct tdb_chain, next));
  177. if (next == TDB_OFF_ERR)
  178. return TDB_OFF_ERR;
  179. if (next)
  180. next += sizeof(struct tdb_used_record);
  181. }
  182. return 0;
  183. }
  184. /* This is the core routine which searches the hashtable for an entry.
  185. * On error, no locks are held and TDB_OFF_ERR is returned.
  186. * Otherwise, hinfo is filled in (and the optional tinfo).
  187. * If not found, the return value is 0.
  188. * If found, the return value is the offset, and *rec is the record. */
  189. tdb_off_t find_and_lock(struct tdb_context *tdb,
  190. struct tdb_data key,
  191. int ltype,
  192. struct hash_info *h,
  193. struct tdb_used_record *rec,
  194. struct traverse_info *tinfo)
  195. {
  196. uint32_t i, group;
  197. tdb_off_t hashtable;
  198. h->h = tdb_hash(tdb, key.dptr, key.dsize);
  199. h->hash_used = 0;
  200. group = use_bits(h, TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
  201. h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
  202. h->hlock_start = hlock_range(group, &h->hlock_range);
  203. if (tdb_lock_hashes(tdb, h->hlock_start, h->hlock_range, ltype,
  204. TDB_LOCK_WAIT))
  205. return TDB_OFF_ERR;
  206. hashtable = offsetof(struct tdb_header, hashtable);
  207. if (tinfo) {
  208. tinfo->toplevel_group = group;
  209. tinfo->num_levels = 1;
  210. tinfo->levels[0].entry = 0;
  211. tinfo->levels[0].hashtable = hashtable
  212. + (group << TDB_HASH_GROUP_BITS) * sizeof(tdb_off_t);
  213. tinfo->levels[0].total_buckets = 1 << TDB_HASH_GROUP_BITS;
  214. }
  215. while (h->hash_used <= 64) {
  216. /* Read in the hash group. */
  217. h->group_start = hashtable
  218. + group * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
  219. if (tdb_read_convert(tdb, h->group_start, &h->group,
  220. sizeof(h->group)) == -1)
  221. goto fail;
  222. /* Pointer to another hash table? Go down... */
  223. if (is_subhash(h->group[h->home_bucket])) {
  224. hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
  225. + sizeof(struct tdb_used_record);
  226. if (tinfo) {
  227. /* When we come back, use *next* bucket */
  228. tinfo->levels[tinfo->num_levels-1].entry
  229. += h->home_bucket + 1;
  230. }
  231. group = use_bits(h, TDB_SUBLEVEL_HASH_BITS
  232. - TDB_HASH_GROUP_BITS);
  233. h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
  234. if (tinfo) {
  235. tinfo->levels[tinfo->num_levels].hashtable
  236. = hashtable;
  237. tinfo->levels[tinfo->num_levels].total_buckets
  238. = 1 << TDB_SUBLEVEL_HASH_BITS;
  239. tinfo->levels[tinfo->num_levels].entry
  240. = group << TDB_HASH_GROUP_BITS;
  241. tinfo->num_levels++;
  242. }
  243. continue;
  244. }
  245. /* It's in this group: search (until 0 or all searched) */
  246. for (i = 0, h->found_bucket = h->home_bucket;
  247. i < (1 << TDB_HASH_GROUP_BITS);
  248. i++, h->found_bucket = ((h->found_bucket+1)
  249. % (1 << TDB_HASH_GROUP_BITS))) {
  250. if (is_subhash(h->group[h->found_bucket]))
  251. continue;
  252. if (!h->group[h->found_bucket])
  253. break;
  254. if (match(tdb, h, &key, h->group[h->found_bucket],
  255. rec)) {
  256. if (tinfo) {
  257. tinfo->levels[tinfo->num_levels-1].entry
  258. += h->found_bucket;
  259. }
  260. return h->group[h->found_bucket] & TDB_OFF_MASK;
  261. }
  262. }
  263. /* Didn't find it: h indicates where it would go. */
  264. return 0;
  265. }
  266. return find_in_chain(tdb, key, hashtable, h, rec, tinfo);
  267. fail:
  268. tdb_unlock_hashes(tdb, h->hlock_start, h->hlock_range, ltype);
  269. return TDB_OFF_ERR;
  270. }
  271. /* I wrote a simple test, expanding a hash to 2GB, for the following
  272. * cases:
  273. * 1) Expanding all the buckets at once,
  274. * 2) Expanding the bucket we wanted to place the new entry into.
  275. * 3) Expanding the most-populated bucket,
  276. *
  277. * I measured the worst/average/best density during this process.
  278. * 1) 3%/16%/30%
  279. * 2) 4%/20%/38%
  280. * 3) 6%/22%/41%
  281. *
  282. * So we figure out the busiest bucket for the moment.
  283. */
  284. static unsigned fullest_bucket(struct tdb_context *tdb,
  285. const tdb_off_t *group,
  286. unsigned new_bucket)
  287. {
  288. unsigned counts[1 << TDB_HASH_GROUP_BITS] = { 0 };
  289. unsigned int i, best_bucket;
  290. /* Count the new entry. */
  291. counts[new_bucket]++;
  292. best_bucket = new_bucket;
  293. for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
  294. unsigned this_bucket;
  295. if (is_subhash(group[i]))
  296. continue;
  297. this_bucket = group[i] & TDB_OFF_HASH_GROUP_MASK;
  298. if (++counts[this_bucket] > counts[best_bucket])
  299. best_bucket = this_bucket;
  300. }
  301. return best_bucket;
  302. }
  303. static bool put_into_group(tdb_off_t *group,
  304. unsigned bucket, tdb_off_t encoded)
  305. {
  306. unsigned int i;
  307. for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
  308. unsigned b = (bucket + i) % (1 << TDB_HASH_GROUP_BITS);
  309. if (group[b] == 0) {
  310. group[b] = encoded;
  311. return true;
  312. }
  313. }
  314. return false;
  315. }
  316. static void force_into_group(tdb_off_t *group,
  317. unsigned bucket, tdb_off_t encoded)
  318. {
  319. if (!put_into_group(group, bucket, encoded))
  320. abort();
  321. }
  322. static tdb_off_t encode_offset(tdb_off_t new_off, struct hash_info *h)
  323. {
  324. return h->home_bucket
  325. | new_off
  326. | ((uint64_t)bits(h->h,
  327. 64 - h->hash_used - TDB_OFF_UPPER_STEAL_EXTRA,
  328. TDB_OFF_UPPER_STEAL_EXTRA)
  329. << TDB_OFF_HASH_EXTRA_BIT);
  330. }
  331. /* Simply overwrite the hash entry we found before. */
  332. int replace_in_hash(struct tdb_context *tdb,
  333. struct hash_info *h,
  334. tdb_off_t new_off)
  335. {
  336. return tdb_write_off(tdb, hbucket_off(h->group_start, h->found_bucket),
  337. encode_offset(new_off, h));
  338. }
  339. /* We slot in anywhere that's empty in the chain. */
  340. static int COLD add_to_chain(struct tdb_context *tdb,
  341. tdb_off_t subhash,
  342. tdb_off_t new_off)
  343. {
  344. size_t entry = tdb_find_zero_off(tdb, subhash, 1<<TDB_HASH_GROUP_BITS);
  345. if (entry == 1 << TDB_HASH_GROUP_BITS) {
  346. tdb_off_t next;
  347. next = tdb_read_off(tdb, subhash
  348. + offsetof(struct tdb_chain, next));
  349. if (next == TDB_OFF_ERR)
  350. return -1;
  351. if (!next) {
  352. next = alloc(tdb, 0, sizeof(struct tdb_chain), 0,
  353. TDB_CHAIN_MAGIC, false);
  354. if (next == TDB_OFF_ERR)
  355. return -1;
  356. if (zero_out(tdb, next+sizeof(struct tdb_used_record),
  357. sizeof(struct tdb_chain)))
  358. return -1;
  359. if (tdb_write_off(tdb, subhash
  360. + offsetof(struct tdb_chain, next),
  361. next) != 0)
  362. return -1;
  363. }
  364. return add_to_chain(tdb, next, new_off);
  365. }
  366. return tdb_write_off(tdb, subhash + entry * sizeof(tdb_off_t),
  367. new_off);
  368. }
  369. /* Add into a newly created subhash. */
  370. static int add_to_subhash(struct tdb_context *tdb, tdb_off_t subhash,
  371. unsigned hash_used, tdb_off_t val)
  372. {
  373. tdb_off_t off = (val & TDB_OFF_MASK), *group;
  374. struct hash_info h;
  375. unsigned int gnum;
  376. h.hash_used = hash_used;
  377. if (hash_used + TDB_SUBLEVEL_HASH_BITS > 64)
  378. return add_to_chain(tdb, subhash, off);
  379. h.h = hash_record(tdb, off);
  380. gnum = use_bits(&h, TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS);
  381. h.group_start = subhash
  382. + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
  383. h.home_bucket = use_bits(&h, TDB_HASH_GROUP_BITS);
  384. group = tdb_access_write(tdb, h.group_start,
  385. sizeof(*group) << TDB_HASH_GROUP_BITS, true);
  386. if (!group)
  387. return -1;
  388. force_into_group(group, h.home_bucket, encode_offset(off, &h));
  389. return tdb_access_commit(tdb, group);
  390. }
  391. static int expand_group(struct tdb_context *tdb, struct hash_info *h)
  392. {
  393. unsigned bucket, num_vals, i, magic;
  394. size_t subsize;
  395. tdb_off_t subhash;
  396. tdb_off_t vals[1 << TDB_HASH_GROUP_BITS];
  397. /* Attach new empty subhash under fullest bucket. */
  398. bucket = fullest_bucket(tdb, h->group, h->home_bucket);
  399. if (h->hash_used == 64) {
  400. add_stat(tdb, alloc_chain, 1);
  401. subsize = sizeof(struct tdb_chain);
  402. magic = TDB_CHAIN_MAGIC;
  403. } else {
  404. add_stat(tdb, alloc_subhash, 1);
  405. subsize = (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS);
  406. magic = TDB_HTABLE_MAGIC;
  407. }
  408. subhash = alloc(tdb, 0, subsize, 0, magic, false);
  409. if (subhash == TDB_OFF_ERR)
  410. return -1;
  411. if (zero_out(tdb, subhash + sizeof(struct tdb_used_record), subsize))
  412. return -1;
  413. /* Remove any which are destined for bucket or are in wrong place. */
  414. num_vals = 0;
  415. for (i = 0; i < (1 << TDB_HASH_GROUP_BITS); i++) {
  416. unsigned home_bucket = h->group[i] & TDB_OFF_HASH_GROUP_MASK;
  417. if (!h->group[i] || is_subhash(h->group[i]))
  418. continue;
  419. if (home_bucket == bucket || home_bucket != i) {
  420. vals[num_vals++] = h->group[i];
  421. h->group[i] = 0;
  422. }
  423. }
  424. /* FIXME: This assert is valid, but we do this during unit test :( */
  425. /* assert(num_vals); */
  426. /* Overwrite expanded bucket with subhash pointer. */
  427. h->group[bucket] = subhash | (1ULL << TDB_OFF_UPPER_STEAL_SUBHASH_BIT);
  428. /* Point to actual contents of record. */
  429. subhash += sizeof(struct tdb_used_record);
  430. /* Put values back. */
  431. for (i = 0; i < num_vals; i++) {
  432. unsigned this_bucket = vals[i] & TDB_OFF_HASH_GROUP_MASK;
  433. if (this_bucket == bucket) {
  434. if (add_to_subhash(tdb, subhash, h->hash_used, vals[i]))
  435. return -1;
  436. } else {
  437. /* There should be room to put this back. */
  438. force_into_group(h->group, this_bucket, vals[i]);
  439. }
  440. }
  441. return 0;
  442. }
  443. int delete_from_hash(struct tdb_context *tdb, struct hash_info *h)
  444. {
  445. unsigned int i, num_movers = 0;
  446. tdb_off_t movers[1 << TDB_HASH_GROUP_BITS];
  447. h->group[h->found_bucket] = 0;
  448. for (i = 1; i < (1 << TDB_HASH_GROUP_BITS); i++) {
  449. unsigned this_bucket;
  450. this_bucket = (h->found_bucket+i) % (1 << TDB_HASH_GROUP_BITS);
  451. /* Empty bucket? We're done. */
  452. if (!h->group[this_bucket])
  453. break;
  454. /* Ignore subhashes. */
  455. if (is_subhash(h->group[this_bucket]))
  456. continue;
  457. /* If this one is not happy where it is, we'll move it. */
  458. if ((h->group[this_bucket] & TDB_OFF_HASH_GROUP_MASK)
  459. != this_bucket) {
  460. movers[num_movers++] = h->group[this_bucket];
  461. h->group[this_bucket] = 0;
  462. }
  463. }
  464. /* Put back the ones we erased. */
  465. for (i = 0; i < num_movers; i++) {
  466. force_into_group(h->group, movers[i] & TDB_OFF_HASH_GROUP_MASK,
  467. movers[i]);
  468. }
  469. /* Now we write back the hash group */
  470. return tdb_write_convert(tdb, h->group_start,
  471. h->group, sizeof(h->group));
  472. }
  473. int add_to_hash(struct tdb_context *tdb, struct hash_info *h, tdb_off_t new_off)
  474. {
  475. /* We hit an empty bucket during search? That's where it goes. */
  476. if (!h->group[h->found_bucket]) {
  477. h->group[h->found_bucket] = encode_offset(new_off, h);
  478. /* Write back the modified group. */
  479. return tdb_write_convert(tdb, h->group_start,
  480. h->group, sizeof(h->group));
  481. }
  482. if (h->hash_used > 64)
  483. return add_to_chain(tdb, h->group_start, new_off);
  484. /* We're full. Expand. */
  485. if (expand_group(tdb, h) == -1)
  486. return -1;
  487. if (is_subhash(h->group[h->home_bucket])) {
  488. /* We were expanded! */
  489. tdb_off_t hashtable;
  490. unsigned int gnum;
  491. /* Write back the modified group. */
  492. if (tdb_write_convert(tdb, h->group_start, h->group,
  493. sizeof(h->group)))
  494. return -1;
  495. /* Move hashinfo down a level. */
  496. hashtable = (h->group[h->home_bucket] & TDB_OFF_MASK)
  497. + sizeof(struct tdb_used_record);
  498. gnum = use_bits(h,TDB_SUBLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS);
  499. h->home_bucket = use_bits(h, TDB_HASH_GROUP_BITS);
  500. h->group_start = hashtable
  501. + gnum * (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
  502. if (tdb_read_convert(tdb, h->group_start, &h->group,
  503. sizeof(h->group)) == -1)
  504. return -1;
  505. }
  506. /* Expanding the group must have made room if it didn't choose this
  507. * bucket. */
  508. if (put_into_group(h->group, h->home_bucket, encode_offset(new_off, h)))
  509. return tdb_write_convert(tdb, h->group_start,
  510. h->group, sizeof(h->group));
  511. /* This can happen if all hashes in group (and us) dropped into same
  512. * group in subhash. */
  513. return add_to_hash(tdb, h, new_off);
  514. }
  515. /* Traverse support: returns offset of record, or 0 or TDB_OFF_ERR. */
  516. static tdb_off_t iterate_hash(struct tdb_context *tdb,
  517. struct traverse_info *tinfo)
  518. {
  519. tdb_off_t off, val;
  520. unsigned int i;
  521. struct traverse_level *tlevel;
  522. tlevel = &tinfo->levels[tinfo->num_levels-1];
  523. again:
  524. for (i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
  525. tlevel->entry, tlevel->total_buckets);
  526. i != tlevel->total_buckets;
  527. i = tdb_find_nonzero_off(tdb, tlevel->hashtable,
  528. i+1, tlevel->total_buckets)) {
  529. val = tdb_read_off(tdb, tlevel->hashtable+sizeof(tdb_off_t)*i);
  530. if (unlikely(val == TDB_OFF_ERR))
  531. return TDB_OFF_ERR;
  532. off = val & TDB_OFF_MASK;
  533. /* This makes the delete-all-in-traverse case work
  534. * (and simplifies our logic a little). */
  535. if (off == tinfo->prev)
  536. continue;
  537. tlevel->entry = i;
  538. if (!is_subhash(val)) {
  539. /* Found one. */
  540. tinfo->prev = off;
  541. return off;
  542. }
  543. /* When we come back, we want the next one */
  544. tlevel->entry++;
  545. tinfo->num_levels++;
  546. tlevel++;
  547. tlevel->hashtable = off + sizeof(struct tdb_used_record);
  548. tlevel->entry = 0;
  549. /* Next level is a chain? */
  550. if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1))
  551. tlevel->total_buckets = (1 << TDB_HASH_GROUP_BITS);
  552. else
  553. tlevel->total_buckets = (1 << TDB_SUBLEVEL_HASH_BITS);
  554. goto again;
  555. }
  556. /* Nothing there? */
  557. if (tinfo->num_levels == 1)
  558. return 0;
  559. /* Handle chained entries. */
  560. if (unlikely(tinfo->num_levels == TDB_MAX_LEVELS + 1)) {
  561. tlevel->hashtable = tdb_read_off(tdb, tlevel->hashtable
  562. + offsetof(struct tdb_chain,
  563. next));
  564. if (tlevel->hashtable == TDB_OFF_ERR)
  565. return TDB_OFF_ERR;
  566. if (tlevel->hashtable) {
  567. tlevel->hashtable += sizeof(struct tdb_used_record);
  568. tlevel->entry = 0;
  569. goto again;
  570. }
  571. }
  572. /* Go back up and keep searching. */
  573. tinfo->num_levels--;
  574. tlevel--;
  575. goto again;
  576. }
  577. /* Return 1 if we find something, 0 if not, -1 on error. */
  578. int next_in_hash(struct tdb_context *tdb, int ltype,
  579. struct traverse_info *tinfo,
  580. TDB_DATA *kbuf, size_t *dlen)
  581. {
  582. const unsigned group_bits = TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS;
  583. tdb_off_t hlock_start, hlock_range, off;
  584. while (tinfo->toplevel_group < (1 << group_bits)) {
  585. hlock_start = (tdb_off_t)tinfo->toplevel_group
  586. << (64 - group_bits);
  587. hlock_range = 1ULL << group_bits;
  588. if (tdb_lock_hashes(tdb, hlock_start, hlock_range, ltype,
  589. TDB_LOCK_WAIT) != 0)
  590. return -1;
  591. off = iterate_hash(tdb, tinfo);
  592. if (off) {
  593. struct tdb_used_record rec;
  594. if (tdb_read_convert(tdb, off, &rec, sizeof(rec))) {
  595. tdb_unlock_hashes(tdb,
  596. hlock_start, hlock_range,
  597. ltype);
  598. return -1;
  599. }
  600. if (rec_magic(&rec) != TDB_USED_MAGIC) {
  601. tdb_logerr(tdb, TDB_ERR_CORRUPT,
  602. TDB_DEBUG_FATAL,
  603. "next_in_hash:"
  604. " corrupt record at %llu",
  605. (long long)off);
  606. return -1;
  607. }
  608. kbuf->dsize = rec_key_length(&rec);
  609. /* They want data as well? */
  610. if (dlen) {
  611. *dlen = rec_data_length(&rec);
  612. kbuf->dptr = tdb_alloc_read(tdb,
  613. off + sizeof(rec),
  614. kbuf->dsize
  615. + *dlen);
  616. } else {
  617. kbuf->dptr = tdb_alloc_read(tdb,
  618. off + sizeof(rec),
  619. kbuf->dsize);
  620. }
  621. tdb_unlock_hashes(tdb, hlock_start, hlock_range, ltype);
  622. return kbuf->dptr ? 1 : -1;
  623. }
  624. tdb_unlock_hashes(tdb, hlock_start, hlock_range, ltype);
  625. tinfo->toplevel_group++;
  626. tinfo->levels[0].hashtable
  627. += (sizeof(tdb_off_t) << TDB_HASH_GROUP_BITS);
  628. tinfo->levels[0].entry = 0;
  629. }
  630. return 0;
  631. }
  632. /* Return 1 if we find something, 0 if not, -1 on error. */
  633. int first_in_hash(struct tdb_context *tdb, int ltype,
  634. struct traverse_info *tinfo,
  635. TDB_DATA *kbuf, size_t *dlen)
  636. {
  637. tinfo->prev = 0;
  638. tinfo->toplevel_group = 0;
  639. tinfo->num_levels = 1;
  640. tinfo->levels[0].hashtable = offsetof(struct tdb_header, hashtable);
  641. tinfo->levels[0].entry = 0;
  642. tinfo->levels[0].total_buckets = (1 << TDB_HASH_GROUP_BITS);
  643. return next_in_hash(tdb, ltype, tinfo, kbuf, dlen);
  644. }
  645. /* Even if the entry isn't in this hash bucket, you'd have to lock this
  646. * bucket to find it. */
  647. static int chainlock(struct tdb_context *tdb, const TDB_DATA *key,
  648. int ltype, enum tdb_lock_flags waitflag,
  649. const char *func)
  650. {
  651. int ret;
  652. uint64_t h = tdb_hash(tdb, key->dptr, key->dsize);
  653. tdb_off_t lockstart, locksize;
  654. unsigned int group, gbits;
  655. gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
  656. group = bits(h, 64 - gbits, gbits);
  657. lockstart = hlock_range(group, &locksize);
  658. ret = tdb_lock_hashes(tdb, lockstart, locksize, ltype, waitflag);
  659. tdb_trace_1rec(tdb, func, *key);
  660. return ret;
  661. }
  662. /* lock/unlock one hash chain. This is meant to be used to reduce
  663. contention - it cannot guarantee how many records will be locked */
  664. int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
  665. {
  666. return chainlock(tdb, &key, F_WRLCK, TDB_LOCK_WAIT, "tdb_chainlock");
  667. }
  668. int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
  669. {
  670. uint64_t h = tdb_hash(tdb, key.dptr, key.dsize);
  671. tdb_off_t lockstart, locksize;
  672. unsigned int group, gbits;
  673. gbits = TDB_TOPLEVEL_HASH_BITS - TDB_HASH_GROUP_BITS;
  674. group = bits(h, 64 - gbits, gbits);
  675. lockstart = hlock_range(group, &locksize);
  676. tdb_trace_1rec(tdb, "tdb_chainunlock", key);
  677. return tdb_unlock_hashes(tdb, lockstart, locksize, F_WRLCK);
  678. }