disk-io.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 btrfs_root *root, struct btrfs_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 btrfs_root *root)
  24. {
  25. struct list_head *node, *next;
  26. struct btrfs_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 btrfs_buffer, cache);
  31. if (b->count == 1) {
  32. BUG_ON(!list_empty(&b->dirty));
  33. list_del_init(&b->cache);
  34. btrfs_block_release(root, b);
  35. if (root->cache_size < cache_max)
  36. break;
  37. }
  38. }
  39. return 0;
  40. }
  41. struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
  42. {
  43. struct btrfs_buffer *buf;
  44. int ret;
  45. buf = malloc(sizeof(struct btrfs_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 btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
  65. {
  66. struct btrfs_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 btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
  80. {
  81. loff_t offset = blocknr * BTRFS_BLOCKSIZE;
  82. struct btrfs_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, BTRFS_BLOCKSIZE, offset);
  92. if (ret != BTRFS_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 btrfs_root *root, struct btrfs_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 btrfs_root *root, struct btrfs_buffer *buf)
  110. {
  111. if (!list_empty(&buf->dirty)) {
  112. list_del_init(&buf->dirty);
  113. btrfs_block_release(root, buf);
  114. }
  115. return 0;
  116. }
  117. int write_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
  118. {
  119. u64 blocknr = buf->blocknr;
  120. loff_t offset = blocknr * BTRFS_BLOCKSIZE;
  121. int ret;
  122. if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
  123. BUG();
  124. ret = pwrite(root->fp, &buf->node, BTRFS_BLOCKSIZE, offset);
  125. if (ret != BTRFS_BLOCKSIZE)
  126. return ret;
  127. return 0;
  128. }
  129. static int __commit_transaction(struct btrfs_root *root)
  130. {
  131. struct btrfs_buffer *b;
  132. int ret = 0;
  133. int wret;
  134. while(!list_empty(&root->trans)) {
  135. b = list_entry(root->trans.next, struct btrfs_buffer, dirty);
  136. list_del_init(&b->dirty);
  137. wret = write_tree_block(root, b);
  138. if (wret)
  139. ret = wret;
  140. btrfs_block_release(root, b);
  141. }
  142. return ret;
  143. }
  144. static int commit_extent_and_tree_roots(struct btrfs_root *tree_root,
  145. struct btrfs_root *extent_root)
  146. {
  147. int ret;
  148. u64 old_extent_block;
  149. while(1) {
  150. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  151. if (old_extent_block == extent_root->node->blocknr)
  152. break;
  153. btrfs_set_root_blocknr(&extent_root->root_item,
  154. extent_root->node->blocknr);
  155. ret = btrfs_update_root(tree_root,
  156. &extent_root->root_key,
  157. &extent_root->root_item);
  158. BUG_ON(ret);
  159. }
  160. __commit_transaction(extent_root);
  161. __commit_transaction(tree_root);
  162. return 0;
  163. }
  164. int btrfs_commit_transaction(struct btrfs_root *root,
  165. struct btrfs_super_block *s)
  166. {
  167. int ret = 0;
  168. struct btrfs_buffer *snap = root->commit_root;
  169. struct btrfs_key snap_key;
  170. ret = __commit_transaction(root);
  171. BUG_ON(ret);
  172. if (root->commit_root == root->node)
  173. return 0;
  174. memcpy(&snap_key, &root->root_key, sizeof(snap_key));
  175. root->root_key.offset++;
  176. btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
  177. ret = btrfs_insert_root(root->tree_root, &root->root_key,
  178. &root->root_item);
  179. BUG_ON(ret);
  180. ret = commit_extent_and_tree_roots(root->tree_root, root->extent_root);
  181. BUG_ON(ret);
  182. write_ctree_super(root, s);
  183. btrfs_finish_extent_commit(root->extent_root);
  184. btrfs_finish_extent_commit(root->tree_root);
  185. root->commit_root = root->node;
  186. root->node->count++;
  187. ret = btrfs_drop_snapshot(root, snap);
  188. BUG_ON(ret);
  189. ret = btrfs_del_root(root->tree_root, &snap_key);
  190. BUG_ON(ret);
  191. return ret;
  192. }
  193. static int __setup_root(struct btrfs_root *root, u64 objectid, int fp)
  194. {
  195. INIT_LIST_HEAD(&root->trans);
  196. INIT_LIST_HEAD(&root->cache);
  197. root->cache_size = 0;
  198. root->fp = fp;
  199. root->node = NULL;
  200. root->commit_root = NULL;
  201. memset(&root->current_insert, 0, sizeof(root->current_insert));
  202. memset(&root->last_insert, 0, sizeof(root->last_insert));
  203. memset(&root->root_key, 0, sizeof(root->root_key));
  204. memset(&root->root_item, 0, sizeof(root->root_item));
  205. return 0;
  206. }
  207. static int find_and_setup_root(struct btrfs_root *tree_root, u64 objectid,
  208. struct btrfs_root *root, int fp)
  209. {
  210. int ret;
  211. __setup_root(root, objectid, fp);
  212. ret = btrfs_find_last_root(tree_root, objectid,
  213. &root->root_item, &root->root_key);
  214. BUG_ON(ret);
  215. root->node = read_tree_block(root,
  216. btrfs_root_blocknr(&root->root_item));
  217. root->ref_cows = 0;
  218. BUG_ON(!root->node);
  219. return 0;
  220. }
  221. struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
  222. {
  223. struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
  224. struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
  225. struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
  226. int fp;
  227. int ret;
  228. root->extent_root = extent_root;
  229. root->tree_root = tree_root;
  230. extent_root->extent_root = extent_root;
  231. extent_root->tree_root = tree_root;
  232. tree_root->extent_root = extent_root;
  233. tree_root->tree_root = tree_root;
  234. fp = open(filename, O_CREAT | O_RDWR, 0600);
  235. if (fp < 0) {
  236. free(root);
  237. return NULL;
  238. }
  239. INIT_RADIX_TREE(&root->cache_radix, GFP_KERNEL);
  240. INIT_RADIX_TREE(&root->pinned_radix, GFP_KERNEL);
  241. INIT_RADIX_TREE(&extent_root->pinned_radix, GFP_KERNEL);
  242. INIT_RADIX_TREE(&extent_root->cache_radix, GFP_KERNEL);
  243. INIT_RADIX_TREE(&tree_root->pinned_radix, GFP_KERNEL);
  244. INIT_RADIX_TREE(&tree_root->cache_radix, GFP_KERNEL);
  245. ret = pread(fp, super, sizeof(struct btrfs_super_block),
  246. BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
  247. if (ret == 0 || btrfs_super_root(super) == 0) {
  248. printf("making new FS!\n");
  249. ret = mkfs(fp, 0, BTRFS_BLOCKSIZE);
  250. if (ret)
  251. return NULL;
  252. ret = pread(fp, super, sizeof(struct btrfs_super_block),
  253. BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
  254. if (ret != sizeof(struct btrfs_super_block))
  255. return NULL;
  256. }
  257. BUG_ON(ret < 0);
  258. __setup_root(tree_root, BTRFS_ROOT_TREE_OBJECTID, fp);
  259. tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
  260. BUG_ON(!tree_root->node);
  261. ret = find_and_setup_root(tree_root, BTRFS_EXTENT_TREE_OBJECTID,
  262. extent_root, fp);
  263. BUG_ON(ret);
  264. ret = find_and_setup_root(tree_root, BTRFS_FS_TREE_OBJECTID,
  265. root, fp);
  266. BUG_ON(ret);
  267. root->commit_root = root->node;
  268. root->node->count++;
  269. root->ref_cows = 1;
  270. return root;
  271. }
  272. int write_ctree_super(struct btrfs_root *root, struct btrfs_super_block *s)
  273. {
  274. int ret;
  275. btrfs_set_super_root(s, root->tree_root->node->blocknr);
  276. ret = pwrite(root->fp, s, sizeof(*s),
  277. BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
  278. if (ret != sizeof(*s)) {
  279. fprintf(stderr, "failed to write new super block err %d\n", ret);
  280. return ret;
  281. }
  282. return 0;
  283. }
  284. static int drop_cache(struct btrfs_root *root)
  285. {
  286. while(!list_empty(&root->cache)) {
  287. struct btrfs_buffer *b = list_entry(root->cache.next,
  288. struct btrfs_buffer, cache);
  289. list_del_init(&b->cache);
  290. btrfs_block_release(root, b);
  291. }
  292. return 0;
  293. }
  294. int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
  295. {
  296. int ret;
  297. btrfs_commit_transaction(root, s);
  298. ret = commit_extent_and_tree_roots(root->tree_root, root->extent_root);
  299. BUG_ON(ret);
  300. write_ctree_super(root, s);
  301. drop_cache(root->extent_root);
  302. drop_cache(root->tree_root);
  303. drop_cache(root);
  304. BUG_ON(!list_empty(&root->trans));
  305. BUG_ON(!list_empty(&root->extent_root->trans));
  306. BUG_ON(!list_empty(&root->tree_root->trans));
  307. close(root->fp);
  308. if (root->node)
  309. btrfs_block_release(root, root->node);
  310. if (root->extent_root->node)
  311. btrfs_block_release(root->extent_root, root->extent_root->node);
  312. if (root->tree_root->node)
  313. btrfs_block_release(root->tree_root, root->tree_root->node);
  314. btrfs_block_release(root, root->commit_root);
  315. free(root);
  316. printf("on close %d blocks are allocated\n", allocated_blocks);
  317. return 0;
  318. }
  319. void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
  320. {
  321. buf->count--;
  322. if (buf->count < 0)
  323. BUG();
  324. if (buf->count == 0) {
  325. BUG_ON(!list_empty(&buf->cache));
  326. BUG_ON(!list_empty(&buf->dirty));
  327. if (!radix_tree_lookup(&root->cache_radix, buf->blocknr))
  328. BUG();
  329. radix_tree_delete(&root->cache_radix, buf->blocknr);
  330. memset(buf, 0, sizeof(*buf));
  331. free(buf);
  332. BUG_ON(allocated_blocks == 0);
  333. allocated_blocks--;
  334. BUG_ON(root->cache_size == 0);
  335. root->cache_size--;
  336. }
  337. }