dm-btree.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 void unlock_all_frames(struct del_stack *s)
  187. {
  188. struct frame *f;
  189. while (unprocessed_frames(s)) {
  190. f = s->spine + s->top--;
  191. dm_tm_unlock(s->tm, f->b);
  192. }
  193. }
  194. int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
  195. {
  196. int r;
  197. struct del_stack *s;
  198. s = kmalloc(sizeof(*s), GFP_NOIO);
  199. if (!s)
  200. return -ENOMEM;
  201. s->tm = info->tm;
  202. s->top = -1;
  203. r = push_frame(s, root, 1);
  204. if (r)
  205. goto out;
  206. while (unprocessed_frames(s)) {
  207. uint32_t flags;
  208. struct frame *f;
  209. dm_block_t b;
  210. r = top_frame(s, &f);
  211. if (r)
  212. goto out;
  213. if (f->current_child >= f->nr_children) {
  214. pop_frame(s);
  215. continue;
  216. }
  217. flags = le32_to_cpu(f->n->header.flags);
  218. if (flags & INTERNAL_NODE) {
  219. b = value64(f->n, f->current_child);
  220. f->current_child++;
  221. r = push_frame(s, b, f->level);
  222. if (r)
  223. goto out;
  224. } else if (f->level != (info->levels - 1)) {
  225. b = value64(f->n, f->current_child);
  226. f->current_child++;
  227. r = push_frame(s, b, f->level + 1);
  228. if (r)
  229. goto out;
  230. } else {
  231. if (info->value_type.dec) {
  232. unsigned i;
  233. for (i = 0; i < f->nr_children; i++)
  234. info->value_type.dec(info->value_type.context,
  235. value_ptr(f->n, i));
  236. }
  237. f->current_child = f->nr_children;
  238. }
  239. }
  240. out:
  241. if (r) {
  242. /* cleanup all frames of del_stack */
  243. unlock_all_frames(s);
  244. }
  245. kfree(s);
  246. return r;
  247. }
  248. EXPORT_SYMBOL_GPL(dm_btree_del);
  249. /*----------------------------------------------------------------*/
  250. static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
  251. int (*search_fn)(struct btree_node *, uint64_t),
  252. uint64_t *result_key, void *v, size_t value_size)
  253. {
  254. int i, r;
  255. uint32_t flags, nr_entries;
  256. do {
  257. r = ro_step(s, block);
  258. if (r < 0)
  259. return r;
  260. i = search_fn(ro_node(s), key);
  261. flags = le32_to_cpu(ro_node(s)->header.flags);
  262. nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
  263. if (i < 0 || i >= nr_entries)
  264. return -ENODATA;
  265. if (flags & INTERNAL_NODE)
  266. block = value64(ro_node(s), i);
  267. } while (!(flags & LEAF_NODE));
  268. *result_key = le64_to_cpu(ro_node(s)->keys[i]);
  269. memcpy(v, value_ptr(ro_node(s), i), value_size);
  270. return 0;
  271. }
  272. int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
  273. uint64_t *keys, void *value_le)
  274. {
  275. unsigned level, last_level = info->levels - 1;
  276. int r = -ENODATA;
  277. uint64_t rkey;
  278. __le64 internal_value_le;
  279. struct ro_spine spine;
  280. init_ro_spine(&spine, info);
  281. for (level = 0; level < info->levels; level++) {
  282. size_t size;
  283. void *value_p;
  284. if (level == last_level) {
  285. value_p = value_le;
  286. size = info->value_type.size;
  287. } else {
  288. value_p = &internal_value_le;
  289. size = sizeof(uint64_t);
  290. }
  291. r = btree_lookup_raw(&spine, root, keys[level],
  292. lower_bound, &rkey,
  293. value_p, size);
  294. if (!r) {
  295. if (rkey != keys[level]) {
  296. exit_ro_spine(&spine);
  297. return -ENODATA;
  298. }
  299. } else {
  300. exit_ro_spine(&spine);
  301. return r;
  302. }
  303. root = le64_to_cpu(internal_value_le);
  304. }
  305. exit_ro_spine(&spine);
  306. return r;
  307. }
  308. EXPORT_SYMBOL_GPL(dm_btree_lookup);
  309. /*
  310. * Splits a node by creating a sibling node and shifting half the nodes
  311. * contents across. Assumes there is a parent node, and it has room for
  312. * another child.
  313. *
  314. * Before:
  315. * +--------+
  316. * | Parent |
  317. * +--------+
  318. * |
  319. * v
  320. * +----------+
  321. * | A ++++++ |
  322. * +----------+
  323. *
  324. *
  325. * After:
  326. * +--------+
  327. * | Parent |
  328. * +--------+
  329. * | |
  330. * v +------+
  331. * +---------+ |
  332. * | A* +++ | v
  333. * +---------+ +-------+
  334. * | B +++ |
  335. * +-------+
  336. *
  337. * Where A* is a shadow of A.
  338. */
  339. static int btree_split_sibling(struct shadow_spine *s, dm_block_t root,
  340. unsigned parent_index, uint64_t key)
  341. {
  342. int r;
  343. size_t size;
  344. unsigned nr_left, nr_right;
  345. struct dm_block *left, *right, *parent;
  346. struct btree_node *ln, *rn, *pn;
  347. __le64 location;
  348. left = shadow_current(s);
  349. r = new_block(s->info, &right);
  350. if (r < 0)
  351. return r;
  352. ln = dm_block_data(left);
  353. rn = dm_block_data(right);
  354. nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
  355. nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
  356. ln->header.nr_entries = cpu_to_le32(nr_left);
  357. rn->header.flags = ln->header.flags;
  358. rn->header.nr_entries = cpu_to_le32(nr_right);
  359. rn->header.max_entries = ln->header.max_entries;
  360. rn->header.value_size = ln->header.value_size;
  361. memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
  362. size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
  363. sizeof(uint64_t) : s->info->value_type.size;
  364. memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left),
  365. size * nr_right);
  366. /*
  367. * Patch up the parent
  368. */
  369. parent = shadow_parent(s);
  370. pn = dm_block_data(parent);
  371. location = cpu_to_le64(dm_block_location(left));
  372. __dm_bless_for_disk(&location);
  373. memcpy_disk(value_ptr(pn, parent_index),
  374. &location, sizeof(__le64));
  375. location = cpu_to_le64(dm_block_location(right));
  376. __dm_bless_for_disk(&location);
  377. r = insert_at(sizeof(__le64), pn, parent_index + 1,
  378. le64_to_cpu(rn->keys[0]), &location);
  379. if (r) {
  380. unlock_block(s->info, right);
  381. return r;
  382. }
  383. if (key < le64_to_cpu(rn->keys[0])) {
  384. unlock_block(s->info, right);
  385. s->nodes[1] = left;
  386. } else {
  387. unlock_block(s->info, left);
  388. s->nodes[1] = right;
  389. }
  390. return 0;
  391. }
  392. /*
  393. * Splits a node by creating two new children beneath the given node.
  394. *
  395. * Before:
  396. * +----------+
  397. * | A ++++++ |
  398. * +----------+
  399. *
  400. *
  401. * After:
  402. * +------------+
  403. * | A (shadow) |
  404. * +------------+
  405. * | |
  406. * +------+ +----+
  407. * | |
  408. * v v
  409. * +-------+ +-------+
  410. * | B +++ | | C +++ |
  411. * +-------+ +-------+
  412. */
  413. static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
  414. {
  415. int r;
  416. size_t size;
  417. unsigned nr_left, nr_right;
  418. struct dm_block *left, *right, *new_parent;
  419. struct btree_node *pn, *ln, *rn;
  420. __le64 val;
  421. new_parent = shadow_current(s);
  422. pn = dm_block_data(new_parent);
  423. size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
  424. sizeof(__le64) : s->info->value_type.size;
  425. /* create & init the left block */
  426. r = new_block(s->info, &left);
  427. if (r < 0)
  428. return r;
  429. ln = dm_block_data(left);
  430. nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
  431. ln->header.flags = pn->header.flags;
  432. ln->header.nr_entries = cpu_to_le32(nr_left);
  433. ln->header.max_entries = pn->header.max_entries;
  434. ln->header.value_size = pn->header.value_size;
  435. memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
  436. memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
  437. /* create & init the right block */
  438. r = new_block(s->info, &right);
  439. if (r < 0) {
  440. unlock_block(s->info, left);
  441. return r;
  442. }
  443. rn = dm_block_data(right);
  444. nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
  445. rn->header.flags = pn->header.flags;
  446. rn->header.nr_entries = cpu_to_le32(nr_right);
  447. rn->header.max_entries = pn->header.max_entries;
  448. rn->header.value_size = pn->header.value_size;
  449. memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
  450. memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
  451. nr_right * size);
  452. /* new_parent should just point to l and r now */
  453. pn->header.flags = cpu_to_le32(INTERNAL_NODE);
  454. pn->header.nr_entries = cpu_to_le32(2);
  455. pn->header.max_entries = cpu_to_le32(
  456. calc_max_entries(sizeof(__le64),
  457. dm_bm_block_size(
  458. dm_tm_get_bm(s->info->tm))));
  459. pn->header.value_size = cpu_to_le32(sizeof(__le64));
  460. val = cpu_to_le64(dm_block_location(left));
  461. __dm_bless_for_disk(&val);
  462. pn->keys[0] = ln->keys[0];
  463. memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
  464. val = cpu_to_le64(dm_block_location(right));
  465. __dm_bless_for_disk(&val);
  466. pn->keys[1] = rn->keys[0];
  467. memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
  468. /*
  469. * rejig the spine. This is ugly, since it knows too
  470. * much about the spine
  471. */
  472. if (s->nodes[0] != new_parent) {
  473. unlock_block(s->info, s->nodes[0]);
  474. s->nodes[0] = new_parent;
  475. }
  476. if (key < le64_to_cpu(rn->keys[0])) {
  477. unlock_block(s->info, right);
  478. s->nodes[1] = left;
  479. } else {
  480. unlock_block(s->info, left);
  481. s->nodes[1] = right;
  482. }
  483. s->count = 2;
  484. return 0;
  485. }
  486. static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
  487. struct dm_btree_value_type *vt,
  488. uint64_t key, unsigned *index)
  489. {
  490. int r, i = *index, top = 1;
  491. struct btree_node *node;
  492. for (;;) {
  493. r = shadow_step(s, root, vt);
  494. if (r < 0)
  495. return r;
  496. node = dm_block_data(shadow_current(s));
  497. /*
  498. * We have to patch up the parent node, ugly, but I don't
  499. * see a way to do this automatically as part of the spine
  500. * op.
  501. */
  502. if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
  503. __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
  504. __dm_bless_for_disk(&location);
  505. memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
  506. &location, sizeof(__le64));
  507. }
  508. node = dm_block_data(shadow_current(s));
  509. if (node->header.nr_entries == node->header.max_entries) {
  510. if (top)
  511. r = btree_split_beneath(s, key);
  512. else
  513. r = btree_split_sibling(s, root, i, key);
  514. if (r < 0)
  515. return r;
  516. }
  517. node = dm_block_data(shadow_current(s));
  518. i = lower_bound(node, key);
  519. if (le32_to_cpu(node->header.flags) & LEAF_NODE)
  520. break;
  521. if (i < 0) {
  522. /* change the bounds on the lowest key */
  523. node->keys[0] = cpu_to_le64(key);
  524. i = 0;
  525. }
  526. root = value64(node, i);
  527. top = 0;
  528. }
  529. if (i < 0 || le64_to_cpu(node->keys[i]) != key)
  530. i++;
  531. *index = i;
  532. return 0;
  533. }
  534. static int insert(struct dm_btree_info *info, dm_block_t root,
  535. uint64_t *keys, void *value, dm_block_t *new_root,
  536. int *inserted)
  537. __dm_written_to_disk(value)
  538. {
  539. int r, need_insert;
  540. unsigned level, index = -1, last_level = info->levels - 1;
  541. dm_block_t block = root;
  542. struct shadow_spine spine;
  543. struct btree_node *n;
  544. struct dm_btree_value_type le64_type;
  545. init_le64_type(info->tm, &le64_type);
  546. init_shadow_spine(&spine, info);
  547. for (level = 0; level < (info->levels - 1); level++) {
  548. r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
  549. if (r < 0)
  550. goto bad;
  551. n = dm_block_data(shadow_current(&spine));
  552. need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
  553. (le64_to_cpu(n->keys[index]) != keys[level]));
  554. if (need_insert) {
  555. dm_block_t new_tree;
  556. __le64 new_le;
  557. r = dm_btree_empty(info, &new_tree);
  558. if (r < 0)
  559. goto bad;
  560. new_le = cpu_to_le64(new_tree);
  561. __dm_bless_for_disk(&new_le);
  562. r = insert_at(sizeof(uint64_t), n, index,
  563. keys[level], &new_le);
  564. if (r)
  565. goto bad;
  566. }
  567. if (level < last_level)
  568. block = value64(n, index);
  569. }
  570. r = btree_insert_raw(&spine, block, &info->value_type,
  571. keys[level], &index);
  572. if (r < 0)
  573. goto bad;
  574. n = dm_block_data(shadow_current(&spine));
  575. need_insert = ((index >= le32_to_cpu(n->header.nr_entries)) ||
  576. (le64_to_cpu(n->keys[index]) != keys[level]));
  577. if (need_insert) {
  578. if (inserted)
  579. *inserted = 1;
  580. r = insert_at(info->value_type.size, n, index,
  581. keys[level], value);
  582. if (r)
  583. goto bad_unblessed;
  584. } else {
  585. if (inserted)
  586. *inserted = 0;
  587. if (info->value_type.dec &&
  588. (!info->value_type.equal ||
  589. !info->value_type.equal(
  590. info->value_type.context,
  591. value_ptr(n, index),
  592. value))) {
  593. info->value_type.dec(info->value_type.context,
  594. value_ptr(n, index));
  595. }
  596. memcpy_disk(value_ptr(n, index),
  597. value, info->value_type.size);
  598. }
  599. *new_root = shadow_root(&spine);
  600. exit_shadow_spine(&spine);
  601. return 0;
  602. bad:
  603. __dm_unbless_for_disk(value);
  604. bad_unblessed:
  605. exit_shadow_spine(&spine);
  606. return r;
  607. }
  608. int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
  609. uint64_t *keys, void *value, dm_block_t *new_root)
  610. __dm_written_to_disk(value)
  611. {
  612. return insert(info, root, keys, value, new_root, NULL);
  613. }
  614. EXPORT_SYMBOL_GPL(dm_btree_insert);
  615. int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
  616. uint64_t *keys, void *value, dm_block_t *new_root,
  617. int *inserted)
  618. __dm_written_to_disk(value)
  619. {
  620. return insert(info, root, keys, value, new_root, inserted);
  621. }
  622. EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
  623. /*----------------------------------------------------------------*/
  624. static int find_highest_key(struct ro_spine *s, dm_block_t block,
  625. uint64_t *result_key, dm_block_t *next_block)
  626. {
  627. int i, r;
  628. uint32_t flags;
  629. do {
  630. r = ro_step(s, block);
  631. if (r < 0)
  632. return r;
  633. flags = le32_to_cpu(ro_node(s)->header.flags);
  634. i = le32_to_cpu(ro_node(s)->header.nr_entries);
  635. if (!i)
  636. return -ENODATA;
  637. else
  638. i--;
  639. *result_key = le64_to_cpu(ro_node(s)->keys[i]);
  640. if (next_block || flags & INTERNAL_NODE)
  641. block = value64(ro_node(s), i);
  642. } while (flags & INTERNAL_NODE);
  643. if (next_block)
  644. *next_block = block;
  645. return 0;
  646. }
  647. int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
  648. uint64_t *result_keys)
  649. {
  650. int r = 0, count = 0, level;
  651. struct ro_spine spine;
  652. init_ro_spine(&spine, info);
  653. for (level = 0; level < info->levels; level++) {
  654. r = find_highest_key(&spine, root, result_keys + level,
  655. level == info->levels - 1 ? NULL : &root);
  656. if (r == -ENODATA) {
  657. r = 0;
  658. break;
  659. } else if (r)
  660. break;
  661. count++;
  662. }
  663. exit_ro_spine(&spine);
  664. return r ? r : count;
  665. }
  666. EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);