btree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * lib/btree.c - Simple In-memory B+Tree
  3. *
  4. * As should be obvious for Linux kernel code, license is GPLv2
  5. *
  6. * Copyright (c) 2007-2008 Joern Engel <joern@logfs.org>
  7. * Bits and pieces stolen from Peter Zijlstra's code, which is
  8. * Copyright 2007, Red Hat Inc. Peter Zijlstra <pzijlstr@redhat.com>
  9. * GPLv2
  10. *
  11. * see http://programming.kicks-ass.net/kernel-patches/vma_lookup/btree.patch
  12. *
  13. * A relatively simple B+Tree implementation. I have written it as a learning
  14. * exercise to understand how B+Trees work. Turned out to be useful as well.
  15. *
  16. * B+Trees can be used similar to Linux radix trees (which don't have anything
  17. * in common with textbook radix trees, beware). Prerequisite for them working
  18. * well is that access to a random tree node is much faster than a large number
  19. * of operations within each node.
  20. *
  21. * Disks have fulfilled the prerequisite for a long time. More recently DRAM
  22. * has gained similar properties, as memory access times, when measured in cpu
  23. * cycles, have increased. Cacheline sizes have increased as well, which also
  24. * helps B+Trees.
  25. *
  26. * Compared to radix trees, B+Trees are more efficient when dealing with a
  27. * sparsely populated address space. Between 25% and 50% of the memory is
  28. * occupied with valid pointers. When densely populated, radix trees contain
  29. * ~98% pointers - hard to beat. Very sparse radix trees contain only ~2%
  30. * pointers.
  31. *
  32. * This particular implementation stores pointers identified by a long value.
  33. * Storing NULL pointers is illegal, lookup will return NULL when no entry
  34. * was found.
  35. *
  36. * A tricks was used that is not commonly found in textbooks. The lowest
  37. * values are to the right, not to the left. All used slots within a node
  38. * are on the left, all unused slots contain NUL values. Most operations
  39. * simply loop once over all slots and terminate on the first NUL.
  40. */
  41. #include <linux/btree.h>
  42. #include <linux/cache.h>
  43. #include <linux/kernel.h>
  44. #include <linux/slab.h>
  45. #include <linux/module.h>
  46. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  47. #define NODESIZE MAX(L1_CACHE_BYTES, 128)
  48. struct btree_geo {
  49. int keylen;
  50. int no_pairs;
  51. int no_longs;
  52. };
  53. struct btree_geo btree_geo32 = {
  54. .keylen = 1,
  55. .no_pairs = NODESIZE / sizeof(long) / 2,
  56. .no_longs = NODESIZE / sizeof(long) / 2,
  57. };
  58. EXPORT_SYMBOL_GPL(btree_geo32);
  59. #define LONG_PER_U64 (64 / BITS_PER_LONG)
  60. struct btree_geo btree_geo64 = {
  61. .keylen = LONG_PER_U64,
  62. .no_pairs = NODESIZE / sizeof(long) / (1 + LONG_PER_U64),
  63. .no_longs = LONG_PER_U64 * (NODESIZE / sizeof(long) / (1 + LONG_PER_U64)),
  64. };
  65. EXPORT_SYMBOL_GPL(btree_geo64);
  66. struct btree_geo btree_geo128 = {
  67. .keylen = 2 * LONG_PER_U64,
  68. .no_pairs = NODESIZE / sizeof(long) / (1 + 2 * LONG_PER_U64),
  69. .no_longs = 2 * LONG_PER_U64 * (NODESIZE / sizeof(long) / (1 + 2 * LONG_PER_U64)),
  70. };
  71. EXPORT_SYMBOL_GPL(btree_geo128);
  72. static struct kmem_cache *btree_cachep;
  73. void *btree_alloc(gfp_t gfp_mask, void *pool_data)
  74. {
  75. return kmem_cache_alloc(btree_cachep, gfp_mask);
  76. }
  77. EXPORT_SYMBOL_GPL(btree_alloc);
  78. void btree_free(void *element, void *pool_data)
  79. {
  80. kmem_cache_free(btree_cachep, element);
  81. }
  82. EXPORT_SYMBOL_GPL(btree_free);
  83. static unsigned long *btree_node_alloc(struct btree_head *head, gfp_t gfp)
  84. {
  85. unsigned long *node;
  86. node = mempool_alloc(head->mempool, gfp);
  87. if (likely(node))
  88. memset(node, 0, NODESIZE);
  89. return node;
  90. }
  91. static int longcmp(const unsigned long *l1, const unsigned long *l2, size_t n)
  92. {
  93. size_t i;
  94. for (i = 0; i < n; i++) {
  95. if (l1[i] < l2[i])
  96. return -1;
  97. if (l1[i] > l2[i])
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. static unsigned long *longcpy(unsigned long *dest, const unsigned long *src,
  103. size_t n)
  104. {
  105. size_t i;
  106. for (i = 0; i < n; i++)
  107. dest[i] = src[i];
  108. return dest;
  109. }
  110. static unsigned long *longset(unsigned long *s, unsigned long c, size_t n)
  111. {
  112. size_t i;
  113. for (i = 0; i < n; i++)
  114. s[i] = c;
  115. return s;
  116. }
  117. static void dec_key(struct btree_geo *geo, unsigned long *key)
  118. {
  119. unsigned long val;
  120. int i;
  121. for (i = geo->keylen - 1; i >= 0; i--) {
  122. val = key[i];
  123. key[i] = val - 1;
  124. if (val)
  125. break;
  126. }
  127. }
  128. static unsigned long *bkey(struct btree_geo *geo, unsigned long *node, int n)
  129. {
  130. return &node[n * geo->keylen];
  131. }
  132. static void *bval(struct btree_geo *geo, unsigned long *node, int n)
  133. {
  134. return (void *)node[geo->no_longs + n];
  135. }
  136. static void setkey(struct btree_geo *geo, unsigned long *node, int n,
  137. unsigned long *key)
  138. {
  139. longcpy(bkey(geo, node, n), key, geo->keylen);
  140. }
  141. static void setval(struct btree_geo *geo, unsigned long *node, int n,
  142. void *val)
  143. {
  144. node[geo->no_longs + n] = (unsigned long) val;
  145. }
  146. static void clearpair(struct btree_geo *geo, unsigned long *node, int n)
  147. {
  148. longset(bkey(geo, node, n), 0, geo->keylen);
  149. node[geo->no_longs + n] = 0;
  150. }
  151. static inline void __btree_init(struct btree_head *head)
  152. {
  153. head->node = NULL;
  154. head->height = 0;
  155. }
  156. void btree_init_mempool(struct btree_head *head, mempool_t *mempool)
  157. {
  158. __btree_init(head);
  159. head->mempool = mempool;
  160. }
  161. EXPORT_SYMBOL_GPL(btree_init_mempool);
  162. int btree_init(struct btree_head *head)
  163. {
  164. __btree_init(head);
  165. head->mempool = mempool_create(0, btree_alloc, btree_free, NULL);
  166. if (!head->mempool)
  167. return -ENOMEM;
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(btree_init);
  171. void btree_destroy(struct btree_head *head)
  172. {
  173. mempool_destroy(head->mempool);
  174. head->mempool = NULL;
  175. }
  176. EXPORT_SYMBOL_GPL(btree_destroy);
  177. void *btree_last(struct btree_head *head, struct btree_geo *geo,
  178. unsigned long *key)
  179. {
  180. int height = head->height;
  181. unsigned long *node = head->node;
  182. if (height == 0)
  183. return NULL;
  184. for ( ; height > 1; height--)
  185. node = bval(geo, node, 0);
  186. longcpy(key, bkey(geo, node, 0), geo->keylen);
  187. return bval(geo, node, 0);
  188. }
  189. EXPORT_SYMBOL_GPL(btree_last);
  190. static int keycmp(struct btree_geo *geo, unsigned long *node, int pos,
  191. unsigned long *key)
  192. {
  193. return longcmp(bkey(geo, node, pos), key, geo->keylen);
  194. }
  195. static int keyzero(struct btree_geo *geo, unsigned long *key)
  196. {
  197. int i;
  198. for (i = 0; i < geo->keylen; i++)
  199. if (key[i])
  200. return 0;
  201. return 1;
  202. }
  203. void *btree_lookup(struct btree_head *head, struct btree_geo *geo,
  204. unsigned long *key)
  205. {
  206. int i, height = head->height;
  207. unsigned long *node = head->node;
  208. if (height == 0)
  209. return NULL;
  210. for ( ; height > 1; height--) {
  211. for (i = 0; i < geo->no_pairs; i++)
  212. if (keycmp(geo, node, i, key) <= 0)
  213. break;
  214. if (i == geo->no_pairs)
  215. return NULL;
  216. node = bval(geo, node, i);
  217. if (!node)
  218. return NULL;
  219. }
  220. if (!node)
  221. return NULL;
  222. for (i = 0; i < geo->no_pairs; i++)
  223. if (keycmp(geo, node, i, key) == 0)
  224. return bval(geo, node, i);
  225. return NULL;
  226. }
  227. EXPORT_SYMBOL_GPL(btree_lookup);
  228. int btree_update(struct btree_head *head, struct btree_geo *geo,
  229. unsigned long *key, void *val)
  230. {
  231. int i, height = head->height;
  232. unsigned long *node = head->node;
  233. if (height == 0)
  234. return -ENOENT;
  235. for ( ; height > 1; height--) {
  236. for (i = 0; i < geo->no_pairs; i++)
  237. if (keycmp(geo, node, i, key) <= 0)
  238. break;
  239. if (i == geo->no_pairs)
  240. return -ENOENT;
  241. node = bval(geo, node, i);
  242. if (!node)
  243. return -ENOENT;
  244. }
  245. if (!node)
  246. return -ENOENT;
  247. for (i = 0; i < geo->no_pairs; i++)
  248. if (keycmp(geo, node, i, key) == 0) {
  249. setval(geo, node, i, val);
  250. return 0;
  251. }
  252. return -ENOENT;
  253. }
  254. EXPORT_SYMBOL_GPL(btree_update);
  255. /*
  256. * Usually this function is quite similar to normal lookup. But the key of
  257. * a parent node may be smaller than the smallest key of all its siblings.
  258. * In such a case we cannot just return NULL, as we have only proven that no
  259. * key smaller than __key, but larger than this parent key exists.
  260. * So we set __key to the parent key and retry. We have to use the smallest
  261. * such parent key, which is the last parent key we encountered.
  262. */
  263. void *btree_get_prev(struct btree_head *head, struct btree_geo *geo,
  264. unsigned long *__key)
  265. {
  266. int i, height;
  267. unsigned long *node, *oldnode;
  268. unsigned long *retry_key = NULL, key[geo->keylen];
  269. if (keyzero(geo, __key))
  270. return NULL;
  271. if (head->height == 0)
  272. return NULL;
  273. retry:
  274. longcpy(key, __key, geo->keylen);
  275. dec_key(geo, key);
  276. node = head->node;
  277. for (height = head->height ; height > 1; height--) {
  278. for (i = 0; i < geo->no_pairs; i++)
  279. if (keycmp(geo, node, i, key) <= 0)
  280. break;
  281. if (i == geo->no_pairs)
  282. goto miss;
  283. oldnode = node;
  284. node = bval(geo, node, i);
  285. if (!node)
  286. goto miss;
  287. retry_key = bkey(geo, oldnode, i);
  288. }
  289. if (!node)
  290. goto miss;
  291. for (i = 0; i < geo->no_pairs; i++) {
  292. if (keycmp(geo, node, i, key) <= 0) {
  293. if (bval(geo, node, i)) {
  294. longcpy(__key, bkey(geo, node, i), geo->keylen);
  295. return bval(geo, node, i);
  296. } else
  297. goto miss;
  298. }
  299. }
  300. miss:
  301. if (retry_key) {
  302. __key = retry_key;
  303. retry_key = NULL;
  304. goto retry;
  305. }
  306. return NULL;
  307. }
  308. static int getpos(struct btree_geo *geo, unsigned long *node,
  309. unsigned long *key)
  310. {
  311. int i;
  312. for (i = 0; i < geo->no_pairs; i++) {
  313. if (keycmp(geo, node, i, key) <= 0)
  314. break;
  315. }
  316. return i;
  317. }
  318. static int getfill(struct btree_geo *geo, unsigned long *node, int start)
  319. {
  320. int i;
  321. for (i = start; i < geo->no_pairs; i++)
  322. if (!bval(geo, node, i))
  323. break;
  324. return i;
  325. }
  326. /*
  327. * locate the correct leaf node in the btree
  328. */
  329. static unsigned long *find_level(struct btree_head *head, struct btree_geo *geo,
  330. unsigned long *key, int level)
  331. {
  332. unsigned long *node = head->node;
  333. int i, height;
  334. for (height = head->height; height > level; height--) {
  335. for (i = 0; i < geo->no_pairs; i++)
  336. if (keycmp(geo, node, i, key) <= 0)
  337. break;
  338. if ((i == geo->no_pairs) || !bval(geo, node, i)) {
  339. /* right-most key is too large, update it */
  340. /* FIXME: If the right-most key on higher levels is
  341. * always zero, this wouldn't be necessary. */
  342. i--;
  343. setkey(geo, node, i, key);
  344. }
  345. BUG_ON(i < 0);
  346. node = bval(geo, node, i);
  347. }
  348. BUG_ON(!node);
  349. return node;
  350. }
  351. static int btree_grow(struct btree_head *head, struct btree_geo *geo,
  352. gfp_t gfp)
  353. {
  354. unsigned long *node;
  355. int fill;
  356. node = btree_node_alloc(head, gfp);
  357. if (!node)
  358. return -ENOMEM;
  359. if (head->node) {
  360. fill = getfill(geo, head->node, 0);
  361. setkey(geo, node, 0, bkey(geo, head->node, fill - 1));
  362. setval(geo, node, 0, head->node);
  363. }
  364. head->node = node;
  365. head->height++;
  366. return 0;
  367. }
  368. static void btree_shrink(struct btree_head *head, struct btree_geo *geo)
  369. {
  370. unsigned long *node;
  371. int fill;
  372. if (head->height <= 1)
  373. return;
  374. node = head->node;
  375. fill = getfill(geo, node, 0);
  376. BUG_ON(fill > 1);
  377. head->node = bval(geo, node, 0);
  378. head->height--;
  379. mempool_free(node, head->mempool);
  380. }
  381. static int btree_insert_level(struct btree_head *head, struct btree_geo *geo,
  382. unsigned long *key, void *val, int level,
  383. gfp_t gfp)
  384. {
  385. unsigned long *node;
  386. int i, pos, fill, err;
  387. BUG_ON(!val);
  388. if (head->height < level) {
  389. err = btree_grow(head, geo, gfp);
  390. if (err)
  391. return err;
  392. }
  393. retry:
  394. node = find_level(head, geo, key, level);
  395. pos = getpos(geo, node, key);
  396. fill = getfill(geo, node, pos);
  397. /* two identical keys are not allowed */
  398. BUG_ON(pos < fill && keycmp(geo, node, pos, key) == 0);
  399. if (fill == geo->no_pairs) {
  400. /* need to split node */
  401. unsigned long *new;
  402. new = btree_node_alloc(head, gfp);
  403. if (!new)
  404. return -ENOMEM;
  405. err = btree_insert_level(head, geo,
  406. bkey(geo, node, fill / 2 - 1),
  407. new, level + 1, gfp);
  408. if (err) {
  409. mempool_free(new, head->mempool);
  410. return err;
  411. }
  412. for (i = 0; i < fill / 2; i++) {
  413. setkey(geo, new, i, bkey(geo, node, i));
  414. setval(geo, new, i, bval(geo, node, i));
  415. setkey(geo, node, i, bkey(geo, node, i + fill / 2));
  416. setval(geo, node, i, bval(geo, node, i + fill / 2));
  417. clearpair(geo, node, i + fill / 2);
  418. }
  419. if (fill & 1) {
  420. setkey(geo, node, i, bkey(geo, node, fill - 1));
  421. setval(geo, node, i, bval(geo, node, fill - 1));
  422. clearpair(geo, node, fill - 1);
  423. }
  424. goto retry;
  425. }
  426. BUG_ON(fill >= geo->no_pairs);
  427. /* shift and insert */
  428. for (i = fill; i > pos; i--) {
  429. setkey(geo, node, i, bkey(geo, node, i - 1));
  430. setval(geo, node, i, bval(geo, node, i - 1));
  431. }
  432. setkey(geo, node, pos, key);
  433. setval(geo, node, pos, val);
  434. return 0;
  435. }
  436. int btree_insert(struct btree_head *head, struct btree_geo *geo,
  437. unsigned long *key, void *val, gfp_t gfp)
  438. {
  439. return btree_insert_level(head, geo, key, val, 1, gfp);
  440. }
  441. EXPORT_SYMBOL_GPL(btree_insert);
  442. static void *btree_remove_level(struct btree_head *head, struct btree_geo *geo,
  443. unsigned long *key, int level);
  444. static void merge(struct btree_head *head, struct btree_geo *geo, int level,
  445. unsigned long *left, int lfill,
  446. unsigned long *right, int rfill,
  447. unsigned long *parent, int lpos)
  448. {
  449. int i;
  450. for (i = 0; i < rfill; i++) {
  451. /* Move all keys to the left */
  452. setkey(geo, left, lfill + i, bkey(geo, right, i));
  453. setval(geo, left, lfill + i, bval(geo, right, i));
  454. }
  455. /* Exchange left and right child in parent */
  456. setval(geo, parent, lpos, right);
  457. setval(geo, parent, lpos + 1, left);
  458. /* Remove left (formerly right) child from parent */
  459. btree_remove_level(head, geo, bkey(geo, parent, lpos), level + 1);
  460. mempool_free(right, head->mempool);
  461. }
  462. static void rebalance(struct btree_head *head, struct btree_geo *geo,
  463. unsigned long *key, int level, unsigned long *child, int fill)
  464. {
  465. unsigned long *parent, *left = NULL, *right = NULL;
  466. int i, no_left, no_right;
  467. if (fill == 0) {
  468. /* Because we don't steal entries from a neighbour, this case
  469. * can happen. Parent node contains a single child, this
  470. * node, so merging with a sibling never happens.
  471. */
  472. btree_remove_level(head, geo, key, level + 1);
  473. mempool_free(child, head->mempool);
  474. return;
  475. }
  476. parent = find_level(head, geo, key, level + 1);
  477. i = getpos(geo, parent, key);
  478. BUG_ON(bval(geo, parent, i) != child);
  479. if (i > 0) {
  480. left = bval(geo, parent, i - 1);
  481. no_left = getfill(geo, left, 0);
  482. if (fill + no_left <= geo->no_pairs) {
  483. merge(head, geo, level,
  484. left, no_left,
  485. child, fill,
  486. parent, i - 1);
  487. return;
  488. }
  489. }
  490. if (i + 1 < getfill(geo, parent, i)) {
  491. right = bval(geo, parent, i + 1);
  492. no_right = getfill(geo, right, 0);
  493. if (fill + no_right <= geo->no_pairs) {
  494. merge(head, geo, level,
  495. child, fill,
  496. right, no_right,
  497. parent, i);
  498. return;
  499. }
  500. }
  501. /*
  502. * We could also try to steal one entry from the left or right
  503. * neighbor. By not doing so we changed the invariant from
  504. * "all nodes are at least half full" to "no two neighboring
  505. * nodes can be merged". Which means that the average fill of
  506. * all nodes is still half or better.
  507. */
  508. }
  509. static void *btree_remove_level(struct btree_head *head, struct btree_geo *geo,
  510. unsigned long *key, int level)
  511. {
  512. unsigned long *node;
  513. int i, pos, fill;
  514. void *ret;
  515. if (level > head->height) {
  516. /* we recursed all the way up */
  517. head->height = 0;
  518. head->node = NULL;
  519. return NULL;
  520. }
  521. node = find_level(head, geo, key, level);
  522. pos = getpos(geo, node, key);
  523. fill = getfill(geo, node, pos);
  524. if ((level == 1) && (keycmp(geo, node, pos, key) != 0))
  525. return NULL;
  526. ret = bval(geo, node, pos);
  527. /* remove and shift */
  528. for (i = pos; i < fill - 1; i++) {
  529. setkey(geo, node, i, bkey(geo, node, i + 1));
  530. setval(geo, node, i, bval(geo, node, i + 1));
  531. }
  532. clearpair(geo, node, fill - 1);
  533. if (fill - 1 < geo->no_pairs / 2) {
  534. if (level < head->height)
  535. rebalance(head, geo, key, level, node, fill - 1);
  536. else if (fill - 1 == 1)
  537. btree_shrink(head, geo);
  538. }
  539. return ret;
  540. }
  541. void *btree_remove(struct btree_head *head, struct btree_geo *geo,
  542. unsigned long *key)
  543. {
  544. if (head->height == 0)
  545. return NULL;
  546. return btree_remove_level(head, geo, key, 1);
  547. }
  548. EXPORT_SYMBOL_GPL(btree_remove);
  549. int btree_merge(struct btree_head *target, struct btree_head *victim,
  550. struct btree_geo *geo, gfp_t gfp)
  551. {
  552. unsigned long key[geo->keylen];
  553. unsigned long dup[geo->keylen];
  554. void *val;
  555. int err;
  556. BUG_ON(target == victim);
  557. if (!(target->node)) {
  558. /* target is empty, just copy fields over */
  559. target->node = victim->node;
  560. target->height = victim->height;
  561. __btree_init(victim);
  562. return 0;
  563. }
  564. /* TODO: This needs some optimizations. Currently we do three tree
  565. * walks to remove a single object from the victim.
  566. */
  567. for (;;) {
  568. if (!btree_last(victim, geo, key))
  569. break;
  570. val = btree_lookup(victim, geo, key);
  571. err = btree_insert(target, geo, key, val, gfp);
  572. if (err)
  573. return err;
  574. /* We must make a copy of the key, as the original will get
  575. * mangled inside btree_remove. */
  576. longcpy(dup, key, geo->keylen);
  577. btree_remove(victim, geo, dup);
  578. }
  579. return 0;
  580. }
  581. EXPORT_SYMBOL_GPL(btree_merge);
  582. static size_t __btree_for_each(struct btree_head *head, struct btree_geo *geo,
  583. unsigned long *node, unsigned long opaque,
  584. void (*func)(void *elem, unsigned long opaque,
  585. unsigned long *key, size_t index,
  586. void *func2),
  587. void *func2, int reap, int height, size_t count)
  588. {
  589. int i;
  590. unsigned long *child;
  591. for (i = 0; i < geo->no_pairs; i++) {
  592. child = bval(geo, node, i);
  593. if (!child)
  594. break;
  595. if (height > 1)
  596. count = __btree_for_each(head, geo, child, opaque,
  597. func, func2, reap, height - 1, count);
  598. else
  599. func(child, opaque, bkey(geo, node, i), count++,
  600. func2);
  601. }
  602. if (reap)
  603. mempool_free(node, head->mempool);
  604. return count;
  605. }
  606. static void empty(void *elem, unsigned long opaque, unsigned long *key,
  607. size_t index, void *func2)
  608. {
  609. }
  610. void visitorl(void *elem, unsigned long opaque, unsigned long *key,
  611. size_t index, void *__func)
  612. {
  613. visitorl_t func = __func;
  614. func(elem, opaque, *key, index);
  615. }
  616. EXPORT_SYMBOL_GPL(visitorl);
  617. void visitor32(void *elem, unsigned long opaque, unsigned long *__key,
  618. size_t index, void *__func)
  619. {
  620. visitor32_t func = __func;
  621. u32 *key = (void *)__key;
  622. func(elem, opaque, *key, index);
  623. }
  624. EXPORT_SYMBOL_GPL(visitor32);
  625. void visitor64(void *elem, unsigned long opaque, unsigned long *__key,
  626. size_t index, void *__func)
  627. {
  628. visitor64_t func = __func;
  629. u64 *key = (void *)__key;
  630. func(elem, opaque, *key, index);
  631. }
  632. EXPORT_SYMBOL_GPL(visitor64);
  633. void visitor128(void *elem, unsigned long opaque, unsigned long *__key,
  634. size_t index, void *__func)
  635. {
  636. visitor128_t func = __func;
  637. u64 *key = (void *)__key;
  638. func(elem, opaque, key[0], key[1], index);
  639. }
  640. EXPORT_SYMBOL_GPL(visitor128);
  641. size_t btree_visitor(struct btree_head *head, struct btree_geo *geo,
  642. unsigned long opaque,
  643. void (*func)(void *elem, unsigned long opaque,
  644. unsigned long *key,
  645. size_t index, void *func2),
  646. void *func2)
  647. {
  648. size_t count = 0;
  649. if (!func2)
  650. func = empty;
  651. if (head->node)
  652. count = __btree_for_each(head, geo, head->node, opaque, func,
  653. func2, 0, head->height, 0);
  654. return count;
  655. }
  656. EXPORT_SYMBOL_GPL(btree_visitor);
  657. size_t btree_grim_visitor(struct btree_head *head, struct btree_geo *geo,
  658. unsigned long opaque,
  659. void (*func)(void *elem, unsigned long opaque,
  660. unsigned long *key,
  661. size_t index, void *func2),
  662. void *func2)
  663. {
  664. size_t count = 0;
  665. if (!func2)
  666. func = empty;
  667. if (head->node)
  668. count = __btree_for_each(head, geo, head->node, opaque, func,
  669. func2, 1, head->height, 0);
  670. __btree_init(head);
  671. return count;
  672. }
  673. EXPORT_SYMBOL_GPL(btree_grim_visitor);
  674. static int __init btree_module_init(void)
  675. {
  676. btree_cachep = kmem_cache_create("btree_node", NODESIZE, 0,
  677. SLAB_HWCACHE_ALIGN, NULL);
  678. return 0;
  679. }
  680. static void __exit btree_module_exit(void)
  681. {
  682. kmem_cache_destroy(btree_cachep);
  683. }
  684. /* If core code starts using btree, initialization should happen even earlier */
  685. module_init(btree_module_init);
  686. module_exit(btree_module_exit);
  687. MODULE_AUTHOR("Joern Engel <joern@logfs.org>");
  688. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  689. MODULE_LICENSE("GPL");