extent-tree.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include "print-tree.h"
  8. /*
  9. * pending extents are blocks that we're trying to allocate in the extent
  10. * map while trying to grow the map because of other allocations. To avoid
  11. * recursing, they are tagged in the radix tree and cleaned up after
  12. * other allocations are done. The pending tag is also used in the same
  13. * manner for deletes.
  14. */
  15. #define CTREE_EXTENT_PENDING 0
  16. /*
  17. * find all the blocks marked as pending in the radix tree and remove
  18. * them from the extent map
  19. */
  20. static int del_pending_extents(struct ctree_root *extent_root)
  21. {
  22. int ret;
  23. struct key key;
  24. struct tree_buffer *gang[4];
  25. int i;
  26. struct ctree_path path;
  27. while(1) {
  28. ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
  29. (void **)gang, 0,
  30. ARRAY_SIZE(gang),
  31. CTREE_EXTENT_PENDING);
  32. if (!ret)
  33. break;
  34. for (i = 0; i < ret; i++) {
  35. key.objectid = gang[i]->blocknr;
  36. key.flags = 0;
  37. key.offset = 1;
  38. init_path(&path);
  39. ret = search_slot(extent_root, &key, &path, 0);
  40. if (ret) {
  41. print_tree(extent_root, extent_root->node);
  42. printf("unable to find %Lu\n", key.objectid);
  43. BUG();
  44. // FIXME undo it and return sane
  45. return ret;
  46. }
  47. ret = del_item(extent_root, &path);
  48. if (ret) {
  49. BUG();
  50. return ret;
  51. }
  52. release_path(extent_root, &path);
  53. radix_tree_tag_clear(&extent_root->cache_radix,
  54. gang[i]->blocknr,
  55. CTREE_EXTENT_PENDING);
  56. tree_block_release(extent_root, gang[i]);
  57. }
  58. }
  59. return 0;
  60. }
  61. /*
  62. * remove an extent from the root, returns 0 on success
  63. */
  64. int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks)
  65. {
  66. struct ctree_path path;
  67. struct key key;
  68. struct ctree_root *extent_root = root->extent_root;
  69. struct tree_buffer *t;
  70. int pending_ret;
  71. int ret;
  72. key.objectid = blocknr;
  73. key.flags = 0;
  74. key.offset = num_blocks;
  75. if (root == extent_root) {
  76. t = read_tree_block(root, key.objectid);
  77. radix_tree_tag_set(&root->cache_radix, key.objectid,
  78. CTREE_EXTENT_PENDING);
  79. return 0;
  80. }
  81. init_path(&path);
  82. ret = search_slot(extent_root, &key, &path, 0);
  83. if (ret) {
  84. print_tree(extent_root, extent_root->node);
  85. printf("failed to find %Lu\n", key.objectid);
  86. BUG();
  87. }
  88. ret = del_item(extent_root, &path);
  89. if (ret)
  90. BUG();
  91. release_path(extent_root, &path);
  92. pending_ret = del_pending_extents(root->extent_root);
  93. return ret ? ret : pending_ret;
  94. }
  95. /*
  96. * walks the btree of allocated extents and find a hole of a given size.
  97. * The key ins is changed to record the hole:
  98. * ins->objectid == block start
  99. * ins->flags = 0
  100. * ins->offset == number of blocks
  101. * Any available blocks before search_start are skipped.
  102. */
  103. int find_free_extent(struct ctree_root *orig_root, u64 num_blocks,
  104. u64 search_start, u64 search_end, struct key *ins)
  105. {
  106. struct ctree_path path;
  107. struct key *key;
  108. int ret;
  109. u64 hole_size = 0;
  110. int slot = 0;
  111. u64 last_block;
  112. int start_found;
  113. struct leaf *l;
  114. struct ctree_root * root = orig_root->extent_root;
  115. check_failed:
  116. init_path(&path);
  117. ins->objectid = search_start;
  118. ins->offset = 0;
  119. ins->flags = 0;
  120. start_found = 0;
  121. ret = search_slot(root, ins, &path, 0);
  122. while (1) {
  123. l = &path.nodes[0]->leaf;
  124. slot = path.slots[0];
  125. if (slot >= l->header.nritems) {
  126. ret = next_leaf(root, &path);
  127. if (ret == 0)
  128. continue;
  129. if (!start_found) {
  130. ins->objectid = search_start;
  131. ins->offset = num_blocks;
  132. start_found = 1;
  133. goto check_pending;
  134. }
  135. ins->objectid = last_block > search_start ?
  136. last_block : search_start;
  137. ins->offset = num_blocks;
  138. goto check_pending;
  139. }
  140. key = &l->items[slot].key;
  141. if (key->objectid >= search_start) {
  142. if (start_found) {
  143. hole_size = key->objectid - last_block;
  144. if (hole_size > num_blocks) {
  145. ins->objectid = last_block;
  146. ins->offset = num_blocks;
  147. goto check_pending;
  148. }
  149. } else
  150. start_found = 1;
  151. last_block = key->objectid + key->offset;
  152. }
  153. path.slots[0]++;
  154. }
  155. // FIXME -ENOSPC
  156. check_pending:
  157. /* we have to make sure we didn't find an extent that has already
  158. * been allocated by the map tree or the original allocation
  159. */
  160. release_path(root, &path);
  161. BUG_ON(ins->objectid < search_start);
  162. if (orig_root->extent_root == orig_root) {
  163. BUG_ON(num_blocks != 1);
  164. if ((root->current_insert.objectid <= ins->objectid &&
  165. root->current_insert.objectid +
  166. root->current_insert.offset > ins->objectid) ||
  167. (root->current_insert.objectid > ins->objectid &&
  168. root->current_insert.objectid <= ins->objectid +
  169. ins->offset) ||
  170. radix_tree_tag_get(&root->cache_radix, ins->objectid,
  171. CTREE_EXTENT_PENDING)) {
  172. search_start = ins->objectid + 1;
  173. goto check_failed;
  174. }
  175. }
  176. if (ins->offset != 1)
  177. BUG();
  178. return 0;
  179. }
  180. /*
  181. * insert all of the pending extents reserved during the original
  182. * allocation. (CTREE_EXTENT_PENDING). Returns zero if it all worked out
  183. */
  184. static int insert_pending_extents(struct ctree_root *extent_root)
  185. {
  186. int ret;
  187. struct key key;
  188. struct extent_item item;
  189. struct tree_buffer *gang[4];
  190. int i;
  191. // FIXME -ENOSPC
  192. item.refs = 1;
  193. item.owner = extent_root->node->node.header.parentid;
  194. while(1) {
  195. ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
  196. (void **)gang, 0,
  197. ARRAY_SIZE(gang),
  198. CTREE_EXTENT_PENDING);
  199. if (!ret)
  200. break;
  201. for (i = 0; i < ret; i++) {
  202. key.objectid = gang[i]->blocknr;
  203. key.flags = 0;
  204. key.offset = 1;
  205. ret = insert_item(extent_root, &key, &item,
  206. sizeof(item));
  207. if (ret) {
  208. BUG();
  209. // FIXME undo it and return sane
  210. return ret;
  211. }
  212. radix_tree_tag_clear(&extent_root->cache_radix,
  213. gang[i]->blocknr,
  214. CTREE_EXTENT_PENDING);
  215. tree_block_release(extent_root, gang[i]);
  216. }
  217. }
  218. return 0;
  219. }
  220. /*
  221. * finds a free extent and does all the dirty work required for allocation
  222. * returns the key for the extent through ins, and a tree buffer for
  223. * the first block of the extent through buf.
  224. *
  225. * returns 0 if everything worked, non-zero otherwise.
  226. */
  227. int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
  228. u64 search_end, u64 owner, struct key *ins,
  229. struct tree_buffer **buf)
  230. {
  231. int ret;
  232. int pending_ret;
  233. struct extent_item extent_item;
  234. extent_item.refs = 1;
  235. extent_item.owner = owner;
  236. ret = find_free_extent(root, num_blocks, search_start, search_end, ins);
  237. if (ret)
  238. return ret;
  239. if (root != root->extent_root) {
  240. memcpy(&root->extent_root->current_insert, ins, sizeof(*ins));
  241. ret = insert_item(root->extent_root, ins, &extent_item,
  242. sizeof(extent_item));
  243. memset(&root->extent_root->current_insert, 0,
  244. sizeof(struct key));
  245. pending_ret = insert_pending_extents(root->extent_root);
  246. if (ret)
  247. return ret;
  248. if (pending_ret)
  249. return pending_ret;
  250. *buf = find_tree_block(root, ins->objectid);
  251. return 0;
  252. }
  253. /* we're allocating an extent for the extent tree, don't recurse */
  254. BUG_ON(ins->offset != 1);
  255. *buf = find_tree_block(root, ins->objectid);
  256. BUG_ON(!*buf);
  257. radix_tree_tag_set(&root->cache_radix, ins->objectid,
  258. CTREE_EXTENT_PENDING);
  259. (*buf)->count++;
  260. return 0;
  261. }
  262. /*
  263. * helper function to allocate a block for a given tree
  264. * returns the tree buffer or NULL.
  265. */
  266. struct tree_buffer *alloc_free_block(struct ctree_root *root)
  267. {
  268. struct key ins;
  269. int ret;
  270. struct tree_buffer *buf = NULL;
  271. ret = alloc_extent(root, 1, 0, (unsigned long)-1,
  272. root->node->node.header.parentid,
  273. &ins, &buf);
  274. if (ret) {
  275. BUG();
  276. return NULL;
  277. }
  278. if (root != root->extent_root)
  279. BUG_ON(radix_tree_tag_get(&root->extent_root->cache_radix,
  280. buf->blocknr, CTREE_EXTENT_PENDING));
  281. return buf;
  282. }