dm-btree.c 18 KB

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