dm-btree.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-btree-internal.h"
  8. #include "dm-space-map.h"
  9. #include "dm-transaction-manager.h"
  10. #include <linux/export.h>
  11. #include <linux/device-mapper.h>
  12. #define DM_MSG_PREFIX "btree"
  13. /*
  14. *--------------------------------------------------------------
  15. * Array manipulation
  16. *--------------------------------------------------------------
  17. */
  18. static void memcpy_disk(void *dest, const void *src, size_t len)
  19. __dm_written_to_disk(src)
  20. {
  21. memcpy(dest, src, len);
  22. __dm_unbless_for_disk(src);
  23. }
  24. static void array_insert(void *base, size_t elt_size, unsigned int nr_elts,
  25. unsigned int index, void *elt)
  26. __dm_written_to_disk(elt)
  27. {
  28. if (index < nr_elts)
  29. memmove(base + (elt_size * (index + 1)),
  30. base + (elt_size * index),
  31. (nr_elts - index) * elt_size);
  32. memcpy_disk(base + (elt_size * index), elt, elt_size);
  33. }
  34. /*----------------------------------------------------------------*/
  35. /* makes the assumption that no two keys are the same. */
  36. static int bsearch(struct btree_node *n, uint64_t key, int want_hi)
  37. {
  38. int lo = -1, hi = le32_to_cpu(n->header.nr_entries);
  39. while (hi - lo > 1) {
  40. int mid = lo + ((hi - lo) / 2);
  41. uint64_t mid_key = le64_to_cpu(n->keys[mid]);
  42. if (mid_key == key)
  43. return mid;
  44. if (mid_key < key)
  45. lo = mid;
  46. else
  47. hi = mid;
  48. }
  49. return want_hi ? hi : lo;
  50. }
  51. int lower_bound(struct btree_node *n, uint64_t key)
  52. {
  53. return bsearch(n, key, 0);
  54. }
  55. static int upper_bound(struct btree_node *n, uint64_t key)
  56. {
  57. return bsearch(n, key, 1);
  58. }
  59. void inc_children(struct dm_transaction_manager *tm, struct btree_node *n,
  60. struct dm_btree_value_type *vt)
  61. {
  62. uint32_t nr_entries = le32_to_cpu(n->header.nr_entries);
  63. if (le32_to_cpu(n->header.flags) & INTERNAL_NODE)
  64. dm_tm_with_runs(tm, value_ptr(n, 0), nr_entries, dm_tm_inc_range);
  65. else if (vt->inc)
  66. vt->inc(vt->context, value_ptr(n, 0), nr_entries);
  67. }
  68. static int insert_at(size_t value_size, struct btree_node *node, unsigned int 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. uint32_t max_entries = le32_to_cpu(node->header.max_entries);
  74. __le64 key_le = cpu_to_le64(key);
  75. if (index > nr_entries ||
  76. index >= max_entries ||
  77. nr_entries >= max_entries) {
  78. DMERR("too many entries in btree node for insert");
  79. __dm_unbless_for_disk(value);
  80. return -ENOMEM;
  81. }
  82. __dm_bless_for_disk(&key_le);
  83. array_insert(node->keys, sizeof(*node->keys), nr_entries, index, &key_le);
  84. array_insert(value_base(node), value_size, nr_entries, index, value);
  85. node->header.nr_entries = cpu_to_le32(nr_entries + 1);
  86. return 0;
  87. }
  88. /*----------------------------------------------------------------*/
  89. /*
  90. * We want 3n entries (for some n). This works more nicely for repeated
  91. * insert remove loops than (2n + 1).
  92. */
  93. static uint32_t calc_max_entries(size_t value_size, size_t block_size)
  94. {
  95. uint32_t total, n;
  96. size_t elt_size = sizeof(uint64_t) + value_size; /* key + value */
  97. block_size -= sizeof(struct node_header);
  98. total = block_size / elt_size;
  99. n = total / 3; /* rounds down */
  100. return 3 * n;
  101. }
  102. int dm_btree_empty(struct dm_btree_info *info, dm_block_t *root)
  103. {
  104. int r;
  105. struct dm_block *b;
  106. struct btree_node *n;
  107. size_t block_size;
  108. uint32_t max_entries;
  109. r = new_block(info, &b);
  110. if (r < 0)
  111. return r;
  112. block_size = dm_bm_block_size(dm_tm_get_bm(info->tm));
  113. max_entries = calc_max_entries(info->value_type.size, block_size);
  114. n = dm_block_data(b);
  115. memset(n, 0, block_size);
  116. n->header.flags = cpu_to_le32(LEAF_NODE);
  117. n->header.nr_entries = cpu_to_le32(0);
  118. n->header.max_entries = cpu_to_le32(max_entries);
  119. n->header.value_size = cpu_to_le32(info->value_type.size);
  120. *root = dm_block_location(b);
  121. unlock_block(info, b);
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(dm_btree_empty);
  125. /*----------------------------------------------------------------*/
  126. /*
  127. * Deletion uses a recursive algorithm, since we have limited stack space
  128. * we explicitly manage our own stack on the heap.
  129. */
  130. #define MAX_SPINE_DEPTH 64
  131. struct frame {
  132. struct dm_block *b;
  133. struct btree_node *n;
  134. unsigned int level;
  135. unsigned int nr_children;
  136. unsigned int current_child;
  137. };
  138. struct del_stack {
  139. struct dm_btree_info *info;
  140. struct dm_transaction_manager *tm;
  141. int top;
  142. struct frame spine[MAX_SPINE_DEPTH];
  143. };
  144. static int top_frame(struct del_stack *s, struct frame **f)
  145. {
  146. if (s->top < 0) {
  147. DMERR("btree deletion stack empty");
  148. return -EINVAL;
  149. }
  150. *f = s->spine + s->top;
  151. return 0;
  152. }
  153. static int unprocessed_frames(struct del_stack *s)
  154. {
  155. return s->top >= 0;
  156. }
  157. static void prefetch_children(struct del_stack *s, struct frame *f)
  158. {
  159. unsigned int i;
  160. struct dm_block_manager *bm = dm_tm_get_bm(s->tm);
  161. for (i = 0; i < f->nr_children; i++)
  162. dm_bm_prefetch(bm, value64(f->n, i));
  163. }
  164. static bool is_internal_level(struct dm_btree_info *info, struct frame *f)
  165. {
  166. return f->level < (info->levels - 1);
  167. }
  168. static int push_frame(struct del_stack *s, dm_block_t b, unsigned int level)
  169. {
  170. int r;
  171. uint32_t ref_count;
  172. if (s->top >= MAX_SPINE_DEPTH - 1) {
  173. DMERR("btree deletion stack out of memory");
  174. return -ENOMEM;
  175. }
  176. r = dm_tm_ref(s->tm, b, &ref_count);
  177. if (r)
  178. return r;
  179. if (ref_count > 1)
  180. /*
  181. * This is a shared node, so we can just decrement it's
  182. * reference counter and leave the children.
  183. */
  184. dm_tm_dec(s->tm, b);
  185. else {
  186. uint32_t flags;
  187. struct frame *f = s->spine + ++s->top;
  188. r = dm_tm_read_lock(s->tm, b, &btree_node_validator, &f->b);
  189. if (r) {
  190. s->top--;
  191. return r;
  192. }
  193. f->n = dm_block_data(f->b);
  194. f->level = level;
  195. f->nr_children = le32_to_cpu(f->n->header.nr_entries);
  196. f->current_child = 0;
  197. flags = le32_to_cpu(f->n->header.flags);
  198. if (flags & INTERNAL_NODE || is_internal_level(s->info, f))
  199. prefetch_children(s, f);
  200. }
  201. return 0;
  202. }
  203. static void pop_frame(struct del_stack *s)
  204. {
  205. struct frame *f = s->spine + s->top--;
  206. dm_tm_dec(s->tm, dm_block_location(f->b));
  207. dm_tm_unlock(s->tm, f->b);
  208. }
  209. static void unlock_all_frames(struct del_stack *s)
  210. {
  211. struct frame *f;
  212. while (unprocessed_frames(s)) {
  213. f = s->spine + s->top--;
  214. dm_tm_unlock(s->tm, f->b);
  215. }
  216. }
  217. int dm_btree_del(struct dm_btree_info *info, dm_block_t root)
  218. {
  219. int r;
  220. struct del_stack *s;
  221. /*
  222. * dm_btree_del() is called via an ioctl, as such should be
  223. * considered an FS op. We can't recurse back into the FS, so we
  224. * allocate GFP_NOFS.
  225. */
  226. s = kmalloc(sizeof(*s), GFP_NOFS);
  227. if (!s)
  228. return -ENOMEM;
  229. s->info = info;
  230. s->tm = info->tm;
  231. s->top = -1;
  232. r = push_frame(s, root, 0);
  233. if (r)
  234. goto out;
  235. while (unprocessed_frames(s)) {
  236. uint32_t flags;
  237. struct frame *f;
  238. dm_block_t b;
  239. r = top_frame(s, &f);
  240. if (r)
  241. goto out;
  242. if (f->current_child >= f->nr_children) {
  243. pop_frame(s);
  244. continue;
  245. }
  246. flags = le32_to_cpu(f->n->header.flags);
  247. if (flags & INTERNAL_NODE) {
  248. b = value64(f->n, f->current_child);
  249. f->current_child++;
  250. r = push_frame(s, b, f->level);
  251. if (r)
  252. goto out;
  253. } else if (is_internal_level(info, f)) {
  254. b = value64(f->n, f->current_child);
  255. f->current_child++;
  256. r = push_frame(s, b, f->level + 1);
  257. if (r)
  258. goto out;
  259. } else {
  260. if (info->value_type.dec)
  261. info->value_type.dec(info->value_type.context,
  262. value_ptr(f->n, 0), f->nr_children);
  263. pop_frame(s);
  264. }
  265. }
  266. out:
  267. if (r) {
  268. /* cleanup all frames of del_stack */
  269. unlock_all_frames(s);
  270. }
  271. kfree(s);
  272. return r;
  273. }
  274. EXPORT_SYMBOL_GPL(dm_btree_del);
  275. /*----------------------------------------------------------------*/
  276. static int btree_lookup_raw(struct ro_spine *s, dm_block_t block, uint64_t key,
  277. int (*search_fn)(struct btree_node *, uint64_t),
  278. uint64_t *result_key, void *v, size_t value_size)
  279. {
  280. int i, r;
  281. uint32_t flags, nr_entries;
  282. do {
  283. r = ro_step(s, block);
  284. if (r < 0)
  285. return r;
  286. i = search_fn(ro_node(s), key);
  287. flags = le32_to_cpu(ro_node(s)->header.flags);
  288. nr_entries = le32_to_cpu(ro_node(s)->header.nr_entries);
  289. if (i < 0 || i >= nr_entries)
  290. return -ENODATA;
  291. if (flags & INTERNAL_NODE)
  292. block = value64(ro_node(s), i);
  293. } while (!(flags & LEAF_NODE));
  294. *result_key = le64_to_cpu(ro_node(s)->keys[i]);
  295. if (v)
  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 int 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 int 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. /*
  408. * Copies entries from one region of a btree node to another. The regions
  409. * must not overlap.
  410. */
  411. static void copy_entries(struct btree_node *dest, unsigned int dest_offset,
  412. struct btree_node *src, unsigned int src_offset,
  413. unsigned int count)
  414. {
  415. size_t value_size = le32_to_cpu(dest->header.value_size);
  416. memcpy(dest->keys + dest_offset, src->keys + src_offset, count * sizeof(uint64_t));
  417. memcpy(value_ptr(dest, dest_offset), value_ptr(src, src_offset), count * value_size);
  418. }
  419. /*
  420. * Moves entries from one region fo a btree node to another. The regions
  421. * may overlap.
  422. */
  423. static void move_entries(struct btree_node *dest, unsigned int dest_offset,
  424. struct btree_node *src, unsigned int src_offset,
  425. unsigned int count)
  426. {
  427. size_t value_size = le32_to_cpu(dest->header.value_size);
  428. memmove(dest->keys + dest_offset, src->keys + src_offset, count * sizeof(uint64_t));
  429. memmove(value_ptr(dest, dest_offset), value_ptr(src, src_offset), count * value_size);
  430. }
  431. /*
  432. * Erases the first 'count' entries of a btree node, shifting following
  433. * entries down into their place.
  434. */
  435. static void shift_down(struct btree_node *n, unsigned int count)
  436. {
  437. move_entries(n, 0, n, count, le32_to_cpu(n->header.nr_entries) - count);
  438. }
  439. /*
  440. * Moves entries in a btree node up 'count' places, making space for
  441. * new entries at the start of the node.
  442. */
  443. static void shift_up(struct btree_node *n, unsigned int count)
  444. {
  445. move_entries(n, count, n, 0, le32_to_cpu(n->header.nr_entries));
  446. }
  447. /*
  448. * Redistributes entries between two btree nodes to make them
  449. * have similar numbers of entries.
  450. */
  451. static void redistribute2(struct btree_node *left, struct btree_node *right)
  452. {
  453. unsigned int nr_left = le32_to_cpu(left->header.nr_entries);
  454. unsigned int nr_right = le32_to_cpu(right->header.nr_entries);
  455. unsigned int total = nr_left + nr_right;
  456. unsigned int target_left = total / 2;
  457. unsigned int target_right = total - target_left;
  458. if (nr_left < target_left) {
  459. unsigned int delta = target_left - nr_left;
  460. copy_entries(left, nr_left, right, 0, delta);
  461. shift_down(right, delta);
  462. } else if (nr_left > target_left) {
  463. unsigned int delta = nr_left - target_left;
  464. if (nr_right)
  465. shift_up(right, delta);
  466. copy_entries(right, 0, left, target_left, delta);
  467. }
  468. left->header.nr_entries = cpu_to_le32(target_left);
  469. right->header.nr_entries = cpu_to_le32(target_right);
  470. }
  471. /*
  472. * Redistribute entries between three nodes. Assumes the central
  473. * node is empty.
  474. */
  475. static void redistribute3(struct btree_node *left, struct btree_node *center,
  476. struct btree_node *right)
  477. {
  478. unsigned int nr_left = le32_to_cpu(left->header.nr_entries);
  479. unsigned int nr_center = le32_to_cpu(center->header.nr_entries);
  480. unsigned int nr_right = le32_to_cpu(right->header.nr_entries);
  481. unsigned int total, target_left, target_center, target_right;
  482. BUG_ON(nr_center);
  483. total = nr_left + nr_right;
  484. target_left = total / 3;
  485. target_center = (total - target_left) / 2;
  486. target_right = (total - target_left - target_center);
  487. if (nr_left < target_left) {
  488. unsigned int left_short = target_left - nr_left;
  489. copy_entries(left, nr_left, right, 0, left_short);
  490. copy_entries(center, 0, right, left_short, target_center);
  491. shift_down(right, nr_right - target_right);
  492. } else if (nr_left < (target_left + target_center)) {
  493. unsigned int left_to_center = nr_left - target_left;
  494. copy_entries(center, 0, left, target_left, left_to_center);
  495. copy_entries(center, left_to_center, right, 0, target_center - left_to_center);
  496. shift_down(right, nr_right - target_right);
  497. } else {
  498. unsigned int right_short = target_right - nr_right;
  499. shift_up(right, right_short);
  500. copy_entries(right, 0, left, nr_left - right_short, right_short);
  501. copy_entries(center, 0, left, target_left, nr_left - target_left);
  502. }
  503. left->header.nr_entries = cpu_to_le32(target_left);
  504. center->header.nr_entries = cpu_to_le32(target_center);
  505. right->header.nr_entries = cpu_to_le32(target_right);
  506. }
  507. /*
  508. * Splits a node by creating a sibling node and shifting half the nodes
  509. * contents across. Assumes there is a parent node, and it has room for
  510. * another child.
  511. *
  512. * Before:
  513. * +--------+
  514. * | Parent |
  515. * +--------+
  516. * |
  517. * v
  518. * +----------+
  519. * | A ++++++ |
  520. * +----------+
  521. *
  522. *
  523. * After:
  524. * +--------+
  525. * | Parent |
  526. * +--------+
  527. * | |
  528. * v +------+
  529. * +---------+ |
  530. * | A* +++ | v
  531. * +---------+ +-------+
  532. * | B +++ |
  533. * +-------+
  534. *
  535. * Where A* is a shadow of A.
  536. */
  537. static int split_one_into_two(struct shadow_spine *s, unsigned int parent_index,
  538. struct dm_btree_value_type *vt, uint64_t key)
  539. {
  540. int r;
  541. struct dm_block *left, *right, *parent;
  542. struct btree_node *ln, *rn, *pn;
  543. __le64 location;
  544. left = shadow_current(s);
  545. r = new_block(s->info, &right);
  546. if (r < 0)
  547. return r;
  548. ln = dm_block_data(left);
  549. rn = dm_block_data(right);
  550. rn->header.flags = ln->header.flags;
  551. rn->header.nr_entries = cpu_to_le32(0);
  552. rn->header.max_entries = ln->header.max_entries;
  553. rn->header.value_size = ln->header.value_size;
  554. redistribute2(ln, rn);
  555. /* patch up the parent */
  556. parent = shadow_parent(s);
  557. pn = dm_block_data(parent);
  558. location = cpu_to_le64(dm_block_location(right));
  559. __dm_bless_for_disk(&location);
  560. r = insert_at(sizeof(__le64), pn, parent_index + 1,
  561. le64_to_cpu(rn->keys[0]), &location);
  562. if (r) {
  563. unlock_block(s->info, right);
  564. return r;
  565. }
  566. /* patch up the spine */
  567. if (key < le64_to_cpu(rn->keys[0])) {
  568. unlock_block(s->info, right);
  569. s->nodes[1] = left;
  570. } else {
  571. unlock_block(s->info, left);
  572. s->nodes[1] = right;
  573. }
  574. return 0;
  575. }
  576. /*
  577. * We often need to modify a sibling node. This function shadows a particular
  578. * child of the given parent node. Making sure to update the parent to point
  579. * to the new shadow.
  580. */
  581. static int shadow_child(struct dm_btree_info *info, struct dm_btree_value_type *vt,
  582. struct btree_node *parent, unsigned int index,
  583. struct dm_block **result)
  584. {
  585. int r, inc;
  586. dm_block_t root;
  587. struct btree_node *node;
  588. root = value64(parent, index);
  589. r = dm_tm_shadow_block(info->tm, root, &btree_node_validator,
  590. result, &inc);
  591. if (r)
  592. return r;
  593. node = dm_block_data(*result);
  594. if (inc)
  595. inc_children(info->tm, node, vt);
  596. *((__le64 *) value_ptr(parent, index)) =
  597. cpu_to_le64(dm_block_location(*result));
  598. return 0;
  599. }
  600. /*
  601. * Splits two nodes into three. This is more work, but results in fuller
  602. * nodes, so saves metadata space.
  603. */
  604. static int split_two_into_three(struct shadow_spine *s, unsigned int parent_index,
  605. struct dm_btree_value_type *vt, uint64_t key)
  606. {
  607. int r;
  608. unsigned int middle_index;
  609. struct dm_block *left, *middle, *right, *parent;
  610. struct btree_node *ln, *rn, *mn, *pn;
  611. __le64 location;
  612. parent = shadow_parent(s);
  613. pn = dm_block_data(parent);
  614. if (parent_index == 0) {
  615. middle_index = 1;
  616. left = shadow_current(s);
  617. r = shadow_child(s->info, vt, pn, parent_index + 1, &right);
  618. if (r)
  619. return r;
  620. } else {
  621. middle_index = parent_index;
  622. right = shadow_current(s);
  623. r = shadow_child(s->info, vt, pn, parent_index - 1, &left);
  624. if (r)
  625. return r;
  626. }
  627. r = new_block(s->info, &middle);
  628. if (r < 0)
  629. return r;
  630. ln = dm_block_data(left);
  631. mn = dm_block_data(middle);
  632. rn = dm_block_data(right);
  633. mn->header.nr_entries = cpu_to_le32(0);
  634. mn->header.flags = ln->header.flags;
  635. mn->header.max_entries = ln->header.max_entries;
  636. mn->header.value_size = ln->header.value_size;
  637. redistribute3(ln, mn, rn);
  638. /* patch up the parent */
  639. pn->keys[middle_index] = rn->keys[0];
  640. location = cpu_to_le64(dm_block_location(middle));
  641. __dm_bless_for_disk(&location);
  642. r = insert_at(sizeof(__le64), pn, middle_index,
  643. le64_to_cpu(mn->keys[0]), &location);
  644. if (r) {
  645. if (shadow_current(s) != left)
  646. unlock_block(s->info, left);
  647. unlock_block(s->info, middle);
  648. if (shadow_current(s) != right)
  649. unlock_block(s->info, right);
  650. return r;
  651. }
  652. /* patch up the spine */
  653. if (key < le64_to_cpu(mn->keys[0])) {
  654. unlock_block(s->info, middle);
  655. unlock_block(s->info, right);
  656. s->nodes[1] = left;
  657. } else if (key < le64_to_cpu(rn->keys[0])) {
  658. unlock_block(s->info, left);
  659. unlock_block(s->info, right);
  660. s->nodes[1] = middle;
  661. } else {
  662. unlock_block(s->info, left);
  663. unlock_block(s->info, middle);
  664. s->nodes[1] = right;
  665. }
  666. return 0;
  667. }
  668. /*----------------------------------------------------------------*/
  669. /*
  670. * Splits a node by creating two new children beneath the given node.
  671. *
  672. * Before:
  673. * +----------+
  674. * | A ++++++ |
  675. * +----------+
  676. *
  677. *
  678. * After:
  679. * +------------+
  680. * | A (shadow) |
  681. * +------------+
  682. * | |
  683. * +------+ +----+
  684. * | |
  685. * v v
  686. * +-------+ +-------+
  687. * | B +++ | | C +++ |
  688. * +-------+ +-------+
  689. */
  690. static int btree_split_beneath(struct shadow_spine *s, uint64_t key)
  691. {
  692. int r;
  693. size_t size;
  694. unsigned int nr_left, nr_right;
  695. struct dm_block *left, *right, *new_parent;
  696. struct btree_node *pn, *ln, *rn;
  697. __le64 val;
  698. new_parent = shadow_current(s);
  699. pn = dm_block_data(new_parent);
  700. size = le32_to_cpu(pn->header.flags) & INTERNAL_NODE ?
  701. sizeof(__le64) : s->info->value_type.size;
  702. /* create & init the left block */
  703. r = new_block(s->info, &left);
  704. if (r < 0)
  705. return r;
  706. ln = dm_block_data(left);
  707. nr_left = le32_to_cpu(pn->header.nr_entries) / 2;
  708. ln->header.flags = pn->header.flags;
  709. ln->header.nr_entries = cpu_to_le32(nr_left);
  710. ln->header.max_entries = pn->header.max_entries;
  711. ln->header.value_size = pn->header.value_size;
  712. memcpy(ln->keys, pn->keys, nr_left * sizeof(pn->keys[0]));
  713. memcpy(value_ptr(ln, 0), value_ptr(pn, 0), nr_left * size);
  714. /* create & init the right block */
  715. r = new_block(s->info, &right);
  716. if (r < 0) {
  717. unlock_block(s->info, left);
  718. return r;
  719. }
  720. rn = dm_block_data(right);
  721. nr_right = le32_to_cpu(pn->header.nr_entries) - nr_left;
  722. rn->header.flags = pn->header.flags;
  723. rn->header.nr_entries = cpu_to_le32(nr_right);
  724. rn->header.max_entries = pn->header.max_entries;
  725. rn->header.value_size = pn->header.value_size;
  726. memcpy(rn->keys, pn->keys + nr_left, nr_right * sizeof(pn->keys[0]));
  727. memcpy(value_ptr(rn, 0), value_ptr(pn, nr_left),
  728. nr_right * size);
  729. /* new_parent should just point to l and r now */
  730. pn->header.flags = cpu_to_le32(INTERNAL_NODE);
  731. pn->header.nr_entries = cpu_to_le32(2);
  732. pn->header.max_entries = cpu_to_le32(
  733. calc_max_entries(sizeof(__le64),
  734. dm_bm_block_size(
  735. dm_tm_get_bm(s->info->tm))));
  736. pn->header.value_size = cpu_to_le32(sizeof(__le64));
  737. val = cpu_to_le64(dm_block_location(left));
  738. __dm_bless_for_disk(&val);
  739. pn->keys[0] = ln->keys[0];
  740. memcpy_disk(value_ptr(pn, 0), &val, sizeof(__le64));
  741. val = cpu_to_le64(dm_block_location(right));
  742. __dm_bless_for_disk(&val);
  743. pn->keys[1] = rn->keys[0];
  744. memcpy_disk(value_ptr(pn, 1), &val, sizeof(__le64));
  745. unlock_block(s->info, left);
  746. unlock_block(s->info, right);
  747. return 0;
  748. }
  749. /*----------------------------------------------------------------*/
  750. /*
  751. * Redistributes a node's entries with its left sibling.
  752. */
  753. static int rebalance_left(struct shadow_spine *s, struct dm_btree_value_type *vt,
  754. unsigned int parent_index, uint64_t key)
  755. {
  756. int r;
  757. struct dm_block *sib;
  758. struct btree_node *left, *right, *parent = dm_block_data(shadow_parent(s));
  759. r = shadow_child(s->info, vt, parent, parent_index - 1, &sib);
  760. if (r)
  761. return r;
  762. left = dm_block_data(sib);
  763. right = dm_block_data(shadow_current(s));
  764. redistribute2(left, right);
  765. *key_ptr(parent, parent_index) = right->keys[0];
  766. if (key < le64_to_cpu(right->keys[0])) {
  767. unlock_block(s->info, s->nodes[1]);
  768. s->nodes[1] = sib;
  769. } else {
  770. unlock_block(s->info, sib);
  771. }
  772. return 0;
  773. }
  774. /*
  775. * Redistributes a nodes entries with its right sibling.
  776. */
  777. static int rebalance_right(struct shadow_spine *s, struct dm_btree_value_type *vt,
  778. unsigned int parent_index, uint64_t key)
  779. {
  780. int r;
  781. struct dm_block *sib;
  782. struct btree_node *left, *right, *parent = dm_block_data(shadow_parent(s));
  783. r = shadow_child(s->info, vt, parent, parent_index + 1, &sib);
  784. if (r)
  785. return r;
  786. left = dm_block_data(shadow_current(s));
  787. right = dm_block_data(sib);
  788. redistribute2(left, right);
  789. *key_ptr(parent, parent_index + 1) = right->keys[0];
  790. if (key < le64_to_cpu(right->keys[0])) {
  791. unlock_block(s->info, sib);
  792. } else {
  793. unlock_block(s->info, s->nodes[1]);
  794. s->nodes[1] = sib;
  795. }
  796. return 0;
  797. }
  798. /*
  799. * Returns the number of spare entries in a node.
  800. */
  801. static int get_node_free_space(struct dm_btree_info *info, dm_block_t b, unsigned int *space)
  802. {
  803. int r;
  804. unsigned int nr_entries;
  805. struct dm_block *block;
  806. struct btree_node *node;
  807. r = bn_read_lock(info, b, &block);
  808. if (r)
  809. return r;
  810. node = dm_block_data(block);
  811. nr_entries = le32_to_cpu(node->header.nr_entries);
  812. *space = le32_to_cpu(node->header.max_entries) - nr_entries;
  813. unlock_block(info, block);
  814. return 0;
  815. }
  816. /*
  817. * Make space in a node, either by moving some entries to a sibling,
  818. * or creating a new sibling node. SPACE_THRESHOLD defines the minimum
  819. * number of free entries that must be in the sibling to make the move
  820. * worth while. If the siblings are shared (eg, part of a snapshot),
  821. * then they are not touched, since this break sharing and so consume
  822. * more space than we save.
  823. */
  824. #define SPACE_THRESHOLD 8
  825. static int rebalance_or_split(struct shadow_spine *s, struct dm_btree_value_type *vt,
  826. unsigned int parent_index, uint64_t key)
  827. {
  828. int r;
  829. struct btree_node *parent = dm_block_data(shadow_parent(s));
  830. unsigned int nr_parent = le32_to_cpu(parent->header.nr_entries);
  831. unsigned int free_space;
  832. int left_shared = 0, right_shared = 0;
  833. /* Should we move entries to the left sibling? */
  834. if (parent_index > 0) {
  835. dm_block_t left_b = value64(parent, parent_index - 1);
  836. r = dm_tm_block_is_shared(s->info->tm, left_b, &left_shared);
  837. if (r)
  838. return r;
  839. if (!left_shared) {
  840. r = get_node_free_space(s->info, left_b, &free_space);
  841. if (r)
  842. return r;
  843. if (free_space >= SPACE_THRESHOLD)
  844. return rebalance_left(s, vt, parent_index, key);
  845. }
  846. }
  847. /* Should we move entries to the right sibling? */
  848. if (parent_index < (nr_parent - 1)) {
  849. dm_block_t right_b = value64(parent, parent_index + 1);
  850. r = dm_tm_block_is_shared(s->info->tm, right_b, &right_shared);
  851. if (r)
  852. return r;
  853. if (!right_shared) {
  854. r = get_node_free_space(s->info, right_b, &free_space);
  855. if (r)
  856. return r;
  857. if (free_space >= SPACE_THRESHOLD)
  858. return rebalance_right(s, vt, parent_index, key);
  859. }
  860. }
  861. /*
  862. * We need to split the node, normally we split two nodes
  863. * into three. But when inserting a sequence that is either
  864. * monotonically increasing or decreasing it's better to split
  865. * a single node into two.
  866. */
  867. if (left_shared || right_shared || (nr_parent <= 2) ||
  868. (parent_index == 0) || (parent_index + 1 == nr_parent)) {
  869. return split_one_into_two(s, parent_index, vt, key);
  870. } else {
  871. return split_two_into_three(s, parent_index, vt, key);
  872. }
  873. }
  874. /*
  875. * Does the node contain a particular key?
  876. */
  877. static bool contains_key(struct btree_node *node, uint64_t key)
  878. {
  879. int i = lower_bound(node, key);
  880. if (i >= 0 && le64_to_cpu(node->keys[i]) == key)
  881. return true;
  882. return false;
  883. }
  884. /*
  885. * In general we preemptively make sure there's a free entry in every
  886. * node on the spine when doing an insert. But we can avoid that with
  887. * leaf nodes if we know it's an overwrite.
  888. */
  889. static bool has_space_for_insert(struct btree_node *node, uint64_t key)
  890. {
  891. if (node->header.nr_entries == node->header.max_entries) {
  892. if (le32_to_cpu(node->header.flags) & LEAF_NODE) {
  893. /* we don't need space if it's an overwrite */
  894. return contains_key(node, key);
  895. }
  896. return false;
  897. }
  898. return true;
  899. }
  900. static int btree_insert_raw(struct shadow_spine *s, dm_block_t root,
  901. struct dm_btree_value_type *vt,
  902. uint64_t key, unsigned int *index)
  903. {
  904. int r, i = *index, top = 1;
  905. struct btree_node *node;
  906. for (;;) {
  907. r = shadow_step(s, root, vt);
  908. if (r < 0)
  909. return r;
  910. node = dm_block_data(shadow_current(s));
  911. /*
  912. * We have to patch up the parent node, ugly, but I don't
  913. * see a way to do this automatically as part of the spine
  914. * op.
  915. */
  916. if (shadow_has_parent(s) && i >= 0) { /* FIXME: second clause unness. */
  917. __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
  918. __dm_bless_for_disk(&location);
  919. memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
  920. &location, sizeof(__le64));
  921. }
  922. node = dm_block_data(shadow_current(s));
  923. if (!has_space_for_insert(node, key)) {
  924. if (top)
  925. r = btree_split_beneath(s, key);
  926. else
  927. r = rebalance_or_split(s, vt, i, key);
  928. if (r < 0)
  929. return r;
  930. /* making space can cause the current node to change */
  931. node = dm_block_data(shadow_current(s));
  932. }
  933. i = lower_bound(node, key);
  934. if (le32_to_cpu(node->header.flags) & LEAF_NODE)
  935. break;
  936. if (i < 0) {
  937. /* change the bounds on the lowest key */
  938. node->keys[0] = cpu_to_le64(key);
  939. i = 0;
  940. }
  941. root = value64(node, i);
  942. top = 0;
  943. }
  944. if (i < 0 || le64_to_cpu(node->keys[i]) != key)
  945. i++;
  946. *index = i;
  947. return 0;
  948. }
  949. static int __btree_get_overwrite_leaf(struct shadow_spine *s, dm_block_t root,
  950. uint64_t key, int *index)
  951. {
  952. int r, i = -1;
  953. struct btree_node *node;
  954. *index = 0;
  955. for (;;) {
  956. r = shadow_step(s, root, &s->info->value_type);
  957. if (r < 0)
  958. return r;
  959. node = dm_block_data(shadow_current(s));
  960. /*
  961. * We have to patch up the parent node, ugly, but I don't
  962. * see a way to do this automatically as part of the spine
  963. * op.
  964. */
  965. if (shadow_has_parent(s) && i >= 0) {
  966. __le64 location = cpu_to_le64(dm_block_location(shadow_current(s)));
  967. __dm_bless_for_disk(&location);
  968. memcpy_disk(value_ptr(dm_block_data(shadow_parent(s)), i),
  969. &location, sizeof(__le64));
  970. }
  971. node = dm_block_data(shadow_current(s));
  972. i = lower_bound(node, key);
  973. BUG_ON(i < 0);
  974. BUG_ON(i >= le32_to_cpu(node->header.nr_entries));
  975. if (le32_to_cpu(node->header.flags) & LEAF_NODE) {
  976. if (key != le64_to_cpu(node->keys[i]))
  977. return -EINVAL;
  978. break;
  979. }
  980. root = value64(node, i);
  981. }
  982. *index = i;
  983. return 0;
  984. }
  985. int btree_get_overwrite_leaf(struct dm_btree_info *info, dm_block_t root,
  986. uint64_t key, int *index,
  987. dm_block_t *new_root, struct dm_block **leaf)
  988. {
  989. int r;
  990. struct shadow_spine spine;
  991. BUG_ON(info->levels > 1);
  992. init_shadow_spine(&spine, info);
  993. r = __btree_get_overwrite_leaf(&spine, root, key, index);
  994. if (!r) {
  995. *new_root = shadow_root(&spine);
  996. *leaf = shadow_current(&spine);
  997. /*
  998. * Decrement the count so exit_shadow_spine() doesn't
  999. * unlock the leaf.
  1000. */
  1001. spine.count--;
  1002. }
  1003. exit_shadow_spine(&spine);
  1004. return r;
  1005. }
  1006. static bool need_insert(struct btree_node *node, uint64_t *keys,
  1007. unsigned int level, unsigned int index)
  1008. {
  1009. return ((index >= le32_to_cpu(node->header.nr_entries)) ||
  1010. (le64_to_cpu(node->keys[index]) != keys[level]));
  1011. }
  1012. static int insert(struct dm_btree_info *info, dm_block_t root,
  1013. uint64_t *keys, void *value, dm_block_t *new_root,
  1014. int *inserted)
  1015. __dm_written_to_disk(value)
  1016. {
  1017. int r;
  1018. unsigned int level, index = -1, last_level = info->levels - 1;
  1019. dm_block_t block = root;
  1020. struct shadow_spine spine;
  1021. struct btree_node *n;
  1022. struct dm_btree_value_type le64_type;
  1023. init_le64_type(info->tm, &le64_type);
  1024. init_shadow_spine(&spine, info);
  1025. for (level = 0; level < (info->levels - 1); level++) {
  1026. r = btree_insert_raw(&spine, block, &le64_type, keys[level], &index);
  1027. if (r < 0)
  1028. goto bad;
  1029. n = dm_block_data(shadow_current(&spine));
  1030. if (need_insert(n, keys, level, index)) {
  1031. dm_block_t new_tree;
  1032. __le64 new_le;
  1033. r = dm_btree_empty(info, &new_tree);
  1034. if (r < 0)
  1035. goto bad;
  1036. new_le = cpu_to_le64(new_tree);
  1037. __dm_bless_for_disk(&new_le);
  1038. r = insert_at(sizeof(uint64_t), n, index,
  1039. keys[level], &new_le);
  1040. if (r)
  1041. goto bad;
  1042. }
  1043. if (level < last_level)
  1044. block = value64(n, index);
  1045. }
  1046. r = btree_insert_raw(&spine, block, &info->value_type,
  1047. keys[level], &index);
  1048. if (r < 0)
  1049. goto bad;
  1050. n = dm_block_data(shadow_current(&spine));
  1051. if (need_insert(n, keys, level, index)) {
  1052. if (inserted)
  1053. *inserted = 1;
  1054. r = insert_at(info->value_type.size, n, index,
  1055. keys[level], value);
  1056. if (r)
  1057. goto bad_unblessed;
  1058. } else {
  1059. if (inserted)
  1060. *inserted = 0;
  1061. if (info->value_type.dec &&
  1062. (!info->value_type.equal ||
  1063. !info->value_type.equal(
  1064. info->value_type.context,
  1065. value_ptr(n, index),
  1066. value))) {
  1067. info->value_type.dec(info->value_type.context,
  1068. value_ptr(n, index), 1);
  1069. }
  1070. memcpy_disk(value_ptr(n, index),
  1071. value, info->value_type.size);
  1072. }
  1073. *new_root = shadow_root(&spine);
  1074. exit_shadow_spine(&spine);
  1075. return 0;
  1076. bad:
  1077. __dm_unbless_for_disk(value);
  1078. bad_unblessed:
  1079. exit_shadow_spine(&spine);
  1080. return r;
  1081. }
  1082. int dm_btree_insert(struct dm_btree_info *info, dm_block_t root,
  1083. uint64_t *keys, void *value, dm_block_t *new_root)
  1084. __dm_written_to_disk(value)
  1085. {
  1086. return insert(info, root, keys, value, new_root, NULL);
  1087. }
  1088. EXPORT_SYMBOL_GPL(dm_btree_insert);
  1089. int dm_btree_insert_notify(struct dm_btree_info *info, dm_block_t root,
  1090. uint64_t *keys, void *value, dm_block_t *new_root,
  1091. int *inserted)
  1092. __dm_written_to_disk(value)
  1093. {
  1094. return insert(info, root, keys, value, new_root, inserted);
  1095. }
  1096. EXPORT_SYMBOL_GPL(dm_btree_insert_notify);
  1097. /*----------------------------------------------------------------*/
  1098. static int find_key(struct ro_spine *s, dm_block_t block, bool find_highest,
  1099. uint64_t *result_key, dm_block_t *next_block)
  1100. {
  1101. int i, r;
  1102. uint32_t flags;
  1103. do {
  1104. r = ro_step(s, block);
  1105. if (r < 0)
  1106. return r;
  1107. flags = le32_to_cpu(ro_node(s)->header.flags);
  1108. i = le32_to_cpu(ro_node(s)->header.nr_entries);
  1109. if (!i)
  1110. return -ENODATA;
  1111. i--;
  1112. if (find_highest)
  1113. *result_key = le64_to_cpu(ro_node(s)->keys[i]);
  1114. else
  1115. *result_key = le64_to_cpu(ro_node(s)->keys[0]);
  1116. if (next_block || flags & INTERNAL_NODE) {
  1117. if (find_highest)
  1118. block = value64(ro_node(s), i);
  1119. else
  1120. block = value64(ro_node(s), 0);
  1121. }
  1122. } while (flags & INTERNAL_NODE);
  1123. if (next_block)
  1124. *next_block = block;
  1125. return 0;
  1126. }
  1127. static int dm_btree_find_key(struct dm_btree_info *info, dm_block_t root,
  1128. bool find_highest, uint64_t *result_keys)
  1129. {
  1130. int r = 0, count = 0, level;
  1131. struct ro_spine spine;
  1132. init_ro_spine(&spine, info);
  1133. for (level = 0; level < info->levels; level++) {
  1134. r = find_key(&spine, root, find_highest, result_keys + level,
  1135. level == info->levels - 1 ? NULL : &root);
  1136. if (r == -ENODATA) {
  1137. r = 0;
  1138. break;
  1139. } else if (r)
  1140. break;
  1141. count++;
  1142. }
  1143. exit_ro_spine(&spine);
  1144. return r ? r : count;
  1145. }
  1146. int dm_btree_find_highest_key(struct dm_btree_info *info, dm_block_t root,
  1147. uint64_t *result_keys)
  1148. {
  1149. return dm_btree_find_key(info, root, true, result_keys);
  1150. }
  1151. EXPORT_SYMBOL_GPL(dm_btree_find_highest_key);
  1152. int dm_btree_find_lowest_key(struct dm_btree_info *info, dm_block_t root,
  1153. uint64_t *result_keys)
  1154. {
  1155. return dm_btree_find_key(info, root, false, result_keys);
  1156. }
  1157. EXPORT_SYMBOL_GPL(dm_btree_find_lowest_key);
  1158. /*----------------------------------------------------------------*/
  1159. /*
  1160. * FIXME: We shouldn't use a recursive algorithm when we have limited stack
  1161. * space. Also this only works for single level trees.
  1162. */
  1163. static int walk_node(struct dm_btree_info *info, dm_block_t block,
  1164. int (*fn)(void *context, uint64_t *keys, void *leaf),
  1165. void *context)
  1166. {
  1167. int r;
  1168. unsigned int i, nr;
  1169. struct dm_block *node;
  1170. struct btree_node *n;
  1171. uint64_t keys;
  1172. r = bn_read_lock(info, block, &node);
  1173. if (r)
  1174. return r;
  1175. n = dm_block_data(node);
  1176. nr = le32_to_cpu(n->header.nr_entries);
  1177. for (i = 0; i < nr; i++) {
  1178. if (le32_to_cpu(n->header.flags) & INTERNAL_NODE) {
  1179. r = walk_node(info, value64(n, i), fn, context);
  1180. if (r)
  1181. goto out;
  1182. } else {
  1183. keys = le64_to_cpu(*key_ptr(n, i));
  1184. r = fn(context, &keys, value_ptr(n, i));
  1185. if (r)
  1186. goto out;
  1187. }
  1188. }
  1189. out:
  1190. dm_tm_unlock(info->tm, node);
  1191. return r;
  1192. }
  1193. int dm_btree_walk(struct dm_btree_info *info, dm_block_t root,
  1194. int (*fn)(void *context, uint64_t *keys, void *leaf),
  1195. void *context)
  1196. {
  1197. BUG_ON(info->levels > 1);
  1198. return walk_node(info, root, fn, context);
  1199. }
  1200. EXPORT_SYMBOL_GPL(dm_btree_walk);
  1201. /*----------------------------------------------------------------*/
  1202. static void prefetch_values(struct dm_btree_cursor *c)
  1203. {
  1204. unsigned int i, nr;
  1205. __le64 value_le;
  1206. struct cursor_node *n = c->nodes + c->depth - 1;
  1207. struct btree_node *bn = dm_block_data(n->b);
  1208. struct dm_block_manager *bm = dm_tm_get_bm(c->info->tm);
  1209. BUG_ON(c->info->value_type.size != sizeof(value_le));
  1210. nr = le32_to_cpu(bn->header.nr_entries);
  1211. for (i = 0; i < nr; i++) {
  1212. memcpy(&value_le, value_ptr(bn, i), sizeof(value_le));
  1213. dm_bm_prefetch(bm, le64_to_cpu(value_le));
  1214. }
  1215. }
  1216. static bool leaf_node(struct dm_btree_cursor *c)
  1217. {
  1218. struct cursor_node *n = c->nodes + c->depth - 1;
  1219. struct btree_node *bn = dm_block_data(n->b);
  1220. return le32_to_cpu(bn->header.flags) & LEAF_NODE;
  1221. }
  1222. static int push_node(struct dm_btree_cursor *c, dm_block_t b)
  1223. {
  1224. int r;
  1225. struct cursor_node *n = c->nodes + c->depth;
  1226. if (c->depth >= DM_BTREE_CURSOR_MAX_DEPTH - 1) {
  1227. DMERR("couldn't push cursor node, stack depth too high");
  1228. return -EINVAL;
  1229. }
  1230. r = bn_read_lock(c->info, b, &n->b);
  1231. if (r)
  1232. return r;
  1233. n->index = 0;
  1234. c->depth++;
  1235. if (c->prefetch_leaves || !leaf_node(c))
  1236. prefetch_values(c);
  1237. return 0;
  1238. }
  1239. static void pop_node(struct dm_btree_cursor *c)
  1240. {
  1241. c->depth--;
  1242. unlock_block(c->info, c->nodes[c->depth].b);
  1243. }
  1244. static int inc_or_backtrack(struct dm_btree_cursor *c)
  1245. {
  1246. struct cursor_node *n;
  1247. struct btree_node *bn;
  1248. for (;;) {
  1249. if (!c->depth)
  1250. return -ENODATA;
  1251. n = c->nodes + c->depth - 1;
  1252. bn = dm_block_data(n->b);
  1253. n->index++;
  1254. if (n->index < le32_to_cpu(bn->header.nr_entries))
  1255. break;
  1256. pop_node(c);
  1257. }
  1258. return 0;
  1259. }
  1260. static int find_leaf(struct dm_btree_cursor *c)
  1261. {
  1262. int r = 0;
  1263. struct cursor_node *n;
  1264. struct btree_node *bn;
  1265. __le64 value_le;
  1266. for (;;) {
  1267. n = c->nodes + c->depth - 1;
  1268. bn = dm_block_data(n->b);
  1269. if (le32_to_cpu(bn->header.flags) & LEAF_NODE)
  1270. break;
  1271. memcpy(&value_le, value_ptr(bn, n->index), sizeof(value_le));
  1272. r = push_node(c, le64_to_cpu(value_le));
  1273. if (r) {
  1274. DMERR("push_node failed");
  1275. break;
  1276. }
  1277. }
  1278. if (!r && (le32_to_cpu(bn->header.nr_entries) == 0))
  1279. return -ENODATA;
  1280. return r;
  1281. }
  1282. int dm_btree_cursor_begin(struct dm_btree_info *info, dm_block_t root,
  1283. bool prefetch_leaves, struct dm_btree_cursor *c)
  1284. {
  1285. int r;
  1286. c->info = info;
  1287. c->root = root;
  1288. c->depth = 0;
  1289. c->prefetch_leaves = prefetch_leaves;
  1290. r = push_node(c, root);
  1291. if (r)
  1292. return r;
  1293. return find_leaf(c);
  1294. }
  1295. EXPORT_SYMBOL_GPL(dm_btree_cursor_begin);
  1296. void dm_btree_cursor_end(struct dm_btree_cursor *c)
  1297. {
  1298. while (c->depth)
  1299. pop_node(c);
  1300. }
  1301. EXPORT_SYMBOL_GPL(dm_btree_cursor_end);
  1302. int dm_btree_cursor_next(struct dm_btree_cursor *c)
  1303. {
  1304. int r = inc_or_backtrack(c);
  1305. if (!r) {
  1306. r = find_leaf(c);
  1307. if (r)
  1308. DMERR("find_leaf failed");
  1309. }
  1310. return r;
  1311. }
  1312. EXPORT_SYMBOL_GPL(dm_btree_cursor_next);
  1313. int dm_btree_cursor_skip(struct dm_btree_cursor *c, uint32_t count)
  1314. {
  1315. int r = 0;
  1316. while (count-- && !r)
  1317. r = dm_btree_cursor_next(c);
  1318. return r;
  1319. }
  1320. EXPORT_SYMBOL_GPL(dm_btree_cursor_skip);
  1321. int dm_btree_cursor_get_value(struct dm_btree_cursor *c, uint64_t *key, void *value_le)
  1322. {
  1323. if (c->depth) {
  1324. struct cursor_node *n = c->nodes + c->depth - 1;
  1325. struct btree_node *bn = dm_block_data(n->b);
  1326. if (le32_to_cpu(bn->header.flags) & INTERNAL_NODE)
  1327. return -EINVAL;
  1328. *key = le64_to_cpu(*key_ptr(bn, n->index));
  1329. memcpy(value_le, value_ptr(bn, n->index), c->info->value_type.size);
  1330. return 0;
  1331. } else
  1332. return -ENODATA;
  1333. }
  1334. EXPORT_SYMBOL_GPL(dm_btree_cursor_get_value);