btree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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
  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_free(head->node, head->mempool);
  174. mempool_destroy(head->mempool);
  175. head->mempool = NULL;
  176. }
  177. EXPORT_SYMBOL_GPL(btree_destroy);
  178. void *btree_last(struct btree_head *head, struct btree_geo *geo,
  179. unsigned long *key)
  180. {
  181. int height = head->height;
  182. unsigned long *node = head->node;
  183. if (height == 0)
  184. return NULL;
  185. for ( ; height > 1; height--)
  186. node = bval(geo, node, 0);
  187. longcpy(key, bkey(geo, node, 0), geo->keylen);
  188. return bval(geo, node, 0);
  189. }
  190. EXPORT_SYMBOL_GPL(btree_last);
  191. static int keycmp(struct btree_geo *geo, unsigned long *node, int pos,
  192. unsigned long *key)
  193. {
  194. return longcmp(bkey(geo, node, pos), key, geo->keylen);
  195. }
  196. static int keyzero(struct btree_geo *geo, unsigned long *key)
  197. {
  198. int i;
  199. for (i = 0; i < geo->keylen; i++)
  200. if (key[i])
  201. return 0;
  202. return 1;
  203. }
  204. void *btree_lookup(struct btree_head *head, struct btree_geo *geo,
  205. unsigned long *key)
  206. {
  207. int i, height = head->height;
  208. unsigned long *node = head->node;
  209. if (height == 0)
  210. return NULL;
  211. for ( ; height > 1; height--) {
  212. for (i = 0; i < geo->no_pairs; i++)
  213. if (keycmp(geo, node, i, key) <= 0)
  214. break;
  215. if (i == geo->no_pairs)
  216. return NULL;
  217. node = bval(geo, node, i);
  218. if (!node)
  219. return NULL;
  220. }
  221. if (!node)
  222. return NULL;
  223. for (i = 0; i < geo->no_pairs; i++)
  224. if (keycmp(geo, node, i, key) == 0)
  225. return bval(geo, node, i);
  226. return NULL;
  227. }
  228. EXPORT_SYMBOL_GPL(btree_lookup);
  229. int btree_update(struct btree_head *head, struct btree_geo *geo,
  230. unsigned long *key, void *val)
  231. {
  232. int i, height = head->height;
  233. unsigned long *node = head->node;
  234. if (height == 0)
  235. return -ENOENT;
  236. for ( ; height > 1; height--) {
  237. for (i = 0; i < geo->no_pairs; i++)
  238. if (keycmp(geo, node, i, key) <= 0)
  239. break;
  240. if (i == geo->no_pairs)
  241. return -ENOENT;
  242. node = bval(geo, node, i);
  243. if (!node)
  244. return -ENOENT;
  245. }
  246. if (!node)
  247. return -ENOENT;
  248. for (i = 0; i < geo->no_pairs; i++)
  249. if (keycmp(geo, node, i, key) == 0) {
  250. setval(geo, node, i, val);
  251. return 0;
  252. }
  253. return -ENOENT;
  254. }
  255. EXPORT_SYMBOL_GPL(btree_update);
  256. /*
  257. * Usually this function is quite similar to normal lookup. But the key of
  258. * a parent node may be smaller than the smallest key of all its siblings.
  259. * In such a case we cannot just return NULL, as we have only proven that no
  260. * key smaller than __key, but larger than this parent key exists.
  261. * So we set __key to the parent key and retry. We have to use the smallest
  262. * such parent key, which is the last parent key we encountered.
  263. */
  264. void *btree_get_prev(struct btree_head *head, struct btree_geo *geo,
  265. unsigned long *__key)
  266. {
  267. int i, height;
  268. unsigned long *node, *oldnode;
  269. unsigned long *retry_key = NULL, key[geo->keylen];
  270. if (keyzero(geo, __key))
  271. return NULL;
  272. if (head->height == 0)
  273. return NULL;
  274. longcpy(key, __key, geo->keylen);
  275. retry:
  276. dec_key(geo, key);
  277. node = head->node;
  278. for (height = head->height ; height > 1; height--) {
  279. for (i = 0; i < geo->no_pairs; i++)
  280. if (keycmp(geo, node, i, key) <= 0)
  281. break;
  282. if (i == geo->no_pairs)
  283. goto miss;
  284. oldnode = node;
  285. node = bval(geo, node, i);
  286. if (!node)
  287. goto miss;
  288. retry_key = bkey(geo, oldnode, i);
  289. }
  290. if (!node)
  291. goto miss;
  292. for (i = 0; i < geo->no_pairs; i++) {
  293. if (keycmp(geo, node, i, key) <= 0) {
  294. if (bval(geo, node, i)) {
  295. longcpy(__key, bkey(geo, node, i), geo->keylen);
  296. return bval(geo, node, i);
  297. } else
  298. goto miss;
  299. }
  300. }
  301. miss:
  302. if (retry_key) {
  303. longcpy(key, retry_key, geo->keylen);
  304. retry_key = NULL;
  305. goto retry;
  306. }
  307. return NULL;
  308. }
  309. EXPORT_SYMBOL_GPL(btree_get_prev);
  310. static int getpos(struct btree_geo *geo, unsigned long *node,
  311. unsigned long *key)
  312. {
  313. int i;
  314. for (i = 0; i < geo->no_pairs; i++) {
  315. if (keycmp(geo, node, i, key) <= 0)
  316. break;
  317. }
  318. return i;
  319. }
  320. static int getfill(struct btree_geo *geo, unsigned long *node, int start)
  321. {
  322. int i;
  323. for (i = start; i < geo->no_pairs; i++)
  324. if (!bval(geo, node, i))
  325. break;
  326. return i;
  327. }
  328. /*
  329. * locate the correct leaf node in the btree
  330. */
  331. static unsigned long *find_level(struct btree_head *head, struct btree_geo *geo,
  332. unsigned long *key, int level)
  333. {
  334. unsigned long *node = head->node;
  335. int i, height;
  336. for (height = head->height; height > level; height--) {
  337. for (i = 0; i < geo->no_pairs; i++)
  338. if (keycmp(geo, node, i, key) <= 0)
  339. break;
  340. if ((i == geo->no_pairs) || !bval(geo, node, i)) {
  341. /* right-most key is too large, update it */
  342. /* FIXME: If the right-most key on higher levels is
  343. * always zero, this wouldn't be necessary. */
  344. i--;
  345. setkey(geo, node, i, key);
  346. }
  347. BUG_ON(i < 0);
  348. node = bval(geo, node, i);
  349. }
  350. BUG_ON(!node);
  351. return node;
  352. }
  353. static int btree_grow(struct btree_head *head, struct btree_geo *geo,
  354. gfp_t gfp)
  355. {
  356. unsigned long *node;
  357. int fill;
  358. node = btree_node_alloc(head, gfp);
  359. if (!node)
  360. return -ENOMEM;
  361. if (head->node) {
  362. fill = getfill(geo, head->node, 0);
  363. setkey(geo, node, 0, bkey(geo, head->node, fill - 1));
  364. setval(geo, node, 0, head->node);
  365. }
  366. head->node = node;
  367. head->height++;
  368. return 0;
  369. }
  370. static void btree_shrink(struct btree_head *head, struct btree_geo *geo)
  371. {
  372. unsigned long *node;
  373. int fill;
  374. if (head->height <= 1)
  375. return;
  376. node = head->node;
  377. fill = getfill(geo, node, 0);
  378. BUG_ON(fill > 1);
  379. head->node = bval(geo, node, 0);
  380. head->height--;
  381. mempool_free(node, head->mempool);
  382. }
  383. static int btree_insert_level(struct btree_head *head, struct btree_geo *geo,
  384. unsigned long *key, void *val, int level,
  385. gfp_t gfp)
  386. {
  387. unsigned long *node;
  388. int i, pos, fill, err;
  389. BUG_ON(!val);
  390. if (head->height < level) {
  391. err = btree_grow(head, geo, gfp);
  392. if (err)
  393. return err;
  394. }
  395. retry:
  396. node = find_level(head, geo, key, level);
  397. pos = getpos(geo, node, key);
  398. fill = getfill(geo, node, pos);
  399. /* two identical keys are not allowed */
  400. BUG_ON(pos < fill && keycmp(geo, node, pos, key) == 0);
  401. if (fill == geo->no_pairs) {
  402. /* need to split node */
  403. unsigned long *new;
  404. new = btree_node_alloc(head, gfp);
  405. if (!new)
  406. return -ENOMEM;
  407. err = btree_insert_level(head, geo,
  408. bkey(geo, node, fill / 2 - 1),
  409. new, level + 1, gfp);
  410. if (err) {
  411. mempool_free(new, head->mempool);
  412. return err;
  413. }
  414. for (i = 0; i < fill / 2; i++) {
  415. setkey(geo, new, i, bkey(geo, node, i));
  416. setval(geo, new, i, bval(geo, node, i));
  417. setkey(geo, node, i, bkey(geo, node, i + fill / 2));
  418. setval(geo, node, i, bval(geo, node, i + fill / 2));
  419. clearpair(geo, node, i + fill / 2);
  420. }
  421. if (fill & 1) {
  422. setkey(geo, node, i, bkey(geo, node, fill - 1));
  423. setval(geo, node, i, bval(geo, node, fill - 1));
  424. clearpair(geo, node, fill - 1);
  425. }
  426. goto retry;
  427. }
  428. BUG_ON(fill >= geo->no_pairs);
  429. /* shift and insert */
  430. for (i = fill; i > pos; i--) {
  431. setkey(geo, node, i, bkey(geo, node, i - 1));
  432. setval(geo, node, i, bval(geo, node, i - 1));
  433. }
  434. setkey(geo, node, pos, key);
  435. setval(geo, node, pos, val);
  436. return 0;
  437. }
  438. int btree_insert(struct btree_head *head, struct btree_geo *geo,
  439. unsigned long *key, void *val, gfp_t gfp)
  440. {
  441. BUG_ON(!val);
  442. return btree_insert_level(head, geo, key, val, 1, gfp);
  443. }
  444. EXPORT_SYMBOL_GPL(btree_insert);
  445. static void *btree_remove_level(struct btree_head *head, struct btree_geo *geo,
  446. unsigned long *key, int level);
  447. static void merge(struct btree_head *head, struct btree_geo *geo, int level,
  448. unsigned long *left, int lfill,
  449. unsigned long *right, int rfill,
  450. unsigned long *parent, int lpos)
  451. {
  452. int i;
  453. for (i = 0; i < rfill; i++) {
  454. /* Move all keys to the left */
  455. setkey(geo, left, lfill + i, bkey(geo, right, i));
  456. setval(geo, left, lfill + i, bval(geo, right, i));
  457. }
  458. /* Exchange left and right child in parent */
  459. setval(geo, parent, lpos, right);
  460. setval(geo, parent, lpos + 1, left);
  461. /* Remove left (formerly right) child from parent */
  462. btree_remove_level(head, geo, bkey(geo, parent, lpos), level + 1);
  463. mempool_free(right, head->mempool);
  464. }
  465. static void rebalance(struct btree_head *head, struct btree_geo *geo,
  466. unsigned long *key, int level, unsigned long *child, int fill)
  467. {
  468. unsigned long *parent, *left = NULL, *right = NULL;
  469. int i, no_left, no_right;
  470. if (fill == 0) {
  471. /* Because we don't steal entries from a neighbour, this case
  472. * can happen. Parent node contains a single child, this
  473. * node, so merging with a sibling never happens.
  474. */
  475. btree_remove_level(head, geo, key, level + 1);
  476. mempool_free(child, head->mempool);
  477. return;
  478. }
  479. parent = find_level(head, geo, key, level + 1);
  480. i = getpos(geo, parent, key);
  481. BUG_ON(bval(geo, parent, i) != child);
  482. if (i > 0) {
  483. left = bval(geo, parent, i - 1);
  484. no_left = getfill(geo, left, 0);
  485. if (fill + no_left <= geo->no_pairs) {
  486. merge(head, geo, level,
  487. left, no_left,
  488. child, fill,
  489. parent, i - 1);
  490. return;
  491. }
  492. }
  493. if (i + 1 < getfill(geo, parent, i)) {
  494. right = bval(geo, parent, i + 1);
  495. no_right = getfill(geo, right, 0);
  496. if (fill + no_right <= geo->no_pairs) {
  497. merge(head, geo, level,
  498. child, fill,
  499. right, no_right,
  500. parent, i);
  501. return;
  502. }
  503. }
  504. /*
  505. * We could also try to steal one entry from the left or right
  506. * neighbor. By not doing so we changed the invariant from
  507. * "all nodes are at least half full" to "no two neighboring
  508. * nodes can be merged". Which means that the average fill of
  509. * all nodes is still half or better.
  510. */
  511. }
  512. static void *btree_remove_level(struct btree_head *head, struct btree_geo *geo,
  513. unsigned long *key, int level)
  514. {
  515. unsigned long *node;
  516. int i, pos, fill;
  517. void *ret;
  518. if (level > head->height) {
  519. /* we recursed all the way up */
  520. head->height = 0;
  521. head->node = NULL;
  522. return NULL;
  523. }
  524. node = find_level(head, geo, key, level);
  525. pos = getpos(geo, node, key);
  526. fill = getfill(geo, node, pos);
  527. if ((level == 1) && (keycmp(geo, node, pos, key) != 0))
  528. return NULL;
  529. ret = bval(geo, node, pos);
  530. /* remove and shift */
  531. for (i = pos; i < fill - 1; i++) {
  532. setkey(geo, node, i, bkey(geo, node, i + 1));
  533. setval(geo, node, i, bval(geo, node, i + 1));
  534. }
  535. clearpair(geo, node, fill - 1);
  536. if (fill - 1 < geo->no_pairs / 2) {
  537. if (level < head->height)
  538. rebalance(head, geo, key, level, node, fill - 1);
  539. else if (fill - 1 == 1)
  540. btree_shrink(head, geo);
  541. }
  542. return ret;
  543. }
  544. void *btree_remove(struct btree_head *head, struct btree_geo *geo,
  545. unsigned long *key)
  546. {
  547. if (head->height == 0)
  548. return NULL;
  549. return btree_remove_level(head, geo, key, 1);
  550. }
  551. EXPORT_SYMBOL_GPL(btree_remove);
  552. int btree_merge(struct btree_head *target, struct btree_head *victim,
  553. struct btree_geo *geo, gfp_t gfp)
  554. {
  555. unsigned long key[geo->keylen];
  556. unsigned long dup[geo->keylen];
  557. void *val;
  558. int err;
  559. BUG_ON(target == victim);
  560. if (!(target->node)) {
  561. /* target is empty, just copy fields over */
  562. target->node = victim->node;
  563. target->height = victim->height;
  564. __btree_init(victim);
  565. return 0;
  566. }
  567. /* TODO: This needs some optimizations. Currently we do three tree
  568. * walks to remove a single object from the victim.
  569. */
  570. for (;;) {
  571. if (!btree_last(victim, geo, key))
  572. break;
  573. val = btree_lookup(victim, geo, key);
  574. err = btree_insert(target, geo, key, val, gfp);
  575. if (err)
  576. return err;
  577. /* We must make a copy of the key, as the original will get
  578. * mangled inside btree_remove. */
  579. longcpy(dup, key, geo->keylen);
  580. btree_remove(victim, geo, dup);
  581. }
  582. return 0;
  583. }
  584. EXPORT_SYMBOL_GPL(btree_merge);
  585. static size_t __btree_for_each(struct btree_head *head, struct btree_geo *geo,
  586. unsigned long *node, unsigned long opaque,
  587. void (*func)(void *elem, unsigned long opaque,
  588. unsigned long *key, size_t index,
  589. void *func2),
  590. void *func2, int reap, int height, size_t count)
  591. {
  592. int i;
  593. unsigned long *child;
  594. for (i = 0; i < geo->no_pairs; i++) {
  595. child = bval(geo, node, i);
  596. if (!child)
  597. break;
  598. if (height > 1)
  599. count = __btree_for_each(head, geo, child, opaque,
  600. func, func2, reap, height - 1, count);
  601. else
  602. func(child, opaque, bkey(geo, node, i), count++,
  603. func2);
  604. }
  605. if (reap)
  606. mempool_free(node, head->mempool);
  607. return count;
  608. }
  609. static void empty(void *elem, unsigned long opaque, unsigned long *key,
  610. size_t index, void *func2)
  611. {
  612. }
  613. void visitorl(void *elem, unsigned long opaque, unsigned long *key,
  614. size_t index, void *__func)
  615. {
  616. visitorl_t func = __func;
  617. func(elem, opaque, *key, index);
  618. }
  619. EXPORT_SYMBOL_GPL(visitorl);
  620. void visitor32(void *elem, unsigned long opaque, unsigned long *__key,
  621. size_t index, void *__func)
  622. {
  623. visitor32_t func = __func;
  624. u32 *key = (void *)__key;
  625. func(elem, opaque, *key, index);
  626. }
  627. EXPORT_SYMBOL_GPL(visitor32);
  628. void visitor64(void *elem, unsigned long opaque, unsigned long *__key,
  629. size_t index, void *__func)
  630. {
  631. visitor64_t func = __func;
  632. u64 *key = (void *)__key;
  633. func(elem, opaque, *key, index);
  634. }
  635. EXPORT_SYMBOL_GPL(visitor64);
  636. void visitor128(void *elem, unsigned long opaque, unsigned long *__key,
  637. size_t index, void *__func)
  638. {
  639. visitor128_t func = __func;
  640. u64 *key = (void *)__key;
  641. func(elem, opaque, key[0], key[1], index);
  642. }
  643. EXPORT_SYMBOL_GPL(visitor128);
  644. size_t btree_visitor(struct btree_head *head, struct btree_geo *geo,
  645. unsigned long opaque,
  646. void (*func)(void *elem, unsigned long opaque,
  647. unsigned long *key,
  648. size_t index, void *func2),
  649. void *func2)
  650. {
  651. size_t count = 0;
  652. if (!func2)
  653. func = empty;
  654. if (head->node)
  655. count = __btree_for_each(head, geo, head->node, opaque, func,
  656. func2, 0, head->height, 0);
  657. return count;
  658. }
  659. EXPORT_SYMBOL_GPL(btree_visitor);
  660. size_t btree_grim_visitor(struct btree_head *head, struct btree_geo *geo,
  661. unsigned long opaque,
  662. void (*func)(void *elem, unsigned long opaque,
  663. unsigned long *key,
  664. size_t index, void *func2),
  665. void *func2)
  666. {
  667. size_t count = 0;
  668. if (!func2)
  669. func = empty;
  670. if (head->node)
  671. count = __btree_for_each(head, geo, head->node, opaque, func,
  672. func2, 1, head->height, 0);
  673. __btree_init(head);
  674. return count;
  675. }
  676. EXPORT_SYMBOL_GPL(btree_grim_visitor);
  677. static int __init btree_module_init(void)
  678. {
  679. btree_cachep = kmem_cache_create("btree_node", NODESIZE, 0,
  680. SLAB_HWCACHE_ALIGN, NULL);
  681. return 0;
  682. }
  683. static void __exit btree_module_exit(void)
  684. {
  685. kmem_cache_destroy(btree_cachep);
  686. }
  687. /* If core code starts using btree, initialization should happen even earlier */
  688. module_init(btree_module_init);
  689. module_exit(btree_module_exit);
  690. MODULE_AUTHOR("Joern Engel <joern@logfs.org>");
  691. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  692. MODULE_LICENSE("GPL");