disk-io.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include "ctree.h"
  4. #include "disk-io.h"
  5. #include "transaction.h"
  6. static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf)
  7. {
  8. struct btrfs_node *node = btrfs_buffer_node(buf);
  9. if (buf->b_blocknr != btrfs_header_blocknr(&node->header))
  10. BUG();
  11. if (root->node && btrfs_header_parentid(&node->header) !=
  12. btrfs_header_parentid(btrfs_buffer_header(root->node)))
  13. BUG();
  14. return 0;
  15. }
  16. struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
  17. {
  18. return sb_getblk(root->fs_info->sb, blocknr);
  19. }
  20. struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
  21. {
  22. return sb_getblk(root->fs_info->sb, blocknr);
  23. }
  24. struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
  25. {
  26. struct buffer_head *buf = sb_bread(root->fs_info->sb, blocknr);
  27. if (!buf)
  28. return buf;
  29. if (check_tree_block(root, buf))
  30. BUG();
  31. return buf;
  32. }
  33. int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  34. struct buffer_head *buf)
  35. {
  36. mark_buffer_dirty(buf);
  37. return 0;
  38. }
  39. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  40. struct buffer_head *buf)
  41. {
  42. clear_buffer_dirty(buf);
  43. return 0;
  44. }
  45. int write_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  46. struct buffer_head *buf)
  47. {
  48. mark_buffer_dirty(buf);
  49. return 0;
  50. }
  51. static int __commit_transaction(struct btrfs_trans_handle *trans, struct
  52. btrfs_root *root)
  53. {
  54. filemap_write_and_wait(root->fs_info->sb->s_bdev->bd_inode->i_mapping);
  55. return 0;
  56. }
  57. static int commit_tree_roots(struct btrfs_trans_handle *trans,
  58. struct btrfs_fs_info *fs_info)
  59. {
  60. int ret;
  61. u64 old_extent_block;
  62. struct btrfs_root *tree_root = fs_info->tree_root;
  63. struct btrfs_root *extent_root = fs_info->extent_root;
  64. struct btrfs_root *inode_root = fs_info->inode_root;
  65. btrfs_set_root_blocknr(&inode_root->root_item,
  66. inode_root->node->b_blocknr);
  67. ret = btrfs_update_root(trans, tree_root,
  68. &inode_root->root_key,
  69. &inode_root->root_item);
  70. BUG_ON(ret);
  71. while(1) {
  72. old_extent_block = btrfs_root_blocknr(&extent_root->root_item);
  73. if (old_extent_block == extent_root->node->b_blocknr)
  74. break;
  75. btrfs_set_root_blocknr(&extent_root->root_item,
  76. extent_root->node->b_blocknr);
  77. ret = btrfs_update_root(trans, tree_root,
  78. &extent_root->root_key,
  79. &extent_root->root_item);
  80. BUG_ON(ret);
  81. }
  82. return 0;
  83. }
  84. int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct
  85. btrfs_root *root, struct btrfs_super_block *s)
  86. {
  87. int ret = 0;
  88. struct buffer_head *snap = root->commit_root;
  89. struct btrfs_key snap_key;
  90. if (root->commit_root == root->node)
  91. return 0;
  92. memcpy(&snap_key, &root->root_key, sizeof(snap_key));
  93. root->root_key.offset++;
  94. btrfs_set_root_blocknr(&root->root_item, root->node->b_blocknr);
  95. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  96. &root->root_key, &root->root_item);
  97. BUG_ON(ret);
  98. ret = commit_tree_roots(trans, root->fs_info);
  99. BUG_ON(ret);
  100. ret = __commit_transaction(trans, root);
  101. BUG_ON(ret);
  102. write_ctree_super(trans, root, s);
  103. btrfs_finish_extent_commit(trans, root->fs_info->extent_root);
  104. btrfs_finish_extent_commit(trans, root->fs_info->tree_root);
  105. root->commit_root = root->node;
  106. get_bh(root->node);
  107. ret = btrfs_drop_snapshot(trans, root, snap);
  108. BUG_ON(ret);
  109. ret = btrfs_del_root(trans, root->fs_info->tree_root, &snap_key);
  110. BUG_ON(ret);
  111. root->fs_info->generation = root->root_key.offset + 1;
  112. return ret;
  113. }
  114. static int __setup_root(struct btrfs_super_block *super,
  115. struct btrfs_root *root,
  116. struct btrfs_fs_info *fs_info,
  117. u64 objectid)
  118. {
  119. root->node = NULL;
  120. root->commit_root = NULL;
  121. root->blocksize = btrfs_super_blocksize(super);
  122. root->ref_cows = 0;
  123. root->fs_info = fs_info;
  124. memset(&root->root_key, 0, sizeof(root->root_key));
  125. memset(&root->root_item, 0, sizeof(root->root_item));
  126. return 0;
  127. }
  128. static int find_and_setup_root(struct btrfs_super_block *super,
  129. struct btrfs_root *tree_root,
  130. struct btrfs_fs_info *fs_info,
  131. u64 objectid,
  132. struct btrfs_root *root)
  133. {
  134. int ret;
  135. __setup_root(super, root, fs_info, objectid);
  136. ret = btrfs_find_last_root(tree_root, objectid,
  137. &root->root_item, &root->root_key);
  138. BUG_ON(ret);
  139. root->node = read_tree_block(root,
  140. btrfs_root_blocknr(&root->root_item));
  141. BUG_ON(!root->node);
  142. return 0;
  143. }
  144. struct btrfs_root *open_ctree(struct super_block *sb,
  145. struct buffer_head *sb_buffer,
  146. struct btrfs_super_block *disk_super)
  147. {
  148. struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
  149. GFP_NOFS);
  150. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  151. GFP_NOFS);
  152. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  153. GFP_NOFS);
  154. struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
  155. GFP_NOFS);
  156. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  157. GFP_NOFS);
  158. int ret;
  159. /* FIXME: don't be stupid */
  160. if (!btrfs_super_root(disk_super))
  161. return NULL;
  162. INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
  163. fs_info->running_transaction = NULL;
  164. fs_info->fs_root = root;
  165. fs_info->tree_root = tree_root;
  166. fs_info->extent_root = extent_root;
  167. fs_info->inode_root = inode_root;
  168. fs_info->last_inode_alloc = 0;
  169. fs_info->last_inode_alloc_dirid = 0;
  170. fs_info->disk_super = disk_super;
  171. fs_info->sb_buffer = sb_buffer;
  172. fs_info->sb = sb;
  173. memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
  174. memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
  175. __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
  176. tree_root->node = read_tree_block(tree_root,
  177. btrfs_super_root(disk_super));
  178. BUG_ON(!tree_root->node);
  179. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  180. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  181. BUG_ON(ret);
  182. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  183. BTRFS_INODE_MAP_OBJECTID, inode_root);
  184. BUG_ON(ret);
  185. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  186. BTRFS_FS_TREE_OBJECTID, root);
  187. BUG_ON(ret);
  188. root->commit_root = root->node;
  189. get_bh(root->node);
  190. root->ref_cows = 1;
  191. root->fs_info->generation = root->root_key.offset + 1;
  192. return root;
  193. }
  194. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  195. *root, struct btrfs_super_block *s)
  196. {
  197. return 0;
  198. #if 0
  199. int ret;
  200. btrfs_set_super_root(s, root->fs_info->tree_root->node->b_blocknr);
  201. ret = pwrite(root->fs_info->fp, s, sizeof(*s),
  202. BTRFS_SUPER_INFO_OFFSET);
  203. if (ret != sizeof(*s)) {
  204. fprintf(stderr, "failed to write new super block err %d\n", ret);
  205. return ret;
  206. }
  207. return 0;
  208. #endif
  209. }
  210. static int drop_cache(struct btrfs_root *root)
  211. {
  212. return 0;
  213. #if 0
  214. while(!list_empty(&root->fs_info->cache)) {
  215. struct buffer_head *b = list_entry(root->fs_info->cache.next,
  216. struct buffer_head,
  217. cache);
  218. list_del_init(&b->cache);
  219. btrfs_block_release(root, b);
  220. }
  221. return 0;
  222. #endif
  223. }
  224. int close_ctree(struct btrfs_root *root)
  225. {
  226. int ret;
  227. struct btrfs_trans_handle *trans;
  228. trans = root->fs_info->running_transaction;
  229. btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
  230. ret = commit_tree_roots(trans, root->fs_info);
  231. BUG_ON(ret);
  232. ret = __commit_transaction(trans, root);
  233. BUG_ON(ret);
  234. write_ctree_super(trans, root, root->fs_info->disk_super);
  235. drop_cache(root);
  236. if (root->node)
  237. btrfs_block_release(root, root->node);
  238. if (root->fs_info->extent_root->node)
  239. btrfs_block_release(root->fs_info->extent_root,
  240. root->fs_info->extent_root->node);
  241. if (root->fs_info->inode_root->node)
  242. btrfs_block_release(root->fs_info->inode_root,
  243. root->fs_info->inode_root->node);
  244. if (root->fs_info->tree_root->node)
  245. btrfs_block_release(root->fs_info->tree_root,
  246. root->fs_info->tree_root->node);
  247. btrfs_block_release(root, root->commit_root);
  248. btrfs_block_release(root, root->fs_info->sb_buffer);
  249. kfree(root->fs_info->extent_root);
  250. kfree(root->fs_info->inode_root);
  251. kfree(root->fs_info->tree_root);
  252. kfree(root->fs_info);
  253. kfree(root);
  254. return 0;
  255. }
  256. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  257. {
  258. brelse(buf);
  259. }