disk-io.c 5.8 KB

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