hash.c 24 KB

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