dm-btree.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. static int upper_bound(struct btree_node *n, uint64_t key)
  53. {
  54. return bsearch(n, key, 1);
  55. }
  56. void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
  57. struct dm_btree_value_type *vt)
  58. {
  59. unsigned i;
  60. uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
  61. if (le32_to_cpu(n->header.flags) & INTERNAL_NODE)
  62. for (i = 0; i < nr_entries; i++)
  63. dm_tm_inc(tm, value64(n, i));
  64. else if (vt->inc)
  65. for (i = 0; i < nr_entries; i++)
  66. vt->inc(vt->context, value_ptr(n, i));
  67. }
  68. static int insert_at(size_t value_size, struct btree_node *node, unsigned index,
  69. uint64_t key, void *value)
  70. __dm_written_to_disk(value)
  71. {
  72. uint32_t nr_entries = le32_to_cpu(node->header.nr_entries);
  73. __le64 key_le = cpu_to_le64(key);
  74. if (index > nr_entries ||
  75. index >= le32_to_cpu(node->header.max_entries)) {
  76. DMERR("too many entries in btree node for insert");
  77. __dm_unbless_for_disk(value);
  78. return -ENOMEM;
  79. }
  80. __dm_bless_for_disk(&key_le);
  81. array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
  82. array_insert(value_base(node), value_size, nr_entries, index, value);
  83. node->header.nr_entries = cpu_to_le32(nr_entries + 1);
  84. return 0;
  85. }
  86. /*----------------------------------------------------------------*/
  87. /*
  88. * We want 3n entries (for some n). This works more nicely for repeated
  89. * insert remove loops than (2n + 1).
  90. */
  91. static uint32_t calc_max_entries(size_t value_size, size_t block_size)
  92. {
  93. uint32_t total, n;
  94. size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
  95. block_size -= sizeof(struct node_header);
  96. total = block_size / elt_size;
  97. n = total / 3; /* rounds down */
  98. return 3 * n;
  99. }
  100. int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
  101. {
  102. int r;
  103. struct dm_block *b;
  104. struct btree_node *n;
  105. size_t block_size;
  106. uint32_t max_entries;
  107. r = new_block(info, &b);
  108. if (r < 0)
  109. return r;
  110. block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
  111. max_entries = calc_max_entries(info->value_type.size, block_size);
  112. n = dm_block_data(b);
  113. memset(n, 0, block_size);
  114. n->header.flags = cpu_to_le32(LEAF_NODE);
  115. n->header.nr_entries = cpu_to_le32(0);
  116. n->header.max_entries = cpu_to_le32(max_entries);
  117. n->header.value_size = cpu_to_le32(info->value_type.size);
  118. *root = dm_block_location(b);
  119. unlock_block(info, b);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(dm_btree_empty);
  123. /*----------------------------------------------------------------*/
  124. /*
  125. * Deletion uses a recursive algorithm, since we have limited stack space
  126. * we explicitly manage our own stack on the heap.
  127. */
  128. #define MAX_SPINE_DEPTH 64
  129. struct frame {
  130. struct dm_block *b;
  131. struct btree_node *n;
  132. unsigned level;
  133. unsigned nr_children;
  134. unsigned current_child;
  135. };
  136. struct del_stack {
  137. struct dm_btree_info *info;
  138. struct dm_transaction_manager *tm;
  139. int top;
  140. struct frame spine[MAX_SPINE_DEPTH];
  141. };
  142. static int top_frame(struct del_stack *s, struct frame **f)
  143. {
  144. if (s->top < 0) {
  145. DMERR("btree deletion stack empty");
  146. return -EINVAL;
  147. }
  148. *f = s->spine + s->top;
  149. return 0;
  150. }
  151. static int unprocessed_frames(struct del_stack *s)
  152. {
  153. return s->top >= 0;
  154. }
  155. static void prefetch_children(struct del_stack *s, struct frame *f)
  156. {
  157. unsigned i;
  158. struct dm_block_manager *bm = dm_tm_get_bm(s->tm);
  159. for (i = 0; i < f->nr_children; i++)
  160. dm_bm_prefetch(bm, value64(f->n, i));
  161. }
  162. static bool is_internal_level(struct dm_btree_info *info, struct frame *f)
  163. {
  164. return f->level < (info->levels - 1);
  165. }
  166. static int push_frame(struct del_stack *s, dm_block_t b, unsigned level)
  167. {
  168. int r;
  169. uint32_t ref_count;
  170. if (s->top >= MAX_SPINE_DEPTH - 1) {
  171. DMERR("btree deletion stack out of memory");
  172. return -ENOMEM;
  173. }
  174. r = dm_tm_ref(s->tm, b, &ref_count);
  175. if (r)
  176. return r;
  177. if (ref_count > 1)
  178. /*
  179. * This is a shared node, so we can just decrement it's
  180. * reference counter and leave the children.
  181. */
  182. dm_tm_dec(s->tm, b);
  183. else {
  184. uint32_t flags;
  185. struct frame *f = s->spine + ++s->top;
  186. r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
  187. if (r) {
  188. s->top--;
  189. return r;
  190. }
  191. f->n = dm_block_data(f->b);
  192. f->level = level;
  193. f->nr_children = le32_to_cpu(f->n->header.nr_entries);
  194. f->current_child = 0;
  195. flags = le32_to_cpu(f->n->header.flags);
  196. if (flags & INTERNAL_NODE || is_internal_level(s->info, f))
  197. prefetch_children(s, f);
  198. }
  199. return 0;
  200. }
  201. static void pop_frame(struct del_stack *s)
  202. {
  203. struct frame *f = s->spine + s->top--;
  204. dm_tm_dec(s->tm, dm_block_location(f->b));
  205. dm_tm_unlock(s->tm, f->b);
  206. }
  207. static void unlock_all_frames(struct del_stack *s)
  208. {
  209. struct frame *f;
  210. while (unprocessed_frames(s)) {
  211. f = s->spine + s->top--;
  212. dm_tm_unlock(s->tm, f->b);
  213. }
  214. }
  215. int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
  216. {
  217. int r;
  218. struct del_stack *s;
  219. /*
  220. * dm_btree_del() is called via an ioctl, as such should be
  221. * considered an FS op. We can't recurse back into the FS, so we
  222. * allocate GFP_NOFS.
  223. */
  224. s = kmalloc(sizeof(*s), GFP_NOFS);
  225. if (!s)
  226. return -ENOMEM;
  227. s->info = info;
  228. s->tm = info->tm;
  229. s->top = -1;
  230. r = push_frame(s, root, 0);
  231. if (r)
  232. goto out;
  233. while (unprocessed_frames(s)) {
  234. uint32_t flags;
  235. struct frame *f;
  236. dm_block_t b;
  237. r = top_frame(s, &f);
  238. if (r)
  239. goto out;
  240. if (f->current_child >= f->nr_children) {
  241. pop_frame(s);
  242. continue;
  243. }
  244. flags = le32_to_cpu(f->n->header.flags);
  245. if (flags & INTERNAL_NODE) {
  246. b = value64(f->n, f->current_child);
  247. f->current_child++;
  248. r = push_frame(s, b, f->level);
  249. if (r)
  250. goto out;
  251. } else if (is_internal_level(info, f)) {
  252. b = value64(f->n, f->current_child);
  253. f->current_child++;
  254. r = push_frame(s, b, f->level + 1);
  255. if (r)
  256. goto out;
  257. } else {
  258. if (info->value_type.dec) {
  259. unsigned i;
  260. for (i = 0; i < f->nr_children; i++)
  261. info->value_type.dec(info->value_type.context,
  262. value_ptr(f->n, i));
  263. }
  264. pop_frame(s);
  265. }
  266. }
  267. out:
  268. if (r) {
  269. /* cleanup all frames of del_stack */
  270. unlock_all_frames(s);
  271. }
  272. kfree(s);
  273. return r;
  274. }
  275. EXPORT_SYMBOL_GPL(dm_btree_del);
  276. /*----------------------------------------------------------------*/
  277. static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
  278. int (*search_fn)(struct btree_node *, uint64_t),
  279. uint64_t *result_key, void *v, size_t value_size)
  280. {
  281. int i, r;
  282. uint32_t flags, nr_entries;
  283. do {
  284. r = ro_step(s, block);
  285. if (r < 0)
  286. return r;
  287. i = search_fn(ro_node(s), key);
  288. flags = le32_to_cpu(ro_node(s)->header.flags);
  289. nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
  290. if (i < 0 || i >= nr_entries)
  291. return -ENODATA;
  292. if (flags & INTERNAL_NODE)
  293. block = value64(ro_node(s), i);
  294. } while (!(flags & LEAF_NODE));
  295. *result_key = le64_to_cpu(ro_node(s)->keys[i]);
  296. memcpy(v, value_ptr(ro_node(s), i), value_size);
  297. return 0;
  298. }
  299. int dm_btree_lookup(struct dm_btree_info *info, dm_block_t root,
  300. uint64_t *keys, void *value_le)
  301. {
  302. unsigned level, last_level = info->levels - 1;
  303. int r = -ENODATA;
  304. uint64_t rkey;
  305. __le64 internal_value_le;
  306. struct ro_spine spine;
  307. init_ro_spine(&spine, info);
  308. for (level = 0; level < info->levels; level++) {
  309. size_t size;
  310. void *value_p;
  311. if (level == last_level) {
  312. value_p = value_le;
  313. size = info->value_type.size;
  314. } else {
  315. value_p = &internal_value_le;
  316. size = sizeof(uint64_t);
  317. }
  318. r = btree_lookup_raw(&spine, root, keys[level],
  319. lower_bound, &rkey,
  320. value_p, size);
  321. if (!r) {
  322. if (rkey != keys[level]) {
  323. exit_ro_spine(&spine);
  324. return -ENODATA;
  325. }
  326. } else {
  327. exit_ro_spine(&spine);
  328. return r;
  329. }
  330. root = le64_to_cpu(internal_value_le);
  331. }
  332. exit_ro_spine(&spine);
  333. return r;
  334. }
  335. EXPORT_SYMBOL_GPL(dm_btree_lookup);
  336. static int dm_btree_lookup_next_single(struct dm_btree_info *info, dm_block_t root,
  337. uint64_t key, uint64_t *rkey, void *value_le)
  338. {
  339. int r, i;
  340. uint32_t flags, nr_entries;
  341. struct dm_block *node;
  342. struct btree_node *n;
  343. r = bn_read_lock(info, root, &node);
  344. if (r)
  345. return r;
  346. n = dm_block_data(node);
  347. flags = le32_to_cpu(n->header.flags);
  348. nr_entries = le32_to_cpu(n->header.nr_entries);
  349. if (flags & INTERNAL_NODE) {
  350. i = lower_bound(n, key);
  351. if (i < 0) {
  352. /*
  353. * avoid early -ENODATA return when all entries are
  354. * higher than the search @key.
  355. */
  356. i = 0;
  357. }
  358. if (i >= nr_entries) {
  359. r = -ENODATA;
  360. goto out;
  361. }
  362. r = dm_btree_lookup_next_single(info, value64(n, i), key, rkey, value_le);
  363. if (r == -ENODATA && i < (nr_entries - 1)) {
  364. i++;
  365. r = dm_btree_lookup_next_single(info, value64(n, i), key, rkey, value_le);
  366. }
  367. } else {
  368. i = upper_bound(n, key);
  369. if (i < 0 || i >= nr_entries) {
  370. r = -ENODATA;
  371. goto out;
  372. }
  373. *rkey = le64_to_cpu(n->keys[i]);
  374. memcpy(value_le, value_ptr(n, i), info->value_type.size);
  375. }
  376. out:
  377. dm_tm_unlock(info->tm, node);
  378. return r;
  379. }
  380. int dm_btree_lookup_next(struct dm_btree_info *info, dm_block_t root,
  381. uint64_t *keys, uint64_t *rkey, void *value_le)
  382. {
  383. unsigned level;
  384. int r = -ENODATA;
  385. __le64 internal_value_le;
  386. struct ro_spine spine;
  387. init_ro_spine(&spine, info);
  388. for (level = 0; level < info->levels - 1u; level++) {
  389. r = btree_lookup_raw(&spine, root, keys[level],
  390. lower_bound, rkey,
  391. &internal_value_le, sizeof(uint64_t));
  392. if (r)
  393. goto out;
  394. if (*rkey != keys[level]) {
  395. r = -ENODATA;
  396. goto out;
  397. }
  398. root = le64_to_cpu(internal_value_le);
  399. }
  400. r = dm_btree_lookup_next_single(info, root, keys[level], rkey, value_le);
  401. out:
  402. exit_ro_spine(&spine);
  403. return r;
  404. }
  405. EXPORT_SYMBOL_GPL(dm_btree_lookup_next);
  406. /*
  407. * Splits a node by creating a sibling node and shifting half the nodes
  408. * contents across. Assumes there is a parent node, and it has room for
  409. * another child.
  410. *
  411. * Before:
  412. * +--------+
  413. * | Parent |
  414. * +--------+
  415. * |
  416. * v
  417. * +----------+
  418. * | A ++++++ |
  419. * +----------+
  420. *
  421. *
  422. * After:
  423. * +--------+
  424. * | Parent |
  425. * +--------+
  426. * | |
  427. * v +------+
  428. * +---------+ |
  429. * | A* +++ | v
  430. * +---------+ +-------+
  431. * | B +++ |
  432. * +-------+
  433. *
  434. * Where A* is a shadow of A.
  435. */
  436. static int btree_split_sibling(struct shadow_spine *s, unsigned parent_index,
  437. uint64_t key)
  438. {
  439. int r;
  440. size_t size;
  441. unsigned nr_left, nr_right;
  442. struct dm_block *left, *right, *parent;
  443. struct btree_node *ln, *rn, *pn;
  444. __le64 location;
  445. left = shadow_current(s);
  446. r = new_block(s->info, &right);
  447. if (r < 0)
  448. return r;
  449. ln = dm_block_data(left);
  450. rn = dm_block_data(right);
  451. nr_left = le32_to_cpu(ln->header.nr_entries) / 2;
  452. nr_right = le32_to_cpu(ln->header.nr_entries) - nr_left;
  453. ln->header.nr_entries = cpu_to_le32(nr_left);
  454. rn->header.flags = ln->header.flags;
  455. rn->header.nr_entries = cpu_to_le32(nr_right);
  456. rn->header.max_entries = ln->header.max_entries;
  457. rn->header.value_size = ln->header.value_size;
  458. memcpy(rn->keys, ln->keys + nr_left, nr_right * sizeof(rn->keys[0]));
  459. size = le32_to_cpu(ln->header.flags) & INTERNAL_NODE ?
  460. sizeof(uint64_t) : s->info->value_type.size;
  461. memcpy(value_ptr(rn, 0), value_ptr(ln, nr_left),
  462. size * nr_right);
  463. /*
  464. * Patch up the parent
  465. */
  466. parent = shadow_parent(s);
  467. pn = dm_block_data(parent);
  468. location = cpu_to_le64(dm_block_location(left));
  469. __dm_bless_for_disk(&location);
  470. memcpy_disk(value_ptr(pn, parent_index),
  471. &location, sizeof(__le64));
  472. location = cpu_to_le64(dm_block_location(right));
  473. __dm_bless_for_disk(&location);
  474. r = insert_at(sizeof(__le64), pn, parent_index + 1,
  475. le64_to_cpu(rn->keys[0]), &location);
  476. if (r) {
  477. unlock_block(s->info, right);
  478. return r;
  479. }
  480. if (key < le64_to_cpu(rn->keys[0])) {
  481. unlock_block(s->info, right);
  482. s->nodes[1] = left;
  483. } else {
  484. unlock_block(s->info, left);
  485. s->nodes[1] = right;
  486. }
  487. return 0;
  488. }
  489. /*
  490. * Splits a node by creating two new children beneath the given node.
  491. *
  492. * Before:
  493. * +----------+
  494. * | A ++++++ |
  495. * +----------+
  496. *
  497. *
  498. * After:
  499. * +------------+
  500. * | A (shadow) |
  501. * +------------+
  502. * | |
  503. * +------+ +----+
  504. * | |
  505. * v v
  506. * +-------+ +-------+
  507. * | B +++ | | C +++ |
  508. * +-------+ +-------+
  509. */
  510. static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
  511. {
  512. int r;
  513. size_t size;
  514. unsigned nr_left, nr_right;
  515. struct dm_block *left, *right, *new_parent;
  516. struct btree_node *pn, *ln, *rn;
  517. __le64 val;
  518. new_parent = shadow_current(s);
  519. pn = dm_block_data(new_parent);
  520. size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
  521. sizeof(__le64) : s->info->value_type.size;
  522. /* create & init the left block */
  523. r = new_block(s->info, &left);
  524. if (r < 0)
  525. return r;
  526. ln = dm_block_data(left);
  527. nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
  528. ln->header.flags = pn->header.flags;
  529. ln->header.nr_entries = cpu_to_le32(nr_left);
  530. ln->header.max_entries = pn->header.max_entries;
  531. ln->header.value_size = pn->header.value_size;
  532. memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
  533. memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
  534. /* create & init the right block */
  535. r = new_block(s->info, &right);
  536. if (r < 0) {
  537. unlock_block(s->info, left);
  538. return r;
  539. }
  540. rn = dm_block_data(right);
  541. nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
  542. rn->header.flags = pn->header.flags;
  543. rn->header.nr_entries = cpu_to_le32(nr_right);
  544. rn->header.max_entries = pn->header.max_entries;
  545. rn->header.value_size = pn->header.value_size;
  546. memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
  547. memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
  548. nr_right * size);
  549. /* new_parent should just point to l and r now */
  550. pn->header.flags = cpu_to_le32(INTERNAL_NODE);
  551. pn->header.nr_entries = cpu_to_le32(2);
  552. pn->header.max_entries = cpu_to_le32(
  553. calc_max_entries(sizeof(__le64),
  554. dm_bm_block_size(
  555. dm_tm_get_bm(s->info->tm))));
  556. pn->header.value_size = cpu_to_le32(sizeof(__le64));
  557. val = cpu_to_le64(dm_block_location(left));
  558. __dm_bless_for_disk(&val);
  559. pn->keys[0] = ln->keys[0];
  560. memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
  561. val = cpu_to_le64(dm_block_location(right));
  562. __dm_bless_for_disk(&val);
  563. pn->keys[1] = rn->keys[0];
  564. memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
  565. unlock_block(s->info, left);
  566. unlock_block(s->info, right);
  567. return 0;
  568. }
  569. static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
  570. struct dm_btree_value_type *vt,
  571. uint64_t key, unsigned *index)
  572. {
  573. int r, i = *index, top = 1;
  574. struct btree_node *node;
  575. for (;;) {
  576. r = shadow_step(s, root, vt);
  577. if (r < 0)
  578. return r;
  579. node = dm_block_data(shadow_current(s));
  580. /*
  581. * We have to patch up the parent node, ugly, but I don't
  582. * see a way to do this automatically as part of the spine
  583. * op.
  584. */
  585. if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
  586. __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
  587. __dm_bless_for_disk(&location);
  588. memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
  589. &location, sizeof(__le64));
  590. }
  591. node = dm_block_data(shadow_current(s));
  592. if (node->header.nr_entries == node->header.max_entries) {
  593. if (top)
  594. r = btree_split_beneath(s, key);
  595. else
  596. r = btree_split_sibling(s, i, key);
  597. if (r < 0)
  598. return r;
  599. }
  600. node = dm_block_data(shadow_current(s));
  601. i = lower_bound(node, key);
  602. if (le32_to_cpu(node->header.flags) & LEAF_NODE)
  603. break;
  604. if (i < 0) {
  605. /* change the bounds on the lowest key */
  606. node->keys[0] = cpu_to_le64(key);
  607. i = 0;
  608. }
  609. root = value64(node, i);
  610. top = 0;
  611. }
  612. if (i < 0 || le64_to_cpu(node->keys[i]) != key)
  613. i++;
  614. *index = i;
  615. return 0;
  616. }
  617. static bool need_insert(struct btree_node *node, uint64_t *keys,
  618. unsigned level, unsigned index)
  619. {
  620. return ((index >= le32_to_cpu(node->header.nr_entries)) ||
  621. (le64_to_cpu(node->keys[index]) != keys[level]));
  622. }
  623. static int insert(struct dm_btree_info *info, dm_block_t root,
  624. uint64_t *keys, void *value, dm_block_t *new_root,
  625. int *inserted)
  626. __dm_written_to_disk(value)
  627. {
  628. int r;
  629. unsigned level, index = -1, last_level = info->levels - 1;
  630. dm_block_t block = root;
  631. struct shadow_spine spine;
  632. struct btree_node *n;
  633. struct dm_btree_value_type le64_type;
  634. init_le64_type(info->tm, &le64_type);
  635. init_shadow_spine(&spine, info);
  636. for (level = 0; level < (info->levels - 1); level++) {
  637. r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
  638. if (r < 0)
  639. goto bad;
  640. n = dm_block_data(shadow_current(&spine));
  641. if (need_insert(n, keys, level, index)) {
  642. dm_block_t new_tree;
  643. __le64 new_le;
  644. r = dm_btree_empty(info, &new_tree);
  645. if (r < 0)
  646. goto bad;
  647. new_le = cpu_to_le64(new_tree);
  648. __dm_bless_for_disk(&new_le);
  649. r = insert_at(sizeof(uint64_t), n, index,
  650. keys[level], &new_le);
  651. if (r)
  652. goto bad;
  653. }
  654. if (level < last_level)
  655. block = value64(n, index);
  656. }
  657. r = btree_insert_raw(&spine, block, &info->value_type,
  658. keys[level], &index);
  659. if (r < 0)
  660. goto bad;
  661. n = dm_block_data(shadow_current(&spine));
  662. if (need_insert(n, keys, level, index)) {
  663. if (inserted)
  664. *inserted = 1;
  665. r = insert_at(info->value_type.size, n, index,
  666. keys[level], value);
  667. if (r)
  668. goto bad_unblessed;
  669. } else {
  670. if (inserted)
  671. *inserted = 0;
  672. if (info->value_type.dec &&
  673. (!info->value_type.equal ||
  674. !info->value_type.equal(
  675. info->value_type.context,
  676. value_ptr(n, index),
  677. value))) {
  678. info->value_type.dec(info->value_type.context,
  679. value_ptr(n, index));
  680. }
  681. memcpy_disk(value_ptr(n, index),
  682. value, info->value_type.size);
  683. }
  684. *new_root = shadow_root(&spine);
  685. exit_shadow_spine(&spine);
  686. return 0;
  687. bad:
  688. __dm_unbless_for_disk(value);
  689. bad_unblessed:
  690. exit_shadow_spine(&spine);
  691. return r;
  692. }
  693. int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
  694. uint64_t *keys, void *value, dm_block_t *new_root)
  695. __dm_written_to_disk(value)
  696. {
  697. return insert(info, root, keys, value, new_root, NULL);
  698. }
  699. EXPORT_SYMBOL_GPL(dm_btree_insert);
  700. int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
  701. uint64_t *keys, void *value, dm_block_t *new_root,
  702. int *inserted)
  703. __dm_written_to_disk(value)
  704. {
  705. return insert(info, root, keys, value, new_root, inserted);
  706. }
  707. EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
  708. /*----------------------------------------------------------------*/
  709. static int find_key(struct ro_spine *s, dm_block_t block, bool find_highest,
  710. uint64_t *result_key, dm_block_t *next_block)
  711. {
  712. int i, r;
  713. uint32_t flags;
  714. do {
  715. r = ro_step(s, block);
  716. if (r < 0)
  717. return r;
  718. flags = le32_to_cpu(ro_node(s)->header.flags);
  719. i = le32_to_cpu(ro_node(s)->header.nr_entries);
  720. if (!i)
  721. return -ENODATA;
  722. else
  723. i--;
  724. if (find_highest)
  725. *result_key = le64_to_cpu(ro_node(s)->keys[i]);
  726. else
  727. *result_key = le64_to_cpu(ro_node(s)->keys[0]);
  728. if (next_block || flags & INTERNAL_NODE) {
  729. if (find_highest)
  730. block = value64(ro_node(s), i);
  731. else
  732. block = value64(ro_node(s), 0);
  733. }
  734. } while (flags & INTERNAL_NODE);
  735. if (next_block)
  736. *next_block = block;
  737. return 0;
  738. }
  739. static int dm_btree_find_key(struct dm_btree_info *info, dm_block_t root,
  740. bool find_highest, uint64_t *result_keys)
  741. {
  742. int r = 0, count = 0, level;
  743. struct ro_spine spine;
  744. init_ro_spine(&spine, info);
  745. for (level = 0; level < info->levels; level++) {
  746. r = find_key(&spine, root, find_highest, result_keys + level,
  747. level == info->levels - 1 ? NULL : &root);
  748. if (r == -ENODATA) {
  749. r = 0;
  750. break;
  751. } else if (r)
  752. break;
  753. count++;
  754. }
  755. exit_ro_spine(&spine);
  756. return r ? r : count;
  757. }
  758. int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
  759. uint64_t *result_keys)
  760. {
  761. return dm_btree_find_key(info, root, true, result_keys);
  762. }
  763. EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
  764. int dm_btree_find_lowest_key(struct dm_btree_info *info, dm_block_t root,
  765. uint64_t *result_keys)
  766. {
  767. return dm_btree_find_key(info, root, false, result_keys);
  768. }
  769. EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key);
  770. /*----------------------------------------------------------------*/
  771. /*
  772. * FIXME: We shouldn't use a recursive algorithm when we have limited stack
  773. * space. Also this only works for single level trees.
  774. */
  775. static int walk_node(struct dm_btree_info *info, dm_block_t block,
  776. int (*fn)(void *context, uint64_t *keys, void *leaf),
  777. void *context)
  778. {
  779. int r;
  780. unsigned i, nr;
  781. struct dm_block *node;
  782. struct btree_node *n;
  783. uint64_t keys;
  784. r = bn_read_lock(info, block, &node);
  785. if (r)
  786. return r;
  787. n = dm_block_data(node);
  788. nr = le32_to_cpu(n->header.nr_entries);
  789. for (i = 0; i < nr; i++) {
  790. if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
  791. r = walk_node(info, value64(n, i), fn, context);
  792. if (r)
  793. goto out;
  794. } else {
  795. keys = le64_to_cpu(*key_ptr(n, i));
  796. r = fn(context, &keys, value_ptr(n, i));
  797. if (r)
  798. goto out;
  799. }
  800. }
  801. out:
  802. dm_tm_unlock(info->tm, node);
  803. return r;
  804. }
  805. int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
  806. int (*fn)(void *context, uint64_t *keys, void *leaf),
  807. void *context)
  808. {
  809. BUG_ON(info->levels > 1);
  810. return walk_node(info, root, fn, context);
  811. }
  812. EXPORT_SYMBOL_GPL(dm_btree_walk);
  813. /*----------------------------------------------------------------*/
  814. static void prefetch_values(struct dm_btree_cursor *c)
  815. {
  816. unsigned i, nr;
  817. __le64 value_le;
  818. struct cursor_node *n = c->nodes + c->depth - 1;
  819. struct btree_node *bn = dm_block_data(n->b);
  820. struct dm_block_manager *bm = dm_tm_get_bm(c->info->tm);
  821. BUG_ON(c->info->value_type.size != sizeof(value_le));
  822. nr = le32_to_cpu(bn->header.nr_entries);
  823. for (i = 0; i < nr; i++) {
  824. memcpy(&value_le, value_ptr(bn, i), sizeof(value_le));
  825. dm_bm_prefetch(bm, le64_to_cpu(value_le));
  826. }
  827. }
  828. static bool leaf_node(struct dm_btree_cursor *c)
  829. {
  830. struct cursor_node *n = c->nodes + c->depth - 1;
  831. struct btree_node *bn = dm_block_data(n->b);
  832. return le32_to_cpu(bn->header.flags) & LEAF_NODE;
  833. }
  834. static int push_node(struct dm_btree_cursor *c, dm_block_t b)
  835. {
  836. int r;
  837. struct cursor_node *n = c->nodes + c->depth;
  838. if (c->depth >= DM_BTREE_CURSOR_MAX_DEPTH - 1) {
  839. DMERR("couldn't push cursor node, stack depth too high");
  840. return -EINVAL;
  841. }
  842. r = bn_read_lock(c->info, b, &n->b);
  843. if (r)
  844. return r;
  845. n->index = 0;
  846. c->depth++;
  847. if (c->prefetch_leaves || !leaf_node(c))
  848. prefetch_values(c);
  849. return 0;
  850. }
  851. static void pop_node(struct dm_btree_cursor *c)
  852. {
  853. c->depth--;
  854. unlock_block(c->info, c->nodes[c->depth].b);
  855. }
  856. static int inc_or_backtrack(struct dm_btree_cursor *c)
  857. {
  858. struct cursor_node *n;
  859. struct btree_node *bn;
  860. for (;;) {
  861. if (!c->depth)
  862. return -ENODATA;
  863. n = c->nodes + c->depth - 1;
  864. bn = dm_block_data(n->b);
  865. n->index++;
  866. if (n->index < le32_to_cpu(bn->header.nr_entries))
  867. break;
  868. pop_node(c);
  869. }
  870. return 0;
  871. }
  872. static int find_leaf(struct dm_btree_cursor *c)
  873. {
  874. int r = 0;
  875. struct cursor_node *n;
  876. struct btree_node *bn;
  877. __le64 value_le;
  878. for (;;) {
  879. n = c->nodes + c->depth - 1;
  880. bn = dm_block_data(n->b);
  881. if (le32_to_cpu(bn->header.flags) & LEAF_NODE)
  882. break;
  883. memcpy(&value_le, value_ptr(bn, n->index), sizeof(value_le));
  884. r = push_node(c, le64_to_cpu(value_le));
  885. if (r) {
  886. DMERR("push_node failed");
  887. break;
  888. }
  889. }
  890. if (!r && (le32_to_cpu(bn->header.nr_entries) == 0))
  891. return -ENODATA;
  892. return r;
  893. }
  894. int dm_btree_cursor_begin(struct dm_btree_info *info, dm_block_t root,
  895. bool prefetch_leaves, struct dm_btree_cursor *c)
  896. {
  897. int r;
  898. c->info = info;
  899. c->root = root;
  900. c->depth = 0;
  901. c->prefetch_leaves = prefetch_leaves;
  902. r = push_node(c, root);
  903. if (r)
  904. return r;
  905. return find_leaf(c);
  906. }
  907. EXPORT_SYMBOL_GPL(dm_btree_cursor_begin);
  908. void dm_btree_cursor_end(struct dm_btree_cursor *c)
  909. {
  910. while (c->depth)
  911. pop_node(c);
  912. }
  913. EXPORT_SYMBOL_GPL(dm_btree_cursor_end);
  914. int dm_btree_cursor_next(struct dm_btree_cursor *c)
  915. {
  916. int r = inc_or_backtrack(c);
  917. if (!r) {
  918. r = find_leaf(c);
  919. if (r)
  920. DMERR("find_leaf failed");
  921. }
  922. return r;
  923. }
  924. EXPORT_SYMBOL_GPL(dm_btree_cursor_next);
  925. int dm_btree_cursor_skip(struct dm_btree_cursor *c, uint32_t count)
  926. {
  927. int r = 0;
  928. while (count-- && !r)
  929. r = dm_btree_cursor_next(c);
  930. return r;
  931. }
  932. EXPORT_SYMBOL_GPL(dm_btree_cursor_skip);
  933. int dm_btree_cursor_get_value(struct dm_btree_cursor *c, uint64_t *key, void *value_le)
  934. {
  935. if (c->depth) {
  936. struct cursor_node *n = c->nodes + c->depth - 1;
  937. struct btree_node *bn = dm_block_data(n->b);
  938. if (le32_to_cpu(bn->header.flags) & INTERNAL_NODE)
  939. return -EINVAL;
  940. *key = le64_to_cpu(*key_ptr(bn, n->index));
  941. memcpy(value_le, value_ptr(bn, n->index), c->info->value_type.size);
  942. return 0;
  943. } else
  944. return -ENODATA;
  945. }
  946. EXPORT_SYMBOL_GPL(dm_btree_cursor_get_value);