extent-tree.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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, -1);
  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, -1);
  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. static 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. if (ret < 0)
  123. goto error;
  124. while (1) {
  125. l = &path.nodes[0]->leaf;
  126. slot = path.slots[0];
  127. if (slot >= l->header.nritems) {
  128. ret = next_leaf(root, &path);
  129. if (ret == 0)
  130. continue;
  131. if (ret < 0)
  132. goto error;
  133. if (!start_found) {
  134. ins->objectid = search_start;
  135. ins->offset = num_blocks;
  136. start_found = 1;
  137. goto check_pending;
  138. }
  139. ins->objectid = last_block > search_start ?
  140. last_block : search_start;
  141. ins->offset = num_blocks;
  142. goto check_pending;
  143. }
  144. key = &l->items[slot].key;
  145. if (key->objectid >= search_start) {
  146. if (start_found) {
  147. hole_size = key->objectid - last_block;
  148. if (hole_size > num_blocks) {
  149. ins->objectid = last_block;
  150. ins->offset = num_blocks;
  151. goto check_pending;
  152. }
  153. } else
  154. start_found = 1;
  155. last_block = key->objectid + key->offset;
  156. }
  157. path.slots[0]++;
  158. }
  159. // FIXME -ENOSPC
  160. check_pending:
  161. /* we have to make sure we didn't find an extent that has already
  162. * been allocated by the map tree or the original allocation
  163. */
  164. release_path(root, &path);
  165. BUG_ON(ins->objectid < search_start);
  166. if (orig_root->extent_root == orig_root) {
  167. BUG_ON(num_blocks != 1);
  168. if ((root->current_insert.objectid <= ins->objectid &&
  169. root->current_insert.objectid +
  170. root->current_insert.offset > ins->objectid) ||
  171. (root->current_insert.objectid > ins->objectid &&
  172. root->current_insert.objectid <= ins->objectid +
  173. ins->offset) ||
  174. radix_tree_tag_get(&root->cache_radix, ins->objectid,
  175. CTREE_EXTENT_PENDING)) {
  176. search_start = ins->objectid + 1;
  177. goto check_failed;
  178. }
  179. }
  180. if (ins->offset != 1)
  181. BUG();
  182. return 0;
  183. error:
  184. release_path(root, &path);
  185. return ret;
  186. }
  187. /*
  188. * insert all of the pending extents reserved during the original
  189. * allocation. (CTREE_EXTENT_PENDING). Returns zero if it all worked out
  190. */
  191. static int insert_pending_extents(struct ctree_root *extent_root)
  192. {
  193. int ret;
  194. struct key key;
  195. struct extent_item item;
  196. struct tree_buffer *gang[4];
  197. int i;
  198. // FIXME -ENOSPC
  199. item.refs = 1;
  200. item.owner = extent_root->node->node.header.parentid;
  201. while(1) {
  202. ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix,
  203. (void **)gang, 0,
  204. ARRAY_SIZE(gang),
  205. CTREE_EXTENT_PENDING);
  206. if (!ret)
  207. break;
  208. for (i = 0; i < ret; i++) {
  209. key.objectid = gang[i]->blocknr;
  210. key.flags = 0;
  211. key.offset = 1;
  212. ret = insert_item(extent_root, &key, &item,
  213. sizeof(item));
  214. if (ret) {
  215. BUG();
  216. // FIXME undo it and return sane
  217. return ret;
  218. }
  219. radix_tree_tag_clear(&extent_root->cache_radix,
  220. gang[i]->blocknr,
  221. CTREE_EXTENT_PENDING);
  222. tree_block_release(extent_root, gang[i]);
  223. }
  224. }
  225. return 0;
  226. }
  227. /*
  228. * finds a free extent and does all the dirty work required for allocation
  229. * returns the key for the extent through ins, and a tree buffer for
  230. * the first block of the extent through buf.
  231. *
  232. * returns 0 if everything worked, non-zero otherwise.
  233. */
  234. int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start,
  235. u64 search_end, u64 owner, struct key *ins,
  236. struct tree_buffer **buf)
  237. {
  238. int ret;
  239. int pending_ret;
  240. struct extent_item extent_item;
  241. extent_item.refs = 1;
  242. extent_item.owner = owner;
  243. ret = find_free_extent(root, num_blocks, search_start, search_end, ins);
  244. if (ret)
  245. return ret;
  246. if (root != root->extent_root) {
  247. memcpy(&root->extent_root->current_insert, ins, sizeof(*ins));
  248. ret = insert_item(root->extent_root, ins, &extent_item,
  249. sizeof(extent_item));
  250. memset(&root->extent_root->current_insert, 0,
  251. sizeof(struct key));
  252. pending_ret = insert_pending_extents(root->extent_root);
  253. if (ret)
  254. return ret;
  255. if (pending_ret)
  256. return pending_ret;
  257. *buf = find_tree_block(root, ins->objectid);
  258. return 0;
  259. }
  260. /* we're allocating an extent for the extent tree, don't recurse */
  261. BUG_ON(ins->offset != 1);
  262. *buf = find_tree_block(root, ins->objectid);
  263. BUG_ON(!*buf);
  264. radix_tree_tag_set(&root->cache_radix, ins->objectid,
  265. CTREE_EXTENT_PENDING);
  266. (*buf)->count++;
  267. return 0;
  268. }
  269. /*
  270. * helper function to allocate a block for a given tree
  271. * returns the tree buffer or NULL.
  272. */
  273. struct tree_buffer *alloc_free_block(struct ctree_root *root)
  274. {
  275. struct key ins;
  276. int ret;
  277. struct tree_buffer *buf = NULL;
  278. ret = alloc_extent(root, 1, 0, (unsigned long)-1,
  279. root->node->node.header.parentid,
  280. &ins, &buf);
  281. if (ret) {
  282. BUG();
  283. return NULL;
  284. }
  285. if (root != root->extent_root)
  286. BUG_ON(radix_tree_tag_get(&root->extent_root->cache_radix,
  287. buf->blocknr, CTREE_EXTENT_PENDING));
  288. return buf;
  289. }