disk-io.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. static int get_free_block(struct ctree_root *root, u64 *block)
  14. {
  15. struct stat st;
  16. int ret = 0;
  17. if (root->alloc_extent->num_used >= root->alloc_extent->num_blocks)
  18. return -1;
  19. *block = root->alloc_extent->blocknr + root->alloc_extent->num_used;
  20. root->alloc_extent->num_used += 1;
  21. if (root->alloc_extent->num_used >= root->alloc_extent->num_blocks) {
  22. struct alloc_extent *ae = root->alloc_extent;
  23. root->alloc_extent = root->reserve_extent;
  24. root->reserve_extent = ae;
  25. ae->num_blocks = 0;
  26. }
  27. st.st_size = 0;
  28. ret = fstat(root->fp, &st);
  29. if (st.st_size < (*block + 1) * CTREE_BLOCKSIZE) {
  30. ret = ftruncate(root->fp,
  31. (*block + 1) * CTREE_BLOCKSIZE);
  32. if (ret) {
  33. perror("ftruncate");
  34. exit(1);
  35. }
  36. }
  37. return ret;
  38. }
  39. struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
  40. {
  41. struct tree_buffer *buf;
  42. int ret;
  43. buf = malloc(sizeof(struct tree_buffer));
  44. if (!buf)
  45. return buf;
  46. allocated_blocks++;
  47. buf->blocknr = blocknr;
  48. buf->count = 1;
  49. radix_tree_preload(GFP_KERNEL);
  50. ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
  51. radix_tree_preload_end();
  52. if (ret) {
  53. free(buf);
  54. return NULL;
  55. }
  56. return buf;
  57. }
  58. struct tree_buffer *alloc_free_block(struct ctree_root *root)
  59. {
  60. u64 free_block;
  61. int ret;
  62. struct tree_buffer * buf;
  63. ret = get_free_block(root, &free_block);
  64. if (ret) {
  65. BUG();
  66. return NULL;
  67. }
  68. buf = alloc_tree_block(root, free_block);
  69. if (!buf)
  70. BUG();
  71. return buf;
  72. }
  73. struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
  74. {
  75. loff_t offset = blocknr * CTREE_BLOCKSIZE;
  76. struct tree_buffer *buf;
  77. int ret;
  78. buf = radix_tree_lookup(&root->cache_radix, blocknr);
  79. if (buf) {
  80. buf->count++;
  81. goto test;
  82. }
  83. buf = alloc_tree_block(root, blocknr);
  84. if (!buf)
  85. return NULL;
  86. ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
  87. if (ret != CTREE_BLOCKSIZE) {
  88. free(buf);
  89. return NULL;
  90. }
  91. test:
  92. if (buf->blocknr != buf->node.header.blocknr)
  93. BUG();
  94. if (root->node && buf->node.header.parentid != root->node->node.header.parentid)
  95. BUG();
  96. return buf;
  97. }
  98. int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
  99. {
  100. u64 blocknr = buf->blocknr;
  101. loff_t offset = blocknr * CTREE_BLOCKSIZE;
  102. int ret;
  103. if (buf->blocknr != buf->node.header.blocknr)
  104. BUG();
  105. ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
  106. if (ret != CTREE_BLOCKSIZE)
  107. return ret;
  108. return 0;
  109. }
  110. static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root,
  111. struct ctree_root_info *info, int fp)
  112. {
  113. INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
  114. root->fp = fp;
  115. root->node = NULL;
  116. root->node = read_tree_block(root, info->tree_root);
  117. root->extent_root = extent_root;
  118. memcpy(&root->ai1, &info->alloc_extent, sizeof(info->alloc_extent));
  119. memcpy(&root->ai2, &info->reserve_extent, sizeof(info->reserve_extent));
  120. root->alloc_extent = &root->ai1;
  121. root->reserve_extent = &root->ai2;
  122. printf("setup done reading root %p, used %lu available %lu\n", root, root->alloc_extent->num_used, root->alloc_extent->num_blocks);
  123. printf("setup done reading root %p, reserve used %lu available %lu\n", root, root->reserve_extent->num_used, root->reserve_extent->num_blocks);
  124. return 0;
  125. }
  126. struct ctree_root *open_ctree(char *filename, struct ctree_super_block *super)
  127. {
  128. struct ctree_root *root = malloc(sizeof(struct ctree_root));
  129. struct ctree_root *extent_root = malloc(sizeof(struct ctree_root));
  130. int fp;
  131. int ret;
  132. fp = open(filename, O_CREAT | O_RDWR);
  133. if (fp < 0) {
  134. free(root);
  135. return NULL;
  136. }
  137. ret = pread(fp, super, sizeof(struct ctree_super_block),
  138. CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  139. if (ret == 0 || super->root_info.tree_root == 0) {
  140. printf("making new FS!\n");
  141. ret = mkfs(fp);
  142. if (ret)
  143. return NULL;
  144. ret = pread(fp, super, sizeof(struct ctree_super_block),
  145. CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  146. if (ret != sizeof(struct ctree_super_block))
  147. return NULL;
  148. }
  149. BUG_ON(ret < 0);
  150. __setup_root(root, extent_root, &super->root_info, fp);
  151. __setup_root(extent_root, extent_root, &super->extent_info, fp);
  152. return root;
  153. }
  154. static int __update_root(struct ctree_root *root, struct ctree_root_info *info)
  155. {
  156. info->tree_root = root->node->blocknr;
  157. memcpy(&info->alloc_extent, root->alloc_extent, sizeof(struct alloc_extent));
  158. memcpy(&info->reserve_extent, root->reserve_extent, sizeof(struct alloc_extent));
  159. return 0;
  160. }
  161. int write_ctree_super(struct ctree_root *root, struct ctree_super_block *s)
  162. {
  163. int ret;
  164. __update_root(root, &s->root_info);
  165. __update_root(root->extent_root, &s->extent_info);
  166. ret = pwrite(root->fp, s, sizeof(*s), CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  167. if (ret != sizeof(*s)) {
  168. fprintf(stderr, "failed to write new super block err %d\n", ret);
  169. return ret;
  170. }
  171. return 0;
  172. }
  173. int close_ctree(struct ctree_root *root)
  174. {
  175. close(root->fp);
  176. if (root->node)
  177. tree_block_release(root, root->node);
  178. if (root->extent_root->node)
  179. tree_block_release(root->extent_root, root->extent_root->node);
  180. free(root);
  181. printf("on close %d blocks are allocated\n", allocated_blocks);
  182. return 0;
  183. }
  184. void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
  185. {
  186. buf->count--;
  187. if (buf->count < 0)
  188. BUG();
  189. if (buf->count == 0) {
  190. if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
  191. BUG();
  192. radix_tree_delete(&root->cache_radix, buf->blocknr);
  193. memset(buf, 0, sizeof(*buf));
  194. free(buf);
  195. BUG_ON(allocated_blocks == 0);
  196. allocated_blocks--;
  197. }
  198. }