disk-io.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #define _XOPEN_SOURCE 500
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include "kerncompat.h"
  9. #include "radix-tree.h"
  10. #include "ctree.h"
  11. #include "disk-io.h"
  12. static int allocated_blocks = 0;
  13. int cache_max = 10000;
  14. static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
  15. {
  16. if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
  17. BUG();
  18. if (root->node && btrfs_header_parentid(&buf->node.header) !=
  19. btrfs_header_parentid(&root->node->node.header))
  20. BUG();
  21. return 0;
  22. }
  23. static int free_some_buffers(struct ctree_root *root)
  24. {
  25. struct list_head *node, *next;
  26. struct tree_buffer *b;
  27. if (root->cache_size < cache_max)
  28. return 0;
  29. list_for_each_safe(node, next, &root->cache) {
  30. b = list_entry(node, struct tree_buffer, cache);
  31. if (b->count == 1) {
  32. BUG_ON(!list_empty(&b->dirty));
  33. list_del_init(&b->cache);
  34. tree_block_release(root, b);
  35. if (root->cache_size < cache_max)
  36. break;
  37. }
  38. }
  39. return 0;
  40. }
  41. struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
  42. {
  43. struct tree_buffer *buf;
  44. int ret;
  45. buf = malloc(sizeof(struct tree_buffer));
  46. if (!buf)
  47. return buf;
  48. allocated_blocks++;
  49. buf->blocknr = blocknr;
  50. buf->count = 2;
  51. INIT_LIST_HEAD(&buf->dirty);
  52. free_some_buffers(root);
  53. radix_tree_preload(GFP_KERNEL);
  54. ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
  55. radix_tree_preload_end();
  56. list_add_tail(&buf->cache, &root->cache);
  57. root->cache_size++;
  58. if (ret) {
  59. free(buf);
  60. return NULL;
  61. }
  62. return buf;
  63. }
  64. struct tree_buffer *find_tree_block(struct ctree_root *root, u64 blocknr)
  65. {
  66. struct tree_buffer *buf;
  67. buf = radix_tree_lookup(&root->cache_radix, blocknr);
  68. if (buf) {
  69. buf->count++;
  70. } else {
  71. buf = alloc_tree_block(root, blocknr);
  72. if (!buf) {
  73. BUG();
  74. return NULL;
  75. }
  76. }
  77. return buf;
  78. }
  79. struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
  80. {
  81. loff_t offset = blocknr * CTREE_BLOCKSIZE;
  82. struct tree_buffer *buf;
  83. int ret;
  84. buf = radix_tree_lookup(&root->cache_radix, blocknr);
  85. if (buf) {
  86. buf->count++;
  87. } else {
  88. buf = alloc_tree_block(root, blocknr);
  89. if (!buf)
  90. return NULL;
  91. ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
  92. if (ret != CTREE_BLOCKSIZE) {
  93. free(buf);
  94. return NULL;
  95. }
  96. }
  97. if (check_tree_block(root, buf))
  98. BUG();
  99. return buf;
  100. }
  101. int dirty_tree_block(struct ctree_root *root, struct tree_buffer *buf)
  102. {
  103. if (!list_empty(&buf->dirty))
  104. return 0;
  105. list_add_tail(&buf->dirty, &root->trans);
  106. buf->count++;
  107. return 0;
  108. }
  109. int clean_tree_block(struct ctree_root *root, struct tree_buffer *buf)
  110. {
  111. if (!list_empty(&buf->dirty)) {
  112. list_del_init(&buf->dirty);
  113. tree_block_release(root, buf);
  114. }
  115. return 0;
  116. }
  117. int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
  118. {
  119. u64 blocknr = buf->blocknr;
  120. loff_t offset = blocknr * CTREE_BLOCKSIZE;
  121. int ret;
  122. if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
  123. BUG();
  124. ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
  125. if (ret != CTREE_BLOCKSIZE)
  126. return ret;
  127. return 0;
  128. }
  129. static int __commit_transaction(struct ctree_root *root)
  130. {
  131. struct tree_buffer *b;
  132. int ret = 0;
  133. int wret;
  134. while(!list_empty(&root->trans)) {
  135. b = list_entry(root->trans.next, struct tree_buffer, dirty);
  136. list_del_init(&b->dirty);
  137. wret = write_tree_block(root, b);
  138. if (wret)
  139. ret = wret;
  140. tree_block_release(root, b);
  141. }
  142. return ret;
  143. }
  144. int commit_transaction(struct ctree_root *root, struct ctree_super_block *s)
  145. {
  146. int ret = 0;
  147. ret = __commit_transaction(root);
  148. if (!ret && root != root->extent_root)
  149. ret = __commit_transaction(root->extent_root);
  150. BUG_ON(ret);
  151. if (root->commit_root != root->node) {
  152. struct tree_buffer *snap = root->commit_root;
  153. root->commit_root = root->node;
  154. root->node->count++;
  155. ret = btrfs_drop_snapshot(root, snap);
  156. BUG_ON(ret);
  157. // tree_block_release(root, snap);
  158. }
  159. write_ctree_super(root, s);
  160. btrfs_finish_extent_commit(root);
  161. return ret;
  162. }
  163. static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root,
  164. struct ctree_root_info *info, int fp)
  165. {
  166. INIT_LIST_HEAD(&root->trans);
  167. INIT_LIST_HEAD(&root->cache);
  168. root->cache_size = 0;
  169. root->fp = fp;
  170. root->node = NULL;
  171. root->extent_root = extent_root;
  172. root->commit_root = NULL;
  173. root->node = read_tree_block(root, info->tree_root);
  174. memset(&root->current_insert, 0, sizeof(root->current_insert));
  175. memset(&root->last_insert, 0, sizeof(root->last_insert));
  176. return 0;
  177. }
  178. struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super)
  179. {
  180. struct ctree_root *root = malloc(sizeof(struct ctree_root));
  181. struct ctree_root *extent_root = malloc(sizeof(struct ctree_root));
  182. int fp;
  183. int ret;
  184. fp = open(filename, O_CREAT | O_RDWR, 0600);
  185. if (fp < 0) {
  186. free(root);
  187. return NULL;
  188. }
  189. INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
  190. INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL);
  191. INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL);
  192. INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
  193. ret = pread(fp, super, sizeof(struct ctree_super_block),
  194. CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  195. if (ret == 0 || super->root_info.tree_root == 0) {
  196. printf("making new FS!\n");
  197. ret = mkfs(fp);
  198. if (ret)
  199. return NULL;
  200. ret = pread(fp, super, sizeof(struct ctree_super_block),
  201. CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  202. if (ret != sizeof(struct ctree_super_block))
  203. return NULL;
  204. }
  205. BUG_ON(ret < 0);
  206. __setup_root(root, extent_root, &super->root_info, fp);
  207. __setup_root(extent_root, extent_root, &super->extent_info, fp);
  208. root->commit_root = root->node;
  209. root->node->count++;
  210. return root;
  211. }
  212. static int __update_root(struct ctree_root *root, struct ctree_root_info *info)
  213. {
  214. info->tree_root = root->node->blocknr;
  215. return 0;
  216. }
  217. int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s)
  218. {
  219. int ret;
  220. __update_root(root, &s->root_info);
  221. __update_root(root->extent_root, &s->extent_info);
  222. ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  223. if (ret != sizeof(*s)) {
  224. fprintf(stderr, "failed to write new super block err %d\n", ret);
  225. return ret;
  226. }
  227. return 0;
  228. }
  229. static int drop_cache(struct ctree_root *root)
  230. {
  231. while(!list_empty(&root->cache)) {
  232. struct tree_buffer *b = list_entry(root->cache.next,
  233. struct tree_buffer, cache);
  234. list_del_init(&b->cache);
  235. tree_block_release(root, b);
  236. }
  237. return 0;
  238. }
  239. int close_ctree(struct ctree_root *root, struct ctree_super_block *s)
  240. {
  241. commit_transaction(root, s);
  242. __commit_transaction(root->extent_root);
  243. write_ctree_super(root, s);
  244. drop_cache(root->extent_root);
  245. drop_cache(root);
  246. BUG_ON(!list_empty(&root->trans));
  247. BUG_ON(!list_empty(&root->extent_root->trans));
  248. close(root->fp);
  249. if (root->node)
  250. tree_block_release(root, root->node);
  251. if (root->extent_root->node)
  252. tree_block_release(root->extent_root, root->extent_root->node);
  253. tree_block_release(root, root->commit_root);
  254. free(root);
  255. printf("on close %d blocks are allocated\n", allocated_blocks);
  256. return 0;
  257. }
  258. void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
  259. {
  260. buf->count--;
  261. if (buf->count < 0)
  262. BUG();
  263. if (buf->count == 0) {
  264. BUG_ON(!list_empty(&buf->cache));
  265. BUG_ON(!list_empty(&buf->dirty));
  266. if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
  267. BUG();
  268. radix_tree_delete(&root->cache_radix, buf->blocknr);
  269. memset(buf, 0, sizeof(*buf));
  270. free(buf);
  271. BUG_ON(allocated_blocks == 0);
  272. allocated_blocks--;
  273. BUG_ON(root->cache_size == 0);
  274. root->cache_size--;
  275. }
  276. }