dm-btree.c 19 KB

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