hash.c 23 KB

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