disk-io.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_size = 0;
  14. int cache_max = 10000;
  15. static int check_tree_block(struct ctree_root *root, struct tree_buffer *buf)
  16. {
  17. if (buf->blocknr != buf->node.header.blocknr)
  18. BUG();
  19. if (root->node && buf->node.header.parentid != root->node->node.header.parentid)
  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. return 0;
  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 != buf->node.header.blocknr)
  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)
  145. {
  146. int ret;
  147. ret = __commit_transaction(root);
  148. if (!ret && root != root->extent_root)
  149. ret = __commit_transaction(root->extent_root);
  150. BUG_ON(ret);
  151. return ret;
  152. }
  153. static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root,
  154. struct ctree_root_info *info, int fp)
  155. {
  156. INIT_LIST_HEAD(&root->trans);
  157. INIT_LIST_HEAD(&root->cache);
  158. root->fp = fp;
  159. root->node = NULL;
  160. root->node = read_tree_block(root, info->tree_root);
  161. root->extent_root = extent_root;
  162. return 0;
  163. }
  164. struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super)
  165. {
  166. struct ctree_root *root = malloc(sizeof(struct ctree_root));
  167. struct ctree_root *extent_root = malloc(sizeof(struct ctree_root));
  168. int fp;
  169. int ret;
  170. fp = open(filename, O_CREAT | O_RDWR, 0600);
  171. if (fp < 0) {
  172. free(root);
  173. return NULL;
  174. }
  175. INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
  176. INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
  177. ret = pread(fp, super, sizeof(struct ctree_super_block),
  178. CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  179. if (ret == 0 || super->root_info.tree_root == 0) {
  180. printf("making new FS!\n");
  181. ret = mkfs(fp);
  182. if (ret)
  183. return NULL;
  184. ret = pread(fp, super, sizeof(struct ctree_super_block),
  185. CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  186. if (ret != sizeof(struct ctree_super_block))
  187. return NULL;
  188. }
  189. BUG_ON(ret < 0);
  190. __setup_root(root, extent_root, &super->root_info, fp);
  191. __setup_root(extent_root, extent_root, &super->extent_info, fp);
  192. return root;
  193. }
  194. static int __update_root(struct ctree_root *root, struct ctree_root_info *info)
  195. {
  196. info->tree_root = root->node->blocknr;
  197. return 0;
  198. }
  199. int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s)
  200. {
  201. int ret;
  202. __update_root(root, &s->root_info);
  203. __update_root(root->extent_root, &s->extent_info);
  204. ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  205. if (ret != sizeof(*s)) {
  206. fprintf(stderr, "failed to write new super block err %d\n", ret);
  207. return ret;
  208. }
  209. return 0;
  210. }
  211. static int drop_cache(struct ctree_root *root)
  212. {
  213. while(!list_empty(&root->cache)) {
  214. struct tree_buffer *b = list_entry(root->cache.next,
  215. struct tree_buffer, cache);
  216. list_del_init(&b->cache);
  217. tree_block_release(root, b);
  218. }
  219. return 0;
  220. }
  221. int close_ctree(struct ctree_root *root)
  222. {
  223. commit_transaction(root);
  224. drop_cache(root->extent_root);
  225. drop_cache(root);
  226. BUG_ON(!list_empty(&root->trans));
  227. BUG_ON(!list_empty(&root->extent_root->trans));
  228. close(root->fp);
  229. if (root->node)
  230. tree_block_release(root, root->node);
  231. if (root->extent_root->node)
  232. tree_block_release(root->extent_root, root->extent_root->node);
  233. free(root);
  234. printf("on close %d blocks are allocated\n", allocated_blocks);
  235. return 0;
  236. }
  237. void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
  238. {
  239. buf->count--;
  240. if (buf->count < 0)
  241. BUG();
  242. if (buf->count == 0) {
  243. if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
  244. BUG();
  245. radix_tree_delete(&root->cache_radix, buf->blocknr);
  246. memset(buf, 0, sizeof(*buf));
  247. free(buf);
  248. BUG_ON(allocated_blocks == 0);
  249. allocated_blocks--;
  250. BUG_ON(root->cache_size == 0);
  251. root->cache_size--;
  252. }
  253. }