dm-btree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-btree-internal.h"
  7. #include "dm-space-map.h"
  8. #include "dm-transaction-manager.h"
  9. #include <linux/export.h>
  10. #include <linux/device-mapper.h>
  11. #define DM_MSG_PREFIX "btree"
  12. /*----------------------------------------------------------------
  13. * Array manipulation
  14. *--------------------------------------------------------------*/
  15. static void memcpy_disk(void *dest, const void *src, size_t len)
  16. __dm_written_to_disk(src)
  17. {
  18. memcpy(dest, src, len);
  19. __dm_unbless_for_disk(src);
  20. }
  21. static void array_insert(void *base, size_t elt_size, unsigned nr_elts,
  22. unsigned index, void *elt)
  23. __dm_written_to_disk(elt)
  24. {
  25. if (index < nr_elts)
  26. memmove(base + (elt_size * (index + 1)),
  27. base + (elt_size * index),
  28. (nr_elts - index) * elt_size);
  29. memcpy_disk(base + (elt_size * index), elt, elt_size);
  30. }
  31. /*----------------------------------------------------------------*/
  32. /* makes the assumption that no two keys are the same. */
  33. static int bsearch(struct btree_node *n, uint64_t key, int want_hi)
  34. {
  35. int lo = -1, hi = le32_to_cpu(n->header.nr_entries);
  36. while (hi - lo > 1) {
  37. int mid = lo + ((hi - lo) / 2);
  38. uint64_t mid_key = le64_to_cpu(n->keys[mid]);
  39. if (mid_key == key)
  40. return mid;
  41. if (mid_key < key)
  42. lo = mid;
  43. else
  44. hi = mid;
  45. }
  46. return want_hi ? hi : lo;
  47. }
  48. int lower_bound(struct btree_node *n, uint64_t key)
  49. {
  50. return bsearch(n, key, 0);
  51. }
  52. void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
  53. struct dm_btree_value_type *vt)
  54. {
  55. unsigned i;
  56. uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
  57. if (le32_to_cpu(n->header.flags) & INTERNAL_NODE)
  58. for (i = 0; i < nr_entries; i++)
  59. dm_tm_inc(tm, value64(n, i));
  60. else if (vt->inc)
  61. for (i = 0; i < nr_entries; i++)
  62. vt->inc(vt->context, value_ptr(n, i));
  63. }
  64. static int insert_at(size_t value_size, struct btree_node *node, unsigned index,
  65. uint64_t key, void *value)
  66. __dm_written_to_disk(value)
  67. {
  68. uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
  69. __le64 key_le = cpu_to_le64(key);
  70. if (index > nr_entries ||
  71. index >= le32_to_cpu(node->header.max_entries)) {
  72. DMERR("too many entries in btree node for insert");
  73. __dm_unbless_for_disk(value);
  74. return -ENOMEM;
  75. }
  76. __dm_bless_for_disk(&key_le);
  77. array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
  78. array_insert(value_base(node), value_size, nr_entries, index, value);
  79. node->header.nr_entries = cpu_to_le32(nr_entries + 1);
  80. return 0;
  81. }
  82. /*----------------------------------------------------------------*/
  83. /*
  84. * We want 3n entries (for some n). This works more nicely for repeated
  85. * insert remove loops than (2n + 1).
  86. */
  87. static uint32_t calc_max_entries(size_t value_size, size_t block_size)
  88. {
  89. uint32_t total, n;
  90. size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
  91. block_size -= sizeof(struct node_header);
  92. total = block_size / elt_size;
  93. n = total / 3; /* rounds down */
  94. return 3 * n;
  95. }
  96. int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
  97. {
  98. int r;
  99. struct dm_block *b;
  100. struct btree_node *n;
  101. size_t block_size;
  102. uint32_t max_entries;
  103. r = new_block(info, &b);
  104. if (r < 0)
  105. return r;
  106. block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
  107. max_entries = calc_max_entries(info->value_type.size, block_size);
  108. n = dm_block_data(b);
  109. memset(n, 0, block_size);
  110. n->header.flags = cpu_to_le32(LEAF_NODE);
  111. n->header.nr_entries = cpu_to_le32(0);
  112. n->header.max_entries = cpu_to_le32(max_entries);
  113. n->header.value_size = cpu_to_le32(info->value_type.size);
  114. *root = dm_block_location(b);
  115. return unlock_block(info, b);
  116. }
  117. EXPORT_SYMBOL_GPL(dm_btree_empty);
  118. /*----------------------------------------------------------------*/
  119. /*
  120. * Deletion uses a recursive algorithm, since we have limited stack space
  121. * we explicitly manage our own stack on the heap.
  122. */
  123. #define MAX_SPINE_DEPTH 64
  124. struct frame {
  125. struct dm_block *b;
  126. struct btree_node *n;
  127. unsigned level;
  128. unsigned nr_children;
  129. unsigned current_child;
  130. };
  131. struct del_stack {
  132. struct dm_transaction_manager *tm;
  133. int top;
  134. struct frame spine[MAX_SPINE_DEPTH];
  135. };
  136. static int top_frame(struct del_stack *s, struct frame **f)
  137. {
  138. if (s->top < 0) {
  139. DMERR("btree deletion stack empty");
  140. return -EINVAL;
  141. }
  142. *f = s->spine + s->top;
  143. return 0;
  144. }
  145. static int unprocessed_frames(struct del_stack *s)
  146. {
  147. return s->top >= 0;
  148. }
  149. static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
  150. {
  151. int r;
  152. uint32_t ref_count;
  153. if (s->top >= MAX_SPINE_DEPTH - 1) {
  154. DMERR("btree deletion stack out of memory");
  155. return -ENOMEM;
  156. }
  157. r = dm_tm_ref(s->tm, b, &ref_count);
  158. if (r)
  159. return r;
  160. if (ref_count > 1)
  161. /*
  162. * This is a shared node, so we can just decrement it's
  163. * reference counter and leave the children.
  164. */
  165. dm_tm_dec(s->tm, b);
  166. else {
  167. struct frame *f = s->spine + ++s->top;
  168. r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
  169. if (r) {
  170. s->top--;
  171. return r;
  172. }
  173. f->n = dm_block_data(f->b);
  174. f->level = level;
  175. f->nr_children = le32_to_cpu(f->n->header.nr_entries);
  176. f->current_child = 0;
  177. }
  178. return 0;
  179. }
  180. static void pop_frame(struct del_stack *s)
  181. {
  182. struct frame *f = s->spine + s->top--;
  183. dm_tm_dec(s->tm, dm_block_location(f->b));
  184. dm_tm_unlock(s->tm, f->b);
  185. }
  186. static bool is_internal_level(struct dm_btree_info *info, struct frame *f)
  187. {
  188. return f->level < (info->levels - 1);
  189. }
  190. int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
  191. {
  192. int r;
  193. struct del_stack *s;
  194. s = kmalloc(sizeof(*s), GFP_KERNEL);
  195. if (!s)
  196. return -ENOMEM;
  197. s->tm = info->tm;
  198. s->top = -1;
  199. r = push_frame(s, root, 0);
  200. if (r)
  201. goto out;
  202. while (unprocessed_frames(s)) {
  203. uint32_t flags;
  204. struct frame *f;
  205. dm_block_t b;
  206. r = top_frame(s, &f);
  207. if (r)
  208. goto out;
  209. if (f->current_child >= f->nr_children) {
  210. pop_frame(s);
  211. continue;
  212. }
  213. flags = le32_to_cpu(f->n->header.flags);
  214. if (flags & INTERNAL_NODE) {
  215. b = value64(f->n, f->current_child);
  216. f->current_child++;
  217. r = push_frame(s, b, f->level);
  218. if (r)
  219. goto out;
  220. } else if (is_internal_level(info, f)) {
  221. b = value64(f->n, f->current_child);
  222. f->current_child++;
  223. r = push_frame(s, b, f->level + 1);
  224. if (r)
  225. goto out;
  226. } else {
  227. if (info->value_type.dec) {
  228. unsigned i;
  229. for (i = 0; i < f->nr_children; i++)
  230. info->value_type.dec(info->value_type.context,
  231. value_ptr(f->n, i));
  232. }
  233. f->current_child = f->nr_children;
  234. }
  235. }
  236. out:
  237. kfree(s);
  238. return r;
  239. }
  240. EXPORT_SYMBOL_GPL(dm_btree_del);
  241. /*----------------------------------------------------------------*/
  242. static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
  243. int (*search_fn)(struct btree_node *, uint64_t),
  244. uint64_t *result_key, void *v, size_t value_size)
  245. {
  246. int i, r;
  247. uint32_t flags, nr_entries;
  248. do {
  249. r = ro_step(s, block);
  250. if (r < 0)
  251. return r;
  252. i = search_fn(ro_node(s), key);
  253. flags = le32_to_cpu(ro_node(s)->header.flags);
  254. nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
  255. if (i < 0 || i >= nr_entries)
  256. return -ENODATA;
  257. if (flags & INTERNAL_NODE)
  258. block = value64(ro_node(s), i);
  259. } while (!(flags & LEAF_NODE));
  260. *result_key = le64_to_cpu(ro_node(s)->keys[i]);
  261. memcpy(v, value_ptr(ro_node(s), i), value_size);
  262. return 0;
  263. }
  264. int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
  265. uint64_t *keys, void *value_le)
  266. {
  267. unsigned level, last_level = info->levels - 1;
  268. int r = -ENODATA;
  269. uint64_t rkey;
  270. __le64 internal_value_le;
  271. struct ro_spine spine;
  272. init_ro_spine(&spine, info);
  273. for (level = 0; level < info->levels; level++) {
  274. size_t size;
  275. void *value_p;
  276. if (level == last_level) {
  277. value_p = value_le;
  278. size = info->value_type.size;
  279. } else {
  280. value_p = &internal_value_le;
  281. size = sizeof(uint64_t);
  282. }
  283. r = btree_lookup_raw(&spine, root, keys[level],
  284. lower_bound, &rkey,
  285. value_p, size);
  286. if (!r) {
  287. if (rkey != keys[level]) {
  288. exit_ro_spine(&spine);
  289. return -ENODATA;
  290. }
  291. } else {
  292. exit_ro_spine(&spine);
  293. return r;
  294. }
  295. root = le64_to_cpu(internal_value_le);
  296. }
  297. exit_ro_spine(&spine);
  298. return r;
  299. }
  300. EXPORT_SYMBOL_GPL(dm_btree_lookup);
  301. /*
  302. * Splits a node by creating a sibling node and shifting half the nodes
  303. * contents across. Assumes there is a parent node, and it has room for
  304. * another child.
  305. *
  306. * Before:
  307. * +--------+
  308. * | Parent |
  309. * +--------+
  310. * |
  311. * v
  312. * +----------+
  313. * | A ++++++ |
  314. * +----------+
  315. *
  316. *
  317. * After:
  318. * +--------+
  319. * | Parent |
  320. * +--------+
  321. * | |
  322. * v +------+
  323. * +---------+ |
  324. * | A* +++ | v
  325. * +---------+ +-------+
  326. * | B +++ |
  327. * +-------+
  328. *
  329. * Where A* is a shadow of A.
  330. */
  331. static int btree_split_sibling(struct shadow_spine *s, dm_block_t root,
  332. unsigned parent_index, uint64_t key)
  333. {
  334. int r;
  335. size_t size;
  336. unsigned nr_left, nr_right;
  337. struct dm_block *left, *right, *parent;
  338. struct btree_node *ln, *rn, *pn;
  339. __le64 location;
  340. left = shadow_current(s);
  341. r = new_block(s->info, &right);
  342. if (r < 0)
  343. return r;
  344. ln = dm_block_data(left);
  345. rn = dm_block_data(right);
  346. nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
  347. nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
  348. ln->header.nr_entries = cpu_to_le32(nr_left);
  349. rn->header.flags = ln->header.flags;
  350. rn->header.nr_entries = cpu_to_le32(nr_right);
  351. rn->header.max_entries = ln->header.max_entries;
  352. rn->header.value_size = ln->header.value_size;
  353. memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
  354. size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
  355. sizeof(uint64_t) : s->info->value_type.size;
  356. memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left),
  357. size * nr_right);
  358. /*
  359. * Patch up the parent
  360. */
  361. parent = shadow_parent(s);
  362. pn = dm_block_data(parent);
  363. location = cpu_to_le64(dm_block_location(left));
  364. __dm_bless_for_disk(&location);
  365. memcpy_disk(value_ptr(pn, parent_index),
  366. &location, sizeof(__le64));
  367. location = cpu_to_le64(dm_block_location(right));
  368. __dm_bless_for_disk(&location);
  369. r = insert_at(sizeof(__le64), pn, parent_index + 1,
  370. le64_to_cpu(rn->keys[0]), &location);
  371. if (r)
  372. return r;
  373. if (key < le64_to_cpu(rn->keys[0])) {
  374. unlock_block(s->info, right);
  375. s->nodes[1] = left;
  376. } else {
  377. unlock_block(s->info, left);
  378. s->nodes[1] = right;
  379. }
  380. return 0;
  381. }
  382. /*
  383. * Splits a node by creating two new children beneath the given node.
  384. *
  385. * Before:
  386. * +----------+
  387. * | A ++++++ |
  388. * +----------+
  389. *
  390. *
  391. * After:
  392. * +------------+
  393. * | A (shadow) |
  394. * +------------+
  395. * | |
  396. * +------+ +----+
  397. * | |
  398. * v v
  399. * +-------+ +-------+
  400. * | B +++ | | C +++ |
  401. * +-------+ +-------+
  402. */
  403. static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
  404. {
  405. int r;
  406. size_t size;
  407. unsigned nr_left, nr_right;
  408. struct dm_block *left, *right, *new_parent;
  409. struct btree_node *pn, *ln, *rn;
  410. __le64 val;
  411. new_parent = shadow_current(s);
  412. r = new_block(s->info, &left);
  413. if (r < 0)
  414. return r;
  415. r = new_block(s->info, &right);
  416. if (r < 0) {
  417. /* FIXME: put left */
  418. return r;
  419. }
  420. pn = dm_block_data(new_parent);
  421. ln = dm_block_data(left);
  422. rn = dm_block_data(right);
  423. nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
  424. nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
  425. ln->header.flags = pn->header.flags;
  426. ln->header.nr_entries = cpu_to_le32(nr_left);
  427. ln->header.max_entries = pn->header.max_entries;
  428. ln->header.value_size = pn->header.value_size;
  429. rn->header.flags = pn->header.flags;
  430. rn->header.nr_entries = cpu_to_le32(nr_right);
  431. rn->header.max_entries = pn->header.max_entries;
  432. rn->header.value_size = pn->header.value_size;
  433. memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
  434. memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
  435. size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
  436. sizeof(__le64) : s->info->value_type.size;
  437. memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
  438. memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
  439. nr_right * size);
  440. /* new_parent should just point to l and r now */
  441. pn->header.flags = cpu_to_le32(INTERNAL_NODE);
  442. pn->header.nr_entries = cpu_to_le32(2);
  443. pn->header.max_entries = cpu_to_le32(
  444. calc_max_entries(sizeof(__le64),
  445. dm_bm_block_size(
  446. dm_tm_get_bm(s->info->tm))));
  447. pn->header.value_size = cpu_to_le32(sizeof(__le64));
  448. val = cpu_to_le64(dm_block_location(left));
  449. __dm_bless_for_disk(&val);
  450. pn->keys[0] = ln->keys[0];
  451. memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
  452. val = cpu_to_le64(dm_block_location(right));
  453. __dm_bless_for_disk(&val);
  454. pn->keys[1] = rn->keys[0];
  455. memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
  456. /*
  457. * rejig the spine. This is ugly, since it knows too
  458. * much about the spine
  459. */
  460. if (s->nodes[0] != new_parent) {
  461. unlock_block(s->info, s->nodes[0]);
  462. s->nodes[0] = new_parent;
  463. }
  464. if (key < le64_to_cpu(rn->keys[0])) {
  465. unlock_block(s->info, right);
  466. s->nodes[1] = left;
  467. } else {
  468. unlock_block(s->info, left);
  469. s->nodes[1] = right;
  470. }
  471. s->count = 2;
  472. return 0;
  473. }
  474. static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
  475. struct dm_btree_value_type *vt,
  476. uint64_t key, unsigned *index)
  477. {
  478. int r, i = *index, top = 1;
  479. struct btree_node *node;
  480. for (;;) {
  481. r = shadow_step(s, root, vt);
  482. if (r < 0)
  483. return r;
  484. node = dm_block_data(shadow_current(s));
  485. /*
  486. * We have to patch up the parent node, ugly, but I don't
  487. * see a way to do this automatically as part of the spine
  488. * op.
  489. */
  490. if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
  491. __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
  492. __dm_bless_for_disk(&location);
  493. memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
  494. &location, sizeof(__le64));
  495. }
  496. node = dm_block_data(shadow_current(s));
  497. if (node->header.nr_entries == node->header.max_entries) {
  498. if (top)
  499. r = btree_split_beneath(s, key);
  500. else
  501. r = btree_split_sibling(s, root, i, key);
  502. if (r < 0)
  503. return r;
  504. }
  505. node = dm_block_data(shadow_current(s));
  506. i = lower_bound(node, key);
  507. if (le32_to_cpu(node->header.flags) & LEAF_NODE)
  508. break;
  509. if (i < 0) {
  510. /* change the bounds on the lowest key */
  511. node->keys[0] = cpu_to_le64(key);
  512. i = 0;
  513. }
  514. root = value64(node, i);
  515. top = 0;
  516. }
  517. if (i < 0 || le64_to_cpu(node->keys[i]) != key)
  518. i++;
  519. *index = i;
  520. return 0;
  521. }
  522. static int insert(struct dm_btree_info *info, dm_block_t root,
  523. uint64_t *keys, void *value, dm_block_t *new_root,
  524. int *inserted)
  525. __dm_written_to_disk(value)
  526. {
  527. int r, need_insert;
  528. unsigned level, index = -1, last_level = info->levels - 1;
  529. dm_block_t block = root;
  530. struct shadow_spine spine;
  531. struct btree_node *n;
  532. struct dm_btree_value_type le64_type;
  533. le64_type.context = NULL;
  534. le64_type.size = sizeof(__le64);
  535. le64_type.inc = NULL;
  536. le64_type.dec = NULL;
  537. le64_type.equal = NULL;
  538. init_shadow_spine(&spine, info);
  539. for (level = 0; level < (info->levels - 1); level++) {
  540. r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
  541. if (r < 0)
  542. goto bad;
  543. n = dm_block_data(shadow_current(&spine));
  544. need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
  545. (le64_to_cpu(n->keys[index]) != keys[level]));
  546. if (need_insert) {
  547. dm_block_t new_tree;
  548. __le64 new_le;
  549. r = dm_btree_empty(info, &new_tree);
  550. if (r < 0)
  551. goto bad;
  552. new_le = cpu_to_le64(new_tree);
  553. __dm_bless_for_disk(&new_le);
  554. r = insert_at(sizeof(uint64_t), n, index,
  555. keys[level], &new_le);
  556. if (r)
  557. goto bad;
  558. }
  559. if (level < last_level)
  560. block = value64(n, index);
  561. }
  562. r = btree_insert_raw(&spine, block, &info->value_type,
  563. keys[level], &index);
  564. if (r < 0)
  565. goto bad;
  566. n = dm_block_data(shadow_current(&spine));
  567. need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
  568. (le64_to_cpu(n->keys[index]) != keys[level]));
  569. if (need_insert) {
  570. if (inserted)
  571. *inserted = 1;
  572. r = insert_at(info->value_type.size, n, index,
  573. keys[level], value);
  574. if (r)
  575. goto bad_unblessed;
  576. } else {
  577. if (inserted)
  578. *inserted = 0;
  579. if (info->value_type.dec &&
  580. (!info->value_type.equal ||
  581. !info->value_type.equal(
  582. info->value_type.context,
  583. value_ptr(n, index),
  584. value))) {
  585. info->value_type.dec(info->value_type.context,
  586. value_ptr(n, index));
  587. }
  588. memcpy_disk(value_ptr(n, index),
  589. value, info->value_type.size);
  590. }
  591. *new_root = shadow_root(&spine);
  592. exit_shadow_spine(&spine);
  593. return 0;
  594. bad:
  595. __dm_unbless_for_disk(value);
  596. bad_unblessed:
  597. exit_shadow_spine(&spine);
  598. return r;
  599. }
  600. int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
  601. uint64_t *keys, void *value, dm_block_t *new_root)
  602. __dm_written_to_disk(value)
  603. {
  604. return insert(info, root, keys, value, new_root, NULL);
  605. }
  606. EXPORT_SYMBOL_GPL(dm_btree_insert);
  607. int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
  608. uint64_t *keys, void *value, dm_block_t *new_root,
  609. int *inserted)
  610. __dm_written_to_disk(value)
  611. {
  612. return insert(info, root, keys, value, new_root, inserted);
  613. }
  614. EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
  615. /*----------------------------------------------------------------*/
  616. static int find_highest_key(struct ro_spine *s, dm_block_t block,
  617. uint64_t *result_key, dm_block_t *next_block)
  618. {
  619. int i, r;
  620. uint32_t flags;
  621. do {
  622. r = ro_step(s, block);
  623. if (r < 0)
  624. return r;
  625. flags = le32_to_cpu(ro_node(s)->header.flags);
  626. i = le32_to_cpu(ro_node(s)->header.nr_entries);
  627. if (!i)
  628. return -ENODATA;
  629. else
  630. i--;
  631. *result_key = le64_to_cpu(ro_node(s)->keys[i]);
  632. if (next_block || flags & INTERNAL_NODE)
  633. block = value64(ro_node(s), i);
  634. } while (flags & INTERNAL_NODE);
  635. if (next_block)
  636. *next_block = block;
  637. return 0;
  638. }
  639. int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
  640. uint64_t *result_keys)
  641. {
  642. int r = 0, count = 0, level;
  643. struct ro_spine spine;
  644. init_ro_spine(&spine, info);
  645. for (level = 0; level < info->levels; level++) {
  646. r = find_highest_key(&spine, root, result_keys + level,
  647. level == info->levels - 1 ? NULL : &root);
  648. if (r == -ENODATA) {
  649. r = 0;
  650. break;
  651. } else if (r)
  652. break;
  653. count++;
  654. }
  655. exit_ro_spine(&spine);
  656. return r ? r : count;
  657. }
  658. EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
  659. /*
  660. * FIXME: We shouldn't use a recursive algorithm when we have limited stack
  661. * space. Also this only works for single level trees.
  662. */
  663. static int walk_node(struct ro_spine *s, dm_block_t block,
  664. int (*fn)(void *context, uint64_t *keys, void *leaf),
  665. void *context)
  666. {
  667. int r;
  668. unsigned i, nr;
  669. struct btree_node *n;
  670. uint64_t keys;
  671. r = ro_step(s, block);
  672. n = ro_node(s);
  673. nr = le32_to_cpu(n->header.nr_entries);
  674. for (i = 0; i < nr; i++) {
  675. if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
  676. r = walk_node(s, value64(n, i), fn, context);
  677. if (r)
  678. goto out;
  679. } else {
  680. keys = le64_to_cpu(*key_ptr(n, i));
  681. r = fn(context, &keys, value_ptr(n, i));
  682. if (r)
  683. goto out;
  684. }
  685. }
  686. out:
  687. ro_pop(s);
  688. return r;
  689. }
  690. int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
  691. int (*fn)(void *context, uint64_t *keys, void *leaf),
  692. void *context)
  693. {
  694. int r;
  695. struct ro_spine spine;
  696. BUG_ON(info->levels > 1);
  697. init_ro_spine(&spine, info);
  698. r = walk_node(&spine, root, fn, context);
  699. exit_ro_spine(&spine);
  700. return r;
  701. }
  702. EXPORT_SYMBOL_GPL(dm_btree_walk);