hash.c 24 KB

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