ctree.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "kerncompat.h"
  4. #include "radix-tree.h"
  5. #include "ctree.h"
  6. #include "disk-io.h"
  7. static int refill_alloc_extent(struct ctree_root *root);
  8. static inline void init_path(struct ctree_path *p)
  9. {
  10. memset(p, 0, sizeof(*p));
  11. }
  12. static void release_path(struct ctree_root *root, struct ctree_path *p)
  13. {
  14. int i;
  15. for (i = 0; i < MAX_LEVEL; i++) {
  16. if (!p->nodes[i])
  17. break;
  18. tree_block_release(root, p->nodes[i]);
  19. }
  20. }
  21. /*
  22. * The leaf data grows from end-to-front in the node.
  23. * this returns the address of the start of the last item,
  24. * which is the stop of the leaf data stack
  25. */
  26. static inline unsigned int leaf_data_end(struct leaf *leaf)
  27. {
  28. unsigned int nr = leaf->header.nritems;
  29. if (nr == 0)
  30. return sizeof(leaf->data);
  31. return leaf->items[nr-1].offset;
  32. }
  33. /*
  34. * The space between the end of the leaf items and
  35. * the start of the leaf data. IOW, how much room
  36. * the leaf has left for both items and data
  37. */
  38. static inline int leaf_free_space(struct leaf *leaf)
  39. {
  40. int data_end = leaf_data_end(leaf);
  41. int nritems = leaf->header.nritems;
  42. char *items_end = (char *)(leaf->items + nritems + 1);
  43. return (char *)(leaf->data + data_end) - (char *)items_end;
  44. }
  45. /*
  46. * compare two keys in a memcmp fashion
  47. */
  48. int comp_keys(struct key *k1, struct key *k2)
  49. {
  50. if (k1->objectid > k2->objectid)
  51. return 1;
  52. if (k1->objectid < k2->objectid)
  53. return -1;
  54. if (k1->flags > k2->flags)
  55. return 1;
  56. if (k1->flags < k2->flags)
  57. return -1;
  58. if (k1->offset > k2->offset)
  59. return 1;
  60. if (k1->offset < k2->offset)
  61. return -1;
  62. return 0;
  63. }
  64. /*
  65. * search for key in the array p. items p are item_size apart
  66. * and there are 'max' items in p
  67. * the slot in the array is returned via slot, and it points to
  68. * the place where you would insert key if it is not found in
  69. * the array.
  70. *
  71. * slot may point to max if the key is bigger than all of the keys
  72. */
  73. int generic_bin_search(char *p, int item_size, struct key *key,
  74. int max, int *slot)
  75. {
  76. int low = 0;
  77. int high = max;
  78. int mid;
  79. int ret;
  80. struct key *tmp;
  81. while(low < high) {
  82. mid = (low + high) / 2;
  83. tmp = (struct key *)(p + mid * item_size);
  84. ret = comp_keys(tmp, key);
  85. if (ret < 0)
  86. low = mid + 1;
  87. else if (ret > 0)
  88. high = mid;
  89. else {
  90. *slot = mid;
  91. return 0;
  92. }
  93. }
  94. *slot = low;
  95. return 1;
  96. }
  97. int bin_search(struct node *c, struct key *key, int *slot)
  98. {
  99. if (is_leaf(c->header.flags)) {
  100. struct leaf *l = (struct leaf *)c;
  101. return generic_bin_search((void *)l->items, sizeof(struct item),
  102. key, c->header.nritems, slot);
  103. } else {
  104. return generic_bin_search((void *)c->keys, sizeof(struct key),
  105. key, c->header.nritems, slot);
  106. }
  107. return -1;
  108. }
  109. /*
  110. * look for key in the tree. path is filled in with nodes along the way
  111. * if key is found, we return zero and you can find the item in the leaf
  112. * level of the path (level 0)
  113. *
  114. * If the key isn't found, the path points to the slot where it should
  115. * be inserted.
  116. */
  117. int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p)
  118. {
  119. struct tree_buffer *b = root->node;
  120. struct node *c;
  121. int slot;
  122. int ret;
  123. int level;
  124. b->count++;
  125. while (b) {
  126. c = &b->node;
  127. level = node_level(c->header.flags);
  128. p->nodes[level] = b;
  129. ret = bin_search(c, key, &slot);
  130. if (!is_leaf(c->header.flags)) {
  131. if (ret && slot > 0)
  132. slot -= 1;
  133. p->slots[level] = slot;
  134. b = read_tree_block(root, c->blockptrs[slot]);
  135. continue;
  136. } else {
  137. p->slots[level] = slot;
  138. return ret;
  139. }
  140. }
  141. return -1;
  142. }
  143. /*
  144. * adjust the pointers going up the tree, starting at level
  145. * making sure the right key of each node is points to 'key'.
  146. * This is used after shifting pointers to the left, so it stops
  147. * fixing up pointers when a given leaf/node is not in slot 0 of the
  148. * higher levels
  149. */
  150. static void fixup_low_keys(struct ctree_root *root,
  151. struct ctree_path *path, struct key *key,
  152. int level)
  153. {
  154. int i;
  155. for (i = level; i < MAX_LEVEL; i++) {
  156. struct node *t;
  157. int tslot = path->slots[i];
  158. if (!path->nodes[i])
  159. break;
  160. t = &path->nodes[i]->node;
  161. memcpy(t->keys + tslot, key, sizeof(*key));
  162. write_tree_block(root, path->nodes[i]);
  163. if (tslot != 0)
  164. break;
  165. }
  166. }
  167. /*
  168. * try to push data from one node into the next node left in the
  169. * tree. The src node is found at specified level in the path.
  170. * If some bytes were pushed, return 0, otherwise return 1.
  171. *
  172. * Lower nodes/leaves in the path are not touched, higher nodes may
  173. * be modified to reflect the push.
  174. *
  175. * The path is altered to reflect the push.
  176. */
  177. int push_node_left(struct ctree_root *root, struct ctree_path *path, int level)
  178. {
  179. int slot;
  180. struct node *left;
  181. struct node *right;
  182. int push_items = 0;
  183. int left_nritems;
  184. int right_nritems;
  185. struct tree_buffer *t;
  186. struct tree_buffer *right_buf;
  187. if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
  188. return 1;
  189. slot = path->slots[level + 1];
  190. if (slot == 0)
  191. return 1;
  192. t = read_tree_block(root,
  193. path->nodes[level + 1]->node.blockptrs[slot - 1]);
  194. left = &t->node;
  195. right_buf = path->nodes[level];
  196. right = &right_buf->node;
  197. left_nritems = left->header.nritems;
  198. right_nritems = right->header.nritems;
  199. push_items = NODEPTRS_PER_BLOCK - (left_nritems + 1);
  200. if (push_items <= 0) {
  201. tree_block_release(root, t);
  202. return 1;
  203. }
  204. if (right_nritems < push_items)
  205. push_items = right_nritems;
  206. memcpy(left->keys + left_nritems, right->keys,
  207. push_items * sizeof(struct key));
  208. memcpy(left->blockptrs + left_nritems, right->blockptrs,
  209. push_items * sizeof(u64));
  210. memmove(right->keys, right->keys + push_items,
  211. (right_nritems - push_items) * sizeof(struct key));
  212. memmove(right->blockptrs, right->blockptrs + push_items,
  213. (right_nritems - push_items) * sizeof(u64));
  214. right->header.nritems -= push_items;
  215. left->header.nritems += push_items;
  216. /* adjust the pointers going up the tree */
  217. fixup_low_keys(root, path, right->keys, level + 1);
  218. write_tree_block(root, t);
  219. write_tree_block(root, right_buf);
  220. /* then fixup the leaf pointer in the path */
  221. if (path->slots[level] < push_items) {
  222. path->slots[level] += left_nritems;
  223. tree_block_release(root, path->nodes[level]);
  224. path->nodes[level] = t;
  225. path->slots[level + 1] -= 1;
  226. } else {
  227. path->slots[level] -= push_items;
  228. tree_block_release(root, t);
  229. }
  230. return 0;
  231. }
  232. /*
  233. * try to push data from one node into the next node right in the
  234. * tree. The src node is found at specified level in the path.
  235. * If some bytes were pushed, return 0, otherwise return 1.
  236. *
  237. * Lower nodes/leaves in the path are not touched, higher nodes may
  238. * be modified to reflect the push.
  239. *
  240. * The path is altered to reflect the push.
  241. */
  242. int push_node_right(struct ctree_root *root, struct ctree_path *path, int level)
  243. {
  244. int slot;
  245. struct tree_buffer *t;
  246. struct tree_buffer *src_buffer;
  247. struct node *dst;
  248. struct node *src;
  249. int push_items = 0;
  250. int dst_nritems;
  251. int src_nritems;
  252. /* can't push from the root */
  253. if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
  254. return 1;
  255. /* only try to push inside the node higher up */
  256. slot = path->slots[level + 1];
  257. if (slot == NODEPTRS_PER_BLOCK - 1)
  258. return 1;
  259. if (slot >= path->nodes[level + 1]->node.header.nritems -1)
  260. return 1;
  261. t = read_tree_block(root,
  262. path->nodes[level + 1]->node.blockptrs[slot + 1]);
  263. dst = &t->node;
  264. src_buffer = path->nodes[level];
  265. src = &src_buffer->node;
  266. dst_nritems = dst->header.nritems;
  267. src_nritems = src->header.nritems;
  268. push_items = NODEPTRS_PER_BLOCK - (dst_nritems + 1);
  269. if (push_items <= 0) {
  270. tree_block_release(root, t);
  271. return 1;
  272. }
  273. if (src_nritems < push_items)
  274. push_items = src_nritems;
  275. memmove(dst->keys + push_items, dst->keys,
  276. dst_nritems * sizeof(struct key));
  277. memcpy(dst->keys, src->keys + src_nritems - push_items,
  278. push_items * sizeof(struct key));
  279. memmove(dst->blockptrs + push_items, dst->blockptrs,
  280. dst_nritems * sizeof(u64));
  281. memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
  282. push_items * sizeof(u64));
  283. src->header.nritems -= push_items;
  284. dst->header.nritems += push_items;
  285. /* adjust the pointers going up the tree */
  286. memcpy(path->nodes[level + 1]->node.keys + path->slots[level + 1] + 1,
  287. dst->keys, sizeof(struct key));
  288. write_tree_block(root, path->nodes[level + 1]);
  289. write_tree_block(root, t);
  290. write_tree_block(root, src_buffer);
  291. /* then fixup the pointers in the path */
  292. if (path->slots[level] >= src->header.nritems) {
  293. path->slots[level] -= src->header.nritems;
  294. tree_block_release(root, path->nodes[level]);
  295. path->nodes[level] = t;
  296. path->slots[level + 1] += 1;
  297. } else {
  298. tree_block_release(root, t);
  299. }
  300. return 0;
  301. }
  302. /*
  303. * worker function to insert a single pointer in a node.
  304. * the node should have enough room for the pointer already
  305. * slot and level indicate where you want the key to go, and
  306. * blocknr is the block the key points to.
  307. */
  308. int __insert_ptr(struct ctree_root *root,
  309. struct ctree_path *path, struct key *key,
  310. u64 blocknr, int slot, int level)
  311. {
  312. struct node *c;
  313. struct node *lower;
  314. struct key *lower_key;
  315. int nritems;
  316. /* need a new root */
  317. if (!path->nodes[level]) {
  318. struct tree_buffer *t;
  319. t = alloc_free_block(root);
  320. c = &t->node;
  321. memset(c, 0, sizeof(c));
  322. c->header.nritems = 2;
  323. c->header.flags = node_level(level);
  324. c->header.blocknr = t->blocknr;
  325. lower = &path->nodes[level-1]->node;
  326. if (is_leaf(lower->header.flags))
  327. lower_key = &((struct leaf *)lower)->items[0].key;
  328. else
  329. lower_key = lower->keys;
  330. memcpy(c->keys, lower_key, sizeof(struct key));
  331. memcpy(c->keys + 1, key, sizeof(struct key));
  332. c->blockptrs[0] = path->nodes[level-1]->blocknr;
  333. c->blockptrs[1] = blocknr;
  334. /* the path has an extra ref to root->node */
  335. tree_block_release(root, root->node);
  336. root->node = t;
  337. t->count++;
  338. write_tree_block(root, t);
  339. path->nodes[level] = t;
  340. path->slots[level] = 0;
  341. if (c->keys[1].objectid == 0)
  342. BUG();
  343. return 0;
  344. }
  345. lower = &path->nodes[level]->node;
  346. nritems = lower->header.nritems;
  347. if (slot > nritems)
  348. BUG();
  349. if (nritems == NODEPTRS_PER_BLOCK)
  350. BUG();
  351. if (slot != nritems) {
  352. memmove(lower->keys + slot + 1, lower->keys + slot,
  353. (nritems - slot) * sizeof(struct key));
  354. memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
  355. (nritems - slot) * sizeof(u64));
  356. }
  357. memcpy(lower->keys + slot, key, sizeof(struct key));
  358. lower->blockptrs[slot] = blocknr;
  359. lower->header.nritems++;
  360. if (lower->keys[1].objectid == 0)
  361. BUG();
  362. write_tree_block(root, path->nodes[level]);
  363. return 0;
  364. }
  365. /*
  366. * insert a key,blocknr pair into the tree at a given level
  367. * If the node at that level in the path doesn't have room,
  368. * it is split or shifted as appropriate.
  369. */
  370. int insert_ptr(struct ctree_root *root,
  371. struct ctree_path *path, struct key *key,
  372. u64 blocknr, int level)
  373. {
  374. struct tree_buffer *t = path->nodes[level];
  375. struct node *c = &path->nodes[level]->node;
  376. struct node *b;
  377. struct tree_buffer *b_buffer;
  378. struct tree_buffer *bal[MAX_LEVEL];
  379. int bal_level = level;
  380. int mid;
  381. int bal_start = -1;
  382. /*
  383. * check to see if we need to make room in the node for this
  384. * pointer. If we do, keep walking the tree, making sure there
  385. * is enough room in each level for the required insertions.
  386. *
  387. * The bal array is filled in with any nodes to be inserted
  388. * due to splitting. Once we've done all the splitting required
  389. * do the inserts based on the data in the bal array.
  390. */
  391. memset(bal, 0, sizeof(bal));
  392. while(t && t->node.header.nritems == NODEPTRS_PER_BLOCK) {
  393. c = &t->node;
  394. if (push_node_left(root, path,
  395. node_level(c->header.flags)) == 0)
  396. break;
  397. if (push_node_right(root, path,
  398. node_level(c->header.flags)) == 0)
  399. break;
  400. bal_start = bal_level;
  401. if (bal_level == MAX_LEVEL - 1)
  402. BUG();
  403. b_buffer = alloc_free_block(root);
  404. b = &b_buffer->node;
  405. b->header.flags = c->header.flags;
  406. b->header.blocknr = b_buffer->blocknr;
  407. mid = (c->header.nritems + 1) / 2;
  408. memcpy(b->keys, c->keys + mid,
  409. (c->header.nritems - mid) * sizeof(struct key));
  410. memcpy(b->blockptrs, c->blockptrs + mid,
  411. (c->header.nritems - mid) * sizeof(u64));
  412. b->header.nritems = c->header.nritems - mid;
  413. c->header.nritems = mid;
  414. write_tree_block(root, t);
  415. write_tree_block(root, b_buffer);
  416. bal[bal_level] = b_buffer;
  417. if (bal_level == MAX_LEVEL - 1)
  418. break;
  419. bal_level += 1;
  420. t = path->nodes[bal_level];
  421. }
  422. /*
  423. * bal_start tells us the first level in the tree that needed to
  424. * be split. Go through the bal array inserting the new nodes
  425. * as needed. The path is fixed as we go.
  426. */
  427. while(bal_start > 0) {
  428. b_buffer = bal[bal_start];
  429. c = &path->nodes[bal_start]->node;
  430. __insert_ptr(root, path, b_buffer->node.keys, b_buffer->blocknr,
  431. path->slots[bal_start + 1] + 1, bal_start + 1);
  432. if (path->slots[bal_start] >= c->header.nritems) {
  433. path->slots[bal_start] -= c->header.nritems;
  434. tree_block_release(root, path->nodes[bal_start]);
  435. path->nodes[bal_start] = b_buffer;
  436. path->slots[bal_start + 1] += 1;
  437. } else {
  438. tree_block_release(root, b_buffer);
  439. }
  440. bal_start--;
  441. if (!bal[bal_start])
  442. break;
  443. }
  444. /* Now that the tree has room, insert the requested pointer */
  445. return __insert_ptr(root, path, key, blocknr, path->slots[level] + 1,
  446. level);
  447. }
  448. /*
  449. * how many bytes are required to store the items in a leaf. start
  450. * and nr indicate which items in the leaf to check. This totals up the
  451. * space used both by the item structs and the item data
  452. */
  453. int leaf_space_used(struct leaf *l, int start, int nr)
  454. {
  455. int data_len;
  456. int end = start + nr - 1;
  457. if (!nr)
  458. return 0;
  459. data_len = l->items[start].offset + l->items[start].size;
  460. data_len = data_len - l->items[end].offset;
  461. data_len += sizeof(struct item) * nr;
  462. return data_len;
  463. }
  464. /*
  465. * push some data in the path leaf to the left, trying to free up at
  466. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  467. */
  468. int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
  469. int data_size)
  470. {
  471. struct tree_buffer *right_buf = path->nodes[0];
  472. struct leaf *right = &right_buf->leaf;
  473. struct tree_buffer *t;
  474. struct leaf *left;
  475. int slot;
  476. int i;
  477. int free_space;
  478. int push_space = 0;
  479. int push_items = 0;
  480. struct item *item;
  481. int old_left_nritems;
  482. slot = path->slots[1];
  483. if (slot == 0) {
  484. return 1;
  485. }
  486. if (!path->nodes[1]) {
  487. return 1;
  488. }
  489. t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
  490. left = &t->leaf;
  491. free_space = leaf_free_space(left);
  492. if (free_space < data_size + sizeof(struct item)) {
  493. tree_block_release(root, t);
  494. return 1;
  495. }
  496. for (i = 0; i < right->header.nritems; i++) {
  497. item = right->items + i;
  498. if (path->slots[0] == i)
  499. push_space += data_size + sizeof(*item);
  500. if (item->size + sizeof(*item) + push_space > free_space)
  501. break;
  502. push_items++;
  503. push_space += item->size + sizeof(*item);
  504. }
  505. if (push_items == 0) {
  506. tree_block_release(root, t);
  507. return 1;
  508. }
  509. /* push data from right to left */
  510. memcpy(left->items + left->header.nritems,
  511. right->items, push_items * sizeof(struct item));
  512. push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset;
  513. memcpy(left->data + leaf_data_end(left) - push_space,
  514. right->data + right->items[push_items - 1].offset,
  515. push_space);
  516. old_left_nritems = left->header.nritems;
  517. BUG_ON(old_left_nritems < 0);
  518. for(i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  519. left->items[i].offset -= LEAF_DATA_SIZE -
  520. left->items[old_left_nritems -1].offset;
  521. }
  522. left->header.nritems += push_items;
  523. /* fixup right node */
  524. push_space = right->items[push_items-1].offset - leaf_data_end(right);
  525. memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
  526. leaf_data_end(right), push_space);
  527. memmove(right->items, right->items + push_items,
  528. (right->header.nritems - push_items) * sizeof(struct item));
  529. right->header.nritems -= push_items;
  530. push_space = LEAF_DATA_SIZE;
  531. for (i = 0; i < right->header.nritems; i++) {
  532. right->items[i].offset = push_space - right->items[i].size;
  533. push_space = right->items[i].offset;
  534. }
  535. write_tree_block(root, t);
  536. write_tree_block(root, right_buf);
  537. fixup_low_keys(root, path, &right->items[0].key, 1);
  538. /* then fixup the leaf pointer in the path */
  539. if (path->slots[0] < push_items) {
  540. path->slots[0] += old_left_nritems;
  541. tree_block_release(root, path->nodes[0]);
  542. path->nodes[0] = t;
  543. path->slots[1] -= 1;
  544. } else {
  545. tree_block_release(root, t);
  546. path->slots[0] -= push_items;
  547. }
  548. BUG_ON(path->slots[0] < 0);
  549. return 0;
  550. }
  551. /*
  552. * split the path's leaf in two, making sure there is at least data_size
  553. * available for the resulting leaf level of the path.
  554. */
  555. int split_leaf(struct ctree_root *root, struct ctree_path *path, int data_size)
  556. {
  557. struct tree_buffer *l_buf = path->nodes[0];
  558. struct leaf *l = &l_buf->leaf;
  559. int nritems;
  560. int mid;
  561. int slot;
  562. struct leaf *right;
  563. struct tree_buffer *right_buffer;
  564. int space_needed = data_size + sizeof(struct item);
  565. int data_copy_size;
  566. int rt_data_off;
  567. int i;
  568. int ret;
  569. if (push_leaf_left(root, path, data_size) == 0) {
  570. l_buf = path->nodes[0];
  571. l = &l_buf->leaf;
  572. if (leaf_free_space(l) >= sizeof(struct item) + data_size)
  573. return 0;
  574. }
  575. slot = path->slots[0];
  576. nritems = l->header.nritems;
  577. mid = (nritems + 1)/ 2;
  578. right_buffer = alloc_free_block(root);
  579. BUG_ON(!right_buffer);
  580. BUG_ON(mid == nritems);
  581. right = &right_buffer->leaf;
  582. memset(right, 0, sizeof(*right));
  583. if (mid <= slot) {
  584. if (leaf_space_used(l, mid, nritems - mid) + space_needed >
  585. LEAF_DATA_SIZE)
  586. BUG();
  587. } else {
  588. if (leaf_space_used(l, 0, mid + 1) + space_needed >
  589. LEAF_DATA_SIZE)
  590. BUG();
  591. }
  592. right->header.nritems = nritems - mid;
  593. right->header.blocknr = right_buffer->blocknr;
  594. right->header.flags = node_level(0);
  595. data_copy_size = l->items[mid].offset + l->items[mid].size -
  596. leaf_data_end(l);
  597. memcpy(right->items, l->items + mid,
  598. (nritems - mid) * sizeof(struct item));
  599. memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
  600. l->data + leaf_data_end(l), data_copy_size);
  601. rt_data_off = LEAF_DATA_SIZE -
  602. (l->items[mid].offset + l->items[mid].size);
  603. for (i = 0; i < right->header.nritems; i++)
  604. right->items[i].offset += rt_data_off;
  605. l->header.nritems = mid;
  606. ret = insert_ptr(root, path, &right->items[0].key,
  607. right_buffer->blocknr, 1);
  608. write_tree_block(root, right_buffer);
  609. write_tree_block(root, l_buf);
  610. BUG_ON(path->slots[0] != slot);
  611. if (mid <= slot) {
  612. tree_block_release(root, path->nodes[0]);
  613. path->nodes[0] = right_buffer;
  614. path->slots[0] -= mid;
  615. path->slots[1] += 1;
  616. } else
  617. tree_block_release(root, right_buffer);
  618. BUG_ON(path->slots[0] < 0);
  619. return ret;
  620. }
  621. /*
  622. * Given a key and some data, insert an item into the tree.
  623. * This does all the path init required, making room in the tree if needed.
  624. */
  625. int insert_item(struct ctree_root *root, struct key *key,
  626. void *data, int data_size)
  627. {
  628. int ret;
  629. int slot;
  630. int slot_orig;
  631. struct leaf *leaf;
  632. struct tree_buffer *leaf_buf;
  633. unsigned int nritems;
  634. unsigned int data_end;
  635. struct ctree_path path;
  636. /* create a root if there isn't one */
  637. if (!root->node) {
  638. struct tree_buffer *t;
  639. t = alloc_free_block(root);
  640. BUG_ON(!t);
  641. t->node.header.nritems = 0;
  642. t->node.header.flags = node_level(0);
  643. t->node.header.blocknr = t->blocknr;
  644. root->node = t;
  645. write_tree_block(root, t);
  646. }
  647. init_path(&path);
  648. ret = search_slot(root, key, &path);
  649. if (ret == 0) {
  650. release_path(root, &path);
  651. return -EEXIST;
  652. }
  653. slot_orig = path.slots[0];
  654. leaf_buf = path.nodes[0];
  655. leaf = &leaf_buf->leaf;
  656. /* make room if needed */
  657. if (leaf_free_space(leaf) < sizeof(struct item) + data_size) {
  658. split_leaf(root, &path, data_size);
  659. leaf_buf = path.nodes[0];
  660. leaf = &path.nodes[0]->leaf;
  661. }
  662. nritems = leaf->header.nritems;
  663. data_end = leaf_data_end(leaf);
  664. if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
  665. BUG();
  666. slot = path.slots[0];
  667. BUG_ON(slot < 0);
  668. if (slot == 0)
  669. fixup_low_keys(root, &path, key, 1);
  670. if (slot != nritems) {
  671. int i;
  672. unsigned int old_data = leaf->items[slot].offset +
  673. leaf->items[slot].size;
  674. /*
  675. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  676. */
  677. /* first correct the data pointers */
  678. for (i = slot; i < nritems; i++)
  679. leaf->items[i].offset -= data_size;
  680. /* shift the items */
  681. memmove(leaf->items + slot + 1, leaf->items + slot,
  682. (nritems - slot) * sizeof(struct item));
  683. /* shift the data */
  684. memmove(leaf->data + data_end - data_size, leaf->data +
  685. data_end, old_data - data_end);
  686. data_end = old_data;
  687. }
  688. /* copy the new data in */
  689. memcpy(&leaf->items[slot].key, key, sizeof(struct key));
  690. leaf->items[slot].offset = data_end - data_size;
  691. leaf->items[slot].size = data_size;
  692. memcpy(leaf->data + data_end - data_size, data, data_size);
  693. leaf->header.nritems += 1;
  694. write_tree_block(root, leaf_buf);
  695. if (leaf_free_space(leaf) < 0)
  696. BUG();
  697. release_path(root, &path);
  698. refill_alloc_extent(root);
  699. return 0;
  700. }
  701. /*
  702. * delete the pointer from a given level in the path. The path is not
  703. * fixed up, so after calling this it is not valid at that level.
  704. *
  705. * If the delete empties a node, the node is removed from the tree,
  706. * continuing all the way the root if required. The root is converted into
  707. * a leaf if all the nodes are emptied.
  708. */
  709. int del_ptr(struct ctree_root *root, struct ctree_path *path, int level)
  710. {
  711. int slot;
  712. struct tree_buffer *t;
  713. struct node *node;
  714. int nritems;
  715. while(1) {
  716. t = path->nodes[level];
  717. if (!t)
  718. break;
  719. node = &t->node;
  720. slot = path->slots[level];
  721. nritems = node->header.nritems;
  722. if (slot != nritems -1) {
  723. memmove(node->keys + slot, node->keys + slot + 1,
  724. sizeof(struct key) * (nritems - slot - 1));
  725. memmove(node->blockptrs + slot,
  726. node->blockptrs + slot + 1,
  727. sizeof(u64) * (nritems - slot - 1));
  728. }
  729. node->header.nritems--;
  730. write_tree_block(root, t);
  731. if (node->header.nritems != 0) {
  732. int tslot;
  733. if (slot == 0)
  734. fixup_low_keys(root, path, node->keys,
  735. level + 1);
  736. tslot = path->slots[level+1];
  737. t->count++;
  738. push_node_left(root, path, level);
  739. if (node->header.nritems) {
  740. push_node_right(root, path, level);
  741. }
  742. if (node->header.nritems) {
  743. tree_block_release(root, t);
  744. break;
  745. }
  746. tree_block_release(root, t);
  747. path->slots[level+1] = tslot;
  748. }
  749. if (t == root->node) {
  750. /* just turn the root into a leaf and break */
  751. root->node->node.header.flags = node_level(0);
  752. write_tree_block(root, t);
  753. break;
  754. }
  755. level++;
  756. if (!path->nodes[level])
  757. BUG();
  758. }
  759. return 0;
  760. }
  761. /*
  762. * delete the item at the leaf level in path. If that empties
  763. * the leaf, remove it from the tree
  764. */
  765. int del_item(struct ctree_root *root, struct ctree_path *path)
  766. {
  767. int slot;
  768. struct leaf *leaf;
  769. struct tree_buffer *leaf_buf;
  770. int doff;
  771. int dsize;
  772. leaf_buf = path->nodes[0];
  773. leaf = &leaf_buf->leaf;
  774. slot = path->slots[0];
  775. doff = leaf->items[slot].offset;
  776. dsize = leaf->items[slot].size;
  777. if (slot != leaf->header.nritems - 1) {
  778. int i;
  779. int data_end = leaf_data_end(leaf);
  780. memmove(leaf->data + data_end + dsize,
  781. leaf->data + data_end,
  782. doff - data_end);
  783. for (i = slot + 1; i < leaf->header.nritems; i++)
  784. leaf->items[i].offset += dsize;
  785. memmove(leaf->items + slot, leaf->items + slot + 1,
  786. sizeof(struct item) *
  787. (leaf->header.nritems - slot - 1));
  788. }
  789. leaf->header.nritems -= 1;
  790. /* delete the leaf if we've emptied it */
  791. if (leaf->header.nritems == 0) {
  792. if (leaf_buf == root->node) {
  793. leaf->header.flags = node_level(0);
  794. write_tree_block(root, leaf_buf);
  795. } else
  796. del_ptr(root, path, 1);
  797. } else {
  798. if (slot == 0)
  799. fixup_low_keys(root, path, &leaf->items[0].key, 1);
  800. write_tree_block(root, leaf_buf);
  801. /* delete the leaf if it is mostly empty */
  802. if (leaf_space_used(leaf, 0, leaf->header.nritems) <
  803. LEAF_DATA_SIZE / 4) {
  804. /* push_leaf_left fixes the path.
  805. * make sure the path still points to our leaf
  806. * for possible call to del_ptr below
  807. */
  808. slot = path->slots[1];
  809. leaf_buf->count++;
  810. push_leaf_left(root, path, 1);
  811. if (leaf->header.nritems == 0) {
  812. path->slots[1] = slot;
  813. del_ptr(root, path, 1);
  814. }
  815. tree_block_release(root, leaf_buf);
  816. }
  817. }
  818. return 0;
  819. }
  820. int next_leaf(struct ctree_root *root, struct ctree_path *path)
  821. {
  822. int slot;
  823. int level = 1;
  824. u64 blocknr;
  825. struct tree_buffer *c;
  826. struct tree_buffer *next;
  827. while(level < MAX_LEVEL) {
  828. if (!path->nodes[level])
  829. return -1;
  830. slot = path->slots[level] + 1;
  831. c = path->nodes[level];
  832. if (slot >= c->node.header.nritems) {
  833. level++;
  834. continue;
  835. }
  836. blocknr = c->node.blockptrs[slot];
  837. next = read_tree_block(root, blocknr);
  838. break;
  839. }
  840. path->slots[level] = slot;
  841. while(1) {
  842. level--;
  843. c = path->nodes[level];
  844. tree_block_release(root, c);
  845. path->nodes[level] = next;
  846. path->slots[level] = 0;
  847. if (!level)
  848. break;
  849. next = read_tree_block(root, next->node.blockptrs[0]);
  850. }
  851. return 0;
  852. }
  853. int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
  854. u64 search_end, u64 owner, struct key *ins)
  855. {
  856. struct ctree_path path;
  857. struct key *key;
  858. int ret;
  859. u64 hole_size = 0;
  860. int slot = 0;
  861. u64 last_block;
  862. int start_found = 0;
  863. struct leaf *l;
  864. struct extent_item extent_item;
  865. init_path(&path);
  866. ins->objectid = search_start;
  867. ins->offset = 0;
  868. ins->flags = 0;
  869. ret = search_slot(root, ins, &path);
  870. while (1) {
  871. l = &path.nodes[0]->leaf;
  872. slot = path.slots[0];
  873. if (!l) {
  874. // FIXME allocate root
  875. }
  876. if (slot >= l->header.nritems) {
  877. ret = next_leaf(root, &path);
  878. if (ret == 0)
  879. continue;
  880. if (!start_found) {
  881. ins->objectid = search_start;
  882. ins->offset = num_blocks;
  883. hole_size = search_end - search_start;
  884. goto insert;
  885. }
  886. ins->objectid = last_block;
  887. ins->offset = num_blocks;
  888. hole_size = search_end - last_block;
  889. goto insert;
  890. }
  891. key = &l->items[slot].key;
  892. if (start_found) {
  893. hole_size = key->objectid - last_block;
  894. if (hole_size > num_blocks) {
  895. ins->objectid = last_block;
  896. ins->offset = num_blocks;
  897. goto insert;
  898. }
  899. } else
  900. start_found = 1;
  901. last_block = key->objectid + key->offset;
  902. path.slots[0]++;
  903. printf("last block is not %lu\n", last_block);
  904. }
  905. // FIXME -ENOSPC
  906. insert:
  907. extent_item.refs = 1;
  908. extent_item.owner = owner;
  909. ret = insert_item(root, ins, &extent_item, sizeof(extent_item));
  910. return ret;
  911. }
  912. static int refill_alloc_extent(struct ctree_root *root)
  913. {
  914. struct alloc_extent *ae = root->alloc_extent;
  915. struct key key;
  916. int ret;
  917. int min_blocks = MAX_LEVEL * 2;
  918. printf("refill alloc root %p, numused %lu total %lu\n", root, ae->num_used, ae->num_blocks);
  919. if (ae->num_blocks > ae->num_used && ae->num_blocks - ae->num_used >
  920. min_blocks)
  921. return 0;
  922. ae = root->reserve_extent;
  923. if (ae->num_blocks > ae->num_used) {
  924. if (root->alloc_extent->num_blocks == 0) {
  925. /* we should swap reserve/alloc_extent when alloc
  926. * fills up
  927. */
  928. BUG();
  929. }
  930. if (ae->num_blocks - ae->num_used < min_blocks)
  931. BUG();
  932. return 0;
  933. }
  934. // FIXME, this recurses
  935. ret = alloc_extent(root->extent_root,
  936. min_blocks * 2, 0, (unsigned long)-1, 0, &key);
  937. ae->blocknr = key.objectid;
  938. ae->num_blocks = key.offset;
  939. ae->num_used = 0;
  940. return ret;
  941. }
  942. void print_leaf(struct leaf *l)
  943. {
  944. int i;
  945. int nr = l->header.nritems;
  946. struct item *item;
  947. printf("leaf %lu total ptrs %d free space %d\n", l->header.blocknr, nr,
  948. leaf_free_space(l));
  949. fflush(stdout);
  950. for (i = 0 ; i < nr ; i++) {
  951. item = l->items + i;
  952. printf("\titem %d key (%lu %u %lu) itemoff %d itemsize %d\n",
  953. i,
  954. item->key.objectid, item->key.flags, item->key.offset,
  955. item->offset, item->size);
  956. fflush(stdout);
  957. printf("\t\titem data %.*s\n", item->size, l->data+item->offset);
  958. fflush(stdout);
  959. }
  960. }
  961. void print_tree(struct ctree_root *root, struct tree_buffer *t)
  962. {
  963. int i;
  964. int nr;
  965. struct node *c;
  966. if (!t)
  967. return;
  968. c = &t->node;
  969. nr = c->header.nritems;
  970. if (c->header.blocknr != t->blocknr)
  971. BUG();
  972. if (is_leaf(c->header.flags)) {
  973. print_leaf((struct leaf *)c);
  974. return;
  975. }
  976. printf("node %lu level %d total ptrs %d free spc %lu\n", t->blocknr,
  977. node_level(c->header.flags), c->header.nritems,
  978. NODEPTRS_PER_BLOCK - c->header.nritems);
  979. fflush(stdout);
  980. for (i = 0; i < nr; i++) {
  981. printf("\tkey %d (%lu %u %lu) block %lu\n",
  982. i,
  983. c->keys[i].objectid, c->keys[i].flags, c->keys[i].offset,
  984. c->blockptrs[i]);
  985. fflush(stdout);
  986. }
  987. for (i = 0; i < nr; i++) {
  988. struct tree_buffer *next_buf = read_tree_block(root,
  989. c->blockptrs[i]);
  990. struct node *next = &next_buf->node;
  991. if (is_leaf(next->header.flags) &&
  992. node_level(c->header.flags) != 1)
  993. BUG();
  994. if (node_level(next->header.flags) !=
  995. node_level(c->header.flags) - 1)
  996. BUG();
  997. print_tree(root, next_buf);
  998. tree_block_release(root, next_buf);
  999. }
  1000. }
  1001. /* for testing only */
  1002. int next_key(int i, int max_key) {
  1003. // return rand() % max_key;
  1004. return i;
  1005. }
  1006. int main() {
  1007. struct ctree_root *root;
  1008. struct key ins;
  1009. struct key last = { (u64)-1, 0, 0};
  1010. char *buf;
  1011. int i;
  1012. int num;
  1013. int ret;
  1014. int run_size = 256;
  1015. int max_key = 100000000;
  1016. int tree_size = 0;
  1017. struct ctree_path path;
  1018. radix_tree_init();
  1019. root = open_ctree("dbfile");
  1020. srand(55);
  1021. for (i = 0; i < run_size; i++) {
  1022. buf = malloc(64);
  1023. num = next_key(i, max_key);
  1024. // num = i;
  1025. sprintf(buf, "string-%d", num);
  1026. // printf("insert %d\n", num);
  1027. ins.objectid = num;
  1028. ins.offset = 0;
  1029. ins.flags = 0;
  1030. printf("insert %d\n", i);
  1031. ret = insert_item(root, &ins, buf, strlen(buf));
  1032. if (!ret)
  1033. tree_size++;
  1034. printf("done insert %d\n", i);
  1035. }
  1036. printf("root used: %lu\n", root->alloc_extent->num_used);
  1037. printf("root tree\n");
  1038. print_tree(root, root->node);
  1039. printf("map tree\n");
  1040. printf("map used: %lu\n", root->extent_root->alloc_extent->num_used);
  1041. print_tree(root->extent_root, root->extent_root->node);
  1042. exit(1);
  1043. close_ctree(root);
  1044. root = open_ctree("dbfile");
  1045. printf("starting search\n");
  1046. srand(55);
  1047. for (i = 0; i < run_size; i++) {
  1048. num = next_key(i, max_key);
  1049. ins.objectid = num;
  1050. init_path(&path);
  1051. ret = search_slot(root, &ins, &path);
  1052. if (ret) {
  1053. print_tree(root, root->node);
  1054. printf("unable to find %d\n", num);
  1055. exit(1);
  1056. }
  1057. release_path(root, &path);
  1058. }
  1059. close_ctree(root);
  1060. root = open_ctree("dbfile");
  1061. printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
  1062. node_level(root->node->node.header.flags),
  1063. root->node->node.header.nritems,
  1064. NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
  1065. printf("all searches good, deleting some items\n");
  1066. i = 0;
  1067. srand(55);
  1068. for (i = 0 ; i < run_size/4; i++) {
  1069. num = next_key(i, max_key);
  1070. ins.objectid = num;
  1071. init_path(&path);
  1072. ret = search_slot(root, &ins, &path);
  1073. if (ret)
  1074. continue;
  1075. ret = del_item(root, &path);
  1076. if (ret != 0)
  1077. BUG();
  1078. release_path(root, &path);
  1079. tree_size--;
  1080. }
  1081. srand(128);
  1082. for (i = 0; i < run_size; i++) {
  1083. buf = malloc(64);
  1084. num = next_key(i, max_key);
  1085. sprintf(buf, "string-%d", num);
  1086. ins.objectid = num;
  1087. ret = insert_item(root, &ins, buf, strlen(buf));
  1088. if (!ret)
  1089. tree_size++;
  1090. }
  1091. close_ctree(root);
  1092. root = open_ctree("dbfile");
  1093. printf("starting search2\n");
  1094. srand(128);
  1095. for (i = 0; i < run_size; i++) {
  1096. num = next_key(i, max_key);
  1097. ins.objectid = num;
  1098. init_path(&path);
  1099. ret = search_slot(root, &ins, &path);
  1100. if (ret) {
  1101. print_tree(root, root->node);
  1102. printf("unable to find %d\n", num);
  1103. exit(1);
  1104. }
  1105. release_path(root, &path);
  1106. }
  1107. printf("starting big long delete run\n");
  1108. while(root->node && root->node->node.header.nritems > 0) {
  1109. struct leaf *leaf;
  1110. int slot;
  1111. ins.objectid = (u64)-1;
  1112. init_path(&path);
  1113. ret = search_slot(root, &ins, &path);
  1114. if (ret == 0)
  1115. BUG();
  1116. leaf = &path.nodes[0]->leaf;
  1117. slot = path.slots[0];
  1118. if (slot != leaf->header.nritems)
  1119. BUG();
  1120. while(path.slots[0] > 0) {
  1121. path.slots[0] -= 1;
  1122. slot = path.slots[0];
  1123. leaf = &path.nodes[0]->leaf;
  1124. if (comp_keys(&last, &leaf->items[slot].key) <= 0)
  1125. BUG();
  1126. memcpy(&last, &leaf->items[slot].key, sizeof(last));
  1127. ret = del_item(root, &path);
  1128. if (ret != 0) {
  1129. printf("del_item returned %d\n", ret);
  1130. BUG();
  1131. }
  1132. tree_size--;
  1133. }
  1134. release_path(root, &path);
  1135. }
  1136. close_ctree(root);
  1137. printf("tree size is now %d\n", tree_size);
  1138. return 0;
  1139. }