Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/fs.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3 | #include "ctree.h" |
| 4 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 5 | #include "transaction.h" |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 6 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 7 | static int check_tree_block(struct btrfs_root *root, struct buffer_head *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 8 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 9 | struct btrfs_node *node = btrfs_buffer_node(buf); |
| 10 | if (buf->b_blocknr != btrfs_header_blocknr(&node->header)) |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 11 | BUG(); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 12 | if (root->node && btrfs_header_parentid(&node->header) != |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 13 | btrfs_header_parentid(btrfs_buffer_header(root->node))) { |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 14 | BUG(); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 15 | } |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 16 | return 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 17 | } |
| 18 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 19 | struct buffer_head *alloc_tree_block(struct btrfs_root *root, u64 blocknr) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 20 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 21 | return sb_getblk(root->fs_info->sb, blocknr); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 22 | } |
| 23 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 24 | struct buffer_head *find_tree_block(struct btrfs_root *root, u64 blocknr) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 25 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 26 | return sb_getblk(root->fs_info->sb, blocknr); |
| 27 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 28 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 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 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 33 | if (!buf) |
| 34 | return buf; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 35 | if (check_tree_block(root, buf)) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 36 | BUG(); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 37 | return buf; |
| 38 | } |
| 39 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 40 | int dirty_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 41 | struct buffer_head *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 42 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 43 | mark_buffer_dirty(buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 47 | int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 48 | struct buffer_head *buf) |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 49 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 50 | clear_buffer_dirty(buf); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 54 | static int __setup_root(struct btrfs_super_block *super, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 55 | struct btrfs_root *root, |
| 56 | struct btrfs_fs_info *fs_info, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 57 | u64 objectid) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 58 | { |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 59 | root->node = NULL; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 60 | root->commit_root = NULL; |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 61 | root->blocksize = btrfs_super_blocksize(super); |
| 62 | root->ref_cows = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 63 | root->fs_info = fs_info; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 64 | memset(&root->root_key, 0, sizeof(root->root_key)); |
| 65 | memset(&root->root_item, 0, sizeof(root->root_item)); |
| 66 | return 0; |
| 67 | } |
| 68 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 69 | static int find_and_setup_root(struct btrfs_super_block *super, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 70 | struct btrfs_root *tree_root, |
| 71 | struct btrfs_fs_info *fs_info, |
| 72 | u64 objectid, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 73 | struct btrfs_root *root) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 74 | { |
| 75 | int ret; |
| 76 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 77 | __setup_root(super, root, fs_info, objectid); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 78 | ret = btrfs_find_last_root(tree_root, objectid, |
| 79 | &root->root_item, &root->root_key); |
| 80 | BUG_ON(ret); |
| 81 | |
| 82 | root->node = read_tree_block(root, |
| 83 | btrfs_root_blocknr(&root->root_item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 84 | BUG_ON(!root->node); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 88 | struct btrfs_root *open_ctree(struct super_block *sb, |
| 89 | struct buffer_head *sb_buffer, |
| 90 | struct btrfs_super_block *disk_super) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 91 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 92 | struct btrfs_root *root = kmalloc(sizeof(struct btrfs_root), |
| 93 | GFP_NOFS); |
| 94 | struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root), |
| 95 | GFP_NOFS); |
| 96 | struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root), |
| 97 | GFP_NOFS); |
| 98 | struct btrfs_root *inode_root = kmalloc(sizeof(struct btrfs_root), |
| 99 | GFP_NOFS); |
| 100 | struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info), |
| 101 | GFP_NOFS); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 102 | int ret; |
| 103 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 104 | /* FIXME: don't be stupid */ |
| 105 | if (!btrfs_super_root(disk_super)) |
| 106 | return NULL; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame^] | 107 | init_bit_radix(&fs_info->pinned_radix); |
| 108 | init_bit_radix(&fs_info->pending_del_radix); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 109 | fs_info->running_transaction = NULL; |
| 110 | fs_info->fs_root = root; |
| 111 | fs_info->tree_root = tree_root; |
| 112 | fs_info->extent_root = extent_root; |
| 113 | fs_info->inode_root = inode_root; |
| 114 | fs_info->last_inode_alloc = 0; |
| 115 | fs_info->last_inode_alloc_dirid = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 116 | fs_info->disk_super = disk_super; |
| 117 | fs_info->sb_buffer = sb_buffer; |
| 118 | fs_info->sb = sb; |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 119 | mutex_init(&fs_info->trans_mutex); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 120 | mutex_init(&fs_info->fs_mutex); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 121 | memset(&fs_info->current_insert, 0, sizeof(fs_info->current_insert)); |
| 122 | memset(&fs_info->last_insert, 0, sizeof(fs_info->last_insert)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 123 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 124 | __setup_root(disk_super, tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID); |
| 125 | tree_root->node = read_tree_block(tree_root, |
| 126 | btrfs_super_root(disk_super)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 127 | BUG_ON(!tree_root->node); |
| 128 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 129 | ret = find_and_setup_root(disk_super, tree_root, fs_info, |
| 130 | BTRFS_EXTENT_TREE_OBJECTID, extent_root); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 131 | BUG_ON(ret); |
| 132 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 133 | ret = find_and_setup_root(disk_super, tree_root, fs_info, |
| 134 | BTRFS_INODE_MAP_OBJECTID, inode_root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 135 | BUG_ON(ret); |
| 136 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 137 | ret = find_and_setup_root(disk_super, tree_root, fs_info, |
| 138 | BTRFS_FS_TREE_OBJECTID, root); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 139 | BUG_ON(ret); |
| 140 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 141 | root->commit_root = root->node; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 142 | get_bh(root->node); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 143 | root->ref_cows = 1; |
Chris Mason | 293ffd5 | 2007-03-20 15:57:25 -0400 | [diff] [blame] | 144 | root->fs_info->generation = root->root_key.offset + 1; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 145 | return root; |
| 146 | } |
| 147 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 148 | int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 149 | *root) |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 150 | { |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 151 | struct buffer_head *bh = root->fs_info->sb_buffer; |
| 152 | btrfs_set_super_root(root->fs_info->disk_super, |
| 153 | root->fs_info->tree_root->node->b_blocknr); |
| 154 | lock_buffer(bh); |
| 155 | clear_buffer_dirty(bh); |
| 156 | bh->b_end_io = end_buffer_write_sync; |
| 157 | get_bh(bh); |
| 158 | submit_bh(WRITE, bh); |
| 159 | wait_on_buffer(bh); |
| 160 | if (!buffer_uptodate(bh)) { |
| 161 | WARN_ON(1); |
| 162 | return -EIO; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 167 | int close_ctree(struct btrfs_root *root) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 168 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 169 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 170 | struct btrfs_trans_handle *trans; |
| 171 | |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 172 | trans = btrfs_start_transaction(root, 1); |
| 173 | btrfs_commit_transaction(trans, root); |
| 174 | /* run commit again to drop the original snapshot */ |
| 175 | trans = btrfs_start_transaction(root, 1); |
| 176 | btrfs_commit_transaction(trans, root); |
| 177 | ret = btrfs_write_and_wait_transaction(NULL, root); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 178 | BUG_ON(ret); |
Chris Mason | 79154b1 | 2007-03-22 15:59:16 -0400 | [diff] [blame] | 179 | write_ctree_super(NULL, root); |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 180 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 181 | if (root->node) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 182 | btrfs_block_release(root, root->node); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 183 | if (root->fs_info->extent_root->node) |
| 184 | btrfs_block_release(root->fs_info->extent_root, |
| 185 | root->fs_info->extent_root->node); |
| 186 | if (root->fs_info->inode_root->node) |
| 187 | btrfs_block_release(root->fs_info->inode_root, |
| 188 | root->fs_info->inode_root->node); |
| 189 | if (root->fs_info->tree_root->node) |
| 190 | btrfs_block_release(root->fs_info->tree_root, |
| 191 | root->fs_info->tree_root->node); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 192 | btrfs_block_release(root, root->commit_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 193 | btrfs_block_release(root, root->fs_info->sb_buffer); |
| 194 | kfree(root->fs_info->extent_root); |
| 195 | kfree(root->fs_info->inode_root); |
| 196 | kfree(root->fs_info->tree_root); |
| 197 | kfree(root->fs_info); |
| 198 | kfree(root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 202 | void btrfs_block_release(struct btrfs_root *root, struct buffer_head *buf) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 203 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 204 | brelse(buf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 205 | } |
| 206 | |