disk-io.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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;
  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. return ret;
  33. }
  34. struct tree_buffer *alloc_tree_block(struct ctree_root *root, u64 blocknr)
  35. {
  36. struct tree_buffer *buf;
  37. int ret;
  38. buf = malloc(sizeof(struct tree_buffer));
  39. if (!buf)
  40. return buf;
  41. allocated_blocks++;
  42. buf->blocknr = blocknr;
  43. buf->count = 1;
  44. radix_tree_preload(GFP_KERNEL);
  45. ret = radix_tree_insert(&root->cache_radix, blocknr, buf);
  46. radix_tree_preload_end();
  47. if (ret) {
  48. free(buf);
  49. return NULL;
  50. }
  51. return buf;
  52. }
  53. struct tree_buffer *alloc_free_block(struct ctree_root *root)
  54. {
  55. u64 free_block;
  56. int ret;
  57. struct tree_buffer * buf;
  58. ret = get_free_block(root, &free_block);
  59. if (ret) {
  60. BUG();
  61. return NULL;
  62. }
  63. buf = alloc_tree_block(root, free_block);
  64. if (!buf)
  65. BUG();
  66. return buf;
  67. }
  68. struct tree_buffer *read_tree_block(struct ctree_root *root, u64 blocknr)
  69. {
  70. loff_t offset = blocknr * CTREE_BLOCKSIZE;
  71. struct tree_buffer *buf;
  72. int ret;
  73. buf = radix_tree_lookup(&root->cache_radix, blocknr);
  74. if (buf) {
  75. buf->count++;
  76. if (buf->blocknr != blocknr)
  77. BUG();
  78. if (buf->blocknr != buf->node.header.blocknr)
  79. BUG();
  80. return buf;
  81. }
  82. buf = alloc_tree_block(root, blocknr);
  83. if (!buf)
  84. return NULL;
  85. ret = pread(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
  86. if (ret != CTREE_BLOCKSIZE) {
  87. free(buf);
  88. return NULL;
  89. }
  90. if (buf->blocknr != buf->node.header.blocknr)
  91. BUG();
  92. return buf;
  93. }
  94. int write_tree_block(struct ctree_root *root, struct tree_buffer *buf)
  95. {
  96. u64 blocknr = buf->blocknr;
  97. loff_t offset = blocknr * CTREE_BLOCKSIZE;
  98. int ret;
  99. if (buf->blocknr != buf->node.header.blocknr)
  100. BUG();
  101. ret = pwrite(root->fp, &buf->node, CTREE_BLOCKSIZE, offset);
  102. if (ret != CTREE_BLOCKSIZE)
  103. return ret;
  104. if (buf == root->node)
  105. return update_root_block(root);
  106. return 0;
  107. }
  108. struct ctree_super_block {
  109. struct ctree_root_info root_info;
  110. struct ctree_root_info extent_info;
  111. } __attribute__ ((__packed__));
  112. static int __setup_root(struct ctree_root *root, struct ctree_root *extent_root,
  113. struct ctree_root_info *info, int fp)
  114. {
  115. root->fp = fp;
  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. INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
  123. printf("setup done reading root %p, used %lu\n", root, root->alloc_extent->num_used);
  124. return 0;
  125. }
  126. struct ctree_root *open_ctree(char *filename)
  127. {
  128. struct ctree_root *root = malloc(sizeof(struct ctree_root));
  129. struct ctree_root *extent_root = malloc(sizeof(struct ctree_root));
  130. struct ctree_super_block super;
  131. int fp;
  132. int ret;
  133. fp = open(filename, O_CREAT | O_RDWR);
  134. if (fp < 0) {
  135. free(root);
  136. return NULL;
  137. }
  138. ret = pread(fp, &super, sizeof(struct ctree_super_block),
  139. CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
  140. if (ret == 0) {
  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. int close_ctree(struct ctree_root *root)
  155. {
  156. close(root->fp);
  157. if (root->node)
  158. tree_block_release(root, root->node);
  159. free(root);
  160. printf("on close %d blocks are allocated\n", allocated_blocks);
  161. return 0;
  162. }
  163. int update_root_block(struct ctree_root *root)
  164. {
  165. int ret;
  166. u64 root_block = root->node->blocknr;
  167. ret = pwrite(root->fp, &root_block, sizeof(u64), 0);
  168. if (ret != sizeof(u64))
  169. return ret;
  170. return 0;
  171. }
  172. void tree_block_release(struct ctree_root *root, struct tree_buffer *buf)
  173. {
  174. return;
  175. buf->count--;
  176. if (buf->count == 0) {
  177. if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
  178. BUG();
  179. radix_tree_delete(&root->cache_radix, buf->blocknr);
  180. memset(buf, 0, sizeof(*buf));
  181. free(buf);
  182. BUG_ON(allocated_blocks == 0);
  183. allocated_blocks--;
  184. }
  185. }