disk-io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #include "transaction.h"
  13. static int allocated_blocks = 0;
  14. int cache_max = 10000;
  15. static int check_tree_block(struct btrfs_root *root, struct btrfs_buffer *buf)
  16. {
  17. if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
  18. BUG();
  19. if (root->node && btrfs_header_parentid(&buf->node.header) !=
  20. btrfs_header_parentid(&root->node->node.header))
  21. BUG();
  22. return 0;
  23. }
  24. static int free_some_buffers(struct btrfs_root *root)
  25. {
  26. struct list_head *node, *next;
  27. struct btrfs_buffer *b;
  28. if (root->fs_info->cache_size < cache_max)
  29. return 0;
  30. list_for_each_safe(node, next, &root->fs_info->cache) {
  31. b = list_entry(node, struct btrfs_buffer, cache);
  32. if (b->count == 1) {
  33. BUG_ON(!list_empty(&b->dirty));
  34. list_del_init(&b->cache);
  35. btrfs_block_release(root, b);
  36. if (root->fs_info->cache_size < cache_max)
  37. break;
  38. }
  39. }
  40. return 0;
  41. }
  42. struct btrfs_buffer *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
  43. {
  44. struct btrfs_buffer *buf;
  45. int ret;
  46. buf = malloc(sizeof(struct btrfs_buffer) + root->blocksize);
  47. if (!buf)
  48. return buf;
  49. allocated_blocks++;
  50. buf->blocknr = blocknr;
  51. buf->count = 2;
  52. INIT_LIST_HEAD(&buf->dirty);
  53. free_some_buffers(root);
  54. radix_tree_preload(GFP_KERNEL);
  55. ret = radix_tree_insert(&root->fs_info->cache_radix, blocknr, buf);
  56. radix_tree_preload_end();
  57. list_add_tail(&buf->cache, &root->fs_info->cache);
  58. root->fs_info->cache_size++;
  59. if (ret) {
  60. free(buf);
  61. return NULL;
  62. }
  63. return buf;
  64. }
  65. struct btrfs_buffer *find_tree_block(struct btrfs_root *root, u64 blocknr)
  66. {
  67. struct btrfs_buffer *buf;
  68. buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
  69. if (buf) {
  70. buf->count++;
  71. } else {
  72. buf = alloc_tree_block(root, blocknr);
  73. if (!buf) {
  74. BUG();
  75. return NULL;
  76. }
  77. }
  78. return buf;
  79. }
  80. struct btrfs_buffer *read_tree_block(struct btrfs_root *root, u64 blocknr)
  81. {
  82. loff_t offset = blocknr * root->blocksize;
  83. struct btrfs_buffer *buf;
  84. int ret;
  85. buf = radix_tree_lookup(&root->fs_info->cache_radix, blocknr);
  86. if (buf) {
  87. buf->count++;
  88. } else {
  89. buf = alloc_tree_block(root, blocknr);
  90. if (!buf)
  91. return NULL;
  92. ret = pread(root->fs_info->fp, &buf->node, root->blocksize,
  93. offset);
  94. if (ret != root->blocksize) {
  95. free(buf);
  96. return NULL;
  97. }
  98. }
  99. if (check_tree_block(root, buf))
  100. BUG();
  101. return buf;
  102. }
  103. int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  104. struct btrfs_buffer *buf)
  105. {
  106. if (!list_empty(&buf->dirty))
  107. return 0;
  108. list_add_tail(&buf->dirty, &root->fs_info->trans);
  109. buf->count++;
  110. return 0;
  111. }
  112. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  113. struct btrfs_buffer *buf)
  114. {
  115. if (!list_empty(&buf->dirty)) {
  116. list_del_init(&buf->dirty);
  117. btrfs_block_release(root, buf);
  118. }
  119. return 0;
  120. }
  121. int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  122. struct btrfs_buffer *buf)
  123. {
  124. u64 blocknr = buf->blocknr;
  125. loff_t offset = blocknr * root->blocksize;
  126. int ret;
  127. if (buf->blocknr != btrfs_header_blocknr(&buf->node.header))
  128. BUG();
  129. ret = pwrite(root->fs_info->fp, &buf->node, root->blocksize, offset);
  130. if (ret != root->blocksize)
  131. return ret;
  132. return 0;
  133. }
  134. static int __commit_transaction(struct btrfs_trans_handle *trans, struct
  135. btrfs_root *root)
  136. {
  137. struct btrfs_buffer *b;
  138. int ret = 0;
  139. int wret;
  140. while(!list_empty(&root->fs_info->trans)) {
  141. b = list_entry(root->fs_info->trans.next, struct btrfs_buffer,
  142. dirty);
  143. list_del_init(&b->dirty);
  144. wret = write_tree_block(trans, root, b);
  145. if (wret)
  146. ret = wret;
  147. btrfs_block_release(root, b);
  148. }
  149. return ret;
  150. }
  151. static int commit_tree_roots(struct btrfs_trans_handle *trans,
  152. struct btrfs_fs_info *fs_info)
  153. {
  154. int ret;
  155. u64 old_extent_block;
  156. struct btrfs_root *tree_root = fs_info->tree_root;
  157. struct btrfs_root *extent_root = fs_info->extent_root;
  158. struct btrfs_root *inode_root = fs_info->inode_root;
  159. btrfs_set_root_blocknr(&inode_root->root_item,
  160. inode_root->node->blocknr);
  161. ret = btrfs_update_root(trans, tree_root,
  162. &inode_root->root_key,
  163. &inode_root->root_item);
  164. BUG_ON(ret);
  165. while(1) {
  166. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  167. if (old_extent_block == extent_root->node->blocknr)
  168. break;
  169. btrfs_set_root_blocknr(&extent_root->root_item,
  170. extent_root->node->blocknr);
  171. ret = btrfs_update_root(trans, tree_root,
  172. &extent_root->root_key,
  173. &extent_root->root_item);
  174. BUG_ON(ret);
  175. }
  176. return 0;
  177. }
  178. int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
  179. btrfs_root *root, struct btrfs_super_block *s)
  180. {
  181. int ret = 0;
  182. struct btrfs_buffer *snap = root->commit_root;
  183. struct btrfs_key snap_key;
  184. if (root->commit_root == root->node)
  185. return 0;
  186. memcpy(&snap_key, &root->root_key, sizeof(snap_key));
  187. root->root_key.offset++;
  188. btrfs_set_root_blocknr(&root->root_item, root->node->blocknr);
  189. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  190. &root->root_key, &root->root_item);
  191. BUG_ON(ret);
  192. ret = commit_tree_roots(trans, root->fs_info);
  193. BUG_ON(ret);
  194. ret = __commit_transaction(trans, root);
  195. BUG_ON(ret);
  196. write_ctree_super(trans, root, s);
  197. btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
  198. btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
  199. root->commit_root = root->node;
  200. root->node->count++;
  201. ret = btrfs_drop_snapshot(trans, root, snap);
  202. BUG_ON(ret);
  203. ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
  204. BUG_ON(ret);
  205. root->fs_info->generation = root->root_key.offset + 1;
  206. return ret;
  207. }
  208. static int __setup_root(struct btrfs_super_block *super,
  209. struct btrfs_root *root,
  210. struct btrfs_fs_info *fs_info,
  211. u64 objectid, int fp)
  212. {
  213. root->node = NULL;
  214. root->commit_root = NULL;
  215. root->blocksize = btrfs_super_blocksize(super);
  216. root->ref_cows = 0;
  217. root->fs_info = fs_info;
  218. memset(&root->root_key, 0, sizeof(root->root_key));
  219. memset(&root->root_item, 0, sizeof(root->root_item));
  220. return 0;
  221. }
  222. static int find_and_setup_root(struct btrfs_super_block *super,
  223. struct btrfs_root *tree_root,
  224. struct btrfs_fs_info *fs_info,
  225. u64 objectid,
  226. struct btrfs_root *root, int fp)
  227. {
  228. int ret;
  229. __setup_root(super, root, fs_info, objectid, fp);
  230. ret = btrfs_find_last_root(tree_root, objectid,
  231. &root->root_item, &root->root_key);
  232. BUG_ON(ret);
  233. root->node = read_tree_block(root,
  234. btrfs_root_blocknr(&root->root_item));
  235. BUG_ON(!root->node);
  236. return 0;
  237. }
  238. struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
  239. {
  240. int fp;
  241. fp = open(filename, O_CREAT | O_RDWR, 0600);
  242. if (fp < 0) {
  243. return NULL;
  244. }
  245. return open_ctree_fd(fp, super);
  246. }
  247. struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super)
  248. {
  249. struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
  250. struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
  251. struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
  252. struct btrfs_root *inode_root = malloc(sizeof(struct btrfs_root));
  253. struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
  254. int ret;
  255. INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
  256. INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
  257. INIT_LIST_HEAD(&fs_info->trans);
  258. INIT_LIST_HEAD(&fs_info->cache);
  259. fs_info->cache_size = 0;
  260. fs_info->fp = fp;
  261. fs_info->running_transaction = NULL;
  262. fs_info->fs_root = root;
  263. fs_info->tree_root = tree_root;
  264. fs_info->extent_root = extent_root;
  265. fs_info->inode_root = inode_root;
  266. fs_info->last_inode_alloc = 0;
  267. fs_info->last_inode_alloc_dirid = 0;
  268. fs_info->disk_super = super;
  269. memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
  270. memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
  271. ret = pread(fp, super, sizeof(struct btrfs_super_block),
  272. BTRFS_SUPER_INFO_OFFSET);
  273. if (ret == 0 || btrfs_super_root(super) == 0) {
  274. BUG();
  275. return NULL;
  276. }
  277. BUG_ON(ret < 0);
  278. __setup_root(super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID, fp);
  279. tree_root->node = read_tree_block(tree_root, btrfs_super_root(super));
  280. BUG_ON(!tree_root->node);
  281. ret = find_and_setup_root(super, tree_root, fs_info,
  282. BTRFS_EXTENT_TREE_OBJECTID, extent_root, fp);
  283. BUG_ON(ret);
  284. ret = find_and_setup_root(super, tree_root, fs_info,
  285. BTRFS_INODE_MAP_OBJECTID, inode_root, fp);
  286. BUG_ON(ret);
  287. ret = find_and_setup_root(super, tree_root, fs_info,
  288. BTRFS_FS_TREE_OBJECTID, root, fp);
  289. BUG_ON(ret);
  290. root->commit_root = root->node;
  291. root->node->count++;
  292. root->ref_cows = 1;
  293. root->fs_info->generation = root->root_key.offset + 1;
  294. return root;
  295. }
  296. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  297. *root, struct btrfs_super_block *s)
  298. {
  299. int ret;
  300. btrfs_set_super_root(s, root->fs_info->tree_root->node->blocknr);
  301. ret = pwrite(root->fs_info->fp, s, sizeof(*s),
  302. BTRFS_SUPER_INFO_OFFSET);
  303. if (ret != sizeof(*s)) {
  304. fprintf(stderr, "failed to write new super block err %d\n", ret);
  305. return ret;
  306. }
  307. return 0;
  308. }
  309. static int drop_cache(struct btrfs_root *root)
  310. {
  311. while(!list_empty(&root->fs_info->cache)) {
  312. struct btrfs_buffer *b = list_entry(root->fs_info->cache.next,
  313. struct btrfs_buffer,
  314. cache);
  315. list_del_init(&b->cache);
  316. btrfs_block_release(root, b);
  317. }
  318. return 0;
  319. }
  320. int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s)
  321. {
  322. int ret;
  323. struct btrfs_trans_handle *trans;
  324. trans = root->fs_info->running_transaction;
  325. btrfs_commit_transaction(trans, root, s);
  326. ret = commit_tree_roots(trans, root->fs_info);
  327. BUG_ON(ret);
  328. ret = __commit_transaction(trans, root);
  329. BUG_ON(ret);
  330. write_ctree_super(trans, root, s);
  331. drop_cache(root);
  332. BUG_ON(!list_empty(&root->fs_info->trans));
  333. close(root->fs_info->fp);
  334. if (root->node)
  335. btrfs_block_release(root, root->node);
  336. if (root->fs_info->extent_root->node)
  337. btrfs_block_release(root->fs_info->extent_root,
  338. root->fs_info->extent_root->node);
  339. if (root->fs_info->inode_root->node)
  340. btrfs_block_release(root->fs_info->inode_root,
  341. root->fs_info->inode_root->node);
  342. if (root->fs_info->tree_root->node)
  343. btrfs_block_release(root->fs_info->tree_root,
  344. root->fs_info->tree_root->node);
  345. btrfs_block_release(root, root->commit_root);
  346. free(root);
  347. printf("on close %d blocks are allocated\n", allocated_blocks);
  348. return 0;
  349. }
  350. void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf)
  351. {
  352. buf->count--;
  353. if (buf->count < 0)
  354. BUG();
  355. if (buf->count == 0) {
  356. BUG_ON(!list_empty(&buf->cache));
  357. BUG_ON(!list_empty(&buf->dirty));
  358. if (!radix_tree_lookup(&root->fs_info->cache_radix,
  359. buf->blocknr))
  360. BUG();
  361. radix_tree_delete(&root->fs_info->cache_radix, buf->blocknr);
  362. memset(buf, 0, sizeof(*buf));
  363. free(buf);
  364. BUG_ON(allocated_blocks == 0);
  365. allocated_blocks--;
  366. BUG_ON(root->fs_info->cache_size == 0);
  367. root->fs_info->cache_size--;
  368. }
  369. }