dm-btree.c 38 KB

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