disk-io.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. static int __setup_root(struct btrfs_super_block *super,
  46. struct btrfs_root *root,
  47. struct btrfs_fs_info *fs_info,
  48. u64 objectid)
  49. {
  50. root->node = NULL;
  51. root->commit_root = NULL;
  52. root->blocksize = btrfs_super_blocksize(super);
  53. root->ref_cows = 0;
  54. root->fs_info = fs_info;
  55. memset(&root->root_key, 0, sizeof(root->root_key));
  56. memset(&root->root_item, 0, sizeof(root->root_item));
  57. return 0;
  58. }
  59. static int find_and_setup_root(struct btrfs_super_block *super,
  60. struct btrfs_root *tree_root,
  61. struct btrfs_fs_info *fs_info,
  62. u64 objectid,
  63. struct btrfs_root *root)
  64. {
  65. int ret;
  66. __setup_root(super, root, fs_info, objectid);
  67. ret = btrfs_find_last_root(tree_root, objectid,
  68. &root->root_item, &root->root_key);
  69. BUG_ON(ret);
  70. root->node = read_tree_block(root,
  71. btrfs_root_blocknr(&root->root_item));
  72. BUG_ON(!root->node);
  73. return 0;
  74. }
  75. struct btrfs_root *open_ctree(struct super_block *sb,
  76. struct buffer_head *sb_buffer,
  77. struct btrfs_super_block *disk_super)
  78. {
  79. struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root),
  80. GFP_NOFS);
  81. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  82. GFP_NOFS);
  83. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  84. GFP_NOFS);
  85. struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root),
  86. GFP_NOFS);
  87. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  88. GFP_NOFS);
  89. int ret;
  90. /* FIXME: don't be stupid */
  91. if (!btrfs_super_root(disk_super))
  92. return NULL;
  93. INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
  94. fs_info->running_transaction = NULL;
  95. fs_info->fs_root = root;
  96. fs_info->tree_root = tree_root;
  97. fs_info->extent_root = extent_root;
  98. fs_info->inode_root = inode_root;
  99. fs_info->last_inode_alloc = 0;
  100. fs_info->last_inode_alloc_dirid = 0;
  101. fs_info->disk_super = disk_super;
  102. fs_info->sb_buffer = sb_buffer;
  103. fs_info->sb = sb;
  104. mutex_init(&fs_info->trans_mutex);
  105. memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert));
  106. memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert));
  107. __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
  108. tree_root->node = read_tree_block(tree_root,
  109. btrfs_super_root(disk_super));
  110. BUG_ON(!tree_root->node);
  111. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  112. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  113. BUG_ON(ret);
  114. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  115. BTRFS_INODE_MAP_OBJECTID, inode_root);
  116. BUG_ON(ret);
  117. ret = find_and_setup_root(disk_super, tree_root, fs_info,
  118. BTRFS_FS_TREE_OBJECTID, root);
  119. BUG_ON(ret);
  120. root->commit_root = root->node;
  121. get_bh(root->node);
  122. root->ref_cows = 1;
  123. root->fs_info->generation = root->root_key.offset + 1;
  124. return root;
  125. }
  126. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  127. *root)
  128. {
  129. return 0;
  130. #if 0
  131. int ret;
  132. btrfs_set_super_root(s, root->fs_info->tree_root->node->b_blocknr);
  133. ret = pwrite(root->fs_info->fp, s, sizeof(*s),
  134. BTRFS_SUPER_INFO_OFFSET);
  135. if (ret != sizeof(*s)) {
  136. fprintf(stderr, "failed to write new super block err %d\n", ret);
  137. return ret;
  138. }
  139. return 0;
  140. #endif
  141. }
  142. int close_ctree(struct btrfs_root *root)
  143. {
  144. int ret;
  145. struct btrfs_trans_handle *trans;
  146. trans = btrfs_start_transaction(root, 1);
  147. btrfs_commit_transaction(trans, root);
  148. /* run commit again to drop the original snapshot */
  149. trans = btrfs_start_transaction(root, 1);
  150. btrfs_commit_transaction(trans, root);
  151. ret = btrfs_write_and_wait_transaction(NULL, root);
  152. BUG_ON(ret);
  153. write_ctree_super(NULL, root);
  154. if (root->node)
  155. btrfs_block_release(root, root->node);
  156. if (root->fs_info->extent_root->node)
  157. btrfs_block_release(root->fs_info->extent_root,
  158. root->fs_info->extent_root->node);
  159. if (root->fs_info->inode_root->node)
  160. btrfs_block_release(root->fs_info->inode_root,
  161. root->fs_info->inode_root->node);
  162. if (root->fs_info->tree_root->node)
  163. btrfs_block_release(root->fs_info->tree_root,
  164. root->fs_info->tree_root->node);
  165. btrfs_block_release(root, root->commit_root);
  166. btrfs_block_release(root, root->fs_info->sb_buffer);
  167. kfree(root->fs_info->extent_root);
  168. kfree(root->fs_info->inode_root);
  169. kfree(root->fs_info->tree_root);
  170. kfree(root->fs_info);
  171. kfree(root);
  172. return 0;
  173. }
  174. void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf)
  175. {
  176. brelse(buf);
  177. }