hash.c 23 KB

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