disk-io.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. }
  15. return 0;
  16. }
  17. struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr)
  18. {
  19. return sb_getblk(root->fs_info->sb, blocknr);
  20. }
  21. struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr)
  22. {
  23. return sb_getblk(root->fs_info->sb, blocknr);
  24. }
  25. struct buffer_head *read_tree_block(struct btrfs_root *root, u64 blocknr)
  26. {
  27. struct buffer_head *buf = sb_bread(root->fs_info->sb, blocknr);
  28. if (!buf)
  29. return buf;
  30. if (check_tree_block(root, buf))
  31. BUG();
  32. return buf;
  33. }
  34. int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  35. struct buffer_head *buf)
  36. {
  37. mark_buffer_dirty(buf);
  38. return 0;
  39. }
  40. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  41. struct buffer_head *buf)
  42. {
  43. clear_buffer_dirty(buf);
  44. return 0;
  45. }
  46. static int __setup_root(struct btrfs_super_block *super,
  47. struct btrfs_root *root,
  48. struct btrfs_fs_info *fs_info,
  49. u64 objectid)
  50. {
  51. root->node = NULL;
  52. root->commit_root = NULL;
  53. root->blocksize = btrfs_super_blocksize(super);
  54. root->ref_cows = 0;
  55. root->fs_info = fs_info;
  56. memset(&root->root_key, 0, sizeof(root->root_key));
  57. memset(&root->root_item, 0, sizeof(root->root_item));
  58. return 0;
  59. }
  60. static int find_and_setup_root(struct btrfs_super_block *super,
  61. struct btrfs_root *tree_root,
  62. struct btrfs_fs_info *fs_info,
  63. u64 objectid,
  64. struct btrfs_root *root)
  65. {
  66. int ret;
  67. __setup_root(super, root, fs_info, objectid);
  68. ret = btrfs_find_last_root(tree_root, objectid,
  69. &root->root_item, &root->root_key);
  70. BUG_ON(ret);
  71. root->node = read_tree_block(root,
  72. btrfs_root_blocknr(&root->root_item));
  73. BUG_ON(!root->node);
  74. return 0;
  75. }
  76. struct btrfs_root *open_ctree(struct super_block *sb,
  77. struct buffer_head *sb_buffer,
  78. struct btrfs_super_block *disk_super)
  79. {
  80. struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
  81. GFP_NOFS);
  82. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  83. GFP_NOFS);
  84. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  85. GFP_NOFS);
  86. struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
  87. GFP_NOFS);
  88. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  89. GFP_NOFS);
  90. int ret;
  91. /* FIXME: don't be stupid */
  92. if (!btrfs_super_root(disk_super))
  93. return NULL;
  94. INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
  95. fs_info->running_transaction = NULL;
  96. fs_info->fs_root = root;
  97. fs_info->tree_root = tree_root;
  98. fs_info->extent_root = extent_root;
  99. fs_info->inode_root = inode_root;
  100. fs_info->last_inode_alloc = 0;
  101. fs_info->last_inode_alloc_dirid = 0;
  102. fs_info->disk_super = disk_super;
  103. fs_info->sb_buffer = sb_buffer;
  104. fs_info->sb = sb;
  105. mutex_init(&fs_info->trans_mutex);
  106. memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
  107. memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
  108. __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
  109. tree_root->node = read_tree_block(tree_root,
  110. btrfs_super_root(disk_super));
  111. BUG_ON(!tree_root->node);
  112. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  113. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  114. BUG_ON(ret);
  115. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  116. BTRFS_INODE_MAP_OBJECTID, inode_root);
  117. BUG_ON(ret);
  118. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  119. BTRFS_FS_TREE_OBJECTID, root);
  120. BUG_ON(ret);
  121. root->commit_root = root->node;
  122. get_bh(root->node);
  123. root->ref_cows = 1;
  124. root->fs_info->generation = root->root_key.offset + 1;
  125. return root;
  126. }
  127. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  128. *root)
  129. {
  130. struct buffer_head *bh = root->fs_info->sb_buffer;
  131. btrfs_set_super_root(root->fs_info->disk_super,
  132. root->fs_info->tree_root->node->b_blocknr);
  133. lock_buffer(bh);
  134. clear_buffer_dirty(bh);
  135. bh->b_end_io = end_buffer_write_sync;
  136. get_bh(bh);
  137. submit_bh(WRITE, bh);
  138. wait_on_buffer(bh);
  139. if (!buffer_uptodate(bh)) {
  140. WARN_ON(1);
  141. return -EIO;
  142. }
  143. return 0;
  144. }
  145. int close_ctree(struct btrfs_root *root)
  146. {
  147. int ret;
  148. struct btrfs_trans_handle *trans;
  149. trans = btrfs_start_transaction(root, 1);
  150. btrfs_commit_transaction(trans, root);
  151. /* run commit again to drop the original snapshot */
  152. trans = btrfs_start_transaction(root, 1);
  153. btrfs_commit_transaction(trans, root);
  154. ret = btrfs_write_and_wait_transaction(NULL, root);
  155. BUG_ON(ret);
  156. write_ctree_super(NULL, root);
  157. if (root->node)
  158. btrfs_block_release(root, root->node);
  159. if (root->fs_info->extent_root->node)
  160. btrfs_block_release(root->fs_info->extent_root,
  161. root->fs_info->extent_root->node);
  162. if (root->fs_info->inode_root->node)
  163. btrfs_block_release(root->fs_info->inode_root,
  164. root->fs_info->inode_root->node);
  165. if (root->fs_info->tree_root->node)
  166. btrfs_block_release(root->fs_info->tree_root,
  167. root->fs_info->tree_root->node);
  168. btrfs_block_release(root, root->commit_root);
  169. btrfs_block_release(root, root->fs_info->sb_buffer);
  170. kfree(root->fs_info->extent_root);
  171. kfree(root->fs_info->inode_root);
  172. kfree(root->fs_info->tree_root);
  173. kfree(root->fs_info);
  174. kfree(root);
  175. return 0;
  176. }
  177. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  178. {
  179. brelse(buf);
  180. }