dm-btree.c 18 KB

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