disk-io.c 7.3 KB

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